formatter.cpp 25 KB

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