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