aboutsummaryrefslogtreecommitdiff
path: root/src/check_expr.cpp
diff options
context:
space:
mode:
authorRaph <raphfl.dev@gmail.com>2025-06-20 16:50:00 -0100
committerGitHub <noreply@github.com>2025-06-20 16:50:00 -0100
commita7e89e1324f64346b201aea8ac6205e0bc85eb21 (patch)
tree612abe74fa630e7cddad4d37ca5a04e18ff81471 /src/check_expr.cpp
parent0b5be6ad6a3c40ced071c89bb066dfd326b72943 (diff)
parentd9e08bc5d8a1292e3eccdb325bde4d180ebb4749 (diff)
Merge branch 'master' into tiocgwinsz_time
Diffstat (limited to 'src/check_expr.cpp')
-rw-r--r--src/check_expr.cpp481
1 files changed, 283 insertions, 198 deletions
diff --git a/src/check_expr.cpp b/src/check_expr.cpp
index 6fba64765..335b82f1a 100644
--- a/src/check_expr.cpp
+++ b/src/check_expr.cpp
@@ -263,6 +263,9 @@ gb_internal void check_did_you_mean_scope(String const &name, Scope *scope, char
gb_internal Entity *entity_from_expr(Ast *expr) {
expr = unparen_expr(expr);
+ if (expr == nullptr) {
+ return nullptr;
+ }
switch (expr->kind) {
case Ast_Ident:
return expr->Ident.entity;
@@ -643,7 +646,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 +998,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 (operand->mode == Addressing_Invalid || type == t_invalid) {
+ if (score_) *score_ = 0;
+ return false;
}
- if (score_) *score_ = score;
- return ok;
+
+ // 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;
}
@@ -1854,7 +1877,10 @@ gb_internal Entity *check_ident(CheckerContext *c, Operand *o, Ast *n, Type *nam
o->type = t_invalid;
}
if (o->type != nullptr && o->type->kind == Type_Named && o->type->Named.type_name->TypeName.is_type_alias) {
- o->type = base_type(o->type);
+ Type *bt = base_type(o->type);
+ if (bt != nullptr) {
+ o->type = bt;
+ }
}
break;
@@ -2907,9 +2933,20 @@ gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Oper
if (!defined) {
gbString xs = type_to_string(x->type, temporary_allocator());
gbString ys = type_to_string(y->type, temporary_allocator());
- err_str = gb_string_make(temporary_allocator(),
- gb_bprintf("operator '%.*s' not defined between the types '%s' and '%s'", LIT(token_strings[op]), xs, ys)
- );
+
+ if (!is_type_comparable(x->type)) {
+ err_str = gb_string_make(temporary_allocator(),
+ gb_bprintf("Type '%s' is not simply comparable, so operator '%.*s' is not defined for it", xs, LIT(token_strings[op]))
+ );
+ } else if (!is_type_comparable(y->type)) {
+ err_str = gb_string_make(temporary_allocator(),
+ gb_bprintf("Type '%s' is not simply comparable, so operator '%.*s' is not defined for it", ys, LIT(token_strings[op]))
+ );
+ } else {
+ err_str = gb_string_make(temporary_allocator(),
+ gb_bprintf("Operator '%.*s' not defined between the types '%s' and '%s'", LIT(token_strings[op]), xs, ys)
+ );
+ }
} else {
Type *comparison_type = x->type;
if (x->type == err_type && is_operand_nil(*x)) {
@@ -2930,11 +2967,11 @@ gb_internal void check_comparison(CheckerContext *c, Ast *node, Operand *x, Oper
} else {
yt = type_to_string(y->type);
}
- err_str = gb_string_make(temporary_allocator(), gb_bprintf("mismatched types '%s' and '%s'", xt, yt));
+ err_str = gb_string_make(temporary_allocator(), gb_bprintf("Mismatched types '%s' and '%s'", xt, yt));
}
if (err_str != nullptr) {
- error(node, "Cannot compare expression, %s", err_str);
+ error(node, "Cannot compare expression. %s.", err_str);
x->type = t_untyped_bool;
} else {
if (x->mode == Addressing_Constant &&
@@ -3850,6 +3887,59 @@ matrix_error:
}
+gb_internal void check_binary_expr_dependency(CheckerContext *c, Token op, Type *bt, bool REQUIRE) {
+ if (op.kind == Token_Mod || op.kind == Token_ModEq ||
+ op.kind == Token_ModMod || op.kind == Token_ModModEq) {
+ if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
+ case Basic_u128: add_package_dependency(c, "runtime", "umodti3", REQUIRE); break;
+ case Basic_i128: add_package_dependency(c, "runtime", "modti3", REQUIRE); break;
+ }
+ } else if (op.kind == Token_Quo || op.kind == Token_QuoEq) {
+ if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
+ case Basic_complex32: add_package_dependency(c, "runtime", "quo_complex32"); break;
+ case Basic_complex64: add_package_dependency(c, "runtime", "quo_complex64"); break;
+ case Basic_complex128: add_package_dependency(c, "runtime", "quo_complex128"); break;
+ case Basic_quaternion64: add_package_dependency(c, "runtime", "quo_quaternion64"); break;
+ case Basic_quaternion128: add_package_dependency(c, "runtime", "quo_quaternion128"); break;
+ case Basic_quaternion256: add_package_dependency(c, "runtime", "quo_quaternion256"); break;
+
+ case Basic_u128: add_package_dependency(c, "runtime", "udivti3", REQUIRE); break;
+ case Basic_i128: add_package_dependency(c, "runtime", "divti3", REQUIRE); break;
+ }
+ } else if (op.kind == Token_Mul || op.kind == Token_MulEq) {
+ if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
+ case Basic_quaternion64: add_package_dependency(c, "runtime", "mul_quaternion64"); break;
+ case Basic_quaternion128: add_package_dependency(c, "runtime", "mul_quaternion128"); break;
+ case Basic_quaternion256: add_package_dependency(c, "runtime", "mul_quaternion256"); break;
+
+
+ case Basic_u128:
+ case Basic_i128:
+ if (is_arch_wasm()) {
+ add_package_dependency(c, "runtime", "__multi3", REQUIRE);
+ }
+ break;
+ }
+ } else if (op.kind == Token_Shl || op.kind == Token_ShlEq) {
+ if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
+ case Basic_u128:
+ case Basic_i128:
+ if (is_arch_wasm()) {
+ add_package_dependency(c, "runtime", "__ashlti3", REQUIRE);
+ }
+ break;
+ }
+ } else if (op.kind == Token_Shr || op.kind == Token_ShrEq) {
+ if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
+ case Basic_u128:
+ case Basic_i128:
+ if (is_arch_wasm()) {
+ add_package_dependency(c, "runtime", "__lshrti3", REQUIRE);
+ }
+ break;
+ }
+ }
+}
gb_internal void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Type *type_hint, bool use_lhs_as_type_hint=false) {
GB_ASSERT(node->kind == Ast_BinaryExpr);
@@ -4047,58 +4137,10 @@ gb_internal void check_binary_expr(CheckerContext *c, Operand *x, Ast *node, Typ
}
bool REQUIRE = true;
- Type *bt = base_type(x->type);
- if (op.kind == Token_Mod || op.kind == Token_ModEq ||
- op.kind == Token_ModMod || op.kind == Token_ModModEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_u128: add_package_dependency(c, "runtime", "umodti3", REQUIRE); break;
- case Basic_i128: add_package_dependency(c, "runtime", "modti3", REQUIRE); break;
- }
- } else if (op.kind == Token_Quo || op.kind == Token_QuoEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_complex32: add_package_dependency(c, "runtime", "quo_complex32"); break;
- case Basic_complex64: add_package_dependency(c, "runtime", "quo_complex64"); break;
- case Basic_complex128: add_package_dependency(c, "runtime", "quo_complex128"); break;
- case Basic_quaternion64: add_package_dependency(c, "runtime", "quo_quaternion64"); break;
- case Basic_quaternion128: add_package_dependency(c, "runtime", "quo_quaternion128"); break;
- case Basic_quaternion256: add_package_dependency(c, "runtime", "quo_quaternion256"); break;
-
- case Basic_u128: add_package_dependency(c, "runtime", "udivti3", REQUIRE); break;
- case Basic_i128: add_package_dependency(c, "runtime", "divti3", REQUIRE); break;
- }
- } else if (op.kind == Token_Mul || op.kind == Token_MulEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_quaternion64: add_package_dependency(c, "runtime", "mul_quaternion64"); break;
- case Basic_quaternion128: add_package_dependency(c, "runtime", "mul_quaternion128"); break;
- case Basic_quaternion256: add_package_dependency(c, "runtime", "mul_quaternion256"); break;
-
-
- case Basic_u128:
- case Basic_i128:
- if (is_arch_wasm()) {
- add_package_dependency(c, "runtime", "__multi3", REQUIRE);
- }
- break;
- }
- } else if (op.kind == Token_Shl || op.kind == Token_ShlEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_u128:
- case Basic_i128:
- if (is_arch_wasm()) {
- add_package_dependency(c, "runtime", "__ashlti3", REQUIRE);
- }
- break;
- }
- } else if (op.kind == Token_Shr || op.kind == Token_ShrEq) {
- if (bt->kind == Type_Basic) switch (bt->Basic.kind) {
- case Basic_u128:
- case Basic_i128:
- if (is_arch_wasm()) {
- add_package_dependency(c, "runtime", "__lshrti3", REQUIRE);
- }
- break;
- }
- }
+ Type *btx = base_type(x->type);
+ Type *bty = base_type(y->type);
+ check_binary_expr_dependency(c, op, btx, REQUIRE);
+ check_binary_expr_dependency(c, op, bty, REQUIRE);
if (token_is_shift(op.kind)) {
check_shift(c, x, y, node, type_hint);
@@ -5339,16 +5381,6 @@ gb_internal Entity *check_selector(CheckerContext *c, Operand *operand, Ast *nod
return nullptr;
}
- check_entity_decl(c, entity, nullptr, nullptr);
- if (entity->kind == Entity_ProcGroup) {
- operand->mode = Addressing_ProcGroup;
- operand->proc_group = entity;
-
- add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
- return entity;
- }
- GB_ASSERT_MSG(entity->type != nullptr, "%.*s (%.*s)", LIT(entity->token.string), LIT(entity_strings[entity->kind]));
-
if (!is_entity_exported(entity, allow_builtin)) {
gbString sel_str = expr_to_string(selector);
error(node, "'%s' is not exported by '%.*s'", sel_str, LIT(import_name));
@@ -5359,36 +5391,15 @@ gb_internal Entity *check_selector(CheckerContext *c, Operand *operand, Ast *nod
// return nullptr;
}
+ check_entity_decl(c, entity, nullptr, nullptr);
if (entity->kind == Entity_ProcGroup) {
- Array<Entity *> procs = entity->ProcGroup.entities;
- bool skip = false;
- for (Entity *p : procs) {
- Type *t = base_type(p->type);
- if (t == t_invalid) {
- continue;
- }
-
- Operand x = {};
- x.mode = Addressing_Value;
- x.type = t;
- if (type_hint != nullptr) {
- if (check_is_assignable_to(c, &x, type_hint)) {
- entity = p;
- skip = true;
- break;
- }
- }
- }
+ operand->mode = Addressing_ProcGroup;
+ operand->proc_group = entity;
- if (!skip) {
- GB_ASSERT(entity != nullptr);
- operand->mode = Addressing_ProcGroup;
- operand->type = t_invalid;
- operand->expr = node;
- operand->proc_group = entity;
- return entity;
- }
+ add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
+ return entity;
}
+ GB_ASSERT_MSG(entity->type != nullptr, "%.*s (%.*s)", LIT(entity->token.string), LIT(entity_strings[entity->kind]));
}
}
@@ -5427,8 +5438,18 @@ gb_internal Entity *check_selector(CheckerContext *c, Operand *operand, Ast *nod
}
}
+ if (operand->type && is_type_simd_vector(type_deref(operand->type))) {
+ String field_name = selector->Ident.token.string;
+ if (field_name.len == 1) {
+ error(op_expr, "Extracting an element from a #simd array using .%.*s syntax is disallowed, prefer `simd.extract`", LIT(field_name));
+ } else {
+ error(op_expr, "Extracting elements from a #simd array using .%.*s syntax is disallowed, prefer `swizzle`", LIT(field_name));
+ }
+ return nullptr;
+ }
+
if (entity == nullptr && selector->kind == Ast_Ident && operand->type != nullptr &&
- (is_type_array(type_deref(operand->type)) || is_type_simd_vector(type_deref(operand->type)))) {
+ (is_type_array(type_deref(operand->type)))) {
String field_name = selector->Ident.token.string;
if (1 < field_name.len && field_name.len <= 4) {
u8 swizzles_xyzw[4] = {'x', 'y', 'z', 'w'};
@@ -5483,7 +5504,7 @@ gb_internal Entity *check_selector(CheckerContext *c, Operand *operand, Ast *nod
Type *original_type = operand->type;
Type *array_type = base_type(type_deref(original_type));
- GB_ASSERT(array_type->kind == Type_Array || array_type->kind == Type_SimdVector);
+ GB_ASSERT(array_type->kind == Type_Array);
i64 array_count = get_array_type_count(array_type);
@@ -5524,10 +5545,6 @@ gb_internal Entity *check_selector(CheckerContext *c, Operand *operand, Ast *nod
break;
}
- if (array_type->kind == Type_SimdVector) {
- operand->mode = Addressing_Value;
- }
-
Entity *swizzle_entity = alloc_entity_variable(nullptr, make_token_ident(field_name), operand->type, EntityState_Resolved);
add_type_and_value(c, operand->expr, operand->mode, operand->type, operand->value);
return swizzle_entity;
@@ -6480,7 +6497,7 @@ gb_internal Entity **populate_proc_parameter_list(CheckerContext *c, Type *proc_
Entity **lhs = nullptr;
isize lhs_count = -1;
- if (proc_type == nullptr) {
+ if (proc_type == nullptr || proc_type == t_invalid) {
return nullptr;
}
@@ -6680,6 +6697,9 @@ gb_internal bool check_call_arguments_single(CheckerContext *c, Ast *call, Opera
GB_ASSERT(proc_type != nullptr);
proc_type = base_type(proc_type);
+ if (proc_type == t_invalid) {
+ return false;
+ }
GB_ASSERT(proc_type->kind == Type_Proc);
CallArgumentError err = check_call_arguments_internal(c, call, e, proc_type, positional_operands, named_operands, show_error_mode, data);
@@ -7920,7 +7940,27 @@ gb_internal ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *c
default:
{
gbString str = type_to_string(t);
- error(call, "Too many arguments in conversion to '%s'", str);
+ if (t->kind == Type_Basic) {
+ ERROR_BLOCK();
+ switch (t->Basic.kind) {
+ case Basic_complex32:
+ case Basic_complex64:
+ case Basic_complex128:
+ error(call, "Too many arguments in conversion to '%s'", str);
+ error_line("\tSuggestion: %s(1+2i) or construct with 'complex'\n", str);
+ break;
+ case Basic_quaternion64:
+ case Basic_quaternion128:
+ case Basic_quaternion256:
+ error(call, "Too many arguments in conversion to '%s'", str);
+ error_line("\tSuggestion: %s(1+2i+3j+4k) or construct with 'quaternion'\n", str);
+ break;
+ default:
+ error(call, "Too many arguments in conversion to '%s'", str);
+ }
+ } else {
+ error(call, "Too many arguments in conversion to '%s'", str);
+ }
gb_string_free(str);
} break;
case 1: {
@@ -8036,7 +8076,9 @@ gb_internal ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *c
if (pt->kind == Type_Proc && pt->Proc.calling_convention == ProcCC_Odin) {
if ((c->scope->flags & ScopeFlag_ContextDefined) == 0) {
+ ERROR_BLOCK();
error(call, "'context' has not been defined within this scope, but is required for this procedure call");
+ error_line("\tSuggestion: 'context = runtime.default_context()'");
}
}
@@ -8140,7 +8182,7 @@ gb_internal ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *c
type = pt;
}
type = base_type(type);
- if (type->kind == Type_Proc && type->Proc.optional_ok) {
+ if (type->kind == Type_Proc && type->Proc.optional_ok && type->Proc.result_count > 0) {
operand->mode = Addressing_OptionalOk;
operand->type = type->Proc.results->Tuple.variables[0]->type;
if (operand->expr != nullptr && operand->expr->kind == Ast_CallExpr) {
@@ -8977,8 +9019,14 @@ gb_internal ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node
o->expr = node;
return Expr_Expr;
}
+
+ Type *left_type = nullptr;
+ Type *right_type = nullptr;
+ check_or_else_split_types(c, &x, name, &left_type, &right_type);
+ add_type_and_value(c, arg, x.mode, x.type, x.value);
+
bool y_is_diverging = false;
- check_expr_base(c, &y, default_value, x.type);
+ check_expr_base(c, &y, default_value, left_type);
switch (y.mode) {
case Addressing_NoValue:
if (is_diverging_expr(y.expr)) {
@@ -9003,14 +9051,21 @@ gb_internal ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node
return Expr_Expr;
}
- Type *left_type = nullptr;
- Type *right_type = nullptr;
- check_or_else_split_types(c, &x, name, &left_type, &right_type);
- add_type_and_value(c, arg, x.mode, x.type, x.value);
-
if (left_type != nullptr) {
if (!y_is_diverging) {
- check_assignment(c, &y, left_type, name);
+ if (is_type_tuple(left_type)) {
+ if (!is_type_tuple(y.type)) {
+ error(y.expr, "Found a single value where a %td-valued expression was expected", left_type->Tuple.variables.count);
+ } else if (!are_types_identical(left_type, y.type)) {
+ gbString xt = type_to_string(left_type);
+ gbString yt = type_to_string(y.type);
+ error(y.expr, "Mismatched types, expected (%s), got (%s)", xt, yt);
+ gb_string_free(yt);
+ gb_string_free(xt);
+ }
+ } else {
+ check_assignment(c, &y, left_type, name);
+ }
}
} else {
check_or_else_expr_no_value_error(c, name, x, type_hint);
@@ -9386,7 +9441,7 @@ gb_internal bool is_expr_inferred_fixed_array(Ast *type_expr) {
}
gb_internal bool check_for_dynamic_literals(CheckerContext *c, Ast *node, AstCompoundLit *cl) {
- if (cl->elems.count > 0 && (check_feature_flags(c, node) & OptInFeatureFlag_DynamicLiterals) == 0) {
+ if (cl->elems.count > 0 && (check_feature_flags(c, node) & OptInFeatureFlag_DynamicLiterals) == 0 && !build_context.dynamic_literals) {
ERROR_BLOCK();
error(node, "Compound literals of dynamic types are disabled by default");
error_line("\tSuggestion: If you want to enable them for this specific file, add '#+feature dynamic-literals' at the top of the file\n");
@@ -9412,6 +9467,7 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
}
bool is_to_be_determined_array_count = false;
bool is_constant = true;
+ bool is_soa = false;
Ast *type_expr = cl->type;
@@ -9444,8 +9500,14 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
GB_ASSERT(tag->kind == Ast_BasicDirective);
String name = tag->BasicDirective.name.string;
if (name == "soa") {
- error(node, "#soa arrays are not supported for compound literals");
- return kind;
+ is_soa = true;
+ if (count == nullptr) {
+ error(node, "#soa slices are not supported for compound literals");
+ return kind;
+ } else if (count->kind == Ast_UnaryExpr &&
+ count->UnaryExpr.op.kind == Token_Question) {
+ error(node, "#soa fixed length arrays must specify their length and cannot use ?");
+ }
}
}
}
@@ -9455,7 +9517,8 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
GB_ASSERT(tag->kind == Ast_BasicDirective);
String name = tag->BasicDirective.name.string;
if (name == "soa") {
- error(node, "#soa arrays are not supported for compound literals");
+ is_soa = true;
+ error(node, "#soa dynamic arrays are not supported for compound literals");
return kind;
}
}
@@ -9484,101 +9547,101 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
switch (t->kind) {
- case Type_Struct: {
+ case Type_Struct:
if (cl->elems.count == 0) {
break; // NOTE(bill): No need to init
}
- if (t->Struct.soa_kind != StructSoa_None) {
- error(node, "#soa arrays are not supported for compound literals");
- break;
- }
-
- if (t->Struct.is_raw_union) {
- if (cl->elems.count > 0) {
- // NOTE: unions cannot be constant
- is_constant = false;
+ if (t->Struct.soa_kind == StructSoa_None) {
+ if (t->Struct.is_raw_union) {
+ if (cl->elems.count > 0) {
+ // NOTE: unions cannot be constant
+ is_constant = false;
- if (cl->elems[0]->kind != Ast_FieldValue) {
- gbString type_str = type_to_string(type);
- error(node, "%s ('struct #raw_union') compound literals are only allowed to contain 'field = value' elements", type_str);
- gb_string_free(type_str);
- } else {
- if (cl->elems.count != 1) {
+ if (cl->elems[0]->kind != Ast_FieldValue) {
gbString type_str = type_to_string(type);
- error(node, "%s ('struct #raw_union') compound literals are only allowed to contain up to 1 'field = value' element, got %td", type_str, cl->elems.count);
+ error(node, "%s ('struct #raw_union') compound literals are only allowed to contain 'field = value' elements", type_str);
gb_string_free(type_str);
} else {
- check_compound_literal_field_values(c, cl->elems, o, type, is_constant);
+ if (cl->elems.count != 1) {
+ gbString type_str = type_to_string(type);
+ error(node, "%s ('struct #raw_union') compound literals are only allowed to contain up to 1 'field = value' element, got %td", type_str, cl->elems.count);
+ gb_string_free(type_str);
+ } else {
+ check_compound_literal_field_values(c, cl->elems, o, type, is_constant);
+ }
}
}
- }
- break;
- }
-
- wait_signal_until_available(&t->Struct.fields_wait_signal);
- isize field_count = t->Struct.fields.count;
- isize min_field_count = t->Struct.fields.count;
- for (isize i = min_field_count-1; i >= 0; i--) {
- Entity *e = t->Struct.fields[i];
- GB_ASSERT(e->kind == Entity_Variable);
- if (e->Variable.param_value.kind != ParameterValue_Invalid) {
- min_field_count--;
- } else {
break;
}
- }
- if (cl->elems[0]->kind == Ast_FieldValue) {
- check_compound_literal_field_values(c, cl->elems, o, type, is_constant);
- } else {
- bool seen_field_value = false;
-
- for_array(index, cl->elems) {
- Entity *field = nullptr;
- Ast *elem = cl->elems[index];
- if (elem->kind == Ast_FieldValue) {
- seen_field_value = true;
- error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
- continue;
- } else if (seen_field_value) {
- error(elem, "Value elements cannot be used after a 'field = value'");
- continue;
- }
- if (index >= field_count) {
- error(elem, "Too many values in structure literal, expected %td, got %td", field_count, cl->elems.count);
+ wait_signal_until_available(&t->Struct.fields_wait_signal);
+ isize field_count = t->Struct.fields.count;
+ isize min_field_count = t->Struct.fields.count;
+ for (isize i = min_field_count-1; i >= 0; i--) {
+ Entity *e = t->Struct.fields[i];
+ GB_ASSERT(e->kind == Entity_Variable);
+ if (e->Variable.param_value.kind != ParameterValue_Invalid) {
+ min_field_count--;
+ } else {
break;
}
+ }
- if (field == nullptr) {
- field = t->Struct.fields[index];
- }
+ if (cl->elems[0]->kind == Ast_FieldValue) {
+ check_compound_literal_field_values(c, cl->elems, o, type, is_constant);
+ } else {
+ bool seen_field_value = false;
- Operand o = {};
- check_expr_or_type(c, &o, elem, field->type);
+ for_array(index, cl->elems) {
+ Entity *field = nullptr;
+ Ast *elem = cl->elems[index];
+ if (elem->kind == Ast_FieldValue) {
+ seen_field_value = true;
+ error(elem, "Mixture of 'field = value' and value elements in a literal is not allowed");
+ continue;
+ } else if (seen_field_value) {
+ error(elem, "Value elements cannot be used after a 'field = value'");
+ continue;
+ }
+ if (index >= field_count) {
+ error(elem, "Too many values in structure literal, expected %td, got %td", field_count, cl->elems.count);
+ break;
+ }
- if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type) || is_type_typeid(field->type)) {
- is_constant = false;
- }
- if (is_constant) {
- is_constant = check_is_operand_compound_lit_constant(c, &o);
- }
+ if (field == nullptr) {
+ field = t->Struct.fields[index];
+ }
- check_assignment(c, &o, field->type, str_lit("structure literal"));
- }
- if (cl->elems.count < field_count) {
- if (min_field_count < field_count) {
- if (cl->elems.count < min_field_count) {
- error(cl->close, "Too few values in structure literal, expected at least %td, got %td", min_field_count, cl->elems.count);
- }
- } else {
- error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
+ Operand o = {};
+ check_expr_or_type(c, &o, elem, field->type);
+
+ if (is_type_any(field->type) || is_type_union(field->type) || is_type_raw_union(field->type) || is_type_typeid(field->type)) {
+ is_constant = false;
+ }
+ if (is_constant) {
+ is_constant = check_is_operand_compound_lit_constant(c, &o);
+ }
+
+ check_assignment(c, &o, field->type, str_lit("structure literal"));
+ }
+ if (cl->elems.count < field_count) {
+ if (min_field_count < field_count) {
+ if (cl->elems.count < min_field_count) {
+ error(cl->close, "Too few values in structure literal, expected at least %td, got %td", min_field_count, cl->elems.count);
+ }
+ } else {
+ error(cl->close, "Too few values in structure literal, expected %td, got %td", field_count, cl->elems.count);
+ }
}
}
- }
- break;
- }
+ break;
+ } else if (t->Struct.soa_kind != StructSoa_Fixed) {
+ error(node, "#soa slices and dynamic arrays are not supported for compound literals");
+ break;
+ }
+ /*fallthrough*/
case Type_Slice:
case Type_Array:
@@ -9589,7 +9652,14 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
Type *elem_type = nullptr;
String context_name = {};
i64 max_type_count = -1;
- if (t->kind == Type_Slice) {
+ if (t->kind == Type_Struct) {
+ GB_ASSERT(t->Struct.soa_kind == StructSoa_Fixed);
+ elem_type = t->Struct.soa_elem;
+ context_name = str_lit("#soa array literal");
+ if (!is_to_be_determined_array_count) {
+ max_type_count = t->Struct.soa_count;
+ }
+ } else if (t->kind == Type_Slice) {
elem_type = t->Slice.elem;
context_name = str_lit("slice literal");
} else if (t->kind == Type_Array) {
@@ -9598,6 +9668,12 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
if (!is_to_be_determined_array_count) {
max_type_count = t->Array.count;
}
+ } else if (t->kind == Type_Array) {
+ elem_type = t->Array.elem;
+ context_name = str_lit("array literal");
+ if (!is_to_be_determined_array_count) {
+ max_type_count = t->Array.count;
+ }
} else if (t->kind == Type_DynamicArray) {
elem_type = t->DynamicArray.elem;
context_name = str_lit("dynamic array literal");
@@ -9765,6 +9841,15 @@ gb_internal ExprKind check_compound_literal(CheckerContext *c, Operand *o, Ast *
error(node, "Expected %lld values for this array literal, got %lld", cast(long long)t->Array.count, cast(long long)max);
}
}
+ } else if (t->kind == Type_Struct) {
+ GB_ASSERT(t->Struct.soa_kind == StructSoa_Fixed);
+ if (is_to_be_determined_array_count) {
+ t->Struct.soa_count = cast(i32)max;
+ } else if (cl->elems.count > 0 && cl->elems[0]->kind != Ast_FieldValue) {
+ if (0 < max && max < t->Struct.soa_count) {
+ error(node, "Expected %lld values for this #soa array literal, got %lld", cast(long long)t->Struct.soa_count, cast(long long)max);
+ }
+ }
}
@@ -10969,7 +11054,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:
{