Skip to content

Commit

Permalink
[cpp] codegen will now error out if you forget to return a value from…
Browse files Browse the repository at this point in the history
… a non-u0 function.
  • Loading branch information
harrand committed May 21, 2024
1 parent 7ef7af0 commit dd9f807
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
7 changes: 5 additions & 2 deletions cpp/src/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,12 @@ namespace build
// because its become a function that returns an integer, we *must* sneak in a return statement:
meta_region.children.front().children.push_back(ast::node
{
.payload = ast::return_statement
.payload = ast::expression
{
.expr = ast::expression{.expr = ast::integer_literal{.val = 0}}
.expr = ast::return_statement
{
.expr = ast::expression{.expr = ast::integer_literal{.val = 0}}
}
}
});
ret.root.children.push_back(meta_region);
Expand Down
13 changes: 10 additions & 3 deletions cpp/src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1229,18 +1229,25 @@ namespace code
const ast::node& child = node.children[i];
ast::path_t child_path = blk_path;
child_path.push_back(i);
if(std::holds_alternative<ast::return_statement>(child.payload))
if(std::holds_alternative<ast::expression>(child.payload) && std::holds_alternative<ast::return_statement>(std::get<ast::expression>(child.payload).expr))
{
has_return = true;
}
semal::context new_ctx = {.tree = d.ctx.tree, .path = child_path};
codegen_thing({.ctx = new_ctx, .state = d.state}, new_ctx.node().payload);
}

if(!has_return)
{
if(funcdata.return_ty.is_void())
{
// automatically add a return :) you're welcome
builder->CreateRetVoid();
}
d.ctx.error(error_code::codegen, "missing return value for function \"{}\"", payload.func_name);
}
if(!has_return && funcdata.return_ty.is_void())
{
// automatically add a return :) you're welcome
builder->CreateRetVoid();
}
}
llvm::verifyFunction(*llvm_fn);
Expand Down
3 changes: 2 additions & 1 deletion samples/scratchpad.psy
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ dub :: (val : i64) -> i64
{
putchar(myint@i8);
}
return (val * 2);
}

complicated :: (par1 : i64, par : i64, par3 : i64) -> i64
{
putchar(-par@i8);
return 0;
}

poggers : i64 := 5;
Expand Down Expand Up @@ -104,7 +106,6 @@ morb :: (par0 : i64) -> f64
return retval;
//return morb(5 * complicated(dub(5)));
}
morb(65);

== default : build ==
{
Expand Down

0 comments on commit dd9f807

Please sign in to comment.