node_ids.h 4.6 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/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 AnyMemberNameOrMemberExprId =
  63. NodeIdInCategory<NodeCategory::MemberName | NodeCategory::MemberExpr>;
  64. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  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, ImportDeclId>;
  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_