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