cgo: implement bool/float/complex types

Этот коммит содержится в:
Ayke van Laethem 2019-02-08 19:56:43 +01:00 коммит произвёл Ron Evans
родитель fab38a0749
коммит da345e8723
5 изменённых файлов: 64 добавлений и 14 удалений

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

@ -177,25 +177,44 @@ func (info *fileInfo) makeASTType(typ C.CXType) ast.Expr {
var typeName string
switch typ.kind {
case C.CXType_SChar:
typeName = "schar"
typeName = "C.schar"
case C.CXType_UChar:
typeName = "uchar"
typeName = "C.uchar"
case C.CXType_Short:
typeName = "short"
typeName = "C.short"
case C.CXType_UShort:
typeName = "ushort"
typeName = "C.ushort"
case C.CXType_Int:
typeName = "int"
typeName = "C.int"
case C.CXType_UInt:
typeName = "uint"
typeName = "C.uint"
case C.CXType_Long:
typeName = "long"
typeName = "C.long"
case C.CXType_ULong:
typeName = "ulong"
typeName = "C.ulong"
case C.CXType_LongLong:
typeName = "longlong"
typeName = "C.longlong"
case C.CXType_ULongLong:
typeName = "ulonglong"
typeName = "C.ulonglong"
case C.CXType_Bool:
typeName = "bool"
case C.CXType_Float, C.CXType_Double, C.CXType_LongDouble:
switch C.clang_Type_getSizeOf(typ) {
case 4:
typeName = "float32"
case 8:
typeName = "float64"
default:
// Don't do anything, rely on the fallback code to show a somewhat
// sensible error message like "undeclared name: C.long double".
}
case C.CXType_Complex:
switch C.clang_Type_getSizeOf(typ) {
case 8:
typeName = "complex64"
case 16:
typeName = "complex128"
}
case C.CXType_Pointer:
return &ast.StarExpr{
Star: info.importCPos,
@ -218,13 +237,14 @@ func (info *fileInfo) makeASTType(typ C.CXType) ast.Expr {
Name: "byte",
},
}
default:
}
if typeName == "" {
// Fallback, probably incorrect but at least the error points to an odd
// type name.
typeName = getString(C.clang_getTypeSpelling(typ))
typeName = "C." + getString(C.clang_getTypeSpelling(typ))
}
return &ast.Ident{
NamePos: info.importCPos,
Name: "C." + typeName,
Name: typeName,
}
}

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

@ -1,6 +1,13 @@
#include "main.h"
int global = 3;
_Bool globalBool = 1;
_Bool globalBool2 = 10; // test narrowing
float globalFloat = 3.1;
double globalDouble = 3.2;
_Complex float globalComplexFloat = 4.1+3.3i;
_Complex double globalComplexDouble = 4.2+3.4i;
_Complex double globalComplexLongDouble = 4.3+3.5i;
int fortytwo() {
return 42;

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

@ -28,6 +28,14 @@ func main() {
println("callback 1:", C.doCallback(20, 30, cb))
cb = C.binop_t(C.mul)
println("callback 2:", C.doCallback(20, 30, cb))
// more globals
println("bool:", C.globalBool, C.globalBool2 == true)
println("float:", C.globalFloat)
println("double:", C.globalDouble)
println("complex float:", C.globalComplexFloat)
println("complex double:", C.globalComplexDouble)
println("complex long double:", C.globalComplexLongDouble)
}
//export mul

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

@ -3,9 +3,18 @@ int add(int a, int b);
typedef int (*binop_t) (int, int);
int doCallback(int a, int b, binop_t cb);
typedef int * intPointer;
extern int global;
void store(int value, int *ptr);
// test globals
extern int global;
extern _Bool globalBool;
extern _Bool globalBool2;
extern float globalFloat;
extern double globalDouble;
extern _Complex float globalComplexFloat;
extern _Complex double globalComplexDouble;
extern _Complex double globalComplexLongDouble;
// test duplicate definitions
int add(int a, int b);
extern int global;

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

@ -8,3 +8,9 @@ global: 3
25: 25
callback 1: 50
callback 2: 600
bool: true true
float: +3.100000e+000
double: +3.200000e+000
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)