node_ids.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 =
  89. NodeIdInCategory<NodeCategory::Pattern | NodeCategory::Expr>;
  90. using AnyStatementId =
  91. NodeIdInCategory<NodeCategory::Statement | NodeCategory::Decl>;
  92. using AnyRequireImplsId = NodeIdInCategory<NodeCategory::RequireImpls>;
  93. using AnyRequirementId = NodeIdInCategory<NodeCategory::Requirement>;
  94. using AnyObserveOperatorId = NodeIdInCategory<NodeCategory::ObserveOperator>;
  95. using AnyObserveOperandId =
  96. NodeIdInCategory<NodeCategory::Expr | NodeCategory::ObserveOperator>;
  97. using AnyNonExprNameId = NodeIdInCategory<NodeCategory::NonExprName>;
  98. using AnyPackageNameId = NodeIdInCategory<NodeCategory::PackageName>;
  99. using AnyReturnDeclId = NodeIdInCategory<NodeCategory::ReturnDecl>;
  100. namespace Internal {
  101. template <typename T>
  102. concept IsNodeKind = std::same_as<std::remove_cvref_t<T>, NodeKind> ||
  103. std::same_as<std::remove_cvref_t<T>, NodeKind::Definition>;
  104. template <typename T>
  105. concept IsTypedNode = requires {
  106. { T::Kind } -> IsNodeKind;
  107. };
  108. } // namespace Internal
  109. // NodeId with kind that matches one of the `T::Kind`s.
  110. template <typename... T>
  111. requires(sizeof...(T) >= 2 && (Internal::IsTypedNode<T> && ...))
  112. struct NodeIdOneOf : public NodeId {
  113. private:
  114. // True if `OtherT` is one of `T`.
  115. template <typename OtherT>
  116. static constexpr bool Contains = (std::is_same<OtherT, T>{} || ...);
  117. // True if `NodeIdOneOf<SubsetT...>` is a subset of this `NodeIdOneOf`.
  118. template <typename Unused>
  119. static constexpr bool IsSubset = false;
  120. template <typename... SubsetT>
  121. static constexpr bool IsSubset<NodeIdOneOf<SubsetT...>> =
  122. (Contains<SubsetT> && ...);
  123. // Private to prevent accidental explicit construction from an untyped
  124. // NodeId.
  125. explicit constexpr NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  126. public:
  127. // Provide a factory function for construction from `NodeId`. This doesn't
  128. // validate the type, so it's unsafe.
  129. static constexpr auto UnsafeMake(NodeId node_id) -> NodeIdOneOf {
  130. return NodeIdOneOf(node_id);
  131. }
  132. template <const NodeKind& Kind>
  133. requires(Contains<NodeIdForKind<Kind>>)
  134. explicit(false) NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {}
  135. template <typename OtherNodeIdOneOf>
  136. requires(IsSubset<OtherNodeIdOneOf>)
  137. explicit(false) NodeIdOneOf(OtherNodeIdOneOf node_id) : NodeId(node_id) {}
  138. explicit(false) constexpr NodeIdOneOf(NoneNodeId /*none*/)
  139. : NodeId(NoneIndex) {}
  140. };
  141. using AnyClassDeclId =
  142. NodeIdOneOf<ClassDeclId, ClassDefinitionStartId,
  143. // TODO: This may be wrong? But we have choice types produce a
  144. // class, so they are a form of class decls. This avoids
  145. // duplicating all of SemIR::ClassDecl.
  146. ChoiceDefinitionStartId>;
  147. using AnyFunctionDeclId = NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId,
  148. BuiltinFunctionDefinitionStartId>;
  149. using AnyFunctionDefinitionId =
  150. NodeIdOneOf<FunctionDefinitionId, FunctionTerseDefinitionId,
  151. BuiltinFunctionDefinitionId>;
  152. using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
  153. using AnyInterfaceDeclId =
  154. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  155. using AnyNamedConstraintDeclId =
  156. NodeIdOneOf<NamedConstraintDeclId, NamedConstraintDefinitionStartId>;
  157. using AnyNamespaceId =
  158. NodeIdOneOf<NamespaceId, ImportDeclId, LibraryDeclId, PackageDeclId>;
  159. using AnyPackagingDeclId =
  160. NodeIdOneOf<ImportDeclId, LibraryDeclId, PackageDeclId>;
  161. using AnyPointerDeferenceExprId =
  162. NodeIdOneOf<PrefixOperatorStarId, PointerMemberAccessExprId>;
  163. using AnyRuntimeBindingPatternName =
  164. NodeIdOneOf<IdentifierNameNotBeforeSignatureId, SelfValueNameId,
  165. UnderscoreNameId>;
  166. using AnyPrimitiveFormIdId =
  167. NodeIdOneOf<RefPrimitiveFormId, VarPrimitiveFormId, ValPrimitiveFormId>;
  168. // NodeId with kind that is anything but T::Kind.
  169. template <typename T>
  170. struct NodeIdNot : public NodeId {
  171. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  172. explicit(false) constexpr NodeIdNot(NoneNodeId /*none*/)
  173. : NodeId(NoneIndex) {}
  174. };
  175. // Note that the support for extracting these types using the `Tree::Extract*`
  176. // functions is defined in `extract.cpp`.
  177. } // namespace Carbon::Parse
  178. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_