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" |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 21 | #include "HandleType.h" |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 22 | #include "Interface.h" |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 23 | #include "Location.h" |
Hridya Valsaraju | a32bde8 | 2016-12-27 11:47:46 -0800 | [diff] [blame] | 24 | #include "FmqType.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 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 28 | #include <hidl-util/Formatter.h> |
Steven Moreland | 7ae3d54 | 2017-01-18 16:46:01 -0800 | [diff] [blame] | 29 | #include <hidl-util/FQName.h> |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 30 | #include <android-base/logging.h> |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 31 | #include <iostream> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | #include <stdlib.h> |
| 33 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 36 | AST::AST(Coordinator *coordinator, const std::string &path) |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 37 | : mCoordinator(coordinator), |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 38 | mPath(path), |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 39 | mScanner(NULL), |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 40 | mRootScope(new Scope("" /* localName */, Location::startOf(path))) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 41 | enterScope(mRootScope); |
| 42 | } |
| 43 | |
| 44 | AST::~AST() { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 45 | delete mRootScope; |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 46 | mRootScope = nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 47 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 48 | CHECK(mScanner == NULL); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 49 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 50 | // Ownership of "coordinator" was NOT transferred. |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Yifan Hong | be627b3 | 2016-10-28 18:38:56 -0700 | [diff] [blame] | 53 | // used by the parser. |
| 54 | void AST::addSyntaxError() { |
| 55 | mSyntaxErrors++; |
| 56 | } |
| 57 | |
| 58 | size_t AST::syntaxErrors() const { |
| 59 | return mSyntaxErrors; |
| 60 | } |
| 61 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 62 | void *AST::scanner() { |
| 63 | return mScanner; |
| 64 | } |
| 65 | |
| 66 | void AST::setScanner(void *scanner) { |
| 67 | mScanner = scanner; |
| 68 | } |
| 69 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 70 | const std::string &AST::getFilename() const { |
| 71 | return mPath; |
| 72 | } |
| 73 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 74 | bool AST::setPackage(const char *package) { |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 75 | mPackage.setTo(package); |
| 76 | CHECK(mPackage.isValid()); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 77 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 78 | if (mPackage.package().empty() |
| 79 | || mPackage.version().empty() |
| 80 | || !mPackage.name().empty()) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 81 | return false; |
| 82 | } |
| 83 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 84 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 87 | FQName AST::package() const { |
| 88 | return mPackage; |
| 89 | } |
| 90 | |
| 91 | bool AST::isInterface(std::string *ifaceName) const { |
| 92 | return mRootScope->containsSingleInterface(ifaceName); |
| 93 | } |
| 94 | |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 95 | bool AST::containsInterfaces() const { |
| 96 | return mRootScope->containsInterfaces(); |
| 97 | } |
| 98 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 99 | bool AST::addImport(const char *import) { |
| 100 | FQName fqName(import); |
| 101 | CHECK(fqName.isValid()); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 102 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 103 | fqName.applyDefaults(mPackage.package(), mPackage.version()); |
| 104 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 105 | // LOG(INFO) << "importing " << fqName.string(); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 106 | |
| 107 | if (fqName.name().empty()) { |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 108 | // import a package |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 109 | std::vector<FQName> packageInterfaces; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 110 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 111 | status_t err = |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 112 | mCoordinator->appendPackageInterfacesToVector(fqName, |
| 113 | &packageInterfaces); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 114 | |
| 115 | if (err != OK) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 116 | return false; |
| 117 | } |
| 118 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 119 | for (const auto &subFQName : packageInterfaces) { |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 120 | // Do not enforce restrictions on imports. |
| 121 | AST *ast = mCoordinator->parse(subFQName, &mImportedASTs, false /* enforce */); |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 122 | if (ast == nullptr) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 123 | return false; |
| 124 | } |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 125 | // all previous single type imports are ignored. |
| 126 | mImportedTypes.erase(ast); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | return true; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 132 | AST *importAST; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 133 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 134 | // cases like android.hardware.foo@1.0::IFoo.Internal |
| 135 | // android.hardware.foo@1.0::Abc.Internal |
| 136 | |
| 137 | // assume it is an interface, and try to import it. |
| 138 | const FQName interfaceName = fqName.getTopLevelType(); |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 139 | // Do not enforce restrictions on imports. |
| 140 | importAST = mCoordinator->parse(interfaceName, &mImportedASTs, false /* enforce */); |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 141 | |
| 142 | if (importAST != nullptr) { |
| 143 | // cases like android.hardware.foo@1.0::IFoo.Internal |
| 144 | // and android.hardware.foo@1.0::IFoo |
| 145 | if (fqName == interfaceName) { |
| 146 | // import a single file. |
| 147 | // all previous single type imports are ignored. |
| 148 | // cases like android.hardware.foo@1.0::IFoo |
| 149 | // and android.hardware.foo@1.0::types |
| 150 | mImportedTypes.erase(importAST); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | // import a single type from this file |
| 155 | // cases like android.hardware.foo@1.0::IFoo.Internal |
| 156 | FQName matchingName; |
| 157 | Type *match = importAST->findDefinedType(fqName, &matchingName); |
| 158 | if (match == nullptr) { |
| 159 | return false; |
| 160 | } |
| 161 | // will automatically create a set if it does not exist |
| 162 | mImportedTypes[importAST].insert(match); |
| 163 | return true; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 164 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 165 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 166 | // probably a type in types.hal, like android.hardware.foo@1.0::Abc.Internal |
| 167 | FQName typesFQName = fqName.getTypesForPackage(); |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 168 | |
| 169 | // Do not enforce restrictions on imports. |
| 170 | importAST = mCoordinator->parse(typesFQName, &mImportedASTs, false /* enforce */); |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 171 | |
| 172 | if (importAST != nullptr) { |
| 173 | // Attempt to find Abc.Internal in types. |
| 174 | FQName matchingName; |
| 175 | Type *match = importAST->findDefinedType(fqName, &matchingName); |
| 176 | if (match == nullptr) { |
| 177 | return false; |
| 178 | } |
| 179 | // will automatically create a set if not exist |
| 180 | mImportedTypes[importAST].insert(match); |
| 181 | return true; |
| 182 | } |
| 183 | |
| 184 | // can't find an appropriate AST for fqName. |
| 185 | return false; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 188 | void AST::addImportedAST(AST *ast) { |
| 189 | mImportedASTs.insert(ast); |
| 190 | } |
| 191 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 192 | void AST::enterScope(Scope *container) { |
| 193 | mScopePath.push_back(container); |
| 194 | } |
| 195 | |
| 196 | void AST::leaveScope() { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 197 | mScopePath.pop_back(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | Scope *AST::scope() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 201 | CHECK(!mScopePath.empty()); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 202 | return mScopePath.back(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 205 | bool AST::addTypeDef(const char *localName, Type *type, const Location &location, |
| 206 | std::string *errorMsg) { |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 207 | // The reason we wrap the given type in a TypeDef is simply to suppress |
| 208 | // emitting any type definitions later on, since this is just an alias |
| 209 | // to a type defined elsewhere. |
| 210 | return addScopedTypeInternal( |
Yifan Hong | a4b53d0 | 2016-10-31 17:29:10 -0700 | [diff] [blame] | 211 | new TypeDef(localName, location, type), errorMsg); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 214 | bool AST::addScopedType(NamedType *type, std::string *errorMsg) { |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 215 | return addScopedTypeInternal( |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 216 | type, errorMsg); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | bool AST::addScopedTypeInternal( |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 220 | NamedType *type, |
| 221 | std::string *errorMsg) { |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 222 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 223 | bool success = scope()->addType(type, errorMsg); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 224 | if (!success) { |
| 225 | return false; |
| 226 | } |
| 227 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 228 | std::string path; |
| 229 | for (size_t i = 1; i < mScopePath.size(); ++i) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 230 | path.append(mScopePath[i]->localName()); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 231 | path.append("."); |
| 232 | } |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 233 | path.append(type->localName()); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 234 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 235 | FQName fqName(mPackage.package(), mPackage.version(), path); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 236 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 237 | type->setFullName(fqName); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 238 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 239 | mDefinedTypesByFullName[fqName] = type; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 240 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 241 | return true; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 242 | } |
| 243 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 244 | EnumValue *AST::lookupEnumValue(const FQName &fqName, std::string *errorMsg) { |
| 245 | |
| 246 | FQName enumTypeName = fqName.typeName(); |
| 247 | std::string enumValueName = fqName.valueName(); |
| 248 | |
| 249 | CHECK(enumTypeName.isValid()); |
| 250 | CHECK(!enumValueName.empty()); |
| 251 | |
| 252 | Type *type = lookupType(enumTypeName); |
| 253 | if(type == nullptr) { |
| 254 | *errorMsg = "Cannot find type " + enumTypeName.string(); |
| 255 | return nullptr; |
| 256 | } |
| 257 | if(!type->isEnum()) { |
| 258 | *errorMsg = "Type " + enumTypeName.string() + " is not an enum type"; |
| 259 | return nullptr; |
| 260 | } |
| 261 | |
| 262 | EnumType *enumType = static_cast<EnumType *>(type); |
| 263 | EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName)); |
| 264 | if(v == nullptr) { |
| 265 | *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName; |
| 266 | return nullptr; |
| 267 | } |
| 268 | return v; |
| 269 | } |
| 270 | |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 271 | Type *AST::lookupType(const FQName &fqName) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 272 | CHECK(fqName.isValid()); |
| 273 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 274 | if (fqName.name().empty()) { |
| 275 | // Given a package and version??? |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 276 | return nullptr; |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 279 | Type *returnedType = nullptr; |
| 280 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 281 | if (fqName.package().empty() && fqName.version().empty()) { |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 282 | // resolve locally first if possible. |
| 283 | returnedType = lookupTypeLocally(fqName); |
| 284 | if (returnedType != nullptr) { |
| 285 | return returnedType; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
Yifan Hong | 87ff823 | 2017-01-09 12:07:05 -0800 | [diff] [blame] | 289 | if (!fqName.isFullyQualified()) { |
| 290 | status_t status = lookupAutofilledType(fqName, &returnedType); |
| 291 | if (status != OK) { |
| 292 | return nullptr; |
| 293 | } |
| 294 | if (returnedType != nullptr) { |
| 295 | return returnedType; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return lookupTypeFromImports(fqName); |
| 300 | } |
| 301 | |
| 302 | // Rule 0: try resolve locally |
| 303 | Type *AST::lookupTypeLocally(const FQName &fqName) { |
| 304 | CHECK(fqName.package().empty() && fqName.version().empty() |
| 305 | && !fqName.name().empty() && fqName.valueName().empty()); |
| 306 | |
| 307 | for (size_t i = mScopePath.size(); i-- > 0;) { |
| 308 | Type *type = mScopePath[i]->lookupType(fqName); |
| 309 | |
| 310 | if (type != nullptr) { |
| 311 | // Resolve typeDefs to the target type. |
| 312 | while (type->isTypeDef()) { |
| 313 | type = static_cast<TypeDef *>(type)->referencedType(); |
| 314 | } |
| 315 | |
| 316 | return type; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | return nullptr; |
| 321 | } |
| 322 | |
| 323 | // Rule 1: auto-fill with current package |
| 324 | status_t AST::lookupAutofilledType(const FQName &fqName, Type **returnedType) { |
| 325 | CHECK(!fqName.isFullyQualified() && !fqName.name().empty() && fqName.valueName().empty()); |
| 326 | |
| 327 | FQName autofilled = fqName; |
| 328 | autofilled.applyDefaults(mPackage.package(), mPackage.version()); |
| 329 | FQName matchingName; |
| 330 | // Given this fully-qualified name, the type may be defined in this AST, or other files |
| 331 | // in import. |
| 332 | Type *local = findDefinedType(autofilled, &matchingName); |
| 333 | CHECK(local == nullptr || autofilled == matchingName); |
| 334 | Type *fromImport = lookupType(autofilled); |
| 335 | |
| 336 | if (local != nullptr && fromImport != nullptr && local != fromImport) { |
| 337 | // Something bad happen; two types have the same FQName. |
| 338 | std::cerr << "ERROR: Unable to resolve type name '" |
| 339 | << fqName.string() |
| 340 | << "' (i.e. '" |
| 341 | << autofilled.string() |
| 342 | << "'), multiple definitions found.\n"; |
| 343 | |
| 344 | return UNKNOWN_ERROR; |
| 345 | } |
| 346 | if (local != nullptr) { |
| 347 | *returnedType = local; |
| 348 | return OK; |
| 349 | } |
| 350 | // If fromImport is nullptr as well, return nullptr to fall through to next rule. |
| 351 | *returnedType = fromImport; |
| 352 | return OK; |
| 353 | } |
| 354 | |
| 355 | // Rule 2: look at imports |
| 356 | Type *AST::lookupTypeFromImports(const FQName &fqName) { |
| 357 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 358 | Type *resolvedType = nullptr; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 359 | Type *returnedType = nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 360 | FQName resolvedName; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 361 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 362 | for (const auto &importedAST : mImportedASTs) { |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 363 | if (mImportedTypes.find(importedAST) != mImportedTypes.end()) { |
| 364 | // ignore single type imports |
| 365 | continue; |
| 366 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 367 | FQName matchingName; |
| 368 | Type *match = importedAST->findDefinedType(fqName, &matchingName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 369 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 370 | if (match != nullptr) { |
| 371 | if (resolvedType != nullptr) { |
| 372 | std::cerr << "ERROR: Unable to resolve type name '" |
| 373 | << fqName.string() |
| 374 | << "', multiple matches found:\n"; |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 375 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 376 | std::cerr << " " << resolvedName.string() << "\n"; |
| 377 | std::cerr << " " << matchingName.string() << "\n"; |
| 378 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 379 | return nullptr; |
| 380 | } |
| 381 | |
| 382 | resolvedType = match; |
| 383 | returnedType = resolvedType; |
| 384 | resolvedName = matchingName; |
| 385 | |
| 386 | // Keep going even after finding a match. |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | for (const auto &pair : mImportedTypes) { |
| 391 | AST *importedAST = pair.first; |
| 392 | std::set<Type *> importedTypes = pair.second; |
| 393 | |
| 394 | FQName matchingName; |
| 395 | Type *match = importedAST->findDefinedType(fqName, &matchingName); |
| 396 | if (match != nullptr && |
| 397 | importedTypes.find(match) != importedTypes.end()) { |
| 398 | if (resolvedType != nullptr) { |
| 399 | std::cerr << "ERROR: Unable to resolve type name '" |
| 400 | << fqName.string() |
| 401 | << "', multiple matches found:\n"; |
| 402 | |
| 403 | std::cerr << " " << resolvedName.string() << "\n"; |
| 404 | std::cerr << " " << matchingName.string() << "\n"; |
| 405 | |
| 406 | return nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | resolvedType = match; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 410 | returnedType = resolvedType; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 411 | resolvedName = matchingName; |
| 412 | |
| 413 | // Keep going even after finding a match. |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (resolvedType) { |
| 418 | #if 0 |
| 419 | LOG(INFO) << "found '" |
| 420 | << resolvedName.string() |
| 421 | << "' after looking for '" |
| 422 | << fqName.string() |
| 423 | << "'."; |
| 424 | #endif |
| 425 | |
| 426 | // Resolve typeDefs to the target type. |
| 427 | while (resolvedType->isTypeDef()) { |
| 428 | resolvedType = |
| 429 | static_cast<TypeDef *>(resolvedType)->referencedType(); |
| 430 | } |
| 431 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 432 | returnedType = resolvedType; |
| 433 | |
| 434 | // If the resolved type is not an interface, we need to determine |
| 435 | // whether it is defined in types.hal, or in some other interface. In |
| 436 | // the latter case, we need to emit a dependency for the interface in |
| 437 | // which the type is defined. |
| 438 | // |
| 439 | // Consider the following: |
| 440 | // android.hardware.tests.foo@1.0::Record |
| 441 | // android.hardware.tests.foo@1.0::IFoo.Folder |
| 442 | // android.hardware.tests.foo@1.0::Folder |
| 443 | // |
| 444 | // If Record is an interface, then we keep track of it for the purpose |
| 445 | // of emitting dependencies in the target language (for example #include |
| 446 | // in C++). If Record is a UDT, then we assume it is defined in |
| 447 | // types.hal in android.hardware.tests.foo@1.0. |
| 448 | // |
| 449 | // In the case of IFoo.Folder, the same applies. If IFoo is an |
| 450 | // interface, we need to track this for the purpose of emitting |
| 451 | // dependencies. If not, then it must have been defined in types.hal. |
| 452 | // |
| 453 | // In the case of just specifying Folder, the resolved type is |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 454 | // android.hardware.tests.foo@1.0::Folder, and the same logic as |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 455 | // above applies. |
| 456 | |
| 457 | if (!resolvedType->isInterface()) { |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 458 | FQName ifc = resolvedName.getTopLevelType(); |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 459 | for (const auto &importedAST : mImportedASTs) { |
| 460 | FQName matchingName; |
| 461 | Type *match = importedAST->findDefinedType(ifc, &matchingName); |
| 462 | if (match != nullptr && match->isInterface()) { |
| 463 | resolvedType = match; |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 468 | if (!resolvedType->isInterface()) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 469 | // Non-interface types are declared in the associated types header. |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 470 | FQName typesName = resolvedName.getTypesForPackage(); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 471 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 472 | mImportedNames.insert(typesName); |
| 473 | } else { |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 474 | // Do _not_ use fqName, i.e. the name we used to look up the type, |
| 475 | // but instead use the name of the interface we found. |
| 476 | // This is necessary because if fqName pointed to a typedef which |
| 477 | // in turn referenced the found interface we'd mistakenly use the |
| 478 | // name of the typedef instead of the proper name of the interface. |
| 479 | |
| 480 | mImportedNames.insert( |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 481 | static_cast<Interface *>(resolvedType)->fqName()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 482 | } |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Steven Moreland | bb5c80b | 2016-10-05 11:07:36 -0700 | [diff] [blame] | 485 | return returnedType; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 488 | Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 489 | for (const auto &pair : mDefinedTypesByFullName) { |
| 490 | const FQName &key = pair.first; |
| 491 | Type* type = pair.second; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 492 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 493 | if (key.endsWith(fqName)) { |
| 494 | *matchingName = key; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 495 | return type; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 496 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 497 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 498 | |
| 499 | return nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 502 | void AST::getImportedPackages(std::set<FQName> *importSet) const { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 503 | for (const auto &fqName : mImportedNames) { |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 504 | FQName packageName = fqName.getPackageAndVersion(); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 505 | |
| 506 | if (packageName == mPackage) { |
| 507 | // We only care about external imports, not our own package. |
| 508 | continue; |
| 509 | } |
| 510 | |
| 511 | importSet->insert(packageName); |
| 512 | } |
| 513 | } |
| 514 | |
Yifan Hong | 40a373d | 2016-11-30 15:16:47 -0800 | [diff] [blame] | 515 | void AST::getImportedPackagesHierarchy(std::set<FQName> *importSet) const { |
| 516 | getImportedPackages(importSet); |
| 517 | std::set<FQName> newSet; |
| 518 | for (const auto &ast : mImportedASTs) { |
| 519 | if (importSet->find(ast->package()) != importSet->end()) { |
| 520 | ast->getImportedPackagesHierarchy(&newSet); |
| 521 | } |
| 522 | } |
| 523 | importSet->insert(newSet.begin(), newSet.end()); |
| 524 | } |
| 525 | |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 526 | void AST::getAllImportedNames(std::set<FQName> *allImportNames) const { |
| 527 | for (const auto& name : mImportedNames) { |
| 528 | allImportNames->insert(name); |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 529 | AST *ast = mCoordinator->parse(name, nullptr /* imported */, false /* enforce */); |
Zhuoyao Zhang | c4e1060 | 2017-01-27 16:48:05 -0800 | [diff] [blame] | 530 | ast->getAllImportedNames(allImportNames); |
| 531 | } |
| 532 | } |
| 533 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 534 | bool AST::isJavaCompatible() const { |
| 535 | std::string ifaceName; |
| 536 | if (!AST::isInterface(&ifaceName)) { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 537 | for (const auto *type : mRootScope->getSubTypes()) { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 538 | if (!type->isJavaCompatible()) { |
| 539 | return false; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | const Interface *iface = mRootScope->getInterface(); |
| 547 | return iface->isJavaCompatible(); |
| 548 | } |
| 549 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 550 | void AST::appendToExportedTypesVector( |
| 551 | std::vector<const Type *> *exportedTypes) const { |
| 552 | mRootScope->appendToExportedTypesVector(exportedTypes); |
| 553 | } |
| 554 | |
Yifan Hong | c893404 | 2016-11-17 17:10:52 -0800 | [diff] [blame] | 555 | bool AST::isIBase() const { |
| 556 | Interface *iface = mRootScope->getInterface(); |
| 557 | return iface != nullptr && iface->isIBase(); |
| 558 | } |
| 559 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 560 | const Interface *AST::getInterface() const { |
| 561 | return mRootScope->getInterface(); |
| 562 | } |
| 563 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 564 | } // namespace android; |