blob: e33fbbf847a41f363c082bf1abf78b3ca6c65a7c [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"
Andreas Hubereb1081f2016-07-28 13:13:24 -070021#include "HandleType.h"
Andreas Huberbfd76212016-08-09 11:12:16 -070022#include "Interface.h"
Yifan Honga4b53d02016-10-31 17:29:10 -070023#include "Location.h"
Hridya Valsarajua32bde82016-12-27 11:47:46 -080024#include "FmqType.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
Andreas Huber5345ec22016-07-29 13:33:27 -070085bool AST::addImport(const char *import) {
86 FQName fqName(import);
87 CHECK(fqName.isValid());
Andreas Hubereb1081f2016-07-28 13:13:24 -070088
Andreas Huber5345ec22016-07-29 13:33:27 -070089 fqName.applyDefaults(mPackage.package(), mPackage.version());
90
Andreas Huber68f24592016-07-29 14:53:48 -070091 // LOG(INFO) << "importing " << fqName.string();
Andreas Huber5345ec22016-07-29 13:33:27 -070092
93 if (fqName.name().empty()) {
Yifan Hong1977ea32016-10-05 12:49:08 -070094 // import a package
Andreas Huberd2943e12016-08-05 11:59:31 -070095 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -070096
Andreas Huberd2943e12016-08-05 11:59:31 -070097 status_t err =
Steven Morelandaa186832016-09-26 13:51:43 -070098 mCoordinator->appendPackageInterfacesToVector(fqName,
99 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -0700100
101 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700102 return false;
103 }
104
Andreas Huberd2943e12016-08-05 11:59:31 -0700105 for (const auto &subFQName : packageInterfaces) {
Yifan Hongf619fc72017-04-07 15:40:06 -0700106 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700107 AST* ast = mCoordinator->parse(subFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700108 if (ast == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700109 return false;
110 }
Yifan Hong1977ea32016-10-05 12:49:08 -0700111 // all previous single type imports are ignored.
112 mImportedTypes.erase(ast);
Andreas Huber68f24592016-07-29 14:53:48 -0700113 }
114
115 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700116 }
117
Yifan Hong1977ea32016-10-05 12:49:08 -0700118 AST *importAST;
Andreas Huber5345ec22016-07-29 13:33:27 -0700119
Yifan Hong1977ea32016-10-05 12:49:08 -0700120 // cases like android.hardware.foo@1.0::IFoo.Internal
121 // android.hardware.foo@1.0::Abc.Internal
122
123 // assume it is an interface, and try to import it.
124 const FQName interfaceName = fqName.getTopLevelType();
Yifan Hongf619fc72017-04-07 15:40:06 -0700125 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700126 importAST = mCoordinator->parse(interfaceName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700127
128 if (importAST != nullptr) {
129 // cases like android.hardware.foo@1.0::IFoo.Internal
130 // and android.hardware.foo@1.0::IFoo
131 if (fqName == interfaceName) {
132 // import a single file.
133 // all previous single type imports are ignored.
134 // cases like android.hardware.foo@1.0::IFoo
135 // and android.hardware.foo@1.0::types
136 mImportedTypes.erase(importAST);
137 return true;
138 }
139
140 // import a single type from this file
141 // cases like android.hardware.foo@1.0::IFoo.Internal
142 FQName matchingName;
143 Type *match = importAST->findDefinedType(fqName, &matchingName);
144 if (match == nullptr) {
145 return false;
146 }
147 // will automatically create a set if it does not exist
148 mImportedTypes[importAST].insert(match);
149 return true;
Andreas Huber68f24592016-07-29 14:53:48 -0700150 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700151
Yifan Hong1977ea32016-10-05 12:49:08 -0700152 // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal
153 FQName typesFQName = fqName.getTypesForPackage();
Yifan Hongf619fc72017-04-07 15:40:06 -0700154
155 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700156 importAST = mCoordinator->parse(typesFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700157
158 if (importAST != nullptr) {
159 // Attempt to find Abc.Internal in types.
160 FQName matchingName;
161 Type *match = importAST->findDefinedType(fqName, &matchingName);
162 if (match == nullptr) {
163 return false;
164 }
165 // will automatically create a set if not exist
166 mImportedTypes[importAST].insert(match);
167 return true;
168 }
169
170 // can't find an appropriate AST for fqName.
171 return false;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700172}
173
Andreas Huber39fa7182016-08-19 14:27:33 -0700174void AST::addImportedAST(AST *ast) {
175 mImportedASTs.insert(ast);
176}
177
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700178bool AST::addTypeDef(const char* localName, Type* type, const Location& location,
179 std::string* errorMsg, Scope* scope) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700180 // The reason we wrap the given type in a TypeDef is simply to suppress
181 // emitting any type definitions later on, since this is just an alias
182 // to a type defined elsewhere.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700183 return addScopedTypeInternal(new TypeDef(localName, location, scope, type), errorMsg, scope);
Andreas Huber39fa7182016-08-19 14:27:33 -0700184}
185
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700186bool AST::addScopedType(NamedType* type, std::string* errorMsg, Scope* scope) {
187 return addScopedTypeInternal(type, errorMsg, scope);
Andreas Huber39fa7182016-08-19 14:27:33 -0700188}
189
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700190bool AST::addScopedTypeInternal(NamedType* type, std::string* errorMsg, Scope* scope) {
191 bool success = scope->addType(type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700192 if (!success) {
193 return false;
194 }
195
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700196 std::vector<std::string> pathComponents{{type->localName()}};
197 for (; scope != &mRootScope; scope = scope->parent()) {
198 pathComponents.push_back(scope->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700199 }
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700200
201 std::reverse(pathComponents.begin(), pathComponents.end());
202 std::string path = StringHelper::JoinStrings(pathComponents, ".");
Andreas Huber31629bc2016-08-03 09:06:40 -0700203
Andreas Huber31629bc2016-08-03 09:06:40 -0700204 FQName fqName(mPackage.package(), mPackage.version(), path);
Steven Morelandd537ab02016-09-12 10:32:01 -0700205 type->setFullName(fqName);
Andreas Huber39fa7182016-08-19 14:27:33 -0700206
Steven Morelandd537ab02016-09-12 10:32:01 -0700207 mDefinedTypesByFullName[fqName] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700208
Andreas Huber5a545442016-08-03 10:44:56 -0700209 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700210}
211
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700212EnumValue* AST::lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700213 FQName enumTypeName = fqName.typeName();
214 std::string enumValueName = fqName.valueName();
215
216 CHECK(enumTypeName.isValid());
217 CHECK(!enumValueName.empty());
218
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700219 Type* type = lookupType(enumTypeName, scope);
Yifan Hongf24fa852016-09-23 11:03:15 -0700220 if(type == nullptr) {
221 *errorMsg = "Cannot find type " + enumTypeName.string();
222 return nullptr;
223 }
224 if(!type->isEnum()) {
225 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
226 return nullptr;
227 }
228
229 EnumType *enumType = static_cast<EnumType *>(type);
230 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
231 if(v == nullptr) {
232 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
233 return nullptr;
234 }
235 return v;
236}
237
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700238Type* AST::lookupType(const FQName& fqName, Scope* scope) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700239 CHECK(fqName.isValid());
240
Andreas Huberda51b8e2016-07-28 16:00:57 -0700241 if (fqName.name().empty()) {
242 // Given a package and version???
Yifan Hong1977ea32016-10-05 12:49:08 -0700243 return nullptr;
Andreas Huberda51b8e2016-07-28 16:00:57 -0700244 }
245
Yifan Hong87ff8232017-01-09 12:07:05 -0800246 Type *returnedType = nullptr;
247
Andreas Huber84f89de2016-07-28 15:39:51 -0700248 if (fqName.package().empty() && fqName.version().empty()) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800249 // resolve locally first if possible.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700250 returnedType = lookupTypeLocally(fqName, scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800251 if (returnedType != nullptr) {
252 return returnedType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700253 }
254 }
255
Yifan Hong87ff8232017-01-09 12:07:05 -0800256 if (!fqName.isFullyQualified()) {
257 status_t status = lookupAutofilledType(fqName, &returnedType);
258 if (status != OK) {
259 return nullptr;
260 }
261 if (returnedType != nullptr) {
262 return returnedType;
263 }
264 }
265
266 return lookupTypeFromImports(fqName);
267}
268
269// Rule 0: try resolve locally
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700270Type* AST::lookupTypeLocally(const FQName& fqName, Scope* scope) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800271 CHECK(fqName.package().empty() && fqName.version().empty()
272 && !fqName.name().empty() && fqName.valueName().empty());
273
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700274 for (; scope != nullptr; scope = scope->parent()) {
275 Type* type = scope->lookupType(fqName);
Yifan Hong87ff8232017-01-09 12:07:05 -0800276
277 if (type != nullptr) {
278 // Resolve typeDefs to the target type.
279 while (type->isTypeDef()) {
280 type = static_cast<TypeDef *>(type)->referencedType();
281 }
282
283 return type;
284 }
285 }
286
287 return nullptr;
288}
289
290// Rule 1: auto-fill with current package
291status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) {
292 CHECK(!fqName.isFullyQualified() && !fqName.name().empty() && fqName.valueName().empty());
293
294 FQName autofilled = fqName;
295 autofilled.applyDefaults(mPackage.package(), mPackage.version());
296 FQName matchingName;
297 // Given this fully-qualified name, the type may be defined in this AST, or other files
298 // in import.
299 Type *local = findDefinedType(autofilled, &matchingName);
300 CHECK(local == nullptr || autofilled == matchingName);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700301 Type* fromImport = lookupType(autofilled, nullptr /* scope */);
Yifan Hong87ff8232017-01-09 12:07:05 -0800302
303 if (local != nullptr && fromImport != nullptr && local != fromImport) {
304 // Something bad happen; two types have the same FQName.
305 std::cerr << "ERROR: Unable to resolve type name '"
306 << fqName.string()
307 << "' (i.e. '"
308 << autofilled.string()
309 << "'), multiple definitions found.\n";
310
311 return UNKNOWN_ERROR;
312 }
313 if (local != nullptr) {
314 *returnedType = local;
315 return OK;
316 }
317 // If fromImport is nullptr as well, return nullptr to fall through to next rule.
318 *returnedType = fromImport;
319 return OK;
320}
321
322// Rule 2: look at imports
323Type *AST::lookupTypeFromImports(const FQName &fqName) {
324
Andreas Huber39fa7182016-08-19 14:27:33 -0700325 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700326 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700327 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700328
Andreas Huber39fa7182016-08-19 14:27:33 -0700329 for (const auto &importedAST : mImportedASTs) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700330 if (mImportedTypes.find(importedAST) != mImportedTypes.end()) {
331 // ignore single type imports
332 continue;
333 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700334 FQName matchingName;
335 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700336
Andreas Huber39fa7182016-08-19 14:27:33 -0700337 if (match != nullptr) {
338 if (resolvedType != nullptr) {
339 std::cerr << "ERROR: Unable to resolve type name '"
340 << fqName.string()
341 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700342
Andreas Huber39fa7182016-08-19 14:27:33 -0700343 std::cerr << " " << resolvedName.string() << "\n";
344 std::cerr << " " << matchingName.string() << "\n";
345
Yifan Hong1977ea32016-10-05 12:49:08 -0700346 return nullptr;
347 }
348
349 resolvedType = match;
350 returnedType = resolvedType;
351 resolvedName = matchingName;
352
353 // Keep going even after finding a match.
354 }
355 }
356
357 for (const auto &pair : mImportedTypes) {
358 AST *importedAST = pair.first;
359 std::set<Type *> importedTypes = pair.second;
360
361 FQName matchingName;
362 Type *match = importedAST->findDefinedType(fqName, &matchingName);
363 if (match != nullptr &&
364 importedTypes.find(match) != importedTypes.end()) {
365 if (resolvedType != nullptr) {
366 std::cerr << "ERROR: Unable to resolve type name '"
367 << fqName.string()
368 << "', multiple matches found:\n";
369
370 std::cerr << " " << resolvedName.string() << "\n";
371 std::cerr << " " << matchingName.string() << "\n";
372
373 return nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700374 }
375
376 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700377 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700378 resolvedName = matchingName;
379
380 // Keep going even after finding a match.
381 }
382 }
383
384 if (resolvedType) {
385#if 0
386 LOG(INFO) << "found '"
387 << resolvedName.string()
388 << "' after looking for '"
389 << fqName.string()
390 << "'.";
391#endif
392
393 // Resolve typeDefs to the target type.
394 while (resolvedType->isTypeDef()) {
395 resolvedType =
396 static_cast<TypeDef *>(resolvedType)->referencedType();
397 }
398
Iliyan Malchev800273d2016-09-02 15:25:07 -0700399 returnedType = resolvedType;
400
401 // If the resolved type is not an interface, we need to determine
402 // whether it is defined in types.hal, or in some other interface. In
403 // the latter case, we need to emit a dependency for the interface in
404 // which the type is defined.
405 //
406 // Consider the following:
407 // android.hardware.tests.foo@1.0::Record
408 // android.hardware.tests.foo@1.0::IFoo.Folder
409 // android.hardware.tests.foo@1.0::Folder
410 //
411 // If Record is an interface, then we keep track of it for the purpose
412 // of emitting dependencies in the target language (for example #include
413 // in C++). If Record is a UDT, then we assume it is defined in
414 // types.hal in android.hardware.tests.foo@1.0.
415 //
416 // In the case of IFoo.Folder, the same applies. If IFoo is an
417 // interface, we need to track this for the purpose of emitting
418 // dependencies. If not, then it must have been defined in types.hal.
419 //
420 // In the case of just specifying Folder, the resolved type is
Yifan Hongfece6ec2017-01-12 17:04:04 -0800421 // android.hardware.tests.foo@1.0::Folder, and the same logic as
Iliyan Malchev800273d2016-09-02 15:25:07 -0700422 // above applies.
423
424 if (!resolvedType->isInterface()) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800425 FQName ifc = resolvedName.getTopLevelType();
Iliyan Malchev800273d2016-09-02 15:25:07 -0700426 for (const auto &importedAST : mImportedASTs) {
427 FQName matchingName;
428 Type *match = importedAST->findDefinedType(ifc, &matchingName);
429 if (match != nullptr && match->isInterface()) {
430 resolvedType = match;
431 }
432 }
433 }
434
Andreas Huber39fa7182016-08-19 14:27:33 -0700435 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700436 // Non-interface types are declared in the associated types header.
Yifan Hongfece6ec2017-01-12 17:04:04 -0800437 FQName typesName = resolvedName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -0700438
Andreas Huber0e00de42016-08-03 09:56:02 -0700439 mImportedNames.insert(typesName);
440 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700441 // Do _not_ use fqName, i.e. the name we used to look up the type,
442 // but instead use the name of the interface we found.
443 // This is necessary because if fqName pointed to a typedef which
444 // in turn referenced the found interface we'd mistakenly use the
445 // name of the typedef instead of the proper name of the interface.
446
447 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700448 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700449 }
Andreas Huber737080b2016-08-02 15:38:04 -0700450 }
451
Steven Morelandbb5c80b2016-10-05 11:07:36 -0700452 return returnedType;
Andreas Huber5345ec22016-07-29 13:33:27 -0700453}
454
Andreas Huber39fa7182016-08-19 14:27:33 -0700455Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700456 for (const auto &pair : mDefinedTypesByFullName) {
457 const FQName &key = pair.first;
458 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700459
Andreas Huber39fa7182016-08-19 14:27:33 -0700460 if (key.endsWith(fqName)) {
461 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700462 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700463 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700464 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700465
466 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700467}
468
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700469void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700470 for (const auto &fqName : mImportedNames) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800471 FQName packageName = fqName.getPackageAndVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700472
473 if (packageName == mPackage) {
474 // We only care about external imports, not our own package.
475 continue;
476 }
477
478 importSet->insert(packageName);
479 }
480}
481
Yifan Hong40a373d2016-11-30 15:16:47 -0800482void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const {
483 getImportedPackages(importSet);
484 std::set<FQName> newSet;
485 for (const auto &ast : mImportedASTs) {
486 if (importSet->find(ast->package()) != importSet->end()) {
487 ast->getImportedPackagesHierarchy(&newSet);
488 }
489 }
490 importSet->insert(newSet.begin(), newSet.end());
491}
492
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800493void AST::getAllImportedNames(std::set<FQName> *allImportNames) const {
494 for (const auto& name : mImportedNames) {
495 allImportNames->insert(name);
Steven Morelandc59326e2017-06-20 15:19:30 -0700496 AST* ast = mCoordinator->parse(name, nullptr /* imported */, Coordinator::Enforce::NONE);
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800497 ast->getAllImportedNames(allImportNames);
498 }
499}
500
Andreas Huber0fa9e392016-08-31 09:05:44 -0700501bool AST::isJavaCompatible() const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700502 if (!AST::isInterface()) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700503 for (const auto* type : mRootScope.getSubTypes()) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700504 if (!type->isJavaCompatible()) {
505 return false;
506 }
507 }
508
509 return true;
510 }
511
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700512 const Interface* iface = mRootScope.getInterface();
Andreas Huber0fa9e392016-08-31 09:05:44 -0700513 return iface->isJavaCompatible();
514}
515
Andreas Huber019d21d2016-10-03 12:59:47 -0700516void AST::appendToExportedTypesVector(
517 std::vector<const Type *> *exportedTypes) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700518 mRootScope.appendToExportedTypesVector(exportedTypes);
Andreas Huber019d21d2016-10-03 12:59:47 -0700519}
520
Yifan Hongc8934042016-11-17 17:10:52 -0800521bool AST::isIBase() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700522 Interface* iface = mRootScope.getInterface();
Yifan Hongc8934042016-11-17 17:10:52 -0800523 return iface != nullptr && iface->isIBase();
524}
525
Yifan Hong78b38d12017-02-13 18:14:46 +0000526const Interface *AST::getInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700527 return mRootScope.getInterface();
Yifan Hong78b38d12017-02-13 18:14:46 +0000528}
529
Steven Moreland19f11b52017-05-12 18:22:21 -0700530std::string AST::getBaseName() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700531 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -0700532
533 return iface ? iface->getBaseName() : "types";
534}
535
Andreas Huberc9410c72016-07-28 12:18:40 -0700536} // namespace android;