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