This repository has been archived by the owner on Mar 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
gen_header.go
156 lines (148 loc) · 2.99 KB
/
gen_header.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
"flag"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
var (
outFile = flag.String("o", "libuast.h", "output file")
)
func main() {
flag.Parse()
if err := run(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
const (
headerPrologue = `
#ifdef __cplusplus
extern "C" {
#endif
`
headerEpilogue = `
#ifdef __cplusplus
}
#endif
`
)
func run() error {
buf := new(bytes.Buffer)
buf.WriteString(headerPrologue)
for _, fname := range []string{
"api.go",
"src_index.go",
} {
src, err := ioutil.ReadFile(filepath.Join("./src", fname))
if err != nil {
return err
}
fs := token.NewFileSet()
tree, err := parser.ParseFile(fs, fname, src, parser.ParseComments)
if err != nil {
return err
}
for _, dec := range tree.Decls {
fnc, ok := dec.(*ast.FuncDecl)
if !ok {
continue
}
doc := fnc.Doc.Text()
pref := "export " + fnc.Name.Name
if !strings.Contains(doc, pref) {
continue
}
buf.WriteString("\n")
for _, c := range fnc.Doc.List {
if strings.HasPrefix(c.Text, "//"+pref) {
continue
}
buf.WriteString(strings.TrimRight(c.Text, "\n") + "\n")
}
if err := writeCSignature(buf, fnc); err != nil {
return err
}
}
}
buf.WriteString(headerEpilogue)
return writeHeader(buf.Bytes())
}
func writeCSignature(buf *bytes.Buffer, fnc *ast.FuncDecl) error {
typ := fnc.Type
buf.WriteString("extern ")
if typ.Results != nil && len(typ.Results.List) != 0 {
if err := writeCType(buf, typ.Results.List[0].Type); err != nil {
return err
}
} else {
buf.WriteString("void")
}
buf.WriteString(" ")
buf.WriteString(fnc.Name.Name)
buf.WriteString("(")
for i, p := range fnc.Type.Params.List {
if i != 0 {
buf.WriteString(", ")
}
for j, name := range p.Names {
if j != 0 {
buf.WriteString(", ")
}
if err := writeCType(buf, p.Type); err != nil {
return err
}
buf.WriteString(" ")
buf.WriteString(name.Name)
}
}
buf.WriteString(");\n")
return nil
}
func writeCType(buf *bytes.Buffer, typ ast.Expr) error {
switch typ := typ.(type) {
case *ast.Ident:
buf.WriteString(typ.Name)
case *ast.StarExpr:
if err := writeCType(buf, typ.X); err != nil {
return err
}
buf.WriteString("*")
case *ast.SelectorExpr:
pkg, ok := typ.X.(*ast.Ident)
if !ok {
return fmt.Errorf("unknown package: %T", typ.X)
}
switch pkg.Name {
default:
return fmt.Errorf("unknown package: %v", pkg.Name)
case "unsafe":
if typ.Sel.Name != "Pointer" {
return fmt.Errorf("unknown unsafe type: %v", typ.Sel.Name)
}
buf.WriteString("void*")
return nil
case "C":
}
if err := writeCType(buf, typ.Sel); err != nil {
return err
}
default:
return fmt.Errorf("unsupported type: %T", typ)
}
return nil
}
func writeHeader(data []byte) error {
hdr, err := ioutil.ReadFile("./src/uast.h")
if err != nil {
return err
}
hdr = bytes.Replace(hdr, []byte("/*GO-HEADER*/"), data, 1)
return ioutil.WriteFile(*outFile, hdr, 0644)
}