node_ids.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 InvalidNodeId {};
  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 {
  18. // An explicitly invalid node ID.
  19. static constexpr InvalidNodeId Invalid;
  20. using IdBase::IdBase;
  21. // NOLINTNEXTLINE(google-explicit-constructor)
  22. constexpr NodeId(InvalidNodeId /*invalid*/) : IdBase(NodeId::InvalidIndex) {}
  23. };
  24. // For looking up the type associated with a given id type.
  25. template <typename T>
  26. struct NodeForId;
  27. // `<KindName>Id` is a typed version of `NodeId` that references a node of kind
  28. // `<KindName>`:
  29. template <const NodeKind& K>
  30. struct NodeIdForKind : public NodeId {
  31. // NOLINTNEXTLINE(readability-identifier-naming)
  32. static const NodeKind& Kind;
  33. constexpr explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
  34. // NOLINTNEXTLINE(google-explicit-constructor)
  35. constexpr NodeIdForKind(InvalidNodeId /*invalid*/)
  36. : NodeId(NodeId::InvalidIndex) {}
  37. };
  38. template <const NodeKind& K>
  39. const NodeKind& NodeIdForKind<K>::Kind = K;
  40. #define CARBON_PARSE_NODE_KIND(KindName) \
  41. using KindName##Id = NodeIdForKind<NodeKind::KindName>;
  42. #include "toolchain/parse/node_kind.def"
  43. // NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
  44. template <NodeCategory::RawEnumType Category>
  45. struct NodeIdInCategory : public NodeId {
  46. // Support conversion from `NodeIdForKind<Kind>` if Kind's category
  47. // overlaps with `Category`.
  48. template <const NodeKind& Kind>
  49. // NOLINTNEXTLINE(google-explicit-constructor)
  50. NodeIdInCategory(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  51. CARBON_CHECK(Kind.category().HasAnyOf(Category));
  52. }
  53. constexpr explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
  54. // NOLINTNEXTLINE(google-explicit-constructor)
  55. constexpr NodeIdInCategory(InvalidNodeId /*invalid*/)
  56. : NodeId(NodeId::InvalidIndex) {}
  57. };
  58. // Aliases for `NodeIdInCategory` to describe particular categories of nodes.
  59. using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
  60. using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
  61. using AnyImplAsId = NodeIdInCategory<NodeCategory::ImplAs>;
  62. using AnyMemberAccessId =
  63. NodeIdInCategory<NodeCategory::MemberName | NodeCategory::MemberExpr |
  64. NodeCategory::IntConst>;
  65. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  66. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  67. using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
  68. using AnyRequirementId = NodeIdInCategory<NodeCategory::Requirement>;
  69. // NodeId with kind that matches one of the `T::Kind`s.
  70. template <typename... T>
  71. struct NodeIdOneOf : public NodeId {
  72. static_assert(sizeof...(T) >= 2, "Expected at least two types.");
  73. constexpr explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  74. template <const NodeKind& Kind>
  75. // NOLINTNEXTLINE(google-explicit-constructor)
  76. NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  77. static_assert(((T::Kind == Kind) || ...));
  78. }
  79. // NOLINTNEXTLINE(google-explicit-constructor)
  80. constexpr NodeIdOneOf(InvalidNodeId /*invalid*/)
  81. : NodeId(NodeId::InvalidIndex) {}
  82. };
  83. using AnyClassDeclId = NodeIdOneOf<ClassDeclId, ClassDefinitionStartId>;
  84. using AnyFunctionDeclId = NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId,
  85. BuiltinFunctionDefinitionStartId>;
  86. using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
  87. using AnyInterfaceDeclId =
  88. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  89. using AnyNamespaceId = NodeIdOneOf<NamespaceId, ImportDeclId>;
  90. using AnyPointerDeferenceExprId =
  91. NodeIdOneOf<PrefixOperatorStarId, PointerMemberAccessExprId>;
  92. // NodeId with kind that is anything but T::Kind.
  93. template <typename T>
  94. struct NodeIdNot : public NodeId {
  95. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  96. // NOLINTNEXTLINE(google-explicit-constructor)
  97. constexpr NodeIdNot(InvalidNodeId /*invalid*/)
  98. : NodeId(NodeId::InvalidIndex) {}
  99. };
  100. // Note that the support for extracting these types using the `Tree::Extract*`
  101. // functions is defined in `extract.cpp`.
  102. } // namespace Carbon::Parse
  103. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_