blob: f695bec6f698e2c5035a41288bd21a92137e8694 [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 {
Steven Moreland19f11b52017-05-12 18:22:21 -070033 if (AST::isInterface()) {
34 const Interface *iface = mRootScope->getInterface();
35 return iface->emitVtsAttributeDeclaration(out);
Zhuoyao Zhang864c7712016-08-16 15:35:28 -070036 }
Steven Moreland19f11b52017-05-12 18:22:21 -070037
Zhuoyao Zhang40404fe2017-02-11 19:50:33 -080038 for (const auto &type : mRootScope->getSubTypes()) {
Steven Moreland19f11b52017-05-12 18:22:21 -070039 // Skip for TypeDef as it is just an alias of a defined type.
40 if (type->isTypeDef()) {
41 continue;
42 }
43 out << "attribute: {\n";
44 out.indent();
45 status_t status = type->emitVtsTypeDeclarations(out);
46 if (status != OK) {
47 return status;
48 }
49 out.unindent();
50 out << "}\n\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070051 }
Steven Moreland19f11b52017-05-12 18:22:21 -070052
53 return OK;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070054}
55
56status_t AST::generateVts(const std::string &outputPath) const {
Steven Moreland19f11b52017-05-12 18:22:21 -070057 std::string baseName = AST::getBaseName();
58 const Interface *iface = AST::getInterface();
59
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070060 std::string path = outputPath;
61 path.append(mCoordinator->convertPackageRootToPath(mPackage));
62 path.append(mCoordinator->getPackagePath(mPackage, true /* relative */));
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070063 path.append(baseName);
64 path.append(".vts");
65
66 CHECK(Coordinator::MakeParentHierarchy(path));
67 FILE *file = fopen(path.c_str(), "w");
68
69 if (file == NULL) {
70 return -errno;
71 }
72
73 Formatter out(file);
74
75 out << "component_class: HAL_HIDL\n";
Yifan Hong90ea87f2016-11-01 14:25:47 -070076 out << "component_type_version: " << mPackage.version()
Zhuoyao Zhang8f492942016-09-28 14:27:56 -070077 << "\n";
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070078 out << "component_name: \""
Steven Moreland19f11b52017-05-12 18:22:21 -070079 << (iface ? iface->localName() : "types")
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070080 << "\"\n\n";
81
82 out << "package: \"" << mPackage.package() << "\"\n\n";
83
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -080084 // Generate import statement for all imported interface/types.
85 std::set<FQName> allImportedNames;
86 getAllImportedNames(&allImportedNames);
87 for (const auto &name : allImportedNames) {
Zhuoyao Zhang5deccac2016-12-16 10:07:34 -080088 // ignore IBase.
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -080089 if (name != gIBaseFqName) {
90 out << "import: \"" << name.string() << "\"\n";
Zhuoyao Zhang5deccac2016-12-16 10:07:34 -080091 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070092 }
93
94 out << "\n";
95
Steven Moreland19f11b52017-05-12 18:22:21 -070096 if (isInterface()) {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -070097 const Interface *iface = mRootScope->getInterface();
98 out << "interface: {\n";
99 out.indent();
100
Yifan Hong10fe0b52016-10-19 14:20:17 -0700101 std::vector<const Interface *> chain = iface->typeChain();
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700102
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700103 // Generate all the attribute declarations first.
Zhuoyao Zhanga6239b32017-01-11 12:48:58 -0800104 status_t status = emitVtsTypeDeclarations(out);
105 if (status != OK) {
106 return status;
107 }
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700108 // Generate all the method declarations.
109 for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
110 const Interface *superInterface = *it;
111 status_t status = superInterface->emitVtsMethodDeclaration(out);
112 if (status != OK) {
113 return status;
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700114 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700115 }
116
117 out.unindent();
118 out << "}\n";
119 } else {
Zhuoyao Zhang864c7712016-08-16 15:35:28 -0700120 status_t status = emitVtsTypeDeclarations(out);
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700121 if (status != OK) {
122 return status;
123 }
124 }
125 return OK;
126}
127
128} // namespace android