semantics_context.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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_context.h"
  5. #include <utility>
  6. #include "common/vlog.h"
  7. #include "toolchain/diagnostics/diagnostic_kind.h"
  8. #include "toolchain/lexer/tokenized_buffer.h"
  9. #include "toolchain/parser/parse_node_kind.h"
  10. #include "toolchain/semantics/semantics_declaration_name_stack.h"
  11. #include "toolchain/semantics/semantics_ir.h"
  12. #include "toolchain/semantics/semantics_node.h"
  13. #include "toolchain/semantics/semantics_node_block_stack.h"
  14. namespace Carbon {
  15. SemanticsContext::SemanticsContext(const TokenizedBuffer& tokens,
  16. DiagnosticEmitter<ParseTree::Node>& emitter,
  17. const ParseTree& parse_tree,
  18. SemanticsIR& semantics_ir,
  19. llvm::raw_ostream* vlog_stream)
  20. : tokens_(&tokens),
  21. emitter_(&emitter),
  22. parse_tree_(&parse_tree),
  23. semantics_ir_(&semantics_ir),
  24. vlog_stream_(vlog_stream),
  25. node_stack_(parse_tree, vlog_stream),
  26. node_block_stack_("node_block_stack_", semantics_ir, vlog_stream),
  27. params_or_args_stack_("params_or_args_stack_", semantics_ir, vlog_stream),
  28. args_type_info_stack_("args_type_info_stack_", semantics_ir, vlog_stream),
  29. declaration_name_stack_(this) {
  30. // Inserts the "Error" and "Type" types as "used types" so that
  31. // canonicalization can skip them. We don't emit either for lowering.
  32. canonical_types_.insert(
  33. {SemanticsNodeId::BuiltinError, SemanticsTypeId::Error});
  34. canonical_types_.insert(
  35. {SemanticsNodeId::BuiltinTypeType, SemanticsTypeId::TypeType});
  36. }
  37. auto SemanticsContext::TODO(ParseTree::Node parse_node, std::string label)
  38. -> bool {
  39. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: {0}", std::string);
  40. emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
  41. return false;
  42. }
  43. auto SemanticsContext::VerifyOnFinish() -> void {
  44. // Information in all the various context objects should be cleaned up as
  45. // various pieces of context go out of scope. At this point, nothing should
  46. // remain.
  47. // node_stack_ will still contain top-level entities.
  48. CARBON_CHECK(name_lookup_.empty()) << name_lookup_.size();
  49. CARBON_CHECK(scope_stack_.empty()) << scope_stack_.size();
  50. CARBON_CHECK(node_block_stack_.empty()) << node_block_stack_.size();
  51. CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
  52. }
  53. auto SemanticsContext::AddNode(SemanticsNode node) -> SemanticsNodeId {
  54. return AddNodeToBlock(node_block_stack_.PeekForAdd(), node);
  55. }
  56. auto SemanticsContext::AddNodeToBlock(SemanticsNodeBlockId block,
  57. SemanticsNode node) -> SemanticsNodeId {
  58. CARBON_VLOG() << "AddNode " << block << ": " << node << "\n";
  59. return semantics_ir_->AddNode(block, node);
  60. }
  61. auto SemanticsContext::AddNodeAndPush(ParseTree::Node parse_node,
  62. SemanticsNode node) -> void {
  63. auto node_id = AddNode(node);
  64. node_stack_.Push(parse_node, node_id);
  65. }
  66. auto SemanticsContext::DiagnoseDuplicateName(ParseTree::Node parse_node,
  67. SemanticsNodeId prev_def_id)
  68. -> void {
  69. CARBON_DIAGNOSTIC(NameDeclarationDuplicate, Error,
  70. "Duplicate name being declared in the same scope.");
  71. CARBON_DIAGNOSTIC(NameDeclarationPrevious, Note,
  72. "Name is previously declared here.");
  73. auto prev_def = semantics_ir_->GetNode(prev_def_id);
  74. emitter_->Build(parse_node, NameDeclarationDuplicate)
  75. .Note(prev_def.parse_node(), NameDeclarationPrevious)
  76. .Emit();
  77. }
  78. auto SemanticsContext::DiagnoseNameNotFound(ParseTree::Node parse_node,
  79. SemanticsStringId name_id) -> void {
  80. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name {0} not found", llvm::StringRef);
  81. emitter_->Emit(parse_node, NameNotFound, semantics_ir_->GetString(name_id));
  82. }
  83. auto SemanticsContext::AddNameToLookup(ParseTree::Node name_node,
  84. SemanticsStringId name_id,
  85. SemanticsNodeId target_id) -> void {
  86. if (current_scope().names.insert(name_id).second) {
  87. name_lookup_[name_id].push_back(target_id);
  88. } else {
  89. DiagnoseDuplicateName(name_node, name_lookup_[name_id].back());
  90. }
  91. }
  92. auto SemanticsContext::LookupName(ParseTree::Node parse_node,
  93. SemanticsStringId name_id,
  94. SemanticsNameScopeId scope_id,
  95. bool print_diagnostics) -> SemanticsNodeId {
  96. if (scope_id == SemanticsNameScopeId::Invalid) {
  97. auto it = name_lookup_.find(name_id);
  98. if (it == name_lookup_.end()) {
  99. if (print_diagnostics) {
  100. DiagnoseNameNotFound(parse_node, name_id);
  101. }
  102. return SemanticsNodeId::BuiltinError;
  103. }
  104. CARBON_CHECK(!it->second.empty())
  105. << "Should have been erased: " << semantics_ir_->GetString(name_id);
  106. // TODO: Check for ambiguous lookups.
  107. return it->second.back();
  108. } else {
  109. const auto& scope = semantics_ir_->GetNameScope(scope_id);
  110. auto it = scope.find(name_id);
  111. if (it == scope.end()) {
  112. if (print_diagnostics) {
  113. DiagnoseNameNotFound(parse_node, name_id);
  114. }
  115. return SemanticsNodeId::BuiltinError;
  116. }
  117. return it->second;
  118. }
  119. }
  120. auto SemanticsContext::PushScope() -> void { scope_stack_.push_back({}); }
  121. auto SemanticsContext::PopScope() -> void {
  122. auto scope = scope_stack_.pop_back_val();
  123. for (const auto& str_id : scope.names) {
  124. auto it = name_lookup_.find(str_id);
  125. if (it->second.size() == 1) {
  126. // Erase names that no longer resolve.
  127. name_lookup_.erase(it);
  128. } else {
  129. it->second.pop_back();
  130. }
  131. }
  132. }
  133. template <typename BranchNode, typename... Args>
  134. static auto AddDominatedBlockAndBranchImpl(SemanticsContext& context,
  135. ParseTree::Node parse_node,
  136. Args... args)
  137. -> SemanticsNodeBlockId {
  138. if (!context.node_block_stack().is_current_block_reachable()) {
  139. return SemanticsNodeBlockId::Unreachable;
  140. }
  141. auto block_id = context.semantics_ir().AddNodeBlock();
  142. context.AddNode(BranchNode::Make(parse_node, block_id, args...));
  143. return block_id;
  144. }
  145. auto SemanticsContext::AddDominatedBlockAndBranch(ParseTree::Node parse_node)
  146. -> SemanticsNodeBlockId {
  147. return AddDominatedBlockAndBranchImpl<SemanticsNode::Branch>(*this,
  148. parse_node);
  149. }
  150. auto SemanticsContext::AddDominatedBlockAndBranchWithArg(
  151. ParseTree::Node parse_node, SemanticsNodeId arg_id)
  152. -> SemanticsNodeBlockId {
  153. return AddDominatedBlockAndBranchImpl<SemanticsNode::BranchWithArg>(
  154. *this, parse_node, arg_id);
  155. }
  156. auto SemanticsContext::AddDominatedBlockAndBranchIf(ParseTree::Node parse_node,
  157. SemanticsNodeId cond_id)
  158. -> SemanticsNodeBlockId {
  159. return AddDominatedBlockAndBranchImpl<SemanticsNode::BranchIf>(
  160. *this, parse_node, cond_id);
  161. }
  162. auto SemanticsContext::AddConvergenceBlockAndPush(
  163. ParseTree::Node parse_node,
  164. std::initializer_list<SemanticsNodeBlockId> blocks) -> void {
  165. CARBON_CHECK(blocks.size() >= 2) << "no convergence";
  166. SemanticsNodeBlockId new_block_id = SemanticsNodeBlockId::Unreachable;
  167. for (SemanticsNodeBlockId block_id : blocks) {
  168. if (block_id != SemanticsNodeBlockId::Unreachable) {
  169. if (new_block_id == SemanticsNodeBlockId::Unreachable) {
  170. new_block_id = semantics_ir().AddNodeBlock();
  171. }
  172. AddNodeToBlock(block_id,
  173. SemanticsNode::Branch::Make(parse_node, new_block_id));
  174. }
  175. }
  176. node_block_stack().Push(new_block_id);
  177. }
  178. auto SemanticsContext::AddConvergenceBlockWithArgAndPush(
  179. ParseTree::Node parse_node,
  180. std::initializer_list<std::pair<SemanticsNodeBlockId, SemanticsNodeId>>
  181. blocks_and_args) -> SemanticsNodeId {
  182. CARBON_CHECK(blocks_and_args.size() >= 2) << "no convergence";
  183. SemanticsNodeBlockId new_block_id = SemanticsNodeBlockId::Unreachable;
  184. for (auto [block_id, arg_id] : blocks_and_args) {
  185. if (block_id != SemanticsNodeBlockId::Unreachable) {
  186. if (new_block_id == SemanticsNodeBlockId::Unreachable) {
  187. new_block_id = semantics_ir().AddNodeBlock();
  188. }
  189. AddNodeToBlock(block_id, SemanticsNode::BranchWithArg::Make(
  190. parse_node, new_block_id, arg_id));
  191. }
  192. }
  193. node_block_stack().Push(new_block_id);
  194. // Acquire the result value.
  195. SemanticsTypeId result_type_id =
  196. semantics_ir().GetNode(blocks_and_args.begin()->second).type_id();
  197. return AddNode(
  198. SemanticsNode::BlockArg::Make(parse_node, result_type_id, new_block_id));
  199. }
  200. // Add the current code block to the enclosing function.
  201. auto SemanticsContext::AddCurrentCodeBlockToFunction() -> void {
  202. CARBON_CHECK(!node_block_stack().empty()) << "no current code block";
  203. CARBON_CHECK(!return_scope_stack().empty()) << "no current function";
  204. if (!node_block_stack().is_current_block_reachable()) {
  205. // Don't include unreachable blocks in the function.
  206. return;
  207. }
  208. auto function_id = semantics_ir()
  209. .GetNode(return_scope_stack().back())
  210. .GetAsFunctionDeclaration();
  211. semantics_ir()
  212. .GetFunction(function_id)
  213. .body_block_ids.push_back(node_block_stack().PeekForAdd());
  214. }
  215. auto SemanticsContext::is_current_position_reachable() -> bool {
  216. switch (auto block_id = node_block_stack().Peek(); block_id.index) {
  217. case SemanticsNodeBlockId::Unreachable.index: {
  218. return false;
  219. }
  220. case SemanticsNodeBlockId::Invalid.index: {
  221. return true;
  222. }
  223. default: {
  224. // Our current position is at the end of a real block. That position is
  225. // reachable unless the previous instruction is a terminator instruction.
  226. const auto& block_contents = semantics_ir().GetNodeBlock(block_id);
  227. if (block_contents.empty()) {
  228. return true;
  229. }
  230. const auto& last_node = semantics_ir().GetNode(block_contents.back());
  231. return last_node.kind().terminator_kind() !=
  232. SemanticsTerminatorKind::Terminator;
  233. }
  234. }
  235. }
  236. auto SemanticsContext::ImplicitAsForArgs(
  237. SemanticsNodeBlockId arg_refs_id, ParseTree::Node param_parse_node,
  238. SemanticsNodeBlockId param_refs_id,
  239. DiagnosticEmitter<ParseTree::Node>::DiagnosticBuilder* diagnostic) -> bool {
  240. // If both arguments and parameters are empty, return quickly. Otherwise,
  241. // we'll fetch both so that errors are consistent.
  242. if (arg_refs_id == SemanticsNodeBlockId::Empty &&
  243. param_refs_id == SemanticsNodeBlockId::Empty) {
  244. return true;
  245. }
  246. auto arg_refs = semantics_ir_->GetNodeBlock(arg_refs_id);
  247. auto param_refs = semantics_ir_->GetNodeBlock(param_refs_id);
  248. // If sizes mismatch, fail early.
  249. if (arg_refs.size() != param_refs.size()) {
  250. CARBON_CHECK(diagnostic != nullptr) << "Should have validated first";
  251. CARBON_DIAGNOSTIC(CallArgCountMismatch, Note,
  252. "Function cannot be used: Received {0} argument(s), but "
  253. "require {1} argument(s).",
  254. int, int);
  255. diagnostic->Note(param_parse_node, CallArgCountMismatch, arg_refs.size(),
  256. param_refs.size());
  257. return false;
  258. }
  259. // Check type conversions per-element.
  260. // TODO: arg_ir_id is passed so that implicit conversions can be inserted.
  261. // It's currently not supported, but will be needed.
  262. for (size_t i = 0; i < arg_refs.size(); ++i) {
  263. auto value_id = arg_refs[i];
  264. auto as_type_id = semantics_ir_->GetNode(param_refs[i]).type_id();
  265. if (ImplicitAsImpl(value_id, as_type_id,
  266. diagnostic == nullptr ? &value_id : nullptr) ==
  267. ImplicitAsKind::Incompatible) {
  268. CARBON_CHECK(diagnostic != nullptr) << "Should have validated first";
  269. CARBON_DIAGNOSTIC(CallArgTypeMismatch, Note,
  270. "Function cannot be used: Cannot implicityly convert "
  271. "argument {0} from `{1}` to `{2}`.",
  272. size_t, std::string, std::string);
  273. diagnostic->Note(param_parse_node, CallArgTypeMismatch, i,
  274. semantics_ir_->StringifyType(
  275. semantics_ir_->GetNode(value_id).type_id()),
  276. semantics_ir_->StringifyType(as_type_id));
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. auto SemanticsContext::ImplicitAsRequired(ParseTree::Node parse_node,
  283. SemanticsNodeId value_id,
  284. SemanticsTypeId as_type_id)
  285. -> SemanticsNodeId {
  286. SemanticsNodeId output_value_id = value_id;
  287. if (ImplicitAsImpl(value_id, as_type_id, &output_value_id) ==
  288. ImplicitAsKind::Incompatible) {
  289. // Only error when the system is trying to use the result.
  290. CARBON_DIAGNOSTIC(ImplicitAsConversionFailure, Error,
  291. "Cannot implicitly convert from `{0}` to `{1}`.",
  292. std::string, std::string);
  293. emitter_
  294. ->Build(parse_node, ImplicitAsConversionFailure,
  295. semantics_ir_->StringifyType(
  296. semantics_ir_->GetNode(value_id).type_id()),
  297. semantics_ir_->StringifyType(as_type_id))
  298. .Emit();
  299. }
  300. return output_value_id;
  301. }
  302. auto SemanticsContext::ImplicitAsBool(ParseTree::Node parse_node,
  303. SemanticsNodeId value_id)
  304. -> SemanticsNodeId {
  305. return ImplicitAsRequired(parse_node, value_id,
  306. CanonicalizeType(SemanticsNodeId::BuiltinBoolType));
  307. }
  308. auto SemanticsContext::ImplicitAsImpl(SemanticsNodeId value_id,
  309. SemanticsTypeId as_type_id,
  310. SemanticsNodeId* output_value_id)
  311. -> ImplicitAsKind {
  312. // Start by making sure both sides are valid. If any part is invalid, the
  313. // result is invalid and we shouldn't error.
  314. if (value_id == SemanticsNodeId::BuiltinError) {
  315. // If the value is invalid, we can't do much, but do "succeed".
  316. return ImplicitAsKind::Identical;
  317. }
  318. auto value = semantics_ir_->GetNode(value_id);
  319. auto value_type_id = value.type_id();
  320. if (value_type_id == SemanticsTypeId::Error) {
  321. return ImplicitAsKind::Identical;
  322. }
  323. if (as_type_id == SemanticsTypeId::Error) {
  324. // Although the target type is invalid, this still changes the value.
  325. if (output_value_id != nullptr) {
  326. *output_value_id = SemanticsNodeId::BuiltinError;
  327. }
  328. return ImplicitAsKind::Compatible;
  329. }
  330. if (value_type_id == as_type_id) {
  331. // Type doesn't need to change.
  332. return ImplicitAsKind::Identical;
  333. }
  334. if (as_type_id == SemanticsTypeId::TypeType) {
  335. // TODO: When converting `()` to a type, the result is `() as Type`.
  336. // Right now there is no tuple value support.
  337. // When converting `{}` to a type, the result is `{} as Type`.
  338. if (value.kind() == SemanticsNodeKind::StructValue &&
  339. value.GetAsStructValue() == SemanticsNodeBlockId::Empty) {
  340. if (output_value_id != nullptr) {
  341. *output_value_id = semantics_ir_->GetType(value_type_id);
  342. }
  343. return ImplicitAsKind::Compatible;
  344. }
  345. }
  346. // TODO: Handle ImplicitAs for compatible structs and tuples.
  347. if (output_value_id != nullptr) {
  348. *output_value_id = SemanticsNodeId::BuiltinError;
  349. }
  350. return ImplicitAsKind::Incompatible;
  351. }
  352. auto SemanticsContext::ParamOrArgStart() -> void {
  353. params_or_args_stack_.Push();
  354. }
  355. auto SemanticsContext::ParamOrArgComma(bool for_args) -> void {
  356. ParamOrArgSave(for_args);
  357. }
  358. auto SemanticsContext::ParamOrArgEnd(bool for_args, ParseNodeKind start_kind)
  359. -> SemanticsNodeBlockId {
  360. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  361. ParamOrArgSave(for_args);
  362. }
  363. return params_or_args_stack_.Pop();
  364. }
  365. auto SemanticsContext::ParamOrArgSave(bool for_args) -> void {
  366. SemanticsNodeId param_or_arg_id = SemanticsNodeId::Invalid;
  367. if (for_args) {
  368. // For an argument, we add a stub reference to the expression on the top of
  369. // the stack. There may not be anything on the IR prior to this.
  370. auto [entry_parse_node, entry_node_id] =
  371. node_stack_.PopExpressionWithParseNode();
  372. param_or_arg_id = AddNode(SemanticsNode::StubReference::Make(
  373. entry_parse_node, semantics_ir_->GetNode(entry_node_id).type_id(),
  374. entry_node_id));
  375. } else {
  376. // For a parameter, there should always be something in the IR.
  377. node_stack_.PopAndIgnore();
  378. auto ir_id = node_block_stack_.Peek();
  379. CARBON_CHECK(ir_id.is_valid());
  380. auto& ir = semantics_ir_->GetNodeBlock(ir_id);
  381. CARBON_CHECK(!ir.empty()) << "Should have had a param";
  382. param_or_arg_id = ir.back();
  383. }
  384. // Save the param or arg ID.
  385. auto& params_or_args =
  386. semantics_ir_->GetNodeBlock(params_or_args_stack_.PeekForAdd());
  387. params_or_args.push_back(param_or_arg_id);
  388. }
  389. auto SemanticsContext::CanonicalizeType(SemanticsNodeId node_id)
  390. -> SemanticsTypeId {
  391. auto it = canonical_types_.find(node_id);
  392. if (it != canonical_types_.end()) {
  393. return it->second;
  394. }
  395. auto type_id = semantics_ir_->AddType(node_id);
  396. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  397. return type_id;
  398. }
  399. auto SemanticsContext::CanonicalizeStructType(ParseTree::Node parse_node,
  400. SemanticsNodeBlockId refs_id)
  401. -> SemanticsTypeId {
  402. // Construct the field structure for lookup.
  403. auto refs = semantics_ir_->GetNodeBlock(refs_id);
  404. llvm::FoldingSetNodeID canonical_id;
  405. for (const auto& ref_id : refs) {
  406. auto ref = semantics_ir_->GetNode(ref_id);
  407. canonical_id.AddInteger(ref.GetAsStructTypeField().index);
  408. canonical_id.AddInteger(ref.type_id().index);
  409. }
  410. // If a struct with matching fields was already created, reuse it.
  411. void* insert_pos;
  412. auto* node =
  413. canonical_struct_types_.FindNodeOrInsertPos(canonical_id, insert_pos);
  414. if (node != nullptr) {
  415. return node->type_id();
  416. }
  417. // The struct doesn't already exist, so create and store it as canonical.
  418. auto node_id = AddNode(SemanticsNode::StructType::Make(
  419. parse_node, SemanticsTypeId::TypeType, refs_id));
  420. auto type_id = semantics_ir_->AddType(node_id);
  421. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  422. canonical_struct_types_nodes_.push_back(
  423. std::make_unique<StructTypeNode>(canonical_id, type_id));
  424. canonical_struct_types_.InsertNode(canonical_struct_types_nodes_.back().get(),
  425. insert_pos);
  426. return type_id;
  427. }
  428. auto SemanticsContext::PrintForStackDump(llvm::raw_ostream& output) const
  429. -> void {
  430. node_stack_.PrintForStackDump(output);
  431. node_block_stack_.PrintForStackDump(output);
  432. params_or_args_stack_.PrintForStackDump(output);
  433. args_type_info_stack_.PrintForStackDump(output);
  434. }
  435. } // namespace Carbon