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