Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.173
      1 /*	$NetBSD: init.c,v 1.173 2021/03/28 19:53:58 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.173 2021/03/28 19:53:58 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 = 4, .x = 3 };
     63  *
     64  * Any scalar expression in the initializer may be surrounded by arbitrarily
     65  * many extra pairs of braces, like in the example 'number_with_braces' (C99
     66  * 6.7.8p11).
     67  *
     68  * For multi-dimensional arrays, the inner braces may be omitted like in
     69  * array_flat or spelled out like in array_nested.
     70  *
     71  * For the initializer, the grammar parser calls these functions:
     72  *
     73  *	begin_initialization
     74  *		init_lbrace			for each '{'
     75  *		add_designator_member		for each '.member' before '='
     76  *		add_designator_subscript	for each '[123]' before '='
     77  *		init_using_expr			for each expression
     78  *		init_rbrace			for each '}'
     79  *	end_initialization
     80  *
     81  * Each '{' begins a new brace level, each '}' ends the current brace level.
     82  * Each brace level has an associated "current object".
     83  *
     84  * Most of the time, the topmost level of brace_level contains a scalar type,
     85  * and its remaining count toggles between 1 and 0.
     86  *
     87  * See also:
     88  *	C99 6.7.8 "Initialization"
     89  *	d_c99_init.c for more examples
     90  */
     91 
     92 
     93 /*
     94  * Describes a single brace level of an ongoing initialization.
     95  *
     96  * XXX: Since C99, the initializers can be listed in arbitrary order by using
     97  * designators to specify the sub-object to be initialized.  The member names
     98  * of non-leaf structs may thus appear repeatedly, as demonstrated in
     99  * d_init_pop_member.c.
    100  *
    101  * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
    102  * selected examples.
    103  */
    104 struct brace_level {
    105 
    106 	/*
    107 	 * The type of the current object that is initialized at this brace
    108 	 * level.
    109 	 *
    110 	 * On the outermost element, this is always NULL since the outermost
    111 	 * initializer-expression may be enclosed in an optional pair of
    112 	 * braces, as of the current implementation.
    113 	 *
    114 	 * FIXME: This approach is wrong.  It's not that the outermost
    115 	 * initializer may be enclosed in additional braces, it's every scalar
    116 	 * that may be enclosed in additional braces, as of C99 6.7.8p11.
    117 	 *
    118 	 * Everywhere else it is nonnull.
    119 	 */
    120 	type_t	*bl_type;
    121 
    122 	/*
    123 	 * The type that will be initialized at the next initialization level,
    124 	 * usually enclosed by another pair of braces.
    125 	 *
    126 	 * For an array, it is the element type, but without 'const'.
    127 	 *
    128 	 * For a struct or union type, it is one of the member types, but
    129 	 * without 'const'.
    130 	 *
    131 	 * The outermost stack element has no bl_type but nevertheless has
    132 	 * bl_subtype.  For example, in 'int var = { 12345 }', initially there
    133 	 * is a brace_level with bl_subtype 'int'.  When the '{' is processed,
    134 	 * an element with bl_type 'int' is pushed to the stack.  When the
    135 	 * corresponding '}' is processed, the inner element is popped again.
    136 	 *
    137 	 * During initialization, only the top 2 elements of the stack are
    138 	 * looked at.
    139 	 *
    140 	 * XXX: Having bl_subtype here is the wrong approach, it should not be
    141 	 * necessary at all; see bl_type.
    142 	 */
    143 	type_t	*bl_subtype;
    144 
    145 	/*
    146 	 * Whether this level of the initializer requires a '}' to be
    147 	 * completed.
    148 	 *
    149 	 * Multidimensional arrays do not need a closing brace to complete
    150 	 * an inner array; for example, { 1, 2, 3, 4 } is a valid initializer
    151 	 * for 'int arr[2][2]'.
    152 	 *
    153 	 * XXX: Double-check whether this is the correct approach at all; see
    154 	 * bl_type.
    155 	 */
    156 	bool bl_brace: 1;
    157 
    158 	/* Whether bl_type is an array of unknown size. */
    159 	bool bl_array_of_unknown_size: 1;
    160 
    161 	/*
    162 	 * XXX: This feels wrong.  Whether or not there has been a named
    163 	 * initializer (called 'designation' since C99) should not matter at
    164 	 * all.  Even after an initializer with designation, counting of the
    165 	 * remaining elements continues, see C99 6.7.8p17.
    166 	 */
    167 	bool bl_seen_named_member: 1;
    168 
    169 	/*
    170 	 * For structs, the next member to be initialized by a designator-less
    171 	 * initializer.
    172 	 */
    173 	sym_t *bl_next_member;
    174 
    175 	/* TODO: Add bl_next_subscript for arrays. */
    176 
    177 	/* TODO: Understand C99 6.7.8p17 and footnote 128 for unions. */
    178 
    179 	/*
    180 	 * The number of remaining elements to be used by expressions without
    181 	 * designator.
    182 	 *
    183 	 * This says nothing about which members have been initialized or not
    184 	 * since starting with C99, members may be initialized in arbitrary
    185 	 * order by using designators.
    186 	 *
    187 	 * For an array of unknown size, this is always 0 and thus irrelevant.
    188 	 *
    189 	 * XXX: for scalars?
    190 	 * XXX: for structs?
    191 	 * XXX: for unions?
    192 	 * XXX: for arrays?
    193 	 *
    194 	 * XXX: Having the count of remaining objects should not be necessary.
    195 	 * It is probably clearer to use bl_next_member and bl_next_subscript
    196 	 * for this purpose.
    197 	 */
    198 	int bl_remaining;
    199 
    200 	/*
    201 	 * The initialization state of the enclosing data structure
    202 	 * (struct, union, array).
    203 	 *
    204 	 * XXX: Or for a scalar, for the top-level element, or for expressions
    205 	 * in redundant braces such as '{{{{ 0 }}}}' (not yet implemented as
    206 	 * of 2021-03-25).
    207 	 */
    208 	struct brace_level *bl_enclosing;
    209 };
    210 
    211 /*
    212  * A single component on the path to the sub-object that is initialized by an
    213  * initializer expression.  Either a struct or union member, or an array
    214  * subscript.
    215  *
    216  * See also: C99 6.7.8 "Initialization"
    217  */
    218 struct designator {
    219 	const char *name;		/* for struct and union */
    220 	/* TODO: add 'subscript' for arrays */
    221 	struct designator *next;
    222 };
    223 
    224 /*
    225  * The optional designation for an initializer, saying which sub-object to
    226  * initialize.  Examples for designations are '.member' or
    227  * '.member[123].member.member[1][1]'.
    228  *
    229  * See also: C99 6.7.8 "Initialization"
    230  */
    231 struct designation {
    232 	struct designator *head;
    233 	struct designator *tail;
    234 };
    235 
    236 struct initialization {
    237 	/*
    238 	 * is set as soon as a fatal error occurred in the initialization.
    239 	 * The effect is that the rest of the initialization is ignored
    240 	 * (parsed by yacc, expression trees built, but no initialization
    241 	 * takes place).
    242 	 */
    243 	bool	initerr;
    244 
    245 	/* The symbol that is to be initialized. */
    246 	sym_t	*initsym;
    247 
    248 	/* The innermost brace level. */
    249 	struct brace_level *brace_level;
    250 
    251 	/*
    252 	 * The C99 designator, if any, for the current initialization
    253 	 * expression.
    254 	 */
    255 	struct designation designation;
    256 
    257 	struct initialization *next;
    258 };
    259 
    260 
    261 static struct initialization *init;
    262 
    263 #ifdef DEBUG
    264 static int debug_ind = 0;
    265 #endif
    266 
    267 
    268 #ifdef DEBUG
    269 
    270 static void __printflike(1, 2)
    271 debug_printf(const char *fmt, ...)
    272 {
    273 	va_list va;
    274 
    275 	va_start(va, fmt);
    276 	vfprintf(stdout, fmt, va);
    277 	va_end(va);
    278 }
    279 
    280 static void
    281 debug_indent(void)
    282 {
    283 	debug_printf("%*s", 2 * debug_ind, "");
    284 }
    285 
    286 static void
    287 debug_enter(const char *func)
    288 {
    289 	printf("%*s+ %s\n", 2 * debug_ind++, "", func);
    290 }
    291 
    292 static void __printflike(1, 2)
    293 debug_step(const char *fmt, ...)
    294 {
    295 	va_list va;
    296 
    297 	debug_indent();
    298 	va_start(va, fmt);
    299 	vfprintf(stdout, fmt, va);
    300 	va_end(va);
    301 	printf("\n");
    302 }
    303 
    304 static void
    305 debug_leave(const char *func)
    306 {
    307 	printf("%*s- %s\n", 2 * --debug_ind, "", func);
    308 }
    309 
    310 #define debug_enter() (debug_enter)(__func__)
    311 #define debug_leave() (debug_leave)(__func__)
    312 
    313 #else
    314 
    315 #define debug_printf(fmt, ...)	do { } while (false)
    316 #define debug_indent()		do { } while (false)
    317 #define debug_enter()		do { } while (false)
    318 #define debug_step(fmt, ...)	do { } while (false)
    319 #define debug_leave()		do { } while (false)
    320 
    321 #endif
    322 
    323 
    324 static bool
    325 is_struct_or_union(tspec_t t)
    326 {
    327 	return t == STRUCT || t == UNION;
    328 }
    329 
    330 
    331 /* In traditional C, bit-fields can be initialized only by integer constants. */
    332 static void
    333 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
    334 {
    335 	if (tflag &&
    336 	    is_integer(lt) &&
    337 	    ln->tn_type->t_bitfield &&
    338 	    !is_integer(rt)) {
    339 		/* bit-field initialization is illegal in traditional C */
    340 		warning(186);
    341 	}
    342 }
    343 
    344 static void
    345 check_non_constant_initializer(const tnode_t *tn, scl_t sclass)
    346 {
    347 	/* TODO: rename CON to CONSTANT to avoid ambiguity with CONVERT */
    348 	if (tn == NULL || tn->tn_op == CON)
    349 		return;
    350 
    351 	const sym_t *sym;
    352 	ptrdiff_t offs;
    353 	if (constant_addr(tn, &sym, &offs))
    354 		return;
    355 
    356 	if (sclass == AUTO || sclass == REG) {
    357 		/* non-constant initializer */
    358 		c99ism(177);
    359 	} else {
    360 		/* non-constant initializer */
    361 		error(177);
    362 	}
    363 }
    364 
    365 static void
    366 check_init_expr(scl_t sclass, type_t *tp, sym_t *sym, tnode_t *tn)
    367 {
    368 	tnode_t *ln;
    369 	tspec_t lt, rt;
    370 	struct mbl *tmem;
    371 
    372 	/* Create a temporary node for the left side. */
    373 	ln = tgetblk(sizeof *ln);
    374 	ln->tn_op = NAME;
    375 	ln->tn_type = tduptyp(tp);
    376 	ln->tn_type->t_const = false;
    377 	ln->tn_lvalue = true;
    378 	ln->tn_sym = sym;
    379 
    380 	tn = cconv(tn);
    381 
    382 	lt = ln->tn_type->t_tspec;
    383 	rt = tn->tn_type->t_tspec;
    384 
    385 	debug_step("typeok '%s', '%s'",
    386 	    type_name(ln->tn_type), type_name(tn->tn_type));
    387 	if (!typeok(INIT, 0, ln, tn))
    388 		return;
    389 
    390 	/*
    391 	 * Preserve the tree memory. This is necessary because otherwise
    392 	 * expr() would free it.
    393 	 */
    394 	tmem = tsave();
    395 	expr(tn, true, false, true, false);
    396 	trestor(tmem);
    397 
    398 	check_bit_field_init(ln, lt, rt);
    399 
    400 	/*
    401 	 * XXX: Is it correct to do this conversion _after_ the typeok above?
    402 	 */
    403 	if (lt != rt || (tp->t_bitfield && tn->tn_op == CON))
    404 		tn = convert(INIT, 0, tp, tn);
    405 
    406 	check_non_constant_initializer(tn, sclass);
    407 }
    408 
    409 
    410 static struct designator *
    411 designator_new(const char *name)
    412 {
    413 	struct designator *d = xcalloc(1, sizeof *d);
    414 	d->name = name;
    415 	return d;
    416 }
    417 
    418 static void
    419 designator_free(struct designator *d)
    420 {
    421 	free(d);
    422 }
    423 
    424 
    425 #ifdef DEBUG
    426 static void
    427 designation_debug(const struct designation *dn)
    428 {
    429 	const struct designator *p;
    430 
    431 	if (dn->head == NULL)
    432 		return;
    433 
    434 	debug_indent();
    435 	debug_printf("designation: ");
    436 	for (p = dn->head; p != NULL; p = p->next)
    437 		debug_printf(".%s", p->name);
    438 	debug_printf("\n");
    439 }
    440 #else
    441 #define designation_debug(dn) do { } while (false)
    442 #endif
    443 
    444 static void
    445 designation_add(struct designation *dn, struct designator *dr)
    446 {
    447 
    448 	if (dn->head != NULL) {
    449 		dn->tail->next = dr;
    450 		dn->tail = dr;
    451 	} else {
    452 		dn->head = dr;
    453 		dn->tail = dr;
    454 	}
    455 
    456 	designation_debug(dn);
    457 }
    458 
    459 /* TODO: add support for array subscripts, not only named members */
    460 /*
    461  * TODO: This function should not be necessary at all.  There is no need to
    462  *  remove the head of the list.
    463  */
    464 static void
    465 designation_shift_level(struct designation *dn)
    466 {
    467 	lint_assert(dn->head != NULL);
    468 
    469 	if (dn->head == dn->tail) {
    470 		designator_free(dn->head);
    471 		dn->head = NULL;
    472 		dn->tail = NULL;
    473 	} else {
    474 		struct designator *head = dn->head;
    475 		dn->head = dn->head->next;
    476 		designator_free(head);
    477 	}
    478 
    479 	designation_debug(dn);
    480 }
    481 
    482 
    483 static struct brace_level *
    484 brace_level_new(type_t *type, type_t *subtype, int remaining,
    485 		struct brace_level *enclosing)
    486 {
    487 	struct brace_level *level = xcalloc(1, sizeof(*level));
    488 
    489 	level->bl_type = type;
    490 	level->bl_subtype = subtype;
    491 	level->bl_remaining = remaining;
    492 	level->bl_enclosing = enclosing;
    493 
    494 	return level;
    495 }
    496 
    497 static void
    498 brace_level_free(struct brace_level *level)
    499 {
    500 	free(level);
    501 }
    502 
    503 #ifdef DEBUG
    504 /*
    505  * TODO: only log the top of the stack after each modifying operation
    506  *
    507  * TODO: wrap all write accesses to brace_level in setter functions
    508  */
    509 static void
    510 brace_level_debug(const struct brace_level *level)
    511 {
    512 	if (level->bl_type != NULL)
    513 		debug_printf("type '%s'", type_name(level->bl_type));
    514 	if (level->bl_type != NULL && level->bl_subtype != NULL)
    515 		debug_printf(", ");
    516 	if (level->bl_subtype != NULL)
    517 		debug_printf("subtype '%s'", type_name(level->bl_subtype));
    518 
    519 	if (level->bl_brace)
    520 		debug_printf(", needs closing brace");
    521 	if (level->bl_array_of_unknown_size)
    522 		debug_printf(", array of unknown size");
    523 	if (level->bl_seen_named_member)
    524 		debug_printf(", seen named member");
    525 
    526 	const type_t *eff_type = level->bl_type != NULL
    527 	    ? level->bl_type : level->bl_subtype;
    528 	if (eff_type->t_tspec == STRUCT && level->bl_next_member != NULL)
    529 		debug_printf(", next member '%s'",
    530 		    level->bl_next_member->s_name);
    531 
    532 	debug_printf(", remaining %d\n", level->bl_remaining);
    533 }
    534 #else
    535 #define brace_level_debug(level) do { } while (false)
    536 #endif
    537 
    538 static type_t *
    539 brace_level_subtype(struct brace_level *level)
    540 {
    541 
    542 	if (level->bl_subtype != NULL)
    543 		return level->bl_subtype;
    544 
    545 	return level->bl_type;
    546 }
    547 
    548 static void
    549 brace_level_set_array_dimension(struct brace_level *level, int dim)
    550 {
    551 	debug_step("setting the array size to %d", dim);
    552 	level->bl_type->t_dim = dim;
    553 	debug_indent();
    554 	brace_level_debug(level);
    555 }
    556 
    557 static void
    558 brace_level_next_member(struct brace_level *level)
    559 {
    560 	const sym_t *m;
    561 
    562 	do {
    563 		m = level->bl_next_member = level->bl_next_member->s_next;
    564 		/* XXX: can this assertion be made to fail? */
    565 		lint_assert(m != NULL);
    566 	} while (m->s_bitfield && m->s_name == unnamed);
    567 
    568 	debug_indent();
    569 	brace_level_debug(level);
    570 }
    571 
    572 static const sym_t *
    573 brace_level_look_up_member(const struct brace_level *level, const char *name)
    574 {
    575 	const type_t *tp = level->bl_type;
    576 	const sym_t *m;
    577 
    578 	lint_assert(is_struct_or_union(tp->t_tspec));
    579 
    580 	for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next) {
    581 		if (m->s_bitfield && m->s_name == unnamed)
    582 			continue;
    583 		if (strcmp(m->s_name, name) == 0)
    584 			return m;
    585 	}
    586 
    587 	return NULL;
    588 }
    589 
    590 /* TODO: merge duplicate code */
    591 static sym_t *
    592 brace_level_look_up_first_member_named(struct brace_level *level,
    593 				       const char *name, int *count)
    594 {
    595 	sym_t *m;
    596 
    597 	for (m = level->bl_type->t_str->sou_first_member;
    598 	     m != NULL; m = m->s_next) {
    599 		if (m->s_bitfield && m->s_name == unnamed)
    600 			continue;
    601 		if (strcmp(m->s_name, name) != 0)
    602 			continue;
    603 		(*count)++;
    604 		break;
    605 	}
    606 
    607 	return m;
    608 }
    609 
    610 /* TODO: merge duplicate code */
    611 static sym_t *
    612 brace_level_look_up_first_member_unnamed(struct brace_level *level, int *count)
    613 {
    614 	sym_t *m;
    615 
    616 	for (m = level->bl_type->t_str->sou_first_member;
    617 	     m != NULL; m = m->s_next) {
    618 		if (m->s_bitfield && m->s_name == unnamed)
    619 			continue;
    620 		/* XXX: What is this code for? */
    621 		if (++(*count) == 1) {
    622 			level->bl_next_member = m;
    623 			level->bl_subtype = m->s_type;
    624 		}
    625 	}
    626 
    627 	return m;
    628 }
    629 
    630 /* TODO: document me */
    631 /* TODO: think of a better name than 'push' */
    632 static bool
    633 brace_level_push_array(struct brace_level *level)
    634 {
    635 	if (level->bl_enclosing->bl_seen_named_member) {
    636 		level->bl_brace = true;
    637 		debug_step("ARRAY, seen named member, needs closing brace");
    638 	}
    639 
    640 	if (is_incomplete(level->bl_type) &&
    641 	    level->bl_enclosing->bl_enclosing != NULL) {
    642 		/* initialization of an incomplete type */
    643 		error(175);
    644 		return false;
    645 	}
    646 
    647 	level->bl_subtype = level->bl_type->t_subt;
    648 	level->bl_array_of_unknown_size = is_incomplete(level->bl_type);
    649 	level->bl_remaining = level->bl_type->t_dim;
    650 	debug_step("type '%s' remaining %d",
    651 	    type_name(level->bl_type), level->bl_remaining);
    652 	return true;
    653 }
    654 
    655 /*
    656  * If the removed element was a structure member, we must go
    657  * to the next structure member.
    658  *
    659  * XXX: Nothing should ever be "removed" at this point.
    660  *
    661  * TODO: think of a better name than 'pop'
    662  */
    663 static void
    664 brace_level_pop_item_unnamed(struct brace_level *level)
    665 {
    666 	if (level->bl_remaining > 0 && level->bl_type->t_tspec == STRUCT &&
    667 	    !level->bl_seen_named_member) {
    668 		brace_level_next_member(level);
    669 		level->bl_subtype = level->bl_next_member->s_type;
    670 	}
    671 }
    672 
    673 static bool
    674 brace_level_check_too_many_initializers(struct brace_level *level)
    675 {
    676 	if (level->bl_remaining > 0)
    677 		return true;
    678 	/*
    679 	 * FIXME: even with named members, there can be too many initializers
    680 	 */
    681 	if (level->bl_array_of_unknown_size || level->bl_seen_named_member)
    682 		return true;
    683 
    684 	tspec_t t = level->bl_type->t_tspec;
    685 	if (t == ARRAY) {
    686 		/* too many array initializers, expected %d */
    687 		error(173, level->bl_type->t_dim);
    688 	} else if (is_struct_or_union(t)) {
    689 		/* too many struct/union initializers */
    690 		error(172);
    691 	} else {
    692 		/* too many initializers */
    693 		error(174);
    694 	}
    695 	return false;
    696 }
    697 
    698 /* Extend an array of unknown size by one element */
    699 static void
    700 brace_level_extend_if_array_of_unknown_size(struct brace_level *level)
    701 {
    702 
    703 	if (level->bl_remaining != 0)
    704 		return;
    705 	/*
    706 	 * XXX: According to the function name, there should be a 'return' if
    707 	 * bl_array_of_unknown_size is false.  There's probably a test missing
    708 	 * for that case.
    709 	 */
    710 
    711 	/*
    712 	 * The only place where an incomplete array may appear is at the
    713 	 * outermost aggregate level of the object to be initialized.
    714 	 */
    715 	lint_assert(level->bl_enclosing->bl_enclosing == NULL);
    716 	lint_assert(level->bl_type->t_tspec == ARRAY);
    717 
    718 	debug_step("extending array of unknown size '%s'",
    719 	    type_name(level->bl_type));
    720 	level->bl_remaining = 1;
    721 	level->bl_type->t_dim++;
    722 	setcomplete(level->bl_type, true);
    723 
    724 	debug_step("extended type is '%s'", type_name(level->bl_type));
    725 }
    726 
    727 
    728 static struct initialization *
    729 initialization_new(sym_t *sym)
    730 {
    731 	struct initialization *in = xcalloc(1, sizeof(*in));
    732 
    733 	in->initsym = sym;
    734 
    735 	return in;
    736 }
    737 
    738 static void
    739 initialization_free(struct initialization *in)
    740 {
    741 	struct brace_level *level, *next;
    742 
    743 	for (level = in->brace_level; level != NULL; level = next) {
    744 		next = level->bl_enclosing;
    745 		brace_level_free(level);
    746 	}
    747 
    748 	free(in);
    749 }
    750 
    751 #ifdef DEBUG
    752 /*
    753  * TODO: only call debug_initstack after each push/pop.
    754  */
    755 static void
    756 initialization_debug(const struct initialization *in)
    757 {
    758 	if (in->brace_level == NULL) {
    759 		debug_step("no brace level in the current initialization");
    760 		return;
    761 	}
    762 
    763 	size_t i = 0;
    764 	for (const struct brace_level *level = in->brace_level;
    765 	     level != NULL; level = level->bl_enclosing) {
    766 		debug_indent();
    767 		debug_printf("brace level %zu: ", i);
    768 		brace_level_debug(level);
    769 		i++;
    770 	}
    771 }
    772 #else
    773 #define initialization_debug(in) do { } while (false)
    774 #endif
    775 
    776 /*
    777  * Initialize the initialization stack by putting an entry for the object
    778  * which is to be initialized on it.
    779  *
    780  * TODO: merge into initialization_new if possible
    781  */
    782 static void
    783 initialization_init(struct initialization *in)
    784 {
    785 	if (in->initerr)
    786 		return;
    787 
    788 	debug_enter();
    789 
    790 	/*
    791 	 * If the type which is to be initialized is an incomplete array,
    792 	 * it must be duplicated.
    793 	 */
    794 	if (in->initsym->s_type->t_tspec == ARRAY && is_incomplete(in->initsym->s_type))
    795 		in->initsym->s_type = duptyp(in->initsym->s_type);
    796 	/* TODO: does 'duptyp' create a memory leak? */
    797 
    798 	in->brace_level = brace_level_new(NULL, in->initsym->s_type, 1, NULL);
    799 
    800 	initialization_debug(in);
    801 	debug_leave();
    802 }
    803 
    804 static void
    805 initialization_set_error(struct initialization *in)
    806 {
    807 	in->initerr = true;
    808 }
    809 
    810 /* TODO: document me */
    811 /* TODO: think of a better name than 'push' */
    812 static bool
    813 initialization_push_struct_or_union(struct initialization *in)
    814 {
    815 	struct brace_level *level = in->brace_level;
    816 	int cnt;
    817 	sym_t *m;
    818 
    819 	if (is_incomplete(level->bl_type)) {
    820 		/* initialization of an incomplete type */
    821 		error(175);
    822 		initialization_set_error(in);
    823 		return false;
    824 	}
    825 
    826 	cnt = 0;
    827 	designation_debug(&in->designation);
    828 	debug_step("lookup for '%s'%s",
    829 	    type_name(level->bl_type),
    830 	    level->bl_seen_named_member ? ", seen named member" : "");
    831 
    832 	if (in->designation.head != NULL)
    833 		m = brace_level_look_up_first_member_named(level,
    834 		    in->designation.head->name, &cnt);
    835 	else
    836 		m = brace_level_look_up_first_member_unnamed(level, &cnt);
    837 
    838 	if (in->designation.head != NULL) {
    839 		if (m == NULL) {
    840 			debug_step("pop struct");
    841 			return true;
    842 		}
    843 		level->bl_next_member = m;
    844 		level->bl_subtype = m->s_type;
    845 		level->bl_seen_named_member = true;
    846 		debug_step("named member '%s'",
    847 		    in->designation.head->name);
    848 		designation_shift_level(&in->designation);
    849 		cnt = level->bl_type->t_tspec == STRUCT ? 2 : 1;
    850 	}
    851 	level->bl_brace = true;
    852 	debug_step("unnamed element with type '%s'%s",
    853 	    type_name(
    854 		level->bl_type != NULL ? level->bl_type : level->bl_subtype),
    855 	    level->bl_brace ? ", needs closing brace" : "");
    856 	if (cnt == 0) {
    857 		/* cannot init. struct/union with no named member */
    858 		error(179);
    859 		initialization_set_error(in);
    860 		return false;
    861 	}
    862 	level->bl_remaining = level->bl_type->t_tspec == STRUCT ? cnt : 1;
    863 	return false;
    864 }
    865 
    866 static void
    867 initialization_end_brace_level(struct initialization *in)
    868 {
    869 	struct brace_level *level = in->brace_level;
    870 	in->brace_level = level->bl_enclosing;
    871 	brace_level_free(level);
    872 }
    873 
    874 /* TODO: document me */
    875 /* TODO: think of a better name than 'push' */
    876 static void
    877 initialization_push(struct initialization *in)
    878 {
    879 	struct brace_level *level;
    880 
    881 	debug_enter();
    882 
    883 	brace_level_extend_if_array_of_unknown_size(in->brace_level);
    884 
    885 	level = in->brace_level;
    886 	lint_assert(level->bl_remaining > 0);
    887 
    888 	in->brace_level = brace_level_new(brace_level_subtype(level), NULL, 0,
    889 	    level);
    890 	lint_assert(in->brace_level->bl_type != NULL);
    891 	lint_assert(in->brace_level->bl_type->t_tspec != FUNC);
    892 
    893 again:
    894 	level = in->brace_level;
    895 
    896 	debug_step("expecting type '%s'", type_name(level->bl_type));
    897 	lint_assert(level->bl_type != NULL);
    898 	switch (level->bl_type->t_tspec) {
    899 	case ARRAY:
    900 		if (in->designation.head != NULL) {
    901 			debug_step("pop array, named member '%s'%s",
    902 			    in->designation.head->name,
    903 			    level->bl_brace ? ", needs closing brace" : "");
    904 			goto pop;
    905 		}
    906 
    907 		if (!brace_level_push_array(level))
    908 			initialization_set_error(in);
    909 		break;
    910 
    911 	case UNION:
    912 		if (tflag)
    913 			/* initialization of union is illegal in trad. C */
    914 			warning(238);
    915 		/* FALLTHROUGH */
    916 	case STRUCT:
    917 		if (initialization_push_struct_or_union(in))
    918 			goto pop;
    919 		break;
    920 	default:
    921 		if (in->designation.head != NULL) {
    922 			debug_step("pop scalar");
    923 		pop:
    924 			initialization_end_brace_level(in);
    925 			goto again;
    926 		}
    927 		/* The initialization stack now expects a single scalar. */
    928 		level->bl_remaining = 1;
    929 		break;
    930 	}
    931 
    932 	initialization_debug(in);
    933 	debug_leave();
    934 }
    935 
    936 /* TODO: document me */
    937 static void
    938 initialization_pop_item_named(struct initialization *in, const char *name)
    939 {
    940 	struct brace_level *level = in->brace_level;
    941 	const sym_t *m;
    942 
    943 	/*
    944 	 * TODO: fix wording of the debug message; this doesn't seem to be
    945 	 * related to initializing the named member.
    946 	 */
    947 	debug_step("initializing named member '%s'", name);
    948 
    949 	if (!is_struct_or_union(level->bl_type->t_tspec)) {
    950 		/* syntax error '%s' */
    951 		error(249, "named member must only be used with struct/union");
    952 		initialization_set_error(in);
    953 		return;
    954 	}
    955 
    956 	m = brace_level_look_up_member(level, name);
    957 	if (m == NULL) {
    958 		/* TODO: add type information to the message */
    959 		/* undefined struct/union member: %s */
    960 		error(101, name);
    961 
    962 		designation_shift_level(&in->designation);
    963 		level->bl_seen_named_member = true;
    964 		return;
    965 	}
    966 
    967 	debug_step("found matching member");
    968 	level->bl_subtype = m->s_type;
    969 	/* XXX: why ++? */
    970 	level->bl_remaining++;
    971 	/* XXX: why is bl_seen_named_member not set? */
    972 	designation_shift_level(&in->designation);
    973 }
    974 
    975 /* TODO: think of a better name than 'pop' */
    976 static void
    977 initialization_pop_item(struct initialization *in)
    978 {
    979 	struct brace_level *level;
    980 
    981 	debug_enter();
    982 
    983 	level = in->brace_level;
    984 	debug_indent();
    985 	debug_printf("popping: ");
    986 	brace_level_debug(level);
    987 
    988 	in->brace_level = level->bl_enclosing;
    989 	brace_level_free(level);
    990 	level = in->brace_level;
    991 	lint_assert(level != NULL);
    992 
    993 	level->bl_remaining--;
    994 	lint_assert(level->bl_remaining >= 0);
    995 	debug_step("%d elements remaining", level->bl_remaining);
    996 
    997 	if (in->designation.head != NULL && in->designation.head->name != NULL)
    998 		initialization_pop_item_named(in, in->designation.head->name);
    999 	else
   1000 		brace_level_pop_item_unnamed(level);
   1001 
   1002 	initialization_debug(in);
   1003 	debug_leave();
   1004 }
   1005 
   1006 /*
   1007  * Take all entries which cannot be used for further initializers from the
   1008  * stack, but do this only if they do not require a closing brace.
   1009  */
   1010 /* TODO: think of a better name than 'pop' */
   1011 static void
   1012 initialization_pop_nobrace(struct initialization *in)
   1013 {
   1014 
   1015 	debug_enter();
   1016 	while (!in->brace_level->bl_brace &&
   1017 	       in->brace_level->bl_remaining == 0 &&
   1018 	       !in->brace_level->bl_array_of_unknown_size)
   1019 		initialization_pop_item(in);
   1020 	debug_leave();
   1021 }
   1022 
   1023 /*
   1024  * Process a '{' in an initializer by starting the initialization of the
   1025  * nested data structure, with bl_type being the bl_subtype of the outer
   1026  * initialization level.
   1027  */
   1028 static void
   1029 initialization_next_brace(struct initialization *in)
   1030 {
   1031 
   1032 	debug_enter();
   1033 	initialization_debug(in);
   1034 
   1035 	if (!in->initerr &&
   1036 	    !brace_level_check_too_many_initializers(in->brace_level))
   1037 		initialization_set_error(in);
   1038 
   1039 	if (!in->initerr)
   1040 		initialization_push(in);
   1041 
   1042 	if (!in->initerr) {
   1043 		in->brace_level->bl_brace = true;
   1044 		designation_debug(&in->designation);
   1045 		if (in->brace_level->bl_type != NULL)
   1046 			debug_step("expecting type '%s'",
   1047 			    type_name(in->brace_level->bl_type));
   1048 	}
   1049 
   1050 	initialization_debug(in);
   1051 	debug_leave();
   1052 }
   1053 
   1054 static void
   1055 check_no_auto_aggregate(scl_t sclass, const struct brace_level *level)
   1056 {
   1057 	if (!tflag)
   1058 		return;
   1059 	if (!(sclass == AUTO || sclass == REG))
   1060 		return;
   1061 	if (!(level->bl_enclosing == NULL))
   1062 		return;
   1063 	if (is_scalar(level->bl_subtype->t_tspec))
   1064 		return;
   1065 
   1066 	/* no automatic aggregate initialization in trad. C */
   1067 	warning(188);
   1068 }
   1069 
   1070 static void
   1071 initialization_lbrace(struct initialization *in)
   1072 {
   1073 	if (in->initerr)
   1074 		return;
   1075 
   1076 	debug_enter();
   1077 	initialization_debug(in);
   1078 
   1079 	check_no_auto_aggregate(in->initsym->s_scl, in->brace_level);
   1080 
   1081 	/*
   1082 	 * Remove all entries which cannot be used for further initializers
   1083 	 * and do not expect a closing brace.
   1084 	 */
   1085 	initialization_pop_nobrace(in);
   1086 
   1087 	initialization_next_brace(in);
   1088 
   1089 	initialization_debug(in);
   1090 	debug_leave();
   1091 }
   1092 
   1093 /*
   1094  * Process a '}' in an initializer by finishing the current level of the
   1095  * initialization stack.
   1096  *
   1097  * Take all entries, including the first which requires a closing brace,
   1098  * from the stack.
   1099  */
   1100 static void
   1101 initialization_rbrace(struct initialization *in)
   1102 {
   1103 	bool brace;
   1104 
   1105 	if (in->initerr)
   1106 		return;
   1107 
   1108 	debug_enter();
   1109 	do {
   1110 		brace = in->brace_level->bl_brace;
   1111 		/* TODO: improve wording of the debug message */
   1112 		debug_step("loop brace=%d", brace);
   1113 		initialization_pop_item(in);
   1114 	} while (!brace);
   1115 	initialization_debug(in);
   1116 	debug_leave();
   1117 }
   1118 
   1119 /*
   1120  * A sub-object of an array is initialized using a designator.  This does not
   1121  * have to be an array element directly, it can also be used to initialize
   1122  * only a sub-object of the array element.
   1123  *
   1124  * C99 example: struct { int member[4]; } var = { [2] = 12345 };
   1125  *
   1126  * GNU example: struct { int member[4]; } var = { [1 ... 3] = 12345 };
   1127  *
   1128  * TODO: test the following initialization with an outer and an inner type:
   1129  *
   1130  * .deeply[0].nested = {
   1131  *	.deeply[1].nested = {
   1132  *		12345,
   1133  *	},
   1134  * }
   1135  */
   1136 static void
   1137 initialization_add_designator_subscript(struct initialization *in,
   1138 					range_t range)
   1139 {
   1140 	struct brace_level *level;
   1141 
   1142 	debug_enter();
   1143 	if (range.lo == range.hi)
   1144 		debug_step("subscript is %zu", range.hi);
   1145 	else
   1146 		debug_step("subscript range is %zu ... %zu",
   1147 		    range.lo, range.hi);
   1148 
   1149 	/* XXX: This call is wrong here, it must be somewhere else. */
   1150 	initialization_pop_nobrace(in);
   1151 
   1152 	level = in->brace_level;
   1153 	if (level->bl_array_of_unknown_size) {
   1154 		/* No +1 here, extend_if_array_of_unknown_size will add it. */
   1155 		int auto_dim = (int)range.hi;
   1156 		if (auto_dim > level->bl_type->t_dim)
   1157 			brace_level_set_array_dimension(level, auto_dim);
   1158 	}
   1159 
   1160 	debug_leave();
   1161 }
   1162 
   1163 static bool
   1164 is_string_array(const type_t *tp, tspec_t t)
   1165 {
   1166 	tspec_t st;
   1167 
   1168 	if (tp == NULL || tp->t_tspec != ARRAY)
   1169 		return false;
   1170 	st = tp->t_subt->t_tspec;
   1171 	return t == CHAR
   1172 	    ? st == CHAR || st == UCHAR || st == SCHAR
   1173 	    : st == WCHAR;
   1174 }
   1175 
   1176 /* Initialize a character array or wchar_t array with a string literal. */
   1177 static bool
   1178 initialization_init_array_using_string(struct initialization *in, tnode_t *tn)
   1179 {
   1180 	struct brace_level *level;
   1181 	strg_t	*strg;
   1182 
   1183 	if (tn->tn_op != STRING)
   1184 		return false;
   1185 
   1186 	debug_enter();
   1187 
   1188 	level = in->brace_level;
   1189 	strg = tn->tn_string;
   1190 
   1191 	/*
   1192 	 * Check if we have an array type which can be initialized by
   1193 	 * the string.
   1194 	 */
   1195 	if (is_string_array(level->bl_subtype, strg->st_tspec)) {
   1196 		debug_step("subtype is an array");
   1197 
   1198 		/* Put the array at top of stack */
   1199 		initialization_push(in);
   1200 		level = in->brace_level;
   1201 
   1202 	} else if (is_string_array(level->bl_type, strg->st_tspec)) {
   1203 		debug_step("type is an array");
   1204 
   1205 		/*
   1206 		 * If the array is already partly initialized, we are
   1207 		 * wrong here.
   1208 		 */
   1209 		if (level->bl_remaining != level->bl_type->t_dim)
   1210 			goto nope;
   1211 	} else
   1212 		goto nope;
   1213 
   1214 	if (level->bl_array_of_unknown_size) {
   1215 		level->bl_array_of_unknown_size = false;
   1216 		level->bl_type->t_dim = (int)(strg->st_len + 1);
   1217 		setcomplete(level->bl_type, true);
   1218 	} else {
   1219 		/*
   1220 		 * TODO: check for buffer overflow in the object to be
   1221 		 * initialized
   1222 		 */
   1223 		/* XXX: double-check for off-by-one error */
   1224 		if (level->bl_type->t_dim < (int)strg->st_len) {
   1225 			/* non-null byte ignored in string initializer */
   1226 			warning(187);
   1227 		}
   1228 
   1229 		/*
   1230 		 * TODO: C99 6.7.8p14 allows a string literal to be enclosed
   1231 		 * in optional redundant braces, just like scalars.  Add tests
   1232 		 * for this.
   1233 		 */
   1234 	}
   1235 
   1236 	/* In every case the array is initialized completely. */
   1237 	level->bl_remaining = 0;
   1238 
   1239 	initialization_debug(in);
   1240 	debug_leave();
   1241 	return true;
   1242 nope:
   1243 	debug_leave();
   1244 	return false;
   1245 }
   1246 
   1247 /*
   1248  * Initialize a non-array object with automatic storage duration and only a
   1249  * single initializer expression without braces by delegating to ASSIGN.
   1250  */
   1251 static bool
   1252 initialization_init_using_assign(struct initialization *in, tnode_t *rn)
   1253 {
   1254 	tnode_t *ln, *tn;
   1255 
   1256 	if (in->initsym->s_type->t_tspec == ARRAY)
   1257 		return false;
   1258 	if (in->brace_level->bl_enclosing != NULL)
   1259 		return false;
   1260 
   1261 	debug_step("handing over to ASSIGN");
   1262 
   1263 	ln = new_name_node(in->initsym, 0);
   1264 	ln->tn_type = tduptyp(ln->tn_type);
   1265 	ln->tn_type->t_const = false;
   1266 
   1267 	tn = build(ASSIGN, ln, rn);
   1268 	expr(tn, false, false, false, false);
   1269 
   1270 	/* XXX: why not clean up the initstack here already? */
   1271 	return true;
   1272 }
   1273 
   1274 /* TODO: document me, or think of a better name */
   1275 static void
   1276 initialization_next_nobrace(struct initialization *in, tnode_t *tn)
   1277 {
   1278 	debug_enter();
   1279 
   1280 	if (in->brace_level->bl_type == NULL &&
   1281 	    !is_scalar(in->brace_level->bl_subtype->t_tspec)) {
   1282 		/* {}-enclosed initializer required */
   1283 		error(181);
   1284 		/* XXX: maybe set initerr here */
   1285 	}
   1286 
   1287 	if (!in->initerr &&
   1288 	    !brace_level_check_too_many_initializers(in->brace_level))
   1289 		initialization_set_error(in);
   1290 
   1291 	while (!in->initerr) {
   1292 		struct brace_level *level = in->brace_level;
   1293 
   1294 		if (tn->tn_type->t_tspec == STRUCT &&
   1295 		    level->bl_type == tn->tn_type &&
   1296 		    level->bl_enclosing != NULL &&
   1297 		    level->bl_enclosing->bl_enclosing != NULL) {
   1298 			level->bl_brace = false;
   1299 			level->bl_remaining = 1; /* the struct itself */
   1300 			break;
   1301 		}
   1302 
   1303 		if (level->bl_type != NULL &&
   1304 		    is_scalar(level->bl_type->t_tspec))
   1305 			break;
   1306 		initialization_push(in);
   1307 	}
   1308 
   1309 	initialization_debug(in);
   1310 	debug_leave();
   1311 }
   1312 
   1313 static void
   1314 initialization_expr(struct initialization *in, tnode_t *tn)
   1315 {
   1316 	scl_t	sclass;
   1317 
   1318 	debug_enter();
   1319 	initialization_debug(in);
   1320 	designation_debug(&in->designation);
   1321 	debug_step("expr:");
   1322 	debug_node(tn, debug_ind + 1);
   1323 
   1324 	if (in->initerr || tn == NULL)
   1325 		goto done;
   1326 
   1327 	sclass = in->initsym->s_scl;
   1328 	if ((sclass == AUTO || sclass == REG) &&
   1329 	    initialization_init_using_assign(in, tn))
   1330 		goto done;
   1331 
   1332 	initialization_pop_nobrace(in);
   1333 
   1334 	if (initialization_init_array_using_string(in, tn)) {
   1335 		debug_step("after initializing the string:");
   1336 		goto done_debug;
   1337 	}
   1338 
   1339 	initialization_next_nobrace(in, tn);
   1340 	if (in->initerr || tn == NULL)
   1341 		goto done_debug;
   1342 
   1343 	/* Using initsym here is better than nothing. */
   1344 	check_init_expr(sclass, in->brace_level->bl_type, in->initsym, tn);
   1345 
   1346 	in->brace_level->bl_remaining--;
   1347 	debug_step("%d elements remaining", in->brace_level->bl_remaining);
   1348 
   1349 done_debug:
   1350 	initialization_debug(in);
   1351 
   1352 done:
   1353 	while (in->designation.head != NULL)
   1354 		designation_shift_level(&in->designation);
   1355 
   1356 	debug_leave();
   1357 }
   1358 
   1359 static struct initialization *
   1360 current_init(void)
   1361 {
   1362 	lint_assert(init != NULL);
   1363 	return init;
   1364 }
   1365 
   1366 bool *
   1367 current_initerr(void)
   1368 {
   1369 	return &current_init()->initerr;
   1370 }
   1371 
   1372 sym_t **
   1373 current_initsym(void)
   1374 {
   1375 	return &current_init()->initsym;
   1376 }
   1377 
   1378 void
   1379 begin_initialization(sym_t *sym)
   1380 {
   1381 	struct initialization *in;
   1382 
   1383 	debug_step("begin initialization of '%s'", type_name(sym->s_type));
   1384 	in = initialization_new(sym);
   1385 	in->next = init;
   1386 	init = in;
   1387 }
   1388 
   1389 void
   1390 end_initialization(void)
   1391 {
   1392 	struct initialization *in;
   1393 
   1394 	in = init;
   1395 	init = init->next;
   1396 	initialization_free(in);
   1397 	debug_step("end initialization");
   1398 }
   1399 
   1400 void
   1401 add_designator_member(sbuf_t *sb)
   1402 {
   1403 	designation_add(&current_init()->designation,
   1404 	    designator_new(sb->sb_name));
   1405 }
   1406 
   1407 void
   1408 add_designator_subscript(range_t range)
   1409 {
   1410 	initialization_add_designator_subscript(current_init(), range);
   1411 }
   1412 
   1413 void
   1414 initstack_init(void)
   1415 {
   1416 	initialization_init(current_init());
   1417 }
   1418 
   1419 void
   1420 init_lbrace(void)
   1421 {
   1422 	initialization_lbrace(current_init());
   1423 }
   1424 
   1425 void
   1426 init_using_expr(tnode_t *tn)
   1427 {
   1428 	initialization_expr(current_init(), tn);
   1429 }
   1430 
   1431 void
   1432 init_rbrace(void)
   1433 {
   1434 	initialization_rbrace(current_init());
   1435 }
   1436