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) {...@@ -2987,23 +2987,6 @@ void semantic_analyze(CodeGen *g) {
2987 }2987 }
2988}2988}
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
3007TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {2990TypeTableEntry **get_int_type_ptr(CodeGen *g, bool is_signed, uint32_t size_in_bits) {
3008 size_t index;2991 size_t index;
3009 if (size_in_bits == 8) {2992 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/ir.cpp+36-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,33 +3376,58 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3375,33 +3376,58 @@ 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
3378 return_value = nullptr;3379 // a label is an entry point
3380 is_continuation_unreachable = false;
3379 continue;3381 continue;
3380 }3382 }
33813383
3382 if (return_value && instr_is_unreachable(return_value)) {3384 if (is_continuation_unreachable) {
3383 if (is_node_void_expr(statement_node))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)
3384 continue;3389 continue;
3385 add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));3390 add_node_error(irb->codegen, statement_node, buf_sprintf("unreachable code"));
3386 }3391 }
33873392
3388 return_value = ir_gen_node(irb, statement_node, child_scope);3393 IrInstruction *statement_value = ir_gen_node(irb, statement_node, child_scope);
3389 if (statement_node->type == NodeTypeDefer && return_value != irb->codegen->invalid_instruction) {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) {
3390 // defer starts a new scope3400 // defer starts a new scope
3391 child_scope = statement_node->data.defer.child_scope;3401 child_scope = statement_node->data.defer.child_scope;
3392 assert(child_scope);3402 assert(child_scope);
3393 } else if (return_value->id == IrInstructionIdDeclVar) {3403 } else if (statement_value->id == IrInstructionIdDeclVar) {
3394 // variable declarations start a new scope3404 // variable declarations start a new scope
3395 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)return_value;3405 IrInstructionDeclVar *decl_var_instruction = (IrInstructionDeclVar *)statement_value;
3396 child_scope = decl_var_instruction->var->child_scope;3406 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 }
3397 }3422 }
3398 }3423 }
33993424
3400 // labels are never the last statement
3401 assert(return_value != nullptr);3425 assert(return_value != nullptr);
34023426
3403 if (!instr_is_unreachable(return_value))3427 if (!is_continuation_unreachable) {
3428 // control flow falls out of block
3404 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);3429 ir_gen_defers_for_block(irb, child_scope, outer_block_scope, false, false);
3430 }
34053431
3406 return return_value;3432 return return_value;
3407}3433}