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