| 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 |  | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 17 | #include "AST.h" | 
|  | 18 |  | 
|  | 19 | #include "Annotation.h" | 
|  | 20 | #include "Coordinator.h" | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 21 | #include "Interface.h" | 
|  | 22 | #include "Method.h" | 
|  | 23 | #include "Scope.h" | 
|  | 24 |  | 
| Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 25 | #include <hidl-util/Formatter.h> | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> | 
|  | 27 | #include <string> | 
|  | 28 | #include <vector> | 
|  | 29 |  | 
|  | 30 | namespace android { | 
|  | 31 |  | 
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 32 | status_t AST::emitVtsTypeDeclarations(Formatter &out) const { | 
|  | 33 | std::set<AST *> allImportedASTs; | 
|  | 34 | return emitVtsTypeDeclarationsHelper(out, &allImportedASTs); | 
|  | 35 | } | 
|  | 36 |  | 
|  | 37 | status_t AST::emitVtsTypeDeclarationsHelper( | 
| Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame^] | 38 | Formatter &out, std::set<AST *> *allImportSet) const { | 
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 39 | // First, generate vts type declaration for all imported AST. | 
| Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame^] | 40 | for (const auto &importedName : mImportedNames) { | 
|  | 41 | AST *ast = mCoordinator->parse(importedName); | 
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 42 | // Already processed, skip. | 
|  | 43 | if (allImportSet->find(ast) != allImportSet->end()) { | 
|  | 44 | continue; | 
|  | 45 | } | 
|  | 46 | allImportSet->insert(ast); | 
| Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame^] | 47 | status_t status = ast->emitVtsTypeDeclarationsHelper(out, | 
|  | 48 | allImportSet); | 
|  | 49 | if (status != OK) { | 
|  | 50 | return status; | 
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 51 | } | 
|  | 52 | } | 
|  | 53 | // Next, generate vts type declaration for the current AST. | 
| Zhuoyao Zhang | a6239b3 | 2017-01-11 12:48:58 -0800 | [diff] [blame] | 54 | std::string ifaceName; | 
| Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame^] | 55 | if (AST::isInterface(&ifaceName)) { | 
|  | 56 | const Interface *iface = mRootScope->getInterface(); | 
|  | 57 | status_t status = iface->emitVtsAttributeDeclaration(out); | 
|  | 58 | if (status != OK) { | 
|  | 59 | return status; | 
|  | 60 | } | 
|  | 61 | } else { | 
| Zhuoyao Zhang | a6239b3 | 2017-01-11 12:48:58 -0800 | [diff] [blame] | 62 | for (const auto &type : mRootScope->getSubTypes()) { | 
|  | 63 | // Skip for TypeDef as it is just an alias of a defined type. | 
|  | 64 | if (type->isTypeDef()) { | 
|  | 65 | continue; | 
|  | 66 | } | 
|  | 67 | out << "attribute: {\n"; | 
|  | 68 | out.indent(); | 
|  | 69 | status_t status = type->emitVtsTypeDeclarations(out); | 
|  | 70 | if (status != OK) { | 
|  | 71 | return status; | 
|  | 72 | } | 
|  | 73 | out.unindent(); | 
|  | 74 | out << "}\n\n"; | 
| Zhuoyao Zhang | 3684913 | 2016-08-25 17:18:44 -0700 | [diff] [blame] | 75 | } | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 76 | } | 
|  | 77 | return OK; | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | status_t AST::generateVts(const std::string &outputPath) const { | 
|  | 81 | std::string path = outputPath; | 
|  | 82 | path.append(mCoordinator->convertPackageRootToPath(mPackage)); | 
|  | 83 | path.append(mCoordinator->getPackagePath(mPackage, true /* relative */)); | 
|  | 84 |  | 
|  | 85 | std::string ifaceName; | 
|  | 86 | std::string baseName; | 
|  | 87 |  | 
|  | 88 | bool isInterface = true; | 
|  | 89 | if (!AST::isInterface(&ifaceName)) { | 
|  | 90 | baseName = "types"; | 
|  | 91 | isInterface = false; | 
|  | 92 | } else { | 
| Jayant Chowdhary | 3f32c1f | 2016-09-15 16:53:56 -0700 | [diff] [blame] | 93 | const Interface *iface = mRootScope->getInterface(); | 
|  | 94 | baseName = iface->getBaseName(); | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
|  | 97 | path.append(baseName); | 
|  | 98 | path.append(".vts"); | 
|  | 99 |  | 
|  | 100 | CHECK(Coordinator::MakeParentHierarchy(path)); | 
|  | 101 | FILE *file = fopen(path.c_str(), "w"); | 
|  | 102 |  | 
|  | 103 | if (file == NULL) { | 
|  | 104 | return -errno; | 
|  | 105 | } | 
|  | 106 |  | 
|  | 107 | Formatter out(file); | 
|  | 108 |  | 
|  | 109 | out << "component_class: HAL_HIDL\n"; | 
| Yifan Hong | 90ea87f | 2016-11-01 14:25:47 -0700 | [diff] [blame] | 110 | out << "component_type_version: " << mPackage.version() | 
| Zhuoyao Zhang | 8f49294 | 2016-09-28 14:27:56 -0700 | [diff] [blame] | 111 | << "\n"; | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 112 | out << "component_name: \"" | 
|  | 113 | << (isInterface ? ifaceName : "types") | 
|  | 114 | << "\"\n\n"; | 
|  | 115 |  | 
|  | 116 | out << "package: \"" << mPackage.package() << "\"\n\n"; | 
|  | 117 |  | 
| Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame^] | 118 | // Generate import statement for all imported interface/types. | 
|  | 119 | std::set<FQName> allImportedNames; | 
|  | 120 | getAllImportedNames(&allImportedNames); | 
|  | 121 | for (const auto &name : allImportedNames) { | 
| Zhuoyao Zhang | 5deccac | 2016-12-16 10:07:34 -0800 | [diff] [blame] | 122 | // ignore IBase. | 
| Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame^] | 123 | if (name != gIBaseFqName) { | 
|  | 124 | out << "import: \"" << name.string() << "\"\n"; | 
| Zhuoyao Zhang | 5deccac | 2016-12-16 10:07:34 -0800 | [diff] [blame] | 125 | } | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 126 | } | 
|  | 127 |  | 
|  | 128 | out << "\n"; | 
|  | 129 |  | 
|  | 130 | if (isInterface) { | 
|  | 131 | const Interface *iface = mRootScope->getInterface(); | 
|  | 132 | out << "interface: {\n"; | 
|  | 133 | out.indent(); | 
|  | 134 |  | 
| Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 135 | std::vector<const Interface *> chain = iface->typeChain(); | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 136 |  | 
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 137 | // Generate all the attribute declarations first. | 
| Zhuoyao Zhang | a6239b3 | 2017-01-11 12:48:58 -0800 | [diff] [blame] | 138 | status_t status = emitVtsTypeDeclarations(out); | 
|  | 139 | if (status != OK) { | 
|  | 140 | return status; | 
|  | 141 | } | 
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 142 | // Generate all the method declarations. | 
|  | 143 | for (auto it = chain.rbegin(); it != chain.rend(); ++it) { | 
|  | 144 | const Interface *superInterface = *it; | 
|  | 145 | status_t status = superInterface->emitVtsMethodDeclaration(out); | 
|  | 146 | if (status != OK) { | 
|  | 147 | return status; | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 148 | } | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
|  | 151 | out.unindent(); | 
|  | 152 | out << "}\n"; | 
|  | 153 | } else { | 
| Zhuoyao Zhang | 864c771 | 2016-08-16 15:35:28 -0700 | [diff] [blame] | 154 | status_t status = emitVtsTypeDeclarations(out); | 
| Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 155 | if (status != OK) { | 
|  | 156 | return status; | 
|  | 157 | } | 
|  | 158 | } | 
|  | 159 | return OK; | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | }  // namespace android |