blob: 2ee09faf2bbbe65e0e82a4563348cfc9a68f3b41 [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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "Scope.h"
18
Timur Iskhakove9ccfa22017-08-14 15:07:03 -070019#include "Annotation.h"
Andreas Hubera2723d22016-07-29 15:36:07 -070020#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070021
Andreas Huber2831d512016-08-15 09:33:47 -070022#include <android-base/logging.h>
Yifan Hong327cfe12016-10-03 10:29:42 -070023#include <hidl-util/Formatter.h>
24#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070025
Andreas Huberc9410c72016-07-28 12:18:40 -070026namespace android {
27
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070028Scope::Scope(const char* localName, const Location& location, Scope* parent)
29 : NamedType(localName, location, parent) {}
Yifan Hongf24fa852016-09-23 11:03:15 -070030Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070031
Steven Morelandd537ab02016-09-12 10:32:01 -070032bool Scope::addType(NamedType *type, std::string *errorMsg) {
33 const std::string &localName = type->localName();
34
35 auto it = mTypeIndexByName.find(localName);
36
37 if (it != mTypeIndexByName.end()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070038 *errorMsg = "A type named '";
39 (*errorMsg) += localName;
40 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070041
Andreas Huberc9410c72016-07-28 12:18:40 -070042 return false;
43 }
44
45 size_t index = mTypes.size();
46 mTypes.push_back(type);
Steven Morelandd537ab02016-09-12 10:32:01 -070047 mTypeIndexByName[localName] = index;
Andreas Huberc9410c72016-07-28 12:18:40 -070048
49 return true;
50}
51
Yifan Hongae16eed2016-09-23 13:25:25 -070052NamedType *Scope::lookupType(const FQName &fqName) const {
Yifan Hong327cfe12016-10-03 10:29:42 -070053 CHECK(fqName.package().empty() && fqName.version().empty());
54 if (!fqName.valueName().empty()) {
55 LOG(WARNING) << fqName.string() << " does not refer to a type.";
56 return nullptr;
57 }
58 std::vector<std::string> names = fqName.names();
59 CHECK_GT(names.size(), 0u);
60 auto it = mTypeIndexByName.find(names[0]);
Andreas Huberc9410c72016-07-28 12:18:40 -070061
Yifan Hong327cfe12016-10-03 10:29:42 -070062 if (it == mTypeIndexByName.end()) {
63 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -070064 }
65
Yifan Hong327cfe12016-10-03 10:29:42 -070066 NamedType *outerType = mTypes[it->second];
67 if (names.size() == 1) {
68 return outerType;
69 }
70 if (!outerType->isScope()) {
71 // more than one names, but the first name is not a scope
72 return nullptr;
73 }
74 Scope *outerScope = static_cast<Scope *>(outerType);
75 // *slowly* pop first element
76 names.erase(names.begin());
77 FQName innerName(names);
78 return outerScope->lookupType(innerName);
Andreas Huberc9410c72016-07-28 12:18:40 -070079}
80
Yifan Hongf24fa852016-09-23 11:03:15 -070081LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
82 return NULL;
83}
84
Andreas Huber5345ec22016-07-29 13:33:27 -070085bool Scope::isScope() const {
86 return true;
87}
88
Andreas Huber881227d2016-08-02 14:20:21 -070089Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070090 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070091 return static_cast<Interface *>(mTypes[0]);
92 }
93
94 return NULL;
95}
96
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070097bool Scope::containsInterfaces() const {
98 for (const NamedType *type : mTypes) {
99 if (type->isInterface()) {
100 return true;
101 }
102 }
103
104 return false;
105}
106
Timur Iskhakove9ccfa22017-08-14 15:07:03 -0700107const std::vector<Annotation*>& Scope::annotations() const {
108 return mAnnotations;
109}
110
111void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
112 CHECK(mAnnotations.empty());
113 CHECK(annotations != nullptr);
114 mAnnotations = *annotations;
115}
116
Timur Iskhakov33431e62017-08-21 17:31:23 -0700117std::vector<Type*> Scope::getDefinedTypes() const {
118 std::vector<Type*> ret;
119 for (auto* type : mTypes) {
120 ret.push_back(type);
121 }
122 return ret;
123}
124
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700125status_t Scope::forEachType(const std::function<status_t(Type *)> &func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700126 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800127 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700128
129 if (err != OK) {
130 return err;
131 }
132 }
133
134 return OK;
135}
136
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700137status_t Scope::resolveInheritance() {
138 status_t err = forEachType(&Type::resolveInheritance);
139 if (err != OK) return err;
140 return NamedType::resolveInheritance();
141}
142
143status_t Scope::evaluate() {
144 status_t err = forEachType(&Type::evaluate);
145 if (err != OK) return err;
146
147 for (auto* annotation : mAnnotations) {
148 err = annotation->evaluate();
149 if (err != OK) return err;
150 }
151
152 return NamedType::evaluate();
153}
154
155status_t Scope::validate() const {
156 status_t err = forEachType(&Type::validate);
157 if (err != OK) return err;
158
159 for (const auto* annotation : mAnnotations) {
160 err = annotation->validate();
161 if (err != OK) return err;
162 }
163
164 return NamedType::validate();
165}
166
Yifan Hong244e82d2016-11-11 11:13:57 -0800167status_t Scope::emitTypeDeclarations(Formatter &out) const {
168 return forEachType([&](Type *type) {
169 return type->emitTypeDeclarations(out);
170 });
171}
172
Andreas Hubere3f769a2016-10-10 10:54:44 -0700173status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800174 return forEachType([&](Type *type) {
175 return type->emitGlobalTypeDeclarations(out);
176 });
177}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700178
Yifan Hong244e82d2016-11-11 11:13:57 -0800179status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
180 return forEachType([&](Type *type) {
181 return type->emitGlobalHwDeclarations(out);
182 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700183}
184
Andreas Huber85eabdb2016-08-25 11:24:49 -0700185status_t Scope::emitJavaTypeDeclarations(
186 Formatter &out, bool atTopLevel) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800187 return forEachType([&](Type *type) {
188 return type->emitJavaTypeDeclarations(out, atTopLevel);
189 });
Andreas Huber2831d512016-08-15 09:33:47 -0700190}
191
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700192status_t Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800193 return forEachType([&](Type *type) {
194 return type->emitTypeDefinitions(out, prefix);
195 });
Andreas Huber881227d2016-08-02 14:20:21 -0700196}
197
Steven Morelandd537ab02016-09-12 10:32:01 -0700198const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700199 return mTypes;
200}
201
202status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800203 return forEachType([&](Type *type) {
204 return type->emitVtsTypeDeclarations(out);
205 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700206}
207
Andreas Huber70a59e12016-08-16 12:57:01 -0700208bool Scope::isJavaCompatible() const {
209 for (const auto &type : mTypes) {
210 if (!type->isJavaCompatible()) {
211 return false;
212 }
213 }
214
215 return true;
216}
217
Andreas Huber60d3b222017-03-30 09:10:56 -0700218bool Scope::containsPointer() const {
219 for (const auto &type : mTypes) {
220 if (type->containsPointer()) {
221 return true;
222 }
223 }
224
225 return false;
226}
227
Andreas Huber019d21d2016-10-03 12:59:47 -0700228void Scope::appendToExportedTypesVector(
229 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800230 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700231 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800232 return OK;
233 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700234}
235
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700236////////////////////////////////////////
237
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700238RootScope::RootScope(const char* localName, const Location& location, Scope* parent)
239 : Scope(localName, location, parent) {}
240RootScope::~RootScope() {}
241
242std::string RootScope::typeName() const {
243 return "(root scope)";
244}
245
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700246status_t RootScope::validate() const {
247 CHECK(annotations().empty());
248 return Scope::validate();
249}
250
251////////////////////////////////////////
252
Yifan Hongf24fa852016-09-23 11:03:15 -0700253LocalIdentifier::LocalIdentifier(){}
254LocalIdentifier::~LocalIdentifier(){}
255
256bool LocalIdentifier::isEnumValue() const {
257 return false;
258}
259
Timur Iskhakov7296af12017-08-09 21:52:48 +0000260ConstantExpression* LocalIdentifier::constExpr() const {
261 return nullptr;
262}
263
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700264status_t LocalIdentifier::evaluate() {
265 return OK;
266}
267
268status_t LocalIdentifier::validate() const {
269 CHECK(isEnumValue());
270 return OK;
271}
272
Andreas Huberc9410c72016-07-28 12:18:40 -0700273} // namespace android
274