Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.53
      1  1.53    rillig /*	$NetBSD: init.c,v 1.53 2021/01/01 19:15:58 rillig Exp $	*/
      2   1.2       cgd 
      3   1.1       cgd /*
      4   1.1       cgd  * Copyright (c) 1994, 1995 Jochen Pohl
      5   1.1       cgd  * All Rights Reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *      This product includes software developed by Jochen Pohl for
     18   1.1       cgd  *	The NetBSD Project.
     19   1.1       cgd  * 4. The name of the author may not be used to endorse or promote products
     20   1.1       cgd  *    derived from this software without specific prior written permission.
     21   1.1       cgd  *
     22   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1       cgd  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1       cgd  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1       cgd  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1       cgd  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1       cgd  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1       cgd  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1       cgd  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1       cgd  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1       cgd  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1       cgd  */
     33   1.1       cgd 
     34  1.18       jmc #if HAVE_NBTOOL_CONFIG_H
     35  1.18       jmc #include "nbtool_config.h"
     36  1.18       jmc #endif
     37  1.18       jmc 
     38   1.5  christos #include <sys/cdefs.h>
     39  1.10        tv #if defined(__RCSID) && !defined(lint)
     40  1.53    rillig __RCSID("$NetBSD: init.c,v 1.53 2021/01/01 19:15:58 rillig Exp $");
     41   1.1       cgd #endif
     42   1.1       cgd 
     43  1.31    rillig #include <ctype.h>
     44   1.1       cgd #include <stdlib.h>
     45  1.17   thorpej #include <string.h>
     46   1.1       cgd 
     47   1.1       cgd #include "lint1.h"
     48   1.1       cgd 
     49  1.53    rillig 
     50  1.53    rillig /*
     51  1.53    rillig  * Type of stack which is used for initialisation of aggregate types.
     52  1.53    rillig  */
     53  1.53    rillig typedef	struct istk {
     54  1.53    rillig 	type_t	*i_type;		/* type of initialisation */
     55  1.53    rillig 	type_t	*i_subt;		/* type of next level */
     56  1.53    rillig 	u_int	i_brace : 1;		/* need } for pop */
     57  1.53    rillig 	u_int	i_nolimit : 1;		/* incomplete array type */
     58  1.53    rillig 	u_int	i_namedmem : 1;		/* has c9x named members */
     59  1.53    rillig 	sym_t	*i_mem;			/* next structure member */
     60  1.53    rillig 	int	i_remaining;		/* # of remaining elements */
     61  1.53    rillig 	struct	istk *i_next;		/* previous level */
     62  1.53    rillig } istk_t;
     63  1.53    rillig 
     64  1.53    rillig typedef struct namlist {
     65  1.53    rillig 	const char *n_name;
     66  1.53    rillig 	struct namlist *n_prev;
     67  1.53    rillig 	struct namlist *n_next;
     68  1.53    rillig } namlist_t;
     69  1.53    rillig 
     70  1.53    rillig 
     71   1.1       cgd /*
     72   1.8       wiz  * initerr is set as soon as a fatal error occurred in an initialisation.
     73   1.1       cgd  * The effect is that the rest of the initialisation is ignored (parsed
     74   1.1       cgd  * by yacc, expression trees built, but no initialisation takes place).
     75   1.1       cgd  */
     76   1.1       cgd int	initerr;
     77   1.1       cgd 
     78   1.1       cgd /* Pointer to the symbol which is to be initialized. */
     79   1.1       cgd sym_t	*initsym;
     80   1.1       cgd 
     81   1.1       cgd /* Points to the top element of the initialisation stack. */
     82   1.1       cgd istk_t	*initstk;
     83   1.1       cgd 
     84  1.12  christos /* Points to a c9x named member; */
     85  1.12  christos namlist_t	*namedmem = NULL;
     86  1.12  christos 
     87   1.1       cgd 
     88  1.35    rillig static	int	initstack_string(tnode_t *);
     89  1.12  christos 
     90  1.12  christos #ifndef DEBUG
     91  1.12  christos #define DPRINTF(a)
     92  1.12  christos #else
     93  1.12  christos #define DPRINTF(a) printf a
     94  1.12  christos #endif
     95  1.12  christos 
     96  1.12  christos void
     97  1.51    rillig push_member(sbuf_t *sb)
     98  1.12  christos {
     99  1.28    rillig 	namlist_t *nam = xcalloc(1, sizeof (namlist_t));
    100  1.12  christos 	nam->n_name = sb->sb_name;
    101  1.27  christos 	DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam));
    102  1.12  christos 	if (namedmem == NULL) {
    103  1.51    rillig 		/*
    104  1.51    rillig 		 * XXX: Why is this a circular list?
    105  1.51    rillig 		 * XXX: Why is this a doubly-linked list?
    106  1.51    rillig 		 * A simple stack should suffice.
    107  1.51    rillig 		 */
    108  1.12  christos 		nam->n_prev = nam->n_next = nam;
    109  1.12  christos 		namedmem = nam;
    110  1.12  christos 	} else {
    111  1.12  christos 		namedmem->n_prev->n_next = nam;
    112  1.12  christos 		nam->n_prev = namedmem->n_prev;
    113  1.12  christos 		nam->n_next = namedmem;
    114  1.12  christos 		namedmem->n_prev = nam;
    115  1.12  christos 	}
    116  1.12  christos }
    117  1.12  christos 
    118  1.12  christos static void
    119  1.34    rillig pop_member(void)
    120  1.12  christos {
    121  1.27  christos 	DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem));
    122  1.12  christos 	if (namedmem->n_next == namedmem) {
    123  1.12  christos 		free(namedmem);
    124  1.12  christos 		namedmem = NULL;
    125  1.12  christos 	} else {
    126  1.12  christos 		namlist_t *nam = namedmem;
    127  1.12  christos 		namedmem = namedmem->n_next;
    128  1.52    rillig 		nam->n_prev->n_next = nam->n_next;
    129  1.52    rillig 		nam->n_next->n_prev = nam->n_prev;
    130  1.12  christos 		free(nam);
    131  1.12  christos 	}
    132  1.12  christos }
    133   1.1       cgd 
    134   1.1       cgd 
    135   1.1       cgd /*
    136   1.1       cgd  * Initialize the initialisation stack by putting an entry for the variable
    137   1.1       cgd  * which is to be initialized on it.
    138   1.1       cgd  */
    139   1.1       cgd void
    140  1.35    rillig initstack_init(void)
    141   1.1       cgd {
    142   1.1       cgd 	istk_t	*istk;
    143   1.1       cgd 
    144   1.1       cgd 	if (initerr)
    145   1.1       cgd 		return;
    146   1.1       cgd 
    147   1.1       cgd 	/* free memory used in last initialisation */
    148   1.1       cgd 	while ((istk = initstk) != NULL) {
    149  1.46    rillig 		initstk = istk->i_next;
    150   1.1       cgd 		free(istk);
    151   1.1       cgd 	}
    152   1.1       cgd 
    153  1.38    rillig 	DPRINTF(("%s\n", __func__));
    154  1.38    rillig 
    155   1.1       cgd 	/*
    156   1.1       cgd 	 * If the type which is to be initialized is an incomplete type,
    157   1.1       cgd 	 * it must be duplicated.
    158   1.1       cgd 	 */
    159   1.1       cgd 	if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
    160   1.1       cgd 		initsym->s_type = duptyp(initsym->s_type);
    161   1.1       cgd 
    162   1.1       cgd 	istk = initstk = xcalloc(1, sizeof (istk_t));
    163   1.1       cgd 	istk->i_subt = initsym->s_type;
    164  1.43    rillig 	istk->i_remaining = 1;
    165   1.1       cgd }
    166   1.1       cgd 
    167   1.1       cgd static void
    168  1.35    rillig initstack_pop_item(void)
    169   1.1       cgd {
    170  1.12  christos #ifdef DEBUG
    171  1.12  christos 	char	buf[64];
    172  1.12  christos #endif
    173   1.1       cgd 	istk_t	*istk;
    174   1.1       cgd 	sym_t	*m;
    175   1.1       cgd 
    176  1.41    rillig 	istk = initstk;
    177  1.40    rillig 	DPRINTF(("%s: pop type=%s, brace=%d remaining=%d named=%d\n", __func__,
    178  1.41    rillig 	    tyname(buf, sizeof buf, istk->i_type ? istk->i_type : istk->i_subt),
    179  1.43    rillig 	    istk->i_brace, istk->i_remaining, istk->i_namedmem));
    180  1.40    rillig 
    181  1.46    rillig 	initstk = istk->i_next;
    182   1.1       cgd 	free(istk);
    183   1.1       cgd 	istk = initstk;
    184  1.44    rillig 	lint_assert(istk != NULL);
    185  1.26  christos 
    186  1.40    rillig 	DPRINTF(("%s: top type=%s, brace=%d remaining=%d named=%d\n", __func__,
    187  1.41    rillig 	    tyname(buf, sizeof buf, istk->i_type ? istk->i_type : istk->i_subt),
    188  1.43    rillig 	    istk->i_brace, istk->i_remaining, istk->i_namedmem));
    189   1.1       cgd 
    190  1.43    rillig 	istk->i_remaining--;
    191  1.44    rillig 	lint_assert(istk->i_remaining >= 0);
    192   1.1       cgd 
    193  1.40    rillig 	DPRINTF(("%s: top remaining=%d rhs.name=%s\n", __func__,
    194  1.43    rillig 	    istk->i_remaining, namedmem ? namedmem->n_name : "*null*"));
    195  1.40    rillig 
    196  1.43    rillig 	if (istk->i_remaining >= 0 && namedmem != NULL) {
    197  1.40    rillig 
    198  1.40    rillig 		DPRINTF(("%s: named remaining=%d type=%s, rhs.name=%s\n",
    199  1.43    rillig 		    __func__, istk->i_remaining,
    200  1.40    rillig 		    tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
    201  1.38    rillig 
    202  1.45    rillig 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) {
    203  1.40    rillig 			DPRINTF(("%s: pop lhs.name=%s rhs.name=%s\n", __func__,
    204  1.40    rillig 			    m->s_name, namedmem->n_name));
    205  1.48    rillig 			if (m->s_bitfield && m->s_name == unnamed)
    206  1.12  christos 				continue;
    207  1.12  christos 			if (strcmp(m->s_name, namedmem->n_name) == 0) {
    208  1.12  christos 				istk->i_subt = m->s_type;
    209  1.43    rillig 				istk->i_remaining++;
    210  1.34    rillig 				pop_member();
    211  1.12  christos 				return;
    212  1.12  christos 			}
    213  1.12  christos 		}
    214  1.50    rillig 		/* undefined struct/union member: %s */
    215  1.12  christos 		error(101, namedmem->n_name);
    216  1.40    rillig 		DPRINTF(("%s: end rhs.name=%s\n", __func__, namedmem->n_name));
    217  1.34    rillig 		pop_member();
    218  1.12  christos 		istk->i_namedmem = 1;
    219  1.12  christos 		return;
    220  1.12  christos 	}
    221   1.1       cgd 	/*
    222   1.1       cgd 	 * If the removed element was a structure member, we must go
    223   1.1       cgd 	 * to the next structure member.
    224   1.1       cgd 	 */
    225  1.43    rillig 	if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
    226  1.12  christos 	    !istk->i_namedmem) {
    227   1.1       cgd 		do {
    228  1.45    rillig 			m = istk->i_mem = istk->i_mem->s_next;
    229  1.44    rillig 			lint_assert(m != NULL);
    230  1.40    rillig 			DPRINTF(("%s: pop %s\n", __func__, m->s_name));
    231  1.48    rillig 		} while (m->s_bitfield && m->s_name == unnamed);
    232   1.1       cgd 		istk->i_subt = m->s_type;
    233   1.1       cgd 	}
    234   1.1       cgd }
    235   1.1       cgd 
    236  1.36    rillig /*
    237  1.36    rillig  * Take all entries, including the first which requires a closing brace,
    238  1.36    rillig  * from the stack.
    239  1.36    rillig  */
    240  1.36    rillig static void
    241  1.36    rillig initstack_pop_brace(void)
    242  1.36    rillig {
    243  1.36    rillig 	int brace;
    244  1.36    rillig 
    245  1.36    rillig 	DPRINTF(("%s\n", __func__));
    246  1.36    rillig 	do {
    247  1.36    rillig 		brace = initstk->i_brace;
    248  1.38    rillig 		DPRINTF(("%s: loop brace=%d\n", __func__, brace));
    249  1.36    rillig 		initstack_pop_item();
    250  1.36    rillig 	} while (!brace);
    251  1.36    rillig 	DPRINTF(("%s: done\n", __func__));
    252  1.36    rillig }
    253  1.36    rillig 
    254  1.36    rillig /*
    255  1.36    rillig  * Take all entries which cannot be used for further initializers from the
    256  1.36    rillig  * stack, but do this only if they do not require a closing brace.
    257  1.36    rillig  */
    258   1.1       cgd static void
    259  1.36    rillig initstack_pop_nobrace(void)
    260   1.1       cgd {
    261   1.7     lukem 
    262  1.36    rillig 	DPRINTF(("%s\n", __func__));
    263  1.43    rillig 	while (!initstk->i_brace && initstk->i_remaining == 0 &&
    264  1.43    rillig 	       !initstk->i_nolimit)
    265  1.36    rillig 		initstack_pop_item();
    266  1.36    rillig 	DPRINTF(("%s: done\n", __func__));
    267   1.1       cgd }
    268   1.1       cgd 
    269   1.1       cgd static void
    270  1.35    rillig initstack_push(void)
    271   1.1       cgd {
    272  1.12  christos #ifdef DEBUG
    273  1.12  christos 	char	buf[64];
    274  1.12  christos #endif
    275  1.22        ad 	istk_t	*istk, *inxt;
    276   1.1       cgd 	int	cnt;
    277   1.1       cgd 	sym_t	*m;
    278   1.1       cgd 
    279   1.1       cgd 	istk = initstk;
    280   1.1       cgd 
    281   1.1       cgd 	/* Extend an incomplete array type by one element */
    282  1.43    rillig 	if (istk->i_remaining == 0) {
    283  1.30    rillig 		DPRINTF(("%s(extend) %s\n", __func__,
    284  1.30    rillig 		    tyname(buf, sizeof(buf), istk->i_type)));
    285   1.1       cgd 		/*
    286   1.1       cgd 		 * Inside of other aggregate types must not be an incomplete
    287   1.1       cgd 		 * type.
    288   1.1       cgd 		 */
    289  1.46    rillig 		lint_assert(istk->i_next->i_next == NULL);
    290  1.43    rillig 		istk->i_remaining = 1;
    291  1.44    rillig 		lint_assert(istk->i_type->t_tspec == ARRAY);
    292   1.1       cgd 		istk->i_type->t_dim++;
    293  1.32    rillig 		setcomplete(istk->i_type, 1);
    294   1.1       cgd 	}
    295   1.1       cgd 
    296  1.44    rillig 	lint_assert(istk->i_remaining > 0);
    297  1.44    rillig 	lint_assert(istk->i_type == NULL ||
    298  1.44    rillig 	    !tspec_is_scalar(istk->i_type->t_tspec));
    299   1.1       cgd 
    300   1.1       cgd 	initstk = xcalloc(1, sizeof (istk_t));
    301  1.46    rillig 	initstk->i_next = istk;
    302   1.1       cgd 	initstk->i_type = istk->i_subt;
    303  1.44    rillig 	lint_assert(initstk->i_type->t_tspec != FUNC);
    304   1.1       cgd 
    305  1.12  christos again:
    306   1.1       cgd 	istk = initstk;
    307   1.1       cgd 
    308  1.26  christos 	DPRINTF(("%s(%s)\n", __func__, tyname(buf, sizeof(buf), istk->i_type)));
    309   1.1       cgd 	switch (istk->i_type->t_tspec) {
    310   1.1       cgd 	case ARRAY:
    311  1.16  christos 		if (namedmem) {
    312  1.26  christos 			DPRINTF(("%s: ARRAY %s brace=%d\n", __func__,
    313  1.20  christos 			    namedmem->n_name, istk->i_brace));
    314  1.19  christos 			goto pop;
    315  1.46    rillig 		} else if (istk->i_next->i_namedmem) {
    316  1.26  christos 			istk->i_brace = 1;
    317  1.26  christos 			DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__,
    318  1.46    rillig 			    istk->i_brace, istk->i_next->i_namedmem));
    319  1.20  christos 		}
    320  1.20  christos 
    321  1.46    rillig 		if (incompl(istk->i_type) && istk->i_next->i_next != NULL) {
    322   1.1       cgd 			/* initialisation of an incomplete type */
    323   1.1       cgd 			error(175);
    324   1.1       cgd 			initerr = 1;
    325   1.1       cgd 			return;
    326   1.1       cgd 		}
    327   1.1       cgd 		istk->i_subt = istk->i_type->t_subt;
    328   1.1       cgd 		istk->i_nolimit = incompl(istk->i_type);
    329  1.43    rillig 		istk->i_remaining = istk->i_type->t_dim;
    330  1.26  christos 		DPRINTF(("%s: elements array %s[%d] %s\n", __func__,
    331  1.43    rillig 		    tyname(buf, sizeof(buf), istk->i_subt), istk->i_remaining,
    332  1.16  christos 		    namedmem ? namedmem->n_name : "*none*"));
    333   1.1       cgd 		break;
    334   1.1       cgd 	case UNION:
    335   1.1       cgd 		if (tflag)
    336   1.1       cgd 			/* initialisation of union is illegal in trad. C */
    337   1.1       cgd 			warning(238);
    338   1.1       cgd 		/* FALLTHROUGH */
    339   1.1       cgd 	case STRUCT:
    340   1.1       cgd 		if (incompl(istk->i_type)) {
    341   1.1       cgd 			/* initialisation of an incomplete type */
    342   1.1       cgd 			error(175);
    343   1.1       cgd 			initerr = 1;
    344   1.1       cgd 			return;
    345   1.1       cgd 		}
    346   1.1       cgd 		cnt = 0;
    347  1.40    rillig 		DPRINTF(("%s: lookup type=%s, name=%s named=%d\n", __func__,
    348  1.16  christos 		    tyname(buf, sizeof(buf), istk->i_type),
    349  1.26  christos 		    namedmem ? namedmem->n_name : "*none*", istk->i_namedmem));
    350  1.45    rillig 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) {
    351  1.48    rillig 			if (m->s_bitfield && m->s_name == unnamed)
    352   1.1       cgd 				continue;
    353  1.12  christos 			if (namedmem != NULL) {
    354  1.40    rillig 				DPRINTF(("%s: named lhs.member=%s, rhs.member=%s\n",
    355  1.26  christos 				    __func__, m->s_name, namedmem->n_name));
    356  1.12  christos 				if (strcmp(m->s_name, namedmem->n_name) == 0) {
    357  1.12  christos 					cnt++;
    358  1.12  christos 					break;
    359  1.12  christos 				} else
    360  1.12  christos 					continue;
    361  1.12  christos 			}
    362   1.1       cgd 			if (++cnt == 1) {
    363   1.1       cgd 				istk->i_mem = m;
    364   1.1       cgd 				istk->i_subt = m->s_type;
    365   1.1       cgd 			}
    366   1.1       cgd 		}
    367  1.12  christos 		if (namedmem != NULL) {
    368  1.12  christos 			if (m == NULL) {
    369  1.40    rillig 				DPRINTF(("%s: pop struct\n", __func__));
    370  1.19  christos 				goto pop;
    371  1.28    rillig 			}
    372  1.26  christos 			istk->i_mem = m;
    373  1.26  christos 			istk->i_subt = m->s_type;
    374  1.19  christos 			istk->i_namedmem = 1;
    375  1.40    rillig 			DPRINTF(("%s: named name=%s\n", __func__,
    376  1.26  christos 			    namedmem->n_name));
    377  1.34    rillig 			pop_member();
    378  1.12  christos 			cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
    379  1.12  christos 		}
    380  1.26  christos 		istk->i_brace = 1;
    381  1.40    rillig 		DPRINTF(("%s: unnamed type=%s, brace=%d\n", __func__,
    382  1.26  christos 		    tyname(buf, sizeof(buf),
    383  1.30    rillig 			istk->i_type ? istk->i_type : istk->i_subt),
    384  1.26  christos 		    istk->i_brace));
    385   1.1       cgd 		if (cnt == 0) {
    386   1.1       cgd 			/* cannot init. struct/union with no named member */
    387   1.1       cgd 			error(179);
    388   1.1       cgd 			initerr = 1;
    389   1.1       cgd 			return;
    390   1.1       cgd 		}
    391  1.43    rillig 		istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
    392   1.1       cgd 		break;
    393   1.1       cgd 	default:
    394  1.12  christos 		if (namedmem) {
    395  1.40    rillig 			DPRINTF(("%s: pop\n", __func__));
    396  1.19  christos 	pop:
    397  1.46    rillig 			inxt = initstk->i_next;
    398  1.12  christos 			free(istk);
    399  1.22        ad 			initstk = inxt;
    400  1.12  christos 			goto again;
    401  1.12  christos 		}
    402  1.43    rillig 		istk->i_remaining = 1;
    403   1.1       cgd 		break;
    404   1.1       cgd 	}
    405   1.1       cgd }
    406   1.1       cgd 
    407   1.1       cgd static void
    408  1.35    rillig initstack_check_too_many(void)
    409   1.1       cgd {
    410   1.1       cgd 	istk_t	*istk;
    411   1.1       cgd 
    412   1.1       cgd 	istk = initstk;
    413   1.1       cgd 
    414   1.1       cgd 	/*
    415   1.1       cgd 	 * If a closing brace is expected we have at least one initializer
    416   1.1       cgd 	 * too much.
    417   1.1       cgd 	 */
    418  1.43    rillig 	if (istk->i_remaining == 0 && !istk->i_nolimit && !istk->i_namedmem) {
    419   1.1       cgd 		switch (istk->i_type->t_tspec) {
    420   1.1       cgd 		case ARRAY:
    421  1.49    rillig 			/* too many array initializers, expected %d */
    422  1.23  christos 			error(173, istk->i_type->t_dim);
    423   1.1       cgd 			break;
    424   1.1       cgd 		case STRUCT:
    425   1.1       cgd 		case UNION:
    426   1.1       cgd 			/* too many struct/union initializers */
    427   1.1       cgd 			error(172);
    428   1.1       cgd 			break;
    429   1.1       cgd 		default:
    430   1.1       cgd 			/* too many initializers */
    431   1.1       cgd 			error(174);
    432   1.1       cgd 			break;
    433   1.1       cgd 		}
    434   1.1       cgd 		initerr = 1;
    435   1.1       cgd 	}
    436   1.1       cgd }
    437   1.1       cgd 
    438   1.1       cgd static void
    439  1.37    rillig initstack_next_brace(void)
    440   1.1       cgd {
    441  1.12  christos 	char buf[64];
    442   1.7     lukem 
    443  1.37    rillig 	DPRINTF(("%s\n", __func__));
    444  1.37    rillig 	if (initstk->i_type != NULL &&
    445  1.37    rillig 	    tspec_is_scalar(initstk->i_type->t_tspec)) {
    446  1.49    rillig 		/* invalid initializer type %s */
    447  1.37    rillig 		error(176, tyname(buf, sizeof(buf), initstk->i_type));
    448  1.37    rillig 		initerr = 1;
    449  1.37    rillig 	}
    450  1.37    rillig 	if (!initerr)
    451  1.37    rillig 		initstack_check_too_many();
    452  1.37    rillig 	if (!initerr)
    453  1.37    rillig 		initstack_push();
    454  1.37    rillig 	if (!initerr) {
    455  1.37    rillig 		initstk->i_brace = 1;
    456  1.40    rillig 		DPRINTF(("%s: %p %s\n", __func__,
    457  1.37    rillig 		    namedmem,
    458  1.37    rillig 		    tyname(buf, sizeof(buf),
    459  1.40    rillig 			initstk->i_type ? initstk->i_type : initstk->i_subt)));
    460  1.37    rillig 	}
    461  1.37    rillig }
    462  1.37    rillig 
    463  1.37    rillig static void
    464  1.37    rillig initstack_next_nobrace(void)
    465  1.37    rillig {
    466  1.37    rillig 
    467  1.37    rillig 	DPRINTF(("%s\n", __func__));
    468  1.37    rillig 	if (initstk->i_type == NULL &&
    469  1.37    rillig 	    !tspec_is_scalar(initstk->i_subt->t_tspec)) {
    470  1.37    rillig 		/* {}-enclosed initializer required */
    471  1.37    rillig 		error(181);
    472  1.37    rillig 	}
    473  1.37    rillig 
    474  1.37    rillig 	/* Make sure an entry with a scalar type is at the top of the stack. */
    475  1.37    rillig 	if (!initerr)
    476  1.37    rillig 		initstack_check_too_many();
    477  1.42    rillig 	while (!initerr) {
    478  1.42    rillig 		if ((initstk->i_type != NULL &&
    479  1.42    rillig 		     tspec_is_scalar(initstk->i_type->t_tspec)))
    480  1.42    rillig 			break;
    481  1.42    rillig 		initstack_push();
    482   1.1       cgd 	}
    483   1.1       cgd }
    484   1.1       cgd 
    485   1.1       cgd void
    486  1.34    rillig init_lbrace(void)
    487   1.1       cgd {
    488  1.26  christos 	DPRINTF(("%s\n", __func__));
    489   1.7     lukem 
    490   1.1       cgd 	if (initerr)
    491   1.1       cgd 		return;
    492   1.1       cgd 
    493   1.1       cgd 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
    494  1.46    rillig 	    initstk->i_next == NULL) {
    495  1.29    rillig 		if (tflag && !tspec_is_scalar(initstk->i_subt->t_tspec))
    496  1.50    rillig 			/* no automatic aggregate initialization in trad. C */
    497   1.1       cgd 			warning(188);
    498   1.1       cgd 	}
    499   1.1       cgd 
    500   1.1       cgd 	/*
    501   1.1       cgd 	 * Remove all entries which cannot be used for further initializers
    502   1.1       cgd 	 * and do not expect a closing brace.
    503   1.1       cgd 	 */
    504  1.36    rillig 	initstack_pop_nobrace();
    505   1.1       cgd 
    506  1.37    rillig 	initstack_next_brace();
    507   1.1       cgd }
    508   1.1       cgd 
    509   1.1       cgd void
    510  1.34    rillig init_rbrace(void)
    511   1.1       cgd {
    512  1.26  christos 	DPRINTF(("%s\n", __func__));
    513   1.7     lukem 
    514   1.1       cgd 	if (initerr)
    515   1.1       cgd 		return;
    516   1.1       cgd 
    517  1.36    rillig 	initstack_pop_brace();
    518   1.1       cgd }
    519   1.1       cgd 
    520   1.1       cgd void
    521   1.7     lukem mkinit(tnode_t *tn)
    522   1.1       cgd {
    523   1.1       cgd 	ptrdiff_t offs;
    524   1.1       cgd 	sym_t	*sym;
    525   1.1       cgd 	tspec_t	lt, rt;
    526   1.1       cgd 	tnode_t	*ln;
    527   1.1       cgd 	struct	mbl *tmem;
    528   1.1       cgd 	scl_t	sc;
    529  1.12  christos #ifdef DEBUG
    530  1.19  christos 	char	buf[64], sbuf[64];
    531  1.12  christos #endif
    532   1.1       cgd 
    533  1.40    rillig 	DPRINTF(("%s: type=%s, value=%s\n", __func__,
    534  1.34    rillig 	    tyname(buf, sizeof(buf), tn->tn_type),
    535  1.34    rillig 	    print_tnode(sbuf, sizeof(sbuf), tn)));
    536   1.1       cgd 	if (initerr || tn == NULL)
    537  1.25  christos 		return;
    538   1.1       cgd 
    539   1.1       cgd 	sc = initsym->s_scl;
    540   1.1       cgd 
    541   1.1       cgd 	/*
    542  1.13  christos 	 * Do not test for automatic aggregate initialisation. If the
    543   1.9       wiz 	 * initializer starts with a brace we have the warning already.
    544   1.1       cgd 	 * If not, an error will be printed that the initializer must
    545   1.1       cgd 	 * be enclosed by braces.
    546   1.1       cgd 	 */
    547   1.1       cgd 
    548   1.1       cgd 	/*
    549   1.1       cgd 	 * Local initialisation of non-array-types with only one expression
    550   1.1       cgd 	 * without braces is done by ASSIGN
    551   1.1       cgd 	 */
    552   1.1       cgd 	if ((sc == AUTO || sc == REG) &&
    553  1.46    rillig 	    initsym->s_type->t_tspec != ARRAY && initstk->i_next == NULL) {
    554   1.1       cgd 		ln = getnnode(initsym, 0);
    555   1.1       cgd 		ln->tn_type = tduptyp(ln->tn_type);
    556   1.1       cgd 		ln->tn_type->t_const = 0;
    557   1.1       cgd 		tn = build(ASSIGN, ln, tn);
    558  1.25  christos 		expr(tn, 0, 0, 0);
    559  1.25  christos 		return;
    560   1.1       cgd 	}
    561   1.1       cgd 
    562   1.1       cgd 	/*
    563   1.1       cgd 	 * Remove all entries which cannot be used for further initializers
    564   1.1       cgd 	 * and do not require a closing brace.
    565   1.1       cgd 	 */
    566  1.36    rillig 	initstack_pop_nobrace();
    567   1.1       cgd 
    568  1.35    rillig 	/* Initialisations by strings are done in initstack_string(). */
    569  1.35    rillig 	if (initstack_string(tn))
    570  1.25  christos 		return;
    571   1.1       cgd 
    572  1.37    rillig 	initstack_next_nobrace();
    573   1.1       cgd 	if (initerr || tn == NULL)
    574  1.25  christos 		return;
    575   1.1       cgd 
    576  1.43    rillig 	initstk->i_remaining--;
    577  1.43    rillig 	DPRINTF(("%s: remaining=%d tn=%p\n", __func__,
    578  1.43    rillig 	    initstk->i_remaining, tn));
    579   1.1       cgd 	/* Create a temporary node for the left side. */
    580   1.1       cgd 	ln = tgetblk(sizeof (tnode_t));
    581   1.1       cgd 	ln->tn_op = NAME;
    582   1.1       cgd 	ln->tn_type = tduptyp(initstk->i_type);
    583   1.1       cgd 	ln->tn_type->t_const = 0;
    584   1.1       cgd 	ln->tn_lvalue = 1;
    585   1.1       cgd 	ln->tn_sym = initsym;		/* better than nothing */
    586   1.1       cgd 
    587   1.1       cgd 	tn = cconv(tn);
    588   1.1       cgd 
    589   1.1       cgd 	lt = ln->tn_type->t_tspec;
    590   1.1       cgd 	rt = tn->tn_type->t_tspec;
    591   1.1       cgd 
    592  1.44    rillig 	lint_assert(tspec_is_scalar(lt));
    593   1.1       cgd 
    594   1.1       cgd 	if (!typeok(INIT, 0, ln, tn))
    595  1.25  christos 		return;
    596   1.1       cgd 
    597   1.1       cgd 	/*
    598  1.38    rillig 	 * Store the tree memory. This is necessary because otherwise
    599   1.1       cgd 	 * expr() would free it.
    600   1.1       cgd 	 */
    601   1.1       cgd 	tmem = tsave();
    602  1.15  christos 	expr(tn, 1, 0, 1);
    603   1.1       cgd 	trestor(tmem);
    604   1.7     lukem 
    605  1.29    rillig 	if (tspec_is_int(lt) && ln->tn_type->t_isfield && !tspec_is_int(rt)) {
    606   1.1       cgd 		/*
    607   1.1       cgd 		 * Bit-fields can be initialized in trad. C only by integer
    608   1.1       cgd 		 * constants.
    609   1.1       cgd 		 */
    610   1.1       cgd 		if (tflag)
    611   1.1       cgd 			/* bit-field initialisation is illegal in trad. C */
    612   1.1       cgd 			warning(186);
    613   1.1       cgd 	}
    614   1.1       cgd 
    615   1.1       cgd 	if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
    616   1.1       cgd 		tn = convert(INIT, 0, initstk->i_type, tn);
    617   1.1       cgd 
    618   1.1       cgd 	if (tn != NULL && tn->tn_op != CON) {
    619   1.1       cgd 		sym = NULL;
    620   1.1       cgd 		offs = 0;
    621   1.1       cgd 		if (conaddr(tn, &sym, &offs) == -1) {
    622   1.3       jpo 			if (sc == AUTO || sc == REG) {
    623   1.1       cgd 				/* non-constant initializer */
    624  1.24  christos 				(void)c99ism(177);
    625   1.1       cgd 			} else {
    626   1.1       cgd 				/* non-constant initializer */
    627   1.1       cgd 				error(177);
    628   1.1       cgd 			}
    629   1.1       cgd 		}
    630   1.1       cgd 	}
    631   1.1       cgd }
    632   1.1       cgd 
    633   1.1       cgd 
    634   1.1       cgd static int
    635  1.35    rillig initstack_string(tnode_t *tn)
    636   1.1       cgd {
    637   1.1       cgd 	tspec_t	t;
    638   1.1       cgd 	istk_t	*istk;
    639   1.1       cgd 	int	len;
    640   1.1       cgd 	strg_t	*strg;
    641   1.1       cgd 
    642   1.1       cgd 	if (tn->tn_op != STRING)
    643  1.33    rillig 		return 0;
    644   1.1       cgd 
    645   1.1       cgd 	istk = initstk;
    646  1.47    rillig 	strg = tn->tn_string;
    647   1.1       cgd 
    648   1.1       cgd 	/*
    649   1.1       cgd 	 * Check if we have an array type which can be initialized by
    650   1.1       cgd 	 * the string.
    651   1.1       cgd 	 */
    652  1.12  christos 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
    653  1.26  christos 		DPRINTF(("%s: subt array\n", __func__));
    654   1.1       cgd 		t = istk->i_subt->t_subt->t_tspec;
    655   1.1       cgd 		if (!((strg->st_tspec == CHAR &&
    656   1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    657   1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    658  1.33    rillig 			return 0;
    659   1.1       cgd 		}
    660   1.1       cgd 		/* Put the array at top of stack */
    661  1.35    rillig 		initstack_push();
    662   1.1       cgd 		istk = initstk;
    663   1.1       cgd 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
    664  1.26  christos 		DPRINTF(("%s: type array\n", __func__));
    665   1.1       cgd 		t = istk->i_type->t_subt->t_tspec;
    666   1.1       cgd 		if (!((strg->st_tspec == CHAR &&
    667   1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    668   1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    669  1.33    rillig 			return 0;
    670   1.1       cgd 		}
    671   1.1       cgd 		/*
    672   1.1       cgd 		 * If the array is already partly initialized, we are
    673   1.1       cgd 		 * wrong here.
    674   1.1       cgd 		 */
    675  1.43    rillig 		if (istk->i_remaining != istk->i_type->t_dim)
    676  1.33    rillig 			return 0;
    677   1.1       cgd 	} else {
    678  1.33    rillig 		return 0;
    679   1.1       cgd 	}
    680   1.1       cgd 
    681   1.1       cgd 	/* Get length without trailing NUL character. */
    682   1.1       cgd 	len = strg->st_len;
    683   1.1       cgd 
    684   1.1       cgd 	if (istk->i_nolimit) {
    685   1.1       cgd 		istk->i_nolimit = 0;
    686   1.1       cgd 		istk->i_type->t_dim = len + 1;
    687  1.32    rillig 		setcomplete(istk->i_type, 1);
    688   1.1       cgd 	} else {
    689   1.1       cgd 		if (istk->i_type->t_dim < len) {
    690   1.1       cgd 			/* non-null byte ignored in string initializer */
    691   1.1       cgd 			warning(187);
    692   1.1       cgd 		}
    693   1.1       cgd 	}
    694   1.1       cgd 
    695   1.1       cgd 	/* In every case the array is initialized completely. */
    696  1.43    rillig 	istk->i_remaining = 0;
    697   1.1       cgd 
    698  1.33    rillig 	return 1;
    699   1.1       cgd }
    700