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