node_ids.h 4.8 KB

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