Annotation of src/usr.bin/xlint/lint1/init.c, Revision 1.137
1.137 ! rillig 1: /* $NetBSD: init.c,v 1.136 2021/03/27 13:17:42 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.137 ! rillig 40: __RCSID("$NetBSD: init.c,v 1.136 2021/03/27 13:17:42 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: *
81: * The state of the current initialization is stored in initstk, a stack of
1.94 rillig 82: * initstack_element, one element per type aggregate level.
1.119 rillig 83: * XXX: Or is that "one element per brace level"? C99 mandates in 6.7.8p17
84: * that "each brace-enclosed initializer list has an associated current
85: * object".
1.93 rillig 86: *
1.94 rillig 87: * Most of the time, the topmost level of initstk contains a scalar type, and
88: * its remaining count toggles between 1 and 0.
1.93 rillig 89: *
90: * See also:
91: * C99 6.7.8 "Initialization"
92: * d_c99_init.c for more examples
93: */
94:
95:
96: /*
1.136 rillig 97: * Describes a single level of an ongoing initialization.
1.54 rillig 98: *
1.119 rillig 99: * XXX: Since C99, the initializers can be listed in arbitrary order by using
100: * designators to specify the sub-object to be initialized. The member names
1.54 rillig 101: * of non-leaf structs may thus appear repeatedly, as demonstrated in
102: * d_init_pop_member.c.
103: *
104: * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
105: * selected examples.
1.53 rillig 106: */
1.79 rillig 107: typedef struct initstack_element {
1.77 rillig 108:
1.92 rillig 109: /*
110: * The type to be initialized at this level.
1.102 rillig 111: *
112: * On the outermost element, this is always NULL since the outermost
113: * initializer-expression may be enclosed in an optional pair of
1.119 rillig 114: * braces, as of the current implementation.
115: *
116: * FIXME: This approach is wrong. It's not that the outermost
117: * initializer may be enclosed in additional braces, it's every scalar
118: * that may be enclosed in additional braces, as of C99 6.7.8p11.
1.102 rillig 119: *
120: * Everywhere else it is nonnull.
1.92 rillig 121: */
122: type_t *i_type;
1.102 rillig 123:
1.92 rillig 124: /*
1.102 rillig 125: * The type that will be initialized at the next initialization level,
126: * usually enclosed by another pair of braces.
1.92 rillig 127: *
1.102 rillig 128: * For an array, it is the element type, but without 'const'.
129: *
130: * For a struct or union type, it is one of the member types, but
131: * without 'const'.
132: *
133: * The outermost stack element has no i_type but nevertheless has
134: * i_subt. For example, in 'int var = { 12345 }', initially there is
135: * an initstack_element with i_subt 'int'. When the '{' is processed,
136: * an element with i_type 'int' is pushed to the stack. When the
1.92 rillig 137: * corresponding '}' is processed, the inner element is popped again.
138: *
139: * During initialization, only the top 2 elements of the stack are
140: * looked at.
1.119 rillig 141: *
142: * XXX: Having i_subt here is the wrong approach, it should not be
143: * necessary at all; see i_type.
1.92 rillig 144: */
145: type_t *i_subt;
1.77 rillig 146:
1.82 rillig 147: /*
1.119 rillig 148: * Whether this level of the initializer requires a '}' to be
149: * completed.
1.82 rillig 150: *
151: * Multidimensional arrays do not need a closing brace to complete
152: * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
1.119 rillig 153: * for 'int arr[2][2]'.
1.82 rillig 154: *
155: * TODO: Do structs containing structs need a closing brace?
156: * TODO: Do arrays of structs need a closing brace after each struct?
1.119 rillig 157: *
158: * XXX: Double-check whether this is the correct approach at all; see
159: * i_type.
1.82 rillig 160: */
1.77 rillig 161: bool i_brace: 1;
1.82 rillig 162:
1.93 rillig 163: /* Whether i_type is an array of unknown size. */
1.77 rillig 164: bool i_array_of_unknown_size: 1;
1.119 rillig 165:
166: /*
167: * XXX: This feels wrong. Whether or not there has been a named
168: * initializer (called 'designation' since C99) should not matter at
169: * all. Even after an initializer with designation, counting of the
170: * remaining elements continues, see C99 6.7.8p17.
171: */
1.77 rillig 172: bool i_seen_named_member: 1;
173:
174: /*
1.125 rillig 175: * For structs, the next member to be initialized by a designator-less
176: * initializer.
1.77 rillig 177: */
1.125 rillig 178: sym_t *i_next_member;
179:
180: /* TODO: Add i_next_subscript for arrays. */
181:
182: /* TODO: Understand C99 6.7.8p17 and footnote 128 for unions. */
1.77 rillig 183:
184: /*
1.102 rillig 185: * The number of remaining elements to be used by expressions without
186: * designator.
187: *
188: * This says nothing about which members have been initialized or not
189: * since starting with C99, members may be initialized in arbitrary
190: * order by using designators.
1.77 rillig 191: *
1.93 rillig 192: * For an array of unknown size, this is always 0 and thus irrelevant.
193: *
1.77 rillig 194: * XXX: for scalars?
195: * XXX: for structs?
196: * XXX: for unions?
197: * XXX: for arrays?
1.119 rillig 198: *
199: * XXX: Having the count of remaining objects should not be necessary.
200: * It is probably clearer to use i_next_member and i_next_subscript
1.125 rillig 201: * for this purpose.
1.77 rillig 202: */
203: int i_remaining;
204:
205: /*
206: * The initialization state of the enclosing data structure
207: * (struct, union, array).
1.119 rillig 208: *
209: * XXX: Or for a scalar, for the top-level element, or for expressions
210: * in redundant braces such as '{{{{ 0 }}}}' (not yet implemented as
211: * of 2021-03-25).
1.77 rillig 212: */
1.79 rillig 213: struct initstack_element *i_enclosing;
1.119 rillig 214:
1.79 rillig 215: } initstack_element;
1.53 rillig 216:
1.55 rillig 217: /*
1.135 rillig 218: * A single component on the path to the sub-object that is initialized by an
219: * initializer expression. Either a struct or union member, or an array
220: * subscript.
1.55 rillig 221: *
1.128 rillig 222: * See also: C99 6.7.8 "Initialization"
223: */
224: typedef struct designator {
225: const char *name; /* for struct and union */
226: /* TODO: add 'subscript' for arrays */
227: struct designator *next;
228: } designator;
229:
230: /*
1.135 rillig 231: * The optional designation for an initializer, saying which sub-object to
232: * initialize. Examples for designations are '.member' or
233: * '.member[123].member.member[1][1]'.
234: *
1.128 rillig 235: * See also: C99 6.7.8 "Initialization"
1.55 rillig 236: */
1.128 rillig 237: typedef struct {
238: designator *head;
239: designator *tail;
240: } designation;
1.53 rillig 241:
1.110 rillig 242: struct initialization {
243: /*
1.111 rillig 244: * is set as soon as a fatal error occurred in the initialization.
245: * The effect is that the rest of the initialization is ignored
246: * (parsed by yacc, expression trees built, but no initialization
247: * takes place).
1.110 rillig 248: */
249: bool initerr;
250:
251: /* Pointer to the symbol which is to be initialized. */
252: sym_t *initsym;
1.53 rillig 253:
1.110 rillig 254: /* Points to the top element of the initialization stack. */
255: initstack_element *initstk;
1.1 cgd 256:
1.124 rillig 257: /*
258: * The C99 designator, if any, for the current initialization
259: * expression.
260: */
1.128 rillig 261: designation designation;
1.1 cgd 262:
1.110 rillig 263: struct initialization *next;
264: };
1.1 cgd 265:
1.111 rillig 266: static struct initialization *init;
1.12 christos 267:
1.1 cgd 268:
1.119 rillig 269: /* XXX: unnecessary prototype since it is not recursive */
1.82 rillig 270: static bool init_array_using_string(tnode_t *);
1.12 christos 271:
1.119 rillig 272:
1.135 rillig 273: static struct initialization *
274: current_init(void)
275: {
276: lint_assert(init != NULL);
277: return init;
278: }
1.119 rillig 279:
1.110 rillig 280: bool *
281: current_initerr(void)
282: {
1.135 rillig 283: return ¤t_init()->initerr;
1.110 rillig 284: }
285:
286: sym_t **
287: current_initsym(void)
288: {
1.135 rillig 289: return ¤t_init()->initsym;
1.110 rillig 290: }
291:
1.128 rillig 292: static designation *
1.123 rillig 293: current_designation_mod(void)
1.110 rillig 294: {
1.135 rillig 295: return ¤t_init()->designation;
1.110 rillig 296: }
297:
1.128 rillig 298: static designation
1.123 rillig 299: current_designation(void)
300: {
301: return *current_designation_mod();
302: }
303:
1.129 rillig 304: static const initstack_element *
305: current_initstk(void)
306: {
1.135 rillig 307: return current_init()->initstk;
1.129 rillig 308: }
309:
1.110 rillig 310: static initstack_element **
1.129 rillig 311: current_initstk_lvalue(void)
1.110 rillig 312: {
1.135 rillig 313: return ¤t_init()->initstk;
1.110 rillig 314: }
315:
1.121 rillig 316: static void
317: free_initialization(struct initialization *in)
318: {
319: initstack_element *el, *next;
320:
321: for (el = in->initstk; el != NULL; el = next) {
322: next = el->i_enclosing;
323: free(el);
324: }
325:
326: free(in);
327: }
328:
1.119 rillig 329: #define initerr (*current_initerr())
330: #define initsym (*current_initsym())
1.129 rillig 331: #define initstk (current_initstk())
332: #define initstk_lvalue (*current_initstk_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.90 rillig 342: #define debug_initstack_element(elem) do { } while (false)
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.128 rillig 392: const 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: *
406: * TODO: wrap all write accesses to initstack_element in setter functions
407: */
1.78 rillig 408: static void
1.79 rillig 409: debug_initstack_element(const initstack_element *elem)
1.74 rillig 410: {
1.78 rillig 411: if (elem->i_type != NULL)
1.120 rillig 412: debug_printf("type '%s'", type_name(elem->i_type));
413: if (elem->i_type != NULL && elem->i_subt != NULL)
414: debug_printf(", ");
1.78 rillig 415: if (elem->i_subt != NULL)
1.120 rillig 416: debug_printf("subtype '%s'", type_name(elem->i_subt));
1.78 rillig 417:
418: if (elem->i_brace)
1.120 rillig 419: debug_printf(", needs closing brace");
1.78 rillig 420: if (elem->i_array_of_unknown_size)
1.120 rillig 421: debug_printf(", array of unknown size");
1.78 rillig 422: if (elem->i_seen_named_member)
1.120 rillig 423: debug_printf(", seen named member");
1.78 rillig 424:
425: const type_t *eff_type = elem->i_type != NULL
426: ? elem->i_type : elem->i_subt;
1.125 rillig 427: if (eff_type->t_tspec == STRUCT && elem->i_next_member != NULL)
428: debug_printf(", next member '%s'",
429: elem->i_next_member->s_name);
1.78 rillig 430:
1.120 rillig 431: debug_printf(", remaining %d\n", elem->i_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: {
440: if (initstk == NULL) {
441: debug_step("initstk is empty");
442: return;
443: }
444:
445: size_t i = 0;
1.79 rillig 446: for (const initstack_element *elem = initstk;
1.77 rillig 447: elem != NULL; elem = elem->i_enclosing) {
1.120 rillig 448: debug_indent();
449: debug_printf("initstk[%zu]: ", i);
1.78 rillig 450: debug_initstack_element(elem);
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.90 rillig 484: void
1.131 rillig 485: designation_add_name(sbuf_t *sb)
1.90 rillig 486: {
1.128 rillig 487: designation *dd = current_designation_mod();
1.127 rillig 488:
1.128 rillig 489: designator *d = xcalloc(1, sizeof *d);
490: d->name = sb->sb_name;
1.90 rillig 491:
1.128 rillig 492: if (dd->head != NULL) {
493: dd->tail->next = d;
494: dd->tail = d;
1.90 rillig 495: } else {
1.128 rillig 496: dd->head = d;
497: dd->tail = d;
1.90 rillig 498: }
1.106 rillig 499:
1.126 rillig 500: debug_designation();
1.90 rillig 501: }
502:
1.133 rillig 503: /* TODO: Move the function body up here, to avoid the forward declaration. */
504: static void initstack_pop_nobrace(void);
505:
1.91 rillig 506: /*
1.119 rillig 507: * A sub-object of an array is initialized using a designator. This does not
508: * have to be an array element directly, it can also be used to initialize
509: * only a sub-object of the array element.
1.91 rillig 510: *
511: * C99 example: struct { int member[4]; } var = { [2] = 12345 };
512: *
513: * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
1.119 rillig 514: *
515: * TODO: test the following initialization with an outer and an inner type:
516: *
517: * .deeply[0].nested = {
518: * .deeply[1].nested = {
519: * 12345,
520: * },
521: * }
1.91 rillig 522: */
523: void
1.131 rillig 524: designation_add_subscript(range_t range)
1.91 rillig 525: {
1.132 rillig 526: initstack_element *istk;
527:
1.91 rillig 528: debug_enter();
529: debug_step("subscript range is %zu ... %zu", range.lo, range.hi);
530: debug_initstack();
1.132 rillig 531:
1.133 rillig 532: initstack_pop_nobrace();
533:
1.132 rillig 534: istk = initstk_lvalue;
535: if (istk->i_array_of_unknown_size) {
1.133 rillig 536: /* No +1 here, extend_if_array_of_unknown_size will add it. */
537: int auto_dim = (int)range.hi;
1.132 rillig 538: if (auto_dim > istk->i_type->t_dim) {
539: debug_step("setting the array size to %d", auto_dim);
540: istk->i_type->t_dim = auto_dim;
541: }
542: }
543:
1.91 rillig 544: debug_leave();
545: }
546:
1.119 rillig 547: /* TODO: add support for array subscripts, not only named members */
1.90 rillig 548: static void
1.131 rillig 549: designation_shift_level(void)
1.90 rillig 550: {
1.127 rillig 551: /* TODO: remove direct access to 'init' */
552: lint_assert(init != NULL);
1.128 rillig 553: lint_assert(init->designation.head != NULL);
554: if (init->designation.head == init->designation.tail) {
555: free(init->designation.head);
556: init->designation.head = NULL;
557: init->designation.tail = NULL;
1.90 rillig 558: } else {
1.128 rillig 559: designator *head = init->designation.head;
560: init->designation.head = init->designation.head->next;
1.126 rillig 561: free(head);
1.90 rillig 562: }
1.106 rillig 563:
1.126 rillig 564: debug_designation();
1.90 rillig 565: }
566:
1.1 cgd 567: /*
1.89 rillig 568: * Initialize the initialization stack by putting an entry for the object
1.1 cgd 569: * which is to be initialized on it.
1.119 rillig 570: *
571: * TODO: merge into begin_initialization
1.1 cgd 572: */
573: void
1.35 rillig 574: initstack_init(void)
1.1 cgd 575: {
1.79 rillig 576: initstack_element *istk;
1.1 cgd 577:
578: if (initerr)
579: return;
580:
1.70 rillig 581: debug_enter();
1.38 rillig 582:
1.1 cgd 583: /*
1.77 rillig 584: * If the type which is to be initialized is an incomplete array,
1.1 cgd 585: * it must be duplicated.
586: */
1.65 rillig 587: if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
1.1 cgd 588: initsym->s_type = duptyp(initsym->s_type);
1.119 rillig 589: /* TODO: does 'duptyp' create a memory leak? */
1.1 cgd 590:
1.134 rillig 591: istk = initstk_lvalue = xcalloc(1, sizeof *initstk_lvalue);
1.1 cgd 592: istk->i_subt = initsym->s_type;
1.43 rillig 593: istk->i_remaining = 1;
1.70 rillig 594:
1.73 rillig 595: debug_initstack();
1.70 rillig 596: debug_leave();
1.1 cgd 597: }
598:
1.119 rillig 599: /* TODO: document me */
1.1 cgd 600: static void
1.137 ! rillig 601: initstack_pop_item_named_member(const char *name)
1.1 cgd 602: {
1.129 rillig 603: initstack_element *istk = initstk_lvalue;
1.101 rillig 604: sym_t *m;
1.1 cgd 605:
1.119 rillig 606: /*
607: * TODO: fix wording of the debug message; this doesn't seem to be
608: * related to initializing the named member.
609: */
1.137 ! rillig 610: debug_step("initializing named member '%s'", name);
1.68 rillig 611:
1.104 rillig 612: if (istk->i_type->t_tspec != STRUCT &&
613: istk->i_type->t_tspec != UNION) {
614: /* syntax error '%s' */
615: error(249, "named member must only be used with struct/union");
616: initerr = true;
617: return;
618: }
619:
1.101 rillig 620: for (m = istk->i_type->t_str->sou_first_member;
621: m != NULL; m = m->s_next) {
1.40 rillig 622:
1.101 rillig 623: if (m->s_bitfield && m->s_name == unnamed)
624: continue;
1.26 christos 625:
1.137 ! rillig 626: if (strcmp(m->s_name, name) == 0) {
1.101 rillig 627: debug_step("found matching member");
628: istk->i_subt = m->s_type;
629: /* XXX: why ++? */
630: istk->i_remaining++;
631: /* XXX: why is i_seen_named_member not set? */
1.131 rillig 632: designation_shift_level();
1.101 rillig 633: return;
634: }
635: }
1.40 rillig 636:
1.137 ! rillig 637: /* TODO: add type information to the message */
1.101 rillig 638: /* undefined struct/union member: %s */
1.137 ! rillig 639: error(101, name);
1.38 rillig 640:
1.131 rillig 641: designation_shift_level();
1.101 rillig 642: istk->i_seen_named_member = true;
643: }
1.80 rillig 644:
1.119 rillig 645: /* TODO: think of a better name than 'pop' */
1.101 rillig 646: static void
647: initstack_pop_item_unnamed(void)
648: {
1.129 rillig 649: initstack_element *istk = initstk_lvalue;
1.101 rillig 650: sym_t *m;
1.80 rillig 651:
1.1 cgd 652: /*
653: * If the removed element was a structure member, we must go
654: * to the next structure member.
655: */
1.43 rillig 656: if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
1.77 rillig 657: !istk->i_seen_named_member) {
1.1 cgd 658: do {
1.125 rillig 659: m = istk->i_next_member =
660: istk->i_next_member->s_next;
1.80 rillig 661: /* XXX: can this assertion be made to fail? */
1.44 rillig 662: lint_assert(m != NULL);
1.68 rillig 663: debug_step("pop %s", m->s_name);
1.48 rillig 664: } while (m->s_bitfield && m->s_name == unnamed);
1.80 rillig 665: /* XXX: duplicate code for skipping unnamed bit-fields */
1.1 cgd 666: istk->i_subt = m->s_type;
667: }
1.101 rillig 668: }
669:
1.119 rillig 670: /* TODO: think of a better name than 'pop' */
1.101 rillig 671: static void
672: initstack_pop_item(void)
673: {
674: initstack_element *istk;
1.137 ! rillig 675: designator *first_designator;
1.101 rillig 676:
677: debug_enter();
678:
1.129 rillig 679: istk = initstk_lvalue;
1.120 rillig 680: debug_indent();
681: debug_printf("popping: ");
1.101 rillig 682: debug_initstack_element(istk);
683:
1.129 rillig 684: initstk_lvalue = istk->i_enclosing;
1.101 rillig 685: free(istk);
1.129 rillig 686: istk = initstk_lvalue;
1.101 rillig 687: lint_assert(istk != NULL);
688:
689: istk->i_remaining--;
690: lint_assert(istk->i_remaining >= 0);
691: debug_step("%d elements remaining", istk->i_remaining);
692:
1.137 ! rillig 693: first_designator = current_designation().head;
! 694: if (first_designator != NULL && first_designator->name != NULL)
! 695: initstack_pop_item_named_member(first_designator->name);
1.101 rillig 696: else
697: initstack_pop_item_unnamed();
698:
1.73 rillig 699: debug_initstack();
1.68 rillig 700: debug_leave();
1.1 cgd 701: }
702:
1.36 rillig 703: /*
704: * Take all entries, including the first which requires a closing brace,
705: * from the stack.
706: */
707: static void
708: initstack_pop_brace(void)
709: {
1.62 rillig 710: bool brace;
1.36 rillig 711:
1.68 rillig 712: debug_enter();
1.73 rillig 713: debug_initstack();
1.36 rillig 714: do {
715: brace = initstk->i_brace;
1.119 rillig 716: /* TODO: improve wording of the debug message */
1.68 rillig 717: debug_step("loop brace=%d", brace);
1.36 rillig 718: initstack_pop_item();
719: } while (!brace);
1.73 rillig 720: debug_initstack();
1.68 rillig 721: debug_leave();
1.36 rillig 722: }
723:
724: /*
725: * Take all entries which cannot be used for further initializers from the
726: * stack, but do this only if they do not require a closing brace.
727: */
1.119 rillig 728: /* TODO: think of a better name than 'pop' */
1.1 cgd 729: static void
1.36 rillig 730: initstack_pop_nobrace(void)
1.1 cgd 731: {
1.7 lukem 732:
1.68 rillig 733: debug_enter();
1.43 rillig 734: while (!initstk->i_brace && initstk->i_remaining == 0 &&
1.77 rillig 735: !initstk->i_array_of_unknown_size)
1.36 rillig 736: initstack_pop_item();
1.68 rillig 737: debug_leave();
1.1 cgd 738: }
739:
1.97 rillig 740: /* Extend an array of unknown size by one element */
741: static void
742: extend_if_array_of_unknown_size(void)
743: {
1.129 rillig 744: initstack_element *istk = initstk_lvalue;
1.97 rillig 745:
746: if (istk->i_remaining != 0)
747: return;
1.133 rillig 748: /*
749: * XXX: According to the function name, there should be a 'return' if
750: * i_array_of_unknown_size is false. There's probably a test missing
751: * for that case.
752: */
1.97 rillig 753:
754: /*
755: * The only place where an incomplete array may appear is at the
756: * outermost aggregate level of the object to be initialized.
757: */
758: lint_assert(istk->i_enclosing->i_enclosing == NULL);
759: lint_assert(istk->i_type->t_tspec == ARRAY);
760:
761: debug_step("extending array of unknown size '%s'",
762: type_name(istk->i_type));
763: istk->i_remaining = 1;
764: istk->i_type->t_dim++;
765: setcomplete(istk->i_type, true);
766:
767: debug_step("extended type is '%s'", type_name(istk->i_type));
768: }
769:
1.119 rillig 770: /* TODO: document me */
771: /* TODO: think of a better name than 'push' */
1.1 cgd 772: static void
1.99 rillig 773: initstack_push_array(void)
774: {
1.129 rillig 775: initstack_element *istk = initstk_lvalue;
1.99 rillig 776:
777: if (istk->i_enclosing->i_seen_named_member) {
778: istk->i_brace = true;
1.124 rillig 779: debug_step("ARRAY%s%s",
780: istk->i_brace ? ", needs closing brace" : "",
781: istk->i_enclosing->i_seen_named_member
782: ? ", seen named member" : "");
1.99 rillig 783: }
784:
785: if (is_incomplete(istk->i_type) &&
786: istk->i_enclosing->i_enclosing != NULL) {
787: /* initialization of an incomplete type */
788: error(175);
789: initerr = true;
790: return;
791: }
792:
793: istk->i_subt = istk->i_type->t_subt;
794: istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
795: istk->i_remaining = istk->i_type->t_dim;
1.126 rillig 796: debug_designation();
1.99 rillig 797: debug_step("type '%s' remaining %d",
798: type_name(istk->i_type), istk->i_remaining);
799: }
800:
1.119 rillig 801: /* TODO: document me */
802: /* TODO: think of a better name than 'push' */
1.99 rillig 803: static bool
804: initstack_push_struct_or_union(void)
805: {
1.119 rillig 806: /*
807: * TODO: remove unnecessary 'const' for variables in functions that
808: * fit on a single screen. Keep it for larger functions.
809: */
1.129 rillig 810: initstack_element *istk = initstk_lvalue;
1.99 rillig 811: int cnt;
812: sym_t *m;
813:
814: if (is_incomplete(istk->i_type)) {
815: /* initialization of an incomplete type */
816: error(175);
817: initerr = true;
818: return false;
819: }
1.100 rillig 820:
1.99 rillig 821: cnt = 0;
1.126 rillig 822: debug_designation();
1.99 rillig 823: debug_step("lookup for '%s'%s",
824: type_name(istk->i_type),
825: istk->i_seen_named_member ? ", seen named member" : "");
1.100 rillig 826:
1.99 rillig 827: for (m = istk->i_type->t_str->sou_first_member;
828: m != NULL; m = m->s_next) {
829: if (m->s_bitfield && m->s_name == unnamed)
830: continue;
1.119 rillig 831: /*
832: * TODO: split into separate functions:
833: *
834: * look_up_array_next
835: * look_up_array_designator
836: * look_up_struct_next
837: * look_up_struct_designator
838: */
1.128 rillig 839: if (current_designation().head != NULL) {
1.119 rillig 840: /* XXX: this log entry looks unnecessarily verbose */
1.100 rillig 841: debug_step("have member '%s', want member '%s'",
1.128 rillig 842: m->s_name, current_designation().head->name);
843: if (strcmp(m->s_name,
844: current_designation().head->name) == 0) {
1.99 rillig 845: cnt++;
846: break;
847: } else
848: continue;
849: }
850: if (++cnt == 1) {
1.125 rillig 851: istk->i_next_member = m;
1.99 rillig 852: istk->i_subt = m->s_type;
853: }
854: }
1.100 rillig 855:
1.128 rillig 856: if (current_designation().head != NULL) {
1.99 rillig 857: if (m == NULL) {
858: debug_step("pop struct");
859: return true;
860: }
1.125 rillig 861: istk->i_next_member = m;
1.99 rillig 862: istk->i_subt = m->s_type;
863: istk->i_seen_named_member = true;
1.128 rillig 864: debug_step("named member '%s'",
865: current_designation().head->name);
1.131 rillig 866: designation_shift_level();
1.99 rillig 867: cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
868: }
869: istk->i_brace = true;
870: debug_step("unnamed element with type '%s'%s",
871: type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
872: istk->i_brace ? ", needs closing brace" : "");
873: if (cnt == 0) {
874: /* cannot init. struct/union with no named member */
875: error(179);
876: initerr = true;
877: return false;
878: }
879: istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
880: return false;
881: }
882:
1.119 rillig 883: /* TODO: document me */
884: /* TODO: think of a better name than 'push' */
1.99 rillig 885: static void
1.35 rillig 886: initstack_push(void)
1.1 cgd 887: {
1.79 rillig 888: initstack_element *istk, *inxt;
1.1 cgd 889:
1.68 rillig 890: debug_enter();
891:
1.97 rillig 892: extend_if_array_of_unknown_size();
893:
1.129 rillig 894: istk = initstk_lvalue;
1.44 rillig 895: lint_assert(istk->i_remaining > 0);
1.61 rillig 896: lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
1.1 cgd 897:
1.134 rillig 898: initstk_lvalue = xcalloc(1, sizeof *initstk_lvalue);
1.129 rillig 899: initstk_lvalue->i_enclosing = istk;
900: initstk_lvalue->i_type = istk->i_subt;
901: lint_assert(initstk_lvalue->i_type->t_tspec != FUNC);
1.1 cgd 902:
1.12 christos 903: again:
1.129 rillig 904: istk = initstk_lvalue;
1.1 cgd 905:
1.88 rillig 906: debug_step("expecting type '%s'", type_name(istk->i_type));
1.107 rillig 907: lint_assert(istk->i_type != NULL);
1.1 cgd 908: switch (istk->i_type->t_tspec) {
909: case ARRAY:
1.128 rillig 910: if (current_designation().head != NULL) {
1.124 rillig 911: debug_step("pop array, named member '%s'%s",
1.128 rillig 912: current_designation().head->name,
1.124 rillig 913: istk->i_brace ? ", needs closing brace" : "");
1.19 christos 914: goto pop;
1.98 rillig 915: }
916:
1.99 rillig 917: initstack_push_array();
918: break;
1.20 christos 919:
1.1 cgd 920: case UNION:
921: if (tflag)
1.89 rillig 922: /* initialization of union is illegal in trad. C */
1.1 cgd 923: warning(238);
924: /* FALLTHROUGH */
925: case STRUCT:
1.99 rillig 926: if (initstack_push_struct_or_union())
927: goto pop;
1.1 cgd 928: break;
929: default:
1.128 rillig 930: if (current_designation().head != NULL) {
1.100 rillig 931: debug_step("pop scalar");
1.19 christos 932: pop:
1.119 rillig 933: /* TODO: extract this into end_initializer_level */
1.77 rillig 934: inxt = initstk->i_enclosing;
1.12 christos 935: free(istk);
1.129 rillig 936: initstk_lvalue = inxt;
1.12 christos 937: goto again;
938: }
1.100 rillig 939: /* The initialization stack now expects a single scalar. */
1.43 rillig 940: istk->i_remaining = 1;
1.1 cgd 941: break;
942: }
1.68 rillig 943:
1.73 rillig 944: debug_initstack();
1.68 rillig 945: debug_leave();
1.1 cgd 946: }
947:
948: static void
1.84 rillig 949: check_too_many_initializers(void)
1.1 cgd 950: {
951:
1.84 rillig 952: const initstack_element *istk = initstk;
953: if (istk->i_remaining > 0)
954: return;
1.119 rillig 955: /*
956: * FIXME: even with named members, there can be too many initializers
957: */
1.84 rillig 958: if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
959: return;
1.1 cgd 960:
1.84 rillig 961: tspec_t t = istk->i_type->t_tspec;
962: if (t == ARRAY) {
963: /* too many array initializers, expected %d */
964: error(173, istk->i_type->t_dim);
965: } else if (t == STRUCT || t == UNION) {
966: /* too many struct/union initializers */
967: error(172);
968: } else {
969: /* too many initializers */
970: error(174);
1.1 cgd 971: }
1.84 rillig 972: initerr = true;
1.1 cgd 973: }
974:
1.92 rillig 975: /*
976: * Process a '{' in an initializer by starting the initialization of the
977: * nested data structure, with i_type being the i_subt of the outer
978: * initialization level.
979: */
1.1 cgd 980: static void
1.37 rillig 981: initstack_next_brace(void)
1.1 cgd 982: {
1.7 lukem 983:
1.68 rillig 984: debug_enter();
1.73 rillig 985: debug_initstack();
1.68 rillig 986:
1.61 rillig 987: if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
1.49 rillig 988: /* invalid initializer type %s */
1.57 rillig 989: error(176, type_name(initstk->i_type));
1.62 rillig 990: initerr = true;
1.37 rillig 991: }
992: if (!initerr)
1.84 rillig 993: check_too_many_initializers();
1.37 rillig 994: if (!initerr)
995: initstack_push();
996: if (!initerr) {
1.129 rillig 997: initstk_lvalue->i_brace = true;
1.126 rillig 998: debug_designation();
1.92 rillig 999: debug_step("expecting type '%s'",
1000: type_name(initstk->i_type != NULL ? initstk->i_type
1.74 rillig 1001: : initstk->i_subt));
1.37 rillig 1002: }
1.68 rillig 1003:
1.73 rillig 1004: debug_initstack();
1.68 rillig 1005: debug_leave();
1.37 rillig 1006: }
1007:
1.119 rillig 1008: /* TODO: document me, or think of a better name */
1.37 rillig 1009: static void
1.118 rillig 1010: initstack_next_nobrace(tnode_t *tn)
1.37 rillig 1011: {
1.68 rillig 1012: debug_enter();
1.37 rillig 1013:
1.61 rillig 1014: if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
1.37 rillig 1015: /* {}-enclosed initializer required */
1016: error(181);
1.92 rillig 1017: /* XXX: maybe set initerr here */
1.37 rillig 1018: }
1019:
1.84 rillig 1020: if (!initerr)
1021: check_too_many_initializers();
1022:
1.42 rillig 1023: while (!initerr) {
1.129 rillig 1024: initstack_element *istk = initstk_lvalue;
1.118 rillig 1025:
1026: if (tn->tn_type->t_tspec == STRUCT &&
1027: istk->i_type == tn->tn_type &&
1028: istk->i_enclosing != NULL &&
1029: istk->i_enclosing->i_enclosing != NULL) {
1030: istk->i_brace = false;
1031: istk->i_remaining = 1; /* the struct itself */
1032: break;
1033: }
1034:
1035: if ((istk->i_type != NULL && is_scalar(istk->i_type->t_tspec)))
1.42 rillig 1036: break;
1037: initstack_push();
1.1 cgd 1038: }
1.68 rillig 1039:
1.73 rillig 1040: debug_initstack();
1.68 rillig 1041: debug_leave();
1.1 cgd 1042: }
1043:
1.119 rillig 1044: /* TODO: document me */
1.1 cgd 1045: void
1.34 rillig 1046: init_lbrace(void)
1.1 cgd 1047: {
1048: if (initerr)
1049: return;
1050:
1.68 rillig 1051: debug_enter();
1.73 rillig 1052: debug_initstack();
1.68 rillig 1053:
1.1 cgd 1054: if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
1.77 rillig 1055: initstk->i_enclosing == NULL) {
1.61 rillig 1056: if (tflag && !is_scalar(initstk->i_subt->t_tspec))
1.50 rillig 1057: /* no automatic aggregate initialization in trad. C */
1.1 cgd 1058: warning(188);
1059: }
1060:
1061: /*
1062: * Remove all entries which cannot be used for further initializers
1063: * and do not expect a closing brace.
1064: */
1.36 rillig 1065: initstack_pop_nobrace();
1.1 cgd 1066:
1.37 rillig 1067: initstack_next_brace();
1.68 rillig 1068:
1.73 rillig 1069: debug_initstack();
1.68 rillig 1070: debug_leave();
1.1 cgd 1071: }
1072:
1.92 rillig 1073: /*
1074: * Process a '}' in an initializer by finishing the current level of the
1075: * initialization stack.
1076: */
1.1 cgd 1077: void
1.34 rillig 1078: init_rbrace(void)
1.1 cgd 1079: {
1080: if (initerr)
1081: return;
1082:
1.68 rillig 1083: debug_enter();
1.36 rillig 1084: initstack_pop_brace();
1.68 rillig 1085: debug_leave();
1.1 cgd 1086: }
1087:
1.86 rillig 1088: /* In traditional C, bit-fields can be initialized only by integer constants. */
1089: static void
1090: check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
1091: {
1092: if (tflag &&
1093: is_integer(lt) &&
1094: ln->tn_type->t_bitfield &&
1095: !is_integer(rt)) {
1.89 rillig 1096: /* bit-field initialization is illegal in traditional C */
1.86 rillig 1097: warning(186);
1098: }
1099: }
1100:
1.87 rillig 1101: static void
1102: check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
1103: {
1.119 rillig 1104: /* TODO: rename CON to CONSTANT to avoid ambiguity with CONVERT */
1.87 rillig 1105: if (tn == NULL || tn->tn_op == CON)
1106: return;
1107:
1108: sym_t *sym;
1109: ptrdiff_t offs;
1110: if (constant_addr(tn, &sym, &offs))
1111: return;
1112:
1113: if (sclass == AUTO || sclass == REG) {
1114: /* non-constant initializer */
1115: c99ism(177);
1116: } else {
1117: /* non-constant initializer */
1118: error(177);
1119: }
1120: }
1121:
1.114 rillig 1122: /*
1.119 rillig 1123: * Initialize a non-array object with automatic storage duration and only a
1124: * single initializer expression without braces by delegating to ASSIGN.
1.114 rillig 1125: */
1126: static bool
1127: init_using_assign(tnode_t *rn)
1128: {
1129: tnode_t *ln, *tn;
1130:
1131: if (initsym->s_type->t_tspec == ARRAY)
1132: return false;
1133: if (initstk->i_enclosing != NULL)
1134: return false;
1135:
1136: debug_step("handing over to ASSIGN");
1.119 rillig 1137:
1.114 rillig 1138: ln = new_name_node(initsym, 0);
1139: ln->tn_type = tduptyp(ln->tn_type);
1140: ln->tn_type->t_const = false;
1.119 rillig 1141:
1.114 rillig 1142: tn = build(ASSIGN, ln, rn);
1143: expr(tn, false, false, false, false);
1.119 rillig 1144:
1.114 rillig 1145: /* XXX: why not clean up the initstack here already? */
1146: return true;
1147: }
1148:
1.116 rillig 1149: static void
1150: check_init_expr(tnode_t *tn, scl_t sclass)
1151: {
1152: tnode_t *ln;
1153: tspec_t lt, rt;
1154: struct mbl *tmem;
1155:
1156: /* Create a temporary node for the left side. */
1.134 rillig 1157: ln = tgetblk(sizeof *ln);
1.116 rillig 1158: ln->tn_op = NAME;
1159: ln->tn_type = tduptyp(initstk->i_type);
1160: ln->tn_type->t_const = false;
1161: ln->tn_lvalue = true;
1162: ln->tn_sym = initsym; /* better than nothing */
1163:
1164: tn = cconv(tn);
1165:
1166: lt = ln->tn_type->t_tspec;
1167: rt = tn->tn_type->t_tspec;
1168:
1169: debug_step("typeok '%s', '%s'",
1170: type_name(ln->tn_type), type_name(tn->tn_type));
1171: if (!typeok(INIT, 0, ln, tn))
1172: return;
1173:
1174: /*
1.119 rillig 1175: * Preserve the tree memory. This is necessary because otherwise
1.116 rillig 1176: * expr() would free it.
1177: */
1178: tmem = tsave();
1179: expr(tn, true, false, true, false);
1180: trestor(tmem);
1181:
1182: check_bit_field_init(ln, lt, rt);
1183:
1184: /*
1185: * XXX: Is it correct to do this conversion _after_ the typeok above?
1186: */
1187: if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
1188: tn = convert(INIT, 0, initstk->i_type, tn);
1189:
1190: check_non_constant_initializer(tn, sclass);
1191: }
1192:
1.1 cgd 1193: void
1.69 rillig 1194: init_using_expr(tnode_t *tn)
1.1 cgd 1195: {
1.81 rillig 1196: scl_t sclass;
1.1 cgd 1197:
1.68 rillig 1198: debug_enter();
1.92 rillig 1199: debug_initstack();
1.126 rillig 1200: debug_designation();
1.95 rillig 1201: debug_step("expr:");
1202: debug_node(tn, debug_ind + 1);
1.55 rillig 1203:
1.113 rillig 1204: if (initerr || tn == NULL)
1205: goto done;
1.1 cgd 1206:
1.81 rillig 1207: sclass = initsym->s_scl;
1.114 rillig 1208: if ((sclass == AUTO || sclass == REG) && init_using_assign(tn))
1.113 rillig 1209: goto done;
1.1 cgd 1210:
1.36 rillig 1211: initstack_pop_nobrace();
1.1 cgd 1212:
1.82 rillig 1213: if (init_array_using_string(tn)) {
1214: debug_step("after initializing the string:");
1215: /* XXX: why not clean up the initstack here already? */
1.113 rillig 1216: goto done_initstack;
1.68 rillig 1217: }
1.1 cgd 1218:
1.118 rillig 1219: initstack_next_nobrace(tn);
1.113 rillig 1220: if (initerr || tn == NULL)
1221: goto done_initstack;
1.1 cgd 1222:
1.129 rillig 1223: initstk_lvalue->i_remaining--;
1.83 rillig 1224: debug_step("%d elements remaining", initstk->i_remaining);
1225:
1.116 rillig 1226: check_init_expr(tn, sclass);
1.68 rillig 1227:
1.113 rillig 1228: done_initstack:
1.73 rillig 1229: debug_initstack();
1.130 rillig 1230:
1.113 rillig 1231: done:
1.130 rillig 1232: while (current_designation().head != NULL)
1.131 rillig 1233: designation_shift_level();
1.130 rillig 1234:
1.68 rillig 1235: debug_leave();
1.1 cgd 1236: }
1237:
1238:
1.82 rillig 1239: /* Initialize a character array or wchar_t array with a string literal. */
1.62 rillig 1240: static bool
1.82 rillig 1241: init_array_using_string(tnode_t *tn)
1.1 cgd 1242: {
1243: tspec_t t;
1.79 rillig 1244: initstack_element *istk;
1.1 cgd 1245: int len;
1246: strg_t *strg;
1247:
1248: if (tn->tn_op != STRING)
1.63 rillig 1249: return false;
1.1 cgd 1250:
1.68 rillig 1251: debug_enter();
1.73 rillig 1252: debug_initstack();
1.68 rillig 1253:
1.129 rillig 1254: istk = initstk_lvalue;
1.47 rillig 1255: strg = tn->tn_string;
1.1 cgd 1256:
1257: /*
1258: * Check if we have an array type which can be initialized by
1259: * the string.
1260: */
1.12 christos 1261: if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
1.68 rillig 1262: debug_step("subt array");
1.1 cgd 1263: t = istk->i_subt->t_subt->t_tspec;
1264: if (!((strg->st_tspec == CHAR &&
1265: (t == CHAR || t == UCHAR || t == SCHAR)) ||
1266: (strg->st_tspec == WCHAR && t == WCHAR))) {
1.68 rillig 1267: debug_leave();
1.63 rillig 1268: return false;
1.1 cgd 1269: }
1.82 rillig 1270: /* XXX: duplicate code, see below */
1.119 rillig 1271:
1.1 cgd 1272: /* Put the array at top of stack */
1.35 rillig 1273: initstack_push();
1.129 rillig 1274: istk = initstk_lvalue;
1.119 rillig 1275:
1276:
1277: /* TODO: what if both i_type and i_subt are ARRAY? */
1278:
1.1 cgd 1279: } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
1.68 rillig 1280: debug_step("type array");
1.1 cgd 1281: t = istk->i_type->t_subt->t_tspec;
1282: if (!((strg->st_tspec == CHAR &&
1283: (t == CHAR || t == UCHAR || t == SCHAR)) ||
1284: (strg->st_tspec == WCHAR && t == WCHAR))) {
1.68 rillig 1285: debug_leave();
1.63 rillig 1286: return false;
1.1 cgd 1287: }
1.82 rillig 1288: /* XXX: duplicate code, see above */
1.119 rillig 1289:
1290: /*
1291: * TODO: is this really not needed in the branch above this
1292: * one?
1293: */
1.1 cgd 1294: /*
1295: * If the array is already partly initialized, we are
1296: * wrong here.
1297: */
1.115 rillig 1298: if (istk->i_remaining != istk->i_type->t_dim) {
1.68 rillig 1299: debug_leave();
1.63 rillig 1300: return false;
1.115 rillig 1301: }
1.1 cgd 1302: } else {
1.68 rillig 1303: debug_leave();
1.63 rillig 1304: return false;
1.1 cgd 1305: }
1306:
1307: /* Get length without trailing NUL character. */
1308: len = strg->st_len;
1309:
1.77 rillig 1310: if (istk->i_array_of_unknown_size) {
1311: istk->i_array_of_unknown_size = false;
1.1 cgd 1312: istk->i_type->t_dim = len + 1;
1.63 rillig 1313: setcomplete(istk->i_type, true);
1.1 cgd 1314: } else {
1.119 rillig 1315: /*
1316: * TODO: check for buffer overflow in the object to be
1317: * initialized
1318: */
1319: /* XXX: double-check for off-by-one error */
1.1 cgd 1320: if (istk->i_type->t_dim < len) {
1321: /* non-null byte ignored in string initializer */
1322: warning(187);
1323: }
1.119 rillig 1324:
1325: /*
1326: * TODO: C99 6.7.8p14 allows a string literal to be enclosed
1327: * in optional redundant braces, just like scalars. Add tests
1328: * for this.
1329: */
1.1 cgd 1330: }
1331:
1332: /* In every case the array is initialized completely. */
1.43 rillig 1333: istk->i_remaining = 0;
1.1 cgd 1334:
1.73 rillig 1335: debug_initstack();
1.68 rillig 1336: debug_leave();
1.63 rillig 1337: return true;
1.1 cgd 1338: }
CVSweb <webmaster@jp.NetBSD.org>