blob: d81af7c3bbe3ee056de056cd08044f56c7035b9f [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"
Timur Iskhakov891a8662017-08-25 21:53:48 -070020#include "ConstantExpression.h"
Iliyan Malchev40d474a2016-08-16 06:20:17 -070021#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070022#include "Type.h"
23
Yifan Hong10fe0b52016-10-19 14:20:17 -070024#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070025#include <hidl-util/Formatter.h>
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070026#include <algorithm>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070027
Andreas Huberc9410c72016-07-28 12:18:40 -070028namespace android {
29
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070030Method::Method(const char* name, std::vector<NamedReference<Type>*>* args,
31 std::vector<NamedReference<Type>*>* results, bool oneway,
Timur Iskhakovcec46c42017-08-09 00:22:02 -070032 std::vector<Annotation*>* annotations, const Location& location)
33 : mName(name),
34 mArgs(args),
35 mResults(results),
36 mOneway(oneway),
37 mAnnotations(annotations),
38 mLocation(location) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070039
Yifan Hongffa91392017-01-31 13:41:23 -080040void Method::fillImplementation(
41 size_t serial,
42 MethodImpl cppImpl,
43 MethodImpl javaImpl) {
Yifan Hong10fe0b52016-10-19 14:20:17 -070044 mIsHidlReserved = true;
45 mSerial = serial;
46 mCppImpl = cppImpl;
47 mJavaImpl = javaImpl;
Yifan Hongcd2ae452017-01-31 14:33:40 -080048
49 CHECK(mJavaImpl.find(IMPL_STUB_IMPL) == mJavaImpl.end())
Steven Moreland937408a2017-03-20 09:54:18 -070050 << "FATAL: mJavaImpl should not use IMPL_STUB_IMPL; use IMPL_INTERFACE instead.";
Yifan Hongcd2ae452017-01-31 14:33:40 -080051 CHECK(mCppImpl.find(IMPL_STUB_IMPL) == mCppImpl.end() ||
52 mCppImpl.find(IMPL_STUB) == mCppImpl.end())
53 << "FATAL: mCppImpl IMPL_STUB will override IMPL_STUB_IMPL.";
Yifan Hong10fe0b52016-10-19 14:20:17 -070054}
55
Andreas Huber881227d2016-08-02 14:20:21 -070056std::string Method::name() const {
57 return mName;
58}
59
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070060const std::vector<NamedReference<Type>*>& Method::args() const {
Andreas Huber881227d2016-08-02 14:20:21 -070061 return *mArgs;
62}
63
Timur Iskhakov7fa79f62017-08-09 11:04:54 -070064const std::vector<NamedReference<Type>*>& Method::results() const {
Andreas Huber881227d2016-08-02 14:20:21 -070065 return *mResults;
66}
67
Steven Morelandd537ab02016-09-12 10:32:01 -070068const std::vector<Annotation *> &Method::annotations() const {
69 return *mAnnotations;
Andreas Huber3599d922016-08-09 10:42:57 -070070}
71
Timur Iskhakovb58f4182017-08-29 15:19:24 -070072std::vector<Reference<Type>*> Method::getReferences() {
73 const auto& constRet = static_cast<const Method*>(this)->getReferences();
74 std::vector<Reference<Type>*> ret(constRet.size());
75 std::transform(constRet.begin(), constRet.end(), ret.begin(),
76 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
Timur Iskhakov33431e62017-08-21 17:31:23 -070077 return ret;
78}
79
Timur Iskhakovb58f4182017-08-29 15:19:24 -070080std::vector<const Reference<Type>*> Method::getReferences() const {
81 std::vector<const Reference<Type>*> ret;
82 ret.insert(ret.end(), mArgs->begin(), mArgs->end());
83 ret.insert(ret.end(), mResults->begin(), mResults->end());
84 return ret;
85}
86
Timur Iskhakovff5e64a2017-09-11 14:56:18 -070087std::vector<Reference<Type>*> Method::getStrongReferences() {
88 const auto& constRet = static_cast<const Method*>(this)->getStrongReferences();
89 std::vector<Reference<Type>*> ret(constRet.size());
90 std::transform(constRet.begin(), constRet.end(), ret.begin(),
91 [](const auto* ref) { return const_cast<Reference<Type>*>(ref); });
92 return ret;
93}
94
95std::vector<const Reference<Type>*> Method::getStrongReferences() const {
96 std::vector<const Reference<Type>*> ret;
97 for (const auto* ref : getReferences()) {
98 if (!ref->shallowGet()->isNeverStrongReference()) {
99 ret.push_back(ref);
100 }
101 }
102 return ret;
103}
104
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700105std::vector<ConstantExpression*> Method::getConstantExpressions() {
106 const auto& constRet = static_cast<const Method*>(this)->getConstantExpressions();
107 std::vector<ConstantExpression*> ret(constRet.size());
108 std::transform(constRet.begin(), constRet.end(), ret.begin(),
109 [](const auto* ce) { return const_cast<ConstantExpression*>(ce); });
110 return ret;
111}
112
113std::vector<const ConstantExpression*> Method::getConstantExpressions() const {
114 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700115 for (const auto* annotation : *mAnnotations) {
116 const auto& retAnnotation = annotation->getConstantExpressions();
117 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700118 }
Timur Iskhakov891a8662017-08-25 21:53:48 -0700119 return ret;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700120}
121
Martijn Coenen115d4282016-12-19 05:14:04 +0100122void Method::cppImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700123 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100124 auto it = mCppImpl.find(type);
125 if (it != mCppImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100126 if (it->second != nullptr) {
127 it->second(out);
128 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700129 }
130}
131
Martijn Coenen115d4282016-12-19 05:14:04 +0100132void Method::javaImpl(MethodImplType type, Formatter &out) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700133 CHECK(mIsHidlReserved);
Martijn Coenen115d4282016-12-19 05:14:04 +0100134 auto it = mJavaImpl.find(type);
135 if (it != mJavaImpl.end()) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100136 if (it->second != nullptr) {
137 it->second(out);
138 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700139 }
140}
141
Martijn Coenen115d4282016-12-19 05:14:04 +0100142bool Method::overridesCppImpl(MethodImplType type) const {
143 CHECK(mIsHidlReserved);
144 return mCppImpl.find(type) != mCppImpl.end();
145}
146
147bool Method::overridesJavaImpl(MethodImplType type) const {
148 CHECK(mIsHidlReserved);
149 return mJavaImpl.find(type) != mJavaImpl.end();
150}
151
Yifan Hongffa91392017-01-31 13:41:23 -0800152Method *Method::copySignature() const {
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700153 return new Method(mName.c_str(), mArgs, mResults, mOneway, mAnnotations, Location());
Yifan Hongffa91392017-01-31 13:41:23 -0800154}
155
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700156void Method::setSerialId(size_t serial) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700157 CHECK(!mIsHidlReserved);
Steven Morelandef1a9fe2016-10-06 17:19:09 -0700158 mSerial = serial;
159}
160
161size_t Method::getSerialId() const {
162 return mSerial;
163}
164
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700165bool Method::hasEmptyCppArgSignature() const {
166 return args().empty() && (results().empty() || canElideCallback() != nullptr);
167}
Steven Morelanda7a421a2016-09-07 08:35:18 -0700168
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700169void Method::generateCppReturnType(Formatter &out, bool specifyNamespaces) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700170 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700171 const std::string space = (specifyNamespaces ? "::android::hardware::" : "");
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700172
Steven Morelanda7a421a2016-09-07 08:35:18 -0700173 if (elidedReturn == nullptr) {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700174 out << space << "Return<void> ";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700175 } else {
Iliyan Malchev7f949cb2016-09-09 13:16:19 -0700176 out << space
177 << "Return<"
Yifan Hong3b320f82016-11-01 15:15:54 -0700178 << elidedReturn->type().getCppResultType( specifyNamespaces)
Steven Morelanda7a421a2016-09-07 08:35:18 -0700179 << "> ";
180 }
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700181}
182
183void Method::generateCppSignature(Formatter &out,
184 const std::string &className,
185 bool specifyNamespaces) const {
186 generateCppReturnType(out, specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -0700187
188 if (!className.empty()) {
189 out << className << "::";
190 }
191
192 out << name()
Yifan Hong932464e2017-03-30 15:40:22 -0700193 << "(";
194 emitCppArgSignature(out, specifyNamespaces);
Steven Moreland41c6d2e2016-11-07 12:26:54 -0800195 out << ")";
Steven Morelanda7a421a2016-09-07 08:35:18 -0700196}
197
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700198static void emitCppArgResultSignature(Formatter& out,
199 const std::vector<NamedReference<Type>*>& args,
200 bool specifyNamespaces) {
Yifan Hong932464e2017-03-30 15:40:22 -0700201 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
202 out << arg->type().getCppArgumentType(specifyNamespaces);
203 out << " ";
204 out << arg->name();
205 });
Andreas Huber881227d2016-08-02 14:20:21 -0700206}
207
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700208static void emitJavaArgResultSignature(Formatter& out,
209 const std::vector<NamedReference<Type>*>& args) {
Yifan Hong932464e2017-03-30 15:40:22 -0700210 out.join(args.begin(), args.end(), ", ", [&](auto arg) {
211 out << arg->type().getJavaType();
212 out << " ";
213 out << arg->name();
214 });
215}
Andreas Huber2831d512016-08-15 09:33:47 -0700216
Yifan Hong932464e2017-03-30 15:40:22 -0700217void Method::emitCppArgSignature(Formatter &out, bool specifyNamespaces) const {
218 emitCppArgResultSignature(out, args(), specifyNamespaces);
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700219
220 const bool returnsValue = !results().empty();
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700221 const NamedReference<Type>* elidedReturn = canElideCallback();
Steven Morelandd8b10ee2017-07-31 15:06:20 -0700222 if (returnsValue && elidedReturn == nullptr) {
223 if (!args().empty()) {
224 out << ", ";
225 }
226
227 out << name() << "_cb _hidl_cb";
228 }
Yifan Hong932464e2017-03-30 15:40:22 -0700229}
230void Method::emitCppResultSignature(Formatter &out, bool specifyNamespaces) const {
231 emitCppArgResultSignature(out, results(), specifyNamespaces);
232}
233void Method::emitJavaArgSignature(Formatter &out) const {
234 emitJavaArgResultSignature(out, args());
235}
236void Method::emitJavaResultSignature(Formatter &out) const {
237 emitJavaArgResultSignature(out, results());
Andreas Huber2831d512016-08-15 09:33:47 -0700238}
239
Andreas Huber3599d922016-08-09 10:42:57 -0700240void Method::dumpAnnotations(Formatter &out) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700241 if (mAnnotations->size() == 0) {
Andreas Huber3599d922016-08-09 10:42:57 -0700242 return;
243 }
244
245 out << "// ";
Steven Morelandd537ab02016-09-12 10:32:01 -0700246 for (size_t i = 0; i < mAnnotations->size(); ++i) {
Andreas Huber3599d922016-08-09 10:42:57 -0700247 if (i > 0) {
248 out << " ";
249 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700250 mAnnotations->at(i)->dump(out);
Andreas Huber3599d922016-08-09 10:42:57 -0700251 }
252 out << "\n";
253}
254
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700255bool Method::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700256 if (!std::all_of(mArgs->begin(), mArgs->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700257 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700258 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700259 }
260
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700261 if (!std::all_of(mResults->begin(), mResults->end(),
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700262 [&](const auto* arg) { return (*arg)->isJavaCompatible(visited); })) {
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700263 return false;
Andreas Huber70a59e12016-08-16 12:57:01 -0700264 }
265
266 return true;
267}
268
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700269const NamedReference<Type>* Method::canElideCallback() const {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700270 // Can't elide callback for void or tuple-returning methods
Steven Moreland9df52442016-12-12 08:51:14 -0800271 if (mResults->size() != 1) {
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700272 return nullptr;
273 }
274
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700275 const NamedReference<Type>* typedVar = mResults->at(0);
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700276
Steven Moreland9df52442016-12-12 08:51:14 -0800277 if (typedVar->type().isElidableType()) {
Martijn Coenen99e6beb2016-12-01 15:48:42 +0100278 return typedVar;
279 }
280
Iliyan Malchev40d474a2016-08-16 06:20:17 -0700281 return nullptr;
282}
283
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700284const Location& Method::location() const {
285 return mLocation;
286}
287
Andreas Huber31629bc2016-08-03 09:06:40 -0700288////////////////////////////////////////////////////////////////////////////////
289
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700290bool TypedVarVector::add(NamedReference<Type>* v) {
Yifan Hong7763ab32016-12-13 17:42:11 -0800291 if (mNames.emplace(v->name()).second) {
292 push_back(v);
293 return true;
294 }
295 return false;
296}
297
Andreas Huberc9410c72016-07-28 12:18:40 -0700298} // namespace android
299