blob: 1fa9ed34ec6ba022cbb6dbd247155241a37e0766 [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>
Neel Mehta69920a62019-07-22 16:22:13 -070033#include <string>
Timur Iskhakovcec46c42017-08-09 00:22:02 -070034#include <unordered_map>
Yifan Hong30b5d1f2017-04-03 12:19:25 -070035
Steven Moreland14ee6742016-10-18 12:58:28 -070036#include <android-base/logging.h>
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070037#include <hidl-util/Formatter.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070038#include <hidl-util/StringHelper.h>
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070039
Andreas Huberc9410c72016-07-28 12:18:40 -070040namespace android {
41
Steven Morelandb8e15a52017-02-13 19:35:40 -080042#define B_PACK_CHARS(c1, c2, c3, c4) \
43 ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4))
44
Yifan Hong10fe0b52016-10-19 14:20:17 -070045/* It is very important that these values NEVER change. These values
46 * must remain unchanged over the lifetime of android. This is
47 * because the framework on a device will be updated independently of
48 * the hals on a device. If the hals are compiled with one set of
49 * transaction values, and the framework with another, then the
50 * interface between them will be destroyed, and the device will not
51 * work.
52 */
53enum {
Yifan Hong10fe0b52016-10-19 14:20:17 -070054 /////////////////// User defined transactions
55 FIRST_CALL_TRANSACTION = 0x00000001,
Steven Morelandb8e15a52017-02-13 19:35:40 -080056 LAST_CALL_TRANSACTION = 0x0effffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070057 /////////////////// HIDL reserved
Steven Morelandb8e15a52017-02-13 19:35:40 -080058 FIRST_HIDL_TRANSACTION = 0x0f000000,
59 HIDL_PING_TRANSACTION = B_PACK_CHARS(0x0f, 'P', 'N', 'G'),
60 HIDL_DESCRIPTOR_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'C', 'H', 'N'),
61 HIDL_GET_DESCRIPTOR_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'S', 'C'),
62 HIDL_SYSPROPS_CHANGED_TRANSACTION = B_PACK_CHARS(0x0f, 'S', 'Y', 'S'),
63 HIDL_LINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'L', 'T', 'D'),
64 HIDL_UNLINK_TO_DEATH_TRANSACTION = B_PACK_CHARS(0x0f, 'U', 'T', 'D'),
65 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION = B_PACK_CHARS(0x0f, 'I', 'N', 'T'),
66 HIDL_GET_REF_INFO_TRANSACTION = B_PACK_CHARS(0x0f, 'R', 'E', 'F'),
67 HIDL_DEBUG_TRANSACTION = B_PACK_CHARS(0x0f, 'D', 'B', 'G'),
Yifan Hong30b5d1f2017-04-03 12:19:25 -070068 HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'),
Steven Morelandb8e15a52017-02-13 19:35:40 -080069 LAST_HIDL_TRANSACTION = 0x0fffffff,
Yifan Hong10fe0b52016-10-19 14:20:17 -070070};
71
Steven Moreland77943692018-08-09 12:53:42 -070072const std::unique_ptr<ConstantExpression> Interface::FLAG_ONE_WAY =
73 std::make_unique<LiteralConstantExpression>(ScalarType::KIND_UINT32, 0x01, "oneway");
74
Neel Mehta69920a62019-07-22 16:22:13 -070075Interface::Interface(const std::string& localName, const FQName& fullName, const Location& location,
Steven Moreland04dea8d2018-02-06 13:11:24 -080076 Scope* parent, const Reference<Type>& superType, const Hash* fileHash)
77 : Scope(localName, fullName, location, parent), mSuperType(superType), mFileHash(fileHash) {}
Martijn Coenenaf712c02016-11-16 15:26:27 +010078
Steven Moreland30bb6a82016-11-30 09:18:34 -080079std::string Interface::typeName() const {
80 return "interface " + localName();
81}
82
Steven Moreland04dea8d2018-02-06 13:11:24 -080083const Hash* Interface::getFileHash() const {
84 return mFileHash;
85}
86
Steven Moreland424a9482017-02-13 19:20:40 -080087bool Interface::fillPingMethod(Method *method) const {
88 if (method->name() != "ping") {
89 return false;
90 }
91
92 method->fillImplementation(
93 HIDL_PING_TRANSACTION,
94 {
Steven Moreland937408a2017-03-20 09:54:18 -070095 {IMPL_INTERFACE,
Steven Moreland424a9482017-02-13 19:20:40 -080096 [](auto &out) {
97 out << "return ::android::hardware::Void();\n";
98 }
99 },
100 {IMPL_STUB_IMPL,
101 [](auto &out) {
102 out << "return ::android::hardware::Void();\n";
103 }
104 }
105 }, /*cppImpl*/
106 {
Steven Moreland937408a2017-03-20 09:54:18 -0700107 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700108 [](auto &out) {
Steven Moreland424a9482017-02-13 19:20:40 -0800109 out << "return;\n";
110 }
111 },
Steven Moreland424a9482017-02-13 19:20:40 -0800112 } /*javaImpl*/
113 );
114
115 return true;
116}
117
Yifan Hongffa91392017-01-31 13:41:23 -0800118bool Interface::fillLinkToDeathMethod(Method *method) const {
119 if (method->name() != "linkToDeath") {
120 return false;
121 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100122
Yifan Hongffa91392017-01-31 13:41:23 -0800123 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100124 HIDL_LINK_TO_DEATH_TRANSACTION,
125 {
Steven Moreland937408a2017-03-20 09:54:18 -0700126 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100127 [](auto &out) {
128 out << "(void)cookie;\n"
129 << "return (recipient != nullptr);\n";
130 }
131 },
132 {IMPL_PROXY,
133 [](auto &out) {
Martijn Coenenfa55d6e2016-12-20 06:08:31 +0100134 out << "::android::hardware::ProcessState::self()->startThreadPool();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100135 out << "::android::hardware::hidl_binder_death_recipient *binder_recipient"
136 << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n"
137 << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
138 << "_hidl_mDeathRecipients.push_back(binder_recipient);\n"
139 << "return (remote()->linkToDeath(binder_recipient)"
140 << " == ::android::OK);\n";
141 }
142 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100143 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100144 }, /*cppImpl*/
145 {
Steven Moreland937408a2017-03-20 09:54:18 -0700146 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700147 [](auto &out) {
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700148 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100149 }
150 },
151 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700152 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100153 out << "return mRemote.linkToDeath(recipient, cookie);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100154 }
155 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100156 {IMPL_STUB, nullptr}
Martijn Coenen115d4282016-12-19 05:14:04 +0100157 } /*javaImpl*/
158 );
Yifan Hongffa91392017-01-31 13:41:23 -0800159 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100160}
161
Yifan Hongffa91392017-01-31 13:41:23 -0800162bool Interface::fillUnlinkToDeathMethod(Method *method) const {
163 if (method->name() != "unlinkToDeath") {
164 return false;
165 }
Martijn Coenen115d4282016-12-19 05:14:04 +0100166
Yifan Hongffa91392017-01-31 13:41:23 -0800167 method->fillImplementation(
Martijn Coenen115d4282016-12-19 05:14:04 +0100168 HIDL_UNLINK_TO_DEATH_TRANSACTION,
169 {
Steven Moreland937408a2017-03-20 09:54:18 -0700170 {IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +0100171 [](auto &out) {
172 out << "return (recipient != nullptr);\n";
173 }
174 },
175 {IMPL_PROXY,
176 [](auto &out) {
177 out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n"
Steven Morelandba140f42018-07-24 13:13:15 -0700178 << "for (auto it = _hidl_mDeathRecipients.rbegin();"
179 << "it != _hidl_mDeathRecipients.rend();"
Martijn Coenen115d4282016-12-19 05:14:04 +0100180 << "++it) {\n";
181 out.indent([&] {
182 out.sIf("(*it)->getRecipient() == recipient", [&] {
183 out << "::android::status_t status = remote()->unlinkToDeath(*it);\n"
Steven Morelandba140f42018-07-24 13:13:15 -0700184 << "_hidl_mDeathRecipients.erase(it.base()-1);\n"
Martijn Coenen115d4282016-12-19 05:14:04 +0100185 << "return status == ::android::OK;\n";
186 });
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700187 }).endl();
Martijn Coenen115d4282016-12-19 05:14:04 +0100188 out << "}\n";
189 out << "return false;\n";
190 }
191 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100192 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100193 }, /*cppImpl*/
194 {
Steven Moreland937408a2017-03-20 09:54:18 -0700195 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700196 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100197 out << "return true;\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100198 }
199 },
200 {IMPL_PROXY,
Yi Kongc8ff4672017-04-30 23:46:56 -0700201 [](auto &out) {
Martijn Coenen8d12b502016-12-27 14:30:27 +0100202 out << "return mRemote.unlinkToDeath(recipient);\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100203 }
204 },
Martijn Coenen8d12b502016-12-27 14:30:27 +0100205 {IMPL_STUB, nullptr /* don't generate code */}
Martijn Coenen115d4282016-12-19 05:14:04 +0100206 } /*javaImpl*/
207 );
Yifan Hongffa91392017-01-31 13:41:23 -0800208 return true;
Martijn Coenen115d4282016-12-19 05:14:04 +0100209}
Yifan Hongffa91392017-01-31 13:41:23 -0800210bool Interface::fillSyspropsChangedMethod(Method *method) const {
211 if (method->name() != "notifySyspropsChanged") {
212 return false;
213 }
214
215 method->fillImplementation(
Martijn Coenenaf712c02016-11-16 15:26:27 +0100216 HIDL_SYSPROPS_CHANGED_TRANSACTION,
Yi Kongc8ff4672017-04-30 23:46:56 -0700217 { { IMPL_INTERFACE, [](auto &out) {
Martijn Coenenaf712c02016-11-16 15:26:27 +0100218 out << "::android::report_sysprop_change();\n";
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700219 out << "return ::android::hardware::Void();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100220 } } }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700221 { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700222 out << "android.os.HwBinder.enableInstrumentation();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100223 } } } /*javaImpl */
Martijn Coenenaf712c02016-11-16 15:26:27 +0100224 );
Yifan Hongffa91392017-01-31 13:41:23 -0800225 return true;
Andreas Huberc9410c72016-07-28 12:18:40 -0700226}
227
Yifan Hongffa91392017-01-31 13:41:23 -0800228bool Interface::fillSetHALInstrumentationMethod(Method *method) const {
229 if (method->name() != "setHALInstrumentation") {
230 return false;
231 }
232
233 method->fillImplementation(
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800234 HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION,
235 {
Steven Moreland937408a2017-03-20 09:54:18 -0700236 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700237 [](auto &out) {
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800238 // do nothing for base class.
239 out << "return ::android::hardware::Void();\n";
240 }
241 },
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800242 {IMPL_STUB,
243 [](auto &out) {
244 out << "configureInstrumentation();\n";
245 }
246 },
247 {IMPL_PASSTHROUGH,
248 [](auto &out) {
249 out << "configureInstrumentation();\n";
250 out << "return ::android::hardware::Void();\n";
251 }
252 },
253 }, /*cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700254 { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800255 // Not support for Java Impl for now.
256 } } } /*javaImpl */
257 );
Yifan Hongffa91392017-01-31 13:41:23 -0800258 return true;
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -0800259}
260
Yifan Hongffa91392017-01-31 13:41:23 -0800261bool Interface::fillDescriptorChainMethod(Method *method) const {
262 if (method->name() != "interfaceChain") {
263 return false;
264 }
Yifan Hong10fe0b52016-10-19 14:20:17 -0700265
Yifan Hongffa91392017-01-31 13:41:23 -0800266 method->fillImplementation(
Yifan Hong10fe0b52016-10-19 14:20:17 -0700267 HIDL_DESCRIPTOR_CHAIN_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700268 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700269 std::vector<const Interface *> chain = typeChain();
Yifan Hong03935122016-12-19 11:10:40 -0800270 out << "_hidl_cb(";
271 out.block([&] {
272 for (const Interface *iface : chain) {
273 out << iface->fullName() << "::descriptor,\n";
274 }
275 });
276 out << ");\n";
Steven Moreland3768f422019-05-09 12:29:12 -0700277 out << "return ::android::hardware::Void();\n";
Martijn Coenen115d4282016-12-19 05:14:04 +0100278 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700279 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700280 std::vector<const Interface *> chain = typeChain();
Yifan Hong1af73532016-11-09 14:32:58 -0800281 out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700282 out.indent(); out.indent();
283 for (size_t i = 0; i < chain.size(); ++i) {
284 if (i != 0)
285 out << ",\n";
Steven Morelandd39133b2016-11-11 12:30:08 -0800286 out << chain[i]->fullJavaName() << ".kInterfaceName";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700287 }
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700288 out << "));\n";
Yifan Hong10fe0b52016-10-19 14:20:17 -0700289 out.unindent(); out.unindent();
Yifan Hongffa91392017-01-31 13:41:23 -0800290 } } } /* javaImpl */
Martijn Coenen115d4282016-12-19 05:14:04 +0100291 );
Yifan Hongffa91392017-01-31 13:41:23 -0800292 return true;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700293}
294
Steven Moreland04dea8d2018-02-06 13:11:24 -0800295void Interface::emitDigestChain(
Timur Iskhakov7296af12017-08-09 21:52:48 +0000296 Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain,
Steven Moreland04dea8d2018-02-06 13:11:24 -0800297 std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const {
298 out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) {
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700299 out << prefix;
300 out << "{";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800301 out.join(
302 iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",",
303 [&](const auto& e) {
304 // Use ConstantExpression::cppValue / javaValue
305 // because Java used signed byte for uint8_t.
306 out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e));
307 });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700308 out << "} /* ";
Steven Moreland04dea8d2018-02-06 13:11:24 -0800309 out << iface->getFileHash()->hexString();
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700310 out << " */";
311 });
312}
313
314bool Interface::fillHashChainMethod(Method *method) const {
315 if (method->name() != "getHashChain") {
316 return false;
317 }
318 const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type());
319 const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType());
320
321 method->fillImplementation(
322 HIDL_HASH_CHAIN_TRANSACTION,
323 { { IMPL_INTERFACE, [this, digestType](auto &out) {
324 std::vector<const Interface *> chain = typeChain();
325 out << "_hidl_cb(";
326 out.block([&] {
Timur Iskhakov7296af12017-08-09 21:52:48 +0000327 emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain,
328 [](const auto& e) { return e->cppValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700329 });
330 out << ");\n";
331 out << "return ::android::hardware::Void();\n";
332 } } }, /* cppImpl */
333 { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) {
334 std::vector<const Interface *> chain = typeChain();
335 out << "return new "
336 << chainType->getJavaType(false /* forInitializer */)
337 << "(java.util.Arrays.asList(\n";
338 out.indent(2, [&] {
339 // No need for dimensions when elements are explicitly provided.
340 emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */),
Timur Iskhakov7296af12017-08-09 21:52:48 +0000341 chain, [](const auto& e) { return e->javaValue(); });
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700342 });
343 out << "));\n";
344 } } } /* javaImpl */
345 );
346 return true;
347}
348
Yifan Hongffa91392017-01-31 13:41:23 -0800349bool Interface::fillGetDescriptorMethod(Method *method) const {
350 if (method->name() != "interfaceDescriptor") {
351 return false;
352 }
Yifan Hongc75fd472017-01-11 12:37:31 -0800353
Yifan Hongffa91392017-01-31 13:41:23 -0800354 method->fillImplementation(
Yifan Hongc75fd472017-01-11 12:37:31 -0800355 HIDL_GET_DESCRIPTOR_TRANSACTION,
Steven Moreland937408a2017-03-20 09:54:18 -0700356 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800357 out << "_hidl_cb("
358 << fullName()
359 << "::descriptor);\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700360 << "return ::android::hardware::Void();\n";
Yifan Hongc75fd472017-01-11 12:37:31 -0800361 } } }, /* cppImpl */
Steven Moreland937408a2017-03-20 09:54:18 -0700362 { { IMPL_INTERFACE, [this](auto &out) {
Yifan Hongc75fd472017-01-11 12:37:31 -0800363 out << "return "
364 << fullJavaName()
365 << ".kInterfaceName;\n";
Yifan Hongffa91392017-01-31 13:41:23 -0800366 } } } /* javaImpl */
Yifan Hongc75fd472017-01-11 12:37:31 -0800367 );
Yifan Hongffa91392017-01-31 13:41:23 -0800368 return true;
Yifan Hongc75fd472017-01-11 12:37:31 -0800369}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700370
Yifan Hongbcffce22017-02-01 15:52:06 -0800371bool Interface::fillGetDebugInfoMethod(Method *method) const {
372 if (method->name() != "getDebugInfo") {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800373 return false;
374 }
375
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800376 static const std::string sArch =
377 "#if defined(__LP64__)\n"
378 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n"
379 "#else\n"
380 "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n"
381 "#endif\n";
382
Yifan Hongcd2ae452017-01-31 14:33:40 -0800383 method->fillImplementation(
384 HIDL_GET_REF_INFO_TRANSACTION,
385 {
Steven Moreland937408a2017-03-20 09:54:18 -0700386 {IMPL_INTERFACE,
Yi Kongc8ff4672017-04-30 23:46:56 -0700387 [](auto &out) {
Yifan Hongbcffce22017-02-01 15:52:06 -0800388 // getDebugInfo returns N/A for local objects.
Yifan Hongdf1ea3f2017-03-02 17:02:10 -0800389 out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n"
390 << sArch
391 << "});\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700392 << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800393 }
394 },
395 {IMPL_STUB_IMPL,
Yi Kongc8ff4672017-04-30 23:46:56 -0700396 [](auto &out) {
Yifan Hong2e036c92017-03-07 13:18:12 -0800397 out << "_hidl_cb(";
398 out.block([&] {
Yifan Hong96bb0632017-11-14 16:08:37 -0800399 out << "::android::hardware::details::getPidIfSharable(),\n"
Yifan Hong2e036c92017-03-07 13:18:12 -0800400 << "::android::hardware::details::debuggable()"
401 << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n"
402 << sArch << "\n";
403 });
404 out << ");\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700405 << "return ::android::hardware::Void();\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800406 }
407 }
408 }, /* cppImpl */
Yi Kongc8ff4672017-04-30 23:46:56 -0700409 { { IMPL_INTERFACE, [method](auto &out) {
Yifan Hongcd2ae452017-01-31 14:33:40 -0800410 const Type &refInfo = method->results().front()->type();
411 out << refInfo.getJavaType(false /* forInitializer */) << " info = new "
412 << refInfo.getJavaType(true /* forInitializer */) << "();\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800413 << "info.pid = android.os.HidlSupport.getPidIfSharable();\n"
Yifan Hongbcffce22017-02-01 15:52:06 -0800414 << "info.ptr = 0;\n"
Yifan Hong96bb0632017-11-14 16:08:37 -0800415 << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n"
Steven Moreland23cc5fa2018-05-09 10:48:48 -0700416 << "return info;\n";
Yifan Hongcd2ae452017-01-31 14:33:40 -0800417 } } } /* javaImpl */
418 );
419
420 return true;
421}
422
Andreas Huber37065d62017-02-07 14:36:54 -0800423bool Interface::fillDebugMethod(Method *method) const {
424 if (method->name() != "debug") {
425 return false;
426 }
427
Steven Moreland327fd8b2018-08-10 15:40:41 -0700428 method->fillImplementation(HIDL_DEBUG_TRANSACTION,
429 {
430 {IMPL_INTERFACE,
431 [](auto& out) {
432 out << "(void)fd;\n"
433 << "(void)options;\n"
434 << "return ::android::hardware::Void();\n";
435 }},
436 }, /* cppImpl */
437 {
438 {IMPL_INTERFACE, [](auto& out) { out << "return;\n"; }},
439 } /* javaImpl */
Andreas Huber37065d62017-02-07 14:36:54 -0800440 );
441
442 return true;
443}
444
Neel Mehta0ee353f2019-05-30 17:40:29 -0700445void Interface::addUserDefinedMethod(Method* method) {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700446 CHECK(!method->isHidlReserved());
Yifan Hong10fe0b52016-10-19 14:20:17 -0700447 mUserMethods.push_back(method);
Andreas Huberc9410c72016-07-28 12:18:40 -0700448}
449
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700450std::vector<const Reference<Type>*> Interface::getReferences() const {
451 std::vector<const Reference<Type>*> ret;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700452
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700453 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700454 ret.push_back(&mSuperType);
Timur Iskhakov33431e62017-08-21 17:31:23 -0700455 }
456
457 for (const auto* method : methods()) {
458 const auto& references = method->getReferences();
459 ret.insert(ret.end(), references.begin(), references.end());
460 }
461
462 return ret;
463}
464
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700465std::vector<const ConstantExpression*> Interface::getConstantExpressions() const {
466 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700467 for (const auto* method : methods()) {
468 const auto& retMethod = method->getConstantExpressions();
469 ret.insert(ret.end(), retMethod.begin(), retMethod.end());
470 }
471 return ret;
472}
473
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700474std::vector<const Reference<Type>*> Interface::getStrongReferences() const {
Timur Iskhakov40731af2017-08-24 14:18:35 -0700475 // Interface is a special case as a reference:
476 // its definiton must be completed for extension but
477 // not necessary for other references.
Timur Iskhakov40731af2017-08-24 14:18:35 -0700478
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700479 std::vector<const Reference<Type>*> ret;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700480 if (!isIBase()) {
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700481 ret.push_back(&mSuperType);
Timur Iskhakov40731af2017-08-24 14:18:35 -0700482 }
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700483
484 for (const auto* method : methods()) {
485 const auto& references = method->getStrongReferences();
486 ret.insert(ret.end(), references.begin(), references.end());
487 }
488
Timur Iskhakov40731af2017-08-24 14:18:35 -0700489 return ret;
490}
491
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700492status_t Interface::resolveInheritance() {
493 size_t serial = FIRST_CALL_TRANSACTION;
494 for (const auto* ancestor : superTypeChain()) {
495 serial += ancestor->mUserMethods.size();
496 }
497
498 for (Method* method : mUserMethods) {
499 if (serial > LAST_CALL_TRANSACTION) {
500 std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION
501 << " methods (including super and reserved) are not allowed at " << location()
Steven Morelandcbff5612017-10-11 17:01:54 -0700502 << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700503 return UNKNOWN_ERROR;
504 }
505
506 method->setSerialId(serial);
507 serial++;
508 }
509
510 return Scope::resolveInheritance();
511}
512
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700513status_t Interface::validate() const {
514 CHECK(isIBase() == mSuperType.isEmptyReference());
515
Timur Iskhakov0344e612017-08-25 12:57:09 -0700516 if (!isIBase() && !mSuperType->isInterface()) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700517 std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location()
518 << std::endl;
Timur Iskhakov0344e612017-08-25 12:57:09 -0700519 return UNKNOWN_ERROR;
520 }
521
Steven Moreland368e4602018-02-16 14:21:49 -0800522 status_t err;
523
524 err = validateUniqueNames();
525 if (err != OK) return err;
526
527 err = validateAnnotations();
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700528 if (err != OK) return err;
529
530 return Scope::validate();
531}
532
Howard Chenecfb4512017-11-21 18:28:53 +0800533void Interface::getAlignmentAndSize(size_t* align, size_t* size) const {
534 *align = 8;
535 *size = 8;
536}
537
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700538status_t Interface::validateUniqueNames() const {
539 std::unordered_map<std::string, const Interface*> registeredMethodNames;
540 for (auto const& tuple : allSuperMethodsFromRoot()) {
541 // No need to check super method uniqueness
542 registeredMethodNames[tuple.method()->name()] = tuple.interface();
543 }
544
545 for (const Method* method : mUserMethods) {
546 auto registered = registeredMethodNames.find(method->name());
547
548 if (registered != registeredMethodNames.end()) {
549 const Interface* definedInType = registered->second;
550
551 if (definedInType == this) {
552 // Defined in this interface
553 std::cerr << "ERROR: Redefinition of method '" << method->name() << "'";
554 } else if (definedInType->isIBase()) {
555 // Defined in IBase
556 std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'";
557 } else {
558 // Defined in super not IBase
559 std::cerr << "ERROR: Redefinition of method '" << method->name()
Timur Iskhakov4c9c20b2017-09-13 21:16:59 -0700560 << "' defined in interface '" << definedInType->fullName() << "'";
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700561 }
Steven Morelandcbff5612017-10-11 17:01:54 -0700562 std::cerr << " at " << method->location() << std::endl;
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700563 return UNKNOWN_ERROR;
564 }
565
566 registeredMethodNames[method->name()] = this;
567 }
568
569 return OK;
570}
571
Steven Moreland368e4602018-02-16 14:21:49 -0800572status_t Interface::validateAnnotations() const {
573 for (const Method* method : methods()) {
574 for (const Annotation* annotation : method->annotations()) {
575 const std::string name = annotation->name();
576
577 if (name == "entry" || name == "exit" || name == "callflow") {
578 continue;
579 }
580
581 std::cerr << "ERROR: Unrecognized annotation '" << name
582 << "' for method: " << method->name() << ". An annotation should be one of: "
583 << "entry, exit, callflow." << std::endl;
584 return UNKNOWN_ERROR;
585 }
586 }
587 return OK;
588}
589
Neel Mehta0ee353f2019-05-30 17:40:29 -0700590bool Interface::addAllReservedMethods(const std::map<std::string, Method*>& allReservedMethods) {
Yifan Hongffa91392017-01-31 13:41:23 -0800591 // use a sorted map to insert them in serial ID order.
592 std::map<int32_t, Method *> reservedMethodsById;
Neel Mehta0ee353f2019-05-30 17:40:29 -0700593 for (const auto& pair : allReservedMethods) {
Yifan Hongffa91392017-01-31 13:41:23 -0800594 Method *method = pair.second->copySignature();
Steven Moreland424a9482017-02-13 19:20:40 -0800595 bool fillSuccess = fillPingMethod(method)
596 || fillDescriptorChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800597 || fillGetDescriptorMethod(method)
Yifan Hong30b5d1f2017-04-03 12:19:25 -0700598 || fillHashChainMethod(method)
Yifan Hongffa91392017-01-31 13:41:23 -0800599 || fillSyspropsChangedMethod(method)
600 || fillLinkToDeathMethod(method)
601 || fillUnlinkToDeathMethod(method)
Yifan Hongcd2ae452017-01-31 14:33:40 -0800602 || fillSetHALInstrumentationMethod(method)
Andreas Huber37065d62017-02-07 14:36:54 -0800603 || fillGetDebugInfoMethod(method)
604 || fillDebugMethod(method);
605
Yifan Hongffa91392017-01-31 13:41:23 -0800606 if (!fillSuccess) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700607 std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name()
608 << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800609 return false;
610 }
611 if (!reservedMethodsById.emplace(method->getSerialId(), method).second) {
Steven Morelandcbff5612017-10-11 17:01:54 -0700612 std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name()
613 << " and " << reservedMethodsById[method->getSerialId()]->name()
614 << ", serialId = " << method->getSerialId() << std::endl;
Yifan Hongffa91392017-01-31 13:41:23 -0800615 return false;
616 }
617 }
618 for (const auto &pair : reservedMethodsById) {
619 this->mReservedMethods.push_back(pair.second);
620 }
621 return true;
622}
Yifan Hong10fe0b52016-10-19 14:20:17 -0700623
Timur Iskhakov505316c2017-08-05 03:38:59 +0000624const Interface* Interface::superType() const {
Timur Iskhakov0344e612017-08-25 12:57:09 -0700625 if (isIBase()) return nullptr;
626 if (!mSuperType->isInterface()) {
627 // This is actually an error
628 // that would be caught in validate
629 return nullptr;
630 }
Timur Iskhakov24e605b2017-08-30 14:02:55 -0700631 return static_cast<const Interface*>(mSuperType.get());
Andreas Huberc9410c72016-07-28 12:18:40 -0700632}
633
Yifan Hong10fe0b52016-10-19 14:20:17 -0700634std::vector<const Interface *> Interface::typeChain() const {
635 std::vector<const Interface *> v;
636 const Interface *iface = this;
637 while (iface != nullptr) {
638 v.push_back(iface);
Timur Iskhakov505316c2017-08-05 03:38:59 +0000639 iface = iface->superType();
Yifan Hong10fe0b52016-10-19 14:20:17 -0700640 }
641 return v;
642}
643
Yifan Hongfe95aa22016-10-19 17:26:45 -0700644std::vector<const Interface *> Interface::superTypeChain() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +0000645 return isIBase() ? std::vector<const Interface*>() : superType()->typeChain();
Yifan Hongfe95aa22016-10-19 17:26:45 -0700646}
647
Martijn Coenenb40ef022017-01-02 15:21:46 +0100648bool Interface::isElidableType() const {
649 return true;
650}
651
Andreas Hubera2723d22016-07-29 15:36:07 -0700652bool Interface::isInterface() const {
653 return true;
654}
655
Yifan Hong10fe0b52016-10-19 14:20:17 -0700656const std::vector<Method *> &Interface::userDefinedMethods() const {
657 return mUserMethods;
658}
659
660const std::vector<Method *> &Interface::hidlReservedMethods() const {
661 return mReservedMethods;
662}
663
664std::vector<Method *> Interface::methods() const {
665 std::vector<Method *> v(mUserMethods);
666 v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end());
667 return v;
668}
669
670std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const {
671 std::vector<InterfaceAndMethod> v;
672 std::vector<const Interface *> chain = typeChain();
673 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
674 const Interface *iface = *it;
675 for (Method *userMethod : iface->userDefinedMethods()) {
676 v.push_back(InterfaceAndMethod(iface, userMethod));
677 }
678 }
679 for (Method *reservedMethod : hidlReservedMethods()) {
Yifan Hongc8934042016-11-17 17:10:52 -0800680 v.push_back(InterfaceAndMethod(
681 *chain.rbegin(), // IBase
682 reservedMethod));
Yifan Hong10fe0b52016-10-19 14:20:17 -0700683 }
684 return v;
Andreas Huber881227d2016-08-02 14:20:21 -0700685}
686
Timur Iskhakovf1b902d2017-08-13 20:14:31 -0700687std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const {
688 return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot();
689}
690
Steven Moreland40786312016-08-16 10:29:40 -0700691std::string Interface::getBaseName() const {
Jayant Chowdhary3f32c1f2016-09-15 16:53:56 -0700692 return fqName().getInterfaceBaseName();
Steven Moreland40786312016-08-16 10:29:40 -0700693}
694
Steven Moreland9a6da7a2017-09-15 16:21:24 -0700695std::string Interface::getAdapterName() const {
696 return fqName().getInterfaceAdapterName();
697}
698
Yifan Hongeefe4f22017-01-04 15:32:42 -0800699std::string Interface::getProxyName() const {
700 return fqName().getInterfaceProxyName();
701}
702
703std::string Interface::getStubName() const {
704 return fqName().getInterfaceStubName();
705}
706
707std::string Interface::getHwName() const {
708 return fqName().getInterfaceHwName();
709}
710
711std::string Interface::getPassthroughName() const {
712 return fqName().getInterfacePassthroughName();
713}
714
Yifan Hong51a65092017-01-04 15:41:44 -0800715FQName Interface::getProxyFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800716 return fqName().getInterfaceProxyFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800717}
718
Yifan Hong51a65092017-01-04 15:41:44 -0800719FQName Interface::getStubFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800720 return fqName().getInterfaceStubFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800721}
722
Yifan Hong51a65092017-01-04 15:41:44 -0800723FQName Interface::getPassthroughFqName() const {
Yifan Hongeefe4f22017-01-04 15:32:42 -0800724 return fqName().getInterfacePassthroughFqName();
Yifan Hong158655a2016-11-08 12:34:07 -0800725}
726
Steven Moreland979e0992016-09-07 09:18:08 -0700727std::string Interface::getCppType(StorageMode mode,
Steven Moreland979e0992016-09-07 09:18:08 -0700728 bool specifyNamespaces) const {
Steven Moreland979e0992016-09-07 09:18:08 -0700729 const std::string base =
730 std::string(specifyNamespaces ? "::android::" : "")
731 + "sp<"
Steven Morelande30ee9b2017-05-09 13:31:01 -0700732 + fullName()
Steven Moreland979e0992016-09-07 09:18:08 -0700733 + ">";
Andreas Huber881227d2016-08-02 14:20:21 -0700734
735 switch (mode) {
736 case StorageMode_Stack:
737 case StorageMode_Result:
738 return base;
739
740 case StorageMode_Argument:
741 return "const " + base + "&";
742 }
743}
744
Yifan Hong4ed13472016-11-02 10:44:11 -0700745std::string Interface::getJavaType(bool /* forInitializer */) const {
Andreas Huber2831d512016-08-15 09:33:47 -0700746 return fullJavaName();
747}
748
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800749std::string Interface::getVtsType() const {
750 if (StringHelper::EndsWith(localName(), "Callback")) {
751 return "TYPE_HIDL_CALLBACK";
752 } else {
753 return "TYPE_HIDL_INTERFACE";
754 }
755}
756
Andreas Huber881227d2016-08-02 14:20:21 -0700757void Interface::emitReaderWriter(
758 Formatter &out,
759 const std::string &name,
760 const std::string &parcelObj,
761 bool parcelObjIsPointer,
762 bool isReader,
763 ErrorMode mode) const {
764 const std::string parcelObjDeref =
765 parcelObj + (parcelObjIsPointer ? "->" : ".");
766
767 if (isReader) {
Andreas Hubere7ff2282016-08-16 13:50:03 -0700768 out << "{\n";
769 out.indent();
770
Howard Chenecfb4512017-11-21 18:28:53 +0800771 const std::string binderName = "_hidl_binder";
Andreas Huber8a82ff72016-08-04 10:29:39 -0700772 out << "::android::sp<::android::hardware::IBinder> "
Andreas Huber881227d2016-08-02 14:20:21 -0700773 << binderName << ";\n";
774
Iliyan Malchev549e2592016-08-10 08:59:12 -0700775 out << "_hidl_err = ";
Andreas Huber881227d2016-08-02 14:20:21 -0700776 out << parcelObjDeref
777 << "readNullableStrongBinder(&"
778 << binderName
779 << ");\n";
780
781 handleError(out, mode);
782
783 out << name
784 << " = "
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100785 << "::android::hardware::fromBinder<"
786 << fqName().cppName()
787 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800788 << getProxyFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100789 << ","
Yifan Hong51a65092017-01-04 15:41:44 -0800790 << getStubFqName().cppName()
Martijn Coenena63e0ad2016-12-07 17:29:00 +0100791 << ">("
Andreas Huber881227d2016-08-02 14:20:21 -0700792 << binderName
793 << ");\n";
Andreas Hubere7ff2282016-08-16 13:50:03 -0700794
795 out.unindent();
796 out << "}\n\n";
Andreas Huber881227d2016-08-02 14:20:21 -0700797 } else {
Martijn Coenene1638232016-10-26 12:51:34 +0200798 out << "if (" << name << " == nullptr) {\n";
799 out.indent();
800 out << "_hidl_err = ";
801 out << parcelObjDeref
802 << "writeStrongBinder(nullptr);\n";
803 out.unindent();
804 out << "} else {\n";
805 out.indent();
Yifan Hong158655a2016-11-08 12:34:07 -0800806 out << "::android::sp<::android::hardware::IBinder> _hidl_binder = "
Steven Moreland731dbb12018-12-03 18:09:46 -0800807 << "::android::hardware::getOrCreateCachedBinder(" << name << ".get());\n";
Yifan Hong158655a2016-11-08 12:34:07 -0800808 out << "if (_hidl_binder.get() != nullptr) {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800809 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800810 out << "_hidl_err = "
811 << parcelObjDeref
812 << "writeStrongBinder(_hidl_binder);\n";
813 });
814 out << "} else {\n";
Yifan Hong33223ca2016-12-13 15:07:35 -0800815 out.indent([&] {
Yifan Hong158655a2016-11-08 12:34:07 -0800816 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
817 });
818 out << "}\n";
Martijn Coenene1638232016-10-26 12:51:34 +0200819 out.unindent();
820 out << "}\n";
Steven Moreland40786312016-08-16 10:29:40 -0700821
Andreas Huber881227d2016-08-02 14:20:21 -0700822 handleError(out, mode);
823 }
824}
825
Neel Mehta3b414a82019-07-02 15:47:48 -0700826void Interface::emitHidlDefinition(Formatter& out) const {
827 if (getDocComment() != nullptr) getDocComment()->emit(out);
828 out << typeName() << " ";
829
830 const Interface* super = superType();
831 if (super != nullptr && !super->isIBase()) {
832 out << "extends " << super->fqName().getRelativeFQName(fqName()) << " ";
833 }
834
835 out << "{\n";
836
837 out.indent([&] {
838 const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
839 out.join(definedTypes.begin(), definedTypes.end(), "\n",
840 [&](auto t) { t->emitHidlDefinition(out); });
841
842 if (definedTypes.size() > 0 && userDefinedMethods().size() > 0) out << "\n";
843
844 out.join(userDefinedMethods().begin(), userDefinedMethods().end(), "\n",
845 [&](auto method) { method->emitHidlDefinition(out); });
846 });
847
848 out << "};\n";
849}
850
Steven Moreland368e4602018-02-16 14:21:49 -0800851void Interface::emitPackageTypeDeclarations(Formatter& out) const {
852 Scope::emitPackageTypeDeclarations(out);
Steven Morelandbf714212017-10-27 18:29:01 -0700853
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700854 out << "static inline std::string toString(" << getCppArgumentType() << " o);\n\n";
855}
856
857void Interface::emitPackageTypeHeaderDefinitions(Formatter& out) const {
858 Scope::emitPackageTypeHeaderDefinitions(out);
859
Steven Morelandbf714212017-10-27 18:29:01 -0700860 out << "static inline std::string toString(" << getCppArgumentType() << " o) ";
861
862 out.block([&] {
863 out << "std::string os = \"[class or subclass of \";\n"
864 << "os += " << fullName() << "::descriptor;\n"
865 << "os += \"]\";\n"
866 << "os += o->isRemote() ? \"@remote\" : \"@local\";\n"
867 << "return os;\n";
868 }).endl().endl();
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800869}
870
Steven Moreland368e4602018-02-16 14:21:49 -0800871void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800872 std::string space = prefix.empty() ? "" : (prefix + "::");
Steven Morelandd8c7a292017-10-27 18:32:06 -0700873
Steven Moreland368e4602018-02-16 14:21:49 -0800874 Scope::emitTypeDefinitions(out, space + localName());
Yifan Hongf5cc2f72017-01-04 18:02:34 -0800875}
876
Andreas Huber2831d512016-08-15 09:33:47 -0700877void Interface::emitJavaReaderWriter(
878 Formatter &out,
879 const std::string &parcelObj,
880 const std::string &argName,
881 bool isReader) const {
882 if (isReader) {
883 out << fullJavaName()
884 << ".asInterface("
885 << parcelObj
886 << ".readStrongBinder());\n";
887 } else {
888 out << parcelObj
889 << ".writeStrongBinder("
890 << argName
891 << " == null ? null : "
892 << argName
893 << ".asBinder());\n";
894 }
895}
896
Steven Moreland368e4602018-02-16 14:21:49 -0800897void Interface::emitVtsAttributeDeclaration(Formatter& out) const {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700898 for (const auto &type : getSubTypes()) {
Zhuoyao Zhangc5ea9f52016-10-06 15:05:39 -0700899 // Skip for TypeDef as it is just an alias of a defined type.
900 if (type->isTypeDef()) {
901 continue;
902 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700903 out << "attribute: {\n";
904 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800905 type->emitVtsTypeDeclarations(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700906 out.unindent();
907 out << "}\n\n";
908 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700909}
910
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700911void Interface::emitVtsMethodDeclaration(Formatter& out, bool isInherited) const {
Yifan Hong10fe0b52016-10-19 14:20:17 -0700912 for (const auto &method : methods()) {
Steven Morelandcea24782016-11-07 11:40:48 -0800913 if (method->isHidlReserved()) {
914 continue;
915 }
916
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700917 out << "api: {\n";
918 out.indent();
919 out << "name: \"" << method->name() << "\"\n";
Zhuoyao Zhange59c9332018-07-20 14:16:04 -0700920 out << "is_inherited: " << (isInherited ? "true" : "false") << "\n";
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700921 // Generate declaration for each return value.
922 for (const auto &result : method->results()) {
923 out << "return_type_hidl: {\n";
924 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700925 out << "name: \"" << result->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800926 result->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700927 out.unindent();
928 out << "}\n";
929 }
930 // Generate declaration for each input argument
931 for (const auto &arg : method->args()) {
932 out << "arg: {\n";
933 out.indent();
Socrates Li7b0a42b2018-07-16 13:23:17 -0700934 out << "name: \"" << arg->name() << "\"\n";
Steven Moreland368e4602018-02-16 14:21:49 -0800935 arg->type().emitVtsAttributeType(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700936 out.unindent();
937 out << "}\n";
938 }
939 // Generate declaration for each annotation.
Steven Morelandd537ab02016-09-12 10:32:01 -0700940 for (const auto &annotation : method->annotations()) {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700941 out << "callflow: {\n";
942 out.indent();
Steven Moreland368e4602018-02-16 14:21:49 -0800943 const std::string name = annotation->name();
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700944 if (name == "entry") {
945 out << "entry: true\n";
946 } else if (name == "exit") {
947 out << "exit: true\n";
948 } else if (name == "callflow") {
Steven Morelandd537ab02016-09-12 10:32:01 -0700949 const AnnotationParam *param =
950 annotation->getParam("next");
951 if (param != nullptr) {
Timur Iskhakov7a85dc22017-08-10 19:06:41 -0700952 for (const auto& value : param->getValues()) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700953 out << "next: " << value << "\n";
954 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700955 }
956 } else {
Steven Moreland368e4602018-02-16 14:21:49 -0800957 CHECK(false);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700958 }
959 out.unindent();
960 out << "}\n";
961 }
962 out.unindent();
963 out << "}\n\n";
964 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700965}
966
Steven Moreland368e4602018-02-16 14:21:49 -0800967void Interface::emitVtsAttributeType(Formatter& out) const {
Zhuoyao Zhanga588b232016-11-10 14:37:35 -0800968 out << "type: " << getVtsType() << "\n"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700969 << "predefined_type: \""
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800970 << fullName()
971 << "\"\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700972}
973
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700974bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
975 if (superType() != nullptr && !superType()->isJavaCompatible(visited)) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700976 return false;
977 }
978
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700979 for (const auto* method : methods()) {
980 if (!method->deepIsJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700981 return false;
982 }
983 }
984
Steven Morelandb0da6fc2018-10-16 17:15:11 -0700985 return Scope::deepIsJavaCompatible(visited);
Andreas Huber70a59e12016-08-16 12:57:01 -0700986}
987
Timur Iskhakovff5e64a2017-09-11 14:56:18 -0700988bool Interface::isNeverStrongReference() const {
989 return true;
990}
991
Andreas Huberc9410c72016-07-28 12:18:40 -0700992} // namespace android
993