context.cpp 46 KB

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