blob: 07f429d5ba23289ff8895473e79a4b36ef454058 [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"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070020#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070021#include "Type.h"
22
Yifan Hong10fe0b52016-10-19 14:20:17 -070023#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070024#include <hidl-util/Formatter.h>
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070025#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070026
Andreas Huberc9410c72016-07-28 12:18:40 -070027namespace android {
28
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070029Method::Method(const char* name, std::vector<NamedReference<Type>*>* args,
30 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070031 std::vector<Annotation*>* annotations, const Location& location)
32 : mName(name),
33 mArgs(args),
34 mResults(results),
35 mOneway(oneway),
36 mAnnotations(annotations),
37 mLocation(location) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070038
Yifan Hongffa91392017-01-31 13:41:23 -080039void Method::fillImplementation(
40 size_t serial,
41 MethodImpl cppImpl,
42 MethodImpl javaImpl) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070043 mIsHidlReserved = true;
44 mSerial = serial;
45 mCppImpl = cppImpl;
46 mJavaImpl = javaImpl;
Yifan Hongcd2ae452017-01-31 14:33:40 -080047
48 CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end())
Steven Moreland937408a2017-03-20 09:54:18 -070049 << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead.";
Yifan Hongcd2ae452017-01-31 14:33:40 -080050 CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() ||
51 mCppImpl.find(IMPL_STUB) == mCppImpl.end())
52 << "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL.";
Yifan Hong10fe0b52016-10-19 14:20:17 -070053}
54
Andreas Huber881227d2016-08-02 14:20:21 -070055std::string Method::name() const {
56 return mName;
57}
58
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070059const std::vector<NamedReference<Type>*>& Method::args() const {
Andreas Huber881227d2016-08-02 14:20:21 -070060 return *mArgs;
61}
62
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070063const std::vector<NamedReference<Type>*>& Method::results() const {
Andreas Huber881227d2016-08-02 14:20:21 -070064 return *mResults;
65}
66
Steven Morelandd537ab02016-09-12 10:32:01 -070067const std::vector<Annotation *> &Method::annotations() const {
68 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070069}
70
Timur Iskhakov33431e62017-08-21 17:31:23 -070071std::vector<Reference<Type>> Method::getReferences() const {
72 std::vector<Reference<Type>> ret;
73 for (const auto* arg : *mArgs) {
74 ret.push_back(*arg);
75 }
76 for (const auto* result : *mResults) {
77 ret.push_back(*result);
78 }
79 return ret;
80}
81
Timur Iskhakovcec46c42017-08-09 00:22:02 -070082status_t Method::evaluate() {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070083 for (auto* annotaion : *mAnnotations) {
84 status_t err = annotaion->evaluate();
85 if (err != OK) return err;
86 }
87
88 return OK;
89}
90
91status_t Method::validate() const {
Timur Iskhakovcec46c42017-08-09 00:22:02 -070092 for (const auto* annotaion : *mAnnotations) {
93 status_t err = annotaion->validate();
94 if (err != OK) return err;
95 }
96
97 return OK;
98}
99
Martijn Coenen115d4282016-12-19 05:14:04 +0100100void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700101 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100102 auto it = mCppImpl.find(type);
103 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100104 if (it->second != nullptr) {
105 it->second(out);
106 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700107 }
108}
109
Martijn Coenen115d4282016-12-19 05:14:04 +0100110void Method::javaImpl(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 = mJavaImpl.find(type);
113 if (it != mJavaImpl.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
Andreas Huber37065d62017-02-07 14:36:54 -0800120bool Method::isHiddenFromJava() const {
121 return isHidlReserved() && name() == "debug";
122}
123
Martijn Coenen115d4282016-12-19 05:14:04 +0100124bool Method::overridesCppImpl(MethodImplType type) const {
125 CHECK(mIsHidlReserved);
126 return mCppImpl.find(type) != mCppImpl.end();
127}
128
129bool Method::overridesJavaImpl(MethodImplType type) const {
130 CHECK(mIsHidlReserved);
131 return mJavaImpl.find(type) != mJavaImpl.end();
132}
133
Yifan Hongffa91392017-01-31 13:41:23 -0800134Method *Method::copySignature() const {
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700135 return new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, Location());
Yifan Hongffa91392017-01-31 13:41:23 -0800136}
137
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700138void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700139 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700140 mSerial = serial;
141}
142
143size_t Method::getSerialId() const {
144 return mSerial;
145}
146
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700147bool Method::hasEmptyCppArgSignature() const {
148 return args().empty() && (results().empty() || canElideCallback() != nullptr);
149}
Steven Morelanda7a421a2016-09-07 08:35:18 -0700150
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700151void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700152 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700153 const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700154
Steven Morelanda7a421a2016-09-07 08:35:18 -0700155 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700156 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700157 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700158 out << space
159 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700160 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700161 << "> ";
162 }
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700163}
164
165void Method::generateCppSignature(Formatter &out,
166 const std::string &className,
167 bool specifyNamespaces) const {
168 generateCppReturnType(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700169
170 if (!className.empty()) {
171 out << className << "::";
172 }
173
174 out << name()
Yifan Hong932464e2017-03-30 15:40:22 -0700175 << "(";
176 emitCppArgSignature(out, specifyNamespaces);
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800177 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700178}
179
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700180static void emitCppArgResultSignature(Formatter& out,
181 const std::vector<NamedReference<Type>*>& args,
182 bool specifyNamespaces) {
Yifan Hong932464e2017-03-30 15:40:22 -0700183 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
184 out << arg->type().getCppArgumentType(specifyNamespaces);
185 out << " ";
186 out << arg->name();
187 });
Andreas Huber881227d2016-08-02 14:20:21 -0700188}
189
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700190static void emitJavaArgResultSignature(Formatter& out,
191 const std::vector<NamedReference<Type>*>& args) {
Yifan Hong932464e2017-03-30 15:40:22 -0700192 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
193 out << arg->type().getJavaType();
194 out << " ";
195 out << arg->name();
196 });
197}
Andreas Huber2831d512016-08-15 09:33:47 -0700198
Yifan Hong932464e2017-03-30 15:40:22 -0700199void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
200 emitCppArgResultSignature(out, args(), specifyNamespaces);
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700201
202 const bool returnsValue = !results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700203 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700204 if (returnsValue && elidedReturn == nullptr) {
205 if (!args().empty()) {
206 out << ", ";
207 }
208
209 out << name() << "_cb _hidl_cb";
210 }
Yifan Hong932464e2017-03-30 15:40:22 -0700211}
212void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
213 emitCppArgResultSignature(out, results(), specifyNamespaces);
214}
215void Method::emitJavaArgSignature(Formatter &out) const {
216 emitJavaArgResultSignature(out, args());
217}
218void Method::emitJavaResultSignature(Formatter &out) const {
219 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700220}
221
Andreas Huber3599d922016-08-09 10:42:57 -0700222void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700223 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700224 return;
225 }
226
227 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700228 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700229 if (i > 0) {
230 out << " ";
231 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700232 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700233 }
234 out << "\n";
235}
236
Andreas Huber70a59e12016-08-16 12:57:01 -0700237bool Method::isJavaCompatible() const {
Andreas Huber37065d62017-02-07 14:36:54 -0800238 if (isHiddenFromJava()) {
239 return true;
240 }
241
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700242 if (!std::all_of(mArgs->begin(), mArgs->end(),
243 [](const auto* arg) { return (*arg)->isJavaCompatible(); })) {
244 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700245 }
246
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700247 if (!std::all_of(mResults->begin(), mResults->end(),
248 [](const auto* arg) { return (*arg)->isJavaCompatible(); })) {
249 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700250 }
251
252 return true;
253}
254
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700255const NamedReference<Type>* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700256 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800257 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700258 return nullptr;
259 }
260
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700261 const NamedReference<Type>* typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700262
Steven Moreland9df52442016-12-12 08:51:14 -0800263 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100264 return typedVar;
265 }
266
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700267 return nullptr;
268}
269
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700270const Location& Method::location() const {
271 return mLocation;
272}
273
Andreas Huber31629bc2016-08-03 09:06:40 -0700274////////////////////////////////////////////////////////////////////////////////
275
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700276bool TypedVarVector::add(NamedReference<Type>* v) {
Yifan Hong7763ab32016-12-13 17:42:11 -0800277 if (mNames.emplace(v->name()).second) {
278 push_back(v);
279 return true;
280 }
281 return false;
282}
283
Andreas Huberc9410c72016-07-28 12:18:40 -0700284} // namespace android
285