parse_node_kind.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. // Gets a friendly name for the token for logging or debugging.
  42. [[nodiscard]] auto name() const -> llvm::StringRef;
  43. // Enable conversion to our private enum, including in a `constexpr` context,
  44. // to enable usage in `switch` and `case`. The enum remains private and
  45. // nothing else should be using this function.
  46. // NOLINTNEXTLINE(google-explicit-constructor)
  47. constexpr operator KindEnum() const { return kind_; }
  48. void Print(llvm::raw_ostream& out) const { out << name(); }
  49. // Returns true if the node is bracketed; otherwise, child_count is used.
  50. auto has_bracket() const -> bool;
  51. // Returns the bracketing node kind for the current node kind. Requires that
  52. // has_bracket is true.
  53. auto bracket() const -> ParseNodeKind;
  54. // Returns the number of children that the node must have, often 0. Requires
  55. // that has_bracket is false.
  56. auto child_count() const -> int32_t;
  57. private:
  58. constexpr explicit ParseNodeKind(KindEnum k) : kind_(k) {}
  59. KindEnum kind_;
  60. };
  61. // We expect the parse node kind to fit compactly into 8 bits.
  62. static_assert(sizeof(ParseNodeKind) == 1, "Kind objects include padding!");
  63. } // namespace Carbon
  64. #endif // CARBON_TOOLCHAIN_PARSER_PARSE_NODE_KIND_H_