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