parse_node_kind.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. namespace Carbon {
  7. CARBON_DEFINE_ENUM_CLASS_NAMES(ParseNodeKind) = {
  8. #define CARBON_PARSE_NODE_KIND(Name) CARBON_ENUM_CLASS_NAME_STRING(Name)
  9. #include "toolchain/parser/parse_node_kind.def"
  10. };
  11. auto ParseNodeKind::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/parser/parse_node_kind.def"
  16. };
  17. return HasBracket[AsInt()];
  18. }
  19. auto ParseNodeKind::bracket() const -> ParseNodeKind {
  20. // Nodes are never self-bracketed, so we use that for nodes that instead set
  21. // child_count.
  22. static constexpr ParseNodeKind Bracket[] = {
  23. #define CARBON_PARSE_NODE_KIND_BRACKET(Name, BracketName) \
  24. ParseNodeKind::BracketName,
  25. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, ...) ParseNodeKind::Name,
  26. #include "toolchain/parser/parse_node_kind.def"
  27. };
  28. auto bracket = Bracket[AsInt()];
  29. CARBON_CHECK(bracket != *this) << *this;
  30. return bracket;
  31. }
  32. auto ParseNodeKind::child_count() const -> int32_t {
  33. static constexpr int32_t ChildCount[] = {
  34. #define CARBON_PARSE_NODE_KIND_BRACKET(...) -1,
  35. #define CARBON_PARSE_NODE_KIND_CHILD_COUNT(Name, Size) Size,
  36. #include "toolchain/parser/parse_node_kind.def"
  37. };
  38. auto child_count = ChildCount[AsInt()];
  39. CARBON_CHECK(child_count >= 0) << *this;
  40. return child_count;
  41. }
  42. } // namespace Carbon