2 /* $NetBSD: scan.l,v 1.45 2009/10/02 15:03:45 christos Exp $ */
5 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 * Copyright (c) 1994, 1995 Jochen Pohl
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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.
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 __RCSID("$NetBSD: scan.l,v 1.45 2009/10/02 15:03:45 christos Exp $");
52 #define CHAR_MASK (~(~0 << CHAR_BIT))
54 /* Current position (its also updated when an included file is parsed) */
55 pos_t curr_pos = { 1, "", 0 };
58 * Current position in C source (not updated when an included file is
61 pos_t csrc_pos = { 1, "", 0 };
63 static void incline(void);
64 static void badchar(int);
65 static sbuf_t *allocsb(void);
66 static void freesb(sbuf_t *);
67 static int inpc(void);
68 static int hash(const char *);
69 static sym_t *search(sbuf_t *);
70 static int name(void);
71 static int keyw(sym_t *);
73 static int fcon(void);
74 static int operator(int, op_t);
75 static int ccon(void);
76 static int wccon(void);
77 static int getescc(int);
78 static void directive(void);
79 static void comment(void);
80 static void slashslashcomment(void);
81 static int string(void);
82 static int wcstrg(void);
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]?[i]? |
102 {D}+{EX}[fFlL]?[i]? |
103 0[xX]{HD}+p{HD}+[fFlL]?[i]? |
104 \.{D}+{EX}?[fFlL]?[i]? return (fcon());
105 "=" return (operator(T_ASSIGN, ASSIGN));
106 "*=" return (operator(T_OPASS, MULASS));
107 "/=" return (operator(T_OPASS, DIVASS));
108 "%=" return (operator(T_OPASS, MODASS));
109 "+=" return (operator(T_OPASS, ADDASS));
110 "-=" return (operator(T_OPASS, SUBASS));
111 "<<=" return (operator(T_OPASS, SHLASS));
112 ">>=" return (operator(T_OPASS, SHRASS));
113 "&=" return (operator(T_OPASS, ANDASS));
114 "^=" return (operator(T_OPASS, XORASS));
115 "|=" return (operator(T_OPASS, ORASS));
116 "||" return (operator(T_LOGOR, LOGOR));
117 "&&" return (operator(T_LOGAND, LOGAND));
118 "|" return (operator(T_OR, OR));
119 "&" return (operator(T_AND, AND));
120 "^" return (operator(T_XOR, XOR));
121 "==" return (operator(T_EQOP, EQ));
122 "!=" return (operator(T_EQOP, NE));
123 "<" return (operator(T_RELOP, LT));
124 ">" return (operator(T_RELOP, GT));
125 "<=" return (operator(T_RELOP, LE));
126 ">=" return (operator(T_RELOP, GE));
127 "<<" return (operator(T_SHFTOP, SHL));
128 ">>" return (operator(T_SHFTOP, SHR));
129 "++" return (operator(T_INCDEC, INC));
130 "--" return (operator(T_INCDEC, DEC));
131 "->" return (operator(T_STROP, ARROW));
132 "." return (operator(T_STROP, POINT));
133 "+" return (operator(T_ADDOP, PLUS));
134 "-" return (operator(T_ADDOP, MINUS));
135 "*" return (operator(T_MULT, MULT));
136 "/" return (operator(T_DIVOP, DIV));
137 "%" return (operator(T_DIVOP, MOD));
138 "!" return (operator(T_UNOP, NOT));
139 "~" return (operator(T_UNOP, COMPL));
140 "\"" return (string());
141 "L\"" return (wcstrg());
143 "{" return (T_LBRACE);
144 "}" return (T_RBRACE);
145 "," return (T_COMMA);
146 ":" return (T_COLON);
147 "?" return (T_QUEST);
148 "[" return (T_LBRACK);
149 "]" return (T_RBRACK);
150 "(" return (T_LPARN);
151 ")" return (T_RPARN);
152 "..." return (T_ELLIPSE);
154 "L'" return (wccon());
159 "//" slashslashcomment();
160 . badchar(yytext[0]);
169 if (curr_pos.p_file == csrc_pos.p_file) {
179 /* unknown character \%o */
185 * During initialisation they are written to the symbol table.
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_c89; /* c89 keyword */
194 u_int kw_c99; /* c99 keyword */
195 u_int kw_gcc; /* GCC keyword */
197 { "__alignof__", T_ALIGNOF, 0, 0, 0, 0, 0, 0 },
198 { "__attribute__",T_ATTRIBUTE, 0, 0, 0, 0, 0, 1 },
199 { "attribute", T_ATTRIBUTE, 0, 0, 0, 0, 0, 1 },
200 { "__packed__", T_AT_PACKED, 0, 0, 0, 0, 0, 1 },
201 { "packed", T_AT_PACKED, 0, 0, 0, 0, 0, 1 },
202 { "__aligned__",T_AT_ALIGNED, 0, 0, 0, 0, 0, 1 },
203 { "aligned", T_AT_ALIGNED, 0, 0, 0, 0, 0, 1 },
204 { "__transparent_union__",T_AT_TUNION,0,0, 0, 0, 0, 1 },
205 { "transparent_union",T_AT_TUNION,0, 0, 0, 0, 0, 1 },
206 { "__unused__", T_AT_UNUSED, 0, 0, 0, 0, 0, 1 },
207 { "unused", T_AT_UNUSED, 0, 0, 0, 0, 0, 1 },
208 { "__deprecated__",T_AT_DEPRECATED,0, 0, 0, 0, 0, 1 },
209 { "deprecated", T_AT_DEPRECATED,0, 0, 0, 0, 0, 1 },
210 { "__may_alias__",T_AT_MAY_ALIAS,0, 0, 0, 0, 0, 1 },
211 { "may_alias", T_AT_MAY_ALIAS, 0, 0, 0, 0, 0, 1 },
212 { "asm", T_ASM, 0, 0, 0, 0, 0, 1 },
213 { "__asm", T_ASM, 0, 0, 0, 0, 0, 0 },
214 { "__asm__", T_ASM, 0, 0, 0, 0, 0, 0 },
215 { "auto", T_SCLASS, AUTO, 0, 0, 0, 0, 0 },
216 { "break", T_BREAK, 0, 0, 0, 0, 0, 0 },
217 { "_Bool", T_TYPE, 0, BOOL, 0, 0, 1, 0 },
218 { "case", T_CASE, 0, 0, 0, 0, 0, 0 },
219 { "char", T_TYPE, 0, CHAR, 0, 0, 0, 0 },
220 { "const", T_QUAL, 0, 0, CONST, 1, 0, 0 },
221 { "_Complex", T_TYPE, 0, COMPLEX,0, 0, 1, 0 },
222 { "__const__", T_QUAL, 0, 0, CONST, 0, 0, 0 },
223 { "__const", T_QUAL, 0, 0, CONST, 0, 0, 0 },
224 { "continue", T_CONTINUE, 0, 0, 0, 0, 0, 0 },
225 { "default", T_DEFAULT, 0, 0, 0, 0, 0, 0 },
226 { "do", T_DO, 0, 0, 0, 0, 0, 0 },
227 { "double", T_TYPE, 0, DOUBLE, 0, 0, 0, 0 },
228 { "else", T_ELSE, 0, 0, 0, 0, 0, 0 },
229 { "enum", T_ENUM, 0, 0, 0, 0, 0, 0 },
230 { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0, 0 },
231 { "float", T_TYPE, 0, FLOAT, 0, 0, 0, 0 },
232 { "for", T_FOR, 0, 0, 0, 0, 0, 0 },
233 { "goto", T_GOTO, 0, 0, 0, 0, 0, 0 },
234 { "if", T_IF, 0, 0, 0, 0, 0, 0 },
235 { "__imag__", T_IMAG, 0, 0, 0, 0, 1, 0 },
236 { "inline", T_SCLASS, INLINE, 0, 0, 0, 1, 0 },
237 { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0, 0 },
238 { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0, 0 },
239 { "int", T_TYPE, 0, INT, 0, 0, 0, 0 },
240 { "__symbolrename", T_SYMBOLRENAME, 0, 0, 0, 0, 0, 0 },
241 { "long", T_TYPE, 0, LONG, 0, 0, 0, 0 },
242 { "__real__", T_REAL, 0, 0, 0, 0, 1, 0 },
243 { "register", T_SCLASS, REG, 0, 0, 0, 0, 0 },
244 { "restrict", T_QUAL, 0, 0, RESTRICT, 0, 1, 0 },
245 { "return", T_RETURN, 0, 0, 0, 0, 0, 0 },
246 { "__packed", T_PACKED, 0, 0, 0, 0, 0, 0 },
247 { "short", T_TYPE, 0, SHORT, 0, 0, 0, 0 },
248 { "signed", T_TYPE, 0, SIGNED, 0, 1, 0, 0 },
249 { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0, 0 },
250 { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0, 0 },
251 { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0, 0 },
252 { "static", T_SCLASS, STATIC, 0, 0, 0, 0, 0 },
253 { "struct", T_SOU, 0, STRUCT, 0, 0, 0, 0 },
254 { "switch", T_SWITCH, 0, 0, 0, 0, 0, 0 },
255 { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0, 0 },
256 { "union", T_SOU, 0, UNION, 0, 0, 0, 0 },
257 { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0, 0 },
258 { "void", T_TYPE, 0, VOID, 0, 0, 0, 0 },
259 { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0, 0 },
260 { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0, 0 },
261 { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0, 0 },
262 { "while", T_WHILE, 0, 0, 0, 0, 0, 0 },
263 { NULL, 0, 0, 0, 0, 0, 0, 0 }
267 static sym_t *symtab[HSHSIZ1];
269 /* bit i of the entry with index i is set */
270 uint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT];
272 /* least significant i bits are set in the entry with index i */
273 uint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1];
275 /* least significant i bits are not set in the entry with index i */
276 uint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1];
278 /* free list for sbuf structures */
279 static sbuf_t *sbfrlst;
281 /* Typ of next expected symbol */
286 * All keywords are written to the symbol table. This saves us looking
287 * in a extra table for each name we found.
297 for (kw = kwtab; kw->kw_name != NULL; kw++) {
298 if ((kw->kw_c89 || kw->kw_c99) && tflag)
300 if (kw->kw_c99 && !(Sflag || gflag))
302 if (kw->kw_gcc && !gflag)
304 sym = getblk(sizeof (sym_t));
305 sym->s_name = kw->kw_name;
307 sym->s_value.v_quad = kw->kw_token;
308 if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) {
309 sym->s_tspec = kw->kw_tspec;
310 } else if (kw->kw_token == T_SCLASS) {
311 sym->s_scl = kw->kw_scl;
312 } else if (kw->kw_token == T_QUAL) {
313 sym->s_tqual = kw->kw_tqual;
315 h = hash(sym->s_name);
316 if ((sym->s_link = symtab[h]) != NULL)
317 symtab[h]->s_rlink = &sym->s_link;
318 (symtab[h] = sym)->s_rlink = &symtab[h];
321 /* initialize bit-masks for quads */
322 for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) {
323 qbmasks[i] = (uint64_t)1 << i;
324 uq = ~(uint64_t)0 << i;
329 qlmasks[i] = ~(uint64_t)0;
333 * Get a free sbuf structure, if possible from the free list
340 if ((sb = sbfrlst) != NULL) {
341 sbfrlst = sb->sb_nxt;
343 sb = xmalloc(sizeof (sbuf_t));
345 (void)memset(sb, 0, sizeof (*sb));
350 * Put a sbuf structure to the free list
356 sb->sb_nxt = sbfrlst;
361 * Read a character and ensure that it is positive (except EOF).
362 * Increment line count(s) if necessary.
369 if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
381 for (us = (const u_char *)s; *us != '\0'; us++) {
382 v = (v << sizeof (v)) + *us;
383 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
385 return (v % HSHSIZ1);
389 * Lex has found a letter followed by zero or more letters or digits.
390 * It looks for a symbol in the symbol table with the same name. This
391 * symbol must either be a keyword or a symbol of the type required by
392 * symtyp (label, member, tag, ...).
394 * If it is a keyword, the token is returned. In some cases it is described
395 * more deeply by data written to yylval.
397 * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
398 * is stored in yylval. This struct contains the name of the symbol, it's
399 * length and hash value. If there is already a symbol of the same name
400 * and type in the symbol table, the sbuf struct also contains a pointer
401 * to the symbol table entry.
412 sb->sb_name = yytext;
414 sb->sb_hash = hash(yytext);
415 if ((sym = search(sb)) != NULL && sym->s_keyw) {
423 if (blklev < sym->s_blklev)
425 sb->sb_name = sym->s_name;
426 sb->sb_len = strlen(sym->s_name);
427 tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
429 s = getblk(yyleng + 1);
430 (void)memcpy(s, yytext, yyleng + 1);
445 for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
446 if (strcmp(sym->s_name, sb->sb_name) == 0) {
447 if (sym->s_keyw || sym->s_kind == symtyp)
460 if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
461 yylval.y_scl = sym->s_scl;
462 } else if (t == T_TYPE || t == T_SOU) {
463 yylval.y_tspec = sym->s_tspec;
464 } else if (t == T_QUAL) {
465 yylval.y_tqual = sym->s_tqual;
471 * Convert a string representing an integer into internal representation.
472 * The value is returned in yylval. icon() (and yylex()) returns T_CON.
477 int l_suffix, u_suffix;
484 static tspec_t contypes[2][3] = {
486 { UINT, ULONG, UQUAD }
499 l_suffix = u_suffix = 0;
501 if ((c = cp[len - 1]) == 'l' || c == 'L') {
503 } else if (c == 'u' || c == 'U') {
510 if (l_suffix > 2 || u_suffix > 1) {
511 /* malformed integer constant */
518 if (tflag && u_suffix != 0) {
519 /* suffix U is illegal in traditional C */
522 typ = contypes[u_suffix][l_suffix];
526 uq = strtouq(cp, &eptr, base);
527 if (eptr != cp + len)
530 /* integer constant out of range */
534 * If the value is too big for the current type, we must choose
540 if (uq <= TARG_INT_MAX) {
542 } else if (uq <= TARG_UINT_MAX && base != 10) {
544 } else if (uq <= TARG_LONG_MAX) {
548 if (uq > TARG_ULONG_MAX) {
549 /* integer constant out of range */
553 if (typ == UINT || typ == ULONG) {
558 * Remember that the constant is unsigned
566 if (uq > TARG_UINT_MAX) {
568 if (uq > TARG_ULONG_MAX) {
569 /* integer constant out of range */
575 if (uq > TARG_LONG_MAX && !tflag) {
579 if (uq > TARG_ULONG_MAX) {
580 /* integer constant out of range */
586 if (uq > TARG_ULONG_MAX) {
587 /* integer constant out of range */
592 if (uq > TARG_QUAD_MAX && !tflag) {
599 if (uq > TARG_UQUAD_MAX) {
600 /* integer constant out of range */
604 /* LINTED (enumeration values not handled in switch) */
630 case NTSPEC: /* this value unused */
634 uq = (uint64_t)xsign((int64_t)uq, typ, -1);
636 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
637 yylval.y_val->v_ansiu = ansiu;
638 yylval.y_val->v_quad = (int64_t)uq;
644 * Returns 1 if t is a signed type and the value is negative.
646 * len is the number of significant bits. If len is -1, len is set
647 * to the width of type t.
650 sign(int64_t q, tspec_t t, int len)
653 if (t == PTR || isutyp(t))
655 return (msb(q, t, len));
659 msb(int64_t q, tspec_t t, int len)
664 return ((q & qbmasks[len - 1]) != 0);
668 * Extends the sign of q.
671 xsign(int64_t q, tspec_t t, int len)
677 if (t == PTR || isutyp(t) || !sign(q, t, len)) {
686 * Convert a string representing a floating point value into its interal
687 * representation. Type and value are returned in yylval. fcon()
688 * (and yylex()) returns T_CON.
689 * XXX Currently it is not possible to convert constants of type
690 * long double which are greater than DBL_MAX.
705 if (cp[len - 1] == 'i') {
706 /* imaginary, do nothing for now */
709 if ((c = cp[len - 1]) == 'f' || c == 'F') {
712 } else if (c == 'l' || c == 'L') {
719 if (tflag && typ != DOUBLE) {
720 /* suffixes F and L are illegal in traditional C */
725 d = strtod(cp, &eptr);
726 if (eptr != cp + len) {
729 * XXX: non-native non-current strtod() may not handle hex
730 * floats, ignore the rest if we find traces of hex float
745 /* floating-point constant out of range */
751 /* floating-point constant out of range */
753 f = f > 0 ? FLT_MAX : -FLT_MAX;
757 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
759 yylval.y_val->v_ldbl = f;
761 yylval.y_val->v_ldbl = d;
768 operator(int t, op_t o)
776 * Called if lex found a leading \'.
787 while ((c = getescc('\'')) >= 0) {
788 val = (val << CHAR_BIT) + c;
792 /* unterminated character constant */
795 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
796 /* too many characters in character constant */
799 /* multi-character character constant */
802 /* empty character constant */
811 yylval.y_val = xcalloc(1, sizeof (val_t));
812 yylval.y_val->v_tspec = INT;
813 yylval.y_val->v_quad = val;
819 * Called if lex found a leading L\'
824 static char buf[MB_LEN_MAX + 1];
830 while ((c = getescc('\'')) >= 0) {
839 /* unterminated character constant */
842 /* empty character constant */
845 if (i > MB_CUR_MAX) {
847 /* too many characters in character constant */
851 (void)mbtowc(NULL, NULL, 0);
852 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
853 /* invalid multibyte character */
858 yylval.y_val = xcalloc(1, sizeof (val_t));
859 yylval.y_val->v_tspec = WCHAR;
860 yylval.y_val->v_quad = wc;
866 * Read a character which is part of a character constant or of a string
867 * and handle escapes.
869 * The Argument is the character which delimits the character constant or
872 * Returns -1 if the end of the character constant or string is reached,
873 * -2 if the EOF is reached, and the character otherwise.
892 /* newline in string or char constant */
900 switch (c = inpc()) {
902 if (tflag && d == '\'')
903 /* \" inside character constant undef. ... */
910 /* \? undefined in traditional C */
917 /* \a undefined in traditional C */
932 /* \v undefined in traditional C */
936 /* bad octal digit %c */
939 case '0': case '1': case '2': case '3':
940 case '4': case '5': case '6': case '7':
944 v = (v << 3) + (c - '0');
946 } while (--n && isdigit(c) && (tflag || c <= '7'));
947 if (tflag && n > 0 && isdigit(c))
948 /* bad octal digit %c */
952 /* character escape does not fit in char. */
959 /* \x undefined in traditional C */
963 while ((c = inpc()) >= 0 && isxdigit(c)) {
965 c - '0' : toupper(c) - 'A' + 10;
968 if ((v & ~CHAR_MASK) != 0) {
969 /* overflow in hex escape */
979 /* no hex digits follow \x */
991 /* dubious escape \%c */
994 /* dubious escape \%o */
1003 * Called for preprocessor directives. Currently implemented are:
1005 * # lineno "filename"
1010 const char *cp, *fn;
1014 static int first = 1;
1016 /* Go to first non-whitespace after # */
1017 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
1020 if (!isdigit((unsigned char)c)) {
1022 /* undefined or invalid # directive */
1026 ln = strtol(--cp, &eptr, 10);
1029 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
1031 while ((c = *cp++) == ' ' || c == '\t')
1037 while ((c = *cp) != '"' && c != '\0')
1041 if ((fnl = cp++ - fn) > PATH_MAX)
1043 while ((c = *cp++) == ' ' || c == '\t')
1047 warning("extra character(s) after directive");
1050 /* empty string means stdin */
1052 fn = "{standard input}";
1053 fnl = 16; /* strlen (fn) */
1055 curr_pos.p_file = fnnalloc(fn, fnl);
1057 * If this is the first directive, the name is the name
1058 * of the C source file as specified at the command line.
1059 * It is written to the output file.
1062 csrc_pos.p_file = curr_pos.p_file;
1063 outsrc(curr_pos.p_file);
1067 curr_pos.p_line = (int)ln - 1;
1068 curr_pos.p_uniq = 0;
1069 if (curr_pos.p_file == csrc_pos.p_file) {
1070 csrc_pos.p_line = (int)ln - 1;
1071 csrc_pos.p_uniq = 0;
1076 * Handle lint comments. Following comments are currently understood:
1079 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1080 * FALLTHRU FALLTHROUGH
1089 * If one of this comments is recognized, the arguments, if any, are
1090 * parsed and a function which handles this comment is called.
1101 { "ARGSUSED", 1, argsused },
1102 { "BITFIELDTYPE", 0, bitfieldtype },
1103 { "CONSTCOND", 0, constcond },
1104 { "CONSTANTCOND", 0, constcond },
1105 { "CONSTANTCONDITION", 0, constcond },
1106 { "FALLTHRU", 0, fallthru },
1107 { "FALLTHROUGH", 0, fallthru },
1108 { "LINTLIBRARY", 0, lintlib },
1109 { "LINTED", 0, linted },
1110 { "LONGLONG", 0, longlong },
1111 { "NOSTRICT", 0, linted },
1112 { "NOTREACHED", 0, notreach },
1113 { "PRINTFLIKE", 1, printflike },
1114 { "PROTOLIB", 1, protolib },
1115 { "SCANFLIKE", 1, scanflike },
1116 { "VARARGS", 1, varargs },
1126 /* Skip white spaces after the start of the comment */
1127 while ((c = inpc()) != EOF && isspace(c))
1130 /* Read the potential keyword to keywd */
1132 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1133 keywd[l++] = (char)c;
1138 /* look for the keyword */
1139 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1140 if (strcmp(keywtab[i].keywd, keywd) == 0)
1143 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1146 /* skip white spaces after the keyword */
1147 while (c != EOF && isspace(c))
1150 /* read the argument, if the keyword accepts one and there is one */
1152 if (keywtab[i].arg) {
1153 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1159 a = l != 0 ? atoi(arg) : -1;
1161 /* skip white spaces after the argument */
1162 while (c != EOF && isspace(c))
1165 if (c != '*' || (c = inpc()) != '/') {
1166 if (keywtab[i].func != linted)
1167 /* extra characters in lint comment */
1171 * remember that we have already found the end of the
1177 if (keywtab[i].func != NULL)
1178 (*keywtab[i].func)(a);
1183 if ((c = inpc()) == EOF) {
1184 /* unterminated comment */
1188 if (lc == '*' && c == '/')
1194 * Handle // style comments
1197 slashslashcomment(void)
1201 if (!Sflag && !gflag)
1202 /* // comments only supported in C99 */
1203 (void)gnuism(312, tflag ? "traditional" : "ANSI");
1205 while ((c = inpc()) != EOF && c != '\n')
1210 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1211 * clrwflgs() is called after function definitions and global and
1212 * local declarations and definitions. It is also called between
1213 * the controlling expression and the body of control statements
1214 * (if, switch, for, while).
1226 * Strings are stored in a dynamically alloceted buffer and passed
1227 * in yylval.y_xstrg to the parser. The parser or the routines called
1228 * by the parser are responsible for freeing this buffer.
1238 s = xmalloc(max = 64);
1241 while ((c = getescc('"')) >= 0) {
1242 /* +1 to reserve space for a trailing NUL character */
1244 s = xrealloc(s, max *= 2);
1249 /* unterminated string constant */
1252 strg = xcalloc(1, sizeof (strg_t));
1253 strg->st_tspec = CHAR;
1257 yylval.y_strg = strg;
1267 size_t len, max, wlen;
1271 s = xmalloc(max = 64);
1273 while ((c = getescc('"')) >= 0) {
1274 /* +1 to save space for a trailing NUL character */
1276 s = xrealloc(s, max *= 2);
1281 /* unterminated string constant */
1284 /* get length of wide character string */
1285 (void)mblen(NULL, 0);
1286 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1287 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1288 /* invalid multibyte character */
1296 ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1298 /* convert from multibyte to wide char */
1299 (void)mbtowc(NULL, NULL, 0);
1300 for (i = 0, wi = 0; i < len; i += n, wi++) {
1301 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1309 strg = xcalloc(1, sizeof (strg_t));
1310 strg->st_tspec = WCHAR;
1311 strg->st_len = wlen;
1314 yylval.y_strg = strg;
1319 * As noted above the scanner does not create new symbol table entries
1320 * for symbols it cannot find in the symbol table. This is to avoid
1321 * putting undeclared symbols into the symbol table if a syntax error
1324 * getsym() is called as soon as it is probably ok to put the symbol to
1325 * the symbol table. This does not mean that it is not possible that
1326 * symbols are put to the symbol table which are than not completely
1327 * declared due to syntax errors. To avoid too many problems in this
1328 * case symbols get type int in getsym().
1330 * XXX calls to getsym() should be delayed until decl1*() is called
1342 * During member declaration it is possible that name() looked
1343 * for symbols of type FVFT, although it should have looked for
1344 * symbols of type FTAG. Same can happen for labels. Both cases
1345 * are compensated here.
1347 if (symtyp == FMOS || symtyp == FLAB) {
1348 if (sym == NULL || sym->s_kind == FVFT)
1353 if (sym->s_kind != symtyp)
1354 LERROR("storesym()");
1360 /* create a new symbol table entry */
1362 /* labels must always be allocated at level 1 (outhermost block) */
1363 if (symtyp == FLAB) {
1364 sym = getlblk(1, sizeof (sym_t));
1365 s = getlblk(1, sb->sb_len + 1);
1366 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1370 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1372 if (di->d_ctx != AUTO)
1373 LERROR("storesym()");
1375 sym = getblk(sizeof (sym_t));
1376 sym->s_name = sb->sb_name;
1377 sym->s_blklev = blklev;
1381 UNIQUE_CURR_POS(sym->s_dpos);
1382 if ((sym->s_kind = symtyp) != FLAB)
1383 sym->s_type = gettyp(INT);
1387 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1388 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1389 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1391 *di->d_ldlsym = sym;
1392 di->d_ldlsym = &sym->s_dlnxt;
1399 * Construct a temporary symbol. The symbol starts with a digit, so that
1403 mktempsym(type_t *t)
1407 char *s = getlblk(blklev, 64);
1408 sym_t *sym = getblk(sizeof (sym_t));
1410 (void)snprintf(s, 64, "%.8d_tmp", n++);
1415 sym->s_blklev = blklev;
1421 if ((sym->s_link = symtab[h]) != NULL)
1422 symtab[h]->s_rlink = &sym->s_link;
1423 (symtab[h] = sym)->s_rlink = &symtab[h];
1425 *dcs->d_ldlsym = sym;
1426 dcs->d_ldlsym = &sym->s_dlnxt;
1432 * Remove a symbol forever from the symbol table. s_blklev
1433 * is set to -1 to avoid that the symbol will later be put
1434 * back to the symbol table.
1440 if ((*sym->s_rlink = sym->s_link) != NULL)
1441 sym->s_link->s_rlink = sym->s_rlink;
1447 * Remove a list of symbols declared at one level from the symbol
1455 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1456 if (sym->s_blklev != -1) {
1457 if ((*sym->s_rlink = sym->s_link) != NULL)
1458 sym->s_link->s_rlink = sym->s_rlink;
1460 sym->s_rlink = NULL;
1466 * Put a symbol into the symbol table
1469 inssym(int bl, sym_t *sym)
1473 h = hash(sym->s_name);
1474 if ((sym->s_link = symtab[h]) != NULL)
1475 symtab[h]->s_rlink = &sym->s_link;
1476 (symtab[h] = sym)->s_rlink = &symtab[h];
1478 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1483 * Called at level 0 after syntax errors
1484 * Removes all symbols which are not declared at level 0 from the
1485 * symbol table. Also frees all memory which is not associated with
1494 for (i = 0; i < HSHSIZ1; i++) {
1495 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1497 if (sym->s_blklev >= 1) {
1498 if ((*sym->s_rlink = nsym) != NULL)
1499 nsym->s_rlink = sym->s_rlink;
1504 for (i = mblklev; i > 0; i--)
1509 * Create a new symbol with the name of an existing symbol.
1512 pushdown(sym_t *sym)
1517 h = hash(sym->s_name);
1518 nsym = getblk(sizeof (sym_t));
1519 if (sym->s_blklev > blklev)
1520 LERROR("pushdown()");
1521 nsym->s_name = sym->s_name;
1522 UNIQUE_CURR_POS(nsym->s_dpos);
1523 nsym->s_kind = sym->s_kind;
1524 nsym->s_blklev = blklev;
1526 if ((nsym->s_link = symtab[h]) != NULL)
1527 symtab[h]->s_rlink = &nsym->s_link;
1528 (symtab[h] = nsym)->s_rlink = &symtab[h];
1530 *dcs->d_ldlsym = nsym;
1531 dcs->d_ldlsym = &nsym->s_dlnxt;
1537 * Free any dynamically allocated memory referenced by
1538 * the value stack or yylval.
1539 * The type of information in yylval is described by tok.
1542 freeyyv(void *sp, int tok)
1544 if (tok == T_NAME || tok == T_TYPENAME) {
1545 sbuf_t *sb = *(sbuf_t **)sp;
1547 } else if (tok == T_CON) {
1548 val_t *val = *(val_t **)sp;
1550 } else if (tok == T_STRING) {
1551 strg_t *strg = *(strg_t **)sp;
1552 if (strg->st_tspec == CHAR) {
1554 } else if (strg->st_tspec == WCHAR) {