Fix constant union{proc()}

This commit is contained in:
gingerBill
2025-09-28 22:02:25 +01:00
parent 8be18d9a40
commit 1df9f1d01d
2 changed files with 42 additions and 24 deletions

View File

@@ -2340,6 +2340,20 @@ gb_internal bool check_representable_as_constant(CheckerContext *c, ExactValue i
if (in_value.kind == ExactValue_Integer) {
return true;
}
} else if (is_type_typeid(type)) {
if (in_value.kind == ExactValue_Compound) {
ast_node(cl, CompoundLit, in_value.value_compound);
if (cl->elems.count == 0) {
in_value = exact_value_typeid(nullptr);
} else {
return false;
}
}
if (in_value.kind == ExactValue_Typeid) {
if (out_value) *out_value = in_value;
return true;
}
}
return false;
@@ -3510,6 +3524,7 @@ gb_internal bool check_is_castable_to(CheckerContext *c, Operand *operand, Type
gb_internal bool check_cast_internal(CheckerContext *c, Operand *x, Type *type) {
bool is_const_expr = x->mode == Addressing_Constant;
Type *bt = base_type(type);
if (is_const_expr && is_type_constant_type(bt)) {
if (core_type(bt)->kind == Type_Basic) {

View File

@@ -540,7 +540,7 @@ gb_internal bool lb_is_nested_possibly_constant(Type *ft, Selection const &sel,
}
if (is_type_raw_union(ft) || is_type_typeid(ft)) {
if (is_type_raw_union(ft)) {
return false;
}
return lb_is_elem_const(elem, ft);
@@ -819,28 +819,6 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
return lb_const_nil(m, original_type);
}
if (value.kind == ExactValue_Procedure) {
lbValue res = {};
Ast *expr = unparen_expr(value.value_procedure);
GB_ASSERT(expr != nullptr);
if (expr->kind == Ast_ProcLit) {
res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
} else {
Entity *e = entity_from_expr(expr);
res = lb_find_procedure_value_from_entity(m, e);
}
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) {
// NOTE(bill): do not cast intrinsics as they are not really procedures that can be casted
res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
}
return res;
}
bool is_local = cc.allow_local && m->curr_procedure != nullptr;
@@ -922,7 +900,29 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
res.value = LLVMConstStructInContext(m->ctx, values, value_count, true);
return res;
}
}
if (value.kind == ExactValue_Procedure) {
lbValue res = {};
Ast *expr = unparen_expr(value.value_procedure);
GB_ASSERT(expr != nullptr);
if (expr->kind == Ast_ProcLit) {
res = lb_generate_anonymous_proc_lit(m, str_lit("_proclit"), expr);
} else {
Entity *e = entity_from_expr(expr);
res = lb_find_procedure_value_from_entity(m, e);
}
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) {
// NOTE(bill): do not cast intrinsics as they are not really procedures that can be casted
res.value = LLVMConstPointerCast(res.value, lb_type(m, res.type));
}
return res;
}
// GB_ASSERT_MSG(is_type_typed(type), "%s", type_to_string(type));
@@ -1720,7 +1720,10 @@ gb_internal lbValue lb_const_value(lbModule *m, Type *type, ExactValue value, lb
i32 index = field_remapping[f->Variable.field_index];
if (elem_type_can_be_constant(f->type)) {
if (sel.index.count == 1) {
values[index] = lb_const_value(m, f->type, tav.value, cc, tav.type).value;
lbValue value = lb_const_value(m, f->type, tav.value, cc, tav.type);
LLVMTypeRef value_type = LLVMTypeOf(value.value);
GB_ASSERT_MSG(lb_sizeof(value_type) == type_size_of(f->type), "%s vs %s", LLVMPrintTypeToString(value_type), type_to_string(f->type));
values[index] = value.value;
visited[index] = true;
} else {
if (!visited[index]) {