mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Refactor subpattern logic out of Context (#4929)
Note this is building on #4927 which factors out RegionStack, because this heavily uses the region stack. However, it's a more complex level of logic that appears specific to pattern handling. I've moved InsertHere to pattern_match.cpp because it appears to be in specific use there.
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
// 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/subpattern.h"
|
||||
|
||||
namespace Carbon::Check {
|
||||
|
||||
auto BeginSubpattern(Context& context) -> void {
|
||||
context.inst_block_stack().Push();
|
||||
context.region_stack().PushRegion(context.inst_block_stack().PeekOrAdd());
|
||||
}
|
||||
|
||||
auto EndSubpatternAsExpr(Context& context, SemIR::InstId result_id)
|
||||
-> SemIR::ExprRegionId {
|
||||
if (context.region_stack().PeekRegion().size() > 1) {
|
||||
// End the exit block with a branch to a successor block, whose contents
|
||||
// will be determined later.
|
||||
context.AddInst(SemIR::LocIdAndInst::NoLoc<SemIR::Branch>(
|
||||
{.target_id = context.inst_blocks().AddDefaultValue()}));
|
||||
} else {
|
||||
// This single-block region will be inserted as a SpliceBlock, so we don't
|
||||
// need control flow out of it.
|
||||
}
|
||||
auto block_id = context.inst_block_stack().Pop();
|
||||
CARBON_CHECK(block_id == context.region_stack().PeekRegion().back());
|
||||
|
||||
// TODO: Is it possible to validate that this region is genuinely
|
||||
// single-entry, single-exit?
|
||||
return context.sem_ir().expr_regions().Add(
|
||||
{.block_ids = context.region_stack().PopRegion(),
|
||||
.result_id = result_id});
|
||||
}
|
||||
|
||||
auto EndSubpatternAsEmpty(Context& context) -> void {
|
||||
auto block_id = context.inst_block_stack().Pop();
|
||||
CARBON_CHECK(block_id == context.region_stack().PeekRegion().back());
|
||||
CARBON_CHECK(context.region_stack().PeekRegion().size() == 1);
|
||||
CARBON_CHECK(context.inst_blocks().Get(block_id).empty());
|
||||
context.region_stack().PopAndDiscardRegion();
|
||||
}
|
||||
|
||||
} // namespace Carbon::Check
|
||||
Reference in New Issue
Block a user