blob: 42b4d5cff290d459215639b6ca6ccd8e06eac739 [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 Mehta3b414a82019-07-02 15:47:48 -070029#include <vector>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070030
Andreas Huberc9410c72016-07-28 12:18:40 -070031namespace android {
32
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070033Method::Method(const char* name, std::vector<NamedReference<Type>*>* args,
34 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070035 std::vector<Annotation*>* annotations, const Location& location)
36 : mName(name),
37 mArgs(args),
38 mResults(results),
39 mOneway(oneway),
40 mAnnotations(annotations),
41 mLocation(location) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070042
Yifan Hongffa91392017-01-31 13:41:23 -080043void Method::fillImplementation(
44 size_t serial,
45 MethodImpl cppImpl,
46 MethodImpl javaImpl) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070047 mIsHidlReserved = true;
48 mSerial = serial;
49 mCppImpl = cppImpl;
50 mJavaImpl = javaImpl;
Yifan Hongcd2ae452017-01-31 14:33:40 -080051
52 CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end())
Steven Moreland937408a2017-03-20 09:54:18 -070053 << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead.";
Yifan Hongcd2ae452017-01-31 14:33:40 -080054 CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() ||
55 mCppImpl.find(IMPL_STUB) == mCppImpl.end())
56 << "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL.";
Yifan Hong10fe0b52016-10-19 14:20:17 -070057}
58
Andreas Huber881227d2016-08-02 14:20:21 -070059std::string Method::name() const {
60 return mName;
61}
62
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070063const std::vector<NamedReference<Type>*>& Method::args() const {
Andreas Huber881227d2016-08-02 14:20:21 -070064 return *mArgs;
65}
66
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070067const std::vector<NamedReference<Type>*>& Method::results() const {
Andreas Huber881227d2016-08-02 14:20:21 -070068 return *mResults;
69}
70
Steven Morelandd537ab02016-09-12 10:32:01 -070071const std::vector<Annotation *> &Method::annotations() const {
72 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070073}
74
Timur Iskhakovb58f4182017-08-29 15:19:24 -070075std::vector<Reference<Type>*> Method::getReferences() {
76 const auto& constRet = static_cast<const Method*>(this)->getReferences();
77 std::vector<Reference<Type>*> ret(constRet.size());
78 std::transform(constRet.begin(), constRet.end(), ret.begin(),
79 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
Timur Iskhakov33431e62017-08-21 17:31:23 -070080 return ret;
81}
82
Timur Iskhakovb58f4182017-08-29 15:19:24 -070083std::vector<const Reference<Type>*> Method::getReferences() const {
84 std::vector<const Reference<Type>*> ret;
85 ret.insert(ret.end(), mArgs->begin(), mArgs->end());
86 ret.insert(ret.end(), mResults->begin(), mResults->end());
87 return ret;
88}
89
Timur Iskhakovff5e64a2017-09-11 14:56:18 -070090std::vector<Reference<Type>*> Method::getStrongReferences() {
91 const auto& constRet = static_cast<const Method*>(this)->getStrongReferences();
92 std::vector<Reference<Type>*> ret(constRet.size());
93 std::transform(constRet.begin(), constRet.end(), ret.begin(),
94 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
95 return ret;
96}
97
98std::vector<const Reference<Type>*> Method::getStrongReferences() const {
99 std::vector<const Reference<Type>*> ret;
100 for (const auto* ref : getReferences()) {
101 if (!ref->shallowGet()->isNeverStrongReference()) {
102 ret.push_back(ref);
103 }
104 }
105 return ret;
106}
107
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700108std::vector<ConstantExpression*> Method::getConstantExpressions() {
109 const auto& constRet = static_cast<const Method*>(this)->getConstantExpressions();
110 std::vector<ConstantExpression*> ret(constRet.size());
111 std::transform(constRet.begin(), constRet.end(), ret.begin(),
112 [](const auto* ce) { return const_cast<ConstantExpression*>(ce); });
113 return ret;
114}
115
116std::vector<const ConstantExpression*> Method::getConstantExpressions() const {
117 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700118 for (const auto* annotation : *mAnnotations) {
119 const auto& retAnnotation = annotation->getConstantExpressions();
120 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700121 }
Timur Iskhakov891a8662017-08-25 21:53:48 -0700122 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700123}
124
Martijn Coenen115d4282016-12-19 05:14:04 +0100125void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700126 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100127 auto it = mCppImpl.find(type);
128 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100129 if (it->second != nullptr) {
130 it->second(out);
131 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700132 }
133}
134
Martijn Coenen115d4282016-12-19 05:14:04 +0100135void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700136 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100137 auto it = mJavaImpl.find(type);
138 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100139 if (it->second != nullptr) {
140 it->second(out);
141 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700142 }
143}
144
Martijn Coenen115d4282016-12-19 05:14:04 +0100145bool Method::overridesCppImpl(MethodImplType type) const {
146 CHECK(mIsHidlReserved);
147 return mCppImpl.find(type) != mCppImpl.end();
148}
149
150bool Method::overridesJavaImpl(MethodImplType type) const {
151 CHECK(mIsHidlReserved);
152 return mJavaImpl.find(type) != mJavaImpl.end();
153}
154
Steven Moreland7645fbd2019-03-12 18:49:28 -0700155Method* Method::copySignature() const {
156 Method* method = new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, location());
157 method->setDocComment(getDocComment());
158 return method;
Yifan Hongffa91392017-01-31 13:41:23 -0800159}
160
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700161void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700162 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700163 mSerial = serial;
164}
165
166size_t Method::getSerialId() const {
167 return mSerial;
168}
169
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700170bool Method::hasEmptyCppArgSignature() const {
171 return args().empty() && (results().empty() || canElideCallback() != nullptr);
172}
Steven Morelanda7a421a2016-09-07 08:35:18 -0700173
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700174void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700175 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700176 const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700177
Steven Morelanda7a421a2016-09-07 08:35:18 -0700178 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700179 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700180 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700181 out << space
182 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700183 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700184 << "> ";
185 }
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700186}
187
188void Method::generateCppSignature(Formatter &out,
189 const std::string &className,
190 bool specifyNamespaces) const {
191 generateCppReturnType(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700192
193 if (!className.empty()) {
194 out << className << "::";
195 }
196
197 out << name()
Yifan Hong932464e2017-03-30 15:40:22 -0700198 << "(";
199 emitCppArgSignature(out, specifyNamespaces);
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800200 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700201}
202
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700203static void emitCppArgResultSignature(Formatter& out,
204 const std::vector<NamedReference<Type>*>& args,
205 bool specifyNamespaces) {
Yifan Hong932464e2017-03-30 15:40:22 -0700206 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
207 out << arg->type().getCppArgumentType(specifyNamespaces);
208 out << " ";
209 out << arg->name();
210 });
Andreas Huber881227d2016-08-02 14:20:21 -0700211}
212
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700213static void emitJavaArgResultSignature(Formatter& out,
214 const std::vector<NamedReference<Type>*>& args) {
Yifan Hong932464e2017-03-30 15:40:22 -0700215 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
216 out << arg->type().getJavaType();
217 out << " ";
218 out << arg->name();
219 });
220}
Andreas Huber2831d512016-08-15 09:33:47 -0700221
Yifan Hong932464e2017-03-30 15:40:22 -0700222void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
223 emitCppArgResultSignature(out, args(), specifyNamespaces);
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700224
225 const bool returnsValue = !results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700226 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700227 if (returnsValue && elidedReturn == nullptr) {
228 if (!args().empty()) {
229 out << ", ";
230 }
231
232 out << name() << "_cb _hidl_cb";
233 }
Yifan Hong932464e2017-03-30 15:40:22 -0700234}
235void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
236 emitCppArgResultSignature(out, results(), specifyNamespaces);
237}
238void Method::emitJavaArgSignature(Formatter &out) const {
239 emitJavaArgResultSignature(out, args());
240}
241void Method::emitJavaResultSignature(Formatter &out) const {
242 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700243}
244
Neel Mehta5b447c02019-05-23 16:12:24 -0700245void Method::emitJavaSignature(Formatter& out) const {
246 const bool returnsValue = !results().empty();
247 const bool needsCallback = results().size() > 1;
248
249 if (returnsValue && !needsCallback) {
250 out << results()[0]->type().getJavaType();
251 } else {
252 out << "void";
253 }
254
255 out << " " << name() << "(";
256 emitJavaArgSignature(out);
257
258 if (needsCallback) {
259 if (!args().empty()) {
260 out << ", ";
261 }
262
263 out << name() << "Callback _hidl_cb";
264 }
265
266 out << ")";
267}
268
Neel Mehta3b414a82019-07-02 15:47:48 -0700269static void fillHidlArgResultTokens(const std::vector<NamedReference<Type>*>& args,
270 WrappedOutput* wrappedOutput) {
271 for (auto iter = args.begin(); iter != args.end(); ++iter) {
272 auto arg = *iter;
273 std::string out = arg->localName() + " " + arg->name();
274 if (iter != args.begin()) {
275 *wrappedOutput << ",";
276 wrappedOutput->group([&] {
277 wrappedOutput->printUnlessWrapped(" ");
278 *wrappedOutput << out;
279 });
280 } else {
281 wrappedOutput->group([&] { *wrappedOutput << out; });
282 }
283 }
284}
285
286void Method::emitHidlDefinition(Formatter& out) const {
287 if (getDocComment() != nullptr) getDocComment()->emit(out);
288
289 out.join(mAnnotations->begin(), mAnnotations->end(), "\n",
290 [&](auto annotation) { annotation->dump(out); });
291 if (!mAnnotations->empty()) out << "\n";
292
293 WrappedOutput wrappedOutput(MAX_LINE_LENGTH);
294
295 if (isOneway()) wrappedOutput << "oneway ";
296 wrappedOutput << name() << "(";
297
298 wrappedOutput.group([&] { fillHidlArgResultTokens(args(), &wrappedOutput); });
299
300 wrappedOutput << ")";
301
302 if (!results().empty()) {
303 wrappedOutput.group([&] {
304 wrappedOutput.printUnlessWrapped(" ");
305 wrappedOutput << "generates (";
306 fillHidlArgResultTokens(results(), &wrappedOutput);
307 wrappedOutput << ")";
308 });
309 }
310
311 wrappedOutput << ";\n";
312
313 out << wrappedOutput;
314}
315
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700316bool Method::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700317 if (!std::all_of(mArgs->begin(), mArgs->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700318 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700319 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700320 }
321
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700322 if (!std::all_of(mResults->begin(), mResults->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700323 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700324 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700325 }
326
327 return true;
328}
329
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700330const NamedReference<Type>* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700331 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800332 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700333 return nullptr;
334 }
335
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700336 const NamedReference<Type>* typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700337
Steven Moreland9df52442016-12-12 08:51:14 -0800338 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100339 return typedVar;
340 }
341
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700342 return nullptr;
343}
344
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700345const Location& Method::location() const {
346 return mLocation;
347}
348
Andreas Huber31629bc2016-08-03 09:06:40 -0700349////////////////////////////////////////////////////////////////////////////////
350
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700351bool TypedVarVector::add(NamedReference<Type>* v) {
Yifan Hong7763ab32016-12-13 17:42:11 -0800352 if (mNames.emplace(v->name()).second) {
353 push_back(v);
354 return true;
355 }
356 return false;
357}
358
Andreas Huberc9410c72016-07-28 12:18:40 -0700359} // namespace android
360