forked from ziglang/zig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_h.zig
148 lines (140 loc) · 3.15 KB
/
gen_h.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
const tests = @import("tests.zig");
pub fn addCases(cases: *tests.GenHContext) void {
cases.add("declare enum",
\\const Foo = extern enum { A, B, C };
\\export fn entry(foo: Foo) void { }
, &[_][]const u8{
\\enum Foo {
\\ A = 0,
\\ B = 1,
\\ C = 2
\\};
,
\\void entry(enum Foo foo);
});
cases.add("declare struct",
\\const Foo = extern struct {
\\ A: i32,
\\ B: f32,
\\ C: bool,
\\ D: u64,
\\ E: u64,
\\ F: u64,
\\};
\\export fn entry(foo: Foo) void { }
, &[_][]const u8{
\\struct Foo {
\\ int32_t A;
\\ float B;
\\ bool C;
\\ uint64_t D;
\\ uint64_t E;
\\ uint64_t F;
\\};
,
\\void entry(struct Foo foo);
\\
});
cases.add("declare union",
\\const Big = extern struct {
\\ A: u64,
\\ B: u64,
\\ C: u64,
\\ D: u64,
\\ E: u64,
\\};
\\const Foo = extern union {
\\ A: i32,
\\ B: f32,
\\ C: bool,
\\ D: Big,
\\};
\\export fn entry(foo: Foo) void {}
, &[_][]const u8{
\\struct Big {
\\ uint64_t A;
\\ uint64_t B;
\\ uint64_t C;
\\ uint64_t D;
\\ uint64_t E;
\\};
\\
\\union Foo {
\\ int32_t A;
\\ float B;
\\ bool C;
\\ struct Big D;
\\};
,
\\void entry(union Foo foo);
\\
});
cases.add("declare opaque type",
\\const Foo = opaque {};
\\
\\export fn entry(foo: ?*Foo) void { }
, &[_][]const u8{
\\struct Foo;
,
\\void entry(struct Foo * foo);
});
cases.add("array field-type",
\\const Foo = extern struct {
\\ A: [2]i32,
\\ B: [4]*u32,
\\};
\\export fn entry(foo: Foo, bar: [3]u8) void { }
, &[_][]const u8{
\\struct Foo {
\\ int32_t A[2];
\\ uint32_t * B[4];
\\};
,
\\void entry(struct Foo foo, uint8_t bar[]);
\\
});
cases.add("ptr to zig struct",
\\const S = struct {
\\ a: u8,
\\};
\\
\\export fn a(s: *S) u8 {
\\ return s.a;
\\}
, &[_][]const u8{
\\struct S;
,
\\uint8_t a(struct S * s);
\\
});
cases.add("ptr to zig union",
\\const U = union(enum) {
\\ A: u8,
\\ B: u16,
\\};
\\
\\export fn a(s: *U) u8 {
\\ return s.A;
\\}
, &[_][]const u8{
\\union U;
,
\\uint8_t a(union U * s);
\\
});
cases.add("ptr to zig enum",
\\const E = enum(u8) {
\\ A,
\\ B,
\\};
\\
\\export fn a(s: *E) u8 {
\\ return @intFromEnum(s.*);
\\}
, &[_][]const u8{
\\enum E;
,
\\uint8_t a(enum E * s);
\\
});
}