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