context.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/check/context.h"
  5. #include <string>
  6. #include <utility>
  7. #include "common/check.h"
  8. #include "common/vlog.h"
  9. #include "llvm/ADT/Sequence.h"
  10. #include "toolchain/check/declaration_name_stack.h"
  11. #include "toolchain/check/node_block_stack.h"
  12. #include "toolchain/diagnostics/diagnostic_kind.h"
  13. #include "toolchain/lex/tokenized_buffer.h"
  14. #include "toolchain/parse/node_kind.h"
  15. #include "toolchain/sem_ir/file.h"
  16. #include "toolchain/sem_ir/node.h"
  17. #include "toolchain/sem_ir/node_kind.h"
  18. namespace Carbon::Check {
  19. Context::Context(const Lex::TokenizedBuffer& tokens,
  20. DiagnosticEmitter<Parse::Node>& emitter,
  21. const Parse::Tree& parse_tree, SemIR::File& semantics_ir,
  22. llvm::raw_ostream* vlog_stream)
  23. : tokens_(&tokens),
  24. emitter_(&emitter),
  25. parse_tree_(&parse_tree),
  26. semantics_ir_(&semantics_ir),
  27. vlog_stream_(vlog_stream),
  28. node_stack_(parse_tree, vlog_stream),
  29. node_block_stack_("node_block_stack_", semantics_ir, vlog_stream),
  30. params_or_args_stack_("params_or_args_stack_", semantics_ir, vlog_stream),
  31. args_type_info_stack_("args_type_info_stack_", semantics_ir, vlog_stream),
  32. declaration_name_stack_(this) {
  33. // Inserts the "Error" and "Type" types as "used types" so that
  34. // canonicalization can skip them. We don't emit either for lowering.
  35. canonical_types_.insert({SemIR::NodeId::BuiltinError, SemIR::TypeId::Error});
  36. canonical_types_.insert(
  37. {SemIR::NodeId::BuiltinTypeType, SemIR::TypeId::TypeType});
  38. }
  39. auto Context::TODO(Parse::Node parse_node, std::string label) -> bool {
  40. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: `{0}`.",
  41. std::string);
  42. emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
  43. return false;
  44. }
  45. auto Context::VerifyOnFinish() -> void {
  46. // Information in all the various context objects should be cleaned up as
  47. // various pieces of context go out of scope. At this point, nothing should
  48. // remain.
  49. // node_stack_ will still contain top-level entities.
  50. CARBON_CHECK(name_lookup_.empty()) << name_lookup_.size();
  51. CARBON_CHECK(scope_stack_.empty()) << scope_stack_.size();
  52. CARBON_CHECK(node_block_stack_.empty()) << node_block_stack_.size();
  53. CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
  54. }
  55. auto Context::AddNode(SemIR::Node node) -> SemIR::NodeId {
  56. auto node_id = node_block_stack_.AddNode(node);
  57. CARBON_VLOG() << "AddNode: " << node << "\n";
  58. return node_id;
  59. }
  60. auto Context::AddNodeAndPush(Parse::Node parse_node, SemIR::Node node) -> void {
  61. auto node_id = AddNode(node);
  62. node_stack_.Push(parse_node, node_id);
  63. }
  64. auto Context::DiagnoseDuplicateName(Parse::Node parse_node,
  65. SemIR::NodeId prev_def_id) -> void {
  66. CARBON_DIAGNOSTIC(NameDeclarationDuplicate, Error,
  67. "Duplicate name being declared in the same scope.");
  68. CARBON_DIAGNOSTIC(NameDeclarationPrevious, Note,
  69. "Name is previously declared here.");
  70. auto prev_def = semantics_ir_->GetNode(prev_def_id);
  71. emitter_->Build(parse_node, NameDeclarationDuplicate)
  72. .Note(prev_def.parse_node(), NameDeclarationPrevious)
  73. .Emit();
  74. }
  75. auto Context::DiagnoseNameNotFound(Parse::Node parse_node,
  76. SemIR::StringId name_id) -> void {
  77. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
  78. llvm::StringRef);
  79. emitter_->Emit(parse_node, NameNotFound, semantics_ir_->GetString(name_id));
  80. }
  81. auto Context::AddNameToLookup(Parse::Node name_node, SemIR::StringId name_id,
  82. SemIR::NodeId target_id) -> void {
  83. if (current_scope().names.insert(name_id).second) {
  84. name_lookup_[name_id].push_back(target_id);
  85. } else {
  86. DiagnoseDuplicateName(name_node, name_lookup_[name_id].back());
  87. }
  88. }
  89. auto Context::LookupName(Parse::Node parse_node, SemIR::StringId name_id,
  90. SemIR::NameScopeId scope_id, bool print_diagnostics)
  91. -> SemIR::NodeId {
  92. if (scope_id == SemIR::NameScopeId::Invalid) {
  93. auto it = name_lookup_.find(name_id);
  94. if (it == name_lookup_.end()) {
  95. if (print_diagnostics) {
  96. DiagnoseNameNotFound(parse_node, name_id);
  97. }
  98. return SemIR::NodeId::BuiltinError;
  99. }
  100. CARBON_CHECK(!it->second.empty())
  101. << "Should have been erased: " << semantics_ir_->GetString(name_id);
  102. // TODO: Check for ambiguous lookups.
  103. return it->second.back();
  104. } else {
  105. const auto& scope = semantics_ir_->GetNameScope(scope_id);
  106. auto it = scope.find(name_id);
  107. if (it == scope.end()) {
  108. if (print_diagnostics) {
  109. DiagnoseNameNotFound(parse_node, name_id);
  110. }
  111. return SemIR::NodeId::BuiltinError;
  112. }
  113. return it->second;
  114. }
  115. }
  116. auto Context::PushScope() -> void { scope_stack_.push_back({}); }
  117. auto Context::PopScope() -> void {
  118. auto scope = scope_stack_.pop_back_val();
  119. for (const auto& str_id : scope.names) {
  120. auto it = name_lookup_.find(str_id);
  121. if (it->second.size() == 1) {
  122. // Erase names that no longer resolve.
  123. name_lookup_.erase(it);
  124. } else {
  125. it->second.pop_back();
  126. }
  127. }
  128. }
  129. template <typename BranchNode, typename... Args>
  130. static auto AddDominatedBlockAndBranchImpl(Context& context,
  131. Parse::Node parse_node, Args... args)
  132. -> SemIR::NodeBlockId {
  133. if (!context.node_block_stack().is_current_block_reachable()) {
  134. return SemIR::NodeBlockId::Unreachable;
  135. }
  136. auto block_id = context.semantics_ir().AddNodeBlockId();
  137. context.AddNode(BranchNode::Make(parse_node, block_id, args...));
  138. return block_id;
  139. }
  140. auto Context::AddDominatedBlockAndBranch(Parse::Node parse_node)
  141. -> SemIR::NodeBlockId {
  142. return AddDominatedBlockAndBranchImpl<SemIR::Node::Branch>(*this, parse_node);
  143. }
  144. auto Context::AddDominatedBlockAndBranchWithArg(Parse::Node parse_node,
  145. SemIR::NodeId arg_id)
  146. -> SemIR::NodeBlockId {
  147. return AddDominatedBlockAndBranchImpl<SemIR::Node::BranchWithArg>(
  148. *this, parse_node, arg_id);
  149. }
  150. auto Context::AddDominatedBlockAndBranchIf(Parse::Node parse_node,
  151. SemIR::NodeId cond_id)
  152. -> SemIR::NodeBlockId {
  153. return AddDominatedBlockAndBranchImpl<SemIR::Node::BranchIf>(
  154. *this, parse_node, cond_id);
  155. }
  156. auto Context::AddConvergenceBlockAndPush(Parse::Node parse_node, int num_blocks)
  157. -> void {
  158. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  159. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  160. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  161. if (node_block_stack().is_current_block_reachable()) {
  162. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  163. new_block_id = semantics_ir().AddNodeBlockId();
  164. }
  165. AddNode(SemIR::Node::Branch::Make(parse_node, new_block_id));
  166. }
  167. node_block_stack().Pop();
  168. }
  169. node_block_stack().Push(new_block_id);
  170. }
  171. auto Context::AddConvergenceBlockWithArgAndPush(
  172. Parse::Node parse_node, std::initializer_list<SemIR::NodeId> block_args)
  173. -> SemIR::NodeId {
  174. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  175. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  176. for (auto arg_id : block_args) {
  177. if (node_block_stack().is_current_block_reachable()) {
  178. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  179. new_block_id = semantics_ir().AddNodeBlockId();
  180. }
  181. AddNode(
  182. SemIR::Node::BranchWithArg::Make(parse_node, new_block_id, arg_id));
  183. }
  184. node_block_stack().Pop();
  185. }
  186. node_block_stack().Push(new_block_id);
  187. // Acquire the result value.
  188. SemIR::TypeId result_type_id =
  189. semantics_ir().GetNode(*block_args.begin()).type_id();
  190. return AddNode(
  191. SemIR::Node::BlockArg::Make(parse_node, result_type_id, new_block_id));
  192. }
  193. // Add the current code block to the enclosing function.
  194. auto Context::AddCurrentCodeBlockToFunction() -> void {
  195. CARBON_CHECK(!node_block_stack().empty()) << "no current code block";
  196. CARBON_CHECK(!return_scope_stack().empty()) << "no current function";
  197. if (!node_block_stack().is_current_block_reachable()) {
  198. // Don't include unreachable blocks in the function.
  199. return;
  200. }
  201. auto function_id = semantics_ir()
  202. .GetNode(return_scope_stack().back())
  203. .GetAsFunctionDeclaration();
  204. semantics_ir()
  205. .GetFunction(function_id)
  206. .body_block_ids.push_back(node_block_stack().PeekOrAdd());
  207. }
  208. auto Context::is_current_position_reachable() -> bool {
  209. if (!node_block_stack().is_current_block_reachable()) {
  210. return false;
  211. }
  212. // Our current position is at the end of a reachable block. That position is
  213. // reachable unless the previous instruction is a terminator instruction.
  214. auto block_contents = node_block_stack().PeekCurrentBlockContents();
  215. if (block_contents.empty()) {
  216. return true;
  217. }
  218. const auto& last_node = semantics_ir().GetNode(block_contents.back());
  219. return last_node.kind().terminator_kind() !=
  220. SemIR::TerminatorKind::Terminator;
  221. }
  222. auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
  223. auto Context::ParamOrArgComma() -> void {
  224. ParamOrArgSave(node_stack_.PopExpression());
  225. }
  226. auto Context::ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void {
  227. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  228. ParamOrArgSave(node_stack_.PopExpression());
  229. }
  230. }
  231. auto Context::ParamOrArgPop() -> SemIR::NodeBlockId {
  232. return params_or_args_stack_.Pop();
  233. }
  234. auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::NodeBlockId {
  235. ParamOrArgEndNoPop(start_kind);
  236. return ParamOrArgPop();
  237. }
  238. auto Context::CanonicalizeTypeImpl(
  239. SemIR::NodeKind kind,
  240. llvm::function_ref<void(llvm::FoldingSetNodeID& canonical_id)> profile_type,
  241. llvm::function_ref<SemIR::NodeId()> make_node) -> SemIR::TypeId {
  242. llvm::FoldingSetNodeID canonical_id;
  243. kind.Profile(canonical_id);
  244. profile_type(canonical_id);
  245. void* insert_pos;
  246. auto* node =
  247. canonical_type_nodes_.FindNodeOrInsertPos(canonical_id, insert_pos);
  248. if (node != nullptr) {
  249. return node->type_id();
  250. }
  251. auto node_id = make_node();
  252. auto type_id = semantics_ir_->AddType(node_id);
  253. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  254. type_node_storage_.push_back(
  255. std::make_unique<TypeNode>(canonical_id, type_id));
  256. // In a debug build, check that our insertion position is still valid. It
  257. // could have been invalidated by a misbehaving `make_node`.
  258. CARBON_DCHECK([&] {
  259. void* check_insert_pos;
  260. auto* check_node = canonical_type_nodes_.FindNodeOrInsertPos(
  261. canonical_id, check_insert_pos);
  262. return !check_node && insert_pos == check_insert_pos;
  263. }()) << "Type was created recursively during canonicalization";
  264. canonical_type_nodes_.InsertNode(type_node_storage_.back().get(), insert_pos);
  265. return type_id;
  266. }
  267. // Compute a fingerprint for a tuple type, for use as a key in a folding set.
  268. static auto ProfileTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids,
  269. llvm::FoldingSetNodeID& canonical_id) -> void {
  270. for (auto type_id : type_ids) {
  271. canonical_id.AddInteger(type_id.index);
  272. }
  273. }
  274. // Compute a fingerprint for a type, for use as a key in a folding set.
  275. static auto ProfileType(Context& semantics_context, SemIR::Node node,
  276. llvm::FoldingSetNodeID& canonical_id) -> void {
  277. switch (node.kind()) {
  278. case SemIR::NodeKind::ArrayType: {
  279. auto [bound_id, element_type_id] = node.GetAsArrayType();
  280. canonical_id.AddInteger(
  281. semantics_context.semantics_ir().GetArrayBoundValue(bound_id));
  282. canonical_id.AddInteger(element_type_id.index);
  283. break;
  284. }
  285. case SemIR::NodeKind::Builtin:
  286. canonical_id.AddInteger(node.GetAsBuiltin().AsInt());
  287. break;
  288. case SemIR::NodeKind::CrossReference: {
  289. // TODO: Cross-references should be canonicalized by looking at their
  290. // target rather than treating them as new unique types.
  291. auto [xref_id, node_id] = node.GetAsCrossReference();
  292. canonical_id.AddInteger(xref_id.index);
  293. canonical_id.AddInteger(node_id.index);
  294. break;
  295. }
  296. case SemIR::NodeKind::ConstType:
  297. canonical_id.AddInteger(
  298. semantics_context.GetUnqualifiedType(node.GetAsConstType()).index);
  299. break;
  300. case SemIR::NodeKind::PointerType:
  301. canonical_id.AddInteger(node.GetAsPointerType().index);
  302. break;
  303. case SemIR::NodeKind::StructType: {
  304. auto refs =
  305. semantics_context.semantics_ir().GetNodeBlock(node.GetAsStructType());
  306. for (const auto& ref_id : refs) {
  307. auto ref = semantics_context.semantics_ir().GetNode(ref_id);
  308. auto [name_id, type_id] = ref.GetAsStructTypeField();
  309. canonical_id.AddInteger(name_id.index);
  310. canonical_id.AddInteger(type_id.index);
  311. }
  312. break;
  313. }
  314. case SemIR::NodeKind::TupleType:
  315. ProfileTupleType(
  316. semantics_context.semantics_ir().GetTypeBlock(node.GetAsTupleType()),
  317. canonical_id);
  318. break;
  319. default:
  320. CARBON_FATAL() << "Unexpected type node " << node;
  321. }
  322. }
  323. auto Context::CanonicalizeTypeAndAddNodeIfNew(SemIR::Node node)
  324. -> SemIR::TypeId {
  325. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  326. ProfileType(*this, node, canonical_id);
  327. };
  328. auto make_node = [&] { return AddNode(node); };
  329. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  330. }
  331. auto Context::CanonicalizeType(SemIR::NodeId node_id) -> SemIR::TypeId {
  332. auto it = canonical_types_.find(node_id);
  333. if (it != canonical_types_.end()) {
  334. return it->second;
  335. }
  336. auto node = semantics_ir_->GetNode(node_id);
  337. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  338. ProfileType(*this, node, canonical_id);
  339. };
  340. auto make_node = [&] { return node_id; };
  341. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  342. }
  343. auto Context::CanonicalizeStructType(Parse::Node parse_node,
  344. SemIR::NodeBlockId refs_id)
  345. -> SemIR::TypeId {
  346. return CanonicalizeTypeAndAddNodeIfNew(SemIR::Node::StructType::Make(
  347. parse_node, SemIR::TypeId::TypeType, refs_id));
  348. }
  349. auto Context::CanonicalizeTupleType(Parse::Node parse_node,
  350. llvm::ArrayRef<SemIR::TypeId> type_ids)
  351. -> SemIR::TypeId {
  352. // Defer allocating a SemIR::TypeBlockId until we know this is a new type.
  353. auto profile_tuple = [&](llvm::FoldingSetNodeID& canonical_id) {
  354. ProfileTupleType(type_ids, canonical_id);
  355. };
  356. auto make_tuple_node = [&] {
  357. return AddNode(
  358. SemIR::Node::TupleType::Make(parse_node, SemIR::TypeId::TypeType,
  359. semantics_ir_->AddTypeBlock(type_ids)));
  360. };
  361. return CanonicalizeTypeImpl(SemIR::NodeKind::TupleType, profile_tuple,
  362. make_tuple_node);
  363. }
  364. auto Context::GetPointerType(Parse::Node parse_node,
  365. SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  366. return CanonicalizeTypeAndAddNodeIfNew(SemIR::Node::PointerType::Make(
  367. parse_node, SemIR::TypeId::TypeType, pointee_type_id));
  368. }
  369. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  370. SemIR::Node type_node =
  371. semantics_ir_->GetNode(semantics_ir_->GetTypeAllowBuiltinTypes(type_id));
  372. if (type_node.kind() == SemIR::NodeKind::ConstType) {
  373. return type_node.GetAsConstType();
  374. }
  375. return type_id;
  376. }
  377. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  378. node_stack_.PrintForStackDump(output);
  379. node_block_stack_.PrintForStackDump(output);
  380. params_or_args_stack_.PrintForStackDump(output);
  381. args_type_info_stack_.PrintForStackDump(output);
  382. }
  383. } // namespace Carbon::Check