authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 01:54:27-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 01:54:27-05:00
log0d2f2b79eaf844e6bcdb41fe61c7e1655df9b1a0
tree2072c39406d37f55f4505acd95e23edef42daa11
parent69cf0ea56877a6f9bae51a35cffde371bf142567

IR: basic support for implicit casting to const pointer


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

src/codegen.cpp+3-1
......@@ -1677,6 +1677,7 @@ static LLVMValueRef ir_render_ref(CodeGen *g, IrExecutable *executable, IrInstru
16771677 if (handle_is_ptr(instruction->value->type_entry)) {
16781678 return value;
16791679 } else {
1680 assert(instruction->tmp_ptr);
16801681 LLVMBuildStore(g->builder, value, instruction->tmp_ptr);
16811682 return instruction->tmp_ptr;
16821683 }
......@@ -2843,6 +2844,7 @@ static void do_code_gen(CodeGen *g) {
28432844 for (size_t alloca_i = 0; alloca_i < fn_table_entry->alloca_list.length; alloca_i += 1) {
28442845 IrInstruction *instruction = fn_table_entry->alloca_list.at(alloca_i);
28452846 LLVMValueRef *slot;
2847 TypeTableEntry *slot_type = instruction->type_entry;
28462848 if (instruction->id == IrInstructionIdCast) {
28472849 IrInstructionCast *cast_instruction = (IrInstructionCast *)instruction;
28482850 slot = &cast_instruction->tmp_ptr;
......@@ -2876,7 +2878,7 @@ static void do_code_gen(CodeGen *g) {
28762878 } else {
28772879 zig_unreachable();
28782880 }
2879 *slot = LLVMBuildAlloca(g->builder, instruction->type_entry->type_ref, "");
2881 *slot = LLVMBuildAlloca(g->builder, slot_type->type_ref, "");
28802882 }
28812883
28822884 ImportTableEntry *import = get_scope_import(&fn_table_entry->fndef_scope->base);
src/ir.cpp+39
......@@ -4230,6 +4230,13 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
42304230 return ImplicitCastMatchResultYes;
42314231 }
42324232
4233 // implicitly take a const pointer to something
4234 {
4235 TypeTableEntry *const_ptr_actual = get_pointer_to_type(ira->codegen, actual_type, true);
4236 if (types_match_const_cast_only(expected_type, const_ptr_actual)) {
4237 return ImplicitCastMatchResultYes;
4238 }
4239 }
42334240
42344241 return ImplicitCastMatchResultNo;
42354242}
......@@ -4721,6 +4728,30 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so
47214728 return result;
47224729}
47234730
4731static IrInstruction *ir_analyze_cast_ref(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, TypeTableEntry *wanted_type) {
4732 if (instr_is_comptime(value)) {
4733 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
4734 if (!val)
4735 return ira->codegen->invalid_instruction;
4736
4737 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
4738 source_instr->scope, source_instr->source_node);
4739 const_instruction->base.type_entry = wanted_type;
4740 const_instruction->base.static_value.special = ConstValSpecialStatic;
4741 const_instruction->base.static_value.depends_on_compile_var = val->depends_on_compile_var;
4742 const_instruction->base.static_value.data.x_ptr.base_ptr = val;
4743 const_instruction->base.static_value.data.x_ptr.index = SIZE_MAX;
4744 return &const_instruction->base;
4745 }
4746
4747 if (value->id == IrInstructionIdLoadPtr) {
4748 IrInstructionLoadPtr *load_ptr_inst = (IrInstructionLoadPtr *)value;
4749 return load_ptr_inst->ptr;
4750 } else {
4751 zig_panic("TODO more ways to cast to const pointer");
4752 }
4753}
4754
47244755static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *source_instr, IrInstruction *value, TypeTableEntry *wanted_type) {
47254756 assert(wanted_type->id == TypeTableEntryIdMaybe);
47264757 assert(instr_is_comptime(value));
......@@ -4974,6 +5005,14 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
49745005 return ir_resolve_cast(ira, source_instr, value, wanted_type, CastOpNoop, false);
49755006 }
49765007
5008 // explicit cast from something to const pointer of it
5009 {
5010 TypeTableEntry *const_ptr_actual = get_pointer_to_type(ira->codegen, actual_type, true);
5011 if (types_match_const_cast_only(wanted_type, const_ptr_actual)) {
5012 return ir_analyze_cast_ref(ira, source_instr, value, wanted_type);
5013 }
5014 }
5015
49775016 add_node_error(ira->codegen, source_instr->source_node,
49785017 buf_sprintf("invalid cast from type '%s' to '%s'",
49795018 buf_ptr(&actual_type->name),
std/io.zig+1-1
......@@ -82,7 +82,7 @@ pub struct OutStream {
8282
8383 pub fn write(self: &OutStream, bytes: []const u8) -> %usize {
8484 var src_bytes_left = bytes.len;
85 var src_index: @typeOf(bytes.len) = 0;
85 var src_index: usize = 0;
8686 const dest_space_left = self.buffer.len - self.index;
8787
8888 while (src_bytes_left > 0) {