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