parse_node_kind.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/parser/parse_node_kind.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/StringRef.h"
  7. namespace Carbon {
  8. auto ParseNodeKind::name() const -> llvm::StringRef {
  9. static constexpr llvm::StringLiteral Names[] = {
  10. #define CARBON_PARSE_NODE_KIND(Name) #Name,
  11. #include "toolchain/parser/parse_node_kind.def"
  12. };
  13. return Names[static_cast<int>(kind_)];
  14. }
  15. auto ParseNodeKind::has_bracket() const -> bool {
  16. static constexpr bool HasBracket[] = {
  17. #define CARBON_PARSE_NODE_KIND_BRACKET(...) true,
  18. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(...) false,
  19. #include "toolchain/parser/parse_node_kind.def"
  20. };
  21. return HasBracket[static_cast<int>(kind_)];
  22. }
  23. auto ParseNodeKind::bracket() const -> ParseNodeKind {
  24. // Nodes are never self-bracketed, so we use that for nodes that instead set
  25. // child_count.
  26. static constexpr ParseNodeKind Bracket[] = {
  27. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName) \
  28. ParseNodeKind::BracketName(),
  29. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) ParseNodeKind::Name(),
  30. #include "toolchain/parser/parse_node_kind.def"
  31. };
  32. auto bracket = Bracket[static_cast<int>(kind_)];
  33. CARBON_CHECK(bracket != kind_) << *this;
  34. return bracket;
  35. }
  36. auto ParseNodeKind::child_count() const -> int32_t {
  37. static constexpr int32_t ChildCount[] = {
  38. #define CARBON_PARSE_NODE_KIND_BRACKET(...) -1,
  39. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size) Size,
  40. #include "toolchain/parser/parse_node_kind.def"
  41. };
  42. auto child_count = ChildCount[static_cast<int>(kind_)];
  43. CARBON_CHECK(child_count >= 0) << *this;
  44. return child_count;
  45. }
  46. } // namespace Carbon