Repository: carbon-language/carbon-lang
File: toolchain/parse/node_ids.h (C/C++)

Code before:
using AnyStatementId =
    NodeIdInCategory<NodeCategory::Statement | NodeCategory::Decl>;
using AnyRequirementId = NodeIdInCategory<NodeCategory::Requirement>;
using AnyNonExprNameId = NodeIdInCategory<NodeCategory::NonExprName>;
using AnyPackageNameId = NodeIdInCategory<NodeCategory::PackageName>;

// NodeId with kind that matches one of the `T::Kind`s.
template <typename... T>
  requires(sizeof...(T) >= 2)
struct NodeIdOneOf : public NodeId {
  // Provide a factory function for construction from `NodeId`. This doesn't
  // validate the type, so it's unsafe.
  static constexpr auto UnsafeMake(NodeId node_id) -> NodeIdOneOf {
    return NodeIdOneOf(node_id);
  }

  template <const NodeKind& Kind>
    requires((T::Kind == Kind) || ...)
  // NOLINTNEXTLINE(google-explicit-constructor)
  NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {}

  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr NodeIdOneOf(NoneNodeId /*none*/) : NodeId(NoneIndex) {}

 private:
  // Private to prevent accidentals explicit construction from an untyped
  // NodeId.
  explicit constexpr NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
};

using AnyClassDeclId =
    NodeIdOneOf<ClassDeclId, ClassDefinitionStartId,
                // TODO: This may be wrong? But we have choice types produce a
                // class, so they are a form of class decls. This avoids
                // duplicating all of SemIR::ClassDecl.
                ChoiceDefinitionStartId>;
using AnyFunctionDeclId = NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId,
                                      BuiltinFunctionDefinitionStartId>;
using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
using AnyInterfaceDeclId =
    NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
using AnyNamespaceId = NodeIdOneOf<NamespaceId, ImportDeclId>;
using AnyPointerDeferenceExprId =
    NodeIdOneOf<PrefixOperatorStarId, PointerMemberAccessExprId>;

// NodeId with kind that is anything but T::Kind.
template <typename T>
struct NodeIdNot : public NodeId {
  constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr NodeIdNot(NoneNodeId /*none*/) : NodeId(NoneIndex) {}

Review comment (suggestion):
```suggestion
  // Private to prevent accidental explicit construction from an untyped
```

Code after:
    CARBON_CHECK(Kind.category().HasAnyOf(Category));
  }

  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr NodeIdInCategory(NoneNodeId /*none*/) : NodeId(NoneIndex) {}

 private:
  // Private to prevent accidental explicit construction from an untyped
  // NodeId.
  explicit constexpr NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
};

// Aliases for `NodeIdInCategory` to describe particular categories of nodes.
using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
using AnyImplAsId = NodeIdInCategory<NodeCategory::ImplAs>;
using AnyMemberAccessId =
    NodeIdInCategory<NodeCategory::MemberName | NodeCategory::MemberExpr |
                     NodeCategory::IntConst>;
using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
using AnyStatementId =
    NodeIdInCategory<NodeCategory::Statement | NodeCategory::Decl>;
using AnyRequirementId = NodeIdInCategory<NodeCategory::Requirement>;
using AnyNonExprNameId = NodeIdInCategory<NodeCategory::NonExprName>;
using AnyPackageNameId = NodeIdInCategory<NodeCategory::PackageName>;

// NodeId with kind that matches one of the `T::Kind`s.
template <typename... T>
  requires(sizeof...(T) >= 2)
struct NodeIdOneOf : public NodeId {
  // Provide a factory function for construction from `NodeId`. This doesn't
  // validate the type, so it's unsafe.
  static constexpr auto UnsafeMake(NodeId node_id) -> NodeIdOneOf {
    return NodeIdOneOf(node_id);
  }

  template <const NodeKind& Kind>
    requires((T::Kind == Kind) || ...)
  // NOLINTNEXTLINE(google-explicit-constructor)
  NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {}

  // NOLINTNEXTLINE(google-explicit-constructor)
  constexpr NodeIdOneOf(NoneNodeId /*none*/) : NodeId(NoneIndex) {}

 private:
  // Private to prevent accidental explicit construction from an untyped
  // NodeId.
  explicit constexpr NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
};