Polymorphism: Add already resolved decls for identifiers

This commit is contained in:
Krzosa Karol
2023-04-03 11:55:14 +02:00
parent 41d2baa56b
commit b543e1df9d
4 changed files with 43 additions and 26 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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;

View File

@@ -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");
// }