semantics_context.cpp 18 KB

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