blob: 8f7958e6485eac80fa675e90f41814a0ac55e4f0 [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 Iskhakov458ca362017-09-12 23:16:03 -070025#include <algorithm>
Timur Iskhakov565b0132017-09-06 18:07:11 -070026#include <iostream>
Yifan Hong327cfe12016-10-03 10:29:42 -070027#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070028
Andreas Huberc9410c72016-07-28 12:18:40 -070029namespace android {
30
Timur Iskhakov565b0132017-09-06 18:07:11 -070031Scope::Scope(const char* localName, const FQName& fullName, const Location& location, Scope* parent)
32 : NamedType(localName, fullName, location, parent) {}
Yifan Hongf24fa852016-09-23 11:03:15 -070033Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070034
Timur Iskhakov565b0132017-09-06 18:07:11 -070035void Scope::addType(NamedType* type) {
Andreas Huberc9410c72016-07-28 12:18:40 -070036 size_t index = mTypes.size();
37 mTypes.push_back(type);
Timur Iskhakov565b0132017-09-06 18:07:11 -070038 mTypeIndexByName[type->localName()] = index;
39}
Andreas Huberc9410c72016-07-28 12:18:40 -070040
Timur Iskhakov565b0132017-09-06 18:07:11 -070041status_t Scope::validateUniqueNames() const {
42 for (const auto* type : mTypes) {
43 if (mTypes[mTypeIndexByName.at(type->localName())] != type) {
44 std::cerr << "ERROR: A type named '" << type->localName()
45 << "' is already declared in the scope at " << type->location() << "\n";
46 return UNKNOWN_ERROR;
47 }
48 }
49 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -070050}
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 Iskhakovb58f4182017-08-29 15:19:24 -0700117std::vector<const Type*> Scope::getDefinedTypes() const {
118 std::vector<const Type*> ret;
119 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
Timur Iskhakov33431e62017-08-21 17:31:23 -0700120 return ret;
121}
122
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700123std::vector<const ConstantExpression*> Scope::getConstantExpressions() const {
124 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700125 for (const auto* annotation : mAnnotations) {
126 const auto& retAnnotation = annotation->getConstantExpressions();
127 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
128 }
129 return ret;
130}
131
Timur Iskhakov458ca362017-09-12 23:16:03 -0700132void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) {
133 auto less = [&](const Type* lhs, const Type* rhs) {
134 return reversedOrder.at(lhs) < reversedOrder.at(rhs);
135 };
136
137 if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return;
138
139 mTypeOrderChanged = true;
140 std::sort(mTypes.begin(), mTypes.end(), less);
141
142 for (size_t i = 0; i != mTypes.size(); ++i) {
143 mTypeIndexByName.at(mTypes[i]->localName()) = i;
144 }
145}
146
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700147status_t Scope::forEachType(const std::function<status_t(Type *)> &func) const {
Andreas Huber881227d2016-08-02 14:20:21 -0700148 for (size_t i = 0; i < mTypes.size(); ++i) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800149 status_t err = func(mTypes[i]);
Andreas Huber881227d2016-08-02 14:20:21 -0700150
151 if (err != OK) {
152 return err;
153 }
154 }
155
156 return OK;
157}
158
Yifan Hong244e82d2016-11-11 11:13:57 -0800159status_t Scope::emitTypeDeclarations(Formatter &out) const {
Timur Iskhakov99072c32017-09-13 16:34:21 -0700160 if (mTypes.empty()) return OK;
161
162 out << "// Forward declaration for forward reference support:\n";
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700163 forEachType([&](Type* type) {
164 type->emitTypeForwardDeclaration(out);
165 return OK;
166 });
Timur Iskhakov99072c32017-09-13 16:34:21 -0700167 out << "\n";
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700168
Timur Iskhakov458ca362017-09-12 23:16:03 -0700169 if (mTypeOrderChanged) {
170 out << "// Order of inner types was changed for forward reference support.\n\n";
171 }
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700172 return forEachType([&](Type* type) {
Yifan Hong244e82d2016-11-11 11:13:57 -0800173 return type->emitTypeDeclarations(out);
174 });
175}
176
Andreas Hubere3f769a2016-10-10 10:54:44 -0700177status_t Scope::emitGlobalTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800178 return forEachType([&](Type *type) {
179 return type->emitGlobalTypeDeclarations(out);
180 });
181}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700182
Yifan Hong244e82d2016-11-11 11:13:57 -0800183status_t Scope::emitGlobalHwDeclarations(Formatter &out) const {
184 return forEachType([&](Type *type) {
185 return type->emitGlobalHwDeclarations(out);
186 });
Andreas Hubere3f769a2016-10-10 10:54:44 -0700187}
188
Andreas Huber85eabdb2016-08-25 11:24:49 -0700189status_t Scope::emitJavaTypeDeclarations(
190 Formatter &out, bool atTopLevel) const {
Timur Iskhakov458ca362017-09-12 23:16:03 -0700191 if (mTypeOrderChanged) {
192 out << "// Order of inner types was changed for forward reference support.\n\n";
193 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800194 return forEachType([&](Type *type) {
195 return type->emitJavaTypeDeclarations(out, atTopLevel);
196 });
Andreas Huber2831d512016-08-15 09:33:47 -0700197}
198
Chih-Hung Hsieh8c90cc52017-08-03 14:51:13 -0700199status_t Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800200 return forEachType([&](Type *type) {
201 return type->emitTypeDefinitions(out, prefix);
202 });
Andreas Huber881227d2016-08-02 14:20:21 -0700203}
204
Steven Morelandd537ab02016-09-12 10:32:01 -0700205const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700206 return mTypes;
207}
208
209status_t Scope::emitVtsTypeDeclarations(Formatter &out) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800210 return forEachType([&](Type *type) {
211 return type->emitVtsTypeDeclarations(out);
212 });
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700213}
214
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700215bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Andreas Huber70a59e12016-08-16 12:57:01 -0700216 for (const auto &type : mTypes) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700217 if (!type->isJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700218 return false;
219 }
220 }
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700221 return Type::deepIsJavaCompatible(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -0700222}
223
Andreas Huber019d21d2016-10-03 12:59:47 -0700224void Scope::appendToExportedTypesVector(
225 std::vector<const Type *> *exportedTypes) const {
Yifan Hong244e82d2016-11-11 11:13:57 -0800226 forEachType([&](Type *type) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700227 type->appendToExportedTypesVector(exportedTypes);
Yifan Hong244e82d2016-11-11 11:13:57 -0800228 return OK;
229 });
Andreas Huber019d21d2016-10-03 12:59:47 -0700230}
231
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700232////////////////////////////////////////
233
Timur Iskhakov565b0132017-09-06 18:07:11 -0700234RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
235 Scope* parent)
236 : Scope(localName, fullName, location, parent) {}
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700237RootScope::~RootScope() {}
238
239std::string RootScope::typeName() const {
240 return "(root scope)";
241}
242
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700243status_t RootScope::validate() const {
244 CHECK(annotations().empty());
245 return Scope::validate();
246}
247
248////////////////////////////////////////
249
Yifan Hongf24fa852016-09-23 11:03:15 -0700250LocalIdentifier::LocalIdentifier(){}
251LocalIdentifier::~LocalIdentifier(){}
252
253bool LocalIdentifier::isEnumValue() const {
254 return false;
255}
256
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700257const LocalIdentifier* LocalIdentifier::resolve() const {
258 return this;
259}
260
261LocalIdentifier* LocalIdentifier::resolve() {
262 return this;
263}
264
Timur Iskhakov7296af12017-08-09 21:52:48 +0000265ConstantExpression* LocalIdentifier::constExpr() const {
266 return nullptr;
267}
268
Andreas Huberc9410c72016-07-28 12:18:40 -0700269} // namespace android
270