blob: 7610ba590cd079f2b5c1cfe0fde05a28489a464b [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "AST.h"
2
3#include "Formatter.h"
Andreas Huber84f89de2016-07-28 15:39:51 -07004#include "FQName.h"
Andreas Hubereb1081f2016-07-28 13:13:24 -07005#include "HandleType.h"
6#include "RefType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07007#include "Scope.h"
8
Andreas Hubereb1081f2016-07-28 13:13:24 -07009#include <android-base/logging.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070010#include <stdlib.h>
11
12extern void parseFile(android::AST *ast, const char *path);
13
14namespace android {
15
16AST::AST()
17 : mScanner(NULL),
18 mRootScope(new Scope("root")) {
19 enterScope(mRootScope);
20}
21
22AST::~AST() {
Andreas Hubereb1081f2016-07-28 13:13:24 -070023 CHECK(scope() == mRootScope);
Andreas Huberc9410c72016-07-28 12:18:40 -070024 leaveScope();
25
26 delete mRootScope;
27 mRootScope = NULL;
28
Andreas Hubereb1081f2016-07-28 13:13:24 -070029 CHECK(mScanner == NULL);
Andreas Huberc9410c72016-07-28 12:18:40 -070030}
31
32// static
33AST *AST::Parse(const char *path) {
34 AST *ast = new AST;
35 parseFile(ast, path);
36
37 return ast;
38}
39
40void *AST::scanner() {
41 return mScanner;
42}
43
44void AST::setScanner(void *scanner) {
45 mScanner = scanner;
46}
47
Andreas Huber84f89de2016-07-28 15:39:51 -070048bool AST::setPackage(const char *package) {
Andreas Huberda51b8e2016-07-28 16:00:57 -070049 mPackage.setTo(package);
50 CHECK(mPackage.isValid());
Andreas Huber84f89de2016-07-28 15:39:51 -070051
Andreas Huberda51b8e2016-07-28 16:00:57 -070052 if (mPackage.package().empty()
53 || mPackage.version().empty()
54 || !mPackage.name().empty()) {
Andreas Huber84f89de2016-07-28 15:39:51 -070055 return false;
56 }
57
Andreas Huber84f89de2016-07-28 15:39:51 -070058 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070059}
60
Andreas Huberda51b8e2016-07-28 16:00:57 -070061bool AST::addImport(const char * /* import */) {
Andreas Huber84f89de2016-07-28 15:39:51 -070062#if 0
Andreas Hubereb1081f2016-07-28 13:13:24 -070063 CHECK(!importPath->empty());
64
65 std::string leaf = importPath->itemAt(importPath->size() - 1);
66 scope()->addType(new RefType(leaf.c_str(), new HandleType));
Andreas Huber84f89de2016-07-28 15:39:51 -070067#endif
68
69 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070070}
71
Andreas Huberc9410c72016-07-28 12:18:40 -070072void AST::enterScope(Scope *container) {
73 mScopePath.push_back(container);
74}
75
76void AST::leaveScope() {
77 mScopePath.pop();
78}
79
80Scope *AST::scope() {
Andreas Hubereb1081f2016-07-28 13:13:24 -070081 CHECK(!mScopePath.empty());
Andreas Huberc9410c72016-07-28 12:18:40 -070082 return mScopePath.top();
83}
84
85Type *AST::lookupType(const char *name) {
Andreas Huber84f89de2016-07-28 15:39:51 -070086 FQName fqName(name);
87 CHECK(fqName.isValid());
88
Andreas Huberda51b8e2016-07-28 16:00:57 -070089 if (fqName.name().empty()) {
90 // Given a package and version???
91 return NULL;
92 }
93
Andreas Huber84f89de2016-07-28 15:39:51 -070094 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 Huberc9410c72016-07-28 12:18:40 -0700103 }
104 }
105
Andreas Huberda51b8e2016-07-28 16:00:57 -0700106 fqName.applyDefaults(mPackage.package(), mPackage.version());
Andreas Huber84f89de2016-07-28 15:39:51 -0700107
108 LOG(INFO) << "lookupType now looking for " << fqName.debugString();
109
Andreas Huberc9410c72016-07-28 12:18:40 -0700110 return NULL;
111}
112
113void AST::dump(Formatter &out) const {
114 mRootScope->dump(out);
115}
116
117} // namespace android;