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