node_stack.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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/check/node_stack.h"
  5. #include "llvm/ADT/STLExtras.h"
  6. namespace Carbon::Check {
  7. auto NodeStack::PrintForStackDump(SemIR::Formatter& formatter, int indent,
  8. llvm::raw_ostream& output) const -> void {
  9. auto print_id = [&]<Id::Kind Kind>(Id id) {
  10. if constexpr (Kind == Id::Kind::None) {
  11. output << "no value\n";
  12. } else if constexpr (Kind == Id::Kind::Invalid) {
  13. CARBON_FATAL("Should not be in node stack");
  14. } else if constexpr (Kind == Id::KindFor<SemIR::InstId>()) {
  15. output << "\n";
  16. formatter.PrintInst(id.As<Id::KindFor<SemIR::InstId>()>(), indent + 4,
  17. output);
  18. } else {
  19. output << id.As<Kind>() << "\n";
  20. }
  21. };
  22. output.indent(indent);
  23. output << "NodeStack:\n";
  24. for (auto [i, entry] : llvm::enumerate(stack_)) {
  25. auto node_kind = parse_tree_->node_kind(entry.node_id);
  26. output.indent(indent + 2);
  27. output << i << ". " << node_kind << ": ";
  28. switch (node_kind) {
  29. #define CARBON_PARSE_NODE_KIND(Kind) \
  30. case Parse::NodeKind::Kind: \
  31. print_id.operator()<NodeKindToIdKind(Parse::NodeKind::Kind)>(entry.id); \
  32. break;
  33. #include "toolchain/parse/node_kind.def"
  34. }
  35. }
  36. }
  37. } // namespace Carbon::Check