-
Notifications
You must be signed in to change notification settings - Fork 4
/
write.go
399 lines (349 loc) · 8.3 KB
/
write.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package cwrap
import (
"io"
"log"
"path"
"sort"
)
func (pac *Package) prepareFunctions() error {
// populate functions/callbacks (and collect types)
// write .h/.c directly
var functions []*Function
var callbacks []CallbackFunc
callbackSet := NewSSet()
for _, fn := range pac.XmlDoc.Functions {
if len(fn.Ellipses) > 0 {
continue
}
if !pac.exported(fn.CName(), fn.File()) {
// log.Print("skip unexported function ", fn.CName(), " in ", fn.File())
continue
}
f := pac.newFunction(fn)
if info, ok := fn.HasCallback(); ok {
// Go file
callbackFunc := pac.newCallbackFunc(info)
if !callbackSet.Has(callbackFunc.goName) {
callbacks = append(callbacks, callbackFunc)
}
f1, f2 := pac.TransformOriginalFunc(fn, callbackFunc, info)
functions = append(functions, f1)
functions = append(functions, f2)
if !callbackSet.Has(callbackFunc.goName) {
}
// add into set
callbackSet.Add(callbackFunc.goName)
} else {
functions = append(functions, f)
}
}
pac.Functions = functions
pac.Callbacks = callbacks
// populate variables (and collect types)
variables := make([]*Variable, 0, len(pac.Variables))
for _, v := range pac.XmlDoc.Variables {
if pac.exported(v.CName(), v.File()) {
variables = append(variables, pac.newVariable(v))
}
}
pac.Variables = variables
return nil
}
func (pac *Package) prepareTypesAndNames() {
// populate fields (and collect types till no new types come out)
for {
cnt := len(pac.TypeDeclMap)
pac.TypeDeclMap.Each(func(d TypeDecl) {
switch s := d.(type) {
case *Struct:
if s.Fields == nil {
s.Fields = pac.newStructFields(pac.FindStruct(s.Id()).Fields())
}
case *Union:
if s.Fields == nil {
s.Fields = pac.newUnionFields(pac.FindUnion(s.Id()).Fields(), s)
}
}
})
if cnt == len(pac.TypeDeclMap) {
break
}
}
excluded := []string{}
// find linked list, remove the struct and keep the typedef, must go before
// type names are assigned, so that the typedef can get proper names.
pac.TypeDeclMap.Each(func(d TypeDecl) {
if t, ok := d.(*Typedef); ok {
if o, ok := pac.TypeDeclMap[t.rootId]; ok && o.CName() == t.CName() {
excluded = append(excluded, t.rootId)
t.id = t.rootId
}
}
})
// assign names to types, if empty, remove it.
pac.TypeDeclMap.Each(func(d TypeDecl) {
goName := pac.globalName(d)
if goName != "" {
d.SetGoName(goName)
} else {
excluded = append(excluded, d.Id())
}
})
// assign names to functions (must go after types because type name of
// receiver must be settled first.
{
var fs []*Function
for _, f := range pac.Functions {
if m, ok := f.ConvertToMethod(); ok {
m.SetGoName(pac.UpperName(f.CName()))
} else {
f.SetGoName(pac.localName(f))
fs = append(fs, f)
}
}
pac.Functions = fs
}
// add all enumerations regardless of its appearance in functions
for _, em := range pac.Enumerations {
if pac.excluded(em.CName()) || !pac.included(em.File()) {
continue
}
e := pac.declareEqualType(em).(*Enum)
goName := pac.globalName(e)
if goName != "" {
e.goName = goName
}
for i, v := range e.Values {
e.Values[i].goName = pac.globalName(v)
}
}
// then remove the enumeration if it is typedefed.
pac.TypeDeclMap.Each(func(d TypeDecl) {
if t, ok := d.(*Typedef); ok {
if e, ok := t.Literal.(*Enum); ok {
excluded = append(excluded, e.Id())
}
}
})
// optimize 2nd level names like fields and methods, must go after all
// global level names are settled.
pac.TypeDeclMap.Each(func(d TypeDecl) {
if o, ok := d.(NameOptimizer); ok {
o.OptimizeNames()
}
})
// assign name to variables
for _, v := range pac.Variables {
v.SetGoName(pac.localName(v))
}
// remove excluded types
for _, id := range excluded {
pac.TypeDeclMap.Delete(id)
}
}
// Huge function to write all the stuff
func (pac *Package) write() error {
if len(pac.Callbacks) > 0 {
c, err := pac.createFile(pac.cFile())
if err != nil {
return err
}
defer c.Close()
h, err := pac.createFile(pac.hFile())
if err != nil {
return err
}
defer h.Close()
if err := pac.writeCFile(c, h); err != nil {
return err
}
log.Print("written to ", pac.cFile())
log.Print("written to ", pac.hFile())
}
g, err := pac.createFile(pac.goFile())
if err != nil {
return err
}
defer g.Close()
if err := pac.writeGoFile(g); err != nil {
return err
}
if err := gofmt(pac.goFile()); err != nil {
return err
}
log.Print("written to ", pac.goFile())
pac.Statistics.Print()
p()
return nil
}
func (pac *Package) writeCFile(c, h io.Writer) error {
// C file starts
fp(c, `#include "_cgo_export.h"`)
fp(c, "")
for _, callbackFunc := range pac.Callbacks {
// H file
fpn(h, "extern ")
callbackFunc.CType.WriteCDecl(h, callbackFunc.cFuncName)
fp(h, ";")
fp(h, "")
// C file
callbackFunc.CType.WriteCallbackStub(c, callbackFunc.cFuncName, callbackFunc.goName)
fp(c, "")
}
return nil
}
func (pac *Package) writeGoFile(g io.Writer) error {
// Go file starts
fp(g, "package ", pac.PacName)
fp(g, "")
fp(g, "/*")
fp(g, "#include <", pac.From.File, ">")
if len(pac.Callbacks) > 0 {
fp(g, `#include "`, path.Base(pac.hFile()), `"`)
}
fp(g, "#include <stdlib.h>")
for _, d := range pac.From.CgoDirectives {
fp(g, "#cgo ", d)
}
fp(g, "*/")
fp(g, `import "C"`)
fp(g, "")
fp(g, "import (")
fp(g, `"unsafe"`)
for _, inc := range pac.Included {
fp(g, `"`, inc.PacPath, `"`)
}
fp(g, ")")
fp(g, "")
for _, v := range pac.Variables {
pac.writeDecl(g, "var", v)
}
ds := pac.TypeDeclMap.ToSlice()
for _, d := range ds {
pac.writeDecl(g, "type", d)
fp(g, "")
}
for _, f := range pac.Functions {
pac.writeDecl(g, "func", f)
}
for _, f := range pac.Callbacks {
f.Declare(g)
}
return nil
}
func (pac *Package) writeDecl(w io.Writer, keyword string, d Decl) {
if pac.excluded(d.CName()) || contains(d.GoName(), ".") {
return
}
if d.GoName() != "" &&
d.Id() != "" { // is not simple typedef
fp(w, "// ", d.CName())
fpn(w, keyword, " ", d.GoName(), " ")
d.WriteSpec(w)
}
if t, ok := d.(TypeDecl); ok {
t.WriteMethods(w)
}
fp(w, "")
pac.DefCount++
}
// type name that may be declared in this or included packages.
func (pac *Package) globalName(o CNamer) string {
if pac.fileIds.Has(o.File()) && pac.matched(o.CName()) {
return pac.localName(o)
}
for _, inc := range pac.Included {
if goName := inc.globalName(o); goName != "" && !contains(goName, ".") {
return joins(inc.PacName, ".", goName)
}
}
return ""
}
// upper name that is unique within the package
func (pac *Package) localName(o CNamer) string {
n := pac.UpperName(o.CName())
if sid, exists := pac.localNames[n]; !exists || o.Id() == sid {
pac.localNames[n] = o.Id()
return n
}
for {
n += "_"
if _, exists := pac.localNames[n]; !exists {
break
}
}
pac.localNames[n] = o.Id()
return n
}
// upper camel name
func (pac *Package) UpperName(cName string) string {
return upperName(cName, pac.pat)
}
func (pac *Package) isBool(cTypeName string) bool {
return pac.boolSet.Has(cTypeName)
}
func (pac *Package) declare(d TypeDecl) {
pac.TypeDeclMap[d.Id()] = d
}
func (pac *Package) excluded(cName string) bool {
for _, n := range pac.From.Excluded {
if n == cName {
return true
}
}
return false
}
func (pac *Package) included(file string) bool {
return pac.fileIds.Has(file)
}
func (pac *Package) matched(cName string) bool {
return pac.pat.MatchString(cName)
}
func (pac *Package) exported(cName, file string) bool {
return !pac.excluded(cName) &&
pac.included(file) &&
pac.matched(cName)
}
type TypeDecls []TypeDecl
func (s TypeDecls) Len() int {
return len(s)
}
func (s TypeDecls) Less(i, j int) bool {
ni := s[i].GoName()
if ni == "" {
ni = s[i].CName()
}
nj := s[j].GoName()
if nj == "" {
nj = s[j].CName()
}
return ni < nj
}
func (s TypeDecls) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
type TypeDeclMap map[string]TypeDecl
func (m TypeDeclMap) Delete(id string) {
delete(m, id)
}
func (m TypeDeclMap) ToSlice() TypeDecls {
ds := make(TypeDecls, 0, len(m))
for _, d := range m {
ds = append(ds, d)
}
sort.Sort(ds)
return ds
}
func (m TypeDeclMap) Each(visit func(d TypeDecl)) {
for _, d := range m {
eachDecl(d, visit)
}
}
func eachDecl(d TypeDecl, visit func(TypeDecl)) {
visit(d)
if t, ok := d.(*Typedef); ok {
if dd, ok := t.Literal.(TypeDecl); ok {
eachDecl(dd, visit)
}
}
}