authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-04-12 16:49:47-07:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2017-04-12 22:21:35-07:00
log919910312d7d25fe8f894adbc9ff8a449ac26281
tree20a8c1aaf16ce227f95fe53d0a584133b37c5f84
parentbf67427c67dac00ca10ed7423ae8d99e2901262f

make it an error to ignore a statement's value

this makes {1;} an error.

3 files changed, 36 insertions(+), 28 deletions(-)

src/analyze.cpp-17
......@@ -2987,23 +2987,6 @@ void semantic_analyze(CodeGen *g) {
29872987 }
29882988}
29892989
2990bool is_node_void_expr(AstNode *node) {
2991 if (node->type == NodeTypeContainerInitExpr &&
2992 node->data.container_init_expr.kind == ContainerInitKindArray)
2993 {
2994 AstNode *type_node = node->data.container_init_expr.type;
2995 if (type_node->type == NodeTypeSymbol &&
2996 buf_eql_str(type_node->data.symbol_expr.symbol, "void"))
2997 {
2998 return true;
2999 }
3000 } else if (node->type == NodeTypeBlock && node->data.block.statements.length == 0) {
3001 return true;
3002 }
3003
3004 return false;
3005}
3006
30072990TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
30082991 size_t index;
30092992 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/ir.cpp+36-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,33 +3376,58 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
33753376 }
33763377 ir_set_cursor_at_end(irb, label_block);
33773378
3378 return_value = nullptr;
3379 // a label is an entry point
3380 is_continuation_unreachable = false;
33793381 continue;
33803382 }
33813383
3382 if (return_value && instr_is_unreachable(return_value)) {
3383 if (is_node_void_expr(statement_node))
3384 if (is_continuation_unreachable) {
3385 // if you put a semicolon after a return statement,
3386 // then we get a void statement in the unreachable area.
3387 // this is fine. ignore any void blocks we get from this happening.
3388 if (statement_node->type == NodeTypeBlock && statement_node->data.block.statements.length == 0)
33843389 continue;
33853390 add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));
33863391 }
33873392
3388 return_value = ir_gen_node(irb, statement_node, child_scope);
3389 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {
3393 IrInstruction *statement_value = ir_gen_node(irb, statement_node, child_scope);
3394 is_continuation_unreachable = instr_is_unreachable(statement_value);
3395 if (is_continuation_unreachable)
3396 return_value = statement_value;
3397 else
3398 return_value = nullptr;
3399 if (statement_node->type == NodeTypeDefer && statement_value != irb->codegen->invalid_instruction) {
33903400 // defer starts a new scope
33913401 child_scope = statement_node->data.defer.child_scope;
33923402 assert(child_scope);
3393 } else if (return_value->id == IrInstructionIdDeclVar) {
3403 } else if (statement_value->id == IrInstructionIdDeclVar) {
33943404 // variable declarations start a new scope
3395 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)return_value;
3405 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)statement_value;
33963406 child_scope = decl_var_instruction->var->child_scope;
3407 } else {
3408 // label, defer, variable declaration will never be the last statement
3409 if (i == block_node->data.block.statements.length - 1) {
3410 // this is the result value statement
3411 return_value = statement_value;
3412 } else {
3413 // there are more statements ahead of this one. this statement's value must be void
3414 TypeTableEntry *instruction_type = statement_value->value.type;
3415 if (instruction_type &&
3416 instruction_type->id != TypeTableEntryIdInvalid &&
3417 instruction_type->id != TypeTableEntryIdVoid &&
3418 instruction_type->id != TypeTableEntryIdUnreachable) {
3419 add_node_error(irb->codegen, statement_node, buf_sprintf("expression valued ignored"));
3420 }
3421 }
33973422 }
33983423 }
33993424
3400 // labels are never the last statement
34013425 assert(return_value != nullptr);
34023426
3403 if (!instr_is_unreachable(return_value))
3427 if (!is_continuation_unreachable) {
3428 // control flow falls out of block
34043429 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
3430 }
34053431
34063432 return return_value;
34073433}