node_kind.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include "toolchain/parse/node_kind.h"
  5. #include "common/check.h"
  6. #include "toolchain/parse/typed_nodes.h"
  7. namespace Carbon::Parse {
  8. CARBON_DEFINE_ENUM_CLASS_NAMES(NodeKind) = {
  9. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  10. #include "toolchain/parse/node_kind.def"
  11. };
  12. auto NodeKind::has_bracket() const -> bool {
  13. static constexpr bool HasBracket[] = {
  14. #define CARBON_PARSE_NODE_KIND_BRACKET(...) true,
  15. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(...) false,
  16. #include "toolchain/parse/node_kind.def"
  17. };
  18. return HasBracket[AsInt()];
  19. }
  20. auto NodeKind::bracket() const -> NodeKind {
  21. // Nodes are never self-bracketed, so we use that for nodes that instead set
  22. // child_count.
  23. static constexpr NodeKind Bracket[] = {
  24. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, ...) \
  25. NodeKind::BracketName,
  26. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) NodeKind::Name,
  27. #include "toolchain/parse/node_kind.def"
  28. };
  29. auto bracket = Bracket[AsInt()];
  30. CARBON_CHECK(bracket != *this) << *this;
  31. return bracket;
  32. }
  33. auto NodeKind::child_count() const -> int32_t {
  34. static constexpr int32_t ChildCount[] = {
  35. #define CARBON_PARSE_NODE_KIND_BRACKET(...) -1,
  36. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, ...) Size,
  37. #include "toolchain/parse/node_kind.def"
  38. };
  39. auto child_count = ChildCount[AsInt()];
  40. CARBON_CHECK(child_count >= 0) << *this;
  41. return child_count;
  42. }
  43. auto NodeKind::CheckMatchesTokenKind(Lex::TokenKind token_kind, bool has_error)
  44. -> void {
  45. static constexpr Lex::TokenKind TokenIfValid[] = {
  46. #define CARBON_IF_VALID(LexTokenKind) LexTokenKind
  47. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, LexTokenKind) \
  48. Lex::TokenKind::LexTokenKind,
  49. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, LexTokenKind) \
  50. Lex::TokenKind::LexTokenKind,
  51. #include "toolchain/parse/node_kind.def"
  52. };
  53. static constexpr Lex::TokenKind TokenIfError[] = {
  54. #define CARBON_IF_VALID(LexTokenKind) Error
  55. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, LexTokenKind) \
  56. Lex::TokenKind::LexTokenKind,
  57. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, LexTokenKind) \
  58. Lex::TokenKind::LexTokenKind,
  59. #include "toolchain/parse/node_kind.def"
  60. };
  61. Lex::TokenKind expected_token_kind =
  62. has_error ? TokenIfError[AsInt()] : TokenIfValid[AsInt()];
  63. // Error indicates that the kind shouldn't be enforced.
  64. CARBON_CHECK(Lex::TokenKind::Error == expected_token_kind ||
  65. token_kind == expected_token_kind)
  66. << "Created parse node with NodeKind " << *this << " and has_error "
  67. << has_error << " for lexical token kind " << token_kind
  68. << ", but expected token kind " << expected_token_kind;
  69. }
  70. auto NodeKind::category() const -> NodeCategory {
  71. return definition().category();
  72. }
  73. auto NodeKind::definition() const -> const Definition& {
  74. static constexpr const Definition* Table[] = {
  75. #define CARBON_PARSE_NODE_KIND(Name) &Parse::Name::Kind,
  76. #include "toolchain/parse/node_kind.def"
  77. };
  78. return *Table[AsInt()];
  79. }
  80. } // namespace Carbon::Parse