|
| 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 | + |
| 5 | +#include "toolchain/check/cpp/impl_lookup.h" |
| 6 | + |
| 7 | +#include "clang/Sema/Sema.h" |
| 8 | +#include "toolchain/base/kind_switch.h" |
| 9 | +#include "toolchain/check/cpp/import.h" |
| 10 | +#include "toolchain/check/cpp/location.h" |
| 11 | +#include "toolchain/check/cpp/overload_resolution.h" |
| 12 | +#include "toolchain/check/impl.h" |
| 13 | +#include "toolchain/check/impl_lookup.h" |
| 14 | +#include "toolchain/check/import_ref.h" |
| 15 | +#include "toolchain/check/inst.h" |
| 16 | +#include "toolchain/check/type.h" |
| 17 | +#include "toolchain/sem_ir/ids.h" |
| 18 | +#include "toolchain/sem_ir/typed_insts.h" |
| 19 | + |
| 20 | +namespace Carbon::Check { |
| 21 | + |
| 22 | +// If the given type is a C++ class type, returns the corresponding class |
| 23 | +// declaration. Otherwise returns nullptr. |
| 24 | +// TODO: Handle qualified types. |
| 25 | +static auto TypeAsClassDecl(Context& context, SemIR::TypeId type_id) |
| 26 | + -> clang::CXXRecordDecl* { |
| 27 | + auto class_type = context.types().TryGetAs<SemIR::ClassType>(type_id); |
| 28 | + if (!class_type) { |
| 29 | + // Not a class. |
| 30 | + return nullptr; |
| 31 | + } |
| 32 | + |
| 33 | + SemIR::NameScopeId class_scope_id = |
| 34 | + context.classes().Get(class_type->class_id).scope_id; |
| 35 | + if (!class_scope_id.has_value()) { |
| 36 | + return nullptr; |
| 37 | + } |
| 38 | + |
| 39 | + const auto& scope = context.name_scopes().Get(class_scope_id); |
| 40 | + auto decl_id = scope.clang_decl_context_id(); |
| 41 | + if (!decl_id.has_value()) { |
| 42 | + return nullptr; |
| 43 | + } |
| 44 | + |
| 45 | + return dyn_cast<clang::CXXRecordDecl>( |
| 46 | + context.clang_decls().Get(decl_id).key.decl); |
| 47 | +} |
| 48 | + |
| 49 | +// Builds a witness that the given type implements the given interface, |
| 50 | +// populating it with the specified set of values. Returns a corresponding |
| 51 | +// lookup result. Produces a diagnostic and returns `None` if the specified |
| 52 | +// values aren't suitable for the interface. |
| 53 | +static auto BuildWitness(Context& context, SemIR::LocId loc_id, |
| 54 | + SemIR::TypeId self_type_id, |
| 55 | + SemIR::SpecificInterface specific_interface, |
| 56 | + llvm::ArrayRef<SemIR::InstId> values) |
| 57 | + -> SemIR::InstId { |
| 58 | + const auto& interface = |
| 59 | + context.interfaces().Get(specific_interface.interface_id); |
| 60 | + auto assoc_entities = |
| 61 | + context.inst_blocks().GetOrEmpty(interface.associated_entities_id); |
| 62 | + if (assoc_entities.size() != values.size()) { |
| 63 | + context.TODO(loc_id, ("Unsupported definition of interface " + |
| 64 | + context.names().GetFormatted(interface.name_id)) |
| 65 | + .str()); |
| 66 | + return SemIR::ErrorInst::InstId; |
| 67 | + } |
| 68 | + |
| 69 | + // Prepare an empty witness table. |
| 70 | + auto witness_table_id = |
| 71 | + context.inst_blocks().AddUninitialized(assoc_entities.size()); |
| 72 | + auto witness_table = context.inst_blocks().GetMutable(witness_table_id); |
| 73 | + for (auto& witness_value_id : witness_table) { |
| 74 | + witness_value_id = SemIR::InstId::ImplWitnessTablePlaceholder; |
| 75 | + } |
| 76 | + |
| 77 | + // Build a witness. We use an `ImplWitness` with an `impl_id` of `None` to |
| 78 | + // represent a synthesized witness. |
| 79 | + // TODO: Stop using `ImplWitnessTable` here and add a distinct instruction |
| 80 | + // that doesn't contain an `InstId` and supports deduplication. |
| 81 | + auto witness_table_inst_id = AddInst<SemIR::ImplWitnessTable>( |
| 82 | + context, loc_id, |
| 83 | + {.elements_id = witness_table_id, .impl_id = SemIR::ImplId::None}); |
| 84 | + auto witness_id = AddInst<SemIR::ImplWitness>( |
| 85 | + context, loc_id, |
| 86 | + {.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId), |
| 87 | + .witness_table_id = witness_table_inst_id, |
| 88 | + .specific_id = SemIR::SpecificId::None}); |
| 89 | + |
| 90 | + // Fill in the witness table. |
| 91 | + for (const auto& [assoc_entity_id, value_id, witness_value_id] : |
| 92 | + llvm::zip_equal(assoc_entities, values, witness_table)) { |
| 93 | + LoadImportRef(context, assoc_entity_id); |
| 94 | + auto decl_id = |
| 95 | + context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific( |
| 96 | + context.sem_ir(), specific_interface.specific_id, assoc_entity_id)); |
| 97 | + CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity"); |
| 98 | + auto decl = context.insts().Get(decl_id); |
| 99 | + CARBON_KIND_SWITCH(decl) { |
| 100 | + case CARBON_KIND(SemIR::StructValue struct_value): { |
| 101 | + if (struct_value.type_id == SemIR::ErrorInst::TypeId) { |
| 102 | + return SemIR::ErrorInst::InstId; |
| 103 | + } |
| 104 | + witness_value_id = CheckAssociatedFunctionImplementation( |
| 105 | + context, |
| 106 | + context.types().GetAs<SemIR::FunctionType>(struct_value.type_id), |
| 107 | + value_id, self_type_id, witness_id, |
| 108 | + /*defer_thunk_definition=*/false); |
| 109 | + break; |
| 110 | + } |
| 111 | + case SemIR::AssociatedConstantDecl::Kind: { |
| 112 | + context.TODO(loc_id, |
| 113 | + "Associated constant in interface with synthesized impl"); |
| 114 | + return SemIR::ErrorInst::InstId; |
| 115 | + } |
| 116 | + default: |
| 117 | + CARBON_CHECK(decl_id == SemIR::ErrorInst::InstId, |
| 118 | + "Unexpected kind of associated entity {0}", decl); |
| 119 | + return SemIR::ErrorInst::InstId; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return witness_id; |
| 124 | +} |
| 125 | + |
| 126 | +static auto LookupCopyImpl(Context& context, SemIR::LocId loc_id, |
| 127 | + SemIR::TypeId self_type_id, |
| 128 | + SemIR::SpecificInterface specific_interface) |
| 129 | + -> SemIR::InstId { |
| 130 | + auto* class_decl = TypeAsClassDecl(context, self_type_id); |
| 131 | + if (!class_decl) { |
| 132 | + // TODO: Should we also provide a `Copy` implementation for enumerations? |
| 133 | + return SemIR::InstId::None; |
| 134 | + } |
| 135 | + |
| 136 | + auto* ctor = context.clang_sema().LookupCopyingConstructor( |
| 137 | + class_decl, clang::Qualifiers::Const); |
| 138 | + if (!ctor) { |
| 139 | + // TODO: If the impl lookup failure is an error, we should produce a |
| 140 | + // diagnostic explaining why the class is not copyable. |
| 141 | + return SemIR::InstId::None; |
| 142 | + } |
| 143 | + |
| 144 | + auto ctor_id = |
| 145 | + context.clang_sema().DiagnoseUseOfOverloadedDecl( |
| 146 | + ctor, GetCppLocation(context, loc_id)) |
| 147 | + ? SemIR::ErrorInst::InstId |
| 148 | + : ImportCppFunctionDecl(context, loc_id, ctor, /*num_params=*/1); |
| 149 | + if (auto ctor_decl = |
| 150 | + context.insts().TryGetAsWithId<SemIR::FunctionDecl>(ctor_id)) { |
| 151 | + CheckCppOverloadAccess(context, loc_id, |
| 152 | + clang::DeclAccessPair::make(ctor, ctor->getAccess()), |
| 153 | + ctor_decl->inst_id); |
| 154 | + } else { |
| 155 | + CARBON_CHECK(ctor_id == SemIR::ErrorInst::InstId); |
| 156 | + return SemIR::ErrorInst::InstId; |
| 157 | + } |
| 158 | + return BuildWitness(context, loc_id, self_type_id, specific_interface, |
| 159 | + {ctor_id}); |
| 160 | +} |
| 161 | + |
| 162 | +auto LookupCppImpl(Context& context, SemIR::LocId loc_id, |
| 163 | + SemIR::TypeId self_type_id, |
| 164 | + SemIR::SpecificInterface specific_interface, |
| 165 | + const TypeStructure* best_impl_type_structure, |
| 166 | + SemIR::LocId best_impl_loc_id) -> SemIR::InstId { |
| 167 | + // Determine whether this is an interface that we have special knowledge of. |
| 168 | + auto& interface = context.interfaces().Get(specific_interface.interface_id); |
| 169 | + if (!context.name_scopes().IsCorePackage(interface.parent_scope_id)) { |
| 170 | + return SemIR::InstId::None; |
| 171 | + } |
| 172 | + if (!interface.name_id.AsIdentifierId().has_value()) { |
| 173 | + return SemIR::InstId::None; |
| 174 | + } |
| 175 | + |
| 176 | + if (context.identifiers().Get(interface.name_id.AsIdentifierId()) == "Copy") { |
| 177 | + return LookupCopyImpl(context, loc_id, self_type_id, specific_interface); |
| 178 | + } |
| 179 | + |
| 180 | + // TODO: Handle other interfaces. |
| 181 | + |
| 182 | + // TODO: Infer a C++ type structure and check whether it's less strict than |
| 183 | + // the best Carbon type structure. |
| 184 | + static_cast<void>(best_impl_type_structure); |
| 185 | + static_cast<void>(best_impl_loc_id); |
| 186 | + |
| 187 | + return SemIR::InstId::None; |
| 188 | +} |
| 189 | + |
| 190 | +} // namespace Carbon::Check |
0 commit comments