Add support for C++ calling Carbon functions with parameters (#7024)

This works by generating two thunks, one in C++ and one in Carbon. For
example, given this input:
```c++
// Carbon:
fn Callme(f: f32) {}

// C++:
void F() {
  // This will call `Callme__cpp_thunk`
  Carbon::Callme(1.0);
}
```

These functions are generated:
```c++
// Carbon:
fn Callme__carbon_thunk(ref f: f32) {
  // Call the target function.
  Callme(f);
}

// C++:

// C++ declaration for the Carbon thunk.
void Callme__carbon_thunk(float& f);

void Callme__cpp_thunk(float f) {
  // Call the Carbon thunk with args passed by reference.
  Callme__carbon_thunk(f);
}
```

For now, all arguments are passed by reference, even if they are simple
types like pointers or i32.

Functions with non-void return types are not supported yet.
This commit is contained in:
Nicholas Bishop
2026-04-07 23:25:57 +00:00
committed by GitHub
parent d1dc8e820d
commit 0635f4628f
10 changed files with 571 additions and 81 deletions
+3 -3
View File
@@ -139,8 +139,8 @@ static auto MakeFunctionSignature(Context& context, SemIR::LocId loc_id,
context.full_pattern_stack().EndImplicitParamList();
}
// Build and add any explicit parameters. We always use value parameters for
// now.
// Build and add any explicit parameters. Whether these are references
// or not is controlled by `args.params_are_refs`.
context.full_pattern_stack().StartExplicitParamList();
if (args.param_type_ids.empty()) {
insts.param_patterns_id = SemIR::InstBlockId::Empty;
@@ -154,7 +154,7 @@ static auto MakeFunctionSignature(Context& context, SemIR::LocId loc_id,
context.inst_block_stack().AddInstId(AddParamPattern(
context, loc_id, SemIR::NameId::Underscore, param_type_region_id,
param_type_id, /*is_ref=*/false));
param_type_id, /*is_ref=*/args.params_are_refs));
}
insts.param_patterns_id = context.inst_block_stack().Pop();
}