semantics_ir.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.h"
  5. #include "common/check.h"
  6. #include "toolchain/parser/parse_tree_node_location_translator.h"
  7. #include "toolchain/semantics/semantics_builtin_kind.h"
  8. #include "toolchain/semantics/semantics_node.h"
  9. #include "toolchain/semantics/semantics_parse_tree_handler.h"
  10. namespace Carbon {
  11. auto SemanticsIR::MakeBuiltinIR() -> SemanticsIR {
  12. SemanticsIR semantics(/*builtin_ir=*/nullptr);
  13. semantics.nodes_.reserve(SemanticsBuiltinKind::ValidCount);
  14. #define CARBON_SEMANTICS_BUILTIN_KIND(Name, Type, ...) \
  15. semantics.nodes_.push_back(SemanticsNode::Builtin::Make( \
  16. SemanticsBuiltinKind::Name, SemanticsNodeId::Builtin##Type));
  17. #include "toolchain/semantics/semantics_builtin_kind.def"
  18. CARBON_CHECK(semantics.node_blocks_.size() == 1)
  19. << "BuildBuiltins should only have the empty block, actual: "
  20. << semantics.node_blocks_.size();
  21. CARBON_CHECK(semantics.nodes_.size() == SemanticsBuiltinKind::ValidCount)
  22. << "BuildBuiltins should produce " << SemanticsBuiltinKind::ValidCount
  23. << " nodes, actual: " << semantics.nodes_.size();
  24. return semantics;
  25. }
  26. auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
  27. const TokenizedBuffer& tokens,
  28. const ParseTree& parse_tree,
  29. DiagnosticConsumer& consumer,
  30. llvm::raw_ostream* vlog_stream)
  31. -> SemanticsIR {
  32. SemanticsIR semantics(&builtin_ir);
  33. // Copy builtins over.
  34. semantics.nodes_.resize_for_overwrite(SemanticsBuiltinKind::ValidCount);
  35. static constexpr auto BuiltinIR = SemanticsCrossReferenceIRId(0);
  36. for (int i = 0; i < SemanticsBuiltinKind::ValidCount; ++i) {
  37. // We can reuse the type node ID because the offsets of cross-references
  38. // will be the same in this IR.
  39. auto type = builtin_ir.nodes_[i].type_id();
  40. semantics.nodes_[i] = SemanticsNode::CrossReference::Make(
  41. type, BuiltinIR, SemanticsNodeId(i));
  42. }
  43. ParseTreeNodeLocationTranslator translator(&tokens, &parse_tree);
  44. ErrorTrackingDiagnosticConsumer err_tracker(consumer);
  45. DiagnosticEmitter<ParseTree::Node> emitter(translator, err_tracker);
  46. SemanticsParseTreeHandler(tokens, emitter, parse_tree, semantics, vlog_stream)
  47. .Build();
  48. semantics.has_errors_ = err_tracker.seen_error();
  49. return semantics;
  50. }
  51. static constexpr int Indent = 2;
  52. template <typename T>
  53. static auto PrintList(llvm::raw_ostream& out, llvm::StringLiteral name,
  54. const llvm::SmallVector<T>& list) {
  55. out << name << ": [\n";
  56. for (const auto& element : list) {
  57. out.indent(Indent);
  58. out << element << ",\n";
  59. }
  60. out << "]\n";
  61. }
  62. auto SemanticsIR::Print(llvm::raw_ostream& out, bool include_builtins) const
  63. -> void {
  64. out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
  65. PrintList(out, "calls", calls_);
  66. PrintList(out, "callables", callables_);
  67. PrintList(out, "integer_literals", integer_literals_);
  68. PrintList(out, "real_literals", real_literals_);
  69. PrintList(out, "strings", strings_);
  70. out << "nodes: [\n";
  71. for (int i = include_builtins ? 0 : SemanticsBuiltinKind::ValidCount;
  72. i < static_cast<int>(nodes_.size()); ++i) {
  73. const auto& element = nodes_[i];
  74. out.indent(Indent);
  75. out << element << ",\n";
  76. }
  77. out << "]\n";
  78. out << "node_blocks: [\n";
  79. for (const auto& node_block : node_blocks_) {
  80. out.indent(Indent);
  81. out << "[\n";
  82. for (const auto& node : node_block) {
  83. out.indent(2 * Indent);
  84. out << node << ",\n";
  85. }
  86. out.indent(Indent);
  87. out << "],\n";
  88. }
  89. out << "]\n";
  90. }
  91. auto SemanticsIR::StringifyNode(SemanticsNodeId node_id) -> std::string {
  92. std::string str;
  93. llvm::raw_string_ostream out(str);
  94. struct Step {
  95. // The node to print.
  96. SemanticsNodeId node_id;
  97. // The index into node_id to print. Not used by all types.
  98. int index = 0;
  99. };
  100. llvm::SmallVector<Step> steps = {{.node_id = node_id}};
  101. while (!steps.empty()) {
  102. auto step = steps.pop_back_val();
  103. // Invalid node IDs will use the default invalid printing.
  104. if (!step.node_id.is_valid()) {
  105. out << step.node_id;
  106. continue;
  107. }
  108. // Builtins have designated labels.
  109. if (step.node_id.index < SemanticsBuiltinKind::ValidCount) {
  110. out << SemanticsBuiltinKind::FromInt(step.node_id.index).label();
  111. continue;
  112. }
  113. auto node = GetNode(step.node_id);
  114. switch (node.kind()) {
  115. case SemanticsNodeKind::StructType: {
  116. auto refs = GetNodeBlock(node.GetAsStructType().second);
  117. if (step.index == 0) {
  118. out << "{";
  119. } else if (step.index < static_cast<int>(refs.size())) {
  120. out << ", ";
  121. } else {
  122. out << "}";
  123. break;
  124. }
  125. steps.push_back({.node_id = step.node_id, .index = step.index + 1});
  126. steps.push_back({.node_id = refs[step.index]});
  127. break;
  128. }
  129. case SemanticsNodeKind::StructTypeField: {
  130. out << "." << GetString(node.GetAsStructTypeField()) << ": ";
  131. steps.push_back({.node_id = node.type_id()});
  132. break;
  133. }
  134. case SemanticsNodeKind::Assign:
  135. case SemanticsNodeKind::BinaryOperatorAdd:
  136. case SemanticsNodeKind::BindName:
  137. case SemanticsNodeKind::Builtin:
  138. case SemanticsNodeKind::Call:
  139. case SemanticsNodeKind::CodeBlock:
  140. case SemanticsNodeKind::CrossReference:
  141. case SemanticsNodeKind::FunctionDeclaration:
  142. case SemanticsNodeKind::FunctionDefinition:
  143. case SemanticsNodeKind::IntegerLiteral:
  144. case SemanticsNodeKind::RealLiteral:
  145. case SemanticsNodeKind::Return:
  146. case SemanticsNodeKind::ReturnExpression:
  147. case SemanticsNodeKind::StringLiteral:
  148. case SemanticsNodeKind::StructMemberAccess:
  149. case SemanticsNodeKind::StructValue:
  150. case SemanticsNodeKind::StubReference:
  151. case SemanticsNodeKind::VarStorage:
  152. // We don't need to handle stringification for nodes that don't show up
  153. // in errors, but make it clear what's going on so that it's clearer
  154. // when stringification is needed.
  155. out << "<cannot stringify " << step.node_id << ">";
  156. break;
  157. case SemanticsNodeKind::Invalid:
  158. llvm_unreachable("SemanticsNodeKind::Invalid is never used.");
  159. }
  160. }
  161. return str;
  162. }
  163. } // namespace Carbon