semantics_ir.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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/common/pretty_stack_trace_function.h"
  7. #include "toolchain/parser/parse_tree_node_location_translator.h"
  8. #include "toolchain/semantics/semantics_builtin_kind.h"
  9. #include "toolchain/semantics/semantics_context.h"
  10. #include "toolchain/semantics/semantics_node.h"
  11. namespace Carbon {
  12. auto SemanticsIR::MakeBuiltinIR() -> SemanticsIR {
  13. SemanticsIR semantics_ir(/*builtin_ir=*/nullptr);
  14. semantics_ir.nodes_.reserve(SemanticsBuiltinKind::ValidCount);
  15. // InvalidType uses a self-referential type so that it's not accidentally
  16. // treated as a normal type. Every other builtin is a type, including the
  17. // self-referential TypeType.
  18. #define CARBON_SEMANTICS_BUILTIN_KIND(Name, ...) \
  19. semantics_ir.nodes_.push_back(SemanticsNode::Builtin::Make( \
  20. SemanticsBuiltinKind::Name, \
  21. SemanticsBuiltinKind::Name == SemanticsBuiltinKind::InvalidType \
  22. ? SemanticsTypeId::InvalidType \
  23. : SemanticsTypeId::TypeType));
  24. #include "toolchain/semantics/semantics_builtin_kind.def"
  25. CARBON_CHECK(semantics_ir.node_blocks_.size() == 1)
  26. << "BuildBuiltins should only have the empty block, actual: "
  27. << semantics_ir.node_blocks_.size();
  28. CARBON_CHECK(semantics_ir.nodes_.size() == SemanticsBuiltinKind::ValidCount)
  29. << "BuildBuiltins should produce " << SemanticsBuiltinKind::ValidCount
  30. << " nodes, actual: " << semantics_ir.nodes_.size();
  31. return semantics_ir;
  32. }
  33. auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
  34. const TokenizedBuffer& tokens,
  35. const ParseTree& parse_tree,
  36. DiagnosticConsumer& consumer,
  37. llvm::raw_ostream* vlog_stream)
  38. -> SemanticsIR {
  39. SemanticsIR semantics_ir(&builtin_ir);
  40. // Copy builtins over.
  41. semantics_ir.nodes_.resize_for_overwrite(SemanticsBuiltinKind::ValidCount);
  42. static constexpr auto BuiltinIR = SemanticsCrossReferenceIRId(0);
  43. for (int i = 0; i < SemanticsBuiltinKind::ValidCount; ++i) {
  44. // We can reuse the type node ID because the offsets of cross-references
  45. // will be the same in this IR.
  46. auto type = builtin_ir.nodes_[i].type_id();
  47. semantics_ir.nodes_[i] = SemanticsNode::CrossReference::Make(
  48. type, BuiltinIR, SemanticsNodeId(i));
  49. }
  50. ParseTreeNodeLocationTranslator translator(&tokens, &parse_tree);
  51. ErrorTrackingDiagnosticConsumer err_tracker(consumer);
  52. DiagnosticEmitter<ParseTree::Node> emitter(translator, err_tracker);
  53. SemanticsContext context(tokens, emitter, parse_tree, semantics_ir,
  54. vlog_stream);
  55. PrettyStackTraceFunction context_dumper(
  56. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  57. // Add a block for the ParseTree.
  58. context.node_block_stack().Push();
  59. context.PushScope();
  60. // Loops over all nodes in the tree. On some errors, this may return early,
  61. // for example if an unrecoverable state is encountered.
  62. for (auto parse_node : parse_tree.postorder()) {
  63. switch (auto parse_kind = parse_tree.node_kind(parse_node)) {
  64. #define CARBON_PARSE_NODE_KIND(Name) \
  65. case ParseNodeKind::Name: { \
  66. if (!SemanticsHandle##Name(context, parse_node)) { \
  67. semantics_ir.has_errors_ = true; \
  68. return semantics_ir; \
  69. } \
  70. break; \
  71. }
  72. #include "toolchain/parser/parse_node_kind.def"
  73. }
  74. }
  75. // Pop information for the file-level scope.
  76. semantics_ir.top_node_block_id_ = context.node_block_stack().Pop();
  77. context.PopScope();
  78. context.VerifyOnFinish();
  79. semantics_ir.has_errors_ = err_tracker.seen_error();
  80. return semantics_ir;
  81. }
  82. static constexpr int Indent = 2;
  83. template <typename T>
  84. static auto PrintList(llvm::raw_ostream& out, llvm::StringLiteral name,
  85. const llvm::SmallVector<T>& list) {
  86. out << name << ": [\n";
  87. for (const auto& element : list) {
  88. out.indent(Indent);
  89. out << element << ",\n";
  90. }
  91. out << "]\n";
  92. }
  93. auto SemanticsIR::Print(llvm::raw_ostream& out, bool include_builtins) const
  94. -> void {
  95. out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
  96. PrintList(out, "functions", functions_);
  97. PrintList(out, "integer_literals", integer_literals_);
  98. PrintList(out, "real_literals", real_literals_);
  99. PrintList(out, "strings", strings_);
  100. PrintList(out, "types", types_);
  101. out << "nodes: [\n";
  102. for (int i = include_builtins ? 0 : SemanticsBuiltinKind::ValidCount;
  103. i < static_cast<int>(nodes_.size()); ++i) {
  104. const auto& element = nodes_[i];
  105. out.indent(Indent);
  106. out << element << ",\n";
  107. }
  108. out << "]\n";
  109. out << "node_blocks: [\n";
  110. for (const auto& node_block : node_blocks_) {
  111. out.indent(Indent);
  112. out << "[\n";
  113. for (const auto& node : node_block) {
  114. out.indent(2 * Indent);
  115. out << node << ",\n";
  116. }
  117. out.indent(Indent);
  118. out << "],\n";
  119. }
  120. out << "]\n";
  121. }
  122. auto SemanticsIR::StringifyType(SemanticsTypeId type_id) -> std::string {
  123. std::string str;
  124. llvm::raw_string_ostream out(str);
  125. struct Step {
  126. // The node to print.
  127. SemanticsNodeId node_id;
  128. // The index into node_id to print. Not used by all types.
  129. int index = 0;
  130. };
  131. llvm::SmallVector<Step> steps = {
  132. {.node_id = GetTypeAllowBuiltinTypes(type_id)}};
  133. while (!steps.empty()) {
  134. auto step = steps.pop_back_val();
  135. // Invalid node IDs will use the default invalid printing.
  136. if (!step.node_id.is_valid()) {
  137. out << step.node_id;
  138. continue;
  139. }
  140. // Builtins have designated labels.
  141. if (step.node_id.index < SemanticsBuiltinKind::ValidCount) {
  142. out << SemanticsBuiltinKind::FromInt(step.node_id.index).label();
  143. continue;
  144. }
  145. auto node = GetNode(step.node_id);
  146. switch (node.kind()) {
  147. case SemanticsNodeKind::StructType: {
  148. auto refs = GetNodeBlock(node.GetAsStructType());
  149. if (refs.empty()) {
  150. out << "{} as Type";
  151. break;
  152. } else if (step.index == 0) {
  153. out << "{";
  154. } else if (step.index < static_cast<int>(refs.size())) {
  155. out << ", ";
  156. } else {
  157. out << "}";
  158. break;
  159. }
  160. steps.push_back({.node_id = step.node_id, .index = step.index + 1});
  161. steps.push_back({.node_id = refs[step.index]});
  162. break;
  163. }
  164. case SemanticsNodeKind::StructTypeField: {
  165. out << "." << GetString(node.GetAsStructTypeField()) << ": ";
  166. steps.push_back({.node_id = GetTypeAllowBuiltinTypes(node.type_id())});
  167. break;
  168. }
  169. case SemanticsNodeKind::Assign:
  170. case SemanticsNodeKind::BinaryOperatorAdd:
  171. case SemanticsNodeKind::BindName:
  172. case SemanticsNodeKind::BlockArg:
  173. case SemanticsNodeKind::BoolLiteral:
  174. case SemanticsNodeKind::Branch:
  175. case SemanticsNodeKind::BranchIf:
  176. case SemanticsNodeKind::BranchWithArg:
  177. case SemanticsNodeKind::Builtin:
  178. case SemanticsNodeKind::Call:
  179. case SemanticsNodeKind::CodeBlock:
  180. case SemanticsNodeKind::CrossReference:
  181. case SemanticsNodeKind::FunctionDeclaration:
  182. case SemanticsNodeKind::IntegerLiteral:
  183. case SemanticsNodeKind::RealLiteral:
  184. case SemanticsNodeKind::Return:
  185. case SemanticsNodeKind::ReturnExpression:
  186. case SemanticsNodeKind::StringLiteral:
  187. case SemanticsNodeKind::StructMemberAccess:
  188. case SemanticsNodeKind::StructValue:
  189. case SemanticsNodeKind::StubReference:
  190. case SemanticsNodeKind::VarStorage:
  191. // We don't need to handle stringification for nodes that don't show up
  192. // in errors, but make it clear what's going on so that it's clearer
  193. // when stringification is needed.
  194. out << "<cannot stringify " << step.node_id << ">";
  195. break;
  196. case SemanticsNodeKind::Invalid:
  197. llvm_unreachable("SemanticsNodeKind::Invalid is never used.");
  198. }
  199. }
  200. return str;
  201. }
  202. } // namespace Carbon