node_kind.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "common/ostream.h"
  9. #include "llvm/ADT/BitmaskEnum.h"
  10. #include "toolchain/lex/token_kind.h"
  11. namespace Carbon::Parse {
  12. LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
  13. // Represents a set of keyword modifiers, using a separate bit per modifier.
  14. class NodeCategory : public Printable<NodeCategory> {
  15. public:
  16. // Provide values as an enum. This doesn't expose these as NodeCategory
  17. // instances just due to the duplication of declarations that would cause.
  18. //
  19. // We expect this to grow, so are using a bigger size than needed.
  20. // NOLINTNEXTLINE(performance-enum-size)
  21. enum RawEnumType : uint32_t {
  22. Decl = 1 << 0,
  23. Expr = 1 << 1,
  24. ImplAs = 1 << 2,
  25. MemberExpr = 1 << 3,
  26. MemberName = 1 << 4,
  27. Modifier = 1 << 5,
  28. Pattern = 1 << 6,
  29. Statement = 1 << 7,
  30. IntConst = 1 << 8,
  31. Requirement = 1 << 9,
  32. None = 0,
  33. LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/Requirement)
  34. };
  35. // Support implicit conversion so that the difference with the member enum is
  36. // opaque.
  37. // NOLINTNEXTLINE(google-explicit-constructor)
  38. constexpr NodeCategory(RawEnumType value) : value_(value) {}
  39. // Returns true if there's a non-empty set intersection.
  40. constexpr auto HasAnyOf(NodeCategory other) -> bool {
  41. return value_ & other.value_;
  42. }
  43. // Returns the set inverse.
  44. constexpr auto operator~() -> NodeCategory { return ~value_; }
  45. auto operator==(const NodeCategory& other) const -> bool {
  46. return value_ == other.value_;
  47. }
  48. auto Print(llvm::raw_ostream& out) const -> void;
  49. private:
  50. RawEnumType value_;
  51. };
  52. CARBON_DEFINE_RAW_ENUM_CLASS(NodeKind, uint8_t) {
  53. #define CARBON_PARSE_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  54. #include "toolchain/parse/node_kind.def"
  55. };
  56. // A class wrapping an enumeration of the different kinds of nodes in the parse
  57. // tree.
  58. //
  59. // In order to allow the children of a node to be determined without relying on
  60. // the subtree size field in the parse node, each node kind must have one of:
  61. //
  62. // - a bracketing node kind, which is always the kind for the first child, and
  63. // is never the kind of any other child, or
  64. // - a fixed child count,
  65. //
  66. // or both. This is required even for nodes for which `Tree::node_has_errors`
  67. // returns `true`.
  68. class NodeKind : public CARBON_ENUM_BASE(NodeKind) {
  69. public:
  70. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  71. #include "toolchain/parse/node_kind.def"
  72. // Validates that a `node_kind` parser node can be generated for a
  73. // `lex_token_kind` lexer token.
  74. auto CheckMatchesTokenKind(Lex::TokenKind lex_token_kind, bool has_error)
  75. -> void;
  76. // Returns true if the node is bracketed.
  77. auto has_bracket() const -> bool;
  78. // Returns the bracketing node kind for the current node kind. Requires that
  79. // has_bracket is true.
  80. auto bracket() const -> NodeKind;
  81. // Returns true if the node is has a fixed child count.
  82. auto has_child_count() const -> bool;
  83. // Returns the number of children that the node must have, often 0. Requires
  84. // that has_child_count is true.
  85. auto child_count() const -> int32_t;
  86. // Returns which categories this node kind is in.
  87. auto category() const -> NodeCategory;
  88. // Number of different kinds, usable in a constexpr context.
  89. static const int ValidCount;
  90. using EnumBase::AsInt;
  91. using EnumBase::Make;
  92. class Definition;
  93. struct DefinitionArgs;
  94. // Provides a definition for this parse node kind. Should only be called
  95. // once, to construct the kind as part of defining it in `typed_nodes.h`.
  96. constexpr auto Define(DefinitionArgs args) const -> Definition;
  97. private:
  98. // Looks up the definition for this instruction kind.
  99. auto definition() const -> const Definition&;
  100. };
  101. #define CARBON_PARSE_NODE_KIND(Name) \
  102. CARBON_ENUM_CONSTANT_DEFINITION(NodeKind, Name)
  103. #include "toolchain/parse/node_kind.def"
  104. constexpr int NodeKind::ValidCount = 0
  105. #define CARBON_PARSE_NODE_KIND(Name) +1
  106. #include "toolchain/parse/node_kind.def"
  107. ;
  108. static_assert(
  109. NodeKind::ValidCount != 0,
  110. "The above `constexpr` definition of `ValidCount` makes it available in "
  111. "a `constexpr` context despite being declared as merely `const`. We use it "
  112. "in a static assert here to ensure that.");
  113. // We expect the parse node kind to fit compactly into 8 bits.
  114. static_assert(sizeof(NodeKind) == 1, "Kind objects include padding!");
  115. // Optional arguments that can be supplied when defining a node kind. At least
  116. // one of `bracketed_by` and `child_count` is required.
  117. struct NodeKind::DefinitionArgs {
  118. // The category for the node.
  119. NodeCategory category = NodeCategory::None;
  120. // The kind of the bracketing node, which is the first child.
  121. std::optional<NodeKind> bracketed_by = std::nullopt;
  122. // The fixed child count.
  123. int32_t child_count = -1;
  124. };
  125. // A definition of a parse node kind. This is a NodeKind value, plus
  126. // ancillary data such as the name to use for the node kind in LLVM IR. These
  127. // are not copyable, and only one instance of this type is expected to exist per
  128. // parse node kind, specifically `TypedNode::Kind`. Use `NodeKind` instead as a
  129. // thin wrapper around a parse node kind index.
  130. class NodeKind::Definition : public NodeKind {
  131. public:
  132. // Not copyable.
  133. Definition(const Definition&) = delete;
  134. auto operator=(const Definition&) -> Definition& = delete;
  135. // Returns true if the node is bracketed.
  136. constexpr auto has_bracket() const -> bool { return bracket_ != *this; }
  137. // Returns the bracketing node kind for the current node kind. Requires that
  138. // has_bracket is true.
  139. constexpr auto bracket() const -> NodeKind {
  140. CARBON_CHECK(has_bracket(), "{0}", *this);
  141. return bracket_;
  142. }
  143. // Returns true if the node is has a fixed child count.
  144. constexpr auto has_child_count() const -> bool { return child_count_ >= 0; }
  145. // Returns the number of children that the node must have, often 0. Requires
  146. // that has_child_count is true.
  147. constexpr auto child_count() const -> int32_t {
  148. CARBON_CHECK(has_child_count(), "{0}", *this);
  149. return child_count_;
  150. }
  151. // Returns which categories this node kind is in.
  152. constexpr auto category() const -> NodeCategory { return category_; }
  153. private:
  154. friend class NodeKind;
  155. // This is factored out and non-constexpr to improve the compile-time error
  156. // message if the check below fails.
  157. auto MustSpecifyEitherBracketingNodeOrChildCount() {
  158. CARBON_FATAL("Must specify either bracketing node or fixed child count.");
  159. }
  160. constexpr explicit Definition(NodeKind kind, DefinitionArgs args)
  161. : NodeKind(kind),
  162. category_(args.category),
  163. bracket_(args.bracketed_by.value_or(kind)),
  164. child_count_(args.child_count) {
  165. if (!has_bracket() && !has_child_count()) {
  166. MustSpecifyEitherBracketingNodeOrChildCount();
  167. }
  168. }
  169. NodeCategory category_ = NodeCategory::None;
  170. // Nodes are never self-bracketed, so we use *this to indicate that the node
  171. // is not bracketed.
  172. NodeKind bracket_ = *this;
  173. int32_t child_count_ = -1;
  174. };
  175. constexpr auto NodeKind::Define(DefinitionArgs args) const -> Definition {
  176. return Definition(*this, args);
  177. }
  178. // HasKindMember<T> is true if T has a `static const NodeKind::Definition Kind`
  179. // member.
  180. template <typename T, typename KindType = const NodeKind::Definition*>
  181. inline constexpr bool HasKindMember = false;
  182. template <typename T>
  183. inline constexpr bool HasKindMember<T, decltype(&T::Kind)> = true;
  184. } // namespace Carbon::Parse
  185. #endif // CARBON_TOOLCHAIN_PARSE_NODE_KIND_H_