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