aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2017-04-13 03:09:09 -0400
committerAndrew Kelley <superjoe30@gmail.com>2017-04-13 03:09:09 -0400
commitf2140efc5255aa0f0eb826ddd379e07acf6ba131 (patch)
tree2ec89e3f9f60d602c182c9b1c4f4215d6e8c3a66 /src
parentbf57d8a7e3beb8d69dec38e131a3717f008f6c5e (diff)
parent356424916ced599852e2c38265eebe7a1fc1637d (diff)
downloadzig-f2140efc5255aa0f0eb826ddd379e07acf6ba131.tar.gz
zig-f2140efc5255aa0f0eb826ddd379e07acf6ba131.zip
Merge remote-tracking branch 'origin/parser'
Diffstat (limited to 'src')
-rw-r--r--src/all_types.hpp4
-rw-r--r--src/analyze.cpp17
-rw-r--r--src/analyze.hpp1
-rw-r--r--src/ast_render.cpp4
-rw-r--r--src/ir.cpp53
-rw-r--r--src/parser.cpp124
6 files changed, 97 insertions, 106 deletions
diff --git a/src/all_types.hpp b/src/all_types.hpp
index a186c65a1f..359bd20ae2 100644
--- a/src/all_types.hpp
+++ b/src/all_types.hpp
@@ -401,10 +401,8 @@ 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;
+ bool last_statement_is_result_expression;
};
enum ReturnKind {
diff --git a/src/analyze.cpp b/src/analyze.cpp
index fd84014acb..3b0b9753e8 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -2950,23 +2950,6 @@ void semantic_analyze(CodeGen *g) {
}
}
-bool is_node_void_expr(AstNode *node) {
- if (node->type == NodeTypeContainerInitExpr &&
- node->data.container_init_expr.kind == ContainerInitKindArray)
- {
- AstNode *type_node = node->data.container_init_expr.type;
- if (type_node->type == NodeTypeSymbol &&
- buf_eql_str(type_node->data.symbol_expr.symbol, "void"))
- {
- return true;
- }
- } else if (node->type == NodeTypeBlock && node->data.block.statements.length == 0) {
- return true;
- }
-
- return false;
-}
-
TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
size_t index;
if (size_in_bits == 8) {
diff --git a/src/analyze.hpp b/src/analyze.hpp
index de0d0bc933..f885b2711f 100644
--- a/src/analyze.hpp
+++ b/src/analyze.hpp
@@ -17,7 +17,6 @@ TypeTableEntry *new_type_table_entry(TypeTableEntryId id);
TypeTableEntry *get_pointer_to_type(CodeGen *g, TypeTableEntry *child_type, bool is_const);
TypeTableEntry *get_pointer_to_type_extra(CodeGen *g, TypeTableEntry *child_type, bool is_const,
bool is_volatile, uint32_t bit_offset, uint32_t unaligned_bit_count);
-bool is_node_void_expr(AstNode *node);
uint64_t type_size(CodeGen *g, TypeTableEntry *type_entry);
uint64_t type_size_bits(CodeGen *g, TypeTableEntry *type_entry);
TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits);
diff --git a/src/ast_render.cpp b/src/ast_render.cpp
index 3f47c1379c..0a6c856da9 100644
--- a/src/ast_render.cpp
+++ b/src/ast_render.cpp
@@ -459,8 +459,10 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
}
print_indent(ar);
render_node_grouped(ar, statement);
- if (i != node->data.block.statements.length - 1)
+ if (!(i == node->data.block.statements.length - 1 &&
+ node->data.block.last_statement_is_result_expression)) {
fprintf(ar->f, ";");
+ }
fprintf(ar->f, "\n");
}
ar->indent -= ar->indent_size;
diff --git a/src/ir.cpp b/src/ir.cpp
index bef2571186..07c77ebc81 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -3344,6 +3344,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
return ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
}
+ bool is_continuation_unreachable = false;
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);
@@ -3367,7 +3368,7 @@ 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 (!is_continuation_unreachable) {
// 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)));
@@ -3375,34 +3376,66 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
}
ir_set_cursor_at_end(irb, label_block);
+ // a label is an entry point
+ is_continuation_unreachable = false;
return_value = nullptr;
continue;
}
- if (return_value && instr_is_unreachable(return_value)) {
- if (is_node_void_expr(statement_node))
+ if (is_continuation_unreachable) {
+ // if you put a semicolon after a return statement,
+ // then we get a void statement in the unreachable area.
+ // this is fine. ignore any void blocks we get from this happening.
+ if (statement_node->type == NodeTypeBlock && statement_node->data.block.statements.length == 0)
continue;
add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));
}
- return_value = ir_gen_node(irb, statement_node, child_scope);
- if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {
+ IrInstruction *statement_value = ir_gen_node(irb, statement_node, child_scope);
+ is_continuation_unreachable = instr_is_unreachable(statement_value);
+ if (is_continuation_unreachable)
+ return_value = statement_value;
+ else
+ return_value = nullptr;
+ if (statement_node->type == NodeTypeDefer && statement_value != irb->codegen->invalid_instruction) {
// defer starts a new scope
child_scope = statement_node->data.defer.child_scope;
assert(child_scope);
- } else if (return_value->id == IrInstructionIdDeclVar) {
+ } else if (statement_value->id == IrInstructionIdDeclVar) {
// variable declarations start a new scope
- IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)return_value;
+ IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)statement_value;
child_scope = decl_var_instruction->var->child_scope;
+ } else {
+ // label, defer, variable declaration will never be the last statement
+ if (block_node->data.block.last_statement_is_result_expression &&
+ i == block_node->data.block.statements.length - 1) {
+ // this is the result value statement
+ return_value = statement_value;
+ } else {
+ // there are more statements ahead of this one. this statement's value must be void
+ TypeTableEntry *instruction_type = statement_value->value.type;
+ if (instruction_type &&
+ instruction_type->id != TypeTableEntryIdInvalid &&
+ instruction_type->id != TypeTableEntryIdVoid &&
+ instruction_type->id != TypeTableEntryIdUnreachable) {
+ add_node_error(irb->codegen, statement_node, buf_sprintf("expression valued ignored"));
+ }
+ }
}
}
- // labels are never the last statement
- assert(return_value != nullptr);
+ if (!is_continuation_unreachable) {
+ // control flow falls out of block
+
+ if (!block_node->data.block.last_statement_is_result_expression) {
+ assert(return_value == nullptr);
+ return_value = ir_mark_gen(ir_build_const_void(irb, child_scope, block_node));
+ }
- if (!instr_is_unreachable(return_value))
ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
+ }
+ assert(return_value != nullptr);
return return_value;
}
diff --git a/src/parser.cpp b/src/parser.cpp
index 6629e7bf89..d1184e9fd4 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -1878,36 +1878,6 @@ static AstNode *ast_parse_switch_expr(ParseContext *pc, size_t *token_index, boo
}
}
-static bool statement_has_block_body(AstNode *node) {
- switch (node->type) {
- case NodeTypeIfBoolExpr:
- if (node->data.if_bool_expr.else_node)
- return statement_has_block_body(node->data.if_bool_expr.else_node);
- return node->data.if_bool_expr.then_block->type == NodeTypeBlock;
- case NodeTypeIfVarExpr:
- if (node->data.if_var_expr.else_node)
- return statement_has_block_body(node->data.if_var_expr.else_node);
- return node->data.if_var_expr.then_block->type == NodeTypeBlock;
- case NodeTypeTryExpr:
- if (node->data.try_expr.else_node)
- return statement_has_block_body(node->data.try_expr.else_node);
- return node->data.try_expr.then_node->type == NodeTypeBlock;
- case NodeTypeWhileExpr:
- return node->data.while_expr.body->type == NodeTypeBlock;
- case NodeTypeForExpr:
- return node->data.for_expr.body->type == NodeTypeBlock;
- case NodeTypeSwitchExpr:
- case NodeTypeBlock:
- return true;
- case NodeTypeCompTime:
- return node->data.comptime_expr.expr->type == NodeTypeBlock;
- case NodeTypeDefer:
- return node->data.defer.expr->type == NodeTypeBlock;
- default:
- return false;
- }
-}
-
/*
BlockExpression(body) = Block | IfExpression(body) | TryExpression(body) | WhileExpression(body) | ForExpression(body) | SwitchExpression | CompTimeExpression(body)
*/
@@ -2120,9 +2090,35 @@ static AstNode *ast_parse_label(ParseContext *pc, size_t *token_index, bool mand
return node;
}
-static AstNode *ast_create_void_expr(ParseContext *pc, Token *token) {
- AstNode *node = ast_create_node(pc, NodeTypeBlock, token);
- return node;
+static bool statement_terminates_without_semicolon(AstNode *node) {
+ switch (node->type) {
+ case NodeTypeIfBoolExpr:
+ if (node->data.if_bool_expr.else_node)
+ return statement_terminates_without_semicolon(node->data.if_bool_expr.else_node);
+ return node->data.if_bool_expr.then_block->type == NodeTypeBlock;
+ case NodeTypeIfVarExpr:
+ if (node->data.if_var_expr.else_node)
+ return statement_terminates_without_semicolon(node->data.if_var_expr.else_node);
+ return node->data.if_var_expr.then_block->type == NodeTypeBlock;
+ case NodeTypeTryExpr:
+ if (node->data.try_expr.else_node)
+ return statement_terminates_without_semicolon(node->data.try_expr.else_node);
+ return node->data.try_expr.then_node->type == NodeTypeBlock;
+ case NodeTypeWhileExpr:
+ return node->data.while_expr.body->type == NodeTypeBlock;
+ case NodeTypeForExpr:
+ return node->data.for_expr.body->type == NodeTypeBlock;
+ case NodeTypeCompTime:
+ return node->data.comptime_expr.expr->type == NodeTypeBlock;
+ case NodeTypeDefer:
+ return node->data.defer.expr->type == NodeTypeBlock;
+ case NodeTypeSwitchExpr:
+ case NodeTypeBlock:
+ case NodeTypeLabel:
+ return true;
+ default:
+ return false;
+ }
}
/*
@@ -2145,56 +2141,36 @@ static AstNode *ast_parse_block(ParseContext *pc, size_t *token_index, bool mand
for (;;) {
AstNode *statement_node = ast_parse_label(pc, token_index, false);
- bool need_implicit_final_void_statement = false;
+ if (!statement_node)
+ statement_node = ast_parse_variable_declaration_expr(pc, token_index, false, VisibModPrivate);
+ if (!statement_node)
+ statement_node = ast_parse_defer_expr(pc, token_index);
+ if (!statement_node)
+ statement_node = ast_parse_block_expr(pc, token_index, false);
+ if (!statement_node)
+ statement_node = ast_parse_expression(pc, token_index, false);
+
bool semicolon_expected = true;
if (statement_node) {
- // label
- 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) {
- statement_node = ast_parse_defer_expr(pc, token_index);
- if (statement_node) {
- // defer
- if (statement_has_block_body(statement_node)) {
- // don't let defer be the last statement in a block
- need_implicit_final_void_statement = true;
- semicolon_expected = false;
- } else {
- // defer without a block body requires a semicolon
- Token *token = &pc->tokens->at(*token_index);
- ast_expect_token(pc, token, TokenIdSemicolon);
- }
- } else {
- statement_node = ast_parse_block_expr(pc, token_index, false);
- if (statement_node) {
- // block expr
- if (statement_has_block_body(statement_node))
- semicolon_expected = false;
- } else {
- statement_node = ast_parse_expression(pc, token_index, false);
- if (!statement_node) {
- // no statement.
- // final semicolon means add a void statement.
- need_implicit_final_void_statement = true;
- }
- }
+ node->data.block.statements.append(statement_node);
+ if (statement_terminates_without_semicolon(statement_node)) {
+ semicolon_expected = false;
+ } else {
+ if (statement_node->type == NodeTypeDefer) {
+ // defer without a block body requires a semicolon
+ Token *token = &pc->tokens->at(*token_index);
+ ast_expect_token(pc, token, TokenIdSemicolon);
}
}
}
- if (statement_node)
- node->data.block.statements.append(statement_node);
+
+ node->data.block.last_statement_is_result_expression = statement_node && !(
+ statement_node->type == NodeTypeLabel ||
+ statement_node->type == NodeTypeDefer);
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;