blob: e41ebd079e7b1d18cf4516e785f8499440945cef [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 Hong10fe0b52016-10-19 14:20:17 -070040// HIDL reserved methods.
41Method::Method(const char *name,
42 std::vector<TypedVar *> *args,
43 std::vector<TypedVar *> *results,
44 bool oneway,
45 std::vector<Annotation *> *annotations,
46 size_t serial,
47 MethodImpl cppImpl,
48 MethodImpl javaImpl)
49 : Method(name, args, results, oneway, annotations) {
50
51 mIsHidlReserved = true;
52 mSerial = serial;
53 mCppImpl = cppImpl;
54 mJavaImpl = javaImpl;
55}
56
57
Andreas Huber881227d2016-08-02 14:20:21 -070058std::string Method::name() const {
59 return mName;
60}
61
62const std::vector<TypedVar *> &Method::args() const {
63 return *mArgs;
64}
65
66const std::vector<TypedVar *> &Method::results() const {
67 return *mResults;
68}
69
Steven Morelandd537ab02016-09-12 10:32:01 -070070const std::vector<Annotation *> &Method::annotations() const {
71 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070072}
73
Martijn Coenen115d4282016-12-19 05:14:04 +010074void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -070075 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +010076 auto it = mCppImpl.find(type);
77 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +010078 if (it->second != nullptr) {
79 it->second(out);
80 }
Yifan Hong10fe0b52016-10-19 14:20:17 -070081 }
82}
83
Martijn Coenen115d4282016-12-19 05:14:04 +010084void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -070085 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +010086 auto it = mJavaImpl.find(type);
87 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +010088 if (it->second != nullptr) {
89 it->second(out);
90 }
Yifan Hong10fe0b52016-10-19 14:20:17 -070091 }
92}
93
Martijn Coenen115d4282016-12-19 05:14:04 +010094bool Method::overridesCppImpl(MethodImplType type) const {
95 CHECK(mIsHidlReserved);
96 return mCppImpl.find(type) != mCppImpl.end();
97}
98
99bool Method::overridesJavaImpl(MethodImplType type) const {
100 CHECK(mIsHidlReserved);
101 return mJavaImpl.find(type) != mJavaImpl.end();
102}
103
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700104void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700105 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700106 mSerial = serial;
107}
108
109size_t Method::getSerialId() const {
110 return mSerial;
111}
112
Steven Morelanda7a421a2016-09-07 08:35:18 -0700113void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -0700114 const std::string &className,
115 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700116 const bool returnsValue = !results().empty();
117
118 const TypedVar *elidedReturn = canElideCallback();
119
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700120 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
121
Steven Morelanda7a421a2016-09-07 08:35:18 -0700122 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700123 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700124 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700125 out << space
126 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700127 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700128 << "> ";
129 }
130
131 if (!className.empty()) {
132 out << className << "::";
133 }
134
135 out << name()
136 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700137 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700138
139 if (returnsValue && elidedReturn == nullptr) {
140 if (!args().empty()) {
141 out << ", ";
142 }
143
144 out << name() << "_cb _hidl_cb";
145 }
146
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800147 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700148}
149
Andreas Huber881227d2016-08-02 14:20:21 -0700150// static
Steven Moreland979e0992016-09-07 09:18:08 -0700151std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
152 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -0700153 bool first = true;
154 std::string out;
155 for (const auto &arg : args) {
156 if (!first) {
157 out += ", ";
158 }
159
Yifan Hong3b320f82016-11-01 15:15:54 -0700160 out += arg->type().getCppArgumentType(specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700161 out += " ";
162 out += arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -0700163
164 first = false;
165 }
166
167 return out;
168}
169
Andreas Huber2831d512016-08-15 09:33:47 -0700170// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700171std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700172 bool first = true;
173 std::string out;
174 for (const auto &arg : args) {
175 if (!first) {
176 out += ", ";
177 }
178
Yifan Hong4ed13472016-11-02 10:44:11 -0700179 out += arg->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700180 out += " ";
181 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700182
183 first = false;
184 }
185
186 return out;
187}
188
Andreas Huber3599d922016-08-09 10:42:57 -0700189void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700190 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700191 return;
192 }
193
194 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700195 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700196 if (i > 0) {
197 out << " ";
198 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700199 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700200 }
201 out << "\n";
202}
203
Andreas Huber70a59e12016-08-16 12:57:01 -0700204bool Method::isJavaCompatible() const {
205 for (const auto &arg : *mArgs) {
206 if (!arg->isJavaCompatible()) {
207 return false;
208 }
209 }
210
211 for (const auto &result : *mResults) {
212 if (!result->isJavaCompatible()) {
213 return false;
214 }
215 }
216
217 return true;
218}
219
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700220const TypedVar* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700221 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800222 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700223 return nullptr;
224 }
225
Steven Moreland9df52442016-12-12 08:51:14 -0800226 const TypedVar *typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700227
Steven Moreland9df52442016-12-12 08:51:14 -0800228 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100229 return typedVar;
230 }
231
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700232 return nullptr;
233}
234
Andreas Huber31629bc2016-08-03 09:06:40 -0700235////////////////////////////////////////////////////////////////////////////////
236
237TypedVar::TypedVar(const char *name, Type *type)
238 : mName(name),
239 mType(type) {
240}
241
242std::string TypedVar::name() const {
243 return mName;
244}
245
246const Type &TypedVar::type() const {
247 return *mType;
248}
249
Andreas Huber70a59e12016-08-16 12:57:01 -0700250bool TypedVar::isJavaCompatible() const {
251 return mType->isJavaCompatible();
252}
253
Yifan Hong7763ab32016-12-13 17:42:11 -0800254////////////////////////////////////////////////////////////////////////////////
255bool TypedVarVector::add(TypedVar *v) {
256 if (mNames.emplace(v->name()).second) {
257 push_back(v);
258 return true;
259 }
260 return false;
261}
262
Andreas Huberc9410c72016-07-28 12:18:40 -0700263} // namespace android
264