parse_node_kind.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // The formatting for this macro is weird due to a `clang-format` bug. 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 as objects of this type should always be
  38. // constructed using the above factory functions for each unique kind.
  39. ParseNodeKind() = delete;
  40. friend auto operator==(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
  41. return lhs.kind_ == rhs.kind_;
  42. }
  43. friend auto operator!=(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
  44. return lhs.kind_ != rhs.kind_;
  45. }
  46. // Gets a friendly name for the token for logging or debugging.
  47. [[nodiscard]] auto name() const -> llvm::StringRef;
  48. // Enable conversion to our private enum, including in a `constexpr` context,
  49. // to enable usage in `switch` and `case`. The enum remains private and
  50. // nothing else should be using this.
  51. // NOLINTNEXTLINE(google-explicit-constructor)
  52. constexpr operator KindEnum() const { return kind_; }
  53. void Print(llvm::raw_ostream& out) const { out << name(); }
  54. private:
  55. constexpr explicit ParseNodeKind(KindEnum k) : kind_(k) {}
  56. KindEnum kind_;
  57. };
  58. // We expect the parse node kind to fit compactly into 8 bits.
  59. static_assert(sizeof(ParseNodeKind) == 1, "Kind objects include padding!");
  60. } // namespace Carbon
  61. #endif // CARBON_TOOLCHAIN_PARSER_PARSE_NODE_KIND_H_