impl_lookup.cpp 6.2 KB

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