diff --git a/cgo/cgo.go b/cgo/cgo.go index 803af428..67979230 100644 --- a/cgo/cgo.go +++ b/cgo/cgo.go @@ -86,6 +86,8 @@ var cgoAliases = map[string]string{ "C.uint32_t": "uint32", "C.uint64_t": "uint64", "C.uintptr_t": "uintptr", + "C.float": "float32", + "C.double": "float64", } // builtinAliases are handled specially because they only exist on the Go side @@ -309,6 +311,12 @@ func Process(files []*ast.File, dir, importPath string, fset *token.FileSet, cfl // Process CGo imports for each file. for i, f := range files { cf := p.newCGoFile(f, i) + // Float and double are aliased, meaning that C.float is the same thing + // as float32 in Go. + cf.names["float"] = clangCursor{} + cf.names["double"] = clangCursor{} + // Now read all the names (identifies) that C defines in the header + // snippet. cf.readNames(p.cgoHeaders[i], cflagsForCGo, filepath.Base(fset.File(f.Pos()).Name()), func(names map[string]clangCursor) { for _, name := range builtinAliases { // Names such as C.int should not be obtained from C. diff --git a/cgo/testdata/types.go b/cgo/testdata/types.go index 50d5b8b6..fd15b717 100644 --- a/cgo/testdata/types.go +++ b/cgo/testdata/types.go @@ -112,6 +112,10 @@ import "C" import "C" var ( + // aliases + _ C.float + _ C.double + // Simple typedefs. _ C.myint diff --git a/testdata/cgo/main.go b/testdata/cgo/main.go index 417b43a9..f4ee532a 100644 --- a/testdata/cgo/main.go +++ b/testdata/cgo/main.go @@ -74,8 +74,10 @@ func main() { println("union:", C.int(unsafe.Sizeof(C.globalUnion)) == C.globalUnionSize) C.unionSetShort(22) println("union s:", *C.globalUnion.unionfield_s()) - C.unionSetFloat(3.14) - println("union f:", *C.globalUnion.unionfield_f()) + C.unionSetFloat(C.float(6.28)) + println("union f (C.float):", *C.globalUnion.unionfield_f()) + C.unionSetFloat(float32(3.14)) + println("union f (float32):", *C.globalUnion.unionfield_f()) C.unionSetData(5, 8, 1) data := C.globalUnion.unionfield_data() println("union global data:", data[0], data[1], data[2]) diff --git a/testdata/cgo/out.txt b/testdata/cgo/out.txt index 48e29a16..d5f3b137 100644 --- a/testdata/cgo/out.txt +++ b/testdata/cgo/out.txt @@ -32,7 +32,8 @@ struct: true 256 -123456 +3.140000e+000 array: 5 6 7 union: true union s: 22 -union f: +3.140000e+000 +union f (C.float): +6.280000e+000 +union f (float32): +3.140000e+000 union global data: 5 8 1 union local data: 5 8 1 union s: true