blob: 8b456f52cbd6db384209093818626f66f1782df1 [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
61 if (elidedReturn == nullptr) {
Iliyan Malchevd57066f2016-09-08 13:59:38 -070062 out << "::android::hardware::Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -070063 } else {
64 std::string extra;
65 out << "::android::hardware::Return<"
66 << elidedReturn->type().getCppResultType(&extra)
67 << "> ";
68 }
69
70 if (!className.empty()) {
71 out << className << "::";
72 }
73
74 out << name()
75 << "("
Steven Moreland979e0992016-09-07 09:18:08 -070076 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070077
78 if (returnsValue && elidedReturn == nullptr) {
79 if (!args().empty()) {
80 out << ", ";
81 }
82
83 out << name() << "_cb _hidl_cb";
84 }
85
86 out << ") ";
87}
88
Andreas Huber881227d2016-08-02 14:20:21 -070089// static
Steven Moreland979e0992016-09-07 09:18:08 -070090std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
91 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -070092 bool first = true;
93 std::string out;
94 for (const auto &arg : args) {
95 if (!first) {
96 out += ", ";
97 }
98
99 std::string extra;
Steven Moreland979e0992016-09-07 09:18:08 -0700100 out += arg->type().getCppArgumentType(&extra,
101 specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700102 out += " ";
103 out += arg->name();
104 out += extra;
105
106 first = false;
107 }
108
109 return out;
110}
111
Andreas Huber2831d512016-08-15 09:33:47 -0700112// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700113std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700114 bool first = true;
115 std::string out;
116 for (const auto &arg : args) {
117 if (!first) {
118 out += ", ";
119 }
120
121 std::string extra;
122 out += arg->type().getJavaType();
123 out += " ";
124 out += arg->name();
125 out += extra;
126
127 first = false;
128 }
129
130 return out;
131}
132
Andreas Huber3599d922016-08-09 10:42:57 -0700133void Method::dumpAnnotations(Formatter &out) const {
134 if (mAnnotationsByName->size() == 0) {
135 return;
136 }
137
138 out << "// ";
139 for (size_t i = 0; i < mAnnotationsByName->size(); ++i) {
140 if (i > 0) {
141 out << " ";
142 }
143 mAnnotationsByName->valueAt(i)->dump(out);
144 }
145 out << "\n";
146}
147
Andreas Huber70a59e12016-08-16 12:57:01 -0700148bool Method::isJavaCompatible() const {
149 for (const auto &arg : *mArgs) {
150 if (!arg->isJavaCompatible()) {
151 return false;
152 }
153 }
154
155 for (const auto &result : *mResults) {
156 if (!result->isJavaCompatible()) {
157 return false;
158 }
159 }
160
161 return true;
162}
163
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700164const TypedVar* Method::canElideCallback() const {
165 auto &res = results();
166
167 // Can't elide callback for void or tuple-returning methods
168 if (res.size() != 1) {
169 return nullptr;
170 }
171
172 const TypedVar *typedVar = res.at(0);
173
174 // We only elide callbacks for methods returning a single scalar.
175 if (typedVar->type().resolveToScalarType() != nullptr) {
176 return typedVar;
177 }
178
179 return nullptr;
180}
181
Andreas Huber31629bc2016-08-03 09:06:40 -0700182////////////////////////////////////////////////////////////////////////////////
183
184TypedVar::TypedVar(const char *name, Type *type)
185 : mName(name),
186 mType(type) {
187}
188
189std::string TypedVar::name() const {
190 return mName;
191}
192
193const Type &TypedVar::type() const {
194 return *mType;
195}
196
Andreas Huber70a59e12016-08-16 12:57:01 -0700197bool TypedVar::isJavaCompatible() const {
198 return mType->isJavaCompatible();
199}
200
Andreas Huberc9410c72016-07-28 12:18:40 -0700201} // namespace android
202