Files
carbon-lang/toolchain/semantics/semantics_handle_variable.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

55 lines
1.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 "toolchain/semantics/semantics_context.h"
#include "toolchain/semantics/semantics_node.h"
namespace Carbon::Check {
auto HandleVariableDeclaration(Context& context, ParseTree::Node parse_node)
-> bool {
// Handle the optional initializer.
auto expr_node_id = SemIR::NodeId::Invalid;
bool has_init =
context.parse_tree().node_kind(context.node_stack().PeekParseNode()) !=
ParseNodeKind::PatternBinding;
if (has_init) {
expr_node_id = context.node_stack().PopExpression();
context.node_stack()
.PopAndDiscardSoloParseNode<ParseNodeKind::VariableInitializer>();
}
// Get the storage and add it to name lookup.
SemIR::NodeId var_id =
context.node_stack().Pop<ParseNodeKind::PatternBinding>();
auto var = context.semantics_ir().GetNode(var_id);
auto name_id = var.GetAsVarStorage();
context.AddNameToLookup(var.parse_node(), name_id, var_id);
// If there was an initializer, assign it to storage.
if (has_init) {
context.Initialize(parse_node, var_id, expr_node_id);
}
context.node_stack()
.PopAndDiscardSoloParseNode<ParseNodeKind::VariableIntroducer>();
return true;
}
auto HandleVariableIntroducer(Context& context, ParseTree::Node parse_node)
-> bool {
// No action, just a bracketing node.
context.node_stack().Push(parse_node);
return true;
}
auto HandleVariableInitializer(Context& context, ParseTree::Node parse_node)
-> bool {
// No action, just a bracketing node.
context.node_stack().Push(parse_node);
return true;
}
} // namespace Carbon::Check