Add parent_scope on all nodes

This commit is contained in:
Krzosa Karol
2022-06-10 16:12:47 +02:00
parent 3402b4fe4d
commit e2d07923c8
6 changed files with 109 additions and 86 deletions

57
ast.cpp
View File

@@ -55,6 +55,7 @@ struct Ast{
Ast_Kind kind;
Ast *parent;
Ast_Scope *parent_scope;
Ast_Flag flags;
};
@@ -226,6 +227,7 @@ struct Ast_Package : Ast_Scope{
Ast_##T *result = exp_alloc_type(pctx->perm, Ast_##T, AF_ZeroMemory);\
result->flags = iflags; \
result->kind = AST_##ikind; \
result->parent_scope = pctx->currently_parsed_scope; \
result->pos = ipos; \
result->id = ++pctx->unique_ids
@@ -417,38 +419,52 @@ ast_array(Token *pos, Ast_Expr *expr){
}
function Ast_Scope *
ast_decl_scope(Token *pos, Array<Ast_Decl *> decls){
begin_decl_scope(Allocator *scratch, Token *pos){
AST_NEW(Scope, SCOPE, pos, AST_DECL);
result->decls = decls.tight_copy(pctx->perm);
For(result->decls){
it->parent = result;
}
result->decls = {scratch};
pctx->currently_parsed_scope = result;
return result;
}
function void
finalize_decl_scope(Ast_Scope *scope){
scope->decls = scope->decls.tight_copy(pctx->perm);
pctx->currently_parsed_scope = scope->parent_scope;
For(scope->decls){
it->parent = scope;
}
}
function Ast_Scope *
ast_stmt_scope(Token *pos, Array<Ast *> stmts){
begin_stmt_scope(Allocator *scratch, Token *pos){
AST_NEW(Scope, SCOPE, pos, AST_STMT);
result->stmts = stmts.tight_copy(pctx->perm);
result->stmts = {scratch};
result->decls = {pctx->heap};
For(result->stmts){
it->parent = result;
}
pctx->currently_parsed_scope = result;
return result;
}
function void
finalize_stmt_scope(Ast_Scope *scope){
scope->stmts = scope->stmts.tight_copy(pctx->perm);
pctx->currently_parsed_scope = scope->parent_scope;
For(scope->stmts){
it->parent = scope;
}
}
function Ast_Decl *
ast_struct(Token *pos, Array<Ast_Decl *> decls){
ast_struct(Token *pos, Ast_Scope *scope){
AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE);
result->scope = ast_decl_scope(pos, decls);
result->scope = scope;
result->scope->parent = result;
return result;
}
function Ast_Decl *
ast_enum(Token *pos, Ast_Expr *typespec, Array<Ast_Decl *> decls){
ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope){
AST_NEW(Decl, ENUM, pos, AST_DECL | AST_AGGREGATE);
result->scope = ast_decl_scope(pos, decls);
result->scope = scope;
result->typespec = typespec;
result->scope->parent = result;
if(result->typespec) result->typespec->parent = result;
@@ -492,13 +508,12 @@ ast_type(Token *pos, Intern_String name, Ast_Resolved_Type *type){
return result;
}
function Ast_Package
ast_package(Allocator *allocator, Intern_String name, Array<Ast_Decl *> decls){
Ast_Package result = {};
result.kind = AST_PACKAGE;
result.decls = decls.copy(allocator);
result.name = name;
For(result.decls) it->parent = &result;
function Ast_Package *
ast_package(Token *pos, Allocator *allocator, Intern_String name){
AST_NEW(Package, PACKAGE, pos, 0);
result->kind = AST_PACKAGE;
result->decls = {allocator};
result->name = name;
return result;
}