dump.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.is_valid()) {
  28. llvm::errs() << "LocId(invalid)";
  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(";
  37. llvm::errs().write_escaped(context.sem_ir().filename());
  38. llvm::errs() << ":" << line << ":" << col << implicit << ")";
  39. } else {
  40. CARBON_CHECK(loc_id.is_import_ir_inst_id());
  41. auto import_ir_id = context.sem_ir()
  42. .import_ir_insts()
  43. .Get(loc_id.import_ir_inst_id())
  44. .ir_id;
  45. const auto* import_file =
  46. context.sem_ir().import_irs().Get(import_ir_id).sem_ir;
  47. llvm::errs() << "LocId(import from \"";
  48. llvm::errs().write_escaped(import_file->filename());
  49. llvm::errs() << "\")";
  50. }
  51. }
  52. LLVM_DUMP_METHOD static auto Dump(const Context& context, Lex::TokenIndex token)
  53. -> void {
  54. Parse::Dump(context.parse_tree(), token);
  55. }
  56. LLVM_DUMP_METHOD static auto Dump(const Context& context, Parse::NodeId node_id)
  57. -> void {
  58. Parse::Dump(context.parse_tree(), node_id);
  59. }
  60. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  61. SemIR::ClassId class_id) -> void {
  62. SemIR::Dump(context.sem_ir(), class_id);
  63. }
  64. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  65. SemIR::ConstantId const_id) -> void {
  66. SemIR::Dump(context.sem_ir(), const_id);
  67. }
  68. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  69. SemIR::EntityNameId entity_name_id) -> void {
  70. SemIR::Dump(context.sem_ir(), entity_name_id);
  71. }
  72. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  73. SemIR::FacetTypeId facet_type_id) -> void {
  74. SemIR::Dump(context.sem_ir(), facet_type_id);
  75. }
  76. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  77. SemIR::FunctionId function_id) -> void {
  78. SemIR::Dump(context.sem_ir(), function_id);
  79. }
  80. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  81. SemIR::GenericId generic_id) -> void {
  82. SemIR::Dump(context.sem_ir(), generic_id);
  83. }
  84. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::ImplId impl_id)
  85. -> void {
  86. SemIR::Dump(context.sem_ir(), impl_id);
  87. }
  88. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  89. SemIR::InstBlockId inst_block_id) -> void {
  90. SemIR::Dump(context.sem_ir(), inst_block_id);
  91. }
  92. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::InstId inst_id)
  93. -> void {
  94. SemIR::Dump(context.sem_ir(), inst_id);
  95. }
  96. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  97. SemIR::InterfaceId interface_id) -> void {
  98. SemIR::Dump(context.sem_ir(), interface_id);
  99. }
  100. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::LocId loc_id)
  101. -> void {
  102. DumpNoNewline(context, loc_id);
  103. llvm::errs() << '\n';
  104. }
  105. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::NameId name_id)
  106. -> void {
  107. SemIR::Dump(context.sem_ir(), name_id);
  108. }
  109. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  110. SemIR::NameScopeId name_scope_id) -> void {
  111. SemIR::Dump(context.sem_ir(), name_scope_id);
  112. }
  113. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  114. SemIR::SpecificId specific_id) -> void {
  115. SemIR::Dump(context.sem_ir(), specific_id);
  116. }
  117. LLVM_DUMP_METHOD static auto Dump(
  118. const Context& context, SemIR::StructTypeFieldsId struct_type_fields_id)
  119. -> void {
  120. SemIR::Dump(context.sem_ir(), struct_type_fields_id);
  121. }
  122. LLVM_DUMP_METHOD static auto Dump(const Context& context,
  123. SemIR::TypeBlockId type_block_id) -> void {
  124. SemIR::Dump(context.sem_ir(), type_block_id);
  125. }
  126. LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::TypeId type_id)
  127. -> void {
  128. SemIR::Dump(context.sem_ir(), type_id);
  129. }
  130. } // namespace Carbon::Check
  131. #endif // NDEBUG