xref: /netbsd-src/usr.bin/xlint/lint1/scan.l (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 %{
2 /* $NetBSD: scan.l,v 1.38 2008/04/25 22:18:34 christos 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.38 2008/04/25 22:18:34 christos 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 QUAD:
568 		if (uq > TARG_QUAD_MAX && !tflag) {
569 			typ = UQUAD;
570 			if (!sflag)
571 				ansiu = 1;
572 		}
573 		break;
574 		/* LINTED (enumeration values not handled in switch) */
575 	case STRUCT:
576 	case VOID:
577 	case LDOUBLE:
578 	case FUNC:
579 	case ARRAY:
580 	case PTR:
581 	case ENUM:
582 	case UNION:
583 	case SIGNED:
584 	case NOTSPEC:
585 	case DOUBLE:
586 	case FLOAT:
587 	case UQUAD:
588 	case ULONG:
589 	case USHORT:
590 	case SHORT:
591 	case UCHAR:
592 	case SCHAR:
593 	case CHAR:
594 	case BOOL:
595 	case UNSIGN:
596 	case FCOMPLEX:
597 	case DCOMPLEX:
598 	case COMPLEX:
599 		break;
600 
601 	case NTSPEC:	/* this value unused */
602 		break;
603 	}
604 
605 	uq = (uint64_t)xsign((int64_t)uq, typ, -1);
606 
607 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
608 	yylval.y_val->v_ansiu = ansiu;
609 	yylval.y_val->v_quad = (int64_t)uq;
610 
611 	return (T_CON);
612 }
613 
614 /*
615  * Returns 1 if t is a signed type and the value is negative.
616  *
617  * len is the number of significant bits. If len is -1, len is set
618  * to the width of type t.
619  */
620 int
621 sign(int64_t q, tspec_t t, int len)
622 {
623 
624 	if (t == PTR || isutyp(t))
625 		return (0);
626 	return (msb(q, t, len));
627 }
628 
629 int
630 msb(int64_t q, tspec_t t, int len)
631 {
632 
633 	if (len <= 0)
634 		len = size(t);
635 	return ((q & qbmasks[len - 1]) != 0);
636 }
637 
638 /*
639  * Extends the sign of q.
640  */
641 int64_t
642 xsign(int64_t q, tspec_t t, int len)
643 {
644 
645 	if (len <= 0)
646 		len = size(t);
647 
648 	if (t == PTR || isutyp(t) || !sign(q, t, len)) {
649 		q &= qlmasks[len];
650 	} else {
651 		q |= qumasks[len];
652 	}
653 	return (q);
654 }
655 
656 /*
657  * Convert a string representing a floating point value into its interal
658  * representation. Type and value are returned in yylval. fcon()
659  * (and yylex()) returns T_CON.
660  * XXX Currently it is not possible to convert constants of type
661  * long double which are greater than DBL_MAX.
662  */
663 static int
664 fcon(void)
665 {
666 	const	char *cp;
667 	int	len;
668 	tspec_t typ;
669 	char	c, *eptr;
670 	double	d;
671 	float	f = 0;
672 
673 	cp = yytext;
674 	len = yyleng;
675 
676 	if (cp[len - 1] == 'i') {
677 		/* imaginary, do nothing for now */
678 		len--;
679 	}
680 	if ((c = cp[len - 1]) == 'f' || c == 'F') {
681 		typ = FLOAT;
682 		len--;
683 	} else if (c == 'l' || c == 'L') {
684 		typ = LDOUBLE;
685 		len--;
686 	} else {
687 		typ = DOUBLE;
688 	}
689 
690 	if (tflag && typ != DOUBLE) {
691 		/* suffixes F and L are illegal in traditional C */
692 		warning(98);
693 	}
694 
695 	errno = 0;
696 	d = strtod(cp, &eptr);
697 	if (eptr != cp + len) {
698 		switch (*eptr) {
699 		/*
700 		 * XXX: non-native non-current strtod() may not handle hex
701 		 * floats, ignore the rest if we find traces of hex float
702 		 * syntax...
703 		 */
704 		case 'p':
705 		case 'P':
706 		case 'x':
707 		case 'X':
708 			d = 0;
709 			errno = 0;
710 			break;
711 		default:
712 			LERROR("fcon()");
713 		}
714 	}
715 	if (errno != 0)
716 		/* floating-point constant out of range */
717 		warning(248);
718 
719 	if (typ == FLOAT) {
720 		f = (float)d;
721 		if (!finite(f)) {
722 			/* floating-point constant out of range */
723 			warning(248);
724 			f = f > 0 ? FLT_MAX : -FLT_MAX;
725 		}
726 	}
727 
728 	(yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
729 	if (typ == FLOAT) {
730 		yylval.y_val->v_ldbl = f;
731 	} else {
732 		yylval.y_val->v_ldbl = d;
733 	}
734 
735 	return (T_CON);
736 }
737 
738 static int
739 operator(int t, op_t o)
740 {
741 
742 	yylval.y_op = o;
743 	return (t);
744 }
745 
746 /*
747  * Called if lex found a leading \'.
748  */
749 static int
750 ccon(void)
751 {
752 	int	n, val, c;
753 	char	cv;
754 
755 	n = 0;
756 	val = 0;
757 	while ((c = getescc('\'')) >= 0) {
758 		val = (val << CHAR_BIT) + c;
759 		n++;
760 	}
761 	if (c == -2) {
762 		/* unterminated character constant */
763 		error(253);
764 	} else {
765 		if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
766 			/* too many characters in character constant */
767 			error(71);
768 		} else if (n > 1) {
769 			/* multi-character character constant */
770 			warning(294);
771 		} else if (n == 0) {
772 			/* empty character constant */
773 			error(73);
774 		}
775 	}
776 	if (n == 1) {
777 		cv = (char)val;
778 		val = cv;
779 	}
780 
781 	yylval.y_val = xcalloc(1, sizeof (val_t));
782 	yylval.y_val->v_tspec = INT;
783 	yylval.y_val->v_quad = val;
784 
785 	return (T_CON);
786 }
787 
788 /*
789  * Called if lex found a leading L\'
790  */
791 static int
792 wccon(void)
793 {
794 	static	char buf[MB_LEN_MAX + 1];
795 	int	i, c;
796 	wchar_t	wc;
797 
798 	i = 0;
799 	while ((c = getescc('\'')) >= 0) {
800 		if (i < MB_CUR_MAX)
801 			buf[i] = (char)c;
802 		i++;
803 	}
804 
805 	wc = 0;
806 
807 	if (c == -2) {
808 		/* unterminated character constant */
809 		error(253);
810 	} else if (c == 0) {
811 		/* empty character constant */
812 		error(73);
813 	} else {
814 		if (i > MB_CUR_MAX) {
815 			i = MB_CUR_MAX;
816 			/* too many characters in character constant */
817 			error(71);
818 		} else {
819 			buf[i] = '\0';
820 			(void)mbtowc(NULL, NULL, 0);
821 			if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
822 				/* invalid multibyte character */
823 				error(291);
824 		}
825 	}
826 
827 	yylval.y_val = xcalloc(1, sizeof (val_t));
828 	yylval.y_val->v_tspec = WCHAR;
829 	yylval.y_val->v_quad = wc;
830 
831 	return (T_CON);
832 }
833 
834 /*
835  * Read a character which is part of a character constant or of a string
836  * and handle escapes.
837  *
838  * The Argument is the character which delimits the character constant or
839  * string.
840  *
841  * Returns -1 if the end of the character constant or string is reached,
842  * -2 if the EOF is reached, and the character otherwise.
843  */
844 static int
845 getescc(int d)
846 {
847 	static	int pbc = -1;
848 	int	n, c, v;
849 
850 	if (pbc == -1) {
851 		c = inpc();
852 	} else {
853 		c = pbc;
854 		pbc = -1;
855 	}
856 	if (c == d)
857 		return (-1);
858 	switch (c) {
859 	case '\n':
860 		if (tflag) {
861 			/* newline in string or char constant */
862 			error(254);
863 			return (-2);
864 		}
865 		return (c);
866 	case EOF:
867 		return (-2);
868 	case '\\':
869 		switch (c = inpc()) {
870 		case '"':
871 			if (tflag && d == '\'')
872 				/* \" inside character constant undef. ... */
873 				warning(262);
874 			return ('"');
875 		case '\'':
876 			return ('\'');
877 		case '?':
878 			if (tflag)
879 				/* \? undefined in traditional C */
880 				warning(263);
881 			return ('?');
882 		case '\\':
883 			return ('\\');
884 		case 'a':
885 			if (tflag)
886 				/* \a undefined in traditional C */
887 				warning(81);
888 			return ('\a');
889 		case 'b':
890 			return ('\b');
891 		case 'f':
892 			return ('\f');
893 		case 'n':
894 			return ('\n');
895 		case 'r':
896 			return ('\r');
897 		case 't':
898 			return ('\t');
899 		case 'v':
900 			if (tflag)
901 				/* \v undefined in traditional C */
902 				warning(264);
903 			return ('\v');
904 		case '8': case '9':
905 			/* bad octal digit %c */
906 			warning(77, c);
907 			/* FALLTHROUGH */
908 		case '0': case '1': case '2': case '3':
909 		case '4': case '5': case '6': case '7':
910 			n = 3;
911 			v = 0;
912 			do {
913 				v = (v << 3) + (c - '0');
914 				c = inpc();
915 			} while (--n && isdigit(c) && (tflag || c <= '7'));
916 			if (tflag && n > 0 && isdigit(c))
917 				/* bad octal digit %c */
918 				warning(77, c);
919 			pbc = c;
920 			if (v > UCHAR_MAX) {
921 				/* character escape does not fit in char. */
922 				warning(76);
923 				v &= CHAR_MASK;
924 			}
925 			return (v);
926 		case 'x':
927 			if (tflag)
928 				/* \x undefined in traditional C */
929 				warning(82);
930 			v = 0;
931 			n = 0;
932 			while ((c = inpc()) >= 0 && isxdigit(c)) {
933 				c = isdigit(c) ?
934 					c - '0' : toupper(c) - 'A' + 10;
935 				v = (v << 4) + c;
936 				if (n >= 0) {
937 					if ((v & ~CHAR_MASK) != 0) {
938 						/* overflow in hex escape */
939 						warning(75);
940 						n = -1;
941 					} else {
942 						n++;
943 					}
944 				}
945 			}
946 			pbc = c;
947 			if (n == 0) {
948 				/* no hex digits follow \x */
949 				error(74);
950 			} if (n == -1) {
951 				v &= CHAR_MASK;
952 			}
953 			return (v);
954 		case '\n':
955 			return (getescc(d));
956 		case EOF:
957 			return (-2);
958 		default:
959 			if (isprint(c)) {
960 				/* dubious escape \%c */
961 				warning(79, c);
962 			} else {
963 				/* dubious escape \%o */
964 				warning(80, c);
965 			}
966 		}
967 	}
968 	return (c);
969 }
970 
971 /*
972  * Called for preprocessor directives. Currently implemented are:
973  *	# lineno
974  *	# lineno "filename"
975  */
976 static void
977 directive(void)
978 {
979 	const	char *cp, *fn;
980 	char	c, *eptr;
981 	size_t	fnl;
982 	long	ln;
983 	static	int first = 1;
984 
985 	/* Go to first non-whitespace after # */
986 	for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
987 		continue;
988 
989 	if (!isdigit((unsigned char)c)) {
990 	error:
991 		/* undefined or invalid # directive */
992 		warning(255);
993 		return;
994 	}
995 	ln = strtol(--cp, &eptr, 10);
996 	if (cp == eptr)
997 		goto error;
998 	if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
999 		goto error;
1000 	while ((c = *cp++) == ' ' || c == '\t')
1001 		continue;
1002 	if (c != '\0') {
1003 		if (c != '"')
1004 			goto error;
1005 		fn = cp;
1006 		while ((c = *cp) != '"' && c != '\0')
1007 			cp++;
1008 		if (c != '"')
1009 			goto error;
1010 		if ((fnl = cp++ - fn) > PATH_MAX)
1011 			goto error;
1012 		while ((c = *cp++) == ' ' || c == '\t')
1013 			continue;
1014 #if 0
1015 		if (c != '\0')
1016 			warning("extra character(s) after directive");
1017 #endif
1018 
1019 		/* empty string means stdin */
1020 		if (fnl == 0) {
1021 			fn = "{standard input}";
1022 			fnl = 16;			/* strlen (fn) */
1023 		}
1024 		curr_pos.p_file = fnnalloc(fn, fnl);
1025 		/*
1026 		 * If this is the first directive, the name is the name
1027 		 * of the C source file as specified at the command line.
1028 		 * It is written to the output file.
1029 		 */
1030 		if (first) {
1031 			csrc_pos.p_file = curr_pos.p_file;
1032 			outsrc(curr_pos.p_file);
1033 			first = 0;
1034 		}
1035 	}
1036 	curr_pos.p_line = (int)ln - 1;
1037 	curr_pos.p_uniq = 0;
1038 	if (curr_pos.p_file == csrc_pos.p_file) {
1039 		csrc_pos.p_line = (int)ln - 1;
1040 		csrc_pos.p_uniq = 0;
1041 	}
1042 }
1043 
1044 /*
1045  * Handle lint comments. Following comments are currently understood:
1046  *	ARGSUSEDn
1047  *	BITFIELDTYPE
1048  *	CONSTCOND CONSTANTCOND CONSTANTCONDITION
1049  *	FALLTHRU FALLTHROUGH
1050  *	LINTLIBRARY
1051  *	LINTED NOSTRICT
1052  *	LONGLONG
1053  *	NOTREACHED
1054  *	PRINTFLIKEn
1055  *	PROTOLIB
1056  *	SCANFLIKEn
1057  *	VARARGSn
1058  * If one of this comments is recognized, the arguments, if any, are
1059  * parsed and a function which handles this comment is called.
1060  */
1061 static void
1062 comment(void)
1063 {
1064 	int	c, lc;
1065 	static struct {
1066 		const	char *keywd;
1067 		int	arg;
1068 		void	(*func)(int);
1069 	} keywtab[] = {
1070 		{ "ARGSUSED",		1,	argsused	},
1071 		{ "BITFIELDTYPE",	0,	bitfieldtype	},
1072 		{ "CONSTCOND",		0,	constcond	},
1073 		{ "CONSTANTCOND",	0,	constcond	},
1074 		{ "CONSTANTCONDITION",	0,	constcond	},
1075 		{ "FALLTHRU",		0,	fallthru	},
1076 		{ "FALLTHROUGH",	0,	fallthru	},
1077 		{ "LINTLIBRARY",	0,	lintlib		},
1078 		{ "LINTED",		0,	linted		},
1079 		{ "LONGLONG",		0,	longlong	},
1080 		{ "NOSTRICT",		0,	linted		},
1081 		{ "NOTREACHED",		0,	notreach	},
1082 		{ "PRINTFLIKE",		1,	printflike	},
1083 		{ "PROTOLIB",		1,	protolib	},
1084 		{ "SCANFLIKE",		1,	scanflike	},
1085 		{ "VARARGS",		1,	varargs		},
1086 	};
1087 	char	keywd[32];
1088 	char	arg[32];
1089 	int	l, i, a;
1090 	int	eoc;
1091 
1092 	eoc = 0;
1093 
1094 	/* Skip white spaces after the start of the comment */
1095 	while ((c = inpc()) != EOF && isspace(c))
1096 		continue;
1097 
1098 	/* Read the potential keyword to keywd */
1099 	l = 0;
1100 	while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1101 		keywd[l++] = (char)c;
1102 		c = inpc();
1103 	}
1104 	keywd[l] = '\0';
1105 
1106 	/* look for the keyword */
1107 	for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1108 		if (strcmp(keywtab[i].keywd, keywd) == 0)
1109 			break;
1110 	}
1111 	if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1112 		goto skip_rest;
1113 
1114 	/* skip white spaces after the keyword */
1115 	while (c != EOF && isspace(c))
1116 		c = inpc();
1117 
1118 	/* read the argument, if the keyword accepts one and there is one */
1119 	l = 0;
1120 	if (keywtab[i].arg) {
1121 		while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1122 			arg[l++] = (char)c;
1123 			c = inpc();
1124 		}
1125 	}
1126 	arg[l] = '\0';
1127 	a = l != 0 ? atoi(arg) : -1;
1128 
1129 	/* skip white spaces after the argument */
1130 	while (c != EOF && isspace(c))
1131 		c = inpc();
1132 
1133 	if (c != '*' || (c = inpc()) != '/') {
1134 		if (keywtab[i].func != linted)
1135 			/* extra characters in lint comment */
1136 			warning(257);
1137 	} else {
1138 		/*
1139 		 * remember that we have already found the end of the
1140 		 * comment
1141 		 */
1142 		eoc = 1;
1143 	}
1144 
1145 	if (keywtab[i].func != NULL)
1146 		(*keywtab[i].func)(a);
1147 
1148  skip_rest:
1149 	while (!eoc) {
1150 		lc = c;
1151 		if ((c = inpc()) == EOF) {
1152 			/* unterminated comment */
1153 			error(256);
1154 			break;
1155 		}
1156 		if (lc == '*' && c == '/')
1157 			eoc = 1;
1158 	}
1159 }
1160 
1161 /*
1162  * Handle // style comments
1163  */
1164 static void
1165 slashslashcomment(void)
1166 {
1167 	int c;
1168 
1169 	if (!Sflag && !gflag)
1170 		/* // comments only supported in C99 */
1171 		(void)gnuism(312, tflag ? "traditional" : "ANSI");
1172 
1173 	while ((c = inpc()) != EOF && c != '\n')
1174 		continue;
1175 }
1176 
1177 /*
1178  * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1179  * clrwflgs() is called after function definitions and global and
1180  * local declarations and definitions. It is also called between
1181  * the controlling expression and the body of control statements
1182  * (if, switch, for, while).
1183  */
1184 void
1185 clrwflgs(void)
1186 {
1187 
1188 	nowarn = 0;
1189 	quadflg = 0;
1190 	ccflg = 0;
1191 }
1192 
1193 /*
1194  * Strings are stored in a dynamically alloceted buffer and passed
1195  * in yylval.y_xstrg to the parser. The parser or the routines called
1196  * by the parser are responsible for freeing this buffer.
1197  */
1198 static int
1199 string(void)
1200 {
1201 	u_char	*s;
1202 	int	c;
1203 	size_t	len, max;
1204 	strg_t	*strg;
1205 
1206 	s = xmalloc(max = 64);
1207 
1208 	len = 0;
1209 	while ((c = getescc('"')) >= 0) {
1210 		/* +1 to reserve space for a trailing NUL character */
1211 		if (len + 1 == max)
1212 			s = xrealloc(s, max *= 2);
1213 		s[len++] = (char)c;
1214 	}
1215 	s[len] = '\0';
1216 	if (c == -2)
1217 		/* unterminated string constant */
1218 		error(258);
1219 
1220 	strg = xcalloc(1, sizeof (strg_t));
1221 	strg->st_tspec = CHAR;
1222 	strg->st_len = len;
1223 	strg->st_cp = s;
1224 
1225 	yylval.y_strg = strg;
1226 	return (T_STRING);
1227 }
1228 
1229 static int
1230 wcstrg(void)
1231 {
1232 	char	*s;
1233 	int	c, i, n, wi;
1234 	size_t	len, max, wlen;
1235 	wchar_t	*ws;
1236 	strg_t	*strg;
1237 
1238 	s = xmalloc(max = 64);
1239 	len = 0;
1240 	while ((c = getescc('"')) >= 0) {
1241 		/* +1 to save space for a trailing NUL character */
1242 		if (len + 1 >= max)
1243 			s = xrealloc(s, max *= 2);
1244 		s[len++] = (char)c;
1245 	}
1246 	s[len] = '\0';
1247 	if (c == -2)
1248 		/* unterminated string constant */
1249 		error(258);
1250 
1251 	/* get length of wide character string */
1252 	(void)mblen(NULL, 0);
1253 	for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1254 		if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1255 			/* invalid multibyte character */
1256 			error(291);
1257 			break;
1258 		}
1259 		if (n == 0)
1260 			n = 1;
1261 	}
1262 
1263 	ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1264 
1265 	/* convert from multibyte to wide char */
1266 	(void)mbtowc(NULL, NULL, 0);
1267 	for (i = 0, wi = 0; i < len; i += n, wi++) {
1268 		if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1269 			break;
1270 		if (n == 0)
1271 			n = 1;
1272 	}
1273 	ws[wi] = 0;
1274 	free(s);
1275 
1276 	strg = xcalloc(1, sizeof (strg_t));
1277 	strg->st_tspec = WCHAR;
1278 	strg->st_len = wlen;
1279 	strg->st_wcp = ws;
1280 
1281 	yylval.y_strg = strg;
1282 	return (T_STRING);
1283 }
1284 
1285 /*
1286  * As noted above the scanner does not create new symbol table entries
1287  * for symbols it cannot find in the symbol table. This is to avoid
1288  * putting undeclared symbols into the symbol table if a syntax error
1289  * occurs.
1290  *
1291  * getsym() is called as soon as it is probably ok to put the symbol to
1292  * the symbol table. This does not mean that it is not possible that
1293  * symbols are put to the symbol table which are than not completely
1294  * declared due to syntax errors. To avoid too many problems in this
1295  * case symbols get type int in getsym().
1296  *
1297  * XXX calls to getsym() should be delayed until decl1*() is called
1298  */
1299 sym_t *
1300 getsym(sbuf_t *sb)
1301 {
1302 	dinfo_t	*di;
1303 	char	*s;
1304 	sym_t	*sym;
1305 
1306 	sym = sb->sb_sym;
1307 
1308 	/*
1309 	 * During member declaration it is possible that name() looked
1310 	 * for symbols of type FVFT, although it should have looked for
1311 	 * symbols of type FTAG. Same can happen for labels. Both cases
1312 	 * are compensated here.
1313 	 */
1314 	if (symtyp == FMOS || symtyp == FLAB) {
1315 		if (sym == NULL || sym->s_kind == FVFT)
1316 			sym = search(sb);
1317 	}
1318 
1319 	if (sym != NULL) {
1320 		if (sym->s_kind != symtyp)
1321 			LERROR("storesym()");
1322 		symtyp = FVFT;
1323 		freesb(sb);
1324 		return (sym);
1325 	}
1326 
1327 	/* create a new symbol table entry */
1328 
1329 	/* labels must always be allocated at level 1 (outhermost block) */
1330 	if (symtyp == FLAB) {
1331 		sym = getlblk(1, sizeof (sym_t));
1332 		s = getlblk(1, sb->sb_len + 1);
1333 		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1334 		sym->s_name = s;
1335 		sym->s_blklev = 1;
1336 		di = dcs;
1337 		while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1338 			di = di->d_nxt;
1339 		if (di->d_ctx != AUTO)
1340 			LERROR("storesym()");
1341 	} else {
1342 		sym = getblk(sizeof (sym_t));
1343 		sym->s_name = sb->sb_name;
1344 		sym->s_blklev = blklev;
1345 		di = dcs;
1346 	}
1347 
1348 	UNIQUE_CURR_POS(sym->s_dpos);
1349 	if ((sym->s_kind = symtyp) != FLAB)
1350 		sym->s_type = gettyp(INT);
1351 
1352 	symtyp = FVFT;
1353 
1354 	if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1355 		symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1356 	(symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1357 
1358 	*di->d_ldlsym = sym;
1359 	di->d_ldlsym = &sym->s_dlnxt;
1360 
1361 	freesb(sb);
1362 	return (sym);
1363 }
1364 
1365 /*
1366  * Construct a temporary symbol. The symbol starts with a digit, so that
1367  * it is illegal.
1368  */
1369 sym_t *
1370 mktempsym(type_t *t)
1371 {
1372 	static int n = 0;
1373 	int h;
1374 	char *s = getlblk(blklev, 64);
1375 	sym_t *sym = getblk(sizeof (sym_t));
1376 
1377 	(void)snprintf(s, 64, "%.8d_tmp", n++);
1378 	h = hash(s);
1379 
1380 	sym->s_name = s;
1381 	sym->s_type = t;
1382 	sym->s_blklev = blklev;
1383 	sym->s_scl = AUTO;
1384 	sym->s_kind = FVFT;
1385 	sym->s_used = 1;
1386 	sym->s_set = 1;
1387 
1388 	if ((sym->s_link = symtab[h]) != NULL)
1389 		symtab[h]->s_rlink = &sym->s_link;
1390 	(symtab[h] = sym)->s_rlink = &symtab[h];
1391 
1392 	*dcs->d_ldlsym = sym;
1393 	dcs->d_ldlsym = &sym->s_dlnxt;
1394 
1395 	return sym;
1396 }
1397 
1398 /*
1399  * Remove a symbol forever from the symbol table. s_blklev
1400  * is set to -1 to avoid that the symbol will later be put
1401  * back to the symbol table.
1402  */
1403 void
1404 rmsym(sym_t *sym)
1405 {
1406 
1407 	if ((*sym->s_rlink = sym->s_link) != NULL)
1408 		sym->s_link->s_rlink = sym->s_rlink;
1409 	sym->s_blklev = -1;
1410 	sym->s_link = NULL;
1411 }
1412 
1413 /*
1414  * Remove a list of symbols declared at one level from the symbol
1415  * table.
1416  */
1417 void
1418 rmsyms(sym_t *syms)
1419 {
1420 	sym_t	*sym;
1421 
1422 	for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1423 		if (sym->s_blklev != -1) {
1424 			if ((*sym->s_rlink = sym->s_link) != NULL)
1425 				sym->s_link->s_rlink = sym->s_rlink;
1426 			sym->s_link = NULL;
1427 			sym->s_rlink = NULL;
1428 		}
1429 	}
1430 }
1431 
1432 /*
1433  * Put a symbol into the symbol table
1434  */
1435 void
1436 inssym(int bl, sym_t *sym)
1437 {
1438 	int	h;
1439 
1440 	h = hash(sym->s_name);
1441 	if ((sym->s_link = symtab[h]) != NULL)
1442 		symtab[h]->s_rlink = &sym->s_link;
1443 	(symtab[h] = sym)->s_rlink = &symtab[h];
1444 	sym->s_blklev = bl;
1445 	if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1446 		LERROR("inssym()");
1447 }
1448 
1449 /*
1450  * Called at level 0 after syntax errors
1451  * Removes all symbols which are not declared at level 0 from the
1452  * symbol table. Also frees all memory which is not associated with
1453  * level 0.
1454  */
1455 void
1456 cleanup(void)
1457 {
1458 	sym_t	*sym, *nsym;
1459 	int	i;
1460 
1461 	for (i = 0; i < HSHSIZ1; i++) {
1462 		for (sym = symtab[i]; sym != NULL; sym = nsym) {
1463 			nsym = sym->s_link;
1464 			if (sym->s_blklev >= 1) {
1465 				if ((*sym->s_rlink = nsym) != NULL)
1466 					nsym->s_rlink = sym->s_rlink;
1467 			}
1468 		}
1469 	}
1470 
1471 	for (i = mblklev; i > 0; i--)
1472 		freelblk(i);
1473 }
1474 
1475 /*
1476  * Create a new symbol with the name of an existing symbol.
1477  */
1478 sym_t *
1479 pushdown(sym_t *sym)
1480 {
1481 	int	h;
1482 	sym_t	*nsym;
1483 
1484 	h = hash(sym->s_name);
1485 	nsym = getblk(sizeof (sym_t));
1486 	if (sym->s_blklev > blklev)
1487 		LERROR("pushdown()");
1488 	nsym->s_name = sym->s_name;
1489 	UNIQUE_CURR_POS(nsym->s_dpos);
1490 	nsym->s_kind = sym->s_kind;
1491 	nsym->s_blklev = blklev;
1492 
1493 	if ((nsym->s_link = symtab[h]) != NULL)
1494 		symtab[h]->s_rlink = &nsym->s_link;
1495 	(symtab[h] = nsym)->s_rlink = &symtab[h];
1496 
1497 	*dcs->d_ldlsym = nsym;
1498 	dcs->d_ldlsym = &nsym->s_dlnxt;
1499 
1500 	return (nsym);
1501 }
1502 
1503 /*
1504  * Free any dynamically allocated memory referenced by
1505  * the value stack or yylval.
1506  * The type of information in yylval is described by tok.
1507  */
1508 void
1509 freeyyv(void *sp, int tok)
1510 {
1511 	if (tok == T_NAME || tok == T_TYPENAME) {
1512 		sbuf_t *sb = *(sbuf_t **)sp;
1513 		freesb(sb);
1514 	} else if (tok == T_CON) {
1515 		val_t *val = *(val_t **)sp;
1516 		free(val);
1517 	} else if (tok == T_STRING) {
1518 		strg_t *strg = *(strg_t **)sp;
1519 		if (strg->st_tspec == CHAR) {
1520 			free(strg->st_cp);
1521 		} else if (strg->st_tspec == WCHAR) {
1522 			free(strg->st_wcp);
1523 		} else {
1524 			LERROR("fryylv()");
1525 		}
1526 		free(strg);
1527 	}
1528 }
1529