semantics_ir.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. #include "toolchain/semantics/semantics_node_kind.h"
  12. namespace Carbon {
  13. auto SemanticsIR::MakeBuiltinIR() -> SemanticsIR {
  14. SemanticsIR semantics_ir(/*builtin_ir=*/nullptr);
  15. semantics_ir.nodes_.reserve(SemanticsBuiltinKind::ValidCount);
  16. // Error uses a self-referential type so that it's not accidentally treated as
  17. // a normal type. Every other builtin is a type, including the
  18. // self-referential TypeType.
  19. #define CARBON_SEMANTICS_BUILTIN_KIND(Name, ...) \
  20. semantics_ir.nodes_.push_back(SemanticsNode::Builtin::Make( \
  21. SemanticsBuiltinKind::Name, \
  22. SemanticsBuiltinKind::Name == SemanticsBuiltinKind::Error \
  23. ? SemanticsTypeId::Error \
  24. : SemanticsTypeId::TypeType));
  25. #include "toolchain/semantics/semantics_builtin_kind.def"
  26. CARBON_CHECK(semantics_ir.node_blocks_.size() == 1)
  27. << "BuildBuiltins should only have the empty block, actual: "
  28. << semantics_ir.node_blocks_.size();
  29. CARBON_CHECK(semantics_ir.nodes_.size() == SemanticsBuiltinKind::ValidCount)
  30. << "BuildBuiltins should produce " << SemanticsBuiltinKind::ValidCount
  31. << " nodes, actual: " << semantics_ir.nodes_.size();
  32. return semantics_ir;
  33. }
  34. auto SemanticsIR::MakeFromParseTree(const SemanticsIR& builtin_ir,
  35. const TokenizedBuffer& tokens,
  36. const ParseTree& parse_tree,
  37. DiagnosticConsumer& consumer,
  38. llvm::raw_ostream* vlog_stream)
  39. -> SemanticsIR {
  40. SemanticsIR semantics_ir(&builtin_ir);
  41. // Copy builtins over.
  42. semantics_ir.nodes_.resize_for_overwrite(SemanticsBuiltinKind::ValidCount);
  43. static constexpr auto BuiltinIR = SemanticsCrossReferenceIRId(0);
  44. for (int i = 0; i < SemanticsBuiltinKind::ValidCount; ++i) {
  45. // We can reuse the type node ID because the offsets of cross-references
  46. // will be the same in this IR.
  47. auto type = builtin_ir.nodes_[i].type_id();
  48. semantics_ir.nodes_[i] = SemanticsNode::CrossReference::Make(
  49. type, BuiltinIR, SemanticsNodeId(i));
  50. }
  51. ParseTreeNodeLocationTranslator translator(&tokens, &parse_tree);
  52. ErrorTrackingDiagnosticConsumer err_tracker(consumer);
  53. DiagnosticEmitter<ParseTree::Node> emitter(translator, err_tracker);
  54. SemanticsContext context(tokens, emitter, parse_tree, semantics_ir,
  55. vlog_stream);
  56. PrettyStackTraceFunction context_dumper(
  57. [&](llvm::raw_ostream& output) { context.PrintForStackDump(output); });
  58. // Add a block for the ParseTree.
  59. context.node_block_stack().Push();
  60. context.PushScope();
  61. // Loops over all nodes in the tree. On some errors, this may return early,
  62. // for example if an unrecoverable state is encountered.
  63. for (auto parse_node : parse_tree.postorder()) {
  64. switch (auto parse_kind = parse_tree.node_kind(parse_node)) {
  65. #define CARBON_PARSE_NODE_KIND(Name) \
  66. case ParseNodeKind::Name: { \
  67. if (!SemanticsHandle##Name(context, parse_node)) { \
  68. semantics_ir.has_errors_ = true; \
  69. return semantics_ir; \
  70. } \
  71. break; \
  72. }
  73. #include "toolchain/parser/parse_node_kind.def"
  74. }
  75. }
  76. // Pop information for the file-level scope.
  77. semantics_ir.top_node_block_id_ = context.node_block_stack().Pop();
  78. context.PopScope();
  79. context.VerifyOnFinish();
  80. semantics_ir.has_errors_ = err_tracker.seen_error();
  81. #ifndef NDEBUG
  82. if (auto verify = semantics_ir.Verify(); !verify.ok()) {
  83. CARBON_FATAL() << semantics_ir
  84. << "Built invalid semantics IR: " << verify.error() << "\n";
  85. }
  86. #endif
  87. return semantics_ir;
  88. }
  89. auto SemanticsIR::Verify() const -> ErrorOr<Success> {
  90. // Invariants don't necessarily hold for invalid IR.
  91. if (has_errors_) {
  92. return Success();
  93. }
  94. // Check that every code block has a terminator sequence that appears at the
  95. // end of the block.
  96. for (const SemanticsFunction& function : functions_) {
  97. for (SemanticsNodeBlockId block_id : function.body_block_ids) {
  98. SemanticsTerminatorKind prior_kind =
  99. SemanticsTerminatorKind::NotTerminator;
  100. for (SemanticsNodeId node_id : GetNodeBlock(block_id)) {
  101. SemanticsTerminatorKind node_kind =
  102. GetNode(node_id).kind().terminator_kind();
  103. if (prior_kind == SemanticsTerminatorKind::Terminator) {
  104. return Error(llvm::formatv("Node {0} in block {1} follows terminator",
  105. node_id, block_id));
  106. }
  107. if (prior_kind > node_kind) {
  108. return Error(
  109. llvm::formatv("Non-terminator node {0} in block {1} follows "
  110. "terminator sequence",
  111. node_id, block_id));
  112. }
  113. prior_kind = node_kind;
  114. }
  115. if (prior_kind != SemanticsTerminatorKind::Terminator) {
  116. return Error(llvm::formatv("No terminator in block {0}", block_id));
  117. }
  118. }
  119. }
  120. // TODO: Check that a node only references other nodes that are either global
  121. // or that dominate it.
  122. return Success();
  123. }
  124. static constexpr int Indent = 2;
  125. template <typename T>
  126. static auto PrintList(llvm::raw_ostream& out, llvm::StringLiteral name,
  127. const llvm::SmallVector<T>& list) {
  128. out << name << ": [\n";
  129. for (const auto& element : list) {
  130. out.indent(Indent);
  131. out << element << ",\n";
  132. }
  133. out << "]\n";
  134. }
  135. template <typename T>
  136. static auto PrintBlock(llvm::raw_ostream& out, llvm::StringLiteral block_name,
  137. const llvm::SmallVector<T>& blocks) {
  138. out << block_name << ": [\n";
  139. for (const auto& block : blocks) {
  140. out.indent(Indent);
  141. out << "[\n";
  142. for (const auto& node : block) {
  143. out.indent(2 * Indent);
  144. out << node << ",\n";
  145. }
  146. out.indent(Indent);
  147. out << "],\n";
  148. }
  149. out << "]\n";
  150. }
  151. auto SemanticsIR::Print(llvm::raw_ostream& out, bool include_builtins) const
  152. -> void {
  153. out << "cross_reference_irs_size: " << cross_reference_irs_.size() << "\n";
  154. PrintList(out, "functions", functions_);
  155. PrintList(out, "integer_literals", integer_literals_);
  156. PrintList(out, "real_literals", real_literals_);
  157. PrintList(out, "strings", strings_);
  158. PrintList(out, "types", types_);
  159. PrintBlock(out, "type_blocks", type_blocks_);
  160. out << "nodes: [\n";
  161. for (int i = include_builtins ? 0 : SemanticsBuiltinKind::ValidCount;
  162. i < static_cast<int>(nodes_.size()); ++i) {
  163. const auto& element = nodes_[i];
  164. out.indent(Indent);
  165. out << element << ",\n";
  166. }
  167. out << "]\n";
  168. PrintBlock(out, "node_blocks", node_blocks_);
  169. }
  170. // Map a node kind representing a type into an integer describing the
  171. // precedence of that type's syntax. Higher numbers correspond to higher
  172. // precedence.
  173. static auto GetTypePrecedence(SemanticsNodeKind kind) -> int {
  174. switch (kind) {
  175. case SemanticsNodeKind::Builtin:
  176. case SemanticsNodeKind::StructType:
  177. case SemanticsNodeKind::TupleType:
  178. return 0;
  179. case SemanticsNodeKind::ConstType:
  180. return -1;
  181. case SemanticsNodeKind::PointerType:
  182. return -2;
  183. case SemanticsNodeKind::CrossReference:
  184. // TODO: Once we support stringification of cross-references, we'll need
  185. // to determine the precedence of the target of the cross-reference. For
  186. // now, all cross-references refer to builtin types from the prelude.
  187. return 0;
  188. case SemanticsNodeKind::Assign:
  189. case SemanticsNodeKind::BinaryOperatorAdd:
  190. case SemanticsNodeKind::BindName:
  191. case SemanticsNodeKind::BlockArg:
  192. case SemanticsNodeKind::BoolLiteral:
  193. case SemanticsNodeKind::Branch:
  194. case SemanticsNodeKind::BranchIf:
  195. case SemanticsNodeKind::BranchWithArg:
  196. case SemanticsNodeKind::Call:
  197. case SemanticsNodeKind::FunctionDeclaration:
  198. case SemanticsNodeKind::IntegerLiteral:
  199. case SemanticsNodeKind::Invalid:
  200. case SemanticsNodeKind::Namespace:
  201. case SemanticsNodeKind::RealLiteral:
  202. case SemanticsNodeKind::Return:
  203. case SemanticsNodeKind::ReturnExpression:
  204. case SemanticsNodeKind::StringLiteral:
  205. case SemanticsNodeKind::StructMemberAccess:
  206. case SemanticsNodeKind::StructTypeField:
  207. case SemanticsNodeKind::StructValue:
  208. case SemanticsNodeKind::StubReference:
  209. case SemanticsNodeKind::TupleValue:
  210. case SemanticsNodeKind::UnaryOperatorNot:
  211. case SemanticsNodeKind::VarStorage:
  212. CARBON_FATAL() << "GetTypePrecedence for non-type node kind " << kind;
  213. }
  214. }
  215. auto SemanticsIR::StringifyType(SemanticsTypeId type_id) -> std::string {
  216. std::string str;
  217. llvm::raw_string_ostream out(str);
  218. struct Step {
  219. // The node to print.
  220. SemanticsNodeId node_id;
  221. // The index into node_id to print. Not used by all types.
  222. int index = 0;
  223. auto Next() const -> Step {
  224. return {.node_id = node_id, .index = index + 1};
  225. }
  226. };
  227. auto outer_node_id = GetTypeAllowBuiltinTypes(type_id);
  228. llvm::SmallVector<Step> steps = {{.node_id = outer_node_id}};
  229. while (!steps.empty()) {
  230. auto step = steps.pop_back_val();
  231. // Invalid node IDs will use the default invalid printing.
  232. if (!step.node_id.is_valid()) {
  233. out << step.node_id;
  234. continue;
  235. }
  236. // Builtins have designated labels.
  237. if (step.node_id.index < SemanticsBuiltinKind::ValidCount) {
  238. out << SemanticsBuiltinKind::FromInt(step.node_id.index).label();
  239. continue;
  240. }
  241. auto node = GetNode(step.node_id);
  242. switch (node.kind()) {
  243. case SemanticsNodeKind::ConstType: {
  244. if (step.index == 0) {
  245. out << "const ";
  246. // Add parentheses if required.
  247. auto inner_type_node_id = GetType(node.GetAsConstType());
  248. if (GetTypePrecedence(GetNode(inner_type_node_id).kind()) <
  249. GetTypePrecedence(node.kind())) {
  250. out << "(";
  251. steps.push_back(step.Next());
  252. }
  253. steps.push_back({.node_id = inner_type_node_id});
  254. } else if (step.index == 1) {
  255. out << ")";
  256. }
  257. break;
  258. }
  259. case SemanticsNodeKind::PointerType: {
  260. if (step.index == 0) {
  261. steps.push_back(step.Next());
  262. steps.push_back({.node_id = GetType(node.GetAsPointerType())});
  263. } else if (step.index == 1) {
  264. out << "*";
  265. }
  266. break;
  267. }
  268. case SemanticsNodeKind::StructType: {
  269. auto refs = GetNodeBlock(node.GetAsStructType());
  270. if (refs.empty()) {
  271. out << "{}";
  272. break;
  273. } else if (step.index == 0) {
  274. out << "{";
  275. } else if (step.index < static_cast<int>(refs.size())) {
  276. out << ", ";
  277. } else {
  278. out << "}";
  279. break;
  280. }
  281. steps.push_back(step.Next());
  282. steps.push_back({.node_id = refs[step.index]});
  283. break;
  284. }
  285. case SemanticsNodeKind::StructTypeField: {
  286. auto [name_id, type_id] = node.GetAsStructTypeField();
  287. out << "." << GetString(name_id) << ": ";
  288. steps.push_back({.node_id = GetTypeAllowBuiltinTypes(type_id)});
  289. break;
  290. }
  291. case SemanticsNodeKind::TupleType: {
  292. auto refs = GetTypeBlock(node.GetAsTupleType());
  293. if (refs.empty()) {
  294. out << "()";
  295. break;
  296. } else if (step.index == 0) {
  297. out << "(";
  298. } else if (step.index < static_cast<int>(refs.size())) {
  299. out << ", ";
  300. } else {
  301. // A tuple of one element has a comma to disambiguate from an
  302. // expression.
  303. if (step.index == 1) {
  304. out << ",";
  305. }
  306. out << ")";
  307. break;
  308. }
  309. steps.push_back(step.Next());
  310. steps.push_back(
  311. {.node_id = GetTypeAllowBuiltinTypes(refs[step.index])});
  312. break;
  313. }
  314. case SemanticsNodeKind::Assign:
  315. case SemanticsNodeKind::BinaryOperatorAdd:
  316. case SemanticsNodeKind::BindName:
  317. case SemanticsNodeKind::BlockArg:
  318. case SemanticsNodeKind::BoolLiteral:
  319. case SemanticsNodeKind::Branch:
  320. case SemanticsNodeKind::BranchIf:
  321. case SemanticsNodeKind::BranchWithArg:
  322. case SemanticsNodeKind::Builtin:
  323. case SemanticsNodeKind::Call:
  324. case SemanticsNodeKind::CrossReference:
  325. case SemanticsNodeKind::FunctionDeclaration:
  326. case SemanticsNodeKind::IntegerLiteral:
  327. case SemanticsNodeKind::Namespace:
  328. case SemanticsNodeKind::RealLiteral:
  329. case SemanticsNodeKind::Return:
  330. case SemanticsNodeKind::ReturnExpression:
  331. case SemanticsNodeKind::StringLiteral:
  332. case SemanticsNodeKind::StructMemberAccess:
  333. case SemanticsNodeKind::StructValue:
  334. case SemanticsNodeKind::StubReference:
  335. case SemanticsNodeKind::TupleValue:
  336. case SemanticsNodeKind::UnaryOperatorNot:
  337. case SemanticsNodeKind::VarStorage:
  338. // We don't need to handle stringification for nodes that don't show up
  339. // in errors, but make it clear what's going on so that it's clearer
  340. // when stringification is needed.
  341. out << "<cannot stringify " << step.node_id << ">";
  342. break;
  343. case SemanticsNodeKind::Invalid:
  344. llvm_unreachable("SemanticsNodeKind::Invalid is never used.");
  345. }
  346. }
  347. // For `{}` or any tuple type, we've printed a non-type expression, so add a
  348. // conversion to type `type`.
  349. auto outer_node = GetNode(outer_node_id);
  350. if (outer_node.kind() == SemanticsNodeKind::TupleType ||
  351. (outer_node.kind() == SemanticsNodeKind::StructType &&
  352. GetNodeBlock(outer_node.GetAsStructType()).empty())) {
  353. out << " as type";
  354. }
  355. return str;
  356. }
  357. } // namespace Carbon