blob: 38a6c04b0526508035fd62cc837652c3ea170658 [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;
48}
49
Andreas Huber881227d2016-08-02 14:20:21 -070050std::string Method::name() const {
51 return mName;
52}
53
54const std::vector<TypedVar *> &Method::args() const {
55 return *mArgs;
56}
57
58const std::vector<TypedVar *> &Method::results() const {
59 return *mResults;
60}
61
Steven Morelandd537ab02016-09-12 10:32:01 -070062const std::vector<Annotation *> &Method::annotations() const {
63 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070064}
65
Martijn Coenen115d4282016-12-19 05:14:04 +010066void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -070067 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +010068 auto it = mCppImpl.find(type);
69 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +010070 if (it->second != nullptr) {
71 it->second(out);
72 }
Yifan Hong10fe0b52016-10-19 14:20:17 -070073 }
74}
75
Martijn Coenen115d4282016-12-19 05:14:04 +010076void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -070077 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +010078 auto it = mJavaImpl.find(type);
79 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +010080 if (it->second != nullptr) {
81 it->second(out);
82 }
Yifan Hong10fe0b52016-10-19 14:20:17 -070083 }
84}
85
Martijn Coenen115d4282016-12-19 05:14:04 +010086bool Method::overridesCppImpl(MethodImplType type) const {
87 CHECK(mIsHidlReserved);
88 return mCppImpl.find(type) != mCppImpl.end();
89}
90
91bool Method::overridesJavaImpl(MethodImplType type) const {
92 CHECK(mIsHidlReserved);
93 return mJavaImpl.find(type) != mJavaImpl.end();
94}
95
Yifan Hongffa91392017-01-31 13:41:23 -080096Method *Method::copySignature() const {
97 return new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations);
98}
99
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700100void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700101 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700102 mSerial = serial;
103}
104
105size_t Method::getSerialId() const {
106 return mSerial;
107}
108
Steven Morelanda7a421a2016-09-07 08:35:18 -0700109void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -0700110 const std::string &className,
111 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700112 const bool returnsValue = !results().empty();
113
114 const TypedVar *elidedReturn = canElideCallback();
115
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700116 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
117
Steven Morelanda7a421a2016-09-07 08:35:18 -0700118 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700119 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700120 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700121 out << space
122 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700123 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700124 << "> ";
125 }
126
127 if (!className.empty()) {
128 out << className << "::";
129 }
130
131 out << name()
132 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700133 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700134
135 if (returnsValue && elidedReturn == nullptr) {
136 if (!args().empty()) {
137 out << ", ";
138 }
139
140 out << name() << "_cb _hidl_cb";
141 }
142
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800143 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700144}
145
Andreas Huber881227d2016-08-02 14:20:21 -0700146// static
Steven Moreland979e0992016-09-07 09:18:08 -0700147std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
148 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -0700149 bool first = true;
150 std::string out;
151 for (const auto &arg : args) {
152 if (!first) {
153 out += ", ";
154 }
155
Yifan Hong3b320f82016-11-01 15:15:54 -0700156 out += arg->type().getCppArgumentType(specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700157 out += " ";
158 out += arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -0700159
160 first = false;
161 }
162
163 return out;
164}
165
Andreas Huber2831d512016-08-15 09:33:47 -0700166// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700167std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700168 bool first = true;
169 std::string out;
170 for (const auto &arg : args) {
171 if (!first) {
172 out += ", ";
173 }
174
Yifan Hong4ed13472016-11-02 10:44:11 -0700175 out += arg->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700176 out += " ";
177 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700178
179 first = false;
180 }
181
182 return out;
183}
184
Andreas Huber3599d922016-08-09 10:42:57 -0700185void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700186 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700187 return;
188 }
189
190 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700191 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700192 if (i > 0) {
193 out << " ";
194 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700195 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700196 }
197 out << "\n";
198}
199
Andreas Huber70a59e12016-08-16 12:57:01 -0700200bool Method::isJavaCompatible() const {
201 for (const auto &arg : *mArgs) {
202 if (!arg->isJavaCompatible()) {
203 return false;
204 }
205 }
206
207 for (const auto &result : *mResults) {
208 if (!result->isJavaCompatible()) {
209 return false;
210 }
211 }
212
213 return true;
214}
215
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700216const TypedVar* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700217 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800218 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700219 return nullptr;
220 }
221
Steven Moreland9df52442016-12-12 08:51:14 -0800222 const TypedVar *typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700223
Steven Moreland9df52442016-12-12 08:51:14 -0800224 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100225 return typedVar;
226 }
227
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700228 return nullptr;
229}
230
Andreas Huber31629bc2016-08-03 09:06:40 -0700231////////////////////////////////////////////////////////////////////////////////
232
233TypedVar::TypedVar(const char *name, Type *type)
234 : mName(name),
235 mType(type) {
236}
237
238std::string TypedVar::name() const {
239 return mName;
240}
241
242const Type &TypedVar::type() const {
243 return *mType;
244}
245
Andreas Huber70a59e12016-08-16 12:57:01 -0700246bool TypedVar::isJavaCompatible() const {
247 return mType->isJavaCompatible();
248}
249
Yifan Hong7763ab32016-12-13 17:42:11 -0800250////////////////////////////////////////////////////////////////////////////////
251bool TypedVarVector::add(TypedVar *v) {
252 if (mNames.emplace(v->name()).second) {
253 push_back(v);
254 return true;
255 }
256 return false;
257}
258
Andreas Huberc9410c72016-07-28 12:18:40 -0700259} // namespace android
260