blob: 4f3585f28b953588592789c47a400c004e4d2101 [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";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700110 out << "component_type_version: " << mPackage.version().substr(1) << "\n";
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) {
118 out << "import: \"" << item.string() << "\"\n";
119 }
120
121 out << "\n";
122
123 if (isInterface) {
124 const Interface *iface = mRootScope->getInterface();
125 out << "interface: {\n";
126 out.indent();
127
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700128 std::vector<const Interface *> chain;
129 while (iface != NULL) {
130 chain.push_back(iface);
131 iface = iface->superType();
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700132 }
133
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700134 // Generate all the attribute declarations first.
135 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
136 const Interface *superInterface = *it;
137 status_t status = superInterface->emitVtsAttributeDeclaration(out);
138 if (status != OK) {
139 return status;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700140 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700141 }
142
143 // Generate all the method declarations.
144 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
145 const Interface *superInterface = *it;
146 status_t status = superInterface->emitVtsMethodDeclaration(out);
147 if (status != OK) {
148 return status;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700149 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700150 }
151
152 out.unindent();
153 out << "}\n";
154 } else {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700155 status_t status = emitVtsTypeDeclarations(out);
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700156 if (status != OK) {
157 return status;
158 }
159 }
160 return OK;
161}
162
163} // namespace android
164
165
166
167