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