node_ids.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 AnyImplAsId = NodeIdInCategory<NodeCategory::ImplAs>;
  56. using AnyMemberNameId = NodeIdInCategory<NodeCategory::MemberName>;
  57. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  58. using AnyNameComponentId = NodeIdInCategory<NodeCategory::NameComponent>;
  59. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  60. using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
  61. // NodeId with kind that matches either T::Kind or U::Kind.
  62. template <typename T, typename U>
  63. struct NodeIdOneOf : public NodeId {
  64. constexpr explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  65. template <const NodeKind& Kind>
  66. // NOLINTNEXTLINE(google-explicit-constructor)
  67. NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  68. static_assert(T::Kind == Kind || U::Kind == Kind);
  69. }
  70. // NOLINTNEXTLINE(google-explicit-constructor)
  71. constexpr NodeIdOneOf(InvalidNodeId /*invalid*/)
  72. : NodeId(NodeId::InvalidIndex) {}
  73. };
  74. using AnyClassDeclId = NodeIdOneOf<ClassDeclId, ClassDefinitionStartId>;
  75. using AnyFunctionDeclId =
  76. NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId>;
  77. using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
  78. using AnyInterfaceDeclId =
  79. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  80. // NodeId with kind that is anything but T::Kind.
  81. template <typename T>
  82. struct NodeIdNot : public NodeId {
  83. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  84. // NOLINTNEXTLINE(google-explicit-constructor)
  85. constexpr NodeIdNot(InvalidNodeId /*invalid*/)
  86. : NodeId(NodeId::InvalidIndex) {}
  87. };
  88. // Note that the support for extracting these types using the `Tree::Extract*`
  89. // functions is defined in `extract.cpp`.
  90. } // namespace Carbon::Parse
  91. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_