blob: b9c3b9c7c4ac0a93b96f91d4ac0a03b8860f4ec1 [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::addScopedType(NamedType* type, std::string* errorMsg, Scope* scope) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700179 bool success = scope->addType(type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700180 if (!success) {
181 return false;
182 }
183
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700184 std::vector<std::string> pathComponents{{type->localName()}};
185 for (; scope != &mRootScope; scope = scope->parent()) {
186 pathComponents.push_back(scope->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700187 }
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700188
189 std::reverse(pathComponents.begin(), pathComponents.end());
190 std::string path = StringHelper::JoinStrings(pathComponents, ".");
Andreas Huber31629bc2016-08-03 09:06:40 -0700191
Andreas Huber31629bc2016-08-03 09:06:40 -0700192 FQName fqName(mPackage.package(), mPackage.version(), path);
Steven Morelandd537ab02016-09-12 10:32:01 -0700193 type->setFullName(fqName);
Andreas Huber39fa7182016-08-19 14:27:33 -0700194
Steven Morelandd537ab02016-09-12 10:32:01 -0700195 mDefinedTypesByFullName[fqName] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700196
Andreas Huber5a545442016-08-03 10:44:56 -0700197 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700198}
199
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700200EnumValue* AST::lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700201 FQName enumTypeName = fqName.typeName();
202 std::string enumValueName = fqName.valueName();
203
204 CHECK(enumTypeName.isValid());
205 CHECK(!enumValueName.empty());
206
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700207 Type* type = lookupType(enumTypeName, scope);
Yifan Hongf24fa852016-09-23 11:03:15 -0700208 if(type == nullptr) {
209 *errorMsg = "Cannot find type " + enumTypeName.string();
210 return nullptr;
211 }
212 if(!type->isEnum()) {
213 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
214 return nullptr;
215 }
216
217 EnumType *enumType = static_cast<EnumType *>(type);
218 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
219 if(v == nullptr) {
220 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
221 return nullptr;
222 }
223 return v;
224}
225
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700226Type* AST::lookupType(const FQName& fqName, Scope* scope) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700227 CHECK(fqName.isValid());
228
Andreas Huberda51b8e2016-07-28 16:00:57 -0700229 if (fqName.name().empty()) {
230 // Given a package and version???
Yifan Hong1977ea32016-10-05 12:49:08 -0700231 return nullptr;
Andreas Huberda51b8e2016-07-28 16:00:57 -0700232 }
233
Yifan Hong87ff8232017-01-09 12:07:05 -0800234 Type *returnedType = nullptr;
235
Andreas Huber84f89de2016-07-28 15:39:51 -0700236 if (fqName.package().empty() && fqName.version().empty()) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800237 // resolve locally first if possible.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700238 returnedType = lookupTypeLocally(fqName, scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800239 if (returnedType != nullptr) {
240 return returnedType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700241 }
242 }
243
Yifan Hong87ff8232017-01-09 12:07:05 -0800244 if (!fqName.isFullyQualified()) {
245 status_t status = lookupAutofilledType(fqName, &returnedType);
246 if (status != OK) {
247 return nullptr;
248 }
249 if (returnedType != nullptr) {
250 return returnedType;
251 }
252 }
253
254 return lookupTypeFromImports(fqName);
255}
256
257// Rule 0: try resolve locally
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700258Type* AST::lookupTypeLocally(const FQName& fqName, Scope* scope) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800259 CHECK(fqName.package().empty() && fqName.version().empty()
260 && !fqName.name().empty() && fqName.valueName().empty());
261
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700262 for (; scope != nullptr; scope = scope->parent()) {
263 Type* type = scope->lookupType(fqName);
Yifan Hong87ff8232017-01-09 12:07:05 -0800264
265 if (type != nullptr) {
266 // Resolve typeDefs to the target type.
267 while (type->isTypeDef()) {
268 type = static_cast<TypeDef *>(type)->referencedType();
269 }
270
271 return type;
272 }
273 }
274
275 return nullptr;
276}
277
278// Rule 1: auto-fill with current package
279status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) {
280 CHECK(!fqName.isFullyQualified() && !fqName.name().empty() && fqName.valueName().empty());
281
282 FQName autofilled = fqName;
283 autofilled.applyDefaults(mPackage.package(), mPackage.version());
284 FQName matchingName;
285 // Given this fully-qualified name, the type may be defined in this AST, or other files
286 // in import.
287 Type *local = findDefinedType(autofilled, &matchingName);
288 CHECK(local == nullptr || autofilled == matchingName);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700289 Type* fromImport = lookupType(autofilled, nullptr /* scope */);
Yifan Hong87ff8232017-01-09 12:07:05 -0800290
291 if (local != nullptr && fromImport != nullptr && local != fromImport) {
292 // Something bad happen; two types have the same FQName.
293 std::cerr << "ERROR: Unable to resolve type name '"
294 << fqName.string()
295 << "' (i.e. '"
296 << autofilled.string()
297 << "'), multiple definitions found.\n";
298
299 return UNKNOWN_ERROR;
300 }
301 if (local != nullptr) {
302 *returnedType = local;
303 return OK;
304 }
305 // If fromImport is nullptr as well, return nullptr to fall through to next rule.
306 *returnedType = fromImport;
307 return OK;
308}
309
310// Rule 2: look at imports
311Type *AST::lookupTypeFromImports(const FQName &fqName) {
312
Andreas Huber39fa7182016-08-19 14:27:33 -0700313 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700314 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700315 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700316
Andreas Huber39fa7182016-08-19 14:27:33 -0700317 for (const auto &importedAST : mImportedASTs) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700318 if (mImportedTypes.find(importedAST) != mImportedTypes.end()) {
319 // ignore single type imports
320 continue;
321 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700322 FQName matchingName;
323 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700324
Andreas Huber39fa7182016-08-19 14:27:33 -0700325 if (match != nullptr) {
326 if (resolvedType != nullptr) {
327 std::cerr << "ERROR: Unable to resolve type name '"
328 << fqName.string()
329 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700330
Andreas Huber39fa7182016-08-19 14:27:33 -0700331 std::cerr << " " << resolvedName.string() << "\n";
332 std::cerr << " " << matchingName.string() << "\n";
333
Yifan Hong1977ea32016-10-05 12:49:08 -0700334 return nullptr;
335 }
336
337 resolvedType = match;
338 returnedType = resolvedType;
339 resolvedName = matchingName;
340
341 // Keep going even after finding a match.
342 }
343 }
344
345 for (const auto &pair : mImportedTypes) {
346 AST *importedAST = pair.first;
347 std::set<Type *> importedTypes = pair.second;
348
349 FQName matchingName;
350 Type *match = importedAST->findDefinedType(fqName, &matchingName);
351 if (match != nullptr &&
352 importedTypes.find(match) != importedTypes.end()) {
353 if (resolvedType != nullptr) {
354 std::cerr << "ERROR: Unable to resolve type name '"
355 << fqName.string()
356 << "', multiple matches found:\n";
357
358 std::cerr << " " << resolvedName.string() << "\n";
359 std::cerr << " " << matchingName.string() << "\n";
360
361 return nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700362 }
363
364 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700365 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700366 resolvedName = matchingName;
367
368 // Keep going even after finding a match.
369 }
370 }
371
372 if (resolvedType) {
373#if 0
374 LOG(INFO) << "found '"
375 << resolvedName.string()
376 << "' after looking for '"
377 << fqName.string()
378 << "'.";
379#endif
380
381 // Resolve typeDefs to the target type.
382 while (resolvedType->isTypeDef()) {
383 resolvedType =
384 static_cast<TypeDef *>(resolvedType)->referencedType();
385 }
386
Iliyan Malchev800273d2016-09-02 15:25:07 -0700387 returnedType = resolvedType;
388
389 // If the resolved type is not an interface, we need to determine
390 // whether it is defined in types.hal, or in some other interface. In
391 // the latter case, we need to emit a dependency for the interface in
392 // which the type is defined.
393 //
394 // Consider the following:
395 // android.hardware.tests.foo@1.0::Record
396 // android.hardware.tests.foo@1.0::IFoo.Folder
397 // android.hardware.tests.foo@1.0::Folder
398 //
399 // If Record is an interface, then we keep track of it for the purpose
400 // of emitting dependencies in the target language (for example #include
401 // in C++). If Record is a UDT, then we assume it is defined in
402 // types.hal in android.hardware.tests.foo@1.0.
403 //
404 // In the case of IFoo.Folder, the same applies. If IFoo is an
405 // interface, we need to track this for the purpose of emitting
406 // dependencies. If not, then it must have been defined in types.hal.
407 //
408 // In the case of just specifying Folder, the resolved type is
Yifan Hongfece6ec2017-01-12 17:04:04 -0800409 // android.hardware.tests.foo@1.0::Folder, and the same logic as
Iliyan Malchev800273d2016-09-02 15:25:07 -0700410 // above applies.
411
412 if (!resolvedType->isInterface()) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800413 FQName ifc = resolvedName.getTopLevelType();
Iliyan Malchev800273d2016-09-02 15:25:07 -0700414 for (const auto &importedAST : mImportedASTs) {
415 FQName matchingName;
416 Type *match = importedAST->findDefinedType(ifc, &matchingName);
417 if (match != nullptr && match->isInterface()) {
418 resolvedType = match;
419 }
420 }
421 }
422
Andreas Huber39fa7182016-08-19 14:27:33 -0700423 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700424 // Non-interface types are declared in the associated types header.
Yifan Hongfece6ec2017-01-12 17:04:04 -0800425 FQName typesName = resolvedName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -0700426
Andreas Huber0e00de42016-08-03 09:56:02 -0700427 mImportedNames.insert(typesName);
428 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700429 // Do _not_ use fqName, i.e. the name we used to look up the type,
430 // but instead use the name of the interface we found.
431 // This is necessary because if fqName pointed to a typedef which
432 // in turn referenced the found interface we'd mistakenly use the
433 // name of the typedef instead of the proper name of the interface.
434
435 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700436 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700437 }
Andreas Huber737080b2016-08-02 15:38:04 -0700438 }
439
Steven Morelandbb5c80b2016-10-05 11:07:36 -0700440 return returnedType;
Andreas Huber5345ec22016-07-29 13:33:27 -0700441}
442
Andreas Huber39fa7182016-08-19 14:27:33 -0700443Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700444 for (const auto &pair : mDefinedTypesByFullName) {
445 const FQName &key = pair.first;
446 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700447
Andreas Huber39fa7182016-08-19 14:27:33 -0700448 if (key.endsWith(fqName)) {
449 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700450 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700451 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700452 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700453
454 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700455}
456
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700457void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700458 for (const auto &fqName : mImportedNames) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800459 FQName packageName = fqName.getPackageAndVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700460
461 if (packageName == mPackage) {
462 // We only care about external imports, not our own package.
463 continue;
464 }
465
466 importSet->insert(packageName);
467 }
468}
469
Yifan Hong40a373d2016-11-30 15:16:47 -0800470void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const {
471 getImportedPackages(importSet);
472 std::set<FQName> newSet;
473 for (const auto &ast : mImportedASTs) {
474 if (importSet->find(ast->package()) != importSet->end()) {
475 ast->getImportedPackagesHierarchy(&newSet);
476 }
477 }
478 importSet->insert(newSet.begin(), newSet.end());
479}
480
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800481void AST::getAllImportedNames(std::set<FQName> *allImportNames) const {
482 for (const auto& name : mImportedNames) {
483 allImportNames->insert(name);
Steven Morelandc59326e2017-06-20 15:19:30 -0700484 AST* ast = mCoordinator->parse(name, nullptr /* imported */, Coordinator::Enforce::NONE);
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800485 ast->getAllImportedNames(allImportNames);
486 }
487}
488
Andreas Huber0fa9e392016-08-31 09:05:44 -0700489bool AST::isJavaCompatible() const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700490 if (!AST::isInterface()) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700491 for (const auto* type : mRootScope.getSubTypes()) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700492 if (!type->isJavaCompatible()) {
493 return false;
494 }
495 }
496
497 return true;
498 }
499
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700500 const Interface* iface = mRootScope.getInterface();
Andreas Huber0fa9e392016-08-31 09:05:44 -0700501 return iface->isJavaCompatible();
502}
503
Andreas Huber019d21d2016-10-03 12:59:47 -0700504void AST::appendToExportedTypesVector(
505 std::vector<const Type *> *exportedTypes) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700506 mRootScope.appendToExportedTypesVector(exportedTypes);
Andreas Huber019d21d2016-10-03 12:59:47 -0700507}
508
Yifan Hongc8934042016-11-17 17:10:52 -0800509bool AST::isIBase() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700510 Interface* iface = mRootScope.getInterface();
Yifan Hongc8934042016-11-17 17:10:52 -0800511 return iface != nullptr && iface->isIBase();
512}
513
Yifan Hong78b38d12017-02-13 18:14:46 +0000514const Interface *AST::getInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700515 return mRootScope.getInterface();
Yifan Hong78b38d12017-02-13 18:14:46 +0000516}
517
Steven Moreland19f11b52017-05-12 18:22:21 -0700518std::string AST::getBaseName() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700519 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -0700520
521 return iface ? iface->getBaseName() : "types";
522}
523
Andreas Huberc9410c72016-07-28 12:18:40 -0700524} // namespace android;