Support C++ calling Carbon functions with ref parameters (#7107)

When creating the C++ thunk, make the parameters references if the
corresponding callee parameters are `ref`s.

When creating the Carbon thunk, tag the call arguments as `ref` if the
corresponding callee parameters are `ref`s.
This commit is contained in:
Nicholas Bishop
2026-04-23 23:45:26 +00:00
committed by GitHub
parent 709776ad1c
commit 3f63cf4b10
4 changed files with 151 additions and 30 deletions
+24 -3
View File
@@ -399,12 +399,33 @@ auto BuildThunkDefinitionForExport(Context& context,
param_pattern_ids =
context.inst_blocks().Get(thunk_function.param_patterns_id);
}
auto call_param_ids =
context.inst_blocks().Get(thunk_function.call_params_id);
llvm::SmallVector<SemIR::InstId> call_param_ids(
context.inst_blocks().Get(thunk_function.call_params_id));
if (thunk_has_return_param) {
param_pattern_ids = param_pattern_ids.drop_back();
call_param_ids = call_param_ids.drop_back();
call_param_ids.pop_back();
}
auto callee_param_ids =
context.inst_blocks().Get(callee_function.call_param_patterns_id);
// If any explicit parameters of the callee are `ref` parameters,
// modify the corresponding call arguments to be `ref` tagged.
for (auto index = thunk_function.call_param_ranges.explicit_begin().index;
index < thunk_function.call_param_ranges.explicit_end().index; index++) {
if (context.insts().Is<SemIR::RefParamPattern>(callee_param_ids[index])) {
auto& call_param_id = call_param_ids[index];
auto type = context.insts().Get(call_param_id).type_id();
SemIR::LocId loc_id(thunk_id);
call_param_id =
AddInst(context, SemIR::LocIdAndInst::RuntimeVerified(
context.sem_ir(), SemIR::LocId(call_param_id),
SemIR::RefTagExpr{
.type_id = type,
.expr_id = call_param_id,
}));
}
}
auto call_id = BuildThunkCall(context, thunk_function_id, callee_id,