node_ids.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. constexpr NodeId(InvalidNodeId) : IdBase(NodeId::InvalidIndex) {}
  21. };
  22. // For looking up the type associated with a given id type.
  23. template <typename T>
  24. struct NodeForId;
  25. // `<KindName>Id` is a typed version of `NodeId` that references a node of kind
  26. // `<KindName>`:
  27. template <const NodeKind& K>
  28. struct NodeIdForKind : public NodeId {
  29. static const NodeKind& Kind;
  30. constexpr explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
  31. constexpr NodeIdForKind(InvalidNodeId) : NodeId(NodeId::InvalidIndex) {}
  32. };
  33. template <const NodeKind& K>
  34. const NodeKind& NodeIdForKind<K>::Kind = K;
  35. #define CARBON_PARSE_NODE_KIND(KindName) \
  36. using KindName##Id = NodeIdForKind<NodeKind::KindName>;
  37. #include "toolchain/parse/node_kind.def"
  38. // NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
  39. template <NodeCategory Category>
  40. struct NodeIdInCategory : public NodeId {
  41. // TODO: Support conversion from `NodeIdForKind<Kind>` if `Kind::category()`
  42. // overlaps with `Category`.
  43. constexpr explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
  44. constexpr NodeIdInCategory(InvalidNodeId) : NodeId(NodeId::InvalidIndex) {}
  45. };
  46. // Aliases for `NodeIdInCategory` to describe particular categories of nodes.
  47. using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
  48. using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
  49. using AnyMemberNameId = NodeIdInCategory<NodeCategory::MemberName>;
  50. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  51. using AnyNameComponentId = NodeIdInCategory<NodeCategory::NameComponent>;
  52. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  53. using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
  54. // NodeId with kind that matches either T::Kind or U::Kind.
  55. template <typename T, typename U>
  56. struct NodeIdOneOf : public NodeId {
  57. constexpr explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  58. template <const NodeKind& Kind>
  59. NodeIdOneOf(NodeIdForKind<Kind> node_id) : NodeId(node_id) {
  60. static_assert(T::Kind == Kind || U::Kind == Kind);
  61. }
  62. constexpr NodeIdOneOf(InvalidNodeId) : NodeId(NodeId::InvalidIndex) {}
  63. };
  64. using AnyClassDeclId = NodeIdOneOf<ClassDeclId, ClassDefinitionStartId>;
  65. using AnyFunctionDeclId =
  66. NodeIdOneOf<FunctionDeclId, FunctionDefinitionStartId>;
  67. using AnyInterfaceDeclId =
  68. NodeIdOneOf<InterfaceDeclId, InterfaceDefinitionStartId>;
  69. // NodeId with kind that is anything but T::Kind.
  70. template <typename T>
  71. struct NodeIdNot : public NodeId {
  72. constexpr explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  73. constexpr NodeIdNot(InvalidNodeId) : NodeId(NodeId::InvalidIndex) {}
  74. };
  75. // Note that the support for extracting these types using the `Tree::Extract*`
  76. // functions is defined in `extract.cpp`.
  77. } // namespace Carbon::Parse
  78. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_