2 /* $NetBSD: scan.l,v 1.8 1995/10/23 13:38:51 jpo Exp $ */
5 * Copyright (c) 1994, 1995 Jochen Pohl
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * $NetBSD: scan.l,v 1.8 1995/10/23 13:38:51 jpo Exp $
35 * $DragonFly: src/usr.bin/xlint/lint1/scan.l,v 1.5 2004/06/19 18:55:48 joerg Exp $
50 #define CHAR_MASK (~(~0 << CHAR_BIT))
52 /* Current position (its also updated when an included file is parsed) */
53 pos_t curr_pos = { 1, "" };
56 * Current position in C source (not updated when an included file is
59 pos_t csrc_pos = { 1, "" };
61 static void incline(void);
62 static void badchar(int);
63 static sbuf_t *allocsb(void);
64 static void freesb(sbuf_t *);
65 static int inpc(void);
66 static int hash(const char *);
67 static sym_t *search(sbuf_t *);
68 static int name(void);
69 static int keyw(sym_t *);
71 static int fcon(void);
72 static int operator(int, op_t);
73 static int ccon(void);
74 static int wccon(void);
75 static int getescc(int);
76 static void directive(void);
77 static void comment(void);
78 static int string(void);
79 static int wcstrg(void);
92 {L}({L}|{D})* return (name());
93 0{OD}*[lLuU]* return (icon(8));
94 {NZD}{D}*[lLuU]* return (icon(10));
95 0[xX]{HD}+[lLuU]* return (icon(16));
96 {D}+\.{D}*{EX}?[fFlL]? |
98 \.{D}+{EX}?[fFlL]? return (fcon());
99 "=" return (operator(T_ASSIGN, ASSIGN));
100 "*=" return (operator(T_OPASS, MULASS));
101 "/=" return (operator(T_OPASS, DIVASS));
102 "%=" return (operator(T_OPASS, MODASS));
103 "+=" return (operator(T_OPASS, ADDASS));
104 "-=" return (operator(T_OPASS, SUBASS));
105 "<<=" return (operator(T_OPASS, SHLASS));
106 ">>=" return (operator(T_OPASS, SHRASS));
107 "&=" return (operator(T_OPASS, ANDASS));
108 "^=" return (operator(T_OPASS, XORASS));
109 "|=" return (operator(T_OPASS, ORASS));
110 "||" return (operator(T_LOGOR, LOGOR));
111 "&&" return (operator(T_LOGAND, LOGAND));
112 "|" return (operator(T_OR, OR));
113 "&" return (operator(T_AND, AND));
114 "^" return (operator(T_XOR, XOR));
115 "==" return (operator(T_EQOP, EQ));
116 "!=" return (operator(T_EQOP, NE));
117 "<" return (operator(T_RELOP, LT));
118 ">" return (operator(T_RELOP, GT));
119 "<=" return (operator(T_RELOP, LE));
120 ">=" return (operator(T_RELOP, GE));
121 "<<" return (operator(T_SHFTOP, SHL));
122 ">>" return (operator(T_SHFTOP, SHR));
123 "++" return (operator(T_INCDEC, INC));
124 "--" return (operator(T_INCDEC, DEC));
125 "->" return (operator(T_STROP, ARROW));
126 "." return (operator(T_STROP, POINT));
127 "+" return (operator(T_ADDOP, PLUS));
128 "-" return (operator(T_ADDOP, MINUS));
129 "*" return (operator(T_MULT, MULT));
130 "/" return (operator(T_DIVOP, DIV));
131 "%" return (operator(T_DIVOP, MOD));
132 "!" return (operator(T_UNOP, NOT));
133 "~" return (operator(T_UNOP, COMPL));
134 "\"" return (string());
135 "L\"" return (wcstrg());
137 "{" return (T_LBRACE);
138 "}" return (T_RBRACE);
139 "," return (T_COMMA);
140 ":" return (T_COLON);
141 "?" return (T_QUEST);
142 "[" return (T_LBRACK);
143 "]" return (T_RBRACK);
144 "(" return (T_LPARN);
145 ")" return (T_RPARN);
146 "..." return (T_ELLIPSE);
148 "L'" return (wccon());
153 . badchar(yytext[0]);
161 if (curr_pos.p_file == csrc_pos.p_file)
169 /* unknown character \%o */
175 * During initialisation they are written to the symbol table.
177 static struct kwtab {
178 const char *kw_name; /* keyword */
179 int kw_token; /* token returned by yylex() */
180 scl_t kw_scl; /* storage class if kw_token T_SCLASS */
181 tspec_t kw_tspec; /* type spec. if kw_token T_TYPE or T_SOU */
182 tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */
183 u_int kw_stdc : 1; /* STDC keyword */
184 u_int kw_gcc : 1; /* GCC keyword */
186 { "asm", T_ASM, 0, 0, 0, 0, 1 },
187 { "__asm", T_ASM, 0, 0, 0, 0, 0 },
188 { "__asm__", T_ASM, 0, 0, 0, 0, 0 },
189 { "auto", T_SCLASS, AUTO, 0, 0, 0, 0 },
190 { "break", T_BREAK, 0, 0, 0, 0, 0 },
191 { "case", T_CASE, 0, 0, 0, 0, 0 },
192 { "char", T_TYPE, 0, CHAR, 0, 0, 0 },
193 { "const", T_QUAL, 0, 0, CONST, 1, 0 },
194 { "__const__", T_QUAL, 0, 0, CONST, 0, 0 },
195 { "__const", T_QUAL, 0, 0, CONST, 0, 0 },
196 { "continue", T_CONTINUE, 0, 0, 0, 0, 0 },
197 { "default", T_DEFAULT, 0, 0, 0, 0, 0 },
198 { "do", T_DO, 0, 0, 0, 0, 0 },
199 { "double", T_TYPE, 0, DOUBLE, 0, 0, 0 },
200 { "else", T_ELSE, 0, 0, 0, 0, 0 },
201 { "enum", T_ENUM, 0, 0, 0, 0, 0 },
202 { "extern", T_SCLASS, EXTERN, 0, 0, 0, 0 },
203 { "float", T_TYPE, 0, FLOAT, 0, 0, 0 },
204 { "for", T_FOR, 0, 0, 0, 0, 0 },
205 { "goto", T_GOTO, 0, 0, 0, 0, 0 },
206 { "if", T_IF, 0, 0, 0, 0, 0 },
207 { "inline", T_SCLASS, INLINE, 0, 0, 0, 1 },
208 { "__inline__", T_SCLASS, INLINE, 0, 0, 0, 0 },
209 { "__inline", T_SCLASS, INLINE, 0, 0, 0, 0 },
210 { "int", T_TYPE, 0, INT, 0, 0, 0 },
211 { "long", T_TYPE, 0, LONG, 0, 0, 0 },
212 { "register", T_SCLASS, REG, 0, 0, 0, 0 },
213 { "return", T_RETURN, 0, 0, 0, 0, 0 },
214 { "short", T_TYPE, 0, SHORT, 0, 0, 0 },
215 { "signed", T_TYPE, 0, SIGNED, 0, 1, 0 },
216 { "__signed__", T_TYPE, 0, SIGNED, 0, 0, 0 },
217 { "__signed", T_TYPE, 0, SIGNED, 0, 0, 0 },
218 { "sizeof", T_SIZEOF, 0, 0, 0, 0, 0 },
219 { "static", T_SCLASS, STATIC, 0, 0, 0, 0 },
220 { "struct", T_SOU, 0, STRUCT, 0, 0, 0 },
221 { "switch", T_SWITCH, 0, 0, 0, 0, 0 },
222 { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0, 0 },
223 { "union", T_SOU, 0, UNION, 0, 0, 0 },
224 { "unsigned", T_TYPE, 0, UNSIGN, 0, 0, 0 },
225 { "void", T_TYPE, 0, VOID, 0, 0, 0 },
226 { "volatile", T_QUAL, 0, 0, VOLATILE, 1, 0 },
227 { "__volatile__", T_QUAL, 0, 0, VOLATILE, 0, 0 },
228 { "__volatile", T_QUAL, 0, 0, VOLATILE, 0, 0 },
229 { "while", T_WHILE, 0, 0, 0, 0, 0 },
230 { NULL, 0, 0, 0, 0, 0, 0 }
234 static sym_t *symtab[HSHSIZ1];
236 /* bit i of the entry with index i is set */
237 u_quad_t qbmasks[sizeof(u_quad_t) * CHAR_BIT];
239 /* least significant i bits are set in the entry with index i */
240 u_quad_t qlmasks[sizeof(u_quad_t) * CHAR_BIT + 1];
242 /* least significant i bits are not set in the entry with index i */
243 u_quad_t qumasks[sizeof(u_quad_t) * CHAR_BIT + 1];
245 /* free list for sbuf structures */
246 static sbuf_t *sbfrlst;
248 /* Typ of next expected symbol */
253 * All keywords are written to the symbol table. This saves us looking
254 * in a extra table for each name we found.
264 for (kw = kwtab; kw->kw_name != NULL; kw++) {
265 if (kw->kw_stdc && tflag)
267 if (kw->kw_gcc && !gflag)
269 sym = getblk(sizeof (sym_t));
270 sym->s_name = kw->kw_name;
272 sym->s_value.v_quad = kw->kw_token;
273 if (kw->kw_token == T_TYPE || kw->kw_token == T_SOU) {
274 sym->s_tspec = kw->kw_tspec;
275 } else if (kw->kw_token == T_SCLASS) {
276 sym->s_scl = kw->kw_scl;
277 } else if (kw->kw_token == T_QUAL) {
278 sym->s_tqual = kw->kw_tqual;
280 h = hash(sym->s_name);
281 if ((sym->s_link = symtab[h]) != NULL)
282 symtab[h]->s_rlink = &sym->s_link;
283 (symtab[h] = sym)->s_rlink = &symtab[h];
286 /* initialize bit-masks for quads */
287 for (i = 0; i < sizeof (u_quad_t) * CHAR_BIT; i++) {
288 qbmasks[i] = (u_quad_t)1 << i;
289 uq = ~(u_quad_t)0 << i;
294 qlmasks[i] = ~(u_quad_t)0;
298 * Get a free sbuf structure, if possible from the free list
305 if ((sb = sbfrlst) != NULL) {
306 sbfrlst = sb->sb_nxt;
308 sb = xmalloc(sizeof (sbuf_t));
310 (void)memset(sb, 0, sizeof (sb));
315 * Put a sbuf structure to the free list
321 sb->sb_nxt = sbfrlst;
326 * Read a character and ensure that it is positive (except EOF).
327 * Increment line count(s) if necessary.
334 if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
347 for (us = (const u_char *)s; *us != '\0'; us++) {
348 v = (v << sizeof (v)) + *us;
349 v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
351 return (v % HSHSIZ1);
355 * Lex has found a letter followed by zero or more letters or digits.
356 * It looks for a symbol in the symbol table with the same name. This
357 * symbol must either be a keyword or a symbol of the type required by
358 * symtyp (label, member, tag, ...).
360 * If it is a keyword, the token is returned. In some cases it is described
361 * more deeply by data written to yylval.
363 * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
364 * is stored in yylval. This struct contains the name of the symbol, it's
365 * length and hash value. If there is already a symbol of the same name
366 * and type in the symbol table, the sbuf struct also contains a pointer
367 * to the symbol table entry.
378 sb->sb_name = yytext;
380 sb->sb_hash = hash(yytext);
382 if ((sym = search(sb)) != NULL && sym->s_keyw) {
390 if (blklev < sym->s_blklev)
392 sb->sb_name = sym->s_name;
393 sb->sb_len = strlen(sym->s_name);
394 tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
396 s = getblk(yyleng + 1);
397 (void)memcpy(s, yytext, yyleng + 1);
413 for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
414 if (strcmp(sym->s_name, sb->sb_name) == 0) {
415 if (sym->s_keyw || sym->s_kind == symtyp)
429 if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
430 yylval.y_scl = sym->s_scl;
431 } else if (t == T_TYPE || t == T_SOU) {
432 yylval.y_tspec = sym->s_tspec;
433 } else if (t == T_QUAL) {
434 yylval.y_tqual = sym->s_tqual;
440 * Convert a string representing an integer into internal representation.
441 * The value is returned in yylval. icon() (and yylex()) returns T_CON.
447 int l_suffix, u_suffix;
455 static tspec_t contypes[2][3] = {
457 { UINT, ULONG, UQUAD }
470 l_suffix = u_suffix = 0;
472 if ((c = cp[len - 1]) == 'l' || c == 'L') {
474 } else if (c == 'u' || c == 'U') {
481 if (l_suffix > 2 || u_suffix > 1) {
482 /* malformed integer constant */
489 if (tflag && u_suffix != 0) {
490 /* suffix U is illegal in traditional C */
493 typ = contypes[u_suffix][l_suffix];
497 ul = strtoul(cp, &eptr, base);
499 uq = strtouq(cp, &eptr, base);
501 if (eptr != cp + len)
504 /* integer constant out of range */
508 * If the value is to big for the current type, we must choose
516 } else if (ul <= (unsigned)UINT_MAX && base != 10) {
518 } else if (ul <= LONG_MAX) {
523 if (typ == UINT || typ == ULONG) {
528 * Remember that the constant is unsigned
536 if (ul > (u_int)UINT_MAX)
540 if (ul > LONG_MAX && !tflag) {
547 if (uq > QUAD_MAX && !tflag) {
553 /* LINTED (enumeration values not handled in switch) */
558 if (typ != QUAD && typ != UQUAD) {
562 uq = (quad_t)(long)ul;
566 uq = (u_quad_t)xsign((quad_t)uq, typ, -1);
568 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
569 yylval.y_val->v_ansiu = ansiu;
570 yylval.y_val->v_quad = (quad_t)uq;
576 * Returns 1 if t is a signed type and the value is negative.
578 * len is the number of significant bits. If len is -1, len is set
579 * to the width of type t.
587 if (t == PTR || isutyp(t))
589 return (msb(q, t, len));
600 return ((q & qbmasks[len - 1]) != 0);
604 * Extends the sign of q.
615 if (t == PTR || isutyp(t) || !sign(q, t, len)) {
624 * Convert a string representing a floating point value into its interal
625 * representation. Type and value are returned in yylval. fcon()
626 * (and yylex()) returns T_CON.
627 * XXX Currently it is not possible to convert constants of type
628 * long double which are greater then DBL_MAX.
643 if ((c = cp[len - 1]) == 'f' || c == 'F') {
646 } else if (c == 'l' || c == 'L') {
653 if (tflag && typ != DOUBLE) {
654 /* suffixes F and L are illegal in traditional C */
659 d = strtod(cp, &eptr);
660 if (eptr != cp + len)
663 /* floating-point constant out of range */
669 /* floating-point constant out of range */
671 f = f > 0 ? FLT_MAX : -FLT_MAX;
675 (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
677 yylval.y_val->v_ldbl = f;
679 yylval.y_val->v_ldbl = d;
695 * Called if lex found a leading \'.
705 while ((c = getescc('\'')) >= 0) {
706 val = (val << CHAR_BIT) + c;
710 /* unterminated character constant */
713 if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
714 /* too many characters in character constant */
717 /* multi-character character constant */
720 /* empty character constant */
729 yylval.y_val = xcalloc(1, sizeof (val_t));
730 yylval.y_val->v_tspec = INT;
731 yylval.y_val->v_quad = val;
737 * Called if lex found a leading L\'
742 static char buf[MB_LEN_MAX + 1];
747 while ((c = getescc('\'')) >= 0) {
756 /* unterminated character constant */
759 /* empty character constant */
762 if (i > MB_CUR_MAX) {
764 /* too many characters in character constant */
768 (void)mbtowc(NULL, NULL, 0);
769 if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
770 /* invalid multibyte character */
775 yylval.y_val = xcalloc(1, sizeof (val_t));
776 yylval.y_val->v_tspec = WCHAR;
777 yylval.y_val->v_quad = wc;
783 * Read a character which is part of a character constant or of a string
784 * and handle escapes.
786 * The Argument is the character which delimits the character constant or
789 * Returns -1 if the end of the character constant or string is reached,
790 * -2 if the EOF is reached, and the charachter otherwise.
809 /* newline in string or char constant */
815 switch (c = inpc()) {
817 if (tflag && d == '\'')
818 /* \" inside character constant undef. ... */
825 /* \? undefined in traditional C */
832 /* \a undefined in traditional C */
851 /* \v undefined in traditional C */
859 /* bad octal digit %c */
862 case '0': case '1': case '2': case '3':
863 case '4': case '5': case '6': case '7':
867 v = (v << 3) + (c - '0');
869 } while (--n && isdigit(c) && (tflag || c <= '7'));
870 if (tflag && n > 0 && isdigit(c))
871 /* bad octal digit %c */
875 /* character escape does not fit in char. */
882 /* \x undefined in traditional C */
886 while ((c = inpc()) >= 0 && isxdigit(c)) {
888 c - '0' : toupper(c) - 'A' + 10;
891 if ((v & ~CHAR_MASK) != 0) {
892 /* overflow in hex escape */
902 /* no hex digits follow \x */
914 /* dubious escape \%c */
917 /* dubious escape \%o */
926 * Called for preprocessor directives. Currently implemented are:
928 * # lineno "filename"
937 static int first = 1;
939 /* Go to first non-whitespace after # */
940 for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++) ;
944 /* undefined or invalid # directive */
948 ln = strtol(--cp, &eptr, 10);
951 if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
953 while ((c = *cp++) == ' ' || c == '\t') ;
958 while ((c = *cp) != '"' && c != '\0')
962 if ((fnl = cp++ - fn) > PATH_MAX)
964 while ((c = *cp++) == ' ' || c == '\t') ;
967 warning("extra character(s) after directive");
969 curr_pos.p_file = fnnalloc(fn, fnl);
971 * If this is the first directive, the name is the name
972 * of the C source file as specified at the command line.
973 * It is written to the output file.
976 csrc_pos.p_file = curr_pos.p_file;
977 outsrc(curr_pos.p_file);
981 curr_pos.p_line = (int)ln - 1;
982 if (curr_pos.p_file == csrc_pos.p_file)
983 csrc_pos.p_line = (int)ln - 1;
987 * Handle lint comments. Following comments are currently understood:
989 * CONSTCOND CONSTANTCOND CONSTANTCONDITION
990 * FALLTHRU FALLTHROUGH
999 * If one of this comments is recognized, the arguments, if any, are
1000 * parsed and a function which handles this comment is called.
1011 { "ARGSUSED", 1, argsused },
1012 { "CONSTCOND", 0, constcond },
1013 { "CONSTANTCOND", 0, constcond },
1014 { "CONSTANTCONDITION", 0, constcond },
1015 { "FALLTHRU", 0, fallthru },
1016 { "FALLTHROUGH", 0, fallthru },
1017 { "LINTLIBRARY", 0, lintlib },
1018 { "LINTED", 0, linted },
1019 { "LONGLONG", 0, longlong },
1020 { "NOSTRICT", 0, linted },
1021 { "NOTREACHED", 0, notreach },
1022 { "PRINTFLIKE", 1, printflike },
1023 { "PROTOLIB", 1, protolib },
1024 { "SCANFLIKE", 1, scanflike },
1025 { "VARARGS", 1, varargs },
1034 /* Skip white spaces after the start of the comment */
1035 while ((c = inpc()) != EOF && isspace(c)) ;
1037 /* Read the potential keyword to keywd */
1039 while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1040 keywd[l++] = (char)c;
1045 /* look for the keyword */
1046 for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1047 if (strcmp(keywtab[i].keywd, keywd) == 0)
1050 if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1053 /* skip white spaces after the keyword */
1054 while (c != EOF && isspace(c))
1057 /* read the argument, if the keyword accepts one and there is one */
1059 if (keywtab[i].arg) {
1060 while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1066 a = l != 0 ? atoi(arg) : -1;
1068 /* skip white spaces after the argument */
1069 while (c != EOF && isspace(c))
1072 if (c != '*' || (c = inpc()) != '/') {
1073 if (keywtab[i].func != linted)
1074 /* extra characters in lint comment */
1078 * remember that we have already found the end of the
1084 if (keywtab[i].func != NULL)
1085 (*keywtab[i].func)(a);
1090 if ((c = inpc()) == EOF) {
1091 /* unterminated comment */
1095 if (lc == '*' && c == '/')
1101 * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1102 * clrwflgs() is called after function definitions and global and
1103 * local declarations and definitions. It is also called between
1104 * the controlling expression and the body of control statements
1105 * (if, switch, for, while).
1116 * Strings are stored in a dynamically alloceted buffer and passed
1117 * in yylval.y_xstrg to the parser. The parser or the routines called
1118 * by the parser are responsible for freeing this buffer.
1128 s = xmalloc(max = 64);
1131 while ((c = getescc('"')) >= 0) {
1132 /* +1 to reserve space for a trailing NUL character */
1134 s = xrealloc(s, max *= 2);
1139 /* unterminated string constant */
1142 strg = xcalloc(1, sizeof (strg_t));
1143 strg->st_tspec = CHAR;
1147 yylval.y_strg = strg;
1156 size_t len, max, wlen;
1160 s = xmalloc(max = 64);
1162 while ((c = getescc('"')) >= 0) {
1163 /* +1 to save space for a trailing NUL character */
1165 s = xrealloc(s, max *= 2);
1170 /* unterminated string constant */
1173 /* get length of wide character string */
1174 (void)mblen(NULL, 0);
1175 for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1176 if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1177 /* invalid multibyte character */
1185 ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1187 /* convert from multibyte to wide char */
1188 (void)mbtowc(NULL, NULL, 0);
1189 for (i = 0, wi = 0; i < len; i += n, wi++) {
1190 if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1198 strg = xcalloc(1, sizeof (strg_t));
1199 strg->st_tspec = WCHAR;
1200 strg->st_len = wlen;
1203 yylval.y_strg = strg;
1208 * As noted above the scanner does not create new symbol table entries
1209 * for symbols it cannot find in the symbol table. This is to avoid
1210 * putting undeclared symbols into the symbol table if a syntax error
1213 * getsym() is called as soon as it is probably ok to put the symbol to
1214 * the symbol table. This does not mean that it is not possible that
1215 * symbols are put to the symbol table which are than not completely
1216 * declared due to syntax errors. To avoid too many problems in this
1217 * case symbols get type int in getsym().
1219 * XXX calls to getsym() should be delayed until decl1*() is called
1232 * During member declaration it is possible that name() looked
1233 * for symbols of type FVFT, although it should have looked for
1234 * symbols of type FTAG. Same can happen for labels. Both cases
1235 * are compensated here.
1237 if (symtyp == FMOS || symtyp == FLAB) {
1238 if (sym == NULL || sym->s_kind == FVFT)
1243 if (sym->s_kind != symtyp)
1244 lerror("storesym() 1");
1250 /* create a new symbol table entry */
1252 /* labels must always be allocated at level 1 (outhermost block) */
1253 if (symtyp == FLAB) {
1254 sym = getlblk(1, sizeof (sym_t));
1255 s = getlblk(1, sb->sb_len + 1);
1256 (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1260 while (di->d_nxt != NULL && di->d_nxt->d_nxt != NULL)
1262 if (di->d_ctx != AUTO)
1263 lerror("storesym() 2");
1265 sym = getblk(sizeof (sym_t));
1266 sym->s_name = sb->sb_name;
1267 sym->s_blklev = blklev;
1271 STRUCT_ASSIGN(sym->s_dpos, curr_pos);
1272 if ((sym->s_kind = symtyp) != FLAB)
1273 sym->s_type = gettyp(INT);
1277 if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1278 symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1279 (symtab[sb->sb_hash] = sym)->s_rlink = &symtab[sb->sb_hash];
1281 *di->d_ldlsym = sym;
1282 di->d_ldlsym = &sym->s_dlnxt;
1289 * Remove a symbol forever from the symbol table. s_blklev
1290 * is set to -1 to avoid that the symbol will later be put
1291 * back to the symbol table.
1297 if ((*sym->s_rlink = sym->s_link) != NULL)
1298 sym->s_link->s_rlink = sym->s_rlink;
1304 * Remove a list of symbols declared at one level from the symbol
1313 for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1314 if (sym->s_blklev != -1) {
1315 if ((*sym->s_rlink = sym->s_link) != NULL)
1316 sym->s_link->s_rlink = sym->s_rlink;
1318 sym->s_rlink = NULL;
1324 * Put a symbol into the symbol table
1333 h = hash(sym->s_name);
1334 if ((sym->s_link = symtab[h]) != NULL)
1335 symtab[h]->s_rlink = &sym->s_link;
1336 (symtab[h] = sym)->s_rlink = &symtab[h];
1338 if (sym->s_link != NULL && sym->s_blklev < sym->s_link->s_blklev)
1343 * Called at level 0 after syntax errors
1344 * Removes all symbols which are not declared at level 0 from the
1345 * symbol table. Also frees all memory which is not associated with
1354 for (i = 0; i < HSHSIZ1; i++) {
1355 for (sym = symtab[i]; sym != NULL; sym = nsym) {
1357 if (sym->s_blklev >= 1) {
1358 if ((*sym->s_rlink = nsym) != NULL)
1359 nsym->s_rlink = sym->s_rlink;
1364 for (i = mblklev; i > 0; i--)
1369 * Create a new symbol with the name of an existing symbol.
1378 h = hash(sym->s_name);
1379 nsym = getblk(sizeof (sym_t));
1380 if (sym->s_blklev > blklev)
1381 lerror("pushdown()");
1382 nsym->s_name = sym->s_name;
1383 STRUCT_ASSIGN(nsym->s_dpos, curr_pos);
1384 nsym->s_kind = sym->s_kind;
1385 nsym->s_blklev = blklev;
1387 if ((nsym->s_link = symtab[h]) != NULL)
1388 symtab[h]->s_rlink = &nsym->s_link;
1389 (symtab[h] = nsym)->s_rlink = &symtab[h];
1391 *dcs->d_ldlsym = nsym;
1392 dcs->d_ldlsym = &nsym->s_dlnxt;
1398 * Free any dynamically allocated memory referenced by
1399 * the value stack or yylval.
1400 * The type of information in yylval is described by tok.
1407 if (tok == T_NAME || tok == T_TYPENAME) {
1408 sbuf_t *sb = *(sbuf_t **)sp;
1410 } else if (tok == T_CON) {
1411 val_t *val = *(val_t **)sp;
1413 } else if (tok == T_STRING) {
1414 strg_t *strg = *(strg_t **)sp;
1415 if (strg->st_tspec == CHAR) {
1417 } else if (strg->st_tspec == WCHAR) {
1420 lerror("fryylv() 1");