| [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 | 
|---|
|  | 11 | // Last Modified By : Rob Schluntz | 
|---|
|  | 12 | // Last Modified On : Thu Jun 29 15:27:05 2017 | 
|---|
|  | 13 | // Update Count     : 2 | 
|---|
|  | 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 |  | 
|---|
|  | 91 | int main() { | 
|---|
| [62423350] | 92 | // simple designation case - starting from beginning of structure, leaves ptr default-initialized (zero) | 
|---|
|  | 93 | struct A y0 = { | 
|---|
|  | 94 | .x DES 2, | 
|---|
|  | 95 | .y DES 3 | 
|---|
|  | 96 | }; | 
|---|
| [bb1cd95] | 97 |  | 
|---|
| [62423350] | 98 | // simple initializaiton case - initialize all elements explicitly with no designations | 
|---|
|  | 99 | struct A y1 = { | 
|---|
|  | 100 | 2, 3, 0 | 
|---|
|  | 101 | }; | 
|---|
| [bb1cd95] | 102 |  | 
|---|
|  | 103 |  | 
|---|
| [62423350] | 104 | // use designation to move to member y, leaving x default-initialized (zero) | 
|---|
|  | 105 | struct A y2 = { | 
|---|
|  | 106 | .y DES 3, | 
|---|
|  | 107 | 0 | 
|---|
|  | 108 | }; | 
|---|
| [bb1cd95] | 109 |  | 
|---|
|  | 110 | #if ERROR | 
|---|
| [62423350] | 111 | struct A yErr0 = { | 
|---|
|  | 112 | {} // error - empty scalar initializer is illegal | 
|---|
|  | 113 | }; | 
|---|
| [bb1cd95] | 114 | #endif | 
|---|
|  | 115 |  | 
|---|
| [62423350] | 116 | printf("=====A=====\n"); | 
|---|
|  | 117 | printA(y0, 0); | 
|---|
|  | 118 | printA(y1, 0); | 
|---|
|  | 119 | printA(y2, 0); | 
|---|
|  | 120 | printf("=====A=====\n\n"); | 
|---|
|  | 121 |  | 
|---|
|  | 122 | // initialize only first element (z0.a.x), leaving everything else default-initialized (zero), no nested curly-braces | 
|---|
|  | 123 | struct B z0 = { 5 }; | 
|---|
|  | 124 |  | 
|---|
|  | 125 | // some nested curly braces, use designation to 'jump around' within structure, leaving some members default-initialized | 
|---|
|  | 126 | struct B z1 = { | 
|---|
|  | 127 | { 3 }, // z1.a0 | 
|---|
|  | 128 | { 4 }, // z1.a1 | 
|---|
|  | 129 | .a0 DES { 5 }, // z1.a0 | 
|---|
|  | 130 | { 6 }, // z1.a1 | 
|---|
|  | 131 | .a0.y DES 2, // z1.a0.y | 
|---|
|  | 132 | 0, // z1.a0.ptr | 
|---|
|  | 133 | }; | 
|---|
|  | 134 |  | 
|---|
|  | 135 | // z2.a0.y and z2.a0.ptr default-initialized, everything else explicit | 
|---|
|  | 136 | struct B z2 = { | 
|---|
|  | 137 | { 1 }, | 
|---|
|  | 138 | { 2, 3, 0 } | 
|---|
|  | 139 | }; | 
|---|
|  | 140 |  | 
|---|
|  | 141 | // initialize every member, omitting nested curly braces | 
|---|
|  | 142 | struct B z3 = { | 
|---|
|  | 143 | 1, 2, 0, 4, 5, 0 | 
|---|
|  | 144 | }; | 
|---|
|  | 145 |  | 
|---|
|  | 146 | // no initializer - legal C, but garbage values - don't print this one | 
|---|
|  | 147 | struct B z4; | 
|---|
|  | 148 |  | 
|---|
|  | 149 | // no curly braces - initialize with object of same type | 
|---|
|  | 150 | struct B z5 = z2; | 
|---|
|  | 151 |  | 
|---|
|  | 152 | // z6.a0.y and z6.a0.ptr default-initialized, everything else explicit. | 
|---|
|  | 153 | // no curly braces on z6.a1 initializers | 
|---|
|  | 154 | struct B z6 = { | 
|---|
|  | 155 | { 1 }, | 
|---|
|  | 156 | 2, 3, 0 | 
|---|
|  | 157 | }; | 
|---|
|  | 158 |  | 
|---|
|  | 159 | printf("=====B=====\n"); | 
|---|
|  | 160 | printB(z0, 0); | 
|---|
|  | 161 | printB(z1, 0); | 
|---|
|  | 162 | printB(z2, 0); | 
|---|
|  | 163 | printB(z3, 0); | 
|---|
|  | 164 | printB(z5, 0); | 
|---|
|  | 165 | printB(z6, 0); | 
|---|
|  | 166 | printf("=====B=====\n\n"); | 
|---|
|  | 167 |  | 
|---|
|  | 168 | // TODO: what about extra things in a nested init? are empty structs skipped?? | 
|---|
|  | 169 |  | 
|---|
|  | 170 | // test that initializing 'past array bound' correctly moves to next member. | 
|---|
|  | 171 | struct C c1 = { | 
|---|
|  | 172 | 2, 3, 4,  // arr | 
|---|
|  | 173 | 5, 6, 0,  // b.a0 | 
|---|
|  | 174 | 7, 8, 0,  // b.a1 | 
|---|
|  | 175 | }; | 
|---|
|  | 176 |  | 
|---|
|  | 177 | printf("=====C=====\n"); | 
|---|
|  | 178 | printC(c1, 0); | 
|---|
|  | 179 | printf("=====C=====\n\n"); | 
|---|
| [bb1cd95] | 180 |  | 
|---|
|  | 181 | #if ERROR | 
|---|
| [62423350] | 182 | // nested initializer can't refer to same type in C | 
|---|
|  | 183 | struct C cErr0 = { c1 }; | 
|---|
| [bb1cd95] | 184 |  | 
|---|
| [62423350] | 185 | // must use curly braces to initialize members | 
|---|
|  | 186 | struct C cErr1 = 2; | 
|---|
| [bb1cd95] | 187 |  | 
|---|
| [62423350] | 188 | // can't initialize with array compound literal | 
|---|
|  | 189 | struct C cErr2 = { | 
|---|
|  | 190 | (int[3]) { 1, 2, 3 }  // error: array initialized from non-constant array expression | 
|---|
|  | 191 | }; | 
|---|
| [bb1cd95] | 192 | #endif | 
|---|
|  | 193 |  | 
|---|
|  | 194 | #if WARNING | 
|---|
| [62423350] | 195 | // can't initialize array with array - converts to int* | 
|---|
|  | 196 | int cWarn0_arr[3] = { 1, 2, 3 }; | 
|---|
|  | 197 | struct C cWarn0 = { | 
|---|
|  | 198 | cWarn0_arr  // warning: initialization makes integer from ptr without cast | 
|---|
|  | 199 | }; | 
|---|
| [bb1cd95] | 200 | #endif | 
|---|
|  | 201 |  | 
|---|
| [62423350] | 202 | // allowed to have 'too many' initialized lists - essentially they are ignored. | 
|---|
|  | 203 | int i1 = { 3 }; | 
|---|
| [bb1cd95] | 204 |  | 
|---|
| [62423350] | 205 | // doesn't work yet. | 
|---|
|  | 206 | // designate unnamed object's members | 
|---|
|  | 207 | // struct D d = { .x DES 3 }; | 
|---|
| [bb1cd95] | 208 | #if ERROR | 
|---|
| [62423350] | 209 | struct D d1 = { .y DES 3 }; | 
|---|
| [bb1cd95] | 210 | #endif | 
|---|
|  | 211 |  | 
|---|
| [62423350] | 212 | // simple union initialization - initialized first member (e0.a) | 
|---|
|  | 213 | union E e0 = { | 
|---|
|  | 214 | y0 | 
|---|
|  | 215 | }; | 
|---|
|  | 216 |  | 
|---|
|  | 217 | // simple union initialization - initializes first member (e1.a) - with nested initializer list | 
|---|
|  | 218 | union E e1 = { | 
|---|
|  | 219 | { 2, 3, 0 } | 
|---|
|  | 220 | }; | 
|---|
|  | 221 |  | 
|---|
|  | 222 | // simple union initialization - initializes first member (e2.a) - without nested initializer list | 
|---|
|  | 223 | union E e2 = { | 
|---|
|  | 224 | 2, 3, 0 | 
|---|
|  | 225 | }; | 
|---|
|  | 226 |  | 
|---|
|  | 227 | // move cursor to e4.b.a0.x and initialize until e3.b.a1.ptr inclusive | 
|---|
|  | 228 | union E e3 = { | 
|---|
|  | 229 | .b.a0.x DES 2, 3, 0, 5, 6, 0 | 
|---|
|  | 230 | }; | 
|---|
|  | 231 |  | 
|---|
|  | 232 | printf("=====E=====\n"); | 
|---|
|  | 233 | printA(e0.a, 0); | 
|---|
|  | 234 | printA(e1.a, 0); | 
|---|
|  | 235 | printA(e2.a, 0); | 
|---|
|  | 236 | printB(e3.b, 0); | 
|---|
|  | 237 | printf("=====E=====\n\n"); | 
|---|
|  | 238 |  | 
|---|
|  | 239 | // special case of initialization: char[] can be initialized with a string literal | 
|---|
|  | 240 | const char * str0 = "hello"; | 
|---|
|  | 241 | char str1[] = "hello"; | 
|---|
| [bb1cd95] | 242 | } | 
|---|
|  | 243 |  | 
|---|
|  | 244 | // Local Variables: // | 
|---|
|  | 245 | // tab-width: 4 // | 
|---|
|  | 246 | // End: // | 
|---|