forked from go-clang/gen
/
headerfile.go
535 lines (430 loc) · 13.8 KB
/
headerfile.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package gen
import (
"fmt"
"io/ioutil"
"regexp"
"strings"
"unicode"
"github.com/go-clang/bootstrap/clang"
)
type HeaderFile struct {
api *API
name string
dir string
enums []*Enum
functions []*Function
structs []*Struct
lookupEnum map[string]*Enum
lookupNonTypedefs map[string]string
lookupStruct map[string]*Struct
}
func (h *HeaderFile) addMethod(f *Function, fname string, fnamePrefix string, rt Receiver) bool {
fname = UpperFirstCharacter(fnamePrefix + UpperFirstCharacter(fname))
if e, ok := h.lookupEnum[rt.Type.GoName]; ok {
f.Name = fname
e.Methods = append(e.Methods, f)
return true
} else if s, ok := h.lookupStruct[rt.Type.GoName]; ok && s.CName != "CXString" {
f.Name = fname
if !rt.Type.IsSlice && rt.Type.PointerLevel > 0 {
s.IsPointerComposition = true
}
s.Methods = append(s.Methods, f)
return true
}
return false
}
func (h *HeaderFile) addBasicMethods(f *Function, fname string, fnamePrefix string, rt Receiver) bool {
if len(f.Parameters) == 0 && h.IsEnumOrStruct(f.ReturnType.GoName) {
rtc := rt
rtc.Type = f.ReturnType
fname = TrimCommonFunctionNamePrefix(fname)
if fname == "" {
fname = f.ReturnType.GoName
}
if strings.HasPrefix(f.CName, "clang_create") || strings.HasPrefix(f.CName, "clang_get") || strings.HasSuffix(f.CName, "_create") {
fname = "New" + fname
}
return h.addMethod(f, fname, fnamePrefix, rtc)
} else if (fname[0] == 'i' && fname[1] == 's' && unicode.IsUpper(rune(fname[2]))) || (fname[0] == 'h' && fname[1] == 'a' && fname[2] == 's' && unicode.IsUpper(rune(fname[3]))) {
f.ReturnType.GoName = "bool"
return h.addMethod(f, fname, fnamePrefix, rt)
} else if len(f.Parameters) == 1 && h.IsEnumOrStruct(f.Parameters[0].Type.GoName) && strings.HasPrefix(fname, "dispose") && f.ReturnType.GoName == "void" {
fname = "Dispose"
return h.addMethod(f, fname, fnamePrefix, rt)
} else if len(f.Parameters) == 2 && strings.HasPrefix(fname, "equal") && h.IsEnumOrStruct(f.Parameters[0].Type.GoName) && f.Parameters[0].Type == f.Parameters[1].Type {
fname = "Equal"
f.Parameters[0].Name = commonReceiverName(f.Parameters[0].Type.GoName)
f.Parameters[1].Name = f.Parameters[0].Name + "2"
f.ReturnType.GoName = "bool"
return h.addMethod(f, fname, fnamePrefix, rt)
}
return false
}
func (h *HeaderFile) IsEnumOrStruct(name string) bool {
if _, ok := h.lookupEnum[name]; ok {
return true
} else if _, ok := h.lookupStruct[name]; ok {
return true
}
return false
}
func (h *HeaderFile) setIsPointerComposition(typ *Type) {
if s, ok := h.lookupStruct[typ.GoName]; ok && s.IsPointerComposition {
typ.IsPointerComposition = true
}
}
func (h *HeaderFile) prepareFile() error {
/*
Hide all "void *" fields of structs by replacing the type with "uintptr_t".
To paraphrase the original go-clang source code:
Not hiding these fields confuses the Go GC during garbage collection and
pointer scanning, making it think the heap/stack has been somehow corrupted.
I do not know how the original author debugged this, but one thing: Thank you!
*/
findStructsRe := regexp.MustCompile(`(?s)struct[\s\w]+{.+?}`)
f, err := ioutil.ReadFile(h.name)
if err != nil {
return fmt.Errorf("Cannot read Index.h: %v", err)
}
voidPointerReplacements := map[string]string{}
findVoidPointerRe := regexp.MustCompile(`(?:const\s+)?void\s*\*\s*(\w+(\[\d+\])?;)`)
for _, s := range findStructsRe.FindAll(f, -1) {
s2 := findVoidPointerRe.ReplaceAll(s, []byte("uintptr_t $1"))
if len(s) != len(s2) {
voidPointerReplacements[string(s)] = string(s2)
}
}
fs := string(f)
for s, r := range voidPointerReplacements {
fs = strings.Replace(fs, s, r, -1)
}
if incl := "#include <stdint.h>"; !strings.HasPrefix(fs, incl) { // Include for uintptr_t
fs = "#include <stdint.h>\n\n" + fs
}
err = ioutil.WriteFile(h.name, []byte(fs), 0600)
if err != nil {
return fmt.Errorf("Cannot write %s: %v", h.name, err)
}
return nil
}
func (h *HeaderFile) handleFile(cursor clang.Cursor) {
/*
TODO mark the enum https://github.com/go-clang/gen/issues/40
typedef enum CXChildVisitResult (*CXCursorVisitor)(CXCursor cursor, CXCursor parent, CXClientData client_data);
as manually implemented
*/
// TODO report other enums like callbacks that they are not implemented https://github.com/go-clang/gen/issues/51
cursor.Visit(func(cursor, parent clang.Cursor) clang.ChildVisitResult {
// Only handle code of the current file
sourceFile, _, _, _ := cursor.Location().FileLocation()
isCurrentFile := sourceFile.Name() == h.name
if !strings.HasPrefix(sourceFile.Name(), h.dir) {
return clang.ChildVisit_Continue
}
cname := cursor.Spelling()
cnameIsTypeDef := false
if parentCName := parent.Spelling(); parent.Kind() == clang.Cursor_TypedefDecl && parentCName != "" {
cname = parentCName
cnameIsTypeDef = true
}
switch cursor.Kind() {
case clang.Cursor_EnumDecl:
if cname == "" {
break
}
e := handleEnumCursor(cursor, cname, cnameIsTypeDef)
e.IncludeFiles.addIncludeFile(sourceFile.Name())
if _, ok := h.lookupEnum[e.Name]; !ok {
h.lookupEnum[e.Name] = e
h.lookupNonTypedefs["enum "+e.CName] = e.Name
h.lookupEnum[e.CName] = e
h.enums = append(h.enums, e)
}
case clang.Cursor_FunctionDecl:
if !isCurrentFile {
return clang.ChildVisit_Continue
}
f := handleFunctionCursor(cursor)
if f != nil {
f.IncludeFiles.addIncludeFile(sourceFile.Name())
h.functions = append(h.functions, f)
}
case clang.Cursor_StructDecl:
if cname == "" {
break
}
s := handleStructCursor(cursor, cname, cnameIsTypeDef)
s.api = h.api
s.IncludeFiles.addIncludeFile(sourceFile.Name())
if _, ok := h.lookupStruct[s.Name]; !ok {
h.lookupStruct[s.Name] = s
h.lookupNonTypedefs["struct "+s.CName] = s.Name
h.lookupStruct[s.CName] = s
h.structs = append(h.structs, s)
}
case clang.Cursor_TypedefDecl:
underlyingType := cursor.TypedefDeclUnderlyingType().Spelling()
underlyingStructType := strings.TrimSuffix(strings.TrimPrefix(underlyingType, "struct "), " *")
if s, ok := h.lookupStruct[underlyingStructType]; ok && !s.CNameIsTypeDef && strings.HasPrefix(underlyingType, "struct "+s.CName) {
// Sometimes the typedef is not a parent of the struct but a sibling
sn := handleStructCursor(cursor, cname, true)
sn.api = h.api
sn.IncludeFiles.addIncludeFile(sourceFile.Name())
if sn.Comment == "" {
sn.Comment = s.Comment
}
sn.Members = s.Members
sn.Methods = s.Methods
h.lookupStruct[sn.Name] = sn
h.lookupNonTypedefs["struct "+sn.CName] = sn.Name
h.lookupStruct[sn.CName] = sn
// Update the lookups for the old struct
h.lookupStruct[s.Name] = sn
h.lookupStruct[s.CName] = sn
for i, si := range h.structs {
if si == s {
h.structs[i] = sn
break
}
}
} else if underlyingType == "void *" {
s := handleStructCursor(cursor, cname, true)
s.api = h.api
s.IncludeFiles.addIncludeFile(sourceFile.Name())
if _, ok := h.lookupStruct[s.Name]; !ok {
h.lookupStruct[s.Name] = s
h.lookupNonTypedefs["struct "+s.CName] = s.Name
h.lookupStruct[s.CName] = s
h.structs = append(h.structs, s)
}
}
}
return clang.ChildVisit_Recurse
})
}
func (h *HeaderFile) parse(clangArguments []string) error {
if err := h.prepareFile(); err != nil {
return err
}
// Parse the header file to analyse everything we need to know
idx := clang.NewIndex(0, 1)
defer idx.Dispose()
tu := idx.ParseTranslationUnit(h.name, clangArguments, nil, 0)
defer tu.Dispose()
if !tu.IsValid() {
return fmt.Errorf("Cannot parse Index.h")
}
for _, diag := range tu.Diagnostics() {
switch diag.Severity() {
case clang.Diagnostic_Error:
return fmt.Errorf("Diagnostic error in Index.h: %s", diag.Spelling())
case clang.Diagnostic_Fatal:
return fmt.Errorf("Diagnostic fatal in Index.h: %s", diag.Spelling())
}
}
h.handleFile(tu.TranslationUnitCursor())
return nil
}
func (h *HeaderFile) handle() error {
// Prepare all functions
clangFile := newFile("clang")
for _, f := range h.functions {
var fname string
if h.api.PrepareFunctionName != nil {
fname = h.api.PrepareFunctionName(h, f)
f.Name = fname
} else {
fname = f.Name
}
if h.api.FilterFunction != nil && !h.api.FilterFunction(f) {
continue
}
// Prepare the parameters
for i := range f.Parameters {
p := &f.Parameters[i]
if n, ok := h.lookupNonTypedefs[p.Type.CGoName]; ok {
p.Type.GoName = n
}
if e, ok := h.lookupEnum[p.Type.GoName]; ok {
p.CName = e.Receiver.CName
// TODO remove the receiver... and copy only names here to preserve the original pointers and so https://github.com/go-clang/gen/issues/52
p.Type.GoName = e.Receiver.Type.GoName
p.Type.CGoName = e.Receiver.Type.CGoName
p.Type.CGoName = e.Receiver.Type.CGoName
} else if _, ok := h.lookupStruct[p.Type.GoName]; ok {
}
}
if h.api.PrepareFunction != nil {
h.api.PrepareFunction(f)
}
// Prepare the return argument
if n, ok := h.lookupNonTypedefs[f.ReturnType.CGoName]; ok {
f.ReturnType.GoName = n
}
if e, ok := h.lookupEnum[f.ReturnType.GoName]; ok {
f.ReturnType.CGoName = e.Receiver.Type.CGoName
} else if _, ok := h.lookupStruct[f.ReturnType.GoName]; ok {
}
// Prepare the receiver
var rt Receiver
if len(f.Parameters) > 0 {
rt.Name = commonReceiverName(f.Parameters[0].Type.GoName)
rt.CName = f.Parameters[0].CName
rt.Type = f.Parameters[0].Type
} else {
if e, ok := h.lookupEnum[f.ReturnType.GoName]; ok {
rt.Type = e.Receiver.Type
} else if s, ok := h.lookupStruct[f.ReturnType.GoName]; ok {
rt.Type.GoName = s.Name
}
}
// Check upfront if we can handle a function
found := false
for _, p := range f.Parameters {
if h.api.FilterFunctionParameter != nil && !h.api.FilterFunctionParameter(p) {
continue
}
// Return arguments and slices are always ok since we mark them earlier
if p.Type.IsReturnArgument || p.Type.IsSlice {
continue
}
if (!h.IsEnumOrStruct(p.Type.GoName) && !p.Type.IsPrimitive) || p.Type.PointerLevel != 0 {
found = true
fmt.Printf("Cannot handle parameter %s -> %#v\n", f.CName, p)
break
}
}
// If we find a heuristic to add the function, add it!
added := false
if !found {
added = h.addBasicMethods(f, fname, "", rt)
if !added {
if s := strings.Split(f.Name, "_"); len(s) == 2 {
if s[0] == rt.Type.GoName {
rtc := rt
rtc.Name = s[0]
added = h.addBasicMethods(f, s[1], "", rtc)
} else {
if s[0] != "" {
s[0] += "_"
}
added = h.addBasicMethods(f, strings.Join(s[1:], ""), s[0], rt)
}
}
}
if !added {
fname = TrimCommonFunctionName(fname, rt.Type)
if fn := strings.TrimPrefix(fname, f.ReturnType.GoName+"_"); len(fn) != len(fname) {
fname = fn
}
added = h.addMethod(f, fname, "", rt)
if !added && h.IsEnumOrStruct(f.ReturnType.GoName) {
rtc := rt
rtc.Type = f.ReturnType
fname = TrimCommonFunctionNamePrefix(fname)
if fname == "" {
fname = f.ReturnType.GoName
}
if strings.HasPrefix(f.CName, "clang_create") || strings.HasPrefix(f.CName, "clang_get") || strings.HasSuffix(f.CName, "_create") {
fname = "New" + fname
}
added = h.addMethod(f, fname, "", rtc)
}
if !added {
f.Name = UpperFirstCharacter(f.Name)
clangFile.Functions = append(clangFile.Functions, f)
added = true
}
}
}
if !added {
fmt.Println("Unused function:", f.Name)
}
}
for _, e := range h.enums {
if err := e.addEnumStringMethods(); err != nil {
return fmt.Errorf("Cannot generate enum string methods: %v", err)
}
for i, m := range e.Methods {
if r := h.handleMethod(e.Name, m); r != "" {
e.Methods[i] = r
}
switch m := m.(type) {
case *Function:
if len(m.Parameters) > 0 && !m.Parameters[0].Type.IsSlice && m.Parameters[0].Type.GoName == e.Name {
m.Receiver = Receiver{
Name: commonReceiverName(e.Name),
Type: m.Parameters[0].Type,
}
h.setIsPointerComposition(&m.Receiver.Type)
}
for i := range m.Parameters {
h.setIsPointerComposition(&m.Parameters[i].Type)
}
e.IncludeFiles.unifyIncludeFiles(m.IncludeFiles)
e.Methods[i] = m.generate()
}
}
if err := e.generate(); err != nil {
return fmt.Errorf("Cannot generate enum: %v", err)
}
}
for _, s := range h.structs {
if err := s.addMemberGetters(); err != nil {
return fmt.Errorf("Cannot generate struct member getters: %v", err)
}
for i, m := range s.Methods {
if r := h.handleMethod(s.Name, m); r != "" {
s.Methods[i] = r
}
switch m := m.(type) {
case *Function:
s.IncludeFiles.unifyIncludeFiles(m.IncludeFiles)
}
}
if err := s.generate(); err != nil {
return fmt.Errorf("Cannot generate struct: %v", err)
}
}
if len(clangFile.Functions) > 0 {
for _, m := range clangFile.Functions {
switch m := m.(type) {
case *Function:
clangFile.IncludeFiles.unifyIncludeFiles(m.IncludeFiles)
}
}
for i, m := range clangFile.Functions {
if r := h.handleMethod("", m); r != "" {
clangFile.Functions[i] = r
}
}
if err := clangFile.generate(); err != nil {
return fmt.Errorf("Cannot generate clang file: %v", err)
}
}
return nil
}
func (h *HeaderFile) handleMethod(receiverName string, m interface{}) string {
switch m := m.(type) {
case *Function:
if h.api.FixedFunctionName != nil {
if fname := h.api.FixedFunctionName(m); fname != "" {
m.Name = fname
}
}
if len(m.Parameters) > 0 && !m.Parameters[0].Type.IsSlice && m.Parameters[0].Type.GoName == receiverName {
m.Receiver = Receiver{
Name: commonReceiverName(receiverName),
Type: m.Parameters[0].Type,
}
h.setIsPointerComposition(&m.Receiver.Type)
}
for i := range m.Parameters {
h.setIsPointerComposition(&m.Parameters[i].Type)
}
h.setIsPointerComposition(&m.ReturnType)
return m.generate()
}
return ""
}