Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.111
      1 /*	$NetBSD: init.c,v 1.111 2021/03/23 18:40:50 rillig Exp $	*/
      2 
      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 
     34 #if HAVE_NBTOOL_CONFIG_H
     35 #include "nbtool_config.h"
     36 #endif
     37 
     38 #include <sys/cdefs.h>
     39 #if defined(__RCSID) && !defined(lint)
     40 __RCSID("$NetBSD: init.c,v 1.111 2021/03/23 18:40:50 rillig Exp $");
     41 #endif
     42 
     43 #include <stdlib.h>
     44 #include <string.h>
     45 
     46 #include "lint1.h"
     47 
     48 
     49 /*
     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 };
     62  *	struct { int x, y; } point = { .y = 3, .x = 4 };
     63  *
     64  * The initializer that follows the '=' may be surrounded by an extra pair of
     65  * braces, like in the example 'number_with_braces'.  For multi-dimensional
     66  * arrays, the inner braces may be omitted like in array_flat or spelled out
     67  * like in array_nested.
     68  *
     69  * For the initializer, the grammar parser calls these functions:
     70  *
     71  *	init_lbrace	for each '{'
     72  *	init_using_expr	for each value
     73  *	init_rbrace	for each '}'
     74  *
     75  * The state of the current initialization is stored in initstk, a stack of
     76  * initstack_element, one element per type aggregate level.
     77  *
     78  * Most of the time, the topmost level of initstk contains a scalar type, and
     79  * its remaining count toggles between 1 and 0.
     80  *
     81  * See also:
     82  *	C99 6.7.8 "Initialization"
     83  *	d_c99_init.c for more examples
     84  */
     85 
     86 
     87 /*
     88  * Type of stack which is used for initialization of aggregate types.
     89  *
     90  * XXX: Since C99, a stack is an inappropriate data structure for modelling
     91  * an initialization, since the designators don't have to be listed in a
     92  * particular order and can designate parts of sub-objects.  The member names
     93  * of non-leaf structs may thus appear repeatedly, as demonstrated in
     94  * d_init_pop_member.c.
     95  *
     96  * XXX: During initialization, there may be members of the top-level struct
     97  * that are partially initialized.  The simple i_remaining cannot model this
     98  * appropriately.
     99  *
    100  * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
    101  * selected examples.
    102  */
    103 typedef	struct initstack_element {
    104 
    105 	/*
    106 	 * The type to be initialized at this level.
    107 	 *
    108 	 * On the outermost element, this is always NULL since the outermost
    109 	 * initializer-expression may be enclosed in an optional pair of
    110 	 * braces.  This optional pair of braces is handled by the combination
    111 	 * of i_type and i_subt.
    112 	 *
    113 	 * Everywhere else it is nonnull.
    114 	 */
    115 	type_t	*i_type;
    116 
    117 	/*
    118 	 * The type that will be initialized at the next initialization level,
    119 	 * usually enclosed by another pair of braces.
    120 	 *
    121 	 * For an array, it is the element type, but without 'const'.
    122 	 *
    123 	 * For a struct or union type, it is one of the member types, but
    124 	 * without 'const'.
    125 	 *
    126 	 * The outermost stack element has no i_type but nevertheless has
    127 	 * i_subt.  For example, in 'int var = { 12345 }', initially there is
    128 	 * an initstack_element with i_subt 'int'.  When the '{' is processed,
    129 	 * an element with i_type 'int' is pushed to the stack.  When the
    130 	 * corresponding '}' is processed, the inner element is popped again.
    131 	 *
    132 	 * During initialization, only the top 2 elements of the stack are
    133 	 * looked at.
    134 	 */
    135 	type_t	*i_subt;
    136 
    137 	/*
    138 	 * This level of the initializer requires a '}' to be completed.
    139 	 *
    140 	 * Multidimensional arrays do not need a closing brace to complete
    141 	 * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
    142 	 * for int arr[2][2].
    143 	 *
    144 	 * TODO: Do structs containing structs need a closing brace?
    145 	 * TODO: Do arrays of structs need a closing brace after each struct?
    146 	 */
    147 	bool i_brace: 1;
    148 
    149 	/* Whether i_type is an array of unknown size. */
    150 	bool i_array_of_unknown_size: 1;
    151 	bool i_seen_named_member: 1;
    152 
    153 	/*
    154 	 * For structs, the next member to be initialized by an initializer
    155 	 * without an optional designator.
    156 	 */
    157 	sym_t *i_current_object;
    158 
    159 	/*
    160 	 * The number of remaining elements to be used by expressions without
    161 	 * designator.
    162 	 *
    163 	 * This says nothing about which members have been initialized or not
    164 	 * since starting with C99, members may be initialized in arbitrary
    165 	 * order by using designators.
    166 	 *
    167 	 * For an array of unknown size, this is always 0 and thus irrelevant.
    168 	 *
    169 	 * XXX: for scalars?
    170 	 * XXX: for structs?
    171 	 * XXX: for unions?
    172 	 * XXX: for arrays?
    173 	 */
    174 	int i_remaining;
    175 
    176 	/*
    177 	 * The initialization state of the enclosing data structure
    178 	 * (struct, union, array).
    179 	 */
    180 	struct initstack_element *i_enclosing;
    181 } initstack_element;
    182 
    183 /*
    184  * The names for a nested C99 initialization designator, in a circular list.
    185  *
    186  * Example:
    187  *	struct stat st = {
    188  *		.st_size = 123,
    189  *		.st_mtim.tv_sec = 45,
    190  *		.st_mtim.tv_nsec
    191  *	};
    192  *
    193  *	During initialization, this list first contains ["st_size"], then
    194  *	["st_mtim", "tv_sec"], then ["st_mtim", "tv_nsec"].
    195  */
    196 typedef struct namlist {
    197 	const char *n_name;
    198 	struct namlist *n_prev;
    199 	struct namlist *n_next;
    200 } namlist_t;
    201 
    202 struct initialization {
    203 	/*
    204 	 * is set as soon as a fatal error occurred in the initialization.
    205 	 * The effect is that the rest of the initialization is ignored
    206 	 * (parsed by yacc, expression trees built, but no initialization
    207 	 * takes place).
    208 	 */
    209 	bool	initerr;
    210 
    211 	/* Pointer to the symbol which is to be initialized. */
    212 	sym_t	*initsym;
    213 
    214 	/* Points to the top element of the initialization stack. */
    215 	initstack_element *initstk;
    216 
    217 	/* Points to a c9x named member. */
    218 	namlist_t *namedmem;
    219 
    220 	struct initialization *next;
    221 };
    222 
    223 static struct initialization *init;
    224 
    225 
    226 static	bool	init_array_using_string(tnode_t *);
    227 
    228 bool *
    229 current_initerr(void)
    230 {
    231 	lint_assert(init != NULL);
    232 	return &init->initerr;
    233 }
    234 
    235 sym_t **
    236 current_initsym(void)
    237 {
    238 	lint_assert(init != NULL);
    239 	return &init->initsym;
    240 }
    241 
    242 static namlist_t **
    243 current_namedmem(void)
    244 {
    245 	lint_assert(init != NULL);
    246 	return &init->namedmem;
    247 }
    248 
    249 static initstack_element **
    250 current_initstk(void)
    251 {
    252 	lint_assert(init != NULL);
    253 	return &init->initstk;
    254 }
    255 
    256 #define initerr (*current_initerr())
    257 #define initsym (*current_initsym())
    258 #define initstk (*current_initstk())
    259 #define namedmem (*current_namedmem())
    260 
    261 #ifndef DEBUG
    262 
    263 #define debug_printf(fmt, ...)	do { } while (false)
    264 #define debug_indent()		do { } while (false)
    265 #define debug_enter(a)		do { } while (false)
    266 #define debug_step(fmt, ...)	do { } while (false)
    267 #define debug_leave(a)		do { } while (false)
    268 #define debug_named_member()	do { } while (false)
    269 #define debug_initstack_element(elem) do { } while (false)
    270 #define debug_initstack()	do { } while (false)
    271 
    272 #else
    273 
    274 static int debug_ind = 0;
    275 
    276 static void __printflike(1, 2)
    277 debug_printf(const char *fmt, ...)
    278 {
    279 	va_list va;
    280 
    281 	va_start(va, fmt);
    282 	vfprintf(stdout, fmt, va);
    283 	va_end(va);
    284 }
    285 
    286 static void
    287 debug_indent(void)
    288 {
    289 	debug_printf("%*s", 2 * debug_ind, "");
    290 }
    291 
    292 static void
    293 debug_enter(const char *func)
    294 {
    295 	printf("%*s+ %s\n", 2 * debug_ind++, "", func);
    296 }
    297 
    298 static void __printflike(1, 2)
    299 debug_step(const char *fmt, ...)
    300 {
    301 	va_list va;
    302 
    303 	printf("%*s", 2 * debug_ind, "");
    304 	va_start(va, fmt);
    305 	vfprintf(stdout, fmt, va);
    306 	va_end(va);
    307 	printf("\n");
    308 }
    309 
    310 static void
    311 debug_leave(const char *func)
    312 {
    313 	printf("%*s- %s\n", 2 * --debug_ind, "", func);
    314 }
    315 
    316 static void
    317 debug_named_member(void)
    318 {
    319 	namlist_t *name;
    320 
    321 	if (namedmem == NULL)
    322 		return;
    323 	name = namedmem;
    324 	debug_indent();
    325 	debug_printf("named member: ");
    326 	do {
    327 		debug_printf(".%s", name->n_name);
    328 		name = name->n_next;
    329 	} while (name != namedmem);
    330 	debug_printf("\n");
    331 }
    332 
    333 static void
    334 debug_initstack_element(const initstack_element *elem)
    335 {
    336 	if (elem->i_type != NULL)
    337 		debug_step("  i_type           = %s", type_name(elem->i_type));
    338 	if (elem->i_subt != NULL)
    339 		debug_step("  i_subt           = %s", type_name(elem->i_subt));
    340 
    341 	if (elem->i_brace)
    342 		debug_step("  i_brace");
    343 	if (elem->i_array_of_unknown_size)
    344 		debug_step("  i_array_of_unknown_size");
    345 	if (elem->i_seen_named_member)
    346 		debug_step("  i_seen_named_member");
    347 
    348 	const type_t *eff_type = elem->i_type != NULL
    349 	    ? elem->i_type : elem->i_subt;
    350 	if (eff_type->t_tspec == STRUCT && elem->i_current_object != NULL)
    351 		debug_step("  i_current_object = %s",
    352 		    elem->i_current_object->s_name);
    353 
    354 	debug_step("  i_remaining      = %d", elem->i_remaining);
    355 }
    356 
    357 static void
    358 debug_initstack(void)
    359 {
    360 	if (initstk == NULL) {
    361 		debug_step("initstk is empty");
    362 		return;
    363 	}
    364 
    365 	size_t i = 0;
    366 	for (const initstack_element *elem = initstk;
    367 	     elem != NULL; elem = elem->i_enclosing) {
    368 		debug_step("initstk[%zu]:", i);
    369 		debug_initstack_element(elem);
    370 		i++;
    371 	}
    372 }
    373 
    374 #define debug_enter() debug_enter(__func__)
    375 #define debug_leave() debug_leave(__func__)
    376 
    377 #endif
    378 
    379 
    380 void
    381 begin_initialization(sym_t *sym)
    382 {
    383 	struct initialization *curr_init;
    384 
    385 	debug_step("begin initialization");
    386 	curr_init = xcalloc(1, sizeof *curr_init);
    387 	curr_init->next = init;
    388 	init = curr_init;
    389 	initsym = sym;
    390 }
    391 
    392 void
    393 end_initialization(void)
    394 {
    395 	struct initialization *curr_init;
    396 
    397 	curr_init = init;
    398 	init = init->next;
    399 	free(curr_init);
    400 	debug_step("end initialization");
    401 }
    402 
    403 void
    404 designator_push_name(sbuf_t *sb)
    405 {
    406 	namlist_t *nam = xcalloc(1, sizeof (namlist_t));
    407 	nam->n_name = sb->sb_name;
    408 
    409 	if (namedmem == NULL) {
    410 		/*
    411 		 * XXX: Why is this a circular list?
    412 		 * XXX: Why is this a doubly-linked list?
    413 		 * A simple stack should suffice.
    414 		 */
    415 		nam->n_prev = nam->n_next = nam;
    416 		namedmem = nam;
    417 	} else {
    418 		namedmem->n_prev->n_next = nam;
    419 		nam->n_prev = namedmem->n_prev;
    420 		nam->n_next = namedmem;
    421 		namedmem->n_prev = nam;
    422 	}
    423 
    424 	debug_named_member();
    425 }
    426 
    427 /*
    428  * A struct member that has array type is initialized using a designator.
    429  *
    430  * C99 example: struct { int member[4]; } var = { [2] = 12345 };
    431  *
    432  * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
    433  */
    434 void
    435 designator_push_subscript(range_t range)
    436 {
    437 	debug_enter();
    438 	debug_step("subscript range is %zu ... %zu", range.lo, range.hi);
    439 	debug_initstack();
    440 	debug_leave();
    441 }
    442 
    443 static void
    444 designator_shift_name(void)
    445 {
    446 	if (namedmem->n_next == namedmem) {
    447 		free(namedmem);
    448 		namedmem = NULL;
    449 	} else {
    450 		namlist_t *nam = namedmem;
    451 		namedmem = namedmem->n_next;
    452 		nam->n_prev->n_next = nam->n_next;
    453 		nam->n_next->n_prev = nam->n_prev;
    454 		free(nam);
    455 	}
    456 
    457 	debug_named_member();
    458 }
    459 
    460 /*
    461  * Initialize the initialization stack by putting an entry for the object
    462  * which is to be initialized on it.
    463  */
    464 void
    465 initstack_init(void)
    466 {
    467 	initstack_element *istk;
    468 
    469 	if (initerr)
    470 		return;
    471 
    472 	/* free memory used in last initialization */
    473 	while ((istk = initstk) != NULL) {
    474 		initstk = istk->i_enclosing;
    475 		free(istk);
    476 	}
    477 	while (namedmem != NULL)
    478 		designator_shift_name();
    479 
    480 	debug_enter();
    481 
    482 	/*
    483 	 * If the type which is to be initialized is an incomplete array,
    484 	 * it must be duplicated.
    485 	 */
    486 	if (initsym->s_type->t_tspec == ARRAY && is_incomplete(initsym->s_type))
    487 		initsym->s_type = duptyp(initsym->s_type);
    488 
    489 	istk = initstk = xcalloc(1, sizeof (initstack_element));
    490 	istk->i_subt = initsym->s_type;
    491 	istk->i_remaining = 1;
    492 
    493 	debug_initstack();
    494 	debug_leave();
    495 }
    496 
    497 static void
    498 initstack_pop_item_named_member(void)
    499 {
    500 	initstack_element *istk = initstk;
    501 	sym_t *m;
    502 
    503 	debug_step("initializing named member '%s'", namedmem->n_name);
    504 
    505 	if (istk->i_type->t_tspec != STRUCT &&
    506 	    istk->i_type->t_tspec != UNION) {
    507 		/* syntax error '%s' */
    508 		error(249, "named member must only be used with struct/union");
    509 		initerr = true;
    510 		return;
    511 	}
    512 
    513 	for (m = istk->i_type->t_str->sou_first_member;
    514 	     m != NULL; m = m->s_next) {
    515 
    516 		if (m->s_bitfield && m->s_name == unnamed)
    517 			continue;
    518 
    519 		if (strcmp(m->s_name, namedmem->n_name) == 0) {
    520 			debug_step("found matching member");
    521 			istk->i_subt = m->s_type;
    522 			/* XXX: why ++? */
    523 			istk->i_remaining++;
    524 			/* XXX: why is i_seen_named_member not set? */
    525 			designator_shift_name();
    526 			return;
    527 		}
    528 	}
    529 
    530 	/* undefined struct/union member: %s */
    531 	error(101, namedmem->n_name);
    532 
    533 	designator_shift_name();
    534 	istk->i_seen_named_member = true;
    535 }
    536 
    537 static void
    538 initstack_pop_item_unnamed(void)
    539 {
    540 	initstack_element *istk = initstk;
    541 	sym_t *m;
    542 
    543 	/*
    544 	 * If the removed element was a structure member, we must go
    545 	 * to the next structure member.
    546 	 */
    547 	if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
    548 	    !istk->i_seen_named_member) {
    549 		do {
    550 			m = istk->i_current_object =
    551 			    istk->i_current_object->s_next;
    552 			/* XXX: can this assertion be made to fail? */
    553 			lint_assert(m != NULL);
    554 			debug_step("pop %s", m->s_name);
    555 		} while (m->s_bitfield && m->s_name == unnamed);
    556 		/* XXX: duplicate code for skipping unnamed bit-fields */
    557 		istk->i_subt = m->s_type;
    558 	}
    559 }
    560 
    561 static void
    562 initstack_pop_item(void)
    563 {
    564 	initstack_element *istk;
    565 
    566 	debug_enter();
    567 
    568 	istk = initstk;
    569 	debug_step("popping:");
    570 	debug_initstack_element(istk);
    571 
    572 	initstk = istk->i_enclosing;
    573 	free(istk);
    574 	istk = initstk;
    575 	lint_assert(istk != NULL);
    576 
    577 	istk->i_remaining--;
    578 	lint_assert(istk->i_remaining >= 0);
    579 	debug_step("%d elements remaining", istk->i_remaining);
    580 
    581 	if (namedmem != NULL)
    582 		initstack_pop_item_named_member();
    583 	else
    584 		initstack_pop_item_unnamed();
    585 
    586 	debug_initstack();
    587 	debug_leave();
    588 }
    589 
    590 /*
    591  * Take all entries, including the first which requires a closing brace,
    592  * from the stack.
    593  */
    594 static void
    595 initstack_pop_brace(void)
    596 {
    597 	bool brace;
    598 
    599 	debug_enter();
    600 	debug_initstack();
    601 	do {
    602 		brace = initstk->i_brace;
    603 		debug_step("loop brace=%d", brace);
    604 		initstack_pop_item();
    605 	} while (!brace);
    606 	debug_initstack();
    607 	debug_leave();
    608 }
    609 
    610 /*
    611  * Take all entries which cannot be used for further initializers from the
    612  * stack, but do this only if they do not require a closing brace.
    613  */
    614 static void
    615 initstack_pop_nobrace(void)
    616 {
    617 
    618 	debug_enter();
    619 	while (!initstk->i_brace && initstk->i_remaining == 0 &&
    620 	       !initstk->i_array_of_unknown_size)
    621 		initstack_pop_item();
    622 	debug_leave();
    623 }
    624 
    625 /* Extend an array of unknown size by one element */
    626 static void
    627 extend_if_array_of_unknown_size(void)
    628 {
    629 	initstack_element *istk = initstk;
    630 
    631 	if (istk->i_remaining != 0)
    632 		return;
    633 
    634 	/*
    635 	 * The only place where an incomplete array may appear is at the
    636 	 * outermost aggregate level of the object to be initialized.
    637 	 */
    638 	lint_assert(istk->i_enclosing->i_enclosing == NULL);
    639 	lint_assert(istk->i_type->t_tspec == ARRAY);
    640 
    641 	debug_step("extending array of unknown size '%s'",
    642 	    type_name(istk->i_type));
    643 	istk->i_remaining = 1;
    644 	istk->i_type->t_dim++;
    645 	setcomplete(istk->i_type, true);
    646 
    647 	debug_step("extended type is '%s'", type_name(istk->i_type));
    648 }
    649 
    650 static void
    651 initstack_push_array(void)
    652 {
    653 	initstack_element *const istk = initstk;
    654 
    655 	if (istk->i_enclosing->i_seen_named_member) {
    656 		istk->i_brace = true;
    657 		debug_step("ARRAY brace=%d, namedmem=%d",
    658 		    istk->i_brace, istk->i_enclosing->i_seen_named_member);
    659 	}
    660 
    661 	if (is_incomplete(istk->i_type) &&
    662 	    istk->i_enclosing->i_enclosing != NULL) {
    663 		/* initialization of an incomplete type */
    664 		error(175);
    665 		initerr = true;
    666 		return;
    667 	}
    668 
    669 	istk->i_subt = istk->i_type->t_subt;
    670 	istk->i_array_of_unknown_size = is_incomplete(istk->i_type);
    671 	istk->i_remaining = istk->i_type->t_dim;
    672 	debug_named_member();
    673 	debug_step("type '%s' remaining %d",
    674 	    type_name(istk->i_type), istk->i_remaining);
    675 }
    676 
    677 static bool
    678 initstack_push_struct_or_union(void)
    679 {
    680 	initstack_element *const istk = initstk;
    681 	int cnt;
    682 	sym_t *m;
    683 
    684 	if (is_incomplete(istk->i_type)) {
    685 		/* initialization of an incomplete type */
    686 		error(175);
    687 		initerr = true;
    688 		return false;
    689 	}
    690 
    691 	cnt = 0;
    692 	debug_named_member();
    693 	debug_step("lookup for '%s'%s",
    694 	    type_name(istk->i_type),
    695 	    istk->i_seen_named_member ? ", seen named member" : "");
    696 
    697 	for (m = istk->i_type->t_str->sou_first_member;
    698 	     m != NULL; m = m->s_next) {
    699 		if (m->s_bitfield && m->s_name == unnamed)
    700 			continue;
    701 		if (namedmem != NULL) {
    702 			debug_step("have member '%s', want member '%s'",
    703 			    m->s_name, namedmem->n_name);
    704 			if (strcmp(m->s_name, namedmem->n_name) == 0) {
    705 				cnt++;
    706 				break;
    707 			} else
    708 				continue;
    709 		}
    710 		if (++cnt == 1) {
    711 			istk->i_current_object = m;
    712 			istk->i_subt = m->s_type;
    713 		}
    714 	}
    715 
    716 	if (namedmem != NULL) {
    717 		if (m == NULL) {
    718 			debug_step("pop struct");
    719 			return true;
    720 		}
    721 		istk->i_current_object = m;
    722 		istk->i_subt = m->s_type;
    723 		istk->i_seen_named_member = true;
    724 		debug_step("named member '%s'", namedmem->n_name);
    725 		designator_shift_name();
    726 		cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
    727 	}
    728 	istk->i_brace = true;
    729 	debug_step("unnamed element with type '%s'%s",
    730 	    type_name(istk->i_type != NULL ? istk->i_type : istk->i_subt),
    731 	    istk->i_brace ? ", needs closing brace" : "");
    732 	if (cnt == 0) {
    733 		/* cannot init. struct/union with no named member */
    734 		error(179);
    735 		initerr = true;
    736 		return false;
    737 	}
    738 	istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
    739 	return false;
    740 }
    741 
    742 static void
    743 initstack_push(void)
    744 {
    745 	initstack_element *istk, *inxt;
    746 
    747 	debug_enter();
    748 
    749 	extend_if_array_of_unknown_size();
    750 
    751 	istk = initstk;
    752 	lint_assert(istk->i_remaining > 0);
    753 	lint_assert(istk->i_type == NULL || !is_scalar(istk->i_type->t_tspec));
    754 
    755 	initstk = xcalloc(1, sizeof (initstack_element));
    756 	initstk->i_enclosing = istk;
    757 	initstk->i_type = istk->i_subt;
    758 	lint_assert(initstk->i_type->t_tspec != FUNC);
    759 
    760 again:
    761 	istk = initstk;
    762 
    763 	debug_step("expecting type '%s'", type_name(istk->i_type));
    764 	lint_assert(istk->i_type != NULL);
    765 	switch (istk->i_type->t_tspec) {
    766 	case ARRAY:
    767 		if (namedmem != NULL) {
    768 			debug_step("pop array namedmem=%s brace=%d",
    769 			    namedmem->n_name, istk->i_brace);
    770 			goto pop;
    771 		}
    772 
    773 		initstack_push_array();
    774 		break;
    775 
    776 	case UNION:
    777 		if (tflag)
    778 			/* initialization of union is illegal in trad. C */
    779 			warning(238);
    780 		/* FALLTHROUGH */
    781 	case STRUCT:
    782 		if (initstack_push_struct_or_union())
    783 			goto pop;
    784 		break;
    785 	default:
    786 		if (namedmem != NULL) {
    787 			debug_step("pop scalar");
    788 	pop:
    789 			inxt = initstk->i_enclosing;
    790 			free(istk);
    791 			initstk = inxt;
    792 			goto again;
    793 		}
    794 		/* The initialization stack now expects a single scalar. */
    795 		istk->i_remaining = 1;
    796 		break;
    797 	}
    798 
    799 	debug_initstack();
    800 	debug_leave();
    801 }
    802 
    803 static void
    804 check_too_many_initializers(void)
    805 {
    806 
    807 	const initstack_element *istk = initstk;
    808 	if (istk->i_remaining > 0)
    809 		return;
    810 	if (istk->i_array_of_unknown_size || istk->i_seen_named_member)
    811 		return;
    812 
    813 	tspec_t t = istk->i_type->t_tspec;
    814 	if (t == ARRAY) {
    815 		/* too many array initializers, expected %d */
    816 		error(173, istk->i_type->t_dim);
    817 	} else if (t == STRUCT || t == UNION) {
    818 		/* too many struct/union initializers */
    819 		error(172);
    820 	} else {
    821 		/* too many initializers */
    822 		error(174);
    823 	}
    824 	initerr = true;
    825 }
    826 
    827 /*
    828  * Process a '{' in an initializer by starting the initialization of the
    829  * nested data structure, with i_type being the i_subt of the outer
    830  * initialization level.
    831  */
    832 static void
    833 initstack_next_brace(void)
    834 {
    835 
    836 	debug_enter();
    837 	debug_initstack();
    838 
    839 	if (initstk->i_type != NULL && is_scalar(initstk->i_type->t_tspec)) {
    840 		/* invalid initializer type %s */
    841 		error(176, type_name(initstk->i_type));
    842 		initerr = true;
    843 	}
    844 	if (!initerr)
    845 		check_too_many_initializers();
    846 	if (!initerr)
    847 		initstack_push();
    848 	if (!initerr) {
    849 		initstk->i_brace = true;
    850 		debug_named_member();
    851 		debug_step("expecting type '%s'",
    852 		    type_name(initstk->i_type != NULL ? initstk->i_type
    853 			: initstk->i_subt));
    854 	}
    855 
    856 	debug_initstack();
    857 	debug_leave();
    858 }
    859 
    860 static void
    861 initstack_next_nobrace(void)
    862 {
    863 	debug_enter();
    864 
    865 	if (initstk->i_type == NULL && !is_scalar(initstk->i_subt->t_tspec)) {
    866 		/* {}-enclosed initializer required */
    867 		error(181);
    868 		/* XXX: maybe set initerr here */
    869 	}
    870 
    871 	if (!initerr)
    872 		check_too_many_initializers();
    873 
    874 	/*
    875 	 * Make sure an entry with a scalar type is at the top of the stack.
    876 	 *
    877 	 * FIXME: Since C99, an initializer for an object with automatic
    878 	 *  storage need not be a constant expression anymore.  It is
    879 	 *  perfectly fine to initialize a struct with a struct expression,
    880 	 *  see d_struct_init_nested.c for a demonstration.
    881 	 */
    882 	while (!initerr) {
    883 		if ((initstk->i_type != NULL &&
    884 		     is_scalar(initstk->i_type->t_tspec)))
    885 			break;
    886 		initstack_push();
    887 	}
    888 
    889 	debug_initstack();
    890 	debug_leave();
    891 }
    892 
    893 void
    894 init_lbrace(void)
    895 {
    896 	if (initerr)
    897 		return;
    898 
    899 	debug_enter();
    900 	debug_initstack();
    901 
    902 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
    903 	    initstk->i_enclosing == NULL) {
    904 		if (tflag && !is_scalar(initstk->i_subt->t_tspec))
    905 			/* no automatic aggregate initialization in trad. C */
    906 			warning(188);
    907 	}
    908 
    909 	/*
    910 	 * Remove all entries which cannot be used for further initializers
    911 	 * and do not expect a closing brace.
    912 	 */
    913 	initstack_pop_nobrace();
    914 
    915 	initstack_next_brace();
    916 
    917 	debug_initstack();
    918 	debug_leave();
    919 }
    920 
    921 /*
    922  * Process a '}' in an initializer by finishing the current level of the
    923  * initialization stack.
    924  */
    925 void
    926 init_rbrace(void)
    927 {
    928 	if (initerr)
    929 		return;
    930 
    931 	debug_enter();
    932 	initstack_pop_brace();
    933 	debug_leave();
    934 }
    935 
    936 /* In traditional C, bit-fields can be initialized only by integer constants. */
    937 static void
    938 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
    939 {
    940 	if (tflag &&
    941 	    is_integer(lt) &&
    942 	    ln->tn_type->t_bitfield &&
    943 	    !is_integer(rt)) {
    944 		/* bit-field initialization is illegal in traditional C */
    945 		warning(186);
    946 	}
    947 }
    948 
    949 static void
    950 check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
    951 {
    952 	if (tn == NULL || tn->tn_op == CON)
    953 		return;
    954 
    955 	sym_t *sym;
    956 	ptrdiff_t offs;
    957 	if (constant_addr(tn, &sym, &offs))
    958 		return;
    959 
    960 	if (sclass == AUTO || sclass == REG) {
    961 		/* non-constant initializer */
    962 		c99ism(177);
    963 	} else {
    964 		/* non-constant initializer */
    965 		error(177);
    966 	}
    967 }
    968 
    969 void
    970 init_using_expr(tnode_t *tn)
    971 {
    972 	tspec_t	lt, rt;
    973 	tnode_t	*ln;
    974 	struct	mbl *tmem;
    975 	scl_t	sclass;
    976 
    977 	debug_enter();
    978 	debug_initstack();
    979 	debug_named_member();
    980 	debug_step("expr:");
    981 	debug_node(tn, debug_ind + 1);
    982 
    983 	if (initerr || tn == NULL) {
    984 		debug_leave();
    985 		return;
    986 	}
    987 
    988 	sclass = initsym->s_scl;
    989 
    990 	/*
    991 	 * Do not test for automatic aggregate initialization. If the
    992 	 * initializer starts with a brace we have the warning already.
    993 	 * If not, an error will be printed that the initializer must
    994 	 * be enclosed by braces.
    995 	 */
    996 
    997 	/*
    998 	 * Local initialization of non-array-types with only one expression
    999 	 * without braces is done by ASSIGN
   1000 	 */
   1001 	if ((sclass == AUTO || sclass == REG) &&
   1002 	    initsym->s_type->t_tspec != ARRAY && initstk->i_enclosing == NULL) {
   1003 		debug_step("handing over to ASSIGN");
   1004 		ln = new_name_node(initsym, 0);
   1005 		ln->tn_type = tduptyp(ln->tn_type);
   1006 		ln->tn_type->t_const = false;
   1007 		tn = build(ASSIGN, ln, tn);
   1008 		expr(tn, false, false, false, false);
   1009 		/* XXX: why not clean up the initstack here already? */
   1010 		debug_leave();
   1011 		return;
   1012 	}
   1013 
   1014 	initstack_pop_nobrace();
   1015 
   1016 	if (init_array_using_string(tn)) {
   1017 		debug_step("after initializing the string:");
   1018 		/* XXX: why not clean up the initstack here already? */
   1019 		debug_initstack();
   1020 		debug_leave();
   1021 		return;
   1022 	}
   1023 
   1024 	initstack_next_nobrace();
   1025 	if (initerr || tn == NULL) {
   1026 		debug_initstack();
   1027 		debug_leave();
   1028 		return;
   1029 	}
   1030 
   1031 	initstk->i_remaining--;
   1032 	debug_step("%d elements remaining", initstk->i_remaining);
   1033 
   1034 	/* Create a temporary node for the left side. */
   1035 	ln = tgetblk(sizeof (tnode_t));
   1036 	ln->tn_op = NAME;
   1037 	ln->tn_type = tduptyp(initstk->i_type);
   1038 	ln->tn_type->t_const = false;
   1039 	ln->tn_lvalue = true;
   1040 	ln->tn_sym = initsym;		/* better than nothing */
   1041 
   1042 	tn = cconv(tn);
   1043 
   1044 	lt = ln->tn_type->t_tspec;
   1045 	rt = tn->tn_type->t_tspec;
   1046 
   1047 	lint_assert(is_scalar(lt));	/* at least before C99 */
   1048 
   1049 	debug_step("typeok '%s', '%s'",
   1050 	    type_name(ln->tn_type), type_name(tn->tn_type));
   1051 	if (!typeok(INIT, 0, ln, tn)) {
   1052 		debug_initstack();
   1053 		debug_leave();
   1054 		return;
   1055 	}
   1056 
   1057 	/*
   1058 	 * Store the tree memory. This is necessary because otherwise
   1059 	 * expr() would free it.
   1060 	 */
   1061 	tmem = tsave();
   1062 	expr(tn, true, false, true, false);
   1063 	trestor(tmem);
   1064 
   1065 	check_bit_field_init(ln, lt, rt);
   1066 
   1067 	/*
   1068 	 * XXX: Is it correct to do this conversion _after_ the typeok above?
   1069 	 */
   1070 	if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
   1071 		tn = convert(INIT, 0, initstk->i_type, tn);
   1072 
   1073 	check_non_constant_initializer(tn, sclass);
   1074 
   1075 	debug_initstack();
   1076 	debug_leave();
   1077 }
   1078 
   1079 
   1080 /* Initialize a character array or wchar_t array with a string literal. */
   1081 static bool
   1082 init_array_using_string(tnode_t *tn)
   1083 {
   1084 	tspec_t	t;
   1085 	initstack_element *istk;
   1086 	int	len;
   1087 	strg_t	*strg;
   1088 
   1089 	if (tn->tn_op != STRING)
   1090 		return false;
   1091 
   1092 	debug_enter();
   1093 	debug_initstack();
   1094 
   1095 	istk = initstk;
   1096 	strg = tn->tn_string;
   1097 
   1098 	/*
   1099 	 * Check if we have an array type which can be initialized by
   1100 	 * the string.
   1101 	 */
   1102 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
   1103 		debug_step("subt array");
   1104 		t = istk->i_subt->t_subt->t_tspec;
   1105 		if (!((strg->st_tspec == CHAR &&
   1106 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
   1107 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
   1108 			debug_leave();
   1109 			return false;
   1110 		}
   1111 		/* XXX: duplicate code, see below */
   1112 		/* Put the array at top of stack */
   1113 		initstack_push();
   1114 		istk = initstk;
   1115 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
   1116 		debug_step("type array");
   1117 		t = istk->i_type->t_subt->t_tspec;
   1118 		if (!((strg->st_tspec == CHAR &&
   1119 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
   1120 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
   1121 			debug_leave();
   1122 			return false;
   1123 		}
   1124 		/* XXX: duplicate code, see above */
   1125 		/*
   1126 		 * If the array is already partly initialized, we are
   1127 		 * wrong here.
   1128 		 */
   1129 		if (istk->i_remaining != istk->i_type->t_dim)
   1130 			debug_leave();
   1131 			return false;
   1132 	} else {
   1133 		debug_leave();
   1134 		return false;
   1135 	}
   1136 
   1137 	/* Get length without trailing NUL character. */
   1138 	len = strg->st_len;
   1139 
   1140 	if (istk->i_array_of_unknown_size) {
   1141 		istk->i_array_of_unknown_size = false;
   1142 		istk->i_type->t_dim = len + 1;
   1143 		setcomplete(istk->i_type, true);
   1144 	} else {
   1145 		if (istk->i_type->t_dim < len) {
   1146 			/* non-null byte ignored in string initializer */
   1147 			warning(187);
   1148 		}
   1149 	}
   1150 
   1151 	/* In every case the array is initialized completely. */
   1152 	istk->i_remaining = 0;
   1153 
   1154 	debug_initstack();
   1155 	debug_leave();
   1156 	return true;
   1157 }
   1158