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"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 276 | out << "return ::android::hardware::Void();"; |
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 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 444 | static std::map<std::string, Method *> gAllReservedMethods; |
| 445 | |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 446 | bool Interface::addMethod(Method *method) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 447 | if (isIBase()) { |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 448 | if (!gAllReservedMethods.emplace(method->name(), method).second) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 449 | std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name() |
| 450 | << std::endl; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 451 | return false; |
| 452 | } |
| 453 | // will add it in addAllReservedMethods |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 454 | return true; |
| 455 | } |
| 456 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 457 | CHECK(!method->isHidlReserved()); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 458 | mUserMethods.push_back(method); |
Steven Moreland | 14ee674 | 2016-10-18 12:58:28 -0700 | [diff] [blame] | 459 | |
| 460 | return true; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 463 | std::vector<const Reference<Type>*> Interface::getReferences() const { |
| 464 | std::vector<const Reference<Type>*> ret; |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 465 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 466 | if (!isIBase()) { |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 467 | ret.push_back(&mSuperType); |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | for (const auto* method : methods()) { |
| 471 | const auto& references = method->getReferences(); |
| 472 | ret.insert(ret.end(), references.begin(), references.end()); |
| 473 | } |
| 474 | |
| 475 | return ret; |
| 476 | } |
| 477 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 478 | std::vector<const ConstantExpression*> Interface::getConstantExpressions() const { |
| 479 | std::vector<const ConstantExpression*> ret; |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 480 | for (const auto* method : methods()) { |
| 481 | const auto& retMethod = method->getConstantExpressions(); |
| 482 | ret.insert(ret.end(), retMethod.begin(), retMethod.end()); |
| 483 | } |
| 484 | return ret; |
| 485 | } |
| 486 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 487 | std::vector<const Reference<Type>*> Interface::getStrongReferences() const { |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 488 | // Interface is a special case as a reference: |
| 489 | // its definiton must be completed for extension but |
| 490 | // not necessary for other references. |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 491 | |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 492 | std::vector<const Reference<Type>*> ret; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 493 | if (!isIBase()) { |
Timur Iskhakov | b58f418 | 2017-08-29 15:19:24 -0700 | [diff] [blame] | 494 | ret.push_back(&mSuperType); |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 495 | } |
Timur Iskhakov | ff5e64a | 2017-09-11 14:56:18 -0700 | [diff] [blame] | 496 | |
| 497 | for (const auto* method : methods()) { |
| 498 | const auto& references = method->getStrongReferences(); |
| 499 | ret.insert(ret.end(), references.begin(), references.end()); |
| 500 | } |
| 501 | |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 502 | return ret; |
| 503 | } |
| 504 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 505 | status_t Interface::resolveInheritance() { |
| 506 | size_t serial = FIRST_CALL_TRANSACTION; |
| 507 | for (const auto* ancestor : superTypeChain()) { |
| 508 | serial += ancestor->mUserMethods.size(); |
| 509 | } |
| 510 | |
| 511 | for (Method* method : mUserMethods) { |
| 512 | if (serial > LAST_CALL_TRANSACTION) { |
| 513 | std::cerr << "ERROR: More than " << LAST_CALL_TRANSACTION |
| 514 | << " methods (including super and reserved) are not allowed at " << location() |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 515 | << std::endl; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 516 | return UNKNOWN_ERROR; |
| 517 | } |
| 518 | |
| 519 | method->setSerialId(serial); |
| 520 | serial++; |
| 521 | } |
| 522 | |
| 523 | return Scope::resolveInheritance(); |
| 524 | } |
| 525 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 526 | status_t Interface::validate() const { |
| 527 | CHECK(isIBase() == mSuperType.isEmptyReference()); |
| 528 | |
Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 529 | if (!isIBase() && !mSuperType->isInterface()) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 530 | std::cerr << "ERROR: You can only extend interfaces at " << mSuperType.location() |
| 531 | << std::endl; |
Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 532 | return UNKNOWN_ERROR; |
| 533 | } |
| 534 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 535 | status_t err; |
| 536 | |
| 537 | err = validateUniqueNames(); |
| 538 | if (err != OK) return err; |
| 539 | |
| 540 | err = validateAnnotations(); |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 541 | if (err != OK) return err; |
| 542 | |
| 543 | return Scope::validate(); |
| 544 | } |
| 545 | |
Howard Chen | ecfb451 | 2017-11-21 18:28:53 +0800 | [diff] [blame] | 546 | void Interface::getAlignmentAndSize(size_t* align, size_t* size) const { |
| 547 | *align = 8; |
| 548 | *size = 8; |
| 549 | } |
| 550 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 551 | status_t Interface::validateUniqueNames() const { |
| 552 | std::unordered_map<std::string, const Interface*> registeredMethodNames; |
| 553 | for (auto const& tuple : allSuperMethodsFromRoot()) { |
| 554 | // No need to check super method uniqueness |
| 555 | registeredMethodNames[tuple.method()->name()] = tuple.interface(); |
| 556 | } |
| 557 | |
| 558 | for (const Method* method : mUserMethods) { |
| 559 | auto registered = registeredMethodNames.find(method->name()); |
| 560 | |
| 561 | if (registered != registeredMethodNames.end()) { |
| 562 | const Interface* definedInType = registered->second; |
| 563 | |
| 564 | if (definedInType == this) { |
| 565 | // Defined in this interface |
| 566 | std::cerr << "ERROR: Redefinition of method '" << method->name() << "'"; |
| 567 | } else if (definedInType->isIBase()) { |
| 568 | // Defined in IBase |
| 569 | std::cerr << "ERROR: Redefinition of reserved method '" << method->name() << "'"; |
| 570 | } else { |
| 571 | // Defined in super not IBase |
| 572 | std::cerr << "ERROR: Redefinition of method '" << method->name() |
Timur Iskhakov | 4c9c20b | 2017-09-13 21:16:59 -0700 | [diff] [blame] | 573 | << "' defined in interface '" << definedInType->fullName() << "'"; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 574 | } |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 575 | std::cerr << " at " << method->location() << std::endl; |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 576 | return UNKNOWN_ERROR; |
| 577 | } |
| 578 | |
| 579 | registeredMethodNames[method->name()] = this; |
| 580 | } |
| 581 | |
| 582 | return OK; |
| 583 | } |
| 584 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 585 | status_t Interface::validateAnnotations() const { |
| 586 | for (const Method* method : methods()) { |
| 587 | for (const Annotation* annotation : method->annotations()) { |
| 588 | const std::string name = annotation->name(); |
| 589 | |
| 590 | if (name == "entry" || name == "exit" || name == "callflow") { |
| 591 | continue; |
| 592 | } |
| 593 | |
| 594 | std::cerr << "ERROR: Unrecognized annotation '" << name |
| 595 | << "' for method: " << method->name() << ". An annotation should be one of: " |
| 596 | << "entry, exit, callflow." << std::endl; |
| 597 | return UNKNOWN_ERROR; |
| 598 | } |
| 599 | } |
| 600 | return OK; |
| 601 | } |
| 602 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 603 | bool Interface::addAllReservedMethods() { |
| 604 | // use a sorted map to insert them in serial ID order. |
| 605 | std::map<int32_t, Method *> reservedMethodsById; |
| 606 | for (const auto &pair : gAllReservedMethods) { |
| 607 | Method *method = pair.second->copySignature(); |
Steven Moreland | 424a948 | 2017-02-13 19:20:40 -0800 | [diff] [blame] | 608 | bool fillSuccess = fillPingMethod(method) |
| 609 | || fillDescriptorChainMethod(method) |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 610 | || fillGetDescriptorMethod(method) |
Yifan Hong | 30b5d1f | 2017-04-03 12:19:25 -0700 | [diff] [blame] | 611 | || fillHashChainMethod(method) |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 612 | || fillSyspropsChangedMethod(method) |
| 613 | || fillLinkToDeathMethod(method) |
| 614 | || fillUnlinkToDeathMethod(method) |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 615 | || fillSetHALInstrumentationMethod(method) |
Andreas Huber | 37065d6 | 2017-02-07 14:36:54 -0800 | [diff] [blame] | 616 | || fillGetDebugInfoMethod(method) |
| 617 | || fillDebugMethod(method); |
| 618 | |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 619 | if (!fillSuccess) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 620 | std::cerr << "ERROR: hidl-gen does not recognize a reserved method " << method->name() |
| 621 | << std::endl; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 622 | return false; |
| 623 | } |
| 624 | if (!reservedMethodsById.emplace(method->getSerialId(), method).second) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 625 | std::cerr << "ERROR: hidl-gen uses duplicated serial id for " << method->name() |
| 626 | << " and " << reservedMethodsById[method->getSerialId()]->name() |
| 627 | << ", serialId = " << method->getSerialId() << std::endl; |
Yifan Hong | ffa9139 | 2017-01-31 13:41:23 -0800 | [diff] [blame] | 628 | return false; |
| 629 | } |
| 630 | } |
| 631 | for (const auto &pair : reservedMethodsById) { |
| 632 | this->mReservedMethods.push_back(pair.second); |
| 633 | } |
| 634 | return true; |
| 635 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 636 | |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 637 | const Interface* Interface::superType() const { |
Timur Iskhakov | 0344e61 | 2017-08-25 12:57:09 -0700 | [diff] [blame] | 638 | if (isIBase()) return nullptr; |
| 639 | if (!mSuperType->isInterface()) { |
| 640 | // This is actually an error |
| 641 | // that would be caught in validate |
| 642 | return nullptr; |
| 643 | } |
Timur Iskhakov | 24e605b | 2017-08-30 14:02:55 -0700 | [diff] [blame] | 644 | return static_cast<const Interface*>(mSuperType.get()); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 647 | std::vector<const Interface *> Interface::typeChain() const { |
| 648 | std::vector<const Interface *> v; |
| 649 | const Interface *iface = this; |
| 650 | while (iface != nullptr) { |
| 651 | v.push_back(iface); |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 652 | iface = iface->superType(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 653 | } |
| 654 | return v; |
| 655 | } |
| 656 | |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 657 | std::vector<const Interface *> Interface::superTypeChain() const { |
Timur Iskhakov | 505316c | 2017-08-05 03:38:59 +0000 | [diff] [blame] | 658 | return isIBase() ? std::vector<const Interface*>() : superType()->typeChain(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 659 | } |
| 660 | |
Martijn Coenen | b40ef02 | 2017-01-02 15:21:46 +0100 | [diff] [blame] | 661 | bool Interface::isElidableType() const { |
| 662 | return true; |
| 663 | } |
| 664 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 665 | bool Interface::isInterface() const { |
| 666 | return true; |
| 667 | } |
| 668 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 669 | const std::vector<Method *> &Interface::userDefinedMethods() const { |
| 670 | return mUserMethods; |
| 671 | } |
| 672 | |
| 673 | const std::vector<Method *> &Interface::hidlReservedMethods() const { |
| 674 | return mReservedMethods; |
| 675 | } |
| 676 | |
| 677 | std::vector<Method *> Interface::methods() const { |
| 678 | std::vector<Method *> v(mUserMethods); |
| 679 | v.insert(v.end(), mReservedMethods.begin(), mReservedMethods.end()); |
| 680 | return v; |
| 681 | } |
| 682 | |
| 683 | std::vector<InterfaceAndMethod> Interface::allMethodsFromRoot() const { |
| 684 | std::vector<InterfaceAndMethod> v; |
| 685 | std::vector<const Interface *> chain = typeChain(); |
| 686 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 687 | const Interface *iface = *it; |
| 688 | for (Method *userMethod : iface->userDefinedMethods()) { |
| 689 | v.push_back(InterfaceAndMethod(iface, userMethod)); |
| 690 | } |
| 691 | } |
| 692 | for (Method *reservedMethod : hidlReservedMethods()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 693 | v.push_back(InterfaceAndMethod( |
| 694 | *chain.rbegin(), // IBase |
| 695 | reservedMethod)); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 696 | } |
| 697 | return v; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Timur Iskhakov | f1b902d | 2017-08-13 20:14:31 -0700 | [diff] [blame] | 700 | std::vector<InterfaceAndMethod> Interface::allSuperMethodsFromRoot() const { |
| 701 | return isIBase() ? std::vector<InterfaceAndMethod>() : superType()->allMethodsFromRoot(); |
| 702 | } |
| 703 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 704 | std::string Interface::getBaseName() const { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 705 | return fqName().getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 708 | std::string Interface::getAdapterName() const { |
| 709 | return fqName().getInterfaceAdapterName(); |
| 710 | } |
| 711 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 712 | std::string Interface::getProxyName() const { |
| 713 | return fqName().getInterfaceProxyName(); |
| 714 | } |
| 715 | |
| 716 | std::string Interface::getStubName() const { |
| 717 | return fqName().getInterfaceStubName(); |
| 718 | } |
| 719 | |
| 720 | std::string Interface::getHwName() const { |
| 721 | return fqName().getInterfaceHwName(); |
| 722 | } |
| 723 | |
| 724 | std::string Interface::getPassthroughName() const { |
| 725 | return fqName().getInterfacePassthroughName(); |
| 726 | } |
| 727 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 728 | FQName Interface::getProxyFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 729 | return fqName().getInterfaceProxyFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 730 | } |
| 731 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 732 | FQName Interface::getStubFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 733 | return fqName().getInterfaceStubFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 734 | } |
| 735 | |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 736 | FQName Interface::getPassthroughFqName() const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 737 | return fqName().getInterfacePassthroughFqName(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 738 | } |
| 739 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 740 | std::string Interface::getCppType(StorageMode mode, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 741 | bool specifyNamespaces) const { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 742 | const std::string base = |
| 743 | std::string(specifyNamespaces ? "::android::" : "") |
| 744 | + "sp<" |
Steven Moreland | e30ee9b | 2017-05-09 13:31:01 -0700 | [diff] [blame] | 745 | + fullName() |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 746 | + ">"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 747 | |
| 748 | switch (mode) { |
| 749 | case StorageMode_Stack: |
| 750 | case StorageMode_Result: |
| 751 | return base; |
| 752 | |
| 753 | case StorageMode_Argument: |
| 754 | return "const " + base + "&"; |
| 755 | } |
| 756 | } |
| 757 | |
Yifan Hong | 4ed1347 | 2016-11-02 10:44:11 -0700 | [diff] [blame] | 758 | std::string Interface::getJavaType(bool /* forInitializer */) const { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 759 | return fullJavaName(); |
| 760 | } |
| 761 | |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 762 | std::string Interface::getVtsType() const { |
| 763 | if (StringHelper::EndsWith(localName(), "Callback")) { |
| 764 | return "TYPE_HIDL_CALLBACK"; |
| 765 | } else { |
| 766 | return "TYPE_HIDL_INTERFACE"; |
| 767 | } |
| 768 | } |
| 769 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 770 | void Interface::emitReaderWriter( |
| 771 | Formatter &out, |
| 772 | const std::string &name, |
| 773 | const std::string &parcelObj, |
| 774 | bool parcelObjIsPointer, |
| 775 | bool isReader, |
| 776 | ErrorMode mode) const { |
| 777 | const std::string parcelObjDeref = |
| 778 | parcelObj + (parcelObjIsPointer ? "->" : "."); |
| 779 | |
| 780 | if (isReader) { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 781 | out << "{\n"; |
| 782 | out.indent(); |
| 783 | |
Howard Chen | ecfb451 | 2017-11-21 18:28:53 +0800 | [diff] [blame] | 784 | const std::string binderName = "_hidl_binder"; |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 785 | out << "::android::sp<::android::hardware::IBinder> " |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 786 | << binderName << ";\n"; |
| 787 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 788 | out << "_hidl_err = "; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 789 | out << parcelObjDeref |
| 790 | << "readNullableStrongBinder(&" |
| 791 | << binderName |
| 792 | << ");\n"; |
| 793 | |
| 794 | handleError(out, mode); |
| 795 | |
| 796 | out << name |
| 797 | << " = " |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 798 | << "::android::hardware::fromBinder<" |
| 799 | << fqName().cppName() |
| 800 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 801 | << getProxyFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 802 | << "," |
Yifan Hong | 51a6509 | 2017-01-04 15:41:44 -0800 | [diff] [blame] | 803 | << getStubFqName().cppName() |
Martijn Coenen | a63e0ad | 2016-12-07 17:29:00 +0100 | [diff] [blame] | 804 | << ">(" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 805 | << binderName |
| 806 | << ");\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 807 | |
| 808 | out.unindent(); |
| 809 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 810 | } else { |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 811 | out << "if (" << name << " == nullptr) {\n"; |
| 812 | out.indent(); |
| 813 | out << "_hidl_err = "; |
| 814 | out << parcelObjDeref |
| 815 | << "writeStrongBinder(nullptr);\n"; |
| 816 | out.unindent(); |
| 817 | out << "} else {\n"; |
| 818 | out.indent(); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 819 | out << "::android::sp<::android::hardware::IBinder> _hidl_binder = " |
Steven Moreland | 731dbb1 | 2018-12-03 18:09:46 -0800 | [diff] [blame] | 820 | << "::android::hardware::getOrCreateCachedBinder(" << name << ".get());\n"; |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 821 | out << "if (_hidl_binder.get() != nullptr) {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 822 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 823 | out << "_hidl_err = " |
| 824 | << parcelObjDeref |
| 825 | << "writeStrongBinder(_hidl_binder);\n"; |
| 826 | }); |
| 827 | out << "} else {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 828 | out.indent([&] { |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 829 | out << "_hidl_err = ::android::UNKNOWN_ERROR;\n"; |
| 830 | }); |
| 831 | out << "}\n"; |
Martijn Coenen | e163823 | 2016-10-26 12:51:34 +0200 | [diff] [blame] | 832 | out.unindent(); |
| 833 | out << "}\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 834 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 835 | handleError(out, mode); |
| 836 | } |
| 837 | } |
| 838 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 839 | void Interface::emitPackageTypeDeclarations(Formatter& out) const { |
| 840 | Scope::emitPackageTypeDeclarations(out); |
Steven Moreland | bf71421 | 2017-10-27 18:29:01 -0700 | [diff] [blame] | 841 | |
Steven Moreland | 09c6ebe | 2018-10-09 10:15:48 -0700 | [diff] [blame] | 842 | out << "static inline std::string toString(" << getCppArgumentType() << " o);\n\n"; |
| 843 | } |
| 844 | |
| 845 | void Interface::emitPackageTypeHeaderDefinitions(Formatter& out) const { |
| 846 | Scope::emitPackageTypeHeaderDefinitions(out); |
| 847 | |
Steven Moreland | bf71421 | 2017-10-27 18:29:01 -0700 | [diff] [blame] | 848 | out << "static inline std::string toString(" << getCppArgumentType() << " o) "; |
| 849 | |
| 850 | out.block([&] { |
| 851 | out << "std::string os = \"[class or subclass of \";\n" |
| 852 | << "os += " << fullName() << "::descriptor;\n" |
| 853 | << "os += \"]\";\n" |
| 854 | << "os += o->isRemote() ? \"@remote\" : \"@local\";\n" |
| 855 | << "return os;\n"; |
| 856 | }).endl().endl(); |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 857 | } |
| 858 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 859 | void Interface::emitTypeDefinitions(Formatter& out, const std::string& prefix) const { |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 860 | std::string space = prefix.empty() ? "" : (prefix + "::"); |
Steven Moreland | d8c7a29 | 2017-10-27 18:32:06 -0700 | [diff] [blame] | 861 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 862 | Scope::emitTypeDefinitions(out, space + localName()); |
Yifan Hong | f5cc2f7 | 2017-01-04 18:02:34 -0800 | [diff] [blame] | 863 | } |
| 864 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 865 | void Interface::emitJavaReaderWriter( |
| 866 | Formatter &out, |
| 867 | const std::string &parcelObj, |
| 868 | const std::string &argName, |
| 869 | bool isReader) const { |
| 870 | if (isReader) { |
| 871 | out << fullJavaName() |
| 872 | << ".asInterface(" |
| 873 | << parcelObj |
| 874 | << ".readStrongBinder());\n"; |
| 875 | } else { |
| 876 | out << parcelObj |
| 877 | << ".writeStrongBinder(" |
| 878 | << argName |
| 879 | << " == null ? null : " |
| 880 | << argName |
| 881 | << ".asBinder());\n"; |
| 882 | } |
| 883 | } |
| 884 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 885 | void Interface::emitVtsAttributeDeclaration(Formatter& out) const { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 886 | for (const auto &type : getSubTypes()) { |
Zhuoyao Zhang | c5ea9f5 | 2016-10-06 15:05:39 -0700 | [diff] [blame] | 887 | // Skip for TypeDef as it is just an alias of a defined type. |
| 888 | if (type->isTypeDef()) { |
| 889 | continue; |
| 890 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 891 | out << "attribute: {\n"; |
| 892 | out.indent(); |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 893 | type->emitVtsTypeDeclarations(out); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 894 | out.unindent(); |
| 895 | out << "}\n\n"; |
| 896 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Zhuoyao Zhang | e59c933 | 2018-07-20 14:16:04 -0700 | [diff] [blame] | 899 | void Interface::emitVtsMethodDeclaration(Formatter& out, bool isInherited) const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 900 | for (const auto &method : methods()) { |
Steven Moreland | cea2478 | 2016-11-07 11:40:48 -0800 | [diff] [blame] | 901 | if (method->isHidlReserved()) { |
| 902 | continue; |
| 903 | } |
| 904 | |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 905 | out << "api: {\n"; |
| 906 | out.indent(); |
| 907 | out << "name: \"" << method->name() << "\"\n"; |
Zhuoyao Zhang | e59c933 | 2018-07-20 14:16:04 -0700 | [diff] [blame] | 908 | out << "is_inherited: " << (isInherited ? "true" : "false") << "\n"; |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 909 | // Generate declaration for each return value. |
| 910 | for (const auto &result : method->results()) { |
| 911 | out << "return_type_hidl: {\n"; |
| 912 | out.indent(); |
Socrates Li | 7b0a42b | 2018-07-16 13:23:17 -0700 | [diff] [blame] | 913 | out << "name: \"" << result->name() << "\"\n"; |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 914 | result->type().emitVtsAttributeType(out); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 915 | out.unindent(); |
| 916 | out << "}\n"; |
| 917 | } |
| 918 | // Generate declaration for each input argument |
| 919 | for (const auto &arg : method->args()) { |
| 920 | out << "arg: {\n"; |
| 921 | out.indent(); |
Socrates Li | 7b0a42b | 2018-07-16 13:23:17 -0700 | [diff] [blame] | 922 | out << "name: \"" << arg->name() << "\"\n"; |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 923 | arg->type().emitVtsAttributeType(out); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 924 | out.unindent(); |
| 925 | out << "}\n"; |
| 926 | } |
| 927 | // Generate declaration for each annotation. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 928 | for (const auto &annotation : method->annotations()) { |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 929 | out << "callflow: {\n"; |
| 930 | out.indent(); |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 931 | const std::string name = annotation->name(); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 932 | if (name == "entry") { |
| 933 | out << "entry: true\n"; |
| 934 | } else if (name == "exit") { |
| 935 | out << "exit: true\n"; |
| 936 | } else if (name == "callflow") { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 937 | const AnnotationParam *param = |
| 938 | annotation->getParam("next"); |
| 939 | if (param != nullptr) { |
Timur Iskhakov | 7a85dc2 | 2017-08-10 19:06:41 -0700 | [diff] [blame] | 940 | for (const auto& value : param->getValues()) { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 941 | out << "next: " << value << "\n"; |
| 942 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 943 | } |
| 944 | } else { |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 945 | CHECK(false); |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 946 | } |
| 947 | out.unindent(); |
| 948 | out << "}\n"; |
| 949 | } |
| 950 | out.unindent(); |
| 951 | out << "}\n\n"; |
| 952 | } |
Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 953 | } |
| 954 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 955 | void Interface::emitVtsAttributeType(Formatter& out) const { |
Zhuoyao Zhang | a588b23 | 2016-11-10 14:37:35 -0800 | [diff] [blame] | 956 | out << "type: " << getVtsType() << "\n" |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 957 | << "predefined_type: \"" |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 958 | << fullName() |
| 959 | << "\"\n"; |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 960 | } |
| 961 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 962 | bool Interface::hasOnewayMethods() const { |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 963 | for (auto const &method : methods()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 964 | if (method->isOneway()) { |
| 965 | return true; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | const Interface* superClass = superType(); |
| 970 | |
| 971 | if (superClass != nullptr) { |
| 972 | return superClass->hasOnewayMethods(); |
| 973 | } |
| 974 | |
| 975 | return false; |
| 976 | } |
| 977 | |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 978 | bool Interface::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const { |
| 979 | if (superType() != nullptr && !superType()->isJavaCompatible(visited)) { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 980 | return false; |
| 981 | } |
| 982 | |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 983 | for (const auto* method : methods()) { |
| 984 | if (!method->deepIsJavaCompatible(visited)) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 985 | return false; |
| 986 | } |
| 987 | } |
| 988 | |
Steven Moreland | b0da6fc | 2018-10-16 17:15:11 -0700 | [diff] [blame] | 989 | return Scope::deepIsJavaCompatible(visited); |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 990 | } |
| 991 | |
Timur Iskhakov | ff5e64a | 2017-09-11 14:56:18 -0700 | [diff] [blame] | 992 | bool Interface::isNeverStrongReference() const { |
| 993 | return true; |
| 994 | } |
| 995 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 996 | } // namespace android |
| 997 | |