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