Polymorphism: Add already resolved decls for identifiers
This commit is contained in:
@@ -26,10 +26,7 @@ Array :: struct($T: Type)
|
|||||||
Tuple :: struct($A: Type, $B: Type)
|
Tuple :: struct($A: Type, $B: Type)
|
||||||
a: A
|
a: A
|
||||||
b: B
|
b: B
|
||||||
Triple :: struct($A: Type, $B: Type, $C: Type)
|
|
||||||
a: A
|
|
||||||
b: B
|
|
||||||
c: C
|
|
||||||
Variant :: union($A: Type, $B: Type, $C: Type)
|
Variant :: union($A: Type, $B: Type, $C: Type)
|
||||||
a: A
|
a: A
|
||||||
b: B
|
b: B
|
||||||
@@ -63,12 +60,24 @@ GetCount :: (a: int): int
|
|||||||
// @todo: this is allowed, shouldn't be
|
// @todo: this is allowed, shouldn't be
|
||||||
// Test :: (a: int, b: int = 10, c: int???)
|
// 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
|
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
|
main :: (argc: int, argv: **char): int
|
||||||
buff: *int
|
buff: *int
|
||||||
array: Array(int) = {len = 10, cap = 10, data = buff}
|
array: Array(S64)
|
||||||
second_array: Array(int)
|
second_array: Array(int)
|
||||||
third_array: Array(int)
|
third_array: Array(int)
|
||||||
fourth: Array(F32)
|
fourth: Array(F32)
|
||||||
@@ -76,6 +85,7 @@ main :: (argc: int, argv: **char): int
|
|||||||
sixth: Array(Array(F32))
|
sixth: Array(Array(F32))
|
||||||
seventh: Variant(int, F32, S64)
|
seventh: Variant(int, F32, S64)
|
||||||
|
|
||||||
|
Test({1,2,3})
|
||||||
test_a := int
|
test_a := int
|
||||||
test := *int
|
test := *int
|
||||||
Assert(test_a != test)
|
Assert(test_a != test)
|
||||||
@@ -85,12 +95,7 @@ main :: (argc: int, argv: **char): int
|
|||||||
|
|
||||||
a := MultipleArgs()
|
a := MultipleArgs()
|
||||||
|
|
||||||
Test(10)
|
Add(&array, 32)
|
||||||
Test(10, 20)
|
|
||||||
Test(10, 20, 30)
|
|
||||||
Test(10, b = 10)
|
|
||||||
Test(10, b = 10, c = 20)
|
|
||||||
Test(a = 10, b = 10, c = 20)
|
|
||||||
|
|
||||||
// value := PolyLambda(**int)
|
// value := PolyLambda(**int)
|
||||||
// PolyType_r1 := PolyType(10)
|
// PolyType_r1 := PolyType(10)
|
||||||
|
|||||||
@@ -15,3 +15,8 @@ fread :: #foreign (buffer: *void, element_size: size_t, element_count: size_t, s
|
|||||||
SEEK_CUR :: 1
|
SEEK_CUR :: 1
|
||||||
SEEK_END :: 2
|
SEEK_END :: 2
|
||||||
SEEK_SET :: 0
|
SEEK_SET :: 0
|
||||||
|
|
||||||
|
Triple :: struct($A: Type, $B: Type, $C: Type)
|
||||||
|
a: A
|
||||||
|
b: B
|
||||||
|
c: C
|
||||||
@@ -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));
|
Ast_Array *arr = ast_array(pos, create_typespec_from_type(pos, parent_scope, type->arr.base));
|
||||||
result = arr;
|
result = arr;
|
||||||
} break;
|
} break;
|
||||||
case TYPE_LAMBDA: {
|
|
||||||
invalid_codepath;
|
|
||||||
} break;
|
|
||||||
case TYPE_STRUCT:
|
case TYPE_STRUCT:
|
||||||
case TYPE_UNION:
|
case TYPE_UNION:
|
||||||
case TYPE_ENUM: {
|
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;
|
Ast_Decl *decl = (Ast_Decl *)type->ast;
|
||||||
|
Ast_Atom *atom = ast_ident(pos, decl->name);
|
||||||
// @warning:
|
atom->resolved_decl = decl;
|
||||||
// Can we do this differently?
|
result = atom;
|
||||||
// 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;
|
} 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;
|
result->parent_scope = parent_scope;
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
@@ -1154,7 +1154,11 @@ resolve_expr(Ast_Expr *ast, Resolve_Flag flags, Ast_Type *compound_and_const_str
|
|||||||
int a = 10;
|
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) {
|
// if (decl->type_val && decl->type_val->kind == TYPE_POLYMORPH) {
|
||||||
// compiler_error(ast->pos, "Trying to use a polymorphic in an invalid way");
|
// compiler_error(ast->pos, "Trying to use a polymorphic in an invalid way");
|
||||||
// }
|
// }
|
||||||
|
|||||||
Reference in New Issue
Block a user