blob: ab0e38650b8d68790b9ebc765de0590ac0253d5b [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
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070028#include <hidl-util/Formatter.h>
Steven Moreland7ae3d542017-01-18 16:46:01 -080029#include <hidl-util/FQName.h>
Andreas Hubereb1081f2016-07-28 13:13:24 -070030#include <android-base/logging.h>
Andreas Huber39fa7182016-08-19 14:27:33 -070031#include <iostream>
Andreas Huberc9410c72016-07-28 12:18:40 -070032#include <stdlib.h>
33
Andreas Huberc9410c72016-07-28 12:18:40 -070034namespace android {
35
Andreas Huber0d0f9a22016-08-17 10:26:11 -070036AST::AST(Coordinator *coordinator, const std::string &path)
Andreas Huber5345ec22016-07-29 13:33:27 -070037 : mCoordinator(coordinator),
Andreas Huber0d0f9a22016-08-17 10:26:11 -070038 mPath(path),
Andreas Huber5345ec22016-07-29 13:33:27 -070039 mScanner(NULL),
Yifan Honga4b53d02016-10-31 17:29:10 -070040 mRootScope(new Scope("" /* localName */, Location::startOf(path))) {
Andreas Huberc9410c72016-07-28 12:18:40 -070041 enterScope(mRootScope);
42}
43
44AST::~AST() {
Andreas Huberc9410c72016-07-28 12:18:40 -070045 delete mRootScope;
Yifan Hong1977ea32016-10-05 12:49:08 -070046 mRootScope = nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -070047
Andreas Hubereb1081f2016-07-28 13:13:24 -070048 CHECK(mScanner == NULL);
Andreas Huberc9410c72016-07-28 12:18:40 -070049
Andreas Huber5345ec22016-07-29 13:33:27 -070050 // Ownership of "coordinator" was NOT transferred.
Andreas Huberc9410c72016-07-28 12:18:40 -070051}
52
Yifan Hongbe627b32016-10-28 18:38:56 -070053// used by the parser.
54void AST::addSyntaxError() {
55 mSyntaxErrors++;
56}
57
58size_t AST::syntaxErrors() const {
59 return mSyntaxErrors;
60}
61
Andreas Huberc9410c72016-07-28 12:18:40 -070062void *AST::scanner() {
63 return mScanner;
64}
65
66void AST::setScanner(void *scanner) {
67 mScanner = scanner;
68}
69
Andreas Huber0d0f9a22016-08-17 10:26:11 -070070const std::string &AST::getFilename() const {
71 return mPath;
72}
73
Andreas Huber84f89de2016-07-28 15:39:51 -070074bool AST::setPackage(const char *package) {
Andreas Huberda51b8e2016-07-28 16:00:57 -070075 mPackage.setTo(package);
76 CHECK(mPackage.isValid());
Andreas Huber84f89de2016-07-28 15:39:51 -070077
Andreas Huberda51b8e2016-07-28 16:00:57 -070078 if (mPackage.package().empty()
79 || mPackage.version().empty()
80 || !mPackage.name().empty()) {
Andreas Huber84f89de2016-07-28 15:39:51 -070081 return false;
82 }
83
Andreas Huber84f89de2016-07-28 15:39:51 -070084 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070085}
86
Andreas Hubera2723d22016-07-29 15:36:07 -070087FQName AST::package() const {
88 return mPackage;
89}
90
91bool AST::isInterface(std::string *ifaceName) const {
92 return mRootScope->containsSingleInterface(ifaceName);
93}
94
Andreas Huber7c5ddfb2016-09-29 13:45:22 -070095bool AST::containsInterfaces() const {
96 return mRootScope->containsInterfaces();
97}
98
Andreas Huber5345ec22016-07-29 13:33:27 -070099bool AST::addImport(const char *import) {
100 FQName fqName(import);
101 CHECK(fqName.isValid());
Andreas Hubereb1081f2016-07-28 13:13:24 -0700102
Andreas Huber5345ec22016-07-29 13:33:27 -0700103 fqName.applyDefaults(mPackage.package(), mPackage.version());
104
Andreas Huber68f24592016-07-29 14:53:48 -0700105 // LOG(INFO) << "importing " << fqName.string();
Andreas Huber5345ec22016-07-29 13:33:27 -0700106
107 if (fqName.name().empty()) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700108 // import a package
Andreas Huberd2943e12016-08-05 11:59:31 -0700109 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -0700110
Andreas Huberd2943e12016-08-05 11:59:31 -0700111 status_t err =
Steven Morelandaa186832016-09-26 13:51:43 -0700112 mCoordinator->appendPackageInterfacesToVector(fqName,
113 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -0700114
115 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700116 return false;
117 }
118
Andreas Huberd2943e12016-08-05 11:59:31 -0700119 for (const auto &subFQName : packageInterfaces) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700120 AST *ast = mCoordinator->parse(subFQName, &mImportedASTs);
Yifan Hong1977ea32016-10-05 12:49:08 -0700121 if (ast == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700122 return false;
123 }
Yifan Hong1977ea32016-10-05 12:49:08 -0700124 // all previous single type imports are ignored.
125 mImportedTypes.erase(ast);
Andreas Huber68f24592016-07-29 14:53:48 -0700126 }
127
128 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700129 }
130
Yifan Hong1977ea32016-10-05 12:49:08 -0700131 AST *importAST;
Andreas Huber5345ec22016-07-29 13:33:27 -0700132
Yifan Hong1977ea32016-10-05 12:49:08 -0700133 // cases like android.hardware.foo@1.0::IFoo.Internal
134 // android.hardware.foo@1.0::Abc.Internal
135
136 // assume it is an interface, and try to import it.
137 const FQName interfaceName = fqName.getTopLevelType();
138 importAST = mCoordinator->parse(interfaceName, &mImportedASTs);
139
140 if (importAST != nullptr) {
141 // cases like android.hardware.foo@1.0::IFoo.Internal
142 // and android.hardware.foo@1.0::IFoo
143 if (fqName == interfaceName) {
144 // import a single file.
145 // all previous single type imports are ignored.
146 // cases like android.hardware.foo@1.0::IFoo
147 // and android.hardware.foo@1.0::types
148 mImportedTypes.erase(importAST);
149 return true;
150 }
151
152 // import a single type from this file
153 // cases like android.hardware.foo@1.0::IFoo.Internal
154 FQName matchingName;
155 Type *match = importAST->findDefinedType(fqName, &matchingName);
156 if (match == nullptr) {
157 return false;
158 }
159 // will automatically create a set if it does not exist
160 mImportedTypes[importAST].insert(match);
161 return true;
Andreas Huber68f24592016-07-29 14:53:48 -0700162 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700163
Yifan Hong1977ea32016-10-05 12:49:08 -0700164 // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal
165 FQName typesFQName = fqName.getTypesForPackage();
166 importAST = mCoordinator->parse(typesFQName, &mImportedASTs);
167
168 if (importAST != nullptr) {
169 // Attempt to find Abc.Internal in types.
170 FQName matchingName;
171 Type *match = importAST->findDefinedType(fqName, &matchingName);
172 if (match == nullptr) {
173 return false;
174 }
175 // will automatically create a set if not exist
176 mImportedTypes[importAST].insert(match);
177 return true;
178 }
179
180 // can't find an appropriate AST for fqName.
181 return false;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700182}
183
Andreas Huber39fa7182016-08-19 14:27:33 -0700184void AST::addImportedAST(AST *ast) {
185 mImportedASTs.insert(ast);
186}
187
Andreas Huberc9410c72016-07-28 12:18:40 -0700188void AST::enterScope(Scope *container) {
189 mScopePath.push_back(container);
190}
191
192void AST::leaveScope() {
Steven Morelandd537ab02016-09-12 10:32:01 -0700193 mScopePath.pop_back();
Andreas Huberc9410c72016-07-28 12:18:40 -0700194}
195
196Scope *AST::scope() {
Andreas Hubereb1081f2016-07-28 13:13:24 -0700197 CHECK(!mScopePath.empty());
Steven Morelandd537ab02016-09-12 10:32:01 -0700198 return mScopePath.back();
Andreas Huberc9410c72016-07-28 12:18:40 -0700199}
200
Yifan Honga4b53d02016-10-31 17:29:10 -0700201bool AST::addTypeDef(const char *localName, Type *type, const Location &location,
202 std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700203 // The reason we wrap the given type in a TypeDef is simply to suppress
204 // emitting any type definitions later on, since this is just an alias
205 // to a type defined elsewhere.
206 return addScopedTypeInternal(
Yifan Honga4b53d02016-10-31 17:29:10 -0700207 new TypeDef(localName, location, type), errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700208}
209
Andreas Huber9ed827c2016-08-22 12:31:13 -0700210bool AST::addScopedType(NamedType *type, std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700211 return addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700212 type, errorMsg);
Andreas Huber39fa7182016-08-19 14:27:33 -0700213}
214
215bool AST::addScopedTypeInternal(
Steven Morelandd537ab02016-09-12 10:32:01 -0700216 NamedType *type,
217 std::string *errorMsg) {
Andreas Huber39fa7182016-08-19 14:27:33 -0700218
Steven Morelandd537ab02016-09-12 10:32:01 -0700219 bool success = scope()->addType(type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700220 if (!success) {
221 return false;
222 }
223
Andreas Huber31629bc2016-08-03 09:06:40 -0700224 std::string path;
225 for (size_t i = 1; i < mScopePath.size(); ++i) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700226 path.append(mScopePath[i]->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700227 path.append(".");
228 }
Steven Morelandd537ab02016-09-12 10:32:01 -0700229 path.append(type->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700230
Andreas Huber31629bc2016-08-03 09:06:40 -0700231 FQName fqName(mPackage.package(), mPackage.version(), path);
Andreas Huber39fa7182016-08-19 14:27:33 -0700232
Steven Morelandd537ab02016-09-12 10:32:01 -0700233 type->setFullName(fqName);
Andreas Huber39fa7182016-08-19 14:27:33 -0700234
Steven Morelandd537ab02016-09-12 10:32:01 -0700235 mDefinedTypesByFullName[fqName] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700236
Andreas Huber5a545442016-08-03 10:44:56 -0700237 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700238}
239
Yifan Hongf24fa852016-09-23 11:03:15 -0700240EnumValue *AST::lookupEnumValue(const FQName &fqName, std::string *errorMsg) {
241
242 FQName enumTypeName = fqName.typeName();
243 std::string enumValueName = fqName.valueName();
244
245 CHECK(enumTypeName.isValid());
246 CHECK(!enumValueName.empty());
247
248 Type *type = lookupType(enumTypeName);
249 if(type == nullptr) {
250 *errorMsg = "Cannot find type " + enumTypeName.string();
251 return nullptr;
252 }
253 if(!type->isEnum()) {
254 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
255 return nullptr;
256 }
257
258 EnumType *enumType = static_cast<EnumType *>(type);
259 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
260 if(v == nullptr) {
261 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
262 return nullptr;
263 }
264 return v;
265}
266
Yifan Hongae16eed2016-09-23 13:25:25 -0700267Type *AST::lookupType(const FQName &fqName) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700268 CHECK(fqName.isValid());
269
Andreas Huberda51b8e2016-07-28 16:00:57 -0700270 if (fqName.name().empty()) {
271 // Given a package and version???
Yifan Hong1977ea32016-10-05 12:49:08 -0700272 return nullptr;
Andreas Huberda51b8e2016-07-28 16:00:57 -0700273 }
274
Yifan Hong87ff8232017-01-09 12:07:05 -0800275 Type *returnedType = nullptr;
276
Andreas Huber84f89de2016-07-28 15:39:51 -0700277 if (fqName.package().empty() && fqName.version().empty()) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800278 // resolve locally first if possible.
279 returnedType = lookupTypeLocally(fqName);
280 if (returnedType != nullptr) {
281 return returnedType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700282 }
283 }
284
Yifan Hong87ff8232017-01-09 12:07:05 -0800285 if (!fqName.isFullyQualified()) {
286 status_t status = lookupAutofilledType(fqName, &returnedType);
287 if (status != OK) {
288 return nullptr;
289 }
290 if (returnedType != nullptr) {
291 return returnedType;
292 }
293 }
294
295 return lookupTypeFromImports(fqName);
296}
297
298// Rule 0: try resolve locally
299Type *AST::lookupTypeLocally(const FQName &fqName) {
300 CHECK(fqName.package().empty() && fqName.version().empty()
301 && !fqName.name().empty() && fqName.valueName().empty());
302
303 for (size_t i = mScopePath.size(); i-- > 0;) {
304 Type *type = mScopePath[i]->lookupType(fqName);
305
306 if (type != nullptr) {
307 // Resolve typeDefs to the target type.
308 while (type->isTypeDef()) {
309 type = static_cast<TypeDef *>(type)->referencedType();
310 }
311
312 return type;
313 }
314 }
315
316 return nullptr;
317}
318
319// Rule 1: auto-fill with current package
320status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) {
321 CHECK(!fqName.isFullyQualified() && !fqName.name().empty() && fqName.valueName().empty());
322
323 FQName autofilled = fqName;
324 autofilled.applyDefaults(mPackage.package(), mPackage.version());
325 FQName matchingName;
326 // Given this fully-qualified name, the type may be defined in this AST, or other files
327 // in import.
328 Type *local = findDefinedType(autofilled, &matchingName);
329 CHECK(local == nullptr || autofilled == matchingName);
330 Type *fromImport = lookupType(autofilled);
331
332 if (local != nullptr && fromImport != nullptr && local != fromImport) {
333 // Something bad happen; two types have the same FQName.
334 std::cerr << "ERROR: Unable to resolve type name '"
335 << fqName.string()
336 << "' (i.e. '"
337 << autofilled.string()
338 << "'), multiple definitions found.\n";
339
340 return UNKNOWN_ERROR;
341 }
342 if (local != nullptr) {
343 *returnedType = local;
344 return OK;
345 }
346 // If fromImport is nullptr as well, return nullptr to fall through to next rule.
347 *returnedType = fromImport;
348 return OK;
349}
350
351// Rule 2: look at imports
352Type *AST::lookupTypeFromImports(const FQName &fqName) {
353
Andreas Huber39fa7182016-08-19 14:27:33 -0700354 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700355 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700356 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700357
Andreas Huber39fa7182016-08-19 14:27:33 -0700358 for (const auto &importedAST : mImportedASTs) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700359 if (mImportedTypes.find(importedAST) != mImportedTypes.end()) {
360 // ignore single type imports
361 continue;
362 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700363 FQName matchingName;
364 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700365
Andreas Huber39fa7182016-08-19 14:27:33 -0700366 if (match != nullptr) {
367 if (resolvedType != nullptr) {
368 std::cerr << "ERROR: Unable to resolve type name '"
369 << fqName.string()
370 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700371
Andreas Huber39fa7182016-08-19 14:27:33 -0700372 std::cerr << " " << resolvedName.string() << "\n";
373 std::cerr << " " << matchingName.string() << "\n";
374
Yifan Hong1977ea32016-10-05 12:49:08 -0700375 return nullptr;
376 }
377
378 resolvedType = match;
379 returnedType = resolvedType;
380 resolvedName = matchingName;
381
382 // Keep going even after finding a match.
383 }
384 }
385
386 for (const auto &pair : mImportedTypes) {
387 AST *importedAST = pair.first;
388 std::set<Type *> importedTypes = pair.second;
389
390 FQName matchingName;
391 Type *match = importedAST->findDefinedType(fqName, &matchingName);
392 if (match != nullptr &&
393 importedTypes.find(match) != importedTypes.end()) {
394 if (resolvedType != nullptr) {
395 std::cerr << "ERROR: Unable to resolve type name '"
396 << fqName.string()
397 << "', multiple matches found:\n";
398
399 std::cerr << " " << resolvedName.string() << "\n";
400 std::cerr << " " << matchingName.string() << "\n";
401
402 return nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700403 }
404
405 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700406 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700407 resolvedName = matchingName;
408
409 // Keep going even after finding a match.
410 }
411 }
412
413 if (resolvedType) {
414#if 0
415 LOG(INFO) << "found '"
416 << resolvedName.string()
417 << "' after looking for '"
418 << fqName.string()
419 << "'.";
420#endif
421
422 // Resolve typeDefs to the target type.
423 while (resolvedType->isTypeDef()) {
424 resolvedType =
425 static_cast<TypeDef *>(resolvedType)->referencedType();
426 }
427
Iliyan Malchev800273d2016-09-02 15:25:07 -0700428 returnedType = resolvedType;
429
430 // If the resolved type is not an interface, we need to determine
431 // whether it is defined in types.hal, or in some other interface. In
432 // the latter case, we need to emit a dependency for the interface in
433 // which the type is defined.
434 //
435 // Consider the following:
436 // android.hardware.tests.foo@1.0::Record
437 // android.hardware.tests.foo@1.0::IFoo.Folder
438 // android.hardware.tests.foo@1.0::Folder
439 //
440 // If Record is an interface, then we keep track of it for the purpose
441 // of emitting dependencies in the target language (for example #include
442 // in C++). If Record is a UDT, then we assume it is defined in
443 // types.hal in android.hardware.tests.foo@1.0.
444 //
445 // In the case of IFoo.Folder, the same applies. If IFoo is an
446 // interface, we need to track this for the purpose of emitting
447 // dependencies. If not, then it must have been defined in types.hal.
448 //
449 // In the case of just specifying Folder, the resolved type is
Yifan Hongfece6ec2017-01-12 17:04:04 -0800450 // android.hardware.tests.foo@1.0::Folder, and the same logic as
Iliyan Malchev800273d2016-09-02 15:25:07 -0700451 // above applies.
452
453 if (!resolvedType->isInterface()) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800454 FQName ifc = resolvedName.getTopLevelType();
Iliyan Malchev800273d2016-09-02 15:25:07 -0700455 for (const auto &importedAST : mImportedASTs) {
456 FQName matchingName;
457 Type *match = importedAST->findDefinedType(ifc, &matchingName);
458 if (match != nullptr && match->isInterface()) {
459 resolvedType = match;
460 }
461 }
462 }
463
Andreas Huber39fa7182016-08-19 14:27:33 -0700464 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700465 // Non-interface types are declared in the associated types header.
Yifan Hongfece6ec2017-01-12 17:04:04 -0800466 FQName typesName = resolvedName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -0700467
Andreas Huber0e00de42016-08-03 09:56:02 -0700468 mImportedNames.insert(typesName);
469 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700470 // Do _not_ use fqName, i.e. the name we used to look up the type,
471 // but instead use the name of the interface we found.
472 // This is necessary because if fqName pointed to a typedef which
473 // in turn referenced the found interface we'd mistakenly use the
474 // name of the typedef instead of the proper name of the interface.
475
476 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700477 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700478 }
Andreas Huber737080b2016-08-02 15:38:04 -0700479 }
480
Steven Morelandbb5c80b2016-10-05 11:07:36 -0700481 return returnedType;
Andreas Huber5345ec22016-07-29 13:33:27 -0700482}
483
Andreas Huber39fa7182016-08-19 14:27:33 -0700484Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700485 for (const auto &pair : mDefinedTypesByFullName) {
486 const FQName &key = pair.first;
487 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700488
Andreas Huber39fa7182016-08-19 14:27:33 -0700489 if (key.endsWith(fqName)) {
490 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700491 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700492 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700493 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700494
495 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700496}
497
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700498void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700499 for (const auto &fqName : mImportedNames) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800500 FQName packageName = fqName.getPackageAndVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700501
502 if (packageName == mPackage) {
503 // We only care about external imports, not our own package.
504 continue;
505 }
506
507 importSet->insert(packageName);
508 }
509}
510
Yifan Hong40a373d2016-11-30 15:16:47 -0800511void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const {
512 getImportedPackages(importSet);
513 std::set<FQName> newSet;
514 for (const auto &ast : mImportedASTs) {
515 if (importSet->find(ast->package()) != importSet->end()) {
516 ast->getImportedPackagesHierarchy(&newSet);
517 }
518 }
519 importSet->insert(newSet.begin(), newSet.end());
520}
521
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800522void AST::getAllImportedNames(std::set<FQName> *allImportNames) const {
523 for (const auto& name : mImportedNames) {
524 allImportNames->insert(name);
525 AST *ast = mCoordinator->parse(name);
526 ast->getAllImportedNames(allImportNames);
527 }
528}
529
Andreas Huber0fa9e392016-08-31 09:05:44 -0700530bool AST::isJavaCompatible() const {
531 std::string ifaceName;
532 if (!AST::isInterface(&ifaceName)) {
Steven Morelandd537ab02016-09-12 10:32:01 -0700533 for (const auto *type : mRootScope->getSubTypes()) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700534 if (!type->isJavaCompatible()) {
535 return false;
536 }
537 }
538
539 return true;
540 }
541
542 const Interface *iface = mRootScope->getInterface();
543 return iface->isJavaCompatible();
544}
545
Andreas Huber019d21d2016-10-03 12:59:47 -0700546void AST::appendToExportedTypesVector(
547 std::vector<const Type *> *exportedTypes) const {
548 mRootScope->appendToExportedTypesVector(exportedTypes);
549}
550
Yifan Hongc8934042016-11-17 17:10:52 -0800551bool AST::isIBase() const {
552 Interface *iface = mRootScope->getInterface();
553 return iface != nullptr && iface->isIBase();
554}
555
Andreas Huberc9410c72016-07-28 12:18:40 -0700556} // namespace android;