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