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