blob: 588ba957ceb6656097f68464eadd1342e027adef [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>
Steven Moreland169a2d22018-01-25 10:05:31 -080025#include <hidl-util/StringHelper.h>
Timur Iskhakov458ca362017-09-12 23:16:03 -070026#include <algorithm>
Timur Iskhakov565b0132017-09-06 18:07:11 -070027#include <iostream>
Neel Mehta69920a62019-07-22 16:22:13 -070028#include <string>
Yifan Hong327cfe12016-10-03 10:29:42 -070029#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070030
Andreas Huberc9410c72016-07-28 12:18:40 -070031namespace android {
32
Neel Mehta69920a62019-07-22 16:22:13 -070033Scope::Scope(const std::string& localName, const FQName& fullName, const Location& location,
34 Scope* parent)
Timur Iskhakov565b0132017-09-06 18:07:11 -070035 : NamedType(localName, fullName, location, parent) {}
Yifan Hongf24fa852016-09-23 11:03:15 -070036Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070037
Timur Iskhakov565b0132017-09-06 18:07:11 -070038void Scope::addType(NamedType* type) {
Andreas Huberc9410c72016-07-28 12:18:40 -070039 size_t index = mTypes.size();
40 mTypes.push_back(type);
Neel Mehta9200af02019-07-19 13:24:57 -070041 mTypeIndexByName[type->definedName()] = index;
Timur Iskhakov565b0132017-09-06 18:07:11 -070042}
Andreas Huberc9410c72016-07-28 12:18:40 -070043
Timur Iskhakov565b0132017-09-06 18:07:11 -070044status_t Scope::validateUniqueNames() const {
45 for (const auto* type : mTypes) {
Neel Mehta9200af02019-07-19 13:24:57 -070046 if (mTypes[mTypeIndexByName.at(type->definedName())] != type) {
47 std::cerr << "ERROR: A type named '" << type->definedName()
Steven Morelandcbff5612017-10-11 17:01:54 -070048 << "' is already declared in the scope at " << type->location() << std::endl;
Timur Iskhakov565b0132017-09-06 18:07:11 -070049 return UNKNOWN_ERROR;
50 }
51 }
52 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -070053}
54
Yifan Hongae16eed2016-09-23 13:25:25 -070055NamedType *Scope::lookupType(const FQName &fqName) const {
Yifan Hong327cfe12016-10-03 10:29:42 -070056 CHECK(fqName.package().empty() && fqName.version().empty());
57 if (!fqName.valueName().empty()) {
Steven Morelandcbff5612017-10-11 17:01:54 -070058 std::cerr << "ERROR: " << fqName.string() << " does not refer to a type." << std::endl;
Yifan Hong327cfe12016-10-03 10:29:42 -070059 return nullptr;
60 }
61 std::vector<std::string> names = fqName.names();
62 CHECK_GT(names.size(), 0u);
63 auto it = mTypeIndexByName.find(names[0]);
Andreas Huberc9410c72016-07-28 12:18:40 -070064
Yifan Hong327cfe12016-10-03 10:29:42 -070065 if (it == mTypeIndexByName.end()) {
66 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -070067 }
68
Yifan Hong327cfe12016-10-03 10:29:42 -070069 NamedType *outerType = mTypes[it->second];
70 if (names.size() == 1) {
71 return outerType;
72 }
73 if (!outerType->isScope()) {
74 // more than one names, but the first name is not a scope
75 return nullptr;
76 }
77 Scope *outerScope = static_cast<Scope *>(outerType);
78 // *slowly* pop first element
79 names.erase(names.begin());
Steven Morelande1b157e2018-03-06 14:18:32 -080080 FQName innerName;
81 CHECK(FQName::parse(StringHelper::JoinStrings(names, "."), &innerName));
Yifan Hong327cfe12016-10-03 10:29:42 -070082 return outerScope->lookupType(innerName);
Andreas Huberc9410c72016-07-28 12:18:40 -070083}
84
Yifan Hongf24fa852016-09-23 11:03:15 -070085LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
Yi Kongd7f8ab32018-07-24 11:27:02 -070086 return nullptr;
Yifan Hongf24fa852016-09-23 11:03:15 -070087}
88
Andreas Huber5345ec22016-07-29 13:33:27 -070089bool Scope::isScope() const {
90 return true;
91}
92
Andreas Huber881227d2016-08-02 14:20:21 -070093Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070094 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070095 return static_cast<Interface *>(mTypes[0]);
96 }
97
Yi Kongd7f8ab32018-07-24 11:27:02 -070098 return nullptr;
Andreas Huber881227d2016-08-02 14:20:21 -070099}
100
Steven Morelandb47a2622018-07-11 09:04:25 -0700101bool Scope::definesInterfaces() const {
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700102 for (const NamedType *type : mTypes) {
103 if (type->isInterface()) {
104 return true;
105 }
106 }
107
108 return false;
109}
110
Timur Iskhakove9ccfa22017-08-14 15:07:03 -0700111const std::vector<Annotation*>& Scope::annotations() const {
112 return mAnnotations;
113}
114
115void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
116 CHECK(mAnnotations.empty());
117 CHECK(annotations != nullptr);
118 mAnnotations = *annotations;
119}
120
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700121std::vector<const Type*> Scope::getDefinedTypes() const {
122 std::vector<const Type*> ret;
123 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
Timur Iskhakov33431e62017-08-21 17:31:23 -0700124 return ret;
125}
126
Neel Mehta3b414a82019-07-02 15:47:48 -0700127std::vector<const NamedType*> Scope::getSortedDefinedTypes() const {
128 std::vector<const NamedType*> ret;
129 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
130
131 std::sort(ret.begin(), ret.end(), [](const NamedType* lhs, const NamedType* rhs) -> bool {
132 return lhs->location() < rhs->location();
133 });
134 return ret;
135}
136
Timur Iskhakov458ca362017-09-12 23:16:03 -0700137void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) {
138 auto less = [&](const Type* lhs, const Type* rhs) {
139 return reversedOrder.at(lhs) < reversedOrder.at(rhs);
140 };
141
142 if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return;
143
144 mTypeOrderChanged = true;
145 std::sort(mTypes.begin(), mTypes.end(), less);
146
147 for (size_t i = 0; i != mTypes.size(); ++i) {
Neel Mehta9200af02019-07-19 13:24:57 -0700148 mTypeIndexByName.at(mTypes[i]->definedName()) = i;
Timur Iskhakov458ca362017-09-12 23:16:03 -0700149 }
150}
151
Neel Mehta3b414a82019-07-02 15:47:48 -0700152void Scope::emitHidlDefinition(Formatter& out) const {
153 const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
154 out.join(definedTypes.begin(), definedTypes.end(), "\n",
155 [&](auto t) { t->emitHidlDefinition(out); });
156}
157
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800158void Scope::emitTypeDeclarations(Formatter& out) const {
159 if (mTypes.empty()) return;
Timur Iskhakov99072c32017-09-13 16:34:21 -0700160
161 out << "// Forward declaration for forward reference support:\n";
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800162 for (const Type* type : mTypes) {
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700163 type->emitTypeForwardDeclaration(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800164 }
Timur Iskhakov99072c32017-09-13 16:34:21 -0700165 out << "\n";
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700166
Timur Iskhakov458ca362017-09-12 23:16:03 -0700167 if (mTypeOrderChanged) {
168 out << "// Order of inner types was changed for forward reference support.\n\n";
169 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800170
171 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700172 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800173 type->emitTypeDeclarations(out);
174 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800175}
176
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800177void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800178 for (const Type* type : mTypes) {
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800179 type->emitGlobalTypeDeclarations(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800180 }
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800181}
182
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800183void Scope::emitPackageTypeDeclarations(Formatter& out) const {
184 for (const Type* type : mTypes) {
185 type->emitPackageTypeDeclarations(out);
186 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800187}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700188
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700189void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const {
190 for (const Type* type : mTypes) {
191 type->emitPackageTypeHeaderDefinitions(out);
192 }
193}
194
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800195void Scope::emitPackageHwDeclarations(Formatter& out) const {
196 for (const Type* type : mTypes) {
197 type->emitPackageHwDeclarations(out);
198 }
Andreas Hubere3f769a2016-10-10 10:54:44 -0700199}
200
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800201void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
Timur Iskhakov458ca362017-09-12 23:16:03 -0700202 if (mTypeOrderChanged) {
203 out << "// Order of inner types was changed for forward reference support.\n\n";
204 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800205
206 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700207 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800208 type->emitJavaTypeDeclarations(out, atTopLevel);
209 }
Andreas Huber2831d512016-08-15 09:33:47 -0700210}
211
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800212void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
213 for (const Type* type : mTypes) {
214 type->emitTypeDefinitions(out, prefix);
215 }
Andreas Huber881227d2016-08-02 14:20:21 -0700216}
217
Steven Morelandd537ab02016-09-12 10:32:01 -0700218const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700219 return mTypes;
220}
221
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800222void Scope::emitVtsTypeDeclarations(Formatter& out) const {
223 for (const Type* type : mTypes) {
224 type->emitVtsTypeDeclarations(out);
225 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700226}
227
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700228bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800229 for (const Type* type : mTypes) {
Steven Moreland8f8e8622019-11-04 12:38:38 -0800230 // Java compatibility focuses on types that are actually used by interfaces.
231 // Declarations of java-incompatible types are simply omitted from
232 // corresponding Java libraries.
233 if (type->isInterface() && !type->isJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700234 return false;
235 }
236 }
Steven Moreland8f8e8622019-11-04 12:38:38 -0800237
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700238 return Type::deepIsJavaCompatible(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -0700239}
240
Andreas Huber019d21d2016-10-03 12:59:47 -0700241void Scope::appendToExportedTypesVector(
242 std::vector<const Type *> *exportedTypes) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800243 for (const Type* type : mTypes) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700244 type->appendToExportedTypesVector(exportedTypes);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800245 }
Andreas Huber019d21d2016-10-03 12:59:47 -0700246}
247
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700248////////////////////////////////////////
249
Timur Iskhakov565b0132017-09-06 18:07:11 -0700250RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
251 Scope* parent)
252 : Scope(localName, fullName, location, parent) {}
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700253RootScope::~RootScope() {}
254
255std::string RootScope::typeName() const {
256 return "(root scope)";
257}
258
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700259status_t RootScope::validate() const {
260 CHECK(annotations().empty());
261 return Scope::validate();
262}
263
264////////////////////////////////////////
265
Yifan Hongf24fa852016-09-23 11:03:15 -0700266LocalIdentifier::LocalIdentifier(){}
267LocalIdentifier::~LocalIdentifier(){}
268
269bool LocalIdentifier::isEnumValue() const {
270 return false;
271}
272
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700273const LocalIdentifier* LocalIdentifier::resolve() const {
274 return this;
275}
276
277LocalIdentifier* LocalIdentifier::resolve() {
278 return this;
279}
280
Timur Iskhakov7296af12017-08-09 21:52:48 +0000281ConstantExpression* LocalIdentifier::constExpr() const {
282 return nullptr;
283}
284
Andreas Huberc9410c72016-07-28 12:18:40 -0700285} // namespace android
286