mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-04 19:17:47 +00:00
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:
committed by
Ron Evans
parent
138add2b96
commit
e02727679f
Vendored
+4
@@ -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
|
||||
|
||||
Vendored
+1
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user