1 /*
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
17 #include "Scope.h"
18
19 #include "Annotation.h"
20 #include "ConstantExpression.h"
21 #include "Interface.h"
22
23 #include <android-base/logging.h>
24 #include <hidl-util/Formatter.h>
25 #include <hidl-util/StringHelper.h>
26 #include <algorithm>
27 #include <iostream>
28 #include <vector>
29
30 namespace android {
31
Scope(const char * localName,const FQName & fullName,const Location & location,Scope * parent)32 Scope::Scope(const char* localName, const FQName& fullName, const Location& location, Scope* parent)
33 : NamedType(localName, fullName, location, parent) {}
~Scope()34 Scope::~Scope(){}
35
addType(NamedType * type)36 void Scope::addType(NamedType* type) {
37 size_t index = mTypes.size();
38 mTypes.push_back(type);
39 mTypeIndexByName[type->localName()] = index;
40 }
41
validateUniqueNames() const42 status_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()
46 << "' is already declared in the scope at " << type->location() << std::endl;
47 return UNKNOWN_ERROR;
48 }
49 }
50 return OK;
51 }
52
lookupType(const FQName & fqName) const53 NamedType *Scope::lookupType(const FQName &fqName) const {
54 CHECK(fqName.package().empty() && fqName.version().empty());
55 if (!fqName.valueName().empty()) {
56 std::cerr << "ERROR: " << fqName.string() << " does not refer to a type." << std::endl;
57 return nullptr;
58 }
59 std::vector<std::string> names = fqName.names();
60 CHECK_GT(names.size(), 0u);
61 auto it = mTypeIndexByName.find(names[0]);
62
63 if (it == mTypeIndexByName.end()) {
64 return nullptr;
65 }
66
67 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());
78 FQName innerName;
79 CHECK(FQName::parse(StringHelper::JoinStrings(names, "."), &innerName));
80 return outerScope->lookupType(innerName);
81 }
82
lookupIdentifier(const std::string &) const83 LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
84 return nullptr;
85 }
86
isScope() const87 bool Scope::isScope() const {
88 return true;
89 }
90
getInterface() const91 Interface *Scope::getInterface() const {
92 if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
93 return static_cast<Interface *>(mTypes[0]);
94 }
95
96 return nullptr;
97 }
98
definesInterfaces() const99 bool Scope::definesInterfaces() const {
100 for (const NamedType *type : mTypes) {
101 if (type->isInterface()) {
102 return true;
103 }
104 }
105
106 return false;
107 }
108
annotations() const109 const std::vector<Annotation*>& Scope::annotations() const {
110 return mAnnotations;
111 }
112
setAnnotations(std::vector<Annotation * > * annotations)113 void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
114 CHECK(mAnnotations.empty());
115 CHECK(annotations != nullptr);
116 mAnnotations = *annotations;
117 }
118
getDefinedTypes() const119 std::vector<const Type*> Scope::getDefinedTypes() const {
120 std::vector<const Type*> ret;
121 ret.insert(ret.end(), mTypes.begin(), mTypes.end());
122 return ret;
123 }
124
getConstantExpressions() const125 std::vector<const ConstantExpression*> Scope::getConstantExpressions() const {
126 std::vector<const ConstantExpression*> ret;
127 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
topologicalReorder(const std::unordered_map<const Type *,size_t> & reversedOrder)134 void 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
emitTypeDeclarations(Formatter & out) const149 void Scope::emitTypeDeclarations(Formatter& out) const {
150 if (mTypes.empty()) return;
151
152 out << "// Forward declaration for forward reference support:\n";
153 for (const Type* type : mTypes) {
154 type->emitTypeForwardDeclaration(out);
155 }
156 out << "\n";
157
158 if (mTypeOrderChanged) {
159 out << "// Order of inner types was changed for forward reference support.\n\n";
160 }
161
162 for (const Type* type : mTypes) {
163 type->emitDocComment(out);
164 type->emitTypeDeclarations(out);
165 }
166 }
167
emitGlobalTypeDeclarations(Formatter & out) const168 void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
169 for (const Type* type : mTypes) {
170 type->emitGlobalTypeDeclarations(out);
171 }
172 }
173
emitPackageTypeDeclarations(Formatter & out) const174 void Scope::emitPackageTypeDeclarations(Formatter& out) const {
175 for (const Type* type : mTypes) {
176 type->emitPackageTypeDeclarations(out);
177 }
178 }
179
emitPackageTypeHeaderDefinitions(Formatter & out) const180 void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const {
181 for (const Type* type : mTypes) {
182 type->emitPackageTypeHeaderDefinitions(out);
183 }
184 }
185
emitPackageHwDeclarations(Formatter & out) const186 void Scope::emitPackageHwDeclarations(Formatter& out) const {
187 for (const Type* type : mTypes) {
188 type->emitPackageHwDeclarations(out);
189 }
190 }
191
emitJavaTypeDeclarations(Formatter & out,bool atTopLevel) const192 void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
193 if (mTypeOrderChanged) {
194 out << "// Order of inner types was changed for forward reference support.\n\n";
195 }
196
197 for (const Type* type : mTypes) {
198 type->emitDocComment(out);
199 type->emitJavaTypeDeclarations(out, atTopLevel);
200 }
201 }
202
emitTypeDefinitions(Formatter & out,const std::string & prefix) const203 void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
204 for (const Type* type : mTypes) {
205 type->emitTypeDefinitions(out, prefix);
206 }
207 }
208
getSubTypes() const209 const std::vector<NamedType *> &Scope::getSubTypes() const {
210 return mTypes;
211 }
212
emitVtsTypeDeclarations(Formatter & out) const213 void Scope::emitVtsTypeDeclarations(Formatter& out) const {
214 for (const Type* type : mTypes) {
215 type->emitVtsTypeDeclarations(out);
216 }
217 }
218
deepIsJavaCompatible(std::unordered_set<const Type * > * visited) const219 bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
220 for (const Type* type : mTypes) {
221 if (!type->isJavaCompatible(visited)) {
222 return false;
223 }
224 }
225 return Type::deepIsJavaCompatible(visited);
226 }
227
appendToExportedTypesVector(std::vector<const Type * > * exportedTypes) const228 void Scope::appendToExportedTypesVector(
229 std::vector<const Type *> *exportedTypes) const {
230 for (const Type* type : mTypes) {
231 type->appendToExportedTypesVector(exportedTypes);
232 }
233 }
234
235 ////////////////////////////////////////
236
RootScope(const char * localName,const FQName & fullName,const Location & location,Scope * parent)237 RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
238 Scope* parent)
239 : Scope(localName, fullName, location, parent) {}
~RootScope()240 RootScope::~RootScope() {}
241
typeName() const242 std::string RootScope::typeName() const {
243 return "(root scope)";
244 }
245
validate() const246 status_t RootScope::validate() const {
247 CHECK(annotations().empty());
248 return Scope::validate();
249 }
250
251 ////////////////////////////////////////
252
LocalIdentifier()253 LocalIdentifier::LocalIdentifier(){}
~LocalIdentifier()254 LocalIdentifier::~LocalIdentifier(){}
255
isEnumValue() const256 bool LocalIdentifier::isEnumValue() const {
257 return false;
258 }
259
resolve() const260 const LocalIdentifier* LocalIdentifier::resolve() const {
261 return this;
262 }
263
resolve()264 LocalIdentifier* LocalIdentifier::resolve() {
265 return this;
266 }
267
constExpr() const268 ConstantExpression* LocalIdentifier::constExpr() const {
269 return nullptr;
270 }
271
272 } // namespace android
273
274