blob: 7f245201f0c0b70d32a470639124c0181619a30b [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
Timur Iskhakov35930c42017-08-28 18:49:54 -070093 // Make future packages not to call passes
94 // for processed types and expressions
95 constantExpressionRecursivePass([](ConstantExpression* ce) {
96 ce->setPostParseCompleted();
97 return OK;
98 });
99 std::unordered_set<const Type*> visited;
100 mRootScope.recursivePass(
101 [](Type* type) {
102 type->setPostParseCompleted();
103 return OK;
104 },
105 &visited);
106
Timur Iskhakov33431e62017-08-21 17:31:23 -0700107 return OK;
108}
109
Timur Iskhakov891a8662017-08-25 21:53:48 -0700110status_t AST::constantExpressionRecursivePass(
111 const std::function<status_t(ConstantExpression*)>& func) {
112 std::unordered_set<const Type*> visitedTypes;
113 std::unordered_set<const ConstantExpression*> visitedCE;
114 return mRootScope.recursivePass(
115 [&](Type* type) -> status_t {
116 for (auto* ce : type->getConstantExpressions()) {
117 status_t err = ce->recursivePass(func, &visitedCE);
118 if (err != OK) return err;
119 }
120 return OK;
121 },
122 &visitedTypes);
123}
124
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700125status_t AST::resolveInheritance() {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700126 std::unordered_set<const Type*> visited;
127 return mRootScope.recursivePass(&Type::resolveInheritance, &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700128}
129
130status_t AST::evaluate() {
Timur Iskhakov891a8662017-08-25 21:53:48 -0700131 return constantExpressionRecursivePass([](ConstantExpression* ce) {
132 ce->evaluate();
133 return OK;
134 });
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700135}
136
137status_t AST::validate() const {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700138 std::unordered_set<const Type*> visited;
139 return mRootScope.recursivePass(&Type::validate, &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700140}
141
Andreas Huber5345ec22016-07-29 13:33:27 -0700142bool AST::addImport(const char *import) {
143 FQName fqName(import);
144 CHECK(fqName.isValid());
Andreas Hubereb1081f2016-07-28 13:13:24 -0700145
Andreas Huber5345ec22016-07-29 13:33:27 -0700146 fqName.applyDefaults(mPackage.package(), mPackage.version());
147
Andreas Huber68f24592016-07-29 14:53:48 -0700148 // LOG(INFO) << "importing " << fqName.string();
Andreas Huber5345ec22016-07-29 13:33:27 -0700149
150 if (fqName.name().empty()) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700151 // import a package
Andreas Huberd2943e12016-08-05 11:59:31 -0700152 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -0700153
Andreas Huberd2943e12016-08-05 11:59:31 -0700154 status_t err =
Steven Morelandaa186832016-09-26 13:51:43 -0700155 mCoordinator->appendPackageInterfacesToVector(fqName,
156 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -0700157
158 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700159 return false;
160 }
161
Andreas Huberd2943e12016-08-05 11:59:31 -0700162 for (const auto &subFQName : packageInterfaces) {
Yifan Hongf619fc72017-04-07 15:40:06 -0700163 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700164 AST* ast = mCoordinator->parse(subFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700165 if (ast == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700166 return false;
167 }
Yifan Hong1977ea32016-10-05 12:49:08 -0700168 // all previous single type imports are ignored.
169 mImportedTypes.erase(ast);
Andreas Huber68f24592016-07-29 14:53:48 -0700170 }
171
172 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700173 }
174
Yifan Hong1977ea32016-10-05 12:49:08 -0700175 AST *importAST;
Andreas Huber5345ec22016-07-29 13:33:27 -0700176
Yifan Hong1977ea32016-10-05 12:49:08 -0700177 // cases like android.hardware.foo@1.0::IFoo.Internal
178 // android.hardware.foo@1.0::Abc.Internal
179
180 // assume it is an interface, and try to import it.
181 const FQName interfaceName = fqName.getTopLevelType();
Yifan Hongf619fc72017-04-07 15:40:06 -0700182 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700183 importAST = mCoordinator->parse(interfaceName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700184
185 if (importAST != nullptr) {
186 // cases like android.hardware.foo@1.0::IFoo.Internal
187 // and android.hardware.foo@1.0::IFoo
188 if (fqName == interfaceName) {
189 // import a single file.
190 // all previous single type imports are ignored.
191 // cases like android.hardware.foo@1.0::IFoo
192 // and android.hardware.foo@1.0::types
193 mImportedTypes.erase(importAST);
194 return true;
195 }
196
197 // import a single type from this file
198 // cases like android.hardware.foo@1.0::IFoo.Internal
199 FQName matchingName;
200 Type *match = importAST->findDefinedType(fqName, &matchingName);
201 if (match == nullptr) {
202 return false;
203 }
204 // will automatically create a set if it does not exist
205 mImportedTypes[importAST].insert(match);
206 return true;
Andreas Huber68f24592016-07-29 14:53:48 -0700207 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700208
Yifan Hong1977ea32016-10-05 12:49:08 -0700209 // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal
210 FQName typesFQName = fqName.getTypesForPackage();
Yifan Hongf619fc72017-04-07 15:40:06 -0700211
212 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700213 importAST = mCoordinator->parse(typesFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700214
215 if (importAST != nullptr) {
216 // Attempt to find Abc.Internal in types.
217 FQName matchingName;
218 Type *match = importAST->findDefinedType(fqName, &matchingName);
219 if (match == nullptr) {
220 return false;
221 }
222 // will automatically create a set if not exist
223 mImportedTypes[importAST].insert(match);
224 return true;
225 }
226
227 // can't find an appropriate AST for fqName.
228 return false;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700229}
230
Andreas Huber39fa7182016-08-19 14:27:33 -0700231void AST::addImportedAST(AST *ast) {
232 mImportedASTs.insert(ast);
233}
234
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700235bool AST::addScopedType(NamedType* type, std::string* errorMsg, Scope* scope) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700236 bool success = scope->addType(type, errorMsg);
Andreas Huber5a545442016-08-03 10:44:56 -0700237 if (!success) {
238 return false;
239 }
240
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700241 std::vector<std::string> pathComponents{{type->localName()}};
242 for (; scope != &mRootScope; scope = scope->parent()) {
243 pathComponents.push_back(scope->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700244 }
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700245
246 std::reverse(pathComponents.begin(), pathComponents.end());
247 std::string path = StringHelper::JoinStrings(pathComponents, ".");
Andreas Huber31629bc2016-08-03 09:06:40 -0700248
Andreas Huber31629bc2016-08-03 09:06:40 -0700249 FQName fqName(mPackage.package(), mPackage.version(), path);
Steven Morelandd537ab02016-09-12 10:32:01 -0700250 type->setFullName(fqName);
Andreas Huber39fa7182016-08-19 14:27:33 -0700251
Steven Morelandd537ab02016-09-12 10:32:01 -0700252 mDefinedTypesByFullName[fqName] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700253
Andreas Huber5a545442016-08-03 10:44:56 -0700254 return true;
Andreas Huber31629bc2016-08-03 09:06:40 -0700255}
256
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700257EnumValue* AST::lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700258 FQName enumTypeName = fqName.typeName();
259 std::string enumValueName = fqName.valueName();
260
261 CHECK(enumTypeName.isValid());
262 CHECK(!enumValueName.empty());
263
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700264 Type* type = lookupType(enumTypeName, scope);
Yifan Hongf24fa852016-09-23 11:03:15 -0700265 if(type == nullptr) {
266 *errorMsg = "Cannot find type " + enumTypeName.string();
267 return nullptr;
268 }
269 if(!type->isEnum()) {
270 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
271 return nullptr;
272 }
273
274 EnumType *enumType = static_cast<EnumType *>(type);
275 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
276 if(v == nullptr) {
277 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
278 return nullptr;
279 }
280 return v;
281}
282
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700283Type* AST::lookupType(const FQName& fqName, Scope* scope) {
Andreas Huber84f89de2016-07-28 15:39:51 -0700284 CHECK(fqName.isValid());
285
Andreas Huberda51b8e2016-07-28 16:00:57 -0700286 if (fqName.name().empty()) {
287 // Given a package and version???
Yifan Hong1977ea32016-10-05 12:49:08 -0700288 return nullptr;
Andreas Huberda51b8e2016-07-28 16:00:57 -0700289 }
290
Yifan Hong87ff8232017-01-09 12:07:05 -0800291 Type *returnedType = nullptr;
292
Andreas Huber84f89de2016-07-28 15:39:51 -0700293 if (fqName.package().empty() && fqName.version().empty()) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800294 // resolve locally first if possible.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700295 returnedType = lookupTypeLocally(fqName, scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800296 if (returnedType != nullptr) {
297 return returnedType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700298 }
299 }
300
Yifan Hong87ff8232017-01-09 12:07:05 -0800301 if (!fqName.isFullyQualified()) {
302 status_t status = lookupAutofilledType(fqName, &returnedType);
303 if (status != OK) {
304 return nullptr;
305 }
306 if (returnedType != nullptr) {
307 return returnedType;
308 }
309 }
310
311 return lookupTypeFromImports(fqName);
312}
313
314// Rule 0: try resolve locally
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700315Type* AST::lookupTypeLocally(const FQName& fqName, Scope* scope) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800316 CHECK(fqName.package().empty() && fqName.version().empty()
317 && !fqName.name().empty() && fqName.valueName().empty());
318
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700319 for (; scope != nullptr; scope = scope->parent()) {
320 Type* type = scope->lookupType(fqName);
Yifan Hong87ff8232017-01-09 12:07:05 -0800321
322 if (type != nullptr) {
323 // Resolve typeDefs to the target type.
324 while (type->isTypeDef()) {
325 type = static_cast<TypeDef *>(type)->referencedType();
326 }
327
328 return type;
329 }
330 }
331
332 return nullptr;
333}
334
335// Rule 1: auto-fill with current package
336status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) {
337 CHECK(!fqName.isFullyQualified() && !fqName.name().empty() && fqName.valueName().empty());
338
339 FQName autofilled = fqName;
340 autofilled.applyDefaults(mPackage.package(), mPackage.version());
341 FQName matchingName;
342 // Given this fully-qualified name, the type may be defined in this AST, or other files
343 // in import.
344 Type *local = findDefinedType(autofilled, &matchingName);
345 CHECK(local == nullptr || autofilled == matchingName);
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700346 Type* fromImport = lookupType(autofilled, nullptr /* scope */);
Yifan Hong87ff8232017-01-09 12:07:05 -0800347
348 if (local != nullptr && fromImport != nullptr && local != fromImport) {
349 // Something bad happen; two types have the same FQName.
350 std::cerr << "ERROR: Unable to resolve type name '"
351 << fqName.string()
352 << "' (i.e. '"
353 << autofilled.string()
354 << "'), multiple definitions found.\n";
355
356 return UNKNOWN_ERROR;
357 }
358 if (local != nullptr) {
359 *returnedType = local;
360 return OK;
361 }
362 // If fromImport is nullptr as well, return nullptr to fall through to next rule.
363 *returnedType = fromImport;
364 return OK;
365}
366
367// Rule 2: look at imports
368Type *AST::lookupTypeFromImports(const FQName &fqName) {
369
Andreas Huber39fa7182016-08-19 14:27:33 -0700370 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700371 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700372 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700373
Andreas Huber39fa7182016-08-19 14:27:33 -0700374 for (const auto &importedAST : mImportedASTs) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700375 if (mImportedTypes.find(importedAST) != mImportedTypes.end()) {
376 // ignore single type imports
377 continue;
378 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700379 FQName matchingName;
380 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700381
Andreas Huber39fa7182016-08-19 14:27:33 -0700382 if (match != nullptr) {
383 if (resolvedType != nullptr) {
384 std::cerr << "ERROR: Unable to resolve type name '"
385 << fqName.string()
386 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700387
Andreas Huber39fa7182016-08-19 14:27:33 -0700388 std::cerr << " " << resolvedName.string() << "\n";
389 std::cerr << " " << matchingName.string() << "\n";
390
Yifan Hong1977ea32016-10-05 12:49:08 -0700391 return nullptr;
392 }
393
394 resolvedType = match;
395 returnedType = resolvedType;
396 resolvedName = matchingName;
397
398 // Keep going even after finding a match.
399 }
400 }
401
402 for (const auto &pair : mImportedTypes) {
403 AST *importedAST = pair.first;
404 std::set<Type *> importedTypes = pair.second;
405
406 FQName matchingName;
407 Type *match = importedAST->findDefinedType(fqName, &matchingName);
408 if (match != nullptr &&
409 importedTypes.find(match) != importedTypes.end()) {
410 if (resolvedType != nullptr) {
411 std::cerr << "ERROR: Unable to resolve type name '"
412 << fqName.string()
413 << "', multiple matches found:\n";
414
415 std::cerr << " " << resolvedName.string() << "\n";
416 std::cerr << " " << matchingName.string() << "\n";
417
418 return nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700419 }
420
421 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700422 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700423 resolvedName = matchingName;
424
425 // Keep going even after finding a match.
426 }
427 }
428
429 if (resolvedType) {
430#if 0
431 LOG(INFO) << "found '"
432 << resolvedName.string()
433 << "' after looking for '"
434 << fqName.string()
435 << "'.";
436#endif
437
438 // Resolve typeDefs to the target type.
439 while (resolvedType->isTypeDef()) {
440 resolvedType =
441 static_cast<TypeDef *>(resolvedType)->referencedType();
442 }
443
Iliyan Malchev800273d2016-09-02 15:25:07 -0700444 returnedType = resolvedType;
445
446 // If the resolved type is not an interface, we need to determine
447 // whether it is defined in types.hal, or in some other interface. In
448 // the latter case, we need to emit a dependency for the interface in
449 // which the type is defined.
450 //
451 // Consider the following:
452 // android.hardware.tests.foo@1.0::Record
453 // android.hardware.tests.foo@1.0::IFoo.Folder
454 // android.hardware.tests.foo@1.0::Folder
455 //
456 // If Record is an interface, then we keep track of it for the purpose
457 // of emitting dependencies in the target language (for example #include
458 // in C++). If Record is a UDT, then we assume it is defined in
459 // types.hal in android.hardware.tests.foo@1.0.
460 //
461 // In the case of IFoo.Folder, the same applies. If IFoo is an
462 // interface, we need to track this for the purpose of emitting
463 // dependencies. If not, then it must have been defined in types.hal.
464 //
465 // In the case of just specifying Folder, the resolved type is
Yifan Hongfece6ec2017-01-12 17:04:04 -0800466 // android.hardware.tests.foo@1.0::Folder, and the same logic as
Iliyan Malchev800273d2016-09-02 15:25:07 -0700467 // above applies.
468
469 if (!resolvedType->isInterface()) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800470 FQName ifc = resolvedName.getTopLevelType();
Iliyan Malchev800273d2016-09-02 15:25:07 -0700471 for (const auto &importedAST : mImportedASTs) {
472 FQName matchingName;
473 Type *match = importedAST->findDefinedType(ifc, &matchingName);
474 if (match != nullptr && match->isInterface()) {
475 resolvedType = match;
476 }
477 }
478 }
479
Andreas Huber39fa7182016-08-19 14:27:33 -0700480 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700481 // Non-interface types are declared in the associated types header.
Yifan Hongfece6ec2017-01-12 17:04:04 -0800482 FQName typesName = resolvedName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -0700483
Andreas Huber0e00de42016-08-03 09:56:02 -0700484 mImportedNames.insert(typesName);
485 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700486 // Do _not_ use fqName, i.e. the name we used to look up the type,
487 // but instead use the name of the interface we found.
488 // This is necessary because if fqName pointed to a typedef which
489 // in turn referenced the found interface we'd mistakenly use the
490 // name of the typedef instead of the proper name of the interface.
491
492 mImportedNames.insert(
Andreas Huber39fa7182016-08-19 14:27:33 -0700493 static_cast<Interface *>(resolvedType)->fqName());
Andreas Huber0e00de42016-08-03 09:56:02 -0700494 }
Andreas Huber737080b2016-08-02 15:38:04 -0700495 }
496
Steven Morelandbb5c80b2016-10-05 11:07:36 -0700497 return returnedType;
Andreas Huber5345ec22016-07-29 13:33:27 -0700498}
499
Andreas Huber39fa7182016-08-19 14:27:33 -0700500Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700501 for (const auto &pair : mDefinedTypesByFullName) {
502 const FQName &key = pair.first;
503 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700504
Andreas Huber39fa7182016-08-19 14:27:33 -0700505 if (key.endsWith(fqName)) {
506 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700507 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700508 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700509 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700510
511 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700512}
513
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700514void AST::getImportedPackages(std::set<FQName> *importSet) const {
Andreas Huberd2943e12016-08-05 11:59:31 -0700515 for (const auto &fqName : mImportedNames) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800516 FQName packageName = fqName.getPackageAndVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700517
518 if (packageName == mPackage) {
519 // We only care about external imports, not our own package.
520 continue;
521 }
522
523 importSet->insert(packageName);
524 }
525}
526
Yifan Hong40a373d2016-11-30 15:16:47 -0800527void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const {
528 getImportedPackages(importSet);
529 std::set<FQName> newSet;
530 for (const auto &ast : mImportedASTs) {
531 if (importSet->find(ast->package()) != importSet->end()) {
532 ast->getImportedPackagesHierarchy(&newSet);
533 }
534 }
535 importSet->insert(newSet.begin(), newSet.end());
536}
537
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800538void AST::getAllImportedNames(std::set<FQName> *allImportNames) const {
539 for (const auto& name : mImportedNames) {
540 allImportNames->insert(name);
Steven Morelandc59326e2017-06-20 15:19:30 -0700541 AST* ast = mCoordinator->parse(name, nullptr /* imported */, Coordinator::Enforce::NONE);
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800542 ast->getAllImportedNames(allImportNames);
543 }
544}
545
Andreas Huber0fa9e392016-08-31 09:05:44 -0700546bool AST::isJavaCompatible() const {
Steven Moreland19f11b52017-05-12 18:22:21 -0700547 if (!AST::isInterface()) {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700548 for (const auto* type : mRootScope.getSubTypes()) {
Andreas Huber0fa9e392016-08-31 09:05:44 -0700549 if (!type->isJavaCompatible()) {
550 return false;
551 }
552 }
553
554 return true;
555 }
556
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700557 const Interface* iface = mRootScope.getInterface();
Andreas Huber0fa9e392016-08-31 09:05:44 -0700558 return iface->isJavaCompatible();
559}
560
Andreas Huber019d21d2016-10-03 12:59:47 -0700561void AST::appendToExportedTypesVector(
562 std::vector<const Type *> *exportedTypes) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700563 mRootScope.appendToExportedTypesVector(exportedTypes);
Andreas Huber019d21d2016-10-03 12:59:47 -0700564}
565
Yifan Hongc8934042016-11-17 17:10:52 -0800566bool AST::isIBase() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700567 Interface* iface = mRootScope.getInterface();
Yifan Hongc8934042016-11-17 17:10:52 -0800568 return iface != nullptr && iface->isIBase();
569}
570
Yifan Hong78b38d12017-02-13 18:14:46 +0000571const Interface *AST::getInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700572 return mRootScope.getInterface();
Yifan Hong78b38d12017-02-13 18:14:46 +0000573}
574
Steven Moreland19f11b52017-05-12 18:22:21 -0700575std::string AST::getBaseName() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700576 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -0700577
578 return iface ? iface->getBaseName() : "types";
579}
580
Andreas Huberc9410c72016-07-28 12:18:40 -0700581} // namespace android;