impl_lookup.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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::FunctionId>: {
  62. add_entity(context.functions().Get(SemIR::FunctionId(arg)));
  63. break;
  64. }
  65. default: {
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. // Deduplicate.
  72. std::sort(result.begin(), result.end(),
  73. [](SemIR::ImportIRId a, SemIR::ImportIRId b) {
  74. return a.index < b.index;
  75. });
  76. result.erase(std::unique(result.begin(), result.end()), result.end());
  77. return result;
  78. }
  79. auto LookupInterfaceWitness(Context& context, SemIR::LocId loc_id,
  80. SemIR::ConstantId type_const_id,
  81. SemIR::ConstantId interface_const_id)
  82. -> SemIR::InstId {
  83. auto import_irs =
  84. FindAssociatedImportIRs(context, type_const_id, interface_const_id);
  85. for (auto import_ir : import_irs) {
  86. // TODO: Instead of importing all impls, only import ones that are in some
  87. // way connected to this query.
  88. for (auto impl_index : llvm::seq(
  89. context.import_irs().Get(import_ir).sem_ir->impls().size())) {
  90. // TODO: Track the relevant impls and only consider those ones and any
  91. // local impls, rather than looping over all impls below.
  92. ImportImpl(context, import_ir, SemIR::ImplId(impl_index));
  93. }
  94. }
  95. // TODO: Add a better impl lookup system. At the very least, we should only be
  96. // considering impls that are for the same interface we're querying. We can
  97. // also skip impls that mention any types that aren't part of our impl query.
  98. for (const auto& impl : context.impls().array_ref()) {
  99. auto specific_id = SemIR::SpecificId::Invalid;
  100. if (impl.generic_id.is_valid()) {
  101. specific_id = DeduceImplArguments(context, loc_id, impl, type_const_id,
  102. interface_const_id);
  103. if (!specific_id.is_valid()) {
  104. continue;
  105. }
  106. }
  107. if (!context.constant_values().AreEqualAcrossDeclarations(
  108. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  109. impl.self_id),
  110. type_const_id)) {
  111. continue;
  112. }
  113. if (!context.constant_values().AreEqualAcrossDeclarations(
  114. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  115. impl.constraint_id),
  116. interface_const_id)) {
  117. // TODO: An impl of a constraint type should be treated as implementing
  118. // the constraint's interfaces.
  119. continue;
  120. }
  121. if (!impl.witness_id.is_valid()) {
  122. // TODO: Diagnose if the impl isn't defined yet?
  123. return SemIR::InstId::Invalid;
  124. }
  125. LoadImportRef(context, impl.witness_id);
  126. if (specific_id.is_valid()) {
  127. // We need a definition of the specific `impl` so we can access its
  128. // witness.
  129. ResolveSpecificDefinition(context, specific_id);
  130. }
  131. return context.constant_values().GetInstId(
  132. SemIR::GetConstantValueInSpecific(context.sem_ir(), specific_id,
  133. impl.witness_id));
  134. }
  135. return SemIR::InstId::Invalid;
  136. }
  137. } // namespace Carbon::Check