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