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