node_kind.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_KIND_H_
  5. #define CARBON_TOOLCHAIN_PARSE_NODE_KIND_H_
  6. #include <cstdint>
  7. #include "common/enum_base.h"
  8. #include "llvm/ADT/BitmaskEnum.h"
  9. #include "toolchain/lex/token_kind.h"
  10. namespace Carbon::Parse {
  11. LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
  12. // Represents a set of keyword modifiers, using a separate bit per modifier.
  13. //
  14. // We expect this to grow, so are using a bigger size than needed.
  15. // NOLINTNEXTLINE(performance-enum-size)
  16. enum class NodeCategory : uint32_t {
  17. Decl = 1 << 0,
  18. Expr = 1 << 1,
  19. ImplAs = 1 << 2,
  20. MemberExpr = 1 << 3,
  21. MemberName = 1 << 4,
  22. Modifier = 1 << 5,
  23. NameComponent = 1 << 6,
  24. Pattern = 1 << 7,
  25. Statement = 1 << 8,
  26. None = 0,
  27. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Statement)
  28. };
  29. inline constexpr auto operator!(NodeCategory k) -> bool {
  30. return !static_cast<uint32_t>(k);
  31. }
  32. auto operator<<(llvm::raw_ostream& output, NodeCategory category)
  33. -> llvm::raw_ostream&;
  34. CARBON_DEFINE_RAW_ENUM_CLASS(NodeKind, uint8_t) {
  35. #define CARBON_PARSE_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  36. #include "toolchain/parse/node_kind.def"
  37. };
  38. // A class wrapping an enumeration of the different kinds of nodes in the parse
  39. // tree.
  40. class NodeKind : public CARBON_ENUM_BASE(NodeKind) {
  41. public:
  42. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  43. #include "toolchain/parse/node_kind.def"
  44. // Validates that a `node_kind` parser node can be generated for a
  45. // `lex_token_kind` lexer token.
  46. auto CheckMatchesTokenKind(Lex::TokenKind lex_token_kind, bool has_error)
  47. -> void;
  48. // Returns true if the node is bracketed; otherwise, child_count is used.
  49. auto has_bracket() const -> bool;
  50. // Returns the bracketing node kind for the current node kind. Requires that
  51. // has_bracket is true.
  52. auto bracket() const -> NodeKind;
  53. // Returns the number of children that the node must have, often 0. Requires
  54. // that has_bracket is false.
  55. auto child_count() const -> int32_t;
  56. // Returns which categories this node kind is in.
  57. auto category() const -> NodeCategory;
  58. // Number of different kinds, usable in a constexpr context.
  59. static const int ValidCount;
  60. using EnumBase::AsInt;
  61. using EnumBase::Make;
  62. class Definition;
  63. // Provides a definition for this parse node kind. Should only be called
  64. // once, to construct the kind as part of defining it in `typed_nodes.h`.
  65. constexpr auto Define(NodeCategory category = NodeCategory::None) const
  66. -> Definition;
  67. private:
  68. // Looks up the definition for this instruction kind.
  69. auto definition() const -> const Definition&;
  70. };
  71. #define CARBON_PARSE_NODE_KIND(Name) \
  72. CARBON_ENUM_CONSTANT_DEFINITION(NodeKind, Name)
  73. #include "toolchain/parse/node_kind.def"
  74. constexpr int NodeKind::ValidCount = 0
  75. #define CARBON_PARSE_NODE_KIND(Name) +1
  76. #include "toolchain/parse/node_kind.def"
  77. ;
  78. static_assert(
  79. NodeKind::ValidCount != 0,
  80. "The above `constexpr` definition of `ValidCount` makes it available in "
  81. "a `constexpr` context despite being declared as merely `const`. We use it "
  82. "in a static assert here to ensure that.");
  83. // We expect the parse node kind to fit compactly into 8 bits.
  84. static_assert(sizeof(NodeKind) == 1, "Kind objects include padding!");
  85. // A definition of a parse node kind. This is a NodeKind value, plus
  86. // ancillary data such as the name to use for the node kind in LLVM IR. These
  87. // are not copyable, and only one instance of this type is expected to exist per
  88. // parse node kind, specifically `TypedNode::Kind`. Use `NodeKind` instead as a
  89. // thin wrapper around a parse node kind index.
  90. class NodeKind::Definition : public NodeKind {
  91. public:
  92. // Not copyable.
  93. Definition(const Definition&) = delete;
  94. auto operator=(const Definition&) -> Definition& = delete;
  95. // Returns which categories this node kind is in.
  96. constexpr auto category() const -> NodeCategory { return category_; }
  97. private:
  98. friend class NodeKind;
  99. constexpr Definition(NodeKind kind, NodeCategory category)
  100. : NodeKind(kind), category_(category) {}
  101. NodeCategory category_;
  102. };
  103. constexpr auto NodeKind::Define(NodeCategory category) const -> Definition {
  104. return Definition(*this, category);
  105. }
  106. // HasKindMember<T> is true if T has a `static const NodeKind::Definition Kind`
  107. // member.
  108. template <typename T, typename KindType = const NodeKind::Definition*>
  109. inline constexpr bool HasKindMember = false;
  110. template <typename T>
  111. inline constexpr bool HasKindMember<T, decltype(&T::Kind)> = true;
  112. } // namespace Carbon::Parse
  113. #endif // CARBON_TOOLCHAIN_PARSE_NODE_KIND_H_