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