enum Value [src]
Fields
none = std.math.maxInt(u31)
false = first_constant + @intFromEnum(Constant.false)
true = first_constant + @intFromEnum(Constant.true)
@"0" = first_constant + @intFromEnum(Constant.@"0")
@"1" = first_constant + @intFromEnum(Constant.@"1")
_
Members
Source
pub const Value = enum(u32) {
none = std.math.maxInt(u31),
false = first_constant + @intFromEnum(Constant.false),
true = first_constant + @intFromEnum(Constant.true),
@"0" = first_constant + @intFromEnum(Constant.@"0"),
@"1" = first_constant + @intFromEnum(Constant.@"1"),
_,
const first_constant = 1 << 30;
const first_metadata = 1 << 31;
pub fn unwrap(self: Value) union(enum) {
instruction: Function.Instruction.Index,
constant: Constant,
metadata: Metadata,
} {
return if (@intFromEnum(self) < first_constant)
.{ .instruction = @enumFromInt(@intFromEnum(self)) }
else if (@intFromEnum(self) < first_metadata)
.{ .constant = @enumFromInt(@intFromEnum(self) - first_constant) }
else
.{ .metadata = @enumFromInt(@intFromEnum(self) - first_metadata) };
}
pub fn typeOfWip(self: Value, wip: *const WipFunction) Type {
return switch (self.unwrap()) {
.instruction => |instruction| instruction.typeOfWip(wip),
.constant => |constant| constant.typeOf(wip.builder),
.metadata => .metadata,
};
}
pub fn typeOf(self: Value, function: Function.Index, builder: *Builder) Type {
return switch (self.unwrap()) {
.instruction => |instruction| instruction.typeOf(function, builder),
.constant => |constant| constant.typeOf(builder),
.metadata => .metadata,
};
}
pub fn toConst(self: Value) ?Constant {
return switch (self.unwrap()) {
.instruction, .metadata => null,
.constant => |constant| constant,
};
}
const FormatData = struct {
value: Value,
function: Function.Index,
builder: *Builder,
flags: FormatFlags,
};
fn format(data: FormatData, w: *Writer) Writer.Error!void {
switch (data.value.unwrap()) {
.instruction => |instruction| try Function.Instruction.Index.format(.{
.instruction = instruction,
.function = data.function,
.builder = data.builder,
.flags = data.flags,
}, w),
.constant => |constant| try Constant.format(.{
.constant = constant,
.builder = data.builder,
.flags = data.flags,
}, w),
.metadata => unreachable,
}
}
pub fn fmt(self: Value, function: Function.Index, builder: *Builder, flags: FormatFlags) std.fmt.Alt(FormatData, format) {
return .{ .data = .{ .value = self, .function = function, .builder = builder, .flags = flags } };
}
}