Skip to content

Commit

Permalink
Avoid walking the node stack
Browse files Browse the repository at this point in the history
  • Loading branch information
danakj committed Jan 20, 2025
1 parent d0508d5 commit 32a9976
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 22 deletions.
7 changes: 4 additions & 3 deletions toolchain/check/handle_binding_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
break;
}
case Lex::TokenKind::Choice:
if (context.node_stack()
.PeekContains<Parse::NodeKind::ChoiceDefinitionStart>()) {
if (context.scope_stack().PeekInstId().is_valid()) {
// We're building a pattern for a choice alternative, not the choice
// type itself.
// type itself. The Choice itself has an invalid instruction id on
// the scope stack, but the Choice's declaration instruction is
// present and valid for checking the alternatives.
if (context_node_kind == Parse::NodeKind::ImplicitParamListStart) {
CARBON_DIAGNOSTIC(ChoiceAlternativeImplicitParams, Error,
"choice alternative with implicit parameters");
Expand Down
7 changes: 2 additions & 5 deletions toolchain/check/handle_choice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@ auto HandleParseNode(Context& context, Parse::ChoiceDefinitionStartId node_id)
context.scope_stack().Push(class_decl_id, class_info.scope_id,
self_specific_id);
// Checking the binding pattern for an alternative requires a non-empty stack.
// FIXME: `Lex::TokenKind::Choice` is incorrect as we're not parsing the
// pattern for a choice name, but there's no Lex token that's a decl
// introducer that we could safely use here. Is there a better way to
// communicate to `HandleAnyBindingPattern` that we're checking a choice
// alternative?
// We reuse the `Choice` token kind, along with the scope_stac() entry to let
// the pattern checking determine we are building a choice alternative.
context.decl_introducer_state_stack().Push<Lex::TokenKind::Choice>();
StartGenericDefinition(context);

Expand Down
14 changes: 0 additions & 14 deletions toolchain/check/node_stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,20 +145,6 @@ class NodeStack {
RequiredParseKind;
}

// Returns whether any node on the stack above the current node is a given
// kind.
template <const Parse::NodeKind& RequiredParseKind>
auto PeekContains() const -> bool {
CARBON_CHECK(stack_.size() >= 2);
for (int i = 0; i < static_cast<int>(stack_.size()) - 1; ++i) {
if (parse_tree_->node_kind(stack_[stack_.size() - 2 - i].node_id) ==
RequiredParseKind) {
return true;
}
}
return false;
}

// Pops the top of the stack without any verification.
auto PopAndIgnore() -> void {
Entry back = stack_.pop_back_val();
Expand Down

0 comments on commit 32a9976

Please sign in to comment.