mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-05 23:28:48 +00:00
Add type_enum_is_contiguous intrinsic
This commit is contained in:
@@ -213,6 +213,9 @@ type_is_subtype_of :: proc($T, $U: typeid) -> bool ---
|
|||||||
|
|
||||||
type_field_index_of :: proc($T: typeid, $name: string) -> uintptr ---
|
type_field_index_of :: proc($T: typeid, $name: string) -> uintptr ---
|
||||||
|
|
||||||
|
// Contiguous as in having a set of constants, when sorted, the difference between consecutive values is only 0 or 1
|
||||||
|
type_enum_is_contiguous :: proc($T: typeid) -> bool where type_is_enum(T) ---
|
||||||
|
|
||||||
type_equal_proc :: proc($T: typeid) -> (equal: proc "contextless" (rawptr, rawptr) -> bool) where type_is_comparable(T) ---
|
type_equal_proc :: proc($T: typeid) -> (equal: proc "contextless" (rawptr, rawptr) -> bool) where type_is_comparable(T) ---
|
||||||
type_hasher_proc :: proc($T: typeid) -> (hasher: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr) where type_is_comparable(T) ---
|
type_hasher_proc :: proc($T: typeid) -> (hasher: proc "contextless" (data: rawptr, seed: uintptr) -> uintptr) where type_is_comparable(T) ---
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,11 @@
|
|||||||
typedef bool (BuiltinTypeIsProc)(Type *t);
|
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] = {
|
gb_global BuiltinTypeIsProc *builtin_type_is_procs[BuiltinProc__type_simple_boolean_end - BuiltinProc__type_simple_boolean_begin] = {
|
||||||
nullptr, // 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;
|
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:
|
case BuiltinProc_type_equal_proc:
|
||||||
{
|
{
|
||||||
Operand op = {};
|
Operand op = {};
|
||||||
|
|||||||
@@ -325,6 +325,8 @@ BuiltinProc__type_simple_boolean_end,
|
|||||||
|
|
||||||
BuiltinProc_type_bit_set_backing_type,
|
BuiltinProc_type_bit_set_backing_type,
|
||||||
|
|
||||||
|
BuiltinProc_type_enum_is_contiguous,
|
||||||
|
|
||||||
BuiltinProc_type_equal_proc,
|
BuiltinProc_type_equal_proc,
|
||||||
BuiltinProc_type_hasher_proc,
|
BuiltinProc_type_hasher_proc,
|
||||||
BuiltinProc_type_map_info,
|
BuiltinProc_type_map_info,
|
||||||
@@ -678,6 +680,8 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
|
|||||||
|
|
||||||
{STR_LIT("type_bit_set_backing_type"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_bit_set_backing_type"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|
||||||
|
{STR_LIT("type_enum_is_contiguous"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics },
|
||||||
|
|
||||||
{STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_equal_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_hasher_proc"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
{STR_LIT("type_map_info"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
{STR_LIT("type_map_info"), 1, false, Expr_Expr, BuiltinProcPkg_intrinsics},
|
||||||
|
|||||||
Reference in New Issue
Block a user