diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index 697d868..1704f8a 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -366,9 +366,7 @@ enum { struct Ast { uint64_t di; // Debug id, shouldn't ever be used in the program Token *pos; - Ast_Kind kind; - Ast_Scope *parent_scope; Ast_Flag flags; }; diff --git a/core_polymorph.cpp b/core_polymorph.cpp index f573479..6a3b817 100644 --- a/core_polymorph.cpp +++ b/core_polymorph.cpp @@ -113,30 +113,29 @@ void next(Ast_Iter *iter) { // We are not copying module and file Ast's // We are not copying resolved data -// @todo: Need to copy Ast->parent_scope somehow -// @todo: reserve array sizes and use perm -Ast *ast_copy(Ast *ast) { +Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) { if (!ast) return 0; switch (ast->kind) { case AST_SCOPE: { Ast_Scope *src = (Ast_Scope *)ast; Ast_Scope *dst = push_struct_copy(pctx->perm, Ast_Scope, ast); + dst->parent_scope = parent_scope; dst->implicit_imports = {}; For(src->implicit_imports) { - Ast_Scope *copy = (Ast_Scope *)ast_copy(it); + Ast_Scope *copy = (Ast_Scope *)ast_copy(it, dst); add(pctx->perm, &dst->implicit_imports, copy); } dst->decls = {}; For(src->decls) { - Ast_Decl *copy = (Ast_Decl *)ast_copy(it); + Ast_Decl *copy = (Ast_Decl *)ast_copy(it, dst); add(pctx->perm, &dst->decls, copy); } - dst->stmts = {}; + dst->stmts.init(pctx->perm, src->stmts.len); For(src->stmts) { - Ast *copy = ast_copy(it); + Ast *copy = ast_copy(it, dst); dst->stmts.add(copy); } return dst; @@ -149,41 +148,46 @@ Ast *ast_copy(Ast *ast) { case AST_VALUE: { Ast_Atom *src = (Ast_Atom *)ast; Ast_Atom *dst = push_struct_copy(pctx->perm, Ast_Atom, ast); + dst->parent_scope = parent_scope; return dst; } break; case AST_INDEX: { Ast_Index *src = (Ast_Index *)ast; Ast_Index *dst = push_struct_copy(pctx->perm, Ast_Index, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr); - dst->index = (Ast_Expr *)ast_copy(src->index); + dst->parent_scope = parent_scope; + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope); return dst; } break; case AST_UNARY: { Ast_Unary *src = (Ast_Unary *)ast; Ast_Unary *dst = push_struct_copy(pctx->perm, Ast_Unary, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr); + dst->parent_scope = parent_scope; + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); return dst; } break; case AST_BINARY: { Ast_Binary *src = (Ast_Binary *)ast; Ast_Binary *dst = push_struct_copy(pctx->perm, Ast_Binary, ast); - dst->left = (Ast_Expr *)ast_copy(src->left); - dst->right = (Ast_Expr *)ast_copy(src->right); + dst->parent_scope = parent_scope; + dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope); + dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope); return dst; } break; case AST_CALL_ITEM: { Ast_Call_Item *src = (Ast_Call_Item *)ast; Ast_Call_Item *dst = push_struct_copy(pctx->perm, Ast_Call_Item, ast); - dst->item = (Ast_Expr *)ast_copy(src->item); + dst->parent_scope = parent_scope; + dst->item = (Ast_Expr *)ast_copy(src->item, parent_scope); if (src->call_flags & CALL_INDEX) { - dst->index = (Ast_Expr *)ast_copy(src->index); + dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope); } else if (src->call_flags & CALL_NAME) { - dst->name = (Ast_Atom *)ast_copy(src->name); + dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope); } return dst; } break; @@ -192,11 +196,12 @@ Ast *ast_copy(Ast *ast) { case AST_CALL: { Ast_Call *src = (Ast_Call *)ast; Ast_Call *dst = push_struct_copy(pctx->perm, Ast_Call, ast); - dst->name = (Ast_Expr *)ast_copy(src->name); + dst->parent_scope = parent_scope; + dst->name = (Ast_Expr *)ast_copy(src->name, parent_scope); - dst->exprs = {}; + dst->exprs.init(pctx->perm, src->exprs.len); For(dst->exprs) { - auto copy = (Ast_Call_Item *)ast_copy(it); + auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope); dst->exprs.add(copy); } return dst; @@ -210,18 +215,21 @@ Ast *ast_copy(Ast *ast) { case AST_CONSTANT_ASSERT: { Ast_Builtin *src = (Ast_Builtin *)ast; Ast_Builtin *dst = push_struct_copy(pctx->perm, Ast_Builtin, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr); + dst->parent_scope = parent_scope; + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); return dst; } break; case AST_SWITCH: { Ast_Switch *src = (Ast_Switch *)ast; Ast_Switch *dst = push_struct_copy(pctx->perm, Ast_Switch, ast); - dst->value = (Ast_Expr *)ast_copy(src->value); - dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope); - dst->cases = {}; + dst->parent_scope = parent_scope; + dst->value = (Ast_Expr *)ast_copy(src->value, parent_scope); + dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope); + + dst->cases.init(pctx->perm, src->cases.len); For(src->cases) { - auto copy = (Ast_Switch_Case *)ast_copy(it); + auto copy = (Ast_Switch_Case *)ast_copy(it, parent_scope); assert(copy->kind == AST_SWITCH_CASE); dst->cases.add(copy); } @@ -231,11 +239,12 @@ Ast *ast_copy(Ast *ast) { case AST_SWITCH_CASE: { Ast_Switch_Case *src = (Ast_Switch_Case *)ast; Ast_Switch_Case *dst = push_struct_copy(pctx->perm, Ast_Switch_Case, ast); + dst->parent_scope = parent_scope; - dst->scope = (Ast_Scope *)ast_copy(src->scope); - dst->labels = {}; + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); + dst->labels.init(pctx->perm, src->labels.len); For(src->labels) { - auto copy = (Ast_Expr *)ast_copy(it); + auto copy = (Ast_Expr *)ast_copy(it, parent_scope); dst->labels.add(copy); } return dst; @@ -244,11 +253,12 @@ Ast *ast_copy(Ast *ast) { case AST_VAR_UNPACK: { Ast_Var_Unpack *src = (Ast_Var_Unpack *)ast; Ast_Var_Unpack *dst = push_struct_copy(pctx->perm, Ast_Var_Unpack, ast); + dst->parent_scope = parent_scope; - dst->expr = (Ast_Expr *)ast_copy(src->expr); - dst->vars = {}; + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->vars.init(pctx->perm, src->vars.len); For(src->vars) { - auto copy = (Ast_Decl *)ast_copy(it); + auto copy = (Ast_Decl *)ast_copy(it, parent_scope); dst->vars.add(copy); } return dst; @@ -258,6 +268,7 @@ Ast *ast_copy(Ast *ast) { case AST_BREAK: { Ast *src = (Ast *)ast; Ast *dst = push_struct_copy(pctx->perm, Ast, ast); + dst->parent_scope = parent_scope; return dst; } break; @@ -271,42 +282,42 @@ Ast *ast_copy(Ast *ast) { case AST_VAR: { Ast_Decl *src = (Ast_Decl *)ast; Ast_Decl *dst = push_struct_copy(pctx->perm, Ast_Decl, ast); + dst->parent_scope = parent_scope; dst->overload_op_info = push_struct_copy(pctx->perm, Ast_Operator_Info, src->overload_op_info); // omitting polymorphs // omitting polymorph parameters - // For(src->polymorph_parameters) { - // auto copy = (Ast_Decl *)ast_copy(it); - // dst->polymorph_parameters.add(copy); - // } - dst->scope = (Ast_Scope *)ast_copy(src->scope); - dst->typespec = (Ast_Expr *)ast_copy(src->typespec); - dst->expr = (Ast_Expr *)ast_copy(src->expr); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); + dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope); + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); return dst; } break; case AST_ARRAY: { Ast_Array *src = (Ast_Array *)ast; Ast_Array *dst = push_struct_copy(pctx->perm, Ast_Array, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr); + dst->parent_scope = parent_scope; + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); return dst; } break; case AST_FOR: { Ast_For *src = (Ast_For *)ast; Ast_For *dst = push_struct_copy(pctx->perm, Ast_For, ast); - dst->init = (Ast_Expr *)ast_copy(src->init); - dst->cond = (Ast_Expr *)ast_copy(src->cond); - dst->iter = (Ast_Expr *)ast_copy(src->iter); - dst->scope = (Ast_Scope *)ast_copy(src->scope); + dst->parent_scope = parent_scope; + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); + dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope); + dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope); + dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope); return dst; } break; case AST_IF: { Ast_If *src = (Ast_If *)ast; Ast_If *dst = push_struct_copy(pctx->perm, Ast_If, ast); - dst->ifs = {}; + dst->parent_scope = parent_scope; + dst->ifs.init(pctx->perm, src->ifs.len); For(src->ifs) { - auto copy = (Ast_If_Node *)ast_copy(it); + auto copy = (Ast_If_Node *)ast_copy(it, parent_scope); assert(copy->kind == AST_IF_NODE); dst->ifs.add(copy); } @@ -316,17 +327,20 @@ Ast *ast_copy(Ast *ast) { case AST_IF_NODE: { Ast_If_Node *src = (Ast_If_Node *)ast; Ast_If_Node *dst = push_struct_copy(pctx->perm, Ast_If_Node, ast); - dst->expr = (Ast_Expr *)ast_copy(src->expr); - dst->init = (Ast_Binary *)ast_copy(src->init); - dst->scope = (Ast_Scope *)ast_copy(src->scope); + dst->parent_scope = parent_scope; + dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); + dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); return dst; } break; case AST_RETURN: { Ast_Return *src = (Ast_Return *)ast; Ast_Return *dst = push_struct_copy(pctx->perm, Ast_Return, ast); + + dst->expr.init(pctx->perm, src->expr.len); For(src->expr) { - auto copy = (Ast_Expr *)ast_copy(it); + auto copy = (Ast_Expr *)ast_copy(it, parent_scope); dst->expr.add(copy); } return dst; @@ -335,15 +349,20 @@ Ast *ast_copy(Ast *ast) { case AST_LAMBDA_EXPR: { Ast_Lambda *src = (Ast_Lambda *)ast; Ast_Lambda *dst = push_struct_copy(pctx->perm, Ast_Lambda, ast); + dst->parent_scope = parent_scope; + + dst->args.init(pctx->perm, src->args.len); For(src->args) { - auto copy = (Ast_Decl *)ast_copy(it); + auto copy = (Ast_Decl *)ast_copy(it, parent_scope); dst->args.add(copy); } + + dst->ret.init(pctx->perm, src->ret.len); For(src->ret) { - auto copy = (Ast_Expr *)ast_copy(it); + auto copy = (Ast_Expr *)ast_copy(it, parent_scope); dst->ret.add(copy); } - dst->scope = (Ast_Scope *)ast_copy(src->scope); + dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); return dst; } break; }