Skip to content

Commit

Permalink
[cpp] struct default initialisers are now respected, and can be mixed…
Browse files Browse the repository at this point in the history
…-and-matched as you please when making a struct initialiser. if only c++20 bothered to do the same
  • Loading branch information
harrand committed May 23, 2024
1 parent 5e168fb commit 5bd32eb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 12 additions & 1 deletion cpp/src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1422,9 +1422,20 @@ namespace code
d.ctx.assert_that(var != nullptr, error_code::codegen,"could not find local variable \"{}\"", payload.var_name);
llvm::Type* llvm_ty = as_llvm_type(var->ty, d.state);
llvm::AllocaInst* llvm_var = builder->CreateAlloca(llvm_ty, nullptr, payload.var_name);
value init_value;
if(payload.initialiser.has_value())
{
value init_value = expression(d, *payload.initialiser.value());
init_value = expression(d, *payload.initialiser.value());
}
else if(var->ty.is_struct())
{
// if a struct wasn't given an initialiser, give them an empty struct initialiser.
// otherwise default struct member initialisers will be ignored.
init_value = struct_initialiser(d, {.name = payload.type_name, .designated_initialisers = {}});
d.ctx.assert_that(init_value.llv != nullptr, error_code::codegen, "default struct initialiser yielded nullptr.");
}
if(init_value.llv != nullptr)
{
if(init_value.is_variable)
{
init_value = get_variable_val(init_value, d);
Expand Down
10 changes: 6 additions & 4 deletions samples/scratchpad.psy
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ get_fox :: () -> animal
//ret.id = 5;
//return ret;

return animal
{
.id := 8
};
//return animal
//{
// .id := 8
//};
ret : animal;
return ret;
}

puts :: (str : i8&) -> u0 := extern;
Expand Down

0 comments on commit 5bd32eb

Please sign in to comment.