blob: 476a10bd77efcb376fd56928e98b59193ae13804 [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
Martijn Coenen115d4282016-12-19 05:14:04 +010092bool Method::overridesCppImpl(MethodImplType type) const {
93 CHECK(mIsHidlReserved);
94 return mCppImpl.find(type) != mCppImpl.end();
95}
96
97bool Method::overridesJavaImpl(MethodImplType type) const {
98 CHECK(mIsHidlReserved);
99 return mJavaImpl.find(type) != mJavaImpl.end();
100}
101
Yifan Hongffa91392017-01-31 13:41:23 -0800102Method *Method::copySignature() const {
103 return new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations);
104}
105
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700106void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700107 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700108 mSerial = serial;
109}
110
111size_t Method::getSerialId() const {
112 return mSerial;
113}
114
Steven Morelanda7a421a2016-09-07 08:35:18 -0700115void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -0700116 const std::string &className,
117 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700118 const bool returnsValue = !results().empty();
119
120 const TypedVar *elidedReturn = canElideCallback();
121
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700122 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
123
Steven Morelanda7a421a2016-09-07 08:35:18 -0700124 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700125 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700126 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700127 out << space
128 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700129 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700130 << "> ";
131 }
132
133 if (!className.empty()) {
134 out << className << "::";
135 }
136
137 out << name()
138 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700139 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700140
141 if (returnsValue && elidedReturn == nullptr) {
142 if (!args().empty()) {
143 out << ", ";
144 }
145
146 out << name() << "_cb _hidl_cb";
147 }
148
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800149 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700150}
151
Andreas Huber881227d2016-08-02 14:20:21 -0700152// static
Steven Moreland979e0992016-09-07 09:18:08 -0700153std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
154 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -0700155 bool first = true;
156 std::string out;
157 for (const auto &arg : args) {
158 if (!first) {
159 out += ", ";
160 }
161
Yifan Hong3b320f82016-11-01 15:15:54 -0700162 out += arg->type().getCppArgumentType(specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700163 out += " ";
164 out += arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -0700165
166 first = false;
167 }
168
169 return out;
170}
171
Andreas Huber2831d512016-08-15 09:33:47 -0700172// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700173std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700174 bool first = true;
175 std::string out;
176 for (const auto &arg : args) {
177 if (!first) {
178 out += ", ";
179 }
180
Yifan Hong4ed13472016-11-02 10:44:11 -0700181 out += arg->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700182 out += " ";
183 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700184
185 first = false;
186 }
187
188 return out;
189}
190
Andreas Huber3599d922016-08-09 10:42:57 -0700191void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700192 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700193 return;
194 }
195
196 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700197 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700198 if (i > 0) {
199 out << " ";
200 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700201 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700202 }
203 out << "\n";
204}
205
Andreas Huber70a59e12016-08-16 12:57:01 -0700206bool Method::isJavaCompatible() const {
207 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