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