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