authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-03 00:04:12-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-03 00:04:12-04:00
log95636c7e5ff6ad0eeb768a2a0a1d7533b5872e20
treebface1d6e36b3830f0229efcaf73d20bc3480f05
parent92f747435930bc4d54114e414b372c7eafe7cc02
signaturelock-open Commit is signed but in an unrecognized format.

ability to @ptrCast to *void

fixes #960

3 files changed, 17 insertions(+), 2 deletions(-)

src/codegen.cpp+3
......@@ -2655,6 +2655,9 @@ static LLVMValueRef ir_render_ptr_cast(CodeGen *g, IrExecutable *executable,
26552655 IrInstructionPtrCast *instruction)
26562656{
26572657 TypeTableEntry *wanted_type = instruction->base.value.type;
2658 if (!type_has_bits(wanted_type)) {
2659 return nullptr;
2660 }
26582661 LLVMValueRef ptr = ir_llvm_value(g, instruction->ptr);
26592662 return LLVMBuildBitCast(g->builder, ptr, wanted_type->type_ref, "");
26602663}
src/ir.cpp+8-2
......@@ -19757,6 +19757,8 @@ static IrInstruction *ir_align_cast(IrAnalyze *ira, IrInstruction *target, uint3
1975719757}
1975819758
1975919759static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstructionPtrCast *instruction) {
19760 Error err;
19761
1976019762 IrInstruction *dest_type_value = instruction->dest_type->other;
1976119763 TypeTableEntry *dest_type = ir_resolve_type(ira, dest_type_value);
1976219764 if (type_is_invalid(dest_type))
......@@ -19810,9 +19812,13 @@ static TypeTableEntry *ir_analyze_instruction_ptr_cast(IrAnalyze *ira, IrInstruc
1981019812 instruction->base.source_node, nullptr, ptr);
1981119813 casted_ptr->value.type = dest_type;
1981219814
19813 // keep the bigger alignment, it can only help
19815 // Keep the bigger alignment, it can only help-
19816 // unless the target is zero bits.
19817 if ((err = type_ensure_zero_bits_known(ira->codegen, dest_type)))
19818 return ira->codegen->builtin_types.entry_invalid;
19819
1981419820 IrInstruction *result;
19815 if (src_align_bytes > dest_align_bytes) {
19821 if (src_align_bytes > dest_align_bytes && type_has_bits(dest_type)) {
1981619822 result = ir_align_cast(ira, casted_ptr, src_align_bytes, false);
1981719823 if (type_is_invalid(result->value.type))
1981819824 return ira->codegen->builtin_types.entry_invalid;
test/cases/cast.zig+6
......@@ -519,3 +519,9 @@ fn incrementVoidPtrArray(array: ?*c_void, len: usize) void {
519519 @ptrCast([*]u8, array.?)[n] += 1;
520520 }
521521}
522
523test "*usize to *void" {
524 var i = usize(0);
525 var v = @ptrCast(*void, &i);
526 v.* = {};
527}