impl_lookup.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // TODO: Add a better impl lookup system. At the very least, we should only be
  120. // considering impls that are for the same interface we're querying. We can
  121. // also skip impls that mention any types that aren't part of our impl query.
  122. for (const auto& impl : context.impls().array_ref()) {
  123. auto specific_id = SemIR::SpecificId::None;
  124. if (impl.generic_id.has_value()) {
  125. specific_id = DeduceImplArguments(context, loc_id, impl, type_const_id,
  126. interface_const_id);
  127. if (!specific_id.has_value()) {
  128. continue;
  129. }
  130. }
  131. if (!context.constant_values().AreEqualAcrossDeclarations(
  132. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  133. impl.self_id),
  134. type_const_id)) {
  135. continue;
  136. }
  137. if (!context.constant_values().AreEqualAcrossDeclarations(
  138. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  139. impl.constraint_id),
  140. interface_const_id)) {
  141. // TODO: An impl of a constraint type should be treated as implementing
  142. // the constraint's interfaces.
  143. continue;
  144. }
  145. if (!impl.witness_id.has_value()) {
  146. // TODO: Diagnose if the impl isn't defined yet?
  147. return SemIR::InstId::None;
  148. }
  149. LoadImportRef(context, impl.witness_id);
  150. if (specific_id.has_value()) {
  151. // We need a definition of the specific `impl` so we can access its
  152. // witness.
  153. ResolveSpecificDefinition(context, loc_id, specific_id);
  154. }
  155. return context.constant_values().GetInstId(
  156. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  157. impl.witness_id));
  158. }
  159. return SemIR::InstId::None;
  160. }
  161. } // namespace Carbon::Check