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