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