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