scan.l revision 1.129 1 1.1 cgd %{
2 1.129 rillig /* $NetBSD: scan.l,v 1.129 2021/01/18 20:02:34 rillig Exp $ */
3 1.2 cgd
4 1.1 cgd /*
5 1.9 cgd * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
6 1.1 cgd * Copyright (c) 1994, 1995 Jochen Pohl
7 1.1 cgd * All Rights Reserved.
8 1.1 cgd *
9 1.1 cgd * Redistribution and use in source and binary forms, with or without
10 1.1 cgd * modification, are permitted provided that the following conditions
11 1.1 cgd * are met:
12 1.1 cgd * 1. Redistributions of source code must retain the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer.
14 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer in the
16 1.1 cgd * documentation and/or other materials provided with the distribution.
17 1.1 cgd * 3. All advertising materials mentioning features or use of this software
18 1.1 cgd * must display the following acknowledgement:
19 1.1 cgd * This product includes software developed by Jochen Pohl for
20 1.1 cgd * The NetBSD Project.
21 1.1 cgd * 4. The name of the author may not be used to endorse or promote products
22 1.1 cgd * derived from this software without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25 1.1 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 1.1 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 1.1 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 1.1 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 1.1 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 1.1 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 1.1 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 1.1 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.11 christos #include <sys/cdefs.h>
37 1.25 tv #if defined(__RCSID) && !defined(lint)
38 1.129 rillig __RCSID("$NetBSD: scan.l,v 1.129 2021/01/18 20:02:34 rillig Exp $");
39 1.1 cgd #endif
40 1.1 cgd
41 1.1 cgd #include <ctype.h>
42 1.1 cgd #include <errno.h>
43 1.94 rillig #include <float.h>
44 1.94 rillig #include <limits.h>
45 1.1 cgd #include <math.h>
46 1.94 rillig #include <stdlib.h>
47 1.94 rillig #include <string.h>
48 1.1 cgd
49 1.1 cgd #include "lint1.h"
50 1.12 tv #include "cgram.h"
51 1.1 cgd
52 1.115 rillig #define CHAR_MASK ((int)(~(~0U << CHAR_SIZE)))
53 1.1 cgd
54 1.105 rillig /* Current position (it's also updated when an included file is parsed) */
55 1.9 cgd pos_t curr_pos = { 1, "", 0 };
56 1.1 cgd
57 1.1 cgd /*
58 1.1 cgd * Current position in C source (not updated when an included file is
59 1.1 cgd * parsed).
60 1.1 cgd */
61 1.9 cgd pos_t csrc_pos = { 1, "", 0 };
62 1.1 cgd
63 1.55 christos /* Are we parsing a gcc attribute? */
64 1.118 rillig bool attron;
65 1.55 christos
66 1.121 rillig bool in_system_header = false;
67 1.121 rillig
68 1.19 lukem static void incline(void);
69 1.19 lukem static void badchar(int);
70 1.19 lukem static sbuf_t *allocsb(void);
71 1.19 lukem static void freesb(sbuf_t *);
72 1.19 lukem static int inpc(void);
73 1.19 lukem static int hash(const char *);
74 1.19 lukem static sym_t *search(sbuf_t *);
75 1.19 lukem static int name(void);
76 1.19 lukem static int keyw(sym_t *);
77 1.19 lukem static int icon(int);
78 1.19 lukem static int fcon(void);
79 1.19 lukem static int operator(int, op_t);
80 1.19 lukem static int ccon(void);
81 1.19 lukem static int wccon(void);
82 1.19 lukem static int getescc(int);
83 1.19 lukem static void directive(void);
84 1.19 lukem static void comment(void);
85 1.19 lukem static void slashslashcomment(void);
86 1.19 lukem static int string(void);
87 1.19 lukem static int wcstrg(void);
88 1.1 cgd
89 1.1 cgd %}
90 1.1 cgd
91 1.62 christos
92 1.1 cgd L [_A-Za-z]
93 1.1 cgd D [0-9]
94 1.1 cgd NZD [1-9]
95 1.62 christos BD [0-1]
96 1.1 cgd OD [0-7]
97 1.1 cgd HD [0-9A-Fa-f]
98 1.1 cgd EX ([eE][+-]?[0-9]+)
99 1.47 christos HX (p[+-]?[0-9A-Fa-f]+)
100 1.47 christos TL ([fFlL]?[i]?)
101 1.1 cgd
102 1.46 christos %option nounput
103 1.46 christos
104 1.1 cgd %%
105 1.1 cgd
106 1.97 rillig {L}({L}|{D})* return name();
107 1.97 rillig 0[bB]{BD}+[lLuU]* return icon(2);
108 1.97 rillig 0{OD}*[lLuU]* return icon(8);
109 1.97 rillig {NZD}{D}*[lLuU]* return icon(10);
110 1.97 rillig 0[xX]{HD}+[lLuU]* return icon(16);
111 1.129 rillig {D}+\.{D}*{EX}?{TL} |
112 1.129 rillig {D}+{EX}{TL} |
113 1.129 rillig 0[xX]{HD}+\.{HD}*{HX}{TL} |
114 1.129 rillig 0[xX]{HD}+{HX}{TL} |
115 1.129 rillig \.{D}+{EX}?{TL} return fcon();
116 1.126 rillig "=" return operator(T_ASSIGN, NOOP);
117 1.127 rillig "*=" return operator(T_OPASSIGN, MULASS);
118 1.127 rillig "/=" return operator(T_OPASSIGN, DIVASS);
119 1.127 rillig "%=" return operator(T_OPASSIGN, MODASS);
120 1.127 rillig "+=" return operator(T_OPASSIGN, ADDASS);
121 1.127 rillig "-=" return operator(T_OPASSIGN, SUBASS);
122 1.127 rillig "<<=" return operator(T_OPASSIGN, SHLASS);
123 1.127 rillig ">>=" return operator(T_OPASSIGN, SHRASS);
124 1.127 rillig "&=" return operator(T_OPASSIGN, ANDASS);
125 1.127 rillig "^=" return operator(T_OPASSIGN, XORASS);
126 1.127 rillig "|=" return operator(T_OPASSIGN, ORASS);
127 1.126 rillig "||" return operator(T_LOGOR, NOOP);
128 1.126 rillig "&&" return operator(T_LOGAND, NOOP);
129 1.127 rillig "|" return operator(T_BITOR, NOOP);
130 1.126 rillig "&" return operator(T_AMPER, NOOP);
131 1.126 rillig "^" return operator(T_XOR, NOOP);
132 1.127 rillig "==" return operator(T_EQUALITY, EQ);
133 1.127 rillig "!=" return operator(T_EQUALITY, NE);
134 1.127 rillig "<" return operator(T_RELATIONAL, LT);
135 1.127 rillig ">" return operator(T_RELATIONAL, GT);
136 1.127 rillig "<=" return operator(T_RELATIONAL, LE);
137 1.127 rillig ">=" return operator(T_RELATIONAL, GE);
138 1.127 rillig "<<" return operator(T_SHIFT, SHL);
139 1.127 rillig ">>" return operator(T_SHIFT, SHR);
140 1.97 rillig "++" return operator(T_INCDEC, INC);
141 1.97 rillig "--" return operator(T_INCDEC, DEC);
142 1.122 rillig "->" return operator(T_MEMBACC, ARROW);
143 1.122 rillig "." return operator(T_MEMBACC, POINT);
144 1.127 rillig "+" return operator(T_ADDITIVE, PLUS);
145 1.127 rillig "-" return operator(T_ADDITIVE, MINUS);
146 1.117 rillig "*" return operator(T_ASTERISK, NOOP);
147 1.127 rillig "/" return operator(T_MULTIPLICATIVE, DIV);
148 1.127 rillig "%" return operator(T_MULTIPLICATIVE, MOD);
149 1.127 rillig "!" return operator(T_UNARY, NOT);
150 1.127 rillig "~" return operator(T_UNARY, COMPL);
151 1.97 rillig "\"" return string();
152 1.97 rillig "L\"" return wcstrg();
153 1.97 rillig ";" return T_SEMI;
154 1.97 rillig "{" return T_LBRACE;
155 1.97 rillig "}" return T_RBRACE;
156 1.97 rillig "," return T_COMMA;
157 1.97 rillig ":" return T_COLON;
158 1.97 rillig "?" return T_QUEST;
159 1.97 rillig "[" return T_LBRACK;
160 1.97 rillig "]" return T_RBRACK;
161 1.108 rillig "(" return T_LPAREN;
162 1.108 rillig ")" return T_RPAREN;
163 1.123 rillig "..." return T_ELLIPSIS;
164 1.97 rillig "'" return ccon();
165 1.97 rillig "L'" return wccon();
166 1.1 cgd ^#.*$ directive();
167 1.1 cgd \n incline();
168 1.1 cgd \t|" "|\f|\v ;
169 1.1 cgd "/*" comment();
170 1.18 lukem "//" slashslashcomment();
171 1.1 cgd . badchar(yytext[0]);
172 1.1 cgd
173 1.1 cgd %%
174 1.1 cgd
175 1.1 cgd static void
176 1.19 lukem incline(void)
177 1.1 cgd {
178 1.1 cgd curr_pos.p_line++;
179 1.9 cgd curr_pos.p_uniq = 0;
180 1.113 rillig #ifdef DEBUG
181 1.113 rillig printf("parsing %s:%d\n", curr_pos.p_file, curr_pos.p_line);
182 1.113 rillig #endif
183 1.9 cgd if (curr_pos.p_file == csrc_pos.p_file) {
184 1.1 cgd csrc_pos.p_line++;
185 1.9 cgd csrc_pos.p_uniq = 0;
186 1.9 cgd }
187 1.1 cgd }
188 1.1 cgd
189 1.1 cgd static void
190 1.19 lukem badchar(int c)
191 1.1 cgd {
192 1.19 lukem
193 1.1 cgd /* unknown character \%o */
194 1.1 cgd error(250, c);
195 1.1 cgd }
196 1.1 cgd
197 1.1 cgd /*
198 1.1 cgd * Keywords.
199 1.1 cgd * During initialisation they are written to the symbol table.
200 1.1 cgd */
201 1.1 cgd static struct kwtab {
202 1.1 cgd const char *kw_name; /* keyword */
203 1.1 cgd int kw_token; /* token returned by yylex() */
204 1.1 cgd scl_t kw_scl; /* storage class if kw_token T_SCLASS */
205 1.128 rillig tspec_t kw_tspec; /* type spec. if kw_token
206 1.128 rillig * T_TYPE or T_STRUCT_OR_UNION */
207 1.1 cgd tqual_t kw_tqual; /* type qual. fi kw_token T_QUAL */
208 1.110 rillig bool kw_c89 : 1; /* C89 keyword */
209 1.110 rillig bool kw_c99 : 1; /* C99 keyword */
210 1.110 rillig bool kw_gcc : 1; /* GCC keyword */
211 1.110 rillig bool kw_attr : 1; /* GCC attribute, keyword */
212 1.98 rillig u_int kw_deco : 3; /* 1 = name, 2 = __name, 4 = __name__ */
213 1.1 cgd } kwtab[] = {
214 1.83 christos #ifdef INT128_SIZE
215 1.83 christos { "__int128_t", T_TYPE, 0, INT128, 0, 0,1,0,0,1 },
216 1.83 christos { "__uint128_t",T_TYPE, 0, UINT128,0, 0,1,0,0,1 },
217 1.83 christos #endif
218 1.90 christos { "__thread", T_QUAL, 0, 0, THREAD, 0,0,1,0,1 },
219 1.91 christos { "_Alignof", T_ALIGNOF, 0, 0, 0, 0,0,0,0,1 },
220 1.73 christos { "_Bool", T_TYPE, 0, BOOL, 0, 0,1,0,0,1 },
221 1.73 christos { "_Complex", T_TYPE, 0, COMPLEX,0, 0,1,0,0,1 },
222 1.73 christos { "_Generic", T_GENERIC, 0, 0, 0, 0,1,0,0,1 },
223 1.80 christos { "_Noreturn", T_NORETURN, 0, 0, 0, 0,1,0,0,1 },
224 1.90 christos { "_Thread_local",T_QUAL, 0, 0, THREAD, 0,1,0,0,1 },
225 1.73 christos { "alias", T_AT_ALIAS, 0, 0, 0, 0,0,1,1,5 },
226 1.73 christos { "aligned", T_AT_ALIGNED, 0, 0, 0, 0,0,1,1,5 },
227 1.73 christos { "alignof", T_ALIGNOF, 0, 0, 0, 0,0,0,0,4 },
228 1.87 christos { "alloc_size", T_AT_ALLOC_SIZE,0, 0, 0, 0,0,1,1,5 },
229 1.73 christos { "always_inline", T_AT_ALWAYS_INLINE, 0,0, 0, 0,0,1,1,5 },
230 1.73 christos { "asm", T_ASM, 0, 0, 0, 0,0,1,0,7 },
231 1.73 christos { "attribute", T_ATTRIBUTE, 0, 0, 0, 0,0,1,0,6 },
232 1.73 christos { "auto", T_SCLASS, AUTO, 0, 0, 0,0,0,0,1 },
233 1.75 christos { "bounded", T_AT_BOUNDED, 0, 0, 0, 0,0,1,1,5 },
234 1.73 christos { "break", T_BREAK, 0, 0, 0, 0,0,0,0,1 },
235 1.76 christos { "buffer", T_AT_BUFFER, 0, 0, 0, 0,0,1,1,5 },
236 1.79 christos { "builtin_offsetof", T_BUILTIN_OFFSETOF, 0, 0, 0, 0,0,1,0,2 },
237 1.73 christos { "case", T_CASE, 0, 0, 0, 0,0,0,0,1 },
238 1.73 christos { "char", T_TYPE, 0, CHAR, 0, 0,0,0,0,1 },
239 1.73 christos { "cold", T_AT_COLD, 0, 0, 0, 0,0,1,1,5 },
240 1.73 christos { "const", T_QUAL, 0, 0, CONST, 1,0,0,0,7 },
241 1.73 christos { "constructor",T_AT_CONSTRUCTOR,0, 0, 0, 0,0,1,1,5 },
242 1.73 christos { "continue", T_CONTINUE, 0, 0, 0, 0,0,0,0,1 },
243 1.73 christos { "default", T_DEFAULT, 0, 0, 0, 0,0,0,0,1 },
244 1.73 christos { "deprecated", T_AT_DEPRECATED,0, 0, 0, 0,0,1,1,5 },
245 1.86 christos { "destructor", T_AT_DESTRUCTOR,0, 0, 0, 0,0,1,1,5 },
246 1.73 christos { "do", T_DO, 0, 0, 0, 0,0,0,0,1 },
247 1.73 christos { "double", T_TYPE, 0, DOUBLE, 0, 0,0,0,0,1 },
248 1.73 christos { "else", T_ELSE, 0, 0, 0, 0,0,0,0,1 },
249 1.73 christos { "enum", T_ENUM, 0, 0, 0, 0,0,0,0,1 },
250 1.73 christos { "extension", T_EXTENSION, 0, 0, 0, 0,0,1,0,4 },
251 1.73 christos { "extern", T_SCLASS, EXTERN, 0, 0, 0,0,0,0,1 },
252 1.73 christos { "float", T_TYPE, 0, FLOAT, 0, 0,0,0,0,1 },
253 1.73 christos { "for", T_FOR, 0, 0, 0, 0,0,0,0,1 },
254 1.73 christos { "format", T_AT_FORMAT, 0, 0, 0, 0,0,1,1,5 },
255 1.73 christos { "format_arg", T_AT_FORMAT_ARG,0, 0, 0, 0,0,1,1,5 },
256 1.73 christos { "gnu_inline", T_AT_GNU_INLINE,0, 0, 0, 0,0,1,1,5 },
257 1.88 christos { "gnu_printf", T_AT_FORMAT_GNU_PRINTF,0,0, 0, 0,0,1,1,5 },
258 1.73 christos { "goto", T_GOTO, 0, 0, 0, 0,0,0,0,1 },
259 1.73 christos { "if", T_IF, 0, 0, 0, 0,0,0,0,1 },
260 1.73 christos { "imag", T_IMAG, 0, 0, 0, 0,1,0,0,4 },
261 1.73 christos { "inline", T_SCLASS, INLINE, 0, 0, 0,1,0,0,7 },
262 1.73 christos { "int", T_TYPE, 0, INT, 0, 0,0,0,0,1 },
263 1.73 christos { "long", T_TYPE, 0, LONG, 0, 0,0,0,0,1 },
264 1.87 christos { "malloc", T_AT_MALLOC, 0, 0, 0, 0,0,1,1,5 },
265 1.73 christos { "may_alias", T_AT_MAY_ALIAS, 0, 0, 0, 0,0,1,1,5 },
266 1.75 christos { "minbytes", T_AT_MINBYTES, 0, 0, 0, 0,0,1,1,5 },
267 1.73 christos { "mode", T_AT_MODE, 0, 0, 0, 0,0,1,1,5 },
268 1.69 christos { "no_instrument_function", T_AT_NO_INSTRUMENT_FUNCTION,
269 1.73 christos 0, 0, 0, 0,0,1,1,5 },
270 1.75 christos { "nonnull", T_AT_NONNULL, 0, 0, 0, 0,0,1,1,5 },
271 1.82 christos { "noinline", T_AT_NOINLINE, 0, 0, 0, 0,0,1,1,5 },
272 1.73 christos { "noreturn", T_AT_NORETURN, 0, 0, 0, 0,0,1,1,5 },
273 1.87 christos { "nothrow", T_AT_NOTHROW, 0, 0, 0, 0,0,1,1,5 },
274 1.92 christos { "optimize", T_AT_OPTIMIZE, 0, 0, 0, 0,0,1,1,5 },
275 1.73 christos { "packed", T_AT_PACKED, 0, 0, 0, 0,0,1,1,5 },
276 1.73 christos { "packed", T_PACKED, 0, 0, 0, 0,0,0,0,2 },
277 1.77 christos { "pcs", T_AT_PCS, 0, 0, 0, 0,0,0,0,5 },
278 1.73 christos { "printf", T_AT_FORMAT_PRINTF,0, 0, 0, 0,0,1,1,5 },
279 1.73 christos { "pure", T_AT_PURE, 0, 0, 0, 0,0,1,1,5 },
280 1.73 christos { "real", T_REAL, 0, 0, 0, 0,1,0,0,4 },
281 1.73 christos { "register", T_SCLASS, REG, 0, 0, 0,0,0,0,1 },
282 1.73 christos { "restrict", T_QUAL, 0, 0, RESTRICT, 0,1,0,0,5 },
283 1.73 christos { "return", T_RETURN, 0, 0, 0, 0,0,0,0,1 },
284 1.73 christos { "returns_twice", T_AT_RETURNS_TWICE,0,0, 0, 0,0,1,1,5 },
285 1.73 christos { "scanf", T_AT_FORMAT_SCANF,0, 0, 0, 0,0,1,1,5 },
286 1.73 christos { "section", T_AT_SECTION, 0, 0, 0, 0,0,1,1,7 },
287 1.73 christos { "sentinel", T_AT_SENTINEL, 0, 0, 0, 0,0,1,1,5 },
288 1.73 christos { "short", T_TYPE, 0, SHORT, 0, 0,0,0,0,1 },
289 1.73 christos { "signed", T_TYPE, 0, SIGNED, 0, 1,0,0,0,3 },
290 1.73 christos { "sizeof", T_SIZEOF, 0, 0, 0, 0,0,0,0,1 },
291 1.73 christos { "static", T_SCLASS, STATIC, 0, 0, 0,0,0,0,1 },
292 1.73 christos { "strfmon", T_AT_FORMAT_STRFMON,0, 0, 0, 0,0,1,1,5 },
293 1.73 christos { "strftime", T_AT_FORMAT_STRFTIME,0, 0, 0, 0,0,1,1,5 },
294 1.75 christos { "string", T_AT_STRING, 0, 0, 0, 0,0,1,1,5 },
295 1.128 rillig { "struct", T_STRUCT_OR_UNION, 0, STRUCT, 0, 0,0,0,0,1 },
296 1.73 christos { "switch", T_SWITCH, 0, 0, 0, 0,0,0,0,1 },
297 1.89 christos { "symbolrename", T_SYMBOLRENAME,0, 0, 0, 0,0,0,0,2 },
298 1.81 christos { "syslog", T_AT_FORMAT_SYSLOG,0, 0, 0, 0,0,1,1,5 },
299 1.73 christos { "transparent_union",T_AT_TUNION,0, 0, 0, 0,0,1,1,5 },
300 1.89 christos { "tls_model", T_AT_TLS_MODEL, 0, 0, 0, 0,0,1,1,5 },
301 1.73 christos { "typedef", T_SCLASS, TYPEDEF, 0, 0, 0,0,0,0,1 },
302 1.74 christos { "typeof", T_TYPEOF, 0, 0, 0, 0,0,1,0,7 },
303 1.128 rillig { "union", T_STRUCT_OR_UNION, 0, UNION, 0, 0,0,0,0,1 },
304 1.73 christos { "unsigned", T_TYPE, 0, UNSIGN, 0, 0,0,0,0,1 },
305 1.73 christos { "unused", T_AT_UNUSED, 0, 0, 0, 0,0,1,1,5 },
306 1.73 christos { "used", T_AT_USED, 0, 0, 0, 0,0,1,1,5 },
307 1.73 christos { "visibility", T_AT_VISIBILITY,0, 0, 0, 0,0,1,1,5 },
308 1.73 christos { "void", T_TYPE, 0, VOID, 0, 0,0,0,0,1 },
309 1.73 christos { "volatile", T_QUAL, 0, 0, VOLATILE, 1,0,0,0,7 },
310 1.85 christos { "warn_unused_result", T_AT_WARN_UNUSED_RESULT, 0, 0, 0, 0,0,1,1,5 },
311 1.73 christos { "weak", T_AT_WEAK, 0, 0, 0, 0,0,1,1,5 },
312 1.73 christos { "while", T_WHILE, 0, 0, 0, 0,0,0,0,1 },
313 1.73 christos { NULL, 0, 0, 0, 0, 0,0,0,0,0 }
314 1.1 cgd };
315 1.1 cgd
316 1.1 cgd /* Symbol table */
317 1.1 cgd static sym_t *symtab[HSHSIZ1];
318 1.1 cgd
319 1.1 cgd /* bit i of the entry with index i is set */
320 1.115 rillig uint64_t qbmasks[64];
321 1.1 cgd
322 1.1 cgd /* least significant i bits are set in the entry with index i */
323 1.115 rillig uint64_t qlmasks[64 + 1];
324 1.1 cgd
325 1.1 cgd /* least significant i bits are not set in the entry with index i */
326 1.115 rillig uint64_t qumasks[64 + 1];
327 1.1 cgd
328 1.1 cgd /* free list for sbuf structures */
329 1.1 cgd static sbuf_t *sbfrlst;
330 1.1 cgd
331 1.129 rillig /* type of next expected symbol */
332 1.1 cgd symt_t symtyp;
333 1.1 cgd
334 1.1 cgd
335 1.73 christos static void
336 1.98 rillig add_keyword(struct kwtab *kw, int deco)
337 1.73 christos {
338 1.73 christos sym_t *sym;
339 1.73 christos size_t h;
340 1.73 christos char buf[256];
341 1.73 christos const char *name;
342 1.73 christos
343 1.73 christos if (!(kw->kw_deco & deco))
344 1.73 christos return;
345 1.73 christos
346 1.73 christos switch (deco) {
347 1.73 christos case 1:
348 1.73 christos name = kw->kw_name;
349 1.73 christos break;
350 1.73 christos case 2:
351 1.73 christos snprintf(buf, sizeof(buf), "__%s", kw->kw_name);
352 1.73 christos name = strdup(buf);
353 1.73 christos break;
354 1.125 rillig default:
355 1.125 rillig lint_assert(deco == 4);
356 1.73 christos snprintf(buf, sizeof(buf), "__%s__", kw->kw_name);
357 1.73 christos name = strdup(buf);
358 1.73 christos break;
359 1.73 christos }
360 1.73 christos
361 1.73 christos if (name == NULL)
362 1.73 christos err(1, "Can't init symbol table");
363 1.95 rillig
364 1.73 christos sym = getblk(sizeof (sym_t));
365 1.73 christos sym->s_name = name;
366 1.103 rillig sym->s_keyword = kw;
367 1.73 christos sym->s_value.v_quad = kw->kw_token;
368 1.128 rillig if (kw->kw_token == T_TYPE || kw->kw_token == T_STRUCT_OR_UNION) {
369 1.73 christos sym->s_tspec = kw->kw_tspec;
370 1.73 christos } else if (kw->kw_token == T_SCLASS) {
371 1.73 christos sym->s_scl = kw->kw_scl;
372 1.73 christos } else if (kw->kw_token == T_QUAL) {
373 1.73 christos sym->s_tqual = kw->kw_tqual;
374 1.73 christos }
375 1.73 christos h = hash(sym->s_name);
376 1.73 christos if ((sym->s_link = symtab[h]) != NULL)
377 1.73 christos symtab[h]->s_rlink = &sym->s_link;
378 1.73 christos sym->s_rlink = &symtab[h];
379 1.73 christos symtab[h] = sym;
380 1.73 christos }
381 1.73 christos
382 1.1 cgd /*
383 1.1 cgd * All keywords are written to the symbol table. This saves us looking
384 1.1 cgd * in a extra table for each name we found.
385 1.1 cgd */
386 1.1 cgd void
387 1.19 lukem initscan(void)
388 1.1 cgd {
389 1.1 cgd struct kwtab *kw;
390 1.73 christos size_t i;
391 1.27 thorpej uint64_t uq;
392 1.1 cgd
393 1.1 cgd for (kw = kwtab; kw->kw_name != NULL; kw++) {
394 1.32 perry if ((kw->kw_c89 || kw->kw_c99) && tflag)
395 1.32 perry continue;
396 1.32 perry if (kw->kw_c99 && !(Sflag || gflag))
397 1.5 jpo continue;
398 1.5 jpo if (kw->kw_gcc && !gflag)
399 1.5 jpo continue;
400 1.98 rillig add_keyword(kw, 1);
401 1.98 rillig add_keyword(kw, 2);
402 1.98 rillig add_keyword(kw, 4);
403 1.1 cgd }
404 1.1 cgd
405 1.1 cgd /* initialize bit-masks for quads */
406 1.115 rillig for (i = 0; i < 64; i++) {
407 1.27 thorpej qbmasks[i] = (uint64_t)1 << i;
408 1.27 thorpej uq = ~(uint64_t)0 << i;
409 1.1 cgd qumasks[i] = uq;
410 1.1 cgd qlmasks[i] = ~uq;
411 1.1 cgd }
412 1.1 cgd qumasks[i] = 0;
413 1.27 thorpej qlmasks[i] = ~(uint64_t)0;
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd /*
417 1.1 cgd * Get a free sbuf structure, if possible from the free list
418 1.1 cgd */
419 1.1 cgd static sbuf_t *
420 1.19 lukem allocsb(void)
421 1.1 cgd {
422 1.1 cgd sbuf_t *sb;
423 1.1 cgd
424 1.1 cgd if ((sb = sbfrlst) != NULL) {
425 1.100 rillig sbfrlst = sb->sb_next;
426 1.55 christos #ifdef BLKDEBUG
427 1.55 christos (void)memset(sb, 0, sizeof (*sb));
428 1.55 christos #else
429 1.100 rillig sb->sb_next = NULL;
430 1.55 christos #endif
431 1.1 cgd } else {
432 1.1 cgd sb = xmalloc(sizeof (sbuf_t));
433 1.55 christos (void)memset(sb, 0, sizeof (*sb));
434 1.1 cgd }
435 1.97 rillig return sb;
436 1.1 cgd }
437 1.1 cgd
438 1.1 cgd /*
439 1.1 cgd * Put a sbuf structure to the free list
440 1.1 cgd */
441 1.1 cgd static void
442 1.19 lukem freesb(sbuf_t *sb)
443 1.1 cgd {
444 1.19 lukem
445 1.55 christos (void)memset(sb, ZERO, sizeof (*sb));
446 1.100 rillig sb->sb_next = sbfrlst;
447 1.1 cgd sbfrlst = sb;
448 1.1 cgd }
449 1.1 cgd
450 1.1 cgd /*
451 1.1 cgd * Read a character and ensure that it is positive (except EOF).
452 1.1 cgd * Increment line count(s) if necessary.
453 1.1 cgd */
454 1.1 cgd static int
455 1.19 lukem inpc(void)
456 1.1 cgd {
457 1.1 cgd int c;
458 1.1 cgd
459 1.1 cgd if ((c = input()) != EOF && (c &= CHAR_MASK) == '\n')
460 1.1 cgd incline();
461 1.97 rillig return c;
462 1.1 cgd }
463 1.1 cgd
464 1.1 cgd static int
465 1.19 lukem hash(const char *s)
466 1.1 cgd {
467 1.1 cgd u_int v;
468 1.1 cgd const u_char *us;
469 1.1 cgd
470 1.1 cgd v = 0;
471 1.1 cgd for (us = (const u_char *)s; *us != '\0'; us++) {
472 1.1 cgd v = (v << sizeof (v)) + *us;
473 1.1 cgd v ^= v >> (sizeof (v) * CHAR_BIT - sizeof (v));
474 1.1 cgd }
475 1.97 rillig return v % HSHSIZ1;
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd /*
479 1.1 cgd * Lex has found a letter followed by zero or more letters or digits.
480 1.1 cgd * It looks for a symbol in the symbol table with the same name. This
481 1.1 cgd * symbol must either be a keyword or a symbol of the type required by
482 1.1 cgd * symtyp (label, member, tag, ...).
483 1.1 cgd *
484 1.1 cgd * If it is a keyword, the token is returned. In some cases it is described
485 1.1 cgd * more deeply by data written to yylval.
486 1.1 cgd *
487 1.1 cgd * If it is a symbol, T_NAME is returned and the pointer to a sbuf struct
488 1.60 snj * is stored in yylval. This struct contains the name of the symbol, its
489 1.1 cgd * length and hash value. If there is already a symbol of the same name
490 1.1 cgd * and type in the symbol table, the sbuf struct also contains a pointer
491 1.1 cgd * to the symbol table entry.
492 1.1 cgd */
493 1.1 cgd static int
494 1.19 lukem name(void)
495 1.1 cgd {
496 1.1 cgd char *s;
497 1.1 cgd sbuf_t *sb;
498 1.1 cgd sym_t *sym;
499 1.1 cgd int tok;
500 1.1 cgd
501 1.1 cgd sb = allocsb();
502 1.1 cgd sb->sb_name = yytext;
503 1.1 cgd sb->sb_len = yyleng;
504 1.1 cgd sb->sb_hash = hash(yytext);
505 1.103 rillig if ((sym = search(sb)) != NULL && sym->s_keyword) {
506 1.56 christos freesb(sb);
507 1.97 rillig return keyw(sym);
508 1.1 cgd }
509 1.1 cgd
510 1.1 cgd sb->sb_sym = sym;
511 1.1 cgd
512 1.1 cgd if (sym != NULL) {
513 1.107 rillig lint_assert(blklev >= sym->s_blklev);
514 1.1 cgd sb->sb_name = sym->s_name;
515 1.1 cgd sb->sb_len = strlen(sym->s_name);
516 1.1 cgd tok = sym->s_scl == TYPEDEF ? T_TYPENAME : T_NAME;
517 1.1 cgd } else {
518 1.1 cgd s = getblk(yyleng + 1);
519 1.1 cgd (void)memcpy(s, yytext, yyleng + 1);
520 1.1 cgd sb->sb_name = s;
521 1.1 cgd sb->sb_len = yyleng;
522 1.1 cgd tok = T_NAME;
523 1.1 cgd }
524 1.1 cgd
525 1.1 cgd yylval.y_sb = sb;
526 1.97 rillig return tok;
527 1.1 cgd }
528 1.1 cgd
529 1.1 cgd static sym_t *
530 1.19 lukem search(sbuf_t *sb)
531 1.1 cgd {
532 1.1 cgd sym_t *sym;
533 1.1 cgd
534 1.1 cgd for (sym = symtab[sb->sb_hash]; sym != NULL; sym = sym->s_link) {
535 1.1 cgd if (strcmp(sym->s_name, sb->sb_name) == 0) {
536 1.103 rillig if (sym->s_keyword) {
537 1.103 rillig struct kwtab *kw = sym->s_keyword;
538 1.56 christos if (!kw->kw_attr || attron)
539 1.97 rillig return sym;
540 1.72 christos } else if (!attron && sym->s_kind == symtyp)
541 1.56 christos return sym;
542 1.1 cgd }
543 1.1 cgd }
544 1.1 cgd
545 1.97 rillig return NULL;
546 1.1 cgd }
547 1.19 lukem
548 1.1 cgd static int
549 1.19 lukem keyw(sym_t *sym)
550 1.1 cgd {
551 1.1 cgd int t;
552 1.1 cgd
553 1.1 cgd if ((t = (int)sym->s_value.v_quad) == T_SCLASS) {
554 1.1 cgd yylval.y_scl = sym->s_scl;
555 1.128 rillig } else if (t == T_TYPE || t == T_STRUCT_OR_UNION) {
556 1.1 cgd yylval.y_tspec = sym->s_tspec;
557 1.1 cgd } else if (t == T_QUAL) {
558 1.1 cgd yylval.y_tqual = sym->s_tqual;
559 1.1 cgd }
560 1.97 rillig return t;
561 1.1 cgd }
562 1.1 cgd
563 1.1 cgd /*
564 1.1 cgd * Convert a string representing an integer into internal representation.
565 1.1 cgd * The value is returned in yylval. icon() (and yylex()) returns T_CON.
566 1.1 cgd */
567 1.1 cgd static int
568 1.19 lukem icon(int base)
569 1.1 cgd {
570 1.1 cgd int l_suffix, u_suffix;
571 1.1 cgd int len;
572 1.64 christos const char *cp;
573 1.1 cgd char c, *eptr;
574 1.1 cgd tspec_t typ;
575 1.118 rillig bool ansiu;
576 1.84 christos #ifdef TARG_INT128_MAX
577 1.83 christos __uint128_t uq = 0;
578 1.83 christos static tspec_t contypes[2][4] = {
579 1.83 christos { INT, LONG, QUAD, INT128, },
580 1.83 christos { UINT, ULONG, UQUAD, UINT128, }
581 1.83 christos };
582 1.83 christos #else
583 1.27 thorpej uint64_t uq = 0;
584 1.1 cgd static tspec_t contypes[2][3] = {
585 1.83 christos { INT, LONG, QUAD, },
586 1.83 christos { UINT, ULONG, UQUAD, }
587 1.1 cgd };
588 1.83 christos #endif
589 1.1 cgd
590 1.1 cgd cp = yytext;
591 1.1 cgd len = yyleng;
592 1.1 cgd
593 1.62 christos /* skip 0[xX] or 0[bB] */
594 1.62 christos if (base == 16 || base == 2) {
595 1.1 cgd cp += 2;
596 1.1 cgd len -= 2;
597 1.1 cgd }
598 1.1 cgd
599 1.1 cgd /* read suffixes */
600 1.1 cgd l_suffix = u_suffix = 0;
601 1.129 rillig for (;;) {
602 1.1 cgd if ((c = cp[len - 1]) == 'l' || c == 'L') {
603 1.1 cgd l_suffix++;
604 1.1 cgd } else if (c == 'u' || c == 'U') {
605 1.1 cgd u_suffix++;
606 1.1 cgd } else {
607 1.1 cgd break;
608 1.1 cgd }
609 1.1 cgd len--;
610 1.1 cgd }
611 1.1 cgd if (l_suffix > 2 || u_suffix > 1) {
612 1.1 cgd /* malformed integer constant */
613 1.1 cgd warning(251);
614 1.1 cgd if (l_suffix > 2)
615 1.1 cgd l_suffix = 2;
616 1.1 cgd if (u_suffix > 1)
617 1.1 cgd u_suffix = 1;
618 1.1 cgd }
619 1.1 cgd if (tflag && u_suffix != 0) {
620 1.1 cgd /* suffix U is illegal in traditional C */
621 1.1 cgd warning(97);
622 1.1 cgd }
623 1.1 cgd typ = contypes[u_suffix][l_suffix];
624 1.1 cgd
625 1.1 cgd errno = 0;
626 1.35 he
627 1.64 christos uq = strtouq(cp, &eptr, base);
628 1.107 rillig lint_assert(eptr == cp + len);
629 1.1 cgd if (errno != 0)
630 1.1 cgd /* integer constant out of range */
631 1.1 cgd warning(252);
632 1.1 cgd
633 1.1 cgd /*
634 1.35 he * If the value is too big for the current type, we must choose
635 1.1 cgd * another type.
636 1.1 cgd */
637 1.118 rillig ansiu = false;
638 1.1 cgd switch (typ) {
639 1.1 cgd case INT:
640 1.35 he if (uq <= TARG_INT_MAX) {
641 1.1 cgd /* ok */
642 1.35 he } else if (uq <= TARG_UINT_MAX && base != 10) {
643 1.1 cgd typ = UINT;
644 1.35 he } else if (uq <= TARG_LONG_MAX) {
645 1.1 cgd typ = LONG;
646 1.1 cgd } else {
647 1.1 cgd typ = ULONG;
648 1.35 he if (uq > TARG_ULONG_MAX) {
649 1.35 he /* integer constant out of range */
650 1.35 he warning(252);
651 1.35 he }
652 1.1 cgd }
653 1.1 cgd if (typ == UINT || typ == ULONG) {
654 1.1 cgd if (tflag) {
655 1.1 cgd typ = LONG;
656 1.1 cgd } else if (!sflag) {
657 1.1 cgd /*
658 1.1 cgd * Remember that the constant is unsigned
659 1.1 cgd * only in ANSI C
660 1.1 cgd */
661 1.118 rillig ansiu = true;
662 1.1 cgd }
663 1.1 cgd }
664 1.1 cgd break;
665 1.1 cgd case UINT:
666 1.35 he if (uq > TARG_UINT_MAX) {
667 1.1 cgd typ = ULONG;
668 1.35 he if (uq > TARG_ULONG_MAX) {
669 1.35 he /* integer constant out of range */
670 1.35 he warning(252);
671 1.35 he }
672 1.35 he }
673 1.1 cgd break;
674 1.1 cgd case LONG:
675 1.35 he if (uq > TARG_LONG_MAX && !tflag) {
676 1.1 cgd typ = ULONG;
677 1.1 cgd if (!sflag)
678 1.118 rillig ansiu = true;
679 1.35 he if (uq > TARG_ULONG_MAX) {
680 1.35 he /* integer constant out of range */
681 1.35 he warning(252);
682 1.35 he }
683 1.1 cgd }
684 1.1 cgd break;
685 1.39 joerg case ULONG:
686 1.39 joerg if (uq > TARG_ULONG_MAX) {
687 1.39 joerg /* integer constant out of range */
688 1.39 joerg warning(252);
689 1.39 joerg }
690 1.39 joerg break;
691 1.1 cgd case QUAD:
692 1.35 he if (uq > TARG_QUAD_MAX && !tflag) {
693 1.1 cgd typ = UQUAD;
694 1.1 cgd if (!sflag)
695 1.118 rillig ansiu = true;
696 1.1 cgd }
697 1.1 cgd break;
698 1.39 joerg case UQUAD:
699 1.39 joerg if (uq > TARG_UQUAD_MAX) {
700 1.39 joerg /* integer constant out of range */
701 1.39 joerg warning(252);
702 1.39 joerg }
703 1.39 joerg break;
704 1.83 christos #ifdef INT128_SIZE
705 1.83 christos case INT128:
706 1.84 christos #ifdef TARG_INT128_MAX
707 1.83 christos if (uq > TARG_INT128_MAX && !tflag) {
708 1.83 christos typ = UINT128;
709 1.83 christos if (!sflag)
710 1.118 rillig ansiu = true;
711 1.83 christos }
712 1.84 christos #endif
713 1.83 christos break;
714 1.83 christos case UINT128:
715 1.84 christos #ifdef TARG_INT128_MAX
716 1.83 christos if (uq > TARG_UINT128_MAX) {
717 1.83 christos /* integer constant out of range */
718 1.83 christos warning(252);
719 1.83 christos }
720 1.84 christos #endif
721 1.83 christos break;
722 1.83 christos #endif
723 1.52 christos /* LINTED206: (enumeration values not handled in switch) */
724 1.11 christos case STRUCT:
725 1.11 christos case VOID:
726 1.11 christos case LDOUBLE:
727 1.11 christos case FUNC:
728 1.11 christos case ARRAY:
729 1.11 christos case PTR:
730 1.11 christos case ENUM:
731 1.11 christos case UNION:
732 1.11 christos case SIGNED:
733 1.11 christos case NOTSPEC:
734 1.11 christos case DOUBLE:
735 1.11 christos case FLOAT:
736 1.11 christos case USHORT:
737 1.11 christos case SHORT:
738 1.11 christos case UCHAR:
739 1.11 christos case SCHAR:
740 1.11 christos case CHAR:
741 1.33 yamt case BOOL:
742 1.11 christos case UNSIGN:
743 1.38 christos case FCOMPLEX:
744 1.38 christos case DCOMPLEX:
745 1.40 matt case LCOMPLEX:
746 1.38 christos case COMPLEX:
747 1.23 thorpej break;
748 1.1 cgd }
749 1.1 cgd
750 1.27 thorpej uq = (uint64_t)xsign((int64_t)uq, typ, -1);
751 1.1 cgd
752 1.1 cgd (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
753 1.1 cgd yylval.y_val->v_ansiu = ansiu;
754 1.27 thorpej yylval.y_val->v_quad = (int64_t)uq;
755 1.1 cgd
756 1.97 rillig return T_CON;
757 1.1 cgd }
758 1.1 cgd
759 1.1 cgd /*
760 1.1 cgd * Returns 1 if t is a signed type and the value is negative.
761 1.1 cgd *
762 1.1 cgd * len is the number of significant bits. If len is -1, len is set
763 1.1 cgd * to the width of type t.
764 1.1 cgd */
765 1.1 cgd int
766 1.27 thorpej sign(int64_t q, tspec_t t, int len)
767 1.1 cgd {
768 1.19 lukem
769 1.116 rillig if (t == PTR || is_uinteger(t))
770 1.97 rillig return 0;
771 1.97 rillig return msb(q, t, len);
772 1.1 cgd }
773 1.1 cgd
774 1.1 cgd int
775 1.27 thorpej msb(int64_t q, tspec_t t, int len)
776 1.1 cgd {
777 1.19 lukem
778 1.1 cgd if (len <= 0)
779 1.1 cgd len = size(t);
780 1.97 rillig return (q & qbmasks[len - 1]) != 0;
781 1.1 cgd }
782 1.1 cgd
783 1.1 cgd /*
784 1.1 cgd * Extends the sign of q.
785 1.1 cgd */
786 1.27 thorpej int64_t
787 1.27 thorpej xsign(int64_t q, tspec_t t, int len)
788 1.1 cgd {
789 1.19 lukem
790 1.1 cgd if (len <= 0)
791 1.1 cgd len = size(t);
792 1.1 cgd
793 1.116 rillig if (t == PTR || is_uinteger(t) || !sign(q, t, len)) {
794 1.1 cgd q &= qlmasks[len];
795 1.1 cgd } else {
796 1.1 cgd q |= qumasks[len];
797 1.1 cgd }
798 1.97 rillig return q;
799 1.1 cgd }
800 1.1 cgd
801 1.1 cgd /*
802 1.105 rillig * Convert a string representing a floating point value into its integral
803 1.1 cgd * representation. Type and value are returned in yylval. fcon()
804 1.1 cgd * (and yylex()) returns T_CON.
805 1.1 cgd * XXX Currently it is not possible to convert constants of type
806 1.20 wiz * long double which are greater than DBL_MAX.
807 1.1 cgd */
808 1.1 cgd static int
809 1.19 lukem fcon(void)
810 1.1 cgd {
811 1.1 cgd const char *cp;
812 1.1 cgd int len;
813 1.1 cgd tspec_t typ;
814 1.1 cgd char c, *eptr;
815 1.1 cgd double d;
816 1.11 christos float f = 0;
817 1.1 cgd
818 1.1 cgd cp = yytext;
819 1.1 cgd len = yyleng;
820 1.1 cgd
821 1.38 christos if (cp[len - 1] == 'i') {
822 1.38 christos /* imaginary, do nothing for now */
823 1.38 christos len--;
824 1.38 christos }
825 1.1 cgd if ((c = cp[len - 1]) == 'f' || c == 'F') {
826 1.1 cgd typ = FLOAT;
827 1.1 cgd len--;
828 1.1 cgd } else if (c == 'l' || c == 'L') {
829 1.1 cgd typ = LDOUBLE;
830 1.1 cgd len--;
831 1.1 cgd } else {
832 1.95 rillig if (c == 'd' || c == 'D')
833 1.49 christos len--;
834 1.1 cgd typ = DOUBLE;
835 1.1 cgd }
836 1.1 cgd
837 1.1 cgd if (tflag && typ != DOUBLE) {
838 1.1 cgd /* suffixes F and L are illegal in traditional C */
839 1.1 cgd warning(98);
840 1.1 cgd }
841 1.1 cgd
842 1.1 cgd errno = 0;
843 1.1 cgd d = strtod(cp, &eptr);
844 1.37 he if (eptr != cp + len) {
845 1.37 he switch (*eptr) {
846 1.37 he /*
847 1.37 he * XXX: non-native non-current strtod() may not handle hex
848 1.37 he * floats, ignore the rest if we find traces of hex float
849 1.37 he * syntax...
850 1.37 he */
851 1.37 he case 'p':
852 1.37 he case 'P':
853 1.37 he case 'x':
854 1.37 he case 'X':
855 1.37 he d = 0;
856 1.37 he errno = 0;
857 1.37 he break;
858 1.37 he default:
859 1.49 christos LERROR("fcon(%s->%s)", cp, eptr);
860 1.37 he }
861 1.37 he }
862 1.1 cgd if (errno != 0)
863 1.1 cgd /* floating-point constant out of range */
864 1.1 cgd warning(248);
865 1.1 cgd
866 1.1 cgd if (typ == FLOAT) {
867 1.1 cgd f = (float)d;
868 1.26 tv if (!finite(f)) {
869 1.1 cgd /* floating-point constant out of range */
870 1.1 cgd warning(248);
871 1.1 cgd f = f > 0 ? FLT_MAX : -FLT_MAX;
872 1.1 cgd }
873 1.1 cgd }
874 1.1 cgd
875 1.1 cgd (yylval.y_val = xcalloc(1, sizeof (val_t)))->v_tspec = typ;
876 1.1 cgd if (typ == FLOAT) {
877 1.1 cgd yylval.y_val->v_ldbl = f;
878 1.1 cgd } else {
879 1.1 cgd yylval.y_val->v_ldbl = d;
880 1.1 cgd }
881 1.1 cgd
882 1.97 rillig return T_CON;
883 1.1 cgd }
884 1.1 cgd
885 1.1 cgd static int
886 1.19 lukem operator(int t, op_t o)
887 1.1 cgd {
888 1.19 lukem
889 1.1 cgd yylval.y_op = o;
890 1.97 rillig return t;
891 1.1 cgd }
892 1.1 cgd
893 1.1 cgd /*
894 1.1 cgd * Called if lex found a leading \'.
895 1.1 cgd */
896 1.1 cgd static int
897 1.19 lukem ccon(void)
898 1.1 cgd {
899 1.43 christos size_t n;
900 1.43 christos int val, c;
901 1.1 cgd char cv;
902 1.1 cgd
903 1.1 cgd n = 0;
904 1.1 cgd val = 0;
905 1.1 cgd while ((c = getescc('\'')) >= 0) {
906 1.115 rillig val = (val << CHAR_SIZE) + c;
907 1.1 cgd n++;
908 1.1 cgd }
909 1.1 cgd if (c == -2) {
910 1.1 cgd /* unterminated character constant */
911 1.1 cgd error(253);
912 1.1 cgd } else {
913 1.1 cgd if (n > sizeof (int) || (n > 1 && (pflag || hflag))) {
914 1.1 cgd /* too many characters in character constant */
915 1.1 cgd error(71);
916 1.1 cgd } else if (n > 1) {
917 1.1 cgd /* multi-character character constant */
918 1.1 cgd warning(294);
919 1.1 cgd } else if (n == 0) {
920 1.1 cgd /* empty character constant */
921 1.1 cgd error(73);
922 1.1 cgd }
923 1.1 cgd }
924 1.1 cgd if (n == 1) {
925 1.1 cgd cv = (char)val;
926 1.1 cgd val = cv;
927 1.1 cgd }
928 1.19 lukem
929 1.1 cgd yylval.y_val = xcalloc(1, sizeof (val_t));
930 1.1 cgd yylval.y_val->v_tspec = INT;
931 1.1 cgd yylval.y_val->v_quad = val;
932 1.1 cgd
933 1.97 rillig return T_CON;
934 1.1 cgd }
935 1.1 cgd
936 1.1 cgd /*
937 1.1 cgd * Called if lex found a leading L\'
938 1.1 cgd */
939 1.1 cgd static int
940 1.19 lukem wccon(void)
941 1.1 cgd {
942 1.8 jpo static char buf[MB_LEN_MAX + 1];
943 1.43 christos size_t i;
944 1.43 christos int c;
945 1.1 cgd wchar_t wc;
946 1.1 cgd
947 1.1 cgd i = 0;
948 1.1 cgd while ((c = getescc('\'')) >= 0) {
949 1.1 cgd if (i < MB_CUR_MAX)
950 1.1 cgd buf[i] = (char)c;
951 1.1 cgd i++;
952 1.1 cgd }
953 1.1 cgd
954 1.1 cgd wc = 0;
955 1.1 cgd
956 1.1 cgd if (c == -2) {
957 1.1 cgd /* unterminated character constant */
958 1.1 cgd error(253);
959 1.1 cgd } else if (c == 0) {
960 1.1 cgd /* empty character constant */
961 1.1 cgd error(73);
962 1.1 cgd } else {
963 1.1 cgd if (i > MB_CUR_MAX) {
964 1.1 cgd i = MB_CUR_MAX;
965 1.1 cgd /* too many characters in character constant */
966 1.1 cgd error(71);
967 1.1 cgd } else {
968 1.1 cgd buf[i] = '\0';
969 1.1 cgd (void)mbtowc(NULL, NULL, 0);
970 1.1 cgd if (mbtowc(&wc, buf, MB_CUR_MAX) < 0)
971 1.1 cgd /* invalid multibyte character */
972 1.1 cgd error(291);
973 1.1 cgd }
974 1.1 cgd }
975 1.1 cgd
976 1.1 cgd yylval.y_val = xcalloc(1, sizeof (val_t));
977 1.1 cgd yylval.y_val->v_tspec = WCHAR;
978 1.1 cgd yylval.y_val->v_quad = wc;
979 1.1 cgd
980 1.97 rillig return T_CON;
981 1.1 cgd }
982 1.1 cgd
983 1.1 cgd /*
984 1.1 cgd * Read a character which is part of a character constant or of a string
985 1.1 cgd * and handle escapes.
986 1.1 cgd *
987 1.1 cgd * The Argument is the character which delimits the character constant or
988 1.1 cgd * string.
989 1.1 cgd *
990 1.1 cgd * Returns -1 if the end of the character constant or string is reached,
991 1.16 lukem * -2 if the EOF is reached, and the character otherwise.
992 1.1 cgd */
993 1.1 cgd static int
994 1.19 lukem getescc(int d)
995 1.1 cgd {
996 1.1 cgd static int pbc = -1;
997 1.1 cgd int n, c, v;
998 1.1 cgd
999 1.1 cgd if (pbc == -1) {
1000 1.1 cgd c = inpc();
1001 1.1 cgd } else {
1002 1.1 cgd c = pbc;
1003 1.1 cgd pbc = -1;
1004 1.1 cgd }
1005 1.1 cgd if (c == d)
1006 1.97 rillig return -1;
1007 1.1 cgd switch (c) {
1008 1.1 cgd case '\n':
1009 1.16 lukem if (tflag) {
1010 1.16 lukem /* newline in string or char constant */
1011 1.16 lukem error(254);
1012 1.97 rillig return -2;
1013 1.16 lukem }
1014 1.97 rillig return c;
1015 1.1 cgd case EOF:
1016 1.97 rillig return -2;
1017 1.1 cgd case '\\':
1018 1.1 cgd switch (c = inpc()) {
1019 1.1 cgd case '"':
1020 1.1 cgd if (tflag && d == '\'')
1021 1.106 rillig /* \" inside character constants undef... */
1022 1.1 cgd warning(262);
1023 1.97 rillig return '"';
1024 1.1 cgd case '\'':
1025 1.97 rillig return '\'';
1026 1.1 cgd case '?':
1027 1.1 cgd if (tflag)
1028 1.1 cgd /* \? undefined in traditional C */
1029 1.1 cgd warning(263);
1030 1.97 rillig return '?';
1031 1.1 cgd case '\\':
1032 1.97 rillig return '\\';
1033 1.1 cgd case 'a':
1034 1.1 cgd if (tflag)
1035 1.1 cgd /* \a undefined in traditional C */
1036 1.1 cgd warning(81);
1037 1.97 rillig return '\a';
1038 1.1 cgd case 'b':
1039 1.97 rillig return '\b';
1040 1.1 cgd case 'f':
1041 1.97 rillig return '\f';
1042 1.1 cgd case 'n':
1043 1.97 rillig return '\n';
1044 1.1 cgd case 'r':
1045 1.97 rillig return '\r';
1046 1.1 cgd case 't':
1047 1.97 rillig return '\t';
1048 1.1 cgd case 'v':
1049 1.1 cgd if (tflag)
1050 1.1 cgd /* \v undefined in traditional C */
1051 1.1 cgd warning(264);
1052 1.97 rillig return '\v';
1053 1.1 cgd case '8': case '9':
1054 1.1 cgd /* bad octal digit %c */
1055 1.1 cgd warning(77, c);
1056 1.1 cgd /* FALLTHROUGH */
1057 1.1 cgd case '0': case '1': case '2': case '3':
1058 1.1 cgd case '4': case '5': case '6': case '7':
1059 1.1 cgd n = 3;
1060 1.1 cgd v = 0;
1061 1.1 cgd do {
1062 1.1 cgd v = (v << 3) + (c - '0');
1063 1.1 cgd c = inpc();
1064 1.1 cgd } while (--n && isdigit(c) && (tflag || c <= '7'));
1065 1.1 cgd if (tflag && n > 0 && isdigit(c))
1066 1.1 cgd /* bad octal digit %c */
1067 1.1 cgd warning(77, c);
1068 1.1 cgd pbc = c;
1069 1.50 christos if (v > TARG_UCHAR_MAX) {
1070 1.106 rillig /* character escape does not fit in character */
1071 1.1 cgd warning(76);
1072 1.1 cgd v &= CHAR_MASK;
1073 1.1 cgd }
1074 1.97 rillig return v;
1075 1.1 cgd case 'x':
1076 1.1 cgd if (tflag)
1077 1.1 cgd /* \x undefined in traditional C */
1078 1.1 cgd warning(82);
1079 1.1 cgd v = 0;
1080 1.1 cgd n = 0;
1081 1.1 cgd while ((c = inpc()) >= 0 && isxdigit(c)) {
1082 1.1 cgd c = isdigit(c) ?
1083 1.1 cgd c - '0' : toupper(c) - 'A' + 10;
1084 1.1 cgd v = (v << 4) + c;
1085 1.1 cgd if (n >= 0) {
1086 1.1 cgd if ((v & ~CHAR_MASK) != 0) {
1087 1.1 cgd /* overflow in hex escape */
1088 1.1 cgd warning(75);
1089 1.1 cgd n = -1;
1090 1.1 cgd } else {
1091 1.1 cgd n++;
1092 1.1 cgd }
1093 1.1 cgd }
1094 1.1 cgd }
1095 1.1 cgd pbc = c;
1096 1.1 cgd if (n == 0) {
1097 1.1 cgd /* no hex digits follow \x */
1098 1.1 cgd error(74);
1099 1.1 cgd } if (n == -1) {
1100 1.1 cgd v &= CHAR_MASK;
1101 1.1 cgd }
1102 1.97 rillig return v;
1103 1.1 cgd case '\n':
1104 1.97 rillig return getescc(d);
1105 1.1 cgd case EOF:
1106 1.97 rillig return -2;
1107 1.1 cgd default:
1108 1.1 cgd if (isprint(c)) {
1109 1.1 cgd /* dubious escape \%c */
1110 1.1 cgd warning(79, c);
1111 1.1 cgd } else {
1112 1.1 cgd /* dubious escape \%o */
1113 1.1 cgd warning(80, c);
1114 1.1 cgd }
1115 1.1 cgd }
1116 1.1 cgd }
1117 1.97 rillig return c;
1118 1.1 cgd }
1119 1.1 cgd
1120 1.121 rillig /* See https://gcc.gnu.org/onlinedocs/cpp/Preprocessor-Output.html */
1121 1.121 rillig static void
1122 1.121 rillig parse_line_directive_flags(const char *p)
1123 1.121 rillig {
1124 1.121 rillig
1125 1.121 rillig in_system_header = false;
1126 1.121 rillig
1127 1.121 rillig while (ch_isspace(*p))
1128 1.121 rillig p++;
1129 1.121 rillig while (ch_isdigit(*p)) {
1130 1.121 rillig if (*p == '3' && !ch_isdigit(p[1]))
1131 1.121 rillig in_system_header = true;
1132 1.121 rillig p++;
1133 1.121 rillig while (ch_isspace(*p))
1134 1.121 rillig p++;
1135 1.121 rillig }
1136 1.121 rillig
1137 1.121 rillig #if 0
1138 1.121 rillig if (c != '\0')
1139 1.121 rillig warning("extra character(s) after directive");
1140 1.121 rillig #endif
1141 1.121 rillig }
1142 1.121 rillig
1143 1.1 cgd /*
1144 1.1 cgd * Called for preprocessor directives. Currently implemented are:
1145 1.1 cgd * # lineno
1146 1.1 cgd * # lineno "filename"
1147 1.1 cgd */
1148 1.1 cgd static void
1149 1.19 lukem directive(void)
1150 1.1 cgd {
1151 1.1 cgd const char *cp, *fn;
1152 1.1 cgd char c, *eptr;
1153 1.1 cgd size_t fnl;
1154 1.1 cgd long ln;
1155 1.118 rillig static bool first = true;
1156 1.1 cgd
1157 1.1 cgd /* Go to first non-whitespace after # */
1158 1.19 lukem for (cp = yytext + 1; (c = *cp) == ' ' || c == '\t'; cp++)
1159 1.19 lukem continue;
1160 1.1 cgd
1161 1.118 rillig if (!ch_isdigit(c)) {
1162 1.118 rillig if (strncmp(cp, "pragma", 6) == 0 && ch_isspace(cp[6]))
1163 1.78 christos return;
1164 1.1 cgd error:
1165 1.1 cgd /* undefined or invalid # directive */
1166 1.1 cgd warning(255);
1167 1.1 cgd return;
1168 1.1 cgd }
1169 1.1 cgd ln = strtol(--cp, &eptr, 10);
1170 1.1 cgd if (cp == eptr)
1171 1.1 cgd goto error;
1172 1.1 cgd if ((c = *(cp = eptr)) != ' ' && c != '\t' && c != '\0')
1173 1.1 cgd goto error;
1174 1.17 lukem while ((c = *cp++) == ' ' || c == '\t')
1175 1.17 lukem continue;
1176 1.1 cgd if (c != '\0') {
1177 1.1 cgd if (c != '"')
1178 1.1 cgd goto error;
1179 1.1 cgd fn = cp;
1180 1.1 cgd while ((c = *cp) != '"' && c != '\0')
1181 1.1 cgd cp++;
1182 1.1 cgd if (c != '"')
1183 1.1 cgd goto error;
1184 1.1 cgd if ((fnl = cp++ - fn) > PATH_MAX)
1185 1.1 cgd goto error;
1186 1.121 rillig parse_line_directive_flags(cp);
1187 1.9 cgd
1188 1.9 cgd /* empty string means stdin */
1189 1.9 cgd if (fnl == 0) {
1190 1.9 cgd fn = "{standard input}";
1191 1.9 cgd fnl = 16; /* strlen (fn) */
1192 1.9 cgd }
1193 1.1 cgd curr_pos.p_file = fnnalloc(fn, fnl);
1194 1.1 cgd /*
1195 1.1 cgd * If this is the first directive, the name is the name
1196 1.1 cgd * of the C source file as specified at the command line.
1197 1.1 cgd * It is written to the output file.
1198 1.1 cgd */
1199 1.1 cgd if (first) {
1200 1.1 cgd csrc_pos.p_file = curr_pos.p_file;
1201 1.68 christos outsrc(fnxform(curr_pos.p_file,
1202 1.68 christos strlen(curr_pos.p_file)));
1203 1.118 rillig first = false;
1204 1.1 cgd }
1205 1.1 cgd }
1206 1.1 cgd curr_pos.p_line = (int)ln - 1;
1207 1.9 cgd curr_pos.p_uniq = 0;
1208 1.9 cgd if (curr_pos.p_file == csrc_pos.p_file) {
1209 1.1 cgd csrc_pos.p_line = (int)ln - 1;
1210 1.9 cgd csrc_pos.p_uniq = 0;
1211 1.9 cgd }
1212 1.1 cgd }
1213 1.1 cgd
1214 1.1 cgd /*
1215 1.1 cgd * Handle lint comments. Following comments are currently understood:
1216 1.1 cgd * ARGSUSEDn
1217 1.22 thorpej * BITFIELDTYPE
1218 1.1 cgd * CONSTCOND CONSTANTCOND CONSTANTCONDITION
1219 1.1 cgd * FALLTHRU FALLTHROUGH
1220 1.1 cgd * LINTLIBRARY
1221 1.52 christos * LINTEDn NOSTRICTn
1222 1.7 jpo * LONGLONG
1223 1.1 cgd * NOTREACHED
1224 1.1 cgd * PRINTFLIKEn
1225 1.1 cgd * PROTOLIB
1226 1.1 cgd * SCANFLIKEn
1227 1.1 cgd * VARARGSn
1228 1.129 rillig * If one of these comments is recognized, the argument, if any, is
1229 1.1 cgd * parsed and a function which handles this comment is called.
1230 1.1 cgd */
1231 1.1 cgd static void
1232 1.19 lukem comment(void)
1233 1.1 cgd {
1234 1.1 cgd int c, lc;
1235 1.129 rillig static const struct {
1236 1.1 cgd const char *keywd;
1237 1.1 cgd int arg;
1238 1.19 lukem void (*func)(int);
1239 1.1 cgd } keywtab[] = {
1240 1.1 cgd { "ARGSUSED", 1, argsused },
1241 1.22 thorpej { "BITFIELDTYPE", 0, bitfieldtype },
1242 1.1 cgd { "CONSTCOND", 0, constcond },
1243 1.1 cgd { "CONSTANTCOND", 0, constcond },
1244 1.1 cgd { "CONSTANTCONDITION", 0, constcond },
1245 1.1 cgd { "FALLTHRU", 0, fallthru },
1246 1.1 cgd { "FALLTHROUGH", 0, fallthru },
1247 1.1 cgd { "LINTLIBRARY", 0, lintlib },
1248 1.51 christos { "LINTED", 1, linted },
1249 1.7 jpo { "LONGLONG", 0, longlong },
1250 1.52 christos { "NOSTRICT", 1, linted },
1251 1.1 cgd { "NOTREACHED", 0, notreach },
1252 1.1 cgd { "PRINTFLIKE", 1, printflike },
1253 1.1 cgd { "PROTOLIB", 1, protolib },
1254 1.1 cgd { "SCANFLIKE", 1, scanflike },
1255 1.1 cgd { "VARARGS", 1, varargs },
1256 1.1 cgd };
1257 1.1 cgd char keywd[32];
1258 1.1 cgd char arg[32];
1259 1.43 christos size_t l, i;
1260 1.43 christos int a;
1261 1.118 rillig bool eoc;
1262 1.1 cgd
1263 1.118 rillig eoc = false;
1264 1.1 cgd
1265 1.129 rillig /* Skip whitespace after the start of the comment */
1266 1.17 lukem while ((c = inpc()) != EOF && isspace(c))
1267 1.17 lukem continue;
1268 1.1 cgd
1269 1.1 cgd /* Read the potential keyword to keywd */
1270 1.1 cgd l = 0;
1271 1.1 cgd while (c != EOF && isupper(c) && l < sizeof (keywd) - 1) {
1272 1.1 cgd keywd[l++] = (char)c;
1273 1.1 cgd c = inpc();
1274 1.1 cgd }
1275 1.1 cgd keywd[l] = '\0';
1276 1.1 cgd
1277 1.1 cgd /* look for the keyword */
1278 1.1 cgd for (i = 0; i < sizeof (keywtab) / sizeof (keywtab[0]); i++) {
1279 1.1 cgd if (strcmp(keywtab[i].keywd, keywd) == 0)
1280 1.1 cgd break;
1281 1.1 cgd }
1282 1.1 cgd if (i == sizeof (keywtab) / sizeof (keywtab[0]))
1283 1.1 cgd goto skip_rest;
1284 1.1 cgd
1285 1.129 rillig /* skip whitespace after the keyword */
1286 1.1 cgd while (c != EOF && isspace(c))
1287 1.1 cgd c = inpc();
1288 1.1 cgd
1289 1.1 cgd /* read the argument, if the keyword accepts one and there is one */
1290 1.1 cgd l = 0;
1291 1.1 cgd if (keywtab[i].arg) {
1292 1.1 cgd while (c != EOF && isdigit(c) && l < sizeof (arg) - 1) {
1293 1.1 cgd arg[l++] = (char)c;
1294 1.1 cgd c = inpc();
1295 1.1 cgd }
1296 1.1 cgd }
1297 1.1 cgd arg[l] = '\0';
1298 1.1 cgd a = l != 0 ? atoi(arg) : -1;
1299 1.1 cgd
1300 1.129 rillig /* skip whitespace after the argument */
1301 1.1 cgd while (c != EOF && isspace(c))
1302 1.1 cgd c = inpc();
1303 1.1 cgd
1304 1.1 cgd if (c != '*' || (c = inpc()) != '/') {
1305 1.1 cgd if (keywtab[i].func != linted)
1306 1.1 cgd /* extra characters in lint comment */
1307 1.1 cgd warning(257);
1308 1.1 cgd } else {
1309 1.1 cgd /*
1310 1.1 cgd * remember that we have already found the end of the
1311 1.1 cgd * comment
1312 1.1 cgd */
1313 1.118 rillig eoc = true;
1314 1.1 cgd }
1315 1.1 cgd
1316 1.1 cgd if (keywtab[i].func != NULL)
1317 1.1 cgd (*keywtab[i].func)(a);
1318 1.1 cgd
1319 1.1 cgd skip_rest:
1320 1.1 cgd while (!eoc) {
1321 1.1 cgd lc = c;
1322 1.1 cgd if ((c = inpc()) == EOF) {
1323 1.1 cgd /* unterminated comment */
1324 1.1 cgd error(256);
1325 1.1 cgd break;
1326 1.1 cgd }
1327 1.1 cgd if (lc == '*' && c == '/')
1328 1.118 rillig eoc = true;
1329 1.1 cgd }
1330 1.18 lukem }
1331 1.18 lukem
1332 1.18 lukem /*
1333 1.18 lukem * Handle // style comments
1334 1.18 lukem */
1335 1.18 lukem static void
1336 1.19 lukem slashslashcomment(void)
1337 1.18 lukem {
1338 1.18 lukem int c;
1339 1.18 lukem
1340 1.32 perry if (!Sflag && !gflag)
1341 1.111 rillig /* %s C does not support // comments */
1342 1.111 rillig gnuism(312, tflag ? "traditional" : "ANSI");
1343 1.18 lukem
1344 1.18 lukem while ((c = inpc()) != EOF && c != '\n')
1345 1.18 lukem continue;
1346 1.7 jpo }
1347 1.7 jpo
1348 1.7 jpo /*
1349 1.7 jpo * Clear flags for lint comments LINTED, LONGLONG and CONSTCOND.
1350 1.124 rillig * clear_warn_flags() is called after function definitions and global and
1351 1.7 jpo * local declarations and definitions. It is also called between
1352 1.7 jpo * the controlling expression and the body of control statements
1353 1.7 jpo * (if, switch, for, while).
1354 1.7 jpo */
1355 1.7 jpo void
1356 1.124 rillig clear_warn_flags(void)
1357 1.7 jpo {
1358 1.19 lukem
1359 1.51 christos lwarn = LWARN_ALL;
1360 1.118 rillig quadflg = false;
1361 1.118 rillig constcond_flag = false;
1362 1.1 cgd }
1363 1.1 cgd
1364 1.1 cgd /*
1365 1.112 rillig * Strings are stored in a dynamically allocated buffer and passed
1366 1.1 cgd * in yylval.y_xstrg to the parser. The parser or the routines called
1367 1.1 cgd * by the parser are responsible for freeing this buffer.
1368 1.1 cgd */
1369 1.1 cgd static int
1370 1.19 lukem string(void)
1371 1.1 cgd {
1372 1.1 cgd u_char *s;
1373 1.1 cgd int c;
1374 1.1 cgd size_t len, max;
1375 1.1 cgd strg_t *strg;
1376 1.1 cgd
1377 1.1 cgd s = xmalloc(max = 64);
1378 1.1 cgd
1379 1.1 cgd len = 0;
1380 1.1 cgd while ((c = getescc('"')) >= 0) {
1381 1.1 cgd /* +1 to reserve space for a trailing NUL character */
1382 1.1 cgd if (len + 1 == max)
1383 1.1 cgd s = xrealloc(s, max *= 2);
1384 1.1 cgd s[len++] = (char)c;
1385 1.1 cgd }
1386 1.1 cgd s[len] = '\0';
1387 1.1 cgd if (c == -2)
1388 1.1 cgd /* unterminated string constant */
1389 1.1 cgd error(258);
1390 1.1 cgd
1391 1.1 cgd strg = xcalloc(1, sizeof (strg_t));
1392 1.1 cgd strg->st_tspec = CHAR;
1393 1.1 cgd strg->st_len = len;
1394 1.1 cgd strg->st_cp = s;
1395 1.1 cgd
1396 1.102 rillig yylval.y_string = strg;
1397 1.97 rillig return T_STRING;
1398 1.1 cgd }
1399 1.1 cgd
1400 1.1 cgd static int
1401 1.19 lukem wcstrg(void)
1402 1.1 cgd {
1403 1.1 cgd char *s;
1404 1.43 christos int c, n;
1405 1.43 christos size_t i, wi;
1406 1.1 cgd size_t len, max, wlen;
1407 1.1 cgd wchar_t *ws;
1408 1.1 cgd strg_t *strg;
1409 1.1 cgd
1410 1.1 cgd s = xmalloc(max = 64);
1411 1.1 cgd len = 0;
1412 1.1 cgd while ((c = getescc('"')) >= 0) {
1413 1.1 cgd /* +1 to save space for a trailing NUL character */
1414 1.1 cgd if (len + 1 >= max)
1415 1.1 cgd s = xrealloc(s, max *= 2);
1416 1.1 cgd s[len++] = (char)c;
1417 1.1 cgd }
1418 1.1 cgd s[len] = '\0';
1419 1.1 cgd if (c == -2)
1420 1.1 cgd /* unterminated string constant */
1421 1.1 cgd error(258);
1422 1.1 cgd
1423 1.48 wiz /* get length of wide-character string */
1424 1.1 cgd (void)mblen(NULL, 0);
1425 1.1 cgd for (i = 0, wlen = 0; i < len; i += n, wlen++) {
1426 1.1 cgd if ((n = mblen(&s[i], MB_CUR_MAX)) == -1) {
1427 1.1 cgd /* invalid multibyte character */
1428 1.1 cgd error(291);
1429 1.1 cgd break;
1430 1.1 cgd }
1431 1.1 cgd if (n == 0)
1432 1.1 cgd n = 1;
1433 1.1 cgd }
1434 1.1 cgd
1435 1.1 cgd ws = xmalloc((wlen + 1) * sizeof (wchar_t));
1436 1.1 cgd
1437 1.1 cgd /* convert from multibyte to wide char */
1438 1.1 cgd (void)mbtowc(NULL, NULL, 0);
1439 1.1 cgd for (i = 0, wi = 0; i < len; i += n, wi++) {
1440 1.1 cgd if ((n = mbtowc(&ws[wi], &s[i], MB_CUR_MAX)) == -1)
1441 1.1 cgd break;
1442 1.1 cgd if (n == 0)
1443 1.1 cgd n = 1;
1444 1.1 cgd }
1445 1.1 cgd ws[wi] = 0;
1446 1.1 cgd free(s);
1447 1.1 cgd
1448 1.1 cgd strg = xcalloc(1, sizeof (strg_t));
1449 1.1 cgd strg->st_tspec = WCHAR;
1450 1.1 cgd strg->st_len = wlen;
1451 1.1 cgd strg->st_wcp = ws;
1452 1.1 cgd
1453 1.102 rillig yylval.y_string = strg;
1454 1.97 rillig return T_STRING;
1455 1.1 cgd }
1456 1.1 cgd
1457 1.1 cgd /*
1458 1.1 cgd * As noted above the scanner does not create new symbol table entries
1459 1.1 cgd * for symbols it cannot find in the symbol table. This is to avoid
1460 1.1 cgd * putting undeclared symbols into the symbol table if a syntax error
1461 1.1 cgd * occurs.
1462 1.1 cgd *
1463 1.1 cgd * getsym() is called as soon as it is probably ok to put the symbol to
1464 1.1 cgd * the symbol table. This does not mean that it is not possible that
1465 1.129 rillig * symbols are put to the symbol table which are not completely
1466 1.1 cgd * declared due to syntax errors. To avoid too many problems in this
1467 1.129 rillig * case, symbols get type int in getsym().
1468 1.1 cgd *
1469 1.1 cgd * XXX calls to getsym() should be delayed until decl1*() is called
1470 1.1 cgd */
1471 1.1 cgd sym_t *
1472 1.19 lukem getsym(sbuf_t *sb)
1473 1.1 cgd {
1474 1.1 cgd dinfo_t *di;
1475 1.1 cgd char *s;
1476 1.1 cgd sym_t *sym;
1477 1.1 cgd
1478 1.1 cgd sym = sb->sb_sym;
1479 1.1 cgd
1480 1.1 cgd /*
1481 1.1 cgd * During member declaration it is possible that name() looked
1482 1.1 cgd * for symbols of type FVFT, although it should have looked for
1483 1.1 cgd * symbols of type FTAG. Same can happen for labels. Both cases
1484 1.1 cgd * are compensated here.
1485 1.1 cgd */
1486 1.99 rillig if (symtyp == FMEMBER || symtyp == FLABEL) {
1487 1.1 cgd if (sym == NULL || sym->s_kind == FVFT)
1488 1.1 cgd sym = search(sb);
1489 1.1 cgd }
1490 1.1 cgd
1491 1.1 cgd if (sym != NULL) {
1492 1.1 cgd if (sym->s_kind != symtyp)
1493 1.107 rillig LERROR("getsym(%d, %d)", sym->s_kind, symtyp);
1494 1.1 cgd symtyp = FVFT;
1495 1.1 cgd freesb(sb);
1496 1.97 rillig return sym;
1497 1.1 cgd }
1498 1.1 cgd
1499 1.1 cgd /* create a new symbol table entry */
1500 1.1 cgd
1501 1.129 rillig /* labels must always be allocated at level 1 (outermost block) */
1502 1.99 rillig if (symtyp == FLABEL) {
1503 1.1 cgd sym = getlblk(1, sizeof (sym_t));
1504 1.1 cgd s = getlblk(1, sb->sb_len + 1);
1505 1.1 cgd (void)memcpy(s, sb->sb_name, sb->sb_len + 1);
1506 1.1 cgd sym->s_name = s;
1507 1.1 cgd sym->s_blklev = 1;
1508 1.1 cgd di = dcs;
1509 1.101 rillig while (di->d_next != NULL && di->d_next->d_next != NULL)
1510 1.101 rillig di = di->d_next;
1511 1.107 rillig lint_assert(di->d_ctx == AUTO);
1512 1.1 cgd } else {
1513 1.1 cgd sym = getblk(sizeof (sym_t));
1514 1.1 cgd sym->s_name = sb->sb_name;
1515 1.1 cgd sym->s_blklev = blklev;
1516 1.1 cgd di = dcs;
1517 1.1 cgd }
1518 1.1 cgd
1519 1.104 rillig UNIQUE_CURR_POS(sym->s_def_pos);
1520 1.99 rillig if ((sym->s_kind = symtyp) != FLABEL)
1521 1.1 cgd sym->s_type = gettyp(INT);
1522 1.1 cgd
1523 1.1 cgd symtyp = FVFT;
1524 1.1 cgd
1525 1.1 cgd if ((sym->s_link = symtab[sb->sb_hash]) != NULL)
1526 1.1 cgd symtab[sb->sb_hash]->s_rlink = &sym->s_link;
1527 1.53 christos sym->s_rlink = &symtab[sb->sb_hash];
1528 1.53 christos symtab[sb->sb_hash] = sym;
1529 1.1 cgd
1530 1.3 jpo *di->d_ldlsym = sym;
1531 1.3 jpo di->d_ldlsym = &sym->s_dlnxt;
1532 1.1 cgd
1533 1.1 cgd freesb(sb);
1534 1.97 rillig return sym;
1535 1.29 christos }
1536 1.29 christos
1537 1.29 christos /*
1538 1.29 christos * Construct a temporary symbol. The symbol starts with a digit, so that
1539 1.29 christos * it is illegal.
1540 1.29 christos */
1541 1.29 christos sym_t *
1542 1.29 christos mktempsym(type_t *t)
1543 1.29 christos {
1544 1.29 christos static int n = 0;
1545 1.29 christos int h;
1546 1.29 christos char *s = getlblk(blklev, 64);
1547 1.29 christos sym_t *sym = getblk(sizeof (sym_t));
1548 1.29 christos
1549 1.29 christos (void)snprintf(s, 64, "%.8d_tmp", n++);
1550 1.29 christos h = hash(s);
1551 1.30 christos
1552 1.29 christos sym->s_name = s;
1553 1.29 christos sym->s_type = t;
1554 1.29 christos sym->s_blklev = blklev;
1555 1.29 christos sym->s_scl = AUTO;
1556 1.30 christos sym->s_kind = FVFT;
1557 1.118 rillig sym->s_used = true;
1558 1.118 rillig sym->s_set = true;
1559 1.30 christos
1560 1.29 christos if ((sym->s_link = symtab[h]) != NULL)
1561 1.29 christos symtab[h]->s_rlink = &sym->s_link;
1562 1.53 christos sym->s_rlink = &symtab[h];
1563 1.53 christos symtab[h] = sym;
1564 1.30 christos
1565 1.30 christos *dcs->d_ldlsym = sym;
1566 1.30 christos dcs->d_ldlsym = &sym->s_dlnxt;
1567 1.30 christos
1568 1.29 christos return sym;
1569 1.1 cgd }
1570 1.1 cgd
1571 1.1 cgd /*
1572 1.1 cgd * Remove a symbol forever from the symbol table. s_blklev
1573 1.1 cgd * is set to -1 to avoid that the symbol will later be put
1574 1.1 cgd * back to the symbol table.
1575 1.1 cgd */
1576 1.1 cgd void
1577 1.19 lukem rmsym(sym_t *sym)
1578 1.1 cgd {
1579 1.19 lukem
1580 1.1 cgd if ((*sym->s_rlink = sym->s_link) != NULL)
1581 1.1 cgd sym->s_link->s_rlink = sym->s_rlink;
1582 1.1 cgd sym->s_blklev = -1;
1583 1.1 cgd sym->s_link = NULL;
1584 1.1 cgd }
1585 1.1 cgd
1586 1.1 cgd /*
1587 1.1 cgd * Remove a list of symbols declared at one level from the symbol
1588 1.1 cgd * table.
1589 1.1 cgd */
1590 1.1 cgd void
1591 1.19 lukem rmsyms(sym_t *syms)
1592 1.1 cgd {
1593 1.1 cgd sym_t *sym;
1594 1.1 cgd
1595 1.1 cgd for (sym = syms; sym != NULL; sym = sym->s_dlnxt) {
1596 1.1 cgd if (sym->s_blklev != -1) {
1597 1.1 cgd if ((*sym->s_rlink = sym->s_link) != NULL)
1598 1.1 cgd sym->s_link->s_rlink = sym->s_rlink;
1599 1.1 cgd sym->s_link = NULL;
1600 1.1 cgd sym->s_rlink = NULL;
1601 1.1 cgd }
1602 1.1 cgd }
1603 1.1 cgd }
1604 1.1 cgd
1605 1.1 cgd /*
1606 1.1 cgd * Put a symbol into the symbol table
1607 1.1 cgd */
1608 1.1 cgd void
1609 1.19 lukem inssym(int bl, sym_t *sym)
1610 1.1 cgd {
1611 1.1 cgd int h;
1612 1.1 cgd
1613 1.1 cgd h = hash(sym->s_name);
1614 1.1 cgd if ((sym->s_link = symtab[h]) != NULL)
1615 1.1 cgd symtab[h]->s_rlink = &sym->s_link;
1616 1.53 christos sym->s_rlink = &symtab[h];
1617 1.53 christos symtab[h] = sym;
1618 1.1 cgd sym->s_blklev = bl;
1619 1.107 rillig lint_assert(sym->s_link == NULL ||
1620 1.107 rillig sym->s_blklev >= sym->s_link->s_blklev);
1621 1.1 cgd }
1622 1.1 cgd
1623 1.1 cgd /*
1624 1.1 cgd * Called at level 0 after syntax errors
1625 1.1 cgd * Removes all symbols which are not declared at level 0 from the
1626 1.1 cgd * symbol table. Also frees all memory which is not associated with
1627 1.1 cgd * level 0.
1628 1.1 cgd */
1629 1.1 cgd void
1630 1.19 lukem cleanup(void)
1631 1.1 cgd {
1632 1.1 cgd sym_t *sym, *nsym;
1633 1.1 cgd int i;
1634 1.1 cgd
1635 1.1 cgd for (i = 0; i < HSHSIZ1; i++) {
1636 1.1 cgd for (sym = symtab[i]; sym != NULL; sym = nsym) {
1637 1.1 cgd nsym = sym->s_link;
1638 1.1 cgd if (sym->s_blklev >= 1) {
1639 1.1 cgd if ((*sym->s_rlink = nsym) != NULL)
1640 1.1 cgd nsym->s_rlink = sym->s_rlink;
1641 1.1 cgd }
1642 1.1 cgd }
1643 1.1 cgd }
1644 1.1 cgd
1645 1.1 cgd for (i = mblklev; i > 0; i--)
1646 1.1 cgd freelblk(i);
1647 1.1 cgd }
1648 1.1 cgd
1649 1.1 cgd /*
1650 1.1 cgd * Create a new symbol with the name of an existing symbol.
1651 1.1 cgd */
1652 1.1 cgd sym_t *
1653 1.19 lukem pushdown(sym_t *sym)
1654 1.1 cgd {
1655 1.1 cgd int h;
1656 1.1 cgd sym_t *nsym;
1657 1.1 cgd
1658 1.1 cgd h = hash(sym->s_name);
1659 1.1 cgd nsym = getblk(sizeof (sym_t));
1660 1.107 rillig lint_assert(sym->s_blklev <= blklev);
1661 1.1 cgd nsym->s_name = sym->s_name;
1662 1.104 rillig UNIQUE_CURR_POS(nsym->s_def_pos);
1663 1.1 cgd nsym->s_kind = sym->s_kind;
1664 1.1 cgd nsym->s_blklev = blklev;
1665 1.1 cgd
1666 1.1 cgd if ((nsym->s_link = symtab[h]) != NULL)
1667 1.1 cgd symtab[h]->s_rlink = &nsym->s_link;
1668 1.53 christos nsym->s_rlink = &symtab[h];
1669 1.53 christos symtab[h] = nsym;
1670 1.1 cgd
1671 1.3 jpo *dcs->d_ldlsym = nsym;
1672 1.3 jpo dcs->d_ldlsym = &nsym->s_dlnxt;
1673 1.1 cgd
1674 1.97 rillig return nsym;
1675 1.6 jpo }
1676 1.6 jpo
1677 1.6 jpo /*
1678 1.6 jpo * Free any dynamically allocated memory referenced by
1679 1.6 jpo * the value stack or yylval.
1680 1.6 jpo * The type of information in yylval is described by tok.
1681 1.6 jpo */
1682 1.6 jpo void
1683 1.19 lukem freeyyv(void *sp, int tok)
1684 1.6 jpo {
1685 1.6 jpo if (tok == T_NAME || tok == T_TYPENAME) {
1686 1.6 jpo sbuf_t *sb = *(sbuf_t **)sp;
1687 1.6 jpo freesb(sb);
1688 1.6 jpo } else if (tok == T_CON) {
1689 1.6 jpo val_t *val = *(val_t **)sp;
1690 1.6 jpo free(val);
1691 1.6 jpo } else if (tok == T_STRING) {
1692 1.6 jpo strg_t *strg = *(strg_t **)sp;
1693 1.6 jpo if (strg->st_tspec == CHAR) {
1694 1.6 jpo free(strg->st_cp);
1695 1.107 rillig } else {
1696 1.107 rillig lint_assert(strg->st_tspec == WCHAR);
1697 1.6 jpo free(strg->st_wcp);
1698 1.6 jpo }
1699 1.6 jpo free(strg);
1700 1.19 lukem }
1701 1.1 cgd }
1702