scan.l revision 1.51 1 %{
2 /* $NetBSD: scan.l,v 1.51 2013/04/19 17:43:05 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.51 2013/04/19 17:43:05 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 if (c == 'd' || c == 'D')
720 len--;
721 typ = DOUBLE;
722 }
723
724 if (tflag && typ != DOUBLE) {
725 /* suffixes F and L are illegal in traditional C */
726 warning(98);
727 }
728
729 errno = 0;
730 d = strtod(cp, &eptr);
731 if (eptr != cp + len) {
732 switch (*eptr) {
733 /*
734 * XXX: non-native non-current strtod() may not handle hex
735 * floats, ignore the rest if we find traces of hex float
736 * syntax...
737 */
738 case 'p':
739 case 'P':
740 case 'x':
741 case 'X':
742 d = 0;
743 errno = 0;
744 break;
745 default:
746 LERROR("fcon(%s->%s)", cp, eptr);
747 }
748 }
749 if (errno != 0)
750 /* floating-point constant out of range */
751 warning(248);
752
753 if (typ == FLOAT) {
754 f = (float)d;
755 if (!finite(f)) {
756 /* floating-point constant out of range */
757 warning(248);
758 f = f > 0 ? FLT_MAX : -FLT_MAX;
759 }
760 }
761
762 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
763 if (typ == FLOAT) {
764 yylval.y_val->v_ldbl = f;
765 } else {
766 yylval.y_val->v_ldbl = d;
767 }
768
769 return (T_CON);
770 }
771
772 static int
773 operator(int t, op_t o)
774 {
775
776 yylval.y_op = o;
777 return (t);
778 }
779
780 /*
781 * Called if lex found a leading \'.
782 */
783 static int
784 ccon(void)
785 {
786 size_t n;
787 int val, c;
788 char cv;
789
790 n = 0;
791 val = 0;
792 while ((c = getescc('\'')) >= 0) {
793 val = (val << CHAR_BIT) + c;
794 n++;
795 }
796 if (c == -2) {
797 /* unterminated character constant */
798 error(253);
799 } else {
800 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
801 /* too many characters in character constant */
802 error(71);
803 } else if (n > 1) {
804 /* multi-character character constant */
805 warning(294);
806 } else if (n == 0) {
807 /* empty character constant */
808 error(73);
809 }
810 }
811 if (n == 1) {
812 cv = (char)val;
813 val = cv;
814 }
815
816 yylval.y_val = xcalloc(1, sizeof (val_t));
817 yylval.y_val->v_tspec = INT;
818 yylval.y_val->v_quad = val;
819
820 return (T_CON);
821 }
822
823 /*
824 * Called if lex found a leading L\'
825 */
826 static int
827 wccon(void)
828 {
829 static char buf[MB_LEN_MAX + 1];
830 size_t i;
831 int c;
832 wchar_t wc;
833
834 i = 0;
835 while ((c = getescc('\'')) >= 0) {
836 if (i < MB_CUR_MAX)
837 buf[i] = (char)c;
838 i++;
839 }
840
841 wc = 0;
842
843 if (c == -2) {
844 /* unterminated character constant */
845 error(253);
846 } else if (c == 0) {
847 /* empty character constant */
848 error(73);
849 } else {
850 if (i > MB_CUR_MAX) {
851 i = MB_CUR_MAX;
852 /* too many characters in character constant */
853 error(71);
854 } else {
855 buf[i] = '\0';
856 (void)mbtowc(NULL, NULL, 0);
857 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
858 /* invalid multibyte character */
859 error(291);
860 }
861 }
862
863 yylval.y_val = xcalloc(1, sizeof (val_t));
864 yylval.y_val->v_tspec = WCHAR;
865 yylval.y_val->v_quad = wc;
866
867 return (T_CON);
868 }
869
870 /*
871 * Read a character which is part of a character constant or of a string
872 * and handle escapes.
873 *
874 * The Argument is the character which delimits the character constant or
875 * string.
876 *
877 * Returns -1 if the end of the character constant or string is reached,
878 * -2 if the EOF is reached, and the character otherwise.
879 */
880 static int
881 getescc(int d)
882 {
883 static int pbc = -1;
884 int n, c, v;
885
886 if (pbc == -1) {
887 c = inpc();
888 } else {
889 c = pbc;
890 pbc = -1;
891 }
892 if (c == d)
893 return (-1);
894 switch (c) {
895 case '\n':
896 if (tflag) {
897 /* newline in string or char constant */
898 error(254);
899 return (-2);
900 }
901 return (c);
902 case EOF:
903 return (-2);
904 case '\\':
905 switch (c = inpc()) {
906 case '"':
907 if (tflag && d == '\'')
908 /* \" inside character constant undef. ... */
909 warning(262);
910 return ('"');
911 case '\'':
912 return ('\'');
913 case '?':
914 if (tflag)
915 /* \? undefined in traditional C */
916 warning(263);
917 return ('?');
918 case '\\':
919 return ('\\');
920 case 'a':
921 if (tflag)
922 /* \a undefined in traditional C */
923 warning(81);
924 return ('\a');
925 case 'b':
926 return ('\b');
927 case 'f':
928 return ('\f');
929 case 'n':
930 return ('\n');
931 case 'r':
932 return ('\r');
933 case 't':
934 return ('\t');
935 case 'v':
936 if (tflag)
937 /* \v undefined in traditional C */
938 warning(264);
939 return ('\v');
940 case '8': case '9':
941 /* bad octal digit %c */
942 warning(77, c);
943 /* FALLTHROUGH */
944 case '0': case '1': case '2': case '3':
945 case '4': case '5': case '6': case '7':
946 n = 3;
947 v = 0;
948 do {
949 v = (v << 3) + (c - '0');
950 c = inpc();
951 } while (--n && isdigit(c) && (tflag || c <= '7'));
952 if (tflag && n > 0 && isdigit(c))
953 /* bad octal digit %c */
954 warning(77, c);
955 pbc = c;
956 if (v > TARG_UCHAR_MAX) {
957 /* character escape does not fit in char. */
958 warning(76);
959 v &= CHAR_MASK;
960 }
961 return (v);
962 case 'x':
963 if (tflag)
964 /* \x undefined in traditional C */
965 warning(82);
966 v = 0;
967 n = 0;
968 while ((c = inpc()) >= 0 && isxdigit(c)) {
969 c = isdigit(c) ?
970 c - '0' : toupper(c) - 'A' + 10;
971 v = (v << 4) + c;
972 if (n >= 0) {
973 if ((v & ~CHAR_MASK) != 0) {
974 /* overflow in hex escape */
975 warning(75);
976 n = -1;
977 } else {
978 n++;
979 }
980 }
981 }
982 pbc = c;
983 if (n == 0) {
984 /* no hex digits follow \x */
985 error(74);
986 } if (n == -1) {
987 v &= CHAR_MASK;
988 }
989 return (v);
990 case '\n':
991 return (getescc(d));
992 case EOF:
993 return (-2);
994 default:
995 if (isprint(c)) {
996 /* dubious escape \%c */
997 warning(79, c);
998 } else {
999 /* dubious escape \%o */
1000 warning(80, c);
1001 }
1002 }
1003 }
1004 return (c);
1005 }
1006
1007 /*
1008 * Called for preprocessor directives. Currently implemented are:
1009 * # lineno
1010 * # lineno "filename"
1011 */
1012 static void
1013 directive(void)
1014 {
1015 const char *cp, *fn;
1016 char c, *eptr;
1017 size_t fnl;
1018 long ln;
1019 static int first = 1;
1020
1021 /* Go to first non-whitespace after # */
1022 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
1023 continue;
1024
1025 if (!isdigit((unsigned char)c)) {
1026 error:
1027 /* undefined or invalid # directive */
1028 warning(255);
1029 return;
1030 }
1031 ln = strtol(--cp, &eptr, 10);
1032 if (cp == eptr)
1033 goto error;
1034 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
1035 goto error;
1036 while ((c = *cp++) == ' ' || c == '\t')
1037 continue;
1038 if (c != '\0') {
1039 if (c != '"')
1040 goto error;
1041 fn = cp;
1042 while ((c = *cp) != '"' && c != '\0')
1043 cp++;
1044 if (c != '"')
1045 goto error;
1046 if ((fnl = cp++ - fn) > PATH_MAX)
1047 goto error;
1048 while ((c = *cp++) == ' ' || c == '\t')
1049 continue;
1050 #if 0
1051 if (c != '\0')
1052 warning("extra character(s) after directive");
1053 #endif
1054
1055 /* empty string means stdin */
1056 if (fnl == 0) {
1057 fn = "{standard input}";
1058 fnl = 16; /* strlen (fn) */
1059 }
1060 curr_pos.p_file = fnnalloc(fn, fnl);
1061 /*
1062 * If this is the first directive, the name is the name
1063 * of the C source file as specified at the command line.
1064 * It is written to the output file.
1065 */
1066 if (first) {
1067 csrc_pos.p_file = curr_pos.p_file;
1068 outsrc(curr_pos.p_file);
1069 first = 0;
1070 }
1071 }
1072 curr_pos.p_line = (int)ln - 1;
1073 curr_pos.p_uniq = 0;
1074 if (curr_pos.p_file == csrc_pos.p_file) {
1075 csrc_pos.p_line = (int)ln - 1;
1076 csrc_pos.p_uniq = 0;
1077 }
1078 }
1079
1080 /*
1081 * Handle lint comments. Following comments are currently understood:
1082 * ARGSUSEDn
1083 * BITFIELDTYPE
1084 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1085 * FALLTHRU FALLTHROUGH
1086 * LINTLIBRARY
1087 * LINTED NOSTRICT
1088 * LONGLONG
1089 * NOTREACHED
1090 * PRINTFLIKEn
1091 * PROTOLIB
1092 * SCANFLIKEn
1093 * VARARGSn
1094 * If one of this comments is recognized, the arguments, if any, are
1095 * parsed and a function which handles this comment is called.
1096 */
1097 static void
1098 comment(void)
1099 {
1100 int c, lc;
1101 static struct {
1102 const char *keywd;
1103 int arg;
1104 void (*func)(int);
1105 } keywtab[] = {
1106 { "ARGSUSED", 1, argsused },
1107 { "BITFIELDTYPE", 0, bitfieldtype },
1108 { "CONSTCOND", 0, constcond },
1109 { "CONSTANTCOND", 0, constcond },
1110 { "CONSTANTCONDITION", 0, constcond },
1111 { "FALLTHRU", 0, fallthru },
1112 { "FALLTHROUGH", 0, fallthru },
1113 { "LINTLIBRARY", 0, lintlib },
1114 { "LINTED", 1, linted },
1115 { "LONGLONG", 0, longlong },
1116 { "NOSTRICT", 0, linted },
1117 { "NOTREACHED", 0, notreach },
1118 { "PRINTFLIKE", 1, printflike },
1119 { "PROTOLIB", 1, protolib },
1120 { "SCANFLIKE", 1, scanflike },
1121 { "VARARGS", 1, varargs },
1122 };
1123 char keywd[32];
1124 char arg[32];
1125 size_t l, i;
1126 int a;
1127 int eoc;
1128
1129 eoc = 0;
1130
1131 /* Skip white spaces after the start of the comment */
1132 while ((c = inpc()) != EOF && isspace(c))
1133 continue;
1134
1135 /* Read the potential keyword to keywd */
1136 l = 0;
1137 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1138 keywd[l++] = (char)c;
1139 c = inpc();
1140 }
1141 keywd[l] = '\0';
1142
1143 /* look for the keyword */
1144 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1145 if (strcmp(keywtab[i].keywd, keywd) == 0)
1146 break;
1147 }
1148 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1149 goto skip_rest;
1150
1151 /* skip white spaces after the keyword */
1152 while (c != EOF && isspace(c))
1153 c = inpc();
1154
1155 /* read the argument, if the keyword accepts one and there is one */
1156 l = 0;
1157 if (keywtab[i].arg) {
1158 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1159 arg[l++] = (char)c;
1160 c = inpc();
1161 }
1162 }
1163 arg[l] = '\0';
1164 a = l != 0 ? atoi(arg) : -1;
1165
1166 /* skip white spaces after the argument */
1167 while (c != EOF && isspace(c))
1168 c = inpc();
1169
1170 if (c != '*' || (c = inpc()) != '/') {
1171 if (keywtab[i].func != linted)
1172 /* extra characters in lint comment */
1173 warning(257);
1174 } else {
1175 /*
1176 * remember that we have already found the end of the
1177 * comment
1178 */
1179 eoc = 1;
1180 }
1181
1182 if (keywtab[i].func != NULL)
1183 (*keywtab[i].func)(a);
1184
1185 skip_rest:
1186 while (!eoc) {
1187 lc = c;
1188 if ((c = inpc()) == EOF) {
1189 /* unterminated comment */
1190 error(256);
1191 break;
1192 }
1193 if (lc == '*' && c == '/')
1194 eoc = 1;
1195 }
1196 }
1197
1198 /*
1199 * Handle // style comments
1200 */
1201 static void
1202 slashslashcomment(void)
1203 {
1204 int c;
1205
1206 if (!Sflag && !gflag)
1207 /* // comments only supported in C99 */
1208 (void)gnuism(312, tflag ? "traditional" : "ANSI");
1209
1210 while ((c = inpc()) != EOF && c != '\n')
1211 continue;
1212 }
1213
1214 /*
1215 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1216 * clrwflgs() is called after function definitions and global and
1217 * local declarations and definitions. It is also called between
1218 * the controlling expression and the body of control statements
1219 * (if, switch, for, while).
1220 */
1221 void
1222 clrwflgs(void)
1223 {
1224
1225 lwarn = LWARN_ALL;
1226 quadflg = 0;
1227 ccflg = 0;
1228 }
1229
1230 /*
1231 * Strings are stored in a dynamically alloceted buffer and passed
1232 * in yylval.y_xstrg to the parser. The parser or the routines called
1233 * by the parser are responsible for freeing this buffer.
1234 */
1235 static int
1236 string(void)
1237 {
1238 u_char *s;
1239 int c;
1240 size_t len, max;
1241 strg_t *strg;
1242
1243 s = xmalloc(max = 64);
1244
1245 len = 0;
1246 while ((c = getescc('"')) >= 0) {
1247 /* +1 to reserve space for a trailing NUL character */
1248 if (len + 1 == max)
1249 s = xrealloc(s, max *= 2);
1250 s[len++] = (char)c;
1251 }
1252 s[len] = '\0';
1253 if (c == -2)
1254 /* unterminated string constant */
1255 error(258);
1256
1257 strg = xcalloc(1, sizeof (strg_t));
1258 strg->st_tspec = CHAR;
1259 strg->st_len = len;
1260 strg->st_cp = s;
1261
1262 yylval.y_strg = strg;
1263 return (T_STRING);
1264 }
1265
1266 static int
1267 wcstrg(void)
1268 {
1269 char *s;
1270 int c, n;
1271 size_t i, wi;
1272 size_t len, max, wlen;
1273 wchar_t *ws;
1274 strg_t *strg;
1275
1276 s = xmalloc(max = 64);
1277 len = 0;
1278 while ((c = getescc('"')) >= 0) {
1279 /* +1 to save space for a trailing NUL character */
1280 if (len + 1 >= max)
1281 s = xrealloc(s, max *= 2);
1282 s[len++] = (char)c;
1283 }
1284 s[len] = '\0';
1285 if (c == -2)
1286 /* unterminated string constant */
1287 error(258);
1288
1289 /* get length of wide-character string */
1290 (void)mblen(NULL, 0);
1291 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1292 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1293 /* invalid multibyte character */
1294 error(291);
1295 break;
1296 }
1297 if (n == 0)
1298 n = 1;
1299 }
1300
1301 ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1302
1303 /* convert from multibyte to wide char */
1304 (void)mbtowc(NULL, NULL, 0);
1305 for (i = 0, wi = 0; i < len; i += n, wi++) {
1306 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1307 break;
1308 if (n == 0)
1309 n = 1;
1310 }
1311 ws[wi] = 0;
1312 free(s);
1313
1314 strg = xcalloc(1, sizeof (strg_t));
1315 strg->st_tspec = WCHAR;
1316 strg->st_len = wlen;
1317 strg->st_wcp = ws;
1318
1319 yylval.y_strg = strg;
1320 return (T_STRING);
1321 }
1322
1323 /*
1324 * As noted above the scanner does not create new symbol table entries
1325 * for symbols it cannot find in the symbol table. This is to avoid
1326 * putting undeclared symbols into the symbol table if a syntax error
1327 * occurs.
1328 *
1329 * getsym() is called as soon as it is probably ok to put the symbol to
1330 * the symbol table. This does not mean that it is not possible that
1331 * symbols are put to the symbol table which are than not completely
1332 * declared due to syntax errors. To avoid too many problems in this
1333 * case symbols get type int in getsym().
1334 *
1335 * XXX calls to getsym() should be delayed until decl1*() is called
1336 */
1337 sym_t *
1338 getsym(sbuf_t *sb)
1339 {
1340 dinfo_t *di;
1341 char *s;
1342 sym_t *sym;
1343
1344 sym = sb->sb_sym;
1345
1346 /*
1347 * During member declaration it is possible that name() looked
1348 * for symbols of type FVFT, although it should have looked for
1349 * symbols of type FTAG. Same can happen for labels. Both cases
1350 * are compensated here.
1351 */
1352 if (symtyp == FMOS || symtyp == FLAB) {
1353 if (sym == NULL || sym->s_kind == FVFT)
1354 sym = search(sb);
1355 }
1356
1357 if (sym != NULL) {
1358 if (sym->s_kind != symtyp)
1359 LERROR("storesym()");
1360 symtyp = FVFT;
1361 freesb(sb);
1362 return (sym);
1363 }
1364
1365 /* create a new symbol table entry */
1366
1367 /* labels must always be allocated at level 1 (outhermost block) */
1368 if (symtyp == FLAB) {
1369 sym = getlblk(1, sizeof (sym_t));
1370 s = getlblk(1, sb->sb_len + 1);
1371 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1372 sym->s_name = s;
1373 sym->s_blklev = 1;
1374 di = dcs;
1375 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1376 di = di->d_nxt;
1377 if (di->d_ctx != AUTO)
1378 LERROR("storesym()");
1379 } else {
1380 sym = getblk(sizeof (sym_t));
1381 sym->s_name = sb->sb_name;
1382 sym->s_blklev = blklev;
1383 di = dcs;
1384 }
1385
1386 UNIQUE_CURR_POS(sym->s_dpos);
1387 if ((sym->s_kind = symtyp) != FLAB)
1388 sym->s_type = gettyp(INT);
1389
1390 symtyp = FVFT;
1391
1392 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1393 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1394 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1395
1396 *di->d_ldlsym = sym;
1397 di->d_ldlsym = &sym->s_dlnxt;
1398
1399 freesb(sb);
1400 return (sym);
1401 }
1402
1403 /*
1404 * Construct a temporary symbol. The symbol starts with a digit, so that
1405 * it is illegal.
1406 */
1407 sym_t *
1408 mktempsym(type_t *t)
1409 {
1410 static int n = 0;
1411 int h;
1412 char *s = getlblk(blklev, 64);
1413 sym_t *sym = getblk(sizeof (sym_t));
1414
1415 (void)snprintf(s, 64, "%.8d_tmp", n++);
1416 h = hash(s);
1417
1418 sym->s_name = s;
1419 sym->s_type = t;
1420 sym->s_blklev = blklev;
1421 sym->s_scl = AUTO;
1422 sym->s_kind = FVFT;
1423 sym->s_used = 1;
1424 sym->s_set = 1;
1425
1426 if ((sym->s_link = symtab[h]) != NULL)
1427 symtab[h]->s_rlink = &sym->s_link;
1428 (symtab[h] = sym)->s_rlink = &symtab[h];
1429
1430 *dcs->d_ldlsym = sym;
1431 dcs->d_ldlsym = &sym->s_dlnxt;
1432
1433 return sym;
1434 }
1435
1436 /*
1437 * Remove a symbol forever from the symbol table. s_blklev
1438 * is set to -1 to avoid that the symbol will later be put
1439 * back to the symbol table.
1440 */
1441 void
1442 rmsym(sym_t *sym)
1443 {
1444
1445 if ((*sym->s_rlink = sym->s_link) != NULL)
1446 sym->s_link->s_rlink = sym->s_rlink;
1447 sym->s_blklev = -1;
1448 sym->s_link = NULL;
1449 }
1450
1451 /*
1452 * Remove a list of symbols declared at one level from the symbol
1453 * table.
1454 */
1455 void
1456 rmsyms(sym_t *syms)
1457 {
1458 sym_t *sym;
1459
1460 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1461 if (sym->s_blklev != -1) {
1462 if ((*sym->s_rlink = sym->s_link) != NULL)
1463 sym->s_link->s_rlink = sym->s_rlink;
1464 sym->s_link = NULL;
1465 sym->s_rlink = NULL;
1466 }
1467 }
1468 }
1469
1470 /*
1471 * Put a symbol into the symbol table
1472 */
1473 void
1474 inssym(int bl, sym_t *sym)
1475 {
1476 int h;
1477
1478 h = hash(sym->s_name);
1479 if ((sym->s_link = symtab[h]) != NULL)
1480 symtab[h]->s_rlink = &sym->s_link;
1481 (symtab[h] = sym)->s_rlink = &symtab[h];
1482 sym->s_blklev = bl;
1483 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1484 LERROR("inssym()");
1485 }
1486
1487 /*
1488 * Called at level 0 after syntax errors
1489 * Removes all symbols which are not declared at level 0 from the
1490 * symbol table. Also frees all memory which is not associated with
1491 * level 0.
1492 */
1493 void
1494 cleanup(void)
1495 {
1496 sym_t *sym, *nsym;
1497 int i;
1498
1499 for (i = 0; i < HSHSIZ1; i++) {
1500 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1501 nsym = sym->s_link;
1502 if (sym->s_blklev >= 1) {
1503 if ((*sym->s_rlink = nsym) != NULL)
1504 nsym->s_rlink = sym->s_rlink;
1505 }
1506 }
1507 }
1508
1509 for (i = mblklev; i > 0; i--)
1510 freelblk(i);
1511 }
1512
1513 /*
1514 * Create a new symbol with the name of an existing symbol.
1515 */
1516 sym_t *
1517 pushdown(sym_t *sym)
1518 {
1519 int h;
1520 sym_t *nsym;
1521
1522 h = hash(sym->s_name);
1523 nsym = getblk(sizeof (sym_t));
1524 if (sym->s_blklev > blklev)
1525 LERROR("pushdown()");
1526 nsym->s_name = sym->s_name;
1527 UNIQUE_CURR_POS(nsym->s_dpos);
1528 nsym->s_kind = sym->s_kind;
1529 nsym->s_blklev = blklev;
1530
1531 if ((nsym->s_link = symtab[h]) != NULL)
1532 symtab[h]->s_rlink = &nsym->s_link;
1533 (symtab[h] = nsym)->s_rlink = &symtab[h];
1534
1535 *dcs->d_ldlsym = nsym;
1536 dcs->d_ldlsym = &nsym->s_dlnxt;
1537
1538 return (nsym);
1539 }
1540
1541 /*
1542 * Free any dynamically allocated memory referenced by
1543 * the value stack or yylval.
1544 * The type of information in yylval is described by tok.
1545 */
1546 void
1547 freeyyv(void *sp, int tok)
1548 {
1549 if (tok == T_NAME || tok == T_TYPENAME) {
1550 sbuf_t *sb = *(sbuf_t **)sp;
1551 freesb(sb);
1552 } else if (tok == T_CON) {
1553 val_t *val = *(val_t **)sp;
1554 free(val);
1555 } else if (tok == T_STRING) {
1556 strg_t *strg = *(strg_t **)sp;
1557 if (strg->st_tspec == CHAR) {
1558 free(strg->st_cp);
1559 } else if (strg->st_tspec == WCHAR) {
1560 free(strg->st_wcp);
1561 } else {
1562 LERROR("fryylv()");
1563 }
1564 free(strg);
1565 }
1566 }
1567