node_kind.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "toolchain/lex/token_kind.h"
  9. namespace Carbon::Parse {
  10. CARBON_DEFINE_RAW_ENUM_CLASS(NodeKind, uint8_t) {
  11. #define CARBON_PARSE_NODE_KIND(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  12. #include "toolchain/parse/node_kind.def"
  13. };
  14. // A class wrapping an enumeration of the different kinds of nodes in the parse
  15. // tree.
  16. class NodeKind : public CARBON_ENUM_BASE(NodeKind) {
  17. public:
  18. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  19. #include "toolchain/parse/node_kind.def"
  20. // Validates that a `parse_node_kind` parser node can be generated for a
  21. // `lex_token_kind` lexer token.
  22. auto CheckMatchesTokenKind(Lex::TokenKind lex_token_kind, bool has_error)
  23. -> void;
  24. // Returns true if the node is bracketed; otherwise, child_count is used.
  25. auto has_bracket() const -> bool;
  26. // Returns the bracketing node kind for the current node kind. Requires that
  27. // has_bracket is true.
  28. auto bracket() const -> NodeKind;
  29. // Returns the number of children that the node must have, often 0. Requires
  30. // that has_bracket is false.
  31. auto child_count() const -> int32_t;
  32. using EnumBase::Create;
  33. };
  34. #define CARBON_PARSE_NODE_KIND(Name) \
  35. CARBON_ENUM_CONSTANT_DEFINITION(NodeKind, Name)
  36. #include "toolchain/parse/node_kind.def"
  37. // We expect the parse node kind to fit compactly into 8 bits.
  38. static_assert(sizeof(NodeKind) == 1, "Kind objects include padding!");
  39. } // namespace Carbon::Parse
  40. #endif // CARBON_TOOLCHAIN_PARSE_NODE_KIND_H_