node_kind.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "toolchain/lex/token_kind.h"
  10. #include "toolchain/parse/node_category.h"
  11. namespace Carbon::Parse {
  12. CARBON_DEFINE_RAW_ENUM_CLASS(NodeKind, uint16_t) {
  13. #define CARBON_PARSE_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  14. #include "toolchain/parse/node_kind.def"
  15. };
  16. // A class wrapping an enumeration of the different kinds of nodes in the parse
  17. // tree.
  18. //
  19. // In order to allow the children of a node to be determined without relying on
  20. // the subtree size field in the parse node, each node kind must have one of:
  21. //
  22. // - a bracketing node kind, which is always the kind for the first child, and
  23. // is never the kind of any other child, or
  24. // - a fixed child count,
  25. //
  26. // or both. This is required even for nodes for which `Tree::node_has_errors`
  27. // returns `true`.
  28. class NodeKind : public CARBON_ENUM_BASE(NodeKind) {
  29. public:
  30. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  31. #include "toolchain/parse/node_kind.def"
  32. // Validates that a `node_kind` parser node can be generated for a
  33. // `lex_token_kind` lexer token.
  34. auto CheckMatchesTokenKind(Lex::TokenKind lex_token_kind, bool has_error)
  35. -> void;
  36. // Returns true if the node is bracketed.
  37. auto has_bracket() const -> bool;
  38. // Returns the bracketing node kind for the current node kind. Requires that
  39. // has_bracket is true.
  40. auto bracket() const -> NodeKind;
  41. // Returns true if the node is has a fixed child count.
  42. auto has_child_count() const -> bool;
  43. // Returns the number of children that the node must have, often 0. Requires
  44. // that has_child_count is true.
  45. auto child_count() const -> int32_t;
  46. // Returns which categories this node kind is in.
  47. auto category() const -> NodeCategory;
  48. // Number of different kinds, usable in a constexpr context.
  49. static const int ValidCount;
  50. using EnumBase::AsInt;
  51. using EnumBase::Make;
  52. class Definition;
  53. struct DefinitionArgs;
  54. // Provides a definition for this parse node kind. Should only be called
  55. // once, to construct the kind as part of defining it in `typed_nodes.h`.
  56. constexpr auto Define(DefinitionArgs args) const -> Definition;
  57. private:
  58. // Looks up the definition for this instruction kind.
  59. auto definition() const -> const Definition&;
  60. };
  61. #define CARBON_PARSE_NODE_KIND(Name) \
  62. CARBON_ENUM_CONSTANT_DEFINITION(NodeKind, Name)
  63. #include "toolchain/parse/node_kind.def"
  64. inline constexpr int NodeKind::ValidCount = 0
  65. #define CARBON_PARSE_NODE_KIND(Name) +1
  66. #include "toolchain/parse/node_kind.def"
  67. ;
  68. static_assert(
  69. NodeKind::ValidCount != 0,
  70. "The above `constexpr` definition of `ValidCount` makes it available in "
  71. "a `constexpr` context despite being declared as merely `const`. We use it "
  72. "in a static assert here to ensure that.");
  73. // We expect the parse node kind to fit compactly into 16 bits.
  74. static_assert(sizeof(NodeKind) == 2, "Kind objects include padding!");
  75. // Optional arguments that can be supplied when defining a node kind. At least
  76. // one of `bracketed_by` and `child_count` is required.
  77. struct NodeKind::DefinitionArgs {
  78. // The category for the node.
  79. NodeCategory category = NodeCategory::None;
  80. // The kind of the bracketing node, which is the first child.
  81. std::optional<NodeKind> bracketed_by = std::nullopt;
  82. // The fixed child count.
  83. int32_t child_count = -1;
  84. };
  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 true if the node is bracketed.
  96. constexpr auto has_bracket() const -> bool { return bracket_ != *this; }
  97. // Returns the bracketing node kind for the current node kind. Requires that
  98. // has_bracket is true.
  99. constexpr auto bracket() const -> NodeKind {
  100. CARBON_CHECK(has_bracket(), "{0}", *this);
  101. return bracket_;
  102. }
  103. // Returns true if the node is has a fixed child count.
  104. constexpr auto has_child_count() const -> bool { return child_count_ >= 0; }
  105. // Returns the number of children that the node must have, often 0. Requires
  106. // that has_child_count is true.
  107. constexpr auto child_count() const -> int32_t {
  108. CARBON_CHECK(has_child_count(), "{0}", *this);
  109. return child_count_;
  110. }
  111. // Returns which categories this node kind is in.
  112. constexpr auto category() const -> NodeCategory { return category_; }
  113. private:
  114. friend class NodeKind;
  115. // This is factored out and non-constexpr to improve the compile-time error
  116. // message if the check below fails.
  117. auto MustSpecifyEitherBracketingNodeOrChildCount() -> void {
  118. CARBON_FATAL("Must specify either bracketing node or fixed child count.");
  119. }
  120. constexpr explicit Definition(NodeKind kind, DefinitionArgs args)
  121. : NodeKind(kind),
  122. category_(args.category),
  123. bracket_(args.bracketed_by.value_or(kind)),
  124. child_count_(args.child_count) {
  125. if (!has_bracket() && !has_child_count()) {
  126. MustSpecifyEitherBracketingNodeOrChildCount();
  127. }
  128. }
  129. NodeCategory category_ = NodeCategory::None;
  130. // Nodes are never self-bracketed, so we use *this to indicate that the node
  131. // is not bracketed.
  132. NodeKind bracket_ = *this;
  133. int32_t child_count_ = -1;
  134. };
  135. constexpr auto NodeKind::Define(DefinitionArgs args) const -> Definition {
  136. return Definition(*this, args);
  137. }
  138. // HasKindMember<T> is true if T has a `static const NodeKind::Definition Kind`
  139. // member.
  140. template <typename T, typename KindType = const NodeKind::Definition*>
  141. inline constexpr bool HasKindMember = false;
  142. template <typename T>
  143. inline constexpr bool HasKindMember<T, decltype(&T::Kind)> = true;
  144. } // namespace Carbon::Parse
  145. #endif // CARBON_TOOLCHAIN_PARSE_NODE_KIND_H_