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