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