aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-10-23 19:46:48 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-10-23 19:47:32 -0700
commit22b4c9e1a9595bd94ada4c500a430e2668ffcd07 (patch)
tree325e312f063422e19b0427373194b703b8cc3b53 /src/value.zig
parentee98d8700818aa667137e3aa580b16df2ba6d680 (diff)
downloadzig-22b4c9e1a9595bd94ada4c500a430e2668ffcd07.tar.gz
zig-22b4c9e1a9595bd94ada4c500a430e2668ffcd07.zip
stage2: implement more C pointer Sema and comptime ptr arith
Diffstat (limited to 'src/value.zig')
-rw-r--r--src/value.zig22
1 files changed, 14 insertions, 8 deletions
diff --git a/src/value.zig b/src/value.zig
index 3a4cdb1438..af1216f652 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -937,9 +937,10 @@ pub const Value = extern union {
}
}
- /// Asserts the value is an integer and it fits in a u64
- pub fn toUnsignedInt(self: Value) u64 {
- switch (self.tag()) {
+ /// If the value fits in a u64, return it, otherwise null.
+ /// Asserts not undefined.
+ pub fn getUnsignedInt(val: Value) ?u64 {
+ switch (val.tag()) {
.zero,
.bool_false,
.the_only_possible_value, // i0, u0
@@ -949,16 +950,21 @@ pub const Value = extern union {
.bool_true,
=> return 1,
- .int_u64 => return self.castTag(.int_u64).?.data,
- .int_i64 => return @intCast(u64, self.castTag(.int_i64).?.data),
- .int_big_positive => return self.castTag(.int_big_positive).?.asBigInt().to(u64) catch unreachable,
- .int_big_negative => return self.castTag(.int_big_negative).?.asBigInt().to(u64) catch unreachable,
+ .int_u64 => return val.castTag(.int_u64).?.data,
+ .int_i64 => return @intCast(u64, val.castTag(.int_i64).?.data),
+ .int_big_positive => return val.castTag(.int_big_positive).?.asBigInt().to(u64) catch null,
+ .int_big_negative => return val.castTag(.int_big_negative).?.asBigInt().to(u64) catch null,
.undef => unreachable,
- else => unreachable,
+ else => return null,
}
}
+ /// Asserts the value is an integer and it fits in a u64
+ pub fn toUnsignedInt(val: Value) u64 {
+ return getUnsignedInt(val).?;
+ }
+
/// Asserts the value is an integer and it fits in a i64
pub fn toSignedInt(self: Value) i64 {
switch (self.tag()) {