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