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