Files
carbon-lang/toolchain/semantics/semantics_handle_paren.cpp
T
Richard SmithandChandler Carruth 1013d1773c 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>
2023-08-24 19:35:07 +00:00

77 lines
2.7 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <utility>
#include "toolchain/semantics/semantics_context.h"
namespace Carbon::Check {
auto HandleParenExpression(Context& context, ParseTree::Node parse_node)
-> bool {
auto value_id = context.node_stack().PopExpression();
// ParamOrArgStart was called for tuple handling; clean up the ParamOrArg
// support for non-tuple cases.
context.ParamOrArgEnd(
/*for_args=*/true, ParseNodeKind::ParenExpressionOrTupleLiteralStart);
context.node_stack()
.PopAndDiscardSoloParseNode<
ParseNodeKind::ParenExpressionOrTupleLiteralStart>();
context.node_stack().Push(parse_node, value_id);
return true;
}
auto HandleParenExpressionOrTupleLiteralStart(Context& context,
ParseTree::Node parse_node)
-> bool {
context.node_stack().Push(parse_node);
context.ParamOrArgStart();
return true;
}
static auto HandleTupleLiteralElement(Context& context) -> void {
// Convert the operand to a value.
// TODO: We need to decide how tuple literals interact with expression
// categories.
auto [value_node, value_id] =
context.node_stack().PopExpressionWithParseNode();
value_id = context.ConvertToValueExpression(value_id);
context.node_stack().Push(value_node, value_id);
}
auto HandleTupleLiteralComma(Context& context, ParseTree::Node /*parse_node*/)
-> bool {
HandleTupleLiteralElement(context);
context.ParamOrArgComma(/*for_args=*/true);
return true;
}
auto HandleTupleLiteral(Context& context, ParseTree::Node parse_node) -> bool {
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
ParseNodeKind::ParenExpressionOrTupleLiteralStart) {
HandleTupleLiteralElement(context);
}
auto refs_id = context.ParamOrArgEnd(
/*for_args=*/true, ParseNodeKind::ParenExpressionOrTupleLiteralStart);
context.node_stack()
.PopAndDiscardSoloParseNode<
ParseNodeKind::ParenExpressionOrTupleLiteralStart>();
const auto& node_block = context.semantics_ir().GetNodeBlock(refs_id);
llvm::SmallVector<SemIR::TypeId> type_ids;
type_ids.reserve(node_block.size());
for (auto node : node_block) {
type_ids.push_back(context.semantics_ir().GetNode(node).type_id());
}
auto type_id = context.CanonicalizeTupleType(parse_node, std::move(type_ids));
auto value_id = context.AddNode(
SemIR::Node::TupleValue::Make(parse_node, type_id, refs_id));
context.node_stack().Push(parse_node, value_id);
return true;
}
} // namespace Carbon::Check