authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-10 18:02:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-10 18:02:59-04:00
logfec4555476e38d2eeb1dfb02572404b243acd0b2
treefde4a0b91d29679162750a7d76b5b15b467ebd24
parent918dbd4551f6c737d1c4b7416ef40b59c902eac8
signaturelock-open Commit is signed but in an unrecognized format.

fix inconsistent type information of optional C pointers

solves an assertion failure in LLVM

3 files changed, 9 insertions(+), 14 deletions(-)

src/analyze.cpp+3-7
......@@ -601,7 +601,7 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
601601 if (child_type->zero_bits) {
602602 entry->type_ref = LLVMInt1Type();
603603 entry->di_type = g->builtin_types.entry_bool->di_type;
604 } else if (type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
604 } else if (type_is_nonnull_ptr(child_type) || child_type->id == ZigTypeIdErrorSet) {
605605 assert(child_type->di_type);
606606 // this is an optimization but also is necessary for calling C
607607 // functions where all pointers are maybe pointers
......@@ -4145,11 +4145,7 @@ ZigType *get_codegen_ptr_type(ZigType *type) {
41454145}
41464146
41474147bool type_is_nonnull_ptr(ZigType *type) {
4148 return type_is_non_optional_pointer(type) && !ptr_allows_addr_zero(type);
4149}
4150
4151bool type_is_non_optional_pointer(ZigType *type) {
4152 return get_codegen_ptr_type(type) == type;
4148 return get_codegen_ptr_type(type) == type && !ptr_allows_addr_zero(type);
41534149}
41544150
41554151uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
......@@ -4667,7 +4663,7 @@ bool handle_is_ptr(ZigType *type_entry) {
46674663 return type_has_bits(type_entry->data.error_union.payload_type);
46684664 case ZigTypeIdOptional:
46694665 return type_has_bits(type_entry->data.maybe.child_type) &&
4670 !type_is_non_optional_pointer(type_entry->data.maybe.child_type) &&
4666 !type_is_nonnull_ptr(type_entry->data.maybe.child_type) &&
46714667 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;
46724668 case ZigTypeIdUnion:
46734669 assert(type_entry->data.unionation.zero_bits_known);
src/analyze.hpp-1
......@@ -60,7 +60,6 @@ ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **c
6060Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
6161Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
6262void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
63bool type_is_non_optional_pointer(ZigType *type);
6463
6564ZigType *get_src_ptr_type(ZigType *type);
6665ZigType *get_codegen_ptr_type(ZigType *type);
src/codegen.cpp+6-6
......@@ -3948,10 +3948,10 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
39483948static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueRef maybe_handle) {
39493949 assert(maybe_type->id == ZigTypeIdOptional);
39503950 ZigType *child_type = maybe_type->data.maybe.child_type;
3951 if (child_type->zero_bits) {
3951 if (!type_has_bits(child_type)) {
39523952 return maybe_handle;
39533953 } else {
3954 bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
3954 bool is_scalar = !handle_is_ptr(maybe_type);
39553955 if (is_scalar) {
39563956 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
39573957 } else {
......@@ -3991,7 +3991,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
39913991 if (child_type->zero_bits) {
39923992 return nullptr;
39933993 } else {
3994 bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
3994 bool is_scalar = !handle_is_ptr(maybe_type);
39953995 if (is_scalar) {
39963996 return maybe_ptr;
39973997 } else {
......@@ -4854,7 +4854,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
48544854 }
48554855
48564856 LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
4857 if (type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
4857 if (!handle_is_ptr(wanted_type)) {
48584858 return payload_val;
48594859 }
48604860
......@@ -8690,10 +8690,10 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
86908690 case ZigTypeIdOptional:
86918691 {
86928692 ZigType *child_type = type_entry->data.maybe.child_type;
8693 if (child_type->zero_bits) {
8693 if (!type_has_bits(child_type)) {
86948694 buf_init_from_str(out_buf, "bool");
86958695 return;
8696 } else if (type_is_non_optional_pointer(child_type)) {
8696 } else if (type_is_nonnull_ptr(child_type)) {
86978697 return get_c_type(g, gen_h, child_type, out_buf);
86988698 } else {
86998699 zig_unreachable();