formatter.cpp 24 KB

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