Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | #include "AST.h" |
| 2 | |
| 3 | #include "Formatter.h" |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 4 | #include "FQName.h" |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 5 | #include "HandleType.h" |
| 6 | #include "RefType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 7 | #include "Scope.h" |
| 8 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 9 | #include <android-base/logging.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 10 | #include <stdlib.h> |
| 11 | |
| 12 | extern void parseFile(android::AST *ast, const char *path); |
| 13 | |
| 14 | namespace android { |
| 15 | |
| 16 | AST::AST() |
| 17 | : mScanner(NULL), |
| 18 | mRootScope(new Scope("root")) { |
| 19 | enterScope(mRootScope); |
| 20 | } |
| 21 | |
| 22 | AST::~AST() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 23 | CHECK(scope() == mRootScope); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 24 | leaveScope(); |
| 25 | |
| 26 | delete mRootScope; |
| 27 | mRootScope = NULL; |
| 28 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 29 | CHECK(mScanner == NULL); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | // static |
| 33 | AST *AST::Parse(const char *path) { |
| 34 | AST *ast = new AST; |
| 35 | parseFile(ast, path); |
| 36 | |
| 37 | return ast; |
| 38 | } |
| 39 | |
| 40 | void *AST::scanner() { |
| 41 | return mScanner; |
| 42 | } |
| 43 | |
| 44 | void AST::setScanner(void *scanner) { |
| 45 | mScanner = scanner; |
| 46 | } |
| 47 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 48 | bool AST::setPackage(const char *package) { |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 49 | mPackage.setTo(package); |
| 50 | CHECK(mPackage.isValid()); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 51 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 52 | if (mPackage.package().empty() |
| 53 | || mPackage.version().empty() |
| 54 | || !mPackage.name().empty()) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 58 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 61 | bool AST::addImport(const char * /* import */) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 62 | #if 0 |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 63 | CHECK(!importPath->empty()); |
| 64 | |
| 65 | std::string leaf = importPath->itemAt(importPath->size() - 1); |
| 66 | scope()->addType(new RefType(leaf.c_str(), new HandleType)); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 67 | #endif |
| 68 | |
| 69 | return true; |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 72 | void AST::enterScope(Scope *container) { |
| 73 | mScopePath.push_back(container); |
| 74 | } |
| 75 | |
| 76 | void AST::leaveScope() { |
| 77 | mScopePath.pop(); |
| 78 | } |
| 79 | |
| 80 | Scope *AST::scope() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 81 | CHECK(!mScopePath.empty()); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 82 | return mScopePath.top(); |
| 83 | } |
| 84 | |
| 85 | Type *AST::lookupType(const char *name) { |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 86 | FQName fqName(name); |
| 87 | CHECK(fqName.isValid()); |
| 88 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 89 | if (fqName.name().empty()) { |
| 90 | // Given a package and version??? |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 94 | if (fqName.package().empty() && fqName.version().empty()) { |
| 95 | // This is just a plain identifier, resolve locally first if possible. |
| 96 | |
| 97 | for (size_t i = mScopePath.size(); i-- > 0;) { |
| 98 | Type *type = mScopePath[i]->lookupType(name); |
| 99 | |
| 100 | if (type != NULL) { |
| 101 | return new RefType(name, type); |
| 102 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
Andreas Huber | da51b8e | 2016-07-28 16:00:57 -0700 | [diff] [blame] | 106 | fqName.applyDefaults(mPackage.package(), mPackage.version()); |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 107 | |
| 108 | LOG(INFO) << "lookupType now looking for " << fqName.debugString(); |
| 109 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | void AST::dump(Formatter &out) const { |
| 114 | mRootScope->dump(out); |
| 115 | } |
| 116 | |
| 117 | } // namespace android; |