We are generating basic polymorphic types!

This commit is contained in:
Krzosa Karol
2023-04-01 10:30:07 +02:00
parent db3790016d
commit b14979d754
4 changed files with 19 additions and 4 deletions

View File

@@ -1042,6 +1042,7 @@ compile_to_c_code() {
genln("Type_Info *type_infos = (Type_Info[]){"); genln("Type_Info *type_infos = (Type_Info[]){");
global_indent++; global_indent++;
For_Named(pctx->all_types, t) { For_Named(pctx->all_types, t) {
if (t->kind == TYPE_POLYMORPH) continue;
genln("{/*%Q*/.kind = %d, .size = %d, .align = %d, .is_unsigned = %s, .type = %d, ", typestring(t), genln("{/*%Q*/.kind = %d, .size = %d, .align = %d, .is_unsigned = %s, .type = %d, ", typestring(t),
(S32)t->kind, (S32)t->size, (S32)t->align, t->is_unsigned ? "true" : "false", t->type_id); (S32)t->kind, (S32)t->size, (S32)t->align, t->is_unsigned ? "true" : "false", t->type_id);
switch (t->kind) { switch (t->kind) {

View File

@@ -41,8 +41,13 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array<Ast_Decl *> *replace, Arr
if ((dst->flags & AST_TYPESPEC)) { if ((dst->flags & AST_TYPESPEC)) {
For(*replace) { For(*replace) {
assert(it->type == pctx->type_type);
if (it->name == dst->intern_val) { if (it->name == dst->intern_val) {
Breakpoint; int it_index = replace->get_index(&it);
Ast_Call_Item *replacement = with[0][it_index];
Ast_Atom *replacement_v = (Ast_Atom *)replacement->item;
assert(replacement_v->resolved_type == pctx->type_type);
dst = ast_create_copy(parent_scope, Ast_Atom, replacement_v);
} }
} }
} }
@@ -289,6 +294,9 @@ 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);
result->type_val = type_incomplete(result);
result->polymorph_hash = hash;
poly->polymorphs.add(result); poly->polymorphs.add(result);
} }

View File

@@ -1577,8 +1577,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
// node->exprs = params // node->exprs = params
Ast_Decl *instance = get_or_instantiate_polymorph_type(node->pos, poly, node->exprs, field_access_scope); Ast_Decl *instance = get_or_instantiate_polymorph_type(node->pos, poly, node->exprs, field_access_scope);
// type_complete(decl); resolve_decl(instance);
return {}; // operand_type(resolved_type); type_complete(instance->type_val);
return operand_type(instance->type_val);
} }
/* @todo: /* @todo:
@@ -1843,6 +1844,9 @@ resolve_decl(Ast_Decl *ast) {
} }
ast->state = DECL_RESOLVED; ast->state = DECL_RESOLVED;
if (is_flag_set(ast->flags, AST_GLOBAL)) bool is_polymorph = is_flag_set(ast->flags, AST_POLYMORPH);
bool is_global = is_flag_set(ast->flags, AST_GLOBAL);
if (is_global && !is_polymorph) {
add(pctx->perm, &pctx->ordered_decls, ast); add(pctx->perm, &pctx->ordered_decls, ast);
}
} }

View File

@@ -26,5 +26,7 @@ Array :: struct($T: Type)
main :: (argc: int, argv: **char): int main :: (argc: int, argv: **char): int
array: Array(int) array: Array(int)
second_array: Array(int)
third_array: Array(int)
return 0 return 0