semantics_ir.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. StringifyNodeImpl(out, node_id);
  95. return str;
  96. }
  97. auto SemanticsIR::StringifyNodeImpl(llvm::raw_ostream& out,
  98. SemanticsNodeId node_id) -> void {
  99. // Invalid node IDs will use the default invalid printing.
  100. if (!node_id.is_valid()) {
  101. out << node_id;
  102. return;
  103. }
  104. // Builtins have designated labels.
  105. if (node_id.index < SemanticsBuiltinKind::ValidCount) {
  106. out << SemanticsBuiltinKind::FromInt(node_id.index).label();
  107. return;
  108. }
  109. auto node = GetNode(node_id);
  110. switch (node.kind()) {
  111. case SemanticsNodeKind::StructType: {
  112. out << "{";
  113. auto refs = GetNodeBlock(node.GetAsStructType().second);
  114. llvm::ListSeparator sep;
  115. for (const auto& ref_id : refs) {
  116. out << sep;
  117. // TODO: Bound recursion depth or remove recursive step.
  118. StringifyNodeImpl(out, ref_id);
  119. }
  120. out << "}";
  121. break;
  122. }
  123. case SemanticsNodeKind::StructTypeField: {
  124. out << "." << GetString(node.GetAsStructTypeField()) << ": ";
  125. StringifyNodeImpl(out, node.type_id());
  126. break;
  127. }
  128. case SemanticsNodeKind::Assign:
  129. case SemanticsNodeKind::BinaryOperatorAdd:
  130. case SemanticsNodeKind::BindName:
  131. case SemanticsNodeKind::Builtin:
  132. case SemanticsNodeKind::Call:
  133. case SemanticsNodeKind::CodeBlock:
  134. case SemanticsNodeKind::CrossReference:
  135. case SemanticsNodeKind::FunctionDeclaration:
  136. case SemanticsNodeKind::FunctionDefinition:
  137. case SemanticsNodeKind::IntegerLiteral:
  138. case SemanticsNodeKind::RealLiteral:
  139. case SemanticsNodeKind::Return:
  140. case SemanticsNodeKind::ReturnExpression:
  141. case SemanticsNodeKind::StringLiteral:
  142. case SemanticsNodeKind::StructValue:
  143. case SemanticsNodeKind::StubReference:
  144. case SemanticsNodeKind::VarStorage:
  145. // We don't need to handle stringification for nodes that don't show up in
  146. // errors, but make it clear what's going on so that it's clearer when
  147. // stringification is needed.
  148. out << "<cannot stringify " << node_id << ">";
  149. return;
  150. case SemanticsNodeKind::Invalid:
  151. llvm_unreachable("SemanticsNodeKind::Invalid is never used.");
  152. }
  153. }
  154. } // namespace Carbon