authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-13 03:09:09-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-13 03:09:09-04:00
logf2140efc5255aa0f0eb826ddd379e07acf6ba131
tree2ec89e3f9f60d602c182c9b1c4f4215d6e8c3a66
parentbf57d8a7e3beb8d69dec38e131a3717f008f6c5e
parent356424916ced599852e2c38265eebe7a1fc1637d

Merge remote-tracking branch 'origin/parser'


6 files changed, 97 insertions(+), 106 deletions(-)

src/all_types.hpp+1-3
...@@ -401,10 +401,8 @@ struct AstNodeParamDecl {...@@ -401,10 +401,8 @@ struct AstNodeParamDecl {
401};401};
402402
403struct AstNodeBlock {403struct AstNodeBlock {
404 // the final statement is the returned expression.
405 // if there are no statements, the returned expression is void.
406 // the final statement is never a label.
407 ZigList<AstNode *> statements;404 ZigList<AstNode *> statements;
405 bool last_statement_is_result_expression;
408};406};
409407
410enum ReturnKind {408enum ReturnKind {
src/analyze.cpp-17
...@@ -2950,23 +2950,6 @@ void semantic_analyze(CodeGen *g) {...@@ -2950,23 +2950,6 @@ void semantic_analyze(CodeGen *g) {
2950 }2950 }
2951}2951}
29522952
2953bool is_node_void_expr(AstNode *node) {
2954 if (node->type == NodeTypeContainerInitExpr &&
2955 node->data.container_init_expr.kind == ContainerInitKindArray)
2956 {
2957 AstNode *type_node = node->data.container_init_expr.type;
2958 if (type_node->type == NodeTypeSymbol &&
2959 buf_eql_str(type_node->data.symbol_expr.symbol, "void"))
2960 {
2961 return true;
2962 }
2963 } else if (node->type == NodeTypeBlock && node->data.block.statements.length == 0) {
2964 return true;
2965 }
2966
2967 return false;
2968}
2969
2970TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {2953TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
2971 size_t index;2954 size_t index;
2972 if (size_in_bits == 8) {2955 if (size_in_bits == 8) {
src/analyze.hpp-1
...@@ -17,7 +17,6 @@ TypeTableEntry *new_type_table_entry(TypeTableEntryId id);...@@ -17,7 +17,6 @@ TypeTableEntry *new_type_table_entry(TypeTableEntryId id);
17TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);17TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
18TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const,18TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const,
19 bool is_volatile, uint32_t bit_offset, uint32_t unaligned_bit_count);19 bool is_volatile, uint32_t bit_offset, uint32_t unaligned_bit_count);
20bool is_node_void_expr(AstNode *node);
21uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);20uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);
22uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry);21uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry);
23TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits);22TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits);
src/ast_render.cpp+3-1
...@@ -459,8 +459,10 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {...@@ -459,8 +459,10 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
459 }459 }
460 print_indent(ar);460 print_indent(ar);
461 render_node_grouped(ar, statement);461 render_node_grouped(ar, statement);
462 if (i != node->data.block.statements.length - 1)462 if (!(i == node->data.block.statements.length - 1 &&
463 node->data.block.last_statement_is_result_expression)) {
463 fprintf(ar->f, ";");464 fprintf(ar->f, ";");
465 }
464 fprintf(ar->f, "\n");466 fprintf(ar->f, "\n");
465 }467 }
466 ar->indent -= ar->indent_size;468 ar->indent -= ar->indent_size;
src/ir.cpp+43-10
...@@ -3344,6 +3344,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3344,6 +3344,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
3344 return ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));3344 return ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
3345 }3345 }
33463346
3347 bool is_continuation_unreachable = false;
3347 IrInstruction *return_value = nullptr;3348 IrInstruction *return_value = nullptr;
3348 for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {3349 for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {
3349 AstNode *statement_node = block_node->data.block.statements.at(i);3350 AstNode *statement_node = block_node->data.block.statements.at(i);
...@@ -3367,7 +3368,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3367,7 +3368,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
3367 scope_block->label_table.put(label_name, label);3368 scope_block->label_table.put(label_name, label);
3368 }3369 }
33693370
3370 if (!(return_value && instr_is_unreachable(return_value))) {3371 if (!is_continuation_unreachable) {
3371 // fall through into new labeled basic block3372 // fall through into new labeled basic block
3372 IrInstruction *is_comptime = ir_mark_gen(ir_build_const_bool(irb, child_scope, statement_node,3373 IrInstruction *is_comptime = ir_mark_gen(ir_build_const_bool(irb, child_scope, statement_node,
3373 ir_should_inline(irb->exec, child_scope)));3374 ir_should_inline(irb->exec, child_scope)));
...@@ -3375,34 +3376,66 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3375,34 +3376,66 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
3375 }3376 }
3376 ir_set_cursor_at_end(irb, label_block);3377 ir_set_cursor_at_end(irb, label_block);
33773378
3379 // a label is an entry point
3380 is_continuation_unreachable = false;
3378 return_value = nullptr;3381 return_value = nullptr;
3379 continue;3382 continue;
3380 }3383 }
33813384
3382 if (return_value && instr_is_unreachable(return_value)) {3385 if (is_continuation_unreachable) {
3383 if (is_node_void_expr(statement_node))3386 // if you put a semicolon after a return statement,
3387 // then we get a void statement in the unreachable area.
3388 // this is fine. ignore any void blocks we get from this happening.
3389 if (statement_node->type == NodeTypeBlock && statement_node->data.block.statements.length == 0)
3384 continue;3390 continue;
3385 add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));3391 add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));
3386 }3392 }
33873393
3388 return_value = ir_gen_node(irb, statement_node, child_scope);3394 IrInstruction *statement_value = ir_gen_node(irb, statement_node, child_scope);
3389 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {3395 is_continuation_unreachable = instr_is_unreachable(statement_value);
3396 if (is_continuation_unreachable)
3397 return_value = statement_value;
3398 else
3399 return_value = nullptr;
3400 if (statement_node->type == NodeTypeDefer && statement_value != irb->codegen->invalid_instruction) {
3390 // defer starts a new scope3401 // defer starts a new scope
3391 child_scope = statement_node->data.defer.child_scope;3402 child_scope = statement_node->data.defer.child_scope;
3392 assert(child_scope);3403 assert(child_scope);
3393 } else if (return_value->id == IrInstructionIdDeclVar) {3404 } else if (statement_value->id == IrInstructionIdDeclVar) {
3394 // variable declarations start a new scope3405 // variable declarations start a new scope
3395 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)return_value;3406 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)statement_value;
3396 child_scope = decl_var_instruction->var->child_scope;3407 child_scope = decl_var_instruction->var->child_scope;
3408 } else {
3409 // label, defer, variable declaration will never be the last statement
3410 if (block_node->data.block.last_statement_is_result_expression &&
3411 i == block_node->data.block.statements.length - 1) {
3412 // this is the result value statement
3413 return_value = statement_value;
3414 } else {
3415 // there are more statements ahead of this one. this statement's value must be void
3416 TypeTableEntry *instruction_type = statement_value->value.type;
3417 if (instruction_type &&
3418 instruction_type->id != TypeTableEntryIdInvalid &&
3419 instruction_type->id != TypeTableEntryIdVoid &&
3420 instruction_type->id != TypeTableEntryIdUnreachable) {
3421 add_node_error(irb->codegen, statement_node, buf_sprintf("expression valued ignored"));
3422 }
3423 }
3397 }3424 }
3398 }3425 }
33993426
3400 // labels are never the last statement3427 if (!is_continuation_unreachable) {
3401 assert(return_value != nullptr);3428 // control flow falls out of block
3429
3430 if (!block_node->data.block.last_statement_is_result_expression) {
3431 assert(return_value == nullptr);
3432 return_value = ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
3433 }
34023434
3403 if (!instr_is_unreachable(return_value))
3404 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);3435 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
3436 }
34053437
3438 assert(return_value != nullptr);
3406 return return_value;3439 return return_value;
3407}3440}
34083441
src/parser.cpp+50-74
...@@ -1878,36 +1878,6 @@ static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, boo...@@ -1878,36 +1878,6 @@ static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, boo
1878 }1878 }
1879}1879}
18801880
1881static bool statement_has_block_body(AstNode *node) {
1882 switch (node->type) {
1883 case NodeTypeIfBoolExpr:
1884 if (node->data.if_bool_expr.else_node)
1885 return statement_has_block_body(node->data.if_bool_expr.else_node);
1886 return node->data.if_bool_expr.then_block->type == NodeTypeBlock;
1887 case NodeTypeIfVarExpr:
1888 if (node->data.if_var_expr.else_node)
1889 return statement_has_block_body(node->data.if_var_expr.else_node);
1890 return node->data.if_var_expr.then_block->type == NodeTypeBlock;
1891 case NodeTypeTryExpr:
1892 if (node->data.try_expr.else_node)
1893 return statement_has_block_body(node->data.try_expr.else_node);
1894 return node->data.try_expr.then_node->type == NodeTypeBlock;
1895 case NodeTypeWhileExpr:
1896 return node->data.while_expr.body->type == NodeTypeBlock;
1897 case NodeTypeForExpr:
1898 return node->data.for_expr.body->type == NodeTypeBlock;
1899 case NodeTypeSwitchExpr:
1900 case NodeTypeBlock:
1901 return true;
1902 case NodeTypeCompTime:
1903 return node->data.comptime_expr.expr->type == NodeTypeBlock;
1904 case NodeTypeDefer:
1905 return node->data.defer.expr->type == NodeTypeBlock;
1906 default:
1907 return false;
1908 }
1909}
1910
1911/*1881/*
1912BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)1882BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)
1913*/1883*/
...@@ -2120,9 +2090,35 @@ static AstNode *ast_parse_label(ParseContext *pc, size_t *token_index, bool mand...@@ -2120,9 +2090,35 @@ static AstNode *ast_parse_label(ParseContext *pc, size_t *token_index, bool mand
2120 return node;2090 return node;
2121}2091}
21222092
2123static AstNode *ast_create_void_expr(ParseContext *pc, Token *token) {2093static bool statement_terminates_without_semicolon(AstNode *node) {
2124 AstNode *node = ast_create_node(pc, NodeTypeBlock, token);2094 switch (node->type) {
2125 return node;2095 case NodeTypeIfBoolExpr:
2096 if (node->data.if_bool_expr.else_node)
2097 return statement_terminates_without_semicolon(node->data.if_bool_expr.else_node);
2098 return node->data.if_bool_expr.then_block->type == NodeTypeBlock;
2099 case NodeTypeIfVarExpr:
2100 if (node->data.if_var_expr.else_node)
2101 return statement_terminates_without_semicolon(node->data.if_var_expr.else_node);
2102 return node->data.if_var_expr.then_block->type == NodeTypeBlock;
2103 case NodeTypeTryExpr:
2104 if (node->data.try_expr.else_node)
2105 return statement_terminates_without_semicolon(node->data.try_expr.else_node);
2106 return node->data.try_expr.then_node->type == NodeTypeBlock;
2107 case NodeTypeWhileExpr:
2108 return node->data.while_expr.body->type == NodeTypeBlock;
2109 case NodeTypeForExpr:
2110 return node->data.for_expr.body->type == NodeTypeBlock;
2111 case NodeTypeCompTime:
2112 return node->data.comptime_expr.expr->type == NodeTypeBlock;
2113 case NodeTypeDefer:
2114 return node->data.defer.expr->type == NodeTypeBlock;
2115 case NodeTypeSwitchExpr:
2116 case NodeTypeBlock:
2117 case NodeTypeLabel:
2118 return true;
2119 default:
2120 return false;
2121 }
2126}2122}
21272123
2128/*2124/*
...@@ -2145,56 +2141,36 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand...@@ -2145,56 +2141,36 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
21452141
2146 for (;;) {2142 for (;;) {
2147 AstNode *statement_node = ast_parse_label(pc, token_index, false);2143 AstNode *statement_node = ast_parse_label(pc, token_index, false);
2148 bool need_implicit_final_void_statement = false;2144 if (!statement_node)
2145 statement_node = ast_parse_variable_declaration_expr(pc, token_index, false, VisibModPrivate);
2146 if (!statement_node)
2147 statement_node = ast_parse_defer_expr(pc, token_index);
2148 if (!statement_node)
2149 statement_node = ast_parse_block_expr(pc, token_index, false);
2150 if (!statement_node)
2151 statement_node = ast_parse_expression(pc, token_index, false);
2152
2149 bool semicolon_expected = true;2153 bool semicolon_expected = true;
2150 if (statement_node) {2154 if (statement_node) {
2151 // label2155 node->data.block.statements.append(statement_node);
2152 semicolon_expected = false;2156 if (statement_terminates_without_semicolon(statement_node)) {
2153 // if a label is the last thing in a block, add a void statement.2157 semicolon_expected = false;
2154 need_implicit_final_void_statement = true;2158 } else {
2155 } else {2159 if (statement_node->type == NodeTypeDefer) {
2156 statement_node = ast_parse_variable_declaration_expr(pc, token_index, false, VisibModPrivate);2160 // defer without a block body requires a semicolon
2157 if (!statement_node) {2161 Token *token = &pc->tokens->at(*token_index);
2158 statement_node = ast_parse_defer_expr(pc, token_index);2162 ast_expect_token(pc, token, TokenIdSemicolon);
2159 if (statement_node) {
2160 // defer
2161 if (statement_has_block_body(statement_node)) {
2162 // don't let defer be the last statement in a block
2163 need_implicit_final_void_statement = true;
2164 semicolon_expected = false;
2165 } else {
2166 // defer without a block body requires a semicolon
2167 Token *token = &pc->tokens->at(*token_index);
2168 ast_expect_token(pc, token, TokenIdSemicolon);
2169 }
2170 } else {
2171 statement_node = ast_parse_block_expr(pc, token_index, false);
2172 if (statement_node) {
2173 // block expr
2174 if (statement_has_block_body(statement_node))
2175 semicolon_expected = false;
2176 } else {
2177 statement_node = ast_parse_expression(pc, token_index, false);
2178 if (!statement_node) {
2179 // no statement.
2180 // final semicolon means add a void statement.
2181 need_implicit_final_void_statement = true;
2182 }
2183 }
2184 }2163 }
2185 }2164 }
2186 }2165 }
2187 if (statement_node)2166
2188 node->data.block.statements.append(statement_node);2167 node->data.block.last_statement_is_result_expression = statement_node && !(
2168 statement_node->type == NodeTypeLabel ||
2169 statement_node->type == NodeTypeDefer);
21892170
2190 last_token = &pc->tokens->at(*token_index);2171 last_token = &pc->tokens->at(*token_index);
2191 if (last_token->id == TokenIdRBrace) {2172 if (last_token->id == TokenIdRBrace) {
2192 *token_index += 1;2173 *token_index += 1;
2193
2194 if (node->data.block.statements.length > 0 && need_implicit_final_void_statement) {
2195 node->data.block.statements.append(ast_create_void_expr(pc, last_token));
2196 }
2197
2198 return node;2174 return node;
2199 } else if (!semicolon_expected) {2175 } else if (!semicolon_expected) {
2200 continue;2176 continue;