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