C++ Interop: Support getting void* from C++ functions and passing void* it to C++ function (#6279)

This defines `Cpp.void` as a custom type.
`Cpp.void*` is mapped to C++ `void*`.

Not supported yet: Conversions from and to other pointer types.

C++ Interop Demo:

```carbon
// main.carbon

library "Main";

import Core library "io";

import Cpp inline '''
#include <cstdio>

auto GetPointer() -> void* _Nonnull {
  static int x = 8;
  return &x;
}

auto GetValue(void* _Nonnull ptr) -> int {
  return *static_cast<int*>(ptr);
}
''';

fn Run() -> i32 {
  let ptr: Cpp.void* = Cpp.GetPointer();
  Core.Print(Cpp.GetValue(ptr));
  return 0;
}
```

```shell
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o --output=demo
$ ./demo
8
```

Part of #6280.
This commit is contained in:
Boaz Brickner
2025-10-29 09:03:53 +00:00
committed by GitHub
parent 29d7e52a46
commit 4d4d720ff0
24 changed files with 2673 additions and 2002 deletions
+4
View File
@@ -310,6 +310,10 @@ class Stringifier {
step_stack_->PushInstId(inst.inner_id);
}
auto StringifyInst(InstId /*inst_id*/, CppVoidType /*inst*/) -> void {
*out_ << "Cpp.void";
}
auto StringifyInst(InstId /*inst_id*/, CustomLayoutType inst) -> void {
auto layout = sem_ir_->custom_layouts().Get(inst.layout_id);
*out_ << "<size " << layout[CustomLayoutId::SizeIndex] << ", align "