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 | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 17 | #include "AST.h" |
| 18 | |
| 19 | #include "Coordinator.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 20 | #include "EnumType.h" |
Andreas Huber | 6755e9d | 2017-04-06 11:09:07 -0700 | [diff] [blame] | 21 | #include "HidlTypeAssertion.h" |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 22 | #include "Interface.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 23 | #include "Method.h" |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 24 | #include "Reference.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 25 | #include "ScalarType.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 26 | #include "Scope.h" |
| 27 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 28 | #include <algorithm> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 29 | #include <hidl-util/Formatter.h> |
Steven Moreland | 5708edf | 2016-11-04 15:33:31 +0000 | [diff] [blame] | 30 | #include <hidl-util/StringHelper.h> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 31 | #include <android-base/logging.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 32 | #include <string> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
| 35 | namespace android { |
| 36 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 37 | void AST::getPackageComponents( |
| 38 | std::vector<std::string> *components) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 39 | mPackage.getPackageComponents(components); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void AST::getPackageAndVersionComponents( |
| 43 | std::vector<std::string> *components, bool cpp_compatible) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 44 | mPackage.getPackageAndVersionComponents(components, cpp_compatible); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Steven Moreland | 5708edf | 2016-11-04 15:33:31 +0000 | [diff] [blame] | 47 | std::string AST::makeHeaderGuard(const std::string &baseName, |
| 48 | bool indicateGenerated) const { |
| 49 | std::string guard; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 50 | |
Steven Moreland | 5708edf | 2016-11-04 15:33:31 +0000 | [diff] [blame] | 51 | if (indicateGenerated) { |
| 52 | guard += "HIDL_GENERATED_"; |
| 53 | } |
| 54 | |
| 55 | guard += StringHelper::Uppercase(mPackage.tokenName()); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 56 | guard += "_"; |
Steven Moreland | 5708edf | 2016-11-04 15:33:31 +0000 | [diff] [blame] | 57 | guard += StringHelper::Uppercase(baseName); |
| 58 | guard += "_H"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 59 | |
| 60 | return guard; |
| 61 | } |
| 62 | |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 63 | void AST::generateCppPackageInclude( |
| 64 | Formatter &out, |
| 65 | const FQName &package, |
| 66 | const std::string &klass) { |
| 67 | |
| 68 | out << "#include <"; |
| 69 | |
| 70 | std::vector<std::string> components; |
| 71 | package.getPackageAndVersionComponents(&components, false /* cpp_compatible */); |
| 72 | |
| 73 | for (const auto &component : components) { |
| 74 | out << component << "/"; |
| 75 | } |
| 76 | |
| 77 | out << klass |
| 78 | << ".h>\n"; |
| 79 | } |
| 80 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 81 | void AST::enterLeaveNamespace(Formatter &out, bool enter) const { |
| 82 | std::vector<std::string> packageComponents; |
| 83 | getPackageAndVersionComponents( |
| 84 | &packageComponents, true /* cpp_compatible */); |
| 85 | |
| 86 | if (enter) { |
| 87 | for (const auto &component : packageComponents) { |
| 88 | out << "namespace " << component << " {\n"; |
| 89 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 90 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 91 | out.setNamespace(mPackage.cppNamespace() + "::"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 92 | } else { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 93 | out.setNamespace(std::string()); |
| 94 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 95 | for (auto it = packageComponents.rbegin(); |
| 96 | it != packageComponents.rend(); |
| 97 | ++it) { |
| 98 | out << "} // namespace " << *it << "\n"; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
Steven Moreland | 038903b | 2017-03-30 12:11:24 -0700 | [diff] [blame] | 103 | static void declareGetService(Formatter &out, const std::string &interfaceName, bool isTry) { |
| 104 | const std::string functionName = isTry ? "tryGetService" : "getService"; |
| 105 | |
| 106 | out << "static ::android::sp<" << interfaceName << "> " << functionName << "(" |
Chris Phoenix | db0d634 | 2017-01-11 16:10:00 -0800 | [diff] [blame] | 107 | << "const std::string &serviceName=\"default\", bool getStub=false);\n"; |
Steven Moreland | 038903b | 2017-03-30 12:11:24 -0700 | [diff] [blame] | 108 | out << "static ::android::sp<" << interfaceName << "> " << functionName << "(" |
Chris Phoenix | db0d634 | 2017-01-11 16:10:00 -0800 | [diff] [blame] | 109 | << "const char serviceName[], bool getStub=false)" |
| 110 | << " { std::string str(serviceName ? serviceName : \"\");" |
Steven Moreland | 038903b | 2017-03-30 12:11:24 -0700 | [diff] [blame] | 111 | << " return " << functionName << "(str, getStub); }\n"; |
| 112 | out << "static ::android::sp<" << interfaceName << "> " << functionName << "(" |
Chris Phoenix | db0d634 | 2017-01-11 16:10:00 -0800 | [diff] [blame] | 113 | << "const ::android::hardware::hidl_string& serviceName, bool getStub=false)" |
| 114 | // without c_str the std::string constructor is ambiguous |
| 115 | << " { std::string str(serviceName.c_str());" |
Steven Moreland | 038903b | 2017-03-30 12:11:24 -0700 | [diff] [blame] | 116 | << " return " << functionName << "(str, getStub); }\n"; |
| 117 | out << "static ::android::sp<" << interfaceName << "> " << functionName << "(" |
| 118 | << "bool getStub) { return " << functionName << "(\"default\", getStub); }\n"; |
| 119 | } |
| 120 | |
| 121 | static void declareServiceManagerInteractions(Formatter &out, const std::string &interfaceName) { |
| 122 | declareGetService(out, interfaceName, true /* isTry */); |
| 123 | declareGetService(out, interfaceName, false /* isTry */); |
| 124 | |
Steven Moreland | 9083150 | 2017-03-27 12:08:40 -0700 | [diff] [blame] | 125 | out << "__attribute__ ((warn_unused_result))" |
| 126 | << "::android::status_t registerAsService(const std::string &serviceName=\"default\");\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 127 | out << "static bool registerForNotifications(\n"; |
| 128 | out.indent(2, [&] { |
| 129 | out << "const std::string &serviceName,\n" |
| 130 | << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> " |
| 131 | << "¬ification);\n"; |
| 132 | }); |
| 133 | |
| 134 | } |
| 135 | |
Steven Moreland | 038903b | 2017-03-30 12:11:24 -0700 | [diff] [blame] | 136 | static void implementGetService(Formatter &out, |
| 137 | const FQName &fqName, |
| 138 | bool isTry) { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 139 | |
| 140 | const std::string interfaceName = fqName.getInterfaceName(); |
Steven Moreland | 038903b | 2017-03-30 12:11:24 -0700 | [diff] [blame] | 141 | const std::string functionName = isTry ? "tryGetService" : "getService"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 142 | |
Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 143 | out << "::android::sp<" << interfaceName << "> " << interfaceName << "::" << functionName << "(" |
Yifan Hong | 31f07ff | 2017-03-21 18:56:35 +0000 | [diff] [blame] | 144 | << "const std::string &serviceName, const bool getStub) "; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 145 | out.block([&] { |
Steven Moreland | 78f95f9 | 2017-10-06 17:07:40 -0700 | [diff] [blame] | 146 | out << "return ::android::hardware::details::getServiceInternal<" |
| 147 | << fqName.getInterfaceProxyName() |
| 148 | << ">(serviceName, " |
| 149 | << (!isTry ? "true" : "false") // retry |
| 150 | << ", getStub);\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 151 | }).endl().endl(); |
Steven Moreland | 038903b | 2017-03-30 12:11:24 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void implementServiceManagerInteractions(Formatter &out, |
| 155 | const FQName &fqName, const std::string &package) { |
| 156 | |
| 157 | const std::string interfaceName = fqName.getInterfaceName(); |
| 158 | |
| 159 | implementGetService(out, fqName, true /* isTry */); |
| 160 | implementGetService(out, fqName, false /* isTry */); |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 161 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 162 | out << "::android::status_t " << interfaceName << "::registerAsService(" |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 163 | << "const std::string &serviceName) "; |
| 164 | out.block([&] { |
Steven Moreland | 5f84b38 | 2018-10-11 12:10:35 -0700 | [diff] [blame] | 165 | out << "return ::android::hardware::details::registerAsServiceInternal(this, serviceName);\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 166 | }).endl().endl(); |
| 167 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 168 | out << "bool " << interfaceName << "::registerForNotifications(\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 169 | out.indent(2, [&] { |
| 170 | out << "const std::string &serviceName,\n" |
| 171 | << "const ::android::sp<::android::hidl::manager::V1_0::IServiceNotification> " |
| 172 | << "¬ification) "; |
| 173 | }); |
| 174 | out.block([&] { |
| 175 | out << "const ::android::sp<::android::hidl::manager::V1_0::IServiceManager> sm\n"; |
| 176 | out.indent(2, [&] { |
| 177 | out << "= ::android::hardware::defaultServiceManager();\n"; |
| 178 | }); |
| 179 | out.sIf("sm == nullptr", [&] { |
| 180 | out << "return false;\n"; |
| 181 | }).endl(); |
| 182 | out << "::android::hardware::Return<bool> success =\n"; |
| 183 | out.indent(2, [&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 184 | out << "sm->registerForNotifications(\"" << package << "::" << interfaceName << "\",\n"; |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 185 | out.indent(2, [&] { |
| 186 | out << "serviceName, notification);\n"; |
| 187 | }); |
| 188 | }); |
| 189 | out << "return success.isOk() && success;\n"; |
| 190 | }).endl().endl(); |
| 191 | } |
| 192 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 193 | void AST::generateInterfaceHeader(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 194 | const Interface *iface = getInterface(); |
| 195 | std::string ifaceName = iface ? iface->localName() : "types"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 196 | const std::string guard = makeHeaderGuard(ifaceName); |
| 197 | |
| 198 | out << "#ifndef " << guard << "\n"; |
| 199 | out << "#define " << guard << "\n\n"; |
| 200 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 201 | for (const auto &item : mImportedNames) { |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 202 | generateCppPackageInclude(out, item, item.name()); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | if (!mImportedNames.empty()) { |
| 206 | out << "\n"; |
| 207 | } |
| 208 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 209 | if (iface) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 210 | if (isIBase()) { |
| 211 | out << "// skipped #include IServiceNotification.h\n\n"; |
| 212 | } else { |
| 213 | out << "#include <android/hidl/manager/1.0/IServiceNotification.h>\n\n"; |
| 214 | } |
Steven Moreland | 0693f31 | 2016-11-09 15:06:14 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 217 | out << "#include <hidl/HidlSupport.h>\n"; |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 218 | out << "#include <hidl/MQDescriptor.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 219 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 220 | if (iface) { |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 221 | out << "#include <hidl/Status.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Martijn Coenen | af712c0 | 2016-11-16 15:26:27 +0100 | [diff] [blame] | 224 | out << "#include <utils/NativeHandle.h>\n"; |
| 225 | out << "#include <utils/misc.h>\n\n"; /* for report_sysprop_change() */ |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 226 | |
| 227 | enterLeaveNamespace(out, true /* enter */); |
| 228 | out << "\n"; |
| 229 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 230 | if (iface) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 231 | out << "struct " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 232 | << ifaceName; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 233 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 234 | const Interface *superType = iface->superType(); |
| 235 | |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 236 | if (superType == nullptr) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 237 | out << " : virtual public ::android::RefBase"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 238 | } else { |
Steven Moreland | d916a70 | 2016-10-26 22:23:09 +0000 | [diff] [blame] | 239 | out << " : public " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 240 | << superType->fullName(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | out << " {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 244 | |
| 245 | out.indent(); |
| 246 | |
Steven Moreland | 1a52e82 | 2017-07-27 13:56:29 -0700 | [diff] [blame] | 247 | generateCppTag(out, "android::hardware::details::i_tag"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 250 | emitTypeDeclarations(out); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 251 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 252 | if (iface) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 253 | out << "virtual bool isRemote() const "; |
| 254 | if (!isIBase()) { |
| 255 | out << "override "; |
| 256 | } |
| 257 | out << "{ return false; }\n\n"; |
Steven Moreland | d732ea1 | 2016-11-08 17:12:06 -0800 | [diff] [blame] | 258 | |
Steven Moreland | f7f2a9a | 2017-07-21 18:05:38 -0700 | [diff] [blame] | 259 | for (const auto& tuple : iface->allMethodsFromRoot()) { |
| 260 | const Method* method = tuple.method(); |
| 261 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 262 | out << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 263 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 264 | const bool returnsValue = !method->results().empty(); |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 265 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Steven Moreland | d732ea1 | 2016-11-08 17:12:06 -0800 | [diff] [blame] | 266 | |
| 267 | if (elidedReturn == nullptr && returnsValue) { |
| 268 | out << "using " |
| 269 | << method->name() |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 270 | << "_cb = std::function<void("; |
| 271 | method->emitCppResultSignature(out, true /* specify namespaces */); |
| 272 | out << ")>;\n"; |
Steven Moreland | d732ea1 | 2016-11-08 17:12:06 -0800 | [diff] [blame] | 273 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 274 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 275 | method->dumpAnnotations(out); |
| 276 | |
Steven Moreland | 49bad8d | 2018-05-17 15:45:26 -0700 | [diff] [blame] | 277 | method->emitDocComment(out); |
| 278 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 279 | if (elidedReturn) { |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 280 | out << "virtual ::android::hardware::Return<"; |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 281 | out << elidedReturn->type().getCppResultType() << "> "; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 282 | } else { |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 283 | out << "virtual ::android::hardware::Return<void> "; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | out << method->name() |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 287 | << "("; |
| 288 | method->emitCppArgSignature(out, true /* specify namespaces */); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 289 | out << ")"; |
| 290 | if (method->isHidlReserved()) { |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 291 | if (!isIBase()) { |
| 292 | out << " override"; |
| 293 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 294 | } else { |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 295 | out << " = 0"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 296 | } |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 297 | out << ";\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 298 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 299 | |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 300 | out << "// cast static functions\n"; |
| 301 | std::string childTypeResult = iface->getCppResultType(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 302 | |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 303 | for (const Interface *superType : iface->typeChain()) { |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 304 | out << "static ::android::hardware::Return<" |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 305 | << childTypeResult |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 306 | << "> castFrom(" |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 307 | << superType->getCppArgumentType() |
| 308 | << " parent" |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 309 | << ", bool emitError = false);\n"; |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Steven Moreland | d39133b | 2016-11-11 12:30:08 -0800 | [diff] [blame] | 312 | out << "\nstatic const char* descriptor;\n\n"; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 313 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 314 | if (isIBase()) { |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 315 | out << "// skipped getService, registerAsService, registerForNotifications\n\n"; |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 316 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 317 | declareServiceManagerInteractions(out, iface->localName()); |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 318 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 319 | } |
| 320 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 321 | if (iface) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 322 | out.unindent(); |
| 323 | |
Andreas Huber | e3f769a | 2016-10-10 10:54:44 -0700 | [diff] [blame] | 324 | out << "};\n\n"; |
| 325 | } |
| 326 | |
Steven Moreland | 09c6ebe | 2018-10-09 10:15:48 -0700 | [diff] [blame] | 327 | out << "//\n"; |
| 328 | out << "// type declarations for package\n"; |
| 329 | out << "//\n\n"; |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 330 | mRootScope.emitPackageTypeDeclarations(out); |
Steven Moreland | 09c6ebe | 2018-10-09 10:15:48 -0700 | [diff] [blame] | 331 | out << "//\n"; |
| 332 | out << "// type header definitions for package\n"; |
| 333 | out << "//\n\n"; |
| 334 | mRootScope.emitPackageTypeHeaderDefinitions(out); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 335 | |
| 336 | out << "\n"; |
| 337 | enterLeaveNamespace(out, false /* enter */); |
Steven Moreland | 09c6ebe | 2018-10-09 10:15:48 -0700 | [diff] [blame] | 338 | out << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 339 | |
Steven Moreland | 09c6ebe | 2018-10-09 10:15:48 -0700 | [diff] [blame] | 340 | out << "//\n"; |
| 341 | out << "// global type declarations for package\n"; |
| 342 | out << "//\n\n"; |
Steven Moreland | 8e61c5a | 2017-11-17 15:55:28 -0800 | [diff] [blame] | 343 | mRootScope.emitGlobalTypeDeclarations(out); |
| 344 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 345 | out << "\n#endif // " << guard << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 348 | void AST::generateHwBinderHeader(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 349 | const Interface *iface = getInterface(); |
| 350 | std::string klassName = iface ? iface->getHwName() : "hwtypes"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 351 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 352 | const std::string guard = makeHeaderGuard(klassName); |
| 353 | |
| 354 | out << "#ifndef " << guard << "\n"; |
| 355 | out << "#define " << guard << "\n\n"; |
| 356 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 357 | generateCppPackageInclude(out, mPackage, iface ? iface->localName() : "types"); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 358 | |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 359 | out << "\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 360 | |
| 361 | for (const auto &item : mImportedNames) { |
| 362 | if (item.name() == "types") { |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 363 | generateCppPackageInclude(out, item, "hwtypes"); |
| 364 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 365 | generateCppPackageInclude(out, item, item.getInterfaceStubName()); |
| 366 | generateCppPackageInclude(out, item, item.getInterfaceProxyName()); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 367 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | out << "\n"; |
| 371 | |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 372 | out << "#include <hidl/Status.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 373 | out << "#include <hwbinder/IBinder.h>\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 374 | out << "#include <hwbinder/Parcel.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 375 | |
| 376 | out << "\n"; |
| 377 | |
| 378 | enterLeaveNamespace(out, true /* enter */); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 379 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 380 | mRootScope.emitPackageHwDeclarations(out); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 381 | |
| 382 | enterLeaveNamespace(out, false /* enter */); |
| 383 | |
| 384 | out << "\n#endif // " << guard << "\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 387 | void AST::emitTypeDeclarations(Formatter& out) const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 388 | return mRootScope.emitTypeDeclarations(out); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 391 | static std::string wrapPassthroughArg(Formatter& out, const NamedReference<Type>* arg, |
| 392 | std::string name, std::function<void(void)> handleError) { |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 393 | if (!arg->type().isInterface()) { |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 394 | return name; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 395 | } |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 396 | std::string wrappedName = "_hidl_wrapped_" + name; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 397 | const Interface &iface = static_cast<const Interface &>(arg->type()); |
| 398 | out << iface.getCppStackType() << " " << wrappedName << ";\n"; |
| 399 | // TODO(elsk): b/33754152 Should not wrap this if object is Bs* |
| 400 | out.sIf(name + " != nullptr && !" + name + "->isRemote()", [&] { |
| 401 | out << wrappedName |
| 402 | << " = " |
Steven Moreland | bff4bd2 | 2017-10-02 14:46:06 -0700 | [diff] [blame] | 403 | << "::android::hardware::details::wrapPassthrough(" |
| 404 | << name |
| 405 | << ");\n"; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 406 | out.sIf(wrappedName + " == nullptr", [&] { |
| 407 | // Fatal error. Happens when the BsFoo class is not found in the binary |
| 408 | // or any dynamic libraries. |
| 409 | handleError(); |
| 410 | }).endl(); |
| 411 | }).sElse([&] { |
| 412 | out << wrappedName << " = " << name << ";\n"; |
| 413 | }).endl().endl(); |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 414 | |
| 415 | return wrappedName; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 416 | } |
| 417 | |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 418 | void AST::generatePassthroughMethod(Formatter& out, const Method* method, const Interface* superInterface) const { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 419 | method->generateCppSignature(out); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 420 | |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 421 | out << " override {\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 422 | out.indent(); |
| 423 | |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 424 | if (method->isHidlReserved() |
| 425 | && method->overridesCppImpl(IMPL_PASSTHROUGH)) { |
| 426 | method->cppImpl(IMPL_PASSTHROUGH, out); |
| 427 | out.unindent(); |
| 428 | out << "}\n\n"; |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 429 | return; |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 430 | } |
| 431 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 432 | const bool returnsValue = !method->results().empty(); |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 433 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 434 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 435 | if (returnsValue && elidedReturn == nullptr) { |
| 436 | generateCheckNonNull(out, "_hidl_cb"); |
| 437 | } |
| 438 | |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 439 | generateCppInstrumentationCall( |
| 440 | out, |
| 441 | InstrumentationEvent::PASSTHROUGH_ENTRY, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 442 | method, |
| 443 | superInterface); |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 444 | |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 445 | std::vector<std::string> wrappedArgNames; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 446 | for (const auto &arg : method->args()) { |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 447 | std::string name = wrapPassthroughArg(out, arg, arg->name(), [&] { |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 448 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 449 | out.indent(2, [&] { |
| 450 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n" |
Yifan Hong | 0abd739 | 2016-12-20 16:43:26 -0800 | [diff] [blame] | 451 | << "\"Cannot wrap passthrough interface.\");\n"; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 452 | }); |
| 453 | }); |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 454 | |
| 455 | wrappedArgNames.push_back(name); |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 456 | } |
| 457 | |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 458 | out << "::android::hardware::Status _hidl_error = ::android::hardware::Status::ok();\n"; |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 459 | out << "auto _hidl_return = "; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 460 | |
| 461 | if (method->isOneway()) { |
Steven Moreland | 836cb31 | 2017-06-05 17:25:55 -0700 | [diff] [blame] | 462 | out << "addOnewayTask([mImpl = this->mImpl\n" |
| 463 | << "#ifdef __ANDROID_DEBUGGABLE__\n" |
| 464 | ", mEnableInstrumentation = this->mEnableInstrumentation, " |
| 465 | "mInstrumentationCallbacks = this->mInstrumentationCallbacks\n" |
| 466 | << "#endif // __ANDROID_DEBUGGABLE__\n"; |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 467 | for (const std::string& arg : wrappedArgNames) { |
| 468 | out << ", " << arg; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 469 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 470 | out << "] {\n"; |
| 471 | out.indent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | out << "mImpl->" |
| 475 | << method->name() |
| 476 | << "("; |
| 477 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 478 | out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) { |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 479 | out << (arg->type().isInterface() ? "_hidl_wrapped_" : "") << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 480 | }); |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 481 | |
| 482 | std::function<void(void)> kHandlePassthroughError = [&] { |
| 483 | out << "_hidl_error = ::android::hardware::Status::fromExceptionCode(\n"; |
| 484 | out.indent(2, [&] { |
| 485 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n" |
| 486 | << "\"Cannot wrap passthrough interface.\");\n"; |
| 487 | }); |
| 488 | }; |
| 489 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 490 | if (returnsValue && elidedReturn == nullptr) { |
Steven Moreland | 340c882 | 2017-05-02 14:41:49 -0700 | [diff] [blame] | 491 | // never true if oneway since oneway methods don't return values |
| 492 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 493 | if (!method->args().empty()) { |
| 494 | out << ", "; |
| 495 | } |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 496 | out << "[&]("; |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 497 | out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) { |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 498 | out << "const auto &_hidl_out_" |
| 499 | << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 500 | }); |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 501 | |
| 502 | out << ") {\n"; |
| 503 | out.indent(); |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 504 | generateCppInstrumentationCall( |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 505 | out, |
| 506 | InstrumentationEvent::PASSTHROUGH_EXIT, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 507 | method, |
| 508 | superInterface); |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 509 | |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 510 | std::vector<std::string> wrappedOutNames; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 511 | for (const auto &arg : method->results()) { |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 512 | wrappedOutNames.push_back( |
| 513 | wrapPassthroughArg(out, arg, "_hidl_out_" + arg->name(), kHandlePassthroughError)); |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 514 | } |
| 515 | |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 516 | out << "_hidl_cb("; |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 517 | out.join(wrappedOutNames.begin(), wrappedOutNames.end(), ", ", |
| 518 | [&](const std::string& arg) { out << arg; }); |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 519 | out << ");\n"; |
| 520 | out.unindent(); |
| 521 | out << "});\n\n"; |
| 522 | } else { |
| 523 | out << ");\n\n"; |
Steven Moreland | 30b76e9 | 2017-06-02 18:52:24 -0700 | [diff] [blame] | 524 | |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 525 | if (elidedReturn != nullptr) { |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 526 | const std::string outName = "_hidl_out_" + elidedReturn->name(); |
| 527 | |
| 528 | out << elidedReturn->type().getCppResultType() << " " << outName |
| 529 | << " = _hidl_return;\n"; |
| 530 | out << "(void) " << outName << ";\n"; |
| 531 | |
| 532 | const std::string wrappedName = |
| 533 | wrapPassthroughArg(out, elidedReturn, outName, kHandlePassthroughError); |
| 534 | |
| 535 | if (outName != wrappedName) { |
| 536 | // update the original value since it is used by generateCppInstrumentationCall |
| 537 | out << outName << " = " << wrappedName << ";\n\n"; |
| 538 | |
| 539 | // update the value to be returned |
| 540 | out << "_hidl_return = " << outName << "\n;"; |
| 541 | } |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 542 | } |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 543 | generateCppInstrumentationCall( |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 544 | out, |
| 545 | InstrumentationEvent::PASSTHROUGH_EXIT, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 546 | method, |
| 547 | superInterface); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 548 | } |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 549 | |
| 550 | if (method->isOneway()) { |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 551 | out.unindent(); |
| 552 | out << "});\n"; |
Steven Moreland | 58a20c7 | 2018-10-09 12:30:51 -0700 | [diff] [blame] | 553 | } else { |
| 554 | out << "if (!_hidl_error.isOk()) return _hidl_error;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 555 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 556 | |
| 557 | out << "return _hidl_return;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 558 | |
| 559 | out.unindent(); |
| 560 | out << "}\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 563 | void AST::generateMethods(Formatter& out, const MethodGenerator& gen, bool includeParent) const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 564 | const Interface* iface = mRootScope.getInterface(); |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 565 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 566 | const Interface *prevIterface = nullptr; |
| 567 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 568 | const Method *method = tuple.method(); |
| 569 | const Interface *superInterface = tuple.interface(); |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 570 | |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 571 | if (!includeParent && superInterface != iface) { |
| 572 | continue; |
| 573 | } |
| 574 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 575 | if(prevIterface != superInterface) { |
| 576 | if (prevIterface != nullptr) { |
| 577 | out << "\n"; |
| 578 | } |
| 579 | out << "// Methods from " |
| 580 | << superInterface->fullName() |
| 581 | << " follow.\n"; |
| 582 | prevIterface = superInterface; |
| 583 | } |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 584 | gen(method, superInterface); |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 587 | out << "\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 588 | } |
| 589 | |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 590 | void AST::generateTemplatizationLink(Formatter& out) const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 591 | out << "typedef " << mRootScope.getInterface()->localName() << " Pure;\n\n"; |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Steven Moreland | 1a52e82 | 2017-07-27 13:56:29 -0700 | [diff] [blame] | 594 | void AST::generateCppTag(Formatter& out, const std::string& tag) const { |
| 595 | out << "typedef " << tag << " _hidl_tag;\n\n"; |
| 596 | } |
| 597 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 598 | void AST::generateStubHeader(Formatter& out) const { |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 599 | CHECK(AST::isInterface()); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 600 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 601 | const Interface* iface = mRootScope.getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 602 | const std::string klassName = iface->getStubName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 603 | const std::string guard = makeHeaderGuard(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 604 | |
| 605 | out << "#ifndef " << guard << "\n"; |
| 606 | out << "#define " << guard << "\n\n"; |
| 607 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 608 | generateCppPackageInclude(out, mPackage, iface->getHwName()); |
Steven Moreland | 1a52e82 | 2017-07-27 13:56:29 -0700 | [diff] [blame] | 609 | |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 610 | out << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 611 | |
| 612 | enterLeaveNamespace(out, true /* enter */); |
| 613 | out << "\n"; |
| 614 | |
| 615 | out << "struct " |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 616 | << klassName; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 617 | if (iface->isIBase()) { |
Yifan Hong | 96a79e2 | 2017-01-12 14:22:05 -0800 | [diff] [blame] | 618 | out << " : public ::android::hardware::BHwBinder"; |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 619 | out << ", public ::android::hardware::details::HidlInstrumentor {\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 620 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 621 | out << " : public " |
| 622 | << gIBaseFqName.getInterfaceStubFqName().cppName() |
| 623 | << " {\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 624 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 625 | |
| 626 | out.indent(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 627 | out << "explicit " |
| 628 | << klassName |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 629 | << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl);" |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 630 | << "\n"; |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 631 | out << "explicit " |
| 632 | << klassName |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 633 | << "(const ::android::sp<" << iface->localName() << "> &_hidl_impl," |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 634 | << " const std::string& HidlInstrumentor_package," |
| 635 | << " const std::string& HidlInstrumentor_interface);" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 636 | << "\n\n"; |
Steven Moreland | 57a8936 | 2017-07-21 19:29:54 +0000 | [diff] [blame] | 637 | out << "virtual ~" << klassName << "();\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 638 | out << "::android::status_t onTransact(\n"; |
| 639 | out.indent(); |
| 640 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 641 | out << "uint32_t _hidl_code,\n"; |
| 642 | out << "const ::android::hardware::Parcel &_hidl_data,\n"; |
| 643 | out << "::android::hardware::Parcel *_hidl_reply,\n"; |
| 644 | out << "uint32_t _hidl_flags = 0,\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 645 | out << "TransactCallback _hidl_cb = nullptr) override;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 646 | out.unindent(); |
| 647 | out.unindent(); |
| 648 | |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 649 | out.endl(); |
| 650 | generateTemplatizationLink(out); |
Steven Moreland | 1a52e82 | 2017-07-27 13:56:29 -0700 | [diff] [blame] | 651 | generateCppTag(out, "android::hardware::details::bnhw_tag"); |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 652 | |
Steven Moreland | cbcf9f7 | 2017-11-20 10:04:15 -0800 | [diff] [blame] | 653 | out << "::android::sp<" << iface->localName() << "> getImpl() { return _hidl_mImpl; }\n"; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 654 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 655 | generateMethods(out, |
| 656 | [&](const Method* method, const Interface*) { |
| 657 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) { |
| 658 | return; |
| 659 | } |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 660 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 661 | out << "static ::android::status_t _hidl_" << method->name() << "(\n"; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 662 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 663 | out.indent(2, |
| 664 | [&] { |
| 665 | out << "::android::hidl::base::V1_0::BnHwBase* _hidl_this,\n" |
| 666 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 667 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 668 | << "TransactCallback _hidl_cb);\n"; |
| 669 | }) |
| 670 | .endl() |
| 671 | .endl(); |
| 672 | }, |
| 673 | false /* include parents */); |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 674 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 675 | out.unindent(); |
| 676 | out << "private:\n"; |
| 677 | out.indent(); |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 678 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 679 | generateMethods(out, [&](const Method* method, const Interface* iface) { |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 680 | if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) { |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 681 | return; |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 682 | } |
| 683 | const bool returnsValue = !method->results().empty(); |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 684 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 685 | |
| 686 | if (elidedReturn == nullptr && returnsValue) { |
| 687 | out << "using " << method->name() << "_cb = " |
| 688 | << iface->fqName().cppName() |
| 689 | << "::" << method->name() << "_cb;\n"; |
| 690 | } |
| 691 | method->generateCppSignature(out); |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 692 | out << ";\n"; |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 693 | }); |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 694 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 695 | out << "::android::sp<" << iface->localName() << "> _hidl_mImpl;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 696 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 697 | out << "};\n\n"; |
| 698 | |
| 699 | enterLeaveNamespace(out, false /* enter */); |
| 700 | |
| 701 | out << "\n#endif // " << guard << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 702 | } |
| 703 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 704 | void AST::generateProxyHeader(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 705 | if (!AST::isInterface()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 706 | // types.hal does not get a proxy header. |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 707 | return; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 710 | const Interface* iface = mRootScope.getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 711 | const std::string proxyName = iface->getProxyName(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 712 | const std::string guard = makeHeaderGuard(proxyName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 713 | |
| 714 | out << "#ifndef " << guard << "\n"; |
| 715 | out << "#define " << guard << "\n\n"; |
| 716 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 717 | out << "#include <hidl/HidlTransportSupport.h>\n\n"; |
| 718 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 719 | std::vector<std::string> packageComponents; |
| 720 | getPackageAndVersionComponents( |
| 721 | &packageComponents, false /* cpp_compatible */); |
| 722 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 723 | generateCppPackageInclude(out, mPackage, iface->getHwName()); |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 724 | out << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 725 | |
| 726 | enterLeaveNamespace(out, true /* enter */); |
| 727 | out << "\n"; |
| 728 | |
| 729 | out << "struct " |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 730 | << proxyName |
| 731 | << " : public ::android::hardware::BpInterface<" |
| 732 | << iface->localName() |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 733 | << ">, public ::android::hardware::details::HidlInstrumentor {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 734 | |
| 735 | out.indent(); |
| 736 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 737 | out << "explicit " |
| 738 | << proxyName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 739 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 740 | << "\n\n"; |
| 741 | |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 742 | generateTemplatizationLink(out); |
Steven Moreland | 1a52e82 | 2017-07-27 13:56:29 -0700 | [diff] [blame] | 743 | generateCppTag(out, "android::hardware::details::bphw_tag"); |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 744 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 745 | out << "virtual bool isRemote() const override { return true; }\n\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 746 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 747 | generateMethods( |
| 748 | out, |
| 749 | [&](const Method* method, const Interface*) { |
| 750 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) { |
| 751 | return; |
| 752 | } |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 753 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 754 | out << "static "; |
| 755 | method->generateCppReturnType(out); |
| 756 | out << " _hidl_" << method->name() << "(" |
| 757 | << "::android::hardware::IInterface* _hidl_this, " |
| 758 | << "::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor"; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 759 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 760 | if (!method->hasEmptyCppArgSignature()) { |
| 761 | out << ", "; |
| 762 | } |
| 763 | method->emitCppArgSignature(out); |
| 764 | out << ");\n"; |
| 765 | }, |
| 766 | false /* include parents */); |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 767 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 768 | generateMethods(out, [&](const Method* method, const Interface*) { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 769 | method->generateCppSignature(out); |
| 770 | out << " override;\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 771 | }); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 772 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 773 | out.unindent(); |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 774 | out << "private:\n"; |
| 775 | out.indent(); |
| 776 | out << "std::mutex _hidl_mMutex;\n" |
| 777 | << "std::vector<::android::sp<::android::hardware::hidl_binder_death_recipient>>" |
| 778 | << " _hidl_mDeathRecipients;\n"; |
| 779 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 780 | out << "};\n\n"; |
| 781 | |
| 782 | enterLeaveNamespace(out, false /* enter */); |
| 783 | |
| 784 | out << "\n#endif // " << guard << "\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 785 | } |
| 786 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 787 | void AST::generateCppSource(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 788 | std::string baseName = getBaseName(); |
| 789 | const Interface *iface = getInterface(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 790 | |
Steven Moreland | a885d25 | 2017-09-25 18:44:43 -0700 | [diff] [blame] | 791 | const std::string klassName = baseName + (baseName == "types" ? "" : "All"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 792 | |
Steven Moreland | 623c004 | 2017-01-13 14:42:29 -0800 | [diff] [blame] | 793 | out << "#define LOG_TAG \"" |
| 794 | << mPackage.string() << "::" << baseName |
| 795 | << "\"\n\n"; |
| 796 | |
Steven Moreland | 5add34d | 2018-11-08 16:31:30 -0800 | [diff] [blame] | 797 | out << "#include <log/log.h>\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 798 | out << "#include <cutils/trace.h>\n"; |
| 799 | out << "#include <hidl/HidlTransportSupport.h>\n\n"; |
Steven Moreland | 26896a9 | 2018-07-31 15:31:01 -0700 | [diff] [blame] | 800 | out << "#include <hidl/Static.h>\n"; |
| 801 | out << "#include <hwbinder/ProcessState.h>\n"; |
Steven Moreland | 4607ef5 | 2018-05-09 10:52:47 -0700 | [diff] [blame] | 802 | out << "#include <utils/Trace.h>\n"; |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 803 | if (iface) { |
Steven Moreland | 19d5c17 | 2016-10-20 19:20:25 -0700 | [diff] [blame] | 804 | // This is a no-op for IServiceManager itself. |
| 805 | out << "#include <android/hidl/manager/1.0/IServiceManager.h>\n"; |
| 806 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 807 | generateCppPackageInclude(out, mPackage, iface->getProxyName()); |
| 808 | generateCppPackageInclude(out, mPackage, iface->getStubName()); |
| 809 | generateCppPackageInclude(out, mPackage, iface->getPassthroughName()); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 810 | |
| 811 | for (const Interface *superType : iface->superTypeChain()) { |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 812 | generateCppPackageInclude(out, |
| 813 | superType->fqName(), |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 814 | superType->fqName().getInterfaceProxyName()); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 815 | } |
Yifan Hong | 2cbbdf9 | 2016-12-05 15:20:50 -0800 | [diff] [blame] | 816 | |
| 817 | out << "#include <hidl/ServiceManagement.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 818 | } else { |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 819 | generateCppPackageInclude(out, mPackage, "types"); |
Yifan Hong | 244e82d | 2016-11-11 11:13:57 -0800 | [diff] [blame] | 820 | generateCppPackageInclude(out, mPackage, "hwtypes"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | out << "\n"; |
| 824 | |
| 825 | enterLeaveNamespace(out, true /* enter */); |
| 826 | out << "\n"; |
| 827 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 828 | generateTypeSource(out, iface ? iface->localName() : ""); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 829 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 830 | if (iface) { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 831 | const Interface* iface = mRootScope.getInterface(); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 832 | |
| 833 | // need to be put here, generateStubSource is using this. |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 834 | out << "const char* " |
| 835 | << iface->localName() |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 836 | << "::descriptor(\"" |
| 837 | << iface->fqName().string() |
| 838 | << "\");\n\n"; |
Yifan Hong | 91977fd | 2017-11-09 16:07:37 -0800 | [diff] [blame] | 839 | out << "__attribute__((constructor)) "; |
Martijn Coenen | 8adcb65 | 2017-02-03 17:37:36 +0100 | [diff] [blame] | 840 | out << "static void static_constructor() {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 841 | out.indent([&] { |
Yifan Hong | 91977fd | 2017-11-09 16:07:37 -0800 | [diff] [blame] | 842 | out << "::android::hardware::details::getBnConstructorMap().set(" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 843 | << iface->localName() |
Yifan Hong | b04de38 | 2017-02-06 15:31:52 -0800 | [diff] [blame] | 844 | << "::descriptor,\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 845 | out.indent(2, [&] { |
Yifan Hong | b04de38 | 2017-02-06 15:31:52 -0800 | [diff] [blame] | 846 | out << "[](void *iIntf) -> ::android::sp<::android::hardware::IBinder> {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 847 | out.indent([&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 848 | out << "return new " |
| 849 | << iface->getStubName() |
Yifan Hong | 341112d | 2017-04-20 18:12:05 -0700 | [diff] [blame] | 850 | << "(static_cast<" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 851 | << iface->localName() |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 852 | << " *>(iIntf));\n"; |
| 853 | }); |
Yifan Hong | b04de38 | 2017-02-06 15:31:52 -0800 | [diff] [blame] | 854 | out << "});\n"; |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 855 | }); |
Yifan Hong | 91977fd | 2017-11-09 16:07:37 -0800 | [diff] [blame] | 856 | out << "::android::hardware::details::getBsConstructorMap().set(" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 857 | << iface->localName() |
Yifan Hong | b04de38 | 2017-02-06 15:31:52 -0800 | [diff] [blame] | 858 | << "::descriptor,\n"; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 859 | out.indent(2, [&] { |
Yifan Hong | b04de38 | 2017-02-06 15:31:52 -0800 | [diff] [blame] | 860 | out << "[](void *iIntf) -> ::android::sp<" |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 861 | << gIBaseFqName.cppName() |
| 862 | << "> {\n"; |
| 863 | out.indent([&] { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 864 | out << "return new " |
| 865 | << iface->getPassthroughName() |
Yifan Hong | 341112d | 2017-04-20 18:12:05 -0700 | [diff] [blame] | 866 | << "(static_cast<" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 867 | << iface->localName() |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 868 | << " *>(iIntf));\n"; |
| 869 | }); |
Yifan Hong | b04de38 | 2017-02-06 15:31:52 -0800 | [diff] [blame] | 870 | out << "});\n"; |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 871 | }); |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 872 | }); |
Martijn Coenen | 8adcb65 | 2017-02-03 17:37:36 +0100 | [diff] [blame] | 873 | out << "};\n\n"; |
| 874 | out << "__attribute__((destructor))"; |
| 875 | out << "static void static_destructor() {\n"; |
| 876 | out.indent([&] { |
Yifan Hong | 91977fd | 2017-11-09 16:07:37 -0800 | [diff] [blame] | 877 | out << "::android::hardware::details::getBnConstructorMap().erase(" |
Martijn Coenen | 8adcb65 | 2017-02-03 17:37:36 +0100 | [diff] [blame] | 878 | << iface->localName() |
| 879 | << "::descriptor);\n"; |
Yifan Hong | 91977fd | 2017-11-09 16:07:37 -0800 | [diff] [blame] | 880 | out << "::android::hardware::details::getBsConstructorMap().erase(" |
Martijn Coenen | 8adcb65 | 2017-02-03 17:37:36 +0100 | [diff] [blame] | 881 | << iface->localName() |
| 882 | << "::descriptor);\n"; |
| 883 | }); |
| 884 | out << "};\n\n"; |
Yifan Hong | 158655a | 2016-11-08 12:34:07 -0800 | [diff] [blame] | 885 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 886 | generateInterfaceSource(out); |
| 887 | generateProxySource(out, iface->fqName()); |
| 888 | generateStubSource(out, iface); |
| 889 | generatePassthroughSource(out); |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 890 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 891 | if (isIBase()) { |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 892 | out << "// skipped getService, registerAsService, registerForNotifications\n"; |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 893 | } else { |
Yifan Hong | 83c8e5f | 2016-12-13 14:33:53 -0800 | [diff] [blame] | 894 | std::string package = iface->fqName().package() |
| 895 | + iface->fqName().atVersion(); |
| 896 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 897 | implementServiceManagerInteractions(out, iface->fqName(), package); |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 898 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 899 | } |
| 900 | |
Andreas Huber | 6755e9d | 2017-04-06 11:09:07 -0700 | [diff] [blame] | 901 | HidlTypeAssertion::EmitAll(out); |
| 902 | out << "\n"; |
| 903 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 904 | enterLeaveNamespace(out, false /* enter */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 907 | void AST::generateCheckNonNull(Formatter &out, const std::string &nonNull) { |
Yifan Hong | a018ed5 | 2016-12-13 16:35:08 -0800 | [diff] [blame] | 908 | out.sIf(nonNull + " == nullptr", [&] { |
| 909 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 910 | out.indent(2, [&] { |
Steven Moreland | 610002f | 2017-06-16 13:02:49 -0700 | [diff] [blame] | 911 | out << "::android::hardware::Status::EX_ILLEGAL_ARGUMENT,\n" |
| 912 | << "\"Null synchronous callback passed.\");\n"; |
Yifan Hong | a018ed5 | 2016-12-13 16:35:08 -0800 | [diff] [blame] | 913 | }); |
| 914 | }).endl().endl(); |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 915 | } |
| 916 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 917 | void AST::generateTypeSource(Formatter& out, const std::string& ifaceName) const { |
| 918 | mRootScope.emitTypeDefinitions(out, ifaceName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 921 | void AST::declareCppReaderLocals(Formatter& out, const std::vector<NamedReference<Type>*>& args, |
| 922 | bool forResults) const { |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 923 | if (args.empty()) { |
| 924 | return; |
| 925 | } |
| 926 | |
| 927 | for (const auto &arg : args) { |
| 928 | const Type &type = arg->type(); |
| 929 | |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 930 | out << type.getCppResultType() |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 931 | << " " |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 932 | << (forResults ? "_hidl_out_" : "") + arg->name() |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 933 | << ";\n"; |
| 934 | } |
| 935 | |
| 936 | out << "\n"; |
| 937 | } |
| 938 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 939 | void AST::emitCppReaderWriter(Formatter& out, const std::string& parcelObj, bool parcelObjIsPointer, |
| 940 | const NamedReference<Type>* arg, bool isReader, Type::ErrorMode mode, |
| 941 | bool addPrefixToName) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 942 | const Type &type = arg->type(); |
| 943 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 944 | type.emitReaderWriter( |
| 945 | out, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 946 | addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(), |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 947 | parcelObj, |
| 948 | parcelObjIsPointer, |
| 949 | isReader, |
| 950 | mode); |
| 951 | } |
| 952 | |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 953 | void AST::emitCppResolveReferences(Formatter& out, const std::string& parcelObj, |
| 954 | bool parcelObjIsPointer, const NamedReference<Type>* arg, |
| 955 | bool isReader, Type::ErrorMode mode, |
| 956 | bool addPrefixToName) const { |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 957 | const Type &type = arg->type(); |
| 958 | if(type.needsResolveReferences()) { |
| 959 | type.emitResolveReferences( |
| 960 | out, |
| 961 | addPrefixToName ? ("_hidl_out_" + arg->name()) : arg->name(), |
| 962 | isReader, // nameIsPointer |
| 963 | parcelObj, |
| 964 | parcelObjIsPointer, |
| 965 | isReader, |
| 966 | mode); |
| 967 | } |
| 968 | } |
| 969 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 970 | void AST::generateProxyMethodSource(Formatter& out, const std::string& klassName, |
| 971 | const Method* method, const Interface* superInterface) const { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 972 | method->generateCppSignature(out, |
| 973 | klassName, |
| 974 | true /* specify namespaces */); |
| 975 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 976 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) { |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 977 | out.block([&] { |
| 978 | method->cppImpl(IMPL_PROXY, out); |
| 979 | }).endl().endl(); |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 980 | return; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 981 | } |
| 982 | |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 983 | out.block([&] { |
| 984 | const bool returnsValue = !method->results().empty(); |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 985 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 986 | |
| 987 | method->generateCppReturnType(out); |
| 988 | |
| 989 | out << " _hidl_out = " |
| 990 | << superInterface->fqName().cppNamespace() |
| 991 | << "::" |
| 992 | << superInterface->getProxyName() |
| 993 | << "::_hidl_" |
| 994 | << method->name() |
| 995 | << "(this, this"; |
| 996 | |
| 997 | if (!method->hasEmptyCppArgSignature()) { |
| 998 | out << ", "; |
| 999 | } |
| 1000 | |
| 1001 | out.join(method->args().begin(), method->args().end(), ", ", [&](const auto &arg) { |
| 1002 | out << arg->name(); |
| 1003 | }); |
| 1004 | |
| 1005 | if (returnsValue && elidedReturn == nullptr) { |
| 1006 | if (!method->args().empty()) { |
| 1007 | out << ", "; |
| 1008 | } |
| 1009 | out << "_hidl_cb"; |
| 1010 | } |
| 1011 | |
| 1012 | out << ");\n\n"; |
| 1013 | |
| 1014 | out << "return _hidl_out;\n"; |
| 1015 | }).endl().endl(); |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1018 | void AST::generateStaticProxyMethodSource(Formatter& out, const std::string& klassName, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1019 | const Method* method, const Interface* superInterface) const { |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1020 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_PROXY)) { |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1021 | return; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | method->generateCppReturnType(out); |
| 1025 | |
| 1026 | out << klassName |
| 1027 | << "::_hidl_" |
| 1028 | << method->name() |
| 1029 | << "(" |
| 1030 | << "::android::hardware::IInterface *_hidl_this, " |
| 1031 | << "::android::hardware::details::HidlInstrumentor *_hidl_this_instrumentor"; |
| 1032 | |
| 1033 | if (!method->hasEmptyCppArgSignature()) { |
| 1034 | out << ", "; |
| 1035 | } |
| 1036 | |
| 1037 | method->emitCppArgSignature(out); |
| 1038 | out << ") {\n"; |
| 1039 | |
| 1040 | out.indent(); |
| 1041 | |
| 1042 | out << "#ifdef __ANDROID_DEBUGGABLE__\n"; |
| 1043 | out << "bool mEnableInstrumentation = _hidl_this_instrumentor->isInstrumentationEnabled();\n"; |
| 1044 | out << "const auto &mInstrumentationCallbacks = _hidl_this_instrumentor->getInstrumentationCallbacks();\n"; |
| 1045 | out << "#else\n"; |
| 1046 | out << "(void) _hidl_this_instrumentor;\n"; |
| 1047 | out << "#endif // __ANDROID_DEBUGGABLE__\n"; |
| 1048 | |
| 1049 | const bool returnsValue = !method->results().empty(); |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 1050 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1051 | if (returnsValue && elidedReturn == nullptr) { |
| 1052 | generateCheckNonNull(out, "_hidl_cb"); |
| 1053 | } |
| 1054 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1055 | generateCppInstrumentationCall( |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1056 | out, |
| 1057 | InstrumentationEvent::CLIENT_API_ENTRY, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1058 | method, |
| 1059 | superInterface); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1060 | |
| 1061 | out << "::android::hardware::Parcel _hidl_data;\n"; |
| 1062 | out << "::android::hardware::Parcel _hidl_reply;\n"; |
| 1063 | out << "::android::status_t _hidl_err;\n"; |
| 1064 | out << "::android::hardware::Status _hidl_status;\n\n"; |
| 1065 | |
| 1066 | declareCppReaderLocals( |
| 1067 | out, method->results(), true /* forResults */); |
| 1068 | |
| 1069 | out << "_hidl_err = _hidl_data.writeInterfaceToken("; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1070 | out << klassName; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1071 | out << "::descriptor);\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1072 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
| 1073 | |
Martijn Coenen | fff7335 | 2017-01-04 16:36:31 +0100 | [diff] [blame] | 1074 | bool hasInterfaceArgument = false; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1075 | // First DFS: write all buffers and resolve pointers for parent |
| 1076 | for (const auto &arg : method->args()) { |
Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 1077 | if (arg->type().isInterface()) { |
| 1078 | hasInterfaceArgument = true; |
| 1079 | } |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1080 | emitCppReaderWriter( |
| 1081 | out, |
| 1082 | "_hidl_data", |
| 1083 | false /* parcelObjIsPointer */, |
| 1084 | arg, |
| 1085 | false /* reader */, |
| 1086 | Type::ErrorMode_Goto, |
| 1087 | false /* addPrefixToName */); |
| 1088 | } |
| 1089 | |
| 1090 | // Second DFS: resolve references. |
| 1091 | for (const auto &arg : method->args()) { |
| 1092 | emitCppResolveReferences( |
| 1093 | out, |
| 1094 | "_hidl_data", |
| 1095 | false /* parcelObjIsPointer */, |
| 1096 | arg, |
| 1097 | false /* reader */, |
| 1098 | Type::ErrorMode_Goto, |
| 1099 | false /* addPrefixToName */); |
| 1100 | } |
| 1101 | |
Martijn Coenen | fa55d6e | 2016-12-20 06:08:31 +0100 | [diff] [blame] | 1102 | if (hasInterfaceArgument) { |
| 1103 | // Start binder threadpool to handle incoming transactions |
| 1104 | out << "::android::hardware::ProcessState::self()->startThreadPool();\n"; |
| 1105 | } |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1106 | out << "_hidl_err = ::android::hardware::IInterface::asBinder(_hidl_this)->transact(" |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1107 | << method->getSerialId() |
| 1108 | << " /* " |
| 1109 | << method->name() |
| 1110 | << " */, _hidl_data, &_hidl_reply"; |
| 1111 | |
| 1112 | if (method->isOneway()) { |
Steven Moreland | 7794369 | 2018-08-09 12:53:42 -0700 | [diff] [blame] | 1113 | out << ", " << Interface::FLAG_ONE_WAY->cppValue(); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1114 | } |
| 1115 | out << ");\n"; |
| 1116 | |
| 1117 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
| 1118 | |
| 1119 | if (!method->isOneway()) { |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1120 | out << "_hidl_err = ::android::hardware::readFromParcel(&_hidl_status, _hidl_reply);\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1121 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
| 1122 | out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n"; |
| 1123 | |
| 1124 | |
| 1125 | // First DFS: write all buffers and resolve pointers for parent |
| 1126 | for (const auto &arg : method->results()) { |
| 1127 | emitCppReaderWriter( |
| 1128 | out, |
| 1129 | "_hidl_reply", |
| 1130 | false /* parcelObjIsPointer */, |
| 1131 | arg, |
| 1132 | true /* reader */, |
| 1133 | Type::ErrorMode_Goto, |
| 1134 | true /* addPrefixToName */); |
| 1135 | } |
| 1136 | |
| 1137 | // Second DFS: resolve references. |
| 1138 | for (const auto &arg : method->results()) { |
| 1139 | emitCppResolveReferences( |
| 1140 | out, |
| 1141 | "_hidl_reply", |
| 1142 | false /* parcelObjIsPointer */, |
| 1143 | arg, |
| 1144 | true /* reader */, |
| 1145 | Type::ErrorMode_Goto, |
| 1146 | true /* addPrefixToName */); |
| 1147 | } |
| 1148 | |
| 1149 | if (returnsValue && elidedReturn == nullptr) { |
| 1150 | out << "_hidl_cb("; |
| 1151 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1152 | out.join(method->results().begin(), method->results().end(), ", ", [&] (const auto &arg) { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1153 | if (arg->type().resultNeedsDeref()) { |
| 1154 | out << "*"; |
| 1155 | } |
| 1156 | out << "_hidl_out_" << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1157 | }); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1158 | |
| 1159 | out << ");\n\n"; |
| 1160 | } |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1161 | } |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1162 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1163 | generateCppInstrumentationCall( |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1164 | out, |
| 1165 | InstrumentationEvent::CLIENT_API_EXIT, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1166 | method, |
| 1167 | superInterface); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1168 | |
| 1169 | if (elidedReturn != nullptr) { |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1170 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1171 | out << "return ::android::hardware::Return<"; |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1172 | out << elidedReturn->type().getCppResultType() |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1173 | << ">(_hidl_out_" << elidedReturn->name() << ");\n\n"; |
| 1174 | } else { |
| 1175 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1176 | out << "return ::android::hardware::Return<void>();\n\n"; |
| 1177 | } |
| 1178 | |
| 1179 | out.unindent(); |
| 1180 | out << "_hidl_error:\n"; |
| 1181 | out.indent(); |
| 1182 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1183 | out << "return ::android::hardware::Return<"; |
| 1184 | if (elidedReturn != nullptr) { |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1185 | out << method->results().at(0)->type().getCppResultType(); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1186 | } else { |
| 1187 | out << "void"; |
| 1188 | } |
| 1189 | out << ">(_hidl_status);\n"; |
| 1190 | |
| 1191 | out.unindent(); |
| 1192 | out << "}\n\n"; |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1195 | void AST::generateProxySource(Formatter& out, const FQName& fqName) const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1196 | const std::string klassName = fqName.getInterfaceProxyName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1197 | |
| 1198 | out << klassName |
| 1199 | << "::" |
| 1200 | << klassName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1201 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1202 | |
| 1203 | out.indent(); |
| 1204 | out.indent(); |
| 1205 | |
| 1206 | out << ": BpInterface" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1207 | << "<" |
| 1208 | << fqName.getInterfaceName() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1209 | << ">(_hidl_impl),\n" |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1210 | << " ::android::hardware::details::HidlInstrumentor(\"" |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1211 | << mPackage.string() |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1212 | << "\", \"" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1213 | << fqName.getInterfaceName() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1214 | << "\") {\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1215 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1216 | out.unindent(); |
| 1217 | out.unindent(); |
| 1218 | out << "}\n\n"; |
| 1219 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1220 | generateMethods(out, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1221 | [&](const Method* method, const Interface* superInterface) { |
| 1222 | generateStaticProxyMethodSource(out, klassName, method, superInterface); |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1223 | }, |
| 1224 | false /* include parents */); |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1225 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1226 | generateMethods(out, [&](const Method* method, const Interface* superInterface) { |
| 1227 | generateProxyMethodSource(out, klassName, method, superInterface); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1228 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1229 | } |
| 1230 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1231 | void AST::generateStubSource(Formatter& out, const Interface* iface) const { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1232 | const std::string interfaceName = iface->localName(); |
| 1233 | const std::string klassName = iface->getStubName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1234 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1235 | out << klassName |
| 1236 | << "::" |
| 1237 | << klassName |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1238 | << "(const ::android::sp<" << interfaceName <<"> &_hidl_impl)\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1239 | |
| 1240 | out.indent(); |
| 1241 | out.indent(); |
| 1242 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1243 | if (iface->isIBase()) { |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1244 | out << ": ::android::hardware::details::HidlInstrumentor(\""; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1245 | } else { |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1246 | out << ": " |
| 1247 | << gIBaseFqName.getInterfaceStubFqName().cppName() |
| 1248 | << "(_hidl_impl, \""; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | out << mPackage.string() |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1252 | << "\", \"" |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1253 | << interfaceName |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1254 | << "\") { \n"; |
| 1255 | out.indent(); |
| 1256 | out << "_hidl_mImpl = _hidl_impl;\n"; |
Martijn Coenen | b4d7795 | 2017-05-03 13:44:29 -0700 | [diff] [blame] | 1257 | out << "auto prio = ::android::hardware::details::gServicePrioMap.get(" |
| 1258 | << "_hidl_impl, {SCHED_NORMAL, 0});\n"; |
| 1259 | out << "mSchedPolicy = prio.sched_policy;\n"; |
| 1260 | out << "mSchedPriority = prio.prio;\n"; |
Steven Moreland | 1209ed7 | 2019-01-09 18:00:46 -0800 | [diff] [blame] | 1261 | out << "setRequestingSid(::android::hardware::details::gServiceSidMap.get(_hidl_impl, false));\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1262 | out.unindent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1263 | |
| 1264 | out.unindent(); |
| 1265 | out.unindent(); |
| 1266 | out << "}\n\n"; |
| 1267 | |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1268 | if (iface->isIBase()) { |
Yifan Hong | 01e7cde | 2017-01-09 17:45:45 -0800 | [diff] [blame] | 1269 | // BnHwBase has a constructor to initialize the HidlInstrumentor |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1270 | // class properly. |
| 1271 | out << klassName |
| 1272 | << "::" |
| 1273 | << klassName |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1274 | << "(const ::android::sp<" << interfaceName << "> &_hidl_impl," |
| 1275 | << " const std::string &HidlInstrumentor_package," |
| 1276 | << " const std::string &HidlInstrumentor_interface)\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1277 | |
| 1278 | out.indent(); |
| 1279 | out.indent(); |
| 1280 | |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1281 | out << ": ::android::hardware::details::HidlInstrumentor(" |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1282 | << "HidlInstrumentor_package, HidlInstrumentor_interface) {\n"; |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1283 | out.indent(); |
| 1284 | out << "_hidl_mImpl = _hidl_impl;\n"; |
| 1285 | out.unindent(); |
| 1286 | |
| 1287 | out.unindent(); |
| 1288 | out.unindent(); |
| 1289 | out << "}\n\n"; |
| 1290 | } |
| 1291 | |
Steven Moreland | 57a8936 | 2017-07-21 19:29:54 +0000 | [diff] [blame] | 1292 | out << klassName << "::~" << klassName << "() "; |
| 1293 | out.block([&]() { |
Martijn Coenen | ce6fd19 | 2017-07-27 13:24:34 +0200 | [diff] [blame] | 1294 | out << "::android::hardware::details::gBnMap.eraseIfEqual(_hidl_mImpl.get(), this);\n"; |
Steven Moreland | 57a8936 | 2017-07-21 19:29:54 +0000 | [diff] [blame] | 1295 | }).endl().endl(); |
| 1296 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1297 | generateMethods(out, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1298 | [&](const Method* method, const Interface* superInterface) { |
| 1299 | return generateStaticStubMethodSource(out, iface->fqName(), method, superInterface); |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1300 | }, |
| 1301 | false /* include parents */); |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1302 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1303 | generateMethods(out, [&](const Method* method, const Interface*) { |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 1304 | if (!method->isHidlReserved() || !method->overridesCppImpl(IMPL_STUB_IMPL)) { |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1305 | return; |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 1306 | } |
| 1307 | method->generateCppSignature(out, iface->getStubName()); |
| 1308 | out << " "; |
| 1309 | out.block([&] { |
| 1310 | method->cppImpl(IMPL_STUB_IMPL, out); |
| 1311 | }).endl(); |
Yifan Hong | bcffce2 | 2017-02-01 15:52:06 -0800 | [diff] [blame] | 1312 | }); |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1313 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1314 | out << "::android::status_t " << klassName << "::onTransact(\n"; |
| 1315 | |
| 1316 | out.indent(); |
| 1317 | out.indent(); |
| 1318 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1319 | out << "uint32_t _hidl_code,\n" |
| 1320 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 1321 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 1322 | << "uint32_t _hidl_flags,\n" |
| 1323 | << "TransactCallback _hidl_cb) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1324 | |
| 1325 | out.unindent(); |
| 1326 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1327 | out << "::android::status_t _hidl_err = ::android::OK;\n\n"; |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1328 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1329 | out.indent(); |
| 1330 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1331 | for (const auto &tuple : iface->allMethodsFromRoot()) { |
| 1332 | const Method *method = tuple.method(); |
| 1333 | const Interface *superInterface = tuple.interface(); |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1334 | |
Howard Chen | 71f289f | 2017-08-29 17:35:01 +0800 | [diff] [blame] | 1335 | if (!isIBase() && method->isHidlReserved()) { |
| 1336 | continue; |
| 1337 | } |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1338 | out << "case " |
| 1339 | << method->getSerialId() |
| 1340 | << " /* " |
| 1341 | << method->name() |
| 1342 | << " */:\n{\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1343 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1344 | out.indent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1345 | |
Steven Moreland | 7794369 | 2018-08-09 12:53:42 -0700 | [diff] [blame] | 1346 | out << "bool _hidl_is_oneway = _hidl_flags & " << Interface::FLAG_ONE_WAY->cppValue() |
| 1347 | << ";\n"; |
Steven Moreland | ebd8c72 | 2017-11-03 14:47:32 -0700 | [diff] [blame] | 1348 | out << "if (_hidl_is_oneway != " << (method->isOneway() ? "true" : "false") << ") "; |
| 1349 | out.block([&] { out << "return ::android::UNKNOWN_ERROR;\n"; }).endl().endl(); |
| 1350 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1351 | generateStubSourceForMethod(out, method, superInterface); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 1352 | |
| 1353 | out.unindent(); |
| 1354 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | out << "default:\n{\n"; |
| 1358 | out.indent(); |
| 1359 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1360 | if (iface->isIBase()) { |
| 1361 | out << "(void)_hidl_flags;\n"; |
| 1362 | out << "return ::android::UNKNOWN_TRANSACTION;\n"; |
| 1363 | } else { |
| 1364 | out << "return "; |
| 1365 | out << gIBaseFqName.getInterfaceStubFqName().cppName(); |
| 1366 | out << "::onTransact(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1367 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1368 | out.indent(); |
| 1369 | out.indent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1370 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1371 | out << "_hidl_code, _hidl_data, _hidl_reply, " |
| 1372 | << "_hidl_flags, _hidl_cb);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1373 | |
Martijn Coenen | 225bc92 | 2017-06-27 14:39:46 -0700 | [diff] [blame] | 1374 | out.unindent(); |
| 1375 | out.unindent(); |
| 1376 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1377 | |
| 1378 | out.unindent(); |
| 1379 | out << "}\n"; |
| 1380 | |
| 1381 | out.unindent(); |
| 1382 | out << "}\n\n"; |
| 1383 | |
Yifan Hong | a018ed5 | 2016-12-13 16:35:08 -0800 | [diff] [blame] | 1384 | out.sIf("_hidl_err == ::android::UNEXPECTED_NULL", [&] { |
| 1385 | out << "_hidl_err = ::android::hardware::writeToParcel(\n"; |
| 1386 | out.indent(2, [&] { |
| 1387 | out << "::android::hardware::Status::fromExceptionCode(::android::hardware::Status::EX_NULL_POINTER),\n"; |
| 1388 | out << "_hidl_reply);\n"; |
| 1389 | }); |
| 1390 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1391 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1392 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1393 | |
| 1394 | out.unindent(); |
| 1395 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1396 | } |
| 1397 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1398 | void AST::generateStubSourceForMethod(Formatter& out, const Method* method, |
| 1399 | const Interface* superInterface) const { |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 1400 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) { |
| 1401 | method->cppImpl(IMPL_STUB, out); |
| 1402 | out << "break;\n"; |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1403 | return; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1406 | out << "_hidl_err = " |
| 1407 | << superInterface->fqName().cppNamespace() |
| 1408 | << "::" |
| 1409 | << superInterface->getStubName() |
| 1410 | << "::_hidl_" |
| 1411 | << method->name() |
| 1412 | << "(this, _hidl_data, _hidl_reply, _hidl_cb);\n"; |
| 1413 | out << "break;\n"; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1416 | void AST::generateStaticStubMethodSource(Formatter& out, const FQName& fqName, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1417 | const Method* method, const Interface* superInterface) const { |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1418 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB)) { |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1419 | return; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
Steven Moreland | f819790 | 2018-01-30 15:38:37 -0800 | [diff] [blame] | 1422 | const std::string& klassName = fqName.getInterfaceStubName(); |
| 1423 | |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1424 | out << "::android::status_t " << klassName << "::_hidl_" << method->name() << "(\n"; |
| 1425 | |
| 1426 | out.indent(); |
| 1427 | out.indent(); |
| 1428 | |
| 1429 | out << "::android::hidl::base::V1_0::BnHwBase* _hidl_this,\n" |
| 1430 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 1431 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 1432 | << "TransactCallback _hidl_cb) {\n"; |
| 1433 | |
| 1434 | out.unindent(); |
| 1435 | |
| 1436 | out << "#ifdef __ANDROID_DEBUGGABLE__\n"; |
| 1437 | out << "bool mEnableInstrumentation = _hidl_this->isInstrumentationEnabled();\n"; |
| 1438 | out << "const auto &mInstrumentationCallbacks = _hidl_this->getInstrumentationCallbacks();\n"; |
| 1439 | out << "#endif // __ANDROID_DEBUGGABLE__\n\n"; |
| 1440 | |
| 1441 | out << "::android::status_t _hidl_err = ::android::OK;\n"; |
| 1442 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1443 | out << "if (!_hidl_data.enforceInterface(" |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1444 | << klassName |
| 1445 | << "::Pure::descriptor)) {\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1446 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1447 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1448 | out << "_hidl_err = ::android::BAD_TYPE;\n"; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1449 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1450 | out.unindent(); |
| 1451 | out << "}\n\n"; |
| 1452 | |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1453 | declareCppReaderLocals(out, method->args(), false /* forResults */); |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1454 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1455 | // First DFS: write buffers |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1456 | for (const auto &arg : method->args()) { |
| 1457 | emitCppReaderWriter( |
| 1458 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1459 | "_hidl_data", |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1460 | false /* parcelObjIsPointer */, |
| 1461 | arg, |
| 1462 | true /* reader */, |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1463 | Type::ErrorMode_Return, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1464 | false /* addPrefixToName */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1467 | // Second DFS: resolve references |
| 1468 | for (const auto &arg : method->args()) { |
| 1469 | emitCppResolveReferences( |
| 1470 | out, |
| 1471 | "_hidl_data", |
| 1472 | false /* parcelObjIsPointer */, |
| 1473 | arg, |
| 1474 | true /* reader */, |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1475 | Type::ErrorMode_Return, |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1476 | false /* addPrefixToName */); |
| 1477 | } |
| 1478 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1479 | generateCppInstrumentationCall( |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1480 | out, |
| 1481 | InstrumentationEvent::SERVER_API_ENTRY, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1482 | method, |
| 1483 | superInterface); |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1484 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1485 | const bool returnsValue = !method->results().empty(); |
Timur Iskhakov | 7fa79f6 | 2017-08-09 11:04:54 -0700 | [diff] [blame] | 1486 | const NamedReference<Type>* elidedReturn = method->canElideCallback(); |
Steven Moreland | 3e78700 | 2017-08-16 14:59:54 -0700 | [diff] [blame] | 1487 | |
| 1488 | std::string callee; |
| 1489 | |
| 1490 | if (method->isHidlReserved() && method->overridesCppImpl(IMPL_STUB_IMPL)) { |
| 1491 | callee = "_hidl_this"; |
| 1492 | } else { |
Steven Moreland | f819790 | 2018-01-30 15:38:37 -0800 | [diff] [blame] | 1493 | callee = "static_cast<" + fqName.getInterfaceName() + "*>(_hidl_this->getImpl().get())"; |
Steven Moreland | 3e78700 | 2017-08-16 14:59:54 -0700 | [diff] [blame] | 1494 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1495 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1496 | if (elidedReturn != nullptr) { |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1497 | out << elidedReturn->type().getCppResultType() |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1498 | << " _hidl_out_" |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1499 | << elidedReturn->name() |
Martijn Coenen | 6ec2f0b | 2016-12-11 01:04:55 +0100 | [diff] [blame] | 1500 | << " = " |
Yifan Hong | cd2ae45 | 2017-01-31 14:33:40 -0800 | [diff] [blame] | 1501 | << callee << "->" << method->name() |
Yifan Hong | 3b320f8 | 2016-11-01 15:15:54 -0700 | [diff] [blame] | 1502 | << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1503 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1504 | out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1505 | if (arg->type().resultNeedsDeref()) { |
| 1506 | out << "*"; |
| 1507 | } |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1508 | out << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1509 | }); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1510 | |
Steven Moreland | 2ae5bca | 2016-12-01 05:56:49 +0000 | [diff] [blame] | 1511 | out << ");\n\n"; |
Steven Moreland | 1e497e0 | 2019-03-05 19:39:10 -0800 | [diff] [blame^] | 1512 | |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1513 | out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), " |
| 1514 | << "_hidl_reply);\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1515 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1516 | elidedReturn->type().emitReaderWriter( |
| 1517 | out, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1518 | "_hidl_out_" + elidedReturn->name(), |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1519 | "_hidl_reply", |
| 1520 | true, /* parcelObjIsPointer */ |
| 1521 | false, /* isReader */ |
| 1522 | Type::ErrorMode_Ignore); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1523 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1524 | emitCppResolveReferences( |
| 1525 | out, |
| 1526 | "_hidl_reply", |
| 1527 | true /* parcelObjIsPointer */, |
| 1528 | elidedReturn, |
| 1529 | false /* reader */, |
| 1530 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1531 | true /* addPrefixToName */); |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1532 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1533 | generateCppInstrumentationCall( |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1534 | out, |
| 1535 | InstrumentationEvent::SERVER_API_EXIT, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1536 | method, |
| 1537 | superInterface); |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1538 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1539 | out << "_hidl_cb(*_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1540 | } else { |
| 1541 | if (returnsValue) { |
| 1542 | out << "bool _hidl_callbackCalled = false;\n\n"; |
| 1543 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1544 | |
Steven Moreland | 1e497e0 | 2019-03-05 19:39:10 -0800 | [diff] [blame^] | 1545 | out << "::android::hardware::Return<void> _hidl_ret = " << callee << "->" << method->name() |
| 1546 | << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1547 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1548 | out.join(method->args().begin(), method->args().end(), ", ", [&] (const auto &arg) { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1549 | if (arg->type().resultNeedsDeref()) { |
| 1550 | out << "*"; |
| 1551 | } |
| 1552 | |
| 1553 | out << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1554 | }); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1555 | |
| 1556 | if (returnsValue) { |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1557 | if (!method->args().empty()) { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1558 | out << ", "; |
| 1559 | } |
| 1560 | |
| 1561 | out << "[&]("; |
| 1562 | |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1563 | out.join(method->results().begin(), method->results().end(), ", ", [&](const auto &arg) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1564 | out << "const auto &_hidl_out_" << arg->name(); |
Yifan Hong | 932464e | 2017-03-30 15:40:22 -0700 | [diff] [blame] | 1565 | }); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1566 | |
| 1567 | out << ") {\n"; |
| 1568 | out.indent(); |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1569 | out << "if (_hidl_callbackCalled) {\n"; |
| 1570 | out.indent(); |
| 1571 | out << "LOG_ALWAYS_FATAL(\"" |
| 1572 | << method->name() |
| 1573 | << ": _hidl_cb called a second time, but must be called once.\");\n"; |
| 1574 | out.unindent(); |
| 1575 | out << "}\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1576 | out << "_hidl_callbackCalled = true;\n\n"; |
| 1577 | |
Yifan Hong | 859e53f | 2016-11-14 19:08:24 -0800 | [diff] [blame] | 1578 | out << "::android::hardware::writeToParcel(::android::hardware::Status::ok(), " |
| 1579 | << "_hidl_reply);\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1580 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1581 | // First DFS: buffers |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1582 | for (const auto &arg : method->results()) { |
| 1583 | emitCppReaderWriter( |
| 1584 | out, |
| 1585 | "_hidl_reply", |
| 1586 | true /* parcelObjIsPointer */, |
| 1587 | arg, |
| 1588 | false /* reader */, |
Andreas Huber | 5e44a29 | 2016-09-27 14:52:39 -0700 | [diff] [blame] | 1589 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1590 | true /* addPrefixToName */); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1591 | } |
| 1592 | |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1593 | // Second DFS: resolve references |
| 1594 | for (const auto &arg : method->results()) { |
| 1595 | emitCppResolveReferences( |
| 1596 | out, |
| 1597 | "_hidl_reply", |
| 1598 | true /* parcelObjIsPointer */, |
| 1599 | arg, |
| 1600 | false /* reader */, |
| 1601 | Type::ErrorMode_Ignore, |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1602 | true /* addPrefixToName */); |
Yifan Hong | bf459bc | 2016-08-23 16:50:37 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1605 | generateCppInstrumentationCall( |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1606 | out, |
| 1607 | InstrumentationEvent::SERVER_API_EXIT, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1608 | method, |
| 1609 | superInterface); |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame] | 1610 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1611 | out << "_hidl_cb(*_hidl_reply);\n"; |
| 1612 | |
| 1613 | out.unindent(); |
Martijn Coenen | 8e4fc84 | 2017-01-09 16:28:59 +0100 | [diff] [blame] | 1614 | out << "});\n\n"; |
| 1615 | } else { |
| 1616 | out << ");\n\n"; |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1617 | out << "(void) _hidl_cb;\n\n"; |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1618 | generateCppInstrumentationCall( |
Martijn Coenen | 8e4fc84 | 2017-01-09 16:28:59 +0100 | [diff] [blame] | 1619 | out, |
| 1620 | InstrumentationEvent::SERVER_API_EXIT, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1621 | method, |
| 1622 | superInterface); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1623 | } |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1624 | |
Steven Moreland | 1e497e0 | 2019-03-05 19:39:10 -0800 | [diff] [blame^] | 1625 | out << "_hidl_ret.assertOk();\n"; |
| 1626 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1627 | if (returnsValue) { |
| 1628 | out << "if (!_hidl_callbackCalled) {\n"; |
| 1629 | out.indent(); |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1630 | out << "LOG_ALWAYS_FATAL(\"" |
| 1631 | << method->name() |
| 1632 | << ": _hidl_cb not called, but must be called once.\");\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1633 | out.unindent(); |
| 1634 | out << "}\n\n"; |
Steven Moreland | 05cd423 | 2016-11-21 16:01:12 -0800 | [diff] [blame] | 1635 | } else { |
| 1636 | out << "::android::hardware::writeToParcel(" |
| 1637 | << "::android::hardware::Status::ok(), " |
| 1638 | << "_hidl_reply);\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1639 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
Steven Moreland | f16c5c0 | 2017-07-31 16:50:06 -0700 | [diff] [blame] | 1642 | out << "return _hidl_err;\n"; |
| 1643 | out.unindent(); |
| 1644 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1647 | void AST::generatePassthroughHeader(Formatter& out) const { |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1648 | if (!AST::isInterface()) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1649 | // types.hal does not get a stub header. |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1650 | return; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 1653 | const Interface* iface = mRootScope.getInterface(); |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1654 | CHECK(iface != nullptr); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1655 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1656 | const std::string klassName = iface->getPassthroughName(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1657 | |
| 1658 | bool supportOneway = iface->hasOnewayMethods(); |
| 1659 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1660 | const std::string guard = makeHeaderGuard(klassName); |
| 1661 | |
| 1662 | out << "#ifndef " << guard << "\n"; |
| 1663 | out << "#define " << guard << "\n\n"; |
| 1664 | |
| 1665 | std::vector<std::string> packageComponents; |
| 1666 | getPackageAndVersionComponents( |
| 1667 | &packageComponents, false /* cpp_compatible */); |
| 1668 | |
Steven Moreland | 61d3f4b | 2017-04-28 17:30:38 -0700 | [diff] [blame] | 1669 | out << "#include <android-base/macros.h>\n"; |
Yifan Hong | b094943 | 2016-12-15 15:32:24 -0800 | [diff] [blame] | 1670 | out << "#include <cutils/trace.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1671 | out << "#include <future>\n"; |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 1672 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1673 | generateCppPackageInclude(out, mPackage, iface->localName()); |
Steven Moreland | ee88eed | 2016-10-31 17:49:00 -0700 | [diff] [blame] | 1674 | out << "\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1675 | |
Yifan Hong | 7a118f5 | 2016-12-07 11:21:15 -0800 | [diff] [blame] | 1676 | out << "#include <hidl/HidlPassthroughSupport.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1677 | if (supportOneway) { |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1678 | out << "#include <hidl/TaskRunner.h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | enterLeaveNamespace(out, true /* enter */); |
| 1682 | out << "\n"; |
| 1683 | |
| 1684 | out << "struct " |
| 1685 | << klassName |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1686 | << " : " << iface->localName() |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1687 | << ", ::android::hardware::details::HidlInstrumentor {\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1688 | |
| 1689 | out.indent(); |
| 1690 | out << "explicit " |
| 1691 | << klassName |
Steven Moreland | c46e984 | 2016-11-02 13:21:26 -0700 | [diff] [blame] | 1692 | << "(const ::android::sp<" |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1693 | << iface->localName() |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1694 | << "> impl);\n"; |
| 1695 | |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 1696 | out.endl(); |
| 1697 | generateTemplatizationLink(out); |
Steven Moreland | 1a52e82 | 2017-07-27 13:56:29 -0700 | [diff] [blame] | 1698 | generateCppTag(out, "android::hardware::details::bs_tag"); |
Steven Moreland | 0b84377 | 2017-06-23 16:33:38 -0700 | [diff] [blame] | 1699 | |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1700 | generateMethods(out, [&](const Method* method, const Interface* superInterface) { |
| 1701 | generatePassthroughMethod(out, method, superInterface); |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 1702 | }); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1703 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1704 | out.unindent(); |
| 1705 | out << "private:\n"; |
| 1706 | out.indent(); |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 1707 | out << "const ::android::sp<" << iface->localName() << "> mImpl;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1708 | |
| 1709 | if (supportOneway) { |
Yifan Hong | ef91d36 | 2017-03-20 17:18:13 -0700 | [diff] [blame] | 1710 | out << "::android::hardware::details::TaskRunner mOnewayQueue;\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1711 | |
| 1712 | out << "\n"; |
| 1713 | |
| 1714 | out << "::android::hardware::Return<void> addOnewayTask(" |
| 1715 | "std::function<void(void)>);\n\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | out.unindent(); |
| 1719 | |
| 1720 | out << "};\n\n"; |
| 1721 | |
| 1722 | enterLeaveNamespace(out, false /* enter */); |
| 1723 | |
| 1724 | out << "\n#endif // " << guard << "\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1725 | } |
| 1726 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1727 | void AST::generateInterfaceSource(Formatter& out) const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 1728 | const Interface* iface = mRootScope.getInterface(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1729 | |
Yifan Hong | 2d7126b | 2016-10-20 15:12:57 -0700 | [diff] [blame] | 1730 | // generate castFrom functions |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1731 | std::string childTypeResult = iface->getCppResultType(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1732 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1733 | generateMethods(out, [&](const Method* method, const Interface*) { |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 1734 | bool reserved = method->isHidlReserved(); |
| 1735 | |
| 1736 | if (!reserved) { |
| 1737 | out << "// no default implementation for: "; |
| 1738 | } |
| 1739 | method->generateCppSignature(out, iface->localName()); |
| 1740 | if (reserved) { |
| 1741 | out.block([&]() { |
Steven Moreland | 937408a | 2017-03-20 09:54:18 -0700 | [diff] [blame] | 1742 | method->cppImpl(IMPL_INTERFACE, out); |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 1743 | }).endl(); |
| 1744 | } |
| 1745 | |
| 1746 | out << "\n"; |
| 1747 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1748 | return; |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 1749 | }); |
Steven Moreland | d4b068a | 2017-03-20 06:30:51 -0700 | [diff] [blame] | 1750 | |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1751 | for (const Interface *superType : iface->typeChain()) { |
Steven Moreland | 23cc5fa | 2018-05-09 10:48:48 -0700 | [diff] [blame] | 1752 | out << "::android::hardware::Return<" |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1753 | << childTypeResult |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 1754 | << "> " |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1755 | << iface->localName() |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1756 | << "::castFrom(" |
| 1757 | << superType->getCppArgumentType() |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 1758 | << " parent, bool " |
| 1759 | << (iface == superType ? "/* emitError */" : "emitError") |
| 1760 | << ") {\n"; |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1761 | out.indent(); |
| 1762 | if (iface == superType) { |
| 1763 | out << "return parent;\n"; |
| 1764 | } else { |
Yifan Hong | 33e7801 | 2017-03-13 17:46:33 -0700 | [diff] [blame] | 1765 | out << "return ::android::hardware::details::castInterface<"; |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1766 | out << iface->localName() << ", " |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1767 | << superType->fqName().cppName() << ", " |
Steven Moreland | 57a8936 | 2017-07-21 19:29:54 +0000 | [diff] [blame] | 1768 | << iface->getProxyName() |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1769 | << ">(\n"; |
| 1770 | out.indent(); |
| 1771 | out.indent(); |
| 1772 | out << "parent, \"" |
| 1773 | << iface->fqName().string() |
Yifan Hong | 200209c | 2017-03-29 03:39:09 -0700 | [diff] [blame] | 1774 | << "\", emitError);\n"; |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1775 | out.unindent(); |
| 1776 | out.unindent(); |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1777 | } |
Yifan Hong | 3d74609 | 2016-12-07 14:26:33 -0800 | [diff] [blame] | 1778 | out.unindent(); |
| 1779 | out << "}\n\n"; |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1780 | } |
Yifan Hong | fe95aa2 | 2016-10-19 17:26:45 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Steven Moreland | 368e460 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1783 | void AST::generatePassthroughSource(Formatter& out) const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 1784 | const Interface* iface = mRootScope.getInterface(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1785 | |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1786 | const std::string klassName = iface->getPassthroughName(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1787 | |
| 1788 | out << klassName |
| 1789 | << "::" |
| 1790 | << klassName |
Steven Moreland | c46e984 | 2016-11-02 13:21:26 -0700 | [diff] [blame] | 1791 | << "(const ::android::sp<" |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1792 | << iface->fullName() |
Zhuoyao Zhang | 7d3ac80 | 2017-02-15 21:05:49 +0000 | [diff] [blame] | 1793 | << "> impl) : ::android::hardware::details::HidlInstrumentor(\"" |
Zhuoyao Zhang | d10feea | 2017-01-23 17:29:58 -0800 | [diff] [blame] | 1794 | << mPackage.string() |
| 1795 | << "\", \"" |
| 1796 | << iface->localName() |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 1797 | << "\"), mImpl(impl) {"; |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1798 | if (iface->hasOnewayMethods()) { |
| 1799 | out << "\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 1800 | out.indent([&] { |
Yifan Hong | f01dad4 | 2017-03-20 19:03:11 -0700 | [diff] [blame] | 1801 | out << "mOnewayQueue.start(3000 /* similar limit to binderized */);\n"; |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1802 | }); |
| 1803 | } |
| 1804 | out << "}\n\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1805 | |
| 1806 | if (iface->hasOnewayMethods()) { |
| 1807 | out << "::android::hardware::Return<void> " |
| 1808 | << klassName |
| 1809 | << "::addOnewayTask(std::function<void(void)> fun) {\n"; |
| 1810 | out.indent(); |
Yifan Hong | 2cbc147 | 2016-10-25 19:02:40 -0700 | [diff] [blame] | 1811 | out << "if (!mOnewayQueue.push(fun)) {\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1812 | out.indent(); |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 1813 | out << "return ::android::hardware::Status::fromExceptionCode(\n"; |
| 1814 | out.indent(); |
| 1815 | out.indent(); |
Steven Moreland | 610002f | 2017-06-16 13:02:49 -0700 | [diff] [blame] | 1816 | out << "::android::hardware::Status::EX_TRANSACTION_FAILED,\n" |
| 1817 | << "\"Passthrough oneway function queue exceeds maximum size.\");\n"; |
Steven Moreland | 67f67b4 | 2016-09-29 08:59:02 -0700 | [diff] [blame] | 1818 | out.unindent(); |
| 1819 | out.unindent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1820 | out.unindent(); |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1821 | out << "}\n"; |
| 1822 | |
Steven Moreland | d366c26 | 2016-10-11 15:29:10 -0700 | [diff] [blame] | 1823 | out << "return ::android::hardware::Status();\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1824 | |
| 1825 | out.unindent(); |
| 1826 | out << "}\n\n"; |
| 1827 | |
| 1828 | |
| 1829 | } |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1830 | } |
| 1831 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1832 | void AST::generateCppAtraceCall(Formatter &out, |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1833 | InstrumentationEvent event, |
| 1834 | const Method *method) const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 1835 | const Interface* iface = mRootScope.getInterface(); |
Yifan Hong | eefe4f2 | 2017-01-04 15:32:42 -0800 | [diff] [blame] | 1836 | std::string baseString = "HIDL::" + iface->localName() + "::" + method->name(); |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1837 | switch (event) { |
| 1838 | case SERVER_API_ENTRY: |
| 1839 | { |
| 1840 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 1841 | << baseString + "::server\");\n"; |
| 1842 | break; |
| 1843 | } |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1844 | case PASSTHROUGH_ENTRY: |
| 1845 | { |
| 1846 | out << "atrace_begin(ATRACE_TAG_HAL, \"" |
| 1847 | << baseString + "::passthrough\");\n"; |
| 1848 | break; |
| 1849 | } |
| 1850 | case SERVER_API_EXIT: |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1851 | case PASSTHROUGH_EXIT: |
| 1852 | { |
| 1853 | out << "atrace_end(ATRACE_TAG_HAL);\n"; |
| 1854 | break; |
| 1855 | } |
Steven Moreland | 4607ef5 | 2018-05-09 10:52:47 -0700 | [diff] [blame] | 1856 | // client uses scope because of gotos |
| 1857 | // this isn't done for server because the profiled code isn't alone in its scope |
| 1858 | // this isn't done for passthrough becuase the profiled boundary isn't even in the same code |
| 1859 | case CLIENT_API_ENTRY: { |
Michael Butler | 0a3d99a | 2018-07-26 13:47:10 -0700 | [diff] [blame] | 1860 | out << "::android::ScopedTrace PASTE(___tracer, __LINE__) (ATRACE_TAG_HAL, \"" |
| 1861 | << baseString + "::client\");\n"; |
Steven Moreland | 4607ef5 | 2018-05-09 10:52:47 -0700 | [diff] [blame] | 1862 | break; |
| 1863 | } |
| 1864 | case CLIENT_API_EXIT: |
| 1865 | break; |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1866 | default: |
| 1867 | { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 1868 | CHECK(false) << "Unsupported instrumentation event: " << event; |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1869 | } |
| 1870 | } |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1873 | void AST::generateCppInstrumentationCall( |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1874 | Formatter &out, |
| 1875 | InstrumentationEvent event, |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1876 | const Method *method, |
| 1877 | const Interface* superInterface) const { |
Steven Moreland | 92a08a7 | 2017-07-31 14:57:37 -0700 | [diff] [blame] | 1878 | generateCppAtraceCall(out, event, method); |
Martijn Coenen | 7b29524 | 2016-11-04 16:52:56 +0100 | [diff] [blame] | 1879 | |
Steven Moreland | 30b76e9 | 2017-06-02 18:52:24 -0700 | [diff] [blame] | 1880 | out << "#ifdef __ANDROID_DEBUGGABLE__\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1881 | out << "if (UNLIKELY(mEnableInstrumentation)) {\n"; |
| 1882 | out.indent(); |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1883 | out << "std::vector<void *> _hidl_args;\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1884 | std::string event_str = ""; |
| 1885 | switch (event) { |
| 1886 | case SERVER_API_ENTRY: |
| 1887 | { |
| 1888 | event_str = "InstrumentationEvent::SERVER_API_ENTRY"; |
| 1889 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1890 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1891 | << (arg->type().resultNeedsDeref() ? "" : "&") |
| 1892 | << arg->name() |
| 1893 | << ");\n"; |
| 1894 | } |
| 1895 | break; |
| 1896 | } |
| 1897 | case SERVER_API_EXIT: |
| 1898 | { |
| 1899 | event_str = "InstrumentationEvent::SERVER_API_EXIT"; |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1900 | for (const auto &arg : method->results()) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1901 | out << "_hidl_args.push_back((void *)&_hidl_out_" |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1902 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1903 | << ");\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1904 | } |
| 1905 | break; |
| 1906 | } |
| 1907 | case CLIENT_API_ENTRY: |
| 1908 | { |
| 1909 | event_str = "InstrumentationEvent::CLIENT_API_ENTRY"; |
| 1910 | for (const auto &arg : method->args()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1911 | out << "_hidl_args.push_back((void *)&" |
| 1912 | << arg->name() |
| 1913 | << ");\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1914 | } |
| 1915 | break; |
| 1916 | } |
| 1917 | case CLIENT_API_EXIT: |
| 1918 | { |
| 1919 | event_str = "InstrumentationEvent::CLIENT_API_EXIT"; |
| 1920 | for (const auto &arg : method->results()) { |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1921 | out << "_hidl_args.push_back((void *)" |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1922 | << (arg->type().resultNeedsDeref() ? "" : "&") |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1923 | << "_hidl_out_" |
| 1924 | << arg->name() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1925 | << ");\n"; |
| 1926 | } |
| 1927 | break; |
| 1928 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 1929 | case PASSTHROUGH_ENTRY: |
| 1930 | { |
| 1931 | event_str = "InstrumentationEvent::PASSTHROUGH_ENTRY"; |
| 1932 | for (const auto &arg : method->args()) { |
| 1933 | out << "_hidl_args.push_back((void *)&" |
| 1934 | << arg->name() |
| 1935 | << ");\n"; |
| 1936 | } |
| 1937 | break; |
| 1938 | } |
| 1939 | case PASSTHROUGH_EXIT: |
| 1940 | { |
| 1941 | event_str = "InstrumentationEvent::PASSTHROUGH_EXIT"; |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 1942 | for (const auto &arg : method->results()) { |
Yifan Hong | a47eef3 | 2016-12-12 10:38:54 -0800 | [diff] [blame] | 1943 | out << "_hidl_args.push_back((void *)&_hidl_out_" |
Zhuoyao Zhang | 085a8c3 | 2016-11-17 15:35:49 -0800 | [diff] [blame] | 1944 | << arg->name() |
| 1945 | << ");\n"; |
| 1946 | } |
Steven Moreland | 9b1cbdf | 2016-11-01 12:23:27 -0700 | [diff] [blame] | 1947 | break; |
| 1948 | } |
Steven Moreland | 031ccf1 | 2016-10-31 15:54:38 -0700 | [diff] [blame] | 1949 | default: |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1950 | { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 1951 | CHECK(false) << "Unsupported instrumentation event: " << event; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1952 | } |
| 1953 | } |
| 1954 | |
Steven Moreland | 1ab3144 | 2016-11-03 18:37:51 -0700 | [diff] [blame] | 1955 | out << "for (const auto &callback: mInstrumentationCallbacks) {\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1956 | out.indent(); |
| 1957 | out << "callback(" |
| 1958 | << event_str |
| 1959 | << ", \"" |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1960 | << superInterface->fqName().package() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1961 | << "\", \"" |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1962 | << superInterface->fqName().version() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1963 | << "\", \"" |
Steven Moreland | 616cf4d | 2018-10-02 13:52:18 -0700 | [diff] [blame] | 1964 | << superInterface->localName() |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1965 | << "\", \"" |
| 1966 | << method->name() |
Zhuoyao Zhang | 964f72f | 2016-10-21 11:12:03 -0700 | [diff] [blame] | 1967 | << "\", &_hidl_args);\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1968 | out.unindent(); |
| 1969 | out << "}\n"; |
| 1970 | out.unindent(); |
Steven Moreland | 30b76e9 | 2017-06-02 18:52:24 -0700 | [diff] [blame] | 1971 | out << "}\n"; |
| 1972 | out << "#endif // __ANDROID_DEBUGGABLE__\n\n"; |
Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1975 | } // namespace android |