Prechádzať zdrojové kódy

Remove node_stack Peek templating where possible (#4801)

The "templated for consistency" variants felt a little confusing when I
was working on #4795, so suggesting to remove templating where it's not
helpful to instantiate (particularly when there were both templated and
non-templated variants). Note this leaves a `PeekIs<IdT>` because
there's indirection there, but that's more the exception than the rule.
Jon Ross-Perkins 1 rok pred
rodič
commit
f7269482fe

+ 1 - 1
toolchain/check/handle_binding_pattern.cpp

@@ -77,7 +77,7 @@ static auto HandleAnyBindingPattern(Context& context, Parse::NodeId node_id,
 
 
   // A `self` binding can only appear in an implicit parameter list.
   // A `self` binding can only appear in an implicit parameter list.
   if (name_id == SemIR::NameId::SelfValue &&
   if (name_id == SemIR::NameId::SelfValue &&
-      !context.node_stack().PeekIs<Parse::NodeKind::ImplicitParamListStart>()) {
+      !context.node_stack().PeekIs(Parse::NodeKind::ImplicitParamListStart)) {
     CARBON_DIAGNOSTIC(
     CARBON_DIAGNOSTIC(
         SelfOutsideImplicitParamList, Error,
         SelfOutsideImplicitParamList, Error,
         "`self` can only be declared in an implicit parameter list");
         "`self` can only be declared in an implicit parameter list");

+ 2 - 2
toolchain/check/handle_let_and_var.cpp

@@ -153,12 +153,12 @@ static auto HandleDecl(Context& context, NodeT node_id)
   context.pattern_block_stack().PopAndDiscard();
   context.pattern_block_stack().PopAndDiscard();
 
 
   // Handle the optional initializer.
   // Handle the optional initializer.
-  if (context.node_stack().PeekNextIs<InitializerNodeKind>()) {
+  if (context.node_stack().PeekNextIs(InitializerNodeKind)) {
     decl_info->init_id = context.node_stack().PopExpr();
     decl_info->init_id = context.node_stack().PopExpr();
     context.node_stack().PopAndDiscardSoloNodeId<InitializerNodeKind>();
     context.node_stack().PopAndDiscardSoloNodeId<InitializerNodeKind>();
   }
   }
 
 
-  if (context.node_stack().PeekIs<Parse::NodeKind::TuplePattern>()) {
+  if (context.node_stack().PeekIs(Parse::NodeKind::TuplePattern)) {
     if (decl_info->init_id &&
     if (decl_info->init_id &&
         context.scope_stack().PeekIndex() == ScopeIndex::Package) {
         context.scope_stack().PeekIndex() == ScopeIndex::Package) {
       context.global_init().Suspend();
       context.global_init().Suspend();

+ 1 - 1
toolchain/check/handle_pattern_list.cpp

@@ -17,7 +17,7 @@ auto HandleParseNode(Context& context, Parse::ImplicitParamListStartId node_id)
 
 
 auto HandleParseNode(Context& context, Parse::ImplicitParamListId node_id)
 auto HandleParseNode(Context& context, Parse::ImplicitParamListId node_id)
     -> bool {
     -> bool {
-  if (context.node_stack().PeekIs<Parse::NodeKind::ImplicitParamListStart>()) {
+  if (context.node_stack().PeekIs(Parse::NodeKind::ImplicitParamListStart)) {
     // End the subpattern started by a trailing comma, or the opening delimiter
     // End the subpattern started by a trailing comma, or the opening delimiter
     // of an empty list.
     // of an empty list.
     context.EndSubpatternAsEmpty();
     context.EndSubpatternAsEmpty();

+ 8 - 25
toolchain/check/node_stack.h

@@ -105,27 +105,12 @@ class NodeStack {
     return !stack_.empty() && PeekNodeKind() == kind;
     return !stack_.empty() && PeekNodeKind() == kind;
   }
   }
 
 
-  // Returns whether there is a node of the specified kind on top of the stack.
-  // Templated for consistency with other functions taking a parse node kind.
-  template <const Parse::NodeKind& RequiredParseKind>
-  auto PeekIs() const -> bool {
-    return PeekIs(RequiredParseKind);
-  }
-
   // Returns whether the node on the top of the stack has an overlapping
   // Returns whether the node on the top of the stack has an overlapping
   // category.
   // category.
   auto PeekIs(Parse::NodeCategory category) const -> bool {
   auto PeekIs(Parse::NodeCategory category) const -> bool {
     return !stack_.empty() && PeekNodeKind().category().HasAnyOf(category);
     return !stack_.empty() && PeekNodeKind().category().HasAnyOf(category);
   }
   }
 
 
-  // Returns whether the node on the top of the stack has an overlapping
-  // category. Templated for consistency with other functions taking a parse
-  // node category.
-  template <Parse::NodeCategory::RawEnumType RequiredParseCategory>
-  auto PeekIs() const -> bool {
-    return PeekIs(RequiredParseCategory);
-  }
-
   // Returns whether there is a node with the corresponding ID on top of the
   // Returns whether there is a node with the corresponding ID on top of the
   // stack.
   // stack.
   template <typename IdT>
   template <typename IdT>
@@ -138,11 +123,9 @@ class NodeStack {
   // have the breadth of support versus other Peek functions because it's
   // have the breadth of support versus other Peek functions because it's
   // expected to be used in narrow circumstances when determining how to treat
   // expected to be used in narrow circumstances when determining how to treat
   // the *current* top of the stack.
   // the *current* top of the stack.
-  template <const Parse::NodeKind& RequiredParseKind>
-  auto PeekNextIs() const -> bool {
+  auto PeekNextIs(Parse::NodeKind kind) const -> bool {
     CARBON_CHECK(stack_.size() >= 2);
     CARBON_CHECK(stack_.size() >= 2);
-    return parse_tree_->node_kind(stack_[stack_.size() - 2].node_id) ==
-           RequiredParseKind;
+    return parse_tree_->node_kind(stack_[stack_.size() - 2].node_id) == kind;
   }
   }
 
 
   // Pops the top of the stack without any verification.
   // Pops the top of the stack without any verification.
@@ -166,7 +149,7 @@ class NodeStack {
   template <const Parse::NodeKind& RequiredParseKind>
   template <const Parse::NodeKind& RequiredParseKind>
   auto PopForSoloNodeIdIf()
   auto PopForSoloNodeIdIf()
       -> std::optional<Parse::NodeIdForKind<RequiredParseKind>> {
       -> std::optional<Parse::NodeIdForKind<RequiredParseKind>> {
-    if (PeekIs<RequiredParseKind>()) {
+    if (PeekIs(RequiredParseKind)) {
       return PopForSoloNodeId<RequiredParseKind>();
       return PopForSoloNodeId<RequiredParseKind>();
     }
     }
     return std::nullopt;
     return std::nullopt;
@@ -182,7 +165,7 @@ class NodeStack {
   // was popped.
   // was popped.
   template <const Parse::NodeKind& RequiredParseKind>
   template <const Parse::NodeKind& RequiredParseKind>
   auto PopAndDiscardSoloNodeIdIf() -> bool {
   auto PopAndDiscardSoloNodeIdIf() -> bool {
-    if (!PeekIs<RequiredParseKind>()) {
+    if (!PeekIs(RequiredParseKind)) {
       return false;
       return false;
     }
     }
     PopForSoloNodeId<RequiredParseKind>();
     PopForSoloNodeId<RequiredParseKind>();
@@ -256,7 +239,7 @@ class NodeStack {
   // Otherwise returns std::nullopt.
   // Otherwise returns std::nullopt.
   template <const Parse::NodeKind& RequiredParseKind>
   template <const Parse::NodeKind& RequiredParseKind>
   auto PopIf() -> std::optional<decltype(Pop<RequiredParseKind>())> {
   auto PopIf() -> std::optional<decltype(Pop<RequiredParseKind>())> {
-    if (PeekIs<RequiredParseKind>()) {
+    if (PeekIs(RequiredParseKind)) {
       return Pop<RequiredParseKind>();
       return Pop<RequiredParseKind>();
     }
     }
     return std::nullopt;
     return std::nullopt;
@@ -266,7 +249,7 @@ class NodeStack {
   // Otherwise returns std::nullopt.
   // Otherwise returns std::nullopt.
   template <Parse::NodeCategory::RawEnumType RequiredParseCategory>
   template <Parse::NodeCategory::RawEnumType RequiredParseCategory>
   auto PopIf() -> std::optional<decltype(Pop<RequiredParseCategory>())> {
   auto PopIf() -> std::optional<decltype(Pop<RequiredParseCategory>())> {
-    if (PeekIs<RequiredParseCategory>()) {
+    if (PeekIs(RequiredParseCategory)) {
       return Pop<RequiredParseCategory>();
       return Pop<RequiredParseCategory>();
     }
     }
     return std::nullopt;
     return std::nullopt;
@@ -287,7 +270,7 @@ class NodeStack {
   template <const Parse::NodeKind& RequiredParseKind>
   template <const Parse::NodeKind& RequiredParseKind>
   auto PopWithNodeIdIf() -> std::pair<Parse::NodeIdForKind<RequiredParseKind>,
   auto PopWithNodeIdIf() -> std::pair<Parse::NodeIdForKind<RequiredParseKind>,
                                       decltype(PopIf<RequiredParseKind>())> {
                                       decltype(PopIf<RequiredParseKind>())> {
-    if (!PeekIs<RequiredParseKind>()) {
+    if (!PeekIs(RequiredParseKind)) {
       return {Parse::NodeId::Invalid, std::nullopt};
       return {Parse::NodeId::Invalid, std::nullopt};
     }
     }
     return PopWithNodeId<RequiredParseKind>();
     return PopWithNodeId<RequiredParseKind>();
@@ -299,7 +282,7 @@ class NodeStack {
   auto PopWithNodeIdIf()
   auto PopWithNodeIdIf()
       -> std::pair<Parse::NodeIdInCategory<RequiredParseCategory>,
       -> std::pair<Parse::NodeIdInCategory<RequiredParseCategory>,
                    decltype(PopIf<RequiredParseCategory>())> {
                    decltype(PopIf<RequiredParseCategory>())> {
-    if (!PeekIs<RequiredParseCategory>()) {
+    if (!PeekIs(RequiredParseCategory)) {
       return {Parse::NodeId::Invalid, std::nullopt};
       return {Parse::NodeId::Invalid, std::nullopt};
     }
     }
     return PopWithNodeId<RequiredParseCategory>();
     return PopWithNodeId<RequiredParseCategory>();