blob: 9d40856bdc3109750d18efb6d4fe6bc9037544f2 [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())
50 << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_HEADER instead.";
51 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()
142 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700143 << GetArgSignature(args(), 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
Andreas Huber881227d2016-08-02 14:20:21 -0700156// static
Steven Moreland979e0992016-09-07 09:18:08 -0700157std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
158 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -0700159 bool first = true;
160 std::string out;
161 for (const auto &arg : args) {
162 if (!first) {
163 out += ", ";
164 }
165
Yifan Hong3b320f82016-11-01 15:15:54 -0700166 out += arg->type().getCppArgumentType(specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700167 out += " ";
168 out += arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -0700169
170 first = false;
171 }
172
173 return out;
174}
175
Andreas Huber2831d512016-08-15 09:33:47 -0700176// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700177std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700178 bool first = true;
179 std::string out;
180 for (const auto &arg : args) {
181 if (!first) {
182 out += ", ";
183 }
184
Yifan Hong4ed13472016-11-02 10:44:11 -0700185 out += arg->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700186 out += " ";
187 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700188
189 first = false;
190 }
191
192 return out;
193}
194
Andreas Huber3599d922016-08-09 10:42:57 -0700195void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700196 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700197 return;
198 }
199
200 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700201 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700202 if (i > 0) {
203 out << " ";
204 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700205 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700206 }
207 out << "\n";
208}
209
Andreas Huber70a59e12016-08-16 12:57:01 -0700210bool Method::isJavaCompatible() const {
Andreas Huber37065d62017-02-07 14:36:54 -0800211 if (isHiddenFromJava()) {
212 return true;
213 }
214
Andreas Huber70a59e12016-08-16 12:57:01 -0700215 for (const auto &arg : *mArgs) {
216 if (!arg->isJavaCompatible()) {
217 return false;
218 }
219 }
220
221 for (const auto &result : *mResults) {
222 if (!result->isJavaCompatible()) {
223 return false;
224 }
225 }
226
227 return true;
228}
229
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700230const TypedVar* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700231 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800232 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700233 return nullptr;
234 }
235
Steven Moreland9df52442016-12-12 08:51:14 -0800236 const TypedVar *typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700237
Steven Moreland9df52442016-12-12 08:51:14 -0800238 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100239 return typedVar;
240 }
241
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700242 return nullptr;
243}
244
Andreas Huber31629bc2016-08-03 09:06:40 -0700245////////////////////////////////////////////////////////////////////////////////
246
247TypedVar::TypedVar(const char *name, Type *type)
248 : mName(name),
249 mType(type) {
250}
251
252std::string TypedVar::name() const {
253 return mName;
254}
255
256const Type &TypedVar::type() const {
257 return *mType;
258}
259
Andreas Huber70a59e12016-08-16 12:57:01 -0700260bool TypedVar::isJavaCompatible() const {
261 return mType->isJavaCompatible();
262}
263
Yifan Hong7763ab32016-12-13 17:42:11 -0800264////////////////////////////////////////////////////////////////////////////////
265bool TypedVarVector::add(TypedVar *v) {
266 if (mNames.emplace(v->name()).second) {
267 push_back(v);
268 return true;
269 }
270 return false;
271}
272
Andreas Huberc9410c72016-07-28 12:18:40 -0700273} // namespace android
274