lex.c revision 1.1 1 /* $NetBSD: lex.c,v 1.1 2021/01/23 17:58:03 rillig Exp $ */
2
3 /*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <sys/cdefs.h>
36 #if defined(__RCSID) && !defined(lint)
37 __RCSID("$NetBSD: lex.c,v 1.1 2021/01/23 17:58:03 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 d)
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 == d)
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 && d == '\'')
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(d);
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 */
1049 void
1050 lex_directive(const char *yytext)
1051 {
1052 const char *cp, *fn;
1053 char c, *eptr;
1054 size_t fnl;
1055 long ln;
1056 static bool first = true;
1057
1058 /* Go to first non-whitespace after # */
1059 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
1060 continue;
1061
1062 if (!ch_isdigit(c)) {
1063 if (strncmp(cp, "pragma", 6) == 0 && ch_isspace(cp[6]))
1064 return;
1065 error:
1066 /* undefined or invalid # directive */
1067 warning(255);
1068 return;
1069 }
1070 ln = strtol(--cp, &eptr, 10);
1071 if (cp == eptr)
1072 goto error;
1073 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
1074 goto error;
1075 while ((c = *cp++) == ' ' || c == '\t')
1076 continue;
1077 if (c != '\0') {
1078 if (c != '"')
1079 goto error;
1080 fn = cp;
1081 while ((c = *cp) != '"' && c != '\0')
1082 cp++;
1083 if (c != '"')
1084 goto error;
1085 if ((fnl = cp++ - fn) > PATH_MAX)
1086 goto error;
1087 parse_line_directive_flags(cp);
1088
1089 /* empty string means stdin */
1090 if (fnl == 0) {
1091 fn = "{standard input}";
1092 fnl = 16; /* strlen (fn) */
1093 }
1094 curr_pos.p_file = fnnalloc(fn, fnl);
1095 /*
1096 * If this is the first directive, the name is the name
1097 * of the C source file as specified at the command line.
1098 * It is written to the output file.
1099 */
1100 if (first) {
1101 csrc_pos.p_file = curr_pos.p_file;
1102 outsrc(fnxform(curr_pos.p_file,
1103 strlen(curr_pos.p_file)));
1104 first = false;
1105 }
1106 }
1107 curr_pos.p_line = (int)ln - 1;
1108 curr_pos.p_uniq = 0;
1109 if (curr_pos.p_file == csrc_pos.p_file) {
1110 csrc_pos.p_line = (int)ln - 1;
1111 csrc_pos.p_uniq = 0;
1112 }
1113 }
1114
1115 /*
1116 * Handle lint comments. Following comments are currently understood:
1117 * ARGSUSEDn
1118 * BITFIELDTYPE
1119 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1120 * FALLTHRU FALLTHROUGH
1121 * LINTLIBRARY
1122 * LINTEDn NOSTRICTn
1123 * LONGLONG
1124 * NOTREACHED
1125 * PRINTFLIKEn
1126 * PROTOLIB
1127 * SCANFLIKEn
1128 * VARARGSn
1129 * If one of these comments is recognized, the argument, if any, is
1130 * parsed and a function which handles this comment is called.
1131 */
1132 void
1133 lex_comment(void)
1134 {
1135 int c, lc;
1136 static const struct {
1137 const char *keywd;
1138 int arg;
1139 void (*func)(int);
1140 } keywtab[] = {
1141 { "ARGSUSED", 1, argsused },
1142 { "BITFIELDTYPE", 0, bitfieldtype },
1143 { "CONSTCOND", 0, constcond },
1144 { "CONSTANTCOND", 0, constcond },
1145 { "CONSTANTCONDITION", 0, constcond },
1146 { "FALLTHRU", 0, fallthru },
1147 { "FALLTHROUGH", 0, fallthru },
1148 { "LINTLIBRARY", 0, lintlib },
1149 { "LINTED", 1, linted },
1150 { "LONGLONG", 0, longlong },
1151 { "NOSTRICT", 1, linted },
1152 { "NOTREACHED", 0, notreach },
1153 { "PRINTFLIKE", 1, printflike },
1154 { "PROTOLIB", 1, protolib },
1155 { "SCANFLIKE", 1, scanflike },
1156 { "VARARGS", 1, varargs },
1157 };
1158 char keywd[32];
1159 char arg[32];
1160 size_t l, i;
1161 int a;
1162 bool eoc;
1163
1164 eoc = false;
1165
1166 /* Skip whitespace after the start of the comment */
1167 while ((c = inpc()) != EOF && isspace(c))
1168 continue;
1169
1170 /* Read the potential keyword to keywd */
1171 l = 0;
1172 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1173 keywd[l++] = (char)c;
1174 c = inpc();
1175 }
1176 keywd[l] = '\0';
1177
1178 /* look for the keyword */
1179 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1180 if (strcmp(keywtab[i].keywd, keywd) == 0)
1181 break;
1182 }
1183 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1184 goto skip_rest;
1185
1186 /* skip whitespace after the keyword */
1187 while (c != EOF && isspace(c))
1188 c = inpc();
1189
1190 /* read the argument, if the keyword accepts one and there is one */
1191 l = 0;
1192 if (keywtab[i].arg) {
1193 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1194 arg[l++] = (char)c;
1195 c = inpc();
1196 }
1197 }
1198 arg[l] = '\0';
1199 a = l != 0 ? atoi(arg) : -1;
1200
1201 /* skip whitespace after the argument */
1202 while (c != EOF && isspace(c))
1203 c = inpc();
1204
1205 if (c != '*' || (c = inpc()) != '/') {
1206 if (keywtab[i].func != linted)
1207 /* extra characters in lint comment */
1208 warning(257);
1209 } else {
1210 /*
1211 * remember that we have already found the end of the
1212 * comment
1213 */
1214 eoc = true;
1215 }
1216
1217 if (keywtab[i].func != NULL)
1218 (*keywtab[i].func)(a);
1219
1220 skip_rest:
1221 while (!eoc) {
1222 lc = c;
1223 if ((c = inpc()) == EOF) {
1224 /* unterminated comment */
1225 error(256);
1226 break;
1227 }
1228 if (lc == '*' && c == '/')
1229 eoc = true;
1230 }
1231 }
1232
1233 /*
1234 * Handle // style comments
1235 */
1236 void
1237 lex_slashslashcomment(void)
1238 {
1239 int c;
1240
1241 if (!Sflag && !gflag)
1242 /* %s C does not support // comments */
1243 gnuism(312, tflag ? "traditional" : "ANSI");
1244
1245 while ((c = inpc()) != EOF && c != '\n')
1246 continue;
1247 }
1248
1249 /*
1250 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1251 * clear_warn_flags() is called after function definitions and global and
1252 * local declarations and definitions. It is also called between
1253 * the controlling expression and the body of control statements
1254 * (if, switch, for, while).
1255 */
1256 void
1257 clear_warn_flags(void)
1258 {
1259
1260 lwarn = LWARN_ALL;
1261 quadflg = false;
1262 constcond_flag = false;
1263 }
1264
1265 /*
1266 * Strings are stored in a dynamically allocated buffer and passed
1267 * in yylval.y_xstrg to the parser. The parser or the routines called
1268 * by the parser are responsible for freeing this buffer.
1269 */
1270 int
1271 lex_string(void)
1272 {
1273 u_char *s;
1274 int c;
1275 size_t len, max;
1276 strg_t *strg;
1277
1278 s = xmalloc(max = 64);
1279
1280 len = 0;
1281 while ((c = getescc('"')) >= 0) {
1282 /* +1 to reserve space for a trailing NUL character */
1283 if (len + 1 == max)
1284 s = xrealloc(s, max *= 2);
1285 s[len++] = (char)c;
1286 }
1287 s[len] = '\0';
1288 if (c == -2)
1289 /* unterminated string constant */
1290 error(258);
1291
1292 strg = xcalloc(1, sizeof (strg_t));
1293 strg->st_tspec = CHAR;
1294 strg->st_len = len;
1295 strg->st_cp = s;
1296
1297 yylval.y_string = strg;
1298 return T_STRING;
1299 }
1300
1301 int
1302 lex_wcstrg(void)
1303 {
1304 char *s;
1305 int c, n;
1306 size_t i, wi;
1307 size_t len, max, wlen;
1308 wchar_t *ws;
1309 strg_t *strg;
1310
1311 s = xmalloc(max = 64);
1312 len = 0;
1313 while ((c = getescc('"')) >= 0) {
1314 /* +1 to save space for a trailing NUL character */
1315 if (len + 1 >= max)
1316 s = xrealloc(s, max *= 2);
1317 s[len++] = (char)c;
1318 }
1319 s[len] = '\0';
1320 if (c == -2)
1321 /* unterminated string constant */
1322 error(258);
1323
1324 /* get length of wide-character string */
1325 (void)mblen(NULL, 0);
1326 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1327 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1328 /* invalid multibyte character */
1329 error(291);
1330 break;
1331 }
1332 if (n == 0)
1333 n = 1;
1334 }
1335
1336 ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1337
1338 /* convert from multibyte to wide char */
1339 (void)mbtowc(NULL, NULL, 0);
1340 for (i = 0, wi = 0; i < len; i += n, wi++) {
1341 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1342 break;
1343 if (n == 0)
1344 n = 1;
1345 }
1346 ws[wi] = 0;
1347 free(s);
1348
1349 strg = xcalloc(1, sizeof (strg_t));
1350 strg->st_tspec = WCHAR;
1351 strg->st_len = wlen;
1352 strg->st_wcp = ws;
1353
1354 yylval.y_string = strg;
1355 return T_STRING;
1356 }
1357
1358 /*
1359 * As noted above the scanner does not create new symbol table entries
1360 * for symbols it cannot find in the symbol table. This is to avoid
1361 * putting undeclared symbols into the symbol table if a syntax error
1362 * occurs.
1363 *
1364 * getsym() is called as soon as it is probably ok to put the symbol to
1365 * the symbol table. This does not mean that it is not possible that
1366 * symbols are put to the symbol table which are not completely
1367 * declared due to syntax errors. To avoid too many problems in this
1368 * case, symbols get type int in getsym().
1369 *
1370 * XXX calls to getsym() should be delayed until decl1*() is called
1371 */
1372 sym_t *
1373 getsym(sbuf_t *sb)
1374 {
1375 dinfo_t *di;
1376 char *s;
1377 sym_t *sym;
1378
1379 sym = sb->sb_sym;
1380
1381 /*
1382 * During member declaration it is possible that name() looked
1383 * for symbols of type FVFT, although it should have looked for
1384 * symbols of type FTAG. Same can happen for labels. Both cases
1385 * are compensated here.
1386 */
1387 if (symtyp == FMEMBER || symtyp == FLABEL) {
1388 if (sym == NULL || sym->s_kind == FVFT)
1389 sym = search(sb);
1390 }
1391
1392 if (sym != NULL) {
1393 if (sym->s_kind != symtyp)
1394 LERROR("getsym(%d, %d)", sym->s_kind, symtyp);
1395 symtyp = FVFT;
1396 freesb(sb);
1397 return sym;
1398 }
1399
1400 /* create a new symbol table entry */
1401
1402 /* labels must always be allocated at level 1 (outermost block) */
1403 if (symtyp == FLABEL) {
1404 sym = getlblk(1, sizeof (sym_t));
1405 s = getlblk(1, sb->sb_len + 1);
1406 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1407 sym->s_name = s;
1408 sym->s_blklev = 1;
1409 di = dcs;
1410 while (di->d_next != NULL && di->d_next->d_next != NULL)
1411 di = di->d_next;
1412 lint_assert(di->d_ctx == AUTO);
1413 } else {
1414 sym = getblk(sizeof (sym_t));
1415 sym->s_name = sb->sb_name;
1416 sym->s_blklev = blklev;
1417 di = dcs;
1418 }
1419
1420 UNIQUE_CURR_POS(sym->s_def_pos);
1421 if ((sym->s_kind = symtyp) != FLABEL)
1422 sym->s_type = gettyp(INT);
1423
1424 symtyp = FVFT;
1425
1426 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1427 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1428 sym->s_rlink = &symtab[sb->sb_hash];
1429 symtab[sb->sb_hash] = sym;
1430
1431 *di->d_ldlsym = sym;
1432 di->d_ldlsym = &sym->s_dlnxt;
1433
1434 freesb(sb);
1435 return sym;
1436 }
1437
1438 /*
1439 * Construct a temporary symbol. The symbol starts with a digit, so that
1440 * it is illegal.
1441 */
1442 sym_t *
1443 mktempsym(type_t *t)
1444 {
1445 static int n = 0;
1446 int h;
1447 char *s = getlblk(blklev, 64);
1448 sym_t *sym = getblk(sizeof (sym_t));
1449
1450 (void)snprintf(s, 64, "%.8d_tmp", n++);
1451 h = hash(s);
1452
1453 sym->s_name = s;
1454 sym->s_type = t;
1455 sym->s_blklev = blklev;
1456 sym->s_scl = AUTO;
1457 sym->s_kind = FVFT;
1458 sym->s_used = true;
1459 sym->s_set = true;
1460
1461 if ((sym->s_link = symtab[h]) != NULL)
1462 symtab[h]->s_rlink = &sym->s_link;
1463 sym->s_rlink = &symtab[h];
1464 symtab[h] = sym;
1465
1466 *dcs->d_ldlsym = sym;
1467 dcs->d_ldlsym = &sym->s_dlnxt;
1468
1469 return sym;
1470 }
1471
1472 /*
1473 * Remove a symbol forever from the symbol table. s_blklev
1474 * is set to -1 to avoid that the symbol will later be put
1475 * back to the symbol table.
1476 */
1477 void
1478 rmsym(sym_t *sym)
1479 {
1480
1481 if ((*sym->s_rlink = sym->s_link) != NULL)
1482 sym->s_link->s_rlink = sym->s_rlink;
1483 sym->s_blklev = -1;
1484 sym->s_link = NULL;
1485 }
1486
1487 /*
1488 * Remove a list of symbols declared at one level from the symbol
1489 * table.
1490 */
1491 void
1492 rmsyms(sym_t *syms)
1493 {
1494 sym_t *sym;
1495
1496 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1497 if (sym->s_blklev != -1) {
1498 if ((*sym->s_rlink = sym->s_link) != NULL)
1499 sym->s_link->s_rlink = sym->s_rlink;
1500 sym->s_link = NULL;
1501 sym->s_rlink = NULL;
1502 }
1503 }
1504 }
1505
1506 /*
1507 * Put a symbol into the symbol table
1508 */
1509 void
1510 inssym(int bl, sym_t *sym)
1511 {
1512 int h;
1513
1514 h = hash(sym->s_name);
1515 if ((sym->s_link = symtab[h]) != NULL)
1516 symtab[h]->s_rlink = &sym->s_link;
1517 sym->s_rlink = &symtab[h];
1518 symtab[h] = sym;
1519 sym->s_blklev = bl;
1520 lint_assert(sym->s_link == NULL ||
1521 sym->s_blklev >= sym->s_link->s_blklev);
1522 }
1523
1524 /*
1525 * Called at level 0 after syntax errors
1526 * Removes all symbols which are not declared at level 0 from the
1527 * symbol table. Also frees all memory which is not associated with
1528 * level 0.
1529 */
1530 void
1531 cleanup(void)
1532 {
1533 sym_t *sym, *nsym;
1534 int i;
1535
1536 for (i = 0; i < HSHSIZ1; i++) {
1537 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1538 nsym = sym->s_link;
1539 if (sym->s_blklev >= 1) {
1540 if ((*sym->s_rlink = nsym) != NULL)
1541 nsym->s_rlink = sym->s_rlink;
1542 }
1543 }
1544 }
1545
1546 for (i = mblklev; i > 0; i--)
1547 freelblk(i);
1548 }
1549
1550 /*
1551 * Create a new symbol with the name of an existing symbol.
1552 */
1553 sym_t *
1554 pushdown(sym_t *sym)
1555 {
1556 int h;
1557 sym_t *nsym;
1558
1559 h = hash(sym->s_name);
1560 nsym = getblk(sizeof (sym_t));
1561 lint_assert(sym->s_blklev <= blklev);
1562 nsym->s_name = sym->s_name;
1563 UNIQUE_CURR_POS(nsym->s_def_pos);
1564 nsym->s_kind = sym->s_kind;
1565 nsym->s_blklev = blklev;
1566
1567 if ((nsym->s_link = symtab[h]) != NULL)
1568 symtab[h]->s_rlink = &nsym->s_link;
1569 nsym->s_rlink = &symtab[h];
1570 symtab[h] = nsym;
1571
1572 *dcs->d_ldlsym = nsym;
1573 dcs->d_ldlsym = &nsym->s_dlnxt;
1574
1575 return nsym;
1576 }
1577
1578 /*
1579 * Free any dynamically allocated memory referenced by
1580 * the value stack or yylval.
1581 * The type of information in yylval is described by tok.
1582 */
1583 void
1584 freeyyv(void *sp, int tok)
1585 {
1586 if (tok == T_NAME || tok == T_TYPENAME) {
1587 sbuf_t *sb = *(sbuf_t **)sp;
1588 freesb(sb);
1589 } else if (tok == T_CON) {
1590 val_t *val = *(val_t **)sp;
1591 free(val);
1592 } else if (tok == T_STRING) {
1593 strg_t *strg = *(strg_t **)sp;
1594 if (strg->st_tspec == CHAR) {
1595 free(strg->st_cp);
1596 } else {
1597 lint_assert(strg->st_tspec == WCHAR);
1598 free(strg->st_wcp);
1599 }
1600 free(strg);
1601 }
1602 }
1603