blob: cd4b4dd2c40fea4c288d7f92ca6cc428e6fb727f [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"
Neel Mehta0ee353f2019-05-30 17:40:29 -070025#include "Method.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070026#include "Scope.h"
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070027#include "TypeDef.h"
Andreas Huberc9410c72016-07-28 12:18:40 -070028
Andreas Hubereb1081f2016-07-28 13:13:24 -070029#include <android-base/logging.h>
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070030#include <hidl-util/FQName.h>
31#include <hidl-util/Formatter.h>
32#include <hidl-util/StringHelper.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070033#include <stdlib.h>
Neel Mehta0ee353f2019-05-30 17:40:29 -070034
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070035#include <algorithm>
36#include <iostream>
Neel Mehta0ee353f2019-05-30 17:40:29 -070037#include <map>
38#include <string>
Andreas Huberc9410c72016-07-28 12:18:40 -070039
Andreas Huberc9410c72016-07-28 12:18:40 -070040namespace android {
41
Steven Moreland04dea8d2018-02-06 13:11:24 -080042AST::AST(const Coordinator* coordinator, const Hash* fileHash)
Andreas Huber5345ec22016-07-29 13:33:27 -070043 : mCoordinator(coordinator),
Steven Moreland04dea8d2018-02-06 13:11:24 -080044 mFileHash(fileHash),
Neel Mehtaf6293d32019-06-12 17:16:38 -070045 mRootScope("(root scope)", FQName(),
46 Location::startOf(coordinator->makeRelative(fileHash->getPath())),
Steven Moreland04dea8d2018-02-06 13:11:24 -080047 nullptr /* parent */) {}
Andreas Huberc9410c72016-07-28 12:18:40 -070048
Neel Mehta693169b2019-05-29 18:45:25 -070049Scope* AST::getMutableRootScope() {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070050 return &mRootScope;
Andreas Huberc9410c72016-07-28 12:18:40 -070051}
52
Neel Mehta693169b2019-05-29 18:45:25 -070053const Scope& AST::getRootScope() const {
54 return mRootScope;
55}
56
Yifan Hongbe627b32016-10-28 18:38:56 -070057// used by the parser.
58void AST::addSyntaxError() {
59 mSyntaxErrors++;
60}
61
62size_t AST::syntaxErrors() const {
63 return mSyntaxErrors;
64}
65
Steven Moreland04dea8d2018-02-06 13:11:24 -080066const std::string& AST::getFilename() const {
67 return mFileHash->getPath();
68}
69const Hash* AST::getFileHash() const {
70 return mFileHash;
Andreas Huber0d0f9a22016-08-17 10:26:11 -070071}
72
Neel Mehtaf6293d32019-06-12 17:16:38 -070073const Coordinator& AST::getCoordinator() const {
74 return *mCoordinator;
75}
76
Andreas Huber84f89de2016-07-28 15:39:51 -070077bool AST::setPackage(const char *package) {
Steven Morelande1b157e2018-03-06 14:18:32 -080078 if (!mPackage.setTo(package)) {
79 return false;
80 }
Andreas Huber84f89de2016-07-28 15:39:51 -070081
Andreas Huberda51b8e2016-07-28 16:00:57 -070082 if (mPackage.package().empty()
83 || mPackage.version().empty()
84 || !mPackage.name().empty()) {
Andreas Huber84f89de2016-07-28 15:39:51 -070085 return false;
86 }
87
Andreas Huber84f89de2016-07-28 15:39:51 -070088 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070089}
90
Andreas Hubera2723d22016-07-29 15:36:07 -070091FQName AST::package() const {
92 return mPackage;
93}
94
Steven Moreland19f11b52017-05-12 18:22:21 -070095bool AST::isInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -070096 return mRootScope.getInterface() != nullptr;
Andreas Hubera2723d22016-07-29 15:36:07 -070097}
98
Steven Morelandb47a2622018-07-11 09:04:25 -070099bool AST::definesInterfaces() const {
100 return mRootScope.definesInterfaces();
Andreas Huber7c5ddfb2016-09-29 13:45:22 -0700101}
102
Timur Iskhakov33431e62017-08-21 17:31:23 -0700103status_t AST::postParse() {
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700104 status_t err;
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700105
Yifan Hong0e192c42018-10-23 15:32:19 -0700106 // lookupTypes is the first pass for references to be resolved.
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700107 err = lookupTypes();
108 if (err != OK) return err;
Yifan Hong0e192c42018-10-23 15:32:19 -0700109
110 // Indicate that all types are now in "postParse" stage.
111 err = setParseStage(Type::ParseStage::PARSE, Type::ParseStage::POST_PARSE);
112 if (err != OK) return err;
113
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700114 // validateDefinedTypesUniqueNames is the first call
115 // after lookup, as other errors could appear because
116 // user meant different type than we assumed.
Timur Iskhakov565b0132017-09-06 18:07:11 -0700117 err = validateDefinedTypesUniqueNames();
118 if (err != OK) return err;
Timur Iskhakov458ca362017-09-12 23:16:03 -0700119 // topologicalReorder is before resolveInheritance, as we
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700120 // need to have no cycle while getting parent class.
Timur Iskhakov458ca362017-09-12 23:16:03 -0700121 err = topologicalReorder();
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700122 if (err != OK) return err;
123 err = resolveInheritance();
124 if (err != OK) return err;
Steven Moreland12f0ab12018-11-02 17:27:37 -0700125 err = lookupConstantExpressions();
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700126 if (err != OK) return err;
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700127 // checkAcyclicConstantExpressions is after resolveInheritance,
128 // as resolveInheritance autofills enum values.
129 err = checkAcyclicConstantExpressions();
Timur Iskhakov33431e62017-08-21 17:31:23 -0700130 if (err != OK) return err;
Steven Moreland12f0ab12018-11-02 17:27:37 -0700131 err = validateConstantExpressions();
132 if (err != OK) return err;
133 err = evaluateConstantExpressions();
Timur Iskhakov33431e62017-08-21 17:31:23 -0700134 if (err != OK) return err;
135 err = validate();
136 if (err != OK) return err;
Timur Iskhakov041fdfe2017-09-06 15:56:01 -0700137 err = checkForwardReferenceRestrictions();
138 if (err != OK) return err;
Andreas Huber308d8a22017-11-06 14:46:52 -0800139 err = gatherReferencedTypes();
140 if (err != OK) return err;
Timur Iskhakov33431e62017-08-21 17:31:23 -0700141
Timur Iskhakov35930c42017-08-28 18:49:54 -0700142 // Make future packages not to call passes
143 // for processed types and expressions
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700144 constantExpressionRecursivePass(
145 [](ConstantExpression* ce) {
146 ce->setPostParseCompleted();
147 return OK;
148 },
149 true /* processBeforeDependencies */);
Yifan Hong0e192c42018-10-23 15:32:19 -0700150
151 err = setParseStage(Type::ParseStage::POST_PARSE, Type::ParseStage::COMPLETED);
152 if (err != OK) return err;
Timur Iskhakov35930c42017-08-28 18:49:54 -0700153
Timur Iskhakov33431e62017-08-21 17:31:23 -0700154 return OK;
155}
156
Timur Iskhakov891a8662017-08-25 21:53:48 -0700157status_t AST::constantExpressionRecursivePass(
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700158 const std::function<status_t(ConstantExpression*)>& func, bool processBeforeDependencies) {
Timur Iskhakov891a8662017-08-25 21:53:48 -0700159 std::unordered_set<const Type*> visitedTypes;
160 std::unordered_set<const ConstantExpression*> visitedCE;
Yifan Hong0e192c42018-10-23 15:32:19 -0700161 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
162 [&](Type* type) -> status_t {
163 for (auto* ce : type->getConstantExpressions()) {
164 status_t err = ce->recursivePass(
165 func, &visitedCE, processBeforeDependencies);
166 if (err != OK) return err;
167 }
168 return OK;
169 },
170 &visitedTypes);
171}
172
Steven Moreland12f0ab12018-11-02 17:27:37 -0700173status_t AST::constantExpressionRecursivePass(
174 const std::function<status_t(const ConstantExpression*)>& func,
175 bool processBeforeDependencies) const {
176 std::unordered_set<const Type*> visitedTypes;
177 std::unordered_set<const ConstantExpression*> visitedCE;
178 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
179 [&](const Type* type) -> status_t {
180 for (auto* ce : type->getConstantExpressions()) {
181 status_t err = ce->recursivePass(
182 func, &visitedCE, processBeforeDependencies);
183 if (err != OK) return err;
184 }
185 return OK;
186 },
187 &visitedTypes);
188}
189
Yifan Hong0e192c42018-10-23 15:32:19 -0700190status_t AST::setParseStage(Type::ParseStage oldStage, Type::ParseStage newStage) {
191 std::unordered_set<const Type*> visited;
192 return mRootScope.recursivePass(oldStage,
193 [oldStage, newStage](Type* type) {
194 CHECK(type->getParseStage() == oldStage);
195 type->setParseStage(newStage);
196 return OK;
197 },
198 &visited);
Timur Iskhakov891a8662017-08-25 21:53:48 -0700199}
200
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700201status_t AST::lookupTypes() {
202 std::unordered_set<const Type*> visited;
203 return mRootScope.recursivePass(
Yifan Hong0e192c42018-10-23 15:32:19 -0700204 Type::ParseStage::PARSE,
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700205 [&](Type* type) -> status_t {
206 Scope* scope = type->isScope() ? static_cast<Scope*>(type) : type->parent();
207
208 for (auto* nextRef : type->getReferences()) {
Andreas Huber308d8a22017-11-06 14:46:52 -0800209 if (nextRef->isResolved()) {
210 continue;
211 }
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700212
213 Type* nextType = lookupType(nextRef->getLookupFqName(), scope);
214 if (nextType == nullptr) {
215 std::cerr << "ERROR: Failed to lookup type '"
216 << nextRef->getLookupFqName().string() << "' at "
Steven Morelandfebfc0c2019-12-12 16:32:48 -0800217 << nextRef->location()
218 << " (is it imported and spelled correctly?)\n";
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700219 return UNKNOWN_ERROR;
220 }
221 nextRef->set(nextType);
222 }
223
224 return OK;
225 },
226 &visited);
227}
228
Andreas Huber308d8a22017-11-06 14:46:52 -0800229status_t AST::gatherReferencedTypes() {
230 std::unordered_set<const Type*> visited;
231 return mRootScope.recursivePass(
Yifan Hong0e192c42018-10-23 15:32:19 -0700232 Type::ParseStage::POST_PARSE,
Andreas Huber308d8a22017-11-06 14:46:52 -0800233 [&](Type* type) -> status_t {
234 for (auto* nextRef : type->getReferences()) {
235 const Type *targetType = nextRef->get();
236 if (targetType->isNamedType()) {
237 mReferencedTypeNames.insert(
238 static_cast<const NamedType *>(targetType)->fqName());
239 }
240 }
241
242 return OK;
243 },
244 &visited);
245}
246
Steven Moreland12f0ab12018-11-02 17:27:37 -0700247status_t AST::lookupConstantExpressions() {
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700248 std::unordered_set<const Type*> visitedTypes;
249 std::unordered_set<const ConstantExpression*> visitedCE;
250
251 return mRootScope.recursivePass(
Yifan Hong0e192c42018-10-23 15:32:19 -0700252 Type::ParseStage::POST_PARSE,
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700253 [&](Type* type) -> status_t {
254 Scope* scope = type->isScope() ? static_cast<Scope*>(type) : type->parent();
255
256 for (auto* ce : type->getConstantExpressions()) {
257 status_t err = ce->recursivePass(
258 [&](ConstantExpression* ce) {
259 for (auto* nextRef : ce->getReferences()) {
260 if (nextRef->isResolved()) continue;
261
262 LocalIdentifier* iden = lookupLocalIdentifier(*nextRef, scope);
263 if (iden == nullptr) return UNKNOWN_ERROR;
264 nextRef->set(iden);
265 }
Steven Moreland12f0ab12018-11-02 17:27:37 -0700266 for (auto* nextRef : ce->getTypeReferences()) {
267 if (nextRef->isResolved()) continue;
268
269 Type* nextType = lookupType(nextRef->getLookupFqName(), scope);
270 if (nextType == nullptr) {
271 std::cerr << "ERROR: Failed to lookup type '"
272 << nextRef->getLookupFqName().string() << "' at "
273 << nextRef->location() << "\n";
274 return UNKNOWN_ERROR;
275 }
276 nextRef->set(nextType);
277 }
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700278 return OK;
279 },
280 &visitedCE, true /* processBeforeDependencies */);
281 if (err != OK) return err;
282 }
283
284 return OK;
285 },
286 &visitedTypes);
287}
288
Timur Iskhakov565b0132017-09-06 18:07:11 -0700289status_t AST::validateDefinedTypesUniqueNames() const {
290 std::unordered_set<const Type*> visited;
291 return mRootScope.recursivePass(
Yifan Hong0e192c42018-10-23 15:32:19 -0700292 Type::ParseStage::POST_PARSE,
Timur Iskhakov565b0132017-09-06 18:07:11 -0700293 [&](const Type* type) -> status_t {
294 // We only want to validate type definition names in this place.
295 if (type->isScope()) {
296 return static_cast<const Scope*>(type)->validateUniqueNames();
297 }
298 return OK;
299 },
300 &visited);
301}
302
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700303status_t AST::resolveInheritance() {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700304 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700305 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, &Type::resolveInheritance,
306 &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700307}
308
Steven Moreland12f0ab12018-11-02 17:27:37 -0700309status_t AST::validateConstantExpressions() const {
310 return constantExpressionRecursivePass(
311 [](const ConstantExpression* ce) { return ce->validate(); },
312 true /* processBeforeDependencies */);
313}
314
315status_t AST::evaluateConstantExpressions() {
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700316 return constantExpressionRecursivePass(
317 [](ConstantExpression* ce) {
318 ce->evaluate();
319 return OK;
320 },
321 false /* processBeforeDependencies */);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700322}
323
324status_t AST::validate() const {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700325 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700326 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, &Type::validate, &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700327}
328
Timur Iskhakov458ca362017-09-12 23:16:03 -0700329status_t AST::topologicalReorder() {
330 std::unordered_map<const Type*, size_t> reversedOrder;
Timur Iskhakov40731af2017-08-24 14:18:35 -0700331 std::unordered_set<const Type*> stack;
Timur Iskhakov458ca362017-09-12 23:16:03 -0700332 status_t err = mRootScope.topologicalOrder(&reversedOrder, &stack).status;
333 if (err != OK) return err;
334
335 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700336 mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
337 [&](Type* type) {
338 if (type->isScope()) {
339 static_cast<Scope*>(type)->topologicalReorder(reversedOrder);
340 }
341 return OK;
342 },
343 &visited);
Timur Iskhakov458ca362017-09-12 23:16:03 -0700344 return OK;
Timur Iskhakov40731af2017-08-24 14:18:35 -0700345}
346
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700347status_t AST::checkAcyclicConstantExpressions() const {
348 std::unordered_set<const Type*> visitedTypes;
349 std::unordered_set<const ConstantExpression*> visitedCE;
350 std::unordered_set<const ConstantExpression*> stack;
Yifan Hong0e192c42018-10-23 15:32:19 -0700351 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
352 [&](const Type* type) -> status_t {
353 for (auto* ce : type->getConstantExpressions()) {
354 status_t err =
355 ce->checkAcyclic(&visitedCE, &stack).status;
356 CHECK(err != OK || stack.empty());
357 if (err != OK) return err;
358 }
359 return OK;
360 },
361 &visitedTypes);
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700362}
363
Timur Iskhakov041fdfe2017-09-06 15:56:01 -0700364status_t AST::checkForwardReferenceRestrictions() const {
365 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700366 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
367 [](const Type* type) -> status_t {
368 for (const Reference<Type>* ref : type->getReferences()) {
369 status_t err =
370 type->checkForwardReferenceRestrictions(*ref);
371 if (err != OK) return err;
372 }
373 return OK;
374 },
375 &visited);
Timur Iskhakov041fdfe2017-09-06 15:56:01 -0700376}
377
Neel Mehta8b0f06a2019-07-11 18:13:21 -0700378bool AST::importFQName(const FQName& fqName) {
Andreas Huber5345ec22016-07-29 13:33:27 -0700379 if (fqName.name().empty()) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700380 // import a package
Andreas Huber4ba5c972017-11-29 11:06:25 -0800381
Andreas Huberd2943e12016-08-05 11:59:31 -0700382 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -0700383
Neel Mehta8b0f06a2019-07-11 18:13:21 -0700384 status_t err = mCoordinator->appendPackageInterfacesToVector(fqName, &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -0700385
386 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700387 return false;
388 }
389
Neel Mehta8b0f06a2019-07-11 18:13:21 -0700390 for (const auto& subFQName : packageInterfaces) {
Yifan Hongf619fc72017-04-07 15:40:06 -0700391 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700392 AST* ast = mCoordinator->parse(subFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700393 if (ast == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700394 return false;
395 }
Steven Morelandb7c47472019-11-20 10:52:40 -0800396 addToImportedNamesGranular(subFQName);
397
Yifan Hong1977ea32016-10-05 12:49:08 -0700398 // all previous single type imports are ignored.
399 mImportedTypes.erase(ast);
Andreas Huber68f24592016-07-29 14:53:48 -0700400 }
401
402 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700403 }
404
Yifan Hong1977ea32016-10-05 12:49:08 -0700405 // cases like android.hardware.foo@1.0::IFoo.Internal
406 // android.hardware.foo@1.0::Abc.Internal
407
408 // assume it is an interface, and try to import it.
409 const FQName interfaceName = fqName.getTopLevelType();
Yifan Hongf619fc72017-04-07 15:40:06 -0700410 // Do not enforce restrictions on imports.
Steven Moreland71f09132018-02-20 12:24:30 -0800411 AST* importAST;
412 status_t err = mCoordinator->parseOptional(interfaceName, &importAST, &mImportedASTs,
413 Coordinator::Enforce::NONE);
414 if (err != OK) return false;
415 // importAST nullptr == file doesn't exist
Yifan Hong1977ea32016-10-05 12:49:08 -0700416
417 if (importAST != nullptr) {
418 // cases like android.hardware.foo@1.0::IFoo.Internal
419 // and android.hardware.foo@1.0::IFoo
420 if (fqName == interfaceName) {
421 // import a single file.
422 // all previous single type imports are ignored.
423 // cases like android.hardware.foo@1.0::IFoo
424 // and android.hardware.foo@1.0::types
425 mImportedTypes.erase(importAST);
Steven Morelandb7c47472019-11-20 10:52:40 -0800426 addToImportedNamesGranular(fqName);
Yifan Hong1977ea32016-10-05 12:49:08 -0700427 return true;
428 }
429
430 // import a single type from this file
431 // cases like android.hardware.foo@1.0::IFoo.Internal
432 FQName matchingName;
Neel Mehta8b0f06a2019-07-11 18:13:21 -0700433 Type* match = importAST->findDefinedType(fqName, &matchingName);
Yifan Hong1977ea32016-10-05 12:49:08 -0700434 if (match == nullptr) {
435 return false;
436 }
437 // will automatically create a set if it does not exist
438 mImportedTypes[importAST].insert(match);
Steven Morelandb7c47472019-11-20 10:52:40 -0800439 addToImportedNamesGranular(fqName);
Yifan Hong1977ea32016-10-05 12:49:08 -0700440 return true;
Andreas Huber68f24592016-07-29 14:53:48 -0700441 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700442
Yifan Hong1977ea32016-10-05 12:49:08 -0700443 // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal
444 FQName typesFQName = fqName.getTypesForPackage();
Yifan Hongf619fc72017-04-07 15:40:06 -0700445
446 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700447 importAST = mCoordinator->parse(typesFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700448
449 if (importAST != nullptr) {
450 // Attempt to find Abc.Internal in types.
451 FQName matchingName;
Neel Mehta8b0f06a2019-07-11 18:13:21 -0700452 Type* match = importAST->findDefinedType(fqName, &matchingName);
Yifan Hong1977ea32016-10-05 12:49:08 -0700453 if (match == nullptr) {
454 return false;
455 }
456 // will automatically create a set if not exist
457 mImportedTypes[importAST].insert(match);
Steven Morelandb7c47472019-11-20 10:52:40 -0800458 addToImportedNamesGranular(fqName);
Yifan Hong1977ea32016-10-05 12:49:08 -0700459 return true;
460 }
461
462 // can't find an appropriate AST for fqName.
463 return false;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700464}
465
Neel Mehta8b0f06a2019-07-11 18:13:21 -0700466bool AST::addImplicitImport(const FQName& fqName) {
467 CHECK(fqName.isFullyQualified());
468
469 if (importFQName(fqName)) {
470 mImplicitImports.push_back(fqName);
471 return true;
472 }
473
474 return false;
475}
476
477bool AST::addImport(const char* import, const Location& location) {
478 FQName fqName;
479 if (!FQName::parse(import, &fqName)) {
480 std::cerr << "ERROR: '" << import << "' is an invalid fully-qualified name." << std::endl;
481 return false;
482 }
483
484 fqName.applyDefaults(mPackage.package(), mPackage.version());
485
486 if (importFQName(fqName)) {
487 mImportStatements.push_back({fqName, location});
488 return true;
489 }
490
Steven Moreland277c2702020-01-22 09:51:32 -0800491 std::cerr << "while importing " << import << " at " << location << "." << std::endl;
492
Neel Mehta8b0f06a2019-07-11 18:13:21 -0700493 return false;
494}
495
Andreas Huber39fa7182016-08-19 14:27:33 -0700496void AST::addImportedAST(AST *ast) {
497 mImportedASTs.insert(ast);
498}
499
Timur Iskhakov565b0132017-09-06 18:07:11 -0700500FQName AST::makeFullName(const char* localName, Scope* scope) const {
501 std::vector<std::string> pathComponents{{localName}};
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700502 for (; scope != &mRootScope; scope = scope->parent()) {
Neel Mehta9200af02019-07-19 13:24:57 -0700503 pathComponents.push_back(scope->definedName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700504 }
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700505
506 std::reverse(pathComponents.begin(), pathComponents.end());
507 std::string path = StringHelper::JoinStrings(pathComponents, ".");
Andreas Huber31629bc2016-08-03 09:06:40 -0700508
Timur Iskhakov565b0132017-09-06 18:07:11 -0700509 return FQName(mPackage.package(), mPackage.version(), path);
510}
Andreas Huber39fa7182016-08-19 14:27:33 -0700511
Timur Iskhakov565b0132017-09-06 18:07:11 -0700512void AST::addScopedType(NamedType* type, Scope* scope) {
513 scope->addType(type);
514 mDefinedTypesByFullName[type->fqName()] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700515}
516
Steven Moreland8f8e8622019-11-04 12:38:38 -0800517LocalIdentifier* AST::lookupLocalIdentifier(const Reference<LocalIdentifier>& ref,
518 const Scope* scope) {
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700519 const FQName& fqName = ref.getLookupFqName();
520
521 if (fqName.isIdentifier()) {
522 LocalIdentifier* iden = scope->lookupIdentifier(fqName.name());
523 if (iden == nullptr) {
524 std::cerr << "ERROR: identifier " << fqName.string() << " could not be found at "
525 << ref.location() << "\n";
526 return nullptr;
527 }
528 return iden;
529 } else {
530 std::string errorMsg;
531 EnumValue* enumValue = lookupEnumValue(fqName, &errorMsg, scope);
532 if (enumValue == nullptr) {
533 std::cerr << "ERROR: " << errorMsg << " at " << ref.location() << "\n";
534 return nullptr;
535 }
536 return enumValue;
537 }
538}
539
Steven Moreland8f8e8622019-11-04 12:38:38 -0800540EnumValue* AST::lookupEnumValue(const FQName& fqName, std::string* errorMsg, const Scope* scope) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700541 FQName enumTypeName = fqName.typeName();
542 std::string enumValueName = fqName.valueName();
543
Yifan Hongf24fa852016-09-23 11:03:15 -0700544 CHECK(!enumValueName.empty());
545
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700546 Type* type = lookupType(enumTypeName, scope);
Yifan Hongf24fa852016-09-23 11:03:15 -0700547 if(type == nullptr) {
548 *errorMsg = "Cannot find type " + enumTypeName.string();
549 return nullptr;
550 }
Steven Morelandd965ce92017-11-27 16:05:34 -0800551 type = type->resolve();
Yifan Hongf24fa852016-09-23 11:03:15 -0700552 if(!type->isEnum()) {
553 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
554 return nullptr;
555 }
556
557 EnumType *enumType = static_cast<EnumType *>(type);
558 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
559 if(v == nullptr) {
560 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
561 return nullptr;
562 }
Andreas Huber4ba5c972017-11-29 11:06:25 -0800563
564 mReferencedTypeNames.insert(enumType->fqName());
565
Yifan Hongf24fa852016-09-23 11:03:15 -0700566 return v;
567}
568
Steven Moreland8f8e8622019-11-04 12:38:38 -0800569Type* AST::lookupType(const FQName& fqName, const Scope* scope) {
Andreas Huberda51b8e2016-07-28 16:00:57 -0700570 if (fqName.name().empty()) {
571 // Given a package and version???
Yifan Hong1977ea32016-10-05 12:49:08 -0700572 return nullptr;
Andreas Huberda51b8e2016-07-28 16:00:57 -0700573 }
574
Yifan Hong87ff8232017-01-09 12:07:05 -0800575 Type *returnedType = nullptr;
576
Andreas Huber84f89de2016-07-28 15:39:51 -0700577 if (fqName.package().empty() && fqName.version().empty()) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800578 // resolve locally first if possible.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700579 returnedType = lookupTypeLocally(fqName, scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800580 if (returnedType != nullptr) {
581 return returnedType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700582 }
583 }
584
Steven Moreland87e69dc2017-09-12 18:14:28 -0700585 status_t status = lookupAutofilledType(fqName, &returnedType);
586 if (status != OK) {
587 return nullptr;
588 }
589 if (returnedType != nullptr) {
590 return returnedType;
Yifan Hong87ff8232017-01-09 12:07:05 -0800591 }
592
593 return lookupTypeFromImports(fqName);
594}
595
596// Rule 0: try resolve locally
Steven Moreland8f8e8622019-11-04 12:38:38 -0800597Type* AST::lookupTypeLocally(const FQName& fqName, const Scope* scope) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800598 CHECK(fqName.package().empty() && fqName.version().empty()
599 && !fqName.name().empty() && fqName.valueName().empty());
600
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700601 for (; scope != nullptr; scope = scope->parent()) {
602 Type* type = scope->lookupType(fqName);
Yifan Hong87ff8232017-01-09 12:07:05 -0800603 if (type != nullptr) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800604 return type;
605 }
606 }
607
608 return nullptr;
609}
610
611// Rule 1: auto-fill with current package
612status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) {
Steven Moreland87e69dc2017-09-12 18:14:28 -0700613 CHECK(!fqName.name().empty() && fqName.valueName().empty());
Yifan Hong87ff8232017-01-09 12:07:05 -0800614
615 FQName autofilled = fqName;
616 autofilled.applyDefaults(mPackage.package(), mPackage.version());
617 FQName matchingName;
618 // Given this fully-qualified name, the type may be defined in this AST, or other files
619 // in import.
620 Type *local = findDefinedType(autofilled, &matchingName);
621 CHECK(local == nullptr || autofilled == matchingName);
Steven Moreland87e69dc2017-09-12 18:14:28 -0700622 Type* fromImport = lookupTypeFromImports(autofilled);
Yifan Hong87ff8232017-01-09 12:07:05 -0800623
624 if (local != nullptr && fromImport != nullptr && local != fromImport) {
625 // Something bad happen; two types have the same FQName.
626 std::cerr << "ERROR: Unable to resolve type name '"
627 << fqName.string()
628 << "' (i.e. '"
629 << autofilled.string()
630 << "'), multiple definitions found.\n";
631
632 return UNKNOWN_ERROR;
633 }
634 if (local != nullptr) {
635 *returnedType = local;
636 return OK;
637 }
638 // If fromImport is nullptr as well, return nullptr to fall through to next rule.
639 *returnedType = fromImport;
640 return OK;
641}
642
643// Rule 2: look at imports
644Type *AST::lookupTypeFromImports(const FQName &fqName) {
645
Andreas Huber39fa7182016-08-19 14:27:33 -0700646 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700647 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700648 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700649
Andreas Huber39fa7182016-08-19 14:27:33 -0700650 for (const auto &importedAST : mImportedASTs) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700651 if (mImportedTypes.find(importedAST) != mImportedTypes.end()) {
652 // ignore single type imports
653 continue;
654 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700655 FQName matchingName;
656 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700657
Andreas Huber39fa7182016-08-19 14:27:33 -0700658 if (match != nullptr) {
659 if (resolvedType != nullptr) {
660 std::cerr << "ERROR: Unable to resolve type name '"
661 << fqName.string()
662 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700663
Andreas Huber39fa7182016-08-19 14:27:33 -0700664 std::cerr << " " << resolvedName.string() << "\n";
665 std::cerr << " " << matchingName.string() << "\n";
666
Yifan Hong1977ea32016-10-05 12:49:08 -0700667 return nullptr;
668 }
669
670 resolvedType = match;
671 returnedType = resolvedType;
672 resolvedName = matchingName;
673
674 // Keep going even after finding a match.
675 }
676 }
677
678 for (const auto &pair : mImportedTypes) {
679 AST *importedAST = pair.first;
680 std::set<Type *> importedTypes = pair.second;
681
682 FQName matchingName;
683 Type *match = importedAST->findDefinedType(fqName, &matchingName);
684 if (match != nullptr &&
685 importedTypes.find(match) != importedTypes.end()) {
686 if (resolvedType != nullptr) {
687 std::cerr << "ERROR: Unable to resolve type name '"
688 << fqName.string()
689 << "', multiple matches found:\n";
690
691 std::cerr << " " << resolvedName.string() << "\n";
692 std::cerr << " " << matchingName.string() << "\n";
693
694 return nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700695 }
696
697 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700698 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700699 resolvedName = matchingName;
700
701 // Keep going even after finding a match.
702 }
703 }
704
705 if (resolvedType) {
Iliyan Malchev800273d2016-09-02 15:25:07 -0700706 returnedType = resolvedType;
707
708 // If the resolved type is not an interface, we need to determine
709 // whether it is defined in types.hal, or in some other interface. In
710 // the latter case, we need to emit a dependency for the interface in
711 // which the type is defined.
712 //
713 // Consider the following:
714 // android.hardware.tests.foo@1.0::Record
715 // android.hardware.tests.foo@1.0::IFoo.Folder
716 // android.hardware.tests.foo@1.0::Folder
717 //
718 // If Record is an interface, then we keep track of it for the purpose
719 // of emitting dependencies in the target language (for example #include
720 // in C++). If Record is a UDT, then we assume it is defined in
721 // types.hal in android.hardware.tests.foo@1.0.
722 //
723 // In the case of IFoo.Folder, the same applies. If IFoo is an
724 // interface, we need to track this for the purpose of emitting
725 // dependencies. If not, then it must have been defined in types.hal.
726 //
727 // In the case of just specifying Folder, the resolved type is
Yifan Hongfece6ec2017-01-12 17:04:04 -0800728 // android.hardware.tests.foo@1.0::Folder, and the same logic as
Iliyan Malchev800273d2016-09-02 15:25:07 -0700729 // above applies.
730
731 if (!resolvedType->isInterface()) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800732 FQName ifc = resolvedName.getTopLevelType();
Iliyan Malchev800273d2016-09-02 15:25:07 -0700733 for (const auto &importedAST : mImportedASTs) {
734 FQName matchingName;
735 Type *match = importedAST->findDefinedType(ifc, &matchingName);
736 if (match != nullptr && match->isInterface()) {
737 resolvedType = match;
738 }
739 }
740 }
741
Andreas Huber39fa7182016-08-19 14:27:33 -0700742 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700743 // Non-interface types are declared in the associated types header.
Yifan Hongfece6ec2017-01-12 17:04:04 -0800744 FQName typesName = resolvedName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -0700745
Andreas Huber0e00de42016-08-03 09:56:02 -0700746 mImportedNames.insert(typesName);
747 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700748 // Do _not_ use fqName, i.e. the name we used to look up the type,
749 // but instead use the name of the interface we found.
750 // This is necessary because if fqName pointed to a typedef which
751 // in turn referenced the found interface we'd mistakenly use the
752 // name of the typedef instead of the proper name of the interface.
753
Andreas Huber4ba5c972017-11-29 11:06:25 -0800754 const FQName &typeName =
755 static_cast<Interface *>(resolvedType)->fqName();
756
757 mImportedNames.insert(typeName);
Andreas Huber0e00de42016-08-03 09:56:02 -0700758 }
Andreas Huber737080b2016-08-02 15:38:04 -0700759 }
760
Steven Morelandbb5c80b2016-10-05 11:07:36 -0700761 return returnedType;
Andreas Huber5345ec22016-07-29 13:33:27 -0700762}
763
Andreas Huber4ba5c972017-11-29 11:06:25 -0800764void AST::addToImportedNamesGranular(const FQName &fqName) {
765 if (fqName.package() == package().package()
766 && fqName.version() == package().version()) {
767 // Our own names are _defined_ here, not imported.
768 return;
769 }
770
771 mImportedNamesGranular.insert(fqName);
772}
773
Andreas Huber39fa7182016-08-19 14:27:33 -0700774Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700775 for (const auto &pair : mDefinedTypesByFullName) {
776 const FQName &key = pair.first;
777 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700778
Andreas Huber39fa7182016-08-19 14:27:33 -0700779 if (key.endsWith(fqName)) {
780 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700781 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700782 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700783 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700784
785 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700786}
787
Neel Mehta55c065e2019-05-31 13:30:12 -0700788const std::vector<ImportStatement>& AST::getImportStatements() const {
789 return mImportStatements;
790}
791
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700792void AST::getImportedPackages(std::set<FQName> *importSet) const {
Steven Moreland06a81cf2018-01-17 11:13:46 -0800793 for (const auto& fqName : mImportedNamesGranular) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800794 FQName packageName = fqName.getPackageAndVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700795
796 if (packageName == mPackage) {
797 // We only care about external imports, not our own package.
798 continue;
799 }
800
801 importSet->insert(packageName);
802 }
803}
804
Yifan Hong40a373d2016-11-30 15:16:47 -0800805void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const {
806 getImportedPackages(importSet);
Steven Moreland06a81cf2018-01-17 11:13:46 -0800807
Yifan Hong40a373d2016-11-30 15:16:47 -0800808 std::set<FQName> newSet;
809 for (const auto &ast : mImportedASTs) {
810 if (importSet->find(ast->package()) != importSet->end()) {
811 ast->getImportedPackagesHierarchy(&newSet);
812 }
813 }
814 importSet->insert(newSet.begin(), newSet.end());
815}
816
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800817void AST::getAllImportedNames(std::set<FQName> *allImportNames) const {
818 for (const auto& name : mImportedNames) {
819 allImportNames->insert(name);
Steven Morelandc59326e2017-06-20 15:19:30 -0700820 AST* ast = mCoordinator->parse(name, nullptr /* imported */, Coordinator::Enforce::NONE);
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800821 ast->getAllImportedNames(allImportNames);
822 }
823}
824
Andreas Huber4ba5c972017-11-29 11:06:25 -0800825void AST::getAllImportedNamesGranular(std::set<FQName> *allImportNames) const {
826 for (const auto& fqName : mImportedNamesGranular) {
827 if (fqName.name() == "types") {
828 // A package will export everything _defined_ but will not
829 // re-export anything it itself imported.
830 AST* ast = mCoordinator->parse(
831 fqName, nullptr /* imported */, Coordinator::Enforce::NONE);
832
Steven Morelandb7c47472019-11-20 10:52:40 -0800833 // imported names must have already been validated
834 CHECK(ast != nullptr) << fqName.string();
835
Andreas Huber4ba5c972017-11-29 11:06:25 -0800836 ast->addDefinedTypes(allImportNames);
837 } else {
838 allImportNames->insert(fqName);
839 }
840 }
841}
842
Andreas Huber0fa9e392016-08-31 09:05:44 -0700843bool AST::isJavaCompatible() const {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700844 return mRootScope.isJavaCompatible();
Andreas Huber0fa9e392016-08-31 09:05:44 -0700845}
846
Andreas Huber019d21d2016-10-03 12:59:47 -0700847void AST::appendToExportedTypesVector(
848 std::vector<const Type *> *exportedTypes) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700849 mRootScope.appendToExportedTypesVector(exportedTypes);
Andreas Huber019d21d2016-10-03 12:59:47 -0700850}
851
Yifan Hongc8934042016-11-17 17:10:52 -0800852bool AST::isIBase() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700853 Interface* iface = mRootScope.getInterface();
Yifan Hongc8934042016-11-17 17:10:52 -0800854 return iface != nullptr && iface->isIBase();
855}
856
Yifan Hong78b38d12017-02-13 18:14:46 +0000857const Interface *AST::getInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700858 return mRootScope.getInterface();
Yifan Hong78b38d12017-02-13 18:14:46 +0000859}
860
Steven Moreland19f11b52017-05-12 18:22:21 -0700861std::string AST::getBaseName() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700862 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -0700863
864 return iface ? iface->getBaseName() : "types";
865}
866
Andreas Huber308d8a22017-11-06 14:46:52 -0800867void AST::addDefinedTypes(std::set<FQName> *definedTypes) const {
868 std::for_each(
869 mDefinedTypesByFullName.begin(),
870 mDefinedTypesByFullName.end(),
871 [definedTypes](const auto &elem) {
872 if (!elem.second->isTypeDef()) {
873 definedTypes->insert(elem.first);
874 }
875 });
876}
877
878void AST::addReferencedTypes(std::set<FQName> *referencedTypes) const {
879 std::for_each(
880 mReferencedTypeNames.begin(),
881 mReferencedTypeNames.end(),
882 [referencedTypes](const auto &fqName) {
883 referencedTypes->insert(fqName);
884 });
885}
886
Neel Mehta0ee353f2019-05-30 17:40:29 -0700887bool AST::addMethod(Method* method, Interface* iface) {
888 if (iface->isIBase()) {
889 if (!mAllReservedMethods.emplace(method->name(), method).second) {
890 std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name()
891 << std::endl;
892 return false;
893 }
894
895 // methods will be added to iface in addAllReservedMethodsToInterface
896 return true;
897 }
898
899 iface->addUserDefinedMethod(method);
900
901 return true;
902}
903
904bool AST::addAllReservedMethodsToInterface(Interface* iface) {
905 std::map<std::string, Method*> allReservedMethods(mAllReservedMethods);
906 // Looking for the IBase AST which is imported for all interfaces that are not IBase
907 for (const AST* importedAST : mImportedASTs) {
908 allReservedMethods.insert(importedAST->mAllReservedMethods.begin(),
909 importedAST->mAllReservedMethods.end());
910 }
911
912 return iface->addAllReservedMethods(allReservedMethods);
913}
914
Steven Moreland4d89ee22019-03-08 13:25:32 -0800915void AST::setHeader(const DocComment* header) {
916 mHeader = header;
917}
918
919const DocComment* AST::getHeader() const {
920 return mHeader;
921}
922
923void AST::addUnhandledComment(const DocComment* docComment) {
924 if (docComment != nullptr) mUnhandledComments.push_back(docComment);
925}
926
927const std::vector<const DocComment*> AST::getUnhandledComments() const {
928 return mUnhandledComments;
929}
930
Andreas Huberc9410c72016-07-28 12:18:40 -0700931} // namespace android;