formatter.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. #include "toolchain/sem_ir/formatter.h"
  5. #include "common/ostream.h"
  6. #include "llvm/ADT/Sequence.h"
  7. #include "llvm/ADT/StringExtras.h"
  8. #include "llvm/Support/SaveAndRestore.h"
  9. #include "toolchain/base/kind_switch.h"
  10. #include "toolchain/base/value_store.h"
  11. #include "toolchain/lex/tokenized_buffer.h"
  12. #include "toolchain/parse/tree.h"
  13. #include "toolchain/sem_ir/builtin_function_kind.h"
  14. #include "toolchain/sem_ir/function.h"
  15. #include "toolchain/sem_ir/ids.h"
  16. #include "toolchain/sem_ir/inst_namer.h"
  17. #include "toolchain/sem_ir/name_scope.h"
  18. #include "toolchain/sem_ir/typed_insts.h"
  19. namespace Carbon::SemIR {
  20. // Formatter for printing textual Semantics IR.
  21. class FormatterImpl {
  22. public:
  23. explicit FormatterImpl(const File& sem_ir, InstNamer* inst_namer,
  24. llvm::raw_ostream& out, int indent)
  25. : sem_ir_(sem_ir), inst_namer_(inst_namer), out_(out), indent_(indent) {}
  26. // Prints the SemIR.
  27. //
  28. // Constants are printed first and may be referenced by later sections,
  29. // including file-scoped instructions. The file scope may contain entity
  30. // declarations which are defined later, such as classes.
  31. auto Format() -> void {
  32. out_ << "--- " << sem_ir_.filename() << "\n\n";
  33. FormatConstants();
  34. FormatImportRefs();
  35. out_ << inst_namer_->GetScopeName(InstNamer::ScopeId::File) << " ";
  36. OpenBrace();
  37. // TODO: Handle the case where there are multiple top-level instruction
  38. // blocks. For example, there may be branching in the initializer of a
  39. // global or a type expression.
  40. if (auto block_id = sem_ir_.top_inst_block_id(); block_id.is_valid()) {
  41. llvm::SaveAndRestore file_scope(scope_, InstNamer::ScopeId::File);
  42. FormatCodeBlock(block_id);
  43. }
  44. CloseBrace();
  45. out_ << '\n';
  46. for (int i : llvm::seq(sem_ir_.interfaces().size())) {
  47. FormatInterface(InterfaceId(i));
  48. }
  49. for (int i : llvm::seq(sem_ir_.impls().size())) {
  50. FormatImpl(ImplId(i));
  51. }
  52. for (int i : llvm::seq(sem_ir_.classes().size())) {
  53. FormatClass(ClassId(i));
  54. }
  55. for (int i : llvm::seq(sem_ir_.functions().size())) {
  56. FormatFunction(FunctionId(i));
  57. }
  58. for (int i : llvm::seq(sem_ir_.specifics().size())) {
  59. FormatSpecific(SpecificId(i));
  60. }
  61. // End-of-file newline.
  62. out_ << "\n";
  63. }
  64. // Prints a code block.
  65. auto FormatPartialTrailingCodeBlock(llvm::ArrayRef<SemIR::InstId> block)
  66. -> void {
  67. out_ << ' ';
  68. OpenBrace();
  69. constexpr int NumPrintedOnSkip = 9;
  70. // Avoid only skipping one item.
  71. if (block.size() > NumPrintedOnSkip + 1) {
  72. Indent();
  73. out_ << "... skipping " << (block.size() - NumPrintedOnSkip)
  74. << " insts ...\n";
  75. block = block.take_back(NumPrintedOnSkip);
  76. }
  77. FormatCodeBlock(block);
  78. CloseBrace();
  79. }
  80. // Prints a single instruction.
  81. auto FormatInst(InstId inst_id) -> void {
  82. if (!inst_id.is_valid()) {
  83. Indent();
  84. out_ << "invalid\n";
  85. return;
  86. }
  87. FormatInst(inst_id, sem_ir_.insts().Get(inst_id));
  88. }
  89. private:
  90. enum class AddSpace : bool { Before, After };
  91. // Begins a braced block. Writes an open brace, and prepares to insert a
  92. // newline after it if the braced block is non-empty.
  93. auto OpenBrace() -> void {
  94. // Put the constant value of an instruction before any braced block, rather
  95. // than at the end.
  96. FormatPendingConstantValue(AddSpace::After);
  97. out_ << '{';
  98. indent_ += 2;
  99. after_open_brace_ = true;
  100. }
  101. // Ends a braced block by writing a close brace.
  102. auto CloseBrace() -> void {
  103. indent_ -= 2;
  104. if (!after_open_brace_) {
  105. Indent();
  106. }
  107. out_ << '}';
  108. after_open_brace_ = false;
  109. }
  110. // Adds beginning-of-line indentation. If we're at the start of a braced
  111. // block, first starts a new line.
  112. auto Indent(int offset = 0) -> void {
  113. if (after_open_brace_) {
  114. out_ << '\n';
  115. after_open_brace_ = false;
  116. }
  117. out_.indent(indent_ + offset);
  118. }
  119. // Adds beginning-of-label indentation. This is one level less than normal
  120. // indentation. Labels also get a preceding blank line unless they're at the
  121. // start of a block.
  122. auto IndentLabel() -> void {
  123. CARBON_CHECK(indent_ >= 2);
  124. if (!after_open_brace_) {
  125. out_ << '\n';
  126. }
  127. Indent(-2);
  128. }
  129. auto FormatConstants() -> void {
  130. if (!sem_ir_.constants().size()) {
  131. return;
  132. }
  133. llvm::SaveAndRestore constants_scope(scope_, InstNamer::ScopeId::Constants);
  134. out_ << inst_namer_->GetScopeName(InstNamer::ScopeId::Constants) << " ";
  135. OpenBrace();
  136. FormatCodeBlock(sem_ir_.constants().array_ref());
  137. CloseBrace();
  138. out_ << "\n\n";
  139. }
  140. auto FormatImportRefs() -> void {
  141. auto import_refs = sem_ir_.inst_blocks().Get(InstBlockId::ImportRefs);
  142. if (import_refs.empty()) {
  143. return;
  144. }
  145. llvm::SaveAndRestore scope(scope_, InstNamer::ScopeId::ImportRefs);
  146. out_ << inst_namer_->GetScopeName(InstNamer::ScopeId::ImportRefs) << " ";
  147. OpenBrace();
  148. FormatCodeBlock(import_refs);
  149. CloseBrace();
  150. out_ << "\n\n";
  151. }
  152. template <typename IdT>
  153. auto FormatEntityStart(llvm::StringRef entity_kind, GenericId generic_id,
  154. IdT entity_id) -> void {
  155. if (generic_id.is_valid()) {
  156. FormatGenericStart(entity_kind, generic_id);
  157. out_ << "\n";
  158. Indent();
  159. out_ << entity_kind;
  160. } else {
  161. out_ << "\n";
  162. Indent();
  163. out_ << entity_kind << " ";
  164. FormatName(entity_id);
  165. }
  166. }
  167. auto FormatEntityEnd(GenericId generic_id) -> void {
  168. if (generic_id.is_valid()) {
  169. FormatGenericEnd();
  170. }
  171. }
  172. auto FormatClass(ClassId id) -> void {
  173. const Class& class_info = sem_ir_.classes().Get(id);
  174. FormatEntityStart("class", class_info.generic_id, id);
  175. llvm::SaveAndRestore class_scope(scope_, inst_namer_->GetScopeFor(id));
  176. if (class_info.scope_id.is_valid()) {
  177. out_ << ' ';
  178. OpenBrace();
  179. FormatCodeBlock(class_info.body_block_id);
  180. FormatNameScope(class_info.scope_id, "!members:\n");
  181. CloseBrace();
  182. out_ << '\n';
  183. } else {
  184. out_ << ";\n";
  185. }
  186. FormatEntityEnd(class_info.generic_id);
  187. }
  188. auto FormatInterface(InterfaceId id) -> void {
  189. const Interface& interface_info = sem_ir_.interfaces().Get(id);
  190. FormatEntityStart("interface", interface_info.generic_id, id);
  191. llvm::SaveAndRestore interface_scope(scope_, inst_namer_->GetScopeFor(id));
  192. if (interface_info.scope_id.is_valid()) {
  193. out_ << ' ';
  194. OpenBrace();
  195. FormatCodeBlock(interface_info.body_block_id);
  196. // Always include the !members label because we always list the witness in
  197. // this section.
  198. IndentLabel();
  199. out_ << "!members:\n";
  200. FormatNameScope(interface_info.scope_id);
  201. Indent();
  202. out_ << "witness = ";
  203. FormatArg(interface_info.associated_entities_id);
  204. out_ << "\n";
  205. CloseBrace();
  206. out_ << '\n';
  207. } else {
  208. out_ << ";\n";
  209. }
  210. FormatEntityEnd(interface_info.generic_id);
  211. }
  212. auto FormatImpl(ImplId id) -> void {
  213. const Impl& impl_info = sem_ir_.impls().Get(id);
  214. FormatEntityStart("impl", SemIR::GenericId::Invalid, id);
  215. out_ << ": ";
  216. // TODO: Include the deduced parameter list if present.
  217. FormatType(impl_info.self_id);
  218. out_ << " as ";
  219. FormatType(impl_info.constraint_id);
  220. llvm::SaveAndRestore impl_scope(scope_, inst_namer_->GetScopeFor(id));
  221. if (impl_info.scope_id.is_valid()) {
  222. out_ << ' ';
  223. OpenBrace();
  224. FormatCodeBlock(impl_info.body_block_id);
  225. // Print the !members label even if the name scope is empty because we
  226. // always list the witness in this section.
  227. IndentLabel();
  228. out_ << "!members:\n";
  229. FormatNameScope(impl_info.scope_id);
  230. Indent();
  231. out_ << "witness = ";
  232. FormatArg(impl_info.witness_id);
  233. out_ << "\n";
  234. CloseBrace();
  235. out_ << '\n';
  236. } else {
  237. out_ << ";\n";
  238. }
  239. }
  240. auto FormatFunction(FunctionId id) -> void {
  241. const Function& fn = sem_ir_.functions().Get(id);
  242. FormatEntityStart(fn.is_extern ? "extern fn" : "fn", fn.generic_id, id);
  243. llvm::SaveAndRestore function_scope(scope_, inst_namer_->GetScopeFor(id));
  244. if (fn.implicit_param_refs_id.is_valid()) {
  245. out_ << "[";
  246. FormatParamList(fn.implicit_param_refs_id);
  247. out_ << "]";
  248. }
  249. if (fn.param_refs_id.is_valid()) {
  250. out_ << "(";
  251. FormatParamList(fn.param_refs_id);
  252. out_ << ")";
  253. }
  254. if (fn.return_storage_id.is_valid()) {
  255. out_ << " -> ";
  256. auto return_info = ReturnTypeInfo::ForFunction(sem_ir_, fn);
  257. if (!fn.body_block_ids.empty() && return_info.is_valid() &&
  258. return_info.has_return_slot()) {
  259. FormatName(fn.return_storage_id);
  260. out_ << ": ";
  261. }
  262. FormatType(sem_ir_.insts().Get(fn.return_storage_id).type_id());
  263. }
  264. if (fn.builtin_function_kind != BuiltinFunctionKind::None) {
  265. out_ << " = \"";
  266. out_.write_escaped(fn.builtin_function_kind.name(),
  267. /*UseHexEscapes=*/true);
  268. out_ << "\"";
  269. }
  270. if (!fn.body_block_ids.empty()) {
  271. out_ << ' ';
  272. OpenBrace();
  273. for (auto block_id : fn.body_block_ids) {
  274. IndentLabel();
  275. FormatLabel(block_id);
  276. out_ << ":\n";
  277. FormatCodeBlock(block_id);
  278. }
  279. CloseBrace();
  280. out_ << '\n';
  281. } else {
  282. out_ << ";\n";
  283. }
  284. FormatEntityEnd(fn.generic_id);
  285. }
  286. auto FormatGenericStart(llvm::StringRef entity_kind, GenericId generic_id)
  287. -> void {
  288. const auto& generic = sem_ir_.generics().Get(generic_id);
  289. out_ << "\n";
  290. Indent();
  291. out_ << "generic " << entity_kind << " ";
  292. FormatName(generic_id);
  293. llvm::SaveAndRestore generic_scope(scope_,
  294. inst_namer_->GetScopeFor(generic_id));
  295. out_ << "(";
  296. FormatParamList(generic.bindings_id);
  297. out_ << ") ";
  298. OpenBrace();
  299. FormatCodeBlock(generic.decl_block_id);
  300. if (generic.definition_block_id.is_valid()) {
  301. IndentLabel();
  302. out_ << "!definition:\n";
  303. FormatCodeBlock(generic.definition_block_id);
  304. }
  305. }
  306. auto FormatGenericEnd() -> void {
  307. CloseBrace();
  308. out_ << '\n';
  309. }
  310. auto FormatSpecificRegion(const Generic& generic, const Specific& specific,
  311. GenericInstIndex::Region region,
  312. llvm::StringRef region_name) -> void {
  313. if (!specific.GetValueBlock(region).is_valid()) {
  314. return;
  315. }
  316. if (!region_name.empty()) {
  317. IndentLabel();
  318. out_ << "!" << region_name << ":\n";
  319. }
  320. for (auto [generic_inst_id, specific_inst_id] : llvm::zip_longest(
  321. sem_ir_.inst_blocks().Get(generic.GetEvalBlock(region)),
  322. sem_ir_.inst_blocks().Get(specific.GetValueBlock(region)))) {
  323. if (generic_inst_id && specific_inst_id &&
  324. sem_ir_.insts().Is<StructTypeField>(*generic_inst_id) &&
  325. sem_ir_.insts().Is<StructTypeField>(*specific_inst_id)) {
  326. // Skip printing struct type fields to match the way we print the
  327. // generic.
  328. continue;
  329. }
  330. Indent();
  331. if (generic_inst_id) {
  332. FormatName(*generic_inst_id);
  333. } else {
  334. out_ << "<missing>";
  335. }
  336. out_ << " => ";
  337. if (specific_inst_id) {
  338. FormatName(*specific_inst_id);
  339. } else {
  340. out_ << "<missing>";
  341. }
  342. out_ << "\n";
  343. }
  344. }
  345. auto FormatSpecific(SpecificId id) -> void {
  346. const auto& specific = sem_ir_.specifics().Get(id);
  347. out_ << "\n";
  348. out_ << "specific ";
  349. FormatName(id);
  350. // TODO: Remove once we stop forming generic specifics with no generic
  351. // during import.
  352. if (!specific.generic_id.is_valid()) {
  353. out_ << ";\n";
  354. return;
  355. }
  356. out_ << " ";
  357. const auto& generic = sem_ir_.generics().Get(specific.generic_id);
  358. llvm::SaveAndRestore generic_scope(
  359. scope_, inst_namer_->GetScopeFor(specific.generic_id));
  360. OpenBrace();
  361. FormatSpecificRegion(generic, specific,
  362. GenericInstIndex::Region::Declaration, "");
  363. FormatSpecificRegion(generic, specific,
  364. GenericInstIndex::Region::Definition, "definition");
  365. CloseBrace();
  366. out_ << "\n";
  367. }
  368. auto FormatParamList(InstBlockId param_refs_id) -> void {
  369. llvm::ListSeparator sep;
  370. for (InstId param_id : sem_ir_.inst_blocks().Get(param_refs_id)) {
  371. out_ << sep;
  372. if (!param_id.is_valid()) {
  373. out_ << "invalid";
  374. continue;
  375. }
  376. if (auto addr = sem_ir_.insts().TryGetAs<SemIR::AddrPattern>(param_id)) {
  377. out_ << "addr ";
  378. param_id = addr->inner_id;
  379. }
  380. FormatName(param_id);
  381. out_ << ": ";
  382. FormatType(sem_ir_.insts().Get(param_id).type_id());
  383. }
  384. }
  385. auto FormatCodeBlock(InstBlockId block_id) -> void {
  386. if (block_id.is_valid()) {
  387. FormatCodeBlock(sem_ir_.inst_blocks().Get(block_id));
  388. }
  389. }
  390. auto FormatCodeBlock(llvm::ArrayRef<InstId> block) -> void {
  391. for (const InstId inst_id : block) {
  392. FormatInst(inst_id);
  393. }
  394. }
  395. auto FormatTrailingBlock(InstBlockId block_id) -> void {
  396. out_ << ' ';
  397. OpenBrace();
  398. FormatCodeBlock(block_id);
  399. CloseBrace();
  400. }
  401. auto FormatNameScope(NameScopeId id, llvm::StringRef label = "") -> void {
  402. const auto& scope = sem_ir_.name_scopes().Get(id);
  403. if (scope.names.empty() && scope.extended_scopes.empty() &&
  404. scope.import_ir_scopes.empty() && !scope.has_error) {
  405. // Name scope is empty.
  406. return;
  407. }
  408. if (!label.empty()) {
  409. IndentLabel();
  410. out_ << label;
  411. }
  412. for (auto [name_id, inst_id, access_kind] : scope.names) {
  413. Indent();
  414. out_ << ".";
  415. FormatName(name_id);
  416. switch (access_kind) {
  417. case SemIR::AccessKind::Public:
  418. break;
  419. case SemIR::AccessKind::Protected:
  420. out_ << " [protected]";
  421. break;
  422. case SemIR::AccessKind::Private:
  423. out_ << " [private]";
  424. break;
  425. }
  426. out_ << " = ";
  427. FormatName(inst_id);
  428. out_ << "\n";
  429. }
  430. for (auto extended_scope_id : scope.extended_scopes) {
  431. // TODO: Print this scope in a better way.
  432. Indent();
  433. out_ << "extend " << extended_scope_id << "\n";
  434. }
  435. for (auto [import_ir_id, unused] : scope.import_ir_scopes) {
  436. Indent();
  437. out_ << "import ";
  438. FormatArg(import_ir_id);
  439. out_ << "\n";
  440. }
  441. if (scope.has_error) {
  442. Indent();
  443. out_ << "has_error\n";
  444. }
  445. }
  446. auto FormatInst(InstId inst_id, Inst inst) -> void {
  447. CARBON_KIND_SWITCH(inst) {
  448. #define CARBON_SEM_IR_INST_KIND(InstT) \
  449. case CARBON_KIND(InstT typed_inst): { \
  450. FormatInst(inst_id, typed_inst); \
  451. break; \
  452. }
  453. #include "toolchain/sem_ir/inst_kind.def"
  454. }
  455. }
  456. template <typename InstT>
  457. auto FormatInst(InstId inst_id, InstT inst) -> void {
  458. Indent();
  459. FormatInstLHS(inst_id, inst);
  460. out_ << InstT::Kind.ir_name();
  461. pending_constant_value_ = sem_ir_.constant_values().Get(inst_id);
  462. pending_constant_value_is_self_ =
  463. sem_ir_.constant_values().GetInstId(pending_constant_value_) == inst_id;
  464. FormatInstRHS(inst);
  465. FormatPendingConstantValue(AddSpace::Before);
  466. out_ << "\n";
  467. }
  468. // Don't print a constant for ImportRefUnloaded.
  469. auto FormatInst(InstId inst_id, ImportRefUnloaded inst) -> void {
  470. Indent();
  471. FormatInstLHS(inst_id, inst);
  472. out_ << ImportRefUnloaded::Kind.ir_name();
  473. FormatInstRHS(inst);
  474. out_ << "\n";
  475. }
  476. // If there is a pending constant value attached to the current instruction,
  477. // print it now and clear it out. The constant value gets printed before the
  478. // first braced block argument, or at the end of the instruction if there are
  479. // no such arguments.
  480. auto FormatPendingConstantValue(AddSpace space_where) -> void {
  481. if (pending_constant_value_ == ConstantId::NotConstant) {
  482. return;
  483. }
  484. if (space_where == AddSpace::Before) {
  485. out_ << ' ';
  486. }
  487. out_ << '[';
  488. if (pending_constant_value_.is_valid()) {
  489. out_ << (pending_constant_value_.is_symbolic() ? "symbolic" : "template");
  490. if (!pending_constant_value_is_self_) {
  491. out_ << " = ";
  492. FormatConstant(pending_constant_value_);
  493. }
  494. } else {
  495. out_ << pending_constant_value_;
  496. }
  497. out_ << ']';
  498. if (space_where == AddSpace::After) {
  499. out_ << ' ';
  500. }
  501. pending_constant_value_ = ConstantId::NotConstant;
  502. }
  503. auto FormatInstLHS(InstId inst_id, Inst inst) -> void {
  504. switch (inst.kind().value_kind()) {
  505. case InstValueKind::Typed:
  506. FormatName(inst_id);
  507. out_ << ": ";
  508. switch (GetExprCategory(sem_ir_, inst_id)) {
  509. case ExprCategory::NotExpr:
  510. case ExprCategory::Error:
  511. case ExprCategory::Value:
  512. case ExprCategory::Mixed:
  513. break;
  514. case ExprCategory::DurableRef:
  515. case ExprCategory::EphemeralRef:
  516. out_ << "ref ";
  517. break;
  518. case ExprCategory::Initializing:
  519. out_ << "init ";
  520. break;
  521. }
  522. FormatType(inst.type_id());
  523. out_ << " = ";
  524. break;
  525. case InstValueKind::None:
  526. break;
  527. }
  528. }
  529. // Format ImportDecl with its name.
  530. auto FormatInstLHS(InstId inst_id, ImportDecl /*inst*/) -> void {
  531. FormatName(inst_id);
  532. out_ << " = ";
  533. }
  534. // Print ImportRefUnloaded with type-like semantics even though it lacks a
  535. // type_id.
  536. auto FormatInstLHS(InstId inst_id, ImportRefUnloaded /*inst*/) -> void {
  537. FormatName(inst_id);
  538. out_ << " = ";
  539. }
  540. template <typename InstT>
  541. auto FormatInstRHS(InstT inst) -> void {
  542. // By default, an instruction has a comma-separated argument list.
  543. using Info = Internal::InstLikeTypeInfo<InstT>;
  544. if constexpr (Info::NumArgs == 2) {
  545. FormatArgs(Info::template Get<0>(inst), Info::template Get<1>(inst));
  546. } else if constexpr (Info::NumArgs == 1) {
  547. FormatArgs(Info::template Get<0>(inst));
  548. } else {
  549. FormatArgs();
  550. }
  551. }
  552. auto FormatInstRHS(BindSymbolicName inst) -> void {
  553. // A BindSymbolicName with no value is a purely symbolic binding, such as
  554. // the `Self` in an interface. Don't print out `invalid` for the value.
  555. if (inst.value_id.is_valid()) {
  556. FormatArgs(inst.entity_name_id, inst.value_id);
  557. } else {
  558. FormatArgs(inst.entity_name_id);
  559. }
  560. }
  561. auto FormatInstRHS(BlockArg inst) -> void {
  562. out_ << " ";
  563. FormatLabel(inst.block_id);
  564. }
  565. auto FormatInstRHS(Namespace inst) -> void {
  566. if (inst.import_id.is_valid()) {
  567. FormatArgs(inst.import_id, inst.name_scope_id);
  568. } else {
  569. FormatArgs(inst.name_scope_id);
  570. }
  571. }
  572. auto FormatInst(InstId /*inst_id*/, BranchIf inst) -> void {
  573. if (!in_terminator_sequence_) {
  574. Indent();
  575. }
  576. out_ << "if ";
  577. FormatName(inst.cond_id);
  578. out_ << " " << Branch::Kind.ir_name() << " ";
  579. FormatLabel(inst.target_id);
  580. out_ << " else ";
  581. in_terminator_sequence_ = true;
  582. }
  583. auto FormatInst(InstId /*inst_id*/, BranchWithArg inst) -> void {
  584. if (!in_terminator_sequence_) {
  585. Indent();
  586. }
  587. out_ << BranchWithArg::Kind.ir_name() << " ";
  588. FormatLabel(inst.target_id);
  589. out_ << "(";
  590. FormatName(inst.arg_id);
  591. out_ << ")\n";
  592. in_terminator_sequence_ = false;
  593. }
  594. auto FormatInst(InstId /*inst_id*/, Branch inst) -> void {
  595. if (!in_terminator_sequence_) {
  596. Indent();
  597. }
  598. out_ << Branch::Kind.ir_name() << " ";
  599. FormatLabel(inst.target_id);
  600. out_ << "\n";
  601. in_terminator_sequence_ = false;
  602. }
  603. auto FormatInstRHS(Call inst) -> void {
  604. out_ << " ";
  605. FormatArg(inst.callee_id);
  606. if (!inst.args_id.is_valid()) {
  607. out_ << "(<invalid>)";
  608. return;
  609. }
  610. llvm::ArrayRef<InstId> args = sem_ir_.inst_blocks().Get(inst.args_id);
  611. auto return_info = ReturnTypeInfo::ForType(sem_ir_, inst.type_id);
  612. bool has_return_slot = return_info.has_return_slot();
  613. InstId return_slot_id = InstId::Invalid;
  614. if (has_return_slot) {
  615. return_slot_id = args.back();
  616. args = args.drop_back();
  617. }
  618. llvm::ListSeparator sep;
  619. out_ << '(';
  620. for (auto inst_id : args) {
  621. out_ << sep;
  622. FormatArg(inst_id);
  623. }
  624. out_ << ')';
  625. if (has_return_slot) {
  626. FormatReturnSlot(return_slot_id);
  627. }
  628. }
  629. auto FormatInstRHS(ArrayInit inst) -> void {
  630. FormatArgs(inst.inits_id);
  631. FormatReturnSlot(inst.dest_id);
  632. }
  633. auto FormatInstRHS(InitializeFrom inst) -> void {
  634. FormatArgs(inst.src_id);
  635. FormatReturnSlot(inst.dest_id);
  636. }
  637. auto FormatInstRHS(ReturnExpr ret) -> void {
  638. FormatArgs(ret.expr_id);
  639. if (ret.dest_id.is_valid()) {
  640. FormatReturnSlot(ret.dest_id);
  641. }
  642. }
  643. auto FormatInstRHS(StructInit init) -> void {
  644. FormatArgs(init.elements_id);
  645. FormatReturnSlot(init.dest_id);
  646. }
  647. auto FormatInstRHS(TupleInit init) -> void {
  648. FormatArgs(init.elements_id);
  649. FormatReturnSlot(init.dest_id);
  650. }
  651. auto FormatInstRHS(FunctionDecl inst) -> void {
  652. FormatArgs(inst.function_id);
  653. FormatTrailingBlock(inst.decl_block_id);
  654. }
  655. auto FormatInstRHS(FunctionType inst) -> void {
  656. if (inst.specific_id.is_valid()) {
  657. FormatArgs(inst.function_id, inst.specific_id);
  658. } else {
  659. FormatArgs(inst.function_id);
  660. }
  661. }
  662. auto FormatInstRHS(ClassDecl inst) -> void {
  663. FormatArgs(inst.class_id);
  664. FormatTrailingBlock(inst.decl_block_id);
  665. }
  666. auto FormatInstRHS(ClassType inst) -> void {
  667. if (inst.specific_id.is_valid()) {
  668. FormatArgs(inst.class_id, inst.specific_id);
  669. } else {
  670. FormatArgs(inst.class_id);
  671. }
  672. }
  673. auto FormatInstRHS(ImplDecl inst) -> void {
  674. FormatArgs(inst.impl_id);
  675. FormatTrailingBlock(inst.decl_block_id);
  676. }
  677. auto FormatInstRHS(InterfaceDecl inst) -> void {
  678. FormatArgs(inst.interface_id);
  679. FormatTrailingBlock(inst.decl_block_id);
  680. }
  681. auto FormatInstRHS(InterfaceType inst) -> void {
  682. if (inst.specific_id.is_valid()) {
  683. FormatArgs(inst.interface_id, inst.specific_id);
  684. } else {
  685. FormatArgs(inst.interface_id);
  686. }
  687. }
  688. auto FormatInstRHS(IntLiteral inst) -> void {
  689. out_ << " ";
  690. sem_ir_.ints()
  691. .Get(inst.int_id)
  692. .print(out_, sem_ir_.types().IsSignedInt(inst.type_id));
  693. }
  694. auto FormatInstRHS(FloatLiteral inst) -> void {
  695. llvm::SmallVector<char, 16> buffer;
  696. sem_ir_.floats().Get(inst.float_id).toString(buffer);
  697. out_ << " " << buffer;
  698. }
  699. auto FormatInstRHS(ImportRefUnloaded inst) -> void {
  700. FormatArgs(inst.import_ir_inst_id);
  701. out_ << ", unloaded";
  702. }
  703. auto FormatInstRHS(ImportRefLoaded inst) -> void {
  704. FormatArgs(inst.import_ir_inst_id);
  705. out_ << ", loaded";
  706. }
  707. auto FormatInstRHS(SpliceBlock inst) -> void {
  708. FormatArgs(inst.result_id);
  709. FormatTrailingBlock(inst.block_id);
  710. }
  711. // StructTypeFields are formatted as part of their StructType.
  712. auto FormatInst(InstId /*inst_id*/, StructTypeField /*inst*/) -> void {}
  713. auto FormatInstRHS(StructType inst) -> void {
  714. out_ << " {";
  715. llvm::ListSeparator sep;
  716. for (auto field_id : sem_ir_.inst_blocks().Get(inst.fields_id)) {
  717. out_ << sep << ".";
  718. auto field = sem_ir_.insts().GetAs<StructTypeField>(field_id);
  719. FormatName(field.name_id);
  720. out_ << ": ";
  721. FormatType(field.field_type_id);
  722. }
  723. out_ << "}";
  724. }
  725. auto FormatArgs() -> void {}
  726. template <typename... Args>
  727. auto FormatArgs(Args... args) -> void {
  728. out_ << ' ';
  729. llvm::ListSeparator sep;
  730. ((out_ << sep, FormatArg(args)), ...);
  731. }
  732. auto FormatArg(BoolValue v) -> void { out_ << v; }
  733. auto FormatArg(BuiltinInstKind kind) -> void { out_ << kind.label(); }
  734. auto FormatArg(EntityNameId id) -> void {
  735. const auto& info = sem_ir_.entity_names().Get(id);
  736. FormatName(info.name_id);
  737. if (info.bind_index.is_valid()) {
  738. out_ << " " << info.bind_index.index;
  739. }
  740. }
  741. auto FormatArg(FunctionId id) -> void { FormatName(id); }
  742. auto FormatArg(ClassId id) -> void { FormatName(id); }
  743. auto FormatArg(InterfaceId id) -> void { FormatName(id); }
  744. auto FormatArg(IntKind k) -> void { k.Print(out_); }
  745. auto FormatArg(FloatKind k) -> void { k.Print(out_); }
  746. auto FormatArg(ImplId id) -> void { FormatName(id); }
  747. auto FormatArg(ImportIRId id) -> void {
  748. if (!id.is_valid()) {
  749. out_ << id;
  750. return;
  751. }
  752. const auto& import_ir = *sem_ir_.import_irs().Get(id).sem_ir;
  753. if (import_ir.package_id().is_valid()) {
  754. out_ << import_ir.identifiers().Get(import_ir.package_id());
  755. } else {
  756. out_ << "Main";
  757. }
  758. out_ << "//";
  759. if (import_ir.library_id().is_valid()) {
  760. out_ << import_ir.string_literal_values().Get(import_ir.library_id());
  761. } else {
  762. out_ << "default";
  763. }
  764. }
  765. auto FormatArg(ImportIRInstId id) -> void {
  766. // Don't format the inst_id because it refers to a different IR.
  767. // TODO: Consider a better way to format the InstID from other IRs.
  768. auto import_ir_inst = sem_ir_.import_ir_insts().Get(id);
  769. FormatArg(import_ir_inst.ir_id);
  770. out_ << ", " << import_ir_inst.inst_id;
  771. }
  772. auto FormatArg(IntId id) -> void {
  773. // We don't know the signedness to use here. Default to unsigned.
  774. sem_ir_.ints().Get(id).print(out_, /*isSigned=*/false);
  775. }
  776. auto FormatArg(LocId id) -> void {
  777. if (id.is_import_ir_inst_id()) {
  778. out_ << "{";
  779. FormatArg(id.import_ir_inst_id());
  780. out_ << "}";
  781. } else {
  782. // TODO: For a NodeId, this prints the index of the node. Do we want it to
  783. // print a line number or something in order to make it less dependent on
  784. // parse?
  785. out_ << id;
  786. }
  787. }
  788. auto FormatArg(ElementIndex index) -> void { out_ << index; }
  789. auto FormatArg(NameScopeId id) -> void {
  790. OpenBrace();
  791. FormatNameScope(id);
  792. CloseBrace();
  793. }
  794. auto FormatArg(InstId id) -> void { FormatName(id); }
  795. auto FormatArg(InstBlockId id) -> void {
  796. if (!id.is_valid()) {
  797. out_ << "invalid";
  798. return;
  799. }
  800. out_ << '(';
  801. llvm::ListSeparator sep;
  802. for (auto inst_id : sem_ir_.inst_blocks().Get(id)) {
  803. out_ << sep;
  804. FormatArg(inst_id);
  805. }
  806. out_ << ')';
  807. }
  808. auto FormatArg(SpecificId id) -> void { FormatName(id); }
  809. auto FormatArg(RealId id) -> void {
  810. // TODO: Format with a `.` when the exponent is near zero.
  811. const auto& real = sem_ir_.reals().Get(id);
  812. real.mantissa.print(out_, /*isSigned=*/false);
  813. out_ << (real.is_decimal ? 'e' : 'p') << real.exponent;
  814. }
  815. auto FormatArg(StringLiteralValueId id) -> void {
  816. out_ << '"';
  817. out_.write_escaped(sem_ir_.string_literal_values().Get(id),
  818. /*UseHexEscapes=*/true);
  819. out_ << '"';
  820. }
  821. auto FormatArg(NameId id) -> void { FormatName(id); }
  822. auto FormatArg(TypeId id) -> void { FormatType(id); }
  823. auto FormatArg(TypeBlockId id) -> void {
  824. out_ << '(';
  825. llvm::ListSeparator sep;
  826. for (auto type_id : sem_ir_.type_blocks().Get(id)) {
  827. out_ << sep;
  828. FormatArg(type_id);
  829. }
  830. out_ << ')';
  831. }
  832. auto FormatReturnSlot(InstId dest_id) -> void {
  833. out_ << " to ";
  834. FormatArg(dest_id);
  835. }
  836. auto FormatName(NameId id) -> void {
  837. out_ << sem_ir_.names().GetFormatted(id);
  838. }
  839. auto FormatName(InstId id) -> void {
  840. out_ << inst_namer_->GetNameFor(scope_, id);
  841. }
  842. template <typename IdT>
  843. auto FormatName(IdT id) -> void {
  844. out_ << inst_namer_->GetNameFor(id);
  845. }
  846. auto FormatName(SpecificId id) -> void {
  847. const auto& specific = sem_ir_.specifics().Get(id);
  848. FormatName(specific.generic_id);
  849. FormatArg(specific.args_id);
  850. }
  851. auto FormatLabel(InstBlockId id) -> void {
  852. out_ << inst_namer_->GetLabelFor(scope_, id);
  853. }
  854. auto FormatConstant(ConstantId id) -> void {
  855. if (!id.is_valid()) {
  856. out_ << "<not constant>";
  857. return;
  858. }
  859. // For a symbolic constant in a generic, list the constant value in the
  860. // generic first, and the canonical constant second.
  861. if (id.is_symbolic()) {
  862. const auto& symbolic_constant =
  863. sem_ir_.constant_values().GetSymbolicConstant(id);
  864. if (symbolic_constant.generic_id.is_valid()) {
  865. const auto& generic =
  866. sem_ir_.generics().Get(symbolic_constant.generic_id);
  867. FormatName(sem_ir_.inst_blocks().Get(generic.GetEvalBlock(
  868. symbolic_constant.index
  869. .region()))[symbolic_constant.index.index()]);
  870. out_ << " (";
  871. FormatName(sem_ir_.constant_values().GetInstId(id));
  872. out_ << ")";
  873. return;
  874. }
  875. }
  876. FormatName(sem_ir_.constant_values().GetInstId(id));
  877. }
  878. auto FormatType(TypeId id) -> void {
  879. if (!id.is_valid()) {
  880. out_ << "invalid";
  881. } else {
  882. // Types are formatted in the `constants` scope because they only refer to
  883. // constants.
  884. llvm::SaveAndRestore file_scope(scope_, InstNamer::ScopeId::Constants);
  885. FormatConstant(sem_ir_.types().GetConstantId(id));
  886. }
  887. }
  888. const File& sem_ir_;
  889. InstNamer* const inst_namer_;
  890. // The output stream. Set while formatting instructions.
  891. llvm::raw_ostream& out_;
  892. // The current scope that we are formatting within. References to names in
  893. // this scope will not have a `@scope.` prefix added.
  894. InstNamer::ScopeId scope_ = InstNamer::ScopeId::None;
  895. // Whether we are formatting in a terminator sequence, that is, a sequence of
  896. // branches at the end of a block. The entirety of a terminator sequence is
  897. // formatted on a single line, despite being multiple instructions.
  898. bool in_terminator_sequence_ = false;
  899. // The indent depth to use for new instructions.
  900. int indent_;
  901. // Whether we are currently formatting immediately after an open brace. If so,
  902. // a newline will be inserted before the next line indent.
  903. bool after_open_brace_ = false;
  904. // The constant value of the current instruction, if it has one that has not
  905. // yet been printed. The value `NotConstant` is used as a sentinel to indicate
  906. // there is nothing to print.
  907. ConstantId pending_constant_value_ = ConstantId::NotConstant;
  908. // Whether `pending_constant_value_`'s instruction is the same as the
  909. // instruction currently being printed. If true, only the phase of the
  910. // constant is printed, and the value is omitted.
  911. bool pending_constant_value_is_self_ = false;
  912. };
  913. Formatter::Formatter(const Lex::TokenizedBuffer& tokenized_buffer,
  914. const Parse::Tree& parse_tree, const File& sem_ir)
  915. : sem_ir_(sem_ir), inst_namer_(tokenized_buffer, parse_tree, sem_ir) {}
  916. Formatter::~Formatter() = default;
  917. auto Formatter::Print(llvm::raw_ostream& out) -> void {
  918. FormatterImpl formatter(sem_ir_, &inst_namer_, out, /*indent=*/0);
  919. formatter.Format();
  920. }
  921. auto Formatter::PrintPartialTrailingCodeBlock(
  922. llvm::ArrayRef<SemIR::InstId> block, int indent, llvm::raw_ostream& out)
  923. -> void {
  924. FormatterImpl formatter(sem_ir_, &inst_namer_, out, indent);
  925. formatter.FormatPartialTrailingCodeBlock(block);
  926. }
  927. auto Formatter::PrintInst(SemIR::InstId inst_id, int indent,
  928. llvm::raw_ostream& out) -> void {
  929. FormatterImpl formatter(sem_ir_, &inst_namer_, out, indent);
  930. formatter.FormatInst(inst_id);
  931. }
  932. } // namespace Carbon::SemIR