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