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