builder, cgo: support function definitions in CGo headers

For example, the following did not work before but does work with this
change:

    // int add(int a, int b) {
    //   return a + b;
    // }
    import "C"

    func main() {
        println("add:", C.add(3, 5))
    }

Even better, the functions in the header are compiled together with the
rest of the Go code and so they can be optimized together! Currently,
inlining is not yet allowed but const-propagation across functions
works. This should be improved in the future.
This commit is contained in:
Ayke van Laethem
2021-09-17 02:41:45 +02:00
committed by Ron Evans
parent 138add2b96
commit e02727679f
7 changed files with 86 additions and 10 deletions
+4
View File
@@ -10,6 +10,7 @@ int mul(int, int);
*/
import "C"
// int headerfunc(int a) { return a + 1; }
import "C"
import "unsafe"
@@ -43,6 +44,9 @@ func main() {
println("variadic0:", C.variadic0())
println("variadic2:", C.variadic2(3, 5))
// functions in the header C snippet
println("headerfunc:", C.headerfunc(5))
// equivalent types
var goInt8 int8 = 5
var _ C.int8_t = goInt8
+1
View File
@@ -15,6 +15,7 @@ callback 1: 50
callback 2: 600
variadic0: 1
variadic2: 15
headerfunc: 6
bool: true true
float: +3.100000e+000
double: +3.200000e+000