dump.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. }
  86. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  87. SemIR::InstBlockId inst_block_id) -> void {
  88. SemIR::Dump(context.sem_ir(), inst_block_id);
  89. }
  90. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::InstId inst_id)
  91. -> void {
  92. SemIR::Dump(context.sem_ir(), inst_id);
  93. }
  94. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  95. SemIR::InterfaceId interface_id) -> void {
  96. SemIR::Dump(context.sem_ir(), interface_id);
  97. }
  98. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::LocId loc_id)
  99. -> void {
  100. DumpNoNewline(context, loc_id);
  101. llvm::errs() << '\n';
  102. }
  103. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::NameId name_id)
  104. -> void {
  105. SemIR::Dump(context.sem_ir(), name_id);
  106. }
  107. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  108. SemIR::NameScopeId name_scope_id) -> void {
  109. SemIR::Dump(context.sem_ir(), name_scope_id);
  110. }
  111. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  112. SemIR::SpecificId specific_id) -> void {
  113. SemIR::Dump(context.sem_ir(), specific_id);
  114. }
  115. LLVM_DUMP_METHOD static auto Dump(
  116. const Context& context, SemIR::StructTypeFieldsId struct_type_fields_id)
  117. -> void {
  118. SemIR::Dump(context.sem_ir(), struct_type_fields_id);
  119. }
  120. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  121. SemIR::TypeBlockId type_block_id) -> void {
  122. SemIR::Dump(context.sem_ir(), type_block_id);
  123. }
  124. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::TypeId type_id)
  125. -> void {
  126. SemIR::Dump(context.sem_ir(), type_id);
  127. }
  128. } // namespace Carbon::Check
  129. #endif // NDEBUG