blob: 6602ba4fae88fdb1d1582a97553fc00b55e887e0 [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 "AST.h"
18
Andreas Huber5345ec22016-07-29 13:33:27 -070019#include "Coordinator.h"
Yifan Hongf24fa852016-09-23 11:03:15 -070020#include "EnumType.h"
Timur Iskhakov33431e62017-08-21 17:31:23 -070021#include "FmqType.h"
Andreas Hubereb1081f2016-07-28 13:13:24 -070022#include "HandleType.h"
Andreas Huberbfd76212016-08-09 11:12:16 -070023#include "Interface.h"
Yifan Honga4b53d02016-10-31 17:29:10 -070024#include "Location.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070025#include "Scope.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070026#include "TypeDef.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070027
Andreas Hubereb1081f2016-07-28 13:13:24 -070028#include <android-base/logging.h>
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070029#include <hidl-util/FQName.h>
30#include <hidl-util/Formatter.h>
31#include <hidl-util/StringHelper.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070032#include <stdlib.h>
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070033#include <algorithm>
34#include <iostream>
Andreas Huberc9410c72016-07-28 12:18:40 -070035
Andreas Huberc9410c72016-07-28 12:18:40 -070036namespace android {
37
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070038AST::AST(const Coordinator* coordinator, const std::string& path)
Andreas Huber5345ec22016-07-29 13:33:27 -070039 : mCoordinator(coordinator),
Andreas Huber0d0f9a22016-08-17 10:26:11 -070040 mPath(path),
Steven Moreland0ecc7b82017-07-19 12:59:23 -070041 mRootScope("(root scope)", Location::startOf(path), nullptr /* parent */) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070042
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070043Scope* AST::getRootScope() {
44 return &mRootScope;
Andreas Huberc9410c72016-07-28 12:18:40 -070045}
46
Yifan Hongbe627b32016-10-28 18:38:56 -070047// used by the parser.
48void AST::addSyntaxError() {
49 mSyntaxErrors++;
50}
51
52size_t AST::syntaxErrors() const {
53 return mSyntaxErrors;
54}
55
Andreas Huber0d0f9a22016-08-17 10:26:11 -070056const std::string &AST::getFilename() const {
57 return mPath;
58}
59
Andreas Huber84f89de2016-07-28 15:39:51 -070060bool AST::setPackage(const char *package) {
Andreas Huberda51b8e2016-07-28 16:00:57 -070061 mPackage.setTo(package);
62 CHECK(mPackage.isValid());
Andreas Huber84f89de2016-07-28 15:39:51 -070063
Andreas Huberda51b8e2016-07-28 16:00:57 -070064 if (mPackage.package().empty()
65 || mPackage.version().empty()
66 || !mPackage.name().empty()) {
Andreas Huber84f89de2016-07-28 15:39:51 -070067 return false;
68 }
69
Andreas Huber84f89de2016-07-28 15:39:51 -070070 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070071}
72
Andreas Hubera2723d22016-07-29 15:36:07 -070073FQName AST::package() const {
74 return mPackage;
75}
76
Steven Moreland19f11b52017-05-12 18:22:21 -070077bool AST::isInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070078 return mRootScope.getInterface() != nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -070079}
80
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070081bool AST::containsInterfaces() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070082 return mRootScope.containsInterfaces();
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070083}
84
Timur Iskhakov33431e62017-08-21 17:31:23 -070085status_t AST::postParse() {
86 status_t err = resolveInheritance();
87 if (err != OK) return err;
88 err = evaluate();
89 if (err != OK) return err;
90 err = validate();
91 if (err != OK) return err;
92
93 return OK;
94}
95
Timur Iskhakovcec46c42017-08-09 00:22:02 -070096status_t AST::resolveInheritance() {
Timur Iskhakov33431e62017-08-21 17:31:23 -070097 std::unordered_set<const Type*> visited;
98 return mRootScope.recursivePass(&Type::resolveInheritance, &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -070099}
100
101status_t AST::evaluate() {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700102 std::unordered_set<const Type*> visited;
103 return mRootScope.recursivePass(&Type::evaluate, &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700104}
105
106status_t AST::validate() const {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700107 std::unordered_set<const Type*> visited;
108 return mRootScope.recursivePass(&Type::validate, &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700109}
110
Andreas Huber5345ec22016-07-29 13:33:27 -0700111bool AST::addImport(const char *import) {
112 FQName fqName(import);
113 CHECK(fqName.isValid());
Andreas Hubereb1081f2016-07-28 13:13:24 -0700114
Andreas Huber5345ec22016-07-29 13:33:27 -0700115 fqName.applyDefaults(mPackage.package(), mPackage.version());
116
Andreas Huber68f24592016-07-29 14:53:48 -0700117 // LOG(INFO) << "importing " << fqName.string();
Andreas Huber5345ec22016-07-29 13:33:27 -0700118
119 if (fqName.name().empty()) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700120 // import a package
Andreas Huberd2943e12016-08-05 11:59:31 -0700121 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -0700122
Andreas Huberd2943e12016-08-05 11:59:31 -0700123 status_t err =
Steven Morelandaa186832016-09-26 13:51:43 -0700124 mCoordinator->appendPackageInterfacesToVector(fqName,
125 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -0700126
127 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700128 return false;
129 }
130
Andreas Huberd2943e12016-08-05 11:59:31 -0700131 for (const auto &subFQName : packageInterfaces) {
Yifan Hongf619fc72017-04-07 15:40:06 -0700132 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700133 AST* ast = mCoordinator->parse(subFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700134 if (ast == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700135 return false;
136 }
Yifan Hong1977ea32016-10-05 12:49:08 -0700137 // all previous single type imports are ignored.
138 mImportedTypes.erase(ast);
Andreas Huber68f24592016-07-29 14:53:48 -0700139 }
140
141 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700142 }
143
Yifan Hong1977ea32016-10-05 12:49:08 -0700144 AST *importAST;
Andreas Huber5345ec22016-07-29 13:33:27 -0700145
Yifan Hong1977ea32016-10-05 12:49:08 -0700146 // cases like android.hardware.foo@1.0::IFoo.Internal
147 // android.hardware.foo@1.0::Abc.Internal
148
149 // assume it is an interface, and try to import it.
150 const FQName interfaceName = fqName.getTopLevelType();
Yifan Hongf619fc72017-04-07 15:40:06 -0700151 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700152 importAST = mCoordinator->parse(interfaceName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700153
154 if (importAST != nullptr) {
155 // cases like android.hardware.foo@1.0::IFoo.Internal
156 // and android.hardware.foo@1.0::IFoo
157 if (fqName == interfaceName) {
158 // import a single file.
159 // all previous single type imports are ignored.
160 // cases like android.hardware.foo@1.0::IFoo
161 // and android.hardware.foo@1.0::types
162 mImportedTypes.erase(importAST);
163 return true;
164 }
165
166 // import a single type from this file
167 // cases like android.hardware.foo@1.0::IFoo.Internal
168 FQName matchingName;
169 Type *match = importAST->findDefinedType(fqName, &matchingName);
170 if (match == nullptr) {
171 return false;
172 }
173 // will automatically create a set if it does not exist
174 mImportedTypes[importAST].insert(match);
175 return true;
Andreas Huber68f24592016-07-29 14:53:48 -0700176 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700177
Yifan Hong1977ea32016-10-05 12:49:08 -0700178 // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal
179 FQName typesFQName = fqName.getTypesForPackage();
Yifan Hongf619fc72017-04-07 15:40:06 -0700180
181 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700182 importAST = mCoordinator->parse(typesFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700183
184 if (importAST != nullptr) {
185 // Attempt to find Abc.Internal in types.
186 FQName matchingName;
187 Type *match = importAST->findDefinedType(fqName, &matchingName);
188 if (match == nullptr) {
189 return false;
190 }
191 // will automatically create a set if not exist
192 mImportedTypes[importAST].insert(match);
193 return true;
194 }
195
196 // can't find an appropriate AST for fqName.
197 return false;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700198}
199
Andreas Huber39fa7182016-08-19 14:27:33 -0700200void AST::addImportedAST(AST *ast) {
201 mImportedASTs.insert(ast);
202}
203
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700204bool AST::addScopedType(NamedType* type, std::string* errorMsg, Scope* scope) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700205 bool success = scope->addType(type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700206 if (!success) {
207 return false;
208 }
209
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700210 std::vector<std::string> pathComponents{{type->localName()}};
211 for (; scope != &mRootScope; scope = scope->parent()) {
212 pathComponents.push_back(scope->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700213 }
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700214
215 std::reverse(pathComponents.begin(), pathComponents.end());
216 std::string path = StringHelper::JoinStrings(pathComponents, ".");
Andreas Huber31629bc2016-08-03 09:06:40 -0700217
Andreas Huber31629bc2016-08-03 09:06:40 -0700218 FQName fqName(mPackage.package(), mPackage.version(), path);
Steven Morelandd537ab02016-09-12 10:32:01 -0700219 type->setFullName(fqName);
Andreas Huber39fa7182016-08-19 14:27:33 -0700220
Steven Morelandd537ab02016-09-12 10:32:01 -0700221 mDefinedTypesByFullName[fqName] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700222
Andreas Huber5a545442016-08-03 10:44:56 -0700223 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700224}
225
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700226EnumValue* AST::lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700227 FQName enumTypeName = fqName.typeName();
228 std::string enumValueName = fqName.valueName();
229
230 CHECK(enumTypeName.isValid());
231 CHECK(!enumValueName.empty());
232
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700233 Type* type = lookupType(enumTypeName, scope);
Yifan Hongf24fa852016-09-23 11:03:15 -0700234 if(type == nullptr) {
235 *errorMsg = "Cannot find type " + enumTypeName.string();
236 return nullptr;
237 }
238 if(!type->isEnum()) {
239 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
240 return nullptr;
241 }
242
243 EnumType *enumType = static_cast<EnumType *>(type);
244 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
245 if(v == nullptr) {
246 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
247 return nullptr;
248 }
249 return v;
250}
251
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700252Type* AST::lookupType(const FQName& fqName, Scope* scope) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700253 CHECK(fqName.isValid());
254
Andreas Huberda51b8e2016-07-28 16:00:57 -0700255 if (fqName.name().empty()) {
256 // Given a package and version???
Yifan Hong1977ea32016-10-05 12:49:08 -0700257 return nullptr;
Andreas Huberda51b8e2016-07-28 16:00:57 -0700258 }
259
Yifan Hong87ff8232017-01-09 12:07:05 -0800260 Type *returnedType = nullptr;
261
Andreas Huber84f89de2016-07-28 15:39:51 -0700262 if (fqName.package().empty() && fqName.version().empty()) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800263 // resolve locally first if possible.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700264 returnedType = lookupTypeLocally(fqName, scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800265 if (returnedType != nullptr) {
266 return returnedType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700267 }
268 }
269
Yifan Hong87ff8232017-01-09 12:07:05 -0800270 if (!fqName.isFullyQualified()) {
271 status_t status = lookupAutofilledType(fqName, &returnedType);
272 if (status != OK) {
273 return nullptr;
274 }
275 if (returnedType != nullptr) {
276 return returnedType;
277 }
278 }
279
280 return lookupTypeFromImports(fqName);
281}
282
283// Rule 0: try resolve locally
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700284Type* AST::lookupTypeLocally(const FQName& fqName, Scope* scope) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800285 CHECK(fqName.package().empty() && fqName.version().empty()
286 && !fqName.name().empty() && fqName.valueName().empty());
287
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700288 for (; scope != nullptr; scope = scope->parent()) {
289 Type* type = scope->lookupType(fqName);
Yifan Hong87ff8232017-01-09 12:07:05 -0800290
291 if (type != nullptr) {
292 // Resolve typeDefs to the target type.
293 while (type->isTypeDef()) {
294 type = static_cast<TypeDef *>(type)->referencedType();
295 }
296
297 return type;
298 }
299 }
300
301 return nullptr;
302}
303
304// Rule 1: auto-fill with current package
305status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) {
306 CHECK(!fqName.isFullyQualified() && !fqName.name().empty() && fqName.valueName().empty());
307
308 FQName autofilled = fqName;
309 autofilled.applyDefaults(mPackage.package(), mPackage.version());
310 FQName matchingName;
311 // Given this fully-qualified name, the type may be defined in this AST, or other files
312 // in import.
313 Type *local = findDefinedType(autofilled, &matchingName);
314 CHECK(local == nullptr || autofilled == matchingName);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700315 Type* fromImport = lookupType(autofilled, nullptr /* scope */);
Yifan Hong87ff8232017-01-09 12:07:05 -0800316
317 if (local != nullptr && fromImport != nullptr && local != fromImport) {
318 // Something bad happen; two types have the same FQName.
319 std::cerr << "ERROR: Unable to resolve type name '"
320 << fqName.string()
321 << "' (i.e. '"
322 << autofilled.string()
323 << "'), multiple definitions found.\n";
324
325 return UNKNOWN_ERROR;
326 }
327 if (local != nullptr) {
328 *returnedType = local;
329 return OK;
330 }
331 // If fromImport is nullptr as well, return nullptr to fall through to next rule.
332 *returnedType = fromImport;
333 return OK;
334}
335
336// Rule 2: look at imports
337Type *AST::lookupTypeFromImports(const FQName &fqName) {
338
Andreas Huber39fa7182016-08-19 14:27:33 -0700339 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700340 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700341 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700342
Andreas Huber39fa7182016-08-19 14:27:33 -0700343 for (const auto &importedAST : mImportedASTs) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700344 if (mImportedTypes.find(importedAST) != mImportedTypes.end()) {
345 // ignore single type imports
346 continue;
347 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700348 FQName matchingName;
349 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700350
Andreas Huber39fa7182016-08-19 14:27:33 -0700351 if (match != nullptr) {
352 if (resolvedType != nullptr) {
353 std::cerr << "ERROR: Unable to resolve type name '"
354 << fqName.string()
355 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700356
Andreas Huber39fa7182016-08-19 14:27:33 -0700357 std::cerr << " " << resolvedName.string() << "\n";
358 std::cerr << " " << matchingName.string() << "\n";
359
Yifan Hong1977ea32016-10-05 12:49:08 -0700360 return nullptr;
361 }
362
363 resolvedType = match;
364 returnedType = resolvedType;
365 resolvedName = matchingName;
366
367 // Keep going even after finding a match.
368 }
369 }
370
371 for (const auto &pair : mImportedTypes) {
372 AST *importedAST = pair.first;
373 std::set<Type *> importedTypes = pair.second;
374
375 FQName matchingName;
376 Type *match = importedAST->findDefinedType(fqName, &matchingName);
377 if (match != nullptr &&
378 importedTypes.find(match) != importedTypes.end()) {
379 if (resolvedType != nullptr) {
380 std::cerr << "ERROR: Unable to resolve type name '"
381 << fqName.string()
382 << "', multiple matches found:\n";
383
384 std::cerr << " " << resolvedName.string() << "\n";
385 std::cerr << " " << matchingName.string() << "\n";
386
387 return nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700388 }
389
390 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700391 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700392 resolvedName = matchingName;
393
394 // Keep going even after finding a match.
395 }
396 }
397
398 if (resolvedType) {
399#if 0
400 LOG(INFO) << "found '"
401 << resolvedName.string()
402 << "' after looking for '"
403 << fqName.string()
404 << "'.";
405#endif
406
407 // Resolve typeDefs to the target type.
408 while (resolvedType->isTypeDef()) {
409 resolvedType =
410 static_cast<TypeDef *>(resolvedType)->referencedType();
411 }
412
Iliyan Malchev800273d2016-09-02 15:25:07 -0700413 returnedType = resolvedType;
414
415 // If the resolved type is not an interface, we need to determine
416 // whether it is defined in types.hal, or in some other interface. In
417 // the latter case, we need to emit a dependency for the interface in
418 // which the type is defined.
419 //
420 // Consider the following:
421 // android.hardware.tests.foo@1.0::Record
422 // android.hardware.tests.foo@1.0::IFoo.Folder
423 // android.hardware.tests.foo@1.0::Folder
424 //
425 // If Record is an interface, then we keep track of it for the purpose
426 // of emitting dependencies in the target language (for example #include
427 // in C++). If Record is a UDT, then we assume it is defined in
428 // types.hal in android.hardware.tests.foo@1.0.
429 //
430 // In the case of IFoo.Folder, the same applies. If IFoo is an
431 // interface, we need to track this for the purpose of emitting
432 // dependencies. If not, then it must have been defined in types.hal.
433 //
434 // In the case of just specifying Folder, the resolved type is
Yifan Hongfece6ec2017-01-12 17:04:04 -0800435 // android.hardware.tests.foo@1.0::Folder, and the same logic as
Iliyan Malchev800273d2016-09-02 15:25:07 -0700436 // above applies.
437
438 if (!resolvedType->isInterface()) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800439 FQName ifc = resolvedName.getTopLevelType();
Iliyan Malchev800273d2016-09-02 15:25:07 -0700440 for (const auto &importedAST : mImportedASTs) {
441 FQName matchingName;
442 Type *match = importedAST->findDefinedType(ifc, &matchingName);
443 if (match != nullptr && match->isInterface()) {
444 resolvedType = match;
445 }
446 }
447 }
448
Andreas Huber39fa7182016-08-19 14:27:33 -0700449 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700450 // Non-interface types are declared in the associated types header.
Yifan Hongfece6ec2017-01-12 17:04:04 -0800451 FQName typesName = resolvedName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -0700452
Andreas Huber0e00de42016-08-03 09:56:02 -0700453 mImportedNames.insert(typesName);
454 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700455 // Do _not_ use fqName, i.e. the name we used to look up the type,
456 // but instead use the name of the interface we found.
457 // This is necessary because if fqName pointed to a typedef which
458 // in turn referenced the found interface we'd mistakenly use the
459 // name of the typedef instead of the proper name of the interface.
460
461 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700462 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700463 }
Andreas Huber737080b2016-08-02 15:38:04 -0700464 }
465
Steven Morelandbb5c80b2016-10-05 11:07:36 -0700466 return returnedType;
Andreas Huber5345ec22016-07-29 13:33:27 -0700467}
468
Andreas Huber39fa7182016-08-19 14:27:33 -0700469Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700470 for (const auto &pair : mDefinedTypesByFullName) {
471 const FQName &key = pair.first;
472 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700473
Andreas Huber39fa7182016-08-19 14:27:33 -0700474 if (key.endsWith(fqName)) {
475 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700476 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700477 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700478 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700479
480 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700481}
482
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700483void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700484 for (const auto &fqName : mImportedNames) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800485 FQName packageName = fqName.getPackageAndVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700486
487 if (packageName == mPackage) {
488 // We only care about external imports, not our own package.
489 continue;
490 }
491
492 importSet->insert(packageName);
493 }
494}
495
Yifan Hong40a373d2016-11-30 15:16:47 -0800496void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const {
497 getImportedPackages(importSet);
498 std::set<FQName> newSet;
499 for (const auto &ast : mImportedASTs) {
500 if (importSet->find(ast->package()) != importSet->end()) {
501 ast->getImportedPackagesHierarchy(&newSet);
502 }
503 }
504 importSet->insert(newSet.begin(), newSet.end());
505}
506
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800507void AST::getAllImportedNames(std::set<FQName> *allImportNames) const {
508 for (const auto& name : mImportedNames) {
509 allImportNames->insert(name);
Steven Morelandc59326e2017-06-20 15:19:30 -0700510 AST* ast = mCoordinator->parse(name, nullptr /* imported */, Coordinator::Enforce::NONE);
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800511 ast->getAllImportedNames(allImportNames);
512 }
513}
514
Andreas Huber0fa9e392016-08-31 09:05:44 -0700515bool AST::isJavaCompatible() const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700516 if (!AST::isInterface()) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700517 for (const auto* type : mRootScope.getSubTypes()) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700518 if (!type->isJavaCompatible()) {
519 return false;
520 }
521 }
522
523 return true;
524 }
525
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700526 const Interface* iface = mRootScope.getInterface();
Andreas Huber0fa9e392016-08-31 09:05:44 -0700527 return iface->isJavaCompatible();
528}
529
Andreas Huber019d21d2016-10-03 12:59:47 -0700530void AST::appendToExportedTypesVector(
531 std::vector<const Type *> *exportedTypes) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700532 mRootScope.appendToExportedTypesVector(exportedTypes);
Andreas Huber019d21d2016-10-03 12:59:47 -0700533}
534
Yifan Hongc8934042016-11-17 17:10:52 -0800535bool AST::isIBase() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700536 Interface* iface = mRootScope.getInterface();
Yifan Hongc8934042016-11-17 17:10:52 -0800537 return iface != nullptr && iface->isIBase();
538}
539
Yifan Hong78b38d12017-02-13 18:14:46 +0000540const Interface *AST::getInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700541 return mRootScope.getInterface();
Yifan Hong78b38d12017-02-13 18:14:46 +0000542}
543
Steven Moreland19f11b52017-05-12 18:22:21 -0700544std::string AST::getBaseName() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700545 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -0700546
547 return iface ? iface->getBaseName() : "types";
548}
549
Andreas Huberc9410c72016-07-28 12:18:40 -0700550} // namespace android;