semantics_ir.cpp 18 KB

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