node_ids.h 4.9 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 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. constexpr explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
  35. // NOLINTNEXTLINE(google-explicit-constructor)
  36. constexpr NodeIdForKind(NoneNodeId /*none*/) : NodeId(NoneIndex) {}
  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(NoneNodeId /*none*/) : NodeId(NoneIndex) {}
  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 AnyMemberAccessId =
  62. NodeIdInCategory<NodeCategory::MemberName | NodeCategory::MemberExpr |
  63. NodeCategory::IntConst>;
  64. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  65. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  66. using AnyStatementId =
  67. NodeIdInCategory<NodeCategory::Statement | NodeCategory::Decl>;
  68. using AnyRequirementId = NodeIdInCategory<NodeCategory::Requirement>;
  69. using AnyNonExprIdentifierNameId =
  70. NodeIdInCategory<NodeCategory::NonExprIdentifierName>;
  71. using AnyPackageNameId = NodeIdInCategory<NodeCategory::PackageName>;
  72. // NodeId with kind that matches one of the `T::Kind`s.
  73. template <typename... T>
  74. struct NodeIdOneOf : public NodeId {
  75. static_assert(sizeof...(T) >= 2, "Expected at least two types.");
  76. constexpr explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  77. template <const NodeKind& Kind>
  78. // NOLINTNEXTLINE(google-explicit-constructor)
  79. NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  80. static_assert(((T::Kind == Kind) || ...));
  81. }
  82. // NOLINTNEXTLINE(google-explicit-constructor)
  83. constexpr NodeIdOneOf(NoneNodeId /*none*/) : NodeId(NoneIndex) {}
  84. };
  85. using AnyClassDeclId = NodeIdOneOf<ClassDeclId, ClassDefinitionStartId>;
  86. using AnyFunctionDeclId = NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId,
  87. BuiltinFunctionDefinitionStartId>;
  88. using AnyImplDeclId = NodeIdOneOf<ImplDeclId, ImplDefinitionStartId>;
  89. using AnyInterfaceDeclId =
  90. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  91. using AnyNamespaceId = NodeIdOneOf<NamespaceId, ImportDeclId>;
  92. using AnyPointerDeferenceExprId =
  93. NodeIdOneOf<PrefixOperatorStarId, PointerMemberAccessExprId>;
  94. // NodeId with kind that is anything but T::Kind.
  95. template <typename T>
  96. struct NodeIdNot : public NodeId {
  97. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  98. // NOLINTNEXTLINE(google-explicit-constructor)
  99. constexpr NodeIdNot(NoneNodeId /*none*/) : NodeId(NoneIndex) {}
  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_