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