blob: b9f90b930c52be9aec827304c0acc184b32a987a [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"
Timur Iskhakov891a8662017-08-25 21:53:48 -070020#include "ConstantExpression.h"
Andreas Hubera2723d22016-07-29 15:36:07 -070021#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070022
Andreas Huber2831d512016-08-15 09:33:47 -070023#include <android-base/logging.h>
Yifan Hong327cfe12016-10-03 10:29:42 -070024#include <hidl-util/Formatter.h>
Timur Iskhakov565b0132017-09-06 18:07:11 -070025#include <iostream>
Yifan Hong327cfe12016-10-03 10:29:42 -070026#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070027
Andreas Huberc9410c72016-07-28 12:18:40 -070028namespace android {
29
Timur Iskhakov565b0132017-09-06 18:07:11 -070030Scope::Scope(const char* localName, const FQName& fullName, const Location& location, Scope* parent)
31 : NamedType(localName, fullName, location, parent) {}
Yifan Hongf24fa852016-09-23 11:03:15 -070032Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070033
Timur Iskhakov565b0132017-09-06 18:07:11 -070034void Scope::addType(NamedType* type) {
Andreas Huberc9410c72016-07-28 12:18:40 -070035 size_t index = mTypes.size();
36 mTypes.push_back(type);
Timur Iskhakov565b0132017-09-06 18:07:11 -070037 mTypeIndexByName[type->localName()] = index;
38}
Andreas Huberc9410c72016-07-28 12:18:40 -070039
Timur Iskhakov565b0132017-09-06 18:07:11 -070040status_t Scope::validateUniqueNames() const {
41 for (const auto* type : mTypes) {
42 if (mTypes[mTypeIndexByName.at(type->localName())] != type) {
43 std::cerr << "ERROR: A type named '" << type->localName()
44 << "' is already declared in the scope at " << type->location() << "\n";
45 return UNKNOWN_ERROR;
46 }
47 }
48 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -070049}
50
Yifan Hongae16eed2016-09-23 13:25:25 -070051NamedType *Scope::lookupType(const FQName &fqName) const {
Yifan Hong327cfe12016-10-03 10:29:42 -070052 CHECK(fqName.package().empty() && fqName.version().empty());
53 if (!fqName.valueName().empty()) {
54 LOG(WARNING) << fqName.string() << " does not refer to a type.";
55 return nullptr;
56 }
57 std::vector<std::string> names = fqName.names();
58 CHECK_GT(names.size(), 0u);
59 auto it = mTypeIndexByName.find(names[0]);
Andreas Huberc9410c72016-07-28 12:18:40 -070060
Yifan Hong327cfe12016-10-03 10:29:42 -070061 if (it == mTypeIndexByName.end()) {
62 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -070063 }
64
Yifan Hong327cfe12016-10-03 10:29:42 -070065 NamedType *outerType = mTypes[it->second];
66 if (names.size() == 1) {
67 return outerType;
68 }
69 if (!outerType->isScope()) {
70 // more than one names, but the first name is not a scope
71 return nullptr;
72 }
73 Scope *outerScope = static_cast<Scope *>(outerType);
74 // *slowly* pop first element
75 names.erase(names.begin());
76 FQName innerName(names);
77 return outerScope->lookupType(innerName);
Andreas Huberc9410c72016-07-28 12:18:40 -070078}
79
Yifan Hongf24fa852016-09-23 11:03:15 -070080LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
81 return NULL;
82}
83
Andreas Huber5345ec22016-07-29 13:33:27 -070084bool Scope::isScope() const {
85 return true;
86}
87
Andreas Huber881227d2016-08-02 14:20:21 -070088Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070089 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070090 return static_cast<Interface *>(mTypes[0]);
91 }
92
93 return NULL;
94}
95
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070096bool Scope::containsInterfaces() const {
97 for (const NamedType *type : mTypes) {
98 if (type->isInterface()) {
99 return true;
100 }
101 }
102
103 return false;
104}
105
Timur Iskhakove9ccfa22017-08-14 15:07:03 -0700106const std::vector<Annotation*>& Scope::annotations() const {
107 return mAnnotations;
108}
109
110void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
111 CHECK(mAnnotations.empty());
112 CHECK(annotations != nullptr);
113 mAnnotations = *annotations;
114}
115
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700116std::vector<const Type*> Scope::getDefinedTypes() const {
117 std::vector<const Type*> ret;
118 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
Timur Iskhakov33431e62017-08-21 17:31:23 -0700119 return ret;
120}
121
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700122std::vector<const ConstantExpression*> Scope::getConstantExpressions() const {
123 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700124 for (const auto* annotation : mAnnotations) {
125 const auto& retAnnotation = annotation->getConstantExpressions();
126 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
127 }
128 return ret;
129}
130
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700131status_t Scope::forEachType(const std::function<status_t(Type *)> &func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700132 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800133 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700134
135 if (err != OK) {
136 return err;
137 }
138 }
139
140 return OK;
141}
142
Yifan Hong244e82d2016-11-11 11:13:57 -0800143status_t Scope::emitTypeDeclarations(Formatter &out) const {
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700144 forEachType([&](Type* type) {
145 type->emitTypeForwardDeclaration(out);
146 return OK;
147 });
148
149 return forEachType([&](Type* type) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800150 return type->emitTypeDeclarations(out);
151 });
152}
153
Andreas Hubere3f769a2016-10-10 10:54:44 -0700154status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800155 return forEachType([&](Type *type) {
156 return type->emitGlobalTypeDeclarations(out);
157 });
158}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700159
Yifan Hong244e82d2016-11-11 11:13:57 -0800160status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
161 return forEachType([&](Type *type) {
162 return type->emitGlobalHwDeclarations(out);
163 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700164}
165
Andreas Huber85eabdb2016-08-25 11:24:49 -0700166status_t Scope::emitJavaTypeDeclarations(
167 Formatter &out, bool atTopLevel) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800168 return forEachType([&](Type *type) {
169 return type->emitJavaTypeDeclarations(out, atTopLevel);
170 });
Andreas Huber2831d512016-08-15 09:33:47 -0700171}
172
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700173status_t Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800174 return forEachType([&](Type *type) {
175 return type->emitTypeDefinitions(out, prefix);
176 });
Andreas Huber881227d2016-08-02 14:20:21 -0700177}
178
Steven Morelandd537ab02016-09-12 10:32:01 -0700179const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700180 return mTypes;
181}
182
183status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800184 return forEachType([&](Type *type) {
185 return type->emitVtsTypeDeclarations(out);
186 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700187}
188
Andreas Huber70a59e12016-08-16 12:57:01 -0700189bool Scope::isJavaCompatible() const {
190 for (const auto &type : mTypes) {
191 if (!type->isJavaCompatible()) {
192 return false;
193 }
194 }
195
196 return true;
197}
198
Andreas Huber60d3b222017-03-30 09:10:56 -0700199bool Scope::containsPointer() const {
200 for (const auto &type : mTypes) {
201 if (type->containsPointer()) {
202 return true;
203 }
204 }
205
206 return false;
207}
208
Andreas Huber019d21d2016-10-03 12:59:47 -0700209void Scope::appendToExportedTypesVector(
210 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800211 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700212 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800213 return OK;
214 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700215}
216
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700217////////////////////////////////////////
218
Timur Iskhakov565b0132017-09-06 18:07:11 -0700219RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
220 Scope* parent)
221 : Scope(localName, fullName, location, parent) {}
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700222RootScope::~RootScope() {}
223
224std::string RootScope::typeName() const {
225 return "(root scope)";
226}
227
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700228status_t RootScope::validate() const {
229 CHECK(annotations().empty());
230 return Scope::validate();
231}
232
233////////////////////////////////////////
234
Yifan Hongf24fa852016-09-23 11:03:15 -0700235LocalIdentifier::LocalIdentifier(){}
236LocalIdentifier::~LocalIdentifier(){}
237
238bool LocalIdentifier::isEnumValue() const {
239 return false;
240}
241
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700242const LocalIdentifier* LocalIdentifier::resolve() const {
243 return this;
244}
245
246LocalIdentifier* LocalIdentifier::resolve() {
247 return this;
248}
249
Timur Iskhakov7296af12017-08-09 21:52:48 +0000250ConstantExpression* LocalIdentifier::constExpr() const {
251 return nullptr;
252}
253
Andreas Huberc9410c72016-07-28 12:18:40 -0700254} // namespace android
255