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