node_kind.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "llvm/ADT/StringExtras.h"
  6. #include "toolchain/parse/typed_nodes.h"
  7. namespace Carbon::Parse {
  8. auto NodeCategory::Print(llvm::raw_ostream& out) const -> void {
  9. llvm::ListSeparator sep("|");
  10. auto value = value_;
  11. do {
  12. // The lowest set bit in the value, or 0 (`None`) if no bits are set.
  13. auto lowest_bit = static_cast<RawEnumType>(value & -value);
  14. switch (lowest_bit) {
  15. #define CARBON_NODE_CATEGORY(Name) \
  16. case NodeCategory::Name: { \
  17. out << sep << #Name; \
  18. break; \
  19. }
  20. CARBON_NODE_CATEGORY(Decl);
  21. CARBON_NODE_CATEGORY(Expr);
  22. CARBON_NODE_CATEGORY(ImplAs);
  23. CARBON_NODE_CATEGORY(MemberExpr);
  24. CARBON_NODE_CATEGORY(MemberName);
  25. CARBON_NODE_CATEGORY(Modifier);
  26. CARBON_NODE_CATEGORY(Pattern);
  27. CARBON_NODE_CATEGORY(Statement);
  28. CARBON_NODE_CATEGORY(IntConst);
  29. CARBON_NODE_CATEGORY(Requirement);
  30. CARBON_NODE_CATEGORY(NonExprIdentifierName);
  31. CARBON_NODE_CATEGORY(PackageName);
  32. CARBON_NODE_CATEGORY(None);
  33. #undef CARBON_NODE_CATEGORY
  34. }
  35. value &= ~lowest_bit;
  36. } while (value);
  37. }
  38. CARBON_DEFINE_ENUM_CLASS_NAMES(NodeKind) = {
  39. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  40. #include "toolchain/parse/node_kind.def"
  41. };
  42. // Check that each typed node defines a `token` member.
  43. #define CARBON_PARSE_NODE_KIND(Name) \
  44. static_assert(requires(Name node) { node.token; });
  45. #include "toolchain/parse/node_kind.def"
  46. auto NodeKind::has_bracket() const -> bool {
  47. return definition().has_bracket();
  48. }
  49. auto NodeKind::bracket() const -> NodeKind { return definition().bracket(); }
  50. auto NodeKind::has_child_count() const -> bool {
  51. return definition().has_child_count();
  52. }
  53. auto NodeKind::child_count() const -> int32_t {
  54. return definition().child_count();
  55. }
  56. auto NodeKind::category() const -> NodeCategory {
  57. return definition().category();
  58. }
  59. auto NodeKind::definition() const -> const Definition& {
  60. static constexpr const Definition* Table[] = {
  61. #define CARBON_PARSE_NODE_KIND(Name) &Parse::Name::Kind,
  62. #include "toolchain/parse/node_kind.def"
  63. };
  64. return *Table[AsInt()];
  65. }
  66. } // namespace Carbon::Parse