Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | #include "AST.h" |
| 2 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 3 | #include "Coordinator.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 4 | #include "Formatter.h" |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 5 | #include "FQName.h" |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 6 | #include "HandleType.h" |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 7 | #include "Interface.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 8 | #include "Scope.h" |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 9 | #include "TypeDef.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 10 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 11 | #include <android-base/logging.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 12 | #include <stdlib.h> |
| 13 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 14 | namespace android { |
| 15 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 16 | AST::AST(Coordinator *coordinator) |
| 17 | : mCoordinator(coordinator), |
| 18 | mScanner(NULL), |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 19 | mRootScope(new Scope) { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 20 | enterScope(mRootScope); |
| 21 | } |
| 22 | |
| 23 | AST::~AST() { |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 24 | delete mRootScope; |
| 25 | mRootScope = NULL; |
| 26 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 27 | CHECK(mScanner == NULL); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 28 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 29 | // Ownership of "coordinator" was NOT transferred. |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void *AST::scanner() { |
| 33 | return mScanner; |
| 34 | } |
| 35 | |
| 36 | void AST::setScanner(void *scanner) { |
| 37 | mScanner = scanner; |
| 38 | } |
| 39 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 40 | bool AST::setPackage(const char *package) { |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 41 | mPackage.setTo(package); |
| 42 | CHECK(mPackage.isValid()); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 43 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 44 | if (mPackage.package().empty() |
| 45 | || mPackage.version().empty() |
| 46 | || !mPackage.name().empty()) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 47 | return false; |
| 48 | } |
| 49 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 50 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 53 | FQName AST::package() const { |
| 54 | return mPackage; |
| 55 | } |
| 56 | |
| 57 | bool AST::isInterface(std::string *ifaceName) const { |
| 58 | return mRootScope->containsSingleInterface(ifaceName); |
| 59 | } |
| 60 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 61 | bool AST::addImport(const char *import) { |
| 62 | FQName fqName(import); |
| 63 | CHECK(fqName.isValid()); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 64 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 65 | fqName.applyDefaults(mPackage.package(), mPackage.version()); |
| 66 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 67 | // LOG(INFO) << "importing " << fqName.string(); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 68 | |
| 69 | if (fqName.name().empty()) { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 70 | std::vector<FQName> packageInterfaces; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 71 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 72 | status_t err = |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 73 | mCoordinator->appendPackageInterfacesToSet(fqName, |
| 74 | &packageInterfaces); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 75 | |
| 76 | if (err != OK) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 77 | return false; |
| 78 | } |
| 79 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 80 | for (const auto &subFQName : packageInterfaces) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 81 | if (mCoordinator->parse(subFQName) == NULL) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return true; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 89 | AST *importAST = mCoordinator->parse(fqName); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 90 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 91 | if (importAST == NULL) { |
| 92 | return false; |
| 93 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 94 | |
| 95 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 98 | void AST::enterScope(Scope *container) { |
| 99 | mScopePath.push_back(container); |
| 100 | } |
| 101 | |
| 102 | void AST::leaveScope() { |
| 103 | mScopePath.pop(); |
| 104 | } |
| 105 | |
| 106 | Scope *AST::scope() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 107 | CHECK(!mScopePath.empty()); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 108 | return mScopePath.top(); |
| 109 | } |
| 110 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 111 | bool AST::addScopedType(const char *localName, NamedType *type) { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 112 | // LOG(INFO) << "adding scoped type '" << localName << "'"; |
| 113 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 114 | bool success = scope()->addType(localName, type); |
| 115 | if (!success) { |
| 116 | return false; |
| 117 | } |
| 118 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 119 | std::string path; |
| 120 | for (size_t i = 1; i < mScopePath.size(); ++i) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 121 | path.append(mScopePath[i]->localName()); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 122 | path.append("."); |
| 123 | } |
| 124 | path.append(localName); |
| 125 | |
| 126 | type->setLocalName(localName); |
| 127 | |
| 128 | FQName fqName(mPackage.package(), mPackage.version(), path); |
| 129 | type->setFullName(fqName); |
| 130 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 131 | return true; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 134 | Type *AST::lookupType(const char *name) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 135 | FQName fqName(name); |
| 136 | CHECK(fqName.isValid()); |
| 137 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 138 | if (fqName.name().empty()) { |
| 139 | // Given a package and version??? |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 143 | if (fqName.package().empty() && fqName.version().empty()) { |
| 144 | // This is just a plain identifier, resolve locally first if possible. |
| 145 | |
| 146 | for (size_t i = mScopePath.size(); i-- > 0;) { |
| 147 | Type *type = mScopePath[i]->lookupType(name); |
| 148 | |
| 149 | if (type != NULL) { |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 150 | // Resolve typeDefs to the target type. |
| 151 | while (type->isTypeDef()) { |
| 152 | type = static_cast<TypeDef *>(type)->referencedType(); |
| 153 | } |
| 154 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 155 | return type->ref(); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 156 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 160 | fqName.applyDefaults(mPackage.package(), mPackage.version()); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 161 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 162 | // LOG(INFO) << "lookupType now looking for " << fqName.string(); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 163 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 164 | Type *resultType = mCoordinator->lookupType(fqName); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 165 | |
| 166 | if (resultType) { |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 167 | if (!resultType->isInterface()) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 168 | // Non-interface types are declared in the associated types header. |
| 169 | FQName typesName(fqName.package(), fqName.version(), "types"); |
| 170 | mImportedNames.insert(typesName); |
| 171 | } else { |
Andreas Huber | bfd7621 | 2016-08-09 11:12:16 -0700 | [diff] [blame] | 172 | // Do _not_ use fqName, i.e. the name we used to look up the type, |
| 173 | // but instead use the name of the interface we found. |
| 174 | // This is necessary because if fqName pointed to a typedef which |
| 175 | // in turn referenced the found interface we'd mistakenly use the |
| 176 | // name of the typedef instead of the proper name of the interface. |
| 177 | |
| 178 | mImportedNames.insert( |
| 179 | static_cast<Interface *>(resultType)->fqName()); |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 180 | } |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return resultType; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | Type *AST::lookupTypeInternal(const std::string &namePath) const { |
| 187 | Scope *scope = mRootScope; |
| 188 | |
| 189 | size_t startPos = 0; |
| 190 | for (;;) { |
| 191 | size_t dotPos = namePath.find('.', startPos); |
| 192 | |
| 193 | std::string component; |
| 194 | if (dotPos == std::string::npos) { |
| 195 | component = namePath.substr(startPos); |
| 196 | } else { |
| 197 | component = namePath.substr(startPos, dotPos - startPos); |
| 198 | } |
| 199 | |
| 200 | Type *type = scope->lookupType(component.c_str()); |
| 201 | |
| 202 | if (type == NULL) { |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | if (dotPos == std::string::npos) { |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 207 | // Resolve typeDefs to the target type. |
| 208 | while (type->isTypeDef()) { |
| 209 | type = static_cast<TypeDef *>(type)->referencedType(); |
| 210 | } |
| 211 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 212 | return type; |
| 213 | } |
| 214 | |
| 215 | if (!type->isScope()) { |
| 216 | return NULL; |
| 217 | } |
| 218 | |
| 219 | scope = static_cast<Scope *>(type); |
| 220 | startPos = dotPos + 1; |
| 221 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 224 | void AST::getImportedPackages(std::set<FQName> *importSet) const { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 225 | for (const auto &fqName : mImportedNames) { |
| 226 | FQName packageName(fqName.package(), fqName.version(), ""); |
| 227 | |
| 228 | if (packageName == mPackage) { |
| 229 | // We only care about external imports, not our own package. |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | importSet->insert(packageName); |
| 234 | } |
| 235 | } |
| 236 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 237 | } // namespace android; |