blob: cd245f83977364e7555c1cc966cd40cbae55c7b6 [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>
25#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070026
Andreas Huberc9410c72016-07-28 12:18:40 -070027namespace android {
28
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070029Scope::Scope(const char* localName, const Location& location, Scope* parent)
30 : NamedType(localName, location, parent) {}
Yifan Hongf24fa852016-09-23 11:03:15 -070031Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070032
Steven Morelandd537ab02016-09-12 10:32:01 -070033bool Scope::addType(NamedType *type, std::string *errorMsg) {
34 const std::string &localName = type->localName();
35
36 auto it = mTypeIndexByName.find(localName);
37
38 if (it != mTypeIndexByName.end()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070039 *errorMsg = "A type named '";
40 (*errorMsg) += localName;
41 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070042
Andreas Huberc9410c72016-07-28 12:18:40 -070043 return false;
44 }
45
46 size_t index = mTypes.size();
47 mTypes.push_back(type);
Steven Morelandd537ab02016-09-12 10:32:01 -070048 mTypeIndexByName[localName] = index;
Andreas Huberc9410c72016-07-28 12:18:40 -070049
50 return true;
51}
52
Yifan Hongae16eed2016-09-23 13:25:25 -070053NamedType *Scope::lookupType(const FQName &fqName) const {
Yifan Hong327cfe12016-10-03 10:29:42 -070054 CHECK(fqName.package().empty() && fqName.version().empty());
55 if (!fqName.valueName().empty()) {
56 LOG(WARNING) << fqName.string() << " does not refer to a type.";
57 return nullptr;
58 }
59 std::vector<std::string> names = fqName.names();
60 CHECK_GT(names.size(), 0u);
61 auto it = mTypeIndexByName.find(names[0]);
Andreas Huberc9410c72016-07-28 12:18:40 -070062
Yifan Hong327cfe12016-10-03 10:29:42 -070063 if (it == mTypeIndexByName.end()) {
64 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -070065 }
66
Yifan Hong327cfe12016-10-03 10:29:42 -070067 NamedType *outerType = mTypes[it->second];
68 if (names.size() == 1) {
69 return outerType;
70 }
71 if (!outerType->isScope()) {
72 // more than one names, but the first name is not a scope
73 return nullptr;
74 }
75 Scope *outerScope = static_cast<Scope *>(outerType);
76 // *slowly* pop first element
77 names.erase(names.begin());
78 FQName innerName(names);
79 return outerScope->lookupType(innerName);
Andreas Huberc9410c72016-07-28 12:18:40 -070080}
81
Yifan Hongf24fa852016-09-23 11:03:15 -070082LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
83 return NULL;
84}
85
Andreas Huber5345ec22016-07-29 13:33:27 -070086bool Scope::isScope() const {
87 return true;
88}
89
Andreas Huber881227d2016-08-02 14:20:21 -070090Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070091 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070092 return static_cast<Interface *>(mTypes[0]);
93 }
94
95 return NULL;
96}
97
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070098bool Scope::containsInterfaces() const {
99 for (const NamedType *type : mTypes) {
100 if (type->isInterface()) {
101 return true;
102 }
103 }
104
105 return false;
106}
107
Timur Iskhakove9ccfa22017-08-14 15:07:03 -0700108const std::vector<Annotation*>& Scope::annotations() const {
109 return mAnnotations;
110}
111
112void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
113 CHECK(mAnnotations.empty());
114 CHECK(annotations != nullptr);
115 mAnnotations = *annotations;
116}
117
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700118std::vector<const Type*> Scope::getDefinedTypes() const {
119 std::vector<const Type*> ret;
120 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
Timur Iskhakov33431e62017-08-21 17:31:23 -0700121 return ret;
122}
123
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700124std::vector<const ConstantExpression*> Scope::getConstantExpressions() const {
125 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700126 for (const auto* annotation : mAnnotations) {
127 const auto& retAnnotation = annotation->getConstantExpressions();
128 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
129 }
130 return ret;
131}
132
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700133status_t Scope::forEachType(const std::function<status_t(Type *)> &func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700134 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800135 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700136
137 if (err != OK) {
138 return err;
139 }
140 }
141
142 return OK;
143}
144
Yifan Hong244e82d2016-11-11 11:13:57 -0800145status_t Scope::emitTypeDeclarations(Formatter &out) const {
146 return forEachType([&](Type *type) {
147 return type->emitTypeDeclarations(out);
148 });
149}
150
Andreas Hubere3f769a2016-10-10 10:54:44 -0700151status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800152 return forEachType([&](Type *type) {
153 return type->emitGlobalTypeDeclarations(out);
154 });
155}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700156
Yifan Hong244e82d2016-11-11 11:13:57 -0800157status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
158 return forEachType([&](Type *type) {
159 return type->emitGlobalHwDeclarations(out);
160 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700161}
162
Andreas Huber85eabdb2016-08-25 11:24:49 -0700163status_t Scope::emitJavaTypeDeclarations(
164 Formatter &out, bool atTopLevel) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800165 return forEachType([&](Type *type) {
166 return type->emitJavaTypeDeclarations(out, atTopLevel);
167 });
Andreas Huber2831d512016-08-15 09:33:47 -0700168}
169
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700170status_t Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800171 return forEachType([&](Type *type) {
172 return type->emitTypeDefinitions(out, prefix);
173 });
Andreas Huber881227d2016-08-02 14:20:21 -0700174}
175
Steven Morelandd537ab02016-09-12 10:32:01 -0700176const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700177 return mTypes;
178}
179
180status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800181 return forEachType([&](Type *type) {
182 return type->emitVtsTypeDeclarations(out);
183 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700184}
185
Andreas Huber70a59e12016-08-16 12:57:01 -0700186bool Scope::isJavaCompatible() const {
187 for (const auto &type : mTypes) {
188 if (!type->isJavaCompatible()) {
189 return false;
190 }
191 }
192
193 return true;
194}
195
Andreas Huber60d3b222017-03-30 09:10:56 -0700196bool Scope::containsPointer() const {
197 for (const auto &type : mTypes) {
198 if (type->containsPointer()) {
199 return true;
200 }
201 }
202
203 return false;
204}
205
Andreas Huber019d21d2016-10-03 12:59:47 -0700206void Scope::appendToExportedTypesVector(
207 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800208 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700209 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800210 return OK;
211 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700212}
213
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700214////////////////////////////////////////
215
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700216RootScope::RootScope(const char* localName, const Location& location, Scope* parent)
217 : Scope(localName, location, parent) {}
218RootScope::~RootScope() {}
219
220std::string RootScope::typeName() const {
221 return "(root scope)";
222}
223
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700224status_t RootScope::validate() const {
225 CHECK(annotations().empty());
226 return Scope::validate();
227}
228
229////////////////////////////////////////
230
Yifan Hongf24fa852016-09-23 11:03:15 -0700231LocalIdentifier::LocalIdentifier(){}
232LocalIdentifier::~LocalIdentifier(){}
233
234bool LocalIdentifier::isEnumValue() const {
235 return false;
236}
237
Timur Iskhakov7296af12017-08-09 21:52:48 +0000238ConstantExpression* LocalIdentifier::constExpr() const {
239 return nullptr;
240}
241
Andreas Huberc9410c72016-07-28 12:18:40 -0700242} // namespace android
243