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