From ccce3d852681bfe60bce92a6766b4f431dd73571 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 31 May 2019 01:36:57 -0400 Subject: no-copy semantics for function forwarding ```zig fn foo() Foo { return bar(); } ``` ```llvm define internal fastcc void @foo(%Foo* nonnull sret) unnamed_addr #2 !dbg !48 { Entry: call fastcc void @bar(%Foo* sret %0), !dbg !52 ret void, !dbg !54 } ``` --- src/ir.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index f700404f29..3420a85fce 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -1382,6 +1382,9 @@ static IrInstruction *ir_build_call_gen(IrAnalyze *ira, IrInstruction *source_in FnInline fn_inline, bool is_async, IrInstruction *async_allocator, IrInstruction *new_stack, ResultLoc *result_loc, ZigType *return_type) { + // must be resolved before building the call instruction + IrInstruction *resolved_result_loc = ir_resolve_result(ira, result_loc, return_type, nullptr); + IrInstructionCallGen *call_instruction = ir_build_instruction(&ira->new_irb, source_instruction->scope, source_instruction->source_node); call_instruction->base.value.type = return_type; @@ -1393,7 +1396,7 @@ static IrInstruction *ir_build_call_gen(IrAnalyze *ira, IrInstruction *source_in call_instruction->is_async = is_async; call_instruction->async_allocator = async_allocator; call_instruction->new_stack = new_stack; - call_instruction->result_loc = ir_resolve_result(ira, result_loc, return_type, nullptr); + call_instruction->result_loc = resolved_result_loc; if (fn_ref != nullptr) ir_ref_instruction(fn_ref, ira->new_irb.current_basic_block); for (size_t i = 0; i < arg_count; i += 1) @@ -14347,10 +14350,14 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, ResultLoc *result_lo } // give nullptr for value to resolve it at runtime -// returns a result location, or nullptr if the result location was already taken care of by this function +// returns a result location, or nullptr if the result location was already taken care of static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, ZigType *value_type, IrInstruction *value) { + if (result_loc->implicit_elem_type != nullptr) { + // already resolved + return nullptr; + } result_loc->gen_instruction = value; result_loc->implicit_elem_type = value_type; switch (result_loc->id) { -- cgit v1.2.3