blob: d4953aa56ae7087b1fa1343f69ddfa67575c4cfb [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
Timur Iskhakovb58f4182017-08-29 15:19:24 -0700125std::vector<const ConstantExpression*> Scope::getConstantExpressions() const {
126 std::vector<const ConstantExpression*> ret;
Timur Iskhakov891a8662017-08-25 21:53:48 -0700127 for (const auto* annotation : mAnnotations) {
128 const auto& retAnnotation = annotation->getConstantExpressions();
129 ret.insert(ret.end(), retAnnotation.begin(), retAnnotation.end());
130 }
131 return ret;
132}
133
Timur Iskhakov458ca362017-09-12 23:16:03 -0700134void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) {
135 auto less = [&](const Type* lhs, const Type* rhs) {
136 return reversedOrder.at(lhs) < reversedOrder.at(rhs);
137 };
138
139 if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return;
140
141 mTypeOrderChanged = true;
142 std::sort(mTypes.begin(), mTypes.end(), less);
143
144 for (size_t i = 0; i != mTypes.size(); ++i) {
145 mTypeIndexByName.at(mTypes[i]->localName()) = i;
146 }
147}
148
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800149void Scope::emitTypeDeclarations(Formatter& out) const {
150 if (mTypes.empty()) return;
Timur Iskhakov99072c32017-09-13 16:34:21 -0700151
152 out << "// Forward declaration for forward reference support:\n";
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800153 for (const Type* type : mTypes) {
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700154 type->emitTypeForwardDeclaration(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800155 }
Timur Iskhakov99072c32017-09-13 16:34:21 -0700156 out << "\n";
Timur Iskhakovfd3f2502017-09-05 16:25:02 -0700157
Timur Iskhakov458ca362017-09-12 23:16:03 -0700158 if (mTypeOrderChanged) {
159 out << "// Order of inner types was changed for forward reference support.\n\n";
160 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800161
162 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700163 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800164 type->emitTypeDeclarations(out);
165 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800166}
167
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800168void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800169 for (const Type* type : mTypes) {
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800170 type->emitGlobalTypeDeclarations(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800171 }
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800172}
173
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800174void Scope::emitPackageTypeDeclarations(Formatter& out) const {
175 for (const Type* type : mTypes) {
176 type->emitPackageTypeDeclarations(out);
177 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800178}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700179
Steven Moreland09c6ebe2018-10-09 10:15:48 -0700180void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const {
181 for (const Type* type : mTypes) {
182 type->emitPackageTypeHeaderDefinitions(out);
183 }
184}
185
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800186void Scope::emitPackageHwDeclarations(Formatter& out) const {
187 for (const Type* type : mTypes) {
188 type->emitPackageHwDeclarations(out);
189 }
Andreas Hubere3f769a2016-10-10 10:54:44 -0700190}
191
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800192void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
Timur Iskhakov458ca362017-09-12 23:16:03 -0700193 if (mTypeOrderChanged) {
194 out << "// Order of inner types was changed for forward reference support.\n\n";
195 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800196
197 for (const Type* type : mTypes) {
Steven Moreland073269e2018-05-17 15:45:26 -0700198 type->emitDocComment(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800199 type->emitJavaTypeDeclarations(out, atTopLevel);
200 }
Andreas Huber2831d512016-08-15 09:33:47 -0700201}
202
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800203void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
204 for (const Type* type : mTypes) {
205 type->emitTypeDefinitions(out, prefix);
206 }
Andreas Huber881227d2016-08-02 14:20:21 -0700207}
208
Steven Morelandd537ab02016-09-12 10:32:01 -0700209const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700210 return mTypes;
211}
212
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800213void Scope::emitVtsTypeDeclarations(Formatter& out) const {
214 for (const Type* type : mTypes) {
215 type->emitVtsTypeDeclarations(out);
216 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700217}
218
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700219bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800220 for (const Type* type : mTypes) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700221 if (!type->isJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700222 return false;
223 }
224 }
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700225 return Type::deepIsJavaCompatible(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -0700226}
227
Andreas Huber019d21d2016-10-03 12:59:47 -0700228void Scope::appendToExportedTypesVector(
229 std::vector<const Type *> *exportedTypes) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800230 for (const Type* type : mTypes) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700231 type->appendToExportedTypesVector(exportedTypes);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800232 }
Andreas Huber019d21d2016-10-03 12:59:47 -0700233}
234
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700235////////////////////////////////////////
236
Timur Iskhakov565b0132017-09-06 18:07:11 -0700237RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
238 Scope* parent)
239 : Scope(localName, fullName, location, parent) {}
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700240RootScope::~RootScope() {}
241
242std::string RootScope::typeName() const {
243 return "(root scope)";
244}
245
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700246status_t RootScope::validate() const {
247 CHECK(annotations().empty());
248 return Scope::validate();
249}
250
251////////////////////////////////////////
252
Yifan Hongf24fa852016-09-23 11:03:15 -0700253LocalIdentifier::LocalIdentifier(){}
254LocalIdentifier::~LocalIdentifier(){}
255
256bool LocalIdentifier::isEnumValue() const {
257 return false;
258}
259
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700260const LocalIdentifier* LocalIdentifier::resolve() const {
261 return this;
262}
263
264LocalIdentifier* LocalIdentifier::resolve() {
265 return this;
266}
267
Timur Iskhakov7296af12017-08-09 21:52:48 +0000268ConstantExpression* LocalIdentifier::constExpr() const {
269 return nullptr;
270}
271
Andreas Huberc9410c72016-07-28 12:18:40 -0700272} // namespace android
273