diff options
| author | FourteenBrush <74827262+FourteenBrush@users.noreply.github.com> | 2025-07-11 17:03:49 +0200 |
|---|---|---|
| committer | FourteenBrush <74827262+FourteenBrush@users.noreply.github.com> | 2025-07-11 17:03:49 +0200 |
| commit | 8cbf75c928e91a93fbd0ab48877c5f32afa3a9f4 (patch) | |
| tree | ca0225b76d0ec0e58fe22eba6a604fb0d96f2257 /src/check_builtin.cpp | |
| parent | 5ce8d1ede834134ec7fd71c62738791c2d36e171 (diff) | |
Add type_enum_is_contiguous intrinsic
Diffstat (limited to 'src/check_builtin.cpp')
| -rw-r--r-- | src/check_builtin.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/check_builtin.cpp b/src/check_builtin.cpp index 9f9787b61..70848cec6 100644 --- a/src/check_builtin.cpp +++ b/src/check_builtin.cpp @@ -1,5 +1,11 @@ typedef bool (BuiltinTypeIsProc)(Type *t); +gb_internal int enum_constant_entity_cmp(void const* a, void const* b) { + BigInt bi = (*cast(Entity const **)a)->Constant.value.value_integer; + BigInt bj = (*cast(Entity const **)b)->Constant.value.value_integer; + return big_int_cmp(&bi, &bj); +} + gb_global BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - BuiltinProc__type_simple_boolean_begin] = { nullptr, // BuiltinProc__type_simple_boolean_begin @@ -6919,6 +6925,48 @@ gb_internal bool check_builtin_procedure(CheckerContext *c, Operand *operand, As break; } + case BuiltinProc_type_enum_is_contiguous: + { + Operand op = {}; + Type *bt = check_type(c, ce->args[0]); + Type *type = base_type(bt); + if (type == nullptr || type == t_invalid) { + error(ce->args[0], "Expected a type for '%.*s'", LIT(builtin_name)); + return false; + } + if (!is_type_enum(type)) { + gbString t = type_to_string(type); + error(ce->args[0], "Expected an enum type for '%.*s', got %s", LIT(builtin_name), t); + gb_string_free(t); + return false; + } + + // sort enum fields in place in ascending order + Array<Entity *> enum_constants = type->Enum.fields; + array_sort(enum_constants, enum_constant_entity_cmp); + + BigInt minus_one = big_int_make_i64(-1); + BigInt diff = {}; + + bool contiguous = true; + operand->mode = Addressing_Constant; + operand->type = t_untyped_bool; + + for (isize i = 0; i < enum_constants.count - 1; i++) { + BigInt curr = enum_constants[i]->Constant.value.value_integer; + BigInt next = enum_constants[i + 1]->Constant.value.value_integer; + big_int_sub(&diff, &curr, &next); + + if (!big_int_is_zero(&diff) && big_int_cmp(&diff, &minus_one) != 0) { + contiguous = false; + break; + } + } + + operand->value = exact_value_bool(contiguous); + break; + } + case BuiltinProc_type_equal_proc: { Operand op = {}; |