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