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