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