mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-16 02:18:12 +00:00
Switch from a fully lowering-oriented approach to initializing
expressions in semantics IR to an approach that retains more information
about the high-level semantics of the program. Specifically:
- Always form an `Assign` node for a variable initialization or
assignment, even for empty types and when the actual initialization is
performed in-place by evaluating the right-hand side.
- Always form a `ReturnExpression` node for a return statement with an
expression, even when the actual initialization is performed in-place
into the return slot and the function doesn't return a value.
This is intended to permit us to more easily write semantic checks over
the IR, and to preserve a use edge from the use of an initializing
expression and its initializer in all cases.
To better support this, `MaterializeTemporary` is also split into two
nodes:
- A `TemporaryStorage` node allocates and provides the storage for a
temporary, and is emitted prior to emitting the initialization of the
temporary.
- A `Temporary` node connects the temporary storage to the initializer,
and is emitted after emitting the initialization.
Example testcase:
```carbon
fn F() -> (i32, i32);
fn G() -> (i32, i32) {
var v: (i32, i32) = F();
v = F();
return F();
}
fn H() -> i32 {
return G()[0];
}
```
Before:
```carbon-semir
fn @G() -> %return: (i32, i32) {
// ...
%v: (i32, i32) = var "v"
%.loc4_24: (i32, i32) = call @F() to %v
%.loc5: (i32, i32) = call @F() to %v
%.loc6: (i32, i32) = call @F() to %return
return
}
fn @H() -> i32 {
!entry:
%.loc10_11.1: (i32, i32) = materialize_temporary
%.loc10_11.2: (i32, i32) = call @G() to %.loc10_11.1
%.loc10_14: i32 = int_literal 0
%.loc10_15.1: i32 = tuple_index %.loc10_11.1, %.loc10_14
%.loc10_15.2: i32 = bind_value %.loc10_15.1
return %.loc10_15.2
}
```
After:
```carbon-semir
fn @G() -> %return: (i32, i32) {
// ...
%v: (i32, i32) = var "v"
%.loc4_24: (i32, i32) = call @F() to %v
assign %v, %.loc4_24
%.loc5: (i32, i32) = call @F() to %v
assign %v, %.loc5
%.loc6: (i32, i32) = call @F() to %return
return %.loc6
}
fn @H() -> i32 {
!entry:
%.loc10_11.1: (i32, i32) = temporary_storage
%.loc10_11.2: (i32, i32) = call @G() to %.loc10_11.1
%.loc10_14: i32 = int_literal 0
%.loc10_11.3: (i32, i32) = temporary %.loc10_11.1, %.loc10_11.2
%.loc10_15.1: i32 = tuple_index %.loc10_11.3, %.loc10_14
%.loc10_15.2: i32 = bind_value %.loc10_15.1
return %.loc10_15.2
}
```
Note that in the "Before" code, there is no indication in `G()` that we
assigned to `v`, and no path through "use" edges from the tuple indexing
in `H()` to the call to `G()`.
This change results in our not performing `memcpy`s into the destination
for tuple and struct initializers. This is a consequence of those
initializers not yet performing in-place initialization, and will be
fixed in a subsequent change.
---------
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
80 lines
3.0 KiB
C++
80 lines
3.0 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 HandleExpressionStatement(Context& context, Parse::Node /*parse_node*/)
|
|
-> bool {
|
|
context.HandleDiscardedExpression(context.node_stack().PopExpression());
|
|
return true;
|
|
}
|
|
|
|
auto HandleReturnStatement(Context& context, Parse::Node parse_node) -> bool {
|
|
CARBON_CHECK(!context.return_scope_stack().empty());
|
|
const auto& fn_node =
|
|
context.semantics_ir().GetNode(context.return_scope_stack().back());
|
|
const auto& callable =
|
|
context.semantics_ir().GetFunction(fn_node.GetAsFunctionDeclaration());
|
|
|
|
if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
|
|
Parse::NodeKind::ReturnStatementStart) {
|
|
context.node_stack()
|
|
.PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
|
|
|
|
if (callable.return_type_id.is_valid()) {
|
|
// TODO: Add a note pointing at the return type's parse node.
|
|
CARBON_DIAGNOSTIC(ReturnStatementMissingExpression, Error,
|
|
"Must return a {0}.", std::string);
|
|
context.emitter()
|
|
.Build(parse_node, ReturnStatementMissingExpression,
|
|
context.semantics_ir().StringifyType(callable.return_type_id))
|
|
.Emit();
|
|
}
|
|
|
|
context.AddNode(SemIR::Node::Return::Make(parse_node));
|
|
} else {
|
|
auto arg = context.node_stack().PopExpression();
|
|
context.node_stack()
|
|
.PopAndDiscardSoloParseNode<Parse::NodeKind::ReturnStatementStart>();
|
|
|
|
if (!callable.return_type_id.is_valid()) {
|
|
CARBON_DIAGNOSTIC(
|
|
ReturnStatementDisallowExpression, Error,
|
|
"No return expression should be provided in this context.");
|
|
CARBON_DIAGNOSTIC(ReturnStatementImplicitNote, Note,
|
|
"There was no return type provided.");
|
|
context.emitter()
|
|
.Build(parse_node, ReturnStatementDisallowExpression)
|
|
.Note(fn_node.parse_node(), ReturnStatementImplicitNote)
|
|
.Emit();
|
|
} else if (callable.return_slot_id.is_valid()) {
|
|
arg = context.Initialize(parse_node, callable.return_slot_id, arg);
|
|
} else {
|
|
arg = context.ConvertToValueOfType(parse_node, arg,
|
|
callable.return_type_id);
|
|
}
|
|
|
|
context.AddNode(SemIR::Node::ReturnExpression::Make(parse_node, arg));
|
|
}
|
|
|
|
// Switch to a new, unreachable, empty node block. This typically won't
|
|
// contain any semantics IR, but it can do if there are statements following
|
|
// the `return` statement.
|
|
context.node_block_stack().Pop();
|
|
context.node_block_stack().PushUnreachable();
|
|
return true;
|
|
}
|
|
|
|
auto HandleReturnStatementStart(Context& context, Parse::Node parse_node)
|
|
-> bool {
|
|
// No action, just a bracketing node.
|
|
context.node_stack().Push(parse_node);
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|