aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2023-05-21 03:57:12 -0400
committerAndrew Kelley <andrew@ziglang.org>2023-06-10 20:47:54 -0700
commitbe1b23120648dff5e23d67b089c10b479564bffd (patch)
tree79e455747856a29c096ee4f5e5ef61e7186b57c2 /src/value.zig
parent9584feae5f27a8b987975d8fe8242e2169098a75 (diff)
downloadzig-be1b23120648dff5e23d67b089c10b479564bffd.tar.gz
zig-be1b23120648dff5e23d67b089c10b479564bffd.zip
InternPool: add missing logic
Diffstat (limited to 'src/value.zig')
-rw-r--r--src/value.zig50
1 files changed, 33 insertions, 17 deletions
diff --git a/src/value.zig b/src/value.zig
index 5963b84b25..0277f0bdb2 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -718,18 +718,25 @@ pub const Value = struct {
},
else => unreachable,
};
- const enum_type = ip.indexToKey(ty.ip_index).enum_type;
- if (enum_type.values.len != 0) {
- return enum_type.values[field_index].toValue();
- } else {
- // Field index and integer values are the same.
- return mod.intValue(enum_type.tag_ty.toType(), field_index);
- }
+ return switch (ip.indexToKey(ty.ip_index)) {
+ // Assume it is already an integer and return it directly.
+ .simple_type, .int_type => val,
+ .enum_type => |enum_type| if (enum_type.values.len != 0)
+ enum_type.values[field_index].toValue()
+ else // Field index and integer values are the same.
+ mod.intValue(enum_type.tag_ty.toType(), field_index),
+ else => unreachable,
+ };
},
- else => {
- const enum_type = ip.indexToKey(ip.typeOf(val.ip_index)).enum_type;
- const int = try ip.getCoerced(mod.gpa, val.ip_index, enum_type.tag_ty);
- return int.toValue();
+ else => return switch (ip.indexToKey(ip.typeOf(val.ip_index))) {
+ // Assume it is already an integer and return it directly.
+ .simple_type, .int_type => val,
+ .enum_type => |enum_type| (try ip.getCoerced(
+ mod.gpa,
+ val.ip_index,
+ enum_type.tag_ty,
+ )).toValue(),
+ else => unreachable,
},
}
}
@@ -2906,7 +2913,7 @@ pub const Value = struct {
inline .u64, .i64 => |x| x == 0,
},
.opt => |opt| opt.val == .none,
- else => unreachable,
+ else => false,
},
};
}
@@ -2949,11 +2956,20 @@ pub const Value = struct {
/// Value of the optional, null if optional has no payload.
pub fn optionalValue(val: Value, mod: *const Module) ?Value {
- if (val.isNull(mod)) return null;
-
- // Valid for optional representation to be the direct value
- // and not use opt_payload.
- return if (val.castTag(.opt_payload)) |p| p.data else val;
+ return switch (val.ip_index) {
+ .none => if (val.isNull(mod)) null
+ // Valid for optional representation to be the direct value
+ // and not use opt_payload.
+ else if (val.castTag(.opt_payload)) |p| p.data else val,
+ .null_value => null,
+ else => switch (mod.intern_pool.indexToKey(val.ip_index)) {
+ .opt => |opt| switch (opt.val) {
+ .none => null,
+ else => opt.val.toValue(),
+ },
+ else => unreachable,
+ },
+ };
}
/// Valid for all types. Asserts the value is not undefined.