mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 08:31:04 +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:
@@ -1137,6 +1137,9 @@ static auto MapBuiltinType(Context& context, SemIR::LocId loc_id,
|
||||
context.ints().Add(ast_context.getTypeSize(qual_type))));
|
||||
}
|
||||
// TODO: Handle floating-point types that map to named aliases.
|
||||
} else if (type.isVoidType()) {
|
||||
return ExprAsType(context, Parse::NodeId::None,
|
||||
SemIR::CppVoidType::TypeInstId);
|
||||
}
|
||||
|
||||
return TypeExpr::None;
|
||||
@@ -2116,6 +2119,7 @@ static auto LookupBuiltinTypes(Context& context, SemIR::LocId loc_id,
|
||||
.Case("float", ast_context.FloatTy)
|
||||
.Case("double", ast_context.DoubleTy)
|
||||
.Case("long_double", ast_context.LongDoubleTy)
|
||||
.Case("void", ast_context.VoidTy)
|
||||
.Default(clang::QualType());
|
||||
if (builtin_type.isNull()) {
|
||||
return SemIR::InstId::None;
|
||||
|
||||
Reference in New Issue
Block a user