blob: f2cd80c901cf709f3b107136864ec83545f44a61 [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 {
84 return NULL;
85}
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
96 return NULL;
97}
98
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070099bool Scope::containsInterfaces() const {
100 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) {
163 type->emitTypeDeclarations(out);
164 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800165}
166
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800167void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800168 for (const Type* type : mTypes) {
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800169 type->emitGlobalTypeDeclarations(out);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800170 }
Steven Moreland8e61c5a2017-11-17 15:55:28 -0800171}
172
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800173void Scope::emitPackageTypeDeclarations(Formatter& out) const {
174 for (const Type* type : mTypes) {
175 type->emitPackageTypeDeclarations(out);
176 }
Yifan Hong244e82d2016-11-11 11:13:57 -0800177}
Andreas Hubere3f769a2016-10-10 10:54:44 -0700178
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800179void Scope::emitPackageHwDeclarations(Formatter& out) const {
180 for (const Type* type : mTypes) {
181 type->emitPackageHwDeclarations(out);
182 }
Andreas Hubere3f769a2016-10-10 10:54:44 -0700183}
184
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800185void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
Timur Iskhakov458ca362017-09-12 23:16:03 -0700186 if (mTypeOrderChanged) {
187 out << "// Order of inner types was changed for forward reference support.\n\n";
188 }
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800189
190 for (const Type* type : mTypes) {
191 type->emitJavaTypeDeclarations(out, atTopLevel);
192 }
Andreas Huber2831d512016-08-15 09:33:47 -0700193}
194
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800195void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
196 for (const Type* type : mTypes) {
197 type->emitTypeDefinitions(out, prefix);
198 }
Andreas Huber881227d2016-08-02 14:20:21 -0700199}
200
Steven Morelandd537ab02016-09-12 10:32:01 -0700201const std::vector<NamedType *> &Scope::getSubTypes() const {
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700202 return mTypes;
203}
204
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800205void Scope::emitVtsTypeDeclarations(Formatter& out) const {
206 for (const Type* type : mTypes) {
207 type->emitVtsTypeDeclarations(out);
208 }
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700209}
210
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700211bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800212 for (const Type* type : mTypes) {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700213 if (!type->isJavaCompatible(visited)) {
Andreas Huber70a59e12016-08-16 12:57:01 -0700214 return false;
215 }
216 }
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700217 return Type::deepIsJavaCompatible(visited);
Andreas Huber60d3b222017-03-30 09:10:56 -0700218}
219
Andreas Huber019d21d2016-10-03 12:59:47 -0700220void Scope::appendToExportedTypesVector(
221 std::vector<const Type *> *exportedTypes) const {
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800222 for (const Type* type : mTypes) {
Andreas Huber019d21d2016-10-03 12:59:47 -0700223 type->appendToExportedTypesVector(exportedTypes);
Steven Moreland6ec9eb92018-02-16 14:21:49 -0800224 }
Andreas Huber019d21d2016-10-03 12:59:47 -0700225}
226
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700227////////////////////////////////////////
228
Timur Iskhakov565b0132017-09-06 18:07:11 -0700229RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
230 Scope* parent)
231 : Scope(localName, fullName, location, parent) {}
Steven Moreland0ecc7b82017-07-19 12:59:23 -0700232RootScope::~RootScope() {}
233
234std::string RootScope::typeName() const {
235 return "(root scope)";
236}
237
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700238status_t RootScope::validate() const {
239 CHECK(annotations().empty());
240 return Scope::validate();
241}
242
243////////////////////////////////////////
244
Yifan Hongf24fa852016-09-23 11:03:15 -0700245LocalIdentifier::LocalIdentifier(){}
246LocalIdentifier::~LocalIdentifier(){}
247
248bool LocalIdentifier::isEnumValue() const {
249 return false;
250}
251
Timur Iskhakovdbaed332017-08-31 16:33:41 -0700252const LocalIdentifier* LocalIdentifier::resolve() const {
253 return this;
254}
255
256LocalIdentifier* LocalIdentifier::resolve() {
257 return this;
258}
259
Timur Iskhakov7296af12017-08-09 21:52:48 +0000260ConstantExpression* LocalIdentifier::constExpr() const {
261 return nullptr;
262}
263
Andreas Huberc9410c72016-07-28 12:18:40 -0700264} // namespace android
265