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 | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 21 | #include "FQName.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 | 4b2cf35 | 2016-08-31 13:58:19 -0700 | [diff] [blame] | 25 | #include "PredefinedType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 26 | #include "Scope.h" |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 27 | #include "TypeDef.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 28 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 29 | #include <hidl-util/Formatter.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 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 275 | if (fqName.package().empty() && fqName.version().empty()) { |
| 276 | // This is just a plain identifier, resolve locally first if possible. |
| 277 | |
| 278 | for (size_t i = mScopePath.size(); i-- > 0;) { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 279 | Type *type = mScopePath[i]->lookupType(fqName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 280 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 281 | if (type != nullptr) { |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 282 | // Resolve typeDefs to the target type. |
| 283 | while (type->isTypeDef()) { |
| 284 | type = static_cast<TypeDef *>(type)->referencedType(); |
| 285 | } |
| 286 | |
Steven Moreland | bb5c80b | 2016-10-05 11:07:36 -0700 | [diff] [blame] | 287 | return type; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 288 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 292 | Type *resolvedType = nullptr; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 293 | Type *returnedType = nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 294 | FQName resolvedName; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 295 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 296 | for (const auto &importedAST : mImportedASTs) { |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 297 | if (mImportedTypes.find(importedAST) != mImportedTypes.end()) { |
| 298 | // ignore single type imports |
| 299 | continue; |
| 300 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 301 | FQName matchingName; |
| 302 | Type *match = importedAST->findDefinedType(fqName, &matchingName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 303 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 304 | if (match != nullptr) { |
| 305 | if (resolvedType != nullptr) { |
| 306 | std::cerr << "ERROR: Unable to resolve type name '" |
| 307 | << fqName.string() |
| 308 | << "', multiple matches found:\n"; |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 309 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 310 | std::cerr << " " << resolvedName.string() << "\n"; |
| 311 | std::cerr << " " << matchingName.string() << "\n"; |
| 312 | |
Yifan Hong | 1977ea3 | 2016-10-05 12:49:08 -0700 | [diff] [blame] | 313 | return nullptr; |
| 314 | } |
| 315 | |
| 316 | resolvedType = match; |
| 317 | returnedType = resolvedType; |
| 318 | resolvedName = matchingName; |
| 319 | |
| 320 | // Keep going even after finding a match. |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | for (const auto &pair : mImportedTypes) { |
| 325 | AST *importedAST = pair.first; |
| 326 | std::set<Type *> importedTypes = pair.second; |
| 327 | |
| 328 | FQName matchingName; |
| 329 | Type *match = importedAST->findDefinedType(fqName, &matchingName); |
| 330 | if (match != nullptr && |
| 331 | importedTypes.find(match) != importedTypes.end()) { |
| 332 | if (resolvedType != nullptr) { |
| 333 | std::cerr << "ERROR: Unable to resolve type name '" |
| 334 | << fqName.string() |
| 335 | << "', multiple matches found:\n"; |
| 336 | |
| 337 | std::cerr << " " << resolvedName.string() << "\n"; |
| 338 | std::cerr << " " << matchingName.string() << "\n"; |
| 339 | |
| 340 | return nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | resolvedType = match; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 344 | returnedType = resolvedType; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 345 | resolvedName = matchingName; |
| 346 | |
| 347 | // Keep going even after finding a match. |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (resolvedType) { |
| 352 | #if 0 |
| 353 | LOG(INFO) << "found '" |
| 354 | << resolvedName.string() |
| 355 | << "' after looking for '" |
| 356 | << fqName.string() |
| 357 | << "'."; |
| 358 | #endif |
| 359 | |
| 360 | // Resolve typeDefs to the target type. |
| 361 | while (resolvedType->isTypeDef()) { |
| 362 | resolvedType = |
| 363 | static_cast<TypeDef *>(resolvedType)->referencedType(); |
| 364 | } |
| 365 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 366 | returnedType = resolvedType; |
| 367 | |
| 368 | // If the resolved type is not an interface, we need to determine |
| 369 | // whether it is defined in types.hal, or in some other interface. In |
| 370 | // the latter case, we need to emit a dependency for the interface in |
| 371 | // which the type is defined. |
| 372 | // |
| 373 | // Consider the following: |
| 374 | // android.hardware.tests.foo@1.0::Record |
| 375 | // android.hardware.tests.foo@1.0::IFoo.Folder |
| 376 | // android.hardware.tests.foo@1.0::Folder |
| 377 | // |
| 378 | // If Record is an interface, then we keep track of it for the purpose |
| 379 | // of emitting dependencies in the target language (for example #include |
| 380 | // in C++). If Record is a UDT, then we assume it is defined in |
| 381 | // types.hal in android.hardware.tests.foo@1.0. |
| 382 | // |
| 383 | // In the case of IFoo.Folder, the same applies. If IFoo is an |
| 384 | // interface, we need to track this for the purpose of emitting |
| 385 | // dependencies. If not, then it must have been defined in types.hal. |
| 386 | // |
| 387 | // In the case of just specifying Folder, the resolved type is |
| 388 | // android.hardware.tests.foo@1.0::IFoo.Folder, and the same logic as |
| 389 | // above applies. |
| 390 | |
| 391 | if (!resolvedType->isInterface()) { |
| 392 | FQName ifc(resolvedName.package(), |
| 393 | resolvedName.version(), |
| 394 | resolvedName.names().at(0)); |
| 395 | for (const auto &importedAST : mImportedASTs) { |
| 396 | FQName matchingName; |
| 397 | Type *match = importedAST->findDefinedType(ifc, &matchingName); |
| 398 | if (match != nullptr && match->isInterface()) { |
| 399 | resolvedType = match; |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 404 | if (!resolvedType->isInterface()) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 405 | // Non-interface types are declared in the associated types header. |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 406 | FQName typesName( |
| 407 | resolvedName.package(), resolvedName.version(), "types"); |
| 408 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 409 | mImportedNames.insert(typesName); |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 410 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 411 | if (resolvedType->isNamedType() && !resolvedType->isTypeDef()) { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 412 | mImportedNamesForJava.insert( |
| 413 | static_cast<NamedType *>(resolvedType)->fqName()); |
| 414 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 415 | } else { |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 416 | // Do _not_ use fqName, i.e. the name we used to look up the type, |
| 417 | // but instead use the name of the interface we found. |
| 418 | // This is necessary because if fqName pointed to a typedef which |
| 419 | // in turn referenced the found interface we'd mistakenly use the |
| 420 | // name of the typedef instead of the proper name of the interface. |
| 421 | |
| 422 | mImportedNames.insert( |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 423 | static_cast<Interface *>(resolvedType)->fqName()); |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 424 | |
| 425 | mImportedNamesForJava.insert( |
| 426 | static_cast<Interface *>(resolvedType)->fqName()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 427 | } |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Steven Moreland | bb5c80b | 2016-10-05 11:07:36 -0700 | [diff] [blame] | 430 | return returnedType; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 433 | Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 434 | for (const auto &pair : mDefinedTypesByFullName) { |
| 435 | const FQName &key = pair.first; |
| 436 | Type* type = pair.second; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 437 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 438 | if (key.endsWith(fqName)) { |
| 439 | *matchingName = key; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 440 | return type; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 441 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 442 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 443 | |
| 444 | return nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 447 | void AST::getImportedPackages(std::set<FQName> *importSet) const { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 448 | for (const auto &fqName : mImportedNames) { |
| 449 | FQName packageName(fqName.package(), fqName.version(), ""); |
| 450 | |
| 451 | if (packageName == mPackage) { |
| 452 | // We only care about external imports, not our own package. |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | importSet->insert(packageName); |
| 457 | } |
| 458 | } |
| 459 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 460 | bool AST::isJavaCompatible() const { |
| 461 | std::string ifaceName; |
| 462 | if (!AST::isInterface(&ifaceName)) { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 463 | for (const auto *type : mRootScope->getSubTypes()) { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 464 | if (!type->isJavaCompatible()) { |
| 465 | return false; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | const Interface *iface = mRootScope->getInterface(); |
| 473 | return iface->isJavaCompatible(); |
| 474 | } |
| 475 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 476 | void AST::appendToExportedTypesVector( |
| 477 | std::vector<const Type *> *exportedTypes) const { |
| 478 | mRootScope->appendToExportedTypesVector(exportedTypes); |
| 479 | } |
| 480 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 481 | } // namespace android; |