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