lex.c revision 1.37 1 /* $NetBSD: lex.c,v 1.37 2021/06/15 20:46:45 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.37 2021/06/15 20:46:45 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 EOF:
936 return -2;
937 case '\\':
938 switch (c = inpc()) {
939 case '"':
940 if (tflag && delim == '\'')
941 /* \" inside character constants undef... */
942 warning(262);
943 return '"';
944 case '\'':
945 return '\'';
946 case '?':
947 if (tflag)
948 /* \? undefined in traditional C */
949 warning(263);
950 return '?';
951 case '\\':
952 return '\\';
953 case 'a':
954 if (tflag)
955 /* \a undefined in traditional C */
956 warning(81);
957 return '\a';
958 case 'b':
959 return '\b';
960 case 'f':
961 return '\f';
962 case 'n':
963 return '\n';
964 case 'r':
965 return '\r';
966 case 't':
967 return '\t';
968 case 'v':
969 if (tflag)
970 /* \v undefined in traditional C */
971 warning(264);
972 return '\v';
973 case '8': case '9':
974 /* bad octal digit %c */
975 warning(77, c);
976 /* FALLTHROUGH */
977 case '0': case '1': case '2': case '3':
978 case '4': case '5': case '6': case '7':
979 n = 3;
980 v = 0;
981 do {
982 v = (v << 3) + (c - '0');
983 c = inpc();
984 } while (--n > 0 && isdigit(c) && (tflag || c <= '7'));
985 if (tflag && n > 0 && isdigit(c))
986 /* bad octal digit %c */
987 warning(77, c);
988 pbc = c;
989 if (v > TARG_UCHAR_MAX) {
990 /* character escape does not fit in character */
991 warning(76);
992 v &= CHAR_MASK;
993 }
994 return v;
995 case 'x':
996 if (tflag)
997 /* \x undefined in traditional C */
998 warning(82);
999 v = 0;
1000 n = 0;
1001 while ((c = inpc()) >= 0 && isxdigit(c)) {
1002 c = isdigit(c) ?
1003 c - '0' : toupper(c) - 'A' + 10;
1004 v = (v << 4) + c;
1005 if (n >= 0) {
1006 if ((v & ~CHAR_MASK) != 0) {
1007 /* overflow in hex escape */
1008 warning(75);
1009 n = -1;
1010 } else {
1011 n++;
1012 }
1013 }
1014 }
1015 pbc = c;
1016 if (n == 0) {
1017 /* no hex digits follow \x */
1018 error(74);
1019 } if (n == -1) {
1020 v &= CHAR_MASK;
1021 }
1022 return v;
1023 case '\n':
1024 return get_escaped_char(delim);
1025 case EOF:
1026 return -2;
1027 default:
1028 if (isprint(c)) {
1029 /* dubious escape \%c */
1030 warning(79, c);
1031 } else {
1032 /* dubious escape \%o */
1033 warning(80, c);
1034 }
1035 }
1036 }
1037 return c;
1038 }
1039
1040 /* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
1041 static void
1042 parse_line_directive_flags(const char *p,
1043 bool *is_begin, bool *is_end, bool *is_system)
1044 {
1045
1046 *is_begin = false;
1047 *is_end = false;
1048 *is_system = false;
1049
1050 while (*p != '\0') {
1051 while (ch_isspace(*p))
1052 p++;
1053
1054 const char *word_start = p;
1055 while (*p != '\0' && !ch_isspace(*p))
1056 p++;
1057 const char *word_end = p;
1058
1059 if (word_end - word_start == 1 && word_start[0] == '1')
1060 *is_begin = true;
1061 if (word_end - word_start == 1 && word_start[0] == '2')
1062 *is_end = true;
1063 if (word_end - word_start == 1 && word_start[0] == '3')
1064 *is_system = true;
1065 /* Flag '4' would only be interesting if lint handled C++. */
1066 }
1067
1068 #if 0
1069 if (*p != '\0') {
1070 /* syntax error '%s' */
1071 warning(249, "extra character(s) after directive");
1072 }
1073 #endif
1074 }
1075
1076 /*
1077 * Called for preprocessor directives. Currently implemented are:
1078 * # lineno
1079 * # lineno "filename"
1080 * # lineno "filename" GCC-flag...
1081 */
1082 void
1083 lex_directive(const char *yytext)
1084 {
1085 const char *cp, *fn;
1086 char c, *eptr;
1087 size_t fnl;
1088 long ln;
1089 bool is_begin, is_end, is_system;
1090
1091 static bool first = true;
1092
1093 /* Go to first non-whitespace after # */
1094 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
1095 continue;
1096
1097 if (!ch_isdigit(c)) {
1098 if (strncmp(cp, "pragma", 6) == 0 && ch_isspace(cp[6]))
1099 return;
1100 error:
1101 /* undefined or invalid # directive */
1102 warning(255);
1103 return;
1104 }
1105 ln = strtol(--cp, &eptr, 10);
1106 if (cp == eptr)
1107 goto error;
1108 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
1109 goto error;
1110 while ((c = *cp++) == ' ' || c == '\t')
1111 continue;
1112 if (c != '\0') {
1113 if (c != '"')
1114 goto error;
1115 fn = cp;
1116 while ((c = *cp) != '"' && c != '\0')
1117 cp++;
1118 if (c != '"')
1119 goto error;
1120 if ((fnl = cp++ - fn) > PATH_MAX)
1121 goto error;
1122 /* empty string means stdin */
1123 if (fnl == 0) {
1124 fn = "{standard input}";
1125 fnl = 16; /* strlen (fn) */
1126 }
1127 curr_pos.p_file = record_filename(fn, fnl);
1128 /*
1129 * If this is the first directive, the name is the name
1130 * of the C source file as specified at the command line.
1131 * It is written to the output file.
1132 */
1133 if (first) {
1134 csrc_pos.p_file = curr_pos.p_file;
1135 outsrc(transform_filename(curr_pos.p_file,
1136 strlen(curr_pos.p_file)));
1137 first = false;
1138 }
1139
1140 parse_line_directive_flags(cp, &is_begin, &is_end, &is_system);
1141 update_location(curr_pos.p_file, (int)ln, is_begin, is_end);
1142 in_system_header = is_system;
1143 }
1144 curr_pos.p_line = (int)ln - 1;
1145 curr_pos.p_uniq = 0;
1146 if (curr_pos.p_file == csrc_pos.p_file) {
1147 csrc_pos.p_line = (int)ln - 1;
1148 csrc_pos.p_uniq = 0;
1149 }
1150 }
1151
1152 /*
1153 * Handle lint comments such as ARGSUSED.
1154 *
1155 * If one of these comments is recognized, the argument, if any, is
1156 * parsed and a function which handles this comment is called.
1157 */
1158 void
1159 lex_comment(void)
1160 {
1161 int c, lc;
1162 static const struct {
1163 const char *keywd;
1164 bool arg;
1165 void (*func)(int);
1166 } keywtab[] = {
1167 { "ARGSUSED", true, argsused },
1168 { "BITFIELDTYPE", false, bitfieldtype },
1169 { "CONSTCOND", false, constcond },
1170 { "CONSTANTCOND", false, constcond },
1171 { "CONSTANTCONDITION", false, constcond },
1172 { "FALLTHRU", false, fallthru },
1173 { "FALLTHROUGH", false, fallthru },
1174 { "LINTLIBRARY", false, lintlib },
1175 { "LINTED", true, linted },
1176 { "LONGLONG", false, longlong },
1177 { "NOSTRICT", true, linted },
1178 { "NOTREACHED", false, not_reached },
1179 { "PRINTFLIKE", true, printflike },
1180 { "PROTOLIB", true, protolib },
1181 { "SCANFLIKE", true, scanflike },
1182 { "VARARGS", true, varargs },
1183 };
1184 char keywd[32];
1185 char arg[32];
1186 size_t l, i;
1187 int a;
1188 bool eoc;
1189
1190 eoc = false;
1191
1192 /* Skip whitespace after the start of the comment */
1193 while ((c = inpc()) != EOF && isspace(c))
1194 continue;
1195
1196 /* Read the potential keyword to keywd */
1197 l = 0;
1198 while (c != EOF && isupper(c) && l < sizeof(keywd) - 1) {
1199 keywd[l++] = (char)c;
1200 c = inpc();
1201 }
1202 keywd[l] = '\0';
1203
1204 /* look for the keyword */
1205 for (i = 0; i < sizeof(keywtab) / sizeof(keywtab[0]); i++) {
1206 if (strcmp(keywtab[i].keywd, keywd) == 0)
1207 break;
1208 }
1209 if (i == sizeof(keywtab) / sizeof(keywtab[0]))
1210 goto skip_rest;
1211
1212 /* skip whitespace after the keyword */
1213 while (c != EOF && isspace(c))
1214 c = inpc();
1215
1216 /* read the argument, if the keyword accepts one and there is one */
1217 l = 0;
1218 if (keywtab[i].arg) {
1219 while (c != EOF && isdigit(c) && l < sizeof(arg) - 1) {
1220 arg[l++] = (char)c;
1221 c = inpc();
1222 }
1223 }
1224 arg[l] = '\0';
1225 a = l != 0 ? atoi(arg) : -1;
1226
1227 /* skip whitespace after the argument */
1228 while (c != EOF && isspace(c))
1229 c = inpc();
1230
1231 if (c != '*' || (c = inpc()) != '/') {
1232 if (keywtab[i].func != linted)
1233 /* extra characters in lint comment */
1234 warning(257);
1235 } else {
1236 /*
1237 * remember that we have already found the end of the
1238 * comment
1239 */
1240 eoc = true;
1241 }
1242
1243 if (keywtab[i].func != NULL)
1244 (*keywtab[i].func)(a);
1245
1246 skip_rest:
1247 while (!eoc) {
1248 lc = c;
1249 if ((c = inpc()) == EOF) {
1250 /* unterminated comment */
1251 error(256);
1252 break;
1253 }
1254 if (lc == '*' && c == '/')
1255 eoc = true;
1256 }
1257 }
1258
1259 /*
1260 * Handle // style comments
1261 */
1262 void
1263 lex_slash_slash_comment(void)
1264 {
1265 int c;
1266
1267 if (!Sflag && !gflag)
1268 /* %s C does not support // comments */
1269 gnuism(312, tflag ? "traditional" : "ANSI");
1270
1271 while ((c = inpc()) != EOF && c != '\n')
1272 continue;
1273 }
1274
1275 /*
1276 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1277 * clear_warn_flags() is called after function definitions and global and
1278 * local declarations and definitions. It is also called between
1279 * the controlling expression and the body of control statements
1280 * (if, switch, for, while).
1281 */
1282 void
1283 clear_warn_flags(void)
1284 {
1285
1286 lwarn = LWARN_ALL;
1287 quadflg = false;
1288 constcond_flag = false;
1289 }
1290
1291 /*
1292 * Strings are stored in a dynamically allocated buffer and passed
1293 * in yylval.y_xstrg to the parser. The parser or the routines called
1294 * by the parser are responsible for freeing this buffer.
1295 */
1296 int
1297 lex_string(void)
1298 {
1299 u_char *s;
1300 int c;
1301 size_t len, max;
1302 strg_t *strg;
1303
1304 s = xmalloc(max = 64);
1305
1306 len = 0;
1307 while ((c = get_escaped_char('"')) >= 0) {
1308 /* +1 to reserve space for a trailing NUL character */
1309 if (len + 1 == max)
1310 s = xrealloc(s, max *= 2);
1311 s[len++] = (char)c;
1312 }
1313 s[len] = '\0';
1314 if (c == -2)
1315 /* unterminated string constant */
1316 error(258);
1317
1318 strg = xcalloc(1, sizeof(*strg));
1319 strg->st_tspec = CHAR;
1320 strg->st_len = len;
1321 strg->st_cp = s;
1322
1323 yylval.y_string = strg;
1324 return T_STRING;
1325 }
1326
1327 int
1328 lex_wide_string(void)
1329 {
1330 char *s;
1331 int c, n;
1332 size_t i, wi;
1333 size_t len, max, wlen;
1334 wchar_t *ws;
1335 strg_t *strg;
1336
1337 s = xmalloc(max = 64);
1338 len = 0;
1339 while ((c = get_escaped_char('"')) >= 0) {
1340 /* +1 to save space for a trailing NUL character */
1341 if (len + 1 >= max)
1342 s = xrealloc(s, max *= 2);
1343 s[len++] = (char)c;
1344 }
1345 s[len] = '\0';
1346 if (c == -2)
1347 /* unterminated string constant */
1348 error(258);
1349
1350 /* get length of wide-character string */
1351 (void)mblen(NULL, 0);
1352 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1353 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1354 /* invalid multibyte character */
1355 error(291);
1356 break;
1357 }
1358 if (n == 0)
1359 n = 1;
1360 }
1361
1362 ws = xmalloc((wlen + 1) * sizeof(*ws));
1363
1364 /* convert from multibyte to wide char */
1365 (void)mbtowc(NULL, NULL, 0);
1366 for (i = 0, wi = 0; i < len; i += n, wi++) {
1367 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1368 break;
1369 if (n == 0)
1370 n = 1;
1371 }
1372 ws[wi] = 0;
1373 free(s);
1374
1375 strg = xcalloc(1, sizeof(*strg));
1376 strg->st_tspec = WCHAR;
1377 strg->st_len = wlen;
1378 strg->st_wcp = ws;
1379
1380 yylval.y_string = strg;
1381 return T_STRING;
1382 }
1383
1384 /*
1385 * As noted above the scanner does not create new symbol table entries
1386 * for symbols it cannot find in the symbol table. This is to avoid
1387 * putting undeclared symbols into the symbol table if a syntax error
1388 * occurs.
1389 *
1390 * getsym() is called as soon as it is probably ok to put the symbol to
1391 * the symbol table. This does not mean that it is not possible that
1392 * symbols are put to the symbol table which are not completely
1393 * declared due to syntax errors. To avoid too many problems in this
1394 * case, symbols get type int in getsym().
1395 *
1396 * XXX calls to getsym() should be delayed until decl1*() is called.
1397 */
1398 sym_t *
1399 getsym(sbuf_t *sb)
1400 {
1401 dinfo_t *di;
1402 char *s;
1403 sym_t *sym;
1404
1405 sym = sb->sb_sym;
1406
1407 /*
1408 * During member declaration it is possible that name() looked
1409 * for symbols of type FVFT, although it should have looked for
1410 * symbols of type FTAG. Same can happen for labels. Both cases
1411 * are compensated here.
1412 */
1413 if (symtyp == FMEMBER || symtyp == FLABEL) {
1414 if (sym == NULL || sym->s_kind == FVFT)
1415 sym = search(sb);
1416 }
1417
1418 if (sym != NULL) {
1419 if (sym->s_kind != symtyp)
1420 INTERNAL_ERROR("getsym(%d, %d)", sym->s_kind, symtyp);
1421 symtyp = FVFT;
1422 freesb(sb);
1423 return sym;
1424 }
1425
1426 /* create a new symbol table entry */
1427
1428 /* labels must always be allocated at level 1 (outermost block) */
1429 if (symtyp == FLABEL) {
1430 sym = getlblk(1, sizeof(*sym));
1431 s = getlblk(1, sb->sb_len + 1);
1432 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1433 sym->s_name = s;
1434 sym->s_block_level = 1;
1435 di = dcs;
1436 while (di->d_next != NULL && di->d_next->d_next != NULL)
1437 di = di->d_next;
1438 lint_assert(di->d_ctx == AUTO);
1439 } else {
1440 sym = getblk(sizeof(*sym));
1441 sym->s_name = sb->sb_name;
1442 sym->s_block_level = block_level;
1443 di = dcs;
1444 }
1445
1446 UNIQUE_CURR_POS(sym->s_def_pos);
1447 if ((sym->s_kind = symtyp) != FLABEL)
1448 sym->s_type = gettyp(INT);
1449
1450 symtyp = FVFT;
1451
1452 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1453 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1454 sym->s_rlink = &symtab[sb->sb_hash];
1455 symtab[sb->sb_hash] = sym;
1456
1457 *di->d_ldlsym = sym;
1458 di->d_ldlsym = &sym->s_dlnxt;
1459
1460 freesb(sb);
1461 return sym;
1462 }
1463
1464 /*
1465 * Construct a temporary symbol. The symbol starts with a digit, so that
1466 * it is illegal.
1467 */
1468 sym_t *
1469 mktempsym(type_t *t)
1470 {
1471 static int n = 0;
1472 int h;
1473 char *s = getlblk(block_level, 64);
1474 sym_t *sym = getblk(sizeof(*sym));
1475 scl_t scl;
1476
1477 (void)snprintf(s, 64, "%.8d_tmp", n++);
1478 h = hash(s);
1479
1480 scl = dcs->d_scl;
1481 if (scl == NOSCL)
1482 scl = block_level > 0 ? AUTO : EXTERN;
1483
1484 sym->s_name = s;
1485 sym->s_type = t;
1486 sym->s_block_level = block_level;
1487 sym->s_scl = scl;
1488 sym->s_kind = FVFT;
1489 sym->s_used = true;
1490 sym->s_set = true;
1491
1492 if ((sym->s_link = symtab[h]) != NULL)
1493 symtab[h]->s_rlink = &sym->s_link;
1494 sym->s_rlink = &symtab[h];
1495 symtab[h] = sym;
1496
1497 *dcs->d_ldlsym = sym;
1498 dcs->d_ldlsym = &sym->s_dlnxt;
1499
1500 return sym;
1501 }
1502
1503 /*
1504 * Remove a symbol forever from the symbol table. s_block_level
1505 * is set to -1 to avoid that the symbol will later be put
1506 * back to the symbol table.
1507 */
1508 void
1509 rmsym(sym_t *sym)
1510 {
1511
1512 if ((*sym->s_rlink = sym->s_link) != NULL)
1513 sym->s_link->s_rlink = sym->s_rlink;
1514 sym->s_block_level = -1;
1515 sym->s_link = NULL;
1516 }
1517
1518 /*
1519 * Remove a list of symbols declared at one level from the symbol
1520 * table.
1521 */
1522 void
1523 rmsyms(sym_t *syms)
1524 {
1525 sym_t *sym;
1526
1527 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1528 if (sym->s_block_level != -1) {
1529 if ((*sym->s_rlink = sym->s_link) != NULL)
1530 sym->s_link->s_rlink = sym->s_rlink;
1531 sym->s_link = NULL;
1532 sym->s_rlink = NULL;
1533 }
1534 }
1535 }
1536
1537 /*
1538 * Put a symbol into the symbol table.
1539 */
1540 void
1541 inssym(int bl, sym_t *sym)
1542 {
1543 int h;
1544
1545 h = hash(sym->s_name);
1546 if ((sym->s_link = symtab[h]) != NULL)
1547 symtab[h]->s_rlink = &sym->s_link;
1548 sym->s_rlink = &symtab[h];
1549 symtab[h] = sym;
1550 sym->s_block_level = bl;
1551 lint_assert(sym->s_link == NULL ||
1552 sym->s_block_level >= sym->s_link->s_block_level);
1553 }
1554
1555 /*
1556 * Called at level 0 after syntax errors.
1557 *
1558 * Removes all symbols which are not declared at level 0 from the
1559 * symbol table. Also frees all memory which is not associated with
1560 * level 0.
1561 */
1562 void
1563 cleanup(void)
1564 {
1565 sym_t *sym, *nsym;
1566 int i;
1567
1568 for (i = 0; i < HSHSIZ1; i++) {
1569 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1570 nsym = sym->s_link;
1571 if (sym->s_block_level >= 1) {
1572 if ((*sym->s_rlink = nsym) != NULL)
1573 nsym->s_rlink = sym->s_rlink;
1574 }
1575 }
1576 }
1577
1578 for (i = mem_block_level; i > 0; i--)
1579 freelblk(i);
1580 }
1581
1582 /*
1583 * Create a new symbol with the name of an existing symbol.
1584 */
1585 sym_t *
1586 pushdown(const sym_t *sym)
1587 {
1588 int h;
1589 sym_t *nsym;
1590
1591 h = hash(sym->s_name);
1592 nsym = getblk(sizeof(*nsym));
1593 lint_assert(sym->s_block_level <= block_level);
1594 nsym->s_name = sym->s_name;
1595 UNIQUE_CURR_POS(nsym->s_def_pos);
1596 nsym->s_kind = sym->s_kind;
1597 nsym->s_block_level = block_level;
1598
1599 if ((nsym->s_link = symtab[h]) != NULL)
1600 symtab[h]->s_rlink = &nsym->s_link;
1601 nsym->s_rlink = &symtab[h];
1602 symtab[h] = nsym;
1603
1604 *dcs->d_ldlsym = nsym;
1605 dcs->d_ldlsym = &nsym->s_dlnxt;
1606
1607 return nsym;
1608 }
1609
1610 /*
1611 * Free any dynamically allocated memory referenced by
1612 * the value stack or yylval.
1613 * The type of information in yylval is described by tok.
1614 */
1615 void
1616 freeyyv(void *sp, int tok)
1617 {
1618 if (tok == T_NAME || tok == T_TYPENAME) {
1619 sbuf_t *sb = *(sbuf_t **)sp;
1620 freesb(sb);
1621 } else if (tok == T_CON) {
1622 val_t *val = *(val_t **)sp;
1623 free(val);
1624 } else if (tok == T_STRING) {
1625 strg_t *strg = *(strg_t **)sp;
1626 if (strg->st_tspec == CHAR) {
1627 free(strg->st_cp);
1628 } else {
1629 lint_assert(strg->st_tspec == WCHAR);
1630 free(strg->st_wcp);
1631 }
1632 free(strg);
1633 }
1634 }
1635