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 "Formatter.h" |
| 22 | #include "Interface.h" |
| 23 | #include "Method.h" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 24 | #include "ScalarType.h" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 25 | #include "Scope.h" |
| 26 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 27 | #include <algorithm> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 28 | #include <android-base/logging.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 29 | #include <string> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 30 | #include <vector> |
| 31 | |
| 32 | namespace android { |
| 33 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 34 | static std::string upcase(const std::string in) { |
| 35 | std::string out{in}; |
| 36 | |
| 37 | for (auto &ch : out) { |
| 38 | ch = toupper(ch); |
| 39 | } |
| 40 | |
| 41 | return out; |
| 42 | } |
| 43 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 44 | status_t AST::generateCpp(const std::string &outputPath) const { |
| 45 | status_t err = generateInterfaceHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 46 | |
| 47 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 48 | err = generateStubHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (err == OK) { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 52 | err = generateHwBinderHeader(outputPath); |
| 53 | } |
| 54 | |
| 55 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 56 | err = generateProxyHeader(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | if (err == OK) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 60 | err = generateAllSource(outputPath); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return err; |
| 64 | } |
| 65 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 66 | void AST::getPackageComponents( |
| 67 | std::vector<std::string> *components) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 68 | mPackage.getPackageComponents(components); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | void AST::getPackageAndVersionComponents( |
| 72 | std::vector<std::string> *components, bool cpp_compatible) const { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 73 | mPackage.getPackageAndVersionComponents(components, cpp_compatible); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 76 | std::string AST::makeHeaderGuard(const std::string &baseName) const { |
| 77 | std::vector<std::string> packageComponents; |
| 78 | getPackageAndVersionComponents( |
| 79 | &packageComponents, true /* cpp_compatible */); |
| 80 | |
| 81 | std::string guard = "HIDL_GENERATED"; |
| 82 | for (const auto &component : packageComponents) { |
| 83 | guard += "_"; |
| 84 | guard += component; |
| 85 | } |
| 86 | |
| 87 | guard += "_"; |
| 88 | guard += baseName; |
| 89 | guard += "_H_"; |
| 90 | |
| 91 | return guard; |
| 92 | } |
| 93 | |
| 94 | void AST::enterLeaveNamespace(Formatter &out, bool enter) const { |
| 95 | std::vector<std::string> packageComponents; |
| 96 | getPackageAndVersionComponents( |
| 97 | &packageComponents, true /* cpp_compatible */); |
| 98 | |
| 99 | if (enter) { |
| 100 | for (const auto &component : packageComponents) { |
| 101 | out << "namespace " << component << " {\n"; |
| 102 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 103 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 104 | out.setNamespace(mPackage.cppNamespace() + "::"); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 105 | } else { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 106 | out.setNamespace(std::string()); |
| 107 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 108 | for (auto it = packageComponents.rbegin(); |
| 109 | it != packageComponents.rend(); |
| 110 | ++it) { |
| 111 | out << "} // namespace " << *it << "\n"; |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 116 | status_t AST::generateInterfaceHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 117 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 118 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 119 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 120 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 121 | |
| 122 | std::string ifaceName; |
| 123 | bool isInterface = true; |
| 124 | if (!AST::isInterface(&ifaceName)) { |
| 125 | ifaceName = "types"; |
| 126 | isInterface = false; |
| 127 | } |
| 128 | path.append(ifaceName); |
| 129 | path.append(".h"); |
| 130 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 131 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 132 | FILE *file = fopen(path.c_str(), "w"); |
| 133 | |
| 134 | if (file == NULL) { |
| 135 | return -errno; |
| 136 | } |
| 137 | |
| 138 | Formatter out(file); |
| 139 | |
| 140 | const std::string guard = makeHeaderGuard(ifaceName); |
| 141 | |
| 142 | out << "#ifndef " << guard << "\n"; |
| 143 | out << "#define " << guard << "\n\n"; |
| 144 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 145 | for (const auto &item : mImportedNames) { |
| 146 | out << "#include <"; |
| 147 | |
| 148 | std::vector<std::string> components; |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 149 | item.getPackageAndVersionComponents( |
| 150 | &components, false /* cpp_compatible */); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 151 | |
| 152 | for (const auto &component : components) { |
| 153 | out << component << "/"; |
| 154 | } |
| 155 | |
| 156 | out << item.name() |
| 157 | << ".h>\n"; |
| 158 | } |
| 159 | |
| 160 | if (!mImportedNames.empty()) { |
| 161 | out << "\n"; |
| 162 | } |
| 163 | |
Martijn Coenen | 7473fab | 2016-08-19 14:05:40 +0200 | [diff] [blame] | 164 | out << "#include <hidl/HidlSupport.h>\n"; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 165 | out << "#include <hidl/IServiceManager.h>\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 166 | |
| 167 | if (isInterface) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 168 | out << "#include <hwbinder/Status.h>\n"; |
| 169 | } |
| 170 | |
| 171 | out << "#include <utils/NativeHandle.h>\n\n"; |
| 172 | |
| 173 | enterLeaveNamespace(out, true /* enter */); |
| 174 | out << "\n"; |
| 175 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 176 | // cut off the leading 'I'. |
| 177 | const std::string baseName = ifaceName.substr(1); |
| 178 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 179 | if (isInterface) { |
| 180 | out << "struct " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 181 | << ifaceName; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 182 | |
| 183 | const Interface *iface = mRootScope->getInterface(); |
| 184 | const Interface *superType = iface->superType(); |
| 185 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 186 | if (superType == NULL) { |
| 187 | out << " : virtual public RefBase"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 188 | } else { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 189 | out << " : public " |
| 190 | << superType->fullName(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | out << " {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 194 | |
| 195 | out.indent(); |
| 196 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | status_t err = emitTypeDeclarations(out); |
| 200 | |
| 201 | if (err != OK) { |
| 202 | return err; |
| 203 | } |
| 204 | |
| 205 | if (isInterface) { |
| 206 | const Interface *iface = mRootScope->getInterface(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 207 | const Interface *superType = iface->superType(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 208 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 209 | out << "virtual bool isRemote() const { return false; } \n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 210 | bool haveCallbacks = false; |
| 211 | for (const auto &method : iface->methods()) { |
| 212 | const bool returnsValue = !method->results().empty(); |
| 213 | |
| 214 | if (!returnsValue) { |
| 215 | continue; |
| 216 | } |
| 217 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 218 | if (method->canElideCallback() != nullptr) { |
| 219 | continue; |
| 220 | } |
| 221 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 222 | haveCallbacks = true; |
| 223 | |
| 224 | out << "using " |
| 225 | << method->name() |
| 226 | << "_cb = std::function<void(" |
| 227 | << Method::GetSignature(method->results()) |
| 228 | << ")>;\n"; |
| 229 | } |
| 230 | |
| 231 | if (haveCallbacks) { |
| 232 | out << "\n"; |
| 233 | } |
| 234 | |
| 235 | for (const auto &method : iface->methods()) { |
| 236 | const bool returnsValue = !method->results().empty(); |
| 237 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 238 | method->dumpAnnotations(out); |
| 239 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 240 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 241 | if (elidedReturn) { |
| 242 | std::string extra; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 243 | out << "virtual ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 244 | out << elidedReturn->type().getCppResultType(&extra) << "> "; |
| 245 | } else { |
| 246 | out << "virtual ::android::hardware::Status "; |
| 247 | } |
| 248 | |
| 249 | out << method->name() |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 250 | << "(" |
| 251 | << Method::GetSignature(method->args()); |
| 252 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 253 | if (returnsValue && elidedReturn == nullptr) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 254 | if (!method->args().empty()) { |
| 255 | out << ", "; |
| 256 | } |
| 257 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 258 | out << method->name() << "_cb _hidl_cb = nullptr"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | out << ") = 0;\n"; |
| 262 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 263 | |
| 264 | out << "DECLARE_REGISTER_AND_GET_SERVICE(" << baseName << ")\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | if (isInterface) { |
| 268 | out.unindent(); |
| 269 | |
| 270 | out << "};\n"; |
| 271 | } |
| 272 | |
| 273 | out << "\n"; |
| 274 | enterLeaveNamespace(out, false /* enter */); |
| 275 | |
| 276 | out << "\n#endif // " << guard << "\n"; |
| 277 | |
| 278 | return OK; |
| 279 | } |
| 280 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 281 | status_t AST::generateHwBinderHeader(const std::string &outputPath) const { |
| 282 | std::string ifaceName; |
| 283 | if(!AST::isInterface(&ifaceName)) { |
| 284 | // types.hal does not get an HwBinder header. |
| 285 | return OK; |
| 286 | } |
| 287 | |
| 288 | // cut off the leading 'I'. |
| 289 | const std::string baseName = ifaceName.substr(1); |
| 290 | |
| 291 | const std::string klassName = "IHw" + baseName; |
| 292 | |
| 293 | std::string path = outputPath; |
| 294 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
| 295 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
| 296 | path.append(klassName + ".h"); |
| 297 | |
| 298 | FILE* file = fopen(path.c_str(), "w"); |
| 299 | |
| 300 | if (file == NULL) { |
| 301 | return -errno; |
| 302 | } |
| 303 | |
| 304 | Formatter out(file); |
| 305 | |
| 306 | const std::string guard = makeHeaderGuard(klassName); |
| 307 | |
| 308 | out << "#ifndef " << guard << "\n"; |
| 309 | out << "#define " << guard << "\n\n"; |
| 310 | |
| 311 | std::vector<std::string> packageComponents; |
| 312 | getPackageAndVersionComponents( |
| 313 | &packageComponents, false /* cpp_compatible */); |
| 314 | |
| 315 | out << "#include <"; |
| 316 | for (const auto &component : packageComponents) { |
| 317 | out << component << "/"; |
| 318 | } |
| 319 | out << ifaceName << ".h>\n\n"; |
| 320 | |
| 321 | for (const auto &item : mImportedNames) { |
| 322 | if (item.name() == "types") { |
| 323 | continue; |
| 324 | } |
| 325 | |
| 326 | out << "#include <"; |
| 327 | |
| 328 | std::vector<std::string> components; |
| 329 | item.getPackageAndVersionComponents( |
| 330 | &components, false /* cpp_compatible */); |
| 331 | |
| 332 | for (const auto &component : components) { |
| 333 | out << component << "/"; |
| 334 | } |
| 335 | |
| 336 | // cut off the leading I |
| 337 | const std::string itemBaseName = item.name().substr(1); |
| 338 | |
| 339 | out << "Bn" |
| 340 | << itemBaseName |
| 341 | << ".h>\n"; |
| 342 | } |
| 343 | |
| 344 | out << "\n"; |
| 345 | |
| 346 | out << "#include <hidl/HidlSupport.h>\n"; |
| 347 | out << "#include <hwbinder/IBinder.h>\n"; |
| 348 | out << "#include <hwbinder/IInterface.h>\n"; |
| 349 | out << "#include <hwbinder/Status.h>\n"; |
| 350 | |
| 351 | out << "\n"; |
| 352 | |
| 353 | enterLeaveNamespace(out, true /* enter */); |
| 354 | out << "\n"; |
| 355 | |
| 356 | out << "struct " |
| 357 | << klassName |
| 358 | << " : public " |
| 359 | << ifaceName; |
| 360 | |
| 361 | const Interface *iface = mRootScope->getInterface(); |
| 362 | const Interface *superType = iface->superType(); |
| 363 | |
| 364 | out << ", public ::android::hardware::IInterface"; |
| 365 | |
| 366 | out << " {\n"; |
| 367 | |
| 368 | out.indent(); |
| 369 | |
| 370 | out << "DECLARE_HWBINDER_META_INTERFACE(" << baseName << ");\n\n"; |
| 371 | |
| 372 | out << "enum Call {\n"; |
| 373 | out.indent(); |
| 374 | |
| 375 | bool first = true; |
| 376 | for (const auto &method : iface->methods()) { |
| 377 | out << upcase(method->name()); |
| 378 | |
| 379 | if (first) { |
| 380 | out << " = "; |
| 381 | if (superType != NULL) { |
| 382 | out << superType->fqName().cppNamespace() |
| 383 | << "::IHw" |
| 384 | << superType->getBaseName() |
| 385 | << "::Call::CallCount"; |
| 386 | } else { |
| 387 | out << "::android::hardware::IBinder::FIRST_CALL_TRANSACTION"; |
| 388 | } |
| 389 | |
| 390 | first = false; |
| 391 | } |
| 392 | |
| 393 | out << ",\n"; |
| 394 | } |
| 395 | |
| 396 | out << "CallCount\n"; |
| 397 | |
| 398 | out.unindent(); |
| 399 | out << "};\n\n"; |
| 400 | |
| 401 | out.unindent(); |
| 402 | |
| 403 | out << "};\n\n"; |
| 404 | |
| 405 | enterLeaveNamespace(out, false /* enter */); |
| 406 | |
| 407 | out << "\n#endif // " << guard << "\n"; |
| 408 | |
| 409 | return OK; |
| 410 | } |
| 411 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 412 | status_t AST::emitTypeDeclarations(Formatter &out) const { |
| 413 | return mRootScope->emitTypeDeclarations(out); |
| 414 | } |
| 415 | |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 416 | status_t AST::generateHeaderMethodSignatures( |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 417 | Formatter &out, bool stub) const { |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 418 | const Interface *iface = mRootScope->getInterface(); |
| 419 | |
| 420 | std::vector<const Interface *> chain; |
| 421 | while (iface != NULL) { |
| 422 | chain.push_back(iface); |
| 423 | iface = iface->superType(); |
| 424 | } |
| 425 | |
| 426 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 427 | const Interface *superInterface = *it; |
| 428 | |
| 429 | out << "// Methods from " |
| 430 | << superInterface->fullName() |
| 431 | << " follow.\n"; |
| 432 | |
| 433 | for (const auto &method : superInterface->methods()) { |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 434 | if (stub) { |
| 435 | out << "inline "; |
| 436 | } |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 437 | const bool returnsValue = !method->results().empty(); |
| 438 | |
| 439 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 440 | |
| 441 | if (elidedReturn == nullptr) { |
| 442 | out << "::android::hardware::Status "; |
| 443 | } else { |
| 444 | std::string extra; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 445 | out << "::android::hardware::Return<"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 446 | out << elidedReturn->type().getCppResultType(&extra) << "> "; |
| 447 | } |
| 448 | out << method->name() |
| 449 | << "(" |
| 450 | << Method::GetSignature(method->args()); |
| 451 | |
| 452 | if (returnsValue && elidedReturn == nullptr) { |
| 453 | if (!method->args().empty()) { |
| 454 | out << ", "; |
| 455 | } |
| 456 | |
| 457 | out << method->name() << "_cb _hidl_cb"; |
| 458 | } |
| 459 | |
| 460 | out << ") "; |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 461 | if (stub) { |
| 462 | out << " {\n"; |
| 463 | out.indent(); |
| 464 | out << "return mImpl->" |
| 465 | << method->name() |
| 466 | << "("; |
| 467 | bool first = true; |
| 468 | for (const auto &arg : method->args()) { |
| 469 | if (!first) { |
| 470 | out << ", "; |
| 471 | } |
| 472 | first = false; |
| 473 | out << arg->name(); |
| 474 | } |
| 475 | if (returnsValue && elidedReturn == nullptr) { |
| 476 | if (!method->args().empty()) { |
| 477 | out << ", "; |
| 478 | } |
| 479 | |
| 480 | out << "_hidl_cb"; |
| 481 | } |
| 482 | out << ");\n"; |
| 483 | out.unindent(); |
| 484 | out << "}"; |
| 485 | } else { |
| 486 | out << "override"; |
| 487 | } |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 488 | out << ";\n"; |
| 489 | } |
| 490 | |
| 491 | out << "\n"; |
| 492 | } |
| 493 | |
| 494 | return OK; |
| 495 | } |
| 496 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 497 | status_t AST::generateStubHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 498 | std::string ifaceName; |
| 499 | if (!AST::isInterface(&ifaceName)) { |
| 500 | // types.hal does not get a stub header. |
| 501 | return OK; |
| 502 | } |
| 503 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 504 | // cut off the leading 'I'. |
| 505 | const std::string baseName = ifaceName.substr(1); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 506 | const std::string klassName = "Bn" + baseName; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 507 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 508 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 509 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 510 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 511 | path.append(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 512 | path.append(".h"); |
| 513 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 514 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 515 | FILE *file = fopen(path.c_str(), "w"); |
| 516 | |
| 517 | if (file == NULL) { |
| 518 | return -errno; |
| 519 | } |
| 520 | |
| 521 | Formatter out(file); |
| 522 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 523 | const std::string guard = makeHeaderGuard(klassName); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 524 | |
| 525 | out << "#ifndef " << guard << "\n"; |
| 526 | out << "#define " << guard << "\n\n"; |
| 527 | |
| 528 | std::vector<std::string> packageComponents; |
| 529 | getPackageAndVersionComponents( |
| 530 | &packageComponents, false /* cpp_compatible */); |
| 531 | |
| 532 | out << "#include <"; |
| 533 | for (const auto &component : packageComponents) { |
| 534 | out << component << "/"; |
| 535 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 536 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 537 | |
| 538 | enterLeaveNamespace(out, true /* enter */); |
| 539 | out << "\n"; |
| 540 | |
| 541 | out << "struct " |
| 542 | << "Bn" |
| 543 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 544 | << " : public ::android::hardware::BnInterface<I" |
| 545 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 546 | << "> {\n"; |
| 547 | |
| 548 | out.indent(); |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 549 | out << "explicit Bn" |
| 550 | << baseName |
| 551 | << "(const ::android::sp<" << ifaceName << "> &_hidl_impl);" |
| 552 | << "\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 553 | out << "::android::status_t onTransact(\n"; |
| 554 | out.indent(); |
| 555 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 556 | out << "uint32_t _hidl_code,\n"; |
| 557 | out << "const ::android::hardware::Parcel &_hidl_data,\n"; |
| 558 | out << "::android::hardware::Parcel *_hidl_reply,\n"; |
| 559 | out << "uint32_t _hidl_flags = 0,\n"; |
Iliyan Malchev | 62c3d18 | 2016-08-16 20:33:39 -0700 | [diff] [blame] | 560 | out << "TransactCallback _hidl_cb = nullptr) override;\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 561 | out.unindent(); |
| 562 | out.unindent(); |
| 563 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 564 | generateHeaderMethodSignatures(out, true); // stub |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 565 | out.unindent(); |
| 566 | |
| 567 | out << "};\n\n"; |
| 568 | |
| 569 | enterLeaveNamespace(out, false /* enter */); |
| 570 | |
| 571 | out << "\n#endif // " << guard << "\n"; |
| 572 | |
| 573 | return OK; |
| 574 | } |
| 575 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 576 | status_t AST::generateProxyHeader(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 577 | std::string ifaceName; |
| 578 | if (!AST::isInterface(&ifaceName)) { |
| 579 | // types.hal does not get a proxy header. |
| 580 | return OK; |
| 581 | } |
| 582 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 583 | // cut off the leading 'I'. |
| 584 | const std::string baseName = ifaceName.substr(1); |
| 585 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 586 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 587 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 588 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 589 | path.append("Bp"); |
| 590 | path.append(baseName); |
| 591 | path.append(".h"); |
| 592 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 593 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 594 | FILE *file = fopen(path.c_str(), "w"); |
| 595 | |
| 596 | if (file == NULL) { |
| 597 | return -errno; |
| 598 | } |
| 599 | |
| 600 | Formatter out(file); |
| 601 | |
| 602 | const std::string guard = makeHeaderGuard("Bp" + baseName); |
| 603 | |
| 604 | out << "#ifndef " << guard << "\n"; |
| 605 | out << "#define " << guard << "\n\n"; |
| 606 | |
| 607 | std::vector<std::string> packageComponents; |
| 608 | getPackageAndVersionComponents( |
| 609 | &packageComponents, false /* cpp_compatible */); |
| 610 | |
| 611 | out << "#include <"; |
| 612 | for (const auto &component : packageComponents) { |
| 613 | out << component << "/"; |
| 614 | } |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 615 | out << "IHw" << baseName << ".h>\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 616 | |
| 617 | enterLeaveNamespace(out, true /* enter */); |
| 618 | out << "\n"; |
| 619 | |
| 620 | out << "struct " |
| 621 | << "Bp" |
| 622 | << baseName |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 623 | << " : public ::android::hardware::BpInterface<IHw" |
| 624 | << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 625 | << "> {\n"; |
| 626 | |
| 627 | out.indent(); |
| 628 | |
| 629 | out << "explicit Bp" |
| 630 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 631 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl);" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 632 | << "\n\n"; |
| 633 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 634 | out << "virtual bool isRemote() const { return true; } \n\n"; |
| 635 | |
| 636 | generateHeaderMethodSignatures(out, false); // proxy |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 637 | |
| 638 | out.unindent(); |
| 639 | |
| 640 | out << "};\n\n"; |
| 641 | |
| 642 | enterLeaveNamespace(out, false /* enter */); |
| 643 | |
| 644 | out << "\n#endif // " << guard << "\n"; |
| 645 | |
| 646 | return OK; |
| 647 | } |
| 648 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 649 | status_t AST::generateAllSource(const std::string &outputPath) const { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 650 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 651 | std::string path = outputPath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 652 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 653 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 654 | |
| 655 | std::string ifaceName; |
| 656 | std::string baseName; |
| 657 | |
| 658 | bool isInterface = true; |
| 659 | if (!AST::isInterface(&ifaceName)) { |
| 660 | baseName = "types"; |
| 661 | isInterface = false; |
| 662 | } else { |
| 663 | baseName = ifaceName.substr(1); // cut off the leading 'I'. |
| 664 | } |
| 665 | |
| 666 | path.append(baseName); |
| 667 | |
| 668 | if (baseName != "types") { |
| 669 | path.append("All"); |
| 670 | } |
| 671 | |
| 672 | path.append(".cpp"); |
| 673 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 674 | CHECK(Coordinator::MakeParentHierarchy(path)); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 675 | FILE *file = fopen(path.c_str(), "w"); |
| 676 | |
| 677 | if (file == NULL) { |
| 678 | return -errno; |
| 679 | } |
| 680 | |
| 681 | Formatter out(file); |
| 682 | |
| 683 | std::vector<std::string> packageComponents; |
| 684 | getPackageAndVersionComponents( |
| 685 | &packageComponents, false /* cpp_compatible */); |
| 686 | |
| 687 | std::string prefix; |
| 688 | for (const auto &component : packageComponents) { |
| 689 | prefix += component; |
| 690 | prefix += "/"; |
| 691 | } |
| 692 | |
| 693 | if (isInterface) { |
| 694 | out << "#include <" << prefix << "/Bp" << baseName << ".h>\n"; |
| 695 | out << "#include <" << prefix << "/Bn" << baseName << ".h>\n"; |
| 696 | } else { |
| 697 | out << "#include <" << prefix << "types.h>\n"; |
| 698 | } |
| 699 | |
| 700 | out << "\n"; |
| 701 | |
| 702 | enterLeaveNamespace(out, true /* enter */); |
| 703 | out << "\n"; |
| 704 | |
| 705 | status_t err = generateTypeSource(out, ifaceName); |
| 706 | |
| 707 | if (err == OK && isInterface) { |
| 708 | err = generateProxySource(out, baseName); |
| 709 | } |
| 710 | |
| 711 | if (err == OK && isInterface) { |
| 712 | err = generateStubSource(out, baseName); |
| 713 | } |
| 714 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 715 | if (err == OK && isInterface) { |
| 716 | out << "IMPLEMENT_REGISTER_AND_GET_SERVICE(" << baseName << ")\n"; |
| 717 | } |
| 718 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 719 | enterLeaveNamespace(out, false /* enter */); |
| 720 | |
| 721 | return err; |
| 722 | } |
| 723 | |
| 724 | status_t AST::generateTypeSource( |
| 725 | Formatter &out, const std::string &ifaceName) const { |
| 726 | return mRootScope->emitTypeDefinitions(out, ifaceName); |
| 727 | } |
| 728 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 729 | void AST::declareCppReaderLocals( |
| 730 | Formatter &out, const std::vector<TypedVar *> &args) const { |
| 731 | if (args.empty()) { |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | for (const auto &arg : args) { |
| 736 | const Type &type = arg->type(); |
| 737 | |
| 738 | std::string extra; |
| 739 | out << type.getCppResultType(&extra) |
| 740 | << " " |
| 741 | << arg->name() |
| 742 | << extra |
| 743 | << ";\n"; |
| 744 | } |
| 745 | |
| 746 | out << "\n"; |
| 747 | } |
| 748 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 749 | void AST::emitCppReaderWriter( |
| 750 | Formatter &out, |
| 751 | const std::string &parcelObj, |
| 752 | bool parcelObjIsPointer, |
| 753 | const TypedVar *arg, |
| 754 | bool isReader, |
| 755 | Type::ErrorMode mode) const { |
| 756 | const Type &type = arg->type(); |
| 757 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 758 | type.emitReaderWriter( |
| 759 | out, |
| 760 | arg->name(), |
| 761 | parcelObj, |
| 762 | parcelObjIsPointer, |
| 763 | isReader, |
| 764 | mode); |
| 765 | } |
| 766 | |
| 767 | status_t AST::generateProxySource( |
| 768 | Formatter &out, const std::string &baseName) const { |
| 769 | const std::string klassName = "Bp" + baseName; |
| 770 | |
| 771 | out << klassName |
| 772 | << "::" |
| 773 | << klassName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 774 | << "(const ::android::sp<::android::hardware::IBinder> &_hidl_impl)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 775 | |
| 776 | out.indent(); |
| 777 | out.indent(); |
| 778 | |
| 779 | out << ": BpInterface" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 780 | << "<IHw" |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 781 | << baseName |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 782 | << ">(_hidl_impl) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 783 | |
| 784 | out.unindent(); |
| 785 | out.unindent(); |
| 786 | out << "}\n\n"; |
| 787 | |
| 788 | const Interface *iface = mRootScope->getInterface(); |
| 789 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 790 | std::vector<const Interface *> chain; |
| 791 | while (iface != NULL) { |
| 792 | chain.push_back(iface); |
| 793 | iface = iface->superType(); |
| 794 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 795 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 796 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 797 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 798 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 799 | for (const auto &method : superInterface->methods()) { |
| 800 | const bool returnsValue = !method->results().empty(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 801 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 802 | const TypedVar *elidedReturn = method->canElideCallback(); |
| 803 | if (elidedReturn) { |
| 804 | std::string extra; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 805 | out << "::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 806 | out << elidedReturn->type().getCppResultType(&extra) << "> "; |
| 807 | } else { |
| 808 | out << "::android::hardware::Status "; |
| 809 | } |
| 810 | |
| 811 | out << klassName |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 812 | << "::" |
| 813 | << method->name() |
| 814 | << "(" |
| 815 | << Method::GetSignature(method->args()); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 816 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 817 | if (returnsValue && elidedReturn == nullptr) { |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 818 | if (!method->args().empty()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 819 | out << ", "; |
| 820 | } |
| 821 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 822 | out << method->name() << "_cb _hidl_cb"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 825 | out << ") {\n"; |
| 826 | |
| 827 | out.indent(); |
| 828 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 829 | out << "::android::hardware::Parcel _hidl_data;\n"; |
| 830 | out << "::android::hardware::Parcel _hidl_reply;\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 831 | out << "::android::status_t _hidl_err;\n"; |
| 832 | out << "::android::hardware::Status _hidl_status;\n\n"; |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 833 | declareCppReaderLocals(out, method->results()); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 834 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 835 | out << "_hidl_err = _hidl_data.writeInterfaceToken(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 836 | << superInterface->fqName().cppNamespace() |
| 837 | << "::IHw" |
| 838 | << superInterface->getBaseName() |
| 839 | << "::descriptor);\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 840 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 841 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 842 | |
| 843 | for (const auto &arg : method->args()) { |
| 844 | emitCppReaderWriter( |
| 845 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 846 | "_hidl_data", |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 847 | false /* parcelObjIsPointer */, |
| 848 | arg, |
| 849 | false /* reader */, |
| 850 | Type::ErrorMode_Goto); |
| 851 | } |
| 852 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 853 | out << "_hidl_err = remote()->transact(" |
| 854 | << superInterface->fqName().cppNamespace() |
| 855 | << "::IHw" |
| 856 | << superInterface->getBaseName() |
| 857 | << "::Call::" |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 858 | << upcase(method->name()) |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 859 | << ", _hidl_data, &_hidl_reply"; |
| 860 | if (method->isOneway()) { |
| 861 | out << ", ::android::hardware::IBinder::FLAG_ONEWAY"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 862 | } |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 863 | out << ");\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 864 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 865 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 866 | |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 867 | if (!method->isOneway()) { |
| 868 | out << "_hidl_err = _hidl_status.readFromParcel(_hidl_reply);\n"; |
| 869 | out << "if (_hidl_err != ::android::OK) { goto _hidl_error; }\n\n"; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 870 | out << "if (!_hidl_status.isOk()) { return _hidl_status; }\n\n"; |
| 871 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 872 | for (const auto &arg : method->results()) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 873 | emitCppReaderWriter( |
| 874 | out, |
| 875 | "_hidl_reply", |
| 876 | false /* parcelObjIsPointer */, |
| 877 | arg, |
| 878 | true /* reader */, |
| 879 | Type::ErrorMode_Goto); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 882 | if (returnsValue && elidedReturn == nullptr) { |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 883 | out << "if (_hidl_cb != nullptr) {\n"; |
| 884 | out.indent(); |
| 885 | out << "_hidl_cb("; |
| 886 | |
| 887 | bool first = true; |
| 888 | for (const auto &arg : method->results()) { |
| 889 | if (!first) { |
| 890 | out << ", "; |
| 891 | } |
| 892 | |
| 893 | if (arg->type().resultNeedsDeref()) { |
| 894 | out << "*"; |
| 895 | } |
| 896 | out << arg->name(); |
| 897 | |
| 898 | first = false; |
| 899 | } |
| 900 | |
| 901 | out << ");\n"; |
| 902 | out.unindent(); |
| 903 | out << "}\n\n"; |
| 904 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 905 | } |
| 906 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 907 | if (elidedReturn != nullptr) { |
| 908 | std::string extra; |
| 909 | |
| 910 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 911 | out << "return ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 912 | out << elidedReturn->type().getCppResultType(&extra) |
| 913 | << ">(" << elidedReturn->name() << ");\n\n"; |
| 914 | |
| 915 | out.unindent(); |
| 916 | out << "_hidl_error:\n"; |
| 917 | out.indent(); |
| 918 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
Iliyan Malchev | 2b6591b | 2016-08-18 19:15:19 -0700 | [diff] [blame] | 919 | out << "return ::android::hardware::Return<"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 920 | out << method->results().at(0)->type().getCppResultType(&extra) |
| 921 | << ">(_hidl_status);\n"; |
| 922 | } else { |
| 923 | out.unindent(); |
| 924 | out << "_hidl_error:\n"; |
| 925 | out.indent(); |
| 926 | out << "_hidl_status.setFromStatusT(_hidl_err);\n"; |
| 927 | out << "return _hidl_status;\n"; |
| 928 | } |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 929 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 930 | out.unindent(); |
| 931 | out << "}\n\n"; |
| 932 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | return OK; |
| 936 | } |
| 937 | |
| 938 | status_t AST::generateStubSource( |
| 939 | Formatter &out, const std::string &baseName) const { |
| 940 | out << "IMPLEMENT_HWBINDER_META_INTERFACE(" |
| 941 | << baseName |
| 942 | << ", \"" |
| 943 | << mPackage.string() |
| 944 | << "::I" |
| 945 | << baseName |
| 946 | << "\");\n\n"; |
| 947 | |
| 948 | const std::string klassName = "Bn" + baseName; |
| 949 | |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 950 | out << klassName |
| 951 | << "::" |
| 952 | << klassName |
| 953 | << "(const ::android::sp<I" << baseName <<"> &_hidl_impl)\n"; |
| 954 | |
| 955 | out.indent(); |
| 956 | out.indent(); |
| 957 | |
| 958 | out << ": BnInterface" |
| 959 | << "<I" |
| 960 | << baseName |
| 961 | << ", IHw" |
| 962 | << baseName |
| 963 | << ">(_hidl_impl) {\n"; |
| 964 | |
| 965 | out.unindent(); |
| 966 | out.unindent(); |
| 967 | out << "}\n\n"; |
| 968 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 969 | out << "::android::status_t " << klassName << "::onTransact(\n"; |
| 970 | |
| 971 | out.indent(); |
| 972 | out.indent(); |
| 973 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 974 | out << "uint32_t _hidl_code,\n" |
| 975 | << "const ::android::hardware::Parcel &_hidl_data,\n" |
| 976 | << "::android::hardware::Parcel *_hidl_reply,\n" |
| 977 | << "uint32_t _hidl_flags,\n" |
| 978 | << "TransactCallback _hidl_cb) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 979 | |
| 980 | out.unindent(); |
| 981 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 982 | out << "::android::status_t _hidl_err = ::android::OK;\n\n"; |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 983 | out << "switch (_hidl_code) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 984 | out.indent(); |
| 985 | |
| 986 | const Interface *iface = mRootScope->getInterface(); |
| 987 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 988 | std::vector<const Interface *> chain; |
| 989 | while (iface != NULL) { |
| 990 | chain.push_back(iface); |
| 991 | iface = iface->superType(); |
| 992 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 993 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 994 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { |
| 995 | const Interface *superInterface = *it; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 996 | |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 997 | for (const auto &method : superInterface->methods()) { |
| 998 | out << "case " |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 999 | << superInterface->fqName().cppNamespace() |
| 1000 | << "::IHw" |
| 1001 | << superInterface->getBaseName() |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1002 | << "::Call::" |
| 1003 | << upcase(method->name()) |
| 1004 | << ":\n{\n"; |
| 1005 | |
| 1006 | out.indent(); |
| 1007 | |
| 1008 | status_t err = |
| 1009 | generateStubSourceForMethod(out, superInterface, method); |
| 1010 | |
| 1011 | if (err != OK) { |
| 1012 | return err; |
| 1013 | } |
| 1014 | |
| 1015 | out.unindent(); |
| 1016 | out << "}\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1017 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | out << "default:\n{\n"; |
| 1021 | out.indent(); |
| 1022 | |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1023 | out << "return ::android::hardware::BnInterface<I" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 1024 | << baseName << ", IHw" << baseName |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1025 | << ">::onTransact(\n"; |
| 1026 | |
| 1027 | out.indent(); |
| 1028 | out.indent(); |
| 1029 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1030 | out << "_hidl_code, _hidl_data, _hidl_reply, " |
| 1031 | << "_hidl_flags, _hidl_cb);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1032 | |
| 1033 | out.unindent(); |
| 1034 | out.unindent(); |
| 1035 | |
| 1036 | out.unindent(); |
| 1037 | out << "}\n"; |
| 1038 | |
| 1039 | out.unindent(); |
| 1040 | out << "}\n\n"; |
| 1041 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1042 | out << "if (_hidl_err == ::android::UNEXPECTED_NULL) {\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1043 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1044 | out << "_hidl_err = ::android::hardware::Status::fromExceptionCode(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1045 | out.indent(); |
| 1046 | out.indent(); |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1047 | out << "::android::hardware::Status::EX_NULL_POINTER)\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1048 | out.indent(); |
| 1049 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1050 | out << ".writeToParcel(_hidl_reply);\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1051 | out.unindent(); |
| 1052 | out.unindent(); |
| 1053 | out.unindent(); |
| 1054 | out.unindent(); |
| 1055 | |
| 1056 | out.unindent(); |
| 1057 | out << "}\n\n"; |
| 1058 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1059 | out << "return _hidl_err;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1060 | |
| 1061 | out.unindent(); |
| 1062 | out << "}\n\n"; |
| 1063 | |
| 1064 | return OK; |
| 1065 | } |
| 1066 | |
| 1067 | status_t AST::generateStubSourceForMethod( |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1068 | Formatter &out, const Interface *iface, const Method *method) const { |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1069 | out << "if (!_hidl_data.enforceInterface(" |
Steven Moreland | 4078631 | 2016-08-16 10:29:40 -0700 | [diff] [blame^] | 1070 | << iface->fqName().cppNamespace() |
| 1071 | << "::IHw" |
| 1072 | << iface->getBaseName() |
| 1073 | << "::descriptor)) {\n"; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 1074 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1075 | out.indent(); |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1076 | out << "_hidl_err = ::android::BAD_TYPE;\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1077 | out << "break;\n"; |
| 1078 | out.unindent(); |
| 1079 | out << "}\n\n"; |
| 1080 | |
Andreas Huber | e7ff228 | 2016-08-16 13:50:03 -0700 | [diff] [blame] | 1081 | declareCppReaderLocals(out, method->args()); |
| 1082 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1083 | for (const auto &arg : method->args()) { |
| 1084 | emitCppReaderWriter( |
| 1085 | out, |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1086 | "_hidl_data", |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1087 | false /* parcelObjIsPointer */, |
| 1088 | arg, |
| 1089 | true /* reader */, |
| 1090 | Type::ErrorMode_Break); |
| 1091 | } |
| 1092 | |
| 1093 | const bool returnsValue = !method->results().empty(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1094 | const TypedVar *elidedReturn = method->canElideCallback(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1095 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1096 | if (elidedReturn != nullptr) { |
| 1097 | std::string extra; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1098 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1099 | out << elidedReturn->type().getCppResultType(&extra) << " "; |
| 1100 | out << elidedReturn->name() << " = "; |
| 1101 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1102 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1103 | bool first = true; |
| 1104 | for (const auto &arg : method->args()) { |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1105 | if (!first) { |
| 1106 | out << ", "; |
| 1107 | } |
| 1108 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1109 | if (arg->type().resultNeedsDeref()) { |
| 1110 | out << "*"; |
| 1111 | } |
| 1112 | |
| 1113 | out << arg->name(); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1114 | |
| 1115 | first = false; |
| 1116 | } |
| 1117 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1118 | out << ");\n\n"; |
Andreas Huber | 8a82ff7 | 2016-08-04 10:29:39 -0700 | [diff] [blame] | 1119 | out << "::android::hardware::Status::ok()" |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1120 | << ".writeToParcel(_hidl_reply);\n\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1121 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1122 | elidedReturn->type().emitReaderWriter( |
| 1123 | out, |
| 1124 | elidedReturn->name(), |
| 1125 | "_hidl_reply", |
| 1126 | true, /* parcelObjIsPointer */ |
| 1127 | false, /* isReader */ |
| 1128 | Type::ErrorMode_Ignore); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1129 | |
Iliyan Malchev | 549e259 | 2016-08-10 08:59:12 -0700 | [diff] [blame] | 1130 | out << "_hidl_cb(*_hidl_reply);\n"; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1131 | } else { |
| 1132 | if (returnsValue) { |
| 1133 | out << "bool _hidl_callbackCalled = false;\n\n"; |
| 1134 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1135 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1136 | out << "::android::hardware::Status _hidl_status(\n"; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1137 | out.indent(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1138 | out.indent(); |
| 1139 | out << method->name() << "("; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1140 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1141 | bool first = true; |
| 1142 | for (const auto &arg : method->args()) { |
| 1143 | if (!first) { |
| 1144 | out << ", "; |
| 1145 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1146 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1147 | if (arg->type().resultNeedsDeref()) { |
| 1148 | out << "*"; |
| 1149 | } |
| 1150 | |
| 1151 | out << arg->name(); |
| 1152 | |
| 1153 | first = false; |
| 1154 | } |
| 1155 | |
| 1156 | if (returnsValue) { |
| 1157 | if (!first) { |
| 1158 | out << ", "; |
| 1159 | } |
| 1160 | |
| 1161 | out << "[&]("; |
| 1162 | |
| 1163 | first = true; |
| 1164 | for (const auto &arg : method->results()) { |
| 1165 | if (!first) { |
| 1166 | out << ", "; |
| 1167 | } |
| 1168 | |
| 1169 | out << "const auto &" << arg->name(); |
| 1170 | |
| 1171 | first = false; |
| 1172 | } |
| 1173 | |
| 1174 | out << ") {\n"; |
| 1175 | out.indent(); |
| 1176 | out << "_hidl_callbackCalled = true;\n\n"; |
| 1177 | |
| 1178 | out << "::android::hardware::Status::ok()" |
| 1179 | << ".writeToParcel(_hidl_reply);\n\n"; |
| 1180 | |
| 1181 | for (const auto &arg : method->results()) { |
| 1182 | emitCppReaderWriter( |
| 1183 | out, |
| 1184 | "_hidl_reply", |
| 1185 | true /* parcelObjIsPointer */, |
| 1186 | arg, |
| 1187 | false /* reader */, |
| 1188 | Type::ErrorMode_Ignore); |
| 1189 | } |
| 1190 | |
| 1191 | out << "_hidl_cb(*_hidl_reply);\n"; |
| 1192 | |
| 1193 | out.unindent(); |
| 1194 | out << "}\n"; |
| 1195 | } |
| 1196 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1197 | out.unindent(); |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 1198 | out.unindent(); |
| 1199 | out << "));\n\n"; |
| 1200 | |
| 1201 | if (returnsValue) { |
| 1202 | out << "if (!_hidl_callbackCalled) {\n"; |
| 1203 | out.indent(); |
| 1204 | } |
| 1205 | |
| 1206 | out << "_hidl_err = _hidl_status.writeToParcel(_hidl_reply);\n"; |
| 1207 | |
| 1208 | if (returnsValue) { |
| 1209 | out.unindent(); |
| 1210 | out << "}\n\n"; |
| 1211 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | out << "break;\n"; |
| 1215 | |
| 1216 | return OK; |
| 1217 | } |
| 1218 | |
| 1219 | } // namespace android |
| 1220 | |