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