context.cpp 46 KB

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