From 41d2baa56b1b525189821a6fd85eff89e15ca78c Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Sun, 2 Apr 2023 15:07:47 +0200 Subject: [PATCH] We are working! POLYMORPHS There was an error where we have to reconstruct exact typespec from type. This means we will probably have problems with namespaces!! --- build/examples/_polymorphism.core | 13 +++++++------ core_compiler_interface.hpp | 2 ++ core_polymorph.cpp | 18 +++++++++++++++--- core_typechecking.cpp | 3 +++ core_types.cpp | 4 +++- 5 files changed, 30 insertions(+), 10 deletions(-) diff --git a/build/examples/_polymorphism.core b/build/examples/_polymorphism.core index 52bb129..ced9239 100644 --- a/build/examples/_polymorphism.core +++ b/build/examples/_polymorphism.core @@ -52,7 +52,7 @@ MultipleArgs :: (): Tuple(int, F32) return {32, 32} PolyLambda :: ($T: Type = *int): T - return 32 + return 0 PolyType :: (a: $T): T return a @@ -92,11 +92,12 @@ main :: (argc: int, argv: **char): int Test(10, b = 10, c = 20) Test(a = 10, b = 10, c = 20) - value := PolyLambda(**int) - PolyType_r1 := PolyType(10) - PolyType_r2 := PolyType(int) - // PolyType_r3 := PolyType(array) - // PolyType_r4 := PolyType(seventh) + // value := PolyLambda(**int) + // PolyType_r1 := PolyType(10) + // PolyType_r2 := PolyType(int) + PolyType_r5 := PolyType(seventh) + // PolyType_r3 := PolyType(test) + // PolyType_r4 := PolyType(test_a) // PolyType_r5 := PolyType(sixth) return 0 \ No newline at end of file diff --git a/core_compiler_interface.hpp b/core_compiler_interface.hpp index 97fb707..004c2ba 100644 --- a/core_compiler_interface.hpp +++ b/core_compiler_interface.hpp @@ -363,6 +363,7 @@ enum { AST_TYPE_POLYMORPH = 1 << 16, AST_POLYMORPH = AST_IDENT_POLYMORPH | AST_TYPE_POLYMORPH, AST_PARENT_POLYMORPH = 1 << 17, + AST_POLYMORPH_INSTANCE = 1 << 18, AST_TYPESPEC = 1 << 18, }; @@ -614,6 +615,7 @@ struct Ast_Decl : Ast { Ast_Operator_Info *overload_op_info; uint64_t polymorph_hash; + Array instantiation_call_items; Array polymorph_parameters; Array polymorphs; // instantiated polymorphs diff --git a/core_polymorph.cpp b/core_polymorph.cpp index 75e8262..1703db1 100644 --- a/core_polymorph.cpp +++ b/core_polymorph.cpp @@ -70,7 +70,16 @@ Ast_Expr *create_typespec_from_type(Token *pos, Ast_Scope *parent_scope, Ast_Typ case TYPE_UNION: case TYPE_ENUM: { Ast_Decl *decl = (Ast_Decl *)type->ast; - result = ast_ident(pos, decl->name); + + // @warning: + // Can we do this differently? + // WE MIGHT HAVE PROBLEM WITH NAMESPACES in the future!!! + if (decl->flags & AST_POLYMORPH_INSTANCE) { + Ast_Atom *ident = ast_ident(pos, decl->name); + ident->parent_scope = parent_scope; + result = ast_call(pos, ident, decl->instantiation_call_items); + } + else result = ast_ident(pos, decl->name); } break; } result->parent_scope = parent_scope; @@ -362,6 +371,7 @@ Ast *ast_copy(Ast *ast, Ast_Scope *parent_scope, Array *replace, Arr assert(result); unset_flag(result->flags, AST_POLYMORPH | AST_PARENT_POLYMORPH); + set_flag(result->flags, AST_POLYMORPH_INSTANCE); return result; } @@ -399,8 +409,9 @@ Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Arrayparent_scope, &poly->polymorph_parameters, ¶ms); - if (poly->kind == AST_STRUCT || poly->kind == AST_UNION) result->type_val = type_incomplete(result); + result->type_val = type_incomplete(result); result->polymorph_hash = hash; + result->instantiation_call_items = params.tight_copy(pctx->perm); assert(result->di != poly->di); result->unique_name = get_unique_name_for_decl(result); @@ -410,7 +421,7 @@ Ast_Decl *get_or_instantiate_polymorph_type(Token *pos, Ast_Decl *poly, Arraykind == AST_STRUCT || poly->kind == AST_UNION) type_complete(result->type_val); + type_complete(result->type_val); return result; } @@ -436,6 +447,7 @@ Ast_Decl *get_or_instantiate_polymorph_lambda(Token *pos, Ast_Decl *poly, Array< Operand op = resolve_expr(it->item, AST_CANT_BE_NULL, 0, field_access_scope); try_converting_untyped_to_default_type(&op); try_propagating_resolved_type_to_untyped_literals(it->item, op.type); + // type_complete(op.type); assert(it->item->resolved_type == op.type); hash = hash_mix(hash, hash_ptr(op.type)); diff --git a/core_typechecking.cpp b/core_typechecking.cpp index bac7a2d..4d35d91 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1155,6 +1155,9 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str } Ast_Decl *decl = resolve_name(scope, node->pos, node->intern_val, flag | RESOLVE_NAME_MAKE_SURE_OPERATOR_OVERLOAD_IS_NOT_EVER_CALLED); + // if (decl->type_val && decl->type_val->kind == TYPE_POLYMORPH) { + // compiler_error(ast->pos, "Trying to use a polymorphic in an invalid way"); + // } // Substitute lambda alias if (decl->kind == AST_CONST && decl->resolved_decl && decl->resolved_decl->kind == AST_LAMBDA) { diff --git a/core_types.cpp b/core_types.cpp index 0b5bab9..e160fc9 100644 --- a/core_types.cpp +++ b/core_types.cpp @@ -19,6 +19,8 @@ get_name_of_type(Ast_Type *type) { case TYPE_TUPLE: return "Tuple"; case TYPE_TYPE: return "Type"; + case TYPE_POLYMORPH: + return ""; invalid_default_case; } return ""; @@ -219,8 +221,8 @@ type_enum(Ast_Decl *ast, Ast_Type *type) { CORE_Static Ast_Type * type_incomplete(Ast *ast) { - if (is_flag_set(ast->flags, AST_POLYMORPH)) return 0; Ast_Type_Kind kind = TYPE_INCOMPLETE; + if (is_flag_set(ast->flags, AST_POLYMORPH)) kind = TYPE_POLYMORPH; Ast_Type *result = type_new(pctx->perm, kind, 0, 0); result->ast = ast; return result;