blob: ff33972d331133fc728ef24d7a020870f62f3726 [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";
Steven Moreland3768f422019-05-09 12:29:12 -0700276 out << "return ::android::hardware::Void();\n";
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
Neel Mehta0ee353f2019-05-30 17:40:29 -0700444void Interface::addUserDefinedMethod(Method* method) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700445 CHECK(!method->isHidlReserved());
Yifan Hong10fe0b52016-10-19 14:20:17 -0700446 mUserMethods.push_back(method);
Andreas Huberc9410c72016-07-28 12:18:40 -0700447}
448
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700449std::vector<const Reference<Type>*> Interface::getReferences() const {
450 std::vector<const Reference<Type>*> ret;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700451
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700452 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700453 ret.push_back(&mSuperType);
Timur Iskhakov33431e62017-08-21 17:31:23 -0700454 }
455
456 for (const auto* method : methods()) {
457 const auto& references = method->getReferences();
458 ret.insert(ret.end(), references.begin(), references.end());
459 }
460
461 return ret;
462}
463
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700464std::vector<const ConstantExpression*> Interface::getConstantExpressions() const {
465 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700466 for (const auto* method : methods()) {
467 const auto& retMethod = method->getConstantExpressions();
468 ret.insert(ret.end(), retMethod.begin(), retMethod.end());
469 }
470 return ret;
471}
472
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700473std::vector<const Reference<Type>*> Interface::getStrongReferences() const {
Timur Iskhakov40731af2017-08-24 14:18:35 -0700474 // Interface is a special case as a reference:
475 // its definiton must be completed for extension but
476 // not necessary for other references.
Timur Iskhakov40731af2017-08-24 14:18:35 -0700477
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700478 std::vector<const Reference<Type>*> ret;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700479 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700480 ret.push_back(&mSuperType);
Timur Iskhakov40731af2017-08-24 14:18:35 -0700481 }
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700482
483 for (const auto* method : methods()) {
484 const auto& references = method->getStrongReferences();
485 ret.insert(ret.end(), references.begin(), references.end());
486 }
487
Timur Iskhakov40731af2017-08-24 14:18:35 -0700488 return ret;
489}
490
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700491status_t Interface::resolveInheritance() {
492 size_t serial = FIRST_CALL_TRANSACTION;
493 for (const auto* ancestor : superTypeChain()) {
494 serial += ancestor->mUserMethods.size();
495 }
496
497 for (Method* method : mUserMethods) {
498 if (serial > LAST_CALL_TRANSACTION) {
499 std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION
500 << " methods (including super and reserved) are not allowed at " << location()
Steven Morelandcbff5612017-10-11 17:01:54 -0700501 << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700502 return UNKNOWN_ERROR;
503 }
504
505 method->setSerialId(serial);
506 serial++;
507 }
508
509 return Scope::resolveInheritance();
510}
511
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700512status_t Interface::validate() const {
513 CHECK(isIBase() == mSuperType.isEmptyReference());
514
Timur Iskhakov0344e612017-08-25 12:57:09 -0700515 if (!isIBase() && !mSuperType->isInterface()) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700516 std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location()
517 << std::endl;
Timur Iskhakov0344e612017-08-25 12:57:09 -0700518 return UNKNOWN_ERROR;
519 }
520
Steven Moreland368e4602018-02-16 14:21:49 -0800521 status_t err;
522
523 err = validateUniqueNames();
524 if (err != OK) return err;
525
526 err = validateAnnotations();
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700527 if (err != OK) return err;
528
529 return Scope::validate();
530}
531
Howard Chenecfb4512017-11-21 18:28:53 +0800532void Interface::getAlignmentAndSize(size_t* align, size_t* size) const {
533 *align = 8;
534 *size = 8;
535}
536
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700537status_t Interface::validateUniqueNames() const {
538 std::unordered_map<std::string, const Interface*> registeredMethodNames;
539 for (auto const& tuple : allSuperMethodsFromRoot()) {
540 // No need to check super method uniqueness
541 registeredMethodNames[tuple.method()->name()] = tuple.interface();
542 }
543
544 for (const Method* method : mUserMethods) {
545 auto registered = registeredMethodNames.find(method->name());
546
547 if (registered != registeredMethodNames.end()) {
548 const Interface* definedInType = registered->second;
549
550 if (definedInType == this) {
551 // Defined in this interface
552 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
553 } else if (definedInType->isIBase()) {
554 // Defined in IBase
555 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
556 } else {
557 // Defined in super not IBase
558 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700559 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700560 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700561 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700562 return UNKNOWN_ERROR;
563 }
564
565 registeredMethodNames[method->name()] = this;
566 }
567
568 return OK;
569}
570
Steven Moreland368e4602018-02-16 14:21:49 -0800571status_t Interface::validateAnnotations() const {
572 for (const Method* method : methods()) {
573 for (const Annotation* annotation : method->annotations()) {
574 const std::string name = annotation->name();
575
576 if (name == "entry" || name == "exit" || name == "callflow") {
577 continue;
578 }
579
580 std::cerr << "ERROR: Unrecognized annotation '" << name
581 << "' for method: " << method->name() << ". An annotation should be one of: "
582 << "entry, exit, callflow." << std::endl;
583 return UNKNOWN_ERROR;
584 }
585 }
586 return OK;
587}
588
Neel Mehta0ee353f2019-05-30 17:40:29 -0700589bool Interface::addAllReservedMethods(const std::map<std::string, Method*>& allReservedMethods) {
Yifan Hongffa91392017-01-31 13:41:23 -0800590 // use a sorted map to insert them in serial ID order.
591 std::map<int32_t, Method *> reservedMethodsById;
Neel Mehta0ee353f2019-05-30 17:40:29 -0700592 for (const auto& pair : allReservedMethods) {
Yifan Hongffa91392017-01-31 13:41:23 -0800593 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800594 bool fillSuccess = fillPingMethod(method)
595 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800596 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700597 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800598 || fillSyspropsChangedMethod(method)
599 || fillLinkToDeathMethod(method)
600 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800601 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800602 || fillGetDebugInfoMethod(method)
603 || fillDebugMethod(method);
604
Yifan Hongffa91392017-01-31 13:41:23 -0800605 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700606 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
607 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800608 return false;
609 }
610 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700611 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
612 << " and " << reservedMethodsById[method->getSerialId()]->name()
613 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800614 return false;
615 }
616 }
617 for (const auto &pair : reservedMethodsById) {
618 this->mReservedMethods.push_back(pair.second);
619 }
620 return true;
621}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700622
Timur Iskhakov505316c2017-08-05 03:38:59 +0000623const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700624 if (isIBase()) return nullptr;
625 if (!mSuperType->isInterface()) {
626 // This is actually an error
627 // that would be caught in validate
628 return nullptr;
629 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700630 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700631}
632
Yifan Hong10fe0b52016-10-19 14:20:17 -0700633std::vector<const Interface *> Interface::typeChain() const {
634 std::vector<const Interface *> v;
635 const Interface *iface = this;
636 while (iface != nullptr) {
637 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000638 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700639 }
640 return v;
641}
642
Yifan Hongfe95aa22016-10-19 17:26:45 -0700643std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000644 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700645}
646
Martijn Coenenb40ef022017-01-02 15:21:46 +0100647bool Interface::isElidableType() const {
648 return true;
649}
650
Andreas Hubera2723d22016-07-29 15:36:07 -0700651bool Interface::isInterface() const {
652 return true;
653}
654
Yifan Hong10fe0b52016-10-19 14:20:17 -0700655const std::vector<Method *> &Interface::userDefinedMethods() const {
656 return mUserMethods;
657}
658
659const std::vector<Method *> &Interface::hidlReservedMethods() const {
660 return mReservedMethods;
661}
662
663std::vector<Method *> Interface::methods() const {
664 std::vector<Method *> v(mUserMethods);
665 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
666 return v;
667}
668
669std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
670 std::vector<InterfaceAndMethod> v;
671 std::vector<const Interface *> chain = typeChain();
672 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
673 const Interface *iface = *it;
674 for (Method *userMethod : iface->userDefinedMethods()) {
675 v.push_back(InterfaceAndMethod(iface, userMethod));
676 }
677 }
678 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800679 v.push_back(InterfaceAndMethod(
680 *chain.rbegin(), // IBase
681 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700682 }
683 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700684}
685
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700686std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
687 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
688}
689
Steven Moreland40786312016-08-16 10:29:40 -0700690std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700691 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700692}
693
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700694std::string Interface::getAdapterName() const {
695 return fqName().getInterfaceAdapterName();
696}
697
Yifan Hongeefe4f22017-01-04 15:32:42 -0800698std::string Interface::getProxyName() const {
699 return fqName().getInterfaceProxyName();
700}
701
702std::string Interface::getStubName() const {
703 return fqName().getInterfaceStubName();
704}
705
706std::string Interface::getHwName() const {
707 return fqName().getInterfaceHwName();
708}
709
710std::string Interface::getPassthroughName() const {
711 return fqName().getInterfacePassthroughName();
712}
713
Yifan Hong51a65092017-01-04 15:41:44 -0800714FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800715 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800716}
717
Yifan Hong51a65092017-01-04 15:41:44 -0800718FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800719 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800720}
721
Yifan Hong51a65092017-01-04 15:41:44 -0800722FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800723 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800724}
725
Steven Moreland979e0992016-09-07 09:18:08 -0700726std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700727 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700728 const std::string base =
729 std::string(specifyNamespaces ? "::android::" : "")
730 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700731 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700732 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700733
734 switch (mode) {
735 case StorageMode_Stack:
736 case StorageMode_Result:
737 return base;
738
739 case StorageMode_Argument:
740 return "const " + base + "&";
741 }
742}
743
Yifan Hong4ed13472016-11-02 10:44:11 -0700744std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700745 return fullJavaName();
746}
747
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800748std::string Interface::getVtsType() const {
749 if (StringHelper::EndsWith(localName(), "Callback")) {
750 return "TYPE_HIDL_CALLBACK";
751 } else {
752 return "TYPE_HIDL_INTERFACE";
753 }
754}
755
Andreas Huber881227d2016-08-02 14:20:21 -0700756void Interface::emitReaderWriter(
757 Formatter &out,
758 const std::string &name,
759 const std::string &parcelObj,
760 bool parcelObjIsPointer,
761 bool isReader,
762 ErrorMode mode) const {
763 const std::string parcelObjDeref =
764 parcelObj + (parcelObjIsPointer ? "->" : ".");
765
766 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700767 out << "{\n";
768 out.indent();
769
Howard Chenecfb4512017-11-21 18:28:53 +0800770 const std::string binderName = "_hidl_binder";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700771 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700772 << binderName << ";\n";
773
Iliyan Malchev549e2592016-08-10 08:59:12 -0700774 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700775 out << parcelObjDeref
776 << "readNullableStrongBinder(&"
777 << binderName
778 << ");\n";
779
780 handleError(out, mode);
781
782 out << name
783 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100784 << "::android::hardware::fromBinder<"
785 << fqName().cppName()
786 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800787 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100788 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800789 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100790 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700791 << binderName
792 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700793
794 out.unindent();
795 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700796 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200797 out << "if (" << name << " == nullptr) {\n";
798 out.indent();
799 out << "_hidl_err = ";
800 out << parcelObjDeref
801 << "writeStrongBinder(nullptr);\n";
802 out.unindent();
803 out << "} else {\n";
804 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800805 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
Steven Moreland731dbb12018-12-03 18:09:46 -0800806 << "::android::hardware::getOrCreateCachedBinder(" << name << ".get());\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800807 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800808 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800809 out << "_hidl_err = "
810 << parcelObjDeref
811 << "writeStrongBinder(_hidl_binder);\n";
812 });
813 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800814 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800815 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
816 });
817 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200818 out.unindent();
819 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700820
Andreas Huber881227d2016-08-02 14:20:21 -0700821 handleError(out, mode);
822 }
823}
824
Neel Mehta3b414a82019-07-02 15:47:48 -0700825void Interface::emitHidlDefinition(Formatter& out) const {
826 if (getDocComment() != nullptr) getDocComment()->emit(out);
827 out << typeName() << " ";
828
829 const Interface* super = superType();
830 if (super != nullptr && !super->isIBase()) {
831 out << "extends " << super->fqName().getRelativeFQName(fqName()) << " ";
832 }
833
834 out << "{\n";
835
836 out.indent([&] {
837 const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
838 out.join(definedTypes.begin(), definedTypes.end(), "\n",
839 [&](auto t) { t->emitHidlDefinition(out); });
840
841 if (definedTypes.size() > 0 && userDefinedMethods().size() > 0) out << "\n";
842
843 out.join(userDefinedMethods().begin(), userDefinedMethods().end(), "\n",
844 [&](auto method) { method->emitHidlDefinition(out); });
845 });
846
847 out << "};\n";
848}
849
Steven Moreland368e4602018-02-16 14:21:49 -0800850void Interface::emitPackageTypeDeclarations(Formatter& out) const {
851 Scope::emitPackageTypeDeclarations(out);
Steven Morelandbf714212017-10-27 18:29:01 -0700852
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700853 out << "static inline std::string toString(" << getCppArgumentType() << " o);\n\n";
854}
855
856void Interface::emitPackageTypeHeaderDefinitions(Formatter& out) const {
857 Scope::emitPackageTypeHeaderDefinitions(out);
858
Steven Morelandbf714212017-10-27 18:29:01 -0700859 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
860
861 out.block([&] {
862 out << "std::string os = \"[class or subclass of \";\n"
863 << "os += " << fullName() << "::descriptor;\n"
864 << "os += \"]\";\n"
865 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
866 << "return os;\n";
867 }).endl().endl();
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800868}
869
Steven Moreland368e4602018-02-16 14:21:49 -0800870void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800871 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Morelandd8c7a292017-10-27 18:32:06 -0700872
Steven Moreland368e4602018-02-16 14:21:49 -0800873 Scope::emitTypeDefinitions(out, space + localName());
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800874}
875
Andreas Huber2831d512016-08-15 09:33:47 -0700876void Interface::emitJavaReaderWriter(
877 Formatter &out,
878 const std::string &parcelObj,
879 const std::string &argName,
880 bool isReader) const {
881 if (isReader) {
882 out << fullJavaName()
883 << ".asInterface("
884 << parcelObj
885 << ".readStrongBinder());\n";
886 } else {
887 out << parcelObj
888 << ".writeStrongBinder("
889 << argName
890 << " == null ? null : "
891 << argName
892 << ".asBinder());\n";
893 }
894}
895
Steven Moreland368e4602018-02-16 14:21:49 -0800896void Interface::emitVtsAttributeDeclaration(Formatter& out) const {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700897 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700898 // Skip for TypeDef as it is just an alias of a defined type.
899 if (type->isTypeDef()) {
900 continue;
901 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700902 out << "attribute: {\n";
903 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800904 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700905 out.unindent();
906 out << "}\n\n";
907 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700908}
909
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700910void Interface::emitVtsMethodDeclaration(Formatter& out, bool isInherited) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700911 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800912 if (method->isHidlReserved()) {
913 continue;
914 }
915
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700916 out << "api: {\n";
917 out.indent();
918 out << "name: \"" << method->name() << "\"\n";
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700919 out << "is_inherited: " << (isInherited ? "true" : "false") << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700920 // Generate declaration for each return value.
921 for (const auto &result : method->results()) {
922 out << "return_type_hidl: {\n";
923 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700924 out << "name: \"" << result->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800925 result->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700926 out.unindent();
927 out << "}\n";
928 }
929 // Generate declaration for each input argument
930 for (const auto &arg : method->args()) {
931 out << "arg: {\n";
932 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700933 out << "name: \"" << arg->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800934 arg->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700935 out.unindent();
936 out << "}\n";
937 }
938 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700939 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700940 out << "callflow: {\n";
941 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800942 const std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700943 if (name == "entry") {
944 out << "entry: true\n";
945 } else if (name == "exit") {
946 out << "exit: true\n";
947 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700948 const AnnotationParam *param =
949 annotation->getParam("next");
950 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700951 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700952 out << "next: " << value << "\n";
953 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700954 }
955 } else {
Steven Moreland368e4602018-02-16 14:21:49 -0800956 CHECK(false);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700957 }
958 out.unindent();
959 out << "}\n";
960 }
961 out.unindent();
962 out << "}\n\n";
963 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700964}
965
Steven Moreland368e4602018-02-16 14:21:49 -0800966void Interface::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800967 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700968 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800969 << fullName()
970 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700971}
972
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700973bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
974 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700975 return false;
976 }
977
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700978 for (const auto* method : methods()) {
979 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700980 return false;
981 }
982 }
983
Steven Morelandb0da6fc2018-10-16 17:15:11 -0700984 return Scope::deepIsJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -0700985}
986
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700987bool Interface::isNeverStrongReference() const {
988 return true;
989}
990
Andreas Huberc9410c72016-07-28 12:18:40 -0700991} // namespace android
992