formatter.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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. if (!fn.body_block_ids.empty() && fn.has_return_slot()) {
  257. FormatName(fn.return_storage_id);
  258. out_ << ": ";
  259. }
  260. FormatType(sem_ir_.insts().Get(fn.return_storage_id).type_id());
  261. }
  262. if (fn.builtin_function_kind != BuiltinFunctionKind::None) {
  263. out_ << " = \"";
  264. out_.write_escaped(fn.builtin_function_kind.name(),
  265. /*UseHexEscapes=*/true);
  266. out_ << "\"";
  267. }
  268. if (!fn.body_block_ids.empty()) {
  269. out_ << ' ';
  270. OpenBrace();
  271. for (auto block_id : fn.body_block_ids) {
  272. IndentLabel();
  273. FormatLabel(block_id);
  274. out_ << ":\n";
  275. FormatCodeBlock(block_id);
  276. }
  277. CloseBrace();
  278. out_ << '\n';
  279. } else {
  280. out_ << ";\n";
  281. }
  282. FormatEntityEnd(fn.generic_id);
  283. }
  284. auto FormatGenericStart(llvm::StringRef entity_kind, GenericId generic_id)
  285. -> void {
  286. const auto& generic = sem_ir_.generics().Get(generic_id);
  287. out_ << "\n";
  288. Indent();
  289. out_ << "generic " << entity_kind << " ";
  290. FormatName(generic_id);
  291. llvm::SaveAndRestore generic_scope(scope_,
  292. inst_namer_->GetScopeFor(generic_id));
  293. out_ << "(";
  294. FormatParamList(generic.bindings_id);
  295. out_ << ") ";
  296. OpenBrace();
  297. FormatCodeBlock(generic.decl_block_id);
  298. if (generic.definition_block_id.is_valid()) {
  299. IndentLabel();
  300. out_ << "!definition:\n";
  301. FormatCodeBlock(generic.definition_block_id);
  302. }
  303. }
  304. auto FormatGenericEnd() -> void {
  305. CloseBrace();
  306. out_ << '\n';
  307. }
  308. auto FormatSpecificRegion(const Generic& generic, const Specific& specific,
  309. GenericInstIndex::Region region,
  310. llvm::StringRef region_name) -> void {
  311. if (!specific.GetValueBlock(region).is_valid()) {
  312. return;
  313. }
  314. if (!region_name.empty()) {
  315. IndentLabel();
  316. out_ << "!" << region_name << ":\n";
  317. }
  318. for (auto [generic_inst_id, specific_inst_id] : llvm::zip_longest(
  319. sem_ir_.inst_blocks().Get(generic.GetEvalBlock(region)),
  320. sem_ir_.inst_blocks().Get(specific.GetValueBlock(region)))) {
  321. if (generic_inst_id && specific_inst_id &&
  322. sem_ir_.insts().Is<StructTypeField>(*generic_inst_id) &&
  323. sem_ir_.insts().Is<StructTypeField>(*specific_inst_id)) {
  324. // Skip printing struct type fields to match the way we print the
  325. // generic.
  326. continue;
  327. }
  328. Indent();
  329. if (generic_inst_id) {
  330. FormatName(*generic_inst_id);
  331. } else {
  332. out_ << "<missing>";
  333. }
  334. out_ << " => ";
  335. if (specific_inst_id) {
  336. FormatName(*specific_inst_id);
  337. } else {
  338. out_ << "<missing>";
  339. }
  340. out_ << "\n";
  341. }
  342. }
  343. auto FormatSpecific(SpecificId id) -> void {
  344. const auto& specific = sem_ir_.specifics().Get(id);
  345. out_ << "\n";
  346. out_ << "specific ";
  347. FormatName(id);
  348. // TODO: Remove once we stop forming generic specifics with no generic
  349. // during import.
  350. if (!specific.generic_id.is_valid()) {
  351. out_ << ";\n";
  352. return;
  353. }
  354. out_ << " ";
  355. const auto& generic = sem_ir_.generics().Get(specific.generic_id);
  356. llvm::SaveAndRestore generic_scope(
  357. scope_, inst_namer_->GetScopeFor(specific.generic_id));
  358. OpenBrace();
  359. FormatSpecificRegion(generic, specific,
  360. GenericInstIndex::Region::Declaration, "");
  361. FormatSpecificRegion(generic, specific,
  362. GenericInstIndex::Region::Definition, "definition");
  363. CloseBrace();
  364. out_ << "\n";
  365. }
  366. auto FormatParamList(InstBlockId param_refs_id) -> void {
  367. llvm::ListSeparator sep;
  368. for (InstId param_id : sem_ir_.inst_blocks().Get(param_refs_id)) {
  369. out_ << sep;
  370. if (!param_id.is_valid()) {
  371. out_ << "invalid";
  372. continue;
  373. }
  374. if (auto addr = sem_ir_.insts().TryGetAs<SemIR::AddrPattern>(param_id)) {
  375. out_ << "addr ";
  376. param_id = addr->inner_id;
  377. }
  378. FormatName(param_id);
  379. out_ << ": ";
  380. FormatType(sem_ir_.insts().Get(param_id).type_id());
  381. }
  382. }
  383. auto FormatCodeBlock(InstBlockId block_id) -> void {
  384. if (block_id.is_valid()) {
  385. FormatCodeBlock(sem_ir_.inst_blocks().Get(block_id));
  386. }
  387. }
  388. auto FormatCodeBlock(llvm::ArrayRef<InstId> block) -> void {
  389. for (const InstId inst_id : block) {
  390. FormatInst(inst_id);
  391. }
  392. }
  393. auto FormatTrailingBlock(InstBlockId block_id) -> void {
  394. out_ << ' ';
  395. OpenBrace();
  396. FormatCodeBlock(block_id);
  397. CloseBrace();
  398. }
  399. auto FormatNameScope(NameScopeId id, llvm::StringRef label = "") -> void {
  400. const auto& scope = sem_ir_.name_scopes().Get(id);
  401. if (scope.names.empty() && scope.extended_scopes.empty() &&
  402. scope.import_ir_scopes.empty() && !scope.has_error) {
  403. // Name scope is empty.
  404. return;
  405. }
  406. if (!label.empty()) {
  407. IndentLabel();
  408. out_ << label;
  409. }
  410. for (auto [name_id, inst_id, access_kind] : scope.names) {
  411. Indent();
  412. out_ << ".";
  413. FormatName(name_id);
  414. switch (access_kind) {
  415. case SemIR::AccessKind::Public:
  416. break;
  417. case SemIR::AccessKind::Protected:
  418. out_ << " [protected]";
  419. break;
  420. case SemIR::AccessKind::Private:
  421. out_ << " [private]";
  422. break;
  423. }
  424. out_ << " = ";
  425. FormatName(inst_id);
  426. out_ << "\n";
  427. }
  428. for (auto extended_scope_id : scope.extended_scopes) {
  429. // TODO: Print this scope in a better way.
  430. Indent();
  431. out_ << "extend " << extended_scope_id << "\n";
  432. }
  433. for (auto [import_ir_id, unused] : scope.import_ir_scopes) {
  434. Indent();
  435. out_ << "import ";
  436. FormatArg(import_ir_id);
  437. out_ << "\n";
  438. }
  439. if (scope.has_error) {
  440. Indent();
  441. out_ << "has_error\n";
  442. }
  443. }
  444. auto FormatInst(InstId inst_id, Inst inst) -> void {
  445. CARBON_KIND_SWITCH(inst) {
  446. #define CARBON_SEM_IR_INST_KIND(InstT) \
  447. case CARBON_KIND(InstT typed_inst): { \
  448. FormatInst(inst_id, typed_inst); \
  449. break; \
  450. }
  451. #include "toolchain/sem_ir/inst_kind.def"
  452. }
  453. }
  454. template <typename InstT>
  455. auto FormatInst(InstId inst_id, InstT inst) -> void {
  456. Indent();
  457. FormatInstLHS(inst_id, inst);
  458. out_ << InstT::Kind.ir_name();
  459. pending_constant_value_ = sem_ir_.constant_values().Get(inst_id);
  460. pending_constant_value_is_self_ =
  461. sem_ir_.constant_values().GetInstId(pending_constant_value_) == inst_id;
  462. FormatInstRHS(inst);
  463. FormatPendingConstantValue(AddSpace::Before);
  464. out_ << "\n";
  465. }
  466. // Don't print a constant for ImportRefUnloaded.
  467. auto FormatInst(InstId inst_id, ImportRefUnloaded inst) -> void {
  468. Indent();
  469. FormatInstLHS(inst_id, inst);
  470. out_ << ImportRefUnloaded::Kind.ir_name();
  471. FormatInstRHS(inst);
  472. out_ << "\n";
  473. }
  474. // If there is a pending constant value attached to the current instruction,
  475. // print it now and clear it out. The constant value gets printed before the
  476. // first braced block argument, or at the end of the instruction if there are
  477. // no such arguments.
  478. auto FormatPendingConstantValue(AddSpace space_where) -> void {
  479. if (pending_constant_value_ == ConstantId::NotConstant) {
  480. return;
  481. }
  482. if (space_where == AddSpace::Before) {
  483. out_ << ' ';
  484. }
  485. out_ << '[';
  486. if (pending_constant_value_.is_valid()) {
  487. out_ << (pending_constant_value_.is_symbolic() ? "symbolic" : "template");
  488. if (!pending_constant_value_is_self_) {
  489. out_ << " = ";
  490. FormatConstant(pending_constant_value_);
  491. }
  492. } else {
  493. out_ << pending_constant_value_;
  494. }
  495. out_ << ']';
  496. if (space_where == AddSpace::After) {
  497. out_ << ' ';
  498. }
  499. pending_constant_value_ = ConstantId::NotConstant;
  500. }
  501. auto FormatInstLHS(InstId inst_id, Inst inst) -> void {
  502. switch (inst.kind().value_kind()) {
  503. case InstValueKind::Typed:
  504. FormatName(inst_id);
  505. out_ << ": ";
  506. switch (GetExprCategory(sem_ir_, inst_id)) {
  507. case ExprCategory::NotExpr:
  508. case ExprCategory::Error:
  509. case ExprCategory::Value:
  510. case ExprCategory::Mixed:
  511. break;
  512. case ExprCategory::DurableRef:
  513. case ExprCategory::EphemeralRef:
  514. out_ << "ref ";
  515. break;
  516. case ExprCategory::Initializing:
  517. out_ << "init ";
  518. break;
  519. }
  520. FormatType(inst.type_id());
  521. out_ << " = ";
  522. break;
  523. case InstValueKind::None:
  524. break;
  525. }
  526. }
  527. // Format ImportDecl with its name.
  528. auto FormatInstLHS(InstId inst_id, ImportDecl /*inst*/) -> void {
  529. FormatName(inst_id);
  530. out_ << " = ";
  531. }
  532. // Print ImportRefUnloaded with type-like semantics even though it lacks a
  533. // type_id.
  534. auto FormatInstLHS(InstId inst_id, ImportRefUnloaded /*inst*/) -> void {
  535. FormatName(inst_id);
  536. out_ << " = ";
  537. }
  538. template <typename InstT>
  539. auto FormatInstRHS(InstT inst) -> void {
  540. // By default, an instruction has a comma-separated argument list.
  541. using Info = Internal::InstLikeTypeInfo<InstT>;
  542. if constexpr (Info::NumArgs == 2) {
  543. FormatArgs(Info::template Get<0>(inst), Info::template Get<1>(inst));
  544. } else if constexpr (Info::NumArgs == 1) {
  545. FormatArgs(Info::template Get<0>(inst));
  546. } else {
  547. FormatArgs();
  548. }
  549. }
  550. auto FormatInstRHS(BindSymbolicName inst) -> void {
  551. // A BindSymbolicName with no value is a purely symbolic binding, such as
  552. // the `Self` in an interface. Don't print out `invalid` for the value.
  553. if (inst.value_id.is_valid()) {
  554. FormatArgs(inst.entity_name_id, inst.value_id);
  555. } else {
  556. FormatArgs(inst.entity_name_id);
  557. }
  558. }
  559. auto FormatInstRHS(BlockArg inst) -> void {
  560. out_ << " ";
  561. FormatLabel(inst.block_id);
  562. }
  563. auto FormatInstRHS(Namespace inst) -> void {
  564. if (inst.import_id.is_valid()) {
  565. FormatArgs(inst.import_id, inst.name_scope_id);
  566. } else {
  567. FormatArgs(inst.name_scope_id);
  568. }
  569. }
  570. auto FormatInst(InstId /*inst_id*/, BranchIf inst) -> void {
  571. if (!in_terminator_sequence_) {
  572. Indent();
  573. }
  574. out_ << "if ";
  575. FormatName(inst.cond_id);
  576. out_ << " " << Branch::Kind.ir_name() << " ";
  577. FormatLabel(inst.target_id);
  578. out_ << " else ";
  579. in_terminator_sequence_ = true;
  580. }
  581. auto FormatInst(InstId /*inst_id*/, BranchWithArg inst) -> void {
  582. if (!in_terminator_sequence_) {
  583. Indent();
  584. }
  585. out_ << BranchWithArg::Kind.ir_name() << " ";
  586. FormatLabel(inst.target_id);
  587. out_ << "(";
  588. FormatName(inst.arg_id);
  589. out_ << ")\n";
  590. in_terminator_sequence_ = false;
  591. }
  592. auto FormatInst(InstId /*inst_id*/, Branch inst) -> void {
  593. if (!in_terminator_sequence_) {
  594. Indent();
  595. }
  596. out_ << Branch::Kind.ir_name() << " ";
  597. FormatLabel(inst.target_id);
  598. out_ << "\n";
  599. in_terminator_sequence_ = false;
  600. }
  601. auto FormatInstRHS(Call inst) -> void {
  602. out_ << " ";
  603. FormatArg(inst.callee_id);
  604. if (!inst.args_id.is_valid()) {
  605. out_ << "(<invalid>)";
  606. return;
  607. }
  608. llvm::ArrayRef<InstId> args = sem_ir_.inst_blocks().Get(inst.args_id);
  609. bool has_return_slot = GetInitRepr(sem_ir_, inst.type_id).has_return_slot();
  610. InstId return_slot_id = InstId::Invalid;
  611. if (has_return_slot) {
  612. return_slot_id = args.back();
  613. args = args.drop_back();
  614. }
  615. llvm::ListSeparator sep;
  616. out_ << '(';
  617. for (auto inst_id : args) {
  618. out_ << sep;
  619. FormatArg(inst_id);
  620. }
  621. out_ << ')';
  622. if (has_return_slot) {
  623. FormatReturnSlot(return_slot_id);
  624. }
  625. }
  626. auto FormatInstRHS(ArrayInit inst) -> void {
  627. FormatArgs(inst.inits_id);
  628. FormatReturnSlot(inst.dest_id);
  629. }
  630. auto FormatInstRHS(InitializeFrom inst) -> void {
  631. FormatArgs(inst.src_id);
  632. FormatReturnSlot(inst.dest_id);
  633. }
  634. auto FormatInstRHS(ReturnExpr ret) -> void {
  635. FormatArgs(ret.expr_id);
  636. if (ret.dest_id.is_valid()) {
  637. FormatReturnSlot(ret.dest_id);
  638. }
  639. }
  640. auto FormatInstRHS(StructInit init) -> void {
  641. FormatArgs(init.elements_id);
  642. FormatReturnSlot(init.dest_id);
  643. }
  644. auto FormatInstRHS(TupleInit init) -> void {
  645. FormatArgs(init.elements_id);
  646. FormatReturnSlot(init.dest_id);
  647. }
  648. auto FormatInstRHS(FunctionDecl inst) -> void {
  649. FormatArgs(inst.function_id);
  650. FormatTrailingBlock(inst.decl_block_id);
  651. }
  652. auto FormatInstRHS(FunctionType inst) -> void {
  653. if (inst.specific_id.is_valid()) {
  654. FormatArgs(inst.function_id, inst.specific_id);
  655. } else {
  656. FormatArgs(inst.function_id);
  657. }
  658. }
  659. auto FormatInstRHS(ClassDecl inst) -> void {
  660. FormatArgs(inst.class_id);
  661. FormatTrailingBlock(inst.decl_block_id);
  662. }
  663. auto FormatInstRHS(ClassType inst) -> void {
  664. if (inst.specific_id.is_valid()) {
  665. FormatArgs(inst.class_id, inst.specific_id);
  666. } else {
  667. FormatArgs(inst.class_id);
  668. }
  669. }
  670. auto FormatInstRHS(ImplDecl inst) -> void {
  671. FormatArgs(inst.impl_id);
  672. FormatTrailingBlock(inst.decl_block_id);
  673. }
  674. auto FormatInstRHS(InterfaceDecl inst) -> void {
  675. FormatArgs(inst.interface_id);
  676. FormatTrailingBlock(inst.decl_block_id);
  677. }
  678. auto FormatInstRHS(InterfaceType inst) -> void {
  679. if (inst.specific_id.is_valid()) {
  680. FormatArgs(inst.interface_id, inst.specific_id);
  681. } else {
  682. FormatArgs(inst.interface_id);
  683. }
  684. }
  685. auto FormatInstRHS(IntLiteral inst) -> void {
  686. out_ << " ";
  687. sem_ir_.ints()
  688. .Get(inst.int_id)
  689. .print(out_, sem_ir_.types().IsSignedInt(inst.type_id));
  690. }
  691. auto FormatInstRHS(FloatLiteral inst) -> void {
  692. llvm::SmallVector<char, 16> buffer;
  693. sem_ir_.floats().Get(inst.float_id).toString(buffer);
  694. out_ << " " << buffer;
  695. }
  696. auto FormatInstRHS(ImportRefUnloaded inst) -> void {
  697. FormatArgs(inst.import_ir_inst_id);
  698. out_ << ", unloaded";
  699. }
  700. auto FormatInstRHS(ImportRefLoaded inst) -> void {
  701. FormatArgs(inst.import_ir_inst_id);
  702. out_ << ", loaded";
  703. }
  704. auto FormatInstRHS(SpliceBlock inst) -> void {
  705. FormatArgs(inst.result_id);
  706. FormatTrailingBlock(inst.block_id);
  707. }
  708. // StructTypeFields are formatted as part of their StructType.
  709. auto FormatInst(InstId /*inst_id*/, StructTypeField /*inst*/) -> void {}
  710. auto FormatInstRHS(StructType inst) -> void {
  711. out_ << " {";
  712. llvm::ListSeparator sep;
  713. for (auto field_id : sem_ir_.inst_blocks().Get(inst.fields_id)) {
  714. out_ << sep << ".";
  715. auto field = sem_ir_.insts().GetAs<StructTypeField>(field_id);
  716. FormatName(field.name_id);
  717. out_ << ": ";
  718. FormatType(field.field_type_id);
  719. }
  720. out_ << "}";
  721. }
  722. auto FormatArgs() -> void {}
  723. template <typename... Args>
  724. auto FormatArgs(Args... args) -> void {
  725. out_ << ' ';
  726. llvm::ListSeparator sep;
  727. ((out_ << sep, FormatArg(args)), ...);
  728. }
  729. auto FormatArg(BoolValue v) -> void { out_ << v; }
  730. auto FormatArg(BuiltinInstKind kind) -> void { out_ << kind.label(); }
  731. auto FormatArg(EntityNameId id) -> void {
  732. const auto& info = sem_ir_.entity_names().Get(id);
  733. FormatName(info.name_id);
  734. if (info.bind_index.is_valid()) {
  735. out_ << " " << info.bind_index.index;
  736. }
  737. }
  738. auto FormatArg(FunctionId id) -> void { FormatName(id); }
  739. auto FormatArg(ClassId id) -> void { FormatName(id); }
  740. auto FormatArg(InterfaceId id) -> void { FormatName(id); }
  741. auto FormatArg(IntKind k) -> void { k.Print(out_); }
  742. auto FormatArg(FloatKind k) -> void { k.Print(out_); }
  743. auto FormatArg(ImplId id) -> void { FormatName(id); }
  744. auto FormatArg(ImportIRId id) -> void {
  745. if (!id.is_valid()) {
  746. out_ << id;
  747. return;
  748. }
  749. const auto& import_ir = *sem_ir_.import_irs().Get(id).sem_ir;
  750. if (import_ir.package_id().is_valid()) {
  751. out_ << import_ir.identifiers().Get(import_ir.package_id());
  752. } else {
  753. out_ << "Main";
  754. }
  755. out_ << "//";
  756. if (import_ir.library_id().is_valid()) {
  757. out_ << import_ir.string_literal_values().Get(import_ir.library_id());
  758. } else {
  759. out_ << "default";
  760. }
  761. }
  762. auto FormatArg(ImportIRInstId id) -> void {
  763. // Don't format the inst_id because it refers to a different IR.
  764. // TODO: Consider a better way to format the InstID from other IRs.
  765. auto import_ir_inst = sem_ir_.import_ir_insts().Get(id);
  766. FormatArg(import_ir_inst.ir_id);
  767. out_ << ", " << import_ir_inst.inst_id;
  768. }
  769. auto FormatArg(IntId id) -> void {
  770. // We don't know the signedness to use here. Default to unsigned.
  771. sem_ir_.ints().Get(id).print(out_, /*isSigned=*/false);
  772. }
  773. auto FormatArg(LocId id) -> void {
  774. if (id.is_import_ir_inst_id()) {
  775. out_ << "{";
  776. FormatArg(id.import_ir_inst_id());
  777. out_ << "}";
  778. } else {
  779. // TODO: For a NodeId, this prints the index of the node. Do we want it to
  780. // print a line number or something in order to make it less dependent on
  781. // parse?
  782. out_ << id;
  783. }
  784. }
  785. auto FormatArg(ElementIndex index) -> void { out_ << index; }
  786. auto FormatArg(NameScopeId id) -> void {
  787. OpenBrace();
  788. FormatNameScope(id);
  789. CloseBrace();
  790. }
  791. auto FormatArg(InstId id) -> void { FormatName(id); }
  792. auto FormatArg(InstBlockId id) -> void {
  793. if (!id.is_valid()) {
  794. out_ << "invalid";
  795. return;
  796. }
  797. out_ << '(';
  798. llvm::ListSeparator sep;
  799. for (auto inst_id : sem_ir_.inst_blocks().Get(id)) {
  800. out_ << sep;
  801. FormatArg(inst_id);
  802. }
  803. out_ << ')';
  804. }
  805. auto FormatArg(SpecificId id) -> void { FormatName(id); }
  806. auto FormatArg(RealId id) -> void {
  807. // TODO: Format with a `.` when the exponent is near zero.
  808. const auto& real = sem_ir_.reals().Get(id);
  809. real.mantissa.print(out_, /*isSigned=*/false);
  810. out_ << (real.is_decimal ? 'e' : 'p') << real.exponent;
  811. }
  812. auto FormatArg(StringLiteralValueId id) -> void {
  813. out_ << '"';
  814. out_.write_escaped(sem_ir_.string_literal_values().Get(id),
  815. /*UseHexEscapes=*/true);
  816. out_ << '"';
  817. }
  818. auto FormatArg(NameId id) -> void { FormatName(id); }
  819. auto FormatArg(TypeId id) -> void { FormatType(id); }
  820. auto FormatArg(TypeBlockId id) -> void {
  821. out_ << '(';
  822. llvm::ListSeparator sep;
  823. for (auto type_id : sem_ir_.type_blocks().Get(id)) {
  824. out_ << sep;
  825. FormatArg(type_id);
  826. }
  827. out_ << ')';
  828. }
  829. auto FormatReturnSlot(InstId dest_id) -> void {
  830. out_ << " to ";
  831. FormatArg(dest_id);
  832. }
  833. auto FormatName(NameId id) -> void {
  834. out_ << sem_ir_.names().GetFormatted(id);
  835. }
  836. auto FormatName(InstId id) -> void {
  837. out_ << inst_namer_->GetNameFor(scope_, id);
  838. }
  839. template <typename IdT>
  840. auto FormatName(IdT id) -> void {
  841. out_ << inst_namer_->GetNameFor(id);
  842. }
  843. auto FormatName(SpecificId id) -> void {
  844. const auto& specific = sem_ir_.specifics().Get(id);
  845. FormatName(specific.generic_id);
  846. FormatArg(specific.args_id);
  847. }
  848. auto FormatLabel(InstBlockId id) -> void {
  849. out_ << inst_namer_->GetLabelFor(scope_, id);
  850. }
  851. auto FormatConstant(ConstantId id) -> void {
  852. if (!id.is_valid()) {
  853. out_ << "<not constant>";
  854. return;
  855. }
  856. // For a symbolic constant in a generic, list the constant value in the
  857. // generic first, and the canonical constant second.
  858. if (id.is_symbolic()) {
  859. const auto& symbolic_constant =
  860. sem_ir_.constant_values().GetSymbolicConstant(id);
  861. if (symbolic_constant.generic_id.is_valid()) {
  862. const auto& generic =
  863. sem_ir_.generics().Get(symbolic_constant.generic_id);
  864. FormatName(sem_ir_.inst_blocks().Get(generic.GetEvalBlock(
  865. symbolic_constant.index
  866. .region()))[symbolic_constant.index.index()]);
  867. out_ << " (";
  868. FormatName(sem_ir_.constant_values().GetInstId(id));
  869. out_ << ")";
  870. return;
  871. }
  872. }
  873. FormatName(sem_ir_.constant_values().GetInstId(id));
  874. }
  875. auto FormatType(TypeId id) -> void {
  876. if (!id.is_valid()) {
  877. out_ << "invalid";
  878. } else {
  879. // Types are formatted in the `constants` scope because they only refer to
  880. // constants.
  881. llvm::SaveAndRestore file_scope(scope_, InstNamer::ScopeId::Constants);
  882. FormatConstant(sem_ir_.types().GetConstantId(id));
  883. }
  884. }
  885. const File& sem_ir_;
  886. InstNamer* const inst_namer_;
  887. // The output stream. Set while formatting instructions.
  888. llvm::raw_ostream& out_;
  889. // The current scope that we are formatting within. References to names in
  890. // this scope will not have a `@scope.` prefix added.
  891. InstNamer::ScopeId scope_ = InstNamer::ScopeId::None;
  892. // Whether we are formatting in a terminator sequence, that is, a sequence of
  893. // branches at the end of a block. The entirety of a terminator sequence is
  894. // formatted on a single line, despite being multiple instructions.
  895. bool in_terminator_sequence_ = false;
  896. // The indent depth to use for new instructions.
  897. int indent_;
  898. // Whether we are currently formatting immediately after an open brace. If so,
  899. // a newline will be inserted before the next line indent.
  900. bool after_open_brace_ = false;
  901. // The constant value of the current instruction, if it has one that has not
  902. // yet been printed. The value `NotConstant` is used as a sentinel to indicate
  903. // there is nothing to print.
  904. ConstantId pending_constant_value_ = ConstantId::NotConstant;
  905. // Whether `pending_constant_value_`'s instruction is the same as the
  906. // instruction currently being printed. If true, only the phase of the
  907. // constant is printed, and the value is omitted.
  908. bool pending_constant_value_is_self_ = false;
  909. };
  910. Formatter::Formatter(const Lex::TokenizedBuffer& tokenized_buffer,
  911. const Parse::Tree& parse_tree, const File& sem_ir)
  912. : sem_ir_(sem_ir), inst_namer_(tokenized_buffer, parse_tree, sem_ir) {}
  913. Formatter::~Formatter() = default;
  914. auto Formatter::Print(llvm::raw_ostream& out) -> void {
  915. FormatterImpl formatter(sem_ir_, &inst_namer_, out, /*indent=*/0);
  916. formatter.Format();
  917. }
  918. auto Formatter::PrintPartialTrailingCodeBlock(
  919. llvm::ArrayRef<SemIR::InstId> block, int indent, llvm::raw_ostream& out)
  920. -> void {
  921. FormatterImpl formatter(sem_ir_, &inst_namer_, out, indent);
  922. formatter.FormatPartialTrailingCodeBlock(block);
  923. }
  924. auto Formatter::PrintInst(SemIR::InstId inst_id, int indent,
  925. llvm::raw_ostream& out) -> void {
  926. FormatterImpl formatter(sem_ir_, &inst_namer_, out, indent);
  927. formatter.FormatInst(inst_id);
  928. }
  929. } // namespace Carbon::SemIR