blob: 0c3ba939e0d25fe033d3cb82066053291c75e3cd [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Zhang5158db42016-08-10 10:25:20 -070017#include "AST.h"
18
19#include "Annotation.h"
20#include "Coordinator.h"
21#include "Formatter.h"
22#include "Interface.h"
23#include "Method.h"
24#include "Scope.h"
25
26#include <android-base/logging.h>
27#include <string>
28#include <vector>
29
30namespace android {
31
32// Remove the double quotas in a string.
33static std::string removeQuotes(const std::string in) {
34 std::string out{in};
35 return out.substr(1, out.size() - 2);
36}
37
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070038status_t AST::emitVtsTypeDeclarations(Formatter &out) const {
39 std::set<AST *> allImportedASTs;
40 return emitVtsTypeDeclarationsHelper(out, &allImportedASTs);
41}
42
43status_t AST::emitVtsTypeDeclarationsHelper(
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070044 Formatter &out,
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070045 std::set<AST *> *allImportSet) const {
46 // First, generate vts type declaration for all imported AST.
47 for (const auto &ast : mImportedASTs) {
48 // Already processed, skip.
49 if (allImportSet->find(ast) != allImportSet->end()) {
50 continue;
51 }
52 allImportSet->insert(ast);
53 std::string ifaceName;
54 // We only care about types.hal.
55 if (!ast->isInterface(&ifaceName)) {
56 status_t status = ast->emitVtsTypeDeclarationsHelper(
57 out, allImportSet);
58 if (status != OK) {
59 return status;
60 }
61 }
62 }
63 // Next, generate vts type declaration for the current AST.
64 for (const auto &type : mRootScope->getSubTypes()) {
Zhuoyao Zhang36849132016-08-25 17:18:44 -070065 // Skip for TypeDef as it is just an alias of a defined type.
66 if (type->isTypeDef()) {
67 continue;
68 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070069 out << "attribute: {\n";
70 out.indent();
71 status_t status = type->emitVtsTypeDeclarations(out);
72 if (status != OK) {
73 return status;
74 }
75 out.unindent();
76 out << "}\n\n";
77 }
78 return OK;
79}
80
81status_t AST::generateVts(const std::string &outputPath) const {
82 std::string path = outputPath;
83 path.append(mCoordinator->convertPackageRootToPath(mPackage));
84 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
85
86 std::string ifaceName;
87 std::string baseName;
88
89 bool isInterface = true;
90 if (!AST::isInterface(&ifaceName)) {
91 baseName = "types";
92 isInterface = false;
93 } else {
94 baseName = ifaceName.substr(1); // cut off the leading 'I'.
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";
110
111 // Get the component_type for interface from annotation.
112 if (isInterface) {
113 const Interface *iface = mRootScope->getInterface();
114 Annotation *annotation = iface->annotations().valueFor("hal_type");
115 if (annotation != NULL) {
116 std::vector<std::string> * values = annotation->params().valueFor(
117 "type");
118 if (values != NULL) {
119 out << "component_type: "
120 << removeQuotes(values->at(0))
121 << "\n";
122 }
123 }
124 }
125
126 out << "component_type_version: " << mPackage.version().substr(1) << "\n";
127 out << "component_name: \""
128 << (isInterface ? ifaceName : "types")
129 << "\"\n\n";
130
131 out << "package: \"" << mPackage.package() << "\"\n\n";
132
133 for (const auto &item : mImportedNames) {
134 out << "import: \"" << item.string() << "\"\n";
135 }
136
137 out << "\n";
138
139 if (isInterface) {
140 const Interface *iface = mRootScope->getInterface();
141 out << "interface: {\n";
142 out.indent();
143
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700144 std::vector<const Interface *> chain;
145 while (iface != NULL) {
146 chain.push_back(iface);
147 iface = iface->superType();
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700148 }
149
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700150 // Generate all the attribute declarations first.
151 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
152 const Interface *superInterface = *it;
153 status_t status = superInterface->emitVtsAttributeDeclaration(out);
154 if (status != OK) {
155 return status;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700156 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700157 }
158
159 // Generate all the method declarations.
160 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
161 const Interface *superInterface = *it;
162 status_t status = superInterface->emitVtsMethodDeclaration(out);
163 if (status != OK) {
164 return status;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700165 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700166 }
167
168 out.unindent();
169 out << "}\n";
170 } else {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700171 status_t status = emitVtsTypeDeclarations(out);
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700172 if (status != OK) {
173 return status;
174 }
175 }
176 return OK;
177}
178
179} // namespace android
180
181
182
183