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