blob: dfac66284b68a9334a8c87b5c06eb10c3bf1acbe [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
Yifan Hong10fe0b52016-10-19 14:20:17 -070074void Method::cppImpl(Formatter &out) const {
75 CHECK(mIsHidlReserved);
76 if (mCppImpl) {
77 mCppImpl(out);
78 }
79}
80
81void Method::javaImpl(Formatter &out) const {
82 CHECK(mIsHidlReserved);
83 if (mJavaImpl) {
84 mJavaImpl(out);
85 }
86}
87
Steven Morelandef1a9fe2016-10-06 17:19:09 -070088void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070089 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -070090 mSerial = serial;
91}
92
93size_t Method::getSerialId() const {
94 return mSerial;
95}
96
Steven Morelanda7a421a2016-09-07 08:35:18 -070097void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -070098 const std::string &className,
99 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -0700100 const bool returnsValue = !results().empty();
101
102 const TypedVar *elidedReturn = canElideCallback();
103
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700104 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
105
Steven Morelanda7a421a2016-09-07 08:35:18 -0700106 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700107 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700108 } else {
109 std::string extra;
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700110 out << space
111 << "Return<"
Steven Moreland941aea12016-09-09 14:20:22 -0700112 << elidedReturn->type().getCppResultType(&extra, specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700113 << "> ";
114 }
115
116 if (!className.empty()) {
117 out << className << "::";
118 }
119
120 out << name()
121 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700122 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700123
124 if (returnsValue && elidedReturn == nullptr) {
125 if (!args().empty()) {
126 out << ", ";
127 }
128
129 out << name() << "_cb _hidl_cb";
130 }
131
132 out << ") ";
133}
134
Andreas Huber881227d2016-08-02 14:20:21 -0700135// static
Steven Moreland979e0992016-09-07 09:18:08 -0700136std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
137 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -0700138 bool first = true;
139 std::string out;
140 for (const auto &arg : args) {
141 if (!first) {
142 out += ", ";
143 }
144
145 std::string extra;
Steven Moreland979e0992016-09-07 09:18:08 -0700146 out += arg->type().getCppArgumentType(&extra,
147 specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700148 out += " ";
149 out += arg->name();
150 out += extra;
151
152 first = false;
153 }
154
155 return out;
156}
157
Andreas Huber2831d512016-08-15 09:33:47 -0700158// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700159std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700160 bool first = true;
161 std::string out;
162 for (const auto &arg : args) {
163 if (!first) {
164 out += ", ";
165 }
166
167 std::string extra;
Andreas Huber4c865b72016-09-14 15:26:27 -0700168 out += arg->type().getJavaType(&extra);
169 out += extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700170 out += " ";
171 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700172
173 first = false;
174 }
175
176 return out;
177}
178
Andreas Huber3599d922016-08-09 10:42:57 -0700179void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700180 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700181 return;
182 }
183
184 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700185 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700186 if (i > 0) {
187 out << " ";
188 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700189 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700190 }
191 out << "\n";
192}
193
Andreas Huber70a59e12016-08-16 12:57:01 -0700194bool Method::isJavaCompatible() const {
195 for (const auto &arg : *mArgs) {
196 if (!arg->isJavaCompatible()) {
197 return false;
198 }
199 }
200
201 for (const auto &result : *mResults) {
202 if (!result->isJavaCompatible()) {
203 return false;
204 }
205 }
206
207 return true;
208}
209
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700210const TypedVar* Method::canElideCallback() const {
211 auto &res = results();
212
213 // Can't elide callback for void or tuple-returning methods
214 if (res.size() != 1) {
215 return nullptr;
216 }
217
218 const TypedVar *typedVar = res.at(0);
219
220 // We only elide callbacks for methods returning a single scalar.
221 if (typedVar->type().resolveToScalarType() != nullptr) {
222 return typedVar;
223 }
224
225 return nullptr;
226}
227
Andreas Huber31629bc2016-08-03 09:06:40 -0700228////////////////////////////////////////////////////////////////////////////////
229
230TypedVar::TypedVar(const char *name, Type *type)
231 : mName(name),
232 mType(type) {
233}
234
235std::string TypedVar::name() const {
236 return mName;
237}
238
239const Type &TypedVar::type() const {
240 return *mType;
241}
242
Andreas Huber70a59e12016-08-16 12:57:01 -0700243bool TypedVar::isJavaCompatible() const {
244 return mType->isJavaCompatible();
245}
246
Andreas Huberc9410c72016-07-28 12:18:40 -0700247} // namespace android
248