node_ids.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Support conversion from `NodeIdForKind<Kind>` if Kind's category
  46. // overlaps with `Category`.
  47. template <const NodeKind& Kind>
  48. // NOLINTNEXTLINE(google-explicit-constructor)
  49. NodeIdInCategory(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  50. CARBON_CHECK(!!(Kind.category() & Category));
  51. }
  52. constexpr explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
  53. // NOLINTNEXTLINE(google-explicit-constructor)
  54. constexpr NodeIdInCategory(InvalidNodeId /*invalid*/)
  55. : NodeId(NodeId::InvalidIndex) {}
  56. };
  57. // Aliases for `NodeIdInCategory` to describe particular categories of nodes.
  58. using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
  59. using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
  60. using AnyImplAsId = NodeIdInCategory<NodeCategory::ImplAs>;
  61. using AnyMemberNameOrMemberExprId =
  62. NodeIdInCategory<NodeCategory::MemberName | NodeCategory::MemberExpr>;
  63. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  64. using AnyNameComponentId = NodeIdInCategory<NodeCategory::NameComponent>;
  65. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  66. using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
  67. // NodeId with kind that matches one of the `T::Kind`s.
  68. template <typename... T>
  69. struct NodeIdOneOf : public NodeId {
  70. static_assert(sizeof...(T) >= 2, "Expected at least two types.");
  71. constexpr explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  72. template <const NodeKind& Kind>
  73. // NOLINTNEXTLINE(google-explicit-constructor)
  74. NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  75. static_assert(((T::Kind == Kind) || ...));
  76. }
  77. // NOLINTNEXTLINE(google-explicit-constructor)
  78. constexpr NodeIdOneOf(InvalidNodeId /*invalid*/)
  79. : NodeId(NodeId::InvalidIndex) {}
  80. };
  81. using AnyClassDeclId = NodeIdOneOf<ClassDeclId, ClassDefinitionStartId>;
  82. using AnyFunctionDeclId = NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId,
  83. BuiltinFunctionDefinitionStartId>;
  84. using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
  85. using AnyInterfaceDeclId =
  86. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  87. using AnyNamespaceId = NodeIdOneOf<NamespaceId, ImportDirectiveId>;
  88. using AnyPointerDeferenceExprId =
  89. NodeIdOneOf<PrefixOperatorStarId, PointerMemberAccessExprId>;
  90. // NodeId with kind that is anything but T::Kind.
  91. template <typename T>
  92. struct NodeIdNot : public NodeId {
  93. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  94. // NOLINTNEXTLINE(google-explicit-constructor)
  95. constexpr NodeIdNot(InvalidNodeId /*invalid*/)
  96. : NodeId(NodeId::InvalidIndex) {}
  97. };
  98. // Note that the support for extracting these types using the `Tree::Extract*`
  99. // functions is defined in `extract.cpp`.
  100. } // namespace Carbon::Parse
  101. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_