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