Home | History | Annotate | Line # | Download | only in lint1
lex.c revision 1.103
      1 /* $NetBSD: lex.c,v 1.103 2022/02/27 18:29:14 rillig Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All Rights Reserved.
      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 #if HAVE_NBTOOL_CONFIG_H
     36 #include "nbtool_config.h"
     37 #endif
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(__RCSID) && !defined(lint)
     41 __RCSID("$NetBSD: lex.c,v 1.103 2022/02/27 18:29:14 rillig Exp $");
     42 #endif
     43 
     44 #include <ctype.h>
     45 #include <errno.h>
     46 #include <float.h>
     47 #include <limits.h>
     48 #include <math.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 
     52 #include "lint1.h"
     53 #include "cgram.h"
     54 
     55 #define CHAR_MASK	((1U << CHAR_SIZE) - 1)
     56 
     57 
     58 /* Current position (it's also updated when an included file is parsed) */
     59 pos_t	curr_pos = { "", 1, 0 };
     60 
     61 /*
     62  * Current position in C source (not updated when an included file is
     63  * parsed).
     64  */
     65 pos_t	csrc_pos = { "", 1, 0 };
     66 
     67 bool in_gcc_attribute;
     68 bool in_system_header;
     69 
     70 static	int	inpc(void);
     71 static	unsigned int hash(const char *);
     72 static	sym_t *	search(sbuf_t *);
     73 static	int	keyw(sym_t *);
     74 static	int	get_escaped_char(int);
     75 
     76 void
     77 lex_next_line(void)
     78 {
     79 	curr_pos.p_line++;
     80 	curr_pos.p_uniq = 0;
     81 	debug_step("parsing %s:%d", curr_pos.p_file, curr_pos.p_line);
     82 	if (curr_pos.p_file == csrc_pos.p_file) {
     83 		csrc_pos.p_line++;
     84 		csrc_pos.p_uniq = 0;
     85 	}
     86 }
     87 
     88 void
     89 lex_unknown_character(int c)
     90 {
     91 
     92 	/* unknown character \%o */
     93 	error(250, c);
     94 }
     95 
     96 #define kwdef(name, token, scl, tspec, tqual,	c90, c99, gcc, attr, deco) \
     97 	{ \
     98 		name, token, scl, tspec, tqual, \
     99 		(c90) > 0, (c99) > 0, (gcc) > 0, (attr) > 0, \
    100 		((deco) & 1) != 0, ((deco) & 2) != 0, ((deco) & 4) != 0, \
    101 	}
    102 #define kwdef_token(name, token,		c90, c99, gcc, attr, deco) \
    103 	kwdef(name, token, 0, 0, 0,		c90, c99, gcc, attr, deco)
    104 #define kwdef_sclass(name, sclass,		c90, c99, gcc, attr, deco) \
    105 	kwdef(name, T_SCLASS, sclass, 0, 0,	c90, c99, gcc, attr, deco)
    106 #define kwdef_type(name, tspec,			c90, c99, gcc, attr, deco) \
    107 	kwdef(name, T_TYPE, 0, tspec, 0,	c90, c99, gcc, attr, deco)
    108 #define kwdef_tqual(name, tqual,		c90, c99, gcc, attr, deco) \
    109 	kwdef(name, T_QUAL, 0, 0, tqual,	c90, c99, gcc, attr, deco)
    110 #define kwdef_keyword(name, token) \
    111 	kwdef(name, token, 0, 0, 0,		0, 0, 0, 0, 1)
    112 #define kwdef_gcc_attr(name, token) \
    113 	kwdef(name, token, 0, 0, 0,		0, 0, 1, 1, 5)
    114 
    115 /* During initialization, these keywords are written to the symbol table. */
    116 static struct keyword {
    117 	const	char *kw_name;	/* keyword */
    118 	int	kw_token;	/* token returned by yylex() */
    119 	scl_t	kw_scl;		/* storage class if kw_token T_SCLASS */
    120 	tspec_t	kw_tspec;	/* type spec. if kw_token
    121 				 * T_TYPE or T_STRUCT_OR_UNION */
    122 	tqual_t	kw_tqual;	/* type qual. if kw_token T_QUAL */
    123 	bool	kw_c90:1;	/* C90 keyword */
    124 	bool	kw_c99:1;	/* C99 keyword */
    125 	bool	kw_gcc:1;	/* GCC keyword */
    126 	bool	kw_attr:1;	/* GCC attribute, keyword */
    127 	bool	kw_plain:1;	/* 'name' */
    128 	bool	kw_leading:1;	/* '__name' */
    129 	bool	kw_both:1;	/* '__name__' */
    130 } keywords[] = {
    131 	kwdef_gcc_attr(	"alias",	T_AT_ALIAS),
    132 	kwdef_keyword(	"_Alignas",	T_ALIGNAS),
    133 	kwdef_keyword(	"_Alignof",	T_ALIGNOF),
    134 	kwdef_gcc_attr(	"aligned",	T_AT_ALIGNED),
    135 	kwdef_token(	"__alignof__",	T_ALIGNOF,		0,0,0,0,1),
    136 	kwdef_gcc_attr(	"alloc_size",	T_AT_ALLOC_SIZE),
    137 	kwdef_gcc_attr(	"always_inline",T_AT_ALWAYS_INLINE),
    138 	kwdef_token(	"asm",		T_ASM,			0,0,1,0,7),
    139 	kwdef_token(	"attribute",	T_ATTRIBUTE,		0,0,1,0,6),
    140 	kwdef_sclass(	"auto",		AUTO,			0,0,0,0,1),
    141 	kwdef_type(	"_Bool",	BOOL,			0,1,0,0,1),
    142 	kwdef_gcc_attr(	"bounded",	T_AT_BOUNDED),
    143 	kwdef_keyword(	"break",	T_BREAK),
    144 	kwdef_gcc_attr(	"buffer",	T_AT_BUFFER),
    145 	kwdef_token(	"__builtin_offsetof", T_BUILTIN_OFFSETOF, 0,0,1,0,1),
    146 	kwdef_keyword(	"case",		T_CASE),
    147 	kwdef_type(	"char",		CHAR,			0,0,0,0,1),
    148 	kwdef_gcc_attr(	"cold",		T_AT_COLD),
    149 	kwdef_gcc_attr(	"common",	T_AT_COMMON),
    150 	kwdef_type(	"_Complex",	COMPLEX,		0,1,0,0,1),
    151 	kwdef_tqual(	"const",	CONST,			1,0,0,0,7),
    152 	kwdef_gcc_attr(	"constructor",	T_AT_CONSTRUCTOR),
    153 	kwdef_keyword(	"continue",	T_CONTINUE),
    154 	kwdef_keyword(	"default",	T_DEFAULT),
    155 	kwdef_gcc_attr(	"deprecated",	T_AT_DEPRECATED),
    156 	kwdef_gcc_attr(	"destructor",	T_AT_DESTRUCTOR),
    157 	kwdef_gcc_attr(	"disable_sanitizer_instrumentation",
    158 	    T_AT_DISABLE_SANITIZER_INSTRUMENTATION),
    159 	kwdef_keyword(	"do",		T_DO),
    160 	kwdef_type(	"double",	DOUBLE,			0,0,0,0,1),
    161 	kwdef_keyword(	"else",		T_ELSE),
    162 	kwdef_keyword(	"enum",		T_ENUM),
    163 	kwdef_token(	"__extension__",T_EXTENSION,		0,0,1,0,1),
    164 	kwdef_sclass(	"extern",	EXTERN,			0,0,0,0,1),
    165 	kwdef_gcc_attr(	"fallthrough",	T_AT_FALLTHROUGH),
    166 	kwdef_type(	"float",	FLOAT,			0,0,0,0,1),
    167 	kwdef_keyword(	"for",		T_FOR),
    168 	kwdef_gcc_attr(	"format",	T_AT_FORMAT),
    169 	kwdef_gcc_attr(	"format_arg",	T_AT_FORMAT_ARG),
    170 	kwdef_token(	"_Generic",	T_GENERIC,		0,1,0,0,1),
    171 	kwdef_gcc_attr(	"gnu_inline",	T_AT_GNU_INLINE),
    172 	kwdef_gcc_attr(	"gnu_printf",	T_AT_FORMAT_GNU_PRINTF),
    173 	kwdef_keyword(	"goto",		T_GOTO),
    174 	kwdef_gcc_attr(	"hot",		T_AT_HOT),
    175 	kwdef_keyword(	"if",		T_IF),
    176 	kwdef_token(	"__imag__",	T_IMAG,			0,0,1,0,1),
    177 	kwdef_sclass(	"inline",	INLINE,			0,1,0,0,7),
    178 	kwdef_type(	"int",		INT,			0,0,0,0,1),
    179 #ifdef INT128_SIZE
    180 	kwdef_type(	"__int128_t",	INT128,			0,1,0,0,1),
    181 #endif
    182 	kwdef_type(	"long",		LONG,			0,0,0,0,1),
    183 	kwdef_gcc_attr(	"malloc",	T_AT_MALLOC),
    184 	kwdef_gcc_attr(	"may_alias",	T_AT_MAY_ALIAS),
    185 	kwdef_gcc_attr(	"minbytes",	T_AT_MINBYTES),
    186 	kwdef_gcc_attr(	"mode",		T_AT_MODE),
    187 	kwdef_gcc_attr("no_instrument_function",
    188 					T_AT_NO_INSTRUMENT_FUNCTION),
    189 	kwdef_gcc_attr(	"no_sanitize",	T_AT_NO_SANITIZE),
    190 	kwdef_gcc_attr(	"no_sanitize_thread",	T_AT_NO_SANITIZE_THREAD),
    191 	kwdef_gcc_attr(	"noinline",	T_AT_NOINLINE),
    192 	kwdef_gcc_attr(	"nonnull",	T_AT_NONNULL),
    193 	kwdef_gcc_attr(	"nonstring",	T_AT_NONSTRING),
    194 	kwdef_token(	"_Noreturn",	T_NORETURN,		0,1,0,0,1),
    195 	kwdef_gcc_attr(	"noreturn",	T_AT_NORETURN),
    196 	kwdef_gcc_attr(	"nothrow",	T_AT_NOTHROW),
    197 	kwdef_gcc_attr(	"optimize",	T_AT_OPTIMIZE),
    198 	kwdef_gcc_attr(	"optnone",	T_AT_OPTNONE),
    199 	kwdef_gcc_attr(	"packed",	T_AT_PACKED),
    200 	kwdef_token(	"__packed",	T_PACKED,		0,0,0,0,1),
    201 	kwdef_gcc_attr(	"pcs",		T_AT_PCS),
    202 	kwdef_gcc_attr(	"printf",	T_AT_FORMAT_PRINTF),
    203 	kwdef_gcc_attr(	"pure",		T_AT_PURE),
    204 	kwdef_token(	"__real__",	T_REAL,			0,0,1,0,1),
    205 	kwdef_sclass(	"register",	REG,			0,0,0,0,1),
    206 	kwdef_gcc_attr(	"regparm",	T_AT_REGPARM),
    207 	kwdef_tqual(	"restrict",	RESTRICT,		0,1,0,0,7),
    208 	kwdef_keyword(	"return",	T_RETURN),
    209 	kwdef_gcc_attr(	"returns_nonnull",T_AT_RETURNS_NONNULL),
    210 	kwdef_gcc_attr(	"returns_twice",T_AT_RETURNS_TWICE),
    211 	kwdef_gcc_attr(	"scanf",	T_AT_FORMAT_SCANF),
    212 	kwdef_token(	"section",	T_AT_SECTION,		0,0,1,1,7),
    213 	kwdef_gcc_attr(	"sentinel",	T_AT_SENTINEL),
    214 	kwdef_type(	"short",	SHORT,			0,0,0,0,1),
    215 	kwdef_type(	"signed",	SIGNED,			1,0,0,0,3),
    216 	kwdef_keyword(	"sizeof",	T_SIZEOF),
    217 	kwdef_sclass(	"static",	STATIC,			0,0,0,0,1),
    218 	kwdef_keyword(	"_Static_assert",	T_STATIC_ASSERT),
    219 	kwdef_gcc_attr(	"strfmon",	T_AT_FORMAT_STRFMON),
    220 	kwdef_gcc_attr(	"strftime",	T_AT_FORMAT_STRFTIME),
    221 	kwdef_gcc_attr(	"string",	T_AT_STRING),
    222 	kwdef("struct",	T_STRUCT_OR_UNION, 0,	STRUCT,	0,	0,0,0,0,1),
    223 	kwdef_keyword(	"switch",	T_SWITCH),
    224 	kwdef_token(	"__symbolrename",	T_SYMBOLRENAME,	0,0,0,0,1),
    225 	kwdef_gcc_attr(	"syslog",	T_AT_FORMAT_SYSLOG),
    226 	kwdef_gcc_attr(	"target",	T_AT_TARGET),
    227 	kwdef_tqual(	"__thread",	THREAD,			0,0,1,0,1),
    228 	kwdef_tqual(	"_Thread_local", THREAD,		0,1,0,0,1),
    229 	kwdef_gcc_attr(	"tls_model",	T_AT_TLS_MODEL),
    230 	kwdef_gcc_attr(	"transparent_union", T_AT_TUNION),
    231 	kwdef_sclass(	"typedef",	TYPEDEF,		0,0,0,0,1),
    232 	kwdef_token(	"typeof",	T_TYPEOF,		0,0,1,0,7),
    233 #ifdef INT128_SIZE
    234 	kwdef_type(	"__uint128_t",	UINT128,		0,1,0,0,1),
    235 #endif
    236 	kwdef("union",	T_STRUCT_OR_UNION, 0,	UNION,	0,	0,0,0,0,1),
    237 	kwdef_type(	"unsigned",	UNSIGN,			0,0,0,0,1),
    238 	kwdef_gcc_attr(	"unused",	T_AT_UNUSED),
    239 	kwdef_gcc_attr(	"used",		T_AT_USED),
    240 	kwdef_gcc_attr(	"visibility",	T_AT_VISIBILITY),
    241 	kwdef_type(	"void",		VOID,			0,0,0,0,1),
    242 	kwdef_tqual(	"volatile",	VOLATILE,		1,0,0,0,7),
    243 	kwdef_gcc_attr(	"warn_unused_result", T_AT_WARN_UNUSED_RESULT),
    244 	kwdef_gcc_attr(	"weak",		T_AT_WEAK),
    245 	kwdef_keyword(	"while",	T_WHILE),
    246 	kwdef(NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0),
    247 #undef kwdef
    248 #undef kwdef_token
    249 #undef kwdef_sclass
    250 #undef kwdef_type
    251 #undef kwdef_tqual
    252 #undef kwdef_keyword
    253 #undef kwdef_gcc_attr
    254 };
    255 
    256 /* Symbol table */
    257 static	sym_t	*symtab[HSHSIZ1];
    258 
    259 /* type of next expected symbol */
    260 symt_t	symtyp;
    261 
    262 
    263 static void
    264 symtab_add(sym_t *sym)
    265 {
    266 	size_t h;
    267 
    268 	h = hash(sym->s_name);
    269 	if ((sym->s_symtab_next = symtab[h]) != NULL)
    270 		symtab[h]->s_symtab_ref = &sym->s_symtab_next;
    271 	sym->s_symtab_ref = &symtab[h];
    272 	symtab[h] = sym;
    273 }
    274 
    275 static void
    276 symtab_remove(sym_t *sym)
    277 {
    278 
    279 	if ((*sym->s_symtab_ref = sym->s_symtab_next) != NULL)
    280 		sym->s_symtab_next->s_symtab_ref = sym->s_symtab_ref;
    281 	sym->s_symtab_next = NULL;
    282 }
    283 
    284 
    285 static void
    286 add_keyword(const struct keyword *kw, bool leading, bool trailing)
    287 {
    288 	sym_t *sym;
    289 	char buf[256];
    290 	const char *name;
    291 
    292 	if (!leading && !trailing) {
    293 		name = kw->kw_name;
    294 	} else {
    295 		(void)snprintf(buf, sizeof(buf), "%s%s%s",
    296 		    leading ? "__" : "", kw->kw_name, trailing ? "__" : "");
    297 		name = xstrdup(buf);
    298 	}
    299 
    300 	sym = block_zero_alloc(sizeof(*sym));
    301 	sym->s_name = name;
    302 	sym->s_keyword = kw;
    303 	sym->s_value.v_quad = kw->kw_token;
    304 	if (kw->kw_token == T_TYPE || kw->kw_token == T_STRUCT_OR_UNION) {
    305 		sym->s_tspec = kw->kw_tspec;
    306 	} else if (kw->kw_token == T_SCLASS) {
    307 		sym->s_scl = kw->kw_scl;
    308 	} else if (kw->kw_token == T_QUAL) {
    309 		sym->s_tqual = kw->kw_tqual;
    310 	}
    311 
    312 	symtab_add(sym);
    313 }
    314 
    315 /*
    316  * All keywords are written to the symbol table. This saves us looking
    317  * in an extra table for each name we found.
    318  */
    319 void
    320 initscan(void)
    321 {
    322 	struct keyword *kw;
    323 
    324 	for (kw = keywords; kw->kw_name != NULL; kw++) {
    325 		if ((kw->kw_c90 || kw->kw_c99) && tflag)
    326 			continue;
    327 		if (kw->kw_c99 && !(Sflag || gflag))
    328 			continue;
    329 		if (kw->kw_gcc && !gflag)
    330 			continue;
    331 		if (kw->kw_plain)
    332 			add_keyword(kw, false, false);
    333 		if (kw->kw_leading)
    334 			add_keyword(kw, true, false);
    335 		if (kw->kw_both)
    336 			add_keyword(kw, true, true);
    337 	}
    338 }
    339 
    340 /*
    341  * Read a character and ensure that it is positive (except EOF).
    342  * Increment line count(s) if necessary.
    343  */
    344 static int
    345 inpc(void)
    346 {
    347 	int	c;
    348 
    349 	if ((c = lex_input()) == EOF)
    350 		return c;
    351 	c &= CHAR_MASK;
    352 	if (c == '\0')
    353 		return EOF;	/* lex returns 0 on EOF. */
    354 	if (c == '\n')
    355 		lex_next_line();
    356 	return c;
    357 }
    358 
    359 static unsigned int
    360 hash(const char *s)
    361 {
    362 	unsigned int v;
    363 	const char *p;
    364 
    365 	v = 0;
    366 	for (p = s; *p != '\0'; p++) {
    367 		v = (v << 4) + (unsigned char)*p;
    368 		v ^= v >> 28;
    369 	}
    370 	return v % HSHSIZ1;
    371 }
    372 
    373 /*
    374  * Lex has found a letter followed by zero or more letters or digits.
    375  * It looks for a symbol in the symbol table with the same name. This
    376  * symbol must either be a keyword or a symbol of the type required by
    377  * symtyp (label, member, tag, ...).
    378  *
    379  * If it is a keyword, the token is returned. In some cases it is described
    380  * more deeply by data written to yylval.
    381  *
    382  * If it is a symbol, T_NAME is returned and the name is stored in yylval.
    383  * If there is already a symbol of the same name and type in the symbol
    384  * table, yylval.y_name->sb_sym points there.
    385  */
    386 extern int
    387 lex_name(const char *yytext, size_t yyleng)
    388 {
    389 	char	*s;
    390 	sbuf_t	*sb;
    391 	sym_t	*sym;
    392 	int	tok;
    393 
    394 	sb = xmalloc(sizeof(*sb));
    395 	sb->sb_name = yytext;
    396 	sb->sb_len = yyleng;
    397 	if ((sym = search(sb)) != NULL && sym->s_keyword != NULL) {
    398 		free(sb);
    399 		return keyw(sym);
    400 	}
    401 
    402 	sb->sb_sym = sym;
    403 
    404 	if (sym != NULL) {
    405 		lint_assert(block_level >= sym->s_block_level);
    406 		sb->sb_name = sym->s_name;
    407 		sb->sb_len = strlen(sym->s_name);
    408 		tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
    409 	} else {
    410 		s = block_zero_alloc(yyleng + 1);
    411 		(void)memcpy(s, yytext, yyleng + 1);
    412 		sb->sb_name = s;
    413 		sb->sb_len = yyleng;
    414 		tok = T_NAME;
    415 	}
    416 
    417 	yylval.y_name = sb;
    418 	return tok;
    419 }
    420 
    421 static sym_t *
    422 search(sbuf_t *sb)
    423 {
    424 	unsigned int h;
    425 	sym_t *sym;
    426 	const struct keyword *kw;
    427 
    428 	h = hash(sb->sb_name);
    429 	for (sym = symtab[h]; sym != NULL; sym = sym->s_symtab_next) {
    430 		if (strcmp(sym->s_name, sb->sb_name) != 0)
    431 			continue;
    432 		kw = sym->s_keyword;
    433 
    434 		if (kw != NULL && !kw->kw_attr)
    435 			return sym;
    436 		if (kw != NULL && in_gcc_attribute)
    437 			return sym;
    438 		if (kw == NULL && !in_gcc_attribute && sym->s_kind == symtyp)
    439 			return sym;
    440 	}
    441 
    442 	return NULL;
    443 }
    444 
    445 static int
    446 keyw(sym_t *sym)
    447 {
    448 	int	t;
    449 
    450 	if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
    451 		yylval.y_scl = sym->s_scl;
    452 	} else if (t == T_TYPE || t == T_STRUCT_OR_UNION) {
    453 		yylval.y_tspec = sym->s_tspec;
    454 	} else if (t == T_QUAL) {
    455 		yylval.y_tqual = sym->s_tqual;
    456 	}
    457 	return t;
    458 }
    459 
    460 /*
    461  * Convert a string representing an integer into internal representation.
    462  * Return T_CON, storing the numeric value in yylval, for yylex.
    463  */
    464 int
    465 lex_integer_constant(const char *yytext, size_t yyleng, int base)
    466 {
    467 	int	l_suffix, u_suffix;
    468 	size_t	len;
    469 	const	char *cp;
    470 	char	c, *eptr;
    471 	tspec_t	typ;
    472 	bool	ansiu;
    473 	bool	warned = false;
    474 	uint64_t uq = 0;
    475 
    476 	/* C11 6.4.4.1p5 */
    477 	static const tspec_t suffix_type[2][3] = {
    478 		{ INT,  LONG,  QUAD, },
    479 		{ UINT, ULONG, UQUAD, }
    480 	};
    481 
    482 	cp = yytext;
    483 	len = yyleng;
    484 
    485 	/* skip 0[xX] or 0[bB] */
    486 	if (base == 16 || base == 2) {
    487 		cp += 2;
    488 		len -= 2;
    489 	}
    490 
    491 	/* read suffixes */
    492 	l_suffix = u_suffix = 0;
    493 	for (;;) {
    494 		if ((c = cp[len - 1]) == 'l' || c == 'L') {
    495 			l_suffix++;
    496 		} else if (c == 'u' || c == 'U') {
    497 			u_suffix++;
    498 		} else {
    499 			break;
    500 		}
    501 		len--;
    502 	}
    503 	if (l_suffix > 2 || u_suffix > 1) {
    504 		/* malformed integer constant */
    505 		warning(251);
    506 		if (l_suffix > 2)
    507 			l_suffix = 2;
    508 		if (u_suffix > 1)
    509 			u_suffix = 1;
    510 	}
    511 	if (tflag && u_suffix != 0) {
    512 		/* suffix U is illegal in traditional C */
    513 		warning(97);
    514 	}
    515 	typ = suffix_type[u_suffix][l_suffix];
    516 
    517 	errno = 0;
    518 
    519 	uq = (uint64_t)strtoull(cp, &eptr, base);
    520 	lint_assert(eptr == cp + len);
    521 	if (errno != 0) {
    522 		/* integer constant out of range */
    523 		warning(252);
    524 		warned = true;
    525 	}
    526 
    527 	/*
    528 	 * If the value is too big for the current type, we must choose
    529 	 * another type.
    530 	 */
    531 	ansiu = false;
    532 	switch (typ) {
    533 	case INT:
    534 		if (uq <= TARG_INT_MAX) {
    535 			/* ok */
    536 		} else if (uq <= TARG_UINT_MAX && base != 10) {
    537 			typ = UINT;
    538 		} else if (uq <= TARG_LONG_MAX) {
    539 			typ = LONG;
    540 		} else {
    541 			typ = ULONG;
    542 			if (uq > TARG_ULONG_MAX && !warned) {
    543 				/* integer constant out of range */
    544 				warning(252);
    545 			}
    546 		}
    547 		if (typ == UINT || typ == ULONG) {
    548 			if (tflag) {
    549 				typ = LONG;
    550 			} else if (!sflag) {
    551 				/*
    552 				 * Remember that the constant is unsigned
    553 				 * only in ANSI C
    554 				 */
    555 				ansiu = true;
    556 			}
    557 		}
    558 		break;
    559 	case UINT:
    560 		if (uq > TARG_UINT_MAX) {
    561 			typ = ULONG;
    562 			if (uq > TARG_ULONG_MAX && !warned) {
    563 				/* integer constant out of range */
    564 				warning(252);
    565 			}
    566 		}
    567 		break;
    568 	case LONG:
    569 		if (uq > TARG_LONG_MAX && !tflag) {
    570 			typ = ULONG;
    571 			if (!sflag)
    572 				ansiu = true;
    573 			if (uq > TARG_ULONG_MAX && !warned) {
    574 				/* integer constant out of range */
    575 				warning(252);
    576 			}
    577 		}
    578 		break;
    579 	case ULONG:
    580 		if (uq > TARG_ULONG_MAX && !warned) {
    581 			/* integer constant out of range */
    582 			warning(252);
    583 		}
    584 		break;
    585 	case QUAD:
    586 		if (uq > TARG_QUAD_MAX && !tflag) {
    587 			typ = UQUAD;
    588 			if (!sflag)
    589 				ansiu = true;
    590 		}
    591 		break;
    592 	case UQUAD:
    593 		if (uq > TARG_UQUAD_MAX && !warned) {
    594 			/* integer constant out of range */
    595 			warning(252);
    596 		}
    597 		break;
    598 	default:
    599 		break;
    600 	}
    601 
    602 	uq = (uint64_t)convert_integer((int64_t)uq, typ, 0);
    603 
    604 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    605 	yylval.y_val->v_tspec = typ;
    606 	yylval.y_val->v_unsigned_since_c90 = ansiu;
    607 	yylval.y_val->v_quad = (int64_t)uq;
    608 
    609 	return T_CON;
    610 }
    611 
    612 /*
    613  * Extend or truncate q to match t.  If t is signed, sign-extend.
    614  *
    615  * len is the number of significant bits. If len is -1, len is set
    616  * to the width of type t.
    617  */
    618 int64_t
    619 convert_integer(int64_t q, tspec_t t, unsigned int len)
    620 {
    621 	uint64_t vbits;
    622 
    623 	if (len == 0)
    624 		len = size_in_bits(t);
    625 
    626 	vbits = value_bits(len);
    627 	return t == PTR || is_uinteger(t) || ((q & bit(len - 1)) == 0)
    628 	    ? (int64_t)(q & vbits)
    629 	    : (int64_t)(q | ~vbits);
    630 }
    631 
    632 /*
    633  * Convert a string representing a floating point value into its numerical
    634  * representation. Type and value are returned in yylval.
    635  *
    636  * XXX Currently it is not possible to convert constants of type
    637  * long double which are greater than DBL_MAX.
    638  */
    639 int
    640 lex_floating_constant(const char *yytext, size_t yyleng)
    641 {
    642 	const	char *cp;
    643 	size_t	len;
    644 	tspec_t typ;
    645 	char	c, *eptr;
    646 	double	d;
    647 	float	f = 0;
    648 
    649 	cp = yytext;
    650 	len = yyleng;
    651 
    652 	if (cp[len - 1] == 'i')
    653 		len--;		/* imaginary, do nothing for now */
    654 
    655 	if ((c = cp[len - 1]) == 'f' || c == 'F') {
    656 		typ = FLOAT;
    657 		len--;
    658 	} else if (c == 'l' || c == 'L') {
    659 		typ = LDOUBLE;
    660 		len--;
    661 	} else {
    662 		if (c == 'd' || c == 'D')
    663 			len--;
    664 		typ = DOUBLE;
    665 	}
    666 
    667 	if (tflag && typ != DOUBLE) {
    668 		/* suffixes F and L are illegal in traditional C */
    669 		warning(98);
    670 	}
    671 
    672 	errno = 0;
    673 	d = strtod(cp, &eptr);
    674 	if (eptr != cp + len) {
    675 		switch (*eptr) {
    676 			/*
    677 			 * XXX: non-native non-current strtod() may not handle hex
    678 			 * floats, ignore the rest if we find traces of hex float
    679 			 * syntax...
    680 			 */
    681 		case 'p':
    682 		case 'P':
    683 		case 'x':
    684 		case 'X':
    685 			d = 0;
    686 			errno = 0;
    687 			break;
    688 		default:
    689 			INTERNAL_ERROR("lex_floating_constant(%s->%s)",
    690 			    cp, eptr);
    691 		}
    692 	}
    693 	if (errno != 0)
    694 		/* floating-point constant out of range */
    695 		warning(248);
    696 
    697 	if (typ == FLOAT) {
    698 		f = (float)d;
    699 		if (isfinite(f) == 0) {
    700 			/* floating-point constant out of range */
    701 			warning(248);
    702 			f = f > 0 ? FLT_MAX : -FLT_MAX;
    703 		}
    704 	}
    705 
    706 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    707 	yylval.y_val->v_tspec = typ;
    708 	if (typ == FLOAT) {
    709 		yylval.y_val->v_ldbl = f;
    710 	} else {
    711 		yylval.y_val->v_ldbl = d;
    712 	}
    713 
    714 	return T_CON;
    715 }
    716 
    717 int
    718 lex_operator(int t, op_t o)
    719 {
    720 
    721 	yylval.y_op = o;
    722 	return t;
    723 }
    724 
    725 /*
    726  * Called if lex found a leading \'.
    727  */
    728 int
    729 lex_character_constant(void)
    730 {
    731 	size_t	n;
    732 	int val, c;
    733 
    734 	n = 0;
    735 	val = 0;
    736 	while ((c = get_escaped_char('\'')) >= 0) {
    737 		val = (val << CHAR_SIZE) + c;
    738 		n++;
    739 	}
    740 	if (c == -2) {
    741 		/* unterminated character constant */
    742 		error(253);
    743 	} else if (n > sizeof(int) || (n > 1 && (pflag || hflag))) {
    744 		/* XXX: should rather be sizeof(TARG_INT) */
    745 
    746 		/* too many characters in character constant */
    747 		error(71);
    748 	} else if (n > 1) {
    749 		/* multi-character character constant */
    750 		warning(294);
    751 	} else if (n == 0) {
    752 		/* empty character constant */
    753 		error(73);
    754 	}
    755 	if (n == 1)
    756 		val = (int)convert_integer(val, CHAR, CHAR_SIZE);
    757 
    758 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    759 	yylval.y_val->v_tspec = INT;
    760 	yylval.y_val->v_quad = val;
    761 
    762 	return T_CON;
    763 }
    764 
    765 /*
    766  * Called if lex found a leading L\'
    767  */
    768 int
    769 lex_wide_character_constant(void)
    770 {
    771 	static	char buf[MB_LEN_MAX + 1];
    772 	size_t	n, nmax;
    773 	int c;
    774 	wchar_t	wc;
    775 
    776 	nmax = MB_CUR_MAX;
    777 
    778 	n = 0;
    779 	while ((c = get_escaped_char('\'')) >= 0) {
    780 		if (n < nmax)
    781 			buf[n] = (char)c;
    782 		n++;
    783 	}
    784 
    785 	wc = 0;
    786 
    787 	if (c == -2) {
    788 		/* unterminated character constant */
    789 		error(253);
    790 	} else if (n == 0) {
    791 		/* empty character constant */
    792 		error(73);
    793 	} else if (n > nmax) {
    794 		n = nmax;
    795 		/* too many characters in character constant */
    796 		error(71);
    797 	} else {
    798 		buf[n] = '\0';
    799 		(void)mbtowc(NULL, NULL, 0);
    800 		if (mbtowc(&wc, buf, nmax) < 0)
    801 			/* invalid multibyte character */
    802 			error(291);
    803 	}
    804 
    805 	yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
    806 	yylval.y_val->v_tspec = WCHAR;
    807 	yylval.y_val->v_quad = wc;
    808 
    809 	return T_CON;
    810 }
    811 
    812 /*
    813  * Read a character which is part of a character constant or of a string
    814  * and handle escapes.
    815  *
    816  * The argument is the character which delimits the character constant or
    817  * string.
    818  *
    819  * Returns -1 if the end of the character constant or string is reached,
    820  * -2 if the EOF is reached, and the character otherwise.
    821  */
    822 static int
    823 get_escaped_char(int delim)
    824 {
    825 	static	int pbc = -1;
    826 	int	n, c, v;
    827 
    828 	if (pbc == -1) {
    829 		c = inpc();
    830 	} else {
    831 		c = pbc;
    832 		pbc = -1;
    833 	}
    834 	if (c == delim)
    835 		return -1;
    836 	switch (c) {
    837 	case '\n':
    838 		if (tflag) {
    839 			/* newline in string or char constant */
    840 			error(254);
    841 			return -2;
    842 		}
    843 		return c;
    844 	case 0:
    845 		/* syntax error '%s' */
    846 		error(249, "EOF or null byte in literal");
    847 		return -2;
    848 	case EOF:
    849 		return -2;
    850 	case '\\':
    851 		switch (c = inpc()) {
    852 		case '"':
    853 			if (tflag && delim == '\'')
    854 				/* \" inside character constants undef... */
    855 				warning(262);
    856 			return '"';
    857 		case '\'':
    858 			return '\'';
    859 		case '?':
    860 			if (tflag)
    861 				/* \? undefined in traditional C */
    862 				warning(263);
    863 			return '?';
    864 		case '\\':
    865 			return '\\';
    866 		case 'a':
    867 			if (tflag)
    868 				/* \a undefined in traditional C */
    869 				warning(81);
    870 			return '\a';
    871 		case 'b':
    872 			return '\b';
    873 		case 'f':
    874 			return '\f';
    875 		case 'n':
    876 			return '\n';
    877 		case 'r':
    878 			return '\r';
    879 		case 't':
    880 			return '\t';
    881 		case 'v':
    882 			if (tflag)
    883 				/* \v undefined in traditional C */
    884 				warning(264);
    885 			return '\v';
    886 		case '8': case '9':
    887 			/* bad octal digit %c */
    888 			warning(77, c);
    889 			/* FALLTHROUGH */
    890 		case '0': case '1': case '2': case '3':
    891 		case '4': case '5': case '6': case '7':
    892 			n = 3;
    893 			v = 0;
    894 			do {
    895 				v = (v << 3) + (c - '0');
    896 				c = inpc();
    897 			} while (--n > 0 && '0' <= c && c <= '7');
    898 			pbc = c;
    899 			if (v > TARG_UCHAR_MAX) {
    900 				/* character escape does not fit in character */
    901 				warning(76);
    902 				v &= CHAR_MASK;
    903 			}
    904 			return v;
    905 		case 'x':
    906 			if (tflag)
    907 				/* \x undefined in traditional C */
    908 				warning(82);
    909 			v = 0;
    910 			n = 0;
    911 			while (c = inpc(), isxdigit(c)) {
    912 				c = isdigit(c) ?
    913 				    c - '0' : toupper(c) - 'A' + 10;
    914 				v = (v << 4) + c;
    915 				if (n >= 0) {
    916 					if ((v & ~CHAR_MASK) != 0) {
    917 						/* overflow in hex escape */
    918 						warning(75);
    919 						n = -1;
    920 					} else {
    921 						n++;
    922 					}
    923 				}
    924 			}
    925 			pbc = c;
    926 			if (n == 0) {
    927 				/* no hex digits follow \x */
    928 				error(74);
    929 			} if (n == -1) {
    930 				v &= CHAR_MASK;
    931 			}
    932 			return v;
    933 		case '\n':
    934 			return get_escaped_char(delim);
    935 		case EOF:
    936 			return -2;
    937 		default:
    938 			if (isprint(c)) {
    939 				/* dubious escape \%c */
    940 				warning(79, c);
    941 			} else {
    942 				/* dubious escape \%o */
    943 				warning(80, c);
    944 			}
    945 		}
    946 	}
    947 	return c;
    948 }
    949 
    950 /* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
    951 static void
    952 parse_line_directive_flags(const char *p,
    953 			   bool *is_begin, bool *is_end, bool *is_system)
    954 {
    955 
    956 	*is_begin = false;
    957 	*is_end = false;
    958 	*is_system = false;
    959 
    960 	while (*p != '\0') {
    961 		const char *word_start, *word_end;
    962 
    963 		while (ch_isspace(*p))
    964 			p++;
    965 
    966 		word_start = p;
    967 		while (*p != '\0' && !ch_isspace(*p))
    968 			p++;
    969 		word_end = p;
    970 
    971 		if (word_end - word_start == 1 && word_start[0] == '1')
    972 			*is_begin = true;
    973 		if (word_end - word_start == 1 && word_start[0] == '2')
    974 			*is_end = true;
    975 		if (word_end - word_start == 1 && word_start[0] == '3')
    976 			*is_system = true;
    977 		/* Flag '4' is only interesting for C++. */
    978 	}
    979 }
    980 
    981 /*
    982  * Called for preprocessor directives. Currently implemented are:
    983  *	# pragma [argument...]
    984  *	# lineno
    985  *	# lineno "filename"
    986  *	# lineno "filename" GCC-flag...
    987  */
    988 void
    989 lex_directive(const char *yytext)
    990 {
    991 	const	char *cp, *fn;
    992 	char	c, *eptr;
    993 	size_t	fnl;
    994 	long	ln;
    995 	bool	is_begin, is_end, is_system;
    996 
    997 	static	bool first = true;
    998 
    999 	/* Go to first non-whitespace after # */
   1000 	for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
   1001 		continue;
   1002 
   1003 	if (!ch_isdigit(c)) {
   1004 		if (strncmp(cp, "pragma", 6) == 0 && ch_isspace(cp[6]))
   1005 			return;
   1006 	error:
   1007 		/* undefined or invalid # directive */
   1008 		warning(255);
   1009 		return;
   1010 	}
   1011 	ln = strtol(--cp, &eptr, 10);
   1012 	if (eptr == cp)
   1013 		goto error;
   1014 	if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
   1015 		goto error;
   1016 	while ((c = *cp++) == ' ' || c == '\t')
   1017 		continue;
   1018 	if (c != '\0') {
   1019 		if (c != '"')
   1020 			goto error;
   1021 		fn = cp;
   1022 		while ((c = *cp) != '"' && c != '\0')
   1023 			cp++;
   1024 		if (c != '"')
   1025 			goto error;
   1026 		if ((fnl = cp++ - fn) > PATH_MAX)
   1027 			goto error;
   1028 		/* empty string means stdin */
   1029 		if (fnl == 0) {
   1030 			fn = "{standard input}";
   1031 			fnl = 16;			/* strlen (fn) */
   1032 		}
   1033 		curr_pos.p_file = record_filename(fn, fnl);
   1034 		/*
   1035 		 * If this is the first directive, the name is the name
   1036 		 * of the C source file as specified at the command line.
   1037 		 * It is written to the output file.
   1038 		 */
   1039 		if (first) {
   1040 			csrc_pos.p_file = curr_pos.p_file;
   1041 			outsrc(transform_filename(curr_pos.p_file,
   1042 			    strlen(curr_pos.p_file)));
   1043 			first = false;
   1044 		}
   1045 
   1046 		parse_line_directive_flags(cp, &is_begin, &is_end, &is_system);
   1047 		update_location(curr_pos.p_file, (int)ln, is_begin, is_end);
   1048 		in_system_header = is_system;
   1049 	}
   1050 	curr_pos.p_line = (int)ln - 1;
   1051 	curr_pos.p_uniq = 0;
   1052 	if (curr_pos.p_file == csrc_pos.p_file) {
   1053 		csrc_pos.p_line = (int)ln - 1;
   1054 		csrc_pos.p_uniq = 0;
   1055 	}
   1056 }
   1057 
   1058 /*
   1059  * Handle lint comments such as ARGSUSED.
   1060  *
   1061  * If one of these comments is recognized, the argument, if any, is
   1062  * parsed and a function which handles this comment is called.
   1063  */
   1064 void
   1065 lex_comment(void)
   1066 {
   1067 	int	c, lc;
   1068 	static const struct {
   1069 		const	char *keywd;
   1070 		bool	arg;
   1071 		void	(*func)(int);
   1072 	} keywtab[] = {
   1073 		{ "ARGSUSED",		true,	argsused	},
   1074 		{ "BITFIELDTYPE",	false,	bitfieldtype	},
   1075 		{ "CONSTCOND",		false,	constcond	},
   1076 		{ "CONSTANTCOND",	false,	constcond	},
   1077 		{ "CONSTANTCONDITION",	false,	constcond	},
   1078 		{ "FALLTHRU",		false,	fallthru	},
   1079 		{ "FALLTHROUGH",	false,	fallthru	},
   1080 		{ "FALL THROUGH",	false,	fallthru	},
   1081 		{ "fallthrough",	false,	fallthru	},
   1082 		{ "LINTLIBRARY",	false,	lintlib		},
   1083 		{ "LINTED",		true,	linted		},
   1084 		{ "LONGLONG",		false,	longlong	},
   1085 		{ "NOSTRICT",		true,	linted		},
   1086 		{ "NOTREACHED",		false,	not_reached	},
   1087 		{ "PRINTFLIKE",		true,	printflike	},
   1088 		{ "PROTOLIB",		true,	protolib	},
   1089 		{ "SCANFLIKE",		true,	scanflike	},
   1090 		{ "VARARGS",		true,	varargs		},
   1091 	};
   1092 	char	keywd[32];
   1093 	char	arg[32];
   1094 	size_t	l, i;
   1095 	int	a;
   1096 	bool	eoc;
   1097 
   1098 	eoc = false;
   1099 
   1100 	/* Skip whitespace after the start of the comment */
   1101 	while (c = inpc(), isspace(c))
   1102 		continue;
   1103 
   1104 	/* Read the potential keyword to keywd */
   1105 	l = 0;
   1106 	while (c != EOF && l < sizeof(keywd) - 1 &&
   1107 	    (isalpha(c) || isspace(c))) {
   1108 		if (islower(c) && l > 0 && ch_isupper(keywd[0]))
   1109 			break;
   1110 		keywd[l++] = (char)c;
   1111 		c = inpc();
   1112 	}
   1113 	while (l > 0 && ch_isspace(keywd[l - 1]))
   1114 		l--;
   1115 	keywd[l] = '\0';
   1116 
   1117 	/* look for the keyword */
   1118 	for (i = 0; i < sizeof(keywtab) / sizeof(keywtab[0]); i++) {
   1119 		if (strcmp(keywtab[i].keywd, keywd) == 0)
   1120 			break;
   1121 	}
   1122 	if (i == sizeof(keywtab) / sizeof(keywtab[0]))
   1123 		goto skip_rest;
   1124 
   1125 	/* skip whitespace after the keyword */
   1126 	while (isspace(c))
   1127 		c = inpc();
   1128 
   1129 	/* read the argument, if the keyword accepts one and there is one */
   1130 	l = 0;
   1131 	if (keywtab[i].arg) {
   1132 		while (isdigit(c) && l < sizeof(arg) - 1) {
   1133 			arg[l++] = (char)c;
   1134 			c = inpc();
   1135 		}
   1136 	}
   1137 	arg[l] = '\0';
   1138 	a = l != 0 ? atoi(arg) : -1;
   1139 
   1140 	/* skip whitespace after the argument */
   1141 	while (isspace(c))
   1142 		c = inpc();
   1143 
   1144 	if (c != '*' || (c = inpc()) != '/') {
   1145 		if (keywtab[i].func != linted)
   1146 			/* extra characters in lint comment */
   1147 			warning(257);
   1148 	} else {
   1149 		/*
   1150 		 * remember that we have already found the end of the
   1151 		 * comment
   1152 		 */
   1153 		eoc = true;
   1154 	}
   1155 
   1156 	if (keywtab[i].func != NULL)
   1157 		(*keywtab[i].func)(a);
   1158 
   1159 skip_rest:
   1160 	while (!eoc) {
   1161 		lc = c;
   1162 		if ((c = inpc()) == EOF) {
   1163 			/* unterminated comment */
   1164 			error(256);
   1165 			break;
   1166 		}
   1167 		if (lc == '*' && c == '/')
   1168 			eoc = true;
   1169 	}
   1170 }
   1171 
   1172 /*
   1173  * Handle // style comments
   1174  */
   1175 void
   1176 lex_slash_slash_comment(void)
   1177 {
   1178 	int c;
   1179 
   1180 	if (!Sflag && !gflag)
   1181 		/* %s does not support // comments */
   1182 		gnuism(312, tflag ? "traditional C" : "C90");
   1183 
   1184 	while ((c = inpc()) != EOF && c != '\n')
   1185 		continue;
   1186 }
   1187 
   1188 /*
   1189  * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
   1190  * clear_warn_flags is called after function definitions and global and
   1191  * local declarations and definitions. It is also called between
   1192  * the controlling expression and the body of control statements
   1193  * (if, switch, for, while).
   1194  */
   1195 void
   1196 clear_warn_flags(void)
   1197 {
   1198 
   1199 	lwarn = LWARN_ALL;
   1200 	quadflg = false;
   1201 	constcond_flag = false;
   1202 }
   1203 
   1204 /*
   1205  * Strings are stored in a dynamically allocated buffer and passed
   1206  * in yylval.y_string to the parser. The parser or the routines called
   1207  * by the parser are responsible for freeing this buffer.
   1208  */
   1209 int
   1210 lex_string(void)
   1211 {
   1212 	unsigned char *s;
   1213 	int	c;
   1214 	size_t	len, max;
   1215 	strg_t	*strg;
   1216 
   1217 	s = xmalloc(max = 64);
   1218 
   1219 	len = 0;
   1220 	while ((c = get_escaped_char('"')) >= 0) {
   1221 		/* +1 to reserve space for a trailing NUL character */
   1222 		if (len + 1 == max)
   1223 			s = xrealloc(s, max *= 2);
   1224 		s[len++] = (char)c;
   1225 	}
   1226 	s[len] = '\0';
   1227 	if (c == -2)
   1228 		/* unterminated string constant */
   1229 		error(258);
   1230 
   1231 	strg = xcalloc(1, sizeof(*strg));
   1232 	strg->st_char = true;
   1233 	strg->st_len = len;
   1234 	strg->st_mem = s;
   1235 
   1236 	yylval.y_string = strg;
   1237 	return T_STRING;
   1238 }
   1239 
   1240 int
   1241 lex_wide_string(void)
   1242 {
   1243 	char	*s;
   1244 	int	c, n;
   1245 	size_t	i, wi;
   1246 	size_t	len, max, wlen;
   1247 	wchar_t	*ws;
   1248 	strg_t	*strg;
   1249 
   1250 	s = xmalloc(max = 64);
   1251 	len = 0;
   1252 	while ((c = get_escaped_char('"')) >= 0) {
   1253 		/* +1 to save space for a trailing NUL character */
   1254 		if (len + 1 >= max)
   1255 			s = xrealloc(s, max *= 2);
   1256 		s[len++] = (char)c;
   1257 	}
   1258 	s[len] = '\0';
   1259 	if (c == -2)
   1260 		/* unterminated string constant */
   1261 		error(258);
   1262 
   1263 	/* get length of wide-character string */
   1264 	(void)mblen(NULL, 0);
   1265 	for (i = 0, wlen = 0; i < len; i += n, wlen++) {
   1266 		if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
   1267 			/* invalid multibyte character */
   1268 			error(291);
   1269 			break;
   1270 		}
   1271 		if (n == 0)
   1272 			n = 1;
   1273 	}
   1274 
   1275 	ws = xmalloc((wlen + 1) * sizeof(*ws));
   1276 
   1277 	/* convert from multibyte to wide char */
   1278 	(void)mbtowc(NULL, NULL, 0);
   1279 	for (i = 0, wi = 0; i < len; i += n, wi++) {
   1280 		if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
   1281 			break;
   1282 		if (n == 0)
   1283 			n = 1;
   1284 	}
   1285 	ws[wi] = 0;
   1286 	free(s);
   1287 
   1288 	strg = xcalloc(1, sizeof(*strg));
   1289 	strg->st_char = false;
   1290 	strg->st_len = wlen;
   1291 	strg->st_mem = ws;
   1292 
   1293 	yylval.y_string = strg;
   1294 	return T_STRING;
   1295 }
   1296 
   1297 #ifdef DEBUG
   1298 static const char *
   1299 symt_name(symt_t kind)
   1300 {
   1301 	static const char *name[] = {
   1302 		"var-func-type",
   1303 		"member",
   1304 		"tag",
   1305 		"label",
   1306 	};
   1307 	return name[kind];
   1308 }
   1309 #endif
   1310 
   1311 /*
   1312  * As noted above, the scanner does not create new symbol table entries
   1313  * for symbols it cannot find in the symbol table. This is to avoid
   1314  * putting undeclared symbols into the symbol table if a syntax error
   1315  * occurs.
   1316  *
   1317  * getsym() is called as soon as it is probably ok to put the symbol in the
   1318  * symbol table. It is still possible that symbols are put in the symbol
   1319  * table that are not completely declared due to syntax errors. To avoid too
   1320  * many problems in this case, symbols get type 'int' in getsym().
   1321  *
   1322  * XXX calls to getsym() should be delayed until decl1*() is called.
   1323  */
   1324 sym_t *
   1325 getsym(sbuf_t *sb)
   1326 {
   1327 	dinfo_t	*di;
   1328 	char	*s;
   1329 	sym_t	*sym;
   1330 
   1331 	sym = sb->sb_sym;
   1332 
   1333 	/*
   1334 	 * During member declaration it is possible that name() looked
   1335 	 * for symbols of type FVFT, although it should have looked for
   1336 	 * symbols of type FTAG. Same can happen for labels. Both cases
   1337 	 * are compensated here.
   1338 	 */
   1339 	if (symtyp == FMEMBER || symtyp == FLABEL) {
   1340 		if (sym == NULL || sym->s_kind == FVFT)
   1341 			sym = search(sb);
   1342 	}
   1343 
   1344 	if (sym != NULL) {
   1345 		lint_assert(sym->s_kind == symtyp);
   1346 		symtyp = FVFT;
   1347 		free(sb);
   1348 		return sym;
   1349 	}
   1350 
   1351 	/* create a new symbol table entry */
   1352 
   1353 	/* labels must always be allocated at level 1 (outermost block) */
   1354 	if (symtyp == FLABEL) {
   1355 		sym = level_zero_alloc(1, sizeof(*sym));
   1356 		s = level_zero_alloc(1, sb->sb_len + 1);
   1357 		(void)memcpy(s, sb->sb_name, sb->sb_len + 1);
   1358 		sym->s_name = s;
   1359 		sym->s_block_level = 1;
   1360 		di = dcs;
   1361 		while (di->d_next != NULL && di->d_next->d_next != NULL)
   1362 			di = di->d_next;
   1363 		lint_assert(di->d_ctx == AUTO);
   1364 	} else {
   1365 		sym = block_zero_alloc(sizeof(*sym));
   1366 		sym->s_name = sb->sb_name;
   1367 		sym->s_block_level = block_level;
   1368 		di = dcs;
   1369 	}
   1370 
   1371 	UNIQUE_CURR_POS(sym->s_def_pos);
   1372 	if ((sym->s_kind = symtyp) != FLABEL)
   1373 		sym->s_type = gettyp(INT);
   1374 
   1375 	symtyp = FVFT;
   1376 
   1377 	symtab_add(sym);
   1378 
   1379 	*di->d_ldlsym = sym;
   1380 	di->d_ldlsym = &sym->s_level_next;
   1381 
   1382 	free(sb);
   1383 	return sym;
   1384 }
   1385 
   1386 /*
   1387  * Construct a temporary symbol. The symbol name starts with a digit, making
   1388  * the name illegal.
   1389  */
   1390 sym_t *
   1391 mktempsym(type_t *t)
   1392 {
   1393 	static int n = 0;
   1394 	char *s = level_zero_alloc(block_level, 64);
   1395 	sym_t *sym = block_zero_alloc(sizeof(*sym));
   1396 	scl_t scl;
   1397 
   1398 	(void)snprintf(s, 64, "%.8d_tmp", n++);
   1399 
   1400 	scl = dcs->d_scl;
   1401 	if (scl == NOSCL)
   1402 		scl = block_level > 0 ? AUTO : EXTERN;
   1403 
   1404 	sym->s_name = s;
   1405 	sym->s_type = t;
   1406 	sym->s_block_level = block_level;
   1407 	sym->s_scl = scl;
   1408 	sym->s_kind = FVFT;
   1409 	sym->s_used = true;
   1410 	sym->s_set = true;
   1411 
   1412 	symtab_add(sym);
   1413 
   1414 	*dcs->d_ldlsym = sym;
   1415 	dcs->d_ldlsym = &sym->s_level_next;
   1416 
   1417 	return sym;
   1418 }
   1419 
   1420 /* Remove a symbol forever from the symbol table. */
   1421 void
   1422 rmsym(sym_t *sym)
   1423 {
   1424 
   1425 	debug_step("rmsym '%s' %s '%s'",
   1426 	    sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
   1427 	symtab_remove(sym);
   1428 
   1429 	/* avoid that the symbol will later be put back to the symbol table */
   1430 	sym->s_block_level = -1;
   1431 }
   1432 
   1433 /*
   1434  * Remove a list of symbols declared at one level from the symbol
   1435  * table.
   1436  */
   1437 void
   1438 rmsyms(sym_t *syms)
   1439 {
   1440 	sym_t	*sym;
   1441 
   1442 	for (sym = syms; sym != NULL; sym = sym->s_level_next) {
   1443 		if (sym->s_block_level != -1) {
   1444 			debug_step("rmsyms '%s' %s '%s'",
   1445 			    sym->s_name, symt_name(sym->s_kind),
   1446 			    type_name(sym->s_type));
   1447 			symtab_remove(sym);
   1448 			sym->s_symtab_ref = NULL;
   1449 		}
   1450 	}
   1451 }
   1452 
   1453 /*
   1454  * Put a symbol into the symbol table.
   1455  */
   1456 void
   1457 inssym(int bl, sym_t *sym)
   1458 {
   1459 
   1460 	debug_step("inssym '%s' %s '%s'",
   1461 	    sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
   1462 	symtab_add(sym);
   1463 	sym->s_block_level = bl;
   1464 	lint_assert(sym->s_symtab_next == NULL ||
   1465 		    sym->s_block_level >= sym->s_symtab_next->s_block_level);
   1466 }
   1467 
   1468 /*
   1469  * Called at level 0 after syntax errors.
   1470  *
   1471  * Removes all symbols which are not declared at level 0 from the
   1472  * symbol table. Also frees all memory which is not associated with
   1473  * level 0.
   1474  */
   1475 void
   1476 cleanup(void)
   1477 {
   1478 	sym_t	*sym, *nsym;
   1479 	size_t	i;
   1480 
   1481 	for (i = 0; i < HSHSIZ1; i++) {
   1482 		for (sym = symtab[i]; sym != NULL; sym = nsym) {
   1483 			nsym = sym->s_symtab_next;
   1484 			if (sym->s_block_level >= 1)
   1485 				symtab_remove(sym);
   1486 		}
   1487 	}
   1488 
   1489 	for (i = mem_block_level; i > 0; i--)
   1490 		level_free_all(i);
   1491 }
   1492 
   1493 /*
   1494  * Create a new symbol with the name of an existing symbol.
   1495  */
   1496 sym_t *
   1497 pushdown(const sym_t *sym)
   1498 {
   1499 	sym_t	*nsym;
   1500 
   1501 	debug_step("pushdown '%s' %s '%s'",
   1502 	    sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
   1503 	nsym = block_zero_alloc(sizeof(*nsym));
   1504 	lint_assert(sym->s_block_level <= block_level);
   1505 	nsym->s_name = sym->s_name;
   1506 	UNIQUE_CURR_POS(nsym->s_def_pos);
   1507 	nsym->s_kind = sym->s_kind;
   1508 	nsym->s_block_level = block_level;
   1509 
   1510 	symtab_add(nsym);
   1511 
   1512 	*dcs->d_ldlsym = nsym;
   1513 	dcs->d_ldlsym = &nsym->s_level_next;
   1514 
   1515 	return nsym;
   1516 }
   1517 
   1518 /*
   1519  * Free any dynamically allocated memory referenced by
   1520  * the value stack or yylval.
   1521  * The type of information in yylval is described by tok.
   1522  */
   1523 void
   1524 freeyyv(void *sp, int tok)
   1525 {
   1526 	if (tok == T_NAME || tok == T_TYPENAME) {
   1527 		sbuf_t *sb = *(sbuf_t **)sp;
   1528 		free(sb);
   1529 	} else if (tok == T_CON) {
   1530 		val_t *val = *(val_t **)sp;
   1531 		free(val);
   1532 	} else if (tok == T_STRING) {
   1533 		strg_t *strg = *(strg_t **)sp;
   1534 		free(strg->st_mem);
   1535 		free(strg);
   1536 	}
   1537 }
   1538