blob: 596722ee0a152964c1e0e8964dcb98ea250c143e [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "AST.h"
2
3#include "Formatter.h"
Andreas Hubereb1081f2016-07-28 13:13:24 -07004#include "HandleType.h"
5#include "RefType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07006#include "Scope.h"
7
Andreas Hubereb1081f2016-07-28 13:13:24 -07008#include <android-base/logging.h>
Andreas Huberc9410c72016-07-28 12:18:40 -07009#include <stdlib.h>
10
11extern void parseFile(android::AST *ast, const char *path);
12
13namespace android {
14
15AST::AST()
16 : mScanner(NULL),
17 mRootScope(new Scope("root")) {
18 enterScope(mRootScope);
19}
20
21AST::~AST() {
Andreas Hubereb1081f2016-07-28 13:13:24 -070022 CHECK(scope() == mRootScope);
Andreas Huberc9410c72016-07-28 12:18:40 -070023 leaveScope();
24
25 delete mRootScope;
26 mRootScope = NULL;
27
Andreas Hubereb1081f2016-07-28 13:13:24 -070028 CHECK(mScanner == NULL);
Andreas Huberc9410c72016-07-28 12:18:40 -070029}
30
31// static
32AST *AST::Parse(const char *path) {
33 AST *ast = new AST;
34 parseFile(ast, path);
35
36 return ast;
37}
38
39void *AST::scanner() {
40 return mScanner;
41}
42
43void AST::setScanner(void *scanner) {
44 mScanner = scanner;
45}
46
Andreas Hubereb1081f2016-07-28 13:13:24 -070047void AST::setVersion(const char *, const char *) {
48}
49
50void AST::setPackage(Vector<std::string> *) {
51}
52
53void 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 Huberc9410c72016-07-28 12:18:40 -070060void AST::enterScope(Scope *container) {
61 mScopePath.push_back(container);
62}
63
64void AST::leaveScope() {
65 mScopePath.pop();
66}
67
68Scope *AST::scope() {
Andreas Hubereb1081f2016-07-28 13:13:24 -070069 CHECK(!mScopePath.empty());
Andreas Huberc9410c72016-07-28 12:18:40 -070070 return mScopePath.top();
71}
72
73Type *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
85void AST::dump(Formatter &out) const {
86 mRootScope->dump(out);
87}
88
89} // namespace android;