stage2 codegen: Implement genTypedValue for enums

This commit is contained in:
joachimschmidt557 2021-07-30 22:48:20 +02:00 committed by Andrew Kelley
parent 7aaea20e7e
commit 84039a57e4
2 changed files with 45 additions and 0 deletions

View File

@ -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" => {

View File

@ -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(