Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | #include "AST.h" |
| 2 | |
| 3 | #include "Formatter.h" |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame^] | 4 | #include "HandleType.h" |
| 5 | #include "RefType.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 6 | #include "Scope.h" |
| 7 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame^] | 8 | #include <android-base/logging.h> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 9 | #include <stdlib.h> |
| 10 | |
| 11 | extern void parseFile(android::AST *ast, const char *path); |
| 12 | |
| 13 | namespace android { |
| 14 | |
| 15 | AST::AST() |
| 16 | : mScanner(NULL), |
| 17 | mRootScope(new Scope("root")) { |
| 18 | enterScope(mRootScope); |
| 19 | } |
| 20 | |
| 21 | AST::~AST() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame^] | 22 | CHECK(scope() == mRootScope); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 23 | leaveScope(); |
| 24 | |
| 25 | delete mRootScope; |
| 26 | mRootScope = NULL; |
| 27 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame^] | 28 | CHECK(mScanner == NULL); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | // static |
| 32 | AST *AST::Parse(const char *path) { |
| 33 | AST *ast = new AST; |
| 34 | parseFile(ast, path); |
| 35 | |
| 36 | return ast; |
| 37 | } |
| 38 | |
| 39 | void *AST::scanner() { |
| 40 | return mScanner; |
| 41 | } |
| 42 | |
| 43 | void AST::setScanner(void *scanner) { |
| 44 | mScanner = scanner; |
| 45 | } |
| 46 | |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame^] | 47 | void AST::setVersion(const char *, const char *) { |
| 48 | } |
| 49 | |
| 50 | void AST::setPackage(Vector<std::string> *) { |
| 51 | } |
| 52 | |
| 53 | void AST::addImport(Vector<std::string> *importPath) { |
| 54 | CHECK(!importPath->empty()); |
| 55 | |
| 56 | std::string leaf = importPath->itemAt(importPath->size() - 1); |
| 57 | scope()->addType(new RefType(leaf.c_str(), new HandleType)); |
| 58 | } |
| 59 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 60 | void AST::enterScope(Scope *container) { |
| 61 | mScopePath.push_back(container); |
| 62 | } |
| 63 | |
| 64 | void AST::leaveScope() { |
| 65 | mScopePath.pop(); |
| 66 | } |
| 67 | |
| 68 | Scope *AST::scope() { |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame^] | 69 | CHECK(!mScopePath.empty()); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 70 | return mScopePath.top(); |
| 71 | } |
| 72 | |
| 73 | Type *AST::lookupType(const char *name) { |
| 74 | for (size_t i = mScopePath.size(); i-- > 0;) { |
| 75 | Type *type = mScopePath[i]->lookupType(name); |
| 76 | |
| 77 | if (type != NULL) { |
| 78 | return type; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | void AST::dump(Formatter &out) const { |
| 86 | mRootScope->dump(out); |
| 87 | } |
| 88 | |
| 89 | } // namespace android; |