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