node_ids.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_
  5. #define CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_
  6. #include "toolchain/base/index_base.h"
  7. #include "toolchain/parse/node_kind.h"
  8. namespace Carbon::Parse {
  9. // Represents an invalid node id of any type
  10. struct InvalidNodeId {};
  11. // A lightweight handle representing a node in the tree.
  12. //
  13. // Objects of this type are small and cheap to copy and store. They don't
  14. // contain any of the information about the node, and serve as a handle that
  15. // can be used with the underlying tree to query for detailed information.
  16. struct NodeId : public IdBase {
  17. // An explicitly invalid instance.
  18. static constexpr InvalidNodeId Invalid;
  19. using IdBase::IdBase;
  20. // NOLINTNEXTLINE(google-explicit-constructor)
  21. constexpr NodeId(InvalidNodeId /*invalid*/) : IdBase(NodeId::InvalidIndex) {}
  22. };
  23. // For looking up the type associated with a given id type.
  24. template <typename T>
  25. struct NodeForId;
  26. // `<KindName>Id` is a typed version of `NodeId` that references a node of kind
  27. // `<KindName>`:
  28. template <const NodeKind& K>
  29. struct NodeIdForKind : public NodeId {
  30. // NOLINTNEXTLINE(readability-identifier-naming)
  31. static const NodeKind& Kind;
  32. constexpr explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
  33. // NOLINTNEXTLINE(google-explicit-constructor)
  34. constexpr NodeIdForKind(InvalidNodeId /*invalid*/)
  35. : NodeId(NodeId::InvalidIndex) {}
  36. };
  37. template <const NodeKind& K>
  38. const NodeKind& NodeIdForKind<K>::Kind = K;
  39. #define CARBON_PARSE_NODE_KIND(KindName) \
  40. using KindName##Id = NodeIdForKind<NodeKind::KindName>;
  41. #include "toolchain/parse/node_kind.def"
  42. // NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
  43. template <NodeCategory Category>
  44. struct NodeIdInCategory : public NodeId {
  45. // TODO: Support conversion from `NodeIdForKind<Kind>` if `Kind::category()`
  46. // overlaps with `Category`.
  47. constexpr explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
  48. // NOLINTNEXTLINE(google-explicit-constructor)
  49. constexpr NodeIdInCategory(InvalidNodeId /*invalid*/)
  50. : NodeId(NodeId::InvalidIndex) {}
  51. };
  52. // Aliases for `NodeIdInCategory` to describe particular categories of nodes.
  53. using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
  54. using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
  55. using AnyMemberNameId = NodeIdInCategory<NodeCategory::MemberName>;
  56. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  57. using AnyNameComponentId = NodeIdInCategory<NodeCategory::NameComponent>;
  58. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  59. using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
  60. // NodeId with kind that matches either T::Kind or U::Kind.
  61. template <typename T, typename U>
  62. struct NodeIdOneOf : public NodeId {
  63. constexpr explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  64. template <const NodeKind& Kind>
  65. // NOLINTNEXTLINE(google-explicit-constructor)
  66. NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  67. static_assert(T::Kind == Kind || U::Kind == Kind);
  68. }
  69. // NOLINTNEXTLINE(google-explicit-constructor)
  70. constexpr NodeIdOneOf(InvalidNodeId /*invalid*/)
  71. : NodeId(NodeId::InvalidIndex) {}
  72. };
  73. using AnyClassDeclId = NodeIdOneOf<ClassDeclId, ClassDefinitionStartId>;
  74. using AnyFunctionDeclId =
  75. NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId>;
  76. using AnyInterfaceDeclId =
  77. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  78. // NodeId with kind that is anything but T::Kind.
  79. template <typename T>
  80. struct NodeIdNot : public NodeId {
  81. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  82. // NOLINTNEXTLINE(google-explicit-constructor)
  83. constexpr NodeIdNot(InvalidNodeId /*invalid*/)
  84. : NodeId(NodeId::InvalidIndex) {}
  85. };
  86. // Note that the support for extracting these types using the `Tree::Extract*`
  87. // functions is defined in `extract.cpp`.
  88. } // namespace Carbon::Parse
  89. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_