Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #include "Interface.h" |
| 18 | |
Zhuoyao Zhang | ba7e6e9 | 2016-08-10 12:19:02 -0700 | [diff] [blame] | 19 | #include "Annotation.h" |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 20 | #include "ArrayType.h" |
| 21 | #include "ConstantExpression.h" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 22 | #include "DeathRecipientType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 23 | #include "Method.h" |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 24 | #include "ScalarType.h" |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 25 | #include "StringType.h" |
| 26 | #include "VectorType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 30 | #include <iostream> |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 31 | #include <memory> |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 32 | #include <sstream> |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 33 | #include <unordered_map> |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 34 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 35 | #include <android-base/logging.h> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 36 | #include <hidl-util/Formatter.h> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 37 | #include <hidl-util/StringHelper.h> |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 38 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 41 | #define B_PACK_CHARS(c1, c2, c3, c4) \ |
| 42 | ((((c1)<<24)) | (((c2)<<16)) | (((c3)<<8)) | (c4)) |
| 43 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 44 | /* 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 | */ |
| 52 | enum { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 53 | /////////////////// User defined transactions |
| 54 | FIRST_CALL_TRANSACTION = 0x00000001, |
Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 55 | LAST_CALL_TRANSACTION = 0x0effffff, |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 56 | /////////////////// HIDL reserved |
Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 57 | 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 Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 67 | HIDL_HASH_CHAIN_TRANSACTION = B_PACK_CHARS(0x0f, 'H', 'S', 'H'), |
Steven Moreland | b8e15a5 | 2017-02-13 19:35:40 -0800 | [diff] [blame] | 68 | LAST_HIDL_TRANSACTION = 0x0fffffff, |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 71 | Interface::Interface(const char* localName, const FQName& fullName, const Location& location, |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 72 | Scope* parent, const Reference<Type>& superType, const Hash* fileHash) |
| 73 | : Scope(localName, fullName, location, parent), mSuperType(superType), mFileHash(fileHash) {} |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 74 | |
Steven Moreland | 30bb6a8 | 2016-11-30 09:18:34 -0800 | [diff] [blame] | 75 | std::string Interface::typeName() const { |
| 76 | return "interface " + localName(); |
| 77 | } |
| 78 | |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 79 | const Hash* Interface::getFileHash() const { |
| 80 | return mFileHash; |
| 81 | } |
| 82 | |
Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 83 | bool Interface::fillPingMethod(Method *method) const { |
| 84 | if (method->name() != "ping") { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | method->fillImplementation( |
| 89 | HIDL_PING_TRANSACTION, |
| 90 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 91 | {IMPL_INTERFACE, |
Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 92 | [](auto &out) { |
| 93 | out << "return ::android::hardware::Void();\n"; |
| 94 | } |
| 95 | }, |
| 96 | {IMPL_STUB_IMPL, |
| 97 | [](auto &out) { |
| 98 | out << "return ::android::hardware::Void();\n"; |
| 99 | } |
| 100 | } |
| 101 | }, /*cppImpl*/ |
| 102 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 103 | {IMPL_INTERFACE, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 104 | [](auto &out) { |
Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 105 | out << "return;\n"; |
| 106 | } |
| 107 | }, |
Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 108 | } /*javaImpl*/ |
| 109 | ); |
| 110 | |
| 111 | return true; |
| 112 | } |
| 113 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 114 | bool Interface::fillLinkToDeathMethod(Method *method) const { |
| 115 | if (method->name() != "linkToDeath") { |
| 116 | return false; |
| 117 | } |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 118 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 119 | method->fillImplementation( |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 120 | HIDL_LINK_TO_DEATH_TRANSACTION, |
| 121 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 122 | {IMPL_INTERFACE, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 123 | [](auto &out) { |
| 124 | out << "(void)cookie;\n" |
| 125 | << "return (recipient != nullptr);\n"; |
| 126 | } |
| 127 | }, |
| 128 | {IMPL_PROXY, |
| 129 | [](auto &out) { |
Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 130 | out << "::android::hardware::ProcessState::self()->startThreadPool();\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 131 | out << "::android::hardware::hidl_binder_death_recipient *binder_recipient" |
| 132 | << " = new ::android::hardware::hidl_binder_death_recipient(recipient, cookie, this);\n" |
| 133 | << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| 134 | << "_hidl_mDeathRecipients.push_back(binder_recipient);\n" |
| 135 | << "return (remote()->linkToDeath(binder_recipient)" |
| 136 | << " == ::android::OK);\n"; |
| 137 | } |
| 138 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 139 | {IMPL_STUB, nullptr} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 140 | }, /*cppImpl*/ |
| 141 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 142 | {IMPL_INTERFACE, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 143 | [](auto &out) { |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 144 | out << "return true;"; |
| 145 | } |
| 146 | }, |
| 147 | {IMPL_PROXY, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 148 | [](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 149 | out << "return mRemote.linkToDeath(recipient, cookie);\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 150 | } |
| 151 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 152 | {IMPL_STUB, nullptr} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 153 | } /*javaImpl*/ |
| 154 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 155 | return true; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 158 | bool Interface::fillUnlinkToDeathMethod(Method *method) const { |
| 159 | if (method->name() != "unlinkToDeath") { |
| 160 | return false; |
| 161 | } |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 162 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 163 | method->fillImplementation( |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 164 | HIDL_UNLINK_TO_DEATH_TRANSACTION, |
| 165 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 166 | {IMPL_INTERFACE, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 167 | [](auto &out) { |
| 168 | out << "return (recipient != nullptr);\n"; |
| 169 | } |
| 170 | }, |
| 171 | {IMPL_PROXY, |
| 172 | [](auto &out) { |
| 173 | out << "std::unique_lock<std::mutex> lock(_hidl_mMutex);\n" |
| 174 | << "for (auto it = _hidl_mDeathRecipients.begin();" |
| 175 | << "it != _hidl_mDeathRecipients.end();" |
| 176 | << "++it) {\n"; |
| 177 | out.indent([&] { |
| 178 | out.sIf("(*it)->getRecipient() == recipient", [&] { |
| 179 | out << "::android::status_t status = remote()->unlinkToDeath(*it);\n" |
| 180 | << "_hidl_mDeathRecipients.erase(it);\n" |
| 181 | << "return status == ::android::OK;\n"; |
| 182 | }); |
| 183 | }); |
| 184 | out << "}\n"; |
| 185 | out << "return false;\n"; |
| 186 | } |
| 187 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 188 | {IMPL_STUB, nullptr /* don't generate code */} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 189 | }, /*cppImpl*/ |
| 190 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 191 | {IMPL_INTERFACE, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 192 | [](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 193 | out << "return true;\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 194 | } |
| 195 | }, |
| 196 | {IMPL_PROXY, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 197 | [](auto &out) { |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 198 | out << "return mRemote.unlinkToDeath(recipient);\n"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 199 | } |
| 200 | }, |
Martijn Coenen | 8d12b50 | 2016-12-27 14:30:27 +0100 | [diff] [blame] | 201 | {IMPL_STUB, nullptr /* don't generate code */} |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 202 | } /*javaImpl*/ |
| 203 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 204 | return true; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 205 | } |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 206 | bool Interface::fillSyspropsChangedMethod(Method *method) const { |
| 207 | if (method->name() != "notifySyspropsChanged") { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | method->fillImplementation( |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 212 | HIDL_SYSPROPS_CHANGED_TRANSACTION, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 213 | { { IMPL_INTERFACE, [](auto &out) { |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 214 | out << "::android::report_sysprop_change();\n"; |
| 215 | out << "return ::android::hardware::Void();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 216 | } } }, /*cppImpl */ |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 217 | { { IMPL_INTERFACE, [](auto &out) { /* javaImpl */ |
Steven Moreland | 575ab5c | 2018-01-25 15:44:39 -0800 | [diff] [blame] | 218 | out << "android.os.HwBinder.enableInstrumentation();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 219 | } } } /*javaImpl */ |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 220 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 221 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 224 | bool Interface::fillSetHALInstrumentationMethod(Method *method) const { |
| 225 | if (method->name() != "setHALInstrumentation") { |
| 226 | return false; |
| 227 | } |
| 228 | |
| 229 | method->fillImplementation( |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 230 | HIDL_SET_HAL_INSTRUMENTATION_TRANSACTION, |
| 231 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 232 | {IMPL_INTERFACE, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 233 | [](auto &out) { |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 234 | // do nothing for base class. |
| 235 | out << "return ::android::hardware::Void();\n"; |
| 236 | } |
| 237 | }, |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 238 | {IMPL_STUB, |
| 239 | [](auto &out) { |
| 240 | out << "configureInstrumentation();\n"; |
| 241 | } |
| 242 | }, |
| 243 | {IMPL_PASSTHROUGH, |
| 244 | [](auto &out) { |
| 245 | out << "configureInstrumentation();\n"; |
| 246 | out << "return ::android::hardware::Void();\n"; |
| 247 | } |
| 248 | }, |
| 249 | }, /*cppImpl */ |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 250 | { { IMPL_INTERFACE, [](auto & /*out*/) { /* javaImpl */ |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 251 | // Not support for Java Impl for now. |
| 252 | } } } /*javaImpl */ |
| 253 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 254 | return true; |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 255 | } |
| 256 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 257 | bool Interface::fillDescriptorChainMethod(Method *method) const { |
| 258 | if (method->name() != "interfaceChain") { |
| 259 | return false; |
| 260 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 261 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 262 | method->fillImplementation( |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 263 | HIDL_DESCRIPTOR_CHAIN_TRANSACTION, |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 264 | { { IMPL_INTERFACE, [this](auto &out) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 265 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 0393512 | 2016-12-19 11:10:40 -0800 | [diff] [blame] | 266 | out << "_hidl_cb("; |
| 267 | out.block([&] { |
| 268 | for (const Interface *iface : chain) { |
| 269 | out << iface->fullName() << "::descriptor,\n"; |
| 270 | } |
| 271 | }); |
| 272 | out << ");\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 273 | out << "return ::android::hardware::Void();"; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 274 | } } }, /* cppImpl */ |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 275 | { { IMPL_INTERFACE, [this](auto &out) { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 276 | std::vector<const Interface *> chain = typeChain(); |
Yifan Hong | 1af7353 | 2016-11-09 14:32:58 -0800 | [diff] [blame] | 277 | out << "return new java.util.ArrayList<String>(java.util.Arrays.asList(\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 278 | out.indent(); out.indent(); |
| 279 | for (size_t i = 0; i < chain.size(); ++i) { |
| 280 | if (i != 0) |
| 281 | out << ",\n"; |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 282 | out << chain[i]->fullJavaName() << ".kInterfaceName"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 283 | } |
| 284 | out << "));"; |
| 285 | out.unindent(); out.unindent(); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 286 | } } } /* javaImpl */ |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 287 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 288 | return true; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 291 | void Interface::emitDigestChain( |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 292 | Formatter& out, const std::string& prefix, const std::vector<const Interface*>& chain, |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 293 | std::function<std::string(std::unique_ptr<ConstantExpression>)> byteToString) const { |
| 294 | out.join(chain.begin(), chain.end(), ",\n", [&](const auto& iface) { |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 295 | out << prefix; |
| 296 | out << "{"; |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 297 | out.join( |
| 298 | iface->getFileHash()->raw().begin(), iface->getFileHash()->raw().end(), ",", |
| 299 | [&](const auto& e) { |
| 300 | // Use ConstantExpression::cppValue / javaValue |
| 301 | // because Java used signed byte for uint8_t. |
| 302 | out << byteToString(ConstantExpression::ValueOf(ScalarType::Kind::KIND_UINT8, e)); |
| 303 | }); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 304 | out << "} /* "; |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 305 | out << iface->getFileHash()->hexString(); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 306 | out << " */"; |
| 307 | }); |
| 308 | } |
| 309 | |
| 310 | bool Interface::fillHashChainMethod(Method *method) const { |
| 311 | if (method->name() != "getHashChain") { |
| 312 | return false; |
| 313 | } |
| 314 | const VectorType *chainType = static_cast<const VectorType *>(&method->results()[0]->type()); |
| 315 | const ArrayType *digestType = static_cast<const ArrayType *>(chainType->getElementType()); |
| 316 | |
| 317 | method->fillImplementation( |
| 318 | HIDL_HASH_CHAIN_TRANSACTION, |
| 319 | { { IMPL_INTERFACE, [this, digestType](auto &out) { |
| 320 | std::vector<const Interface *> chain = typeChain(); |
| 321 | out << "_hidl_cb("; |
| 322 | out.block([&] { |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 323 | emitDigestChain(out, "(" + digestType->getInternalDataCppType() + ")", chain, |
| 324 | [](const auto& e) { return e->cppValue(); }); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 325 | }); |
| 326 | out << ");\n"; |
| 327 | out << "return ::android::hardware::Void();\n"; |
| 328 | } } }, /* cppImpl */ |
| 329 | { { IMPL_INTERFACE, [this, digestType, chainType](auto &out) { |
| 330 | std::vector<const Interface *> chain = typeChain(); |
| 331 | out << "return new " |
| 332 | << chainType->getJavaType(false /* forInitializer */) |
| 333 | << "(java.util.Arrays.asList(\n"; |
| 334 | out.indent(2, [&] { |
| 335 | // No need for dimensions when elements are explicitly provided. |
| 336 | emitDigestChain(out, "new " + digestType->getJavaType(false /* forInitializer */), |
Timur Iskhakov | 7296af1 | 2017-08-09 21:52:48 +0000 | [diff] [blame] | 337 | chain, [](const auto& e) { return e->javaValue(); }); |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 338 | }); |
| 339 | out << "));\n"; |
| 340 | } } } /* javaImpl */ |
| 341 | ); |
| 342 | return true; |
| 343 | } |
| 344 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 345 | bool Interface::fillGetDescriptorMethod(Method *method) const { |
| 346 | if (method->name() != "interfaceDescriptor") { |
| 347 | return false; |
| 348 | } |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 349 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 350 | method->fillImplementation( |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 351 | HIDL_GET_DESCRIPTOR_TRANSACTION, |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 352 | { { IMPL_INTERFACE, [this](auto &out) { |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 353 | out << "_hidl_cb(" |
| 354 | << fullName() |
| 355 | << "::descriptor);\n" |
| 356 | << "return ::android::hardware::Void();"; |
| 357 | } } }, /* cppImpl */ |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 358 | { { IMPL_INTERFACE, [this](auto &out) { |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 359 | out << "return " |
| 360 | << fullJavaName() |
| 361 | << ".kInterfaceName;\n"; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 362 | } } } /* javaImpl */ |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 363 | ); |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 364 | return true; |
Yifan Hong | c75fd47 | 2017-01-11 12:37:31 -0800 | [diff] [blame] | 365 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 366 | |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 367 | bool Interface::fillGetDebugInfoMethod(Method *method) const { |
| 368 | if (method->name() != "getDebugInfo") { |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 369 | return false; |
| 370 | } |
| 371 | |
Yifan Hong | df1ea3f | 2017-03-02 17:02:10 -0800 | [diff] [blame] | 372 | static const std::string sArch = |
| 373 | "#if defined(__LP64__)\n" |
| 374 | "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_64BIT\n" |
| 375 | "#else\n" |
| 376 | "::android::hidl::base::V1_0::DebugInfo::Architecture::IS_32BIT\n" |
| 377 | "#endif\n"; |
| 378 | |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 379 | method->fillImplementation( |
| 380 | HIDL_GET_REF_INFO_TRANSACTION, |
| 381 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 382 | {IMPL_INTERFACE, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 383 | [](auto &out) { |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 384 | // getDebugInfo returns N/A for local objects. |
Yifan Hong | df1ea3f | 2017-03-02 17:02:10 -0800 | [diff] [blame] | 385 | out << "_hidl_cb({ -1 /* pid */, 0 /* ptr */, \n" |
| 386 | << sArch |
| 387 | << "});\n" |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 388 | << "return ::android::hardware::Void();"; |
| 389 | } |
| 390 | }, |
| 391 | {IMPL_STUB_IMPL, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 392 | [](auto &out) { |
Yifan Hong | 2e036c9 | 2017-03-07 13:18:12 -0800 | [diff] [blame] | 393 | out << "_hidl_cb("; |
| 394 | out.block([&] { |
Yifan Hong | 96bb063 | 2017-11-14 16:08:37 -0800 | [diff] [blame] | 395 | out << "::android::hardware::details::getPidIfSharable(),\n" |
Yifan Hong | 2e036c9 | 2017-03-07 13:18:12 -0800 | [diff] [blame] | 396 | << "::android::hardware::details::debuggable()" |
| 397 | << "? reinterpret_cast<uint64_t>(this) : 0 /* ptr */,\n" |
| 398 | << sArch << "\n"; |
| 399 | }); |
| 400 | out << ");\n" |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 401 | << "return ::android::hardware::Void();"; |
| 402 | } |
| 403 | } |
| 404 | }, /* cppImpl */ |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 405 | { { IMPL_INTERFACE, [method](auto &out) { |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 406 | const Type &refInfo = method->results().front()->type(); |
| 407 | out << refInfo.getJavaType(false /* forInitializer */) << " info = new " |
| 408 | << refInfo.getJavaType(true /* forInitializer */) << "();\n" |
Yifan Hong | 96bb063 | 2017-11-14 16:08:37 -0800 | [diff] [blame] | 409 | << "info.pid = android.os.HidlSupport.getPidIfSharable();\n" |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 410 | << "info.ptr = 0;\n" |
Yifan Hong | 96bb063 | 2017-11-14 16:08:37 -0800 | [diff] [blame] | 411 | << "info.arch = android.hidl.base.V1_0.DebugInfo.Architecture.UNKNOWN;\n" |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 412 | << "return info;"; |
| 413 | } } } /* javaImpl */ |
| 414 | ); |
| 415 | |
| 416 | return true; |
| 417 | } |
| 418 | |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 419 | bool Interface::fillDebugMethod(Method *method) const { |
| 420 | if (method->name() != "debug") { |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | method->fillImplementation( |
| 425 | HIDL_DEBUG_TRANSACTION, |
| 426 | { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 427 | {IMPL_INTERFACE, |
Yi Kong | c8ff467 | 2017-04-30 23:46:56 -0700 | [diff] [blame] | 428 | [](auto &out) { |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 429 | out << "(void)fd;\n" |
| 430 | << "(void)options;\n" |
| 431 | << "return ::android::hardware::Void();"; |
| 432 | } |
| 433 | }, |
| 434 | }, /* cppImpl */ |
| 435 | { |
| 436 | /* unused, as the debug method is hidden from Java */ |
| 437 | } /* javaImpl */ |
| 438 | ); |
| 439 | |
| 440 | return true; |
| 441 | } |
| 442 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 443 | static std::map<std::string, Method *> gAllReservedMethods; |
| 444 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 445 | bool Interface::addMethod(Method *method) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 446 | if (isIBase()) { |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 447 | if (!gAllReservedMethods.emplace(method->name(), method).second) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 448 | std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name() |
| 449 | << std::endl; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 450 | return false; |
| 451 | } |
| 452 | // will add it in addAllReservedMethods |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 453 | return true; |
| 454 | } |
| 455 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 456 | CHECK(!method->isHidlReserved()); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 457 | mUserMethods.push_back(method); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 458 | |
| 459 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 462 | std::vector<const Reference<Type>*> Interface::getReferences() const { |
| 463 | std::vector<const Reference<Type>*> ret; |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 464 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 465 | if (!isIBase()) { |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 466 | ret.push_back(&mSuperType); |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | for (const auto* method : methods()) { |
| 470 | const auto& references = method->getReferences(); |
| 471 | ret.insert(ret.end(), references.begin(), references.end()); |
| 472 | } |
| 473 | |
| 474 | return ret; |
| 475 | } |
| 476 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 477 | std::vector<const ConstantExpression*> Interface::getConstantExpressions() const { |
| 478 | std::vector<const ConstantExpression*> ret; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 479 | for (const auto* method : methods()) { |
| 480 | const auto& retMethod = method->getConstantExpressions(); |
| 481 | ret.insert(ret.end(), retMethod.begin(), retMethod.end()); |
| 482 | } |
| 483 | return ret; |
| 484 | } |
| 485 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 486 | std::vector<const Reference<Type>*> Interface::getStrongReferences() const { |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 487 | // Interface is a special case as a reference: |
| 488 | // its definiton must be completed for extension but |
| 489 | // not necessary for other references. |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 490 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 491 | std::vector<const Reference<Type>*> ret; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 492 | if (!isIBase()) { |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 493 | ret.push_back(&mSuperType); |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 494 | } |
Timur Iskhakov | ff5e64a | 2017-09-11 14:56:18 -0700 | [diff] [blame] | 495 | |
| 496 | for (const auto* method : methods()) { |
| 497 | const auto& references = method->getStrongReferences(); |
| 498 | ret.insert(ret.end(), references.begin(), references.end()); |
| 499 | } |
| 500 | |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 501 | return ret; |
| 502 | } |
| 503 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 504 | status_t Interface::resolveInheritance() { |
| 505 | size_t serial = FIRST_CALL_TRANSACTION; |
| 506 | for (const auto* ancestor : superTypeChain()) { |
| 507 | serial += ancestor->mUserMethods.size(); |
| 508 | } |
| 509 | |
| 510 | for (Method* method : mUserMethods) { |
| 511 | if (serial > LAST_CALL_TRANSACTION) { |
| 512 | std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION |
| 513 | << " methods (including super and reserved) are not allowed at " << location() |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 514 | << std::endl; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 515 | return UNKNOWN_ERROR; |
| 516 | } |
| 517 | |
| 518 | method->setSerialId(serial); |
| 519 | serial++; |
| 520 | } |
| 521 | |
| 522 | return Scope::resolveInheritance(); |
| 523 | } |
| 524 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 525 | status_t Interface::validate() const { |
| 526 | CHECK(isIBase() == mSuperType.isEmptyReference()); |
| 527 | |
Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 528 | if (!isIBase() && !mSuperType->isInterface()) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 529 | std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location() |
| 530 | << std::endl; |
Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 531 | return UNKNOWN_ERROR; |
| 532 | } |
| 533 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 534 | status_t err; |
| 535 | |
| 536 | err = validateUniqueNames(); |
| 537 | if (err != OK) return err; |
| 538 | |
| 539 | err = validateAnnotations(); |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 540 | if (err != OK) return err; |
| 541 | |
| 542 | return Scope::validate(); |
| 543 | } |
| 544 | |
Howard Chen | ecfb451 | 2017-11-21 18:28:53 +0800 | [diff] [blame] | 545 | void Interface::getAlignmentAndSize(size_t* align, size_t* size) const { |
| 546 | *align = 8; |
| 547 | *size = 8; |
| 548 | } |
| 549 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 550 | status_t Interface::validateUniqueNames() const { |
| 551 | std::unordered_map<std::string, const Interface*> registeredMethodNames; |
| 552 | for (auto const& tuple : allSuperMethodsFromRoot()) { |
| 553 | // No need to check super method uniqueness |
| 554 | registeredMethodNames[tuple.method()->name()] = tuple.interface(); |
| 555 | } |
| 556 | |
| 557 | for (const Method* method : mUserMethods) { |
| 558 | auto registered = registeredMethodNames.find(method->name()); |
| 559 | |
| 560 | if (registered != registeredMethodNames.end()) { |
| 561 | const Interface* definedInType = registered->second; |
| 562 | |
| 563 | if (definedInType == this) { |
| 564 | // Defined in this interface |
| 565 | std::cerr << "ERROR: Redefinition of method '" << method->name() << "'"; |
| 566 | } else if (definedInType->isIBase()) { |
| 567 | // Defined in IBase |
| 568 | std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'"; |
| 569 | } else { |
| 570 | // Defined in super not IBase |
| 571 | std::cerr << "ERROR: Redefinition of method '" << method->name() |
Timur Iskhakov | 4c9c20b | 2017-09-13 21:16:59 -0700 | [diff] [blame] | 572 | << "' defined in interface '" << definedInType->fullName() << "'"; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 573 | } |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 574 | std::cerr << " at " << method->location() << std::endl; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 575 | return UNKNOWN_ERROR; |
| 576 | } |
| 577 | |
| 578 | registeredMethodNames[method->name()] = this; |
| 579 | } |
| 580 | |
| 581 | return OK; |
| 582 | } |
| 583 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 584 | status_t Interface::validateAnnotations() const { |
| 585 | for (const Method* method : methods()) { |
| 586 | for (const Annotation* annotation : method->annotations()) { |
| 587 | const std::string name = annotation->name(); |
| 588 | |
| 589 | if (name == "entry" || name == "exit" || name == "callflow") { |
| 590 | continue; |
| 591 | } |
| 592 | |
| 593 | std::cerr << "ERROR: Unrecognized annotation '" << name |
| 594 | << "' for method: " << method->name() << ". An annotation should be one of: " |
| 595 | << "entry, exit, callflow." << std::endl; |
| 596 | return UNKNOWN_ERROR; |
| 597 | } |
| 598 | } |
| 599 | return OK; |
| 600 | } |
| 601 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 602 | bool Interface::addAllReservedMethods() { |
| 603 | // use a sorted map to insert them in serial ID order. |
| 604 | std::map<int32_t, Method *> reservedMethodsById; |
| 605 | for (const auto &pair : gAllReservedMethods) { |
| 606 | Method *method = pair.second->copySignature(); |
Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 607 | bool fillSuccess = fillPingMethod(method) |
| 608 | || fillDescriptorChainMethod(method) |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 609 | || fillGetDescriptorMethod(method) |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 610 | || fillHashChainMethod(method) |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 611 | || fillSyspropsChangedMethod(method) |
| 612 | || fillLinkToDeathMethod(method) |
| 613 | || fillUnlinkToDeathMethod(method) |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 614 | || fillSetHALInstrumentationMethod(method) |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 615 | || fillGetDebugInfoMethod(method) |
| 616 | || fillDebugMethod(method); |
| 617 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 618 | if (!fillSuccess) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 619 | std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name() |
| 620 | << std::endl; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | if (!reservedMethodsById.emplace(method->getSerialId(), method).second) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 624 | std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name() |
| 625 | << " and " << reservedMethodsById[method->getSerialId()]->name() |
| 626 | << ", serialId = " << method->getSerialId() << std::endl; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 627 | return false; |
| 628 | } |
| 629 | } |
| 630 | for (const auto &pair : reservedMethodsById) { |
| 631 | this->mReservedMethods.push_back(pair.second); |
| 632 | } |
| 633 | return true; |
| 634 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 635 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 636 | const Interface* Interface::superType() const { |
Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 637 | if (isIBase()) return nullptr; |
| 638 | if (!mSuperType->isInterface()) { |
| 639 | // This is actually an error |
| 640 | // that would be caught in validate |
| 641 | return nullptr; |
| 642 | } |
Timur Iskhakov | 24e605b | 2017-08-30 14:02:55 -0700 | [diff] [blame] | 643 | return static_cast<const Interface*>(mSuperType.get()); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 644 | } |
| 645 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 646 | std::vector<const Interface *> Interface::typeChain() const { |
| 647 | std::vector<const Interface *> v; |
| 648 | const Interface *iface = this; |
| 649 | while (iface != nullptr) { |
| 650 | v.push_back(iface); |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 651 | iface = iface->superType(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 652 | } |
| 653 | return v; |
| 654 | } |
| 655 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 656 | std::vector<const Interface *> Interface::superTypeChain() const { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 657 | return isIBase() ? std::vector<const Interface*>() : superType()->typeChain(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Martijn Coenen | b40ef02 | 2017-01-02 15:21:46 +0100 | [diff] [blame] | 660 | bool Interface::isElidableType() const { |
| 661 | return true; |
| 662 | } |
| 663 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 664 | bool Interface::isInterface() const { |
| 665 | return true; |
| 666 | } |
| 667 | |
Andreas Huber | 295ad30 | 2016-08-16 11:35:00 -0700 | [diff] [blame] | 668 | bool Interface::isBinder() const { |
| 669 | return true; |
| 670 | } |
| 671 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 672 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 673 | return mUserMethods; |
| 674 | } |
| 675 | |
| 676 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 677 | return mReservedMethods; |
| 678 | } |
| 679 | |
| 680 | std::vector<Method *> Interface::methods() const { |
| 681 | std::vector<Method *> v(mUserMethods); |
| 682 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 683 | return v; |
| 684 | } |
| 685 | |
| 686 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 687 | std::vector<InterfaceAndMethod> v; |
| 688 | std::vector<const Interface *> chain = typeChain(); |
| 689 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 690 | const Interface *iface = *it; |
| 691 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 692 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 693 | } |
| 694 | } |
| 695 | for (Method *reservedMethod : hidlReservedMethods()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 696 | v.push_back(InterfaceAndMethod( |
| 697 | *chain.rbegin(), // IBase |
| 698 | reservedMethod)); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 699 | } |
| 700 | return v; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 701 | } |
| 702 | |
Timur Iskhakov | f1b902d | 2017-08-13 20:14:31 -0700 | [diff] [blame] | 703 | std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const { |
| 704 | return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot(); |
| 705 | } |
| 706 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 707 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 708 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 711 | std::string Interface::getAdapterName() const { |
| 712 | return fqName().getInterfaceAdapterName(); |
| 713 | } |
| 714 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 715 | std::string Interface::getProxyName() const { |
| 716 | return fqName().getInterfaceProxyName(); |
| 717 | } |
| 718 | |
| 719 | std::string Interface::getStubName() const { |
| 720 | return fqName().getInterfaceStubName(); |
| 721 | } |
| 722 | |
| 723 | std::string Interface::getHwName() const { |
| 724 | return fqName().getInterfaceHwName(); |
| 725 | } |
| 726 | |
| 727 | std::string Interface::getPassthroughName() const { |
| 728 | return fqName().getInterfacePassthroughName(); |
| 729 | } |
| 730 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 731 | FQName Interface::getProxyFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 732 | return fqName().getInterfaceProxyFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 735 | FQName Interface::getStubFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 736 | return fqName().getInterfaceStubFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 739 | FQName Interface::getPassthroughFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 740 | return fqName().getInterfacePassthroughFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 743 | std::string Interface::getCppType(StorageMode mode, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 744 | bool specifyNamespaces) const { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 745 | const std::string base = |
| 746 | std::string(specifyNamespaces ? "::android::" : "") |
| 747 | + "sp<" |
Steven Moreland | e30ee9b | 2017-05-09 13:31:01 -0700 | [diff] [blame] | 748 | + fullName() |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 749 | + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 750 | |
| 751 | switch (mode) { |
| 752 | case StorageMode_Stack: |
| 753 | case StorageMode_Result: |
| 754 | return base; |
| 755 | |
| 756 | case StorageMode_Argument: |
| 757 | return "const " + base + "&"; |
| 758 | } |
| 759 | } |
| 760 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 761 | std::string Interface::getJavaType(bool /* forInitializer */) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 762 | return fullJavaName(); |
| 763 | } |
| 764 | |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 765 | std::string Interface::getVtsType() const { |
| 766 | if (StringHelper::EndsWith(localName(), "Callback")) { |
| 767 | return "TYPE_HIDL_CALLBACK"; |
| 768 | } else { |
| 769 | return "TYPE_HIDL_INTERFACE"; |
| 770 | } |
| 771 | } |
| 772 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 773 | void Interface::emitReaderWriter( |
| 774 | Formatter &out, |
| 775 | const std::string &name, |
| 776 | const std::string &parcelObj, |
| 777 | bool parcelObjIsPointer, |
| 778 | bool isReader, |
| 779 | ErrorMode mode) const { |
| 780 | const std::string parcelObjDeref = |
| 781 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 782 | |
| 783 | if (isReader) { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 784 | out << "{\n"; |
| 785 | out.indent(); |
| 786 | |
Howard Chen | ecfb451 | 2017-11-21 18:28:53 +0800 | [diff] [blame] | 787 | const std::string binderName = "_hidl_binder"; |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 788 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 789 | << binderName << ";\n"; |
| 790 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 791 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 792 | out << parcelObjDeref |
| 793 | << "readNullableStrongBinder(&" |
| 794 | << binderName |
| 795 | << ");\n"; |
| 796 | |
| 797 | handleError(out, mode); |
| 798 | |
| 799 | out << name |
| 800 | << " = " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 801 | << "::android::hardware::fromBinder<" |
| 802 | << fqName().cppName() |
| 803 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 804 | << getProxyFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 805 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 806 | << getStubFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 807 | << ">(" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 808 | << binderName |
| 809 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 810 | |
| 811 | out.unindent(); |
| 812 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 813 | } else { |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 814 | out << "if (" << name << " == nullptr) {\n"; |
| 815 | out.indent(); |
| 816 | out << "_hidl_err = "; |
| 817 | out << parcelObjDeref |
| 818 | << "writeStrongBinder(nullptr);\n"; |
| 819 | out.unindent(); |
| 820 | out << "} else {\n"; |
| 821 | out.indent(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 822 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
| 823 | << "::android::hardware::toBinder<\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 824 | out.indent(2, [&] { |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 825 | out << fqName().cppName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 826 | << ">(" |
| 827 | << name |
| 828 | << ");\n"; |
| 829 | }); |
| 830 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 831 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 832 | out << "_hidl_err = " |
| 833 | << parcelObjDeref |
| 834 | << "writeStrongBinder(_hidl_binder);\n"; |
| 835 | }); |
| 836 | out << "} else {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 837 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 838 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 839 | }); |
| 840 | out << "}\n"; |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 841 | out.unindent(); |
| 842 | out << "}\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 843 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 844 | handleError(out, mode); |
| 845 | } |
| 846 | } |
| 847 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 848 | void Interface::emitPackageTypeDeclarations(Formatter& out) const { |
| 849 | Scope::emitPackageTypeDeclarations(out); |
Steven Moreland | bf71421 | 2017-10-27 18:29:01 -0700 | [diff] [blame] | 850 | |
| 851 | // TODO(b/65200821): remove these ifndefs |
| 852 | out << "#ifdef REALLY_IS_HIDL_INTERNAL_LIB" << gCurrentCompileName << "\n"; |
Steven Moreland | 3d98bc4 | 2017-06-23 21:36:41 +0000 | [diff] [blame] | 853 | out << "std::string toString(" |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 854 | << getCppArgumentType() |
Steven Moreland | 3d98bc4 | 2017-06-23 21:36:41 +0000 | [diff] [blame] | 855 | << ");\n"; |
Steven Moreland | bf71421 | 2017-10-27 18:29:01 -0700 | [diff] [blame] | 856 | out << "#else\n"; |
| 857 | out << "static inline std::string toString(" << getCppArgumentType() << " o) "; |
| 858 | |
| 859 | out.block([&] { |
| 860 | out << "std::string os = \"[class or subclass of \";\n" |
| 861 | << "os += " << fullName() << "::descriptor;\n" |
| 862 | << "os += \"]\";\n" |
| 863 | << "os += o->isRemote() ? \"@remote\" : \"@local\";\n" |
| 864 | << "return os;\n"; |
| 865 | }).endl().endl(); |
| 866 | out << "#endif // REALLY_IS_HIDL_INTERNAL_LIB\n"; |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 869 | void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const { |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 870 | std::string space = prefix.empty() ? "" : (prefix + "::"); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 871 | Scope::emitTypeDefinitions(out, space + localName()); |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 872 | |
Steven Moreland | bf71421 | 2017-10-27 18:29:01 -0700 | [diff] [blame] | 873 | // TODO(b/65200821): remove toString from .cpp once all prebuilts are rebuilt |
Steven Moreland | 3d98bc4 | 2017-06-23 21:36:41 +0000 | [diff] [blame] | 874 | out << "std::string toString(" |
| 875 | << getCppArgumentType() |
| 876 | << " o) "; |
| 877 | |
| 878 | out.block([&] { |
| 879 | out << "std::string os = \"[class or subclass of \";\n" |
| 880 | << "os += " << fullName() << "::descriptor;\n" |
| 881 | << "os += \"]\";\n" |
| 882 | << "os += o->isRemote() ? \"@remote\" : \"@local\";\n" |
| 883 | << "return os;\n"; |
| 884 | }).endl().endl(); |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 887 | void Interface::emitJavaReaderWriter( |
| 888 | Formatter &out, |
| 889 | const std::string &parcelObj, |
| 890 | const std::string &argName, |
| 891 | bool isReader) const { |
| 892 | if (isReader) { |
| 893 | out << fullJavaName() |
| 894 | << ".asInterface(" |
| 895 | << parcelObj |
| 896 | << ".readStrongBinder());\n"; |
| 897 | } else { |
| 898 | out << parcelObj |
| 899 | << ".writeStrongBinder(" |
| 900 | << argName |
| 901 | << " == null ? null : " |
| 902 | << argName |
| 903 | << ".asBinder());\n"; |
| 904 | } |
| 905 | } |
| 906 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 907 | void Interface::emitVtsAttributeDeclaration(Formatter& out) const { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 908 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 909 | // Skip for TypeDef as it is just an alias of a defined type. |
| 910 | if (type->isTypeDef()) { |
| 911 | continue; |
| 912 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 913 | out << "attribute: {\n"; |
| 914 | out.indent(); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 915 | type->emitVtsTypeDeclarations(out); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 916 | out.unindent(); |
| 917 | out << "}\n\n"; |
| 918 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 921 | void Interface::emitVtsMethodDeclaration(Formatter& out) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 922 | for (const auto &method : methods()) { |
Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 923 | if (method->isHidlReserved()) { |
| 924 | continue; |
| 925 | } |
| 926 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 927 | out << "api: {\n"; |
| 928 | out.indent(); |
| 929 | out << "name: \"" << method->name() << "\"\n"; |
| 930 | // Generate declaration for each return value. |
| 931 | for (const auto &result : method->results()) { |
| 932 | out << "return_type_hidl: {\n"; |
| 933 | out.indent(); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 934 | result->type().emitVtsAttributeType(out); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 935 | out.unindent(); |
| 936 | out << "}\n"; |
| 937 | } |
| 938 | // Generate declaration for each input argument |
| 939 | for (const auto &arg : method->args()) { |
| 940 | out << "arg: {\n"; |
| 941 | out.indent(); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 942 | arg->type().emitVtsAttributeType(out); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 943 | out.unindent(); |
| 944 | out << "}\n"; |
| 945 | } |
| 946 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 947 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 948 | out << "callflow: {\n"; |
| 949 | out.indent(); |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 950 | const std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 951 | if (name == "entry") { |
| 952 | out << "entry: true\n"; |
| 953 | } else if (name == "exit") { |
| 954 | out << "exit: true\n"; |
| 955 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 956 | const AnnotationParam *param = |
| 957 | annotation->getParam("next"); |
| 958 | if (param != nullptr) { |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 959 | for (const auto& value : param->getValues()) { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 960 | out << "next: " << value << "\n"; |
| 961 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 962 | } |
| 963 | } else { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 964 | CHECK(false); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 965 | } |
| 966 | out.unindent(); |
| 967 | out << "}\n"; |
| 968 | } |
| 969 | out.unindent(); |
| 970 | out << "}\n\n"; |
| 971 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 972 | } |
| 973 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 974 | void Interface::emitVtsAttributeType(Formatter& out) const { |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 975 | out << "type: " << getVtsType() << "\n" |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 976 | << "predefined_type: \"" |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 977 | << fullName() |
| 978 | << "\"\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 979 | } |
| 980 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 981 | bool Interface::hasOnewayMethods() const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 982 | for (auto const &method : methods()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 983 | if (method->isOneway()) { |
| 984 | return true; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | const Interface* superClass = superType(); |
| 989 | |
| 990 | if (superClass != nullptr) { |
| 991 | return superClass->hasOnewayMethods(); |
| 992 | } |
| 993 | |
| 994 | return false; |
| 995 | } |
| 996 | |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 997 | bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const { |
| 998 | if (superType() != nullptr && !superType()->isJavaCompatible(visited)) { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 999 | return false; |
| 1000 | } |
| 1001 | |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 1002 | for (const auto* method : methods()) { |
| 1003 | if (!method->deepIsJavaCompatible(visited)) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 1004 | return false; |
| 1005 | } |
| 1006 | } |
| 1007 | |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 1008 | return Scope::isJavaCompatible(visited); |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Timur Iskhakov | ff5e64a | 2017-09-11 14:56:18 -0700 | [diff] [blame] | 1011 | bool Interface::isNeverStrongReference() const { |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1015 | } // namespace android |
| 1016 | |