blob: fe71487142cfe7b741da7b089019c5c61e066b88 [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"
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070021#include "Interface.h"
22#include "Method.h"
23#include "Scope.h"
24
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070025#include <hidl-util/Formatter.h>
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070026#include <android-base/logging.h>
27#include <string>
28#include <vector>
29
30namespace android {
31
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070032status_t AST::emitVtsTypeDeclarations(Formatter &out) const {
33 std::set<AST *> allImportedASTs;
34 return emitVtsTypeDeclarationsHelper(out, &allImportedASTs);
35}
36
37status_t AST::emitVtsTypeDeclarationsHelper(
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070038 Formatter &out,
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070039 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;
48 // We only care about types.hal.
49 if (!ast->isInterface(&ifaceName)) {
50 status_t status = ast->emitVtsTypeDeclarationsHelper(
51 out, allImportSet);
52 if (status != OK) {
53 return status;
54 }
55 }
56 }
57 // Next, generate vts type declaration for the current AST.
58 for (const auto &type : mRootScope->getSubTypes()) {
Zhuoyao Zhang36849132016-08-25 17:18:44 -070059 // Skip for TypeDef as it is just an alias of a defined type.
60 if (type->isTypeDef()) {
61 continue;
62 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070063 out << "attribute: {\n";
64 out.indent();
65 status_t status = type->emitVtsTypeDeclarations(out);
66 if (status != OK) {
67 return status;
68 }
69 out.unindent();
70 out << "}\n\n";
71 }
72 return OK;
73}
74
75status_t AST::generateVts(const std::string &outputPath) const {
76 std::string path = outputPath;
77 path.append(mCoordinator->convertPackageRootToPath(mPackage));
78 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
79
80 std::string ifaceName;
81 std::string baseName;
82
83 bool isInterface = true;
84 if (!AST::isInterface(&ifaceName)) {
85 baseName = "types";
86 isInterface = false;
87 } else {
88 baseName = ifaceName.substr(1); // cut off the leading 'I'.
89 }
90
91 path.append(baseName);
92 path.append(".vts");
93
94 CHECK(Coordinator::MakeParentHierarchy(path));
95 FILE *file = fopen(path.c_str(), "w");
96
97 if (file == NULL) {
98 return -errno;
99 }
100
101 Formatter out(file);
102
103 out << "component_class: HAL_HIDL\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700104 out << "component_type_version: " << mPackage.version().substr(1) << "\n";
105 out << "component_name: \""
106 << (isInterface ? ifaceName : "types")
107 << "\"\n\n";
108
109 out << "package: \"" << mPackage.package() << "\"\n\n";
110
111 for (const auto &item : mImportedNames) {
112 out << "import: \"" << item.string() << "\"\n";
113 }
114
115 out << "\n";
116
117 if (isInterface) {
118 const Interface *iface = mRootScope->getInterface();
119 out << "interface: {\n";
120 out.indent();
121
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700122 std::vector<const Interface *> chain;
123 while (iface != NULL) {
124 chain.push_back(iface);
125 iface = iface->superType();
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700126 }
127
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700128 // Generate all the attribute declarations first.
129 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
130 const Interface *superInterface = *it;
131 status_t status = superInterface->emitVtsAttributeDeclaration(out);
132 if (status != OK) {
133 return status;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700134 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700135 }
136
137 // Generate all the method declarations.
138 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
139 const Interface *superInterface = *it;
140 status_t status = superInterface->emitVtsMethodDeclaration(out);
141 if (status != OK) {
142 return status;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700143 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700144 }
145
146 out.unindent();
147 out << "}\n";
148 } else {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700149 status_t status = emitVtsTypeDeclarations(out);
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700150 if (status != OK) {
151 return status;
152 }
153 }
154 return OK;
155}
156
157} // namespace android
158
159
160
161