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