node_ids.h 6.9 KB

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