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) {...@@ -601,7 +601,7 @@ ZigType *get_optional_type(CodeGen *g, ZigType *child_type) {
601 if (child_type->zero_bits) {601 if (child_type->zero_bits) {
602 entry->type_ref = LLVMInt1Type();602 entry->type_ref = LLVMInt1Type();
603 entry->di_type = g->builtin_types.entry_bool->di_type;603 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) {
605 assert(child_type->di_type);605 assert(child_type->di_type);
606 // this is an optimization but also is necessary for calling C606 // this is an optimization but also is necessary for calling C
607 // functions where all pointers are maybe pointers607 // functions where all pointers are maybe pointers
...@@ -4145,11 +4145,7 @@ ZigType *get_codegen_ptr_type(ZigType *type) {...@@ -4145,11 +4145,7 @@ ZigType *get_codegen_ptr_type(ZigType *type) {
4145}4145}
41464146
4147bool type_is_nonnull_ptr(ZigType *type) {4147bool type_is_nonnull_ptr(ZigType *type) {
4148 return type_is_non_optional_pointer(type) && !ptr_allows_addr_zero(type);4148 return get_codegen_ptr_type(type) == type && !ptr_allows_addr_zero(type);
4149}
4150
4151bool type_is_non_optional_pointer(ZigType *type) {
4152 return get_codegen_ptr_type(type) == type;
4153}4149}
41544150
4155uint32_t get_ptr_align(CodeGen *g, ZigType *type) {4151uint32_t get_ptr_align(CodeGen *g, ZigType *type) {
...@@ -4667,7 +4663,7 @@ bool handle_is_ptr(ZigType *type_entry) {...@@ -4667,7 +4663,7 @@ bool handle_is_ptr(ZigType *type_entry) {
4667 return type_has_bits(type_entry->data.error_union.payload_type);4663 return type_has_bits(type_entry->data.error_union.payload_type);
4668 case ZigTypeIdOptional:4664 case ZigTypeIdOptional:
4669 return type_has_bits(type_entry->data.maybe.child_type) &&4665 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) &&
4671 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;4667 type_entry->data.maybe.child_type->id != ZigTypeIdErrorSet;
4672 case ZigTypeIdUnion:4668 case ZigTypeIdUnion:
4673 assert(type_entry->data.unionation.zero_bits_known);4669 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...@@ -60,7 +60,6 @@ ZigVar *find_variable(CodeGen *g, Scope *orig_context, Buf *name, ScopeFnDef **c
60Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);60Tld *find_decl(CodeGen *g, Scope *scope, Buf *name);
61Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);61Tld *find_container_decl(CodeGen *g, ScopeDecls *decls_scope, Buf *name);
62void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);62void resolve_top_level_decl(CodeGen *g, Tld *tld, AstNode *source_node);
63bool type_is_non_optional_pointer(ZigType *type);
6463
65ZigType *get_src_ptr_type(ZigType *type);64ZigType *get_src_ptr_type(ZigType *type);
66ZigType *get_codegen_ptr_type(ZigType *type);65ZigType *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...@@ -3948,10 +3948,10 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
3948static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueRef maybe_handle) {3948static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueRef maybe_handle) {
3949 assert(maybe_type->id == ZigTypeIdOptional);3949 assert(maybe_type->id == ZigTypeIdOptional);
3950 ZigType *child_type = maybe_type->data.maybe.child_type;3950 ZigType *child_type = maybe_type->data.maybe.child_type;
3951 if (child_type->zero_bits) {3951 if (!type_has_bits(child_type)) {
3952 return maybe_handle;3952 return maybe_handle;
3953 } else {3953 } 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);
3955 if (is_scalar) {3955 if (is_scalar) {
3956 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");3956 return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
3957 } else {3957 } else {
...@@ -3991,7 +3991,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec...@@ -3991,7 +3991,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
3991 if (child_type->zero_bits) {3991 if (child_type->zero_bits) {
3992 return nullptr;3992 return nullptr;
3993 } else {3993 } 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);
3995 if (is_scalar) {3995 if (is_scalar) {
3996 return maybe_ptr;3996 return maybe_ptr;
3997 } else {3997 } else {
...@@ -4854,7 +4854,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I...@@ -4854,7 +4854,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
4854 }4854 }
48554855
4856 LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);4856 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)) {
4858 return payload_val;4858 return payload_val;
4859 }4859 }
48604860
...@@ -8690,10 +8690,10 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu...@@ -8690,10 +8690,10 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
8690 case ZigTypeIdOptional:8690 case ZigTypeIdOptional:
8691 {8691 {
8692 ZigType *child_type = type_entry->data.maybe.child_type;8692 ZigType *child_type = type_entry->data.maybe.child_type;
8693 if (child_type->zero_bits) {8693 if (!type_has_bits(child_type)) {
8694 buf_init_from_str(out_buf, "bool");8694 buf_init_from_str(out_buf, "bool");
8695 return;8695 return;
8696 } else if (type_is_non_optional_pointer(child_type)) {8696 } else if (type_is_nonnull_ptr(child_type)) {
8697 return get_c_type(g, gen_h, child_type, out_buf);8697 return get_c_type(g, gen_h, child_type, out_buf);
8698 } else {8698 } else {
8699 zig_unreachable();8699 zig_unreachable();