parse_node_kind.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 PARSER_PARSE_NODE_KIND_H_
  5. #define PARSER_PARSE_NODE_KIND_H_
  6. #include <cstdint>
  7. #include <iterator>
  8. #include "llvm/ADT/StringRef.h"
  9. namespace Carbon {
  10. // A class wrapping an enumeration of the different kinds of nodes in the parse
  11. // tree.
  12. //
  13. // Rather than using a raw enumerator for each distinct kind of node produced by
  14. // the parser, we wrap the enumerator in a class to expose a more rich API
  15. // including bidirectional mappings to string spellings of the different kinds
  16. // and any relevant classification.
  17. //
  18. // Instances of this type should always be created using the `constexpr` static
  19. // member functions. These instances are designed specifically to be usable in
  20. // `case` labels of `switch` statements just like an enumerator would.
  21. class ParseNodeKind {
  22. public:
  23. // The formatting for this macro is weird due to a `clang-format` bug. See
  24. // https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
  25. #define CARBON_PARSE_NODE_KIND(Name) \
  26. static constexpr auto Name()->ParseNodeKind { \
  27. return ParseNodeKind(KindEnum::Name); \
  28. }
  29. #include "parser/parse_node_kind.def"
  30. // The default constructor is deleted as objects of this type should always be
  31. // constructed using the above factory functions for each unique kind.
  32. ParseNodeKind() = delete;
  33. auto operator==(const ParseNodeKind& rhs) const -> bool {
  34. return kind == rhs.kind;
  35. }
  36. auto operator!=(const ParseNodeKind& rhs) const -> bool {
  37. return kind != rhs.kind;
  38. }
  39. // Gets a friendly name for the token for logging or debugging.
  40. [[nodiscard]] auto GetName() const -> llvm::StringRef;
  41. private:
  42. enum class KindEnum : uint8_t {
  43. #define CARBON_PARSE_NODE_KIND(Name) Name,
  44. #include "parser/parse_node_kind.def"
  45. };
  46. constexpr explicit ParseNodeKind(KindEnum k) : kind(k) {}
  47. // Enable conversion to our private enum, including in a `constexpr` context,
  48. // to enable usage in `switch` and `case`. The enum remains private and
  49. // nothing else should be using this.
  50. // NOLINTNEXTLINE(google-explicit-constructor)
  51. constexpr operator KindEnum() const { return kind; }
  52. KindEnum kind;
  53. };
  54. // We expect the parse node kind to fit compactly into 8 bits.
  55. static_assert(sizeof(ParseNodeKind) == 1, "Kind objects include padding!");
  56. } // namespace Carbon
  57. #endif // PARSER_PARSE_NODE_KIND_H_