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