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