mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-29 15:24:58 +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
@@ -14,6 +14,7 @@ auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction {
|
||||
CalleeFunction result = {.function_id = FunctionId::None,
|
||||
.enclosing_specific_id = SpecificId::None,
|
||||
.resolved_specific_id = SpecificId::None,
|
||||
.self_type_id = InstId::None,
|
||||
.self_id = InstId::None,
|
||||
.is_error = false};
|
||||
|
||||
@@ -28,19 +29,24 @@ auto GetCalleeFunction(const File& sem_ir, InstId callee_id) -> CalleeFunction {
|
||||
callee_id = bound_method->function_decl_id;
|
||||
}
|
||||
|
||||
// Identify the function we're calling.
|
||||
// Identify the function we're calling by its type.
|
||||
auto val_id = sem_ir.constant_values().GetConstantInstId(callee_id);
|
||||
if (!val_id.has_value()) {
|
||||
return result;
|
||||
}
|
||||
auto val_inst = sem_ir.insts().Get(val_id);
|
||||
auto struct_val = val_inst.TryAs<StructValue>();
|
||||
if (!struct_val) {
|
||||
result.is_error = val_inst.type_id() == SemIR::ErrorInst::SingletonTypeId;
|
||||
return result;
|
||||
auto fn_type_inst =
|
||||
sem_ir.types().GetAsInst(sem_ir.insts().Get(val_id).type_id());
|
||||
|
||||
if (auto impl_fn_type = fn_type_inst.TryAs<FunctionTypeWithSelfType>()) {
|
||||
// Combine the associated function's `Self` with the interface function
|
||||
// data.
|
||||
result.self_type_id = impl_fn_type->self_id;
|
||||
fn_type_inst = sem_ir.insts().Get(impl_fn_type->interface_function_type_id);
|
||||
}
|
||||
auto fn_type = sem_ir.types().TryGetAs<FunctionType>(struct_val->type_id);
|
||||
|
||||
auto fn_type = fn_type_inst.TryAs<FunctionType>();
|
||||
if (!fn_type) {
|
||||
result.is_error = fn_type_inst.Is<SemIR::ErrorInst>();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user