blob: ab080b16c20e32f498e357e3c73c76c7bf8d39d0 [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>
25
Andreas Huberc9410c72016-07-28 12:18:40 -070026namespace android {
27
Andreas Huber3599d922016-08-09 10:42:57 -070028Method::Method(const char *name,
29 std::vector<TypedVar *> *args,
30 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070031 bool oneway,
Steven Morelandd537ab02016-09-12 10:32:01 -070032 std::vector<Annotation *> *annotations)
Andreas Huberc9410c72016-07-28 12:18:40 -070033 : mName(name),
34 mArgs(args),
Andreas Huber3599d922016-08-09 10:42:57 -070035 mResults(results),
Iliyan Malchev639bff82016-08-13 14:24:11 -070036 mOneway(oneway),
Steven Morelandd537ab02016-09-12 10:32:01 -070037 mAnnotations(annotations) {
Andreas Huberc9410c72016-07-28 12:18:40 -070038}
39
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
60const std::vector<TypedVar *> &Method::args() const {
61 return *mArgs;
62}
63
64const std::vector<TypedVar *> &Method::results() const {
65 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
Martijn Coenen115d4282016-12-19 05:14:04 +010072void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -070073 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +010074 auto it = mCppImpl.find(type);
75 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +010076 if (it->second != nullptr) {
77 it->second(out);
78 }
Yifan Hong10fe0b52016-10-19 14:20:17 -070079 }
80}
81
Martijn Coenen115d4282016-12-19 05:14:04 +010082void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -070083 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +010084 auto it = mJavaImpl.find(type);
85 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +010086 if (it->second != nullptr) {
87 it->second(out);
88 }
Yifan Hong10fe0b52016-10-19 14:20:17 -070089 }
90}
91
Andreas Huber37065d62017-02-07 14:36:54 -080092bool Method::isHiddenFromJava() const {
93 return isHidlReserved() && name() == "debug";
94}
95
Martijn Coenen115d4282016-12-19 05:14:04 +010096bool Method::overridesCppImpl(MethodImplType type) const {
97 CHECK(mIsHidlReserved);
98 return mCppImpl.find(type) != mCppImpl.end();
99}
100
101bool Method::overridesJavaImpl(MethodImplType type) const {
102 CHECK(mIsHidlReserved);
103 return mJavaImpl.find(type) != mJavaImpl.end();
104}
105
Yifan Hongffa91392017-01-31 13:41:23 -0800106Method *Method::copySignature() const {
107 return new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations);
108}
109
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700110void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700111 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700112 mSerial = serial;
113}
114
115size_t Method::getSerialId() const {
116 return mSerial;
117}
118
Steven Morelanda7a421a2016-09-07 08:35:18 -0700119void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -0700120 const std::string &className,
121 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700122 const bool returnsValue = !results().empty();
123
124 const TypedVar *elidedReturn = canElideCallback();
125
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700126 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
127
Steven Morelanda7a421a2016-09-07 08:35:18 -0700128 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700129 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700130 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700131 out << space
132 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700133 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700134 << "> ";
135 }
136
137 if (!className.empty()) {
138 out << className << "::";
139 }
140
141 out << name()
Yifan Hong7d234ea2017-03-30 15:40:22 -0700142 << "(";
143 emitCppArgSignature(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700144
145 if (returnsValue && elidedReturn == nullptr) {
146 if (!args().empty()) {
147 out << ", ";
148 }
149
150 out << name() << "_cb _hidl_cb";
151 }
152
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800153 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700154}
155
Yifan Hong7d234ea2017-03-30 15:40:22 -0700156static void emitCppArgResultSignature(Formatter &out,
157 const std::vector<TypedVar *> &args,
158 bool specifyNamespaces) {
159 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
160 out << arg->type().getCppArgumentType(specifyNamespaces);
161 out << " ";
162 out << arg->name();
163 });
Andreas Huber881227d2016-08-02 14:20:21 -0700164}
165
Yifan Hong7d234ea2017-03-30 15:40:22 -0700166static void emitJavaArgResultSignature(Formatter &out, const std::vector<TypedVar *> &args) {
167 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
168 out << arg->type().getJavaType();
169 out << " ";
170 out << arg->name();
171 });
172}
Andreas Huber2831d512016-08-15 09:33:47 -0700173
Yifan Hong7d234ea2017-03-30 15:40:22 -0700174void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
175 emitCppArgResultSignature(out, args(), specifyNamespaces);
176}
177void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
178 emitCppArgResultSignature(out, results(), specifyNamespaces);
179}
180void Method::emitJavaArgSignature(Formatter &out) const {
181 emitJavaArgResultSignature(out, args());
182}
183void Method::emitJavaResultSignature(Formatter &out) const {
184 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700185}
186
Andreas Huber3599d922016-08-09 10:42:57 -0700187void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700188 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700189 return;
190 }
191
192 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700193 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700194 if (i > 0) {
195 out << " ";
196 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700197 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700198 }
199 out << "\n";
200}
201
Andreas Huber70a59e12016-08-16 12:57:01 -0700202bool Method::isJavaCompatible() const {
Andreas Huber37065d62017-02-07 14:36:54 -0800203 if (isHiddenFromJava()) {
204 return true;
205 }
206
Andreas Huber70a59e12016-08-16 12:57:01 -0700207 for (const auto &arg : *mArgs) {
208 if (!arg->isJavaCompatible()) {
209 return false;
210 }
211 }
212
213 for (const auto &result : *mResults) {
214 if (!result->isJavaCompatible()) {
215 return false;
216 }
217 }
218
219 return true;
220}
221
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700222const TypedVar* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700223 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800224 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700225 return nullptr;
226 }
227
Steven Moreland9df52442016-12-12 08:51:14 -0800228 const TypedVar *typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700229
Steven Moreland9df52442016-12-12 08:51:14 -0800230 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100231 return typedVar;
232 }
233
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700234 return nullptr;
235}
236
Andreas Huber31629bc2016-08-03 09:06:40 -0700237////////////////////////////////////////////////////////////////////////////////
238
239TypedVar::TypedVar(const char *name, Type *type)
240 : mName(name),
241 mType(type) {
242}
243
244std::string TypedVar::name() const {
245 return mName;
246}
247
248const Type &TypedVar::type() const {
249 return *mType;
250}
251
Andreas Huber70a59e12016-08-16 12:57:01 -0700252bool TypedVar::isJavaCompatible() const {
253 return mType->isJavaCompatible();
254}
255
Yifan Hong7763ab32016-12-13 17:42:11 -0800256////////////////////////////////////////////////////////////////////////////////
257bool TypedVarVector::add(TypedVar *v) {
258 if (mNames.emplace(v->name()).second) {
259 push_back(v);
260 return true;
261 }
262 return false;
263}
264
Andreas Huberc9410c72016-07-28 12:18:40 -0700265} // namespace android
266