forked from hexops/mach-objc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.zig
2395 lines (2149 loc) · 93.9 KB
/
generator.zig
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
const std = @import("std");
const reg = @import("registry.zig");
const Container = reg.Container;
const Enum = reg.Enum;
const EnumValue = reg.EnumValue;
const Method = reg.Method;
const Param = reg.Param;
const Property = reg.Property;
const Registry = reg.Registry;
const Type = reg.Type;
const TypeParam = reg.TypeParam;
var registry: Registry = undefined;
// ------------------------------------------------------------------------------------------------
pub const ParseError = error{ UnexpectedCharacter, UnexpectedToken };
pub const Token = struct {
kind: Kind,
text: []const u8,
const Kind = enum {
int,
id,
dot,
comma,
lparen,
rparen,
lbracket,
rbracket,
less,
greater,
caret,
star,
quote,
hyphen,
colon,
semi_colon,
plus,
grave_accent,
single_quote,
kw_bool,
kw_char,
kw_class,
kw_const,
kw_double,
kw_float,
kw_id,
kw_imp,
kw_instancetype,
kw_int,
kw_int8_t,
kw_int16_t,
kw_int32_t,
kw_int64_t,
kw_kindof,
kw_long,
kw_nonnull,
kw_nullable,
kw_null_unspecified,
kw_nullable_result,
kw_short,
kw_sel,
kw_struct,
kw_unsigned,
kw_uint,
kw_uint8_t,
kw_uint16_t,
kw_uint32_t,
kw_uint64_t,
kw_void,
eof,
};
};
fn isdigit(c: u8) bool {
switch (c) {
'0'...'9' => return true,
else => return false,
}
}
fn isalnum(c: u8) bool {
switch (c) {
'a'...'z', 'A'...'Z', '0'...'9' => return true,
else => return false,
}
}
pub const Lexer = struct {
const Self = @This();
source: []const u8,
offset: usize = 0,
pub fn next(self: *Self) !Token {
while (true) {
const start = self.offset;
var c = self.peek();
switch (c) {
' ', '\t', '\n', '\r' => {
self.skip();
},
'0'...'9' => {
self.skip();
c = self.peek();
while (isdigit(c)) {
self.skip();
c = self.peek();
}
return self.token(Token.Kind.int, start);
},
'a'...'z', 'A'...'Z', '_' => {
self.skip();
c = self.peek();
while (isalnum(c) or c == '_') {
self.skip();
c = self.peek();
}
const text = self.source[start..self.offset];
const kind = if (std.mem.eql(u8, text, "BOOL"))
Token.Kind.kw_bool
else if (std.mem.eql(u8, text, "char"))
Token.Kind.kw_char
else if (std.mem.eql(u8, text, "Class"))
Token.Kind.kw_class
else if (std.mem.eql(u8, text, "const"))
Token.Kind.kw_const
else if (std.mem.eql(u8, text, "double"))
Token.Kind.kw_double
else if (std.mem.eql(u8, text, "float"))
Token.Kind.kw_float
else if (std.mem.eql(u8, text, "id"))
Token.Kind.kw_id
else if (std.mem.eql(u8, text, "IMP"))
Token.Kind.kw_imp
else if (std.mem.eql(u8, text, "instancetype"))
Token.Kind.kw_instancetype
else if (std.mem.eql(u8, text, "int"))
Token.Kind.kw_int
else if (std.mem.eql(u8, text, "int8_t"))
Token.Kind.kw_int8_t
else if (std.mem.eql(u8, text, "int16_t"))
Token.Kind.kw_int16_t
else if (std.mem.eql(u8, text, "int32_t"))
Token.Kind.kw_int32_t
else if (std.mem.eql(u8, text, "int64_t"))
Token.Kind.kw_int64_t
else if (std.mem.eql(u8, text, "__kindof"))
Token.Kind.kw_kindof
else if (std.mem.eql(u8, text, "long"))
Token.Kind.kw_long
else if (std.mem.eql(u8, text, "_Nonnull"))
Token.Kind.kw_nonnull
else if (std.mem.eql(u8, text, "_Nullable"))
Token.Kind.kw_nullable
else if (std.mem.eql(u8, text, "_Null_unspecified"))
Token.Kind.kw_null_unspecified
else if (std.mem.eql(u8, text, "_Nullable_result"))
Token.Kind.kw_nullable_result
else if (std.mem.eql(u8, text, "SEL"))
Token.Kind.kw_sel
else if (std.mem.eql(u8, text, "short"))
Token.Kind.kw_short
else if (std.mem.eql(u8, text, "struct"))
Token.Kind.kw_struct
else if (std.mem.eql(u8, text, "uint8_t"))
Token.Kind.kw_uint8_t
else if (std.mem.eql(u8, text, "uint16_t"))
Token.Kind.kw_uint16_t
else if (std.mem.eql(u8, text, "uint32_t"))
Token.Kind.kw_uint32_t
else if (std.mem.eql(u8, text, "uint64_t"))
Token.Kind.kw_uint64_t
else if (std.mem.eql(u8, text, "unsigned"))
Token.Kind.kw_unsigned
else if (std.mem.eql(u8, text, "void"))
Token.Kind.kw_void
else
Token.Kind.id;
return self.token(kind, start);
},
'(' => {
self.skip();
return self.token(Token.Kind.lparen, start);
},
')' => {
self.skip();
return self.token(Token.Kind.rparen, start);
},
'[' => {
self.skip();
return self.token(Token.Kind.lbracket, start);
},
']' => {
self.skip();
return self.token(Token.Kind.rbracket, start);
},
'<' => {
self.skip();
return self.token(Token.Kind.less, start);
},
'>' => {
self.skip();
return self.token(Token.Kind.greater, start);
},
'^' => {
self.skip();
return self.token(Token.Kind.caret, start);
},
'*' => {
self.skip();
return self.token(Token.Kind.star, start);
},
'"' => {
self.skip();
return self.token(Token.Kind.quote, start);
},
'-' => {
self.skip();
return self.token(Token.Kind.hyphen, start);
},
':' => {
self.skip();
return self.token(Token.Kind.colon, start);
},
';' => {
self.skip();
return self.token(Token.Kind.semi_colon, start);
},
'+' => {
self.skip();
return self.token(Token.Kind.plus, start);
},
'`' => {
self.skip();
return self.token(Token.Kind.grave_accent, start);
},
'\'' => {
self.skip();
return self.token(Token.Kind.single_quote, start);
},
',' => {
self.skip();
return self.token(Token.Kind.comma, start);
},
'.' => {
self.skip();
return self.token(Token.Kind.dot, start);
},
0 => {
return self.token(Token.Kind.eof, start);
},
else => {
std.debug.print("Unexpected character {c} {}\n", .{ c, c });
std.debug.print("Parsing {s}\n", .{self.source});
return error.UnexpectedCharacter;
},
}
}
}
fn skip(self: *Self) void {
self.offset += 1;
}
fn peek(self: *Self) u8 {
if (self.offset < self.source.len) {
return self.source[self.offset];
} else {
return 0;
}
}
fn token(self: *Self, kind: Token.Kind, start: usize) Token {
return Token{ .kind = kind, .text = self.source[start..self.offset] };
}
};
pub const Parser = struct {
const Self = @This();
const PointerProps = struct { is_const: bool, is_optional: bool };
allocator: std.mem.Allocator,
lookahead: Token,
lexer: *Lexer,
pub fn init(allocator: std.mem.Allocator, lexer: *Lexer) !Parser {
return Parser{ .allocator = allocator, .lookahead = try lexer.next(), .lexer = lexer };
}
pub fn parseType(self: *Self) !Type {
if (self.lookahead.kind == .id) {
const text = self.lookahead.text;
if (std.mem.eql(u8, text, "API_AVAILABLE")) {
try self.match(.id);
try self.skipParenContent();
} else if (std.mem.eql(u8, text, "API_UNAVAILABLE")) {
try self.match(.id);
try self.skipParenContent();
} else if (std.mem.eql(u8, text, "API_DEPRECATED")) {
try self.match(.id);
try self.skipParenContent();
} else if (std.mem.eql(u8, text, "API_DEPRECATED_WITH_REPLACEMENT")) {
try self.match(.id);
try self.skipParenContent();
} else if (std.mem.eql(u8, text, "NS_AVAILABLE")) {
try self.match(.id);
try self.skipParenContent();
} else if (std.mem.eql(u8, text, "NS_AVAILABLE_MAC")) {
try self.match(.id);
try self.skipParenContent();
} else if (std.mem.eql(u8, text, "NS_SWIFT_UNAVAILABLE")) {
try self.match(.id);
try self.skipParenContent();
} else if (std.mem.eql(u8, text, "NS_SWIFT_UNAVAILABLE_FROM_ASYNC")) {
try self.match(.id);
try self.skipParenContent();
}
}
const is_const = self.lookahead.kind == .kw_const;
if (is_const)
try self.match(.kw_const);
// TODO - what does this mean?
if (self.lookahead.kind == .kw_kindof)
try self.match(.kw_kindof);
switch (self.lookahead.kind) {
.kw_void => {
try self.match(.kw_void);
return self.parseTypeSuffix(.{ .void = {} }, is_const, false);
},
.kw_bool => {
try self.match(.kw_bool);
return self.parseTypeSuffix(.{ .bool = {} }, is_const, false);
},
.kw_char => {
try self.match(.kw_char);
return self.parseTypeSuffix(.{ .uint = 8 }, is_const, false);
},
.kw_short => {
try self.match(.kw_short);
return self.parseTypeSuffix(.{ .c_short = {} }, is_const, false);
},
.kw_int => {
try self.match(.kw_int);
return self.parseTypeSuffix(.{ .c_int = {} }, is_const, false);
},
.kw_long => {
try self.match(.kw_long);
if (self.lookahead.kind == .kw_long) {
try self.match(.kw_long);
return self.parseTypeSuffix(.{ .c_longlong = {} }, is_const, false);
} else {
return self.parseTypeSuffix(.{ .c_long = {} }, is_const, false);
}
},
.kw_unsigned => {
try self.match(.kw_unsigned);
switch (self.lookahead.kind) {
.kw_char => {
try self.match(.kw_char);
return self.parseTypeSuffix(.{ .uint = 8 }, is_const, false);
},
.kw_short => {
try self.match(.kw_short);
return self.parseTypeSuffix(.{ .c_ushort = {} }, is_const, false);
},
.kw_int => {
try self.match(.kw_int);
return self.parseTypeSuffix(.{ .c_uint = {} }, is_const, false);
},
.kw_long => {
try self.match(.kw_long);
if (self.lookahead.kind == .kw_long) {
try self.match(.kw_long);
return self.parseTypeSuffix(.{ .c_ulonglong = {} }, is_const, false);
} else {
return self.parseTypeSuffix(.{ .c_ulong = {} }, is_const, false);
}
},
else => {
return self.parseTypeSuffix(.{ .c_uint = {} }, is_const, false);
},
}
},
.kw_int8_t => {
try self.match(.kw_int8_t);
return self.parseTypeSuffix(.{ .int = 8 }, is_const, false);
},
.kw_int16_t => {
try self.match(.kw_int16_t);
return self.parseTypeSuffix(.{ .int = 16 }, is_const, false);
},
.kw_int32_t => {
try self.match(.kw_int32_t);
return self.parseTypeSuffix(.{ .int = 32 }, is_const, false);
},
.kw_int64_t => {
try self.match(.kw_int64_t);
return self.parseTypeSuffix(.{ .int = 64 }, is_const, false);
},
.kw_uint => {
try self.match(.kw_uint);
return self.parseTypeSuffix(.{ .uint = 32 }, is_const, false);
},
.kw_uint8_t => {
try self.match(.kw_uint8_t);
return self.parseTypeSuffix(.{ .uint = 8 }, is_const, false);
},
.kw_uint16_t => {
try self.match(.kw_uint16_t);
return self.parseTypeSuffix(.{ .uint = 16 }, is_const, false);
},
.kw_uint32_t => {
try self.match(.kw_uint32_t);
return self.parseTypeSuffix(.{ .uint = 32 }, is_const, false);
},
.kw_uint64_t => {
try self.match(.kw_uint64_t);
return self.parseTypeSuffix(.{ .uint = 64 }, is_const, false);
},
.kw_float => {
try self.match(.kw_float);
return self.parseTypeSuffix(.{ .float = 32 }, is_const, false);
},
.kw_double => {
try self.match(.kw_double);
return self.parseTypeSuffix(.{ .float = 64 }, is_const, false);
},
.kw_class => {
try self.match(.kw_class);
const props = try self.parsePointerProps(is_const);
const t = Type{ .name = "c.objc_class" };
const child = try self.allocator.create(Type);
child.* = t;
return Type{ .pointer = .{
.is_single = true,
.is_const = props.is_const,
.is_optional = props.is_optional,
.child = child,
} };
},
.kw_sel => {
try self.match(.kw_sel);
const t = Type{ .name = "c.objc_selector" };
const child = try self.allocator.create(Type);
child.* = t;
return Type{ .pointer = .{ .is_single = true, .is_const = is_const, .is_optional = false, .child = child } };
},
.kw_id => {
try self.match(.kw_id);
if (self.lookahead.kind == .less) {
try self.match(.less);
const types = try self.parseTypeList();
try self.match(.greater);
// TODO - how should handle multiple types?
return self.parsePointerSuffix(types.items[0], is_const, true);
} else {
const t = Type{ .name = "c.objc_object" };
return self.parsePointerSuffix(t, is_const, true);
}
},
.kw_imp => {
try self.match(.kw_imp);
//void (*)(void)
const return_type = try self.allocator.create(Type);
return_type.* = .{ .void = {} };
const ty = .{
.function = .{
.return_type = return_type,
.params = std.ArrayList(Type).init(self.allocator),
},
};
return self.parseTypeSuffix(ty, is_const, true);
},
.kw_instancetype => {
try self.match(.kw_instancetype);
const child = try self.allocator.create(Type);
child.* = .{ .instance_type = {} };
return Type{ .pointer = .{ .is_single = true, .is_const = is_const, .is_optional = false, .child = child } };
},
.kw_struct => {
try self.match(.kw_struct);
const name = self.lookahead.text;
try self.match(.id);
return self.parseTypeSuffix(.{ .name = name }, is_const, false);
},
else => {
const text = self.lookahead.text;
try self.match(.id);
return self.parseTypeSuffix(.{ .name = text }, is_const, false);
},
}
}
fn parseTypeList(self: *Self) (ParseError || error{OutOfMemory})!std.ArrayList(Type) {
var types = std.ArrayList(Type).init(self.allocator);
try types.append(try self.parseType());
while (self.lookahead.kind == .comma) {
try self.match(.comma);
try types.append(try self.parseType());
}
return types;
}
fn parseTypeSuffix(self: *Self, base_type: Type, is_const: bool, is_single: bool) !Type {
if (self.lookahead.kind == .star) {
try self.match(.star);
return self.parsePointerSuffix(base_type, is_const, is_single);
} else if (self.lookahead.kind == .lbracket) {
// TODO - handle arrays
try self.match(.lbracket);
if (self.lookahead.kind == .int)
try self.match(.int);
try self.match(.rbracket);
return self.parsePointerSuffix(base_type, is_const, is_single);
} else if (self.lookahead.kind == .lparen) {
try self.match(.lparen);
if (self.lookahead.kind == .star) {
try self.match(.star);
const props = try self.parsePointerProps(is_const);
_ = props;
try self.match(.rparen);
try self.match(.lparen);
_ = try self.parseTypeList();
try self.match(.rparen);
} else if (self.lookahead.kind == .caret) {
try self.match(.caret);
const props = try self.parsePointerProps(is_const);
_ = props;
try self.match(.rparen);
try self.match(.lparen);
const params = try self.parseTypeList();
try self.match(.rparen);
const return_type = try self.allocator.create(Type);
return_type.* = base_type;
return .{
.function = .{
.return_type = return_type,
.params = params,
},
};
} else {
_ = try self.parseTypeList();
try self.match(.rparen);
}
return base_type;
} else if (self.lookahead.kind == .less) {
try self.match(.less);
const types = try self.parseTypeList();
try self.match(.greater);
const child = try self.allocator.create(Type);
child.* = base_type;
return self.parseTypeSuffix(.{ .generic = .{ .base_type = child, .args = types } }, is_const, true);
} else {
const props = try self.parsePointerProps(false);
_ = props;
return base_type;
}
}
fn parsePointerProps(self: *Self, elem_is_const: bool) !PointerProps {
var is_const = elem_is_const;
var is_optional = false;
while (true) {
if (self.lookahead.kind == .kw_const) {
try self.match(.kw_const);
is_const = true;
} else if (self.lookahead.kind == .kw_nullable) {
try self.match(.kw_nullable);
is_optional = true;
} else if (self.lookahead.kind == .kw_nonnull) {
try self.match(.kw_nonnull);
} else if (self.lookahead.kind == .kw_null_unspecified) {
try self.match(.kw_null_unspecified);
} else if (self.lookahead.kind == .kw_nullable_result) {
try self.match(.kw_nullable_result);
} else break;
}
return .{ .is_const = is_const, .is_optional = is_optional };
}
fn parsePointerSuffix(self: *Self, base_type: Type, is_const: bool, is_single: bool) error{
OutOfMemory,
UnexpectedToken,
UnexpectedCharacter,
}!Type {
const props = try self.parsePointerProps(is_const);
const child = try self.allocator.create(Type);
child.* = base_type;
const t = Type{ .pointer = .{
.is_single = is_single,
.is_const = props.is_const,
.is_optional = props.is_optional,
.child = child,
} };
return self.parseTypeSuffix(t, false, is_single);
}
fn skipParenContent(self: *Self) !void {
var nestLevel: u32 = 0;
while (true) {
if (self.lookahead.kind == .lparen) {
try self.match(.lparen);
nestLevel = nestLevel + 1;
} else if (self.lookahead.kind == .rparen) {
try self.match(.rparen);
nestLevel = nestLevel - 1;
} else {
self.lookahead = try self.lexer.next();
}
if (nestLevel == 0)
break;
}
}
fn match(self: *Self, k: Token.Kind) !void {
if (self.lookahead.kind == k) {
self.lookahead = try self.lexer.next();
} else {
std.debug.print("Expected {any} but found {any}\n", .{ k, self.lookahead.kind });
return error.UnexpectedToken;
}
}
};
// ------------------------------------------------------------------------------------------------
pub fn getObject(x: std.json.Value, key: []const u8) ?std.json.Value {
switch (x) {
.object => |o| {
if (o.get(key)) |v| {
switch (v) {
.object => return v,
else => return null,
}
} else {
return null;
}
},
else => return null,
}
}
pub fn getArray(x: std.json.Value, key: []const u8) []std.json.Value {
switch (x) {
.object => |o| {
if (o.get(key)) |v| {
switch (v) {
.array => |a| {
return a.items;
},
else => return &[_]std.json.Value{},
}
} else {
return &[_]std.json.Value{};
}
},
else => return &[_]std.json.Value{},
}
}
pub fn getString(x: std.json.Value, key: []const u8) []const u8 {
switch (x) {
.object => |o| {
if (o.get(key)) |v| {
switch (v) {
.string => |s| {
return s;
},
else => return "",
}
} else {
return "";
}
},
else => return "",
}
}
pub fn getBool(x: std.json.Value, key: []const u8) bool {
switch (x) {
.object => |o| {
if (o.get(key)) |v| {
switch (v) {
.bool => |b| {
return b;
},
else => return false,
}
} else {
return false;
}
},
else => return false,
}
}
pub const Converter = struct {
const Self = @This();
allocator: std.mem.Allocator,
arena: std.heap.ArenaAllocator,
pub fn init(allocator: std.mem.Allocator) Converter {
return Converter{ .allocator = allocator, .arena = std.heap.ArenaAllocator.init(allocator) };
}
pub fn deinit(self: *Self) void {
self.arena.deinit();
}
pub fn convert(self: *Self, n: std.json.Value) !void {
const kind = getString(n, "kind");
if (std.mem.eql(u8, kind, "TranslationUnitDecl")) {
try self.convertTranslationUnitDecl(n);
}
}
fn convertTranslationUnitDecl(self: *Self, n: std.json.Value) !void {
for (getArray(n, "inner")) |child| {
const childKind = getString(child, "kind");
if (std.mem.eql(u8, childKind, "EnumDecl")) {
try self.convertEnumDecl(child);
} else if (std.mem.eql(u8, childKind, "FunctionDecl")) {
self.convertFunctionDecl(child);
} else if (std.mem.eql(u8, childKind, "ObjCCategoryDecl")) {
try self.convertObjCCategoryDecl(child);
} else if (std.mem.eql(u8, childKind, "ObjCInterfaceDecl")) {
try self.convertObjCInterfaceDecl(child);
} else if (std.mem.eql(u8, childKind, "ObjCProtocolDecl")) {
try self.convertObjcProtocolDecl(child);
} else if (std.mem.eql(u8, childKind, "RecordDecl")) {
self.convertRecordDecl(child);
} else if (std.mem.eql(u8, childKind, "TypedefDecl")) {
try self.convertTypedefDecl(child);
} else if (std.mem.eql(u8, childKind, "VarDecl")) {
self.convertVarDecl(child);
}
}
}
fn convertEnumDecl(self: *Self, n: std.json.Value) !void {
const name = getString(n, "name");
if (name.len == 0) {
return;
}
var e = try registry.getEnum(name);
if (getObject(n, "fixedUnderlyingType")) |ty| {
e.ty = try self.convertType(ty);
} else {
e.ty = .{ .int = 32 };
}
for (getArray(n, "inner")) |child| {
const childKind = getString(child, "kind");
if (std.mem.eql(u8, childKind, "EnumConstantDecl")) {
const v = try self.convertEnumConstantDecl(child);
try e.values.append(v);
}
}
}
fn convertEnumConstantDecl(self: *Self, n: std.json.Value) !EnumValue {
var value: i64 = 0;
for (getArray(n, "inner")) |child| {
const childKind = getString(child, "kind");
if (std.mem.eql(u8, childKind, "ConstantExpr")) {
value = self.convertConstantExpr(child);
} else if (std.mem.eql(u8, childKind, "ImplicitCastExpr")) {
value = self.convertImplicitCastExpr(child);
}
}
return .{ .name = getString(n, "name"), .value = value };
}
fn convertConstantExpr(_: *Self, n: std.json.Value) i64 {
const value = getString(n, "value");
return std.fmt.parseInt(i64, value, 10) catch 0;
}
fn convertImplicitCastExpr(self: *Self, n: std.json.Value) i64 {
for (getArray(n, "inner")) |child| {
const childKind = getString(child, "kind");
if (std.mem.eql(u8, childKind, "ConstantExpr")) {
return self.convertConstantExpr(child);
}
}
return 0;
}
fn convertFunctionDecl(self: *Self, n: std.json.Value) void {
_ = self;
_ = n;
}
fn convertObjCCategoryDecl(self: *Self, n: std.json.Value) !void {
const interfaceDecl = getObject(n, "interface").?;
const container = try registry.getInterface(getString(interfaceDecl, "name"));
try self.convertContainer(container, n);
}
fn convertObjCInterfaceDecl(self: *Self, n: std.json.Value) !void {
var container = try registry.getInterface(getString(n, "name"));
if (getObject(n, "super")) |super| {
const superName = getString(super, "name");
if (superName.len > 0) {
container.super = try registry.getInterface(superName);
}
}
try self.convertContainer(container, n);
}
fn convertObjcProtocolDecl(self: *Self, n: std.json.Value) !void {
var container = try registry.getProtocol(getString(n, "name"));
if (getObject(n, "super")) |super| {
const superName = getString(super, "name");
if (superName.len > 0) {
container.super = try registry.getProtocol(superName);
}
}
try self.convertContainer(container, n);
}
fn convertRecordDecl(self: *Self, n: std.json.Value) void {
_ = self;
_ = n;
}
fn convertTypedefDecl(self: *Self, n: std.json.Value) !void {
const name = getString(n, "name");
const ty = try self.convertType(getObject(n, "type").?);
try registry.typedefs.put(name, ty);
}
fn convertVarDecl(self: *Self, n: std.json.Value) void {
_ = self;
_ = n;
}
fn convertContainer(self: *Self, container: *Container, n: std.json.Value) !void {
// TODO - better solution for this?
container.type_params.clearAndFree();
container.protocols.clearAndFree();
for (getArray(n, "protocols")) |protocolJson| {
const protocolName = getString(protocolJson, "name");
const protocol = try registry.getProtocol(protocolName);
try container.protocols.append(protocol);
}
for (getArray(n, "inner")) |child| {
const childKind = getString(child, "kind");
if (std.mem.eql(u8, childKind, "ObjCTypeParamDecl")) {
const type_param = try self.convertTypeParam(child);
try container.type_params.append(type_param);
} else if (std.mem.eql(u8, childKind, "ObjCPropertyDecl")) {
const property = try self.convertProperty(child);
try container.properties.append(property);
} else if (std.mem.eql(u8, childKind, "ObjCMethodDecl")) {
const method = try self.convertMethod(child);
try container.methods.append(method);
}
}
}
fn convertTypeParam(self: *Self, n: std.json.Value) !TypeParam {
_ = self;
return TypeParam.init(getString(n, "name"));
}
fn convertProperty(self: *Self, n: std.json.Value) !Property {
const ty = try self.convertType(getObject(n, "type").?);
return Property.init(getString(n, "name"), ty);
}
fn convertMethod(self: *Self, n: std.json.Value) !Method {
const return_type = try self.convertType(getObject(n, "returnType").?);
var params = std.ArrayList(Param).init(registry.allocator);
for (getArray(n, "inner")) |child| {
const childKind = getString(child, "kind");
if (std.mem.eql(u8, childKind, "ParmVarDecl")) {
const param = try self.convertParam(child);
try params.append(param);
}
}
return Method.init(getString(n, "name"), getBool(n, "instance"), return_type, params);
}
fn convertParam(self: *Self, n: std.json.Value) !Param {
const ty = try self.convertType(getObject(n, "type").?);
return Param.init(getString(n, "name"), ty);
}
fn convertType(self: *Self, t: std.json.Value) !Type {
//std.debug.print("convertType: {s}\n", .{t.qualType});
var lexer = Lexer{ .source = getString(t, "qualType") };
var parser = try Parser.init(self.arena.allocator(), &lexer);
return parser.parseType();
}
};
// ------------------------------------------------------------------------------------------------
const prefixes = [_][]const u8{ "CA", "CF", "CG", "MTK", "MTL", "NS" };
pub fn getNamespace(id: []const u8) []const u8 {
for (prefixes) |prefix| {
if (std.mem.startsWith(u8, id, prefix)) {
return prefix;
}
}
return &[_]u8{};
}
pub fn trimNamespace(id: []const u8) []const u8 {
for (prefixes) |prefix| {
if (std.mem.startsWith(u8, id, prefix)) {
return id[prefix.len..];
}
}
return id;
}
pub fn trimTrailingColon(id: []const u8) []const u8 {
if (id.len == 0) {
return id;
} else if (id[id.len - 1] == ':') {
return id[0 .. id.len - 1];
} else {
return id;
}
}
fn isKeyword(id: []const u8) bool {
if (std.mem.eql(u8, id, "error")) {
return true;
} else if (std.mem.eql(u8, id, "opaque")) {
return true;
} else if (std.mem.eql(u8, id, "type")) {
return true;
} else if (std.mem.eql(u8, id, "resume")) {
return true;
} else {
return false;
}
}
fn Generator(comptime WriterType: type) type {
return struct {
const Self = @This();
const WriteError = WriterType.Error;
const EnumList = std.ArrayList(*reg.Enum);
const ContainerList = std.ArrayList(*reg.Container);
const SelectorHashSet = std.StringHashMap(void);