Home | History | Annotate | Line # | Download | only in lint1
init.c revision 1.17
      1  1.17   thorpej /*	$NetBSD: init.c,v 1.17 2002/12/06 03:27:39 thorpej 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.5  christos #include <sys/cdefs.h>
     35  1.10        tv #if defined(__RCSID) && !defined(lint)
     36  1.17   thorpej __RCSID("$NetBSD: init.c,v 1.17 2002/12/06 03:27:39 thorpej Exp $");
     37   1.1       cgd #endif
     38   1.1       cgd 
     39   1.1       cgd #include <stdlib.h>
     40  1.17   thorpej #include <string.h>
     41  1.14  christos #include <ctype.h>
     42   1.1       cgd 
     43   1.1       cgd #include "lint1.h"
     44   1.1       cgd 
     45   1.1       cgd /*
     46   1.8       wiz  * initerr is set as soon as a fatal error occurred in an initialisation.
     47   1.1       cgd  * The effect is that the rest of the initialisation is ignored (parsed
     48   1.1       cgd  * by yacc, expression trees built, but no initialisation takes place).
     49   1.1       cgd  */
     50   1.1       cgd int	initerr;
     51   1.1       cgd 
     52   1.1       cgd /* Pointer to the symbol which is to be initialized. */
     53   1.1       cgd sym_t	*initsym;
     54   1.1       cgd 
     55   1.1       cgd /* Points to the top element of the initialisation stack. */
     56   1.1       cgd istk_t	*initstk;
     57   1.1       cgd 
     58  1.12  christos typedef struct namlist {
     59  1.12  christos 	const char *n_name;
     60  1.12  christos 	struct namlist *n_prev;
     61  1.12  christos 	struct namlist *n_next;
     62  1.12  christos } namlist_t;
     63  1.12  christos 
     64  1.12  christos /* Points to a c9x named member; */
     65  1.12  christos namlist_t	*namedmem = NULL;
     66  1.12  christos 
     67   1.1       cgd 
     68   1.7     lukem static	void	popi2(void);
     69   1.7     lukem static	void	popinit(int);
     70   1.7     lukem static	void	pushinit(void);
     71   1.7     lukem static	void	testinit(void);
     72   1.7     lukem static	void	nextinit(int);
     73   1.7     lukem static	int	strginit(tnode_t *);
     74  1.12  christos static	void	memberpop(void);
     75  1.12  christos 
     76  1.12  christos #ifndef DEBUG
     77  1.12  christos #define DPRINTF(a)
     78  1.12  christos #else
     79  1.12  christos #define DPRINTF(a) printf a
     80  1.12  christos #endif
     81  1.12  christos 
     82  1.12  christos void
     83  1.12  christos memberpush(sb)
     84  1.12  christos 	sbuf_t *sb;
     85  1.12  christos {
     86  1.12  christos 	namlist_t *nam = xcalloc(1, sizeof (namlist_t));
     87  1.12  christos 	nam->n_name = sb->sb_name;
     88  1.12  christos 	DPRINTF(("memberpush = %s\n", nam->n_name));
     89  1.12  christos 	if (namedmem == NULL) {
     90  1.12  christos 		nam->n_prev = nam->n_next = nam;
     91  1.12  christos 		namedmem = nam;
     92  1.12  christos 	} else {
     93  1.12  christos 		namedmem->n_prev->n_next = nam;
     94  1.12  christos 		nam->n_prev = namedmem->n_prev;
     95  1.12  christos 		nam->n_next = namedmem;
     96  1.12  christos 		namedmem->n_prev = nam;
     97  1.12  christos 	}
     98  1.12  christos #if 0
     99  1.12  christos 	nam->n_next = namedmem;
    100  1.12  christos 	namedmem = nam;
    101  1.12  christos #endif
    102  1.12  christos }
    103  1.12  christos 
    104  1.12  christos static void
    105  1.12  christos memberpop()
    106  1.12  christos {
    107  1.12  christos 	DPRINTF(("memberpop = %s\n", namedmem->n_name));
    108  1.12  christos 	if (namedmem->n_next == namedmem) {
    109  1.12  christos 		free(namedmem);
    110  1.12  christos 		namedmem = NULL;
    111  1.12  christos 	} else {
    112  1.12  christos 		namlist_t *nam = namedmem;
    113  1.12  christos 		namedmem = namedmem->n_next;
    114  1.12  christos 		free(nam);
    115  1.12  christos 	}
    116  1.12  christos #if 0
    117  1.12  christos 	namedmem = namedmem->n_next;
    118  1.12  christos 	free(nam);
    119  1.12  christos #endif
    120  1.12  christos }
    121   1.1       cgd 
    122   1.1       cgd 
    123   1.1       cgd /*
    124   1.1       cgd  * Initialize the initialisation stack by putting an entry for the variable
    125   1.1       cgd  * which is to be initialized on it.
    126   1.1       cgd  */
    127   1.1       cgd void
    128   1.7     lukem prepinit(void)
    129   1.1       cgd {
    130   1.1       cgd 	istk_t	*istk;
    131   1.1       cgd 
    132   1.1       cgd 	if (initerr)
    133   1.1       cgd 		return;
    134   1.1       cgd 
    135   1.1       cgd 	/* free memory used in last initialisation */
    136   1.1       cgd 	while ((istk = initstk) != NULL) {
    137   1.1       cgd 		initstk = istk->i_nxt;
    138   1.1       cgd 		free(istk);
    139   1.1       cgd 	}
    140   1.1       cgd 
    141   1.1       cgd 	/*
    142   1.1       cgd 	 * If the type which is to be initialized is an incomplete type,
    143   1.1       cgd 	 * it must be duplicated.
    144   1.1       cgd 	 */
    145   1.1       cgd 	if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
    146   1.1       cgd 		initsym->s_type = duptyp(initsym->s_type);
    147   1.1       cgd 
    148   1.1       cgd 	istk = initstk = xcalloc(1, sizeof (istk_t));
    149   1.1       cgd 	istk->i_subt = initsym->s_type;
    150   1.1       cgd 	istk->i_cnt = 1;
    151   1.1       cgd 
    152   1.1       cgd }
    153   1.1       cgd 
    154   1.1       cgd static void
    155   1.7     lukem popi2(void)
    156   1.1       cgd {
    157  1.12  christos #ifdef DEBUG
    158  1.12  christos 	char	buf[64];
    159  1.12  christos #endif
    160   1.1       cgd 	istk_t	*istk;
    161   1.1       cgd 	sym_t	*m;
    162   1.1       cgd 
    163   1.1       cgd 	initstk = (istk = initstk)->i_nxt;
    164   1.1       cgd 	if (initstk == NULL)
    165  1.11  christos 		LERROR("popi2()");
    166   1.1       cgd 	free(istk);
    167   1.1       cgd 
    168   1.1       cgd 	istk = initstk;
    169   1.1       cgd 
    170   1.1       cgd 	istk->i_cnt--;
    171   1.1       cgd 	if (istk->i_cnt < 0)
    172  1.11  christos 		LERROR("popi2()");
    173   1.1       cgd 
    174  1.16  christos 	DPRINTF(("popi2(): %d %s\n", istk->i_cnt,
    175  1.16  christos 	    namedmem ? namedmem->n_name : "*null*"));
    176  1.12  christos 	if (istk->i_cnt >= 0 && namedmem != NULL) {
    177  1.12  christos 		DPRINTF(("popi2(): %d %s %s\n", istk->i_cnt,
    178  1.12  christos 		    tyname(buf, sizeof(buf), istk->i_type), namedmem->n_name));
    179  1.12  christos 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
    180  1.12  christos 			if (m->s_field && m->s_name == unnamed)
    181  1.12  christos 				continue;
    182  1.12  christos 			if (strcmp(m->s_name, namedmem->n_name) == 0) {
    183  1.12  christos 				istk->i_subt = m->s_type;
    184  1.12  christos 				istk->i_cnt++;
    185  1.12  christos 				memberpop();
    186  1.12  christos 				return;
    187  1.12  christos 			}
    188  1.12  christos 		}
    189  1.12  christos 		error(101, namedmem->n_name);
    190  1.12  christos 		memberpop();
    191  1.12  christos 		istk->i_namedmem = 1;
    192  1.12  christos 		return;
    193  1.12  christos 	}
    194   1.1       cgd 	/*
    195   1.1       cgd 	 * If the removed element was a structure member, we must go
    196   1.1       cgd 	 * to the next structure member.
    197   1.1       cgd 	 */
    198  1.12  christos 	if (istk->i_cnt > 0 && istk->i_type->t_tspec == STRUCT &&
    199  1.12  christos 	    !istk->i_namedmem) {
    200   1.1       cgd 		do {
    201   1.1       cgd 			m = istk->i_mem = istk->i_mem->s_nxt;
    202   1.1       cgd 			if (m == NULL)
    203  1.11  christos 				LERROR("popi2()");
    204   1.1       cgd 		} while (m->s_field && m->s_name == unnamed);
    205   1.1       cgd 		istk->i_subt = m->s_type;
    206   1.1       cgd 	}
    207   1.1       cgd }
    208   1.1       cgd 
    209   1.1       cgd static void
    210   1.7     lukem popinit(int brace)
    211   1.1       cgd {
    212  1.12  christos 	DPRINTF(("popinit(%d)\n", brace));
    213   1.7     lukem 
    214   1.1       cgd 	if (brace) {
    215   1.1       cgd 		/*
    216   1.1       cgd 		 * Take all entries, including the first which requires
    217   1.1       cgd 		 * a closing brace, from the stack.
    218   1.1       cgd 		 */
    219   1.1       cgd 		do {
    220   1.1       cgd 			brace = initstk->i_brace;
    221   1.1       cgd 			popi2();
    222   1.1       cgd 		} while (!brace);
    223   1.1       cgd 	} else {
    224   1.1       cgd 		/*
    225   1.1       cgd 		 * Take all entries which cannot be used for further
    226   1.1       cgd 		 * initializers from the stack, but do this only if
    227   1.1       cgd 		 * they do not require a closing brace.
    228   1.1       cgd 		 */
    229   1.1       cgd 		while (!initstk->i_brace &&
    230   1.1       cgd 		       initstk->i_cnt == 0 && !initstk->i_nolimit) {
    231   1.1       cgd 			popi2();
    232   1.1       cgd 		}
    233   1.1       cgd 	}
    234   1.1       cgd }
    235   1.1       cgd 
    236   1.1       cgd static void
    237   1.7     lukem pushinit(void)
    238   1.1       cgd {
    239  1.12  christos #ifdef DEBUG
    240  1.12  christos 	char	buf[64];
    241  1.12  christos #endif
    242   1.1       cgd 	istk_t	*istk;
    243   1.1       cgd 	int	cnt;
    244   1.1       cgd 	sym_t	*m;
    245   1.1       cgd 
    246   1.1       cgd 	istk = initstk;
    247   1.1       cgd 
    248   1.1       cgd 	/* Extend an incomplete array type by one element */
    249   1.1       cgd 	if (istk->i_cnt == 0) {
    250  1.12  christos 		DPRINTF(("pushinit(extend) %s\n", tyname(buf, sizeof(buf),
    251  1.12  christos 		    istk->i_type)));
    252   1.1       cgd 		/*
    253   1.1       cgd 		 * Inside of other aggregate types must not be an incomplete
    254   1.1       cgd 		 * type.
    255   1.1       cgd 		 */
    256   1.1       cgd 		if (istk->i_nxt->i_nxt != NULL)
    257  1.11  christos 			LERROR("pushinit()");
    258   1.1       cgd 		istk->i_cnt = 1;
    259   1.1       cgd 		if (istk->i_type->t_tspec != ARRAY)
    260  1.11  christos 			LERROR("pushinit()");
    261   1.1       cgd 		istk->i_type->t_dim++;
    262   1.1       cgd 		/* from now its an complete type */
    263   1.1       cgd 		setcompl(istk->i_type, 0);
    264   1.1       cgd 	}
    265   1.1       cgd 
    266   1.1       cgd 	if (istk->i_cnt <= 0)
    267  1.11  christos 		LERROR("pushinit()");
    268   1.1       cgd 	if (istk->i_type != NULL && issclt(istk->i_type->t_tspec))
    269  1.11  christos 		LERROR("pushinit()");
    270   1.1       cgd 
    271   1.1       cgd 	initstk = xcalloc(1, sizeof (istk_t));
    272   1.1       cgd 	initstk->i_nxt = istk;
    273   1.1       cgd 	initstk->i_type = istk->i_subt;
    274   1.1       cgd 	if (initstk->i_type->t_tspec == FUNC)
    275  1.11  christos 		LERROR("pushinit()");
    276   1.1       cgd 
    277  1.12  christos again:
    278   1.1       cgd 	istk = initstk;
    279   1.1       cgd 
    280  1.12  christos 	DPRINTF(("pushinit(%s)\n", tyname(buf, sizeof(buf), istk->i_type)));
    281   1.1       cgd 	switch (istk->i_type->t_tspec) {
    282   1.1       cgd 	case ARRAY:
    283  1.16  christos 		if (namedmem) {
    284  1.16  christos 			DPRINTF(("pushinit ARRAY %s\n", namedmem->n_name));
    285  1.16  christos 			free(istk);
    286  1.16  christos 			initstk = initstk->i_nxt;
    287  1.16  christos 			goto again;
    288  1.16  christos 		}
    289   1.1       cgd 		if (incompl(istk->i_type) && istk->i_nxt->i_nxt != NULL) {
    290   1.1       cgd 			/* initialisation of an incomplete type */
    291   1.1       cgd 			error(175);
    292   1.1       cgd 			initerr = 1;
    293   1.1       cgd 			return;
    294   1.1       cgd 		}
    295   1.1       cgd 		istk->i_subt = istk->i_type->t_subt;
    296   1.1       cgd 		istk->i_nolimit = incompl(istk->i_type);
    297   1.1       cgd 		istk->i_cnt = istk->i_type->t_dim;
    298  1.16  christos 		DPRINTF(("elements array %s[%d] %s\n",
    299  1.16  christos 		    tyname(buf, sizeof(buf), istk->i_subt), istk->i_cnt,
    300  1.16  christos 		    namedmem ? namedmem->n_name : "*none*"));
    301   1.1       cgd 		break;
    302   1.1       cgd 	case UNION:
    303   1.1       cgd 		if (tflag)
    304   1.1       cgd 			/* initialisation of union is illegal in trad. C */
    305   1.1       cgd 			warning(238);
    306   1.1       cgd 		/* FALLTHROUGH */
    307   1.1       cgd 	case STRUCT:
    308   1.1       cgd 		if (incompl(istk->i_type)) {
    309   1.1       cgd 			/* initialisation of an incomplete type */
    310   1.1       cgd 			error(175);
    311   1.1       cgd 			initerr = 1;
    312   1.1       cgd 			return;
    313   1.1       cgd 		}
    314   1.1       cgd 		cnt = 0;
    315  1.16  christos 		DPRINTF(("2. member lookup %s %s\n",
    316  1.16  christos 		    tyname(buf, sizeof(buf), istk->i_type),
    317  1.16  christos 		    namedmem ? namedmem->n_name : "*none*"));
    318   1.1       cgd 		for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_nxt) {
    319   1.1       cgd 			if (m->s_field && m->s_name == unnamed)
    320   1.1       cgd 				continue;
    321  1.12  christos 			if (namedmem != NULL) {
    322  1.12  christos 				DPRINTF(("pushinit():[member:%s, looking:%s]\n",
    323  1.12  christos 				    m->s_name, namedmem->n_name));
    324  1.12  christos 				if (strcmp(m->s_name, namedmem->n_name) == 0) {
    325  1.12  christos 					cnt++;
    326  1.12  christos 					break;
    327  1.12  christos 				} else
    328  1.12  christos 					continue;
    329  1.12  christos 			}
    330   1.1       cgd 			if (++cnt == 1) {
    331   1.1       cgd 				istk->i_mem = m;
    332   1.1       cgd 				istk->i_subt = m->s_type;
    333   1.1       cgd 			}
    334   1.1       cgd 		}
    335  1.12  christos 		if (namedmem != NULL) {
    336  1.12  christos 			istk->i_namedmem = 1;
    337  1.12  christos 			if (m == NULL) {
    338  1.12  christos 				error(101, namedmem->n_name);
    339  1.12  christos 				initerr = 1;
    340  1.12  christos 			} else {
    341  1.12  christos 				istk->i_mem = m;
    342  1.12  christos 				istk->i_subt = m->s_type;
    343  1.12  christos 			}
    344  1.12  christos 			memberpop();
    345  1.12  christos 			cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
    346  1.12  christos 		}
    347   1.1       cgd 		if (cnt == 0) {
    348   1.1       cgd 			/* cannot init. struct/union with no named member */
    349   1.1       cgd 			error(179);
    350   1.1       cgd 			initerr = 1;
    351   1.1       cgd 			return;
    352   1.1       cgd 		}
    353   1.1       cgd 		istk->i_cnt = istk->i_type->t_tspec == STRUCT ? cnt : 1;
    354   1.1       cgd 		break;
    355   1.1       cgd 	default:
    356  1.12  christos 		if (namedmem) {
    357  1.12  christos 			DPRINTF(("pushinit(): pop\n"));
    358  1.12  christos 			free(istk);
    359  1.12  christos 			initstk = initstk->i_nxt;
    360  1.12  christos 			goto again;
    361  1.12  christos 		}
    362   1.1       cgd 		istk->i_cnt = 1;
    363   1.1       cgd 		break;
    364   1.1       cgd 	}
    365   1.1       cgd }
    366   1.1       cgd 
    367   1.1       cgd static void
    368   1.7     lukem testinit(void)
    369   1.1       cgd {
    370   1.1       cgd 	istk_t	*istk;
    371   1.1       cgd 
    372   1.1       cgd 	istk = initstk;
    373   1.1       cgd 
    374   1.1       cgd 	/*
    375   1.1       cgd 	 * If a closing brace is expected we have at least one initializer
    376   1.1       cgd 	 * too much.
    377   1.1       cgd 	 */
    378  1.12  christos 	if (istk->i_cnt == 0 && !istk->i_nolimit && !istk->i_namedmem) {
    379   1.1       cgd 		switch (istk->i_type->t_tspec) {
    380   1.1       cgd 		case ARRAY:
    381   1.1       cgd 			/* too many array initializers */
    382   1.1       cgd 			error(173);
    383   1.1       cgd 			break;
    384   1.1       cgd 		case STRUCT:
    385   1.1       cgd 		case UNION:
    386   1.1       cgd 			/* too many struct/union initializers */
    387   1.1       cgd 			error(172);
    388   1.1       cgd 			break;
    389   1.1       cgd 		default:
    390   1.1       cgd 			/* too many initializers */
    391   1.1       cgd 			error(174);
    392   1.1       cgd 			break;
    393   1.1       cgd 		}
    394   1.1       cgd 		initerr = 1;
    395   1.1       cgd 	}
    396   1.1       cgd }
    397   1.1       cgd 
    398   1.1       cgd static void
    399   1.7     lukem nextinit(int brace)
    400   1.1       cgd {
    401  1.12  christos 	char buf[64];
    402   1.7     lukem 
    403  1.12  christos 	DPRINTF(("nextinit(%d)\n", brace));
    404   1.1       cgd 	if (!brace) {
    405   1.1       cgd 		if (initstk->i_type == NULL &&
    406   1.1       cgd 		    !issclt(initstk->i_subt->t_tspec)) {
    407   1.1       cgd 			/* {}-enclosed initializer required */
    408   1.1       cgd 			error(181);
    409   1.1       cgd 		}
    410   1.1       cgd 		/*
    411   1.1       cgd 		 * Make sure an entry with a scalar type is at the top
    412   1.1       cgd 		 * of the stack.
    413   1.1       cgd 		 */
    414   1.1       cgd 		if (!initerr)
    415   1.1       cgd 			testinit();
    416   1.1       cgd 		while (!initerr && (initstk->i_type == NULL ||
    417   1.1       cgd 				    !issclt(initstk->i_type->t_tspec))) {
    418   1.1       cgd 			if (!initerr)
    419   1.1       cgd 				pushinit();
    420   1.1       cgd 		}
    421   1.1       cgd 	} else {
    422   1.1       cgd 		if (initstk->i_type != NULL &&
    423   1.1       cgd 		    issclt(initstk->i_type->t_tspec)) {
    424   1.1       cgd 			/* invalid initializer */
    425  1.12  christos 			error(176, tyname(buf, sizeof(buf), initstk->i_type));
    426   1.1       cgd 			initerr = 1;
    427   1.1       cgd 		}
    428   1.1       cgd 		if (!initerr)
    429   1.1       cgd 			testinit();
    430   1.1       cgd 		if (!initerr)
    431   1.1       cgd 			pushinit();
    432   1.1       cgd 		if (!initerr)
    433   1.1       cgd 			initstk->i_brace = 1;
    434   1.1       cgd 	}
    435   1.1       cgd }
    436   1.1       cgd 
    437   1.1       cgd void
    438   1.7     lukem initlbr(void)
    439   1.1       cgd {
    440   1.7     lukem 
    441   1.1       cgd 	if (initerr)
    442   1.1       cgd 		return;
    443   1.1       cgd 
    444   1.1       cgd 	if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
    445   1.1       cgd 	    initstk->i_nxt == NULL) {
    446   1.1       cgd 		if (tflag && !issclt(initstk->i_subt->t_tspec))
    447   1.1       cgd 			/* no automatic aggregate initialization in trad. C*/
    448   1.1       cgd 			warning(188);
    449   1.1       cgd 	}
    450   1.1       cgd 
    451   1.1       cgd 	/*
    452   1.1       cgd 	 * Remove all entries which cannot be used for further initializers
    453   1.1       cgd 	 * and do not expect a closing brace.
    454   1.1       cgd 	 */
    455   1.1       cgd 	popinit(0);
    456   1.1       cgd 
    457   1.1       cgd 	nextinit(1);
    458   1.1       cgd }
    459   1.1       cgd 
    460   1.1       cgd void
    461   1.7     lukem initrbr(void)
    462   1.1       cgd {
    463   1.7     lukem 
    464   1.1       cgd 	if (initerr)
    465   1.1       cgd 		return;
    466   1.1       cgd 
    467   1.1       cgd 	popinit(1);
    468   1.1       cgd }
    469   1.1       cgd 
    470   1.1       cgd void
    471   1.7     lukem mkinit(tnode_t *tn)
    472   1.1       cgd {
    473   1.1       cgd 	ptrdiff_t offs;
    474   1.1       cgd 	sym_t	*sym;
    475   1.1       cgd 	tspec_t	lt, rt;
    476   1.1       cgd 	tnode_t	*ln;
    477   1.1       cgd 	struct	mbl *tmem;
    478   1.1       cgd 	scl_t	sc;
    479  1.12  christos #ifdef DEBUG
    480  1.12  christos 	char	buf[64];
    481  1.12  christos #endif
    482   1.1       cgd 
    483  1.12  christos 	DPRINTF(("mkinit(%s)\n", tyname(buf, sizeof(buf), tn->tn_type)));
    484   1.1       cgd 	if (initerr || tn == NULL)
    485   1.1       cgd 		goto end;
    486   1.1       cgd 
    487   1.1       cgd 	sc = initsym->s_scl;
    488   1.1       cgd 
    489   1.1       cgd 	/*
    490  1.13  christos 	 * Do not test for automatic aggregate initialisation. If the
    491   1.9       wiz 	 * initializer starts with a brace we have the warning already.
    492   1.1       cgd 	 * If not, an error will be printed that the initializer must
    493   1.1       cgd 	 * be enclosed by braces.
    494   1.1       cgd 	 */
    495   1.1       cgd 
    496   1.1       cgd 	/*
    497   1.1       cgd 	 * Local initialisation of non-array-types with only one expression
    498   1.1       cgd 	 * without braces is done by ASSIGN
    499   1.1       cgd 	 */
    500   1.1       cgd 	if ((sc == AUTO || sc == REG) &&
    501   1.1       cgd 	    initsym->s_type->t_tspec != ARRAY && initstk->i_nxt == NULL) {
    502   1.1       cgd 		ln = getnnode(initsym, 0);
    503   1.1       cgd 		ln->tn_type = tduptyp(ln->tn_type);
    504   1.1       cgd 		ln->tn_type->t_const = 0;
    505   1.1       cgd 		tn = build(ASSIGN, ln, tn);
    506  1.15  christos 		expr(tn, 0, 0, 1);
    507   1.1       cgd 		goto end;
    508   1.1       cgd 	}
    509   1.1       cgd 
    510   1.1       cgd 	/*
    511   1.1       cgd 	 * Remove all entries which cannot be used for further initializers
    512   1.1       cgd 	 * and do not require a closing brace.
    513   1.1       cgd 	 */
    514   1.1       cgd 	popinit(0);
    515   1.1       cgd 
    516   1.1       cgd 	/* Initialisations by strings are done in strginit(). */
    517   1.1       cgd 	if (strginit(tn))
    518   1.1       cgd 		goto end;
    519   1.1       cgd 
    520   1.1       cgd 	nextinit(0);
    521   1.1       cgd 	if (initerr || tn == NULL)
    522   1.1       cgd 		goto end;
    523   1.1       cgd 
    524   1.1       cgd 	initstk->i_cnt--;
    525  1.14  christos 	DPRINTF(("mkinit() cnt=%d tn=%p\n", initstk->i_cnt, tn));
    526   1.1       cgd 	/* Create a temporary node for the left side. */
    527   1.1       cgd 	ln = tgetblk(sizeof (tnode_t));
    528   1.1       cgd 	ln->tn_op = NAME;
    529   1.1       cgd 	ln->tn_type = tduptyp(initstk->i_type);
    530   1.1       cgd 	ln->tn_type->t_const = 0;
    531   1.1       cgd 	ln->tn_lvalue = 1;
    532   1.1       cgd 	ln->tn_sym = initsym;		/* better than nothing */
    533   1.1       cgd 
    534   1.1       cgd 	tn = cconv(tn);
    535   1.1       cgd 
    536   1.1       cgd 	lt = ln->tn_type->t_tspec;
    537   1.1       cgd 	rt = tn->tn_type->t_tspec;
    538   1.1       cgd 
    539   1.1       cgd 	if (!issclt(lt))
    540  1.11  christos 		LERROR("mkinit()");
    541   1.1       cgd 
    542   1.1       cgd 	if (!typeok(INIT, 0, ln, tn))
    543   1.1       cgd 		goto end;
    544   1.1       cgd 
    545   1.1       cgd 	/*
    546   1.1       cgd 	 * Store the tree memory. This is nessesary because otherwise
    547   1.1       cgd 	 * expr() would free it.
    548   1.1       cgd 	 */
    549   1.1       cgd 	tmem = tsave();
    550  1.15  christos 	expr(tn, 1, 0, 1);
    551   1.1       cgd 	trestor(tmem);
    552   1.7     lukem 
    553   1.1       cgd 	if (isityp(lt) && ln->tn_type->t_isfield && !isityp(rt)) {
    554   1.1       cgd 		/*
    555   1.1       cgd 		 * Bit-fields can be initialized in trad. C only by integer
    556   1.1       cgd 		 * constants.
    557   1.1       cgd 		 */
    558   1.1       cgd 		if (tflag)
    559   1.1       cgd 			/* bit-field initialisation is illegal in trad. C */
    560   1.1       cgd 			warning(186);
    561   1.1       cgd 	}
    562   1.1       cgd 
    563   1.1       cgd 	if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON))
    564   1.1       cgd 		tn = convert(INIT, 0, initstk->i_type, tn);
    565   1.1       cgd 
    566   1.1       cgd 	if (tn != NULL && tn->tn_op != CON) {
    567   1.1       cgd 		sym = NULL;
    568   1.1       cgd 		offs = 0;
    569   1.1       cgd 		if (conaddr(tn, &sym, &offs) == -1) {
    570   1.3       jpo 			if (sc == AUTO || sc == REG) {
    571   1.1       cgd 				/* non-constant initializer */
    572   1.4       jpo 				(void)gnuism(177);
    573   1.1       cgd 			} else {
    574   1.1       cgd 				/* non-constant initializer */
    575   1.1       cgd 				error(177);
    576   1.1       cgd 			}
    577   1.1       cgd 		}
    578   1.1       cgd 	}
    579   1.1       cgd 
    580   1.1       cgd  end:
    581  1.14  christos 	/*
    582  1.14  christos 	 * We only free the block, if we are not a compound declaration
    583  1.14  christos 	 * We know that the only symbols that start with a digit are the
    584  1.14  christos 	 * ones we allocate with mktempsym() for compound declarations
    585  1.14  christos 	 */
    586  1.14  christos 	if (!isdigit((unsigned char)initsym->s_name[0]))
    587  1.14  christos 		tfreeblk();
    588   1.1       cgd }
    589   1.1       cgd 
    590   1.1       cgd 
    591   1.1       cgd static int
    592   1.7     lukem strginit(tnode_t *tn)
    593   1.1       cgd {
    594   1.1       cgd 	tspec_t	t;
    595   1.1       cgd 	istk_t	*istk;
    596   1.1       cgd 	int	len;
    597   1.1       cgd 	strg_t	*strg;
    598   1.1       cgd 
    599   1.1       cgd 	if (tn->tn_op != STRING)
    600   1.1       cgd 		return (0);
    601   1.1       cgd 
    602   1.1       cgd 	istk = initstk;
    603   1.1       cgd 	strg = tn->tn_strg;
    604   1.1       cgd 
    605   1.1       cgd 	/*
    606   1.1       cgd 	 * Check if we have an array type which can be initialized by
    607   1.1       cgd 	 * the string.
    608   1.1       cgd 	 */
    609  1.12  christos 	if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
    610   1.1       cgd 		t = istk->i_subt->t_subt->t_tspec;
    611   1.1       cgd 		if (!((strg->st_tspec == CHAR &&
    612   1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    613   1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    614   1.1       cgd 			return (0);
    615   1.1       cgd 		}
    616   1.1       cgd 		/* Put the array at top of stack */
    617   1.1       cgd 		pushinit();
    618   1.1       cgd 		istk = initstk;
    619   1.1       cgd 	} else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
    620   1.1       cgd 		t = istk->i_type->t_subt->t_tspec;
    621   1.1       cgd 		if (!((strg->st_tspec == CHAR &&
    622   1.1       cgd 		       (t == CHAR || t == UCHAR || t == SCHAR)) ||
    623   1.1       cgd 		      (strg->st_tspec == WCHAR && t == WCHAR))) {
    624   1.1       cgd 			return (0);
    625   1.1       cgd 		}
    626   1.1       cgd 		/*
    627   1.1       cgd 		 * If the array is already partly initialized, we are
    628   1.1       cgd 		 * wrong here.
    629   1.1       cgd 		 */
    630   1.1       cgd 		if (istk->i_cnt != istk->i_type->t_dim)
    631   1.1       cgd 			return (0);
    632   1.1       cgd 	} else {
    633   1.1       cgd 		return (0);
    634   1.1       cgd 	}
    635   1.1       cgd 
    636   1.1       cgd 	/* Get length without trailing NUL character. */
    637   1.1       cgd 	len = strg->st_len;
    638   1.1       cgd 
    639   1.1       cgd 	if (istk->i_nolimit) {
    640   1.1       cgd 		istk->i_nolimit = 0;
    641   1.1       cgd 		istk->i_type->t_dim = len + 1;
    642   1.1       cgd 		/* from now complete type */
    643   1.1       cgd 		setcompl(istk->i_type, 0);
    644   1.1       cgd 	} else {
    645   1.1       cgd 		if (istk->i_type->t_dim < len) {
    646   1.1       cgd 			/* non-null byte ignored in string initializer */
    647   1.1       cgd 			warning(187);
    648   1.1       cgd 		}
    649   1.1       cgd 	}
    650   1.1       cgd 
    651   1.1       cgd 	/* In every case the array is initialized completely. */
    652   1.1       cgd 	istk->i_cnt = 0;
    653   1.1       cgd 
    654   1.1       cgd 	return (1);
    655   1.1       cgd }
    656