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