authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-18 15:00:14-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-18 15:00:14-04:00
log8c77c5705f41e55a5f2d80db20270d0c644338ed
treed3316a485f7e86d99127439f6e7487a6f6df550c
parent5fd3af9dc6ef08f23e6b64dfa6d6776371f9344a
signaturelock-open Commit is signed but in an unrecognized format.

implementation for bitcasting extern enum type to c_int

closes #1036

2 files changed, 22 insertions(+), 2 deletions(-)

src/ir.cpp+5-2
......@@ -20254,6 +20254,11 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2025420254 bigint_write_twos_complement(&val->data.x_bigint, buf, val->type->data.integral.bit_count,
2025520255 codegen->is_big_endian);
2025620256 return;
20257 case ZigTypeIdEnum:
20258 bigint_write_twos_complement(&val->data.x_enum_tag, buf,
20259 val->type->data.enumeration.tag_int_type->data.integral.bit_count,
20260 codegen->is_big_endian);
20261 return;
2025720262 case ZigTypeIdFloat:
2025820263 float_write_ieee597(val, buf, codegen->is_big_endian);
2025920264 return;
......@@ -20285,8 +20290,6 @@ static void buf_write_value_bytes(CodeGen *codegen, uint8_t *buf, ConstExprValue
2028520290 zig_panic("TODO buf_write_value_bytes error union");
2028620291 case ZigTypeIdErrorSet:
2028720292 zig_panic("TODO buf_write_value_bytes pure error type");
20288 case ZigTypeIdEnum:
20289 zig_panic("TODO buf_write_value_bytes enum type");
2029020293 case ZigTypeIdFn:
2029120294 zig_panic("TODO buf_write_value_bytes fn type");
2029220295 case ZigTypeIdUnion:
test/cases/bitcast.zig+17
......@@ -16,3 +16,20 @@ fn conv(x: i32) u32 {
1616fn conv2(x: u32) i32 {
1717 return @bitCast(i32, x);
1818}
19
20test "@bitCast extern enum to its integer type" {
21 const SOCK = extern enum {
22 A,
23 B,
24
25 fn testBitCastExternEnum() void {
26 var SOCK_DGRAM = @This().B;
27 var sock_dgram = @bitCast(c_int, SOCK_DGRAM);
28 assert(sock_dgram == 1);
29 }
30 };
31
32 SOCK.testBitCastExternEnum();
33 comptime SOCK.testBitCastExternEnum();
34}
35