semantics_ir_formatter.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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/semantics/semantics_ir_formatter.h"
  5. #include "llvm/ADT/Sequence.h"
  6. #include "llvm/ADT/StringExtras.h"
  7. #include "llvm/Support/SaveAndRestore.h"
  8. #include "toolchain/lexer/tokenized_buffer.h"
  9. #include "toolchain/parser/parse_tree.h"
  10. namespace Carbon::SemIR {
  11. namespace {
  12. // Assigns names to nodes, blocks, and scopes in the Semantics IR.
  13. //
  14. // TODOs / future work ideas:
  15. // - Add a documentation file for the textual format and link to the
  16. // naming section here.
  17. // - Consider representing literals as just `literal` in the IR and using the
  18. // type to distinguish.
  19. class NodeNamer {
  20. public:
  21. // int32_t matches the input value size.
  22. // NOLINTNEXTLINE(performance-enum-size)
  23. enum class ScopeIndex : int32_t {
  24. None = -1,
  25. Package = 0,
  26. };
  27. static_assert(sizeof(ScopeIndex) == sizeof(FunctionId));
  28. NodeNamer(const TokenizedBuffer& tokenized_buffer,
  29. const ParseTree& parse_tree, const File& semantics_ir)
  30. : tokenized_buffer_(tokenized_buffer),
  31. parse_tree_(parse_tree),
  32. semantics_ir_(semantics_ir) {
  33. nodes.resize(semantics_ir.nodes_size());
  34. labels.resize(semantics_ir.node_blocks_size());
  35. scopes.resize(1 + semantics_ir.functions_size());
  36. // Build the package scope.
  37. GetScopeInfo(ScopeIndex::Package).name =
  38. globals.AddNameUnchecked("package");
  39. CollectNamesInBlock(ScopeIndex::Package, semantics_ir.top_node_block_id());
  40. // Build each function scope.
  41. for (int i : llvm::seq(semantics_ir.functions_size())) {
  42. auto fn_id = FunctionId(i);
  43. auto fn_scope = GetScopeFor(fn_id);
  44. const auto& fn = semantics_ir.GetFunction(fn_id);
  45. // TODO: Provide a location for the function for use as a
  46. // disambiguator.
  47. auto fn_loc = ParseTree::Node::Invalid;
  48. GetScopeInfo(fn_scope).name = globals.AllocateName(
  49. *this, fn_loc,
  50. fn.name_id.is_valid() ? semantics_ir.GetString(fn.name_id).str()
  51. : "");
  52. CollectNamesInBlock(fn_scope, fn.param_refs_id);
  53. if (!fn.body_block_ids.empty()) {
  54. AddBlockLabel(fn_scope, fn.body_block_ids.front(), "entry", fn_loc);
  55. }
  56. for (auto block_id : fn.body_block_ids) {
  57. CollectNamesInBlock(fn_scope, block_id);
  58. }
  59. for (auto block_id : fn.body_block_ids) {
  60. AddBlockLabel(fn_scope, block_id);
  61. }
  62. }
  63. }
  64. // Returns the scope index corresponding to a function.
  65. auto GetScopeFor(FunctionId fn_id) -> ScopeIndex {
  66. return static_cast<ScopeIndex>(fn_id.index + 1);
  67. }
  68. // Returns the IR name to use for a function.
  69. auto GetNameFor(FunctionId fn_id) -> llvm::StringRef {
  70. if (!fn_id.is_valid()) {
  71. return "invalid";
  72. }
  73. return GetScopeInfo(GetScopeFor(fn_id)).name.str();
  74. }
  75. // Returns the IR name to use for a node, when referenced from a given scope.
  76. auto GetNameFor(ScopeIndex scope_idx, NodeId node_id) -> std::string {
  77. if (!node_id.is_valid()) {
  78. return "invalid";
  79. }
  80. // Check for a builtin.
  81. if (node_id.index < BuiltinKind::ValidCount) {
  82. return BuiltinKind::FromInt(node_id.index).label().str();
  83. }
  84. auto& [node_scope, node_name] = nodes[node_id.index];
  85. if (!node_name) {
  86. // This should not happen in valid IR.
  87. return "<unexpected noderef " + llvm::itostr(node_id.index) + ">";
  88. }
  89. if (node_scope == scope_idx) {
  90. return node_name.str().str();
  91. }
  92. return (GetScopeInfo(node_scope).name.str() + "." + node_name.str()).str();
  93. }
  94. // Returns the IR name to use for a label, when referenced from a given scope.
  95. auto GetLabelFor(ScopeIndex scope_idx, NodeBlockId block_id) -> std::string {
  96. if (!block_id.is_valid()) {
  97. return "!invalid";
  98. }
  99. auto& [label_scope, label_name] = labels[block_id.index];
  100. if (!label_name) {
  101. // This should not happen in valid IR.
  102. return "<unexpected nodeblockref " + llvm::itostr(block_id.index) + ">";
  103. }
  104. if (label_scope == scope_idx) {
  105. return label_name.str().str();
  106. }
  107. return (GetScopeInfo(label_scope).name.str() + "." + label_name.str())
  108. .str();
  109. }
  110. private:
  111. // A space in which unique names can be allocated.
  112. struct Namespace {
  113. // A result of a name lookup.
  114. struct NameResult;
  115. // A name in a namespace, which might be redirected to refer to another name
  116. // for disambiguation purposes.
  117. class Name {
  118. public:
  119. Name() : value_(nullptr) {}
  120. explicit Name(llvm::StringMapIterator<NameResult> it) : value_(&*it) {}
  121. explicit operator bool() const { return value_; }
  122. auto str() const -> llvm::StringRef {
  123. llvm::StringMapEntry<NameResult>* value = value_;
  124. CARBON_CHECK(value) << "cannot print a null name";
  125. while (value->second.ambiguous && value->second.fallback) {
  126. value = value->second.fallback.value_;
  127. }
  128. return value->first();
  129. }
  130. auto SetFallback(Name name) -> void { value_->second.fallback = name; }
  131. auto SetAmbiguous() -> void { value_->second.ambiguous = true; }
  132. private:
  133. llvm::StringMapEntry<NameResult>* value_;
  134. };
  135. struct NameResult {
  136. bool ambiguous = false;
  137. Name fallback = Name();
  138. };
  139. llvm::StringRef prefix;
  140. llvm::StringMap<NameResult> allocated = {};
  141. int unnamed_count = 0;
  142. auto AddNameUnchecked(llvm::StringRef name) -> Name {
  143. return Name(allocated.insert({name, NameResult()}).first);
  144. }
  145. auto AllocateName(const NodeNamer& namer, ParseTree::Node node,
  146. std::string name = "") -> Name {
  147. // The best (shortest) name for this node so far, and the current name
  148. // for it.
  149. Name best;
  150. Name current;
  151. // Add `name` as a name for this entity.
  152. auto add_name = [&](bool mark_ambiguous = true) {
  153. auto [it, added] = allocated.insert({name, NameResult()});
  154. Name new_name = Name(it);
  155. if (!added) {
  156. if (mark_ambiguous) {
  157. // This name was allocated for a different node. Mark it as
  158. // ambiguous and keep looking for a name for this node.
  159. new_name.SetAmbiguous();
  160. }
  161. } else {
  162. if (!best) {
  163. best = new_name;
  164. } else {
  165. CARBON_CHECK(current);
  166. current.SetFallback(new_name);
  167. }
  168. current = new_name;
  169. }
  170. return added;
  171. };
  172. // All names start with the prefix.
  173. name.insert(0, prefix);
  174. // Use the given name if it's available and not just the prefix.
  175. if (name.size() > prefix.size()) {
  176. add_name();
  177. }
  178. // Append location information to try to disambiguate.
  179. if (node.is_valid()) {
  180. auto token = namer.parse_tree_.node_token(node);
  181. llvm::raw_string_ostream(name)
  182. << ".loc" << namer.tokenized_buffer_.GetLineNumber(token);
  183. add_name();
  184. llvm::raw_string_ostream(name)
  185. << "_" << namer.tokenized_buffer_.GetColumnNumber(token);
  186. add_name();
  187. }
  188. // Append numbers until we find an available name.
  189. name += ".";
  190. auto name_size_without_counter = name.size();
  191. for (int counter = 1;; ++counter) {
  192. name.resize(name_size_without_counter);
  193. llvm::raw_string_ostream(name) << counter;
  194. if (add_name(/*mark_ambiguous=*/false)) {
  195. return best;
  196. }
  197. }
  198. }
  199. };
  200. // A named scope that contains named entities.
  201. struct Scope {
  202. Namespace::Name name;
  203. Namespace nodes = {.prefix = "%"};
  204. Namespace labels = {.prefix = "!"};
  205. };
  206. auto GetScopeInfo(ScopeIndex scope_idx) -> Scope& {
  207. return scopes[static_cast<int>(scope_idx)];
  208. }
  209. auto AddBlockLabel(ScopeIndex scope_idx, NodeBlockId block_id,
  210. std::string name = "",
  211. ParseTree::Node parse_node = ParseTree::Node::Invalid)
  212. -> void {
  213. if (!block_id.is_valid() || labels[block_id.index].second) {
  214. return;
  215. }
  216. if (parse_node == ParseTree::Node::Invalid) {
  217. if (const auto& block = semantics_ir_.GetNodeBlock(block_id);
  218. !block.empty()) {
  219. parse_node = semantics_ir_.GetNode(block.front()).parse_node();
  220. }
  221. }
  222. labels[block_id.index] = {scope_idx,
  223. GetScopeInfo(scope_idx).labels.AllocateName(
  224. *this, parse_node, std::move(name))};
  225. }
  226. // Finds and adds a suitable block label for the given semantics node that
  227. // represents some kind of branch.
  228. auto AddBlockLabel(ScopeIndex scope_idx, NodeBlockId block_id, Node node)
  229. -> void {
  230. llvm::StringRef name;
  231. switch (parse_tree_.node_kind(node.parse_node())) {
  232. case ParseNodeKind::IfExpressionIf:
  233. switch (node.kind()) {
  234. case NodeKind::BranchIf:
  235. name = "if.expr.then";
  236. break;
  237. case NodeKind::Branch:
  238. name = "if.expr.else";
  239. break;
  240. case NodeKind::BranchWithArg:
  241. name = "if.expr.result";
  242. break;
  243. default:
  244. break;
  245. }
  246. break;
  247. case ParseNodeKind::IfCondition:
  248. switch (node.kind()) {
  249. case NodeKind::BranchIf:
  250. name = "if.then";
  251. break;
  252. case NodeKind::Branch:
  253. name = "if.else";
  254. break;
  255. default:
  256. break;
  257. }
  258. break;
  259. case ParseNodeKind::IfStatement:
  260. name = "if.done";
  261. break;
  262. case ParseNodeKind::ShortCircuitOperand: {
  263. bool is_rhs = node.kind() == NodeKind::BranchIf;
  264. bool is_and = tokenized_buffer_.GetKind(parse_tree_.node_token(
  265. node.parse_node())) == TokenKind::And;
  266. name = is_and ? (is_rhs ? "and.rhs" : "and.result")
  267. : (is_rhs ? "or.rhs" : "or.result");
  268. break;
  269. }
  270. default:
  271. break;
  272. }
  273. AddBlockLabel(scope_idx, block_id, name.str(), node.parse_node());
  274. }
  275. auto CollectNamesInBlock(ScopeIndex scope_idx, NodeBlockId block_id) -> void {
  276. if (!block_id.is_valid()) {
  277. return;
  278. }
  279. Scope& scope = GetScopeInfo(scope_idx);
  280. // Use bound names where available. Otherwise, assign a backup name.
  281. for (auto node_id : semantics_ir_.GetNodeBlock(block_id)) {
  282. auto node = semantics_ir_.GetNode(node_id);
  283. switch (node.kind()) {
  284. case NodeKind::Branch: {
  285. auto dest_id = node.GetAsBranch();
  286. AddBlockLabel(scope_idx, dest_id, node);
  287. break;
  288. }
  289. case NodeKind::BranchIf: {
  290. auto [dest_id, cond_id] = node.GetAsBranchIf();
  291. AddBlockLabel(scope_idx, dest_id, node);
  292. break;
  293. }
  294. case NodeKind::BranchWithArg: {
  295. auto [dest_id, arg_id] = node.GetAsBranchWithArg();
  296. AddBlockLabel(scope_idx, dest_id, node);
  297. break;
  298. }
  299. case NodeKind::Parameter: {
  300. auto name_id = node.GetAsParameter();
  301. nodes[node_id.index] = {
  302. scope_idx,
  303. scope.nodes.AllocateName(*this, node.parse_node(),
  304. semantics_ir_.GetString(name_id).str())};
  305. break;
  306. }
  307. case NodeKind::VarStorage: {
  308. // TODO: Eventually this name will be optional, and we'll want to
  309. // provide something like `var` as a default. However, that's not
  310. // possible right now so cannot be tested.
  311. auto name_id = node.GetAsVarStorage();
  312. nodes[node_id.index] = {
  313. scope_idx,
  314. scope.nodes.AllocateName(*this, node.parse_node(),
  315. semantics_ir_.GetString(name_id).str())};
  316. break;
  317. }
  318. default: {
  319. // Sequentially number all remaining values.
  320. if (node.kind().value_kind() != NodeValueKind::None) {
  321. nodes[node_id.index] = {
  322. scope_idx, scope.nodes.AllocateName(*this, node.parse_node())};
  323. }
  324. break;
  325. }
  326. }
  327. }
  328. }
  329. const TokenizedBuffer& tokenized_buffer_;
  330. const ParseTree& parse_tree_;
  331. const File& semantics_ir_;
  332. Namespace globals = {.prefix = "@"};
  333. std::vector<std::pair<ScopeIndex, Namespace::Name>> nodes;
  334. std::vector<std::pair<ScopeIndex, Namespace::Name>> labels;
  335. std::vector<Scope> scopes;
  336. };
  337. } // namespace
  338. // Formatter for printing textual Semantics IR.
  339. class Formatter {
  340. public:
  341. explicit Formatter(const TokenizedBuffer& tokenized_buffer,
  342. const ParseTree& parse_tree, const File& semantics_ir,
  343. llvm::raw_ostream& out)
  344. : semantics_ir_(semantics_ir),
  345. out_(out),
  346. node_namer_(tokenized_buffer, parse_tree, semantics_ir) {}
  347. auto Format() -> void {
  348. // TODO: Include information from the package declaration, once we fully
  349. // support it.
  350. out_ << "package {\n";
  351. // TODO: Handle the case where there are multiple top-level node blocks.
  352. // For example, there may be branching in the initializer of a global or a
  353. // type expression.
  354. if (auto block_id = semantics_ir_.top_node_block_id();
  355. block_id.is_valid()) {
  356. llvm::SaveAndRestore package_scope(scope_,
  357. NodeNamer::ScopeIndex::Package);
  358. FormatCodeBlock(block_id);
  359. }
  360. out_ << "}\n";
  361. for (int i : llvm::seq(semantics_ir_.functions_size())) {
  362. FormatFunction(FunctionId(i));
  363. }
  364. }
  365. auto FormatFunction(FunctionId id) -> void {
  366. const Function& fn = semantics_ir_.GetFunction(id);
  367. out_ << "\nfn ";
  368. FormatFunctionName(id);
  369. out_ << "(";
  370. llvm::SaveAndRestore function_scope(scope_, node_namer_.GetScopeFor(id));
  371. llvm::ListSeparator sep;
  372. for (const NodeId param_id : semantics_ir_.GetNodeBlock(fn.param_refs_id)) {
  373. out_ << sep;
  374. FormatNodeName(param_id);
  375. out_ << ": ";
  376. FormatType(semantics_ir_.GetNode(param_id).type_id());
  377. }
  378. out_ << ")";
  379. if (fn.return_type_id.is_valid()) {
  380. out_ << " -> ";
  381. FormatType(fn.return_type_id);
  382. }
  383. if (!fn.body_block_ids.empty()) {
  384. out_ << " {";
  385. for (auto block_id : fn.body_block_ids) {
  386. out_ << "\n";
  387. FormatLabel(block_id);
  388. out_ << ":\n";
  389. FormatCodeBlock(block_id);
  390. }
  391. out_ << "}\n";
  392. } else {
  393. out_ << ";\n";
  394. }
  395. }
  396. auto FormatCodeBlock(NodeBlockId block_id) -> void {
  397. if (!block_id.is_valid()) {
  398. return;
  399. }
  400. for (const NodeId node_id : semantics_ir_.GetNodeBlock(block_id)) {
  401. FormatInstruction(node_id);
  402. }
  403. }
  404. auto FormatInstruction(NodeId node_id) -> void {
  405. if (!node_id.is_valid()) {
  406. out_ << " " << NodeKind::Invalid.ir_name() << "\n";
  407. return;
  408. }
  409. FormatInstruction(node_id, semantics_ir_.GetNode(node_id));
  410. }
  411. auto FormatInstruction(NodeId node_id, Node node) -> void {
  412. switch (node.kind()) {
  413. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  414. case NodeKind::Name: \
  415. FormatInstruction<Node::Name>(node_id, node); \
  416. break;
  417. #include "toolchain/semantics/semantics_node_kind.def"
  418. }
  419. }
  420. template <typename Kind>
  421. auto FormatInstruction(NodeId node_id, Node node) -> void {
  422. out_ << " ";
  423. FormatInstructionLHS(node_id, node);
  424. out_ << node.kind().ir_name();
  425. FormatInstructionRHS<Kind>(node);
  426. out_ << "\n";
  427. }
  428. auto FormatInstructionLHS(NodeId node_id, Node node) -> void {
  429. switch (node.kind().value_kind()) {
  430. case NodeValueKind::Typed:
  431. FormatNodeName(node_id);
  432. out_ << ": ";
  433. FormatType(node.type_id());
  434. out_ << " = ";
  435. break;
  436. case NodeValueKind::Untyped:
  437. FormatNodeName(node_id);
  438. out_ << " = ";
  439. break;
  440. case NodeValueKind::None:
  441. break;
  442. }
  443. }
  444. template <typename Kind>
  445. auto FormatInstructionRHS(Node node) -> void {
  446. // By default, an instruction has a comma-separated argument list.
  447. FormatArgs(Kind::Get(node));
  448. }
  449. template <>
  450. auto FormatInstructionRHS<Node::BlockArg>(Node node) -> void {
  451. out_ << " ";
  452. FormatLabel(node.GetAsBlockArg());
  453. }
  454. template <>
  455. auto FormatInstruction<Node::BranchIf>(NodeId /*node_id*/, Node node)
  456. -> void {
  457. if (!in_terminator_sequence) {
  458. out_ << " ";
  459. }
  460. auto [label_id, cond_id] = node.GetAsBranchIf();
  461. out_ << "if ";
  462. FormatNodeName(cond_id);
  463. out_ << " " << NodeKind::Branch.ir_name() << " ";
  464. FormatLabel(label_id);
  465. out_ << " else ";
  466. in_terminator_sequence = true;
  467. }
  468. template <>
  469. auto FormatInstruction<Node::BranchWithArg>(NodeId /*node_id*/, Node node)
  470. -> void {
  471. if (!in_terminator_sequence) {
  472. out_ << " ";
  473. }
  474. auto [label_id, arg_id] = node.GetAsBranchWithArg();
  475. out_ << NodeKind::BranchWithArg.ir_name() << " ";
  476. FormatLabel(label_id);
  477. out_ << "(";
  478. FormatNodeName(arg_id);
  479. out_ << ")\n";
  480. in_terminator_sequence = false;
  481. }
  482. template <>
  483. auto FormatInstruction<Node::Branch>(NodeId /*node_id*/, Node node) -> void {
  484. if (!in_terminator_sequence) {
  485. out_ << " ";
  486. }
  487. out_ << NodeKind::Branch.ir_name() << " ";
  488. FormatLabel(node.GetAsBranch());
  489. out_ << "\n";
  490. in_terminator_sequence = false;
  491. }
  492. template <>
  493. auto FormatInstructionRHS<Node::Call>(Node node) -> void {
  494. out_ << " ";
  495. auto [args_id, callee_id] = node.GetAsCall();
  496. FormatArg(callee_id);
  497. FormatArg(args_id);
  498. }
  499. template <>
  500. auto FormatInstructionRHS<Node::CrossReference>(Node node) -> void {
  501. // TODO: Figure out a way to make this meaningful. We'll need some way to
  502. // name cross-reference IRs, perhaps by the node ID of the import?
  503. auto [xref_id, node_id] = node.GetAsCrossReference();
  504. out_ << " " << xref_id << "." << node_id;
  505. }
  506. // StructTypeFields are formatted as part of their StructType.
  507. template <>
  508. auto FormatInstruction<Node::StructTypeField>(NodeId /*node_id*/,
  509. Node /*node*/) -> void {}
  510. template <>
  511. auto FormatInstructionRHS<Node::StructType>(Node node) -> void {
  512. out_ << " {";
  513. llvm::ListSeparator sep;
  514. for (auto field_id : semantics_ir_.GetNodeBlock(node.GetAsStructType())) {
  515. out_ << sep << ".";
  516. auto [field_name_id, field_type_id] =
  517. semantics_ir_.GetNode(field_id).GetAsStructTypeField();
  518. FormatString(field_name_id);
  519. out_ << ": ";
  520. FormatType(field_type_id);
  521. }
  522. out_ << "}";
  523. }
  524. auto FormatArgs(Node::NoArgs /*unused*/) -> void {}
  525. template <typename Arg1>
  526. auto FormatArgs(Arg1 arg) -> void {
  527. out_ << ' ';
  528. FormatArg(arg);
  529. }
  530. template <typename Arg1, typename Arg2>
  531. auto FormatArgs(std::pair<Arg1, Arg2> args) -> void {
  532. out_ << ' ';
  533. FormatArg(args.first);
  534. out_ << ",";
  535. FormatArgs(args.second);
  536. }
  537. auto FormatArg(BoolValue v) -> void { out_ << v; }
  538. auto FormatArg(BuiltinKind kind) -> void { out_ << kind.label(); }
  539. auto FormatArg(FunctionId id) -> void { FormatFunctionName(id); }
  540. auto FormatArg(IntegerLiteralId id) -> void {
  541. out_ << semantics_ir_.GetIntegerLiteral(id);
  542. }
  543. auto FormatArg(MemberIndex index) -> void { out_ << index; }
  544. // TODO: Should we be printing scopes inline, or should we have a separate
  545. // step to print them like we do for functions?
  546. auto FormatArg(NameScopeId id) -> void {
  547. // Name scopes aren't kept in any particular order. Sort the entries before
  548. // we print them for stability and consistency.
  549. std::vector<std::pair<NodeId, StringId>> entries;
  550. for (auto [name_id, node_id] : semantics_ir_.GetNameScope(id)) {
  551. entries.push_back({node_id, name_id});
  552. }
  553. llvm::sort(entries,
  554. [](auto a, auto b) { return a.first.index < b.first.index; });
  555. out_ << '{';
  556. llvm::ListSeparator sep;
  557. for (auto [node_id, name_id] : entries) {
  558. out_ << sep << ".";
  559. FormatString(name_id);
  560. out_ << " = ";
  561. FormatNodeName(node_id);
  562. }
  563. out_ << '}';
  564. }
  565. auto FormatArg(NodeId id) -> void { FormatNodeName(id); }
  566. auto FormatArg(NodeBlockId id) -> void {
  567. out_ << '(';
  568. llvm::ListSeparator sep;
  569. for (auto node_id : semantics_ir_.GetNodeBlock(id)) {
  570. out_ << sep;
  571. FormatArg(node_id);
  572. }
  573. out_ << ')';
  574. }
  575. auto FormatArg(RealLiteralId id) -> void {
  576. // TODO: Format with a `.` when the exponent is near zero.
  577. const auto& real = semantics_ir_.GetRealLiteral(id);
  578. out_ << real.mantissa << (real.is_decimal ? 'e' : 'p') << real.exponent;
  579. }
  580. auto FormatArg(StringId id) -> void {
  581. out_ << '"';
  582. out_.write_escaped(semantics_ir_.GetString(id), /*UseHexEscapes=*/true);
  583. out_ << '"';
  584. }
  585. auto FormatArg(TypeId id) -> void { FormatType(id); }
  586. auto FormatArg(TypeBlockId id) -> void {
  587. out_ << '(';
  588. llvm::ListSeparator sep;
  589. for (auto type_id : semantics_ir_.GetTypeBlock(id)) {
  590. out_ << sep;
  591. FormatArg(type_id);
  592. }
  593. out_ << ')';
  594. }
  595. auto FormatNodeName(NodeId id) -> void {
  596. out_ << node_namer_.GetNameFor(scope_, id);
  597. }
  598. auto FormatLabel(NodeBlockId id) -> void {
  599. out_ << node_namer_.GetLabelFor(scope_, id);
  600. }
  601. auto FormatString(StringId id) -> void {
  602. out_ << semantics_ir_.GetString(id);
  603. }
  604. auto FormatFunctionName(FunctionId id) -> void {
  605. out_ << node_namer_.GetNameFor(id);
  606. }
  607. auto FormatType(TypeId id) -> void {
  608. if (!id.is_valid()) {
  609. out_ << "invalid";
  610. } else {
  611. out_ << semantics_ir_.StringifyType(id, /*in_type_context=*/true);
  612. }
  613. }
  614. private:
  615. const File& semantics_ir_;
  616. llvm::raw_ostream& out_;
  617. NodeNamer node_namer_;
  618. NodeNamer::ScopeIndex scope_ = NodeNamer::ScopeIndex::None;
  619. bool in_terminator_sequence = false;
  620. };
  621. auto FormatFile(const TokenizedBuffer& tokenized_buffer,
  622. const ParseTree& parse_tree, const File& semantics_ir,
  623. llvm::raw_ostream& out) -> void {
  624. Formatter(tokenized_buffer, parse_tree, semantics_ir, out).Format();
  625. }
  626. } // namespace Carbon::SemIR