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