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