impl_lookup.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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/impl_lookup.h"
  5. #include "toolchain/check/deduce.h"
  6. #include "toolchain/check/generic.h"
  7. #include "toolchain/check/import_ref.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::Check {
  11. static auto FindAssociatedImportIRs(Context& context,
  12. SemIR::ConstantId type_const_id,
  13. SemIR::ConstantId interface_const_id)
  14. -> llvm::SmallVector<SemIR::ImportIRId> {
  15. llvm::SmallVector<SemIR::ImportIRId> result;
  16. // Add an entity to our result.
  17. auto add_entity = [&](const SemIR::EntityWithParamsBase& entity) {
  18. // We will look for impls in the import IR associated with the first owning
  19. // declaration.
  20. auto decl_id = entity.first_owning_decl_id;
  21. if (!decl_id.has_value()) {
  22. return;
  23. }
  24. if (auto ir_id = GetCanonicalImportIRInst(context, decl_id).ir_id;
  25. ir_id.has_value()) {
  26. result.push_back(ir_id);
  27. }
  28. };
  29. llvm::SmallVector<SemIR::InstId> worklist;
  30. worklist.push_back(context.constant_values().GetInstId(type_const_id));
  31. worklist.push_back(context.constant_values().GetInstId(interface_const_id));
  32. // Push the contents of an instruction block onto our worklist.
  33. auto push_block = [&](SemIR::InstBlockId block_id) {
  34. if (block_id.has_value()) {
  35. llvm::append_range(worklist, context.inst_blocks().Get(block_id));
  36. }
  37. };
  38. // Add the arguments of a specific to the worklist.
  39. auto push_args = [&](SemIR::SpecificId specific_id) {
  40. if (specific_id.has_value()) {
  41. push_block(context.specifics().Get(specific_id).args_id);
  42. }
  43. };
  44. while (!worklist.empty()) {
  45. auto inst_id = worklist.pop_back_val();
  46. // Visit the operands of the constant.
  47. auto inst = context.insts().Get(inst_id);
  48. auto [arg0_kind, arg1_kind] = inst.ArgKinds();
  49. for (auto [arg, kind] :
  50. {std::pair{inst.arg0(), arg0_kind}, {inst.arg1(), arg1_kind}}) {
  51. switch (kind) {
  52. case SemIR::IdKind::For<SemIR::InstId>: {
  53. if (auto id = SemIR::InstId(arg); id.has_value()) {
  54. worklist.push_back(id);
  55. }
  56. break;
  57. }
  58. case SemIR::IdKind::For<SemIR::InstBlockId>: {
  59. push_block(SemIR::InstBlockId(arg));
  60. break;
  61. }
  62. case SemIR::IdKind::For<SemIR::ClassId>: {
  63. add_entity(context.classes().Get(SemIR::ClassId(arg)));
  64. break;
  65. }
  66. case SemIR::IdKind::For<SemIR::InterfaceId>: {
  67. add_entity(context.interfaces().Get(SemIR::InterfaceId(arg)));
  68. break;
  69. }
  70. case SemIR::IdKind::For<SemIR::FacetTypeId>: {
  71. const auto& facet_type_info =
  72. context.facet_types().Get(SemIR::FacetTypeId(arg));
  73. for (const auto& impl : facet_type_info.impls_constraints) {
  74. add_entity(context.interfaces().Get(impl.interface_id));
  75. push_args(impl.specific_id);
  76. }
  77. break;
  78. }
  79. case SemIR::IdKind::For<SemIR::FunctionId>: {
  80. add_entity(context.functions().Get(SemIR::FunctionId(arg)));
  81. break;
  82. }
  83. case SemIR::IdKind::For<SemIR::SpecificId>: {
  84. push_args(SemIR::SpecificId(arg));
  85. break;
  86. }
  87. default: {
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. // Deduplicate.
  94. llvm::sort(result, [](SemIR::ImportIRId a, SemIR::ImportIRId b) {
  95. return a.index < b.index;
  96. });
  97. result.erase(llvm::unique(result), result.end());
  98. return result;
  99. }
  100. auto LookupImplWitness(Context& context, SemIR::LocId loc_id,
  101. SemIR::ConstantId type_const_id,
  102. SemIR::ConstantId interface_const_id) -> SemIR::InstId {
  103. if (type_const_id == SemIR::ErrorInst::SingletonConstantId ||
  104. interface_const_id == SemIR::ErrorInst::SingletonConstantId) {
  105. return SemIR::ErrorInst::SingletonInstId;
  106. }
  107. auto import_irs =
  108. FindAssociatedImportIRs(context, type_const_id, interface_const_id);
  109. for (auto import_ir : import_irs) {
  110. // TODO: Instead of importing all impls, only import ones that are in some
  111. // way connected to this query.
  112. for (auto impl_index : llvm::seq(
  113. context.import_irs().Get(import_ir).sem_ir->impls().size())) {
  114. // TODO: Track the relevant impls and only consider those ones and any
  115. // local impls, rather than looping over all impls below.
  116. ImportImpl(context, import_ir, SemIR::ImplId(impl_index));
  117. }
  118. }
  119. for (const auto& impl : context.impls().array_ref()) {
  120. // If impl.constraint_id is not symbolic, and doesn't match the query, then
  121. // we don't need to proceed.
  122. auto impl_interface_const_id =
  123. context.constant_values().Get(impl.constraint_id);
  124. if (!impl_interface_const_id.is_symbolic() &&
  125. interface_const_id != impl_interface_const_id) {
  126. continue;
  127. }
  128. // TODO: If the interface id of the `impl` and the query are not the same,
  129. // then we can skip this `impl`. (The interface id is the root of the
  130. // constraint, the unique `interface` declaration.)
  131. auto specific_id = SemIR::SpecificId::None;
  132. // This check comes first to avoid deduction with an invalid impl. We use an
  133. // error value to indicate an error during creation of the impl, such as a
  134. // recursive impl which will cause deduction to recurse infinitely.
  135. if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
  136. continue;
  137. }
  138. if (impl.generic_id.has_value()) {
  139. specific_id = DeduceImplArguments(context, loc_id, impl, type_const_id,
  140. interface_const_id);
  141. if (!specific_id.has_value()) {
  142. continue;
  143. }
  144. }
  145. if (!context.constant_values().AreEqualAcrossDeclarations(
  146. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  147. impl.self_id),
  148. type_const_id)) {
  149. continue;
  150. }
  151. if (!context.constant_values().AreEqualAcrossDeclarations(
  152. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  153. impl.constraint_id),
  154. interface_const_id)) {
  155. // TODO: An impl of a constraint type should be treated as implementing
  156. // the constraint's interfaces.
  157. continue;
  158. }
  159. if (!impl.witness_id.has_value()) {
  160. // TODO: Diagnose if the impl isn't defined yet?
  161. return SemIR::InstId::None;
  162. }
  163. LoadImportRef(context, impl.witness_id);
  164. if (specific_id.has_value()) {
  165. // We need a definition of the specific `impl` so we can access its
  166. // witness.
  167. ResolveSpecificDefinition(context, loc_id, specific_id);
  168. }
  169. return context.constant_values().GetInstId(
  170. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  171. impl.witness_id));
  172. }
  173. return SemIR::InstId::None;
  174. }
  175. } // namespace Carbon::Check