blob: 7d7717ae580b4e0693620319a8bbcc3f12619343 [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 Morelanda7a421a2016-09-07 08:35:18 -070055void Method::generateCppSignature(Formatter &out,
Steven Moreland979e0992016-09-07 09:18:08 -070056 const std::string &className,
57 bool specifyNamespaces) const {
Steven Morelanda7a421a2016-09-07 08:35:18 -070058 const bool returnsValue = !results().empty();
59
60 const TypedVar *elidedReturn = canElideCallback();
61
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070062 std::string space = (specifyNamespaces ? "::android::hardware::" : "");
63
Steven Morelanda7a421a2016-09-07 08:35:18 -070064 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070065 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -070066 } else {
67 std::string extra;
Iliyan Malchev7f949cb2016-09-09 13:16:19 -070068 out << space
69 << "Return<"
Steven Moreland941aea12016-09-09 14:20:22 -070070 << elidedReturn->type().getCppResultType(&extra, specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -070071 << "> ";
72 }
73
74 if (!className.empty()) {
75 out << className << "::";
76 }
77
78 out << name()
79 << "("
Steven Moreland979e0992016-09-07 09:18:08 -070080 << GetArgSignature(args(), specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070081
82 if (returnsValue && elidedReturn == nullptr) {
83 if (!args().empty()) {
84 out << ", ";
85 }
86
87 out << name() << "_cb _hidl_cb";
88 }
89
90 out << ") ";
91}
92
Andreas Huber881227d2016-08-02 14:20:21 -070093// static
Steven Moreland979e0992016-09-07 09:18:08 -070094std::string Method::GetArgSignature(const std::vector<TypedVar *> &args,
95 bool specifyNamespaces) {
Andreas Huber881227d2016-08-02 14:20:21 -070096 bool first = true;
97 std::string out;
98 for (const auto &arg : args) {
99 if (!first) {
100 out += ", ";
101 }
102
103 std::string extra;
Steven Moreland979e0992016-09-07 09:18:08 -0700104 out += arg->type().getCppArgumentType(&extra,
105 specifyNamespaces);
Andreas Huber881227d2016-08-02 14:20:21 -0700106 out += " ";
107 out += arg->name();
108 out += extra;
109
110 first = false;
111 }
112
113 return out;
114}
115
Andreas Huber2831d512016-08-15 09:33:47 -0700116// static
Steven Morelanda7a421a2016-09-07 08:35:18 -0700117std::string Method::GetJavaArgSignature(const std::vector<TypedVar *> &args) {
Andreas Huber2831d512016-08-15 09:33:47 -0700118 bool first = true;
119 std::string out;
120 for (const auto &arg : args) {
121 if (!first) {
122 out += ", ";
123 }
124
125 std::string extra;
Andreas Huber4c865b72016-09-14 15:26:27 -0700126 out += arg->type().getJavaType(&extra);
127 out += extra;
Andreas Huber2831d512016-08-15 09:33:47 -0700128 out += " ";
129 out += arg->name();
Andreas Huber2831d512016-08-15 09:33:47 -0700130
131 first = false;
132 }
133
134 return out;
135}
136
Andreas Huber3599d922016-08-09 10:42:57 -0700137void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700138 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700139 return;
140 }
141
142 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700143 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700144 if (i > 0) {
145 out << " ";
146 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700147 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700148 }
149 out << "\n";
150}
151
Andreas Huber70a59e12016-08-16 12:57:01 -0700152bool Method::isJavaCompatible() const {
153 for (const auto &arg : *mArgs) {
154 if (!arg->isJavaCompatible()) {
155 return false;
156 }
157 }
158
159 for (const auto &result : *mResults) {
160 if (!result->isJavaCompatible()) {
161 return false;
162 }
163 }
164
165 return true;
166}
167
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700168const TypedVar* Method::canElideCallback() const {
169 auto &res = results();
170
171 // Can't elide callback for void or tuple-returning methods
172 if (res.size() != 1) {
173 return nullptr;
174 }
175
176 const TypedVar *typedVar = res.at(0);
177
178 // We only elide callbacks for methods returning a single scalar.
179 if (typedVar->type().resolveToScalarType() != nullptr) {
180 return typedVar;
181 }
182
183 return nullptr;
184}
185
Andreas Huber31629bc2016-08-03 09:06:40 -0700186////////////////////////////////////////////////////////////////////////////////
187
188TypedVar::TypedVar(const char *name, Type *type)
189 : mName(name),
190 mType(type) {
191}
192
193std::string TypedVar::name() const {
194 return mName;
195}
196
197const Type &TypedVar::type() const {
198 return *mType;
199}
200
Andreas Huber70a59e12016-08-16 12:57:01 -0700201bool TypedVar::isJavaCompatible() const {
202 return mType->isJavaCompatible();
203}
204
Andreas Huberc9410c72016-07-28 12:18:40 -0700205} // namespace android
206