From cb616cb7972bb2f38ea4527c7ec0ae3cc0d64c7c Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 5 Oct 2021 21:38:47 -0700 Subject: stage2: implement runtime `@intToEnum` * Update AIR instruction `intcast` to allow the dest type to be an enum. * LLVM backend: update `intcast` to support when the bit counts of operand and dest type are the same. This was already a requirement of the instruction previously. * Type: `intInfo` supports the case when the type is an enum, and retrieves the info for the integer tag type. This makes it pretty easy for backends to implement `intcast` without having to care explicitly that the new type is an enum. As a bonus, simple enums never have to go through the type system; their signedness and bit count are computed directly. The "int to enum" behavior test case is now passing for stage2 in the LLVM backend. --- src/Sema.zig | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index 137d2ca2fd..993cde739e 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -4312,7 +4312,8 @@ fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A } try sema.requireRuntimeBlock(block, src); - return block.addTyOp(.bitcast, dest_ty, operand); + // TODO insert safety check to make sure the value matches an enum value + return block.addTyOp(.intcast, dest_ty, operand); } /// Pointer in, pointer out. @@ -5050,6 +5051,7 @@ fn zirIntCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air } try sema.requireRuntimeBlock(block, operand_src); + // TODO insert safety check to make sure the value fits in the dest type return block.addTyOp(.intcast, dest_type, operand); } -- cgit v1.2.3