context.cpp 46 KB

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