node_ids.h 8.0 KB

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