From 1ed926c3216016d73e83377a295869d8579e2fde Mon Sep 17 00:00:00 2001 From: Josh Wolfe Date: Tue, 1 Dec 2015 15:54:46 -0700 Subject: implicit void statements and all tests pass with type checking --- src/codegen.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/codegen.cpp') diff --git a/src/codegen.cpp b/src/codegen.cpp index ab74ae19ce..18a5c4c5a1 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -401,6 +401,8 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) { case NodeTypeUnreachable: add_debug_source_node(g, node); return LLVMBuildUnreachable(g->builder); + case NodeTypeVoid: + return nullptr; case NodeTypeNumberLiteral: { Buf *number_str = &node->data.number; @@ -441,7 +443,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *node) { zig_unreachable(); } -static void gen_block(CodeGen *g, ImportTableEntry *import, AstNode *block_node, bool add_implicit_return) { +static void gen_block(CodeGen *g, ImportTableEntry *import, AstNode *block_node, TypeTableEntry *implicit_return_type) { assert(block_node->type == NodeTypeBlock); LLVMZigDILexicalBlock *di_block = LLVMZigCreateLexicalBlock(g->dbuilder, g->block_scopes.last(), @@ -450,13 +452,16 @@ static void gen_block(CodeGen *g, ImportTableEntry *import, AstNode *block_node, add_debug_source_node(g, block_node); + LLVMValueRef return_value; for (int i = 0; i < block_node->data.block.statements.length; i += 1) { AstNode *statement_node = block_node->data.block.statements.at(i); - gen_expr(g, statement_node); + return_value = gen_expr(g, statement_node); } - if (add_implicit_return) { + if (implicit_return_type == g->builtin_types.entry_void) { LLVMBuildRetVoid(g->builder); + } else if (implicit_return_type != g->builtin_types.entry_unreachable) { + LLVMBuildRet(g->builder, return_value); } g->block_scopes.pop(); @@ -552,8 +557,8 @@ static void do_code_gen(CodeGen *g) { codegen_fn_def->params = allocate(LLVMCountParams(fn)); LLVMGetParams(fn, codegen_fn_def->params); - bool add_implicit_return = codegen_fn_def->add_implicit_return; - gen_block(g, import, fn_def_node->data.fn_def.body, add_implicit_return); + TypeTableEntry *implicit_return_type = codegen_fn_def->implicit_return_type; + gen_block(g, import, fn_def_node->data.fn_def.body, implicit_return_type); g->block_scopes.pop(); } -- cgit v1.2.3