parse_node_kind.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Note that this must be declared earlier in the class so that its type can
  23. // be used, for example in the conversion operator.
  24. enum class KindEnum : uint8_t {
  25. #define CARBON_PARSE_NODE_KIND(Name) Name,
  26. #include "parser/parse_node_kind.def"
  27. };
  28. public:
  29. // The formatting for this macro is weird due to a `clang-format` bug. See
  30. // https://bugs.llvm.org/show_bug.cgi?id=48320 for details.
  31. #define CARBON_PARSE_NODE_KIND(Name) \
  32. static constexpr auto Name()->ParseNodeKind { \
  33. return ParseNodeKind(KindEnum::Name); \
  34. }
  35. #include "parser/parse_node_kind.def"
  36. // The default constructor is deleted as objects of this type should always be
  37. // constructed using the above factory functions for each unique kind.
  38. ParseNodeKind() = delete;
  39. friend auto operator==(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
  40. return lhs.kind == rhs.kind;
  41. }
  42. friend auto operator!=(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
  43. return lhs.kind != rhs.kind;
  44. }
  45. // Gets a friendly name for the token for logging or debugging.
  46. [[nodiscard]] auto GetName() const -> llvm::StringRef;
  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. private:
  53. constexpr explicit ParseNodeKind(KindEnum k) : kind(k) {}
  54. KindEnum kind;
  55. };
  56. // We expect the parse node kind to fit compactly into 8 bits.
  57. static_assert(sizeof(ParseNodeKind) == 1, "Kind objects include padding!");
  58. } // namespace Carbon
  59. #endif // PARSER_PARSE_NODE_KIND_H_