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