dump.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #ifndef NDEBUG
  13. #include "toolchain/lex/dump.h"
  14. #include <string>
  15. #include "common/check.h"
  16. #include "common/raw_string_ostream.h"
  17. #include "toolchain/check/context.h"
  18. #include "toolchain/lex/tokenized_buffer.h"
  19. #include "toolchain/parse/dump.h"
  20. #include "toolchain/parse/tree.h"
  21. #include "toolchain/sem_ir/dump.h"
  22. #include "toolchain/sem_ir/file.h"
  23. namespace Carbon::Check {
  24. static auto Dump(const Context& context, SemIR::LocId loc_id) -> std::string;
  25. LLVM_DUMP_METHOD static auto Dump(const Context& context, Lex::TokenIndex token)
  26. -> std::string {
  27. return Parse::Dump(context.parse_tree(), token);
  28. }
  29. LLVM_DUMP_METHOD static auto Dump(const Context& context, Parse::NodeId node_id)
  30. -> std::string {
  31. return Parse::Dump(context.parse_tree(), node_id);
  32. }
  33. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  34. SemIR::ClassId class_id) -> std::string {
  35. return SemIR::Dump(context.sem_ir(), class_id);
  36. }
  37. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  38. SemIR::ConstantId const_id) -> std::string {
  39. return SemIR::Dump(context.sem_ir(), const_id);
  40. }
  41. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  42. SemIR::EntityNameId entity_name_id)
  43. -> std::string {
  44. return SemIR::Dump(context.sem_ir(), entity_name_id);
  45. }
  46. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  47. SemIR::FacetTypeId facet_type_id)
  48. -> std::string {
  49. return SemIR::Dump(context.sem_ir(), facet_type_id);
  50. }
  51. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  52. SemIR::FunctionId function_id)
  53. -> std::string {
  54. return SemIR::Dump(context.sem_ir(), function_id);
  55. }
  56. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  57. SemIR::GenericId generic_id) -> std::string {
  58. return SemIR::Dump(context.sem_ir(), generic_id);
  59. }
  60. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::ImplId impl_id)
  61. -> std::string {
  62. RawStringOstream out;
  63. out << SemIR::Dump(context.sem_ir(), impl_id);
  64. if (!impl_id.has_value()) {
  65. return out.TakeStr();
  66. }
  67. const auto& impl = context.sem_ir().impls().Get(impl_id);
  68. out << "\nwitness loc: " << Dump(context, SemIR::LocId(impl.witness_id));
  69. return out.TakeStr();
  70. }
  71. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  72. SemIR::InstBlockId inst_block_id)
  73. -> std::string {
  74. return SemIR::Dump(context.sem_ir(), inst_block_id);
  75. }
  76. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::InstId inst_id)
  77. -> std::string {
  78. RawStringOstream out;
  79. out << SemIR::Dump(context.sem_ir(), inst_id) << '\n'
  80. << " - " << Dump(context, SemIR::LocId(inst_id));
  81. return out.TakeStr();
  82. }
  83. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  84. SemIR::InterfaceId interface_id)
  85. -> std::string {
  86. return SemIR::Dump(context.sem_ir(), interface_id);
  87. }
  88. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::LocId loc_id)
  89. -> std::string {
  90. RawStringOstream out;
  91. // TODO: If the canonical location is None but the original is an InstId,
  92. // should we dump the InstId anyway even though it has no location? Is that
  93. // ever useful?
  94. loc_id = context.sem_ir().insts().GetCanonicalLocId(loc_id);
  95. switch (loc_id.kind()) {
  96. case SemIR::LocId::Kind::None: {
  97. out << "LocId(<none>)";
  98. break;
  99. }
  100. case SemIR::LocId::Kind::ImportIRInstId: {
  101. auto import_ir_id = context.sem_ir()
  102. .import_ir_insts()
  103. .Get(loc_id.import_ir_inst_id())
  104. .ir_id();
  105. const auto* import_file =
  106. context.sem_ir().import_irs().Get(import_ir_id).sem_ir;
  107. out << "LocId(import from \"" << FormatEscaped(import_file->filename())
  108. << "\")";
  109. break;
  110. }
  111. case SemIR::LocId::Kind::NodeId: {
  112. auto token = context.parse_tree().node_token(loc_id.node_id());
  113. auto line = context.tokens().GetLineNumber(token);
  114. auto col = context.tokens().GetColumnNumber(token);
  115. const char* implicit = loc_id.is_implicit() ? " implicit" : "";
  116. out << "LocId(" << FormatEscaped(context.sem_ir().filename()) << ":"
  117. << line << ":" << col << implicit << ")";
  118. break;
  119. }
  120. case SemIR::LocId::Kind::InstId:
  121. CARBON_FATAL("unexpected LocId kind");
  122. }
  123. return out.TakeStr();
  124. }
  125. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::NameId name_id)
  126. -> std::string {
  127. return SemIR::Dump(context.sem_ir(), name_id);
  128. }
  129. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  130. SemIR::NameScopeId name_scope_id)
  131. -> std::string {
  132. return SemIR::Dump(context.sem_ir(), name_scope_id);
  133. }
  134. LLVM_DUMP_METHOD static auto Dump(
  135. const Context& context,
  136. SemIR::IdentifiedFacetTypeId identified_facet_type_id) -> std::string {
  137. return SemIR::Dump(context.sem_ir(), identified_facet_type_id);
  138. }
  139. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  140. SemIR::SpecificId specific_id)
  141. -> std::string {
  142. return SemIR::Dump(context.sem_ir(), specific_id);
  143. }
  144. LLVM_DUMP_METHOD static auto Dump(
  145. const Context& context, SemIR::SpecificInterfaceId specific_interface_id)
  146. -> std::string {
  147. return SemIR::Dump(context.sem_ir(), specific_interface_id);
  148. }
  149. LLVM_DUMP_METHOD static auto Dump(
  150. const Context& context, SemIR::StructTypeFieldsId struct_type_fields_id)
  151. -> std::string {
  152. return SemIR::Dump(context.sem_ir(), struct_type_fields_id);
  153. }
  154. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::TypeId type_id)
  155. -> std::string {
  156. return SemIR::Dump(context.sem_ir(), type_id);
  157. }
  158. } // namespace Carbon::Check
  159. #endif // NDEBUG