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