blob: 1cee4f87c13e0e258f9b48d39a83b7ebd1a8e827 [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
Andreas Hubera2723d22016-07-29 15:36:07 -070019#include "Interface.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070020
Andreas Huber2831d512016-08-15 09:33:47 -070021#include <android-base/logging.h>
Yifan Hong327cfe12016-10-03 10:29:42 -070022#include <hidl-util/Formatter.h>
23#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070024
Andreas Huberc9410c72016-07-28 12:18:40 -070025namespace android {
26
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070027Scope::Scope(const char* localName, const Location& location, Scope* parent)
28 : NamedType(localName, location, parent) {}
Yifan Hongf24fa852016-09-23 11:03:15 -070029Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070030
Steven Morelandd537ab02016-09-12 10:32:01 -070031bool Scope::addType(NamedType *type, std::string *errorMsg) {
32 const std::string &localName = type->localName();
33
34 auto it = mTypeIndexByName.find(localName);
35
36 if (it != mTypeIndexByName.end()) {
Andreas Huber0d0f9a22016-08-17 10:26:11 -070037 *errorMsg = "A type named '";
38 (*errorMsg) += localName;
39 (*errorMsg) += "' is already declared in the current scope.";
Andreas Huberc7dfef32016-08-16 10:57:14 -070040
Andreas Huberc9410c72016-07-28 12:18:40 -070041 return false;
42 }
43
44 size_t index = mTypes.size();
45 mTypes.push_back(type);
Steven Morelandd537ab02016-09-12 10:32:01 -070046 mTypeIndexByName[localName] = index;
Andreas Huberc9410c72016-07-28 12:18:40 -070047
48 return true;
49}
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
Yifan Hong244e82d2016-11-11 11:13:57 -0800106status_t Scope::forEachType(std::function<status_t(Type *)> func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700107 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800108 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700109
110 if (err != OK) {
111 return err;
112 }
113 }
114
115 return OK;
116}
117
Yifan Hong244e82d2016-11-11 11:13:57 -0800118status_t Scope::emitTypeDeclarations(Formatter &out) const {
119 return forEachType([&](Type *type) {
120 return type->emitTypeDeclarations(out);
121 });
122}
123
Andreas Hubere3f769a2016-10-10 10:54:44 -0700124status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800125 return forEachType([&](Type *type) {
126 return type->emitGlobalTypeDeclarations(out);
127 });
128}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700129
Yifan Hong244e82d2016-11-11 11:13:57 -0800130status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
131 return forEachType([&](Type *type) {
132 return type->emitGlobalHwDeclarations(out);
133 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700134}
135
Andreas Huber85eabdb2016-08-25 11:24:49 -0700136status_t Scope::emitJavaTypeDeclarations(
137 Formatter &out, bool atTopLevel) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800138 return forEachType([&](Type *type) {
139 return type->emitJavaTypeDeclarations(out, atTopLevel);
140 });
Andreas Huber2831d512016-08-15 09:33:47 -0700141}
142
Andreas Huber881227d2016-08-02 14:20:21 -0700143status_t Scope::emitTypeDefinitions(
144 Formatter &out, const std::string prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800145 return forEachType([&](Type *type) {
146 return type->emitTypeDefinitions(out, prefix);
147 });
Andreas Huber881227d2016-08-02 14:20:21 -0700148}
149
Steven Morelandd537ab02016-09-12 10:32:01 -0700150const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700151 return mTypes;
152}
153
154status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800155 return forEachType([&](Type *type) {
156 return type->emitVtsTypeDeclarations(out);
157 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700158}
159
Andreas Huber70a59e12016-08-16 12:57:01 -0700160bool Scope::isJavaCompatible() const {
161 for (const auto &type : mTypes) {
162 if (!type->isJavaCompatible()) {
163 return false;
164 }
165 }
166
167 return true;
168}
169
Andreas Huber60d3b222017-03-30 09:10:56 -0700170bool Scope::containsPointer() const {
171 for (const auto &type : mTypes) {
172 if (type->containsPointer()) {
173 return true;
174 }
175 }
176
177 return false;
178}
179
Andreas Huber019d21d2016-10-03 12:59:47 -0700180void Scope::appendToExportedTypesVector(
181 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800182 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700183 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800184 return OK;
185 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700186}
187
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700188RootScope::RootScope(const char* localName, const Location& location, Scope* parent)
189 : Scope(localName, location, parent) {}
190RootScope::~RootScope() {}
191
192std::string RootScope::typeName() const {
193 return "(root scope)";
194}
195
Yifan Hongf24fa852016-09-23 11:03:15 -0700196LocalIdentifier::LocalIdentifier(){}
197LocalIdentifier::~LocalIdentifier(){}
198
199bool LocalIdentifier::isEnumValue() const {
200 return false;
201}
202
Timur Iskhakov7296af12017-08-09 21:52:48 +0000203ConstantExpression* LocalIdentifier::constExpr() const {
204 return nullptr;
205}
206
Andreas Huberc9410c72016-07-28 12:18:40 -0700207} // namespace android
208