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