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 | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 21 | #include "Interface.h" |
| 22 | #include "Method.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 23 | #include "ScalarType.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 24 | #include "Scope.h" |
| 25 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 26 | #include <algorithm> |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 27 | #include <hidl-util/StringHelper.h> |
| 28 | #include <hidl-util/Formatter.h> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 29 | #include <android-base/logging.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 30 | #include <string> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| 33 | namespace android { |
| 34 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 35 | status_t AST::generateCpp(const std::string &outputPath) const { |
| 36 | status_t err = generateInterfaceHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 37 | |
| 38 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 39 | err = generateStubHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | if (err == OK) { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 43 | err = generateHwBinderHeader(outputPath); |
| 44 | } |
| 45 | |
| 46 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 47 | err = generateProxyHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 51 | err = generateAllSource(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 54 | if (err == OK) { |
| 55 | generatePassthroughHeader(outputPath); |
| 56 | } |
| 57 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 58 | return err; |
| 59 | } |
| 60 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 61 | void AST::getPackageComponents( |
| 62 | std::vector<std::string> *components) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 63 | mPackage.getPackageComponents(components); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void AST::getPackageAndVersionComponents( |
| 67 | std::vector<std::string> *components, bool cpp_compatible) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 68 | mPackage.getPackageAndVersionComponents(components, cpp_compatible); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 71 | std::string AST::makeHeaderGuard(const std::string &baseName) const { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 72 | std::string guard = "HIDL_GENERATED_"; |
| 73 | guard += mPackage.tokenName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 74 | |
| 75 | guard += "_"; |
| 76 | guard += baseName; |
| 77 | guard += "_H_"; |
| 78 | |
| 79 | return guard; |
| 80 | } |
| 81 | |
| 82 | void AST::enterLeaveNamespace(Formatter &out, bool enter) const { |
| 83 | std::vector<std::string> packageComponents; |
| 84 | getPackageAndVersionComponents( |
| 85 | &packageComponents, true /* cpp_compatible */); |
| 86 | |
| 87 | if (enter) { |
| 88 | for (const auto &component : packageComponents) { |
| 89 | out << "namespace " << component << " {\n"; |
| 90 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 91 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 92 | out.setNamespace(mPackage.cppNamespace() + "::"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 93 | } else { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 94 | out.setNamespace(std::string()); |
| 95 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 96 | for (auto it = packageComponents.rbegin(); |
| 97 | it != packageComponents.rend(); |
| 98 | ++it) { |
| 99 | out << "} // namespace " << *it << "\n"; |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 104 | status_t AST::generateInterfaceHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 105 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 106 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 107 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 108 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 109 | |
| 110 | std::string ifaceName; |
| 111 | bool isInterface = true; |
| 112 | if (!AST::isInterface(&ifaceName)) { |
| 113 | ifaceName = "types"; |
| 114 | isInterface = false; |
| 115 | } |
| 116 | path.append(ifaceName); |
| 117 | path.append(".h"); |
| 118 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 119 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 120 | FILE *file = fopen(path.c_str(), "w"); |
| 121 | |
| 122 | if (file == NULL) { |
| 123 | return -errno; |
| 124 | } |
| 125 | |
| 126 | Formatter out(file); |
| 127 | |
| 128 | const std::string guard = makeHeaderGuard(ifaceName); |
| 129 | |
| 130 | out << "#ifndef " << guard << "\n"; |
| 131 | out << "#define " << guard << "\n\n"; |
| 132 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 133 | for (const auto &item : mImportedNames) { |
| 134 | out << "#include <"; |
| 135 | |
| 136 | std::vector<std::string> components; |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 137 | item.getPackageAndVersionComponents( |
| 138 | &components, false /* cpp_compatible */); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 139 | |
| 140 | for (const auto &component : components) { |
| 141 | out << component << "/"; |
| 142 | } |
| 143 | |
| 144 | out << item.name() |
| 145 | << ".h>\n"; |
| 146 | } |
| 147 | |
| 148 | if (!mImportedNames.empty()) { |
| 149 | out << "\n"; |
| 150 | } |
| 151 | |
Martijn Coenen | 7473fab | 2016-08-19 14:05:40 +0200 | [diff] [blame] | 152 | out << "#include <hidl/HidlSupport.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 153 | out << "#include <hidl/IServiceManager.h>\n"; |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 154 | out << "#include <hidl/MQDescriptor.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 155 | |
| 156 | if (isInterface) { |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 157 | out << "#include <hidl/Status.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | out << "#include <utils/NativeHandle.h>\n\n"; |
| 161 | |
| 162 | enterLeaveNamespace(out, true /* enter */); |
| 163 | out << "\n"; |
| 164 | |
| 165 | if (isInterface) { |
| 166 | out << "struct " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 167 | << ifaceName; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 168 | |
| 169 | const Interface *iface = mRootScope->getInterface(); |
| 170 | const Interface *superType = iface->superType(); |
| 171 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 172 | if (superType == NULL) { |
| 173 | out << " : virtual public RefBase"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 174 | } else { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 175 | out << " : public " |
| 176 | << superType->fullName(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | out << " {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 180 | |
| 181 | out.indent(); |
| 182 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | status_t err = emitTypeDeclarations(out); |
| 186 | |
| 187 | if (err != OK) { |
| 188 | return err; |
| 189 | } |
| 190 | |
| 191 | if (isInterface) { |
| 192 | const Interface *iface = mRootScope->getInterface(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 193 | const Interface *superType = iface->superType(); |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 194 | const std::string baseName = iface->getBaseName(); |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 195 | out << "constexpr static hidl_version version = {" |
| 196 | << mPackage.getPackageMajorVersion() << "," |
| 197 | << mPackage.getPackageMinorVersion() << "};\n"; |
| 198 | out << "virtual const hidl_version& getInterfaceVersion() const {\n"; |
| 199 | out.indent(); |
| 200 | out << "return version;\n"; |
| 201 | out.unindent(); |
| 202 | out << "}\n\n"; |
Iliyan Malchev | 795dd43 | 2016-09-02 11:48:26 -0700 | [diff] [blame] | 203 | out << "virtual bool isRemote() const { return false; }\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 204 | bool haveCallbacks = false; |
| 205 | for (const auto &method : iface->methods()) { |
| 206 | const bool returnsValue = !method->results().empty(); |
| 207 | |
| 208 | if (!returnsValue) { |
| 209 | continue; |
| 210 | } |
| 211 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 212 | if (method->canElideCallback() != nullptr) { |
| 213 | continue; |
| 214 | } |
| 215 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 216 | haveCallbacks = true; |
| 217 | |
| 218 | out << "using " |
| 219 | << method->name() |
| 220 | << "_cb = std::function<void(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 221 | << Method::GetArgSignature(method->results(), |
| 222 | true /* specify namespaces */) |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 223 | << ")>;\n"; |
| 224 | } |
| 225 | |
| 226 | if (haveCallbacks) { |
| 227 | out << "\n"; |
| 228 | } |
| 229 | |
| 230 | for (const auto &method : iface->methods()) { |
| 231 | const bool returnsValue = !method->results().empty(); |
| 232 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 233 | method->dumpAnnotations(out); |
| 234 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 235 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 236 | if (elidedReturn) { |
| 237 | std::string extra; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 238 | out << "virtual ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 239 | out << elidedReturn->type().getCppResultType(&extra) << "> "; |
| 240 | } else { |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 241 | out << "virtual ::android::hardware::Return<void> "; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | out << method->name() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 245 | << "(" |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 246 | << Method::GetArgSignature(method->args(), |
| 247 | true /* specify namespaces */); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 248 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 249 | if (returnsValue && elidedReturn == nullptr) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 250 | if (!method->args().empty()) { |
| 251 | out << ", "; |
| 252 | } |
| 253 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 254 | out << method->name() << "_cb _hidl_cb = nullptr"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | out << ") = 0;\n"; |
| 258 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 259 | |
| 260 | out << "DECLARE_REGISTER_AND_GET_SERVICE(" << baseName << ")\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | if (isInterface) { |
| 264 | out.unindent(); |
| 265 | |
| 266 | out << "};\n"; |
| 267 | } |
| 268 | |
| 269 | out << "\n"; |
| 270 | enterLeaveNamespace(out, false /* enter */); |
| 271 | |
| 272 | out << "\n#endif // " << guard << "\n"; |
| 273 | |
| 274 | return OK; |
| 275 | } |
| 276 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 277 | status_t AST::generateHwBinderHeader(const std::string &outputPath) const { |
| 278 | std::string ifaceName; |
| 279 | if(!AST::isInterface(&ifaceName)) { |
| 280 | // types.hal does not get an HwBinder header. |
| 281 | return OK; |
| 282 | } |
| 283 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 284 | const Interface *iface = mRootScope->getInterface(); |
| 285 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 286 | |
| 287 | const std::string klassName = "IHw" + baseName; |
| 288 | |
| 289 | std::string path = outputPath; |
| 290 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 291 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 292 | path.append(klassName + ".h"); |
| 293 | |
| 294 | FILE* file = fopen(path.c_str(), "w"); |
| 295 | |
| 296 | if (file == NULL) { |
| 297 | return -errno; |
| 298 | } |
| 299 | |
| 300 | Formatter out(file); |
| 301 | |
| 302 | const std::string guard = makeHeaderGuard(klassName); |
| 303 | |
| 304 | out << "#ifndef " << guard << "\n"; |
| 305 | out << "#define " << guard << "\n\n"; |
| 306 | |
| 307 | std::vector<std::string> packageComponents; |
| 308 | getPackageAndVersionComponents( |
| 309 | &packageComponents, false /* cpp_compatible */); |
| 310 | |
| 311 | out << "#include <"; |
| 312 | for (const auto &component : packageComponents) { |
| 313 | out << component << "/"; |
| 314 | } |
| 315 | out << ifaceName << ".h>\n\n"; |
| 316 | |
| 317 | for (const auto &item : mImportedNames) { |
| 318 | if (item.name() == "types") { |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | out << "#include <"; |
| 323 | |
| 324 | std::vector<std::string> components; |
| 325 | item.getPackageAndVersionComponents( |
| 326 | &components, false /* cpp_compatible */); |
| 327 | |
| 328 | for (const auto &component : components) { |
| 329 | out << component << "/"; |
| 330 | } |
| 331 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 332 | const std::string itemBaseName = item.getInterfaceBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 333 | |
| 334 | out << "Bn" |
| 335 | << itemBaseName |
| 336 | << ".h>\n"; |
| 337 | } |
| 338 | |
| 339 | out << "\n"; |
| 340 | |
| 341 | out << "#include <hidl/HidlSupport.h>\n"; |
Martijn Coenen | 9391510 | 2016-09-01 01:35:52 +0200 | [diff] [blame] | 342 | out << "#include <hidl/Status.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 343 | out << "#include <hwbinder/IBinder.h>\n"; |
| 344 | out << "#include <hwbinder/IInterface.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 345 | |
| 346 | out << "\n"; |
| 347 | |
| 348 | enterLeaveNamespace(out, true /* enter */); |
| 349 | out << "\n"; |
| 350 | |
| 351 | out << "struct " |
| 352 | << klassName |
| 353 | << " : public " |
| 354 | << ifaceName; |
| 355 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 356 | const Interface *superType = iface->superType(); |
| 357 | |
| 358 | out << ", public ::android::hardware::IInterface"; |
| 359 | |
| 360 | out << " {\n"; |
| 361 | |
| 362 | out.indent(); |
| 363 | |
| 364 | out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n"; |
| 365 | |
| 366 | out << "enum Call {\n"; |
| 367 | out.indent(); |
| 368 | |
| 369 | bool first = true; |
| 370 | for (const auto &method : iface->methods()) { |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 371 | out << StringHelper::Upcase(method->name()); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 372 | |
| 373 | if (first) { |
| 374 | out << " = "; |
| 375 | if (superType != NULL) { |
| 376 | out << superType->fqName().cppNamespace() |
| 377 | << "::IHw" |
| 378 | << superType->getBaseName() |
| 379 | << "::Call::CallCount"; |
| 380 | } else { |
| 381 | out << "::android::hardware::IBinder::FIRST_CALL_TRANSACTION"; |
| 382 | } |
| 383 | |
| 384 | first = false; |
| 385 | } |
| 386 | |
| 387 | out << ",\n"; |
| 388 | } |
| 389 | |
| 390 | out << "CallCount\n"; |
| 391 | |
| 392 | out.unindent(); |
| 393 | out << "};\n\n"; |
| 394 | |
| 395 | out.unindent(); |
| 396 | |
| 397 | out << "};\n\n"; |
| 398 | |
| 399 | enterLeaveNamespace(out, false /* enter */); |
| 400 | |
| 401 | out << "\n#endif // " << guard << "\n"; |
| 402 | |
| 403 | return OK; |
| 404 | } |
| 405 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 406 | status_t AST::emitTypeDeclarations(Formatter &out) const { |
| 407 | return mRootScope->emitTypeDeclarations(out); |
| 408 | } |
| 409 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 410 | status_t AST::generateStubMethod(Formatter &out, |
| 411 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 412 | const Method *method, |
| 413 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 414 | out << "inline "; |
| 415 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 416 | method->generateCppSignature(out, |
| 417 | className, |
| 418 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 419 | |
| 420 | const bool returnsValue = !method->results().empty(); |
| 421 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 422 | out << " {\n"; |
| 423 | out.indent(); |
| 424 | out << "return mImpl->" |
| 425 | << method->name() |
| 426 | << "("; |
| 427 | bool first = true; |
| 428 | for (const auto &arg : method->args()) { |
| 429 | if (!first) { |
| 430 | out << ", "; |
| 431 | } |
| 432 | first = false; |
| 433 | out << arg->name(); |
| 434 | } |
| 435 | if (returnsValue && elidedReturn == nullptr) { |
| 436 | if (!method->args().empty()) { |
| 437 | out << ", "; |
| 438 | } |
| 439 | |
| 440 | out << "_hidl_cb"; |
| 441 | } |
| 442 | out << ");\n"; |
| 443 | out.unindent(); |
| 444 | out << "}"; |
| 445 | |
| 446 | out << ";\n"; |
| 447 | |
| 448 | return OK; |
| 449 | } |
| 450 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 451 | status_t AST::generateProxyDeclaration(Formatter &out, |
| 452 | const std::string &className, |
| 453 | const Method *method, |
| 454 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 455 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 456 | method->generateCppSignature(out, |
| 457 | className, |
| 458 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 459 | out << " override;\n"; |
| 460 | |
| 461 | return OK; |
| 462 | } |
| 463 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 464 | |
| 465 | status_t AST::generatePassthroughMethod(Formatter &out, |
| 466 | const std::string &className, |
| 467 | const Method *method, |
| 468 | bool specifyNamespaces) const { |
| 469 | method->generateCppSignature(out, className, specifyNamespaces); |
| 470 | |
| 471 | out << " {\n"; |
| 472 | out.indent(); |
| 473 | |
| 474 | const bool returnsValue = !method->results().empty(); |
| 475 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 476 | |
| 477 | out << "return "; |
| 478 | |
| 479 | if (method->isOneway()) { |
| 480 | out << "addOnewayTask([this"; |
| 481 | for (const auto &arg : method->args()) { |
| 482 | out << ", " << arg->name(); |
| 483 | } |
| 484 | out << "] {this->"; |
| 485 | } |
| 486 | |
| 487 | out << "mImpl->" |
| 488 | << method->name() |
| 489 | << "("; |
| 490 | |
| 491 | bool first = true; |
| 492 | for (const auto &arg : method->args()) { |
| 493 | if (!first) { |
| 494 | out << ", "; |
| 495 | } |
| 496 | first = false; |
| 497 | out << arg->name(); |
| 498 | } |
| 499 | if (returnsValue && elidedReturn == nullptr) { |
| 500 | if (!method->args().empty()) { |
| 501 | out << ", "; |
| 502 | } |
| 503 | |
| 504 | out << "_hidl_cb"; |
| 505 | } |
| 506 | out << ")"; |
| 507 | |
| 508 | if (method->isOneway()) { |
| 509 | out << ";})"; |
| 510 | } |
| 511 | out << ";\n"; |
| 512 | |
| 513 | out.unindent(); |
| 514 | out << "}\n"; |
| 515 | |
| 516 | return OK; |
| 517 | } |
| 518 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 519 | status_t AST::generateMethods( |
| 520 | Formatter &out, |
| 521 | const std::string &className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 522 | MethodLocation type, |
| 523 | bool specifyNamespaces) const { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 524 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 525 | const Interface *iface = mRootScope->getInterface(); |
| 526 | |
| 527 | std::vector<const Interface *> chain; |
| 528 | while (iface != NULL) { |
| 529 | chain.push_back(iface); |
| 530 | iface = iface->superType(); |
| 531 | } |
| 532 | |
| 533 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 534 | const Interface *superInterface = *it; |
| 535 | |
| 536 | out << "// Methods from " |
| 537 | << superInterface->fullName() |
| 538 | << " follow.\n"; |
| 539 | |
| 540 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 541 | status_t err; |
| 542 | switch(type) { |
| 543 | case STUB_HEADER: |
| 544 | err = generateStubMethod(out, |
| 545 | className, |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 546 | method, |
| 547 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 548 | break; |
| 549 | case PROXY_HEADER: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 550 | err = generateProxyDeclaration(out, |
| 551 | className, |
| 552 | method, |
| 553 | specifyNamespaces); |
| 554 | break; |
| 555 | case IMPL_HEADER: |
| 556 | err = generateStubImplDeclaration(out, |
| 557 | className, |
| 558 | method, |
| 559 | specifyNamespaces); |
| 560 | break; |
| 561 | case IMPL_SOURCE: |
| 562 | err = generateStubImplMethod(out, |
| 563 | className, |
| 564 | method, |
| 565 | specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 566 | break; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 567 | case PASSTHROUGH_HEADER: |
| 568 | err = generatePassthroughMethod(out, |
| 569 | className, |
| 570 | method, |
| 571 | specifyNamespaces); |
| 572 | break; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 573 | default: |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 574 | LOG(ERROR) << "Unkown method type: " << type; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 575 | err = UNKNOWN_ERROR; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 576 | } |
| 577 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 578 | if (err != OK) { |
| 579 | return err; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 580 | } |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | out << "\n"; |
| 584 | } |
| 585 | |
| 586 | return OK; |
| 587 | } |
| 588 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 589 | status_t AST::generateStubHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 590 | std::string ifaceName; |
| 591 | if (!AST::isInterface(&ifaceName)) { |
| 592 | // types.hal does not get a stub header. |
| 593 | return OK; |
| 594 | } |
| 595 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 596 | const Interface *iface = mRootScope->getInterface(); |
| 597 | const std::string baseName = iface->getBaseName(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 598 | const std::string klassName = "Bn" + baseName; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 599 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 600 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 601 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 602 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 603 | path.append(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 604 | path.append(".h"); |
| 605 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 606 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 607 | FILE *file = fopen(path.c_str(), "w"); |
| 608 | |
| 609 | if (file == NULL) { |
| 610 | return -errno; |
| 611 | } |
| 612 | |
| 613 | Formatter out(file); |
| 614 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 615 | const std::string guard = makeHeaderGuard(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 616 | |
| 617 | out << "#ifndef " << guard << "\n"; |
| 618 | out << "#define " << guard << "\n\n"; |
| 619 | |
| 620 | std::vector<std::string> packageComponents; |
| 621 | getPackageAndVersionComponents( |
| 622 | &packageComponents, false /* cpp_compatible */); |
| 623 | |
| 624 | out << "#include <"; |
| 625 | for (const auto &component : packageComponents) { |
| 626 | out << component << "/"; |
| 627 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 628 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 629 | |
| 630 | enterLeaveNamespace(out, true /* enter */); |
| 631 | out << "\n"; |
| 632 | |
| 633 | out << "struct " |
| 634 | << "Bn" |
| 635 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 636 | << " : public ::android::hardware::BnInterface<I" |
| 637 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 638 | << "> {\n"; |
| 639 | |
| 640 | out.indent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 641 | out << "explicit Bn" |
| 642 | << baseName |
| 643 | << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);" |
| 644 | << "\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 645 | out << "::android::status_t onTransact(\n"; |
| 646 | out.indent(); |
| 647 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 648 | out << "uint32_t _hidl_code,\n"; |
| 649 | out << "const ::android::hardware::Parcel &_hidl_data,\n"; |
| 650 | out << "::android::hardware::Parcel *_hidl_reply,\n"; |
| 651 | out << "uint32_t _hidl_flags = 0,\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 652 | out << "TransactCallback _hidl_cb = nullptr) override;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 653 | out.unindent(); |
| 654 | out.unindent(); |
| 655 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 656 | status_t err = generateMethods(out, |
| 657 | "" /* class name */, |
| 658 | MethodLocation::STUB_HEADER, |
| 659 | true /* specify namespaces */); |
| 660 | |
| 661 | if (err != OK) { |
| 662 | return err; |
| 663 | } |
| 664 | |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame^] | 665 | // Generated code for instrumentation. |
| 666 | out << "// for hidl instrumentation.\n"; |
| 667 | out << "std::vector<InstrumentationCallback> instrumentationCallbacks;\n\n"; |
| 668 | out << "bool enableInstrumentation = false;\n"; |
| 669 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 670 | out.unindent(); |
| 671 | |
| 672 | out << "};\n\n"; |
| 673 | |
| 674 | enterLeaveNamespace(out, false /* enter */); |
| 675 | |
| 676 | out << "\n#endif // " << guard << "\n"; |
| 677 | |
| 678 | return OK; |
| 679 | } |
| 680 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 681 | status_t AST::generateProxyHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 682 | std::string ifaceName; |
| 683 | if (!AST::isInterface(&ifaceName)) { |
| 684 | // types.hal does not get a proxy header. |
| 685 | return OK; |
| 686 | } |
| 687 | |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 688 | const Interface *iface = mRootScope->getInterface(); |
| 689 | const std::string baseName = iface->getBaseName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 690 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 691 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 692 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 693 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 694 | path.append("Bp"); |
| 695 | path.append(baseName); |
| 696 | path.append(".h"); |
| 697 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 698 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 699 | FILE *file = fopen(path.c_str(), "w"); |
| 700 | |
| 701 | if (file == NULL) { |
| 702 | return -errno; |
| 703 | } |
| 704 | |
| 705 | Formatter out(file); |
| 706 | |
| 707 | const std::string guard = makeHeaderGuard("Bp" + baseName); |
| 708 | |
| 709 | out << "#ifndef " << guard << "\n"; |
| 710 | out << "#define " << guard << "\n\n"; |
| 711 | |
| 712 | std::vector<std::string> packageComponents; |
| 713 | getPackageAndVersionComponents( |
| 714 | &packageComponents, false /* cpp_compatible */); |
| 715 | |
| 716 | out << "#include <"; |
| 717 | for (const auto &component : packageComponents) { |
| 718 | out << component << "/"; |
| 719 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 720 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 721 | |
| 722 | enterLeaveNamespace(out, true /* enter */); |
| 723 | out << "\n"; |
| 724 | |
| 725 | out << "struct " |
| 726 | << "Bp" |
| 727 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 728 | << " : public ::android::hardware::BpInterface<IHw" |
| 729 | << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 730 | << "> {\n"; |
| 731 | |
| 732 | out.indent(); |
| 733 | |
| 734 | out << "explicit Bp" |
| 735 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 736 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 737 | << "\n\n"; |
| 738 | |
Iliyan Malchev | 795dd43 | 2016-09-02 11:48:26 -0700 | [diff] [blame] | 739 | out << "virtual bool isRemote() const { return true; }\n\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 740 | |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 741 | status_t err = generateMethods(out, |
| 742 | "" /* class name */, |
| 743 | MethodLocation::PROXY_HEADER, |
| 744 | true /* generate specify namespaces */); |
| 745 | |
| 746 | if (err != OK) { |
| 747 | return err; |
| 748 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 749 | |
| 750 | out.unindent(); |
| 751 | |
| 752 | out << "};\n\n"; |
| 753 | |
| 754 | enterLeaveNamespace(out, false /* enter */); |
| 755 | |
| 756 | out << "\n#endif // " << guard << "\n"; |
| 757 | |
| 758 | return OK; |
| 759 | } |
| 760 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 761 | status_t AST::generateAllSource(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 762 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 763 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 764 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 765 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 766 | |
| 767 | std::string ifaceName; |
| 768 | std::string baseName; |
| 769 | |
| 770 | bool isInterface = true; |
| 771 | if (!AST::isInterface(&ifaceName)) { |
| 772 | baseName = "types"; |
| 773 | isInterface = false; |
| 774 | } else { |
Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 775 | const Interface *iface = mRootScope->getInterface(); |
| 776 | baseName = iface->getBaseName(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | path.append(baseName); |
| 780 | |
| 781 | if (baseName != "types") { |
| 782 | path.append("All"); |
| 783 | } |
| 784 | |
| 785 | path.append(".cpp"); |
| 786 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 787 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 788 | FILE *file = fopen(path.c_str(), "w"); |
| 789 | |
| 790 | if (file == NULL) { |
| 791 | return -errno; |
| 792 | } |
| 793 | |
| 794 | Formatter out(file); |
| 795 | |
| 796 | std::vector<std::string> packageComponents; |
| 797 | getPackageAndVersionComponents( |
| 798 | &packageComponents, false /* cpp_compatible */); |
| 799 | |
| 800 | std::string prefix; |
| 801 | for (const auto &component : packageComponents) { |
| 802 | prefix += component; |
| 803 | prefix += "/"; |
| 804 | } |
| 805 | |
| 806 | if (isInterface) { |
| 807 | out << "#include <" << prefix << "/Bp" << baseName << ".h>\n"; |
| 808 | out << "#include <" << prefix << "/Bn" << baseName << ".h>\n"; |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 809 | out << "#include <" << prefix << "/Bs" << baseName << ".h>\n"; |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame^] | 810 | out << "#include <cutils/properties.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 811 | } else { |
| 812 | out << "#include <" << prefix << "types.h>\n"; |
| 813 | } |
| 814 | |
| 815 | out << "\n"; |
| 816 | |
| 817 | enterLeaveNamespace(out, true /* enter */); |
| 818 | out << "\n"; |
| 819 | |
| 820 | status_t err = generateTypeSource(out, ifaceName); |
| 821 | |
| 822 | if (err == OK && isInterface) { |
Martijn Coenen | a21f149 | 2016-09-08 15:55:14 +0200 | [diff] [blame] | 823 | out << "constexpr hidl_version " << ifaceName << "::version;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 824 | err = generateProxySource(out, baseName); |
| 825 | } |
| 826 | |
| 827 | if (err == OK && isInterface) { |
| 828 | err = generateStubSource(out, baseName); |
| 829 | } |
| 830 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 831 | if (err == OK && isInterface) { |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 832 | err = generatePassthroughSource(out); |
| 833 | } |
| 834 | |
| 835 | if (err == OK && isInterface) { |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 836 | const Interface *iface = mRootScope->getInterface(); |
| 837 | |
| 838 | out << "IMPLEMENT_REGISTER_AND_GET_SERVICE(" |
| 839 | << baseName << ", " |
| 840 | << "\"" << iface->fqName().package() |
Iliyan Malchev | 4923f93 | 2016-09-09 13:04:59 -0700 | [diff] [blame] | 841 | << iface->fqName().version() << "-impl.so\"" |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 842 | << ")\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 843 | } |
| 844 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 845 | enterLeaveNamespace(out, false /* enter */); |
| 846 | |
| 847 | return err; |
| 848 | } |
| 849 | |
| 850 | status_t AST::generateTypeSource( |
| 851 | Formatter &out, const std::string &ifaceName) const { |
| 852 | return mRootScope->emitTypeDefinitions(out, ifaceName); |
| 853 | } |
| 854 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 855 | void AST::declareCppReaderLocals( |
| 856 | Formatter &out, const std::vector<TypedVar *> &args) const { |
| 857 | if (args.empty()) { |
| 858 | return; |
| 859 | } |
| 860 | |
| 861 | for (const auto &arg : args) { |
| 862 | const Type &type = arg->type(); |
| 863 | |
| 864 | std::string extra; |
| 865 | out << type.getCppResultType(&extra) |
| 866 | << " " |
| 867 | << arg->name() |
| 868 | << extra |
| 869 | << ";\n"; |
| 870 | } |
| 871 | |
| 872 | out << "\n"; |
| 873 | } |
| 874 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 875 | void AST::emitCppReaderWriter( |
| 876 | Formatter &out, |
| 877 | const std::string &parcelObj, |
| 878 | bool parcelObjIsPointer, |
| 879 | const TypedVar *arg, |
| 880 | bool isReader, |
| 881 | Type::ErrorMode mode) const { |
| 882 | const Type &type = arg->type(); |
| 883 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 884 | type.emitReaderWriter( |
| 885 | out, |
| 886 | arg->name(), |
| 887 | parcelObj, |
| 888 | parcelObjIsPointer, |
| 889 | isReader, |
| 890 | mode); |
| 891 | } |
| 892 | |
| 893 | status_t AST::generateProxySource( |
| 894 | Formatter &out, const std::string &baseName) const { |
| 895 | const std::string klassName = "Bp" + baseName; |
| 896 | |
| 897 | out << klassName |
| 898 | << "::" |
| 899 | << klassName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 900 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 901 | |
| 902 | out.indent(); |
| 903 | out.indent(); |
| 904 | |
| 905 | out << ": BpInterface" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 906 | << "<IHw" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 907 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 908 | << ">(_hidl_impl) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 909 | |
| 910 | out.unindent(); |
| 911 | out.unindent(); |
| 912 | out << "}\n\n"; |
| 913 | |
| 914 | const Interface *iface = mRootScope->getInterface(); |
| 915 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 916 | std::vector<const Interface *> chain; |
| 917 | while (iface != NULL) { |
| 918 | chain.push_back(iface); |
| 919 | iface = iface->superType(); |
| 920 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 921 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 922 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 923 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 924 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 925 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 926 | method->generateCppSignature(out, |
| 927 | klassName, |
| 928 | true /* specify namespaces */); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 929 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 930 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 931 | const TypedVar *elidedReturn = method->canElideCallback(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 932 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 933 | out << "{\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 934 | |
| 935 | out.indent(); |
| 936 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 937 | out << "::android::hardware::Parcel _hidl_data;\n"; |
| 938 | out << "::android::hardware::Parcel _hidl_reply;\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 939 | out << "::android::status_t _hidl_err;\n"; |
| 940 | out << "::android::hardware::Status _hidl_status;\n\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 941 | declareCppReaderLocals(out, method->results()); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 942 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 943 | out << "_hidl_err = _hidl_data.writeInterfaceToken(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 944 | << superInterface->fqName().cppNamespace() |
| 945 | << "::IHw" |
| 946 | << superInterface->getBaseName() |
| 947 | << "::descriptor);\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 948 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 949 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 950 | |
| 951 | for (const auto &arg : method->args()) { |
| 952 | emitCppReaderWriter( |
| 953 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 954 | "_hidl_data", |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 955 | false /* parcelObjIsPointer */, |
| 956 | arg, |
| 957 | false /* reader */, |
| 958 | Type::ErrorMode_Goto); |
| 959 | } |
| 960 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 961 | out << "_hidl_err = remote()->transact(" |
| 962 | << superInterface->fqName().cppNamespace() |
| 963 | << "::IHw" |
| 964 | << superInterface->getBaseName() |
| 965 | << "::Call::" |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 966 | << StringHelper::Upcase(method->name()) |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 967 | << ", _hidl_data, &_hidl_reply"; |
| 968 | if (method->isOneway()) { |
| 969 | out << ", ::android::hardware::IBinder::FLAG_ONEWAY"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 970 | } |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 971 | out << ");\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 972 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 973 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 974 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 975 | if (!method->isOneway()) { |
| 976 | out << "_hidl_err = _hidl_status.readFromParcel(_hidl_reply);\n"; |
| 977 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 978 | out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n"; |
| 979 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 980 | for (const auto &arg : method->results()) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 981 | emitCppReaderWriter( |
| 982 | out, |
| 983 | "_hidl_reply", |
| 984 | false /* parcelObjIsPointer */, |
| 985 | arg, |
| 986 | true /* reader */, |
| 987 | Type::ErrorMode_Goto); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 988 | } |
| 989 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 990 | if (returnsValue && elidedReturn == nullptr) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 991 | out << "if (_hidl_cb != nullptr) {\n"; |
| 992 | out.indent(); |
| 993 | out << "_hidl_cb("; |
| 994 | |
| 995 | bool first = true; |
| 996 | for (const auto &arg : method->results()) { |
| 997 | if (!first) { |
| 998 | out << ", "; |
| 999 | } |
| 1000 | |
| 1001 | if (arg->type().resultNeedsDeref()) { |
| 1002 | out << "*"; |
| 1003 | } |
| 1004 | out << arg->name(); |
| 1005 | |
| 1006 | first = false; |
| 1007 | } |
| 1008 | |
| 1009 | out << ");\n"; |
| 1010 | out.unindent(); |
| 1011 | out << "}\n\n"; |
| 1012 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1015 | if (elidedReturn != nullptr) { |
| 1016 | std::string extra; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1017 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 1018 | out << "return ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1019 | out << elidedReturn->type().getCppResultType(&extra) |
| 1020 | << ">(" << elidedReturn->name() << ");\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1021 | } else { |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1022 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1023 | out << "return ::android::hardware::Return<void>();\n\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1024 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1025 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1026 | out.unindent(); |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1027 | out << "_hidl_error:\n"; |
| 1028 | out.indent(); |
| 1029 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 1030 | out << "return ::android::hardware::Return<"; |
| 1031 | if (elidedReturn != nullptr) { |
| 1032 | std::string extra; |
| 1033 | out << method->results().at(0)->type().getCppResultType(&extra); |
| 1034 | } else { |
| 1035 | out << "void"; |
| 1036 | } |
| 1037 | out << ">(_hidl_status);\n"; |
| 1038 | |
| 1039 | out.unindent(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1040 | out << "}\n\n"; |
| 1041 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | return OK; |
| 1045 | } |
| 1046 | |
| 1047 | status_t AST::generateStubSource( |
| 1048 | Formatter &out, const std::string &baseName) const { |
| 1049 | out << "IMPLEMENT_HWBINDER_META_INTERFACE(" |
| 1050 | << baseName |
| 1051 | << ", \"" |
| 1052 | << mPackage.string() |
| 1053 | << "::I" |
| 1054 | << baseName |
| 1055 | << "\");\n\n"; |
| 1056 | |
| 1057 | const std::string klassName = "Bn" + baseName; |
| 1058 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1059 | out << klassName |
| 1060 | << "::" |
| 1061 | << klassName |
| 1062 | << "(const ::android::sp<I" << baseName <<"> &_hidl_impl)\n"; |
| 1063 | |
| 1064 | out.indent(); |
| 1065 | out.indent(); |
| 1066 | |
| 1067 | out << ": BnInterface" |
| 1068 | << "<I" |
| 1069 | << baseName |
| 1070 | << ", IHw" |
| 1071 | << baseName |
| 1072 | << ">(_hidl_impl) {\n"; |
| 1073 | |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame^] | 1074 | out << "enableInstrumentation = " |
| 1075 | "property_get_bool(\"hal.instrumentation.enable\", false);\n"; |
| 1076 | out << "registerInstrumentationCallbacks(\"" |
| 1077 | << mPackage.string() |
| 1078 | << "::I" |
| 1079 | << baseName |
| 1080 | << "\", &instrumentationCallbacks);\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1081 | out.unindent(); |
| 1082 | out.unindent(); |
| 1083 | out << "}\n\n"; |
| 1084 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1085 | out << "::android::status_t " << klassName << "::onTransact(\n"; |
| 1086 | |
| 1087 | out.indent(); |
| 1088 | out.indent(); |
| 1089 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1090 | out << "uint32_t _hidl_code,\n" |
| 1091 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 1092 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 1093 | << "uint32_t _hidl_flags,\n" |
| 1094 | << "TransactCallback _hidl_cb) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1095 | |
| 1096 | out.unindent(); |
| 1097 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1098 | out << "::android::status_t _hidl_err = ::android::OK;\n\n"; |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1099 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1100 | out.indent(); |
| 1101 | |
| 1102 | const Interface *iface = mRootScope->getInterface(); |
| 1103 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1104 | std::vector<const Interface *> chain; |
| 1105 | while (iface != NULL) { |
| 1106 | chain.push_back(iface); |
| 1107 | iface = iface->superType(); |
| 1108 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1109 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1110 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 1111 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1112 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1113 | for (const auto &method : superInterface->methods()) { |
| 1114 | out << "case " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1115 | << superInterface->fqName().cppNamespace() |
| 1116 | << "::IHw" |
| 1117 | << superInterface->getBaseName() |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1118 | << "::Call::" |
Steven Moreland | af44014 | 2016-09-07 10:09:11 -0700 | [diff] [blame] | 1119 | << StringHelper::Upcase(method->name()) |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1120 | << ":\n{\n"; |
| 1121 | |
| 1122 | out.indent(); |
| 1123 | |
| 1124 | status_t err = |
| 1125 | generateStubSourceForMethod(out, superInterface, method); |
| 1126 | |
| 1127 | if (err != OK) { |
| 1128 | return err; |
| 1129 | } |
| 1130 | |
| 1131 | out.unindent(); |
| 1132 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1133 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | out << "default:\n{\n"; |
| 1137 | out.indent(); |
| 1138 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1139 | out << "return ::android::hardware::BnInterface<I" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1140 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1141 | << ">::onTransact(\n"; |
| 1142 | |
| 1143 | out.indent(); |
| 1144 | out.indent(); |
| 1145 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1146 | out << "_hidl_code, _hidl_data, _hidl_reply, " |
| 1147 | << "_hidl_flags, _hidl_cb);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1148 | |
| 1149 | out.unindent(); |
| 1150 | out.unindent(); |
| 1151 | |
| 1152 | out.unindent(); |
| 1153 | out << "}\n"; |
| 1154 | |
| 1155 | out.unindent(); |
| 1156 | out << "}\n\n"; |
| 1157 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1158 | out << "if (_hidl_err == ::android::UNEXPECTED_NULL) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1159 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1160 | out << "_hidl_err = ::android::hardware::Status::fromExceptionCode(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1161 | out.indent(); |
| 1162 | out.indent(); |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1163 | out << "::android::hardware::Status::EX_NULL_POINTER)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1164 | out.indent(); |
| 1165 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1166 | out << ".writeToParcel(_hidl_reply);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1167 | out.unindent(); |
| 1168 | out.unindent(); |
| 1169 | out.unindent(); |
| 1170 | out.unindent(); |
| 1171 | |
| 1172 | out.unindent(); |
| 1173 | out << "}\n\n"; |
| 1174 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1175 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1176 | |
| 1177 | out.unindent(); |
| 1178 | out << "}\n\n"; |
| 1179 | |
| 1180 | return OK; |
| 1181 | } |
| 1182 | |
| 1183 | status_t AST::generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1184 | Formatter &out, const Interface *iface, const Method *method) const { |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1185 | out << "if (!_hidl_data.enforceInterface(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame] | 1186 | << iface->fqName().cppNamespace() |
| 1187 | << "::IHw" |
| 1188 | << iface->getBaseName() |
| 1189 | << "::descriptor)) {\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1190 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1191 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1192 | out << "_hidl_err = ::android::BAD_TYPE;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1193 | out << "break;\n"; |
| 1194 | out.unindent(); |
| 1195 | out << "}\n\n"; |
| 1196 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1197 | declareCppReaderLocals(out, method->args()); |
| 1198 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1199 | for (const auto &arg : method->args()) { |
| 1200 | emitCppReaderWriter( |
| 1201 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1202 | "_hidl_data", |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1203 | false /* parcelObjIsPointer */, |
| 1204 | arg, |
| 1205 | true /* reader */, |
| 1206 | Type::ErrorMode_Break); |
| 1207 | } |
| 1208 | |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame^] | 1209 | out << "if (UNLIKELY(enableInstrumentation)) {\n"; |
| 1210 | out.indent(); |
| 1211 | out << "std::vector<void *> args;\n"; |
| 1212 | for (const auto &arg : method->args()) { |
| 1213 | out << "args.push_back((void *)" |
| 1214 | << (arg->type().resultNeedsDeref() ? "" : "&") |
| 1215 | << arg->name() |
| 1216 | << ");\n"; |
| 1217 | } |
| 1218 | |
| 1219 | out << "for (auto callback: instrumentationCallbacks) {\n"; |
| 1220 | out.indent(); |
| 1221 | out << "callback(InstrumentationEvent::SERVER_API_ENTRY, \"" |
| 1222 | << mPackage.package() |
| 1223 | << "\", \"" << mPackage.version().substr(1) << "\", \"" |
| 1224 | << iface->localName() << "\", \"" << method->name() << "\", &args);\n"; |
| 1225 | out.unindent(); |
| 1226 | out << "}\n"; |
| 1227 | out.unindent(); |
| 1228 | out << "}\n\n"; |
| 1229 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1230 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1231 | const TypedVar *elidedReturn = method->canElideCallback(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1232 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1233 | if (elidedReturn != nullptr) { |
| 1234 | std::string extra; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1235 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1236 | out << elidedReturn->type().getCppResultType(&extra) << " "; |
| 1237 | out << elidedReturn->name() << " = "; |
| 1238 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1239 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1240 | bool first = true; |
| 1241 | for (const auto &arg : method->args()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1242 | if (!first) { |
| 1243 | out << ", "; |
| 1244 | } |
| 1245 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1246 | if (arg->type().resultNeedsDeref()) { |
| 1247 | out << "*"; |
| 1248 | } |
| 1249 | |
| 1250 | out << arg->name(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1251 | |
| 1252 | first = false; |
| 1253 | } |
| 1254 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1255 | out << ");\n\n"; |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1256 | out << "::android::hardware::Status::ok()" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1257 | << ".writeToParcel(_hidl_reply);\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1258 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1259 | elidedReturn->type().emitReaderWriter( |
| 1260 | out, |
| 1261 | elidedReturn->name(), |
| 1262 | "_hidl_reply", |
| 1263 | true, /* parcelObjIsPointer */ |
| 1264 | false, /* isReader */ |
| 1265 | Type::ErrorMode_Ignore); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1266 | |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame^] | 1267 | out << "if (UNLIKELY(enableInstrumentation)) {\n"; |
| 1268 | out.indent(); |
| 1269 | out << "std::vector<void *> results;\n"; |
| 1270 | out << "results.push_back((void *)&" << elidedReturn->name() << ");\n"; |
| 1271 | |
| 1272 | out << "for (auto callback: instrumentationCallbacks) {\n"; |
| 1273 | out.indent(); |
| 1274 | out << "callback(InstrumentationEvent::SERVER_API_EXIT, \"" |
| 1275 | << mPackage.package() |
| 1276 | << "\", \"" << mPackage.version().substr(1) << "\", \"" |
| 1277 | << iface->localName() << "\", \"" << method->name() |
| 1278 | << "\", &results);\n"; |
| 1279 | out.unindent(); |
| 1280 | out << "}\n"; |
| 1281 | out.unindent(); |
| 1282 | out << "}\n\n"; |
| 1283 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1284 | out << "_hidl_cb(*_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1285 | } else { |
| 1286 | if (returnsValue) { |
| 1287 | out << "bool _hidl_callbackCalled = false;\n\n"; |
| 1288 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1289 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1290 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1291 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1292 | bool first = true; |
| 1293 | for (const auto &arg : method->args()) { |
| 1294 | if (!first) { |
| 1295 | out << ", "; |
| 1296 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1297 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1298 | if (arg->type().resultNeedsDeref()) { |
| 1299 | out << "*"; |
| 1300 | } |
| 1301 | |
| 1302 | out << arg->name(); |
| 1303 | |
| 1304 | first = false; |
| 1305 | } |
| 1306 | |
| 1307 | if (returnsValue) { |
| 1308 | if (!first) { |
| 1309 | out << ", "; |
| 1310 | } |
| 1311 | |
| 1312 | out << "[&]("; |
| 1313 | |
| 1314 | first = true; |
| 1315 | for (const auto &arg : method->results()) { |
| 1316 | if (!first) { |
| 1317 | out << ", "; |
| 1318 | } |
| 1319 | |
| 1320 | out << "const auto &" << arg->name(); |
| 1321 | |
| 1322 | first = false; |
| 1323 | } |
| 1324 | |
| 1325 | out << ") {\n"; |
| 1326 | out.indent(); |
| 1327 | out << "_hidl_callbackCalled = true;\n\n"; |
| 1328 | |
| 1329 | out << "::android::hardware::Status::ok()" |
| 1330 | << ".writeToParcel(_hidl_reply);\n\n"; |
| 1331 | |
| 1332 | for (const auto &arg : method->results()) { |
| 1333 | emitCppReaderWriter( |
| 1334 | out, |
| 1335 | "_hidl_reply", |
| 1336 | true /* parcelObjIsPointer */, |
| 1337 | arg, |
| 1338 | false /* reader */, |
| 1339 | Type::ErrorMode_Ignore); |
| 1340 | } |
| 1341 | |
Zhuoyao Zhang | de57800 | 2016-09-07 18:24:17 -0700 | [diff] [blame^] | 1342 | out << "if (UNLIKELY(enableInstrumentation)) {\n"; |
| 1343 | out.indent(); |
| 1344 | out << "std::vector<void *> results;\n"; |
| 1345 | for (const auto &arg : method->results()) { |
| 1346 | out << "results.push_back((void *)&" << arg->name() << ");\n"; |
| 1347 | } |
| 1348 | |
| 1349 | out << "for (auto callback: instrumentationCallbacks) {\n"; |
| 1350 | out.indent(); |
| 1351 | out << "callback(InstrumentationEvent::SERVER_API_EXIT, \"" |
| 1352 | << mPackage.package() |
| 1353 | << "\", \"" << mPackage.version().substr(1) << "\", \"" |
| 1354 | << iface->localName() << "\", \"" << method->name() |
| 1355 | << "\", &results);\n"; |
| 1356 | out.unindent(); |
| 1357 | out << "}\n"; |
| 1358 | out.unindent(); |
| 1359 | out << "}\n\n"; |
| 1360 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1361 | out << "_hidl_cb(*_hidl_reply);\n"; |
| 1362 | |
| 1363 | out.unindent(); |
| 1364 | out << "}\n"; |
| 1365 | } |
| 1366 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1367 | out << ");\n\n"; |
| 1368 | |
| 1369 | // What to do if the stub implementation has a synchronous callback |
| 1370 | // which does not get invoked? This is not a transport error but a |
| 1371 | // service error of sorts. For now, return OK to the caller, as this is |
| 1372 | // not a transport error. |
| 1373 | // |
| 1374 | // TODO(b/31365311) Figure out how to deal with this later. |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1375 | |
| 1376 | if (returnsValue) { |
| 1377 | out << "if (!_hidl_callbackCalled) {\n"; |
| 1378 | out.indent(); |
| 1379 | } |
| 1380 | |
Iliyan Malchev | d57066f | 2016-09-08 13:59:38 -0700 | [diff] [blame] | 1381 | out << "::android::hardware::Status::ok()" |
| 1382 | << ".writeToParcel(_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1383 | |
| 1384 | if (returnsValue) { |
| 1385 | out.unindent(); |
| 1386 | out << "}\n\n"; |
| 1387 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | out << "break;\n"; |
| 1391 | |
| 1392 | return OK; |
| 1393 | } |
| 1394 | |
Steven Moreland | 69e7c70 | 2016-09-09 11:16:32 -0700 | [diff] [blame] | 1395 | status_t AST::generatePassthroughHeader(const std::string &outputPath) const { |
| 1396 | std::string ifaceName; |
| 1397 | if (!AST::isInterface(&ifaceName)) { |
| 1398 | // types.hal does not get a stub header. |
| 1399 | return OK; |
| 1400 | } |
| 1401 | |
| 1402 | const Interface *iface = mRootScope->getInterface(); |
| 1403 | |
| 1404 | const std::string baseName = iface->getBaseName(); |
| 1405 | const std::string klassName = "Bs" + baseName; |
| 1406 | |
| 1407 | bool supportOneway = iface->hasOnewayMethods(); |
| 1408 | |
| 1409 | std::string path = outputPath; |
| 1410 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 1411 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 1412 | path.append(klassName); |
| 1413 | path.append(".h"); |
| 1414 | |
| 1415 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 1416 | FILE *file = fopen(path.c_str(), "w"); |
| 1417 | |
| 1418 | if (file == NULL) { |
| 1419 | return -errno; |
| 1420 | } |
| 1421 | |
| 1422 | Formatter out(file); |
| 1423 | |
| 1424 | const std::string guard = makeHeaderGuard(klassName); |
| 1425 | |
| 1426 | out << "#ifndef " << guard << "\n"; |
| 1427 | out << "#define " << guard << "\n\n"; |
| 1428 | |
| 1429 | std::vector<std::string> packageComponents; |
| 1430 | getPackageAndVersionComponents( |
| 1431 | &packageComponents, false /* cpp_compatible */); |
| 1432 | |
| 1433 | out << "#include <future>\n"; |
| 1434 | out << "#include <"; |
| 1435 | for (const auto &component : packageComponents) { |
| 1436 | out << component << "/"; |
| 1437 | } |
| 1438 | out << ifaceName << ".h>\n\n"; |
| 1439 | |
| 1440 | if (supportOneway) { |
| 1441 | out << "#include <hidl/SynchronizedQueue.h>\n"; |
| 1442 | } |
| 1443 | |
| 1444 | enterLeaveNamespace(out, true /* enter */); |
| 1445 | out << "\n"; |
| 1446 | |
| 1447 | out << "struct " |
| 1448 | << klassName |
| 1449 | << " : " << ifaceName |
| 1450 | << " {\n"; |
| 1451 | |
| 1452 | out.indent(); |
| 1453 | out << "explicit " |
| 1454 | << klassName |
| 1455 | << "(const sp<" |
| 1456 | << ifaceName |
| 1457 | << "> impl);\n"; |
| 1458 | |
| 1459 | status_t err = generateMethods(out, |
| 1460 | "" /* class name */, |
| 1461 | MethodLocation::PASSTHROUGH_HEADER, |
| 1462 | true /* specify namespaces */); |
| 1463 | |
| 1464 | if (err != OK) { |
| 1465 | return err; |
| 1466 | } |
| 1467 | |
| 1468 | out.unindent(); |
| 1469 | out << "private:\n"; |
| 1470 | out.indent(); |
| 1471 | out << "const sp<" << ifaceName << "> mImpl;\n"; |
| 1472 | |
| 1473 | if (supportOneway) { |
| 1474 | out << "SynchronizedQueue<std::function<void(void)>> mOnewayQueue;\n"; |
| 1475 | out << "std::thread *mOnewayThread = nullptr;\n"; |
| 1476 | |
| 1477 | out << "\n"; |
| 1478 | |
| 1479 | out << "::android::hardware::Return<void> addOnewayTask(" |
| 1480 | "std::function<void(void)>);\n\n"; |
| 1481 | |
| 1482 | out << "static const int kOnewayQueueMaxSize = 3000;\n"; |
| 1483 | } |
| 1484 | |
| 1485 | out.unindent(); |
| 1486 | |
| 1487 | out << "};\n\n"; |
| 1488 | |
| 1489 | enterLeaveNamespace(out, false /* enter */); |
| 1490 | |
| 1491 | out << "\n#endif // " << guard << "\n"; |
| 1492 | |
| 1493 | return OK; |
| 1494 | } |
| 1495 | |
| 1496 | |
| 1497 | status_t AST::generatePassthroughSource(Formatter &out) const { |
| 1498 | const Interface *iface = mRootScope->getInterface(); |
| 1499 | |
| 1500 | const std::string baseName = iface->getBaseName(); |
| 1501 | const std::string klassName = "Bs" + baseName; |
| 1502 | |
| 1503 | out << klassName |
| 1504 | << "::" |
| 1505 | << klassName |
| 1506 | << "(const sp<" |
| 1507 | << iface->fullName() |
| 1508 | << "> impl) : mImpl(impl) {}\n\n"; |
| 1509 | |
| 1510 | if (iface->hasOnewayMethods()) { |
| 1511 | out << "::android::hardware::Return<void> " |
| 1512 | << klassName |
| 1513 | << "::addOnewayTask(std::function<void(void)> fun) {\n"; |
| 1514 | out.indent(); |
| 1515 | out << "if (mOnewayThread == nullptr) {\n"; |
| 1516 | out.indent(); |
| 1517 | out << "mOnewayThread = new std::thread([this]() {\n"; |
| 1518 | out.indent(); |
| 1519 | out << "while(true) { (this->mOnewayQueue.wait_pop())(); }"; |
| 1520 | out.unindent(); |
| 1521 | out << "});\n"; |
| 1522 | out.unindent(); |
| 1523 | out << "}\n\n"; |
| 1524 | |
| 1525 | out << "if (mOnewayQueue.size() > kOnewayQueueMaxSize) {\n"; |
| 1526 | out.indent(); |
| 1527 | out << "return Status::fromExceptionCode(Status::EX_TRANSACTION_FAILED);\n"; |
| 1528 | out.unindent(); |
| 1529 | out << "} else {\n"; |
| 1530 | out.indent(); |
| 1531 | out << "mOnewayQueue.push(fun);\n"; |
| 1532 | out.unindent(); |
| 1533 | out << "}\n"; |
| 1534 | |
| 1535 | out << "return Status();\n"; |
| 1536 | |
| 1537 | out.unindent(); |
| 1538 | out << "}\n\n"; |
| 1539 | |
| 1540 | |
| 1541 | } |
| 1542 | |
| 1543 | return OK; |
| 1544 | } |
| 1545 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1546 | } // namespace android |
| 1547 | |