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