scan.l revision 1.34 1 %{
2 /* $NetBSD: scan.l,v 1.34 2006/03/22 02:14:03 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.34 2006/03/22 02:14:03 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 (~(~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 #if INT_MAX != LONG_MAX
528 } else if (ul <= LONG_MAX) {
529 typ = LONG;
530 #endif
531 } else {
532 typ = ULONG;
533 }
534 if (typ == UINT || typ == ULONG) {
535 if (tflag) {
536 typ = LONG;
537 } else if (!sflag) {
538 /*
539 * Remember that the constant is unsigned
540 * only in ANSI C
541 */
542 ansiu = 1;
543 }
544 }
545 break;
546 case UINT:
547 if (ul > (u_int)UINT_MAX)
548 typ = ULONG;
549 break;
550 case LONG:
551 if (ul > LONG_MAX && !tflag) {
552 typ = ULONG;
553 if (!sflag)
554 ansiu = 1;
555 }
556 break;
557 case QUAD:
558 if (uq > QUAD_MAX && !tflag) {
559 typ = UQUAD;
560 if (!sflag)
561 ansiu = 1;
562 }
563 break;
564 /* LINTED (enumeration values not handled in switch) */
565 case STRUCT:
566 case VOID:
567 case LDOUBLE:
568 case FUNC:
569 case ARRAY:
570 case PTR:
571 case ENUM:
572 case UNION:
573 case SIGNED:
574 case NOTSPEC:
575 case DOUBLE:
576 case FLOAT:
577 case UQUAD:
578 case ULONG:
579 case USHORT:
580 case SHORT:
581 case UCHAR:
582 case SCHAR:
583 case CHAR:
584 case BOOL:
585 case UNSIGN:
586 break;
587
588 case NTSPEC: /* this value unused */
589 break;
590 }
591
592 if (typ != QUAD && typ != UQUAD) {
593 if (isutyp(typ)) {
594 uq = ul;
595 } else {
596 uq = (int64_t)(long)ul;
597 }
598 }
599
600 uq = (uint64_t)xsign((int64_t)uq, typ, -1);
601
602 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
603 yylval.y_val->v_ansiu = ansiu;
604 yylval.y_val->v_quad = (int64_t)uq;
605
606 return (T_CON);
607 }
608
609 /*
610 * Returns 1 if t is a signed type and the value is negative.
611 *
612 * len is the number of significant bits. If len is -1, len is set
613 * to the width of type t.
614 */
615 int
616 sign(int64_t q, tspec_t t, int len)
617 {
618
619 if (t == PTR || isutyp(t))
620 return (0);
621 return (msb(q, t, len));
622 }
623
624 int
625 msb(int64_t q, tspec_t t, int len)
626 {
627
628 if (len <= 0)
629 len = size(t);
630 return ((q & qbmasks[len - 1]) != 0);
631 }
632
633 /*
634 * Extends the sign of q.
635 */
636 int64_t
637 xsign(int64_t q, tspec_t t, int len)
638 {
639
640 if (len <= 0)
641 len = size(t);
642
643 if (t == PTR || isutyp(t) || !sign(q, t, len)) {
644 q &= qlmasks[len];
645 } else {
646 q |= qumasks[len];
647 }
648 return (q);
649 }
650
651 /*
652 * Convert a string representing a floating point value into its interal
653 * representation. Type and value are returned in yylval. fcon()
654 * (and yylex()) returns T_CON.
655 * XXX Currently it is not possible to convert constants of type
656 * long double which are greater than DBL_MAX.
657 */
658 static int
659 fcon(void)
660 {
661 const char *cp;
662 int len;
663 tspec_t typ;
664 char c, *eptr;
665 double d;
666 float f = 0;
667
668 cp = yytext;
669 len = yyleng;
670
671 if ((c = cp[len - 1]) == 'f' || c == 'F') {
672 typ = FLOAT;
673 len--;
674 } else if (c == 'l' || c == 'L') {
675 typ = LDOUBLE;
676 len--;
677 } else {
678 typ = DOUBLE;
679 }
680
681 if (tflag && typ != DOUBLE) {
682 /* suffixes F and L are illegal in traditional C */
683 warning(98);
684 }
685
686 errno = 0;
687 d = strtod(cp, &eptr);
688 if (eptr != cp + len)
689 LERROR("fcon()");
690 if (errno != 0)
691 /* floating-point constant out of range */
692 warning(248);
693
694 if (typ == FLOAT) {
695 f = (float)d;
696 if (!finite(f)) {
697 /* floating-point constant out of range */
698 warning(248);
699 f = f > 0 ? FLT_MAX : -FLT_MAX;
700 }
701 }
702
703 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
704 if (typ == FLOAT) {
705 yylval.y_val->v_ldbl = f;
706 } else {
707 yylval.y_val->v_ldbl = d;
708 }
709
710 return (T_CON);
711 }
712
713 static int
714 operator(int t, op_t o)
715 {
716
717 yylval.y_op = o;
718 return (t);
719 }
720
721 /*
722 * Called if lex found a leading \'.
723 */
724 static int
725 ccon(void)
726 {
727 int n, val, c;
728 char cv;
729
730 n = 0;
731 val = 0;
732 while ((c = getescc('\'')) >= 0) {
733 val = (val << CHAR_BIT) + c;
734 n++;
735 }
736 if (c == -2) {
737 /* unterminated character constant */
738 error(253);
739 } else {
740 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
741 /* too many characters in character constant */
742 error(71);
743 } else if (n > 1) {
744 /* multi-character character constant */
745 warning(294);
746 } else if (n == 0) {
747 /* empty character constant */
748 error(73);
749 }
750 }
751 if (n == 1) {
752 cv = (char)val;
753 val = cv;
754 }
755
756 yylval.y_val = xcalloc(1, sizeof (val_t));
757 yylval.y_val->v_tspec = INT;
758 yylval.y_val->v_quad = val;
759
760 return (T_CON);
761 }
762
763 /*
764 * Called if lex found a leading L\'
765 */
766 static int
767 wccon(void)
768 {
769 static char buf[MB_LEN_MAX + 1];
770 int i, c;
771 wchar_t wc;
772
773 i = 0;
774 while ((c = getescc('\'')) >= 0) {
775 if (i < MB_CUR_MAX)
776 buf[i] = (char)c;
777 i++;
778 }
779
780 wc = 0;
781
782 if (c == -2) {
783 /* unterminated character constant */
784 error(253);
785 } else if (c == 0) {
786 /* empty character constant */
787 error(73);
788 } else {
789 if (i > MB_CUR_MAX) {
790 i = MB_CUR_MAX;
791 /* too many characters in character constant */
792 error(71);
793 } else {
794 buf[i] = '\0';
795 (void)mbtowc(NULL, NULL, 0);
796 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
797 /* invalid multibyte character */
798 error(291);
799 }
800 }
801
802 yylval.y_val = xcalloc(1, sizeof (val_t));
803 yylval.y_val->v_tspec = WCHAR;
804 yylval.y_val->v_quad = wc;
805
806 return (T_CON);
807 }
808
809 /*
810 * Read a character which is part of a character constant or of a string
811 * and handle escapes.
812 *
813 * The Argument is the character which delimits the character constant or
814 * string.
815 *
816 * Returns -1 if the end of the character constant or string is reached,
817 * -2 if the EOF is reached, and the character otherwise.
818 */
819 static int
820 getescc(int d)
821 {
822 static int pbc = -1;
823 int n, c, v;
824
825 if (pbc == -1) {
826 c = inpc();
827 } else {
828 c = pbc;
829 pbc = -1;
830 }
831 if (c == d)
832 return (-1);
833 switch (c) {
834 case '\n':
835 if (tflag) {
836 /* newline in string or char constant */
837 error(254);
838 return (-2);
839 }
840 return (c);
841 case EOF:
842 return (-2);
843 case '\\':
844 switch (c = inpc()) {
845 case '"':
846 if (tflag && d == '\'')
847 /* \" inside character constant undef. ... */
848 warning(262);
849 return ('"');
850 case '\'':
851 return ('\'');
852 case '?':
853 if (tflag)
854 /* \? undefined in traditional C */
855 warning(263);
856 return ('?');
857 case '\\':
858 return ('\\');
859 case 'a':
860 if (tflag)
861 /* \a undefined in traditional C */
862 warning(81);
863 return ('\a');
864 case 'b':
865 return ('\b');
866 case 'f':
867 return ('\f');
868 case 'n':
869 return ('\n');
870 case 'r':
871 return ('\r');
872 case 't':
873 return ('\t');
874 case 'v':
875 if (tflag)
876 /* \v undefined in traditional C */
877 warning(264);
878 return ('\v');
879 case '8': case '9':
880 /* bad octal digit %c */
881 warning(77, c);
882 /* FALLTHROUGH */
883 case '0': case '1': case '2': case '3':
884 case '4': case '5': case '6': case '7':
885 n = 3;
886 v = 0;
887 do {
888 v = (v << 3) + (c - '0');
889 c = inpc();
890 } while (--n && isdigit(c) && (tflag || c <= '7'));
891 if (tflag && n > 0 && isdigit(c))
892 /* bad octal digit %c */
893 warning(77, c);
894 pbc = c;
895 if (v > UCHAR_MAX) {
896 /* character escape does not fit in char. */
897 warning(76);
898 v &= CHAR_MASK;
899 }
900 return (v);
901 case 'x':
902 if (tflag)
903 /* \x undefined in traditional C */
904 warning(82);
905 v = 0;
906 n = 0;
907 while ((c = inpc()) >= 0 && isxdigit(c)) {
908 c = isdigit(c) ?
909 c - '0' : toupper(c) - 'A' + 10;
910 v = (v << 4) + c;
911 if (n >= 0) {
912 if ((v & ~CHAR_MASK) != 0) {
913 /* overflow in hex escape */
914 warning(75);
915 n = -1;
916 } else {
917 n++;
918 }
919 }
920 }
921 pbc = c;
922 if (n == 0) {
923 /* no hex digits follow \x */
924 error(74);
925 } if (n == -1) {
926 v &= CHAR_MASK;
927 }
928 return (v);
929 case '\n':
930 return (getescc(d));
931 case EOF:
932 return (-2);
933 default:
934 if (isprint(c)) {
935 /* dubious escape \%c */
936 warning(79, c);
937 } else {
938 /* dubious escape \%o */
939 warning(80, c);
940 }
941 }
942 }
943 return (c);
944 }
945
946 /*
947 * Called for preprocessor directives. Currently implemented are:
948 * # lineno
949 * # lineno "filename"
950 */
951 static void
952 directive(void)
953 {
954 const char *cp, *fn;
955 char c, *eptr;
956 size_t fnl;
957 long ln;
958 static int first = 1;
959
960 /* Go to first non-whitespace after # */
961 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
962 continue;
963
964 if (!isdigit((unsigned char)c)) {
965 error:
966 /* undefined or invalid # directive */
967 warning(255);
968 return;
969 }
970 ln = strtol(--cp, &eptr, 10);
971 if (cp == eptr)
972 goto error;
973 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
974 goto error;
975 while ((c = *cp++) == ' ' || c == '\t')
976 continue;
977 if (c != '\0') {
978 if (c != '"')
979 goto error;
980 fn = cp;
981 while ((c = *cp) != '"' && c != '\0')
982 cp++;
983 if (c != '"')
984 goto error;
985 if ((fnl = cp++ - fn) > PATH_MAX)
986 goto error;
987 while ((c = *cp++) == ' ' || c == '\t')
988 continue;
989 #if 0
990 if (c != '\0')
991 warning("extra character(s) after directive");
992 #endif
993
994 /* empty string means stdin */
995 if (fnl == 0) {
996 fn = "{standard input}";
997 fnl = 16; /* strlen (fn) */
998 }
999 curr_pos.p_file = fnnalloc(fn, fnl);
1000 /*
1001 * If this is the first directive, the name is the name
1002 * of the C source file as specified at the command line.
1003 * It is written to the output file.
1004 */
1005 if (first) {
1006 csrc_pos.p_file = curr_pos.p_file;
1007 outsrc(curr_pos.p_file);
1008 first = 0;
1009 }
1010 }
1011 curr_pos.p_line = (int)ln - 1;
1012 curr_pos.p_uniq = 0;
1013 if (curr_pos.p_file == csrc_pos.p_file) {
1014 csrc_pos.p_line = (int)ln - 1;
1015 csrc_pos.p_uniq = 0;
1016 }
1017 }
1018
1019 /*
1020 * Handle lint comments. Following comments are currently understood:
1021 * ARGSUSEDn
1022 * BITFIELDTYPE
1023 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1024 * FALLTHRU FALLTHROUGH
1025 * LINTLIBRARY
1026 * LINTED NOSTRICT
1027 * LONGLONG
1028 * NOTREACHED
1029 * PRINTFLIKEn
1030 * PROTOLIB
1031 * SCANFLIKEn
1032 * VARARGSn
1033 * If one of this comments is recognized, the arguments, if any, are
1034 * parsed and a function which handles this comment is called.
1035 */
1036 static void
1037 comment(void)
1038 {
1039 int c, lc;
1040 static struct {
1041 const char *keywd;
1042 int arg;
1043 void (*func)(int);
1044 } keywtab[] = {
1045 { "ARGSUSED", 1, argsused },
1046 { "BITFIELDTYPE", 0, bitfieldtype },
1047 { "CONSTCOND", 0, constcond },
1048 { "CONSTANTCOND", 0, constcond },
1049 { "CONSTANTCONDITION", 0, constcond },
1050 { "FALLTHRU", 0, fallthru },
1051 { "FALLTHROUGH", 0, fallthru },
1052 { "LINTLIBRARY", 0, lintlib },
1053 { "LINTED", 0, linted },
1054 { "LONGLONG", 0, longlong },
1055 { "NOSTRICT", 0, linted },
1056 { "NOTREACHED", 0, notreach },
1057 { "PRINTFLIKE", 1, printflike },
1058 { "PROTOLIB", 1, protolib },
1059 { "SCANFLIKE", 1, scanflike },
1060 { "VARARGS", 1, varargs },
1061 };
1062 char keywd[32];
1063 char arg[32];
1064 int l, i, a;
1065 int eoc;
1066
1067 eoc = 0;
1068
1069 /* Skip white spaces after the start of the comment */
1070 while ((c = inpc()) != EOF && isspace(c))
1071 continue;
1072
1073 /* Read the potential keyword to keywd */
1074 l = 0;
1075 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1076 keywd[l++] = (char)c;
1077 c = inpc();
1078 }
1079 keywd[l] = '\0';
1080
1081 /* look for the keyword */
1082 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1083 if (strcmp(keywtab[i].keywd, keywd) == 0)
1084 break;
1085 }
1086 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1087 goto skip_rest;
1088
1089 /* skip white spaces after the keyword */
1090 while (c != EOF && isspace(c))
1091 c = inpc();
1092
1093 /* read the argument, if the keyword accepts one and there is one */
1094 l = 0;
1095 if (keywtab[i].arg) {
1096 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1097 arg[l++] = (char)c;
1098 c = inpc();
1099 }
1100 }
1101 arg[l] = '\0';
1102 a = l != 0 ? atoi(arg) : -1;
1103
1104 /* skip white spaces after the argument */
1105 while (c != EOF && isspace(c))
1106 c = inpc();
1107
1108 if (c != '*' || (c = inpc()) != '/') {
1109 if (keywtab[i].func != linted)
1110 /* extra characters in lint comment */
1111 warning(257);
1112 } else {
1113 /*
1114 * remember that we have already found the end of the
1115 * comment
1116 */
1117 eoc = 1;
1118 }
1119
1120 if (keywtab[i].func != NULL)
1121 (*keywtab[i].func)(a);
1122
1123 skip_rest:
1124 while (!eoc) {
1125 lc = c;
1126 if ((c = inpc()) == EOF) {
1127 /* unterminated comment */
1128 error(256);
1129 break;
1130 }
1131 if (lc == '*' && c == '/')
1132 eoc = 1;
1133 }
1134 }
1135
1136 /*
1137 * Handle // style comments
1138 */
1139 static void
1140 slashslashcomment(void)
1141 {
1142 int c;
1143
1144 if (!Sflag && !gflag)
1145 /* // comments only supported in C99 */
1146 (void)gnuism(312, tflag ? "traditional" : "ANSI");
1147
1148 while ((c = inpc()) != EOF && c != '\n')
1149 continue;
1150 }
1151
1152 /*
1153 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1154 * clrwflgs() is called after function definitions and global and
1155 * local declarations and definitions. It is also called between
1156 * the controlling expression and the body of control statements
1157 * (if, switch, for, while).
1158 */
1159 void
1160 clrwflgs(void)
1161 {
1162
1163 nowarn = 0;
1164 quadflg = 0;
1165 ccflg = 0;
1166 }
1167
1168 /*
1169 * Strings are stored in a dynamically alloceted buffer and passed
1170 * in yylval.y_xstrg to the parser. The parser or the routines called
1171 * by the parser are responsible for freeing this buffer.
1172 */
1173 static int
1174 string(void)
1175 {
1176 u_char *s;
1177 int c;
1178 size_t len, max;
1179 strg_t *strg;
1180
1181 s = xmalloc(max = 64);
1182
1183 len = 0;
1184 while ((c = getescc('"')) >= 0) {
1185 /* +1 to reserve space for a trailing NUL character */
1186 if (len + 1 == max)
1187 s = xrealloc(s, max *= 2);
1188 s[len++] = (char)c;
1189 }
1190 s[len] = '\0';
1191 if (c == -2)
1192 /* unterminated string constant */
1193 error(258);
1194
1195 strg = xcalloc(1, sizeof (strg_t));
1196 strg->st_tspec = CHAR;
1197 strg->st_len = len;
1198 strg->st_cp = s;
1199
1200 yylval.y_strg = strg;
1201 return (T_STRING);
1202 }
1203
1204 static int
1205 wcstrg(void)
1206 {
1207 char *s;
1208 int c, i, n, wi;
1209 size_t len, max, wlen;
1210 wchar_t *ws;
1211 strg_t *strg;
1212
1213 s = xmalloc(max = 64);
1214 len = 0;
1215 while ((c = getescc('"')) >= 0) {
1216 /* +1 to save space for a trailing NUL character */
1217 if (len + 1 >= max)
1218 s = xrealloc(s, max *= 2);
1219 s[len++] = (char)c;
1220 }
1221 s[len] = '\0';
1222 if (c == -2)
1223 /* unterminated string constant */
1224 error(258);
1225
1226 /* get length of wide character string */
1227 (void)mblen(NULL, 0);
1228 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1229 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1230 /* invalid multibyte character */
1231 error(291);
1232 break;
1233 }
1234 if (n == 0)
1235 n = 1;
1236 }
1237
1238 ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1239
1240 /* convert from multibyte to wide char */
1241 (void)mbtowc(NULL, NULL, 0);
1242 for (i = 0, wi = 0; i < len; i += n, wi++) {
1243 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1244 break;
1245 if (n == 0)
1246 n = 1;
1247 }
1248 ws[wi] = 0;
1249 free(s);
1250
1251 strg = xcalloc(1, sizeof (strg_t));
1252 strg->st_tspec = WCHAR;
1253 strg->st_len = wlen;
1254 strg->st_wcp = ws;
1255
1256 yylval.y_strg = strg;
1257 return (T_STRING);
1258 }
1259
1260 /*
1261 * As noted above the scanner does not create new symbol table entries
1262 * for symbols it cannot find in the symbol table. This is to avoid
1263 * putting undeclared symbols into the symbol table if a syntax error
1264 * occurs.
1265 *
1266 * getsym() is called as soon as it is probably ok to put the symbol to
1267 * the symbol table. This does not mean that it is not possible that
1268 * symbols are put to the symbol table which are than not completely
1269 * declared due to syntax errors. To avoid too many problems in this
1270 * case symbols get type int in getsym().
1271 *
1272 * XXX calls to getsym() should be delayed until decl1*() is called
1273 */
1274 sym_t *
1275 getsym(sbuf_t *sb)
1276 {
1277 dinfo_t *di;
1278 char *s;
1279 sym_t *sym;
1280
1281 sym = sb->sb_sym;
1282
1283 /*
1284 * During member declaration it is possible that name() looked
1285 * for symbols of type FVFT, although it should have looked for
1286 * symbols of type FTAG. Same can happen for labels. Both cases
1287 * are compensated here.
1288 */
1289 if (symtyp == FMOS || symtyp == FLAB) {
1290 if (sym == NULL || sym->s_kind == FVFT)
1291 sym = search(sb);
1292 }
1293
1294 if (sym != NULL) {
1295 if (sym->s_kind != symtyp)
1296 LERROR("storesym()");
1297 symtyp = FVFT;
1298 freesb(sb);
1299 return (sym);
1300 }
1301
1302 /* create a new symbol table entry */
1303
1304 /* labels must always be allocated at level 1 (outhermost block) */
1305 if (symtyp == FLAB) {
1306 sym = getlblk(1, sizeof (sym_t));
1307 s = getlblk(1, sb->sb_len + 1);
1308 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1309 sym->s_name = s;
1310 sym->s_blklev = 1;
1311 di = dcs;
1312 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1313 di = di->d_nxt;
1314 if (di->d_ctx != AUTO)
1315 LERROR("storesym()");
1316 } else {
1317 sym = getblk(sizeof (sym_t));
1318 sym->s_name = sb->sb_name;
1319 sym->s_blklev = blklev;
1320 di = dcs;
1321 }
1322
1323 UNIQUE_CURR_POS(sym->s_dpos);
1324 if ((sym->s_kind = symtyp) != FLAB)
1325 sym->s_type = gettyp(INT);
1326
1327 symtyp = FVFT;
1328
1329 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1330 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1331 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1332
1333 *di->d_ldlsym = sym;
1334 di->d_ldlsym = &sym->s_dlnxt;
1335
1336 freesb(sb);
1337 return (sym);
1338 }
1339
1340 /*
1341 * Construct a temporary symbol. The symbol starts with a digit, so that
1342 * it is illegal.
1343 */
1344 sym_t *
1345 mktempsym(type_t *t)
1346 {
1347 static int n = 0;
1348 int h;
1349 char *s = getlblk(blklev, 64);
1350 sym_t *sym = getblk(sizeof (sym_t));
1351
1352 (void)snprintf(s, 64, "%.8d_tmp", n++);
1353 h = hash(s);
1354
1355 sym->s_name = s;
1356 sym->s_type = t;
1357 sym->s_blklev = blklev;
1358 sym->s_scl = AUTO;
1359 sym->s_kind = FVFT;
1360 sym->s_used = 1;
1361 sym->s_set = 1;
1362
1363 if ((sym->s_link = symtab[h]) != NULL)
1364 symtab[h]->s_rlink = &sym->s_link;
1365 (symtab[h] = sym)->s_rlink = &symtab[h];
1366
1367 *dcs->d_ldlsym = sym;
1368 dcs->d_ldlsym = &sym->s_dlnxt;
1369
1370 return sym;
1371 }
1372
1373 /*
1374 * Remove a symbol forever from the symbol table. s_blklev
1375 * is set to -1 to avoid that the symbol will later be put
1376 * back to the symbol table.
1377 */
1378 void
1379 rmsym(sym_t *sym)
1380 {
1381
1382 if ((*sym->s_rlink = sym->s_link) != NULL)
1383 sym->s_link->s_rlink = sym->s_rlink;
1384 sym->s_blklev = -1;
1385 sym->s_link = NULL;
1386 }
1387
1388 /*
1389 * Remove a list of symbols declared at one level from the symbol
1390 * table.
1391 */
1392 void
1393 rmsyms(sym_t *syms)
1394 {
1395 sym_t *sym;
1396
1397 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1398 if (sym->s_blklev != -1) {
1399 if ((*sym->s_rlink = sym->s_link) != NULL)
1400 sym->s_link->s_rlink = sym->s_rlink;
1401 sym->s_link = NULL;
1402 sym->s_rlink = NULL;
1403 }
1404 }
1405 }
1406
1407 /*
1408 * Put a symbol into the symbol table
1409 */
1410 void
1411 inssym(int bl, sym_t *sym)
1412 {
1413 int h;
1414
1415 h = hash(sym->s_name);
1416 if ((sym->s_link = symtab[h]) != NULL)
1417 symtab[h]->s_rlink = &sym->s_link;
1418 (symtab[h] = sym)->s_rlink = &symtab[h];
1419 sym->s_blklev = bl;
1420 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1421 LERROR("inssym()");
1422 }
1423
1424 /*
1425 * Called at level 0 after syntax errors
1426 * Removes all symbols which are not declared at level 0 from the
1427 * symbol table. Also frees all memory which is not associated with
1428 * level 0.
1429 */
1430 void
1431 cleanup(void)
1432 {
1433 sym_t *sym, *nsym;
1434 int i;
1435
1436 for (i = 0; i < HSHSIZ1; i++) {
1437 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1438 nsym = sym->s_link;
1439 if (sym->s_blklev >= 1) {
1440 if ((*sym->s_rlink = nsym) != NULL)
1441 nsym->s_rlink = sym->s_rlink;
1442 }
1443 }
1444 }
1445
1446 for (i = mblklev; i > 0; i--)
1447 freelblk(i);
1448 }
1449
1450 /*
1451 * Create a new symbol with the name of an existing symbol.
1452 */
1453 sym_t *
1454 pushdown(sym_t *sym)
1455 {
1456 int h;
1457 sym_t *nsym;
1458
1459 h = hash(sym->s_name);
1460 nsym = getblk(sizeof (sym_t));
1461 if (sym->s_blklev > blklev)
1462 LERROR("pushdown()");
1463 nsym->s_name = sym->s_name;
1464 UNIQUE_CURR_POS(nsym->s_dpos);
1465 nsym->s_kind = sym->s_kind;
1466 nsym->s_blklev = blklev;
1467
1468 if ((nsym->s_link = symtab[h]) != NULL)
1469 symtab[h]->s_rlink = &nsym->s_link;
1470 (symtab[h] = nsym)->s_rlink = &symtab[h];
1471
1472 *dcs->d_ldlsym = nsym;
1473 dcs->d_ldlsym = &nsym->s_dlnxt;
1474
1475 return (nsym);
1476 }
1477
1478 /*
1479 * Free any dynamically allocated memory referenced by
1480 * the value stack or yylval.
1481 * The type of information in yylval is described by tok.
1482 */
1483 void
1484 freeyyv(void *sp, int tok)
1485 {
1486 if (tok == T_NAME || tok == T_TYPENAME) {
1487 sbuf_t *sb = *(sbuf_t **)sp;
1488 freesb(sb);
1489 } else if (tok == T_CON) {
1490 val_t *val = *(val_t **)sp;
1491 free(val);
1492 } else if (tok == T_STRING) {
1493 strg_t *strg = *(strg_t **)sp;
1494 if (strg->st_tspec == CHAR) {
1495 free(strg->st_cp);
1496 } else if (strg->st_tspec == WCHAR) {
1497 free(strg->st_wcp);
1498 } else {
1499 LERROR("fryylv()");
1500 }
1501 free(strg);
1502 }
1503 }
1504