blob: 5d08a47d8440b68f1e701487b63f837de72af96c [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"
Andreas Huberc9410c72016-07-28 12:18:40 -070020#include "Formatter.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070021#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070022#include "Type.h"
23
24namespace android {
25
Andreas Huber3599d922016-08-09 10:42:57 -070026Method::Method(const char *name,
27 std::vector<TypedVar *> *args,
28 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070029 bool oneway,
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070030 AnnotationVector *annotations)
Andreas Huberc9410c72016-07-28 12:18:40 -070031 : mName(name),
32 mArgs(args),
Andreas Huber3599d922016-08-09 10:42:57 -070033 mResults(results),
Iliyan Malchev639bff82016-08-13 14:24:11 -070034 mOneway(oneway),
Andreas Huber3599d922016-08-09 10:42:57 -070035 mAnnotationsByName(annotations) {
Andreas Huberc9410c72016-07-28 12:18:40 -070036}
37
Andreas Huber881227d2016-08-02 14:20:21 -070038std::string Method::name() const {
39 return mName;
40}
41
42const std::vector<TypedVar *> &Method::args() const {
43 return *mArgs;
44}
45
46const std::vector<TypedVar *> &Method::results() const {
47 return *mResults;
48}
49
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070050const AnnotationVector &Method::annotations() const {
Andreas Huber3599d922016-08-09 10:42:57 -070051 return *mAnnotationsByName;
52}
53
Steven Morelanda7a421a2016-09-07 08:35:18 -070054void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -070055 const std::string &className,
56 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -070057 const bool returnsValue = !results().empty();
58
59 const TypedVar *elidedReturn = canElideCallback();
60
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070061 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
62
Steven Morelanda7a421a2016-09-07 08:35:18 -070063 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070064 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -070065 } else {
66 std::string extra;
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070067 out << space
68 << "Return<"
Steven Morelanda7a421a2016-09-07 08:35:18 -070069 << elidedReturn->type().getCppResultType(&extra)
70 << "> ";
71 }
72
73 if (!className.empty()) {
74 out << className << "::";
75 }
76
77 out << name()
78 << "("
Steven Moreland979e0992016-09-07 09:18:08 -070079 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070080
81 if (returnsValue && elidedReturn == nullptr) {
82 if (!args().empty()) {
83 out << ", ";
84 }
85
86 out << name() << "_cb _hidl_cb";
87 }
88
89 out << ") ";
90}
91
Andreas Huber881227d2016-08-02 14:20:21 -070092// static
Steven Moreland979e0992016-09-07 09:18:08 -070093std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
94 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -070095 bool first = true;
96 std::string out;
97 for (const auto &arg : args) {
98 if (!first) {
99 out += ", ";
100 }
101
102 std::string extra;
Steven Moreland979e0992016-09-07 09:18:08 -0700103 out += arg->type().getCppArgumentType(&extra,
104 specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700105 out += " ";
106 out += arg->name();
107 out += extra;
108
109 first = false;
110 }
111
112 return out;
113}
114
Andreas Huber2831d512016-08-15 09:33:47 -0700115// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700116std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700117 bool first = true;
118 std::string out;
119 for (const auto &arg : args) {
120 if (!first) {
121 out += ", ";
122 }
123
124 std::string extra;
125 out += arg->type().getJavaType();
126 out += " ";
127 out += arg->name();
128 out += extra;
129
130 first = false;
131 }
132
133 return out;
134}
135
Andreas Huber3599d922016-08-09 10:42:57 -0700136void Method::dumpAnnotations(Formatter &out) const {
137 if (mAnnotationsByName->size() == 0) {
138 return;
139 }
140
141 out << "// ";
142 for (size_t i = 0; i < mAnnotationsByName->size(); ++i) {
143 if (i > 0) {
144 out << " ";
145 }
146 mAnnotationsByName->valueAt(i)->dump(out);
147 }
148 out << "\n";
149}
150
Andreas Huber70a59e12016-08-16 12:57:01 -0700151bool Method::isJavaCompatible() const {
152 for (const auto &arg : *mArgs) {
153 if (!arg->isJavaCompatible()) {
154 return false;
155 }
156 }
157
158 for (const auto &result : *mResults) {
159 if (!result->isJavaCompatible()) {
160 return false;
161 }
162 }
163
164 return true;
165}
166
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700167const TypedVar* Method::canElideCallback() const {
168 auto &res = results();
169
170 // Can't elide callback for void or tuple-returning methods
171 if (res.size() != 1) {
172 return nullptr;
173 }
174
175 const TypedVar *typedVar = res.at(0);
176
177 // We only elide callbacks for methods returning a single scalar.
178 if (typedVar->type().resolveToScalarType() != nullptr) {
179 return typedVar;
180 }
181
182 return nullptr;
183}
184
Andreas Huber31629bc2016-08-03 09:06:40 -0700185////////////////////////////////////////////////////////////////////////////////
186
187TypedVar::TypedVar(const char *name, Type *type)
188 : mName(name),
189 mType(type) {
190}
191
192std::string TypedVar::name() const {
193 return mName;
194}
195
196const Type &TypedVar::type() const {
197 return *mType;
198}
199
Andreas Huber70a59e12016-08-16 12:57:01 -0700200bool TypedVar::isJavaCompatible() const {
201 return mType->isJavaCompatible();
202}
203
Andreas Huberc9410c72016-07-28 12:18:40 -0700204} // namespace android
205