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