context.cpp 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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/inst_block_stack.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/ids.h"
  17. #include "toolchain/sem_ir/inst.h"
  18. #include "toolchain/sem_ir/inst_kind.h"
  19. #include "toolchain/sem_ir/typed_insts.h"
  20. #include "toolchain/sem_ir/value_stores.h"
  21. namespace Carbon::Check {
  22. Context::Context(const Lex::TokenizedBuffer& tokens, DiagnosticEmitter& emitter,
  23. const Parse::Tree& parse_tree, SemIR::File& sem_ir,
  24. llvm::raw_ostream* vlog_stream)
  25. : tokens_(&tokens),
  26. emitter_(&emitter),
  27. parse_tree_(&parse_tree),
  28. sem_ir_(&sem_ir),
  29. vlog_stream_(vlog_stream),
  30. node_stack_(parse_tree, vlog_stream),
  31. inst_block_stack_("inst_block_stack_", sem_ir, vlog_stream),
  32. params_or_args_stack_("params_or_args_stack_", sem_ir, vlog_stream),
  33. args_type_info_stack_("args_type_info_stack_", sem_ir, vlog_stream),
  34. decl_name_stack_(this),
  35. lexical_lookup_(sem_ir_->identifiers()) {
  36. // Inserts the "Error" and "Type" types as "used types" so that
  37. // canonicalization can skip them. We don't emit either for lowering.
  38. canonical_types_.insert({SemIR::InstId::BuiltinError, SemIR::TypeId::Error});
  39. canonical_types_.insert(
  40. {SemIR::InstId::BuiltinTypeType, SemIR::TypeId::TypeType});
  41. }
  42. auto Context::TODO(Parse::NodeId parse_node, std::string label) -> bool {
  43. CARBON_DIAGNOSTIC(SemanticsTodo, Error, "Semantics TODO: `{0}`.",
  44. std::string);
  45. emitter_->Emit(parse_node, SemanticsTodo, std::move(label));
  46. return false;
  47. }
  48. auto Context::VerifyOnFinish() -> void {
  49. // Information in all the various context objects should be cleaned up as
  50. // various pieces of context go out of scope. At this point, nothing should
  51. // remain.
  52. // node_stack_ will still contain top-level entities.
  53. CARBON_CHECK(scope_stack_.empty()) << scope_stack_.size();
  54. CARBON_CHECK(inst_block_stack_.empty()) << inst_block_stack_.size();
  55. CARBON_CHECK(params_or_args_stack_.empty()) << params_or_args_stack_.size();
  56. }
  57. auto Context::AddInstInNoBlock(SemIR::ParseNodeAndInst parse_node_and_inst)
  58. -> SemIR::InstId {
  59. auto inst_id = sem_ir().insts().AddInNoBlock(parse_node_and_inst);
  60. CARBON_VLOG() << "AddInst: " << parse_node_and_inst.inst << "\n";
  61. auto const_id = TryEvalInst(*this, inst_id, parse_node_and_inst.inst);
  62. if (const_id.is_constant()) {
  63. CARBON_VLOG() << "Constant: " << parse_node_and_inst.inst << " -> "
  64. << const_id.inst_id() << "\n";
  65. constant_values().Set(inst_id, const_id);
  66. }
  67. return inst_id;
  68. }
  69. auto Context::AddInst(SemIR::ParseNodeAndInst parse_node_and_inst)
  70. -> SemIR::InstId {
  71. auto inst_id = AddInstInNoBlock(parse_node_and_inst);
  72. inst_block_stack_.AddInstId(inst_id);
  73. return inst_id;
  74. }
  75. auto Context::AddPlaceholderInstInNoBlock(
  76. SemIR::ParseNodeAndInst parse_node_and_inst) -> SemIR::InstId {
  77. auto inst_id = sem_ir().insts().AddInNoBlock(parse_node_and_inst);
  78. CARBON_VLOG() << "AddPlaceholderInst: " << parse_node_and_inst.inst << "\n";
  79. return inst_id;
  80. }
  81. auto Context::AddPlaceholderInst(SemIR::ParseNodeAndInst parse_node_and_inst)
  82. -> SemIR::InstId {
  83. auto inst_id = AddPlaceholderInstInNoBlock(parse_node_and_inst);
  84. inst_block_stack_.AddInstId(inst_id);
  85. return inst_id;
  86. }
  87. auto Context::AddConstant(SemIR::Inst inst, bool is_symbolic)
  88. -> SemIR::ConstantId {
  89. // TODO: Deduplicate constants.
  90. auto inst_id = sem_ir().insts().AddInNoBlock(
  91. SemIR::ParseNodeAndInst::Untyped(Parse::NodeId::Invalid, inst));
  92. constants().Add(inst_id);
  93. auto const_id = is_symbolic ? SemIR::ConstantId::ForSymbolicConstant(inst_id)
  94. : SemIR::ConstantId::ForTemplateConstant(inst_id);
  95. constant_values().Set(inst_id, const_id);
  96. CARBON_VLOG() << "AddConstantInst: " << inst << "\n";
  97. return const_id;
  98. }
  99. auto Context::AddInstAndPush(SemIR::ParseNodeAndInst parse_node_and_inst)
  100. -> void {
  101. auto inst_id = AddInst(parse_node_and_inst);
  102. node_stack_.Push(parse_node_and_inst.parse_node, inst_id);
  103. }
  104. auto Context::ReplaceInstBeforeConstantUse(
  105. SemIR::InstId inst_id, SemIR::ParseNodeAndInst parse_node_and_inst)
  106. -> void {
  107. sem_ir().insts().Set(inst_id, parse_node_and_inst);
  108. CARBON_VLOG() << "ReplaceInst: " << inst_id << " -> "
  109. << parse_node_and_inst.inst << "\n";
  110. // Redo evaluation. This is only safe to do if this instruction has not
  111. // already been used as a constant, which is the caller's responsibility to
  112. // ensure.
  113. auto const_id = TryEvalInst(*this, inst_id, parse_node_and_inst.inst);
  114. if (const_id.is_constant()) {
  115. CARBON_VLOG() << "Constant: " << parse_node_and_inst.inst << " -> "
  116. << const_id.inst_id() << "\n";
  117. }
  118. constant_values().Set(inst_id, const_id);
  119. }
  120. auto Context::DiagnoseDuplicateName(Parse::NodeId parse_node,
  121. SemIR::InstId prev_def_id) -> void {
  122. CARBON_DIAGNOSTIC(NameDeclDuplicate, Error,
  123. "Duplicate name being declared in the same scope.");
  124. CARBON_DIAGNOSTIC(NameDeclPrevious, Note,
  125. "Name is previously declared here.");
  126. emitter_->Build(parse_node, NameDeclDuplicate)
  127. .Note(prev_def_id, NameDeclPrevious)
  128. .Emit();
  129. }
  130. auto Context::DiagnoseNameNotFound(Parse::NodeId parse_node,
  131. SemIR::NameId name_id) -> void {
  132. CARBON_DIAGNOSTIC(NameNotFound, Error, "Name `{0}` not found.", std::string);
  133. emitter_->Emit(parse_node, NameNotFound, names().GetFormatted(name_id).str());
  134. }
  135. auto Context::NoteIncompleteClass(SemIR::ClassId class_id,
  136. DiagnosticBuilder& builder) -> void {
  137. CARBON_DIAGNOSTIC(ClassForwardDeclaredHere, Note,
  138. "Class was forward declared here.");
  139. CARBON_DIAGNOSTIC(ClassIncompleteWithinDefinition, Note,
  140. "Class is incomplete within its definition.");
  141. const auto& class_info = classes().Get(class_id);
  142. CARBON_CHECK(!class_info.is_defined()) << "Class is not incomplete";
  143. if (class_info.definition_id.is_valid()) {
  144. builder.Note(class_info.definition_id, ClassIncompleteWithinDefinition);
  145. } else {
  146. builder.Note(class_info.decl_id, ClassForwardDeclaredHere);
  147. }
  148. }
  149. auto Context::AddPackageImports(Parse::NodeId import_node,
  150. IdentifierId package_id,
  151. llvm::ArrayRef<const SemIR::File*> sem_irs,
  152. bool has_load_error) -> void {
  153. CARBON_CHECK(has_load_error || !sem_irs.empty())
  154. << "There should be either a load error or at least one IR.";
  155. auto name_id = SemIR::NameId::ForIdentifier(package_id);
  156. SemIR::CrossRefIRId first_id(cross_ref_irs().size());
  157. for (const auto* sem_ir : sem_irs) {
  158. cross_ref_irs().Add(sem_ir);
  159. }
  160. if (has_load_error) {
  161. cross_ref_irs().Add(nullptr);
  162. }
  163. SemIR::CrossRefIRId last_id(cross_ref_irs().size() - 1);
  164. auto type_id = GetBuiltinType(SemIR::BuiltinKind::NamespaceType);
  165. auto inst_id =
  166. AddInst({import_node, SemIR::Import{.type_id = type_id,
  167. .first_cross_ref_ir_id = first_id,
  168. .last_cross_ref_ir_id = last_id}});
  169. // Add the import to lookup. Should always succeed because imports will be
  170. // uniquely named.
  171. AddNameToLookup(import_node, name_id, inst_id);
  172. // Add a name for formatted output. This isn't used in name lookup in order
  173. // to reduce indirection, but it's separate from the Import because it
  174. // otherwise fits in an Inst.
  175. auto bind_name_id = bind_names().Add(
  176. {.name_id = name_id, .enclosing_scope_id = SemIR::NameScopeId::Package});
  177. AddInst({import_node, SemIR::BindName{.type_id = type_id,
  178. .bind_name_id = bind_name_id,
  179. .value_id = inst_id}});
  180. }
  181. auto Context::AddNameToLookup(Parse::NodeId name_node, SemIR::NameId name_id,
  182. SemIR::InstId target_id) -> void {
  183. if (current_scope().names.insert(name_id).second) {
  184. // TODO: Reject if we previously performed a failed lookup for this name in
  185. // this scope or a scope nested within it.
  186. auto& lexical_results = lexical_lookup_.Get(name_id);
  187. CARBON_CHECK(lexical_results.empty() ||
  188. lexical_results.back().scope_index < current_scope_index())
  189. << "Failed to clean up after scope nested within the current scope";
  190. lexical_results.push_back(
  191. {.inst_id = target_id, .scope_index = current_scope_index()});
  192. } else {
  193. DiagnoseDuplicateName(name_node,
  194. lexical_lookup_.Get(name_id).back().inst_id);
  195. }
  196. }
  197. auto Context::ResolveIfLazyImportRef(SemIR::InstId inst_id) -> void {
  198. auto inst = insts().Get(inst_id);
  199. auto lazy_inst = inst.TryAs<SemIR::LazyImportRef>();
  200. if (!lazy_inst) {
  201. return;
  202. }
  203. const SemIR::File& import_ir = *cross_ref_irs().Get(lazy_inst->ir_id);
  204. auto import_inst = import_ir.insts().Get(lazy_inst->inst_id);
  205. switch (import_inst.kind()) {
  206. case SemIR::InstKind::FunctionDecl: {
  207. // TODO: Fill this in better.
  208. auto function_id =
  209. functions().Add({.name_id = SemIR::NameId::Invalid,
  210. .enclosing_scope_id = SemIR::NameScopeId::Invalid,
  211. .decl_id = inst_id,
  212. .implicit_param_refs_id = SemIR::InstBlockId::Empty,
  213. .param_refs_id = SemIR::InstBlockId::Empty,
  214. .return_type_id = SemIR::TypeId::Invalid,
  215. .return_slot_id = SemIR::InstId::Invalid});
  216. ReplaceInstBeforeConstantUse(
  217. inst_id,
  218. // TODO: For diagnostic purposes, we should provide some form of
  219. // location for the function.
  220. {Parse::NodeId::Invalid,
  221. SemIR::FunctionDecl{GetBuiltinType(SemIR::BuiltinKind::FunctionType),
  222. function_id}});
  223. constant_values().Set(inst_id,
  224. SemIR::ConstantId::ForTemplateConstant(inst_id));
  225. break;
  226. }
  227. default:
  228. // TODO: We need more type support. For now we inject an arbitrary
  229. // invalid node that's unrelated to the underlying value. The TODO
  230. // diagnostic is used since this section shouldn't typically be able to
  231. // error.
  232. TODO(Parse::NodeId::Invalid,
  233. (llvm::Twine("TODO: support ") + import_inst.kind().name()).str());
  234. ReplaceInstBeforeConstantUse(
  235. inst_id, {Parse::NodeId::Invalid,
  236. SemIR::VarStorage{SemIR::TypeId::Error,
  237. SemIR::NameId::PackageNamespace}});
  238. break;
  239. }
  240. }
  241. auto Context::LookupNameInDecl(Parse::NodeId /*parse_node*/,
  242. SemIR::NameId name_id,
  243. SemIR::NameScopeId scope_id) -> SemIR::InstId {
  244. if (!scope_id.is_valid()) {
  245. // Look for a name in the current scope only. There are two cases where the
  246. // name would be in an outer scope:
  247. //
  248. // - The name is the sole component of the declared name:
  249. //
  250. // class A;
  251. // fn F() {
  252. // class A;
  253. // }
  254. //
  255. // In this case, the inner A is not the same class as the outer A, so
  256. // lookup should not find the outer A.
  257. //
  258. // - The name is a qualifier of some larger declared name:
  259. //
  260. // class A { class B; }
  261. // fn F() {
  262. // class A.B {}
  263. // }
  264. //
  265. // In this case, we're not in the correct scope to define a member of
  266. // class A, so we should reject, and we achieve this by not finding the
  267. // name A from the outer scope.
  268. auto& lexical_results = lexical_lookup_.Get(name_id);
  269. if (!lexical_results.empty()) {
  270. auto result = lexical_results.back();
  271. if (result.scope_index == current_scope_index()) {
  272. ResolveIfLazyImportRef(result.inst_id);
  273. return result.inst_id;
  274. }
  275. }
  276. return SemIR::InstId::Invalid;
  277. } else {
  278. // We do not look into `extend`ed scopes here. A qualified name in a
  279. // declaration must specify the exact scope in which the name was originally
  280. // introduced:
  281. //
  282. // base class A { fn F(); }
  283. // class B { extend base: A; }
  284. //
  285. // // Error, no `F` in `B`.
  286. // fn B.F() {}
  287. return LookupNameInExactScope(name_id, name_scopes().Get(scope_id));
  288. }
  289. }
  290. auto Context::LookupUnqualifiedName(Parse::NodeId parse_node,
  291. SemIR::NameId name_id) -> SemIR::InstId {
  292. // TODO: Check for shadowed lookup results.
  293. // Find the results from enclosing lexical scopes. These will be combined with
  294. // results from non-lexical scopes such as namespaces and classes.
  295. llvm::ArrayRef<LexicalLookup::Result> lexical_results =
  296. lexical_lookup_.Get(name_id);
  297. // Walk the non-lexical scopes and perform lookups into each of them.
  298. for (auto [index, name_scope_id] : llvm::reverse(non_lexical_scope_stack_)) {
  299. // If the innermost lexical result is within this non-lexical scope, then
  300. // it shadows all further non-lexical results and we're done.
  301. if (!lexical_results.empty() &&
  302. lexical_results.back().scope_index > index) {
  303. auto inst_id = lexical_results.back().inst_id;
  304. ResolveIfLazyImportRef(inst_id);
  305. return inst_id;
  306. }
  307. if (auto non_lexical_result =
  308. LookupQualifiedName(parse_node, name_id, name_scope_id,
  309. /*required=*/false);
  310. non_lexical_result.is_valid()) {
  311. return non_lexical_result;
  312. }
  313. }
  314. if (!lexical_results.empty()) {
  315. auto inst_id = lexical_results.back().inst_id;
  316. ResolveIfLazyImportRef(inst_id);
  317. return inst_id;
  318. }
  319. // We didn't find anything at all.
  320. if (!lexical_lookup_has_load_error_) {
  321. DiagnoseNameNotFound(parse_node, name_id);
  322. }
  323. return SemIR::InstId::BuiltinError;
  324. }
  325. auto Context::LookupNameInExactScope(SemIR::NameId name_id,
  326. const SemIR::NameScope& scope)
  327. -> SemIR::InstId {
  328. if (auto it = scope.names.find(name_id); it != scope.names.end()) {
  329. ResolveIfLazyImportRef(it->second);
  330. return it->second;
  331. }
  332. return SemIR::InstId::Invalid;
  333. }
  334. auto Context::LookupQualifiedName(Parse::NodeId parse_node,
  335. SemIR::NameId name_id,
  336. SemIR::NameScopeId scope_id, bool required)
  337. -> SemIR::InstId {
  338. llvm::SmallVector<SemIR::NameScopeId> scope_ids = {scope_id};
  339. auto result_id = SemIR::InstId::Invalid;
  340. bool has_error = false;
  341. // Walk this scope and, if nothing is found here, the scopes it extends.
  342. while (!scope_ids.empty()) {
  343. const auto& scope = name_scopes().Get(scope_ids.pop_back_val());
  344. has_error |= scope.has_error;
  345. auto scope_result_id = LookupNameInExactScope(name_id, scope);
  346. if (!scope_result_id.is_valid()) {
  347. // Nothing found in this scope: also look in its extended scopes.
  348. auto extended = llvm::reverse(scope.extended_scopes);
  349. scope_ids.append(extended.begin(), extended.end());
  350. continue;
  351. }
  352. // If this is our second lookup result, diagnose an ambiguity.
  353. if (result_id.is_valid()) {
  354. // TODO: This is currently not reachable because the only scope that can
  355. // extend is a class scope, and it can only extend a single base class.
  356. // Add test coverage once this is possible.
  357. CARBON_DIAGNOSTIC(
  358. NameAmbiguousDueToExtend, Error,
  359. "Ambiguous use of name `{0}` found in multiple extended scopes.",
  360. std::string);
  361. emitter_->Emit(parse_node, NameAmbiguousDueToExtend,
  362. names().GetFormatted(name_id).str());
  363. // TODO: Add notes pointing to the scopes.
  364. return SemIR::InstId::BuiltinError;
  365. }
  366. result_id = scope_result_id;
  367. }
  368. if (required && !result_id.is_valid()) {
  369. if (!has_error) {
  370. DiagnoseNameNotFound(parse_node, name_id);
  371. }
  372. return SemIR::InstId::BuiltinError;
  373. }
  374. return result_id;
  375. }
  376. auto Context::PushScope(SemIR::InstId scope_inst_id,
  377. SemIR::NameScopeId scope_id,
  378. bool lexical_lookup_has_load_error) -> void {
  379. scope_stack_.push_back(
  380. {.index = next_scope_index_,
  381. .scope_inst_id = scope_inst_id,
  382. .scope_id = scope_id,
  383. .prev_lexical_lookup_has_load_error = lexical_lookup_has_load_error_});
  384. if (scope_id.is_valid()) {
  385. non_lexical_scope_stack_.push_back({next_scope_index_, scope_id});
  386. }
  387. lexical_lookup_has_load_error_ |= lexical_lookup_has_load_error;
  388. // TODO: Handle this case more gracefully.
  389. CARBON_CHECK(next_scope_index_.index != std::numeric_limits<int32_t>::max())
  390. << "Ran out of scopes";
  391. ++next_scope_index_.index;
  392. }
  393. auto Context::PopScope() -> void {
  394. auto scope = scope_stack_.pop_back_val();
  395. lexical_lookup_has_load_error_ = scope.prev_lexical_lookup_has_load_error;
  396. for (const auto& str_id : scope.names) {
  397. auto& lexical_results = lexical_lookup_.Get(str_id);
  398. CARBON_CHECK(lexical_results.back().scope_index == scope.index)
  399. << "Inconsistent scope index for name " << names().GetFormatted(str_id);
  400. lexical_results.pop_back();
  401. }
  402. if (scope.scope_id.is_valid()) {
  403. CARBON_CHECK(non_lexical_scope_stack_.back().first == scope.index);
  404. non_lexical_scope_stack_.pop_back();
  405. }
  406. if (scope.has_returned_var) {
  407. CARBON_CHECK(!return_scope_stack_.empty());
  408. CARBON_CHECK(return_scope_stack_.back().returned_var.is_valid());
  409. return_scope_stack_.back().returned_var = SemIR::InstId::Invalid;
  410. }
  411. }
  412. auto Context::PopToScope(ScopeIndex index) -> void {
  413. while (current_scope_index() > index) {
  414. PopScope();
  415. }
  416. CARBON_CHECK(current_scope_index() == index)
  417. << "Scope index " << index << " does not enclose the current scope "
  418. << current_scope_index();
  419. }
  420. auto Context::SetReturnedVarOrGetExisting(SemIR::InstId inst_id)
  421. -> SemIR::InstId {
  422. CARBON_CHECK(!return_scope_stack_.empty()) << "`returned var` in no function";
  423. auto& returned_var = return_scope_stack_.back().returned_var;
  424. if (returned_var.is_valid()) {
  425. return returned_var;
  426. }
  427. returned_var = inst_id;
  428. CARBON_CHECK(!current_scope().has_returned_var)
  429. << "Scope has returned var but none is set";
  430. if (inst_id.is_valid()) {
  431. current_scope().has_returned_var = true;
  432. }
  433. return SemIR::InstId::Invalid;
  434. }
  435. auto Context::FollowNameRefs(SemIR::InstId inst_id) -> SemIR::InstId {
  436. while (auto name_ref = insts().Get(inst_id).TryAs<SemIR::NameRef>()) {
  437. inst_id = name_ref->value_id;
  438. }
  439. return inst_id;
  440. }
  441. template <typename BranchNode, typename... Args>
  442. static auto AddDominatedBlockAndBranchImpl(Context& context,
  443. Parse::NodeId parse_node,
  444. Args... args) -> SemIR::InstBlockId {
  445. if (!context.inst_block_stack().is_current_block_reachable()) {
  446. return SemIR::InstBlockId::Unreachable;
  447. }
  448. auto block_id = context.inst_blocks().AddDefaultValue();
  449. context.AddInst({parse_node, BranchNode{block_id, args...}});
  450. return block_id;
  451. }
  452. auto Context::AddDominatedBlockAndBranch(Parse::NodeId parse_node)
  453. -> SemIR::InstBlockId {
  454. return AddDominatedBlockAndBranchImpl<SemIR::Branch>(*this, parse_node);
  455. }
  456. auto Context::AddDominatedBlockAndBranchWithArg(Parse::NodeId parse_node,
  457. SemIR::InstId arg_id)
  458. -> SemIR::InstBlockId {
  459. return AddDominatedBlockAndBranchImpl<SemIR::BranchWithArg>(*this, parse_node,
  460. arg_id);
  461. }
  462. auto Context::AddDominatedBlockAndBranchIf(Parse::NodeId parse_node,
  463. SemIR::InstId cond_id)
  464. -> SemIR::InstBlockId {
  465. return AddDominatedBlockAndBranchImpl<SemIR::BranchIf>(*this, parse_node,
  466. cond_id);
  467. }
  468. auto Context::AddConvergenceBlockAndPush(Parse::NodeId parse_node,
  469. int num_blocks) -> void {
  470. CARBON_CHECK(num_blocks >= 2) << "no convergence";
  471. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  472. for ([[maybe_unused]] auto _ : llvm::seq(num_blocks)) {
  473. if (inst_block_stack().is_current_block_reachable()) {
  474. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  475. new_block_id = inst_blocks().AddDefaultValue();
  476. }
  477. AddInst({parse_node, SemIR::Branch{new_block_id}});
  478. }
  479. inst_block_stack().Pop();
  480. }
  481. inst_block_stack().Push(new_block_id);
  482. }
  483. auto Context::AddConvergenceBlockWithArgAndPush(
  484. Parse::NodeId parse_node, std::initializer_list<SemIR::InstId> block_args)
  485. -> SemIR::InstId {
  486. CARBON_CHECK(block_args.size() >= 2) << "no convergence";
  487. SemIR::InstBlockId new_block_id = SemIR::InstBlockId::Unreachable;
  488. for (auto arg_id : block_args) {
  489. if (inst_block_stack().is_current_block_reachable()) {
  490. if (new_block_id == SemIR::InstBlockId::Unreachable) {
  491. new_block_id = inst_blocks().AddDefaultValue();
  492. }
  493. AddInst({parse_node, SemIR::BranchWithArg{new_block_id, arg_id}});
  494. }
  495. inst_block_stack().Pop();
  496. }
  497. inst_block_stack().Push(new_block_id);
  498. // Acquire the result value.
  499. SemIR::TypeId result_type_id = insts().Get(*block_args.begin()).type_id();
  500. return AddInst({parse_node, SemIR::BlockArg{result_type_id, new_block_id}});
  501. }
  502. // Add the current code block to the enclosing function.
  503. auto Context::AddCurrentCodeBlockToFunction(Parse::NodeId parse_node) -> void {
  504. CARBON_CHECK(!inst_block_stack().empty()) << "no current code block";
  505. if (return_scope_stack().empty()) {
  506. CARBON_CHECK(parse_node.is_valid())
  507. << "No current function, but parse_node not provided";
  508. TODO(parse_node,
  509. "Control flow expressions are currently only supported inside "
  510. "functions.");
  511. return;
  512. }
  513. if (!inst_block_stack().is_current_block_reachable()) {
  514. // Don't include unreachable blocks in the function.
  515. return;
  516. }
  517. auto function_id =
  518. insts()
  519. .GetAs<SemIR::FunctionDecl>(return_scope_stack().back().decl_id)
  520. .function_id;
  521. functions()
  522. .Get(function_id)
  523. .body_block_ids.push_back(inst_block_stack().PeekOrAdd());
  524. }
  525. auto Context::is_current_position_reachable() -> bool {
  526. if (!inst_block_stack().is_current_block_reachable()) {
  527. return false;
  528. }
  529. // Our current position is at the end of a reachable block. That position is
  530. // reachable unless the previous instruction is a terminator instruction.
  531. auto block_contents = inst_block_stack().PeekCurrentBlockContents();
  532. if (block_contents.empty()) {
  533. return true;
  534. }
  535. const auto& last_inst = insts().Get(block_contents.back());
  536. return last_inst.kind().terminator_kind() !=
  537. SemIR::TerminatorKind::Terminator;
  538. }
  539. auto Context::ParamOrArgStart() -> void { params_or_args_stack_.Push(); }
  540. auto Context::ParamOrArgComma() -> void {
  541. ParamOrArgSave(node_stack_.PopExpr());
  542. }
  543. auto Context::ParamOrArgEndNoPop(Parse::NodeKind start_kind) -> void {
  544. if (!node_stack_.PeekIs(start_kind)) {
  545. ParamOrArgSave(node_stack_.PopExpr());
  546. }
  547. }
  548. auto Context::ParamOrArgPop() -> SemIR::InstBlockId {
  549. return params_or_args_stack_.Pop();
  550. }
  551. auto Context::ParamOrArgEnd(Parse::NodeKind start_kind) -> SemIR::InstBlockId {
  552. ParamOrArgEndNoPop(start_kind);
  553. return ParamOrArgPop();
  554. }
  555. namespace {
  556. // Worklist-based type completion mechanism.
  557. //
  558. // When attempting to complete a type, we may find other types that also need to
  559. // be completed: types nested within that type, and the value representation of
  560. // the type. In order to complete a type without recursing arbitrarily deeply,
  561. // we use a worklist of tasks:
  562. //
  563. // - An `AddNestedIncompleteTypes` step adds a task for all incomplete types
  564. // nested within a type to the work list.
  565. // - A `BuildValueRepr` step computes the value representation for a
  566. // type, once all of its nested types are complete, and marks the type as
  567. // complete.
  568. class TypeCompleter {
  569. public:
  570. TypeCompleter(
  571. Context& context,
  572. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  573. diagnoser)
  574. : context_(context), diagnoser_(diagnoser) {}
  575. // Attempts to complete the given type. Returns true if it is now complete,
  576. // false if it could not be completed.
  577. auto Complete(SemIR::TypeId type_id) -> bool {
  578. Push(type_id);
  579. while (!work_list_.empty()) {
  580. if (!ProcessStep()) {
  581. return false;
  582. }
  583. }
  584. return true;
  585. }
  586. private:
  587. // Adds `type_id` to the work list, if it's not already complete.
  588. auto Push(SemIR::TypeId type_id) -> void {
  589. if (!context_.types().IsComplete(type_id)) {
  590. work_list_.push_back({type_id, Phase::AddNestedIncompleteTypes});
  591. }
  592. }
  593. // Runs the next step.
  594. auto ProcessStep() -> bool {
  595. auto [type_id, phase] = work_list_.back();
  596. // We might have enqueued the same type more than once. Just skip the
  597. // type if it's already complete.
  598. if (context_.types().IsComplete(type_id)) {
  599. work_list_.pop_back();
  600. return true;
  601. }
  602. auto inst_id = context_.types().GetInstId(type_id);
  603. auto inst = context_.insts().Get(inst_id);
  604. auto old_work_list_size = work_list_.size();
  605. switch (phase) {
  606. case Phase::AddNestedIncompleteTypes:
  607. if (!AddNestedIncompleteTypes(inst)) {
  608. return false;
  609. }
  610. CARBON_CHECK(work_list_.size() >= old_work_list_size)
  611. << "AddNestedIncompleteTypes should not remove work items";
  612. work_list_[old_work_list_size - 1].phase = Phase::BuildValueRepr;
  613. break;
  614. case Phase::BuildValueRepr: {
  615. auto value_rep = BuildValueRepr(type_id, inst);
  616. context_.sem_ir().CompleteType(type_id, value_rep);
  617. CARBON_CHECK(old_work_list_size == work_list_.size())
  618. << "BuildValueRepr should not change work items";
  619. work_list_.pop_back();
  620. // Also complete the value representation type, if necessary. This
  621. // should never fail: the value representation shouldn't require any
  622. // additional nested types to be complete.
  623. if (!context_.types().IsComplete(value_rep.type_id)) {
  624. work_list_.push_back({value_rep.type_id, Phase::BuildValueRepr});
  625. }
  626. // For a pointer representation, the pointee also needs to be complete.
  627. if (value_rep.kind == SemIR::ValueRepr::Pointer) {
  628. auto pointee_type_id =
  629. context_.sem_ir().GetPointeeType(value_rep.type_id);
  630. if (!context_.types().IsComplete(pointee_type_id)) {
  631. work_list_.push_back({pointee_type_id, Phase::BuildValueRepr});
  632. }
  633. }
  634. break;
  635. }
  636. }
  637. return true;
  638. }
  639. // Adds any types nested within `type_inst` that need to be complete for
  640. // `type_inst` to be complete to our work list.
  641. auto AddNestedIncompleteTypes(SemIR::Inst type_inst) -> bool {
  642. switch (type_inst.kind()) {
  643. case SemIR::ArrayType::Kind:
  644. Push(type_inst.As<SemIR::ArrayType>().element_type_id);
  645. break;
  646. case SemIR::StructType::Kind:
  647. for (auto field_id : context_.inst_blocks().Get(
  648. type_inst.As<SemIR::StructType>().fields_id)) {
  649. Push(context_.insts()
  650. .GetAs<SemIR::StructTypeField>(field_id)
  651. .field_type_id);
  652. }
  653. break;
  654. case SemIR::TupleType::Kind:
  655. for (auto element_type_id : context_.type_blocks().Get(
  656. type_inst.As<SemIR::TupleType>().elements_id)) {
  657. Push(element_type_id);
  658. }
  659. break;
  660. case SemIR::ClassType::Kind: {
  661. auto class_type = type_inst.As<SemIR::ClassType>();
  662. auto& class_info = context_.classes().Get(class_type.class_id);
  663. if (!class_info.is_defined()) {
  664. if (diagnoser_) {
  665. auto builder = (*diagnoser_)();
  666. context_.NoteIncompleteClass(class_type.class_id, builder);
  667. builder.Emit();
  668. }
  669. return false;
  670. }
  671. Push(class_info.object_repr_id);
  672. break;
  673. }
  674. case SemIR::ConstType::Kind:
  675. Push(type_inst.As<SemIR::ConstType>().inner_id);
  676. break;
  677. default:
  678. break;
  679. }
  680. return true;
  681. }
  682. // Makes an empty value representation, which is used for types that have no
  683. // state, such as empty structs and tuples.
  684. auto MakeEmptyValueRepr() const -> SemIR::ValueRepr {
  685. return {.kind = SemIR::ValueRepr::None,
  686. .type_id = context_.CanonicalizeTupleType({})};
  687. }
  688. // Makes a value representation that uses pass-by-copy, copying the given
  689. // type.
  690. auto MakeCopyValueRepr(SemIR::TypeId rep_id,
  691. SemIR::ValueRepr::AggregateKind aggregate_kind =
  692. SemIR::ValueRepr::NotAggregate) const
  693. -> SemIR::ValueRepr {
  694. return {.kind = SemIR::ValueRepr::Copy,
  695. .aggregate_kind = aggregate_kind,
  696. .type_id = rep_id};
  697. }
  698. // Makes a value representation that uses pass-by-address with the given
  699. // pointee type.
  700. auto MakePointerValueRepr(SemIR::TypeId pointee_id,
  701. SemIR::ValueRepr::AggregateKind aggregate_kind =
  702. SemIR::ValueRepr::NotAggregate) const
  703. -> SemIR::ValueRepr {
  704. // TODO: Should we add `const` qualification to `pointee_id`?
  705. return {.kind = SemIR::ValueRepr::Pointer,
  706. .aggregate_kind = aggregate_kind,
  707. .type_id = context_.GetPointerType(pointee_id)};
  708. }
  709. // Gets the value representation of a nested type, which should already be
  710. // complete.
  711. auto GetNestedValueRepr(SemIR::TypeId nested_type_id) const {
  712. CARBON_CHECK(context_.types().IsComplete(nested_type_id))
  713. << "Nested type should already be complete";
  714. auto value_rep = context_.types().GetValueRepr(nested_type_id);
  715. CARBON_CHECK(value_rep.kind != SemIR::ValueRepr::Unknown)
  716. << "Complete type should have a value representation";
  717. return value_rep;
  718. };
  719. auto BuildCrossRefValueRepr(SemIR::TypeId type_id, SemIR::CrossRef xref) const
  720. -> SemIR::ValueRepr {
  721. auto xref_inst =
  722. context_.cross_ref_irs().Get(xref.ir_id)->insts().Get(xref.inst_id);
  723. // The canonical description of a type should only have cross-references
  724. // for entities owned by another File, such as builtins, which are owned
  725. // by the prelude, and named entities like classes and interfaces, which
  726. // we don't support yet.
  727. CARBON_CHECK(xref_inst.kind() == SemIR::Builtin::Kind)
  728. << "TODO: Handle other kinds of inst cross-references";
  729. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  730. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  731. switch (xref_inst.As<SemIR::Builtin>().builtin_kind) {
  732. case SemIR::BuiltinKind::TypeType:
  733. case SemIR::BuiltinKind::Error:
  734. case SemIR::BuiltinKind::Invalid:
  735. case SemIR::BuiltinKind::BoolType:
  736. case SemIR::BuiltinKind::IntType:
  737. case SemIR::BuiltinKind::FloatType:
  738. case SemIR::BuiltinKind::NamespaceType:
  739. case SemIR::BuiltinKind::FunctionType:
  740. case SemIR::BuiltinKind::BoundMethodType:
  741. return MakeCopyValueRepr(type_id);
  742. case SemIR::BuiltinKind::StringType:
  743. // TODO: Decide on string value semantics. This should probably be a
  744. // custom value representation carrying a pointer and size or
  745. // similar.
  746. return MakePointerValueRepr(type_id);
  747. }
  748. llvm_unreachable("All builtin kinds were handled above");
  749. }
  750. auto BuildStructOrTupleValueRepr(std::size_t num_elements,
  751. SemIR::TypeId elementwise_rep,
  752. bool same_as_object_rep) const
  753. -> SemIR::ValueRepr {
  754. SemIR::ValueRepr::AggregateKind aggregate_kind =
  755. same_as_object_rep ? SemIR::ValueRepr::ValueAndObjectAggregate
  756. : SemIR::ValueRepr::ValueAggregate;
  757. if (num_elements == 1) {
  758. // The value representation for a struct or tuple with a single element
  759. // is a struct or tuple containing the value representation of the
  760. // element.
  761. // TODO: Consider doing the same whenever `elementwise_rep` is
  762. // sufficiently small.
  763. return MakeCopyValueRepr(elementwise_rep, aggregate_kind);
  764. }
  765. // For a struct or tuple with multiple fields, we use a pointer
  766. // to the elementwise value representation.
  767. return MakePointerValueRepr(elementwise_rep, aggregate_kind);
  768. }
  769. auto BuildStructTypeValueRepr(SemIR::TypeId type_id,
  770. SemIR::StructType struct_type) const
  771. -> SemIR::ValueRepr {
  772. // TODO: Share more code with tuples.
  773. auto fields = context_.inst_blocks().Get(struct_type.fields_id);
  774. if (fields.empty()) {
  775. return MakeEmptyValueRepr();
  776. }
  777. // Find the value representation for each field, and construct a struct
  778. // of value representations.
  779. llvm::SmallVector<SemIR::InstId> value_rep_fields;
  780. value_rep_fields.reserve(fields.size());
  781. bool same_as_object_rep = true;
  782. for (auto field_id : fields) {
  783. auto field = context_.insts().GetAs<SemIR::StructTypeField>(field_id);
  784. auto field_value_rep = GetNestedValueRepr(field.field_type_id);
  785. if (field_value_rep.type_id != field.field_type_id) {
  786. same_as_object_rep = false;
  787. field.field_type_id = field_value_rep.type_id;
  788. // TODO: Use `TryEvalInst` to form this value.
  789. field_id = context_
  790. .AddConstant(field, context_.constant_values()
  791. .Get(context_.types().GetInstId(
  792. field.field_type_id))
  793. .is_symbolic())
  794. .inst_id();
  795. }
  796. value_rep_fields.push_back(field_id);
  797. }
  798. auto value_rep = same_as_object_rep
  799. ? type_id
  800. : context_.CanonicalizeStructType(
  801. context_.inst_blocks().Add(value_rep_fields));
  802. return BuildStructOrTupleValueRepr(fields.size(), value_rep,
  803. same_as_object_rep);
  804. }
  805. auto BuildTupleTypeValueRepr(SemIR::TypeId type_id,
  806. SemIR::TupleType tuple_type) const
  807. -> SemIR::ValueRepr {
  808. // TODO: Share more code with structs.
  809. auto elements = context_.type_blocks().Get(tuple_type.elements_id);
  810. if (elements.empty()) {
  811. return MakeEmptyValueRepr();
  812. }
  813. // Find the value representation for each element, and construct a tuple
  814. // of value representations.
  815. llvm::SmallVector<SemIR::TypeId> value_rep_elements;
  816. value_rep_elements.reserve(elements.size());
  817. bool same_as_object_rep = true;
  818. for (auto element_type_id : elements) {
  819. auto element_value_rep = GetNestedValueRepr(element_type_id);
  820. if (element_value_rep.type_id != element_type_id) {
  821. same_as_object_rep = false;
  822. }
  823. value_rep_elements.push_back(element_value_rep.type_id);
  824. }
  825. auto value_rep = same_as_object_rep
  826. ? type_id
  827. : context_.CanonicalizeTupleType(value_rep_elements);
  828. return BuildStructOrTupleValueRepr(elements.size(), value_rep,
  829. same_as_object_rep);
  830. }
  831. // Builds and returns the value representation for the given type. All nested
  832. // types, as found by AddNestedIncompleteTypes, are known to be complete.
  833. auto BuildValueRepr(SemIR::TypeId type_id, SemIR::Inst inst) const
  834. -> SemIR::ValueRepr {
  835. // TODO: This can emit new SemIR instructions. Consider emitting them into a
  836. // dedicated file-scope instruction block where possible, or somewhere else
  837. // that better reflects the definition of the type, rather than wherever the
  838. // type happens to first be required to be complete.
  839. // clang warns on unhandled enum values; clang-tidy is incorrect here.
  840. // NOLINTNEXTLINE(bugprone-switch-missing-default-case)
  841. switch (inst.kind()) {
  842. case SemIR::AddrOf::Kind:
  843. case SemIR::AddrPattern::Kind:
  844. case SemIR::ArrayIndex::Kind:
  845. case SemIR::ArrayInit::Kind:
  846. case SemIR::Assign::Kind:
  847. case SemIR::BaseDecl::Kind:
  848. case SemIR::BindName::Kind:
  849. case SemIR::BindValue::Kind:
  850. case SemIR::BlockArg::Kind:
  851. case SemIR::BoolLiteral::Kind:
  852. case SemIR::BoundMethod::Kind:
  853. case SemIR::Branch::Kind:
  854. case SemIR::BranchIf::Kind:
  855. case SemIR::BranchWithArg::Kind:
  856. case SemIR::Call::Kind:
  857. case SemIR::ClassDecl::Kind:
  858. case SemIR::ClassElementAccess::Kind:
  859. case SemIR::ClassInit::Kind:
  860. case SemIR::Converted::Kind:
  861. case SemIR::Deref::Kind:
  862. case SemIR::FieldDecl::Kind:
  863. case SemIR::FunctionDecl::Kind:
  864. case SemIR::Import::Kind:
  865. case SemIR::InitializeFrom::Kind:
  866. case SemIR::InterfaceDecl::Kind:
  867. case SemIR::IntLiteral::Kind:
  868. case SemIR::LazyImportRef::Kind:
  869. case SemIR::NameRef::Kind:
  870. case SemIR::Namespace::Kind:
  871. case SemIR::Param::Kind:
  872. case SemIR::RealLiteral::Kind:
  873. case SemIR::Return::Kind:
  874. case SemIR::ReturnExpr::Kind:
  875. case SemIR::SpliceBlock::Kind:
  876. case SemIR::StringLiteral::Kind:
  877. case SemIR::StructAccess::Kind:
  878. case SemIR::StructTypeField::Kind:
  879. case SemIR::StructLiteral::Kind:
  880. case SemIR::StructInit::Kind:
  881. case SemIR::StructValue::Kind:
  882. case SemIR::Temporary::Kind:
  883. case SemIR::TemporaryStorage::Kind:
  884. case SemIR::TupleAccess::Kind:
  885. case SemIR::TupleIndex::Kind:
  886. case SemIR::TupleLiteral::Kind:
  887. case SemIR::TupleInit::Kind:
  888. case SemIR::TupleValue::Kind:
  889. case SemIR::UnaryOperatorNot::Kind:
  890. case SemIR::ValueAsRef::Kind:
  891. case SemIR::ValueOfInitializer::Kind:
  892. case SemIR::VarStorage::Kind:
  893. CARBON_FATAL() << "Type refers to non-type inst " << inst;
  894. case SemIR::CrossRef::Kind:
  895. return BuildCrossRefValueRepr(type_id, inst.As<SemIR::CrossRef>());
  896. case SemIR::ArrayType::Kind: {
  897. // For arrays, it's convenient to always use a pointer representation,
  898. // even when the array has zero or one element, in order to support
  899. // indexing.
  900. return MakePointerValueRepr(type_id, SemIR::ValueRepr::ObjectAggregate);
  901. }
  902. case SemIR::StructType::Kind:
  903. return BuildStructTypeValueRepr(type_id, inst.As<SemIR::StructType>());
  904. case SemIR::TupleType::Kind:
  905. return BuildTupleTypeValueRepr(type_id, inst.As<SemIR::TupleType>());
  906. case SemIR::ClassType::Kind:
  907. // The value representation for a class is a pointer to the object
  908. // representation.
  909. // TODO: Support customized value representations for classes.
  910. // TODO: Pick a better value representation when possible.
  911. return MakePointerValueRepr(
  912. context_.classes()
  913. .Get(inst.As<SemIR::ClassType>().class_id)
  914. .object_repr_id,
  915. SemIR::ValueRepr::ObjectAggregate);
  916. case SemIR::Builtin::Kind:
  917. CARBON_FATAL() << "Builtins should be named as cross-references";
  918. case SemIR::BindSymbolicName::Kind:
  919. case SemIR::PointerType::Kind:
  920. case SemIR::UnboundElementType::Kind:
  921. return MakeCopyValueRepr(type_id);
  922. case SemIR::ConstType::Kind:
  923. // The value representation of `const T` is the same as that of `T`.
  924. // Objects are not modifiable through their value representations.
  925. return GetNestedValueRepr(inst.As<SemIR::ConstType>().inner_id);
  926. }
  927. }
  928. enum class Phase : int8_t {
  929. // The next step is to add nested types to the list of types to complete.
  930. AddNestedIncompleteTypes,
  931. // The next step is to build the value representation for the type.
  932. BuildValueRepr,
  933. };
  934. struct WorkItem {
  935. SemIR::TypeId type_id;
  936. Phase phase;
  937. };
  938. Context& context_;
  939. llvm::SmallVector<WorkItem> work_list_;
  940. std::optional<llvm::function_ref<auto()->Context::DiagnosticBuilder>>
  941. diagnoser_;
  942. };
  943. } // namespace
  944. auto Context::TryToCompleteType(
  945. SemIR::TypeId type_id,
  946. std::optional<llvm::function_ref<auto()->DiagnosticBuilder>> diagnoser)
  947. -> bool {
  948. return TypeCompleter(*this, diagnoser).Complete(type_id);
  949. }
  950. auto Context::CanonicalizeTypeImpl(
  951. SemIR::InstKind kind,
  952. llvm::function_ref<bool(llvm::FoldingSetNodeID& canonical_id)> profile_type,
  953. llvm::function_ref<SemIR::InstId()> make_inst) -> SemIR::TypeId {
  954. llvm::FoldingSetNodeID canonical_id;
  955. kind.Profile(canonical_id);
  956. if (!profile_type(canonical_id)) {
  957. return SemIR::TypeId::Error;
  958. }
  959. void* insert_pos;
  960. auto* node =
  961. canonical_type_nodes_.FindNodeOrInsertPos(canonical_id, insert_pos);
  962. if (node != nullptr) {
  963. return node->type_id();
  964. }
  965. auto inst_id = make_inst();
  966. auto type_id = types().Add({.inst_id = inst_id});
  967. CARBON_CHECK(canonical_types_.insert({inst_id, type_id}).second);
  968. type_node_storage_.push_back(
  969. std::make_unique<TypeNode>(canonical_id, type_id));
  970. // In a debug build, check that our insertion position is still valid. It
  971. // could have been invalidated by a misbehaving `make_inst`.
  972. CARBON_DCHECK([&] {
  973. void* check_insert_pos;
  974. auto* check_node = canonical_type_nodes_.FindNodeOrInsertPos(
  975. canonical_id, check_insert_pos);
  976. return !check_node && insert_pos == check_insert_pos;
  977. }()) << "Type was created recursively during canonicalization";
  978. canonical_type_nodes_.InsertNode(type_node_storage_.back().get(), insert_pos);
  979. return type_id;
  980. }
  981. // Compute a fingerprint for a tuple type, for use as a key in a folding set.
  982. static auto ProfileTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids,
  983. llvm::FoldingSetNodeID& canonical_id) -> void {
  984. for (auto type_id : type_ids) {
  985. canonical_id.AddInteger(type_id.index);
  986. }
  987. }
  988. // Compute a fingerprint for a type, for use as a key in a folding set. Returns
  989. // false if not supported, which is presently the case for compile-time
  990. // expressions.
  991. // TODO: Once support is more complete, in particular ensuring that various
  992. // valid compile-time expressions are supported, it may be desirable to switch
  993. // the default to a CARBON_FATAL error.
  994. static auto ProfileType(Context& semantics_context, SemIR::Inst inst,
  995. llvm::FoldingSetNodeID& canonical_id) -> bool {
  996. switch (inst.kind()) {
  997. case SemIR::ArrayType::Kind: {
  998. auto array_type = inst.As<SemIR::ArrayType>();
  999. canonical_id.AddInteger(
  1000. semantics_context.sem_ir().GetArrayBoundValue(array_type.bound_id));
  1001. canonical_id.AddInteger(array_type.element_type_id.index);
  1002. break;
  1003. }
  1004. case SemIR::Builtin::Kind:
  1005. canonical_id.AddInteger(inst.As<SemIR::Builtin>().builtin_kind.AsInt());
  1006. break;
  1007. case SemIR::ClassType::Kind:
  1008. canonical_id.AddInteger(inst.As<SemIR::ClassType>().class_id.index);
  1009. break;
  1010. case SemIR::CrossRef::Kind: {
  1011. // TODO: Cross-references should be canonicalized by looking at their
  1012. // target rather than treating them as new unique types.
  1013. auto xref = inst.As<SemIR::CrossRef>();
  1014. canonical_id.AddInteger(xref.ir_id.index);
  1015. canonical_id.AddInteger(xref.inst_id.index);
  1016. break;
  1017. }
  1018. case SemIR::ConstType::Kind:
  1019. canonical_id.AddInteger(
  1020. semantics_context
  1021. .GetUnqualifiedType(inst.As<SemIR::ConstType>().inner_id)
  1022. .index);
  1023. break;
  1024. case SemIR::BindSymbolicName::Kind:
  1025. // TODO: Use de Bruijn levels or similar to identify equivalent type
  1026. // bindings across redeclarations.
  1027. canonical_id.AddInteger(
  1028. inst.As<SemIR::BindSymbolicName>().bind_name_id.index);
  1029. break;
  1030. case SemIR::PointerType::Kind:
  1031. canonical_id.AddInteger(inst.As<SemIR::PointerType>().pointee_id.index);
  1032. break;
  1033. case SemIR::StructType::Kind: {
  1034. auto fields = semantics_context.inst_blocks().Get(
  1035. inst.As<SemIR::StructType>().fields_id);
  1036. for (const auto& field_id : fields) {
  1037. auto field =
  1038. semantics_context.insts().GetAs<SemIR::StructTypeField>(field_id);
  1039. canonical_id.AddInteger(field.name_id.index);
  1040. canonical_id.AddInteger(field.field_type_id.index);
  1041. }
  1042. break;
  1043. }
  1044. case SemIR::TupleType::Kind:
  1045. ProfileTupleType(semantics_context.type_blocks().Get(
  1046. inst.As<SemIR::TupleType>().elements_id),
  1047. canonical_id);
  1048. break;
  1049. case SemIR::UnboundElementType::Kind: {
  1050. auto unbound_field_type = inst.As<SemIR::UnboundElementType>();
  1051. canonical_id.AddInteger(unbound_field_type.class_type_id.index);
  1052. canonical_id.AddInteger(unbound_field_type.element_type_id.index);
  1053. break;
  1054. }
  1055. default: {
  1056. // Right now, this is only expected to occur in calls from
  1057. // ExprAsType. Diagnostics are issued there.
  1058. return false;
  1059. }
  1060. }
  1061. return true;
  1062. }
  1063. auto Context::CanonicalizeTypeAndAddInstIfNew(SemIR::Inst inst)
  1064. -> SemIR::TypeId {
  1065. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  1066. return ProfileType(*this, inst, canonical_id);
  1067. };
  1068. auto make_inst = [&] {
  1069. // TODO: Properly determine whether types are symbolic.
  1070. return AddConstant(inst, /*is_symbolic=*/false).inst_id();
  1071. };
  1072. return CanonicalizeTypeImpl(inst.kind(), profile_node, make_inst);
  1073. }
  1074. auto Context::CanonicalizeType(SemIR::InstId inst_id) -> SemIR::TypeId {
  1075. while (auto converted = insts().Get(inst_id).TryAs<SemIR::Converted>()) {
  1076. inst_id = converted->result_id;
  1077. }
  1078. inst_id = FollowNameRefs(inst_id);
  1079. auto it = canonical_types_.find(inst_id);
  1080. if (it != canonical_types_.end()) {
  1081. return it->second;
  1082. }
  1083. auto inst = insts().Get(inst_id);
  1084. auto profile_node = [&](llvm::FoldingSetNodeID& canonical_id) {
  1085. return ProfileType(*this, inst, canonical_id);
  1086. };
  1087. auto make_inst = [&] { return inst_id; };
  1088. return CanonicalizeTypeImpl(inst.kind(), profile_node, make_inst);
  1089. }
  1090. auto Context::CanonicalizeStructType(SemIR::InstBlockId refs_id)
  1091. -> SemIR::TypeId {
  1092. return CanonicalizeTypeAndAddInstIfNew(
  1093. SemIR::StructType{SemIR::TypeId::TypeType, refs_id});
  1094. }
  1095. auto Context::CanonicalizeTupleType(llvm::ArrayRef<SemIR::TypeId> type_ids)
  1096. -> SemIR::TypeId {
  1097. // Defer allocating a SemIR::TypeBlockId until we know this is a new type.
  1098. auto profile_tuple = [&](llvm::FoldingSetNodeID& canonical_id) {
  1099. ProfileTupleType(type_ids, canonical_id);
  1100. return true;
  1101. };
  1102. auto make_tuple_inst = [&] {
  1103. // TODO: Properly determine when types are symbolic.
  1104. return AddConstant(SemIR::TupleType{SemIR::TypeId::TypeType,
  1105. type_blocks().Add(type_ids)},
  1106. /*is_symbolic=*/false)
  1107. .inst_id();
  1108. };
  1109. return CanonicalizeTypeImpl(SemIR::TupleType::Kind, profile_tuple,
  1110. make_tuple_inst);
  1111. }
  1112. auto Context::GetBuiltinType(SemIR::BuiltinKind kind) -> SemIR::TypeId {
  1113. CARBON_CHECK(kind != SemIR::BuiltinKind::Invalid);
  1114. auto type_id = CanonicalizeType(SemIR::InstId::ForBuiltin(kind));
  1115. // To keep client code simpler, complete builtin types before returning them.
  1116. bool complete = TryToCompleteType(type_id);
  1117. CARBON_CHECK(complete) << "Failed to complete builtin type";
  1118. return type_id;
  1119. }
  1120. auto Context::GetPointerType(SemIR::TypeId pointee_type_id) -> SemIR::TypeId {
  1121. return CanonicalizeTypeAndAddInstIfNew(
  1122. SemIR::PointerType{SemIR::TypeId::TypeType, pointee_type_id});
  1123. }
  1124. auto Context::GetUnqualifiedType(SemIR::TypeId type_id) -> SemIR::TypeId {
  1125. if (auto const_type = types().TryGetAs<SemIR::ConstType>(type_id)) {
  1126. return const_type->inner_id;
  1127. }
  1128. return type_id;
  1129. }
  1130. auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
  1131. node_stack_.PrintForStackDump(output);
  1132. inst_block_stack_.PrintForStackDump(output);
  1133. params_or_args_stack_.PrintForStackDump(output);
  1134. args_type_info_stack_.PrintForStackDump(output);
  1135. }
  1136. } // namespace Carbon::Check