2 /* $NetBSD: scan.l,v 1.26 2002/01/31 22:30:21 tv 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.26 2002/01/31 22:30:21 tv Exp $");
40 __FBSDID("$FreeBSD$");
54 #define CHAR_MASK (~(~0 << CHAR_BIT))
57 /* Current position (its also updated when an included file is parsed) */
58 pos_t curr_pos = { 1, "", 0 };
61 * Current position in C source (not updated when an included file is
64 pos_t csrc_pos = { 1, "", 0 };
66 static void incline(void);
67 static void badchar(int);
68 static sbuf_t *allocsb(void);
69 static void freesb(sbuf_t *);
70 static int inpc(void);
71 static int hash(const char *);
72 static sym_t *search(sbuf_t *);
73 static int name(void);
74 static int keyw(sym_t *);
76 static int fcon(void);
77 static int operator(int, op_t);
78 static int ccon(void);
79 static int wccon(void);
80 static int getescc(int);
81 static void directive(void);
82 static void comment(void);
83 static void slashslashcomment(void);
84 static int string(void);
85 static int wcstrg(void);
98 {L}({L}|{D})* return (name());
99 0{OD}*[lLuU]* return (icon(8));
100 {NZD}{D}*[lLuU]* return (icon(10));
101 0[xX]{HD}+[lLuU]* return (icon(16));
102 {D}+\.{D}*{EX}?[fFlL]? |
104 \.{D}+{EX}?[fFlL]? 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_stdc : 1; /* STDC keyword */
194 u_int kw_gcc : 1; /* GCC keyword */
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 }
245 static sym_t *symtab[HSHSIZ1];
247 /* bit i of the entry with index i is set */
248 uint64_t qbmasks[sizeof(uint64_t) * CHAR_BIT];
250 /* least significant i bits are set in the entry with index i */
251 uint64_t qlmasks[sizeof(uint64_t) * CHAR_BIT + 1];
253 /* least significant i bits are not set in the entry with index i */
254 uint64_t qumasks[sizeof(uint64_t) * CHAR_BIT + 1];
256 /* free list for sbuf structures */
257 static sbuf_t *sbfrlst;
259 /* Typ of next expected symbol */
264 * All keywords are written to the symbol table. This saves us looking
265 * in an extra table for each name we found.
275 for (kw = kwtab; kw->kw_name != NULL; kw++) {
276 if (kw->kw_stdc && tflag)
278 if (kw->kw_gcc && !gflag)
280 sym = getblk(sizeof (sym_t));
281 sym->s_name = kw->kw_name;
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;
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];
297 /* initialize bit-masks for quads */
298 for (i = 0; i < sizeof (uint64_t) * CHAR_BIT; i++) {
299 qbmasks[i] = (uint64_t)1 << i;
300 uq = ~(uint64_t)0 << i;
305 qlmasks[i] = ~(uint64_t)0;
309 * Get a free sbuf structure, if possible from the free list
316 if ((sb = sbfrlst) != NULL) {
317 sbfrlst = sb->sb_nxt;
319 if ((sb = malloc(sizeof (sbuf_t))) == NULL)
322 (void)memset(sb, 0, sizeof (*sb));
327 * Put a sbuf structure to the free list
333 sb->sb_nxt = sbfrlst;
338 * Read a character and ensure that it is positive (except EOF).
339 * Increment line count(s) if necessary.
346 if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
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));
362 return (v % HSHSIZ1);
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, ...).
371 * If it is a keyword, the token is returned. In some cases it is described
372 * more deeply by data written to yylval.
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.
389 sb->sb_name = yytext;
391 sb->sb_hash = hash(yytext);
393 if ((sym = search(sb)) != NULL && sym->s_keyw) {
401 if (blklev < sym->s_blklev)
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;
407 s = getblk(yyleng + 1);
408 (void)memcpy(s, yytext, yyleng + 1);
423 for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
424 if (strcmp(sym->s_name, sb->sb_name) == 0) {
425 if (sym->s_keyw || sym->s_kind == symtyp)
438 if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
439 yylval.y_scl = sym->s_scl;
440 } else if (t == T_TYPE || t == T_SOU) {
441 yylval.y_tspec = sym->s_tspec;
442 } else if (t == T_QUAL) {
443 yylval.y_tqual = sym->s_tqual;
449 * Convert a string representing an integer into internal representation.
450 * The value is returned in yylval. icon() (and yylex()) returns T_CON.
455 int l_suffix, u_suffix;
463 static tspec_t contypes[2][3] = {
465 { UINT, ULONG, UQUAD }
478 l_suffix = u_suffix = 0;
480 if ((c = cp[len - 1]) == 'l' || c == 'L') {
482 } else if (c == 'u' || c == 'U') {
489 if (l_suffix > 2 || u_suffix > 1) {
490 /* malformed integer constant */
497 if (tflag && u_suffix != 0) {
498 /* suffix U is illegal in traditional C */
501 typ = contypes[u_suffix][l_suffix];
505 ul = strtoul(cp, &eptr, base);
507 uq = strtouq(cp, &eptr, base);
509 if (eptr != cp + len)
512 /* integer constant out of range */
516 * If the value is to big for the current type, we must choose
524 } else if (ul <= (unsigned)UINT_MAX && base != 10) {
526 } else if (ul <= LONG_MAX) {
531 if (typ == UINT || typ == ULONG) {
536 * Remember that the constant is unsigned
544 if (ul > (u_int)UINT_MAX)
548 if (ul > LONG_MAX && !tflag) {
555 if (uq > QUAD_MAX && !tflag) {
561 /* LINTED (enumeration values not handled in switch) */
584 case NTSPEC: /* this value unused */
588 if (typ != QUAD && typ != UQUAD) {
592 uq = (int64_t)(long)ul;
596 uq = (uint64_t)xsign((int64_t)uq, typ, -1);
598 if ((yylval.y_val = calloc(1, sizeof(val_t))) == NULL)
600 yylval.y_val->v_tspec = typ;
601 yylval.y_val->v_ansiu = ansiu;
602 yylval.y_val->v_quad = (int64_t)uq;
608 * Returns 1 if t is a signed type and the value is negative.
610 * len is the number of significant bits. If len is -1, len is set
611 * to the width of type t.
614 sign(int64_t q, tspec_t t, int len)
617 if (t == PTR || isutyp(t))
619 return (msb(q, t, len));
623 msb(int64_t q, tspec_t t, int len)
628 return ((q & qbmasks[len - 1]) != 0);
632 * Extends the sign of q.
635 xsign(int64_t q, tspec_t t, int len)
641 if (t == PTR || isutyp(t) || !sign(q, t, len)) {
650 * Convert a string representing a floating point value into its interal
651 * representation. Type and value are returned in yylval. fcon()
652 * (and yylex()) returns T_CON.
653 * XXX Currently it is not possible to convert constants of type
654 * long double which are greater than DBL_MAX.
669 if ((c = cp[len - 1]) == 'f' || c == 'F') {
672 } else if (c == 'l' || c == 'L') {
679 if (tflag && typ != DOUBLE) {
680 /* suffixes F and L are illegal in traditional C */
685 d = strtod(cp, &eptr);
686 if (eptr != cp + len)
689 /* floating-point constant out of range */
695 /* floating-point constant out of range */
697 f = f > 0 ? FLT_MAX : -FLT_MAX;
701 if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
703 yylval.y_val->v_tspec = typ;
705 yylval.y_val->v_ldbl = f;
707 yylval.y_val->v_ldbl = d;
714 operator(int t, op_t o)
722 * Called if lex found a leading \'.
732 while ((c = getescc('\'')) >= 0) {
733 val = (val << CHAR_BIT) + c;
737 /* unterminated character constant */
740 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
741 /* too many characters in character constant */
744 /* multi-character character constant */
747 /* empty character constant */
756 yylval.y_val = xcalloc(1, sizeof (val_t));
757 yylval.y_val->v_tspec = INT;
758 yylval.y_val->v_quad = val;
764 * Called if lex found a leading L\'
769 static char buf[MB_LEN_MAX + 1];
774 while ((c = getescc('\'')) >= 0) {
783 /* unterminated character constant */
786 /* empty character constant */
789 if (i > MB_CUR_MAX) {
791 /* too many characters in character constant */
795 (void)mbtowc(NULL, NULL, 0);
796 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
797 /* invalid multibyte character */
802 if ((yylval.y_val = calloc(1, sizeof (val_t))) == NULL)
804 yylval.y_val->v_tspec = WCHAR;
805 yylval.y_val->v_quad = wc;
811 * Read a character which is part of a character constant or of a string
812 * and handle escapes.
814 * The Argument is the character which delimits the character constant or
817 * Returns -1 if the end of the character constant or string is reached,
818 * -2 if the EOF is reached, and the character otherwise.
837 /* newline in string or char constant */
845 switch (c = inpc()) {
847 if (tflag && d == '\'')
848 /* \" inside character constant undef. ... */
855 /* \? undefined in traditional C */
862 /* \a undefined in traditional C */
877 /* \v undefined in traditional C */
881 /* bad octal digit %c */
884 case '0': case '1': case '2': case '3':
885 case '4': case '5': case '6': case '7':
889 v = (v << 3) + (c - '0');
891 } while (--n && isdigit(c) && (tflag || c <= '7'));
892 if (tflag && n > 0 && isdigit(c))
893 /* bad octal digit %c */
897 /* character escape does not fit in char. */
904 /* \x undefined in traditional C */
908 while ((c = inpc()) >= 0 && isxdigit(c)) {
910 c - '0' : toupper(c) - 'A' + 10;
913 if ((v & ~CHAR_MASK) != 0) {
914 /* overflow in hex escape */
924 /* no hex digits follow \x */
936 /* dubious escape \%c */
939 /* dubious escape \%o */
948 * Called for preprocessor directives. Currently implemented are:
950 * # lineno "filename"
959 static int first = 1;
961 /* Go to first non-whitespace after # */
962 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
965 if (!isdigit((unsigned char)c)) {
967 /* undefined or invalid # directive */
971 ln = strtol(--cp, &eptr, 10);
974 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
976 while ((c = *cp++) == ' ' || c == '\t')
982 while ((c = *cp) != '"' && c != '\0')
986 if ((fnl = cp++ - fn) > PATH_MAX)
988 while ((c = *cp++) == ' ' || c == '\t')
992 warning("extra character(s) after directive");
995 /* empty string means stdin */
997 fn = "{standard input}";
998 fnl = 16; /* strlen (fn) */
1000 curr_pos.p_file = fnnalloc(fn, fnl);
1002 * If this is the first directive, the name is the name
1003 * of the C source file as specified at the command line.
1004 * It is written to the output file.
1007 csrc_pos.p_file = curr_pos.p_file;
1008 outsrc(curr_pos.p_file);
1012 curr_pos.p_line = (int)ln - 1;
1013 curr_pos.p_uniq = 0;
1014 if (curr_pos.p_file == csrc_pos.p_file) {
1015 csrc_pos.p_line = (int)ln - 1;
1016 csrc_pos.p_uniq = 0;
1021 * Handle lint comments. Following comments are currently understood:
1024 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1025 * FALLTHRU FALLTHROUGH
1034 * If one of this comments is recognized, the arguments, if any, are
1035 * parsed and a function which handles this comment is called.
1046 { "ARGSUSED", 1, argsused },
1047 { "BITFIELDTYPE", 0, bitfieldtype },
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 },
1070 /* Skip white spaces after the start of the comment */
1071 while ((c = inpc()) != EOF && isspace(c))
1074 /* Read the potential keyword to keywd */
1076 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1077 keywd[l++] = (char)c;
1082 /* look for the keyword */
1083 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1084 if (strcmp(keywtab[i].keywd, keywd) == 0)
1087 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1090 /* skip white spaces after the keyword */
1091 while (c != EOF && isspace(c))
1094 /* read the argument, if the keyword accepts one and there is one */
1096 if (keywtab[i].arg) {
1097 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1103 a = l != 0 ? atoi(arg) : -1;
1105 /* skip white spaces after the argument */
1106 while (c != EOF && isspace(c))
1109 if (c != '*' || (c = inpc()) != '/') {
1110 if (keywtab[i].func != linted)
1111 /* extra characters in lint comment */
1115 * remember that we have already found the end of the
1121 if (keywtab[i].func != NULL)
1122 (*keywtab[i].func)(a);
1127 if ((c = inpc()) == EOF) {
1128 /* unterminated comment */
1132 if (lc == '*' && c == '/')
1138 * Handle // style comments
1141 slashslashcomment(void)
1145 if (sflag < 2 && !gflag)
1146 /* // comments only supported in C99 */
1147 (void)gnuism(312, tflag ? "traditional" : "ANSI");
1149 while ((c = inpc()) != EOF && c != '\n')
1154 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1155 * clrwflgs() is called after function definitions and global and
1156 * local declarations and definitions. It is also called between
1157 * the controlling expression and the body of control statements
1158 * (if, switch, for, while).
1170 * Strings are stored in a dynamically alloceted buffer and passed
1171 * in yylval.y_xstrg to the parser. The parser or the routines called
1172 * by the parser are responsible for freeing this buffer.
1182 if ((s = malloc(max = 64)) == NULL)
1186 while ((c = getescc('"')) >= 0) {
1187 /* +1 to reserve space for a trailing NUL character */
1189 if ((s = realloc(s, max *= 2)) == NULL)
1195 /* unterminated string constant */
1198 if ((strg = calloc(1, sizeof (strg_t))) == NULL)
1200 strg->st_tspec = CHAR;
1204 yylval.y_strg = strg;
1213 size_t len, max, wlen;
1217 if ((s = malloc(max = 64)) == NULL)
1220 while ((c = getescc('"')) >= 0) {
1221 /* +1 to save space for a trailing NUL character */
1223 if ((s = realloc(s, max *= 2)) == NULL)
1229 /* unterminated string constant */
1232 /* get length of wide character string */
1233 (void)mblen(NULL, 0);
1234 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1235 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1236 /* invalid multibyte character */
1244 if ((ws = malloc((wlen + 1) * sizeof (wchar_t))) == NULL)
1247 /* convert from multibyte to wide char */
1248 (void)mbtowc(NULL, NULL, 0);
1249 for (i = 0, wi = 0; i < len; i += n, wi++) {
1250 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1258 if ((strg = calloc(1, sizeof (strg_t))) == NULL)
1260 strg->st_tspec = WCHAR;
1261 strg->st_len = wlen;
1264 yylval.y_strg = strg;
1269 * As noted above the scanner does not create new symbol table entries
1270 * for symbols it cannot find in the symbol table. This is to avoid
1271 * putting undeclared symbols into the symbol table if a syntax error
1274 * getsym() is called as soon as it is probably ok to put the symbol to
1275 * the symbol table. This does not mean that it is not possible that
1276 * symbols are put to the symbol table which are than not completely
1277 * declared due to syntax errors. To avoid too many problems in this
1278 * case symbols get type int in getsym().
1280 * XXX calls to getsym() should be delayed until decl1*() is called
1292 * During member declaration it is possible that name() looked
1293 * for symbols of type FVFT, although it should have looked for
1294 * symbols of type FTAG. Same can happen for labels. Both cases
1295 * are compensated here.
1297 if (symtyp == FMOS || symtyp == FLAB) {
1298 if (sym == NULL || sym->s_kind == FVFT)
1303 if (sym->s_kind != symtyp)
1304 lerror("storesym() 1");
1310 /* create a new symbol table entry */
1312 /* labels must always be allocated at level 1 (outhermost block) */
1313 if (symtyp == FLAB) {
1314 sym = getlblk(1, sizeof (sym_t));
1315 s = getlblk(1, sb->sb_len + 1);
1316 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1320 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1322 if (di->d_ctx != AUTO)
1323 lerror("storesym() 2");
1325 sym = getblk(sizeof (sym_t));
1326 sym->s_name = sb->sb_name;
1327 sym->s_blklev = blklev;
1331 UNIQUE_CURR_POS(sym->s_dpos);
1332 if ((sym->s_kind = symtyp) != FLAB)
1333 sym->s_type = gettyp(INT);
1337 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1338 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1339 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1341 *di->d_ldlsym = sym;
1342 di->d_ldlsym = &sym->s_dlnxt;
1349 * Remove a symbol forever from the symbol table. s_blklev
1350 * is set to -1 to avoid that the symbol will later be put
1351 * back to the symbol table.
1357 if ((*sym->s_rlink = sym->s_link) != NULL)
1358 sym->s_link->s_rlink = sym->s_rlink;
1364 * Remove a list of symbols declared at one level from the symbol
1372 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1373 if (sym->s_blklev != -1) {
1374 if ((*sym->s_rlink = sym->s_link) != NULL)
1375 sym->s_link->s_rlink = sym->s_rlink;
1377 sym->s_rlink = NULL;
1383 * Put a symbol into the symbol table
1386 inssym(int bl, sym_t *sym)
1390 h = hash(sym->s_name);
1391 if ((sym->s_link = symtab[h]) != NULL)
1392 symtab[h]->s_rlink = &sym->s_link;
1393 (symtab[h] = sym)->s_rlink = &symtab[h];
1395 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1400 * Called at level 0 after syntax errors
1401 * Removes all symbols which are not declared at level 0 from the
1402 * symbol table. Also frees all memory which is not associated with
1411 for (i = 0; i < HSHSIZ1; i++) {
1412 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1414 if (sym->s_blklev >= 1) {
1415 if ((*sym->s_rlink = nsym) != NULL)
1416 nsym->s_rlink = sym->s_rlink;
1421 for (i = mblklev; i > 0; i--)
1426 * Create a new symbol with the name of an existing symbol.
1429 pushdown(sym_t *sym)
1434 h = hash(sym->s_name);
1435 nsym = getblk(sizeof (sym_t));
1436 if (sym->s_blklev > blklev)
1437 lerror("pushdown()");
1438 nsym->s_name = sym->s_name;
1439 UNIQUE_CURR_POS(nsym->s_dpos);
1440 nsym->s_kind = sym->s_kind;
1441 nsym->s_blklev = blklev;
1443 if ((nsym->s_link = symtab[h]) != NULL)
1444 symtab[h]->s_rlink = &nsym->s_link;
1445 (symtab[h] = nsym)->s_rlink = &symtab[h];
1447 *dcs->d_ldlsym = nsym;
1448 dcs->d_ldlsym = &nsym->s_dlnxt;
1454 * Free any dynamically allocated memory referenced by
1455 * the value stack or yylval.
1456 * The type of information in yylval is described by tok.
1459 freeyyv(void *sp, int tok)
1461 if (tok == T_NAME || tok == T_TYPENAME) {
1462 sbuf_t *sb = *(sbuf_t **)sp;
1464 } else if (tok == T_CON) {
1465 val_t *val = *(val_t **)sp;
1467 } else if (tok == T_STRING) {
1468 strg_t *strg = *(strg_t **)sp;
1469 if (strg->st_tspec == CHAR) {
1471 } else if (strg->st_tspec == WCHAR) {
1474 lerror("fryylv() 1");