mirror of
https://github.com/ziglang/zig.git
synced 2024-12-12 22:25:21 +00:00
stage2 codegen: Implement genTypedValue for enums
This commit is contained in:
parent
7aaea20e7e
commit
84039a57e4
@ -4740,6 +4740,29 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
|
||||
}
|
||||
return self.fail("TODO non pointer optionals", .{});
|
||||
},
|
||||
.Enum => {
|
||||
if (typed_value.val.castTag(.enum_field_index)) |field_index| {
|
||||
switch (typed_value.ty.tag()) {
|
||||
.enum_simple => {
|
||||
return MCValue{ .immediate = field_index.data };
|
||||
},
|
||||
.enum_full, .enum_nonexhaustive => {
|
||||
const enum_full = typed_value.ty.cast(Type.Payload.EnumFull).?.data;
|
||||
if (enum_full.values.count() != 0) {
|
||||
const tag_val = enum_full.values.keys()[field_index.data];
|
||||
return self.genTypedValue(.{ .ty = enum_full.tag_ty, .val = tag_val });
|
||||
} else {
|
||||
return MCValue{ .immediate = field_index.data };
|
||||
}
|
||||
},
|
||||
else => unreachable,
|
||||
}
|
||||
} else {
|
||||
var int_tag_buffer: Type.Payload.Bits = undefined;
|
||||
const int_tag_ty = typed_value.ty.intTagType(&int_tag_buffer);
|
||||
return self.genTypedValue(.{ .ty = int_tag_ty, .val = typed_value.val });
|
||||
}
|
||||
},
|
||||
.ErrorSet => {
|
||||
switch (typed_value.val.tag()) {
|
||||
.@"error" => {
|
||||
|
@ -299,6 +299,28 @@ pub fn addCases(ctx: *TestContext) !void {
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
var case = ctx.exe("enums", linux_arm);
|
||||
case.addCompareOutput(
|
||||
\\const Number = enum { one, two, three };
|
||||
\\
|
||||
\\pub fn main() void {
|
||||
\\ var x: Number = .one;
|
||||
\\ var y = Number.two;
|
||||
\\ var z = @intToEnum(Number, 2);
|
||||
\\ assert(@enumToInt(x) == 0);
|
||||
\\ assert(@enumToInt(y) == 1);
|
||||
\\ assert(@enumToInt(z) == 2);
|
||||
\\}
|
||||
\\
|
||||
\\fn assert(ok: bool) void {
|
||||
\\ if (!ok) unreachable; // assertion failure
|
||||
\\}
|
||||
,
|
||||
"",
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
var case = ctx.exe("recursive fibonacci", linux_arm);
|
||||
case.addCompareOutput(
|
||||
|
Loading…
Reference in New Issue
Block a user