Annotation of src/usr.bin/xlint/lint1/init.c, Revision 1.146
1.146 ! rillig 1: /* $NetBSD: init.c,v 1.145 2021/03/28 07:59:09 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.146 ! rillig 40: __RCSID("$NetBSD: init.c,v 1.145 2021/03/28 07:59:09 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.145 rillig 260:
1.111 rillig 261: static struct initialization *init;
1.12 christos 262:
1.145 rillig 263: #ifdef DEBUG
264: static int debug_ind = 0;
265: #endif
266:
267:
268: #ifdef DEBUG
269:
270: static void __printflike(1, 2)
271: debug_printf(const char *fmt, ...)
272: {
273: va_list va;
274:
275: va_start(va, fmt);
276: vfprintf(stdout, fmt, va);
277: va_end(va);
278: }
279:
280: static void
281: debug_indent(void)
282: {
283: debug_printf("%*s", 2 * debug_ind, "");
284: }
285:
286: static void
287: debug_enter(const char *func)
288: {
289: printf("%*s+ %s\n", 2 * debug_ind++, "", func);
290: }
291:
292: static void __printflike(1, 2)
293: debug_step(const char *fmt, ...)
294: {
295: va_list va;
296:
297: debug_indent();
298: va_start(va, fmt);
299: vfprintf(stdout, fmt, va);
300: va_end(va);
301: printf("\n");
302: }
303:
304: static void
305: debug_leave(const char *func)
306: {
307: printf("%*s- %s\n", 2 * --debug_ind, "", func);
308: }
309:
310: #else
311:
312: #define debug_printf(fmt, ...) do { } while (false)
313: #define debug_indent() do { } while (false)
314: #define debug_enter(function) do { } while (false)
315: #define debug_step(fmt, ...) do { } while (false)
316: #define debug_leave(function) do { } while (false)
317:
318: #endif
319:
1.1 cgd 320:
1.146 ! rillig 321: static struct designator *
! 322: designator_new(const char *name)
1.135 rillig 323: {
1.146 ! rillig 324: struct designator *d = xcalloc(1, sizeof *d);
! 325: d->name = name;
! 326: return d;
1.135 rillig 327: }
1.119 rillig 328:
1.146 ! rillig 329: static void
! 330: designator_free(struct designator *d)
1.110 rillig 331: {
1.146 ! rillig 332: free(d);
1.110 rillig 333: }
334:
335:
1.146 ! rillig 336: #ifdef DEBUG
! 337: static void
! 338: designation_debug(const struct designation *dn)
1.110 rillig 339: {
1.146 ! rillig 340: const struct designator *p;
1.110 rillig 341:
1.146 ! rillig 342: if (dn->head == NULL)
! 343: return;
1.123 rillig 344:
1.146 ! rillig 345: debug_indent();
! 346: debug_printf("designation: ");
! 347: for (p = dn->head; p != NULL; p = p->next)
! 348: debug_printf(".%s", p->name);
! 349: debug_printf("\n");
1.110 rillig 350: }
1.146 ! rillig 351: #else
! 352: #define designation_debug(dn) do { } while (false)
! 353: #endif
1.110 rillig 354:
1.121 rillig 355: static void
1.146 ! rillig 356: designation_add(struct designation *dn, struct designator *dr)
1.121 rillig 357: {
358:
1.146 ! rillig 359: if (dn->head != NULL) {
! 360: dn->tail->next = dr;
! 361: dn->tail = dr;
! 362: } else {
! 363: dn->head = dr;
! 364: dn->tail = dr;
1.121 rillig 365: }
366:
1.146 ! rillig 367: designation_debug(dn);
1.121 rillig 368: }
369:
1.146 ! rillig 370: /* TODO: add support for array subscripts, not only named members */
! 371: /*
! 372: * TODO: This function should not be necessary at all. There is no need to
! 373: * remove the head of the list.
! 374: */
1.141 rillig 375: static void
1.146 ! rillig 376: designation_shift_level(struct designation *dn)
1.141 rillig 377: {
1.146 ! rillig 378: lint_assert(dn->head != NULL);
1.141 rillig 379:
1.146 ! rillig 380: if (dn->head == dn->tail) {
! 381: designator_free(dn->head);
! 382: dn->head = NULL;
! 383: dn->tail = NULL;
! 384: } else {
! 385: struct designator *head = dn->head;
! 386: dn->head = dn->head->next;
! 387: designator_free(head);
! 388: }
1.110 rillig 389:
1.146 ! rillig 390: designation_debug(dn);
! 391: }
1.90 rillig 392:
1.1 cgd 393:
1.146 ! rillig 394: #ifdef DEBUG
1.119 rillig 395: /*
396: * TODO: only log the top of the stack after each modifying operation
397: *
1.139 rillig 398: * TODO: wrap all write accesses to brace_level in setter functions
1.119 rillig 399: */
1.78 rillig 400: static void
1.146 ! rillig 401: brace_level_debug(const struct brace_level *level)
1.74 rillig 402: {
1.139 rillig 403: if (level->bl_type != NULL)
404: debug_printf("type '%s'", type_name(level->bl_type));
405: if (level->bl_type != NULL && level->bl_subtype != NULL)
1.120 rillig 406: debug_printf(", ");
1.139 rillig 407: if (level->bl_subtype != NULL)
408: debug_printf("subtype '%s'", type_name(level->bl_subtype));
1.78 rillig 409:
1.139 rillig 410: if (level->bl_brace)
1.120 rillig 411: debug_printf(", needs closing brace");
1.139 rillig 412: if (level->bl_array_of_unknown_size)
1.120 rillig 413: debug_printf(", array of unknown size");
1.139 rillig 414: if (level->bl_seen_named_member)
1.120 rillig 415: debug_printf(", seen named member");
1.78 rillig 416:
1.139 rillig 417: const type_t *eff_type = level->bl_type != NULL
418: ? level->bl_type : level->bl_subtype;
419: if (eff_type->t_tspec == STRUCT && level->bl_next_member != NULL)
1.125 rillig 420: debug_printf(", next member '%s'",
1.139 rillig 421: level->bl_next_member->s_name);
1.78 rillig 422:
1.139 rillig 423: debug_printf(", remaining %d\n", level->bl_remaining);
1.74 rillig 424: }
1.146 ! rillig 425: #else
! 426: #define brace_level_debug(level) do { } while (false)
! 427: #endif
! 428:
! 429:
! 430: static struct initialization *
! 431: initialization_new(sym_t *sym)
! 432: {
! 433: struct initialization *in = xcalloc(1, sizeof(*in));
! 434:
! 435: in->initsym = sym;
! 436:
! 437: return in;
! 438: }
1.74 rillig 439:
1.146 ! rillig 440: static void
! 441: initialization_free(struct initialization *in)
! 442: {
! 443: struct brace_level *level, *next;
! 444:
! 445: for (level = in->brace_level; level != NULL; level = next) {
! 446: next = level->bl_enclosing;
! 447: free(level);
! 448: }
! 449:
! 450: free(in);
! 451: }
! 452:
! 453: #ifdef DEBUG
1.119 rillig 454: /*
455: * TODO: only call debug_initstack after each push/pop.
456: */
1.73 rillig 457: static void
1.146 ! rillig 458: initialization_debug(const struct initialization *in)
1.73 rillig 459: {
1.146 ! rillig 460: if (in->brace_level == NULL) {
1.139 rillig 461: debug_step("no brace level in the current initialization");
1.73 rillig 462: return;
463: }
464:
465: size_t i = 0;
1.146 ! rillig 466: for (const struct brace_level *level = in->brace_level;
1.139 rillig 467: level != NULL; level = level->bl_enclosing) {
1.120 rillig 468: debug_indent();
1.139 rillig 469: debug_printf("brace level %zu: ", i);
1.146 ! rillig 470: brace_level_debug(level);
1.73 rillig 471: i++;
472: }
473: }
1.146 ! rillig 474: #else
! 475: #define initialization_debug(in) do { } while (false)
! 476: #endif
! 477:
! 478: /* XXX: unnecessary prototype since it is not recursive */
! 479: static bool init_array_using_string(tnode_t *);
! 480:
! 481:
! 482: static struct initialization *
! 483: current_init(void)
! 484: {
! 485: lint_assert(init != NULL);
! 486: return init;
! 487: }
! 488:
! 489: bool *
! 490: current_initerr(void)
! 491: {
! 492: return ¤t_init()->initerr;
! 493: }
! 494:
! 495: sym_t **
! 496: current_initsym(void)
! 497: {
! 498: return ¤t_init()->initsym;
! 499: }
! 500:
! 501: static struct designation *
! 502: current_designation_mod(void)
! 503: {
! 504: return ¤t_init()->designation;
! 505: }
! 506:
! 507: static struct designation
! 508: current_designation(void)
! 509: {
! 510: return *current_designation_mod();
! 511: }
! 512:
! 513: static const struct brace_level *
! 514: current_brace_level(void)
! 515: {
! 516: return current_init()->brace_level;
! 517: }
! 518:
! 519: static struct brace_level **
! 520: current_brace_level_lvalue(void)
! 521: {
! 522: return ¤t_init()->brace_level;
! 523: }
! 524:
! 525: static void
! 526: set_initerr(void)
! 527: {
! 528: current_init()->initerr = true;
! 529: }
! 530:
! 531: #define initerr (*current_initerr())
! 532: #define initsym (*current_initsym())
! 533: #define brace_level_rvalue (current_brace_level())
! 534: #define brace_level_lvalue (*current_brace_level_lvalue())
! 535:
! 536: #ifndef DEBUG
! 537:
! 538: #define debug_designation() do { } while (false)
! 539: #define debug_brace_level(level) do { } while (false)
! 540: #define debug_initstack() do { } while (false)
! 541:
! 542: #else
! 543:
1.90 rillig 544:
545: #define debug_enter() debug_enter(__func__)
546: #define debug_leave() debug_leave(__func__)
547:
1.73 rillig 548: #endif
549:
1.111 rillig 550:
551: void
552: begin_initialization(sym_t *sym)
553: {
554: struct initialization *curr_init;
555:
1.118 rillig 556: debug_step("begin initialization of '%s'", type_name(sym->s_type));
1.146 ! rillig 557: curr_init = initialization_new(sym);
1.111 rillig 558: curr_init->next = init;
559: init = curr_init;
560: }
561:
562: void
563: end_initialization(void)
564: {
565: struct initialization *curr_init;
566:
567: curr_init = init;
568: init = init->next;
1.146 ! rillig 569: initialization_free(curr_init);
1.111 rillig 570: debug_step("end initialization");
571: }
572:
1.106 rillig 573:
1.90 rillig 574:
1.142 rillig 575: void
576: designation_add_name(sbuf_t *sb)
577: {
1.146 ! rillig 578: designation_add(current_designation_mod(),
! 579: designator_new(sb->sb_name));
1.142 rillig 580: }
581:
1.133 rillig 582: /* TODO: Move the function body up here, to avoid the forward declaration. */
583: static void initstack_pop_nobrace(void);
584:
1.143 rillig 585: static struct brace_level *
586: brace_level_new(type_t *type, type_t *subtype, int remaining)
587: {
588: struct brace_level *level = xcalloc(1, sizeof(*level));
589:
590: level->bl_type = type;
591: level->bl_subtype = subtype;
592: level->bl_remaining = remaining;
593:
594: return level;
595: }
596:
597: static const sym_t *
598: brace_level_look_up_member(const char *name)
599: {
600: const type_t *tp = current_brace_level()->bl_type;
601: const sym_t *m;
602:
603: lint_assert(tp->t_tspec == STRUCT || tp->t_tspec == UNION);
604:
605: for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
606: if (m->s_bitfield && m->s_name == unnamed)
607: continue;
608: if (strcmp(m->s_name, name) == 0)
609: return m;
610: }
611:
612: return NULL;
613: }
614:
1.142 rillig 615: static void
616: brace_level_set_array_dimension(int dim)
617: {
618: debug_step("setting the array size to %d", dim);
619: brace_level_lvalue->bl_type->t_dim = dim;
620: debug_indent();
1.146 ! rillig 621: brace_level_debug(brace_level_rvalue);
1.142 rillig 622: }
623:
1.144 rillig 624: static void
625: brace_level_next_member(struct brace_level *level)
626: {
627: const sym_t *m;
628:
629: do {
630: m = level->bl_next_member = level->bl_next_member->s_next;
631: /* XXX: can this assertion be made to fail? */
632: lint_assert(m != NULL);
633: } while (m->s_bitfield && m->s_name == unnamed);
634:
635: debug_indent();
1.146 ! rillig 636: brace_level_debug(level);
1.144 rillig 637: }
638:
1.91 rillig 639: /*
1.119 rillig 640: * A sub-object of an array is initialized using a designator. This does not
641: * have to be an array element directly, it can also be used to initialize
642: * only a sub-object of the array element.
1.91 rillig 643: *
644: * C99 example: struct { int member[4]; } var = { [2] = 12345 };
645: *
646: * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
1.119 rillig 647: *
648: * TODO: test the following initialization with an outer and an inner type:
649: *
650: * .deeply[0].nested = {
651: * .deeply[1].nested = {
652: * 12345,
653: * },
654: * }
1.91 rillig 655: */
656: void
1.131 rillig 657: designation_add_subscript(range_t range)
1.91 rillig 658: {
1.139 rillig 659: struct brace_level *level;
1.132 rillig 660:
1.91 rillig 661: debug_enter();
1.142 rillig 662: if (range.lo == range.hi)
663: debug_step("subscript is %zu", range.hi);
664: else
665: debug_step("subscript range is %zu ... %zu",
666: range.lo, range.hi);
1.132 rillig 667:
1.133 rillig 668: initstack_pop_nobrace();
669:
1.139 rillig 670: level = brace_level_lvalue;
671: if (level->bl_array_of_unknown_size) {
1.133 rillig 672: /* No +1 here, extend_if_array_of_unknown_size will add it. */
673: int auto_dim = (int)range.hi;
1.142 rillig 674: if (auto_dim > level->bl_type->t_dim)
675: brace_level_set_array_dimension(auto_dim);
1.132 rillig 676: }
677:
1.91 rillig 678: debug_leave();
679: }
680:
1.90 rillig 681:
1.1 cgd 682: /*
1.89 rillig 683: * Initialize the initialization stack by putting an entry for the object
1.1 cgd 684: * which is to be initialized on it.
1.119 rillig 685: *
686: * TODO: merge into begin_initialization
1.1 cgd 687: */
688: void
1.35 rillig 689: initstack_init(void)
1.1 cgd 690: {
691:
692: if (initerr)
693: return;
694:
1.70 rillig 695: debug_enter();
1.38 rillig 696:
1.1 cgd 697: /*
1.77 rillig 698: * If the type which is to be initialized is an incomplete array,
1.1 cgd 699: * it must be duplicated.
700: */
1.65 rillig 701: if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
1.1 cgd 702: initsym->s_type = duptyp(initsym->s_type);
1.119 rillig 703: /* TODO: does 'duptyp' create a memory leak? */
1.1 cgd 704:
1.143 rillig 705: brace_level_lvalue = brace_level_new(NULL, initsym->s_type, 1);
1.70 rillig 706:
1.146 ! rillig 707: initialization_debug(current_init());
1.70 rillig 708: debug_leave();
1.1 cgd 709: }
710:
1.119 rillig 711: /* TODO: document me */
1.1 cgd 712: static void
1.137 rillig 713: initstack_pop_item_named_member(const char *name)
1.1 cgd 714: {
1.139 rillig 715: struct brace_level *level = brace_level_lvalue;
1.143 rillig 716: const sym_t *m;
1.1 cgd 717:
1.119 rillig 718: /*
719: * TODO: fix wording of the debug message; this doesn't seem to be
720: * related to initializing the named member.
721: */
1.137 rillig 722: debug_step("initializing named member '%s'", name);
1.68 rillig 723:
1.139 rillig 724: if (level->bl_type->t_tspec != STRUCT &&
725: level->bl_type->t_tspec != UNION) {
1.104 rillig 726: /* syntax error '%s' */
727: error(249, "named member must only be used with struct/union");
1.141 rillig 728: set_initerr();
1.104 rillig 729: return;
730: }
731:
1.143 rillig 732: m = brace_level_look_up_member(name);
733: if (m == NULL) {
734: /* TODO: add type information to the message */
735: /* undefined struct/union member: %s */
736: error(101, name);
1.40 rillig 737:
1.146 ! rillig 738: designation_shift_level(current_designation_mod());
1.143 rillig 739: level->bl_seen_named_member = true;
740: return;
1.101 rillig 741: }
1.40 rillig 742:
1.143 rillig 743: debug_step("found matching member");
744: level->bl_subtype = m->s_type;
745: /* XXX: why ++? */
746: level->bl_remaining++;
747: /* XXX: why is bl_seen_named_member not set? */
1.146 ! rillig 748: designation_shift_level(current_designation_mod());
1.101 rillig 749: }
1.80 rillig 750:
1.119 rillig 751: /* TODO: think of a better name than 'pop' */
1.101 rillig 752: static void
753: initstack_pop_item_unnamed(void)
754: {
1.139 rillig 755: struct brace_level *level = brace_level_lvalue;
1.80 rillig 756:
1.1 cgd 757: /*
758: * If the removed element was a structure member, we must go
759: * to the next structure member.
760: */
1.139 rillig 761: if (level->bl_remaining > 0 && level->bl_type->t_tspec == STRUCT &&
762: !level->bl_seen_named_member) {
1.144 rillig 763: brace_level_next_member(level);
764: level->bl_subtype = level->bl_next_member->s_type;
1.1 cgd 765: }
1.101 rillig 766: }
767:
1.119 rillig 768: /* TODO: think of a better name than 'pop' */
1.101 rillig 769: static void
770: initstack_pop_item(void)
771: {
1.139 rillig 772: struct brace_level *level;
1.140 rillig 773: struct designator *first_designator;
1.101 rillig 774:
775: debug_enter();
776:
1.139 rillig 777: level = brace_level_lvalue;
1.120 rillig 778: debug_indent();
779: debug_printf("popping: ");
1.146 ! rillig 780: brace_level_debug(level);
1.101 rillig 781:
1.139 rillig 782: brace_level_lvalue = level->bl_enclosing;
783: free(level);
784: level = brace_level_lvalue;
785: lint_assert(level != NULL);
786:
787: level->bl_remaining--;
788: lint_assert(level->bl_remaining >= 0);
789: debug_step("%d elements remaining", level->bl_remaining);
1.101 rillig 790:
1.137 rillig 791: first_designator = current_designation().head;
792: if (first_designator != NULL && first_designator->name != NULL)
793: initstack_pop_item_named_member(first_designator->name);
1.101 rillig 794: else
795: initstack_pop_item_unnamed();
796:
1.146 ! rillig 797: initialization_debug(current_init());
1.68 rillig 798: debug_leave();
1.1 cgd 799: }
800:
1.36 rillig 801: /*
802: * Take all entries, including the first which requires a closing brace,
803: * from the stack.
804: */
805: static void
806: initstack_pop_brace(void)
807: {
1.62 rillig 808: bool brace;
1.36 rillig 809:
1.68 rillig 810: debug_enter();
1.146 ! rillig 811: initialization_debug(current_init());
1.36 rillig 812: do {
1.139 rillig 813: brace = brace_level_rvalue->bl_brace;
1.119 rillig 814: /* TODO: improve wording of the debug message */
1.68 rillig 815: debug_step("loop brace=%d", brace);
1.36 rillig 816: initstack_pop_item();
817: } while (!brace);
1.146 ! rillig 818: initialization_debug(current_init());
1.68 rillig 819: debug_leave();
1.36 rillig 820: }
821:
822: /*
823: * Take all entries which cannot be used for further initializers from the
824: * stack, but do this only if they do not require a closing brace.
825: */
1.119 rillig 826: /* TODO: think of a better name than 'pop' */
1.1 cgd 827: static void
1.36 rillig 828: initstack_pop_nobrace(void)
1.1 cgd 829: {
1.7 lukem 830:
1.68 rillig 831: debug_enter();
1.139 rillig 832: while (!brace_level_rvalue->bl_brace &&
833: brace_level_rvalue->bl_remaining == 0 &&
834: !brace_level_rvalue->bl_array_of_unknown_size)
1.36 rillig 835: initstack_pop_item();
1.68 rillig 836: debug_leave();
1.1 cgd 837: }
838:
1.97 rillig 839: /* Extend an array of unknown size by one element */
840: static void
841: extend_if_array_of_unknown_size(void)
842: {
1.139 rillig 843: struct brace_level *level = brace_level_lvalue;
1.97 rillig 844:
1.139 rillig 845: if (level->bl_remaining != 0)
1.97 rillig 846: return;
1.133 rillig 847: /*
848: * XXX: According to the function name, there should be a 'return' if
1.139 rillig 849: * bl_array_of_unknown_size is false. There's probably a test missing
1.133 rillig 850: * for that case.
851: */
1.97 rillig 852:
853: /*
854: * The only place where an incomplete array may appear is at the
855: * outermost aggregate level of the object to be initialized.
856: */
1.139 rillig 857: lint_assert(level->bl_enclosing->bl_enclosing == NULL);
858: lint_assert(level->bl_type->t_tspec == ARRAY);
1.97 rillig 859:
860: debug_step("extending array of unknown size '%s'",
1.139 rillig 861: type_name(level->bl_type));
862: level->bl_remaining = 1;
863: level->bl_type->t_dim++;
864: setcomplete(level->bl_type, true);
1.97 rillig 865:
1.139 rillig 866: debug_step("extended type is '%s'", type_name(level->bl_type));
1.97 rillig 867: }
868:
1.119 rillig 869: /* TODO: document me */
870: /* TODO: think of a better name than 'push' */
1.1 cgd 871: static void
1.99 rillig 872: initstack_push_array(void)
873: {
1.139 rillig 874: struct brace_level *level = brace_level_lvalue;
1.99 rillig 875:
1.139 rillig 876: if (level->bl_enclosing->bl_seen_named_member) {
877: level->bl_brace = true;
1.124 rillig 878: debug_step("ARRAY%s%s",
1.139 rillig 879: level->bl_brace ? ", needs closing brace" : "",
880: /* TODO: this is redundant, always true */
881: level->bl_enclosing->bl_seen_named_member
1.124 rillig 882: ? ", seen named member" : "");
1.99 rillig 883: }
884:
1.139 rillig 885: if (is_incomplete(level->bl_type) &&
886: level->bl_enclosing->bl_enclosing != NULL) {
1.99 rillig 887: /* initialization of an incomplete type */
888: error(175);
1.141 rillig 889: set_initerr();
1.99 rillig 890: return;
891: }
892:
1.139 rillig 893: level->bl_subtype = level->bl_type->t_subt;
894: level->bl_array_of_unknown_size = is_incomplete(level->bl_type);
895: level->bl_remaining = level->bl_type->t_dim;
1.146 ! rillig 896: designation_debug(current_designation_mod());
1.99 rillig 897: debug_step("type '%s' remaining %d",
1.139 rillig 898: type_name(level->bl_type), level->bl_remaining);
1.99 rillig 899: }
900:
1.138 rillig 901: static sym_t *
1.139 rillig 902: look_up_member(struct brace_level *level, int *count)
1.99 rillig 903: {
904: sym_t *m;
905:
1.139 rillig 906: for (m = level->bl_type->t_str->sou_first_member;
1.99 rillig 907: m != NULL; m = m->s_next) {
908: if (m->s_bitfield && m->s_name == unnamed)
909: continue;
1.119 rillig 910: /*
911: * TODO: split into separate functions:
912: *
913: * look_up_array_next
914: * look_up_array_designator
915: * look_up_struct_next
916: * look_up_struct_designator
917: */
1.128 rillig 918: if (current_designation().head != NULL) {
1.119 rillig 919: /* XXX: this log entry looks unnecessarily verbose */
1.100 rillig 920: debug_step("have member '%s', want member '%s'",
1.128 rillig 921: m->s_name, current_designation().head->name);
922: if (strcmp(m->s_name,
923: current_designation().head->name) == 0) {
1.138 rillig 924: (*count)++;
1.99 rillig 925: break;
926: } else
927: continue;
928: }
1.138 rillig 929: if (++(*count) == 1) {
1.139 rillig 930: level->bl_next_member = m;
931: level->bl_subtype = m->s_type;
1.99 rillig 932: }
933: }
1.100 rillig 934:
1.138 rillig 935: return m;
936: }
937:
938: /* TODO: document me */
939: /* TODO: think of a better name than 'push' */
940: static bool
941: initstack_push_struct_or_union(void)
942: {
943: /*
944: * TODO: remove unnecessary 'const' for variables in functions that
945: * fit on a single screen. Keep it for larger functions.
946: */
1.139 rillig 947: struct brace_level *level = brace_level_lvalue;
1.138 rillig 948: int cnt;
949: sym_t *m;
950:
1.139 rillig 951: if (is_incomplete(level->bl_type)) {
1.138 rillig 952: /* initialization of an incomplete type */
953: error(175);
1.141 rillig 954: set_initerr();
1.138 rillig 955: return false;
956: }
957:
958: cnt = 0;
1.146 ! rillig 959: designation_debug(current_designation_mod());
1.138 rillig 960: debug_step("lookup for '%s'%s",
1.139 rillig 961: type_name(level->bl_type),
962: level->bl_seen_named_member ? ", seen named member" : "");
1.138 rillig 963:
1.139 rillig 964: m = look_up_member(level, &cnt);
1.138 rillig 965:
1.128 rillig 966: if (current_designation().head != NULL) {
1.99 rillig 967: if (m == NULL) {
968: debug_step("pop struct");
969: return true;
970: }
1.139 rillig 971: level->bl_next_member = m;
972: level->bl_subtype = m->s_type;
973: level->bl_seen_named_member = true;
1.128 rillig 974: debug_step("named member '%s'",
975: current_designation().head->name);
1.146 ! rillig 976: designation_shift_level(current_designation_mod());
1.139 rillig 977: cnt = level->bl_type->t_tspec == STRUCT ? 2 : 1;
1.99 rillig 978: }
1.139 rillig 979: level->bl_brace = true;
1.99 rillig 980: debug_step("unnamed element with type '%s'%s",
1.139 rillig 981: type_name(
982: level->bl_type != NULL ? level->bl_type : level->bl_subtype),
983: level->bl_brace ? ", needs closing brace" : "");
1.99 rillig 984: if (cnt == 0) {
985: /* cannot init. struct/union with no named member */
986: error(179);
1.141 rillig 987: set_initerr();
1.99 rillig 988: return false;
989: }
1.139 rillig 990: level->bl_remaining = level->bl_type->t_tspec == STRUCT ? cnt : 1;
1.99 rillig 991: return false;
992: }
993:
1.119 rillig 994: /* TODO: document me */
995: /* TODO: think of a better name than 'push' */
1.99 rillig 996: static void
1.35 rillig 997: initstack_push(void)
1.1 cgd 998: {
1.139 rillig 999: struct brace_level *level, *enclosing;
1.1 cgd 1000:
1.68 rillig 1001: debug_enter();
1002:
1.97 rillig 1003: extend_if_array_of_unknown_size();
1004:
1.139 rillig 1005: level = brace_level_lvalue;
1006: lint_assert(level->bl_remaining > 0);
1007: lint_assert(level->bl_type == NULL ||
1008: !is_scalar(level->bl_type->t_tspec));
1009:
1010: brace_level_lvalue = xcalloc(1, sizeof *brace_level_lvalue);
1011: brace_level_lvalue->bl_enclosing = level;
1012: brace_level_lvalue->bl_type = level->bl_subtype;
1013: lint_assert(brace_level_lvalue->bl_type->t_tspec != FUNC);
1.1 cgd 1014:
1.12 christos 1015: again:
1.139 rillig 1016: level = brace_level_lvalue;
1.1 cgd 1017:
1.139 rillig 1018: debug_step("expecting type '%s'", type_name(level->bl_type));
1019: lint_assert(level->bl_type != NULL);
1020: switch (level->bl_type->t_tspec) {
1.1 cgd 1021: case ARRAY:
1.128 rillig 1022: if (current_designation().head != NULL) {
1.124 rillig 1023: debug_step("pop array, named member '%s'%s",
1.128 rillig 1024: current_designation().head->name,
1.139 rillig 1025: level->bl_brace ? ", needs closing brace" : "");
1.19 christos 1026: goto pop;
1.98 rillig 1027: }
1028:
1.99 rillig 1029: initstack_push_array();
1030: break;
1.20 christos 1031:
1.1 cgd 1032: case UNION:
1033: if (tflag)
1.89 rillig 1034: /* initialization of union is illegal in trad. C */
1.1 cgd 1035: warning(238);
1036: /* FALLTHROUGH */
1037: case STRUCT:
1.99 rillig 1038: if (initstack_push_struct_or_union())
1039: goto pop;
1.1 cgd 1040: break;
1041: default:
1.128 rillig 1042: if (current_designation().head != NULL) {
1.100 rillig 1043: debug_step("pop scalar");
1.19 christos 1044: pop:
1.119 rillig 1045: /* TODO: extract this into end_initializer_level */
1.139 rillig 1046: enclosing = brace_level_rvalue->bl_enclosing;
1047: free(level);
1048: brace_level_lvalue = enclosing;
1.12 christos 1049: goto again;
1050: }
1.100 rillig 1051: /* The initialization stack now expects a single scalar. */
1.139 rillig 1052: level->bl_remaining = 1;
1.1 cgd 1053: break;
1054: }
1.68 rillig 1055:
1.146 ! rillig 1056: initialization_debug(current_init());
1.68 rillig 1057: debug_leave();
1.1 cgd 1058: }
1059:
1060: static void
1.84 rillig 1061: check_too_many_initializers(void)
1.1 cgd 1062: {
1.139 rillig 1063: const struct brace_level *level = brace_level_rvalue;
1064: if (level->bl_remaining > 0)
1.84 rillig 1065: return;
1.119 rillig 1066: /*
1067: * FIXME: even with named members, there can be too many initializers
1068: */
1.139 rillig 1069: if (level->bl_array_of_unknown_size || level->bl_seen_named_member)
1.84 rillig 1070: return;
1.1 cgd 1071:
1.139 rillig 1072: tspec_t t = level->bl_type->t_tspec;
1.84 rillig 1073: if (t == ARRAY) {
1074: /* too many array initializers, expected %d */
1.139 rillig 1075: error(173, level->bl_type->t_dim);
1.84 rillig 1076: } else if (t == STRUCT || t == UNION) {
1077: /* too many struct/union initializers */
1078: error(172);
1079: } else {
1080: /* too many initializers */
1081: error(174);
1.1 cgd 1082: }
1.141 rillig 1083: set_initerr();
1.1 cgd 1084: }
1085:
1.92 rillig 1086: /*
1087: * Process a '{' in an initializer by starting the initialization of the
1.139 rillig 1088: * nested data structure, with bl_type being the bl_subtype of the outer
1.92 rillig 1089: * initialization level.
1090: */
1.1 cgd 1091: static void
1.37 rillig 1092: initstack_next_brace(void)
1.1 cgd 1093: {
1.7 lukem 1094:
1.68 rillig 1095: debug_enter();
1.146 ! rillig 1096: initialization_debug(current_init());
1.68 rillig 1097:
1.139 rillig 1098: if (brace_level_rvalue->bl_type != NULL &&
1099: is_scalar(brace_level_rvalue->bl_type->t_tspec)) {
1.49 rillig 1100: /* invalid initializer type %s */
1.139 rillig 1101: error(176, type_name(brace_level_rvalue->bl_type));
1.141 rillig 1102: set_initerr();
1.37 rillig 1103: }
1104: if (!initerr)
1.84 rillig 1105: check_too_many_initializers();
1.37 rillig 1106: if (!initerr)
1107: initstack_push();
1108: if (!initerr) {
1.139 rillig 1109: brace_level_lvalue->bl_brace = true;
1.146 ! rillig 1110: designation_debug(current_designation_mod());
1.92 rillig 1111: debug_step("expecting type '%s'",
1.139 rillig 1112: type_name(brace_level_rvalue->bl_type != NULL
1113: ? brace_level_rvalue->bl_type
1114: : brace_level_rvalue->bl_subtype));
1.37 rillig 1115: }
1.68 rillig 1116:
1.146 ! rillig 1117: initialization_debug(current_init());
1.68 rillig 1118: debug_leave();
1.37 rillig 1119: }
1120:
1.119 rillig 1121: /* TODO: document me, or think of a better name */
1.37 rillig 1122: static void
1.118 rillig 1123: initstack_next_nobrace(tnode_t *tn)
1.37 rillig 1124: {
1.68 rillig 1125: debug_enter();
1.37 rillig 1126:
1.139 rillig 1127: if (brace_level_rvalue->bl_type == NULL &&
1128: !is_scalar(brace_level_rvalue->bl_subtype->t_tspec)) {
1.37 rillig 1129: /* {}-enclosed initializer required */
1130: error(181);
1.92 rillig 1131: /* XXX: maybe set initerr here */
1.37 rillig 1132: }
1133:
1.84 rillig 1134: if (!initerr)
1135: check_too_many_initializers();
1136:
1.42 rillig 1137: while (!initerr) {
1.139 rillig 1138: struct brace_level *level = brace_level_lvalue;
1.118 rillig 1139:
1140: if (tn->tn_type->t_tspec == STRUCT &&
1.139 rillig 1141: level->bl_type == tn->tn_type &&
1142: level->bl_enclosing != NULL &&
1143: level->bl_enclosing->bl_enclosing != NULL) {
1144: level->bl_brace = false;
1145: level->bl_remaining = 1; /* the struct itself */
1.118 rillig 1146: break;
1147: }
1148:
1.139 rillig 1149: if (level->bl_type != NULL &&
1150: is_scalar(level->bl_type->t_tspec))
1.42 rillig 1151: break;
1152: initstack_push();
1.1 cgd 1153: }
1.68 rillig 1154:
1.146 ! rillig 1155: initialization_debug(current_init());
1.68 rillig 1156: debug_leave();
1.1 cgd 1157: }
1158:
1.119 rillig 1159: /* TODO: document me */
1.1 cgd 1160: void
1.34 rillig 1161: init_lbrace(void)
1.1 cgd 1162: {
1163: if (initerr)
1164: return;
1165:
1.68 rillig 1166: debug_enter();
1.146 ! rillig 1167: initialization_debug(current_init());
1.68 rillig 1168:
1.1 cgd 1169: if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
1.139 rillig 1170: brace_level_rvalue->bl_enclosing == NULL) {
1171: if (tflag &&
1172: !is_scalar(brace_level_rvalue->bl_subtype->t_tspec))
1.50 rillig 1173: /* no automatic aggregate initialization in trad. C */
1.1 cgd 1174: warning(188);
1175: }
1176:
1177: /*
1178: * Remove all entries which cannot be used for further initializers
1179: * and do not expect a closing brace.
1180: */
1.36 rillig 1181: initstack_pop_nobrace();
1.1 cgd 1182:
1.37 rillig 1183: initstack_next_brace();
1.68 rillig 1184:
1.146 ! rillig 1185: initialization_debug(current_init());
1.68 rillig 1186: debug_leave();
1.1 cgd 1187: }
1188:
1.92 rillig 1189: /*
1190: * Process a '}' in an initializer by finishing the current level of the
1191: * initialization stack.
1192: */
1.1 cgd 1193: void
1.34 rillig 1194: init_rbrace(void)
1.1 cgd 1195: {
1196: if (initerr)
1197: return;
1198:
1.68 rillig 1199: debug_enter();
1.36 rillig 1200: initstack_pop_brace();
1.68 rillig 1201: debug_leave();
1.1 cgd 1202: }
1203:
1.86 rillig 1204: /* In traditional C, bit-fields can be initialized only by integer constants. */
1205: static void
1206: check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
1207: {
1208: if (tflag &&
1209: is_integer(lt) &&
1210: ln->tn_type->t_bitfield &&
1211: !is_integer(rt)) {
1.89 rillig 1212: /* bit-field initialization is illegal in traditional C */
1.86 rillig 1213: warning(186);
1214: }
1215: }
1216:
1.87 rillig 1217: static void
1218: check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
1219: {
1.119 rillig 1220: /* TODO: rename CON to CONSTANT to avoid ambiguity with CONVERT */
1.87 rillig 1221: if (tn == NULL || tn->tn_op == CON)
1222: return;
1223:
1224: sym_t *sym;
1225: ptrdiff_t offs;
1226: if (constant_addr(tn, &sym, &offs))
1227: return;
1228:
1229: if (sclass == AUTO || sclass == REG) {
1230: /* non-constant initializer */
1231: c99ism(177);
1232: } else {
1233: /* non-constant initializer */
1234: error(177);
1235: }
1236: }
1237:
1.114 rillig 1238: /*
1.119 rillig 1239: * Initialize a non-array object with automatic storage duration and only a
1240: * single initializer expression without braces by delegating to ASSIGN.
1.114 rillig 1241: */
1242: static bool
1243: init_using_assign(tnode_t *rn)
1244: {
1245: tnode_t *ln, *tn;
1246:
1247: if (initsym->s_type->t_tspec == ARRAY)
1248: return false;
1.139 rillig 1249: if (brace_level_rvalue->bl_enclosing != NULL)
1.114 rillig 1250: return false;
1251:
1252: debug_step("handing over to ASSIGN");
1.119 rillig 1253:
1.114 rillig 1254: ln = new_name_node(initsym, 0);
1255: ln->tn_type = tduptyp(ln->tn_type);
1256: ln->tn_type->t_const = false;
1.119 rillig 1257:
1.114 rillig 1258: tn = build(ASSIGN, ln, rn);
1259: expr(tn, false, false, false, false);
1.119 rillig 1260:
1.114 rillig 1261: /* XXX: why not clean up the initstack here already? */
1262: return true;
1263: }
1264:
1.116 rillig 1265: static void
1266: check_init_expr(tnode_t *tn, scl_t sclass)
1267: {
1268: tnode_t *ln;
1269: tspec_t lt, rt;
1270: struct mbl *tmem;
1271:
1272: /* Create a temporary node for the left side. */
1.134 rillig 1273: ln = tgetblk(sizeof *ln);
1.116 rillig 1274: ln->tn_op = NAME;
1.139 rillig 1275: ln->tn_type = tduptyp(brace_level_rvalue->bl_type);
1.116 rillig 1276: ln->tn_type->t_const = false;
1277: ln->tn_lvalue = true;
1278: ln->tn_sym = initsym; /* better than nothing */
1279:
1280: tn = cconv(tn);
1281:
1282: lt = ln->tn_type->t_tspec;
1283: rt = tn->tn_type->t_tspec;
1284:
1285: debug_step("typeok '%s', '%s'",
1286: type_name(ln->tn_type), type_name(tn->tn_type));
1287: if (!typeok(INIT, 0, ln, tn))
1288: return;
1289:
1290: /*
1.119 rillig 1291: * Preserve the tree memory. This is necessary because otherwise
1.116 rillig 1292: * expr() would free it.
1293: */
1294: tmem = tsave();
1295: expr(tn, true, false, true, false);
1296: trestor(tmem);
1297:
1298: check_bit_field_init(ln, lt, rt);
1299:
1300: /*
1301: * XXX: Is it correct to do this conversion _after_ the typeok above?
1302: */
1.139 rillig 1303: if (lt != rt ||
1304: (brace_level_rvalue->bl_type->t_bitfield && tn->tn_op == CON))
1305: tn = convert(INIT, 0, brace_level_rvalue->bl_type, tn);
1.116 rillig 1306:
1307: check_non_constant_initializer(tn, sclass);
1308: }
1309:
1.1 cgd 1310: void
1.69 rillig 1311: init_using_expr(tnode_t *tn)
1.1 cgd 1312: {
1.81 rillig 1313: scl_t sclass;
1.1 cgd 1314:
1.68 rillig 1315: debug_enter();
1.146 ! rillig 1316: initialization_debug(current_init());
! 1317: designation_debug(current_designation_mod());
1.95 rillig 1318: debug_step("expr:");
1319: debug_node(tn, debug_ind + 1);
1.55 rillig 1320:
1.113 rillig 1321: if (initerr || tn == NULL)
1322: goto done;
1.1 cgd 1323:
1.81 rillig 1324: sclass = initsym->s_scl;
1.114 rillig 1325: if ((sclass == AUTO || sclass == REG) && init_using_assign(tn))
1.113 rillig 1326: goto done;
1.1 cgd 1327:
1.36 rillig 1328: initstack_pop_nobrace();
1.1 cgd 1329:
1.82 rillig 1330: if (init_array_using_string(tn)) {
1331: debug_step("after initializing the string:");
1332: /* XXX: why not clean up the initstack here already? */
1.113 rillig 1333: goto done_initstack;
1.68 rillig 1334: }
1.1 cgd 1335:
1.118 rillig 1336: initstack_next_nobrace(tn);
1.113 rillig 1337: if (initerr || tn == NULL)
1338: goto done_initstack;
1.1 cgd 1339:
1.139 rillig 1340: brace_level_lvalue->bl_remaining--;
1341: debug_step("%d elements remaining", brace_level_rvalue->bl_remaining);
1.83 rillig 1342:
1.116 rillig 1343: check_init_expr(tn, sclass);
1.68 rillig 1344:
1.113 rillig 1345: done_initstack:
1.146 ! rillig 1346: initialization_debug(current_init());
1.130 rillig 1347:
1.113 rillig 1348: done:
1.130 rillig 1349: while (current_designation().head != NULL)
1.146 ! rillig 1350: designation_shift_level(current_designation_mod());
1.130 rillig 1351:
1.68 rillig 1352: debug_leave();
1.1 cgd 1353: }
1354:
1355:
1.82 rillig 1356: /* Initialize a character array or wchar_t array with a string literal. */
1.62 rillig 1357: static bool
1.82 rillig 1358: init_array_using_string(tnode_t *tn)
1.1 cgd 1359: {
1360: tspec_t t;
1.139 rillig 1361: struct brace_level *level;
1.1 cgd 1362: int len;
1363: strg_t *strg;
1364:
1365: if (tn->tn_op != STRING)
1.63 rillig 1366: return false;
1.1 cgd 1367:
1.68 rillig 1368: debug_enter();
1.146 ! rillig 1369: initialization_debug(current_init());
1.68 rillig 1370:
1.139 rillig 1371: level = brace_level_lvalue;
1.47 rillig 1372: strg = tn->tn_string;
1.1 cgd 1373:
1374: /*
1375: * Check if we have an array type which can be initialized by
1376: * the string.
1377: */
1.139 rillig 1378: if (level->bl_subtype != NULL && level->bl_subtype->t_tspec == ARRAY) {
1.68 rillig 1379: debug_step("subt array");
1.139 rillig 1380: t = level->bl_subtype->t_subt->t_tspec;
1.1 cgd 1381: if (!((strg->st_tspec == CHAR &&
1382: (t == CHAR || t == UCHAR || t == SCHAR)) ||
1383: (strg->st_tspec == WCHAR && t == WCHAR))) {
1.68 rillig 1384: debug_leave();
1.63 rillig 1385: return false;
1.1 cgd 1386: }
1.82 rillig 1387: /* XXX: duplicate code, see below */
1.119 rillig 1388:
1.1 cgd 1389: /* Put the array at top of stack */
1.35 rillig 1390: initstack_push();
1.139 rillig 1391: level = brace_level_lvalue;
1.119 rillig 1392:
1.139 rillig 1393: /* TODO: what if both bl_type and bl_subtype are ARRAY? */
1.119 rillig 1394:
1.139 rillig 1395: } else if (level->bl_type != NULL && level->bl_type->t_tspec == ARRAY) {
1.68 rillig 1396: debug_step("type array");
1.139 rillig 1397: t = level->bl_type->t_subt->t_tspec;
1.1 cgd 1398: if (!((strg->st_tspec == CHAR &&
1399: (t == CHAR || t == UCHAR || t == SCHAR)) ||
1400: (strg->st_tspec == WCHAR && t == WCHAR))) {
1.68 rillig 1401: debug_leave();
1.63 rillig 1402: return false;
1.1 cgd 1403: }
1.82 rillig 1404: /* XXX: duplicate code, see above */
1.119 rillig 1405:
1406: /*
1407: * TODO: is this really not needed in the branch above this
1408: * one?
1409: */
1.1 cgd 1410: /*
1411: * If the array is already partly initialized, we are
1412: * wrong here.
1413: */
1.139 rillig 1414: if (level->bl_remaining != level->bl_type->t_dim) {
1.68 rillig 1415: debug_leave();
1.63 rillig 1416: return false;
1.115 rillig 1417: }
1.1 cgd 1418: } else {
1.68 rillig 1419: debug_leave();
1.63 rillig 1420: return false;
1.1 cgd 1421: }
1422:
1423: /* Get length without trailing NUL character. */
1424: len = strg->st_len;
1425:
1.139 rillig 1426: if (level->bl_array_of_unknown_size) {
1427: level->bl_array_of_unknown_size = false;
1428: level->bl_type->t_dim = len + 1;
1429: setcomplete(level->bl_type, true);
1.1 cgd 1430: } else {
1.119 rillig 1431: /*
1432: * TODO: check for buffer overflow in the object to be
1433: * initialized
1434: */
1435: /* XXX: double-check for off-by-one error */
1.139 rillig 1436: if (level->bl_type->t_dim < len) {
1.1 cgd 1437: /* non-null byte ignored in string initializer */
1438: warning(187);
1439: }
1.119 rillig 1440:
1441: /*
1442: * TODO: C99 6.7.8p14 allows a string literal to be enclosed
1443: * in optional redundant braces, just like scalars. Add tests
1444: * for this.
1445: */
1.1 cgd 1446: }
1447:
1448: /* In every case the array is initialized completely. */
1.139 rillig 1449: level->bl_remaining = 0;
1.1 cgd 1450:
1.146 ! rillig 1451: initialization_debug(current_init());
1.68 rillig 1452: debug_leave();
1.63 rillig 1453: return true;
1.1 cgd 1454: }
CVSweb <webmaster@jp.NetBSD.org>