aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.cpp
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-03-10 11:21:41 -0500
committerAndrew Kelley <superjoe30@gmail.com>2017-03-10 11:21:41 -0500
commit434f017aeefc392bcf524732940e2f2b908222f3 (patch)
treeff7f935dbaca2093a88f4e1a1411c6699157952e /src/codegen.cpp
parentc78dc5043bb8f0fa3ff2fec876bcab4ee499972c (diff)
downloadzig-434f017aeefc392bcf524732940e2f2b908222f3.tar.gz
zig-434f017aeefc392bcf524732940e2f2b908222f3.zip
codegen nullable void the same way as bool
See #104
Diffstat (limited to 'src/codegen.cpp')
-rw-r--r--src/codegen.cpp41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp
index cefcf8df6f..28dd4f2198 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -1755,12 +1755,16 @@ static LLVMValueRef ir_render_asm(CodeGen *g, IrExecutable *executable, IrInstru
static LLVMValueRef gen_non_null_bit(CodeGen *g, TypeTableEntry *maybe_type, LLVMValueRef maybe_handle) {
assert(maybe_type->id == TypeTableEntryIdMaybe);
TypeTableEntry *child_type = maybe_type->data.maybe.child_type;
- bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn);
- if (maybe_is_ptr) {
- return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
+ if (child_type->zero_bits) {
+ return maybe_handle;
} else {
- LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");
- return LLVMBuildLoad(g->builder, maybe_field_ptr, "");
+ bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn);
+ if (maybe_is_ptr) {
+ return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
+ } else {
+ LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, maybe_handle, maybe_null_index, "");
+ return LLVMBuildLoad(g->builder, maybe_field_ptr, "");
+ }
}
}
@@ -1779,7 +1783,6 @@ static LLVMValueRef ir_render_unwrap_maybe(CodeGen *g, IrExecutable *executable,
TypeTableEntry *maybe_type = ptr_type->data.pointer.child_type;
assert(maybe_type->id == TypeTableEntryIdMaybe);
TypeTableEntry *child_type = maybe_type->data.maybe.child_type;
- bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn);
LLVMValueRef maybe_ptr = ir_llvm_value(g, instruction->value);
LLVMValueRef maybe_handle = get_handle_value(g, maybe_ptr, maybe_type, is_volatile);
if (ir_want_debug_safety(g, &instruction->base) && instruction->safety_check_on) {
@@ -1793,11 +1796,16 @@ static LLVMValueRef ir_render_unwrap_maybe(CodeGen *g, IrExecutable *executable,
LLVMPositionBuilderAtEnd(g->builder, ok_block);
}
- if (maybe_is_ptr) {
- return maybe_ptr;
+ if (child_type->zero_bits) {
+ return nullptr;
} else {
- LLVMValueRef maybe_struct_ref = get_handle_value(g, maybe_ptr, maybe_type, is_volatile);
- return LLVMBuildStructGEP(g->builder, maybe_struct_ref, maybe_child_index, "");
+ bool maybe_is_ptr = (child_type->id == TypeTableEntryIdPointer || child_type->id == TypeTableEntryIdFn);
+ if (maybe_is_ptr) {
+ return maybe_ptr;
+ } else {
+ LLVMValueRef maybe_struct_ref = get_handle_value(g, maybe_ptr, maybe_type, is_volatile);
+ return LLVMBuildStructGEP(g->builder, maybe_struct_ref, maybe_child_index, "");
+ }
}
}
@@ -2319,6 +2327,10 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
TypeTableEntry *child_type = wanted_type->data.maybe.child_type;
+ if (child_type->zero_bits) {
+ return LLVMConstInt(LLVMInt1Type(), 1, false);
+ }
+
LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
if (child_type->id == TypeTableEntryIdPointer ||
child_type->id == TypeTableEntryIdFn)
@@ -2806,7 +2818,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
case TypeTableEntryIdMaybe:
{
TypeTableEntry *child_type = canon_type->data.maybe.child_type;
- if (child_type->id == TypeTableEntryIdPointer ||
+ if (child_type->zero_bits) {
+ return LLVMConstInt(LLVMInt1Type(), const_val->data.x_maybe ? 1 : 0, false);
+ } else if (child_type->id == TypeTableEntryIdPointer ||
child_type->id == TypeTableEntryIdFn)
{
if (const_val->data.x_maybe) {
@@ -4322,7 +4336,10 @@ static void get_c_type(CodeGen *g, TypeTableEntry *type_entry, Buf *out_buf) {
case TypeTableEntryIdMaybe:
{
TypeTableEntry *child_type = type_entry->data.maybe.child_type;
- if (child_type->id == TypeTableEntryIdPointer ||
+ if (child_type->zero_bits) {
+ buf_init_from_str(out_buf, "bool");
+ return;
+ } else if (child_type->id == TypeTableEntryIdPointer ||
child_type->id == TypeTableEntryIdFn)
{
return get_c_type(g, child_type, out_buf);