authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-10-29 19:24:59-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-10-29 19:24:59-04:00
log56cdaff9e71c10eb9c4dd130ee2516acacc1c13f
tree7a93d1f91bcb3fd580c9667e728273bf8ce694db
parentafc5507b6469c9963db8e00efc5648f8e682cf02

ir: support return expression


2 files changed, 34 insertions(+), 18 deletions(-)

src/analyze.cpp+1-10
......@@ -4970,11 +4970,6 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
49704970static TypeTableEntry *analyze_return_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
49714971 TypeTableEntry *expected_type, AstNode *node)
49724972{
4973 if (!context->fn_entry) {
4974 add_node_error(g, node, buf_sprintf("return expression outside function definition"));
4975 return g->builtin_types.entry_invalid;
4976 }
4977
49784973 if (!node->data.return_expr.expr) {
49794974 node->data.return_expr.expr = create_ast_void_node(g, import, node);
49804975 normalize_parent_ptrs(node);
......@@ -4984,11 +4979,7 @@ static TypeTableEntry *analyze_return_expr(CodeGen *g, ImportTableEntry *import,
49844979
49854980 switch (node->data.return_expr.kind) {
49864981 case ReturnKindUnconditional:
4987 {
4988 analyze_expression(g, import, context, expected_return_type, node->data.return_expr.expr);
4989
4990 return g->builtin_types.entry_unreachable;
4991 }
4982 zig_panic("TODO moved to ir.cpp");
49924983 case ReturnKindError:
49934984 {
49944985 TypeTableEntry *expected_err_type;
src/ir.cpp+33-8
......@@ -594,6 +594,37 @@ static void ir_gen_defers_for_block(IrBuilder *irb, BlockContext *inner_block, B
594594 }
595595}
596596
597static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *node) {
598 assert(node->type == NodeTypeReturnExpr);
599
600 BlockContext *scope = node->block_context;
601
602 if (!scope->fn_entry) {
603 add_node_error(irb->codegen, node, buf_sprintf("return expression outside function definition"));
604 return irb->codegen->invalid_instruction;
605 }
606
607 AstNode *expr_node = node->data.return_expr.expr;
608 switch (node->data.return_expr.kind) {
609 case ReturnKindUnconditional:
610 {
611 IrInstruction *return_value;
612 if (expr_node) {
613 return_value = ir_gen_node(irb, expr_node, scope);
614 } else {
615 return_value = ir_build_const_void(irb, node);
616 }
617
618 return ir_build_return(irb, node, return_value);
619 }
620 case ReturnKindError:
621 zig_panic("TODO %%return");
622 case ReturnKindMaybe:
623 zig_panic("TODO ?return");
624 }
625 zig_unreachable();
626}
627
597628//static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *source_node, IrInstruction *value, ReturnKnowledge rk) {
598629// BlockContext *defer_inner_block = source_node->block_context;
599630// BlockContext *defer_outer_block = irb->node->block_context;
......@@ -1230,15 +1261,9 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
12301261 return ir_gen_while_expr(irb, node);
12311262 case NodeTypeArrayAccessExpr:
12321263 return ir_gen_array_access(irb, node, lval);
1233 case NodeTypeUnwrapErrorExpr:
12341264 case NodeTypeReturnExpr:
1235 // TODO
1236 //if (!scope->fn_entry) {
1237 // add_node_error(ira->codegen, source_node,
1238 // buf_sprintf("return expression outside function definition"));
1239 // return ira->codegen->builtin_types.entry_invalid;
1240 //}
1241
1265 return ir_gen_return(irb, node);
1266 case NodeTypeUnwrapErrorExpr:
12421267 case NodeTypeDefer:
12431268 case NodeTypeSliceExpr:
12441269 case NodeTypeFieldAccessExpr: