mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
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:
@@ -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 "
|
||||
|
||||
Reference in New Issue
Block a user