mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-26 02:41:11 +00:00
This is only fixing the decision about *whether* to produce a witness. Implementation of the witness is still a TODO, though where a body is generated, it should also precisely reflect where one _needs_ to be generated. Note the tests: - toolchain/lower/testdata/function/generic/import_core_witness.carbon - toolchain/lower/testdata/function/generic/import_unused_def.carbon These tests can probably be produced _without_ Core.Destroy, but I found the essence of them while trying to build //examples with Core.Destroy and a simpler minimization wasn't striking me. Assisted-by: Google Antigravity with Gemini --------- Co-authored-by: jonmeow <jperkins@google.com>
682 lines
28 KiB
C++
682 lines
28 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include "toolchain/check/custom_witness.h"
|
|
|
|
#include "toolchain/base/kind_switch.h"
|
|
#include "toolchain/check/facet_type.h"
|
|
#include "toolchain/check/function.h"
|
|
#include "toolchain/check/generic.h"
|
|
#include "toolchain/check/impl.h"
|
|
#include "toolchain/check/impl_lookup.h"
|
|
#include "toolchain/check/import_ref.h"
|
|
#include "toolchain/check/inst.h"
|
|
#include "toolchain/check/name_lookup.h"
|
|
#include "toolchain/check/type.h"
|
|
#include "toolchain/check/type_completion.h"
|
|
#include "toolchain/sem_ir/associated_constant.h"
|
|
#include "toolchain/sem_ir/builtin_function_kind.h"
|
|
#include "toolchain/sem_ir/ids.h"
|
|
#include "toolchain/sem_ir/typed_insts.h"
|
|
|
|
namespace Carbon::Check {
|
|
|
|
// Given a value whose type `IsFacetTypeOrError`, returns the corresponding
|
|
// type.
|
|
static auto GetFacetAsType(Context& context,
|
|
SemIR::ConstantId facet_or_type_const_id)
|
|
-> SemIR::TypeId {
|
|
auto facet_or_type_id =
|
|
context.constant_values().GetInstId(facet_or_type_const_id);
|
|
auto type_type_id = context.insts().Get(facet_or_type_id).type_id();
|
|
CARBON_CHECK(context.types().IsFacetTypeOrError(type_type_id));
|
|
|
|
if (context.types().Is<SemIR::FacetType>(type_type_id)) {
|
|
// It's a facet; access its type.
|
|
facet_or_type_id = context.types().GetTypeInstId(
|
|
GetFacetAccessType(context, facet_or_type_id));
|
|
}
|
|
return context.types().GetTypeIdForTypeInstId(facet_or_type_id);
|
|
}
|
|
|
|
// Returns a manufactured `Copy.Op` function with the `self` parameter typed
|
|
// to `self_type_id`.
|
|
static auto MakeCopyOpFunction(Context& context, SemIR::LocId loc_id,
|
|
SemIR::TypeId self_type_id,
|
|
SemIR::NameScopeId parent_scope_id)
|
|
-> SemIR::InstId {
|
|
auto name_id = context.core_identifiers().AddNameId(CoreIdentifier::Op);
|
|
|
|
auto [decl_id, function_id] =
|
|
MakeGeneratedFunctionDecl(context, loc_id,
|
|
{.parent_scope_id = parent_scope_id,
|
|
.name_id = name_id,
|
|
.self_type_id = self_type_id,
|
|
.self_is_ref = false,
|
|
.return_type_id = self_type_id});
|
|
|
|
auto& function = context.functions().Get(function_id);
|
|
function.SetCoreWitness(SemIR::BuiltinFunctionKind::PrimitiveCopy);
|
|
|
|
return decl_id;
|
|
}
|
|
|
|
// Returns the body for `Destroy.Op`. This will return `None` if using the
|
|
// builtin `NoOp` is appropriate.
|
|
// Returns a FacetType that contains only the query interface.
|
|
static auto GetFacetTypeForQuerySpecificInterface(
|
|
Context& context, SemIR::LocId loc_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id)
|
|
-> SemIR::ConstantId {
|
|
const auto query_specific_interface =
|
|
context.specific_interfaces().Get(query_specific_interface_id);
|
|
|
|
// The Self facet will have type FacetType, for the query interface.
|
|
auto const_id = EvalOrAddInst<SemIR::FacetType>(
|
|
context, loc_id,
|
|
FacetTypeFromInterface(context, query_specific_interface.interface_id,
|
|
query_specific_interface.specific_id));
|
|
return const_id;
|
|
}
|
|
|
|
// Starts a block for lookup-related instructions, and returns the `FacetType`
|
|
// for lookups in `HasWitnessForRepeatedField`.
|
|
static auto PrepareForHasWitness(
|
|
Context& context, SemIR::LocId loc_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id)
|
|
-> SemIR::ConstantId {
|
|
context.inst_block_stack().Push();
|
|
StartGenericDecl(context);
|
|
|
|
return GetFacetTypeForQuerySpecificInterface(context, loc_id,
|
|
query_specific_interface_id);
|
|
}
|
|
|
|
// Cleans up state `PrepareForHasWitness`.
|
|
static auto CleanupAfterHasWitness(Context& context) -> void {
|
|
DiscardGenericDecl(context);
|
|
context.inst_block_stack().PopAndDiscard();
|
|
}
|
|
|
|
// Returns true if `type_inst_id` has a witness for the query interface, which
|
|
// comes from `PrepareForHasWitness`.
|
|
static auto HasWitnessForRepeatedField(
|
|
Context& context, SemIR::LocId loc_id, SemIR::InstId type_inst_id,
|
|
SemIR::ConstantId query_facet_type_const_id) -> bool {
|
|
auto type_const_id = context.constant_values().Get(type_inst_id);
|
|
auto block_or_err = LookupImplWitness(context, loc_id, type_const_id,
|
|
query_facet_type_const_id);
|
|
return block_or_err.has_value();
|
|
}
|
|
|
|
// The format for `Destroy.Op`.
|
|
enum class DestroyFormat {
|
|
NoDestroy,
|
|
Trivial,
|
|
NonTrivial,
|
|
};
|
|
|
|
// Similar to `HasWitnessForRepeatedField`, but for cases where there's only one
|
|
// field, this can handle the call to `PrepareForHasWitness`.
|
|
static auto HasWitnessForOneField(
|
|
Context& context, SemIR::LocId loc_id, SemIR::InstId field_inst_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id) -> DestroyFormat {
|
|
auto query_facet_type_const_id =
|
|
PrepareForHasWitness(context, loc_id, query_specific_interface_id);
|
|
auto has_witness = HasWitnessForRepeatedField(context, loc_id, field_inst_id,
|
|
query_facet_type_const_id);
|
|
CleanupAfterHasWitness(context);
|
|
return has_witness ? DestroyFormat::NonTrivial : DestroyFormat::NoDestroy;
|
|
}
|
|
|
|
// Returns true if `class_type` should impl `Destroy`.
|
|
static auto CanDestroyClass(
|
|
Context& context, SemIR::LocId loc_id, SemIR::ClassType class_type,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id, bool is_partial)
|
|
-> DestroyFormat {
|
|
auto class_info = context.classes().Get(class_type.class_id);
|
|
// Incomplete and abstract classes can't be destroyed.
|
|
if (!class_info.is_complete() ||
|
|
(!is_partial && class_info.inheritance_kind ==
|
|
SemIR::Class::InheritanceKind::Abstract)) {
|
|
return DestroyFormat::NoDestroy;
|
|
}
|
|
|
|
// `LookupCppImpl` handles C++ types.
|
|
if (context.name_scopes().Get(class_info.scope_id).is_cpp_scope()) {
|
|
return DestroyFormat::NoDestroy;
|
|
}
|
|
|
|
auto object_repr_id =
|
|
class_info.GetObjectRepr(context.sem_ir(), class_type.specific_id);
|
|
return HasWitnessForOneField(context, loc_id,
|
|
context.types().GetTypeInstId(object_repr_id),
|
|
query_specific_interface_id);
|
|
}
|
|
|
|
// Returns true if the `Self` should impl `Destroy`. This will recurse into impl
|
|
// lookup of `Destroy` for members, similar to `where .Self.members each impls
|
|
// Destroy`.
|
|
static auto CanDestroyType(
|
|
Context& context, SemIR::LocId loc_id,
|
|
SemIR::ConstantId query_self_const_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id) -> DestroyFormat {
|
|
auto query_specific_interface =
|
|
context.specific_interfaces().Get(query_specific_interface_id);
|
|
auto destroy_interface_id = query_specific_interface.interface_id;
|
|
|
|
auto inst = context.insts().Get(context.constant_values().GetInstId(
|
|
GetCanonicalFacetOrTypeValue(context, query_self_const_id)));
|
|
|
|
CARBON_KIND_SWITCH(inst) {
|
|
case SemIR::ImplWitnessAccess::Kind:
|
|
case SemIR::SymbolicBinding::Kind: {
|
|
// These are symbolic, so should never reach `MakeDestroyOpBody`.
|
|
// For facet values, look if the FacetType provides the same.
|
|
if (auto facet_type =
|
|
context.types().TryGetAs<SemIR::FacetType>(inst.type_id())) {
|
|
const auto& info = context.facet_types().Get(facet_type->facet_type_id);
|
|
for (auto interface : info.extend_constraints) {
|
|
if (interface.interface_id == destroy_interface_id) {
|
|
return DestroyFormat::Trivial;
|
|
}
|
|
}
|
|
}
|
|
return DestroyFormat::NoDestroy;
|
|
}
|
|
|
|
case CARBON_KIND(SemIR::ArrayType array_type): {
|
|
// A zero element array is always trivially destructible.
|
|
if (auto int_bound =
|
|
context.sem_ir().GetArrayBoundValue(array_type.bound_id);
|
|
!int_bound || *int_bound == 0) {
|
|
return DestroyFormat::Trivial;
|
|
}
|
|
|
|
// Verify the element can be destroyed.
|
|
return HasWitnessForOneField(context, loc_id,
|
|
array_type.element_type_inst_id,
|
|
query_specific_interface_id);
|
|
}
|
|
|
|
case SemIR::Call::Kind:
|
|
// TODO: These seem like they shouldn't be getting directly queried for
|
|
// destroy. The use is in a test that was TODO before this TODO.
|
|
return DestroyFormat::NoDestroy;
|
|
|
|
case CARBON_KIND(SemIR::ClassType class_type): {
|
|
return CanDestroyClass(context, loc_id, class_type,
|
|
query_specific_interface_id,
|
|
/*is_partial=*/false);
|
|
}
|
|
|
|
case CARBON_KIND(SemIR::ConstType const_type): {
|
|
return HasWitnessForOneField(context, loc_id, const_type.inner_id,
|
|
query_specific_interface_id);
|
|
}
|
|
|
|
case CARBON_KIND(SemIR::MaybeUnformedType maybe_unformed_type): {
|
|
return HasWitnessForOneField(context, loc_id,
|
|
maybe_unformed_type.inner_id,
|
|
query_specific_interface_id);
|
|
}
|
|
|
|
case CARBON_KIND(SemIR::PartialType partial_type): {
|
|
// In contrast with something like `const`, need to treat the inner
|
|
// class differently based on the `partial` modifier.
|
|
auto class_type =
|
|
context.insts().GetAs<SemIR::ClassType>(partial_type.inner_id);
|
|
return CanDestroyClass(context, loc_id, class_type,
|
|
query_specific_interface_id,
|
|
/*is_partial=*/true);
|
|
}
|
|
|
|
case CARBON_KIND(SemIR::StructType struct_type): {
|
|
auto fields = context.struct_type_fields().Get(struct_type.fields_id);
|
|
if (fields.empty()) {
|
|
return DestroyFormat::Trivial;
|
|
}
|
|
auto query_facet_type_const_id =
|
|
PrepareForHasWitness(context, loc_id, query_specific_interface_id);
|
|
bool has_witness = true;
|
|
for (const auto& field : fields) {
|
|
if (!HasWitnessForRepeatedField(context, loc_id, field.type_inst_id,
|
|
query_facet_type_const_id)) {
|
|
has_witness = false;
|
|
break;
|
|
}
|
|
}
|
|
CleanupAfterHasWitness(context);
|
|
return has_witness ? DestroyFormat::NonTrivial : DestroyFormat::NoDestroy;
|
|
}
|
|
|
|
case CARBON_KIND(SemIR::SymbolicBindingType sym_binding): {
|
|
return HasWitnessForOneField(context, loc_id,
|
|
sym_binding.facet_value_inst_id,
|
|
query_specific_interface_id);
|
|
}
|
|
|
|
case CARBON_KIND(SemIR::TupleType tuple_type): {
|
|
auto block = context.inst_blocks().Get(tuple_type.type_elements_id);
|
|
if (block.empty()) {
|
|
return DestroyFormat::Trivial;
|
|
}
|
|
auto query_facet_type_const_id =
|
|
PrepareForHasWitness(context, loc_id, query_specific_interface_id);
|
|
bool has_witness = true;
|
|
for (const auto& element_id : block) {
|
|
if (!HasWitnessForRepeatedField(context, loc_id, element_id,
|
|
query_facet_type_const_id)) {
|
|
has_witness = false;
|
|
break;
|
|
}
|
|
}
|
|
CleanupAfterHasWitness(context);
|
|
return has_witness ? DestroyFormat::NonTrivial : DestroyFormat::NoDestroy;
|
|
}
|
|
|
|
case SemIR::BoolType::Kind:
|
|
case SemIR::FloatType::Kind:
|
|
case SemIR::IntType::Kind:
|
|
case SemIR::PointerType::Kind:
|
|
case SemIR::TypeType::Kind:
|
|
// Trivially destructible.
|
|
return DestroyFormat::Trivial;
|
|
|
|
default:
|
|
CARBON_FATAL("Unexpected type for CanDestroyType: {0}", inst.kind());
|
|
}
|
|
}
|
|
|
|
// Returns the body for `Destroy.Op`.
|
|
//
|
|
// TODO: This is a placeholder still not actually destroying things, intended to
|
|
// maintain mostly-consistent behavior with current logic while working. That
|
|
// also means using `self`.
|
|
static auto MakeDestroyOpBody(Context& context, SemIR::LocId loc_id,
|
|
SemIR::TypeId self_type_id,
|
|
SemIR::InstId self_param_id)
|
|
-> SemIR::InstBlockId {
|
|
context.inst_block_stack().Push();
|
|
auto inst = context.types().GetAsInst(self_type_id);
|
|
|
|
CARBON_KIND_SWITCH(inst) {
|
|
case SemIR::ArrayType::Kind:
|
|
case SemIR::ClassType::Kind:
|
|
case SemIR::ConstType::Kind:
|
|
case SemIR::MaybeUnformedType::Kind:
|
|
case SemIR::PartialType::Kind:
|
|
case SemIR::StructType::Kind:
|
|
case SemIR::TupleType::Kind:
|
|
(void)self_param_id;
|
|
// TODO: Implement destruction of the type.
|
|
break;
|
|
default:
|
|
CARBON_FATAL("Unexpected type for MakeDestroyOpBody: {0}", inst);
|
|
}
|
|
|
|
AddInst(context, loc_id, SemIR::Return{});
|
|
return context.inst_block_stack().Pop();
|
|
}
|
|
|
|
// Returns a manufactured `Destroy.Op` function with the `self` parameter typed
|
|
// to `self_type_id`.
|
|
static auto MakeDestroyOpFunction(Context& context, SemIR::LocId loc_id,
|
|
SemIR::TypeId self_type_id,
|
|
SemIR::NameScopeId parent_scope_id,
|
|
DestroyFormat format) -> SemIR::InstId {
|
|
auto name_id = context.core_identifiers().AddNameId(CoreIdentifier::Op);
|
|
|
|
auto [decl_id, function_id] =
|
|
MakeGeneratedFunctionDecl(context, loc_id,
|
|
{.parent_scope_id = parent_scope_id,
|
|
.name_id = name_id,
|
|
.self_type_id = self_type_id});
|
|
|
|
auto& function = context.functions().Get(function_id);
|
|
|
|
if (format == DestroyFormat::Trivial) {
|
|
function.SetCoreWitness(SemIR::BuiltinFunctionKind::NoOp);
|
|
} else {
|
|
CARBON_CHECK(format == DestroyFormat::NonTrivial);
|
|
function.SetCoreWitness(SemIR::BuiltinFunctionKind::None);
|
|
auto body_id = MakeDestroyOpBody(context, loc_id, self_type_id,
|
|
function.self_param_id);
|
|
function.body_block_ids.push_back(body_id);
|
|
}
|
|
|
|
return decl_id;
|
|
}
|
|
|
|
static auto MakeCustomWitnessConstantInst(
|
|
Context& context, SemIR::LocId loc_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id,
|
|
SemIR::InstBlockId associated_entities_block_id) -> SemIR::InstId {
|
|
// The witness is a CustomWitness of the query interface with a table that
|
|
// contains each associated entity.
|
|
auto const_id = EvalOrAddInst<SemIR::CustomWitness>(
|
|
context, loc_id,
|
|
{.type_id = GetSingletonType(context, SemIR::WitnessType::TypeInstId),
|
|
.elements_id = associated_entities_block_id,
|
|
.query_specific_interface_id = query_specific_interface_id});
|
|
return context.constant_values().GetInstId(const_id);
|
|
}
|
|
|
|
struct TypesForSelfFacet {
|
|
// A FacetType that contains only the query interface.
|
|
SemIR::TypeId facet_type_for_query_specific_interface;
|
|
// The query self as a type, which involves a conversion if it was a facet.
|
|
SemIR::TypeId query_self_as_type_id;
|
|
};
|
|
|
|
static auto GetTypesForSelfFacet(
|
|
Context& context, SemIR::LocId loc_id,
|
|
SemIR::ConstantId query_self_const_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id)
|
|
-> TypesForSelfFacet {
|
|
// The Self facet will have type FacetType, for the query interface.
|
|
auto facet_type_for_query_specific_interface =
|
|
context.types().GetTypeIdForTypeConstantId(
|
|
GetFacetTypeForQuerySpecificInterface(context, loc_id,
|
|
query_specific_interface_id));
|
|
// The Self facet needs to point to a type value. If it's not one already,
|
|
// convert to type.
|
|
auto query_self_as_type_id = GetFacetAsType(context, query_self_const_id);
|
|
return {facet_type_for_query_specific_interface, query_self_as_type_id};
|
|
}
|
|
|
|
// Build a new facet from the query self, using a CustomWitness for the query
|
|
// interface with an entry for each associated entity so far.
|
|
static auto MakeSelfFacetWithCustomWitness(
|
|
Context& context, SemIR::LocId loc_id, TypesForSelfFacet query_types,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id,
|
|
SemIR::InstBlockId associated_entities_block_id) -> SemIR::ConstantId {
|
|
// We are building a facet value for a single interface, so the witness block
|
|
// is a single witness for that interface.
|
|
auto witnesses_block_id =
|
|
context.inst_blocks().Add({MakeCustomWitnessConstantInst(
|
|
context, loc_id, query_specific_interface_id,
|
|
associated_entities_block_id)});
|
|
|
|
return EvalOrAddInst<SemIR::FacetValue>(
|
|
context, loc_id,
|
|
{.type_id = query_types.facet_type_for_query_specific_interface,
|
|
.type_inst_id =
|
|
context.types().GetTypeInstId(query_types.query_self_as_type_id),
|
|
.witnesses_block_id = witnesses_block_id});
|
|
}
|
|
|
|
auto BuildCustomWitness(Context& context, SemIR::LocId loc_id,
|
|
SemIR::ConstantId query_self_const_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id,
|
|
llvm::ArrayRef<SemIR::InstId> values) -> SemIR::InstId {
|
|
const auto query_specific_interface =
|
|
context.specific_interfaces().Get(query_specific_interface_id);
|
|
const auto& interface =
|
|
context.interfaces().Get(query_specific_interface.interface_id);
|
|
auto assoc_entities =
|
|
context.inst_blocks().GetOrEmpty(interface.associated_entities_id);
|
|
if (assoc_entities.size() != values.size()) {
|
|
context.TODO(loc_id, ("Unsupported definition of interface " +
|
|
context.names().GetFormatted(interface.name_id))
|
|
.str());
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
|
|
auto query_types_for_self_facet = GetTypesForSelfFacet(
|
|
context, loc_id, query_self_const_id, query_specific_interface_id);
|
|
|
|
// The values that will go in the witness table.
|
|
llvm::SmallVector<SemIR::InstId> entries;
|
|
|
|
// Fill in the witness table.
|
|
for (const auto& [assoc_entity_id, value_id] :
|
|
llvm::zip_equal(assoc_entities, values)) {
|
|
LoadImportRef(context, assoc_entity_id);
|
|
|
|
// Build a witness with the current contents of the witness table. This will
|
|
// grow as we progress through the impl. In theory this will build O(n^2)
|
|
// table entries, but in practice n <= 2, so that's OK.
|
|
//
|
|
// This is necessary because later associated entities may refer to earlier
|
|
// associated entities in their signatures. In particular, an associated
|
|
// result type may be used as the return type of an associated function.
|
|
auto self_facet = MakeSelfFacetWithCustomWitness(
|
|
context, loc_id, query_types_for_self_facet,
|
|
query_specific_interface_id, context.inst_blocks().Add(entries));
|
|
auto interface_with_self_specific_id = MakeSpecificWithInnerSelf(
|
|
context, loc_id, interface.generic_id, interface.generic_with_self_id,
|
|
query_specific_interface.specific_id, self_facet);
|
|
CARBON_CHECK(
|
|
!context.specifics().Get(interface_with_self_specific_id).HasError());
|
|
|
|
auto decl_id =
|
|
context.constant_values().GetInstId(SemIR::GetConstantValueInSpecific(
|
|
context.sem_ir(), interface_with_self_specific_id,
|
|
assoc_entity_id));
|
|
CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
|
|
auto decl = context.insts().Get(decl_id);
|
|
CARBON_KIND_SWITCH(decl) {
|
|
case CARBON_KIND(SemIR::StructValue struct_value): {
|
|
if (struct_value.type_id == SemIR::ErrorInst::TypeId) {
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
// TODO: If a thunk is needed, this will build a different value each
|
|
// time it's called, so we won't properly deduplicate repeated
|
|
// witnesses.
|
|
entries.push_back(CheckAssociatedFunctionImplementation(
|
|
context,
|
|
context.types().GetAs<SemIR::FunctionType>(struct_value.type_id),
|
|
query_specific_interface.specific_id, value_id,
|
|
/*defer_thunk_definition=*/false));
|
|
break;
|
|
}
|
|
case CARBON_KIND(SemIR::AssociatedConstantDecl decl): {
|
|
if (decl.type_id == SemIR::ErrorInst::TypeId) {
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
|
|
// TODO: remove once we have a test-case for all associated constants.
|
|
// Special-case the ones we want to support in this if-statement, until
|
|
// we're able to account for everything.
|
|
if (decl.type_id != SemIR::TypeType::TypeId) {
|
|
context.TODO(loc_id,
|
|
"Associated constant of type other than `TypeType` in "
|
|
"synthesized impl");
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
|
|
auto type_id = context.insts().Get(value_id).type_id();
|
|
CARBON_CHECK(type_id == SemIR::TypeType::TypeId ||
|
|
type_id == SemIR::ErrorInst::TypeId);
|
|
auto impl_witness_associated_constant =
|
|
AddInst<SemIR::ImplWitnessAssociatedConstant>(
|
|
context, loc_id, {.type_id = type_id, .inst_id = value_id});
|
|
entries.push_back(impl_witness_associated_constant);
|
|
break;
|
|
}
|
|
default:
|
|
CARBON_CHECK(decl_id == SemIR::ErrorInst::InstId,
|
|
"Unexpected kind of associated entity {0}", decl);
|
|
return SemIR::ErrorInst::InstId;
|
|
}
|
|
}
|
|
|
|
// TODO: Consider building one witness after all associated constants, and
|
|
// then a second after all associated functions, rather than building one in
|
|
// each `StructValue`. Right now the code is written assuming at most one
|
|
// function, though this CHECK can be removed as a temporary workaround.
|
|
auto associated_functions = llvm::count_if(entries, [&](SemIR::InstId id) {
|
|
return context.insts().Get(id).kind() == SemIR::InstKind::FunctionDecl;
|
|
});
|
|
CARBON_CHECK(associated_functions <= 1,
|
|
"TODO: Support multiple associated functions");
|
|
|
|
return MakeCustomWitnessConstantInst(context, loc_id,
|
|
query_specific_interface_id,
|
|
context.inst_blocks().Add(entries));
|
|
}
|
|
|
|
auto GetCoreInterface(Context& context, SemIR::InterfaceId interface_id)
|
|
-> CoreInterface {
|
|
const auto& interface = context.interfaces().Get(interface_id);
|
|
if (!context.name_scopes().IsCorePackage(interface.parent_scope_id) ||
|
|
!interface.name_id.AsIdentifierId().has_value()) {
|
|
return CoreInterface::Unknown;
|
|
}
|
|
|
|
constexpr auto CoreIdentifiersToInterfaces = std::array{
|
|
std::pair{CoreIdentifier::Copy, CoreInterface::Copy},
|
|
std::pair{CoreIdentifier::CppUnsafeDeref, CoreInterface::CppUnsafeDeref},
|
|
std::pair{CoreIdentifier::Default, CoreInterface::Default},
|
|
std::pair{CoreIdentifier::Destroy, CoreInterface::Destroy},
|
|
std::pair{CoreIdentifier::IntFitsIn, CoreInterface::IntFitsIn}};
|
|
|
|
for (auto [core_identifier, core_interface] : CoreIdentifiersToInterfaces) {
|
|
if (interface.name_id ==
|
|
context.core_identifiers().AddNameId(core_identifier)) {
|
|
return core_interface;
|
|
}
|
|
}
|
|
return CoreInterface::Unknown;
|
|
}
|
|
|
|
auto BuildPrimitiveCopyWitness(
|
|
Context& context, SemIR::LocId loc_id, SemIR::NameScopeId parent_scope_id,
|
|
SemIR::ConstantId query_self_const_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id) -> SemIR::InstId {
|
|
auto self_type_id = GetFacetAsType(context, query_self_const_id);
|
|
auto op_id =
|
|
MakeCopyOpFunction(context, loc_id, self_type_id, parent_scope_id);
|
|
return BuildCustomWitness(context, loc_id, query_self_const_id,
|
|
query_specific_interface_id, {op_id});
|
|
}
|
|
static auto MakeDestroyWitness(
|
|
Context& context, SemIR::LocId loc_id,
|
|
SemIR::ConstantId query_self_const_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id, bool build_witness)
|
|
-> std::optional<SemIR::InstId> {
|
|
auto format = CanDestroyType(context, loc_id, query_self_const_id,
|
|
query_specific_interface_id);
|
|
if (format == DestroyFormat::NoDestroy) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (!build_witness || query_self_const_id.is_symbolic()) {
|
|
// The type can be destroyed, but we shouldn't make a witness right now.
|
|
return SemIR::InstId::None;
|
|
}
|
|
|
|
// Mark functions with the interface's scope as a hint to mangling. This
|
|
// does not add them to the scope.
|
|
auto query_specific_interface =
|
|
context.specific_interfaces().Get(query_specific_interface_id);
|
|
auto parent_scope_id = context.interfaces()
|
|
.Get(query_specific_interface.interface_id)
|
|
.scope_without_self_id;
|
|
|
|
auto self_type_id = GetFacetAsType(context, query_self_const_id);
|
|
auto op_id = MakeDestroyOpFunction(context, loc_id, self_type_id,
|
|
parent_scope_id, format);
|
|
return BuildCustomWitness(context, loc_id, query_self_const_id,
|
|
query_specific_interface_id, {op_id});
|
|
}
|
|
|
|
static auto MakeIntFitsInWitness(
|
|
Context& context, SemIR::LocId loc_id,
|
|
SemIR::ConstantId query_self_const_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id, bool build_witness)
|
|
-> std::optional<SemIR::InstId> {
|
|
auto query_specific_interface =
|
|
context.specific_interfaces().Get(query_specific_interface_id);
|
|
|
|
auto args_id = query_specific_interface.specific_id;
|
|
if (!args_id.has_value()) {
|
|
return std::nullopt;
|
|
}
|
|
auto args_block_id = context.specifics().Get(args_id).args_id;
|
|
auto args_block = context.inst_blocks().Get(args_block_id);
|
|
if (args_block.size() != 1) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
auto dest_const_id = context.constant_values().Get(args_block[0]);
|
|
if (!dest_const_id.is_constant()) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
auto src_type_id = GetFacetAsType(context, query_self_const_id);
|
|
auto dest_type_id = GetFacetAsType(context, dest_const_id);
|
|
|
|
auto context_fn = [](DiagnosticContextBuilder& /*builder*/) -> void {};
|
|
if (!RequireCompleteType(context, src_type_id, loc_id, context_fn) ||
|
|
!RequireCompleteType(context, dest_type_id, loc_id, context_fn)) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
auto src_info = context.types().TryGetIntTypeInfo(src_type_id);
|
|
auto dest_info = context.types().TryGetIntTypeInfo(dest_type_id);
|
|
|
|
if (!src_info || !dest_info) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
// If the bit width is unknown (e.g., due to symbolic evaluation), we cannot
|
|
// determine whether it fits yet.
|
|
if (src_info->bit_width == IntId::None ||
|
|
dest_info->bit_width == IntId::None) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
const auto& src_width = context.ints().Get(src_info->bit_width);
|
|
const auto& dest_width = context.ints().Get(dest_info->bit_width);
|
|
|
|
bool fits = false;
|
|
if (src_info->is_signed && !dest_info->is_signed) {
|
|
// Signed -> unsigned: would truncate the sign bit.
|
|
fits = false;
|
|
} else if (src_info->is_signed == dest_info->is_signed) {
|
|
// Signed -> signed or unsigned -> unsigned: allow widening or preserving
|
|
// width.
|
|
fits = src_width.sle(dest_width);
|
|
} else {
|
|
// Unsigned -> signed: strict widening required.
|
|
fits = src_width.slt(dest_width);
|
|
}
|
|
|
|
if (!fits) {
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (!build_witness) {
|
|
return SemIR::InstId::None;
|
|
}
|
|
|
|
return BuildCustomWitness(context, loc_id, query_self_const_id,
|
|
query_specific_interface_id, {});
|
|
}
|
|
|
|
auto LookupCustomWitness(Context& context, SemIR::LocId loc_id,
|
|
CoreInterface core_interface,
|
|
SemIR::ConstantId query_self_const_id,
|
|
SemIR::SpecificInterfaceId query_specific_interface_id,
|
|
bool build_witness) -> std::optional<SemIR::InstId> {
|
|
switch (core_interface) {
|
|
case CoreInterface::Destroy:
|
|
return MakeDestroyWitness(context, loc_id, query_self_const_id,
|
|
query_specific_interface_id, build_witness);
|
|
case CoreInterface::IntFitsIn:
|
|
return MakeIntFitsInWitness(context, loc_id, query_self_const_id,
|
|
query_specific_interface_id, build_witness);
|
|
case CoreInterface::Copy:
|
|
case CoreInterface::CppUnsafeDeref:
|
|
case CoreInterface::Default:
|
|
case CoreInterface::Unknown:
|
|
// TODO: Handle more interfaces, particularly copy, move, and conversion.
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon::Check
|