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