• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
29 #include <vector>
30 
31 namespace android {
32 
Scope(const std::string & localName,const FQName & fullName,const Location & location,Scope * parent)33 Scope::Scope(const std::string& localName, const FQName& fullName, const Location& location,
34              Scope* parent)
35     : NamedType(localName, fullName, location, parent) {}
~Scope()36 Scope::~Scope(){}
37 
addType(NamedType * type)38 void Scope::addType(NamedType* type) {
39     size_t index = mTypes.size();
40     mTypes.push_back(type);
41     mTypeIndexByName[type->definedName()] = index;
42 }
43 
validate() const44 status_t Scope::validate() const {
45     status_t status = validateAnnotations();
46     if (status != OK) return status;
47 
48     return NamedType::validate();
49 }
50 
validateUniqueNames() const51 status_t Scope::validateUniqueNames() const {
52     for (const auto* type : mTypes) {
53         if (mTypes[mTypeIndexByName.at(type->definedName())] != type) {
54             std::cerr << "ERROR: A type named '" << type->definedName()
55                       << "' is already declared in the scope at " << type->location() << std::endl;
56             return UNKNOWN_ERROR;
57         }
58     }
59     return OK;
60 }
61 
validateAnnotations() const62 status_t Scope::validateAnnotations() const {
63     for (const Annotation* annotation : annotations()) {
64         std::cerr << "WARNING: Unrecognized annotation '" << annotation->name() << "' at "
65                   << location() << ". No annotations are supported here." << std::endl;
66         // This is a warning to avoid breaking downstream unnecessarily.
67     }
68     return OK;
69 }
70 
lookupType(const FQName & fqName) const71 NamedType *Scope::lookupType(const FQName &fqName) const {
72     CHECK(fqName.package().empty() && fqName.version().empty());
73     if (!fqName.valueName().empty()) {
74         std::cerr << "ERROR: " << fqName.string() << " does not refer to a type." << std::endl;
75         return nullptr;
76     }
77     std::vector<std::string> names = fqName.names();
78     CHECK_GT(names.size(), 0u);
79     auto it = mTypeIndexByName.find(names[0]);
80 
81     if (it == mTypeIndexByName.end()) {
82         return nullptr;
83     }
84 
85     NamedType *outerType = mTypes[it->second];
86     if (names.size() == 1) {
87         return outerType;
88     }
89     if (!outerType->isScope()) {
90         // more than one names, but the first name is not a scope
91         return nullptr;
92     }
93     Scope *outerScope = static_cast<Scope *>(outerType);
94     // *slowly* pop first element
95     names.erase(names.begin());
96     FQName innerName;
97     CHECK(FQName::parse(StringHelper::JoinStrings(names, "."), &innerName));
98     return outerScope->lookupType(innerName);
99 }
100 
lookupIdentifier(const std::string &) const101 LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
102     return nullptr;
103 }
104 
isScope() const105 bool Scope::isScope() const {
106     return true;
107 }
108 
getInterface() const109 Interface *Scope::getInterface() const {
110     if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
111         return static_cast<Interface *>(mTypes[0]);
112     }
113 
114     return nullptr;
115 }
116 
definesInterfaces() const117 bool Scope::definesInterfaces() const {
118     for (const NamedType *type : mTypes) {
119         if (type->isInterface()) {
120             return true;
121         }
122     }
123 
124     return false;
125 }
126 
annotations() const127 const std::vector<Annotation*>& Scope::annotations() const {
128     return mAnnotations;
129 }
130 
setAnnotations(std::vector<Annotation * > * annotations)131 void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
132     CHECK(mAnnotations.empty());
133     CHECK(annotations != nullptr);
134     mAnnotations = *annotations;
135 }
136 
getDefinedTypes() const137 std::vector<const Type*> Scope::getDefinedTypes() const {
138     std::vector<const Type*> ret;
139     ret.insert(ret.end(), mTypes.begin(), mTypes.end());
140     return ret;
141 }
142 
getSortedDefinedTypes() const143 std::vector<const NamedType*> Scope::getSortedDefinedTypes() const {
144     std::vector<const NamedType*> ret;
145     ret.insert(ret.end(), mTypes.begin(), mTypes.end());
146 
147     std::sort(ret.begin(), ret.end(), [](const NamedType* lhs, const NamedType* rhs) -> bool {
148         return lhs->location() < rhs->location();
149     });
150     return ret;
151 }
152 
topologicalReorder(const std::unordered_map<const Type *,size_t> & reversedOrder)153 void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) {
154     auto less = [&](const Type* lhs, const Type* rhs) {
155         return reversedOrder.at(lhs) < reversedOrder.at(rhs);
156     };
157 
158     if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return;
159 
160     mTypeOrderChanged = true;
161     std::sort(mTypes.begin(), mTypes.end(), less);
162 
163     for (size_t i = 0; i != mTypes.size(); ++i) {
164         mTypeIndexByName.at(mTypes[i]->definedName()) = i;
165     }
166 }
167 
emitHidlDefinition(Formatter & out) const168 void Scope::emitHidlDefinition(Formatter& out) const {
169     const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
170     out.join(definedTypes.begin(), definedTypes.end(), "\n",
171              [&](auto t) { t->emitHidlDefinition(out); });
172 }
173 
emitTypeDeclarations(Formatter & out) const174 void Scope::emitTypeDeclarations(Formatter& out) const {
175     if (mTypes.empty()) return;
176 
177     out << "// Forward declaration for forward reference support:\n";
178     for (const Type* type : mTypes) {
179         type->emitTypeForwardDeclaration(out);
180     }
181     out << "\n";
182 
183     if (mTypeOrderChanged) {
184         out << "// Order of inner types was changed for forward reference support.\n\n";
185     }
186 
187     for (const Type* type : mTypes) {
188         type->emitDocComment(out);
189         type->emitTypeDeclarations(out);
190     }
191 }
192 
emitGlobalTypeDeclarations(Formatter & out) const193 void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
194     for (const Type* type : mTypes) {
195         type->emitGlobalTypeDeclarations(out);
196     }
197 }
198 
emitPackageTypeDeclarations(Formatter & out) const199 void Scope::emitPackageTypeDeclarations(Formatter& out) const {
200     for (const Type* type : mTypes) {
201         type->emitPackageTypeDeclarations(out);
202     }
203 }
204 
emitPackageTypeHeaderDefinitions(Formatter & out) const205 void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const {
206     for (const Type* type : mTypes) {
207         type->emitPackageTypeHeaderDefinitions(out);
208     }
209 }
210 
emitPackageHwDeclarations(Formatter & out) const211 void Scope::emitPackageHwDeclarations(Formatter& out) const {
212     for (const Type* type : mTypes) {
213         type->emitPackageHwDeclarations(out);
214     }
215 }
216 
emitJavaTypeDeclarations(Formatter & out,bool atTopLevel) const217 void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
218     if (mTypeOrderChanged) {
219         out << "// Order of inner types was changed for forward reference support.\n\n";
220     }
221 
222     for (const Type* type : mTypes) {
223         type->emitDocComment(out);
224         type->emitJavaTypeDeclarations(out, atTopLevel);
225     }
226 }
227 
emitTypeDefinitions(Formatter & out,const std::string & prefix) const228 void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
229     for (const Type* type : mTypes) {
230         type->emitTypeDefinitions(out, prefix);
231     }
232 }
233 
getSubTypes() const234 const std::vector<NamedType *> &Scope::getSubTypes() const {
235     return mTypes;
236 }
237 
emitVtsTypeDeclarations(Formatter & out) const238 void Scope::emitVtsTypeDeclarations(Formatter& out) const {
239     for (const Type* type : mTypes) {
240         type->emitVtsTypeDeclarations(out);
241     }
242 }
243 
deepIsJavaCompatible(std::unordered_set<const Type * > * visited) const244 bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
245     for (const Type* type : mTypes) {
246         // Java compatibility focuses on types that are actually used by interfaces.
247         // Declarations of java-incompatible types are simply omitted from
248         // corresponding Java libraries.
249         if (type->isInterface() && !type->isJavaCompatible(visited)) {
250             return false;
251         }
252     }
253 
254     return Type::deepIsJavaCompatible(visited);
255 }
256 
appendToExportedTypesVector(std::vector<const Type * > * exportedTypes) const257 void Scope::appendToExportedTypesVector(
258         std::vector<const Type *> *exportedTypes) const {
259     for (const Type* type : mTypes) {
260         type->appendToExportedTypesVector(exportedTypes);
261     }
262 }
263 
264 ////////////////////////////////////////
265 
RootScope(const char * localName,const FQName & fullName,const Location & location,Scope * parent)266 RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
267                      Scope* parent)
268     : Scope(localName, fullName, location, parent) {}
~RootScope()269 RootScope::~RootScope() {}
270 
typeName() const271 std::string RootScope::typeName() const {
272     return "(root scope)";
273 }
274 
validate() const275 status_t RootScope::validate() const {
276     CHECK(annotations().empty());
277     return Scope::validate();
278 }
279 
280 ////////////////////////////////////////
281 
LocalIdentifier()282 LocalIdentifier::LocalIdentifier(){}
~LocalIdentifier()283 LocalIdentifier::~LocalIdentifier(){}
284 
isEnumValue() const285 bool LocalIdentifier::isEnumValue() const {
286     return false;
287 }
288 
resolve() const289 const LocalIdentifier* LocalIdentifier::resolve() const {
290     return this;
291 }
292 
resolve()293 LocalIdentifier* LocalIdentifier::resolve() {
294     return this;
295 }
296 
constExpr() const297 ConstantExpression* LocalIdentifier::constExpr() const {
298     return nullptr;
299 }
300 
301 }  // namespace android
302 
303