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