mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Instead of tracking the cleanup scope depth on entry to each scope, track an "ambient" cleanup scope depth that's *after* the destructors of local variables in that scope. This gets increased to include the destructors of local variables when we create a name-binding declaration. Then, when we reach a point where temporaries should be destroyed, run cleanups that are after the ambient cleanup scope depth on the stack. This happens: * At the `;` of a statement expression. * At the `)` of an `if` or `while` statement. * After performing the implied `HasValue()` call in a `for` statement. Per informal agreement with leads, this means we lifetime-extend all temporaries created in the initializer of a name-binding declaration to the full scope of that declaration, but that temporaries created in an expression statement are destroyed at the `;`.
28 lines
904 B
C++
28 lines
904 B
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/check/context.h"
|
|
#include "toolchain/check/control_flow.h"
|
|
#include "toolchain/check/handle.h"
|
|
#include "toolchain/check/unused.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
auto HandleParseNode(Context& context, Parse::CodeBlockStartId node_id)
|
|
-> bool {
|
|
context.node_stack().Push(node_id);
|
|
context.scope_stack().PushForSameRegion(ScopeStack::CleanupScopeKind::Owned);
|
|
return true;
|
|
}
|
|
|
|
auto HandleParseNode(Context& context, Parse::CodeBlockId /*node_id*/) -> bool {
|
|
AddAndDiscardScopeCleanups(context);
|
|
context.scope_stack().Pop(/*check_unused=*/true);
|
|
context.node_stack()
|
|
.PopAndDiscardSoloNodeId<Parse::NodeKind::CodeBlockStart>();
|
|
return true;
|
|
}
|
|
|
|
} // namespace Carbon::Check
|