lex.c revision 1.109 1 /* $NetBSD: lex.c,v 1.109 2022/03/13 14:40:36 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.109 2022/03/13 14:40:36 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 + 1 >= 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 = 0;; level++) {
343 debug_printf("symbol table level %d\n", level);
344
345 bool more = false;
346 size_t n = sizeof(symtab) / sizeof(symtab[0]);
347
348 syms.len = 0;
349 for (size_t i = 0; i < n; i++) {
350 for (sym_t *sym = symtab[i]; sym != NULL;) {
351 if (sym->s_block_level == level &&
352 sym->s_keyword == NULL)
353 syms_add(&syms, sym);
354 if (sym->s_block_level > level)
355 more = true;
356 sym = sym->s_symtab_next;
357 }
358 }
359
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 if (!more)
368 break;
369 }
370
371 free(syms.items);
372 }
373 #endif
374
375 static void
376 add_keyword(const struct keyword *kw, bool leading, bool trailing)
377 {
378 sym_t *sym;
379 char buf[256];
380 const char *name;
381
382 if (!leading && !trailing) {
383 name = kw->kw_name;
384 } else {
385 (void)snprintf(buf, sizeof(buf), "%s%s%s",
386 leading ? "__" : "", kw->kw_name, trailing ? "__" : "");
387 name = xstrdup(buf);
388 }
389
390 sym = block_zero_alloc(sizeof(*sym));
391 sym->s_name = name;
392 sym->s_keyword = kw;
393 sym->s_value.v_quad = kw->kw_token;
394 if (kw->kw_token == T_TYPE || kw->kw_token == T_STRUCT_OR_UNION) {
395 sym->s_tspec = kw->kw_tspec;
396 } else if (kw->kw_token == T_SCLASS) {
397 sym->s_scl = kw->kw_scl;
398 } else if (kw->kw_token == T_QUAL) {
399 sym->s_tqual = kw->kw_tqual;
400 }
401
402 symtab_add(sym);
403 }
404
405 /*
406 * All keywords are written to the symbol table. This saves us looking
407 * in an extra table for each name we found.
408 */
409 void
410 initscan(void)
411 {
412 const struct keyword *kw;
413
414 for (kw = keywords; kw->kw_name != NULL; kw++) {
415 if ((kw->kw_c90 || kw->kw_c99) && tflag)
416 continue;
417 if (kw->kw_c99 && !(Sflag || gflag))
418 continue;
419 if (kw->kw_gcc && !gflag)
420 continue;
421 if (kw->kw_plain)
422 add_keyword(kw, false, false);
423 if (kw->kw_leading)
424 add_keyword(kw, true, false);
425 if (kw->kw_both)
426 add_keyword(kw, true, true);
427 }
428 }
429
430 /*
431 * Read a character and ensure that it is positive (except EOF).
432 * Increment line count(s) if necessary.
433 */
434 static int
435 inpc(void)
436 {
437 int c;
438
439 if ((c = lex_input()) == EOF)
440 return c;
441 c &= CHAR_MASK;
442 if (c == '\0')
443 return EOF; /* lex returns 0 on EOF. */
444 if (c == '\n')
445 lex_next_line();
446 return c;
447 }
448
449 static int
450 lex_keyword(sym_t *sym)
451 {
452 int t;
453
454 if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
455 yylval.y_scl = sym->s_scl;
456 } else if (t == T_TYPE || t == T_STRUCT_OR_UNION) {
457 yylval.y_tspec = sym->s_tspec;
458 } else if (t == T_QUAL) {
459 yylval.y_tqual = sym->s_tqual;
460 }
461 return t;
462 }
463
464 /*
465 * Lex has found a letter followed by zero or more letters or digits.
466 * It looks for a symbol in the symbol table with the same name. This
467 * symbol must either be a keyword or a symbol of the type required by
468 * symtyp (label, member, tag, ...).
469 *
470 * If it is a keyword, the token is returned. In some cases it is described
471 * more deeply by data written to yylval.
472 *
473 * If it is a symbol, T_NAME is returned and the name is stored in yylval.
474 * If there is already a symbol of the same name and type in the symbol
475 * table, yylval.y_name->sb_sym points there.
476 */
477 extern int
478 lex_name(const char *yytext, size_t yyleng)
479 {
480 char *s;
481 sbuf_t *sb;
482 sym_t *sym;
483 int tok;
484
485 sb = xmalloc(sizeof(*sb));
486 sb->sb_name = yytext;
487 sb->sb_len = yyleng;
488 if ((sym = symtab_search(sb)) != NULL && sym->s_keyword != NULL) {
489 free(sb);
490 return lex_keyword(sym);
491 }
492
493 sb->sb_sym = sym;
494
495 if (sym != NULL) {
496 lint_assert(block_level >= sym->s_block_level);
497 sb->sb_name = sym->s_name;
498 tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
499 } else {
500 s = block_zero_alloc(yyleng + 1);
501 (void)memcpy(s, yytext, yyleng + 1);
502 sb->sb_name = s;
503 tok = T_NAME;
504 }
505
506 yylval.y_name = sb;
507 return tok;
508 }
509
510 /*
511 * Convert a string representing an integer into internal representation.
512 * Return T_CON, storing the numeric value in yylval, for yylex.
513 */
514 int
515 lex_integer_constant(const char *yytext, size_t yyleng, int base)
516 {
517 int l_suffix, u_suffix;
518 size_t len;
519 const char *cp;
520 char c, *eptr;
521 tspec_t typ;
522 bool ansiu;
523 bool warned = false;
524 uint64_t uq = 0;
525
526 /* C11 6.4.4.1p5 */
527 static const tspec_t suffix_type[2][3] = {
528 { INT, LONG, QUAD, },
529 { UINT, ULONG, UQUAD, }
530 };
531
532 cp = yytext;
533 len = yyleng;
534
535 /* skip 0[xX] or 0[bB] */
536 if (base == 16 || base == 2) {
537 cp += 2;
538 len -= 2;
539 }
540
541 /* read suffixes */
542 l_suffix = u_suffix = 0;
543 for (;;) {
544 if ((c = cp[len - 1]) == 'l' || c == 'L') {
545 l_suffix++;
546 } else if (c == 'u' || c == 'U') {
547 u_suffix++;
548 } else {
549 break;
550 }
551 len--;
552 }
553 if (l_suffix > 2 || u_suffix > 1) {
554 /* malformed integer constant */
555 warning(251);
556 if (l_suffix > 2)
557 l_suffix = 2;
558 if (u_suffix > 1)
559 u_suffix = 1;
560 }
561 if (tflag && u_suffix != 0) {
562 /* suffix U is illegal in traditional C */
563 warning(97);
564 }
565 typ = suffix_type[u_suffix][l_suffix];
566
567 errno = 0;
568
569 uq = (uint64_t)strtoull(cp, &eptr, base);
570 lint_assert(eptr == cp + len);
571 if (errno != 0) {
572 /* integer constant out of range */
573 warning(252);
574 warned = true;
575 }
576
577 /*
578 * If the value is too big for the current type, we must choose
579 * another type.
580 */
581 ansiu = false;
582 switch (typ) {
583 case INT:
584 if (uq <= TARG_INT_MAX) {
585 /* ok */
586 } else if (uq <= TARG_UINT_MAX && base != 10) {
587 typ = UINT;
588 } else if (uq <= TARG_LONG_MAX) {
589 typ = LONG;
590 } else {
591 typ = ULONG;
592 if (uq > TARG_ULONG_MAX && !warned) {
593 /* integer constant out of range */
594 warning(252);
595 }
596 }
597 if (typ == UINT || typ == ULONG) {
598 if (tflag) {
599 typ = LONG;
600 } else if (!sflag) {
601 /*
602 * Remember that the constant is unsigned
603 * only in ANSI C
604 */
605 ansiu = true;
606 }
607 }
608 break;
609 case UINT:
610 if (uq > TARG_UINT_MAX) {
611 typ = ULONG;
612 if (uq > TARG_ULONG_MAX && !warned) {
613 /* integer constant out of range */
614 warning(252);
615 }
616 }
617 break;
618 case LONG:
619 if (uq > TARG_LONG_MAX && !tflag) {
620 typ = ULONG;
621 if (!sflag)
622 ansiu = true;
623 if (uq > TARG_ULONG_MAX && !warned) {
624 /* integer constant out of range */
625 warning(252);
626 }
627 }
628 break;
629 case ULONG:
630 if (uq > TARG_ULONG_MAX && !warned) {
631 /* integer constant out of range */
632 warning(252);
633 }
634 break;
635 case QUAD:
636 if (uq > TARG_QUAD_MAX && !tflag) {
637 typ = UQUAD;
638 if (!sflag)
639 ansiu = true;
640 }
641 break;
642 case UQUAD:
643 if (uq > TARG_UQUAD_MAX && !warned) {
644 /* integer constant out of range */
645 warning(252);
646 }
647 break;
648 default:
649 break;
650 }
651
652 uq = (uint64_t)convert_integer((int64_t)uq, typ, 0);
653
654 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
655 yylval.y_val->v_tspec = typ;
656 yylval.y_val->v_unsigned_since_c90 = ansiu;
657 yylval.y_val->v_quad = (int64_t)uq;
658
659 return T_CON;
660 }
661
662 /*
663 * Extend or truncate q to match t. If t is signed, sign-extend.
664 *
665 * len is the number of significant bits. If len is -1, len is set
666 * to the width of type t.
667 */
668 int64_t
669 convert_integer(int64_t q, tspec_t t, unsigned int len)
670 {
671 uint64_t vbits;
672
673 if (len == 0)
674 len = size_in_bits(t);
675
676 vbits = value_bits(len);
677 return t == PTR || is_uinteger(t) || ((q & bit(len - 1)) == 0)
678 ? (int64_t)(q & vbits)
679 : (int64_t)(q | ~vbits);
680 }
681
682 /*
683 * Convert a string representing a floating point value into its numerical
684 * representation. Type and value are returned in yylval.
685 *
686 * XXX Currently it is not possible to convert constants of type
687 * long double which are greater than DBL_MAX.
688 */
689 int
690 lex_floating_constant(const char *yytext, size_t yyleng)
691 {
692 const char *cp;
693 size_t len;
694 tspec_t typ;
695 char c, *eptr;
696 double d;
697 float f = 0;
698
699 cp = yytext;
700 len = yyleng;
701
702 if (cp[len - 1] == 'i')
703 len--; /* imaginary, do nothing for now */
704
705 if ((c = cp[len - 1]) == 'f' || c == 'F') {
706 typ = FLOAT;
707 len--;
708 } else if (c == 'l' || c == 'L') {
709 typ = LDOUBLE;
710 len--;
711 } else {
712 if (c == 'd' || c == 'D')
713 len--;
714 typ = DOUBLE;
715 }
716
717 if (tflag && typ != DOUBLE) {
718 /* suffixes F and L are illegal in traditional C */
719 warning(98);
720 }
721
722 errno = 0;
723 d = strtod(cp, &eptr);
724 if (eptr != cp + len) {
725 switch (*eptr) {
726 /*
727 * XXX: non-native non-current strtod() may not handle hex
728 * floats, ignore the rest if we find traces of hex float
729 * syntax...
730 */
731 case 'p':
732 case 'P':
733 case 'x':
734 case 'X':
735 d = 0;
736 errno = 0;
737 break;
738 default:
739 INTERNAL_ERROR("lex_floating_constant(%s->%s)",
740 cp, eptr);
741 }
742 }
743 if (errno != 0)
744 /* floating-point constant out of range */
745 warning(248);
746
747 if (typ == FLOAT) {
748 f = (float)d;
749 if (isfinite(f) == 0) {
750 /* floating-point constant out of range */
751 warning(248);
752 f = f > 0 ? FLT_MAX : -FLT_MAX;
753 }
754 }
755
756 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
757 yylval.y_val->v_tspec = typ;
758 if (typ == FLOAT) {
759 yylval.y_val->v_ldbl = f;
760 } else {
761 yylval.y_val->v_ldbl = d;
762 }
763
764 return T_CON;
765 }
766
767 int
768 lex_operator(int t, op_t o)
769 {
770
771 yylval.y_op = o;
772 return t;
773 }
774
775 /*
776 * Called if lex found a leading \'.
777 */
778 int
779 lex_character_constant(void)
780 {
781 size_t n;
782 int val, c;
783
784 n = 0;
785 val = 0;
786 while ((c = get_escaped_char('\'')) >= 0) {
787 val = (val << CHAR_SIZE) + c;
788 n++;
789 }
790 if (c == -2) {
791 /* unterminated character constant */
792 error(253);
793 } else if (n > sizeof(int) || (n > 1 && (pflag || hflag))) {
794 /* XXX: should rather be sizeof(TARG_INT) */
795
796 /* too many characters in character constant */
797 error(71);
798 } else if (n > 1) {
799 /* multi-character character constant */
800 warning(294);
801 } else if (n == 0) {
802 /* empty character constant */
803 error(73);
804 }
805 if (n == 1)
806 val = (int)convert_integer(val, CHAR, CHAR_SIZE);
807
808 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
809 yylval.y_val->v_tspec = INT;
810 yylval.y_val->v_quad = val;
811
812 return T_CON;
813 }
814
815 /*
816 * Called if lex found a leading L\'
817 */
818 int
819 lex_wide_character_constant(void)
820 {
821 static char buf[MB_LEN_MAX + 1];
822 size_t n, nmax;
823 int c;
824 wchar_t wc;
825
826 nmax = MB_CUR_MAX;
827
828 n = 0;
829 while ((c = get_escaped_char('\'')) >= 0) {
830 if (n < nmax)
831 buf[n] = (char)c;
832 n++;
833 }
834
835 wc = 0;
836
837 if (c == -2) {
838 /* unterminated character constant */
839 error(253);
840 } else if (n == 0) {
841 /* empty character constant */
842 error(73);
843 } else if (n > nmax) {
844 n = nmax;
845 /* too many characters in character constant */
846 error(71);
847 } else {
848 buf[n] = '\0';
849 (void)mbtowc(NULL, NULL, 0);
850 if (mbtowc(&wc, buf, nmax) < 0)
851 /* invalid multibyte character */
852 error(291);
853 }
854
855 yylval.y_val = xcalloc(1, sizeof(*yylval.y_val));
856 yylval.y_val->v_tspec = WCHAR;
857 yylval.y_val->v_quad = wc;
858
859 return T_CON;
860 }
861
862 /*
863 * Read a character which is part of a character constant or of a string
864 * and handle escapes.
865 *
866 * The argument is the character which delimits the character constant or
867 * string.
868 *
869 * Returns -1 if the end of the character constant or string is reached,
870 * -2 if the EOF is reached, and the character otherwise.
871 */
872 static int
873 get_escaped_char(int delim)
874 {
875 static int pbc = -1;
876 int n, c, v;
877
878 if (pbc == -1) {
879 c = inpc();
880 } else {
881 c = pbc;
882 pbc = -1;
883 }
884 if (c == delim)
885 return -1;
886 switch (c) {
887 case '\n':
888 if (tflag) {
889 /* newline in string or char constant */
890 error(254);
891 return -2;
892 }
893 return c;
894 case 0:
895 /* syntax error '%s' */
896 error(249, "EOF or null byte in literal");
897 return -2;
898 case EOF:
899 return -2;
900 case '\\':
901 switch (c = inpc()) {
902 case '"':
903 if (tflag && delim == '\'')
904 /* \" inside character constants undef... */
905 warning(262);
906 return '"';
907 case '\'':
908 return '\'';
909 case '?':
910 if (tflag)
911 /* \? undefined in traditional C */
912 warning(263);
913 return '?';
914 case '\\':
915 return '\\';
916 case 'a':
917 if (tflag)
918 /* \a undefined in traditional C */
919 warning(81);
920 return '\a';
921 case 'b':
922 return '\b';
923 case 'f':
924 return '\f';
925 case 'n':
926 return '\n';
927 case 'r':
928 return '\r';
929 case 't':
930 return '\t';
931 case 'v':
932 if (tflag)
933 /* \v undefined in traditional C */
934 warning(264);
935 return '\v';
936 case '8': case '9':
937 /* bad octal digit %c */
938 warning(77, c);
939 /* FALLTHROUGH */
940 case '0': case '1': case '2': case '3':
941 case '4': case '5': case '6': case '7':
942 n = 3;
943 v = 0;
944 do {
945 v = (v << 3) + (c - '0');
946 c = inpc();
947 } while (--n > 0 && '0' <= c && c <= '7');
948 pbc = c;
949 if (v > TARG_UCHAR_MAX) {
950 /* character escape does not fit in character */
951 warning(76);
952 v &= CHAR_MASK;
953 }
954 return v;
955 case 'x':
956 if (tflag)
957 /* \x undefined in traditional C */
958 warning(82);
959 v = 0;
960 n = 0;
961 while (c = inpc(), isxdigit(c)) {
962 c = isdigit(c) ?
963 c - '0' : toupper(c) - 'A' + 10;
964 v = (v << 4) + c;
965 if (n >= 0) {
966 if ((v & ~CHAR_MASK) != 0) {
967 /* overflow in hex escape */
968 warning(75);
969 n = -1;
970 } else {
971 n++;
972 }
973 }
974 }
975 pbc = c;
976 if (n == 0) {
977 /* no hex digits follow \x */
978 error(74);
979 } if (n == -1) {
980 v &= CHAR_MASK;
981 }
982 return v;
983 case '\n':
984 return get_escaped_char(delim);
985 case EOF:
986 return -2;
987 default:
988 if (isprint(c)) {
989 /* dubious escape \%c */
990 warning(79, c);
991 } else {
992 /* dubious escape \%o */
993 warning(80, c);
994 }
995 }
996 }
997 return c;
998 }
999
1000 /* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
1001 static void
1002 parse_line_directive_flags(const char *p,
1003 bool *is_begin, bool *is_end, bool *is_system)
1004 {
1005
1006 *is_begin = false;
1007 *is_end = false;
1008 *is_system = false;
1009
1010 while (*p != '\0') {
1011 const char *word_start, *word_end;
1012
1013 while (ch_isspace(*p))
1014 p++;
1015
1016 word_start = p;
1017 while (*p != '\0' && !ch_isspace(*p))
1018 p++;
1019 word_end = p;
1020
1021 if (word_end - word_start == 1 && word_start[0] == '1')
1022 *is_begin = true;
1023 if (word_end - word_start == 1 && word_start[0] == '2')
1024 *is_end = true;
1025 if (word_end - word_start == 1 && word_start[0] == '3')
1026 *is_system = true;
1027 /* Flag '4' is only interesting for C++. */
1028 }
1029 }
1030
1031 /*
1032 * Called for preprocessor directives. Currently implemented are:
1033 * # pragma [argument...]
1034 * # lineno
1035 * # lineno "filename"
1036 * # lineno "filename" GCC-flag...
1037 */
1038 void
1039 lex_directive(const char *yytext)
1040 {
1041 const char *cp, *fn;
1042 char c, *eptr;
1043 size_t fnl;
1044 long ln;
1045 bool is_begin, is_end, is_system;
1046
1047 static bool first = true;
1048
1049 /* Go to first non-whitespace after # */
1050 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
1051 continue;
1052
1053 if (!ch_isdigit(c)) {
1054 if (strncmp(cp, "pragma", 6) == 0 && ch_isspace(cp[6]))
1055 return;
1056 error:
1057 /* undefined or invalid # directive */
1058 warning(255);
1059 return;
1060 }
1061 ln = strtol(--cp, &eptr, 10);
1062 if (eptr == cp)
1063 goto error;
1064 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
1065 goto error;
1066 while ((c = *cp++) == ' ' || c == '\t')
1067 continue;
1068 if (c != '\0') {
1069 if (c != '"')
1070 goto error;
1071 fn = cp;
1072 while ((c = *cp) != '"' && c != '\0')
1073 cp++;
1074 if (c != '"')
1075 goto error;
1076 if ((fnl = cp++ - fn) > PATH_MAX)
1077 goto error;
1078 /* empty string means stdin */
1079 if (fnl == 0) {
1080 fn = "{standard input}";
1081 fnl = 16; /* strlen (fn) */
1082 }
1083 curr_pos.p_file = record_filename(fn, fnl);
1084 /*
1085 * If this is the first directive, the name is the name
1086 * of the C source file as specified at the command line.
1087 * It is written to the output file.
1088 */
1089 if (first) {
1090 csrc_pos.p_file = curr_pos.p_file;
1091 outsrc(transform_filename(curr_pos.p_file,
1092 strlen(curr_pos.p_file)));
1093 first = false;
1094 }
1095
1096 parse_line_directive_flags(cp, &is_begin, &is_end, &is_system);
1097 update_location(curr_pos.p_file, (int)ln, is_begin, is_end);
1098 in_system_header = is_system;
1099 }
1100 curr_pos.p_line = (int)ln - 1;
1101 curr_pos.p_uniq = 0;
1102 if (curr_pos.p_file == csrc_pos.p_file) {
1103 csrc_pos.p_line = (int)ln - 1;
1104 csrc_pos.p_uniq = 0;
1105 }
1106 }
1107
1108 /*
1109 * Handle lint comments such as ARGSUSED.
1110 *
1111 * If one of these comments is recognized, the argument, if any, is
1112 * parsed and a function which handles this comment is called.
1113 */
1114 void
1115 lex_comment(void)
1116 {
1117 int c, lc;
1118 static const struct {
1119 const char *keywd;
1120 bool arg;
1121 void (*func)(int);
1122 } keywtab[] = {
1123 { "ARGSUSED", true, argsused },
1124 { "BITFIELDTYPE", false, bitfieldtype },
1125 { "CONSTCOND", false, constcond },
1126 { "CONSTANTCOND", false, constcond },
1127 { "CONSTANTCONDITION", false, constcond },
1128 { "FALLTHRU", false, fallthru },
1129 { "FALLTHROUGH", false, fallthru },
1130 { "FALL THROUGH", false, fallthru },
1131 { "fallthrough", false, fallthru },
1132 { "LINTLIBRARY", false, lintlib },
1133 { "LINTED", true, linted },
1134 { "LONGLONG", false, longlong },
1135 { "NOSTRICT", true, linted },
1136 { "NOTREACHED", false, not_reached },
1137 { "PRINTFLIKE", true, printflike },
1138 { "PROTOLIB", true, protolib },
1139 { "SCANFLIKE", true, scanflike },
1140 { "VARARGS", true, varargs },
1141 };
1142 char keywd[32];
1143 char arg[32];
1144 size_t l, i;
1145 int a;
1146 bool eoc;
1147
1148 eoc = false;
1149
1150 /* Skip whitespace after the start of the comment */
1151 while (c = inpc(), isspace(c))
1152 continue;
1153
1154 /* Read the potential keyword to keywd */
1155 l = 0;
1156 while (c != EOF && l < sizeof(keywd) - 1 &&
1157 (isalpha(c) || isspace(c))) {
1158 if (islower(c) && l > 0 && ch_isupper(keywd[0]))
1159 break;
1160 keywd[l++] = (char)c;
1161 c = inpc();
1162 }
1163 while (l > 0 && ch_isspace(keywd[l - 1]))
1164 l--;
1165 keywd[l] = '\0';
1166
1167 /* look for the keyword */
1168 for (i = 0; i < sizeof(keywtab) / sizeof(keywtab[0]); i++) {
1169 if (strcmp(keywtab[i].keywd, keywd) == 0)
1170 break;
1171 }
1172 if (i == sizeof(keywtab) / sizeof(keywtab[0]))
1173 goto skip_rest;
1174
1175 /* skip whitespace after the keyword */
1176 while (isspace(c))
1177 c = inpc();
1178
1179 /* read the argument, if the keyword accepts one and there is one */
1180 l = 0;
1181 if (keywtab[i].arg) {
1182 while (isdigit(c) && l < sizeof(arg) - 1) {
1183 arg[l++] = (char)c;
1184 c = inpc();
1185 }
1186 }
1187 arg[l] = '\0';
1188 a = l != 0 ? atoi(arg) : -1;
1189
1190 /* skip whitespace after the argument */
1191 while (isspace(c))
1192 c = inpc();
1193
1194 if (c != '*' || (c = inpc()) != '/') {
1195 if (keywtab[i].func != linted)
1196 /* extra characters in lint comment */
1197 warning(257);
1198 } else {
1199 /*
1200 * remember that we have already found the end of the
1201 * comment
1202 */
1203 eoc = true;
1204 }
1205
1206 if (keywtab[i].func != NULL)
1207 (*keywtab[i].func)(a);
1208
1209 skip_rest:
1210 while (!eoc) {
1211 lc = c;
1212 if ((c = inpc()) == EOF) {
1213 /* unterminated comment */
1214 error(256);
1215 break;
1216 }
1217 if (lc == '*' && c == '/')
1218 eoc = true;
1219 }
1220 }
1221
1222 /*
1223 * Handle // style comments
1224 */
1225 void
1226 lex_slash_slash_comment(void)
1227 {
1228 int c;
1229
1230 if (!Sflag && !gflag)
1231 /* %s does not support // comments */
1232 gnuism(312, tflag ? "traditional C" : "C90");
1233
1234 while ((c = inpc()) != EOF && c != '\n')
1235 continue;
1236 }
1237
1238 /*
1239 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1240 * clear_warn_flags is called after function definitions and global and
1241 * local declarations and definitions. It is also called between
1242 * the controlling expression and the body of control statements
1243 * (if, switch, for, while).
1244 */
1245 void
1246 clear_warn_flags(void)
1247 {
1248
1249 lwarn = LWARN_ALL;
1250 quadflg = false;
1251 constcond_flag = false;
1252 }
1253
1254 /*
1255 * Strings are stored in a dynamically allocated buffer and passed
1256 * in yylval.y_string to the parser. The parser or the routines called
1257 * by the parser are responsible for freeing this buffer.
1258 */
1259 int
1260 lex_string(void)
1261 {
1262 unsigned char *s;
1263 int c;
1264 size_t len, max;
1265 strg_t *strg;
1266
1267 s = xmalloc(max = 64);
1268
1269 len = 0;
1270 while ((c = get_escaped_char('"')) >= 0) {
1271 /* +1 to reserve space for a trailing NUL character */
1272 if (len + 1 == max)
1273 s = xrealloc(s, max *= 2);
1274 s[len++] = (char)c;
1275 }
1276 s[len] = '\0';
1277 if (c == -2)
1278 /* unterminated string constant */
1279 error(258);
1280
1281 strg = xcalloc(1, sizeof(*strg));
1282 strg->st_char = true;
1283 strg->st_len = len;
1284 strg->st_mem = s;
1285
1286 yylval.y_string = strg;
1287 return T_STRING;
1288 }
1289
1290 int
1291 lex_wide_string(void)
1292 {
1293 char *s;
1294 int c, n;
1295 size_t i, wi;
1296 size_t len, max, wlen;
1297 wchar_t *ws;
1298 strg_t *strg;
1299
1300 s = xmalloc(max = 64);
1301 len = 0;
1302 while ((c = get_escaped_char('"')) >= 0) {
1303 /* +1 to save space for a trailing NUL character */
1304 if (len + 1 >= max)
1305 s = xrealloc(s, max *= 2);
1306 s[len++] = (char)c;
1307 }
1308 s[len] = '\0';
1309 if (c == -2)
1310 /* unterminated string constant */
1311 error(258);
1312
1313 /* get length of wide-character string */
1314 (void)mblen(NULL, 0);
1315 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1316 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1317 /* invalid multibyte character */
1318 error(291);
1319 break;
1320 }
1321 if (n == 0)
1322 n = 1;
1323 }
1324
1325 ws = xmalloc((wlen + 1) * sizeof(*ws));
1326
1327 /* convert from multibyte to wide char */
1328 (void)mbtowc(NULL, NULL, 0);
1329 for (i = 0, wi = 0; i < len; i += n, wi++) {
1330 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1331 break;
1332 if (n == 0)
1333 n = 1;
1334 }
1335 ws[wi] = 0;
1336 free(s);
1337
1338 strg = xcalloc(1, sizeof(*strg));
1339 strg->st_char = false;
1340 strg->st_len = wlen;
1341 strg->st_mem = ws;
1342
1343 yylval.y_string = strg;
1344 return T_STRING;
1345 }
1346
1347 void
1348 lex_next_line(void)
1349 {
1350 curr_pos.p_line++;
1351 curr_pos.p_uniq = 0;
1352 debug_step("parsing %s:%d", curr_pos.p_file, curr_pos.p_line);
1353 if (curr_pos.p_file == csrc_pos.p_file) {
1354 csrc_pos.p_line++;
1355 csrc_pos.p_uniq = 0;
1356 }
1357 }
1358
1359 void
1360 lex_unknown_character(int c)
1361 {
1362
1363 /* unknown character \%o */
1364 error(250, c);
1365 }
1366
1367 /*
1368 * As noted above, the scanner does not create new symbol table entries
1369 * for symbols it cannot find in the symbol table. This is to avoid
1370 * putting undeclared symbols into the symbol table if a syntax error
1371 * occurs.
1372 *
1373 * getsym() is called as soon as it is probably ok to put the symbol in the
1374 * symbol table. It is still possible that symbols are put in the symbol
1375 * table that are not completely declared due to syntax errors. To avoid too
1376 * many problems in this case, symbols get type 'int' in getsym().
1377 *
1378 * XXX calls to getsym() should be delayed until decl1*() is called.
1379 */
1380 sym_t *
1381 getsym(sbuf_t *sb)
1382 {
1383 dinfo_t *di;
1384 char *s;
1385 sym_t *sym;
1386
1387 sym = sb->sb_sym;
1388
1389 /*
1390 * During member declaration it is possible that name() looked
1391 * for symbols of type FVFT, although it should have looked for
1392 * symbols of type FTAG. Same can happen for labels. Both cases
1393 * are compensated here.
1394 */
1395 if (symtyp == FMEMBER || symtyp == FLABEL) {
1396 if (sym == NULL || sym->s_kind == FVFT)
1397 sym = symtab_search(sb);
1398 }
1399
1400 if (sym != NULL) {
1401 lint_assert(sym->s_kind == symtyp);
1402 symtyp = FVFT;
1403 free(sb);
1404 return sym;
1405 }
1406
1407 /* create a new symbol table entry */
1408
1409 /* labels must always be allocated at level 1 (outermost block) */
1410 if (symtyp == FLABEL) {
1411 sym = level_zero_alloc(1, sizeof(*sym));
1412 s = level_zero_alloc(1, sb->sb_len + 1);
1413 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1414 sym->s_name = s;
1415 sym->s_block_level = 1;
1416 di = dcs;
1417 while (di->d_next != NULL && di->d_next->d_next != NULL)
1418 di = di->d_next;
1419 lint_assert(di->d_ctx == AUTO);
1420 } else {
1421 sym = block_zero_alloc(sizeof(*sym));
1422 sym->s_name = sb->sb_name;
1423 sym->s_block_level = block_level;
1424 di = dcs;
1425 }
1426
1427 UNIQUE_CURR_POS(sym->s_def_pos);
1428 if ((sym->s_kind = symtyp) != FLABEL)
1429 sym->s_type = gettyp(INT);
1430
1431 symtyp = FVFT;
1432
1433 symtab_add(sym);
1434
1435 *di->d_ldlsym = sym;
1436 di->d_ldlsym = &sym->s_level_next;
1437
1438 free(sb);
1439 return sym;
1440 }
1441
1442 /*
1443 * Construct a temporary symbol. The symbol name starts with a digit, making
1444 * the name illegal.
1445 */
1446 sym_t *
1447 mktempsym(type_t *t)
1448 {
1449 static int n = 0;
1450 char *s = level_zero_alloc(block_level, 64);
1451 sym_t *sym = block_zero_alloc(sizeof(*sym));
1452 scl_t scl;
1453
1454 (void)snprintf(s, 64, "%.8d_tmp", n++);
1455
1456 scl = dcs->d_scl;
1457 if (scl == NOSCL)
1458 scl = block_level > 0 ? AUTO : EXTERN;
1459
1460 sym->s_name = s;
1461 sym->s_type = t;
1462 sym->s_block_level = block_level;
1463 sym->s_scl = scl;
1464 sym->s_kind = FVFT;
1465 sym->s_used = true;
1466 sym->s_set = true;
1467
1468 symtab_add(sym);
1469
1470 *dcs->d_ldlsym = sym;
1471 dcs->d_ldlsym = &sym->s_level_next;
1472
1473 return sym;
1474 }
1475
1476 /* Remove a symbol forever from the symbol table. */
1477 void
1478 rmsym(sym_t *sym)
1479 {
1480
1481 debug_step("rmsym '%s' %s '%s'",
1482 sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
1483 symtab_remove(sym);
1484
1485 /* avoid that the symbol will later be put back to the symbol table */
1486 sym->s_block_level = -1;
1487 }
1488
1489 /*
1490 * Remove a list of symbols declared at one level from the symbol
1491 * table.
1492 */
1493 void
1494 rmsyms(sym_t *syms)
1495 {
1496 sym_t *sym;
1497
1498 for (sym = syms; sym != NULL; sym = sym->s_level_next) {
1499 if (sym->s_block_level != -1) {
1500 debug_step("rmsyms '%s' %s '%s'",
1501 sym->s_name, symt_name(sym->s_kind),
1502 type_name(sym->s_type));
1503 symtab_remove(sym);
1504 sym->s_symtab_ref = NULL;
1505 }
1506 }
1507 }
1508
1509 /*
1510 * Put a symbol into the symbol table.
1511 */
1512 void
1513 inssym(int bl, sym_t *sym)
1514 {
1515
1516 debug_step("inssym '%s' %s '%s'",
1517 sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
1518 symtab_add(sym);
1519 sym->s_block_level = bl;
1520 lint_assert(sym->s_symtab_next == NULL ||
1521 sym->s_block_level >= sym->s_symtab_next->s_block_level);
1522 }
1523
1524 /*
1525 * Called at level 0 after syntax errors.
1526 *
1527 * Removes all symbols which are not declared at level 0 from the
1528 * symbol table. Also frees all memory which is not associated with
1529 * level 0.
1530 */
1531 void
1532 clean_up_after_error(void)
1533 {
1534
1535 symtab_remove_locals();
1536
1537 for (size_t i = mem_block_level; i > 0; i--)
1538 level_free_all(i);
1539 }
1540
1541 /*
1542 * Create a new symbol with the name of an existing symbol.
1543 */
1544 sym_t *
1545 pushdown(const sym_t *sym)
1546 {
1547 sym_t *nsym;
1548
1549 debug_step("pushdown '%s' %s '%s'",
1550 sym->s_name, symt_name(sym->s_kind), type_name(sym->s_type));
1551 nsym = block_zero_alloc(sizeof(*nsym));
1552 lint_assert(sym->s_block_level <= block_level);
1553 nsym->s_name = sym->s_name;
1554 UNIQUE_CURR_POS(nsym->s_def_pos);
1555 nsym->s_kind = sym->s_kind;
1556 nsym->s_block_level = block_level;
1557
1558 symtab_add(nsym);
1559
1560 *dcs->d_ldlsym = nsym;
1561 dcs->d_ldlsym = &nsym->s_level_next;
1562
1563 return nsym;
1564 }
1565
1566 /*
1567 * Free any dynamically allocated memory referenced by
1568 * the value stack or yylval.
1569 * The type of information in yylval is described by tok.
1570 */
1571 void
1572 freeyyv(void *sp, int tok)
1573 {
1574 if (tok == T_NAME || tok == T_TYPENAME) {
1575 sbuf_t *sb = *(sbuf_t **)sp;
1576 free(sb);
1577 } else if (tok == T_CON) {
1578 val_t *val = *(val_t **)sp;
1579 free(val);
1580 } else if (tok == T_STRING) {
1581 strg_t *strg = *(strg_t **)sp;
1582 free(strg->st_mem);
1583 free(strg);
1584 }
1585 }
1586