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 {
401401};
402402
403403struct 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.
407404 ZigList<AstNode *> statements;
405 bool last_statement_is_result_expression;
408406};
409407
410408enum ReturnKind {
src/analyze.cpp-17
......@@ -2950,23 +2950,6 @@ void semantic_analyze(CodeGen *g) {
29502950 }
29512951}
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
29702953TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
29712954 size_t index;
29722955 if (size_in_bits == 8) {
src/analyze.hpp-1
......@@ -17,7 +17,6 @@ TypeTableEntry *new_type_table_entry(TypeTableEntryId id);
1717TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
1818TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const,
1919 bool is_volatile, uint32_t bit_offset, uint32_t unaligned_bit_count);
20bool is_node_void_expr(AstNode *node);
2120uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);
2221uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry);
2322TypeTableEntry **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) {
459459 }
460460 print_indent(ar);
461461 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)) {
463464 fprintf(ar->f, ";");
465 }
464466 fprintf(ar->f, "\n");
465467 }
466468 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
33443344 return ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
33453345 }
33463346
3347 bool is_continuation_unreachable = false;
33473348 IrInstruction *return_value = nullptr;
33483349 for (size_t i = 0; i < block_node->data.block.statements.length; i += 1) {
33493350 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
33673368 scope_block->label_table.put(label_name, label);
33683369 }
33693370
3370 if (!(return_value && instr_is_unreachable(return_value))) {
3371 if (!is_continuation_unreachable) {
33713372 // fall through into new labeled basic block
33723373 IrInstruction *is_comptime = ir_mark_gen(ir_build_const_bool(irb, child_scope, statement_node,
33733374 ir_should_inline(irb->exec, child_scope)));
......@@ -3375,34 +3376,66 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
33753376 }
33763377 ir_set_cursor_at_end(irb, label_block);
33773378
3379 // a label is an entry point
3380 is_continuation_unreachable = false;
33783381 return_value = nullptr;
33793382 continue;
33803383 }
33813384
3382 if (return_value && instr_is_unreachable(return_value)) {
3383 if (is_node_void_expr(statement_node))
3385 if (is_continuation_unreachable) {
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)
33843390 continue;
33853391 add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));
33863392 }
33873393
3388 return_value = ir_gen_node(irb, statement_node, child_scope);
3389 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {
3394 IrInstruction *statement_value = ir_gen_node(irb, statement_node, child_scope);
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) {
33903401 // defer starts a new scope
33913402 child_scope = statement_node->data.defer.child_scope;
33923403 assert(child_scope);
3393 } else if (return_value->id == IrInstructionIdDeclVar) {
3404 } else if (statement_value->id == IrInstructionIdDeclVar) {
33943405 // variable declarations start a new scope
3395 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)return_value;
3406 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)statement_value;
33963407 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 }
33973424 }
33983425 }
33993426
3400 // labels are never the last statement
3401 assert(return_value != nullptr);
3427 if (!is_continuation_unreachable) {
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))
34043435 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
3436 }
34053437
3438 assert(return_value != nullptr);
34063439 return return_value;
34073440}
34083441
src/parser.cpp+50-74
......@@ -1878,36 +1878,6 @@ static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, boo
18781878 }
18791879}
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
19111881/*
19121882BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)
19131883*/
......@@ -2120,9 +2090,35 @@ static AstNode *ast_parse_label(ParseContext *pc, size_t *token_index, bool mand
21202090 return node;
21212091}
21222092
2123static AstNode *ast_create_void_expr(ParseContext *pc, Token *token) {
2124 AstNode *node = ast_create_node(pc, NodeTypeBlock, token);
2125 return node;
2093static bool statement_terminates_without_semicolon(AstNode *node) {
2094 switch (node->type) {
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 }
21262122}
21272123
21282124/*
......@@ -2145,56 +2141,36 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
21452141
21462142 for (;;) {
21472143 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
21492153 bool semicolon_expected = true;
21502154 if (statement_node) {
2151 // label
2152 semicolon_expected = false;
2153 // if a label is the last thing in a block, add a void statement.
2154 need_implicit_final_void_statement = true;
2155 } else {
2156 statement_node = ast_parse_variable_declaration_expr(pc, token_index, false, VisibModPrivate);
2157 if (!statement_node) {
2158 statement_node = ast_parse_defer_expr(pc, token_index);
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 }
2155 node->data.block.statements.append(statement_node);
2156 if (statement_terminates_without_semicolon(statement_node)) {
2157 semicolon_expected = false;
2158 } else {
2159 if (statement_node->type == NodeTypeDefer) {
2160 // defer without a block body requires a semicolon
2161 Token *token = &pc->tokens->at(*token_index);
2162 ast_expect_token(pc, token, TokenIdSemicolon);
21842163 }
21852164 }
21862165 }
2187 if (statement_node)
2188 node->data.block.statements.append(statement_node);
2166
2167 node->data.block.last_statement_is_result_expression = statement_node && !(
2168 statement_node->type == NodeTypeLabel ||
2169 statement_node->type == NodeTypeDefer);
21892170
21902171 last_token = &pc->tokens->at(*token_index);
21912172 if (last_token->id == TokenIdRBrace) {
21922173 *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
21982174 return node;
21992175 } else if (!semicolon_expected) {
22002176 continue;