scan.l revision 1.14.2.1 1 %{
2 /* $NetBSD: scan.l,v 1.14.2.1 2000/06/23 16:40:19 minoura 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.14.2.1 2000/06/23 16:40:19 minoura 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 __P((void));
66 static void badchar __P((int));
67 static sbuf_t *allocsb __P((void));
68 static void freesb __P((sbuf_t *));
69 static int inpc __P((void));
70 static int hash __P((const char *));
71 static sym_t *search __P((sbuf_t *));
72 static int name __P((void));
73 static int keyw __P((sym_t *));
74 static int icon __P((int));
75 static int fcon __P((void));
76 static int operator __P((int, op_t));
77 static int ccon __P((void));
78 static int wccon __P((void));
79 static int getescc __P((int));
80 static void directive __P((void));
81 static void comment __P((void));
82 static int string __P((void));
83 static int wcstrg __P((void));
84
85 %}
86
87 L [_A-Za-z]
88 D [0-9]
89 NZD [1-9]
90 OD [0-7]
91 HD [0-9A-Fa-f]
92 EX ([eE][+-]?[0-9]+)
93
94 %%
95
96 {L}({L}|{D})* return (name());
97 0{OD}*[lLuU]* return (icon(8));
98 {NZD}{D}*[lLuU]* return (icon(10));
99 0[xX]{HD}+[lLuU]* return (icon(16));
100 {D}+\.{D}*{EX}?[fFlL]? |
101 {D}+{EX}[fFlL]? |
102 \.{D}+{EX}?[fFlL]? return (fcon());
103 "=" return (operator(T_ASSIGN, ASSIGN));
104 "*=" return (operator(T_OPASS, MULASS));
105 "/=" return (operator(T_OPASS, DIVASS));
106 "%=" return (operator(T_OPASS, MODASS));
107 "+=" return (operator(T_OPASS, ADDASS));
108 "-=" return (operator(T_OPASS, SUBASS));
109 "<<=" return (operator(T_OPASS, SHLASS));
110 ">>=" return (operator(T_OPASS, SHRASS));
111 "&=" return (operator(T_OPASS, ANDASS));
112 "^=" return (operator(T_OPASS, XORASS));
113 "|=" return (operator(T_OPASS, ORASS));
114 "||" return (operator(T_LOGOR, LOGOR));
115 "&&" return (operator(T_LOGAND, LOGAND));
116 "|" return (operator(T_OR, OR));
117 "&" return (operator(T_AND, AND));
118 "^" return (operator(T_XOR, XOR));
119 "==" return (operator(T_EQOP, EQ));
120 "!=" return (operator(T_EQOP, NE));
121 "<" return (operator(T_RELOP, LT));
122 ">" return (operator(T_RELOP, GT));
123 "<=" return (operator(T_RELOP, LE));
124 ">=" return (operator(T_RELOP, GE));
125 "<<" return (operator(T_SHFTOP, SHL));
126 ">>" return (operator(T_SHFTOP, SHR));
127 "++" return (operator(T_INCDEC, INC));
128 "--" return (operator(T_INCDEC, DEC));
129 "->" return (operator(T_STROP, ARROW));
130 "." return (operator(T_STROP, POINT));
131 "+" return (operator(T_ADDOP, PLUS));
132 "-" return (operator(T_ADDOP, MINUS));
133 "*" return (operator(T_MULT, MULT));
134 "/" return (operator(T_DIVOP, DIV));
135 "%" return (operator(T_DIVOP, MOD));
136 "!" return (operator(T_UNOP, NOT));
137 "~" return (operator(T_UNOP, COMPL));
138 "\"" return (string());
139 "L\"" return (wcstrg());
140 ";" return (T_SEMI);
141 "{" return (T_LBRACE);
142 "}" return (T_RBRACE);
143 "," return (T_COMMA);
144 ":" return (T_COLON);
145 "?" return (T_QUEST);
146 "[" return (T_LBRACK);
147 "]" return (T_RBRACK);
148 "(" return (T_LPARN);
149 ")" return (T_RPARN);
150 "..." return (T_ELLIPSE);
151 "'" return (ccon());
152 "L'" return (wccon());
153 ^#.*$ directive();
154 \n incline();
155 \t|" "|\f|\v ;
156 "/*" comment();
157 . badchar(yytext[0]);
158
159 %%
160
161 static void
162 incline()
163 {
164 curr_pos.p_line++;
165 curr_pos.p_uniq = 0;
166 if (curr_pos.p_file == csrc_pos.p_file) {
167 csrc_pos.p_line++;
168 csrc_pos.p_uniq = 0;
169 }
170 }
171
172 static void
173 badchar(c)
174 int c;
175 {
176 /* unknown character \%o */
177 error(250, c);
178 }
179
180 /*
181 * Keywords.
182 * During initialisation they are written to the symbol table.
183 */
184 static struct kwtab {
185 const char *kw_name; /* keyword */
186 int kw_token; /* token returned by yylex() */
187 scl_t kw_scl; /* storage class if kw_token T_SCLASS */
188 tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */
189 tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */
190 u_int kw_stdc : 1; /* STDC keyword */
191 u_int kw_gcc : 1; /* GCC keyword */
192 } kwtab[] = {
193 { "asm", T_ASM, 0, 0, 0, 0, 1 },
194 { "__asm", T_ASM, 0, 0, 0, 0, 0 },
195 { "__asm__", T_ASM, 0, 0, 0, 0, 0 },
196 { "auto", T_SCLASS, AUTO, 0, 0, 0, 0 },
197 { "break", T_BREAK, 0, 0, 0, 0, 0 },
198 { "case", T_CASE, 0, 0, 0, 0, 0 },
199 { "char", T_TYPE, 0, CHAR, 0, 0, 0 },
200 { "const", T_QUAL, 0, 0, CONST, 1, 0 },
201 { "__const__", T_QUAL, 0, 0, CONST, 0, 0 },
202 { "__const", T_QUAL, 0, 0, CONST, 0, 0 },
203 { "continue", T_CONTINUE, 0, 0, 0, 0, 0 },
204 { "default", T_DEFAULT, 0, 0, 0, 0, 0 },
205 { "do", T_DO, 0, 0, 0, 0, 0 },
206 { "double", T_TYPE, 0, DOUBLE, 0, 0, 0 },
207 { "else", T_ELSE, 0, 0, 0, 0, 0 },
208 { "enum", T_ENUM, 0, 0, 0, 0, 0 },
209 { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0 },
210 { "float", T_TYPE, 0, FLOAT, 0, 0, 0 },
211 { "for", T_FOR, 0, 0, 0, 0, 0 },
212 { "goto", T_GOTO, 0, 0, 0, 0, 0 },
213 { "if", T_IF, 0, 0, 0, 0, 0 },
214 { "inline", T_SCLASS, INLINE, 0, 0, 0, 1 },
215 { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0 },
216 { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0 },
217 { "int", T_TYPE, 0, INT, 0, 0, 0 },
218 { "__symbolrename", T_SYMBOLRENAME, 0, 0, 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 = 0;
461 u_quad_t uq = 0;
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 case STRUCT:
563 case VOID:
564 case LDOUBLE:
565 case FUNC:
566 case ARRAY:
567 case PTR:
568 case ENUM:
569 case UNION:
570 case SIGNED:
571 case NOTSPEC:
572 case DOUBLE:
573 case FLOAT:
574 case UQUAD:
575 case ULONG:
576 case USHORT:
577 case SHORT:
578 case UCHAR:
579 case SCHAR:
580 case CHAR:
581 case UNSIGN:
582 break;
583 }
584
585 if (typ != QUAD && typ != UQUAD) {
586 if (isutyp(typ)) {
587 uq = ul;
588 } else {
589 uq = (quad_t)(long)ul;
590 }
591 }
592
593 uq = (u_quad_t)xsign((quad_t)uq, typ, -1);
594
595 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
596 yylval.y_val->v_ansiu = ansiu;
597 yylval.y_val->v_quad = (quad_t)uq;
598
599 return (T_CON);
600 }
601
602 /*
603 * Returns 1 if t is a signed type and the value is negative.
604 *
605 * len is the number of significant bits. If len is -1, len is set
606 * to the width of type t.
607 */
608 int
609 sign(q, t, len)
610 quad_t q;
611 tspec_t t;
612 int len;
613 {
614 if (t == PTR || isutyp(t))
615 return (0);
616 return (msb(q, t, len));
617 }
618
619 int
620 msb(q, t, len)
621 quad_t q;
622 tspec_t t;
623 int len;
624 {
625 if (len <= 0)
626 len = size(t);
627 return ((q & qbmasks[len - 1]) != 0);
628 }
629
630 /*
631 * Extends the sign of q.
632 */
633 quad_t
634 xsign(q, t, len)
635 quad_t q;
636 tspec_t t;
637 int len;
638 {
639 if (len <= 0)
640 len = size(t);
641
642 if (t == PTR || isutyp(t) || !sign(q, t, len)) {
643 q &= qlmasks[len];
644 } else {
645 q |= qumasks[len];
646 }
647 return (q);
648 }
649
650 /*
651 * Convert a string representing a floating point value into its interal
652 * representation. Type and value are returned in yylval. fcon()
653 * (and yylex()) returns T_CON.
654 * XXX Currently it is not possible to convert constants of type
655 * long double which are greater then DBL_MAX.
656 */
657 static int
658 fcon()
659 {
660 const char *cp;
661 int len;
662 tspec_t typ;
663 char c, *eptr;
664 double d;
665 float f = 0;
666
667 cp = yytext;
668 len = yyleng;
669
670 if ((c = cp[len - 1]) == 'f' || c == 'F') {
671 typ = FLOAT;
672 len--;
673 } else if (c == 'l' || c == 'L') {
674 typ = LDOUBLE;
675 len--;
676 } else {
677 typ = DOUBLE;
678 }
679
680 if (tflag && typ != DOUBLE) {
681 /* suffixes F and L are illegal in traditional C */
682 warning(98);
683 }
684
685 errno = 0;
686 d = strtod(cp, &eptr);
687 if (eptr != cp + len)
688 lerror("fcon() 1");
689 if (errno != 0)
690 /* floating-point constant out of range */
691 warning(248);
692
693 if (typ == FLOAT) {
694 f = (float)d;
695 if (isinf(f)) {
696 /* floating-point constant out of range */
697 warning(248);
698 f = f > 0 ? FLT_MAX : -FLT_MAX;
699 }
700 }
701
702 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
703 if (typ == FLOAT) {
704 yylval.y_val->v_ldbl = f;
705 } else {
706 yylval.y_val->v_ldbl = d;
707 }
708
709 return (T_CON);
710 }
711
712 static int
713 operator(t, o)
714 int t;
715 op_t o;
716 {
717 yylval.y_op = o;
718 return (t);
719 }
720
721 /*
722 * Called if lex found a leading \'.
723 */
724 static int
725 ccon()
726 {
727 int n, val, c;
728 char cv;
729
730 n = 0;
731 val = 0;
732 while ((c = getescc('\'')) >= 0) {
733 val = (val << CHAR_BIT) + c;
734 n++;
735 }
736 if (c == -2) {
737 /* unterminated character constant */
738 error(253);
739 } else {
740 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
741 /* too many characters in character constant */
742 error(71);
743 } else if (n > 1) {
744 /* multi-character character constant */
745 warning(294);
746 } else if (n == 0) {
747 /* empty character constant */
748 error(73);
749 }
750 }
751 if (n == 1) {
752 cv = (char)val;
753 val = cv;
754 }
755
756 yylval.y_val = xcalloc(1, sizeof (val_t));
757 yylval.y_val->v_tspec = INT;
758 yylval.y_val->v_quad = val;
759
760 return (T_CON);
761 }
762
763 /*
764 * Called if lex found a leading L\'
765 */
766 static int
767 wccon()
768 {
769 static char buf[MB_LEN_MAX + 1];
770 int i, c;
771 wchar_t wc;
772
773 i = 0;
774 while ((c = getescc('\'')) >= 0) {
775 if (i < MB_CUR_MAX)
776 buf[i] = (char)c;
777 i++;
778 }
779
780 wc = 0;
781
782 if (c == -2) {
783 /* unterminated character constant */
784 error(253);
785 } else if (c == 0) {
786 /* empty character constant */
787 error(73);
788 } else {
789 if (i > MB_CUR_MAX) {
790 i = MB_CUR_MAX;
791 /* too many characters in character constant */
792 error(71);
793 } else {
794 buf[i] = '\0';
795 (void)mbtowc(NULL, NULL, 0);
796 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
797 /* invalid multibyte character */
798 error(291);
799 }
800 }
801
802 yylval.y_val = xcalloc(1, sizeof (val_t));
803 yylval.y_val->v_tspec = WCHAR;
804 yylval.y_val->v_quad = wc;
805
806 return (T_CON);
807 }
808
809 /*
810 * Read a character which is part of a character constant or of a string
811 * and handle escapes.
812 *
813 * The Argument is the character which delimits the character constant or
814 * string.
815 *
816 * Returns -1 if the end of the character constant or string is reached,
817 * -2 if the EOF is reached, and the charachter otherwise.
818 */
819 static int
820 getescc(d)
821 int d;
822 {
823 static int pbc = -1;
824 int n, c, v;
825
826 if (pbc == -1) {
827 c = inpc();
828 } else {
829 c = pbc;
830 pbc = -1;
831 }
832 if (c == d)
833 return (-1);
834 switch (c) {
835 case '\n':
836 /* newline in string or char constant */
837 error(254);
838 return (-2);
839 case EOF:
840 return (-2);
841 case '\\':
842 switch (c = inpc()) {
843 case '"':
844 if (tflag && d == '\'')
845 /* \" inside character constant undef. ... */
846 warning(262);
847 return ('"');
848 case '\'':
849 return ('\'');
850 case '?':
851 if (tflag)
852 /* \? undefined in traditional C */
853 warning(263);
854 return ('?');
855 case '\\':
856 return ('\\');
857 case 'a':
858 if (tflag)
859 /* \a undefined in traditional C */
860 warning(81);
861 #ifdef __STDC__
862 return ('\a');
863 #else
864 return ('\007');
865 #endif
866 case 'b':
867 return ('\b');
868 case 'f':
869 return ('\f');
870 case 'n':
871 return ('\n');
872 case 'r':
873 return ('\r');
874 case 't':
875 return ('\t');
876 case 'v':
877 if (tflag)
878 /* \v undefined in traditional C */
879 warning(264);
880 #ifdef __STDC__
881 return ('\v');
882 #else
883 return ('\013');
884 #endif
885 case '8': case '9':
886 /* bad octal digit %c */
887 warning(77, c);
888 /* FALLTHROUGH */
889 case '0': case '1': case '2': case '3':
890 case '4': case '5': case '6': case '7':
891 n = 3;
892 v = 0;
893 do {
894 v = (v << 3) + (c - '0');
895 c = inpc();
896 } while (--n && isdigit(c) && (tflag || c <= '7'));
897 if (tflag && n > 0 && isdigit(c))
898 /* bad octal digit %c */
899 warning(77, c);
900 pbc = c;
901 if (v > UCHAR_MAX) {
902 /* character escape does not fit in char. */
903 warning(76);
904 v &= CHAR_MASK;
905 }
906 return (v);
907 case 'x':
908 if (tflag)
909 /* \x undefined in traditional C */
910 warning(82);
911 v = 0;
912 n = 0;
913 while ((c = inpc()) >= 0 && isxdigit(c)) {
914 c = isdigit(c) ?
915 c - '0' : toupper(c) - 'A' + 10;
916 v = (v << 4) + c;
917 if (n >= 0) {
918 if ((v & ~CHAR_MASK) != 0) {
919 /* overflow in hex escape */
920 warning(75);
921 n = -1;
922 } else {
923 n++;
924 }
925 }
926 }
927 pbc = c;
928 if (n == 0) {
929 /* no hex digits follow \x */
930 error(74);
931 } if (n == -1) {
932 v &= CHAR_MASK;
933 }
934 return (v);
935 case '\n':
936 return (getescc(d));
937 case EOF:
938 return (-2);
939 default:
940 if (isprint(c)) {
941 /* dubious escape \%c */
942 warning(79, c);
943 } else {
944 /* dubious escape \%o */
945 warning(80, c);
946 }
947 }
948 }
949 return (c);
950 }
951
952 /*
953 * Called for preprocessor directives. Currently implemented are:
954 * # lineno
955 * # lineno "filename"
956 */
957 static void
958 directive()
959 {
960 const char *cp, *fn;
961 char c, *eptr;
962 size_t fnl;
963 long ln;
964 static int first = 1;
965
966 /* Go to first non-whitespace after # */
967 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) ;
968
969 if (!isdigit((unsigned char)c)) {
970 error:
971 /* undefined or invalid # directive */
972 warning(255);
973 return;
974 }
975 ln = strtol(--cp, &eptr, 10);
976 if (cp == eptr)
977 goto error;
978 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
979 goto error;
980 while ((c = *cp++) == ' ' || c == '\t') ;
981 if (c != '\0') {
982 if (c != '"')
983 goto error;
984 fn = cp;
985 while ((c = *cp) != '"' && c != '\0')
986 cp++;
987 if (c != '"')
988 goto error;
989 if ((fnl = cp++ - fn) > PATH_MAX)
990 goto error;
991 while ((c = *cp++) == ' ' || c == '\t') ;
992 #if 0
993 if (c != '\0')
994 warning("extra character(s) after directive");
995 #endif
996
997 /* empty string means stdin */
998 if (fnl == 0) {
999 fn = "{standard input}";
1000 fnl = 16; /* strlen (fn) */
1001 }
1002 curr_pos.p_file = fnnalloc(fn, fnl);
1003 /*
1004 * If this is the first directive, the name is the name
1005 * of the C source file as specified at the command line.
1006 * It is written to the output file.
1007 */
1008 if (first) {
1009 csrc_pos.p_file = curr_pos.p_file;
1010 outsrc(curr_pos.p_file);
1011 first = 0;
1012 }
1013 }
1014 curr_pos.p_line = (int)ln - 1;
1015 curr_pos.p_uniq = 0;
1016 if (curr_pos.p_file == csrc_pos.p_file) {
1017 csrc_pos.p_line = (int)ln - 1;
1018 csrc_pos.p_uniq = 0;
1019 }
1020 }
1021
1022 /*
1023 * Handle lint comments. Following comments are currently understood:
1024 * ARGSUSEDn
1025 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1026 * FALLTHRU FALLTHROUGH
1027 * LINTLIBRARY
1028 * LINTED NOSTRICT
1029 * LONGLONG
1030 * NOTREACHED
1031 * PRINTFLIKEn
1032 * PROTOLIB
1033 * SCANFLIKEn
1034 * VARARGSn
1035 * If one of this comments is recognized, the arguments, if any, are
1036 * parsed and a function which handles this comment is called.
1037 */
1038 static void
1039 comment()
1040 {
1041 int c, lc;
1042 static struct {
1043 const char *keywd;
1044 int arg;
1045 void (*func) __P((int));
1046 } keywtab[] = {
1047 { "ARGSUSED", 1, argsused },
1048 { "CONSTCOND", 0, constcond },
1049 { "CONSTANTCOND", 0, constcond },
1050 { "CONSTANTCONDITION", 0, constcond },
1051 { "FALLTHRU", 0, fallthru },
1052 { "FALLTHROUGH", 0, fallthru },
1053 { "LINTLIBRARY", 0, lintlib },
1054 { "LINTED", 0, linted },
1055 { "LONGLONG", 0, longlong },
1056 { "NOSTRICT", 0, linted },
1057 { "NOTREACHED", 0, notreach },
1058 { "PRINTFLIKE", 1, printflike },
1059 { "PROTOLIB", 1, protolib },
1060 { "SCANFLIKE", 1, scanflike },
1061 { "VARARGS", 1, varargs },
1062 };
1063 char keywd[32];
1064 char arg[32];
1065 int l, i, a;
1066 int eoc;
1067
1068 eoc = 0;
1069
1070 /* Skip white spaces after the start of the comment */
1071 while ((c = inpc()) != EOF && isspace(c)) ;
1072
1073 /* Read the potential keyword to keywd */
1074 l = 0;
1075 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1076 keywd[l++] = (char)c;
1077 c = inpc();
1078 }
1079 keywd[l] = '\0';
1080
1081 /* look for the keyword */
1082 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1083 if (strcmp(keywtab[i].keywd, keywd) == 0)
1084 break;
1085 }
1086 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1087 goto skip_rest;
1088
1089 /* skip white spaces after the keyword */
1090 while (c != EOF && isspace(c))
1091 c = inpc();
1092
1093 /* read the argument, if the keyword accepts one and there is one */
1094 l = 0;
1095 if (keywtab[i].arg) {
1096 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1097 arg[l++] = (char)c;
1098 c = inpc();
1099 }
1100 }
1101 arg[l] = '\0';
1102 a = l != 0 ? atoi(arg) : -1;
1103
1104 /* skip white spaces after the argument */
1105 while (c != EOF && isspace(c))
1106 c = inpc();
1107
1108 if (c != '*' || (c = inpc()) != '/') {
1109 if (keywtab[i].func != linted)
1110 /* extra characters in lint comment */
1111 warning(257);
1112 } else {
1113 /*
1114 * remember that we have already found the end of the
1115 * comment
1116 */
1117 eoc = 1;
1118 }
1119
1120 if (keywtab[i].func != NULL)
1121 (*keywtab[i].func)(a);
1122
1123 skip_rest:
1124 while (!eoc) {
1125 lc = c;
1126 if ((c = inpc()) == EOF) {
1127 /* unterminated comment */
1128 error(256);
1129 break;
1130 }
1131 if (lc == '*' && c == '/')
1132 eoc = 1;
1133 }
1134 }
1135
1136 /*
1137 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1138 * clrwflgs() is called after function definitions and global and
1139 * local declarations and definitions. It is also called between
1140 * the controlling expression and the body of control statements
1141 * (if, switch, for, while).
1142 */
1143 void
1144 clrwflgs()
1145 {
1146 nowarn = 0;
1147 quadflg = 0;
1148 ccflg = 0;
1149 }
1150
1151 /*
1152 * Strings are stored in a dynamically alloceted buffer and passed
1153 * in yylval.y_xstrg to the parser. The parser or the routines called
1154 * by the parser are responsible for freeing this buffer.
1155 */
1156 static int
1157 string()
1158 {
1159 u_char *s;
1160 int c;
1161 size_t len, max;
1162 strg_t *strg;
1163
1164 s = xmalloc(max = 64);
1165
1166 len = 0;
1167 while ((c = getescc('"')) >= 0) {
1168 /* +1 to reserve space for a trailing NUL character */
1169 if (len + 1 == max)
1170 s = xrealloc(s, max *= 2);
1171 s[len++] = (char)c;
1172 }
1173 s[len] = '\0';
1174 if (c == -2)
1175 /* unterminated string constant */
1176 error(258);
1177
1178 strg = xcalloc(1, sizeof (strg_t));
1179 strg->st_tspec = CHAR;
1180 strg->st_len = len;
1181 strg->st_cp = s;
1182
1183 yylval.y_strg = strg;
1184 return (T_STRING);
1185 }
1186
1187 static int
1188 wcstrg()
1189 {
1190 char *s;
1191 int c, i, n, wi;
1192 size_t len, max, wlen;
1193 wchar_t *ws;
1194 strg_t *strg;
1195
1196 s = xmalloc(max = 64);
1197 len = 0;
1198 while ((c = getescc('"')) >= 0) {
1199 /* +1 to save space for a trailing NUL character */
1200 if (len + 1 >= max)
1201 s = xrealloc(s, max *= 2);
1202 s[len++] = (char)c;
1203 }
1204 s[len] = '\0';
1205 if (c == -2)
1206 /* unterminated string constant */
1207 error(258);
1208
1209 /* get length of wide character string */
1210 (void)mblen(NULL, 0);
1211 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1212 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1213 /* invalid multibyte character */
1214 error(291);
1215 break;
1216 }
1217 if (n == 0)
1218 n = 1;
1219 }
1220
1221 ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1222
1223 /* convert from multibyte to wide char */
1224 (void)mbtowc(NULL, NULL, 0);
1225 for (i = 0, wi = 0; i < len; i += n, wi++) {
1226 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1227 break;
1228 if (n == 0)
1229 n = 1;
1230 }
1231 ws[wi] = 0;
1232 free(s);
1233
1234 strg = xcalloc(1, sizeof (strg_t));
1235 strg->st_tspec = WCHAR;
1236 strg->st_len = wlen;
1237 strg->st_wcp = ws;
1238
1239 yylval.y_strg = strg;
1240 return (T_STRING);
1241 }
1242
1243 /*
1244 * As noted above the scanner does not create new symbol table entries
1245 * for symbols it cannot find in the symbol table. This is to avoid
1246 * putting undeclared symbols into the symbol table if a syntax error
1247 * occurs.
1248 *
1249 * getsym() is called as soon as it is probably ok to put the symbol to
1250 * the symbol table. This does not mean that it is not possible that
1251 * symbols are put to the symbol table which are than not completely
1252 * declared due to syntax errors. To avoid too many problems in this
1253 * case symbols get type int in getsym().
1254 *
1255 * XXX calls to getsym() should be delayed until decl1*() is called
1256 */
1257 sym_t *
1258 getsym(sb)
1259 sbuf_t *sb;
1260 {
1261 dinfo_t *di;
1262 char *s;
1263 sym_t *sym;
1264
1265 sym = sb->sb_sym;
1266
1267 /*
1268 * During member declaration it is possible that name() looked
1269 * for symbols of type FVFT, although it should have looked for
1270 * symbols of type FTAG. Same can happen for labels. Both cases
1271 * are compensated here.
1272 */
1273 if (symtyp == FMOS || symtyp == FLAB) {
1274 if (sym == NULL || sym->s_kind == FVFT)
1275 sym = search(sb);
1276 }
1277
1278 if (sym != NULL) {
1279 if (sym->s_kind != symtyp)
1280 lerror("storesym() 1");
1281 symtyp = FVFT;
1282 freesb(sb);
1283 return (sym);
1284 }
1285
1286 /* create a new symbol table entry */
1287
1288 /* labels must always be allocated at level 1 (outhermost block) */
1289 if (symtyp == FLAB) {
1290 sym = getlblk(1, sizeof (sym_t));
1291 s = getlblk(1, sb->sb_len + 1);
1292 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1293 sym->s_name = s;
1294 sym->s_blklev = 1;
1295 di = dcs;
1296 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1297 di = di->d_nxt;
1298 if (di->d_ctx != AUTO)
1299 lerror("storesym() 2");
1300 } else {
1301 sym = getblk(sizeof (sym_t));
1302 sym->s_name = sb->sb_name;
1303 sym->s_blklev = blklev;
1304 di = dcs;
1305 }
1306
1307 UNIQUE_CURR_POS(sym->s_dpos);
1308 if ((sym->s_kind = symtyp) != FLAB)
1309 sym->s_type = gettyp(INT);
1310
1311 symtyp = FVFT;
1312
1313 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1314 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1315 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1316
1317 *di->d_ldlsym = sym;
1318 di->d_ldlsym = &sym->s_dlnxt;
1319
1320 freesb(sb);
1321 return (sym);
1322 }
1323
1324 /*
1325 * Remove a symbol forever from the symbol table. s_blklev
1326 * is set to -1 to avoid that the symbol will later be put
1327 * back to the symbol table.
1328 */
1329 void
1330 rmsym(sym)
1331 sym_t *sym;
1332 {
1333 if ((*sym->s_rlink = sym->s_link) != NULL)
1334 sym->s_link->s_rlink = sym->s_rlink;
1335 sym->s_blklev = -1;
1336 sym->s_link = NULL;
1337 }
1338
1339 /*
1340 * Remove a list of symbols declared at one level from the symbol
1341 * table.
1342 */
1343 void
1344 rmsyms(syms)
1345 sym_t *syms;
1346 {
1347 sym_t *sym;
1348
1349 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1350 if (sym->s_blklev != -1) {
1351 if ((*sym->s_rlink = sym->s_link) != NULL)
1352 sym->s_link->s_rlink = sym->s_rlink;
1353 sym->s_link = NULL;
1354 sym->s_rlink = NULL;
1355 }
1356 }
1357 }
1358
1359 /*
1360 * Put a symbol into the symbol table
1361 */
1362 void
1363 inssym(bl, sym)
1364 int bl;
1365 sym_t *sym;
1366 {
1367 int h;
1368
1369 h = hash(sym->s_name);
1370 if ((sym->s_link = symtab[h]) != NULL)
1371 symtab[h]->s_rlink = &sym->s_link;
1372 (symtab[h] = sym)->s_rlink = &symtab[h];
1373 sym->s_blklev = bl;
1374 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1375 lerror("inssym()");
1376 }
1377
1378 /*
1379 * Called at level 0 after syntax errors
1380 * Removes all symbols which are not declared at level 0 from the
1381 * symbol table. Also frees all memory which is not associated with
1382 * level 0.
1383 */
1384 void
1385 cleanup()
1386 {
1387 sym_t *sym, *nsym;
1388 int i;
1389
1390 for (i = 0; i < HSHSIZ1; i++) {
1391 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1392 nsym = sym->s_link;
1393 if (sym->s_blklev >= 1) {
1394 if ((*sym->s_rlink = nsym) != NULL)
1395 nsym->s_rlink = sym->s_rlink;
1396 }
1397 }
1398 }
1399
1400 for (i = mblklev; i > 0; i--)
1401 freelblk(i);
1402 }
1403
1404 /*
1405 * Create a new symbol with the name of an existing symbol.
1406 */
1407 sym_t *
1408 pushdown(sym)
1409 sym_t *sym;
1410 {
1411 int h;
1412 sym_t *nsym;
1413
1414 h = hash(sym->s_name);
1415 nsym = getblk(sizeof (sym_t));
1416 if (sym->s_blklev > blklev)
1417 lerror("pushdown()");
1418 nsym->s_name = sym->s_name;
1419 UNIQUE_CURR_POS(nsym->s_dpos);
1420 nsym->s_kind = sym->s_kind;
1421 nsym->s_blklev = blklev;
1422
1423 if ((nsym->s_link = symtab[h]) != NULL)
1424 symtab[h]->s_rlink = &nsym->s_link;
1425 (symtab[h] = nsym)->s_rlink = &symtab[h];
1426
1427 *dcs->d_ldlsym = nsym;
1428 dcs->d_ldlsym = &nsym->s_dlnxt;
1429
1430 return (nsym);
1431 }
1432
1433 /*
1434 * Free any dynamically allocated memory referenced by
1435 * the value stack or yylval.
1436 * The type of information in yylval is described by tok.
1437 */
1438 void
1439 freeyyv(sp, tok)
1440 void *sp;
1441 int tok;
1442 {
1443 if (tok == T_NAME || tok == T_TYPENAME) {
1444 sbuf_t *sb = *(sbuf_t **)sp;
1445 freesb(sb);
1446 } else if (tok == T_CON) {
1447 val_t *val = *(val_t **)sp;
1448 free(val);
1449 } else if (tok == T_STRING) {
1450 strg_t *strg = *(strg_t **)sp;
1451 if (strg->st_tspec == CHAR) {
1452 free(strg->st_cp);
1453 } else if (strg->st_tspec == WCHAR) {
1454 free(strg->st_wcp);
1455 } else {
1456 lerror("fryylv() 1");
1457 }
1458 free(strg);
1459 }
1460 }
1461