diff --git a/build/examples/_polymorphism.core b/build/examples/_polymorphism.core index ced9239..edcd93d 100644 --- a/build/examples/_polymorphism.core +++ b/build/examples/_polymorphism.core @@ -26,10 +26,7 @@ Array :: struct($T: Type) Tuple :: struct($A: Type, $B: Type) a: A b: B -Triple :: struct($A: Type, $B: Type, $C: Type) - a: A - b: B - c: C + Variant :: union($A: Type, $B: Type, $C: Type) a: A b: B @@ -63,12 +60,24 @@ GetCount :: (a: int): int // @todo: this is allowed, shouldn't be // Test :: (a: int, b: int = 10, c: int???) -Test :: (a: int, b: int = 10, c: int = 20) +Test :: (a: C.Triple(int, int, int)) pass +// @todo: +// Add :: (arr: *Array($T), item: T) +// return + +C :: #import "LibC.core" + +Add :: (arr: *Array(T), val: $T) + if arr.cap == 0 + arr.cap = 16 + arr.data = C.malloc(SizeOf(T)->U64 * arr.cap->U64) + arr.data[arr.len++] = val + main :: (argc: int, argv: **char): int buff: *int - array: Array(int) = {len = 10, cap = 10, data = buff} + array: Array(S64) second_array: Array(int) third_array: Array(int) fourth: Array(F32) @@ -76,6 +85,7 @@ main :: (argc: int, argv: **char): int sixth: Array(Array(F32)) seventh: Variant(int, F32, S64) + Test({1,2,3}) test_a := int test := *int Assert(test_a != test) @@ -85,12 +95,7 @@ main :: (argc: int, argv: **char): int a := MultipleArgs() - Test(10) - Test(10, 20) - Test(10, 20, 30) - Test(10, b = 10) - Test(10, b = 10, c = 20) - Test(a = 10, b = 10, c = 20) + Add(&array, 32) // value := PolyLambda(**int) // PolyType_r1 := PolyType(10) diff --git a/build/modules/LibC.core b/build/modules/LibC.core index f08a1f6..ace9eeb 100644 --- a/build/modules/LibC.core +++ b/build/modules/LibC.core @@ -15,3 +15,8 @@ fread :: #foreign (buffer: *void, element_size: size_t, element_count: size_t, s SEEK_CUR :: 1 SEEK_END :: 2 SEEK_SET :: 0 + +Triple :: struct($A: Type, $B: Type, $C: Type) + a: A + b: B + c: C \ No newline at end of file diff --git a/core_polymorph.cpp b/core_polymorph.cpp index 1703db1..bbdf4e8 100644 --- a/core_polymorph.cpp +++ b/core_polymorph.cpp @@ -63,24 +63,27 @@ Ast_Expr *create_typespec_from_type(Token *pos, Ast_Scope *parent_scope, Ast_Typ Ast_Array *arr = ast_array(pos, create_typespec_from_type(pos, parent_scope, type->arr.base)); result = arr; } break; - case TYPE_LAMBDA: { - invalid_codepath; - } break; case TYPE_STRUCT: case TYPE_UNION: case TYPE_ENUM: { + // We fill out the resolved_decl here because the typespecs for + // polymorphic types can be really complex and very context dependent. + // For example for a namespaced polymorph we would need to figure out the + // namespace, add binary expression etc. Ast_Decl *decl = (Ast_Decl *)type->ast; - - // @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); + Ast_Atom *atom = ast_ident(pos, decl->name); + atom->resolved_decl = decl; + result = atom; } break; + case TYPE_LAMBDA: { + // @warning: Not sure if this is correct, need to test! + Ast_Decl *decl = (Ast_Decl *)type->ast; + Ast_Atom *atom = ast_ident(pos, decl->name); + atom->resolved_decl = decl; + result = atom; + } break; + + invalid_default_case; } result->parent_scope = parent_scope; return result; diff --git a/core_typechecking.cpp b/core_typechecking.cpp index 4d35d91..2ad8170 100644 --- a/core_typechecking.cpp +++ b/core_typechecking.cpp @@ -1154,7 +1154,11 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str int a = 10; } - Ast_Decl *decl = resolve_name(scope, node->pos, node->intern_val, flag | RESOLVE_NAME_MAKE_SURE_OPERATOR_OVERLOAD_IS_NOT_EVER_CALLED); + // When copying polymorphs we fill out resolved_decl in + // identifiers, so it can happen that we have already resolved the name + Ast_Decl *decl = node->resolved_decl; + if (!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"); // }