blob: f9a844cab0169c342287b5621796107cee2319b4 [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
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700117status_t Scope::forEachType(const std::function<status_t(Type *)> &func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700118 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800119 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700120
121 if (err != OK) {
122 return err;
123 }
124 }
125
126 return OK;
127}
128
Yifan Hong244e82d2016-11-11 11:13:57 -0800129status_t Scope::emitTypeDeclarations(Formatter &out) const {
130 return forEachType([&](Type *type) {
131 return type->emitTypeDeclarations(out);
132 });
133}
134
Andreas Hubere3f769a2016-10-10 10:54:44 -0700135status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800136 return forEachType([&](Type *type) {
137 return type->emitGlobalTypeDeclarations(out);
138 });
139}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700140
Yifan Hong244e82d2016-11-11 11:13:57 -0800141status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
142 return forEachType([&](Type *type) {
143 return type->emitGlobalHwDeclarations(out);
144 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700145}
146
Andreas Huber85eabdb2016-08-25 11:24:49 -0700147status_t Scope::emitJavaTypeDeclarations(
148 Formatter &out, bool atTopLevel) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800149 return forEachType([&](Type *type) {
150 return type->emitJavaTypeDeclarations(out, atTopLevel);
151 });
Andreas Huber2831d512016-08-15 09:33:47 -0700152}
153
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700154status_t Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800155 return forEachType([&](Type *type) {
156 return type->emitTypeDefinitions(out, prefix);
157 });
Andreas Huber881227d2016-08-02 14:20:21 -0700158}
159
Steven Morelandd537ab02016-09-12 10:32:01 -0700160const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700161 return mTypes;
162}
163
164status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800165 return forEachType([&](Type *type) {
166 return type->emitVtsTypeDeclarations(out);
167 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700168}
169
Andreas Huber70a59e12016-08-16 12:57:01 -0700170bool Scope::isJavaCompatible() const {
171 for (const auto &type : mTypes) {
172 if (!type->isJavaCompatible()) {
173 return false;
174 }
175 }
176
177 return true;
178}
179
Andreas Huber60d3b222017-03-30 09:10:56 -0700180bool Scope::containsPointer() const {
181 for (const auto &type : mTypes) {
182 if (type->containsPointer()) {
183 return true;
184 }
185 }
186
187 return false;
188}
189
Andreas Huber019d21d2016-10-03 12:59:47 -0700190void Scope::appendToExportedTypesVector(
191 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800192 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700193 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800194 return OK;
195 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700196}
197
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700198RootScope::RootScope(const char* localName, const Location& location, Scope* parent)
199 : Scope(localName, location, parent) {}
200RootScope::~RootScope() {}
201
202std::string RootScope::typeName() const {
203 return "(root scope)";
204}
205
Yifan Hongf24fa852016-09-23 11:03:15 -0700206LocalIdentifier::LocalIdentifier(){}
207LocalIdentifier::~LocalIdentifier(){}
208
209bool LocalIdentifier::isEnumValue() const {
210 return false;
211}
212
Timur Iskhakov7296af12017-08-09 21:52:48 +0000213ConstantExpression* LocalIdentifier::constExpr() const {
214 return nullptr;
215}
216
Andreas Huberc9410c72016-07-28 12:18:40 -0700217} // namespace android
218