impl_lookup.cpp 5.8 KB

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