aboutsummaryrefslogtreecommitdiffhomepage
path: root/cgo/cgo.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2021-11-11 14:12:38 +0100
committerRon Evans <[email protected]>2021-11-24 21:09:29 +0100
commit1789570f52598e96b1d638b9d52910d0278b8f15 (patch)
treeba2a44866de226ea4b71a1d5045fe1e21da968d4 /cgo/cgo.go
parenta536ddcda8025233ad093f5f76d9d728ab67fa3f (diff)
downloadtinygo-1789570f52598e96b1d638b9d52910d0278b8f15.tar.gz
tinygo-1789570f52598e96b1d638b9d52910d0278b8f15.zip
cgo: add //go: pragmas to generated functions and globals
This patch adds //go: pragmas directly to declared functions and globals found during CGo processing. This simplifies the logic in the compiler: it no longer has to consider special "C." prefixed function names. It also makes the cgo pass more flexible in the pragmas it emits for functions and global variables.
Diffstat (limited to 'cgo/cgo.go')
-rw-r--r--cgo/cgo.go32
1 files changed, 22 insertions, 10 deletions
diff --git a/cgo/cgo.go b/cgo/cgo.go
index dc1173962..defae0bca 100644
--- a/cgo/cgo.go
+++ b/cgo/cgo.go
@@ -496,6 +496,14 @@ func (p *cgoPackage) addFuncDecls() {
}
args := make([]*ast.Field, len(fn.args))
decl := &ast.FuncDecl{
+ Doc: &ast.CommentGroup{
+ List: []*ast.Comment{
+ {
+ Slash: fn.pos - 1,
+ Text: "//export " + name,
+ },
+ },
+ },
Name: &ast.Ident{
NamePos: fn.pos,
Name: "C." + name,
@@ -512,14 +520,10 @@ func (p *cgoPackage) addFuncDecls() {
},
}
if fn.variadic {
- decl.Doc = &ast.CommentGroup{
- List: []*ast.Comment{
- {
- Slash: fn.pos,
- Text: "//go:variadic",
- },
- },
- }
+ decl.Doc.List = append(decl.Doc.List, &ast.Comment{
+ Slash: fn.pos - 1,
+ Text: "//go:variadic",
+ })
}
obj.Decl = decl
for i, arg := range fn.args {
@@ -652,13 +656,21 @@ func (p *cgoPackage) addVarDecls() {
}
sort.Strings(names)
for _, name := range names {
+ global := p.globals[name]
gen := &ast.GenDecl{
- TokPos: token.NoPos,
+ TokPos: global.pos,
Tok: token.VAR,
Lparen: token.NoPos,
Rparen: token.NoPos,
+ Doc: &ast.CommentGroup{
+ List: []*ast.Comment{
+ {
+ Slash: global.pos - 1,
+ Text: "//go:extern " + name,
+ },
+ },
+ },
}
- global := p.globals[name]
obj := &ast.Object{
Kind: ast.Var,
Name: "C." + name,