cgo: implement void* pointer type

void* is translated to unsafe.Pointer on the Go side.
Этот коммит содержится в:
Ayke van Laethem 2019-04-20 15:48:45 +02:00 коммит произвёл Ron Evans
родитель 9c46ac4eed
коммит b815d3f760
5 изменённых файлов: 22 добавлений и 1 удалений

Просмотреть файл

@ -307,9 +307,23 @@ func (info *fileInfo) makeASTType(typ C.CXType) ast.Expr {
typeName = "complex128"
}
case C.CXType_Pointer:
pointeeType := C.clang_getPointeeType(typ)
if pointeeType.kind == C.CXType_Void {
// void* type is translated to Go as unsafe.Pointer
return &ast.SelectorExpr{
X: &ast.Ident{
NamePos: info.importCPos,
Name: "unsafe",
},
Sel: &ast.Ident{
NamePos: info.importCPos,
Name: "Pointer",
},
}
}
return &ast.StarExpr{
Star: info.importCPos,
X: info.makeASTType(C.clang_getPointeeType(typ)),
X: info.makeASTType(pointeeType),
}
case C.CXType_ConstantArray:
return &ast.ArrayType{

2
testdata/cgo/main.c предоставленный
Просмотреть файл

@ -9,6 +9,8 @@ _Complex float globalComplexFloat = 4.1+3.3i;
_Complex double globalComplexDouble = 4.2+3.4i;
_Complex double globalComplexLongDouble = 4.3+3.5i;
char globalChar = 100;
void *globalVoidPtrSet = &global;
void *globalVoidPtrNull;
collection_t globalStruct = {256, -123456, 3.14, 88};
int globalStructSize = sizeof(globalStruct);
short globalArray[3] = {5, 6, 7};

2
testdata/cgo/main.go предоставленный
Просмотреть файл

@ -41,6 +41,8 @@ func main() {
println("complex double:", C.globalComplexDouble)
println("complex long double:", C.globalComplexLongDouble)
println("char match:", C.globalChar == 100)
var voidPtr unsafe.Pointer = C.globalVoidPtrNull
println("void* match:", voidPtr == nil, C.globalVoidPtrNull == nil, (*C.int)(C.globalVoidPtrSet) == &C.global)
// complex types
println("struct:", C.int(unsafe.Sizeof(C.globalStruct)) == C.globalStructSize, C.globalStruct.s, C.globalStruct.l, C.globalStruct.f)

2
testdata/cgo/main.h предоставленный
Просмотреть файл

@ -37,6 +37,8 @@ extern _Complex float globalComplexFloat;
extern _Complex double globalComplexDouble;
extern _Complex double globalComplexLongDouble;
extern char globalChar;
extern void *globalVoidPtrSet;
extern void *globalVoidPtrNull;
extern collection_t globalStruct;
extern int globalStructSize;
extern short globalArray[3];

1
testdata/cgo/out.txt предоставленный
Просмотреть файл

@ -15,6 +15,7 @@ complex float: (+4.100000e+000+3.300000e+000i)
complex double: (+4.200000e+000+3.400000e+000i)
complex long double: (+4.300000e+000+3.500000e+000i)
char match: true
void* match: true true true
struct: true 256 -123456 +3.140000e+000
array: 5 6 7
union: true