blob: a527b465afcd97b7de7b80dfcacad2abff0c071a [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 -070048void AST::setVersion(const char *major, const char *minor) {
49 mVersion = "@";
50 mVersion.append(major);
51 mVersion.append(".");
52 mVersion.append(minor);
Andreas Hubereb1081f2016-07-28 13:13:24 -070053}
54
Andreas Huber84f89de2016-07-28 15:39:51 -070055bool AST::setPackage(const char *package) {
56 FQName fqName(package);
57 CHECK(fqName.isValid());
58
59 if (!fqName.package().empty() || !fqName.version().empty()) {
60 return false;
61 }
62
63 mPackage = package;
64
65 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070066}
67
Andreas Huber84f89de2016-07-28 15:39:51 -070068bool AST::addImport(const char *import) {
69#if 0
Andreas Hubereb1081f2016-07-28 13:13:24 -070070 CHECK(!importPath->empty());
71
72 std::string leaf = importPath->itemAt(importPath->size() - 1);
73 scope()->addType(new RefType(leaf.c_str(), new HandleType));
Andreas Huber84f89de2016-07-28 15:39:51 -070074#endif
75
76 return true;
Andreas Hubereb1081f2016-07-28 13:13:24 -070077}
78
Andreas Huberc9410c72016-07-28 12:18:40 -070079void AST::enterScope(Scope *container) {
80 mScopePath.push_back(container);
81}
82
83void AST::leaveScope() {
84 mScopePath.pop();
85}
86
87Scope *AST::scope() {
Andreas Hubereb1081f2016-07-28 13:13:24 -070088 CHECK(!mScopePath.empty());
Andreas Huberc9410c72016-07-28 12:18:40 -070089 return mScopePath.top();
90}
91
92Type *AST::lookupType(const char *name) {
Andreas Huber84f89de2016-07-28 15:39:51 -070093 LOG(INFO) << "lookupType " << name;
Andreas Huberc9410c72016-07-28 12:18:40 -070094
Andreas Huber84f89de2016-07-28 15:39:51 -070095 FQName fqName(name);
96 CHECK(fqName.isValid());
97
98 if (fqName.package().empty() && fqName.version().empty()) {
99 // This is just a plain identifier, resolve locally first if possible.
100
101 for (size_t i = mScopePath.size(); i-- > 0;) {
102 Type *type = mScopePath[i]->lookupType(name);
103
104 if (type != NULL) {
105 return new RefType(name, type);
106 }
Andreas Huberc9410c72016-07-28 12:18:40 -0700107 }
108 }
109
Andreas Huber84f89de2016-07-28 15:39:51 -0700110 fqName.applyDefaults(mPackage, mVersion);
111
112 LOG(INFO) << "lookupType now looking for " << fqName.debugString();
113
Andreas Huberc9410c72016-07-28 12:18:40 -0700114 return NULL;
115}
116
117void AST::dump(Formatter &out) const {
118 mRootScope->dump(out);
119}
120
121} // namespace android;