node_kind.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. 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. // Check that each typed node defines a `token` member.
  13. #define CARBON_PARSE_NODE_KIND(Name) \
  14. static_assert(requires(Name node) { node.token; });
  15. #include "toolchain/parse/node_kind.def"
  16. auto NodeKind::has_bracket() const -> bool {
  17. return definition().has_bracket();
  18. }
  19. auto NodeKind::bracket() const -> NodeKind { return definition().bracket(); }
  20. auto NodeKind::has_child_count() const -> bool {
  21. return definition().has_child_count();
  22. }
  23. auto NodeKind::child_count() const -> int32_t {
  24. return definition().child_count();
  25. }
  26. auto NodeKind::category() const -> NodeCategory {
  27. return definition().category();
  28. }
  29. auto NodeKind::definition() const -> const Definition& {
  30. static constexpr const Definition* Table[] = {
  31. #define CARBON_PARSE_NODE_KIND(Name) &Parse::Name::Kind,
  32. #include "toolchain/parse/node_kind.def"
  33. };
  34. return *Table[AsInt()];
  35. }
  36. } // namespace Carbon::Parse