authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-03-31 08:41:00-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-03-31 08:54:03-07:00
log36a015741db9fbf748fe9ab082f78c5d8663024c
tree9ed34ffe3f8b4bd3d274bc21d5db6c1d5e336051
parentd5a6cdb03f874ab53928a67330ebfab1e64fe111

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:{}}.

3 files changed, 23 insertions(+), 10 deletions(-)

src/all_types.hpp+3
......@@ -403,6 +403,9 @@ struct AstNodeParamDecl {
403403};
404404
405405struct AstNodeBlock {
406 // the final statement is the returned expression.
407 // if there are no statements, the returned expression is void.
408 // the final statement is never a label.
406409 ZigList<AstNode *> statements;
407410};
408411
src/ir.cpp+9-3
......@@ -3269,6 +3269,11 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
32693269 fn_entry->def_scope = scope_block;
32703270 }
32713271
3272 if (block_node->data.block.statements.length == 0) {
3273 // {}
3274 return ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
3275 }
3276
32723277 IrInstruction *return_value = nullptr;
32733278 for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {
32743279 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
32923297 scope_block->label_table.put(label_name, label);
32933298 }
32943299
3295 if (!return_value || !instr_is_unreachable(return_value)) {
3300 if (!(return_value && instr_is_unreachable(return_value))) {
3301 // fall through into new labeled basic block
32963302 IrInstruction *is_comptime = ir_mark_gen(ir_build_const_bool(irb, child_scope, statement_node,
32973303 ir_should_inline(irb->exec, child_scope)));
32983304 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
33213327 }
33223328 }
33233329
3324 if (!return_value)
3325 return_value = ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
3330 // labels are never the last statement
3331 assert(return_value != nullptr);
33263332
33273333 if (!instr_is_unreachable(return_value))
33283334 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
src/parser.cpp+11-7
......@@ -2094,16 +2094,14 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
20942094
20952095 AstNode *node = ast_create_node(pc, NodeTypeBlock, last_token);
20962096
2097 // {} -> {void}
2098 // {;} -> {void;void}
2099 // {2} -> {2}
2100 // {2;} -> {2;void}
2101 // {;2} -> {void;2}
21022097 for (;;) {
21032098 AstNode *statement_node = ast_parse_label(pc, token_index, false);
2099 bool need_implicit_final_void_statement = false;
21042100 bool semicolon_expected;
21052101 if (statement_node) {
21062102 semicolon_expected = false;
2103 // if a label is the last thing in a block, add a void statement.
2104 need_implicit_final_void_statement = true;
21072105 } else {
21082106 statement_node = ast_parse_variable_declaration_expr(pc, token_index, false, VisibModPrivate);
21092107 if (!statement_node) {
......@@ -2117,17 +2115,23 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
21172115 if (!statement_node) {
21182116 statement_node = ast_parse_non_block_expr(pc, token_index, false);
21192117 if (!statement_node) {
2120 statement_node = ast_create_void_expr(pc, last_token);
2118 // final semicolon means add a void statement.
2119 need_implicit_final_void_statement = true;
21212120 }
21222121 }
21232122 }
21242123 }
2125 node->data.block.statements.append(statement_node);
2124 if (statement_node)
2125 node->data.block.statements.append(statement_node);
21262126
21272127 last_token = &pc->tokens->at(*token_index);
21282128 if (last_token->id == TokenIdRBrace) {
21292129 *token_index += 1;
21302130
2131 if (node->data.block.statements.length > 0 && need_implicit_final_void_statement) {
2132 node->data.block.statements.append(ast_create_void_expr(pc, last_token));
2133 }
2134
21312135 return node;
21322136 } else if (!semicolon_expected) {
21332137 continue;