Fri Oct 2 21:49:30 2009 UTC ()
handle bit fields in packed structures.


(christos)
diff -r1.50 -r1.51 src/usr.bin/xlint/lint1/decl.c

cvs diff -r1.50 -r1.51 src/usr.bin/xlint/lint1/decl.c (switch to unified diff)

--- src/usr.bin/xlint/lint1/decl.c 2009/10/02 21:04:03 1.50
+++ src/usr.bin/xlint/lint1/decl.c 2009/10/02 21:49:30 1.51
@@ -1,1509 +1,1516 @@ @@ -1,1509 +1,1516 @@
1/* $NetBSD: decl.c,v 1.50 2009/10/02 21:04:03 christos Exp $ */ 1/* $NetBSD: decl.c,v 1.51 2009/10/02 21:49:30 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved.
5 * Copyright (c) 1994, 1995 Jochen Pohl 5 * Copyright (c) 1994, 1995 Jochen Pohl
6 * All Rights Reserved. 6 * All Rights Reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
10 * are met: 10 * are met:
11 * 1. Redistributions of source code must retain the above copyright 11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer. 12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright 13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the 14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution. 15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software 16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement: 17 * must display the following acknowledgement:
18 * This product includes software developed by Jochen Pohl for 18 * This product includes software developed by Jochen Pohl for
19 * The NetBSD Project. 19 * The NetBSD Project.
20 * 4. The name of the author may not be used to endorse or promote products 20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission. 21 * derived from this software without specific prior written permission.
22 * 22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */ 33 */
34 34
35#if HAVE_NBTOOL_CONFIG_H 35#if HAVE_NBTOOL_CONFIG_H
36#include "nbtool_config.h" 36#include "nbtool_config.h"
37#endif 37#endif
38 38
39#include <sys/cdefs.h> 39#include <sys/cdefs.h>
40#if defined(__RCSID) && !defined(lint) 40#if defined(__RCSID) && !defined(lint)
41__RCSID("$NetBSD: decl.c,v 1.50 2009/10/02 21:04:03 christos Exp $"); 41__RCSID("$NetBSD: decl.c,v 1.51 2009/10/02 21:49:30 christos Exp $");
42#endif 42#endif
43 43
44#include <sys/param.h> 44#include <sys/param.h>
45#include <limits.h> 45#include <limits.h>
46#include <stdlib.h> 46#include <stdlib.h>
47#include <string.h> 47#include <string.h>
48 48
49#include "lint1.h" 49#include "lint1.h"
50 50
51const char *unnamed = "<unnamed>"; 51const char *unnamed = "<unnamed>";
52 52
53/* shared type structures for arithmtic types and void */ 53/* shared type structures for arithmtic types and void */
54static type_t *typetab; 54static type_t *typetab;
55 55
56/* value of next enumerator during declaration of enum types */ 56/* value of next enumerator during declaration of enum types */
57int enumval; 57int enumval;
58 58
59/* 59/*
60 * pointer to top element of a stack which contains informations local 60 * pointer to top element of a stack which contains informations local
61 * to nested declarations 61 * to nested declarations
62 */ 62 */
63dinfo_t *dcs; 63dinfo_t *dcs;
64 64
65static type_t *tdeferr(type_t *, tspec_t); 65static type_t *tdeferr(type_t *, tspec_t);
66static void settdsym(type_t *, sym_t *); 66static void settdsym(type_t *, sym_t *);
67static tspec_t mrgtspec(tspec_t, tspec_t); 67static tspec_t mrgtspec(tspec_t, tspec_t);
68static void align(int, int); 68static void align(int, int);
69static sym_t *newtag(sym_t *, scl_t, int, int); 69static sym_t *newtag(sym_t *, scl_t, int, int);
70static int eqargs(type_t *, type_t *, int *); 70static int eqargs(type_t *, type_t *, int *);
71static int mnoarg(type_t *, int *); 71static int mnoarg(type_t *, int *);
72static int chkosdef(sym_t *, sym_t *); 72static int chkosdef(sym_t *, sym_t *);
73static int chkptdecl(sym_t *, sym_t *); 73static int chkptdecl(sym_t *, sym_t *);
74static sym_t *nsfunc(sym_t *, sym_t *); 74static sym_t *nsfunc(sym_t *, sym_t *);
75static void osfunc(sym_t *, sym_t *); 75static void osfunc(sym_t *, sym_t *);
76static void ledecl(sym_t *); 76static void ledecl(sym_t *);
77static int chkinit(sym_t *); 77static int chkinit(sym_t *);
78static void chkausg(int, sym_t *); 78static void chkausg(int, sym_t *);
79static void chkvusg(int, sym_t *); 79static void chkvusg(int, sym_t *);
80static void chklusg(sym_t *); 80static void chklusg(sym_t *);
81static void chktusg(sym_t *); 81static void chktusg(sym_t *);
82static void chkglvar(sym_t *); 82static void chkglvar(sym_t *);
83static void glchksz(sym_t *); 83static void glchksz(sym_t *);
84 84
85/* 85/*
86 * initializes all global vars used in declarations 86 * initializes all global vars used in declarations
87 */ 87 */
88void 88void
89initdecl(void) 89initdecl(void)
90{ 90{
91 int i; 91 int i;
92 92
93 /* declaration stack */ 93 /* declaration stack */
94 dcs = xcalloc(1, sizeof (dinfo_t)); 94 dcs = xcalloc(1, sizeof (dinfo_t));
95 dcs->d_ctx = EXTERN; 95 dcs->d_ctx = EXTERN;
96 dcs->d_ldlsym = &dcs->d_dlsyms; 96 dcs->d_ldlsym = &dcs->d_dlsyms;
97 97
98 /* type information and classification */ 98 /* type information and classification */
99 inittyp(); 99 inittyp();
100 100
101 /* shared type structures */ 101 /* shared type structures */
102 typetab = xcalloc(NTSPEC, sizeof (type_t)); 102 typetab = xcalloc(NTSPEC, sizeof (type_t));
103 for (i = 0; i < NTSPEC; i++) 103 for (i = 0; i < NTSPEC; i++)
104 typetab[i].t_tspec = NOTSPEC; 104 typetab[i].t_tspec = NOTSPEC;
105 typetab[BOOL].t_tspec = BOOL; 105 typetab[BOOL].t_tspec = BOOL;
106 typetab[CHAR].t_tspec = CHAR; 106 typetab[CHAR].t_tspec = CHAR;
107 typetab[SCHAR].t_tspec = SCHAR; 107 typetab[SCHAR].t_tspec = SCHAR;
108 typetab[UCHAR].t_tspec = UCHAR; 108 typetab[UCHAR].t_tspec = UCHAR;
109 typetab[SHORT].t_tspec = SHORT; 109 typetab[SHORT].t_tspec = SHORT;
110 typetab[USHORT].t_tspec = USHORT; 110 typetab[USHORT].t_tspec = USHORT;
111 typetab[INT].t_tspec = INT; 111 typetab[INT].t_tspec = INT;
112 typetab[UINT].t_tspec = UINT; 112 typetab[UINT].t_tspec = UINT;
113 typetab[LONG].t_tspec = LONG; 113 typetab[LONG].t_tspec = LONG;
114 typetab[ULONG].t_tspec = ULONG; 114 typetab[ULONG].t_tspec = ULONG;
115 typetab[QUAD].t_tspec = QUAD; 115 typetab[QUAD].t_tspec = QUAD;
116 typetab[UQUAD].t_tspec = UQUAD; 116 typetab[UQUAD].t_tspec = UQUAD;
117 typetab[FLOAT].t_tspec = FLOAT; 117 typetab[FLOAT].t_tspec = FLOAT;
118 typetab[DOUBLE].t_tspec = DOUBLE; 118 typetab[DOUBLE].t_tspec = DOUBLE;
119 typetab[LDOUBLE].t_tspec = LDOUBLE; 119 typetab[LDOUBLE].t_tspec = LDOUBLE;
120 typetab[FCOMPLEX].t_tspec = FCOMPLEX; 120 typetab[FCOMPLEX].t_tspec = FCOMPLEX;
121 typetab[DCOMPLEX].t_tspec = DCOMPLEX; 121 typetab[DCOMPLEX].t_tspec = DCOMPLEX;
122 typetab[LCOMPLEX].t_tspec = LCOMPLEX; 122 typetab[LCOMPLEX].t_tspec = LCOMPLEX;
123 typetab[COMPLEX].t_tspec = COMPLEX; 123 typetab[COMPLEX].t_tspec = COMPLEX;
124 typetab[VOID].t_tspec = VOID; 124 typetab[VOID].t_tspec = VOID;
125 /* 125 /*
126 * Next two are not real types. They are only used by the parser 126 * Next two are not real types. They are only used by the parser
127 * to return keywords "signed" and "unsigned" 127 * to return keywords "signed" and "unsigned"
128 */ 128 */
129 typetab[SIGNED].t_tspec = SIGNED; 129 typetab[SIGNED].t_tspec = SIGNED;
130 typetab[UNSIGN].t_tspec = UNSIGN; 130 typetab[UNSIGN].t_tspec = UNSIGN;
131} 131}
132 132
133/* 133/*
134 * Returns a shared type structure vor arithmetic types and void. 134 * Returns a shared type structure vor arithmetic types and void.
135 * 135 *
136 * It's important do duplicate this structure (using duptyp() or tdupdyp()) 136 * It's important do duplicate this structure (using duptyp() or tdupdyp())
137 * if it is to be modified (adding qualifiers or anything else). 137 * if it is to be modified (adding qualifiers or anything else).
138 */ 138 */
139type_t * 139type_t *
140gettyp(tspec_t t) 140gettyp(tspec_t t)
141{ 141{
142 142
143 return (&typetab[t]); 143 return (&typetab[t]);
144} 144}
145 145
146type_t * 146type_t *
147duptyp(const type_t *tp) 147duptyp(const type_t *tp)
148{ 148{
149 type_t *ntp; 149 type_t *ntp;
150 150
151 ntp = getblk(sizeof (type_t)); 151 ntp = getblk(sizeof (type_t));
152 STRUCT_ASSIGN(*ntp, *tp); 152 STRUCT_ASSIGN(*ntp, *tp);
153 return (ntp); 153 return (ntp);
154} 154}
155 155
156/* 156/*
157 * Use tduptyp() instead of duptyp() inside expressions (if the 157 * Use tduptyp() instead of duptyp() inside expressions (if the
158 * allocated memory should be freed after the expr). 158 * allocated memory should be freed after the expr).
159 */ 159 */
160type_t * 160type_t *
161tduptyp(const type_t *tp) 161tduptyp(const type_t *tp)
162{ 162{
163 type_t *ntp; 163 type_t *ntp;
164 164
165 ntp = tgetblk(sizeof (type_t)); 165 ntp = tgetblk(sizeof (type_t));
166 STRUCT_ASSIGN(*ntp, *tp); 166 STRUCT_ASSIGN(*ntp, *tp);
167 return (ntp); 167 return (ntp);
168} 168}
169 169
170/* 170/*
171 * Returns 1 if the argument is void or an incomplete array, 171 * Returns 1 if the argument is void or an incomplete array,
172 * struct, union or enum type. 172 * struct, union or enum type.
173 */ 173 */
174int 174int
175incompl(type_t *tp) 175incompl(type_t *tp)
176{ 176{
177 tspec_t t; 177 tspec_t t;
178 178
179 if ((t = tp->t_tspec) == VOID) { 179 if ((t = tp->t_tspec) == VOID) {
180 return (1); 180 return (1);
181 } else if (t == ARRAY) { 181 } else if (t == ARRAY) {
182 return (tp->t_aincompl); 182 return (tp->t_aincompl);
183 } else if (t == STRUCT || t == UNION) { 183 } else if (t == STRUCT || t == UNION) {
184 return (tp->t_str->sincompl); 184 return (tp->t_str->sincompl);
185 } else if (t == ENUM) { 185 } else if (t == ENUM) {
186 return (tp->t_enum->eincompl); 186 return (tp->t_enum->eincompl);
187 } 187 }
188 return (0); 188 return (0);
189} 189}
190 190
191/* 191/*
192 * Set the flag for (in)complete array, struct, union or enum 192 * Set the flag for (in)complete array, struct, union or enum
193 * types. 193 * types.
194 */ 194 */
195void 195void
196setcompl(type_t *tp, int ic) 196setcompl(type_t *tp, int ic)
197{ 197{
198 tspec_t t; 198 tspec_t t;
199 199
200 if ((t = tp->t_tspec) == ARRAY) { 200 if ((t = tp->t_tspec) == ARRAY) {
201 tp->t_aincompl = ic; 201 tp->t_aincompl = ic;
202 } else if (t == STRUCT || t == UNION) { 202 } else if (t == STRUCT || t == UNION) {
203 tp->t_str->sincompl = ic; 203 tp->t_str->sincompl = ic;
204 } else { 204 } else {
205 if (t != ENUM) 205 if (t != ENUM)
206 LERROR("setcompl()"); 206 LERROR("setcompl()");
207 tp->t_enum->eincompl = ic; 207 tp->t_enum->eincompl = ic;
208 } 208 }
209} 209}
210 210
211/* 211/*
212 * Remember the storage class of the current declaration in dcs->d_scl 212 * Remember the storage class of the current declaration in dcs->d_scl
213 * (the top element of the declaration stack) and detect multiple 213 * (the top element of the declaration stack) and detect multiple
214 * storage classes. 214 * storage classes.
215 */ 215 */
216void 216void
217addscl(scl_t sc) 217addscl(scl_t sc)
218{ 218{
219 219
220 if (sc == INLINE) { 220 if (sc == INLINE) {
221 if (dcs->d_inline) 221 if (dcs->d_inline)
222 /* duplicate '%s' */ 222 /* duplicate '%s' */
223 warning(10, "inline"); 223 warning(10, "inline");
224 dcs->d_inline = 1; 224 dcs->d_inline = 1;
225 return; 225 return;
226 } 226 }
227 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 227 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
228 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) { 228 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
229 /* storage class after type is obsolescent */ 229 /* storage class after type is obsolescent */
230 warning(83); 230 warning(83);
231 } 231 }
232 if (dcs->d_scl == NOSCL) { 232 if (dcs->d_scl == NOSCL) {
233 dcs->d_scl = sc; 233 dcs->d_scl = sc;
234 } else { 234 } else {
235 /* 235 /*
236 * multiple storage classes. An error will be reported in 236 * multiple storage classes. An error will be reported in
237 * deftyp(). 237 * deftyp().
238 */ 238 */
239 dcs->d_mscl = 1; 239 dcs->d_mscl = 1;
240 } 240 }
241} 241}
242 242
243/* 243/*
244 * Remember the type, modifier or typedef name returned by the parser 244 * Remember the type, modifier or typedef name returned by the parser
245 * in *dcs (top element of decl stack). This information is used in 245 * in *dcs (top element of decl stack). This information is used in
246 * deftyp() to build the type used for all declarators in this 246 * deftyp() to build the type used for all declarators in this
247 * declaration. 247 * declaration.
248 * 248 *
249 * Is tp->t_typedef 1, the type comes from a previously defined typename. 249 * Is tp->t_typedef 1, the type comes from a previously defined typename.
250 * Otherwise it comes from a type specifier (int, long, ...) or a 250 * Otherwise it comes from a type specifier (int, long, ...) or a
251 * struct/union/enum tag. 251 * struct/union/enum tag.
252 */ 252 */
253void 253void
254addtype(type_t *tp) 254addtype(type_t *tp)
255{ 255{
256 tspec_t t; 256 tspec_t t;
257#ifdef DEBUG 257#ifdef DEBUG
258 char buf[1024]; 258 char buf[1024];
259 printf("addtype %s\n", tyname(buf, sizeof(buf), tp)); 259 printf("addtype %s\n", tyname(buf, sizeof(buf), tp));
260#endif 260#endif
261 if (tp->t_typedef) { 261 if (tp->t_typedef) {
262 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 262 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
263 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) { 263 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
264 /* 264 /*
265 * something like "typedef int a; int a b;" 265 * something like "typedef int a; int a b;"
266 * This should not happen with current grammar. 266 * This should not happen with current grammar.
267 */ 267 */
268 LERROR("addtype()"); 268 LERROR("addtype()");
269 } 269 }
270 dcs->d_type = tp; 270 dcs->d_type = tp;
271 return; 271 return;
272 } 272 }
273 273
274 t = tp->t_tspec; 274 t = tp->t_tspec;
275 275
276 if (t == STRUCT || t == UNION || t == ENUM) { 276 if (t == STRUCT || t == UNION || t == ENUM) {
277 /* 277 /*
278 * something like "int struct a ..." 278 * something like "int struct a ..."
279 * struct/union/enum with anything else is not allowed 279 * struct/union/enum with anything else is not allowed
280 */ 280 */
281 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 281 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
282 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) { 282 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
283 /* 283 /*
284 * remember that an error must be reported in 284 * remember that an error must be reported in
285 * deftyp(). 285 * deftyp().
286 */ 286 */
287 dcs->d_terr = 1; 287 dcs->d_terr = 1;
288 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC; 288 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
289 } 289 }
290 dcs->d_type = tp; 290 dcs->d_type = tp;
291 return; 291 return;
292 } 292 }
293 293
294 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { 294 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
295 /* 295 /*
296 * something like "struct a int" 296 * something like "struct a int"
297 * struct/union/enum with anything else is not allowed 297 * struct/union/enum with anything else is not allowed
298 */ 298 */
299 dcs->d_terr = 1; 299 dcs->d_terr = 1;
300 return; 300 return;
301 } 301 }
302 302
303 if (t == COMPLEX) { 303 if (t == COMPLEX) {
304 if (dcs->d_cmod == FLOAT) 304 if (dcs->d_cmod == FLOAT)
305 t = FCOMPLEX; 305 t = FCOMPLEX;
306 else if (dcs->d_cmod == DOUBLE) { 306 else if (dcs->d_cmod == DOUBLE) {
307 t = DCOMPLEX; 307 t = DCOMPLEX;
308 } else 308 } else
309 error(308, basictyname(dcs->d_cmod)); 309 error(308, basictyname(dcs->d_cmod));
310 dcs->d_cmod = NOTSPEC; 310 dcs->d_cmod = NOTSPEC;
311 } 311 }
312 312
313 if (t == LONG && dcs->d_lmod == LONG) { 313 if (t == LONG && dcs->d_lmod == LONG) {
314 /* "long long" or "long ... long" */ 314 /* "long long" or "long ... long" */
315 t = QUAD; 315 t = QUAD;
316 dcs->d_lmod = NOTSPEC; 316 dcs->d_lmod = NOTSPEC;
317 if (!quadflg) 317 if (!quadflg)
318 /* %s C does not support 'long long' */ 318 /* %s C does not support 'long long' */
319 (void)c99ism(265, tflag ? "traditional" : "c89"); 319 (void)c99ism(265, tflag ? "traditional" : "c89");
320 } 320 }
321 321
322 if (dcs->d_type != NULL && dcs->d_type->t_typedef) { 322 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
323 /* something like "typedef int a; a long ..." */ 323 /* something like "typedef int a; a long ..." */
324 dcs->d_type = tdeferr(dcs->d_type, t); 324 dcs->d_type = tdeferr(dcs->d_type, t);
325 return; 325 return;
326 } 326 }
327 327
328 /* now it can be only a combination of arithmetic types and void */ 328 /* now it can be only a combination of arithmetic types and void */
329 if (t == SIGNED || t == UNSIGN) { 329 if (t == SIGNED || t == UNSIGN) {
330 /* remember specifiers "signed" & "unsigned" in dcs->d_smod */ 330 /* remember specifiers "signed" & "unsigned" in dcs->d_smod */
331 if (dcs->d_smod != NOTSPEC) 331 if (dcs->d_smod != NOTSPEC)
332 /* 332 /*
333 * more than one "signed" and/or "unsigned"; print 333 * more than one "signed" and/or "unsigned"; print
334 * an error in deftyp() 334 * an error in deftyp()
335 */ 335 */
336 dcs->d_terr = 1; 336 dcs->d_terr = 1;
337 dcs->d_smod = t; 337 dcs->d_smod = t;
338 } else if (t == SHORT || t == LONG || t == QUAD) { 338 } else if (t == SHORT || t == LONG || t == QUAD) {
339 /* 339 /*
340 * remember specifiers "short", "long" and "long long" in 340 * remember specifiers "short", "long" and "long long" in
341 * dcs->d_lmod 341 * dcs->d_lmod
342 */ 342 */
343 if (dcs->d_lmod != NOTSPEC) 343 if (dcs->d_lmod != NOTSPEC)
344 /* more than one, print error in deftyp() */ 344 /* more than one, print error in deftyp() */
345 dcs->d_terr = 1; 345 dcs->d_terr = 1;
346 dcs->d_lmod = t; 346 dcs->d_lmod = t;
347 } else if (t == FLOAT || t == DOUBLE) { 347 } else if (t == FLOAT || t == DOUBLE) {
348 if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) { 348 if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) {
349 if (dcs->d_cmod != NOTSPEC 349 if (dcs->d_cmod != NOTSPEC
350 || (t == FLOAT && dcs->d_lmod == LONG)) 350 || (t == FLOAT && dcs->d_lmod == LONG))
351 dcs->d_terr = 1; 351 dcs->d_terr = 1;
352 dcs->d_cmod = t; 352 dcs->d_cmod = t;
353 } else { 353 } else {
354 if (dcs->d_atyp != NOTSPEC) 354 if (dcs->d_atyp != NOTSPEC)
355 dcs->d_terr = 1; 355 dcs->d_terr = 1;
356 dcs->d_atyp = t; 356 dcs->d_atyp = t;
357 } 357 }
358 } else { 358 } else {
359 /* 359 /*
360 * remember specifiers "void", "char", "int", 360 * remember specifiers "void", "char", "int",
361 * or "_Complex" int dcs->d_atyp 361 * or "_Complex" int dcs->d_atyp
362 */ 362 */
363 if (dcs->d_atyp != NOTSPEC) 363 if (dcs->d_atyp != NOTSPEC)
364 /* more than one, print error in deftyp() */ 364 /* more than one, print error in deftyp() */
365 dcs->d_terr = 1; 365 dcs->d_terr = 1;
366 dcs->d_atyp = t; 366 dcs->d_atyp = t;
367 } 367 }
368} 368}
369 369
370/* 370/*
371 * called if a list of declaration specifiers contains a typedef name 371 * called if a list of declaration specifiers contains a typedef name
372 * and other specifiers (except struct, union, enum, typedef name) 372 * and other specifiers (except struct, union, enum, typedef name)
373 */ 373 */
374static type_t * 374static type_t *
375tdeferr(type_t *td, tspec_t t) 375tdeferr(type_t *td, tspec_t t)
376{ 376{
377 tspec_t t2; 377 tspec_t t2;
378 378
379 t2 = td->t_tspec; 379 t2 = td->t_tspec;
380 380
381 switch (t) { 381 switch (t) {
382 case SIGNED: 382 case SIGNED:
383 case UNSIGN: 383 case UNSIGN:
384 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || 384 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
385 t2 == QUAD) { 385 t2 == QUAD) {
386 if (!tflag) 386 if (!tflag)
387 /* modifying typedef with ... */ 387 /* modifying typedef with ... */
388 warning(5, ttab[t].tt_name); 388 warning(5, ttab[t].tt_name);
389 td = duptyp(gettyp(mrgtspec(t2, t))); 389 td = duptyp(gettyp(mrgtspec(t2, t)));
390 td->t_typedef = 1; 390 td->t_typedef = 1;
391 return (td); 391 return (td);
392 } 392 }
393 break; 393 break;
394 case SHORT: 394 case SHORT:
395 if (t2 == INT || t2 == UINT) { 395 if (t2 == INT || t2 == UINT) {
396 /* modifying typedef with ... */ 396 /* modifying typedef with ... */
397 warning(5, "short"); 397 warning(5, "short");
398 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT)); 398 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
399 td->t_typedef = 1; 399 td->t_typedef = 1;
400 return (td); 400 return (td);
401 } 401 }
402 break; 402 break;
403 case LONG: 403 case LONG:
404 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG || 404 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
405 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) { 405 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
406 /* modifying typedef with ... */ 406 /* modifying typedef with ... */
407 warning(5, "long"); 407 warning(5, "long");
408 if (t2 == INT) { 408 if (t2 == INT) {
409 td = gettyp(LONG); 409 td = gettyp(LONG);
410 } else if (t2 == UINT) { 410 } else if (t2 == UINT) {
411 td = gettyp(ULONG); 411 td = gettyp(ULONG);
412 } else if (t2 == LONG) { 412 } else if (t2 == LONG) {
413 td = gettyp(QUAD); 413 td = gettyp(QUAD);
414 } else if (t2 == ULONG) { 414 } else if (t2 == ULONG) {
415 td = gettyp(UQUAD); 415 td = gettyp(UQUAD);
416 } else if (t2 == FLOAT) { 416 } else if (t2 == FLOAT) {
417 td = gettyp(DOUBLE); 417 td = gettyp(DOUBLE);
418 } else if (t2 == DOUBLE) { 418 } else if (t2 == DOUBLE) {
419 td = gettyp(LDOUBLE); 419 td = gettyp(LDOUBLE);
420 } else if (t2 == DCOMPLEX) { 420 } else if (t2 == DCOMPLEX) {
421 td = gettyp(LCOMPLEX); 421 td = gettyp(LCOMPLEX);
422 } 422 }
423 td = duptyp(td); 423 td = duptyp(td);
424 td->t_typedef = 1; 424 td->t_typedef = 1;
425 return (td); 425 return (td);
426 } 426 }
427 break; 427 break;
428 /* LINTED (enumeration values not handled in switch) */ 428 /* LINTED (enumeration values not handled in switch) */
429 case NOTSPEC: 429 case NOTSPEC:
430 case USHORT: 430 case USHORT:
431 case UCHAR: 431 case UCHAR:
432 case SCHAR: 432 case SCHAR:
433 case CHAR: 433 case CHAR:
434 case BOOL: 434 case BOOL:
435 case FUNC: 435 case FUNC:
436 case ARRAY: 436 case ARRAY:
437 case PTR: 437 case PTR:
438 case ENUM: 438 case ENUM:
439 case UNION: 439 case UNION:
440 case STRUCT: 440 case STRUCT:
441 case VOID: 441 case VOID:
442 case LDOUBLE: 442 case LDOUBLE:
443 case FLOAT: 443 case FLOAT:
444 case DOUBLE: 444 case DOUBLE:
445 case UQUAD: 445 case UQUAD:
446 case QUAD: 446 case QUAD:
447 case ULONG: 447 case ULONG:
448 case UINT: 448 case UINT:
449 case INT: 449 case INT:
450 case FCOMPLEX: 450 case FCOMPLEX:
451 case DCOMPLEX: 451 case DCOMPLEX:
452 case LCOMPLEX: 452 case LCOMPLEX:
453 case COMPLEX: 453 case COMPLEX:
454 break; 454 break;
455 455
456 case NTSPEC: /* this value unused */ 456 case NTSPEC: /* this value unused */
457 break; 457 break;
458 } 458 }
459 459
460 /* Anything other is not accepted. */ 460 /* Anything other is not accepted. */
461 461
462 dcs->d_terr = 1; 462 dcs->d_terr = 1;
463 return (td); 463 return (td);
464} 464}
465 465
466/* 466/*
467 * Remember the symbol of a typedef name (2nd arg) in a struct, union 467 * Remember the symbol of a typedef name (2nd arg) in a struct, union
468 * or enum tag if the typedef name is the first defined for this tag. 468 * or enum tag if the typedef name is the first defined for this tag.
469 * 469 *
470 * If the tag is unnamed, the typdef name is used for identification 470 * If the tag is unnamed, the typdef name is used for identification
471 * of this tag in lint2. Although its possible that more than one typedef 471 * of this tag in lint2. Although its possible that more than one typedef
472 * name is defined for one tag, the first name defined should be unique 472 * name is defined for one tag, the first name defined should be unique
473 * if the tag is unnamed. 473 * if the tag is unnamed.
474 */ 474 */
475static void 475static void
476settdsym(type_t *tp, sym_t *sym) 476settdsym(type_t *tp, sym_t *sym)
477{ 477{
478 tspec_t t; 478 tspec_t t;
479 479
480 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 480 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
481 if (tp->t_str->stdef == NULL) 481 if (tp->t_str->stdef == NULL)
482 tp->t_str->stdef = sym; 482 tp->t_str->stdef = sym;
483 } else if (t == ENUM) { 483 } else if (t == ENUM) {
484 if (tp->t_enum->etdef == NULL) 484 if (tp->t_enum->etdef == NULL)
485 tp->t_enum->etdef = sym; 485 tp->t_enum->etdef = sym;
486 } 486 }
487} 487}
488 488
489static void 489static void
490setpackedsize(type_t *tp) 490setpackedsize(type_t *tp)
491{ 491{
492 str_t *sp; 492 str_t *sp;
493 sym_t *mem; 493 sym_t *mem;
494 char buf[256]; 494 char buf[256];
495 495
496 switch (tp->t_tspec) { 496 switch (tp->t_tspec) {
497 case STRUCT: 497 case STRUCT:
498 sp = tp->t_str; 
499 sp->size = 0; 
500 for (mem = sp->memb; mem != NULL; mem = mem->s_nxt) { 
501 size_t x = (size_t)tsize(mem->s_type); 
502 sp->size += x; 
503 } 
504 break; 
505 case UNION: 498 case UNION:
506 sp = tp->t_str; 499 sp = tp->t_str;
507 sp->size = 0; 500 sp->size = 0;
508 for (mem = sp->memb; mem != NULL; mem = mem->s_nxt) { 501 for (mem = sp->memb; mem != NULL; mem = mem->s_nxt) {
 502 if (mem->s_type->t_isfield) {
 503 size_t len = mem->s_type->t_flen;
 504 while (mem && mem->s_type->t_isfield) {
 505 len += mem->s_type->t_flen;
 506 mem = mem->s_nxt;
 507 }
 508 len = ((len + INT_SIZE - 1) /
 509 INT_SIZE) * INT_SIZE;
 510 sp->size += len;
 511 if (mem == NULL)
 512 break;
 513 }
509 size_t x = (size_t)tsize(mem->s_type); 514 size_t x = (size_t)tsize(mem->s_type);
510 if (x > sp->size) 515 if (tp->t_tspec == STRUCT)
 516 sp->size += x;
 517 else if (x > sp->size)
511 sp->size = x; 518 sp->size = x;
512 } 519 }
513 break; 520 break;
514 default: 521 default:
515 warning(326, "packed", tyname(buf, sizeof(buf), tp)); 522 warning(326, "packed", tyname(buf, sizeof(buf), tp));
516 break; 523 break;
517 } 524 }
518} 525}
519 526
520void 527void
521addpacked(void) 528addpacked(void)
522{ 529{
523 if (dcs->d_type == NULL) 530 if (dcs->d_type == NULL)
524 dcs->d_ispacked = 1; 531 dcs->d_ispacked = 1;
525 else 532 else
526 setpackedsize(dcs->d_type); 533 setpackedsize(dcs->d_type);
527} 534}
528 535
529/* 536/*
530 * Remember a qualifier which is part of the declaration specifiers 537 * Remember a qualifier which is part of the declaration specifiers
531 * (and not the declarator) in the top element of the declaration stack. 538 * (and not the declarator) in the top element of the declaration stack.
532 * Also detect multiple qualifiers of the same kind. 539 * Also detect multiple qualifiers of the same kind.
533 540
534 * The remembered qualifier is used by deftyp() to construct the type 541 * The remembered qualifier is used by deftyp() to construct the type
535 * for all declarators. 542 * for all declarators.
536 */ 543 */
537void 544void
538addqual(tqual_t q) 545addqual(tqual_t q)
539{ 546{
540 547
541 if (q == CONST) { 548 if (q == CONST) {
542 if (dcs->d_const) { 549 if (dcs->d_const) {
543 /* duplicate "%s" */ 550 /* duplicate "%s" */
544 warning(10, "const"); 551 warning(10, "const");
545 } 552 }
546 dcs->d_const = 1; 553 dcs->d_const = 1;
547 } else { 554 } else {
548 if (q != VOLATILE) 555 if (q != VOLATILE)
549 LERROR("addqual()"); 556 LERROR("addqual()");
550 if (dcs->d_volatile) { 557 if (dcs->d_volatile) {
551 /* duplicate "%s" */ 558 /* duplicate "%s" */
552 warning(10, "volatile"); 559 warning(10, "volatile");
553 } 560 }
554 dcs->d_volatile = 1; 561 dcs->d_volatile = 1;
555 } 562 }
556} 563}
557 564
558/* 565/*
559 * Go to the next declaration level (structs, nested structs, blocks, 566 * Go to the next declaration level (structs, nested structs, blocks,
560 * argument declaration lists ...) 567 * argument declaration lists ...)
561 */ 568 */
562void 569void
563pushdecl(scl_t sc) 570pushdecl(scl_t sc)
564{ 571{
565 dinfo_t *di; 572 dinfo_t *di;
566 573
567 /* put a new element on the declaration stack */ 574 /* put a new element on the declaration stack */
568 di = xcalloc(1, sizeof (dinfo_t)); 575 di = xcalloc(1, sizeof (dinfo_t));
569 di->d_nxt = dcs; 576 di->d_nxt = dcs;
570 dcs = di; 577 dcs = di;
571 di->d_ctx = sc; 578 di->d_ctx = sc;
572 di->d_ldlsym = &di->d_dlsyms; 579 di->d_ldlsym = &di->d_dlsyms;
573 if (dflag) 580 if (dflag)
574 (void)printf("pushdecl(%p %d)\n", dcs, (int)sc); 581 (void)printf("pushdecl(%p %d)\n", dcs, (int)sc);
575 582
576} 583}
577 584
578/* 585/*
579 * Go back to previous declaration level 586 * Go back to previous declaration level
580 */ 587 */
581void 588void
582popdecl(void) 589popdecl(void)
583{ 590{
584 dinfo_t *di; 591 dinfo_t *di;
585 592
586 if (dflag) 593 if (dflag)
587 (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx); 594 (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx);
588 595
589 if (dcs->d_nxt == NULL) 596 if (dcs->d_nxt == NULL)
590 LERROR("popdecl()"); 597 LERROR("popdecl()");
591 di = dcs; 598 di = dcs;
592 dcs = di->d_nxt; 599 dcs = di->d_nxt;
593 switch (di->d_ctx) { 600 switch (di->d_ctx) {
594 case EXTERN: 601 case EXTERN:
595 /* there is nothing after external declarations */ 602 /* there is nothing after external declarations */
596 LERROR("popdecl()"); 603 LERROR("popdecl()");
597 /* NOTREACHED */ 604 /* NOTREACHED */
598 case MOS: 605 case MOS:
599 case MOU: 606 case MOU:
600 case ENUMCON: 607 case ENUMCON:
601 /* 608 /*
602 * Symbols declared in (nested) structs or enums are 609 * Symbols declared in (nested) structs or enums are
603 * part of the next level (they are removed from the 610 * part of the next level (they are removed from the
604 * symbol table if the symbols of the outher level are 611 * symbol table if the symbols of the outher level are
605 * removed) 612 * removed)
606 */ 613 */
607 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 614 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
608 dcs->d_ldlsym = di->d_ldlsym; 615 dcs->d_ldlsym = di->d_ldlsym;
609 break; 616 break;
610 case ARG: 617 case ARG:
611 /* 618 /*
612 * All symbols in dcs->d_dlsyms are introduced in old style 619 * All symbols in dcs->d_dlsyms are introduced in old style
613 * argument declarations (it's not clean, but possible). 620 * argument declarations (it's not clean, but possible).
614 * They are appended to the list of symbols declared in 621 * They are appended to the list of symbols declared in
615 * an old style argument identifier list or a new style 622 * an old style argument identifier list or a new style
616 * parameter type list. 623 * parameter type list.
617 */ 624 */
618 if (di->d_dlsyms != NULL) { 625 if (di->d_dlsyms != NULL) {
619 *di->d_ldlsym = dcs->d_fpsyms; 626 *di->d_ldlsym = dcs->d_fpsyms;
620 dcs->d_fpsyms = di->d_dlsyms; 627 dcs->d_fpsyms = di->d_dlsyms;
621 } 628 }
622 break; 629 break;
623 case ABSTRACT: 630 case ABSTRACT:
624 /* 631 /*
625 * casts and sizeof 632 * casts and sizeof
626 * Append all symbols declared in the abstract declaration 633 * Append all symbols declared in the abstract declaration
627 * to the list of symbols declared in the surounding decl. 634 * to the list of symbols declared in the surounding decl.
628 * or block. 635 * or block.
629 * XXX I'm not sure whether they should be removed from the 636 * XXX I'm not sure whether they should be removed from the
630 * symbol table now or later. 637 * symbol table now or later.
631 */ 638 */
632 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 639 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
633 dcs->d_ldlsym = di->d_ldlsym; 640 dcs->d_ldlsym = di->d_ldlsym;
634 break; 641 break;
635 case AUTO: 642 case AUTO:
636 /* check usage of local vars */ 643 /* check usage of local vars */
637 chkusage(di); 644 chkusage(di);
638 /* FALLTHROUGH */ 645 /* FALLTHROUGH */
639 case PARG: 646 case PARG:
640 /* usage of arguments will be checked by funcend() */ 647 /* usage of arguments will be checked by funcend() */
641 rmsyms(di->d_dlsyms); 648 rmsyms(di->d_dlsyms);
642 break; 649 break;
643 default: 650 default:
644 LERROR("popdecl()"); 651 LERROR("popdecl()");
645 } 652 }
646 free(di); 653 free(di);
647} 654}
648 655
649/* 656/*
650 * Set flag d_asm in all declaration stack elements up to the 657 * Set flag d_asm in all declaration stack elements up to the
651 * outermost one. 658 * outermost one.
652 * 659 *
653 * This is used to mark compound statements which have, possibly in 660 * This is used to mark compound statements which have, possibly in
654 * nested compound statements, asm statements. For these compound 661 * nested compound statements, asm statements. For these compound
655 * statements no warnings about unused or unitialized variables are 662 * statements no warnings about unused or unitialized variables are
656 * printed. 663 * printed.
657 * 664 *
658 * There is no need to clear d_asm in dinfo structs with context AUTO, 665 * There is no need to clear d_asm in dinfo structs with context AUTO,
659 * because these structs are freed at the end of the compound statement. 666 * because these structs are freed at the end of the compound statement.
660 * But it must be cleard in the outermost dinfo struct, which has 667 * But it must be cleard in the outermost dinfo struct, which has
661 * context EXTERN. This could be done in clrtyp() and would work for 668 * context EXTERN. This could be done in clrtyp() and would work for
662 * C, but not for C++ (due to mixed statements and declarations). Thus 669 * C, but not for C++ (due to mixed statements and declarations). Thus
663 * we clear it in glclup(), which is used to do some cleanup after 670 * we clear it in glclup(), which is used to do some cleanup after
664 * global declarations/definitions. 671 * global declarations/definitions.
665 */ 672 */
666void 673void
667setasm(void) 674setasm(void)
668{ 675{
669 dinfo_t *di; 676 dinfo_t *di;
670 677
671 for (di = dcs; di != NULL; di = di->d_nxt) 678 for (di = dcs; di != NULL; di = di->d_nxt)
672 di->d_asm = 1; 679 di->d_asm = 1;
673} 680}
674 681
675/* 682/*
676 * Clean all elements of the top element of declaration stack which 683 * Clean all elements of the top element of declaration stack which
677 * will be used by the next declaration 684 * will be used by the next declaration
678 */ 685 */
679void 686void
680clrtyp(void) 687clrtyp(void)
681{ 688{
682 689
683 dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC; 690 dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC;
684 dcs->d_scl = NOSCL; 691 dcs->d_scl = NOSCL;
685 dcs->d_type = NULL; 692 dcs->d_type = NULL;
686 dcs->d_const = dcs->d_volatile = 0; 693 dcs->d_const = dcs->d_volatile = 0;
687 dcs->d_inline = 0; 694 dcs->d_inline = 0;
688 dcs->d_mscl = dcs->d_terr = 0; 695 dcs->d_mscl = dcs->d_terr = 0;
689 dcs->d_nedecl = 0; 696 dcs->d_nedecl = 0;
690 dcs->d_notyp = 0; 697 dcs->d_notyp = 0;
691} 698}
692 699
693/* 700/*
694 * Create a type structure from the informations gathered in 701 * Create a type structure from the informations gathered in
695 * the declaration stack. 702 * the declaration stack.
696 * Complain about storage classes which are not possible in current 703 * Complain about storage classes which are not possible in current
697 * context. 704 * context.
698 */ 705 */
699void 706void
700deftyp(void) 707deftyp(void)
701{ 708{
702 tspec_t t, s, l, c; 709 tspec_t t, s, l, c;
703 type_t *tp; 710 type_t *tp;
704 scl_t scl; 711 scl_t scl;
705 712
706 t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */ 713 t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */
707 s = dcs->d_smod; /* SIGNED, UNSIGNED */ 714 s = dcs->d_smod; /* SIGNED, UNSIGNED */
708 l = dcs->d_lmod; /* SHORT, LONG, QUAD */ 715 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
709 c = dcs->d_cmod; /* FLOAT, DOUBLE */ 716 c = dcs->d_cmod; /* FLOAT, DOUBLE */
710 tp = dcs->d_type; 717 tp = dcs->d_type;
711 scl = dcs->d_scl; 718 scl = dcs->d_scl;
712 719
713 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC && 720 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
714 tp == NULL) 721 tp == NULL)
715 dcs->d_notyp = 1; 722 dcs->d_notyp = 1;
716 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) && 723 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
717 tp == NULL) 724 tp == NULL)
718 t = c; 725 t = c;
719 726
720 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) { 727 if (tp != NULL && (t != NOTSPEC || s != NOTSPEC || l != NOTSPEC)) {
721 /* should never happen */ 728 /* should never happen */
722 LERROR("deftyp()"); 729 LERROR("deftyp()");
723 } 730 }
724 731
725 if (tp == NULL) { 732 if (tp == NULL) {
726 switch (t) { 733 switch (t) {
727 case BOOL: 734 case BOOL:
728 break; 735 break;
729 case NOTSPEC: 736 case NOTSPEC:
730 t = INT; 737 t = INT;
731 /* FALLTHROUGH */ 738 /* FALLTHROUGH */
732 case INT: 739 case INT:
733 if (s == NOTSPEC) 740 if (s == NOTSPEC)
734 s = SIGNED; 741 s = SIGNED;
735 break; 742 break;
736 case CHAR: 743 case CHAR:
737 if (l != NOTSPEC) { 744 if (l != NOTSPEC) {
738 dcs->d_terr = 1; 745 dcs->d_terr = 1;
739 l = NOTSPEC; 746 l = NOTSPEC;
740 } 747 }
741 break; 748 break;
742 case FLOAT: 749 case FLOAT:
743 if (l == LONG) { 750 if (l == LONG) {
744 l = NOTSPEC; 751 l = NOTSPEC;
745 t = DOUBLE; 752 t = DOUBLE;
746 if (!tflag) 753 if (!tflag)
747 /* use 'double' instead of ... */ 754 /* use 'double' instead of ... */
748 warning(6); 755 warning(6);
749 } 756 }
750 break; 757 break;
751 case DOUBLE: 758 case DOUBLE:
752 if (l == LONG) { 759 if (l == LONG) {
753 l = NOTSPEC; 760 l = NOTSPEC;
754 t = LDOUBLE; 761 t = LDOUBLE;
755 if (tflag) 762 if (tflag)
756 /* 'long double' is illegal in ... */ 763 /* 'long double' is illegal in ... */
757 warning(266); 764 warning(266);
758 } 765 }
759 break; 766 break;
760 case DCOMPLEX: 767 case DCOMPLEX:
761 if (l == LONG) { 768 if (l == LONG) {
762 l = NOTSPEC; 769 l = NOTSPEC;
763 t = LCOMPLEX; 770 t = LCOMPLEX;
764 if (tflag) 771 if (tflag)
765 /* 'long double' is illegal in ... */ 772 /* 'long double' is illegal in ... */
766 warning(266); 773 warning(266);
767 } 774 }
768 break; 775 break;
769 case VOID: 776 case VOID:
770 case FCOMPLEX: 777 case FCOMPLEX:
771 case LCOMPLEX: 778 case LCOMPLEX:
772 break; 779 break;
773 default: 780 default:
774 LERROR("deftyp()"); 781 LERROR("deftyp()");
775 } 782 }
776 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { 783 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
777 dcs->d_terr = 1; 784 dcs->d_terr = 1;
778 l = s = NOTSPEC; 785 l = s = NOTSPEC;
779 } 786 }
780 if (l != NOTSPEC) 787 if (l != NOTSPEC)
781 t = l; 788 t = l;
782 dcs->d_type = gettyp(mrgtspec(t, s)); 789 dcs->d_type = gettyp(mrgtspec(t, s));
783 } 790 }
784 791
785 if (dcs->d_mscl) { 792 if (dcs->d_mscl) {
786 /* only one storage class allowed */ 793 /* only one storage class allowed */
787 error(7); 794 error(7);
788 } 795 }
789 if (dcs->d_terr) { 796 if (dcs->d_terr) {
790 /* illegal type combination */ 797 /* illegal type combination */
791 error(4); 798 error(4);
792 } 799 }
793 800
794 if (dcs->d_ctx == EXTERN) { 801 if (dcs->d_ctx == EXTERN) {
795 if (scl == REG || scl == AUTO) { 802 if (scl == REG || scl == AUTO) {
796 /* illegal storage class */ 803 /* illegal storage class */
797 error(8); 804 error(8);
798 scl = NOSCL; 805 scl = NOSCL;
799 } 806 }
800 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) { 807 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
801 if (scl != NOSCL && scl != REG) { 808 if (scl != NOSCL && scl != REG) {
802 /* only "register" valid ... */ 809 /* only "register" valid ... */
803 error(9); 810 error(9);
804 scl = NOSCL; 811 scl = NOSCL;
805 } 812 }
806 } 813 }
807 814
808 dcs->d_scl = scl; 815 dcs->d_scl = scl;
809 816
810 if (dcs->d_const && dcs->d_type->t_const) { 817 if (dcs->d_const && dcs->d_type->t_const) {
811 if (!dcs->d_type->t_typedef) 818 if (!dcs->d_type->t_typedef)
812 LERROR("deftyp()"); 819 LERROR("deftyp()");
813 /* typedef already qualified with "%s" */ 820 /* typedef already qualified with "%s" */
814 warning(68, "const"); 821 warning(68, "const");
815 } 822 }
816 if (dcs->d_volatile && dcs->d_type->t_volatile) { 823 if (dcs->d_volatile && dcs->d_type->t_volatile) {
817 if (!dcs->d_type->t_typedef) 824 if (!dcs->d_type->t_typedef)
818 LERROR("deftyp()"); 825 LERROR("deftyp()");
819 /* typedef already qualified with "%s" */ 826 /* typedef already qualified with "%s" */
820 warning(68, "volatile"); 827 warning(68, "volatile");
821 } 828 }
822 829
823 if (dcs->d_const || dcs->d_volatile) { 830 if (dcs->d_const || dcs->d_volatile) {
824 dcs->d_type = duptyp(dcs->d_type); 831 dcs->d_type = duptyp(dcs->d_type);
825 dcs->d_type->t_const |= dcs->d_const; 832 dcs->d_type->t_const |= dcs->d_const;
826 dcs->d_type->t_volatile |= dcs->d_volatile; 833 dcs->d_type->t_volatile |= dcs->d_volatile;
827 } 834 }
828} 835}
829 836
830/* 837/*
831 * Merge type specifiers (char, ..., long long, signed, unsigned). 838 * Merge type specifiers (char, ..., long long, signed, unsigned).
832 */ 839 */
833static tspec_t 840static tspec_t
834mrgtspec(tspec_t t, tspec_t s) 841mrgtspec(tspec_t t, tspec_t s)
835{ 842{
836 843
837 if (s == SIGNED || s == UNSIGN) { 844 if (s == SIGNED || s == UNSIGN) {
838 if (t == CHAR) { 845 if (t == CHAR) {
839 t = s == SIGNED ? SCHAR : UCHAR; 846 t = s == SIGNED ? SCHAR : UCHAR;
840 } else if (t == SHORT) { 847 } else if (t == SHORT) {
841 t = s == SIGNED ? SHORT : USHORT; 848 t = s == SIGNED ? SHORT : USHORT;
842 } else if (t == INT) { 849 } else if (t == INT) {
843 t = s == SIGNED ? INT : UINT; 850 t = s == SIGNED ? INT : UINT;
844 } else if (t == LONG) { 851 } else if (t == LONG) {
845 t = s == SIGNED ? LONG : ULONG; 852 t = s == SIGNED ? LONG : ULONG;
846 } else if (t == QUAD) { 853 } else if (t == QUAD) {
847 t = s == SIGNED ? QUAD : UQUAD; 854 t = s == SIGNED ? QUAD : UQUAD;
848 } 855 }
849 } 856 }
850 857
851 return (t); 858 return (t);
852} 859}
853 860
854/* 861/*
855 * Return the length of a type in bit. 862 * Return the length of a type in bit.
856 * 863 *
857 * Printing a message if the outhermost dimension of an array is 0 must 864 * Printing a message if the outhermost dimension of an array is 0 must
858 * be done by the caller. All other problems are reported by length() 865 * be done by the caller. All other problems are reported by length()
859 * if name is not NULL. 866 * if name is not NULL.
860 */ 867 */
861int 868int
862length(type_t *tp, const char *name) 869length(type_t *tp, const char *name)
863{ 870{
864 int elem, elsz; 871 int elem, elsz;
865 872
866 elem = 1; 873 elem = 1;
867 while (tp && tp->t_tspec == ARRAY) { 874 while (tp && tp->t_tspec == ARRAY) {
868 elem *= tp->t_dim; 875 elem *= tp->t_dim;
869 tp = tp->t_subt; 876 tp = tp->t_subt;
870 } 877 }
871 if (tp == NULL) 878 if (tp == NULL)
872 return -1; 879 return -1;
873 880
874 switch (tp->t_tspec) { 881 switch (tp->t_tspec) {
875 case FUNC: 882 case FUNC:
876 /* compiler takes size of function */ 883 /* compiler takes size of function */
877 LERROR(msgs[12]); 884 LERROR(msgs[12]);
878 /* NOTREACHED */ 885 /* NOTREACHED */
879 case STRUCT: 886 case STRUCT:
880 case UNION: 887 case UNION:
881 if (incompl(tp) && name != NULL) { 888 if (incompl(tp) && name != NULL) {
882 /* incomplete structure or union %s: %s */ 889 /* incomplete structure or union %s: %s */
883 error(31, tp->t_str->stag->s_name, name); 890 error(31, tp->t_str->stag->s_name, name);
884 } 891 }
885 elsz = tp->t_str->size; 892 elsz = tp->t_str->size;
886 break; 893 break;
887 case ENUM: 894 case ENUM:
888 if (incompl(tp) && name != NULL) { 895 if (incompl(tp) && name != NULL) {
889 /* incomplete enum type: %s */ 896 /* incomplete enum type: %s */
890 warning(13, name); 897 warning(13, name);
891 } 898 }
892 /* FALLTHROUGH */ 899 /* FALLTHROUGH */
893 default: 900 default:
894 elsz = size(tp->t_tspec); 901 elsz = size(tp->t_tspec);
895 if (elsz <= 0) 902 if (elsz <= 0)
896 LERROR("length()"); 903 LERROR("length()");
897 break; 904 break;
898 } 905 }
899 return (elem * elsz); 906 return (elem * elsz);
900} 907}
901 908
902/* 909/*
903 * Get the alignment of the given Type in bits. 910 * Get the alignment of the given Type in bits.
904 */ 911 */
905int 912int
906getbound(type_t *tp) 913getbound(type_t *tp)
907{ 914{
908 size_t a; 915 size_t a;
909 tspec_t t; 916 tspec_t t;
910 917
911 while (tp && tp->t_tspec == ARRAY) 918 while (tp && tp->t_tspec == ARRAY)
912 tp = tp->t_subt; 919 tp = tp->t_subt;
913 920
914 if (tp == NULL) 921 if (tp == NULL)
915 return -1; 922 return -1;
916 923
917 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 924 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
918 a = tp->t_str->align; 925 a = tp->t_str->align;
919 } else if (t == FUNC) { 926 } else if (t == FUNC) {
920 /* compiler takes alignment of function */ 927 /* compiler takes alignment of function */
921 error(14); 928 error(14);
922 a = ALIGN(1) * CHAR_BIT; 929 a = ALIGN(1) * CHAR_BIT;
923 } else { 930 } else {
924 if ((a = size(t)) == 0) { 931 if ((a = size(t)) == 0) {
925 a = CHAR_BIT; 932 a = CHAR_BIT;
926 } else if (a > ALIGN(1) * CHAR_BIT) { 933 } else if (a > ALIGN(1) * CHAR_BIT) {
927 a = ALIGN(1) * CHAR_BIT; 934 a = ALIGN(1) * CHAR_BIT;
928 } 935 }
929 } 936 }
930 if (a < CHAR_BIT || a > ALIGN(1) * CHAR_BIT) 937 if (a < CHAR_BIT || a > ALIGN(1) * CHAR_BIT)
931 LERROR("getbound()"); 938 LERROR("getbound()");
932 return (a); 939 return (a);
933} 940}
934 941
935/* 942/*
936 * Concatenate two lists of symbols by s_nxt. Used by declarations of 943 * Concatenate two lists of symbols by s_nxt. Used by declarations of
937 * struct/union/enum elements and parameters. 944 * struct/union/enum elements and parameters.
938 */ 945 */
939sym_t * 946sym_t *
940lnklst(sym_t *l1, sym_t *l2) 947lnklst(sym_t *l1, sym_t *l2)
941{ 948{
942 sym_t *l; 949 sym_t *l;
943 950
944 if ((l = l1) == NULL) 951 if ((l = l1) == NULL)
945 return (l2); 952 return (l2);
946 while (l1->s_nxt != NULL) 953 while (l1->s_nxt != NULL)
947 l1 = l1->s_nxt; 954 l1 = l1->s_nxt;
948 l1->s_nxt = l2; 955 l1->s_nxt = l2;
949 return (l); 956 return (l);
950} 957}
951 958
952/* 959/*
953 * Check if the type of the given symbol is valid and print an error 960 * Check if the type of the given symbol is valid and print an error
954 * message if it is not. 961 * message if it is not.
955 * 962 *
956 * Invalid types are: 963 * Invalid types are:
957 * - arrays of incomlete types or functions 964 * - arrays of incomlete types or functions
958 * - functions returning arrays or functions 965 * - functions returning arrays or functions
959 * - void types other than type of function or pointer 966 * - void types other than type of function or pointer
960 */ 967 */
961void 968void
962chktyp(sym_t *sym) 969chktyp(sym_t *sym)
963{ 970{
964 tspec_t to, t; 971 tspec_t to, t;
965 type_t **tpp, *tp; 972 type_t **tpp, *tp;
966 973
967 tpp = &sym->s_type; 974 tpp = &sym->s_type;
968 to = NOTSPEC; 975 to = NOTSPEC;
969 while ((tp = *tpp) != NULL) { 976 while ((tp = *tpp) != NULL) {
970 t = tp->t_tspec; 977 t = tp->t_tspec;
971 /* 978 /*
972 * If this is the type of an old style function definition, 979 * If this is the type of an old style function definition,
973 * a better warning is printed in funcdef(). 980 * a better warning is printed in funcdef().
974 */ 981 */
975 if (t == FUNC && !tp->t_proto && 982 if (t == FUNC && !tp->t_proto &&
976 !(to == NOTSPEC && sym->s_osdef)) { 983 !(to == NOTSPEC && sym->s_osdef)) {
977 if (sflag && hflag) 984 if (sflag && hflag)
978 /* function declaration is not a prototype */ 985 /* function declaration is not a prototype */
979 warning(287); 986 warning(287);
980 } 987 }
981 if (to == FUNC) { 988 if (to == FUNC) {
982 if (t == FUNC || t == ARRAY) { 989 if (t == FUNC || t == ARRAY) {
983 /* function returns illegal type */ 990 /* function returns illegal type */
984 error(15); 991 error(15);
985 if (t == FUNC) { 992 if (t == FUNC) {
986 *tpp = incref(*tpp, PTR); 993 *tpp = incref(*tpp, PTR);
987 } else { 994 } else {
988 *tpp = incref((*tpp)->t_subt, PTR); 995 *tpp = incref((*tpp)->t_subt, PTR);
989 } 996 }
990 return; 997 return;
991 } else if (tp->t_const || tp->t_volatile) { 998 } else if (tp->t_const || tp->t_volatile) {
992 if (sflag) { /* XXX oder better !tflag ? */ 999 if (sflag) { /* XXX oder better !tflag ? */
993 /* function cannot return const... */ 1000 /* function cannot return const... */
994 warning(228); 1001 warning(228);
995 } 1002 }
996 } 1003 }
997 } if (to == ARRAY) { 1004 } if (to == ARRAY) {
998 if (t == FUNC) { 1005 if (t == FUNC) {
999 /* array of function is illegal */ 1006 /* array of function is illegal */
1000 error(16); 1007 error(16);
1001 *tpp = gettyp(INT); 1008 *tpp = gettyp(INT);
1002 return; 1009 return;
1003 } else if (t == ARRAY && tp->t_dim == 0) { 1010 } else if (t == ARRAY && tp->t_dim == 0) {
1004 /* null dimension */ 1011 /* null dimension */
1005 error(17); 1012 error(17);
1006 return; 1013 return;
1007 } else if (t == VOID) { 1014 } else if (t == VOID) {
1008 /* illegal use of void */ 1015 /* illegal use of void */
1009 error(18); 1016 error(18);
1010 *tpp = gettyp(INT); 1017 *tpp = gettyp(INT);
1011#if 0 /* errors are produced by length() */ 1018#if 0 /* errors are produced by length() */
1012 } else if (incompl(tp)) { 1019 } else if (incompl(tp)) {
1013 /* array of incomplete type */ 1020 /* array of incomplete type */
1014 if (sflag) { 1021 if (sflag) {
1015 error(301); 1022 error(301);
1016 } else { 1023 } else {
1017 warning(301); 1024 warning(301);
1018 } 1025 }
1019#endif 1026#endif
1020 } 1027 }
1021 } else if (to == NOTSPEC && t == VOID) { 1028 } else if (to == NOTSPEC && t == VOID) {
1022 if (dcs->d_ctx == PARG) { 1029 if (dcs->d_ctx == PARG) {
1023 if (sym->s_scl != ABSTRACT) { 1030 if (sym->s_scl != ABSTRACT) {
1024 if (sym->s_name == unnamed) 1031 if (sym->s_name == unnamed)
1025 LERROR("chktyp()"); 1032 LERROR("chktyp()");
1026 /* void param cannot have name: %s */ 1033 /* void param cannot have name: %s */
1027 error(61, sym->s_name); 1034 error(61, sym->s_name);
1028 *tpp = gettyp(INT); 1035 *tpp = gettyp(INT);
1029 } 1036 }
1030 } else if (dcs->d_ctx == ABSTRACT) { 1037 } else if (dcs->d_ctx == ABSTRACT) {
1031 /* ok */ 1038 /* ok */
1032 } else if (sym->s_scl != TYPEDEF) { 1039 } else if (sym->s_scl != TYPEDEF) {
1033 /* void type for %s */ 1040 /* void type for %s */
1034 error(19, sym->s_name); 1041 error(19, sym->s_name);
1035 *tpp = gettyp(INT); 1042 *tpp = gettyp(INT);
1036 } 1043 }
1037 } 1044 }
1038 if (t == VOID && to != PTR) { 1045 if (t == VOID && to != PTR) {
1039 if (tp->t_const || tp->t_volatile) { 1046 if (tp->t_const || tp->t_volatile) {
1040 /* inappropriate qualifiers with "void" */ 1047 /* inappropriate qualifiers with "void" */
1041 warning(69); 1048 warning(69);
1042 tp->t_const = tp->t_volatile = 0; 1049 tp->t_const = tp->t_volatile = 0;
1043 } 1050 }
1044 } 1051 }
1045 tpp = &tp->t_subt; 1052 tpp = &tp->t_subt;
1046 to = t; 1053 to = t;
1047 } 1054 }
1048} 1055}
1049 1056
1050/* 1057/*
1051 * Process the declarator of a struct/union element. 1058 * Process the declarator of a struct/union element.
1052 */ 1059 */
1053sym_t * 1060sym_t *
1054decl1str(sym_t *dsym) 1061decl1str(sym_t *dsym)
1055{ 1062{
1056 type_t *tp; 1063 type_t *tp;
1057 tspec_t t; 1064 tspec_t t;
1058 int sz, len; 1065 int sz, len;
1059 int o = 0; /* Appease gcc */ 1066 int o = 0; /* Appease gcc */
1060 scl_t sc; 1067 scl_t sc;
1061 1068
1062 if ((sc = dsym->s_scl) != MOS && sc != MOU) 1069 if ((sc = dsym->s_scl) != MOS && sc != MOU)
1063 LERROR("decl1str()"); 1070 LERROR("decl1str()");
1064 1071
1065 if (dcs->d_rdcsym != NULL) { 1072 if (dcs->d_rdcsym != NULL) {
1066 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU) 1073 if ((sc = dcs->d_rdcsym->s_scl) != MOS && sc != MOU)
1067 /* should be ensured by storesym() */ 1074 /* should be ensured by storesym() */
1068 LERROR("decl1str()"); 1075 LERROR("decl1str()");
1069 if (dsym->s_styp == dcs->d_rdcsym->s_styp) { 1076 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1070 /* duplicate member name: %s */ 1077 /* duplicate member name: %s */
1071 error(33, dsym->s_name); 1078 error(33, dsym->s_name);
1072 rmsym(dcs->d_rdcsym); 1079 rmsym(dcs->d_rdcsym);
1073 } 1080 }
1074 } 1081 }
1075 1082
1076 chktyp(dsym); 1083 chktyp(dsym);
1077 1084
1078 t = (tp = dsym->s_type)->t_tspec; 1085 t = (tp = dsym->s_type)->t_tspec;
1079 1086
1080 if (dsym->s_field) { 1087 if (dsym->s_field) {
1081 /* 1088 /*
1082 * bit field 1089 * bit field
1083 * 1090 *
1084 * only unsigned und signed int are protable bit-field types 1091 * only unsigned und signed int are protable bit-field types
1085 *(at least in ANSI C, in traditional C only unsigned int) 1092 *(at least in ANSI C, in traditional C only unsigned int)
1086 */ 1093 */
1087 if (t == CHAR || t == UCHAR || t == SCHAR || 1094 if (t == CHAR || t == UCHAR || t == SCHAR ||
1088 t == SHORT || t == USHORT || t == ENUM) { 1095 t == SHORT || t == USHORT || t == ENUM) {
1089 if (bitfieldtype_ok == 0) { 1096 if (bitfieldtype_ok == 0) {
1090 if (sflag) { 1097 if (sflag) {
1091 char buf[64]; 1098 char buf[64];
1092 /* 1099 /*
1093 * bit-field type '%s' invalid in 1100 * bit-field type '%s' invalid in
1094 * ANSI C 1101 * ANSI C
1095 */ 1102 */
1096 warning(273, 1103 warning(273,
1097 tyname(buf, sizeof(buf), tp)); 1104 tyname(buf, sizeof(buf), tp));
1098 } else if (pflag) { 1105 } else if (pflag) {
1099 /* nonportable bit-field type */ 1106 /* nonportable bit-field type */
1100 warning(34); 1107 warning(34);
1101 } 1108 }
1102 } 1109 }
1103 } else if (t == INT && dcs->d_smod == NOTSPEC) { 1110 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1104 if (pflag && bitfieldtype_ok == 0) { 1111 if (pflag && bitfieldtype_ok == 0) {
1105 /* nonportable bit-field type */ 1112 /* nonportable bit-field type */
1106 warning(34); 1113 warning(34);
1107 } 1114 }
1108 } else if (t != INT && t != UINT) { 1115 } else if (t != INT && t != UINT) {
1109 /* 1116 /*
1110 * Non-integer types are always illegal for 1117 * Non-integer types are always illegal for
1111 * bitfields, regardless of BITFIELDTYPE. 1118 * bitfields, regardless of BITFIELDTYPE.
1112 * Integer types not dealt with above are 1119 * Integer types not dealt with above are
1113 * okay only if BITFIELDTYPE is in effect. 1120 * okay only if BITFIELDTYPE is in effect.
1114 */ 1121 */
1115 if (bitfieldtype_ok == 0 || isityp(t) == 0) { 1122 if (bitfieldtype_ok == 0 || isityp(t) == 0) {
1116 /* illegal bit-field type */ 1123 /* illegal bit-field type */
1117 error(35); 1124 error(35);
1118 sz = tp->t_flen; 1125 sz = tp->t_flen;
1119 dsym->s_type = tp = duptyp(gettyp(t = INT)); 1126 dsym->s_type = tp = duptyp(gettyp(t = INT));
1120 if ((tp->t_flen = sz) > size(t)) 1127 if ((tp->t_flen = sz) > size(t))
1121 tp->t_flen = size(t); 1128 tp->t_flen = size(t);
1122 } 1129 }
1123 } 1130 }
1124 if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) { 1131 if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) {
1125 /* illegal bit-field size */ 1132 /* illegal bit-field size */
1126 error(36); 1133 error(36);
1127 tp->t_flen = size(t); 1134 tp->t_flen = size(t);
1128 } else if (len == 0 && dsym->s_name != unnamed) { 1135 } else if (len == 0 && dsym->s_name != unnamed) {
1129 /* zero size bit-field */ 1136 /* zero size bit-field */
1130 error(37); 1137 error(37);
1131 tp->t_flen = size(t); 1138 tp->t_flen = size(t);
1132 } 1139 }
1133 if (dsym->s_scl == MOU) { 1140 if (dsym->s_scl == MOU) {
1134 /* illegal use of bit-field */ 1141 /* illegal use of bit-field */
1135 error(41); 1142 error(41);
1136 dsym->s_type->t_isfield = 0; 1143 dsym->s_type->t_isfield = 0;
1137 dsym->s_field = 0; 1144 dsym->s_field = 0;
1138 } 1145 }
1139 } else if (t == FUNC) { 1146 } else if (t == FUNC) {
1140 /* function illegal in structure or union */ 1147 /* function illegal in structure or union */
1141 error(38); 1148 error(38);
1142 dsym->s_type = tp = incref(tp, t = PTR); 1149 dsym->s_type = tp = incref(tp, t = PTR);
1143 } 1150 }
1144 1151
1145 /* 1152 /*
1146 * bit-fields of length 0 are not warned about because length() 1153 * bit-fields of length 0 are not warned about because length()
1147 * does not return the length of the bit-field but the length 1154 * does not return the length of the bit-field but the length
1148 * of the type the bit-field is packed in (its ok) 1155 * of the type the bit-field is packed in (its ok)
1149 */ 1156 */
1150 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { 1157 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1151 if (t == ARRAY && dsym->s_type->t_dim == 0) { 1158 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1152 /* illegal zero sized structure member: %s */ 1159 /* illegal zero sized structure member: %s */
1153 c99ism(39, dsym->s_name); 1160 c99ism(39, dsym->s_name);
1154 } 1161 }
1155 } 1162 }
1156 1163
1157 if (dcs->d_ctx == MOU) { 1164 if (dcs->d_ctx == MOU) {
1158 o = dcs->d_offset; 1165 o = dcs->d_offset;
1159 dcs->d_offset = 0; 1166 dcs->d_offset = 0;
1160 } 1167 }
1161 if (dsym->s_field) { 1168 if (dsym->s_field) {
1162 align(getbound(tp), tp->t_flen); 1169 align(getbound(tp), tp->t_flen);
1163 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t); 1170 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1164 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad; 1171 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1165 dcs->d_offset += tp->t_flen; 1172 dcs->d_offset += tp->t_flen;
1166 } else { 1173 } else {
1167 align(getbound(tp), 0); 1174 align(getbound(tp), 0);
1168 dsym->s_value.v_quad = dcs->d_offset; 1175 dsym->s_value.v_quad = dcs->d_offset;
1169 dcs->d_offset += sz; 1176 dcs->d_offset += sz;
1170 } 1177 }
1171 if (dcs->d_ctx == MOU) { 1178 if (dcs->d_ctx == MOU) {
1172 if (o > dcs->d_offset) 1179 if (o > dcs->d_offset)
1173 dcs->d_offset = o; 1180 dcs->d_offset = o;
1174 } 1181 }
1175 1182
1176 chkfdef(dsym, 0); 1183 chkfdef(dsym, 0);
1177 1184
1178 /* 1185 /*
1179 * Clear the BITFIELDTYPE indicator after processing each 1186 * Clear the BITFIELDTYPE indicator after processing each
1180 * structure element. 1187 * structure element.
1181 */ 1188 */
1182 bitfieldtype_ok = 0; 1189 bitfieldtype_ok = 0;
1183 1190
1184 return (dsym); 1191 return (dsym);
1185} 1192}
1186 1193
1187/* 1194/*
1188 * Aligns next structure element as required. 1195 * Aligns next structure element as required.
1189 * 1196 *
1190 * al contains the required alignment, len the length of a bit-field. 1197 * al contains the required alignment, len the length of a bit-field.
1191 */ 1198 */
1192static void 1199static void
1193align(int al, int len) 1200align(int al, int len)
1194{ 1201{
1195 int no; 1202 int no;
1196 1203
1197 /* 1204 /*
1198 * The alignment of the current element becomes the alignment of 1205 * The alignment of the current element becomes the alignment of
1199 * the struct/union if it is larger than the current alignment 1206 * the struct/union if it is larger than the current alignment
1200 * of the struct/union. 1207 * of the struct/union.
1201 */ 1208 */
1202 if (al > dcs->d_stralign) 1209 if (al > dcs->d_stralign)
1203 dcs->d_stralign = al; 1210 dcs->d_stralign = al;
1204 1211
1205 no = (dcs->d_offset + (al - 1)) & ~(al - 1); 1212 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1206 if (len == 0 || dcs->d_offset + len > no) 1213 if (len == 0 || dcs->d_offset + len > no)
1207 dcs->d_offset = no; 1214 dcs->d_offset = no;
1208} 1215}
1209 1216
1210/* 1217/*
1211 * Remember the width of the field in its type structure. 1218 * Remember the width of the field in its type structure.
1212 */ 1219 */
1213sym_t * 1220sym_t *
1214bitfield(sym_t *dsym, int len) 1221bitfield(sym_t *dsym, int len)
1215{ 1222{
1216 1223
1217 if (dsym == NULL) { 1224 if (dsym == NULL) {
1218 dsym = getblk(sizeof (sym_t)); 1225 dsym = getblk(sizeof (sym_t));
1219 dsym->s_name = unnamed; 1226 dsym->s_name = unnamed;
1220 dsym->s_kind = FMOS; 1227 dsym->s_kind = FMOS;
1221 dsym->s_scl = MOS; 1228 dsym->s_scl = MOS;
1222 dsym->s_type = gettyp(UINT); 1229 dsym->s_type = gettyp(UINT);
1223 dsym->s_blklev = -1; 1230 dsym->s_blklev = -1;
1224 } 1231 }
1225 dsym->s_type = duptyp(dsym->s_type); 1232 dsym->s_type = duptyp(dsym->s_type);
1226 dsym->s_type->t_isfield = 1; 1233 dsym->s_type->t_isfield = 1;
1227 dsym->s_type->t_flen = len; 1234 dsym->s_type->t_flen = len;
1228 dsym->s_field = 1; 1235 dsym->s_field = 1;
1229 return (dsym); 1236 return (dsym);
1230} 1237}
1231 1238
1232/* 1239/*
1233 * Collect informations about a sequence of asterisks and qualifiers 1240 * Collect informations about a sequence of asterisks and qualifiers
1234 * in a list of type pqinf_t. 1241 * in a list of type pqinf_t.
1235 * Qualifiers refer always to the left asterisk. The rightmost asterisk 1242 * Qualifiers refer always to the left asterisk. The rightmost asterisk
1236 * will be at the top of the list. 1243 * will be at the top of the list.
1237 */ 1244 */
1238pqinf_t * 1245pqinf_t *
1239mergepq(pqinf_t *p1, pqinf_t *p2) 1246mergepq(pqinf_t *p1, pqinf_t *p2)
1240{ 1247{
1241 pqinf_t *p; 1248 pqinf_t *p;
1242 1249
1243 if (p2->p_pcnt != 0) { 1250 if (p2->p_pcnt != 0) {
1244 /* left '*' at the end of the list */ 1251 /* left '*' at the end of the list */
1245 for (p = p2; p->p_nxt != NULL; p = p->p_nxt) 1252 for (p = p2; p->p_nxt != NULL; p = p->p_nxt)
1246 continue; 1253 continue;
1247 p->p_nxt = p1; 1254 p->p_nxt = p1;
1248 return (p2); 1255 return (p2);
1249 } else { 1256 } else {
1250 if (p2->p_const) { 1257 if (p2->p_const) {
1251 if (p1->p_const) { 1258 if (p1->p_const) {
1252 /* duplicate %s */ 1259 /* duplicate %s */
1253 warning(10, "const"); 1260 warning(10, "const");
1254 } 1261 }
1255 p1->p_const = 1; 1262 p1->p_const = 1;
1256 } 1263 }
1257 if (p2->p_volatile) { 1264 if (p2->p_volatile) {
1258 if (p1->p_volatile) { 1265 if (p1->p_volatile) {
1259 /* duplicate %s */ 1266 /* duplicate %s */
1260 warning(10, "volatile"); 1267 warning(10, "volatile");
1261 } 1268 }
1262 p1->p_volatile = 1; 1269 p1->p_volatile = 1;
1263 } 1270 }
1264 free(p2); 1271 free(p2);
1265 return (p1); 1272 return (p1);
1266 } 1273 }
1267} 1274}
1268 1275
1269/* 1276/*
1270 * Followint 3 functions extend the type of a declarator with 1277 * Followint 3 functions extend the type of a declarator with
1271 * pointer, function and array types. 1278 * pointer, function and array types.
1272 * 1279 *
1273 * The current type is the Type built by deftyp() (dcs->d_type) and 1280 * The current type is the Type built by deftyp() (dcs->d_type) and
1274 * pointer, function and array types already added for this 1281 * pointer, function and array types already added for this
1275 * declarator. The new type extension is inserted between both. 1282 * declarator. The new type extension is inserted between both.
1276 */ 1283 */
1277sym_t * 1284sym_t *
1278addptr(sym_t *decl, pqinf_t *pi) 1285addptr(sym_t *decl, pqinf_t *pi)
1279{ 1286{
1280 type_t **tpp, *tp; 1287 type_t **tpp, *tp;
1281 pqinf_t *npi; 1288 pqinf_t *npi;
1282 1289
1283 tpp = &decl->s_type; 1290 tpp = &decl->s_type;
1284 while (*tpp && *tpp != dcs->d_type) 1291 while (*tpp && *tpp != dcs->d_type)
1285 tpp = &(*tpp)->t_subt; 1292 tpp = &(*tpp)->t_subt;
1286 if (*tpp == NULL) 1293 if (*tpp == NULL)
1287 return decl; 1294 return decl;
1288 1295
1289 while (pi != NULL) { 1296 while (pi != NULL) {
1290 *tpp = tp = getblk(sizeof (type_t)); 1297 *tpp = tp = getblk(sizeof (type_t));
1291 tp->t_tspec = PTR; 1298 tp->t_tspec = PTR;
1292 tp->t_const = pi->p_const; 1299 tp->t_const = pi->p_const;
1293 tp->t_volatile = pi->p_volatile; 1300 tp->t_volatile = pi->p_volatile;
1294 *(tpp = &tp->t_subt) = dcs->d_type; 1301 *(tpp = &tp->t_subt) = dcs->d_type;
1295 npi = pi->p_nxt; 1302 npi = pi->p_nxt;
1296 free(pi); 1303 free(pi);
1297 pi = npi; 1304 pi = npi;
1298 } 1305 }
1299 return (decl); 1306 return (decl);
1300} 1307}
1301 1308
1302/* 1309/*
1303 * If a dimension was specified, dim is 1, otherwise 0 1310 * If a dimension was specified, dim is 1, otherwise 0
1304 * n is the specified dimension 1311 * n is the specified dimension
1305 */ 1312 */
1306sym_t * 1313sym_t *
1307addarray(sym_t *decl, int dim, int n) 1314addarray(sym_t *decl, int dim, int n)
1308{ 1315{
1309 type_t **tpp, *tp; 1316 type_t **tpp, *tp;
1310 1317
1311 tpp = &decl->s_type; 1318 tpp = &decl->s_type;
1312 while (*tpp && *tpp != dcs->d_type) 1319 while (*tpp && *tpp != dcs->d_type)
1313 tpp = &(*tpp)->t_subt; 1320 tpp = &(*tpp)->t_subt;
1314 if (*tpp == NULL) 1321 if (*tpp == NULL)
1315 return decl; 1322 return decl;
1316 1323
1317 *tpp = tp = getblk(sizeof (type_t)); 1324 *tpp = tp = getblk(sizeof (type_t));
1318 tp->t_tspec = ARRAY; 1325 tp->t_tspec = ARRAY;
1319 tp->t_subt = dcs->d_type; 1326 tp->t_subt = dcs->d_type;
1320 tp->t_dim = n; 1327 tp->t_dim = n;
1321 1328
1322 if (n < 0) { 1329 if (n < 0) {
1323 /* negative array dimension */ 1330 /* negative array dimension */
1324 error(20, n); 1331 error(20, n);
1325 n = 0; 1332 n = 0;
1326 } else if (n == 0 && dim) { 1333 } else if (n == 0 && dim) {
1327 /* zero array dimension */ 1334 /* zero array dimension */
1328 c99ism(322, dim); 1335 c99ism(322, dim);
1329 } else if (n == 0 && !dim) { 1336 } else if (n == 0 && !dim) {
1330 /* is incomplete type */ 1337 /* is incomplete type */
1331 setcompl(tp, 1); 1338 setcompl(tp, 1);
1332 } 1339 }
1333 1340
1334 return (decl); 1341 return (decl);
1335} 1342}
1336 1343
1337sym_t * 1344sym_t *
1338addfunc(sym_t *decl, sym_t *args) 1345addfunc(sym_t *decl, sym_t *args)
1339{ 1346{
1340 type_t **tpp, *tp; 1347 type_t **tpp, *tp;
1341 1348
1342 if (dcs->d_proto) { 1349 if (dcs->d_proto) {
1343 if (tflag) 1350 if (tflag)
1344 /* function prototypes are illegal in traditional C */ 1351 /* function prototypes are illegal in traditional C */
1345 warning(270); 1352 warning(270);
1346 args = nsfunc(decl, args); 1353 args = nsfunc(decl, args);
1347 } else { 1354 } else {
1348 osfunc(decl, args); 1355 osfunc(decl, args);
1349 } 1356 }
1350 1357
1351 /* 1358 /*
1352 * The symbols are removed from the symbol table by popdecl() after 1359 * The symbols are removed from the symbol table by popdecl() after
1353 * addfunc(). To be able to restore them if this is a function 1360 * addfunc(). To be able to restore them if this is a function
1354 * definition, a pointer to the list of all symbols is stored in 1361 * definition, a pointer to the list of all symbols is stored in
1355 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated 1362 * dcs->d_nxt->d_fpsyms. Also a list of the arguments (concatenated
1356 * by s_nxt) is stored in dcs->d_nxt->d_fargs. 1363 * by s_nxt) is stored in dcs->d_nxt->d_fargs.
1357 * (dcs->d_nxt must be used because *dcs is the declaration stack 1364 * (dcs->d_nxt must be used because *dcs is the declaration stack
1358 * element created for the list of params and is removed after 1365 * element created for the list of params and is removed after
1359 * addfunc()) 1366 * addfunc())
1360 */ 1367 */
1361 if (dcs->d_nxt->d_ctx == EXTERN && 1368 if (dcs->d_nxt->d_ctx == EXTERN &&
1362 decl->s_type == dcs->d_nxt->d_type) { 1369 decl->s_type == dcs->d_nxt->d_type) {
1363 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms; 1370 dcs->d_nxt->d_fpsyms = dcs->d_dlsyms;
1364 dcs->d_nxt->d_fargs = args; 1371 dcs->d_nxt->d_fargs = args;
1365 } 1372 }
1366 1373
1367 tpp = &decl->s_type; 1374 tpp = &decl->s_type;
1368 while (*tpp && *tpp != dcs->d_nxt->d_type) 1375 while (*tpp && *tpp != dcs->d_nxt->d_type)
1369 tpp = &(*tpp)->t_subt; 1376 tpp = &(*tpp)->t_subt;
1370 if (*tpp == NULL) 1377 if (*tpp == NULL)
1371 return decl; 1378 return decl;
1372 1379
1373 *tpp = tp = getblk(sizeof (type_t)); 1380 *tpp = tp = getblk(sizeof (type_t));
1374 tp->t_tspec = FUNC; 1381 tp->t_tspec = FUNC;
1375 tp->t_subt = dcs->d_nxt->d_type; 1382 tp->t_subt = dcs->d_nxt->d_type;
1376 if ((tp->t_proto = dcs->d_proto) != 0) 1383 if ((tp->t_proto = dcs->d_proto) != 0)
1377 tp->t_args = args; 1384 tp->t_args = args;
1378 tp->t_vararg = dcs->d_vararg; 1385 tp->t_vararg = dcs->d_vararg;
1379 1386
1380 return (decl); 1387 return (decl);
1381} 1388}
1382 1389
1383/* 1390/*
1384 * Called for new style function declarations. 1391 * Called for new style function declarations.
1385 */ 1392 */
1386/* ARGSUSED */ 1393/* ARGSUSED */
1387static sym_t * 1394static sym_t *
1388nsfunc(sym_t *decl, sym_t *args) 1395nsfunc(sym_t *decl, sym_t *args)
1389{ 1396{
1390 sym_t *arg, *sym; 1397 sym_t *arg, *sym;
1391 scl_t sc; 1398 scl_t sc;
1392 int n; 1399 int n;
1393 1400
1394 /* 1401 /*
1395 * Declarations of structs/unions/enums in param lists are legal, 1402 * Declarations of structs/unions/enums in param lists are legal,
1396 * but senseless. 1403 * but senseless.
1397 */ 1404 */
1398 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { 1405 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1399 sc = sym->s_scl; 1406 sc = sym->s_scl;
1400 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) { 1407 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1401 /* dubious tag declaration: %s %s */ 1408 /* dubious tag declaration: %s %s */
1402 warning(85, scltoa(sc), sym->s_name); 1409 warning(85, scltoa(sc), sym->s_name);
1403 } 1410 }
1404 } 1411 }
1405 1412
1406 n = 1; 1413 n = 1;
1407 for (arg = args; arg != NULL; arg = arg->s_nxt) { 1414 for (arg = args; arg != NULL; arg = arg->s_nxt) {
1408 if (arg->s_type->t_tspec == VOID) { 1415 if (arg->s_type->t_tspec == VOID) {
1409 if (n > 1 || arg->s_nxt != NULL) { 1416 if (n > 1 || arg->s_nxt != NULL) {
1410 /* "void" must be sole parameter */ 1417 /* "void" must be sole parameter */
1411 error(60); 1418 error(60);
1412 arg->s_type = gettyp(INT); 1419 arg->s_type = gettyp(INT);
1413 } 1420 }
1414 } 1421 }
1415 n++; 1422 n++;
1416 } 1423 }
1417 1424
1418 /* return NULL if first param is VOID */ 1425 /* return NULL if first param is VOID */
1419 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL); 1426 return (args != NULL && args->s_type->t_tspec != VOID ? args : NULL);
1420} 1427}
1421 1428
1422/* 1429/*
1423 * Called for old style function declarations. 1430 * Called for old style function declarations.
1424 */ 1431 */
1425static void 1432static void
1426osfunc(sym_t *decl, sym_t *args) 1433osfunc(sym_t *decl, sym_t *args)
1427{ 1434{
1428 1435
1429 /* 1436 /*
1430 * Remember list of params only if this is really seams to be 1437 * Remember list of params only if this is really seams to be
1431 * a function definition. 1438 * a function definition.
1432 */ 1439 */
1433 if (dcs->d_nxt->d_ctx == EXTERN && 1440 if (dcs->d_nxt->d_ctx == EXTERN &&
1434 decl->s_type == dcs->d_nxt->d_type) { 1441 decl->s_type == dcs->d_nxt->d_type) {
1435 /* 1442 /*
1436 * We assume that this becomes a function definition. If 1443 * We assume that this becomes a function definition. If
1437 * we are wrong, its corrected in chkfdef(). 1444 * we are wrong, its corrected in chkfdef().
1438 */ 1445 */
1439 if (args != NULL) { 1446 if (args != NULL) {
1440 decl->s_osdef = 1; 1447 decl->s_osdef = 1;
1441 decl->s_args = args; 1448 decl->s_args = args;
1442 } 1449 }
1443 } else { 1450 } else {
1444 if (args != NULL) 1451 if (args != NULL)
1445 /* function prototype parameters must have types */ 1452 /* function prototype parameters must have types */
1446 warning(62); 1453 warning(62);
1447 } 1454 }
1448} 1455}
1449 1456
1450/* 1457/*
1451 * Lists of Identifiers in functions declarations are allowed only if 1458 * Lists of Identifiers in functions declarations are allowed only if
1452 * its also a function definition. If this is not the case, print a 1459 * its also a function definition. If this is not the case, print a
1453 * error message. 1460 * error message.
1454 */ 1461 */
1455void 1462void
1456chkfdef(sym_t *sym, int msg) 1463chkfdef(sym_t *sym, int msg)
1457{ 1464{
1458 1465
1459 if (sym->s_osdef) { 1466 if (sym->s_osdef) {
1460 if (msg) { 1467 if (msg) {
1461 /* incomplete or misplaced function definition */ 1468 /* incomplete or misplaced function definition */
1462 error(22); 1469 error(22);
1463 } 1470 }
1464 sym->s_osdef = 0; 1471 sym->s_osdef = 0;
1465 sym->s_args = NULL; 1472 sym->s_args = NULL;
1466 } 1473 }
1467} 1474}
1468 1475
1469/* 1476/*
1470 * Process the name in a declarator. 1477 * Process the name in a declarator.
1471 * If the symbol does already exists, a new one is created. 1478 * If the symbol does already exists, a new one is created.
1472 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or 1479 * The symbol becomes one of the storage classes EXTERN, STATIC, AUTO or
1473 * TYPEDEF. 1480 * TYPEDEF.
1474 * s_def and s_reg are valid after dname(). 1481 * s_def and s_reg are valid after dname().
1475 */ 1482 */
1476sym_t * 1483sym_t *
1477dname(sym_t *sym) 1484dname(sym_t *sym)
1478{ 1485{
1479 scl_t sc = NOSCL; 1486 scl_t sc = NOSCL;
1480 1487
1481 if (sym->s_scl == NOSCL) { 1488 if (sym->s_scl == NOSCL) {
1482 dcs->d_rdcsym = NULL; 1489 dcs->d_rdcsym = NULL;
1483 } else if (sym->s_defarg) { 1490 } else if (sym->s_defarg) {
1484 sym->s_defarg = 0; 1491 sym->s_defarg = 0;
1485 dcs->d_rdcsym = NULL; 1492 dcs->d_rdcsym = NULL;
1486 } else { 1493 } else {
1487 dcs->d_rdcsym = sym; 1494 dcs->d_rdcsym = sym;
1488 sym = pushdown(sym); 1495 sym = pushdown(sym);
1489 } 1496 }
1490 1497
1491 switch (dcs->d_ctx) { 1498 switch (dcs->d_ctx) {
1492 case MOS: 1499 case MOS:
1493 case MOU: 1500 case MOU:
1494 /* Parent setzen */ 1501 /* Parent setzen */
1495 sym->s_styp = dcs->d_tagtyp->t_str; 1502 sym->s_styp = dcs->d_tagtyp->t_str;
1496 sym->s_def = DEF; 1503 sym->s_def = DEF;
1497 sym->s_value.v_tspec = INT; 1504 sym->s_value.v_tspec = INT;
1498 sc = dcs->d_ctx; 1505 sc = dcs->d_ctx;
1499 break; 1506 break;
1500 case EXTERN: 1507 case EXTERN:
1501 /* 1508 /*
1502 * static and external symbols without "extern" are 1509 * static and external symbols without "extern" are
1503 * considered to be tentative defined, external 1510 * considered to be tentative defined, external
1504 * symbols with "extern" are declared, and typedef names 1511 * symbols with "extern" are declared, and typedef names
1505 * are defined. Tentative defined and declared symbols 1512 * are defined. Tentative defined and declared symbols
1506 * may become defined if an initializer is present or 1513 * may become defined if an initializer is present or
1507 * this is a function definition. 1514 * this is a function definition.
1508 */ 1515 */
1509 if ((sc = dcs->d_scl) == NOSCL) { 1516 if ((sc = dcs->d_scl) == NOSCL) {