authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 17:50:47-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-18 17:50:47-05:00
logba008fb9d7bb6cbffba57dcde0853a9f7cc2f674
tree671b4e4619d18023613c9cbac1ab354355d7fc0f
parent37b13bf1512d8eed7308a2194c1935d91a33796c

IR: ability to return a container from a function


1 files changed, 18 insertions(+), 7 deletions(-)

src/ir.cpp+18-7
...@@ -3872,7 +3872,11 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,...@@ -3872,7 +3872,11 @@ static IrInstruction *ir_gen_container_decl(IrBuilder *irb, Scope *parent_scope,
3872 } else {3872 } else {
3873 FnTableEntry *fn_entry = exec_fn_entry(irb->exec);3873 FnTableEntry *fn_entry = exec_fn_entry(irb->exec);
3874 if (fn_entry) {3874 if (fn_entry) {
3875 zig_panic("TODO name the container inside the function");3875 name = buf_alloc();
3876 buf_append_buf(name, &fn_entry->symbol_name);
3877 buf_appendf(name, "(");
3878 // TODO render args
3879 buf_appendf(name, ")");
3876 } else {3880 } else {
3877 name = buf_sprintf("(anonymous %s at %s:%zu:%zu)", container_string(kind),3881 name = buf_sprintf("(anonymous %s at %s:%zu:%zu)", container_string(kind),
3878 buf_ptr(node->owner->path), node->line + 1, node->column + 1);3882 buf_ptr(node->owner->path), node->line + 1, node->column + 1);
...@@ -6115,18 +6119,25 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal...@@ -6115,18 +6119,25 @@ static TypeTableEntry *ir_analyze_fn_call(IrAnalyze *ira, IrInstructionCall *cal
6115 }6119 }
6116 }6120 }
61176121
6118 auto existing_entry = ira->codegen->generic_table.put_unique(generic_id, impl_fn);6122 {
6119 if (existing_entry) {
6120 // throw away all our work and use the existing function
6121 impl_fn = existing_entry->value;
6122 } else {
6123 // finish instantiating the function
6124 AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;6123 AstNode *return_type_node = fn_proto_node->data.fn_proto.return_type;
6125 TypeTableEntry *return_type = analyze_type_expr(ira->codegen, impl_fn->child_scope, return_type_node);6124 TypeTableEntry *return_type = analyze_type_expr(ira->codegen, impl_fn->child_scope, return_type_node);
6126 if (return_type->id == TypeTableEntryIdInvalid)6125 if (return_type->id == TypeTableEntryIdInvalid)
6127 return ira->codegen->builtin_types.entry_invalid;6126 return ira->codegen->builtin_types.entry_invalid;
6128 fn_type_id.return_type = return_type;6127 fn_type_id.return_type = return_type;
61296128
6129 if (type_requires_comptime(return_type)) {
6130 // Throw out our work and call the function as if it were inline.
6131 return ir_analyze_fn_call(ira, call_instruction, fn_entry, fn_type, fn_ref, first_arg_ptr, true);
6132 }
6133 }
6134
6135 auto existing_entry = ira->codegen->generic_table.put_unique(generic_id, impl_fn);
6136 if (existing_entry) {
6137 // throw away all our work and use the existing function
6138 impl_fn = existing_entry->value;
6139 } else {
6140 // finish instantiating the function
6130 impl_fn->type_entry = get_fn_type(ira->codegen, &fn_type_id);6141 impl_fn->type_entry = get_fn_type(ira->codegen, &fn_type_id);
6131 if (impl_fn->type_entry->id == TypeTableEntryIdInvalid)6142 if (impl_fn->type_entry->id == TypeTableEntryIdInvalid)
6132 return ira->codegen->builtin_types.entry_invalid;6143 return ira->codegen->builtin_types.entry_invalid;