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