mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 09:14:42 +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
@@ -16,8 +16,9 @@ auto HandleInfixOperator(Context& context, ParseTree::Node parse_node) -> bool {
|
||||
case TokenKind::Plus:
|
||||
// TODO: This should search for a compatible interface. For now, it's a
|
||||
// very trivial check of validity on the operation.
|
||||
lhs_id = context.ImplicitAsRequired(
|
||||
lhs_id = context.ConvertToValueOfType(
|
||||
parse_node, lhs_id, context.semantics_ir().GetNode(rhs_id).type_id());
|
||||
rhs_id = context.ConvertToValueExpression(rhs_id);
|
||||
|
||||
context.AddNodeAndPush(
|
||||
parse_node,
|
||||
@@ -31,7 +32,7 @@ auto HandleInfixOperator(Context& context, ParseTree::Node parse_node) -> bool {
|
||||
// The first operand is wrapped in a ShortCircuitOperand, which we
|
||||
// already handled by creating a RHS block and a resumption block, which
|
||||
// are the current block and its enclosing block.
|
||||
rhs_id = context.ImplicitAsBool(parse_node, rhs_id);
|
||||
rhs_id = context.ConvertToBoolValue(parse_node, rhs_id);
|
||||
|
||||
// When the second operand is evaluated, the result of `and` and `or` is
|
||||
// its value.
|
||||
@@ -58,10 +59,12 @@ auto HandleInfixOperator(Context& context, ParseTree::Node parse_node) -> bool {
|
||||
"Expression is not assignable.");
|
||||
context.emitter().Emit(lhs_node, AssignmentToNonAssignable);
|
||||
}
|
||||
context.ImplicitAsRequired(
|
||||
parse_node, rhs_id, context.semantics_ir().GetNode(lhs_id).type_id());
|
||||
context.AddNodeAndPush(
|
||||
parse_node, SemIR::Node::Assign::Make(parse_node, lhs_id, rhs_id));
|
||||
context.Initialize(parse_node, lhs_id, rhs_id);
|
||||
// We model assignment as an expression, so we need to push a value for
|
||||
// it, even though it doesn't produce a value.
|
||||
// TODO: Consider changing our parse tree to model assignment as a
|
||||
// different kind of statement than an expression statement.
|
||||
context.node_stack().Push(parse_node, lhs_id);
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
@@ -143,7 +146,7 @@ auto HandlePrefixOperator(Context& context, ParseTree::Node parse_node)
|
||||
}
|
||||
|
||||
case TokenKind::Not:
|
||||
value_id = context.ImplicitAsBool(parse_node, value_id);
|
||||
value_id = context.ConvertToBoolValue(parse_node, value_id);
|
||||
context.AddNodeAndPush(
|
||||
parse_node,
|
||||
SemIR::Node::UnaryOperatorNot::Make(
|
||||
@@ -176,6 +179,7 @@ auto HandlePrefixOperator(Context& context, ParseTree::Node parse_node)
|
||||
}
|
||||
builder.Emit();
|
||||
}
|
||||
value_id = context.ConvertToValueExpression(value_id);
|
||||
context.AddNodeAndPush(
|
||||
parse_node,
|
||||
SemIR::Node::Dereference::Make(parse_node, result_type_id, value_id));
|
||||
@@ -191,7 +195,7 @@ auto HandleShortCircuitOperand(Context& context, ParseTree::Node parse_node)
|
||||
-> bool {
|
||||
// Convert the condition to `bool`.
|
||||
auto cond_value_id = context.node_stack().PopExpression();
|
||||
cond_value_id = context.ImplicitAsBool(parse_node, cond_value_id);
|
||||
cond_value_id = context.ConvertToBoolValue(parse_node, cond_value_id);
|
||||
auto bool_type_id = context.semantics_ir().GetNode(cond_value_id).type_id();
|
||||
|
||||
// Compute the branch value: the condition for `and`, inverted for `or`.
|
||||
|
||||
Reference in New Issue
Block a user