Model function calls as initializing expressions (#3089)

Start treating function calls as initializing expressions instead of as
value expressions.

This required adding support for expression categories. Value bindings
and temporary materialization conversions are created where necessary to
transition between expression categories. For a function call with a
return slot, we speculatively create a materialized temporary before the
call and either commit to it or replace it with something else later,
once we see how the function call expression is actually used.

This change follows the direction suggested in #3133 for initializing
expressions: depending on the return type of a function, the return
value will either be initialized in-place or returned directly. This is
visible in the semantics IR, which is a little unfortunate but is
probably necessary as this is part of the semantics of the program.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Richard Smith
2023-08-24 19:35:07 +00:00
committed by GitHub
co-authored by Chandler Carruth
parent f790a27ace
commit 1013d1773c
101 changed files with 1802 additions and 777 deletions
@@ -41,13 +41,25 @@ auto HandleCallExpression(Context& context, ParseTree::Node parse_node)
callable.param_refs_id,
/*diagnostic=*/nullptr));
// TODO: Propagate return types from callable.
SemIR::TypeId type_id = callable.return_type_id;
// For functions with an implicit return type, set the return type to empty
// For functions with an implicit return type, the return type is the empty
// tuple type.
if (type_id == SemIR::TypeId::Invalid) {
SemIR::TypeId type_id = callable.return_type_id;
if (!type_id.is_valid()) {
type_id = context.CanonicalizeTupleType(call_expr_parse_node, {});
}
// If there is a return slot, add a corresponding argument.
if (callable.return_slot_id.is_valid()) {
if (refs_id == SemIR::NodeBlockId::Empty) {
refs_id = context.semantics_ir().AddNodeBlock();
}
// Tentatively put a materialized temporary in the function's return slot.
// This will be replaced if necessary when we perform initialization.
auto return_slot_id =
context.AddNode(SemIR::Node::MaterializeTemporary::Make(
call_expr_parse_node, callable.return_type_id));
context.semantics_ir().GetNodeBlock(refs_id).push_back(return_slot_id);
}
auto call_node_id = context.AddNode(SemIR::Node::Call::Make(
call_expr_parse_node, type_id, refs_id, function_id));