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,...@@ -4970,11 +4970,6 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
4970static TypeTableEntry *analyze_return_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,4970static TypeTableEntry *analyze_return_expr(CodeGen *g, ImportTableEntry *import, BlockContext *context,
4971 TypeTableEntry *expected_type, AstNode *node)4971 TypeTableEntry *expected_type, AstNode *node)
4972{4972{
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
4978 if (!node->data.return_expr.expr) {4973 if (!node->data.return_expr.expr) {
4979 node->data.return_expr.expr = create_ast_void_node(g, import, node);4974 node->data.return_expr.expr = create_ast_void_node(g, import, node);
4980 normalize_parent_ptrs(node);4975 normalize_parent_ptrs(node);
...@@ -4984,11 +4979,7 @@ static TypeTableEntry *analyze_return_expr(CodeGen *g, ImportTableEntry *import,...@@ -4984,11 +4979,7 @@ static TypeTableEntry *analyze_return_expr(CodeGen *g, ImportTableEntry *import,
49844979
4985 switch (node->data.return_expr.kind) {4980 switch (node->data.return_expr.kind) {
4986 case ReturnKindUnconditional:4981 case ReturnKindUnconditional:
4987 {4982 zig_panic("TODO moved to ir.cpp");
4988 analyze_expression(g, import, context, expected_return_type, node->data.return_expr.expr);
4989
4990 return g->builtin_types.entry_unreachable;
4991 }
4992 case ReturnKindError:4983 case ReturnKindError:
4993 {4984 {
4994 TypeTableEntry *expected_err_type;4985 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...@@ -594,6 +594,37 @@ static void ir_gen_defers_for_block(IrBuilder *irb, BlockContext *inner_block, B
594 }594 }
595}595}
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
597//static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *source_node, IrInstruction *value, ReturnKnowledge rk) {628//static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *source_node, IrInstruction *value, ReturnKnowledge rk) {
598// BlockContext *defer_inner_block = source_node->block_context;629// BlockContext *defer_inner_block = source_node->block_context;
599// BlockContext *defer_outer_block = irb->node->block_context;630// BlockContext *defer_outer_block = irb->node->block_context;
...@@ -1230,15 +1261,9 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont...@@ -1230,15 +1261,9 @@ static IrInstruction *ir_gen_node_extra(IrBuilder *irb, AstNode *node, BlockCont
1230 return ir_gen_while_expr(irb, node);1261 return ir_gen_while_expr(irb, node);
1231 case NodeTypeArrayAccessExpr:1262 case NodeTypeArrayAccessExpr:
1232 return ir_gen_array_access(irb, node, lval);1263 return ir_gen_array_access(irb, node, lval);
1233 case NodeTypeUnwrapErrorExpr:
1234 case NodeTypeReturnExpr:1264 case NodeTypeReturnExpr:
1235 // TODO1265 return ir_gen_return(irb, node);
1236 //if (!scope->fn_entry) {1266 case NodeTypeUnwrapErrorExpr:
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
1242 case NodeTypeDefer:1267 case NodeTypeDefer:
1243 case NodeTypeSliceExpr:1268 case NodeTypeSliceExpr:
1244 case NodeTypeFieldAccessExpr:1269 case NodeTypeFieldAccessExpr: