From 5af13f5d53b4e5f5d472cd8a8bc4444f05ea36d6 Mon Sep 17 00:00:00 2001 From: Harold Brenes Date: Tue, 16 Sep 2025 00:49:31 -0400 Subject: Automatically emit objc_msgSend calls when calling imported or implemented Objective-C methods - Add intrinsics.objc_super() - Emit objc_msgSendSuper2 calls when an objc method call is combined with objc_super(self) - Fix objc_block return value ABI for large struct returns - Fix objc_implement method wrappers bad ABI for large struct returns and indirect args - Simplify parameter forwarding for objc_imlpement methods - Add intrinsics.objc_instancetype to mimi Objective-C instancetype* returns This facilitates returning the correct type on subclasses when calling mehtods such as `alloc`, `init`, `retain`, etc. - Refactor Objective-C class implementations generation so that hierarchies are properly initialized - Better codegen for context passing with ivar-based autocontext - Allow @superclass on imported objc-c objects - Better codegen for block forwarding invoker, arguments are forwarded directly --- src/check_builtin.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 7e1567750..f142f04b7 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -210,7 +210,7 @@ gb_internal ObjcMsgKind get_objc_proc_kind(Type *return_type) { return ObjcMsg_normal; } -gb_internal void add_objc_proc_type(CheckerContext *c, Ast *call, Type *return_type, Slice param_types) { +void add_objc_proc_type(CheckerContext *c, Ast *call, Type *return_type, Slice param_types) { ObjcMsgKind kind = get_objc_proc_kind(return_type); Scope *scope = create_scope(c->info, nullptr); @@ -248,6 +248,12 @@ gb_internal void add_objc_proc_type(CheckerContext *c, Ast *call, Type *return_t try_to_add_package_dependency(c, "runtime", "objc_msgSend_fpret"); try_to_add_package_dependency(c, "runtime", "objc_msgSend_fp2ret"); try_to_add_package_dependency(c, "runtime", "objc_msgSend_stret"); + + Slice args = call->CallExpr.args; + if (args.count > 0 && args[0]->tav.objc_super_target) { + try_to_add_package_dependency(c, "runtime", "objc_msgSendSuper2"); + try_to_add_package_dependency(c, "runtime", "objc_msgSendSuper2_stret"); + } } gb_internal bool is_constant_string(CheckerContext *c, String const &builtin_name, Ast *expr, String *name_) { @@ -466,8 +472,8 @@ gb_internal bool check_builtin_objc_procedure(CheckerContext *c, Operand *operan isize capture_arg_count = ce->args.count - 1; - // NOTE(harold): The first parameter is already checked at check_builtin_procedure(). - // Checking again would invalidate the Entity -> Value map for direct parameters if it's the handler proc. + // NOTE(harold): The first argument is already checked at check_builtin_procedure(). + // Checking again would invalidate the Entity -> Value map for direct arguments if it's the handler proc. param_operands[0] = *operand; for (isize i = 0; i < ce->args.count-1; i++) { @@ -680,6 +686,52 @@ gb_internal bool check_builtin_objc_procedure(CheckerContext *c, Operand *operan operand->mode = Addressing_Value; return true; } break; + + case BuiltinProc_objc_super: + { + // Must be a pointer to an Objective-C object. + Type *objc_obj = operand->type; + if (!is_type_objc_ptr_to_object(objc_obj)) { + gbString e = expr_to_string(operand->expr); + gbString t = type_to_string(objc_obj); + error(operand->expr, "'%.*s' expected a pointer to an Objective-C object, but got '%s' of type %s", LIT(builtin_name), e, t); + gb_string_free(t); + gb_string_free(e); + return false; + } + + if (operand->mode != Addressing_Value && operand->mode != Addressing_Variable) { + gbString e = expr_to_string(operand->expr); + gbString t = type_to_string(operand->type); + error(operand->expr, "'%.*s' expression '%s', of type %s, must be a value or variable.", LIT(builtin_name), e, t); + gb_string_free(t); + gb_string_free(e); + return false; + } + + Type *obj_type = type_deref(objc_obj); + GB_ASSERT(obj_type->kind == Type_Named); + + // NOTE(harold) Track original type before transforming it to the superclass. + // This is needed because objc_msgSendSuper2 must start its search on the subclass, not the superclass. + call->tav.objc_super_target = obj_type; + + // The superclass type must be known at compile time. We require this so that the selector method expressions + // methods are resolved to the superclass's methods instead of the subclass's. + Type *superclass = obj_type->Named.type_name->TypeName.objc_superclass; + if (superclass == nullptr) { + gbString t = type_to_string(obj_type); + error(operand->expr, "'%.*s' target object '%.*s' does not have an Objective-C superclass. One must be set via the @(objc_superclass) attribute", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + GB_ASSERT(superclass->Named.type_name->TypeName.objc_class_name.len > 0); + + operand->type = alloc_type_pointer(superclass); + return true; + + } break; } } @@ -2515,6 +2567,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_objc_register_class: case BuiltinProc_objc_ivar_get: case BuiltinProc_objc_block: + case BuiltinProc_objc_super: return check_builtin_objc_procedure(c, operand, call, id, type_hint); case BuiltinProc___entry_point: -- cgit v1.2.3 From e001e402c29d6ef712dc2fc7efe2135b575a9df1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 9 Oct 2025 09:55:14 +0100 Subject: Add `intrinsics.type_is_cstring` and `intrinsics.type_is_cstring16` --- src/check_builtin.cpp | 4 ++++ src/checker_builtin_procs.hpp | 2 ++ 2 files changed, 6 insertions(+) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index f142f04b7..b95b46450 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -20,6 +20,8 @@ gb_global BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_bool is_type_quaternion, is_type_string, is_type_string16, + is_type_cstring, + is_type_cstring16, is_type_typeid, is_type_any, is_type_endian_platform, @@ -6424,6 +6426,8 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_type_is_quaternion: case BuiltinProc_type_is_string: case BuiltinProc_type_is_string16: + case BuiltinProc_type_is_cstring: + case BuiltinProc_type_is_cstring16: case BuiltinProc_type_is_typeid: case BuiltinProc_type_is_any: case BuiltinProc_type_is_endian_platform: diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index c2255a6ba..80e503c70 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -251,6 +251,8 @@ BuiltinProc__type_simple_boolean_begin, BuiltinProc_type_is_quaternion, BuiltinProc_type_is_string, BuiltinProc_type_is_string16, + BuiltinProc_type_is_cstring, + BuiltinProc_type_is_cstring16, BuiltinProc_type_is_typeid, BuiltinProc_type_is_any, -- cgit v1.2.3 From 41c523faa5a66232239e1f0ea262a6721eefc3b9 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 9 Oct 2025 10:10:49 +0100 Subject: Add `intrinsics.type_is_raw_union` --- src/check_builtin.cpp | 6 ++++-- src/checker_builtin_procs.hpp | 9 +++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index b95b46450..0f8944cb9 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -57,6 +57,7 @@ gb_global BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_bool is_type_bit_field, is_type_simd_vector, is_type_matrix, + is_type_raw_union, is_type_polymorphic_record_specialized, is_type_polymorphic_record_unspecialized, @@ -6424,12 +6425,12 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_type_is_float: case BuiltinProc_type_is_complex: case BuiltinProc_type_is_quaternion: + case BuiltinProc_type_is_typeid: + case BuiltinProc_type_is_any: case BuiltinProc_type_is_string: case BuiltinProc_type_is_string16: case BuiltinProc_type_is_cstring: case BuiltinProc_type_is_cstring16: - case BuiltinProc_type_is_typeid: - case BuiltinProc_type_is_any: case BuiltinProc_type_is_endian_platform: case BuiltinProc_type_is_endian_little: case BuiltinProc_type_is_endian_big: @@ -6461,6 +6462,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_type_is_bit_field: case BuiltinProc_type_is_simd_vector: case BuiltinProc_type_is_matrix: + case BuiltinProc_type_is_raw_union: case BuiltinProc_type_is_specialized_polymorphic_record: case BuiltinProc_type_is_unspecialized_polymorphic_record: case BuiltinProc_type_has_nil: diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 80e503c70..addaeaf23 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -287,6 +287,8 @@ BuiltinProc__type_simple_boolean_begin, BuiltinProc_type_is_bit_set, BuiltinProc_type_is_simd_vector, BuiltinProc_type_is_matrix, + BuiltinProc_type_is_raw_union, + BuiltinProc_type_is_specialized_polymorphic_record, BuiltinProc_type_is_unspecialized_polymorphic_record, @@ -614,10 +616,12 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("type_is_float"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_complex"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_quaternion"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("type_is_string"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, - {STR_LIT("type_is_string16"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_typeid"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_any"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_is_string"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_is_string16"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_is_cstring"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_is_cstring16"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_endian_platform"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_endian_little"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, @@ -651,6 +655,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("type_is_bit_field"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_simd_vector"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_matrix"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("type_is_raw_union"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_specialized_polymorphic_record"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("type_is_unspecialized_polymorphic_record"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics}, -- cgit v1.2.3 From a60058259d9ae6bbff37fff49e53d542354c90c1 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 9 Oct 2025 10:25:48 +0100 Subject: Allow `intrinsics.type_union_tag_offset` with maybe-like unions --- src/check_builtin.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 0f8944cb9..5d1016bba 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -6742,9 +6742,13 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As // NOTE(jakubtomsu): forces calculation of variant_block_size type_size_of(u); - // NOTE(Jeroen): A tag offset of zero is perfectly fine if all members of the union are empty structs. - // What matters is that the tag size is > 0. - GB_ASSERT(u->Union.tag_size > 0); + if (u->Union.tag_size == 0) { + GB_ASSERT(is_type_union_maybe_pointer(u)); + } else { + // NOTE(Jeroen): A tag offset of zero is perfectly fine if all members of the union are empty structs. + // What matters is that the tag size is > 0. + GB_ASSERT(u->Union.tag_size > 0); + } operand->mode = Addressing_Constant; operand->type = t_untyped_integer; -- cgit v1.2.3 From adc44312bf78b1842f568d0bac3170898688007b Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 9 Oct 2025 10:36:50 +0100 Subject: `const string` -> `constant string` --- src/check_builtin.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 5d1016bba..a19fb15ec 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -6525,7 +6525,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As check_expr(c, &x, ce->args[1]); if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) { - error(ce->args[1], "Expected a const string for field argument"); + error(ce->args[1], "Expected a constant string for field argument"); return false; } @@ -6605,7 +6605,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As check_expr(c, &x, ce->args[1]); if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) { - error(ce->args[1], "Expected a const string for field argument"); + error(ce->args[1], "Expected a constant string for field argument"); return false; } @@ -7242,7 +7242,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As check_expr(c, &x, ce->args[1]); if (!is_type_string(x.type) || x.mode != Addressing_Constant || x.value.kind != ExactValue_String) { - error(ce->args[1], "Expected a const string for field argument"); + error(ce->args[1], "Expected a constant string for field argument"); return false; } -- cgit v1.2.3 From 1572ed57b669ed06d4ad7513bc51f70ba0952c16 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 11:40:36 +0100 Subject: Add `intrinsics.concatenate` --- src/check_builtin.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++ src/checker_builtin_procs.hpp | 4 ++ 2 files changed, 90 insertions(+) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index a19fb15ec..13a1c4e69 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4885,6 +4885,92 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As break; } + case BuiltinProc_concatenate: { + Operand lhs = {}; + Operand rhs = {}; + + check_expr(c, &lhs, ce->args[0]); + if (lhs.mode == Addressing_Invalid) { + return false; + } + check_expr(c, &rhs, ce->args[1]); + if (rhs.mode == Addressing_Invalid) { + return false; + } + if (lhs.mode != Addressing_Constant) { + error(lhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); + return false; + } + if (rhs.mode != Addressing_Constant) { + error(rhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); + return false; + } + + if (!are_types_identical(lhs.type, rhs.type)) { + gbString a = type_to_string(lhs.type); + gbString b = type_to_string(rhs.type); + error(rhs.expr, "'%*.s' expects a two constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + + if (!is_type_slice(lhs.type) && !is_type_array(lhs.type)) { + gbString a = type_to_string(lhs.type); + error(lhs.expr, "'%*.s' expects a constant array or slice, got %s", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + + if (lhs.value.kind != ExactValue_Compound) { + gbString a = exact_value_to_string(lhs.value); + error(lhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + if (rhs.value.kind != ExactValue_Compound) { + gbString a = exact_value_to_string(rhs.value); + error(rhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + + ast_node(lhs_cl, CompoundLit, lhs.value.value_compound); + ast_node(rhs_cl, CompoundLit, rhs.value.value_compound); + + for (Ast *elem : lhs_cl->elems) { + if (elem->kind == Ast_FieldValue) { + error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); + return false; + } + } + for (Ast *elem : rhs_cl->elems) { + if (elem->kind == Ast_FieldValue) { + error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); + return false; + } + } + + Ast *type_ast = lhs_cl->type; + if (type_ast == nullptr) { + type_ast = rhs_cl->type; + } + + + Array new_elems = {}; + array_init(&new_elems, heap_allocator()); + + array_add_elems(&new_elems, lhs_cl->elems.data, lhs_cl->elems.count); + array_add_elems(&new_elems, rhs_cl->elems.data, rhs_cl->elems.count); + + Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(rhs.expr)); + + operand->mode = Addressing_Constant; + operand->value = exact_value_compound(new_compound_lit); + operand->type = lhs.type; + break; + } + case BuiltinProc_alloca: { Operand sz = {}; diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index addaeaf23..8de6a4f5e 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -56,6 +56,8 @@ enum BuiltinProcId { BuiltinProc_soa_struct, + BuiltinProc_concatenate, + BuiltinProc_alloca, BuiltinProc_cpu_relax, BuiltinProc_trap, @@ -427,6 +429,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("soa_struct"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, // Type + {STR_LIT("concatenate"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("alloca"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, -- cgit v1.2.3 From 061b14e1649ea5829e2612dc905cdc9af863a071 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 11:48:18 +0100 Subject: Allow `intrinsics.concatenate` to be variadic --- src/check_builtin.cpp | 87 +++++++++++++++++++++++-------------------- src/checker_builtin_procs.hpp | 2 +- 2 files changed, 48 insertions(+), 41 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 13a1c4e69..c337802c9 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4887,56 +4887,32 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_concatenate: { Operand lhs = {}; - Operand rhs = {}; check_expr(c, &lhs, ce->args[0]); if (lhs.mode == Addressing_Invalid) { return false; } - check_expr(c, &rhs, ce->args[1]); - if (rhs.mode == Addressing_Invalid) { - return false; - } if (lhs.mode != Addressing_Constant) { - error(lhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); - return false; - } - if (rhs.mode != Addressing_Constant) { - error(rhs.expr, "'%*.s' expects a constant array or slice", LIT(builtin_name)); - return false; - } - - if (!are_types_identical(lhs.type, rhs.type)) { - gbString a = type_to_string(lhs.type); - gbString b = type_to_string(rhs.type); - error(rhs.expr, "'%*.s' expects a two constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); - gb_string_free(b); - gb_string_free(a); + error(lhs.expr, "'%.*s' expects a constant array or slice", LIT(builtin_name)); return false; } + operand->type = lhs.type; + operand->mode = Addressing_Value; if (!is_type_slice(lhs.type) && !is_type_array(lhs.type)) { gbString a = type_to_string(lhs.type); - error(lhs.expr, "'%*.s' expects a constant array or slice, got %s", LIT(builtin_name), a); + error(lhs.expr, "'%.*s' expects a constant array or slice, got %s", LIT(builtin_name), a); gb_string_free(a); return false; } - if (lhs.value.kind != ExactValue_Compound) { gbString a = exact_value_to_string(lhs.value); error(lhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); gb_string_free(a); return false; } - if (rhs.value.kind != ExactValue_Compound) { - gbString a = exact_value_to_string(rhs.value); - error(rhs.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); - gb_string_free(a); - return false; - } ast_node(lhs_cl, CompoundLit, lhs.value.value_compound); - ast_node(rhs_cl, CompoundLit, rhs.value.value_compound); for (Ast *elem : lhs_cl->elems) { if (elem->kind == Ast_FieldValue) { @@ -4944,26 +4920,57 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As return false; } } - for (Ast *elem : rhs_cl->elems) { - if (elem->kind == Ast_FieldValue) { - error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); - return false; - } - } Ast *type_ast = lhs_cl->type; - if (type_ast == nullptr) { - type_ast = rhs_cl->type; - } - Array new_elems = {}; array_init(&new_elems, heap_allocator()); array_add_elems(&new_elems, lhs_cl->elems.data, lhs_cl->elems.count); - array_add_elems(&new_elems, rhs_cl->elems.data, rhs_cl->elems.count); - Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(rhs.expr)); + for (isize i = 1; i < ce->args.count; i++) { + Operand extra = {}; + check_expr(c, &extra, ce->args[i]); + if (extra.mode == Addressing_Invalid) { + return false; + } + if (extra.mode != Addressing_Constant) { + error(extra.expr, "'%.*s' expects a constant array or slice", LIT(builtin_name)); + return false; + } + if (!are_types_identical(lhs.type, extra.type)) { + gbString a = type_to_string(lhs.type); + gbString b = type_to_string(extra.type); + error(extra.expr, "'%.*s' expects constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + + if (extra.value.kind != ExactValue_Compound) { + gbString a = exact_value_to_string(extra.value); + error(extra.expr, "Expected a compound literal value for '%.*s', got '%s'", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + + ast_node(extra_cl, CompoundLit, extra.value.value_compound); + + + for (Ast *elem : extra_cl->elems) { + if (elem->kind == Ast_FieldValue) { + error(elem, "'%.*s' does not allow the use of 'field = value' to be concatenated together", LIT(builtin_name)); + return false; + } + } + + if (type_ast == nullptr) { + type_ast = extra_cl->type; + } + array_add_elems(&new_elems, extra_cl->elems.data, extra_cl->elems.count); + } + + Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); operand->mode = Addressing_Constant; operand->value = exact_value_compound(new_compound_lit); diff --git a/src/checker_builtin_procs.hpp b/src/checker_builtin_procs.hpp index 8de6a4f5e..01502128a 100644 --- a/src/checker_builtin_procs.hpp +++ b/src/checker_builtin_procs.hpp @@ -429,7 +429,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = { {STR_LIT("soa_struct"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, // Type - {STR_LIT("concatenate"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, + {STR_LIT("concatenate"), 2, true, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("alloca"), 2, false, Expr_Expr, BuiltinProcPkg_intrinsics}, {STR_LIT("cpu_relax"), 0, false, Expr_Stmt, BuiltinProcPkg_intrinsics}, -- cgit v1.2.3 From 5e12532ebad58215cc6039dc77211652961dc1a9 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 11:49:09 +0100 Subject: Add basic type inference to the arguments --- src/check_builtin.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index c337802c9..a4109601e 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4888,7 +4888,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As case BuiltinProc_concatenate: { Operand lhs = {}; - check_expr(c, &lhs, ce->args[0]); + check_expr_with_type_hint(c, &lhs, ce->args[0], type_hint); if (lhs.mode == Addressing_Invalid) { return false; } @@ -4930,7 +4930,7 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As for (isize i = 1; i < ce->args.count; i++) { Operand extra = {}; - check_expr(c, &extra, ce->args[i]); + check_expr_with_type_hint(c, &extra, ce->args[i], lhs.type); if (extra.mode == Addressing_Invalid) { return false; } -- cgit v1.2.3 From 24bc044d78c5d02e174b15490a0f8f5289b0cb03 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 12:00:44 +0100 Subject: Support fixed-length arrays for `intrinsics.concatenate` --- src/check_builtin.cpp | 50 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 9 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index a4109601e..fb7b29edd 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4921,6 +4921,8 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } } + Type *elem_type = base_any_array_type(lhs.type); + Ast *type_ast = lhs_cl->type; Array new_elems = {}; @@ -4930,7 +4932,11 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As for (isize i = 1; i < ce->args.count; i++) { Operand extra = {}; - check_expr_with_type_hint(c, &extra, ce->args[i], lhs.type); + if (is_type_slice(lhs.type)) { + check_expr_with_type_hint(c, &extra, ce->args[i], lhs.type); + } else { + check_expr(c, &extra, ce->args[i]); + } if (extra.mode == Addressing_Invalid) { return false; } @@ -4938,13 +4944,34 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As error(extra.expr, "'%.*s' expects a constant array or slice", LIT(builtin_name)); return false; } - if (!are_types_identical(lhs.type, extra.type)) { - gbString a = type_to_string(lhs.type); - gbString b = type_to_string(extra.type); - error(extra.expr, "'%.*s' expects constant values of the same type, got '%s' vs '%s'", LIT(builtin_name), a, b); - gb_string_free(b); - gb_string_free(a); - return false; + + if (is_type_slice(lhs.type)) { + if (!are_types_identical(lhs.type, extra.type)) { + gbString a = type_to_string(lhs.type); + gbString b = type_to_string(extra.type); + error(extra.expr, "'%.*s' expects constant values of the same slice type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + } else if (is_type_array(lhs.type)) { + if (!is_type_array(extra.type)) { + gbString a = type_to_string(extra.type); + error(extra.expr, "'%.*s' expects a constant array or slice, got %s", LIT(builtin_name), a); + gb_string_free(a); + return false; + } + Type *extra_elem_type = base_array_type(extra.type); + if (!are_types_identical(elem_type, extra_elem_type)) { + gbString a = type_to_string(elem_type); + gbString b = type_to_string(extra_elem_type); + error(extra.expr, "'%.*s' expects constant values of the same element-type, got '%s' vs '%s'", LIT(builtin_name), a, b); + gb_string_free(b); + gb_string_free(a); + return false; + } + } else { + GB_PANIC("Unhandled type: %s", type_to_string(lhs.type)); } if (extra.value.kind != ExactValue_Compound) { @@ -4974,7 +5001,12 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As operand->mode = Addressing_Constant; operand->value = exact_value_compound(new_compound_lit); - operand->type = lhs.type; + + if (is_type_slice(lhs.type)) { + operand->type = lhs.type; + } else { + operand->type = alloc_type_array(elem_type, new_elems.count); + } break; } -- cgit v1.2.3 From 1387c3d311db07edb36d8db5d0a2befb8417b771 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 12:04:22 +0100 Subject: Remove unneeded type expression --- src/check_builtin.cpp | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index fb7b29edd..fcd2200dc 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4923,8 +4923,6 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As Type *elem_type = base_any_array_type(lhs.type); - Ast *type_ast = lhs_cl->type; - Array new_elems = {}; array_init(&new_elems, heap_allocator()); @@ -4991,13 +4989,10 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As } } - if (type_ast == nullptr) { - type_ast = extra_cl->type; - } array_add_elems(&new_elems, extra_cl->elems.data, extra_cl->elems.count); } - Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), type_ast, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); + Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), nullptr, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); operand->mode = Addressing_Constant; operand->value = exact_value_compound(new_compound_lit); -- cgit v1.2.3 From 26b3a4d182788a08ea1d9eb8d7c3b49235065cb6 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 10 Oct 2025 12:09:41 +0100 Subject: Handle concatenation at the end --- src/check_builtin.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) (limited to 'src/check_builtin.cpp') diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index fcd2200dc..530efb3c8 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -4923,11 +4923,6 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As Type *elem_type = base_any_array_type(lhs.type); - Array new_elems = {}; - array_init(&new_elems, heap_allocator()); - - array_add_elems(&new_elems, lhs_cl->elems.data, lhs_cl->elems.count); - for (isize i = 1; i < ce->args.count; i++) { Operand extra = {}; if (is_type_slice(lhs.type)) { @@ -4988,8 +4983,25 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As return false; } } + } + + isize count_needed = 0; - array_add_elems(&new_elems, extra_cl->elems.data, extra_cl->elems.count); + for (Ast *arg : ce->args) { + ExactValue value = arg->tav.value; + GB_ASSERT(value.kind == ExactValue_Compound); + ast_node(cl, CompoundLit, value.value_compound); + count_needed += cl->elems.count; + } + + Array new_elems = {}; + array_init(&new_elems, permanent_allocator(), 0, count_needed); + + for (Ast *arg : ce->args) { + ExactValue value = arg->tav.value; + GB_ASSERT(value.kind == ExactValue_Compound); + ast_node(cl, CompoundLit, value.value_compound); + array_add_elems(&new_elems, cl->elems.data, cl->elems.count); } Ast *new_compound_lit = ast_compound_lit(lhs.expr->file(), nullptr, new_elems, ast_token(lhs.expr), ast_end_token(ce->args[ce->args.count-1])); -- cgit v1.2.3