node_category.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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. #include "toolchain/parse/node_category.h"
  5. #include "llvm/ADT/StringExtras.h"
  6. namespace Carbon::Parse {
  7. // Returns a string form of the node category for printing.
  8. static auto NodeCategoryToString(NodeCategory::RawEnumType category)
  9. -> llvm::StringLiteral {
  10. #define CARBON_NODE_CATEGORY_TO_STRING(Name) \
  11. case NodeCategory::Name: \
  12. return #Name;
  13. switch (category) {
  14. CARBON_NODE_CATEGORY(CARBON_NODE_CATEGORY_TO_STRING)
  15. CARBON_NODE_CATEGORY_TO_STRING(None)
  16. }
  17. #undef CARBON_NODE_CATEGORY_TO_STRING
  18. }
  19. auto NodeCategory::Print(llvm::raw_ostream& out) const -> void {
  20. llvm::ListSeparator sep("|");
  21. auto value = value_;
  22. do {
  23. // The lowest set bit in the value, or 0 (`None`) if no bits are set.
  24. auto lowest_bit = static_cast<RawEnumType>(value & -value);
  25. out << sep << NodeCategoryToString(lowest_bit);
  26. value &= ~lowest_bit;
  27. } while (value);
  28. }
  29. } // namespace Carbon::Parse