mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:30:12 +01:00
When parsing a pattern, if we encounter something that isn't pattern syntax, try parsing as an expression instead. We only need one-token lookahead to distinguish pattern syntax from expression syntax. Track a precedence group through pattern parsing so that we can allow different kinds of expressions in a top-level pattern (such as the operand of `let`) and in a nested pattern (such as a subpattern of a tuple pattern or within grouping parens). For example, we do not allow `case if ...`, and for now I've chosen to also not allow logical or relational operators at the top level of a pattern, so `case 1 + 1` is OK, but `case 1 == 1` and `case true and false` require parentheses. This decision should be ratified or revisited by a design proposal. Very basic check support is also provided, only sufficient to form an `ExprPattern` instruction and nothing beyond that. For now, all pattern matching against an `ExprPattern` fails with a TODO error. To support that, I've switched from calling `BeginSubpattern` in the parent handler of a pattern and `EndSubpatternAs*` in the pattern handler itself to calling both functions in parent handlers, with `EndSubpattern` converting an expression into an expression pattern where needed. Depends on #6976. Assisted-by: Gemini via Google Antigravity
92 lines
3.0 KiB
C++
92 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
|
|
|
|
#ifndef CARBON_TOOLCHAIN_CHECK_REGION_STACK_H_
|
|
#define CARBON_TOOLCHAIN_CHECK_REGION_STACK_H_
|
|
|
|
#include <utility>
|
|
|
|
#include "common/array_stack.h"
|
|
#include "toolchain/check/diagnostic_helpers.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Provides a stack of single-entry regions being built.
|
|
class RegionStack {
|
|
public:
|
|
// A callback for Context::TODO.
|
|
using TodoFn = std::function<auto(SemIR::LocId, std::string)->void>;
|
|
|
|
explicit RegionStack(TodoFn todo_fn) : todo_fn_(std::move(todo_fn)) {}
|
|
|
|
// Mark the start of a new single-entry region with the given entry block.
|
|
auto PushRegion(SemIR::InstBlockId entry_block_id) -> void {
|
|
stack_.PushArray();
|
|
stack_.AppendToTop(entry_block_id);
|
|
}
|
|
|
|
// Mark the start of a new empty, unreachable region.
|
|
auto PushUnreachableRegion() -> void { stack_.PushArray(); }
|
|
|
|
// Add `block_id` to the most recently pushed single-entry region. To preserve
|
|
// the single-entry property, `block_id` must not be directly reachable from
|
|
// any block outside the region. To ensure the region's blocks are in lexical
|
|
// order, this should be called when the first parse node associated with this
|
|
// block is handled, or as close as possible.
|
|
auto AddToRegion(SemIR::InstBlockId block_id, SemIR::LocId loc_id) -> void {
|
|
if (stack_.empty()) {
|
|
todo_fn_(loc_id,
|
|
"Control flow expressions are currently only supported inside "
|
|
"functions.");
|
|
return;
|
|
}
|
|
if (block_id == SemIR::InstBlockId::Unreachable) {
|
|
return;
|
|
}
|
|
|
|
stack_.AppendToTop(block_id);
|
|
}
|
|
|
|
// Adds multiple blocks at once. The caller is responsible for validating that
|
|
// each block is reachable.
|
|
auto AddToRegion(llvm::ArrayRef<SemIR::InstBlockId> block_ids) -> void {
|
|
stack_.AppendToTop(block_ids);
|
|
}
|
|
|
|
// Complete creation of the most recently pushed single-entry region, and
|
|
// return a list of its blocks.
|
|
auto PopRegion() -> llvm::SmallVector<SemIR::InstBlockId> {
|
|
llvm::SmallVector<SemIR::InstBlockId> result(stack_.PeekArray());
|
|
stack_.PopArray();
|
|
return result;
|
|
}
|
|
|
|
// Pops a region, and does not return its contents.
|
|
auto PopAndDiscardRegion() -> void { stack_.PopArray(); }
|
|
|
|
// Returns the top-most region.
|
|
auto PeekRegion() -> llvm::ArrayRef<SemIR::InstBlockId> {
|
|
return stack_.PeekArray();
|
|
}
|
|
|
|
// Runs verification that the processing cleanly finished.
|
|
auto VerifyOnFinish() const -> void {
|
|
CARBON_CHECK(stack_.empty(), "region_stack still has {0} entries",
|
|
stack_.all_values_size());
|
|
}
|
|
|
|
// Returns true if any regions have been added.
|
|
auto empty() -> bool { return stack_.empty(); }
|
|
|
|
private:
|
|
TodoFn todo_fn_;
|
|
|
|
ArrayStack<SemIR::InstBlockId> stack_;
|
|
};
|
|
|
|
} // namespace Carbon::Check
|
|
|
|
#endif // CARBON_TOOLCHAIN_CHECK_REGION_STACK_H_
|