summaryrefslogtreecommitdiff
path: root/src/cmd/cgo/doc.go
diff options
context:
space:
mode:
authorAlberto Garc?a Hierro <alberto@garciahierro.com>2013-08-13 12:42:21 -0400
committerAlberto Garc?a Hierro <alberto@garciahierro.com>2013-08-13 12:42:21 -0400
commite55042570b579081d643b066ec9649838df26d32 (patch)
tree3f9035c4005d0ca3ed1f6f8ef08fb0863c347f83 /src/cmd/cgo/doc.go
parent568afaa247474eeb25a79a0bdcaae9a072b46866 (diff)
downloadgo-e55042570b579081d643b066ec9649838df26d32.tar.gz
cmd/cgo: Add support for C function pointers
* Add a new kind of Name, "fpvar" which stands for function pointer variable * When walking the AST, find functions used as expressions and create a new Name object for them * Track functions which are only used in expr contexts, and avoid generating bridge code for them R=golang-dev, minux.ma, fullung, rsc, iant CC=golang-dev https://codereview.appspot.com/9835047 Committer: Russ Cox <rsc@golang.org>
Diffstat (limited to 'src/cmd/cgo/doc.go')
-rw-r--r--src/cmd/cgo/doc.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/cmd/cgo/doc.go b/src/cmd/cgo/doc.go
index 17f01c313..63737d4c2 100644
--- a/src/cmd/cgo/doc.go
+++ b/src/cmd/cgo/doc.go
@@ -76,6 +76,33 @@ function returns void). For example:
n, err := C.sqrt(-1)
_, err := C.voidFunc()
+Calling C function pointers is currently not supported, however you can
+declare Go variables which hold C function pointers and pass them
+back and forth between Go and C. C code may call function pointers
+received from Go. For example:
+
+ package main
+ // typedef int (*intFunc) ();
+ //
+ // int
+ // bridge_int_func(intFunc f)
+ // {
+ // return f();
+ // }
+ //
+ // int fortytwo()
+ // {
+ // return 42;
+ // }
+ import "C"
+ import "fmt"
+
+ func main() {
+ f := C.intFunc(C.fortytwo)
+ fmt.Println(int(C.bridge_int_func(f)))
+ // Output: 42
+ }
+
In C, a function argument written as a fixed size array
actually requires a pointer to the first element of the array.
C compilers are aware of this calling convention and adjust