diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2019-05-08 18:36:41 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-05-08 18:47:14 -0400 |
| commit | a4aee8b24d321aa4c7a791d20500e5847a08b587 (patch) | |
| tree | b628bc75fc9e41eecff4e5242e0b2367f4d3aa33 /src/ir.cpp | |
| parent | 0099583bd3eccbecbb827edbde46a40cf821fecf (diff) | |
| download | zig-a4aee8b24d321aa4c7a791d20500e5847a08b587.tar.gz zig-a4aee8b24d321aa4c7a791d20500e5847a08b587.zip | |
C pointers support if and orelse
See #1967
Diffstat (limited to 'src/ir.cpp')
| -rw-r--r-- | src/ir.cpp | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/src/ir.cpp b/src/ir.cpp index 90e8f1ed8f..1cfdb4544d 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -16848,11 +16848,30 @@ static IrInstruction *ir_analyze_instruction_size_of(IrAnalyze *ira, static IrInstruction *ir_analyze_test_non_null(IrAnalyze *ira, IrInstruction *source_inst, IrInstruction *value) { ZigType *type_entry = value->value.type; - if (type_entry->id == ZigTypeIdOptional) { + if (type_entry->id == ZigTypeIdPointer && type_entry->data.pointer.allow_zero) { if (instr_is_comptime(value)) { - ConstExprValue *maybe_val = ir_resolve_const(ira, value, UndefBad); - if (!maybe_val) + ConstExprValue *c_ptr_val = ir_resolve_const(ira, value, UndefOk); + if (c_ptr_val == nullptr) + return ira->codegen->invalid_instruction; + if (c_ptr_val->special == ConstValSpecialUndef) + return ir_const_undef(ira, source_inst, ira->codegen->builtin_types.entry_bool); + bool is_null = c_ptr_val->data.x_ptr.special == ConstPtrSpecialNull || + (c_ptr_val->data.x_ptr.special == ConstPtrSpecialHardCodedAddr && + c_ptr_val->data.x_ptr.data.hard_coded_addr.addr == 0); + return ir_const_bool(ira, source_inst, !is_null); + } + + IrInstruction *result = ir_build_test_nonnull(&ira->new_irb, + source_inst->scope, source_inst->source_node, value); + result->value.type = ira->codegen->builtin_types.entry_bool; + return result; + } else if (type_entry->id == ZigTypeIdOptional) { + if (instr_is_comptime(value)) { + ConstExprValue *maybe_val = ir_resolve_const(ira, value, UndefOk); + if (maybe_val == nullptr) return ira->codegen->invalid_instruction; + if (maybe_val->special == ConstValSpecialUndef) + return ir_const_undef(ira, source_inst, ira->codegen->builtin_types.entry_bool); return ir_const_bool(ira, source_inst, !optional_value_is_null(maybe_val)); } |
