This commit is contained in:
Krzosa Karol
2023-04-02 12:33:00 +02:00
parent 6320d705a1
commit 18c0af2b99

View File

@@ -27,6 +27,7 @@ Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) {
// We are not copying resolved data // We are not copying resolved data
Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Array<Ast_Call_Item *> *with) { Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Array<Ast_Call_Item *> *with) {
if (!ast) return 0; if (!ast) return 0;
Ast *result = 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;
@@ -43,7 +44,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
Ast *copy = ast_copy(it, dst, replace, with); Ast *copy = ast_copy(it, dst, replace, with);
dst->stmts.add(copy); dst->stmts.add(copy);
} }
return dst; result = dst;
} break; } break;
case AST_MODULE: invalid_codepath; break; case AST_MODULE: invalid_codepath; break;
@@ -66,12 +67,12 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
} }
} }
return dst; result = dst;
} break; } break;
case AST_VALUE: { case AST_VALUE: {
Ast_Atom *src = (Ast_Atom *)ast; Ast_Atom *src = (Ast_Atom *)ast;
Ast_Atom *dst = ast_create_copy(parent_scope, Ast_Atom, ast); Ast_Atom *dst = ast_create_copy(parent_scope, Ast_Atom, ast);
return dst; result = dst;
} break; } break;
case AST_INDEX: { case AST_INDEX: {
@@ -79,14 +80,14 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
Ast_Index *dst = ast_create_copy(parent_scope, Ast_Index, ast); Ast_Index *dst = ast_create_copy(parent_scope, Ast_Index, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with); dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_UNARY: { case AST_UNARY: {
Ast_Unary *src = (Ast_Unary *)ast; Ast_Unary *src = (Ast_Unary *)ast;
Ast_Unary *dst = ast_create_copy(parent_scope, Ast_Unary, ast); Ast_Unary *dst = ast_create_copy(parent_scope, Ast_Unary, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_BINARY: { case AST_BINARY: {
@@ -94,7 +95,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
Ast_Binary *dst = ast_create_copy(parent_scope, Ast_Binary, ast); Ast_Binary *dst = ast_create_copy(parent_scope, Ast_Binary, ast);
dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope, replace, with); dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope, replace, with);
dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope, replace, with); dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_CALL_ITEM: { case AST_CALL_ITEM: {
@@ -107,7 +108,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
else if (src->call_flags & CALL_NAME) { else if (src->call_flags & CALL_NAME) {
dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope, replace, with); dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope, replace, with);
} }
return dst; result = dst;
} break; } break;
case AST_COMPOUND: case AST_COMPOUND:
@@ -121,7 +122,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope, replace, with); auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope, replace, with);
dst->exprs.add(copy); dst->exprs.add(copy);
} }
return dst; result = dst;
} break; } break;
case AST_TYPE_OF: case AST_TYPE_OF:
@@ -133,7 +134,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
Ast_Builtin *src = (Ast_Builtin *)ast; Ast_Builtin *src = (Ast_Builtin *)ast;
Ast_Builtin *dst = ast_create_copy(parent_scope, Ast_Builtin, ast); Ast_Builtin *dst = ast_create_copy(parent_scope, Ast_Builtin, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_SWITCH: { case AST_SWITCH: {
@@ -148,7 +149,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
assert(copy->kind == AST_SWITCH_CASE); assert(copy->kind == AST_SWITCH_CASE);
dst->cases.add(copy); dst->cases.add(copy);
} }
return dst; result = dst;
} break; } break;
case AST_SWITCH_CASE: { case AST_SWITCH_CASE: {
@@ -161,7 +162,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with); auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
dst->labels.add(copy); dst->labels.add(copy);
} }
return dst; result = dst;
} break; } break;
case AST_VAR_UNPACK: { case AST_VAR_UNPACK: {
@@ -174,14 +175,14 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with); auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with);
dst->vars.add(copy); dst->vars.add(copy);
} }
return dst; result = dst;
} break; } break;
case AST_PASS: case AST_PASS:
case AST_BREAK: { case AST_BREAK: {
Ast *src = (Ast *)ast; Ast *src = (Ast *)ast;
Ast *dst = ast_create_copy(parent_scope, Ast, ast); Ast *dst = ast_create_copy(parent_scope, Ast, ast);
return dst; result = dst;
} break; } break;
case AST_NAMESPACE: case AST_NAMESPACE:
@@ -200,14 +201,14 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope, replace, with); dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope, replace, with);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_ARRAY: { case AST_ARRAY: {
Ast_Array *src = (Ast_Array *)ast; Ast_Array *src = (Ast_Array *)ast;
Ast_Array *dst = ast_create_copy(parent_scope, Ast_Array, ast); Ast_Array *dst = ast_create_copy(parent_scope, Ast_Array, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_FOR: { case AST_FOR: {
@@ -217,7 +218,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope, replace, with); dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope, replace, with);
dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope, replace, with); dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope, replace, with);
dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope, replace, with); dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_IF: { case AST_IF: {
@@ -229,7 +230,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
assert(copy->kind == AST_IF_NODE); assert(copy->kind == AST_IF_NODE);
dst->ifs.add(copy); dst->ifs.add(copy);
} }
return dst; result = dst;
} break; } break;
case AST_IF_NODE: { case AST_IF_NODE: {
@@ -238,7 +239,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope, replace, with); dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope, replace, with);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
case AST_RETURN: { case AST_RETURN: {
@@ -250,7 +251,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with); auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
dst->expr.add(copy); dst->expr.add(copy);
} }
return dst; result = dst;
} break; } break;
case AST_LAMBDA_EXPR: { case AST_LAMBDA_EXPR: {
@@ -269,12 +270,14 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
dst->ret.add(copy); dst->ret.add(copy);
} }
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with); dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
return dst; result = dst;
} break; } break;
default: assert(!"Invalid default case"); default: assert(!"Invalid default case");
} }
invalid_return; assert(result);
unset_flag(result->flags, AST_POLYMORPH | AST_PARENT_POLYMORPH);
return result;
} }
Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<Ast_Call_Item *> params, Ast_Scope *field_access_scope) { Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<Ast_Call_Item *> params, Ast_Scope *field_access_scope) {
@@ -310,8 +313,6 @@ Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<As
if (!result) { if (!result) {
result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &poly->polymorph_parameters, &params); result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &poly->polymorph_parameters, &params);
unset_flag(result->flags, AST_POLYMORPH);
unset_flag(result->flags, AST_PARENT_POLYMORPH);
if (poly->kind == AST_STRUCT || poly->kind == AST_UNION) result->type_val = type_incomplete(result); if (poly->kind == AST_STRUCT || poly->kind == AST_UNION) result->type_val = type_incomplete(result);
result->polymorph_hash = hash; result->polymorph_hash = hash;
@@ -361,8 +362,6 @@ Ast_Decl *get_or_instantiate_polymorph_lambda(Token *pos, Ast_Decl *poly, Array<
} }
} }
unset_flag(result->flags, AST_POLYMORPH);
unset_flag(result->flags, AST_PARENT_POLYMORPH);
result->polymorph_hash = hash; result->polymorph_hash = hash;
assert(result->di != poly->di); assert(result->di != poly->di);