aboutsummaryrefslogtreecommitdiff
path: root/src/value.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-04-12 16:44:51 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-04-15 19:06:39 -0700
commitbcfebb4b2b17dcac445fc5dedbbd259cc8c2f306 (patch)
tree48950201c23c8ccde48e2e31c91464c7e96e6163 /src/value.zig
parent429cd2b5dd27bec15a4a3351114ce1bcd12d8d01 (diff)
downloadzig-bcfebb4b2b17dcac445fc5dedbbd259cc8c2f306.tar.gz
zig-bcfebb4b2b17dcac445fc5dedbbd259cc8c2f306.zip
stage2: improvements aimed at std lib integration
* AstGen: emit decl lookup ZIR instructions rather than directly looking up decls in AstGen. This is necessary because we want to reuse the same immutable ZIR code for multiple generic instantiations (and comptime function calls). * AstGen: fix using members_len instead of fields_len for struct decls. * structs: the struct_decl ZIR instruction is now also a block. This is so that the type expressions, default field value expressions, and alignment expressions can be evaluated in a scope that contains the decls from the struct namespace itself. * Add "std" and "builtin" packages to the builtin package. * Don't try to build glibc, musl, or mingw-w64 when using `-ofmt=c`. * builtin.zig is generated without `usingnamespace`. * builtin.zig takes advantage of `std.zig.fmtId` for CPU features. * A first pass at implementing `usingnamespace`. It's problematic and should either be deleted, or polished, before merging this branch. * Sema: allow explicitly specifying the namespace in which to look up Decls. This is used by `struct_decl` in order to put the decls from the struct namespace itself in scope when evaluating the type expressions, default value expressions, and alignment expressions. * Module: fix `analyzeNamespace` assuming that it is the top-level root declaration node. * Sema: implement comptime and runtime cmp operator. * Sema: implement peer type resolution for enums and enum literals. * Pull in the changes from master branch: 262e09c482d98a78531c049a18b7f24146fe157f. * ZIR: complete out simple_ptr_type debug printing
Diffstat (limited to 'src/value.zig')
-rw-r--r--src/value.zig27
1 files changed, 19 insertions, 8 deletions
diff --git a/src/value.zig b/src/value.zig
index 66a23692c1..a9aec47272 100644
--- a/src/value.zig
+++ b/src/value.zig
@@ -930,7 +930,11 @@ pub const Value = extern union {
/// Asserts the value is comparable.
pub fn compare(lhs: Value, op: std.math.CompareOperator, rhs: Value) bool {
- return order(lhs, rhs).compare(op);
+ return switch (op) {
+ .eq => lhs.eql(rhs),
+ .neq => !lhs.eql(rhs),
+ else => order(lhs, rhs).compare(op),
+ };
}
/// Asserts the value is comparable.
@@ -942,12 +946,19 @@ pub const Value = extern union {
const a_tag = a.tag();
const b_tag = b.tag();
if (a_tag == b_tag) {
- if (a_tag == .void_value or a_tag == .null_value) {
- return true;
- } else if (a_tag == .enum_literal) {
- const a_name = a.castTag(.enum_literal).?.data;
- const b_name = b.castTag(.enum_literal).?.data;
- return std.mem.eql(u8, a_name, b_name);
+ switch (a_tag) {
+ .void_value, .null_value => return true,
+ .enum_literal => {
+ const a_name = a.castTag(.enum_literal).?.data;
+ const b_name = b.castTag(.enum_literal).?.data;
+ return std.mem.eql(u8, a_name, b_name);
+ },
+ .enum_field_index => {
+ const a_field_index = a.castTag(.enum_field_index).?.data;
+ const b_field_index = b.castTag(.enum_field_index).?.data;
+ return a_field_index == b_field_index;
+ },
+ else => {},
}
}
if (a.isType() and b.isType()) {
@@ -958,7 +969,7 @@ pub const Value = extern union {
const b_type = b.toType(&fib.allocator) catch unreachable;
return a_type.eql(b_type);
}
- return compare(a, .eq, b);
+ return order(a, b).compare(.eq);
}
pub fn hash_u32(self: Value) u32 {