aboutsummaryrefslogtreecommitdiff
path: root/src/type.zig
diff options
context:
space:
mode:
authorMeghan Denny <hello@nektro.net>2021-10-16 22:49:51 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-10-17 21:42:22 -0700
commit0ef2e2520af59a42014399282ddea983fa526449 (patch)
treeb186116f3064299d58a81cacb2f68ae492d26d65 /src/type.zig
parent7d0a74456b0f239e6cc8532143badeb6781bd47d (diff)
downloadzig-0ef2e2520af59a42014399282ddea983fa526449.tar.gz
zig-0ef2e2520af59a42014399282ddea983fa526449.zip
stage2: implement `@hasField`
struct and union are kept in stage1 because struct/unionFieldCount are returning zero
Diffstat (limited to 'src/type.zig')
-rw-r--r--src/type.zig72
1 files changed, 12 insertions, 60 deletions
diff --git a/src/type.zig b/src/type.zig
index f7cd00dd06..324901faf0 100644
--- a/src/type.zig
+++ b/src/type.zig
@@ -3252,14 +3252,11 @@ pub const Type = extern union {
};
}
- pub fn enumFieldCount(ty: Type) usize {
- switch (ty.tag()) {
- .enum_full, .enum_nonexhaustive => {
- const enum_full = ty.cast(Payload.EnumFull).?.data;
- return enum_full.fields.count();
- },
- .enum_simple => return ty.castTag(.enum_simple).?.data.fields.count(),
- .enum_numbered => return ty.castTag(.enum_numbered).?.data.fields.count(),
+ pub fn enumFields(ty: Type) Module.EnumFull.NameMap {
+ return switch (ty.tag()) {
+ .enum_full, .enum_nonexhaustive => ty.cast(Payload.EnumFull).?.data.fields,
+ .enum_simple => ty.castTag(.enum_simple).?.data.fields,
+ .enum_numbered => ty.castTag(.enum_numbered).?.data.fields,
.atomic_order,
.atomic_rmw_op,
.calling_convention,
@@ -3270,65 +3267,20 @@ pub const Type = extern union {
.export_options,
.extern_options,
=> @panic("TODO resolve std.builtin types"),
-
else => unreachable,
- }
+ };
+ }
+
+ pub fn enumFieldCount(ty: Type) usize {
+ return ty.enumFields().count();
}
pub fn enumFieldName(ty: Type, field_index: usize) []const u8 {
- switch (ty.tag()) {
- .enum_full, .enum_nonexhaustive => {
- const enum_full = ty.cast(Payload.EnumFull).?.data;
- return enum_full.fields.keys()[field_index];
- },
- .enum_simple => {
- const enum_simple = ty.castTag(.enum_simple).?.data;
- return enum_simple.fields.keys()[field_index];
- },
- .enum_numbered => {
- const enum_numbered = ty.castTag(.enum_numbered).?.data;
- return enum_numbered.fields.keys()[field_index];
- },
- .atomic_order,
- .atomic_rmw_op,
- .calling_convention,
- .address_space,
- .float_mode,
- .reduce_op,
- .call_options,
- .export_options,
- .extern_options,
- => @panic("TODO resolve std.builtin types"),
- else => unreachable,
- }
+ return ty.enumFields().keys()[field_index];
}
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);
- },
- .enum_numbered => {
- const enum_numbered = ty.castTag(.enum_numbered).?.data;
- return enum_numbered.fields.getIndex(field_name);
- },
- .atomic_order,
- .atomic_rmw_op,
- .calling_convention,
- .address_space,
- .float_mode,
- .reduce_op,
- .call_options,
- .export_options,
- .extern_options,
- => @panic("TODO resolve std.builtin types"),
- else => unreachable,
- }
+ return ty.enumFields().getIndex(field_name);
}
/// Asserts `ty` is an enum. `enum_tag` can either be `enum_field_index` or