1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // designations.c -- |
---|
8 | // |
---|
9 | // Author : Rob Schluntz |
---|
10 | // Created On : Thu Jun 29 15:26:36 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Sun Oct 13 11:53:16 2024 |
---|
13 | // Update Count : 31 |
---|
14 | // |
---|
15 | |
---|
16 | #include <fstream.hfa> |
---|
17 | |
---|
18 | // Note: this test case has been crafted so that it compiles with both cfa and with gcc without any modifications. |
---|
19 | #ifdef __cforall |
---|
20 | #define AT @ |
---|
21 | #else |
---|
22 | #define AT |
---|
23 | #endif |
---|
24 | |
---|
25 | const int indentAmt = 2; |
---|
26 | void indent( int level ) { |
---|
27 | sout | wd( level, "" ) | nonl; |
---|
28 | } |
---|
29 | |
---|
30 | // A contains fields with different types (int vs. int *) |
---|
31 | struct A { |
---|
32 | int x, y; |
---|
33 | int * ptr; |
---|
34 | }; |
---|
35 | void printA( struct A a, int level ) { |
---|
36 | indent( level ); |
---|
37 | sout | "(A){ " | a.x | a.y | a.ptr | " }"; |
---|
38 | } |
---|
39 | |
---|
40 | // B contains struct members |
---|
41 | struct B { |
---|
42 | struct A a0, a1; |
---|
43 | }; |
---|
44 | void printB( struct B b, int level ) { |
---|
45 | indent( level ); |
---|
46 | sout | "(B){"; |
---|
47 | printA( b.a0, level+indentAmt ); |
---|
48 | printA( b.a1, level+indentAmt ); |
---|
49 | indent( level ); |
---|
50 | sout | "}"; |
---|
51 | } |
---|
52 | |
---|
53 | // C contains an array - tests that after 3 ints, the members of B are initialized. |
---|
54 | struct C { |
---|
55 | int arr[3]; |
---|
56 | struct B b; |
---|
57 | }; |
---|
58 | void printC( struct C c, int level ) { |
---|
59 | indent( level ); |
---|
60 | sout | "(C){"; |
---|
61 | indent( level+indentAmt ); |
---|
62 | sout | "(int[]{ " | c.arr[0] | c.arr[1] | c.arr[2] | " }"; |
---|
63 | printB( c.b, level+indentAmt ); |
---|
64 | indent( level ); |
---|
65 | sout | "}"; |
---|
66 | } |
---|
67 | |
---|
68 | // D contains an unnamed aggregate - tests that this doesn't interfere with initialization. |
---|
69 | struct D { |
---|
70 | struct { |
---|
71 | int x; |
---|
72 | }; |
---|
73 | }; |
---|
74 | void printD( struct D d, int level ) { |
---|
75 | indent( level); |
---|
76 | sout | "(D){ " | d.x | "}"; |
---|
77 | } |
---|
78 | |
---|
79 | // E tests unions |
---|
80 | union E { |
---|
81 | struct A a; |
---|
82 | struct B b; |
---|
83 | struct C c; |
---|
84 | struct D d; |
---|
85 | int i; |
---|
86 | }; |
---|
87 | |
---|
88 | struct Fred { |
---|
89 | double i[3]; |
---|
90 | int j; |
---|
91 | struct Mary { |
---|
92 | struct Jane { |
---|
93 | double j; |
---|
94 | } j; |
---|
95 | double i; |
---|
96 | } m; |
---|
97 | }; |
---|
98 | struct Fred s1 AT= { .m.j = 3 }; |
---|
99 | struct Fred s2 AT= { .i = { [2] = 2 } }; |
---|
100 | |
---|
101 | int main() { |
---|
102 | // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero) |
---|
103 | struct A y0 = { |
---|
104 | .x = 2, |
---|
105 | .y = 3 |
---|
106 | }; |
---|
107 | |
---|
108 | // simple initializaiton case - initialize all elements explicitly with no designations |
---|
109 | struct A y1 = { |
---|
110 | 2, 3, 0 |
---|
111 | }; |
---|
112 | |
---|
113 | |
---|
114 | // use designation to move to member y, leaving x default-initialized (zero) |
---|
115 | struct A y2 = { |
---|
116 | .y = 3, |
---|
117 | 0 |
---|
118 | }; |
---|
119 | |
---|
120 | #if ERROR |
---|
121 | struct A yErr0 = { |
---|
122 | {} // error - empty scalar initializer is illegal |
---|
123 | }; |
---|
124 | #endif |
---|
125 | |
---|
126 | sout | "=====A====="; |
---|
127 | printA( y0, 0 ); |
---|
128 | printA( y1, 0 ); |
---|
129 | printA( y2, 0 ); |
---|
130 | sout | "=====A=====" | nl | nl; |
---|
131 | |
---|
132 | // initialize only first element (z0.a.x), leaving everything else default-initialized (zero), no nested curly-braces |
---|
133 | struct B z0 = { 5 }; |
---|
134 | |
---|
135 | // some nested curly braces, use designation to 'jump around' within structure, leaving some members default-initialized |
---|
136 | struct B z1 = { |
---|
137 | { 3 }, // z1.a0 |
---|
138 | { 4 }, // z1.a1 |
---|
139 | .a0 = { 5 }, // z1.a0 |
---|
140 | { 6 }, // z1.a1 |
---|
141 | .a0.y = 2, // z1.a0.y |
---|
142 | 0, // z1.a0.ptr |
---|
143 | }; |
---|
144 | |
---|
145 | // z2.a0.y and z2.a0.ptr default-initialized, everything else explicit |
---|
146 | struct B z2 = { |
---|
147 | { 1 }, |
---|
148 | { 2, 3, 0 } |
---|
149 | }; |
---|
150 | |
---|
151 | // initialize every member, omitting nested curly braces |
---|
152 | struct B z3 = { |
---|
153 | 1, 2, 0, 4, 5, 0 |
---|
154 | }; |
---|
155 | |
---|
156 | // no initializer - legal C, but garbage values - don't print this one |
---|
157 | struct B z4; |
---|
158 | |
---|
159 | // no curly braces - initialize with object of same type |
---|
160 | struct B z5 = z2; |
---|
161 | |
---|
162 | // z6.a0.y and z6.a0.ptr default-initialized, everything else explicit. |
---|
163 | // no curly braces on z6.a1 initializers |
---|
164 | struct B z6 = { |
---|
165 | { 1 }, |
---|
166 | 2, 3, 0 |
---|
167 | }; |
---|
168 | |
---|
169 | sout | "=====B====="; |
---|
170 | printB( z0, 0 ); |
---|
171 | printB( z1, 0 ); |
---|
172 | printB( z2, 0 ); |
---|
173 | printB( z3, 0 ); |
---|
174 | printB( z5, 0 ); |
---|
175 | printB( z6, 0 ); |
---|
176 | sout | "=====B=====" | nl | nl; |
---|
177 | |
---|
178 | // TODO: what about extra things in a nested init? are empty structs skipped?? |
---|
179 | |
---|
180 | // test that initializing 'past array bound' correctly moves to next member. |
---|
181 | struct C c1 = { |
---|
182 | 2, 3, 4, // arr |
---|
183 | 5, 6, 0, // b.a0 |
---|
184 | 7, 8, 0, // b.a1 |
---|
185 | }; |
---|
186 | |
---|
187 | sout | "=====C====="; |
---|
188 | printC( c1, 0 ); |
---|
189 | sout | "=====C=====" | nl | nl; |
---|
190 | |
---|
191 | #if ERROR |
---|
192 | // nested initializer can't refer to same type in C |
---|
193 | struct C cErr0 = { c1 }; |
---|
194 | |
---|
195 | // must use curly braces to initialize members |
---|
196 | struct C cErr1 = 2; |
---|
197 | |
---|
198 | // can't initialize with array compound literal |
---|
199 | struct C cErr2 = { |
---|
200 | (int[3]) { 1, 2, 3 } // error: array initialized from non-constant array expression |
---|
201 | }; |
---|
202 | #endif |
---|
203 | |
---|
204 | #if WARNING |
---|
205 | // can't initialize array with array - converts to int* |
---|
206 | int cWarn0_arr[3] = { 1, 2, 3 }; |
---|
207 | struct C cWarn0 = { |
---|
208 | cWarn0_arr // warning: initialization makes integer from ptr without cast |
---|
209 | }; |
---|
210 | #endif |
---|
211 | // array designation |
---|
212 | int i[2] = { [1] = 3 }; |
---|
213 | // allowed to have 'too many' initialized lists - essentially they are ignored. |
---|
214 | int i1 = { 3 }; |
---|
215 | |
---|
216 | // doesn't work yet. |
---|
217 | // designate unnamed object's members |
---|
218 | // struct D d = { .x = 3 }; |
---|
219 | #if ERROR |
---|
220 | struct D d1 = { .y = 3 }; |
---|
221 | #endif |
---|
222 | |
---|
223 | // simple union initialization - initialized first member (e0.a) |
---|
224 | union E e0 = { |
---|
225 | y0 |
---|
226 | }; |
---|
227 | |
---|
228 | // simple union initialization - initializes first member (e1.a) - with nested initializer list |
---|
229 | union E e1 = { |
---|
230 | { 2, 3, 0 } |
---|
231 | }; |
---|
232 | |
---|
233 | // simple union initialization - initializes first member (e2.a) - without nested initializer list |
---|
234 | union E e2 = { |
---|
235 | 2, 3, 0 |
---|
236 | }; |
---|
237 | |
---|
238 | // move cursor to e4.b.a0.x and initialize until e3.b.a1.ptr inclusive |
---|
239 | union E e3 = { |
---|
240 | .b.a0.x = 2, 3, 0, 5, 6, 0 |
---|
241 | }; |
---|
242 | |
---|
243 | sout | "=====E====="; |
---|
244 | printA( e0.a, 0 ); |
---|
245 | printA( e1.a, 0 ); |
---|
246 | printA( e2.a, 0 ); |
---|
247 | printB( e3.b, 0 ); |
---|
248 | sout | "=====E=====" | nl | nl; |
---|
249 | |
---|
250 | // special case of initialization: char[] can be initialized with a string literal |
---|
251 | const char * str0 = "hello"; |
---|
252 | char str1[] = "hello"; |
---|
253 | const char c2[] = "abc"; |
---|
254 | const char c3[] = { 'a', 'b', 'c' }; |
---|
255 | const char c4[][2] = { { 'a', 'b' }, { 'c', 'd'}, { 'c', 'd'} }; |
---|
256 | |
---|
257 | // more cases |
---|
258 | |
---|
259 | // int widths[] = { [3 ... 9] = 1, [10 ... 99] = 2, [100] = 3 }; |
---|
260 | // int widths[] = { [3 ~ 9] = 1, [10 ~ 99] = 2, [100] = 3 }; |
---|
261 | struct point { int x, y; }; |
---|
262 | struct point p = { .y = 5, .x = 7 }; |
---|
263 | union foo { int i; double d; }; |
---|
264 | union foo f = { .d = 4 }; |
---|
265 | int v1, v2, v4; |
---|
266 | int w[6] = { [1] = v1, v2, [4] = v4 }; |
---|
267 | int whitespace[256] = { [' '] = 1, ['\t'] = 1, ['\v'] = 1, ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 }; |
---|
268 | struct point ptarray[10] = { [2].y = 34, [2].x = 35, [0].x = 36 }; |
---|
269 | } |
---|
270 | |
---|
271 | // Local Variables: // |
---|
272 | // tab-width: 4 // |
---|
273 | // End: // |
---|