From 18119aae30660c27b088214319cfca396fdf04bf Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 7 Apr 2021 12:15:05 -0700 Subject: Sema: implement comparison analysis for non-numeric types --- src/type.zig | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/type.zig') diff --git a/src/type.zig b/src/type.zig index 06e84c245b..dfd021617e 100644 --- a/src/type.zig +++ b/src/type.zig @@ -107,6 +107,42 @@ pub const Type = extern union { } } + pub fn isSelfComparable(ty: Type, is_equality_cmp: bool) bool { + return switch (ty.zigTypeTag()) { + .Int, + .Float, + .ComptimeFloat, + .ComptimeInt, + .Vector, // TODO some vectors require is_equality_cmp==true + => true, + + .Bool, + .Type, + .Void, + .ErrorSet, + .Fn, + .BoundFn, + .Opaque, + .AnyFrame, + .Enum, + .EnumLiteral, + => is_equality_cmp, + + .NoReturn, + .Array, + .Struct, + .Undefined, + .Null, + .ErrorUnion, + .Union, + .Frame, + => false, + + .Pointer => is_equality_cmp or ty.isCPtr(), + .Optional => is_equality_cmp and ty.isAbiPtr(), + }; + } + pub fn initTag(comptime small_tag: Tag) Type { comptime assert(@enumToInt(small_tag) < Tag.no_payload_count); return .{ .tag_if_small_enough = @enumToInt(small_tag) }; @@ -1583,6 +1619,11 @@ pub const Type = extern union { } } + /// Returns whether the type is represented as a pointer in the ABI. + pub fn isAbiPtr(self: Type) bool { + @panic("TODO implement this"); + } + /// Asserts that the type is an error union. pub fn errorUnionChild(self: Type) Type { return switch (self.tag()) { -- cgit v1.2.3