blob: 073e44c0f061e772ead7e05fa48bbc192572152a [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 "Interface.h"
18
Zhuoyao Zhangba7e6e92016-08-10 12:19:02 -070019#include "Annotation.h"
Yifan Hong30b5d1f2017-04-03 12:19:25 -070020#include "ArrayType.h"
21#include "ConstantExpression.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010022#include "DeathRecipientType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070023#include "Method.h"
Martijn Coenen115d4282016-12-19 05:14:04 +010024#include "ScalarType.h"
Yifan Hong10fe0b52016-10-19 14:20:17 -070025#include "StringType.h"
26#include "VectorType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Yifan Hong30b5d1f2017-04-03 12:19:25 -070028#include <unistd.h>
29
Yifan Hong30b5d1f2017-04-03 12:19:25 -070030#include <iostream>
Timur Iskhakov7296af12017-08-09 21:52:48 +000031#include <memory>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070032#include <sstream>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070033#include <unordered_map>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070034
Steven Moreland14ee6742016-10-18 12:58:28 -070035#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070036#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070037#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070038
Andreas Huberc9410c72016-07-28 12:18:40 -070039namespace android {
40
Steven Morelandb8e15a52017-02-13 19:35:40 -080041#define B_PACK_CHARS(c1, c2, c3, c4) \
42 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
43
Yifan Hong10fe0b52016-10-19 14:20:17 -070044/* It is very important that these values NEVER change. These values
45 * must remain unchanged over the lifetime of android. This is
46 * because the framework on a device will be updated independently of
47 * the hals on a device. If the hals are compiled with one set of
48 * transaction values, and the framework with another, then the
49 * interface between them will be destroyed, and the device will not
50 * work.
51 */
52enum {
Yifan Hong10fe0b52016-10-19 14:20:17 -070053 /////////////////// User defined transactions
54 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080055 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070056 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080057 FIRST_HIDL_TRANSACTION = 0x0f000000,
58 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
59 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
60 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
61 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
62 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
63 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
64 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
65 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
66 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
Yifan Hong30b5d1f2017-04-03 12:19:25 -070067 HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'),
Steven Morelandb8e15a52017-02-13 19:35:40 -080068 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070069};
70
Steven Moreland77943692018-08-09 12:53:42 -070071const std::unique_ptr<ConstantExpression> Interface::FLAG_ONE_WAY =
72 std::make_unique<LiteralConstantExpression>(ScalarType::KIND_UINT32, 0x01, "oneway");
73
Timur Iskhakov565b0132017-09-06 18:07:11 -070074Interface::Interface(const char* localName, const FQName& fullName, const Location& location,
Steven Moreland04dea8d2018-02-06 13:11:24 -080075 Scope* parent, const Reference<Type>& superType, const Hash* fileHash)
76 : Scope(localName, fullName, location, parent), mSuperType(superType), mFileHash(fileHash) {}
Martijn Coenenaf712c02016-11-16 15:26:27 +010077
Steven Moreland30bb6a82016-11-30 09:18:34 -080078std::string Interface::typeName() const {
79 return "interface " + localName();
80}
81
Steven Moreland04dea8d2018-02-06 13:11:24 -080082const Hash* Interface::getFileHash() const {
83 return mFileHash;
84}
85
Steven Moreland424a9482017-02-13 19:20:40 -080086bool Interface::fillPingMethod(Method *method) const {
87 if (method->name() != "ping") {
88 return false;
89 }
90
91 method->fillImplementation(
92 HIDL_PING_TRANSACTION,
93 {
Steven Moreland937408a2017-03-20 09:54:18 -070094 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080095 [](auto &out) {
96 out << "return ::android::hardware::Void();\n";
97 }
98 },
99 {IMPL_STUB_IMPL,
100 [](auto &out) {
101 out << "return ::android::hardware::Void();\n";
102 }
103 }
104 }, /*cppImpl*/
105 {
Steven Moreland937408a2017-03-20 09:54:18 -0700106 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700107 [](auto &out) {
Steven Moreland424a9482017-02-13 19:20:40 -0800108 out << "return;\n";
109 }
110 },
Steven Moreland424a9482017-02-13 19:20:40 -0800111 } /*javaImpl*/
112 );
113
114 return true;
115}
116
Yifan Hongffa91392017-01-31 13:41:23 -0800117bool Interface::fillLinkToDeathMethod(Method *method) const {
118 if (method->name() != "linkToDeath") {
119 return false;
120 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100121
Yifan Hongffa91392017-01-31 13:41:23 -0800122 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100123 HIDL_LINK_TO_DEATH_TRANSACTION,
124 {
Steven Moreland937408a2017-03-20 09:54:18 -0700125 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100126 [](auto &out) {
127 out << "(void)cookie;\n"
128 << "return (recipient != nullptr);\n";
129 }
130 },
131 {IMPL_PROXY,
132 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100133 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100134 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
135 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
136 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
137 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
138 << "return (remote()->linkToDeath(binder_recipient)"
139 << " == ::android::OK);\n";
140 }
141 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100142 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100143 }, /*cppImpl*/
144 {
Steven Moreland937408a2017-03-20 09:54:18 -0700145 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700146 [](auto &out) {
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700147 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100148 }
149 },
150 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700151 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100152 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100153 }
154 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100155 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100156 } /*javaImpl*/
157 );
Yifan Hongffa91392017-01-31 13:41:23 -0800158 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100159}
160
Yifan Hongffa91392017-01-31 13:41:23 -0800161bool Interface::fillUnlinkToDeathMethod(Method *method) const {
162 if (method->name() != "unlinkToDeath") {
163 return false;
164 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100165
Yifan Hongffa91392017-01-31 13:41:23 -0800166 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100167 HIDL_UNLINK_TO_DEATH_TRANSACTION,
168 {
Steven Moreland937408a2017-03-20 09:54:18 -0700169 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100170 [](auto &out) {
171 out << "return (recipient != nullptr);\n";
172 }
173 },
174 {IMPL_PROXY,
175 [](auto &out) {
176 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
Steven Morelandba140f42018-07-24 13:13:15 -0700177 << "for (auto it = _hidl_mDeathRecipients.rbegin();"
178 << "it != _hidl_mDeathRecipients.rend();"
Martijn Coenen115d4282016-12-19 05:14:04 +0100179 << "++it) {\n";
180 out.indent([&] {
181 out.sIf("(*it)->getRecipient() == recipient", [&] {
182 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
Steven Morelandba140f42018-07-24 13:13:15 -0700183 << "_hidl_mDeathRecipients.erase(it.base()-1);\n"
Martijn Coenen115d4282016-12-19 05:14:04 +0100184 << "return status == ::android::OK;\n";
185 });
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700186 }).endl();
Martijn Coenen115d4282016-12-19 05:14:04 +0100187 out << "}\n";
188 out << "return false;\n";
189 }
190 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100191 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100192 }, /*cppImpl*/
193 {
Steven Moreland937408a2017-03-20 09:54:18 -0700194 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700195 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100196 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100197 }
198 },
199 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700200 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100201 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100202 }
203 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100204 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100205 } /*javaImpl*/
206 );
Yifan Hongffa91392017-01-31 13:41:23 -0800207 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100208}
Yifan Hongffa91392017-01-31 13:41:23 -0800209bool Interface::fillSyspropsChangedMethod(Method *method) const {
210 if (method->name() != "notifySyspropsChanged") {
211 return false;
212 }
213
214 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100215 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yi Kongc8ff4672017-04-30 23:46:56 -0700216 { { IMPL_INTERFACE, [](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100217 out << "::android::report_sysprop_change();\n";
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700218 out << "return ::android::hardware::Void();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100219 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700220 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700221 out << "android.os.HwBinder.enableInstrumentation();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100222 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100223 );
Yifan Hongffa91392017-01-31 13:41:23 -0800224 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700225}
226
Yifan Hongffa91392017-01-31 13:41:23 -0800227bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
228 if (method->name() != "setHALInstrumentation") {
229 return false;
230 }
231
232 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800233 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
234 {
Steven Moreland937408a2017-03-20 09:54:18 -0700235 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700236 [](auto &out) {
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800237 // do nothing for base class.
238 out << "return ::android::hardware::Void();\n";
239 }
240 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800241 {IMPL_STUB,
242 [](auto &out) {
243 out << "configureInstrumentation();\n";
244 }
245 },
246 {IMPL_PASSTHROUGH,
247 [](auto &out) {
248 out << "configureInstrumentation();\n";
249 out << "return ::android::hardware::Void();\n";
250 }
251 },
252 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700253 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800254 // Not support for Java Impl for now.
255 } } } /*javaImpl */
256 );
Yifan Hongffa91392017-01-31 13:41:23 -0800257 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800258}
259
Yifan Hongffa91392017-01-31 13:41:23 -0800260bool Interface::fillDescriptorChainMethod(Method *method) const {
261 if (method->name() != "interfaceChain") {
262 return false;
263 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700264
Yifan Hongffa91392017-01-31 13:41:23 -0800265 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700266 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700267 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700268 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800269 out << "_hidl_cb(";
270 out.block([&] {
271 for (const Interface *iface : chain) {
272 out << iface->fullName() << "::descriptor,\n";
273 }
274 });
275 out << ");\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700276 out << "return ::android::hardware::Void();";
Martijn Coenen115d4282016-12-19 05:14:04 +0100277 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700278 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700279 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800280 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700281 out.indent(); out.indent();
282 for (size_t i = 0; i < chain.size(); ++i) {
283 if (i != 0)
284 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800285 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700286 }
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700287 out << "));\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700288 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800289 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100290 );
Yifan Hongffa91392017-01-31 13:41:23 -0800291 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700292}
293
Steven Moreland04dea8d2018-02-06 13:11:24 -0800294void Interface::emitDigestChain(
Timur Iskhakov7296af12017-08-09 21:52:48 +0000295 Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
Steven Moreland04dea8d2018-02-06 13:11:24 -0800296 std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const {
297 out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700298 out << prefix;
299 out << "{";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800300 out.join(
301 iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",",
302 [&](const auto& e) {
303 // Use ConstantExpression::cppValue / javaValue
304 // because Java used signed byte for uint8_t.
305 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
306 });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700307 out << "} /* ";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800308 out << iface->getFileHash()->hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700309 out << " */";
310 });
311}
312
313bool Interface::fillHashChainMethod(Method *method) const {
314 if (method->name() != "getHashChain") {
315 return false;
316 }
317 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
318 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
319
320 method->fillImplementation(
321 HIDL_HASH_CHAIN_TRANSACTION,
322 { { IMPL_INTERFACE, [this, digestType](auto &out) {
323 std::vector<const Interface *> chain = typeChain();
324 out << "_hidl_cb(";
325 out.block([&] {
Timur Iskhakov7296af12017-08-09 21:52:48 +0000326 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain,
327 [](const auto& e) { return e->cppValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700328 });
329 out << ");\n";
330 out << "return ::android::hardware::Void();\n";
331 } } }, /* cppImpl */
332 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
333 std::vector<const Interface *> chain = typeChain();
334 out << "return new "
335 << chainType->getJavaType(false /* forInitializer */)
336 << "(java.util.Arrays.asList(\n";
337 out.indent(2, [&] {
338 // No need for dimensions when elements are explicitly provided.
339 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
Timur Iskhakov7296af12017-08-09 21:52:48 +0000340 chain, [](const auto& e) { return e->javaValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700341 });
342 out << "));\n";
343 } } } /* javaImpl */
344 );
345 return true;
346}
347
Yifan Hongffa91392017-01-31 13:41:23 -0800348bool Interface::fillGetDescriptorMethod(Method *method) const {
349 if (method->name() != "interfaceDescriptor") {
350 return false;
351 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800352
Yifan Hongffa91392017-01-31 13:41:23 -0800353 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800354 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700355 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800356 out << "_hidl_cb("
357 << fullName()
358 << "::descriptor);\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700359 << "return ::android::hardware::Void();\n";
Yifan Hongc75fd472017-01-11 12:37:31 -0800360 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700361 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800362 out << "return "
363 << fullJavaName()
364 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800365 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800366 );
Yifan Hongffa91392017-01-31 13:41:23 -0800367 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800368}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700369
Yifan Hongbcffce22017-02-01 15:52:06 -0800370bool Interface::fillGetDebugInfoMethod(Method *method) const {
371 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800372 return false;
373 }
374
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800375 static const std::string sArch =
376 "#if defined(__LP64__)\n"
377 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
378 "#else\n"
379 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
380 "#endif\n";
381
Yifan Hongcd2ae452017-01-31 14:33:40 -0800382 method->fillImplementation(
383 HIDL_GET_REF_INFO_TRANSACTION,
384 {
Steven Moreland937408a2017-03-20 09:54:18 -0700385 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700386 [](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800387 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800388 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
389 << sArch
390 << "});\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700391 << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800392 }
393 },
394 {IMPL_STUB_IMPL,
Yi Kongc8ff4672017-04-30 23:46:56 -0700395 [](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800396 out << "_hidl_cb(";
397 out.block([&] {
Yifan Hong96bb0632017-11-14 16:08:37 -0800398 out << "::android::hardware::details::getPidIfSharable(),\n"
Yifan Hong2e036c92017-03-07 13:18:12 -0800399 << "::android::hardware::details::debuggable()"
400 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
401 << sArch << "\n";
402 });
403 out << ");\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700404 << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800405 }
406 }
407 }, /* cppImpl */
Yi Kongc8ff4672017-04-30 23:46:56 -0700408 { { IMPL_INTERFACE, [method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800409 const Type &refInfo = method->results().front()->type();
410 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
411 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800412 << "info.pid = android.os.HidlSupport.getPidIfSharable();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800413 << "info.ptr = 0;\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800414 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700415 << "return info;\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800416 } } } /* javaImpl */
417 );
418
419 return true;
420}
421
Andreas Huber37065d62017-02-07 14:36:54 -0800422bool Interface::fillDebugMethod(Method *method) const {
423 if (method->name() != "debug") {
424 return false;
425 }
426
Steven Moreland327fd8b2018-08-10 15:40:41 -0700427 method->fillImplementation(HIDL_DEBUG_TRANSACTION,
428 {
429 {IMPL_INTERFACE,
430 [](auto& out) {
431 out << "(void)fd;\n"
432 << "(void)options;\n"
433 << "return ::android::hardware::Void();\n";
434 }},
435 }, /* cppImpl */
436 {
437 {IMPL_INTERFACE, [](auto& out) { out << "return;\n"; }},
438 } /* javaImpl */
Andreas Huber37065d62017-02-07 14:36:54 -0800439 );
440
441 return true;
442}
443
Yifan Hongffa91392017-01-31 13:41:23 -0800444static std::map<std::string, Method *> gAllReservedMethods;
445
Steven Moreland14ee6742016-10-18 12:58:28 -0700446bool Interface::addMethod(Method *method) {
Yifan Hongc8934042016-11-17 17:10:52 -0800447 if (isIBase()) {
Yifan Hongffa91392017-01-31 13:41:23 -0800448 if (!gAllReservedMethods.emplace(method->name(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700449 std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name()
450 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800451 return false;
452 }
453 // will add it in addAllReservedMethods
Yifan Hongc8934042016-11-17 17:10:52 -0800454 return true;
455 }
456
Yifan Hong10fe0b52016-10-19 14:20:17 -0700457 CHECK(!method->isHidlReserved());
Yifan Hong10fe0b52016-10-19 14:20:17 -0700458 mUserMethods.push_back(method);
Steven Moreland14ee6742016-10-18 12:58:28 -0700459
460 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700461}
462
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700463std::vector<const Reference<Type>*> Interface::getReferences() const {
464 std::vector<const Reference<Type>*> ret;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700465
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700466 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700467 ret.push_back(&mSuperType);
Timur Iskhakov33431e62017-08-21 17:31:23 -0700468 }
469
470 for (const auto* method : methods()) {
471 const auto& references = method->getReferences();
472 ret.insert(ret.end(), references.begin(), references.end());
473 }
474
475 return ret;
476}
477
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700478std::vector<const ConstantExpression*> Interface::getConstantExpressions() const {
479 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700480 for (const auto* method : methods()) {
481 const auto& retMethod = method->getConstantExpressions();
482 ret.insert(ret.end(), retMethod.begin(), retMethod.end());
483 }
484 return ret;
485}
486
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700487std::vector<const Reference<Type>*> Interface::getStrongReferences() const {
Timur Iskhakov40731af2017-08-24 14:18:35 -0700488 // Interface is a special case as a reference:
489 // its definiton must be completed for extension but
490 // not necessary for other references.
Timur Iskhakov40731af2017-08-24 14:18:35 -0700491
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700492 std::vector<const Reference<Type>*> ret;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700493 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700494 ret.push_back(&mSuperType);
Timur Iskhakov40731af2017-08-24 14:18:35 -0700495 }
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700496
497 for (const auto* method : methods()) {
498 const auto& references = method->getStrongReferences();
499 ret.insert(ret.end(), references.begin(), references.end());
500 }
501
Timur Iskhakov40731af2017-08-24 14:18:35 -0700502 return ret;
503}
504
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700505status_t Interface::resolveInheritance() {
506 size_t serial = FIRST_CALL_TRANSACTION;
507 for (const auto* ancestor : superTypeChain()) {
508 serial += ancestor->mUserMethods.size();
509 }
510
511 for (Method* method : mUserMethods) {
512 if (serial > LAST_CALL_TRANSACTION) {
513 std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION
514 << " methods (including super and reserved) are not allowed at " << location()
Steven Morelandcbff5612017-10-11 17:01:54 -0700515 << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700516 return UNKNOWN_ERROR;
517 }
518
519 method->setSerialId(serial);
520 serial++;
521 }
522
523 return Scope::resolveInheritance();
524}
525
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700526status_t Interface::validate() const {
527 CHECK(isIBase() == mSuperType.isEmptyReference());
528
Timur Iskhakov0344e612017-08-25 12:57:09 -0700529 if (!isIBase() && !mSuperType->isInterface()) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700530 std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location()
531 << std::endl;
Timur Iskhakov0344e612017-08-25 12:57:09 -0700532 return UNKNOWN_ERROR;
533 }
534
Steven Moreland368e4602018-02-16 14:21:49 -0800535 status_t err;
536
537 err = validateUniqueNames();
538 if (err != OK) return err;
539
540 err = validateAnnotations();
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700541 if (err != OK) return err;
542
543 return Scope::validate();
544}
545
Howard Chenecfb4512017-11-21 18:28:53 +0800546void Interface::getAlignmentAndSize(size_t* align, size_t* size) const {
547 *align = 8;
548 *size = 8;
549}
550
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700551status_t Interface::validateUniqueNames() const {
552 std::unordered_map<std::string, const Interface*> registeredMethodNames;
553 for (auto const& tuple : allSuperMethodsFromRoot()) {
554 // No need to check super method uniqueness
555 registeredMethodNames[tuple.method()->name()] = tuple.interface();
556 }
557
558 for (const Method* method : mUserMethods) {
559 auto registered = registeredMethodNames.find(method->name());
560
561 if (registered != registeredMethodNames.end()) {
562 const Interface* definedInType = registered->second;
563
564 if (definedInType == this) {
565 // Defined in this interface
566 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
567 } else if (definedInType->isIBase()) {
568 // Defined in IBase
569 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
570 } else {
571 // Defined in super not IBase
572 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700573 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700574 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700575 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700576 return UNKNOWN_ERROR;
577 }
578
579 registeredMethodNames[method->name()] = this;
580 }
581
582 return OK;
583}
584
Steven Moreland368e4602018-02-16 14:21:49 -0800585status_t Interface::validateAnnotations() const {
586 for (const Method* method : methods()) {
587 for (const Annotation* annotation : method->annotations()) {
588 const std::string name = annotation->name();
589
590 if (name == "entry" || name == "exit" || name == "callflow") {
591 continue;
592 }
593
594 std::cerr << "ERROR: Unrecognized annotation '" << name
595 << "' for method: " << method->name() << ". An annotation should be one of: "
596 << "entry, exit, callflow." << std::endl;
597 return UNKNOWN_ERROR;
598 }
599 }
600 return OK;
601}
602
Yifan Hongffa91392017-01-31 13:41:23 -0800603bool Interface::addAllReservedMethods() {
604 // use a sorted map to insert them in serial ID order.
605 std::map<int32_t, Method *> reservedMethodsById;
606 for (const auto &pair : gAllReservedMethods) {
607 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800608 bool fillSuccess = fillPingMethod(method)
609 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800610 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700611 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800612 || fillSyspropsChangedMethod(method)
613 || fillLinkToDeathMethod(method)
614 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800615 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800616 || fillGetDebugInfoMethod(method)
617 || fillDebugMethod(method);
618
Yifan Hongffa91392017-01-31 13:41:23 -0800619 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700620 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
621 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800622 return false;
623 }
624 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700625 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
626 << " and " << reservedMethodsById[method->getSerialId()]->name()
627 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800628 return false;
629 }
630 }
631 for (const auto &pair : reservedMethodsById) {
632 this->mReservedMethods.push_back(pair.second);
633 }
634 return true;
635}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700636
Timur Iskhakov505316c2017-08-05 03:38:59 +0000637const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700638 if (isIBase()) return nullptr;
639 if (!mSuperType->isInterface()) {
640 // This is actually an error
641 // that would be caught in validate
642 return nullptr;
643 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700644 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700645}
646
Yifan Hong10fe0b52016-10-19 14:20:17 -0700647std::vector<const Interface *> Interface::typeChain() const {
648 std::vector<const Interface *> v;
649 const Interface *iface = this;
650 while (iface != nullptr) {
651 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000652 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700653 }
654 return v;
655}
656
Yifan Hongfe95aa22016-10-19 17:26:45 -0700657std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000658 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700659}
660
Martijn Coenenb40ef022017-01-02 15:21:46 +0100661bool Interface::isElidableType() const {
662 return true;
663}
664
Andreas Hubera2723d22016-07-29 15:36:07 -0700665bool Interface::isInterface() const {
666 return true;
667}
668
Yifan Hong10fe0b52016-10-19 14:20:17 -0700669const std::vector<Method *> &Interface::userDefinedMethods() const {
670 return mUserMethods;
671}
672
673const std::vector<Method *> &Interface::hidlReservedMethods() const {
674 return mReservedMethods;
675}
676
677std::vector<Method *> Interface::methods() const {
678 std::vector<Method *> v(mUserMethods);
679 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
680 return v;
681}
682
683std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
684 std::vector<InterfaceAndMethod> v;
685 std::vector<const Interface *> chain = typeChain();
686 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
687 const Interface *iface = *it;
688 for (Method *userMethod : iface->userDefinedMethods()) {
689 v.push_back(InterfaceAndMethod(iface, userMethod));
690 }
691 }
692 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800693 v.push_back(InterfaceAndMethod(
694 *chain.rbegin(), // IBase
695 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700696 }
697 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700698}
699
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700700std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
701 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
702}
703
Steven Moreland40786312016-08-16 10:29:40 -0700704std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700705 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700706}
707
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700708std::string Interface::getAdapterName() const {
709 return fqName().getInterfaceAdapterName();
710}
711
Yifan Hongeefe4f22017-01-04 15:32:42 -0800712std::string Interface::getProxyName() const {
713 return fqName().getInterfaceProxyName();
714}
715
716std::string Interface::getStubName() const {
717 return fqName().getInterfaceStubName();
718}
719
720std::string Interface::getHwName() const {
721 return fqName().getInterfaceHwName();
722}
723
724std::string Interface::getPassthroughName() const {
725 return fqName().getInterfacePassthroughName();
726}
727
Yifan Hong51a65092017-01-04 15:41:44 -0800728FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800729 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800730}
731
Yifan Hong51a65092017-01-04 15:41:44 -0800732FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800733 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800734}
735
Yifan Hong51a65092017-01-04 15:41:44 -0800736FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800737 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800738}
739
Steven Moreland979e0992016-09-07 09:18:08 -0700740std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700741 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700742 const std::string base =
743 std::string(specifyNamespaces ? "::android::" : "")
744 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700745 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700746 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700747
748 switch (mode) {
749 case StorageMode_Stack:
750 case StorageMode_Result:
751 return base;
752
753 case StorageMode_Argument:
754 return "const " + base + "&";
755 }
756}
757
Yifan Hong4ed13472016-11-02 10:44:11 -0700758std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700759 return fullJavaName();
760}
761
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800762std::string Interface::getVtsType() const {
763 if (StringHelper::EndsWith(localName(), "Callback")) {
764 return "TYPE_HIDL_CALLBACK";
765 } else {
766 return "TYPE_HIDL_INTERFACE";
767 }
768}
769
Andreas Huber881227d2016-08-02 14:20:21 -0700770void Interface::emitReaderWriter(
771 Formatter &out,
772 const std::string &name,
773 const std::string &parcelObj,
774 bool parcelObjIsPointer,
775 bool isReader,
776 ErrorMode mode) const {
777 const std::string parcelObjDeref =
778 parcelObj + (parcelObjIsPointer ? "->" : ".");
779
780 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700781 out << "{\n";
782 out.indent();
783
Howard Chenecfb4512017-11-21 18:28:53 +0800784 const std::string binderName = "_hidl_binder";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700785 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700786 << binderName << ";\n";
787
Iliyan Malchev549e2592016-08-10 08:59:12 -0700788 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700789 out << parcelObjDeref
790 << "readNullableStrongBinder(&"
791 << binderName
792 << ");\n";
793
794 handleError(out, mode);
795
796 out << name
797 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100798 << "::android::hardware::fromBinder<"
799 << fqName().cppName()
800 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800801 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100802 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800803 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100804 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700805 << binderName
806 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700807
808 out.unindent();
809 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700810 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200811 out << "if (" << name << " == nullptr) {\n";
812 out.indent();
813 out << "_hidl_err = ";
814 out << parcelObjDeref
815 << "writeStrongBinder(nullptr);\n";
816 out.unindent();
817 out << "} else {\n";
818 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800819 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
Steven Moreland731dbb12018-12-03 18:09:46 -0800820 << "::android::hardware::getOrCreateCachedBinder(" << name << ".get());\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800821 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800822 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800823 out << "_hidl_err = "
824 << parcelObjDeref
825 << "writeStrongBinder(_hidl_binder);\n";
826 });
827 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800828 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800829 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
830 });
831 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200832 out.unindent();
833 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700834
Andreas Huber881227d2016-08-02 14:20:21 -0700835 handleError(out, mode);
836 }
837}
838
Steven Moreland368e4602018-02-16 14:21:49 -0800839void Interface::emitPackageTypeDeclarations(Formatter& out) const {
840 Scope::emitPackageTypeDeclarations(out);
Steven Morelandbf714212017-10-27 18:29:01 -0700841
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700842 out << "static inline std::string toString(" << getCppArgumentType() << " o);\n\n";
843}
844
845void Interface::emitPackageTypeHeaderDefinitions(Formatter& out) const {
846 Scope::emitPackageTypeHeaderDefinitions(out);
847
Steven Morelandbf714212017-10-27 18:29:01 -0700848 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
849
850 out.block([&] {
851 out << "std::string os = \"[class or subclass of \";\n"
852 << "os += " << fullName() << "::descriptor;\n"
853 << "os += \"]\";\n"
854 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
855 << "return os;\n";
856 }).endl().endl();
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800857}
858
Steven Moreland368e4602018-02-16 14:21:49 -0800859void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800860 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Morelandd8c7a292017-10-27 18:32:06 -0700861
Steven Moreland368e4602018-02-16 14:21:49 -0800862 Scope::emitTypeDefinitions(out, space + localName());
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800863}
864
Andreas Huber2831d512016-08-15 09:33:47 -0700865void Interface::emitJavaReaderWriter(
866 Formatter &out,
867 const std::string &parcelObj,
868 const std::string &argName,
869 bool isReader) const {
870 if (isReader) {
871 out << fullJavaName()
872 << ".asInterface("
873 << parcelObj
874 << ".readStrongBinder());\n";
875 } else {
876 out << parcelObj
877 << ".writeStrongBinder("
878 << argName
879 << " == null ? null : "
880 << argName
881 << ".asBinder());\n";
882 }
883}
884
Steven Moreland368e4602018-02-16 14:21:49 -0800885void Interface::emitVtsAttributeDeclaration(Formatter& out) const {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700886 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700887 // Skip for TypeDef as it is just an alias of a defined type.
888 if (type->isTypeDef()) {
889 continue;
890 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700891 out << "attribute: {\n";
892 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800893 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700894 out.unindent();
895 out << "}\n\n";
896 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700897}
898
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700899void Interface::emitVtsMethodDeclaration(Formatter& out, bool isInherited) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700900 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800901 if (method->isHidlReserved()) {
902 continue;
903 }
904
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700905 out << "api: {\n";
906 out.indent();
907 out << "name: \"" << method->name() << "\"\n";
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700908 out << "is_inherited: " << (isInherited ? "true" : "false") << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700909 // Generate declaration for each return value.
910 for (const auto &result : method->results()) {
911 out << "return_type_hidl: {\n";
912 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700913 out << "name: \"" << result->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800914 result->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700915 out.unindent();
916 out << "}\n";
917 }
918 // Generate declaration for each input argument
919 for (const auto &arg : method->args()) {
920 out << "arg: {\n";
921 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700922 out << "name: \"" << arg->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800923 arg->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700924 out.unindent();
925 out << "}\n";
926 }
927 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700928 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700929 out << "callflow: {\n";
930 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800931 const std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700932 if (name == "entry") {
933 out << "entry: true\n";
934 } else if (name == "exit") {
935 out << "exit: true\n";
936 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700937 const AnnotationParam *param =
938 annotation->getParam("next");
939 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700940 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700941 out << "next: " << value << "\n";
942 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700943 }
944 } else {
Steven Moreland368e4602018-02-16 14:21:49 -0800945 CHECK(false);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700946 }
947 out.unindent();
948 out << "}\n";
949 }
950 out.unindent();
951 out << "}\n\n";
952 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700953}
954
Steven Moreland368e4602018-02-16 14:21:49 -0800955void Interface::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800956 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700957 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800958 << fullName()
959 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700960}
961
Steven Moreland69e7c702016-09-09 11:16:32 -0700962bool Interface::hasOnewayMethods() const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700963 for (auto const &method : methods()) {
Steven Moreland69e7c702016-09-09 11:16:32 -0700964 if (method->isOneway()) {
965 return true;
966 }
967 }
968
969 const Interface* superClass = superType();
970
971 if (superClass != nullptr) {
972 return superClass->hasOnewayMethods();
973 }
974
975 return false;
976}
977
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700978bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
979 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700980 return false;
981 }
982
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700983 for (const auto* method : methods()) {
984 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700985 return false;
986 }
987 }
988
Steven Morelandb0da6fc2018-10-16 17:15:11 -0700989 return Scope::deepIsJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -0700990}
991
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700992bool Interface::isNeverStrongReference() const {
993 return true;
994}
995
Andreas Huberc9410c72016-07-28 12:18:40 -0700996} // namespace android
997