node_ids.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // A lightweight handle representing a node in the tree.
  10. //
  11. // Objects of this type are small and cheap to copy and store. They don't
  12. // contain any of the information about the node, and serve as a handle that
  13. // can be used with the underlying tree to query for detailed information.
  14. struct NodeId : public IdBase {
  15. // An explicitly invalid instance.
  16. static const NodeId Invalid;
  17. using IdBase::IdBase;
  18. };
  19. constexpr NodeId NodeId::Invalid = NodeId(NodeId::InvalidIndex);
  20. // For looking up the type associated with a given id type.
  21. template <typename T>
  22. struct NodeForId;
  23. // `<KindName>Id` is a typed version of `NodeId` that references a node of kind
  24. // `<KindName>`:
  25. template <const NodeKind&>
  26. struct NodeIdForKind : public NodeId {
  27. static const NodeIdForKind Invalid;
  28. explicit NodeIdForKind(NodeId node_id) : NodeId(node_id) {}
  29. };
  30. template <const NodeKind& Kind>
  31. constexpr NodeIdForKind<Kind> NodeIdForKind<Kind>::Invalid =
  32. NodeIdForKind(NodeId::Invalid.index);
  33. #define CARBON_PARSE_NODE_KIND(KindName) \
  34. using KindName##Id = NodeIdForKind<NodeKind::KindName>;
  35. #include "toolchain/parse/node_kind.def"
  36. // NodeId that matches any NodeKind whose `category()` overlaps with `Category`.
  37. template <NodeCategory Category>
  38. struct NodeIdInCategory : public NodeId {
  39. // An explicitly invalid instance.
  40. static const NodeIdInCategory<Category> Invalid;
  41. // TODO: Support conversion from `NodeIdForKind<Kind>` if `Kind::category()`
  42. // overlaps with `Category`.
  43. explicit NodeIdInCategory(NodeId node_id) : NodeId(node_id) {}
  44. };
  45. template <NodeCategory Category>
  46. constexpr NodeIdInCategory<Category> NodeIdInCategory<Category>::Invalid =
  47. NodeIdInCategory<Category>(NodeId::InvalidIndex);
  48. // Aliases for `NodeIdInCategory` to describe particular categories of nodes.
  49. using AnyDeclId = NodeIdInCategory<NodeCategory::Decl>;
  50. using AnyExprId = NodeIdInCategory<NodeCategory::Expr>;
  51. using AnyMemberNameId = NodeIdInCategory<NodeCategory::MemberName>;
  52. using AnyModifierId = NodeIdInCategory<NodeCategory::Modifier>;
  53. using AnyNameComponentId = NodeIdInCategory<NodeCategory::NameComponent>;
  54. using AnyPatternId = NodeIdInCategory<NodeCategory::Pattern>;
  55. using AnyStatementId = NodeIdInCategory<NodeCategory::Statement>;
  56. // NodeId with kind that matches either T::Kind or U::Kind.
  57. template <typename T, typename U>
  58. struct NodeIdOneOf : public NodeId {
  59. // An explicitly invalid instance.
  60. static const NodeIdOneOf<T, U> Invalid;
  61. // TODO: Support conversion from `NodeIdForKind<Kind>` if `Kind` is
  62. // `T::Kind` or `U::Kind`.
  63. explicit NodeIdOneOf(NodeId node_id) : NodeId(node_id) {}
  64. };
  65. template <typename T, typename U>
  66. constexpr NodeIdOneOf<T, U> NodeIdOneOf<T, U>::Invalid =
  67. NodeIdOneOf<T, U>(NodeId::InvalidIndex);
  68. // NodeId with kind that is anything but T::Kind.
  69. template <typename T>
  70. struct NodeIdNot : public NodeId {
  71. // An explicitly invalid instance.
  72. static const NodeIdNot<T> Invalid;
  73. explicit NodeIdNot(NodeId node_id) : NodeId(node_id) {}
  74. };
  75. template <typename T>
  76. constexpr NodeIdNot<T> NodeIdNot<T>::Invalid =
  77. NodeIdNot<T>(NodeId::InvalidIndex);
  78. // Note that the support for extracting these types using the `Tree::Extract*`
  79. // functions is defined in `extract.cpp`.
  80. } // namespace Carbon::Parse
  81. #endif // CARBON_TOOLCHAIN_PARSE_NODE_IDS_H_