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