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