semantics_ir_formatter.cpp 22 KB

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