node_kind.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if (!value_) {
  10. out << "<none>";
  11. } else {
  12. llvm::ListSeparator sep("|");
  13. #define CARBON_NODE_CATEGORY(Name) \
  14. if (value_ & NodeCategory::Name) { \
  15. out << sep << #Name; \
  16. }
  17. CARBON_NODE_CATEGORY(Decl);
  18. CARBON_NODE_CATEGORY(Expr);
  19. CARBON_NODE_CATEGORY(ImplAs);
  20. CARBON_NODE_CATEGORY(MemberExpr);
  21. CARBON_NODE_CATEGORY(MemberName);
  22. CARBON_NODE_CATEGORY(Modifier);
  23. CARBON_NODE_CATEGORY(Pattern);
  24. CARBON_NODE_CATEGORY(Statement);
  25. #undef CARBON_NODE_CATEGORY
  26. }
  27. }
  28. CARBON_DEFINE_ENUM_CLASS_NAMES(NodeKind) = {
  29. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  30. #include "toolchain/parse/node_kind.def"
  31. };
  32. // Check that each typed node defines a `token` member.
  33. #define CARBON_PARSE_NODE_KIND(Name) \
  34. static_assert(requires(Name node) { node.token; });
  35. #include "toolchain/parse/node_kind.def"
  36. auto NodeKind::has_bracket() const -> bool {
  37. return definition().has_bracket();
  38. }
  39. auto NodeKind::bracket() const -> NodeKind { return definition().bracket(); }
  40. auto NodeKind::has_child_count() const -> bool {
  41. return definition().has_child_count();
  42. }
  43. auto NodeKind::child_count() const -> int32_t {
  44. return definition().child_count();
  45. }
  46. auto NodeKind::category() const -> NodeCategory {
  47. return definition().category();
  48. }
  49. auto NodeKind::definition() const -> const Definition& {
  50. static constexpr const Definition* Table[] = {
  51. #define CARBON_PARSE_NODE_KIND(Name) &Parse::Name::Kind,
  52. #include "toolchain/parse/node_kind.def"
  53. };
  54. return *Table[AsInt()];
  55. }
  56. } // namespace Carbon::Parse