node_kind.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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) NodeKind::BracketName,
  24. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) NodeKind::Name,
  25. #include "toolchain/parse/node_kind.def"
  26. };
  27. auto bracket = Bracket[AsInt()];
  28. CARBON_CHECK(bracket != *this) << *this;
  29. return bracket;
  30. }
  31. auto NodeKind::child_count() const -> int32_t {
  32. static constexpr int32_t ChildCount[] = {
  33. #define CARBON_PARSE_NODE_KIND_BRACKET(...) -1,
  34. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size) Size,
  35. #include "toolchain/parse/node_kind.def"
  36. };
  37. auto child_count = ChildCount[AsInt()];
  38. CARBON_CHECK(child_count >= 0) << *this;
  39. return child_count;
  40. }
  41. } // namespace Carbon::Parse