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