Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #include "AST.h" |
| 18 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 19 | #include "Coordinator.h" |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 20 | #include "EnumType.h" |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 21 | #include "FmqType.h" |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 22 | #include "HandleType.h" |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 23 | #include "Interface.h" |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 24 | #include "Location.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 25 | #include "Scope.h" |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 26 | #include "TypeDef.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 27 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 28 | #include <android-base/logging.h> |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 29 | #include <hidl-util/FQName.h> |
| 30 | #include <hidl-util/Formatter.h> |
| 31 | #include <hidl-util/StringHelper.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | #include <stdlib.h> |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 33 | #include <algorithm> |
| 34 | #include <iostream> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 35 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 36 | namespace android { |
| 37 | |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 38 | AST::AST(const Coordinator* coordinator, const Hash* fileHash) |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 39 | : mCoordinator(coordinator), |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 40 | mFileHash(fileHash), |
| 41 | mRootScope("(root scope)", FQName(), Location::startOf(fileHash->getPath()), |
| 42 | nullptr /* parent */) {} |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 43 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 44 | Scope* AST::getRootScope() { |
| 45 | return &mRootScope; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 48 | // used by the parser. |
| 49 | void AST::addSyntaxError() { |
| 50 | mSyntaxErrors++; |
| 51 | } |
| 52 | |
| 53 | size_t AST::syntaxErrors() const { |
| 54 | return mSyntaxErrors; |
| 55 | } |
| 56 | |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 57 | const std::string& AST::getFilename() const { |
| 58 | return mFileHash->getPath(); |
| 59 | } |
| 60 | const Hash* AST::getFileHash() const { |
| 61 | return mFileHash; |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 64 | bool AST::setPackage(const char *package) { |
Steven Moreland | e1b157e | 2018-03-06 14:18:32 -0800 | [diff] [blame] | 65 | if (!mPackage.setTo(package)) { |
| 66 | return false; |
| 67 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 68 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 69 | if (mPackage.package().empty() |
| 70 | || mPackage.version().empty() |
| 71 | || !mPackage.name().empty()) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 75 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 78 | FQName AST::package() const { |
| 79 | return mPackage; |
| 80 | } |
| 81 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 82 | bool AST::isInterface() const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 83 | return mRootScope.getInterface() != nullptr; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Steven Moreland | b47a262 | 2018-07-11 09:04:25 -0700 | [diff] [blame] | 86 | bool AST::definesInterfaces() const { |
| 87 | return mRootScope.definesInterfaces(); |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 90 | status_t AST::postParse() { |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 91 | status_t err; |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 92 | |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 93 | // lookupTypes is the first pass for references to be resolved. |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 94 | err = lookupTypes(); |
| 95 | if (err != OK) return err; |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 96 | |
| 97 | // Indicate that all types are now in "postParse" stage. |
| 98 | err = setParseStage(Type::ParseStage::PARSE, Type::ParseStage::POST_PARSE); |
| 99 | if (err != OK) return err; |
| 100 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 101 | // validateDefinedTypesUniqueNames is the first call |
| 102 | // after lookup, as other errors could appear because |
| 103 | // user meant different type than we assumed. |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 104 | err = validateDefinedTypesUniqueNames(); |
| 105 | if (err != OK) return err; |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 106 | // topologicalReorder is before resolveInheritance, as we |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 107 | // need to have no cycle while getting parent class. |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 108 | err = topologicalReorder(); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 109 | if (err != OK) return err; |
| 110 | err = resolveInheritance(); |
| 111 | if (err != OK) return err; |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 112 | err = lookupConstantExpressions(); |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 113 | if (err != OK) return err; |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 114 | // checkAcyclicConstantExpressions is after resolveInheritance, |
| 115 | // as resolveInheritance autofills enum values. |
| 116 | err = checkAcyclicConstantExpressions(); |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 117 | if (err != OK) return err; |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 118 | err = validateConstantExpressions(); |
| 119 | if (err != OK) return err; |
| 120 | err = evaluateConstantExpressions(); |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 121 | if (err != OK) return err; |
| 122 | err = validate(); |
| 123 | if (err != OK) return err; |
Timur Iskhakov | 041fdfe | 2017-09-06 15:56:01 -0700 | [diff] [blame] | 124 | err = checkForwardReferenceRestrictions(); |
| 125 | if (err != OK) return err; |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 126 | err = gatherReferencedTypes(); |
| 127 | if (err != OK) return err; |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 128 | |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 129 | // Make future packages not to call passes |
| 130 | // for processed types and expressions |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 131 | constantExpressionRecursivePass( |
| 132 | [](ConstantExpression* ce) { |
| 133 | ce->setPostParseCompleted(); |
| 134 | return OK; |
| 135 | }, |
| 136 | true /* processBeforeDependencies */); |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 137 | |
| 138 | err = setParseStage(Type::ParseStage::POST_PARSE, Type::ParseStage::COMPLETED); |
| 139 | if (err != OK) return err; |
Timur Iskhakov | 35930c4 | 2017-08-28 18:49:54 -0700 | [diff] [blame] | 140 | |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 141 | return OK; |
| 142 | } |
| 143 | |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 144 | status_t AST::constantExpressionRecursivePass( |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 145 | const std::function<status_t(ConstantExpression*)>& func, bool processBeforeDependencies) { |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 146 | std::unordered_set<const Type*> visitedTypes; |
| 147 | std::unordered_set<const ConstantExpression*> visitedCE; |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 148 | return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, |
| 149 | [&](Type* type) -> status_t { |
| 150 | for (auto* ce : type->getConstantExpressions()) { |
| 151 | status_t err = ce->recursivePass( |
| 152 | func, &visitedCE, processBeforeDependencies); |
| 153 | if (err != OK) return err; |
| 154 | } |
| 155 | return OK; |
| 156 | }, |
| 157 | &visitedTypes); |
| 158 | } |
| 159 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 160 | status_t AST::constantExpressionRecursivePass( |
| 161 | const std::function<status_t(const ConstantExpression*)>& func, |
| 162 | bool processBeforeDependencies) const { |
| 163 | std::unordered_set<const Type*> visitedTypes; |
| 164 | std::unordered_set<const ConstantExpression*> visitedCE; |
| 165 | return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, |
| 166 | [&](const Type* type) -> status_t { |
| 167 | for (auto* ce : type->getConstantExpressions()) { |
| 168 | status_t err = ce->recursivePass( |
| 169 | func, &visitedCE, processBeforeDependencies); |
| 170 | if (err != OK) return err; |
| 171 | } |
| 172 | return OK; |
| 173 | }, |
| 174 | &visitedTypes); |
| 175 | } |
| 176 | |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 177 | status_t AST::setParseStage(Type::ParseStage oldStage, Type::ParseStage newStage) { |
| 178 | std::unordered_set<const Type*> visited; |
| 179 | return mRootScope.recursivePass(oldStage, |
| 180 | [oldStage, newStage](Type* type) { |
| 181 | CHECK(type->getParseStage() == oldStage); |
| 182 | type->setParseStage(newStage); |
| 183 | return OK; |
| 184 | }, |
| 185 | &visited); |
Timur Iskhakov | 891a866 | 2017-08-25 21:53:48 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 188 | status_t AST::lookupTypes() { |
| 189 | std::unordered_set<const Type*> visited; |
| 190 | return mRootScope.recursivePass( |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 191 | Type::ParseStage::PARSE, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 192 | [&](Type* type) -> status_t { |
| 193 | Scope* scope = type->isScope() ? static_cast<Scope*>(type) : type->parent(); |
| 194 | |
| 195 | for (auto* nextRef : type->getReferences()) { |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 196 | if (nextRef->isResolved()) { |
| 197 | continue; |
| 198 | } |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 199 | |
| 200 | Type* nextType = lookupType(nextRef->getLookupFqName(), scope); |
| 201 | if (nextType == nullptr) { |
| 202 | std::cerr << "ERROR: Failed to lookup type '" |
| 203 | << nextRef->getLookupFqName().string() << "' at " |
| 204 | << nextRef->location() << "\n"; |
| 205 | return UNKNOWN_ERROR; |
| 206 | } |
| 207 | nextRef->set(nextType); |
| 208 | } |
| 209 | |
| 210 | return OK; |
| 211 | }, |
| 212 | &visited); |
| 213 | } |
| 214 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 215 | status_t AST::gatherReferencedTypes() { |
| 216 | std::unordered_set<const Type*> visited; |
| 217 | return mRootScope.recursivePass( |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 218 | Type::ParseStage::POST_PARSE, |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 219 | [&](Type* type) -> status_t { |
| 220 | for (auto* nextRef : type->getReferences()) { |
| 221 | const Type *targetType = nextRef->get(); |
| 222 | if (targetType->isNamedType()) { |
| 223 | mReferencedTypeNames.insert( |
| 224 | static_cast<const NamedType *>(targetType)->fqName()); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return OK; |
| 229 | }, |
| 230 | &visited); |
| 231 | } |
| 232 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 233 | status_t AST::lookupConstantExpressions() { |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 234 | std::unordered_set<const Type*> visitedTypes; |
| 235 | std::unordered_set<const ConstantExpression*> visitedCE; |
| 236 | |
| 237 | return mRootScope.recursivePass( |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 238 | Type::ParseStage::POST_PARSE, |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 239 | [&](Type* type) -> status_t { |
| 240 | Scope* scope = type->isScope() ? static_cast<Scope*>(type) : type->parent(); |
| 241 | |
| 242 | for (auto* ce : type->getConstantExpressions()) { |
| 243 | status_t err = ce->recursivePass( |
| 244 | [&](ConstantExpression* ce) { |
| 245 | for (auto* nextRef : ce->getReferences()) { |
| 246 | if (nextRef->isResolved()) continue; |
| 247 | |
| 248 | LocalIdentifier* iden = lookupLocalIdentifier(*nextRef, scope); |
| 249 | if (iden == nullptr) return UNKNOWN_ERROR; |
| 250 | nextRef->set(iden); |
| 251 | } |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 252 | for (auto* nextRef : ce->getTypeReferences()) { |
| 253 | if (nextRef->isResolved()) continue; |
| 254 | |
| 255 | Type* nextType = lookupType(nextRef->getLookupFqName(), scope); |
| 256 | if (nextType == nullptr) { |
| 257 | std::cerr << "ERROR: Failed to lookup type '" |
| 258 | << nextRef->getLookupFqName().string() << "' at " |
| 259 | << nextRef->location() << "\n"; |
| 260 | return UNKNOWN_ERROR; |
| 261 | } |
| 262 | nextRef->set(nextType); |
| 263 | } |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 264 | return OK; |
| 265 | }, |
| 266 | &visitedCE, true /* processBeforeDependencies */); |
| 267 | if (err != OK) return err; |
| 268 | } |
| 269 | |
| 270 | return OK; |
| 271 | }, |
| 272 | &visitedTypes); |
| 273 | } |
| 274 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 275 | status_t AST::validateDefinedTypesUniqueNames() const { |
| 276 | std::unordered_set<const Type*> visited; |
| 277 | return mRootScope.recursivePass( |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 278 | Type::ParseStage::POST_PARSE, |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 279 | [&](const Type* type) -> status_t { |
| 280 | // We only want to validate type definition names in this place. |
| 281 | if (type->isScope()) { |
| 282 | return static_cast<const Scope*>(type)->validateUniqueNames(); |
| 283 | } |
| 284 | return OK; |
| 285 | }, |
| 286 | &visited); |
| 287 | } |
| 288 | |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 289 | status_t AST::resolveInheritance() { |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 290 | std::unordered_set<const Type*> visited; |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 291 | return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, &Type::resolveInheritance, |
| 292 | &visited); |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Steven Moreland | 12f0ab1 | 2018-11-02 17:27:37 -0700 | [diff] [blame] | 295 | status_t AST::validateConstantExpressions() const { |
| 296 | return constantExpressionRecursivePass( |
| 297 | [](const ConstantExpression* ce) { return ce->validate(); }, |
| 298 | true /* processBeforeDependencies */); |
| 299 | } |
| 300 | |
| 301 | status_t AST::evaluateConstantExpressions() { |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 302 | return constantExpressionRecursivePass( |
| 303 | [](ConstantExpression* ce) { |
| 304 | ce->evaluate(); |
| 305 | return OK; |
| 306 | }, |
| 307 | false /* processBeforeDependencies */); |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | status_t AST::validate() const { |
Timur Iskhakov | 33431e6 | 2017-08-21 17:31:23 -0700 | [diff] [blame] | 311 | std::unordered_set<const Type*> visited; |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 312 | return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, &Type::validate, &visited); |
Timur Iskhakov | cec46c4 | 2017-08-09 00:22:02 -0700 | [diff] [blame] | 313 | } |
| 314 | |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 315 | status_t AST::topologicalReorder() { |
| 316 | std::unordered_map<const Type*, size_t> reversedOrder; |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 317 | std::unordered_set<const Type*> stack; |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 318 | status_t err = mRootScope.topologicalOrder(&reversedOrder, &stack).status; |
| 319 | if (err != OK) return err; |
| 320 | |
| 321 | std::unordered_set<const Type*> visited; |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 322 | mRootScope.recursivePass(Type::ParseStage::POST_PARSE, |
| 323 | [&](Type* type) { |
| 324 | if (type->isScope()) { |
| 325 | static_cast<Scope*>(type)->topologicalReorder(reversedOrder); |
| 326 | } |
| 327 | return OK; |
| 328 | }, |
| 329 | &visited); |
Timur Iskhakov | 458ca36 | 2017-09-12 23:16:03 -0700 | [diff] [blame] | 330 | return OK; |
Timur Iskhakov | 40731af | 2017-08-24 14:18:35 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 333 | status_t AST::checkAcyclicConstantExpressions() const { |
| 334 | std::unordered_set<const Type*> visitedTypes; |
| 335 | std::unordered_set<const ConstantExpression*> visitedCE; |
| 336 | std::unordered_set<const ConstantExpression*> stack; |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 337 | return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, |
| 338 | [&](const Type* type) -> status_t { |
| 339 | for (auto* ce : type->getConstantExpressions()) { |
| 340 | status_t err = |
| 341 | ce->checkAcyclic(&visitedCE, &stack).status; |
| 342 | CHECK(err != OK || stack.empty()); |
| 343 | if (err != OK) return err; |
| 344 | } |
| 345 | return OK; |
| 346 | }, |
| 347 | &visitedTypes); |
Timur Iskhakov | 77dd65c | 2017-08-31 22:46:56 -0700 | [diff] [blame] | 348 | } |
| 349 | |
Timur Iskhakov | 041fdfe | 2017-09-06 15:56:01 -0700 | [diff] [blame] | 350 | status_t AST::checkForwardReferenceRestrictions() const { |
| 351 | std::unordered_set<const Type*> visited; |
Yifan Hong | 0e192c4 | 2018-10-23 15:32:19 -0700 | [diff] [blame] | 352 | return mRootScope.recursivePass(Type::ParseStage::POST_PARSE, |
| 353 | [](const Type* type) -> status_t { |
| 354 | for (const Reference<Type>* ref : type->getReferences()) { |
| 355 | status_t err = |
| 356 | type->checkForwardReferenceRestrictions(*ref); |
| 357 | if (err != OK) return err; |
| 358 | } |
| 359 | return OK; |
| 360 | }, |
| 361 | &visited); |
Timur Iskhakov | 041fdfe | 2017-09-06 15:56:01 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 364 | bool AST::addImport(const char *import) { |
Steven Moreland | e1b157e | 2018-03-06 14:18:32 -0800 | [diff] [blame] | 365 | FQName fqName; |
| 366 | if (!FQName::parse(import, &fqName)) { |
Steven Moreland | 01857a3 | 2018-01-03 10:15:01 -0800 | [diff] [blame] | 367 | std::cerr << "ERROR: '" << import << "' is an invalid fully-qualified name." << std::endl; |
| 368 | return false; |
| 369 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 370 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 371 | fqName.applyDefaults(mPackage.package(), mPackage.version()); |
| 372 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 373 | if (fqName.name().empty()) { |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 374 | // import a package |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 375 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 376 | std::vector<FQName> packageInterfaces; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 377 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 378 | status_t err = |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 379 | mCoordinator->appendPackageInterfacesToVector(fqName, |
| 380 | &packageInterfaces); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 381 | |
| 382 | if (err != OK) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 386 | for (const auto &subFQName : packageInterfaces) { |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 387 | addToImportedNamesGranular(subFQName); |
| 388 | |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 389 | // Do not enforce restrictions on imports. |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 390 | AST* ast = mCoordinator->parse(subFQName, &mImportedASTs, Coordinator::Enforce::NONE); |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 391 | if (ast == nullptr) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 392 | return false; |
| 393 | } |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 394 | // all previous single type imports are ignored. |
| 395 | mImportedTypes.erase(ast); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | return true; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 401 | addToImportedNamesGranular(fqName); |
| 402 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 403 | // cases like android.hardware.foo@1.0::IFoo.Internal |
| 404 | // android.hardware.foo@1.0::Abc.Internal |
| 405 | |
| 406 | // assume it is an interface, and try to import it. |
| 407 | const FQName interfaceName = fqName.getTopLevelType(); |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 408 | // Do not enforce restrictions on imports. |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 409 | AST* importAST; |
| 410 | status_t err = mCoordinator->parseOptional(interfaceName, &importAST, &mImportedASTs, |
| 411 | Coordinator::Enforce::NONE); |
| 412 | if (err != OK) return false; |
| 413 | // importAST nullptr == file doesn't exist |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 414 | |
| 415 | if (importAST != nullptr) { |
| 416 | // cases like android.hardware.foo@1.0::IFoo.Internal |
| 417 | // and android.hardware.foo@1.0::IFoo |
| 418 | if (fqName == interfaceName) { |
| 419 | // import a single file. |
| 420 | // all previous single type imports are ignored. |
| 421 | // cases like android.hardware.foo@1.0::IFoo |
| 422 | // and android.hardware.foo@1.0::types |
| 423 | mImportedTypes.erase(importAST); |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | // import a single type from this file |
| 428 | // cases like android.hardware.foo@1.0::IFoo.Internal |
| 429 | FQName matchingName; |
| 430 | Type *match = importAST->findDefinedType(fqName, &matchingName); |
| 431 | if (match == nullptr) { |
| 432 | return false; |
| 433 | } |
| 434 | // will automatically create a set if it does not exist |
| 435 | mImportedTypes[importAST].insert(match); |
| 436 | return true; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 437 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 438 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 439 | // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal |
| 440 | FQName typesFQName = fqName.getTypesForPackage(); |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 441 | |
| 442 | // Do not enforce restrictions on imports. |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 443 | importAST = mCoordinator->parse(typesFQName, &mImportedASTs, Coordinator::Enforce::NONE); |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 444 | |
| 445 | if (importAST != nullptr) { |
| 446 | // Attempt to find Abc.Internal in types. |
| 447 | FQName matchingName; |
| 448 | Type *match = importAST->findDefinedType(fqName, &matchingName); |
| 449 | if (match == nullptr) { |
| 450 | return false; |
| 451 | } |
| 452 | // will automatically create a set if not exist |
| 453 | mImportedTypes[importAST].insert(match); |
| 454 | return true; |
| 455 | } |
| 456 | |
| 457 | // can't find an appropriate AST for fqName. |
| 458 | return false; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 461 | void AST::addImportedAST(AST *ast) { |
| 462 | mImportedASTs.insert(ast); |
| 463 | } |
| 464 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 465 | FQName AST::makeFullName(const char* localName, Scope* scope) const { |
| 466 | std::vector<std::string> pathComponents{{localName}}; |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 467 | for (; scope != &mRootScope; scope = scope->parent()) { |
| 468 | pathComponents.push_back(scope->localName()); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 469 | } |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 470 | |
| 471 | std::reverse(pathComponents.begin(), pathComponents.end()); |
| 472 | std::string path = StringHelper::JoinStrings(pathComponents, "."); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 473 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 474 | return FQName(mPackage.package(), mPackage.version(), path); |
| 475 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 476 | |
Timur Iskhakov | 565b013 | 2017-09-06 18:07:11 -0700 | [diff] [blame] | 477 | void AST::addScopedType(NamedType* type, Scope* scope) { |
| 478 | scope->addType(type); |
| 479 | mDefinedTypesByFullName[type->fqName()] = type; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Timur Iskhakov | 82c048e | 2017-09-09 01:20:53 -0700 | [diff] [blame] | 482 | LocalIdentifier* AST::lookupLocalIdentifier(const Reference<LocalIdentifier>& ref, Scope* scope) { |
| 483 | const FQName& fqName = ref.getLookupFqName(); |
| 484 | |
| 485 | if (fqName.isIdentifier()) { |
| 486 | LocalIdentifier* iden = scope->lookupIdentifier(fqName.name()); |
| 487 | if (iden == nullptr) { |
| 488 | std::cerr << "ERROR: identifier " << fqName.string() << " could not be found at " |
| 489 | << ref.location() << "\n"; |
| 490 | return nullptr; |
| 491 | } |
| 492 | return iden; |
| 493 | } else { |
| 494 | std::string errorMsg; |
| 495 | EnumValue* enumValue = lookupEnumValue(fqName, &errorMsg, scope); |
| 496 | if (enumValue == nullptr) { |
| 497 | std::cerr << "ERROR: " << errorMsg << " at " << ref.location() << "\n"; |
| 498 | return nullptr; |
| 499 | } |
| 500 | return enumValue; |
| 501 | } |
| 502 | } |
| 503 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 504 | EnumValue* AST::lookupEnumValue(const FQName& fqName, std::string* errorMsg, Scope* scope) { |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 505 | FQName enumTypeName = fqName.typeName(); |
| 506 | std::string enumValueName = fqName.valueName(); |
| 507 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 508 | CHECK(!enumValueName.empty()); |
| 509 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 510 | Type* type = lookupType(enumTypeName, scope); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 511 | if(type == nullptr) { |
| 512 | *errorMsg = "Cannot find type " + enumTypeName.string(); |
| 513 | return nullptr; |
| 514 | } |
Steven Moreland | d965ce9 | 2017-11-27 16:05:34 -0800 | [diff] [blame] | 515 | type = type->resolve(); |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 516 | if(!type->isEnum()) { |
| 517 | *errorMsg = "Type " + enumTypeName.string() + " is not an enum type"; |
| 518 | return nullptr; |
| 519 | } |
| 520 | |
| 521 | EnumType *enumType = static_cast<EnumType *>(type); |
| 522 | EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName)); |
| 523 | if(v == nullptr) { |
| 524 | *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName; |
| 525 | return nullptr; |
| 526 | } |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 527 | |
| 528 | mReferencedTypeNames.insert(enumType->fqName()); |
| 529 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 530 | return v; |
| 531 | } |
| 532 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 533 | Type* AST::lookupType(const FQName& fqName, Scope* scope) { |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 534 | if (fqName.name().empty()) { |
| 535 | // Given a package and version??? |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 536 | return nullptr; |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 539 | Type *returnedType = nullptr; |
| 540 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 541 | if (fqName.package().empty() && fqName.version().empty()) { |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 542 | // resolve locally first if possible. |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 543 | returnedType = lookupTypeLocally(fqName, scope); |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 544 | if (returnedType != nullptr) { |
| 545 | return returnedType; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 546 | } |
| 547 | } |
| 548 | |
Steven Moreland | 87e69dc | 2017-09-12 18:14:28 -0700 | [diff] [blame] | 549 | status_t status = lookupAutofilledType(fqName, &returnedType); |
| 550 | if (status != OK) { |
| 551 | return nullptr; |
| 552 | } |
| 553 | if (returnedType != nullptr) { |
| 554 | return returnedType; |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return lookupTypeFromImports(fqName); |
| 558 | } |
| 559 | |
| 560 | // Rule 0: try resolve locally |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 561 | Type* AST::lookupTypeLocally(const FQName& fqName, Scope* scope) { |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 562 | CHECK(fqName.package().empty() && fqName.version().empty() |
| 563 | && !fqName.name().empty() && fqName.valueName().empty()); |
| 564 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 565 | for (; scope != nullptr; scope = scope->parent()) { |
| 566 | Type* type = scope->lookupType(fqName); |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 567 | if (type != nullptr) { |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 568 | return type; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | return nullptr; |
| 573 | } |
| 574 | |
| 575 | // Rule 1: auto-fill with current package |
| 576 | status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) { |
Steven Moreland | 87e69dc | 2017-09-12 18:14:28 -0700 | [diff] [blame] | 577 | CHECK(!fqName.name().empty() && fqName.valueName().empty()); |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 578 | |
| 579 | FQName autofilled = fqName; |
| 580 | autofilled.applyDefaults(mPackage.package(), mPackage.version()); |
| 581 | FQName matchingName; |
| 582 | // Given this fully-qualified name, the type may be defined in this AST, or other files |
| 583 | // in import. |
| 584 | Type *local = findDefinedType(autofilled, &matchingName); |
| 585 | CHECK(local == nullptr || autofilled == matchingName); |
Steven Moreland | 87e69dc | 2017-09-12 18:14:28 -0700 | [diff] [blame] | 586 | Type* fromImport = lookupTypeFromImports(autofilled); |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 587 | |
| 588 | if (local != nullptr && fromImport != nullptr && local != fromImport) { |
| 589 | // Something bad happen; two types have the same FQName. |
| 590 | std::cerr << "ERROR: Unable to resolve type name '" |
| 591 | << fqName.string() |
| 592 | << "' (i.e. '" |
| 593 | << autofilled.string() |
| 594 | << "'), multiple definitions found.\n"; |
| 595 | |
| 596 | return UNKNOWN_ERROR; |
| 597 | } |
| 598 | if (local != nullptr) { |
| 599 | *returnedType = local; |
| 600 | return OK; |
| 601 | } |
| 602 | // If fromImport is nullptr as well, return nullptr to fall through to next rule. |
| 603 | *returnedType = fromImport; |
| 604 | return OK; |
| 605 | } |
| 606 | |
| 607 | // Rule 2: look at imports |
| 608 | Type *AST::lookupTypeFromImports(const FQName &fqName) { |
| 609 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 610 | Type *resolvedType = nullptr; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 611 | Type *returnedType = nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 612 | FQName resolvedName; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 613 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 614 | for (const auto &importedAST : mImportedASTs) { |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 615 | if (mImportedTypes.find(importedAST) != mImportedTypes.end()) { |
| 616 | // ignore single type imports |
| 617 | continue; |
| 618 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 619 | FQName matchingName; |
| 620 | Type *match = importedAST->findDefinedType(fqName, &matchingName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 621 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 622 | if (match != nullptr) { |
| 623 | if (resolvedType != nullptr) { |
| 624 | std::cerr << "ERROR: Unable to resolve type name '" |
| 625 | << fqName.string() |
| 626 | << "', multiple matches found:\n"; |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 627 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 628 | std::cerr << " " << resolvedName.string() << "\n"; |
| 629 | std::cerr << " " << matchingName.string() << "\n"; |
| 630 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 631 | return nullptr; |
| 632 | } |
| 633 | |
| 634 | resolvedType = match; |
| 635 | returnedType = resolvedType; |
| 636 | resolvedName = matchingName; |
| 637 | |
| 638 | // Keep going even after finding a match. |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | for (const auto &pair : mImportedTypes) { |
| 643 | AST *importedAST = pair.first; |
| 644 | std::set<Type *> importedTypes = pair.second; |
| 645 | |
| 646 | FQName matchingName; |
| 647 | Type *match = importedAST->findDefinedType(fqName, &matchingName); |
| 648 | if (match != nullptr && |
| 649 | importedTypes.find(match) != importedTypes.end()) { |
| 650 | if (resolvedType != nullptr) { |
| 651 | std::cerr << "ERROR: Unable to resolve type name '" |
| 652 | << fqName.string() |
| 653 | << "', multiple matches found:\n"; |
| 654 | |
| 655 | std::cerr << " " << resolvedName.string() << "\n"; |
| 656 | std::cerr << " " << matchingName.string() << "\n"; |
| 657 | |
| 658 | return nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | resolvedType = match; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 662 | returnedType = resolvedType; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 663 | resolvedName = matchingName; |
| 664 | |
| 665 | // Keep going even after finding a match. |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | if (resolvedType) { |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 670 | returnedType = resolvedType; |
| 671 | |
| 672 | // If the resolved type is not an interface, we need to determine |
| 673 | // whether it is defined in types.hal, or in some other interface. In |
| 674 | // the latter case, we need to emit a dependency for the interface in |
| 675 | // which the type is defined. |
| 676 | // |
| 677 | // Consider the following: |
| 678 | // android.hardware.tests.foo@1.0::Record |
| 679 | // android.hardware.tests.foo@1.0::IFoo.Folder |
| 680 | // android.hardware.tests.foo@1.0::Folder |
| 681 | // |
| 682 | // If Record is an interface, then we keep track of it for the purpose |
| 683 | // of emitting dependencies in the target language (for example #include |
| 684 | // in C++). If Record is a UDT, then we assume it is defined in |
| 685 | // types.hal in android.hardware.tests.foo@1.0. |
| 686 | // |
| 687 | // In the case of IFoo.Folder, the same applies. If IFoo is an |
| 688 | // interface, we need to track this for the purpose of emitting |
| 689 | // dependencies. If not, then it must have been defined in types.hal. |
| 690 | // |
| 691 | // In the case of just specifying Folder, the resolved type is |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 692 | // android.hardware.tests.foo@1.0::Folder, and the same logic as |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 693 | // above applies. |
| 694 | |
| 695 | if (!resolvedType->isInterface()) { |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 696 | FQName ifc = resolvedName.getTopLevelType(); |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 697 | for (const auto &importedAST : mImportedASTs) { |
| 698 | FQName matchingName; |
| 699 | Type *match = importedAST->findDefinedType(ifc, &matchingName); |
| 700 | if (match != nullptr && match->isInterface()) { |
| 701 | resolvedType = match; |
| 702 | } |
| 703 | } |
| 704 | } |
| 705 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 706 | if (!resolvedType->isInterface()) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 707 | // Non-interface types are declared in the associated types header. |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 708 | FQName typesName = resolvedName.getTypesForPackage(); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 709 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 710 | mImportedNames.insert(typesName); |
| 711 | } else { |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 712 | // Do _not_ use fqName, i.e. the name we used to look up the type, |
| 713 | // but instead use the name of the interface we found. |
| 714 | // This is necessary because if fqName pointed to a typedef which |
| 715 | // in turn referenced the found interface we'd mistakenly use the |
| 716 | // name of the typedef instead of the proper name of the interface. |
| 717 | |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 718 | const FQName &typeName = |
| 719 | static_cast<Interface *>(resolvedType)->fqName(); |
| 720 | |
| 721 | mImportedNames.insert(typeName); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 722 | } |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Steven Moreland | bb5c80b | 2016-10-05 11:07:36 -0700 | [diff] [blame] | 725 | return returnedType; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 728 | void AST::addToImportedNamesGranular(const FQName &fqName) { |
| 729 | if (fqName.package() == package().package() |
| 730 | && fqName.version() == package().version()) { |
| 731 | // Our own names are _defined_ here, not imported. |
| 732 | return; |
| 733 | } |
| 734 | |
| 735 | mImportedNamesGranular.insert(fqName); |
| 736 | } |
| 737 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 738 | Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 739 | for (const auto &pair : mDefinedTypesByFullName) { |
| 740 | const FQName &key = pair.first; |
| 741 | Type* type = pair.second; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 742 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 743 | if (key.endsWith(fqName)) { |
| 744 | *matchingName = key; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 745 | return type; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 746 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 747 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 748 | |
| 749 | return nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 752 | void AST::getImportedPackages(std::set<FQName> *importSet) const { |
Steven Moreland | 06a81cf | 2018-01-17 11:13:46 -0800 | [diff] [blame] | 753 | for (const auto& fqName : mImportedNamesGranular) { |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 754 | FQName packageName = fqName.getPackageAndVersion(); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 755 | |
| 756 | if (packageName == mPackage) { |
| 757 | // We only care about external imports, not our own package. |
| 758 | continue; |
| 759 | } |
| 760 | |
| 761 | importSet->insert(packageName); |
| 762 | } |
| 763 | } |
| 764 | |
Yifan Hong | 40a373d | 2016-11-30 15:16:47 -0800 | [diff] [blame] | 765 | void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const { |
| 766 | getImportedPackages(importSet); |
Steven Moreland | 06a81cf | 2018-01-17 11:13:46 -0800 | [diff] [blame] | 767 | |
Yifan Hong | 40a373d | 2016-11-30 15:16:47 -0800 | [diff] [blame] | 768 | std::set<FQName> newSet; |
| 769 | for (const auto &ast : mImportedASTs) { |
| 770 | if (importSet->find(ast->package()) != importSet->end()) { |
| 771 | ast->getImportedPackagesHierarchy(&newSet); |
| 772 | } |
| 773 | } |
| 774 | importSet->insert(newSet.begin(), newSet.end()); |
| 775 | } |
| 776 | |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 777 | void AST::getAllImportedNames(std::set<FQName> *allImportNames) const { |
| 778 | for (const auto& name : mImportedNames) { |
| 779 | allImportNames->insert(name); |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 780 | AST* ast = mCoordinator->parse(name, nullptr /* imported */, Coordinator::Enforce::NONE); |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 781 | ast->getAllImportedNames(allImportNames); |
| 782 | } |
| 783 | } |
| 784 | |
Andreas Huber | 4ba5c97 | 2017-11-29 11:06:25 -0800 | [diff] [blame] | 785 | void AST::getAllImportedNamesGranular(std::set<FQName> *allImportNames) const { |
| 786 | for (const auto& fqName : mImportedNamesGranular) { |
| 787 | if (fqName.name() == "types") { |
| 788 | // A package will export everything _defined_ but will not |
| 789 | // re-export anything it itself imported. |
| 790 | AST* ast = mCoordinator->parse( |
| 791 | fqName, nullptr /* imported */, Coordinator::Enforce::NONE); |
| 792 | |
| 793 | ast->addDefinedTypes(allImportNames); |
| 794 | } else { |
| 795 | allImportNames->insert(fqName); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 800 | bool AST::isJavaCompatible() const { |
Timur Iskhakov | 5dc72fe | 2017-09-07 23:13:44 -0700 | [diff] [blame] | 801 | return mRootScope.isJavaCompatible(); |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 804 | void AST::appendToExportedTypesVector( |
| 805 | std::vector<const Type *> *exportedTypes) const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 806 | mRootScope.appendToExportedTypesVector(exportedTypes); |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 807 | } |
| 808 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 809 | bool AST::isIBase() const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 810 | Interface* iface = mRootScope.getInterface(); |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 811 | return iface != nullptr && iface->isIBase(); |
| 812 | } |
| 813 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 814 | const Interface *AST::getInterface() const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 815 | return mRootScope.getInterface(); |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 818 | std::string AST::getBaseName() const { |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 819 | const Interface* iface = mRootScope.getInterface(); |
Steven Moreland | 19f11b5 | 2017-05-12 18:22:21 -0700 | [diff] [blame] | 820 | |
| 821 | return iface ? iface->getBaseName() : "types"; |
| 822 | } |
| 823 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 824 | void AST::addDefinedTypes(std::set<FQName> *definedTypes) const { |
| 825 | std::for_each( |
| 826 | mDefinedTypesByFullName.begin(), |
| 827 | mDefinedTypesByFullName.end(), |
| 828 | [definedTypes](const auto &elem) { |
| 829 | if (!elem.second->isTypeDef()) { |
| 830 | definedTypes->insert(elem.first); |
| 831 | } |
| 832 | }); |
| 833 | } |
| 834 | |
| 835 | void AST::addReferencedTypes(std::set<FQName> *referencedTypes) const { |
| 836 | std::for_each( |
| 837 | mReferencedTypeNames.begin(), |
| 838 | mReferencedTypeNames.end(), |
| 839 | [referencedTypes](const auto &fqName) { |
| 840 | referencedTypes->insert(fqName); |
| 841 | }); |
| 842 | } |
| 843 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 844 | } // namespace android; |