semantics_ir_formatter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. The BindName node appears after the node
  284. // that it's giving a name to, so we need to do this before assigning
  285. // fallback names.
  286. for (auto node_id : semantics_ir_.GetNodeBlock(block_id)) {
  287. auto node = semantics_ir_.GetNode(node_id);
  288. switch (node.kind()) {
  289. case SemanticsNodeKind::BindName: {
  290. auto [name_id, named_node_id] = node.GetAsBindName();
  291. nodes[named_node_id.index] = {
  292. scope_idx,
  293. scope.nodes.AllocateName(*this, node.parse_node(),
  294. semantics_ir_.GetString(name_id).str())};
  295. break;
  296. }
  297. case SemanticsNodeKind::Branch: {
  298. auto dest_id = node.GetAsBranch();
  299. AddBlockLabel(scope_idx, dest_id, node);
  300. break;
  301. }
  302. case SemanticsNodeKind::BranchIf: {
  303. auto [dest_id, cond_id] = node.GetAsBranchIf();
  304. AddBlockLabel(scope_idx, dest_id, node);
  305. break;
  306. }
  307. case SemanticsNodeKind::BranchWithArg: {
  308. auto [dest_id, arg_id] = node.GetAsBranchWithArg();
  309. AddBlockLabel(scope_idx, dest_id, node);
  310. break;
  311. }
  312. default:
  313. break;
  314. }
  315. }
  316. // Sequentially number all remaining values.
  317. for (auto node_id : semantics_ir_.GetNodeBlock(block_id)) {
  318. auto node = semantics_ir_.GetNode(node_id);
  319. if (node.kind() != SemanticsNodeKind::BindName &&
  320. node.kind().value_kind() != SemanticsNodeValueKind::None) {
  321. auto& name = nodes[node_id.index];
  322. if (!name.second) {
  323. name = {scope_idx,
  324. scope.nodes.AllocateName(*this, node.parse_node())};
  325. }
  326. }
  327. }
  328. }
  329. const TokenizedBuffer& tokenized_buffer_;
  330. const ParseTree& parse_tree_;
  331. const SemanticsIR& 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 SemanticsIRFormatter {
  340. public:
  341. explicit SemanticsIRFormatter(const TokenizedBuffer& tokenized_buffer,
  342. const ParseTree& parse_tree,
  343. const SemanticsIR& semantics_ir,
  344. llvm::raw_ostream& out)
  345. : semantics_ir_(semantics_ir),
  346. out_(out),
  347. node_namer_(tokenized_buffer, parse_tree, semantics_ir) {}
  348. auto Format() -> void {
  349. // TODO: Include information from the package declaration, once we fully
  350. // support it.
  351. out_ << "package {\n";
  352. // TODO: Handle the case where there are multiple top-level node blocks.
  353. // For example, there may be branching in the initializer of a global or a
  354. // type expression.
  355. if (auto block_id = semantics_ir_.top_node_block_id();
  356. block_id.is_valid()) {
  357. llvm::SaveAndRestore package_scope(scope_,
  358. NodeNamer::ScopeIndex::Package);
  359. FormatCodeBlock(block_id);
  360. }
  361. out_ << "}\n";
  362. for (int i = 0; i != semantics_ir_.functions_size(); ++i) {
  363. FormatFunction(SemanticsFunctionId(i));
  364. }
  365. }
  366. auto FormatFunction(SemanticsFunctionId id) -> void {
  367. const SemanticsFunction& fn = semantics_ir_.GetFunction(id);
  368. out_ << "\nfn ";
  369. FormatFunctionName(id);
  370. out_ << "(";
  371. llvm::SaveAndRestore function_scope(scope_, node_namer_.GetScopeFor(id));
  372. llvm::ListSeparator sep;
  373. for (const SemanticsNodeId param_id :
  374. semantics_ir_.GetNodeBlock(fn.param_refs_id)) {
  375. out_ << sep;
  376. auto param = semantics_ir_.GetNode(param_id);
  377. auto [name_id, node_id] = param.GetAsBindName();
  378. FormatNodeName(node_id);
  379. out_ << ": ";
  380. FormatType(param.type_id());
  381. }
  382. out_ << ")";
  383. if (fn.return_type_id.is_valid()) {
  384. out_ << " -> ";
  385. FormatType(fn.return_type_id);
  386. }
  387. if (!fn.body_block_ids.empty()) {
  388. out_ << " {";
  389. for (auto block_id : fn.body_block_ids) {
  390. out_ << "\n";
  391. FormatLabel(block_id);
  392. out_ << ":\n";
  393. FormatCodeBlock(block_id);
  394. }
  395. out_ << "}\n";
  396. } else {
  397. out_ << ";\n";
  398. }
  399. }
  400. auto FormatCodeBlock(SemanticsNodeBlockId block_id) -> void {
  401. if (!block_id.is_valid()) {
  402. return;
  403. }
  404. for (const SemanticsNodeId node_id : semantics_ir_.GetNodeBlock(block_id)) {
  405. FormatInstruction(node_id);
  406. }
  407. }
  408. auto FormatInstruction(SemanticsNodeId node_id) -> void {
  409. if (!node_id.is_valid()) {
  410. out_ << " " << SemanticsNodeKind::Invalid.ir_name() << "\n";
  411. return;
  412. }
  413. FormatInstruction(node_id, semantics_ir_.GetNode(node_id));
  414. }
  415. auto FormatInstruction(SemanticsNodeId node_id, SemanticsNode node) -> void {
  416. switch (node.kind()) {
  417. #define CARBON_SEMANTICS_NODE_KIND(Name) \
  418. case SemanticsNodeKind::Name: \
  419. FormatInstruction<SemanticsNode::Name>(node_id, node); \
  420. break;
  421. #include "toolchain/semantics/semantics_node_kind.def"
  422. }
  423. }
  424. template <typename Kind>
  425. auto FormatInstruction(SemanticsNodeId node_id, SemanticsNode node) -> void {
  426. out_ << " ";
  427. FormatInstructionLHS(node_id, node);
  428. out_ << node.kind().ir_name();
  429. FormatInstructionRHS<Kind>(node);
  430. out_ << "\n";
  431. }
  432. auto FormatInstructionLHS(SemanticsNodeId node_id, SemanticsNode node)
  433. -> void {
  434. switch (node.kind().value_kind()) {
  435. case SemanticsNodeValueKind::Typed:
  436. FormatNodeName(node_id);
  437. out_ << ": ";
  438. FormatType(node.type_id());
  439. out_ << " = ";
  440. break;
  441. case SemanticsNodeValueKind::Untyped:
  442. FormatNodeName(node_id);
  443. out_ << " = ";
  444. break;
  445. case SemanticsNodeValueKind::None:
  446. break;
  447. }
  448. }
  449. template <typename Kind>
  450. auto FormatInstructionRHS(SemanticsNode node) -> void {
  451. // By default, an instruction has a comma-separated argument list.
  452. FormatArgs(Kind::Get(node));
  453. }
  454. // BindName is handled by the NodeNamer and doesn't appear in the output.
  455. // These nodes are currently used simply to give a name to another node, and
  456. // are never referenced themselves.
  457. // TODO: Include BindName nodes in the output if we start referring to them.
  458. template <>
  459. auto FormatInstruction<SemanticsNode::BindName>(SemanticsNodeId /*node_id*/,
  460. SemanticsNode /*node*/)
  461. -> void {}
  462. template <>
  463. auto FormatInstructionRHS<SemanticsNode::BlockArg>(SemanticsNode node)
  464. -> void {
  465. out_ << " ";
  466. FormatLabel(node.GetAsBlockArg());
  467. }
  468. template <>
  469. auto FormatInstruction<SemanticsNode::BranchIf>(SemanticsNodeId /*node_id*/,
  470. SemanticsNode node) -> void {
  471. if (!in_terminator_sequence) {
  472. out_ << " ";
  473. }
  474. auto [label_id, cond_id] = node.GetAsBranchIf();
  475. out_ << "if ";
  476. FormatNodeName(cond_id);
  477. out_ << " " << SemanticsNodeKind::Branch.ir_name() << " ";
  478. FormatLabel(label_id);
  479. out_ << " else ";
  480. in_terminator_sequence = true;
  481. }
  482. template <>
  483. auto FormatInstruction<SemanticsNode::BranchWithArg>(
  484. SemanticsNodeId /*node_id*/, SemanticsNode node) -> void {
  485. if (!in_terminator_sequence) {
  486. out_ << " ";
  487. }
  488. auto [label_id, arg_id] = node.GetAsBranchWithArg();
  489. out_ << SemanticsNodeKind::BranchWithArg.ir_name() << " ";
  490. FormatLabel(label_id);
  491. out_ << "(";
  492. FormatNodeName(arg_id);
  493. out_ << ")\n";
  494. in_terminator_sequence = false;
  495. }
  496. template <>
  497. auto FormatInstruction<SemanticsNode::Branch>(SemanticsNodeId /*node_id*/,
  498. SemanticsNode node) -> void {
  499. if (!in_terminator_sequence) {
  500. out_ << " ";
  501. }
  502. out_ << SemanticsNodeKind::Branch.ir_name() << " ";
  503. FormatLabel(node.GetAsBranch());
  504. out_ << "\n";
  505. in_terminator_sequence = false;
  506. }
  507. template <>
  508. auto FormatInstructionRHS<SemanticsNode::Call>(SemanticsNode node) -> void {
  509. out_ << " ";
  510. auto [args_id, callee_id] = node.GetAsCall();
  511. FormatArg(callee_id);
  512. FormatArg(args_id);
  513. }
  514. template <>
  515. auto FormatInstructionRHS<SemanticsNode::CrossReference>(SemanticsNode node)
  516. -> void {
  517. // TODO: Figure out a way to make this meaningful. We'll need some way to
  518. // name cross-reference IRs, perhaps by the node ID of the import?
  519. auto [xref_id, node_id] = node.GetAsCrossReference();
  520. out_ << " " << xref_id << "." << node_id;
  521. }
  522. // StructTypeFields are formatted as part of their StructType.
  523. template <>
  524. auto FormatInstruction<SemanticsNode::StructTypeField>(
  525. SemanticsNodeId /*node_id*/, SemanticsNode /*node*/) -> void {}
  526. template <>
  527. auto FormatInstructionRHS<SemanticsNode::StructType>(SemanticsNode node)
  528. -> void {
  529. out_ << " {";
  530. llvm::ListSeparator sep;
  531. for (auto field_id : semantics_ir_.GetNodeBlock(node.GetAsStructType())) {
  532. out_ << sep << ".";
  533. auto [field_name_id, field_type_id] =
  534. semantics_ir_.GetNode(field_id).GetAsStructTypeField();
  535. FormatString(field_name_id);
  536. out_ << ": ";
  537. FormatType(field_type_id);
  538. }
  539. out_ << "}";
  540. }
  541. auto FormatArgs(SemanticsNode::NoArgs /*unused*/) -> void {}
  542. template <typename Arg1>
  543. auto FormatArgs(Arg1 arg) -> void {
  544. out_ << ' ';
  545. FormatArg(arg);
  546. }
  547. template <typename Arg1, typename Arg2>
  548. auto FormatArgs(std::pair<Arg1, Arg2> args) -> void {
  549. out_ << ' ';
  550. FormatArg(args.first);
  551. out_ << ",";
  552. FormatArgs(args.second);
  553. }
  554. auto FormatArg(SemanticsBoolValue v) -> void { out_ << v; }
  555. auto FormatArg(SemanticsBuiltinKind kind) -> void { out_ << kind.label(); }
  556. auto FormatArg(SemanticsFunctionId id) -> void { FormatFunctionName(id); }
  557. auto FormatArg(SemanticsIntegerLiteralId id) -> void {
  558. out_ << semantics_ir_.GetIntegerLiteral(id);
  559. }
  560. auto FormatArg(SemanticsMemberIndex index) -> void { out_ << index; }
  561. // TODO: Should we be printing scopes inline, or should we have a separate
  562. // step to print them like we do for functions?
  563. auto FormatArg(SemanticsNameScopeId id) -> void {
  564. // Name scopes aren't kept in any particular order. Sort the entries before
  565. // we print them for stability and consistency.
  566. std::vector<std::pair<SemanticsNodeId, SemanticsStringId>> entries;
  567. for (auto [name_id, node_id] : semantics_ir_.GetNameScope(id)) {
  568. entries.push_back({node_id, name_id});
  569. }
  570. llvm::sort(entries,
  571. [](auto a, auto b) { return a.first.index < b.first.index; });
  572. out_ << '{';
  573. llvm::ListSeparator sep;
  574. for (auto [node_id, name_id] : entries) {
  575. out_ << sep << ".";
  576. FormatString(name_id);
  577. out_ << " = ";
  578. FormatNodeName(node_id);
  579. }
  580. out_ << '}';
  581. }
  582. auto FormatArg(SemanticsNodeId id) -> void { FormatNodeName(id); }
  583. auto FormatArg(SemanticsNodeBlockId id) -> void {
  584. out_ << '(';
  585. llvm::ListSeparator sep;
  586. for (auto node_id : semantics_ir_.GetNodeBlock(id)) {
  587. out_ << sep;
  588. FormatArg(node_id);
  589. }
  590. out_ << ')';
  591. }
  592. auto FormatArg(SemanticsRealLiteralId id) -> void {
  593. // TODO: Format with a `.` when the exponent is near zero.
  594. const auto& real = semantics_ir_.GetRealLiteral(id);
  595. out_ << real.mantissa << (real.is_decimal ? 'e' : 'p') << real.exponent;
  596. }
  597. auto FormatArg(SemanticsStringId id) -> void {
  598. out_ << '"';
  599. out_.write_escaped(semantics_ir_.GetString(id), /*UseHexEscapes=*/true);
  600. out_ << '"';
  601. }
  602. auto FormatArg(SemanticsTypeId id) -> void { FormatType(id); }
  603. auto FormatArg(SemanticsTypeBlockId id) -> void {
  604. out_ << '(';
  605. llvm::ListSeparator sep;
  606. for (auto type_id : semantics_ir_.GetTypeBlock(id)) {
  607. out_ << sep;
  608. FormatArg(type_id);
  609. }
  610. out_ << ')';
  611. }
  612. auto FormatNodeName(SemanticsNodeId id) -> void {
  613. out_ << node_namer_.GetNameFor(scope_, id);
  614. }
  615. auto FormatLabel(SemanticsNodeBlockId id) -> void {
  616. out_ << node_namer_.GetLabelFor(scope_, id);
  617. }
  618. auto FormatString(SemanticsStringId id) -> void {
  619. out_ << semantics_ir_.GetString(id);
  620. }
  621. auto FormatFunctionName(SemanticsFunctionId id) -> void {
  622. out_ << node_namer_.GetNameFor(id);
  623. }
  624. auto FormatType(SemanticsTypeId id) -> void {
  625. if (!id.is_valid()) {
  626. out_ << "invalid";
  627. } else {
  628. out_ << semantics_ir_.StringifyType(id, /*in_type_context=*/true);
  629. }
  630. }
  631. private:
  632. const SemanticsIR& semantics_ir_;
  633. llvm::raw_ostream& out_;
  634. NodeNamer node_namer_;
  635. NodeNamer::ScopeIndex scope_ = NodeNamer::ScopeIndex::None;
  636. bool in_terminator_sequence = false;
  637. };
  638. auto FormatSemanticsIR(const TokenizedBuffer& tokenized_buffer,
  639. const ParseTree& parse_tree,
  640. const SemanticsIR& semantics_ir, llvm::raw_ostream& out)
  641. -> void {
  642. SemanticsIRFormatter(tokenized_buffer, parse_tree, semantics_ir, out)
  643. .Format();
  644. }
  645. } // namespace Carbon