import_ref.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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/import_ref.h"
  5. #include "common/check.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/eval.h"
  8. #include "toolchain/parse/node_ids.h"
  9. #include "toolchain/sem_ir/file.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/inst.h"
  12. #include "toolchain/sem_ir/inst_kind.h"
  13. #include "toolchain/sem_ir/typed_insts.h"
  14. #include "toolchain/sem_ir/value_stores.h"
  15. namespace Carbon::Check {
  16. // Resolves an instruction from an imported IR into a constant referring to the
  17. // current IR.
  18. //
  19. // Calling Resolve on an instruction operates in an iterative manner, tracking
  20. // Work items on work_stack_. At a high level, the loop is:
  21. //
  22. // 1. If Work has received a constant, it's considered resolved.
  23. // - If made_incomplete_type, resolve unconditionally.
  24. // - The constant check avoids performance costs of deduplication on add.
  25. // 2. Resolve the instruction: (TryResolveInst/TryResolveTypedInst)
  26. // - For most cases:
  27. // A. For types which _can_ be incomplete, when not made_incomplete_type:
  28. // i. Start by making an incomplete type to address circular references.
  29. // ii. If the imported type is incomplete, return the constant.
  30. // iii. Otherwise, set made_incomplete_type and continue resolving.
  31. // - Creating an incomplete type will have set the constant, which
  32. // influences step (1); setting made_incomplete_type gets us a second
  33. // resolve pass when needed.
  34. // B. Gather all input constants.
  35. // - Gathering constants directly adds unresolved values to work_stack_.
  36. // C. If any need to be resolved (HasNewWork), return Invalid; this
  37. // instruction needs two calls to complete.
  38. // D. Build any necessary IR structures, and return the output constant.
  39. // - For trivial cases with zero or one input constants, this may return
  40. // a constant (if one, potentially Invalid) directly.
  41. // 3. If resolving returned a non-Invalid constant, pop the work; otherwise, it
  42. // needs to remain (and may no longer be at the top of the stack).
  43. //
  44. // TryResolveInst/TryResolveTypedInst can complete in one call for a given
  45. // instruction, but should always complete within two calls. However, due to the
  46. // chance of a second call, it's important to reserve all expensive logic until
  47. // it's been established that input constants are available; this in particular
  48. // includes GetTypeIdForTypeConstant calls which do a hash table lookup.
  49. class ImportRefResolver {
  50. public:
  51. explicit ImportRefResolver(Context& context, SemIR::ImportIRId import_ir_id)
  52. : context_(context),
  53. import_ir_id_(import_ir_id),
  54. import_ir_(*context_.import_irs().Get(import_ir_id)),
  55. import_ir_constant_values_(
  56. context_.import_ir_constant_values()[import_ir_id.index]) {}
  57. // Iteratively resolves an imported instruction's inner references until a
  58. // constant ID referencing the current IR is produced. When an outer
  59. // instruction has unresolved inner references, it will add them to the stack
  60. // for inner evaluation and reattempt outer evaluation after.
  61. auto Resolve(SemIR::InstId inst_id) -> SemIR::ConstantId {
  62. work_stack_.push_back({inst_id});
  63. while (!work_stack_.empty()) {
  64. auto work = work_stack_.back();
  65. CARBON_CHECK(work.inst_id.is_valid());
  66. // Double-check that the constant still doesn't have a calculated value.
  67. // This should typically be checked before adding it, but a given
  68. // instruction may be added multiple times before its constant is
  69. // evaluated.
  70. if (!work.made_incomplete_type &&
  71. import_ir_constant_values_.Get(work.inst_id).is_valid()) {
  72. work_stack_.pop_back();
  73. } else if (auto new_const_id =
  74. TryResolveInst(work.inst_id, work.made_incomplete_type);
  75. new_const_id.is_valid()) {
  76. import_ir_constant_values_.Set(work.inst_id, new_const_id);
  77. work_stack_.pop_back();
  78. }
  79. }
  80. auto constant_id = import_ir_constant_values_.Get(inst_id);
  81. CARBON_CHECK(constant_id.is_valid());
  82. return constant_id;
  83. }
  84. // Wraps constant evaluation with logic to handle types.
  85. auto ResolveType(SemIR::TypeId import_type_id) -> SemIR::TypeId {
  86. if (!import_type_id.is_valid()) {
  87. return import_type_id;
  88. }
  89. auto import_type_inst_id = import_ir_.types().GetInstId(import_type_id);
  90. CARBON_CHECK(import_type_inst_id.is_valid());
  91. if (import_type_inst_id.is_builtin()) {
  92. // Builtins don't require constant resolution; we can use them directly.
  93. return context_.GetBuiltinType(import_type_inst_id.builtin_kind());
  94. } else {
  95. return context_.GetTypeIdForTypeConstant(Resolve(import_type_inst_id));
  96. }
  97. }
  98. private:
  99. // A step in work_stack_.
  100. struct Work {
  101. // The instruction to work on.
  102. SemIR::InstId inst_id;
  103. // True if a first pass made an incomplete type.
  104. bool made_incomplete_type = false;
  105. };
  106. // For imported entities, we use an invalid enclosing scope. This will be okay
  107. // if the scope isn't used later, but we may need to change logic for this if
  108. // the behavior changes.
  109. static constexpr SemIR::NameScopeId NoEnclosingScopeForImports =
  110. SemIR::NameScopeId::Invalid;
  111. // Returns true if new unresolved constants were found.
  112. //
  113. // At the start of a function, do:
  114. // auto initial_work = work_stack_.size();
  115. // Then when determining:
  116. // if (HasNewWork(initial_work)) { ... }
  117. auto HasNewWork(size_t initial_work) -> bool {
  118. CARBON_CHECK(initial_work <= work_stack_.size())
  119. << "Work shouldn't decrease";
  120. return initial_work < work_stack_.size();
  121. }
  122. // Returns the ConstantId for an InstId. Adds unresolved constants to
  123. // work_stack_.
  124. auto GetLocalConstantId(SemIR::InstId inst_id) -> SemIR::ConstantId {
  125. auto const_id = import_ir_constant_values_.Get(inst_id);
  126. if (!const_id.is_valid()) {
  127. work_stack_.push_back({inst_id});
  128. }
  129. return const_id;
  130. }
  131. // Returns the ConstantId for a TypeId. Adds unresolved constants to
  132. // work_stack_.
  133. auto GetLocalConstantId(SemIR::TypeId type_id) -> SemIR::ConstantId {
  134. return GetLocalConstantId(import_ir_.types().GetInstId(type_id));
  135. }
  136. // Returns the ConstantId for each parameter's type. Adds unresolved constants
  137. // to work_stack_.
  138. auto GetLocalParamConstantIds(SemIR::InstBlockId param_refs_id)
  139. -> llvm::SmallVector<SemIR::ConstantId> {
  140. if (param_refs_id == SemIR::InstBlockId::Empty) {
  141. return {};
  142. }
  143. const auto& param_refs = import_ir_.inst_blocks().Get(param_refs_id);
  144. llvm::SmallVector<SemIR::ConstantId> const_ids;
  145. const_ids.reserve(param_refs.size());
  146. for (auto inst_id : param_refs) {
  147. const_ids.push_back(
  148. GetLocalConstantId(import_ir_.insts().Get(inst_id).type_id()));
  149. }
  150. return const_ids;
  151. }
  152. // Given a param_refs_id and const_ids from GetLocalParamConstantIds, returns
  153. // a version of param_refs_id localized to the current IR.
  154. auto GetLocalParamRefsId(
  155. SemIR::InstBlockId param_refs_id,
  156. const llvm::SmallVector<SemIR::ConstantId>& const_ids)
  157. -> SemIR::InstBlockId {
  158. if (param_refs_id == SemIR::InstBlockId::Empty) {
  159. return SemIR::InstBlockId::Empty;
  160. }
  161. const auto& param_refs = import_ir_.inst_blocks().Get(param_refs_id);
  162. llvm::SmallVector<SemIR::InstId> new_param_refs;
  163. for (auto [ref_id, const_id] : llvm::zip(param_refs, const_ids)) {
  164. // Figure out the param structure. This echoes
  165. // Function::GetParamFromParamRefId.
  166. // TODO: Consider a different parameter handling to simplify import logic.
  167. auto inst = import_ir_.insts().Get(ref_id);
  168. auto addr_inst = inst.TryAs<SemIR::AddrPattern>();
  169. if (addr_inst) {
  170. inst = import_ir_.insts().Get(addr_inst->inner_id);
  171. }
  172. auto bind_inst = inst.TryAs<SemIR::AnyBindName>();
  173. if (bind_inst) {
  174. inst = import_ir_.insts().Get(bind_inst->value_id);
  175. }
  176. auto param_inst = inst.As<SemIR::Param>();
  177. // Rebuild the param instruction.
  178. auto name_id = GetLocalNameId(param_inst.name_id);
  179. auto type_id = context_.GetTypeIdForTypeConstant(const_id);
  180. auto new_param_id = context_.AddInstInNoBlock(
  181. {Parse::NodeId::Invalid, SemIR::Param{type_id, name_id}});
  182. if (bind_inst) {
  183. auto bind_name_id = context_.bind_names().Add(
  184. {.name_id = name_id,
  185. .enclosing_scope_id = SemIR::NameScopeId::Invalid});
  186. switch (bind_inst->kind) {
  187. case SemIR::InstKind::BindName:
  188. new_param_id = context_.AddInstInNoBlock(
  189. {Parse::NodeId::Invalid,
  190. SemIR::BindName{type_id, bind_name_id, new_param_id}});
  191. break;
  192. case SemIR::InstKind::BindSymbolicName:
  193. new_param_id = context_.AddInstInNoBlock(
  194. {Parse::NodeId::Invalid,
  195. SemIR::BindSymbolicName{type_id, bind_name_id, new_param_id}});
  196. break;
  197. default:
  198. CARBON_FATAL() << "Unexpected kind: " << bind_inst->kind;
  199. }
  200. }
  201. if (addr_inst) {
  202. new_param_id = context_.AddInstInNoBlock(
  203. {Parse::NodeId::Invalid,
  204. SemIR::AddrPattern{type_id, new_param_id}});
  205. }
  206. new_param_refs.push_back(new_param_id);
  207. }
  208. return context_.inst_blocks().Add(new_param_refs);
  209. }
  210. // Translates a NameId from the import IR to a local NameId.
  211. auto GetLocalNameId(SemIR::NameId import_name_id) -> SemIR::NameId {
  212. if (auto ident_id = import_name_id.AsIdentifierId(); ident_id.is_valid()) {
  213. return SemIR::NameId::ForIdentifier(
  214. context_.identifiers().Add(import_ir_.identifiers().Get(ident_id)));
  215. }
  216. return import_name_id;
  217. }
  218. // Tries to resolve the InstId, returning a constant when ready, or Invalid if
  219. // more has been added to the stack. A similar API is followed for all
  220. // following TryResolveTypedInst helper functions.
  221. //
  222. // TODO: Error is returned when support is missing, but that should go away.
  223. auto TryResolveInst(SemIR::InstId inst_id, bool made_incomplete_type)
  224. -> SemIR::ConstantId {
  225. if (inst_id.is_builtin()) {
  226. CARBON_CHECK(!made_incomplete_type);
  227. // Constants for builtins can be directly copied.
  228. return context_.constant_values().Get(inst_id);
  229. }
  230. auto inst = import_ir_.insts().Get(inst_id);
  231. CARBON_CHECK(!made_incomplete_type ||
  232. inst.kind() == SemIR::InstKind::ClassDecl)
  233. << "Currently only decls with incomplete types should need "
  234. "made_incomplete_type states: "
  235. << inst.kind();
  236. switch (inst.kind()) {
  237. case SemIR::InstKind::BaseDecl:
  238. return TryResolveTypedInst(inst.As<SemIR::BaseDecl>());
  239. case SemIR::InstKind::BindAlias:
  240. return TryResolveTypedInst(inst.As<SemIR::BindAlias>());
  241. case SemIR::InstKind::ClassDecl:
  242. return TryResolveTypedInst(inst.As<SemIR::ClassDecl>(), inst_id,
  243. made_incomplete_type);
  244. case SemIR::InstKind::ClassType:
  245. return TryResolveTypedInst(inst.As<SemIR::ClassType>());
  246. case SemIR::InstKind::ConstType:
  247. return TryResolveTypedInst(inst.As<SemIR::ConstType>());
  248. case SemIR::InstKind::FieldDecl:
  249. return TryResolveTypedInst(inst.As<SemIR::FieldDecl>());
  250. case SemIR::InstKind::FunctionDecl:
  251. return TryResolveTypedInst(inst.As<SemIR::FunctionDecl>());
  252. case SemIR::InstKind::PointerType:
  253. return TryResolveTypedInst(inst.As<SemIR::PointerType>());
  254. case SemIR::InstKind::StructType:
  255. return TryResolveTypedInst(inst.As<SemIR::StructType>());
  256. case SemIR::InstKind::TupleType:
  257. return TryResolveTypedInst(inst.As<SemIR::TupleType>());
  258. case SemIR::InstKind::UnboundElementType:
  259. return TryResolveTypedInst(inst.As<SemIR::UnboundElementType>());
  260. case SemIR::InstKind::BindName:
  261. case SemIR::InstKind::BindSymbolicName:
  262. // Can use TryEvalInst because the resulting constant doesn't really use
  263. // `inst`.
  264. return TryEvalInst(context_, inst_id, inst);
  265. case SemIR::InstKind::InterfaceDecl:
  266. // TODO: Not implemented.
  267. return SemIR::ConstantId::Error;
  268. default:
  269. context_.TODO(
  270. Parse::NodeId::Invalid,
  271. llvm::formatv("TryResolveInst on {0}", inst.kind()).str());
  272. return SemIR::ConstantId::Error;
  273. }
  274. }
  275. auto TryResolveTypedInst(SemIR::BaseDecl inst) -> SemIR::ConstantId {
  276. auto initial_work = work_stack_.size();
  277. auto type_const_id = GetLocalConstantId(inst.type_id);
  278. auto base_type_const_id = GetLocalConstantId(inst.base_type_id);
  279. if (HasNewWork(initial_work)) {
  280. return SemIR::ConstantId::Invalid;
  281. }
  282. // Import the instruction in order to update contained base_type_id.
  283. auto inst_id = context_.AddInstInNoBlock(
  284. {Parse::NodeId::Invalid,
  285. SemIR::BaseDecl{context_.GetTypeIdForTypeConstant(type_const_id),
  286. context_.GetTypeIdForTypeConstant(base_type_const_id),
  287. inst.index}});
  288. return context_.constant_values().Get(inst_id);
  289. }
  290. auto TryResolveTypedInst(SemIR::BindAlias inst) -> SemIR::ConstantId {
  291. auto initial_work = work_stack_.size();
  292. auto value_id = GetLocalConstantId(inst.value_id);
  293. if (HasNewWork(initial_work)) {
  294. return SemIR::ConstantId::Invalid;
  295. }
  296. return value_id;
  297. }
  298. // Makes an incomplete class. This is necessary even with classes with a
  299. // complete declaration, because things such as `Self` may refer back to the
  300. // type.
  301. auto MakeIncompleteClass(SemIR::InstId inst_id,
  302. const SemIR::Class& import_class)
  303. -> SemIR::ConstantId {
  304. auto class_decl =
  305. SemIR::ClassDecl{SemIR::TypeId::Invalid, SemIR::ClassId::Invalid,
  306. SemIR::InstBlockId::Empty};
  307. auto class_decl_id =
  308. context_.AddPlaceholderInst({Parse::NodeId::Invalid, class_decl});
  309. // Regardless of whether ClassDecl is a complete type, we first need an
  310. // incomplete type so that any references have something to point at.
  311. class_decl.class_id = context_.classes().Add({
  312. .name_id = GetLocalNameId(import_class.name_id),
  313. .enclosing_scope_id = NoEnclosingScopeForImports,
  314. // `.self_type_id` depends on the ClassType, so is set below.
  315. .self_type_id = SemIR::TypeId::Invalid,
  316. .decl_id = class_decl_id,
  317. .inheritance_kind = import_class.inheritance_kind,
  318. });
  319. // Write the function ID into the ClassDecl.
  320. context_.ReplaceInstBeforeConstantUse(class_decl_id,
  321. {Parse::NodeId::Invalid, class_decl});
  322. auto self_const_id = context_.constant_values().Get(class_decl_id);
  323. // Build the `Self` type using the resulting type constant.
  324. auto& class_info = context_.classes().Get(class_decl.class_id);
  325. class_info.self_type_id = context_.GetTypeIdForTypeConstant(self_const_id);
  326. // Set a constant corresponding to the incomplete class.
  327. import_ir_constant_values_.Set(inst_id, self_const_id);
  328. return self_const_id;
  329. }
  330. // Fills out the class definition for an incomplete class.
  331. auto AddClassDefinition(const SemIR::Class& import_class,
  332. SemIR::ConstantId class_const_id,
  333. SemIR::ConstantId object_repr_const_id,
  334. SemIR::ConstantId base_const_id) -> void {
  335. auto& new_class = context_.classes().Get(
  336. context_.insts()
  337. .GetAs<SemIR::ClassType>(class_const_id.inst_id())
  338. .class_id);
  339. new_class.object_repr_id =
  340. context_.GetTypeIdForTypeConstant(object_repr_const_id);
  341. new_class.scope_id =
  342. context_.name_scopes().Add(new_class.decl_id, SemIR::NameId::Invalid,
  343. new_class.enclosing_scope_id);
  344. auto& new_scope = context_.name_scopes().Get(new_class.scope_id);
  345. const auto& old_scope = import_ir_.name_scopes().Get(import_class.scope_id);
  346. // Push a block so that we can add scoped instructions to it, primarily for
  347. // textual IR formatting.
  348. context_.inst_block_stack().Push();
  349. for (auto [entry_name_id, entry_inst_id] : old_scope.names) {
  350. CARBON_CHECK(
  351. new_scope.names
  352. .insert({GetLocalNameId(entry_name_id),
  353. context_.AddPlaceholderInst(SemIR::ImportRefUnused{
  354. import_ir_id_, entry_inst_id})})
  355. .second);
  356. }
  357. new_class.body_block_id = context_.inst_block_stack().Pop();
  358. if (import_class.base_id.is_valid()) {
  359. new_class.base_id = base_const_id.inst_id();
  360. // Add the base scope to extended scopes.
  361. auto base_inst_id = context_.types().GetInstId(
  362. context_.insts()
  363. .GetAs<SemIR::BaseDecl>(new_class.base_id)
  364. .base_type_id);
  365. const auto& base_class = context_.classes().Get(
  366. context_.insts().GetAs<SemIR::ClassType>(base_inst_id).class_id);
  367. new_scope.extended_scopes.push_back(base_class.scope_id);
  368. }
  369. CARBON_CHECK(new_scope.extended_scopes.size() ==
  370. old_scope.extended_scopes.size());
  371. }
  372. auto TryResolveTypedInst(SemIR::ClassDecl inst, SemIR::InstId inst_id,
  373. bool made_incomplete_type) -> SemIR::ConstantId {
  374. const auto& import_class = import_ir_.classes().Get(inst.class_id);
  375. SemIR::ConstantId class_const_id = SemIR::ConstantId::Invalid;
  376. // On the first pass, there's no incomplete type; start by adding one for
  377. // any recursive references.
  378. if (!made_incomplete_type) {
  379. class_const_id = MakeIncompleteClass(inst_id, import_class);
  380. // If there's only a forward declaration, we're done.
  381. if (!import_class.object_repr_id.is_valid()) {
  382. return class_const_id;
  383. }
  384. // This may not be needed because all constants might be ready, but we do
  385. // it here so that we don't need to track which work item corresponds to
  386. // this instruction.
  387. work_stack_.back().made_incomplete_type = true;
  388. }
  389. CARBON_CHECK(import_class.object_repr_id.is_valid())
  390. << "Only reachable when there's a definition.";
  391. // Load constants for the definition.
  392. auto initial_work = work_stack_.size();
  393. auto object_repr_const_id = GetLocalConstantId(import_class.object_repr_id);
  394. auto base_const_id = import_class.base_id.is_valid()
  395. ? GetLocalConstantId(import_class.base_id)
  396. : SemIR::ConstantId::Invalid;
  397. if (HasNewWork(initial_work)) {
  398. return SemIR::ConstantId::Invalid;
  399. }
  400. // On the first pass, we build the incomplete type's constant above. If we
  401. // get here on a subsequent pass we need to fetch the one we built in the
  402. // first pass.
  403. if (made_incomplete_type) {
  404. CARBON_CHECK(!class_const_id.is_valid())
  405. << "Shouldn't have a const yet when resuming";
  406. class_const_id = import_ir_constant_values_.Get(inst_id);
  407. }
  408. AddClassDefinition(import_class, class_const_id, object_repr_const_id,
  409. base_const_id);
  410. return class_const_id;
  411. }
  412. auto TryResolveTypedInst(SemIR::ClassType inst) -> SemIR::ConstantId {
  413. CARBON_CHECK(inst.type_id == SemIR::TypeId::TypeType);
  414. // ClassType uses a straight reference to the constant ID generated as part
  415. // of pulling in the ClassDecl, so there's no need to phase logic.
  416. return GetLocalConstantId(import_ir_.classes().Get(inst.class_id).decl_id);
  417. }
  418. auto TryResolveTypedInst(SemIR::ConstType inst) -> SemIR::ConstantId {
  419. auto initial_work = work_stack_.size();
  420. CARBON_CHECK(inst.type_id == SemIR::TypeId::TypeType);
  421. auto inner_const_id = GetLocalConstantId(inst.inner_id);
  422. if (HasNewWork(initial_work)) {
  423. return SemIR::ConstantId::Invalid;
  424. }
  425. auto inner_type_id = context_.GetTypeIdForTypeConstant(inner_const_id);
  426. // TODO: Should ConstType have a wrapper for this similar to the others?
  427. return TryEvalInst(
  428. context_, SemIR::InstId::Invalid,
  429. SemIR::ConstType{SemIR::TypeId::TypeType, inner_type_id});
  430. }
  431. auto TryResolveTypedInst(SemIR::FieldDecl inst) -> SemIR::ConstantId {
  432. auto initial_work = work_stack_.size();
  433. auto const_id = GetLocalConstantId(inst.type_id);
  434. if (HasNewWork(initial_work)) {
  435. return SemIR::ConstantId::Invalid;
  436. }
  437. auto inst_id = context_.AddInstInNoBlock(
  438. {Parse::NodeId::Invalid,
  439. SemIR::FieldDecl{context_.GetTypeIdForTypeConstant(const_id),
  440. GetLocalNameId(inst.name_id), inst.index}});
  441. return context_.constant_values().Get(inst_id);
  442. }
  443. auto TryResolveTypedInst(SemIR::FunctionDecl inst) -> SemIR::ConstantId {
  444. auto initial_work = work_stack_.size();
  445. auto type_const_id = GetLocalConstantId(inst.type_id);
  446. const auto& function = import_ir_.functions().Get(inst.function_id);
  447. auto return_type_const_id = SemIR::ConstantId::Invalid;
  448. if (function.return_type_id.is_valid()) {
  449. return_type_const_id = GetLocalConstantId(function.return_type_id);
  450. }
  451. auto return_slot_const_id = SemIR::ConstantId::Invalid;
  452. if (function.return_slot_id.is_valid()) {
  453. return_slot_const_id = GetLocalConstantId(function.return_slot_id);
  454. }
  455. llvm::SmallVector<SemIR::ConstantId> implicit_param_const_ids =
  456. GetLocalParamConstantIds(function.implicit_param_refs_id);
  457. llvm::SmallVector<SemIR::ConstantId> param_const_ids =
  458. GetLocalParamConstantIds(function.param_refs_id);
  459. if (HasNewWork(initial_work)) {
  460. return SemIR::ConstantId::Invalid;
  461. }
  462. // Add the function declaration.
  463. auto function_decl =
  464. SemIR::FunctionDecl{context_.GetTypeIdForTypeConstant(type_const_id),
  465. SemIR::FunctionId::Invalid};
  466. auto function_decl_id = context_.AddPlaceholderInstInNoBlock(
  467. {Parse::NodeId::Invalid, function_decl});
  468. auto new_return_type_id =
  469. return_type_const_id.is_valid()
  470. ? context_.GetTypeIdForTypeConstant(return_type_const_id)
  471. : SemIR::TypeId::Invalid;
  472. auto new_return_slot = SemIR::InstId::Invalid;
  473. if (function.return_slot_id.is_valid()) {
  474. context_.AddInstInNoBlock({SemIR::ImportRefUsed{
  475. context_.GetTypeIdForTypeConstant(return_slot_const_id),
  476. import_ir_id_, function.return_slot_id}});
  477. }
  478. function_decl.function_id = context_.functions().Add(
  479. {.name_id = GetLocalNameId(function.name_id),
  480. .enclosing_scope_id = NoEnclosingScopeForImports,
  481. .decl_id = function_decl_id,
  482. .implicit_param_refs_id = GetLocalParamRefsId(
  483. function.implicit_param_refs_id, implicit_param_const_ids),
  484. .param_refs_id =
  485. GetLocalParamRefsId(function.param_refs_id, param_const_ids),
  486. .return_type_id = new_return_type_id,
  487. .return_slot_id = new_return_slot});
  488. // Write the function ID into the FunctionDecl.
  489. context_.ReplaceInstBeforeConstantUse(
  490. function_decl_id, {Parse::NodeId::Invalid, function_decl});
  491. return context_.constant_values().Get(function_decl_id);
  492. }
  493. auto TryResolveTypedInst(SemIR::PointerType inst) -> SemIR::ConstantId {
  494. auto initial_work = work_stack_.size();
  495. CARBON_CHECK(inst.type_id == SemIR::TypeId::TypeType);
  496. auto pointee_const_id = GetLocalConstantId(inst.pointee_id);
  497. if (HasNewWork(initial_work)) {
  498. return SemIR::ConstantId::Invalid;
  499. }
  500. auto pointee_type_id = context_.GetTypeIdForTypeConstant(pointee_const_id);
  501. return context_.types().GetConstantId(
  502. context_.GetPointerType(pointee_type_id));
  503. }
  504. auto TryResolveTypedInst(SemIR::StructType inst) -> SemIR::ConstantId {
  505. // Collect all constants first, locating unresolved ones in a single pass.
  506. auto initial_work = work_stack_.size();
  507. CARBON_CHECK(inst.type_id == SemIR::TypeId::TypeType);
  508. auto orig_fields = import_ir_.inst_blocks().Get(inst.fields_id);
  509. llvm::SmallVector<SemIR::ConstantId> field_const_ids;
  510. field_const_ids.reserve(orig_fields.size());
  511. for (auto field_id : orig_fields) {
  512. auto field = import_ir_.insts().GetAs<SemIR::StructTypeField>(field_id);
  513. field_const_ids.push_back(GetLocalConstantId(field.field_type_id));
  514. }
  515. if (HasNewWork(initial_work)) {
  516. return SemIR::ConstantId::Invalid;
  517. }
  518. // Prepare a vector of fields for GetStructType.
  519. // TODO: Should we have field constants so that we can deduplicate fields
  520. // without creating instructions here?
  521. llvm::SmallVector<SemIR::InstId> fields;
  522. fields.reserve(orig_fields.size());
  523. for (auto [field_id, field_const_id] :
  524. llvm::zip(orig_fields, field_const_ids)) {
  525. auto field = import_ir_.insts().GetAs<SemIR::StructTypeField>(field_id);
  526. auto name_id = GetLocalNameId(field.name_id);
  527. auto field_type_id = context_.GetTypeIdForTypeConstant(field_const_id);
  528. fields.push_back(context_.AddInstInNoBlock(
  529. {Parse::NodeId::Invalid,
  530. SemIR::StructTypeField{.name_id = name_id,
  531. .field_type_id = field_type_id}}));
  532. }
  533. return context_.types().GetConstantId(
  534. context_.GetStructType(context_.inst_blocks().Add(fields)));
  535. }
  536. auto TryResolveTypedInst(SemIR::TupleType inst) -> SemIR::ConstantId {
  537. CARBON_CHECK(inst.type_id == SemIR::TypeId::TypeType);
  538. // Collect all constants first, locating unresolved ones in a single pass.
  539. auto initial_work = work_stack_.size();
  540. auto orig_elem_type_ids = import_ir_.type_blocks().Get(inst.elements_id);
  541. llvm::SmallVector<SemIR::ConstantId> elem_const_ids;
  542. elem_const_ids.reserve(orig_elem_type_ids.size());
  543. for (auto elem_type_id : orig_elem_type_ids) {
  544. elem_const_ids.push_back(GetLocalConstantId(elem_type_id));
  545. }
  546. if (HasNewWork(initial_work)) {
  547. return SemIR::ConstantId::Invalid;
  548. }
  549. // Prepare a vector of the tuple types for GetTupleType.
  550. llvm::SmallVector<SemIR::TypeId> elem_type_ids;
  551. elem_type_ids.reserve(orig_elem_type_ids.size());
  552. for (auto elem_const_id : elem_const_ids) {
  553. elem_type_ids.push_back(context_.GetTypeIdForTypeConstant(elem_const_id));
  554. }
  555. return context_.types().GetConstantId(context_.GetTupleType(elem_type_ids));
  556. }
  557. auto TryResolveTypedInst(SemIR::UnboundElementType inst)
  558. -> SemIR::ConstantId {
  559. auto initial_work = work_stack_.size();
  560. CARBON_CHECK(inst.type_id == SemIR::TypeId::TypeType);
  561. auto class_const_id = GetLocalConstantId(inst.class_type_id);
  562. auto elem_const_id = GetLocalConstantId(inst.element_type_id);
  563. if (HasNewWork(initial_work)) {
  564. return SemIR::ConstantId::Invalid;
  565. }
  566. return context_.types().GetConstantId(context_.GetUnboundElementType(
  567. context_.GetTypeIdForTypeConstant(class_const_id),
  568. context_.GetTypeIdForTypeConstant(elem_const_id)));
  569. }
  570. Context& context_;
  571. SemIR::ImportIRId import_ir_id_;
  572. const SemIR::File& import_ir_;
  573. SemIR::ConstantValueStore& import_ir_constant_values_;
  574. llvm::SmallVector<Work> work_stack_;
  575. };
  576. auto TryResolveImportRefUnused(Context& context, SemIR::InstId inst_id)
  577. -> void {
  578. auto inst = context.insts().Get(inst_id);
  579. auto import_ref = inst.TryAs<SemIR::ImportRefUnused>();
  580. if (!import_ref) {
  581. return;
  582. }
  583. const SemIR::File& import_ir = *context.import_irs().Get(import_ref->ir_id);
  584. auto import_inst = import_ir.insts().Get(import_ref->inst_id);
  585. ImportRefResolver resolver(context, import_ref->ir_id);
  586. auto type_id = resolver.ResolveType(import_inst.type_id());
  587. auto constant_id = resolver.Resolve(import_ref->inst_id);
  588. // TODO: Once ClassDecl/InterfaceDecl are supported (no longer return Error),
  589. // remove this.
  590. if (constant_id == SemIR::ConstantId::Error) {
  591. type_id = SemIR::TypeId::Error;
  592. }
  593. // Replace the ImportRefUnused instruction with an ImportRefUsed. This doesn't
  594. // use ReplaceInstBeforeConstantUse because it would trigger TryEvalInst, and
  595. // we're instead doing constant evaluation here in order to minimize recursion
  596. // risks.
  597. context.sem_ir().insts().Set(
  598. inst_id,
  599. SemIR::ImportRefUsed{type_id, import_ref->ir_id, import_ref->inst_id});
  600. // Store the constant for both the ImportRefUsed and imported instruction.
  601. context.constant_values().Set(inst_id, constant_id);
  602. }
  603. } // namespace Carbon::Check