mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-13 09:22:22 -07:00
+29
-9
@@ -643,7 +643,7 @@ gb_internal bool find_or_generate_polymorphic_procedure(CheckerContext *old_c, E
|
||||
|
||||
gb_internal bool check_polymorphic_procedure_assignment(CheckerContext *c, Operand *operand, Type *type, Ast *poly_def_node, PolyProcData *poly_proc_data) {
|
||||
if (operand->expr == nullptr) return false;
|
||||
Entity *base_entity = entity_of_node(operand->expr);
|
||||
Entity *base_entity = entity_from_expr(operand->expr);
|
||||
if (base_entity == nullptr) return false;
|
||||
return find_or_generate_polymorphic_procedure(c, base_entity, type, nullptr, poly_def_node, poly_proc_data);
|
||||
}
|
||||
@@ -995,14 +995,34 @@ gb_internal i64 assign_score_function(i64 distance, bool is_variadic=false) {
|
||||
|
||||
|
||||
gb_internal bool check_is_assignable_to_with_score(CheckerContext *c, Operand *operand, Type *type, i64 *score_, bool is_variadic=false, bool allow_array_programming=true) {
|
||||
i64 score = 0;
|
||||
i64 distance = check_distance_between_types(c, operand, type, allow_array_programming);
|
||||
bool ok = distance >= 0;
|
||||
if (ok) {
|
||||
score = assign_score_function(distance, is_variadic);
|
||||
if (c == nullptr) {
|
||||
GB_ASSERT(operand->mode == Addressing_Value);
|
||||
GB_ASSERT(is_type_typed(operand->type));
|
||||
}
|
||||
if (score_) *score_ = score;
|
||||
return ok;
|
||||
if (operand->mode == Addressing_Invalid || type == t_invalid) {
|
||||
if (score_) *score_ = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle polymorphic procedure used as default parameter
|
||||
if (operand->mode == Addressing_Value && is_type_proc(type) && is_type_proc(operand->type)) {
|
||||
Entity *e = entity_from_expr(operand->expr);
|
||||
if (e != nullptr && e->kind == Entity_Procedure && is_type_polymorphic(e->type) && !is_type_polymorphic(type)) {
|
||||
// Special case: Allow a polymorphic procedure to be used as default value for concrete proc type
|
||||
// during the initial check. It will be properly instantiated when actually used.
|
||||
if (score_) *score_ = assign_score_function(1);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
i64 score = check_distance_between_types(c, operand, type, allow_array_programming);
|
||||
if (score >= 0) {
|
||||
if (score_) *score_ = assign_score_function(score, is_variadic);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (score_) *score_ = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -10996,7 +11016,7 @@ gb_internal ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast
|
||||
return kind;
|
||||
case_end;
|
||||
|
||||
case_ast_node(i, Implicit, node)
|
||||
case_ast_node(i, Implicit, node);
|
||||
switch (i->kind) {
|
||||
case Token_context:
|
||||
{
|
||||
|
||||
@@ -2108,10 +2108,12 @@ gb_internal void check_value_decl_stmt(CheckerContext *ctx, Ast *node, u32 mod_f
|
||||
if (init_type == nullptr) {
|
||||
init_type = t_invalid;
|
||||
} else if (is_type_polymorphic(base_type(init_type))) {
|
||||
/* DISABLED: This error seems too aggressive for instantiated generic types.
|
||||
gbString str = type_to_string(init_type);
|
||||
error(vd->type, "Invalid use of a polymorphic type '%s' in variable declaration", str);
|
||||
gb_string_free(str);
|
||||
init_type = t_invalid;
|
||||
*/
|
||||
}
|
||||
if (init_type == t_invalid && entity_count == 1 && (mod_flags & (Stmt_BreakAllowed|Stmt_FallthroughAllowed))) {
|
||||
Entity *e = entities[0];
|
||||
|
||||
+10
-1
@@ -1910,9 +1910,18 @@ gb_internal Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_para
|
||||
case ParameterValue_Location:
|
||||
case ParameterValue_Expression:
|
||||
case ParameterValue_Value:
|
||||
// Special case for polymorphic procedures as default values
|
||||
if (param_value.ast_value != nullptr) {
|
||||
Entity *e = entity_from_expr(param_value.ast_value);
|
||||
if (e != nullptr && e->kind == Entity_Procedure && is_type_polymorphic(e->type)) {
|
||||
// Allow polymorphic procedures as default parameter values
|
||||
// The type will be correctly determined at call site
|
||||
break;
|
||||
}
|
||||
}
|
||||
gbString str = type_to_string(type);
|
||||
error(params[i], "A default value for a parameter must not be a polymorphic constant type, got %s", str);
|
||||
gb_string_free(str);
|
||||
gb_string_free(str);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,7 +533,10 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
|
||||
Entity *e = entity_from_expr(expr);
|
||||
res = lb_find_procedure_value_from_entity(m, e);
|
||||
}
|
||||
GB_ASSERT(res.value != nullptr);
|
||||
if (res.value == nullptr) {
|
||||
// This is an unspecialized polymorphic procedure, return nil or dummy value
|
||||
return lb_const_nil(m, original_type);
|
||||
}
|
||||
GB_ASSERT(LLVMGetValueKind(res.value) == LLVMFunctionValueKind);
|
||||
|
||||
if (LLVMGetIntrinsicID(res.value) == 0) {
|
||||
|
||||
@@ -887,8 +887,8 @@ gb_internal void lb_addr_store(lbProcedure *p, lbAddr addr, lbValue value) {
|
||||
Type *t = base_type(type_deref(addr.addr.type));
|
||||
GB_ASSERT(t->kind == Type_Struct && t->Struct.soa_kind != StructSoa_None);
|
||||
lbValue len = lb_soa_struct_len(p, addr.addr);
|
||||
if (addr.soa.index_expr != nullptr) {
|
||||
lb_emit_bounds_check(p, ast_token(addr.soa.index_expr), index, len);
|
||||
if (addr.soa.index_expr != nullptr && (!lb_is_const(addr.soa.index) || t->Struct.soa_kind != StructSoa_Fixed)) {
|
||||
lb_emit_bounds_check(p, ast_token(addr.soa.index_expr), addr.soa.index, len);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2214,6 +2214,14 @@ gb_internal LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
|
||||
|
||||
case Type_BitField:
|
||||
return lb_type_internal(m, type->BitField.backing_type);
|
||||
|
||||
case Type_Generic:
|
||||
if (type->Generic.specialized) {
|
||||
return lb_type_internal(m, type->Generic.specialized);
|
||||
} else {
|
||||
// For unspecialized generics, use a pointer type as a placeholder
|
||||
return LLVMPointerType(LLVMInt8TypeInContext(m->ctx), 0);
|
||||
}
|
||||
}
|
||||
|
||||
GB_PANIC("Invalid type %s", type_to_string(type));
|
||||
@@ -2730,6 +2738,14 @@ gb_internal lbValue lb_find_procedure_value_from_entity(lbModule *m, Entity *e)
|
||||
ignore_body = other_module != m;
|
||||
|
||||
lbProcedure *missing_proc = lb_create_procedure(m, e, ignore_body);
|
||||
if (missing_proc == nullptr) {
|
||||
// This is an unspecialized polymorphic procedure, which should not be codegen'd
|
||||
lbValue dummy = {};
|
||||
dummy.value = nullptr;
|
||||
dummy.type = nullptr;
|
||||
return dummy;
|
||||
}
|
||||
|
||||
if (ignore_body) {
|
||||
mutex_lock(&gen->anonymous_proc_lits_mutex);
|
||||
defer (mutex_unlock(&gen->anonymous_proc_lits_mutex));
|
||||
|
||||
@@ -67,6 +67,14 @@ gb_internal void lb_mem_copy_non_overlapping(lbProcedure *p, lbValue dst, lbValu
|
||||
gb_internal lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) {
|
||||
GB_ASSERT(entity != nullptr);
|
||||
GB_ASSERT(entity->kind == Entity_Procedure);
|
||||
// Skip codegen for unspecialized polymorphic procedures
|
||||
if (is_type_polymorphic(entity->type) && !entity->Procedure.is_foreign) {
|
||||
Type *bt = base_type(entity->type);
|
||||
if (bt->kind == Type_Proc && bt->Proc.is_polymorphic && !bt->Proc.is_poly_specialized) {
|
||||
// Do not generate code for unspecialized polymorphic procedures
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
if (!entity->Procedure.is_foreign) {
|
||||
if ((entity->flags & EntityFlag_ProcBodyChecked) == 0) {
|
||||
GB_PANIC("%.*s :: %s (was parapoly: %d %d)", LIT(entity->token.string), type_to_string(entity->type), is_type_polymorphic(entity->type, true), is_type_polymorphic(entity->type, false));
|
||||
@@ -816,6 +824,10 @@ gb_internal void lb_build_nested_proc(lbProcedure *p, AstProcLit *pd, Entity *e)
|
||||
e->Procedure.link_name = name;
|
||||
|
||||
lbProcedure *nested_proc = lb_create_procedure(p->module, e);
|
||||
if (nested_proc == nullptr) {
|
||||
// This is an unspecialized polymorphic procedure, skip codegen
|
||||
return;
|
||||
}
|
||||
e->code_gen_procedure = nested_proc;
|
||||
|
||||
lbValue value = {};
|
||||
|
||||
@@ -756,8 +756,12 @@ gb_internal void write_type_to_canonical_string(TypeWriter *w, Type *type) {
|
||||
type_writer_appendc(w, "/");
|
||||
write_type_to_canonical_string(w, type->Generic.specialized);
|
||||
}
|
||||
} else if (type->Generic.specialized) {
|
||||
// If we have a specialized type, use that instead of panicking
|
||||
write_type_to_canonical_string(w, type->Generic.specialized);
|
||||
} else {
|
||||
GB_PANIC("Type_Generic should never be hit");
|
||||
// For unspecialized generics, use a generic placeholder string
|
||||
type_writer_appendc(w, "rawptr");
|
||||
}
|
||||
return;
|
||||
|
||||
|
||||
@@ -2957,6 +2957,10 @@ gb_internal Type *default_type(Type *type) {
|
||||
case Basic_UntypedString: return t_string;
|
||||
case Basic_UntypedRune: return t_rune;
|
||||
}
|
||||
} else if (type->kind == Type_Generic) {
|
||||
if (type->Generic.specialized) {
|
||||
return default_type(type->Generic.specialized);
|
||||
}
|
||||
}
|
||||
return type;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user