|
@@ -1,5 +1,14 @@
|
|
|
typedef bool (BuiltinTypeIsProc)(Type *t);
|
|
|
|
|
|
+gb_internal int enum_constant_entity_cmp(void const* a, void const* b) {
|
|
|
+ Entity const *ea = *(cast(Entity const **)a);
|
|
|
+ Entity const *eb = *(cast(Entity const **)b);
|
|
|
+ GB_ASSERT(ea->kind == Entity_Constant && eb->kind == Entity_Constant);
|
|
|
+ GB_ASSERT(ea->Constant.value.kind == ExactValue_Integer && eb->Constant.value.kind == ExactValue_Integer);
|
|
|
+
|
|
|
+ return big_int_cmp(&ea->Constant.value.value_integer, &eb->Constant.value.value_integer);
|
|
|
+}
|
|
|
+
|
|
|
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 +6928,50 @@ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ auto enum_constants = array_make<Entity *>(temporary_allocator(), type->Enum.fields.count);
|
|
|
+ array_copy(&enum_constants, type->Enum.fields, 0);
|
|
|
+ array_sort(enum_constants, enum_constant_entity_cmp);
|
|
|
+
|
|
|
+ BigInt minus_one = big_int_make_i64(-1);
|
|
|
+ defer (big_int_dealloc(&minus_one));
|
|
|
+ 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);
|
|
|
+ defer (big_int_dealloc(&diff));
|
|
|
+
|
|
|
+ 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 = {};
|