diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-07 11:26:07 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-07 11:26:07 -0700 |
| commit | 4e8fb9e6a5f9a65bbf6469e386e83ba469e7543b (patch) | |
| tree | 804cac2f05b76ab8c76925b3104dd19ec0e4be44 /src/type.zig | |
| parent | 01a39fa1d4d0d2e3305af8e6d6af1079f020db7f (diff) | |
| download | zig-4e8fb9e6a5f9a65bbf6469e386e83ba469e7543b.tar.gz zig-4e8fb9e6a5f9a65bbf6469e386e83ba469e7543b.zip | |
Sema: DRY up enum field analysis and add "declared here" notes
Diffstat (limited to 'src/type.zig')
| -rw-r--r-- | src/type.zig | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/type.zig b/src/type.zig index 6173c0b800..06e84c245b 100644 --- a/src/type.zig +++ b/src/type.zig @@ -2090,6 +2090,42 @@ pub const Type = extern union { }; } + pub fn enumFieldIndex(ty: Type, field_name: []const u8) ?usize { + switch (ty.tag()) { + .enum_full, .enum_nonexhaustive => { + const enum_full = ty.cast(Payload.EnumFull).?.data; + return enum_full.fields.getIndex(field_name); + }, + .enum_simple => { + const enum_simple = ty.castTag(.enum_simple).?.data; + return enum_simple.fields.getIndex(field_name); + }, + else => unreachable, + } + } + + pub fn declSrcLoc(ty: Type) Module.SrcLoc { + switch (ty.tag()) { + .enum_full, .enum_nonexhaustive => { + const enum_full = ty.cast(Payload.EnumFull).?.data; + return enum_full.srcLoc(); + }, + .enum_simple => { + const enum_simple = ty.castTag(.enum_simple).?.data; + return enum_simple.srcLoc(); + }, + .@"struct" => { + const struct_obj = ty.castTag(.@"struct").?.data; + return struct_obj.srcLoc(); + }, + .error_set => { + const error_set = ty.castTag(.error_set).?.data; + return error_set.srcLoc(); + }, + else => unreachable, + } + } + /// Asserts the type is an enum. pub fn enumHasInt(ty: Type, int: Value, target: Target) bool { const S = struct { |
