blob: 2c19e1168b5a57681e644e38775c2e2ebc64d655 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "Method.h"
18
Andreas Huber3599d922016-08-09 10:42:57 -070019#include "Annotation.h"
Timur Iskhakov891a8662017-08-25 21:53:48 -070020#include "ConstantExpression.h"
Neel Mehta3b414a82019-07-02 15:47:48 -070021#include "FormattingConstants.h"
Neel Mehtafff486d2019-07-23 14:02:49 -070022#include "Reference.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070023#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070024#include "Type.h"
25
Yifan Hong10fe0b52016-10-19 14:20:17 -070026#include <android-base/logging.h>
Neel Mehta3b414a82019-07-02 15:47:48 -070027#include <hidl-util/FQName.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070029#include <algorithm>
Neel Mehta69920a62019-07-22 16:22:13 -070030#include <string>
Neel Mehta3b414a82019-07-02 15:47:48 -070031#include <vector>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070032
Andreas Huberc9410c72016-07-28 12:18:40 -070033namespace android {
34
Neel Mehta69920a62019-07-22 16:22:13 -070035Method::Method(const std::string& name, std::vector<NamedReference<Type>*>* args,
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070036 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070037 std::vector<Annotation*>* annotations, const Location& location)
38 : mName(name),
39 mArgs(args),
40 mResults(results),
41 mOneway(oneway),
42 mAnnotations(annotations),
43 mLocation(location) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070044
Yifan Hongffa91392017-01-31 13:41:23 -080045void Method::fillImplementation(
46 size_t serial,
47 MethodImpl cppImpl,
48 MethodImpl javaImpl) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070049 mIsHidlReserved = true;
50 mSerial = serial;
51 mCppImpl = cppImpl;
52 mJavaImpl = javaImpl;
Yifan Hongcd2ae452017-01-31 14:33:40 -080053
54 CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end())
Steven Moreland937408a2017-03-20 09:54:18 -070055 << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead.";
Yifan Hongcd2ae452017-01-31 14:33:40 -080056 CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() ||
57 mCppImpl.find(IMPL_STUB) == mCppImpl.end())
58 << "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL.";
Yifan Hong10fe0b52016-10-19 14:20:17 -070059}
60
Andreas Huber881227d2016-08-02 14:20:21 -070061std::string Method::name() const {
62 return mName;
63}
64
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070065const std::vector<NamedReference<Type>*>& Method::args() const {
Andreas Huber881227d2016-08-02 14:20:21 -070066 return *mArgs;
67}
68
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070069const std::vector<NamedReference<Type>*>& Method::results() const {
Andreas Huber881227d2016-08-02 14:20:21 -070070 return *mResults;
71}
72
Steven Morelandd537ab02016-09-12 10:32:01 -070073const std::vector<Annotation *> &Method::annotations() const {
74 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070075}
76
Timur Iskhakovb58f4182017-08-29 15:19:24 -070077std::vector<Reference<Type>*> Method::getReferences() {
78 const auto& constRet = static_cast<const Method*>(this)->getReferences();
79 std::vector<Reference<Type>*> ret(constRet.size());
80 std::transform(constRet.begin(), constRet.end(), ret.begin(),
81 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
Timur Iskhakov33431e62017-08-21 17:31:23 -070082 return ret;
83}
84
Timur Iskhakovb58f4182017-08-29 15:19:24 -070085std::vector<const Reference<Type>*> Method::getReferences() const {
86 std::vector<const Reference<Type>*> ret;
87 ret.insert(ret.end(), mArgs->begin(), mArgs->end());
88 ret.insert(ret.end(), mResults->begin(), mResults->end());
89 return ret;
90}
91
Timur Iskhakovff5e64a2017-09-11 14:56:18 -070092std::vector<Reference<Type>*> Method::getStrongReferences() {
93 const auto& constRet = static_cast<const Method*>(this)->getStrongReferences();
94 std::vector<Reference<Type>*> ret(constRet.size());
95 std::transform(constRet.begin(), constRet.end(), ret.begin(),
96 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
97 return ret;
98}
99
100std::vector<const Reference<Type>*> Method::getStrongReferences() const {
101 std::vector<const Reference<Type>*> ret;
102 for (const auto* ref : getReferences()) {
103 if (!ref->shallowGet()->isNeverStrongReference()) {
104 ret.push_back(ref);
105 }
106 }
107 return ret;
108}
109
Martijn Coenen115d4282016-12-19 05:14:04 +0100110void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700111 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100112 auto it = mCppImpl.find(type);
113 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100114 if (it->second != nullptr) {
115 it->second(out);
116 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700117 }
118}
119
Martijn Coenen115d4282016-12-19 05:14:04 +0100120void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700121 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100122 auto it = mJavaImpl.find(type);
123 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100124 if (it->second != nullptr) {
125 it->second(out);
126 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700127 }
128}
129
Martijn Coenen115d4282016-12-19 05:14:04 +0100130bool Method::overridesCppImpl(MethodImplType type) const {
131 CHECK(mIsHidlReserved);
132 return mCppImpl.find(type) != mCppImpl.end();
133}
134
135bool Method::overridesJavaImpl(MethodImplType type) const {
136 CHECK(mIsHidlReserved);
137 return mJavaImpl.find(type) != mJavaImpl.end();
138}
139
Steven Moreland7645fbd2019-03-12 18:49:28 -0700140Method* Method::copySignature() const {
Greg Kaiserac0dfbf2019-07-23 06:25:48 -0700141 Method* method = new Method(mName, mArgs, mResults, mOneway, mAnnotations, location());
Steven Moreland7645fbd2019-03-12 18:49:28 -0700142 method->setDocComment(getDocComment());
143 return method;
Yifan Hongffa91392017-01-31 13:41:23 -0800144}
145
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700146void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700147 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700148 mSerial = serial;
149}
150
151size_t Method::getSerialId() const {
152 return mSerial;
153}
154
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700155bool Method::hasEmptyCppArgSignature() const {
156 return args().empty() && (results().empty() || canElideCallback() != nullptr);
157}
Steven Morelanda7a421a2016-09-07 08:35:18 -0700158
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700159void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700160 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700161 const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700162
Steven Morelanda7a421a2016-09-07 08:35:18 -0700163 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700164 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700165 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700166 out << space
167 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700168 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700169 << "> ";
170 }
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700171}
172
173void Method::generateCppSignature(Formatter &out,
174 const std::string &className,
175 bool specifyNamespaces) const {
176 generateCppReturnType(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700177
178 if (!className.empty()) {
179 out << className << "::";
180 }
181
182 out << name()
Yifan Hong932464e2017-03-30 15:40:22 -0700183 << "(";
184 emitCppArgSignature(out, specifyNamespaces);
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800185 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700186}
187
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700188static void emitCppArgResultSignature(Formatter& out,
189 const std::vector<NamedReference<Type>*>& args,
190 bool specifyNamespaces) {
Yifan Hong932464e2017-03-30 15:40:22 -0700191 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
192 out << arg->type().getCppArgumentType(specifyNamespaces);
193 out << " ";
194 out << arg->name();
195 });
Andreas Huber881227d2016-08-02 14:20:21 -0700196}
197
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700198static void emitJavaArgResultSignature(Formatter& out,
199 const std::vector<NamedReference<Type>*>& args) {
Yifan Hong932464e2017-03-30 15:40:22 -0700200 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
201 out << arg->type().getJavaType();
202 out << " ";
203 out << arg->name();
204 });
205}
Andreas Huber2831d512016-08-15 09:33:47 -0700206
Yifan Hong932464e2017-03-30 15:40:22 -0700207void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
208 emitCppArgResultSignature(out, args(), specifyNamespaces);
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700209
210 const bool returnsValue = !results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700211 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700212 if (returnsValue && elidedReturn == nullptr) {
213 if (!args().empty()) {
214 out << ", ";
215 }
216
217 out << name() << "_cb _hidl_cb";
218 }
Yifan Hong932464e2017-03-30 15:40:22 -0700219}
220void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
221 emitCppArgResultSignature(out, results(), specifyNamespaces);
222}
223void Method::emitJavaArgSignature(Formatter &out) const {
224 emitJavaArgResultSignature(out, args());
225}
226void Method::emitJavaResultSignature(Formatter &out) const {
227 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700228}
229
Neel Mehta5b447c02019-05-23 16:12:24 -0700230void Method::emitJavaSignature(Formatter& out) const {
231 const bool returnsValue = !results().empty();
232 const bool needsCallback = results().size() > 1;
233
234 if (returnsValue && !needsCallback) {
235 out << results()[0]->type().getJavaType();
236 } else {
237 out << "void";
238 }
239
240 out << " " << name() << "(";
241 emitJavaArgSignature(out);
242
243 if (needsCallback) {
244 if (!args().empty()) {
245 out << ", ";
246 }
247
248 out << name() << "Callback _hidl_cb";
249 }
250
251 out << ")";
252}
253
Neel Mehta3b414a82019-07-02 15:47:48 -0700254static void fillHidlArgResultTokens(const std::vector<NamedReference<Type>*>& args,
Neel Mehtafff486d2019-07-23 14:02:49 -0700255 WrappedOutput* wrappedOutput, const std::string& attachToLast) {
256 for (size_t i = 0; i < args.size(); i++) {
257 const NamedReference<Type>* arg = args[i];
Neel Mehta3b414a82019-07-02 15:47:48 -0700258 std::string out = arg->localName() + " " + arg->name();
Neel Mehtafff486d2019-07-23 14:02:49 -0700259 wrappedOutput->group([&] {
260 if (i != 0) wrappedOutput->printUnlessWrapped(" ");
261 *wrappedOutput << out;
262 if (i == args.size() - 1) {
263 if (!attachToLast.empty()) *wrappedOutput << attachToLast;
264 } else {
265 *wrappedOutput << ",";
266 }
267 });
Neel Mehta3b414a82019-07-02 15:47:48 -0700268 }
269}
270
271void Method::emitHidlDefinition(Formatter& out) const {
272 if (getDocComment() != nullptr) getDocComment()->emit(out);
273
274 out.join(mAnnotations->begin(), mAnnotations->end(), "\n",
275 [&](auto annotation) { annotation->dump(out); });
276 if (!mAnnotations->empty()) out << "\n";
277
278 WrappedOutput wrappedOutput(MAX_LINE_LENGTH);
279
280 if (isOneway()) wrappedOutput << "oneway ";
281 wrappedOutput << name() << "(";
282
Neel Mehtafff486d2019-07-23 14:02:49 -0700283 if (!args().empty()) {
284 fillHidlArgResultTokens(args(), &wrappedOutput, results().empty() ? ");\n" : ")");
285 } else {
286 wrappedOutput << (results().empty() ? ");\n" : ")");
287 }
Neel Mehta3b414a82019-07-02 15:47:48 -0700288
289 if (!results().empty()) {
290 wrappedOutput.group([&] {
291 wrappedOutput.printUnlessWrapped(" ");
292 wrappedOutput << "generates (";
Neel Mehtafff486d2019-07-23 14:02:49 -0700293 fillHidlArgResultTokens(results(), &wrappedOutput, ");\n");
Neel Mehta3b414a82019-07-02 15:47:48 -0700294 });
295 }
296
Neel Mehta3b414a82019-07-02 15:47:48 -0700297 out << wrappedOutput;
298}
299
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700300bool Method::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700301 if (!std::all_of(mArgs->begin(), mArgs->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700302 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700303 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700304 }
305
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700306 if (!std::all_of(mResults->begin(), mResults->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700307 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700308 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700309 }
310
311 return true;
312}
313
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700314const NamedReference<Type>* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700315 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800316 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700317 return nullptr;
318 }
319
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700320 const NamedReference<Type>* typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700321
Steven Moreland9df52442016-12-12 08:51:14 -0800322 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100323 return typedVar;
324 }
325
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700326 return nullptr;
327}
328
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700329const Location& Method::location() const {
330 return mLocation;
331}
332
Andreas Huber31629bc2016-08-03 09:06:40 -0700333////////////////////////////////////////////////////////////////////////////////
334
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700335bool TypedVarVector::add(NamedReference<Type>* v) {
Yifan Hong7763ab32016-12-13 17:42:11 -0800336 if (mNames.emplace(v->name()).second) {
337 push_back(v);
338 return true;
339 }
340 return false;
341}
342
Andreas Huberc9410c72016-07-28 12:18:40 -0700343} // namespace android
344