mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-04 22:02:52 +01:00
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:
co-authored by
Chandler Carruth
parent
f790a27ace
commit
1013d1773c
@@ -20,11 +20,21 @@ static auto BuildFunctionDeclaration(Context& context)
|
||||
// prior to the call.
|
||||
context.node_block_stack().Pop();
|
||||
|
||||
SemIR::TypeId return_type_id = SemIR::TypeId::Invalid;
|
||||
auto return_type_id = SemIR::TypeId::Invalid;
|
||||
auto return_slot_id = SemIR::NodeId::Invalid;
|
||||
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
|
||||
ParseNodeKind::ReturnType) {
|
||||
return_type_id = context.node_stack().Pop<ParseNodeKind::ReturnType>();
|
||||
return_slot_id = context.node_stack().Pop<ParseNodeKind::ReturnType>();
|
||||
return_type_id = context.semantics_ir().GetNode(return_slot_id).type_id();
|
||||
|
||||
// The function only has a return slot if it uses in-place initialization.
|
||||
if (!SemIR::GetInitializingRepresentation(context.semantics_ir(),
|
||||
return_type_id)
|
||||
.has_return_slot()) {
|
||||
return_slot_id = SemIR::NodeId::Invalid;
|
||||
}
|
||||
}
|
||||
|
||||
SemIR::NodeBlockId param_refs_id =
|
||||
context.node_stack().Pop<ParseNodeKind::ParameterList>();
|
||||
auto name_context = context.declaration_name_stack().Pop();
|
||||
@@ -42,6 +52,7 @@ static auto BuildFunctionDeclaration(Context& context)
|
||||
: SemIR::StringId(SemIR::StringId::InvalidIndex),
|
||||
.param_refs_id = param_refs_id,
|
||||
.return_type_id = return_type_id,
|
||||
.return_slot_id = return_slot_id,
|
||||
.body_block_ids = {}});
|
||||
auto decl_id = context.AddNode(
|
||||
SemIR::Node::FunctionDeclaration::Make(fn_node, function_id));
|
||||
@@ -121,8 +132,12 @@ auto HandleReturnType(Context& context, ParseTree::Node parse_node) -> bool {
|
||||
// Propagate the type expression.
|
||||
auto [type_parse_node, type_node_id] =
|
||||
context.node_stack().PopExpressionWithParseNode();
|
||||
auto cast_node_id = context.ExpressionAsType(type_parse_node, type_node_id);
|
||||
context.node_stack().Push(parse_node, cast_node_id);
|
||||
auto type_id = context.ExpressionAsType(type_parse_node, type_node_id);
|
||||
// TODO: Use a dedicated node rather than VarStorage here.
|
||||
context.AddNodeAndPush(
|
||||
parse_node,
|
||||
SemIR::Node::VarStorage::Make(
|
||||
parse_node, type_id, context.semantics_ir().AddString("return")));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user