Files
carbon-lang/toolchain/sem_ir/impl.cpp
T
Dana Jansens 32aa7cb1fa Make identifying a facet type an operation on a (self+facet type) pair (#6592)
Identifying a facet type takes both a self and facet type as a pair, and
then encode the self into the IdentifiedFacetType. This makes a
constraint that requires some _other_ type implements an interface
visible in the IdentifiedFacetType. And it will help to enable facet
types with `where T impls Z` for `T` that is not `.Self` in the future.

IdentifiedFacetTypes are now stored in a CanonicalValueStore instead of
a RelationalValueStore as they key is the combination of self and
(declared) facet type together now.

When the self-type is a facet value (has type FacetType) this is most
straightforward. But when it's a type we need to construct a FacetValue
to construct a specific for a require decl, to replace the generic
binding of the symbolic `Self`, which has type FacetType. To do so, we
make a FacetValue with an empty FacetType (equivalent to TypeType). This
prevents any looking for witnesses through the FacetType, which matches
what you can get from a type directly, requiring witnesses to come from
finding an `impl` decl.

Add additional InstNamer logic for such empty facet types so they print
as `<typename>.type.facet` if possible instead of as just `facet_value`.
2026-01-14 17:34:56 +00:00

43 lines
1.6 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/sem_ir/impl.h"
#include "toolchain/base/kind_switch.h"
#include "toolchain/sem_ir/facet_type_info.h"
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/specific_interface.h"
#include "toolchain/sem_ir/specific_named_constraint.h"
namespace Carbon::SemIR {
ImplStore::ImplStore(File& sem_ir)
: sem_ir_(sem_ir), values_(sem_ir.check_ir_id()) {}
auto ImplStore::GetOrAddLookupBucket(const Impl& impl) -> LookupBucketRef {
auto self_const_id = sem_ir_.constant_values().Get(impl.self_id);
auto impl_as_interface = SpecificInterface::None;
auto facet_type_type_id = TypeId::ForTypeConstant(
sem_ir_.constant_values().Get(impl.constraint_id));
if (auto facet_type =
sem_ir_.types().TryGetAs<FacetType>(facet_type_type_id)) {
auto identified_id = sem_ir_.identified_facet_types().Lookup(
{facet_type->facet_type_id, self_const_id});
if (identified_id.has_value()) {
const auto& identified =
sem_ir_.identified_facet_types().Get(identified_id);
if (identified.is_valid_impl_as_target()) {
impl_as_interface = identified.impl_as_target_interface();
}
}
}
return LookupBucketRef(
*this, lookup_
.Insert(std::pair{self_const_id, impl_as_interface},
[] { return ImplOrLookupBucketId::None; })
.value());
}
} // namespace Carbon::SemIR