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