authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-01-06 14:00:34-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-01-06 14:00:34-05:00
log013c7b24d2a2c1f81d2fbcd7c2d07ed582b7acd9
tree8b9e743583fb1e6fa379d6ee84593ab94d19fe60
parent5f26d1dddbe9004429adca6e55d3344ca58062ae
parentda08525bb31c3608667254132b65e409f5869a03
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #1874 from ziglang/issue-1866

Fix #1866

2 files changed, 45 insertions(+), 13 deletions(-)

src/ir.cpp+29-13
...@@ -14577,8 +14577,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -14577,8 +14577,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
14577 return ira->codegen->invalid_instruction;14577 return ira->codegen->invalid_instruction;
14578 if (type_is_invalid(struct_val->type))14578 if (type_is_invalid(struct_val->type))
14579 return ira->codegen->invalid_instruction;14579 return ira->codegen->invalid_instruction;
14580 ConstExprValue *field_val = &struct_val->data.x_struct.fields[field->src_index];14580 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field->type_entry,
14581 ZigType *ptr_type = get_pointer_to_type_extra(ira->codegen, field_val->type,
14582 is_const, is_volatile, PtrLenSingle, align_bytes,14581 is_const, is_volatile, PtrLenSingle, align_bytes,
14583 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),14582 (uint32_t)(ptr_bit_offset + field->bit_offset_in_host),
14584 (uint32_t)host_int_bytes_for_result_type);14583 (uint32_t)host_int_bytes_for_result_type);
...@@ -16883,16 +16882,12 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind...@@ -16883,16 +16882,12 @@ static void ensure_field_index(ZigType *type, const char *field_name, size_t ind
1688316882
16884static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) {16883static ZigType *ir_type_info_get_type(IrAnalyze *ira, const char *type_name, ZigType *root) {
16885 Error err;16884 Error err;
16886 static ConstExprValue *type_info_var = nullptr; // TODO oops this global variable made it past code review16885 ConstExprValue *type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
16887 static ZigType *type_info_type = nullptr; // TODO oops this global variable made it past code review16886 assert(type_info_var->type->id == ZigTypeIdMetaType);
16888 if (type_info_var == nullptr) {16887 assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type));
16889 type_info_var = get_builtin_value(ira->codegen, "TypeInfo");
16890 assert(type_info_var->type->id == ZigTypeIdMetaType);
1689116888
16892 assertNoError(ensure_complete_type(ira->codegen, type_info_var->data.x_type));16889 ZigType *type_info_type = type_info_var->data.x_type;
16893 type_info_type = type_info_var->data.x_type;16890 assert(type_info_type->id == ZigTypeIdUnion);
16894 assert(type_info_type->id == ZigTypeIdUnion);
16895 }
1689616891
16897 if (type_name == nullptr && root == nullptr)16892 if (type_name == nullptr && root == nullptr)
16898 return type_info_type;16893 return type_info_type;
...@@ -20180,8 +20175,29 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t...@@ -20180,8 +20175,29 @@ static Error buf_read_value_bytes(IrAnalyze *ira, AstNode *source_node, uint8_t
20180 val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn);20175 val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn);
20181 return ErrorNone;20176 return ErrorNone;
20182 }20177 }
20183 case ZigTypeIdArray:20178 case ZigTypeIdArray: {
20184 zig_panic("TODO buf_read_value_bytes array type");20179 uint64_t elem_size = type_size(ira->codegen, val->type->data.array.child_type);
20180 size_t len = val->type->data.array.len;
20181
20182 switch (val->data.x_array.special) {
20183 case ConstArraySpecialNone:
20184 val->data.x_array.data.s_none.elements = create_const_vals(len);
20185 for (size_t i = 0; i < len; i++) {
20186 ConstExprValue *elem = &val->data.x_array.data.s_none.elements[i];
20187 elem->special = ConstValSpecialStatic;
20188 elem->type = val->type->data.array.child_type;
20189 if ((err = buf_read_value_bytes(ira, source_node, buf + (elem_size * i), elem)))
20190 return err;
20191 }
20192 break;
20193 case ConstArraySpecialUndef:
20194 zig_panic("TODO buf_read_value_bytes ConstArraySpecialUndef array type");
20195 case ConstArraySpecialBuf:
20196 zig_panic("TODO buf_read_value_bytes ConstArraySpecialBuf array type");
20197 }
20198
20199 return ErrorNone;
20200 }
20185 case ZigTypeIdStruct:20201 case ZigTypeIdStruct:
20186 switch (val->type->data.structure.layout) {20202 switch (val->type->data.structure.layout) {
20187 case ContainerLayoutAuto: {20203 case ContainerLayoutAuto: {
test/cases/ptrcast.zig+16
...@@ -34,3 +34,19 @@ fn testReinterpretBytesAsExternStruct() void {...@@ -34,3 +34,19 @@ fn testReinterpretBytesAsExternStruct() void {
34 var val = ptr.c;34 var val = ptr.c;
35 assertOrPanic(val == 5);35 assertOrPanic(val == 5);
36}36}
37
38test "reinterpret struct field at comptime" {
39 const numLittle = comptime Bytes.init(0x12345678);
40 assertOrPanic(std.mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes));
41}
42
43const Bytes = struct {
44 bytes: [4]u8,
45
46 pub fn init(v: u32) Bytes {
47 var res: Bytes = undefined;
48 @ptrCast(*align(1) u32, &res.bytes).* = v;
49
50 return res;
51 }
52};