Polymorphs, recursice typespec tagging

This commit is contained in:
Krzosa Karol
2023-04-01 10:11:12 +02:00
parent 5fb4999ca8
commit db3790016d
4 changed files with 137 additions and 63 deletions

View File

@@ -1,22 +1,5 @@
Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<Ast_Call_Item *> params, Ast_Scope *field_access_scope) {
if (params.len != poly->polymorph_parameters.len) {
compiler_error(pos, "Invalid count of polymorphic arguments");
}
For(params) {
bool indexed = (it->flags & CALL_INDEX);
bool named = (it->flags & CALL_NAME);
if (indexed == false && named == false) {
compiler_error(it->pos, "Polymorphic type cannot have named/indexed arguments");
}
Operand op = resolve_expr(it->item, AST_CANT_BE_NULL, 0, field_access_scope);
}
return 0;
}
#define ast_create_copy(parent_scope, T, ast) (T *)ast__create_copy(parent_scope, sizeof(T), ast)
Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) {
if (ast == 0) return 0;
@@ -28,7 +11,7 @@ Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) {
// We are not copying module and file Ast's
// We are not copying resolved data
Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Array<Ast_Call_Item *> *with) {
if (!ast) return 0;
switch (ast->kind) {
case AST_SCOPE: {
@@ -37,13 +20,13 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
dst->decls = {};
For(src->decls) {
Ast_Decl *copy = (Ast_Decl *)ast_copy(it, src);
Ast_Decl *copy = (Ast_Decl *)ast_copy(it, src, replace, with);
add(pctx->perm, &dst->decls, copy);
}
dst->stmts.init(pctx->perm, src->stmts.len);
For(src->stmts) {
Ast *copy = ast_copy(it, dst);
Ast *copy = ast_copy(it, dst, replace, with);
dst->stmts.add(copy);
}
return dst;
@@ -52,7 +35,20 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_MODULE: invalid_codepath; break;
case AST_FILE: invalid_codepath; break;
case AST_IDENT:
case AST_IDENT: {
Ast_Atom *src = (Ast_Atom *)ast;
Ast_Atom *dst = ast_create_copy(parent_scope, Ast_Atom, ast);
if ((dst->flags & AST_TYPESPEC)) {
For(*replace) {
if (it->name == dst->intern_val) {
Breakpoint;
}
}
}
return dst;
} break;
case AST_VALUE: {
Ast_Atom *src = (Ast_Atom *)ast;
Ast_Atom *dst = ast_create_copy(parent_scope, Ast_Atom, ast);
@@ -62,35 +58,35 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_INDEX: {
Ast_Index *src = (Ast_Index *)ast;
Ast_Index *dst = ast_create_copy(parent_scope, Ast_Index, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with);
return dst;
} break;
case AST_UNARY: {
Ast_Unary *src = (Ast_Unary *)ast;
Ast_Unary *dst = ast_create_copy(parent_scope, Ast_Unary, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst;
} break;
case AST_BINARY: {
Ast_Binary *src = (Ast_Binary *)ast;
Ast_Binary *dst = ast_create_copy(parent_scope, Ast_Binary, ast);
dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope);
dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope);
dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope, replace, with);
dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope, replace, with);
return dst;
} break;
case AST_CALL_ITEM: {
Ast_Call_Item *src = (Ast_Call_Item *)ast;
Ast_Call_Item *dst = ast_create_copy(parent_scope, Ast_Call_Item, ast);
dst->item = (Ast_Expr *)ast_copy(src->item, parent_scope);
dst->item = (Ast_Expr *)ast_copy(src->item, parent_scope, replace, with);
if (src->call_flags & CALL_INDEX) {
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with);
}
else if (src->call_flags & CALL_NAME) {
dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope);
dst->name = (Ast_Atom *)ast_copy(src->name, parent_scope, replace, with);
}
return dst;
} break;
@@ -99,11 +95,11 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_CALL: {
Ast_Call *src = (Ast_Call *)ast;
Ast_Call *dst = ast_create_copy(parent_scope, Ast_Call, ast);
dst->name = (Ast_Expr *)ast_copy(src->name, parent_scope);
dst->name = (Ast_Expr *)ast_copy(src->name, parent_scope, replace, with);
dst->exprs.init(pctx->perm, src->exprs.len);
For(dst->exprs) {
auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope);
auto copy = (Ast_Call_Item *)ast_copy(it, parent_scope, replace, with);
dst->exprs.add(copy);
}
return dst;
@@ -117,19 +113,19 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_CONSTANT_ASSERT: {
Ast_Builtin *src = (Ast_Builtin *)ast;
Ast_Builtin *dst = ast_create_copy(parent_scope, Ast_Builtin, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst;
} break;
case AST_SWITCH: {
Ast_Switch *src = (Ast_Switch *)ast;
Ast_Switch *dst = ast_create_copy(parent_scope, Ast_Switch, ast);
dst->value = (Ast_Expr *)ast_copy(src->value, parent_scope);
dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope);
dst->value = (Ast_Expr *)ast_copy(src->value, parent_scope, replace, with);
dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope, replace, with);
dst->cases.init(pctx->perm, src->cases.len);
For(src->cases) {
auto copy = (Ast_Switch_Case *)ast_copy(it, parent_scope);
auto copy = (Ast_Switch_Case *)ast_copy(it, parent_scope, replace, with);
assert(copy->kind == AST_SWITCH_CASE);
dst->cases.add(copy);
}
@@ -140,10 +136,10 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
Ast_Switch_Case *src = (Ast_Switch_Case *)ast;
Ast_Switch_Case *dst = ast_create_copy(parent_scope, Ast_Switch_Case, ast);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
dst->labels.init(pctx->perm, src->labels.len);
For(src->labels) {
auto copy = (Ast_Expr *)ast_copy(it, parent_scope);
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
dst->labels.add(copy);
}
return dst;
@@ -153,10 +149,10 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
Ast_Var_Unpack *src = (Ast_Var_Unpack *)ast;
Ast_Var_Unpack *dst = ast_create_copy(parent_scope, Ast_Var_Unpack, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
dst->vars.init(pctx->perm, src->vars.len);
For(src->vars) {
auto copy = (Ast_Decl *)ast_copy(it, parent_scope);
auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with);
dst->vars.add(copy);
}
return dst;
@@ -182,26 +178,26 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
// dst->overload_op_info = ast_create_copy(parent_scope, Ast_Operator_Info, src->overload_op_info);
// omitting polymorphs
// omitting polymorph parameters
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);
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->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst;
} break;
case AST_ARRAY: {
Ast_Array *src = (Ast_Array *)ast;
Ast_Array *dst = ast_create_copy(parent_scope, Ast_Array, ast);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
return dst;
} break;
case AST_FOR: {
Ast_For *src = (Ast_For *)ast;
Ast_For *dst = ast_create_copy(parent_scope, Ast_For, ast);
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);
dst->scope = (Ast_Scope *)ast_copy(src->scope, 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->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope, replace, with);
return dst;
} break;
@@ -210,7 +206,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
Ast_If *dst = ast_create_copy(parent_scope, Ast_If, ast);
dst->ifs.init(pctx->perm, src->ifs.len);
For(src->ifs) {
auto copy = (Ast_If_Node *)ast_copy(it, parent_scope);
auto copy = (Ast_If_Node *)ast_copy(it, parent_scope, replace, with);
assert(copy->kind == AST_IF_NODE);
dst->ifs.add(copy);
}
@@ -220,9 +216,9 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_IF_NODE: {
Ast_If_Node *src = (Ast_If_Node *)ast;
Ast_If_Node *dst = ast_create_copy(parent_scope, Ast_If_Node, ast);
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);
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->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
return dst;
} break;
@@ -232,7 +228,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
dst->expr.init(pctx->perm, src->expr.len);
For(src->expr) {
auto copy = (Ast_Expr *)ast_copy(it, parent_scope);
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
dst->expr.add(copy);
}
return dst;
@@ -244,16 +240,16 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
dst->args.init(pctx->perm, src->args.len);
For(src->args) {
auto copy = (Ast_Decl *)ast_copy(it, parent_scope);
auto copy = (Ast_Decl *)ast_copy(it, parent_scope, replace, with);
dst->args.add(copy);
}
dst->ret.init(pctx->perm, src->ret.len);
For(src->ret) {
auto copy = (Ast_Expr *)ast_copy(it, parent_scope);
auto copy = (Ast_Expr *)ast_copy(it, parent_scope, replace, with);
dst->ret.add(copy);
}
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
return dst;
} break;
@@ -262,7 +258,46 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
invalid_return;
}
Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Array<Ast_Call_Item *> params, Ast_Scope *field_access_scope) {
if (params.len != poly->polymorph_parameters.len) compiler_error(pos, "Invalid count of polymorphic arguments");
int i = 0;
uint64_t hash = 91;
For(params) {
bool indexed = (it->flags & CALL_INDEX);
bool named = (it->flags & CALL_NAME);
if (indexed == false && named == false) compiler_error(it->pos, "Polymorphic type cannot have named/indexed arguments");
Ast_Decl *poly_decl = poly->polymorph_parameters[i++];
resolve_decl(poly_decl);
if (poly_decl->type != pctx->type_type) compiler_error(poly_decl->pos, "Invalid type of polymorphic struct argument");
Operand op = resolve_expr(it->item, AST_CANT_BE_NULL, 0, field_access_scope);
if (!op.is_const) compiler_error(it->pos, "Argument is required to be compile time known");
if (op.type != pctx->type_type) compiler_error(it->pos, "Struct argument required to be a type");
hash = hash_mix(hash, hash_ptr(op.type_val));
}
Ast_Decl *result = 0;
For(poly->polymorphs) {
if (it->polymorph_hash == hash) {
result = it;
break;
}
}
if (!result) {
result = (Ast_Decl *)ast_copy(poly, poly->parent_scope, &poly->polymorph_parameters, &params);
poly->polymorphs.add(result);
}
return result;
}
//
// ITERATOR
//
const unsigned AST_NODE_END = 128;