Commit 36a015741d

Josh Wolfe <thejoshwolfe@gmail.com>
2017-03-31 17:41:00
clean up analysis of {blocks}
* Don't insert void statements all over the place. {} now stays as {} instead of {{}}, and {;} becomes {} instead of {{};{}}. * Ensure final statement is always the return value statement, or the block is empty. This means {label:} becomes {label:{}}.
1 parent d5a6cdb
Changed files (3)
src/all_types.hpp
@@ -403,6 +403,9 @@ struct AstNodeParamDecl {
 };
 
 struct AstNodeBlock {
+    // the final statement is the returned expression.
+    // if there are no statements, the returned expression is void.
+    // the final statement is never a label.
     ZigList<AstNode *> statements;
 };
 
src/ir.cpp
@@ -3269,6 +3269,11 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
         fn_entry->def_scope = scope_block;
     }
 
+    if (block_node->data.block.statements.length == 0) {
+        // {}
+        return ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
+    }
+
     IrInstruction *return_value = nullptr;
     for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {
         AstNode *statement_node = block_node->data.block.statements.at(i);
@@ -3292,7 +3297,8 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
                 scope_block->label_table.put(label_name, label);
             }
 
-            if (!return_value || !instr_is_unreachable(return_value)) {
+            if (!(return_value && instr_is_unreachable(return_value))) {
+                // fall through into new labeled basic block
                 IrInstruction *is_comptime = ir_mark_gen(ir_build_const_bool(irb, child_scope, statement_node,
                         ir_should_inline(irb->exec, child_scope)));
                 ir_mark_gen(ir_build_br(irb, child_scope, statement_node, label_block, is_comptime));
@@ -3321,8 +3327,8 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
         }
     }
 
-    if (!return_value)
-        return_value = ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
+    // labels are never the last statement
+    assert(return_value != nullptr);
 
     if (!instr_is_unreachable(return_value))
         ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
src/parser.cpp
@@ -2094,16 +2094,14 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
 
     AstNode *node = ast_create_node(pc, NodeTypeBlock, last_token);
 
-    // {}   -> {void}
-    // {;}  -> {void;void}
-    // {2}  -> {2}
-    // {2;} -> {2;void}
-    // {;2} -> {void;2}
     for (;;) {
         AstNode *statement_node = ast_parse_label(pc, token_index, false);
+        bool need_implicit_final_void_statement = false;
         bool semicolon_expected;
         if (statement_node) {
             semicolon_expected = false;
+            // if a label is the last thing in a block, add a void statement.
+            need_implicit_final_void_statement = true;
         } else {
             statement_node = ast_parse_variable_declaration_expr(pc, token_index, false, VisibModPrivate);
             if (!statement_node) {
@@ -2117,17 +2115,23 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
                 if (!statement_node) {
                     statement_node = ast_parse_non_block_expr(pc, token_index, false);
                     if (!statement_node) {
-                        statement_node = ast_create_void_expr(pc, last_token);
+                        // final semicolon means add a void statement.
+                        need_implicit_final_void_statement = true;
                     }
                 }
             }
         }
-        node->data.block.statements.append(statement_node);
+        if (statement_node)
+            node->data.block.statements.append(statement_node);
 
         last_token = &pc->tokens->at(*token_index);
         if (last_token->id == TokenIdRBrace) {
             *token_index += 1;
 
+            if (node->data.block.statements.length > 0 && need_implicit_final_void_statement) {
+                node->data.block.statements.append(ast_create_void_expr(pc, last_token));
+            }
+
             return node;
         } else if (!semicolon_expected) {
             continue;