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