scan.l revision 1.22 1 %{
2 /* $NetBSD: scan.l,v 1.22 2002/01/03 04:25:15 thorpej 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.22 2002/01/03 04:25:15 thorpej 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 than 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 * BITFIELDTYPE
1014 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1015 * FALLTHRU FALLTHROUGH
1016 * LINTLIBRARY
1017 * LINTED NOSTRICT
1018 * LONGLONG
1019 * NOTREACHED
1020 * PRINTFLIKEn
1021 * PROTOLIB
1022 * SCANFLIKEn
1023 * VARARGSn
1024 * If one of this comments is recognized, the arguments, if any, are
1025 * parsed and a function which handles this comment is called.
1026 */
1027 static void
1028 comment(void)
1029 {
1030 int c, lc;
1031 static struct {
1032 const char *keywd;
1033 int arg;
1034 void (*func)(int);
1035 } keywtab[] = {
1036 { "ARGSUSED", 1, argsused },
1037 { "BITFIELDTYPE", 0, bitfieldtype },
1038 { "CONSTCOND", 0, constcond },
1039 { "CONSTANTCOND", 0, constcond },
1040 { "CONSTANTCONDITION", 0, constcond },
1041 { "FALLTHRU", 0, fallthru },
1042 { "FALLTHROUGH", 0, fallthru },
1043 { "LINTLIBRARY", 0, lintlib },
1044 { "LINTED", 0, linted },
1045 { "LONGLONG", 0, longlong },
1046 { "NOSTRICT", 0, linted },
1047 { "NOTREACHED", 0, notreach },
1048 { "PRINTFLIKE", 1, printflike },
1049 { "PROTOLIB", 1, protolib },
1050 { "SCANFLIKE", 1, scanflike },
1051 { "VARARGS", 1, varargs },
1052 };
1053 char keywd[32];
1054 char arg[32];
1055 int l, i, a;
1056 int eoc;
1057
1058 eoc = 0;
1059
1060 /* Skip white spaces after the start of the comment */
1061 while ((c = inpc()) != EOF && isspace(c))
1062 continue;
1063
1064 /* Read the potential keyword to keywd */
1065 l = 0;
1066 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1067 keywd[l++] = (char)c;
1068 c = inpc();
1069 }
1070 keywd[l] = '\0';
1071
1072 /* look for the keyword */
1073 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1074 if (strcmp(keywtab[i].keywd, keywd) == 0)
1075 break;
1076 }
1077 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1078 goto skip_rest;
1079
1080 /* skip white spaces after the keyword */
1081 while (c != EOF && isspace(c))
1082 c = inpc();
1083
1084 /* read the argument, if the keyword accepts one and there is one */
1085 l = 0;
1086 if (keywtab[i].arg) {
1087 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1088 arg[l++] = (char)c;
1089 c = inpc();
1090 }
1091 }
1092 arg[l] = '\0';
1093 a = l != 0 ? atoi(arg) : -1;
1094
1095 /* skip white spaces after the argument */
1096 while (c != EOF && isspace(c))
1097 c = inpc();
1098
1099 if (c != '*' || (c = inpc()) != '/') {
1100 if (keywtab[i].func != linted)
1101 /* extra characters in lint comment */
1102 warning(257);
1103 } else {
1104 /*
1105 * remember that we have already found the end of the
1106 * comment
1107 */
1108 eoc = 1;
1109 }
1110
1111 if (keywtab[i].func != NULL)
1112 (*keywtab[i].func)(a);
1113
1114 skip_rest:
1115 while (!eoc) {
1116 lc = c;
1117 if ((c = inpc()) == EOF) {
1118 /* unterminated comment */
1119 error(256);
1120 break;
1121 }
1122 if (lc == '*' && c == '/')
1123 eoc = 1;
1124 }
1125 }
1126
1127 /*
1128 * Handle // style comments
1129 */
1130 static void
1131 slashslashcomment(void)
1132 {
1133 int c;
1134
1135 if (sflag < 2 && !gflag)
1136 /* // comments only supported in C99 */
1137 (void)gnuism(312, tflag ? "traditional" : "ANSI");
1138
1139 while ((c = inpc()) != EOF && c != '\n')
1140 continue;
1141 }
1142
1143 /*
1144 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1145 * clrwflgs() is called after function definitions and global and
1146 * local declarations and definitions. It is also called between
1147 * the controlling expression and the body of control statements
1148 * (if, switch, for, while).
1149 */
1150 void
1151 clrwflgs(void)
1152 {
1153
1154 nowarn = 0;
1155 quadflg = 0;
1156 ccflg = 0;
1157 }
1158
1159 /*
1160 * Strings are stored in a dynamically alloceted buffer and passed
1161 * in yylval.y_xstrg to the parser. The parser or the routines called
1162 * by the parser are responsible for freeing this buffer.
1163 */
1164 static int
1165 string(void)
1166 {
1167 u_char *s;
1168 int c;
1169 size_t len, max;
1170 strg_t *strg;
1171
1172 s = xmalloc(max = 64);
1173
1174 len = 0;
1175 while ((c = getescc('"')) >= 0) {
1176 /* +1 to reserve space for a trailing NUL character */
1177 if (len + 1 == max)
1178 s = xrealloc(s, max *= 2);
1179 s[len++] = (char)c;
1180 }
1181 s[len] = '\0';
1182 if (c == -2)
1183 /* unterminated string constant */
1184 error(258);
1185
1186 strg = xcalloc(1, sizeof (strg_t));
1187 strg->st_tspec = CHAR;
1188 strg->st_len = len;
1189 strg->st_cp = s;
1190
1191 yylval.y_strg = strg;
1192 return (T_STRING);
1193 }
1194
1195 static int
1196 wcstrg(void)
1197 {
1198 char *s;
1199 int c, i, n, wi;
1200 size_t len, max, wlen;
1201 wchar_t *ws;
1202 strg_t *strg;
1203
1204 s = xmalloc(max = 64);
1205 len = 0;
1206 while ((c = getescc('"')) >= 0) {
1207 /* +1 to save space for a trailing NUL character */
1208 if (len + 1 >= max)
1209 s = xrealloc(s, max *= 2);
1210 s[len++] = (char)c;
1211 }
1212 s[len] = '\0';
1213 if (c == -2)
1214 /* unterminated string constant */
1215 error(258);
1216
1217 /* get length of wide character string */
1218 (void)mblen(NULL, 0);
1219 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1220 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1221 /* invalid multibyte character */
1222 error(291);
1223 break;
1224 }
1225 if (n == 0)
1226 n = 1;
1227 }
1228
1229 ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1230
1231 /* convert from multibyte to wide char */
1232 (void)mbtowc(NULL, NULL, 0);
1233 for (i = 0, wi = 0; i < len; i += n, wi++) {
1234 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1235 break;
1236 if (n == 0)
1237 n = 1;
1238 }
1239 ws[wi] = 0;
1240 free(s);
1241
1242 strg = xcalloc(1, sizeof (strg_t));
1243 strg->st_tspec = WCHAR;
1244 strg->st_len = wlen;
1245 strg->st_wcp = ws;
1246
1247 yylval.y_strg = strg;
1248 return (T_STRING);
1249 }
1250
1251 /*
1252 * As noted above the scanner does not create new symbol table entries
1253 * for symbols it cannot find in the symbol table. This is to avoid
1254 * putting undeclared symbols into the symbol table if a syntax error
1255 * occurs.
1256 *
1257 * getsym() is called as soon as it is probably ok to put the symbol to
1258 * the symbol table. This does not mean that it is not possible that
1259 * symbols are put to the symbol table which are than not completely
1260 * declared due to syntax errors. To avoid too many problems in this
1261 * case symbols get type int in getsym().
1262 *
1263 * XXX calls to getsym() should be delayed until decl1*() is called
1264 */
1265 sym_t *
1266 getsym(sbuf_t *sb)
1267 {
1268 dinfo_t *di;
1269 char *s;
1270 sym_t *sym;
1271
1272 sym = sb->sb_sym;
1273
1274 /*
1275 * During member declaration it is possible that name() looked
1276 * for symbols of type FVFT, although it should have looked for
1277 * symbols of type FTAG. Same can happen for labels. Both cases
1278 * are compensated here.
1279 */
1280 if (symtyp == FMOS || symtyp == FLAB) {
1281 if (sym == NULL || sym->s_kind == FVFT)
1282 sym = search(sb);
1283 }
1284
1285 if (sym != NULL) {
1286 if (sym->s_kind != symtyp)
1287 lerror("storesym() 1");
1288 symtyp = FVFT;
1289 freesb(sb);
1290 return (sym);
1291 }
1292
1293 /* create a new symbol table entry */
1294
1295 /* labels must always be allocated at level 1 (outhermost block) */
1296 if (symtyp == FLAB) {
1297 sym = getlblk(1, sizeof (sym_t));
1298 s = getlblk(1, sb->sb_len + 1);
1299 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1300 sym->s_name = s;
1301 sym->s_blklev = 1;
1302 di = dcs;
1303 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1304 di = di->d_nxt;
1305 if (di->d_ctx != AUTO)
1306 lerror("storesym() 2");
1307 } else {
1308 sym = getblk(sizeof (sym_t));
1309 sym->s_name = sb->sb_name;
1310 sym->s_blklev = blklev;
1311 di = dcs;
1312 }
1313
1314 UNIQUE_CURR_POS(sym->s_dpos);
1315 if ((sym->s_kind = symtyp) != FLAB)
1316 sym->s_type = gettyp(INT);
1317
1318 symtyp = FVFT;
1319
1320 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1321 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1322 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1323
1324 *di->d_ldlsym = sym;
1325 di->d_ldlsym = &sym->s_dlnxt;
1326
1327 freesb(sb);
1328 return (sym);
1329 }
1330
1331 /*
1332 * Remove a symbol forever from the symbol table. s_blklev
1333 * is set to -1 to avoid that the symbol will later be put
1334 * back to the symbol table.
1335 */
1336 void
1337 rmsym(sym_t *sym)
1338 {
1339
1340 if ((*sym->s_rlink = sym->s_link) != NULL)
1341 sym->s_link->s_rlink = sym->s_rlink;
1342 sym->s_blklev = -1;
1343 sym->s_link = NULL;
1344 }
1345
1346 /*
1347 * Remove a list of symbols declared at one level from the symbol
1348 * table.
1349 */
1350 void
1351 rmsyms(sym_t *syms)
1352 {
1353 sym_t *sym;
1354
1355 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1356 if (sym->s_blklev != -1) {
1357 if ((*sym->s_rlink = sym->s_link) != NULL)
1358 sym->s_link->s_rlink = sym->s_rlink;
1359 sym->s_link = NULL;
1360 sym->s_rlink = NULL;
1361 }
1362 }
1363 }
1364
1365 /*
1366 * Put a symbol into the symbol table
1367 */
1368 void
1369 inssym(int bl, sym_t *sym)
1370 {
1371 int h;
1372
1373 h = hash(sym->s_name);
1374 if ((sym->s_link = symtab[h]) != NULL)
1375 symtab[h]->s_rlink = &sym->s_link;
1376 (symtab[h] = sym)->s_rlink = &symtab[h];
1377 sym->s_blklev = bl;
1378 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1379 lerror("inssym()");
1380 }
1381
1382 /*
1383 * Called at level 0 after syntax errors
1384 * Removes all symbols which are not declared at level 0 from the
1385 * symbol table. Also frees all memory which is not associated with
1386 * level 0.
1387 */
1388 void
1389 cleanup(void)
1390 {
1391 sym_t *sym, *nsym;
1392 int i;
1393
1394 for (i = 0; i < HSHSIZ1; i++) {
1395 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1396 nsym = sym->s_link;
1397 if (sym->s_blklev >= 1) {
1398 if ((*sym->s_rlink = nsym) != NULL)
1399 nsym->s_rlink = sym->s_rlink;
1400 }
1401 }
1402 }
1403
1404 for (i = mblklev; i > 0; i--)
1405 freelblk(i);
1406 }
1407
1408 /*
1409 * Create a new symbol with the name of an existing symbol.
1410 */
1411 sym_t *
1412 pushdown(sym_t *sym)
1413 {
1414 int h;
1415 sym_t *nsym;
1416
1417 h = hash(sym->s_name);
1418 nsym = getblk(sizeof (sym_t));
1419 if (sym->s_blklev > blklev)
1420 lerror("pushdown()");
1421 nsym->s_name = sym->s_name;
1422 UNIQUE_CURR_POS(nsym->s_dpos);
1423 nsym->s_kind = sym->s_kind;
1424 nsym->s_blklev = blklev;
1425
1426 if ((nsym->s_link = symtab[h]) != NULL)
1427 symtab[h]->s_rlink = &nsym->s_link;
1428 (symtab[h] = nsym)->s_rlink = &symtab[h];
1429
1430 *dcs->d_ldlsym = nsym;
1431 dcs->d_ldlsym = &nsym->s_dlnxt;
1432
1433 return (nsym);
1434 }
1435
1436 /*
1437 * Free any dynamically allocated memory referenced by
1438 * the value stack or yylval.
1439 * The type of information in yylval is described by tok.
1440 */
1441 void
1442 freeyyv(void *sp, int tok)
1443 {
1444 if (tok == T_NAME || tok == T_TYPENAME) {
1445 sbuf_t *sb = *(sbuf_t **)sp;
1446 freesb(sb);
1447 } else if (tok == T_CON) {
1448 val_t *val = *(val_t **)sp;
1449 free(val);
1450 } else if (tok == T_STRING) {
1451 strg_t *strg = *(strg_t **)sp;
1452 if (strg->st_tspec == CHAR) {
1453 free(strg->st_cp);
1454 } else if (strg->st_tspec == WCHAR) {
1455 free(strg->st_wcp);
1456 } else {
1457 lerror("fryylv() 1");
1458 }
1459 free(strg);
1460 }
1461 }
1462