context.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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/decl_name_stack.h"
  11. #include "toolchain/check/eval.h"
  12. #include "toolchain/check/import_ref.h"
  13. #include "toolchain/check/inst_block_stack.h"
  14. #include "toolchain/diagnostics/diagnostic_emitter.h"
  15. #include "toolchain/lex/tokenized_buffer.h"
  16. #include "toolchain/parse/node_ids.h"
  17. #include "toolchain/parse/node_kind.h"
  18. #include "toolchain/sem_ir/builtin_kind.h"
  19. #include "toolchain/sem_ir/file.h"
  20. #include "toolchain/sem_ir/ids.h"
  21. #include "toolchain/sem_ir/inst.h"
  22. #include "toolchain/sem_ir/inst_kind.h"
  23. #include "toolchain/sem_ir/typed_insts.h"
  24. namespace Carbon::Check {
  25. Context::Context(const Lex::TokenizedBuffer& tokens, DiagnosticEmitter& emitter,
  26. const Parse::Tree& parse_tree, SemIR::File& sem_ir,
  27. llvm::raw_ostream* vlog_stream)
  28. : tokens_(&tokens),
  29. emitter_(&emitter),
  30. parse_tree_(&parse_tree),
  31. sem_ir_(&sem_ir),
  32. vlog_stream_(vlog_stream),
  33. node_stack_(parse_tree, vlog_stream),
  34. inst_block_stack_("inst_block_stack_", sem_ir, vlog_stream),
  35. param_and_arg_refs_stack_(sem_ir, vlog_stream, node_stack_),
  36. args_type_info_stack_("args_type_info_stack_", sem_ir, vlog_stream),
  37. decl_name_stack_(this),
  38. scope_stack_(sem_ir_->identifiers()) {
  39. // Map the builtin `<error>` and `type` type constants to their corresponding
  40. // special `TypeId` values.
  41. type_ids_for_type_constants_.insert(
  42. {SemIR::ConstantId::ForTemplateConstant(SemIR::InstId::BuiltinError),
  43. SemIR::TypeId::Error});
  44. type_ids_for_type_constants_.insert(
  45. {SemIR::ConstantId::ForTemplateConstant(SemIR::InstId::BuiltinTypeType),
  46. SemIR::TypeId::TypeType});
  47. }
  48. auto Context::TODO(SemIRLoc loc, std::string label) -> bool {
  49. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: `{0}`.",
  50. std::string);
  51. emitter_->Emit(loc, SemanticsTodo, std::move(label));
  52. return false;
  53. }
  54. auto Context::VerifyOnFinish() -> void {
  55. // Information in all the various context objects should be cleaned up as
  56. // various pieces of context go out of scope. At this point, nothing should
  57. // remain.
  58. // node_stack_ will still contain top-level entities.
  59. scope_stack_.VerifyOnFinish();
  60. inst_block_stack_.VerifyOnFinish();
  61. param_and_arg_refs_stack_.VerifyOnFinish();
  62. }
  63. auto Context::AddInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst)
  64. -> SemIR::InstId {
  65. auto inst_id = sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  66. CARBON_VLOG() << "AddInst: " << loc_id_and_inst.inst << "\n";
  67. auto const_id = TryEvalInst(*this, inst_id, loc_id_and_inst.inst);
  68. if (const_id.is_constant()) {
  69. CARBON_VLOG() << "Constant: " << loc_id_and_inst.inst << " -> "
  70. << const_id.inst_id() << "\n";
  71. constant_values().Set(inst_id, const_id);
  72. }
  73. return inst_id;
  74. }
  75. auto Context::AddInst(SemIR::LocIdAndInst loc_id_and_inst) -> SemIR::InstId {
  76. auto inst_id = AddInstInNoBlock(loc_id_and_inst);
  77. inst_block_stack_.AddInstId(inst_id);
  78. return inst_id;
  79. }
  80. auto Context::AddPlaceholderInstInNoBlock(SemIR::LocIdAndInst loc_id_and_inst)
  81. -> SemIR::InstId {
  82. auto inst_id = sem_ir().insts().AddInNoBlock(loc_id_and_inst);
  83. CARBON_VLOG() << "AddPlaceholderInst: " << loc_id_and_inst.inst << "\n";
  84. constant_values().Set(inst_id, SemIR::ConstantId::Invalid);
  85. return inst_id;
  86. }
  87. auto Context::AddPlaceholderInst(SemIR::LocIdAndInst loc_id_and_inst)
  88. -> SemIR::InstId {
  89. auto inst_id = AddPlaceholderInstInNoBlock(loc_id_and_inst);
  90. inst_block_stack_.AddInstId(inst_id);
  91. return inst_id;
  92. }
  93. auto Context::AddConstant(SemIR::Inst inst, bool is_symbolic)
  94. -> SemIR::ConstantId {
  95. auto const_id = constants().GetOrAdd(inst, is_symbolic);
  96. CARBON_VLOG() << "AddConstant: " << inst << "\n";
  97. return const_id;
  98. }
  99. auto Context::AddInstAndPush(SemIR::LocIdAndInst loc_id_and_inst) -> void {
  100. auto inst_id = AddInst(loc_id_and_inst);
  101. node_stack_.Push(loc_id_and_inst.loc_id.node_id(), inst_id);
  102. }
  103. auto Context::ReplaceLocIdAndInstBeforeConstantUse(
  104. SemIR::InstId inst_id, SemIR::LocIdAndInst loc_id_and_inst) -> void {
  105. sem_ir().insts().SetLocIdAndInst(inst_id, loc_id_and_inst);
  106. CARBON_VLOG() << "ReplaceInst: " << inst_id << " -> " << loc_id_and_inst.inst
  107. << "\n";
  108. // Redo evaluation. This is only safe to do if this instruction has not
  109. // already been used as a constant, which is the caller's responsibility to
  110. // ensure.
  111. auto const_id = TryEvalInst(*this, inst_id, loc_id_and_inst.inst);
  112. if (const_id.is_constant()) {
  113. CARBON_VLOG() << "Constant: " << loc_id_and_inst.inst << " -> "
  114. << const_id.inst_id() << "\n";
  115. }
  116. constant_values().Set(inst_id, const_id);
  117. }
  118. auto Context::ReplaceInstBeforeConstantUse(SemIR::InstId inst_id,
  119. SemIR::Inst inst) -> void {
  120. sem_ir().insts().Set(inst_id, inst);
  121. CARBON_VLOG() << "ReplaceInst: " << inst_id << " -> " << inst << "\n";
  122. // Redo evaluation. This is only safe to do if this instruction has not
  123. // already been used as a constant, which is the caller's responsibility to
  124. // ensure.
  125. auto const_id = TryEvalInst(*this, inst_id, inst);
  126. if (const_id.is_constant()) {
  127. CARBON_VLOG() << "Constant: " << inst << " -> " << const_id.inst_id()
  128. << "\n";
  129. }
  130. constant_values().Set(inst_id, const_id);
  131. }
  132. auto Context::AddImportRef(SemIR::ImportIRId ir_id, SemIR::InstId inst_id)
  133. -> SemIR::InstId {
  134. auto import_ref_id =
  135. AddPlaceholderInstInNoBlock({SemIR::ImportRefUnused{ir_id, inst_id}});
  136. // We can't insert this instruction into whatever block we happen to be in,
  137. // because this function is typically called by name lookup in the middle of
  138. // an otherwise unknown checking step. But we need to add the instruction
  139. // somewhere, because it's referenced by other instructions and needs to be
  140. // visible in textual IR. Adding it to the file block is arbitrary but is the
  141. // best place we have right now.
  142. //
  143. // TODO: Consider adding a dedicated block for import_refs.
  144. inst_block_stack().AddInstIdToFileBlock(import_ref_id);
  145. return import_ref_id;
  146. }
  147. auto Context::DiagnoseDuplicateName(SemIRLoc dup_def, SemIRLoc prev_def)
  148. -> void {
  149. CARBON_DIAGNOSTIC(NameDeclDuplicate, Error,
  150. "Duplicate name being declared in the same scope.");
  151. CARBON_DIAGNOSTIC(NameDeclPrevious, Note,
  152. "Name is previously declared here.");
  153. emitter_->Build(dup_def, NameDeclDuplicate)
  154. .Note(prev_def, NameDeclPrevious)
  155. .Emit();
  156. }
  157. auto Context::DiagnoseNameNotFound(SemIR::LocId loc_id, SemIR::NameId name_id)
  158. -> void {
  159. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.",
  160. SemIR::NameId);
  161. emitter_->Emit(loc_id, NameNotFound, name_id);
  162. }
  163. auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
  164. DiagnosticBuilder& builder) -> void {
  165. const auto& class_info = classes().Get(class_id);
  166. CARBON_CHECK(!class_info.is_defined()) << "Class is not incomplete";
  167. if (class_info.definition_id.is_valid()) {
  168. CARBON_DIAGNOSTIC(ClassIncompleteWithinDefinition, Note,
  169. "Class is incomplete within its definition.");
  170. builder.Note(class_info.definition_id, ClassIncompleteWithinDefinition);
  171. } else {
  172. CARBON_DIAGNOSTIC(ClassForwardDeclaredHere, Note,
  173. "Class was forward declared here.");
  174. builder.Note(class_info.decl_id, ClassForwardDeclaredHere);
  175. }
  176. }
  177. auto Context::NoteUndefinedInterface(SemIR::InterfaceId interface_id,
  178. DiagnosticBuilder& builder) -> void {
  179. const auto& interface_info = interfaces().Get(interface_id);
  180. CARBON_CHECK(!interface_info.is_defined()) << "Interface is not incomplete";
  181. if (interface_info.is_being_defined()) {
  182. CARBON_DIAGNOSTIC(InterfaceUndefinedWithinDefinition, Note,
  183. "Interface is currently being defined.");
  184. builder.Note(interface_info.definition_id,
  185. InterfaceUndefinedWithinDefinition);
  186. } else {
  187. CARBON_DIAGNOSTIC(InterfaceForwardDeclaredHere, Note,
  188. "Interface was forward declared here.");
  189. builder.Note(interface_info.decl_id, InterfaceForwardDeclaredHere);
  190. }
  191. }
  192. auto Context::AddNameToLookup(SemIR::NameId name_id, SemIR::InstId target_id)
  193. -> void {
  194. if (auto existing = scope_stack().LookupOrAddName(name_id, target_id);
  195. existing.is_valid()) {
  196. DiagnoseDuplicateName(target_id, existing);
  197. }
  198. }
  199. auto Context::LookupNameInDecl(SemIR::LocId loc_id, SemIR::NameId name_id,
  200. SemIR::NameScopeId scope_id) -> SemIR::InstId {
  201. if (!scope_id.is_valid()) {
  202. // Look for a name in the current scope only. There are two cases where the
  203. // name would be in an outer scope:
  204. //
  205. // - The name is the sole component of the declared name:
  206. //
  207. // class A;
  208. // fn F() {
  209. // class A;
  210. // }
  211. //
  212. // In this case, the inner A is not the same class as the outer A, so
  213. // lookup should not find the outer A.
  214. //
  215. // - The name is a qualifier of some larger declared name:
  216. //
  217. // class A { class B; }
  218. // fn F() {
  219. // class A.B {}
  220. // }
  221. //
  222. // In this case, we're not in the correct scope to define a member of
  223. // class A, so we should reject, and we achieve this by not finding the
  224. // name A from the outer scope.
  225. return scope_stack().LookupInCurrentScope(name_id);
  226. } else {
  227. // We do not look into `extend`ed scopes here. A qualified name in a
  228. // declaration must specify the exact scope in which the name was originally
  229. // introduced:
  230. //
  231. // base class A { fn F(); }
  232. // class B { extend base: A; }
  233. //
  234. // // Error, no `F` in `B`.
  235. // fn B.F() {}
  236. return LookupNameInExactScope(loc_id, name_id, name_scopes().Get(scope_id));
  237. }
  238. }
  239. auto Context::LookupUnqualifiedName(Parse::NodeId node_id,
  240. SemIR::NameId name_id) -> SemIR::InstId {
  241. // TODO: Check for shadowed lookup results.
  242. // Find the results from enclosing lexical scopes. These will be combined with
  243. // results from non-lexical scopes such as namespaces and classes.
  244. auto [lexical_result, non_lexical_scopes] =
  245. scope_stack().LookupInEnclosingScopes(name_id);
  246. // Walk the non-lexical scopes and perform lookups into each of them.
  247. for (auto [index, name_scope_id] : llvm::reverse(non_lexical_scopes)) {
  248. if (auto non_lexical_result =
  249. LookupQualifiedName(node_id, name_id, name_scope_id,
  250. /*required=*/false);
  251. non_lexical_result.is_valid()) {
  252. return non_lexical_result;
  253. }
  254. }
  255. if (lexical_result.is_valid()) {
  256. return lexical_result;
  257. }
  258. // We didn't find anything at all.
  259. DiagnoseNameNotFound(node_id, name_id);
  260. return SemIR::InstId::BuiltinError;
  261. }
  262. // Handles lookup through the import_ir_scopes for LookupNameInExactScope.
  263. static auto LookupInImportIRScopes(Context& context, SemIRLoc loc,
  264. SemIR::NameId name_id,
  265. const SemIR::NameScope& scope)
  266. -> SemIR::InstId {
  267. auto identifier_id = name_id.AsIdentifierId();
  268. llvm::StringRef identifier;
  269. if (identifier_id.is_valid()) {
  270. identifier = context.identifiers().Get(identifier_id);
  271. }
  272. DiagnosticAnnotationScope annotate_diagnostics(
  273. &context.emitter(), [&](auto& builder) {
  274. CARBON_DIAGNOSTIC(InNameLookup, Note, "In name lookup for `{0}`.",
  275. SemIR::NameId);
  276. builder.Note(loc, InNameLookup, name_id);
  277. });
  278. auto result_id = SemIR::InstId::Invalid;
  279. for (auto [import_ir_id, import_scope_id] : scope.import_ir_scopes) {
  280. auto& import_ir = context.import_irs().Get(import_ir_id);
  281. // Determine the NameId in the import IR.
  282. SemIR::NameId import_name_id = name_id;
  283. if (identifier_id.is_valid()) {
  284. auto import_identifier_id =
  285. import_ir.sem_ir->identifiers().Lookup(identifier);
  286. if (!import_identifier_id.is_valid()) {
  287. // Name doesn't exist in the import IR.
  288. continue;
  289. }
  290. import_name_id = SemIR::NameId::ForIdentifier(import_identifier_id);
  291. }
  292. // Look up the name in the import scope.
  293. const auto& import_scope =
  294. import_ir.sem_ir->name_scopes().Get(import_scope_id);
  295. auto it = import_scope.names.find(import_name_id);
  296. if (it == import_scope.names.end()) {
  297. // Name doesn't exist in the import scope.
  298. continue;
  299. }
  300. auto import_inst_id = context.AddImportRef(import_ir_id, it->second);
  301. TryResolveImportRefUnused(context, import_inst_id);
  302. if (result_id.is_valid()) {
  303. // TODO: Add generalized merge functionality (merge_decls.h?).
  304. context.DiagnoseDuplicateName(import_inst_id, result_id);
  305. } else {
  306. result_id = import_inst_id;
  307. }
  308. }
  309. return result_id;
  310. }
  311. auto Context::LookupNameInExactScope(SemIRLoc loc, SemIR::NameId name_id,
  312. const SemIR::NameScope& scope)
  313. -> SemIR::InstId {
  314. if (auto it = scope.names.find(name_id); it != scope.names.end()) {
  315. TryResolveImportRefUnused(*this, it->second);
  316. return it->second;
  317. }
  318. if (!scope.import_ir_scopes.empty()) {
  319. return LookupInImportIRScopes(*this, loc, name_id, scope);
  320. }
  321. return SemIR::InstId::Invalid;
  322. }
  323. auto Context::LookupQualifiedName(Parse::NodeId node_id, SemIR::NameId name_id,
  324. SemIR::NameScopeId scope_id, bool required)
  325. -> SemIR::InstId {
  326. llvm::SmallVector<SemIR::NameScopeId> scope_ids = {scope_id};
  327. auto result_id = SemIR::InstId::Invalid;
  328. bool has_error = false;
  329. // Walk this scope and, if nothing is found here, the scopes it extends.
  330. while (!scope_ids.empty()) {
  331. const auto& scope = name_scopes().Get(scope_ids.pop_back_val());
  332. has_error |= scope.has_error;
  333. auto scope_result_id = LookupNameInExactScope(node_id, name_id, scope);
  334. if (!scope_result_id.is_valid()) {
  335. // Nothing found in this scope: also look in its extended scopes.
  336. auto extended = llvm::reverse(scope.extended_scopes);
  337. scope_ids.append(extended.begin(), extended.end());
  338. continue;
  339. }
  340. // If this is our second lookup result, diagnose an ambiguity.
  341. if (result_id.is_valid()) {
  342. // TODO: This is currently not reachable because the only scope that can
  343. // extend is a class scope, and it can only extend a single base class.
  344. // Add test coverage once this is possible.
  345. CARBON_DIAGNOSTIC(
  346. NameAmbiguousDueToExtend, Error,
  347. "Ambiguous use of name `{0}` found in multiple extended scopes.",
  348. SemIR::NameId);
  349. emitter_->Emit(node_id, NameAmbiguousDueToExtend, name_id);
  350. // TODO: Add notes pointing to the scopes.
  351. return SemIR::InstId::BuiltinError;
  352. }
  353. result_id = scope_result_id;
  354. }
  355. if (required && !result_id.is_valid()) {
  356. if (!has_error) {
  357. DiagnoseNameNotFound(node_id, name_id);
  358. }
  359. return SemIR::InstId::BuiltinError;
  360. }
  361. return result_id;
  362. }
  363. template <typename BranchNode, typename... Args>
  364. static auto AddDominatedBlockAndBranchImpl(Context& context,
  365. Parse::NodeId node_id, Args... args)
  366. -> SemIR::InstBlockId {
  367. if (!context.inst_block_stack().is_current_block_reachable()) {
  368. return SemIR::InstBlockId::Unreachable;
  369. }
  370. auto block_id = context.inst_blocks().AddDefaultValue();
  371. context.AddInst({node_id, BranchNode{block_id, args...}});
  372. return block_id;
  373. }
  374. auto Context::AddDominatedBlockAndBranch(Parse::NodeId node_id)
  375. -> SemIR::InstBlockId {
  376. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, node_id);
  377. }
  378. auto Context::AddDominatedBlockAndBranchWithArg(Parse::NodeId node_id,
  379. SemIR::InstId arg_id)
  380. -> SemIR::InstBlockId {
  381. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, node_id,
  382. arg_id);
  383. }
  384. auto Context::AddDominatedBlockAndBranchIf(Parse::NodeId node_id,
  385. SemIR::InstId cond_id)
  386. -> SemIR::InstBlockId {
  387. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, node_id,
  388. cond_id);
  389. }
  390. auto Context::AddConvergenceBlockAndPush(Parse::NodeId node_id, int num_blocks)
  391. -> void {
  392. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  393. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  394. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  395. if (inst_block_stack().is_current_block_reachable()) {
  396. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  397. new_block_id = inst_blocks().AddDefaultValue();
  398. }
  399. AddInst({node_id, SemIR::Branch{new_block_id}});
  400. }
  401. inst_block_stack().Pop();
  402. }
  403. inst_block_stack().Push(new_block_id);
  404. }
  405. auto Context::AddConvergenceBlockWithArgAndPush(
  406. Parse::NodeId node_id, std::initializer_list<SemIR::InstId> block_args)
  407. -> SemIR::InstId {
  408. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  409. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  410. for (auto arg_id : block_args) {
  411. if (inst_block_stack().is_current_block_reachable()) {
  412. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  413. new_block_id = inst_blocks().AddDefaultValue();
  414. }
  415. AddInst({node_id, SemIR::BranchWithArg{new_block_id, arg_id}});
  416. }
  417. inst_block_stack().Pop();
  418. }
  419. inst_block_stack().Push(new_block_id);
  420. // Acquire the result value.
  421. SemIR::TypeId result_type_id = insts().Get(*block_args.begin()).type_id();
  422. return AddInst({node_id, SemIR::BlockArg{result_type_id, new_block_id}});
  423. }
  424. // Add the current code block to the enclosing function.
  425. auto Context::AddCurrentCodeBlockToFunction(Parse::NodeId node_id) -> void {
  426. CARBON_CHECK(!inst_block_stack().empty()) << "no current code block";
  427. if (return_scope_stack().empty()) {
  428. CARBON_CHECK(node_id.is_valid())
  429. << "No current function, but node_id not provided";
  430. TODO(node_id,
  431. "Control flow expressions are currently only supported inside "
  432. "functions.");
  433. return;
  434. }
  435. if (!inst_block_stack().is_current_block_reachable()) {
  436. // Don't include unreachable blocks in the function.
  437. return;
  438. }
  439. auto function_id =
  440. insts()
  441. .GetAs<SemIR::FunctionDecl>(return_scope_stack().back().decl_id)
  442. .function_id;
  443. functions()
  444. .Get(function_id)
  445. .body_block_ids.push_back(inst_block_stack().PeekOrAdd());
  446. }
  447. auto Context::is_current_position_reachable() -> bool {
  448. if (!inst_block_stack().is_current_block_reachable()) {
  449. return false;
  450. }
  451. // Our current position is at the end of a reachable block. That position is
  452. // reachable unless the previous instruction is a terminator instruction.
  453. auto block_contents = inst_block_stack().PeekCurrentBlockContents();
  454. if (block_contents.empty()) {
  455. return true;
  456. }
  457. const auto& last_inst = insts().Get(block_contents.back());
  458. return last_inst.kind().terminator_kind() !=
  459. SemIR::TerminatorKind::Terminator;
  460. }
  461. auto Context::FinalizeGlobalInit() -> void {
  462. inst_block_stack().PushGlobalInit();
  463. if (!inst_block_stack().PeekCurrentBlockContents().empty()) {
  464. AddInst({Parse::NodeId::Invalid, SemIR::Return{}});
  465. // Pop the GlobalInit block here to finalize it.
  466. inst_block_stack().Pop();
  467. // __global_init is only added if there are initialization instructions.
  468. auto name_id = sem_ir().identifiers().Add("__global_init");
  469. sem_ir().functions().Add(
  470. {.name_id = SemIR::NameId::ForIdentifier(name_id),
  471. .enclosing_scope_id = SemIR::NameScopeId::Package,
  472. .decl_id = SemIR::InstId::Invalid,
  473. .implicit_param_refs_id = SemIR::InstBlockId::Empty,
  474. .param_refs_id = SemIR::InstBlockId::Empty,
  475. .return_type_id = SemIR::TypeId::Invalid,
  476. .return_slot_id = SemIR::InstId::Invalid,
  477. .body_block_ids = {SemIR::InstBlockId::GlobalInit}});
  478. } else {
  479. inst_block_stack().PopGlobalInit();
  480. }
  481. }
  482. namespace {
  483. // Worklist-based type completion mechanism.
  484. //
  485. // When attempting to complete a type, we may find other types that also need to
  486. // be completed: types nested within that type, and the value representation of
  487. // the type. In order to complete a type without recursing arbitrarily deeply,
  488. // we use a worklist of tasks:
  489. //
  490. // - An `AddNestedIncompleteTypes` step adds a task for all incomplete types
  491. // nested within a type to the work list.
  492. // - A `BuildValueRepr` step computes the value representation for a
  493. // type, once all of its nested types are complete, and marks the type as
  494. // complete.
  495. class TypeCompleter {
  496. public:
  497. TypeCompleter(
  498. Context& context,
  499. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  500. diagnoser)
  501. : context_(context), diagnoser_(diagnoser) {}
  502. // Attempts to complete the given type. Returns true if it is now complete,
  503. // false if it could not be completed.
  504. auto Complete(SemIR::TypeId type_id) -> bool {
  505. Push(type_id);
  506. while (!work_list_.empty()) {
  507. if (!ProcessStep()) {
  508. return false;
  509. }
  510. }
  511. return true;
  512. }
  513. private:
  514. // Adds `type_id` to the work list, if it's not already complete.
  515. auto Push(SemIR::TypeId type_id) -> void {
  516. if (!context_.types().IsComplete(type_id)) {
  517. work_list_.push_back({type_id, Phase::AddNestedIncompleteTypes});
  518. }
  519. }
  520. // Runs the next step.
  521. auto ProcessStep() -> bool {
  522. auto [type_id, phase] = work_list_.back();
  523. // We might have enqueued the same type more than once. Just skip the
  524. // type if it's already complete.
  525. if (context_.types().IsComplete(type_id)) {
  526. work_list_.pop_back();
  527. return true;
  528. }
  529. auto inst_id = context_.types().GetInstId(type_id);
  530. auto inst = context_.insts().Get(inst_id);
  531. auto old_work_list_size = work_list_.size();
  532. switch (phase) {
  533. case Phase::AddNestedIncompleteTypes:
  534. if (!AddNestedIncompleteTypes(inst)) {
  535. return false;
  536. }
  537. CARBON_CHECK(work_list_.size() >= old_work_list_size)
  538. << "AddNestedIncompleteTypes should not remove work items";
  539. work_list_[old_work_list_size - 1].phase = Phase::BuildValueRepr;
  540. break;
  541. case Phase::BuildValueRepr: {
  542. auto value_rep = BuildValueRepr(type_id, inst);
  543. context_.sem_ir().CompleteType(type_id, value_rep);
  544. CARBON_CHECK(old_work_list_size == work_list_.size())
  545. << "BuildValueRepr should not change work items";
  546. work_list_.pop_back();
  547. // Also complete the value representation type, if necessary. This
  548. // should never fail: the value representation shouldn't require any
  549. // additional nested types to be complete.
  550. if (!context_.types().IsComplete(value_rep.type_id)) {
  551. work_list_.push_back({value_rep.type_id, Phase::BuildValueRepr});
  552. }
  553. // For a pointer representation, the pointee also needs to be complete.
  554. if (value_rep.kind == SemIR::ValueRepr::Pointer) {
  555. if (value_rep.type_id == SemIR::TypeId::Error) {
  556. break;
  557. }
  558. auto pointee_type_id =
  559. context_.sem_ir().GetPointeeType(value_rep.type_id);
  560. if (!context_.types().IsComplete(pointee_type_id)) {
  561. work_list_.push_back({pointee_type_id, Phase::BuildValueRepr});
  562. }
  563. }
  564. break;
  565. }
  566. }
  567. return true;
  568. }
  569. // Adds any types nested within `type_inst` that need to be complete for
  570. // `type_inst` to be complete to our work list.
  571. auto AddNestedIncompleteTypes(SemIR::Inst type_inst) -> bool {
  572. switch (type_inst.kind()) {
  573. case SemIR::ArrayType::Kind:
  574. Push(type_inst.As<SemIR::ArrayType>().element_type_id);
  575. break;
  576. case SemIR::StructType::Kind:
  577. for (auto field_id : context_.inst_blocks().Get(
  578. type_inst.As<SemIR::StructType>().fields_id)) {
  579. Push(context_.insts()
  580. .GetAs<SemIR::StructTypeField>(field_id)
  581. .field_type_id);
  582. }
  583. break;
  584. case SemIR::TupleType::Kind:
  585. for (auto element_type_id : context_.type_blocks().Get(
  586. type_inst.As<SemIR::TupleType>().elements_id)) {
  587. Push(element_type_id);
  588. }
  589. break;
  590. case SemIR::ClassType::Kind: {
  591. auto class_type = type_inst.As<SemIR::ClassType>();
  592. auto& class_info = context_.classes().Get(class_type.class_id);
  593. if (!class_info.is_defined()) {
  594. if (diagnoser_) {
  595. auto builder = (*diagnoser_)();
  596. context_.NoteIncompleteClass(class_type.class_id, builder);
  597. builder.Emit();
  598. }
  599. return false;
  600. }
  601. Push(class_info.object_repr_id);
  602. break;
  603. }
  604. case SemIR::ConstType::Kind:
  605. Push(type_inst.As<SemIR::ConstType>().inner_id);
  606. break;
  607. default:
  608. break;
  609. }
  610. return true;
  611. }
  612. // Makes an empty value representation, which is used for types that have no
  613. // state, such as empty structs and tuples.
  614. auto MakeEmptyValueRepr() const -> SemIR::ValueRepr {
  615. return {.kind = SemIR::ValueRepr::None,
  616. .type_id = context_.GetTupleType({})};
  617. }
  618. // Makes a value representation that uses pass-by-copy, copying the given
  619. // type.
  620. auto MakeCopyValueRepr(SemIR::TypeId rep_id,
  621. SemIR::ValueRepr::AggregateKind aggregate_kind =
  622. SemIR::ValueRepr::NotAggregate) const
  623. -> SemIR::ValueRepr {
  624. return {.kind = SemIR::ValueRepr::Copy,
  625. .aggregate_kind = aggregate_kind,
  626. .type_id = rep_id};
  627. }
  628. // Makes a value representation that uses pass-by-address with the given
  629. // pointee type.
  630. auto MakePointerValueRepr(SemIR::TypeId pointee_id,
  631. SemIR::ValueRepr::AggregateKind aggregate_kind =
  632. SemIR::ValueRepr::NotAggregate) const
  633. -> SemIR::ValueRepr {
  634. // TODO: Should we add `const` qualification to `pointee_id`?
  635. return {.kind = SemIR::ValueRepr::Pointer,
  636. .aggregate_kind = aggregate_kind,
  637. .type_id = context_.GetPointerType(pointee_id)};
  638. }
  639. // Gets the value representation of a nested type, which should already be
  640. // complete.
  641. auto GetNestedValueRepr(SemIR::TypeId nested_type_id) const {
  642. CARBON_CHECK(context_.types().IsComplete(nested_type_id))
  643. << "Nested type should already be complete";
  644. auto value_rep = context_.types().GetValueRepr(nested_type_id);
  645. CARBON_CHECK(value_rep.kind != SemIR::ValueRepr::Unknown)
  646. << "Complete type should have a value representation";
  647. return value_rep;
  648. };
  649. auto BuildBuiltinValueRepr(SemIR::TypeId type_id,
  650. SemIR::Builtin builtin) const -> SemIR::ValueRepr {
  651. switch (builtin.builtin_kind) {
  652. case SemIR::BuiltinKind::TypeType:
  653. case SemIR::BuiltinKind::Error:
  654. case SemIR::BuiltinKind::Invalid:
  655. case SemIR::BuiltinKind::BoolType:
  656. case SemIR::BuiltinKind::IntType:
  657. case SemIR::BuiltinKind::FloatType:
  658. case SemIR::BuiltinKind::NamespaceType:
  659. case SemIR::BuiltinKind::FunctionType:
  660. case SemIR::BuiltinKind::BoundMethodType:
  661. case SemIR::BuiltinKind::WitnessType:
  662. return MakeCopyValueRepr(type_id);
  663. case SemIR::BuiltinKind::StringType:
  664. // TODO: Decide on string value semantics. This should probably be a
  665. // custom value representation carrying a pointer and size or
  666. // similar.
  667. return MakePointerValueRepr(type_id);
  668. }
  669. llvm_unreachable("All builtin kinds were handled above");
  670. }
  671. auto BuildImportRefUsedValueRepr(SemIR::TypeId type_id,
  672. SemIR::ImportRefUsed import_ref) const
  673. -> SemIR::ValueRepr {
  674. const auto& import_ir = context_.import_irs().Get(import_ref.ir_id).sem_ir;
  675. auto import_inst = import_ir->insts().Get(import_ref.inst_id);
  676. CARBON_CHECK(import_inst.kind() != SemIR::InstKind::ImportRefUsed)
  677. << "If ImportRefUsed can point at another, this would be recursive.";
  678. return BuildValueRepr(type_id, import_inst);
  679. }
  680. auto BuildStructOrTupleValueRepr(std::size_t num_elements,
  681. SemIR::TypeId elementwise_rep,
  682. bool same_as_object_rep) const
  683. -> SemIR::ValueRepr {
  684. SemIR::ValueRepr::AggregateKind aggregate_kind =
  685. same_as_object_rep ? SemIR::ValueRepr::ValueAndObjectAggregate
  686. : SemIR::ValueRepr::ValueAggregate;
  687. if (num_elements == 1) {
  688. // The value representation for a struct or tuple with a single element
  689. // is a struct or tuple containing the value representation of the
  690. // element.
  691. // TODO: Consider doing the same whenever `elementwise_rep` is
  692. // sufficiently small.
  693. return MakeCopyValueRepr(elementwise_rep, aggregate_kind);
  694. }
  695. // For a struct or tuple with multiple fields, we use a pointer
  696. // to the elementwise value representation.
  697. return MakePointerValueRepr(elementwise_rep, aggregate_kind);
  698. }
  699. auto BuildStructTypeValueRepr(SemIR::TypeId type_id,
  700. SemIR::StructType struct_type) const
  701. -> SemIR::ValueRepr {
  702. // TODO: Share more code with tuples.
  703. auto fields = context_.inst_blocks().Get(struct_type.fields_id);
  704. if (fields.empty()) {
  705. return MakeEmptyValueRepr();
  706. }
  707. // Find the value representation for each field, and construct a struct
  708. // of value representations.
  709. llvm::SmallVector<SemIR::InstId> value_rep_fields;
  710. value_rep_fields.reserve(fields.size());
  711. bool same_as_object_rep = true;
  712. for (auto field_id : fields) {
  713. auto field = context_.insts().GetAs<SemIR::StructTypeField>(field_id);
  714. auto field_value_rep = GetNestedValueRepr(field.field_type_id);
  715. if (field_value_rep.type_id != field.field_type_id) {
  716. same_as_object_rep = false;
  717. field.field_type_id = field_value_rep.type_id;
  718. // TODO: Use `TryEvalInst` to form this value.
  719. field_id = context_
  720. .AddConstant(field, context_.constant_values()
  721. .Get(context_.types().GetInstId(
  722. field.field_type_id))
  723. .is_symbolic())
  724. .inst_id();
  725. }
  726. value_rep_fields.push_back(field_id);
  727. }
  728. auto value_rep = same_as_object_rep
  729. ? type_id
  730. : context_.GetStructType(
  731. context_.inst_blocks().Add(value_rep_fields));
  732. return BuildStructOrTupleValueRepr(fields.size(), value_rep,
  733. same_as_object_rep);
  734. }
  735. auto BuildTupleTypeValueRepr(SemIR::TypeId type_id,
  736. SemIR::TupleType tuple_type) const
  737. -> SemIR::ValueRepr {
  738. // TODO: Share more code with structs.
  739. auto elements = context_.type_blocks().Get(tuple_type.elements_id);
  740. if (elements.empty()) {
  741. return MakeEmptyValueRepr();
  742. }
  743. // Find the value representation for each element, and construct a tuple
  744. // of value representations.
  745. llvm::SmallVector<SemIR::TypeId> value_rep_elements;
  746. value_rep_elements.reserve(elements.size());
  747. bool same_as_object_rep = true;
  748. for (auto element_type_id : elements) {
  749. auto element_value_rep = GetNestedValueRepr(element_type_id);
  750. if (element_value_rep.type_id != element_type_id) {
  751. same_as_object_rep = false;
  752. }
  753. value_rep_elements.push_back(element_value_rep.type_id);
  754. }
  755. auto value_rep = same_as_object_rep
  756. ? type_id
  757. : context_.GetTupleType(value_rep_elements);
  758. return BuildStructOrTupleValueRepr(elements.size(), value_rep,
  759. same_as_object_rep);
  760. }
  761. // Builds and returns the value representation for the given type. All nested
  762. // types, as found by AddNestedIncompleteTypes, are known to be complete.
  763. auto BuildValueRepr(SemIR::TypeId type_id, SemIR::Inst inst) const
  764. -> SemIR::ValueRepr {
  765. switch (inst.kind()) {
  766. case SemIR::AddrOf::Kind:
  767. case SemIR::AddrPattern::Kind:
  768. case SemIR::ArrayIndex::Kind:
  769. case SemIR::ArrayInit::Kind:
  770. case SemIR::Assign::Kind:
  771. case SemIR::AssociatedConstantDecl::Kind:
  772. case SemIR::AssociatedEntity::Kind:
  773. case SemIR::BaseDecl::Kind:
  774. case SemIR::BindAlias::Kind:
  775. case SemIR::BindName::Kind:
  776. case SemIR::BindValue::Kind:
  777. case SemIR::BlockArg::Kind:
  778. case SemIR::BoolLiteral::Kind:
  779. case SemIR::BoundMethod::Kind:
  780. case SemIR::Branch::Kind:
  781. case SemIR::BranchIf::Kind:
  782. case SemIR::BranchWithArg::Kind:
  783. case SemIR::Call::Kind:
  784. case SemIR::ClassDecl::Kind:
  785. case SemIR::ClassElementAccess::Kind:
  786. case SemIR::ClassInit::Kind:
  787. case SemIR::Converted::Kind:
  788. case SemIR::Deref::Kind:
  789. case SemIR::FacetTypeAccess::Kind:
  790. case SemIR::FieldDecl::Kind:
  791. case SemIR::FunctionDecl::Kind:
  792. case SemIR::ImplDecl::Kind:
  793. case SemIR::ImportRefUnused::Kind:
  794. case SemIR::InitializeFrom::Kind:
  795. case SemIR::InterfaceDecl::Kind:
  796. case SemIR::InterfaceWitness::Kind:
  797. case SemIR::InterfaceWitnessAccess::Kind:
  798. case SemIR::IntLiteral::Kind:
  799. case SemIR::NameRef::Kind:
  800. case SemIR::Namespace::Kind:
  801. case SemIR::Param::Kind:
  802. case SemIR::RealLiteral::Kind:
  803. case SemIR::Return::Kind:
  804. case SemIR::ReturnExpr::Kind:
  805. case SemIR::SpliceBlock::Kind:
  806. case SemIR::StringLiteral::Kind:
  807. case SemIR::StructAccess::Kind:
  808. case SemIR::StructTypeField::Kind:
  809. case SemIR::StructLiteral::Kind:
  810. case SemIR::StructInit::Kind:
  811. case SemIR::StructValue::Kind:
  812. case SemIR::Temporary::Kind:
  813. case SemIR::TemporaryStorage::Kind:
  814. case SemIR::TupleAccess::Kind:
  815. case SemIR::TupleIndex::Kind:
  816. case SemIR::TupleLiteral::Kind:
  817. case SemIR::TupleInit::Kind:
  818. case SemIR::TupleValue::Kind:
  819. case SemIR::UnaryOperatorNot::Kind:
  820. case SemIR::ValueAsRef::Kind:
  821. case SemIR::ValueOfInitializer::Kind:
  822. case SemIR::VarStorage::Kind:
  823. CARBON_FATAL() << "Type refers to non-type inst " << inst;
  824. case SemIR::ArrayType::Kind: {
  825. // For arrays, it's convenient to always use a pointer representation,
  826. // even when the array has zero or one element, in order to support
  827. // indexing.
  828. return MakePointerValueRepr(type_id, SemIR::ValueRepr::ObjectAggregate);
  829. }
  830. case SemIR::ImportRefUsed::Kind:
  831. return BuildImportRefUsedValueRepr(type_id,
  832. inst.As<SemIR::ImportRefUsed>());
  833. case SemIR::StructType::Kind:
  834. return BuildStructTypeValueRepr(type_id, inst.As<SemIR::StructType>());
  835. case SemIR::TupleType::Kind:
  836. return BuildTupleTypeValueRepr(type_id, inst.As<SemIR::TupleType>());
  837. case SemIR::ClassType::Kind:
  838. // The value representation for a class is a pointer to the object
  839. // representation.
  840. // TODO: Support customized value representations for classes.
  841. // TODO: Pick a better value representation when possible.
  842. return MakePointerValueRepr(
  843. context_.classes()
  844. .Get(inst.As<SemIR::ClassType>().class_id)
  845. .object_repr_id,
  846. SemIR::ValueRepr::ObjectAggregate);
  847. case SemIR::InterfaceType::Kind:
  848. // TODO: Should we model the value representation as a witness?
  849. return MakeEmptyValueRepr();
  850. case SemIR::Builtin::Kind:
  851. return BuildBuiltinValueRepr(type_id, inst.As<SemIR::Builtin>());
  852. case SemIR::AssociatedEntityType::Kind:
  853. case SemIR::BindSymbolicName::Kind:
  854. case SemIR::PointerType::Kind:
  855. case SemIR::UnboundElementType::Kind:
  856. return MakeCopyValueRepr(type_id);
  857. case SemIR::ConstType::Kind:
  858. // The value representation of `const T` is the same as that of `T`.
  859. // Objects are not modifiable through their value representations.
  860. return GetNestedValueRepr(inst.As<SemIR::ConstType>().inner_id);
  861. }
  862. }
  863. enum class Phase : int8_t {
  864. // The next step is to add nested types to the list of types to complete.
  865. AddNestedIncompleteTypes,
  866. // The next step is to build the value representation for the type.
  867. BuildValueRepr,
  868. };
  869. struct WorkItem {
  870. SemIR::TypeId type_id;
  871. Phase phase;
  872. };
  873. Context& context_;
  874. llvm::SmallVector<WorkItem> work_list_;
  875. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  876. diagnoser_;
  877. };
  878. } // namespace
  879. auto Context::TryToCompleteType(
  880. SemIR::TypeId type_id,
  881. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser)
  882. -> bool {
  883. return TypeCompleter(*this, diagnoser).Complete(type_id);
  884. }
  885. auto Context::GetTypeIdForTypeConstant(SemIR::ConstantId constant_id)
  886. -> SemIR::TypeId {
  887. CARBON_CHECK(constant_id.is_constant())
  888. << "Canonicalizing non-constant type: " << constant_id;
  889. auto [it, added] = type_ids_for_type_constants_.insert(
  890. {constant_id, SemIR::TypeId::Invalid});
  891. if (added) {
  892. it->second = types().Add({.constant_id = constant_id});
  893. }
  894. return it->second;
  895. }
  896. template <typename InstT, typename... EachArgT>
  897. static auto GetTypeImpl(Context& context, EachArgT... each_arg)
  898. -> SemIR::TypeId {
  899. // TODO: Remove inst_id parameter from TryEvalInst.
  900. return context.GetTypeIdForTypeConstant(
  901. TryEvalInst(context, SemIR::InstId::Invalid,
  902. InstT{SemIR::TypeId::TypeType, each_arg...}));
  903. }
  904. auto Context::GetStructType(SemIR::InstBlockId refs_id) -> SemIR::TypeId {
  905. return GetTypeImpl<SemIR::StructType>(*this, refs_id);
  906. }
  907. auto Context::GetTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids)
  908. -> SemIR::TypeId {
  909. // TODO: Deduplicate the type block here. Currently requesting the same tuple
  910. // type more than once will create multiple type blocks, all but one of which
  911. // is unused.
  912. return GetTypeImpl<SemIR::TupleType>(*this, type_blocks().Add(type_ids));
  913. }
  914. auto Context::GetAssociatedEntityType(SemIR::InterfaceId interface_id,
  915. SemIR::TypeId entity_type_id)
  916. -> SemIR::TypeId {
  917. return GetTypeImpl<SemIR::AssociatedEntityType>(*this, interface_id,
  918. entity_type_id);
  919. }
  920. auto Context::GetBuiltinType(SemIR::BuiltinKind kind) -> SemIR::TypeId {
  921. CARBON_CHECK(kind != SemIR::BuiltinKind::Invalid);
  922. auto type_id = GetTypeIdForTypeInst(SemIR::InstId::ForBuiltin(kind));
  923. // To keep client code simpler, complete builtin types before returning them.
  924. bool complete = TryToCompleteType(type_id);
  925. CARBON_CHECK(complete) << "Failed to complete builtin type";
  926. return type_id;
  927. }
  928. auto Context::GetPointerType(SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  929. return GetTypeImpl<SemIR::PointerType>(*this, pointee_type_id);
  930. }
  931. auto Context::GetUnboundElementType(SemIR::TypeId class_type_id,
  932. SemIR::TypeId element_type_id)
  933. -> SemIR::TypeId {
  934. return GetTypeImpl<SemIR::UnboundElementType>(*this, class_type_id,
  935. element_type_id);
  936. }
  937. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  938. if (auto const_type = types().TryGetAs<SemIR::ConstType>(type_id)) {
  939. return const_type->inner_id;
  940. }
  941. return type_id;
  942. }
  943. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  944. node_stack_.PrintForStackDump(output);
  945. inst_block_stack_.PrintForStackDump(output);
  946. param_and_arg_refs_stack_.PrintForStackDump(output);
  947. args_type_info_stack_.PrintForStackDump(output);
  948. }
  949. } // namespace Carbon::Check