blob: 482bae52d59d1a0d942a89ec85d01532243cf1de [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"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070021#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070022#include "Type.h"
23
Yifan Hong10fe0b52016-10-19 14:20:17 -070024#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070025#include <hidl-util/Formatter.h>
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070026#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027
Andreas Huberc9410c72016-07-28 12:18:40 -070028namespace android {
29
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070030Method::Method(const char* name, std::vector<NamedReference<Type>*>* args,
31 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070032 std::vector<Annotation*>* annotations, const Location& location)
33 : mName(name),
34 mArgs(args),
35 mResults(results),
36 mOneway(oneway),
37 mAnnotations(annotations),
38 mLocation(location) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070039
Yifan Hongffa91392017-01-31 13:41:23 -080040void Method::fillImplementation(
41 size_t serial,
42 MethodImpl cppImpl,
43 MethodImpl javaImpl) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070044 mIsHidlReserved = true;
45 mSerial = serial;
46 mCppImpl = cppImpl;
47 mJavaImpl = javaImpl;
Yifan Hongcd2ae452017-01-31 14:33:40 -080048
49 CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end())
Steven Moreland937408a2017-03-20 09:54:18 -070050 << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead.";
Yifan Hongcd2ae452017-01-31 14:33:40 -080051 CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() ||
52 mCppImpl.find(IMPL_STUB) == mCppImpl.end())
53 << "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL.";
Yifan Hong10fe0b52016-10-19 14:20:17 -070054}
55
Andreas Huber881227d2016-08-02 14:20:21 -070056std::string Method::name() const {
57 return mName;
58}
59
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070060const std::vector<NamedReference<Type>*>& Method::args() const {
Andreas Huber881227d2016-08-02 14:20:21 -070061 return *mArgs;
62}
63
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070064const std::vector<NamedReference<Type>*>& Method::results() const {
Andreas Huber881227d2016-08-02 14:20:21 -070065 return *mResults;
66}
67
Steven Morelandd537ab02016-09-12 10:32:01 -070068const std::vector<Annotation *> &Method::annotations() const {
69 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070070}
71
Timur Iskhakovb58f4182017-08-29 15:19:24 -070072std::vector<Reference<Type>*> Method::getReferences() {
73 const auto& constRet = static_cast<const Method*>(this)->getReferences();
74 std::vector<Reference<Type>*> ret(constRet.size());
75 std::transform(constRet.begin(), constRet.end(), ret.begin(),
76 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
Timur Iskhakov33431e62017-08-21 17:31:23 -070077 return ret;
78}
79
Timur Iskhakovb58f4182017-08-29 15:19:24 -070080std::vector<const Reference<Type>*> Method::getReferences() const {
81 std::vector<const Reference<Type>*> ret;
82 ret.insert(ret.end(), mArgs->begin(), mArgs->end());
83 ret.insert(ret.end(), mResults->begin(), mResults->end());
84 return ret;
85}
86
Timur Iskhakovff5e64a2017-09-11 14:56:18 -070087std::vector<Reference<Type>*> Method::getStrongReferences() {
88 const auto& constRet = static_cast<const Method*>(this)->getStrongReferences();
89 std::vector<Reference<Type>*> ret(constRet.size());
90 std::transform(constRet.begin(), constRet.end(), ret.begin(),
91 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
92 return ret;
93}
94
95std::vector<const Reference<Type>*> Method::getStrongReferences() const {
96 std::vector<const Reference<Type>*> ret;
97 for (const auto* ref : getReferences()) {
98 if (!ref->shallowGet()->isNeverStrongReference()) {
99 ret.push_back(ref);
100 }
101 }
102 return ret;
103}
104
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700105std::vector<ConstantExpression*> Method::getConstantExpressions() {
106 const auto& constRet = static_cast<const Method*>(this)->getConstantExpressions();
107 std::vector<ConstantExpression*> ret(constRet.size());
108 std::transform(constRet.begin(), constRet.end(), ret.begin(),
109 [](const auto* ce) { return const_cast<ConstantExpression*>(ce); });
110 return ret;
111}
112
113std::vector<const ConstantExpression*> Method::getConstantExpressions() const {
114 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700115 for (const auto* annotation : *mAnnotations) {
116 const auto& retAnnotation = annotation->getConstantExpressions();
117 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700118 }
Timur Iskhakov891a8662017-08-25 21:53:48 -0700119 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700120}
121
Martijn Coenen115d4282016-12-19 05:14:04 +0100122void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700123 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100124 auto it = mCppImpl.find(type);
125 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100126 if (it->second != nullptr) {
127 it->second(out);
128 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700129 }
130}
131
Martijn Coenen115d4282016-12-19 05:14:04 +0100132void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700133 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100134 auto it = mJavaImpl.find(type);
135 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100136 if (it->second != nullptr) {
137 it->second(out);
138 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700139 }
140}
141
Martijn Coenen115d4282016-12-19 05:14:04 +0100142bool Method::overridesCppImpl(MethodImplType type) const {
143 CHECK(mIsHidlReserved);
144 return mCppImpl.find(type) != mCppImpl.end();
145}
146
147bool Method::overridesJavaImpl(MethodImplType type) const {
148 CHECK(mIsHidlReserved);
149 return mJavaImpl.find(type) != mJavaImpl.end();
150}
151
Steven Moreland7645fbd2019-03-12 18:49:28 -0700152Method* Method::copySignature() const {
153 Method* method = new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, location());
154 method->setDocComment(getDocComment());
155 return method;
Yifan Hongffa91392017-01-31 13:41:23 -0800156}
157
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700158void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700159 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700160 mSerial = serial;
161}
162
163size_t Method::getSerialId() const {
164 return mSerial;
165}
166
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700167bool Method::hasEmptyCppArgSignature() const {
168 return args().empty() && (results().empty() || canElideCallback() != nullptr);
169}
Steven Morelanda7a421a2016-09-07 08:35:18 -0700170
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700171void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700172 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700173 const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700174
Steven Morelanda7a421a2016-09-07 08:35:18 -0700175 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700176 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700177 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700178 out << space
179 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700180 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700181 << "> ";
182 }
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700183}
184
185void Method::generateCppSignature(Formatter &out,
186 const std::string &className,
187 bool specifyNamespaces) const {
188 generateCppReturnType(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700189
190 if (!className.empty()) {
191 out << className << "::";
192 }
193
194 out << name()
Yifan Hong932464e2017-03-30 15:40:22 -0700195 << "(";
196 emitCppArgSignature(out, specifyNamespaces);
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800197 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700198}
199
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700200static void emitCppArgResultSignature(Formatter& out,
201 const std::vector<NamedReference<Type>*>& args,
202 bool specifyNamespaces) {
Yifan Hong932464e2017-03-30 15:40:22 -0700203 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
204 out << arg->type().getCppArgumentType(specifyNamespaces);
205 out << " ";
206 out << arg->name();
207 });
Andreas Huber881227d2016-08-02 14:20:21 -0700208}
209
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700210static void emitJavaArgResultSignature(Formatter& out,
211 const std::vector<NamedReference<Type>*>& args) {
Yifan Hong932464e2017-03-30 15:40:22 -0700212 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
213 out << arg->type().getJavaType();
214 out << " ";
215 out << arg->name();
216 });
217}
Andreas Huber2831d512016-08-15 09:33:47 -0700218
Yifan Hong932464e2017-03-30 15:40:22 -0700219void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
220 emitCppArgResultSignature(out, args(), specifyNamespaces);
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700221
222 const bool returnsValue = !results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700223 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700224 if (returnsValue && elidedReturn == nullptr) {
225 if (!args().empty()) {
226 out << ", ";
227 }
228
229 out << name() << "_cb _hidl_cb";
230 }
Yifan Hong932464e2017-03-30 15:40:22 -0700231}
232void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
233 emitCppArgResultSignature(out, results(), specifyNamespaces);
234}
235void Method::emitJavaArgSignature(Formatter &out) const {
236 emitJavaArgResultSignature(out, args());
237}
238void Method::emitJavaResultSignature(Formatter &out) const {
239 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700240}
241
Andreas Huber3599d922016-08-09 10:42:57 -0700242void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700243 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700244 return;
245 }
246
247 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700248 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700249 if (i > 0) {
250 out << " ";
251 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700252 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700253 }
254 out << "\n";
255}
256
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700257bool Method::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700258 if (!std::all_of(mArgs->begin(), mArgs->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700259 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700260 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700261 }
262
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700263 if (!std::all_of(mResults->begin(), mResults->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700264 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700265 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700266 }
267
268 return true;
269}
270
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700271const NamedReference<Type>* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700272 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800273 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700274 return nullptr;
275 }
276
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700277 const NamedReference<Type>* typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700278
Steven Moreland9df52442016-12-12 08:51:14 -0800279 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100280 return typedVar;
281 }
282
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700283 return nullptr;
284}
285
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700286const Location& Method::location() const {
287 return mLocation;
288}
289
Andreas Huber31629bc2016-08-03 09:06:40 -0700290////////////////////////////////////////////////////////////////////////////////
291
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700292bool TypedVarVector::add(NamedReference<Type>* v) {
Yifan Hong7763ab32016-12-13 17:42:11 -0800293 if (mNames.emplace(v->name()).second) {
294 push_back(v);
295 return true;
296 }
297 return false;
298}
299
Andreas Huberc9410c72016-07-28 12:18:40 -0700300} // namespace android
301