semantics_ir.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #ifndef NDEBUG
  81. if (auto verify = semantics_ir.Verify(); !verify.ok()) {
  82. CARBON_FATAL() << semantics_ir
  83. << "Built invalid semantics IR: " << verify.error() << "\n";
  84. }
  85. #endif
  86. return semantics_ir;
  87. }
  88. auto SemanticsIR::Verify() const -> ErrorOr<Success> {
  89. // Invariants don't necessarily hold for invalid IR.
  90. if (has_errors_) {
  91. return Success();
  92. }
  93. // Check that every code block has a terminator sequence that appears at the
  94. // end of the block.
  95. for (const SemanticsFunction& function : functions_) {
  96. for (SemanticsNodeBlockId block_id : function.body_block_ids) {
  97. SemanticsTerminatorKind prior_kind =
  98. SemanticsTerminatorKind::NotTerminator;
  99. for (SemanticsNodeId node_id : GetNodeBlock(block_id)) {
  100. SemanticsTerminatorKind node_kind =
  101. GetNode(node_id).kind().terminator_kind();
  102. if (prior_kind == SemanticsTerminatorKind::Terminator) {
  103. return Error(llvm::formatv("Node {0} in block {1} follows terminator",
  104. node_id, block_id));
  105. }
  106. if (prior_kind > node_kind) {
  107. return Error(
  108. llvm::formatv("Non-terminator node {0} in block {1} follows "
  109. "terminator sequence",
  110. node_id, block_id));
  111. }
  112. prior_kind = node_kind;
  113. }
  114. if (prior_kind != SemanticsTerminatorKind::Terminator) {
  115. return Error(llvm::formatv("No terminator in block {0}", block_id));
  116. }
  117. }
  118. }
  119. // TODO: Check that a node only references other nodes that are either global
  120. // or that dominate it.
  121. return Success();
  122. }
  123. static constexpr int Indent = 2;
  124. template <typename T>
  125. static auto PrintList(llvm::raw_ostream& out, llvm::StringLiteral name,
  126. const llvm::SmallVector<T>& list) {
  127. out << name << ": [\n";
  128. for (const auto& element : list) {
  129. out.indent(Indent);
  130. out << element << ",\n";
  131. }
  132. out << "]\n";
  133. }
  134. auto SemanticsIR::Print(llvm::raw_ostream& out, bool include_builtins) const
  135. -> void {
  136. out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
  137. PrintList(out, "functions", functions_);
  138. PrintList(out, "integer_literals", integer_literals_);
  139. PrintList(out, "real_literals", real_literals_);
  140. PrintList(out, "strings", strings_);
  141. PrintList(out, "types", types_);
  142. out << "nodes: [\n";
  143. for (int i = include_builtins ? 0 : SemanticsBuiltinKind::ValidCount;
  144. i < static_cast<int>(nodes_.size()); ++i) {
  145. const auto& element = nodes_[i];
  146. out.indent(Indent);
  147. out << element << ",\n";
  148. }
  149. out << "]\n";
  150. out << "node_blocks: [\n";
  151. for (const auto& node_block : node_blocks_) {
  152. out.indent(Indent);
  153. out << "[\n";
  154. for (const auto& node : node_block) {
  155. out.indent(2 * Indent);
  156. out << node << ",\n";
  157. }
  158. out.indent(Indent);
  159. out << "],\n";
  160. }
  161. out << "]\n";
  162. }
  163. auto SemanticsIR::StringifyType(SemanticsTypeId type_id) -> std::string {
  164. std::string str;
  165. llvm::raw_string_ostream out(str);
  166. struct Step {
  167. // The node to print.
  168. SemanticsNodeId node_id;
  169. // The index into node_id to print. Not used by all types.
  170. int index = 0;
  171. };
  172. llvm::SmallVector<Step> steps = {
  173. {.node_id = GetTypeAllowBuiltinTypes(type_id)}};
  174. while (!steps.empty()) {
  175. auto step = steps.pop_back_val();
  176. // Invalid node IDs will use the default invalid printing.
  177. if (!step.node_id.is_valid()) {
  178. out << step.node_id;
  179. continue;
  180. }
  181. // Builtins have designated labels.
  182. if (step.node_id.index < SemanticsBuiltinKind::ValidCount) {
  183. out << SemanticsBuiltinKind::FromInt(step.node_id.index).label();
  184. continue;
  185. }
  186. auto node = GetNode(step.node_id);
  187. switch (node.kind()) {
  188. case SemanticsNodeKind::StructType: {
  189. auto refs = GetNodeBlock(node.GetAsStructType());
  190. if (refs.empty()) {
  191. out << "{} as Type";
  192. break;
  193. } else if (step.index == 0) {
  194. out << "{";
  195. } else if (step.index < static_cast<int>(refs.size())) {
  196. out << ", ";
  197. } else {
  198. out << "}";
  199. break;
  200. }
  201. steps.push_back({.node_id = step.node_id, .index = step.index + 1});
  202. steps.push_back({.node_id = refs[step.index]});
  203. break;
  204. }
  205. case SemanticsNodeKind::StructTypeField: {
  206. out << "." << GetString(node.GetAsStructTypeField()) << ": ";
  207. steps.push_back({.node_id = GetTypeAllowBuiltinTypes(node.type_id())});
  208. break;
  209. }
  210. case SemanticsNodeKind::Assign:
  211. case SemanticsNodeKind::BinaryOperatorAdd:
  212. case SemanticsNodeKind::BindName:
  213. case SemanticsNodeKind::BlockArg:
  214. case SemanticsNodeKind::BoolLiteral:
  215. case SemanticsNodeKind::Branch:
  216. case SemanticsNodeKind::BranchIf:
  217. case SemanticsNodeKind::BranchWithArg:
  218. case SemanticsNodeKind::Builtin:
  219. case SemanticsNodeKind::Call:
  220. case SemanticsNodeKind::CrossReference:
  221. case SemanticsNodeKind::FunctionDeclaration:
  222. case SemanticsNodeKind::IntegerLiteral:
  223. case SemanticsNodeKind::Namespace:
  224. case SemanticsNodeKind::RealLiteral:
  225. case SemanticsNodeKind::Return:
  226. case SemanticsNodeKind::ReturnExpression:
  227. case SemanticsNodeKind::StringLiteral:
  228. case SemanticsNodeKind::StructMemberAccess:
  229. case SemanticsNodeKind::StructValue:
  230. case SemanticsNodeKind::StubReference:
  231. case SemanticsNodeKind::UnaryOperatorNot:
  232. case SemanticsNodeKind::VarStorage:
  233. // We don't need to handle stringification for nodes that don't show up
  234. // in errors, but make it clear what's going on so that it's clearer
  235. // when stringification is needed.
  236. out << "<cannot stringify " << step.node_id << ">";
  237. break;
  238. case SemanticsNodeKind::Invalid:
  239. llvm_unreachable("SemanticsNodeKind::Invalid is never used.");
  240. }
  241. }
  242. return str;
  243. }
  244. } // namespace Carbon