blob: fabf801a32ace586a398868fc8926d683c617a03 [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
163 std::string extra;
Andreas Huber4c865b72016-09-14 15:26:27 -0700164 out += arg->type().getJavaType(&extra);
165 out += extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700166 out += " ";
167 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700168
169 first = false;
170 }
171
172 return out;
173}
174
Andreas Huber3599d922016-08-09 10:42:57 -0700175void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700176 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700177 return;
178 }
179
180 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700181 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700182 if (i > 0) {
183 out << " ";
184 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700185 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700186 }
187 out << "\n";
188}
189
Andreas Huber70a59e12016-08-16 12:57:01 -0700190bool Method::isJavaCompatible() const {
191 for (const auto &arg : *mArgs) {
192 if (!arg->isJavaCompatible()) {
193 return false;
194 }
195 }
196
197 for (const auto &result : *mResults) {
198 if (!result->isJavaCompatible()) {
199 return false;
200 }
201 }
202
203 return true;
204}
205
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700206const TypedVar* Method::canElideCallback() const {
207 auto &res = results();
208
209 // Can't elide callback for void or tuple-returning methods
210 if (res.size() != 1) {
211 return nullptr;
212 }
213
214 const TypedVar *typedVar = res.at(0);
215
216 // We only elide callbacks for methods returning a single scalar.
217 if (typedVar->type().resolveToScalarType() != nullptr) {
218 return typedVar;
219 }
220
221 return nullptr;
222}
223
Andreas Huber31629bc2016-08-03 09:06:40 -0700224////////////////////////////////////////////////////////////////////////////////
225
226TypedVar::TypedVar(const char *name, Type *type)
227 : mName(name),
228 mType(type) {
229}
230
231std::string TypedVar::name() const {
232 return mName;
233}
234
235const Type &TypedVar::type() const {
236 return *mType;
237}
238
Andreas Huber70a59e12016-08-16 12:57:01 -0700239bool TypedVar::isJavaCompatible() const {
240 return mType->isJavaCompatible();
241}
242
Andreas Huberc9410c72016-07-28 12:18:40 -0700243} // namespace android
244