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