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