-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
1580 lines (1321 loc) · 41.7 KB
/
generator.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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
/*
#cgo CFLAGS: -I /opt/homebrew/Cellar/llvm/17.0.2/include
#cgo LDFLAGS: -L /opt/homebrew/Cellar/llvm/17.0.2/lib -l clang
#include <clang-c/Index.h>
#include <stdlib.h>
extern void visitChildren(const CXCursor cursor, const void *context);
extern void ParseHeader(const char *target, const char *isysroot, const char *header, const void * index, const void *translationUnit, const void *cursor);
*/
import "C"
import (
"bytes"
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"regexp"
"runtime"
"strings"
"unsafe"
"gopkg.in/yaml.v2"
)
type SDK string
var MacOS SDK = "macosx"
var IPhoneOS SDK = "iphoneos"
var IPhoneOSSimulator SDK = "iossimulator"
type TAPI []map[any]any
func (t TAPI) ReexportedFrameworks() []string {
frameworks := map[string]bool{}
for _, tapi := range t {
if l, ok := tapi["reexported-libraries"]; ok {
for _, i := range l.([]any) {
if k, ok := i.(map[any]any)["libraries"]; ok {
for _, s := range k.([]any) {
frameworks[s.(string)] = true
}
}
}
}
}
f := []string{}
for framework := range frameworks {
if !strings.HasPrefix(framework, "/System/Library/Frameworks/") {
continue
}
framework = strings.TrimSuffix(strings.Split(strings.TrimPrefix(framework, "/System/Library/Frameworks/"), "/")[0], ".framework")
f = append(f, framework)
}
return f
}
func (t TAPI) IsExportedClass(cls string) bool {
for _, tapi := range t {
if exports, ok := tapi["exports"]; ok {
for _, export := range exports.([]any) {
if classes, ok := export.(map[any]any)["objc-classes"]; ok {
for _, c := range classes.([]any) {
if c == cls {
return true
}
}
}
}
}
}
return false
}
type listener struct {
seen map[string]bool
target string
sdkPath string
frameworksPath string
currentFile string
tapi map[string]TAPI
}
func main() {
parser := &listener{
seen: map[string]bool{"framework:objc": true},
tapi: map[string]TAPI{},
}
target := os.Args[1]
sdk := SDK(os.Args[2])
frameworks := os.Args[3:]
if b, err := exec.Command("xcrun", "-sdk", string(sdk), "--show-sdk-path").Output(); err != nil {
panic(err)
} else {
parser.target = target
parser.sdkPath = strings.TrimSpace(string(b))
parser.frameworksPath = path.Join(strings.TrimSpace(string(b)), "System", "Library", "Frameworks")
for i, framework := range frameworks {
if framework == "*" {
if entries, err := os.ReadDir(parser.frameworksPath); err != nil {
panic(err)
} else {
toAdd := frameworks[:i]
for _, entry := range entries {
if entry.IsDir() && strings.HasSuffix(entry.Name(), ".framework") {
framework := strings.TrimSuffix(path.Base(entry.Name()), ".framework")
parser.seen["framework:"+framework] = true
toAdd = append(toAdd, framework)
}
}
frameworks = append(toAdd, frameworks[i+1:]...)
}
}
}
// if err := parser.ReadHeader(path.Join(parser.sdkPath, "usr", "include", "objc", "objc.h")); err != nil {
// log.Println("WARN: unable to parse objc:", err)
// }
if err := parser.ReadHeader(path.Join(parser.sdkPath, "usr", "include", "os", "object.h")); err != nil {
log.Println("WARN: unable to parse os/object:", err)
}
for _, framework := range frameworks {
if err := parser.ReadFramework(framework); err != nil {
log.Println("error importing framework", framework+":", err)
}
}
}
}
func (p *listener) SearchHeader(header string, relative bool) error {
if relative {
if stats, err := os.Stat(path.Join(path.Dir(p.currentFile), header)); err != nil && !os.IsNotExist(err) {
return err
} else if err == nil && !stats.IsDir() {
return p.ReadHeader(path.Join(path.Dir(p.currentFile), header))
}
} else if strings.Contains(header, "/") {
parts := strings.Split(header, "/")
headerPath := path.Join(p.frameworksPath, parts[0]+".framework", "Headers", strings.Join(parts[1:], "/"))
if stats, err := os.Stat(headerPath); err != nil && !os.IsNotExist(err) {
return err
} else if err == nil && !stats.IsDir() {
return p.ReadHeader(headerPath)
}
} else {
headerPath := path.Join(p.sdkPath, "usr", "include", header)
if stats, err := os.Stat(headerPath); err != nil && !os.IsNotExist(err) {
return err
} else if err == nil && !stats.IsDir() {
return p.ReadHeader(headerPath)
}
}
return os.ErrNotExist
}
func (p *listener) LoadTAPI(framework string) (TAPI, error) {
if tapi, ok := p.tapi[framework]; ok {
return tapi, nil
} else {
tapi := []map[any]any{}
seen := map[string]bool{}
var loadFramework func(framework string) error
loadFramework = func(framework string) error {
if seen[framework] {
return nil
}
seen[framework] = true
if tapiBuf, err := os.ReadFile(path.Join(p.frameworksPath, framework+".framework", framework+".tbd")); err != nil {
return err
} else {
dec := yaml.NewDecoder(bytes.NewReader(tapiBuf))
for {
var doc map[any]any
if dec.Decode(&doc) != nil {
break
}
tapi = append(tapi, doc)
}
for _, framework := range TAPI(tapi).ReexportedFrameworks() {
if err := loadFramework(framework); err != nil {
return err
}
}
return nil
}
}
p.tapi[framework] = tapi
if err := loadFramework(framework); err != nil {
return nil, err
} else {
return tapi, nil
}
}
}
var reHeaders = regexp.MustCompile(`\s+header\s*"([^"]*)"`)
func (p *listener) ReadFramework(framework string) error {
if _, ok := p.seen["framework:"+framework]; !ok {
p.seen["framework:"+framework] = true
log.Println("📚 importing framework", framework)
if b, err := os.ReadFile(path.Join(p.frameworksPath, framework+".framework", "Modules", "module.modulemap")); err != nil {
return err
} else {
for _, m := range reHeaders.FindAllSubmatch(b, -1) {
header := framework + "/" + string(m[1])
if err := p.SearchHeader(header, false); err != nil {
return err
}
}
}
}
return nil
}
type VisitorPhase int
const (
VisitorPhaseInitial VisitorPhase = iota
VisitorPhaseDetail
)
type VisitorContext struct {
tapi TAPI
framework string
phase VisitorPhase
primitives []string
structs map[uint]*Struct
interfaces map[uint]*Interface
methods map[uint]*Method
protocols map[uint]*Protocol
Visitor func(cursor C.CXCursor, parent C.CXCursor, context *VisitorContext) C.int
}
type Loc struct {
Framework string
Filename string
Line uint
Column uint
}
func (l *Loc) String() string {
return fmt.Sprintf("%s:%d:%d", l.Filename, l.Line, l.Column)
}
type Pointer struct {
InnerType any
}
func (p *Pointer) String() string {
return fmt.Sprintf("%s*", p.InnerType)
}
type VariableArray struct {
InnerType any
}
func (a *VariableArray) String() string {
return fmt.Sprintf("%s[]", a.InnerType)
}
type FixedArray struct {
InnerType any
Length int
}
func (a *FixedArray) String() string {
return fmt.Sprintf("%s[%d]", a.InnerType, a.Length)
}
type Interface struct {
Loc *Loc
ForwardDecl bool
Name string
Super *Interface
Protocols []*Protocol
Properties []*Property
Methods []*Method
}
type Protocol struct {
Loc *Loc
Name string
Methods []*Method
}
type Struct struct {
Loc *Loc
Canonical bool
Name string
IsAnonymous bool
Fields []*Field
}
func (s *Struct) String() string {
if s.IsAnonymous {
return "struct (anonymous)"
}
return fmt.Sprintf("struct %s.%s", s.Loc.Framework, s.Name)
}
func (s *Protocol) CanonicalName() string {
return unHyphenate(s.Name, "_")
}
func (s *Interface) CanonicalName() string {
return unHyphenate(s.Name, "_")
}
func (s *Struct) CanonicalName() string {
return unHyphenate(s.Name, "_")
}
type Field struct {
Loc *Loc
Name string
Type any
}
func (f *Field) String() string {
var type_ string
if t, ok := f.Type.(*Interface); ok {
type_ = fmt.Sprintf("%s.%s", t.Loc.Framework, t.Name)
} else if t, ok := f.Type.(*Struct); ok {
type_ = fmt.Sprintf("%s.%s", t.Loc.Framework, t.Name)
} else {
panic("ERR: invalid field type")
}
return fmt.Sprintf("%s %s", type_, f.Name)
}
func (p *Protocol) String() string {
return fmt.Sprintf("protocol %s.%s", p.Loc.Framework, p.Name)
}
func (i *Interface) String() string {
if i.Super == nil {
return fmt.Sprintf("interface %s.%s", i.Loc.Framework, i.Name)
} else {
return fmt.Sprintf("interface %s.%s : %s.%s", i.Loc.Framework, i.Name, i.Super.Loc.Framework, i.Super.Name)
}
}
type Property struct {
Loc *Loc
Name string
Type any
}
type Method struct {
Loc *Loc
Name string
ReturnType any
Instance bool
Interface *Interface
Parameters []*Parameter
}
func (m *Method) String() string {
var returnType string
if t, ok := m.ReturnType.(*Interface); ok {
returnType = t.Name
} else if t, ok := m.ReturnType.(*Struct); ok {
returnType = t.Name
} else {
panic("ERR: invalid return type")
}
if m.Instance {
return fmt.Sprintf("- (%s)%s", returnType, m.Name)
} else {
return fmt.Sprintf("+ (%s)%s", returnType, m.Name)
}
}
type Parameter struct {
Loc *Loc
Name string
Type any
}
func (p *Parameter) String() string {
var type_ string
if t, ok := p.Type.(*Interface); ok {
type_ = fmt.Sprintf("%s.%s", t.Loc.Framework, t.Name)
} else if t, ok := p.Type.(*Struct); ok {
type_ = fmt.Sprintf("%s.%s", t.Loc.Framework, t.Name)
} else {
panic("ERR: invalid field type")
}
return fmt.Sprintf("(%s)%s", type_, p.Name)
}
//export visitor
func visitor(cursor C.CXCursor, parent C.CXCursor, context unsafe.Pointer) C.int {
visitorContext := (*VisitorContext)(context)
return visitorContext.Visitor(cursor, parent, visitorContext)
}
func (v *VisitorContext) ResolveType(t C.CXType) any {
pointers := 0
arrays := []int{}
adornType := func(t any) any {
for i := 0; i < pointers; i++ {
t = &Pointer{InnerType: t}
}
for i := len(arrays) - 1; i >= 0; i-- {
a := arrays[i]
if a == -1 {
t = &VariableArray{InnerType: t}
} else {
t = &FixedArray{InnerType: t, Length: a}
}
}
return t
}
for t.kind == C.CXType_Pointer || t.kind == C.CXType_ObjCObjectPointer {
pointers++
t = C.clang_getPointeeType(t)
}
t = C.clang_getUnqualifiedType(t)
if t.kind == C.CXType_Enum {
return "int"
}
spelling := C.clang_getTypeSpelling(t)
ts := C.GoString(C.clang_getCString(spelling))
C.clang_disposeString(spelling)
if ts == "BOOL" {
return adornType("BOOL")
}
pointers = 0
canonicalType := C.clang_getCanonicalType(t)
for canonicalType.kind == C.CXType_Vector || canonicalType.kind == C.CXType_IncompleteArray || canonicalType.kind == C.CXType_ConstantArray || canonicalType.kind == C.CXType_VariableArray || canonicalType.kind == C.CXType_DependentSizedArray {
if canonicalType.kind == C.CXType_ConstantArray {
size := C.clang_getArraySize(canonicalType)
arrays = append(arrays, int(size))
} else {
arrays = append(arrays, -1)
}
canonicalType = C.clang_getElementType(canonicalType)
}
for canonicalType.kind == C.CXType_Pointer || canonicalType.kind == C.CXType_ObjCObjectPointer {
pointers++
canonicalType = C.clang_getPointeeType(canonicalType)
}
canonicalType = C.clang_getUnqualifiedType(canonicalType)
if canonicalType.kind == C.CXType_Elaborated {
canonicalType = C.clang_Type_getNamedType(canonicalType)
}
if canonicalType.kind == C.CXType_ObjCObject {
canonicalType = C.clang_Type_getObjCObjectBaseType(canonicalType)
}
if canonicalType.kind == C.CXType_Enum {
return adornType("int")
}
if canonicalType.kind == C.CXType_FunctionProto || canonicalType.kind == C.CXType_FunctionNoProto || canonicalType.kind == C.CXType_BlockPointer {
return adornType("func")
}
ctypeSpelling := C.clang_getTypeSpelling(canonicalType)
cts := C.GoString(C.clang_getCString(ctypeSpelling))
C.clang_disposeString(ctypeSpelling)
for _, primitive := range v.primitives {
if primitive == cts {
return adornType(primitive)
}
}
c := C.clang_getTypeDeclaration(canonicalType)
// cursorType := C.clang_getCursorType(c)
// spelling = C.clang_getTypeSpelling(cursorType)
// cts = C.GoString(C.clang_getCString(spelling))
// C.clang_disposeString(spelling)
if c.kind == C.CXCursor_UnionDecl {
return nil
}
ch := uint(C.clang_hashCursor(c))
if struct_, ok := v.structs[ch]; ok {
return adornType(struct_)
}
if protocol, ok := v.structs[ch]; ok {
return adornType(protocol)
}
if iface, ok := v.interfaces[ch]; ok {
return adornType(iface)
}
for _, struct_ := range v.structs {
if struct_.Name == cts {
return adornType(struct_)
}
}
for _, protocol := range v.protocols {
if protocol.Name == cts {
return adornType(protocol)
}
}
for _, iface := range v.interfaces {
if iface.Name == cts {
return adornType(iface)
}
}
log.Println(v.framework, v.structs, canonicalType)
panic("ERR: previous declaration for type not found: " + ts + " (" + cts + ")")
}
func (p *listener) ReadHeader(header string) error {
if p.seen[header] {
return nil
} else {
p.seen[header] = true
}
oldCurrentFile := p.currentFile
p.currentFile = header
defer func() {
p.currentFile = oldCurrentFile
}()
var currentFramework string
if strings.HasPrefix(header, p.frameworksPath) {
currentFramework = strings.TrimSuffix(strings.Split(strings.TrimPrefix(header, p.frameworksPath), "/")[1], ".framework")
} else {
currentFramework = "objc"
}
var tapi TAPI
if currentFramework != "objc" {
if t, err := p.LoadTAPI(currentFramework); err != nil {
return err
} else {
tapi = t
}
}
ch := C.CString(header)
defer C.free(unsafe.Pointer(ch))
target := C.CString(p.target)
defer C.free(unsafe.Pointer(target))
isysroot := C.CString(p.sdkPath)
defer C.free(unsafe.Pointer(isysroot))
context := &VisitorContext{
tapi: tapi,
framework: currentFramework,
methods: map[uint]*Method{},
primitives: []string{
"void",
"instancetype",
"Class",
"SEL",
"Protocol",
"id",
"char",
"signed char",
"struct _NSZone",
"unsigned __int128",
"unsigned char",
"signed char",
"short",
"unsigned short",
"int",
"unsigned int",
"long",
"unsigned long",
"long long",
"unsigned long long",
"float",
"double",
"long double",
},
structs: map[uint]*Struct{},
interfaces: map[uint]*Interface{},
protocols: map[uint]*Protocol{},
Visitor: func(cursor, parent C.CXCursor, context *VisitorContext) C.int {
if cursor.kind == 0 {
return 0
}
kind := C.clang_getCursorKind(cursor)
r := C.clang_getCursorExtent(cursor)
location := C.clang_getRangeStart(r)
var file C.CXFile
var line C.uint
var column C.uint
C.clang_getFileLocation(location, &file, &line, &column, nil)
cxFileName := C.clang_getFileName(file)
filename := C.GoString(C.clang_getCString(cxFileName))
C.clang_disposeString(cxFileName)
var framework string
if strings.HasPrefix(filename, p.frameworksPath) {
framework = strings.TrimSuffix(strings.Split(strings.TrimPrefix(filename, p.frameworksPath), "/")[1], ".framework")
} else {
framework = "objc"
}
loc := &Loc{
Framework: framework,
Filename: filename,
Line: uint(line),
Column: uint(column),
}
if loc.Framework != currentFramework {
if err := p.ReadFramework(loc.Framework); err != nil {
log.Println("error importing framework", loc.Framework+":", err)
}
}
hash := C.clang_hashCursor(cursor)
var parentHash C.uint
if parent.kind != 0 {
parentHash = C.clang_hashCursor(parent)
}
accessSpecifier := C.clang_getCXXAccessSpecifier(cursor)
if accessSpecifier != C.CX_CXXPublic && accessSpecifier != C.CX_CXXInvalidAccessSpecifier {
return C.CXChildVisit_Continue
}
if kind == C.CXCursor_TypedefDecl {
// cxCursorName := C.clang_getCursorDisplayName(cursor)
// cursorName := C.GoString(C.clang_getCString(cxCursorName))
// C.clang_disposeString(cxCursorName)
// cxType := C.clang_getTypedefDeclUnderlyingType(cursor)
return C.CXChildVisit_Recurse
} else if kind == C.CXCursor_StructDecl {
var cursorName string
var isAnonymous bool
if C.clang_Cursor_isAnonymousRecordDecl(cursor) != 0 {
isAnonymous = true
} else {
var cxCursorName C.CXString
if parent.kind == C.CXCursor_TypedefDecl {
cxCursorName = C.clang_getCursorDisplayName(parent)
} else {
cxCursorName = C.clang_getCursorDisplayName(cursor)
}
cursorName = C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
}
ccursor := C.clang_getCanonicalCursor(cursor)
var canonical bool
if C.clang_hashCursor(ccursor) == hash {
canonical = true
}
struct_ := &Struct{
Loc: loc,
Name: cursorName,
Canonical: canonical,
IsAnonymous: isAnonymous,
}
context.structs[uint(hash)] = struct_
return C.CXChildVisit_Recurse
} else if kind == C.CXCursor_ObjCInterfaceDecl {
cxCursorName := C.clang_getCursorDisplayName(cursor)
cursorName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
// hash := C.clang_hashCursor(cxTypeCursor)
for k, iface := range context.interfaces {
if iface.Name == cursorName {
delete(context.interfaces, k)
break
}
}
iface := &Interface{
Loc: loc,
Name: cursorName,
}
context.interfaces[uint(hash)] = iface
return C.CXChildVisit_Recurse
} else if kind == C.CXCursor_ObjCClassRef {
cxCursorName := C.clang_getCursorDisplayName(cursor)
cursorName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
seen := false
for _, iface := range context.interfaces {
if iface.Name == cursorName {
seen = true
break
}
}
if !seen {
iface := &Interface{
Loc: loc,
Name: cursorName,
ForwardDecl: true,
}
context.interfaces[uint(hash)] = iface
}
return C.CXChildVisit_Recurse
} else if kind == C.CXCursor_ObjCProtocolDecl {
cxCursorName := C.clang_getCursorDisplayName(cursor)
cursorName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
protocol := &Protocol{
Loc: loc,
Name: cursorName,
}
context.protocols[uint(hash)] = protocol
return C.CXChildVisit_Recurse
} else if context.phase == VisitorPhaseDetail {
if kind == C.CXCursor_ObjCProtocolRef {
cxCursorName := C.clang_getCursorDisplayName(cursor)
protocolName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
var protocol *Protocol
for _, p := range context.protocols {
if p.Name == protocolName {
protocol = p
}
}
iface := context.interfaces[uint(parentHash)]
if protocol != nil && iface != nil {
iface.Protocols = append(iface.Protocols, protocol)
}
} else if kind == C.CXCursor_ObjCSuperClassRef {
cxCursorType := C.clang_getCursorType(cursor)
cxSuperTypeSpelling := C.clang_getTypeSpelling(cxCursorType)
superType := C.GoString(C.clang_getCString(cxSuperTypeSpelling))
C.clang_disposeString(cxSuperTypeSpelling)
found := false
for _, iface := range context.interfaces {
if iface.Name == superType {
found = true
context.interfaces[uint(parentHash)].Super = iface
}
}
if !found {
panic("ERR: previous declaration for superclass interface not found")
}
} else if kind == C.CXCursor_ObjCInstanceMethodDecl {
cxCursorName := C.clang_getCursorDisplayName(cursor)
cursorName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
cxResultType := C.clang_getCursorResultType(cursor)
method := &Method{
Loc: loc,
Instance: true,
Name: cursorName,
Interface: context.interfaces[uint(parentHash)],
ReturnType: context.ResolveType(cxResultType),
}
context.methods[uint(hash)] = method
if iface, ok := context.interfaces[uint(parentHash)]; ok {
iface.Methods = append(iface.Methods, method)
} else if protocol, ok := context.protocols[uint(parentHash)]; ok {
protocol.Methods = append(protocol.Methods, method)
}
return C.CXChildVisit_Recurse
} else if kind == C.CXCursor_ObjCClassMethodDecl {
cxCursorName := C.clang_getCursorDisplayName(cursor)
cursorName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
cxResultType := C.clang_getCursorResultType(cursor)
method := &Method{
Loc: loc,
Instance: false,
Name: cursorName,
ReturnType: context.ResolveType(cxResultType),
}
if iface, ok := method.ReturnType.(*Interface); ok {
if err := p.ReadFramework(iface.Loc.Framework); err != nil {
log.Println(err)
}
} else if protocol, ok := method.ReturnType.(*Protocol); ok {
if err := p.ReadFramework(protocol.Loc.Framework); err != nil {
log.Println(err)
}
} else if st, ok := method.ReturnType.(*Struct); ok {
if err := p.ReadFramework(st.Loc.Framework); err != nil {
log.Println(err)
}
}
context.methods[uint(hash)] = method
if iface, ok := context.interfaces[uint(parentHash)]; ok {
iface.Methods = append(iface.Methods, method)
} else if protocol, ok := context.protocols[uint(parentHash)]; ok {
protocol.Methods = append(protocol.Methods, method)
}
return C.CXChildVisit_Recurse
} else if kind == C.CXCursor_ParmDecl {
cxCursorName := C.clang_getCursorDisplayName(cursor)
cursorName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
cxCursorType := C.clang_getCursorType(cursor)
parameter := &Parameter{
Loc: loc,
Name: cursorName,
Type: context.ResolveType(cxCursorType),
}
if iface, ok := parameter.Type.(*Interface); ok {
if err := p.ReadFramework(iface.Loc.Framework); err != nil {
log.Println(err)
}
} else if protocol, ok := parameter.Type.(*Protocol); ok {
if err := p.ReadFramework(protocol.Loc.Framework); err != nil {
log.Println(err)
}
} else if st, ok := parameter.Type.(*Struct); ok {
if err := p.ReadFramework(st.Loc.Framework); err != nil {
log.Println(err)
}
}
if method, ok := context.methods[uint(parentHash)]; ok {
method.Parameters = append(method.Parameters, parameter)
}
} else if kind == C.CXCursor_FieldDecl {
cxCursorName := C.clang_getCursorDisplayName(cursor)
cursorName := C.GoString(C.clang_getCString(cxCursorName))
C.clang_disposeString(cxCursorName)
cxCursorType := C.clang_getCursorType(cursor)
field := &Field{
Loc: loc,
Name: cursorName,
Type: context.ResolveType(cxCursorType),
}
if iface, ok := field.Type.(*Interface); ok {
if err := p.ReadFramework(iface.Loc.Framework); err != nil {
log.Println(err)
}
} else if protocol, ok := field.Type.(*Protocol); ok {
if err := p.ReadFramework(protocol.Loc.Framework); err != nil {
log.Println(err)
}
} else if st, ok := field.Type.(*Struct); ok {
if err := p.ReadFramework(st.Loc.Framework); err != nil {
log.Println(err)
}
}
if st, ok := context.structs[uint(parentHash)]; ok {
st.Fields = append(st.Fields, field)
}
}
}
return C.CXChildVisit_Continue
},
}
var pinner runtime.Pinner
pinner.Pin(context)
defer pinner.Unpin()
_cgoCheckPointer := func(interface{}, interface{}) {}
_cgoCheckPointer(nil, nil)
var index C.CXIndex
var translationUnit C.CXTranslationUnit
var cursor C.CXCursor
C.ParseHeader(target, isysroot, ch, unsafe.Pointer(&index), unsafe.Pointer(&translationUnit), unsafe.Pointer(&cursor))
context.phase = VisitorPhaseInitial
C.visitChildren(cursor, unsafe.Pointer(context))
context.phase = VisitorPhaseDetail
C.visitChildren(cursor, unsafe.Pointer(context))
C.clang_disposeTranslationUnit(translationUnit)
C.clang_disposeIndex(index)
seen := map[string]bool{}
for _, st := range context.structs {
if !st.Canonical || st.Loc.Framework != currentFramework {
continue
}
if seen[st.Name] {
panic(fmt.Sprintf("ERR: %s %s", "duplicate struct", st.Name))
}
seen[st.Name] = true
if err := p.WriteStruct(st); err != nil {
return err
}
}
seen = map[string]bool{}
for _, iface := range context.interfaces {
if seen[iface.Name] {
panic(fmt.Sprintf("ERR: %s %s", "duplicate interface", iface.Name))
}
seen[iface.Name] = true
if iface.ForwardDecl || iface.Loc.Framework != currentFramework {
continue
}
if err := p.WriteInterface(iface); err != nil {
return err
}
}
seen = map[string]bool{}
for _, protocol := range context.protocols {
if seen[protocol.Name] {
panic(fmt.Sprintf("ERR: %s %s", "duplicate protocol", protocol.Name))
}
seen[protocol.Name] = true
if protocol.Loc.Framework != currentFramework {