blob: cfe004d1311fab4ff07f149ec3afe820b6e4d50b [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>
Yifan Hong327cfe12016-10-03 10:29:42 -070028#include <vector>
Andreas Huber2831d512016-08-15 09:33:47 -070029
Andreas Huberc9410c72016-07-28 12:18:40 -070030namespace android {
31
Timur Iskhakov565b0132017-09-06 18:07:11 -070032Scope::Scope(const char* localName, const FQName& fullName, const Location& location, Scope* parent)
33 : NamedType(localName, fullName, location, parent) {}
Yifan Hongf24fa852016-09-23 11:03:15 -070034Scope::~Scope(){}
Andreas Huberc9410c72016-07-28 12:18:40 -070035
Timur Iskhakov565b0132017-09-06 18:07:11 -070036void Scope::addType(NamedType* type) {
Andreas Huberc9410c72016-07-28 12:18:40 -070037 size_t index = mTypes.size();
38 mTypes.push_back(type);
Timur Iskhakov565b0132017-09-06 18:07:11 -070039 mTypeIndexByName[type->localName()] = index;
40}
Andreas Huberc9410c72016-07-28 12:18:40 -070041
Timur Iskhakov565b0132017-09-06 18:07:11 -070042status_t Scope::validateUniqueNames() const {
43 for (const auto* type : mTypes) {
44 if (mTypes[mTypeIndexByName.at(type->localName())] != type) {
45 std::cerr << "ERROR: A type named '" << type->localName()
Steven Morelandcbff5612017-10-11 17:01:54 -070046 << "' is already declared in the scope at " << type->location() << std::endl;
Timur Iskhakov565b0132017-09-06 18:07:11 -070047 return UNKNOWN_ERROR;
48 }
49 }
50 return OK;
Andreas Huberc9410c72016-07-28 12:18:40 -070051}
52
Yifan Hongae16eed2016-09-23 13:25:25 -070053NamedType *Scope::lookupType(const FQName &fqName) const {
Yifan Hong327cfe12016-10-03 10:29:42 -070054 CHECK(fqName.package().empty() && fqName.version().empty());
55 if (!fqName.valueName().empty()) {
Steven Morelandcbff5612017-10-11 17:01:54 -070056 std::cerr << "ERROR: " << fqName.string() << " does not refer to a type." << std::endl;
Yifan Hong327cfe12016-10-03 10:29:42 -070057 return nullptr;
58 }
59 std::vector<std::string> names = fqName.names();
60 CHECK_GT(names.size(), 0u);
61 auto it = mTypeIndexByName.find(names[0]);
Andreas Huberc9410c72016-07-28 12:18:40 -070062
Yifan Hong327cfe12016-10-03 10:29:42 -070063 if (it == mTypeIndexByName.end()) {
64 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -070065 }
66
Yifan Hong327cfe12016-10-03 10:29:42 -070067 NamedType *outerType = mTypes[it->second];
68 if (names.size() == 1) {
69 return outerType;
70 }
71 if (!outerType->isScope()) {
72 // more than one names, but the first name is not a scope
73 return nullptr;
74 }
75 Scope *outerScope = static_cast<Scope *>(outerType);
76 // *slowly* pop first element
77 names.erase(names.begin());
Steven Morelande1b157e2018-03-06 14:18:32 -080078 FQName innerName;
79 CHECK(FQName::parse(StringHelper::JoinStrings(names, "."), &innerName));
Yifan Hong327cfe12016-10-03 10:29:42 -070080 return outerScope->lookupType(innerName);
Andreas Huberc9410c72016-07-28 12:18:40 -070081}
82
Yifan Hongf24fa852016-09-23 11:03:15 -070083LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
Yi Kongd7f8ab32018-07-24 11:27:02 -070084 return nullptr;
Yifan Hongf24fa852016-09-23 11:03:15 -070085}
86
Andreas Huber5345ec22016-07-29 13:33:27 -070087bool Scope::isScope() const {
88 return true;
89}
90
Andreas Huber881227d2016-08-02 14:20:21 -070091Interface *Scope::getInterface() const {
Andreas Hubera2723d22016-07-29 15:36:07 -070092 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
Andreas Huber881227d2016-08-02 14:20:21 -070093 return static_cast<Interface *>(mTypes[0]);
94 }
95
Yi Kongd7f8ab32018-07-24 11:27:02 -070096 return nullptr;
Andreas Huber881227d2016-08-02 14:20:21 -070097}
98
Steven Morelandb47a2622018-07-11 09:04:25 -070099bool Scope::definesInterfaces() const {
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700100 for (const NamedType *type : mTypes) {
101 if (type->isInterface()) {
102 return true;
103 }
104 }
105
106 return false;
107}
108
Timur Iskhakove9ccfa22017-08-14 15:07:03 -0700109const std::vector<Annotation*>& Scope::annotations() const {
110 return mAnnotations;
111}
112
113void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
114 CHECK(mAnnotations.empty());
115 CHECK(annotations != nullptr);
116 mAnnotations = *annotations;
117}
118
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700119std::vector<const Type*> Scope::getDefinedTypes() const {
120 std::vector<const Type*> ret;
121 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
Timur Iskhakov33431e62017-08-21 17:31:23 -0700122 return ret;
123}
124
Neel Mehta3b414a82019-07-02 15:47:48 -0700125std::vector<const NamedType*> Scope::getSortedDefinedTypes() const {
126 std::vector<const NamedType*> ret;
127 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
128
129 std::sort(ret.begin(), ret.end(), [](const NamedType* lhs, const NamedType* rhs) -> bool {
130 return lhs->location() < rhs->location();
131 });
132 return ret;
133}
134
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700135std::vector<const ConstantExpression*> Scope::getConstantExpressions() const {
136 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700137 for (const auto* annotation : mAnnotations) {
138 const auto& retAnnotation = annotation->getConstantExpressions();
139 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
140 }
141 return ret;
142}
143
Timur Iskhakov458ca362017-09-12 23:16:03 -0700144void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) {
145 auto less = [&](const Type* lhs, const Type* rhs) {
146 return reversedOrder.at(lhs) < reversedOrder.at(rhs);
147 };
148
149 if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return;
150
151 mTypeOrderChanged = true;
152 std::sort(mTypes.begin(), mTypes.end(), less);
153
154 for (size_t i = 0; i != mTypes.size(); ++i) {
155 mTypeIndexByName.at(mTypes[i]->localName()) = i;
156 }
157}
158
Neel Mehta3b414a82019-07-02 15:47:48 -0700159void Scope::emitHidlDefinition(Formatter& out) const {
160 const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
161 out.join(definedTypes.begin(), definedTypes.end(), "\n",
162 [&](auto t) { t->emitHidlDefinition(out); });
163}
164
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800165void Scope::emitTypeDeclarations(Formatter& out) const {
166 if (mTypes.empty()) return;
Timur Iskhakov99072c32017-09-13 16:34:21 -0700167
168 out << "// Forward declaration for forward reference support:\n";
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800169 for (const Type* type : mTypes) {
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700170 type->emitTypeForwardDeclaration(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800171 }
Timur Iskhakov99072c32017-09-13 16:34:21 -0700172 out << "\n";
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700173
Timur Iskhakov458ca362017-09-12 23:16:03 -0700174 if (mTypeOrderChanged) {
175 out << "// Order of inner types was changed for forward reference support.\n\n";
176 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800177
178 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700179 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800180 type->emitTypeDeclarations(out);
181 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800182}
183
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800184void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800185 for (const Type* type : mTypes) {
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800186 type->emitGlobalTypeDeclarations(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800187 }
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800188}
189
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800190void Scope::emitPackageTypeDeclarations(Formatter& out) const {
191 for (const Type* type : mTypes) {
192 type->emitPackageTypeDeclarations(out);
193 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800194}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700195
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700196void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const {
197 for (const Type* type : mTypes) {
198 type->emitPackageTypeHeaderDefinitions(out);
199 }
200}
201
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800202void Scope::emitPackageHwDeclarations(Formatter& out) const {
203 for (const Type* type : mTypes) {
204 type->emitPackageHwDeclarations(out);
205 }
Andreas Hubere3f769a2016-10-10 10:54:44 -0700206}
207
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800208void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
Timur Iskhakov458ca362017-09-12 23:16:03 -0700209 if (mTypeOrderChanged) {
210 out << "// Order of inner types was changed for forward reference support.\n\n";
211 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800212
213 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700214 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800215 type->emitJavaTypeDeclarations(out, atTopLevel);
216 }
Andreas Huber2831d512016-08-15 09:33:47 -0700217}
218
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800219void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
220 for (const Type* type : mTypes) {
221 type->emitTypeDefinitions(out, prefix);
222 }
Andreas Huber881227d2016-08-02 14:20:21 -0700223}
224
Steven Morelandd537ab02016-09-12 10:32:01 -0700225const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700226 return mTypes;
227}
228
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800229void Scope::emitVtsTypeDeclarations(Formatter& out) const {
230 for (const Type* type : mTypes) {
231 type->emitVtsTypeDeclarations(out);
232 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700233}
234
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700235bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800236 for (const Type* type : mTypes) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700237 if (!type->isJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700238 return false;
239 }
240 }
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700241 return Type::deepIsJavaCompatible(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -0700242}
243
Andreas Huber019d21d2016-10-03 12:59:47 -0700244void Scope::appendToExportedTypesVector(
245 std::vector<const Type *> *exportedTypes) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800246 for (const Type* type : mTypes) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700247 type->appendToExportedTypesVector(exportedTypes);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800248 }
Andreas Huber019d21d2016-10-03 12:59:47 -0700249}
250
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700251////////////////////////////////////////
252
Timur Iskhakov565b0132017-09-06 18:07:11 -0700253RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
254 Scope* parent)
255 : Scope(localName, fullName, location, parent) {}
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700256RootScope::~RootScope() {}
257
258std::string RootScope::typeName() const {
259 return "(root scope)";
260}
261
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700262status_t RootScope::validate() const {
263 CHECK(annotations().empty());
264 return Scope::validate();
265}
266
267////////////////////////////////////////
268
Yifan Hongf24fa852016-09-23 11:03:15 -0700269LocalIdentifier::LocalIdentifier(){}
270LocalIdentifier::~LocalIdentifier(){}
271
272bool LocalIdentifier::isEnumValue() const {
273 return false;
274}
275
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700276const LocalIdentifier* LocalIdentifier::resolve() const {
277 return this;
278}
279
280LocalIdentifier* LocalIdentifier::resolve() {
281 return this;
282}
283
Timur Iskhakov7296af12017-08-09 21:52:48 +0000284ConstantExpression* LocalIdentifier::constExpr() const {
285 return nullptr;
286}
287
Andreas Huberc9410c72016-07-28 12:18:40 -0700288} // namespace android
289