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