aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2019-02-08 19:56:43 +0100
committerRon Evans <[email protected]>2019-02-18 17:17:56 +0100
commitda345e8723eac7956a4895d6f13890d9bd0b2aeb (patch)
treeb03230df3959be5480c230830de68de57558ffd3
parentfab38a07493bd4f4030cdef2356afc316d73f509 (diff)
downloadtinygo-da345e8723eac7956a4895d6f13890d9bd0b2aeb.tar.gz
tinygo-da345e8723eac7956a4895d6f13890d9bd0b2aeb.zip
cgo: implement bool/float/complex types
-rw-r--r--loader/libclang.go46
-rw-r--r--testdata/cgo/main.c7
-rw-r--r--testdata/cgo/main.go8
-rw-r--r--testdata/cgo/main.h11
-rw-r--r--testdata/cgo/out.txt6
5 files changed, 64 insertions, 14 deletions
diff --git a/loader/libclang.go b/loader/libclang.go
index 57e573b21..fe7db21d8 100644
--- a/loader/libclang.go
+++ b/loader/libclang.go
@@ -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,
}
}
diff --git a/testdata/cgo/main.c b/testdata/cgo/main.c
index 2f3269ea0..b4ac14115 100644
--- a/testdata/cgo/main.c
+++ b/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;
diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go
index 0733d226e..261f1b5fe 100644
--- a/testdata/cgo/main.go
+++ b/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
diff --git a/testdata/cgo/main.h b/testdata/cgo/main.h
index be529d4fb..39a76f4f8 100644
--- a/testdata/cgo/main.h
+++ b/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;
diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt
index 8d6845ee7..79f8cc63f 100644
--- a/testdata/cgo/out.txt
+++ b/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)