blob: 2acb3f9a2dbd0e089b7c6eb82a44e9f8ba599a15 [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 Zhangc4e10602017-01-27 16:48:05 -080038 Formatter &out, std::set<AST *> *allImportSet) const {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070039 // First, generate vts type declaration for all imported AST.
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -080040 for (const auto &importedName : mImportedNames) {
41 AST *ast = mCoordinator->parse(importedName);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070042 // Already processed, skip.
43 if (allImportSet->find(ast) != allImportSet->end()) {
44 continue;
45 }
46 allImportSet->insert(ast);
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -080047 status_t status = ast->emitVtsTypeDeclarationsHelper(out,
48 allImportSet);
49 if (status != OK) {
50 return status;
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070051 }
52 }
53 // Next, generate vts type declaration for the current AST.
Zhuoyao Zhanga6239b32017-01-11 12:48:58 -080054 std::string ifaceName;
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -080055 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 Zhanga6239b32017-01-11 12:48:58 -080062 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 Zhang36849132016-08-25 17:18:44 -070075 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070076 }
77 return OK;
78}
79
80status_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 Chowdhary3f32c1f2016-09-15 16:53:56 -070093 const Interface *iface = mRootScope->getInterface();
94 baseName = iface->getBaseName();
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070095 }
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 Hong90ea87f2016-11-01 14:25:47 -0700110 out << "component_type_version: " << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700111 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700112 out << "component_name: \""
113 << (isInterface ? ifaceName : "types")
114 << "\"\n\n";
115
116 out << "package: \"" << mPackage.package() << "\"\n\n";
117
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800118 // Generate import statement for all imported interface/types.
119 std::set<FQName> allImportedNames;
120 getAllImportedNames(&allImportedNames);
121 for (const auto &name : allImportedNames) {
Zhuoyao Zhang5deccac2016-12-16 10:07:34 -0800122 // ignore IBase.
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800123 if (name != gIBaseFqName) {
124 out << "import: \"" << name.string() << "\"\n";
Zhuoyao Zhang5deccac2016-12-16 10:07:34 -0800125 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700126 }
127
128 out << "\n";
129
130 if (isInterface) {
131 const Interface *iface = mRootScope->getInterface();
132 out << "interface: {\n";
133 out.indent();
134
Yifan Hong10fe0b52016-10-19 14:20:17 -0700135 std::vector<const Interface *> chain = iface->typeChain();
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700136
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700137 // Generate all the attribute declarations first.
Zhuoyao Zhanga6239b32017-01-11 12:48:58 -0800138 status_t status = emitVtsTypeDeclarations(out);
139 if (status != OK) {
140 return status;
141 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700142 // 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 Zhang5158db42016-08-10 10:25:20 -0700148 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700149 }
150
151 out.unindent();
152 out << "}\n";
153 } else {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700154 status_t status = emitVtsTypeDeclarations(out);
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700155 if (status != OK) {
156 return status;
157 }
158 }
159 return OK;
160}
161
162} // namespace android