formatter.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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/typed_insts.h"
  18. namespace Carbon::SemIR {
  19. // Formatter for printing textual Semantics IR.
  20. class Formatter {
  21. public:
  22. enum class AddSpace : bool { Before, After };
  23. explicit Formatter(const Lex::TokenizedBuffer& tokenized_buffer,
  24. const Parse::Tree& parse_tree, const File& sem_ir,
  25. llvm::raw_ostream& out)
  26. : sem_ir_(sem_ir),
  27. out_(out),
  28. inst_namer_(tokenized_buffer, parse_tree, sem_ir) {}
  29. // Prints the SemIR.
  30. //
  31. // Constants are printed first and may be referenced by later sections,
  32. // including file-scoped instructions. The file scope may contain entity
  33. // declarations which are defined later, such as classes.
  34. auto Format() -> void {
  35. out_ << "--- " << sem_ir_.filename() << "\n\n";
  36. FormatConstants();
  37. out_ << inst_namer_.GetScopeName(InstNamer::ScopeId::File) << " ";
  38. OpenBrace();
  39. // TODO: Handle the case where there are multiple top-level instruction
  40. // blocks. For example, there may be branching in the initializer of a
  41. // global or a type expression.
  42. if (auto block_id = sem_ir_.top_inst_block_id(); block_id.is_valid()) {
  43. llvm::SaveAndRestore file_scope(scope_, InstNamer::ScopeId::File);
  44. FormatCodeBlock(block_id);
  45. }
  46. CloseBrace();
  47. out_ << '\n';
  48. for (int i : llvm::seq(sem_ir_.interfaces().size())) {
  49. FormatInterface(InterfaceId(i));
  50. }
  51. for (int i : llvm::seq(sem_ir_.impls().size())) {
  52. FormatImpl(ImplId(i));
  53. }
  54. for (int i : llvm::seq(sem_ir_.classes().size())) {
  55. FormatClass(ClassId(i));
  56. }
  57. for (int i : llvm::seq(sem_ir_.functions().size())) {
  58. FormatFunction(FunctionId(i));
  59. }
  60. // End-of-file newline.
  61. out_ << "\n";
  62. }
  63. // Begins a braced block. Writes an open brace, and prepares to insert a
  64. // newline after it if the braced block is non-empty.
  65. auto OpenBrace() -> void {
  66. // Put the constant value of an instruction before any braced block, rather
  67. // than at the end.
  68. FormatPendingConstantValue(AddSpace::After);
  69. out_ << '{';
  70. indent_ += 2;
  71. after_open_brace_ = true;
  72. }
  73. // Ends a braced block by writing a close brace.
  74. auto CloseBrace() -> void {
  75. indent_ -= 2;
  76. if (!after_open_brace_) {
  77. Indent();
  78. }
  79. out_ << '}';
  80. after_open_brace_ = false;
  81. }
  82. // Adds beginning-of-line indentation. If we're at the start of a braced
  83. // block, first starts a new line.
  84. auto Indent(int offset = 0) -> void {
  85. if (after_open_brace_) {
  86. out_ << '\n';
  87. after_open_brace_ = false;
  88. }
  89. out_.indent(indent_ + offset);
  90. }
  91. // Adds beginning-of-label indentation. This is one level less than normal
  92. // indentation. Labels also get a preceding blank line unless they're at the
  93. // start of a block.
  94. auto IndentLabel() -> void {
  95. CARBON_CHECK(indent_ >= 2);
  96. if (!after_open_brace_) {
  97. out_ << '\n';
  98. }
  99. Indent(-2);
  100. }
  101. auto FormatConstants() -> void {
  102. if (!sem_ir_.constants().size()) {
  103. return;
  104. }
  105. llvm::SaveAndRestore constants_scope(scope_, InstNamer::ScopeId::Constants);
  106. out_ << inst_namer_.GetScopeName(InstNamer::ScopeId::Constants) << " ";
  107. OpenBrace();
  108. FormatCodeBlock(sem_ir_.constants().array_ref());
  109. CloseBrace();
  110. out_ << "\n\n";
  111. }
  112. auto FormatClass(ClassId id) -> void {
  113. const Class& class_info = sem_ir_.classes().Get(id);
  114. out_ << "\nclass ";
  115. FormatClassName(id);
  116. llvm::SaveAndRestore class_scope(scope_, inst_namer_.GetScopeFor(id));
  117. if (class_info.scope_id.is_valid()) {
  118. out_ << ' ';
  119. OpenBrace();
  120. FormatCodeBlock(class_info.body_block_id);
  121. FormatNameScope(class_info.scope_id, "!members:\n");
  122. CloseBrace();
  123. out_ << '\n';
  124. } else {
  125. out_ << ";\n";
  126. }
  127. }
  128. auto FormatInterface(InterfaceId id) -> void {
  129. const Interface& interface_info = sem_ir_.interfaces().Get(id);
  130. out_ << "\ninterface ";
  131. FormatInterfaceName(id);
  132. llvm::SaveAndRestore interface_scope(scope_, inst_namer_.GetScopeFor(id));
  133. if (interface_info.scope_id.is_valid()) {
  134. out_ << ' ';
  135. OpenBrace();
  136. FormatCodeBlock(interface_info.body_block_id);
  137. // Always include the !members label because we always list the witness in
  138. // this section.
  139. IndentLabel();
  140. out_ << "!members:\n";
  141. FormatNameScope(interface_info.scope_id);
  142. Indent();
  143. out_ << "witness = ";
  144. FormatArg(interface_info.associated_entities_id);
  145. out_ << "\n";
  146. CloseBrace();
  147. out_ << '\n';
  148. } else {
  149. out_ << ";\n";
  150. }
  151. }
  152. auto FormatImpl(ImplId id) -> void {
  153. const Impl& impl_info = sem_ir_.impls().Get(id);
  154. out_ << "\nimpl ";
  155. FormatImplName(id);
  156. out_ << ": ";
  157. // TODO: Include the deduced parameter list if present.
  158. FormatType(impl_info.self_id);
  159. out_ << " as ";
  160. FormatType(impl_info.constraint_id);
  161. llvm::SaveAndRestore impl_scope(scope_, inst_namer_.GetScopeFor(id));
  162. if (impl_info.scope_id.is_valid()) {
  163. out_ << ' ';
  164. OpenBrace();
  165. FormatCodeBlock(impl_info.body_block_id);
  166. // Print the !members label even if the name scope is empty because we
  167. // always list the witness in this section.
  168. IndentLabel();
  169. out_ << "!members:\n";
  170. FormatNameScope(impl_info.scope_id);
  171. Indent();
  172. out_ << "witness = ";
  173. FormatArg(impl_info.witness_id);
  174. out_ << "\n";
  175. CloseBrace();
  176. out_ << '\n';
  177. } else {
  178. out_ << ";\n";
  179. }
  180. }
  181. auto FormatFunction(FunctionId id) -> void {
  182. const Function& fn = sem_ir_.functions().Get(id);
  183. out_ << "\n";
  184. if (fn.is_extern) {
  185. out_ << "extern ";
  186. }
  187. out_ << "fn ";
  188. FormatFunctionName(id);
  189. llvm::SaveAndRestore function_scope(scope_, inst_namer_.GetScopeFor(id));
  190. if (fn.implicit_param_refs_id != InstBlockId::Empty) {
  191. out_ << "[";
  192. FormatParamList(fn.implicit_param_refs_id);
  193. out_ << "]";
  194. }
  195. out_ << "(";
  196. FormatParamList(fn.param_refs_id);
  197. out_ << ")";
  198. if (fn.return_type_id.is_valid()) {
  199. out_ << " -> ";
  200. if (!fn.body_block_ids.empty() && fn.has_return_slot()) {
  201. FormatInstName(fn.return_storage_id);
  202. out_ << ": ";
  203. }
  204. FormatType(fn.return_type_id);
  205. }
  206. if (fn.builtin_kind != BuiltinFunctionKind::None) {
  207. out_ << " = \"";
  208. out_.write_escaped(fn.builtin_kind.name(),
  209. /*UseHexEscapes=*/true);
  210. out_ << "\"";
  211. }
  212. if (!fn.body_block_ids.empty()) {
  213. out_ << ' ';
  214. OpenBrace();
  215. for (auto block_id : fn.body_block_ids) {
  216. IndentLabel();
  217. FormatLabel(block_id);
  218. out_ << ":\n";
  219. FormatCodeBlock(block_id);
  220. }
  221. CloseBrace();
  222. out_ << '\n';
  223. } else {
  224. out_ << ";\n";
  225. }
  226. }
  227. auto FormatParamList(InstBlockId param_refs_id) -> void {
  228. llvm::ListSeparator sep;
  229. for (InstId param_id : sem_ir_.inst_blocks().Get(param_refs_id)) {
  230. out_ << sep;
  231. if (!param_id.is_valid()) {
  232. out_ << "invalid";
  233. continue;
  234. }
  235. if (auto addr = sem_ir_.insts().TryGetAs<SemIR::AddrPattern>(param_id)) {
  236. out_ << "addr ";
  237. param_id = addr->inner_id;
  238. }
  239. FormatInstName(param_id);
  240. out_ << ": ";
  241. FormatType(sem_ir_.insts().Get(param_id).type_id());
  242. }
  243. }
  244. auto FormatCodeBlock(InstBlockId block_id) -> void {
  245. if (block_id.is_valid()) {
  246. FormatCodeBlock(sem_ir_.inst_blocks().Get(block_id));
  247. }
  248. }
  249. auto FormatCodeBlock(llvm::ArrayRef<InstId> block) -> void {
  250. for (const InstId inst_id : block) {
  251. FormatInstruction(inst_id);
  252. }
  253. }
  254. auto FormatTrailingBlock(InstBlockId block_id) -> void {
  255. out_ << ' ';
  256. OpenBrace();
  257. FormatCodeBlock(block_id);
  258. CloseBrace();
  259. }
  260. auto FormatNameScope(NameScopeId id, llvm::StringRef label = "") -> void {
  261. const auto& scope = sem_ir_.name_scopes().Get(id);
  262. if (scope.names.empty() && scope.extended_scopes.empty() &&
  263. !scope.has_error) {
  264. // Name scope is empty.
  265. return;
  266. }
  267. if (!label.empty()) {
  268. IndentLabel();
  269. out_ << label;
  270. }
  271. // Name scopes aren't kept in any particular order. Sort the entries before
  272. // we print them for stability and consistency.
  273. llvm::SmallVector<std::pair<InstId, NameId>> entries;
  274. for (auto [name_id, inst_id] : scope.names) {
  275. entries.push_back({inst_id, name_id});
  276. }
  277. llvm::sort(entries,
  278. [](auto a, auto b) { return a.first.index < b.first.index; });
  279. for (auto [inst_id, name_id] : entries) {
  280. Indent();
  281. out_ << ".";
  282. FormatName(name_id);
  283. out_ << " = ";
  284. FormatInstName(inst_id);
  285. out_ << "\n";
  286. }
  287. for (auto extended_scope_id : scope.extended_scopes) {
  288. // TODO: Print this scope in a better way.
  289. Indent();
  290. out_ << "extend " << extended_scope_id << "\n";
  291. }
  292. if (scope.has_error) {
  293. Indent();
  294. out_ << "has_error\n";
  295. }
  296. }
  297. auto FormatInstruction(InstId inst_id) -> void {
  298. if (!inst_id.is_valid()) {
  299. Indent();
  300. out_ << "invalid\n";
  301. return;
  302. }
  303. FormatInstruction(inst_id, sem_ir_.insts().Get(inst_id));
  304. }
  305. auto FormatInstruction(InstId inst_id, Inst inst) -> void {
  306. CARBON_KIND_SWITCH(inst) {
  307. #define CARBON_SEM_IR_INST_KIND(InstT) \
  308. case CARBON_KIND(InstT typed_inst): { \
  309. FormatInstruction(inst_id, typed_inst); \
  310. break; \
  311. }
  312. #include "toolchain/sem_ir/inst_kind.def"
  313. }
  314. }
  315. template <typename InstT>
  316. auto FormatInstruction(InstId inst_id, InstT inst) -> void {
  317. Indent();
  318. FormatInstructionLHS(inst_id, inst);
  319. out_ << InstT::Kind.ir_name();
  320. pending_constant_value_ = sem_ir_.constant_values().Get(inst_id);
  321. pending_constant_value_is_self_ =
  322. pending_constant_value_.inst_id() == inst_id;
  323. FormatInstructionRHS(inst);
  324. FormatPendingConstantValue(AddSpace::Before);
  325. out_ << "\n";
  326. }
  327. // Don't print a constant for ImportRefUnloaded.
  328. auto FormatInstruction(InstId inst_id, ImportRefUnloaded inst) -> void {
  329. Indent();
  330. FormatInstructionLHS(inst_id, inst);
  331. out_ << ImportRefUnloaded::Kind.ir_name();
  332. FormatInstructionRHS(inst);
  333. out_ << "\n";
  334. }
  335. // If there is a pending constant value attached to the current instruction,
  336. // print it now and clear it out. The constant value gets printed before the
  337. // first braced block argument, or at the end of the instruction if there are
  338. // no such arguments.
  339. auto FormatPendingConstantValue(AddSpace space_where) -> void {
  340. if (pending_constant_value_ == ConstantId::NotConstant) {
  341. return;
  342. }
  343. if (space_where == AddSpace::Before) {
  344. out_ << ' ';
  345. }
  346. out_ << '[';
  347. if (pending_constant_value_.is_valid()) {
  348. out_ << (pending_constant_value_.is_symbolic() ? "symbolic" : "template");
  349. if (!pending_constant_value_is_self_) {
  350. out_ << " = ";
  351. FormatInstName(pending_constant_value_.inst_id());
  352. }
  353. } else {
  354. out_ << pending_constant_value_;
  355. }
  356. out_ << ']';
  357. if (space_where == AddSpace::After) {
  358. out_ << ' ';
  359. }
  360. pending_constant_value_ = ConstantId::NotConstant;
  361. }
  362. auto FormatInstructionLHS(InstId inst_id, Inst inst) -> void {
  363. switch (inst.kind().value_kind()) {
  364. case InstValueKind::Typed:
  365. FormatInstName(inst_id);
  366. out_ << ": ";
  367. switch (GetExprCategory(sem_ir_, inst_id)) {
  368. case ExprCategory::NotExpr:
  369. case ExprCategory::Error:
  370. case ExprCategory::Value:
  371. case ExprCategory::Mixed:
  372. break;
  373. case ExprCategory::DurableRef:
  374. case ExprCategory::EphemeralRef:
  375. out_ << "ref ";
  376. break;
  377. case ExprCategory::Initializing:
  378. out_ << "init ";
  379. break;
  380. }
  381. FormatType(inst.type_id());
  382. out_ << " = ";
  383. break;
  384. case InstValueKind::None:
  385. break;
  386. }
  387. }
  388. // Print ImportRefUnloaded with type-like semantics even though it lacks a
  389. // type_id.
  390. auto FormatInstructionLHS(InstId inst_id, ImportRefUnloaded /*inst*/)
  391. -> void {
  392. FormatInstName(inst_id);
  393. out_ << " = ";
  394. }
  395. template <typename InstT>
  396. auto FormatInstructionRHS(InstT inst) -> void {
  397. // By default, an instruction has a comma-separated argument list.
  398. using Info = Internal::InstLikeTypeInfo<InstT>;
  399. if constexpr (Info::NumArgs == 2) {
  400. FormatArgs(Info::template Get<0>(inst), Info::template Get<1>(inst));
  401. } else if constexpr (Info::NumArgs == 1) {
  402. FormatArgs(Info::template Get<0>(inst));
  403. } else {
  404. FormatArgs();
  405. }
  406. }
  407. auto FormatInstructionRHS(BindSymbolicName inst) -> void {
  408. // A BindSymbolicName with no value is a purely symbolic binding, such as
  409. // the `Self` in an interface. Don't print out `invalid` for the value.
  410. if (inst.value_id.is_valid()) {
  411. FormatArgs(inst.bind_name_id, inst.value_id);
  412. } else {
  413. FormatArgs(inst.bind_name_id);
  414. }
  415. }
  416. auto FormatInstructionRHS(BlockArg inst) -> void {
  417. out_ << " ";
  418. FormatLabel(inst.block_id);
  419. }
  420. auto FormatInstructionRHS(Namespace inst) -> void {
  421. if (inst.import_id.is_valid()) {
  422. FormatArgs(inst.import_id, inst.name_scope_id);
  423. } else {
  424. FormatArgs(inst.name_scope_id);
  425. }
  426. }
  427. auto FormatInstruction(InstId /*inst_id*/, BranchIf inst) -> void {
  428. if (!in_terminator_sequence_) {
  429. Indent();
  430. }
  431. out_ << "if ";
  432. FormatInstName(inst.cond_id);
  433. out_ << " " << Branch::Kind.ir_name() << " ";
  434. FormatLabel(inst.target_id);
  435. out_ << " else ";
  436. in_terminator_sequence_ = true;
  437. }
  438. auto FormatInstruction(InstId /*inst_id*/, BranchWithArg inst) -> void {
  439. if (!in_terminator_sequence_) {
  440. Indent();
  441. }
  442. out_ << BranchWithArg::Kind.ir_name() << " ";
  443. FormatLabel(inst.target_id);
  444. out_ << "(";
  445. FormatInstName(inst.arg_id);
  446. out_ << ")\n";
  447. in_terminator_sequence_ = false;
  448. }
  449. auto FormatInstruction(InstId /*inst_id*/, Branch inst) -> void {
  450. if (!in_terminator_sequence_) {
  451. Indent();
  452. }
  453. out_ << Branch::Kind.ir_name() << " ";
  454. FormatLabel(inst.target_id);
  455. out_ << "\n";
  456. in_terminator_sequence_ = false;
  457. }
  458. auto FormatInstructionRHS(Call inst) -> void {
  459. out_ << " ";
  460. FormatArg(inst.callee_id);
  461. if (!inst.args_id.is_valid()) {
  462. out_ << "(<invalid>)";
  463. return;
  464. }
  465. llvm::ArrayRef<InstId> args = sem_ir_.inst_blocks().Get(inst.args_id);
  466. bool has_return_slot = GetInitRepr(sem_ir_, inst.type_id).has_return_slot();
  467. InstId return_slot_id = InstId::Invalid;
  468. if (has_return_slot) {
  469. return_slot_id = args.back();
  470. args = args.drop_back();
  471. }
  472. llvm::ListSeparator sep;
  473. out_ << '(';
  474. for (auto inst_id : args) {
  475. out_ << sep;
  476. FormatArg(inst_id);
  477. }
  478. out_ << ')';
  479. if (has_return_slot) {
  480. FormatReturnSlot(return_slot_id);
  481. }
  482. }
  483. auto FormatInstructionRHS(ArrayInit inst) -> void {
  484. FormatArgs(inst.inits_id);
  485. FormatReturnSlot(inst.dest_id);
  486. }
  487. auto FormatInstructionRHS(InitializeFrom inst) -> void {
  488. FormatArgs(inst.src_id);
  489. FormatReturnSlot(inst.dest_id);
  490. }
  491. auto FormatInstructionRHS(ReturnExpr ret) -> void {
  492. FormatArgs(ret.expr_id);
  493. if (ret.dest_id.is_valid()) {
  494. FormatReturnSlot(ret.dest_id);
  495. }
  496. }
  497. auto FormatInstructionRHS(StructInit init) -> void {
  498. FormatArgs(init.elements_id);
  499. FormatReturnSlot(init.dest_id);
  500. }
  501. auto FormatInstructionRHS(TupleInit init) -> void {
  502. FormatArgs(init.elements_id);
  503. FormatReturnSlot(init.dest_id);
  504. }
  505. auto FormatInstructionRHS(FunctionDecl inst) -> void {
  506. FormatArgs(inst.function_id);
  507. FormatTrailingBlock(inst.decl_block_id);
  508. }
  509. auto FormatInstructionRHS(ClassDecl inst) -> void {
  510. FormatArgs(inst.class_id);
  511. FormatTrailingBlock(inst.decl_block_id);
  512. }
  513. auto FormatInstructionRHS(ClassType inst) -> void {
  514. if (inst.args_id.is_valid()) {
  515. FormatArgs(inst.class_id, inst.args_id);
  516. } else {
  517. FormatArgs(inst.class_id);
  518. }
  519. }
  520. auto FormatInstructionRHS(ImplDecl inst) -> void {
  521. FormatArgs(inst.impl_id);
  522. FormatTrailingBlock(inst.decl_block_id);
  523. }
  524. auto FormatInstructionRHS(InterfaceDecl inst) -> void {
  525. FormatArgs(inst.interface_id);
  526. FormatTrailingBlock(inst.decl_block_id);
  527. }
  528. auto FormatInstructionRHS(IntLiteral inst) -> void {
  529. out_ << " ";
  530. sem_ir_.ints()
  531. .Get(inst.int_id)
  532. .print(out_, sem_ir_.types().IsSignedInt(inst.type_id));
  533. }
  534. auto FormatInstructionRHS(FloatLiteral inst) -> void {
  535. llvm::SmallVector<char, 16> buffer;
  536. sem_ir_.floats().Get(inst.float_id).toString(buffer);
  537. out_ << " " << buffer;
  538. }
  539. auto FormatInstructionRHS(ImportRefUnloaded inst) -> void {
  540. FormatArgs(inst.import_ir_inst_id);
  541. out_ << ", unloaded";
  542. }
  543. auto FormatInstructionRHS(ImportRefLoaded inst) -> void {
  544. FormatArgs(inst.import_ir_inst_id);
  545. out_ << ", loaded";
  546. }
  547. auto FormatInstructionRHS(SpliceBlock inst) -> void {
  548. FormatArgs(inst.result_id);
  549. FormatTrailingBlock(inst.block_id);
  550. }
  551. // StructTypeFields are formatted as part of their StructType.
  552. auto FormatInstruction(InstId /*inst_id*/, StructTypeField /*inst*/) -> void {
  553. }
  554. auto FormatInstructionRHS(StructType inst) -> void {
  555. out_ << " {";
  556. llvm::ListSeparator sep;
  557. for (auto field_id : sem_ir_.inst_blocks().Get(inst.fields_id)) {
  558. out_ << sep << ".";
  559. auto field = sem_ir_.insts().GetAs<StructTypeField>(field_id);
  560. FormatName(field.name_id);
  561. out_ << ": ";
  562. FormatType(field.field_type_id);
  563. }
  564. out_ << "}";
  565. }
  566. auto FormatArgs() -> void {}
  567. template <typename... Args>
  568. auto FormatArgs(Args... args) -> void {
  569. out_ << ' ';
  570. llvm::ListSeparator sep;
  571. ((out_ << sep, FormatArg(args)), ...);
  572. }
  573. auto FormatArg(BoolValue v) -> void { out_ << v; }
  574. auto FormatArg(BuiltinKind kind) -> void { out_ << kind.label(); }
  575. auto FormatArg(BindNameId id) -> void {
  576. const auto& info = sem_ir_.bind_names().Get(id);
  577. FormatName(info.name_id);
  578. if (info.bind_index.is_valid()) {
  579. out_ << " " << info.bind_index.index;
  580. }
  581. }
  582. auto FormatArg(FunctionId id) -> void { FormatFunctionName(id); }
  583. auto FormatArg(ClassId id) -> void { FormatClassName(id); }
  584. auto FormatArg(InterfaceId id) -> void { FormatInterfaceName(id); }
  585. auto FormatArg(IntKind k) -> void { k.Print(out_); }
  586. auto FormatArg(FloatKind k) -> void { k.Print(out_); }
  587. auto FormatArg(ImplId id) -> void { FormatImplName(id); }
  588. auto FormatArg(ImportIRId id) -> void { out_ << id; }
  589. auto FormatArg(ImportIRInstId id) -> void {
  590. // Don't format the inst_id because it refers to a different IR.
  591. // TODO: Consider a better way to format the InstID from other IRs.
  592. auto import_ir_inst = sem_ir_.import_ir_insts().Get(id);
  593. out_ << import_ir_inst.ir_id << ", " << import_ir_inst.inst_id;
  594. }
  595. auto FormatArg(IntId id) -> void {
  596. // We don't know the signedness to use here. Default to unsigned.
  597. sem_ir_.ints().Get(id).print(out_, /*isSigned=*/false);
  598. }
  599. auto FormatArg(LocId id) -> void {
  600. if (id.is_import_ir_inst_id()) {
  601. out_ << "{";
  602. FormatArg(id.import_ir_inst_id());
  603. out_ << "}";
  604. } else {
  605. // TODO: For a NodeId, this prints the index of the node. Do we want it to
  606. // print a line number or something in order to make it less dependent on
  607. // parse?
  608. out_ << id;
  609. }
  610. }
  611. auto FormatArg(ElementIndex index) -> void { out_ << index; }
  612. auto FormatArg(NameScopeId id) -> void {
  613. OpenBrace();
  614. FormatNameScope(id);
  615. CloseBrace();
  616. }
  617. auto FormatArg(InstId id) -> void { FormatInstName(id); }
  618. auto FormatArg(InstBlockId id) -> void {
  619. if (!id.is_valid()) {
  620. out_ << "invalid";
  621. return;
  622. }
  623. out_ << '(';
  624. llvm::ListSeparator sep;
  625. for (auto inst_id : sem_ir_.inst_blocks().Get(id)) {
  626. out_ << sep;
  627. FormatArg(inst_id);
  628. }
  629. out_ << ')';
  630. }
  631. auto FormatArg(RealId id) -> void {
  632. // TODO: Format with a `.` when the exponent is near zero.
  633. const auto& real = sem_ir_.reals().Get(id);
  634. real.mantissa.print(out_, /*isSigned=*/false);
  635. out_ << (real.is_decimal ? 'e' : 'p') << real.exponent;
  636. }
  637. auto FormatArg(StringLiteralValueId id) -> void {
  638. out_ << '"';
  639. out_.write_escaped(sem_ir_.string_literal_values().Get(id),
  640. /*UseHexEscapes=*/true);
  641. out_ << '"';
  642. }
  643. auto FormatArg(NameId id) -> void { FormatName(id); }
  644. auto FormatArg(TypeId id) -> void { FormatType(id); }
  645. auto FormatArg(TypeBlockId id) -> void {
  646. out_ << '(';
  647. llvm::ListSeparator sep;
  648. for (auto type_id : sem_ir_.type_blocks().Get(id)) {
  649. out_ << sep;
  650. FormatArg(type_id);
  651. }
  652. out_ << ')';
  653. }
  654. auto FormatReturnSlot(InstId dest_id) -> void {
  655. out_ << " to ";
  656. FormatArg(dest_id);
  657. }
  658. auto FormatName(NameId id) -> void {
  659. out_ << sem_ir_.names().GetFormatted(id);
  660. }
  661. auto FormatInstName(InstId id) -> void {
  662. out_ << inst_namer_.GetNameFor(scope_, id);
  663. }
  664. auto FormatLabel(InstBlockId id) -> void {
  665. out_ << inst_namer_.GetLabelFor(scope_, id);
  666. }
  667. auto FormatFunctionName(FunctionId id) -> void {
  668. out_ << inst_namer_.GetNameFor(id);
  669. }
  670. auto FormatClassName(ClassId id) -> void {
  671. out_ << inst_namer_.GetNameFor(id);
  672. }
  673. auto FormatInterfaceName(InterfaceId id) -> void {
  674. out_ << inst_namer_.GetNameFor(id);
  675. }
  676. auto FormatImplName(ImplId id) -> void { out_ << inst_namer_.GetNameFor(id); }
  677. auto FormatType(TypeId id) -> void {
  678. if (!id.is_valid()) {
  679. out_ << "invalid";
  680. } else {
  681. out_ << sem_ir_.StringifyType(id);
  682. }
  683. }
  684. private:
  685. const File& sem_ir_;
  686. llvm::raw_ostream& out_;
  687. InstNamer inst_namer_;
  688. // The current scope that we are formatting within. References to names in
  689. // this scope will not have a `@scope.` prefix added.
  690. InstNamer::ScopeId scope_ = InstNamer::ScopeId::None;
  691. // Whether we are formatting in a terminator sequence, that is, a sequence of
  692. // branches at the end of a block. The entirety of a terminator sequence is
  693. // formatted on a single line, despite being multiple instructions.
  694. bool in_terminator_sequence_ = false;
  695. // The indent depth to use for new instructions.
  696. int indent_ = 0;
  697. // Whether we are currently formatting immediately after an open brace. If so,
  698. // a newline will be inserted before the next line indent.
  699. bool after_open_brace_ = false;
  700. // The constant value of the current instruction, if it has one that has not
  701. // yet been printed. The value `NotConstant` is used as a sentinel to indicate
  702. // there is nothing to print.
  703. ConstantId pending_constant_value_ = ConstantId::NotConstant;
  704. // Whether `pending_constant_value_`'s instruction is the same as the
  705. // instruction currently being printed. If true, only the phase of the
  706. // constant is printed, and the value is omitted.
  707. bool pending_constant_value_is_self_ = false;
  708. };
  709. auto FormatFile(const Lex::TokenizedBuffer& tokenized_buffer,
  710. const Parse::Tree& parse_tree, const File& sem_ir,
  711. llvm::raw_ostream& out) -> void {
  712. Formatter(tokenized_buffer, parse_tree, sem_ir, out).Format();
  713. }
  714. } // namespace Carbon::SemIR