mirror of
https://github.com/ziglang/zig.git
synced 2025-12-07 06:43:07 +00:00
std: have std.meta.fieldInfo take an enum rather than a string
This commit is contained in:
parent
73bf2e1525
commit
9c97a07f18
@ -487,19 +487,14 @@ test "std.meta.fields" {
|
|||||||
testing.expect(comptime uf[0].field_type == u8);
|
testing.expect(comptime uf[0].field_type == u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) {
|
pub fn fieldInfo(comptime T: type, comptime field: FieldEnum(T)) switch (@typeInfo(T)) {
|
||||||
.Struct => TypeInfo.StructField,
|
.Struct => TypeInfo.StructField,
|
||||||
.Union => TypeInfo.UnionField,
|
.Union => TypeInfo.UnionField,
|
||||||
.ErrorSet => TypeInfo.Error,
|
.ErrorSet => TypeInfo.Error,
|
||||||
.Enum => TypeInfo.EnumField,
|
.Enum => TypeInfo.EnumField,
|
||||||
else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
|
else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
|
||||||
} {
|
} {
|
||||||
inline for (comptime fields(T)) |field| {
|
return fields(T)[@enumToInt(field)];
|
||||||
if (comptime mem.eql(u8, field.name, field_name))
|
|
||||||
return field;
|
|
||||||
}
|
|
||||||
|
|
||||||
@compileError("'" ++ @typeName(T) ++ "' has no field '" ++ field_name ++ "'");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test "std.meta.fieldInfo" {
|
test "std.meta.fieldInfo" {
|
||||||
@ -514,10 +509,10 @@ test "std.meta.fieldInfo" {
|
|||||||
a: u8,
|
a: u8,
|
||||||
};
|
};
|
||||||
|
|
||||||
const e1f = comptime fieldInfo(E1, "A");
|
const e1f = fieldInfo(E1, .A);
|
||||||
const e2f = comptime fieldInfo(E2, "A");
|
const e2f = fieldInfo(E2, .A);
|
||||||
const sf = comptime fieldInfo(S1, "a");
|
const sf = fieldInfo(S1, .a);
|
||||||
const uf = comptime fieldInfo(U1, "a");
|
const uf = fieldInfo(U1, .a);
|
||||||
|
|
||||||
testing.expect(mem.eql(u8, e1f.name, "A"));
|
testing.expect(mem.eql(u8, e1f.name, "A"));
|
||||||
testing.expect(mem.eql(u8, e2f.name, "A"));
|
testing.expect(mem.eql(u8, e2f.name, "A"));
|
||||||
|
|||||||
@ -688,7 +688,7 @@ pub const Node = struct {
|
|||||||
|
|
||||||
/// Prefer `castTag` to this.
|
/// Prefer `castTag` to this.
|
||||||
pub fn cast(base: *Node, comptime T: type) ?*T {
|
pub fn cast(base: *Node, comptime T: type) ?*T {
|
||||||
if (std.meta.fieldInfo(T, "base").default_value) |default_base| {
|
if (std.meta.fieldInfo(T, .base).default_value) |default_base| {
|
||||||
return base.castTag(default_base.tag);
|
return base.castTag(default_base.tag);
|
||||||
}
|
}
|
||||||
inline for (@typeInfo(Tag).Enum.fields) |field| {
|
inline for (@typeInfo(Tag).Enum.fields) |field| {
|
||||||
|
|||||||
@ -758,7 +758,7 @@ fn ptrSliceType(mod: *Module, scope: *Scope, src: usize, ptr_info: *ast.PtrInfo,
|
|||||||
}, child_type);
|
}, child_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, "kw_args").field_type = .{};
|
var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, .kw_args).field_type = .{};
|
||||||
kw_args.size = size;
|
kw_args.size = size;
|
||||||
kw_args.@"allowzero" = ptr_info.allowzero_token != null;
|
kw_args.@"allowzero" = ptr_info.allowzero_token != null;
|
||||||
if (ptr_info.align_info) |some| {
|
if (ptr_info.align_info) |some| {
|
||||||
@ -2756,8 +2756,8 @@ pub fn addZIRInstSpecial(
|
|||||||
scope: *Scope,
|
scope: *Scope,
|
||||||
src: usize,
|
src: usize,
|
||||||
comptime T: type,
|
comptime T: type,
|
||||||
positionals: std.meta.fieldInfo(T, "positionals").field_type,
|
positionals: std.meta.fieldInfo(T, .positionals).field_type,
|
||||||
kw_args: std.meta.fieldInfo(T, "kw_args").field_type,
|
kw_args: std.meta.fieldInfo(T, .kw_args).field_type,
|
||||||
) !*T {
|
) !*T {
|
||||||
const gen_zir = scope.getGenZIR();
|
const gen_zir = scope.getGenZIR();
|
||||||
try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
|
try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1);
|
||||||
@ -2874,8 +2874,8 @@ pub fn addZIRInst(
|
|||||||
scope: *Scope,
|
scope: *Scope,
|
||||||
src: usize,
|
src: usize,
|
||||||
comptime T: type,
|
comptime T: type,
|
||||||
positionals: std.meta.fieldInfo(T, "positionals").field_type,
|
positionals: std.meta.fieldInfo(T, .positionals).field_type,
|
||||||
kw_args: std.meta.fieldInfo(T, "kw_args").field_type,
|
kw_args: std.meta.fieldInfo(T, .kw_args).field_type,
|
||||||
) !*zir.Inst {
|
) !*zir.Inst {
|
||||||
const inst_special = try addZIRInstSpecial(mod, scope, src, T, positionals, kw_args);
|
const inst_special = try addZIRInstSpecial(mod, scope, src, T, positionals, kw_args);
|
||||||
return &inst_special.base;
|
return &inst_special.base;
|
||||||
@ -2883,12 +2883,12 @@ pub fn addZIRInst(
|
|||||||
|
|
||||||
/// TODO The existence of this function is a workaround for a bug in stage1.
|
/// TODO The existence of this function is a workaround for a bug in stage1.
|
||||||
pub fn addZIRInstConst(mod: *Module, scope: *Scope, src: usize, typed_value: TypedValue) !*zir.Inst {
|
pub fn addZIRInstConst(mod: *Module, scope: *Scope, src: usize, typed_value: TypedValue) !*zir.Inst {
|
||||||
const P = std.meta.fieldInfo(zir.Inst.Const, "positionals").field_type;
|
const P = std.meta.fieldInfo(zir.Inst.Const, .positionals).field_type;
|
||||||
return addZIRInst(mod, scope, src, zir.Inst.Const, P{ .typed_value = typed_value }, .{});
|
return addZIRInst(mod, scope, src, zir.Inst.Const, P{ .typed_value = typed_value }, .{});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO The existence of this function is a workaround for a bug in stage1.
|
/// TODO The existence of this function is a workaround for a bug in stage1.
|
||||||
pub fn addZIRInstLoop(mod: *Module, scope: *Scope, src: usize, body: zir.Module.Body) !*zir.Inst.Loop {
|
pub fn addZIRInstLoop(mod: *Module, scope: *Scope, src: usize, body: zir.Module.Body) !*zir.Inst.Loop {
|
||||||
const P = std.meta.fieldInfo(zir.Inst.Loop, "positionals").field_type;
|
const P = std.meta.fieldInfo(zir.Inst.Loop, .positionals).field_type;
|
||||||
return addZIRInstSpecial(mod, scope, src, zir.Inst.Loop, P{ .body = body }, .{});
|
return addZIRInstSpecial(mod, scope, src, zir.Inst.Loop, P{ .body = body }, .{});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -189,7 +189,7 @@ pub const Inst = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn Args(comptime T: type) type {
|
pub fn Args(comptime T: type) type {
|
||||||
return std.meta.fieldInfo(T, "args").field_type;
|
return std.meta.fieldInfo(T, .args).field_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `null` if runtime-known.
|
/// Returns `null` if runtime-known.
|
||||||
|
|||||||
@ -3232,7 +3232,7 @@ pub const Type = extern union {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn Data(comptime t: Tag) type {
|
pub fn Data(comptime t: Tag) type {
|
||||||
return std.meta.fieldInfo(t.Type(), "data").field_type;
|
return std.meta.fieldInfo(t.Type(), .data).field_type;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -207,7 +207,7 @@ pub const Value = extern union {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn Data(comptime t: Tag) type {
|
pub fn Data(comptime t: Tag) type {
|
||||||
return std.meta.fieldInfo(t.Type(), "data").field_type;
|
return std.meta.fieldInfo(t.Type(), .data).field_type;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user