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