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

@@ -20,6 +20,39 @@ _ast_new(size_t size, Ast_Kind kind, Token *pos, Ast_Flag flags = 0) {
return result; return result;
} }
CORE_Static void
set_flag_typespec(Ast_Expr *expr) {
if (!expr) return;
set_flag(expr->flags, AST_TYPESPEC);
switch (expr->kind) {
case AST_UNARY: {
Ast_Unary *n = (Ast_Unary *)expr;
set_flag_typespec(n->expr);
} break;
case AST_ARRAY: {
Ast_Array *n = (Ast_Array *)expr;
set_flag_typespec(n->base);
set_flag_typespec(n->expr);
} break;
case AST_CALL: {
Ast_Call *n = (Ast_Call *)expr;
For(n->exprs) set_flag_typespec(it);
set_flag_typespec(n->name);
} break;
case AST_CALL_ITEM: {
Ast_Call_Item *n = (Ast_Call_Item *)expr;
set_flag_typespec(n->item);
set_flag_typespec(n->index);
} break;
case AST_IDENT:
break;
invalid_default_case;
}
}
CORE_Static void CORE_Static void
propagate_polymorphic(Ast *to, Ast *from) { propagate_polymorphic(Ast *to, Ast *from) {
if (is_flag_set(from->flags, AST_IDENT_POLYMORPH)) set_flag(to->flags, AST_IDENT_POLYMORPH); if (is_flag_set(from->flags, AST_IDENT_POLYMORPH)) set_flag(to->flags, AST_IDENT_POLYMORPH);
@@ -230,8 +263,11 @@ ast_struct(Token *pos, Ast_Scope *scope, Ast_Kind kind, Array<Ast_Decl *> polymo
AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE); AST_NEW(Decl, STRUCT, pos, AST_DECL | AST_AGGREGATE);
result->kind = kind; result->kind = kind;
result->scope = scope; result->scope = scope;
result->polymorph_parameters = polymorph_parameters; if (polymorph_parameters.len) {
if (polymorph_parameters.len) set_flag(result->flags, AST_POLYMORPH); result->polymorph_parameters = polymorph_parameters;
set_flag(result->flags, AST_POLYMORPH);
result->polymorphs.allocator = pctx->heap;
}
return result; return result;
} }
@@ -240,7 +276,7 @@ ast_enum(Token *pos, Ast_Expr *typespec, Ast_Scope *scope) {
AST_NEW(Decl, ENUM, pos, AST_DECL); AST_NEW(Decl, ENUM, pos, AST_DECL);
result->scope = scope; result->scope = scope;
result->typespec = typespec; result->typespec = typespec;
if (typespec) set_flag(typespec->flags, AST_TYPESPEC); set_flag_typespec(typespec);
return result; return result;
} }
@@ -249,7 +285,7 @@ ast_var(Token *pos, Ast_Expr *typespec, Intern_String name, Ast_Expr *expr) {
AST_NEW(Decl, VAR, pos, AST_DECL); AST_NEW(Decl, VAR, pos, AST_DECL);
result->name = name; result->name = name;
result->typespec = typespec; result->typespec = typespec;
if (typespec) set_flag(typespec->flags, AST_TYPESPEC); set_flag_typespec(typespec);
result->expr = expr; result->expr = expr;
return result; return result;
} }

View File

@@ -606,6 +606,7 @@ struct Ast_Decl : Ast {
uint64_t operator_overload_arguments_hash; uint64_t operator_overload_arguments_hash;
Ast_Operator_Info *overload_op_info; Ast_Operator_Info *overload_op_info;
uint64_t polymorph_hash;
Array<Ast_Decl *> polymorph_parameters; Array<Ast_Decl *> polymorph_parameters;
Array<Ast_Decl *> polymorphs; // instantiated polymorphs Array<Ast_Decl *> polymorphs; // instantiated polymorphs

View File

@@ -476,7 +476,7 @@ parse_parameter_list(Arena *arena) {
Ast_Decl *param = ast_new(Ast_Decl, AST_VAR, name, AST_DECL); Ast_Decl *param = ast_new(Ast_Decl, AST_VAR, name, AST_DECL);
param->name = name->intern_val; param->name = name->intern_val;
param->typespec = parse_expr(); param->typespec = parse_expr();
set_flag(param->typespec->flags, AST_TYPESPEC); set_flag_typespec(param->typespec);
propagate_polymorphic(param, param->typespec); propagate_polymorphic(param, param->typespec);
propagate_polymorphic(param, name); propagate_polymorphic(param, name);
@@ -506,7 +506,8 @@ parse_lambda(Token *token) {
if (token_match(TK_Colon)) { if (token_match(TK_Colon)) {
do { do {
Ast_Expr *typespec = parse_expr(); Ast_Expr *typespec = parse_expr();
set_flag(typespec->flags, AST_TYPESPEC); set_flag_typespec(typespec);
ret.add(typespec); ret.add(typespec);
} while (token_match(TK_Comma)); } while (token_match(TK_Comma));
} }
@@ -743,7 +744,7 @@ parse_struct(Token *pos, Ast_Kind kind) {
token_expect(TK_Colon); token_expect(TK_Colon);
Ast_Expr *typespec = parse_expr(); Ast_Expr *typespec = parse_expr();
set_flag(typespec->flags, AST_TYPESPEC); set_flag_typespec(typespec);
Ast_Decl *decl = ast_var(token, typespec, token->intern_val, 0); Ast_Decl *decl = ast_var(token, typespec, token->intern_val, 0);
set_flag(decl->flags, AST_AGGREGATE_CHILD); set_flag(decl->flags, AST_AGGREGATE_CHILD);
@@ -967,7 +968,8 @@ parse_decl(B32 is_global) {
else if (token_match(TK_Identifier, TK_Colon)) { else if (token_match(TK_Identifier, TK_Colon)) {
Ast_Expr *typespec = parse_expr(); Ast_Expr *typespec = parse_expr();
set_flag(typespec->flags, AST_TYPESPEC); set_flag_typespec(typespec);
Ast_Expr *expr = parse_assign_expr(); Ast_Expr *expr = parse_assign_expr();
if (token_match_pound(pctx->intern_foreign)) if (token_match_pound(pctx->intern_foreign))
set_flag(flags, AST_FOREIGN); set_flag(flags, AST_FOREIGN);

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) #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) { Ast *ast__create_copy(Ast_Scope *parent_scope, size_t size, Ast *ast) {
if (ast == 0) return 0; 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 module and file Ast's
// We are not copying resolved data // 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; if (!ast) return 0;
switch (ast->kind) { switch (ast->kind) {
case AST_SCOPE: { case AST_SCOPE: {
@@ -37,13 +20,13 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
dst->decls = {}; dst->decls = {};
For(src->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); add(pctx->perm, &dst->decls, copy);
} }
dst->stmts.init(pctx->perm, src->stmts.len); dst->stmts.init(pctx->perm, src->stmts.len);
For(src->stmts) { For(src->stmts) {
Ast *copy = ast_copy(it, dst); Ast *copy = ast_copy(it, dst, replace, with);
dst->stmts.add(copy); dst->stmts.add(copy);
} }
return dst; return dst;
@@ -52,7 +35,20 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_MODULE: invalid_codepath; break; case AST_MODULE: invalid_codepath; break;
case AST_FILE: 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: { 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);
@@ -62,35 +58,35 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_INDEX: { case AST_INDEX: {
Ast_Index *src = (Ast_Index *)ast; Ast_Index *src = (Ast_Index *)ast;
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); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope); dst->index = (Ast_Expr *)ast_copy(src->index, parent_scope, replace, with);
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 = 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); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
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 = 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); dst->left = (Ast_Expr *)ast_copy(src->left, parent_scope, replace, with);
dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope); dst->right = (Ast_Expr *)ast_copy(src->right, parent_scope, replace, with);
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 = ast_create_copy(parent_scope, 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) { 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) { 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; return dst;
} break; } break;
@@ -99,11 +95,11 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_CALL: { case AST_CALL: {
Ast_Call *src = (Ast_Call *)ast; Ast_Call *src = (Ast_Call *)ast;
Ast_Call *dst = ast_create_copy(parent_scope, 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); dst->exprs.init(pctx->perm, src->exprs.len);
For(dst->exprs) { 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); dst->exprs.add(copy);
} }
return dst; return dst;
@@ -117,19 +113,19 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
case AST_CONSTANT_ASSERT: { case AST_CONSTANT_ASSERT: {
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); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
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 = ast_create_copy(parent_scope, 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->value = (Ast_Expr *)ast_copy(src->value, parent_scope, replace, with);
dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope); dst->default_scope = (Ast_Scope *)ast_copy(src->default_scope, parent_scope, replace, with);
dst->cases.init(pctx->perm, src->cases.len); dst->cases.init(pctx->perm, src->cases.len);
For(src->cases) { 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); assert(copy->kind == AST_SWITCH_CASE);
dst->cases.add(copy); 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 *src = (Ast_Switch_Case *)ast;
Ast_Switch_Case *dst = ast_create_copy(parent_scope, 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); dst->labels.init(pctx->perm, src->labels.len);
For(src->labels) { 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); dst->labels.add(copy);
} }
return dst; 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 *src = (Ast_Var_Unpack *)ast;
Ast_Var_Unpack *dst = ast_create_copy(parent_scope, 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); dst->vars.init(pctx->perm, src->vars.len);
For(src->vars) { 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); dst->vars.add(copy);
} }
return dst; 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); // dst->overload_op_info = ast_create_copy(parent_scope, Ast_Operator_Info, src->overload_op_info);
// omitting polymorphs // omitting polymorphs
// omitting polymorph parameters // omitting polymorph parameters
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope); dst->typespec = (Ast_Expr *)ast_copy(src->typespec, parent_scope, replace, with);
dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
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 = 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); dst->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
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 = ast_create_copy(parent_scope, 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->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope); dst->init = (Ast_Expr *)ast_copy(src->init, parent_scope, replace, with);
dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope); dst->cond = (Ast_Expr *)ast_copy(src->cond, parent_scope, replace, with);
dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope); dst->iter = (Ast_Expr *)ast_copy(src->iter, parent_scope, replace, with);
return dst; return dst;
} break; } 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); Ast_If *dst = ast_create_copy(parent_scope, Ast_If, ast);
dst->ifs.init(pctx->perm, src->ifs.len); dst->ifs.init(pctx->perm, src->ifs.len);
For(src->ifs) { 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); assert(copy->kind == AST_IF_NODE);
dst->ifs.add(copy); dst->ifs.add(copy);
} }
@@ -220,9 +216,9 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
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 = ast_create_copy(parent_scope, 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->expr = (Ast_Expr *)ast_copy(src->expr, parent_scope, replace, with);
dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope); dst->init = (Ast_Binary *)ast_copy(src->init, parent_scope, replace, with);
dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope); dst->scope = (Ast_Scope *)ast_copy(src->scope, parent_scope, replace, with);
return dst; return dst;
} break; } break;
@@ -232,7 +228,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
dst->expr.init(pctx->perm, src->expr.len); dst->expr.init(pctx->perm, src->expr.len);
For(src->expr) { 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); dst->expr.add(copy);
} }
return dst; return dst;
@@ -244,16 +240,16 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
dst->args.init(pctx->perm, src->args.len); dst->args.init(pctx->perm, src->args.len);
For(src->args) { 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->args.add(copy);
} }
dst->ret.init(pctx->perm, src->ret.len); dst->ret.init(pctx->perm, src->ret.len);
For(src->ret) { 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->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; return dst;
} break; } break;
@@ -262,7 +258,46 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope) {
invalid_return; 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 // ITERATOR
//
const unsigned AST_NODE_END = 128; const unsigned AST_NODE_END = 128;