mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Support accessing associated functions by member access into facets (#4872)
For an expression such as `(Type as Interface).AssocFn()`, track the `Self` type `Type` in the result of the member access so that it's available when checking the function call. This introduces a new kind of type, `ImplFunctionType`, that represents the type of a function that is expected within an impl, modeled as the type of the function within the interface plus a value to use as `Self`. Calls to values of this type behave like calls to the underlying function except that the `Self` parameter is pre-bound to the self type from the facet. In order to support this, fix an issue where the imported list of generic bindings lost their association with their enclosing generic. This adds a little complexity to `import_ref`, including a new recursive cycle that I intend to address in a follow-up PR. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
dcfccd3187
commit
fcfb1345d5
+107
-42
@@ -40,55 +40,106 @@ auto BuildAssociatedEntity(Context& context, SemIR::InterfaceId interface_id,
|
||||
{.type_id = type_id, .index = index, .decl_id = decl_id});
|
||||
}
|
||||
|
||||
auto GetSelfSpecificForInterfaceMemberWithSelfType(
|
||||
Context& context, SemIRLoc loc, SemIR::SpecificId enclosing_specific_id,
|
||||
SemIR::GenericId generic_id, SemIR::TypeId self_type_id,
|
||||
SemIR::InstId witness_inst_id) -> SemIR::SpecificId {
|
||||
const auto& generic = context.generics().Get(generic_id);
|
||||
// Returns the `Self` binding for an interface, given a specific for the
|
||||
// interface and a generic for an associated entity within it.
|
||||
static auto GetSelfBinding(Context& context,
|
||||
SemIR::SpecificId interface_specific_id,
|
||||
SemIR::GenericId assoc_entity_generic_id)
|
||||
-> SemIR::InstId {
|
||||
const auto& generic = context.generics().Get(assoc_entity_generic_id);
|
||||
auto bindings = context.inst_blocks().Get(generic.bindings_id);
|
||||
auto interface_args_id =
|
||||
context.specifics().GetArgsOrEmpty(interface_specific_id);
|
||||
auto interface_args = context.inst_blocks().Get(interface_args_id);
|
||||
|
||||
llvm::SmallVector<SemIR::InstId> arg_ids;
|
||||
arg_ids.reserve(bindings.size());
|
||||
// The `Self` binding is the first binding after the interface's arguments.
|
||||
auto self_binding_id = bindings[interface_args.size()];
|
||||
|
||||
// Start with the enclosing arguments.
|
||||
if (enclosing_specific_id.has_value()) {
|
||||
auto enclosing_specific_args_id =
|
||||
context.specifics().Get(enclosing_specific_id).args_id;
|
||||
auto enclosing_specific_args =
|
||||
context.inst_blocks().Get(enclosing_specific_args_id);
|
||||
arg_ids.assign(enclosing_specific_args.begin(),
|
||||
enclosing_specific_args.end());
|
||||
}
|
||||
|
||||
// Add the `Self` argument. First find the `Self` binding.
|
||||
auto self_binding =
|
||||
context.insts().GetAs<SemIR::BindSymbolicName>(bindings[arg_ids.size()]);
|
||||
// Check that we found the self binding. The binding might be a
|
||||
// `BindSymbolicName` or an `ImportRef` naming one.
|
||||
auto self_binding_const_inst_id =
|
||||
context.constant_values().GetConstantInstId(self_binding_id);
|
||||
auto bind_name_inst = context.insts().GetAs<SemIR::BindSymbolicName>(
|
||||
self_binding_const_inst_id);
|
||||
CARBON_CHECK(
|
||||
context.entity_names().Get(self_binding.entity_name_id).name_id ==
|
||||
context.entity_names().Get(bind_name_inst.entity_name_id).name_id ==
|
||||
SemIR::NameId::SelfType,
|
||||
"Expected a Self binding, found {0}", self_binding);
|
||||
"Expected a Self binding, found {0}", bind_name_inst);
|
||||
|
||||
return self_binding_id;
|
||||
}
|
||||
|
||||
// Given a `Self` type and a witness that it implements an interface, along with
|
||||
// that interface's `Self` binding, forms and returns a facet that can be used
|
||||
// as the argument for that `Self` binding.
|
||||
static auto GetSelfFacet(Context& context,
|
||||
SemIR::SpecificId interface_specific_id,
|
||||
SemIR::GenericId generic_id,
|
||||
SemIR::TypeId self_type_id,
|
||||
SemIR::InstId self_witness_id) -> SemIR::InstId {
|
||||
auto self_binding_id =
|
||||
GetSelfBinding(context, interface_specific_id, generic_id);
|
||||
auto self_binding = context.insts().Get(self_binding_id);
|
||||
auto self_facet_type_id = SemIR::GetTypeInSpecific(
|
||||
context.sem_ir(), interface_specific_id, self_binding.type_id());
|
||||
// Create a facet value to be the value of `Self` in the interface.
|
||||
// TODO: Pass this in instead of creating it here. The caller sometimes
|
||||
// already has a facet value.
|
||||
auto type_inst_id = context.types().GetInstId(self_type_id);
|
||||
auto facet_value_const_id =
|
||||
auto self_value_const_id =
|
||||
TryEvalInst(context, SemIR::InstId::None,
|
||||
SemIR::FacetValue{.type_id = self_binding.type_id,
|
||||
SemIR::FacetValue{.type_id = self_facet_type_id,
|
||||
.type_inst_id = type_inst_id,
|
||||
.witness_inst_id = witness_inst_id});
|
||||
arg_ids.push_back(context.constant_values().GetInstId(facet_value_const_id));
|
||||
.witness_inst_id = self_witness_id});
|
||||
return context.constant_values().GetInstId(self_value_const_id);
|
||||
}
|
||||
|
||||
// Builds and returns the argument list from `interface_specific_id` with a
|
||||
// value for the `Self` parameter of `generic_id` appended.
|
||||
static auto GetGenericArgsWithSelfType(Context& context,
|
||||
SemIR::SpecificId interface_specific_id,
|
||||
SemIR::GenericId generic_id,
|
||||
SemIR::TypeId self_type_id,
|
||||
SemIR::InstId witness_inst_id,
|
||||
std::size_t reserve_args_size = 0)
|
||||
-> llvm::SmallVector<SemIR::InstId> {
|
||||
auto interface_args_id =
|
||||
context.specifics().GetArgsOrEmpty(interface_specific_id);
|
||||
auto interface_args = context.inst_blocks().Get(interface_args_id);
|
||||
|
||||
llvm::SmallVector<SemIR::InstId> arg_ids;
|
||||
arg_ids.reserve(std::max(reserve_args_size, interface_args.size() + 1));
|
||||
|
||||
// Start with the enclosing arguments from the interface.
|
||||
arg_ids.assign(interface_args.begin(), interface_args.end());
|
||||
|
||||
// Add the `Self` argument.
|
||||
arg_ids.push_back(GetSelfFacet(context, interface_specific_id, generic_id,
|
||||
self_type_id, witness_inst_id));
|
||||
|
||||
return arg_ids;
|
||||
}
|
||||
|
||||
auto GetSelfSpecificForInterfaceMemberWithSelfType(
|
||||
Context& context, SemIRLoc loc, SemIR::SpecificId interface_specific_id,
|
||||
SemIR::GenericId generic_id, SemIR::TypeId self_type_id,
|
||||
SemIR::InstId witness_inst_id) -> SemIR::SpecificId {
|
||||
const auto& generic = context.generics().Get(generic_id);
|
||||
auto self_specific_args = context.inst_blocks().Get(
|
||||
context.specifics().Get(generic.self_specific_id).args_id);
|
||||
|
||||
auto arg_ids = GetGenericArgsWithSelfType(
|
||||
context, interface_specific_id, generic_id, self_type_id, witness_inst_id,
|
||||
self_specific_args.size());
|
||||
|
||||
// Take any trailing argument values from the self specific.
|
||||
// TODO: If these refer to outer arguments, for example in their types, we may
|
||||
// need to perform extra substitutions here.
|
||||
auto self_specific_args = context.inst_blocks().Get(
|
||||
context.specifics().Get(generic.self_specific_id).args_id);
|
||||
for (auto arg_id : self_specific_args.drop_front(arg_ids.size())) {
|
||||
arg_ids.push_back(context.constant_values().GetConstantInstId(arg_id));
|
||||
}
|
||||
|
||||
auto args_id = context.inst_blocks().AddCanonical(arg_ids);
|
||||
return MakeSpecific(context, loc, generic_id, args_id);
|
||||
return MakeSpecific(context, loc, generic_id, arg_ids);
|
||||
}
|
||||
|
||||
auto GetTypeForSpecificAssociatedEntity(Context& context, SemIRLoc loc,
|
||||
@@ -99,20 +150,34 @@ auto GetTypeForSpecificAssociatedEntity(Context& context, SemIRLoc loc,
|
||||
-> SemIR::TypeId {
|
||||
auto decl =
|
||||
context.insts().Get(context.constant_values().GetConstantInstId(decl_id));
|
||||
|
||||
auto specific_id = interface_specific_id;
|
||||
if (auto assoc_const = decl.TryAs<SemIR::AssociatedConstantDecl>()) {
|
||||
specific_id = GetSelfSpecificForInterfaceMemberWithSelfType(
|
||||
context, loc, interface_specific_id,
|
||||
context.associated_constants()
|
||||
.Get(assoc_const->assoc_const_id)
|
||||
.generic_id,
|
||||
self_type_id, self_witness_id);
|
||||
// Form a specific for the associated constant, and grab the type from
|
||||
// there.
|
||||
auto generic_id = context.associated_constants()
|
||||
.Get(assoc_const->assoc_const_id)
|
||||
.generic_id;
|
||||
auto arg_ids =
|
||||
GetGenericArgsWithSelfType(context, interface_specific_id, generic_id,
|
||||
self_type_id, self_witness_id);
|
||||
auto const_specific_id = MakeSpecific(context, loc, generic_id, arg_ids);
|
||||
return SemIR::GetTypeInSpecific(context.sem_ir(), const_specific_id,
|
||||
context.insts().Get(decl_id).type_id());
|
||||
} else if (auto fn = context.types().TryGetAs<SemIR::FunctionType>(
|
||||
decl.type_id())) {
|
||||
// Form the type of the function within the interface, and attach the `Self`
|
||||
// type.
|
||||
auto interface_fn_type_id =
|
||||
SemIR::GetTypeInSpecific(context.sem_ir(), interface_specific_id,
|
||||
context.insts().Get(decl_id).type_id());
|
||||
auto self_facet_id =
|
||||
GetSelfFacet(context, interface_specific_id,
|
||||
context.functions().Get(fn->function_id).generic_id,
|
||||
self_type_id, self_witness_id);
|
||||
return context.GetFunctionTypeWithSelfType(
|
||||
context.types().GetInstId(interface_fn_type_id), self_facet_id);
|
||||
} else {
|
||||
CARBON_FATAL("Unexpected kind for associated constant {0}", decl);
|
||||
}
|
||||
// TODO: For a `FunctionDecl`, should we substitute `Self` into the type?
|
||||
|
||||
return SemIR::GetTypeInSpecific(context.sem_ir(), specific_id,
|
||||
context.insts().Get(decl_id).type_id());
|
||||
}
|
||||
|
||||
} // namespace Carbon::Check
|
||||
|
||||
Reference in New Issue
Block a user