blob: db9cd359568092bd6952f6fcc71e2b4daa0534e1 [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 "
217 << nextRef->location() << "\n";
218 return UNKNOWN_ERROR;
219 }
220 nextRef->set(nextType);
221 }
222
223 return OK;
224 },
225 &visited);
226}
227
Andreas Huber308d8a22017-11-06 14:46:52 -0800228status_t AST::gatherReferencedTypes() {
229 std::unordered_set<const Type*> visited;
230 return mRootScope.recursivePass(
Yifan Hong0e192c42018-10-23 15:32:19 -0700231 Type::ParseStage::POST_PARSE,
Andreas Huber308d8a22017-11-06 14:46:52 -0800232 [&](Type* type) -> status_t {
233 for (auto* nextRef : type->getReferences()) {
234 const Type *targetType = nextRef->get();
235 if (targetType->isNamedType()) {
236 mReferencedTypeNames.insert(
237 static_cast<const NamedType *>(targetType)->fqName());
238 }
239 }
240
241 return OK;
242 },
243 &visited);
244}
245
Steven Moreland12f0ab12018-11-02 17:27:37 -0700246status_t AST::lookupConstantExpressions() {
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700247 std::unordered_set<const Type*> visitedTypes;
248 std::unordered_set<const ConstantExpression*> visitedCE;
249
250 return mRootScope.recursivePass(
Yifan Hong0e192c42018-10-23 15:32:19 -0700251 Type::ParseStage::POST_PARSE,
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700252 [&](Type* type) -> status_t {
253 Scope* scope = type->isScope() ? static_cast<Scope*>(type) : type->parent();
254
255 for (auto* ce : type->getConstantExpressions()) {
256 status_t err = ce->recursivePass(
257 [&](ConstantExpression* ce) {
258 for (auto* nextRef : ce->getReferences()) {
259 if (nextRef->isResolved()) continue;
260
261 LocalIdentifier* iden = lookupLocalIdentifier(*nextRef, scope);
262 if (iden == nullptr) return UNKNOWN_ERROR;
263 nextRef->set(iden);
264 }
Steven Moreland12f0ab12018-11-02 17:27:37 -0700265 for (auto* nextRef : ce->getTypeReferences()) {
266 if (nextRef->isResolved()) continue;
267
268 Type* nextType = lookupType(nextRef->getLookupFqName(), scope);
269 if (nextType == nullptr) {
270 std::cerr << "ERROR: Failed to lookup type '"
271 << nextRef->getLookupFqName().string() << "' at "
272 << nextRef->location() << "\n";
273 return UNKNOWN_ERROR;
274 }
275 nextRef->set(nextType);
276 }
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700277 return OK;
278 },
279 &visitedCE, true /* processBeforeDependencies */);
280 if (err != OK) return err;
281 }
282
283 return OK;
284 },
285 &visitedTypes);
286}
287
Timur Iskhakov565b0132017-09-06 18:07:11 -0700288status_t AST::validateDefinedTypesUniqueNames() const {
289 std::unordered_set<const Type*> visited;
290 return mRootScope.recursivePass(
Yifan Hong0e192c42018-10-23 15:32:19 -0700291 Type::ParseStage::POST_PARSE,
Timur Iskhakov565b0132017-09-06 18:07:11 -0700292 [&](const Type* type) -> status_t {
293 // We only want to validate type definition names in this place.
294 if (type->isScope()) {
295 return static_cast<const Scope*>(type)->validateUniqueNames();
296 }
297 return OK;
298 },
299 &visited);
300}
301
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700302status_t AST::resolveInheritance() {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700303 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700304 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, &Type::resolveInheritance,
305 &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700306}
307
Steven Moreland12f0ab12018-11-02 17:27:37 -0700308status_t AST::validateConstantExpressions() const {
309 return constantExpressionRecursivePass(
310 [](const ConstantExpression* ce) { return ce->validate(); },
311 true /* processBeforeDependencies */);
312}
313
314status_t AST::evaluateConstantExpressions() {
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700315 return constantExpressionRecursivePass(
316 [](ConstantExpression* ce) {
317 ce->evaluate();
318 return OK;
319 },
320 false /* processBeforeDependencies */);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700321}
322
323status_t AST::validate() const {
Timur Iskhakov33431e62017-08-21 17:31:23 -0700324 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700325 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, &Type::validate, &visited);
Timur Iskhakovcec46c42017-08-09 00:22:02 -0700326}
327
Timur Iskhakov458ca362017-09-12 23:16:03 -0700328status_t AST::topologicalReorder() {
329 std::unordered_map<const Type*, size_t> reversedOrder;
Timur Iskhakov40731af2017-08-24 14:18:35 -0700330 std::unordered_set<const Type*> stack;
Timur Iskhakov458ca362017-09-12 23:16:03 -0700331 status_t err = mRootScope.topologicalOrder(&reversedOrder, &stack).status;
332 if (err != OK) return err;
333
334 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700335 mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
336 [&](Type* type) {
337 if (type->isScope()) {
338 static_cast<Scope*>(type)->topologicalReorder(reversedOrder);
339 }
340 return OK;
341 },
342 &visited);
Timur Iskhakov458ca362017-09-12 23:16:03 -0700343 return OK;
Timur Iskhakov40731af2017-08-24 14:18:35 -0700344}
345
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700346status_t AST::checkAcyclicConstantExpressions() const {
347 std::unordered_set<const Type*> visitedTypes;
348 std::unordered_set<const ConstantExpression*> visitedCE;
349 std::unordered_set<const ConstantExpression*> stack;
Yifan Hong0e192c42018-10-23 15:32:19 -0700350 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
351 [&](const Type* type) -> status_t {
352 for (auto* ce : type->getConstantExpressions()) {
353 status_t err =
354 ce->checkAcyclic(&visitedCE, &stack).status;
355 CHECK(err != OK || stack.empty());
356 if (err != OK) return err;
357 }
358 return OK;
359 },
360 &visitedTypes);
Timur Iskhakov77dd65c2017-08-31 22:46:56 -0700361}
362
Timur Iskhakov041fdfe2017-09-06 15:56:01 -0700363status_t AST::checkForwardReferenceRestrictions() const {
364 std::unordered_set<const Type*> visited;
Yifan Hong0e192c42018-10-23 15:32:19 -0700365 return mRootScope.recursivePass(Type::ParseStage::POST_PARSE,
366 [](const Type* type) -> status_t {
367 for (const Reference<Type>* ref : type->getReferences()) {
368 status_t err =
369 type->checkForwardReferenceRestrictions(*ref);
370 if (err != OK) return err;
371 }
372 return OK;
373 },
374 &visited);
Timur Iskhakov041fdfe2017-09-06 15:56:01 -0700375}
376
Neel Mehta55c065e2019-05-31 13:30:12 -0700377bool AST::addImport(const char* import, const Location& location) {
Steven Morelande1b157e2018-03-06 14:18:32 -0800378 FQName fqName;
379 if (!FQName::parse(import, &fqName)) {
Steven Moreland01857a32018-01-03 10:15:01 -0800380 std::cerr << "ERROR: '" << import << "' is an invalid fully-qualified name." << std::endl;
381 return false;
382 }
Andreas Hubereb1081f2016-07-28 13:13:24 -0700383
Andreas Huber5345ec22016-07-29 13:33:27 -0700384 fqName.applyDefaults(mPackage.package(), mPackage.version());
385
Neel Mehta55c065e2019-05-31 13:30:12 -0700386 mImportStatements.push_back({fqName, location});
387
Andreas Huber5345ec22016-07-29 13:33:27 -0700388 if (fqName.name().empty()) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700389 // import a package
Andreas Huber4ba5c972017-11-29 11:06:25 -0800390
Andreas Huberd2943e12016-08-05 11:59:31 -0700391 std::vector<FQName> packageInterfaces;
Andreas Huber68f24592016-07-29 14:53:48 -0700392
Andreas Huberd2943e12016-08-05 11:59:31 -0700393 status_t err =
Steven Morelandaa186832016-09-26 13:51:43 -0700394 mCoordinator->appendPackageInterfacesToVector(fqName,
395 &packageInterfaces);
Andreas Huberd2943e12016-08-05 11:59:31 -0700396
397 if (err != OK) {
Andreas Huber68f24592016-07-29 14:53:48 -0700398 return false;
399 }
400
Andreas Huberd2943e12016-08-05 11:59:31 -0700401 for (const auto &subFQName : packageInterfaces) {
Andreas Huber4ba5c972017-11-29 11:06:25 -0800402 addToImportedNamesGranular(subFQName);
403
Yifan Hongf619fc72017-04-07 15:40:06 -0700404 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700405 AST* ast = mCoordinator->parse(subFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700406 if (ast == nullptr) {
Andreas Huber68f24592016-07-29 14:53:48 -0700407 return false;
408 }
Yifan Hong1977ea32016-10-05 12:49:08 -0700409 // all previous single type imports are ignored.
410 mImportedTypes.erase(ast);
Andreas Huber68f24592016-07-29 14:53:48 -0700411 }
412
413 return true;
Andreas Huber5345ec22016-07-29 13:33:27 -0700414 }
415
Andreas Huber4ba5c972017-11-29 11:06:25 -0800416 addToImportedNamesGranular(fqName);
417
Yifan Hong1977ea32016-10-05 12:49:08 -0700418 // cases like android.hardware.foo@1.0::IFoo.Internal
419 // android.hardware.foo@1.0::Abc.Internal
420
421 // assume it is an interface, and try to import it.
422 const FQName interfaceName = fqName.getTopLevelType();
Yifan Hongf619fc72017-04-07 15:40:06 -0700423 // Do not enforce restrictions on imports.
Steven Moreland71f09132018-02-20 12:24:30 -0800424 AST* importAST;
425 status_t err = mCoordinator->parseOptional(interfaceName, &importAST, &mImportedASTs,
426 Coordinator::Enforce::NONE);
427 if (err != OK) return false;
428 // importAST nullptr == file doesn't exist
Yifan Hong1977ea32016-10-05 12:49:08 -0700429
430 if (importAST != nullptr) {
431 // cases like android.hardware.foo@1.0::IFoo.Internal
432 // and android.hardware.foo@1.0::IFoo
433 if (fqName == interfaceName) {
434 // import a single file.
435 // all previous single type imports are ignored.
436 // cases like android.hardware.foo@1.0::IFoo
437 // and android.hardware.foo@1.0::types
438 mImportedTypes.erase(importAST);
439 return true;
440 }
441
442 // import a single type from this file
443 // cases like android.hardware.foo@1.0::IFoo.Internal
444 FQName matchingName;
445 Type *match = importAST->findDefinedType(fqName, &matchingName);
446 if (match == nullptr) {
447 return false;
448 }
449 // will automatically create a set if it does not exist
450 mImportedTypes[importAST].insert(match);
451 return true;
Andreas Huber68f24592016-07-29 14:53:48 -0700452 }
Andreas Huber84f89de2016-07-28 15:39:51 -0700453
Yifan Hong1977ea32016-10-05 12:49:08 -0700454 // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal
455 FQName typesFQName = fqName.getTypesForPackage();
Yifan Hongf619fc72017-04-07 15:40:06 -0700456
457 // Do not enforce restrictions on imports.
Steven Morelandc59326e2017-06-20 15:19:30 -0700458 importAST = mCoordinator->parse(typesFQName, &mImportedASTs, Coordinator::Enforce::NONE);
Yifan Hong1977ea32016-10-05 12:49:08 -0700459
460 if (importAST != nullptr) {
461 // Attempt to find Abc.Internal in types.
462 FQName matchingName;
463 Type *match = importAST->findDefinedType(fqName, &matchingName);
464 if (match == nullptr) {
465 return false;
466 }
467 // will automatically create a set if not exist
468 mImportedTypes[importAST].insert(match);
469 return true;
470 }
471
472 // can't find an appropriate AST for fqName.
473 return false;
Andreas Hubereb1081f2016-07-28 13:13:24 -0700474}
475
Andreas Huber39fa7182016-08-19 14:27:33 -0700476void AST::addImportedAST(AST *ast) {
477 mImportedASTs.insert(ast);
478}
479
Timur Iskhakov565b0132017-09-06 18:07:11 -0700480FQName AST::makeFullName(const char* localName, Scope* scope) const {
481 std::vector<std::string> pathComponents{{localName}};
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700482 for (; scope != &mRootScope; scope = scope->parent()) {
483 pathComponents.push_back(scope->localName());
Andreas Huber31629bc2016-08-03 09:06:40 -0700484 }
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700485
486 std::reverse(pathComponents.begin(), pathComponents.end());
487 std::string path = StringHelper::JoinStrings(pathComponents, ".");
Andreas Huber31629bc2016-08-03 09:06:40 -0700488
Timur Iskhakov565b0132017-09-06 18:07:11 -0700489 return FQName(mPackage.package(), mPackage.version(), path);
490}
Andreas Huber39fa7182016-08-19 14:27:33 -0700491
Timur Iskhakov565b0132017-09-06 18:07:11 -0700492void AST::addScopedType(NamedType* type, Scope* scope) {
493 scope->addType(type);
494 mDefinedTypesByFullName[type->fqName()] = type;
Andreas Huber31629bc2016-08-03 09:06:40 -0700495}
496
Timur Iskhakov82c048e2017-09-09 01:20:53 -0700497LocalIdentifier* AST::lookupLocalIdentifier(const Reference<LocalIdentifier>& ref, Scope* scope) {
498 const FQName& fqName = ref.getLookupFqName();
499
500 if (fqName.isIdentifier()) {
501 LocalIdentifier* iden = scope->lookupIdentifier(fqName.name());
502 if (iden == nullptr) {
503 std::cerr << "ERROR: identifier " << fqName.string() << " could not be found at "
504 << ref.location() << "\n";
505 return nullptr;
506 }
507 return iden;
508 } else {
509 std::string errorMsg;
510 EnumValue* enumValue = lookupEnumValue(fqName, &errorMsg, scope);
511 if (enumValue == nullptr) {
512 std::cerr << "ERROR: " << errorMsg << " at " << ref.location() << "\n";
513 return nullptr;
514 }
515 return enumValue;
516 }
517}
518
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700519EnumValue* AST::lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope) {
Yifan Hongf24fa852016-09-23 11:03:15 -0700520 FQName enumTypeName = fqName.typeName();
521 std::string enumValueName = fqName.valueName();
522
Yifan Hongf24fa852016-09-23 11:03:15 -0700523 CHECK(!enumValueName.empty());
524
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700525 Type* type = lookupType(enumTypeName, scope);
Yifan Hongf24fa852016-09-23 11:03:15 -0700526 if(type == nullptr) {
527 *errorMsg = "Cannot find type " + enumTypeName.string();
528 return nullptr;
529 }
Steven Morelandd965ce92017-11-27 16:05:34 -0800530 type = type->resolve();
Yifan Hongf24fa852016-09-23 11:03:15 -0700531 if(!type->isEnum()) {
532 *errorMsg = "Type " + enumTypeName.string() + " is not an enum type";
533 return nullptr;
534 }
535
536 EnumType *enumType = static_cast<EnumType *>(type);
537 EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName));
538 if(v == nullptr) {
539 *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName;
540 return nullptr;
541 }
Andreas Huber4ba5c972017-11-29 11:06:25 -0800542
543 mReferencedTypeNames.insert(enumType->fqName());
544
Yifan Hongf24fa852016-09-23 11:03:15 -0700545 return v;
546}
547
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700548Type* AST::lookupType(const FQName& fqName, Scope* scope) {
Andreas Huberda51b8e2016-07-28 16:00:57 -0700549 if (fqName.name().empty()) {
550 // Given a package and version???
Yifan Hong1977ea32016-10-05 12:49:08 -0700551 return nullptr;
Andreas Huberda51b8e2016-07-28 16:00:57 -0700552 }
553
Yifan Hong87ff8232017-01-09 12:07:05 -0800554 Type *returnedType = nullptr;
555
Andreas Huber84f89de2016-07-28 15:39:51 -0700556 if (fqName.package().empty() && fqName.version().empty()) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800557 // resolve locally first if possible.
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700558 returnedType = lookupTypeLocally(fqName, scope);
Yifan Hong87ff8232017-01-09 12:07:05 -0800559 if (returnedType != nullptr) {
560 return returnedType;
Andreas Huberc9410c72016-07-28 12:18:40 -0700561 }
562 }
563
Steven Moreland87e69dc2017-09-12 18:14:28 -0700564 status_t status = lookupAutofilledType(fqName, &returnedType);
565 if (status != OK) {
566 return nullptr;
567 }
568 if (returnedType != nullptr) {
569 return returnedType;
Yifan Hong87ff8232017-01-09 12:07:05 -0800570 }
571
572 return lookupTypeFromImports(fqName);
573}
574
575// Rule 0: try resolve locally
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700576Type* AST::lookupTypeLocally(const FQName& fqName, Scope* scope) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800577 CHECK(fqName.package().empty() && fqName.version().empty()
578 && !fqName.name().empty() && fqName.valueName().empty());
579
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700580 for (; scope != nullptr; scope = scope->parent()) {
581 Type* type = scope->lookupType(fqName);
Yifan Hong87ff8232017-01-09 12:07:05 -0800582 if (type != nullptr) {
Yifan Hong87ff8232017-01-09 12:07:05 -0800583 return type;
584 }
585 }
586
587 return nullptr;
588}
589
590// Rule 1: auto-fill with current package
591status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) {
Steven Moreland87e69dc2017-09-12 18:14:28 -0700592 CHECK(!fqName.name().empty() && fqName.valueName().empty());
Yifan Hong87ff8232017-01-09 12:07:05 -0800593
594 FQName autofilled = fqName;
595 autofilled.applyDefaults(mPackage.package(), mPackage.version());
596 FQName matchingName;
597 // Given this fully-qualified name, the type may be defined in this AST, or other files
598 // in import.
599 Type *local = findDefinedType(autofilled, &matchingName);
600 CHECK(local == nullptr || autofilled == matchingName);
Steven Moreland87e69dc2017-09-12 18:14:28 -0700601 Type* fromImport = lookupTypeFromImports(autofilled);
Yifan Hong87ff8232017-01-09 12:07:05 -0800602
603 if (local != nullptr && fromImport != nullptr && local != fromImport) {
604 // Something bad happen; two types have the same FQName.
605 std::cerr << "ERROR: Unable to resolve type name '"
606 << fqName.string()
607 << "' (i.e. '"
608 << autofilled.string()
609 << "'), multiple definitions found.\n";
610
611 return UNKNOWN_ERROR;
612 }
613 if (local != nullptr) {
614 *returnedType = local;
615 return OK;
616 }
617 // If fromImport is nullptr as well, return nullptr to fall through to next rule.
618 *returnedType = fromImport;
619 return OK;
620}
621
622// Rule 2: look at imports
623Type *AST::lookupTypeFromImports(const FQName &fqName) {
624
Andreas Huber39fa7182016-08-19 14:27:33 -0700625 Type *resolvedType = nullptr;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700626 Type *returnedType = nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700627 FQName resolvedName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700628
Andreas Huber39fa7182016-08-19 14:27:33 -0700629 for (const auto &importedAST : mImportedASTs) {
Yifan Hong1977ea32016-10-05 12:49:08 -0700630 if (mImportedTypes.find(importedAST) != mImportedTypes.end()) {
631 // ignore single type imports
632 continue;
633 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700634 FQName matchingName;
635 Type *match = importedAST->findDefinedType(fqName, &matchingName);
Andreas Huber84f89de2016-07-28 15:39:51 -0700636
Andreas Huber39fa7182016-08-19 14:27:33 -0700637 if (match != nullptr) {
638 if (resolvedType != nullptr) {
639 std::cerr << "ERROR: Unable to resolve type name '"
640 << fqName.string()
641 << "', multiple matches found:\n";
Andreas Huber737080b2016-08-02 15:38:04 -0700642
Andreas Huber39fa7182016-08-19 14:27:33 -0700643 std::cerr << " " << resolvedName.string() << "\n";
644 std::cerr << " " << matchingName.string() << "\n";
645
Yifan Hong1977ea32016-10-05 12:49:08 -0700646 return nullptr;
647 }
648
649 resolvedType = match;
650 returnedType = resolvedType;
651 resolvedName = matchingName;
652
653 // Keep going even after finding a match.
654 }
655 }
656
657 for (const auto &pair : mImportedTypes) {
658 AST *importedAST = pair.first;
659 std::set<Type *> importedTypes = pair.second;
660
661 FQName matchingName;
662 Type *match = importedAST->findDefinedType(fqName, &matchingName);
663 if (match != nullptr &&
664 importedTypes.find(match) != importedTypes.end()) {
665 if (resolvedType != nullptr) {
666 std::cerr << "ERROR: Unable to resolve type name '"
667 << fqName.string()
668 << "', multiple matches found:\n";
669
670 std::cerr << " " << resolvedName.string() << "\n";
671 std::cerr << " " << matchingName.string() << "\n";
672
673 return nullptr;
Andreas Huber39fa7182016-08-19 14:27:33 -0700674 }
675
676 resolvedType = match;
Iliyan Malchev800273d2016-09-02 15:25:07 -0700677 returnedType = resolvedType;
Andreas Huber39fa7182016-08-19 14:27:33 -0700678 resolvedName = matchingName;
679
680 // Keep going even after finding a match.
681 }
682 }
683
684 if (resolvedType) {
Iliyan Malchev800273d2016-09-02 15:25:07 -0700685 returnedType = resolvedType;
686
687 // If the resolved type is not an interface, we need to determine
688 // whether it is defined in types.hal, or in some other interface. In
689 // the latter case, we need to emit a dependency for the interface in
690 // which the type is defined.
691 //
692 // Consider the following:
693 // android.hardware.tests.foo@1.0::Record
694 // android.hardware.tests.foo@1.0::IFoo.Folder
695 // android.hardware.tests.foo@1.0::Folder
696 //
697 // If Record is an interface, then we keep track of it for the purpose
698 // of emitting dependencies in the target language (for example #include
699 // in C++). If Record is a UDT, then we assume it is defined in
700 // types.hal in android.hardware.tests.foo@1.0.
701 //
702 // In the case of IFoo.Folder, the same applies. If IFoo is an
703 // interface, we need to track this for the purpose of emitting
704 // dependencies. If not, then it must have been defined in types.hal.
705 //
706 // In the case of just specifying Folder, the resolved type is
Yifan Hongfece6ec2017-01-12 17:04:04 -0800707 // android.hardware.tests.foo@1.0::Folder, and the same logic as
Iliyan Malchev800273d2016-09-02 15:25:07 -0700708 // above applies.
709
710 if (!resolvedType->isInterface()) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800711 FQName ifc = resolvedName.getTopLevelType();
Iliyan Malchev800273d2016-09-02 15:25:07 -0700712 for (const auto &importedAST : mImportedASTs) {
713 FQName matchingName;
714 Type *match = importedAST->findDefinedType(ifc, &matchingName);
715 if (match != nullptr && match->isInterface()) {
716 resolvedType = match;
717 }
718 }
719 }
720
Andreas Huber39fa7182016-08-19 14:27:33 -0700721 if (!resolvedType->isInterface()) {
Andreas Huber0e00de42016-08-03 09:56:02 -0700722 // Non-interface types are declared in the associated types header.
Yifan Hongfece6ec2017-01-12 17:04:04 -0800723 FQName typesName = resolvedName.getTypesForPackage();
Andreas Huber39fa7182016-08-19 14:27:33 -0700724
Andreas Huber0e00de42016-08-03 09:56:02 -0700725 mImportedNames.insert(typesName);
726 } else {
Andreas Huberbfd76212016-08-09 11:12:16 -0700727 // Do _not_ use fqName, i.e. the name we used to look up the type,
728 // but instead use the name of the interface we found.
729 // This is necessary because if fqName pointed to a typedef which
730 // in turn referenced the found interface we'd mistakenly use the
731 // name of the typedef instead of the proper name of the interface.
732
Andreas Huber4ba5c972017-11-29 11:06:25 -0800733 const FQName &typeName =
734 static_cast<Interface *>(resolvedType)->fqName();
735
736 mImportedNames.insert(typeName);
Andreas Huber0e00de42016-08-03 09:56:02 -0700737 }
Andreas Huber737080b2016-08-02 15:38:04 -0700738 }
739
Steven Morelandbb5c80b2016-10-05 11:07:36 -0700740 return returnedType;
Andreas Huber5345ec22016-07-29 13:33:27 -0700741}
742
Andreas Huber4ba5c972017-11-29 11:06:25 -0800743void AST::addToImportedNamesGranular(const FQName &fqName) {
744 if (fqName.package() == package().package()
745 && fqName.version() == package().version()) {
746 // Our own names are _defined_ here, not imported.
747 return;
748 }
749
750 mImportedNamesGranular.insert(fqName);
751}
752
Andreas Huber39fa7182016-08-19 14:27:33 -0700753Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const {
Steven Morelandd537ab02016-09-12 10:32:01 -0700754 for (const auto &pair : mDefinedTypesByFullName) {
755 const FQName &key = pair.first;
756 Type* type = pair.second;
Andreas Huber5345ec22016-07-29 13:33:27 -0700757
Andreas Huber39fa7182016-08-19 14:27:33 -0700758 if (key.endsWith(fqName)) {
759 *matchingName = key;
Steven Morelandd537ab02016-09-12 10:32:01 -0700760 return type;
Andreas Huber5345ec22016-07-29 13:33:27 -0700761 }
Andreas Huber5345ec22016-07-29 13:33:27 -0700762 }
Andreas Huber39fa7182016-08-19 14:27:33 -0700763
764 return nullptr;
Andreas Huberc9410c72016-07-28 12:18:40 -0700765}
766
Neel Mehta55c065e2019-05-31 13:30:12 -0700767const std::vector<ImportStatement>& AST::getImportStatements() const {
768 return mImportStatements;
769}
770
Iliyan Malchev5bb14022016-08-09 15:04:39 -0700771void AST::getImportedPackages(std::set<FQName> *importSet) const {
Steven Moreland06a81cf2018-01-17 11:13:46 -0800772 for (const auto& fqName : mImportedNamesGranular) {
Yifan Hongfece6ec2017-01-12 17:04:04 -0800773 FQName packageName = fqName.getPackageAndVersion();
Andreas Huberd2943e12016-08-05 11:59:31 -0700774
775 if (packageName == mPackage) {
776 // We only care about external imports, not our own package.
777 continue;
778 }
779
780 importSet->insert(packageName);
781 }
782}
783
Yifan Hong40a373d2016-11-30 15:16:47 -0800784void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const {
785 getImportedPackages(importSet);
Steven Moreland06a81cf2018-01-17 11:13:46 -0800786
Yifan Hong40a373d2016-11-30 15:16:47 -0800787 std::set<FQName> newSet;
788 for (const auto &ast : mImportedASTs) {
789 if (importSet->find(ast->package()) != importSet->end()) {
790 ast->getImportedPackagesHierarchy(&newSet);
791 }
792 }
793 importSet->insert(newSet.begin(), newSet.end());
794}
795
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800796void AST::getAllImportedNames(std::set<FQName> *allImportNames) const {
797 for (const auto& name : mImportedNames) {
798 allImportNames->insert(name);
Steven Morelandc59326e2017-06-20 15:19:30 -0700799 AST* ast = mCoordinator->parse(name, nullptr /* imported */, Coordinator::Enforce::NONE);
Zhuoyao Zhangc4e10602017-01-27 16:48:05 -0800800 ast->getAllImportedNames(allImportNames);
801 }
802}
803
Andreas Huber4ba5c972017-11-29 11:06:25 -0800804void AST::getAllImportedNamesGranular(std::set<FQName> *allImportNames) const {
805 for (const auto& fqName : mImportedNamesGranular) {
806 if (fqName.name() == "types") {
807 // A package will export everything _defined_ but will not
808 // re-export anything it itself imported.
809 AST* ast = mCoordinator->parse(
810 fqName, nullptr /* imported */, Coordinator::Enforce::NONE);
811
812 ast->addDefinedTypes(allImportNames);
813 } else {
814 allImportNames->insert(fqName);
815 }
816 }
817}
818
Andreas Huber0fa9e392016-08-31 09:05:44 -0700819bool AST::isJavaCompatible() const {
Timur Iskhakov5dc72fe2017-09-07 23:13:44 -0700820 return mRootScope.isJavaCompatible();
Andreas Huber0fa9e392016-08-31 09:05:44 -0700821}
822
Andreas Huber019d21d2016-10-03 12:59:47 -0700823void AST::appendToExportedTypesVector(
824 std::vector<const Type *> *exportedTypes) const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700825 mRootScope.appendToExportedTypesVector(exportedTypes);
Andreas Huber019d21d2016-10-03 12:59:47 -0700826}
827
Yifan Hongc8934042016-11-17 17:10:52 -0800828bool AST::isIBase() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700829 Interface* iface = mRootScope.getInterface();
Yifan Hongc8934042016-11-17 17:10:52 -0800830 return iface != nullptr && iface->isIBase();
831}
832
Yifan Hong78b38d12017-02-13 18:14:46 +0000833const Interface *AST::getInterface() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700834 return mRootScope.getInterface();
Yifan Hong78b38d12017-02-13 18:14:46 +0000835}
836
Steven Moreland19f11b52017-05-12 18:22:21 -0700837std::string AST::getBaseName() const {
Timur Iskhakovcb0ba522017-07-17 20:01:37 -0700838 const Interface* iface = mRootScope.getInterface();
Steven Moreland19f11b52017-05-12 18:22:21 -0700839
840 return iface ? iface->getBaseName() : "types";
841}
842
Andreas Huber308d8a22017-11-06 14:46:52 -0800843void AST::addDefinedTypes(std::set<FQName> *definedTypes) const {
844 std::for_each(
845 mDefinedTypesByFullName.begin(),
846 mDefinedTypesByFullName.end(),
847 [definedTypes](const auto &elem) {
848 if (!elem.second->isTypeDef()) {
849 definedTypes->insert(elem.first);
850 }
851 });
852}
853
854void AST::addReferencedTypes(std::set<FQName> *referencedTypes) const {
855 std::for_each(
856 mReferencedTypeNames.begin(),
857 mReferencedTypeNames.end(),
858 [referencedTypes](const auto &fqName) {
859 referencedTypes->insert(fqName);
860 });
861}
862
Neel Mehta0ee353f2019-05-30 17:40:29 -0700863bool AST::addMethod(Method* method, Interface* iface) {
864 if (iface->isIBase()) {
865 if (!mAllReservedMethods.emplace(method->name(), method).second) {
866 std::cerr << "ERROR: hidl-gen encountered duplicated reserved method " << method->name()
867 << std::endl;
868 return false;
869 }
870
871 // methods will be added to iface in addAllReservedMethodsToInterface
872 return true;
873 }
874
875 iface->addUserDefinedMethod(method);
876
877 return true;
878}
879
880bool AST::addAllReservedMethodsToInterface(Interface* iface) {
881 std::map<std::string, Method*> allReservedMethods(mAllReservedMethods);
882 // Looking for the IBase AST which is imported for all interfaces that are not IBase
883 for (const AST* importedAST : mImportedASTs) {
884 allReservedMethods.insert(importedAST->mAllReservedMethods.begin(),
885 importedAST->mAllReservedMethods.end());
886 }
887
888 return iface->addAllReservedMethods(allReservedMethods);
889}
890
Steven Moreland4d89ee22019-03-08 13:25:32 -0800891void AST::setHeader(const DocComment* header) {
892 mHeader = header;
893}
894
895const DocComment* AST::getHeader() const {
896 return mHeader;
897}
898
899void AST::addUnhandledComment(const DocComment* docComment) {
900 if (docComment != nullptr) mUnhandledComments.push_back(docComment);
901}
902
903const std::vector<const DocComment*> AST::getUnhandledComments() const {
904 return mUnhandledComments;
905}
906
Andreas Huberc9410c72016-07-28 12:18:40 -0700907} // namespace android;