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