parse_node_kind.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef CARBON_TOOLCHAIN_PARSER_PARSE_NODE_KIND_H_
  5. #define CARBON_TOOLCHAIN_PARSER_PARSE_NODE_KIND_H_
  6. #include <cstdint>
  7. #include <iterator>
  8. #include "common/ostream.h"
  9. #include "llvm/ADT/StringRef.h"
  10. namespace Carbon {
  11. // A class wrapping an enumeration of the different kinds of nodes in the parse
  12. // tree.
  13. //
  14. // Rather than using a raw enumerator for each distinct kind of node produced by
  15. // the parser, we wrap the enumerator in a class to expose a more rich API
  16. // including bidirectional mappings to string spellings of the different kinds
  17. // and any relevant classification.
  18. //
  19. // Instances of this type should always be created using the `constexpr` static
  20. // member functions. These instances are designed specifically to be usable in
  21. // `case` labels of `switch` statements just like an enumerator would.
  22. class ParseNodeKind {
  23. // Note that this must be declared earlier in the class so that its type can
  24. // be used, for example in the conversion operator.
  25. enum class KindEnum : uint8_t {
  26. #define CARBON_PARSE_NODE_KIND(Name) Name,
  27. #include "toolchain/parser/parse_node_kind.def"
  28. };
  29. public:
  30. // `clang-format` has a bug with spacing around `->` returns in macros. See
  31. // https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
  32. #define CARBON_PARSE_NODE_KIND(Name) \
  33. static constexpr auto Name()->ParseNodeKind { \
  34. return ParseNodeKind(KindEnum::Name); \
  35. }
  36. #include "toolchain/parser/parse_node_kind.def"
  37. // The default constructor is deleted because objects of this type should
  38. // always be constructed using the above factory functions for each unique
  39. // kind.
  40. ParseNodeKind() = delete;
  41. friend auto operator==(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
  42. return lhs.kind_ == rhs.kind_;
  43. }
  44. friend auto operator!=(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
  45. return lhs.kind_ != rhs.kind_;
  46. }
  47. // Gets a friendly name for the token for logging or debugging.
  48. [[nodiscard]] auto name() const -> llvm::StringRef;
  49. // Enable conversion to our private enum, including in a `constexpr` context,
  50. // to enable usage in `switch` and `case`. The enum remains private and
  51. // nothing else should be using this function.
  52. // NOLINTNEXTLINE(google-explicit-constructor)
  53. constexpr operator KindEnum() const { return kind_; }
  54. void Print(llvm::raw_ostream& out) const { out << name(); }
  55. // Returns true if the node is bracketed; otherwise, child_count is used.
  56. auto has_bracket() const -> bool;
  57. // Returns the bracketing node kind for the current node kind. Requires that
  58. // has_bracket is true.
  59. auto bracket() const -> ParseNodeKind;
  60. // Returns the number of children that the node must have, often 0. Requires
  61. // that has_bracket is false.
  62. auto child_count() const -> int32_t;
  63. private:
  64. constexpr explicit ParseNodeKind(KindEnum k) : kind_(k) {}
  65. KindEnum kind_;
  66. };
  67. // We expect the parse node kind to fit compactly into 8 bits.
  68. static_assert(sizeof(ParseNodeKind) == 1, "Kind objects include padding!");
  69. } // namespace Carbon
  70. #endif // CARBON_TOOLCHAIN_PARSER_PARSE_NODE_KIND_H_