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