aboutsummaryrefslogtreecommitdiffhomepage
path: root/loader
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-09-17 02:41:45 +0200
committerRon Evans <[email protected]>2021-09-28 18:44:11 +0200
commite02727679fc9cc24e59e6d5378eabd313605cfc6 (patch)
tree33fa8df895c98a5e58b9b9fe70eecf1d5ed55e75 /loader
parent138add2b96532fee1f132a69b706bbc5d10ed457 (diff)
downloadtinygo-e02727679fc9cc24e59e6d5378eabd313605cfc6.tar.gz
tinygo-e02727679fc9cc24e59e6d5378eabd313605cfc6.zip
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.
Diffstat (limited to 'loader')
-rw-r--r--loader/loader.go4
1 files changed, 3 insertions, 1 deletions
diff --git a/loader/loader.go b/loader/loader.go
index a201b1526..3991fdcf5 100644
--- a/loader/loader.go
+++ b/loader/loader.go
@@ -72,6 +72,7 @@ type Package struct {
Files []*ast.File
FileHashes map[string][]byte
CFlags []string // CFlags used during CGo preprocessing (only set if CGo is used)
+ CGoHeaders []string // text above 'import "C"' lines
Pkg *types.Package
info types.Info
}
@@ -397,8 +398,9 @@ func (p *Package) parseFiles() ([]*ast.File, error) {
if p.program.clangHeaders != "" {
initialCFlags = append(initialCFlags, "-Xclang", "-internal-isystem", "-Xclang", p.program.clangHeaders)
}
- generated, cflags, ldflags, accessedFiles, errs := cgo.Process(files, p.program.workingDir, p.program.fset, initialCFlags)
+ generated, headerCode, cflags, ldflags, accessedFiles, errs := cgo.Process(files, p.program.workingDir, p.program.fset, initialCFlags)
p.CFlags = append(initialCFlags, cflags...)
+ p.CGoHeaders = headerCode
for path, hash := range accessedFiles {
p.FileHashes[path] = hash
}