dump.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. #ifndef NDEBUG
  5. #include "toolchain/sem_ir/dump.h"
  6. #include "common/ostream.h"
  7. #include "toolchain/sem_ir/stringify_type.h"
  8. namespace Carbon::SemIR {
  9. static auto DumpNameIfValid(const File& file, NameId name_id) -> void {
  10. if (name_id.has_value()) {
  11. llvm::errs() << " `" << file.names().GetFormatted(name_id) << "`";
  12. }
  13. }
  14. static auto DumpNoNewline(const File& file, ConstantId const_id) -> void {
  15. llvm::errs() << const_id;
  16. if (const_id.is_symbolic()) {
  17. llvm::errs() << ": "
  18. << file.constant_values().GetSymbolicConstant(const_id);
  19. } else if (const_id.is_template()) {
  20. llvm::errs() << ": "
  21. << file.insts().Get(
  22. file.constant_values().GetInstId(const_id));
  23. }
  24. }
  25. static auto DumpNoNewline(const File& file, InstId inst_id) -> void {
  26. llvm::errs() << inst_id;
  27. if (inst_id.has_value()) {
  28. llvm::errs() << ": " << file.insts().Get(inst_id);
  29. }
  30. }
  31. static auto DumpNoNewline(const File& file, InterfaceId interface_id) -> void {
  32. llvm::errs() << interface_id;
  33. if (interface_id.has_value()) {
  34. auto interface = file.interfaces().Get(interface_id);
  35. llvm::errs() << ": " << interface;
  36. DumpNameIfValid(file, interface.name_id);
  37. }
  38. }
  39. static auto DumpNoNewline(const File& file, SpecificId specific_id) -> void {
  40. llvm::errs() << specific_id;
  41. if (specific_id.has_value()) {
  42. llvm::errs() << ": " << file.specifics().Get(specific_id);
  43. }
  44. }
  45. LLVM_DUMP_METHOD auto Dump(const File& file, ClassId class_id) -> void {
  46. llvm::errs() << class_id;
  47. if (class_id.has_value()) {
  48. auto class_obj = file.classes().Get(class_id);
  49. llvm::errs() << ": " << class_obj;
  50. DumpNameIfValid(file, class_obj.name_id);
  51. }
  52. llvm::errs() << '\n';
  53. }
  54. LLVM_DUMP_METHOD auto Dump(const File& file, ConstantId const_id) -> void {
  55. DumpNoNewline(file, const_id);
  56. llvm::errs() << '\n';
  57. }
  58. LLVM_DUMP_METHOD auto Dump(const File& file, EntityNameId entity_name_id)
  59. -> void {
  60. llvm::errs() << entity_name_id;
  61. if (entity_name_id.has_value()) {
  62. auto entity_name = file.entity_names().Get(entity_name_id);
  63. llvm::errs() << ": " << entity_name;
  64. DumpNameIfValid(file, entity_name.name_id);
  65. }
  66. llvm::errs() << '\n';
  67. }
  68. LLVM_DUMP_METHOD auto Dump(const File& file, FacetTypeId facet_type_id)
  69. -> void {
  70. llvm::errs() << facet_type_id;
  71. if (facet_type_id.has_value()) {
  72. auto facet_type = file.facet_types().Get(facet_type_id);
  73. llvm::errs() << ": " << facet_type;
  74. for (auto impls : facet_type.impls_constraints) {
  75. llvm::errs() << "\n - ";
  76. DumpNoNewline(file, impls.interface_id);
  77. if (impls.specific_id.has_value()) {
  78. llvm::errs() << "; ";
  79. DumpNoNewline(file, impls.specific_id);
  80. }
  81. }
  82. for (auto rewrite : facet_type.rewrite_constraints) {
  83. llvm::errs() << "\n - ";
  84. DumpNoNewline(file, rewrite.lhs_const_id);
  85. llvm::errs() << "\n - ";
  86. DumpNoNewline(file, rewrite.rhs_const_id);
  87. }
  88. }
  89. llvm::errs() << '\n';
  90. }
  91. LLVM_DUMP_METHOD auto Dump(const File& file, FunctionId function_id) -> void {
  92. llvm::errs() << function_id;
  93. if (function_id.has_value()) {
  94. auto function = file.functions().Get(function_id);
  95. llvm::errs() << ": " << function;
  96. DumpNameIfValid(file, function.name_id);
  97. }
  98. llvm::errs() << '\n';
  99. }
  100. LLVM_DUMP_METHOD auto Dump(const File& file, GenericId generic_id) -> void {
  101. llvm::errs() << generic_id;
  102. if (generic_id.has_value()) {
  103. llvm::errs() << ": " << file.generics().Get(generic_id);
  104. }
  105. llvm::errs() << '\n';
  106. }
  107. LLVM_DUMP_METHOD auto Dump(const File& file, ImplId impl_id) -> void {
  108. llvm::errs() << impl_id;
  109. if (impl_id.has_value()) {
  110. llvm::errs() << ": " << file.impls().Get(impl_id);
  111. }
  112. llvm::errs() << '\n';
  113. }
  114. LLVM_DUMP_METHOD auto Dump(const File& file, InstBlockId inst_block_id)
  115. -> void {
  116. llvm::errs() << inst_block_id;
  117. if (inst_block_id.has_value()) {
  118. llvm::errs() << ":";
  119. auto inst_block = file.inst_blocks().Get(inst_block_id);
  120. for (auto inst_id : inst_block) {
  121. llvm::errs() << "\n - ";
  122. DumpNoNewline(file, inst_id);
  123. }
  124. }
  125. llvm::errs() << '\n';
  126. }
  127. LLVM_DUMP_METHOD auto Dump(const File& file, InstId inst_id) -> void {
  128. DumpNoNewline(file, inst_id);
  129. llvm::errs() << '\n';
  130. if (inst_id.has_value()) {
  131. Inst inst = file.insts().Get(inst_id);
  132. if (inst.type_id().has_value()) {
  133. llvm::errs() << " - type ";
  134. Dump(file, inst.type_id());
  135. }
  136. ConstantId const_id = file.constant_values().Get(inst_id);
  137. if (const_id.has_value()) {
  138. InstId const_inst_id = file.constant_values().GetInstId(const_id);
  139. llvm::errs() << " - value ";
  140. if (const_inst_id == inst_id) {
  141. llvm::errs() << const_id << '\n';
  142. } else {
  143. Dump(file, const_id);
  144. }
  145. }
  146. }
  147. }
  148. LLVM_DUMP_METHOD auto Dump(const File& file, InterfaceId interface_id) -> void {
  149. DumpNoNewline(file, interface_id);
  150. llvm::errs() << '\n';
  151. }
  152. LLVM_DUMP_METHOD auto Dump(const File& file, NameId name_id) -> void {
  153. llvm::errs() << name_id;
  154. DumpNameIfValid(file, name_id);
  155. llvm::errs() << '\n';
  156. }
  157. LLVM_DUMP_METHOD auto Dump(const File& file, NameScopeId name_scope_id)
  158. -> void {
  159. llvm::errs() << name_scope_id;
  160. if (name_scope_id.has_value()) {
  161. auto name_scope = file.name_scopes().Get(name_scope_id);
  162. llvm::errs() << ": " << name_scope;
  163. if (name_scope.inst_id().has_value()) {
  164. llvm::errs() << " " << file.insts().Get(name_scope.inst_id());
  165. }
  166. DumpNameIfValid(file, name_scope.name_id());
  167. }
  168. llvm::errs() << '\n';
  169. }
  170. LLVM_DUMP_METHOD auto Dump(const File& file, SpecificId specific_id) -> void {
  171. DumpNoNewline(file, specific_id);
  172. llvm::errs() << '\n';
  173. }
  174. LLVM_DUMP_METHOD auto Dump(const File& file,
  175. StructTypeFieldsId struct_type_fields_id) -> void {
  176. llvm::errs() << struct_type_fields_id;
  177. if (struct_type_fields_id.has_value()) {
  178. llvm::errs() << ":";
  179. auto block = file.struct_type_fields().Get(struct_type_fields_id);
  180. for (auto field : block) {
  181. llvm::errs() << "\n - " << field;
  182. DumpNameIfValid(file, field.name_id);
  183. if (field.type_id.has_value()) {
  184. InstId inst_id =
  185. file.constant_values().GetInstId(field.type_id.AsConstantId());
  186. llvm::errs() << ": " << StringifyTypeExpr(file, inst_id);
  187. }
  188. }
  189. }
  190. llvm::errs() << '\n';
  191. }
  192. LLVM_DUMP_METHOD auto Dump(const File& file, TypeBlockId type_block_id)
  193. -> void {
  194. llvm::errs() << type_block_id;
  195. if (type_block_id.has_value()) {
  196. llvm::errs() << ":\n";
  197. auto type_block = file.type_blocks().Get(type_block_id);
  198. for (auto type_id : type_block) {
  199. llvm::errs() << " - ";
  200. Dump(file, type_id);
  201. }
  202. } else {
  203. llvm::errs() << '\n';
  204. }
  205. }
  206. LLVM_DUMP_METHOD auto Dump(const File& file, TypeId type_id) -> void {
  207. llvm::errs() << type_id;
  208. if (type_id.has_value()) {
  209. InstId inst_id = file.constant_values().GetInstId(type_id.AsConstantId());
  210. llvm::errs() << ": " << StringifyTypeExpr(file, inst_id) << "; "
  211. << file.insts().Get(inst_id);
  212. }
  213. llvm::errs() << '\n';
  214. }
  215. // Functions that can be used instead of the corresponding constructor, which is
  216. // unavailable during debugging.
  217. LLVM_DUMP_METHOD static auto MakeClassId(int id) -> ClassId {
  218. return ClassId(id);
  219. }
  220. LLVM_DUMP_METHOD static auto MakeConstantId(int id) -> ConstantId {
  221. return ConstantId(id);
  222. }
  223. LLVM_DUMP_METHOD static auto MakeSymbolicConstantId(int id) -> ConstantId {
  224. return ConstantId::ForSymbolicConstantIndex(id);
  225. }
  226. LLVM_DUMP_METHOD static auto MakeEntityNameId(int id) -> EntityNameId {
  227. return EntityNameId(id);
  228. }
  229. LLVM_DUMP_METHOD static auto MakeFacetTypeId(int id) -> FacetTypeId {
  230. return FacetTypeId(id);
  231. }
  232. LLVM_DUMP_METHOD static auto MakeFunctionId(int id) -> FunctionId {
  233. return FunctionId(id);
  234. }
  235. LLVM_DUMP_METHOD static auto MakeGenericId(int id) -> GenericId {
  236. return GenericId(id);
  237. }
  238. LLVM_DUMP_METHOD static auto MakeImplId(int id) -> ImplId { return ImplId(id); }
  239. LLVM_DUMP_METHOD static auto MakeInstBlockId(int id) -> InstBlockId {
  240. return InstBlockId(id);
  241. }
  242. LLVM_DUMP_METHOD static auto MakeInstId(int id) -> InstId { return InstId(id); }
  243. LLVM_DUMP_METHOD static auto MakeInterfaceId(int id) -> InterfaceId {
  244. return InterfaceId(id);
  245. }
  246. LLVM_DUMP_METHOD static auto MakeNameId(int id) -> NameId { return NameId(id); }
  247. LLVM_DUMP_METHOD static auto MakeNameScopeId(int id) -> NameScopeId {
  248. return NameScopeId(id);
  249. }
  250. LLVM_DUMP_METHOD static auto MakeSpecificId(int id) -> SpecificId {
  251. return SpecificId(id);
  252. }
  253. LLVM_DUMP_METHOD static auto MakeStructTypeFieldsId(int id)
  254. -> StructTypeFieldsId {
  255. return StructTypeFieldsId(id);
  256. }
  257. LLVM_DUMP_METHOD static auto MakeTypeBlockId(int id) -> TypeBlockId {
  258. return TypeBlockId(id);
  259. }
  260. LLVM_DUMP_METHOD static auto MakeTypeId(int id) -> TypeId { return TypeId(id); }
  261. } // namespace Carbon::SemIR
  262. #endif // NDEBUG