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