Home | History | Annotate | Line # | Download | only in lint1
scan.l revision 1.6
      1 %{
      2 /*	$NetBSD: scan.l,v 1.6 1995/10/02 17:26:57 jpo Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1994, 1995 Jochen Pohl
      6  * All Rights Reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *      This product includes software developed by Jochen Pohl for
     19  *      The NetBSD Project.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 #ifndef lint
     36 static char rcsid[] = "$NetBSD: scan.l,v 1.6 1995/10/02 17:26:57 jpo Exp $";
     37 #endif
     38 
     39 #include <stdlib.h>
     40 #include <string.h>
     41 #include <limits.h>
     42 #include <float.h>
     43 #include <ctype.h>
     44 #include <errno.h>
     45 #include <math.h>
     46 #include <err.h>
     47 
     48 #include "lint1.h"
     49 #include "y.tab.h"
     50 
     51 #define CHAR_MASK	(~(~0 << CHAR_BIT))
     52 
     53 /* XXX declaration of strtouq() is missing in stdlib.h ? */
     54 extern	u_quad_t strtouq __P((const char *, char **, int));
     55 
     56 /* Current position (its also updated when an included file is parsed) */
     57 pos_t	curr_pos = { 1, "" };
     58 
     59 /*
     60  * Current position in C source (not updated when an included file is
     61  * parsed).
     62  */
     63 pos_t	csrc_pos = { 1, "" };
     64 
     65 /* Line number in output of cpp */
     66 int	isrcline = 1;
     67 
     68 static	void	incline __P((void));
     69 static	void	badchar __P((int));
     70 static	sbuf_t	*allocsb __P((void));
     71 static	void	freesb __P((sbuf_t *));
     72 static	int	inpc __P((void));
     73 static	int	hash __P((const char *));
     74 static	sym_t	*search __P((sbuf_t *));
     75 static	int	name __P((void));
     76 static	int	keyw __P((sym_t *));
     77 static	int	icon __P((int));
     78 static	int	fcon __P((void));
     79 static	int	operator __P((int, op_t));
     80 static	int	ccon __P((void));
     81 static	int	wccon __P((void));
     82 static	int	getescc __P((int));
     83 static	void	directive __P((void));
     84 static	void	comment __P((void));
     85 static	int	string __P((void));
     86 static	int	wcstrg __P((void));
     87 
     88 %}
     89 
     90 L	[_A-Za-z]
     91 D	[0-9]
     92 NZD	[1-9]
     93 OD	[0-7]
     94 HD	[0-9A-Fa-f]
     95 EX	([eE][+-]?[0-9]+)
     96 
     97 %%
     98 
     99 {L}({L}|{D})*		 	return (name());
    100 0{OD}*[lLuU]*			return (icon(8));
    101 {NZD}{D}*[lLuU]*		return (icon(10));
    102 0[xX]{HD}+[lLuU]*		return (icon(16));
    103 {D}+\.{D}*{EX}?[fFlL]?		|
    104 {D}+{EX}[fFlL]?			|
    105 \.{D}+{EX}?[fFlL]?		return (fcon());
    106 "="				return (operator(T_ASSIGN, ASSIGN));
    107 "*="				return (operator(T_OPASS, MULASS));
    108 "/="				return (operator(T_OPASS, DIVASS));
    109 "%="				return (operator(T_OPASS, MODASS));
    110 "+="				return (operator(T_OPASS, ADDASS));
    111 "-="				return (operator(T_OPASS, SUBASS));
    112 "<<="				return (operator(T_OPASS, SHLASS));
    113 ">>="				return (operator(T_OPASS, SHRASS));
    114 "&="				return (operator(T_OPASS, ANDASS));
    115 "^="				return (operator(T_OPASS, XORASS));
    116 "|="				return (operator(T_OPASS, ORASS));
    117 "||"				return (operator(T_LOGOR, LOGOR));
    118 "&&"				return (operator(T_LOGAND, LOGAND));
    119 "|"				return (operator(T_OR, OR));
    120 "&"				return (operator(T_AND, AND));
    121 "^"				return (operator(T_XOR, XOR));
    122 "=="				return (operator(T_EQOP, EQ));
    123 "!="				return (operator(T_EQOP, NE));
    124 "<"				return (operator(T_RELOP, LT));
    125 ">"				return (operator(T_RELOP, GT));
    126 "<="				return (operator(T_RELOP, LE));
    127 ">="				return (operator(T_RELOP, GE));
    128 "<<"				return (operator(T_SHFTOP, SHL));
    129 ">>"				return (operator(T_SHFTOP, SHR));
    130 "++"				return (operator(T_INCDEC, INC));
    131 "--"				return (operator(T_INCDEC, DEC));
    132 "->"				return (operator(T_STROP, ARROW));
    133 "."				return (operator(T_STROP, POINT));
    134 "+"				return (operator(T_ADDOP, PLUS));
    135 "-"				return (operator(T_ADDOP, MINUS));
    136 "*"				return (operator(T_MULT, MULT));
    137 "/"				return (operator(T_DIVOP, DIV));
    138 "%"				return (operator(T_DIVOP, MOD));
    139 "!"				return (operator(T_UNOP, NOT));
    140 "~"				return (operator(T_UNOP, COMPL));
    141 "\""				return (string());
    142 "L\""				return (wcstrg());
    143 ";"				return (T_SEMI);
    144 "{"				return (T_LBRACE);
    145 "}"				return (T_RBRACE);
    146 ","				return (T_COMMA);
    147 ":"				return (T_COLON);
    148 "?"				return (T_QUEST);
    149 "["				return (T_LBRACK);
    150 "]"				return (T_RBRACK);
    151 "("				return (T_LPARN);
    152 ")"				return (T_RPARN);
    153 "..."				return (T_ELLIPSE);
    154 "'"				return (ccon());
    155 "L'"				return (wccon());
    156 ^#.*$				directive();
    157 \n				incline();
    158 \t|" "|\f|\v			;
    159 "/*"				comment();
    160 .				badchar(yytext[0]);
    161 
    162 %%
    163 
    164 static void
    165 incline()
    166 {
    167 	curr_pos.p_line++;
    168 	isrcline++;
    169 	if (curr_pos.p_file == csrc_pos.p_file)
    170 		csrc_pos.p_line++;
    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 	{ "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;
    461 	u_quad_t uq;
    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 	}
    563 
    564 	if (typ != QUAD && typ != UQUAD) {
    565 		if (isutyp(typ)) {
    566 			uq = ul;
    567 		} else {
    568 			uq = (quad_t)(long)ul;
    569 		}
    570 	}
    571 
    572 	uq = (u_quad_t)xsign((quad_t)uq, typ, -1);
    573 
    574 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
    575 	yylval.y_val->v_ansiu = ansiu;
    576 	yylval.y_val->v_quad = (quad_t)uq;
    577 
    578 	return (T_CON);
    579 }
    580 
    581 /*
    582  * Returns 1 if t is a signed type and the value is negative.
    583  *
    584  * len is the number of significant bits. If len is -1, len is set
    585  * to the width of type t.
    586  */
    587 int
    588 sign(q, t, len)
    589 	quad_t	q;
    590 	tspec_t	t;
    591 	int	len;
    592 {
    593 	if (t == PTR || isutyp(t))
    594 		return (0);
    595 	return (msb(q, t, len));
    596 }
    597 
    598 int
    599 msb(q, t, len)
    600 	quad_t	q;
    601 	tspec_t	t;
    602 	int	len;
    603 {
    604 	if (len <= 0)
    605 		len = size(t);
    606 	return ((q & qbmasks[len - 1]) != 0);
    607 }
    608 
    609 /*
    610  * Extends the sign of q.
    611  */
    612 quad_t
    613 xsign(q, t, len)
    614 	quad_t	q;
    615 	tspec_t	t;
    616 	int	len;
    617 {
    618 	if (len <= 0)
    619 		len = size(t);
    620 
    621 	if (t == PTR || isutyp(t) || !sign(q, t, len)) {
    622 		q &= qlmasks[len];
    623 	} else {
    624 		q |= qumasks[len];
    625 	}
    626 	return (q);
    627 }
    628 
    629 /*
    630  * Convert a string representing a floating point value into its interal
    631  * representation. Type and value are returned in yylval. fcon()
    632  * (and yylex()) returns T_CON.
    633  * XXX Currently it is not possible to convert constants of type
    634  * long double which are greater then DBL_MAX.
    635  */
    636 static int
    637 fcon()
    638 {
    639 	const	char *cp;
    640 	int	len;
    641 	tspec_t typ;
    642 	char	c, *eptr;
    643 	double	d;
    644 	float	f;
    645 
    646 	cp = yytext;
    647 	len = yyleng;
    648 
    649 	if ((c = cp[len - 1]) == 'f' || c == 'F') {
    650 		typ = FLOAT;
    651 		len--;
    652 	} else if (c == 'l' || c == 'L') {
    653 		typ = LDOUBLE;
    654 		len--;
    655 	} else {
    656 		typ = DOUBLE;
    657 	}
    658 
    659 	if (tflag && typ != DOUBLE) {
    660 		/* suffixes F and L are illegal in traditional C */
    661 		warning(98);
    662 	}
    663 
    664 	errno = 0;
    665 	d = strtod(cp, &eptr);
    666 	if (eptr != cp + len)
    667 		lerror("fcon() 1");
    668 	if (errno != 0)
    669 		/* floating-point constant out of range */
    670 		warning(248);
    671 
    672 	if (typ == FLOAT) {
    673 		f = (float)d;
    674 		if (isinf(f)) {
    675 			/* floating-point constant out of range */
    676 			warning(248);
    677 			f = f > 0 ? FLT_MAX : -FLT_MAX;
    678 		}
    679 	}
    680 
    681 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
    682 	if (typ == FLOAT) {
    683 		yylval.y_val->v_ldbl = f;
    684 	} else {
    685 		yylval.y_val->v_ldbl = d;
    686 	}
    687 
    688 	return (T_CON);
    689 }
    690 
    691 static int
    692 operator(t, o)
    693 	int	t;
    694 	op_t	o;
    695 {
    696 	yylval.y_op = o;
    697 	return (t);
    698 }
    699 
    700 /*
    701  * Called if lex found a leading \'.
    702  */
    703 static int
    704 ccon()
    705 {
    706 	int	n, val, c;
    707 	char	cv;
    708 
    709 	n = 0;
    710 	val = 0;
    711 	while ((c = getescc('\'')) >= 0) {
    712 		val = (val << CHAR_BIT) + c;
    713 		n++;
    714 	}
    715 	if (c == -2) {
    716 		/* unterminated character constant */
    717 		error(253);
    718 	} else {
    719 		if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
    720 			/* too many characters in character constant */
    721 			error(71);
    722 		} else if (n > 1) {
    723 			/* multi-character character constant */
    724 			warning(294);
    725 		} else if (n == 0) {
    726 			/* empty character constant */
    727 			error(73);
    728 		}
    729 	}
    730 	if (n == 1) {
    731 		cv = (char)val;
    732 		val = cv;
    733 	}
    734 
    735 	yylval.y_val = xcalloc(1, sizeof (val_t));
    736 	yylval.y_val->v_tspec = INT;
    737 	yylval.y_val->v_quad = val;
    738 
    739 	return (T_CON);
    740 }
    741 
    742 /*
    743  * Called if lex found a leading L\'
    744  */
    745 static int
    746 wccon()
    747 {
    748 	static	char buf[MB_CUR_MAX + 1];
    749 	int	i, c;
    750 	wchar_t	wc;
    751 
    752 	i = 0;
    753 	while ((c = getescc('\'')) >= 0) {
    754 		if (i < MB_CUR_MAX)
    755 			buf[i] = (char)c;
    756 		i++;
    757 	}
    758 
    759 	wc = 0;
    760 
    761 	if (c == -2) {
    762 		/* unterminated character constant */
    763 		error(253);
    764 	} else if (c == 0) {
    765 		/* empty character constant */
    766 		error(73);
    767 	} else {
    768 		if (i > MB_CUR_MAX) {
    769 			i = MB_CUR_MAX;
    770 			/* too many characters in character constant */
    771 			error(71);
    772 		} else {
    773 			buf[i] = '\0';
    774 			(void)mbtowc(NULL, NULL, 0);
    775 			if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
    776 				/* invalid multibyte character */
    777 				error(291);
    778 		}
    779 	}
    780 
    781 	yylval.y_val = xcalloc(1, sizeof (val_t));
    782 	yylval.y_val->v_tspec = WCHAR;
    783 	yylval.y_val->v_quad = wc;
    784 
    785 	return (T_CON);
    786 }
    787 
    788 /*
    789  * Read a character which is part of a character constant or of a string
    790  * and handle escapes.
    791  *
    792  * The Argument is the character which delimits the character constant or
    793  * string.
    794  *
    795  * Returns -1 if the end of the character constant or string is reached,
    796  * -2 if the EOF is reached, and the charachter otherwise.
    797  */
    798 static int
    799 getescc(d)
    800 	int	d;
    801 {
    802 	static	int pbc = -1;
    803 	int	n, c, v;
    804 
    805 	if (pbc == -1) {
    806 		c = inpc();
    807 	} else {
    808 		c = pbc;
    809 		pbc = -1;
    810 	}
    811 	if (c == d)
    812 		return (-1);
    813 	switch (c) {
    814 	case '\n':
    815 		/* newline in string or char constant */
    816 		error(254);
    817 		return (-2);
    818 	case EOF:
    819 		return (-2);
    820 	case '\\':
    821 		switch (c = inpc()) {
    822 		case '"':
    823 			if (tflag && d == '\'')
    824 				/* \" inside character constant undef. ... */
    825 				warning(262);
    826 			return ('"');
    827 		case '\'':
    828 			return ('\'');
    829 		case '?':
    830 			if (tflag)
    831 				/* \? undefined in traditional C */
    832 				warning(263);
    833 			return ('?');
    834 		case '\\':
    835 			return ('\\');
    836 		case 'a':
    837 			if (tflag)
    838 				/* \a undefined in traditional C */
    839 				warning(81);
    840 #ifdef __STDC__
    841 			return ('\a');
    842 #else
    843 			return ('\007');
    844 #endif
    845 		case 'b':
    846 			return ('\b');
    847 		case 'f':
    848 			return ('\f');
    849 		case 'n':
    850 			return ('\n');
    851 		case 'r':
    852 			return ('\r');
    853 		case 't':
    854 			return ('\t');
    855 		case 'v':
    856 			if (tflag)
    857 				/* \v undefined in traditional C */
    858 				warning(264);
    859 #ifdef __STDC__
    860 			return ('\v');
    861 #else
    862 			return ('\013');
    863 #endif
    864 		case '8': case '9':
    865 			/* bad octal digit %c */
    866 			warning(77, c);
    867 			/* FALLTHROUGH */
    868 		case '0': case '1': case '2': case '3':
    869 		case '4': case '5': case '6': case '7':
    870 			n = 3;
    871 			v = 0;
    872 			do {
    873 				v = (v << 3) + (c - '0');
    874 				c = inpc();
    875 			} while (--n && isdigit(c) && (tflag || c <= '7'));
    876 			if (tflag && n > 0 && isdigit(c))
    877 				/* bad octal digit %c */
    878 				warning(77, c);
    879 			pbc = c;
    880 			if (v > UCHAR_MAX) {
    881 				/* character escape does not fit in char. */
    882 				warning(76);
    883 				v &= CHAR_MASK;
    884 			}
    885 			return (v);
    886 		case 'x':
    887 			if (tflag)
    888 				/* \x undefined in traditional C */
    889 				warning(82);
    890 			v = 0;
    891 			n = 0;
    892 			while ((c = inpc()) >= 0 && isxdigit(c)) {
    893 				c = isdigit(c) ?
    894 					c - '0' : toupper(c) - 'A' + 10;
    895 				v = (v << 4) + c;
    896 				if (n >= 0) {
    897 					if ((v & ~CHAR_MASK) != 0) {
    898 						/* overflow in hex escape */
    899 						warning(75);
    900 						n = -1;
    901 					} else {
    902 						n++;
    903 					}
    904 				}
    905 			}
    906 			pbc = c;
    907 			if (n == 0) {
    908 				/* no hex digits follow \x */
    909 				error(74);
    910 			} if (n == -1) {
    911 				v &= CHAR_MASK;
    912 			}
    913 			return (v);
    914 		case '\n':
    915 			return (getescc(d));
    916 		case EOF:
    917 			return (-2);
    918 		default:
    919 			if (isprint(c)) {
    920 				/* dubious escape \%c */
    921 				warning(79, c);
    922 			} else {
    923 				/* dubious escape \%o */
    924 				warning(80, c);
    925 			}
    926 		}
    927 	}
    928 	return (c);
    929 }
    930 
    931 /*
    932  * Called for preprocessor directives. Currently implemented are:
    933  *	# lineno
    934  *	# lineno "filename"
    935  */
    936 static void
    937 directive()
    938 {
    939 	const	char *cp, *fn;
    940 	char	c, *eptr;
    941 	size_t	fnl;
    942 	long	ln;
    943 	static	int first = 1;
    944 
    945 	/* Go to first non-whitespace after # */
    946 	for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) ;
    947 
    948 	if (!isdigit(c)) {
    949 	error:
    950 		/* undefined or invalid # directive */
    951 		warning(255);
    952 		return;
    953 	}
    954 	ln = strtol(--cp, &eptr, 10);
    955 	if (cp == eptr)
    956 		goto error;
    957 	if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
    958 		goto error;
    959 	while ((c = *cp++) == ' ' || c == '\t') ;
    960 	if (c != '\0') {
    961 		if (c != '"')
    962 			goto error;
    963 		fn = cp;
    964 		while ((c = *cp) != '"' && c != '\0')
    965 			cp++;
    966 		if (c != '"')
    967 			goto error;
    968 		if ((fnl = cp++ - fn) > PATH_MAX)
    969 			goto error;
    970 		while ((c = *cp++) == ' ' || c == '\t') ;
    971 #if 0
    972 		if (c != '\0')
    973 			warning("extra character(s) after directive");
    974 #endif
    975 		curr_pos.p_file = fnnalloc(fn, fnl);
    976 		/*
    977 		 * If this is the first directive, the name is the name
    978 		 * of the C source file as specified at the command line.
    979 		 * It is written to the output file.
    980 		 */
    981 		if (first) {
    982 			csrc_pos.p_file = curr_pos.p_file;
    983 			outsrc(curr_pos.p_file);
    984 			first = 0;
    985 		}
    986 	}
    987 	curr_pos.p_line = (int)ln - 1;
    988 	if (curr_pos.p_file == csrc_pos.p_file)
    989 		csrc_pos.p_line = (int)ln - 1;
    990 }
    991 
    992 /*
    993  * Handle lint comments. Following comments are currently understood:
    994  *	ARGSUSEDn
    995  *	CONSTCOND CONSTANTCOND CONSTANTCONDITION
    996  *	FALLTHRU FALLTHROUGH
    997  *	LINTLIBRARY
    998  *	LINTED NOSTRICT
    999  *	NOTREACHED
   1000  *	PRINTFLIKEn
   1001  *	PROTOLIB
   1002  *	SCANFLIKEn
   1003  *	VARARGSn
   1004  * If one of this comments is recognized, the arguments, if any, are
   1005  * parsed and a function which handles this comment is called.
   1006  */
   1007 static void
   1008 comment()
   1009 {
   1010 	int	c, lc;
   1011 	static struct {
   1012 		const	char *keywd;
   1013 		int	arg;
   1014 		void	(*func) __P((int));
   1015 	} keywtab[] = {
   1016 		{ "ARGSUSED",		1,	argsused	},
   1017 		{ "CONSTCOND",		0,	constcond	},
   1018 		{ "CONSTANTCOND",	0,	constcond	},
   1019 		{ "CONSTANTCONDITION",	0,	constcond	},
   1020 		{ "FALLTHRU",		0,	fallthru	},
   1021 		{ "FALLTHROUGH",	0,	fallthru	},
   1022 		{ "LINTLIBRARY",	0,	lintlib		},
   1023 		{ "LINTED",		0,	linted		},
   1024 		{ "NOSTRICT",		0,	linted		},
   1025 		{ "NOTREACHED",		0,	notreach	},
   1026 		{ "PRINTFLIKE",		1,	printflike	},
   1027 		{ "PROTOLIB",		1,	protolib	},
   1028 		{ "SCANFLIKE",		1,	scanflike	},
   1029 		{ "VARARGS",		1,	varargs		},
   1030 	};
   1031 	char	keywd[32];
   1032 	char	arg[32];
   1033 	int	l, i, a;
   1034 	int	eoc;
   1035 
   1036 	eoc = 0;
   1037 
   1038 	/* Skip white spaces after the start of the comment */
   1039 	while ((c = inpc()) != EOF && isspace(c)) ;
   1040 
   1041 	/* Read the potential keyword to keywd */
   1042 	l = 0;
   1043 	while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
   1044 		keywd[l++] = (char)c;
   1045 		c = inpc();
   1046 	}
   1047 	keywd[l] = '\0';
   1048 
   1049 	/* look for the keyword */
   1050 	for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
   1051 		if (strcmp(keywtab[i].keywd, keywd) == 0)
   1052 			break;
   1053 	}
   1054 	if (i == sizeof (keywtab) / sizeof (keywtab[0]))
   1055 		goto skip_rest;
   1056 
   1057 	/* skip white spaces after the keyword */
   1058 	while (c != EOF && isspace(c))
   1059 		c = inpc();
   1060 
   1061 	/* read the argument, if the keyword accepts one and there is one */
   1062 	l = 0;
   1063 	if (keywtab[i].arg) {
   1064 		while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
   1065 			arg[l++] = (char)c;
   1066 			c = inpc();
   1067 		}
   1068 	}
   1069 	arg[l] = '\0';
   1070 	a = l != 0 ? atoi(arg) : -1;
   1071 
   1072 	/* skip white spaces after the argument */
   1073 	while (c != EOF && isspace(c))
   1074 		c = inpc();
   1075 
   1076 	if (c != '*' || (c = inpc()) != '/') {
   1077 		if (keywtab[i].func != linted)
   1078 			/* extra characters in lint comment */
   1079 			warning(257);
   1080 	} else {
   1081 		/*
   1082 		 * remember that we have already found the end of the
   1083 		 * comment
   1084 		 */
   1085 		eoc = 1;
   1086 	}
   1087 
   1088 	if (keywtab[i].func != NULL)
   1089 		(*keywtab[i].func)(a);
   1090 
   1091  skip_rest:
   1092 	while (!eoc) {
   1093 		lc = c;
   1094 		if ((c = inpc()) == EOF) {
   1095 			/* unterminated comment */
   1096 			error(256);
   1097 			break;
   1098 		}
   1099 		if (lc == '*' && c == '/')
   1100 			eoc = 1;
   1101 	}
   1102 }
   1103 
   1104 /*
   1105  * Strings are stored in a dynamically alloceted buffer and passed
   1106  * in yylval.y_xstrg to the parser. The parser or the routines called
   1107  * by the parser are responsible for freeing this buffer.
   1108  */
   1109 static int
   1110 string()
   1111 {
   1112 	u_char	*s;
   1113 	int	c;
   1114 	size_t	len, max;
   1115 	strg_t	*strg;
   1116 
   1117 	s = xmalloc(max = 64);
   1118 
   1119 	len = 0;
   1120 	while ((c = getescc('"')) >= 0) {
   1121 		/* +1 to reserve space for a trailing NUL character */
   1122 		if (len + 1 == max)
   1123 			s = xrealloc(s, max *= 2);
   1124 		s[len++] = (char)c;
   1125 	}
   1126 	s[len] = '\0';
   1127 	if (c == -2)
   1128 		/* unterminated string constant */
   1129 		error(258);
   1130 
   1131 	strg = xcalloc(1, sizeof (strg_t));
   1132 	strg->st_tspec = CHAR;
   1133 	strg->st_len = len;
   1134 	strg->st_cp = s;
   1135 
   1136 	yylval.y_strg = strg;
   1137 	return (T_STRING);
   1138 }
   1139 
   1140 static int
   1141 wcstrg()
   1142 {
   1143 	char	*s;
   1144 	int	c, i, n, wi;
   1145 	size_t	len, max, wlen;
   1146 	wchar_t	*ws;
   1147 	strg_t	*strg;
   1148 
   1149 	s = xmalloc(max = 64);
   1150 	len = 0;
   1151 	while ((c = getescc('"')) >= 0) {
   1152 		/* +1 to save space for a trailing NUL character */
   1153 		if (len + 1 >= max)
   1154 			s = xrealloc(s, max *= 2);
   1155 		s[len++] = (char)c;
   1156 	}
   1157 	s[len] = '\0';
   1158 	if (c == -2)
   1159 		/* unterminated string constant */
   1160 		error(258);
   1161 
   1162 	/* get length of wide character string */
   1163 	(void)mblen(NULL, 0);
   1164 	for (i = 0, wlen = 0; i < len; i += n, wlen++) {
   1165 		if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
   1166 			/* invalid multibyte character */
   1167 			error(291);
   1168 			break;
   1169 		}
   1170 		if (n == 0)
   1171 			n = 1;
   1172 	}
   1173 
   1174 	ws = xmalloc((wlen + 1) * sizeof (wchar_t));
   1175 
   1176 	/* convert from multibyte to wide char */
   1177 	(void)mbtowc(NULL, NULL, 0);
   1178 	for (i = 0, wi = 0; i < len; i += n, wi++) {
   1179 		if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
   1180 			break;
   1181 		if (n == 0)
   1182 			n = 1;
   1183 	}
   1184 	ws[wi] = 0;
   1185 	free(s);
   1186 
   1187 	strg = xcalloc(1, sizeof (strg_t));
   1188 	strg->st_tspec = WCHAR;
   1189 	strg->st_len = wlen;
   1190 	strg->st_wcp = ws;
   1191 
   1192 	yylval.y_strg = strg;
   1193 	return (T_STRING);
   1194 }
   1195 
   1196 /*
   1197  * As noted above the scanner does not create new symbol table entries
   1198  * for symbols it cannot find in the symbol table. This is to avoid
   1199  * putting undeclared symbols into the symbol table if a syntax error
   1200  * occurs.
   1201  *
   1202  * getsym() is called as soon as it is probably ok to put the symbol to
   1203  * the symbol table. This does not mean that it is not possible that
   1204  * symbols are put to the symbol table which are than not completely
   1205  * declared due to syntax errors. To avoid too many problems in this
   1206  * case symbols get type int in getsym().
   1207  *
   1208  * XXX calls to getsym() should be delayed until decl1*() is called
   1209  */
   1210 sym_t *
   1211 getsym(sb)
   1212 	sbuf_t	*sb;
   1213 {
   1214 	dinfo_t	*di;
   1215 	char	*s;
   1216 	sym_t	*sym;
   1217 
   1218 	sym = sb->sb_sym;
   1219 
   1220 	/*
   1221 	 * During member declaration it is possible that name() looked
   1222 	 * for symbols of type FVFT, although it should have looked for
   1223 	 * symbols of type FTAG. Same can happen for labels. Both cases
   1224 	 * are compensated here.
   1225 	 */
   1226 	if (symtyp == FMOS || symtyp == FLAB) {
   1227 		if (sym == NULL || sym->s_kind == FVFT)
   1228 			sym = search(sb);
   1229 	}
   1230 
   1231 	if (sym != NULL) {
   1232 		if (sym->s_kind != symtyp)
   1233 			lerror("storesym() 1");
   1234 		symtyp = FVFT;
   1235 		freesb(sb);
   1236 		return (sym);
   1237 	}
   1238 
   1239 	/* create a new symbol table entry */
   1240 
   1241 	/* labels must always be allocated at level 1 (outhermost block) */
   1242 	if (symtyp == FLAB) {
   1243 		sym = getlblk(1, sizeof (sym_t));
   1244 		s = getlblk(1, sb->sb_len + 1);
   1245 		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
   1246 		sym->s_name = s;
   1247 		sym->s_blklev = 1;
   1248 		di = dcs;
   1249 		while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
   1250 			di = di->d_nxt;
   1251 		if (di->d_ctx != AUTO)
   1252 			lerror("storesym() 2");
   1253 	} else {
   1254 		sym = getblk(sizeof (sym_t));
   1255 		sym->s_name = sb->sb_name;
   1256 		sym->s_blklev = blklev;
   1257 		di = dcs;
   1258 	}
   1259 
   1260 	STRUCT_ASSIGN(sym->s_dpos, curr_pos);
   1261 	if ((sym->s_kind = symtyp) != FLAB)
   1262 		sym->s_type = gettyp(INT);
   1263 
   1264 	symtyp = FVFT;
   1265 
   1266 	if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
   1267 		symtab[sb->sb_hash]->s_rlink = &sym->s_link;
   1268 	(symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
   1269 
   1270 	*di->d_ldlsym = sym;
   1271 	di->d_ldlsym = &sym->s_dlnxt;
   1272 
   1273 	freesb(sb);
   1274 	return (sym);
   1275 }
   1276 
   1277 /*
   1278  * Remove a symbol forever from the symbol table. s_blklev
   1279  * is set to -1 to avoid that the symbol will later be put
   1280  * back to the symbol table.
   1281  */
   1282 void
   1283 rmsym(sym)
   1284 	sym_t	*sym;
   1285 {
   1286 	if ((*sym->s_rlink = sym->s_link) != NULL)
   1287 		sym->s_link->s_rlink = sym->s_rlink;
   1288 	sym->s_blklev = -1;
   1289 	sym->s_link = NULL;
   1290 }
   1291 
   1292 /*
   1293  * Remove a list of symbols declared at one level from the symbol
   1294  * table.
   1295  */
   1296 void
   1297 rmsyms(syms)
   1298 	sym_t	*syms;
   1299 {
   1300 	sym_t	*sym;
   1301 
   1302 	for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
   1303 		if (sym->s_blklev != -1) {
   1304 			if ((*sym->s_rlink = sym->s_link) != NULL)
   1305 				sym->s_link->s_rlink = sym->s_rlink;
   1306 			sym->s_link = NULL;
   1307 			sym->s_rlink = NULL;
   1308 		}
   1309 	}
   1310 }
   1311 
   1312 /*
   1313  * Put a symbol into the symbol table
   1314  */
   1315 void
   1316 inssym(bl, sym)
   1317 	int	bl;
   1318 	sym_t	*sym;
   1319 {
   1320 	int	h;
   1321 
   1322 	h = hash(sym->s_name);
   1323 	if ((sym->s_link = symtab[h]) != NULL)
   1324 		symtab[h]->s_rlink = &sym->s_link;
   1325 	(symtab[h] = sym)->s_rlink = &symtab[h];
   1326 	sym->s_blklev = bl;
   1327 	if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
   1328 		lerror("inssym()");
   1329 }
   1330 
   1331 /*
   1332  * Called at level 0 after syntax errors
   1333  * Removes all symbols which are not declared at level 0 from the
   1334  * symbol table. Also frees all memory which is not associated with
   1335  * level 0.
   1336  */
   1337 void
   1338 cleanup()
   1339 {
   1340 	sym_t	*sym, *nsym;
   1341 	int	i;
   1342 
   1343 	for (i = 0; i < HSHSIZ1; i++) {
   1344 		for (sym = symtab[i]; sym != NULL; sym = nsym) {
   1345 			nsym = sym->s_link;
   1346 			if (sym->s_blklev >= 1) {
   1347 				if ((*sym->s_rlink = nsym) != NULL)
   1348 					nsym->s_rlink = sym->s_rlink;
   1349 			}
   1350 		}
   1351 	}
   1352 
   1353 	for (i = mblklev; i > 0; i--)
   1354 		freelblk(i);
   1355 }
   1356 
   1357 /*
   1358  * Create a new symbol with the name of an existing symbol.
   1359  */
   1360 sym_t *
   1361 pushdown(sym)
   1362 	sym_t	*sym;
   1363 {
   1364 	int	h;
   1365 	sym_t	*nsym;
   1366 
   1367 	h = hash(sym->s_name);
   1368 	nsym = getblk(sizeof (sym_t));
   1369 	if (sym->s_blklev > blklev)
   1370 		lerror("pushdown()");
   1371 	nsym->s_name = sym->s_name;
   1372 	STRUCT_ASSIGN(nsym->s_dpos, curr_pos);
   1373 	nsym->s_kind = sym->s_kind;
   1374 	nsym->s_blklev = blklev;
   1375 
   1376 	if ((nsym->s_link = symtab[h]) != NULL)
   1377 		symtab[h]->s_rlink = &nsym->s_link;
   1378 	(symtab[h] = nsym)->s_rlink = &symtab[h];
   1379 
   1380 	*dcs->d_ldlsym = nsym;
   1381 	dcs->d_ldlsym = &nsym->s_dlnxt;
   1382 
   1383 	return (nsym);
   1384 }
   1385 
   1386 /*
   1387  * Free any dynamically allocated memory referenced by
   1388  * the value stack or yylval.
   1389  * The type of information in yylval is described by tok.
   1390  */
   1391 void
   1392 freeyyv(sp, tok)
   1393 	void	*sp;
   1394 	int	tok;
   1395 {
   1396 	if (tok == T_NAME || tok == T_TYPENAME) {
   1397 		sbuf_t *sb = *(sbuf_t **)sp;
   1398 		freesb(sb);
   1399 	} else if (tok == T_CON) {
   1400 		val_t *val = *(val_t **)sp;
   1401 		free(val);
   1402 	} else if (tok == T_STRING) {
   1403 		strg_t *strg = *(strg_t **)sp;
   1404 		if (strg->st_tspec == CHAR) {
   1405 			free(strg->st_cp);
   1406 		} else if (strg->st_tspec == WCHAR) {
   1407 			free(strg->st_wcp);
   1408 		} else {
   1409 			lerror("fryylv() 1");
   1410 		}
   1411 		free(strg);
   1412 	}
   1413 }
   1414