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