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