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