context.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. auto Context::FollowNameReferences(SemIR::NodeId node_id) -> SemIR::NodeId {
  130. while (true) {
  131. auto node = semantics_ir().GetNode(node_id);
  132. switch (node.kind()) {
  133. case SemIR::NodeKind::NameReference: {
  134. node_id = node.As<SemIR::NameReference>().value_id;
  135. break;
  136. }
  137. case SemIR::NodeKind::NameReferenceUntyped: {
  138. node_id = node.As<SemIR::NameReferenceUntyped>().value_id;
  139. break;
  140. }
  141. default:
  142. return node_id;
  143. }
  144. }
  145. }
  146. template <typename BranchNode, typename... Args>
  147. static auto AddDominatedBlockAndBranchImpl(Context& context,
  148. Parse::Node parse_node, Args... args)
  149. -> SemIR::NodeBlockId {
  150. if (!context.node_block_stack().is_current_block_reachable()) {
  151. return SemIR::NodeBlockId::Unreachable;
  152. }
  153. auto block_id = context.semantics_ir().AddNodeBlockId();
  154. context.AddNode(BranchNode(parse_node, block_id, args...));
  155. return block_id;
  156. }
  157. auto Context::AddDominatedBlockAndBranch(Parse::Node parse_node)
  158. -> SemIR::NodeBlockId {
  159. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, parse_node);
  160. }
  161. auto Context::AddDominatedBlockAndBranchWithArg(Parse::Node parse_node,
  162. SemIR::NodeId arg_id)
  163. -> SemIR::NodeBlockId {
  164. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, parse_node,
  165. arg_id);
  166. }
  167. auto Context::AddDominatedBlockAndBranchIf(Parse::Node parse_node,
  168. SemIR::NodeId cond_id)
  169. -> SemIR::NodeBlockId {
  170. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, parse_node,
  171. cond_id);
  172. }
  173. auto Context::AddConvergenceBlockAndPush(Parse::Node parse_node, int num_blocks)
  174. -> void {
  175. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  176. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  177. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  178. if (node_block_stack().is_current_block_reachable()) {
  179. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  180. new_block_id = semantics_ir().AddNodeBlockId();
  181. }
  182. AddNode(SemIR::Branch(parse_node, new_block_id));
  183. }
  184. node_block_stack().Pop();
  185. }
  186. node_block_stack().Push(new_block_id);
  187. }
  188. auto Context::AddConvergenceBlockWithArgAndPush(
  189. Parse::Node parse_node, std::initializer_list<SemIR::NodeId> block_args)
  190. -> SemIR::NodeId {
  191. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  192. SemIR::NodeBlockId new_block_id = SemIR::NodeBlockId::Unreachable;
  193. for (auto arg_id : block_args) {
  194. if (node_block_stack().is_current_block_reachable()) {
  195. if (new_block_id == SemIR::NodeBlockId::Unreachable) {
  196. new_block_id = semantics_ir().AddNodeBlockId();
  197. }
  198. AddNode(SemIR::BranchWithArg(parse_node, new_block_id, arg_id));
  199. }
  200. node_block_stack().Pop();
  201. }
  202. node_block_stack().Push(new_block_id);
  203. // Acquire the result value.
  204. SemIR::TypeId result_type_id =
  205. semantics_ir().GetNode(*block_args.begin()).type_id();
  206. return AddNode(SemIR::BlockArg(parse_node, result_type_id, new_block_id));
  207. }
  208. // Add the current code block to the enclosing function.
  209. auto Context::AddCurrentCodeBlockToFunction() -> void {
  210. CARBON_CHECK(!node_block_stack().empty()) << "no current code block";
  211. CARBON_CHECK(!return_scope_stack().empty()) << "no current function";
  212. if (!node_block_stack().is_current_block_reachable()) {
  213. // Don't include unreachable blocks in the function.
  214. return;
  215. }
  216. auto function_id =
  217. semantics_ir()
  218. .GetNodeAs<SemIR::FunctionDeclaration>(return_scope_stack().back())
  219. .function_id;
  220. semantics_ir()
  221. .GetFunction(function_id)
  222. .body_block_ids.push_back(node_block_stack().PeekOrAdd());
  223. }
  224. auto Context::is_current_position_reachable() -> bool {
  225. if (!node_block_stack().is_current_block_reachable()) {
  226. return false;
  227. }
  228. // Our current position is at the end of a reachable block. That position is
  229. // reachable unless the previous instruction is a terminator instruction.
  230. auto block_contents = node_block_stack().PeekCurrentBlockContents();
  231. if (block_contents.empty()) {
  232. return true;
  233. }
  234. const auto& last_node = semantics_ir().GetNode(block_contents.back());
  235. return last_node.kind().terminator_kind() !=
  236. SemIR::TerminatorKind::Terminator;
  237. }
  238. auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
  239. auto Context::ParamOrArgComma() -> void {
  240. ParamOrArgSave(node_stack_.PopExpression());
  241. }
  242. auto Context::ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void {
  243. if (parse_tree_->node_kind(node_stack_.PeekParseNode()) != start_kind) {
  244. ParamOrArgSave(node_stack_.PopExpression());
  245. }
  246. }
  247. auto Context::ParamOrArgPop() -> SemIR::NodeBlockId {
  248. return params_or_args_stack_.Pop();
  249. }
  250. auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::NodeBlockId {
  251. ParamOrArgEndNoPop(start_kind);
  252. return ParamOrArgPop();
  253. }
  254. auto Context::CanonicalizeTypeImpl(
  255. SemIR::NodeKind kind,
  256. llvm::function_ref<void(llvm::FoldingSetNodeID& canonical_id)> profile_type,
  257. llvm::function_ref<SemIR::NodeId()> make_node) -> SemIR::TypeId {
  258. llvm::FoldingSetNodeID canonical_id;
  259. kind.Profile(canonical_id);
  260. profile_type(canonical_id);
  261. void* insert_pos;
  262. auto* node =
  263. canonical_type_nodes_.FindNodeOrInsertPos(canonical_id, insert_pos);
  264. if (node != nullptr) {
  265. return node->type_id();
  266. }
  267. auto node_id = make_node();
  268. auto type_id = semantics_ir_->AddType(node_id);
  269. CARBON_CHECK(canonical_types_.insert({node_id, type_id}).second);
  270. type_node_storage_.push_back(
  271. std::make_unique<TypeNode>(canonical_id, type_id));
  272. // In a debug build, check that our insertion position is still valid. It
  273. // could have been invalidated by a misbehaving `make_node`.
  274. CARBON_DCHECK([&] {
  275. void* check_insert_pos;
  276. auto* check_node = canonical_type_nodes_.FindNodeOrInsertPos(
  277. canonical_id, check_insert_pos);
  278. return !check_node && insert_pos == check_insert_pos;
  279. }()) << "Type was created recursively during canonicalization";
  280. canonical_type_nodes_.InsertNode(type_node_storage_.back().get(), insert_pos);
  281. return type_id;
  282. }
  283. // Compute a fingerprint for a tuple type, for use as a key in a folding set.
  284. static auto ProfileTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids,
  285. llvm::FoldingSetNodeID& canonical_id) -> void {
  286. for (auto type_id : type_ids) {
  287. canonical_id.AddInteger(type_id.index);
  288. }
  289. }
  290. // Compute a fingerprint for a type, for use as a key in a folding set.
  291. static auto ProfileType(Context& semantics_context, SemIR::Node node,
  292. llvm::FoldingSetNodeID& canonical_id) -> void {
  293. switch (node.kind()) {
  294. case SemIR::NodeKind::ArrayType: {
  295. auto array_type = node.As<SemIR::ArrayType>();
  296. canonical_id.AddInteger(
  297. semantics_context.semantics_ir().GetArrayBoundValue(
  298. array_type.bound_id));
  299. canonical_id.AddInteger(array_type.element_type_id.index);
  300. break;
  301. }
  302. case SemIR::NodeKind::Builtin:
  303. canonical_id.AddInteger(node.As<SemIR::Builtin>().builtin_kind.AsInt());
  304. break;
  305. case SemIR::NodeKind::CrossReference: {
  306. // TODO: Cross-references should be canonicalized by looking at their
  307. // target rather than treating them as new unique types.
  308. auto xref = node.As<SemIR::CrossReference>();
  309. canonical_id.AddInteger(xref.ir_id.index);
  310. canonical_id.AddInteger(xref.node_id.index);
  311. break;
  312. }
  313. case SemIR::NodeKind::ConstType:
  314. canonical_id.AddInteger(
  315. semantics_context
  316. .GetUnqualifiedType(node.As<SemIR::ConstType>().inner_id)
  317. .index);
  318. break;
  319. case SemIR::NodeKind::PointerType:
  320. canonical_id.AddInteger(node.As<SemIR::PointerType>().pointee_id.index);
  321. break;
  322. case SemIR::NodeKind::StructType: {
  323. auto fields = semantics_context.semantics_ir().GetNodeBlock(
  324. node.As<SemIR::StructType>().fields_id);
  325. for (const auto& field_id : fields) {
  326. auto field =
  327. semantics_context.semantics_ir().GetNodeAs<SemIR::StructTypeField>(
  328. field_id);
  329. canonical_id.AddInteger(field.name_id.index);
  330. canonical_id.AddInteger(field.type_id.index);
  331. }
  332. break;
  333. }
  334. case SemIR::NodeKind::TupleType:
  335. ProfileTupleType(semantics_context.semantics_ir().GetTypeBlock(
  336. node.As<SemIR::TupleType>().elements_id),
  337. canonical_id);
  338. break;
  339. default:
  340. CARBON_FATAL() << "Unexpected type node " << node;
  341. }
  342. }
  343. auto Context::CanonicalizeTypeAndAddNodeIfNew(SemIR::Node node)
  344. -> SemIR::TypeId {
  345. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  346. ProfileType(*this, node, canonical_id);
  347. };
  348. auto make_node = [&] { return AddNode(node); };
  349. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  350. }
  351. auto Context::CanonicalizeType(SemIR::NodeId node_id) -> SemIR::TypeId {
  352. auto it = canonical_types_.find(node_id);
  353. if (it != canonical_types_.end()) {
  354. return it->second;
  355. }
  356. auto node = semantics_ir_->GetNode(node_id);
  357. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  358. ProfileType(*this, node, canonical_id);
  359. };
  360. auto make_node = [&] { return node_id; };
  361. return CanonicalizeTypeImpl(node.kind(), profile_node, make_node);
  362. }
  363. auto Context::CanonicalizeStructType(Parse::Node parse_node,
  364. SemIR::NodeBlockId refs_id)
  365. -> SemIR::TypeId {
  366. return CanonicalizeTypeAndAddNodeIfNew(
  367. SemIR::StructType(parse_node, SemIR::TypeId::TypeType, refs_id));
  368. }
  369. auto Context::CanonicalizeTupleType(Parse::Node parse_node,
  370. llvm::ArrayRef<SemIR::TypeId> type_ids)
  371. -> SemIR::TypeId {
  372. // Defer allocating a SemIR::TypeBlockId until we know this is a new type.
  373. auto profile_tuple = [&](llvm::FoldingSetNodeID& canonical_id) {
  374. ProfileTupleType(type_ids, canonical_id);
  375. };
  376. auto make_tuple_node = [&] {
  377. return AddNode(SemIR::TupleType(parse_node, SemIR::TypeId::TypeType,
  378. semantics_ir_->AddTypeBlock(type_ids)));
  379. };
  380. return CanonicalizeTypeImpl(SemIR::NodeKind::TupleType, profile_tuple,
  381. make_tuple_node);
  382. }
  383. auto Context::GetPointerType(Parse::Node parse_node,
  384. SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  385. return CanonicalizeTypeAndAddNodeIfNew(
  386. SemIR::PointerType(parse_node, SemIR::TypeId::TypeType, pointee_type_id));
  387. }
  388. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  389. SemIR::Node type_node =
  390. semantics_ir_->GetNode(semantics_ir_->GetTypeAllowBuiltinTypes(type_id));
  391. if (auto const_type = type_node.TryAs<SemIR::ConstType>()) {
  392. return const_type->inner_id;
  393. }
  394. return type_id;
  395. }
  396. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  397. node_stack_.PrintForStackDump(output);
  398. node_block_stack_.PrintForStackDump(output);
  399. params_or_args_stack_.PrintForStackDump(output);
  400. args_type_info_stack_.PrintForStackDump(output);
  401. }
  402. } // namespace Carbon::Check