Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.183
      1 /*	$NetBSD: init.c,v 1.183 2021/03/30 16:07:07 rillig Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994, 1995 Jochen Pohl
      5  * Copyright (c) 2021 Roland Illig
      6  * All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Jochen Pohl for
     19  *	The NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(lint)
     41 __RCSID("$NetBSD: init.c,v 1.183 2021/03/30 16:07:07 rillig Exp $");
     42 #endif
     43 
     44 #include <stdlib.h>
     45 #include <string.h>
     46 
     47 #include "lint1.h"
     48 
     49 
     50 /*
     51  * Initialization of global or local objects, like in:
     52  *
     53  *	int number = 12345;
     54  *	int number_with_braces = { 12345 };
     55  *
     56  *	int array_of_unknown_size[] = { 111, 222, 333 };
     57  *	int array_flat[2][2] = { 11, 12, 21, 22 };
     58  *	int array_nested[2][2] = { { 11, 12 }, { 21, 22 } };
     59  *
     60  *	struct { int x, y; } point = { 3, 4 };
     61  *	struct { int x, y; } point = { .y = 4, .x = 3 };
     62  *
     63  * Any scalar expression in the initializer may be surrounded by arbitrarily
     64  * many extra pairs of braces, like in the example 'number_with_braces' (C99
     65  * 6.7.8p11).
     66  *
     67  * For multi-dimensional arrays, the inner braces may be omitted like in
     68  * array_flat or spelled out like in array_nested.  This is unusual in
     69  * practice and therefore only supported very basically.
     70  *
     71  * During initialization, 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_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  * See also:
     85  *	C99 6.7.8 "Initialization"
     86  *	d_c99_init.c for more examples
     87  */
     88 
     89 /*
     90  * A single component on the path to the sub-object that is initialized by an
     91  * initializer expression.  Either a struct or union member, or an array
     92  * subscript.
     93  *
     94  * C99 6.7.8p6, 6.7.8p7
     95  */
     96 struct designator {
     97 	const char	*dr_name;	/* for struct and union */
     98 	size_t		dr_subscript;	/* for array */
     99 	struct designator *dr_next;
    100 };
    101 
    102 /*
    103  * The optional designation for an initializer, saying which sub-object to
    104  * initialize.  Examples for designations are '.member' or
    105  * '.member[123].member.member[1][1]'.
    106  *
    107  * C99 6.7.8p6, 6.7.8p7
    108  */
    109 struct designation {
    110 	struct designator *dn_head;
    111 	struct designator *dn_tail;
    112 };
    113 
    114 /*
    115  * Describes a single brace level of an ongoing initialization.
    116  *
    117  * See C99 6.7.8p17.
    118  */
    119 struct brace_level {
    120 	/*
    121 	 * The type of the current object that is initialized at this brace
    122 	 * level.
    123 	 */
    124 	const type_t	*bl_type;
    125 	const sym_t	*bl_next_member;	/* for structs and unions */
    126 	size_t		bl_array_next_subscript;
    127 	bool		bl_array_of_unknown_size: 1;
    128 	bool		bl_scalar_done: 1;	/* for scalars */
    129 	bool		bl_omitted_braces: 1;	/* skip further checks */
    130 	struct designation bl_designation;	/* .member[123].member */
    131 	struct brace_level *bl_enclosing;
    132 };
    133 
    134 struct initialization {
    135 	/*
    136 	 * Is set as soon as a fatal error occurred in the initialization.
    137 	 * The effect is that the rest of the initialization is ignored
    138 	 * (parsed by yacc, expression trees built, but no initialization
    139 	 * takes place).
    140 	 */
    141 	bool		in_err;
    142 
    143 	/* The symbol that is to be initialized. */
    144 	sym_t		*in_sym;
    145 
    146 	/* The innermost brace level. */
    147 	struct brace_level *in_brace_level;
    148 
    149 	struct initialization *in_enclosing;
    150 };
    151 
    152 
    153 #ifdef DEBUG
    154 static int debug_indentation = 0;
    155 #endif
    156 
    157 
    158 #ifdef DEBUG
    159 
    160 static void __printflike(1, 2)
    161 debug_printf(const char *fmt, ...)
    162 {
    163 	va_list va;
    164 
    165 	va_start(va, fmt);
    166 	vfprintf(stdout, fmt, va);
    167 	va_end(va);
    168 }
    169 
    170 static void
    171 debug_indent(void)
    172 {
    173 
    174 	debug_printf("%*s", 2 * debug_indentation, "");
    175 }
    176 
    177 static void
    178 debug_enter(const char *func)
    179 {
    180 
    181 	printf("%*s+ %s\n", 2 * debug_indentation++, "", func);
    182 }
    183 
    184 static void __printflike(1, 2)
    185 debug_step(const char *fmt, ...)
    186 {
    187 	va_list va;
    188 
    189 	debug_indent();
    190 	va_start(va, fmt);
    191 	vfprintf(stdout, fmt, va);
    192 	va_end(va);
    193 	printf("\n");
    194 }
    195 
    196 static void
    197 debug_leave(const char *func)
    198 {
    199 
    200 	printf("%*s- %s\n", 2 * --debug_indentation, "", func);
    201 }
    202 
    203 #define debug_enter()		(debug_enter)(__func__)
    204 #define debug_leave()		(debug_leave)(__func__)
    205 
    206 #else
    207 
    208 /* TODO: This is C99 */
    209 #define debug_printf(fmt, ...)	do { } while (false)
    210 #define debug_indent()		do { } while (false)
    211 #define debug_enter()		do { } while (false)
    212 #define debug_step(fmt, ...)	do { } while (false)
    213 #define debug_leave()		do { } while (false)
    214 
    215 #endif
    216 
    217 
    218 static void *
    219 unconst_cast(const void *p)
    220 {
    221 	void *r;
    222 
    223 	memcpy(&r, &p, sizeof r);
    224 	return r;
    225 }
    226 
    227 /* C99 6.7.8p7 */
    228 static bool
    229 is_struct_or_union(tspec_t t)
    230 {
    231 
    232 	return t == STRUCT || t == UNION;
    233 }
    234 
    235 static bool
    236 has_automatic_storage_duration(const sym_t *sym)
    237 {
    238 
    239 	return sym->s_scl == AUTO || sym->s_scl == REG;
    240 }
    241 
    242 /* C99 6.7.8p14, 6.7.8p15 */
    243 static bool
    244 is_string_array(const type_t *tp, tspec_t t)
    245 {
    246 	tspec_t st;
    247 
    248 	if (tp == NULL || tp->t_tspec != ARRAY)
    249 		return false;
    250 
    251 	st = tp->t_subt->t_tspec;
    252 	return t == CHAR
    253 	    ? st == CHAR || st == UCHAR || st == SCHAR
    254 	    : st == WCHAR;
    255 }
    256 
    257 /* C99 6.7.8p9 */
    258 static bool
    259 is_unnamed(const sym_t *m)
    260 {
    261 
    262 	return m->s_bitfield && m->s_name == unnamed;
    263 }
    264 
    265 /* C99 6.7.8p9 */
    266 static const sym_t *
    267 skip_unnamed(const sym_t *m)
    268 {
    269 
    270 	while (m != NULL && is_unnamed(m))
    271 		m = m->s_next;
    272 	return m;
    273 }
    274 
    275 static const sym_t *
    276 first_named_member(const type_t *tp)
    277 {
    278 
    279 	lint_assert(is_struct_or_union(tp->t_tspec));
    280 	return skip_unnamed(tp->t_str->sou_first_member);
    281 }
    282 
    283 static const sym_t *
    284 look_up_member(const type_t *tp, const char *name)
    285 {
    286 	const sym_t *m;
    287 
    288 	lint_assert(is_struct_or_union(tp->t_tspec));
    289 	for (m = tp->t_str->sou_first_member; m != NULL; m = m->s_next)
    290 		if (!is_unnamed(m) && strcmp(m->s_name, name) == 0)
    291 			return m;
    292 	return NULL;
    293 }
    294 
    295 static const type_t *
    296 sym_type(const sym_t *sym)
    297 {
    298 
    299 	return sym != NULL ? sym->s_type : NULL;
    300 }
    301 
    302 static const type_t *
    303 look_up_member_type(const type_t *tp, const char *name)
    304 {
    305 	const sym_t *member;
    306 
    307 	member = look_up_member(tp, name);
    308 	if (member == NULL) {
    309 		/* type '%s' does not have member '%s' */
    310 		error(101, type_name(tp), name);
    311 	}
    312 
    313 	return sym_type(member);
    314 }
    315 
    316 static void
    317 update_type_of_array_of_unknown_size(sym_t *sym, size_t size)
    318 {
    319 	type_t *tp;
    320 
    321 	tp = duptyp(sym->s_type);
    322 	tp->t_dim = (int)size;
    323 	tp->t_incomplete_array = false;
    324 	sym->s_type = tp;
    325 }
    326 
    327 
    328 /* In traditional C, bit-fields can be initialized only by integer constants. */
    329 static void
    330 check_bit_field_init(const tnode_t *ln, tspec_t lt, tspec_t rt)
    331 {
    332 
    333 	if (tflag &&
    334 	    is_integer(lt) &&
    335 	    ln->tn_type->t_bitfield &&
    336 	    !is_integer(rt)) {
    337 		/* bit-field initialization is illegal in traditional C */
    338 		warning(186);
    339 	}
    340 }
    341 
    342 static void
    343 check_non_constant_initializer(const tnode_t *tn, const sym_t *sym)
    344 {
    345 	const sym_t *unused_sym;
    346 	ptrdiff_t unused_offs;
    347 
    348 	if (tn == NULL || tn->tn_op == CON)
    349 		return;
    350 
    351 	if (constant_addr(tn, &unused_sym, &unused_offs))
    352 		return;
    353 
    354 	if (has_automatic_storage_duration(sym)) {
    355 		/* non-constant initializer */
    356 		c99ism(177);
    357 	} else {
    358 		/* non-constant initializer */
    359 		error(177);
    360 	}
    361 }
    362 
    363 static void
    364 check_no_auto_aggregate(const sym_t *sym)
    365 {
    366 
    367 	if (tflag &&
    368 	    has_automatic_storage_duration(sym) &&
    369 	    !is_scalar(sym->s_type->t_tspec)) {
    370 		/* no automatic aggregate initialization in trad. C */
    371 		warning(188);
    372 	}
    373 }
    374 
    375 static void
    376 check_init_expr(const type_t *tp, sym_t *sym, tnode_t *tn)
    377 {
    378 	tnode_t *ln;
    379 	tspec_t lt, rt;
    380 	struct mbl *tmem;
    381 
    382 	/* Create a temporary node for the left side. */
    383 	ln = tgetblk(sizeof *ln);
    384 	ln->tn_op = NAME;
    385 	ln->tn_type = tduptyp(tp);
    386 	ln->tn_type->t_const = false;
    387 	ln->tn_lvalue = true;
    388 	ln->tn_sym = sym;
    389 
    390 	tn = cconv(tn);
    391 
    392 	lt = ln->tn_type->t_tspec;
    393 	rt = tn->tn_type->t_tspec;
    394 
    395 	debug_step("typeok '%s', '%s'",
    396 	    type_name(ln->tn_type), type_name(tn->tn_type));
    397 	if (!typeok(INIT, 0, ln, tn))
    398 		return;
    399 
    400 	/*
    401 	 * Preserve the tree memory. This is necessary because otherwise
    402 	 * expr() would free it.
    403 	 */
    404 	tmem = tsave();
    405 	expr(tn, true, false, true, false);
    406 	trestor(tmem);
    407 
    408 	check_bit_field_init(ln, lt, rt);
    409 
    410 	/*
    411 	 * XXX: Is it correct to do this conversion _after_ the typeok above?
    412 	 */
    413 	if (lt != rt || (tp->t_bitfield && tn->tn_op == CON))
    414 		tn = convert(INIT, 0, unconst_cast(tp), tn);
    415 
    416 	check_non_constant_initializer(tn, sym);
    417 }
    418 
    419 
    420 static struct designator *
    421 designator_new(const char *name, size_t subscript)
    422 {
    423 	struct designator *dr;
    424 
    425 	dr = xcalloc(1, sizeof *dr);
    426 	dr->dr_name = name;
    427 	dr->dr_subscript = subscript;
    428 	return dr;
    429 }
    430 
    431 static void
    432 designator_free(struct designator *dr)
    433 {
    434 
    435 	free(dr);
    436 }
    437 
    438 
    439 static const type_t *
    440 designator_look_up(const struct designator *dr, const type_t *tp)
    441 {
    442 	switch (tp->t_tspec) {
    443 	case STRUCT:
    444 	case UNION:
    445 		if (dr->dr_name == NULL) {
    446 			/* syntax error '%s' */
    447 			error(249, "designator '[...]' is only for arrays");
    448 			return sym_type(first_named_member(tp));
    449 		}
    450 
    451 		return look_up_member_type(tp, dr->dr_name);
    452 	case ARRAY:
    453 		if (dr->dr_name != NULL) {
    454 			/* syntax error '%s' */
    455 			error(249,
    456 			    "designator '.member' is only for struct/union");
    457 		}
    458 		if (!tp->t_incomplete_array &&
    459 		    dr->dr_subscript >= (size_t)tp->t_dim) {
    460 			/* array subscript cannot be > %d: %ld */
    461 			error(168, tp->t_dim - 1, (long)dr->dr_subscript);
    462 		}
    463 		return tp->t_subt;
    464 	default:
    465 		/* syntax error '%s' */
    466 		error(249, "scalar type cannot use designator");
    467 		return tp;
    468 	}
    469 }
    470 
    471 
    472 #ifdef DEBUG
    473 static void
    474 designation_debug(const struct designation *dn)
    475 {
    476 	const struct designator *dr;
    477 
    478 	if (dn->dn_head == NULL)
    479 		return;
    480 
    481 	debug_indent();
    482 	debug_printf("designation: ");
    483 	for (dr = dn->dn_head; dr != NULL; dr = dr->dr_next) {
    484 		if (dr->dr_name != NULL) {
    485 			debug_printf(".%s", dr->dr_name);
    486 			lint_assert(dr->dr_subscript == 0);
    487 		} else
    488 			debug_printf("[%zu]", dr->dr_subscript);
    489 	}
    490 	debug_printf("\n");
    491 }
    492 #else
    493 #define designation_debug(dn) do { } while (false)
    494 #endif
    495 
    496 static void
    497 designation_add(struct designation *dn, const char *name, size_t subscript)
    498 {
    499 	struct designator *dr;
    500 
    501 	dr = designator_new(name, subscript);
    502 
    503 	if (dn->dn_head != NULL) {
    504 		dn->dn_tail->dr_next = dr;
    505 		dn->dn_tail = dr;
    506 	} else {
    507 		dn->dn_head = dr;
    508 		dn->dn_tail = dr;
    509 	}
    510 }
    511 
    512 /*
    513  * Starting at the type of the current object, resolve the type of the
    514  * sub-object by following each designator in the list.
    515  */
    516 static const type_t *
    517 designation_look_up(const struct designation *dn, const type_t *tp)
    518 {
    519 	const struct designator *dr;
    520 
    521 	for (dr = dn->dn_head; dr != NULL && tp != NULL; dr = dr->dr_next)
    522 		tp = designator_look_up(dr, tp);
    523 	return tp;
    524 }
    525 
    526 
    527 
    528 static void
    529 designation_reset(struct designation *dn)
    530 {
    531 	struct designator *dr, *next;
    532 
    533 	for (dr = dn->dn_head; dr != NULL; dr = next) {
    534 		next = dr->dr_next;
    535 		designator_free(dr);
    536 	}
    537 }
    538 
    539 
    540 static struct brace_level *
    541 brace_level_new(const type_t *tp, struct brace_level *enclosing)
    542 {
    543 	struct brace_level *bl;
    544 
    545 	bl = xcalloc(1, sizeof(*bl));
    546 	bl->bl_type = tp;
    547 	bl->bl_enclosing = enclosing;
    548 	if (is_struct_or_union(tp->t_tspec))
    549 		bl->bl_next_member = first_named_member(tp);
    550 
    551 	return bl;
    552 }
    553 
    554 static void
    555 brace_level_free(struct brace_level *bl)
    556 {
    557 
    558 	designation_reset(&bl->bl_designation);
    559 	free(bl);
    560 }
    561 
    562 #ifdef DEBUG
    563 static void
    564 brace_level_debug(const struct brace_level *bl)
    565 {
    566 
    567 	lint_assert(bl->bl_type != NULL);
    568 	lint_assert(bl->bl_next_member == NULL ||
    569 	    !is_unnamed(bl->bl_next_member));
    570 
    571 	debug_printf("type '%s'", type_name(bl->bl_type));
    572 
    573 	if (bl->bl_array_of_unknown_size)
    574 		debug_printf(", array of unknown size");
    575 
    576 	if (is_struct_or_union(bl->bl_type->t_tspec) &&
    577 	    bl->bl_next_member != NULL)
    578 		debug_printf(", next member '%s'",
    579 		    bl->bl_next_member->s_name);
    580 	if (bl->bl_type->t_tspec == ARRAY)
    581 		debug_printf(", next array subscript %zu",
    582 		    bl->bl_array_next_subscript);
    583 
    584 	debug_printf("\n");
    585 }
    586 #else
    587 #define brace_level_debug(level) do { } while (false)
    588 #endif
    589 
    590 static void
    591 brace_level_remove_designation(struct brace_level *bl)
    592 {
    593 	struct designator *dr, *next;
    594 
    595 	for (dr = bl->bl_designation.dn_head; dr != NULL; dr = next) {
    596 		next = dr->dr_next;
    597 		designator_free(dr);
    598 	}
    599 
    600 	bl->bl_designation.dn_head = NULL;
    601 	bl->bl_designation.dn_tail = NULL;
    602 }
    603 
    604 
    605 static const type_t *
    606 brace_level_sub_type_struct_or_union(const struct brace_level *bl)
    607 {
    608 
    609 	if (bl->bl_next_member == NULL) {
    610 		/* too many struct/union initializers */
    611 		error(172);
    612 		return NULL;
    613 	}
    614 
    615 	lint_assert(!is_unnamed(bl->bl_next_member));
    616 	return sym_type(bl->bl_next_member);
    617 }
    618 
    619 static const type_t *
    620 brace_level_sub_type_array(const struct brace_level *bl)
    621 {
    622 
    623 	if (!bl->bl_type->t_incomplete_array &&
    624 	    !bl->bl_omitted_braces &&
    625 	    bl->bl_array_next_subscript >= (size_t)bl->bl_type->t_dim) {
    626 		/* too many array initializers, expected %d */
    627 		error(173, bl->bl_type->t_dim);
    628 	}
    629 
    630 	return bl->bl_type->t_subt;
    631 }
    632 
    633 static const type_t *
    634 brace_level_sub_type_scalar(const struct brace_level *bl)
    635 {
    636 
    637 	if (bl->bl_scalar_done) {
    638 		/* too many initializers */
    639 		error(174);
    640 	}
    641 
    642 	return bl->bl_type;
    643 }
    644 
    645 /* Return the type of the sub-object that is currently being initialized. */
    646 static const type_t *
    647 brace_level_sub_type(const struct brace_level *bl)
    648 {
    649 
    650 	if (bl->bl_designation.dn_head != NULL)
    651 		return designation_look_up(&bl->bl_designation, bl->bl_type);
    652 
    653 	switch (bl->bl_type->t_tspec) {
    654 	case STRUCT:
    655 	case UNION:
    656 		return brace_level_sub_type_struct_or_union(bl);
    657 	case ARRAY:
    658 		return brace_level_sub_type_array(bl);
    659 	default:
    660 		return brace_level_sub_type_scalar(bl);
    661 	}
    662 }
    663 
    664 static void
    665 brace_level_apply_designation(struct brace_level *bl)
    666 {
    667 	const struct designator *dr = bl->bl_designation.dn_head;
    668 
    669 	if (dr == NULL)
    670 		return;
    671 
    672 	designation_debug(&bl->bl_designation);
    673 
    674 	switch (bl->bl_type->t_tspec) {
    675 	case STRUCT:
    676 	case UNION:
    677 		if (dr->dr_name == NULL)
    678 			return;	/* error, silently ignored */
    679 		bl->bl_next_member = look_up_member(bl->bl_type, dr->dr_name);
    680 		break;
    681 	case ARRAY:
    682 		if (dr->dr_name != NULL)
    683 			return;	/* error, silently ignored */
    684 		bl->bl_array_next_subscript = dr->dr_subscript;
    685 		break;
    686 	default:
    687 		break;		/* error, silently ignored */
    688 	}
    689 }
    690 
    691 /*
    692  * After initializing a sub-object, advance to the next sub-object.
    693  *
    694  * C99 6.7.8p17
    695  */
    696 static void
    697 brace_level_advance(struct brace_level *bl)
    698 {
    699 
    700 	switch (bl->bl_type->t_tspec) {
    701 	case STRUCT:
    702 		lint_assert(bl->bl_next_member != NULL);
    703 		bl->bl_next_member = skip_unnamed(bl->bl_next_member->s_next);
    704 		break;
    705 	case UNION:
    706 		bl->bl_next_member = NULL;
    707 		break;
    708 	case ARRAY:
    709 		bl->bl_array_next_subscript++;
    710 		break;
    711 	default:
    712 		bl->bl_scalar_done = true;
    713 		break;
    714 	}
    715 }
    716 
    717 
    718 static struct initialization *
    719 initialization_new(sym_t *sym)
    720 {
    721 	struct initialization *in;
    722 
    723 	in = xcalloc(1, sizeof(*in));
    724 	in->in_sym = sym;
    725 
    726 	return in;
    727 }
    728 
    729 static void
    730 initialization_free(struct initialization *in)
    731 {
    732 	struct brace_level *bl, *next;
    733 
    734 	for (bl = in->in_brace_level; bl != NULL; bl = next) {
    735 		next = bl->bl_enclosing;
    736 		brace_level_free(bl);
    737 	}
    738 
    739 	free(in);
    740 }
    741 
    742 #ifdef DEBUG
    743 static void
    744 initialization_debug(const struct initialization *in)
    745 {
    746 	size_t i;
    747 	const struct brace_level *bl;
    748 
    749 	if (in->in_brace_level == NULL) {
    750 		debug_step("no brace level");
    751 		return;
    752 	}
    753 
    754 	i = 0;
    755 	for (bl = in->in_brace_level; bl != NULL; bl = bl->bl_enclosing) {
    756 		debug_indent();
    757 		debug_printf("brace level %zu: ", i);
    758 		brace_level_debug(bl);
    759 		i++;
    760 	}
    761 }
    762 #else
    763 #define initialization_debug(in) do { } while (false)
    764 #endif
    765 
    766 /*
    767  * Return the type of the object or sub-object that is currently being
    768  * initialized.
    769  */
    770 static const type_t *
    771 initialization_sub_type(struct initialization *in)
    772 {
    773 	const type_t *tp;
    774 
    775 	tp = in->in_brace_level != NULL
    776 	    ? brace_level_sub_type(in->in_brace_level)
    777 	    : in->in_sym->s_type;
    778 	if (tp == NULL)
    779 		in->in_err = true;
    780 	return tp;
    781 }
    782 
    783 static void
    784 initialization_begin_brace_level(struct initialization *in)
    785 {
    786 	const type_t *tp;
    787 
    788 	if (in->in_err)
    789 		return;
    790 
    791 	debug_enter();
    792 
    793 	tp = initialization_sub_type(in);
    794 	if (tp == NULL) {
    795 		in->in_err = true;
    796 		goto done;
    797 	}
    798 
    799 	if (tflag && in->in_brace_level == NULL)
    800 		check_no_auto_aggregate(in->in_sym);
    801 
    802 	if (tflag && tp->t_tspec == UNION) {
    803 		/* initialization of union is illegal in traditional C */
    804 		warning(238);
    805 	}
    806 
    807 	if (tp->t_tspec == STRUCT && tp->t_str->sou_incomplete) {
    808 		/* initialization of incomplete type '%s' */
    809 		error(175, type_name(tp));
    810 		in->in_err = true;
    811 		goto done;
    812 	}
    813 
    814 	if (in->in_brace_level != NULL)
    815 		brace_level_apply_designation(in->in_brace_level);
    816 
    817 	in->in_brace_level = brace_level_new(tp, in->in_brace_level);
    818 
    819 done:
    820 	initialization_debug(in);
    821 	debug_leave();
    822 }
    823 
    824 /* C99 6.7.8p22 */
    825 static void
    826 initialization_set_size_of_unknown_array(struct initialization *in)
    827 {
    828 
    829 	if (in->in_sym->s_type->t_incomplete_array &&
    830 	    in->in_brace_level->bl_enclosing == NULL)
    831 		update_type_of_array_of_unknown_size(in->in_sym,
    832 		    in->in_brace_level->bl_array_next_subscript);
    833 }
    834 
    835 static void
    836 initialization_end_brace_level(struct initialization *in)
    837 {
    838 	struct brace_level *bl;
    839 
    840 	if (in->in_err)
    841 		return;
    842 
    843 	debug_enter();
    844 
    845 	initialization_set_size_of_unknown_array(in);
    846 
    847 	bl = in->in_brace_level;
    848 	in->in_brace_level = bl->bl_enclosing;
    849 	brace_level_free(bl);
    850 
    851 	if (in->in_brace_level != NULL) {
    852 		brace_level_advance(in->in_brace_level);
    853 		brace_level_remove_designation(in->in_brace_level);
    854 	}
    855 
    856 	initialization_debug(in);
    857 	debug_leave();
    858 }
    859 
    860 static void
    861 initialization_add_designator(struct initialization *in,
    862 			      const char *name, size_t subscript)
    863 {
    864 
    865 	if (in->in_err)
    866 		return;
    867 
    868 	lint_assert(in->in_brace_level != NULL);
    869 	designation_add(&in->in_brace_level->bl_designation, name, subscript);
    870 }
    871 
    872 static void
    873 initialization_remove_designation(struct initialization *in)
    874 {
    875 
    876 	if (in->in_brace_level != NULL)
    877 		brace_level_remove_designation(in->in_brace_level);
    878 }
    879 
    880 /*
    881  * An object with automatic storage duration that has a single initializer
    882  * expression without braces and is not an array is initialized by delegating
    883  * to the ASSIGN operator.
    884  */
    885 static bool
    886 initialization_expr_using_assign(struct initialization *in, tnode_t *rn)
    887 {
    888 	tnode_t *ln, *tn;
    889 
    890 	if (!has_automatic_storage_duration(in->in_sym))
    891 		return false;
    892 	if (in->in_brace_level != NULL)
    893 		return false;
    894 	if (in->in_sym->s_type->t_tspec == ARRAY)
    895 		return false;
    896 
    897 	debug_step("handing over to ASSIGN");
    898 
    899 	ln = new_name_node(in->in_sym, 0);
    900 	ln->tn_type = tduptyp(ln->tn_type);
    901 	ln->tn_type->t_const = false;
    902 
    903 	tn = build(ASSIGN, ln, rn);
    904 	expr(tn, false, false, false, false);
    905 
    906 	return true;
    907 }
    908 
    909 /* Initialize a character array or wchar_t array with a string literal. */
    910 static bool
    911 initialization_init_array_using_string(struct initialization *in, tnode_t *tn)
    912 {
    913 	struct brace_level *bl;
    914 	const type_t *tp;
    915 	strg_t	*strg;
    916 
    917 	if (tn->tn_op != STRING)
    918 		return false;
    919 
    920 	bl = in->in_brace_level;
    921 	tp = initialization_sub_type(in);
    922 	strg = tn->tn_string;
    923 
    924 	if (!is_string_array(tp, strg->st_tspec))
    925 		return false;
    926 	if (bl != NULL && tp->t_tspec != ARRAY &&
    927 	    bl->bl_array_next_subscript != 0)
    928 		return false;
    929 
    930 	if (bl != NULL && tp->t_dim < (int)strg->st_len) {
    931 		/* non-null byte ignored in string initializer */
    932 		warning(187);
    933 	}
    934 
    935 	if (tp == in->in_sym->s_type && tp->t_incomplete_array) {
    936 		if (bl != NULL) {
    937 			bl->bl_array_next_subscript = strg->st_len + 1;
    938 			/* see initialization_set_size_of_unknown_array */
    939 		} else
    940 			update_type_of_array_of_unknown_size(in->in_sym,
    941 			    strg->st_len + 1);
    942 	}
    943 
    944 	return true;
    945 }
    946 
    947 /*
    948  * Initialize a single sub-object as part of the currently ongoing
    949  * initialization.
    950  */
    951 static void
    952 initialization_expr(struct initialization *in, tnode_t *tn)
    953 {
    954 	const type_t *tp;
    955 
    956 	if (in->in_err)
    957 		return;
    958 
    959 	if (in->in_brace_level != NULL &&
    960 	    in->in_brace_level->bl_omitted_braces)
    961 		return;
    962 
    963 	debug_enter();
    964 
    965 	if (tn == NULL)
    966 		goto advance;
    967 	if (initialization_expr_using_assign(in, tn))
    968 		goto done;
    969 	if (initialization_init_array_using_string(in, tn))
    970 		goto advance;
    971 
    972 	if (in->in_brace_level != NULL)
    973 		brace_level_apply_designation(in->in_brace_level);
    974 	tp = initialization_sub_type(in);
    975 	if (tp == NULL)
    976 		goto done;
    977 
    978 	if (in->in_brace_level == NULL && !is_scalar(tp->t_tspec)) {
    979 		/* {}-enclosed initializer required */
    980 		error(181);
    981 		goto done;
    982 	}
    983 
    984 	/*
    985 	 * Hack to accept initializations with omitted braces, see
    986 	 * c99_6_7_8_p28_example5 in test d_c99_init.c.  Since both GCC and
    987 	 * Clang already warn about this at level -Wall, there is no point
    988 	 * in letting lint check this again.
    989 	 */
    990 	if (is_scalar(tn->tn_type->t_tspec) &&
    991 	    tp->t_tspec == ARRAY &&
    992 	    in->in_brace_level != NULL) {
    993 		in->in_brace_level->bl_omitted_braces = true;
    994 		goto done;
    995 	}
    996 
    997 	debug_step("expecting '%s', expression has '%s'",
    998 	    type_name(tp), type_name(tn->tn_type));
    999 	check_init_expr(tp, in->in_sym, tn);
   1000 
   1001 advance:
   1002 	if (in->in_brace_level != NULL)
   1003 		brace_level_advance(in->in_brace_level);
   1004 done:
   1005 	initialization_remove_designation(in);
   1006 	initialization_debug(in);
   1007 	debug_leave();
   1008 }
   1009 
   1010 
   1011 static struct initialization *init;
   1012 
   1013 
   1014 static struct initialization *
   1015 current_init(void)
   1016 {
   1017 
   1018 	lint_assert(init != NULL);
   1019 	return init;
   1020 }
   1021 
   1022 sym_t **
   1023 current_initsym(void)
   1024 {
   1025 
   1026 	return &current_init()->in_sym;
   1027 }
   1028 
   1029 void
   1030 begin_initialization(sym_t *sym)
   1031 {
   1032 	struct initialization *in;
   1033 
   1034 	debug_step("begin initialization of '%s'", type_name(sym->s_type));
   1035 #ifdef DEBUG
   1036 	debug_indentation++;
   1037 #endif
   1038 
   1039 	in = initialization_new(sym);
   1040 	in->in_enclosing = init;
   1041 	init = in;
   1042 }
   1043 
   1044 void
   1045 end_initialization(void)
   1046 {
   1047 	struct initialization *in;
   1048 
   1049 	in = init;
   1050 	init = in->in_enclosing;
   1051 	initialization_free(in);
   1052 
   1053 #ifdef DEBUG
   1054 	debug_indentation--;
   1055 #endif
   1056 	debug_step("end initialization");
   1057 }
   1058 
   1059 void
   1060 add_designator_member(sbuf_t *sb)
   1061 {
   1062 
   1063 	initialization_add_designator(current_init(), sb->sb_name, 0);
   1064 }
   1065 
   1066 void
   1067 add_designator_subscript(range_t range)
   1068 {
   1069 
   1070 	initialization_add_designator(current_init(), NULL, range.hi);
   1071 }
   1072 
   1073 void
   1074 init_lbrace(void)
   1075 {
   1076 
   1077 	initialization_begin_brace_level(current_init());
   1078 }
   1079 
   1080 void
   1081 init_expr(tnode_t *tn)
   1082 {
   1083 
   1084 	initialization_expr(current_init(), tn);
   1085 }
   1086 
   1087 void
   1088 init_rbrace(void)
   1089 {
   1090 
   1091 	initialization_end_brace_level(current_init());
   1092 }
   1093