| ... | ... | @@ -594,6 +594,37 @@ static void ir_gen_defers_for_block(IrBuilder *irb, BlockContext *inner_block, B |
| 594 | 594 | } |
| 595 | 595 | } |
| 596 | 596 | |
| 597 | static 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 | 628 | //static IrInstruction *ir_gen_return(IrBuilder *irb, AstNode *source_node, IrInstruction *value, ReturnKnowledge rk) { |
| 598 | 629 | // BlockContext *defer_inner_block = source_node->block_context; |
| 599 | 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 | 1261 | return ir_gen_while_expr(irb, node); |
| 1231 | 1262 | case NodeTypeArrayAccessExpr: |
| 1232 | 1263 | return ir_gen_array_access(irb, node, lval); |
| 1233 | | case NodeTypeUnwrapErrorExpr: |
| 1234 | 1264 | 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: |
| 1242 | 1267 | case NodeTypeDefer: |
| 1243 | 1268 | case NodeTypeSliceExpr: |
| 1244 | 1269 | case NodeTypeFieldAccessExpr: |