blob: c8f57b83cfe4bb5ce69918ab19979a74eca1e87f [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);
Timur Iskhakov565b0132017-09-06 18:07:11 -070041 mTypeIndexByName[type->localName()] = index;
42}
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) {
46 if (mTypes[mTypeIndexByName.at(type->localName())] != type) {
47 std::cerr << "ERROR: A type named '" << type->localName()
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 Iskhakovb58f4182017-08-29 15:19:24 -0700137std::vector<const ConstantExpression*> Scope::getConstantExpressions() const {
138 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700139 for (const auto* annotation : mAnnotations) {
140 const auto& retAnnotation = annotation->getConstantExpressions();
141 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
142 }
143 return ret;
144}
145
Timur Iskhakov458ca362017-09-12 23:16:03 -0700146void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) {
147 auto less = [&](const Type* lhs, const Type* rhs) {
148 return reversedOrder.at(lhs) < reversedOrder.at(rhs);
149 };
150
151 if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return;
152
153 mTypeOrderChanged = true;
154 std::sort(mTypes.begin(), mTypes.end(), less);
155
156 for (size_t i = 0; i != mTypes.size(); ++i) {
157 mTypeIndexByName.at(mTypes[i]->localName()) = i;
158 }
159}
160
Neel Mehta3b414a82019-07-02 15:47:48 -0700161void Scope::emitHidlDefinition(Formatter& out) const {
162 const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
163 out.join(definedTypes.begin(), definedTypes.end(), "\n",
164 [&](auto t) { t->emitHidlDefinition(out); });
165}
166
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800167void Scope::emitTypeDeclarations(Formatter& out) const {
168 if (mTypes.empty()) return;
Timur Iskhakov99072c32017-09-13 16:34:21 -0700169
170 out << "// Forward declaration for forward reference support:\n";
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800171 for (const Type* type : mTypes) {
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700172 type->emitTypeForwardDeclaration(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800173 }
Timur Iskhakov99072c32017-09-13 16:34:21 -0700174 out << "\n";
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700175
Timur Iskhakov458ca362017-09-12 23:16:03 -0700176 if (mTypeOrderChanged) {
177 out << "// Order of inner types was changed for forward reference support.\n\n";
178 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800179
180 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700181 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800182 type->emitTypeDeclarations(out);
183 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800184}
185
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800186void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800187 for (const Type* type : mTypes) {
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800188 type->emitGlobalTypeDeclarations(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800189 }
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800190}
191
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800192void Scope::emitPackageTypeDeclarations(Formatter& out) const {
193 for (const Type* type : mTypes) {
194 type->emitPackageTypeDeclarations(out);
195 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800196}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700197
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700198void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const {
199 for (const Type* type : mTypes) {
200 type->emitPackageTypeHeaderDefinitions(out);
201 }
202}
203
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800204void Scope::emitPackageHwDeclarations(Formatter& out) const {
205 for (const Type* type : mTypes) {
206 type->emitPackageHwDeclarations(out);
207 }
Andreas Hubere3f769a2016-10-10 10:54:44 -0700208}
209
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800210void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
Timur Iskhakov458ca362017-09-12 23:16:03 -0700211 if (mTypeOrderChanged) {
212 out << "// Order of inner types was changed for forward reference support.\n\n";
213 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800214
215 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700216 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800217 type->emitJavaTypeDeclarations(out, atTopLevel);
218 }
Andreas Huber2831d512016-08-15 09:33:47 -0700219}
220
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800221void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
222 for (const Type* type : mTypes) {
223 type->emitTypeDefinitions(out, prefix);
224 }
Andreas Huber881227d2016-08-02 14:20:21 -0700225}
226
Steven Morelandd537ab02016-09-12 10:32:01 -0700227const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700228 return mTypes;
229}
230
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800231void Scope::emitVtsTypeDeclarations(Formatter& out) const {
232 for (const Type* type : mTypes) {
233 type->emitVtsTypeDeclarations(out);
234 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700235}
236
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700237bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800238 for (const Type* type : mTypes) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700239 if (!type->isJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700240 return false;
241 }
242 }
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700243 return Type::deepIsJavaCompatible(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -0700244}
245
Andreas Huber019d21d2016-10-03 12:59:47 -0700246void Scope::appendToExportedTypesVector(
247 std::vector<const Type *> *exportedTypes) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800248 for (const Type* type : mTypes) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700249 type->appendToExportedTypesVector(exportedTypes);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800250 }
Andreas Huber019d21d2016-10-03 12:59:47 -0700251}
252
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700253////////////////////////////////////////
254
Timur Iskhakov565b0132017-09-06 18:07:11 -0700255RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
256 Scope* parent)
257 : Scope(localName, fullName, location, parent) {}
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700258RootScope::~RootScope() {}
259
260std::string RootScope::typeName() const {
261 return "(root scope)";
262}
263
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700264status_t RootScope::validate() const {
265 CHECK(annotations().empty());
266 return Scope::validate();
267}
268
269////////////////////////////////////////
270
Yifan Hongf24fa852016-09-23 11:03:15 -0700271LocalIdentifier::LocalIdentifier(){}
272LocalIdentifier::~LocalIdentifier(){}
273
274bool LocalIdentifier::isEnumValue() const {
275 return false;
276}
277
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700278const LocalIdentifier* LocalIdentifier::resolve() const {
279 return this;
280}
281
282LocalIdentifier* LocalIdentifier::resolve() {
283 return this;
284}
285
Timur Iskhakov7296af12017-08-09 21:52:48 +0000286ConstantExpression* LocalIdentifier::constExpr() const {
287 return nullptr;
288}
289
Andreas Huberc9410c72016-07-28 12:18:40 -0700290} // namespace android
291