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 | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 7 | #include "Scope.h" |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 8 | #include "TypeDef.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 9 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 10 | #include <android-base/logging.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 11 | #include <stdlib.h> |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 12 | #include <sys/dir.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 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 | dc98133 | 2016-07-29 15:46:54 -0700 | [diff] [blame] | 70 | const std::string packagePath = mCoordinator->getPackagePath(fqName); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 71 | DIR *dir = opendir(packagePath.c_str()); |
| 72 | |
| 73 | if (dir == NULL) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | // Enumerate all regular files ending in ".hal" in the package directory |
| 78 | // and attempt to import all of them. |
| 79 | |
| 80 | Vector<std::string> fileNames; |
| 81 | |
| 82 | struct dirent *ent; |
| 83 | while ((ent = readdir(dir)) != NULL) { |
| 84 | if (ent->d_type != DT_REG) { |
| 85 | continue; |
| 86 | } |
| 87 | |
Andreas Huber | f5dc73e | 2016-08-03 12:54:08 -0700 | [diff] [blame] | 88 | const auto suffix = ".hal"; |
| 89 | const auto suffix_len = std::strlen(suffix); |
| 90 | const auto d_namelen = strlen(ent->d_name); |
| 91 | |
| 92 | if (d_namelen < suffix_len |
| 93 | || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 94 | continue; |
| 95 | } |
| 96 | |
Andreas Huber | f5dc73e | 2016-08-03 12:54:08 -0700 | [diff] [blame] | 97 | fileNames.push_back(std::string(ent->d_name, d_namelen - suffix_len)); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | closedir(dir); |
| 101 | dir = NULL; |
| 102 | |
| 103 | for (size_t i = 0; i < fileNames.size(); ++i) { |
| 104 | FQName subFQName( |
| 105 | fqName.package() + fqName.version() + "::" + fileNames[i]); |
| 106 | |
| 107 | if (!subFQName.isValid()) { |
| 108 | LOG(WARNING) |
| 109 | << "Whole-package import encountered invalid filename '" |
| 110 | << fileNames[i] |
| 111 | << "' in package " |
| 112 | << fqName.package() |
| 113 | << fqName.version(); |
| 114 | |
| 115 | continue; |
| 116 | } |
| 117 | |
| 118 | if (mCoordinator->parse(subFQName) == NULL) { |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return true; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 126 | AST *importAST = mCoordinator->parse(fqName); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 127 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 128 | if (importAST == NULL) { |
| 129 | return false; |
| 130 | } |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 131 | |
| 132 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 135 | void AST::enterScope(Scope *container) { |
| 136 | mScopePath.push_back(container); |
| 137 | } |
| 138 | |
| 139 | void AST::leaveScope() { |
| 140 | mScopePath.pop(); |
| 141 | } |
| 142 | |
| 143 | Scope *AST::scope() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 144 | CHECK(!mScopePath.empty()); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 145 | return mScopePath.top(); |
| 146 | } |
| 147 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 148 | bool AST::addScopedType(const char *localName, NamedType *type) { |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 149 | // LOG(INFO) << "adding scoped type '" << localName << "'"; |
| 150 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 151 | bool success = scope()->addType(localName, type); |
| 152 | if (!success) { |
| 153 | return false; |
| 154 | } |
| 155 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 156 | std::string path; |
| 157 | for (size_t i = 1; i < mScopePath.size(); ++i) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 158 | path.append(mScopePath[i]->localName()); |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 159 | path.append("."); |
| 160 | } |
| 161 | path.append(localName); |
| 162 | |
| 163 | type->setLocalName(localName); |
| 164 | |
| 165 | FQName fqName(mPackage.package(), mPackage.version(), path); |
| 166 | type->setFullName(fqName); |
| 167 | |
Andreas Huber | 5a54544 | 2016-08-03 10:44:56 -0700 | [diff] [blame] | 168 | return true; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 171 | Type *AST::lookupType(const char *name) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 172 | FQName fqName(name); |
| 173 | CHECK(fqName.isValid()); |
| 174 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 175 | if (fqName.name().empty()) { |
| 176 | // Given a package and version??? |
| 177 | return NULL; |
| 178 | } |
| 179 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 180 | if (fqName.package().empty() && fqName.version().empty()) { |
| 181 | // This is just a plain identifier, resolve locally first if possible. |
| 182 | |
| 183 | for (size_t i = mScopePath.size(); i-- > 0;) { |
| 184 | Type *type = mScopePath[i]->lookupType(name); |
| 185 | |
| 186 | if (type != NULL) { |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 187 | // Resolve typeDefs to the target type. |
| 188 | while (type->isTypeDef()) { |
| 189 | type = static_cast<TypeDef *>(type)->referencedType(); |
| 190 | } |
| 191 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 192 | return type->ref(); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 193 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 197 | fqName.applyDefaults(mPackage.package(), mPackage.version()); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 198 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 199 | // LOG(INFO) << "lookupType now looking for " << fqName.string(); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 200 | |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 201 | Type *resultType = mCoordinator->lookupType(fqName); |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 202 | |
| 203 | if (resultType) { |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 204 | if (!resultType->isInterface()) { |
Andreas Huber | 0e00de4 | 2016-08-03 09:56:02 -0700 | [diff] [blame] | 205 | // Non-interface types are declared in the associated types header. |
| 206 | FQName typesName(fqName.package(), fqName.version(), "types"); |
| 207 | mImportedNames.insert(typesName); |
| 208 | } else { |
| 209 | mImportedNames.insert(fqName); |
| 210 | } |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | return resultType; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | Type *AST::lookupTypeInternal(const std::string &namePath) const { |
| 217 | Scope *scope = mRootScope; |
| 218 | |
| 219 | size_t startPos = 0; |
| 220 | for (;;) { |
| 221 | size_t dotPos = namePath.find('.', startPos); |
| 222 | |
| 223 | std::string component; |
| 224 | if (dotPos == std::string::npos) { |
| 225 | component = namePath.substr(startPos); |
| 226 | } else { |
| 227 | component = namePath.substr(startPos, dotPos - startPos); |
| 228 | } |
| 229 | |
| 230 | Type *type = scope->lookupType(component.c_str()); |
| 231 | |
| 232 | if (type == NULL) { |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | if (dotPos == std::string::npos) { |
Andreas Huber | 8d3ac0c | 2016-08-04 14:49:23 -0700 | [diff] [blame] | 237 | // Resolve typeDefs to the target type. |
| 238 | while (type->isTypeDef()) { |
| 239 | type = static_cast<TypeDef *>(type)->referencedType(); |
| 240 | } |
| 241 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 242 | return type; |
| 243 | } |
| 244 | |
| 245 | if (!type->isScope()) { |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | scope = static_cast<Scope *>(type); |
| 250 | startPos = dotPos + 1; |
| 251 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 254 | } // namespace android; |