blob: 9717beb31569d734b42cd530924cff3667704bee [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
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700110std::vector<ConstantExpression*> Method::getConstantExpressions() {
111 const auto& constRet = static_cast<const Method*>(this)->getConstantExpressions();
112 std::vector<ConstantExpression*> ret(constRet.size());
113 std::transform(constRet.begin(), constRet.end(), ret.begin(),
114 [](const auto* ce) { return const_cast<ConstantExpression*>(ce); });
115 return ret;
116}
117
118std::vector<const ConstantExpression*> Method::getConstantExpressions() const {
119 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700120 for (const auto* annotation : *mAnnotations) {
121 const auto& retAnnotation = annotation->getConstantExpressions();
122 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700123 }
Timur Iskhakov891a8662017-08-25 21:53:48 -0700124 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700125}
126
Martijn Coenen115d4282016-12-19 05:14:04 +0100127void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700128 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100129 auto it = mCppImpl.find(type);
130 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100131 if (it->second != nullptr) {
132 it->second(out);
133 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700134 }
135}
136
Martijn Coenen115d4282016-12-19 05:14:04 +0100137void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700138 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100139 auto it = mJavaImpl.find(type);
140 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100141 if (it->second != nullptr) {
142 it->second(out);
143 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700144 }
145}
146
Martijn Coenen115d4282016-12-19 05:14:04 +0100147bool Method::overridesCppImpl(MethodImplType type) const {
148 CHECK(mIsHidlReserved);
149 return mCppImpl.find(type) != mCppImpl.end();
150}
151
152bool Method::overridesJavaImpl(MethodImplType type) const {
153 CHECK(mIsHidlReserved);
154 return mJavaImpl.find(type) != mJavaImpl.end();
155}
156
Steven Moreland7645fbd2019-03-12 18:49:28 -0700157Method* Method::copySignature() const {
158 Method* method = new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, location());
159 method->setDocComment(getDocComment());
160 return method;
Yifan Hongffa91392017-01-31 13:41:23 -0800161}
162
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700163void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700164 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700165 mSerial = serial;
166}
167
168size_t Method::getSerialId() const {
169 return mSerial;
170}
171
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700172bool Method::hasEmptyCppArgSignature() const {
173 return args().empty() && (results().empty() || canElideCallback() != nullptr);
174}
Steven Morelanda7a421a2016-09-07 08:35:18 -0700175
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700176void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700177 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700178 const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700179
Steven Morelanda7a421a2016-09-07 08:35:18 -0700180 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700181 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700182 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700183 out << space
184 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700185 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700186 << "> ";
187 }
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700188}
189
190void Method::generateCppSignature(Formatter &out,
191 const std::string &className,
192 bool specifyNamespaces) const {
193 generateCppReturnType(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700194
195 if (!className.empty()) {
196 out << className << "::";
197 }
198
199 out << name()
Yifan Hong932464e2017-03-30 15:40:22 -0700200 << "(";
201 emitCppArgSignature(out, specifyNamespaces);
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800202 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700203}
204
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700205static void emitCppArgResultSignature(Formatter& out,
206 const std::vector<NamedReference<Type>*>& args,
207 bool specifyNamespaces) {
Yifan Hong932464e2017-03-30 15:40:22 -0700208 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
209 out << arg->type().getCppArgumentType(specifyNamespaces);
210 out << " ";
211 out << arg->name();
212 });
Andreas Huber881227d2016-08-02 14:20:21 -0700213}
214
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700215static void emitJavaArgResultSignature(Formatter& out,
216 const std::vector<NamedReference<Type>*>& args) {
Yifan Hong932464e2017-03-30 15:40:22 -0700217 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
218 out << arg->type().getJavaType();
219 out << " ";
220 out << arg->name();
221 });
222}
Andreas Huber2831d512016-08-15 09:33:47 -0700223
Yifan Hong932464e2017-03-30 15:40:22 -0700224void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
225 emitCppArgResultSignature(out, args(), specifyNamespaces);
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700226
227 const bool returnsValue = !results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700228 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700229 if (returnsValue && elidedReturn == nullptr) {
230 if (!args().empty()) {
231 out << ", ";
232 }
233
234 out << name() << "_cb _hidl_cb";
235 }
Yifan Hong932464e2017-03-30 15:40:22 -0700236}
237void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
238 emitCppArgResultSignature(out, results(), specifyNamespaces);
239}
240void Method::emitJavaArgSignature(Formatter &out) const {
241 emitJavaArgResultSignature(out, args());
242}
243void Method::emitJavaResultSignature(Formatter &out) const {
244 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700245}
246
Neel Mehta5b447c02019-05-23 16:12:24 -0700247void Method::emitJavaSignature(Formatter& out) const {
248 const bool returnsValue = !results().empty();
249 const bool needsCallback = results().size() > 1;
250
251 if (returnsValue && !needsCallback) {
252 out << results()[0]->type().getJavaType();
253 } else {
254 out << "void";
255 }
256
257 out << " " << name() << "(";
258 emitJavaArgSignature(out);
259
260 if (needsCallback) {
261 if (!args().empty()) {
262 out << ", ";
263 }
264
265 out << name() << "Callback _hidl_cb";
266 }
267
268 out << ")";
269}
270
Neel Mehta3b414a82019-07-02 15:47:48 -0700271static void fillHidlArgResultTokens(const std::vector<NamedReference<Type>*>& args,
Neel Mehtafff486d2019-07-23 14:02:49 -0700272 WrappedOutput* wrappedOutput, const std::string& attachToLast) {
273 for (size_t i = 0; i < args.size(); i++) {
274 const NamedReference<Type>* arg = args[i];
Neel Mehta3b414a82019-07-02 15:47:48 -0700275 std::string out = arg->localName() + " " + arg->name();
Neel Mehtafff486d2019-07-23 14:02:49 -0700276 wrappedOutput->group([&] {
277 if (i != 0) wrappedOutput->printUnlessWrapped(" ");
278 *wrappedOutput << out;
279 if (i == args.size() - 1) {
280 if (!attachToLast.empty()) *wrappedOutput << attachToLast;
281 } else {
282 *wrappedOutput << ",";
283 }
284 });
Neel Mehta3b414a82019-07-02 15:47:48 -0700285 }
286}
287
288void Method::emitHidlDefinition(Formatter& out) const {
289 if (getDocComment() != nullptr) getDocComment()->emit(out);
290
291 out.join(mAnnotations->begin(), mAnnotations->end(), "\n",
292 [&](auto annotation) { annotation->dump(out); });
293 if (!mAnnotations->empty()) out << "\n";
294
295 WrappedOutput wrappedOutput(MAX_LINE_LENGTH);
296
297 if (isOneway()) wrappedOutput << "oneway ";
298 wrappedOutput << name() << "(";
299
Neel Mehtafff486d2019-07-23 14:02:49 -0700300 if (!args().empty()) {
301 fillHidlArgResultTokens(args(), &wrappedOutput, results().empty() ? ");\n" : ")");
302 } else {
303 wrappedOutput << (results().empty() ? ");\n" : ")");
304 }
Neel Mehta3b414a82019-07-02 15:47:48 -0700305
306 if (!results().empty()) {
307 wrappedOutput.group([&] {
308 wrappedOutput.printUnlessWrapped(" ");
309 wrappedOutput << "generates (";
Neel Mehtafff486d2019-07-23 14:02:49 -0700310 fillHidlArgResultTokens(results(), &wrappedOutput, ");\n");
Neel Mehta3b414a82019-07-02 15:47:48 -0700311 });
312 }
313
Neel Mehta3b414a82019-07-02 15:47:48 -0700314 out << wrappedOutput;
315}
316
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700317bool Method::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700318 if (!std::all_of(mArgs->begin(), mArgs->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700319 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700320 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700321 }
322
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700323 if (!std::all_of(mResults->begin(), mResults->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700324 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700325 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700326 }
327
328 return true;
329}
330
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700331const NamedReference<Type>* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700332 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800333 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700334 return nullptr;
335 }
336
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700337 const NamedReference<Type>* typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700338
Steven Moreland9df52442016-12-12 08:51:14 -0800339 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100340 return typedVar;
341 }
342
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700343 return nullptr;
344}
345
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700346const Location& Method::location() const {
347 return mLocation;
348}
349
Andreas Huber31629bc2016-08-03 09:06:40 -0700350////////////////////////////////////////////////////////////////////////////////
351
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700352bool TypedVarVector::add(NamedReference<Type>* v) {
Yifan Hong7763ab32016-12-13 17:42:11 -0800353 if (mNames.emplace(v->name()).second) {
354 push_back(v);
355 return true;
356 }
357 return false;
358}
359
Andreas Huberc9410c72016-07-28 12:18:40 -0700360} // namespace android
361