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