init.c (8393B)
1 #include <assert.h> 2 #include <stdbool.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include "util.h" 6 #include "cc.h" 7 8 struct object { 9 unsigned long long offset; 10 struct type *type; 11 union { 12 struct member *mem; 13 unsigned long long idx; 14 } u; 15 bool iscur; 16 }; 17 18 struct initparser { 19 /* TODO: keep track of type depth, and allocate maximum possible 20 * number of nested objects in initializer */ 21 struct object obj[32], *cur, *sub; 22 struct init *init, **last; 23 }; 24 25 struct init * 26 mkinit(unsigned long long start, unsigned long long end, struct bitfield bits, struct expr *expr) 27 { 28 struct init *init; 29 30 init = xmalloc(sizeof(*init)); 31 init->start = start; 32 init->end = end; 33 init->expr = expr; 34 init->bits = bits; 35 init->next = NULL; 36 37 return init; 38 } 39 40 static void 41 initadd(struct initparser *p, struct init *new) 42 { 43 struct init **init, *old; 44 45 init = p->last; 46 for (; (old = *init); init = &old->next) { 47 if (old->end * 8 - old->bits.after <= new->start * 8 + new->bits.before) 48 continue; 49 /* no overlap, insert before `old` */ 50 if (new->end * 8 - new->bits.after <= old->start * 8 + old->bits.before) 51 break; 52 /* replace any initializers that `new` covers */ 53 if (old->end * 8 - old->bits.after <= new->end * 8 - new->bits.after) { 54 do 55 old = old->next; 56 while (old && old->end * 8 - old->bits.after <= new->end * 8 - new->bits.after); 57 break; 58 } 59 /* `old` covers `new`, keep looking */ 60 } 61 new->next = old; 62 *init = new; 63 p->last = &new->next; 64 } 65 66 static void 67 subobj(struct initparser *p, struct type *t, unsigned long long off) 68 { 69 /* the subobject could be incomplete if its a flexible array member */ 70 if (t->incomplete) 71 error(&tok.loc, "initializer specified for incomplete type"); 72 off += p->sub->offset; 73 if (++p->sub == p->obj + countof(p->obj)) 74 fatal("internal error: too many designators"); 75 p->sub->type = t; 76 p->sub->offset = off; 77 p->sub->iscur = false; 78 } 79 80 static bool 81 findmember(struct initparser *p, const char *name) 82 { 83 struct member *m; 84 85 for (m = p->sub->type->u.structunion.members; m; m = m->next) { 86 if (m->name) { 87 if (strcmp(m->name, name) == 0) { 88 p->sub->u.mem = m; 89 subobj(p, m->type, m->offset); 90 return true; 91 } 92 } else { 93 subobj(p, m->type, m->offset); 94 if (findmember(p, name)) 95 return true; 96 --p->sub; 97 } 98 } 99 return false; 100 } 101 102 static void 103 designator(struct scope *s, struct initparser *p) 104 { 105 struct type *t; 106 char *name; 107 108 p->last = &p->init; 109 p->sub = p->cur; 110 for (;;) { 111 t = p->sub->type; 112 switch (tok.kind) { 113 case TLBRACK: 114 if (t->kind != TYPEARRAY) 115 error( 116 &tok.loc, 117 "index designator is only " 118 "valid for array types" 119 ); 120 next(); 121 p->sub->u.idx = intconstexpr(s, false) * t->base->size; 122 if (p->sub->u.idx >= t->size) { 123 if (!t->incomplete) 124 error( 125 &tok.loc, 126 "index designator is larger than " 127 "array length" 128 ); 129 t->size = p->sub->u.idx + t->base->size; 130 } 131 expect(TRBRACK, "for index designator"); 132 subobj(p, t->base, p->sub->u.idx); 133 break; 134 case TPERIOD: 135 if (t->kind != TYPESTRUCT && t->kind != TYPEUNION) 136 error( 137 &tok.loc, 138 "member designator only valid " 139 "for struct/union types" 140 ); 141 next(); 142 name = expect(TIDENT, "for member designator"); 143 if (!findmember(p, name)) 144 error( 145 &tok.loc, 146 "%s has no member named '%s'", 147 t->kind == TYPEUNION ? "union" : "struct", 148 name 149 ); 150 break; 151 default: 152 expect(TASSIGN, "after designator"); 153 return; 154 } 155 } 156 } 157 158 static void 159 focus(struct initparser *p) 160 { 161 struct type *t; 162 163 switch (p->sub->type->kind) { 164 case TYPEARRAY: 165 t = p->sub->type->base; 166 p->sub->u.idx = 0; 167 if (p->sub->type->incomplete) 168 p->sub->type->size = t->size; 169 break; 170 case TYPESTRUCT: 171 case TYPEUNION: 172 p->sub->u.mem = p->sub->type->u.structunion.members; 173 t = p->sub->u.mem->type; 174 break; 175 default: 176 fatal("internal error: init cursor has unexpected type"); 177 } 178 subobj(p, t, 0); 179 } 180 181 static void 182 advance(struct initparser *p) 183 { 184 struct type *t; 185 186 for (;;) { 187 --p->sub; 188 t = p->sub->type; 189 switch (t->kind) { 190 case TYPEARRAY: 191 p->sub->u.idx += t->base->size; 192 if (p->sub->u.idx == t->size) { 193 if (!t->incomplete) 194 break; 195 t->size += t->base->size; 196 } 197 subobj(p, t->base, p->sub->u.idx); 198 return; 199 case TYPESTRUCT: 200 p->sub->u.mem = p->sub->u.mem->next; 201 if (p->sub->u.mem) { 202 subobj(p, p->sub->u.mem->type, p->sub->u.mem->offset); 203 return; 204 } 205 break; 206 default: 207 break; 208 } 209 if (p->sub == p->cur) 210 error(&tok.loc, "too many initializers for type"); 211 } 212 } 213 214 /* 6.7.9 initialization */ 215 struct init * 216 parseinit(struct scope *s, struct type *t) 217 { 218 struct initparser p; 219 struct expr *expr; 220 struct type *base; 221 struct bitfield bits; 222 223 p.cur = NULL; 224 p.sub = p.obj; 225 p.sub->offset = 0; 226 p.sub->type = t; 227 p.sub->iscur = false; 228 p.init = NULL; 229 p.last = &p.init; 230 if (t->incomplete && t->kind != TYPEARRAY) 231 error(&tok.loc, "initializer specified for incomplete type"); 232 if (t->kind == TYPEARRAY && t->base->size == 0) 233 error(&tok.loc, "initializer specified for variable length array type"); 234 for (;;) { 235 if (p.cur) { 236 if (tok.kind == TLBRACK || tok.kind == TPERIOD) 237 designator(s, &p); 238 else if (p.sub != p.cur) 239 advance(&p); 240 else if (p.cur->type->kind == TYPESTRUCT || p.cur->type->kind == TYPEUNION) 241 focus(&p); 242 } 243 if (consume(TLBRACE)) { 244 if (consume(TRBRACE)) { 245 if (p.sub->type->incomplete) 246 error( 247 &tok.loc, 248 "array of unknown size has empty " 249 "initializer" 250 ); 251 goto next; 252 } 253 if (p.cur == p.sub) { 254 if (p.cur->type->prop & PROPSCALAR) 255 error( 256 &tok.loc, 257 "nested braces around " 258 "scalar initializer" 259 ); 260 assert(p.cur->type->kind == TYPEARRAY); 261 focus(&p); 262 } 263 p.cur = p.sub; 264 p.cur->iscur = true; 265 continue; 266 } 267 expr = assignexpr(s); 268 for (;;) { 269 t = p.sub->type; 270 switch (t->kind) { 271 case TYPEARRAY: 272 if (!expr->decayed || expr->base->kind != EXPRSTRING || !(t->base->prop & PROPINT)) 273 break; 274 base = t->base; 275 expr = expr->base; 276 if (!(base->prop & PROPCHAR && expr->type->base->prop & PROPCHAR) 277 && !typecompatible(base, expr->type->base)) 278 error( 279 &tok.loc, 280 "cannot initialize array with " 281 "string literal of different " 282 "width" 283 ); 284 if (t->incomplete) 285 t->size = expr->type->size; 286 goto add; 287 case TYPESTRUCT: 288 case TYPEUNION: 289 case TYPECOMPLEX: 290 if (typecompatible(expr->type, t)) 291 goto add; 292 break; 293 default: /* scalar type */ 294 assert(t->prop & PROPSCALAR); 295 expr = exprassign(expr, t); 296 goto add; 297 } 298 focus(&p); 299 } 300 add: 301 if (p.sub > p.obj && (p.sub[-1].type->kind == TYPESTRUCT || p.sub[-1].type->kind == TYPEUNION)) 302 bits = p.sub[-1].u.mem->bits; 303 else 304 bits = (struct bitfield){0}; 305 initadd(&p, mkinit(p.sub->offset, p.sub->offset + p.sub->type->size, bits, expr)); 306 for (;;) { 307 if (p.sub->type->incomplete) 308 p.sub->type->incomplete = false; 309 next: 310 if (!p.cur) 311 return p.init; 312 if (tok.kind == TCOMMA) { 313 next(); 314 if (tok.kind != TRBRACE) 315 break; 316 } else if (tok.kind != TRBRACE) { 317 error(&tok.loc, "expected ',' or '}' after initializer"); 318 } 319 next(); 320 p.sub = p.cur; 321 do 322 p.cur = p.cur == p.obj ? NULL : p.cur - 1; 323 while (p.cur && !p.cur->iscur); 324 } 325 } 326 }