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