Home | History | Annotate | Line # | Download | only in lint1
scan.l revision 1.39
      1 %{
      2 /* $NetBSD: scan.l,v 1.39 2008/09/10 14:05:00 joerg Exp $ */
      3 
      4 /*
      5  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      6  * Copyright (c) 1994, 1995 Jochen Pohl
      7  * All Rights Reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *      This product includes software developed by Jochen Pohl for
     20  *      The NetBSD Project.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #if defined(__RCSID) && !defined(lint)
     38 __RCSID("$NetBSD: scan.l,v 1.39 2008/09/10 14:05:00 joerg Exp $");
     39 #endif
     40 
     41 #include <stdlib.h>
     42 #include <string.h>
     43 #include <limits.h>
     44 #include <float.h>
     45 #include <ctype.h>
     46 #include <errno.h>
     47 #include <math.h>
     48 
     49 #include "lint1.h"
     50 #include "cgram.h"
     51 
     52 #define CHAR_MASK	(~(~0 << CHAR_BIT))
     53 #define YY_NO_UNPUT
     54 
     55 /* Current position (its also updated when an included file is parsed) */
     56 pos_t	curr_pos = { 1, "", 0 };
     57 
     58 /*
     59  * Current position in C source (not updated when an included file is
     60  * parsed).
     61  */
     62 pos_t	csrc_pos = { 1, "", 0 };
     63 
     64 static	void	incline(void);
     65 static	void	badchar(int);
     66 static	sbuf_t	*allocsb(void);
     67 static	void	freesb(sbuf_t *);
     68 static	int	inpc(void);
     69 static	int	hash(const char *);
     70 static	sym_t	*search(sbuf_t *);
     71 static	int	name(void);
     72 static	int	keyw(sym_t *);
     73 static	int	icon(int);
     74 static	int	fcon(void);
     75 static	int	operator(int, op_t);
     76 static	int	ccon(void);
     77 static	int	wccon(void);
     78 static	int	getescc(int);
     79 static	void	directive(void);
     80 static	void	comment(void);
     81 static	void	slashslashcomment(void);
     82 static	int	string(void);
     83 static	int	wcstrg(void);
     84 
     85 %}
     86 
     87 L	[_A-Za-z]
     88 D	[0-9]
     89 NZD	[1-9]
     90 OD	[0-7]
     91 HD	[0-9A-Fa-f]
     92 EX	([eE][+-]?[0-9]+)
     93 
     94 %%
     95 
     96 {L}({L}|{D})*		 	return (name());
     97 0{OD}*[lLuU]*			return (icon(8));
     98 {NZD}{D}*[lLuU]*		return (icon(10));
     99 0[xX]{HD}+[lLuU]*		return (icon(16));
    100 {D}+\.{D}*{EX}?[fFlL]?[i]?	|
    101 {D}+{EX}[fFlL]?[i]?		|
    102 0[xX]{HD}+p{HD}+[fFlL]?[i]? 	|
    103 \.{D}+{EX}?[fFlL]?[i]?		return (fcon());
    104 "="				return (operator(T_ASSIGN, ASSIGN));
    105 "*="				return (operator(T_OPASS, MULASS));
    106 "/="				return (operator(T_OPASS, DIVASS));
    107 "%="				return (operator(T_OPASS, MODASS));
    108 "+="				return (operator(T_OPASS, ADDASS));
    109 "-="				return (operator(T_OPASS, SUBASS));
    110 "<<="				return (operator(T_OPASS, SHLASS));
    111 ">>="				return (operator(T_OPASS, SHRASS));
    112 "&="				return (operator(T_OPASS, ANDASS));
    113 "^="				return (operator(T_OPASS, XORASS));
    114 "|="				return (operator(T_OPASS, ORASS));
    115 "||"				return (operator(T_LOGOR, LOGOR));
    116 "&&"				return (operator(T_LOGAND, LOGAND));
    117 "|"				return (operator(T_OR, OR));
    118 "&"				return (operator(T_AND, AND));
    119 "^"				return (operator(T_XOR, XOR));
    120 "=="				return (operator(T_EQOP, EQ));
    121 "!="				return (operator(T_EQOP, NE));
    122 "<"				return (operator(T_RELOP, LT));
    123 ">"				return (operator(T_RELOP, GT));
    124 "<="				return (operator(T_RELOP, LE));
    125 ">="				return (operator(T_RELOP, GE));
    126 "<<"				return (operator(T_SHFTOP, SHL));
    127 ">>"				return (operator(T_SHFTOP, SHR));
    128 "++"				return (operator(T_INCDEC, INC));
    129 "--"				return (operator(T_INCDEC, DEC));
    130 "->"				return (operator(T_STROP, ARROW));
    131 "."				return (operator(T_STROP, POINT));
    132 "+"				return (operator(T_ADDOP, PLUS));
    133 "-"				return (operator(T_ADDOP, MINUS));
    134 "*"				return (operator(T_MULT, MULT));
    135 "/"				return (operator(T_DIVOP, DIV));
    136 "%"				return (operator(T_DIVOP, MOD));
    137 "!"				return (operator(T_UNOP, NOT));
    138 "~"				return (operator(T_UNOP, COMPL));
    139 "\""				return (string());
    140 "L\""				return (wcstrg());
    141 ";"				return (T_SEMI);
    142 "{"				return (T_LBRACE);
    143 "}"				return (T_RBRACE);
    144 ","				return (T_COMMA);
    145 ":"				return (T_COLON);
    146 "?"				return (T_QUEST);
    147 "["				return (T_LBRACK);
    148 "]"				return (T_RBRACK);
    149 "("				return (T_LPARN);
    150 ")"				return (T_RPARN);
    151 "..."				return (T_ELLIPSE);
    152 "'"				return (ccon());
    153 "L'"				return (wccon());
    154 ^#.*$				directive();
    155 \n				incline();
    156 \t|" "|\f|\v			;
    157 "/*"				comment();
    158 "//"				slashslashcomment();
    159 .				badchar(yytext[0]);
    160 
    161 %%
    162 
    163 static void
    164 incline(void)
    165 {
    166 	curr_pos.p_line++;
    167 	curr_pos.p_uniq = 0;
    168 	if (curr_pos.p_file == csrc_pos.p_file) {
    169 		csrc_pos.p_line++;
    170 		csrc_pos.p_uniq = 0;
    171 	}
    172 }
    173 
    174 static void
    175 badchar(int c)
    176 {
    177 
    178 	/* unknown character \%o */
    179 	error(250, c);
    180 }
    181 
    182 /*
    183  * Keywords.
    184  * During initialisation they are written to the symbol table.
    185  */
    186 static	struct	kwtab {
    187 	const	char *kw_name;	/* keyword */
    188 	int	kw_token;	/* token returned by yylex() */
    189 	scl_t	kw_scl;		/* storage class if kw_token T_SCLASS */
    190 	tspec_t	kw_tspec;	/* type spec. if kw_token T_TYPE or T_SOU */
    191 	tqual_t	kw_tqual;	/* type qual. fi kw_token T_QUAL */
    192 	u_int	kw_c89;		/* c89 keyword */
    193 	u_int	kw_c99;		/* c99 keyword */
    194 	u_int	kw_gcc;		/* GCC keyword */
    195 } kwtab[] = {
    196 	{ "asm",	T_ASM,		0,	0,	0,	  0, 0, 1 },
    197 	{ "__asm",	T_ASM,		0,	0,	0,	  0, 0, 0 },
    198 	{ "__asm__",	T_ASM,		0,	0,	0,	  0, 0, 0 },
    199 	{ "auto",	T_SCLASS,	AUTO,	0,	0,	  0, 0, 0 },
    200 	{ "break",	T_BREAK,	0,	0,	0,	  0, 0, 0 },
    201 	{ "_Bool",	T_TYPE,		0,	BOOL,	0,	  0, 1, 0 },
    202 	{ "case",	T_CASE,		0,	0,	0,	  0, 0, 0 },
    203 	{ "char",	T_TYPE,		0,	CHAR,	0,	  0, 0, 0 },
    204 	{ "const",	T_QUAL,		0,	0,	CONST,	  1, 0, 0 },
    205 	{ "_Complex",	T_TYPE,		0,	COMPLEX,0,	  0, 1, 0 },
    206 	{ "__const__",	T_QUAL,		0,	0,	CONST,	  0, 0, 0 },
    207 	{ "__const",	T_QUAL,		0,	0,	CONST,	  0, 0, 0 },
    208 	{ "continue",	T_CONTINUE,	0,	0,	0,	  0, 0, 0 },
    209 	{ "default",	T_DEFAULT,	0,	0,	0,	  0, 0, 0 },
    210 	{ "do",		T_DO,		0,	0,	0,	  0, 0, 0 },
    211 	{ "double",	T_TYPE,		0,	DOUBLE,	0,	  0, 0, 0 },
    212 	{ "else",	T_ELSE,		0,	0,	0,	  0, 0, 0 },
    213 	{ "enum",	T_ENUM,		0,	0,	0,	  0, 0, 0 },
    214 	{ "extern",	T_SCLASS,	EXTERN,	0,	0,	  0, 0, 0 },
    215 	{ "float",	T_TYPE,		0,	FLOAT,	0,	  0, 0, 0 },
    216 	{ "for",	T_FOR,		0,	0,	0,	  0, 0, 0 },
    217 	{ "goto",	T_GOTO,		0,	0,	0,	  0, 0, 0 },
    218 	{ "if",		T_IF,		0,	0,	0,	  0, 0, 0 },
    219 	{ "__imag__",	T_IMAG,		0,	0,	0,	  0, 1, 0 },
    220 	{ "inline",	T_SCLASS,	INLINE,	0,	0,	  0, 1, 0 },
    221 	{ "__inline__",	T_SCLASS,	INLINE,	0,	0,	  0, 0, 0 },
    222 	{ "__inline",	T_SCLASS,	INLINE,	0,	0,	  0, 0, 0 },
    223 	{ "int",	T_TYPE,		0,	INT,	0,	  0, 0, 0 },
    224 	{ "__symbolrename", T_SYMBOLRENAME, 0,	0,	0,	  0, 0, 0 },
    225 	{ "long",	T_TYPE,		0,	LONG,	0,	  0, 0, 0 },
    226 	{ "__real__",	T_REAL,		0,	0,	0,	  0, 1, 0 },
    227 	{ "register",	T_SCLASS,	REG,	0,	0,	  0, 0, 0 },
    228 	{ "return",	T_RETURN,	0,	0,	0,	  0, 0, 0 },
    229 	{ "short",	T_TYPE,		0,	SHORT,	0,	  0, 0, 0 },
    230 	{ "signed",	T_TYPE,		0,	SIGNED,	0,	  1, 0, 0 },
    231 	{ "__signed__",	T_TYPE,		0,	SIGNED,	0,	  0, 0, 0 },
    232 	{ "__signed",	T_TYPE,		0,	SIGNED,	0,	  0, 0, 0 },
    233 	{ "sizeof",	T_SIZEOF,	0,	0,	0,	  0, 0, 0 },
    234 	{ "static",	T_SCLASS,	STATIC,	0,	0,	  0, 0, 0 },
    235 	{ "struct",	T_SOU,		0,	STRUCT,	0,	  0, 0, 0 },
    236 	{ "switch",	T_SWITCH,	0,	0,	0,	  0, 0, 0 },
    237 	{ "typedef",	T_SCLASS,	TYPEDEF, 0,	0,	  0, 0, 0 },
    238 	{ "union",	T_SOU,		0,	UNION,	0,	  0, 0, 0 },
    239 	{ "unsigned",	T_TYPE,		0,	UNSIGN,	0,	  0, 0, 0 },
    240 	{ "void",	T_TYPE,		0,	VOID,	0,	  0, 0, 0 },
    241 	{ "volatile",	T_QUAL,		0,	0,	VOLATILE, 1, 0, 0 },
    242 	{ "__volatile__", T_QUAL,	0,	0,	VOLATILE, 0, 0, 0 },
    243 	{ "__volatile",	T_QUAL,		0,	0,	VOLATILE, 0, 0, 0 },
    244 	{ "while",	T_WHILE,	0,	0,	0,	  0, 0, 0 },
    245 	{ NULL,		0,		0,	0,	0,	  0, 0, 0 }
    246 };
    247 
    248 /* Symbol table */
    249 static	sym_t	*symtab[HSHSIZ1];
    250 
    251 /* bit i of the entry with index i is set */
    252 uint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT];
    253 
    254 /* least significant i bits are set in the entry with index i */
    255 uint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1];
    256 
    257 /* least significant i bits are not set in the entry with index i */
    258 uint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1];
    259 
    260 /* free list for sbuf structures */
    261 static	sbuf_t	 *sbfrlst;
    262 
    263 /* Typ of next expected symbol */
    264 symt_t	symtyp;
    265 
    266 
    267 /*
    268  * All keywords are written to the symbol table. This saves us looking
    269  * in a extra table for each name we found.
    270  */
    271 void
    272 initscan(void)
    273 {
    274 	struct	kwtab *kw;
    275 	sym_t	*sym;
    276 	int	h, i;
    277 	uint64_t uq;
    278 
    279 	for (kw = kwtab; kw->kw_name != NULL; kw++) {
    280 		if ((kw->kw_c89 || kw->kw_c99) && tflag)
    281 			continue;
    282 		if (kw->kw_c99 && !(Sflag || gflag))
    283 			continue;
    284 		if (kw->kw_gcc && !gflag)
    285 			continue;
    286 		sym = getblk(sizeof (sym_t));
    287 		sym->s_name = kw->kw_name;
    288 		sym->s_keyw = 1;
    289 		sym->s_value.v_quad = kw->kw_token;
    290 		if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) {
    291 			sym->s_tspec = kw->kw_tspec;
    292 		} else if (kw->kw_token == T_SCLASS) {
    293 			sym->s_scl = kw->kw_scl;
    294 		} else if (kw->kw_token == T_QUAL) {
    295 			sym->s_tqual = kw->kw_tqual;
    296 		}
    297 		h = hash(sym->s_name);
    298 		if ((sym->s_link = symtab[h]) != NULL)
    299 			symtab[h]->s_rlink = &sym->s_link;
    300 		(symtab[h] = sym)->s_rlink = &symtab[h];
    301 	}
    302 
    303 	/* initialize bit-masks for quads */
    304 	for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) {
    305 		qbmasks[i] = (uint64_t)1 << i;
    306 		uq = ~(uint64_t)0 << i;
    307 		qumasks[i] = uq;
    308 		qlmasks[i] = ~uq;
    309 	}
    310 	qumasks[i] = 0;
    311 	qlmasks[i] = ~(uint64_t)0;
    312 }
    313 
    314 /*
    315  * Get a free sbuf structure, if possible from the free list
    316  */
    317 static sbuf_t *
    318 allocsb(void)
    319 {
    320 	sbuf_t	*sb;
    321 
    322 	if ((sb = sbfrlst) != NULL) {
    323 		sbfrlst = sb->sb_nxt;
    324 	} else {
    325 		sb = xmalloc(sizeof (sbuf_t));
    326 	}
    327 	(void)memset(sb, 0, sizeof (sb));
    328 	return (sb);
    329 }
    330 
    331 /*
    332  * Put a sbuf structure to the free list
    333  */
    334 static void
    335 freesb(sbuf_t *sb)
    336 {
    337 
    338 	sb->sb_nxt = sbfrlst;
    339 	sbfrlst = sb;
    340 }
    341 
    342 /*
    343  * Read a character and ensure that it is positive (except EOF).
    344  * Increment line count(s) if necessary.
    345  */
    346 static int
    347 inpc(void)
    348 {
    349 	int	c;
    350 
    351 	if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
    352 		incline();
    353 	return (c);
    354 }
    355 
    356 static int
    357 hash(const char *s)
    358 {
    359 	u_int	v;
    360 	const	u_char *us;
    361 
    362 	v = 0;
    363 	for (us = (const u_char *)s; *us != '\0'; us++) {
    364 		v = (v << sizeof (v)) + *us;
    365 		v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
    366 	}
    367 	return (v % HSHSIZ1);
    368 }
    369 
    370 /*
    371  * Lex has found a letter followed by zero or more letters or digits.
    372  * It looks for a symbol in the symbol table with the same name. This
    373  * symbol must either be a keyword or a symbol of the type required by
    374  * symtyp (label, member, tag, ...).
    375  *
    376  * If it is a keyword, the token is returned. In some cases it is described
    377  * more deeply by data written to yylval.
    378  *
    379  * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
    380  * is stored in yylval. This struct contains the name of the symbol, it's
    381  * length and hash value. If there is already a symbol of the same name
    382  * and type in the symbol table, the sbuf struct also contains a pointer
    383  * to the symbol table entry.
    384  */
    385 static int
    386 name(void)
    387 {
    388 	char	*s;
    389 	sbuf_t	*sb;
    390 	sym_t	*sym;
    391 	int	tok;
    392 
    393 	sb = allocsb();
    394 	sb->sb_name = yytext;
    395 	sb->sb_len = yyleng;
    396 	sb->sb_hash = hash(yytext);
    397 	if ((sym = search(sb)) != NULL && sym->s_keyw) {
    398 		freesb(sb);
    399 		return (keyw(sym));
    400 	}
    401 
    402 	sb->sb_sym = sym;
    403 
    404 	if (sym != NULL) {
    405 		if (blklev < sym->s_blklev)
    406 			LERROR("name()");
    407 		sb->sb_name = sym->s_name;
    408 		sb->sb_len = strlen(sym->s_name);
    409 		tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
    410 	} else {
    411 		s = getblk(yyleng + 1);
    412 		(void)memcpy(s, yytext, yyleng + 1);
    413 		sb->sb_name = s;
    414 		sb->sb_len = yyleng;
    415 		tok = T_NAME;
    416 	}
    417 
    418 	yylval.y_sb = sb;
    419 	return (tok);
    420 }
    421 
    422 static sym_t *
    423 search(sbuf_t *sb)
    424 {
    425 	sym_t	*sym;
    426 
    427 	for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
    428 		if (strcmp(sym->s_name, sb->sb_name) == 0) {
    429 			if (sym->s_keyw || sym->s_kind == symtyp)
    430 				return (sym);
    431 		}
    432 	}
    433 
    434 	return (NULL);
    435 }
    436 
    437 static int
    438 keyw(sym_t *sym)
    439 {
    440 	int	t;
    441 
    442 	if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
    443 		yylval.y_scl = sym->s_scl;
    444 	} else if (t == T_TYPE || t == T_SOU) {
    445 		yylval.y_tspec = sym->s_tspec;
    446 	} else if (t == T_QUAL) {
    447 		yylval.y_tqual = sym->s_tqual;
    448 	}
    449 	return (t);
    450 }
    451 
    452 /*
    453  * Convert a string representing an integer into internal representation.
    454  * The value is returned in yylval. icon() (and yylex()) returns T_CON.
    455  */
    456 static int
    457 icon(int base)
    458 {
    459 	int	l_suffix, u_suffix;
    460 	int	len;
    461 	const	char *cp;
    462 	char	c, *eptr;
    463 	tspec_t	typ;
    464 	uint64_t uq = 0;
    465 	int	ansiu;
    466 	static	tspec_t contypes[2][3] = {
    467 		{ INT,  LONG,  QUAD },
    468 		{ UINT, ULONG, UQUAD }
    469 	};
    470 
    471 	cp = yytext;
    472 	len = yyleng;
    473 
    474 	/* skip 0x */
    475 	if (base == 16) {
    476 		cp += 2;
    477 		len -= 2;
    478 	}
    479 
    480 	/* read suffixes */
    481 	l_suffix = u_suffix = 0;
    482 	for ( ; ; ) {
    483 		if ((c = cp[len - 1]) == 'l' || c == 'L') {
    484 			l_suffix++;
    485 		} else if (c == 'u' || c == 'U') {
    486 			u_suffix++;
    487 		} else {
    488 			break;
    489 		}
    490 		len--;
    491 	}
    492 	if (l_suffix > 2 || u_suffix > 1) {
    493 		/* malformed integer constant */
    494 		warning(251);
    495 		if (l_suffix > 2)
    496 			l_suffix = 2;
    497 		if (u_suffix > 1)
    498 			u_suffix = 1;
    499 	}
    500 	if (tflag && u_suffix != 0) {
    501 		/* suffix U is illegal in traditional C */
    502 		warning(97);
    503 	}
    504 	typ = contypes[u_suffix][l_suffix];
    505 
    506 	errno = 0;
    507 
    508 	uq = strtouq(cp, &eptr, base);
    509 	if (eptr != cp + len)
    510 		LERROR("icon()");
    511 	if (errno != 0)
    512 		/* integer constant out of range */
    513 		warning(252);
    514 
    515 	/*
    516 	 * If the value is too big for the current type, we must choose
    517 	 * another type.
    518 	 */
    519 	ansiu = 0;
    520 	switch (typ) {
    521 	case INT:
    522 		if (uq <= TARG_INT_MAX) {
    523 			/* ok */
    524 		} else if (uq <= TARG_UINT_MAX && base != 10) {
    525 			typ = UINT;
    526 		} else if (uq <= TARG_LONG_MAX) {
    527 			typ = LONG;
    528 		} else {
    529 			typ = ULONG;
    530 			if (uq > TARG_ULONG_MAX) {
    531 				/* integer constant out of range */
    532 				warning(252);
    533 			}
    534 		}
    535 		if (typ == UINT || typ == ULONG) {
    536 			if (tflag) {
    537 				typ = LONG;
    538 			} else if (!sflag) {
    539 				/*
    540 				 * Remember that the constant is unsigned
    541 				 * only in ANSI C
    542 				 */
    543 				ansiu = 1;
    544 			}
    545 		}
    546 		break;
    547 	case UINT:
    548 		if (uq > TARG_UINT_MAX) {
    549 			typ = ULONG;
    550 			if (uq > TARG_ULONG_MAX) {
    551 				/* integer constant out of range */
    552 				warning(252);
    553 			}
    554 		}
    555 		break;
    556 	case LONG:
    557 		if (uq > TARG_LONG_MAX && !tflag) {
    558 			typ = ULONG;
    559 			if (!sflag)
    560 				ansiu = 1;
    561 			if (uq > TARG_ULONG_MAX) {
    562 				/* integer constant out of range */
    563 				warning(252);
    564 			}
    565 		}
    566 		break;
    567 	case ULONG:
    568 		if (uq > TARG_ULONG_MAX) {
    569 			/* integer constant out of range */
    570 			warning(252);
    571 		}
    572 		break;
    573 	case QUAD:
    574 		if (uq > TARG_QUAD_MAX && !tflag) {
    575 			typ = UQUAD;
    576 			if (!sflag)
    577 				ansiu = 1;
    578 		}
    579 		break;
    580 	case UQUAD:
    581 		if (uq > TARG_UQUAD_MAX) {
    582 			/* integer constant out of range */
    583 			warning(252);
    584 		}
    585 		break;
    586 		/* LINTED (enumeration values not handled in switch) */
    587 	case STRUCT:
    588 	case VOID:
    589 	case LDOUBLE:
    590 	case FUNC:
    591 	case ARRAY:
    592 	case PTR:
    593 	case ENUM:
    594 	case UNION:
    595 	case SIGNED:
    596 	case NOTSPEC:
    597 	case DOUBLE:
    598 	case FLOAT:
    599 	case USHORT:
    600 	case SHORT:
    601 	case UCHAR:
    602 	case SCHAR:
    603 	case CHAR:
    604 	case BOOL:
    605 	case UNSIGN:
    606 	case FCOMPLEX:
    607 	case DCOMPLEX:
    608 	case COMPLEX:
    609 		break;
    610 
    611 	case NTSPEC:	/* this value unused */
    612 		break;
    613 	}
    614 
    615 	uq = (uint64_t)xsign((int64_t)uq, typ, -1);
    616 
    617 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
    618 	yylval.y_val->v_ansiu = ansiu;
    619 	yylval.y_val->v_quad = (int64_t)uq;
    620 
    621 	return (T_CON);
    622 }
    623 
    624 /*
    625  * Returns 1 if t is a signed type and the value is negative.
    626  *
    627  * len is the number of significant bits. If len is -1, len is set
    628  * to the width of type t.
    629  */
    630 int
    631 sign(int64_t q, tspec_t t, int len)
    632 {
    633 
    634 	if (t == PTR || isutyp(t))
    635 		return (0);
    636 	return (msb(q, t, len));
    637 }
    638 
    639 int
    640 msb(int64_t q, tspec_t t, int len)
    641 {
    642 
    643 	if (len <= 0)
    644 		len = size(t);
    645 	return ((q & qbmasks[len - 1]) != 0);
    646 }
    647 
    648 /*
    649  * Extends the sign of q.
    650  */
    651 int64_t
    652 xsign(int64_t q, tspec_t t, int len)
    653 {
    654 
    655 	if (len <= 0)
    656 		len = size(t);
    657 
    658 	if (t == PTR || isutyp(t) || !sign(q, t, len)) {
    659 		q &= qlmasks[len];
    660 	} else {
    661 		q |= qumasks[len];
    662 	}
    663 	return (q);
    664 }
    665 
    666 /*
    667  * Convert a string representing a floating point value into its interal
    668  * representation. Type and value are returned in yylval. fcon()
    669  * (and yylex()) returns T_CON.
    670  * XXX Currently it is not possible to convert constants of type
    671  * long double which are greater than DBL_MAX.
    672  */
    673 static int
    674 fcon(void)
    675 {
    676 	const	char *cp;
    677 	int	len;
    678 	tspec_t typ;
    679 	char	c, *eptr;
    680 	double	d;
    681 	float	f = 0;
    682 
    683 	cp = yytext;
    684 	len = yyleng;
    685 
    686 	if (cp[len - 1] == 'i') {
    687 		/* imaginary, do nothing for now */
    688 		len--;
    689 	}
    690 	if ((c = cp[len - 1]) == 'f' || c == 'F') {
    691 		typ = FLOAT;
    692 		len--;
    693 	} else if (c == 'l' || c == 'L') {
    694 		typ = LDOUBLE;
    695 		len--;
    696 	} else {
    697 		typ = DOUBLE;
    698 	}
    699 
    700 	if (tflag && typ != DOUBLE) {
    701 		/* suffixes F and L are illegal in traditional C */
    702 		warning(98);
    703 	}
    704 
    705 	errno = 0;
    706 	d = strtod(cp, &eptr);
    707 	if (eptr != cp + len) {
    708 		switch (*eptr) {
    709 		/*
    710 		 * XXX: non-native non-current strtod() may not handle hex
    711 		 * floats, ignore the rest if we find traces of hex float
    712 		 * syntax...
    713 		 */
    714 		case 'p':
    715 		case 'P':
    716 		case 'x':
    717 		case 'X':
    718 			d = 0;
    719 			errno = 0;
    720 			break;
    721 		default:
    722 			LERROR("fcon()");
    723 		}
    724 	}
    725 	if (errno != 0)
    726 		/* floating-point constant out of range */
    727 		warning(248);
    728 
    729 	if (typ == FLOAT) {
    730 		f = (float)d;
    731 		if (!finite(f)) {
    732 			/* floating-point constant out of range */
    733 			warning(248);
    734 			f = f > 0 ? FLT_MAX : -FLT_MAX;
    735 		}
    736 	}
    737 
    738 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
    739 	if (typ == FLOAT) {
    740 		yylval.y_val->v_ldbl = f;
    741 	} else {
    742 		yylval.y_val->v_ldbl = d;
    743 	}
    744 
    745 	return (T_CON);
    746 }
    747 
    748 static int
    749 operator(int t, op_t o)
    750 {
    751 
    752 	yylval.y_op = o;
    753 	return (t);
    754 }
    755 
    756 /*
    757  * Called if lex found a leading \'.
    758  */
    759 static int
    760 ccon(void)
    761 {
    762 	int	n, val, c;
    763 	char	cv;
    764 
    765 	n = 0;
    766 	val = 0;
    767 	while ((c = getescc('\'')) >= 0) {
    768 		val = (val << CHAR_BIT) + c;
    769 		n++;
    770 	}
    771 	if (c == -2) {
    772 		/* unterminated character constant */
    773 		error(253);
    774 	} else {
    775 		if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
    776 			/* too many characters in character constant */
    777 			error(71);
    778 		} else if (n > 1) {
    779 			/* multi-character character constant */
    780 			warning(294);
    781 		} else if (n == 0) {
    782 			/* empty character constant */
    783 			error(73);
    784 		}
    785 	}
    786 	if (n == 1) {
    787 		cv = (char)val;
    788 		val = cv;
    789 	}
    790 
    791 	yylval.y_val = xcalloc(1, sizeof (val_t));
    792 	yylval.y_val->v_tspec = INT;
    793 	yylval.y_val->v_quad = val;
    794 
    795 	return (T_CON);
    796 }
    797 
    798 /*
    799  * Called if lex found a leading L\'
    800  */
    801 static int
    802 wccon(void)
    803 {
    804 	static	char buf[MB_LEN_MAX + 1];
    805 	int	i, c;
    806 	wchar_t	wc;
    807 
    808 	i = 0;
    809 	while ((c = getescc('\'')) >= 0) {
    810 		if (i < MB_CUR_MAX)
    811 			buf[i] = (char)c;
    812 		i++;
    813 	}
    814 
    815 	wc = 0;
    816 
    817 	if (c == -2) {
    818 		/* unterminated character constant */
    819 		error(253);
    820 	} else if (c == 0) {
    821 		/* empty character constant */
    822 		error(73);
    823 	} else {
    824 		if (i > MB_CUR_MAX) {
    825 			i = MB_CUR_MAX;
    826 			/* too many characters in character constant */
    827 			error(71);
    828 		} else {
    829 			buf[i] = '\0';
    830 			(void)mbtowc(NULL, NULL, 0);
    831 			if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
    832 				/* invalid multibyte character */
    833 				error(291);
    834 		}
    835 	}
    836 
    837 	yylval.y_val = xcalloc(1, sizeof (val_t));
    838 	yylval.y_val->v_tspec = WCHAR;
    839 	yylval.y_val->v_quad = wc;
    840 
    841 	return (T_CON);
    842 }
    843 
    844 /*
    845  * Read a character which is part of a character constant or of a string
    846  * and handle escapes.
    847  *
    848  * The Argument is the character which delimits the character constant or
    849  * string.
    850  *
    851  * Returns -1 if the end of the character constant or string is reached,
    852  * -2 if the EOF is reached, and the character otherwise.
    853  */
    854 static int
    855 getescc(int d)
    856 {
    857 	static	int pbc = -1;
    858 	int	n, c, v;
    859 
    860 	if (pbc == -1) {
    861 		c = inpc();
    862 	} else {
    863 		c = pbc;
    864 		pbc = -1;
    865 	}
    866 	if (c == d)
    867 		return (-1);
    868 	switch (c) {
    869 	case '\n':
    870 		if (tflag) {
    871 			/* newline in string or char constant */
    872 			error(254);
    873 			return (-2);
    874 		}
    875 		return (c);
    876 	case EOF:
    877 		return (-2);
    878 	case '\\':
    879 		switch (c = inpc()) {
    880 		case '"':
    881 			if (tflag && d == '\'')
    882 				/* \" inside character constant undef. ... */
    883 				warning(262);
    884 			return ('"');
    885 		case '\'':
    886 			return ('\'');
    887 		case '?':
    888 			if (tflag)
    889 				/* \? undefined in traditional C */
    890 				warning(263);
    891 			return ('?');
    892 		case '\\':
    893 			return ('\\');
    894 		case 'a':
    895 			if (tflag)
    896 				/* \a undefined in traditional C */
    897 				warning(81);
    898 			return ('\a');
    899 		case 'b':
    900 			return ('\b');
    901 		case 'f':
    902 			return ('\f');
    903 		case 'n':
    904 			return ('\n');
    905 		case 'r':
    906 			return ('\r');
    907 		case 't':
    908 			return ('\t');
    909 		case 'v':
    910 			if (tflag)
    911 				/* \v undefined in traditional C */
    912 				warning(264);
    913 			return ('\v');
    914 		case '8': case '9':
    915 			/* bad octal digit %c */
    916 			warning(77, c);
    917 			/* FALLTHROUGH */
    918 		case '0': case '1': case '2': case '3':
    919 		case '4': case '5': case '6': case '7':
    920 			n = 3;
    921 			v = 0;
    922 			do {
    923 				v = (v << 3) + (c - '0');
    924 				c = inpc();
    925 			} while (--n && isdigit(c) && (tflag || c <= '7'));
    926 			if (tflag && n > 0 && isdigit(c))
    927 				/* bad octal digit %c */
    928 				warning(77, c);
    929 			pbc = c;
    930 			if (v > UCHAR_MAX) {
    931 				/* character escape does not fit in char. */
    932 				warning(76);
    933 				v &= CHAR_MASK;
    934 			}
    935 			return (v);
    936 		case 'x':
    937 			if (tflag)
    938 				/* \x undefined in traditional C */
    939 				warning(82);
    940 			v = 0;
    941 			n = 0;
    942 			while ((c = inpc()) >= 0 && isxdigit(c)) {
    943 				c = isdigit(c) ?
    944 					c - '0' : toupper(c) - 'A' + 10;
    945 				v = (v << 4) + c;
    946 				if (n >= 0) {
    947 					if ((v & ~CHAR_MASK) != 0) {
    948 						/* overflow in hex escape */
    949 						warning(75);
    950 						n = -1;
    951 					} else {
    952 						n++;
    953 					}
    954 				}
    955 			}
    956 			pbc = c;
    957 			if (n == 0) {
    958 				/* no hex digits follow \x */
    959 				error(74);
    960 			} if (n == -1) {
    961 				v &= CHAR_MASK;
    962 			}
    963 			return (v);
    964 		case '\n':
    965 			return (getescc(d));
    966 		case EOF:
    967 			return (-2);
    968 		default:
    969 			if (isprint(c)) {
    970 				/* dubious escape \%c */
    971 				warning(79, c);
    972 			} else {
    973 				/* dubious escape \%o */
    974 				warning(80, c);
    975 			}
    976 		}
    977 	}
    978 	return (c);
    979 }
    980 
    981 /*
    982  * Called for preprocessor directives. Currently implemented are:
    983  *	# lineno
    984  *	# lineno "filename"
    985  */
    986 static void
    987 directive(void)
    988 {
    989 	const	char *cp, *fn;
    990 	char	c, *eptr;
    991 	size_t	fnl;
    992 	long	ln;
    993 	static	int first = 1;
    994 
    995 	/* Go to first non-whitespace after # */
    996 	for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
    997 		continue;
    998 
    999 	if (!isdigit((unsigned char)c)) {
   1000 	error:
   1001 		/* undefined or invalid # directive */
   1002 		warning(255);
   1003 		return;
   1004 	}
   1005 	ln = strtol(--cp, &eptr, 10);
   1006 	if (cp == eptr)
   1007 		goto error;
   1008 	if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
   1009 		goto error;
   1010 	while ((c = *cp++) == ' ' || c == '\t')
   1011 		continue;
   1012 	if (c != '\0') {
   1013 		if (c != '"')
   1014 			goto error;
   1015 		fn = cp;
   1016 		while ((c = *cp) != '"' && c != '\0')
   1017 			cp++;
   1018 		if (c != '"')
   1019 			goto error;
   1020 		if ((fnl = cp++ - fn) > PATH_MAX)
   1021 			goto error;
   1022 		while ((c = *cp++) == ' ' || c == '\t')
   1023 			continue;
   1024 #if 0
   1025 		if (c != '\0')
   1026 			warning("extra character(s) after directive");
   1027 #endif
   1028 
   1029 		/* empty string means stdin */
   1030 		if (fnl == 0) {
   1031 			fn = "{standard input}";
   1032 			fnl = 16;			/* strlen (fn) */
   1033 		}
   1034 		curr_pos.p_file = fnnalloc(fn, fnl);
   1035 		/*
   1036 		 * If this is the first directive, the name is the name
   1037 		 * of the C source file as specified at the command line.
   1038 		 * It is written to the output file.
   1039 		 */
   1040 		if (first) {
   1041 			csrc_pos.p_file = curr_pos.p_file;
   1042 			outsrc(curr_pos.p_file);
   1043 			first = 0;
   1044 		}
   1045 	}
   1046 	curr_pos.p_line = (int)ln - 1;
   1047 	curr_pos.p_uniq = 0;
   1048 	if (curr_pos.p_file == csrc_pos.p_file) {
   1049 		csrc_pos.p_line = (int)ln - 1;
   1050 		csrc_pos.p_uniq = 0;
   1051 	}
   1052 }
   1053 
   1054 /*
   1055  * Handle lint comments. Following comments are currently understood:
   1056  *	ARGSUSEDn
   1057  *	BITFIELDTYPE
   1058  *	CONSTCOND CONSTANTCOND CONSTANTCONDITION
   1059  *	FALLTHRU FALLTHROUGH
   1060  *	LINTLIBRARY
   1061  *	LINTED NOSTRICT
   1062  *	LONGLONG
   1063  *	NOTREACHED
   1064  *	PRINTFLIKEn
   1065  *	PROTOLIB
   1066  *	SCANFLIKEn
   1067  *	VARARGSn
   1068  * If one of this comments is recognized, the arguments, if any, are
   1069  * parsed and a function which handles this comment is called.
   1070  */
   1071 static void
   1072 comment(void)
   1073 {
   1074 	int	c, lc;
   1075 	static struct {
   1076 		const	char *keywd;
   1077 		int	arg;
   1078 		void	(*func)(int);
   1079 	} keywtab[] = {
   1080 		{ "ARGSUSED",		1,	argsused	},
   1081 		{ "BITFIELDTYPE",	0,	bitfieldtype	},
   1082 		{ "CONSTCOND",		0,	constcond	},
   1083 		{ "CONSTANTCOND",	0,	constcond	},
   1084 		{ "CONSTANTCONDITION",	0,	constcond	},
   1085 		{ "FALLTHRU",		0,	fallthru	},
   1086 		{ "FALLTHROUGH",	0,	fallthru	},
   1087 		{ "LINTLIBRARY",	0,	lintlib		},
   1088 		{ "LINTED",		0,	linted		},
   1089 		{ "LONGLONG",		0,	longlong	},
   1090 		{ "NOSTRICT",		0,	linted		},
   1091 		{ "NOTREACHED",		0,	notreach	},
   1092 		{ "PRINTFLIKE",		1,	printflike	},
   1093 		{ "PROTOLIB",		1,	protolib	},
   1094 		{ "SCANFLIKE",		1,	scanflike	},
   1095 		{ "VARARGS",		1,	varargs		},
   1096 	};
   1097 	char	keywd[32];
   1098 	char	arg[32];
   1099 	int	l, i, a;
   1100 	int	eoc;
   1101 
   1102 	eoc = 0;
   1103 
   1104 	/* Skip white spaces after the start of the comment */
   1105 	while ((c = inpc()) != EOF && isspace(c))
   1106 		continue;
   1107 
   1108 	/* Read the potential keyword to keywd */
   1109 	l = 0;
   1110 	while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
   1111 		keywd[l++] = (char)c;
   1112 		c = inpc();
   1113 	}
   1114 	keywd[l] = '\0';
   1115 
   1116 	/* look for the keyword */
   1117 	for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
   1118 		if (strcmp(keywtab[i].keywd, keywd) == 0)
   1119 			break;
   1120 	}
   1121 	if (i == sizeof (keywtab) / sizeof (keywtab[0]))
   1122 		goto skip_rest;
   1123 
   1124 	/* skip white spaces after the keyword */
   1125 	while (c != EOF && isspace(c))
   1126 		c = inpc();
   1127 
   1128 	/* read the argument, if the keyword accepts one and there is one */
   1129 	l = 0;
   1130 	if (keywtab[i].arg) {
   1131 		while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
   1132 			arg[l++] = (char)c;
   1133 			c = inpc();
   1134 		}
   1135 	}
   1136 	arg[l] = '\0';
   1137 	a = l != 0 ? atoi(arg) : -1;
   1138 
   1139 	/* skip white spaces after the argument */
   1140 	while (c != EOF && isspace(c))
   1141 		c = inpc();
   1142 
   1143 	if (c != '*' || (c = inpc()) != '/') {
   1144 		if (keywtab[i].func != linted)
   1145 			/* extra characters in lint comment */
   1146 			warning(257);
   1147 	} else {
   1148 		/*
   1149 		 * remember that we have already found the end of the
   1150 		 * comment
   1151 		 */
   1152 		eoc = 1;
   1153 	}
   1154 
   1155 	if (keywtab[i].func != NULL)
   1156 		(*keywtab[i].func)(a);
   1157 
   1158  skip_rest:
   1159 	while (!eoc) {
   1160 		lc = c;
   1161 		if ((c = inpc()) == EOF) {
   1162 			/* unterminated comment */
   1163 			error(256);
   1164 			break;
   1165 		}
   1166 		if (lc == '*' && c == '/')
   1167 			eoc = 1;
   1168 	}
   1169 }
   1170 
   1171 /*
   1172  * Handle // style comments
   1173  */
   1174 static void
   1175 slashslashcomment(void)
   1176 {
   1177 	int c;
   1178 
   1179 	if (!Sflag && !gflag)
   1180 		/* // comments only supported in C99 */
   1181 		(void)gnuism(312, tflag ? "traditional" : "ANSI");
   1182 
   1183 	while ((c = inpc()) != EOF && c != '\n')
   1184 		continue;
   1185 }
   1186 
   1187 /*
   1188  * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
   1189  * clrwflgs() is called after function definitions and global and
   1190  * local declarations and definitions. It is also called between
   1191  * the controlling expression and the body of control statements
   1192  * (if, switch, for, while).
   1193  */
   1194 void
   1195 clrwflgs(void)
   1196 {
   1197 
   1198 	nowarn = 0;
   1199 	quadflg = 0;
   1200 	ccflg = 0;
   1201 }
   1202 
   1203 /*
   1204  * Strings are stored in a dynamically alloceted buffer and passed
   1205  * in yylval.y_xstrg to the parser. The parser or the routines called
   1206  * by the parser are responsible for freeing this buffer.
   1207  */
   1208 static int
   1209 string(void)
   1210 {
   1211 	u_char	*s;
   1212 	int	c;
   1213 	size_t	len, max;
   1214 	strg_t	*strg;
   1215 
   1216 	s = xmalloc(max = 64);
   1217 
   1218 	len = 0;
   1219 	while ((c = getescc('"')) >= 0) {
   1220 		/* +1 to reserve space for a trailing NUL character */
   1221 		if (len + 1 == max)
   1222 			s = xrealloc(s, max *= 2);
   1223 		s[len++] = (char)c;
   1224 	}
   1225 	s[len] = '\0';
   1226 	if (c == -2)
   1227 		/* unterminated string constant */
   1228 		error(258);
   1229 
   1230 	strg = xcalloc(1, sizeof (strg_t));
   1231 	strg->st_tspec = CHAR;
   1232 	strg->st_len = len;
   1233 	strg->st_cp = s;
   1234 
   1235 	yylval.y_strg = strg;
   1236 	return (T_STRING);
   1237 }
   1238 
   1239 static int
   1240 wcstrg(void)
   1241 {
   1242 	char	*s;
   1243 	int	c, i, n, wi;
   1244 	size_t	len, max, wlen;
   1245 	wchar_t	*ws;
   1246 	strg_t	*strg;
   1247 
   1248 	s = xmalloc(max = 64);
   1249 	len = 0;
   1250 	while ((c = getescc('"')) >= 0) {
   1251 		/* +1 to save space for a trailing NUL character */
   1252 		if (len + 1 >= max)
   1253 			s = xrealloc(s, max *= 2);
   1254 		s[len++] = (char)c;
   1255 	}
   1256 	s[len] = '\0';
   1257 	if (c == -2)
   1258 		/* unterminated string constant */
   1259 		error(258);
   1260 
   1261 	/* get length of wide character string */
   1262 	(void)mblen(NULL, 0);
   1263 	for (i = 0, wlen = 0; i < len; i += n, wlen++) {
   1264 		if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
   1265 			/* invalid multibyte character */
   1266 			error(291);
   1267 			break;
   1268 		}
   1269 		if (n == 0)
   1270 			n = 1;
   1271 	}
   1272 
   1273 	ws = xmalloc((wlen + 1) * sizeof (wchar_t));
   1274 
   1275 	/* convert from multibyte to wide char */
   1276 	(void)mbtowc(NULL, NULL, 0);
   1277 	for (i = 0, wi = 0; i < len; i += n, wi++) {
   1278 		if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
   1279 			break;
   1280 		if (n == 0)
   1281 			n = 1;
   1282 	}
   1283 	ws[wi] = 0;
   1284 	free(s);
   1285 
   1286 	strg = xcalloc(1, sizeof (strg_t));
   1287 	strg->st_tspec = WCHAR;
   1288 	strg->st_len = wlen;
   1289 	strg->st_wcp = ws;
   1290 
   1291 	yylval.y_strg = strg;
   1292 	return (T_STRING);
   1293 }
   1294 
   1295 /*
   1296  * As noted above the scanner does not create new symbol table entries
   1297  * for symbols it cannot find in the symbol table. This is to avoid
   1298  * putting undeclared symbols into the symbol table if a syntax error
   1299  * occurs.
   1300  *
   1301  * getsym() is called as soon as it is probably ok to put the symbol to
   1302  * the symbol table. This does not mean that it is not possible that
   1303  * symbols are put to the symbol table which are than not completely
   1304  * declared due to syntax errors. To avoid too many problems in this
   1305  * case symbols get type int in getsym().
   1306  *
   1307  * XXX calls to getsym() should be delayed until decl1*() is called
   1308  */
   1309 sym_t *
   1310 getsym(sbuf_t *sb)
   1311 {
   1312 	dinfo_t	*di;
   1313 	char	*s;
   1314 	sym_t	*sym;
   1315 
   1316 	sym = sb->sb_sym;
   1317 
   1318 	/*
   1319 	 * During member declaration it is possible that name() looked
   1320 	 * for symbols of type FVFT, although it should have looked for
   1321 	 * symbols of type FTAG. Same can happen for labels. Both cases
   1322 	 * are compensated here.
   1323 	 */
   1324 	if (symtyp == FMOS || symtyp == FLAB) {
   1325 		if (sym == NULL || sym->s_kind == FVFT)
   1326 			sym = search(sb);
   1327 	}
   1328 
   1329 	if (sym != NULL) {
   1330 		if (sym->s_kind != symtyp)
   1331 			LERROR("storesym()");
   1332 		symtyp = FVFT;
   1333 		freesb(sb);
   1334 		return (sym);
   1335 	}
   1336 
   1337 	/* create a new symbol table entry */
   1338 
   1339 	/* labels must always be allocated at level 1 (outhermost block) */
   1340 	if (symtyp == FLAB) {
   1341 		sym = getlblk(1, sizeof (sym_t));
   1342 		s = getlblk(1, sb->sb_len + 1);
   1343 		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
   1344 		sym->s_name = s;
   1345 		sym->s_blklev = 1;
   1346 		di = dcs;
   1347 		while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
   1348 			di = di->d_nxt;
   1349 		if (di->d_ctx != AUTO)
   1350 			LERROR("storesym()");
   1351 	} else {
   1352 		sym = getblk(sizeof (sym_t));
   1353 		sym->s_name = sb->sb_name;
   1354 		sym->s_blklev = blklev;
   1355 		di = dcs;
   1356 	}
   1357 
   1358 	UNIQUE_CURR_POS(sym->s_dpos);
   1359 	if ((sym->s_kind = symtyp) != FLAB)
   1360 		sym->s_type = gettyp(INT);
   1361 
   1362 	symtyp = FVFT;
   1363 
   1364 	if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
   1365 		symtab[sb->sb_hash]->s_rlink = &sym->s_link;
   1366 	(symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
   1367 
   1368 	*di->d_ldlsym = sym;
   1369 	di->d_ldlsym = &sym->s_dlnxt;
   1370 
   1371 	freesb(sb);
   1372 	return (sym);
   1373 }
   1374 
   1375 /*
   1376  * Construct a temporary symbol. The symbol starts with a digit, so that
   1377  * it is illegal.
   1378  */
   1379 sym_t *
   1380 mktempsym(type_t *t)
   1381 {
   1382 	static int n = 0;
   1383 	int h;
   1384 	char *s = getlblk(blklev, 64);
   1385 	sym_t *sym = getblk(sizeof (sym_t));
   1386 
   1387 	(void)snprintf(s, 64, "%.8d_tmp", n++);
   1388 	h = hash(s);
   1389 
   1390 	sym->s_name = s;
   1391 	sym->s_type = t;
   1392 	sym->s_blklev = blklev;
   1393 	sym->s_scl = AUTO;
   1394 	sym->s_kind = FVFT;
   1395 	sym->s_used = 1;
   1396 	sym->s_set = 1;
   1397 
   1398 	if ((sym->s_link = symtab[h]) != NULL)
   1399 		symtab[h]->s_rlink = &sym->s_link;
   1400 	(symtab[h] = sym)->s_rlink = &symtab[h];
   1401 
   1402 	*dcs->d_ldlsym = sym;
   1403 	dcs->d_ldlsym = &sym->s_dlnxt;
   1404 
   1405 	return sym;
   1406 }
   1407 
   1408 /*
   1409  * Remove a symbol forever from the symbol table. s_blklev
   1410  * is set to -1 to avoid that the symbol will later be put
   1411  * back to the symbol table.
   1412  */
   1413 void
   1414 rmsym(sym_t *sym)
   1415 {
   1416 
   1417 	if ((*sym->s_rlink = sym->s_link) != NULL)
   1418 		sym->s_link->s_rlink = sym->s_rlink;
   1419 	sym->s_blklev = -1;
   1420 	sym->s_link = NULL;
   1421 }
   1422 
   1423 /*
   1424  * Remove a list of symbols declared at one level from the symbol
   1425  * table.
   1426  */
   1427 void
   1428 rmsyms(sym_t *syms)
   1429 {
   1430 	sym_t	*sym;
   1431 
   1432 	for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
   1433 		if (sym->s_blklev != -1) {
   1434 			if ((*sym->s_rlink = sym->s_link) != NULL)
   1435 				sym->s_link->s_rlink = sym->s_rlink;
   1436 			sym->s_link = NULL;
   1437 			sym->s_rlink = NULL;
   1438 		}
   1439 	}
   1440 }
   1441 
   1442 /*
   1443  * Put a symbol into the symbol table
   1444  */
   1445 void
   1446 inssym(int bl, sym_t *sym)
   1447 {
   1448 	int	h;
   1449 
   1450 	h = hash(sym->s_name);
   1451 	if ((sym->s_link = symtab[h]) != NULL)
   1452 		symtab[h]->s_rlink = &sym->s_link;
   1453 	(symtab[h] = sym)->s_rlink = &symtab[h];
   1454 	sym->s_blklev = bl;
   1455 	if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
   1456 		LERROR("inssym()");
   1457 }
   1458 
   1459 /*
   1460  * Called at level 0 after syntax errors
   1461  * Removes all symbols which are not declared at level 0 from the
   1462  * symbol table. Also frees all memory which is not associated with
   1463  * level 0.
   1464  */
   1465 void
   1466 cleanup(void)
   1467 {
   1468 	sym_t	*sym, *nsym;
   1469 	int	i;
   1470 
   1471 	for (i = 0; i < HSHSIZ1; i++) {
   1472 		for (sym = symtab[i]; sym != NULL; sym = nsym) {
   1473 			nsym = sym->s_link;
   1474 			if (sym->s_blklev >= 1) {
   1475 				if ((*sym->s_rlink = nsym) != NULL)
   1476 					nsym->s_rlink = sym->s_rlink;
   1477 			}
   1478 		}
   1479 	}
   1480 
   1481 	for (i = mblklev; i > 0; i--)
   1482 		freelblk(i);
   1483 }
   1484 
   1485 /*
   1486  * Create a new symbol with the name of an existing symbol.
   1487  */
   1488 sym_t *
   1489 pushdown(sym_t *sym)
   1490 {
   1491 	int	h;
   1492 	sym_t	*nsym;
   1493 
   1494 	h = hash(sym->s_name);
   1495 	nsym = getblk(sizeof (sym_t));
   1496 	if (sym->s_blklev > blklev)
   1497 		LERROR("pushdown()");
   1498 	nsym->s_name = sym->s_name;
   1499 	UNIQUE_CURR_POS(nsym->s_dpos);
   1500 	nsym->s_kind = sym->s_kind;
   1501 	nsym->s_blklev = blklev;
   1502 
   1503 	if ((nsym->s_link = symtab[h]) != NULL)
   1504 		symtab[h]->s_rlink = &nsym->s_link;
   1505 	(symtab[h] = nsym)->s_rlink = &symtab[h];
   1506 
   1507 	*dcs->d_ldlsym = nsym;
   1508 	dcs->d_ldlsym = &nsym->s_dlnxt;
   1509 
   1510 	return (nsym);
   1511 }
   1512 
   1513 /*
   1514  * Free any dynamically allocated memory referenced by
   1515  * the value stack or yylval.
   1516  * The type of information in yylval is described by tok.
   1517  */
   1518 void
   1519 freeyyv(void *sp, int tok)
   1520 {
   1521 	if (tok == T_NAME || tok == T_TYPENAME) {
   1522 		sbuf_t *sb = *(sbuf_t **)sp;
   1523 		freesb(sb);
   1524 	} else if (tok == T_CON) {
   1525 		val_t *val = *(val_t **)sp;
   1526 		free(val);
   1527 	} else if (tok == T_STRING) {
   1528 		strg_t *strg = *(strg_t **)sp;
   1529 		if (strg->st_tspec == CHAR) {
   1530 			free(strg->st_cp);
   1531 		} else if (strg->st_tspec == WCHAR) {
   1532 			free(strg->st_wcp);
   1533 		} else {
   1534 			LERROR("fryylv()");
   1535 		}
   1536 		free(strg);
   1537 	}
   1538 }
   1539