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