Sun Jan 3 19:10:47 2021 UTC ()
lint: rename type.t_isfield to t_bitfield


(rillig)
diff -r1.107 -r1.108 src/usr.bin/xlint/lint1/decl.c
diff -r1.58 -r1.59 src/usr.bin/xlint/lint1/init.c
diff -r1.49 -r1.50 src/usr.bin/xlint/lint1/lint1.h
diff -r1.119 -r1.120 src/usr.bin/xlint/lint1/tree.c

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

--- src/usr.bin/xlint/lint1/decl.c 2021/01/03 18:48:37 1.107
+++ src/usr.bin/xlint/lint1/decl.c 2021/01/03 19:10:47 1.108
@@ -1,2789 +1,2789 @@ @@ -1,2789 +1,2789 @@
1/* $NetBSD: decl.c,v 1.107 2021/01/03 18:48:37 rillig Exp $ */ 1/* $NetBSD: decl.c,v 1.108 2021/01/03 19:10:47 rillig 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.107 2021/01/03 18:48:37 rillig Exp $"); 41__RCSID("$NetBSD: decl.c,v 1.108 2021/01/03 19:10:47 rillig 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 information local 60 * pointer to top element of a stack which contains information 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 merge_type_specifiers(tspec_t, tspec_t); 67static tspec_t merge_type_specifiers(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 check_old_style_definition(sym_t *, sym_t *); 72static int check_old_style_definition(sym_t *, sym_t *);
73static int check_prototype_declaration(sym_t *, sym_t *); 73static int check_prototype_declaration(sym_t *, sym_t *);
74static sym_t *new_style_function(sym_t *, sym_t *); 74static sym_t *new_style_function(sym_t *, sym_t *);
75static void old_style_function(sym_t *, sym_t *); 75static void old_style_function(sym_t *, sym_t *);
76static void ledecl(sym_t *); 76static void ledecl(sym_t *);
77static int check_init(sym_t *); 77static int check_init(sym_t *);
78static void check_argument_usage(int, sym_t *); 78static void check_argument_usage(int, sym_t *);
79static void check_variable_usage(int, sym_t *); 79static void check_variable_usage(int, sym_t *);
80static void check_label_usage(sym_t *); 80static void check_label_usage(sym_t *);
81static void check_tag_usage(sym_t *); 81static void check_tag_usage(sym_t *);
82static void check_global_variable(sym_t *); 82static void check_global_variable(sym_t *);
83static void check_global_variable_size(sym_t *); 83static void check_global_variable_size(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 for arithmetic types and void. 134 * Returns a shared type structure for arithmetic types and void.
135 * 135 *
136 * It's important to duplicate this structure (using duptyp() or tduptyp()) 136 * It's important to duplicate this structure (using duptyp() or tduptyp())
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 *ntp = *tp; 152 *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 *ntp = *tp; 166 *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 * Mark an array, struct, union or enum type as complete or incomplete. 192 * Mark an array, struct, union or enum type as complete or incomplete.
193 */ 193 */
194void 194void
195setcomplete(type_t *tp, int complete) 195setcomplete(type_t *tp, int complete)
196{ 196{
197 tspec_t t; 197 tspec_t t;
198 198
199 if ((t = tp->t_tspec) == ARRAY) { 199 if ((t = tp->t_tspec) == ARRAY) {
200 tp->t_aincompl = !complete; 200 tp->t_aincompl = !complete;
201 } else if (t == STRUCT || t == UNION) { 201 } else if (t == STRUCT || t == UNION) {
202 tp->t_str->sincompl = !complete; 202 tp->t_str->sincompl = !complete;
203 } else { 203 } else {
204 lint_assert(t == ENUM); 204 lint_assert(t == ENUM);
205 tp->t_enum->eincompl = !complete; 205 tp->t_enum->eincompl = !complete;
206 } 206 }
207} 207}
208 208
209/* 209/*
210 * Remember the storage class of the current declaration in dcs->d_scl 210 * Remember the storage class of the current declaration in dcs->d_scl
211 * (the top element of the declaration stack) and detect multiple 211 * (the top element of the declaration stack) and detect multiple
212 * storage classes. 212 * storage classes.
213 */ 213 */
214void 214void
215add_storage_class(scl_t sc) 215add_storage_class(scl_t sc)
216{ 216{
217 217
218 if (sc == INLINE) { 218 if (sc == INLINE) {
219 if (dcs->d_inline) 219 if (dcs->d_inline)
220 /* duplicate '%s' */ 220 /* duplicate '%s' */
221 warning(10, "inline"); 221 warning(10, "inline");
222 dcs->d_inline = 1; 222 dcs->d_inline = 1;
223 return; 223 return;
224 } 224 }
225 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 225 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
226 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) { 226 dcs->d_smod != NOTSPEC || dcs->d_lmod != NOTSPEC) {
227 /* storage class after type is obsolescent */ 227 /* storage class after type is obsolescent */
228 warning(83); 228 warning(83);
229 } 229 }
230 if (dcs->d_scl == NOSCL) { 230 if (dcs->d_scl == NOSCL) {
231 dcs->d_scl = sc; 231 dcs->d_scl = sc;
232 } else { 232 } else {
233 /* 233 /*
234 * multiple storage classes. An error will be reported in 234 * multiple storage classes. An error will be reported in
235 * deftyp(). 235 * deftyp().
236 */ 236 */
237 dcs->d_mscl = 1; 237 dcs->d_mscl = 1;
238 } 238 }
239} 239}
240 240
241/* 241/*
242 * Remember the type, modifier or typedef name returned by the parser 242 * Remember the type, modifier or typedef name returned by the parser
243 * in *dcs (top element of decl stack). This information is used in 243 * in *dcs (top element of decl stack). This information is used in
244 * deftyp() to build the type used for all declarators in this 244 * deftyp() to build the type used for all declarators in this
245 * declaration. 245 * declaration.
246 * 246 *
247 * If tp->t_typedef is 1, the type comes from a previously defined typename. 247 * If tp->t_typedef is 1, the type comes from a previously defined typename.
248 * Otherwise it comes from a type specifier (int, long, ...) or a 248 * Otherwise it comes from a type specifier (int, long, ...) or a
249 * struct/union/enum tag. 249 * struct/union/enum tag.
250 */ 250 */
251void 251void
252add_type(type_t *tp) 252add_type(type_t *tp)
253{ 253{
254 tspec_t t; 254 tspec_t t;
255#ifdef DEBUG 255#ifdef DEBUG
256 printf("%s: %s\n", __func__, type_name(tp)); 256 printf("%s: %s\n", __func__, type_name(tp));
257#endif 257#endif
258 if (tp->t_typedef) { 258 if (tp->t_typedef) {
259 /* 259 /*
260 * something like "typedef int a; int a b;" 260 * something like "typedef int a; int a b;"
261 * This should not happen with current grammar. 261 * This should not happen with current grammar.
262 */ 262 */
263 lint_assert(dcs->d_type == NULL); 263 lint_assert(dcs->d_type == NULL);
264 lint_assert(dcs->d_atyp == NOTSPEC); 264 lint_assert(dcs->d_atyp == NOTSPEC);
265 lint_assert(dcs->d_lmod == NOTSPEC); 265 lint_assert(dcs->d_lmod == NOTSPEC);
266 lint_assert(dcs->d_smod == NOTSPEC); 266 lint_assert(dcs->d_smod == NOTSPEC);
267 267
268 dcs->d_type = tp; 268 dcs->d_type = tp;
269 return; 269 return;
270 } 270 }
271 271
272 t = tp->t_tspec; 272 t = tp->t_tspec;
273 273
274 if (t == STRUCT || t == UNION || t == ENUM) { 274 if (t == STRUCT || t == UNION || t == ENUM) {
275 /* 275 /*
276 * something like "int struct a ..." 276 * something like "int struct a ..."
277 * struct/union/enum with anything else is not allowed 277 * struct/union/enum with anything else is not allowed
278 */ 278 */
279 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC || 279 if (dcs->d_type != NULL || dcs->d_atyp != NOTSPEC ||
280 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) { 280 dcs->d_lmod != NOTSPEC || dcs->d_smod != NOTSPEC) {
281 /* 281 /*
282 * remember that an error must be reported in 282 * remember that an error must be reported in
283 * deftyp(). 283 * deftyp().
284 */ 284 */
285 dcs->d_terr = 1; 285 dcs->d_terr = 1;
286 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC; 286 dcs->d_atyp = dcs->d_lmod = dcs->d_smod = NOTSPEC;
287 } 287 }
288 dcs->d_type = tp; 288 dcs->d_type = tp;
289 return; 289 return;
290 } 290 }
291 291
292 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) { 292 if (dcs->d_type != NULL && !dcs->d_type->t_typedef) {
293 /* 293 /*
294 * something like "struct a int" 294 * something like "struct a int"
295 * struct/union/enum with anything else is not allowed 295 * struct/union/enum with anything else is not allowed
296 */ 296 */
297 dcs->d_terr = 1; 297 dcs->d_terr = 1;
298 return; 298 return;
299 } 299 }
300 300
301 if (t == COMPLEX) { 301 if (t == COMPLEX) {
302 if (dcs->d_cmod == FLOAT) 302 if (dcs->d_cmod == FLOAT)
303 t = FCOMPLEX; 303 t = FCOMPLEX;
304 else if (dcs->d_cmod == DOUBLE) 304 else if (dcs->d_cmod == DOUBLE)
305 t = DCOMPLEX; 305 t = DCOMPLEX;
306 else { 306 else {
307 /* invalid type for _Complex */ 307 /* invalid type for _Complex */
308 error(308); 308 error(308);
309 t = DCOMPLEX; /* just as a fallback */ 309 t = DCOMPLEX; /* just as a fallback */
310 } 310 }
311 dcs->d_cmod = NOTSPEC; 311 dcs->d_cmod = NOTSPEC;
312 } 312 }
313 313
314 if (t == LONG && dcs->d_lmod == LONG) { 314 if (t == LONG && dcs->d_lmod == LONG) {
315 /* "long long" or "long ... long" */ 315 /* "long long" or "long ... long" */
316 t = QUAD; 316 t = QUAD;
317 dcs->d_lmod = NOTSPEC; 317 dcs->d_lmod = NOTSPEC;
318 if (!quadflg) 318 if (!quadflg)
319 /* %s C does not support 'long long' */ 319 /* %s C does not support 'long long' */
320 c99ism(265, tflag ? "traditional" : "c89"); 320 c99ism(265, tflag ? "traditional" : "c89");
321 } 321 }
322 322
323 if (dcs->d_type != NULL && dcs->d_type->t_typedef) { 323 if (dcs->d_type != NULL && dcs->d_type->t_typedef) {
324 /* something like "typedef int a; a long ..." */ 324 /* something like "typedef int a; a long ..." */
325 dcs->d_type = tdeferr(dcs->d_type, t); 325 dcs->d_type = tdeferr(dcs->d_type, t);
326 return; 326 return;
327 } 327 }
328 328
329 /* now it can be only a combination of arithmetic types and void */ 329 /* now it can be only a combination of arithmetic types and void */
330 if (t == SIGNED || t == UNSIGN) { 330 if (t == SIGNED || t == UNSIGN) {
331 /* remember specifiers "signed" & "unsigned" in dcs->d_smod */ 331 /* remember specifiers "signed" & "unsigned" in dcs->d_smod */
332 if (dcs->d_smod != NOTSPEC) 332 if (dcs->d_smod != NOTSPEC)
333 /* 333 /*
334 * more than one "signed" and/or "unsigned"; print 334 * more than one "signed" and/or "unsigned"; print
335 * an error in deftyp() 335 * an error in deftyp()
336 */ 336 */
337 dcs->d_terr = 1; 337 dcs->d_terr = 1;
338 dcs->d_smod = t; 338 dcs->d_smod = t;
339 } else if (t == SHORT || t == LONG || t == QUAD) { 339 } else if (t == SHORT || t == LONG || t == QUAD) {
340 /* 340 /*
341 * remember specifiers "short", "long" and "long long" in 341 * remember specifiers "short", "long" and "long long" in
342 * dcs->d_lmod 342 * dcs->d_lmod
343 */ 343 */
344 if (dcs->d_lmod != NOTSPEC) 344 if (dcs->d_lmod != NOTSPEC)
345 /* more than one, print error in deftyp() */ 345 /* more than one, print error in deftyp() */
346 dcs->d_terr = 1; 346 dcs->d_terr = 1;
347 dcs->d_lmod = t; 347 dcs->d_lmod = t;
348 } else if (t == FLOAT || t == DOUBLE) { 348 } else if (t == FLOAT || t == DOUBLE) {
349 if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) { 349 if (dcs->d_lmod == NOTSPEC || dcs->d_lmod == LONG) {
350 if (dcs->d_cmod != NOTSPEC 350 if (dcs->d_cmod != NOTSPEC
351 || (t == FLOAT && dcs->d_lmod == LONG)) 351 || (t == FLOAT && dcs->d_lmod == LONG))
352 dcs->d_terr = 1; 352 dcs->d_terr = 1;
353 dcs->d_cmod = t; 353 dcs->d_cmod = t;
354 } else { 354 } else {
355 if (dcs->d_atyp != NOTSPEC) 355 if (dcs->d_atyp != NOTSPEC)
356 dcs->d_terr = 1; 356 dcs->d_terr = 1;
357 dcs->d_atyp = t; 357 dcs->d_atyp = t;
358 } 358 }
359 } else if (t == PTR) { 359 } else if (t == PTR) {
360 dcs->d_type = tp; 360 dcs->d_type = tp;
361 } else { 361 } else {
362 /* 362 /*
363 * remember specifiers "void", "char", "int", 363 * remember specifiers "void", "char", "int",
364 * or "_Complex" int dcs->d_atyp 364 * or "_Complex" int dcs->d_atyp
365 */ 365 */
366 if (dcs->d_atyp != NOTSPEC) 366 if (dcs->d_atyp != NOTSPEC)
367 /* more than one, print error in deftyp() */ 367 /* more than one, print error in deftyp() */
368 dcs->d_terr = 1; 368 dcs->d_terr = 1;
369 dcs->d_atyp = t; 369 dcs->d_atyp = t;
370 } 370 }
371} 371}
372 372
373/* 373/*
374 * called if a list of declaration specifiers contains a typedef name 374 * called if a list of declaration specifiers contains a typedef name
375 * and other specifiers (except struct, union, enum, typedef name) 375 * and other specifiers (except struct, union, enum, typedef name)
376 */ 376 */
377static type_t * 377static type_t *
378tdeferr(type_t *td, tspec_t t) 378tdeferr(type_t *td, tspec_t t)
379{ 379{
380 tspec_t t2; 380 tspec_t t2;
381 381
382 t2 = td->t_tspec; 382 t2 = td->t_tspec;
383 383
384 switch (t) { 384 switch (t) {
385 case SIGNED: 385 case SIGNED:
386 case UNSIGN: 386 case UNSIGN:
387 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG || 387 if (t2 == CHAR || t2 == SHORT || t2 == INT || t2 == LONG ||
388 t2 == QUAD) { 388 t2 == QUAD) {
389 if (!tflag) 389 if (!tflag)
390 /* modifying typedef with '%s'; only ... */ 390 /* modifying typedef with '%s'; only ... */
391 warning(5, ttab[t].tt_name); 391 warning(5, ttab[t].tt_name);
392 td = duptyp(gettyp(merge_type_specifiers(t2, t))); 392 td = duptyp(gettyp(merge_type_specifiers(t2, t)));
393 td->t_typedef = 1; 393 td->t_typedef = 1;
394 return td; 394 return td;
395 } 395 }
396 break; 396 break;
397 case SHORT: 397 case SHORT:
398 if (t2 == INT || t2 == UINT) { 398 if (t2 == INT || t2 == UINT) {
399 /* modifying typedef with '%s'; only qualifiers ... */ 399 /* modifying typedef with '%s'; only qualifiers ... */
400 warning(5, "short"); 400 warning(5, "short");
401 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT)); 401 td = duptyp(gettyp(t2 == INT ? SHORT : USHORT));
402 td->t_typedef = 1; 402 td->t_typedef = 1;
403 return td; 403 return td;
404 } 404 }
405 break; 405 break;
406 case LONG: 406 case LONG:
407 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG || 407 if (t2 == INT || t2 == UINT || t2 == LONG || t2 == ULONG ||
408 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) { 408 t2 == FLOAT || t2 == DOUBLE || t2 == DCOMPLEX) {
409 /* modifying typedef with ... */ 409 /* modifying typedef with ... */
410 warning(5, "long"); 410 warning(5, "long");
411 if (t2 == INT) { 411 if (t2 == INT) {
412 td = gettyp(LONG); 412 td = gettyp(LONG);
413 } else if (t2 == UINT) { 413 } else if (t2 == UINT) {
414 td = gettyp(ULONG); 414 td = gettyp(ULONG);
415 } else if (t2 == LONG) { 415 } else if (t2 == LONG) {
416 td = gettyp(QUAD); 416 td = gettyp(QUAD);
417 } else if (t2 == ULONG) { 417 } else if (t2 == ULONG) {
418 td = gettyp(UQUAD); 418 td = gettyp(UQUAD);
419 } else if (t2 == FLOAT) { 419 } else if (t2 == FLOAT) {
420 td = gettyp(DOUBLE); 420 td = gettyp(DOUBLE);
421 } else if (t2 == DOUBLE) { 421 } else if (t2 == DOUBLE) {
422 td = gettyp(LDOUBLE); 422 td = gettyp(LDOUBLE);
423 } else if (t2 == DCOMPLEX) { 423 } else if (t2 == DCOMPLEX) {
424 td = gettyp(LCOMPLEX); 424 td = gettyp(LCOMPLEX);
425 } 425 }
426 td = duptyp(td); 426 td = duptyp(td);
427 td->t_typedef = 1; 427 td->t_typedef = 1;
428 return td; 428 return td;
429 } 429 }
430 break; 430 break;
431 /* LINTED206: (enumeration values not handled in switch) */ 431 /* LINTED206: (enumeration values not handled in switch) */
432 case NOTSPEC: 432 case NOTSPEC:
433 case USHORT: 433 case USHORT:
434 case UCHAR: 434 case UCHAR:
435 case SCHAR: 435 case SCHAR:
436 case CHAR: 436 case CHAR:
437 case BOOL: 437 case BOOL:
438 case FUNC: 438 case FUNC:
439 case ARRAY: 439 case ARRAY:
440 case PTR: 440 case PTR:
441 case ENUM: 441 case ENUM:
442 case UNION: 442 case UNION:
443 case STRUCT: 443 case STRUCT:
444 case VOID: 444 case VOID:
445 case LDOUBLE: 445 case LDOUBLE:
446 case FLOAT: 446 case FLOAT:
447 case DOUBLE: 447 case DOUBLE:
448 case UQUAD: 448 case UQUAD:
449 case QUAD: 449 case QUAD:
450#ifdef INT128_SIZE 450#ifdef INT128_SIZE
451 case UINT128: 451 case UINT128:
452 case INT128: 452 case INT128:
453#endif 453#endif
454 case ULONG: 454 case ULONG:
455 case UINT: 455 case UINT:
456 case INT: 456 case INT:
457 case FCOMPLEX: 457 case FCOMPLEX:
458 case DCOMPLEX: 458 case DCOMPLEX:
459 case LCOMPLEX: 459 case LCOMPLEX:
460 case COMPLEX: 460 case COMPLEX:
461 break; 461 break;
462 } 462 }
463 463
464 /* Anything other is not accepted. */ 464 /* Anything other is not accepted. */
465 465
466 dcs->d_terr = 1; 466 dcs->d_terr = 1;
467 return td; 467 return td;
468} 468}
469 469
470/* 470/*
471 * Remember the symbol of a typedef name (2nd arg) in a struct, union 471 * Remember the symbol of a typedef name (2nd arg) in a struct, union
472 * or enum tag if the typedef name is the first defined for this tag. 472 * or enum tag if the typedef name is the first defined for this tag.
473 * 473 *
474 * If the tag is unnamed, the typdef name is used for identification 474 * If the tag is unnamed, the typdef name is used for identification
475 * of this tag in lint2. Although it's possible that more than one typedef 475 * of this tag in lint2. Although it's possible that more than one typedef
476 * name is defined for one tag, the first name defined should be unique 476 * name is defined for one tag, the first name defined should be unique
477 * if the tag is unnamed. 477 * if the tag is unnamed.
478 */ 478 */
479static void 479static void
480settdsym(type_t *tp, sym_t *sym) 480settdsym(type_t *tp, sym_t *sym)
481{ 481{
482 tspec_t t; 482 tspec_t t;
483 483
484 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 484 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
485 if (tp->t_str->stdef == NULL) 485 if (tp->t_str->stdef == NULL)
486 tp->t_str->stdef = sym; 486 tp->t_str->stdef = sym;
487 } else if (t == ENUM) { 487 } else if (t == ENUM) {
488 if (tp->t_enum->etdef == NULL) 488 if (tp->t_enum->etdef == NULL)
489 tp->t_enum->etdef = sym; 489 tp->t_enum->etdef = sym;
490 } 490 }
491} 491}
492 492
493static size_t 493static size_t
494bitfieldsize(sym_t **mem) 494bitfieldsize(sym_t **mem)
495{ 495{
496 size_t len = (*mem)->s_type->t_flen; 496 size_t len = (*mem)->s_type->t_flen;
497 while (*mem && (*mem)->s_type->t_isfield) { 497 while (*mem && (*mem)->s_type->t_bitfield) {
498 len += (*mem)->s_type->t_flen; 498 len += (*mem)->s_type->t_flen;
499 *mem = (*mem)->s_next; 499 *mem = (*mem)->s_next;
500 } 500 }
501 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE; 501 return ((len + INT_SIZE - 1) / INT_SIZE) * INT_SIZE;
502} 502}
503 503
504static void 504static void
505setpackedsize(type_t *tp) 505setpackedsize(type_t *tp)
506{ 506{
507 str_t *sp; 507 str_t *sp;
508 sym_t *mem; 508 sym_t *mem;
509 509
510 switch (tp->t_tspec) { 510 switch (tp->t_tspec) {
511 case STRUCT: 511 case STRUCT:
512 case UNION: 512 case UNION:
513 sp = tp->t_str; 513 sp = tp->t_str;
514 sp->size = 0; 514 sp->size = 0;
515 for (mem = sp->memb; mem != NULL; mem = mem->s_next) { 515 for (mem = sp->memb; mem != NULL; mem = mem->s_next) {
516 if (mem->s_type->t_isfield) { 516 if (mem->s_type->t_bitfield) {
517 sp->size += bitfieldsize(&mem); 517 sp->size += bitfieldsize(&mem);
518 if (mem == NULL) 518 if (mem == NULL)
519 break; 519 break;
520 } 520 }
521 size_t x = (size_t)tsize(mem->s_type); 521 size_t x = (size_t)tsize(mem->s_type);
522 if (tp->t_tspec == STRUCT) 522 if (tp->t_tspec == STRUCT)
523 sp->size += x; 523 sp->size += x;
524 else if (x > sp->size) 524 else if (x > sp->size)
525 sp->size = x; 525 sp->size = x;
526 } 526 }
527 break; 527 break;
528 default: 528 default:
529 /* %s attribute ignored for %s */ 529 /* %s attribute ignored for %s */
530 warning(326, "packed", type_name(tp)); 530 warning(326, "packed", type_name(tp));
531 break; 531 break;
532 } 532 }
533} 533}
534 534
535void 535void
536addpacked(void) 536addpacked(void)
537{ 537{
538 if (dcs->d_type == NULL) 538 if (dcs->d_type == NULL)
539 dcs->d_ispacked = 1; 539 dcs->d_ispacked = 1;
540 else 540 else
541 setpackedsize(dcs->d_type); 541 setpackedsize(dcs->d_type);
542} 542}
543 543
544void 544void
545add_attr_used(void) 545add_attr_used(void)
546{ 546{
547 dcs->d_used = 1; 547 dcs->d_used = 1;
548} 548}
549 549
550/* 550/*
551 * Remember a qualifier which is part of the declaration specifiers 551 * Remember a qualifier which is part of the declaration specifiers
552 * (and not the declarator) in the top element of the declaration stack. 552 * (and not the declarator) in the top element of the declaration stack.
553 * Also detect multiple qualifiers of the same kind. 553 * Also detect multiple qualifiers of the same kind.
554 554
555 * The remembered qualifier is used by deftyp() to construct the type 555 * The remembered qualifier is used by deftyp() to construct the type
556 * for all declarators. 556 * for all declarators.
557 */ 557 */
558void 558void
559add_qualifier(tqual_t q) 559add_qualifier(tqual_t q)
560{ 560{
561 561
562 if (q == CONST) { 562 if (q == CONST) {
563 if (dcs->d_const) { 563 if (dcs->d_const) {
564 /* duplicate '%s' */ 564 /* duplicate '%s' */
565 warning(10, "const"); 565 warning(10, "const");
566 } 566 }
567 dcs->d_const = 1; 567 dcs->d_const = 1;
568 } else { 568 } else {
569 if (q == THREAD) 569 if (q == THREAD)
570 return; 570 return;
571 lint_assert(q == VOLATILE); 571 lint_assert(q == VOLATILE);
572 if (dcs->d_volatile) { 572 if (dcs->d_volatile) {
573 /* duplicate '%s' */ 573 /* duplicate '%s' */
574 warning(10, "volatile"); 574 warning(10, "volatile");
575 } 575 }
576 dcs->d_volatile = 1; 576 dcs->d_volatile = 1;
577 } 577 }
578} 578}
579 579
580/* 580/*
581 * Go to the next declaration level (structs, nested structs, blocks, 581 * Go to the next declaration level (structs, nested structs, blocks,
582 * argument declaration lists ...) 582 * argument declaration lists ...)
583 */ 583 */
584void 584void
585pushdecl(scl_t sc) 585pushdecl(scl_t sc)
586{ 586{
587 dinfo_t *di; 587 dinfo_t *di;
588 588
589 /* put a new element on the declaration stack */ 589 /* put a new element on the declaration stack */
590 di = xcalloc(1, sizeof (dinfo_t)); 590 di = xcalloc(1, sizeof (dinfo_t));
591 di->d_next = dcs; 591 di->d_next = dcs;
592 dcs = di; 592 dcs = di;
593 di->d_ctx = sc; 593 di->d_ctx = sc;
594 di->d_ldlsym = &di->d_dlsyms; 594 di->d_ldlsym = &di->d_dlsyms;
595 if (dflag) 595 if (dflag)
596 (void)printf("pushdecl(%p %d)\n", dcs, (int)sc); 596 (void)printf("pushdecl(%p %d)\n", dcs, (int)sc);
597 597
598} 598}
599 599
600/* 600/*
601 * Go back to previous declaration level 601 * Go back to previous declaration level
602 */ 602 */
603void 603void
604popdecl(void) 604popdecl(void)
605{ 605{
606 dinfo_t *di; 606 dinfo_t *di;
607 607
608 if (dflag) 608 if (dflag)
609 (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx); 609 (void)printf("popdecl(%p %d)\n", dcs, (int)dcs->d_ctx);
610 610
611 lint_assert(dcs->d_next != NULL); 611 lint_assert(dcs->d_next != NULL);
612 di = dcs; 612 di = dcs;
613 dcs = di->d_next; 613 dcs = di->d_next;
614 switch (di->d_ctx) { 614 switch (di->d_ctx) {
615 case MOS: 615 case MOS:
616 case MOU: 616 case MOU:
617 case ENUMCON: 617 case ENUMCON:
618 /* 618 /*
619 * Symbols declared in (nested) structs or enums are 619 * Symbols declared in (nested) structs or enums are
620 * part of the next level (they are removed from the 620 * part of the next level (they are removed from the
621 * symbol table if the symbols of the outher level are 621 * symbol table if the symbols of the outher level are
622 * removed). 622 * removed).
623 */ 623 */
624 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 624 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
625 dcs->d_ldlsym = di->d_ldlsym; 625 dcs->d_ldlsym = di->d_ldlsym;
626 break; 626 break;
627 case ARG: 627 case ARG:
628 /* 628 /*
629 * All symbols in dcs->d_dlsyms are introduced in old style 629 * All symbols in dcs->d_dlsyms are introduced in old style
630 * argument declarations (it's not clean, but possible). 630 * argument declarations (it's not clean, but possible).
631 * They are appended to the list of symbols declared in 631 * They are appended to the list of symbols declared in
632 * an old style argument identifier list or a new style 632 * an old style argument identifier list or a new style
633 * parameter type list. 633 * parameter type list.
634 */ 634 */
635 if (di->d_dlsyms != NULL) { 635 if (di->d_dlsyms != NULL) {
636 *di->d_ldlsym = dcs->d_fpsyms; 636 *di->d_ldlsym = dcs->d_fpsyms;
637 dcs->d_fpsyms = di->d_dlsyms; 637 dcs->d_fpsyms = di->d_dlsyms;
638 } 638 }
639 break; 639 break;
640 case ABSTRACT: 640 case ABSTRACT:
641 /* 641 /*
642 * casts and sizeof 642 * casts and sizeof
643 * Append all symbols declared in the abstract declaration 643 * Append all symbols declared in the abstract declaration
644 * to the list of symbols declared in the surrounding 644 * to the list of symbols declared in the surrounding
645 * declaration or block. 645 * declaration or block.
646 * XXX I'm not sure whether they should be removed from the 646 * XXX I'm not sure whether they should be removed from the
647 * symbol table now or later. 647 * symbol table now or later.
648 */ 648 */
649 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL) 649 if ((*dcs->d_ldlsym = di->d_dlsyms) != NULL)
650 dcs->d_ldlsym = di->d_ldlsym; 650 dcs->d_ldlsym = di->d_ldlsym;
651 break; 651 break;
652 case AUTO: 652 case AUTO:
653 /* check usage of local vars */ 653 /* check usage of local vars */
654 check_usage(di); 654 check_usage(di);
655 /* FALLTHROUGH */ 655 /* FALLTHROUGH */
656 case PARG: 656 case PARG:
657 /* usage of arguments will be checked by funcend() */ 657 /* usage of arguments will be checked by funcend() */
658 rmsyms(di->d_dlsyms); 658 rmsyms(di->d_dlsyms);
659 break; 659 break;
660 case EXTERN: 660 case EXTERN:
661 /* there is nothing after external declarations */ 661 /* there is nothing after external declarations */
662 /* FALLTHROUGH */ 662 /* FALLTHROUGH */
663 default: 663 default:
664 lint_assert(/*CONSTCOND*/0); 664 lint_assert(/*CONSTCOND*/0);
665 } 665 }
666 free(di); 666 free(di);
667} 667}
668 668
669/* 669/*
670 * Set flag d_asm in all declaration stack elements up to the 670 * Set flag d_asm in all declaration stack elements up to the
671 * outermost one. 671 * outermost one.
672 * 672 *
673 * This is used to mark compound statements which have, possibly in 673 * This is used to mark compound statements which have, possibly in
674 * nested compound statements, asm statements. For these compound 674 * nested compound statements, asm statements. For these compound
675 * statements no warnings about unused or unitialized variables are 675 * statements no warnings about unused or unitialized variables are
676 * printed. 676 * printed.
677 * 677 *
678 * There is no need to clear d_asm in dinfo structs with context AUTO, 678 * There is no need to clear d_asm in dinfo structs with context AUTO,
679 * because these structs are freed at the end of the compound statement. 679 * because these structs are freed at the end of the compound statement.
680 * But it must be cleared in the outermost dinfo struct, which has 680 * But it must be cleared in the outermost dinfo struct, which has
681 * context EXTERN. This could be done in clrtyp() and would work for 681 * context EXTERN. This could be done in clrtyp() and would work for
682 * C, but not for C++ (due to mixed statements and declarations). Thus 682 * C, but not for C++ (due to mixed statements and declarations). Thus
683 * we clear it in global_clean_up_decl(), which is used to do some cleanup 683 * we clear it in global_clean_up_decl(), which is used to do some cleanup
684 * after global declarations/definitions. 684 * after global declarations/definitions.
685 */ 685 */
686void 686void
687setasm(void) 687setasm(void)
688{ 688{
689 dinfo_t *di; 689 dinfo_t *di;
690 690
691 for (di = dcs; di != NULL; di = di->d_next) 691 for (di = dcs; di != NULL; di = di->d_next)
692 di->d_asm = 1; 692 di->d_asm = 1;
693} 693}
694 694
695/* 695/*
696 * Clean all elements of the top element of declaration stack which 696 * Clean all elements of the top element of declaration stack which
697 * will be used by the next declaration 697 * will be used by the next declaration
698 */ 698 */
699void 699void
700clrtyp(void) 700clrtyp(void)
701{ 701{
702 702
703 dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC; 703 dcs->d_atyp = dcs->d_cmod = dcs->d_smod = dcs->d_lmod = NOTSPEC;
704 dcs->d_scl = NOSCL; 704 dcs->d_scl = NOSCL;
705 dcs->d_type = NULL; 705 dcs->d_type = NULL;
706 dcs->d_const = dcs->d_volatile = 0; 706 dcs->d_const = dcs->d_volatile = 0;
707 dcs->d_inline = 0; 707 dcs->d_inline = 0;
708 dcs->d_mscl = dcs->d_terr = 0; 708 dcs->d_mscl = dcs->d_terr = 0;
709 dcs->d_nedecl = 0; 709 dcs->d_nedecl = 0;
710 dcs->d_notyp = 0; 710 dcs->d_notyp = 0;
711} 711}
712 712
713/* 713/*
714 * Create a type structure from the information gathered in 714 * Create a type structure from the information gathered in
715 * the declaration stack. 715 * the declaration stack.
716 * Complain about storage classes which are not possible in current 716 * Complain about storage classes which are not possible in current
717 * context. 717 * context.
718 */ 718 */
719void 719void
720deftyp(void) 720deftyp(void)
721{ 721{
722 tspec_t t, s, l, c; 722 tspec_t t, s, l, c;
723 type_t *tp; 723 type_t *tp;
724 scl_t scl; 724 scl_t scl;
725 725
726 t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */ 726 t = dcs->d_atyp; /* BOOL, CHAR, INT, COMPLEX, VOID */
727 s = dcs->d_smod; /* SIGNED, UNSIGNED */ 727 s = dcs->d_smod; /* SIGNED, UNSIGNED */
728 l = dcs->d_lmod; /* SHORT, LONG, QUAD */ 728 l = dcs->d_lmod; /* SHORT, LONG, QUAD */
729 c = dcs->d_cmod; /* FLOAT, DOUBLE */ 729 c = dcs->d_cmod; /* FLOAT, DOUBLE */
730 tp = dcs->d_type; 730 tp = dcs->d_type;
731 scl = dcs->d_scl; 731 scl = dcs->d_scl;
732 732
733#ifdef DEBUG 733#ifdef DEBUG
734 printf("%s: %s\n", __func__, type_name(tp)); 734 printf("%s: %s\n", __func__, type_name(tp));
735#endif 735#endif
736 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC && 736 if (t == NOTSPEC && s == NOTSPEC && l == NOTSPEC && c == NOTSPEC &&
737 tp == NULL) 737 tp == NULL)
738 dcs->d_notyp = 1; 738 dcs->d_notyp = 1;
739 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) && 739 if (t == NOTSPEC && s == NOTSPEC && (l == NOTSPEC || l == LONG) &&
740 tp == NULL) 740 tp == NULL)
741 t = c; 741 t = c;
742 742
743 if (tp != NULL) { 743 if (tp != NULL) {
744 lint_assert(t == NOTSPEC); 744 lint_assert(t == NOTSPEC);
745 lint_assert(s == NOTSPEC); 745 lint_assert(s == NOTSPEC);
746 lint_assert(l == NOTSPEC); 746 lint_assert(l == NOTSPEC);
747 } 747 }
748 748
749 if (tp == NULL) { 749 if (tp == NULL) {
750 switch (t) { 750 switch (t) {
751 case BOOL: 751 case BOOL:
752 break; 752 break;
753 case NOTSPEC: 753 case NOTSPEC:
754 t = INT; 754 t = INT;
755 /* FALLTHROUGH */ 755 /* FALLTHROUGH */
756 case INT: 756 case INT:
757 if (s == NOTSPEC) 757 if (s == NOTSPEC)
758 s = SIGNED; 758 s = SIGNED;
759 break; 759 break;
760 case CHAR: 760 case CHAR:
761 if (l != NOTSPEC) { 761 if (l != NOTSPEC) {
762 dcs->d_terr = 1; 762 dcs->d_terr = 1;
763 l = NOTSPEC; 763 l = NOTSPEC;
764 } 764 }
765 break; 765 break;
766 case FLOAT: 766 case FLOAT:
767 if (l == LONG) { 767 if (l == LONG) {
768 l = NOTSPEC; 768 l = NOTSPEC;
769 t = DOUBLE; 769 t = DOUBLE;
770 if (!tflag) 770 if (!tflag)
771 /* use 'double' instead of 'long ... */ 771 /* use 'double' instead of 'long ... */
772 warning(6); 772 warning(6);
773 } 773 }
774 break; 774 break;
775 case DOUBLE: 775 case DOUBLE:
776 if (l == LONG) { 776 if (l == LONG) {
777 case LDOUBLE: 777 case LDOUBLE:
778 l = NOTSPEC; 778 l = NOTSPEC;
779 t = LDOUBLE; 779 t = LDOUBLE;
780 if (tflag) 780 if (tflag)
781 /* 'long double' is illegal in ... */ 781 /* 'long double' is illegal in ... */
782 warning(266); 782 warning(266);
783 } 783 }
784 break; 784 break;
785 case DCOMPLEX: 785 case DCOMPLEX:
786 if (l == LONG) { 786 if (l == LONG) {
787 l = NOTSPEC; 787 l = NOTSPEC;
788 t = LCOMPLEX; 788 t = LCOMPLEX;
789 if (tflag) 789 if (tflag)
790 /* 'long double' is illegal in ... */ 790 /* 'long double' is illegal in ... */
791 warning(266); 791 warning(266);
792 } 792 }
793 break; 793 break;
794 case VOID: 794 case VOID:
795 case FCOMPLEX: 795 case FCOMPLEX:
796 case LCOMPLEX: 796 case LCOMPLEX:
797 break; 797 break;
798 default: 798 default:
799 LERROR("deftyp(%s)", tspec_name(t)); 799 LERROR("deftyp(%s)", tspec_name(t));
800 } 800 }
801 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) { 801 if (t != INT && t != CHAR && (s != NOTSPEC || l != NOTSPEC)) {
802 dcs->d_terr = 1; 802 dcs->d_terr = 1;
803 l = s = NOTSPEC; 803 l = s = NOTSPEC;
804 } 804 }
805 if (l != NOTSPEC) 805 if (l != NOTSPEC)
806 t = l; 806 t = l;
807 dcs->d_type = gettyp(merge_type_specifiers(t, s)); 807 dcs->d_type = gettyp(merge_type_specifiers(t, s));
808 } 808 }
809 809
810 if (dcs->d_mscl) { 810 if (dcs->d_mscl) {
811 /* only one storage class allowed */ 811 /* only one storage class allowed */
812 error(7); 812 error(7);
813 } 813 }
814 if (dcs->d_terr) { 814 if (dcs->d_terr) {
815 /* illegal type combination */ 815 /* illegal type combination */
816 error(4); 816 error(4);
817 } 817 }
818 818
819 if (dcs->d_ctx == EXTERN) { 819 if (dcs->d_ctx == EXTERN) {
820 if (scl == REG || scl == AUTO) { 820 if (scl == REG || scl == AUTO) {
821 /* illegal storage class */ 821 /* illegal storage class */
822 error(8); 822 error(8);
823 scl = NOSCL; 823 scl = NOSCL;
824 } 824 }
825 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) { 825 } else if (dcs->d_ctx == ARG || dcs->d_ctx == PARG) {
826 if (scl != NOSCL && scl != REG) { 826 if (scl != NOSCL && scl != REG) {
827 /* only register valid ... */ 827 /* only register valid ... */
828 error(9); 828 error(9);
829 scl = NOSCL; 829 scl = NOSCL;
830 } 830 }
831 } 831 }
832 832
833 dcs->d_scl = scl; 833 dcs->d_scl = scl;
834 834
835 if (dcs->d_const && dcs->d_type->t_const) { 835 if (dcs->d_const && dcs->d_type->t_const) {
836 lint_assert(dcs->d_type->t_typedef); 836 lint_assert(dcs->d_type->t_typedef);
837 /* typedef already qualified with '%s' */ 837 /* typedef already qualified with '%s' */
838 warning(68, "const"); 838 warning(68, "const");
839 } 839 }
840 if (dcs->d_volatile && dcs->d_type->t_volatile) { 840 if (dcs->d_volatile && dcs->d_type->t_volatile) {
841 lint_assert(dcs->d_type->t_typedef); 841 lint_assert(dcs->d_type->t_typedef);
842 /* typedef already qualified with '%s' */ 842 /* typedef already qualified with '%s' */
843 warning(68, "volatile"); 843 warning(68, "volatile");
844 } 844 }
845 845
846 if (dcs->d_const || dcs->d_volatile) { 846 if (dcs->d_const || dcs->d_volatile) {
847 dcs->d_type = duptyp(dcs->d_type); 847 dcs->d_type = duptyp(dcs->d_type);
848 dcs->d_type->t_const |= dcs->d_const; 848 dcs->d_type->t_const |= dcs->d_const;
849 dcs->d_type->t_volatile |= dcs->d_volatile; 849 dcs->d_type->t_volatile |= dcs->d_volatile;
850 } 850 }
851} 851}
852 852
853/* 853/*
854 * Merge type specifiers (char, ..., long long, signed, unsigned). 854 * Merge type specifiers (char, ..., long long, signed, unsigned).
855 */ 855 */
856static tspec_t 856static tspec_t
857merge_type_specifiers(tspec_t t, tspec_t s) 857merge_type_specifiers(tspec_t t, tspec_t s)
858{ 858{
859 859
860 if (s == SIGNED || s == UNSIGN) { 860 if (s == SIGNED || s == UNSIGN) {
861 if (t == CHAR) { 861 if (t == CHAR) {
862 t = s == SIGNED ? SCHAR : UCHAR; 862 t = s == SIGNED ? SCHAR : UCHAR;
863 } else if (t == SHORT) { 863 } else if (t == SHORT) {
864 t = s == SIGNED ? SHORT : USHORT; 864 t = s == SIGNED ? SHORT : USHORT;
865 } else if (t == INT) { 865 } else if (t == INT) {
866 t = s == SIGNED ? INT : UINT; 866 t = s == SIGNED ? INT : UINT;
867 } else if (t == LONG) { 867 } else if (t == LONG) {
868 t = s == SIGNED ? LONG : ULONG; 868 t = s == SIGNED ? LONG : ULONG;
869 } else if (t == QUAD) { 869 } else if (t == QUAD) {
870 t = s == SIGNED ? QUAD : UQUAD; 870 t = s == SIGNED ? QUAD : UQUAD;
871 } 871 }
872 } 872 }
873 873
874 return t; 874 return t;
875} 875}
876 876
877/* 877/*
878 * Return the length of a type in bits. 878 * Return the length of a type in bits.
879 * 879 *
880 * Printing a message if the outhermost dimension of an array is 0 must 880 * Printing a message if the outhermost dimension of an array is 0 must
881 * be done by the caller. All other problems are reported by length() 881 * be done by the caller. All other problems are reported by length()
882 * if name is not NULL. 882 * if name is not NULL.
883 */ 883 */
884int 884int
885length(type_t *tp, const char *name) 885length(type_t *tp, const char *name)
886{ 886{
887 int elem, elsz; 887 int elem, elsz;
888 888
889 elem = 1; 889 elem = 1;
890 while (tp && tp->t_tspec == ARRAY) { 890 while (tp && tp->t_tspec == ARRAY) {
891 elem *= tp->t_dim; 891 elem *= tp->t_dim;
892 tp = tp->t_subt; 892 tp = tp->t_subt;
893 } 893 }
894 if (tp == NULL) 894 if (tp == NULL)
895 return -1; 895 return -1;
896 896
897 switch (tp->t_tspec) { 897 switch (tp->t_tspec) {
898 case FUNC: 898 case FUNC:
899 /* compiler takes size of function */ 899 /* compiler takes size of function */
900 LERROR("%s", msgs[12]); 900 LERROR("%s", msgs[12]);
901 /* NOTREACHED */ 901 /* NOTREACHED */
902 case STRUCT: 902 case STRUCT:
903 case UNION: 903 case UNION:
904 if (incompl(tp) && name != NULL) { 904 if (incompl(tp) && name != NULL) {
905 /* incomplete structure or union %s: %s */ 905 /* incomplete structure or union %s: %s */
906 error(31, tp->t_str->stag->s_name, name); 906 error(31, tp->t_str->stag->s_name, name);
907 } 907 }
908 elsz = tp->t_str->size; 908 elsz = tp->t_str->size;
909 break; 909 break;
910 case ENUM: 910 case ENUM:
911 if (incompl(tp) && name != NULL) { 911 if (incompl(tp) && name != NULL) {
912 /* incomplete enum type: %s */ 912 /* incomplete enum type: %s */
913 warning(13, name); 913 warning(13, name);
914 } 914 }
915 /* FALLTHROUGH */ 915 /* FALLTHROUGH */
916 default: 916 default:
917 elsz = size(tp->t_tspec); 917 elsz = size(tp->t_tspec);
918 if (elsz <= 0) 918 if (elsz <= 0)
919 LERROR("length(%d)", elsz); 919 LERROR("length(%d)", elsz);
920 break; 920 break;
921 } 921 }
922 return elem * elsz; 922 return elem * elsz;
923} 923}
924 924
925/* 925/*
926 * Get the alignment of the given Type in bits. 926 * Get the alignment of the given Type in bits.
927 */ 927 */
928int 928int
929getbound(type_t *tp) 929getbound(type_t *tp)
930{ 930{
931 size_t a; 931 size_t a;
932 tspec_t t; 932 tspec_t t;
933 933
934 while (tp && tp->t_tspec == ARRAY) 934 while (tp && tp->t_tspec == ARRAY)
935 tp = tp->t_subt; 935 tp = tp->t_subt;
936 936
937 if (tp == NULL) 937 if (tp == NULL)
938 return -1; 938 return -1;
939 939
940 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 940 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
941 a = tp->t_str->align; 941 a = tp->t_str->align;
942 } else if (t == FUNC) { 942 } else if (t == FUNC) {
943 /* compiler takes alignment of function */ 943 /* compiler takes alignment of function */
944 error(14); 944 error(14);
945 a = WORST_ALIGN(1) * CHAR_BIT; 945 a = WORST_ALIGN(1) * CHAR_BIT;
946 } else { 946 } else {
947 if ((a = size(t)) == 0) { 947 if ((a = size(t)) == 0) {
948 a = CHAR_BIT; 948 a = CHAR_BIT;
949 } else if (a > WORST_ALIGN(1) * CHAR_BIT) { 949 } else if (a > WORST_ALIGN(1) * CHAR_BIT) {
950 a = WORST_ALIGN(1) * CHAR_BIT; 950 a = WORST_ALIGN(1) * CHAR_BIT;
951 } 951 }
952 } 952 }
953 lint_assert(a >= CHAR_BIT); 953 lint_assert(a >= CHAR_BIT);
954 lint_assert(a <= WORST_ALIGN(1) * CHAR_BIT); 954 lint_assert(a <= WORST_ALIGN(1) * CHAR_BIT);
955 return a; 955 return a;
956} 956}
957 957
958/* 958/*
959 * Concatenate two lists of symbols by s_next. Used by declarations of 959 * Concatenate two lists of symbols by s_next. Used by declarations of
960 * struct/union/enum elements and parameters. 960 * struct/union/enum elements and parameters.
961 */ 961 */
962sym_t * 962sym_t *
963lnklst(sym_t *l1, sym_t *l2) 963lnklst(sym_t *l1, sym_t *l2)
964{ 964{
965 sym_t *l; 965 sym_t *l;
966 966
967 if ((l = l1) == NULL) 967 if ((l = l1) == NULL)
968 return l2; 968 return l2;
969 while (l1->s_next != NULL) 969 while (l1->s_next != NULL)
970 l1 = l1->s_next; 970 l1 = l1->s_next;
971 l1->s_next = l2; 971 l1->s_next = l2;
972 return l; 972 return l;
973} 973}
974 974
975/* 975/*
976 * Check if the type of the given symbol is valid and print an error 976 * Check if the type of the given symbol is valid and print an error
977 * message if it is not. 977 * message if it is not.
978 * 978 *
979 * Invalid types are: 979 * Invalid types are:
980 * - arrays of incomlete types or functions 980 * - arrays of incomlete types or functions
981 * - functions returning arrays or functions 981 * - functions returning arrays or functions
982 * - void types other than type of function or pointer 982 * - void types other than type of function or pointer
983 */ 983 */
984void 984void
985check_type(sym_t *sym) 985check_type(sym_t *sym)
986{ 986{
987 tspec_t to, t; 987 tspec_t to, t;
988 type_t **tpp, *tp; 988 type_t **tpp, *tp;
989 989
990 tpp = &sym->s_type; 990 tpp = &sym->s_type;
991 to = NOTSPEC; 991 to = NOTSPEC;
992 while ((tp = *tpp) != NULL) { 992 while ((tp = *tpp) != NULL) {
993 t = tp->t_tspec; 993 t = tp->t_tspec;
994 /* 994 /*
995 * If this is the type of an old style function definition, 995 * If this is the type of an old style function definition,
996 * a better warning is printed in funcdef(). 996 * a better warning is printed in funcdef().
997 */ 997 */
998 if (t == FUNC && !tp->t_proto && 998 if (t == FUNC && !tp->t_proto &&
999 !(to == NOTSPEC && sym->s_osdef)) { 999 !(to == NOTSPEC && sym->s_osdef)) {
1000 if (sflag && hflag) 1000 if (sflag && hflag)
1001 /* function declaration is not a prototype */ 1001 /* function declaration is not a prototype */
1002 warning(287); 1002 warning(287);
1003 } 1003 }
1004 if (to == FUNC) { 1004 if (to == FUNC) {
1005 if (t == FUNC || t == ARRAY) { 1005 if (t == FUNC || t == ARRAY) {
1006 /* function returns illegal type */ 1006 /* function returns illegal type */
1007 error(15); 1007 error(15);
1008 if (t == FUNC) { 1008 if (t == FUNC) {
1009 *tpp = incref(*tpp, PTR); 1009 *tpp = incref(*tpp, PTR);
1010 } else { 1010 } else {
1011 *tpp = incref((*tpp)->t_subt, PTR); 1011 *tpp = incref((*tpp)->t_subt, PTR);
1012 } 1012 }
1013 return; 1013 return;
1014 } else if (tp->t_const || tp->t_volatile) { 1014 } else if (tp->t_const || tp->t_volatile) {
1015 if (sflag) { /* XXX oder better !tflag ? */ 1015 if (sflag) { /* XXX oder better !tflag ? */
1016 /* function cannot return const... */ 1016 /* function cannot return const... */
1017 warning(228); 1017 warning(228);
1018 } 1018 }
1019 } 1019 }
1020 } if (to == ARRAY) { 1020 } if (to == ARRAY) {
1021 if (t == FUNC) { 1021 if (t == FUNC) {
1022 /* array of function is illegal */ 1022 /* array of function is illegal */
1023 error(16); 1023 error(16);
1024 *tpp = gettyp(INT); 1024 *tpp = gettyp(INT);
1025 return; 1025 return;
1026 } else if (t == ARRAY && tp->t_dim == 0) { 1026 } else if (t == ARRAY && tp->t_dim == 0) {
1027 /* null dimension */ 1027 /* null dimension */
1028 error(17); 1028 error(17);
1029 return; 1029 return;
1030 } else if (t == VOID) { 1030 } else if (t == VOID) {
1031 /* illegal use of 'void' */ 1031 /* illegal use of 'void' */
1032 error(18); 1032 error(18);
1033 *tpp = gettyp(INT); 1033 *tpp = gettyp(INT);
1034#if 0 /* errors are produced by length() */ 1034#if 0 /* errors are produced by length() */
1035 } else if (incompl(tp)) { 1035 } else if (incompl(tp)) {
1036 /* array of incomplete type */ 1036 /* array of incomplete type */
1037 if (sflag) { 1037 if (sflag) {
1038 /* array of incomplete type */ 1038 /* array of incomplete type */
1039 error(301); 1039 error(301);
1040 } else { 1040 } else {
1041 /* array of incomplete type */ 1041 /* array of incomplete type */
1042 warning(301); 1042 warning(301);
1043 } 1043 }
1044#endif 1044#endif
1045 } 1045 }
1046 } else if (to == NOTSPEC && t == VOID) { 1046 } else if (to == NOTSPEC && t == VOID) {
1047 if (dcs->d_ctx == PARG) { 1047 if (dcs->d_ctx == PARG) {
1048 if (sym->s_scl != ABSTRACT) { 1048 if (sym->s_scl != ABSTRACT) {
1049 lint_assert(sym->s_name != unnamed); 1049 lint_assert(sym->s_name != unnamed);
1050 /* void param. cannot have name: %s */ 1050 /* void param. cannot have name: %s */
1051 error(61, sym->s_name); 1051 error(61, sym->s_name);
1052 *tpp = gettyp(INT); 1052 *tpp = gettyp(INT);
1053 } 1053 }
1054 } else if (dcs->d_ctx == ABSTRACT) { 1054 } else if (dcs->d_ctx == ABSTRACT) {
1055 /* ok */ 1055 /* ok */
1056 } else if (sym->s_scl != TYPEDEF) { 1056 } else if (sym->s_scl != TYPEDEF) {
1057 /* void type for %s */ 1057 /* void type for %s */
1058 error(19, sym->s_name); 1058 error(19, sym->s_name);
1059 *tpp = gettyp(INT); 1059 *tpp = gettyp(INT);
1060 } 1060 }
1061 } 1061 }
1062 if (t == VOID && to != PTR) { 1062 if (t == VOID && to != PTR) {
1063 if (tp->t_const || tp->t_volatile) { 1063 if (tp->t_const || tp->t_volatile) {
1064 /* inappropriate qualifiers with 'void' */ 1064 /* inappropriate qualifiers with 'void' */
1065 warning(69); 1065 warning(69);
1066 tp->t_const = tp->t_volatile = 0; 1066 tp->t_const = tp->t_volatile = 0;
1067 } 1067 }
1068 } 1068 }
1069 tpp = &tp->t_subt; 1069 tpp = &tp->t_subt;
1070 to = t; 1070 to = t;
1071 } 1071 }
1072} 1072}
1073 1073
1074/* 1074/*
1075 * Process the declarator of a struct/union element. 1075 * Process the declarator of a struct/union element.
1076 */ 1076 */
1077sym_t * 1077sym_t *
1078declarator_1_struct_union(sym_t *dsym) 1078declarator_1_struct_union(sym_t *dsym)
1079{ 1079{
1080 type_t *tp; 1080 type_t *tp;
1081 tspec_t t; 1081 tspec_t t;
1082 int sz, len; 1082 int sz, len;
1083 int o = 0; /* Appease gcc */ 1083 int o = 0; /* Appease gcc */
1084 scl_t sc; 1084 scl_t sc;
1085 1085
1086 lint_assert((sc = dsym->s_scl) == MOS || sc == MOU); 1086 lint_assert((sc = dsym->s_scl) == MOS || sc == MOU);
1087 1087
1088 if (dcs->d_rdcsym != NULL) { 1088 if (dcs->d_rdcsym != NULL) {
1089 /* should be ensured by storesym() */ 1089 /* should be ensured by storesym() */
1090 lint_assert((sc = dcs->d_rdcsym->s_scl) == MOS || sc == MOU); 1090 lint_assert((sc = dcs->d_rdcsym->s_scl) == MOS || sc == MOU);
1091 if (dsym->s_styp == dcs->d_rdcsym->s_styp) { 1091 if (dsym->s_styp == dcs->d_rdcsym->s_styp) {
1092 /* duplicate member name: %s */ 1092 /* duplicate member name: %s */
1093 error(33, dsym->s_name); 1093 error(33, dsym->s_name);
1094 rmsym(dcs->d_rdcsym); 1094 rmsym(dcs->d_rdcsym);
1095 } 1095 }
1096 } 1096 }
1097 1097
1098 check_type(dsym); 1098 check_type(dsym);
1099 1099
1100 t = (tp = dsym->s_type)->t_tspec; 1100 t = (tp = dsym->s_type)->t_tspec;
1101 1101
1102 if (dsym->s_bitfield) { 1102 if (dsym->s_bitfield) {
1103 /* 1103 /*
1104 * only unsigned and signed int are portable bit-field types 1104 * only unsigned and signed int are portable bit-field types
1105 *(at least in ANSI C, in traditional C only unsigned int) 1105 *(at least in ANSI C, in traditional C only unsigned int)
1106 */ 1106 */
1107 if (t == CHAR || t == UCHAR || t == SCHAR || 1107 if (t == CHAR || t == UCHAR || t == SCHAR ||
1108 t == SHORT || t == USHORT || t == ENUM) { 1108 t == SHORT || t == USHORT || t == ENUM) {
1109 if (!bitfieldtype_ok) { 1109 if (!bitfieldtype_ok) {
1110 if (sflag) { 1110 if (sflag) {
1111 /* bit-field type '%s' invalid ... */ 1111 /* bit-field type '%s' invalid ... */
1112 warning(273, type_name(tp)); 1112 warning(273, type_name(tp));
1113 } else if (pflag) { 1113 } else if (pflag) {
1114 /* nonportable bit-field type */ 1114 /* nonportable bit-field type */
1115 warning(34); 1115 warning(34);
1116 } 1116 }
1117 } 1117 }
1118 } else if (t == INT && dcs->d_smod == NOTSPEC) { 1118 } else if (t == INT && dcs->d_smod == NOTSPEC) {
1119 if (pflag && !bitfieldtype_ok) { 1119 if (pflag && !bitfieldtype_ok) {
1120 /* nonportable bit-field type */ 1120 /* nonportable bit-field type */
1121 warning(34); 1121 warning(34);
1122 } 1122 }
1123 } else if (t != INT && t != UINT && t != BOOL) { 1123 } else if (t != INT && t != UINT && t != BOOL) {
1124 /* 1124 /*
1125 * Non-integer types are always illegal for 1125 * Non-integer types are always illegal for
1126 * bitfields, regardless of BITFIELDTYPE. 1126 * bitfields, regardless of BITFIELDTYPE.
1127 * Integer types not dealt with above are 1127 * Integer types not dealt with above are
1128 * okay only if BITFIELDTYPE is in effect. 1128 * okay only if BITFIELDTYPE is in effect.
1129 */ 1129 */
1130 if (!bitfieldtype_ok || !tspec_is_int(t)) { 1130 if (!bitfieldtype_ok || !tspec_is_int(t)) {
1131 /* illegal bit-field type */ 1131 /* illegal bit-field type */
1132 warning(35); 1132 warning(35);
1133 sz = tp->t_flen; 1133 sz = tp->t_flen;
1134 dsym->s_type = tp = duptyp(gettyp(t = INT)); 1134 dsym->s_type = tp = duptyp(gettyp(t = INT));
1135 if ((tp->t_flen = sz) > size(t)) 1135 if ((tp->t_flen = sz) > size(t))
1136 tp->t_flen = size(t); 1136 tp->t_flen = size(t);
1137 } 1137 }
1138 } 1138 }
1139 if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) { 1139 if ((len = tp->t_flen) < 0 || len > (ssize_t)size(t)) {
1140 /* illegal bit-field size: %d */ 1140 /* illegal bit-field size: %d */
1141 error(36, len); 1141 error(36, len);
1142 tp->t_flen = size(t); 1142 tp->t_flen = size(t);
1143 } else if (len == 0 && dsym->s_name != unnamed) { 1143 } else if (len == 0 && dsym->s_name != unnamed) {
1144 /* zero size bit-field */ 1144 /* zero size bit-field */
1145 error(37); 1145 error(37);
1146 tp->t_flen = size(t); 1146 tp->t_flen = size(t);
1147 } 1147 }
1148 if (dsym->s_scl == MOU) { 1148 if (dsym->s_scl == MOU) {
1149 /* illegal use of bit-field */ 1149 /* illegal use of bit-field */
1150 error(41); 1150 error(41);
1151 dsym->s_type->t_isfield = 0; 1151 dsym->s_type->t_bitfield = false;
1152 dsym->s_bitfield = 0; 1152 dsym->s_bitfield = 0;
1153 } 1153 }
1154 } else if (t == FUNC) { 1154 } else if (t == FUNC) {
1155 /* function illegal in structure or union */ 1155 /* function illegal in structure or union */
1156 error(38); 1156 error(38);
1157 dsym->s_type = tp = incref(tp, t = PTR); 1157 dsym->s_type = tp = incref(tp, t = PTR);
1158 } 1158 }
1159 1159
1160 /* 1160 /*
1161 * bit-fields of length 0 are not warned about because length() 1161 * bit-fields of length 0 are not warned about because length()
1162 * does not return the length of the bit-field but the length 1162 * does not return the length of the bit-field but the length
1163 * of the type the bit-field is packed in (it's ok) 1163 * of the type the bit-field is packed in (it's ok)
1164 */ 1164 */
1165 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) { 1165 if ((sz = length(dsym->s_type, dsym->s_name)) == 0) {
1166 if (t == ARRAY && dsym->s_type->t_dim == 0) { 1166 if (t == ARRAY && dsym->s_type->t_dim == 0) {
1167 /* zero sized array in struct is a C99 extension: %s */ 1167 /* zero sized array in struct is a C99 extension: %s */
1168 c99ism(39, dsym->s_name); 1168 c99ism(39, dsym->s_name);
1169 } 1169 }
1170 } 1170 }
1171 1171
1172 if (dcs->d_ctx == MOU) { 1172 if (dcs->d_ctx == MOU) {
1173 o = dcs->d_offset; 1173 o = dcs->d_offset;
1174 dcs->d_offset = 0; 1174 dcs->d_offset = 0;
1175 } 1175 }
1176 if (dsym->s_bitfield) { 1176 if (dsym->s_bitfield) {
1177 align(getbound(tp), tp->t_flen); 1177 align(getbound(tp), tp->t_flen);
1178 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t); 1178 dsym->s_value.v_quad = (dcs->d_offset / size(t)) * size(t);
1179 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad; 1179 tp->t_foffs = dcs->d_offset - (int)dsym->s_value.v_quad;
1180 dcs->d_offset += tp->t_flen; 1180 dcs->d_offset += tp->t_flen;
1181 } else { 1181 } else {
1182 align(getbound(tp), 0); 1182 align(getbound(tp), 0);
1183 dsym->s_value.v_quad = dcs->d_offset; 1183 dsym->s_value.v_quad = dcs->d_offset;
1184 dcs->d_offset += sz; 1184 dcs->d_offset += sz;
1185 } 1185 }
1186 if (dcs->d_ctx == MOU) { 1186 if (dcs->d_ctx == MOU) {
1187 if (o > dcs->d_offset) 1187 if (o > dcs->d_offset)
1188 dcs->d_offset = o; 1188 dcs->d_offset = o;
1189 } 1189 }
1190 1190
1191 check_function_definition(dsym, 0); 1191 check_function_definition(dsym, 0);
1192 1192
1193 /* 1193 /*
1194 * Clear the BITFIELDTYPE indicator after processing each 1194 * Clear the BITFIELDTYPE indicator after processing each
1195 * structure element. 1195 * structure element.
1196 */ 1196 */
1197 bitfieldtype_ok = false; 1197 bitfieldtype_ok = false;
1198 1198
1199 return dsym; 1199 return dsym;
1200} 1200}
1201 1201
1202/* 1202/*
1203 * Aligns next structure element as required. 1203 * Aligns next structure element as required.
1204 * 1204 *
1205 * al contains the required alignment, len the length of a bit-field. 1205 * al contains the required alignment, len the length of a bit-field.
1206 */ 1206 */
1207static void 1207static void
1208align(int al, int len) 1208align(int al, int len)
1209{ 1209{
1210 int no; 1210 int no;
1211 1211
1212 /* 1212 /*
1213 * The alignment of the current element becomes the alignment of 1213 * The alignment of the current element becomes the alignment of
1214 * the struct/union if it is larger than the current alignment 1214 * the struct/union if it is larger than the current alignment
1215 * of the struct/union. 1215 * of the struct/union.
1216 */ 1216 */
1217 if (al > dcs->d_stralign) 1217 if (al > dcs->d_stralign)
1218 dcs->d_stralign = al; 1218 dcs->d_stralign = al;
1219 1219
1220 no = (dcs->d_offset + (al - 1)) & ~(al - 1); 1220 no = (dcs->d_offset + (al - 1)) & ~(al - 1);
1221 if (len == 0 || dcs->d_offset + len > no) 1221 if (len == 0 || dcs->d_offset + len > no)
1222 dcs->d_offset = no; 1222 dcs->d_offset = no;
1223} 1223}
1224 1224
1225/* 1225/*
1226 * Remember the width of the field in its type structure. 1226 * Remember the width of the field in its type structure.
1227 */ 1227 */
1228sym_t * 1228sym_t *
1229bitfield(sym_t *dsym, int len) 1229bitfield(sym_t *dsym, int len)
1230{ 1230{
1231 1231
1232 if (dsym == NULL) { 1232 if (dsym == NULL) {
1233 dsym = getblk(sizeof (sym_t)); 1233 dsym = getblk(sizeof (sym_t));
1234 dsym->s_name = unnamed; 1234 dsym->s_name = unnamed;
1235 dsym->s_kind = FMEMBER; 1235 dsym->s_kind = FMEMBER;
1236 dsym->s_scl = MOS; 1236 dsym->s_scl = MOS;
1237 dsym->s_type = gettyp(UINT); 1237 dsym->s_type = gettyp(UINT);
1238 dsym->s_blklev = -1; 1238 dsym->s_blklev = -1;
1239 } 1239 }
1240 dsym->s_type = duptyp(dsym->s_type); 1240 dsym->s_type = duptyp(dsym->s_type);
1241 dsym->s_type->t_isfield = 1; 1241 dsym->s_type->t_bitfield = true;
1242 dsym->s_type->t_flen = len; 1242 dsym->s_type->t_flen = len;
1243 dsym->s_bitfield = 1; 1243 dsym->s_bitfield = 1;
1244 return dsym; 1244 return dsym;
1245} 1245}
1246 1246
1247/* 1247/*
1248 * Collect information about a sequence of asterisks and qualifiers in a 1248 * Collect information about a sequence of asterisks and qualifiers in a
1249 * list of type pqinf_t. 1249 * list of type pqinf_t.
1250 * Qualifiers always refer to the left asterisk. 1250 * Qualifiers always refer to the left asterisk.
1251 * The rightmost asterisk will be at the top of the list. 1251 * The rightmost asterisk will be at the top of the list.
1252 */ 1252 */
1253pqinf_t * 1253pqinf_t *
1254merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2) 1254merge_pointers_and_qualifiers(pqinf_t *p1, pqinf_t *p2)
1255{ 1255{
1256 pqinf_t *p; 1256 pqinf_t *p;
1257 1257
1258 if (p2->p_pcnt != 0) { 1258 if (p2->p_pcnt != 0) {
1259 /* left '*' at the end of the list */ 1259 /* left '*' at the end of the list */
1260 for (p = p2; p->p_next != NULL; p = p->p_next) 1260 for (p = p2; p->p_next != NULL; p = p->p_next)
1261 continue; 1261 continue;
1262 p->p_next = p1; 1262 p->p_next = p1;
1263 return p2; 1263 return p2;
1264 } else { 1264 } else {
1265 if (p2->p_const) { 1265 if (p2->p_const) {
1266 if (p1->p_const) { 1266 if (p1->p_const) {
1267 /* duplicate '%s' */ 1267 /* duplicate '%s' */
1268 warning(10, "const"); 1268 warning(10, "const");
1269 } 1269 }
1270 p1->p_const = 1; 1270 p1->p_const = 1;
1271 } 1271 }
1272 if (p2->p_volatile) { 1272 if (p2->p_volatile) {
1273 if (p1->p_volatile) { 1273 if (p1->p_volatile) {
1274 /* duplicate '%s' */ 1274 /* duplicate '%s' */
1275 warning(10, "volatile"); 1275 warning(10, "volatile");
1276 } 1276 }
1277 p1->p_volatile = 1; 1277 p1->p_volatile = 1;
1278 } 1278 }
1279 free(p2); 1279 free(p2);
1280 return p1; 1280 return p1;
1281 } 1281 }
1282} 1282}
1283 1283
1284/* 1284/*
1285 * The following 3 functions extend the type of a declarator with 1285 * The following 3 functions extend the type of a declarator with
1286 * pointer, function and array types. 1286 * pointer, function and array types.
1287 * 1287 *
1288 * The current type is the Type built by deftyp() (dcs->d_type) and 1288 * The current type is the Type built by deftyp() (dcs->d_type) and
1289 * pointer, function and array types already added for this 1289 * pointer, function and array types already added for this
1290 * declarator. The new type extension is inserted between both. 1290 * declarator. The new type extension is inserted between both.
1291 */ 1291 */
1292sym_t * 1292sym_t *
1293add_pointer(sym_t *decl, pqinf_t *pi) 1293add_pointer(sym_t *decl, pqinf_t *pi)
1294{ 1294{
1295 type_t **tpp, *tp; 1295 type_t **tpp, *tp;
1296 pqinf_t *npi; 1296 pqinf_t *npi;
1297 1297
1298 tpp = &decl->s_type; 1298 tpp = &decl->s_type;
1299 while (*tpp && *tpp != dcs->d_type) 1299 while (*tpp && *tpp != dcs->d_type)
1300 tpp = &(*tpp)->t_subt; 1300 tpp = &(*tpp)->t_subt;
1301 if (*tpp == NULL) 1301 if (*tpp == NULL)
1302 return decl; 1302 return decl;
1303 1303
1304 while (pi != NULL) { 1304 while (pi != NULL) {
1305 *tpp = tp = getblk(sizeof (type_t)); 1305 *tpp = tp = getblk(sizeof (type_t));
1306 tp->t_tspec = PTR; 1306 tp->t_tspec = PTR;
1307 tp->t_const = pi->p_const; 1307 tp->t_const = pi->p_const;
1308 tp->t_volatile = pi->p_volatile; 1308 tp->t_volatile = pi->p_volatile;
1309 *(tpp = &tp->t_subt) = dcs->d_type; 1309 *(tpp = &tp->t_subt) = dcs->d_type;
1310 npi = pi->p_next; 1310 npi = pi->p_next;
1311 free(pi); 1311 free(pi);
1312 pi = npi; 1312 pi = npi;
1313 } 1313 }
1314 return decl; 1314 return decl;
1315} 1315}
1316 1316
1317/* 1317/*
1318 * If a dimension was specified, dim is 1, otherwise 0 1318 * If a dimension was specified, dim is 1, otherwise 0
1319 * n is the specified dimension 1319 * n is the specified dimension
1320 */ 1320 */
1321sym_t * 1321sym_t *
1322add_array(sym_t *decl, int dim, int n) 1322add_array(sym_t *decl, int dim, int n)
1323{ 1323{
1324 type_t **tpp, *tp; 1324 type_t **tpp, *tp;
1325 1325
1326 tpp = &decl->s_type; 1326 tpp = &decl->s_type;
1327 while (*tpp && *tpp != dcs->d_type) 1327 while (*tpp && *tpp != dcs->d_type)
1328 tpp = &(*tpp)->t_subt; 1328 tpp = &(*tpp)->t_subt;
1329 if (*tpp == NULL) 1329 if (*tpp == NULL)
1330 return decl; 1330 return decl;
1331 1331
1332 *tpp = tp = getblk(sizeof (type_t)); 1332 *tpp = tp = getblk(sizeof (type_t));
1333 tp->t_tspec = ARRAY; 1333 tp->t_tspec = ARRAY;
1334 tp->t_subt = dcs->d_type; 1334 tp->t_subt = dcs->d_type;
1335 tp->t_dim = n; 1335 tp->t_dim = n;
1336 1336
1337 if (n < 0) { 1337 if (n < 0) {
1338 /* negative array dimension (%d) */ 1338 /* negative array dimension (%d) */
1339 error(20, n); 1339 error(20, n);
1340 n = 0; 1340 n = 0;
1341 } else if (n == 0 && dim) { 1341 } else if (n == 0 && dim) {
1342 /* zero sized array is a C99 extension */ 1342 /* zero sized array is a C99 extension */
1343 c99ism(322); 1343 c99ism(322);
1344 } else if (n == 0 && !dim) { 1344 } else if (n == 0 && !dim) {
1345 setcomplete(tp, 0); 1345 setcomplete(tp, 0);
1346 } 1346 }
1347 1347
1348 return decl; 1348 return decl;
1349} 1349}
1350 1350
1351sym_t * 1351sym_t *
1352add_function(sym_t *decl, sym_t *args) 1352add_function(sym_t *decl, sym_t *args)
1353{ 1353{
1354 type_t **tpp, *tp; 1354 type_t **tpp, *tp;
1355 1355
1356 if (dcs->d_proto) { 1356 if (dcs->d_proto) {
1357 if (tflag) 1357 if (tflag)
1358 /* function prototypes are illegal in traditional C */ 1358 /* function prototypes are illegal in traditional C */
1359 warning(270); 1359 warning(270);
1360 args = new_style_function(decl, args); 1360 args = new_style_function(decl, args);
1361 } else { 1361 } else {
1362 old_style_function(decl, args); 1362 old_style_function(decl, args);
1363 } 1363 }
1364 1364
1365 /* 1365 /*
1366 * The symbols are removed from the symbol table by popdecl() after 1366 * The symbols are removed from the symbol table by popdecl() after
1367 * add_function(). To be able to restore them if this is a function 1367 * add_function(). To be able to restore them if this is a function
1368 * definition, a pointer to the list of all symbols is stored in 1368 * definition, a pointer to the list of all symbols is stored in
1369 * dcs->d_next->d_fpsyms. Also a list of the arguments (concatenated 1369 * dcs->d_next->d_fpsyms. Also a list of the arguments (concatenated
1370 * by s_next) is stored in dcs->d_next->d_fargs. 1370 * by s_next) is stored in dcs->d_next->d_fargs.
1371 * (dcs->d_next must be used because *dcs is the declaration stack 1371 * (dcs->d_next must be used because *dcs is the declaration stack
1372 * element created for the list of params and is removed after 1372 * element created for the list of params and is removed after
1373 * add_function()) 1373 * add_function())
1374 */ 1374 */
1375 if (dcs->d_next->d_ctx == EXTERN && 1375 if (dcs->d_next->d_ctx == EXTERN &&
1376 decl->s_type == dcs->d_next->d_type) { 1376 decl->s_type == dcs->d_next->d_type) {
1377 dcs->d_next->d_fpsyms = dcs->d_dlsyms; 1377 dcs->d_next->d_fpsyms = dcs->d_dlsyms;
1378 dcs->d_next->d_fargs = args; 1378 dcs->d_next->d_fargs = args;
1379 } 1379 }
1380 1380
1381 tpp = &decl->s_type; 1381 tpp = &decl->s_type;
1382 while (*tpp && *tpp != dcs->d_next->d_type) 1382 while (*tpp && *tpp != dcs->d_next->d_type)
1383 tpp = &(*tpp)->t_subt; 1383 tpp = &(*tpp)->t_subt;
1384 if (*tpp == NULL) 1384 if (*tpp == NULL)
1385 return decl; 1385 return decl;
1386 1386
1387 *tpp = tp = getblk(sizeof (type_t)); 1387 *tpp = tp = getblk(sizeof (type_t));
1388 tp->t_tspec = FUNC; 1388 tp->t_tspec = FUNC;
1389 tp->t_subt = dcs->d_next->d_type; 1389 tp->t_subt = dcs->d_next->d_type;
1390 if ((tp->t_proto = dcs->d_proto) != 0) 1390 if ((tp->t_proto = dcs->d_proto) != 0)
1391 tp->t_args = args; 1391 tp->t_args = args;
1392 tp->t_vararg = dcs->d_vararg; 1392 tp->t_vararg = dcs->d_vararg;
1393 1393
1394 return decl; 1394 return decl;
1395} 1395}
1396 1396
1397/* 1397/*
1398 * Called for new style function declarations. 1398 * Called for new style function declarations.
1399 */ 1399 */
1400/* ARGSUSED */ 1400/* ARGSUSED */
1401static sym_t * 1401static sym_t *
1402new_style_function(sym_t *decl, sym_t *args) 1402new_style_function(sym_t *decl, sym_t *args)
1403{ 1403{
1404 sym_t *arg, *sym; 1404 sym_t *arg, *sym;
1405 scl_t sc; 1405 scl_t sc;
1406 int n; 1406 int n;
1407 1407
1408 /* 1408 /*
1409 * Declarations of structs/unions/enums in param lists are legal, 1409 * Declarations of structs/unions/enums in param lists are legal,
1410 * but senseless. 1410 * but senseless.
1411 */ 1411 */
1412 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) { 1412 for (sym = dcs->d_dlsyms; sym != NULL; sym = sym->s_dlnxt) {
1413 sc = sym->s_scl; 1413 sc = sym->s_scl;
1414 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) { 1414 if (sc == STRTAG || sc == UNIONTAG || sc == ENUMTAG) {
1415 /* dubious tag declaration: %s %s */ 1415 /* dubious tag declaration: %s %s */
1416 warning(85, storage_class_name(sc), sym->s_name); 1416 warning(85, storage_class_name(sc), sym->s_name);
1417 } 1417 }
1418 } 1418 }
1419 1419
1420 n = 1; 1420 n = 1;
1421 for (arg = args; arg != NULL; arg = arg->s_next) { 1421 for (arg = args; arg != NULL; arg = arg->s_next) {
1422 if (arg->s_type->t_tspec == VOID) { 1422 if (arg->s_type->t_tspec == VOID) {
1423 if (n > 1 || arg->s_next != NULL) { 1423 if (n > 1 || arg->s_next != NULL) {
1424 /* void must be sole parameter */ 1424 /* void must be sole parameter */
1425 error(60); 1425 error(60);
1426 arg->s_type = gettyp(INT); 1426 arg->s_type = gettyp(INT);
1427 } 1427 }
1428 } 1428 }
1429 n++; 1429 n++;
1430 } 1430 }
1431 1431
1432 /* return NULL if first param is VOID */ 1432 /* return NULL if first param is VOID */
1433 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL; 1433 return args != NULL && args->s_type->t_tspec != VOID ? args : NULL;
1434} 1434}
1435 1435
1436/* 1436/*
1437 * Called for old style function declarations. 1437 * Called for old style function declarations.
1438 */ 1438 */
1439static void 1439static void
1440old_style_function(sym_t *decl, sym_t *args) 1440old_style_function(sym_t *decl, sym_t *args)
1441{ 1441{
1442 1442
1443 /* 1443 /*
1444 * Remember list of params only if this is really seams to be 1444 * Remember list of params only if this is really seams to be
1445 * a function definition. 1445 * a function definition.
1446 */ 1446 */
1447 if (dcs->d_next->d_ctx == EXTERN && 1447 if (dcs->d_next->d_ctx == EXTERN &&
1448 decl->s_type == dcs->d_next->d_type) { 1448 decl->s_type == dcs->d_next->d_type) {
1449 /* 1449 /*
1450 * We assume that this becomes a function definition. If 1450 * We assume that this becomes a function definition. If
1451 * we are wrong, it's corrected in check_function_definition(). 1451 * we are wrong, it's corrected in check_function_definition().
1452 */ 1452 */
1453 if (args != NULL) { 1453 if (args != NULL) {
1454 decl->s_osdef = 1; 1454 decl->s_osdef = 1;
1455 decl->s_args = args; 1455 decl->s_args = args;
1456 } 1456 }
1457 } else { 1457 } else {
1458 if (args != NULL) 1458 if (args != NULL)
1459 /* function prototype parameters must have types */ 1459 /* function prototype parameters must have types */
1460 warning(62); 1460 warning(62);
1461 } 1461 }
1462} 1462}
1463 1463
1464/* 1464/*
1465 * Lists of Identifiers in functions declarations are allowed only if 1465 * Lists of Identifiers in functions declarations are allowed only if
1466 * it's also a function definition. If this is not the case, print a 1466 * it's also a function definition. If this is not the case, print a
1467 * error message. 1467 * error message.
1468 */ 1468 */
1469void 1469void
1470check_function_definition(sym_t *sym, int msg) 1470check_function_definition(sym_t *sym, int msg)
1471{ 1471{
1472 1472
1473 if (sym->s_osdef) { 1473 if (sym->s_osdef) {
1474 if (msg) { 1474 if (msg) {
1475 /* incomplete or misplaced function definition */ 1475 /* incomplete or misplaced function definition */
1476 error(22); 1476 error(22);
1477 } 1477 }
1478 sym->s_osdef = 0; 1478 sym->s_osdef = 0;
1479 sym->s_args = NULL; 1479 sym->s_args = NULL;
1480 } 1480 }
1481} 1481}
1482 1482
1483/* 1483/*
1484 * Process the name in a declarator. 1484 * Process the name in a declarator.
1485 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or 1485 * The symbol gets one of the storage classes EXTERN, STATIC, AUTO or
1486 * TYPEDEF. 1486 * TYPEDEF.
1487 * s_def and s_reg are valid after declarator_name(). 1487 * s_def and s_reg are valid after declarator_name().
1488 */ 1488 */
1489sym_t * 1489sym_t *
1490declarator_name(sym_t *sym) 1490declarator_name(sym_t *sym)
1491{ 1491{
1492 scl_t sc = NOSCL; 1492 scl_t sc = NOSCL;
1493 1493
1494 if (sym->s_scl == NOSCL) { 1494 if (sym->s_scl == NOSCL) {
1495 dcs->d_rdcsym = NULL; 1495 dcs->d_rdcsym = NULL;
1496 } else if (sym->s_defarg) { 1496 } else if (sym->s_defarg) {
1497 sym->s_defarg = 0; 1497 sym->s_defarg = 0;
1498 dcs->d_rdcsym = NULL; 1498 dcs->d_rdcsym = NULL;
1499 } else { 1499 } else {
1500 dcs->d_rdcsym = sym; 1500 dcs->d_rdcsym = sym;
1501 sym = pushdown(sym); 1501 sym = pushdown(sym);
1502 } 1502 }
1503 1503
1504 switch (dcs->d_ctx) { 1504 switch (dcs->d_ctx) {
1505 case MOS: 1505 case MOS:
1506 case MOU: 1506 case MOU:
1507 /* Set parent */ 1507 /* Set parent */
1508 sym->s_styp = dcs->d_tagtyp->t_str; 1508 sym->s_styp = dcs->d_tagtyp->t_str;
1509 sym->s_def = DEF; 1509 sym->s_def = DEF;
1510 sym->s_value.v_tspec = INT; 1510 sym->s_value.v_tspec = INT;
1511 sc = dcs->d_ctx; 1511 sc = dcs->d_ctx;
1512 break; 1512 break;
1513 case EXTERN: 1513 case EXTERN:
1514 /* 1514 /*
1515 * static and external symbols without "extern" are 1515 * static and external symbols without "extern" are
1516 * considered to be tentative defined, external 1516 * considered to be tentative defined, external
1517 * symbols with "extern" are declared, and typedef names 1517 * symbols with "extern" are declared, and typedef names
1518 * are defined. Tentative defined and declared symbols 1518 * are defined. Tentative defined and declared symbols
1519 * may become defined if an initializer is present or 1519 * may become defined if an initializer is present or
1520 * this is a function definition. 1520 * this is a function definition.
1521 */ 1521 */
1522 if ((sc = dcs->d_scl) == NOSCL) { 1522 if ((sc = dcs->d_scl) == NOSCL) {
1523 sc = EXTERN; 1523 sc = EXTERN;
1524 sym->s_def = TDEF; 1524 sym->s_def = TDEF;
1525 } else if (sc == STATIC) { 1525 } else if (sc == STATIC) {
1526 sym->s_def = TDEF; 1526 sym->s_def = TDEF;
1527 } else if (sc == TYPEDEF) { 1527 } else if (sc == TYPEDEF) {
1528 sym->s_def = DEF; 1528 sym->s_def = DEF;
1529 } else { 1529 } else {
1530 lint_assert(sc == EXTERN); 1530 lint_assert(sc == EXTERN);
1531 sym->s_def = DECL; 1531 sym->s_def = DECL;
1532 } 1532 }
1533 break; 1533 break;
1534 case PARG: 1534 case PARG:
1535 sym->s_arg = 1; 1535 sym->s_arg = 1;
1536 /* FALLTHROUGH */ 1536 /* FALLTHROUGH */
1537 case ARG: 1537 case ARG:
1538 if ((sc = dcs->d_scl) == NOSCL) { 1538 if ((sc = dcs->d_scl) == NOSCL) {
1539 sc = AUTO; 1539 sc = AUTO;
1540 } else { 1540 } else {
1541 lint_assert(sc == REG); 1541 lint_assert(sc == REG);
1542 sym->s_reg = 1; 1542 sym->s_reg = 1;
1543 sc = AUTO; 1543 sc = AUTO;
1544 } 1544 }
1545 sym->s_def = DEF; 1545 sym->s_def = DEF;
1546 break; 1546 break;
1547 case AUTO: 1547 case AUTO:
1548 if ((sc = dcs->d_scl) == NOSCL) { 1548 if ((sc = dcs->d_scl) == NOSCL) {
1549 /* 1549 /*
1550 * XXX somewhat ugly because we dont know whether 1550 * XXX somewhat ugly because we dont know whether
1551 * this is AUTO or EXTERN (functions). If we are 1551 * this is AUTO or EXTERN (functions). If we are
1552 * wrong it must be corrected in decl1loc(), where 1552 * wrong it must be corrected in decl1loc(), where
1553 * we have the necessary type information. 1553 * we have the necessary type information.
1554 */ 1554 */
1555 sc = AUTO; 1555 sc = AUTO;
1556 sym->s_def = DEF; 1556 sym->s_def = DEF;
1557 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) { 1557 } else if (sc == AUTO || sc == STATIC || sc == TYPEDEF) {
1558 sym->s_def = DEF; 1558 sym->s_def = DEF;
1559 } else if (sc == REG) { 1559 } else if (sc == REG) {
1560 sym->s_reg = 1; 1560 sym->s_reg = 1;
1561 sc = AUTO; 1561 sc = AUTO;
1562 sym->s_def = DEF; 1562 sym->s_def = DEF;
1563 } else { 1563 } else {
1564 lint_assert(sc == EXTERN); 1564 lint_assert(sc == EXTERN);
1565 sym->s_def = DECL; 1565 sym->s_def = DECL;
1566 } 1566 }
1567 break; 1567 break;
1568 default: 1568 default:
1569 lint_assert(/*CONSTCOND*/0); 1569 lint_assert(/*CONSTCOND*/0);
1570 } 1570 }
1571 sym->s_scl = sc; 1571 sym->s_scl = sc;
1572 1572
1573 sym->s_type = dcs->d_type; 1573 sym->s_type = dcs->d_type;
1574 1574
1575 dcs->d_fpsyms = NULL; 1575 dcs->d_fpsyms = NULL;
1576 1576
1577 return sym; 1577 return sym;
1578} 1578}
1579 1579
1580/* 1580/*
1581 * Process a name in the list of formal params in an old style function 1581 * Process a name in the list of formal params in an old style function
1582 * definition. 1582 * definition.
1583 */ 1583 */
1584sym_t * 1584sym_t *
1585old_style_function_name(sym_t *sym) 1585old_style_function_name(sym_t *sym)
1586{ 1586{
1587 1587
1588 if (sym->s_scl != NOSCL) { 1588 if (sym->s_scl != NOSCL) {
1589 if (blklev == sym->s_blklev) { 1589 if (blklev == sym->s_blklev) {
1590 /* redeclaration of formal parameter %s */ 1590 /* redeclaration of formal parameter %s */
1591 error(21, sym->s_name); 1591 error(21, sym->s_name);
1592 lint_assert(sym->s_defarg); 1592 lint_assert(sym->s_defarg);
1593 } 1593 }
1594 sym = pushdown(sym); 1594 sym = pushdown(sym);
1595 } 1595 }
1596 sym->s_type = gettyp(INT); 1596 sym->s_type = gettyp(INT);
1597 sym->s_scl = AUTO; 1597 sym->s_scl = AUTO;
1598 sym->s_def = DEF; 1598 sym->s_def = DEF;
1599 sym->s_defarg = sym->s_arg = 1; 1599 sym->s_defarg = sym->s_arg = 1;
1600 return sym; 1600 return sym;
1601} 1601}
1602 1602
1603/* 1603/*
1604 * Create the type of a tag. 1604 * Create the type of a tag.
1605 * 1605 *
1606 * tag points to the symbol table entry of the tag 1606 * tag points to the symbol table entry of the tag
1607 * kind is the kind of the tag (STRUCT/UNION/ENUM) 1607 * kind is the kind of the tag (STRUCT/UNION/ENUM)
1608 * decl is 1 if the type of the tag will be completed in this declaration 1608 * decl is 1 if the type of the tag will be completed in this declaration
1609 * (the following token is T_LBRACE) 1609 * (the following token is T_LBRACE)
1610 * semi is 1 if the following token is T_SEMI 1610 * semi is 1 if the following token is T_SEMI
1611 */ 1611 */
1612type_t * 1612type_t *
1613mktag(sym_t *tag, tspec_t kind, int decl, int semi) 1613mktag(sym_t *tag, tspec_t kind, int decl, int semi)
1614{ 1614{
1615 scl_t scl = NOSCL; 1615 scl_t scl = NOSCL;
1616 type_t *tp; 1616 type_t *tp;
1617 1617
1618 if (kind == STRUCT) { 1618 if (kind == STRUCT) {
1619 scl = STRTAG; 1619 scl = STRTAG;
1620 } else if (kind == UNION) { 1620 } else if (kind == UNION) {
1621 scl = UNIONTAG; 1621 scl = UNIONTAG;
1622 } else { 1622 } else {
1623 lint_assert(kind == ENUM); 1623 lint_assert(kind == ENUM);
1624 scl = ENUMTAG; 1624 scl = ENUMTAG;
1625 } 1625 }
1626 1626
1627 if (tag != NULL) { 1627 if (tag != NULL) {
1628 if (tag->s_scl != NOSCL) { 1628 if (tag->s_scl != NOSCL) {
1629 tag = newtag(tag, scl, decl, semi); 1629 tag = newtag(tag, scl, decl, semi);
1630 } else { 1630 } else {
1631 /* a new tag, no empty declaration */ 1631 /* a new tag, no empty declaration */
1632 dcs->d_next->d_nedecl = 1; 1632 dcs->d_next->d_nedecl = 1;
1633 if (scl == ENUMTAG && !decl) { 1633 if (scl == ENUMTAG && !decl) {
1634 if (!tflag && (sflag || pflag)) 1634 if (!tflag && (sflag || pflag))
1635 /* forward reference to enum type */ 1635 /* forward reference to enum type */
1636 warning(42); 1636 warning(42);
1637 } 1637 }
1638 } 1638 }
1639 if (tag->s_scl == NOSCL) { 1639 if (tag->s_scl == NOSCL) {
1640 tag->s_scl = scl; 1640 tag->s_scl = scl;
1641 tag->s_type = tp = getblk(sizeof (type_t)); 1641 tag->s_type = tp = getblk(sizeof (type_t));
1642 tp->t_ispacked = dcs->d_ispacked; 1642 tp->t_ispacked = dcs->d_ispacked;
1643 } else { 1643 } else {
1644 tp = tag->s_type; 1644 tp = tag->s_type;
1645 } 1645 }
1646 } else { 1646 } else {
1647 tag = getblk(sizeof (sym_t)); 1647 tag = getblk(sizeof (sym_t));
1648 tag->s_name = unnamed; 1648 tag->s_name = unnamed;
1649 UNIQUE_CURR_POS(tag->s_def_pos); 1649 UNIQUE_CURR_POS(tag->s_def_pos);
1650 tag->s_kind = FTAG; 1650 tag->s_kind = FTAG;
1651 tag->s_scl = scl; 1651 tag->s_scl = scl;
1652 tag->s_blklev = -1; 1652 tag->s_blklev = -1;
1653 tag->s_type = tp = getblk(sizeof (type_t)); 1653 tag->s_type = tp = getblk(sizeof (type_t));
1654 tp->t_ispacked = dcs->d_ispacked; 1654 tp->t_ispacked = dcs->d_ispacked;
1655 dcs->d_next->d_nedecl = 1; 1655 dcs->d_next->d_nedecl = 1;
1656 } 1656 }
1657 1657
1658 if (tp->t_tspec == NOTSPEC) { 1658 if (tp->t_tspec == NOTSPEC) {
1659 tp->t_tspec = kind; 1659 tp->t_tspec = kind;
1660 if (kind != ENUM) { 1660 if (kind != ENUM) {
1661 tp->t_str = getblk(sizeof (str_t)); 1661 tp->t_str = getblk(sizeof (str_t));
1662 tp->t_str->align = CHAR_BIT; 1662 tp->t_str->align = CHAR_BIT;
1663 tp->t_str->stag = tag; 1663 tp->t_str->stag = tag;
1664 } else { 1664 } else {
1665 tp->t_isenum = 1; 1665 tp->t_isenum = 1;
1666 tp->t_enum = getblk(sizeof(*tp->t_enum)); 1666 tp->t_enum = getblk(sizeof(*tp->t_enum));
1667 tp->t_enum->etag = tag; 1667 tp->t_enum->etag = tag;
1668 } 1668 }
1669 setcomplete(tp, 0); 1669 setcomplete(tp, 0);
1670 } 1670 }
1671 return tp; 1671 return tp;
1672} 1672}
1673 1673
1674/* 1674/*
1675 * Checks all possible cases of tag redeclarations. 1675 * Checks all possible cases of tag redeclarations.
1676 * decl is 1 if T_LBRACE follows 1676 * decl is 1 if T_LBRACE follows
1677 * semi is 1 if T_SEMI follows 1677 * semi is 1 if T_SEMI follows
1678 */ 1678 */
1679static sym_t * 1679static sym_t *
1680newtag(sym_t *tag, scl_t scl, int decl, int semi) 1680newtag(sym_t *tag, scl_t scl, int decl, int semi)
1681{ 1681{
1682 1682
1683 if (tag->s_blklev < blklev) { 1683 if (tag->s_blklev < blklev) {
1684 if (semi) { 1684 if (semi) {
1685 /* "struct a;" */ 1685 /* "struct a;" */
1686 if (!tflag) { 1686 if (!tflag) {
1687 if (!sflag) 1687 if (!sflag)
1688 /* decl. introduces new type ... */ 1688 /* decl. introduces new type ... */
1689 warning(44, storage_class_name(scl), 1689 warning(44, storage_class_name(scl),
1690 tag->s_name); 1690 tag->s_name);
1691 tag = pushdown(tag); 1691 tag = pushdown(tag);
1692 } else if (tag->s_scl != scl) { 1692 } else if (tag->s_scl != scl) {
1693 /* base type is really '%s %s' */ 1693 /* base type is really '%s %s' */
1694 warning(45, storage_class_name(tag->s_scl), 1694 warning(45, storage_class_name(tag->s_scl),
1695 tag->s_name); 1695 tag->s_name);
1696 } 1696 }
1697 dcs->d_next->d_nedecl = 1; 1697 dcs->d_next->d_nedecl = 1;
1698 } else if (decl) { 1698 } else if (decl) {
1699 /* "struct a { ... } " */ 1699 /* "struct a { ... } " */
1700 if (hflag) 1700 if (hflag)
1701 /* redefinition hides earlier one: %s */ 1701 /* redefinition hides earlier one: %s */
1702 warning(43, tag->s_name); 1702 warning(43, tag->s_name);
1703 tag = pushdown(tag); 1703 tag = pushdown(tag);
1704 dcs->d_next->d_nedecl = 1; 1704 dcs->d_next->d_nedecl = 1;
1705 } else if (tag->s_scl != scl) { 1705 } else if (tag->s_scl != scl) {
1706 /* base type is really '%s %s' */ 1706 /* base type is really '%s %s' */
1707 warning(45, storage_class_name(tag->s_scl), 1707 warning(45, storage_class_name(tag->s_scl),
1708 tag->s_name); 1708 tag->s_name);
1709 /* declaration introduces new type in ANSI C: %s %s */ 1709 /* declaration introduces new type in ANSI C: %s %s */
1710 if (!sflag) { 1710 if (!sflag) {
1711 /* decl. introduces new type in ANSI C: %s %s */ 1711 /* decl. introduces new type in ANSI C: %s %s */
1712 warning(44, storage_class_name(scl), 1712 warning(44, storage_class_name(scl),
1713 tag->s_name); 1713 tag->s_name);
1714 } 1714 }
1715 tag = pushdown(tag); 1715 tag = pushdown(tag);
1716 dcs->d_next->d_nedecl = 1; 1716 dcs->d_next->d_nedecl = 1;
1717 } 1717 }
1718 } else { 1718 } else {
1719 if (tag->s_scl != scl) { 1719 if (tag->s_scl != scl) {
1720 /* (%s) tag redeclared */ 1720 /* (%s) tag redeclared */
1721 error(46, storage_class_name(tag->s_scl)); 1721 error(46, storage_class_name(tag->s_scl));
1722 print_previous_declaration(-1, tag); 1722 print_previous_declaration(-1, tag);
1723 tag = pushdown(tag); 1723 tag = pushdown(tag);
1724 dcs->d_next->d_nedecl = 1; 1724 dcs->d_next->d_nedecl = 1;
1725 } else if (decl && !incompl(tag->s_type)) { 1725 } else if (decl && !incompl(tag->s_type)) {
1726 /* (%s) tag redeclared */ 1726 /* (%s) tag redeclared */
1727 error(46, storage_class_name(tag->s_scl)); 1727 error(46, storage_class_name(tag->s_scl));
1728 print_previous_declaration(-1, tag); 1728 print_previous_declaration(-1, tag);
1729 tag = pushdown(tag); 1729 tag = pushdown(tag);
1730 dcs->d_next->d_nedecl = 1; 1730 dcs->d_next->d_nedecl = 1;
1731 } else if (semi || decl) { 1731 } else if (semi || decl) {
1732 dcs->d_next->d_nedecl = 1; 1732 dcs->d_next->d_nedecl = 1;
1733 } 1733 }
1734 } 1734 }
1735 return tag; 1735 return tag;
1736} 1736}
1737 1737
1738const char * 1738const char *
1739storage_class_name(scl_t sc) 1739storage_class_name(scl_t sc)
1740{ 1740{
1741 const char *s; 1741 const char *s;
1742 1742
1743 switch (sc) { 1743 switch (sc) {
1744 case EXTERN: s = "extern"; break; 1744 case EXTERN: s = "extern"; break;
1745 case STATIC: s = "static"; break; 1745 case STATIC: s = "static"; break;
1746 case AUTO: s = "auto"; break; 1746 case AUTO: s = "auto"; break;
1747 case REG: s = "register"; break; 1747 case REG: s = "register"; break;
1748 case TYPEDEF: s = "typedef"; break; 1748 case TYPEDEF: s = "typedef"; break;
1749 case STRTAG: s = "struct"; break; 1749 case STRTAG: s = "struct"; break;
1750 case UNIONTAG: s = "union"; break; 1750 case UNIONTAG: s = "union"; break;
1751 case ENUMTAG: s = "enum"; break; 1751 case ENUMTAG: s = "enum"; break;
1752 default: lint_assert(/*CONSTCOND*/0); 1752 default: lint_assert(/*CONSTCOND*/0);
1753 } 1753 }
1754 return s; 1754 return s;
1755} 1755}
1756 1756
1757/* 1757/*
1758 * tp points to the type of the tag, fmem to the list of members. 1758 * tp points to the type of the tag, fmem to the list of members.
1759 */ 1759 */
1760type_t * 1760type_t *
1761complete_tag_struct_or_union(type_t *tp, sym_t *fmem) 1761complete_tag_struct_or_union(type_t *tp, sym_t *fmem)
1762{ 1762{
1763 tspec_t t; 1763 tspec_t t;
1764 str_t *sp; 1764 str_t *sp;
1765 int n; 1765 int n;
1766 sym_t *mem; 1766 sym_t *mem;
1767 1767
1768 setcomplete(tp, 1); 1768 setcomplete(tp, 1);
1769 1769
1770 t = tp->t_tspec; 1770 t = tp->t_tspec;
1771 align(dcs->d_stralign, 0); 1771 align(dcs->d_stralign, 0);
1772 sp = tp->t_str; 1772 sp = tp->t_str;
1773 sp->align = dcs->d_stralign; 1773 sp->align = dcs->d_stralign;
1774 sp->memb = fmem; 1774 sp->memb = fmem;
1775 if (tp->t_ispacked) 1775 if (tp->t_ispacked)
1776 setpackedsize(tp); 1776 setpackedsize(tp);
1777 else 1777 else
1778 sp->size = dcs->d_offset; 1778 sp->size = dcs->d_offset;
1779 1779
1780 if (sp->size == 0) { 1780 if (sp->size == 0) {
1781 /* zero sized %s is a C9X feature */ 1781 /* zero sized %s is a C9X feature */
1782 c99ism(47, ttab[t].tt_name); 1782 c99ism(47, ttab[t].tt_name);
1783 } 1783 }
1784 1784
1785 n = 0; 1785 n = 0;
1786 for (mem = fmem; mem != NULL; mem = mem->s_next) { 1786 for (mem = fmem; mem != NULL; mem = mem->s_next) {
1787 /* bind anonymous members to the structure */ 1787 /* bind anonymous members to the structure */
1788 if (mem->s_styp == NULL) { 1788 if (mem->s_styp == NULL) {
1789 mem->s_styp = sp; 1789 mem->s_styp = sp;
1790 if (mem->s_type->t_isfield) { 1790 if (mem->s_type->t_bitfield) {
1791 sp->size += bitfieldsize(&mem); 1791 sp->size += bitfieldsize(&mem);
1792 if (mem == NULL) 1792 if (mem == NULL)
1793 break; 1793 break;
1794 } 1794 }
1795 sp->size += tsize(mem->s_type); 1795 sp->size += tsize(mem->s_type);
1796 } 1796 }
1797 if (mem->s_name != unnamed) 1797 if (mem->s_name != unnamed)
1798 n++; 1798 n++;
1799 } 1799 }
1800 1800
1801 if (n == 0 && sp->size != 0) { 1801 if (n == 0 && sp->size != 0) {
1802 /* %s has no named members */ 1802 /* %s has no named members */
1803 warning(65, t == STRUCT ? "structure" : "union"); 1803 warning(65, t == STRUCT ? "structure" : "union");
1804 } 1804 }
1805 return tp; 1805 return tp;
1806} 1806}
1807 1807
1808type_t * 1808type_t *
1809complete_tag_enum(type_t *tp, sym_t *fmem) 1809complete_tag_enum(type_t *tp, sym_t *fmem)
1810{ 1810{
1811 1811
1812 setcomplete(tp, 1); 1812 setcomplete(tp, 1);
1813 tp->t_enum->elem = fmem; 1813 tp->t_enum->elem = fmem;
1814 return tp; 1814 return tp;
1815} 1815}
1816 1816
1817/* 1817/*
1818 * Processes the name of an enumerator in an enum declaration. 1818 * Processes the name of an enumerator in an enum declaration.
1819 * 1819 *
1820 * sym points to the enumerator 1820 * sym points to the enumerator
1821 * val is the value of the enumerator 1821 * val is the value of the enumerator
1822 * impl is 1 if the value of the enumerator was not explicitly specified. 1822 * impl is 1 if the value of the enumerator was not explicitly specified.
1823 */ 1823 */
1824sym_t * 1824sym_t *
1825ename(sym_t *sym, int val, int impl) 1825ename(sym_t *sym, int val, int impl)
1826{ 1826{
1827 1827
1828 if (sym->s_scl) { 1828 if (sym->s_scl) {
1829 if (sym->s_blklev == blklev) { 1829 if (sym->s_blklev == blklev) {
1830 /* no hflag, because this is illegal!!! */ 1830 /* no hflag, because this is illegal!!! */
1831 if (sym->s_arg) { 1831 if (sym->s_arg) {
1832 /* enumeration constant hides parameter: %s */ 1832 /* enumeration constant hides parameter: %s */
1833 warning(57, sym->s_name); 1833 warning(57, sym->s_name);
1834 } else { 1834 } else {
1835 /* redeclaration of %s */ 1835 /* redeclaration of %s */
1836 error(27, sym->s_name); 1836 error(27, sym->s_name);
1837 /* 1837 /*
1838 * inside blocks it should not be too 1838 * inside blocks it should not be too
1839 * complicated to find the position of the 1839 * complicated to find the position of the
1840 * previous declaration 1840 * previous declaration
1841 */ 1841 */
1842 if (blklev == 0) 1842 if (blklev == 0)
1843 print_previous_declaration(-1, sym); 1843 print_previous_declaration(-1, sym);
1844 } 1844 }
1845 } else { 1845 } else {
1846 if (hflag) 1846 if (hflag)
1847 /* redefinition hides earlier one: %s */ 1847 /* redefinition hides earlier one: %s */
1848 warning(43, sym->s_name); 1848 warning(43, sym->s_name);
1849 } 1849 }
1850 sym = pushdown(sym); 1850 sym = pushdown(sym);
1851 } 1851 }
1852 sym->s_scl = ENUMCON; 1852 sym->s_scl = ENUMCON;
1853 sym->s_type = dcs->d_tagtyp; 1853 sym->s_type = dcs->d_tagtyp;
1854 sym->s_value.v_tspec = INT; 1854 sym->s_value.v_tspec = INT;
1855 sym->s_value.v_quad = val; 1855 sym->s_value.v_quad = val;
1856 if (impl && val - 1 == TARG_INT_MAX) { 1856 if (impl && val - 1 == TARG_INT_MAX) {
1857 /* overflow in enumeration values: %s */ 1857 /* overflow in enumeration values: %s */
1858 warning(48, sym->s_name); 1858 warning(48, sym->s_name);
1859 } 1859 }
1860 enumval = val + 1; 1860 enumval = val + 1;
1861 return sym; 1861 return sym;
1862} 1862}
1863 1863
1864/* 1864/*
1865 * Process a single external declarator. 1865 * Process a single external declarator.
1866 */ 1866 */
1867void 1867void
1868decl1ext(sym_t *dsym, int initflg) 1868decl1ext(sym_t *dsym, int initflg)
1869{ 1869{
1870 int dowarn, rval, redec; 1870 int dowarn, rval, redec;
1871 sym_t *rdsym; 1871 sym_t *rdsym;
1872 1872
1873 check_function_definition(dsym, 1); 1873 check_function_definition(dsym, 1);
1874 1874
1875 check_type(dsym); 1875 check_type(dsym);
1876 1876
1877 if (initflg && !(initerr = check_init(dsym))) 1877 if (initflg && !(initerr = check_init(dsym)))
1878 dsym->s_def = DEF; 1878 dsym->s_def = DEF;
1879 1879
1880 /* 1880 /*
1881 * Declarations of functions are marked as "tentative" in 1881 * Declarations of functions are marked as "tentative" in
1882 * declarator_name(). This is wrong because there are no 1882 * declarator_name(). This is wrong because there are no
1883 * tentative function definitions. 1883 * tentative function definitions.
1884 */ 1884 */
1885 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF) 1885 if (dsym->s_type->t_tspec == FUNC && dsym->s_def == TDEF)
1886 dsym->s_def = DECL; 1886 dsym->s_def = DECL;
1887 1887
1888 if (dcs->d_inline) { 1888 if (dcs->d_inline) {
1889 if (dsym->s_type->t_tspec == FUNC) { 1889 if (dsym->s_type->t_tspec == FUNC) {
1890 dsym->s_inline = 1; 1890 dsym->s_inline = 1;
1891 } else { 1891 } else {
1892 /* variable declared inline: %s */ 1892 /* variable declared inline: %s */
1893 warning(268, dsym->s_name); 1893 warning(268, dsym->s_name);
1894 } 1894 }
1895 } 1895 }
1896 1896
1897 /* Write the declaration into the output file */ 1897 /* Write the declaration into the output file */
1898 if (plibflg && llibflg && 1898 if (plibflg && llibflg &&
1899 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) { 1899 dsym->s_type->t_tspec == FUNC && dsym->s_type->t_proto) {
1900 /* 1900 /*
1901 * With both LINTLIBRARY and PROTOLIB the prototyp is 1901 * With both LINTLIBRARY and PROTOLIB the prototyp is
1902 * written as a function definition to the output file. 1902 * written as a function definition to the output file.
1903 */ 1903 */
1904 rval = dsym->s_type->t_subt->t_tspec != VOID; 1904 rval = dsym->s_type->t_subt->t_tspec != VOID;
1905 outfdef(dsym, &dsym->s_def_pos, rval, 0, NULL); 1905 outfdef(dsym, &dsym->s_def_pos, rval, 0, NULL);
1906 } else { 1906 } else {
1907 outsym(dsym, dsym->s_scl, dsym->s_def); 1907 outsym(dsym, dsym->s_scl, dsym->s_def);
1908 } 1908 }
1909 1909
1910 if ((rdsym = dcs->d_rdcsym) != NULL) { 1910 if ((rdsym = dcs->d_rdcsym) != NULL) {
1911 1911
1912 /* 1912 /*
1913 * If the old symbol stems from an old style function 1913 * If the old symbol stems from an old style function
1914 * definition, we have remembered the params in rdsmy->s_args 1914 * definition, we have remembered the params in rdsmy->s_args
1915 * and compare them with the params of the prototype. 1915 * and compare them with the params of the prototype.
1916 */ 1916 */
1917 if (rdsym->s_osdef && dsym->s_type->t_proto) { 1917 if (rdsym->s_osdef && dsym->s_type->t_proto) {
1918 redec = check_old_style_definition(rdsym, dsym); 1918 redec = check_old_style_definition(rdsym, dsym);
1919 } else { 1919 } else {
1920 redec = 0; 1920 redec = 0;
1921 } 1921 }
1922 1922
1923 if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) { 1923 if (!redec && !check_redeclaration(dsym, (dowarn = 0, &dowarn))) {
1924 1924
1925 if (dowarn) { 1925 if (dowarn) {
1926 if (sflag) 1926 if (sflag)
1927 /* redeclaration of %s */ 1927 /* redeclaration of %s */
1928 error(27, dsym->s_name); 1928 error(27, dsym->s_name);
1929 else 1929 else
1930 /* redeclaration of %s */ 1930 /* redeclaration of %s */
1931 warning(27, dsym->s_name); 1931 warning(27, dsym->s_name);
1932 print_previous_declaration(-1, rdsym); 1932 print_previous_declaration(-1, rdsym);
1933 } 1933 }
1934 1934
1935 /* 1935 /*
1936 * Take over the remembered params if the new symbol 1936 * Take over the remembered params if the new symbol
1937 * is not a prototype. 1937 * is not a prototype.
1938 */ 1938 */
1939 if (rdsym->s_osdef && !dsym->s_type->t_proto) { 1939 if (rdsym->s_osdef && !dsym->s_type->t_proto) {
1940 dsym->s_osdef = rdsym->s_osdef; 1940 dsym->s_osdef = rdsym->s_osdef;
1941 dsym->s_args = rdsym->s_args; 1941 dsym->s_args = rdsym->s_args;
1942 dsym->s_def_pos = rdsym->s_def_pos; 1942 dsym->s_def_pos = rdsym->s_def_pos;
1943 } 1943 }
1944 1944
1945 /* 1945 /*
1946 * Remember the position of the declaration if the 1946 * Remember the position of the declaration if the
1947 * old symbol was a prototype and the new is not. 1947 * old symbol was a prototype and the new is not.
1948 * Also remember the position if the old symbol 1948 * Also remember the position if the old symbol
1949 * was defined and the new is not. 1949 * was defined and the new is not.
1950 */ 1950 */
1951 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) { 1951 if (rdsym->s_type->t_proto && !dsym->s_type->t_proto) {
1952 dsym->s_def_pos = rdsym->s_def_pos; 1952 dsym->s_def_pos = rdsym->s_def_pos;
1953 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) { 1953 } else if (rdsym->s_def == DEF && dsym->s_def != DEF) {
1954 dsym->s_def_pos = rdsym->s_def_pos; 1954 dsym->s_def_pos = rdsym->s_def_pos;
1955 } 1955 }
1956 1956
1957 /* 1957 /*
1958 * Copy usage information of the name into the new 1958 * Copy usage information of the name into the new
1959 * symbol. 1959 * symbol.
1960 */ 1960 */
1961 copy_usage_info(dsym, rdsym); 1961 copy_usage_info(dsym, rdsym);
1962 1962
1963 /* Once a name is defined, it remains defined. */ 1963 /* Once a name is defined, it remains defined. */
1964 if (rdsym->s_def == DEF) 1964 if (rdsym->s_def == DEF)
1965 dsym->s_def = DEF; 1965 dsym->s_def = DEF;
1966 1966
1967 /* once a function is inline, it remains inline */ 1967 /* once a function is inline, it remains inline */
1968 if (rdsym->s_inline) 1968 if (rdsym->s_inline)
1969 dsym->s_inline = 1; 1969 dsym->s_inline = 1;
1970 1970
1971 complete_type(dsym, rdsym); 1971 complete_type(dsym, rdsym);
1972 1972
1973 } 1973 }
1974 1974
1975 rmsym(rdsym); 1975 rmsym(rdsym);
1976 } 1976 }
1977 1977
1978 if (dsym->s_scl == TYPEDEF) { 1978 if (dsym->s_scl == TYPEDEF) {
1979 dsym->s_type = duptyp(dsym->s_type); 1979 dsym->s_type = duptyp(dsym->s_type);
1980 dsym->s_type->t_typedef = 1; 1980 dsym->s_type->t_typedef = 1;
1981 settdsym(dsym->s_type, dsym); 1981 settdsym(dsym->s_type, dsym);
1982 } 1982 }
1983 1983
1984} 1984}
1985 1985
1986/* 1986/*
1987 * Copies information about usage into a new symbol table entry of 1987 * Copies information about usage into a new symbol table entry of
1988 * the same symbol. 1988 * the same symbol.
1989 */ 1989 */
1990void 1990void
1991copy_usage_info(sym_t *sym, sym_t *rdsym) 1991copy_usage_info(sym_t *sym, sym_t *rdsym)
1992{ 1992{
1993 1993
1994 sym->s_set_pos = rdsym->s_set_pos; 1994 sym->s_set_pos = rdsym->s_set_pos;
1995 sym->s_use_pos = rdsym->s_use_pos; 1995 sym->s_use_pos = rdsym->s_use_pos;
1996 sym->s_set = rdsym->s_set; 1996 sym->s_set = rdsym->s_set;
1997 sym->s_used = rdsym->s_used; 1997 sym->s_used = rdsym->s_used;
1998} 1998}
1999 1999
2000/* 2000/*
2001 * Prints an error and returns 1 if a symbol is redeclared/redefined. 2001 * Prints an error and returns 1 if a symbol is redeclared/redefined.
2002 * Otherwise returns 0 and, in some cases of minor problems, prints 2002 * Otherwise returns 0 and, in some cases of minor problems, prints
2003 * a warning. 2003 * a warning.
2004 */ 2004 */
2005int 2005int
2006check_redeclaration(sym_t *dsym, int *dowarn) 2006check_redeclaration(sym_t *dsym, int *dowarn)
2007{ 2007{
2008 sym_t *rsym; 2008 sym_t *rsym;
2009 2009
2010 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) { 2010 if ((rsym = dcs->d_rdcsym)->s_scl == ENUMCON) {
2011 /* redeclaration of %s */ 2011 /* redeclaration of %s */
2012 error(27, dsym->s_name); 2012 error(27, dsym->s_name);
2013 print_previous_declaration(-1, rsym); 2013 print_previous_declaration(-1, rsym);
2014 return 1; 2014 return 1;
2015 } 2015 }
2016 if (rsym->s_scl == TYPEDEF) { 2016 if (rsym->s_scl == TYPEDEF) {
2017 /* typedef redeclared: %s */ 2017 /* typedef redeclared: %s */
2018 error(89, dsym->s_name); 2018 error(89, dsym->s_name);
2019 print_previous_declaration(-1, rsym); 2019 print_previous_declaration(-1, rsym);
2020 return 1; 2020 return 1;
2021 } 2021 }
2022 if (dsym->s_scl == TYPEDEF) { 2022 if (dsym->s_scl == TYPEDEF) {
2023 /* redeclaration of %s */ 2023 /* redeclaration of %s */
2024 error(27, dsym->s_name); 2024 error(27, dsym->s_name);
2025 print_previous_declaration(-1, rsym); 2025 print_previous_declaration(-1, rsym);
2026 return 1; 2026 return 1;
2027 } 2027 }
2028 if (rsym->s_def == DEF && dsym->s_def == DEF) { 2028 if (rsym->s_def == DEF && dsym->s_def == DEF) {
2029 /* redefinition of %s */ 2029 /* redefinition of %s */
2030 error(28, dsym->s_name); 2030 error(28, dsym->s_name);
2031 print_previous_declaration(-1, rsym); 2031 print_previous_declaration(-1, rsym);
2032 return(1); 2032 return(1);
2033 } 2033 }
2034 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) { 2034 if (!eqtype(rsym->s_type, dsym->s_type, 0, 0, dowarn)) {
2035 /* redeclaration of %s */ 2035 /* redeclaration of %s */
2036 error(27, dsym->s_name); 2036 error(27, dsym->s_name);
2037 print_previous_declaration(-1, rsym); 2037 print_previous_declaration(-1, rsym);
2038 return(1); 2038 return(1);
2039 } 2039 }
2040 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN) 2040 if (rsym->s_scl == EXTERN && dsym->s_scl == EXTERN)
2041 return(0); 2041 return(0);
2042 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC) 2042 if (rsym->s_scl == STATIC && dsym->s_scl == STATIC)
2043 return(0); 2043 return(0);
2044 if (rsym->s_scl == STATIC && dsym->s_def == DECL) 2044 if (rsym->s_scl == STATIC && dsym->s_def == DECL)
2045 return(0); 2045 return(0);
2046 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) { 2046 if (rsym->s_scl == EXTERN && rsym->s_def == DEF) {
2047 /* 2047 /*
2048 * All cases except "int a = 1; static int a;" are caught 2048 * All cases except "int a = 1; static int a;" are caught
2049 * above with or without a warning 2049 * above with or without a warning
2050 */ 2050 */
2051 /* redeclaration of %s */ 2051 /* redeclaration of %s */
2052 error(27, dsym->s_name); 2052 error(27, dsym->s_name);
2053 print_previous_declaration(-1, rsym); 2053 print_previous_declaration(-1, rsym);
2054 return(1); 2054 return(1);
2055 } 2055 }
2056 if (rsym->s_scl == EXTERN) { 2056 if (rsym->s_scl == EXTERN) {
2057 /* previously declared extern, becomes static: %s */ 2057 /* previously declared extern, becomes static: %s */
2058 warning(29, dsym->s_name); 2058 warning(29, dsym->s_name);
2059 print_previous_declaration(-1, rsym); 2059 print_previous_declaration(-1, rsym);
2060 return(0); 2060 return(0);
2061 } 2061 }
2062 /* 2062 /*
2063 * Now it's one of: 2063 * Now it's one of:
2064 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;" 2064 * "static a; int a;", "static a; int a = 1;", "static a = 1; int a;"
2065 */ 2065 */
2066 /* redeclaration of %s; ANSI C requires "static" */ 2066 /* redeclaration of %s; ANSI C requires "static" */
2067 if (sflag) { 2067 if (sflag) {
2068 /* redeclaration of %s; ANSI C requires static */ 2068 /* redeclaration of %s; ANSI C requires static */
2069 warning(30, dsym->s_name); 2069 warning(30, dsym->s_name);
2070 print_previous_declaration(-1, rsym); 2070 print_previous_declaration(-1, rsym);
2071 } 2071 }
2072 dsym->s_scl = STATIC; 2072 dsym->s_scl = STATIC;
2073 return 0; 2073 return 0;
2074} 2074}
2075 2075
2076static int 2076static int
2077chkqual(type_t *tp1, type_t *tp2, int ignqual) 2077chkqual(type_t *tp1, type_t *tp2, int ignqual)
2078{ 2078{
2079 if (tp1->t_const != tp2->t_const && !ignqual && !tflag) 2079 if (tp1->t_const != tp2->t_const && !ignqual && !tflag)
2080 return 0; 2080 return 0;
2081 2081
2082 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag) 2082 if (tp1->t_volatile != tp2->t_volatile && !ignqual && !tflag)
2083 return 0; 2083 return 0;
2084 2084
2085 return 1; 2085 return 1;
2086} 2086}
2087 2087
2088int 2088int
2089eqptrtype(type_t *tp1, type_t *tp2, int ignqual) 2089eqptrtype(type_t *tp1, type_t *tp2, int ignqual)
2090{ 2090{
2091 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID) 2091 if (tp1->t_tspec != VOID && tp2->t_tspec != VOID)
2092 return 0; 2092 return 0;
2093 2093
2094 if (!chkqual(tp1, tp2, ignqual)) 2094 if (!chkqual(tp1, tp2, ignqual))
2095 return 0; 2095 return 0;
2096 2096
2097 return 1; 2097 return 1;
2098} 2098}
2099 2099
2100 2100
2101/* 2101/*
2102 * Checks if two types are compatible. Returns 0 if not, otherwise 1. 2102 * Checks if two types are compatible. Returns 0 if not, otherwise 1.
2103 * 2103 *
2104 * ignqual ignore qualifiers of type; used for function params 2104 * ignqual ignore qualifiers of type; used for function params
2105 * promot promote left type; used for comparison of params of 2105 * promot promote left type; used for comparison of params of
2106 * old style function definitions with params of prototypes. 2106 * old style function definitions with params of prototypes.
2107 * *dowarn set to 1 if an old style function declaration is not 2107 * *dowarn set to 1 if an old style function declaration is not
2108 * compatible with a prototype 2108 * compatible with a prototype
2109 */ 2109 */
2110int 2110int
2111eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn) 2111eqtype(type_t *tp1, type_t *tp2, int ignqual, int promot, int *dowarn)
2112{ 2112{
2113 tspec_t t; 2113 tspec_t t;
2114 2114
2115 while (tp1 != NULL && tp2 != NULL) { 2115 while (tp1 != NULL && tp2 != NULL) {
2116 2116
2117 t = tp1->t_tspec; 2117 t = tp1->t_tspec;
2118 if (promot) { 2118 if (promot) {
2119 if (t == FLOAT) { 2119 if (t == FLOAT) {
2120 t = DOUBLE; 2120 t = DOUBLE;
2121 } else if (t == CHAR || t == SCHAR) { 2121 } else if (t == CHAR || t == SCHAR) {
2122 t = INT; 2122 t = INT;
2123 } else if (t == UCHAR) { 2123 } else if (t == UCHAR) {
2124 t = tflag ? UINT : INT; 2124 t = tflag ? UINT : INT;
2125 } else if (t == SHORT) { 2125 } else if (t == SHORT) {
2126 t = INT; 2126 t = INT;
2127 } else if (t == USHORT) { 2127 } else if (t == USHORT) {
2128 /* CONSTCOND */ 2128 /* CONSTCOND */
2129 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT; 2129 t = TARG_INT_MAX < TARG_USHRT_MAX || tflag ? UINT : INT;
2130 } 2130 }
2131 } 2131 }
2132 2132
2133 if (t != tp2->t_tspec) 2133 if (t != tp2->t_tspec)
2134 return 0; 2134 return 0;
2135 2135
2136 if (!chkqual(tp1, tp2, ignqual)) 2136 if (!chkqual(tp1, tp2, ignqual))
2137 return 0; 2137 return 0;
2138 2138
2139 if (t == STRUCT || t == UNION) 2139 if (t == STRUCT || t == UNION)
2140 return tp1->t_str == tp2->t_str; 2140 return tp1->t_str == tp2->t_str;
2141 2141
2142 if (t == ARRAY && tp1->t_dim != tp2->t_dim) { 2142 if (t == ARRAY && tp1->t_dim != tp2->t_dim) {
2143 if (tp1->t_dim != 0 && tp2->t_dim != 0) 2143 if (tp1->t_dim != 0 && tp2->t_dim != 0)
2144 return 0; 2144 return 0;
2145 } 2145 }
2146 2146
2147 /* dont check prototypes for traditional */ 2147 /* dont check prototypes for traditional */
2148 if (t == FUNC && !tflag) { 2148 if (t == FUNC && !tflag) {
2149 if (tp1->t_proto && tp2->t_proto) { 2149 if (tp1->t_proto && tp2->t_proto) {
2150 if (!eqargs(tp1, tp2, dowarn)) 2150 if (!eqargs(tp1, tp2, dowarn))
2151 return 0; 2151 return 0;
2152 } else if (tp1->t_proto) { 2152 } else if (tp1->t_proto) {
2153 if (!mnoarg(tp1, dowarn)) 2153 if (!mnoarg(tp1, dowarn))
2154 return 0; 2154 return 0;
2155 } else if (tp2->t_proto) { 2155 } else if (tp2->t_proto) {
2156 if (!mnoarg(tp2, dowarn)) 2156 if (!mnoarg(tp2, dowarn))
2157 return 0; 2157 return 0;
2158 } 2158 }
2159 } 2159 }
2160 2160
2161 tp1 = tp1->t_subt; 2161 tp1 = tp1->t_subt;
2162 tp2 = tp2->t_subt; 2162 tp2 = tp2->t_subt;
2163 ignqual = promot = 0; 2163 ignqual = promot = 0;
2164 2164
2165 } 2165 }
2166 2166
2167 return tp1 == tp2; 2167 return tp1 == tp2;
2168} 2168}
2169 2169
2170/* 2170/*
2171 * Compares the parameter types of two prototypes. 2171 * Compares the parameter types of two prototypes.
2172 */ 2172 */
2173static int 2173static int
2174eqargs(type_t *tp1, type_t *tp2, int *dowarn) 2174eqargs(type_t *tp1, type_t *tp2, int *dowarn)
2175{ 2175{
2176 sym_t *a1, *a2; 2176 sym_t *a1, *a2;
2177 2177
2178 if (tp1->t_vararg != tp2->t_vararg) 2178 if (tp1->t_vararg != tp2->t_vararg)
2179 return 0; 2179 return 0;
2180 2180
2181 a1 = tp1->t_args; 2181 a1 = tp1->t_args;
2182 a2 = tp2->t_args; 2182 a2 = tp2->t_args;
2183 2183
2184 while (a1 != NULL && a2 != NULL) { 2184 while (a1 != NULL && a2 != NULL) {
2185 2185
2186 if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0) 2186 if (eqtype(a1->s_type, a2->s_type, 1, 0, dowarn) == 0)
2187 return 0; 2187 return 0;
2188 2188
2189 a1 = a1->s_next; 2189 a1 = a1->s_next;
2190 a2 = a2->s_next; 2190 a2 = a2->s_next;
2191 2191
2192 } 2192 }
2193 2193
2194 return a1 == a2; 2194 return a1 == a2;
2195} 2195}
2196 2196
2197/* 2197/*
2198 * mnoarg() (matches functions with no argument type information) 2198 * mnoarg() (matches functions with no argument type information)
2199 * returns 1 if all parameters of a prototype are compatible with 2199 * returns 1 if all parameters of a prototype are compatible with
2200 * an old style function declaration. 2200 * an old style function declaration.
2201 * This is the case if the following conditions are met: 2201 * This is the case if the following conditions are met:
2202 * 1. the prototype must have a fixed number of parameters 2202 * 1. the prototype must have a fixed number of parameters
2203 * 2. no parameter is of type float 2203 * 2. no parameter is of type float
2204 * 3. no parameter is converted to another type if integer promotion 2204 * 3. no parameter is converted to another type if integer promotion
2205 * is applied on it 2205 * is applied on it
2206 */ 2206 */
2207static int 2207static int
2208mnoarg(type_t *tp, int *dowarn) 2208mnoarg(type_t *tp, int *dowarn)
2209{ 2209{
2210 sym_t *arg; 2210 sym_t *arg;
2211 tspec_t t; 2211 tspec_t t;
2212 2212
2213 if (tp->t_vararg) { 2213 if (tp->t_vararg) {
2214 if (dowarn != NULL) 2214 if (dowarn != NULL)
2215 *dowarn = 1; 2215 *dowarn = 1;
2216 } 2216 }
2217 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) { 2217 for (arg = tp->t_args; arg != NULL; arg = arg->s_next) {
2218 if ((t = arg->s_type->t_tspec) == FLOAT || 2218 if ((t = arg->s_type->t_tspec) == FLOAT ||
2219 t == CHAR || t == SCHAR || t == UCHAR || 2219 t == CHAR || t == SCHAR || t == UCHAR ||
2220 t == SHORT || t == USHORT) { 2220 t == SHORT || t == USHORT) {
2221 if (dowarn != NULL) 2221 if (dowarn != NULL)
2222 *dowarn = 1; 2222 *dowarn = 1;
2223 } 2223 }
2224 } 2224 }
2225 return 1; 2225 return 1;
2226} 2226}
2227 2227
2228/* 2228/*
2229 * Compares a prototype declaration with the remembered arguments of 2229 * Compares a prototype declaration with the remembered arguments of
2230 * a previous old style function definition. 2230 * a previous old style function definition.
2231 */ 2231 */
2232static int 2232static int
2233check_old_style_definition(sym_t *rdsym, sym_t *dsym) 2233check_old_style_definition(sym_t *rdsym, sym_t *dsym)
2234{ 2234{
2235 sym_t *args, *pargs, *arg, *parg; 2235 sym_t *args, *pargs, *arg, *parg;
2236 int narg, nparg, n; 2236 int narg, nparg, n;
2237 int dowarn, msg; 2237 int dowarn, msg;
2238 2238
2239 args = rdsym->s_args; 2239 args = rdsym->s_args;
2240 pargs = dsym->s_type->t_args; 2240 pargs = dsym->s_type->t_args;
2241 2241
2242 msg = 0; 2242 msg = 0;
2243 2243
2244 narg = nparg = 0; 2244 narg = nparg = 0;
2245 for (arg = args; arg != NULL; arg = arg->s_next) 2245 for (arg = args; arg != NULL; arg = arg->s_next)
2246 narg++; 2246 narg++;
2247 for (parg = pargs; parg != NULL; parg = parg->s_next) 2247 for (parg = pargs; parg != NULL; parg = parg->s_next)
2248 nparg++; 2248 nparg++;
2249 if (narg != nparg) { 2249 if (narg != nparg) {
2250 /* prototype does not match old-style definition */ 2250 /* prototype does not match old-style definition */
2251 error(63); 2251 error(63);
2252 msg = 1; 2252 msg = 1;
2253 goto end; 2253 goto end;
2254 } 2254 }
2255 2255
2256 arg = args; 2256 arg = args;
2257 parg = pargs; 2257 parg = pargs;
2258 n = 1; 2258 n = 1;
2259 while (narg--) { 2259 while (narg--) {
2260 dowarn = 0; 2260 dowarn = 0;
2261 /* 2261 /*
2262 * If it does not match due to promotion and sflag is 2262 * If it does not match due to promotion and sflag is
2263 * not set we print only a warning. 2263 * not set we print only a warning.
2264 */ 2264 */
2265 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) { 2265 if (!eqtype(arg->s_type, parg->s_type, 1, 1, &dowarn) || dowarn) {
2266 /* prototype does not match old style defn., arg #%d */ 2266 /* prototype does not match old style defn., arg #%d */
2267 error(299, n); 2267 error(299, n);
2268 msg = 1; 2268 msg = 1;
2269 } 2269 }
2270 arg = arg->s_next; 2270 arg = arg->s_next;
2271 parg = parg->s_next; 2271 parg = parg->s_next;
2272 n++; 2272 n++;
2273 } 2273 }
2274 2274
2275 end: 2275 end:
2276 if (msg) 2276 if (msg)
2277 /* old style definition */ 2277 /* old style definition */
2278 print_previous_declaration(300, rdsym); 2278 print_previous_declaration(300, rdsym);
2279 2279
2280 return msg; 2280 return msg;
2281} 2281}
2282 2282
2283/* 2283/*
2284 * Completes a type by copying the dimension and prototype information 2284 * Completes a type by copying the dimension and prototype information
2285 * from a second compatible type. 2285 * from a second compatible type.
2286 * 2286 *
2287 * Following lines are legal: 2287 * Following lines are legal:
2288 * "typedef a[]; a b; a b[10]; a c; a c[20];" 2288 * "typedef a[]; a b; a b[10]; a c; a c[20];"
2289 * "typedef ft(); ft f; f(int); ft g; g(long);" 2289 * "typedef ft(); ft f; f(int); ft g; g(long);"
2290 * This means that, if a type is completed, the type structure must 2290 * This means that, if a type is completed, the type structure must
2291 * be duplicated. 2291 * be duplicated.
2292 */ 2292 */
2293void 2293void
2294complete_type(sym_t *dsym, sym_t *ssym) 2294complete_type(sym_t *dsym, sym_t *ssym)
2295{ 2295{
2296 type_t **dstp, *src; 2296 type_t **dstp, *src;
2297 type_t *dst; 2297 type_t *dst;
2298 2298
2299 dstp = &dsym->s_type; 2299 dstp = &dsym->s_type;
2300 src = ssym->s_type; 2300 src = ssym->s_type;
2301 2301
2302 while ((dst = *dstp) != NULL) { 2302 while ((dst = *dstp) != NULL) {
2303 lint_assert(src != NULL); 2303 lint_assert(src != NULL);
2304 lint_assert(dst->t_tspec == src->t_tspec); 2304 lint_assert(dst->t_tspec == src->t_tspec);
2305 if (dst->t_tspec == ARRAY) { 2305 if (dst->t_tspec == ARRAY) {
2306 if (dst->t_dim == 0 && src->t_dim != 0) { 2306 if (dst->t_dim == 0 && src->t_dim != 0) {
2307 *dstp = dst = duptyp(dst); 2307 *dstp = dst = duptyp(dst);
2308 dst->t_dim = src->t_dim; 2308 dst->t_dim = src->t_dim;
2309 setcomplete(dst, 1); 2309 setcomplete(dst, 1);
2310 } 2310 }
2311 } else if (dst->t_tspec == FUNC) { 2311 } else if (dst->t_tspec == FUNC) {
2312 if (!dst->t_proto && src->t_proto) { 2312 if (!dst->t_proto && src->t_proto) {
2313 *dstp = dst = duptyp(dst); 2313 *dstp = dst = duptyp(dst);
2314 dst->t_proto = 1; 2314 dst->t_proto = 1;
2315 dst->t_args = src->t_args; 2315 dst->t_args = src->t_args;
2316 } 2316 }
2317 } 2317 }
2318 dstp = &dst->t_subt; 2318 dstp = &dst->t_subt;
2319 src = src->t_subt; 2319 src = src->t_subt;
2320 } 2320 }
2321} 2321}
2322 2322
2323/* 2323/*
2324 * Completes the declaration of a single argument. 2324 * Completes the declaration of a single argument.
2325 */ 2325 */
2326sym_t * 2326sym_t *
2327decl1arg(sym_t *sym, int initflg) 2327decl1arg(sym_t *sym, int initflg)
2328{ 2328{
2329 tspec_t t; 2329 tspec_t t;
2330 2330
2331 check_function_definition(sym, 1); 2331 check_function_definition(sym, 1);
2332 2332
2333 check_type(sym); 2333 check_type(sym);
2334 2334
2335 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) { 2335 if (dcs->d_rdcsym != NULL && dcs->d_rdcsym->s_blklev == blklev) {
2336 /* redeclaration of formal parameter %s */ 2336 /* redeclaration of formal parameter %s */
2337 error(237, sym->s_name); 2337 error(237, sym->s_name);
2338 rmsym(dcs->d_rdcsym); 2338 rmsym(dcs->d_rdcsym);
2339 sym->s_arg = 1; 2339 sym->s_arg = 1;
2340 } 2340 }
2341 2341
2342 if (!sym->s_arg) { 2342 if (!sym->s_arg) {
2343 /* declared argument %s is missing */ 2343 /* declared argument %s is missing */
2344 error(53, sym->s_name); 2344 error(53, sym->s_name);
2345 sym->s_arg = 1; 2345 sym->s_arg = 1;
2346 } 2346 }
2347 2347
2348 if (initflg) { 2348 if (initflg) {
2349 /* cannot initialize parameter: %s */ 2349 /* cannot initialize parameter: %s */
2350 error(52, sym->s_name); 2350 error(52, sym->s_name);
2351 initerr = 1; 2351 initerr = 1;
2352 } 2352 }
2353 2353
2354 if ((t = sym->s_type->t_tspec) == ARRAY) { 2354 if ((t = sym->s_type->t_tspec) == ARRAY) {
2355 sym->s_type = incref(sym->s_type->t_subt, PTR); 2355 sym->s_type = incref(sym->s_type->t_subt, PTR);
2356 } else if (t == FUNC) { 2356 } else if (t == FUNC) {
2357 if (tflag) 2357 if (tflag)
2358 /* a function is declared as an argument: %s */ 2358 /* a function is declared as an argument: %s */
2359 warning(50, sym->s_name); 2359 warning(50, sym->s_name);
2360 sym->s_type = incref(sym->s_type, PTR); 2360 sym->s_type = incref(sym->s_type, PTR);
2361 } else if (t == FLOAT) { 2361 } else if (t == FLOAT) {
2362 if (tflag) 2362 if (tflag)
2363 sym->s_type = gettyp(DOUBLE); 2363 sym->s_type = gettyp(DOUBLE);
2364 } 2364 }
2365 2365
2366 if (dcs->d_inline) 2366 if (dcs->d_inline)
2367 /* argument declared inline: %s */ 2367 /* argument declared inline: %s */
2368 warning(269, sym->s_name); 2368 warning(269, sym->s_name);
2369 2369
2370 /* 2370 /*
2371 * Arguments must have complete types. lengths() prints the needed 2371 * Arguments must have complete types. lengths() prints the needed
2372 * error messages (null dimension is impossible because arrays are 2372 * error messages (null dimension is impossible because arrays are
2373 * converted to pointers). 2373 * converted to pointers).
2374 */ 2374 */
2375 if (sym->s_type->t_tspec != VOID) 2375 if (sym->s_type->t_tspec != VOID)
2376 (void)length(sym->s_type, sym->s_name); 2376 (void)length(sym->s_type, sym->s_name);
2377 2377
2378 sym->s_used = dcs->d_used; 2378 sym->s_used = dcs->d_used;
2379 mark_as_set(sym); 2379 mark_as_set(sym);
2380 2380
2381 return sym; 2381 return sym;
2382} 2382}
2383 2383
2384void 2384void
2385check_func_lint_directives(void) 2385check_func_lint_directives(void)
2386{ 2386{
2387 sym_t *arg; 2387 sym_t *arg;
2388 int narg, n; 2388 int narg, n;
2389 tspec_t t; 2389 tspec_t t;
2390 2390
2391 /* check for illegal combinations of lint directives */ 2391 /* check for illegal combinations of lint directives */
2392 if (prflstrg != -1 && scflstrg != -1) { 2392 if (prflstrg != -1 && scflstrg != -1) {
2393 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */ 2393 /* can't be used together: ** PRINTFLIKE ** ** SCANFLIKE ** */
2394 warning(289); 2394 warning(289);
2395 prflstrg = scflstrg = -1; 2395 prflstrg = scflstrg = -1;
2396 } 2396 }
2397 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) { 2397 if (nvararg != -1 && (prflstrg != -1 || scflstrg != -1)) {
2398 /* dubious use of ** VARARGS ** with ** %s ** */ 2398 /* dubious use of ** VARARGS ** with ** %s ** */
2399 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE"); 2399 warning(288, prflstrg != -1 ? "PRINTFLIKE" : "SCANFLIKE");
2400 nvararg = -1; 2400 nvararg = -1;
2401 } 2401 }
2402 2402
2403 /* 2403 /*
2404 * check if the argument of a lint directive is compatible with the 2404 * check if the argument of a lint directive is compatible with the
2405 * number of arguments. 2405 * number of arguments.
2406 */ 2406 */
2407 narg = 0; 2407 narg = 0;
2408 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_next) 2408 for (arg = dcs->d_fargs; arg != NULL; arg = arg->s_next)
2409 narg++; 2409 narg++;
2410 if (nargusg > narg) { 2410 if (nargusg > narg) {
2411 /* argument number mismatch with directive: ** %s ** */ 2411 /* argument number mismatch with directive: ** %s ** */
2412 warning(283, "ARGSUSED"); 2412 warning(283, "ARGSUSED");
2413 nargusg = 0; 2413 nargusg = 0;
2414 } 2414 }
2415 if (nvararg > narg) { 2415 if (nvararg > narg) {
2416 /* argument number mismatch with directive: ** %s ** */ 2416 /* argument number mismatch with directive: ** %s ** */
2417 warning(283, "VARARGS"); 2417 warning(283, "VARARGS");
2418 nvararg = 0; 2418 nvararg = 0;
2419 } 2419 }
2420 if (prflstrg > narg) { 2420 if (prflstrg > narg) {
2421 /* argument number mismatch with directive: ** %s ** */ 2421 /* argument number mismatch with directive: ** %s ** */
2422 warning(283, "PRINTFLIKE"); 2422 warning(283, "PRINTFLIKE");
2423 prflstrg = -1; 2423 prflstrg = -1;
2424 } else if (prflstrg == 0) { 2424 } else if (prflstrg == 0) {
2425 prflstrg = -1; 2425 prflstrg = -1;
2426 } 2426 }
2427 if (scflstrg > narg) { 2427 if (scflstrg > narg) {
2428 /* argument number mismatch with directive: ** %s ** */ 2428 /* argument number mismatch with directive: ** %s ** */
2429 warning(283, "SCANFLIKE"); 2429 warning(283, "SCANFLIKE");
2430 scflstrg = -1; 2430 scflstrg = -1;
2431 } else if (scflstrg == 0) { 2431 } else if (scflstrg == 0) {
2432 scflstrg = -1; 2432 scflstrg = -1;
2433 } 2433 }
2434 if (prflstrg != -1 || scflstrg != -1) { 2434 if (prflstrg != -1 || scflstrg != -1) {
2435 narg = prflstrg != -1 ? prflstrg : scflstrg; 2435 narg = prflstrg != -1 ? prflstrg : scflstrg;
2436 arg = dcs->d_fargs; 2436 arg = dcs->d_fargs;
2437 for (n = 1; n < narg; n++) 2437 for (n = 1; n < narg; n++)
2438 arg = arg->s_next; 2438 arg = arg->s_next;
2439 if (arg->s_type->t_tspec != PTR || 2439 if (arg->s_type->t_tspec != PTR ||
2440 ((t = arg->s_type->t_subt->t_tspec) != CHAR && 2440 ((t = arg->s_type->t_subt->t_tspec) != CHAR &&
2441 t != UCHAR && t != SCHAR)) { 2441 t != UCHAR && t != SCHAR)) {
2442 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */ 2442 /* arg. %d must be 'char *' for PRINTFLIKE/SCANFLIKE */
2443 warning(293, narg); 2443 warning(293, narg);
2444 prflstrg = scflstrg = -1; 2444 prflstrg = scflstrg = -1;
2445 } 2445 }
2446 } 2446 }
2447} 2447}
2448 2448
2449/* 2449/*
2450 * Warn about arguments in old style function definitions that default to int. 2450 * Warn about arguments in old style function definitions that default to int.
2451 * Check that an old style function definition is compatible to a previous 2451 * Check that an old style function definition is compatible to a previous
2452 * prototype. 2452 * prototype.
2453 */ 2453 */
2454void 2454void
2455check_func_old_style_arguments(void) 2455check_func_old_style_arguments(void)
2456{ 2456{
2457 sym_t *args, *arg, *pargs, *parg; 2457 sym_t *args, *arg, *pargs, *parg;
2458 int narg, nparg, msg; 2458 int narg, nparg, msg;
2459 2459
2460 args = funcsym->s_args; 2460 args = funcsym->s_args;
2461 pargs = funcsym->s_type->t_args; 2461 pargs = funcsym->s_type->t_args;
2462 2462
2463 /* 2463 /*
2464 * print a warning for each argument of an old style function 2464 * print a warning for each argument of an old style function
2465 * definition which defaults to int 2465 * definition which defaults to int
2466 */ 2466 */
2467 for (arg = args; arg != NULL; arg = arg->s_next) { 2467 for (arg = args; arg != NULL; arg = arg->s_next) {
2468 if (arg->s_defarg) { 2468 if (arg->s_defarg) {
2469 /* argument type defaults to 'int': %s */ 2469 /* argument type defaults to 'int': %s */
2470 warning(32, arg->s_name); 2470 warning(32, arg->s_name);
2471 arg->s_defarg = 0; 2471 arg->s_defarg = 0;
2472 mark_as_set(arg); 2472 mark_as_set(arg);
2473 } 2473 }
2474 } 2474 }
2475 2475
2476 /* 2476 /*
2477 * If this is an old style function definition and a prototype 2477 * If this is an old style function definition and a prototype
2478 * exists, compare the types of arguments. 2478 * exists, compare the types of arguments.
2479 */ 2479 */
2480 if (funcsym->s_osdef && funcsym->s_type->t_proto) { 2480 if (funcsym->s_osdef && funcsym->s_type->t_proto) {
2481 /* 2481 /*
2482 * If the number of arguments does not match, we need not 2482 * If the number of arguments does not match, we need not
2483 * continue. 2483 * continue.
2484 */ 2484 */
2485 narg = nparg = 0; 2485 narg = nparg = 0;
2486 msg = 0; 2486 msg = 0;
2487 for (parg = pargs; parg != NULL; parg = parg->s_next) 2487 for (parg = pargs; parg != NULL; parg = parg->s_next)
2488 nparg++; 2488 nparg++;
2489 for (arg = args; arg != NULL; arg = arg->s_next) 2489 for (arg = args; arg != NULL; arg = arg->s_next)
2490 narg++; 2490 narg++;
2491 if (narg != nparg) { 2491 if (narg != nparg) {
2492 /* parameter mismatch: %d declared, %d defined */ 2492 /* parameter mismatch: %d declared, %d defined */
2493 error(51, nparg, narg); 2493 error(51, nparg, narg);
2494 msg = 1; 2494 msg = 1;
2495 } else { 2495 } else {
2496 parg = pargs; 2496 parg = pargs;
2497 arg = args; 2497 arg = args;
2498 while (narg--) { 2498 while (narg--) {
2499 msg |= check_prototype_declaration(arg, parg); 2499 msg |= check_prototype_declaration(arg, parg);
2500 parg = parg->s_next; 2500 parg = parg->s_next;
2501 arg = arg->s_next; 2501 arg = arg->s_next;
2502 } 2502 }
2503 } 2503 }
2504 if (msg) 2504 if (msg)
2505 /* prototype declaration */ 2505 /* prototype declaration */
2506 print_previous_declaration(285, dcs->d_rdcsym); 2506 print_previous_declaration(285, dcs->d_rdcsym);
2507 2507
2508 /* from now on the prototype is valid */ 2508 /* from now on the prototype is valid */
2509 funcsym->s_osdef = 0; 2509 funcsym->s_osdef = 0;
2510 funcsym->s_args = NULL; 2510 funcsym->s_args = NULL;
2511 } 2511 }
2512} 2512}
2513 2513
2514/* 2514/*
2515 * Checks compatibility of an old style function definition with a previous 2515 * Checks compatibility of an old style function definition with a previous
2516 * prototype declaration. 2516 * prototype declaration.
2517 * Returns 1 if the position of the previous declaration should be reported. 2517 * Returns 1 if the position of the previous declaration should be reported.
2518 */ 2518 */
2519static int 2519static int
2520check_prototype_declaration(sym_t *arg, sym_t *parg) 2520check_prototype_declaration(sym_t *arg, sym_t *parg)
2521{ 2521{
2522 type_t *tp, *ptp; 2522 type_t *tp, *ptp;
2523 int dowarn, msg; 2523 int dowarn, msg;
2524 2524
2525 tp = arg->s_type; 2525 tp = arg->s_type;
2526 ptp = parg->s_type; 2526 ptp = parg->s_type;
2527 2527
2528 msg = 0; 2528 msg = 0;
2529 dowarn = 0; 2529 dowarn = 0;
2530 2530
2531 if (!eqtype(tp, ptp, 1, 1, &dowarn)) { 2531 if (!eqtype(tp, ptp, 1, 1, &dowarn)) {
2532 if (eqtype(tp, ptp, 1, 0, &dowarn)) { 2532 if (eqtype(tp, ptp, 1, 0, &dowarn)) {
2533 /* type does not match prototype: %s */ 2533 /* type does not match prototype: %s */
2534 gnuism(58, arg->s_name); 2534 gnuism(58, arg->s_name);
2535 msg = sflag || !gflag; 2535 msg = sflag || !gflag;
2536 } else { 2536 } else {
2537 /* type does not match prototype: %s */ 2537 /* type does not match prototype: %s */
2538 error(58, arg->s_name); 2538 error(58, arg->s_name);
2539 msg = 1; 2539 msg = 1;
2540 } 2540 }
2541 } else if (dowarn) { 2541 } else if (dowarn) {
2542 if (sflag) 2542 if (sflag)
2543 /* type does not match prototype: %s */ 2543 /* type does not match prototype: %s */
2544 error(58, arg->s_name); 2544 error(58, arg->s_name);
2545 else 2545 else
2546 /* type does not match prototype: %s */ 2546 /* type does not match prototype: %s */
2547 warning(58, arg->s_name); 2547 warning(58, arg->s_name);
2548 msg = 1; 2548 msg = 1;
2549 } 2549 }
2550 2550
2551 return msg; 2551 return msg;
2552} 2552}
2553 2553
2554/* 2554/*
2555 * Completes a single local declaration/definition. 2555 * Completes a single local declaration/definition.
2556 */ 2556 */
2557void 2557void
2558decl1loc(sym_t *dsym, int initflg) 2558decl1loc(sym_t *dsym, int initflg)
2559{ 2559{
2560 2560
2561 /* Correct a mistake done in declarator_name(). */ 2561 /* Correct a mistake done in declarator_name(). */
2562 if (dsym->s_type->t_tspec == FUNC) { 2562 if (dsym->s_type->t_tspec == FUNC) {
2563 dsym->s_def = DECL; 2563 dsym->s_def = DECL;
2564 if (dcs->d_scl == NOSCL) 2564 if (dcs->d_scl == NOSCL)
2565 dsym->s_scl = EXTERN; 2565 dsym->s_scl = EXTERN;
2566 } 2566 }
2567 2567
2568 if (dsym->s_type->t_tspec == FUNC) { 2568 if (dsym->s_type->t_tspec == FUNC) {
2569 if (dsym->s_scl == STATIC) { 2569 if (dsym->s_scl == STATIC) {
2570 /* dubious static function at block level: %s */ 2570 /* dubious static function at block level: %s */
2571 warning(93, dsym->s_name); 2571 warning(93, dsym->s_name);
2572 dsym->s_scl = EXTERN; 2572 dsym->s_scl = EXTERN;
2573 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) { 2573 } else if (dsym->s_scl != EXTERN && dsym->s_scl != TYPEDEF) {
2574 /* function has illegal storage class: %s */ 2574 /* function has illegal storage class: %s */
2575 error(94, dsym->s_name); 2575 error(94, dsym->s_name);
2576 dsym->s_scl = EXTERN; 2576 dsym->s_scl = EXTERN;
2577 } 2577 }
2578 } 2578 }
2579 2579
2580 /* 2580 /*
2581 * functions may be declared inline at local scope, although 2581 * functions may be declared inline at local scope, although
2582 * this has no effect for a later definition of the same 2582 * this has no effect for a later definition of the same
2583 * function. 2583 * function.
2584 * XXX it should have an effect if tflag is set. this would 2584 * XXX it should have an effect if tflag is set. this would
2585 * also be the way gcc behaves. 2585 * also be the way gcc behaves.
2586 */ 2586 */
2587 if (dcs->d_inline) { 2587 if (dcs->d_inline) {
2588 if (dsym->s_type->t_tspec == FUNC) { 2588 if (dsym->s_type->t_tspec == FUNC) {
2589 dsym->s_inline = 1; 2589 dsym->s_inline = 1;
2590 } else { 2590 } else {
2591 /* variable declared inline: %s */ 2591 /* variable declared inline: %s */
2592 warning(268, dsym->s_name); 2592 warning(268, dsym->s_name);
2593 } 2593 }
2594 } 2594 }
2595 2595
2596 check_function_definition(dsym, 1); 2596 check_function_definition(dsym, 1);
2597 2597
2598 check_type(dsym); 2598 check_type(dsym);
2599 2599
2600 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN) 2600 if (dcs->d_rdcsym != NULL && dsym->s_scl == EXTERN)
2601 ledecl(dsym); 2601 ledecl(dsym);
2602 2602
2603 if (dsym->s_scl == EXTERN) { 2603 if (dsym->s_scl == EXTERN) {
2604 /* 2604 /*
2605 * XXX if the static variable at level 0 is only defined 2605 * XXX if the static variable at level 0 is only defined
2606 * later, checking will be possible. 2606 * later, checking will be possible.
2607 */ 2607 */
2608 if (dsym->s_ext_sym == NULL) { 2608 if (dsym->s_ext_sym == NULL) {
2609 outsym(dsym, EXTERN, dsym->s_def); 2609 outsym(dsym, EXTERN, dsym->s_def);
2610 } else { 2610 } else {
2611 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def); 2611 outsym(dsym, dsym->s_ext_sym->s_scl, dsym->s_def);
2612 } 2612 }
2613 } 2613 }
2614 2614
2615 if (dcs->d_rdcsym != NULL) { 2615 if (dcs->d_rdcsym != NULL) {
2616 2616
2617 if (dcs->d_rdcsym->s_blklev == 0) { 2617 if (dcs->d_rdcsym->s_blklev == 0) {
2618 2618
2619 switch (dsym->s_scl) { 2619 switch (dsym->s_scl) {
2620 case AUTO: 2620 case AUTO:
2621 if (hflag) 2621 if (hflag)
2622 /* automatic hides external decl.: %s */ 2622 /* automatic hides external decl.: %s */
2623 warning(86, dsym->s_name); 2623 warning(86, dsym->s_name);
2624 break; 2624 break;
2625 case STATIC: 2625 case STATIC:
2626 if (hflag) 2626 if (hflag)
2627 /* static hides external decl.: %s */ 2627 /* static hides external decl.: %s */
2628 warning(87, dsym->s_name); 2628 warning(87, dsym->s_name);
2629 break; 2629 break;
2630 case TYPEDEF: 2630 case TYPEDEF:
2631 if (hflag) 2631 if (hflag)
2632 /* typedef hides external decl.: %s */ 2632 /* typedef hides external decl.: %s */
2633 warning(88, dsym->s_name); 2633 warning(88, dsym->s_name);
2634 break; 2634 break;
2635 case EXTERN: 2635 case EXTERN:
2636 /* 2636 /*
2637 * Warnings and errors are printed in ledecl() 2637 * Warnings and errors are printed in ledecl()
2638 */ 2638 */
2639 break; 2639 break;
2640 default: 2640 default:
2641 lint_assert(/*CONSTCOND*/0); 2641 lint_assert(/*CONSTCOND*/0);
2642 } 2642 }
2643 2643
2644 } else if (dcs->d_rdcsym->s_blklev == blklev) { 2644 } else if (dcs->d_rdcsym->s_blklev == blklev) {
2645 2645
2646 /* no hflag, because it's illegal! */ 2646 /* no hflag, because it's illegal! */
2647 if (dcs->d_rdcsym->s_arg) { 2647 if (dcs->d_rdcsym->s_arg) {
2648 /* 2648 /*
2649 * if !tflag, a "redeclaration of %s" error 2649 * if !tflag, a "redeclaration of %s" error
2650 * is produced below 2650 * is produced below
2651 */ 2651 */
2652 if (tflag) { 2652 if (tflag) {
2653 if (hflag) 2653 if (hflag)
2654 /* decl. hides parameter: %s */ 2654 /* decl. hides parameter: %s */
2655 warning(91, dsym->s_name); 2655 warning(91, dsym->s_name);
2656 rmsym(dcs->d_rdcsym); 2656 rmsym(dcs->d_rdcsym);
2657 } 2657 }
2658 } 2658 }
2659 2659
2660 } else if (dcs->d_rdcsym->s_blklev < blklev) { 2660 } else if (dcs->d_rdcsym->s_blklev < blklev) {
2661 2661
2662 if (hflag) 2662 if (hflag)
2663 /* declaration hides earlier one: %s */ 2663 /* declaration hides earlier one: %s */
2664 warning(95, dsym->s_name); 2664 warning(95, dsym->s_name);
2665 2665
2666 } 2666 }
2667 2667
2668 if (dcs->d_rdcsym->s_blklev == blklev) { 2668 if (dcs->d_rdcsym->s_blklev == blklev) {
2669 2669
2670 /* redeclaration of %s */ 2670 /* redeclaration of %s */
2671 error(27, dsym->s_name); 2671 error(27, dsym->s_name);
2672 rmsym(dcs->d_rdcsym); 2672 rmsym(dcs->d_rdcsym);
2673 2673
2674 } 2674 }
2675 2675
2676 } 2676 }
2677 2677
2678 if (initflg && !(initerr = check_init(dsym))) { 2678 if (initflg && !(initerr = check_init(dsym))) {
2679 dsym->s_def = DEF; 2679 dsym->s_def = DEF;
2680 mark_as_set(dsym); 2680 mark_as_set(dsym);
2681 } 2681 }
2682 2682
2683 if (dsym->s_scl == TYPEDEF) { 2683 if (dsym->s_scl == TYPEDEF) {
2684 dsym->s_type = duptyp(dsym->s_type); 2684 dsym->s_type = duptyp(dsym->s_type);
2685 dsym->s_type->t_typedef = 1; 2685 dsym->s_type->t_typedef = 1;
2686 settdsym(dsym->s_type, dsym); 2686 settdsym(dsym->s_type, dsym);
2687 } 2687 }
2688 2688
2689 /* 2689 /*
2690 * Before we can check the size we must wait for a initialization 2690 * Before we can check the size we must wait for a initialization
2691 * which may follow. 2691 * which may follow.
2692 */ 2692 */
2693} 2693}
2694 2694
2695/* 2695/*
2696 * Processes (re)declarations of external symbols inside blocks. 2696 * Processes (re)declarations of external symbols inside blocks.
2697 */ 2697 */
2698static void 2698static void
2699ledecl(sym_t *dsym) 2699ledecl(sym_t *dsym)
2700{ 2700{
2701 int eqt, dowarn; 2701 int eqt, dowarn;
2702 sym_t *esym; 2702 sym_t *esym;
2703 2703
2704 /* look for a symbol with the same name */ 2704 /* look for a symbol with the same name */
2705 esym = dcs->d_rdcsym; 2705 esym = dcs->d_rdcsym;
2706 while (esym != NULL && esym->s_blklev != 0) { 2706 while (esym != NULL && esym->s_blklev != 0) {
2707 while ((esym = esym->s_link) != NULL) { 2707 while ((esym = esym->s_link) != NULL) {
2708 if (esym->s_kind != FVFT) 2708 if (esym->s_kind != FVFT)
2709 continue; 2709 continue;
2710 if (strcmp(dsym->s_name, esym->s_name) == 0) 2710 if (strcmp(dsym->s_name, esym->s_name) == 0)
2711 break; 2711 break;
2712 } 2712 }
2713 } 2713 }
2714 if (esym == NULL) 2714 if (esym == NULL)
2715 return; 2715 return;
2716 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) { 2716 if (esym->s_scl != EXTERN && esym->s_scl != STATIC) {
2717 /* gcc accepts this without a warning, pcc prints an error. */ 2717 /* gcc accepts this without a warning, pcc prints an error. */
2718 /* redeclaration of %s */ 2718 /* redeclaration of %s */
2719 warning(27, dsym->s_name); 2719 warning(27, dsym->s_name);
2720 print_previous_declaration(-1, esym); 2720 print_previous_declaration(-1, esym);
2721 return; 2721 return;
2722 } 2722 }
2723 2723
2724 dowarn = 0; 2724 dowarn = 0;
2725 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &dowarn); 2725 eqt = eqtype(esym->s_type, dsym->s_type, 0, 0, &dowarn);
2726 2726
2727 if (!eqt || dowarn) { 2727 if (!eqt || dowarn) {
2728 if (esym->s_scl == EXTERN) { 2728 if (esym->s_scl == EXTERN) {
2729 /* inconsistent redeclaration of extern: %s */ 2729 /* inconsistent redeclaration of extern: %s */
2730 warning(90, dsym->s_name); 2730 warning(90, dsym->s_name);
2731 print_previous_declaration(-1, esym); 2731 print_previous_declaration(-1, esym);
2732 } else { 2732 } else {
2733 /* inconsistent redeclaration of static: %s */ 2733 /* inconsistent redeclaration of static: %s */
2734 warning(92, dsym->s_name); 2734 warning(92, dsym->s_name);
2735 print_previous_declaration(-1, esym); 2735 print_previous_declaration(-1, esym);
2736 } 2736 }
2737 } 2737 }
2738 2738
2739 if (eqt) { 2739 if (eqt) {
2740 /* 2740 /*
2741 * Remember the external symbol so we can update usage 2741 * Remember the external symbol so we can update usage
2742 * information at the end of the block. 2742 * information at the end of the block.
2743 */ 2743 */
2744 dsym->s_ext_sym = esym; 2744 dsym->s_ext_sym = esym;
2745 } 2745 }
2746} 2746}
2747 2747
2748/* 2748/*
2749 * Print an error or a warning if the symbol cannot be initialized due 2749 * Print an error or a warning if the symbol cannot be initialized due
2750 * to type/storage class. Return 1 if an error has been detected. 2750 * to type/storage class. Return 1 if an error has been detected.
2751 */ 2751 */
2752static int 2752static int
2753check_init(sym_t *sym) 2753check_init(sym_t *sym)
2754{ 2754{
2755 int erred; 2755 int erred;
2756 2756
2757 erred = 0; 2757 erred = 0;
2758 2758
2759 if (sym->s_type->t_tspec == FUNC) { 2759 if (sym->s_type->t_tspec == FUNC) {
2760 /* cannot initialize function: %s */ 2760 /* cannot initialize function: %s */
2761 error(24, sym->s_name); 2761 error(24, sym->s_name);
2762 erred = 1; 2762 erred = 1;
2763 } else if (sym->s_scl == TYPEDEF) { 2763 } else if (sym->s_scl == TYPEDEF) {
2764 /* cannot initialize typedef: %s */ 2764 /* cannot initialize typedef: %s */
2765 error(25, sym->s_name); 2765 error(25, sym->s_name);
2766 erred = 1; 2766 erred = 1;
2767 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) { 2767 } else if (sym->s_scl == EXTERN && sym->s_def == DECL) {
2768 /* cannot initialize "extern" declaration: %s */ 2768 /* cannot initialize "extern" declaration: %s */
2769 if (dcs->d_ctx == EXTERN) { 2769 if (dcs->d_ctx == EXTERN) {
2770 /* cannot initialize extern declaration: %s */ 2770 /* cannot initialize extern declaration: %s */
2771 warning(26, sym->s_name); 2771 warning(26, sym->s_name);
2772 } else { 2772 } else {
2773 /* cannot initialize extern declaration: %s */ 2773 /* cannot initialize extern declaration: %s */
2774 error(26, sym->s_name); 2774 error(26, sym->s_name);
2775 erred = 1; 2775 erred = 1;
2776 } 2776 }
2777 } 2777 }
2778 2778
2779 return erred; 2779 return erred;
2780} 2780}
2781 2781
2782/* 2782/*
2783 * Create a symbol for an abstract declaration. 2783 * Create a symbol for an abstract declaration.
2784 */ 2784 */
2785sym_t * 2785sym_t *
2786abstract_name(void) 2786abstract_name(void)
2787{ 2787{
2788 sym_t *sym; 2788 sym_t *sym;
2789 2789

cvs diff -r1.58 -r1.59 src/usr.bin/xlint/lint1/init.c (switch to unified diff)

--- src/usr.bin/xlint/lint1/init.c 2021/01/03 17:42:45 1.58
+++ src/usr.bin/xlint/lint1/init.c 2021/01/03 19:10:47 1.59
@@ -1,730 +1,730 @@ @@ -1,730 +1,730 @@
1/* $NetBSD: init.c,v 1.58 2021/01/03 17:42:45 rillig Exp $ */ 1/* $NetBSD: init.c,v 1.59 2021/01/03 19:10:47 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for 17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project. 18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products 19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission. 20 * derived from this software without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: init.c,v 1.58 2021/01/03 17:42:45 rillig Exp $"); 40__RCSID("$NetBSD: init.c,v 1.59 2021/01/03 19:10:47 rillig Exp $");
41#endif 41#endif
42 42
43#include <stdlib.h> 43#include <stdlib.h>
44#include <string.h> 44#include <string.h>
45 45
46#include "lint1.h" 46#include "lint1.h"
47 47
48 48
49/* 49/*
50 * Type of stack which is used for initialisation of aggregate types. 50 * Type of stack which is used for initialisation of aggregate types.
51 * 51 *
52 * XXX: Since C99, a stack is an inappropriate data structure for modelling 52 * XXX: Since C99, a stack is an inappropriate data structure for modelling
53 * an initialization, since the designators don't have to be listed in a 53 * an initialization, since the designators don't have to be listed in a
54 * particular order and can designate parts of sub-objects. The member names 54 * particular order and can designate parts of sub-objects. The member names
55 * of non-leaf structs may thus appear repeatedly, as demonstrated in 55 * of non-leaf structs may thus appear repeatedly, as demonstrated in
56 * d_init_pop_member.c. 56 * d_init_pop_member.c.
57 * 57 *
58 * XXX: During initialization, there may be members of the top-level struct 58 * XXX: During initialization, there may be members of the top-level struct
59 * that are partially initialized. The simple i_remaining cannot model this 59 * that are partially initialized. The simple i_remaining cannot model this
60 * appropriately. 60 * appropriately.
61 * 61 *
62 * See C99 6.7.8, which spans 6 pages full of tricky details and carefully 62 * See C99 6.7.8, which spans 6 pages full of tricky details and carefully
63 * selected examples. 63 * selected examples.
64 */ 64 */
65typedef struct istk { 65typedef struct istk {
66 type_t *i_type; /* type of initialisation */ 66 type_t *i_type; /* type of initialisation */
67 type_t *i_subt; /* type of next level */ 67 type_t *i_subt; /* type of next level */
68 bool i_brace : 1; /* need } for pop */ 68 bool i_brace : 1; /* need } for pop */
69 bool i_nolimit : 1; /* incomplete array type */ 69 bool i_nolimit : 1; /* incomplete array type */
70 bool i_namedmem : 1; /* has c9x named members */ 70 bool i_namedmem : 1; /* has c9x named members */
71 sym_t *i_mem; /* next structure member */ 71 sym_t *i_mem; /* next structure member */
72 int i_remaining; /* # of remaining elements */ 72 int i_remaining; /* # of remaining elements */
73 struct istk *i_next; /* previous level */ 73 struct istk *i_next; /* previous level */
74} istk_t; 74} istk_t;
75 75
76/* 76/*
77 * The names for a nested C99 initialization designator, in a circular list. 77 * The names for a nested C99 initialization designator, in a circular list.
78 * 78 *
79 * Example: 79 * Example:
80 * struct stat st = { 80 * struct stat st = {
81 * .st_size = 123, 81 * .st_size = 123,
82 * .st_mtim.tv_sec = 45, 82 * .st_mtim.tv_sec = 45,
83 * .st_mtim.tv_nsec 83 * .st_mtim.tv_nsec
84 * }; 84 * };
85 * 85 *
86 * During initialization, this list first contains ["st_size"], then 86 * During initialization, this list first contains ["st_size"], then
87 * ["st_mtim", "tv_sec"], then ["st_mtim", "tv_nsec"]. 87 * ["st_mtim", "tv_sec"], then ["st_mtim", "tv_nsec"].
88 */ 88 */
89typedef struct namlist { 89typedef struct namlist {
90 const char *n_name; 90 const char *n_name;
91 struct namlist *n_prev; 91 struct namlist *n_prev;
92 struct namlist *n_next; 92 struct namlist *n_next;
93} namlist_t; 93} namlist_t;
94 94
95 95
96/* 96/*
97 * initerr is set as soon as a fatal error occurred in an initialisation. 97 * initerr is set as soon as a fatal error occurred in an initialisation.
98 * The effect is that the rest of the initialisation is ignored (parsed 98 * The effect is that the rest of the initialisation is ignored (parsed
99 * by yacc, expression trees built, but no initialisation takes place). 99 * by yacc, expression trees built, but no initialisation takes place).
100 */ 100 */
101int initerr; 101int initerr;
102 102
103/* Pointer to the symbol which is to be initialized. */ 103/* Pointer to the symbol which is to be initialized. */
104sym_t *initsym; 104sym_t *initsym;
105 105
106/* Points to the top element of the initialisation stack. */ 106/* Points to the top element of the initialisation stack. */
107istk_t *initstk; 107istk_t *initstk;
108 108
109/* Points to a c9x named member; */ 109/* Points to a c9x named member; */
110namlist_t *namedmem = NULL; 110namlist_t *namedmem = NULL;
111 111
112 112
113static int initstack_string(tnode_t *); 113static int initstack_string(tnode_t *);
114 114
115#ifndef DEBUG 115#ifndef DEBUG
116#define DPRINTF(a) 116#define DPRINTF(a)
117#else 117#else
118#define DPRINTF(a) printf a 118#define DPRINTF(a) printf a
119#endif 119#endif
120 120
121void 121void
122push_member(sbuf_t *sb) 122push_member(sbuf_t *sb)
123{ 123{
124 namlist_t *nam = xcalloc(1, sizeof (namlist_t)); 124 namlist_t *nam = xcalloc(1, sizeof (namlist_t));
125 nam->n_name = sb->sb_name; 125 nam->n_name = sb->sb_name;
126 DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam)); 126 DPRINTF(("%s: %s %p\n", __func__, nam->n_name, nam));
127 if (namedmem == NULL) { 127 if (namedmem == NULL) {
128 /* 128 /*
129 * XXX: Why is this a circular list? 129 * XXX: Why is this a circular list?
130 * XXX: Why is this a doubly-linked list? 130 * XXX: Why is this a doubly-linked list?
131 * A simple stack should suffice. 131 * A simple stack should suffice.
132 */ 132 */
133 nam->n_prev = nam->n_next = nam; 133 nam->n_prev = nam->n_next = nam;
134 namedmem = nam; 134 namedmem = nam;
135 } else { 135 } else {
136 namedmem->n_prev->n_next = nam; 136 namedmem->n_prev->n_next = nam;
137 nam->n_prev = namedmem->n_prev; 137 nam->n_prev = namedmem->n_prev;
138 nam->n_next = namedmem; 138 nam->n_next = namedmem;
139 namedmem->n_prev = nam; 139 namedmem->n_prev = nam;
140 } 140 }
141} 141}
142 142
143static void 143static void
144pop_member(void) 144pop_member(void)
145{ 145{
146 DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem)); 146 DPRINTF(("%s: %s %p\n", __func__, namedmem->n_name, namedmem));
147 if (namedmem->n_next == namedmem) { 147 if (namedmem->n_next == namedmem) {
148 free(namedmem); 148 free(namedmem);
149 namedmem = NULL; 149 namedmem = NULL;
150 } else { 150 } else {
151 namlist_t *nam = namedmem; 151 namlist_t *nam = namedmem;
152 namedmem = namedmem->n_next; 152 namedmem = namedmem->n_next;
153 nam->n_prev->n_next = nam->n_next; 153 nam->n_prev->n_next = nam->n_next;
154 nam->n_next->n_prev = nam->n_prev; 154 nam->n_next->n_prev = nam->n_prev;
155 free(nam); 155 free(nam);
156 } 156 }
157} 157}
158 158
159static void 159static void
160named_member_dprint(void) 160named_member_dprint(void)
161{ 161{
162 namlist_t *name; 162 namlist_t *name;
163 163
164 if (namedmem == NULL) 164 if (namedmem == NULL)
165 return; 165 return;
166 name = namedmem; 166 name = namedmem;
167 DPRINTF(("named member:")); 167 DPRINTF(("named member:"));
168 do { 168 do {
169 DPRINTF((" %s", name->n_name)); 169 DPRINTF((" %s", name->n_name));
170 name = name->n_next; 170 name = name->n_next;
171 } while (name != namedmem); 171 } while (name != namedmem);
172 DPRINTF(("\n")); 172 DPRINTF(("\n"));
173} 173}
174 174
175/* 175/*
176 * Initialize the initialisation stack by putting an entry for the variable 176 * Initialize the initialisation stack by putting an entry for the variable
177 * which is to be initialized on it. 177 * which is to be initialized on it.
178 */ 178 */
179void 179void
180initstack_init(void) 180initstack_init(void)
181{ 181{
182 istk_t *istk; 182 istk_t *istk;
183 183
184 if (initerr) 184 if (initerr)
185 return; 185 return;
186 186
187 /* free memory used in last initialisation */ 187 /* free memory used in last initialisation */
188 while ((istk = initstk) != NULL) { 188 while ((istk = initstk) != NULL) {
189 initstk = istk->i_next; 189 initstk = istk->i_next;
190 free(istk); 190 free(istk);
191 } 191 }
192 192
193 DPRINTF(("%s\n", __func__)); 193 DPRINTF(("%s\n", __func__));
194 194
195 /* 195 /*
196 * If the type which is to be initialized is an incomplete type, 196 * If the type which is to be initialized is an incomplete type,
197 * it must be duplicated. 197 * it must be duplicated.
198 */ 198 */
199 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type)) 199 if (initsym->s_type->t_tspec == ARRAY && incompl(initsym->s_type))
200 initsym->s_type = duptyp(initsym->s_type); 200 initsym->s_type = duptyp(initsym->s_type);
201 201
202 istk = initstk = xcalloc(1, sizeof (istk_t)); 202 istk = initstk = xcalloc(1, sizeof (istk_t));
203 istk->i_subt = initsym->s_type; 203 istk->i_subt = initsym->s_type;
204 istk->i_remaining = 1; 204 istk->i_remaining = 1;
205} 205}
206 206
207static void 207static void
208initstack_pop_item(void) 208initstack_pop_item(void)
209{ 209{
210 istk_t *istk; 210 istk_t *istk;
211 sym_t *m; 211 sym_t *m;
212 212
213 istk = initstk; 213 istk = initstk;
214 DPRINTF(("%s: pop type=%s, brace=%d remaining=%d named=%d\n", __func__, 214 DPRINTF(("%s: pop type=%s, brace=%d remaining=%d named=%d\n", __func__,
215 type_name(istk->i_type ? istk->i_type : istk->i_subt), 215 type_name(istk->i_type ? istk->i_type : istk->i_subt),
216 istk->i_brace, istk->i_remaining, istk->i_namedmem)); 216 istk->i_brace, istk->i_remaining, istk->i_namedmem));
217 217
218 initstk = istk->i_next; 218 initstk = istk->i_next;
219 free(istk); 219 free(istk);
220 istk = initstk; 220 istk = initstk;
221 lint_assert(istk != NULL); 221 lint_assert(istk != NULL);
222 222
223 DPRINTF(("%s: top type=%s, brace=%d remaining=%d named=%d\n", __func__, 223 DPRINTF(("%s: top type=%s, brace=%d remaining=%d named=%d\n", __func__,
224 type_name(istk->i_type ? istk->i_type : istk->i_subt), 224 type_name(istk->i_type ? istk->i_type : istk->i_subt),
225 istk->i_brace, istk->i_remaining, istk->i_namedmem)); 225 istk->i_brace, istk->i_remaining, istk->i_namedmem));
226 226
227 istk->i_remaining--; 227 istk->i_remaining--;
228 lint_assert(istk->i_remaining >= 0); 228 lint_assert(istk->i_remaining >= 0);
229 229
230 DPRINTF(("%s: top remaining=%d rhs.name=%s\n", __func__, 230 DPRINTF(("%s: top remaining=%d rhs.name=%s\n", __func__,
231 istk->i_remaining, namedmem ? namedmem->n_name : "*null*")); 231 istk->i_remaining, namedmem ? namedmem->n_name : "*null*"));
232 232
233 if (istk->i_remaining >= 0 && namedmem != NULL) { 233 if (istk->i_remaining >= 0 && namedmem != NULL) {
234 234
235 DPRINTF(("%s: named remaining=%d type=%s, rhs.name=%s\n", 235 DPRINTF(("%s: named remaining=%d type=%s, rhs.name=%s\n",
236 __func__, istk->i_remaining, 236 __func__, istk->i_remaining,
237 type_name(istk->i_type), namedmem->n_name)); 237 type_name(istk->i_type), namedmem->n_name));
238 238
239 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) { 239 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) {
240 DPRINTF(("%s: pop lhs.name=%s rhs.name=%s\n", __func__, 240 DPRINTF(("%s: pop lhs.name=%s rhs.name=%s\n", __func__,
241 m->s_name, namedmem->n_name)); 241 m->s_name, namedmem->n_name));
242 if (m->s_bitfield && m->s_name == unnamed) 242 if (m->s_bitfield && m->s_name == unnamed)
243 continue; 243 continue;
244 if (strcmp(m->s_name, namedmem->n_name) == 0) { 244 if (strcmp(m->s_name, namedmem->n_name) == 0) {
245 istk->i_subt = m->s_type; 245 istk->i_subt = m->s_type;
246 istk->i_remaining++; 246 istk->i_remaining++;
247 pop_member(); 247 pop_member();
248 return; 248 return;
249 } 249 }
250 } 250 }
251 /* undefined struct/union member: %s */ 251 /* undefined struct/union member: %s */
252 error(101, namedmem->n_name); 252 error(101, namedmem->n_name);
253 DPRINTF(("%s: end rhs.name=%s\n", __func__, namedmem->n_name)); 253 DPRINTF(("%s: end rhs.name=%s\n", __func__, namedmem->n_name));
254 pop_member(); 254 pop_member();
255 istk->i_namedmem = 1; 255 istk->i_namedmem = 1;
256 return; 256 return;
257 } 257 }
258 /* 258 /*
259 * If the removed element was a structure member, we must go 259 * If the removed element was a structure member, we must go
260 * to the next structure member. 260 * to the next structure member.
261 */ 261 */
262 if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT && 262 if (istk->i_remaining > 0 && istk->i_type->t_tspec == STRUCT &&
263 !istk->i_namedmem) { 263 !istk->i_namedmem) {
264 do { 264 do {
265 m = istk->i_mem = istk->i_mem->s_next; 265 m = istk->i_mem = istk->i_mem->s_next;
266 lint_assert(m != NULL); 266 lint_assert(m != NULL);
267 DPRINTF(("%s: pop %s\n", __func__, m->s_name)); 267 DPRINTF(("%s: pop %s\n", __func__, m->s_name));
268 } while (m->s_bitfield && m->s_name == unnamed); 268 } while (m->s_bitfield && m->s_name == unnamed);
269 istk->i_subt = m->s_type; 269 istk->i_subt = m->s_type;
270 } 270 }
271} 271}
272 272
273/* 273/*
274 * Take all entries, including the first which requires a closing brace, 274 * Take all entries, including the first which requires a closing brace,
275 * from the stack. 275 * from the stack.
276 */ 276 */
277static void 277static void
278initstack_pop_brace(void) 278initstack_pop_brace(void)
279{ 279{
280 int brace; 280 int brace;
281 281
282 DPRINTF(("%s\n", __func__)); 282 DPRINTF(("%s\n", __func__));
283 do { 283 do {
284 brace = initstk->i_brace; 284 brace = initstk->i_brace;
285 DPRINTF(("%s: loop brace=%d\n", __func__, brace)); 285 DPRINTF(("%s: loop brace=%d\n", __func__, brace));
286 initstack_pop_item(); 286 initstack_pop_item();
287 } while (!brace); 287 } while (!brace);
288 DPRINTF(("%s: done\n", __func__)); 288 DPRINTF(("%s: done\n", __func__));
289} 289}
290 290
291/* 291/*
292 * Take all entries which cannot be used for further initializers from the 292 * Take all entries which cannot be used for further initializers from the
293 * stack, but do this only if they do not require a closing brace. 293 * stack, but do this only if they do not require a closing brace.
294 */ 294 */
295static void 295static void
296initstack_pop_nobrace(void) 296initstack_pop_nobrace(void)
297{ 297{
298 298
299 DPRINTF(("%s\n", __func__)); 299 DPRINTF(("%s\n", __func__));
300 while (!initstk->i_brace && initstk->i_remaining == 0 && 300 while (!initstk->i_brace && initstk->i_remaining == 0 &&
301 !initstk->i_nolimit) 301 !initstk->i_nolimit)
302 initstack_pop_item(); 302 initstack_pop_item();
303 DPRINTF(("%s: done\n", __func__)); 303 DPRINTF(("%s: done\n", __func__));
304} 304}
305 305
306static void 306static void
307initstack_push(void) 307initstack_push(void)
308{ 308{
309 istk_t *istk, *inxt; 309 istk_t *istk, *inxt;
310 int cnt; 310 int cnt;
311 sym_t *m; 311 sym_t *m;
312 312
313 istk = initstk; 313 istk = initstk;
314 314
315 /* Extend an incomplete array type by one element */ 315 /* Extend an incomplete array type by one element */
316 if (istk->i_remaining == 0) { 316 if (istk->i_remaining == 0) {
317 DPRINTF(("%s(extend) %s\n", __func__, type_name(istk->i_type))); 317 DPRINTF(("%s(extend) %s\n", __func__, type_name(istk->i_type)));
318 /* 318 /*
319 * Inside of other aggregate types must not be an incomplete 319 * Inside of other aggregate types must not be an incomplete
320 * type. 320 * type.
321 */ 321 */
322 lint_assert(istk->i_next->i_next == NULL); 322 lint_assert(istk->i_next->i_next == NULL);
323 istk->i_remaining = 1; 323 istk->i_remaining = 1;
324 lint_assert(istk->i_type->t_tspec == ARRAY); 324 lint_assert(istk->i_type->t_tspec == ARRAY);
325 istk->i_type->t_dim++; 325 istk->i_type->t_dim++;
326 setcomplete(istk->i_type, 1); 326 setcomplete(istk->i_type, 1);
327 } 327 }
328 328
329 lint_assert(istk->i_remaining > 0); 329 lint_assert(istk->i_remaining > 0);
330 lint_assert(istk->i_type == NULL || 330 lint_assert(istk->i_type == NULL ||
331 !tspec_is_scalar(istk->i_type->t_tspec)); 331 !tspec_is_scalar(istk->i_type->t_tspec));
332 332
333 initstk = xcalloc(1, sizeof (istk_t)); 333 initstk = xcalloc(1, sizeof (istk_t));
334 initstk->i_next = istk; 334 initstk->i_next = istk;
335 initstk->i_type = istk->i_subt; 335 initstk->i_type = istk->i_subt;
336 lint_assert(initstk->i_type->t_tspec != FUNC); 336 lint_assert(initstk->i_type->t_tspec != FUNC);
337 337
338again: 338again:
339 istk = initstk; 339 istk = initstk;
340 340
341 DPRINTF(("%s(%s)\n", __func__, type_name(istk->i_type))); 341 DPRINTF(("%s(%s)\n", __func__, type_name(istk->i_type)));
342 switch (istk->i_type->t_tspec) { 342 switch (istk->i_type->t_tspec) {
343 case ARRAY: 343 case ARRAY:
344 if (namedmem) { 344 if (namedmem) {
345 DPRINTF(("%s: ARRAY %s brace=%d\n", __func__, 345 DPRINTF(("%s: ARRAY %s brace=%d\n", __func__,
346 namedmem->n_name, istk->i_brace)); 346 namedmem->n_name, istk->i_brace));
347 goto pop; 347 goto pop;
348 } else if (istk->i_next->i_namedmem) { 348 } else if (istk->i_next->i_namedmem) {
349 istk->i_brace = 1; 349 istk->i_brace = 1;
350 DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__, 350 DPRINTF(("%s ARRAY brace=%d, namedmem=%d\n", __func__,
351 istk->i_brace, istk->i_next->i_namedmem)); 351 istk->i_brace, istk->i_next->i_namedmem));
352 } 352 }
353 353
354 if (incompl(istk->i_type) && istk->i_next->i_next != NULL) { 354 if (incompl(istk->i_type) && istk->i_next->i_next != NULL) {
355 /* initialisation of an incomplete type */ 355 /* initialisation of an incomplete type */
356 error(175); 356 error(175);
357 initerr = 1; 357 initerr = 1;
358 return; 358 return;
359 } 359 }
360 istk->i_subt = istk->i_type->t_subt; 360 istk->i_subt = istk->i_type->t_subt;
361 istk->i_nolimit = incompl(istk->i_type); 361 istk->i_nolimit = incompl(istk->i_type);
362 istk->i_remaining = istk->i_type->t_dim; 362 istk->i_remaining = istk->i_type->t_dim;
363 DPRINTF(("%s: elements array %s[%d] %s\n", __func__, 363 DPRINTF(("%s: elements array %s[%d] %s\n", __func__,
364 type_name(istk->i_subt), istk->i_remaining, 364 type_name(istk->i_subt), istk->i_remaining,
365 namedmem ? namedmem->n_name : "*none*")); 365 namedmem ? namedmem->n_name : "*none*"));
366 break; 366 break;
367 case UNION: 367 case UNION:
368 if (tflag) 368 if (tflag)
369 /* initialisation of union is illegal in trad. C */ 369 /* initialisation of union is illegal in trad. C */
370 warning(238); 370 warning(238);
371 /* FALLTHROUGH */ 371 /* FALLTHROUGH */
372 case STRUCT: 372 case STRUCT:
373 if (incompl(istk->i_type)) { 373 if (incompl(istk->i_type)) {
374 /* initialisation of an incomplete type */ 374 /* initialisation of an incomplete type */
375 error(175); 375 error(175);
376 initerr = 1; 376 initerr = 1;
377 return; 377 return;
378 } 378 }
379 cnt = 0; 379 cnt = 0;
380 DPRINTF(("%s: lookup type=%s, name=%s named=%d\n", __func__, 380 DPRINTF(("%s: lookup type=%s, name=%s named=%d\n", __func__,
381 type_name(istk->i_type), 381 type_name(istk->i_type),
382 namedmem ? namedmem->n_name : "*none*", istk->i_namedmem)); 382 namedmem ? namedmem->n_name : "*none*", istk->i_namedmem));
383 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) { 383 for (m = istk->i_type->t_str->memb; m != NULL; m = m->s_next) {
384 if (m->s_bitfield && m->s_name == unnamed) 384 if (m->s_bitfield && m->s_name == unnamed)
385 continue; 385 continue;
386 if (namedmem != NULL) { 386 if (namedmem != NULL) {
387 DPRINTF(("%s: named lhs.member=%s, rhs.member=%s\n", 387 DPRINTF(("%s: named lhs.member=%s, rhs.member=%s\n",
388 __func__, m->s_name, namedmem->n_name)); 388 __func__, m->s_name, namedmem->n_name));
389 if (strcmp(m->s_name, namedmem->n_name) == 0) { 389 if (strcmp(m->s_name, namedmem->n_name) == 0) {
390 cnt++; 390 cnt++;
391 break; 391 break;
392 } else 392 } else
393 continue; 393 continue;
394 } 394 }
395 if (++cnt == 1) { 395 if (++cnt == 1) {
396 istk->i_mem = m; 396 istk->i_mem = m;
397 istk->i_subt = m->s_type; 397 istk->i_subt = m->s_type;
398 } 398 }
399 } 399 }
400 if (namedmem != NULL) { 400 if (namedmem != NULL) {
401 if (m == NULL) { 401 if (m == NULL) {
402 DPRINTF(("%s: pop struct\n", __func__)); 402 DPRINTF(("%s: pop struct\n", __func__));
403 goto pop; 403 goto pop;
404 } 404 }
405 istk->i_mem = m; 405 istk->i_mem = m;
406 istk->i_subt = m->s_type; 406 istk->i_subt = m->s_type;
407 istk->i_namedmem = 1; 407 istk->i_namedmem = 1;
408 DPRINTF(("%s: named name=%s\n", __func__, 408 DPRINTF(("%s: named name=%s\n", __func__,
409 namedmem->n_name)); 409 namedmem->n_name));
410 pop_member(); 410 pop_member();
411 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1; 411 cnt = istk->i_type->t_tspec == STRUCT ? 2 : 1;
412 } 412 }
413 istk->i_brace = 1; 413 istk->i_brace = 1;
414 DPRINTF(("%s: unnamed type=%s, brace=%d\n", __func__, 414 DPRINTF(("%s: unnamed type=%s, brace=%d\n", __func__,
415 type_name(istk->i_type ? istk->i_type : istk->i_subt), 415 type_name(istk->i_type ? istk->i_type : istk->i_subt),
416 istk->i_brace)); 416 istk->i_brace));
417 if (cnt == 0) { 417 if (cnt == 0) {
418 /* cannot init. struct/union with no named member */ 418 /* cannot init. struct/union with no named member */
419 error(179); 419 error(179);
420 initerr = 1; 420 initerr = 1;
421 return; 421 return;
422 } 422 }
423 istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1; 423 istk->i_remaining = istk->i_type->t_tspec == STRUCT ? cnt : 1;
424 break; 424 break;
425 default: 425 default:
426 if (namedmem) { 426 if (namedmem) {
427 DPRINTF(("%s: pop\n", __func__)); 427 DPRINTF(("%s: pop\n", __func__));
428 pop: 428 pop:
429 inxt = initstk->i_next; 429 inxt = initstk->i_next;
430 free(istk); 430 free(istk);
431 initstk = inxt; 431 initstk = inxt;
432 goto again; 432 goto again;
433 } 433 }
434 istk->i_remaining = 1; 434 istk->i_remaining = 1;
435 break; 435 break;
436 } 436 }
437} 437}
438 438
439static void 439static void
440initstack_check_too_many(void) 440initstack_check_too_many(void)
441{ 441{
442 istk_t *istk; 442 istk_t *istk;
443 443
444 istk = initstk; 444 istk = initstk;
445 445
446 /* 446 /*
447 * If a closing brace is expected we have at least one initializer 447 * If a closing brace is expected we have at least one initializer
448 * too much. 448 * too much.
449 */ 449 */
450 if (istk->i_remaining == 0 && !istk->i_nolimit && !istk->i_namedmem) { 450 if (istk->i_remaining == 0 && !istk->i_nolimit && !istk->i_namedmem) {
451 switch (istk->i_type->t_tspec) { 451 switch (istk->i_type->t_tspec) {
452 case ARRAY: 452 case ARRAY:
453 /* too many array initializers, expected %d */ 453 /* too many array initializers, expected %d */
454 error(173, istk->i_type->t_dim); 454 error(173, istk->i_type->t_dim);
455 break; 455 break;
456 case STRUCT: 456 case STRUCT:
457 case UNION: 457 case UNION:
458 /* too many struct/union initializers */ 458 /* too many struct/union initializers */
459 error(172); 459 error(172);
460 break; 460 break;
461 default: 461 default:
462 /* too many initializers */ 462 /* too many initializers */
463 error(174); 463 error(174);
464 break; 464 break;
465 } 465 }
466 initerr = 1; 466 initerr = 1;
467 } 467 }
468} 468}
469 469
470static void 470static void
471initstack_next_brace(void) 471initstack_next_brace(void)
472{ 472{
473 473
474 DPRINTF(("%s\n", __func__)); 474 DPRINTF(("%s\n", __func__));
475 if (initstk->i_type != NULL && 475 if (initstk->i_type != NULL &&
476 tspec_is_scalar(initstk->i_type->t_tspec)) { 476 tspec_is_scalar(initstk->i_type->t_tspec)) {
477 /* invalid initializer type %s */ 477 /* invalid initializer type %s */
478 error(176, type_name(initstk->i_type)); 478 error(176, type_name(initstk->i_type));
479 initerr = 1; 479 initerr = 1;
480 } 480 }
481 if (!initerr) 481 if (!initerr)
482 initstack_check_too_many(); 482 initstack_check_too_many();
483 if (!initerr) 483 if (!initerr)
484 initstack_push(); 484 initstack_push();
485 if (!initerr) { 485 if (!initerr) {
486 initstk->i_brace = 1; 486 initstk->i_brace = 1;
487 DPRINTF(("%s: %p %s\n", __func__, namedmem, type_name( 487 DPRINTF(("%s: %p %s\n", __func__, namedmem, type_name(
488 initstk->i_type ? initstk->i_type : initstk->i_subt))); 488 initstk->i_type ? initstk->i_type : initstk->i_subt)));
489 } 489 }
490} 490}
491 491
492static void 492static void
493initstack_next_nobrace(void) 493initstack_next_nobrace(void)
494{ 494{
495 495
496 DPRINTF(("%s\n", __func__)); 496 DPRINTF(("%s\n", __func__));
497 if (initstk->i_type == NULL && 497 if (initstk->i_type == NULL &&
498 !tspec_is_scalar(initstk->i_subt->t_tspec)) { 498 !tspec_is_scalar(initstk->i_subt->t_tspec)) {
499 /* {}-enclosed initializer required */ 499 /* {}-enclosed initializer required */
500 error(181); 500 error(181);
501 } 501 }
502 502
503 /* Make sure an entry with a scalar type is at the top of the stack. */ 503 /* Make sure an entry with a scalar type is at the top of the stack. */
504 if (!initerr) 504 if (!initerr)
505 initstack_check_too_many(); 505 initstack_check_too_many();
506 while (!initerr) { 506 while (!initerr) {
507 if ((initstk->i_type != NULL && 507 if ((initstk->i_type != NULL &&
508 tspec_is_scalar(initstk->i_type->t_tspec))) 508 tspec_is_scalar(initstk->i_type->t_tspec)))
509 break; 509 break;
510 initstack_push(); 510 initstack_push();
511 } 511 }
512} 512}
513 513
514void 514void
515init_lbrace(void) 515init_lbrace(void)
516{ 516{
517 DPRINTF(("%s\n", __func__)); 517 DPRINTF(("%s\n", __func__));
518 518
519 if (initerr) 519 if (initerr)
520 return; 520 return;
521 521
522 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) && 522 if ((initsym->s_scl == AUTO || initsym->s_scl == REG) &&
523 initstk->i_next == NULL) { 523 initstk->i_next == NULL) {
524 if (tflag && !tspec_is_scalar(initstk->i_subt->t_tspec)) 524 if (tflag && !tspec_is_scalar(initstk->i_subt->t_tspec))
525 /* no automatic aggregate initialization in trad. C */ 525 /* no automatic aggregate initialization in trad. C */
526 warning(188); 526 warning(188);
527 } 527 }
528 528
529 /* 529 /*
530 * Remove all entries which cannot be used for further initializers 530 * Remove all entries which cannot be used for further initializers
531 * and do not expect a closing brace. 531 * and do not expect a closing brace.
532 */ 532 */
533 initstack_pop_nobrace(); 533 initstack_pop_nobrace();
534 534
535 initstack_next_brace(); 535 initstack_next_brace();
536} 536}
537 537
538void 538void
539init_rbrace(void) 539init_rbrace(void)
540{ 540{
541 DPRINTF(("%s\n", __func__)); 541 DPRINTF(("%s\n", __func__));
542 542
543 if (initerr) 543 if (initerr)
544 return; 544 return;
545 545
546 initstack_pop_brace(); 546 initstack_pop_brace();
547} 547}
548 548
549void 549void
550mkinit(tnode_t *tn) 550mkinit(tnode_t *tn)
551{ 551{
552 ptrdiff_t offs; 552 ptrdiff_t offs;
553 sym_t *sym; 553 sym_t *sym;
554 tspec_t lt, rt; 554 tspec_t lt, rt;
555 tnode_t *ln; 555 tnode_t *ln;
556 struct mbl *tmem; 556 struct mbl *tmem;
557 scl_t sc; 557 scl_t sc;
558#ifdef DEBUG 558#ifdef DEBUG
559 char sbuf[64]; 559 char sbuf[64];
560#endif 560#endif
561 561
562 DPRINTF(("%s: type=%s, value=%s\n", __func__, 562 DPRINTF(("%s: type=%s, value=%s\n", __func__,
563 type_name(tn->tn_type), 563 type_name(tn->tn_type),
564 print_tnode(sbuf, sizeof(sbuf), tn))); 564 print_tnode(sbuf, sizeof(sbuf), tn)));
565 named_member_dprint(); 565 named_member_dprint();
566 566
567 if (initerr || tn == NULL) 567 if (initerr || tn == NULL)
568 return; 568 return;
569 569
570 sc = initsym->s_scl; 570 sc = initsym->s_scl;
571 571
572 /* 572 /*
573 * Do not test for automatic aggregate initialisation. If the 573 * Do not test for automatic aggregate initialisation. If the
574 * initializer starts with a brace we have the warning already. 574 * initializer starts with a brace we have the warning already.
575 * If not, an error will be printed that the initializer must 575 * If not, an error will be printed that the initializer must
576 * be enclosed by braces. 576 * be enclosed by braces.
577 */ 577 */
578 578
579 /* 579 /*
580 * Local initialisation of non-array-types with only one expression 580 * Local initialisation of non-array-types with only one expression
581 * without braces is done by ASSIGN 581 * without braces is done by ASSIGN
582 */ 582 */
583 if ((sc == AUTO || sc == REG) && 583 if ((sc == AUTO || sc == REG) &&
584 initsym->s_type->t_tspec != ARRAY && initstk->i_next == NULL) { 584 initsym->s_type->t_tspec != ARRAY && initstk->i_next == NULL) {
585 ln = getnnode(initsym, 0); 585 ln = getnnode(initsym, 0);
586 ln->tn_type = tduptyp(ln->tn_type); 586 ln->tn_type = tduptyp(ln->tn_type);
587 ln->tn_type->t_const = 0; 587 ln->tn_type->t_const = 0;
588 tn = build(ASSIGN, ln, tn); 588 tn = build(ASSIGN, ln, tn);
589 expr(tn, 0, 0, 0); 589 expr(tn, 0, 0, 0);
590 return; 590 return;
591 } 591 }
592 592
593 /* 593 /*
594 * Remove all entries which cannot be used for further initializers 594 * Remove all entries which cannot be used for further initializers
595 * and do not require a closing brace. 595 * and do not require a closing brace.
596 */ 596 */
597 initstack_pop_nobrace(); 597 initstack_pop_nobrace();
598 598
599 /* Initialisations by strings are done in initstack_string(). */ 599 /* Initialisations by strings are done in initstack_string(). */
600 if (initstack_string(tn)) 600 if (initstack_string(tn))
601 return; 601 return;
602 602
603 initstack_next_nobrace(); 603 initstack_next_nobrace();
604 if (initerr || tn == NULL) 604 if (initerr || tn == NULL)
605 return; 605 return;
606 606
607 initstk->i_remaining--; 607 initstk->i_remaining--;
608 DPRINTF(("%s: remaining=%d tn=%p\n", __func__, 608 DPRINTF(("%s: remaining=%d tn=%p\n", __func__,
609 initstk->i_remaining, tn)); 609 initstk->i_remaining, tn));
610 /* Create a temporary node for the left side. */ 610 /* Create a temporary node for the left side. */
611 ln = tgetblk(sizeof (tnode_t)); 611 ln = tgetblk(sizeof (tnode_t));
612 ln->tn_op = NAME; 612 ln->tn_op = NAME;
613 ln->tn_type = tduptyp(initstk->i_type); 613 ln->tn_type = tduptyp(initstk->i_type);
614 ln->tn_type->t_const = 0; 614 ln->tn_type->t_const = 0;
615 ln->tn_lvalue = 1; 615 ln->tn_lvalue = 1;
616 ln->tn_sym = initsym; /* better than nothing */ 616 ln->tn_sym = initsym; /* better than nothing */
617 617
618 tn = cconv(tn); 618 tn = cconv(tn);
619 619
620 lt = ln->tn_type->t_tspec; 620 lt = ln->tn_type->t_tspec;
621 rt = tn->tn_type->t_tspec; 621 rt = tn->tn_type->t_tspec;
622 622
623 lint_assert(tspec_is_scalar(lt)); 623 lint_assert(tspec_is_scalar(lt));
624 624
625 if (!typeok(INIT, 0, ln, tn)) 625 if (!typeok(INIT, 0, ln, tn))
626 return; 626 return;
627 627
628 /* 628 /*
629 * Store the tree memory. This is necessary because otherwise 629 * Store the tree memory. This is necessary because otherwise
630 * expr() would free it. 630 * expr() would free it.
631 */ 631 */
632 tmem = tsave(); 632 tmem = tsave();
633 expr(tn, 1, 0, 1); 633 expr(tn, 1, 0, 1);
634 trestor(tmem); 634 trestor(tmem);
635 635
636 if (tspec_is_int(lt) && ln->tn_type->t_isfield && !tspec_is_int(rt)) { 636 if (tspec_is_int(lt) && ln->tn_type->t_bitfield && !tspec_is_int(rt)) {
637 /* 637 /*
638 * Bit-fields can be initialized in trad. C only by integer 638 * Bit-fields can be initialized in trad. C only by integer
639 * constants. 639 * constants.
640 */ 640 */
641 if (tflag) 641 if (tflag)
642 /* bit-field initialisation is illegal in trad. C */ 642 /* bit-field initialisation is illegal in trad. C */
643 warning(186); 643 warning(186);
644 } 644 }
645 645
646 if (lt != rt || (initstk->i_type->t_isfield && tn->tn_op == CON)) 646 if (lt != rt || (initstk->i_type->t_bitfield && tn->tn_op == CON))
647 tn = convert(INIT, 0, initstk->i_type, tn); 647 tn = convert(INIT, 0, initstk->i_type, tn);
648 648
649 if (tn != NULL && tn->tn_op != CON) { 649 if (tn != NULL && tn->tn_op != CON) {
650 sym = NULL; 650 sym = NULL;
651 offs = 0; 651 offs = 0;
652 if (conaddr(tn, &sym, &offs) == -1) { 652 if (conaddr(tn, &sym, &offs) == -1) {
653 if (sc == AUTO || sc == REG) { 653 if (sc == AUTO || sc == REG) {
654 /* non-constant initializer */ 654 /* non-constant initializer */
655 c99ism(177); 655 c99ism(177);
656 } else { 656 } else {
657 /* non-constant initializer */ 657 /* non-constant initializer */
658 error(177); 658 error(177);
659 } 659 }
660 } 660 }
661 } 661 }
662} 662}
663 663
664 664
665static int 665static int
666initstack_string(tnode_t *tn) 666initstack_string(tnode_t *tn)
667{ 667{
668 tspec_t t; 668 tspec_t t;
669 istk_t *istk; 669 istk_t *istk;
670 int len; 670 int len;
671 strg_t *strg; 671 strg_t *strg;
672 672
673 if (tn->tn_op != STRING) 673 if (tn->tn_op != STRING)
674 return 0; 674 return 0;
675 675
676 istk = initstk; 676 istk = initstk;
677 strg = tn->tn_string; 677 strg = tn->tn_string;
678 678
679 /* 679 /*
680 * Check if we have an array type which can be initialized by 680 * Check if we have an array type which can be initialized by
681 * the string. 681 * the string.
682 */ 682 */
683 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) { 683 if (istk->i_subt != NULL && istk->i_subt->t_tspec == ARRAY) {
684 DPRINTF(("%s: subt array\n", __func__)); 684 DPRINTF(("%s: subt array\n", __func__));
685 t = istk->i_subt->t_subt->t_tspec; 685 t = istk->i_subt->t_subt->t_tspec;
686 if (!((strg->st_tspec == CHAR && 686 if (!((strg->st_tspec == CHAR &&
687 (t == CHAR || t == UCHAR || t == SCHAR)) || 687 (t == CHAR || t == UCHAR || t == SCHAR)) ||
688 (strg->st_tspec == WCHAR && t == WCHAR))) { 688 (strg->st_tspec == WCHAR && t == WCHAR))) {
689 return 0; 689 return 0;
690 } 690 }
691 /* Put the array at top of stack */ 691 /* Put the array at top of stack */
692 initstack_push(); 692 initstack_push();
693 istk = initstk; 693 istk = initstk;
694 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) { 694 } else if (istk->i_type != NULL && istk->i_type->t_tspec == ARRAY) {
695 DPRINTF(("%s: type array\n", __func__)); 695 DPRINTF(("%s: type array\n", __func__));
696 t = istk->i_type->t_subt->t_tspec; 696 t = istk->i_type->t_subt->t_tspec;
697 if (!((strg->st_tspec == CHAR && 697 if (!((strg->st_tspec == CHAR &&
698 (t == CHAR || t == UCHAR || t == SCHAR)) || 698 (t == CHAR || t == UCHAR || t == SCHAR)) ||
699 (strg->st_tspec == WCHAR && t == WCHAR))) { 699 (strg->st_tspec == WCHAR && t == WCHAR))) {
700 return 0; 700 return 0;
701 } 701 }
702 /* 702 /*
703 * If the array is already partly initialized, we are 703 * If the array is already partly initialized, we are
704 * wrong here. 704 * wrong here.
705 */ 705 */
706 if (istk->i_remaining != istk->i_type->t_dim) 706 if (istk->i_remaining != istk->i_type->t_dim)
707 return 0; 707 return 0;
708 } else { 708 } else {
709 return 0; 709 return 0;
710 } 710 }
711 711
712 /* Get length without trailing NUL character. */ 712 /* Get length without trailing NUL character. */
713 len = strg->st_len; 713 len = strg->st_len;
714 714
715 if (istk->i_nolimit) { 715 if (istk->i_nolimit) {
716 istk->i_nolimit = 0; 716 istk->i_nolimit = 0;
717 istk->i_type->t_dim = len + 1; 717 istk->i_type->t_dim = len + 1;
718 setcomplete(istk->i_type, 1); 718 setcomplete(istk->i_type, 1);
719 } else { 719 } else {
720 if (istk->i_type->t_dim < len) { 720 if (istk->i_type->t_dim < len) {
721 /* non-null byte ignored in string initializer */ 721 /* non-null byte ignored in string initializer */
722 warning(187); 722 warning(187);
723 } 723 }
724 } 724 }
725 725
726 /* In every case the array is initialized completely. */ 726 /* In every case the array is initialized completely. */
727 istk->i_remaining = 0; 727 istk->i_remaining = 0;
728 728
729 return 1; 729 return 1;
730} 730}

cvs diff -r1.49 -r1.50 src/usr.bin/xlint/lint1/lint1.h (switch to unified diff)

--- src/usr.bin/xlint/lint1/lint1.h 2021/01/03 18:48:37 1.49
+++ src/usr.bin/xlint/lint1/lint1.h 2021/01/03 19:10:47 1.50
@@ -1,461 +1,461 @@ @@ -1,461 +1,461 @@
1/* $NetBSD: lint1.h,v 1.49 2021/01/03 18:48:37 rillig Exp $ */ 1/* $NetBSD: lint1.h,v 1.50 2021/01/03 19:10:47 rillig 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#include "lint.h" 35#include "lint.h"
36#include "err-msgs.h" 36#include "err-msgs.h"
37#include "op.h" 37#include "op.h"
38 38
39/* 39/*
40 * XXX - Super conservative so that works for most systems, but we should 40 * XXX - Super conservative so that works for most systems, but we should
41 * not depend on the host settings but the target settings in determining 41 * not depend on the host settings but the target settings in determining
42 * the alignment. The only valid use for this is in mem1.c; uses in decl.c 42 * the alignment. The only valid use for this is in mem1.c; uses in decl.c
43 * are bogus. 43 * are bogus.
44 */ 44 */
45#ifndef WORST_ALIGN 45#ifndef WORST_ALIGN
46#ifdef _LP64 46#ifdef _LP64
47# define AVAL 15 47# define AVAL 15
48#else 48#else
49# define AVAL 7 49# define AVAL 7
50#endif 50#endif
51#define WORST_ALIGN(x) (((x) + AVAL) & ~AVAL) 51#define WORST_ALIGN(x) (((x) + AVAL) & ~AVAL)
52#endif 52#endif
53 53
54#define LWARN_BAD -3 54#define LWARN_BAD -3
55#define LWARN_ALL -2 55#define LWARN_ALL -2
56#define LWARN_NONE -1 56#define LWARN_NONE -1
57 57
58/* 58/*
59 * Describes the position of a declaration or anything else. 59 * Describes the position of a declaration or anything else.
60 */ 60 */
61typedef struct { 61typedef struct {
62 int p_line; 62 int p_line;
63 const char *p_file; 63 const char *p_file;
64 int p_uniq; /* uniquifier */ 64 int p_uniq; /* uniquifier */
65} pos_t; 65} pos_t;
66 66
67/* Copies curr_pos, keeping things unique. */ 67/* Copies curr_pos, keeping things unique. */
68#define UNIQUE_CURR_POS(pos) \ 68#define UNIQUE_CURR_POS(pos) \
69 do { \ 69 do { \
70 (pos) = curr_pos; \ 70 (pos) = curr_pos; \
71 curr_pos.p_uniq++; \ 71 curr_pos.p_uniq++; \
72 if (curr_pos.p_file == csrc_pos.p_file) \ 72 if (curr_pos.p_file == csrc_pos.p_file) \
73 csrc_pos.p_uniq++; \ 73 csrc_pos.p_uniq++; \
74 } while (/*CONSTCOND*/0) 74 } while (/*CONSTCOND*/0)
75 75
76/* 76/*
77 * Strings cannot be referenced to simply by a pointer to its first 77 * Strings cannot be referenced to simply by a pointer to its first
78 * char. This is because strings can contain NUL characters other than the 78 * char. This is because strings can contain NUL characters other than the
79 * trailing NUL. 79 * trailing NUL.
80 * 80 *
81 * Strings are stored with a trailing NUL. 81 * Strings are stored with a trailing NUL.
82 */ 82 */
83typedef struct strg { 83typedef struct strg {
84 tspec_t st_tspec; /* CHAR or WCHAR */ 84 tspec_t st_tspec; /* CHAR or WCHAR */
85 size_t st_len; /* length without trailing NUL */ 85 size_t st_len; /* length without trailing NUL */
86 union { 86 union {
87 u_char *_st_cp; 87 u_char *_st_cp;
88 wchar_t *_st_wcp; 88 wchar_t *_st_wcp;
89 } st_u; 89 } st_u;
90} strg_t; 90} strg_t;
91 91
92#define st_cp st_u._st_cp 92#define st_cp st_u._st_cp
93#define st_wcp st_u._st_wcp 93#define st_wcp st_u._st_wcp
94 94
95/* 95/*
96 * qualifiers (only for lex/yacc interface) 96 * qualifiers (only for lex/yacc interface)
97 */ 97 */
98typedef enum { 98typedef enum {
99 CONST, VOLATILE, RESTRICT, THREAD 99 CONST, VOLATILE, RESTRICT, THREAD
100} tqual_t; 100} tqual_t;
101 101
102/* 102/*
103 * Integer and floating point values are stored in this structure 103 * Integer and floating point values are stored in this structure
104 */ 104 */
105typedef struct { 105typedef struct {
106 tspec_t v_tspec; 106 tspec_t v_tspec;
107 int v_ansiu; /* set if an integer constant is 107 int v_ansiu; /* set if an integer constant is
108 unsigned in ANSI C */ 108 unsigned in ANSI C */
109 union { 109 union {
110 int64_t _v_quad; /* integers */ 110 int64_t _v_quad; /* integers */
111 ldbl_t _v_ldbl; /* floats */ 111 ldbl_t _v_ldbl; /* floats */
112 } v_u; 112 } v_u;
113} val_t; 113} val_t;
114 114
115#define v_quad v_u._v_quad 115#define v_quad v_u._v_quad
116#define v_ldbl v_u._v_ldbl 116#define v_ldbl v_u._v_ldbl
117 117
118/* 118/*
119 * Structures of type str_t uniqely identify structures. This can't 119 * Structures of type str_t uniqely identify structures. This can't
120 * be done in structures of type type_t, because these are copied 120 * be done in structures of type type_t, because these are copied
121 * if they must be modified. So it would not be possible to check 121 * if they must be modified. So it would not be possible to check
122 * if two structures are identical by comparing the pointers to 122 * if two structures are identical by comparing the pointers to
123 * the type structures. 123 * the type structures.
124 * 124 *
125 * The typename is used if the structure is unnamed to identify 125 * The typename is used if the structure is unnamed to identify
126 * the structure type in pass 2. 126 * the structure type in pass 2.
127 */ 127 */
128typedef struct { 128typedef struct {
129 u_int size; /* size in bit */ 129 u_int size; /* size in bit */
130 u_int align : 15; /* alignment in bit */ 130 u_int align : 15; /* alignment in bit */
131 bool sincompl : 1; /* set if incomplete type */ 131 bool sincompl : 1; /* set if incomplete type */
132 struct sym *memb; /* list of members */ 132 struct sym *memb; /* list of members */
133 struct sym *stag; /* symbol table entry of tag */ 133 struct sym *stag; /* symbol table entry of tag */
134 struct sym *stdef; /* symbol table entry of first typename */ 134 struct sym *stdef; /* symbol table entry of first typename */
135} str_t; 135} str_t;
136 136
137/* 137/*
138 * same as above for enums 138 * same as above for enums
139 */ 139 */
140typedef struct { 140typedef struct {
141 bool eincompl : 1; /* incomplete enum type */ 141 bool eincompl : 1; /* incomplete enum type */
142 struct sym *elem; /* list of enumerators */ 142 struct sym *elem; /* list of enumerators */
143 struct sym *etag; /* symbol table entry of tag */ 143 struct sym *etag; /* symbol table entry of tag */
144 struct sym *etdef; /* symbol table entry of first typename */ 144 struct sym *etdef; /* symbol table entry of first typename */
145} tenum_t; 145} tenum_t;
146 146
147/* 147/*
148 * Types are represented by concatenation of structures of type type_t 148 * Types are represented by concatenation of structures of type type_t
149 * via t_subt. 149 * via t_subt.
150 */ 150 */
151struct type { 151struct type {
152 tspec_t t_tspec; /* type specifier */ 152 tspec_t t_tspec; /* type specifier */
153 bool t_aincompl : 1; /* incomplete array type */ 153 bool t_aincompl : 1; /* incomplete array type */
154 bool t_const : 1; /* const modifier */ 154 bool t_const : 1; /* const modifier */
155 bool t_volatile : 1; /* volatile modifier */ 155 bool t_volatile : 1; /* volatile modifier */
156 bool t_proto : 1; /* function prototype (t_args valid) */ 156 bool t_proto : 1; /* function prototype (t_args valid) */
157 bool t_vararg : 1; /* prototype with ... */ 157 bool t_vararg : 1; /* prototype with ... */
158 bool t_typedef : 1; /* type defined with typedef */ 158 bool t_typedef : 1; /* type defined with typedef */
159 bool t_isfield : 1; /* type is bitfield */ 159 bool t_bitfield : 1;
160 bool t_isenum : 1; /* type is (or was) enum (t_enum valid) */ 160 bool t_isenum : 1; /* type is (or was) enum (t_enum valid) */
161 bool t_ispacked : 1; /* type is packed */ 161 bool t_ispacked : 1; /* type is packed */
162 union { 162 union {
163 int _t_dim; /* dimension */ 163 int _t_dim; /* dimension */
164 str_t *_t_str; /* struct/union tag */ 164 str_t *_t_str; /* struct/union tag */
165 tenum_t *_t_enum; /* enum tag */ 165 tenum_t *_t_enum; /* enum tag */
166 struct sym *_t_args; /* arguments (if t_proto) */ 166 struct sym *_t_args; /* arguments (if t_proto) */
167 } t_u; 167 } t_u;
168 struct { 168 struct {
169 u_int _t_flen : 8; /* length of bit-field */ 169 u_int _t_flen : 8; /* length of bit-field */
170 u_int _t_foffs : 24; /* offset of bit-field */ 170 u_int _t_foffs : 24; /* offset of bit-field */
171 } t_b; 171 } t_b;
172 struct type *t_subt; /* element type (arrays), return value 172 struct type *t_subt; /* element type (arrays), return value
173 (functions), or type pointer points to */ 173 (functions), or type pointer points to */
174}; 174};
175 175
176#define t_dim t_u._t_dim 176#define t_dim t_u._t_dim
177#define t_str t_u._t_str 177#define t_str t_u._t_str
178#define t_enum t_u._t_enum 178#define t_enum t_u._t_enum
179#define t_args t_u._t_args 179#define t_args t_u._t_args
180#define t_flen t_b._t_flen 180#define t_flen t_b._t_flen
181#define t_foffs t_b._t_foffs 181#define t_foffs t_b._t_foffs
182 182
183/* 183/*
184 * types of symbols 184 * types of symbols
185 */ 185 */
186typedef enum { 186typedef enum {
187 FVFT, /* variables, functions, type names, enums */ 187 FVFT, /* variables, functions, type names, enums */
188 FMEMBER, /* members of structs or unions */ 188 FMEMBER, /* members of structs or unions */
189 FTAG, /* tags */ 189 FTAG, /* tags */
190 FLABEL /* labels */ 190 FLABEL /* labels */
191} symt_t; 191} symt_t;
192 192
193/* 193/*
194 * storage classes 194 * storage classes
195 */ 195 */
196typedef enum { 196typedef enum {
197 NOSCL, 197 NOSCL,
198 EXTERN, /* external symbols (indep. of decl_t) */ 198 EXTERN, /* external symbols (indep. of decl_t) */
199 STATIC, /* static symbols (local and global) */ 199 STATIC, /* static symbols (local and global) */
200 AUTO, /* automatic symbols (except register) */ 200 AUTO, /* automatic symbols (except register) */
201 REG, /* register */ 201 REG, /* register */
202 TYPEDEF, /* typedef */ 202 TYPEDEF, /* typedef */
203 STRTAG, 203 STRTAG,
204 UNIONTAG, 204 UNIONTAG,
205 ENUMTAG, 205 ENUMTAG,
206 MOS, /* member of struct */ 206 MOS, /* member of struct */
207 MOU, /* member of union */ 207 MOU, /* member of union */
208 ENUMCON, /* enumerator, enum constant */ 208 ENUMCON, /* enumerator, enum constant */
209 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */ 209 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed argument) */
210 ARG, /* argument */ 210 ARG, /* argument */
211 PARG, /* used in declaration stack during prototype 211 PARG, /* used in declaration stack during prototype
212 declaration */ 212 declaration */
213 INLINE /* only used by the parser */ 213 INLINE /* only used by the parser */
214} scl_t; 214} scl_t;
215 215
216/* 216/*
217 * symbol table entry 217 * symbol table entry
218 */ 218 */
219typedef struct sym { 219typedef struct sym {
220 const char *s_name; 220 const char *s_name;
221 const char *s_rename; /* renamed symbol's given name */ 221 const char *s_rename; /* renamed symbol's given name */
222 pos_t s_def_pos; /* position of last (prototype) definition, 222 pos_t s_def_pos; /* position of last (prototype) definition,
223 prototype declaration, no-prototype-def., 223 prototype declaration, no-prototype-def.,
224 tentative definition or declaration, 224 tentative definition or declaration,
225 in this order */ 225 in this order */
226 pos_t s_set_pos; /* position of first initialisation */ 226 pos_t s_set_pos; /* position of first initialisation */
227 pos_t s_use_pos; /* position of first use */ 227 pos_t s_use_pos; /* position of first use */
228 symt_t s_kind; /* type of symbol */ 228 symt_t s_kind; /* type of symbol */
229 void *s_keyword; 229 void *s_keyword;
230 bool s_bitfield : 1; 230 bool s_bitfield : 1;
231 bool s_set : 1; /* variable set, label defined */ 231 bool s_set : 1; /* variable set, label defined */
232 bool s_used : 1; /* variable/label used */ 232 bool s_used : 1; /* variable/label used */
233 bool s_arg : 1; /* symbol is function argument */ 233 bool s_arg : 1; /* symbol is function argument */
234 bool s_reg : 1; /* symbol is register variable */ 234 bool s_reg : 1; /* symbol is register variable */
235 bool s_defarg : 1; /* undefined symbol in old style function 235 bool s_defarg : 1; /* undefined symbol in old style function
236 definition */ 236 definition */
237 bool s_rimpl : 1; /* return value of function implicit decl. */ 237 bool s_rimpl : 1; /* return value of function implicit decl. */
238 bool s_osdef : 1; /* symbol stems from old style function def. */ 238 bool s_osdef : 1; /* symbol stems from old style function def. */
239 bool s_inline : 1; /* true if this is an inline function */ 239 bool s_inline : 1; /* true if this is an inline function */
240 struct sym *s_ext_sym; /* for local declared external symbols pointer 240 struct sym *s_ext_sym; /* for local declared external symbols pointer
241 to external symbol with same name */ 241 to external symbol with same name */
242 def_t s_def; /* declared, tentative defined, defined */ 242 def_t s_def; /* declared, tentative defined, defined */
243 scl_t s_scl; /* storage class */ 243 scl_t s_scl; /* storage class */
244 int s_blklev; /* level of declaration, -1 if not in symbol 244 int s_blklev; /* level of declaration, -1 if not in symbol
245 table */ 245 table */
246 type_t *s_type; 246 type_t *s_type;
247 val_t s_value; /* value (if enum constant) */ 247 val_t s_value; /* value (if enum constant) */
248 union { 248 union {
249 str_t *_s_st; /* tag, if it is a struct/union member */ 249 str_t *_s_st; /* tag, if it is a struct/union member */
250 tenum_t *_s_et; /* tag, if it is an enumerator */ 250 tenum_t *_s_et; /* tag, if it is an enumerator */
251 tspec_t _s_tsp; /* type (only for keywords) */ 251 tspec_t _s_tsp; /* type (only for keywords) */
252 tqual_t _s_tqu; /* qualifier (only for keywords) */ 252 tqual_t _s_tqu; /* qualifier (only for keywords) */
253 struct sym *_s_args; /* arguments in old style function 253 struct sym *_s_args; /* arguments in old style function
254 definitions */ 254 definitions */
255 } u; 255 } u;
256 struct sym *s_link; /* next symbol with same hash value */ 256 struct sym *s_link; /* next symbol with same hash value */
257 struct sym **s_rlink; /* pointer to s_link of prev. symbol */ 257 struct sym **s_rlink; /* pointer to s_link of prev. symbol */
258 struct sym *s_next; /* next struct/union member, enumerator, 258 struct sym *s_next; /* next struct/union member, enumerator,
259 argument */ 259 argument */
260 struct sym *s_dlnxt; /* next symbol declared on same level */ 260 struct sym *s_dlnxt; /* next symbol declared on same level */
261} sym_t; 261} sym_t;
262 262
263#define s_styp u._s_st 263#define s_styp u._s_st
264#define s_etyp u._s_et 264#define s_etyp u._s_et
265#define s_tspec u._s_tsp 265#define s_tspec u._s_tsp
266#define s_tqual u._s_tqu 266#define s_tqual u._s_tqu
267#define s_args u._s_args 267#define s_args u._s_args
268 268
269/* 269/*
270 * Used to keep some information about symbols before they are entered 270 * Used to keep some information about symbols before they are entered
271 * into the symbol table. 271 * into the symbol table.
272 */ 272 */
273typedef struct sbuf { 273typedef struct sbuf {
274 const char *sb_name; /* name of symbol */ 274 const char *sb_name; /* name of symbol */
275 size_t sb_len; /* length (without '\0') */ 275 size_t sb_len; /* length (without '\0') */
276 int sb_hash; /* hash value */ 276 int sb_hash; /* hash value */
277 sym_t *sb_sym; /* symbol table entry */ 277 sym_t *sb_sym; /* symbol table entry */
278 struct sbuf *sb_next; /* for freelist */ 278 struct sbuf *sb_next; /* for freelist */
279} sbuf_t; 279} sbuf_t;
280 280
281 281
282/* 282/*
283 * tree node 283 * tree node
284 */ 284 */
285typedef struct tnode { 285typedef struct tnode {
286 op_t tn_op; /* operator */ 286 op_t tn_op; /* operator */
287 type_t *tn_type; /* type */ 287 type_t *tn_type; /* type */
288 bool tn_lvalue : 1; /* node is lvalue */ 288 bool tn_lvalue : 1; /* node is lvalue */
289 bool tn_cast : 1; /* if tn_op == CVT, it's an explicit cast */ 289 bool tn_cast : 1; /* if tn_op == CVT, it's an explicit cast */
290 bool tn_parenthesized : 1; 290 bool tn_parenthesized : 1;
291 union { 291 union {
292 struct { 292 struct {
293 struct tnode *_tn_left; /* (left) operand */ 293 struct tnode *_tn_left; /* (left) operand */
294 struct tnode *_tn_right; /* right operand */ 294 struct tnode *_tn_right; /* right operand */
295 } tn_s; 295 } tn_s;
296 sym_t *_tn_sym; /* symbol if op == NAME */ 296 sym_t *_tn_sym; /* symbol if op == NAME */
297 val_t *_tn_val; /* value if op == CON */ 297 val_t *_tn_val; /* value if op == CON */
298 strg_t *_tn_string; /* string if op == STRING */ 298 strg_t *_tn_string; /* string if op == STRING */
299 } tn_u; 299 } tn_u;
300} tnode_t; 300} tnode_t;
301 301
302#define tn_left tn_u.tn_s._tn_left 302#define tn_left tn_u.tn_s._tn_left
303#define tn_right tn_u.tn_s._tn_right 303#define tn_right tn_u.tn_s._tn_right
304#define tn_sym tn_u._tn_sym 304#define tn_sym tn_u._tn_sym
305#define tn_val tn_u._tn_val 305#define tn_val tn_u._tn_val
306#define tn_string tn_u._tn_string 306#define tn_string tn_u._tn_string
307 307
308/* 308/*
309 * For nested declarations a stack exists, which holds all information 309 * For nested declarations a stack exists, which holds all information
310 * needed for the current level. dcs points to the top element of this 310 * needed for the current level. dcs points to the top element of this
311 * stack. 311 * stack.
312 * 312 *
313 * ctx describes the context of the current declaration. Its value is 313 * ctx describes the context of the current declaration. Its value is
314 * one of 314 * one of
315 * EXTERN global declarations 315 * EXTERN global declarations
316 * MOS oder MOU declarations of struct or union members 316 * MOS oder MOU declarations of struct or union members
317 * ENUMCON declarations of enums 317 * ENUMCON declarations of enums
318 * ARG declaration of arguments in old style function definitions 318 * ARG declaration of arguments in old style function definitions
319 * PARG declaration of arguments in function prototypes 319 * PARG declaration of arguments in function prototypes
320 * AUTO declaration of local symbols 320 * AUTO declaration of local symbols
321 * ABSTRACT abstract declarations (sizeof, casts) 321 * ABSTRACT abstract declarations (sizeof, casts)
322 * 322 *
323 */ 323 */
324typedef struct dinfo { 324typedef struct dinfo {
325 tspec_t d_atyp; /* VOID, CHAR, INT, or COMPLEX */ 325 tspec_t d_atyp; /* VOID, CHAR, INT, or COMPLEX */
326 tspec_t d_cmod; /* FLOAT, or DOUBLE */ 326 tspec_t d_cmod; /* FLOAT, or DOUBLE */
327 tspec_t d_smod; /* SIGNED or UNSIGN */ 327 tspec_t d_smod; /* SIGNED or UNSIGN */
328 tspec_t d_lmod; /* SHORT, LONG or QUAD */ 328 tspec_t d_lmod; /* SHORT, LONG or QUAD */
329 scl_t d_scl; /* storage class */ 329 scl_t d_scl; /* storage class */
330 type_t *d_type; /* after deftyp() pointer to the type used 330 type_t *d_type; /* after deftyp() pointer to the type used
331 for all declarators */ 331 for all declarators */
332 sym_t *d_rdcsym; /* redeclared symbol */ 332 sym_t *d_rdcsym; /* redeclared symbol */
333 int d_offset; /* offset of next structure member */ 333 int d_offset; /* offset of next structure member */
334 int d_stralign; /* alignment required for current structure */ 334 int d_stralign; /* alignment required for current structure */
335 scl_t d_ctx; /* context of declaration */ 335 scl_t d_ctx; /* context of declaration */
336 bool d_const : 1; /* const in declaration specifiers */ 336 bool d_const : 1; /* const in declaration specifiers */
337 bool d_volatile : 1; /* volatile in declaration specifiers */ 337 bool d_volatile : 1; /* volatile in declaration specifiers */
338 bool d_inline : 1; /* inline in declaration specifiers */ 338 bool d_inline : 1; /* inline in declaration specifiers */
339 bool d_mscl : 1; /* multiple storage classes */ 339 bool d_mscl : 1; /* multiple storage classes */
340 bool d_terr : 1; /* invalid type combination */ 340 bool d_terr : 1; /* invalid type combination */
341 bool d_nedecl : 1; /* if at least one tag is declared */ 341 bool d_nedecl : 1; /* if at least one tag is declared */
342 bool d_vararg : 1; /* ... in in current function decl. */ 342 bool d_vararg : 1; /* ... in in current function decl. */
343 bool d_proto : 1; /* current function decl. is prototype */ 343 bool d_proto : 1; /* current function decl. is prototype */
344 bool d_notyp : 1; /* set if no type specifier was present */ 344 bool d_notyp : 1; /* set if no type specifier was present */
345 bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */ 345 bool d_asm : 1; /* set if d_ctx == AUTO and asm() present */
346 bool d_ispacked : 1; /* packed */ 346 bool d_ispacked : 1; /* packed */
347 bool d_used : 1; /* used */ 347 bool d_used : 1; /* used */
348 type_t *d_tagtyp; /* tag during member declaration */ 348 type_t *d_tagtyp; /* tag during member declaration */
349 sym_t *d_fargs; /* list of arguments during function def. */ 349 sym_t *d_fargs; /* list of arguments during function def. */
350 pos_t d_fdpos; /* position of function definition */ 350 pos_t d_fdpos; /* position of function definition */
351 sym_t *d_dlsyms; /* first symbol declared at this level */ 351 sym_t *d_dlsyms; /* first symbol declared at this level */
352 sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl. 352 sym_t **d_ldlsym; /* points to s_dlnxt in last symbol decl.
353 at this level */ 353 at this level */
354 sym_t *d_fpsyms; /* symbols defined in prototype */ 354 sym_t *d_fpsyms; /* symbols defined in prototype */
355 struct dinfo *d_next; /* next level */ 355 struct dinfo *d_next; /* next level */
356} dinfo_t; 356} dinfo_t;
357 357
358/* 358/*
359 * Used to collect information about pointers and qualifiers in 359 * Used to collect information about pointers and qualifiers in
360 * declarators. 360 * declarators.
361 */ 361 */
362typedef struct pqinf { 362typedef struct pqinf {
363 int p_pcnt; /* number of asterisks */ 363 int p_pcnt; /* number of asterisks */
364 bool p_const : 1; 364 bool p_const : 1;
365 bool p_volatile : 1; 365 bool p_volatile : 1;
366 struct pqinf *p_next; 366 struct pqinf *p_next;
367} pqinf_t; 367} pqinf_t;
368 368
369/* 369/*
370 * Case values are stored in a list of type clst_t. 370 * Case values are stored in a list of type clst_t.
371 */ 371 */
372typedef struct clst { 372typedef struct clst {
373 val_t cl_val; 373 val_t cl_val;
374 struct clst *cl_next; 374 struct clst *cl_next;
375} clst_t; 375} clst_t;
376 376
377/* 377/*
378 * Used to keep information about nested control statements. 378 * Used to keep information about nested control statements.
379 */ 379 */
380typedef struct cstk { 380typedef struct cstk {
381 int c_env; /* type of statement (T_IF, ...) */ 381 int c_env; /* type of statement (T_IF, ...) */
382 bool c_loop : 1; /* continue && break are valid */ 382 bool c_loop : 1; /* continue && break are valid */
383 bool c_switch : 1; /* case && break are valid */ 383 bool c_switch : 1; /* case && break are valid */
384 bool c_break : 1; /* loop/switch has break */ 384 bool c_break : 1; /* loop/switch has break */
385 bool c_cont : 1; /* loop has continue */ 385 bool c_cont : 1; /* loop has continue */
386 bool c_default : 1; /* switch has default */ 386 bool c_default : 1; /* switch has default */
387 bool c_infinite : 1; /* break condition always false 387 bool c_infinite : 1; /* break condition always false
388 (for (;;), while (1)) */ 388 (for (;;), while (1)) */
389 bool c_rchif : 1; /* end of if-branch reached */ 389 bool c_rchif : 1; /* end of if-branch reached */
390 bool c_noretval : 1; /* had "return;" */ 390 bool c_noretval : 1; /* had "return;" */
391 bool c_retval : 1; /* had "return (e);" */ 391 bool c_retval : 1; /* had "return (e);" */
392 type_t *c_swtype; /* type of switch expression */ 392 type_t *c_swtype; /* type of switch expression */
393 clst_t *c_clst; /* list of case values */ 393 clst_t *c_clst; /* list of case values */
394 struct mbl *c_fexprm; /* saved memory for end of loop 394 struct mbl *c_fexprm; /* saved memory for end of loop
395 expression in for() */ 395 expression in for() */
396 tnode_t *c_f3expr; /* end of loop expr in for() */ 396 tnode_t *c_f3expr; /* end of loop expr in for() */
397 pos_t c_fpos; /* position of end of loop expr */ 397 pos_t c_fpos; /* position of end of loop expr */
398 pos_t c_cfpos; /* same for csrc_pos */ 398 pos_t c_cfpos; /* same for csrc_pos */
399 struct cstk *c_next; /* outer control statement */ 399 struct cstk *c_next; /* outer control statement */
400} cstk_t; 400} cstk_t;
401 401
402typedef struct { 402typedef struct {
403 size_t lo; 403 size_t lo;
404 size_t hi; 404 size_t hi;
405} range_t; 405} range_t;
406 406
407#include "externs1.h" 407#include "externs1.h"
408 408
409#define ERR_SETSIZE 1024 409#define ERR_SETSIZE 1024
410#define __NERRBITS (sizeof(unsigned int)) 410#define __NERRBITS (sizeof(unsigned int))
411 411
412typedef struct err_set { 412typedef struct err_set {
413 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS]; 413 unsigned int errs_bits[(ERR_SETSIZE + __NERRBITS-1) / __NERRBITS];
414} err_set; 414} err_set;
415 415
416#define ERR_SET(n, p) \ 416#define ERR_SET(n, p) \
417 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS))) 417 ((p)->errs_bits[(n)/__NERRBITS] |= (1 << ((n) % __NERRBITS)))
418#define ERR_CLR(n, p) \ 418#define ERR_CLR(n, p) \
419 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS))) 419 ((p)->errs_bits[(n)/__NERRBITS] &= ~(1 << ((n) % __NERRBITS)))
420#define ERR_ISSET(n, p) \ 420#define ERR_ISSET(n, p) \
421 ((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS))) 421 ((p)->errs_bits[(n)/__NERRBITS] & (1 << ((n) % __NERRBITS)))
422#define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p))) 422#define ERR_ZERO(p) (void)memset((p), 0, sizeof(*(p)))
423 423
424#define LERROR(fmt, args...) lerror(__FILE__, __LINE__, fmt, ##args) 424#define LERROR(fmt, args...) lerror(__FILE__, __LINE__, fmt, ##args)
425 425
426#define lint_assert(cond) \ 426#define lint_assert(cond) \
427 do { \ 427 do { \
428 if (!(cond)) \ 428 if (!(cond)) \
429 assert_failed(__FILE__, __LINE__, __func__, #cond); \ 429 assert_failed(__FILE__, __LINE__, __func__, #cond); \
430 } while (/*CONSTCOND*/0) 430 } while (/*CONSTCOND*/0)
431 431
432#ifdef BLKDEBUG 432#ifdef BLKDEBUG
433#define ZERO 0xa5 433#define ZERO 0xa5
434#else 434#else
435#define ZERO 0 435#define ZERO 0
436#endif 436#endif
437 437
438extern err_set msgset; 438extern err_set msgset;
439 439
440 440
441#ifdef DEBUG 441#ifdef DEBUG
442# include "err-msgs.h" 442# include "err-msgs.h"
443 443
444/* ARGSUSED */ 444/* ARGSUSED */
445static inline void __attribute__((format(printf, 1, 2))) 445static inline void __attribute__((format(printf, 1, 2)))
446check_printf(const char *fmt, ...) 446check_printf(const char *fmt, ...)
447{ 447{
448} 448}
449 449
450# define wrap_check_printf(func, id, args...) \ 450# define wrap_check_printf(func, id, args...) \
451 do { \ 451 do { \
452 check_printf(__CONCAT(MSG_, id), ##args); \ 452 check_printf(__CONCAT(MSG_, id), ##args); \
453 (func)(id, ##args); \ 453 (func)(id, ##args); \
454 } while (/*CONSTCOND*/0) 454 } while (/*CONSTCOND*/0)
455 455
456# define error(id, args...) wrap_check_printf(error, id, ##args) 456# define error(id, args...) wrap_check_printf(error, id, ##args)
457# define warning(id, args...) wrap_check_printf(warning, id, ##args) 457# define warning(id, args...) wrap_check_printf(warning, id, ##args)
458# define message(id, args...) wrap_check_printf(message, id, ##args) 458# define message(id, args...) wrap_check_printf(message, id, ##args)
459# define gnuism(id, args...) wrap_check_printf(gnuism, id, ##args) 459# define gnuism(id, args...) wrap_check_printf(gnuism, id, ##args)
460# define c99ism(id, args...) wrap_check_printf(c99ism, id, ##args) 460# define c99ism(id, args...) wrap_check_printf(c99ism, id, ##args)
461#endif 461#endif

cvs diff -r1.119 -r1.120 src/usr.bin/xlint/lint1/tree.c (switch to unified diff)

--- src/usr.bin/xlint/lint1/tree.c 2021/01/03 18:35:51 1.119
+++ src/usr.bin/xlint/lint1/tree.c 2021/01/03 19:10:47 1.120
@@ -1,4094 +1,4094 @@ @@ -1,4094 +1,4094 @@
1/* $NetBSD: tree.c,v 1.119 2021/01/03 18:35:51 rillig Exp $ */ 1/* $NetBSD: tree.c,v 1.120 2021/01/03 19:10:47 rillig Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1994, 1995 Jochen Pohl 4 * Copyright (c) 1994, 1995 Jochen Pohl
5 * All Rights Reserved. 5 * All Rights Reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software 15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement: 16 * must display the following acknowledgement:
17 * This product includes software developed by Jochen Pohl for 17 * This product includes software developed by Jochen Pohl for
18 * The NetBSD Project. 18 * The NetBSD Project.
19 * 4. The name of the author may not be used to endorse or promote products 19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission. 20 * derived from this software without specific prior written permission.
21 * 21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#if HAVE_NBTOOL_CONFIG_H 34#if HAVE_NBTOOL_CONFIG_H
35#include "nbtool_config.h" 35#include "nbtool_config.h"
36#endif 36#endif
37 37
38#include <sys/cdefs.h> 38#include <sys/cdefs.h>
39#if defined(__RCSID) && !defined(lint) 39#if defined(__RCSID) && !defined(lint)
40__RCSID("$NetBSD: tree.c,v 1.119 2021/01/03 18:35:51 rillig Exp $"); 40__RCSID("$NetBSD: tree.c,v 1.120 2021/01/03 19:10:47 rillig Exp $");
41#endif 41#endif
42 42
43#include <float.h> 43#include <float.h>
44#include <limits.h> 44#include <limits.h>
45#include <math.h> 45#include <math.h>
46#include <signal.h> 46#include <signal.h>
47#include <stdlib.h> 47#include <stdlib.h>
48#include <string.h> 48#include <string.h>
49 49
50#include "lint1.h" 50#include "lint1.h"
51#include "cgram.h" 51#include "cgram.h"
52 52
53static tnode_t *new_int_const_node(tspec_t, int64_t); 53static tnode_t *new_int_const_node(tspec_t, int64_t);
54static void check_pointer_comparison(op_t, tnode_t *, tnode_t *); 54static void check_pointer_comparison(op_t, tnode_t *, tnode_t *);
55static int check_assign_types_compatible(op_t, int, tnode_t *, tnode_t *); 55static int check_assign_types_compatible(op_t, int, tnode_t *, tnode_t *);
56static void check_bad_enum_operation(op_t, tnode_t *, tnode_t *); 56static void check_bad_enum_operation(op_t, tnode_t *, tnode_t *);
57static void check_enum_type_mismatch(op_t, int, tnode_t *, tnode_t *); 57static void check_enum_type_mismatch(op_t, int, tnode_t *, tnode_t *);
58static void check_enum_int_mismatch(op_t, int, tnode_t *, tnode_t *); 58static void check_enum_int_mismatch(op_t, int, tnode_t *, tnode_t *);
59static tnode_t *new_tnode(op_t, type_t *, tnode_t *, tnode_t *); 59static tnode_t *new_tnode(op_t, type_t *, tnode_t *, tnode_t *);
60static void balance(op_t, tnode_t **, tnode_t **); 60static void balance(op_t, tnode_t **, tnode_t **);
61static void warn_incompatible_types(op_t, tspec_t, tspec_t); 61static void warn_incompatible_types(op_t, tspec_t, tspec_t);
62static void warn_incompatible_pointers(mod_t *, type_t *, type_t *); 62static void warn_incompatible_pointers(mod_t *, type_t *, type_t *);
63static void merge_qualifiers(type_t **, type_t *, type_t *); 63static void merge_qualifiers(type_t **, type_t *, type_t *);
64static int has_constant_member(type_t *); 64static int has_constant_member(type_t *);
65static void check_prototype_conversion(int, tspec_t, tspec_t, type_t *, 65static void check_prototype_conversion(int, tspec_t, tspec_t, type_t *,
66 tnode_t *); 66 tnode_t *);
67static void check_integer_conversion(op_t, int, tspec_t, tspec_t, type_t *, 67static void check_integer_conversion(op_t, int, tspec_t, tspec_t, type_t *,
68 tnode_t *); 68 tnode_t *);
69static void check_pointer_integer_conversion(op_t, tspec_t, type_t *, 69static void check_pointer_integer_conversion(op_t, tspec_t, type_t *,
70 tnode_t *); 70 tnode_t *);
71static void check_pointer_conversion(op_t, tnode_t *, type_t *); 71static void check_pointer_conversion(op_t, tnode_t *, type_t *);
72static tnode_t *build_struct_access(op_t, tnode_t *, tnode_t *); 72static tnode_t *build_struct_access(op_t, tnode_t *, tnode_t *);
73static tnode_t *build_prepost_incdec(op_t, tnode_t *); 73static tnode_t *build_prepost_incdec(op_t, tnode_t *);
74static tnode_t *build_real_imag(op_t, tnode_t *); 74static tnode_t *build_real_imag(op_t, tnode_t *);
75static tnode_t *build_ampersand(tnode_t *, int); 75static tnode_t *build_ampersand(tnode_t *, int);
76static tnode_t *build_plus_minus(op_t, tnode_t *, tnode_t *); 76static tnode_t *build_plus_minus(op_t, tnode_t *, tnode_t *);
77static tnode_t *build_bit_shift(op_t, tnode_t *, tnode_t *); 77static tnode_t *build_bit_shift(op_t, tnode_t *, tnode_t *);
78static tnode_t *build_colon(tnode_t *, tnode_t *); 78static tnode_t *build_colon(tnode_t *, tnode_t *);
79static tnode_t *build_assignment(op_t, tnode_t *, tnode_t *); 79static tnode_t *build_assignment(op_t, tnode_t *, tnode_t *);
80static tnode_t *plength(type_t *); 80static tnode_t *plength(type_t *);
81static tnode_t *fold(tnode_t *); 81static tnode_t *fold(tnode_t *);
82static tnode_t *fold_test(tnode_t *); 82static tnode_t *fold_test(tnode_t *);
83static tnode_t *fold_float(tnode_t *); 83static tnode_t *fold_float(tnode_t *);
84static tnode_t *check_function_arguments(type_t *, tnode_t *); 84static tnode_t *check_function_arguments(type_t *, tnode_t *);
85static tnode_t *check_prototype_argument(int, type_t *, tnode_t *); 85static tnode_t *check_prototype_argument(int, type_t *, tnode_t *);
86static void check_null_effect(tnode_t *); 86static void check_null_effect(tnode_t *);
87static void display_expression(tnode_t *, int); 87static void display_expression(tnode_t *, int);
88static void check_array_index(tnode_t *, int); 88static void check_array_index(tnode_t *, int);
89static void check_integer_comparison(op_t, tnode_t *, tnode_t *); 89static void check_integer_comparison(op_t, tnode_t *, tnode_t *);
90static void check_precedence_confusion(tnode_t *); 90static void check_precedence_confusion(tnode_t *);
91 91
92extern sig_atomic_t fpe; 92extern sig_atomic_t fpe;
93 93
94#if 0 94#if 0
95static char * 95static char *
96dumpnode(char *buf, size_t len, tnode_t *tn) { 96dumpnode(char *buf, size_t len, tnode_t *tn) {
97 const char *n = getopname(tn->tn_op); 97 const char *n = getopname(tn->tn_op);
98 const char *s; 98 const char *s;
99 99
100 switch (tn->tn_op) { 100 switch (tn->tn_op) {
101 case NAME: 101 case NAME:
102 s = tn->tn_sym->s_name; 102 s = tn->tn_sym->s_name;
103 break; 103 break;
104 case CON: 104 case CON:
105 case STRING: 105 case STRING:
106 s = "*"; /* todo */ 106 s = "*"; /* todo */
107 break; 107 break;
108 default: 108 default:
109 s = NULL; 109 s = NULL;
110 break; 110 break;
111 } 111 }
112 char lb[1024]; 112 char lb[1024];
113 char rb[1024]; 113 char rb[1024];
114 114
115 if (s == NULL && tn->tn_left != NULL) 115 if (s == NULL && tn->tn_left != NULL)
116 dumpnode(lb, sizeof(lb), tn->tn_left); 116 dumpnode(lb, sizeof(lb), tn->tn_left);
117 else 117 else
118 strcpy(lb, "(null)"); 118 strcpy(lb, "(null)");
119 119
120 if (s == NULL && tn->tn_right != NULL) 120 if (s == NULL && tn->tn_right != NULL)
121 dumpnode(rb, sizeof(rb), tn->tn_right); 121 dumpnode(rb, sizeof(rb), tn->tn_right);
122 else 122 else
123 strcpy(rb, "(null)"); 123 strcpy(rb, "(null)");
124 124
125 125
126 snprintf(buf, len, "%s: (%s) = %s [%s, %s]", n, 126 snprintf(buf, len, "%s: (%s) = %s [%s, %s]", n,
127 type_name(tn->tn_type), s, lb, rb); 127 type_name(tn->tn_type), s, lb, rb);
128 return buf; 128 return buf;
129} 129}
130#endif 130#endif
131 131
132/* 132/*
133 * Increase degree of reference. 133 * Increase degree of reference.
134 * This is most often used to change type "T" in type "pointer to T". 134 * This is most often used to change type "T" in type "pointer to T".
135 */ 135 */
136type_t * 136type_t *
137incref(type_t *tp, tspec_t t) 137incref(type_t *tp, tspec_t t)
138{ 138{
139 type_t *tp2; 139 type_t *tp2;
140 140
141 tp2 = getblk(sizeof (type_t)); 141 tp2 = getblk(sizeof (type_t));
142 tp2->t_tspec = t; 142 tp2->t_tspec = t;
143 tp2->t_subt = tp; 143 tp2->t_subt = tp;
144 return tp2; 144 return tp2;
145} 145}
146 146
147/* 147/*
148 * same for use in expressions 148 * same for use in expressions
149 */ 149 */
150type_t * 150type_t *
151tincref(type_t *tp, tspec_t t) 151tincref(type_t *tp, tspec_t t)
152{ 152{
153 type_t *tp2; 153 type_t *tp2;
154 154
155 tp2 = tgetblk(sizeof (type_t)); 155 tp2 = tgetblk(sizeof (type_t));
156 tp2->t_tspec = t; 156 tp2->t_tspec = t;
157 tp2->t_subt = tp; 157 tp2->t_subt = tp;
158 return tp2; 158 return tp2;
159} 159}
160 160
161/* 161/*
162 * Create a node for a constant. 162 * Create a node for a constant.
163 */ 163 */
164tnode_t * 164tnode_t *
165getcnode(type_t *tp, val_t *v) 165getcnode(type_t *tp, val_t *v)
166{ 166{
167 tnode_t *n; 167 tnode_t *n;
168 168
169 n = getnode(); 169 n = getnode();
170 n->tn_op = CON; 170 n->tn_op = CON;
171 n->tn_type = tp; 171 n->tn_type = tp;
172 n->tn_val = tgetblk(sizeof (val_t)); 172 n->tn_val = tgetblk(sizeof (val_t));
173 n->tn_val->v_tspec = tp->t_tspec; 173 n->tn_val->v_tspec = tp->t_tspec;
174 n->tn_val->v_ansiu = v->v_ansiu; 174 n->tn_val->v_ansiu = v->v_ansiu;
175 n->tn_val->v_u = v->v_u; 175 n->tn_val->v_u = v->v_u;
176 free(v); 176 free(v);
177 return n; 177 return n;
178} 178}
179 179
180/* 180/*
181 * Create a node for a integer constant. 181 * Create a node for a integer constant.
182 */ 182 */
183static tnode_t * 183static tnode_t *
184new_int_const_node(tspec_t t, int64_t q) 184new_int_const_node(tspec_t t, int64_t q)
185{ 185{
186 tnode_t *n; 186 tnode_t *n;
187 187
188 n = getnode(); 188 n = getnode();
189 n->tn_op = CON; 189 n->tn_op = CON;
190 n->tn_type = gettyp(t); 190 n->tn_type = gettyp(t);
191 n->tn_val = tgetblk(sizeof (val_t)); 191 n->tn_val = tgetblk(sizeof (val_t));
192 n->tn_val->v_tspec = t; 192 n->tn_val->v_tspec = t;
193 n->tn_val->v_quad = q; 193 n->tn_val->v_quad = q;
194 return n; 194 return n;
195} 195}
196 196
197/* 197/*
198 * Create a node for a name (symbol table entry). 198 * Create a node for a name (symbol table entry).
199 * ntok is the token which follows the name. 199 * ntok is the token which follows the name.
200 */ 200 */
201tnode_t * 201tnode_t *
202getnnode(sym_t *sym, int ntok) 202getnnode(sym_t *sym, int ntok)
203{ 203{
204 tnode_t *n; 204 tnode_t *n;
205 205
206 if (sym->s_scl == NOSCL) { 206 if (sym->s_scl == NOSCL) {
207 sym->s_scl = EXTERN; 207 sym->s_scl = EXTERN;
208 sym->s_def = DECL; 208 sym->s_def = DECL;
209 if (ntok == T_LPAREN) { 209 if (ntok == T_LPAREN) {
210 if (sflag) { 210 if (sflag) {
211 /* function implicitly declared to ... */ 211 /* function implicitly declared to ... */
212 warning(215); 212 warning(215);
213 } 213 }
214 /* 214 /*
215 * XXX if tflag is set the symbol should be 215 * XXX if tflag is set the symbol should be
216 * exported to level 0 216 * exported to level 0
217 */ 217 */
218 sym->s_type = incref(sym->s_type, FUNC); 218 sym->s_type = incref(sym->s_type, FUNC);
219 } else { 219 } else {
220 if (!blklev) { 220 if (!blklev) {
221 /* %s undefined */ 221 /* %s undefined */
222 error(99, sym->s_name); 222 error(99, sym->s_name);
223 } else { 223 } else {
224 int fixtype; 224 int fixtype;
225 if (strcmp(sym->s_name, "__FUNCTION__") == 0 || 225 if (strcmp(sym->s_name, "__FUNCTION__") == 0 ||
226 strcmp(sym->s_name, "__PRETTY_FUNCTION__") 226 strcmp(sym->s_name, "__PRETTY_FUNCTION__")
227 == 0) { 227 == 0) {
228 /* __FUNCTION__/__PRETTY_FUNCTION... */ 228 /* __FUNCTION__/__PRETTY_FUNCTION... */
229 gnuism(316); 229 gnuism(316);
230 fixtype = 1; 230 fixtype = 1;
231 } else if (strcmp(sym->s_name, "__func__") == 0) { 231 } else if (strcmp(sym->s_name, "__func__") == 0) {
232 if (!Sflag) 232 if (!Sflag)
233 /* __func__ is a C9X feature */ 233 /* __func__ is a C9X feature */
234 warning(317); 234 warning(317);
235 fixtype = 1; 235 fixtype = 1;
236 } else { 236 } else {
237 /* %s undefined */ 237 /* %s undefined */
238 error(99, sym->s_name); 238 error(99, sym->s_name);
239 fixtype = 0; 239 fixtype = 0;
240 } 240 }
241 if (fixtype) { 241 if (fixtype) {
242 sym->s_type = incref(gettyp(CHAR), PTR); 242 sym->s_type = incref(gettyp(CHAR), PTR);
243 sym->s_type->t_const = 1; 243 sym->s_type->t_const = 1;
244 } 244 }
245 } 245 }
246 } 246 }
247 } 247 }
248 248
249 if (sym->s_kind != FVFT && sym->s_kind != FMEMBER) 249 if (sym->s_kind != FVFT && sym->s_kind != FMEMBER)
250 LERROR("getnnode(%d)", sym->s_kind); 250 LERROR("getnnode(%d)", sym->s_kind);
251 251
252 n = getnode(); 252 n = getnode();
253 n->tn_type = sym->s_type; 253 n->tn_type = sym->s_type;
254 if (sym->s_scl != ENUMCON) { 254 if (sym->s_scl != ENUMCON) {
255 n->tn_op = NAME; 255 n->tn_op = NAME;
256 n->tn_sym = sym; 256 n->tn_sym = sym;
257 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC) 257 if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
258 n->tn_lvalue = 1; 258 n->tn_lvalue = 1;
259 } else { 259 } else {
260 n->tn_op = CON; 260 n->tn_op = CON;
261 n->tn_val = tgetblk(sizeof (val_t)); 261 n->tn_val = tgetblk(sizeof (val_t));
262 *n->tn_val = sym->s_value; 262 *n->tn_val = sym->s_value;
263 } 263 }
264 264
265 return n; 265 return n;
266} 266}
267 267
268/* 268/*
269 * Create a node for a string. 269 * Create a node for a string.
270 */ 270 */
271tnode_t * 271tnode_t *
272getsnode(strg_t *strg) 272getsnode(strg_t *strg)
273{ 273{
274 size_t len; 274 size_t len;
275 tnode_t *n; 275 tnode_t *n;
276 276
277 len = strg->st_len; 277 len = strg->st_len;
278 278
279 n = getnode(); 279 n = getnode();
280 280
281 n->tn_op = STRING; 281 n->tn_op = STRING;
282 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY); 282 n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
283 n->tn_type->t_dim = len + 1; 283 n->tn_type->t_dim = len + 1;
284 n->tn_lvalue = 1; 284 n->tn_lvalue = 1;
285 285
286 n->tn_string = tgetblk(sizeof (strg_t)); 286 n->tn_string = tgetblk(sizeof (strg_t));
287 n->tn_string->st_tspec = strg->st_tspec; 287 n->tn_string->st_tspec = strg->st_tspec;
288 n->tn_string->st_len = len; 288 n->tn_string->st_len = len;
289 289
290 if (strg->st_tspec == CHAR) { 290 if (strg->st_tspec == CHAR) {
291 n->tn_string->st_cp = tgetblk(len + 1); 291 n->tn_string->st_cp = tgetblk(len + 1);
292 (void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1); 292 (void)memcpy(n->tn_string->st_cp, strg->st_cp, len + 1);
293 free(strg->st_cp); 293 free(strg->st_cp);
294 } else { 294 } else {
295 n->tn_string->st_wcp = tgetblk((len + 1) * sizeof (wchar_t)); 295 n->tn_string->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
296 (void)memcpy(n->tn_string->st_wcp, strg->st_wcp, 296 (void)memcpy(n->tn_string->st_wcp, strg->st_wcp,
297 (len + 1) * sizeof (wchar_t)); 297 (len + 1) * sizeof (wchar_t));
298 free(strg->st_wcp); 298 free(strg->st_wcp);
299 } 299 }
300 free(strg); 300 free(strg);
301 301
302 return n; 302 return n;
303} 303}
304 304
305/* 305/*
306 * Returns a symbol which has the same name as the msym argument and is a 306 * Returns a symbol which has the same name as the msym argument and is a
307 * member of the struct or union specified by the tn argument. 307 * member of the struct or union specified by the tn argument.
308 */ 308 */
309sym_t * 309sym_t *
310struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym) 310struct_or_union_member(tnode_t *tn, op_t op, sym_t *msym)
311{ 311{
312 str_t *str; 312 str_t *str;
313 type_t *tp; 313 type_t *tp;
314 sym_t *sym, *csym; 314 sym_t *sym, *csym;
315 int eq; 315 int eq;
316 tspec_t t; 316 tspec_t t;
317 317
318 /* 318 /*
319 * Remove the member if it was unknown until now (Which means 319 * Remove the member if it was unknown until now (Which means
320 * that no defined struct or union has a member with the same name). 320 * that no defined struct or union has a member with the same name).
321 */ 321 */
322 if (msym->s_scl == NOSCL) { 322 if (msym->s_scl == NOSCL) {
323 /* undefined struct/union member: %s */ 323 /* undefined struct/union member: %s */
324 error(101, msym->s_name); 324 error(101, msym->s_name);
325 rmsym(msym); 325 rmsym(msym);
326 msym->s_kind = FMEMBER; 326 msym->s_kind = FMEMBER;
327 msym->s_scl = MOS; 327 msym->s_scl = MOS;
328 msym->s_styp = tgetblk(sizeof (str_t)); 328 msym->s_styp = tgetblk(sizeof (str_t));
329 msym->s_styp->stag = tgetblk(sizeof (sym_t)); 329 msym->s_styp->stag = tgetblk(sizeof (sym_t));
330 msym->s_styp->stag->s_name = unnamed; 330 msym->s_styp->stag->s_name = unnamed;
331 msym->s_value.v_tspec = INT; 331 msym->s_value.v_tspec = INT;
332 return msym; 332 return msym;
333 } 333 }
334 334
335 /* Set str to the tag of which msym is expected to be a member. */ 335 /* Set str to the tag of which msym is expected to be a member. */
336 str = NULL; 336 str = NULL;
337 t = (tp = tn->tn_type)->t_tspec; 337 t = (tp = tn->tn_type)->t_tspec;
338 if (op == POINT) { 338 if (op == POINT) {
339 if (t == STRUCT || t == UNION) 339 if (t == STRUCT || t == UNION)
340 str = tp->t_str; 340 str = tp->t_str;
341 } else if (op == ARROW && t == PTR) { 341 } else if (op == ARROW && t == PTR) {
342 t = (tp = tp->t_subt)->t_tspec; 342 t = (tp = tp->t_subt)->t_tspec;
343 if (t == STRUCT || t == UNION) 343 if (t == STRUCT || t == UNION)
344 str = tp->t_str; 344 str = tp->t_str;
345 } 345 }
346 346
347 /* 347 /*
348 * If this struct/union has a member with the name of msym, return 348 * If this struct/union has a member with the name of msym, return
349 * return this it. 349 * return this it.
350 */ 350 */
351 if (str != NULL) { 351 if (str != NULL) {
352 for (sym = msym; sym != NULL; sym = sym->s_link) { 352 for (sym = msym; sym != NULL; sym = sym->s_link) {
353 if (sym->s_scl != MOS && sym->s_scl != MOU) 353 if (sym->s_scl != MOS && sym->s_scl != MOU)
354 continue; 354 continue;
355 if (sym->s_styp != str) 355 if (sym->s_styp != str)
356 continue; 356 continue;
357 if (strcmp(sym->s_name, msym->s_name) != 0) 357 if (strcmp(sym->s_name, msym->s_name) != 0)
358 continue; 358 continue;
359 return sym; 359 return sym;
360 } 360 }
361 } 361 }
362 362
363 /* 363 /*
364 * Set eq to 0 if there are struct/union members with the same name 364 * Set eq to 0 if there are struct/union members with the same name
365 * and different types and/or offsets. 365 * and different types and/or offsets.
366 */ 366 */
367 eq = 1; 367 eq = 1;
368 for (csym = msym; csym != NULL; csym = csym->s_link) { 368 for (csym = msym; csym != NULL; csym = csym->s_link) {
369 if (csym->s_scl != MOS && csym->s_scl != MOU) 369 if (csym->s_scl != MOS && csym->s_scl != MOU)
370 continue; 370 continue;
371 if (strcmp(msym->s_name, csym->s_name) != 0) 371 if (strcmp(msym->s_name, csym->s_name) != 0)
372 continue; 372 continue;
373 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) { 373 for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
374 int w; 374 int w;
375 375
376 if (sym->s_scl != MOS && sym->s_scl != MOU) 376 if (sym->s_scl != MOS && sym->s_scl != MOU)
377 continue; 377 continue;
378 if (strcmp(csym->s_name, sym->s_name) != 0) 378 if (strcmp(csym->s_name, sym->s_name) != 0)
379 continue; 379 continue;
380 if (csym->s_value.v_quad != sym->s_value.v_quad) { 380 if (csym->s_value.v_quad != sym->s_value.v_quad) {
381 eq = 0; 381 eq = 0;
382 break; 382 break;
383 } 383 }
384 w = 0; 384 w = 0;
385 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w; 385 eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
386 if (!eq) 386 if (!eq)
387 break; 387 break;
388 if (csym->s_bitfield != sym->s_bitfield) { 388 if (csym->s_bitfield != sym->s_bitfield) {
389 eq = 0; 389 eq = 0;
390 break; 390 break;
391 } 391 }
392 if (csym->s_bitfield) { 392 if (csym->s_bitfield) {
393 type_t *tp1, *tp2; 393 type_t *tp1, *tp2;
394 394
395 tp1 = csym->s_type; 395 tp1 = csym->s_type;
396 tp2 = sym->s_type; 396 tp2 = sym->s_type;
397 if (tp1->t_flen != tp2->t_flen) { 397 if (tp1->t_flen != tp2->t_flen) {
398 eq = 0; 398 eq = 0;
399 break; 399 break;
400 } 400 }
401 if (tp1->t_foffs != tp2->t_foffs) { 401 if (tp1->t_foffs != tp2->t_foffs) {
402 eq = 0; 402 eq = 0;
403 break; 403 break;
404 } 404 }
405 } 405 }
406 } 406 }
407 if (!eq) 407 if (!eq)
408 break; 408 break;
409 } 409 }
410 410
411 /* 411 /*
412 * Now handle the case in which the left operand refers really 412 * Now handle the case in which the left operand refers really
413 * to a struct/union, but the right operand is not member of it. 413 * to a struct/union, but the right operand is not member of it.
414 */ 414 */
415 if (str != NULL) { 415 if (str != NULL) {
416 if (eq && tflag) { 416 if (eq && tflag) {
417 /* illegal member use: %s */ 417 /* illegal member use: %s */
418 warning(102, msym->s_name); 418 warning(102, msym->s_name);
419 } else { 419 } else {
420 /* illegal member use: %s */ 420 /* illegal member use: %s */
421 error(102, msym->s_name); 421 error(102, msym->s_name);
422 } 422 }
423 return msym; 423 return msym;
424 } 424 }
425 425
426 /* 426 /*
427 * Now the left operand of ARROW does not point to a struct/union 427 * Now the left operand of ARROW does not point to a struct/union
428 * or the left operand of POINT is no struct/union. 428 * or the left operand of POINT is no struct/union.
429 */ 429 */
430 if (eq) { 430 if (eq) {
431 if (op == POINT) { 431 if (op == POINT) {
432 if (tflag) { 432 if (tflag) {
433 /* left operand of '.' must be struct/... */ 433 /* left operand of '.' must be struct/... */
434 warning(103); 434 warning(103);
435 } else { 435 } else {
436 /* left operand of '.' must be struct/... */ 436 /* left operand of '.' must be struct/... */
437 error(103); 437 error(103);
438 } 438 }
439 } else { 439 } else {
440 /* left operand of "->" must be pointer to ... */ 440 /* left operand of "->" must be pointer to ... */
441 if (tflag && tn->tn_type->t_tspec == PTR) { 441 if (tflag && tn->tn_type->t_tspec == PTR) {
442 /* left operand of '->' must be pointer ... */ 442 /* left operand of '->' must be pointer ... */
443 warning(104, type_name(tn->tn_type)); 443 warning(104, type_name(tn->tn_type));
444 } else { 444 } else {
445 /* left operand of '->' must be pointer ... */ 445 /* left operand of '->' must be pointer ... */
446 error(104, type_name(tn->tn_type)); 446 error(104, type_name(tn->tn_type));
447 } 447 }
448 } 448 }
449 } else { 449 } else {
450 if (tflag) { 450 if (tflag) {
451 /* non-unique member requires struct/union %s */ 451 /* non-unique member requires struct/union %s */
452 error(105, op == POINT ? "object" : "pointer"); 452 error(105, op == POINT ? "object" : "pointer");
453 } else { 453 } else {
454 /* unacceptable operand of '%s' */ 454 /* unacceptable operand of '%s' */
455 error(111, modtab[op].m_name); 455 error(111, modtab[op].m_name);
456 } 456 }
457 } 457 }
458 458
459 return msym; 459 return msym;
460} 460}
461 461
462/* 462/*
463 * Create a tree node. Called for most operands except function calls, 463 * Create a tree node. Called for most operands except function calls,
464 * sizeof and casts. 464 * sizeof and casts.
465 * 465 *
466 * op operator 466 * op operator
467 * ln left operand 467 * ln left operand
468 * rn if not NULL, right operand 468 * rn if not NULL, right operand
469 */ 469 */
470tnode_t * 470tnode_t *
471build(op_t op, tnode_t *ln, tnode_t *rn) 471build(op_t op, tnode_t *ln, tnode_t *rn)
472{ 472{
473 mod_t *mp; 473 mod_t *mp;
474 tnode_t *ntn; 474 tnode_t *ntn;
475 type_t *rtp; 475 type_t *rtp;
476 476
477 mp = &modtab[op]; 477 mp = &modtab[op];
478 478
479 /* If there was an error in one of the operands, return. */ 479 /* If there was an error in one of the operands, return. */
480 if (ln == NULL || (mp->m_binary && rn == NULL)) 480 if (ln == NULL || (mp->m_binary && rn == NULL))
481 return NULL; 481 return NULL;
482 482
483 /* 483 /*
484 * Apply class conversions to the left operand, but only if its 484 * Apply class conversions to the left operand, but only if its
485 * value is needed or it is compared with null. 485 * value is needed or it is compared with null.
486 */ 486 */
487 if (mp->m_vctx || mp->m_tctx) 487 if (mp->m_vctx || mp->m_tctx)
488 ln = cconv(ln); 488 ln = cconv(ln);
489 /* 489 /*
490 * The right operand is almost always in a test or value context, 490 * The right operand is almost always in a test or value context,
491 * except if it is a struct or union member. 491 * except if it is a struct or union member.
492 */ 492 */
493 if (mp->m_binary && op != ARROW && op != POINT) 493 if (mp->m_binary && op != ARROW && op != POINT)
494 rn = cconv(rn); 494 rn = cconv(rn);
495 495
496 /* 496 /*
497 * Print some warnings for comparisons of unsigned values with 497 * Print some warnings for comparisons of unsigned values with
498 * constants lower than or equal to null. This must be done 498 * constants lower than or equal to null. This must be done
499 * before promote() because otherwise unsigned char and unsigned 499 * before promote() because otherwise unsigned char and unsigned
500 * short would be promoted to int. Also types are tested to be 500 * short would be promoted to int. Also types are tested to be
501 * CHAR, which would also become int. 501 * CHAR, which would also become int.
502 */ 502 */
503 if (mp->m_comp) 503 if (mp->m_comp)
504 check_integer_comparison(op, ln, rn); 504 check_integer_comparison(op, ln, rn);
505 505
506 /* 506 /*
507 * Promote the left operand if it is in a test or value context 507 * Promote the left operand if it is in a test or value context
508 */ 508 */
509 if (mp->m_vctx || mp->m_tctx) 509 if (mp->m_vctx || mp->m_tctx)
510 ln = promote(op, 0, ln); 510 ln = promote(op, 0, ln);
511 /* 511 /*
512 * Promote the right operand, but only if it is no struct or 512 * Promote the right operand, but only if it is no struct or
513 * union member, or if it is not to be assigned to the left operand 513 * union member, or if it is not to be assigned to the left operand
514 */ 514 */
515 if (mp->m_binary && op != ARROW && op != POINT && 515 if (mp->m_binary && op != ARROW && op != POINT &&
516 op != ASSIGN && op != RETURN) { 516 op != ASSIGN && op != RETURN) {
517 rn = promote(op, 0, rn); 517 rn = promote(op, 0, rn);
518 } 518 }
519 519
520 /* 520 /*
521 * If the result of the operation is different for signed or 521 * If the result of the operation is different for signed or
522 * unsigned operands and one of the operands is signed only in 522 * unsigned operands and one of the operands is signed only in
523 * ANSI C, print a warning. 523 * ANSI C, print a warning.
524 */ 524 */
525 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) { 525 if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
526 /* ANSI C treats constant as unsigned, op %s */ 526 /* ANSI C treats constant as unsigned, op %s */
527 warning(218, mp->m_name); 527 warning(218, mp->m_name);
528 ln->tn_val->v_ansiu = 0; 528 ln->tn_val->v_ansiu = 0;
529 } 529 }
530 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) { 530 if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
531 /* ANSI C treats constant as unsigned, op %s */ 531 /* ANSI C treats constant as unsigned, op %s */
532 warning(218, mp->m_name); 532 warning(218, mp->m_name);
533 rn->tn_val->v_ansiu = 0; 533 rn->tn_val->v_ansiu = 0;
534 } 534 }
535 535
536 /* Make sure both operands are of the same type */ 536 /* Make sure both operands are of the same type */
537 if (mp->m_balance || (tflag && (op == SHL || op == SHR))) 537 if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
538 balance(op, &ln, &rn); 538 balance(op, &ln, &rn);
539 539
540 /* 540 /*
541 * Check types for compatibility with the operation and mutual 541 * Check types for compatibility with the operation and mutual
542 * compatibility. Return if there are serious problems. 542 * compatibility. Return if there are serious problems.
543 */ 543 */
544 if (!typeok(op, 0, ln, rn)) 544 if (!typeok(op, 0, ln, rn))
545 return NULL; 545 return NULL;
546 546
547 /* And now create the node. */ 547 /* And now create the node. */
548 switch (op) { 548 switch (op) {
549 case POINT: 549 case POINT:
550 case ARROW: 550 case ARROW:
551 ntn = build_struct_access(op, ln, rn); 551 ntn = build_struct_access(op, ln, rn);
552 break; 552 break;
553 case INCAFT: 553 case INCAFT:
554 case DECAFT: 554 case DECAFT:
555 case INCBEF: 555 case INCBEF:
556 case DECBEF: 556 case DECBEF:
557 ntn = build_prepost_incdec(op, ln); 557 ntn = build_prepost_incdec(op, ln);
558 break; 558 break;
559 case AMPER: 559 case AMPER:
560 ntn = build_ampersand(ln, 0); 560 ntn = build_ampersand(ln, 0);
561 break; 561 break;
562 case STAR: 562 case STAR:
563 ntn = new_tnode(STAR, ln->tn_type->t_subt, ln, NULL); 563 ntn = new_tnode(STAR, ln->tn_type->t_subt, ln, NULL);
564 break; 564 break;
565 case PLUS: 565 case PLUS:
566 case MINUS: 566 case MINUS:
567 ntn = build_plus_minus(op, ln, rn); 567 ntn = build_plus_minus(op, ln, rn);
568 break; 568 break;
569 case SHL: 569 case SHL:
570 case SHR: 570 case SHR:
571 ntn = build_bit_shift(op, ln, rn); 571 ntn = build_bit_shift(op, ln, rn);
572 break; 572 break;
573 case COLON: 573 case COLON:
574 ntn = build_colon(ln, rn); 574 ntn = build_colon(ln, rn);
575 break; 575 break;
576 case ASSIGN: 576 case ASSIGN:
577 case MULASS: 577 case MULASS:
578 case DIVASS: 578 case DIVASS:
579 case MODASS: 579 case MODASS:
580 case ADDASS: 580 case ADDASS:
581 case SUBASS: 581 case SUBASS:
582 case SHLASS: 582 case SHLASS:
583 case SHRASS: 583 case SHRASS:
584 case ANDASS: 584 case ANDASS:
585 case XORASS: 585 case XORASS:
586 case ORASS: 586 case ORASS:
587 case RETURN: 587 case RETURN:
588 ntn = build_assignment(op, ln, rn); 588 ntn = build_assignment(op, ln, rn);
589 break; 589 break;
590 case COMMA: 590 case COMMA:
591 case QUEST: 591 case QUEST:
592 ntn = new_tnode(op, rn->tn_type, ln, rn); 592 ntn = new_tnode(op, rn->tn_type, ln, rn);
593 break; 593 break;
594 case REAL: 594 case REAL:
595 case IMAG: 595 case IMAG:
596 ntn = build_real_imag(op, ln); 596 ntn = build_real_imag(op, ln);
597 break; 597 break;
598 default: 598 default:
599 rtp = mp->m_logical ? gettyp(INT) : ln->tn_type; 599 rtp = mp->m_logical ? gettyp(INT) : ln->tn_type;
600 lint_assert(mp->m_binary || rn == NULL); 600 lint_assert(mp->m_binary || rn == NULL);
601 ntn = new_tnode(op, rtp, ln, rn); 601 ntn = new_tnode(op, rtp, ln, rn);
602 break; 602 break;
603 } 603 }
604 604
605 /* Return if an error occurred. */ 605 /* Return if an error occurred. */
606 if (ntn == NULL) 606 if (ntn == NULL)
607 return NULL; 607 return NULL;
608 608
609 /* Print a warning if precedence confusion is possible */ 609 /* Print a warning if precedence confusion is possible */
610 if (mp->m_tpconf) 610 if (mp->m_tpconf)
611 check_precedence_confusion(ntn); 611 check_precedence_confusion(ntn);
612 612
613 /* 613 /*
614 * Print a warning if one of the operands is in a context where 614 * Print a warning if one of the operands is in a context where
615 * it is compared with null and if this operand is a constant. 615 * it is compared with null and if this operand is a constant.
616 */ 616 */
617 if (mp->m_tctx) { 617 if (mp->m_tctx) {
618 if (ln->tn_op == CON || 618 if (ln->tn_op == CON ||
619 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) { 619 ((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
620 if (hflag && !constcond_flag) 620 if (hflag && !constcond_flag)
621 /* constant in conditional context */ 621 /* constant in conditional context */
622 warning(161); 622 warning(161);
623 } 623 }
624 } 624 }
625 625
626 /* Fold if the operator requires it */ 626 /* Fold if the operator requires it */
627 if (mp->m_fold) { 627 if (mp->m_fold) {
628 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) { 628 if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
629 if (mp->m_tctx) { 629 if (mp->m_tctx) {
630 ntn = fold_test(ntn); 630 ntn = fold_test(ntn);
631 } else if (tspec_is_float(ntn->tn_type->t_tspec)) { 631 } else if (tspec_is_float(ntn->tn_type->t_tspec)) {
632 ntn = fold_float(ntn); 632 ntn = fold_float(ntn);
633 } else { 633 } else {
634 ntn = fold(ntn); 634 ntn = fold(ntn);
635 } 635 }
636 } else if (op == QUEST && ln->tn_op == CON) { 636 } else if (op == QUEST && ln->tn_op == CON) {
637 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right; 637 ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
638 } 638 }
639 } 639 }
640 640
641 return ntn; 641 return ntn;
642} 642}
643 643
644/* 644/*
645 * Perform class conversions. 645 * Perform class conversions.
646 * 646 *
647 * Arrays of type T are converted into pointers to type T. 647 * Arrays of type T are converted into pointers to type T.
648 * Functions are converted to pointers to functions. 648 * Functions are converted to pointers to functions.
649 * Lvalues are converted to rvalues. 649 * Lvalues are converted to rvalues.
650 */ 650 */
651tnode_t * 651tnode_t *
652cconv(tnode_t *tn) 652cconv(tnode_t *tn)
653{ 653{
654 type_t *tp; 654 type_t *tp;
655 655
656 /* 656 /*
657 * Array-lvalue (array of type T) is converted into rvalue 657 * Array-lvalue (array of type T) is converted into rvalue
658 * (pointer to type T) 658 * (pointer to type T)
659 */ 659 */
660 if (tn->tn_type->t_tspec == ARRAY) { 660 if (tn->tn_type->t_tspec == ARRAY) {
661 if (!tn->tn_lvalue) { 661 if (!tn->tn_lvalue) {
662 /* XXX print correct operator */ 662 /* XXX print correct operator */
663 /* %soperand of '%s' must be lvalue */ 663 /* %soperand of '%s' must be lvalue */
664 gnuism(114, "", modtab[AMPER].m_name); 664 gnuism(114, "", modtab[AMPER].m_name);
665 } 665 }
666 tn = new_tnode(AMPER, tincref(tn->tn_type->t_subt, PTR), 666 tn = new_tnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
667 tn, NULL); 667 tn, NULL);
668 } 668 }
669 669
670 /* 670 /*
671 * Expression of type function (function with return value of type T) 671 * Expression of type function (function with return value of type T)
672 * in rvalue-expression (pointer to function with return value 672 * in rvalue-expression (pointer to function with return value
673 * of type T) 673 * of type T)
674 */ 674 */
675 if (tn->tn_type->t_tspec == FUNC) 675 if (tn->tn_type->t_tspec == FUNC)
676 tn = build_ampersand(tn, 1); 676 tn = build_ampersand(tn, 1);
677 677
678 /* lvalue to rvalue */ 678 /* lvalue to rvalue */
679 if (tn->tn_lvalue) { 679 if (tn->tn_lvalue) {
680 tp = tduptyp(tn->tn_type); 680 tp = tduptyp(tn->tn_type);
681 tp->t_const = tp->t_volatile = 0; 681 tp->t_const = tp->t_volatile = 0;
682 tn = new_tnode(LOAD, tp, tn, NULL); 682 tn = new_tnode(LOAD, tp, tn, NULL);
683 } 683 }
684 684
685 return tn; 685 return tn;
686} 686}
687 687
688/* 688/*
689 * Perform most type checks. First the types are checked using 689 * Perform most type checks. First the types are checked using
690 * the information from modtab[]. After that it is done by hand for 690 * the information from modtab[]. After that it is done by hand for
691 * more complicated operators and type combinations. 691 * more complicated operators and type combinations.
692 * 692 *
693 * If the types are ok, typeok() returns 1, otherwise 0. 693 * If the types are ok, typeok() returns 1, otherwise 0.
694 */ 694 */
695int 695int
696typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn) 696typeok(op_t op, int arg, tnode_t *ln, tnode_t *rn)
697{ 697{
698 mod_t *mp; 698 mod_t *mp;
699 tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC, olt = NOTSPEC, 699 tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC, olt = NOTSPEC,
700 ort = NOTSPEC; 700 ort = NOTSPEC;
701 type_t *ltp, *rtp = NULL, *lstp = NULL, *rstp = NULL; 701 type_t *ltp, *rtp = NULL, *lstp = NULL, *rstp = NULL;
702 tnode_t *tn; 702 tnode_t *tn;
703 703
704 mp = &modtab[op]; 704 mp = &modtab[op];
705 705
706 lint_assert((ltp = ln->tn_type) != NULL); 706 lint_assert((ltp = ln->tn_type) != NULL);
707 707
708 if ((lt = ltp->t_tspec) == PTR) 708 if ((lt = ltp->t_tspec) == PTR)
709 lst = (lstp = ltp->t_subt)->t_tspec; 709 lst = (lstp = ltp->t_subt)->t_tspec;
710 if (mp->m_binary) { 710 if (mp->m_binary) {
711 lint_assert((rtp = rn->tn_type) != NULL); 711 lint_assert((rtp = rn->tn_type) != NULL);
712 if ((rt = rtp->t_tspec) == PTR) 712 if ((rt = rtp->t_tspec) == PTR)
713 rst = (rstp = rtp->t_subt)->t_tspec; 713 rst = (rstp = rtp->t_subt)->t_tspec;
714 } 714 }
715 715
716 if (mp->m_requires_integer) { 716 if (mp->m_requires_integer) {
717 if (!tspec_is_int(lt) || (mp->m_binary && !tspec_is_int(rt))) { 717 if (!tspec_is_int(lt) || (mp->m_binary && !tspec_is_int(rt))) {
718 warn_incompatible_types(op, lt, rt); 718 warn_incompatible_types(op, lt, rt);
719 return 0; 719 return 0;
720 } 720 }
721 } else if (mp->m_requires_integer_or_complex) { 721 } else if (mp->m_requires_integer_or_complex) {
722 if ((!tspec_is_int(lt) && !tspec_is_complex(lt)) || 722 if ((!tspec_is_int(lt) && !tspec_is_complex(lt)) ||
723 (mp->m_binary && 723 (mp->m_binary &&
724 (!tspec_is_int(rt) && !tspec_is_complex(rt)))) { 724 (!tspec_is_int(rt) && !tspec_is_complex(rt)))) {
725 warn_incompatible_types(op, lt, rt); 725 warn_incompatible_types(op, lt, rt);
726 return 0; 726 return 0;
727 } 727 }
728 } else if (mp->m_requires_scalar) { 728 } else if (mp->m_requires_scalar) {
729 if (!tspec_is_scalar(lt) || 729 if (!tspec_is_scalar(lt) ||
730 (mp->m_binary && !tspec_is_scalar(rt))) { 730 (mp->m_binary && !tspec_is_scalar(rt))) {
731 warn_incompatible_types(op, lt, rt); 731 warn_incompatible_types(op, lt, rt);
732 return 0; 732 return 0;
733 } 733 }
734 } else if (mp->m_requires_arith) { 734 } else if (mp->m_requires_arith) {
735 if (!tspec_is_arith(lt) || 735 if (!tspec_is_arith(lt) ||
736 (mp->m_binary && !tspec_is_arith(rt))) { 736 (mp->m_binary && !tspec_is_arith(rt))) {
737 warn_incompatible_types(op, lt, rt); 737 warn_incompatible_types(op, lt, rt);
738 return 0; 738 return 0;
739 } 739 }
740 } 740 }
741 741
742 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) { 742 if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
743 /* 743 /*
744 * For these operations we need the types before promotion 744 * For these operations we need the types before promotion
745 * and balancing. 745 * and balancing.
746 */ 746 */
747 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) 747 for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left)
748 continue; 748 continue;
749 olt = tn->tn_type->t_tspec; 749 olt = tn->tn_type->t_tspec;
750 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) 750 for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left)
751 continue; 751 continue;
752 ort = tn->tn_type->t_tspec; 752 ort = tn->tn_type->t_tspec;
753 } 753 }
754 754
755 switch (op) { 755 switch (op) {
756 case POINT: 756 case POINT:
757 /* 757 /*
758 * Most errors required by ANSI C are reported in 758 * Most errors required by ANSI C are reported in
759 * struct_or_union_member(). 759 * struct_or_union_member().
760 * Here we only must check for totally wrong things. 760 * Here we only must check for totally wrong things.
761 */ 761 */
762 if (lt == FUNC || lt == VOID || ltp->t_isfield || 762 if (lt == FUNC || lt == VOID || ltp->t_bitfield ||
763 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) { 763 ((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
764 /* Without tflag we got already an error */ 764 /* Without tflag we got already an error */
765 if (tflag) 765 if (tflag)
766 /* unacceptable operand of '%s' */ 766 /* unacceptable operand of '%s' */
767 error(111, mp->m_name); 767 error(111, mp->m_name);
768 return 0; 768 return 0;
769 } 769 }
770 /* Now we have an object we can create a pointer to */ 770 /* Now we have an object we can create a pointer to */
771 break; 771 break;
772 case ARROW: 772 case ARROW:
773 if (lt != PTR && !(tflag && tspec_is_int(lt))) { 773 if (lt != PTR && !(tflag && tspec_is_int(lt))) {
774 /* Without tflag we got already an error */ 774 /* Without tflag we got already an error */
775 if (tflag) 775 if (tflag)
776 /* unacceptable operand of '%s' */ 776 /* unacceptable operand of '%s' */
777 error(111, mp->m_name); 777 error(111, mp->m_name);
778 return 0; 778 return 0;
779 } 779 }
780 break; 780 break;
781 case INCAFT: 781 case INCAFT:
782 case DECAFT: 782 case DECAFT:
783 case INCBEF: 783 case INCBEF:
784 case DECBEF: 784 case DECBEF:
785 /* operands have scalar types (checked above) */ 785 /* operands have scalar types (checked above) */
786 if (!ln->tn_lvalue) { 786 if (!ln->tn_lvalue) {
787 if (ln->tn_op == CVT && ln->tn_cast && 787 if (ln->tn_op == CVT && ln->tn_cast &&
788 ln->tn_left->tn_op == LOAD) { 788 ln->tn_left->tn_op == LOAD) {
789 if (ln->tn_type->t_tspec == PTR) 789 if (ln->tn_type->t_tspec == PTR)
790 break; 790 break;
791 /* a cast does not yield an lvalue */ 791 /* a cast does not yield an lvalue */
792 error(163); 792 error(163);
793 } 793 }
794 /* %soperand of '%s' must be lvalue */ 794 /* %soperand of '%s' must be lvalue */
795 error(114, "", mp->m_name); 795 error(114, "", mp->m_name);
796 return 0; 796 return 0;
797 } else if (ltp->t_const) { 797 } else if (ltp->t_const) {
798 if (!tflag) 798 if (!tflag)
799 /* %soperand of '%s' must be modifiable ... */ 799 /* %soperand of '%s' must be modifiable ... */
800 warning(115, "", mp->m_name); 800 warning(115, "", mp->m_name);
801 } 801 }
802 break; 802 break;
803 case AMPER: 803 case AMPER:
804 if (lt == ARRAY || lt == FUNC) { 804 if (lt == ARRAY || lt == FUNC) {
805 /* ok, a warning comes later (in build_ampersand()) */ 805 /* ok, a warning comes later (in build_ampersand()) */
806 } else if (!ln->tn_lvalue) { 806 } else if (!ln->tn_lvalue) {
807 if (ln->tn_op == CVT && ln->tn_cast && 807 if (ln->tn_op == CVT && ln->tn_cast &&
808 ln->tn_left->tn_op == LOAD) { 808 ln->tn_left->tn_op == LOAD) {
809 if (ln->tn_type->t_tspec == PTR) 809 if (ln->tn_type->t_tspec == PTR)
810 break; 810 break;
811 /* a cast does not yield an lvalue */ 811 /* a cast does not yield an lvalue */
812 error(163); 812 error(163);
813 } 813 }
814 /* %soperand of '%s' must be lvalue */ 814 /* %soperand of '%s' must be lvalue */
815 error(114, "", mp->m_name); 815 error(114, "", mp->m_name);
816 return 0; 816 return 0;
817 } else if (tspec_is_scalar(lt)) { 817 } else if (tspec_is_scalar(lt)) {
818 if (ltp->t_isfield) { 818 if (ltp->t_bitfield) {
819 /* cannot take address of bit-field */ 819 /* cannot take address of bit-field */
820 error(112); 820 error(112);
821 return 0; 821 return 0;
822 } 822 }
823 } else if (lt != STRUCT && lt != UNION) { 823 } else if (lt != STRUCT && lt != UNION) {
824 /* unacceptable operand of '%s' */ 824 /* unacceptable operand of '%s' */
825 error(111, mp->m_name); 825 error(111, mp->m_name);
826 return 0; 826 return 0;
827 } 827 }
828 if (ln->tn_op == NAME && ln->tn_sym->s_reg) { 828 if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
829 /* cannot take address of register %s */ 829 /* cannot take address of register %s */
830 error(113, ln->tn_sym->s_name); 830 error(113, ln->tn_sym->s_name);
831 return 0; 831 return 0;
832 } 832 }
833 break; 833 break;
834 case STAR: 834 case STAR:
835 /* until now there were no type checks for this operator */ 835 /* until now there were no type checks for this operator */
836 if (lt != PTR) { 836 if (lt != PTR) {
837 /* cannot dereference non-pointer type */ 837 /* cannot dereference non-pointer type */
838 error(96); 838 error(96);
839 return 0; 839 return 0;
840 } 840 }
841 break; 841 break;
842 case PLUS: 842 case PLUS:
843 /* operands have scalar types (checked above) */ 843 /* operands have scalar types (checked above) */
844 if ((lt == PTR && !tspec_is_int(rt)) || 844 if ((lt == PTR && !tspec_is_int(rt)) ||
845 (rt == PTR && !tspec_is_int(lt))) { 845 (rt == PTR && !tspec_is_int(lt))) {
846 warn_incompatible_types(op, lt, rt); 846 warn_incompatible_types(op, lt, rt);
847 return 0; 847 return 0;
848 } 848 }
849 break; 849 break;
850 case MINUS: 850 case MINUS:
851 /* operands have scalar types (checked above) */ 851 /* operands have scalar types (checked above) */
852 if (lt == PTR && (!tspec_is_int(rt) && rt != PTR)) { 852 if (lt == PTR && (!tspec_is_int(rt) && rt != PTR)) {
853 warn_incompatible_types(op, lt, rt); 853 warn_incompatible_types(op, lt, rt);
854 return 0; 854 return 0;
855 } else if (rt == PTR && lt != PTR) { 855 } else if (rt == PTR && lt != PTR) {
856 warn_incompatible_types(op, lt, rt); 856 warn_incompatible_types(op, lt, rt);
857 return 0; 857 return 0;
858 } 858 }
859 if (lt == PTR && rt == PTR) { 859 if (lt == PTR && rt == PTR) {
860 if (!eqtype(lstp, rstp, 1, 0, NULL)) { 860 if (!eqtype(lstp, rstp, 1, 0, NULL)) {
861 /* illegal pointer subtraction */ 861 /* illegal pointer subtraction */
862 error(116); 862 error(116);
863 } 863 }
864 } 864 }
865 break; 865 break;
866 case SHR: 866 case SHR:
867 /* operands have integer types (checked above) */ 867 /* operands have integer types (checked above) */
868 if (pflag && !tspec_is_uint(lt)) { 868 if (pflag && !tspec_is_uint(lt)) {
869 /* 869 /*
870 * The left operand is signed. This means that 870 * The left operand is signed. This means that
871 * the operation is (possibly) nonportable. 871 * the operation is (possibly) nonportable.
872 */ 872 */
873 if (ln->tn_op != CON) { 873 if (ln->tn_op != CON) {
874 /* bitop. on signed value poss. nonportable */ 874 /* bitop. on signed value poss. nonportable */
875 warning(117); 875 warning(117);
876 } else if (ln->tn_val->v_quad < 0) { 876 } else if (ln->tn_val->v_quad < 0) {
877 /* bitop. on signed value nonportable */ 877 /* bitop. on signed value nonportable */
878 warning(120); 878 warning(120);
879 } 879 }
880 } else if (!tflag && !sflag && 880 } else if (!tflag && !sflag &&
881 !tspec_is_uint(olt) && tspec_is_uint(ort)) { 881 !tspec_is_uint(olt) && tspec_is_uint(ort)) {
882 /* 882 /*
883 * The left operand would become unsigned in 883 * The left operand would become unsigned in
884 * traditional C. 884 * traditional C.
885 */ 885 */
886 if (hflag && 886 if (hflag &&
887 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 887 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
888 /* semantics of '%s' change in ANSI C; ... */ 888 /* semantics of '%s' change in ANSI C; ... */
889 warning(118, mp->m_name); 889 warning(118, mp->m_name);
890 } 890 }
891 } else if (!tflag && !sflag && 891 } else if (!tflag && !sflag &&
892 !tspec_is_uint(olt) && !tspec_is_uint(ort) && 892 !tspec_is_uint(olt) && !tspec_is_uint(ort) &&
893 psize(lt) < psize(rt)) { 893 psize(lt) < psize(rt)) {
894 /* 894 /*
895 * In traditional C the left operand would be extended, 895 * In traditional C the left operand would be extended,
896 * possibly with 1, and then shifted. 896 * possibly with 1, and then shifted.
897 */ 897 */
898 if (hflag && 898 if (hflag &&
899 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) { 899 (ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
900 /* semantics of '%s' change in ANSI C; ... */ 900 /* semantics of '%s' change in ANSI C; ... */
901 warning(118, mp->m_name); 901 warning(118, mp->m_name);
902 } 902 }
903 } 903 }
904 goto shift; 904 goto shift;
905 case SHL: 905 case SHL:
906 /* 906 /*
907 * ANSI C does not perform balancing for shift operations, 907 * ANSI C does not perform balancing for shift operations,
908 * but traditional C does. If the width of the right operand 908 * but traditional C does. If the width of the right operand
909 * is greater than the width of the left operand, than in 909 * is greater than the width of the left operand, than in
910 * traditional C the left operand would be extended to the 910 * traditional C the left operand would be extended to the
911 * width of the right operand. For SHL this may result in 911 * width of the right operand. For SHL this may result in
912 * different results. 912 * different results.
913 */ 913 */
914 if (psize(lt) < psize(rt)) { 914 if (psize(lt) < psize(rt)) {
915 /* 915 /*
916 * XXX If both operands are constant, make sure 916 * XXX If both operands are constant, make sure
917 * that there is really a difference between 917 * that there is really a difference between
918 * ANSI C and traditional C. 918 * ANSI C and traditional C.
919 */ 919 */
920 if (hflag) 920 if (hflag)
921 /* semantics of '%s' change in ANSI C; ... */ 921 /* semantics of '%s' change in ANSI C; ... */
922 warning(118, mp->m_name); 922 warning(118, mp->m_name);
923 } 923 }
924 shift: 924 shift:
925 if (rn->tn_op == CON) { 925 if (rn->tn_op == CON) {
926 if (!tspec_is_uint(rt) && rn->tn_val->v_quad < 0) { 926 if (!tspec_is_uint(rt) && rn->tn_val->v_quad < 0) {
927 /* negative shift */ 927 /* negative shift */
928 warning(121); 928 warning(121);
929 } else if ((uint64_t)rn->tn_val->v_quad == (uint64_t)size(lt)) { 929 } else if ((uint64_t)rn->tn_val->v_quad == (uint64_t)size(lt)) {
930 /* shift equal to size of object */ 930 /* shift equal to size of object */
931 warning(267); 931 warning(267);
932 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size(lt)) { 932 } else if ((uint64_t)rn->tn_val->v_quad > (uint64_t)size(lt)) {
933 /* shift greater than size of object */ 933 /* shift greater than size of object */
934 warning(122); 934 warning(122);
935 } 935 }
936 } 936 }
937 break; 937 break;
938 case EQ: 938 case EQ:
939 case NE: 939 case NE:
940 /* 940 /*
941 * Accept some things which are allowed with EQ and NE, 941 * Accept some things which are allowed with EQ and NE,
942 * but not with ordered comparisons. 942 * but not with ordered comparisons.
943 */ 943 */
944 if (lt == PTR && ((rt == PTR && rst == VOID) || 944 if (lt == PTR && ((rt == PTR && rst == VOID) ||
945 tspec_is_int(rt))) { 945 tspec_is_int(rt))) {
946 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 946 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
947 break; 947 break;
948 } 948 }
949 if (rt == PTR && ((lt == PTR && lst == VOID) || 949 if (rt == PTR && ((lt == PTR && lst == VOID) ||
950 tspec_is_int(lt))) { 950 tspec_is_int(lt))) {
951 if (ln->tn_op == CON && ln->tn_val->v_quad == 0) 951 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
952 break; 952 break;
953 } 953 }
954 /* FALLTHROUGH */ 954 /* FALLTHROUGH */
955 case LT: 955 case LT:
956 case GT: 956 case GT:
957 case LE: 957 case LE:
958 case GE: 958 case GE:
959 if ((lt == PTR || rt == PTR) && lt != rt) { 959 if ((lt == PTR || rt == PTR) && lt != rt) {
960 if (tspec_is_int(lt) || tspec_is_int(rt)) { 960 if (tspec_is_int(lt) || tspec_is_int(rt)) {
961 const char *lx = lt == PTR ? 961 const char *lx = lt == PTR ?
962 "pointer" : "integer"; 962 "pointer" : "integer";
963 const char *rx = rt == PTR ? 963 const char *rx = rt == PTR ?
964 "pointer" : "integer"; 964 "pointer" : "integer";
965 /* illegal combination of %s (%s) and ... */ 965 /* illegal combination of %s (%s) and ... */
966 warning(123, lx, type_name(ltp), 966 warning(123, lx, type_name(ltp),
967 rx, type_name(rtp), mp->m_name); 967 rx, type_name(rtp), mp->m_name);
968 } else { 968 } else {
969 warn_incompatible_types(op, lt, rt); 969 warn_incompatible_types(op, lt, rt);
970 return 0; 970 return 0;
971 } 971 }
972 } else if (lt == PTR && rt == PTR) { 972 } else if (lt == PTR && rt == PTR) {
973 check_pointer_comparison(op, ln, rn); 973 check_pointer_comparison(op, ln, rn);
974 } 974 }
975 break; 975 break;
976 case QUEST: 976 case QUEST:
977 if (!tspec_is_scalar(lt)) { 977 if (!tspec_is_scalar(lt)) {
978 /* first operand must have scalar type, op ? : */ 978 /* first operand must have scalar type, op ? : */
979 error(170); 979 error(170);
980 return 0; 980 return 0;
981 } 981 }
982 while (rn->tn_op == CVT) 982 while (rn->tn_op == CVT)
983 rn = rn->tn_left; 983 rn = rn->tn_left;
984 lint_assert(rn->tn_op == COLON); 984 lint_assert(rn->tn_op == COLON);
985 break; 985 break;
986 case COLON: 986 case COLON:
987 if (tspec_is_arith(lt) && tspec_is_arith(rt)) 987 if (tspec_is_arith(lt) && tspec_is_arith(rt))
988 break; 988 break;
989 989
990 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str) 990 if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
991 break; 991 break;
992 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str) 992 if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
993 break; 993 break;
994 994
995 /* combination of any pointer and 0, 0L or (void *)0 is ok */ 995 /* combination of any pointer and 0, 0L or (void *)0 is ok */
996 if (lt == PTR && ((rt == PTR && rst == VOID) || 996 if (lt == PTR && ((rt == PTR && rst == VOID) ||
997 tspec_is_int(rt))) { 997 tspec_is_int(rt))) {
998 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 998 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
999 break; 999 break;
1000 } 1000 }
1001 if (rt == PTR && ((lt == PTR && lst == VOID) || 1001 if (rt == PTR && ((lt == PTR && lst == VOID) ||
1002 tspec_is_int(lt))) { 1002 tspec_is_int(lt))) {
1003 if (ln->tn_op == CON && ln->tn_val->v_quad == 0) 1003 if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
1004 break; 1004 break;
1005 } 1005 }
1006 1006
1007 if ((lt == PTR && tspec_is_int(rt)) || 1007 if ((lt == PTR && tspec_is_int(rt)) ||
1008 (tspec_is_int(lt) && rt == PTR)) { 1008 (tspec_is_int(lt) && rt == PTR)) {
1009 const char *lx = lt == PTR ? "pointer" : "integer"; 1009 const char *lx = lt == PTR ? "pointer" : "integer";
1010 const char *rx = rt == PTR ? "pointer" : "integer"; 1010 const char *rx = rt == PTR ? "pointer" : "integer";
1011 /* illegal combination of %s (%s) and %s (%s), op %s */ 1011 /* illegal combination of %s (%s) and %s (%s), op %s */
1012 warning(123, lx, type_name(ltp), 1012 warning(123, lx, type_name(ltp),
1013 rx, type_name(rtp), mp->m_name); 1013 rx, type_name(rtp), mp->m_name);
1014 break; 1014 break;
1015 } 1015 }
1016 1016
1017 if (lt == VOID || rt == VOID) { 1017 if (lt == VOID || rt == VOID) {
1018 if (lt != VOID || rt != VOID) 1018 if (lt != VOID || rt != VOID)
1019 /* incompatible types in conditional */ 1019 /* incompatible types in conditional */
1020 warning(126); 1020 warning(126);
1021 break; 1021 break;
1022 } 1022 }
1023 1023
1024 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) || 1024 if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
1025 (lst == FUNC && rst == VOID))) { 1025 (lst == FUNC && rst == VOID))) {
1026 /* (void *)0 handled above */ 1026 /* (void *)0 handled above */
1027 if (sflag) 1027 if (sflag)
1028 /* ANSI C forbids conv. of %s to %s, op %s */ 1028 /* ANSI C forbids conv. of %s to %s, op %s */
1029 warning(305, "function pointer", "'void *'", 1029 warning(305, "function pointer", "'void *'",
1030 mp->m_name); 1030 mp->m_name);
1031 break; 1031 break;
1032 } 1032 }
1033 1033
1034 if (rt == PTR && lt == PTR) { 1034 if (rt == PTR && lt == PTR) {
1035 if (eqptrtype(lstp, rstp, 1)) 1035 if (eqptrtype(lstp, rstp, 1))
1036 break; 1036 break;
1037 if (!eqtype(lstp, rstp, 1, 0, NULL)) 1037 if (!eqtype(lstp, rstp, 1, 0, NULL))
1038 warn_incompatible_pointers(mp, ltp, rtp); 1038 warn_incompatible_pointers(mp, ltp, rtp);
1039 break; 1039 break;
1040 } 1040 }
1041 1041
1042 /* incompatible types in conditional */ 1042 /* incompatible types in conditional */
1043 error(126); 1043 error(126);
1044 return 0; 1044 return 0;
1045 1045
1046 case ASSIGN: 1046 case ASSIGN:
1047 case INIT: 1047 case INIT:
1048 case FARG: 1048 case FARG:
1049 case RETURN: 1049 case RETURN:
1050 if (!check_assign_types_compatible(op, arg, ln, rn)) 1050 if (!check_assign_types_compatible(op, arg, ln, rn))
1051 return 0; 1051 return 0;
1052 goto assign; 1052 goto assign;
1053 case MULASS: 1053 case MULASS:
1054 case DIVASS: 1054 case DIVASS:
1055 case MODASS: 1055 case MODASS:
1056 goto assign; 1056 goto assign;
1057 case ADDASS: 1057 case ADDASS:
1058 case SUBASS: 1058 case SUBASS:
1059 /* operands have scalar types (checked above) */ 1059 /* operands have scalar types (checked above) */
1060 if ((lt == PTR && !tspec_is_int(rt)) || rt == PTR) { 1060 if ((lt == PTR && !tspec_is_int(rt)) || rt == PTR) {
1061 warn_incompatible_types(op, lt, rt); 1061 warn_incompatible_types(op, lt, rt);
1062 return 0; 1062 return 0;
1063 } 1063 }
1064 goto assign; 1064 goto assign;
1065 case SHLASS: 1065 case SHLASS:
1066 goto assign; 1066 goto assign;
1067 case SHRASS: 1067 case SHRASS:
1068 if (pflag && !tspec_is_uint(lt) && 1068 if (pflag && !tspec_is_uint(lt) &&
1069 !(tflag && tspec_is_uint(rt))) { 1069 !(tflag && tspec_is_uint(rt))) {
1070 /* bitop. on signed value possibly nonportable */ 1070 /* bitop. on signed value possibly nonportable */
1071 warning(117); 1071 warning(117);
1072 } 1072 }
1073 goto assign; 1073 goto assign;
1074 case ANDASS: 1074 case ANDASS:
1075 case XORASS: 1075 case XORASS:
1076 case ORASS: 1076 case ORASS:
1077 goto assign; 1077 goto assign;
1078 assign: 1078 assign:
1079 if (!ln->tn_lvalue) { 1079 if (!ln->tn_lvalue) {
1080 if (ln->tn_op == CVT && ln->tn_cast && 1080 if (ln->tn_op == CVT && ln->tn_cast &&
1081 ln->tn_left->tn_op == LOAD) { 1081 ln->tn_left->tn_op == LOAD) {
1082 if (ln->tn_type->t_tspec == PTR) 1082 if (ln->tn_type->t_tspec == PTR)
1083 break; 1083 break;
1084 /* a cast does not yield an lvalue */ 1084 /* a cast does not yield an lvalue */
1085 error(163); 1085 error(163);
1086 } 1086 }
1087 /* %soperand of '%s' must be lvalue */ 1087 /* %soperand of '%s' must be lvalue */
1088 error(114, "left ", mp->m_name); 1088 error(114, "left ", mp->m_name);
1089 return 0; 1089 return 0;
1090 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) && 1090 } else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
1091 has_constant_member(ltp))) { 1091 has_constant_member(ltp))) {
1092 if (!tflag) 1092 if (!tflag)
1093 /* %soperand of '%s' must be modifiable lvalue */ 1093 /* %soperand of '%s' must be modifiable lvalue */
1094 warning(115, "left ", mp->m_name); 1094 warning(115, "left ", mp->m_name);
1095 } 1095 }
1096 break; 1096 break;
1097 case COMMA: 1097 case COMMA:
1098 if (!modtab[ln->tn_op].m_sideeff) 1098 if (!modtab[ln->tn_op].m_sideeff)
1099 check_null_effect(ln); 1099 check_null_effect(ln);
1100 break; 1100 break;
1101 /* LINTED206: (enumeration values not handled in switch) */ 1101 /* LINTED206: (enumeration values not handled in switch) */
1102 case CON: 1102 case CON:
1103 case CASE: 1103 case CASE:
1104 case PUSH: 1104 case PUSH:
1105 case LOAD: 1105 case LOAD:
1106 case ICALL: 1106 case ICALL:
1107 case CVT: 1107 case CVT:
1108 case CALL: 1108 case CALL:
1109 case FSEL: 1109 case FSEL:
1110 case STRING: 1110 case STRING:
1111 case NAME: 1111 case NAME:
1112 case LOGOR: 1112 case LOGOR:
1113 case LOGAND: 1113 case LOGAND:
1114 case OR: 1114 case OR:
1115 case XOR: 1115 case XOR:
1116 case AND: 1116 case AND:
1117 case MOD: 1117 case MOD:
1118 case DIV: 1118 case DIV:
1119 case MULT: 1119 case MULT:
1120 case UMINUS: 1120 case UMINUS:
1121 case UPLUS: 1121 case UPLUS:
1122 case DEC: 1122 case DEC:
1123 case INC: 1123 case INC:
1124 case COMPL: 1124 case COMPL:
1125 case NOT: 1125 case NOT:
1126 case NOOP: 1126 case NOOP:
1127 case REAL: 1127 case REAL:
1128 case IMAG: 1128 case IMAG:
1129 break; 1129 break;
1130 } 1130 }
1131 1131
1132 if (mp->m_bad_on_enum && 1132 if (mp->m_bad_on_enum &&
1133 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) { 1133 (ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
1134 check_bad_enum_operation(op, ln, rn); 1134 check_bad_enum_operation(op, ln, rn);
1135 } else if (mp->m_valid_on_enum && 1135 } else if (mp->m_valid_on_enum &&
1136 (ltp->t_isenum && rtp && rtp->t_isenum)) { 1136 (ltp->t_isenum && rtp && rtp->t_isenum)) {
1137 check_enum_type_mismatch(op, arg, ln, rn); 1137 check_enum_type_mismatch(op, arg, ln, rn);
1138 } else if (mp->m_valid_on_enum && 1138 } else if (mp->m_valid_on_enum &&
1139 (ltp->t_isenum || (rtp && rtp->t_isenum))) { 1139 (ltp->t_isenum || (rtp && rtp->t_isenum))) {
1140 check_enum_int_mismatch(op, arg, ln, rn); 1140 check_enum_int_mismatch(op, arg, ln, rn);
1141 } 1141 }
1142 1142
1143 return 1; 1143 return 1;
1144} 1144}
1145 1145
1146static void 1146static void
1147check_pointer_comparison(op_t op, tnode_t *ln, tnode_t *rn) 1147check_pointer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
1148{ 1148{
1149 type_t *ltp, *rtp; 1149 type_t *ltp, *rtp;
1150 tspec_t lt, rt; 1150 tspec_t lt, rt;
1151 const char *lts, *rts; 1151 const char *lts, *rts;
1152 1152
1153 lt = (ltp = ln->tn_type)->t_subt->t_tspec; 1153 lt = (ltp = ln->tn_type)->t_subt->t_tspec;
1154 rt = (rtp = rn->tn_type)->t_subt->t_tspec; 1154 rt = (rtp = rn->tn_type)->t_subt->t_tspec;
1155 1155
1156 if (lt == VOID || rt == VOID) { 1156 if (lt == VOID || rt == VOID) {
1157 if (sflag && (lt == FUNC || rt == FUNC)) { 1157 if (sflag && (lt == FUNC || rt == FUNC)) {
1158 /* (void *)0 already handled in typeok() */ 1158 /* (void *)0 already handled in typeok() */
1159 *(lt == FUNC ? &lts : &rts) = "function pointer"; 1159 *(lt == FUNC ? &lts : &rts) = "function pointer";
1160 *(lt == VOID ? &lts : &rts) = "'void *'"; 1160 *(lt == VOID ? &lts : &rts) = "'void *'";
1161 /* ANSI C forbids comparison of %s with %s */ 1161 /* ANSI C forbids comparison of %s with %s */
1162 warning(274, lts, rts); 1162 warning(274, lts, rts);
1163 } 1163 }
1164 return; 1164 return;
1165 } 1165 }
1166 1166
1167 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) { 1167 if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
1168 warn_incompatible_pointers(&modtab[op], ltp, rtp); 1168 warn_incompatible_pointers(&modtab[op], ltp, rtp);
1169 return; 1169 return;
1170 } 1170 }
1171 1171
1172 if (lt == FUNC && rt == FUNC) { 1172 if (lt == FUNC && rt == FUNC) {
1173 if (sflag && op != EQ && op != NE) 1173 if (sflag && op != EQ && op != NE)
1174 /* ANSI C forbids ordered comparisons of ... */ 1174 /* ANSI C forbids ordered comparisons of ... */
1175 warning(125); 1175 warning(125);
1176 } 1176 }
1177} 1177}
1178 1178
1179/* 1179/*
1180 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN 1180 * Checks type compatibility for ASSIGN, INIT, FARG and RETURN
1181 * and prints warnings/errors if necessary. 1181 * and prints warnings/errors if necessary.
1182 * If the types are (almost) compatible, 1 is returned, otherwise 0. 1182 * If the types are (almost) compatible, 1 is returned, otherwise 0.
1183 */ 1183 */
1184static int 1184static int
1185check_assign_types_compatible(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1185check_assign_types_compatible(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1186{ 1186{
1187 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC; 1187 tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
1188 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL; 1188 type_t *ltp, *rtp, *lstp = NULL, *rstp = NULL;
1189 mod_t *mp; 1189 mod_t *mp;
1190 const char *lts, *rts; 1190 const char *lts, *rts;
1191 1191
1192 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR) 1192 if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
1193 lst = (lstp = ltp->t_subt)->t_tspec; 1193 lst = (lstp = ltp->t_subt)->t_tspec;
1194 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR) 1194 if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
1195 rst = (rstp = rtp->t_subt)->t_tspec; 1195 rst = (rstp = rtp->t_subt)->t_tspec;
1196 mp = &modtab[op]; 1196 mp = &modtab[op];
1197 1197
1198 if (tspec_is_arith(lt) && tspec_is_arith(rt)) 1198 if (tspec_is_arith(lt) && tspec_is_arith(rt))
1199 return 1; 1199 return 1;
1200 1200
1201 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) 1201 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
1202 /* both are struct or union */ 1202 /* both are struct or union */
1203 return ltp->t_str == rtp->t_str; 1203 return ltp->t_str == rtp->t_str;
1204 1204
1205 /* 0, 0L and (void *)0 may be assigned to any pointer */ 1205 /* 0, 0L and (void *)0 may be assigned to any pointer */
1206 if (lt == PTR && ((rt == PTR && rst == VOID) || tspec_is_int(rt))) { 1206 if (lt == PTR && ((rt == PTR && rst == VOID) || tspec_is_int(rt))) {
1207 if (rn->tn_op == CON && rn->tn_val->v_quad == 0) 1207 if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
1208 return 1; 1208 return 1;
1209 } 1209 }
1210 1210
1211 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) { 1211 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
1212 /* two pointers, at least one pointer to void */ 1212 /* two pointers, at least one pointer to void */
1213 if (sflag && (lst == FUNC || rst == FUNC)) { 1213 if (sflag && (lst == FUNC || rst == FUNC)) {
1214 /* comb. of ptr to func and ptr to void */ 1214 /* comb. of ptr to func and ptr to void */
1215 *(lst == FUNC ? &lts : &rts) = "function pointer"; 1215 *(lst == FUNC ? &lts : &rts) = "function pointer";
1216 *(lst == VOID ? &lts : &rts) = "'void *'"; 1216 *(lst == VOID ? &lts : &rts) = "'void *'";
1217 switch (op) { 1217 switch (op) {
1218 case INIT: 1218 case INIT:
1219 case RETURN: 1219 case RETURN:
1220 /* ANSI C forbids conversion of %s to %s */ 1220 /* ANSI C forbids conversion of %s to %s */
1221 warning(303, rts, lts); 1221 warning(303, rts, lts);
1222 break; 1222 break;
1223 case FARG: 1223 case FARG:
1224 /* ANSI C forbids conv. of %s to %s, arg #%d */ 1224 /* ANSI C forbids conv. of %s to %s, arg #%d */
1225 warning(304, rts, lts, arg); 1225 warning(304, rts, lts, arg);
1226 break; 1226 break;
1227 default: 1227 default:
1228 /* ANSI C forbids conv. of %s to %s, op %s */ 1228 /* ANSI C forbids conv. of %s to %s, op %s */
1229 warning(305, rts, lts, mp->m_name); 1229 warning(305, rts, lts, mp->m_name);
1230 break; 1230 break;
1231 } 1231 }
1232 } 1232 }
1233 } 1233 }
1234 1234
1235 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID || 1235 if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
1236 eqtype(lstp, rstp, 1, 0, NULL))) { 1236 eqtype(lstp, rstp, 1, 0, NULL))) {
1237 /* compatible pointer types (qualifiers ignored) */ 1237 /* compatible pointer types (qualifiers ignored) */
1238 if (!tflag && 1238 if (!tflag &&
1239 ((!lstp->t_const && rstp->t_const) || 1239 ((!lstp->t_const && rstp->t_const) ||
1240 (!lstp->t_volatile && rstp->t_volatile))) { 1240 (!lstp->t_volatile && rstp->t_volatile))) {
1241 /* left side has not all qualifiers of right */ 1241 /* left side has not all qualifiers of right */
1242 switch (op) { 1242 switch (op) {
1243 case INIT: 1243 case INIT:
1244 case RETURN: 1244 case RETURN:
1245 /* incompatible pointer types (%s != %s) */ 1245 /* incompatible pointer types (%s != %s) */
1246 warning(182, type_name(lstp), type_name(rstp)); 1246 warning(182, type_name(lstp), type_name(rstp));
1247 break; 1247 break;
1248 case FARG: 1248 case FARG:
1249 /* argument has incompatible pointer type... */ 1249 /* argument has incompatible pointer type... */
1250 warning(153, 1250 warning(153,
1251 arg, type_name(lstp), type_name(rstp)); 1251 arg, type_name(lstp), type_name(rstp));
1252 break; 1252 break;
1253 default: 1253 default:
1254 /* operands have incompatible pointer type... */ 1254 /* operands have incompatible pointer type... */
1255 warning(128, mp->m_name, 1255 warning(128, mp->m_name,
1256 type_name(lstp), type_name(rstp)); 1256 type_name(lstp), type_name(rstp));
1257 break; 1257 break;
1258 } 1258 }
1259 } 1259 }
1260 return 1; 1260 return 1;
1261 } 1261 }
1262 1262
1263 if ((lt == PTR && tspec_is_int(rt)) || 1263 if ((lt == PTR && tspec_is_int(rt)) ||
1264 (tspec_is_int(lt) && rt == PTR)) { 1264 (tspec_is_int(lt) && rt == PTR)) {
1265 const char *lx = lt == PTR ? "pointer" : "integer"; 1265 const char *lx = lt == PTR ? "pointer" : "integer";
1266 const char *rx = rt == PTR ? "pointer" : "integer"; 1266 const char *rx = rt == PTR ? "pointer" : "integer";
1267 1267
1268 switch (op) { 1268 switch (op) {
1269 case INIT: 1269 case INIT:
1270 case RETURN: 1270 case RETURN:
1271 /* illegal combination of %s (%s) and %s (%s) */ 1271 /* illegal combination of %s (%s) and %s (%s) */
1272 warning(183, lx, type_name(ltp), rx, type_name(rtp)); 1272 warning(183, lx, type_name(ltp), rx, type_name(rtp));
1273 break; 1273 break;
1274 case FARG: 1274 case FARG:
1275 /* illegal comb. of %s (%s) and %s (%s), arg #%d */ 1275 /* illegal comb. of %s (%s) and %s (%s), arg #%d */
1276 warning(154, 1276 warning(154,
1277 lx, type_name(ltp), rx, type_name(rtp), arg); 1277 lx, type_name(ltp), rx, type_name(rtp), arg);
1278 break; 1278 break;
1279 default: 1279 default:
1280 /* illegal combination of %s (%s) and %s (%s), op %s */ 1280 /* illegal combination of %s (%s) and %s (%s), op %s */
1281 warning(123, 1281 warning(123,
1282 lx, type_name(ltp), rx, type_name(rtp), mp->m_name); 1282 lx, type_name(ltp), rx, type_name(rtp), mp->m_name);
1283 break; 1283 break;
1284 } 1284 }
1285 return 1; 1285 return 1;
1286 } 1286 }
1287 1287
1288 if (lt == PTR && rt == PTR) { 1288 if (lt == PTR && rt == PTR) {
1289 switch (op) { 1289 switch (op) {
1290 case INIT: 1290 case INIT:
1291 case RETURN: 1291 case RETURN:
1292 warn_incompatible_pointers(NULL, ltp, rtp); 1292 warn_incompatible_pointers(NULL, ltp, rtp);
1293 break; 1293 break;
1294 case FARG: 1294 case FARG:
1295 /* arg. has incomp. pointer type, arg #%d (%s != %s) */ 1295 /* arg. has incomp. pointer type, arg #%d (%s != %s) */
1296 warning(153, arg, type_name(ltp), type_name(rtp)); 1296 warning(153, arg, type_name(ltp), type_name(rtp));
1297 break; 1297 break;
1298 default: 1298 default:
1299 warn_incompatible_pointers(mp, ltp, rtp); 1299 warn_incompatible_pointers(mp, ltp, rtp);
1300 break; 1300 break;
1301 } 1301 }
1302 return 1; 1302 return 1;
1303 } 1303 }
1304 1304
1305 switch (op) { 1305 switch (op) {
1306 case INIT: 1306 case INIT:
1307 /* initialisation type mismatch (%s) and (%s) */ 1307 /* initialisation type mismatch (%s) and (%s) */
1308 error(185, type_name(ltp), type_name(rtp)); 1308 error(185, type_name(ltp), type_name(rtp));
1309 break; 1309 break;
1310 case RETURN: 1310 case RETURN:
1311 /* return value type mismatch (%s) and (%s) */ 1311 /* return value type mismatch (%s) and (%s) */
1312 error(211, type_name(ltp), type_name(rtp)); 1312 error(211, type_name(ltp), type_name(rtp));
1313 break; 1313 break;
1314 case FARG: 1314 case FARG:
1315 /* argument is incompatible with prototype, arg #%d */ 1315 /* argument is incompatible with prototype, arg #%d */
1316 warning(155, arg); 1316 warning(155, arg);
1317 break; 1317 break;
1318 default: 1318 default:
1319 warn_incompatible_types(op, lt, rt); 1319 warn_incompatible_types(op, lt, rt);
1320 break; 1320 break;
1321 } 1321 }
1322 1322
1323 return 0; 1323 return 0;
1324} 1324}
1325 1325
1326/* 1326/*
1327 * Prints a warning if an operator, which should be senseless for an 1327 * Prints a warning if an operator, which should be senseless for an
1328 * enum type, is applied to an enum type. 1328 * enum type, is applied to an enum type.
1329 */ 1329 */
1330static void 1330static void
1331check_bad_enum_operation(op_t op, tnode_t *ln, tnode_t *rn) 1331check_bad_enum_operation(op_t op, tnode_t *ln, tnode_t *rn)
1332{ 1332{
1333 mod_t *mp; 1333 mod_t *mp;
1334 1334
1335 if (!eflag) 1335 if (!eflag)
1336 return; 1336 return;
1337 1337
1338 mp = &modtab[op]; 1338 mp = &modtab[op];
1339 1339
1340 if (!(ln->tn_type->t_isenum || 1340 if (!(ln->tn_type->t_isenum ||
1341 (mp->m_binary && rn->tn_type->t_isenum))) { 1341 (mp->m_binary && rn->tn_type->t_isenum))) {
1342 return; 1342 return;
1343 } 1343 }
1344 1344
1345 /* 1345 /*
1346 * Enum as offset to a pointer is an exception (otherwise enums 1346 * Enum as offset to a pointer is an exception (otherwise enums
1347 * could not be used as array indizes). 1347 * could not be used as array indizes).
1348 */ 1348 */
1349 if (op == PLUS && 1349 if (op == PLUS &&
1350 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) || 1350 ((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
1351 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) { 1351 (rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
1352 return; 1352 return;
1353 } 1353 }
1354 1354
1355 /* dubious operation on enum, op %s */ 1355 /* dubious operation on enum, op %s */
1356 warning(241, mp->m_name); 1356 warning(241, mp->m_name);
1357 1357
1358} 1358}
1359 1359
1360/* 1360/*
1361 * Prints a warning if an operator is applied to two different enum types. 1361 * Prints a warning if an operator is applied to two different enum types.
1362 */ 1362 */
1363static void 1363static void
1364check_enum_type_mismatch(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1364check_enum_type_mismatch(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1365{ 1365{
1366 mod_t *mp; 1366 mod_t *mp;
1367 1367
1368 mp = &modtab[op]; 1368 mp = &modtab[op];
1369 1369
1370 if (ln->tn_type->t_enum != rn->tn_type->t_enum) { 1370 if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
1371 switch (op) { 1371 switch (op) {
1372 case INIT: 1372 case INIT:
1373 /* enum type mismatch in initialisation */ 1373 /* enum type mismatch in initialisation */
1374 warning(210); 1374 warning(210);
1375 break; 1375 break;
1376 case FARG: 1376 case FARG:
1377 /* enum type mismatch, arg #%d */ 1377 /* enum type mismatch, arg #%d */
1378 warning(156, arg); 1378 warning(156, arg);
1379 break; 1379 break;
1380 case RETURN: 1380 case RETURN:
1381 /* return value type mismatch (%s) and (%s) */ 1381 /* return value type mismatch (%s) and (%s) */
1382 warning(211, 1382 warning(211,
1383 type_name(ln->tn_type), type_name(rn->tn_type)); 1383 type_name(ln->tn_type), type_name(rn->tn_type));
1384 break; 1384 break;
1385 default: 1385 default:
1386 /* enum type mismatch, op %s */ 1386 /* enum type mismatch, op %s */
1387 warning(130, mp->m_name); 1387 warning(130, mp->m_name);
1388 break; 1388 break;
1389 } 1389 }
1390 } else if (Pflag && mp->m_comp && op != EQ && op != NE) { 1390 } else if (Pflag && mp->m_comp && op != EQ && op != NE) {
1391 if (eflag) 1391 if (eflag)
1392 /* dubious comparison of enums, op %s */ 1392 /* dubious comparison of enums, op %s */
1393 warning(243, mp->m_name); 1393 warning(243, mp->m_name);
1394 } 1394 }
1395} 1395}
1396 1396
1397/* 1397/*
1398 * Prints a warning if an operator has both enum and other integer 1398 * Prints a warning if an operator has both enum and other integer
1399 * types. 1399 * types.
1400 */ 1400 */
1401static void 1401static void
1402check_enum_int_mismatch(op_t op, int arg, tnode_t *ln, tnode_t *rn) 1402check_enum_int_mismatch(op_t op, int arg, tnode_t *ln, tnode_t *rn)
1403{ 1403{
1404 1404
1405 if (!eflag) 1405 if (!eflag)
1406 return; 1406 return;
1407 1407
1408 switch (op) { 1408 switch (op) {
1409 case INIT: 1409 case INIT:
1410 /* 1410 /*
1411 * Initializations with 0 should be allowed. Otherwise, 1411 * Initializations with 0 should be allowed. Otherwise,
1412 * we should complain about all uninitialized enums, 1412 * we should complain about all uninitialized enums,
1413 * consequently. 1413 * consequently.
1414 */ 1414 */
1415 if (!rn->tn_type->t_isenum && rn->tn_op == CON && 1415 if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
1416 tspec_is_int(rn->tn_type->t_tspec) && 1416 tspec_is_int(rn->tn_type->t_tspec) &&
1417 rn->tn_val->v_quad == 0) { 1417 rn->tn_val->v_quad == 0) {
1418 return; 1418 return;
1419 } 1419 }
1420 /* initialisation of '%s' with '%s' */ 1420 /* initialisation of '%s' with '%s' */
1421 warning(277, type_name(ln->tn_type), type_name(rn->tn_type)); 1421 warning(277, type_name(ln->tn_type), type_name(rn->tn_type));
1422 break; 1422 break;
1423 case FARG: 1423 case FARG:
1424 /* combination of '%s' and '%s', arg #%d */ 1424 /* combination of '%s' and '%s', arg #%d */
1425 warning(278, 1425 warning(278,
1426 type_name(ln->tn_type), type_name(rn->tn_type), arg); 1426 type_name(ln->tn_type), type_name(rn->tn_type), arg);
1427 break; 1427 break;
1428 case RETURN: 1428 case RETURN:
1429 /* combination of '%s' and '%s' in return */ 1429 /* combination of '%s' and '%s' in return */
1430 warning(279, type_name(ln->tn_type), type_name(rn->tn_type)); 1430 warning(279, type_name(ln->tn_type), type_name(rn->tn_type));
1431 break; 1431 break;
1432 default: 1432 default:
1433 /* combination of '%s' and '%s', op %s */ 1433 /* combination of '%s' and '%s', op %s */
1434 warning(242, type_name(ln->tn_type), type_name(rn->tn_type), 1434 warning(242, type_name(ln->tn_type), type_name(rn->tn_type),
1435 modtab[op].m_name); 1435 modtab[op].m_name);
1436 break; 1436 break;
1437 } 1437 }
1438} 1438}
1439 1439
1440/* 1440/*
1441 * Build and initialize a new node. 1441 * Build and initialize a new node.
1442 */ 1442 */
1443static tnode_t * 1443static tnode_t *
1444new_tnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn) 1444new_tnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
1445{ 1445{
1446 tnode_t *ntn; 1446 tnode_t *ntn;
1447 tspec_t t; 1447 tspec_t t;
1448#ifdef notyet 1448#ifdef notyet
1449 size_t l; 1449 size_t l;
1450 uint64_t rnum; 1450 uint64_t rnum;
1451#endif 1451#endif
1452 1452
1453 ntn = getnode(); 1453 ntn = getnode();
1454 1454
1455 ntn->tn_op = op; 1455 ntn->tn_op = op;
1456 ntn->tn_type = type; 1456 ntn->tn_type = type;
1457 ntn->tn_left = ln; 1457 ntn->tn_left = ln;
1458 ntn->tn_right = rn; 1458 ntn->tn_right = rn;
1459 1459
1460 switch (op) { 1460 switch (op) {
1461#ifdef notyet 1461#ifdef notyet
1462 case SHR: 1462 case SHR:
1463 if (rn->tn_op != CON) 1463 if (rn->tn_op != CON)
1464 break; 1464 break;
1465 rnum = rn->tn_val->v_quad; 1465 rnum = rn->tn_val->v_quad;
1466 l = tsize(ln->tn_type) / CHAR_BIT; 1466 l = tsize(ln->tn_type) / CHAR_BIT;
1467 t = ln->tn_type->t_tspec; 1467 t = ln->tn_type->t_tspec;
1468 switch (l) { 1468 switch (l) {
1469 case 8: 1469 case 8:
1470 if (rnum >= 56) 1470 if (rnum >= 56)
1471 t = UCHAR; 1471 t = UCHAR;
1472 else if (rnum >= 48) 1472 else if (rnum >= 48)
1473 t = USHORT; 1473 t = USHORT;
1474 else if (rnum >= 32) 1474 else if (rnum >= 32)
1475 t = UINT; 1475 t = UINT;
1476 break; 1476 break;
1477 case 4: 1477 case 4:
1478 if (rnum >= 24) 1478 if (rnum >= 24)
1479 t = UCHAR; 1479 t = UCHAR;
1480 else if (rnum >= 16) 1480 else if (rnum >= 16)
1481 t = USHORT; 1481 t = USHORT;
1482 break; 1482 break;
1483 case 2: 1483 case 2:
1484 if (rnum >= 8) 1484 if (rnum >= 8)
1485 t = UCHAR; 1485 t = UCHAR;
1486 break; 1486 break;
1487 default: 1487 default:
1488 break; 1488 break;
1489 } 1489 }
1490 if (t != ln->tn_type->t_tspec) 1490 if (t != ln->tn_type->t_tspec)
1491 ntn->tn_type->t_tspec = t; 1491 ntn->tn_type->t_tspec = t;
1492 break; 1492 break;
1493#endif 1493#endif
1494 case STAR: 1494 case STAR:
1495 case FSEL: 1495 case FSEL:
1496 lint_assert(ln->tn_type->t_tspec == PTR); 1496 lint_assert(ln->tn_type->t_tspec == PTR);
1497 t = ln->tn_type->t_subt->t_tspec; 1497 t = ln->tn_type->t_subt->t_tspec;
1498 if (t != FUNC && t != VOID) 1498 if (t != FUNC && t != VOID)
1499 ntn->tn_lvalue = 1; 1499 ntn->tn_lvalue = 1;
1500 break; 1500 break;
1501 default: 1501 default:
1502 break; 1502 break;
1503 } 1503 }
1504 1504
1505 return ntn; 1505 return ntn;
1506} 1506}
1507 1507
1508/* 1508/*
1509 * Performs usual conversion of operands to (unsigned) int. 1509 * Performs usual conversion of operands to (unsigned) int.
1510 * 1510 *
1511 * If tflag is set or the operand is a function argument with no 1511 * If tflag is set or the operand is a function argument with no
1512 * type information (no prototype or variable # of args), convert 1512 * type information (no prototype or variable # of args), convert
1513 * float to double. 1513 * float to double.
1514 */ 1514 */
1515tnode_t * 1515tnode_t *
1516promote(op_t op, int farg, tnode_t *tn) 1516promote(op_t op, int farg, tnode_t *tn)
1517{ 1517{
1518 tspec_t t; 1518 tspec_t t;
1519 type_t *ntp; 1519 type_t *ntp;
1520 u_int len; 1520 u_int len;
1521 1521
1522 t = tn->tn_type->t_tspec; 1522 t = tn->tn_type->t_tspec;
1523 1523
1524 if (!tspec_is_arith(t)) 1524 if (!tspec_is_arith(t))
1525 return tn; 1525 return tn;
1526 1526
1527 if (!tflag) { 1527 if (!tflag) {
1528 /* 1528 /*
1529 * ANSI C requires that the result is always of type INT 1529 * ANSI C requires that the result is always of type INT
1530 * if INT can represent all possible values of the previous 1530 * if INT can represent all possible values of the previous
1531 * type. 1531 * type.
1532 */ 1532 */
1533 if (tn->tn_type->t_isfield) { 1533 if (tn->tn_type->t_bitfield) {
1534 len = tn->tn_type->t_flen; 1534 len = tn->tn_type->t_flen;
1535 if (size(INT) > len) { 1535 if (size(INT) > len) {
1536 t = INT; 1536 t = INT;
1537 } else { 1537 } else {
1538 lint_assert(len == size(INT)); 1538 lint_assert(len == size(INT));
1539 if (tspec_is_uint(t)) { 1539 if (tspec_is_uint(t)) {
1540 t = UINT; 1540 t = UINT;
1541 } else { 1541 } else {
1542 t = INT; 1542 t = INT;
1543 } 1543 }
1544 } 1544 }
1545 } else if (t == CHAR || t == UCHAR || t == SCHAR) { 1545 } else if (t == CHAR || t == UCHAR || t == SCHAR) {
1546 t = (size(CHAR) < size(INT) || t != UCHAR) ? 1546 t = (size(CHAR) < size(INT) || t != UCHAR) ?
1547 INT : UINT; 1547 INT : UINT;
1548 } else if (t == SHORT || t == USHORT) { 1548 } else if (t == SHORT || t == USHORT) {
1549 t = (size(SHORT) < size(INT) || t == SHORT) ? 1549 t = (size(SHORT) < size(INT) || t == SHORT) ?
1550 INT : UINT; 1550 INT : UINT;
1551 } else if (t == ENUM) { 1551 } else if (t == ENUM) {
1552 t = INT; 1552 t = INT;
1553 } else if (farg && t == FLOAT) { 1553 } else if (farg && t == FLOAT) {
1554 t = DOUBLE; 1554 t = DOUBLE;
1555 } 1555 }
1556 } else { 1556 } else {
1557 /* 1557 /*
1558 * In traditional C, keep unsigned and promote FLOAT 1558 * In traditional C, keep unsigned and promote FLOAT
1559 * to DOUBLE. 1559 * to DOUBLE.
1560 */ 1560 */
1561 if (t == UCHAR || t == USHORT) { 1561 if (t == UCHAR || t == USHORT) {
1562 t = UINT; 1562 t = UINT;
1563 } else if (t == CHAR || t == SCHAR || t == SHORT) { 1563 } else if (t == CHAR || t == SCHAR || t == SHORT) {
1564 t = INT; 1564 t = INT;
1565 } else if (t == FLOAT) { 1565 } else if (t == FLOAT) {
1566 t = DOUBLE; 1566 t = DOUBLE;
1567 } else if (t == ENUM) { 1567 } else if (t == ENUM) {
1568 t = INT; 1568 t = INT;
1569 } 1569 }
1570 } 1570 }
1571 1571
1572 if (t != tn->tn_type->t_tspec) { 1572 if (t != tn->tn_type->t_tspec) {
1573 ntp = tduptyp(tn->tn_type); 1573 ntp = tduptyp(tn->tn_type);
1574 ntp->t_tspec = t; 1574 ntp->t_tspec = t;
1575 /* 1575 /*
1576 * Keep t_isenum so we are later able to check compatibility 1576 * Keep t_isenum so we are later able to check compatibility
1577 * of enum types. 1577 * of enum types.
1578 */ 1578 */
1579 tn = convert(op, 0, ntp, tn); 1579 tn = convert(op, 0, ntp, tn);
1580 } 1580 }
1581 1581
1582 return tn; 1582 return tn;
1583} 1583}
1584 1584
1585/* 1585/*
1586 * Insert conversions which are necessary to give both operands the same 1586 * Insert conversions which are necessary to give both operands the same
1587 * type. This is done in different ways for traditional C and ANIS C. 1587 * type. This is done in different ways for traditional C and ANIS C.
1588 */ 1588 */
1589static void 1589static void
1590balance(op_t op, tnode_t **lnp, tnode_t **rnp) 1590balance(op_t op, tnode_t **lnp, tnode_t **rnp)
1591{ 1591{
1592 tspec_t lt, rt, t; 1592 tspec_t lt, rt, t;
1593 int i, u; 1593 int i, u;
1594 type_t *ntp; 1594 type_t *ntp;
1595 static tspec_t tl[] = { 1595 static tspec_t tl[] = {
1596 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT, 1596 LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
1597 }; 1597 };
1598 1598
1599 lt = (*lnp)->tn_type->t_tspec; 1599 lt = (*lnp)->tn_type->t_tspec;
1600 rt = (*rnp)->tn_type->t_tspec; 1600 rt = (*rnp)->tn_type->t_tspec;
1601 1601
1602 if (!tspec_is_arith(lt) || !tspec_is_arith(rt)) 1602 if (!tspec_is_arith(lt) || !tspec_is_arith(rt))
1603 return; 1603 return;
1604 1604
1605 if (!tflag) { 1605 if (!tflag) {
1606 if (lt == rt) { 1606 if (lt == rt) {
1607 t = lt; 1607 t = lt;
1608 } else if (lt == LCOMPLEX || rt == LCOMPLEX) { 1608 } else if (lt == LCOMPLEX || rt == LCOMPLEX) {
1609 t = LCOMPLEX; 1609 t = LCOMPLEX;
1610 } else if (lt == DCOMPLEX || rt == DCOMPLEX) { 1610 } else if (lt == DCOMPLEX || rt == DCOMPLEX) {
1611 t = DCOMPLEX; 1611 t = DCOMPLEX;
1612 } else if (lt == COMPLEX || rt == COMPLEX) { 1612 } else if (lt == COMPLEX || rt == COMPLEX) {
1613 t = COMPLEX; 1613 t = COMPLEX;
1614 } else if (lt == FCOMPLEX || rt == FCOMPLEX) { 1614 } else if (lt == FCOMPLEX || rt == FCOMPLEX) {
1615 t = FCOMPLEX; 1615 t = FCOMPLEX;
1616 } else if (lt == LDOUBLE || rt == LDOUBLE) { 1616 } else if (lt == LDOUBLE || rt == LDOUBLE) {
1617 t = LDOUBLE; 1617 t = LDOUBLE;
1618 } else if (lt == DOUBLE || rt == DOUBLE) { 1618 } else if (lt == DOUBLE || rt == DOUBLE) {
1619 t = DOUBLE; 1619 t = DOUBLE;
1620 } else if (lt == FLOAT || rt == FLOAT) { 1620 } else if (lt == FLOAT || rt == FLOAT) {
1621 t = FLOAT; 1621 t = FLOAT;
1622 } else { 1622 } else {
1623 /* 1623 /*
1624 * If type A has more bits than type B it should 1624 * If type A has more bits than type B it should
1625 * be able to hold all possible values of type B. 1625 * be able to hold all possible values of type B.
1626 */ 1626 */
1627 if (size(lt) > size(rt)) { 1627 if (size(lt) > size(rt)) {
1628 t = lt; 1628 t = lt;
1629 } else if (size(lt) < size(rt)) { 1629 } else if (size(lt) < size(rt)) {
1630 t = rt; 1630 t = rt;
1631 } else { 1631 } else {
1632 for (i = 3; tl[i] != INT; i++) { 1632 for (i = 3; tl[i] != INT; i++) {
1633 if (tl[i] == lt || tl[i] == rt) 1633 if (tl[i] == lt || tl[i] == rt)
1634 break; 1634 break;
1635 } 1635 }
1636 if ((tspec_is_uint(lt) || tspec_is_uint(rt)) && 1636 if ((tspec_is_uint(lt) || tspec_is_uint(rt)) &&
1637 !tspec_is_uint(tl[i])) { 1637 !tspec_is_uint(tl[i])) {
1638 i--; 1638 i--;
1639 } 1639 }
1640 t = tl[i]; 1640 t = tl[i];
1641 } 1641 }
1642 } 1642 }
1643 } else { 1643 } else {
1644 /* Keep unsigned in traditional C */ 1644 /* Keep unsigned in traditional C */
1645 u = tspec_is_uint(lt) || tspec_is_uint(rt); 1645 u = tspec_is_uint(lt) || tspec_is_uint(rt);
1646 for (i = 0; tl[i] != INT; i++) { 1646 for (i = 0; tl[i] != INT; i++) {
1647 if (lt == tl[i] || rt == tl[i]) 1647 if (lt == tl[i] || rt == tl[i])
1648 break; 1648 break;
1649 } 1649 }
1650 t = tl[i]; 1650 t = tl[i];
1651 if (u && tspec_is_int(t) && !tspec_is_uint(t)) 1651 if (u && tspec_is_int(t) && !tspec_is_uint(t))
1652 t = unsigned_type(t); 1652 t = unsigned_type(t);
1653 } 1653 }
1654 1654
1655 if (t != lt) { 1655 if (t != lt) {
1656 ntp = tduptyp((*lnp)->tn_type); 1656 ntp = tduptyp((*lnp)->tn_type);
1657 ntp->t_tspec = t; 1657 ntp->t_tspec = t;
1658 *lnp = convert(op, 0, ntp, *lnp); 1658 *lnp = convert(op, 0, ntp, *lnp);
1659 } 1659 }
1660 if (t != rt) { 1660 if (t != rt) {
1661 ntp = tduptyp((*rnp)->tn_type); 1661 ntp = tduptyp((*rnp)->tn_type);
1662 ntp->t_tspec = t; 1662 ntp->t_tspec = t;
1663 *rnp = convert(op, 0, ntp, *rnp); 1663 *rnp = convert(op, 0, ntp, *rnp);
1664 } 1664 }
1665} 1665}
1666 1666
1667/* 1667/*
1668 * Insert a conversion operator, which converts the type of the node 1668 * Insert a conversion operator, which converts the type of the node
1669 * to another given type. 1669 * to another given type.
1670 * If op is FARG, arg is the number of the argument (used for warnings). 1670 * If op is FARG, arg is the number of the argument (used for warnings).
1671 */ 1671 */
1672tnode_t * 1672tnode_t *
1673convert(op_t op, int arg, type_t *tp, tnode_t *tn) 1673convert(op_t op, int arg, type_t *tp, tnode_t *tn)
1674{ 1674{
1675 tnode_t *ntn; 1675 tnode_t *ntn;
1676 tspec_t nt, ot, ost = NOTSPEC; 1676 tspec_t nt, ot, ost = NOTSPEC;
1677 1677
1678 nt = tp->t_tspec; 1678 nt = tp->t_tspec;
1679 if ((ot = tn->tn_type->t_tspec) == PTR) 1679 if ((ot = tn->tn_type->t_tspec) == PTR)
1680 ost = tn->tn_type->t_subt->t_tspec; 1680 ost = tn->tn_type->t_subt->t_tspec;
1681 1681
1682 if (!tflag && !sflag && op == FARG) 1682 if (!tflag && !sflag && op == FARG)
1683 check_prototype_conversion(arg, nt, ot, tp, tn); 1683 check_prototype_conversion(arg, nt, ot, tp, tn);
1684 if (tspec_is_int(nt) && tspec_is_int(ot)) { 1684 if (tspec_is_int(nt) && tspec_is_int(ot)) {
1685 check_integer_conversion(op, arg, nt, ot, tp, tn); 1685 check_integer_conversion(op, arg, nt, ot, tp, tn);
1686 } else if (nt == PTR && ((ot == PTR && ost == VOID) || 1686 } else if (nt == PTR && ((ot == PTR && ost == VOID) ||
1687 tspec_is_int(ot)) && tn->tn_op == CON && tn->tn_val->v_quad == 0) { 1687 tspec_is_int(ot)) && tn->tn_op == CON && tn->tn_val->v_quad == 0) {
1688 /* 0, 0L and (void *)0 may be assigned to any pointer. */ 1688 /* 0, 0L and (void *)0 may be assigned to any pointer. */
1689 } else if (tspec_is_int(nt) && ot == PTR) { 1689 } else if (tspec_is_int(nt) && ot == PTR) {
1690 check_pointer_integer_conversion(op, nt, tp, tn); 1690 check_pointer_integer_conversion(op, nt, tp, tn);
1691 } else if (nt == PTR && ot == PTR) { 1691 } else if (nt == PTR && ot == PTR) {
1692 check_pointer_conversion(op, tn, tp); 1692 check_pointer_conversion(op, tn, tp);
1693 } 1693 }
1694 1694
1695 ntn = getnode(); 1695 ntn = getnode();
1696 ntn->tn_op = CVT; 1696 ntn->tn_op = CVT;
1697 ntn->tn_type = tp; 1697 ntn->tn_type = tp;
1698 ntn->tn_cast = op == CVT; 1698 ntn->tn_cast = op == CVT;
1699 ntn->tn_right = NULL; 1699 ntn->tn_right = NULL;
1700 if (tn->tn_op != CON || nt == VOID) { 1700 if (tn->tn_op != CON || nt == VOID) {
1701 ntn->tn_left = tn; 1701 ntn->tn_left = tn;
1702 } else { 1702 } else {
1703 ntn->tn_op = CON; 1703 ntn->tn_op = CON;
1704 ntn->tn_val = tgetblk(sizeof (val_t)); 1704 ntn->tn_val = tgetblk(sizeof (val_t));
1705 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val); 1705 cvtcon(op, arg, ntn->tn_type, ntn->tn_val, tn->tn_val);
1706 } 1706 }
1707 1707
1708 return ntn; 1708 return ntn;
1709} 1709}
1710 1710
1711/* 1711/*
1712 * Print a warning if a prototype causes a type conversion that is 1712 * Print a warning if a prototype causes a type conversion that is
1713 * different from what would happen to the same argument in the 1713 * different from what would happen to the same argument in the
1714 * absence of a prototype. 1714 * absence of a prototype.
1715 * 1715 *
1716 * Errors/Warnings about illegal type combinations are already printed 1716 * Errors/Warnings about illegal type combinations are already printed
1717 * in check_assign_types_compatible(). 1717 * in check_assign_types_compatible().
1718 */ 1718 */
1719static void 1719static void
1720check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp, 1720check_prototype_conversion(int arg, tspec_t nt, tspec_t ot, type_t *tp,
1721 tnode_t *tn) 1721 tnode_t *tn)
1722{ 1722{
1723 tnode_t *ptn; 1723 tnode_t *ptn;
1724 1724
1725 if (!tspec_is_arith(nt) || !tspec_is_arith(ot)) 1725 if (!tspec_is_arith(nt) || !tspec_is_arith(ot))
1726 return; 1726 return;
1727 1727
1728 /* 1728 /*
1729 * If the type of the formal parameter is char/short, a warning 1729 * If the type of the formal parameter is char/short, a warning
1730 * would be useless, because functions declared the old style 1730 * would be useless, because functions declared the old style
1731 * can't expect char/short arguments. 1731 * can't expect char/short arguments.
1732 */ 1732 */
1733 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT) 1733 if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
1734 return; 1734 return;
1735 1735
1736 /* get default promotion */ 1736 /* get default promotion */
1737 ptn = promote(NOOP, 1, tn); 1737 ptn = promote(NOOP, 1, tn);
1738 ot = ptn->tn_type->t_tspec; 1738 ot = ptn->tn_type->t_tspec;
1739 1739
1740 /* return if types are the same with and without prototype */ 1740 /* return if types are the same with and without prototype */
1741 if (nt == ot || (nt == ENUM && ot == INT)) 1741 if (nt == ot || (nt == ENUM && ot == INT))
1742 return; 1742 return;
1743 1743
1744 if (tspec_is_float(nt) != tspec_is_float(ot) || 1744 if (tspec_is_float(nt) != tspec_is_float(ot) ||
1745 psize(nt) != psize(ot)) { 1745 psize(nt) != psize(ot)) {
1746 /* representation and/or width change */ 1746 /* representation and/or width change */
1747 if (!tspec_is_int(ot) || psize(ot) > psize(INT)) { 1747 if (!tspec_is_int(ot) || psize(ot) > psize(INT)) {
1748 /* conversion to '%s' due to prototype, arg #%d */ 1748 /* conversion to '%s' due to prototype, arg #%d */
1749 warning(259, type_name(tp), arg); 1749 warning(259, type_name(tp), arg);
1750 } 1750 }
1751 } else if (hflag) { 1751 } else if (hflag) {
1752 /* 1752 /*
1753 * they differ in sign or base type (char, short, int, 1753 * they differ in sign or base type (char, short, int,
1754 * long, long long, float, double, long double) 1754 * long, long long, float, double, long double)
1755 * 1755 *
1756 * if they differ only in sign and the argument is a constant 1756 * if they differ only in sign and the argument is a constant
1757 * and the msb of the argument is not set, print no warning 1757 * and the msb of the argument is not set, print no warning
1758 */ 1758 */
1759 if (ptn->tn_op == CON && tspec_is_int(nt) && 1759 if (ptn->tn_op == CON && tspec_is_int(nt) &&
1760 signed_type(nt) == signed_type(ot) && 1760 signed_type(nt) == signed_type(ot) &&
1761 msb(ptn->tn_val->v_quad, ot, -1) == 0) { 1761 msb(ptn->tn_val->v_quad, ot, -1) == 0) {
1762 /* ok */ 1762 /* ok */
1763 } else { 1763 } else {
1764 /* conversion to '%s' due to prototype, arg #%d */ 1764 /* conversion to '%s' due to prototype, arg #%d */
1765 warning(259, type_name(tp), arg); 1765 warning(259, type_name(tp), arg);
1766 } 1766 }
1767 } 1767 }
1768} 1768}
1769 1769
1770/* 1770/*
1771 * Print warnings for conversions of integer types which may cause problems. 1771 * Print warnings for conversions of integer types which may cause problems.
1772 */ 1772 */
1773/* ARGSUSED */ 1773/* ARGSUSED */
1774static void 1774static void
1775check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp, 1775check_integer_conversion(op_t op, int arg, tspec_t nt, tspec_t ot, type_t *tp,
1776 tnode_t *tn) 1776 tnode_t *tn)
1777{ 1777{
1778 char opbuf[16]; 1778 char opbuf[16];
1779 1779
1780 if (tn->tn_op == CON) 1780 if (tn->tn_op == CON)
1781 return; 1781 return;
1782 1782
1783 if (op == CVT) 1783 if (op == CVT)
1784 return; 1784 return;
1785 1785
1786 if (Pflag && psize(nt) > psize(ot) && 1786 if (Pflag && psize(nt) > psize(ot) &&
1787 tspec_is_uint(nt) != tspec_is_uint(ot)) { 1787 tspec_is_uint(nt) != tspec_is_uint(ot)) {
1788 if (aflag && pflag) { 1788 if (aflag && pflag) {
1789 if (op == FARG) { 1789 if (op == FARG) {
1790 /* conversion to '%s' may sign-extend ... */ 1790 /* conversion to '%s' may sign-extend ... */
1791 warning(297, type_name(tp), arg); 1791 warning(297, type_name(tp), arg);
1792 } else { 1792 } else {
1793 /* conversion to '%s' may sign-extend ... */ 1793 /* conversion to '%s' may sign-extend ... */
1794 warning(131, type_name(tp)); 1794 warning(131, type_name(tp));
1795 } 1795 }
1796 } 1796 }
1797 } 1797 }
1798 1798
1799 if (Pflag && psize(nt) > psize(ot)) { 1799 if (Pflag && psize(nt) > psize(ot)) {
1800 switch (tn->tn_op) { 1800 switch (tn->tn_op) {
1801 case PLUS: 1801 case PLUS:
1802 case MINUS: 1802 case MINUS:
1803 case MULT: 1803 case MULT:
1804 case SHL: 1804 case SHL:
1805 /* suggest cast from '%s' to '%s' on op %s to ... */ 1805 /* suggest cast from '%s' to '%s' on op %s to ... */
1806 warning(324, type_name(gettyp(ot)), type_name(tp), 1806 warning(324, type_name(gettyp(ot)), type_name(tp),
1807 print_tnode(opbuf, sizeof(opbuf), tn)); 1807 print_tnode(opbuf, sizeof(opbuf), tn));
1808 break; 1808 break;
1809 default: 1809 default:
1810 break; 1810 break;
1811 } 1811 }
1812 } 1812 }
1813 1813
1814 if (psize(nt) < psize(ot) && 1814 if (psize(nt) < psize(ot) &&
1815 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD || 1815 (ot == LONG || ot == ULONG || ot == QUAD || ot == UQUAD ||
1816 aflag > 1)) { 1816 aflag > 1)) {
1817 /* conversion from '%s' may lose accuracy */ 1817 /* conversion from '%s' may lose accuracy */
1818 if (aflag) { 1818 if (aflag) {
1819 if (op == FARG) { 1819 if (op == FARG) {
1820 /* conv. from '%s' to '%s' may lose ... */ 1820 /* conv. from '%s' to '%s' may lose ... */
1821 warning(298, 1821 warning(298,
1822 type_name(tn->tn_type), type_name(tp), arg); 1822 type_name(tn->tn_type), type_name(tp), arg);
1823 } else { 1823 } else {
1824 /* conv. from '%s' to '%s' may lose accuracy */ 1824 /* conv. from '%s' to '%s' may lose accuracy */
1825 warning(132, 1825 warning(132,
1826 type_name(tn->tn_type), type_name(tp)); 1826 type_name(tn->tn_type), type_name(tp));
1827 } 1827 }
1828 } 1828 }
1829 } 1829 }
1830} 1830}
1831 1831
1832/* 1832/*
1833 * Print warnings for dubious conversions of pointer to integer. 1833 * Print warnings for dubious conversions of pointer to integer.
1834 */ 1834 */
1835static void 1835static void
1836check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn) 1836check_pointer_integer_conversion(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
1837{ 1837{
1838 1838
1839 if (tn->tn_op == CON) 1839 if (tn->tn_op == CON)
1840 return; 1840 return;
1841 1841
1842 if (op != CVT) { 1842 if (op != CVT) {
1843 /* We got already an error. */ 1843 /* We got already an error. */
1844 return; 1844 return;
1845 } 1845 }
1846 1846
1847 if (psize(nt) < psize(PTR)) { 1847 if (psize(nt) < psize(PTR)) {
1848 if (pflag && size(nt) >= size(PTR)) { 1848 if (pflag && size(nt) >= size(PTR)) {
1849 /* conv. of pointer to '%s' may lose bits */ 1849 /* conv. of pointer to '%s' may lose bits */
1850 warning(134, type_name(tp)); 1850 warning(134, type_name(tp));
1851 } else { 1851 } else {
1852 /* conv. of pointer to '%s' loses bits */ 1852 /* conv. of pointer to '%s' loses bits */
1853 warning(133, type_name(tp)); 1853 warning(133, type_name(tp));
1854 } 1854 }
1855 } 1855 }
1856} 1856}
1857 1857
1858/* 1858/*
1859 * Print warnings for questionable pointer conversions. 1859 * Print warnings for questionable pointer conversions.
1860 */ 1860 */
1861static void 1861static void
1862check_pointer_conversion(op_t op, tnode_t *tn, type_t *tp) 1862check_pointer_conversion(op_t op, tnode_t *tn, type_t *tp)
1863{ 1863{
1864 tspec_t nt, ot; 1864 tspec_t nt, ot;
1865 const char *nts, *ots; 1865 const char *nts, *ots;
1866 1866
1867 /* 1867 /*
1868 * We got already an error (pointers of different types 1868 * We got already an error (pointers of different types
1869 * without a cast) or we will not get a warning. 1869 * without a cast) or we will not get a warning.
1870 */ 1870 */
1871 if (op != CVT) 1871 if (op != CVT)
1872 return; 1872 return;
1873 1873
1874 nt = tp->t_subt->t_tspec; 1874 nt = tp->t_subt->t_tspec;
1875 ot = tn->tn_type->t_subt->t_tspec; 1875 ot = tn->tn_type->t_subt->t_tspec;
1876 1876
1877 if (nt == VOID || ot == VOID) { 1877 if (nt == VOID || ot == VOID) {
1878 if (sflag && (nt == FUNC || ot == FUNC)) { 1878 if (sflag && (nt == FUNC || ot == FUNC)) {
1879 /* (void *)0 already handled in convert() */ 1879 /* (void *)0 already handled in convert() */
1880 *(nt == FUNC ? &nts : &ots) = "function pointer"; 1880 *(nt == FUNC ? &nts : &ots) = "function pointer";
1881 *(nt == VOID ? &nts : &ots) = "'void *'"; 1881 *(nt == VOID ? &nts : &ots) = "'void *'";
1882 /* ANSI C forbids conversion of %s to %s */ 1882 /* ANSI C forbids conversion of %s to %s */
1883 warning(303, ots, nts); 1883 warning(303, ots, nts);
1884 } 1884 }
1885 return; 1885 return;
1886 } else if (nt == FUNC && ot == FUNC) { 1886 } else if (nt == FUNC && ot == FUNC) {
1887 return; 1887 return;
1888 } else if (nt == FUNC || ot == FUNC) { 1888 } else if (nt == FUNC || ot == FUNC) {
1889 /* questionable conversion of function pointer */ 1889 /* questionable conversion of function pointer */
1890 warning(229); 1890 warning(229);
1891 return; 1891 return;
1892 } 1892 }
1893 1893
1894 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) { 1894 if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
1895 if (hflag) 1895 if (hflag)
1896 /* possible pointer alignment problem */ 1896 /* possible pointer alignment problem */
1897 warning(135); 1897 warning(135);
1898 } 1898 }
1899 if (((nt == STRUCT || nt == UNION) && 1899 if (((nt == STRUCT || nt == UNION) &&
1900 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) || 1900 tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
1901 psize(nt) != psize(ot)) { 1901 psize(nt) != psize(ot)) {
1902 if (cflag) { 1902 if (cflag) {
1903 /* pointer casts may be troublesome */ 1903 /* pointer casts may be troublesome */
1904 warning(247); 1904 warning(247);
1905 } 1905 }
1906 } 1906 }
1907} 1907}
1908 1908
1909/* 1909/*
1910 * Converts a typed constant in a constant of another type. 1910 * Converts a typed constant in a constant of another type.
1911 * 1911 *
1912 * op operator which requires conversion 1912 * op operator which requires conversion
1913 * arg if op is FARG, # of argument 1913 * arg if op is FARG, # of argument
1914 * tp type in which to convert the constant 1914 * tp type in which to convert the constant
1915 * nv new constant 1915 * nv new constant
1916 * v old constant 1916 * v old constant
1917 */ 1917 */
1918void 1918void
1919cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v) 1919cvtcon(op_t op, int arg, type_t *tp, val_t *nv, val_t *v)
1920{ 1920{
1921 tspec_t ot, nt; 1921 tspec_t ot, nt;
1922 ldbl_t max = 0.0, min = 0.0; 1922 ldbl_t max = 0.0, min = 0.0;
1923 int sz, rchk; 1923 int sz, rchk;
1924 int64_t xmask, xmsk1; 1924 int64_t xmask, xmsk1;
1925 int osz, nsz; 1925 int osz, nsz;
1926 1926
1927 ot = v->v_tspec; 1927 ot = v->v_tspec;
1928 nt = nv->v_tspec = tp->t_tspec; 1928 nt = nv->v_tspec = tp->t_tspec;
1929 rchk = 0; 1929 rchk = 0;
1930 1930
1931 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) { 1931 if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
1932 switch (nt) { 1932 switch (nt) {
1933 case BOOL: 1933 case BOOL:
1934 max = 1; min = 0; break; 1934 max = 1; min = 0; break;
1935 case CHAR: 1935 case CHAR:
1936 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break; 1936 max = TARG_CHAR_MAX; min = TARG_CHAR_MIN; break;
1937 case UCHAR: 1937 case UCHAR:
1938 max = TARG_UCHAR_MAX; min = 0; break; 1938 max = TARG_UCHAR_MAX; min = 0; break;
1939 case SCHAR: 1939 case SCHAR:
1940 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break; 1940 max = TARG_SCHAR_MAX; min = TARG_SCHAR_MIN; break;
1941 case SHORT: 1941 case SHORT:
1942 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break; 1942 max = TARG_SHRT_MAX; min = TARG_SHRT_MIN; break;
1943 case USHORT: 1943 case USHORT:
1944 max = TARG_USHRT_MAX; min = 0; break; 1944 max = TARG_USHRT_MAX; min = 0; break;
1945 case ENUM: 1945 case ENUM:
1946 case INT: 1946 case INT:
1947 max = TARG_INT_MAX; min = TARG_INT_MIN; break; 1947 max = TARG_INT_MAX; min = TARG_INT_MIN; break;
1948 case UINT: 1948 case UINT:
1949 max = (u_int)TARG_UINT_MAX;min = 0; break; 1949 max = (u_int)TARG_UINT_MAX;min = 0; break;
1950 case LONG: 1950 case LONG:
1951 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break; 1951 max = TARG_LONG_MAX; min = TARG_LONG_MIN; break;
1952 case ULONG: 1952 case ULONG:
1953 max = (u_long)TARG_ULONG_MAX; min = 0; break; 1953 max = (u_long)TARG_ULONG_MAX; min = 0; break;
1954 case QUAD: 1954 case QUAD:
1955 max = QUAD_MAX; min = QUAD_MIN; break; 1955 max = QUAD_MAX; min = QUAD_MIN; break;
1956 case UQUAD: 1956 case UQUAD:
1957 max = (uint64_t)UQUAD_MAX; min = 0; break; 1957 max = (uint64_t)UQUAD_MAX; min = 0; break;
1958 case FLOAT: 1958 case FLOAT:
1959 case FCOMPLEX: 1959 case FCOMPLEX:
1960 max = FLT_MAX; min = -FLT_MAX; break; 1960 max = FLT_MAX; min = -FLT_MAX; break;
1961 case DOUBLE: 1961 case DOUBLE:
1962 case DCOMPLEX: 1962 case DCOMPLEX:
1963 max = DBL_MAX; min = -DBL_MAX; break; 1963 max = DBL_MAX; min = -DBL_MAX; break;
1964 case PTR: 1964 case PTR:
1965 /* Got already an error because of float --> ptr */ 1965 /* Got already an error because of float --> ptr */
1966 case LDOUBLE: 1966 case LDOUBLE:
1967 case LCOMPLEX: 1967 case LCOMPLEX:
1968 max = LDBL_MAX; min = -LDBL_MAX; break; 1968 max = LDBL_MAX; min = -LDBL_MAX; break;
1969 default: 1969 default:
1970 lint_assert(/*CONSTCOND*/0); 1970 lint_assert(/*CONSTCOND*/0);
1971 } 1971 }
1972 if (v->v_ldbl > max || v->v_ldbl < min) { 1972 if (v->v_ldbl > max || v->v_ldbl < min) {
1973 lint_assert(nt != LDOUBLE); 1973 lint_assert(nt != LDOUBLE);
1974 if (op == FARG) { 1974 if (op == FARG) {
1975 /* conv. of '%s' to '%s' is out of range, ... */ 1975 /* conv. of '%s' to '%s' is out of range, ... */
1976 warning(295, 1976 warning(295,
1977 type_name(gettyp(ot)), type_name(tp), arg); 1977 type_name(gettyp(ot)), type_name(tp), arg);
1978 } else { 1978 } else {
1979 /* conversion of '%s' to '%s' is out of range */ 1979 /* conversion of '%s' to '%s' is out of range */
1980 warning(119, 1980 warning(119,
1981 type_name(gettyp(ot)), type_name(tp)); 1981 type_name(gettyp(ot)), type_name(tp));
1982 } 1982 }
1983 v->v_ldbl = v->v_ldbl > 0 ? max : min; 1983 v->v_ldbl = v->v_ldbl > 0 ? max : min;
1984 } 1984 }
1985 if (nt == FLOAT) { 1985 if (nt == FLOAT) {
1986 nv->v_ldbl = (float)v->v_ldbl; 1986 nv->v_ldbl = (float)v->v_ldbl;
1987 } else if (nt == DOUBLE) { 1987 } else if (nt == DOUBLE) {
1988 nv->v_ldbl = (double)v->v_ldbl; 1988 nv->v_ldbl = (double)v->v_ldbl;
1989 } else if (nt == LDOUBLE) { 1989 } else if (nt == LDOUBLE) {
1990 nv->v_ldbl = v->v_ldbl; 1990 nv->v_ldbl = v->v_ldbl;
1991 } else { 1991 } else {
1992 nv->v_quad = (nt == PTR || tspec_is_uint(nt)) ? 1992 nv->v_quad = (nt == PTR || tspec_is_uint(nt)) ?
1993 (int64_t)v->v_ldbl : (int64_t)v->v_ldbl; 1993 (int64_t)v->v_ldbl : (int64_t)v->v_ldbl;
1994 } 1994 }
1995 } else { 1995 } else {
1996 if (nt == FLOAT) { 1996 if (nt == FLOAT) {
1997 nv->v_ldbl = (ot == PTR || tspec_is_uint(ot)) ? 1997 nv->v_ldbl = (ot == PTR || tspec_is_uint(ot)) ?
1998 (float)(uint64_t)v->v_quad : (float)v->v_quad; 1998 (float)(uint64_t)v->v_quad : (float)v->v_quad;
1999 } else if (nt == DOUBLE) { 1999 } else if (nt == DOUBLE) {
2000 nv->v_ldbl = (ot == PTR || tspec_is_uint(ot)) ? 2000 nv->v_ldbl = (ot == PTR || tspec_is_uint(ot)) ?
2001 (double)(uint64_t)v->v_quad : (double)v->v_quad; 2001 (double)(uint64_t)v->v_quad : (double)v->v_quad;
2002 } else if (nt == LDOUBLE) { 2002 } else if (nt == LDOUBLE) {
2003 nv->v_ldbl = (ot == PTR || tspec_is_uint(ot)) ? 2003 nv->v_ldbl = (ot == PTR || tspec_is_uint(ot)) ?
2004 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad; 2004 (ldbl_t)(uint64_t)v->v_quad : (ldbl_t)v->v_quad;
2005 } else { 2005 } else {
2006 rchk = 1; /* Check for lost precision. */ 2006 rchk = 1; /* Check for lost precision. */
2007 nv->v_quad = v->v_quad; 2007 nv->v_quad = v->v_quad;
2008 } 2008 }
2009 } 2009 }
2010 2010
2011 if (v->v_ansiu && tspec_is_float(nt)) { 2011 if (v->v_ansiu && tspec_is_float(nt)) {
2012 /* ANSI C treats constant as unsigned */ 2012 /* ANSI C treats constant as unsigned */
2013 warning(157); 2013 warning(157);
2014 v->v_ansiu = 0; 2014 v->v_ansiu = 0;
2015 } else if (v->v_ansiu && (tspec_is_int(nt) && !tspec_is_uint(nt) && 2015 } else if (v->v_ansiu && (tspec_is_int(nt) && !tspec_is_uint(nt) &&
2016 psize(nt) > psize(ot))) { 2016 psize(nt) > psize(ot))) {
2017 /* ANSI C treats constant as unsigned */ 2017 /* ANSI C treats constant as unsigned */
2018 warning(157); 2018 warning(157);
2019 v->v_ansiu = 0; 2019 v->v_ansiu = 0;
2020 } 2020 }
2021 2021
2022 switch (nt) { 2022 switch (nt) {
2023 case FLOAT: 2023 case FLOAT:
2024 case FCOMPLEX: 2024 case FCOMPLEX:
2025 case DOUBLE: 2025 case DOUBLE:
2026 case DCOMPLEX: 2026 case DCOMPLEX:
2027 case LDOUBLE: 2027 case LDOUBLE:
2028 case LCOMPLEX: 2028 case LCOMPLEX:
2029 break; 2029 break;
2030 default: 2030 default:
2031 sz = tp->t_isfield ? tp->t_flen : size(nt); 2031 sz = tp->t_bitfield ? tp->t_flen : size(nt);
2032 nv->v_quad = xsign(nv->v_quad, nt, sz); 2032 nv->v_quad = xsign(nv->v_quad, nt, sz);
2033 break; 2033 break;
2034 } 2034 }
2035 2035
2036 if (rchk && op != CVT) { 2036 if (rchk && op != CVT) {
2037 osz = size(ot); 2037 osz = size(ot);
2038 nsz = tp->t_isfield ? tp->t_flen : size(nt); 2038 nsz = tp->t_bitfield ? tp->t_flen : size(nt);
2039 xmask = qlmasks[nsz] ^ qlmasks[osz]; 2039 xmask = qlmasks[nsz] ^ qlmasks[osz];
2040 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1]; 2040 xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
2041 /* 2041 /*
2042 * For bitwise operations we are not interested in the 2042 * For bitwise operations we are not interested in the
2043 * value, but in the bits itself. 2043 * value, but in the bits itself.
2044 */ 2044 */
2045 if (op == ORASS || op == OR || op == XOR) { 2045 if (op == ORASS || op == OR || op == XOR) {
2046 /* 2046 /*
2047 * Print a warning if bits which were set are 2047 * Print a warning if bits which were set are
2048 * lost due to the conversion. 2048 * lost due to the conversion.
2049 * This can happen with operator ORASS only. 2049 * This can happen with operator ORASS only.
2050 */ 2050 */
2051 if (nsz < osz && (v->v_quad & xmask) != 0) { 2051 if (nsz < osz && (v->v_quad & xmask) != 0) {
2052 /* constant truncated by conv., op %s */ 2052 /* constant truncated by conv., op %s */
2053 warning(306, modtab[op].m_name); 2053 warning(306, modtab[op].m_name);
2054 } 2054 }
2055 } else if (op == ANDASS || op == AND) { 2055 } else if (op == ANDASS || op == AND) {
2056 /* 2056 /*
2057 * Print a warning if additional bits are not all 1 2057 * Print a warning if additional bits are not all 1
2058 * and the most significant bit of the old value is 1, 2058 * and the most significant bit of the old value is 1,
2059 * or if at least one (but not all) removed bit was 0. 2059 * or if at least one (but not all) removed bit was 0.
2060 */ 2060 */
2061 if (nsz > osz && 2061 if (nsz > osz &&
2062 (nv->v_quad & qbmasks[osz - 1]) != 0 && 2062 (nv->v_quad & qbmasks[osz - 1]) != 0 &&
2063 (nv->v_quad & xmask) != xmask) { 2063 (nv->v_quad & xmask) != xmask) {
2064 /* extra bits set to 0 in conv. of '%s' ... */ 2064 /* extra bits set to 0 in conv. of '%s' ... */
2065 warning(309, type_name(gettyp(ot)), 2065 warning(309, type_name(gettyp(ot)),
2066 type_name(tp), modtab[op].m_name); 2066 type_name(tp), modtab[op].m_name);
2067 } else if (nsz < osz && 2067 } else if (nsz < osz &&
2068 (v->v_quad & xmask) != xmask && 2068 (v->v_quad & xmask) != xmask &&
2069 (v->v_quad & xmask) != 0) { 2069 (v->v_quad & xmask) != 0) {
2070 /* constant truncated by conv., op %s */ 2070 /* constant truncated by conv., op %s */
2071 warning(306, modtab[op].m_name); 2071 warning(306, modtab[op].m_name);
2072 } 2072 }
2073 } else if ((nt != PTR && tspec_is_uint(nt)) && 2073 } else if ((nt != PTR && tspec_is_uint(nt)) &&
2074 (ot != PTR && !tspec_is_uint(ot)) && v->v_quad < 0) { 2074 (ot != PTR && !tspec_is_uint(ot)) && v->v_quad < 0) {
2075 if (op == ASSIGN) { 2075 if (op == ASSIGN) {
2076 /* assignment of negative constant to ... */ 2076 /* assignment of negative constant to ... */
2077 warning(164); 2077 warning(164);
2078 } else if (op == INIT) { 2078 } else if (op == INIT) {
2079 /* initialisation of unsigned with neg... */ 2079 /* initialisation of unsigned with neg... */
2080 warning(221); 2080 warning(221);
2081 } else if (op == FARG) { 2081 } else if (op == FARG) {
2082 /* conversion of negative constant to ... */ 2082 /* conversion of negative constant to ... */
2083 warning(296, arg); 2083 warning(296, arg);
2084 } else if (modtab[op].m_comp) { 2084 } else if (modtab[op].m_comp) {
2085 /* handled by check_integer_comparison() */ 2085 /* handled by check_integer_comparison() */
2086 } else { 2086 } else {
2087 /* conversion of negative constant to ... */ 2087 /* conversion of negative constant to ... */
2088 warning(222); 2088 warning(222);
2089 } 2089 }
2090 } else if (nv->v_quad != v->v_quad && nsz <= osz && 2090 } else if (nv->v_quad != v->v_quad && nsz <= osz &&
2091 (v->v_quad & xmask) != 0 && 2091 (v->v_quad & xmask) != 0 &&
2092 (tspec_is_uint(ot) || 2092 (tspec_is_uint(ot) ||
2093 (v->v_quad & xmsk1) != xmsk1)) { 2093 (v->v_quad & xmsk1) != xmsk1)) {
2094 /* 2094 /*
2095 * Loss of significant bit(s). All truncated bits 2095 * Loss of significant bit(s). All truncated bits
2096 * of unsigned types or all truncated bits plus the 2096 * of unsigned types or all truncated bits plus the
2097 * msb of the target for signed types are considered 2097 * msb of the target for signed types are considered
2098 * to be significant bits. Loss of significant bits 2098 * to be significant bits. Loss of significant bits
2099 * means that at least on of the bits was set in an 2099 * means that at least on of the bits was set in an
2100 * unsigned type or that at least one, but not all of 2100 * unsigned type or that at least one, but not all of
2101 * the bits was set in an signed type. 2101 * the bits was set in an signed type.
2102 * Loss of significant bits means that it is not 2102 * Loss of significant bits means that it is not
2103 * possible, also not with necessary casts, to convert 2103 * possible, also not with necessary casts, to convert
2104 * back to the original type. A example for a 2104 * back to the original type. A example for a
2105 * necessary cast is: 2105 * necessary cast is:
2106 * char c; int i; c = 128; 2106 * char c; int i; c = 128;
2107 * i = c; ** yields -128 ** 2107 * i = c; ** yields -128 **
2108 * i = (unsigned char)c; ** yields 128 ** 2108 * i = (unsigned char)c; ** yields 128 **
2109 */ 2109 */
2110 if (op == ASSIGN && tp->t_isfield) { 2110 if (op == ASSIGN && tp->t_bitfield) {
2111 /* precision lost in bit-field assignment */ 2111 /* precision lost in bit-field assignment */
2112 warning(166); 2112 warning(166);
2113 } else if (op == ASSIGN) { 2113 } else if (op == ASSIGN) {
2114 /* constant truncated by assignment */ 2114 /* constant truncated by assignment */
2115 warning(165); 2115 warning(165);
2116 } else if (op == INIT && tp->t_isfield) { 2116 } else if (op == INIT && tp->t_bitfield) {
2117 /* bit-field initializer does not fit */ 2117 /* bit-field initializer does not fit */
2118 warning(180); 2118 warning(180);
2119 } else if (op == INIT) { 2119 } else if (op == INIT) {
2120 /* initializer does not fit */ 2120 /* initializer does not fit */
2121 warning(178); 2121 warning(178);
2122 } else if (op == CASE) { 2122 } else if (op == CASE) {
2123 /* case label affected by conversion */ 2123 /* case label affected by conversion */
2124 warning(196); 2124 warning(196);
2125 } else if (op == FARG) { 2125 } else if (op == FARG) {
2126 /* conv. of '%s' to '%s' is out of range, ... */ 2126 /* conv. of '%s' to '%s' is out of range, ... */
2127 warning(295, 2127 warning(295,
2128 type_name(gettyp(ot)), type_name(tp), arg); 2128 type_name(gettyp(ot)), type_name(tp), arg);
2129 } else { 2129 } else {
2130 /* conversion of '%s' to '%s' is out of range */ 2130 /* conversion of '%s' to '%s' is out of range */
2131 warning(119, 2131 warning(119,
2132 type_name(gettyp(ot)), type_name(tp)); 2132 type_name(gettyp(ot)), type_name(tp));
2133 } 2133 }
2134 } else if (nv->v_quad != v->v_quad) { 2134 } else if (nv->v_quad != v->v_quad) {
2135 if (op == ASSIGN && tp->t_isfield) { 2135 if (op == ASSIGN && tp->t_bitfield) {
2136 /* precision lost in bit-field assignment */ 2136 /* precision lost in bit-field assignment */
2137 warning(166); 2137 warning(166);
2138 } else if (op == INIT && tp->t_isfield) { 2138 } else if (op == INIT && tp->t_bitfield) {
2139 /* bit-field initializer out of range */ 2139 /* bit-field initializer out of range */
2140 warning(11); 2140 warning(11);
2141 } else if (op == CASE) { 2141 } else if (op == CASE) {
2142 /* case label affected by conversion */ 2142 /* case label affected by conversion */
2143 warning(196); 2143 warning(196);
2144 } else if (op == FARG) { 2144 } else if (op == FARG) {
2145 /* conv. of '%s' to '%s' is out of range, ... */ 2145 /* conv. of '%s' to '%s' is out of range, ... */
2146 warning(295, 2146 warning(295,
2147 type_name(gettyp(ot)), type_name(tp), arg); 2147 type_name(gettyp(ot)), type_name(tp), arg);
2148 } else { 2148 } else {
2149 /* conversion of '%s' to '%s' is out of range */ 2149 /* conversion of '%s' to '%s' is out of range */
2150 warning(119, 2150 warning(119,
2151 type_name(gettyp(ot)), type_name(tp)); 2151 type_name(gettyp(ot)), type_name(tp));
2152 } 2152 }
2153 } 2153 }
2154 } 2154 }
2155} 2155}
2156 2156
2157/* 2157/*
2158 * Called if incompatible types were detected. 2158 * Called if incompatible types were detected.
2159 * Prints a appropriate warning. 2159 * Prints a appropriate warning.
2160 */ 2160 */
2161static void 2161static void
2162warn_incompatible_types(op_t op, tspec_t lt, tspec_t rt) 2162warn_incompatible_types(op_t op, tspec_t lt, tspec_t rt)
2163{ 2163{
2164 mod_t *mp; 2164 mod_t *mp;
2165 2165
2166 mp = &modtab[op]; 2166 mp = &modtab[op];
2167 2167
2168 if (lt == VOID || (mp->m_binary && rt == VOID)) { 2168 if (lt == VOID || (mp->m_binary && rt == VOID)) {
2169 /* void type illegal in expression */ 2169 /* void type illegal in expression */
2170 error(109); 2170 error(109);
2171 } else if (op == ASSIGN) { 2171 } else if (op == ASSIGN) {
2172 if ((lt == STRUCT || lt == UNION) && 2172 if ((lt == STRUCT || lt == UNION) &&
2173 (rt == STRUCT || rt == UNION)) { 2173 (rt == STRUCT || rt == UNION)) {
2174 /* assignment of different structures (%s != %s) */ 2174 /* assignment of different structures (%s != %s) */
2175 error(240, tspec_name(lt), tspec_name(rt)); 2175 error(240, tspec_name(lt), tspec_name(rt));
2176 } else { 2176 } else {
2177 /* assignment type mismatch (%s != %s) */ 2177 /* assignment type mismatch (%s != %s) */
2178 error(171, tspec_name(lt), tspec_name(rt)); 2178 error(171, tspec_name(lt), tspec_name(rt));
2179 } 2179 }
2180 } else if (mp->m_binary) { 2180 } else if (mp->m_binary) {
2181 /* operands of '%s' have incompatible types (%s != %s) */ 2181 /* operands of '%s' have incompatible types (%s != %s) */
2182 error(107, mp->m_name, tspec_name(lt), tspec_name(rt)); 2182 error(107, mp->m_name, tspec_name(lt), tspec_name(rt));
2183 } else { 2183 } else {
2184 /* operand of '%s' has incompatible type (%s != %s) */ 2184 /* operand of '%s' has incompatible type (%s != %s) */
2185 error(108, mp->m_name, tspec_name(lt), tspec_name(rt)); 2185 error(108, mp->m_name, tspec_name(lt), tspec_name(rt));
2186 } 2186 }
2187} 2187}
2188 2188
2189/* 2189/*
2190 * Called if incompatible pointer types are detected. 2190 * Called if incompatible pointer types are detected.
2191 * Print an appropriate warning. 2191 * Print an appropriate warning.
2192 */ 2192 */
2193static void 2193static void
2194warn_incompatible_pointers(mod_t *mp, type_t *ltp, type_t *rtp) 2194warn_incompatible_pointers(mod_t *mp, type_t *ltp, type_t *rtp)
2195{ 2195{
2196 tspec_t lt, rt; 2196 tspec_t lt, rt;
2197 2197
2198 lint_assert(ltp->t_tspec == PTR); 2198 lint_assert(ltp->t_tspec == PTR);
2199 lint_assert(rtp->t_tspec == PTR); 2199 lint_assert(rtp->t_tspec == PTR);
2200 2200
2201 lt = ltp->t_subt->t_tspec; 2201 lt = ltp->t_subt->t_tspec;
2202 rt = rtp->t_subt->t_tspec; 2202 rt = rtp->t_subt->t_tspec;
2203 2203
2204 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) { 2204 if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
2205 if (mp == NULL) { 2205 if (mp == NULL) {
2206 /* illegal structure pointer combination */ 2206 /* illegal structure pointer combination */
2207 warning(244); 2207 warning(244);
2208 } else { 2208 } else {
2209 /* illegal structure pointer combination, op %s */ 2209 /* illegal structure pointer combination, op %s */
2210 warning(245, mp->m_name); 2210 warning(245, mp->m_name);
2211 } 2211 }
2212 } else { 2212 } else {
2213 if (mp == NULL) { 2213 if (mp == NULL) {
2214 /* illegal pointer combination */ 2214 /* illegal pointer combination */
2215 warning(184); 2215 warning(184);
2216 } else { 2216 } else {
2217 /* illegal pointer combination (%s) and (%s), op %s */ 2217 /* illegal pointer combination (%s) and (%s), op %s */
2218 warning(124, 2218 warning(124,
2219 type_name(ltp), type_name(rtp), mp->m_name); 2219 type_name(ltp), type_name(rtp), mp->m_name);
2220 } 2220 }
2221 } 2221 }
2222} 2222}
2223 2223
2224/* 2224/*
2225 * Make sure type (*tpp)->t_subt has at least the qualifiers 2225 * Make sure type (*tpp)->t_subt has at least the qualifiers
2226 * of tp1->t_subt and tp2->t_subt. 2226 * of tp1->t_subt and tp2->t_subt.
2227 */ 2227 */
2228static void 2228static void
2229merge_qualifiers(type_t **tpp, type_t *tp1, type_t *tp2) 2229merge_qualifiers(type_t **tpp, type_t *tp1, type_t *tp2)
2230{ 2230{
2231 2231
2232 lint_assert((*tpp)->t_tspec == PTR); 2232 lint_assert((*tpp)->t_tspec == PTR);
2233 lint_assert(tp1->t_tspec == PTR); 2233 lint_assert(tp1->t_tspec == PTR);
2234 lint_assert(tp2->t_tspec == PTR); 2234 lint_assert(tp2->t_tspec == PTR);
2235 2235
2236 if ((*tpp)->t_subt->t_const == 2236 if ((*tpp)->t_subt->t_const ==
2237 (tp1->t_subt->t_const | tp2->t_subt->t_const) && 2237 (tp1->t_subt->t_const | tp2->t_subt->t_const) &&
2238 (*tpp)->t_subt->t_volatile == 2238 (*tpp)->t_subt->t_volatile ==
2239 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) { 2239 (tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
2240 return; 2240 return;
2241 } 2241 }
2242 2242
2243 *tpp = tduptyp(*tpp); 2243 *tpp = tduptyp(*tpp);
2244 (*tpp)->t_subt = tduptyp((*tpp)->t_subt); 2244 (*tpp)->t_subt = tduptyp((*tpp)->t_subt);
2245 (*tpp)->t_subt->t_const = 2245 (*tpp)->t_subt->t_const =
2246 tp1->t_subt->t_const | tp2->t_subt->t_const; 2246 tp1->t_subt->t_const | tp2->t_subt->t_const;
2247 (*tpp)->t_subt->t_volatile = 2247 (*tpp)->t_subt->t_volatile =
2248 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile; 2248 tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
2249} 2249}
2250 2250
2251/* 2251/*
2252 * Returns 1 if the given structure or union has a constant member 2252 * Returns 1 if the given structure or union has a constant member
2253 * (maybe recursively). 2253 * (maybe recursively).
2254 */ 2254 */
2255static int 2255static int
2256has_constant_member(type_t *tp) 2256has_constant_member(type_t *tp)
2257{ 2257{
2258 sym_t *m; 2258 sym_t *m;
2259 tspec_t t; 2259 tspec_t t;
2260 2260
2261 lint_assert((t = tp->t_tspec) == STRUCT || t == UNION); 2261 lint_assert((t = tp->t_tspec) == STRUCT || t == UNION);
2262 2262
2263 for (m = tp->t_str->memb; m != NULL; m = m->s_next) { 2263 for (m = tp->t_str->memb; m != NULL; m = m->s_next) {
2264 tp = m->s_type; 2264 tp = m->s_type;
2265 if (tp->t_const) 2265 if (tp->t_const)
2266 return 1; 2266 return 1;
2267 if ((t = tp->t_tspec) == STRUCT || t == UNION) { 2267 if ((t = tp->t_tspec) == STRUCT || t == UNION) {
2268 if (has_constant_member(m->s_type)) 2268 if (has_constant_member(m->s_type))
2269 return 1; 2269 return 1;
2270 } 2270 }
2271 } 2271 }
2272 return 0; 2272 return 0;
2273} 2273}
2274 2274
2275/* 2275/*
2276 * Create a new node for one of the operators POINT and ARROW. 2276 * Create a new node for one of the operators POINT and ARROW.
2277 */ 2277 */
2278static tnode_t * 2278static tnode_t *
2279build_struct_access(op_t op, tnode_t *ln, tnode_t *rn) 2279build_struct_access(op_t op, tnode_t *ln, tnode_t *rn)
2280{ 2280{
2281 tnode_t *ntn, *ctn; 2281 tnode_t *ntn, *ctn;
2282 int nolval; 2282 int nolval;
2283 2283
2284 lint_assert(rn->tn_op == NAME); 2284 lint_assert(rn->tn_op == NAME);
2285 lint_assert(rn->tn_sym->s_value.v_tspec == INT); 2285 lint_assert(rn->tn_sym->s_value.v_tspec == INT);
2286 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU); 2286 lint_assert(rn->tn_sym->s_scl == MOS || rn->tn_sym->s_scl == MOU);
2287 2287
2288 /* 2288 /*
2289 * Remember if the left operand is an lvalue (structure members 2289 * Remember if the left operand is an lvalue (structure members
2290 * are lvalues if and only if the structure itself is an lvalue). 2290 * are lvalues if and only if the structure itself is an lvalue).
2291 */ 2291 */
2292 nolval = op == POINT && !ln->tn_lvalue; 2292 nolval = op == POINT && !ln->tn_lvalue;
2293 2293
2294 if (op == POINT) { 2294 if (op == POINT) {
2295 ln = build_ampersand(ln, 1); 2295 ln = build_ampersand(ln, 1);
2296 } else if (ln->tn_type->t_tspec != PTR) { 2296 } else if (ln->tn_type->t_tspec != PTR) {
2297 lint_assert(tflag); 2297 lint_assert(tflag);
2298 lint_assert(tspec_is_int(ln->tn_type->t_tspec)); 2298 lint_assert(tspec_is_int(ln->tn_type->t_tspec));
2299 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln); 2299 ln = convert(NOOP, 0, tincref(gettyp(VOID), PTR), ln);
2300 } 2300 }
2301 2301
2302#if PTRDIFF_IS_LONG 2302#if PTRDIFF_IS_LONG
2303 ctn = new_int_const_node(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2303 ctn = new_int_const_node(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2304#else 2304#else
2305 ctn = new_int_const_node(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT); 2305 ctn = new_int_const_node(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
2306#endif 2306#endif
2307 2307
2308 ntn = new_tnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn); 2308 ntn = new_tnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
2309 if (ln->tn_op == CON) 2309 if (ln->tn_op == CON)
2310 ntn = fold(ntn); 2310 ntn = fold(ntn);
2311 2311
2312 if (rn->tn_type->t_isfield) { 2312 if (rn->tn_type->t_bitfield) {
2313 ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL); 2313 ntn = new_tnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
2314 } else { 2314 } else {
2315 ntn = new_tnode(STAR, ntn->tn_type->t_subt, ntn, NULL); 2315 ntn = new_tnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
2316 } 2316 }
2317 2317
2318 if (nolval) 2318 if (nolval)
2319 ntn->tn_lvalue = 0; 2319 ntn->tn_lvalue = 0;
2320 2320
2321 return ntn; 2321 return ntn;
2322} 2322}
2323 2323
2324/* 2324/*
2325 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF. 2325 * Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
2326 */ 2326 */
2327static tnode_t * 2327static tnode_t *
2328build_prepost_incdec(op_t op, tnode_t *ln) 2328build_prepost_incdec(op_t op, tnode_t *ln)
2329{ 2329{
2330 tnode_t *cn, *ntn; 2330 tnode_t *cn, *ntn;
2331 2331
2332 lint_assert(ln != NULL); 2332 lint_assert(ln != NULL);
2333 2333
2334 if (ln->tn_type->t_tspec == PTR) { 2334 if (ln->tn_type->t_tspec == PTR) {
2335 cn = plength(ln->tn_type); 2335 cn = plength(ln->tn_type);
2336 } else { 2336 } else {
2337 cn = new_int_const_node(INT, (int64_t)1); 2337 cn = new_int_const_node(INT, (int64_t)1);
2338 } 2338 }
2339 ntn = new_tnode(op, ln->tn_type, ln, cn); 2339 ntn = new_tnode(op, ln->tn_type, ln, cn);
2340 2340
2341 return ntn; 2341 return ntn;
2342} 2342}
2343 2343
2344/* 2344/*
2345 * Create a node for REAL, IMAG 2345 * Create a node for REAL, IMAG
2346 */ 2346 */
2347static tnode_t * 2347static tnode_t *
2348build_real_imag(op_t op, tnode_t *ln) 2348build_real_imag(op_t op, tnode_t *ln)
2349{ 2349{
2350 tnode_t *cn, *ntn; 2350 tnode_t *cn, *ntn;
2351 2351
2352 lint_assert(ln != NULL); 2352 lint_assert(ln != NULL);
2353 2353
2354 switch (ln->tn_type->t_tspec) { 2354 switch (ln->tn_type->t_tspec) {
2355 case LCOMPLEX: 2355 case LCOMPLEX:
2356 cn = new_int_const_node(LDOUBLE, (int64_t)1); 2356 cn = new_int_const_node(LDOUBLE, (int64_t)1);
2357 break; 2357 break;
2358 case DCOMPLEX: 2358 case DCOMPLEX:
2359 cn = new_int_const_node(DOUBLE, (int64_t)1); 2359 cn = new_int_const_node(DOUBLE, (int64_t)1);
2360 break; 2360 break;
2361 case FCOMPLEX: 2361 case FCOMPLEX:
2362 cn = new_int_const_node(FLOAT, (int64_t)1); 2362 cn = new_int_const_node(FLOAT, (int64_t)1);
2363 break; 2363 break;
2364 default: 2364 default:
2365 /* __%s__ is illegal for type %s */ 2365 /* __%s__ is illegal for type %s */
2366 error(276, op == REAL ? "real" : "imag", 2366 error(276, op == REAL ? "real" : "imag",
2367 type_name(ln->tn_type)); 2367 type_name(ln->tn_type));
2368 return NULL; 2368 return NULL;
2369 } 2369 }
2370 ntn = new_tnode(op, cn->tn_type, ln, cn); 2370 ntn = new_tnode(op, cn->tn_type, ln, cn);
2371 ntn->tn_lvalue = 1; 2371 ntn->tn_lvalue = 1;
2372 2372
2373 return ntn; 2373 return ntn;
2374} 2374}
2375/* 2375/*
2376 * Create a tree node for the & operator 2376 * Create a tree node for the & operator
2377 */ 2377 */
2378static tnode_t * 2378static tnode_t *
2379build_ampersand(tnode_t *tn, int noign) 2379build_ampersand(tnode_t *tn, int noign)
2380{ 2380{
2381 tnode_t *ntn; 2381 tnode_t *ntn;
2382 tspec_t t; 2382 tspec_t t;
2383 2383
2384 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) { 2384 if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
2385 if (tflag) 2385 if (tflag)
2386 /* '&' before array or function: ignored */ 2386 /* '&' before array or function: ignored */
2387 warning(127); 2387 warning(127);
2388 return tn; 2388 return tn;
2389 } 2389 }
2390 2390
2391 /* eliminate &* */ 2391 /* eliminate &* */
2392 if (tn->tn_op == STAR && 2392 if (tn->tn_op == STAR &&
2393 tn->tn_left->tn_type->t_tspec == PTR && 2393 tn->tn_left->tn_type->t_tspec == PTR &&
2394 tn->tn_left->tn_type->t_subt == tn->tn_type) { 2394 tn->tn_left->tn_type->t_subt == tn->tn_type) {
2395 return tn->tn_left; 2395 return tn->tn_left;
2396 } 2396 }
2397 2397
2398 ntn = new_tnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL); 2398 ntn = new_tnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
2399 2399
2400 return ntn; 2400 return ntn;
2401} 2401}
2402 2402
2403/* 2403/*
2404 * Create a node for operators PLUS and MINUS. 2404 * Create a node for operators PLUS and MINUS.
2405 */ 2405 */
2406static tnode_t * 2406static tnode_t *
2407build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn) 2407build_plus_minus(op_t op, tnode_t *ln, tnode_t *rn)
2408{ 2408{
2409 tnode_t *ntn, *ctn; 2409 tnode_t *ntn, *ctn;
2410 type_t *tp; 2410 type_t *tp;
2411 2411
2412 /* If pointer and integer, then pointer to the lhs. */ 2412 /* If pointer and integer, then pointer to the lhs. */
2413 if (rn->tn_type->t_tspec == PTR && 2413 if (rn->tn_type->t_tspec == PTR &&
2414 tspec_is_int(ln->tn_type->t_tspec)) { 2414 tspec_is_int(ln->tn_type->t_tspec)) {
2415 ntn = ln; 2415 ntn = ln;
2416 ln = rn; 2416 ln = rn;
2417 rn = ntn; 2417 rn = ntn;
2418 } 2418 }
2419 2419
2420 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) { 2420 if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
2421 2421
2422 lint_assert(tspec_is_int(rn->tn_type->t_tspec)); 2422 lint_assert(tspec_is_int(rn->tn_type->t_tspec));
2423 2423
2424 ctn = plength(ln->tn_type); 2424 ctn = plength(ln->tn_type);
2425 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2425 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2426 rn = convert(NOOP, 0, ctn->tn_type, rn); 2426 rn = convert(NOOP, 0, ctn->tn_type, rn);
2427 rn = new_tnode(MULT, rn->tn_type, rn, ctn); 2427 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2428 if (rn->tn_left->tn_op == CON) 2428 if (rn->tn_left->tn_op == CON)
2429 rn = fold(rn); 2429 rn = fold(rn);
2430 ntn = new_tnode(op, ln->tn_type, ln, rn); 2430 ntn = new_tnode(op, ln->tn_type, ln, rn);
2431 2431
2432 } else if (rn->tn_type->t_tspec == PTR) { 2432 } else if (rn->tn_type->t_tspec == PTR) {
2433 2433
2434 lint_assert(ln->tn_type->t_tspec == PTR); 2434 lint_assert(ln->tn_type->t_tspec == PTR);
2435 lint_assert(op == MINUS); 2435 lint_assert(op == MINUS);
2436#if PTRDIFF_IS_LONG 2436#if PTRDIFF_IS_LONG
2437 tp = gettyp(LONG); 2437 tp = gettyp(LONG);
2438#else 2438#else
2439 tp = gettyp(INT); 2439 tp = gettyp(INT);
2440#endif 2440#endif
2441 ntn = new_tnode(op, tp, ln, rn); 2441 ntn = new_tnode(op, tp, ln, rn);
2442 if (ln->tn_op == CON && rn->tn_op == CON) 2442 if (ln->tn_op == CON && rn->tn_op == CON)
2443 ntn = fold(ntn); 2443 ntn = fold(ntn);
2444 ctn = plength(ln->tn_type); 2444 ctn = plength(ln->tn_type);
2445 balance(NOOP, &ntn, &ctn); 2445 balance(NOOP, &ntn, &ctn);
2446 ntn = new_tnode(DIV, tp, ntn, ctn); 2446 ntn = new_tnode(DIV, tp, ntn, ctn);
2447 2447
2448 } else { 2448 } else {
2449 2449
2450 ntn = new_tnode(op, ln->tn_type, ln, rn); 2450 ntn = new_tnode(op, ln->tn_type, ln, rn);
2451 2451
2452 } 2452 }
2453 return ntn; 2453 return ntn;
2454} 2454}
2455 2455
2456/* 2456/*
2457 * Create a node for operators SHL and SHR. 2457 * Create a node for operators SHL and SHR.
2458 */ 2458 */
2459static tnode_t * 2459static tnode_t *
2460build_bit_shift(op_t op, tnode_t *ln, tnode_t *rn) 2460build_bit_shift(op_t op, tnode_t *ln, tnode_t *rn)
2461{ 2461{
2462 tspec_t t; 2462 tspec_t t;
2463 tnode_t *ntn; 2463 tnode_t *ntn;
2464 2464
2465 if ((t = rn->tn_type->t_tspec) != INT && t != UINT) 2465 if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
2466 rn = convert(CVT, 0, gettyp(INT), rn); 2466 rn = convert(CVT, 0, gettyp(INT), rn);
2467 ntn = new_tnode(op, ln->tn_type, ln, rn); 2467 ntn = new_tnode(op, ln->tn_type, ln, rn);
2468 return ntn; 2468 return ntn;
2469} 2469}
2470 2470
2471/* 2471/*
2472 * Create a node for COLON. 2472 * Create a node for COLON.
2473 */ 2473 */
2474static tnode_t * 2474static tnode_t *
2475build_colon(tnode_t *ln, tnode_t *rn) 2475build_colon(tnode_t *ln, tnode_t *rn)
2476{ 2476{
2477 tspec_t lt, rt, pdt; 2477 tspec_t lt, rt, pdt;
2478 type_t *rtp; 2478 type_t *rtp;
2479 tnode_t *ntn; 2479 tnode_t *ntn;
2480 2480
2481 lt = ln->tn_type->t_tspec; 2481 lt = ln->tn_type->t_tspec;
2482 rt = rn->tn_type->t_tspec; 2482 rt = rn->tn_type->t_tspec;
2483#if PTRDIFF_IS_LONG 2483#if PTRDIFF_IS_LONG
2484 pdt = LONG; 2484 pdt = LONG;
2485#else 2485#else
2486 pdt = INT; 2486 pdt = INT;
2487#endif 2487#endif
2488 2488
2489 /* 2489 /*
2490 * Arithmetic types are balanced, all other type combinations 2490 * Arithmetic types are balanced, all other type combinations
2491 * still need to be handled. 2491 * still need to be handled.
2492 */ 2492 */
2493 if (tspec_is_arith(lt) && tspec_is_arith(rt)) { 2493 if (tspec_is_arith(lt) && tspec_is_arith(rt)) {
2494 rtp = ln->tn_type; 2494 rtp = ln->tn_type;
2495 } else if (lt == VOID || rt == VOID) { 2495 } else if (lt == VOID || rt == VOID) {
2496 rtp = gettyp(VOID); 2496 rtp = gettyp(VOID);
2497 } else if (lt == STRUCT || lt == UNION) { 2497 } else if (lt == STRUCT || lt == UNION) {
2498 /* Both types must be identical. */ 2498 /* Both types must be identical. */
2499 lint_assert(rt == STRUCT || rt == UNION); 2499 lint_assert(rt == STRUCT || rt == UNION);
2500 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str); 2500 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2501 if (incompl(ln->tn_type)) { 2501 if (incompl(ln->tn_type)) {
2502 /* unknown operand size, op %s */ 2502 /* unknown operand size, op %s */
2503 error(138, modtab[COLON].m_name); 2503 error(138, modtab[COLON].m_name);
2504 return NULL; 2504 return NULL;
2505 } 2505 }
2506 rtp = ln->tn_type; 2506 rtp = ln->tn_type;
2507 } else if (lt == PTR && tspec_is_int(rt)) { 2507 } else if (lt == PTR && tspec_is_int(rt)) {
2508 if (rt != pdt) { 2508 if (rt != pdt) {
2509 rn = convert(NOOP, 0, gettyp(pdt), rn); 2509 rn = convert(NOOP, 0, gettyp(pdt), rn);
2510 rt = pdt; 2510 rt = pdt;
2511 } 2511 }
2512 rtp = ln->tn_type; 2512 rtp = ln->tn_type;
2513 } else if (rt == PTR && tspec_is_int(lt)) { 2513 } else if (rt == PTR && tspec_is_int(lt)) {
2514 if (lt != pdt) { 2514 if (lt != pdt) {
2515 ln = convert(NOOP, 0, gettyp(pdt), ln); 2515 ln = convert(NOOP, 0, gettyp(pdt), ln);
2516 lt = pdt; 2516 lt = pdt;
2517 } 2517 }
2518 rtp = rn->tn_type; 2518 rtp = rn->tn_type;
2519 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) { 2519 } else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
2520 lint_assert(rt == PTR); 2520 lint_assert(rt == PTR);
2521 rtp = rn->tn_type; 2521 rtp = rn->tn_type;
2522 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type); 2522 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2523 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) { 2523 } else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
2524 lint_assert(lt == PTR); 2524 lint_assert(lt == PTR);
2525 rtp = ln->tn_type; 2525 rtp = ln->tn_type;
2526 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type); 2526 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2527 } else { 2527 } else {
2528 lint_assert(lt == PTR); 2528 lint_assert(lt == PTR);
2529 lint_assert(rt == PTR); 2529 lint_assert(rt == PTR);
2530 /* 2530 /*
2531 * XXX For now we simply take the left type. This is 2531 * XXX For now we simply take the left type. This is
2532 * probably wrong, if one type contains a function prototype 2532 * probably wrong, if one type contains a function prototype
2533 * and the other one, at the same place, only an old style 2533 * and the other one, at the same place, only an old style
2534 * declaration. 2534 * declaration.
2535 */ 2535 */
2536 rtp = ln->tn_type; 2536 rtp = ln->tn_type;
2537 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type); 2537 merge_qualifiers(&rtp, ln->tn_type, rn->tn_type);
2538 } 2538 }
2539 2539
2540 ntn = new_tnode(COLON, rtp, ln, rn); 2540 ntn = new_tnode(COLON, rtp, ln, rn);
2541 2541
2542 return ntn; 2542 return ntn;
2543} 2543}
2544 2544
2545/* 2545/*
2546 * Create a node for an assignment operator (both = and op= ). 2546 * Create a node for an assignment operator (both = and op= ).
2547 */ 2547 */
2548static tnode_t * 2548static tnode_t *
2549build_assignment(op_t op, tnode_t *ln, tnode_t *rn) 2549build_assignment(op_t op, tnode_t *ln, tnode_t *rn)
2550{ 2550{
2551 tspec_t lt, rt; 2551 tspec_t lt, rt;
2552 tnode_t *ntn, *ctn; 2552 tnode_t *ntn, *ctn;
2553 2553
2554 lint_assert(ln != NULL); 2554 lint_assert(ln != NULL);
2555 lint_assert(rn != NULL); 2555 lint_assert(rn != NULL);
2556 2556
2557 lt = ln->tn_type->t_tspec; 2557 lt = ln->tn_type->t_tspec;
2558 rt = rn->tn_type->t_tspec; 2558 rt = rn->tn_type->t_tspec;
2559 2559
2560 if ((op == ADDASS || op == SUBASS) && lt == PTR) { 2560 if ((op == ADDASS || op == SUBASS) && lt == PTR) {
2561 lint_assert(tspec_is_int(rt)); 2561 lint_assert(tspec_is_int(rt));
2562 ctn = plength(ln->tn_type); 2562 ctn = plength(ln->tn_type);
2563 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec) 2563 if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
2564 rn = convert(NOOP, 0, ctn->tn_type, rn); 2564 rn = convert(NOOP, 0, ctn->tn_type, rn);
2565 rn = new_tnode(MULT, rn->tn_type, rn, ctn); 2565 rn = new_tnode(MULT, rn->tn_type, rn, ctn);
2566 if (rn->tn_left->tn_op == CON) 2566 if (rn->tn_left->tn_op == CON)
2567 rn = fold(rn); 2567 rn = fold(rn);
2568 } 2568 }
2569 2569
2570 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) { 2570 if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
2571 lint_assert(lt == rt); 2571 lint_assert(lt == rt);
2572 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str); 2572 lint_assert(ln->tn_type->t_str == rn->tn_type->t_str);
2573 if (incompl(ln->tn_type)) { 2573 if (incompl(ln->tn_type)) {
2574 if (op == RETURN) { 2574 if (op == RETURN) {
2575 /* cannot return incomplete type */ 2575 /* cannot return incomplete type */
2576 error(212); 2576 error(212);
2577 } else { 2577 } else {
2578 /* unknown operand size, op %s */ 2578 /* unknown operand size, op %s */
2579 error(138, modtab[op].m_name); 2579 error(138, modtab[op].m_name);
2580 } 2580 }
2581 return NULL; 2581 return NULL;
2582 } 2582 }
2583 } 2583 }
2584 2584
2585 if (op == SHLASS) { 2585 if (op == SHLASS) {
2586 if (psize(lt) < psize(rt)) { 2586 if (psize(lt) < psize(rt)) {
2587 if (hflag) 2587 if (hflag)
2588 /* semantics of '%s' change in ANSI C; ... */ 2588 /* semantics of '%s' change in ANSI C; ... */
2589 warning(118, "<<="); 2589 warning(118, "<<=");
2590 } 2590 }
2591 } else if (op != SHRASS) { 2591 } else if (op != SHRASS) {
2592 if (op == ASSIGN || lt != PTR) { 2592 if (op == ASSIGN || lt != PTR) {
2593 if (lt != rt || 2593 if (lt != rt ||
2594 (ln->tn_type->t_isfield && rn->tn_op == CON)) { 2594 (ln->tn_type->t_bitfield && rn->tn_op == CON)) {
2595 rn = convert(op, 0, ln->tn_type, rn); 2595 rn = convert(op, 0, ln->tn_type, rn);
2596 rt = lt; 2596 rt = lt;
2597 } 2597 }
2598 } 2598 }
2599 } 2599 }
2600 2600
2601 ntn = new_tnode(op, ln->tn_type, ln, rn); 2601 ntn = new_tnode(op, ln->tn_type, ln, rn);
2602 2602
2603 return ntn; 2603 return ntn;
2604} 2604}
2605 2605
2606/* 2606/*
2607 * Get length of type tp->t_subt. 2607 * Get length of type tp->t_subt.
2608 */ 2608 */
2609static tnode_t * 2609static tnode_t *
2610plength(type_t *tp) 2610plength(type_t *tp)
2611{ 2611{
2612 int elem, elsz; 2612 int elem, elsz;
2613 tspec_t st; 2613 tspec_t st;
2614 2614
2615 lint_assert(tp->t_tspec == PTR); 2615 lint_assert(tp->t_tspec == PTR);
2616 tp = tp->t_subt; 2616 tp = tp->t_subt;
2617 2617
2618 elem = 1; 2618 elem = 1;
2619 elsz = 0; 2619 elsz = 0;
2620 2620
2621 while (tp->t_tspec == ARRAY) { 2621 while (tp->t_tspec == ARRAY) {
2622 elem *= tp->t_dim; 2622 elem *= tp->t_dim;
2623 tp = tp->t_subt; 2623 tp = tp->t_subt;
2624 } 2624 }
2625 2625
2626 switch (tp->t_tspec) { 2626 switch (tp->t_tspec) {
2627 case FUNC: 2627 case FUNC:
2628 /* pointer to function is not allowed here */ 2628 /* pointer to function is not allowed here */
2629 error(110); 2629 error(110);
2630 break; 2630 break;
2631 case VOID: 2631 case VOID:
2632 /* cannot do pointer arithmetic on operand of ... */ 2632 /* cannot do pointer arithmetic on operand of ... */
2633 gnuism(136); 2633 gnuism(136);
2634 break; 2634 break;
2635 case STRUCT: 2635 case STRUCT:
2636 case UNION: 2636 case UNION:
2637 if ((elsz = tp->t_str->size) == 0) 2637 if ((elsz = tp->t_str->size) == 0)
2638 /* cannot do pointer arithmetic on operand of ... */ 2638 /* cannot do pointer arithmetic on operand of ... */
2639 error(136); 2639 error(136);
2640 break; 2640 break;
2641 case ENUM: 2641 case ENUM:
2642 if (incompl(tp)) { 2642 if (incompl(tp)) {
2643 /* cannot do pointer arithmetic on operand of ... */ 2643 /* cannot do pointer arithmetic on operand of ... */
2644 warning(136); 2644 warning(136);
2645 } 2645 }
2646 /* FALLTHROUGH */ 2646 /* FALLTHROUGH */
2647 default: 2647 default:
2648 if ((elsz = size(tp->t_tspec)) == 0) { 2648 if ((elsz = size(tp->t_tspec)) == 0) {
2649 /* cannot do pointer arithmetic on operand of ... */ 2649 /* cannot do pointer arithmetic on operand of ... */
2650 error(136); 2650 error(136);
2651 } else { 2651 } else {
2652 lint_assert(elsz != -1); 2652 lint_assert(elsz != -1);
2653 } 2653 }
2654 break; 2654 break;
2655 } 2655 }
2656 2656
2657 if (elem == 0 && elsz != 0) { 2657 if (elem == 0 && elsz != 0) {
2658 /* cannot do pointer arithmetic on operand of ... */ 2658 /* cannot do pointer arithmetic on operand of ... */
2659 error(136); 2659 error(136);
2660 } 2660 }
2661 2661
2662 if (elsz == 0) 2662 if (elsz == 0)
2663 elsz = CHAR_BIT; 2663 elsz = CHAR_BIT;
2664 2664
2665#if PTRDIFF_IS_LONG 2665#if PTRDIFF_IS_LONG
2666 st = LONG; 2666 st = LONG;
2667#else 2667#else
2668 st = INT; 2668 st = INT;
2669#endif 2669#endif
2670 2670
2671 return new_int_const_node(st, (int64_t)(elem * elsz / CHAR_BIT)); 2671 return new_int_const_node(st, (int64_t)(elem * elsz / CHAR_BIT));
2672} 2672}
2673 2673
2674/* 2674/*
2675 * XXX 2675 * XXX
2676 * Note: There appear to be a number of bugs in detecting overflow in 2676 * Note: There appear to be a number of bugs in detecting overflow in
2677 * this function. An audit and a set of proper regression tests are needed. 2677 * this function. An audit and a set of proper regression tests are needed.
2678 * --Perry Metzger, Nov. 16, 2001 2678 * --Perry Metzger, Nov. 16, 2001
2679 */ 2679 */
2680/* 2680/*
2681 * Do only as much as necessary to compute constant expressions. 2681 * Do only as much as necessary to compute constant expressions.
2682 * Called only if the operator allows folding and (both) operands 2682 * Called only if the operator allows folding and (both) operands
2683 * are constants. 2683 * are constants.
2684 */ 2684 */
2685static tnode_t * 2685static tnode_t *
2686fold(tnode_t *tn) 2686fold(tnode_t *tn)
2687{ 2687{
2688 val_t *v; 2688 val_t *v;
2689 tspec_t t; 2689 tspec_t t;
2690 int utyp, ovfl; 2690 int utyp, ovfl;
2691 int64_t sl, sr = 0, q = 0, mask; 2691 int64_t sl, sr = 0, q = 0, mask;
2692 uint64_t ul, ur = 0; 2692 uint64_t ul, ur = 0;
2693 tnode_t *cn; 2693 tnode_t *cn;
2694 2694
2695 v = xcalloc(1, sizeof (val_t)); 2695 v = xcalloc(1, sizeof (val_t));
2696 v->v_tspec = t = tn->tn_type->t_tspec; 2696 v->v_tspec = t = tn->tn_type->t_tspec;
2697 2697
2698 utyp = t == PTR || tspec_is_uint(t); 2698 utyp = t == PTR || tspec_is_uint(t);
2699 ul = sl = tn->tn_left->tn_val->v_quad; 2699 ul = sl = tn->tn_left->tn_val->v_quad;
2700 if (modtab[tn->tn_op].m_binary) 2700 if (modtab[tn->tn_op].m_binary)
2701 ur = sr = tn->tn_right->tn_val->v_quad; 2701 ur = sr = tn->tn_right->tn_val->v_quad;
2702 2702
2703 mask = qlmasks[size(t)]; 2703 mask = qlmasks[size(t)];
2704 ovfl = 0; 2704 ovfl = 0;
2705 2705
2706 switch (tn->tn_op) { 2706 switch (tn->tn_op) {
2707 case UPLUS: 2707 case UPLUS:
2708 q = sl; 2708 q = sl;
2709 break; 2709 break;
2710 case UMINUS: 2710 case UMINUS:
2711 q = -sl; 2711 q = -sl;
2712 if (sl != 0 && msb(q, t, -1) == msb(sl, t, -1)) 2712 if (sl != 0 && msb(q, t, -1) == msb(sl, t, -1))
2713 ovfl = 1; 2713 ovfl = 1;
2714 break; 2714 break;
2715 case COMPL: 2715 case COMPL:
2716 q = ~sl; 2716 q = ~sl;
2717 break; 2717 break;
2718 case MULT: 2718 case MULT:
2719 if (utyp) { 2719 if (utyp) {
2720 q = ul * ur; 2720 q = ul * ur;
2721 if (q != (q & mask)) 2721 if (q != (q & mask))
2722 ovfl = 1; 2722 ovfl = 1;
2723 else if ((ul != 0) && ((q / ul) != ur)) 2723 else if ((ul != 0) && ((q / ul) != ur))
2724 ovfl = 1; 2724 ovfl = 1;
2725 } else { 2725 } else {
2726 q = sl * sr; 2726 q = sl * sr;
2727 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1))) 2727 if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
2728 ovfl = 1; 2728 ovfl = 1;
2729 } 2729 }
2730 break; 2730 break;
2731 case DIV: 2731 case DIV:
2732 if (sr == 0) { 2732 if (sr == 0) {
2733 /* division by 0 */ 2733 /* division by 0 */
2734 error(139); 2734 error(139);
2735 q = utyp ? UQUAD_MAX : QUAD_MAX; 2735 q = utyp ? UQUAD_MAX : QUAD_MAX;
2736 } else { 2736 } else {
2737 q = utyp ? (int64_t)(ul / ur) : sl / sr; 2737 q = utyp ? (int64_t)(ul / ur) : sl / sr;
2738 } 2738 }
2739 break; 2739 break;
2740 case MOD: 2740 case MOD:
2741 if (sr == 0) { 2741 if (sr == 0) {
2742 /* modulus by 0 */ 2742 /* modulus by 0 */
2743 error(140); 2743 error(140);
2744 q = 0; 2744 q = 0;
2745 } else { 2745 } else {
2746 q = utyp ? (int64_t)(ul % ur) : sl % sr; 2746 q = utyp ? (int64_t)(ul % ur) : sl % sr;
2747 } 2747 }
2748 break; 2748 break;
2749 case PLUS: 2749 case PLUS:
2750 q = utyp ? (int64_t)(ul + ur) : sl + sr; 2750 q = utyp ? (int64_t)(ul + ur) : sl + sr;
2751 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) { 2751 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
2752 if (msb(q, t, -1) == 0) 2752 if (msb(q, t, -1) == 0)
2753 ovfl = 1; 2753 ovfl = 1;
2754 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) { 2754 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
2755 if (msb(q, t, -1) != 0) 2755 if (msb(q, t, -1) != 0)
2756 ovfl = 1; 2756 ovfl = 1;
2757 } 2757 }
2758 break; 2758 break;
2759 case MINUS: 2759 case MINUS:
2760 q = utyp ? (int64_t)(ul - ur) : sl - sr; 2760 q = utyp ? (int64_t)(ul - ur) : sl - sr;
2761 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) { 2761 if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
2762 if (msb(q, t, -1) == 0) 2762 if (msb(q, t, -1) == 0)
2763 ovfl = 1; 2763 ovfl = 1;
2764 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) { 2764 } else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
2765 if (msb(q, t, -1) != 0) 2765 if (msb(q, t, -1) != 0)
2766 ovfl = 1; 2766 ovfl = 1;
2767 } 2767 }
2768 break; 2768 break;
2769 case SHL: 2769 case SHL:
2770 q = utyp ? (int64_t)(ul << sr) : sl << sr; 2770 q = utyp ? (int64_t)(ul << sr) : sl << sr;
2771 break; 2771 break;
2772 case SHR: 2772 case SHR:
2773 /* 2773 /*
2774 * The sign must be explicitly extended because 2774 * The sign must be explicitly extended because
2775 * shifts of signed values are implementation dependent. 2775 * shifts of signed values are implementation dependent.
2776 */ 2776 */
2777 q = ul >> sr; 2777 q = ul >> sr;
2778 q = xsign(q, t, size(t) - (int)sr); 2778 q = xsign(q, t, size(t) - (int)sr);
2779 break; 2779 break;
2780 case LT: 2780 case LT:
2781 q = utyp ? ul < ur : sl < sr; 2781 q = utyp ? ul < ur : sl < sr;
2782 break; 2782 break;
2783 case LE: 2783 case LE:
2784 q = utyp ? ul <= ur : sl <= sr; 2784 q = utyp ? ul <= ur : sl <= sr;
2785 break; 2785 break;
2786 case GE: 2786 case GE:
2787 q = utyp ? ul >= ur : sl >= sr; 2787 q = utyp ? ul >= ur : sl >= sr;
2788 break; 2788 break;
2789 case GT: 2789 case GT:
2790 q = utyp ? ul > ur : sl > sr; 2790 q = utyp ? ul > ur : sl > sr;
2791 break; 2791 break;
2792 case EQ: 2792 case EQ:
2793 q = utyp ? ul == ur : sl == sr; 2793 q = utyp ? ul == ur : sl == sr;
2794 break; 2794 break;
2795 case NE: 2795 case NE:
2796 q = utyp ? ul != ur : sl != sr; 2796 q = utyp ? ul != ur : sl != sr;
2797 break; 2797 break;
2798 case AND: 2798 case AND:
2799 q = utyp ? (int64_t)(ul & ur) : sl & sr; 2799 q = utyp ? (int64_t)(ul & ur) : sl & sr;
2800 break; 2800 break;
2801 case XOR: 2801 case XOR:
2802 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr; 2802 q = utyp ? (int64_t)(ul ^ ur) : sl ^ sr;
2803 break; 2803 break;
2804 case OR: 2804 case OR:
2805 q = utyp ? (int64_t)(ul | ur) : sl | sr; 2805 q = utyp ? (int64_t)(ul | ur) : sl | sr;
2806 break; 2806 break;
2807 default: 2807 default:
2808 lint_assert(/*CONSTCOND*/0); 2808 lint_assert(/*CONSTCOND*/0);
2809 } 2809 }
2810 2810
2811 /* XXX does not work for quads. */ 2811 /* XXX does not work for quads. */
2812 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 && 2812 if (ovfl || ((uint64_t)(q | mask) != ~(uint64_t)0 &&
2813 (q & ~mask) != 0)) { 2813 (q & ~mask) != 0)) {
2814 if (hflag) 2814 if (hflag)
2815 /* integer overflow detected, op %s */ 2815 /* integer overflow detected, op %s */
2816 warning(141, modtab[tn->tn_op].m_name); 2816 warning(141, modtab[tn->tn_op].m_name);
2817 } 2817 }
2818 2818
2819 v->v_quad = xsign(q, t, -1); 2819 v->v_quad = xsign(q, t, -1);
2820 2820
2821 cn = getcnode(tn->tn_type, v); 2821 cn = getcnode(tn->tn_type, v);
2822 2822
2823 return cn; 2823 return cn;
2824} 2824}
2825 2825
2826/* 2826/*
2827 * Same for operators whose operands are compared with 0 (test context). 2827 * Same for operators whose operands are compared with 0 (test context).
2828 */ 2828 */
2829static tnode_t * 2829static tnode_t *
2830fold_test(tnode_t *tn) 2830fold_test(tnode_t *tn)
2831{ 2831{
2832 int l, r = 0; 2832 int l, r = 0;
2833 val_t *v; 2833 val_t *v;
2834 2834
2835 v = xcalloc(1, sizeof (val_t)); 2835 v = xcalloc(1, sizeof (val_t));
2836 v->v_tspec = tn->tn_type->t_tspec; 2836 v->v_tspec = tn->tn_type->t_tspec;
2837 lint_assert(tn->tn_type->t_tspec == INT); 2837 lint_assert(tn->tn_type->t_tspec == INT);
2838 2838
2839 if (tspec_is_float(tn->tn_left->tn_type->t_tspec)) { 2839 if (tspec_is_float(tn->tn_left->tn_type->t_tspec)) {
2840 l = tn->tn_left->tn_val->v_ldbl != 0.0; 2840 l = tn->tn_left->tn_val->v_ldbl != 0.0;
2841 } else { 2841 } else {
2842 l = tn->tn_left->tn_val->v_quad != 0; 2842 l = tn->tn_left->tn_val->v_quad != 0;
2843 } 2843 }
2844 2844
2845 if (modtab[tn->tn_op].m_binary) { 2845 if (modtab[tn->tn_op].m_binary) {
2846 if (tspec_is_float(tn->tn_right->tn_type->t_tspec)) { 2846 if (tspec_is_float(tn->tn_right->tn_type->t_tspec)) {
2847 r = tn->tn_right->tn_val->v_ldbl != 0.0; 2847 r = tn->tn_right->tn_val->v_ldbl != 0.0;
2848 } else { 2848 } else {
2849 r = tn->tn_right->tn_val->v_quad != 0; 2849 r = tn->tn_right->tn_val->v_quad != 0;
2850 } 2850 }
2851 } 2851 }
2852 2852
2853 switch (tn->tn_op) { 2853 switch (tn->tn_op) {
2854 case NOT: 2854 case NOT:
2855 if (hflag && !constcond_flag) 2855 if (hflag && !constcond_flag)
2856 /* constant argument to NOT */ 2856 /* constant argument to NOT */
2857 warning(239); 2857 warning(239);
2858 v->v_quad = !l; 2858 v->v_quad = !l;
2859 break; 2859 break;
2860 case LOGAND: 2860 case LOGAND:
2861 v->v_quad = l && r; 2861 v->v_quad = l && r;
2862 break; 2862 break;
2863 case LOGOR: 2863 case LOGOR:
2864 v->v_quad = l || r; 2864 v->v_quad = l || r;
2865 break; 2865 break;
2866 default: 2866 default:
2867 lint_assert(/*CONSTCOND*/0); 2867 lint_assert(/*CONSTCOND*/0);
2868 } 2868 }
2869 2869
2870 return getcnode(tn->tn_type, v); 2870 return getcnode(tn->tn_type, v);
2871} 2871}
2872 2872
2873/* 2873/*
2874 * Same for operands with floating point type. 2874 * Same for operands with floating point type.
2875 */ 2875 */
2876static tnode_t * 2876static tnode_t *
2877fold_float(tnode_t *tn) 2877fold_float(tnode_t *tn)
2878{ 2878{
2879 val_t *v; 2879 val_t *v;
2880 tspec_t t; 2880 tspec_t t;
2881 ldbl_t l, r = 0; 2881 ldbl_t l, r = 0;
2882 2882
2883 fpe = 0; 2883 fpe = 0;
2884 v = xcalloc(1, sizeof (val_t)); 2884 v = xcalloc(1, sizeof (val_t));
2885 v->v_tspec = t = tn->tn_type->t_tspec; 2885 v->v_tspec = t = tn->tn_type->t_tspec;
2886 2886
2887 lint_assert(tspec_is_float(t)); 2887 lint_assert(tspec_is_float(t));
2888 lint_assert(t == tn->tn_left->tn_type->t_tspec); 2888 lint_assert(t == tn->tn_left->tn_type->t_tspec);
2889 lint_assert(!modtab[tn->tn_op].m_binary || 2889 lint_assert(!modtab[tn->tn_op].m_binary ||
2890 t == tn->tn_right->tn_type->t_tspec); 2890 t == tn->tn_right->tn_type->t_tspec);
2891 2891
2892 l = tn->tn_left->tn_val->v_ldbl; 2892 l = tn->tn_left->tn_val->v_ldbl;
2893 if (modtab[tn->tn_op].m_binary) 2893 if (modtab[tn->tn_op].m_binary)
2894 r = tn->tn_right->tn_val->v_ldbl; 2894 r = tn->tn_right->tn_val->v_ldbl;
2895 2895
2896 switch (tn->tn_op) { 2896 switch (tn->tn_op) {
2897 case UPLUS: 2897 case UPLUS:
2898 v->v_ldbl = l; 2898 v->v_ldbl = l;
2899 break; 2899 break;
2900 case UMINUS: 2900 case UMINUS:
2901 v->v_ldbl = -l; 2901 v->v_ldbl = -l;
2902 break; 2902 break;
2903 case MULT: 2903 case MULT:
2904 v->v_ldbl = l * r; 2904 v->v_ldbl = l * r;
2905 break; 2905 break;
2906 case DIV: 2906 case DIV:
2907 if (r == 0.0) { 2907 if (r == 0.0) {
2908 /* division by 0 */ 2908 /* division by 0 */
2909 error(139); 2909 error(139);
2910 if (t == FLOAT) { 2910 if (t == FLOAT) {
2911 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX; 2911 v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
2912 } else if (t == DOUBLE) { 2912 } else if (t == DOUBLE) {
2913 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX; 2913 v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
2914 } else { 2914 } else {
2915 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX; 2915 v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
2916 } 2916 }
2917 } else { 2917 } else {
2918 v->v_ldbl = l / r; 2918 v->v_ldbl = l / r;
2919 } 2919 }
2920 break; 2920 break;
2921 case PLUS: 2921 case PLUS:
2922 v->v_ldbl = l + r; 2922 v->v_ldbl = l + r;
2923 break; 2923 break;
2924 case MINUS: 2924 case MINUS:
2925 v->v_ldbl = l - r; 2925 v->v_ldbl = l - r;
2926 break; 2926 break;
2927 case LT: 2927 case LT:
2928 v->v_quad = l < r; 2928 v->v_quad = l < r;
2929 break; 2929 break;
2930 case LE: 2930 case LE:
2931 v->v_quad = l <= r; 2931 v->v_quad = l <= r;
2932 break; 2932 break;
2933 case GE: 2933 case GE:
2934 v->v_quad = l >= r; 2934 v->v_quad = l >= r;
2935 break; 2935 break;
2936 case GT: 2936 case GT:
2937 v->v_quad = l > r; 2937 v->v_quad = l > r;
2938 break; 2938 break;
2939 case EQ: 2939 case EQ:
2940 v->v_quad = l == r; 2940 v->v_quad = l == r;
2941 break; 2941 break;
2942 case NE: 2942 case NE:
2943 v->v_quad = l != r; 2943 v->v_quad = l != r;
2944 break; 2944 break;
2945 default: 2945 default:
2946 lint_assert(/*CONSTCOND*/0); 2946 lint_assert(/*CONSTCOND*/0);
2947 } 2947 }
2948 2948
2949 lint_assert(fpe || !isnan((double)v->v_ldbl)); 2949 lint_assert(fpe || !isnan((double)v->v_ldbl));
2950 if (fpe || !finite((double)v->v_ldbl) || 2950 if (fpe || !finite((double)v->v_ldbl) ||
2951 (t == FLOAT && 2951 (t == FLOAT &&
2952 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) || 2952 (v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
2953 (t == DOUBLE && 2953 (t == DOUBLE &&
2954 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) { 2954 (v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
2955 /* floating point overflow detected, op %s */ 2955 /* floating point overflow detected, op %s */
2956 warning(142, modtab[tn->tn_op].m_name); 2956 warning(142, modtab[tn->tn_op].m_name);
2957 if (t == FLOAT) { 2957 if (t == FLOAT) {
2958 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX; 2958 v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
2959 } else if (t == DOUBLE) { 2959 } else if (t == DOUBLE) {
2960 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX; 2960 v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
2961 } else { 2961 } else {
2962 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX; 2962 v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
2963 } 2963 }
2964 fpe = 0; 2964 fpe = 0;
2965 } 2965 }
2966 2966
2967 return getcnode(tn->tn_type, v); 2967 return getcnode(tn->tn_type, v);
2968} 2968}
2969 2969
2970 2970
2971/* 2971/*
2972 * Create a constant node for sizeof. 2972 * Create a constant node for sizeof.
2973 */ 2973 */
2974tnode_t * 2974tnode_t *
2975build_sizeof(type_t *tp) 2975build_sizeof(type_t *tp)
2976{ 2976{
2977 tspec_t st; 2977 tspec_t st;
2978#if SIZEOF_IS_ULONG 2978#if SIZEOF_IS_ULONG
2979 st = ULONG; 2979 st = ULONG;
2980#else 2980#else
2981 st = UINT; 2981 st = UINT;
2982#endif 2982#endif
2983 return new_int_const_node(st, tsize(tp) / CHAR_BIT); 2983 return new_int_const_node(st, tsize(tp) / CHAR_BIT);
2984} 2984}
2985 2985
2986/* 2986/*
2987 * Create a constant node for offsetof. 2987 * Create a constant node for offsetof.
2988 */ 2988 */
2989tnode_t * 2989tnode_t *
2990build_offsetof(type_t *tp, sym_t *sym) 2990build_offsetof(type_t *tp, sym_t *sym)
2991{ 2991{
2992 tspec_t st; 2992 tspec_t st;
2993#if SIZEOF_IS_ULONG 2993#if SIZEOF_IS_ULONG
2994 st = ULONG; 2994 st = ULONG;
2995#else 2995#else
2996 st = UINT; 2996 st = UINT;
2997#endif 2997#endif
2998 tspec_t t = tp->t_tspec; 2998 tspec_t t = tp->t_tspec;
2999 if (t != STRUCT && t != UNION) 2999 if (t != STRUCT && t != UNION)
3000 /* unacceptable operand of '%s' */ 3000 /* unacceptable operand of '%s' */
3001 error(111, "offsetof"); 3001 error(111, "offsetof");
3002 3002
3003 // XXX: wrong size, no checking for sym fixme 3003 // XXX: wrong size, no checking for sym fixme
3004 return new_int_const_node(st, tsize(tp) / CHAR_BIT); 3004 return new_int_const_node(st, tsize(tp) / CHAR_BIT);
3005} 3005}
3006 3006
3007int64_t 3007int64_t
3008tsize(type_t *tp) 3008tsize(type_t *tp)
3009{ 3009{
3010 int elem, elsz, flex; 3010 int elem, elsz, flex;
3011 3011
3012 elem = 1; 3012 elem = 1;
3013 flex = 0; 3013 flex = 0;
3014 while (tp->t_tspec == ARRAY) { 3014 while (tp->t_tspec == ARRAY) {
3015 flex = 1; /* allow c99 flex arrays [] [0] */ 3015 flex = 1; /* allow c99 flex arrays [] [0] */
3016 elem *= tp->t_dim; 3016 elem *= tp->t_dim;
3017 tp = tp->t_subt; 3017 tp = tp->t_subt;
3018 } 3018 }
3019 if (elem == 0) { 3019 if (elem == 0) {
3020 if (!flex) { 3020 if (!flex) {
3021 /* cannot take size/alignment of incomplete type */ 3021 /* cannot take size/alignment of incomplete type */
3022 error(143); 3022 error(143);
3023 elem = 1; 3023 elem = 1;
3024 } 3024 }
3025 } 3025 }
3026 switch (tp->t_tspec) { 3026 switch (tp->t_tspec) {
3027 case FUNC: 3027 case FUNC:
3028 /* cannot take size/alignment of function */ 3028 /* cannot take size/alignment of function */
3029 error(144); 3029 error(144);
3030 elsz = 1; 3030 elsz = 1;
3031 break; 3031 break;
3032 case STRUCT: 3032 case STRUCT:
3033 case UNION: 3033 case UNION:
3034 if (incompl(tp)) { 3034 if (incompl(tp)) {
3035 /* cannot take size/alignment of incomplete type */ 3035 /* cannot take size/alignment of incomplete type */
3036 error(143); 3036 error(143);
3037 elsz = 1; 3037 elsz = 1;
3038 } else { 3038 } else {
3039 elsz = tp->t_str->size; 3039 elsz = tp->t_str->size;
3040 } 3040 }
3041 break; 3041 break;
3042 case ENUM: 3042 case ENUM:
3043 if (incompl(tp)) { 3043 if (incompl(tp)) {
3044 /* cannot take size/alignment of incomplete type */ 3044 /* cannot take size/alignment of incomplete type */
3045 warning(143); 3045 warning(143);
3046 } 3046 }
3047 /* FALLTHROUGH */ 3047 /* FALLTHROUGH */
3048 default: 3048 default:
3049 if (tp->t_isfield) { 3049 if (tp->t_bitfield) {
3050 /* cannot take size/alignment of bit-field */ 3050 /* cannot take size/alignment of bit-field */
3051 error(145); 3051 error(145);
3052 } 3052 }
3053 if (tp->t_tspec == VOID) { 3053 if (tp->t_tspec == VOID) {
3054 /* cannot take size/alignment of void */ 3054 /* cannot take size/alignment of void */
3055 error(146); 3055 error(146);
3056 elsz = 1; 3056 elsz = 1;
3057 } else { 3057 } else {
3058 elsz = size(tp->t_tspec); 3058 elsz = size(tp->t_tspec);
3059 lint_assert(elsz > 0); 3059 lint_assert(elsz > 0);
3060 } 3060 }
3061 break; 3061 break;
3062 } 3062 }
3063 3063
3064 /* XXX: type conversion is too late */ 3064 /* XXX: type conversion is too late */
3065 return (int64_t)(elem * elsz); 3065 return (int64_t)(elem * elsz);
3066} 3066}
3067 3067
3068/* 3068/*
3069 */ 3069 */
3070tnode_t * 3070tnode_t *
3071build_alignof(type_t *tp) 3071build_alignof(type_t *tp)
3072{ 3072{
3073 tspec_t st; 3073 tspec_t st;
3074 3074
3075 switch (tp->t_tspec) { 3075 switch (tp->t_tspec) {
3076 case ARRAY: 3076 case ARRAY:
3077 break; 3077 break;
3078 3078
3079 case FUNC: 3079 case FUNC:
3080 /* cannot take size/alignment of function */ 3080 /* cannot take size/alignment of function */
3081 error(144); 3081 error(144);
3082 return 0; 3082 return 0;
3083 3083
3084 case STRUCT: 3084 case STRUCT:
3085 case UNION: 3085 case UNION:
3086 if (incompl(tp)) { 3086 if (incompl(tp)) {
3087 /* cannot take size/alignment of incomplete type */ 3087 /* cannot take size/alignment of incomplete type */
3088 error(143); 3088 error(143);
3089 return 0; 3089 return 0;
3090 } 3090 }
3091 break; 3091 break;
3092 case ENUM: 3092 case ENUM:
3093 break; 3093 break;
3094 default: 3094 default:
3095 if (tp->t_isfield) { 3095 if (tp->t_bitfield) {
3096 /* cannot take size/alignment of bit-field */ 3096 /* cannot take size/alignment of bit-field */
3097 error(145); 3097 error(145);
3098 return 0; 3098 return 0;
3099 } 3099 }
3100 if (tp->t_tspec == VOID) { 3100 if (tp->t_tspec == VOID) {
3101 /* cannot take size/alignment of void */ 3101 /* cannot take size/alignment of void */
3102 error(146); 3102 error(146);
3103 return 0; 3103 return 0;
3104 } 3104 }
3105 break; 3105 break;
3106 } 3106 }
3107 3107
3108#if SIZEOF_IS_ULONG 3108#if SIZEOF_IS_ULONG
3109 st = ULONG; 3109 st = ULONG;
3110#else 3110#else
3111 st = UINT; 3111 st = UINT;
3112#endif 3112#endif
3113 3113
3114 return new_int_const_node(st, (int64_t)getbound(tp) / CHAR_BIT); 3114 return new_int_const_node(st, (int64_t)getbound(tp) / CHAR_BIT);
3115} 3115}
3116 3116
3117/* 3117/*
3118 * Type casts. 3118 * Type casts.
3119 */ 3119 */
3120tnode_t * 3120tnode_t *
3121cast(tnode_t *tn, type_t *tp) 3121cast(tnode_t *tn, type_t *tp)
3122{ 3122{
3123 tspec_t nt, ot; 3123 tspec_t nt, ot;
3124 3124
3125 if (tn == NULL) 3125 if (tn == NULL)
3126 return NULL; 3126 return NULL;
3127 3127
3128 tn = cconv(tn); 3128 tn = cconv(tn);
3129 3129
3130 nt = tp->t_tspec; 3130 nt = tp->t_tspec;
3131 ot = tn->tn_type->t_tspec; 3131 ot = tn->tn_type->t_tspec;
3132 3132
3133 if (nt == VOID) { 3133 if (nt == VOID) {
3134 /* 3134 /*
3135 * XXX ANSI C requires scalar types or void (Plauger & Brodie). 3135 * XXX ANSI C requires scalar types or void (Plauger & Brodie).
3136 * But this seams really questionable. 3136 * But this seams really questionable.
3137 */ 3137 */
3138 } else if (nt == UNION) { 3138 } else if (nt == UNION) {
3139 sym_t *m; 3139 sym_t *m;
3140 str_t *str = tp->t_str; 3140 str_t *str = tp->t_str;
3141 if (!Sflag) { 3141 if (!Sflag) {
3142 /* union cast is a C9X feature */ 3142 /* union cast is a C9X feature */
3143 error(328); 3143 error(328);
3144 return NULL; 3144 return NULL;
3145 } 3145 }
3146 for (m = str->memb; m != NULL; m = m->s_next) { 3146 for (m = str->memb; m != NULL; m = m->s_next) {
3147 if (sametype(m->s_type, tn->tn_type)) { 3147 if (sametype(m->s_type, tn->tn_type)) {
3148 tn = getnode(); 3148 tn = getnode();
3149 tn->tn_op = CVT; 3149 tn->tn_op = CVT;
3150 tn->tn_type = tp; 3150 tn->tn_type = tp;
3151 tn->tn_cast = 1; 3151 tn->tn_cast = 1;
3152 tn->tn_right = NULL; 3152 tn->tn_right = NULL;
3153 return tn; 3153 return tn;
3154 } 3154 }
3155 } 3155 }
3156 /* type '%s' is not a member of '%s' */ 3156 /* type '%s' is not a member of '%s' */
3157 error(329, type_name(tn->tn_type), type_name(tp)); 3157 error(329, type_name(tn->tn_type), type_name(tp));
3158 return NULL; 3158 return NULL;
3159 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) { 3159 } else if (nt == STRUCT || nt == ARRAY || nt == FUNC) {
3160 if (!Sflag || nt == ARRAY || nt == FUNC) { 3160 if (!Sflag || nt == ARRAY || nt == FUNC) {
3161 /* invalid cast expression */ 3161 /* invalid cast expression */
3162 error(147); 3162 error(147);
3163 return NULL; 3163 return NULL;
3164 } 3164 }
3165 } else if (ot == STRUCT || ot == UNION) { 3165 } else if (ot == STRUCT || ot == UNION) {
3166 /* invalid cast expression */ 3166 /* invalid cast expression */
3167 error(147); 3167 error(147);
3168 return NULL; 3168 return NULL;
3169 } else if (ot == VOID) { 3169 } else if (ot == VOID) {
3170 /* improper cast of void expression */ 3170 /* improper cast of void expression */
3171 error(148); 3171 error(148);
3172 return NULL; 3172 return NULL;
3173 } else if (tspec_is_int(nt) && tspec_is_scalar(ot)) { 3173 } else if (tspec_is_int(nt) && tspec_is_scalar(ot)) {
3174 /* ok */ 3174 /* ok */
3175 } else if (tspec_is_float(nt) && tspec_is_arith(ot)) { 3175 } else if (tspec_is_float(nt) && tspec_is_arith(ot)) {
3176 /* ok */ 3176 /* ok */
3177 } else if (nt == PTR && tspec_is_int(ot)) { 3177 } else if (nt == PTR && tspec_is_int(ot)) {
3178 /* ok */ 3178 /* ok */
3179 } else if (nt == PTR && ot == PTR) { 3179 } else if (nt == PTR && ot == PTR) {
3180 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) { 3180 if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
3181 if (hflag) 3181 if (hflag)
3182 /* cast discards 'const' from ... */ 3182 /* cast discards 'const' from ... */
3183 warning(275); 3183 warning(275);
3184 } 3184 }
3185 } else { 3185 } else {
3186 /* invalid cast expression */ 3186 /* invalid cast expression */
3187 error(147); 3187 error(147);
3188 return NULL; 3188 return NULL;
3189 } 3189 }
3190 3190
3191 tn = convert(CVT, 0, tp, tn); 3191 tn = convert(CVT, 0, tp, tn);
3192 tn->tn_cast = 1; 3192 tn->tn_cast = 1;
3193 3193
3194 return tn; 3194 return tn;
3195} 3195}
3196 3196
3197/* 3197/*
3198 * Create the node for a function argument. 3198 * Create the node for a function argument.
3199 * All necessary conversions and type checks are done in funccall(), because 3199 * All necessary conversions and type checks are done in funccall(), because
3200 * in funcarg() we have no information about expected argument types. 3200 * in funcarg() we have no information about expected argument types.
3201 */ 3201 */
3202tnode_t * 3202tnode_t *
3203funcarg(tnode_t *args, tnode_t *arg) 3203funcarg(tnode_t *args, tnode_t *arg)
3204{ 3204{
3205 tnode_t *ntn; 3205 tnode_t *ntn;
3206 3206
3207 /* 3207 /*
3208 * If there was a serious error in the expression for the argument, 3208 * If there was a serious error in the expression for the argument,
3209 * create a dummy argument so the positions of the remaining arguments 3209 * create a dummy argument so the positions of the remaining arguments
3210 * will not change. 3210 * will not change.
3211 */ 3211 */
3212 if (arg == NULL) 3212 if (arg == NULL)
3213 arg = new_int_const_node(INT, (int64_t)0); 3213 arg = new_int_const_node(INT, (int64_t)0);
3214 3214
3215 ntn = new_tnode(PUSH, arg->tn_type, arg, args); 3215 ntn = new_tnode(PUSH, arg->tn_type, arg, args);
3216 3216
3217 return ntn; 3217 return ntn;
3218} 3218}
3219 3219
3220/* 3220/*
3221 * Create the node for a function call. Also check types of 3221 * Create the node for a function call. Also check types of
3222 * function arguments and insert conversions, if necessary. 3222 * function arguments and insert conversions, if necessary.
3223 */ 3223 */
3224tnode_t * 3224tnode_t *
3225funccall(tnode_t *func, tnode_t *args) 3225funccall(tnode_t *func, tnode_t *args)
3226{ 3226{
3227 tnode_t *ntn; 3227 tnode_t *ntn;
3228 op_t fcop; 3228 op_t fcop;
3229 3229
3230 if (func == NULL) 3230 if (func == NULL)
3231 return NULL; 3231 return NULL;
3232 3232
3233 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) { 3233 if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
3234 fcop = CALL; 3234 fcop = CALL;
3235 } else { 3235 } else {
3236 fcop = ICALL; 3236 fcop = ICALL;
3237 } 3237 }
3238 3238
3239 /* 3239 /*
3240 * after cconv() func will always be a pointer to a function 3240 * after cconv() func will always be a pointer to a function
3241 * if it is a valid function designator. 3241 * if it is a valid function designator.
3242 */ 3242 */
3243 func = cconv(func); 3243 func = cconv(func);
3244 3244
3245 if (func->tn_type->t_tspec != PTR || 3245 if (func->tn_type->t_tspec != PTR ||
3246 func->tn_type->t_subt->t_tspec != FUNC) { 3246 func->tn_type->t_subt->t_tspec != FUNC) {
3247 /* illegal function (type %s) */ 3247 /* illegal function (type %s) */
3248 error(149, type_name(func->tn_type)); 3248 error(149, type_name(func->tn_type));
3249 return NULL; 3249 return NULL;
3250 } 3250 }
3251 3251
3252 args = check_function_arguments(func->tn_type->t_subt, args); 3252 args = check_function_arguments(func->tn_type->t_subt, args);
3253 3253
3254 ntn = new_tnode(fcop, func->tn_type->t_subt->t_subt, func, args); 3254 ntn = new_tnode(fcop, func->tn_type->t_subt->t_subt, func, args);
3255 3255
3256 return ntn; 3256 return ntn;
3257} 3257}
3258 3258
3259/* 3259/*
3260 * Check types of all function arguments and insert conversions, 3260 * Check types of all function arguments and insert conversions,
3261 * if necessary. 3261 * if necessary.
3262 */ 3262 */
3263static tnode_t * 3263static tnode_t *
3264check_function_arguments(type_t *ftp, tnode_t *args) 3264check_function_arguments(type_t *ftp, tnode_t *args)
3265{ 3265{
3266 tnode_t *arg; 3266 tnode_t *arg;
3267 sym_t *asym; 3267 sym_t *asym;
3268 tspec_t at; 3268 tspec_t at;
3269 int narg, npar, n, i; 3269 int narg, npar, n, i;
3270 3270
3271 /* get # of args in the prototype */ 3271 /* get # of args in the prototype */
3272 npar = 0; 3272 npar = 0;
3273 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next) 3273 for (asym = ftp->t_args; asym != NULL; asym = asym->s_next)
3274 npar++; 3274 npar++;
3275 3275
3276 /* get # of args in function call */ 3276 /* get # of args in function call */
3277 narg = 0; 3277 narg = 0;
3278 for (arg = args; arg != NULL; arg = arg->tn_right) 3278 for (arg = args; arg != NULL; arg = arg->tn_right)
3279 narg++; 3279 narg++;
3280 3280
3281 asym = ftp->t_args; 3281 asym = ftp->t_args;
3282 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) { 3282 if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
3283 /* argument mismatch: %d arg%s passed, %d expected */ 3283 /* argument mismatch: %d arg%s passed, %d expected */
3284 error(150, narg, narg > 1 ? "s" : "", npar); 3284 error(150, narg, narg > 1 ? "s" : "", npar);
3285 asym = NULL; 3285 asym = NULL;
3286 } 3286 }
3287 3287
3288 for (n = 1; n <= narg; n++) { 3288 for (n = 1; n <= narg; n++) {
3289 3289
3290 /* 3290 /*
3291 * The rightmost argument is at the top of the argument 3291 * The rightmost argument is at the top of the argument
3292 * subtree. 3292 * subtree.
3293 */ 3293 */
3294 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) 3294 for (i = narg, arg = args; i > n; i--, arg = arg->tn_right)
3295 continue; 3295 continue;
3296 3296
3297 /* some things which are always not allowed */ 3297 /* some things which are always not allowed */
3298 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) { 3298 if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
3299 /* void expressions may not be arguments, arg #%d */ 3299 /* void expressions may not be arguments, arg #%d */
3300 error(151, n); 3300 error(151, n);
3301 return NULL; 3301 return NULL;
3302 } else if ((at == STRUCT || at == UNION) && 3302 } else if ((at == STRUCT || at == UNION) &&
3303 incompl(arg->tn_left->tn_type)) { 3303 incompl(arg->tn_left->tn_type)) {
3304 /* argument cannot have unknown size, arg #%d */ 3304 /* argument cannot have unknown size, arg #%d */
3305 error(152, n); 3305 error(152, n);
3306 return NULL; 3306 return NULL;
3307 } else if (tspec_is_int(at) && 3307 } else if (tspec_is_int(at) &&
3308 arg->tn_left->tn_type->t_isenum && 3308 arg->tn_left->tn_type->t_isenum &&
3309 incompl(arg->tn_left->tn_type)) { 3309 incompl(arg->tn_left->tn_type)) {
3310 /* argument cannot have unknown size, arg #%d */ 3310 /* argument cannot have unknown size, arg #%d */
3311 warning(152, n); 3311 warning(152, n);
3312 } 3312 }
3313 3313
3314 /* class conversions (arg in value context) */ 3314 /* class conversions (arg in value context) */
3315 arg->tn_left = cconv(arg->tn_left); 3315 arg->tn_left = cconv(arg->tn_left);
3316 3316
3317 if (asym != NULL) { 3317 if (asym != NULL) {
3318 arg->tn_left = check_prototype_argument( 3318 arg->tn_left = check_prototype_argument(
3319 n, asym->s_type, arg->tn_left); 3319 n, asym->s_type, arg->tn_left);
3320 } else { 3320 } else {
3321 arg->tn_left = promote(NOOP, 1, arg->tn_left); 3321 arg->tn_left = promote(NOOP, 1, arg->tn_left);
3322 } 3322 }
3323 arg->tn_type = arg->tn_left->tn_type; 3323 arg->tn_type = arg->tn_left->tn_type;
3324 3324
3325 if (asym != NULL) 3325 if (asym != NULL)
3326 asym = asym->s_next; 3326 asym = asym->s_next;
3327 } 3327 }
3328 3328
3329 return args; 3329 return args;
3330} 3330}
3331 3331
3332/* 3332/*
3333 * Compare the type of an argument with the corresponding type of a 3333 * Compare the type of an argument with the corresponding type of a
3334 * prototype parameter. If it is a valid combination, but both types 3334 * prototype parameter. If it is a valid combination, but both types
3335 * are not the same, insert a conversion to convert the argument into 3335 * are not the same, insert a conversion to convert the argument into
3336 * the type of the parameter. 3336 * the type of the parameter.
3337 */ 3337 */
3338static tnode_t * 3338static tnode_t *
3339check_prototype_argument( 3339check_prototype_argument(
3340 int n, /* pos of arg */ 3340 int n, /* pos of arg */
3341 type_t *tp, /* expected type (from prototype) */ 3341 type_t *tp, /* expected type (from prototype) */
3342 tnode_t *tn) /* argument */ 3342 tnode_t *tn) /* argument */
3343{ 3343{
3344 tnode_t *ln; 3344 tnode_t *ln;
3345 int dowarn; 3345 int dowarn;
3346 3346
3347 ln = xcalloc(1, sizeof (tnode_t)); 3347 ln = xcalloc(1, sizeof (tnode_t));
3348 ln->tn_type = tduptyp(tp); 3348 ln->tn_type = tduptyp(tp);
3349 ln->tn_type->t_const = 0; 3349 ln->tn_type->t_const = 0;
3350 ln->tn_lvalue = 1; 3350 ln->tn_lvalue = 1;
3351 if (typeok(FARG, n, ln, tn)) { 3351 if (typeok(FARG, n, ln, tn)) {
3352 if (!eqtype(tp, tn->tn_type, 1, 0, (dowarn = 0, &dowarn)) || dowarn) 3352 if (!eqtype(tp, tn->tn_type, 1, 0, (dowarn = 0, &dowarn)) || dowarn)
3353 tn = convert(FARG, n, tp, tn); 3353 tn = convert(FARG, n, tp, tn);
3354 } 3354 }
3355 free(ln); 3355 free(ln);
3356 return tn; 3356 return tn;
3357} 3357}
3358 3358
3359/* 3359/*
3360 * Return the value of an integral constant expression. 3360 * Return the value of an integral constant expression.
3361 * If the expression is not constant or its type is not an integer 3361 * If the expression is not constant or its type is not an integer
3362 * type, an error message is printed. 3362 * type, an error message is printed.
3363 */ 3363 */
3364val_t * 3364val_t *
3365constant(tnode_t *tn, int required) 3365constant(tnode_t *tn, int required)
3366{ 3366{
3367 val_t *v; 3367 val_t *v;
3368 3368
3369 if (tn != NULL) 3369 if (tn != NULL)
3370 tn = cconv(tn); 3370 tn = cconv(tn);
3371 if (tn != NULL) 3371 if (tn != NULL)
3372 tn = promote(NOOP, 0, tn); 3372 tn = promote(NOOP, 0, tn);
3373 3373
3374 v = xcalloc(1, sizeof (val_t)); 3374 v = xcalloc(1, sizeof (val_t));
3375 3375
3376 if (tn == NULL) { 3376 if (tn == NULL) {
3377 lint_assert(nerr != 0); 3377 lint_assert(nerr != 0);
3378 v->v_tspec = INT; 3378 v->v_tspec = INT;
3379 v->v_quad = 1; 3379 v->v_quad = 1;
3380 return v; 3380 return v;
3381 } 3381 }
3382 3382
3383 v->v_tspec = tn->tn_type->t_tspec; 3383 v->v_tspec = tn->tn_type->t_tspec;
3384 3384
3385 if (tn->tn_op == CON) { 3385 if (tn->tn_op == CON) {
3386 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec); 3386 lint_assert(tn->tn_type->t_tspec == tn->tn_val->v_tspec);
3387 if (tspec_is_int(tn->tn_val->v_tspec)) { 3387 if (tspec_is_int(tn->tn_val->v_tspec)) {
3388 v->v_ansiu = tn->tn_val->v_ansiu; 3388 v->v_ansiu = tn->tn_val->v_ansiu;
3389 v->v_quad = tn->tn_val->v_quad; 3389 v->v_quad = tn->tn_val->v_quad;
3390 return v; 3390 return v;
3391 } 3391 }
3392 v->v_quad = tn->tn_val->v_ldbl; 3392 v->v_quad = tn->tn_val->v_ldbl;
3393 } else { 3393 } else {
3394 v->v_quad = 1; 3394 v->v_quad = 1;
3395 } 3395 }
3396 3396
3397 if (required) 3397 if (required)
3398 /* integral constant expression expected */ 3398 /* integral constant expression expected */
3399 error(55); 3399 error(55);
3400 else 3400 else
3401 /* variable array dimension is a C99/GCC extension */ 3401 /* variable array dimension is a C99/GCC extension */
3402 c99ism(318); 3402 c99ism(318);
3403 3403
3404 if (!tspec_is_int(v->v_tspec)) 3404 if (!tspec_is_int(v->v_tspec))
3405 v->v_tspec = INT; 3405 v->v_tspec = INT;
3406 3406
3407 return v; 3407 return v;
3408} 3408}
3409 3409
3410/* 3410/*
3411 * Perform some tests on expressions which can't be done in build() and 3411 * Perform some tests on expressions which can't be done in build() and
3412 * functions called by build(). These tests must be done here because 3412 * functions called by build(). These tests must be done here because
3413 * we need some information about the context in which the operations 3413 * we need some information about the context in which the operations
3414 * are performed. 3414 * are performed.
3415 * After all tests are performed, expr() frees the memory which is used 3415 * After all tests are performed, expr() frees the memory which is used
3416 * for the expression. 3416 * for the expression.
3417 */ 3417 */
3418void 3418void
3419expr(tnode_t *tn, int vctx, int tctx, int dofreeblk) 3419expr(tnode_t *tn, int vctx, int tctx, int dofreeblk)
3420{ 3420{
3421 3421
3422 lint_assert(tn != NULL || nerr != 0); 3422 lint_assert(tn != NULL || nerr != 0);
3423 3423
3424 if (tn == NULL) { 3424 if (tn == NULL) {
3425 tfreeblk(); 3425 tfreeblk();
3426 return; 3426 return;
3427 } 3427 }
3428 3428
3429 /* expr() is also called in global initialisations */ 3429 /* expr() is also called in global initialisations */
3430 if (dcs->d_ctx != EXTERN) 3430 if (dcs->d_ctx != EXTERN)
3431 check_statement_reachable(); 3431 check_statement_reachable();
3432 3432
3433 check_expr_misc(tn, vctx, tctx, !tctx, 0, 0, 0); 3433 check_expr_misc(tn, vctx, tctx, !tctx, 0, 0, 0);
3434 if (tn->tn_op == ASSIGN) { 3434 if (tn->tn_op == ASSIGN) {
3435 if (hflag && tctx) 3435 if (hflag && tctx)
3436 /* assignment in conditional context */ 3436 /* assignment in conditional context */
3437 warning(159); 3437 warning(159);
3438 } else if (tn->tn_op == CON) { 3438 } else if (tn->tn_op == CON) {
3439 if (hflag && tctx && !constcond_flag) 3439 if (hflag && tctx && !constcond_flag)
3440 /* constant in conditional context */ 3440 /* constant in conditional context */
3441 warning(161); 3441 warning(161);
3442 } 3442 }
3443 if (!modtab[tn->tn_op].m_sideeff) { 3443 if (!modtab[tn->tn_op].m_sideeff) {
3444 /* 3444 /*
3445 * for left operands of COMMA this warning is already 3445 * for left operands of COMMA this warning is already
3446 * printed 3446 * printed
3447 */ 3447 */
3448 if (tn->tn_op != COMMA && !vctx && !tctx) 3448 if (tn->tn_op != COMMA && !vctx && !tctx)
3449 check_null_effect(tn); 3449 check_null_effect(tn);
3450 } 3450 }
3451 if (dflag) 3451 if (dflag)
3452 display_expression(tn, 0); 3452 display_expression(tn, 0);
3453 3453
3454 /* free the tree memory */ 3454 /* free the tree memory */
3455 if (dofreeblk) 3455 if (dofreeblk)
3456 tfreeblk(); 3456 tfreeblk();
3457} 3457}
3458 3458
3459static void 3459static void
3460check_null_effect(tnode_t *tn) 3460check_null_effect(tnode_t *tn)
3461{ 3461{
3462 3462
3463 if (!hflag) 3463 if (!hflag)
3464 return; 3464 return;
3465 3465
3466 while (!modtab[tn->tn_op].m_sideeff) { 3466 while (!modtab[tn->tn_op].m_sideeff) {
3467 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) { 3467 if (tn->tn_op == CVT && tn->tn_type->t_tspec == VOID) {
3468 tn = tn->tn_left; 3468 tn = tn->tn_left;
3469 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) { 3469 } else if (tn->tn_op == LOGAND || tn->tn_op == LOGOR) {
3470 /* 3470 /*
3471 * && and || have a side effect if the right operand 3471 * && and || have a side effect if the right operand
3472 * has a side effect. 3472 * has a side effect.
3473 */ 3473 */
3474 tn = tn->tn_right; 3474 tn = tn->tn_right;
3475 } else if (tn->tn_op == QUEST) { 3475 } else if (tn->tn_op == QUEST) {
3476 /* 3476 /*
3477 * ? has a side effect if at least one of its right 3477 * ? has a side effect if at least one of its right
3478 * operands has a side effect 3478 * operands has a side effect
3479 */ 3479 */
3480 tn = tn->tn_right; 3480 tn = tn->tn_right;
3481 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) { 3481 } else if (tn->tn_op == COLON || tn->tn_op == COMMA) {
3482 /* 3482 /*
3483 * : has a side effect if at least one of its operands 3483 * : has a side effect if at least one of its operands
3484 * has a side effect 3484 * has a side effect
3485 */ 3485 */
3486 if (modtab[tn->tn_left->tn_op].m_sideeff) { 3486 if (modtab[tn->tn_left->tn_op].m_sideeff) {
3487 tn = tn->tn_left; 3487 tn = tn->tn_left;
3488 } else if (modtab[tn->tn_right->tn_op].m_sideeff) { 3488 } else if (modtab[tn->tn_right->tn_op].m_sideeff) {
3489 tn = tn->tn_right; 3489 tn = tn->tn_right;
3490 } else { 3490 } else {
3491 break; 3491 break;
3492 } 3492 }
3493 } else { 3493 } else {
3494 break; 3494 break;
3495 } 3495 }
3496 } 3496 }
3497 if (!modtab[tn->tn_op].m_sideeff) 3497 if (!modtab[tn->tn_op].m_sideeff)
3498 /* expression has null effect */ 3498 /* expression has null effect */
3499 warning(129); 3499 warning(129);
3500} 3500}
3501 3501
3502/* 3502/*
3503 * Dump an expression to stdout 3503 * Dump an expression to stdout
3504 * only used for debugging 3504 * only used for debugging
3505 */ 3505 */
3506static void 3506static void
3507display_expression(tnode_t *tn, int offs) 3507display_expression(tnode_t *tn, int offs)
3508{ 3508{
3509 uint64_t uq; 3509 uint64_t uq;
3510 3510
3511 if (tn == NULL) { 3511 if (tn == NULL) {
3512 (void)printf("%*s%s\n", offs, "", "NULL"); 3512 (void)printf("%*s%s\n", offs, "", "NULL");
3513 return; 3513 return;
3514 } 3514 }
3515 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name); 3515 (void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
3516 3516
3517 if (tn->tn_op == NAME) { 3517 if (tn->tn_op == NAME) {
3518 (void)printf("%s: %s ", 3518 (void)printf("%s: %s ",
3519 tn->tn_sym->s_name, 3519 tn->tn_sym->s_name,
3520 storage_class_name(tn->tn_sym->s_scl)); 3520 storage_class_name(tn->tn_sym->s_scl));
3521 } else if (tn->tn_op == CON && tspec_is_float(tn->tn_type->t_tspec)) { 3521 } else if (tn->tn_op == CON && tspec_is_float(tn->tn_type->t_tspec)) {
3522 (void)printf("%#g ", (double)tn->tn_val->v_ldbl); 3522 (void)printf("%#g ", (double)tn->tn_val->v_ldbl);
3523 } else if (tn->tn_op == CON && tspec_is_int(tn->tn_type->t_tspec)) { 3523 } else if (tn->tn_op == CON && tspec_is_int(tn->tn_type->t_tspec)) {
3524 uq = tn->tn_val->v_quad; 3524 uq = tn->tn_val->v_quad;
3525 (void)printf("0x %08lx %08lx ", 3525 (void)printf("0x %08lx %08lx ",
3526 (long)(uq >> 32) & 0xffffffffl, 3526 (long)(uq >> 32) & 0xffffffffl,
3527 (long)uq & 0xffffffffl); 3527 (long)uq & 0xffffffffl);
3528 } else if (tn->tn_op == CON) { 3528 } else if (tn->tn_op == CON) {
3529 lint_assert(tn->tn_type->t_tspec == PTR); 3529 lint_assert(tn->tn_type->t_tspec == PTR);
3530 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4), 3530 (void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
3531 (u_long)tn->tn_val->v_quad); 3531 (u_long)tn->tn_val->v_quad);
3532 } else if (tn->tn_op == STRING) { 3532 } else if (tn->tn_op == STRING) {
3533 if (tn->tn_string->st_tspec == CHAR) { 3533 if (tn->tn_string->st_tspec == CHAR) {
3534 (void)printf("\"%s\"", tn->tn_string->st_cp); 3534 (void)printf("\"%s\"", tn->tn_string->st_cp);
3535 } else { 3535 } else {
3536 char *s; 3536 char *s;
3537 size_t n; 3537 size_t n;
3538 n = MB_CUR_MAX * (tn->tn_string->st_len + 1); 3538 n = MB_CUR_MAX * (tn->tn_string->st_len + 1);
3539 s = xmalloc(n); 3539 s = xmalloc(n);
3540 (void)wcstombs(s, tn->tn_string->st_wcp, n); 3540 (void)wcstombs(s, tn->tn_string->st_wcp, n);
3541 (void)printf("L\"%s\"", s); 3541 (void)printf("L\"%s\"", s);
3542 free(s); 3542 free(s);
3543 } 3543 }
3544 (void)printf(" "); 3544 (void)printf(" ");
3545 } else if (tn->tn_op == FSEL) { 3545 } else if (tn->tn_op == FSEL) {
3546 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs, 3546 (void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
3547 tn->tn_type->t_flen); 3547 tn->tn_type->t_flen);
3548 } 3548 }
3549 (void)printf("%s\n", ttos(tn->tn_type)); 3549 (void)printf("%s\n", ttos(tn->tn_type));
3550 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING) 3550 if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
3551 return; 3551 return;
3552 display_expression(tn->tn_left, offs + 2); 3552 display_expression(tn->tn_left, offs + 2);
3553 if (modtab[tn->tn_op].m_binary || 3553 if (modtab[tn->tn_op].m_binary ||
3554 (tn->tn_op == PUSH && tn->tn_right != NULL)) { 3554 (tn->tn_op == PUSH && tn->tn_right != NULL)) {
3555 display_expression(tn->tn_right, offs + 2); 3555 display_expression(tn->tn_right, offs + 2);
3556 } 3556 }
3557} 3557}
3558 3558
3559/* 3559/*
3560 * Called by expr() to recursively perform some tests. 3560 * Called by expr() to recursively perform some tests.
3561 */ 3561 */
3562/* ARGSUSED */ 3562/* ARGSUSED */
3563void 3563void
3564check_expr_misc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc, 3564check_expr_misc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
3565 int szof) 3565 int szof)
3566{ 3566{
3567 tnode_t *ln, *rn; 3567 tnode_t *ln, *rn;
3568 mod_t *mp; 3568 mod_t *mp;
3569 int nrvdisc, cvctx, ctctx; 3569 int nrvdisc, cvctx, ctctx;
3570 op_t op; 3570 op_t op;
3571 scl_t sc; 3571 scl_t sc;
3572 dinfo_t *di; 3572 dinfo_t *di;
3573 3573
3574 if (tn == NULL) 3574 if (tn == NULL)
3575 return; 3575 return;
3576 3576
3577 ln = tn->tn_left; 3577 ln = tn->tn_left;
3578 rn = tn->tn_right; 3578 rn = tn->tn_right;
3579 mp = &modtab[op = tn->tn_op]; 3579 mp = &modtab[op = tn->tn_op];
3580 3580
3581 switch (op) { 3581 switch (op) {
3582 case AMPER: 3582 case AMPER:
3583 if (ln->tn_op == NAME && (reached || rchflg)) { 3583 if (ln->tn_op == NAME && (reached || rchflg)) {
3584 if (!szof) 3584 if (!szof)
3585 mark_as_set(ln->tn_sym); 3585 mark_as_set(ln->tn_sym);
3586 mark_as_used(ln->tn_sym, fcall, szof); 3586 mark_as_used(ln->tn_sym, fcall, szof);
3587 } 3587 }
3588 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3588 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3589 /* check the range of array indices */ 3589 /* check the range of array indices */
3590 check_array_index(ln->tn_left, 1); 3590 check_array_index(ln->tn_left, 1);
3591 break; 3591 break;
3592 case LOAD: 3592 case LOAD:
3593 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3593 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3594 /* check the range of array indices */ 3594 /* check the range of array indices */
3595 check_array_index(ln->tn_left, 0); 3595 check_array_index(ln->tn_left, 0);
3596 /* FALLTHROUGH */ 3596 /* FALLTHROUGH */
3597 case PUSH: 3597 case PUSH:
3598 case INCBEF: 3598 case INCBEF:
3599 case DECBEF: 3599 case DECBEF:
3600 case INCAFT: 3600 case INCAFT:
3601 case DECAFT: 3601 case DECAFT:
3602 case ADDASS: 3602 case ADDASS:
3603 case SUBASS: 3603 case SUBASS:
3604 case MULASS: 3604 case MULASS:
3605 case DIVASS: 3605 case DIVASS:
3606 case MODASS: 3606 case MODASS:
3607 case ANDASS: 3607 case ANDASS:
3608 case ORASS: 3608 case ORASS:
3609 case XORASS: 3609 case XORASS:
3610 case SHLASS: 3610 case SHLASS:
3611 case SHRASS: 3611 case SHRASS:
3612 case REAL: 3612 case REAL:
3613 case IMAG: 3613 case IMAG:
3614 if (ln->tn_op == NAME && (reached || rchflg)) { 3614 if (ln->tn_op == NAME && (reached || rchflg)) {
3615 sc = ln->tn_sym->s_scl; 3615 sc = ln->tn_sym->s_scl;
3616 /* 3616 /*
3617 * Look if there was a asm statement in one of the 3617 * Look if there was a asm statement in one of the
3618 * compound statements we are in. If not, we don't 3618 * compound statements we are in. If not, we don't
3619 * print a warning. 3619 * print a warning.
3620 */ 3620 */
3621 for (di = dcs; di != NULL; di = di->d_next) { 3621 for (di = dcs; di != NULL; di = di->d_next) {
3622 if (di->d_asm) 3622 if (di->d_asm)
3623 break; 3623 break;
3624 } 3624 }
3625 if (sc != EXTERN && sc != STATIC && 3625 if (sc != EXTERN && sc != STATIC &&
3626 !ln->tn_sym->s_set && !szof && di == NULL) { 3626 !ln->tn_sym->s_set && !szof && di == NULL) {
3627 /* %s may be used before set */ 3627 /* %s may be used before set */
3628 warning(158, ln->tn_sym->s_name); 3628 warning(158, ln->tn_sym->s_name);
3629 mark_as_set(ln->tn_sym); 3629 mark_as_set(ln->tn_sym);
3630 } 3630 }
3631 mark_as_used(ln->tn_sym, 0, 0); 3631 mark_as_used(ln->tn_sym, 0, 0);
3632 } 3632 }
3633 break; 3633 break;
3634 case ASSIGN: 3634 case ASSIGN:
3635 if (ln->tn_op == NAME && !szof && (reached || rchflg)) { 3635 if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
3636 mark_as_set(ln->tn_sym); 3636 mark_as_set(ln->tn_sym);
3637 if (ln->tn_sym->s_scl == EXTERN) 3637 if (ln->tn_sym->s_scl == EXTERN)
3638 outusg(ln->tn_sym); 3638 outusg(ln->tn_sym);
3639 } 3639 }
3640 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS) 3640 if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
3641 /* check the range of array indices */ 3641 /* check the range of array indices */
3642 check_array_index(ln->tn_left, 0); 3642 check_array_index(ln->tn_left, 0);
3643 break; 3643 break;
3644 case CALL: 3644 case CALL:
3645 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME) 3645 if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
3646 LERROR("check_expr_misc(op=%s != %s || %s != %s)", 3646 LERROR("check_expr_misc(op=%s != %s || %s != %s)",
3647 getopname(ln->tn_op), getopname(AMPER), 3647 getopname(ln->tn_op), getopname(AMPER),
3648 getopname(ln->tn_left->tn_op), getopname(NAME)); 3648 getopname(ln->tn_left->tn_op), getopname(NAME));
3649 if (!szof) 3649 if (!szof)
3650 outcall(tn, vctx || tctx, rvdisc); 3650 outcall(tn, vctx || tctx, rvdisc);
3651 break; 3651 break;
3652 case EQ: 3652 case EQ:
3653 if (hflag && eqwarn) 3653 if (hflag && eqwarn)
3654 /* operator '==' found where '=' was expected */ 3654 /* operator '==' found where '=' was expected */
3655 warning(160); 3655 warning(160);
3656 break; 3656 break;
3657 case CON: 3657 case CON:
3658 case NAME: 3658 case NAME:
3659 case STRING: 3659 case STRING:
3660 return; 3660 return;
3661 /* LINTED206: (enumeration values not handled in switch) */ 3661 /* LINTED206: (enumeration values not handled in switch) */
3662 case OR: 3662 case OR:
3663 case XOR: 3663 case XOR:
3664 case NE: 3664 case NE:
3665 case GE: 3665 case GE:
3666 case GT: 3666 case GT:
3667 case LE: 3667 case LE:
3668 case LT: 3668 case LT:
3669 case SHR: 3669 case SHR:
3670 case SHL: 3670 case SHL:
3671 case MINUS: 3671 case MINUS:
3672 case PLUS: 3672 case PLUS:
3673 case MOD: 3673 case MOD:
3674 case DIV: 3674 case DIV:
3675 case MULT: 3675 case MULT:
3676 case STAR: 3676 case STAR:
3677 case UMINUS: 3677 case UMINUS:
3678 case UPLUS: 3678 case UPLUS:
3679 case DEC: 3679 case DEC:
3680 case INC: 3680 case INC:
3681 case COMPL: 3681 case COMPL:
3682 case NOT: 3682 case NOT:
3683 case POINT: 3683 case POINT:
3684 case ARROW: 3684 case ARROW:
3685 case NOOP: 3685 case NOOP:
3686 case AND: 3686 case AND:
3687 case FARG: 3687 case FARG:
3688 case CASE: 3688 case CASE:
3689 case INIT: 3689 case INIT:
3690 case RETURN: 3690 case RETURN:
3691 case ICALL: 3691 case ICALL:
3692 case CVT: 3692 case CVT:
3693 case COMMA: 3693 case COMMA:
3694 case FSEL: 3694 case FSEL:
3695 case COLON: 3695 case COLON:
3696 case QUEST: 3696 case QUEST:
3697 case LOGOR: 3697 case LOGOR:
3698 case LOGAND: 3698 case LOGAND:
3699 break; 3699 break;
3700 } 3700 }
3701 3701
3702 cvctx = mp->m_vctx; 3702 cvctx = mp->m_vctx;
3703 ctctx = mp->m_tctx; 3703 ctctx = mp->m_tctx;
3704 /* 3704 /*
3705 * values of operands of ':' are not used if the type of at least 3705 * values of operands of ':' are not used if the type of at least
3706 * one of the operands (for gcc compatibility) is void 3706 * one of the operands (for gcc compatibility) is void
3707 * XXX test/value context of QUEST should probably be used as 3707 * XXX test/value context of QUEST should probably be used as
3708 * context for both operands of COLON 3708 * context for both operands of COLON
3709 */ 3709 */
3710 if (op == COLON && tn->tn_type->t_tspec == VOID) 3710 if (op == COLON && tn->tn_type->t_tspec == VOID)
3711 cvctx = ctctx = 0; 3711 cvctx = ctctx = 0;
3712 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID; 3712 nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
3713 check_expr_misc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof); 3713 check_expr_misc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
3714 3714
3715 switch (op) { 3715 switch (op) {
3716 case PUSH: 3716 case PUSH:
3717 if (rn != NULL) 3717 if (rn != NULL)
3718 check_expr_misc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof); 3718 check_expr_misc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
3719 break; 3719 break;
3720 case LOGAND: 3720 case LOGAND:
3721 case LOGOR: 3721 case LOGOR:
3722 check_expr_misc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof); 3722 check_expr_misc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
3723 break; 3723 break;
3724 case COLON: 3724 case COLON:
3725 check_expr_misc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof); 3725 check_expr_misc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
3726 break; 3726 break;
3727 case COMMA: 3727 case COMMA:
3728 check_expr_misc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof); 3728 check_expr_misc(rn, vctx, tctx, mp->m_eqwarn, 0, 0, szof);
3729 break; 3729 break;
3730 default: 3730 default:
3731 if (mp->m_binary) 3731 if (mp->m_binary)
3732 check_expr_misc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof); 3732 check_expr_misc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
3733 break; 3733 break;
3734 } 3734 }
3735 3735
3736} 3736}
3737 3737
3738/* 3738/*
3739 * Checks the range of array indices, if possible. 3739 * Checks the range of array indices, if possible.
3740 * amper is set if only the address of the element is used. This 3740 * amper is set if only the address of the element is used. This
3741 * means that the index is allowed to refer to the first element 3741 * means that the index is allowed to refer to the first element
3742 * after the array. 3742 * after the array.
3743 */ 3743 */
3744static void 3744static void
3745check_array_index(tnode_t *tn, int amper) 3745check_array_index(tnode_t *tn, int amper)
3746{ 3746{
3747 int dim; 3747 int dim;
3748 tnode_t *ln, *rn; 3748 tnode_t *ln, *rn;
3749 int elsz; 3749 int elsz;
3750 int64_t con; 3750 int64_t con;
3751 3751
3752 ln = tn->tn_left; 3752 ln = tn->tn_left;
3753 rn = tn->tn_right; 3753 rn = tn->tn_right;
3754 3754
3755 /* We can only check constant indices. */ 3755 /* We can only check constant indices. */
3756 if (rn->tn_op != CON) 3756 if (rn->tn_op != CON)
3757 return; 3757 return;
3758 3758
3759 /* Return if the left node does not stem from an array. */ 3759 /* Return if the left node does not stem from an array. */
3760 if (ln->tn_op != AMPER) 3760 if (ln->tn_op != AMPER)
3761 return; 3761 return;
3762 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME) 3762 if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
3763 return; 3763 return;
3764 if (ln->tn_left->tn_type->t_tspec != ARRAY) 3764 if (ln->tn_left->tn_type->t_tspec != ARRAY)
3765 return; 3765 return;
3766 3766
3767 /* 3767 /*
3768 * For incomplete array types, we can print a warning only if 3768 * For incomplete array types, we can print a warning only if
3769 * the index is negative. 3769 * the index is negative.
3770 */ 3770 */
3771 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0) 3771 if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
3772 return; 3772 return;
3773 3773
3774 /* Get the size of one array element */ 3774 /* Get the size of one array element */
3775 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0) 3775 if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
3776 return; 3776 return;
3777 elsz /= CHAR_BIT; 3777 elsz /= CHAR_BIT;
3778 3778
3779 /* Change the unit of the index from bytes to element size. */ 3779 /* Change the unit of the index from bytes to element size. */
3780 if (tspec_is_uint(rn->tn_type->t_tspec)) { 3780 if (tspec_is_uint(rn->tn_type->t_tspec)) {
3781 con = (uint64_t)rn->tn_val->v_quad / elsz; 3781 con = (uint64_t)rn->tn_val->v_quad / elsz;
3782 } else { 3782 } else {
3783 con = rn->tn_val->v_quad / elsz; 3783 con = rn->tn_val->v_quad / elsz;
3784 } 3784 }
3785 3785
3786 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0); 3786 dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
3787 3787
3788 if (!tspec_is_uint(rn->tn_type->t_tspec) && con < 0) { 3788 if (!tspec_is_uint(rn->tn_type->t_tspec) && con < 0) {
3789 /* array subscript cannot be negative: %ld */ 3789 /* array subscript cannot be negative: %ld */
3790 warning(167, (long)con); 3790 warning(167, (long)con);
3791 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) { 3791 } else if (dim > 0 && (uint64_t)con >= (uint64_t)dim) {
3792 /* array subscript cannot be > %d: %ld */ 3792 /* array subscript cannot be > %d: %ld */
3793 warning(168, dim - 1, (long)con); 3793 warning(168, dim - 1, (long)con);
3794 } 3794 }
3795} 3795}
3796 3796
3797/* 3797/*
3798 * Check for ordered comparisons of unsigned values with 0. 3798 * Check for ordered comparisons of unsigned values with 0.
3799 */ 3799 */
3800static void 3800static void
3801check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn) 3801check_integer_comparison(op_t op, tnode_t *ln, tnode_t *rn)
3802{ 3802{
3803 tspec_t lt, rt; 3803 tspec_t lt, rt;
3804 mod_t *mp; 3804 mod_t *mp;
3805 3805
3806 lt = ln->tn_type->t_tspec; 3806 lt = ln->tn_type->t_tspec;
3807 rt = rn->tn_type->t_tspec; 3807 rt = rn->tn_type->t_tspec;
3808 mp = &modtab[op]; 3808 mp = &modtab[op];
3809 3809
3810 if (ln->tn_op != CON && rn->tn_op != CON) 3810 if (ln->tn_op != CON && rn->tn_op != CON)
3811 return; 3811 return;
3812 3812
3813 if (!tspec_is_int(lt) || !tspec_is_int(rt)) 3813 if (!tspec_is_int(lt) || !tspec_is_int(rt))
3814 return; 3814 return;
3815 3815
3816 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON && 3816 if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
3817 (rn->tn_val->v_quad < 0 || 3817 (rn->tn_val->v_quad < 0 ||
3818 rn->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) { 3818 rn->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) {
3819 /* nonportable character comparison, op %s */ 3819 /* nonportable character comparison, op %s */
3820 warning(230, mp->m_name); 3820 warning(230, mp->m_name);
3821 return; 3821 return;
3822 } 3822 }
3823 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON && 3823 if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
3824 (ln->tn_val->v_quad < 0 || 3824 (ln->tn_val->v_quad < 0 ||
3825 ln->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) { 3825 ln->tn_val->v_quad > (int)~(~0U << (CHAR_BIT - 1)))) {
3826 /* nonportable character comparison, op %s */ 3826 /* nonportable character comparison, op %s */
3827 warning(230, mp->m_name); 3827 warning(230, mp->m_name);
3828 return; 3828 return;
3829 } 3829 }
3830 if (tspec_is_uint(lt) && !tspec_is_uint(rt) && 3830 if (tspec_is_uint(lt) && !tspec_is_uint(rt) &&
3831 rn->tn_op == CON && rn->tn_val->v_quad <= 0) { 3831 rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
3832 if (rn->tn_val->v_quad < 0) { 3832 if (rn->tn_val->v_quad < 0) {
3833 /* comparison of %s with %s, op %s */ 3833 /* comparison of %s with %s, op %s */
3834 warning(162, type_name(ln->tn_type), 3834 warning(162, type_name(ln->tn_type),
3835 "negative constant", mp->m_name); 3835 "negative constant", mp->m_name);
3836 } else if (op == LT || op == GE || (hflag && op == LE)) { 3836 } else if (op == LT || op == GE || (hflag && op == LE)) {
3837 /* comparison of %s with %s, op %s */ 3837 /* comparison of %s with %s, op %s */
3838 warning(162, type_name(ln->tn_type), "0", mp->m_name); 3838 warning(162, type_name(ln->tn_type), "0", mp->m_name);
3839 } 3839 }
3840 return; 3840 return;
3841 } 3841 }
3842 if (tspec_is_uint(rt) && !tspec_is_uint(lt) && 3842 if (tspec_is_uint(rt) && !tspec_is_uint(lt) &&
3843 ln->tn_op == CON && ln->tn_val->v_quad <= 0) { 3843 ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
3844 if (ln->tn_val->v_quad < 0) { 3844 if (ln->tn_val->v_quad < 0) {
3845 /* comparison of %s with %s, op %s */ 3845 /* comparison of %s with %s, op %s */
3846 warning(162, "negative constant", 3846 warning(162, "negative constant",
3847 type_name(rn->tn_type), mp->m_name); 3847 type_name(rn->tn_type), mp->m_name);
3848 } else if (op == GT || op == LE || (hflag && op == GE)) { 3848 } else if (op == GT || op == LE || (hflag && op == GE)) {
3849 /* comparison of %s with %s, op %s */ 3849 /* comparison of %s with %s, op %s */
3850 warning(162, "0", type_name(rn->tn_type), mp->m_name); 3850 warning(162, "0", type_name(rn->tn_type), mp->m_name);
3851 } 3851 }
3852 return; 3852 return;
3853 } 3853 }
3854} 3854}
3855 3855
3856/* 3856/*
3857 * Takes an expression an returns 0 if this expression can be used 3857 * Takes an expression an returns 0 if this expression can be used
3858 * for static initialisation, otherwise -1. 3858 * for static initialisation, otherwise -1.
3859 * 3859 *
3860 * Constant initialisation expressions must be constant or an address 3860 * Constant initialisation expressions must be constant or an address
3861 * of a static object with an optional offset. In the first case, 3861 * of a static object with an optional offset. In the first case,
3862 * the result is returned in *offsp. In the second case, the static 3862 * the result is returned in *offsp. In the second case, the static
3863 * object is returned in *symp and the offset in *offsp. 3863 * object is returned in *symp and the offset in *offsp.
3864 * 3864 *
3865 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and 3865 * The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
3866 * CON. Type conversions are allowed if they do not change binary 3866 * CON. Type conversions are allowed if they do not change binary
3867 * representation (including width). 3867 * representation (including width).
3868 */ 3868 */
3869int 3869int
3870conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp) 3870conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
3871{ 3871{
3872 sym_t *sym; 3872 sym_t *sym;
3873 ptrdiff_t offs1, offs2; 3873 ptrdiff_t offs1, offs2;
3874 tspec_t t, ot; 3874 tspec_t t, ot;
3875 3875
3876 switch (tn->tn_op) { 3876 switch (tn->tn_op) {
3877 case MINUS: 3877 case MINUS:
3878 if (tn->tn_right->tn_op == CVT) 3878 if (tn->tn_right->tn_op == CVT)
3879 return conaddr(tn->tn_right, symp, offsp); 3879 return conaddr(tn->tn_right, symp, offsp);
3880 else if (tn->tn_right->tn_op != CON) 3880 else if (tn->tn_right->tn_op != CON)
3881 return -1; 3881 return -1;
3882 /* FALLTHROUGH */ 3882 /* FALLTHROUGH */
3883 case PLUS: 3883 case PLUS:
3884 offs1 = offs2 = 0; 3884 offs1 = offs2 = 0;
3885 if (tn->tn_left->tn_op == CON) { 3885 if (tn->tn_left->tn_op == CON) {
3886 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad; 3886 offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
3887 if (conaddr(tn->tn_right, &sym, &offs2) == -1) 3887 if (conaddr(tn->tn_right, &sym, &offs2) == -1)
3888 return -1; 3888 return -1;
3889 } else if (tn->tn_right->tn_op == CON) { 3889 } else if (tn->tn_right->tn_op == CON) {
3890 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad; 3890 offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
3891 if (tn->tn_op == MINUS) 3891 if (tn->tn_op == MINUS)
3892 offs2 = -offs2; 3892 offs2 = -offs2;
3893 if (conaddr(tn->tn_left, &sym, &offs1) == -1) 3893 if (conaddr(tn->tn_left, &sym, &offs1) == -1)
3894 return -1; 3894 return -1;
3895 } else { 3895 } else {
3896 return -1; 3896 return -1;
3897 } 3897 }
3898 *symp = sym; 3898 *symp = sym;
3899 *offsp = offs1 + offs2; 3899 *offsp = offs1 + offs2;
3900 break; 3900 break;
3901 case AMPER: 3901 case AMPER:
3902 if (tn->tn_left->tn_op == NAME) { 3902 if (tn->tn_left->tn_op == NAME) {
3903 *symp = tn->tn_left->tn_sym; 3903 *symp = tn->tn_left->tn_sym;
3904 *offsp = 0; 3904 *offsp = 0;
3905 } else if (tn->tn_left->tn_op == STRING) { 3905 } else if (tn->tn_left->tn_op == STRING) {
3906 /* 3906 /*
3907 * If this would be the front end of a compiler we 3907 * If this would be the front end of a compiler we
3908 * would return a label instead of 0. 3908 * would return a label instead of 0.
3909 */ 3909 */
3910 *offsp = 0; 3910 *offsp = 0;
3911 } 3911 }
3912 break; 3912 break;
3913 case CVT: 3913 case CVT:
3914 t = tn->tn_type->t_tspec; 3914 t = tn->tn_type->t_tspec;
3915 ot = tn->tn_left->tn_type->t_tspec; 3915 ot = tn->tn_left->tn_type->t_tspec;
3916 if ((!tspec_is_int(t) && t != PTR) || 3916 if ((!tspec_is_int(t) && t != PTR) ||
3917 (!tspec_is_int(ot) && ot != PTR)) { 3917 (!tspec_is_int(ot) && ot != PTR)) {
3918 return -1; 3918 return -1;
3919 } 3919 }
3920#ifdef notdef 3920#ifdef notdef
3921 /* 3921 /*
3922 * consider: 3922 * consider:
3923 * struct foo { 3923 * struct foo {
3924 * unsigned char a; 3924 * unsigned char a;
3925 * } f = { 3925 * } f = {
3926 * (u_char)(u_long)(&(((struct foo *)0)->a)) 3926 * (u_char)(u_long)(&(((struct foo *)0)->a))
3927 * }; 3927 * };
3928 * since psize(u_long) != psize(u_char) this fails. 3928 * since psize(u_long) != psize(u_char) this fails.
3929 */ 3929 */
3930 else if (psize(t) != psize(ot)) 3930 else if (psize(t) != psize(ot))
3931 return -1; 3931 return -1;
3932#endif 3932#endif
3933 if (conaddr(tn->tn_left, symp, offsp) == -1) 3933 if (conaddr(tn->tn_left, symp, offsp) == -1)
3934 return -1; 3934 return -1;
3935 break; 3935 break;
3936 default: 3936 default:
3937 return -1; 3937 return -1;
3938 } 3938 }
3939 return 0; 3939 return 0;
3940} 3940}
3941 3941
3942/* 3942/*
3943 * Concatenate two string constants. 3943 * Concatenate two string constants.
3944 */ 3944 */
3945strg_t * 3945strg_t *
3946cat_strings(strg_t *strg1, strg_t *strg2) 3946cat_strings(strg_t *strg1, strg_t *strg2)
3947{ 3947{
3948 size_t len1, len2, len; 3948 size_t len1, len2, len;
3949 3949
3950 if (strg1->st_tspec != strg2->st_tspec) { 3950 if (strg1->st_tspec != strg2->st_tspec) {
3951 /* cannot concatenate wide and regular string literals */ 3951 /* cannot concatenate wide and regular string literals */
3952 error(292); 3952 error(292);
3953 return strg1; 3953 return strg1;
3954 } 3954 }
3955 3955
3956 len1 = strg1->st_len; 3956 len1 = strg1->st_len;
3957 len2 = strg2->st_len + 1; /* + NUL */ 3957 len2 = strg2->st_len + 1; /* + NUL */
3958 len = len1 + len2; 3958 len = len1 + len2;
3959 3959
3960#define COPY(F) \ 3960#define COPY(F) \
3961 do { \ 3961 do { \
3962 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \ 3962 strg1->F = xrealloc(strg1->F, len * sizeof(*strg1->F)); \
3963 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \ 3963 (void)memcpy(strg1->F + len1, strg2->F, len2 * sizeof(*strg1->F)); \
3964 free(strg2->F); \ 3964 free(strg2->F); \
3965 } while (/*CONSTCOND*/0) 3965 } while (/*CONSTCOND*/0)
3966 3966
3967 if (strg1->st_tspec == CHAR) 3967 if (strg1->st_tspec == CHAR)
3968 COPY(st_cp); 3968 COPY(st_cp);
3969 else 3969 else
3970 COPY(st_wcp); 3970 COPY(st_wcp);
3971 3971
3972 strg1->st_len = len - 1; /* - NUL */ 3972 strg1->st_len = len - 1; /* - NUL */
3973 free(strg2); 3973 free(strg2);
3974 3974
3975 return strg1; 3975 return strg1;
3976} 3976}
3977 3977
3978/* 3978/*
3979 * Print a warning if the given node has operands which should be 3979 * Print a warning if the given node has operands which should be
3980 * parenthesized. 3980 * parenthesized.
3981 * 3981 *
3982 * XXX Does not work if an operand is a constant expression. Constant 3982 * XXX Does not work if an operand is a constant expression. Constant
3983 * expressions are already folded. 3983 * expressions are already folded.
3984 */ 3984 */
3985static void 3985static void
3986check_precedence_confusion(tnode_t *tn) 3986check_precedence_confusion(tnode_t *tn)
3987{ 3987{
3988 tnode_t *ln, *rn; 3988 tnode_t *ln, *rn;
3989 op_t lop, rop = NOOP; 3989 op_t lop, rop = NOOP;
3990 int lparn, rparn = 0; 3990 int lparn, rparn = 0;
3991 mod_t *mp; 3991 mod_t *mp;
3992 int dowarn; 3992 int dowarn;
3993 3993
3994 if (!hflag) 3994 if (!hflag)
3995 return; 3995 return;
3996 3996
3997 mp = &modtab[tn->tn_op]; 3997 mp = &modtab[tn->tn_op];
3998 3998
3999 lparn = 0; 3999 lparn = 0;
4000 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left) 4000 for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
4001 lparn |= ln->tn_parenthesized; 4001 lparn |= ln->tn_parenthesized;
4002 lparn |= ln->tn_parenthesized; 4002 lparn |= ln->tn_parenthesized;
4003 lop = ln->tn_op; 4003 lop = ln->tn_op;
4004 4004
4005 if (mp->m_binary) { 4005 if (mp->m_binary) {
4006 rparn = 0; 4006 rparn = 0;
4007 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left) 4007 for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
4008 rparn |= rn->tn_parenthesized; 4008 rparn |= rn->tn_parenthesized;
4009 rparn |= rn->tn_parenthesized; 4009 rparn |= rn->tn_parenthesized;
4010 rop = rn->tn_op; 4010 rop = rn->tn_op;
4011 } 4011 }
4012 4012
4013 dowarn = 0; 4013 dowarn = 0;
4014 4014
4015 switch (tn->tn_op) { 4015 switch (tn->tn_op) {
4016 case SHL: 4016 case SHL:
4017 case SHR: 4017 case SHR:
4018 if (!lparn && (lop == PLUS || lop == MINUS)) { 4018 if (!lparn && (lop == PLUS || lop == MINUS)) {
4019 dowarn = 1; 4019 dowarn = 1;
4020 } else if (!rparn && (rop == PLUS || rop == MINUS)) { 4020 } else if (!rparn && (rop == PLUS || rop == MINUS)) {
4021 dowarn = 1; 4021 dowarn = 1;
4022 } 4022 }
4023 break; 4023 break;
4024 case LOGOR: 4024 case LOGOR:
4025 if (!lparn && lop == LOGAND) { 4025 if (!lparn && lop == LOGAND) {
4026 dowarn = 1; 4026 dowarn = 1;
4027 } else if (!rparn && rop == LOGAND) { 4027 } else if (!rparn && rop == LOGAND) {
4028 dowarn = 1; 4028 dowarn = 1;
4029 } 4029 }
4030 break; 4030 break;
4031 case AND: 4031 case AND:
4032 case XOR: 4032 case XOR:
4033 case OR: 4033 case OR:
4034 if (!lparn && lop != tn->tn_op) { 4034 if (!lparn && lop != tn->tn_op) {
4035 if (lop == PLUS || lop == MINUS) { 4035 if (lop == PLUS || lop == MINUS) {
4036 dowarn = 1; 4036 dowarn = 1;
4037 } else if (lop == AND || lop == XOR) { 4037 } else if (lop == AND || lop == XOR) {
4038 dowarn = 1; 4038 dowarn = 1;
4039 } 4039 }
4040 } 4040 }
4041 if (!dowarn && !rparn && rop != tn->tn_op) { 4041 if (!dowarn && !rparn && rop != tn->tn_op) {
4042 if (rop == PLUS || rop == MINUS) { 4042 if (rop == PLUS || rop == MINUS) {
4043 dowarn = 1; 4043 dowarn = 1;
4044 } else if (rop == AND || rop == XOR) { 4044 } else if (rop == AND || rop == XOR) {
4045 dowarn = 1; 4045 dowarn = 1;
4046 } 4046 }
4047 } 4047 }
4048 break; 4048 break;
4049 /* LINTED206: (enumeration values not handled in switch) */ 4049 /* LINTED206: (enumeration values not handled in switch) */
4050 case DECAFT: 4050 case DECAFT:
4051 case XORASS: 4051 case XORASS:
4052 case SHLASS: 4052 case SHLASS:
4053 case NOOP: 4053 case NOOP:
4054 case ARROW: 4054 case ARROW:
4055 case ORASS: 4055 case ORASS:
4056 case POINT: 4056 case POINT:
4057 case NAME: 4057 case NAME:
4058 case NOT: 4058 case NOT:
4059 case COMPL: 4059 case COMPL:
4060 case CON: 4060 case CON:
4061 case INC: 4061 case INC:
4062 case STRING: 4062 case STRING:
4063 case DEC: 4063 case DEC:
4064 case INCBEF: 4064 case INCBEF:
4065 case DECBEF: 4065 case DECBEF:
4066 case INCAFT: 4066 case INCAFT:
4067 case FSEL: 4067 case FSEL:
4068 case CALL: 4068 case CALL:
4069 case COMMA: 4069 case COMMA:
4070 case CVT: 4070 case CVT:
4071 case ICALL: 4071 case ICALL:
4072 case LOAD: 4072 case LOAD:
4073 case PUSH: 4073 case PUSH:
4074 case RETURN: 4074 case RETURN:
4075 case INIT: 4075 case INIT:
4076 case CASE: 4076 case CASE:
4077 case FARG: 4077 case FARG:
4078 case SUBASS: 4078 case SUBASS:
4079 case ADDASS: 4079 case ADDASS:
4080 case MODASS: 4080 case MODASS:
4081 case DIVASS: 4081 case DIVASS:
4082 case MULASS: 4082 case MULASS:
4083 case ASSIGN: 4083 case ASSIGN:
4084 case COLON: 4084 case COLON:
4085 case QUEST: 4085 case QUEST:
4086 case LOGAND: 4086 case LOGAND:
4087 case NE: 4087 case NE:
4088 case EQ: 4088 case EQ:
4089 case GE: 4089 case GE:
4090 case GT: 4090 case GT:
4091 case LE: 4091 case LE:
4092 case LT: 4092 case LT:
4093 case MINUS: 4093 case MINUS:
4094 case PLUS: 4094 case PLUS: