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