dump.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // This library contains functions to assist dumping objects to stderr during
  5. // interactive debugging. Functions named `Dump` are intended for direct use by
  6. // developers, and should use overload resolution to determine which will be
  7. // invoked. The debugger should do namespace resolution automatically. For
  8. // example:
  9. //
  10. // - lldb: `expr Dump(context, id)`
  11. // - gdb: `call Dump(context, id)`
  12. //
  13. // The `DumpNoNewline` functions are helpers that exclude a trailing newline.
  14. // They're intended to be composed by `Dump` function implementations.
  15. #ifndef NDEBUG
  16. #include "toolchain/lex/dump.h"
  17. #include "common/check.h"
  18. #include "common/ostream.h"
  19. #include "toolchain/check/context.h"
  20. #include "toolchain/lex/tokenized_buffer.h"
  21. #include "toolchain/parse/dump.h"
  22. #include "toolchain/parse/tree.h"
  23. #include "toolchain/sem_ir/dump.h"
  24. #include "toolchain/sem_ir/file.h"
  25. namespace Carbon::Check {
  26. static auto DumpNoNewline(const Context& context, SemIR::LocId loc_id) -> void {
  27. if (!loc_id.has_value()) {
  28. llvm::errs() << "LocId(<none>)";
  29. return;
  30. }
  31. if (loc_id.is_node_id()) {
  32. auto token = context.parse_tree().node_token(loc_id.node_id());
  33. auto line = context.tokens().GetLineNumber(token);
  34. auto col = context.tokens().GetColumnNumber(token);
  35. const char* implicit = loc_id.is_implicit() ? " implicit" : "";
  36. llvm::errs() << "LocId(" << FormatEscaped(context.sem_ir().filename())
  37. << ":" << line << ":" << col << implicit << ")";
  38. } else {
  39. CARBON_CHECK(loc_id.is_import_ir_inst_id());
  40. auto import_ir_id = context.sem_ir()
  41. .import_ir_insts()
  42. .Get(loc_id.import_ir_inst_id())
  43. .ir_id;
  44. const auto* import_file =
  45. context.sem_ir().import_irs().Get(import_ir_id).sem_ir;
  46. llvm::errs() << "LocId(import from \""
  47. << FormatEscaped(import_file->filename()) << "\")";
  48. }
  49. }
  50. LLVM_DUMP_METHOD static auto Dump(const Context& context, Lex::TokenIndex token)
  51. -> void {
  52. Parse::Dump(context.parse_tree(), token);
  53. }
  54. LLVM_DUMP_METHOD static auto Dump(const Context& context, Parse::NodeId node_id)
  55. -> void {
  56. Parse::Dump(context.parse_tree(), node_id);
  57. }
  58. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  59. SemIR::ClassId class_id) -> void {
  60. SemIR::Dump(context.sem_ir(), class_id);
  61. }
  62. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  63. SemIR::ConstantId const_id) -> void {
  64. SemIR::Dump(context.sem_ir(), const_id);
  65. }
  66. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  67. SemIR::EntityNameId entity_name_id) -> void {
  68. SemIR::Dump(context.sem_ir(), entity_name_id);
  69. }
  70. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  71. SemIR::FacetTypeId facet_type_id) -> void {
  72. SemIR::Dump(context.sem_ir(), facet_type_id);
  73. }
  74. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  75. SemIR::FunctionId function_id) -> void {
  76. SemIR::Dump(context.sem_ir(), function_id);
  77. }
  78. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  79. SemIR::GenericId generic_id) -> void {
  80. SemIR::Dump(context.sem_ir(), generic_id);
  81. }
  82. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::ImplId impl_id)
  83. -> void {
  84. SemIR::Dump(context.sem_ir(), impl_id);
  85. if (impl_id.has_value()) {
  86. const auto& impl = context.sem_ir().impls().Get(impl_id);
  87. auto loc_id = context.sem_ir().insts().GetLocId(impl.witness_id);
  88. llvm::errs() << "witness loc: ";
  89. DumpNoNewline(context, loc_id);
  90. llvm::errs() << '\n';
  91. }
  92. }
  93. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  94. SemIR::InstBlockId inst_block_id) -> void {
  95. SemIR::Dump(context.sem_ir(), inst_block_id);
  96. }
  97. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::InstId inst_id)
  98. -> void {
  99. SemIR::Dump(context.sem_ir(), inst_id);
  100. auto loc_id = context.sem_ir().insts().GetLocId(inst_id);
  101. llvm::errs() << " - ";
  102. DumpNoNewline(context, loc_id);
  103. llvm::errs() << '\n';
  104. }
  105. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  106. SemIR::InterfaceId interface_id) -> void {
  107. SemIR::Dump(context.sem_ir(), interface_id);
  108. }
  109. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::LocId loc_id)
  110. -> void {
  111. DumpNoNewline(context, loc_id);
  112. llvm::errs() << '\n';
  113. }
  114. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::NameId name_id)
  115. -> void {
  116. SemIR::Dump(context.sem_ir(), name_id);
  117. }
  118. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  119. SemIR::NameScopeId name_scope_id) -> void {
  120. SemIR::Dump(context.sem_ir(), name_scope_id);
  121. }
  122. LLVM_DUMP_METHOD static auto Dump(
  123. const Context& context, SemIR::CompleteFacetTypeId complete_facet_type_id)
  124. -> void {
  125. SemIR::Dump(context.sem_ir(), complete_facet_type_id);
  126. }
  127. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  128. SemIR::SpecificId specific_id) -> void {
  129. SemIR::Dump(context.sem_ir(), specific_id);
  130. }
  131. LLVM_DUMP_METHOD static auto Dump(
  132. const Context& context, SemIR::SpecificInterfaceId specific_interface_id)
  133. -> void {
  134. SemIR::Dump(context.sem_ir(), specific_interface_id);
  135. }
  136. LLVM_DUMP_METHOD static auto Dump(
  137. const Context& context, SemIR::StructTypeFieldsId struct_type_fields_id)
  138. -> void {
  139. SemIR::Dump(context.sem_ir(), struct_type_fields_id);
  140. }
  141. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  142. SemIR::TypeBlockId type_block_id) -> void {
  143. SemIR::Dump(context.sem_ir(), type_block_id);
  144. }
  145. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::TypeId type_id)
  146. -> void {
  147. SemIR::Dump(context.sem_ir(), type_id);
  148. }
  149. } // namespace Carbon::Check
  150. #endif // NDEBUG