-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathtranslate_c.zig
3920 lines (3703 loc) · 119 KB
/
translate_c.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 builtin = @import("builtin");
const tests = @import("tests.zig");
// ********************************************************
// * *
// * DO NOT ADD NEW CASES HERE *
// * instead add a file to test/cases/translate_c *
// * *
// ********************************************************
pub fn addCases(cases: *tests.TranslateCContext) void {
const default_enum_type = if (builtin.abi == .msvc) "c_int" else "c_uint";
cases.add("do while with breaks",
\\void foo(int a) {
\\ do {
\\ if (a) break;
\\ } while (4);
\\ do {
\\ if (a) break;
\\ } while (0);
\\ do {
\\ if (a) break;
\\ } while (a);
\\ do {
\\ break;
\\ } while (3);
\\ do {
\\ break;
\\ } while (0);
\\ do {
\\ break;
\\ } while (a);
\\}
, &[_][]const u8{
\\pub export fn foo(arg_a: c_int) void {
\\ var a = arg_a;
\\ _ = &a;
\\ while (true) {
\\ if (a != 0) break;
\\ }
\\ while (true) {
\\ if (a != 0) break;
\\ if (!false) break;
\\ }
\\ while (true) {
\\ if (a != 0) break;
\\ if (!(a != 0)) break;
\\ }
\\ while (true) {
\\ break;
\\ }
\\ while (true) {
\\ break;
\\ }
\\ while (true) {
\\ break;
\\ }
\\}
});
cases.add("variables check for opaque demotion",
\\struct A {
\\ _Atomic int a;
\\} a;
\\int main(void) {
\\ struct A a;
\\}
, &[_][]const u8{
\\pub const struct_A = opaque {};
\\pub const a = @compileError("non-extern variable has opaque type");
,
\\pub extern fn main() c_int;
});
cases.add("unnamed child types of typedef receive typedef's name",
\\typedef enum {
\\ FooA,
\\ FooB,
\\} Foo;
\\typedef struct {
\\ int a, b;
\\} Bar;
, &[_][]const u8{
\\pub const FooA: c_int = 0;
\\pub const FooB: c_int = 1;
\\pub const Foo =
++ " " ++ default_enum_type ++
\\;
\\pub const Bar = extern struct {
\\ a: c_int = @import("std").mem.zeroes(c_int),
\\ b: c_int = @import("std").mem.zeroes(c_int),
\\};
});
cases.add("if as while stmt has semicolon",
\\void foo() {
\\ while (1) if (1) {
\\ int a = 1;
\\ } else {
\\ int b = 2;
\\ }
\\ if (1) if (1) {}
\\}
, &[_][]const u8{
\\pub export fn foo() void {
\\ while (true) if (true) {
\\ var a: c_int = 1;
\\ _ = &a;
\\ } else {
\\ var b: c_int = 2;
\\ _ = &b;
\\ };
\\ if (true) if (true) {};
\\}
});
cases.add("conditional operator cast to void",
\\int bar();
\\void foo() {
\\ int a;
\\ a ? a = 2 : bar();
\\}
, &[_][]const u8{
\\pub extern fn bar(...) c_int;
\\pub export fn foo() void {
\\ var a: c_int = undefined;
\\ _ = &a;
\\ if (a != 0) a = 2 else _ = bar();
\\}
});
cases.add("scoped typedef",
\\void foo() {
\\ typedef union {
\\ int A;
\\ int B;
\\ int C;
\\ } Foo;
\\ Foo a = {0};
\\ {
\\ typedef union {
\\ int A;
\\ int B;
\\ int C;
\\ } Foo;
\\ Foo a = {0};
\\ }
\\}
, &[_][]const u8{
\\pub export fn foo() void {
\\ const union_unnamed_1 = extern union {
\\ A: c_int,
\\ B: c_int,
\\ C: c_int,
\\ };
\\ _ = &union_unnamed_1;
\\ const Foo = union_unnamed_1;
\\ _ = &Foo;
\\ var a: Foo = Foo{
\\ .A = @as(c_int, 0),
\\ };
\\ _ = &a;
\\ {
\\ const union_unnamed_2 = extern union {
\\ A: c_int,
\\ B: c_int,
\\ C: c_int,
\\ };
\\ _ = &union_unnamed_2;
\\ const Foo_1 = union_unnamed_2;
\\ _ = &Foo_1;
\\ var a_2: Foo_1 = Foo_1{
\\ .A = @as(c_int, 0),
\\ };
\\ _ = &a_2;
\\ }
\\}
});
cases.add("use cast param as macro fn return type",
\\#include <stdint.h>
\\#define SYS_BASE_CACHED 0
\\#define MEM_PHYSICAL_TO_K0(x) (void*)((uint32_t)(x) + SYS_BASE_CACHED)
, &[_][]const u8{
\\pub inline fn MEM_PHYSICAL_TO_K0(x: anytype) ?*anyopaque {
\\ _ = &x;
\\ return @import("std").zig.c_translation.cast(?*anyopaque, @import("std").zig.c_translation.cast(u32, x) + SYS_BASE_CACHED);
\\}
});
cases.add("variadic function demoted to extern",
\\int foo(int bar, ...) {
\\ return 1;
\\}
, &[_][]const u8{
\\warning: TODO unable to translate variadic function, demoted to extern
\\pub extern fn foo(bar: c_int, ...) c_int;
});
cases.add("pointer to opaque demoted struct",
\\typedef struct {
\\ _Atomic int foo;
\\} Foo;
\\
\\typedef struct {
\\ Foo *bar;
\\} Bar;
, &[_][]const u8{
\\source.h:1:9: warning: struct demoted to opaque type - unable to translate type of field foo
\\pub const Foo = opaque {};
\\pub const Bar = extern struct {
\\ bar: ?*Foo = @import("std").mem.zeroes(?*Foo),
\\};
});
cases.add("macro expressions respect C operator precedence",
\\int *foo = 0;
\\#define FOO *((foo) + 2)
\\#define VALUE (1 + 2 * 3 + 4 * 5 + 6 << 7 | 8 == 9)
\\#define _AL_READ3BYTES(p) ((*(unsigned char *)(p)) \
\\ | (*((unsigned char *)(p) + 1) << 8) \
\\ | (*((unsigned char *)(p) + 2) << 16))
, &[_][]const u8{
\\pub inline fn FOO() @TypeOf((foo + @as(c_int, 2)).*) {
\\ return (foo + @as(c_int, 2)).*;
\\}
,
\\pub const VALUE = ((((@as(c_int, 1) + (@as(c_int, 2) * @as(c_int, 3))) + (@as(c_int, 4) * @as(c_int, 5))) + @as(c_int, 6)) << @as(c_int, 7)) | @intFromBool(@as(c_int, 8) == @as(c_int, 9));
,
\\pub inline fn _AL_READ3BYTES(p: anytype) @TypeOf((@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16))) {
\\ _ = &p;
\\ return (@import("std").zig.c_translation.cast([*c]u8, p).* | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 1)).* << @as(c_int, 8))) | ((@import("std").zig.c_translation.cast([*c]u8, p) + @as(c_int, 2)).* << @as(c_int, 16));
\\}
});
cases.add("static variable in block scope",
\\float bar;
\\int foo() {
\\ _Thread_local static int bar = 2;
\\}
, &[_][]const u8{
\\pub export var bar: f32 = @import("std").mem.zeroes(f32);
\\pub export fn foo() c_int {
\\ const bar_1 = struct {
\\ threadlocal var static: c_int = 2;
\\ };
\\ _ = &bar_1;
\\ return 0;
\\}
});
cases.add("missing return stmt",
\\int foo() {}
\\int bar() {
\\ int a = 2;
\\}
\\int baz() {
\\ return 0;
\\}
, &[_][]const u8{
\\pub export fn foo() c_int {
\\ return 0;
\\}
\\pub export fn bar() c_int {
\\ var a: c_int = 2;
\\ _ = &a;
\\ return 0;
\\}
\\pub export fn baz() c_int {
\\ return 0;
\\}
});
cases.add("alignof",
\\void main() {
\\ int a = _Alignof(int);
\\}
, &[_][]const u8{
\\pub export fn main() void {
\\ var a: c_int = @as(c_int, @bitCast(@as(c_uint, @truncate(@alignOf(c_int)))));
\\ _ = &a;
\\}
});
cases.add("initializer list macro",
\\typedef struct Color {
\\ unsigned char r;
\\ unsigned char g;
\\ unsigned char b;
\\ unsigned char a;
\\} Color;
\\#define CLITERAL(type) (type)
\\#define LIGHTGRAY CLITERAL(Color){ 200, 200, 200, 255 } // Light Gray
\\typedef struct boom_t
\\{
\\ int i1;
\\} boom_t;
\\#define FOO ((boom_t){1})
\\typedef struct { float x; } MyCStruct;
\\#define A(_x) (MyCStruct) { .x = (_x) }
\\#define B A(0.f)
, &[_][]const u8{
\\pub const struct_Color = extern struct {
\\ r: u8 = @import("std").mem.zeroes(u8),
\\ g: u8 = @import("std").mem.zeroes(u8),
\\ b: u8 = @import("std").mem.zeroes(u8),
\\ a: u8 = @import("std").mem.zeroes(u8),
\\};
\\pub const Color = struct_Color;
,
\\pub inline fn CLITERAL(@"type": anytype) @TypeOf(@"type") {
\\ _ = &@"type";
\\ return @"type";
\\}
,
\\pub const LIGHTGRAY = @import("std").mem.zeroInit(CLITERAL(Color), .{ @as(c_int, 200), @as(c_int, 200), @as(c_int, 200), @as(c_int, 255) });
,
\\pub const struct_boom_t = extern struct {
\\ i1: c_int = @import("std").mem.zeroes(c_int),
\\};
\\pub const boom_t = struct_boom_t;
,
\\pub const FOO = @import("std").mem.zeroInit(boom_t, .{@as(c_int, 1)});
,
\\pub const MyCStruct = extern struct {
\\ x: f32 = @import("std").mem.zeroes(f32),
\\};
,
\\pub inline fn A(_x: anytype) MyCStruct {
\\ _ = &_x;
\\ return @import("std").mem.zeroInit(MyCStruct, .{
\\ .x = _x,
\\ });
\\}
,
\\pub const B = A(@as(f32, 0));
});
cases.add("complex switch",
\\int main() {
\\ int i = 2;
\\ switch (i) {
\\ case 0: {
\\ case 2:{
\\ i += 2;}
\\ i += 1;
\\ }
\\ }
\\}
, &[_][]const u8{ // TODO properly translate this
\\source.h:5:13: warning: TODO complex switch
,
\\source.h:1:5: warning: unable to translate function, demoted to extern
\\pub extern fn main() c_int;
});
cases.add("correct semicolon after infixop",
\\#define _IO_ERR_SEEN 0
\\#define __ferror_unlocked_body(_fp) (((_fp)->_flags & _IO_ERR_SEEN) != 0)
, &[_][]const u8{
\\pub inline fn __ferror_unlocked_body(_fp: anytype) @TypeOf((_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0)) {
\\ _ = &_fp;
\\ return (_fp.*._flags & _IO_ERR_SEEN) != @as(c_int, 0);
\\}
});
cases.add("c booleans are just ints",
\\#define FOO(x) ((x >= 0) + (x >= 0))
\\#define BAR 1 && 2 > 4
, &[_][]const u8{
\\pub inline fn FOO(x: anytype) @TypeOf(@intFromBool(x >= @as(c_int, 0)) + @intFromBool(x >= @as(c_int, 0))) {
\\ _ = &x;
\\ return @intFromBool(x >= @as(c_int, 0)) + @intFromBool(x >= @as(c_int, 0));
\\}
,
\\pub const BAR = (@as(c_int, 1) != 0) and (@as(c_int, 2) > @as(c_int, 4));
});
cases.add("struct with flexible array",
\\struct foo { int x; int y[]; };
\\struct bar { int x; int y[0]; };
, &[_][]const u8{
\\pub const struct_foo = extern struct {
\\ x: c_int align(4) = @import("std").mem.zeroes(c_int),
\\ pub fn y(self: anytype) @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int) {
\\ const Intermediate = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), u8);
\\ const ReturnType = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int);
\\ return @as(ReturnType, @ptrCast(@alignCast(@as(Intermediate, @ptrCast(self)) + 4)));
\\ }
\\};
\\pub const struct_bar = extern struct {
\\ x: c_int align(4) = @import("std").mem.zeroes(c_int),
\\ pub fn y(self: anytype) @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int) {
\\ const Intermediate = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), u8);
\\ const ReturnType = @import("std").zig.c_translation.FlexibleArrayType(@TypeOf(self), c_int);
\\ return @as(ReturnType, @ptrCast(@alignCast(@as(Intermediate, @ptrCast(self)) + 4)));
\\ }
\\};
});
cases.add("nested loops without blocks",
\\void foo() {
\\ while (0) while (0) {}
\\ for (;;) while (0);
\\ for (;;) do {} while (0);
\\}
, &[_][]const u8{
\\pub export fn foo() void {
\\ while (false) while (false) {};
\\ while (true) while (false) {};
\\ while (true) while (true) {
\\ if (!false) break;
\\ };
\\}
});
cases.add("macro comma operator",
\\#define foo (foo, bar)
\\int baz(int x, int y) { return 0; }
\\#define bar(x) (&x, +3, 4 == 4, 5 * 6, baz(1, 2), 2 % 2, baz(1,2))
, &[_][]const u8{
\\pub const foo = blk_1: {
\\ _ = &foo;
\\ break :blk_1 bar;
\\};
,
\\pub inline fn bar(x: anytype) @TypeOf(baz(@as(c_int, 1), @as(c_int, 2))) {
\\ _ = &x;
\\ return blk_1: {
\\ _ = &x;
\\ _ = @as(c_int, 3);
\\ _ = @as(c_int, 4) == @as(c_int, 4);
\\ _ = @as(c_int, 5) * @as(c_int, 6);
\\ _ = baz(@as(c_int, 1), @as(c_int, 2));
\\ _ = @import("std").zig.c_translation.MacroArithmetic.rem(@as(c_int, 2), @as(c_int, 2));
\\ break :blk_1 baz(@as(c_int, 1), @as(c_int, 2));
\\ };
\\}
});
cases.add("macro keyword define",
\\#define foo 1
\\#define inline 2
, &[_][]const u8{
\\pub const foo = @as(c_int, 1);
,
\\pub const @"inline" = @as(c_int, 2);
});
cases.add("macro line continuation",
\\int BAR = 0;
\\#define FOO -\
\\BAR
, &[_][]const u8{
\\pub inline fn FOO() @TypeOf(-BAR) {
\\ return -BAR;
\\}
});
cases.add("struct with atomic field",
\\struct arcan_shmif_cont {
\\ struct arcan_shmif_page* addr;
\\};
\\struct arcan_shmif_page {
\\ volatile _Atomic int abufused[12];
\\};
, &[_][]const u8{
\\source.h:4:8: warning: struct demoted to opaque type - unable to translate type of field abufused
\\pub const struct_arcan_shmif_page = opaque {};
\\pub const struct_arcan_shmif_cont = extern struct {
\\ addr: ?*struct_arcan_shmif_page = @import("std").mem.zeroes(?*struct_arcan_shmif_page),
\\};
});
cases.add("function prototype translated as optional",
\\typedef void (*fnptr_ty)(void);
\\typedef __attribute__((cdecl)) void (*fnptr_attr_ty)(void);
\\struct foo {
\\ __attribute__((cdecl)) void (*foo)(void);
\\ void (*bar)(void);
\\ fnptr_ty baz;
\\ fnptr_attr_ty qux;
\\};
, &[_][]const u8{
\\pub const fnptr_ty = ?*const fn () callconv(.c) void;
\\pub const fnptr_attr_ty = ?*const fn () callconv(.c) void;
\\pub const struct_foo = extern struct {
\\ foo: ?*const fn () callconv(.c) void = @import("std").mem.zeroes(?*const fn () callconv(.c) void),
\\ bar: ?*const fn () callconv(.c) void = @import("std").mem.zeroes(?*const fn () callconv(.c) void),
\\ baz: fnptr_ty = @import("std").mem.zeroes(fnptr_ty),
\\ qux: fnptr_attr_ty = @import("std").mem.zeroes(fnptr_attr_ty),
\\};
});
cases.add("array initializer w/ typedef",
\\typedef unsigned char uuid_t[16];
\\static const uuid_t UUID_NULL __attribute__ ((unused)) = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
, &[_][]const u8{
\\pub const uuid_t = [16]u8;
\\pub const UUID_NULL: uuid_t = [16]u8{
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\ 0,
\\};
});
cases.add("#define hex literal with capital X",
\\#define VAL 0XF00D
, &[_][]const u8{
\\pub const VAL = @import("std").zig.c_translation.promoteIntLiteral(c_int, 0xF00D, .hex);
});
cases.add("anonymous struct & unions",
\\typedef struct {
\\ union {
\\ char x;
\\ struct { int y; };
\\ };
\\} outer;
\\void foo(outer *x) { x->y = x->x; }
, &[_][]const u8{
\\const struct_unnamed_2 = extern struct {
\\ y: c_int = @import("std").mem.zeroes(c_int),
\\};
\\const union_unnamed_1 = extern union {
\\ x: u8,
\\ unnamed_0: struct_unnamed_2,
\\};
\\pub const outer = extern struct {
\\ unnamed_0: union_unnamed_1 = @import("std").mem.zeroes(union_unnamed_1),
\\};
\\pub export fn foo(arg_x: [*c]outer) void {
\\ var x = arg_x;
\\ _ = &x;
\\ x.*.unnamed_0.unnamed_0.y = @as(c_int, @bitCast(@as(c_uint, x.*.unnamed_0.x)));
\\}
});
cases.add("struct initializer - simple",
\\typedef struct { int x; } foo;
\\struct {double x,y,z;} s0 = {1.2, 1.3};
\\struct {int sec,min,hour,day,mon,year;} s1 = {.day=31,12,2014,.sec=30,15,17};
\\struct {int x,y;} s2 = {.y = 2, .x=1};
\\foo s3 = { 123 };
, &[_][]const u8{
\\pub const foo = extern struct {
\\ x: c_int = @import("std").mem.zeroes(c_int),
\\};
\\const struct_unnamed_1 = extern struct {
\\ x: f64 = @import("std").mem.zeroes(f64),
\\ y: f64 = @import("std").mem.zeroes(f64),
\\ z: f64 = @import("std").mem.zeroes(f64),
\\};
\\pub export var s0: struct_unnamed_1 = struct_unnamed_1{
\\ .x = 1.2,
\\ .y = 1.3,
\\ .z = 0,
\\};
\\const struct_unnamed_2 = extern struct {
\\ sec: c_int = @import("std").mem.zeroes(c_int),
\\ min: c_int = @import("std").mem.zeroes(c_int),
\\ hour: c_int = @import("std").mem.zeroes(c_int),
\\ day: c_int = @import("std").mem.zeroes(c_int),
\\ mon: c_int = @import("std").mem.zeroes(c_int),
\\ year: c_int = @import("std").mem.zeroes(c_int),
\\};
\\pub export var s1: struct_unnamed_2 = struct_unnamed_2{
\\ .sec = @as(c_int, 30),
\\ .min = @as(c_int, 15),
\\ .hour = @as(c_int, 17),
\\ .day = @as(c_int, 31),
\\ .mon = @as(c_int, 12),
\\ .year = @as(c_int, 2014),
\\};
\\const struct_unnamed_3 = extern struct {
\\ x: c_int = @import("std").mem.zeroes(c_int),
\\ y: c_int = @import("std").mem.zeroes(c_int),
\\};
\\pub export var s2: struct_unnamed_3 = struct_unnamed_3{
\\ .x = @as(c_int, 1),
\\ .y = @as(c_int, 2),
\\};
\\pub export var s3: foo = foo{
\\ .x = @as(c_int, 123),
\\};
});
cases.add("simple ptrCast for casts between opaque types",
\\struct opaque;
\\struct opaque_2;
\\void function(struct opaque *opaque) {
\\ struct opaque_2 *cast = (struct opaque_2 *)opaque;
\\}
, &[_][]const u8{
\\pub const struct_opaque = opaque {};
\\pub const struct_opaque_2 = opaque {};
\\pub export fn function(arg_opaque_1: ?*struct_opaque) void {
\\ var opaque_1 = arg_opaque_1;
\\ _ = &opaque_1;
\\ var cast: ?*struct_opaque_2 = @as(?*struct_opaque_2, @ptrCast(opaque_1));
\\ _ = &cast;
\\}
});
cases.add("struct initializer - packed",
\\struct {int x,y,z;} __attribute__((packed)) s0 = {1, 2};
, &[_][]const u8{
\\const struct_unnamed_1 = extern struct {
\\ x: c_int align(1) = @import("std").mem.zeroes(c_int),
\\ y: c_int align(1) = @import("std").mem.zeroes(c_int),
\\ z: c_int align(1) = @import("std").mem.zeroes(c_int),
\\};
\\pub export var s0: struct_unnamed_1 = struct_unnamed_1{
\\ .x = @as(c_int, 1),
\\ .y = @as(c_int, 2),
\\ .z = 0,
\\};
});
cases.add("linksection() attribute",
\\// Use the "segment,section" format to make this test pass when
\\// targeting the mach-o binary format
\\__attribute__ ((__section__("NEAR,.data")))
\\extern char my_array[16];
\\__attribute__ ((__section__("NEAR,.data")))
\\void my_fn(void) { }
, &[_][]const u8{
\\pub extern var my_array: [16]u8 linksection("NEAR,.data");
\\pub export fn my_fn() linksection("NEAR,.data") void {}
});
cases.add("simple var decls",
\\void foo(void) {
\\ int a;
\\ char b = 123;
\\ const int c;
\\ const unsigned d = 440;
\\ int e = 10;
\\ unsigned int f = 10u;
\\}
, &[_][]const u8{
\\pub export fn foo() void {
\\ var a: c_int = undefined;
\\ _ = &a;
\\ var b: u8 = 123;
\\ _ = &b;
\\ const c: c_int = undefined;
\\ _ = &c;
\\ const d: c_uint = @as(c_uint, @bitCast(@as(c_int, 440)));
\\ _ = &d;
\\ var e: c_int = 10;
\\ _ = &e;
\\ var f: c_uint = 10;
\\ _ = &f;
\\}
});
cases.add("ignore result, explicit function arguments",
\\void foo(void) {
\\ int a;
\\ 1;
\\ "hey";
\\ 1 + 1;
\\ 1 - 1;
\\ a = 1;
\\}
, &[_][]const u8{
\\pub export fn foo() void {
\\ var a: c_int = undefined;
\\ _ = &a;
\\ _ = @as(c_int, 1);
\\ _ = "hey";
\\ _ = @as(c_int, 1) + @as(c_int, 1);
\\ _ = @as(c_int, 1) - @as(c_int, 1);
\\ a = 1;
\\}
});
cases.add("function with no prototype",
\\int foo() {
\\ return 5;
\\}
, &[_][]const u8{
\\pub export fn foo() c_int {
\\ return 5;
\\}
});
cases.add("variables",
\\extern int extern_var;
\\static const int int_var = 13;
\\int foo;
, &[_][]const u8{
\\pub extern var extern_var: c_int;
\\pub const int_var: c_int = 13;
\\pub export var foo: c_int = @import("std").mem.zeroes(c_int);
});
cases.add("const ptr initializer",
\\static const char *v0 = "0.0.0";
, &[_][]const u8{
\\pub var v0: [*c]const u8 = "0.0.0";
});
cases.add("static incomplete array inside function",
\\void foo(void) {
\\ static const char v2[] = "2.2.2";
\\}
, &[_][]const u8{
\\pub export fn foo() void {
\\ const v2 = struct {
\\ const static: [5:0]u8 = "2.2.2".*;
\\ };
\\ _ = &v2;
\\}
});
cases.add("simple function definition",
\\void foo(void) {}
\\static void bar(void) {}
, &[_][]const u8{
\\pub export fn foo() void {}
\\pub fn bar() callconv(.c) void {}
});
cases.add("typedef void",
\\typedef void Foo;
\\Foo fun(Foo *a);
, &[_][]const u8{
\\pub const Foo = anyopaque;
,
\\pub extern fn fun(a: ?*Foo) void;
});
cases.add("duplicate typedef",
\\typedef long foo;
\\typedef int bar;
\\typedef long foo;
\\typedef int baz;
, &[_][]const u8{
\\pub const foo = c_long;
\\pub const bar = c_int;
\\pub const baz = c_int;
});
cases.add("casting pointers to ints and ints to pointers",
\\void foo(void);
\\void bar(void) {
\\ void *func_ptr = foo;
\\ void (*typed_func_ptr)(void) = (void (*)(void)) (unsigned long) func_ptr;
\\}
, &[_][]const u8{
\\pub extern fn foo() void;
\\pub export fn bar() void {
\\ var func_ptr: ?*anyopaque = @as(?*anyopaque, @ptrCast(&foo));
\\ _ = &func_ptr;
\\ var typed_func_ptr: ?*const fn () callconv(.c) void = @as(?*const fn () callconv(.c) void, @ptrFromInt(@as(c_ulong, @intCast(@intFromPtr(func_ptr)))));
\\ _ = &typed_func_ptr;
\\}
});
cases.add("always_inline attribute",
\\__attribute__((always_inline)) int foo() {
\\ return 5;
\\}
, &[_][]const u8{
\\pub inline fn foo() c_int {
\\ return 5;
\\}
});
cases.add("add, sub, mul, div, rem",
\\int s() {
\\ int a, b, c;
\\ c = a + b;
\\ c = a - b;
\\ c = a * b;
\\ c = a / b;
\\ c = a % b;
\\}
\\unsigned u() {
\\ unsigned a, b, c;
\\ c = a + b;
\\ c = a - b;
\\ c = a * b;
\\ c = a / b;
\\ c = a % b;
\\}
, &[_][]const u8{
\\pub export fn s() c_int {
\\ var a: c_int = undefined;
\\ _ = &a;
\\ var b: c_int = undefined;
\\ _ = &b;
\\ var c: c_int = undefined;
\\ _ = &c;
\\ c = a + b;
\\ c = a - b;
\\ c = a * b;
\\ c = @divTrunc(a, b);
\\ c = @import("std").zig.c_translation.signedRemainder(a, b);
\\ return 0;
\\}
\\pub export fn u() c_uint {
\\ var a: c_uint = undefined;
\\ _ = &a;
\\ var b: c_uint = undefined;
\\ _ = &b;
\\ var c: c_uint = undefined;
\\ _ = &c;
\\ c = a +% b;
\\ c = a -% b;
\\ c = a *% b;
\\ c = a / b;
\\ c = a % b;
\\ return 0;
\\}
});
cases.add("typedef of function in struct field",
\\typedef void lws_callback_function(void);
\\struct Foo {
\\ void (*func)(void);
\\ lws_callback_function *callback_http;
\\};
, &[_][]const u8{
\\pub const lws_callback_function = fn () callconv(.c) void;
\\pub const struct_Foo = extern struct {
\\ func: ?*const fn () callconv(.c) void = @import("std").mem.zeroes(?*const fn () callconv(.c) void),
\\ callback_http: ?*const lws_callback_function = @import("std").mem.zeroes(?*const lws_callback_function),
\\};
});
cases.add("macro with left shift",
\\#define REDISMODULE_READ (1<<0)
, &[_][]const u8{
\\pub const REDISMODULE_READ = @as(c_int, 1) << @as(c_int, 0);
});
cases.add("macro with right shift",
\\#define FLASH_SIZE 0x200000UL /* 2 MB */
\\#define FLASH_BANK_SIZE (FLASH_SIZE >> 1) /* 1 MB */
, &[_][]const u8{
\\pub const FLASH_SIZE = @as(c_ulong, 0x200000);
,
\\pub const FLASH_BANK_SIZE = FLASH_SIZE >> @as(c_int, 1);
});
cases.add("self referential struct with function pointer",
\\struct Foo {
\\ void (*derp)(struct Foo *foo);
\\};
, &[_][]const u8{
\\pub const struct_Foo = extern struct {
\\ derp: ?*const fn ([*c]struct_Foo) callconv(.c) void = @import("std").mem.zeroes(?*const fn ([*c]struct_Foo) callconv(.c) void),
\\};
,
\\pub const Foo = struct_Foo;
});
cases.add("#define an unsigned integer literal",
\\#define CHANNEL_COUNT 24
, &[_][]const u8{
\\pub const CHANNEL_COUNT = @as(c_int, 24);
});
cases.add("#define referencing another #define",
\\#define THING2 THING1
\\#define THING1 1234
, &[_][]const u8{
\\pub const THING1 = @as(c_int, 1234);
,
\\pub const THING2 = THING1;
});
cases.add("#define string",
\\#define foo "a string"
, &[_][]const u8{
\\pub const foo = "a string";
});
cases.add("macro with parens around negative number",
\\#define LUA_GLOBALSINDEX (-10002)
, &[_][]const u8{
\\pub const LUA_GLOBALSINDEX = -@as(c_int, 10002);
});
cases.add(
"u integer suffix after 0 (zero) in macro definition",
"#define ZERO 0U",
&[_][]const u8{
"pub const ZERO = @as(c_uint, 0);",
},
);
cases.add(
"l integer suffix after 0 (zero) in macro definition",
"#define ZERO 0L",
&[_][]const u8{
"pub const ZERO = @as(c_long, 0);",
},
);
cases.add(
"ul integer suffix after 0 (zero) in macro definition",
"#define ZERO 0UL",
&[_][]const u8{
"pub const ZERO = @as(c_ulong, 0);",
},
);
cases.add(
"lu integer suffix after 0 (zero) in macro definition",
"#define ZERO 0LU",
&[_][]const u8{
"pub const ZERO = @as(c_ulong, 0);",
},
);
cases.add(
"ll integer suffix after 0 (zero) in macro definition",
"#define ZERO 0LL",
&[_][]const u8{
"pub const ZERO = @as(c_longlong, 0);",
},
);
cases.add(
"ull integer suffix after 0 (zero) in macro definition",
"#define ZERO 0ULL",
&[_][]const u8{
"pub const ZERO = @as(c_ulonglong, 0);",
},
);
cases.add(
"llu integer suffix after 0 (zero) in macro definition",
"#define ZERO 0LLU",
&[_][]const u8{
"pub const ZERO = @as(c_ulonglong, 0);",
},
);
cases.add(
"bitwise not on u-suffixed 0 (zero) in macro definition",
"#define NOT_ZERO (~0U)",
&[_][]const u8{
"pub const NOT_ZERO = ~@as(c_uint, 0);",
},
);
cases.add("float suffixes",
\\#define foo 3.14f
\\#define bar 16.e-2l
\\#define FOO 0.12345
\\#define BAR .12345
\\#define baz 1e1
\\#define BAZ 42e-3f
\\#define foobar -73.L
\\extern const float my_float = 1.0f;
\\extern const double my_double = 1.0;
\\extern const long double my_longdouble = 1.0l;
\\extern const long double my_extended_precision_longdouble = 1.0000000000000003l;
, &([_][]const u8{
"pub const foo = @as(f32, 3.14);",
"pub const bar = @as(c_longdouble, 16.e-2);",
"pub const FOO = @as(f64, 0.12345);",
"pub const BAR = @as(f64, 0.12345);",
"pub const baz = @as(f64, 1e1);",
"pub const BAZ = @as(f32, 42e-3);",
"pub const foobar = -@as(c_longdouble, 73);",
"pub export const my_float: f32 = 1.0;",
"pub export const my_double: f64 = 1.0;",
"pub export const my_longdouble: c_longdouble = 1.0;",
switch (@bitSizeOf(c_longdouble)) {
// TODO implement decimal format for f128 <https://github.com/ziglang/zig/issues/1181>
// (so that f80/f128 values not exactly representable as f64 can be emitted in decimal form)
80 => "pub export const my_extended_precision_longdouble: c_longdouble = 0x1.000000000000159ep0;",
128 => "pub export const my_extended_precision_longdouble: c_longdouble = 0x1.000000000000159e05f1e2674d21p0;",
else => "pub export const my_extended_precision_longdouble: c_longdouble = 1.0000000000000002;",
},
}));
cases.add("macro defines hexadecimal float",
\\#define FOO 0xf7p38