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