context.cpp 36 KB

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