blob: 797e50343415ded935cedc7f131228f8b3874991 [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"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070022#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023#include "Type.h"
24
Yifan Hong10fe0b52016-10-19 14:20:17 -070025#include <android-base/logging.h>
Neel Mehta3b414a82019-07-02 15:47:48 -070026#include <hidl-util/FQName.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027#include <hidl-util/Formatter.h>
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070028#include <algorithm>
Neel Mehta69920a62019-07-22 16:22:13 -070029#include <string>
Neel Mehta3b414a82019-07-02 15:47:48 -070030#include <vector>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070031
Andreas Huberc9410c72016-07-28 12:18:40 -070032namespace android {
33
Neel Mehta69920a62019-07-22 16:22:13 -070034Method::Method(const std::string& name, std::vector<NamedReference<Type>*>* args,
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070035 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070036 std::vector<Annotation*>* annotations, const Location& location)
37 : mName(name),
38 mArgs(args),
39 mResults(results),
40 mOneway(oneway),
41 mAnnotations(annotations),
42 mLocation(location) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070043
Yifan Hongffa91392017-01-31 13:41:23 -080044void Method::fillImplementation(
45 size_t serial,
46 MethodImpl cppImpl,
47 MethodImpl javaImpl) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070048 mIsHidlReserved = true;
49 mSerial = serial;
50 mCppImpl = cppImpl;
51 mJavaImpl = javaImpl;
Yifan Hongcd2ae452017-01-31 14:33:40 -080052
53 CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end())
Steven Moreland937408a2017-03-20 09:54:18 -070054 << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead.";
Yifan Hongcd2ae452017-01-31 14:33:40 -080055 CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() ||
56 mCppImpl.find(IMPL_STUB) == mCppImpl.end())
57 << "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL.";
Yifan Hong10fe0b52016-10-19 14:20:17 -070058}
59
Andreas Huber881227d2016-08-02 14:20:21 -070060std::string Method::name() const {
61 return mName;
62}
63
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070064const std::vector<NamedReference<Type>*>& Method::args() const {
Andreas Huber881227d2016-08-02 14:20:21 -070065 return *mArgs;
66}
67
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070068const std::vector<NamedReference<Type>*>& Method::results() const {
Andreas Huber881227d2016-08-02 14:20:21 -070069 return *mResults;
70}
71
Steven Morelandd537ab02016-09-12 10:32:01 -070072const std::vector<Annotation *> &Method::annotations() const {
73 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070074}
75
Timur Iskhakovb58f4182017-08-29 15:19:24 -070076std::vector<Reference<Type>*> Method::getReferences() {
77 const auto& constRet = static_cast<const Method*>(this)->getReferences();
78 std::vector<Reference<Type>*> ret(constRet.size());
79 std::transform(constRet.begin(), constRet.end(), ret.begin(),
80 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
Timur Iskhakov33431e62017-08-21 17:31:23 -070081 return ret;
82}
83
Timur Iskhakovb58f4182017-08-29 15:19:24 -070084std::vector<const Reference<Type>*> Method::getReferences() const {
85 std::vector<const Reference<Type>*> ret;
86 ret.insert(ret.end(), mArgs->begin(), mArgs->end());
87 ret.insert(ret.end(), mResults->begin(), mResults->end());
88 return ret;
89}
90
Timur Iskhakovff5e64a2017-09-11 14:56:18 -070091std::vector<Reference<Type>*> Method::getStrongReferences() {
92 const auto& constRet = static_cast<const Method*>(this)->getStrongReferences();
93 std::vector<Reference<Type>*> ret(constRet.size());
94 std::transform(constRet.begin(), constRet.end(), ret.begin(),
95 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
96 return ret;
97}
98
99std::vector<const Reference<Type>*> Method::getStrongReferences() const {
100 std::vector<const Reference<Type>*> ret;
101 for (const auto* ref : getReferences()) {
102 if (!ref->shallowGet()->isNeverStrongReference()) {
103 ret.push_back(ref);
104 }
105 }
106 return ret;
107}
108
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700109std::vector<ConstantExpression*> Method::getConstantExpressions() {
110 const auto& constRet = static_cast<const Method*>(this)->getConstantExpressions();
111 std::vector<ConstantExpression*> ret(constRet.size());
112 std::transform(constRet.begin(), constRet.end(), ret.begin(),
113 [](const auto* ce) { return const_cast<ConstantExpression*>(ce); });
114 return ret;
115}
116
117std::vector<const ConstantExpression*> Method::getConstantExpressions() const {
118 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700119 for (const auto* annotation : *mAnnotations) {
120 const auto& retAnnotation = annotation->getConstantExpressions();
121 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700122 }
Timur Iskhakov891a8662017-08-25 21:53:48 -0700123 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700124}
125
Martijn Coenen115d4282016-12-19 05:14:04 +0100126void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700127 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100128 auto it = mCppImpl.find(type);
129 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100130 if (it->second != nullptr) {
131 it->second(out);
132 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700133 }
134}
135
Martijn Coenen115d4282016-12-19 05:14:04 +0100136void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700137 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100138 auto it = mJavaImpl.find(type);
139 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100140 if (it->second != nullptr) {
141 it->second(out);
142 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700143 }
144}
145
Martijn Coenen115d4282016-12-19 05:14:04 +0100146bool Method::overridesCppImpl(MethodImplType type) const {
147 CHECK(mIsHidlReserved);
148 return mCppImpl.find(type) != mCppImpl.end();
149}
150
151bool Method::overridesJavaImpl(MethodImplType type) const {
152 CHECK(mIsHidlReserved);
153 return mJavaImpl.find(type) != mJavaImpl.end();
154}
155
Steven Moreland7645fbd2019-03-12 18:49:28 -0700156Method* Method::copySignature() const {
157 Method* method = new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, location());
158 method->setDocComment(getDocComment());
159 return method;
Yifan Hongffa91392017-01-31 13:41:23 -0800160}
161
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700162void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700163 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700164 mSerial = serial;
165}
166
167size_t Method::getSerialId() const {
168 return mSerial;
169}
170
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700171bool Method::hasEmptyCppArgSignature() const {
172 return args().empty() && (results().empty() || canElideCallback() != nullptr);
173}
Steven Morelanda7a421a2016-09-07 08:35:18 -0700174
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700175void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700176 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700177 const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700178
Steven Morelanda7a421a2016-09-07 08:35:18 -0700179 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700180 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700181 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700182 out << space
183 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700184 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700185 << "> ";
186 }
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700187}
188
189void Method::generateCppSignature(Formatter &out,
190 const std::string &className,
191 bool specifyNamespaces) const {
192 generateCppReturnType(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700193
194 if (!className.empty()) {
195 out << className << "::";
196 }
197
198 out << name()
Yifan Hong932464e2017-03-30 15:40:22 -0700199 << "(";
200 emitCppArgSignature(out, specifyNamespaces);
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800201 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700202}
203
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700204static void emitCppArgResultSignature(Formatter& out,
205 const std::vector<NamedReference<Type>*>& args,
206 bool specifyNamespaces) {
Yifan Hong932464e2017-03-30 15:40:22 -0700207 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
208 out << arg->type().getCppArgumentType(specifyNamespaces);
209 out << " ";
210 out << arg->name();
211 });
Andreas Huber881227d2016-08-02 14:20:21 -0700212}
213
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700214static void emitJavaArgResultSignature(Formatter& out,
215 const std::vector<NamedReference<Type>*>& args) {
Yifan Hong932464e2017-03-30 15:40:22 -0700216 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
217 out << arg->type().getJavaType();
218 out << " ";
219 out << arg->name();
220 });
221}
Andreas Huber2831d512016-08-15 09:33:47 -0700222
Yifan Hong932464e2017-03-30 15:40:22 -0700223void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
224 emitCppArgResultSignature(out, args(), specifyNamespaces);
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700225
226 const bool returnsValue = !results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700227 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700228 if (returnsValue && elidedReturn == nullptr) {
229 if (!args().empty()) {
230 out << ", ";
231 }
232
233 out << name() << "_cb _hidl_cb";
234 }
Yifan Hong932464e2017-03-30 15:40:22 -0700235}
236void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
237 emitCppArgResultSignature(out, results(), specifyNamespaces);
238}
239void Method::emitJavaArgSignature(Formatter &out) const {
240 emitJavaArgResultSignature(out, args());
241}
242void Method::emitJavaResultSignature(Formatter &out) const {
243 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700244}
245
Neel Mehta5b447c02019-05-23 16:12:24 -0700246void Method::emitJavaSignature(Formatter& out) const {
247 const bool returnsValue = !results().empty();
248 const bool needsCallback = results().size() > 1;
249
250 if (returnsValue && !needsCallback) {
251 out << results()[0]->type().getJavaType();
252 } else {
253 out << "void";
254 }
255
256 out << " " << name() << "(";
257 emitJavaArgSignature(out);
258
259 if (needsCallback) {
260 if (!args().empty()) {
261 out << ", ";
262 }
263
264 out << name() << "Callback _hidl_cb";
265 }
266
267 out << ")";
268}
269
Neel Mehta3b414a82019-07-02 15:47:48 -0700270static void fillHidlArgResultTokens(const std::vector<NamedReference<Type>*>& args,
271 WrappedOutput* wrappedOutput) {
272 for (auto iter = args.begin(); iter != args.end(); ++iter) {
273 auto arg = *iter;
274 std::string out = arg->localName() + " " + arg->name();
275 if (iter != args.begin()) {
276 *wrappedOutput << ",";
277 wrappedOutput->group([&] {
278 wrappedOutput->printUnlessWrapped(" ");
279 *wrappedOutput << out;
280 });
281 } else {
282 wrappedOutput->group([&] { *wrappedOutput << out; });
283 }
284 }
285}
286
287void Method::emitHidlDefinition(Formatter& out) const {
288 if (getDocComment() != nullptr) getDocComment()->emit(out);
289
290 out.join(mAnnotations->begin(), mAnnotations->end(), "\n",
291 [&](auto annotation) { annotation->dump(out); });
292 if (!mAnnotations->empty()) out << "\n";
293
294 WrappedOutput wrappedOutput(MAX_LINE_LENGTH);
295
296 if (isOneway()) wrappedOutput << "oneway ";
297 wrappedOutput << name() << "(";
298
299 wrappedOutput.group([&] { fillHidlArgResultTokens(args(), &wrappedOutput); });
300
301 wrappedOutput << ")";
302
303 if (!results().empty()) {
304 wrappedOutput.group([&] {
305 wrappedOutput.printUnlessWrapped(" ");
306 wrappedOutput << "generates (";
307 fillHidlArgResultTokens(results(), &wrappedOutput);
308 wrappedOutput << ")";
309 });
310 }
311
312 wrappedOutput << ";\n";
313
314 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