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