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