blob: 95a688a95aca22aea692779a593324d3c8e4e7c1 [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
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070023#include <hidl-util/Formatter.h>
24
Andreas Huberc9410c72016-07-28 12:18:40 -070025namespace android {
26
Andreas Huber3599d922016-08-09 10:42:57 -070027Method::Method(const char *name,
28 std::vector<TypedVar *> *args,
29 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070030 bool oneway,
Steven Morelandd537ab02016-09-12 10:32:01 -070031 std::vector<Annotation *> *annotations)
Andreas Huberc9410c72016-07-28 12:18:40 -070032 : mName(name),
33 mArgs(args),
Andreas Huber3599d922016-08-09 10:42:57 -070034 mResults(results),
Iliyan Malchev639bff82016-08-13 14:24:11 -070035 mOneway(oneway),
Steven Morelandd537ab02016-09-12 10:32:01 -070036 mAnnotations(annotations) {
Andreas Huberc9410c72016-07-28 12:18:40 -070037}
38
Andreas Huber881227d2016-08-02 14:20:21 -070039std::string Method::name() const {
40 return mName;
41}
42
43const std::vector<TypedVar *> &Method::args() const {
44 return *mArgs;
45}
46
47const std::vector<TypedVar *> &Method::results() const {
48 return *mResults;
49}
50
Steven Morelandd537ab02016-09-12 10:32:01 -070051const std::vector<Annotation *> &Method::annotations() const {
52 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070053}
54
Steven Morelandef1a9fe2016-10-06 17:19:09 -070055void Method::setSerialId(size_t serial) {
56 mSerial = serial;
57}
58
59size_t Method::getSerialId() const {
60 return mSerial;
61}
62
Steven Morelanda7a421a2016-09-07 08:35:18 -070063void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -070064 const std::string &className,
65 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -070066 const bool returnsValue = !results().empty();
67
68 const TypedVar *elidedReturn = canElideCallback();
69
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070070 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
71
Steven Morelanda7a421a2016-09-07 08:35:18 -070072 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070073 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -070074 } else {
75 std::string extra;
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070076 out << space
77 << "Return<"
Steven Moreland941aea12016-09-09 14:20:22 -070078 << elidedReturn->type().getCppResultType(&extra, specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -070079 << "> ";
80 }
81
82 if (!className.empty()) {
83 out << className << "::";
84 }
85
86 out << name()
87 << "("
Steven Moreland979e0992016-09-07 09:18:08 -070088 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070089
90 if (returnsValue && elidedReturn == nullptr) {
91 if (!args().empty()) {
92 out << ", ";
93 }
94
95 out << name() << "_cb _hidl_cb";
96 }
97
98 out << ") ";
99}
100
Andreas Huber881227d2016-08-02 14:20:21 -0700101// static
Steven Moreland979e0992016-09-07 09:18:08 -0700102std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
103 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -0700104 bool first = true;
105 std::string out;
106 for (const auto &arg : args) {
107 if (!first) {
108 out += ", ";
109 }
110
111 std::string extra;
Steven Moreland979e0992016-09-07 09:18:08 -0700112 out += arg->type().getCppArgumentType(&extra,
113 specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700114 out += " ";
115 out += arg->name();
116 out += extra;
117
118 first = false;
119 }
120
121 return out;
122}
123
Andreas Huber2831d512016-08-15 09:33:47 -0700124// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700125std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700126 bool first = true;
127 std::string out;
128 for (const auto &arg : args) {
129 if (!first) {
130 out += ", ";
131 }
132
133 std::string extra;
Andreas Huber4c865b72016-09-14 15:26:27 -0700134 out += arg->type().getJavaType(&extra);
135 out += extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700136 out += " ";
137 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700138
139 first = false;
140 }
141
142 return out;
143}
144
Andreas Huber3599d922016-08-09 10:42:57 -0700145void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700146 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700147 return;
148 }
149
150 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700151 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700152 if (i > 0) {
153 out << " ";
154 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700155 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700156 }
157 out << "\n";
158}
159
Andreas Huber70a59e12016-08-16 12:57:01 -0700160bool Method::isJavaCompatible() const {
161 for (const auto &arg : *mArgs) {
162 if (!arg->isJavaCompatible()) {
163 return false;
164 }
165 }
166
167 for (const auto &result : *mResults) {
168 if (!result->isJavaCompatible()) {
169 return false;
170 }
171 }
172
173 return true;
174}
175
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700176const TypedVar* Method::canElideCallback() const {
177 auto &res = results();
178
179 // Can't elide callback for void or tuple-returning methods
180 if (res.size() != 1) {
181 return nullptr;
182 }
183
184 const TypedVar *typedVar = res.at(0);
185
186 // We only elide callbacks for methods returning a single scalar.
187 if (typedVar->type().resolveToScalarType() != nullptr) {
188 return typedVar;
189 }
190
191 return nullptr;
192}
193
Andreas Huber31629bc2016-08-03 09:06:40 -0700194////////////////////////////////////////////////////////////////////////////////
195
196TypedVar::TypedVar(const char *name, Type *type)
197 : mName(name),
198 mType(type) {
199}
200
201std::string TypedVar::name() const {
202 return mName;
203}
204
205const Type &TypedVar::type() const {
206 return *mType;
207}
208
Andreas Huber70a59e12016-08-16 12:57:01 -0700209bool TypedVar::isJavaCompatible() const {
210 return mType->isJavaCompatible();
211}
212
Andreas Huberc9410c72016-07-28 12:18:40 -0700213} // namespace android
214