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