aboutsummaryrefslogtreecommitdiff
path: root/src-self-hosted/value.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src-self-hosted/value.zig')
-rw-r--r--src-self-hosted/value.zig75
1 files changed, 75 insertions, 0 deletions
diff --git a/src-self-hosted/value.zig b/src-self-hosted/value.zig
index e71805dc5b..b2fad9207d 100644
--- a/src-self-hosted/value.zig
+++ b/src-self-hosted/value.zig
@@ -568,6 +568,81 @@ pub const Value = extern union {
}
}
+ /// Asserts the value is an integer and it fits in a i64
+ pub fn toSignedInt(self: Value) i64 {
+ switch (self.tag()) {
+ .ty,
+ .int_type,
+ .u8_type,
+ .i8_type,
+ .u16_type,
+ .i16_type,
+ .u32_type,
+ .i32_type,
+ .u64_type,
+ .i64_type,
+ .usize_type,
+ .isize_type,
+ .c_short_type,
+ .c_ushort_type,
+ .c_int_type,
+ .c_uint_type,
+ .c_long_type,
+ .c_ulong_type,
+ .c_longlong_type,
+ .c_ulonglong_type,
+ .c_longdouble_type,
+ .f16_type,
+ .f32_type,
+ .f64_type,
+ .f128_type,
+ .c_void_type,
+ .bool_type,
+ .void_type,
+ .type_type,
+ .anyerror_type,
+ .comptime_int_type,
+ .comptime_float_type,
+ .noreturn_type,
+ .null_type,
+ .undefined_type,
+ .fn_noreturn_no_args_type,
+ .fn_void_no_args_type,
+ .fn_naked_noreturn_no_args_type,
+ .fn_ccc_void_no_args_type,
+ .single_const_pointer_to_comptime_int_type,
+ .const_slice_u8_type,
+ .null_value,
+ .function,
+ .ref_val,
+ .decl_ref,
+ .elem_ptr,
+ .bytes,
+ .repeated,
+ .float_16,
+ .float_32,
+ .float_64,
+ .float_128,
+ .void_value,
+ .unreachable_value,
+ .empty_array,
+ => unreachable,
+
+ .undef => unreachable,
+
+ .zero,
+ .bool_false,
+ => return 0,
+
+ .bool_true => return 1,
+
+ .int_u64 => return @intCast(i64, self.cast(Payload.Int_i64).?.int),
+ .int_i64 => return self.cast(Payload.Int_i64).?.int,
+ .int_big_positive => return self.cast(Payload.IntBigPositive).?.asBigInt().to(i64) catch unreachable,
+ .int_big_negative => return self.cast(Payload.IntBigNegative).?.asBigInt().to(i64) catch unreachable,
+ }
+ }
+
pub fn toBool(self: Value) bool {
return switch (self.tag()) {
.bool_true => true,