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 
validateUniqueNames() const44 status_t Scope::validateUniqueNames() const {
45     for (const auto* type : mTypes) {
46         if (mTypes[mTypeIndexByName.at(type->definedName())] != type) {
47             std::cerr << "ERROR: A type named '" << type->definedName()
48                       << "' is already declared in the scope at " << type->location() << std::endl;
49             return UNKNOWN_ERROR;
50         }
51     }
52     return OK;
53 }
54 
lookupType(const FQName & fqName) const55 NamedType *Scope::lookupType(const FQName &fqName) const {
56     CHECK(fqName.package().empty() && fqName.version().empty());
57     if (!fqName.valueName().empty()) {
58         std::cerr << "ERROR: " << fqName.string() << " does not refer to a type." << std::endl;
59         return nullptr;
60     }
61     std::vector<std::string> names = fqName.names();
62     CHECK_GT(names.size(), 0u);
63     auto it = mTypeIndexByName.find(names[0]);
64 
65     if (it == mTypeIndexByName.end()) {
66         return nullptr;
67     }
68 
69     NamedType *outerType = mTypes[it->second];
70     if (names.size() == 1) {
71         return outerType;
72     }
73     if (!outerType->isScope()) {
74         // more than one names, but the first name is not a scope
75         return nullptr;
76     }
77     Scope *outerScope = static_cast<Scope *>(outerType);
78     // *slowly* pop first element
79     names.erase(names.begin());
80     FQName innerName;
81     CHECK(FQName::parse(StringHelper::JoinStrings(names, "."), &innerName));
82     return outerScope->lookupType(innerName);
83 }
84 
lookupIdentifier(const std::string &) const85 LocalIdentifier *Scope::lookupIdentifier(const std::string & /*name*/) const {
86     return nullptr;
87 }
88 
isScope() const89 bool Scope::isScope() const {
90     return true;
91 }
92 
getInterface() const93 Interface *Scope::getInterface() const {
94     if (mTypes.size() == 1 && mTypes[0]->isInterface()) {
95         return static_cast<Interface *>(mTypes[0]);
96     }
97 
98     return nullptr;
99 }
100 
definesInterfaces() const101 bool Scope::definesInterfaces() const {
102     for (const NamedType *type : mTypes) {
103         if (type->isInterface()) {
104             return true;
105         }
106     }
107 
108     return false;
109 }
110 
annotations() const111 const std::vector<Annotation*>& Scope::annotations() const {
112     return mAnnotations;
113 }
114 
setAnnotations(std::vector<Annotation * > * annotations)115 void Scope::setAnnotations(std::vector<Annotation*>* annotations) {
116     CHECK(mAnnotations.empty());
117     CHECK(annotations != nullptr);
118     mAnnotations = *annotations;
119 }
120 
getDefinedTypes() const121 std::vector<const Type*> Scope::getDefinedTypes() const {
122     std::vector<const Type*> ret;
123     ret.insert(ret.end(), mTypes.begin(), mTypes.end());
124     return ret;
125 }
126 
getSortedDefinedTypes() const127 std::vector<const NamedType*> Scope::getSortedDefinedTypes() const {
128     std::vector<const NamedType*> ret;
129     ret.insert(ret.end(), mTypes.begin(), mTypes.end());
130 
131     std::sort(ret.begin(), ret.end(), [](const NamedType* lhs, const NamedType* rhs) -> bool {
132         return lhs->location() < rhs->location();
133     });
134     return ret;
135 }
136 
topologicalReorder(const std::unordered_map<const Type *,size_t> & reversedOrder)137 void Scope::topologicalReorder(const std::unordered_map<const Type*, size_t>& reversedOrder) {
138     auto less = [&](const Type* lhs, const Type* rhs) {
139         return reversedOrder.at(lhs) < reversedOrder.at(rhs);
140     };
141 
142     if (std::is_sorted(mTypes.begin(), mTypes.end(), less)) return;
143 
144     mTypeOrderChanged = true;
145     std::sort(mTypes.begin(), mTypes.end(), less);
146 
147     for (size_t i = 0; i != mTypes.size(); ++i) {
148         mTypeIndexByName.at(mTypes[i]->definedName()) = i;
149     }
150 }
151 
emitHidlDefinition(Formatter & out) const152 void Scope::emitHidlDefinition(Formatter& out) const {
153     const std::vector<const NamedType*>& definedTypes = getSortedDefinedTypes();
154     out.join(definedTypes.begin(), definedTypes.end(), "\n",
155              [&](auto t) { t->emitHidlDefinition(out); });
156 }
157 
emitTypeDeclarations(Formatter & out) const158 void Scope::emitTypeDeclarations(Formatter& out) const {
159     if (mTypes.empty()) return;
160 
161     out << "// Forward declaration for forward reference support:\n";
162     for (const Type* type : mTypes) {
163         type->emitTypeForwardDeclaration(out);
164     }
165     out << "\n";
166 
167     if (mTypeOrderChanged) {
168         out << "// Order of inner types was changed for forward reference support.\n\n";
169     }
170 
171     for (const Type* type : mTypes) {
172         type->emitDocComment(out);
173         type->emitTypeDeclarations(out);
174     }
175 }
176 
emitGlobalTypeDeclarations(Formatter & out) const177 void Scope::emitGlobalTypeDeclarations(Formatter& out) const {
178     for (const Type* type : mTypes) {
179         type->emitGlobalTypeDeclarations(out);
180     }
181 }
182 
emitPackageTypeDeclarations(Formatter & out) const183 void Scope::emitPackageTypeDeclarations(Formatter& out) const {
184     for (const Type* type : mTypes) {
185         type->emitPackageTypeDeclarations(out);
186     }
187 }
188 
emitPackageTypeHeaderDefinitions(Formatter & out) const189 void Scope::emitPackageTypeHeaderDefinitions(Formatter& out) const {
190     for (const Type* type : mTypes) {
191         type->emitPackageTypeHeaderDefinitions(out);
192     }
193 }
194 
emitPackageHwDeclarations(Formatter & out) const195 void Scope::emitPackageHwDeclarations(Formatter& out) const {
196     for (const Type* type : mTypes) {
197         type->emitPackageHwDeclarations(out);
198     }
199 }
200 
emitJavaTypeDeclarations(Formatter & out,bool atTopLevel) const201 void Scope::emitJavaTypeDeclarations(Formatter& out, bool atTopLevel) const {
202     if (mTypeOrderChanged) {
203         out << "// Order of inner types was changed for forward reference support.\n\n";
204     }
205 
206     for (const Type* type : mTypes) {
207         type->emitDocComment(out);
208         type->emitJavaTypeDeclarations(out, atTopLevel);
209     }
210 }
211 
emitTypeDefinitions(Formatter & out,const std::string & prefix) const212 void Scope::emitTypeDefinitions(Formatter& out, const std::string& prefix) const {
213     for (const Type* type : mTypes) {
214         type->emitTypeDefinitions(out, prefix);
215     }
216 }
217 
getSubTypes() const218 const std::vector<NamedType *> &Scope::getSubTypes() const {
219     return mTypes;
220 }
221 
emitVtsTypeDeclarations(Formatter & out) const222 void Scope::emitVtsTypeDeclarations(Formatter& out) const {
223     for (const Type* type : mTypes) {
224         type->emitVtsTypeDeclarations(out);
225     }
226 }
227 
deepIsJavaCompatible(std::unordered_set<const Type * > * visited) const228 bool Scope::deepIsJavaCompatible(std::unordered_set<const Type*>* visited) const {
229     for (const Type* type : mTypes) {
230         // Java compatibility focuses on types that are actually used by interfaces.
231         // Declarations of java-incompatible types are simply omitted from
232         // corresponding Java libraries.
233         if (type->isInterface() && !type->isJavaCompatible(visited)) {
234             return false;
235         }
236     }
237 
238     return Type::deepIsJavaCompatible(visited);
239 }
240 
appendToExportedTypesVector(std::vector<const Type * > * exportedTypes) const241 void Scope::appendToExportedTypesVector(
242         std::vector<const Type *> *exportedTypes) const {
243     for (const Type* type : mTypes) {
244         type->appendToExportedTypesVector(exportedTypes);
245     }
246 }
247 
248 ////////////////////////////////////////
249 
RootScope(const char * localName,const FQName & fullName,const Location & location,Scope * parent)250 RootScope::RootScope(const char* localName, const FQName& fullName, const Location& location,
251                      Scope* parent)
252     : Scope(localName, fullName, location, parent) {}
~RootScope()253 RootScope::~RootScope() {}
254 
typeName() const255 std::string RootScope::typeName() const {
256     return "(root scope)";
257 }
258 
validate() const259 status_t RootScope::validate() const {
260     CHECK(annotations().empty());
261     return Scope::validate();
262 }
263 
264 ////////////////////////////////////////
265 
LocalIdentifier()266 LocalIdentifier::LocalIdentifier(){}
~LocalIdentifier()267 LocalIdentifier::~LocalIdentifier(){}
268 
isEnumValue() const269 bool LocalIdentifier::isEnumValue() const {
270     return false;
271 }
272 
resolve() const273 const LocalIdentifier* LocalIdentifier::resolve() const {
274     return this;
275 }
276 
resolve()277 LocalIdentifier* LocalIdentifier::resolve() {
278     return this;
279 }
280 
constExpr() const281 ConstantExpression* LocalIdentifier::constExpr() const {
282     return nullptr;
283 }
284 
285 }  // namespace android
286 
287