blob: bfd28c987324d86e03e7a080aa9510c076a58420 [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 {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700109 out << space
110 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700111 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700112 << "> ";
113 }
114
115 if (!className.empty()) {
116 out << className << "::";
117 }
118
119 out << name()
120 << "("
Steven Moreland979e0992016-09-07 09:18:08 -0700121 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700122
123 if (returnsValue && elidedReturn == nullptr) {
124 if (!args().empty()) {
125 out << ", ";
126 }
127
128 out << name() << "_cb _hidl_cb";
129 }
130
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800131 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700132}
133
Andreas Huber881227d2016-08-02 14:20:21 -0700134// static
Steven Moreland979e0992016-09-07 09:18:08 -0700135std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
136 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -0700137 bool first = true;
138 std::string out;
139 for (const auto &arg : args) {
140 if (!first) {
141 out += ", ";
142 }
143
Yifan Hong3b320f82016-11-01 15:15:54 -0700144 out += arg->type().getCppArgumentType(specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700145 out += " ";
146 out += arg->name();
Andreas Huber881227d2016-08-02 14:20:21 -0700147
148 first = false;
149 }
150
151 return out;
152}
153
Andreas Huber2831d512016-08-15 09:33:47 -0700154// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700155std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700156 bool first = true;
157 std::string out;
158 for (const auto &arg : args) {
159 if (!first) {
160 out += ", ";
161 }
162
Yifan Hong4ed13472016-11-02 10:44:11 -0700163 out += arg->type().getJavaType();
Andreas Huber2831d512016-08-15 09:33:47 -0700164 out += " ";
165 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700166
167 first = false;
168 }
169
170 return out;
171}
172
Andreas Huber3599d922016-08-09 10:42:57 -0700173void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700174 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700175 return;
176 }
177
178 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700179 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700180 if (i > 0) {
181 out << " ";
182 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700183 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700184 }
185 out << "\n";
186}
187
Andreas Huber70a59e12016-08-16 12:57:01 -0700188bool Method::isJavaCompatible() const {
189 for (const auto &arg : *mArgs) {
190 if (!arg->isJavaCompatible()) {
191 return false;
192 }
193 }
194
195 for (const auto &result : *mResults) {
196 if (!result->isJavaCompatible()) {
197 return false;
198 }
199 }
200
201 return true;
202}
203
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700204const TypedVar* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700205 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800206 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700207 return nullptr;
208 }
209
Steven Moreland9df52442016-12-12 08:51:14 -0800210 const TypedVar *typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700211
Steven Moreland9df52442016-12-12 08:51:14 -0800212 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100213 return typedVar;
214 }
215
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700216 return nullptr;
217}
218
Andreas Huber31629bc2016-08-03 09:06:40 -0700219////////////////////////////////////////////////////////////////////////////////
220
221TypedVar::TypedVar(const char *name, Type *type)
222 : mName(name),
223 mType(type) {
224}
225
226std::string TypedVar::name() const {
227 return mName;
228}
229
230const Type &TypedVar::type() const {
231 return *mType;
232}
233
Andreas Huber70a59e12016-08-16 12:57:01 -0700234bool TypedVar::isJavaCompatible() const {
235 return mType->isJavaCompatible();
236}
237
Yifan Hong7763ab32016-12-13 17:42:11 -0800238////////////////////////////////////////////////////////////////////////////////
239bool TypedVarVector::add(TypedVar *v) {
240 if (mNames.emplace(v->name()).second) {
241 push_back(v);
242 return true;
243 }
244 return false;
245}
246
Andreas Huberc9410c72016-07-28 12:18:40 -0700247} // namespace android
248