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