3 #include <unordered_set>
6 #include "ast/AstNode.h"
7 #include "ast/DeclContext.h"
9 #include "diagnostics/Diagnostics.h"
10 #include "diagnostics/Location.h"
11 #include "parsetree/ParseTree.h"
12 #include "utils/BumpAllocator.h"
16 using string = std::string;
29 BuiltInType* BuildBuiltInType(parsetree::
BasicType::Type type,
31 BuiltInType* BuildBuiltInType(parsetree::
Literal::Type type);
32 BuiltInType* BuildBuiltInType(ast::BuiltInType::Kind type);
39 ScopeID
const* scope,
Expr* init =
nullptr);
41 string_view name,
Expr* init =
nullptr,
42 bool allowFinal =
false);
48 LinkingUnit* BuildLinkingUnit(array_ref<CompilationUnit*> compilationUnits);
50 array_ref<ImportDeclaration> imports,
54 array_ref<ReferenceType*> interfaces,
55 array_ref<Decl*> classBodyDecls);
58 array_ref<ReferenceType*> extends,
59 array_ref<Decl*> interfaceBodyDecls);
61 string_view name,
Type* returnType,
62 array_ref<VarDecl*> parameters,
bool isConstructor,
68 BlockStatement* BuildBlockStatement(array_ref<Stmt*> stmts);
69 DeclStmt* BuildDeclStmt(
VarDecl* decl);
70 ExprStmt* BuildExprStmt(
Expr* expr);
71 IfStmt* BuildIfStmt(
Expr* condition,
Stmt* thenStmt,
Stmt* elseStmt =
nullptr);
72 WhileStmt* BuildWhileStmt(
Expr* condition,
Stmt* body);
73 ForStmt* BuildForStmt(
Stmt* init,
Expr* condition,
Stmt* update,
Stmt* body);
75 NullStmt* BuildNullStmt() {
return alloc.new_object<NullStmt>(); }
78 BumpAllocator& allocator() {
return alloc; }
83 lexicalLocalScope.clear();
84 lexicalLocalDecls.clear();
85 lexicalLocalDeclStack.clear();
86 currentScope_ = ScopeID::New(alloc);
98 if(lexicalLocalScope.find(nameCpy) != lexicalLocalScope.end())
return false;
99 lexicalLocalScope[nameCpy] = decl;
100 lexicalLocalDecls.push_back(decl);
101 lexicalLocalDeclStack.push_back(decl);
111 int size = lexicalLocalDeclStack.size();
112 currentScope_ = currentScope_
->next(alloc
, currentScope_
);
124 for(
int i = lexicalLocalDeclStack.size() - 1; i >= size; --i)
125 lexicalLocalScope.erase(lexicalLocalDeclStack[i]->name().data());
126 lexicalLocalDeclStack.resize(size);
127 assert(currentScope_->parent() !=
nullptr);
129 currentScope_
->next(alloc
, currentScope_->parent()->parent()
);
137 ast::ScopeID
const* NextScopeID() {
138 currentScope_ = currentScope_
->next(alloc
, currentScope_->parent()
);
139 return currentScope_;
142 ast::ScopeID
const* CurrentScopeID()
const {
return currentScope_; }
144 ast::ScopeID
const* NextFieldScopeID() {
145 currentFieldScope_ = currentFieldScope_
->next(alloc
, currentFieldScope_
);
146 return currentFieldScope_;
149 ast::ScopeID
const* CurrentFieldScopeID()
const {
return currentFieldScope_; }
151 void ResetFieldScope() {
152 currentFieldScope_ = ScopeID::New(alloc);
156 BumpAllocator& alloc;
158 std::vector<VarDecl*> lexicalLocalDeclStack;
159 std::vector<VarDecl*> lexicalLocalDecls;
160 std::unordered_map<std::string, VarDecl
const*> lexicalLocalScope;
164 ast::ScopeID
const* currentScope_;
166 ast::ScopeID
const* currentFieldScope_;