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; |
| 45 | mRootScope = NULL; |
| 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 | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 85 | bool AST::addImport(const char *import) { |
| 86 | FQName fqName(import); |
| 87 | CHECK(fqName.isValid()); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 88 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 89 | fqName.applyDefaults(mPackage.package(), mPackage.version()); |
| 90 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 91 | // LOG(INFO) << "importing " << fqName.string(); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 92 | |
| 93 | if (fqName.name().empty()) { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 94 | std::vector<FQName> packageInterfaces; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 95 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 96 | status_t err = |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 97 | mCoordinator->appendPackageInterfacesToVector(fqName, |
| 98 | &packageInterfaces); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 99 | |
| 100 | if (err != OK) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 104 | for (const auto &subFQName : packageInterfaces) { |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 105 | AST *ast = mCoordinator->parse(subFQName, &mImportedASTs); |
| 106 | if (ast == NULL) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return true; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 114 | AST *importAST = mCoordinator->parse(fqName, &mImportedASTs); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 115 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 116 | if (importAST == NULL) { |
| 117 | return false; |
| 118 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 119 | |
| 120 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 123 | void AST::addImportedAST(AST *ast) { |
| 124 | mImportedASTs.insert(ast); |
| 125 | } |
| 126 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 127 | void AST::enterScope(Scope *container) { |
| 128 | mScopePath.push_back(container); |
| 129 | } |
| 130 | |
| 131 | void AST::leaveScope() { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 132 | mScopePath.pop_back(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | Scope *AST::scope() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 136 | CHECK(!mScopePath.empty()); |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 137 | return mScopePath.back(); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 140 | bool AST::addTypeDef( |
| 141 | const char *localName, Type *type, std::string *errorMsg) { |
| 142 | // The reason we wrap the given type in a TypeDef is simply to suppress |
| 143 | // emitting any type definitions later on, since this is just an alias |
| 144 | // to a type defined elsewhere. |
| 145 | return addScopedTypeInternal( |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 146 | new TypeDef(localName, type), errorMsg); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Andreas Huber | 9ed827c | 2016-08-22 12:31:13 -0700 | [diff] [blame] | 149 | bool AST::addScopedType(NamedType *type, std::string *errorMsg) { |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 150 | return addScopedTypeInternal( |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 151 | type, errorMsg); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | bool AST::addScopedTypeInternal( |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 155 | NamedType *type, |
| 156 | std::string *errorMsg) { |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 157 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 158 | bool success = scope()->addType(type, errorMsg); |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 159 | if (!success) { |
| 160 | return false; |
| 161 | } |
| 162 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 163 | std::string path; |
| 164 | for (size_t i = 1; i < mScopePath.size(); ++i) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 165 | path.append(mScopePath[i]->localName()); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 166 | path.append("."); |
| 167 | } |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 168 | path.append(type->localName()); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 169 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 170 | FQName fqName(mPackage.package(), mPackage.version(), path); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 171 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 172 | type->setFullName(fqName); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 173 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 174 | mDefinedTypesByFullName[fqName] = type; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 175 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 176 | return true; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Yifan Hong | f24fa85 | 2016-09-23 11:03:15 -0700 | [diff] [blame] | 179 | EnumValue *AST::lookupEnumValue(const FQName &fqName, std::string *errorMsg) { |
| 180 | |
| 181 | FQName enumTypeName = fqName.typeName(); |
| 182 | std::string enumValueName = fqName.valueName(); |
| 183 | |
| 184 | CHECK(enumTypeName.isValid()); |
| 185 | CHECK(!enumValueName.empty()); |
| 186 | |
| 187 | Type *type = lookupType(enumTypeName); |
| 188 | if(type == nullptr) { |
| 189 | *errorMsg = "Cannot find type " + enumTypeName.string(); |
| 190 | return nullptr; |
| 191 | } |
| 192 | if(!type->isEnum()) { |
| 193 | *errorMsg = "Type " + enumTypeName.string() + " is not an enum type"; |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
| 197 | EnumType *enumType = static_cast<EnumType *>(type); |
| 198 | EnumValue *v = static_cast<EnumValue *>(enumType->lookupIdentifier(enumValueName)); |
| 199 | if(v == nullptr) { |
| 200 | *errorMsg = "Enum type " + enumTypeName.string() + " does not have " + enumValueName; |
| 201 | return nullptr; |
| 202 | } |
| 203 | return v; |
| 204 | } |
| 205 | |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 206 | Type *AST::lookupType(const FQName &fqName) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 207 | CHECK(fqName.isValid()); |
| 208 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 209 | if (fqName.name().empty()) { |
| 210 | // Given a package and version??? |
| 211 | return NULL; |
| 212 | } |
| 213 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 214 | if (fqName.package().empty() && fqName.version().empty()) { |
| 215 | // This is just a plain identifier, resolve locally first if possible. |
| 216 | |
| 217 | for (size_t i = mScopePath.size(); i-- > 0;) { |
Yifan Hong | ae16eed | 2016-09-23 13:25:25 -0700 | [diff] [blame] | 218 | Type *type = mScopePath[i]->lookupType(fqName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 219 | |
| 220 | if (type != NULL) { |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 221 | // Resolve typeDefs to the target type. |
| 222 | while (type->isTypeDef()) { |
| 223 | type = static_cast<TypeDef *>(type)->referencedType(); |
| 224 | } |
| 225 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 226 | return type->ref(); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 227 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 231 | Type *resolvedType = nullptr; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 232 | Type *returnedType = nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 233 | FQName resolvedName; |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 234 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 235 | for (const auto &importedAST : mImportedASTs) { |
| 236 | FQName matchingName; |
| 237 | Type *match = importedAST->findDefinedType(fqName, &matchingName); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 238 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 239 | if (match != nullptr) { |
| 240 | if (resolvedType != nullptr) { |
| 241 | std::cerr << "ERROR: Unable to resolve type name '" |
| 242 | << fqName.string() |
| 243 | << "', multiple matches found:\n"; |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 244 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 245 | std::cerr << " " << resolvedName.string() << "\n"; |
| 246 | std::cerr << " " << matchingName.string() << "\n"; |
| 247 | |
| 248 | return NULL; |
| 249 | } |
| 250 | |
| 251 | resolvedType = match; |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 252 | returnedType = resolvedType; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 253 | resolvedName = matchingName; |
| 254 | |
| 255 | // Keep going even after finding a match. |
| 256 | } |
| 257 | } |
| 258 | |
Andreas Huber | 4bcf97d | 2016-08-30 11:27:49 -0700 | [diff] [blame] | 259 | if (resolvedType == nullptr |
| 260 | && fqName.package().empty() |
| 261 | && fqName.version().empty() |
| 262 | && fqName.name() == "MQDescriptor") { |
| 263 | return new PredefinedType("::android::hardware::MQDescriptor"); |
| 264 | } |
| 265 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 266 | if (resolvedType) { |
| 267 | #if 0 |
| 268 | LOG(INFO) << "found '" |
| 269 | << resolvedName.string() |
| 270 | << "' after looking for '" |
| 271 | << fqName.string() |
| 272 | << "'."; |
| 273 | #endif |
| 274 | |
| 275 | // Resolve typeDefs to the target type. |
| 276 | while (resolvedType->isTypeDef()) { |
| 277 | resolvedType = |
| 278 | static_cast<TypeDef *>(resolvedType)->referencedType(); |
| 279 | } |
| 280 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 281 | returnedType = resolvedType; |
| 282 | |
| 283 | // If the resolved type is not an interface, we need to determine |
| 284 | // whether it is defined in types.hal, or in some other interface. In |
| 285 | // the latter case, we need to emit a dependency for the interface in |
| 286 | // which the type is defined. |
| 287 | // |
| 288 | // Consider the following: |
| 289 | // android.hardware.tests.foo@1.0::Record |
| 290 | // android.hardware.tests.foo@1.0::IFoo.Folder |
| 291 | // android.hardware.tests.foo@1.0::Folder |
| 292 | // |
| 293 | // If Record is an interface, then we keep track of it for the purpose |
| 294 | // of emitting dependencies in the target language (for example #include |
| 295 | // in C++). If Record is a UDT, then we assume it is defined in |
| 296 | // types.hal in android.hardware.tests.foo@1.0. |
| 297 | // |
| 298 | // In the case of IFoo.Folder, the same applies. If IFoo is an |
| 299 | // interface, we need to track this for the purpose of emitting |
| 300 | // dependencies. If not, then it must have been defined in types.hal. |
| 301 | // |
| 302 | // In the case of just specifying Folder, the resolved type is |
| 303 | // android.hardware.tests.foo@1.0::IFoo.Folder, and the same logic as |
| 304 | // above applies. |
| 305 | |
| 306 | if (!resolvedType->isInterface()) { |
| 307 | FQName ifc(resolvedName.package(), |
| 308 | resolvedName.version(), |
| 309 | resolvedName.names().at(0)); |
| 310 | for (const auto &importedAST : mImportedASTs) { |
| 311 | FQName matchingName; |
| 312 | Type *match = importedAST->findDefinedType(ifc, &matchingName); |
| 313 | if (match != nullptr && match->isInterface()) { |
| 314 | resolvedType = match; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 319 | if (!resolvedType->isInterface()) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 320 | // Non-interface types are declared in the associated types header. |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 321 | FQName typesName( |
| 322 | resolvedName.package(), resolvedName.version(), "types"); |
| 323 | |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 324 | mImportedNames.insert(typesName); |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 325 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 326 | if (resolvedType->isNamedType() && !resolvedType->isTypeDef()) { |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 327 | mImportedNamesForJava.insert( |
| 328 | static_cast<NamedType *>(resolvedType)->fqName()); |
| 329 | } |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 330 | } else { |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 331 | // Do _not_ use fqName, i.e. the name we used to look up the type, |
| 332 | // but instead use the name of the interface we found. |
| 333 | // This is necessary because if fqName pointed to a typedef which |
| 334 | // in turn referenced the found interface we'd mistakenly use the |
| 335 | // name of the typedef instead of the proper name of the interface. |
| 336 | |
| 337 | mImportedNames.insert( |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 338 | static_cast<Interface *>(resolvedType)->fqName()); |
Andreas Huber | 85eabdb | 2016-08-25 11:24:49 -0700 | [diff] [blame] | 339 | |
| 340 | mImportedNamesForJava.insert( |
| 341 | static_cast<Interface *>(resolvedType)->fqName()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 342 | } |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Iliyan Malchev | 800273d | 2016-09-02 15:25:07 -0700 | [diff] [blame] | 345 | return returnedType->ref(); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 348 | Type *AST::findDefinedType(const FQName &fqName, FQName *matchingName) const { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 349 | for (const auto &pair : mDefinedTypesByFullName) { |
| 350 | const FQName &key = pair.first; |
| 351 | Type* type = pair.second; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 352 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 353 | if (key.endsWith(fqName)) { |
| 354 | *matchingName = key; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 355 | return type; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 356 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 357 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 358 | |
| 359 | return nullptr; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 362 | void AST::getImportedPackages(std::set<FQName> *importSet) const { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 363 | for (const auto &fqName : mImportedNames) { |
| 364 | FQName packageName(fqName.package(), fqName.version(), ""); |
| 365 | |
| 366 | if (packageName == mPackage) { |
| 367 | // We only care about external imports, not our own package. |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | importSet->insert(packageName); |
| 372 | } |
| 373 | } |
| 374 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 375 | bool AST::isJavaCompatible() const { |
| 376 | std::string ifaceName; |
| 377 | if (!AST::isInterface(&ifaceName)) { |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 378 | for (const auto *type : mRootScope->getSubTypes()) { |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 379 | if (!type->isJavaCompatible()) { |
| 380 | return false; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | const Interface *iface = mRootScope->getInterface(); |
| 388 | return iface->isJavaCompatible(); |
| 389 | } |
| 390 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 391 | } // namespace android; |