node_ids.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/lex/token_index.h"
  8. #include "toolchain/parse/node_kind.h"
  9. namespace Carbon::Parse {
  10. // Represents an invalid node id of any type
  11. struct NoneNodeId {};
  12. // A lightweight handle representing a node in the tree.
  13. //
  14. // Objects of this type are small and cheap to copy and store. They don't
  15. // contain any of the information about the node, and serve as a handle that
  16. // can be used with the underlying tree to query for detailed information.
  17. struct NodeId : public IdBase<NodeId> {
  18. static constexpr llvm::StringLiteral Label = "node";
  19. // We give NodeId a bit in addition to TokenIndex in order to account for
  20. // virtual nodes, where a token may produce two nodes.
  21. static constexpr int32_t Bits = Lex::TokenIndex::Bits + 1;
  22. // The maximum ID, non-inclusive.
  23. static constexpr int Max = 1 << Bits;
  24. // A node ID with no value.
  25. static constexpr NoneNodeId None;
  26. constexpr explicit NodeId(int32_t index) : IdBase(index) {
  27. CARBON_DCHECK(index < Max, "Index out of range: {0}", index);
  28. }
  29. explicit(false) constexpr NodeId(NoneNodeId /*none*/) : IdBase(NoneIndex) {}
  30. };
  31. // For looking up the type associated with a given id type.
  32. template <typename T>
  33. struct NodeForId;
  34. // `<KindName>Id` is a typed version of `NodeId` that references a node of kind
  35. // `<KindName>`:
  36. template <const NodeKind& K>
  37. struct NodeIdForKind : public NodeId {
  38. // NOLINTNEXTLINE(readability-identifier-naming)
  39. static const NodeKind& Kind;
  40. // Provide a factory function for construction from `NodeId`. This doesn't
  41. // validate the type, so it's unsafe.
  42. static constexpr auto UnsafeMake(NodeId node_id) -> NodeIdForKind {
  43. return NodeIdForKind(node_id);
  44. }
  45. explicit(false) constexpr NodeIdForKind(NoneNodeId /*none*/)
  46. : NodeId(NoneIndex) {}
  47. private:
  48. // Private to prevent accidental explicit construction from an untyped
  49. // NodeId.
  50. explicit constexpr NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
  51. };
  52. template <const NodeKind& K>
  53. const NodeKind& NodeIdForKind<K>::Kind = K;
  54. #define CARBON_PARSE_NODE_KIND(KindName) \
  55. using KindName##Id = NodeIdForKind<NodeKind::KindName>;
  56. #include "toolchain/parse/node_kind.def"
  57. // NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
  58. template <NodeCategory::RawEnumType Category>
  59. struct NodeIdInCategory : public NodeId {
  60. // Provide a factory function for construction from `NodeId`. This doesn't
  61. // validate the type, so it's unsafe.
  62. static constexpr auto UnsafeMake(NodeId node_id) -> NodeIdInCategory {
  63. return NodeIdInCategory(node_id);
  64. }
  65. // Support conversion from `NodeIdForKind<Kind>` if Kind's category
  66. // overlaps with `Category`.
  67. template <const NodeKind& Kind>
  68. explicit(false) NodeIdInCategory(NodeIdForKind<Kind> node_id)
  69. : NodeId(node_id) {
  70. CARBON_CHECK(Kind.category().HasAnyOf(Category));
  71. }
  72. explicit(false) constexpr NodeIdInCategory(NoneNodeId /*none*/)
  73. : NodeId(NoneIndex) {}
  74. private:
  75. // Private to prevent accidental explicit construction from an untyped
  76. // NodeId.
  77. explicit constexpr NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
  78. };
  79. // Aliases for `NodeIdInCategory` to describe particular categories of nodes.
  80. using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
  81. using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
  82. using AnyImplAsId = NodeIdInCategory<NodeCategory::ImplAs>;
  83. using AnyMemberAccessId =
  84. NodeIdInCategory<NodeCategory::MemberName | NodeCategory::MemberExpr |
  85. NodeCategory::IntConst>;
  86. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  87. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  88. using AnyStatementId =
  89. NodeIdInCategory<NodeCategory::Statement | NodeCategory::Decl>;
  90. using AnyRequireImplsId = NodeIdInCategory<NodeCategory::RequireImpls>;
  91. using AnyRequirementId = NodeIdInCategory<NodeCategory::Requirement>;
  92. using AnyNonExprNameId = NodeIdInCategory<NodeCategory::NonExprName>;
  93. using AnyPackageNameId = NodeIdInCategory<NodeCategory::PackageName>;
  94. // NodeId with kind that matches one of the `T::Kind`s.
  95. template <typename... T>
  96. requires(sizeof...(T) >= 2)
  97. struct NodeIdOneOf : public NodeId {
  98. private:
  99. // True if `OtherT` is one of `T`.
  100. template <typename OtherT>
  101. static constexpr bool Contains = (std::is_same<OtherT, T>{} || ...);
  102. // True if `NodeIdOneOf<SubsetT...>` is a subset of this `NodeIdOneOf`.
  103. template <typename Unused>
  104. static constexpr bool IsSubset = false;
  105. template <typename... SubsetT>
  106. static constexpr bool IsSubset<NodeIdOneOf<SubsetT...>> =
  107. (Contains<SubsetT> && ...);
  108. // Private to prevent accidental explicit construction from an untyped
  109. // NodeId.
  110. explicit constexpr NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  111. public:
  112. // Provide a factory function for construction from `NodeId`. This doesn't
  113. // validate the type, so it's unsafe.
  114. static constexpr auto UnsafeMake(NodeId node_id) -> NodeIdOneOf {
  115. return NodeIdOneOf(node_id);
  116. }
  117. template <const NodeKind& Kind>
  118. requires(Contains<NodeIdForKind<Kind>>)
  119. explicit(false) NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {}
  120. template <typename OtherNodeIdOneOf>
  121. requires(IsSubset<OtherNodeIdOneOf>)
  122. explicit(false) NodeIdOneOf(OtherNodeIdOneOf node_id) : NodeId(node_id) {}
  123. explicit(false) constexpr NodeIdOneOf(NoneNodeId /*none*/)
  124. : NodeId(NoneIndex) {}
  125. };
  126. using AnyClassDeclId =
  127. NodeIdOneOf<ClassDeclId, ClassDefinitionStartId,
  128. // TODO: This may be wrong? But we have choice types produce a
  129. // class, so they are a form of class decls. This avoids
  130. // duplicating all of SemIR::ClassDecl.
  131. ChoiceDefinitionStartId>;
  132. using AnyFunctionDeclId = NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId,
  133. BuiltinFunctionDefinitionStartId>;
  134. using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
  135. using AnyInterfaceDeclId =
  136. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  137. using AnyNamedConstraintDeclId =
  138. NodeIdOneOf<NamedConstraintDeclId, NamedConstraintDefinitionStartId>;
  139. using AnyNamespaceId =
  140. NodeIdOneOf<NamespaceId, ImportDeclId, LibraryDeclId, PackageDeclId>;
  141. using AnyPackagingDeclId =
  142. NodeIdOneOf<ImportDeclId, LibraryDeclId, PackageDeclId>;
  143. using AnyPointerDeferenceExprId =
  144. NodeIdOneOf<PrefixOperatorStarId, PointerMemberAccessExprId>;
  145. using AnyRuntimeBindingPatternName =
  146. NodeIdOneOf<IdentifierNameNotBeforeParamsId, SelfValueNameId,
  147. UnderscoreNameId>;
  148. // NodeId with kind that is anything but T::Kind.
  149. template <typename T>
  150. struct NodeIdNot : public NodeId {
  151. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  152. explicit(false) constexpr NodeIdNot(NoneNodeId /*none*/)
  153. : NodeId(NoneIndex) {}
  154. };
  155. // Note that the support for extracting these types using the `Tree::Extract*`
  156. // functions is defined in `extract.cpp`.
  157. } // namespace Carbon::Parse
  158. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_