node_kind.h 7.7 KB

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