Annotation of src/usr.bin/xlint/lint1/init.c, Revision 1.144
1.144 ! rillig 1: /* $NetBSD: init.c,v 1.143 2021/03/27 22:53:10 rillig Exp $ */
1.2 cgd 2:
1.1 cgd 3: /*
4: * Copyright (c) 1994, 1995 Jochen Pohl
5: * All Rights Reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: * 3. All advertising materials mentioning features or use of this software
16: * must display the following acknowledgement:
17: * This product includes software developed by Jochen Pohl for
18: * The NetBSD Project.
19: * 4. The name of the author may not be used to endorse or promote products
20: * derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32: */
33:
1.18 jmc 34: #if HAVE_NBTOOL_CONFIG_H
35: #include "nbtool_config.h"
36: #endif
37:
1.5 christos 38: #include <sys/cdefs.h>
1.10 tv 39: #if defined(__RCSID) && !defined(lint)
1.144 ! rillig 40: __RCSID("$NetBSD: init.c,v 1.143 2021/03/27 22:53:10 rillig Exp $");
1.1 cgd 41: #endif
42:
43: #include <stdlib.h>
1.17 thorpej 44: #include <string.h>
1.1 cgd 45:
46: #include "lint1.h"
47:
1.53 rillig 48:
49: /*
1.93 rillig 50: * Initialization
51: *
52: * Handles initializations of global or local objects, like in:
53: *
54: * int number = 12345;
55: * int number_with_braces = { 12345 };
56: *
57: * int array_of_unknown_size[] = { 111, 222, 333 };
58: * int array_flat[2][2] = { 11, 12, 21, 22 };
59: * int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
60: *
61: * struct { int x, y; } point = { 3, 4 };
1.119 rillig 62: * struct { int x, y; } point = { .y = 4, .x = 3 };
1.93 rillig 63: *
1.119 rillig 64: * Any scalar expression in the initializer may be surrounded by arbitrarily
65: * many extra pairs of braces, like in the example 'number_with_braces' (C99
66: * 6.7.8p11).
67: *
68: * For multi-dimensional arrays, the inner braces may be omitted like in
1.112 rillig 69: * array_flat or spelled out like in array_nested.
1.93 rillig 70: *
71: * For the initializer, the grammar parser calls these functions:
72: *
1.119 rillig 73: * begin_initialization
74: * init_lbrace for each '{'
1.131 rillig 75: * designation_add_name for each '.member' before '='
76: * designation_add_subscript for each '[123]' before '='
1.119 rillig 77: * init_using_expr for each expression
78: * init_rbrace for each '}'
79: * end_initialization
1.93 rillig 80: *
1.139 rillig 81: * Each '{' begins a new brace level, each '}' ends the current brace level.
82: * Each brace level has an associated "current object".
1.93 rillig 83: *
1.139 rillig 84: * Most of the time, the topmost level of brace_level contains a scalar type,
85: * and its remaining count toggles between 1 and 0.
1.93 rillig 86: *
87: * See also:
88: * C99 6.7.8 "Initialization"
89: * d_c99_init.c for more examples
90: */
91:
92:
93: /*
1.139 rillig 94: * Describes a single brace level of an ongoing initialization.
1.54 rillig 95: *
1.119 rillig 96: * XXX: Since C99, the initializers can be listed in arbitrary order by using
97: * designators to specify the sub-object to be initialized. The member names
1.54 rillig 98: * of non-leaf structs may thus appear repeatedly, as demonstrated in
99: * d_init_pop_member.c.
100: *
101: * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
102: * selected examples.
1.53 rillig 103: */
1.139 rillig 104: struct brace_level {
1.77 rillig 105:
1.92 rillig 106: /*
1.139 rillig 107: * The type of the current object that is initialized at this brace
108: * level.
1.102 rillig 109: *
110: * On the outermost element, this is always NULL since the outermost
111: * initializer-expression may be enclosed in an optional pair of
1.119 rillig 112: * braces, as of the current implementation.
113: *
114: * FIXME: This approach is wrong. It's not that the outermost
115: * initializer may be enclosed in additional braces, it's every scalar
116: * that may be enclosed in additional braces, as of C99 6.7.8p11.
1.102 rillig 117: *
118: * Everywhere else it is nonnull.
1.92 rillig 119: */
1.139 rillig 120: type_t *bl_type;
1.102 rillig 121:
1.92 rillig 122: /*
1.102 rillig 123: * The type that will be initialized at the next initialization level,
124: * usually enclosed by another pair of braces.
1.92 rillig 125: *
1.102 rillig 126: * For an array, it is the element type, but without 'const'.
127: *
128: * For a struct or union type, it is one of the member types, but
129: * without 'const'.
130: *
1.139 rillig 131: * The outermost stack element has no bl_type but nevertheless has
132: * bl_subtype. For example, in 'int var = { 12345 }', initially there
133: * is a brace_level with bl_subtype 'int'. When the '{' is processed,
134: * an element with bl_type 'int' is pushed to the stack. When the
1.92 rillig 135: * corresponding '}' is processed, the inner element is popped again.
136: *
137: * During initialization, only the top 2 elements of the stack are
138: * looked at.
1.119 rillig 139: *
1.139 rillig 140: * XXX: Having bl_subtype here is the wrong approach, it should not be
141: * necessary at all; see bl_type.
1.92 rillig 142: */
1.139 rillig 143: type_t *bl_subtype;
1.77 rillig 144:
1.82 rillig 145: /*
1.119 rillig 146: * Whether this level of the initializer requires a '}' to be
147: * completed.
1.82 rillig 148: *
149: * Multidimensional arrays do not need a closing brace to complete
150: * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
1.119 rillig 151: * for 'int arr[2][2]'.
1.82 rillig 152: *
1.119 rillig 153: * XXX: Double-check whether this is the correct approach at all; see
1.139 rillig 154: * bl_type.
1.82 rillig 155: */
1.139 rillig 156: bool bl_brace: 1;
1.82 rillig 157:
1.139 rillig 158: /* Whether bl_type is an array of unknown size. */
159: bool bl_array_of_unknown_size: 1;
1.119 rillig 160:
161: /*
162: * XXX: This feels wrong. Whether or not there has been a named
163: * initializer (called 'designation' since C99) should not matter at
164: * all. Even after an initializer with designation, counting of the
165: * remaining elements continues, see C99 6.7.8p17.
166: */
1.139 rillig 167: bool bl_seen_named_member: 1;
1.77 rillig 168:
169: /*
1.125 rillig 170: * For structs, the next member to be initialized by a designator-less
171: * initializer.
1.77 rillig 172: */
1.139 rillig 173: sym_t *bl_next_member;
1.125 rillig 174:
1.139 rillig 175: /* TODO: Add bl_next_subscript for arrays. */
1.125 rillig 176:
177: /* TODO: Understand C99 6.7.8p17 and footnote 128 for unions. */
1.77 rillig 178:
179: /*
1.102 rillig 180: * The number of remaining elements to be used by expressions without
181: * designator.
182: *
183: * This says nothing about which members have been initialized or not
184: * since starting with C99, members may be initialized in arbitrary
185: * order by using designators.
1.77 rillig 186: *
1.93 rillig 187: * For an array of unknown size, this is always 0 and thus irrelevant.
188: *
1.77 rillig 189: * XXX: for scalars?
190: * XXX: for structs?
191: * XXX: for unions?
192: * XXX: for arrays?
1.119 rillig 193: *
194: * XXX: Having the count of remaining objects should not be necessary.
1.139 rillig 195: * It is probably clearer to use bl_next_member and bl_next_subscript
1.125 rillig 196: * for this purpose.
1.77 rillig 197: */
1.139 rillig 198: int bl_remaining;
1.77 rillig 199:
200: /*
201: * The initialization state of the enclosing data structure
202: * (struct, union, array).
1.119 rillig 203: *
204: * XXX: Or for a scalar, for the top-level element, or for expressions
205: * in redundant braces such as '{{{{ 0 }}}}' (not yet implemented as
206: * of 2021-03-25).
1.77 rillig 207: */
1.139 rillig 208: struct brace_level *bl_enclosing;
209: };
1.53 rillig 210:
1.55 rillig 211: /*
1.135 rillig 212: * A single component on the path to the sub-object that is initialized by an
213: * initializer expression. Either a struct or union member, or an array
214: * subscript.
1.55 rillig 215: *
1.128 rillig 216: * See also: C99 6.7.8 "Initialization"
217: */
1.140 rillig 218: struct designator {
1.128 rillig 219: const char *name; /* for struct and union */
220: /* TODO: add 'subscript' for arrays */
221: struct designator *next;
1.140 rillig 222: };
1.128 rillig 223:
224: /*
1.135 rillig 225: * The optional designation for an initializer, saying which sub-object to
226: * initialize. Examples for designations are '.member' or
227: * '.member[123].member.member[1][1]'.
228: *
1.128 rillig 229: * See also: C99 6.7.8 "Initialization"
1.55 rillig 230: */
1.140 rillig 231: struct designation {
232: struct designator *head;
233: struct designator *tail;
234: };
1.53 rillig 235:
1.110 rillig 236: struct initialization {
237: /*
1.111 rillig 238: * is set as soon as a fatal error occurred in the initialization.
239: * The effect is that the rest of the initialization is ignored
240: * (parsed by yacc, expression trees built, but no initialization
241: * takes place).
1.110 rillig 242: */
243: bool initerr;
244:
1.139 rillig 245: /* The symbol that is to be initialized. */
1.110 rillig 246: sym_t *initsym;
1.53 rillig 247:
1.139 rillig 248: /* The innermost brace level. */
249: struct brace_level *brace_level;
1.1 cgd 250:
1.124 rillig 251: /*
252: * The C99 designator, if any, for the current initialization
253: * expression.
254: */
1.140 rillig 255: struct designation designation;
1.1 cgd 256:
1.110 rillig 257: struct initialization *next;
258: };
1.1 cgd 259:
1.111 rillig 260: static struct initialization *init;
1.12 christos 261:
1.1 cgd 262:
1.119 rillig 263: /* XXX: unnecessary prototype since it is not recursive */
1.82 rillig 264: static bool init_array_using_string(tnode_t *);
1.12 christos 265:
1.119 rillig 266:
1.135 rillig 267: static struct initialization *
268: current_init(void)
269: {
270: lint_assert(init != NULL);
271: return init;
272: }
1.119 rillig 273:
1.110 rillig 274: bool *
275: current_initerr(void)
276: {
1.135 rillig 277: return ¤t_init()->initerr;
1.110 rillig 278: }
279:
280: sym_t **
281: current_initsym(void)
282: {
1.135 rillig 283: return ¤t_init()->initsym;
1.110 rillig 284: }
285:
1.140 rillig 286: static struct designation *
1.123 rillig 287: current_designation_mod(void)
1.110 rillig 288: {
1.135 rillig 289: return ¤t_init()->designation;
1.110 rillig 290: }
291:
1.140 rillig 292: static struct designation
1.123 rillig 293: current_designation(void)
294: {
295: return *current_designation_mod();
296: }
297:
1.139 rillig 298: static const struct brace_level *
299: current_brace_level(void)
1.129 rillig 300: {
1.139 rillig 301: return current_init()->brace_level;
1.129 rillig 302: }
303:
1.139 rillig 304: static struct brace_level **
305: current_brace_level_lvalue(void)
1.110 rillig 306: {
1.139 rillig 307: return ¤t_init()->brace_level;
1.110 rillig 308: }
309:
1.121 rillig 310: static void
311: free_initialization(struct initialization *in)
312: {
1.139 rillig 313: struct brace_level *level, *next;
1.121 rillig 314:
1.139 rillig 315: for (level = in->brace_level; level != NULL; level = next) {
316: next = level->bl_enclosing;
317: free(level);
1.121 rillig 318: }
319:
320: free(in);
321: }
322:
1.141 rillig 323: static void
324: set_initerr(void)
325: {
326: current_init()->initerr = true;
327: }
328:
1.119 rillig 329: #define initerr (*current_initerr())
330: #define initsym (*current_initsym())
1.139 rillig 331: #define brace_level_rvalue (current_brace_level())
332: #define brace_level_lvalue (*current_brace_level_lvalue())
1.110 rillig 333:
1.12 christos 334: #ifndef DEBUG
1.90 rillig 335:
1.75 rillig 336: #define debug_printf(fmt, ...) do { } while (false)
337: #define debug_indent() do { } while (false)
338: #define debug_enter(a) do { } while (false)
339: #define debug_step(fmt, ...) do { } while (false)
340: #define debug_leave(a) do { } while (false)
1.126 rillig 341: #define debug_designation() do { } while (false)
1.139 rillig 342: #define debug_brace_level(level) do { } while (false)
1.90 rillig 343: #define debug_initstack() do { } while (false)
344:
1.12 christos 345: #else
1.90 rillig 346:
1.68 rillig 347: static int debug_ind = 0;
348:
349: static void __printflike(1, 2)
350: debug_printf(const char *fmt, ...)
351: {
352: va_list va;
353:
354: va_start(va, fmt);
355: vfprintf(stdout, fmt, va);
356: va_end(va);
357: }
358:
359: static void
360: debug_indent(void)
361: {
362: debug_printf("%*s", 2 * debug_ind, "");
363: }
364:
365: static void
366: debug_enter(const char *func)
367: {
368: printf("%*s+ %s\n", 2 * debug_ind++, "", func);
369: }
370:
371: static void __printflike(1, 2)
372: debug_step(const char *fmt, ...)
373: {
374: va_list va;
375:
376: printf("%*s", 2 * debug_ind, "");
377: va_start(va, fmt);
378: vfprintf(stdout, fmt, va);
379: va_end(va);
380: printf("\n");
381: }
382:
383: static void
384: debug_leave(const char *func)
385: {
386: printf("%*s- %s\n", 2 * --debug_ind, "", func);
387: }
388:
1.55 rillig 389: static void
1.126 rillig 390: debug_designation(void)
1.55 rillig 391: {
1.140 rillig 392: const struct designator *head = current_designation().head, *p;
1.122 rillig 393: if (head == NULL)
1.55 rillig 394: return;
1.126 rillig 395:
1.68 rillig 396: debug_indent();
1.126 rillig 397: debug_printf("designation: ");
1.128 rillig 398: for (p = head; p != NULL; p = p->next)
399: debug_printf(".%s", p->name);
1.68 rillig 400: debug_printf("\n");
1.55 rillig 401: }
1.1 cgd 402:
1.119 rillig 403: /*
404: * TODO: only log the top of the stack after each modifying operation
405: *
1.139 rillig 406: * TODO: wrap all write accesses to brace_level in setter functions
1.119 rillig 407: */
1.78 rillig 408: static void
1.139 rillig 409: debug_brace_level(const struct brace_level *level)
1.74 rillig 410: {
1.139 rillig 411: if (level->bl_type != NULL)
412: debug_printf("type '%s'", type_name(level->bl_type));
413: if (level->bl_type != NULL && level->bl_subtype != NULL)
1.120 rillig 414: debug_printf(", ");
1.139 rillig 415: if (level->bl_subtype != NULL)
416: debug_printf("subtype '%s'", type_name(level->bl_subtype));
1.78 rillig 417:
1.139 rillig 418: if (level->bl_brace)
1.120 rillig 419: debug_printf(", needs closing brace");
1.139 rillig 420: if (level->bl_array_of_unknown_size)
1.120 rillig 421: debug_printf(", array of unknown size");
1.139 rillig 422: if (level->bl_seen_named_member)
1.120 rillig 423: debug_printf(", seen named member");
1.78 rillig 424:
1.139 rillig 425: const type_t *eff_type = level->bl_type != NULL
426: ? level->bl_type : level->bl_subtype;
427: if (eff_type->t_tspec == STRUCT && level->bl_next_member != NULL)
1.125 rillig 428: debug_printf(", next member '%s'",
1.139 rillig 429: level->bl_next_member->s_name);
1.78 rillig 430:
1.139 rillig 431: debug_printf(", remaining %d\n", level->bl_remaining);
1.74 rillig 432: }
433:
1.119 rillig 434: /*
435: * TODO: only call debug_initstack after each push/pop.
436: */
1.73 rillig 437: static void
438: debug_initstack(void)
439: {
1.139 rillig 440: if (brace_level_rvalue == NULL) {
441: debug_step("no brace level in the current initialization");
1.73 rillig 442: return;
443: }
444:
445: size_t i = 0;
1.139 rillig 446: for (const struct brace_level *level = brace_level_rvalue;
447: level != NULL; level = level->bl_enclosing) {
1.120 rillig 448: debug_indent();
1.139 rillig 449: debug_printf("brace level %zu: ", i);
450: debug_brace_level(level);
1.73 rillig 451: i++;
452: }
453: }
1.90 rillig 454:
455: #define debug_enter() debug_enter(__func__)
456: #define debug_leave() debug_leave(__func__)
457:
1.73 rillig 458: #endif
459:
1.111 rillig 460:
461: void
462: begin_initialization(sym_t *sym)
463: {
464: struct initialization *curr_init;
465:
1.118 rillig 466: debug_step("begin initialization of '%s'", type_name(sym->s_type));
1.111 rillig 467: curr_init = xcalloc(1, sizeof *curr_init);
468: curr_init->next = init;
469: init = curr_init;
470: initsym = sym;
471: }
472:
473: void
474: end_initialization(void)
475: {
476: struct initialization *curr_init;
477:
478: curr_init = init;
479: init = init->next;
1.121 rillig 480: free_initialization(curr_init);
1.111 rillig 481: debug_step("end initialization");
482: }
483:
1.142 rillig 484: static struct designator *
485: designator_new(const char *name)
1.90 rillig 486: {
1.142 rillig 487: struct designator *d = xcalloc(1, sizeof *d);
488: d->name = name;
489: return d;
490: }
1.127 rillig 491:
1.142 rillig 492: static void
493: designator_free(struct designator *d)
494: {
495: free(d);
496: }
497:
498: static void
499: designation_add(struct designator *d)
500: {
501: struct designation *dd = ¤t_init()->designation;
1.90 rillig 502:
1.128 rillig 503: if (dd->head != NULL) {
504: dd->tail->next = d;
505: dd->tail = d;
1.90 rillig 506: } else {
1.128 rillig 507: dd->head = d;
508: dd->tail = d;
1.90 rillig 509: }
1.106 rillig 510:
1.126 rillig 511: debug_designation();
1.90 rillig 512: }
513:
1.142 rillig 514: void
515: designation_add_name(sbuf_t *sb)
516: {
517: designation_add(designator_new(sb->sb_name));
518: }
519:
1.133 rillig 520: /* TODO: Move the function body up here, to avoid the forward declaration. */
521: static void initstack_pop_nobrace(void);
522:
1.143 rillig 523: static struct brace_level *
524: brace_level_new(type_t *type, type_t *subtype, int remaining)
525: {
526: struct brace_level *level = xcalloc(1, sizeof(*level));
527:
528: level->bl_type = type;
529: level->bl_subtype = subtype;
530: level->bl_remaining = remaining;
531:
532: return level;
533: }
534:
535: static const sym_t *
536: brace_level_look_up_member(const char *name)
537: {
538: const type_t *tp = current_brace_level()->bl_type;
539: const sym_t *m;
540:
541: lint_assert(tp->t_tspec == STRUCT || tp->t_tspec == UNION);
542:
543: for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
544: if (m->s_bitfield && m->s_name == unnamed)
545: continue;
546: if (strcmp(m->s_name, name) == 0)
547: return m;
548: }
549:
550: return NULL;
551: }
552:
1.142 rillig 553: static void
554: brace_level_set_array_dimension(int dim)
555: {
556: debug_step("setting the array size to %d", dim);
557: brace_level_lvalue->bl_type->t_dim = dim;
558: debug_indent();
559: debug_brace_level(brace_level_rvalue);
560: }
561:
1.144 ! rillig 562: static void
! 563: brace_level_next_member(struct brace_level *level)
! 564: {
! 565: const sym_t *m;
! 566:
! 567: do {
! 568: m = level->bl_next_member = level->bl_next_member->s_next;
! 569: /* XXX: can this assertion be made to fail? */
! 570: lint_assert(m != NULL);
! 571: } while (m->s_bitfield && m->s_name == unnamed);
! 572:
! 573: debug_indent();
! 574: debug_brace_level(level);
! 575: }
! 576:
1.91 rillig 577: /*
1.119 rillig 578: * A sub-object of an array is initialized using a designator. This does not
579: * have to be an array element directly, it can also be used to initialize
580: * only a sub-object of the array element.
1.91 rillig 581: *
582: * C99 example: struct { int member[4]; } var = { [2] = 12345 };
583: *
584: * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
1.119 rillig 585: *
586: * TODO: test the following initialization with an outer and an inner type:
587: *
588: * .deeply[0].nested = {
589: * .deeply[1].nested = {
590: * 12345,
591: * },
592: * }
1.91 rillig 593: */
594: void
1.131 rillig 595: designation_add_subscript(range_t range)
1.91 rillig 596: {
1.139 rillig 597: struct brace_level *level;
1.132 rillig 598:
1.91 rillig 599: debug_enter();
1.142 rillig 600: if (range.lo == range.hi)
601: debug_step("subscript is %zu", range.hi);
602: else
603: debug_step("subscript range is %zu ... %zu",
604: range.lo, range.hi);
1.132 rillig 605:
1.133 rillig 606: initstack_pop_nobrace();
607:
1.139 rillig 608: level = brace_level_lvalue;
609: if (level->bl_array_of_unknown_size) {
1.133 rillig 610: /* No +1 here, extend_if_array_of_unknown_size will add it. */
611: int auto_dim = (int)range.hi;
1.142 rillig 612: if (auto_dim > level->bl_type->t_dim)
613: brace_level_set_array_dimension(auto_dim);
1.132 rillig 614: }
615:
1.91 rillig 616: debug_leave();
617: }
618:
1.119 rillig 619: /* TODO: add support for array subscripts, not only named members */
1.90 rillig 620: static void
1.131 rillig 621: designation_shift_level(void)
1.90 rillig 622: {
1.142 rillig 623: struct designation *des = current_designation_mod();
624: lint_assert(des->head != NULL);
625:
626: if (des->head == des->tail) {
627: designator_free(des->head);
628: des->head = NULL;
629: des->tail = NULL;
1.90 rillig 630: } else {
1.142 rillig 631: struct designator *head = des->head;
632: des->head = des->head->next;
633: designator_free(head);
1.90 rillig 634: }
1.106 rillig 635:
1.126 rillig 636: debug_designation();
1.90 rillig 637: }
638:
1.1 cgd 639: /*
1.89 rillig 640: * Initialize the initialization stack by putting an entry for the object
1.1 cgd 641: * which is to be initialized on it.
1.119 rillig 642: *
643: * TODO: merge into begin_initialization
1.1 cgd 644: */
645: void
1.35 rillig 646: initstack_init(void)
1.1 cgd 647: {
648:
649: if (initerr)
650: return;
651:
1.70 rillig 652: debug_enter();
1.38 rillig 653:
1.1 cgd 654: /*
1.77 rillig 655: * If the type which is to be initialized is an incomplete array,
1.1 cgd 656: * it must be duplicated.
657: */
1.65 rillig 658: if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
1.1 cgd 659: initsym->s_type = duptyp(initsym->s_type);
1.119 rillig 660: /* TODO: does 'duptyp' create a memory leak? */
1.1 cgd 661:
1.143 rillig 662: brace_level_lvalue = brace_level_new(NULL, initsym->s_type, 1);
1.70 rillig 663:
1.73 rillig 664: debug_initstack();
1.70 rillig 665: debug_leave();
1.1 cgd 666: }
667:
1.119 rillig 668: /* TODO: document me */
1.1 cgd 669: static void
1.137 rillig 670: initstack_pop_item_named_member(const char *name)
1.1 cgd 671: {
1.139 rillig 672: struct brace_level *level = brace_level_lvalue;
1.143 rillig 673: const sym_t *m;
1.1 cgd 674:
1.119 rillig 675: /*
676: * TODO: fix wording of the debug message; this doesn't seem to be
677: * related to initializing the named member.
678: */
1.137 rillig 679: debug_step("initializing named member '%s'", name);
1.68 rillig 680:
1.139 rillig 681: if (level->bl_type->t_tspec != STRUCT &&
682: level->bl_type->t_tspec != UNION) {
1.104 rillig 683: /* syntax error '%s' */
684: error(249, "named member must only be used with struct/union");
1.141 rillig 685: set_initerr();
1.104 rillig 686: return;
687: }
688:
1.143 rillig 689: m = brace_level_look_up_member(name);
690: if (m == NULL) {
691: /* TODO: add type information to the message */
692: /* undefined struct/union member: %s */
693: error(101, name);
1.40 rillig 694:
1.143 rillig 695: designation_shift_level();
696: level->bl_seen_named_member = true;
697: return;
1.101 rillig 698: }
1.40 rillig 699:
1.143 rillig 700: debug_step("found matching member");
701: level->bl_subtype = m->s_type;
702: /* XXX: why ++? */
703: level->bl_remaining++;
704: /* XXX: why is bl_seen_named_member not set? */
1.131 rillig 705: designation_shift_level();
1.101 rillig 706: }
1.80 rillig 707:
1.119 rillig 708: /* TODO: think of a better name than 'pop' */
1.101 rillig 709: static void
710: initstack_pop_item_unnamed(void)
711: {
1.139 rillig 712: struct brace_level *level = brace_level_lvalue;
1.80 rillig 713:
1.1 cgd 714: /*
715: * If the removed element was a structure member, we must go
716: * to the next structure member.
717: */
1.139 rillig 718: if (level->bl_remaining > 0 && level->bl_type->t_tspec == STRUCT &&
719: !level->bl_seen_named_member) {
1.144 ! rillig 720: brace_level_next_member(level);
! 721: level->bl_subtype = level->bl_next_member->s_type;
1.1 cgd 722: }
1.101 rillig 723: }
724:
1.119 rillig 725: /* TODO: think of a better name than 'pop' */
1.101 rillig 726: static void
727: initstack_pop_item(void)
728: {
1.139 rillig 729: struct brace_level *level;
1.140 rillig 730: struct designator *first_designator;
1.101 rillig 731:
732: debug_enter();
733:
1.139 rillig 734: level = brace_level_lvalue;
1.120 rillig 735: debug_indent();
736: debug_printf("popping: ");
1.139 rillig 737: debug_brace_level(level);
1.101 rillig 738:
1.139 rillig 739: brace_level_lvalue = level->bl_enclosing;
740: free(level);
741: level = brace_level_lvalue;
742: lint_assert(level != NULL);
743:
744: level->bl_remaining--;
745: lint_assert(level->bl_remaining >= 0);
746: debug_step("%d elements remaining", level->bl_remaining);
1.101 rillig 747:
1.137 rillig 748: first_designator = current_designation().head;
749: if (first_designator != NULL && first_designator->name != NULL)
750: initstack_pop_item_named_member(first_designator->name);
1.101 rillig 751: else
752: initstack_pop_item_unnamed();
753:
1.73 rillig 754: debug_initstack();
1.68 rillig 755: debug_leave();
1.1 cgd 756: }
757:
1.36 rillig 758: /*
759: * Take all entries, including the first which requires a closing brace,
760: * from the stack.
761: */
762: static void
763: initstack_pop_brace(void)
764: {
1.62 rillig 765: bool brace;
1.36 rillig 766:
1.68 rillig 767: debug_enter();
1.73 rillig 768: debug_initstack();
1.36 rillig 769: do {
1.139 rillig 770: brace = brace_level_rvalue->bl_brace;
1.119 rillig 771: /* TODO: improve wording of the debug message */
1.68 rillig 772: debug_step("loop brace=%d", brace);
1.36 rillig 773: initstack_pop_item();
774: } while (!brace);
1.73 rillig 775: debug_initstack();
1.68 rillig 776: debug_leave();
1.36 rillig 777: }
778:
779: /*
780: * Take all entries which cannot be used for further initializers from the
781: * stack, but do this only if they do not require a closing brace.
782: */
1.119 rillig 783: /* TODO: think of a better name than 'pop' */
1.1 cgd 784: static void
1.36 rillig 785: initstack_pop_nobrace(void)
1.1 cgd 786: {
1.7 lukem 787:
1.68 rillig 788: debug_enter();
1.139 rillig 789: while (!brace_level_rvalue->bl_brace &&
790: brace_level_rvalue->bl_remaining == 0 &&
791: !brace_level_rvalue->bl_array_of_unknown_size)
1.36 rillig 792: initstack_pop_item();
1.68 rillig 793: debug_leave();
1.1 cgd 794: }
795:
1.97 rillig 796: /* Extend an array of unknown size by one element */
797: static void
798: extend_if_array_of_unknown_size(void)
799: {
1.139 rillig 800: struct brace_level *level = brace_level_lvalue;
1.97 rillig 801:
1.139 rillig 802: if (level->bl_remaining != 0)
1.97 rillig 803: return;
1.133 rillig 804: /*
805: * XXX: According to the function name, there should be a 'return' if
1.139 rillig 806: * bl_array_of_unknown_size is false. There's probably a test missing
1.133 rillig 807: * for that case.
808: */
1.97 rillig 809:
810: /*
811: * The only place where an incomplete array may appear is at the
812: * outermost aggregate level of the object to be initialized.
813: */
1.139 rillig 814: lint_assert(level->bl_enclosing->bl_enclosing == NULL);
815: lint_assert(level->bl_type->t_tspec == ARRAY);
1.97 rillig 816:
817: debug_step("extending array of unknown size '%s'",
1.139 rillig 818: type_name(level->bl_type));
819: level->bl_remaining = 1;
820: level->bl_type->t_dim++;
821: setcomplete(level->bl_type, true);
1.97 rillig 822:
1.139 rillig 823: debug_step("extended type is '%s'", type_name(level->bl_type));
1.97 rillig 824: }
825:
1.119 rillig 826: /* TODO: document me */
827: /* TODO: think of a better name than 'push' */
1.1 cgd 828: static void
1.99 rillig 829: initstack_push_array(void)
830: {
1.139 rillig 831: struct brace_level *level = brace_level_lvalue;
1.99 rillig 832:
1.139 rillig 833: if (level->bl_enclosing->bl_seen_named_member) {
834: level->bl_brace = true;
1.124 rillig 835: debug_step("ARRAY%s%s",
1.139 rillig 836: level->bl_brace ? ", needs closing brace" : "",
837: /* TODO: this is redundant, always true */
838: level->bl_enclosing->bl_seen_named_member
1.124 rillig 839: ? ", seen named member" : "");
1.99 rillig 840: }
841:
1.139 rillig 842: if (is_incomplete(level->bl_type) &&
843: level->bl_enclosing->bl_enclosing != NULL) {
1.99 rillig 844: /* initialization of an incomplete type */
845: error(175);
1.141 rillig 846: set_initerr();
1.99 rillig 847: return;
848: }
849:
1.139 rillig 850: level->bl_subtype = level->bl_type->t_subt;
851: level->bl_array_of_unknown_size = is_incomplete(level->bl_type);
852: level->bl_remaining = level->bl_type->t_dim;
1.126 rillig 853: debug_designation();
1.99 rillig 854: debug_step("type '%s' remaining %d",
1.139 rillig 855: type_name(level->bl_type), level->bl_remaining);
1.99 rillig 856: }
857:
1.138 rillig 858: static sym_t *
1.139 rillig 859: look_up_member(struct brace_level *level, int *count)
1.99 rillig 860: {
861: sym_t *m;
862:
1.139 rillig 863: for (m = level->bl_type->t_str->sou_first_member;
1.99 rillig 864: m != NULL; m = m->s_next) {
865: if (m->s_bitfield && m->s_name == unnamed)
866: continue;
1.119 rillig 867: /*
868: * TODO: split into separate functions:
869: *
870: * look_up_array_next
871: * look_up_array_designator
872: * look_up_struct_next
873: * look_up_struct_designator
874: */
1.128 rillig 875: if (current_designation().head != NULL) {
1.119 rillig 876: /* XXX: this log entry looks unnecessarily verbose */
1.100 rillig 877: debug_step("have member '%s', want member '%s'",
1.128 rillig 878: m->s_name, current_designation().head->name);
879: if (strcmp(m->s_name,
880: current_designation().head->name) == 0) {
1.138 rillig 881: (*count)++;
1.99 rillig 882: break;
883: } else
884: continue;
885: }
1.138 rillig 886: if (++(*count) == 1) {
1.139 rillig 887: level->bl_next_member = m;
888: level->bl_subtype = m->s_type;
1.99 rillig 889: }
890: }
1.100 rillig 891:
1.138 rillig 892: return m;
893: }
894:
895: /* TODO: document me */
896: /* TODO: think of a better name than 'push' */
897: static bool
898: initstack_push_struct_or_union(void)
899: {
900: /*
901: * TODO: remove unnecessary 'const' for variables in functions that
902: * fit on a single screen. Keep it for larger functions.
903: */
1.139 rillig 904: struct brace_level *level = brace_level_lvalue;
1.138 rillig 905: int cnt;
906: sym_t *m;
907:
1.139 rillig 908: if (is_incomplete(level->bl_type)) {
1.138 rillig 909: /* initialization of an incomplete type */
910: error(175);
1.141 rillig 911: set_initerr();
1.138 rillig 912: return false;
913: }
914:
915: cnt = 0;
916: debug_designation();
917: debug_step("lookup for '%s'%s",
1.139 rillig 918: type_name(level->bl_type),
919: level->bl_seen_named_member ? ", seen named member" : "");
1.138 rillig 920:
1.139 rillig 921: m = look_up_member(level, &cnt);
1.138 rillig 922:
1.128 rillig 923: if (current_designation().head != NULL) {
1.99 rillig 924: if (m == NULL) {
925: debug_step("pop struct");
926: return true;
927: }
1.139 rillig 928: level->bl_next_member = m;
929: level->bl_subtype = m->s_type;
930: level->bl_seen_named_member = true;
1.128 rillig 931: debug_step("named member '%s'",
932: current_designation().head->name);
1.131 rillig 933: designation_shift_level();
1.139 rillig 934: cnt = level->bl_type->t_tspec == STRUCT ? 2 : 1;
1.99 rillig 935: }
1.139 rillig 936: level->bl_brace = true;
1.99 rillig 937: debug_step("unnamed element with type '%s'%s",
1.139 rillig 938: type_name(
939: level->bl_type != NULL ? level->bl_type : level->bl_subtype),
940: level->bl_brace ? ", needs closing brace" : "");
1.99 rillig 941: if (cnt == 0) {
942: /* cannot init. struct/union with no named member */
943: error(179);
1.141 rillig 944: set_initerr();
1.99 rillig 945: return false;
946: }
1.139 rillig 947: level->bl_remaining = level->bl_type->t_tspec == STRUCT ? cnt : 1;
1.99 rillig 948: return false;
949: }
950:
1.119 rillig 951: /* TODO: document me */
952: /* TODO: think of a better name than 'push' */
1.99 rillig 953: static void
1.35 rillig 954: initstack_push(void)
1.1 cgd 955: {
1.139 rillig 956: struct brace_level *level, *enclosing;
1.1 cgd 957:
1.68 rillig 958: debug_enter();
959:
1.97 rillig 960: extend_if_array_of_unknown_size();
961:
1.139 rillig 962: level = brace_level_lvalue;
963: lint_assert(level->bl_remaining > 0);
964: lint_assert(level->bl_type == NULL ||
965: !is_scalar(level->bl_type->t_tspec));
966:
967: brace_level_lvalue = xcalloc(1, sizeof *brace_level_lvalue);
968: brace_level_lvalue->bl_enclosing = level;
969: brace_level_lvalue->bl_type = level->bl_subtype;
970: lint_assert(brace_level_lvalue->bl_type->t_tspec != FUNC);
1.1 cgd 971:
1.12 christos 972: again:
1.139 rillig 973: level = brace_level_lvalue;
1.1 cgd 974:
1.139 rillig 975: debug_step("expecting type '%s'", type_name(level->bl_type));
976: lint_assert(level->bl_type != NULL);
977: switch (level->bl_type->t_tspec) {
1.1 cgd 978: case ARRAY:
1.128 rillig 979: if (current_designation().head != NULL) {
1.124 rillig 980: debug_step("pop array, named member '%s'%s",
1.128 rillig 981: current_designation().head->name,
1.139 rillig 982: level->bl_brace ? ", needs closing brace" : "");
1.19 christos 983: goto pop;
1.98 rillig 984: }
985:
1.99 rillig 986: initstack_push_array();
987: break;
1.20 christos 988:
1.1 cgd 989: case UNION:
990: if (tflag)
1.89 rillig 991: /* initialization of union is illegal in trad. C */
1.1 cgd 992: warning(238);
993: /* FALLTHROUGH */
994: case STRUCT:
1.99 rillig 995: if (initstack_push_struct_or_union())
996: goto pop;
1.1 cgd 997: break;
998: default:
1.128 rillig 999: if (current_designation().head != NULL) {
1.100 rillig 1000: debug_step("pop scalar");
1.19 christos 1001: pop:
1.119 rillig 1002: /* TODO: extract this into end_initializer_level */
1.139 rillig 1003: enclosing = brace_level_rvalue->bl_enclosing;
1004: free(level);
1005: brace_level_lvalue = enclosing;
1.12 christos 1006: goto again;
1007: }
1.100 rillig 1008: /* The initialization stack now expects a single scalar. */
1.139 rillig 1009: level->bl_remaining = 1;
1.1 cgd 1010: break;
1011: }
1.68 rillig 1012:
1.73 rillig 1013: debug_initstack();
1.68 rillig 1014: debug_leave();
1.1 cgd 1015: }
1016:
1017: static void
1.84 rillig 1018: check_too_many_initializers(void)
1.1 cgd 1019: {
1.139 rillig 1020: const struct brace_level *level = brace_level_rvalue;
1021: if (level->bl_remaining > 0)
1.84 rillig 1022: return;
1.119 rillig 1023: /*
1024: * FIXME: even with named members, there can be too many initializers
1025: */
1.139 rillig 1026: if (level->bl_array_of_unknown_size || level->bl_seen_named_member)
1.84 rillig 1027: return;
1.1 cgd 1028:
1.139 rillig 1029: tspec_t t = level->bl_type->t_tspec;
1.84 rillig 1030: if (t == ARRAY) {
1031: /* too many array initializers, expected %d */
1.139 rillig 1032: error(173, level->bl_type->t_dim);
1.84 rillig 1033: } else if (t == STRUCT || t == UNION) {
1034: /* too many struct/union initializers */
1035: error(172);
1036: } else {
1037: /* too many initializers */
1038: error(174);
1.1 cgd 1039: }
1.141 rillig 1040: set_initerr();
1.1 cgd 1041: }
1042:
1.92 rillig 1043: /*
1044: * Process a '{' in an initializer by starting the initialization of the
1.139 rillig 1045: * nested data structure, with bl_type being the bl_subtype of the outer
1.92 rillig 1046: * initialization level.
1047: */
1.1 cgd 1048: static void
1.37 rillig 1049: initstack_next_brace(void)
1.1 cgd 1050: {
1.7 lukem 1051:
1.68 rillig 1052: debug_enter();
1.73 rillig 1053: debug_initstack();
1.68 rillig 1054:
1.139 rillig 1055: if (brace_level_rvalue->bl_type != NULL &&
1056: is_scalar(brace_level_rvalue->bl_type->t_tspec)) {
1.49 rillig 1057: /* invalid initializer type %s */
1.139 rillig 1058: error(176, type_name(brace_level_rvalue->bl_type));
1.141 rillig 1059: set_initerr();
1.37 rillig 1060: }
1061: if (!initerr)
1.84 rillig 1062: check_too_many_initializers();
1.37 rillig 1063: if (!initerr)
1064: initstack_push();
1065: if (!initerr) {
1.139 rillig 1066: brace_level_lvalue->bl_brace = true;
1.126 rillig 1067: debug_designation();
1.92 rillig 1068: debug_step("expecting type '%s'",
1.139 rillig 1069: type_name(brace_level_rvalue->bl_type != NULL
1070: ? brace_level_rvalue->bl_type
1071: : brace_level_rvalue->bl_subtype));
1.37 rillig 1072: }
1.68 rillig 1073:
1.73 rillig 1074: debug_initstack();
1.68 rillig 1075: debug_leave();
1.37 rillig 1076: }
1077:
1.119 rillig 1078: /* TODO: document me, or think of a better name */
1.37 rillig 1079: static void
1.118 rillig 1080: initstack_next_nobrace(tnode_t *tn)
1.37 rillig 1081: {
1.68 rillig 1082: debug_enter();
1.37 rillig 1083:
1.139 rillig 1084: if (brace_level_rvalue->bl_type == NULL &&
1085: !is_scalar(brace_level_rvalue->bl_subtype->t_tspec)) {
1.37 rillig 1086: /* {}-enclosed initializer required */
1087: error(181);
1.92 rillig 1088: /* XXX: maybe set initerr here */
1.37 rillig 1089: }
1090:
1.84 rillig 1091: if (!initerr)
1092: check_too_many_initializers();
1093:
1.42 rillig 1094: while (!initerr) {
1.139 rillig 1095: struct brace_level *level = brace_level_lvalue;
1.118 rillig 1096:
1097: if (tn->tn_type->t_tspec == STRUCT &&
1.139 rillig 1098: level->bl_type == tn->tn_type &&
1099: level->bl_enclosing != NULL &&
1100: level->bl_enclosing->bl_enclosing != NULL) {
1101: level->bl_brace = false;
1102: level->bl_remaining = 1; /* the struct itself */
1.118 rillig 1103: break;
1104: }
1105:
1.139 rillig 1106: if (level->bl_type != NULL &&
1107: is_scalar(level->bl_type->t_tspec))
1.42 rillig 1108: break;
1109: initstack_push();
1.1 cgd 1110: }
1.68 rillig 1111:
1.73 rillig 1112: debug_initstack();
1.68 rillig 1113: debug_leave();
1.1 cgd 1114: }
1115:
1.119 rillig 1116: /* TODO: document me */
1.1 cgd 1117: void
1.34 rillig 1118: init_lbrace(void)
1.1 cgd 1119: {
1120: if (initerr)
1121: return;
1122:
1.68 rillig 1123: debug_enter();
1.73 rillig 1124: debug_initstack();
1.68 rillig 1125:
1.1 cgd 1126: if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
1.139 rillig 1127: brace_level_rvalue->bl_enclosing == NULL) {
1128: if (tflag &&
1129: !is_scalar(brace_level_rvalue->bl_subtype->t_tspec))
1.50 rillig 1130: /* no automatic aggregate initialization in trad. C */
1.1 cgd 1131: warning(188);
1132: }
1133:
1134: /*
1135: * Remove all entries which cannot be used for further initializers
1136: * and do not expect a closing brace.
1137: */
1.36 rillig 1138: initstack_pop_nobrace();
1.1 cgd 1139:
1.37 rillig 1140: initstack_next_brace();
1.68 rillig 1141:
1.73 rillig 1142: debug_initstack();
1.68 rillig 1143: debug_leave();
1.1 cgd 1144: }
1145:
1.92 rillig 1146: /*
1147: * Process a '}' in an initializer by finishing the current level of the
1148: * initialization stack.
1149: */
1.1 cgd 1150: void
1.34 rillig 1151: init_rbrace(void)
1.1 cgd 1152: {
1153: if (initerr)
1154: return;
1155:
1.68 rillig 1156: debug_enter();
1.36 rillig 1157: initstack_pop_brace();
1.68 rillig 1158: debug_leave();
1.1 cgd 1159: }
1160:
1.86 rillig 1161: /* In traditional C, bit-fields can be initialized only by integer constants. */
1162: static void
1163: check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
1164: {
1165: if (tflag &&
1166: is_integer(lt) &&
1167: ln->tn_type->t_bitfield &&
1168: !is_integer(rt)) {
1.89 rillig 1169: /* bit-field initialization is illegal in traditional C */
1.86 rillig 1170: warning(186);
1171: }
1172: }
1173:
1.87 rillig 1174: static void
1175: check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
1176: {
1.119 rillig 1177: /* TODO: rename CON to CONSTANT to avoid ambiguity with CONVERT */
1.87 rillig 1178: if (tn == NULL || tn->tn_op == CON)
1179: return;
1180:
1181: sym_t *sym;
1182: ptrdiff_t offs;
1183: if (constant_addr(tn, &sym, &offs))
1184: return;
1185:
1186: if (sclass == AUTO || sclass == REG) {
1187: /* non-constant initializer */
1188: c99ism(177);
1189: } else {
1190: /* non-constant initializer */
1191: error(177);
1192: }
1193: }
1194:
1.114 rillig 1195: /*
1.119 rillig 1196: * Initialize a non-array object with automatic storage duration and only a
1197: * single initializer expression without braces by delegating to ASSIGN.
1.114 rillig 1198: */
1199: static bool
1200: init_using_assign(tnode_t *rn)
1201: {
1202: tnode_t *ln, *tn;
1203:
1204: if (initsym->s_type->t_tspec == ARRAY)
1205: return false;
1.139 rillig 1206: if (brace_level_rvalue->bl_enclosing != NULL)
1.114 rillig 1207: return false;
1208:
1209: debug_step("handing over to ASSIGN");
1.119 rillig 1210:
1.114 rillig 1211: ln = new_name_node(initsym, 0);
1212: ln->tn_type = tduptyp(ln->tn_type);
1213: ln->tn_type->t_const = false;
1.119 rillig 1214:
1.114 rillig 1215: tn = build(ASSIGN, ln, rn);
1216: expr(tn, false, false, false, false);
1.119 rillig 1217:
1.114 rillig 1218: /* XXX: why not clean up the initstack here already? */
1219: return true;
1220: }
1221:
1.116 rillig 1222: static void
1223: check_init_expr(tnode_t *tn, scl_t sclass)
1224: {
1225: tnode_t *ln;
1226: tspec_t lt, rt;
1227: struct mbl *tmem;
1228:
1229: /* Create a temporary node for the left side. */
1.134 rillig 1230: ln = tgetblk(sizeof *ln);
1.116 rillig 1231: ln->tn_op = NAME;
1.139 rillig 1232: ln->tn_type = tduptyp(brace_level_rvalue->bl_type);
1.116 rillig 1233: ln->tn_type->t_const = false;
1234: ln->tn_lvalue = true;
1235: ln->tn_sym = initsym; /* better than nothing */
1236:
1237: tn = cconv(tn);
1238:
1239: lt = ln->tn_type->t_tspec;
1240: rt = tn->tn_type->t_tspec;
1241:
1242: debug_step("typeok '%s', '%s'",
1243: type_name(ln->tn_type), type_name(tn->tn_type));
1244: if (!typeok(INIT, 0, ln, tn))
1245: return;
1246:
1247: /*
1.119 rillig 1248: * Preserve the tree memory. This is necessary because otherwise
1.116 rillig 1249: * expr() would free it.
1250: */
1251: tmem = tsave();
1252: expr(tn, true, false, true, false);
1253: trestor(tmem);
1254:
1255: check_bit_field_init(ln, lt, rt);
1256:
1257: /*
1258: * XXX: Is it correct to do this conversion _after_ the typeok above?
1259: */
1.139 rillig 1260: if (lt != rt ||
1261: (brace_level_rvalue->bl_type->t_bitfield && tn->tn_op == CON))
1262: tn = convert(INIT, 0, brace_level_rvalue->bl_type, tn);
1.116 rillig 1263:
1264: check_non_constant_initializer(tn, sclass);
1265: }
1266:
1.1 cgd 1267: void
1.69 rillig 1268: init_using_expr(tnode_t *tn)
1.1 cgd 1269: {
1.81 rillig 1270: scl_t sclass;
1.1 cgd 1271:
1.68 rillig 1272: debug_enter();
1.92 rillig 1273: debug_initstack();
1.126 rillig 1274: debug_designation();
1.95 rillig 1275: debug_step("expr:");
1276: debug_node(tn, debug_ind + 1);
1.55 rillig 1277:
1.113 rillig 1278: if (initerr || tn == NULL)
1279: goto done;
1.1 cgd 1280:
1.81 rillig 1281: sclass = initsym->s_scl;
1.114 rillig 1282: if ((sclass == AUTO || sclass == REG) && init_using_assign(tn))
1.113 rillig 1283: goto done;
1.1 cgd 1284:
1.36 rillig 1285: initstack_pop_nobrace();
1.1 cgd 1286:
1.82 rillig 1287: if (init_array_using_string(tn)) {
1288: debug_step("after initializing the string:");
1289: /* XXX: why not clean up the initstack here already? */
1.113 rillig 1290: goto done_initstack;
1.68 rillig 1291: }
1.1 cgd 1292:
1.118 rillig 1293: initstack_next_nobrace(tn);
1.113 rillig 1294: if (initerr || tn == NULL)
1295: goto done_initstack;
1.1 cgd 1296:
1.139 rillig 1297: brace_level_lvalue->bl_remaining--;
1298: debug_step("%d elements remaining", brace_level_rvalue->bl_remaining);
1.83 rillig 1299:
1.116 rillig 1300: check_init_expr(tn, sclass);
1.68 rillig 1301:
1.113 rillig 1302: done_initstack:
1.73 rillig 1303: debug_initstack();
1.130 rillig 1304:
1.113 rillig 1305: done:
1.130 rillig 1306: while (current_designation().head != NULL)
1.131 rillig 1307: designation_shift_level();
1.130 rillig 1308:
1.68 rillig 1309: debug_leave();
1.1 cgd 1310: }
1311:
1312:
1.82 rillig 1313: /* Initialize a character array or wchar_t array with a string literal. */
1.62 rillig 1314: static bool
1.82 rillig 1315: init_array_using_string(tnode_t *tn)
1.1 cgd 1316: {
1317: tspec_t t;
1.139 rillig 1318: struct brace_level *level;
1.1 cgd 1319: int len;
1320: strg_t *strg;
1321:
1322: if (tn->tn_op != STRING)
1.63 rillig 1323: return false;
1.1 cgd 1324:
1.68 rillig 1325: debug_enter();
1.73 rillig 1326: debug_initstack();
1.68 rillig 1327:
1.139 rillig 1328: level = brace_level_lvalue;
1.47 rillig 1329: strg = tn->tn_string;
1.1 cgd 1330:
1331: /*
1332: * Check if we have an array type which can be initialized by
1333: * the string.
1334: */
1.139 rillig 1335: if (level->bl_subtype != NULL && level->bl_subtype->t_tspec == ARRAY) {
1.68 rillig 1336: debug_step("subt array");
1.139 rillig 1337: t = level->bl_subtype->t_subt->t_tspec;
1.1 cgd 1338: if (!((strg->st_tspec == CHAR &&
1339: (t == CHAR || t == UCHAR || t == SCHAR)) ||
1340: (strg->st_tspec == WCHAR && t == WCHAR))) {
1.68 rillig 1341: debug_leave();
1.63 rillig 1342: return false;
1.1 cgd 1343: }
1.82 rillig 1344: /* XXX: duplicate code, see below */
1.119 rillig 1345:
1.1 cgd 1346: /* Put the array at top of stack */
1.35 rillig 1347: initstack_push();
1.139 rillig 1348: level = brace_level_lvalue;
1.119 rillig 1349:
1.139 rillig 1350: /* TODO: what if both bl_type and bl_subtype are ARRAY? */
1.119 rillig 1351:
1.139 rillig 1352: } else if (level->bl_type != NULL && level->bl_type->t_tspec == ARRAY) {
1.68 rillig 1353: debug_step("type array");
1.139 rillig 1354: t = level->bl_type->t_subt->t_tspec;
1.1 cgd 1355: if (!((strg->st_tspec == CHAR &&
1356: (t == CHAR || t == UCHAR || t == SCHAR)) ||
1357: (strg->st_tspec == WCHAR && t == WCHAR))) {
1.68 rillig 1358: debug_leave();
1.63 rillig 1359: return false;
1.1 cgd 1360: }
1.82 rillig 1361: /* XXX: duplicate code, see above */
1.119 rillig 1362:
1363: /*
1364: * TODO: is this really not needed in the branch above this
1365: * one?
1366: */
1.1 cgd 1367: /*
1368: * If the array is already partly initialized, we are
1369: * wrong here.
1370: */
1.139 rillig 1371: if (level->bl_remaining != level->bl_type->t_dim) {
1.68 rillig 1372: debug_leave();
1.63 rillig 1373: return false;
1.115 rillig 1374: }
1.1 cgd 1375: } else {
1.68 rillig 1376: debug_leave();
1.63 rillig 1377: return false;
1.1 cgd 1378: }
1379:
1380: /* Get length without trailing NUL character. */
1381: len = strg->st_len;
1382:
1.139 rillig 1383: if (level->bl_array_of_unknown_size) {
1384: level->bl_array_of_unknown_size = false;
1385: level->bl_type->t_dim = len + 1;
1386: setcomplete(level->bl_type, true);
1.1 cgd 1387: } else {
1.119 rillig 1388: /*
1389: * TODO: check for buffer overflow in the object to be
1390: * initialized
1391: */
1392: /* XXX: double-check for off-by-one error */
1.139 rillig 1393: if (level->bl_type->t_dim < len) {
1.1 cgd 1394: /* non-null byte ignored in string initializer */
1395: warning(187);
1396: }
1.119 rillig 1397:
1398: /*
1399: * TODO: C99 6.7.8p14 allows a string literal to be enclosed
1400: * in optional redundant braces, just like scalars. Add tests
1401: * for this.
1402: */
1.1 cgd 1403: }
1404:
1405: /* In every case the array is initialized completely. */
1.139 rillig 1406: level->bl_remaining = 0;
1.1 cgd 1407:
1.73 rillig 1408: debug_initstack();
1.68 rillig 1409: debug_leave();
1.63 rillig 1410: return true;
1.1 cgd 1411: }
CVSweb <webmaster@jp.NetBSD.org>