node_kind.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. namespace Carbon::Parse {
  7. CARBON_DEFINE_ENUM_CLASS_NAMES(NodeKind) = {
  8. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  9. #include "toolchain/parse/node_kind.def"
  10. };
  11. auto NodeKind::has_bracket() const -> bool {
  12. static constexpr bool HasBracket[] = {
  13. #define CARBON_PARSE_NODE_KIND_BRACKET(...) true,
  14. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(...) false,
  15. #include "toolchain/parse/node_kind.def"
  16. };
  17. return HasBracket[AsInt()];
  18. }
  19. auto NodeKind::bracket() const -> NodeKind {
  20. // Nodes are never self-bracketed, so we use that for nodes that instead set
  21. // child_count.
  22. static constexpr NodeKind Bracket[] = {
  23. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, ...) \
  24. NodeKind::BracketName,
  25. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) NodeKind::Name,
  26. #include "toolchain/parse/node_kind.def"
  27. };
  28. auto bracket = Bracket[AsInt()];
  29. CARBON_CHECK(bracket != *this) << *this;
  30. return bracket;
  31. }
  32. auto NodeKind::child_count() const -> int32_t {
  33. static constexpr int32_t ChildCount[] = {
  34. #define CARBON_PARSE_NODE_KIND_BRACKET(...) -1,
  35. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, ...) Size,
  36. #include "toolchain/parse/node_kind.def"
  37. };
  38. auto child_count = ChildCount[AsInt()];
  39. CARBON_CHECK(child_count >= 0) << *this;
  40. return child_count;
  41. }
  42. // NOLINTNEXTLINE(readability-function-size): It's hard to extract macros.
  43. auto CheckNodeMatchesLexerToken(NodeKind node_kind, Lex::TokenKind token_kind,
  44. bool has_error) -> void {
  45. // As a special-case, a placeholder node may correspond to any lexer token.
  46. if (node_kind == NodeKind::Placeholder) {
  47. return;
  48. }
  49. switch (node_kind) {
  50. // Use `CARBON_LOG CARBON_ANY_TOKEN` to discover which combinations happen
  51. // in practice.
  52. #define CARBON_LOG \
  53. llvm::errs() << "ZZZ: Created parse node with NodeKind " << node_kind \
  54. << " and has_error " << has_error << " for lexical token " \
  55. << token_kind << "\n";
  56. #define CARBON_TOKEN(Expected) \
  57. if (token_kind == Lex::TokenKind::Expected) { \
  58. return; \
  59. }
  60. #define CARBON_ANY_TOKEN_ON_ERROR \
  61. if (has_error) { \
  62. return; \
  63. }
  64. #define CARBON_CASE(Name, MatchActions) \
  65. case NodeKind::Name: \
  66. MatchActions; /* NOLINT(bugprone-macro-parentheses) */ \
  67. break;
  68. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName, MatchActions) \
  69. CARBON_CASE(Name, MatchActions)
  70. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size, MatchActions) \
  71. CARBON_CASE(Name, MatchActions)
  72. #include "toolchain/parse/node_kind.def"
  73. #undef CARBON_LOG
  74. #undef CARBON_CASE
  75. }
  76. CARBON_FATAL() << "Created parse node with NodeKind " << node_kind
  77. << " and has_error " << has_error
  78. << " for unexpected lexical token " << token_kind;
  79. }
  80. } // namespace Carbon::Parse