]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-typeck.c
Daily bump.
[thirdparty/gcc.git] / gcc / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
06ceef4e 2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
339a28b9 3 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
400fbf9f 4
1322177d 5This file is part of GCC.
400fbf9f 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
400fbf9f 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
400fbf9f
JW
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
400fbf9f
JW
21
22
23/* This file is part of the C front end.
24 It contains routines to build C expressions given their operands,
25 including computing the types of the result, C-specific error checks,
26 and some optimization.
27
28 There are also routines to build RETURN_STMT nodes and CASE_STMT nodes,
29 and to process initializations in declarations (since they work
30 like a strange sort of assignment). */
31
32#include "config.h"
670ee920 33#include "system.h"
4977bab6
ZW
34#include "coretypes.h"
35#include "tm.h"
742b62e7 36#include "rtl.h"
400fbf9f
JW
37#include "tree.h"
38#include "c-tree.h"
6baf1cc8 39#include "tm_p.h"
400fbf9f 40#include "flags.h"
e14417fa 41#include "output.h"
234042f4 42#include "expr.h"
5f6da302 43#include "toplev.h"
ab87f8c8 44#include "intl.h"
4dd7201e 45#include "ggc.h"
672a6f42 46#include "target.h"
400fbf9f 47
b71c7f8a 48/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 49 message within this initializer. */
b71c7f8a 50static int missing_braces_mentioned;
103b7b17 51
7e585d16
ZW
52/* 1 if we explained undeclared var errors. */
53static int undeclared_variable_notice;
54
f55ade6e 55static tree qualify_type (tree, tree);
d1bd0ded 56static int tagged_types_tu_compatible_p (tree, tree, int);
f55ade6e 57static int comp_target_types (tree, tree, int);
d1bd0ded
GK
58static int function_types_compatible_p (tree, tree, int);
59static int type_lists_compatible_p (tree, tree, int);
f55ade6e
AJ
60static tree decl_constant_value_for_broken_optimization (tree);
61static tree default_function_array_conversion (tree);
62static tree lookup_field (tree, tree);
63static void undeclared_variable (tree);
64static tree convert_arguments (tree, tree, tree, tree);
65static tree pointer_diff (tree, tree);
66static tree unary_complex_lvalue (enum tree_code, tree, int);
67static void pedantic_lvalue_warning (enum tree_code);
68static tree internal_build_compound_expr (tree, int);
69static tree convert_for_assignment (tree, tree, const char *, tree, tree,
70 int);
71static void warn_for_assignment (const char *, const char *, tree, int);
72static tree valid_compound_expr_initializer (tree, tree);
73static void push_string (const char *);
74static void push_member_name (tree);
75static void push_array_bounds (int);
76static int spelling_length (void);
77static char *print_spelling (char *);
78static void warning_init (const char *);
79static tree digest_init (tree, tree, int);
80static void output_init_element (tree, tree, tree, int);
81static void output_pending_init_elements (int);
82static int set_designator (int);
83static void push_range_stack (tree);
84static void add_pending_init (tree, tree);
85static void set_nonincremental_init (void);
86static void set_nonincremental_init_from_string (tree);
87static tree find_init_member (tree);
400fbf9f
JW
88\f
89/* Do `exp = require_complete_type (exp);' to make sure exp
90 does not have an incomplete type. (That includes void types.) */
91
92tree
2f6e4e97 93require_complete_type (tree value)
400fbf9f
JW
94{
95 tree type = TREE_TYPE (value);
96
c3d5c3fa 97 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
98 return error_mark_node;
99
400fbf9f 100 /* First, detect a valid value with a complete type. */
d0f062fb 101 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
102 return value;
103
7a228918 104 c_incomplete_type_error (value, type);
400fbf9f
JW
105 return error_mark_node;
106}
107
108/* Print an error message for invalid use of an incomplete type.
109 VALUE is the expression that was used (or 0 if that isn't known)
110 and TYPE is the type that was invalid. */
111
112void
2f6e4e97 113c_incomplete_type_error (tree value, tree type)
400fbf9f 114{
5d5993dd 115 const char *type_code_string;
400fbf9f
JW
116
117 /* Avoid duplicate error message. */
118 if (TREE_CODE (type) == ERROR_MARK)
119 return;
120
121 if (value != 0 && (TREE_CODE (value) == VAR_DECL
122 || TREE_CODE (value) == PARM_DECL))
123 error ("`%s' has an incomplete type",
124 IDENTIFIER_POINTER (DECL_NAME (value)));
125 else
126 {
127 retry:
128 /* We must print an error message. Be clever about what it says. */
129
130 switch (TREE_CODE (type))
131 {
132 case RECORD_TYPE:
ab87f8c8 133 type_code_string = "struct";
400fbf9f
JW
134 break;
135
136 case UNION_TYPE:
ab87f8c8 137 type_code_string = "union";
400fbf9f
JW
138 break;
139
140 case ENUMERAL_TYPE:
ab87f8c8 141 type_code_string = "enum";
400fbf9f
JW
142 break;
143
144 case VOID_TYPE:
145 error ("invalid use of void expression");
146 return;
147
148 case ARRAY_TYPE:
149 if (TYPE_DOMAIN (type))
150 {
fba78abb
RH
151 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
152 {
153 error ("invalid use of flexible array member");
154 return;
155 }
400fbf9f
JW
156 type = TREE_TYPE (type);
157 goto retry;
158 }
159 error ("invalid use of array with unspecified bounds");
160 return;
161
162 default:
163 abort ();
164 }
165
166 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ab87f8c8
JL
167 error ("invalid use of undefined type `%s %s'",
168 type_code_string, IDENTIFIER_POINTER (TYPE_NAME (type)));
400fbf9f
JW
169 else
170 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
171 error ("invalid use of incomplete typedef `%s'",
172 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (type))));
173 }
174}
175
ab393bf1
NB
176/* Given a type, apply default promotions wrt unnamed function
177 arguments and return the new type. */
178
179tree
2f6e4e97 180c_type_promotes_to (tree type)
ab393bf1
NB
181{
182 if (TYPE_MAIN_VARIANT (type) == float_type_node)
183 return double_type_node;
184
185 if (c_promoting_integer_type_p (type))
186 {
187 /* Preserve unsignedness if not really getting any wider. */
188 if (TREE_UNSIGNED (type)
189 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
190 return unsigned_type_node;
191 return integer_type_node;
192 }
193
194 return type;
195}
196
400fbf9f
JW
197/* Return a variant of TYPE which has all the type qualifiers of LIKE
198 as well as those of TYPE. */
199
200static tree
2f6e4e97 201qualify_type (tree type, tree like)
400fbf9f 202{
2f6e4e97 203 return c_build_qualified_type (type,
afbadaa7 204 TYPE_QUALS (type) | TYPE_QUALS (like));
400fbf9f
JW
205}
206\f
207/* Return the common type of two types.
208 We assume that comptypes has already been done and returned 1;
6cb72a7d
RS
209 if that isn't so, this may crash. In particular, we assume that qualifiers
210 match.
400fbf9f
JW
211
212 This is the type for the result of most arithmetic operations
6cb72a7d 213 if the operands have the given two types. */
400fbf9f
JW
214
215tree
2f6e4e97 216common_type (tree t1, tree t2)
400fbf9f 217{
b3694847
SS
218 enum tree_code code1;
219 enum tree_code code2;
4b027d16 220 tree attributes;
400fbf9f
JW
221
222 /* Save time if the two types are the same. */
223
224 if (t1 == t2) return t1;
225
226 /* If one type is nonsense, use the other. */
227 if (t1 == error_mark_node)
228 return t2;
229 if (t2 == error_mark_node)
230 return t1;
231
d9525bec 232 /* Merge the attributes. */
f6897b10 233 attributes = (*targetm.merge_type_attributes) (t1, t2);
4b027d16 234
400fbf9f
JW
235 /* Treat an enum type as the unsigned integer type of the same width. */
236
237 if (TREE_CODE (t1) == ENUMERAL_TYPE)
b0c48229 238 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
400fbf9f 239 if (TREE_CODE (t2) == ENUMERAL_TYPE)
b0c48229 240 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
400fbf9f
JW
241
242 code1 = TREE_CODE (t1);
243 code2 = TREE_CODE (t2);
244
75326e8c
RK
245 /* If one type is complex, form the common type of the non-complex
246 components, then make that complex. Use T1 or T2 if it is the
247 required type. */
b6a10c9f
RS
248 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
249 {
75326e8c
RK
250 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
251 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
252 tree subtype = common_type (subtype1, subtype2);
253
254 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
4b027d16 255 return build_type_attribute_variant (t1, attributes);
75326e8c 256 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
4b027d16 257 return build_type_attribute_variant (t2, attributes);
b6a10c9f 258 else
4b027d16
RK
259 return build_type_attribute_variant (build_complex_type (subtype),
260 attributes);
b6a10c9f
RS
261 }
262
400fbf9f
JW
263 switch (code1)
264 {
265 case INTEGER_TYPE:
266 case REAL_TYPE:
267 /* If only one is real, use it as the result. */
268
269 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
4b027d16 270 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
271
272 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
4b027d16 273 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
274
275 /* Both real or both integers; use the one with greater precision. */
276
277 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
4b027d16 278 return build_type_attribute_variant (t1, attributes);
400fbf9f 279 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
4b027d16 280 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
281
282 /* Same precision. Prefer longs to ints even when same size. */
283
36618528
RS
284 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
285 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
4b027d16
RK
286 return build_type_attribute_variant (long_unsigned_type_node,
287 attributes);
400fbf9f 288
36618528
RS
289 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
290 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
400fbf9f
JW
291 {
292 /* But preserve unsignedness from the other type,
293 since long cannot hold all the values of an unsigned int. */
294 if (TREE_UNSIGNED (t1) || TREE_UNSIGNED (t2))
4b027d16
RK
295 t1 = long_unsigned_type_node;
296 else
297 t1 = long_integer_type_node;
298 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
299 }
300
e9a25f70
JL
301 /* Likewise, prefer long double to double even if same size. */
302 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
303 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
304 return build_type_attribute_variant (long_double_type_node,
305 attributes);
306
400fbf9f
JW
307 /* Otherwise prefer the unsigned one. */
308
309 if (TREE_UNSIGNED (t1))
4b027d16
RK
310 return build_type_attribute_variant (t1, attributes);
311 else
312 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
313
314 case POINTER_TYPE:
400fbf9f
JW
315 /* For two pointers, do this recursively on the target type,
316 and combine the qualifiers of the two types' targets. */
8706edbc
RS
317 /* This code was turned off; I don't know why.
318 But ANSI C specifies doing this with the qualifiers.
319 So I turned it on again. */
400fbf9f 320 {
3932261a
MM
321 tree pointed_to_1 = TREE_TYPE (t1);
322 tree pointed_to_2 = TREE_TYPE (t2);
323 tree target = common_type (TYPE_MAIN_VARIANT (pointed_to_1),
324 TYPE_MAIN_VARIANT (pointed_to_2));
2f6e4e97
AJ
325 t1 = build_pointer_type (c_build_qualified_type
326 (target,
327 TYPE_QUALS (pointed_to_1) |
3932261a 328 TYPE_QUALS (pointed_to_2)));
4b027d16 329 return build_type_attribute_variant (t1, attributes);
400fbf9f 330 }
400fbf9f
JW
331
332 case ARRAY_TYPE:
333 {
334 tree elt = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
335 /* Save space: see if the result is identical to one of the args. */
336 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
4b027d16 337 return build_type_attribute_variant (t1, attributes);
400fbf9f 338 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
4b027d16 339 return build_type_attribute_variant (t2, attributes);
400fbf9f 340 /* Merge the element types, and have a size if either arg has one. */
4b027d16
RK
341 t1 = build_array_type (elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
342 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
343 }
344
345 case FUNCTION_TYPE:
346 /* Function types: prefer the one that specified arg types.
347 If both do, merge the arg types. Also merge the return types. */
348 {
349 tree valtype = common_type (TREE_TYPE (t1), TREE_TYPE (t2));
350 tree p1 = TYPE_ARG_TYPES (t1);
351 tree p2 = TYPE_ARG_TYPES (t2);
352 int len;
353 tree newargs, n;
354 int i;
355
356 /* Save space: see if the result is identical to one of the args. */
357 if (valtype == TREE_TYPE (t1) && ! TYPE_ARG_TYPES (t2))
4b027d16 358 return build_type_attribute_variant (t1, attributes);
400fbf9f 359 if (valtype == TREE_TYPE (t2) && ! TYPE_ARG_TYPES (t1))
4b027d16 360 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
361
362 /* Simple way if one arg fails to specify argument types. */
363 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16
RK
364 {
365 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
366 return build_type_attribute_variant (t1, attributes);
367 }
400fbf9f 368 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
369 {
370 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
371 return build_type_attribute_variant (t1, attributes);
372 }
400fbf9f
JW
373
374 /* If both args specify argument types, we must merge the two
375 lists, argument by argument. */
376
2f4e8f2b 377 pushlevel (0);
eb1dfbb2 378 declare_parm_level ();
2f4e8f2b 379
400fbf9f
JW
380 len = list_length (p1);
381 newargs = 0;
382
383 for (i = 0; i < len; i++)
8d9bfdc5 384 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
385
386 n = newargs;
387
388 for (; p1;
389 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
390 {
391 /* A null type means arg type is not specified.
392 Take whatever the other function type has. */
393 if (TREE_VALUE (p1) == 0)
394 {
395 TREE_VALUE (n) = TREE_VALUE (p2);
396 goto parm_done;
397 }
398 if (TREE_VALUE (p2) == 0)
399 {
400 TREE_VALUE (n) = TREE_VALUE (p1);
401 goto parm_done;
402 }
2f6e4e97 403
400fbf9f
JW
404 /* Given wait (union {union wait *u; int *i} *)
405 and wait (union wait *),
406 prefer union wait * as type of parm. */
407 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
408 && TREE_VALUE (p1) != TREE_VALUE (p2))
409 {
410 tree memb;
411 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
412 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
413 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p2),
414 COMPARE_STRICT))
400fbf9f
JW
415 {
416 TREE_VALUE (n) = TREE_VALUE (p2);
417 if (pedantic)
89abf8d1 418 pedwarn ("function types not truly compatible in ISO C");
400fbf9f
JW
419 goto parm_done;
420 }
421 }
422 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
423 && TREE_VALUE (p2) != TREE_VALUE (p1))
424 {
425 tree memb;
426 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
427 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
428 if (comptypes (TREE_TYPE (memb), TREE_VALUE (p1),
429 COMPARE_STRICT))
400fbf9f
JW
430 {
431 TREE_VALUE (n) = TREE_VALUE (p1);
432 if (pedantic)
89abf8d1 433 pedwarn ("function types not truly compatible in ISO C");
400fbf9f
JW
434 goto parm_done;
435 }
436 }
437 TREE_VALUE (n) = common_type (TREE_VALUE (p1), TREE_VALUE (p2));
438 parm_done: ;
439 }
440
2f4e8f2b
JJ
441 poplevel (0, 0, 0);
442
4b027d16 443 t1 = build_function_type (valtype, newargs);
0f41302f 444 /* ... falls through ... */
400fbf9f
JW
445 }
446
447 default:
4b027d16 448 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
449 }
450
451}
452\f
453/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
454 or various other operations. Return 2 if they are compatible
455 but a warning may be needed if you use them together. */
456
457int
d1bd0ded 458comptypes (tree type1, tree type2, int flags)
400fbf9f 459{
b3694847
SS
460 tree t1 = type1;
461 tree t2 = type2;
4b027d16 462 int attrval, val;
400fbf9f
JW
463
464 /* Suppress errors caused by previously reported errors. */
465
8d47dfc5
RH
466 if (t1 == t2 || !t1 || !t2
467 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
468 return 1;
469
21318741
RK
470 /* If either type is the internal version of sizetype, return the
471 language version. */
472 if (TREE_CODE (t1) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t1)
473 && TYPE_DOMAIN (t1) != 0)
474 t1 = TYPE_DOMAIN (t1);
475
476 if (TREE_CODE (t2) == INTEGER_TYPE && TYPE_IS_SIZETYPE (t2)
477 && TYPE_DOMAIN (t2) != 0)
478 t2 = TYPE_DOMAIN (t2);
479
2f6e4e97 480 /* Treat an enum type as the integer type of the same width and
b8c21346 481 signedness. */
400fbf9f
JW
482
483 if (TREE_CODE (t1) == ENUMERAL_TYPE)
b0c48229 484 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TREE_UNSIGNED (t1));
400fbf9f 485 if (TREE_CODE (t2) == ENUMERAL_TYPE)
b0c48229 486 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TREE_UNSIGNED (t2));
400fbf9f
JW
487
488 if (t1 == t2)
489 return 1;
490
491 /* Different classes of types can't be compatible. */
492
493 if (TREE_CODE (t1) != TREE_CODE (t2)) return 0;
494
495 /* Qualifiers must match. */
496
3932261a 497 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
498 return 0;
499
08632da2
RS
500 /* Allow for two different type nodes which have essentially the same
501 definition. Note that we already checked for equality of the type
38e01259 502 qualifiers (just above). */
400fbf9f
JW
503
504 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
505 return 1;
506
4b027d16 507 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
f6897b10 508 if (! (attrval = (*targetm.comp_type_attributes) (t1, t2)))
4b027d16
RK
509 return 0;
510
511 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
512 val = 0;
513
400fbf9f
JW
514 switch (TREE_CODE (t1))
515 {
516 case POINTER_TYPE:
4b027d16 517 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
d1bd0ded 518 ? 1 : comptypes (TREE_TYPE (t1), TREE_TYPE (t2), flags));
4b027d16 519 break;
400fbf9f
JW
520
521 case FUNCTION_TYPE:
d1bd0ded 522 val = function_types_compatible_p (t1, t2, flags);
4b027d16 523 break;
400fbf9f
JW
524
525 case ARRAY_TYPE:
526 {
400fbf9f
JW
527 tree d1 = TYPE_DOMAIN (t1);
528 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
529 bool d1_variable, d2_variable;
530 bool d1_zero, d2_zero;
4b027d16 531 val = 1;
400fbf9f
JW
532
533 /* Target types must match incl. qualifiers. */
534 if (TREE_TYPE (t1) != TREE_TYPE (t2)
d1bd0ded
GK
535 && 0 == (val = comptypes (TREE_TYPE (t1), TREE_TYPE (t2),
536 flags)))
400fbf9f
JW
537 return 0;
538
539 /* Sizes must match unless one is missing or variable. */
3f85558f 540 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 541 break;
400fbf9f 542
3f85558f
RH
543 d1_zero = ! TYPE_MAX_VALUE (d1);
544 d2_zero = ! TYPE_MAX_VALUE (d2);
545
546 d1_variable = (! d1_zero
547 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
548 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
549 d2_variable = (! d2_zero
550 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
551 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
552
553 if (d1_variable || d2_variable)
554 break;
555 if (d1_zero && d2_zero)
556 break;
557 if (d1_zero || d2_zero
558 || ! tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
05bccae2
RK
559 || ! tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
560 val = 0;
561
4b027d16 562 break;
400fbf9f
JW
563 }
564
565 case RECORD_TYPE:
37fa72e9 566 if (c_dialect_objc () && objc_comptypes (t1, t2, 0) == 1)
4b027d16 567 val = 1;
d1bd0ded
GK
568
569 case ENUMERAL_TYPE:
570 case UNION_TYPE:
571 if (val != 1 && (flags & COMPARE_DIFFERENT_TU))
572 val = tagged_types_tu_compatible_p (t1, t2, flags);
4b027d16 573 break;
e9a25f70 574
62e1dfcf
NC
575 case VECTOR_TYPE:
576 /* The target might allow certain vector types to be compatible. */
c8e4f0e9
AH
577 val = (*targetm.vector_opaque_p) (t1)
578 || (*targetm.vector_opaque_p) (t2);
62e1dfcf
NC
579 break;
580
e9a25f70
JL
581 default:
582 break;
400fbf9f 583 }
4b027d16 584 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
585}
586
587/* Return 1 if TTL and TTR are pointers to types that are equivalent,
1074d9d4
NP
588 ignoring their qualifiers. REFLEXIVE is only used by ObjC - set it
589 to 1 or 0 depending if the check of the pointer types is meant to
590 be reflexive or not (typically, assignments are not reflexive,
591 while comparisons are reflexive).
592*/
400fbf9f
JW
593
594static int
2f6e4e97 595comp_target_types (tree ttl, tree ttr, int reflexive)
400fbf9f 596{
392202b0 597 int val;
8b40563c 598
b50d021d 599 /* Give objc_comptypes a crack at letting these types through. */
1074d9d4 600 if ((val = objc_comptypes (ttl, ttr, reflexive)) >= 0)
392202b0 601 return val;
8b40563c 602
392202b0 603 val = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (ttl)),
d1bd0ded 604 TYPE_MAIN_VARIANT (TREE_TYPE (ttr)), COMPARE_STRICT);
8b40563c 605
400fbf9f
JW
606 if (val == 2 && pedantic)
607 pedwarn ("types are not quite compatible");
608 return val;
609}
610\f
611/* Subroutines of `comptypes'. */
612
d1bd0ded
GK
613/* The C standard says that two structures in different translation
614 units are compatible with each other only if the types of their
615 fields are compatible (among other things). So, consider two copies
616 of this structure: */
617
618struct tagged_tu_seen {
619 const struct tagged_tu_seen * next;
620 tree t1;
621 tree t2;
622};
623
624/* Can they be compatible with each other? We choose to break the
625 recursion by allowing those types to be compatible. */
626
627static const struct tagged_tu_seen * tagged_tu_seen_base;
628
629/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
630 compatible. If the two types are not the same (which has been
631 checked earlier), this can only happen when multiple translation
632 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
633 rules. */
634
635static int
636tagged_types_tu_compatible_p (tree t1, tree t2, int flags)
637{
638 tree s1, s2;
639 bool needs_warning = false;
640
641 /* We have to verify that the tags of the types are the same. This
642 is harder than it looks because this may be a typedef, so we have
643 to go look at the original type. It may even be a typedef of a
4ed43216 644 typedef... */
d1bd0ded
GK
645 while (TYPE_NAME (t1) && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL)
646 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
647
648 while (TYPE_NAME (t2) && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL)
649 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
650
651 /* C90 didn't have the requirement that the two tags be the same. */
652 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
653 return 0;
654
655 /* C90 didn't say what happened if one or both of the types were
656 incomplete; we choose to follow C99 rules here, which is that they
657 are compatible. */
658 if (TYPE_SIZE (t1) == NULL
659 || TYPE_SIZE (t2) == NULL)
660 return 1;
661
662 {
663 const struct tagged_tu_seen * tts_i;
664 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
665 if (tts_i->t1 == t1 && tts_i->t2 == t2)
666 return 1;
667 }
668
669 switch (TREE_CODE (t1))
670 {
671 case ENUMERAL_TYPE:
672 {
673 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
674 return 0;
675
676 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
677 {
678 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
679 if (s2 == NULL
680 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
681 return 0;
682 }
683 return 1;
684 }
685
686 case UNION_TYPE:
687 {
688 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
689 return 0;
690
691 for (s1 = TYPE_FIELDS (t1); s1; s1 = TREE_CHAIN (s1))
692 {
693 bool ok = false;
694 struct tagged_tu_seen tts;
695
696 tts.next = tagged_tu_seen_base;
697 tts.t1 = t1;
698 tts.t2 = t2;
699 tagged_tu_seen_base = &tts;
700
701 if (DECL_NAME (s1) != NULL)
702 for (s2 = TYPE_VALUES (t2); s2; s2 = TREE_CHAIN (s2))
703 if (DECL_NAME (s1) == DECL_NAME (s2))
704 {
705 int result;
706 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
707 if (result == 0)
708 break;
709 if (result == 2)
710 needs_warning = true;
711
712 if (TREE_CODE (s1) == FIELD_DECL
713 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
714 DECL_FIELD_BIT_OFFSET (s2)) != 1)
715 break;
716
717 ok = true;
718 break;
719 }
720 tagged_tu_seen_base = tts.next;
721 if (! ok)
722 return 0;
723 }
724 return needs_warning ? 2 : 1;
725 }
726
727 case RECORD_TYPE:
728 {
729 struct tagged_tu_seen tts;
730
731 tts.next = tagged_tu_seen_base;
732 tts.t1 = t1;
733 tts.t2 = t2;
734 tagged_tu_seen_base = &tts;
735
736 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
737 s1 && s2;
738 s1 = TREE_CHAIN (s1), s2 = TREE_CHAIN (s2))
739 {
740 int result;
741 if (TREE_CODE (s1) != TREE_CODE (s2)
742 || DECL_NAME (s1) != DECL_NAME (s2))
743 break;
744 result = comptypes (TREE_TYPE (s1), TREE_TYPE (s2), flags);
745 if (result == 0)
746 break;
747 if (result == 2)
748 needs_warning = true;
749
750 if (TREE_CODE (s1) == FIELD_DECL
751 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
752 DECL_FIELD_BIT_OFFSET (s2)) != 1)
753 break;
754 }
755 tagged_tu_seen_base = tts.next;
756 if (s1 && s2)
757 return 0;
758 return needs_warning ? 2 : 1;
759 }
760
761 default:
762 abort ();
763 }
764}
765
400fbf9f
JW
766/* Return 1 if two function types F1 and F2 are compatible.
767 If either type specifies no argument types,
768 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 769 Otherwise, if one type specifies only the number of arguments,
400fbf9f
JW
770 the other must specify that number of self-promoting arg types.
771 Otherwise, the argument types must match. */
772
773static int
d1bd0ded 774function_types_compatible_p (tree f1, tree f2, int flags)
400fbf9f
JW
775{
776 tree args1, args2;
777 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
778 int val = 1;
779 int val1;
a6fdc086
GK
780 tree ret1, ret2;
781
782 ret1 = TREE_TYPE (f1);
783 ret2 = TREE_TYPE (f2);
784
785 /* 'volatile' qualifiers on a function's return type mean the function
786 is noreturn. */
787 if (pedantic && TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
788 pedwarn ("function return types not compatible due to `volatile'");
789 if (TYPE_VOLATILE (ret1))
790 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
791 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
792 if (TYPE_VOLATILE (ret2))
793 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
794 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
d1bd0ded 795 val = comptypes (ret1, ret2, flags);
a6fdc086 796 if (val == 0)
400fbf9f
JW
797 return 0;
798
799 args1 = TYPE_ARG_TYPES (f1);
800 args2 = TYPE_ARG_TYPES (f2);
801
802 /* An unspecified parmlist matches any specified parmlist
803 whose argument types don't need default promotions. */
804
805 if (args1 == 0)
806 {
807 if (!self_promoting_args_p (args2))
808 return 0;
809 /* If one of these types comes from a non-prototype fn definition,
810 compare that with the other type's arglist.
811 If they don't match, ask for a warning (but no error). */
812 if (TYPE_ACTUAL_ARG_TYPES (f1)
d1bd0ded
GK
813 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
814 flags))
400fbf9f
JW
815 val = 2;
816 return val;
817 }
818 if (args2 == 0)
819 {
820 if (!self_promoting_args_p (args1))
821 return 0;
822 if (TYPE_ACTUAL_ARG_TYPES (f2)
d1bd0ded
GK
823 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
824 flags))
400fbf9f
JW
825 val = 2;
826 return val;
827 }
828
829 /* Both types have argument lists: compare them and propagate results. */
d1bd0ded 830 val1 = type_lists_compatible_p (args1, args2, flags);
400fbf9f
JW
831 return val1 != 1 ? val1 : val;
832}
833
834/* Check two lists of types for compatibility,
835 returning 0 for incompatible, 1 for compatible,
836 or 2 for compatible with warning. */
837
838static int
d1bd0ded 839type_lists_compatible_p (tree args1, tree args2, int flags)
400fbf9f
JW
840{
841 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
842 int val = 1;
9d5f3e49 843 int newval = 0;
400fbf9f
JW
844
845 while (1)
846 {
847 if (args1 == 0 && args2 == 0)
848 return val;
849 /* If one list is shorter than the other,
850 they fail to match. */
851 if (args1 == 0 || args2 == 0)
852 return 0;
853 /* A null pointer instead of a type
854 means there is supposed to be an argument
855 but nothing is specified about what type it has.
856 So match anything that self-promotes. */
857 if (TREE_VALUE (args1) == 0)
858 {
ab393bf1 859 if (c_type_promotes_to (TREE_VALUE (args2)) != TREE_VALUE (args2))
400fbf9f
JW
860 return 0;
861 }
862 else if (TREE_VALUE (args2) == 0)
863 {
ab393bf1 864 if (c_type_promotes_to (TREE_VALUE (args1)) != TREE_VALUE (args1))
400fbf9f
JW
865 return 0;
866 }
2f6e4e97 867 else if (! (newval = comptypes (TYPE_MAIN_VARIANT (TREE_VALUE (args1)),
d1bd0ded
GK
868 TYPE_MAIN_VARIANT (TREE_VALUE (args2)),
869 flags)))
400fbf9f
JW
870 {
871 /* Allow wait (union {union wait *u; int *i} *)
872 and wait (union wait *) to be compatible. */
873 if (TREE_CODE (TREE_VALUE (args1)) == UNION_TYPE
ea3373cd
RK
874 && (TYPE_NAME (TREE_VALUE (args1)) == 0
875 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args1)))
400fbf9f
JW
876 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args1))) == INTEGER_CST
877 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args1)),
878 TYPE_SIZE (TREE_VALUE (args2))))
879 {
880 tree memb;
881 for (memb = TYPE_FIELDS (TREE_VALUE (args1));
882 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
883 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args2),
884 flags))
400fbf9f
JW
885 break;
886 if (memb == 0)
887 return 0;
888 }
889 else if (TREE_CODE (TREE_VALUE (args2)) == UNION_TYPE
ea3373cd
RK
890 && (TYPE_NAME (TREE_VALUE (args2)) == 0
891 || TYPE_TRANSPARENT_UNION (TREE_VALUE (args2)))
400fbf9f
JW
892 && TREE_CODE (TYPE_SIZE (TREE_VALUE (args2))) == INTEGER_CST
893 && tree_int_cst_equal (TYPE_SIZE (TREE_VALUE (args2)),
894 TYPE_SIZE (TREE_VALUE (args1))))
895 {
896 tree memb;
897 for (memb = TYPE_FIELDS (TREE_VALUE (args2));
898 memb; memb = TREE_CHAIN (memb))
d1bd0ded
GK
899 if (comptypes (TREE_TYPE (memb), TREE_VALUE (args1),
900 flags))
400fbf9f
JW
901 break;
902 if (memb == 0)
903 return 0;
904 }
905 else
906 return 0;
907 }
908
909 /* comptypes said ok, but record if it said to warn. */
910 if (newval > val)
911 val = newval;
912
913 args1 = TREE_CHAIN (args1);
914 args2 = TREE_CHAIN (args2);
915 }
916}
400fbf9f 917\f
400fbf9f
JW
918/* Compute the size to increment a pointer by. */
919
920tree
2f6e4e97 921c_size_in_bytes (tree type)
400fbf9f
JW
922{
923 enum tree_code code = TREE_CODE (type);
924
fed3cef0
RK
925 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
926 return size_one_node;
927
d0f062fb 928 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
929 {
930 error ("arithmetic on pointer to an incomplete type");
fed3cef0 931 return size_one_node;
400fbf9f
JW
932 }
933
934 /* Convert in case a char is more than one unit. */
fed3cef0
RK
935 return size_binop (CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
936 size_int (TYPE_PRECISION (char_type_node)
937 / BITS_PER_UNIT));
400fbf9f 938}
400fbf9f 939\f
400fbf9f
JW
940/* Return either DECL or its known constant value (if it has one). */
941
56cb9733 942tree
2f6e4e97 943decl_constant_value (tree decl)
400fbf9f 944{
a7c1916a 945 if (/* Don't change a variable array bound or initial value to a constant
400fbf9f 946 in a place where a variable is invalid. */
a7c1916a 947 current_function_decl != 0
400fbf9f 948 && ! TREE_THIS_VOLATILE (decl)
83bab8db 949 && TREE_READONLY (decl)
400fbf9f
JW
950 && DECL_INITIAL (decl) != 0
951 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
952 /* This is invalid if initial value is not constant.
953 If it has either a function call, a memory reference,
954 or a variable, then re-evaluating it could give different results. */
955 && TREE_CONSTANT (DECL_INITIAL (decl))
956 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 957 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
958 return DECL_INITIAL (decl);
959 return decl;
960}
961
2f74f7e9
JM
962/* Return either DECL or its known constant value (if it has one), but
963 return DECL if pedantic or DECL has mode BLKmode. This is for
964 bug-compatibility with the old behavior of decl_constant_value
965 (before GCC 3.0); every use of this function is a bug and it should
966 be removed before GCC 3.1. It is not appropriate to use pedantic
967 in a way that affects optimization, and BLKmode is probably not the
968 right test for avoiding misoptimizations either. */
969
970static tree
2f6e4e97 971decl_constant_value_for_broken_optimization (tree decl)
2f74f7e9
JM
972{
973 if (pedantic || DECL_MODE (decl) == BLKmode)
974 return decl;
975 else
976 return decl_constant_value (decl);
977}
978
207bf485
JM
979
980/* Perform the default conversion of arrays and functions to pointers.
981 Return the result of converting EXP. For any other expression, just
982 return EXP. */
983
984static tree
2f6e4e97 985default_function_array_conversion (tree exp)
207bf485
JM
986{
987 tree orig_exp;
988 tree type = TREE_TYPE (exp);
989 enum tree_code code = TREE_CODE (type);
990 int not_lvalue = 0;
991
992 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
2f6e4e97 993 an lvalue.
207bf485
JM
994
995 Do not use STRIP_NOPS here! It will remove conversions from pointer
996 to integer and cause infinite recursion. */
997 orig_exp = exp;
998 while (TREE_CODE (exp) == NON_LVALUE_EXPR
999 || (TREE_CODE (exp) == NOP_EXPR
1000 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1001 {
1002 if (TREE_CODE (exp) == NON_LVALUE_EXPR)
1003 not_lvalue = 1;
1004 exp = TREE_OPERAND (exp, 0);
1005 }
1006
1007 /* Preserve the original expression code. */
1008 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1009 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1010
1011 if (code == FUNCTION_TYPE)
1012 {
1013 return build_unary_op (ADDR_EXPR, exp, 0);
1014 }
1015 if (code == ARRAY_TYPE)
1016 {
1017 tree adr;
1018 tree restype = TREE_TYPE (type);
1019 tree ptrtype;
1020 int constp = 0;
1021 int volatilep = 0;
1022 int lvalue_array_p;
1023
1024 if (TREE_CODE_CLASS (TREE_CODE (exp)) == 'r' || DECL_P (exp))
1025 {
1026 constp = TREE_READONLY (exp);
1027 volatilep = TREE_THIS_VOLATILE (exp);
1028 }
1029
1030 if (TYPE_QUALS (type) || constp || volatilep)
2f6e4e97 1031 restype
207bf485 1032 = c_build_qualified_type (restype,
2f6e4e97 1033 TYPE_QUALS (type)
207bf485
JM
1034 | (constp * TYPE_QUAL_CONST)
1035 | (volatilep * TYPE_QUAL_VOLATILE));
1036
1037 if (TREE_CODE (exp) == INDIRECT_REF)
1038 return convert (TYPE_POINTER_TO (restype),
1039 TREE_OPERAND (exp, 0));
1040
1041 if (TREE_CODE (exp) == COMPOUND_EXPR)
1042 {
1043 tree op1 = default_conversion (TREE_OPERAND (exp, 1));
1044 return build (COMPOUND_EXPR, TREE_TYPE (op1),
1045 TREE_OPERAND (exp, 0), op1);
1046 }
1047
1048 lvalue_array_p = !not_lvalue && lvalue_p (exp);
db3acfa5 1049 if (!flag_isoc99 && !lvalue_array_p)
207bf485
JM
1050 {
1051 /* Before C99, non-lvalue arrays do not decay to pointers.
1052 Normally, using such an array would be invalid; but it can
1053 be used correctly inside sizeof or as a statement expression.
1054 Thus, do not give an error here; an error will result later. */
1055 return exp;
1056 }
1057
1058 ptrtype = build_pointer_type (restype);
1059
1060 if (TREE_CODE (exp) == VAR_DECL)
1061 {
1062 /* ??? This is not really quite correct
1063 in that the type of the operand of ADDR_EXPR
1064 is not the target type of the type of the ADDR_EXPR itself.
1065 Question is, can this lossage be avoided? */
1066 adr = build1 (ADDR_EXPR, ptrtype, exp);
dffd7eb6 1067 if (!c_mark_addressable (exp))
207bf485
JM
1068 return error_mark_node;
1069 TREE_CONSTANT (adr) = staticp (exp);
1070 TREE_SIDE_EFFECTS (adr) = 0; /* Default would be, same as EXP. */
1071 return adr;
1072 }
1073 /* This way is better for a COMPONENT_REF since it can
1074 simplify the offset for a component. */
1075 adr = build_unary_op (ADDR_EXPR, exp, 1);
1076 return convert (ptrtype, adr);
1077 }
1078 return exp;
1079}
1080
400fbf9f
JW
1081/* Perform default promotions for C data used in expressions.
1082 Arrays and functions are converted to pointers;
1083 enumeral types or short or char, to int.
1084 In addition, manifest constants symbols are replaced by their values. */
1085
1086tree
2f6e4e97 1087default_conversion (tree exp)
400fbf9f 1088{
157689c6 1089 tree orig_exp;
b3694847
SS
1090 tree type = TREE_TYPE (exp);
1091 enum tree_code code = TREE_CODE (type);
400fbf9f 1092
207bf485
JM
1093 if (code == FUNCTION_TYPE || code == ARRAY_TYPE)
1094 return default_function_array_conversion (exp);
1095
400fbf9f
JW
1096 /* Constants can be used directly unless they're not loadable. */
1097 if (TREE_CODE (exp) == CONST_DECL)
1098 exp = DECL_INITIAL (exp);
d4424a75
RK
1099
1100 /* Replace a nonvolatile const static variable with its value unless
1101 it is an array, in which case we must be sure that taking the
1102 address of the array produces consistent results. */
1103 else if (optimize && TREE_CODE (exp) == VAR_DECL && code != ARRAY_TYPE)
400fbf9f 1104 {
2f74f7e9 1105 exp = decl_constant_value_for_broken_optimization (exp);
400fbf9f
JW
1106 type = TREE_TYPE (exp);
1107 }
1108
a7d53fce 1109 /* Strip NON_LVALUE_EXPRs and no-op conversions, since we aren't using as
2f6e4e97 1110 an lvalue.
05bccae2
RK
1111
1112 Do not use STRIP_NOPS here! It will remove conversions from pointer
a7d53fce 1113 to integer and cause infinite recursion. */
157689c6 1114 orig_exp = exp;
a7d53fce
RS
1115 while (TREE_CODE (exp) == NON_LVALUE_EXPR
1116 || (TREE_CODE (exp) == NOP_EXPR
1117 && TREE_TYPE (TREE_OPERAND (exp, 0)) == TREE_TYPE (exp)))
1118 exp = TREE_OPERAND (exp, 0);
400fbf9f 1119
157689c6
NB
1120 /* Preserve the original expression code. */
1121 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (exp))))
1122 C_SET_EXP_ORIGINAL_CODE (exp, C_EXP_ORIGINAL_CODE (orig_exp));
1123
400fbf9f
JW
1124 /* Normally convert enums to int,
1125 but convert wide enums to something wider. */
1126 if (code == ENUMERAL_TYPE)
1127 {
b0c48229
NB
1128 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1129 TYPE_PRECISION (integer_type_node)),
1130 ((TYPE_PRECISION (type)
1131 >= TYPE_PRECISION (integer_type_node))
1132 && TREE_UNSIGNED (type)));
05bccae2 1133
400fbf9f
JW
1134 return convert (type, exp);
1135 }
1136
9753f113 1137 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 1138 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 1139 /* If it's thinner than an int, promote it like a
d72040f5 1140 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
1141 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1142 TYPE_PRECISION (integer_type_node)))
f458d1d5 1143 return convert (integer_type_node, exp);
9753f113 1144
d72040f5 1145 if (c_promoting_integer_type_p (type))
400fbf9f 1146 {
f458d1d5 1147 /* Preserve unsignedness if not really getting any wider. */
e83d45c4 1148 if (TREE_UNSIGNED (type)
f458d1d5 1149 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 1150 return convert (unsigned_type_node, exp);
05bccae2 1151
400fbf9f
JW
1152 return convert (integer_type_node, exp);
1153 }
05bccae2 1154
400fbf9f
JW
1155 if (code == VOID_TYPE)
1156 {
1157 error ("void value not ignored as it ought to be");
1158 return error_mark_node;
1159 }
400fbf9f
JW
1160 return exp;
1161}
1162\f
e9b2c823
NB
1163/* Look up COMPONENT in a structure or union DECL.
1164
1165 If the component name is not found, returns NULL_TREE. Otherwise,
1166 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
1167 stepping down the chain to the component, which is in the last
1168 TREE_VALUE of the list. Normally the list is of length one, but if
1169 the component is embedded within (nested) anonymous structures or
1170 unions, the list steps down the chain to the component. */
2f6e4e97 1171
2f2d13da 1172static tree
2f6e4e97 1173lookup_field (tree decl, tree component)
2f2d13da 1174{
e9b2c823 1175 tree type = TREE_TYPE (decl);
2f2d13da
DE
1176 tree field;
1177
1178 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
1179 to the field elements. Use a binary search on this array to quickly
1180 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
1181 will always be set for structures which have many elements. */
1182
1183 if (TYPE_LANG_SPECIFIC (type))
1184 {
1185 int bot, top, half;
d07605f5 1186 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
1187
1188 field = TYPE_FIELDS (type);
1189 bot = 0;
d07605f5 1190 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
1191 while (top - bot > 1)
1192 {
2f2d13da
DE
1193 half = (top - bot + 1) >> 1;
1194 field = field_array[bot+half];
1195
1196 if (DECL_NAME (field) == NULL_TREE)
1197 {
1198 /* Step through all anon unions in linear fashion. */
1199 while (DECL_NAME (field_array[bot]) == NULL_TREE)
1200 {
2f2d13da 1201 field = field_array[bot++];
a68b98cf
RK
1202 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1203 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 1204 {
e9b2c823
NB
1205 tree anon = lookup_field (field, component);
1206
1207 if (anon)
1208 return tree_cons (NULL_TREE, field, anon);
2f6e4e97 1209 }
2f2d13da
DE
1210 }
1211
1212 /* Entire record is only anon unions. */
1213 if (bot > top)
1214 return NULL_TREE;
1215
1216 /* Restart the binary search, with new lower bound. */
1217 continue;
1218 }
1219
e8b87aac 1220 if (DECL_NAME (field) == component)
2f2d13da 1221 break;
e8b87aac 1222 if (DECL_NAME (field) < component)
2f2d13da
DE
1223 bot += half;
1224 else
1225 top = bot + half;
1226 }
1227
1228 if (DECL_NAME (field_array[bot]) == component)
1229 field = field_array[bot];
1230 else if (DECL_NAME (field) != component)
e9b2c823 1231 return NULL_TREE;
2f2d13da
DE
1232 }
1233 else
1234 {
1235 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1236 {
e9b2c823
NB
1237 if (DECL_NAME (field) == NULL_TREE
1238 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
1239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 1240 {
e9b2c823 1241 tree anon = lookup_field (field, component);
a68b98cf 1242
e9b2c823
NB
1243 if (anon)
1244 return tree_cons (NULL_TREE, field, anon);
2f2d13da
DE
1245 }
1246
1247 if (DECL_NAME (field) == component)
1248 break;
1249 }
e9b2c823
NB
1250
1251 if (field == NULL_TREE)
1252 return NULL_TREE;
2f2d13da
DE
1253 }
1254
e9b2c823 1255 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
1256}
1257
400fbf9f
JW
1258/* Make an expression to refer to the COMPONENT field of
1259 structure or union value DATUM. COMPONENT is an IDENTIFIER_NODE. */
1260
1261tree
2f6e4e97 1262build_component_ref (tree datum, tree component)
400fbf9f 1263{
b3694847
SS
1264 tree type = TREE_TYPE (datum);
1265 enum tree_code code = TREE_CODE (type);
1266 tree field = NULL;
1267 tree ref;
400fbf9f 1268
207bf485
JM
1269 /* If DATUM is a COMPOUND_EXPR, move our reference inside it.
1270 If pedantic ensure that the arguments are not lvalues; otherwise,
1271 if the component is an array, it would wrongly decay to a pointer in
1272 C89 mode.
1273 We cannot do this with a COND_EXPR, because in a conditional expression
1274 the default promotions are applied to both sides, and this would yield
1275 the wrong type of the result; for example, if the components have
1276 type "char". */
400fbf9f
JW
1277 switch (TREE_CODE (datum))
1278 {
1279 case COMPOUND_EXPR:
1280 {
1281 tree value = build_component_ref (TREE_OPERAND (datum, 1), component);
400fbf9f 1282 return build (COMPOUND_EXPR, TREE_TYPE (value),
207bf485 1283 TREE_OPERAND (datum, 0), pedantic_non_lvalue (value));
400fbf9f 1284 }
e9a25f70
JL
1285 default:
1286 break;
400fbf9f
JW
1287 }
1288
1289 /* See if there is a field or component with name COMPONENT. */
1290
1291 if (code == RECORD_TYPE || code == UNION_TYPE)
1292 {
d0f062fb 1293 if (!COMPLETE_TYPE_P (type))
400fbf9f 1294 {
7a228918 1295 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
1296 return error_mark_node;
1297 }
1298
e9b2c823 1299 field = lookup_field (datum, component);
400fbf9f
JW
1300
1301 if (!field)
1302 {
913d0833
KG
1303 error ("%s has no member named `%s'",
1304 code == RECORD_TYPE ? "structure" : "union",
400fbf9f
JW
1305 IDENTIFIER_POINTER (component));
1306 return error_mark_node;
1307 }
400fbf9f 1308
e9b2c823
NB
1309 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
1310 This might be better solved in future the way the C++ front
1311 end does it - by giving the anonymous entities each a
1312 separate name and type, and then have build_component_ref
1313 recursively call itself. We can't do that here. */
46ea50cb 1314 do
19d76e60 1315 {
e9b2c823
NB
1316 tree subdatum = TREE_VALUE (field);
1317
1318 if (TREE_TYPE (subdatum) == error_mark_node)
1319 return error_mark_node;
1320
1321 ref = build (COMPONENT_REF, TREE_TYPE (subdatum), datum, subdatum);
1322 if (TREE_READONLY (datum) || TREE_READONLY (subdatum))
19d76e60 1323 TREE_READONLY (ref) = 1;
e9b2c823 1324 if (TREE_THIS_VOLATILE (datum) || TREE_THIS_VOLATILE (subdatum))
19d76e60 1325 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
1326
1327 if (TREE_DEPRECATED (subdatum))
1328 warn_deprecated_use (subdatum);
1329
19d76e60 1330 datum = ref;
46ea50cb
RS
1331
1332 field = TREE_CHAIN (field);
19d76e60 1333 }
46ea50cb 1334 while (field);
19d76e60 1335
400fbf9f
JW
1336 return ref;
1337 }
1338 else if (code != ERROR_MARK)
1339 error ("request for member `%s' in something not a structure or union",
1340 IDENTIFIER_POINTER (component));
1341
1342 return error_mark_node;
1343}
1344\f
1345/* Given an expression PTR for a pointer, return an expression
1346 for the value pointed to.
1347 ERRORSTRING is the name of the operator to appear in error messages. */
1348
1349tree
2f6e4e97 1350build_indirect_ref (tree ptr, const char *errorstring)
400fbf9f 1351{
b3694847
SS
1352 tree pointer = default_conversion (ptr);
1353 tree type = TREE_TYPE (pointer);
400fbf9f
JW
1354
1355 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b
RS
1356 {
1357 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
1358 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
1359 == TREE_TYPE (type)))
1360 return TREE_OPERAND (pointer, 0);
1361 else
1362 {
1363 tree t = TREE_TYPE (type);
b3694847 1364 tree ref = build1 (INDIRECT_REF, TYPE_MAIN_VARIANT (t), pointer);
400fbf9f 1365
baae9b65 1366 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b
RS
1367 {
1368 error ("dereferencing pointer to incomplete type");
1369 return error_mark_node;
1370 }
baae9b65 1371 if (VOID_TYPE_P (t) && skip_evaluation == 0)
870cc33b
RS
1372 warning ("dereferencing `void *' pointer");
1373
1374 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
1375 so that we get the proper error message if the result is used
1376 to assign to. Also, &* is supposed to be a no-op.
1377 And ANSI C seems to specify that the type of the result
1378 should be the const type. */
1379 /* A de-reference of a pointer to const is not a const. It is valid
1380 to change it via some other pointer. */
1381 TREE_READONLY (ref) = TYPE_READONLY (t);
1382 TREE_SIDE_EFFECTS (ref)
271bd540 1383 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 1384 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
870cc33b
RS
1385 return ref;
1386 }
1387 }
400fbf9f
JW
1388 else if (TREE_CODE (pointer) != ERROR_MARK)
1389 error ("invalid type argument of `%s'", errorstring);
1390 return error_mark_node;
1391}
1392
1393/* This handles expressions of the form "a[i]", which denotes
1394 an array reference.
1395
1396 This is logically equivalent in C to *(a+i), but we may do it differently.
1397 If A is a variable or a member, we generate a primitive ARRAY_REF.
1398 This avoids forcing the array out of registers, and can work on
1399 arrays that are not lvalues (for example, members of structures returned
1400 by functions). */
1401
1402tree
2f6e4e97 1403build_array_ref (tree array, tree index)
400fbf9f
JW
1404{
1405 if (index == 0)
1406 {
1407 error ("subscript missing in array reference");
1408 return error_mark_node;
1409 }
1410
1411 if (TREE_TYPE (array) == error_mark_node
1412 || TREE_TYPE (index) == error_mark_node)
1413 return error_mark_node;
1414
1415 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE
1416 && TREE_CODE (array) != INDIRECT_REF)
1417 {
1418 tree rval, type;
1419
400fbf9f
JW
1420 /* Subscripting with type char is likely to lose
1421 on a machine where chars are signed.
1422 So warn on any machine, but optionally.
1423 Don't warn for unsigned char since that type is safe.
1424 Don't warn for signed char because anyone who uses that
1425 must have done so deliberately. */
1426 if (warn_char_subscripts
1427 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1428 warning ("array subscript has type `char'");
1429
0e51ef9b
RS
1430 /* Apply default promotions *after* noticing character types. */
1431 index = default_conversion (index);
1432
fdeefd49
RS
1433 /* Require integer *after* promotion, for sake of enums. */
1434 if (TREE_CODE (TREE_TYPE (index)) != INTEGER_TYPE)
1435 {
1436 error ("array subscript is not an integer");
1437 return error_mark_node;
1438 }
1439
400fbf9f
JW
1440 /* An array that is indexed by a non-constant
1441 cannot be stored in a register; we must be able to do
1442 address arithmetic on its address.
1443 Likewise an array of elements of variable size. */
1444 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 1445 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
1446 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
1447 {
dffd7eb6 1448 if (!c_mark_addressable (array))
400fbf9f
JW
1449 return error_mark_node;
1450 }
e6d52559
JW
1451 /* An array that is indexed by a constant value which is not within
1452 the array bounds cannot be stored in a register either; because we
1453 would get a crash in store_bit_field/extract_bit_field when trying
1454 to access a non-existent part of the register. */
1455 if (TREE_CODE (index) == INTEGER_CST
1456 && TYPE_VALUES (TREE_TYPE (array))
1457 && ! int_fits_type_p (index, TYPE_VALUES (TREE_TYPE (array))))
1458 {
dffd7eb6 1459 if (!c_mark_addressable (array))
e6d52559
JW
1460 return error_mark_node;
1461 }
400fbf9f 1462
400fbf9f
JW
1463 if (pedantic)
1464 {
1465 tree foo = array;
1466 while (TREE_CODE (foo) == COMPONENT_REF)
1467 foo = TREE_OPERAND (foo, 0);
1394aabd 1468 if (TREE_CODE (foo) == VAR_DECL && DECL_REGISTER (foo))
05273f08
GK
1469 pedwarn ("ISO C forbids subscripting `register' array");
1470 else if (! flag_isoc99 && ! lvalue_p (foo))
56508306 1471 pedwarn ("ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
1472 }
1473
1474 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (array)));
1475 rval = build (ARRAY_REF, type, array, index);
1476 /* Array ref is const/volatile if the array elements are
1477 or if the array is. */
1478 TREE_READONLY (rval)
1479 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
1480 | TREE_READONLY (array));
1481 TREE_SIDE_EFFECTS (rval)
1482 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1483 | TREE_SIDE_EFFECTS (array));
1484 TREE_THIS_VOLATILE (rval)
1485 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
1486 /* This was added by rms on 16 Nov 91.
2f6e4e97 1487 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
1488 in an inline function.
1489 Hope it doesn't break something else. */
1490 | TREE_THIS_VOLATILE (array));
1491 return require_complete_type (fold (rval));
1492 }
1493
1494 {
1495 tree ar = default_conversion (array);
1496 tree ind = default_conversion (index);
1497
aed11452
RK
1498 /* Do the same warning check as above, but only on the part that's
1499 syntactically the index and only if it is also semantically
1500 the index. */
1501 if (warn_char_subscripts
1502 && TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE
1503 && TYPE_MAIN_VARIANT (TREE_TYPE (index)) == char_type_node)
1504 warning ("subscript has type `char'");
1505
400fbf9f
JW
1506 /* Put the integer in IND to simplify error checking. */
1507 if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
1508 {
1509 tree temp = ar;
1510 ar = ind;
1511 ind = temp;
1512 }
1513
1514 if (ar == error_mark_node)
1515 return ar;
1516
004252d7
RK
1517 if (TREE_CODE (TREE_TYPE (ar)) != POINTER_TYPE
1518 || TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) == FUNCTION_TYPE)
400fbf9f
JW
1519 {
1520 error ("subscripted value is neither array nor pointer");
1521 return error_mark_node;
1522 }
1523 if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
1524 {
1525 error ("array subscript is not an integer");
1526 return error_mark_node;
1527 }
1528
1529 return build_indirect_ref (build_binary_op (PLUS_EXPR, ar, ind, 0),
1530 "array indexing");
1531 }
1532}
1533\f
339a28b9
ZW
1534/* Issue an error message for a reference to an undeclared variable ID,
1535 including a reference to a builtin outside of function-call context.
1536 Arrange to suppress further errors for the same identifier. */
1537static void
2f6e4e97 1538undeclared_variable (tree id)
339a28b9
ZW
1539{
1540 if (current_function_decl == 0)
1541 {
1542 error ("`%s' undeclared here (not in a function)",
1543 IDENTIFIER_POINTER (id));
1544 IDENTIFIER_SYMBOL_VALUE (id) = error_mark_node;
1545 }
1546 else
1547 {
1548 error ("`%s' undeclared (first use in this function)",
1549 IDENTIFIER_POINTER (id));
1550
1551 if (! undeclared_variable_notice)
1552 {
1553 error ("(Each undeclared identifier is reported only once");
1554 error ("for each function it appears in.)");
1555 undeclared_variable_notice = 1;
1556 }
1557
1558 /* Set IDENTIFIER_SYMBOL_VALUE (id) to error_mark_node
1559 at function scope. This suppresses further warnings
1560 about this undeclared identifier in this function. */
1561 pushdecl_function_level (error_mark_node, id);
1562 }
1563}
1564
7e585d16
ZW
1565/* Build an external reference to identifier ID. FUN indicates
1566 whether this will be used for a function call. */
1567tree
2f6e4e97 1568build_external_ref (tree id, int fun)
7e585d16
ZW
1569{
1570 tree ref;
1571 tree decl = lookup_name (id);
1572 tree objc_ivar = lookup_objc_ivar (id);
1573
339a28b9 1574 if (decl && decl != error_mark_node)
7e585d16
ZW
1575 {
1576 /* Properly declared variable or function reference. */
1577 if (!objc_ivar)
1578 ref = decl;
d1bd0ded 1579 else if (decl != objc_ivar && !C_DECL_FILE_SCOPE (decl))
7e585d16
ZW
1580 {
1581 warning ("local declaration of `%s' hides instance variable",
1582 IDENTIFIER_POINTER (id));
1583 ref = decl;
1584 }
1585 else
1586 ref = objc_ivar;
1587 }
339a28b9
ZW
1588 else if (objc_ivar)
1589 ref = objc_ivar;
1590 else if (fun)
1591 /* Implicit function declaration. */
1592 ref = implicitly_declare (id);
1593 else if (decl == error_mark_node)
1594 /* Don't complain about something that's already been
1595 complained about. */
1596 return error_mark_node;
1597 else
1598 {
1599 undeclared_variable (id);
1600 return error_mark_node;
1601 }
7e585d16
ZW
1602
1603 if (TREE_TYPE (ref) == error_mark_node)
1604 return error_mark_node;
1605
339a28b9
ZW
1606 if (TREE_DEPRECATED (ref))
1607 warn_deprecated_use (ref);
1608
25587e40
AO
1609 if (!skip_evaluation)
1610 assemble_external (ref);
7e585d16
ZW
1611 TREE_USED (ref) = 1;
1612
1613 if (TREE_CODE (ref) == CONST_DECL)
1614 {
1615 ref = DECL_INITIAL (ref);
1616 TREE_CONSTANT (ref) = 1;
1617 }
6a29edea 1618 else if (current_function_decl != 0
d1bd0ded 1619 && !C_DECL_FILE_SCOPE (current_function_decl)
6a29edea
EB
1620 && (TREE_CODE (ref) == VAR_DECL
1621 || TREE_CODE (ref) == PARM_DECL
1622 || TREE_CODE (ref) == FUNCTION_DECL))
1623 {
1624 tree context = decl_function_context (ref);
2f6e4e97 1625
6a29edea
EB
1626 if (context != 0 && context != current_function_decl)
1627 DECL_NONLOCAL (ref) = 1;
1628 }
7e585d16
ZW
1629
1630 return ref;
1631}
1632
400fbf9f
JW
1633/* Build a function call to function FUNCTION with parameters PARAMS.
1634 PARAMS is a list--a chain of TREE_LIST nodes--in which the
1635 TREE_VALUE of each node is a parameter-expression.
1636 FUNCTION's data type may be a function type or a pointer-to-function. */
1637
1638tree
2f6e4e97 1639build_function_call (tree function, tree params)
400fbf9f 1640{
b3694847
SS
1641 tree fntype, fundecl = 0;
1642 tree coerced_params;
4977bab6 1643 tree name = NULL_TREE, result;
400fbf9f 1644
fc76e425 1645 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 1646 STRIP_TYPE_NOPS (function);
400fbf9f
JW
1647
1648 /* Convert anything with function type to a pointer-to-function. */
1649 if (TREE_CODE (function) == FUNCTION_DECL)
1650 {
1651 name = DECL_NAME (function);
19d76e60 1652
400fbf9f
JW
1653 /* Differs from default_conversion by not setting TREE_ADDRESSABLE
1654 (because calling an inline function does not mean the function
1655 needs to be separately compiled). */
1656 fntype = build_type_variant (TREE_TYPE (function),
1657 TREE_READONLY (function),
1658 TREE_THIS_VOLATILE (function));
9b7267b8 1659 fundecl = function;
400fbf9f
JW
1660 function = build1 (ADDR_EXPR, build_pointer_type (fntype), function);
1661 }
1662 else
1663 function = default_conversion (function);
1664
1665 fntype = TREE_TYPE (function);
1666
1667 if (TREE_CODE (fntype) == ERROR_MARK)
1668 return error_mark_node;
1669
1670 if (!(TREE_CODE (fntype) == POINTER_TYPE
1671 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
1672 {
1673 error ("called object is not a function");
1674 return error_mark_node;
1675 }
1676
5ce89b2e
JM
1677 if (fundecl && TREE_THIS_VOLATILE (fundecl))
1678 current_function_returns_abnormally = 1;
1679
400fbf9f
JW
1680 /* fntype now gets the type of function pointed to. */
1681 fntype = TREE_TYPE (fntype);
1682
1683 /* Convert the parameters to the types declared in the
1684 function prototype, or apply default promotions. */
1685
1686 coerced_params
9b7267b8 1687 = convert_arguments (TYPE_ARG_TYPES (fntype), params, name, fundecl);
400fbf9f 1688
b34c7881 1689 /* Check that the arguments to the function are valid. */
400fbf9f 1690
b34c7881 1691 check_function_arguments (TYPE_ATTRIBUTES (fntype), coerced_params);
400fbf9f
JW
1692
1693 /* Recognize certain built-in functions so we can make tree-codes
1694 other than CALL_EXPR. We do this when it enables fold-const.c
1695 to do something useful. */
1696
1697 if (TREE_CODE (function) == ADDR_EXPR
1698 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL
1eb8759b
RH
1699 && DECL_BUILT_IN (TREE_OPERAND (function, 0)))
1700 {
1701 result = expand_tree_builtin (TREE_OPERAND (function, 0),
1702 params, coerced_params);
1703 if (result)
1704 return result;
1705 }
400fbf9f 1706
1eb8759b
RH
1707 result = build (CALL_EXPR, TREE_TYPE (fntype),
1708 function, coerced_params, NULL_TREE);
1eb8759b 1709 TREE_SIDE_EFFECTS (result) = 1;
b0b3afb2
BS
1710 result = fold (result);
1711
71653180 1712 if (VOID_TYPE_P (TREE_TYPE (result)))
1eb8759b
RH
1713 return result;
1714 return require_complete_type (result);
400fbf9f
JW
1715}
1716\f
1717/* Convert the argument expressions in the list VALUES
1718 to the types in the list TYPELIST. The result is a list of converted
1719 argument expressions.
1720
1721 If TYPELIST is exhausted, or when an element has NULL as its type,
1722 perform the default conversions.
1723
1724 PARMLIST is the chain of parm decls for the function being called.
1725 It may be 0, if that info is not available.
1726 It is used only for generating error messages.
1727
1728 NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
1729
1730 This is also where warnings about wrong number of args are generated.
1731
1732 Both VALUES and the returned value are chains of TREE_LIST nodes
1733 with the elements of the list in the TREE_VALUE slots of those nodes. */
1734
1735static tree
2f6e4e97 1736convert_arguments (tree typelist, tree values, tree name, tree fundecl)
400fbf9f 1737{
b3694847
SS
1738 tree typetail, valtail;
1739 tree result = NULL;
400fbf9f
JW
1740 int parmnum;
1741
1742 /* Scan the given expressions and types, producing individual
1743 converted arguments and pushing them on RESULT in reverse order. */
1744
1745 for (valtail = values, typetail = typelist, parmnum = 0;
1746 valtail;
1747 valtail = TREE_CHAIN (valtail), parmnum++)
1748 {
b3694847
SS
1749 tree type = typetail ? TREE_VALUE (typetail) : 0;
1750 tree val = TREE_VALUE (valtail);
400fbf9f
JW
1751
1752 if (type == void_type_node)
1753 {
1754 if (name)
1755 error ("too many arguments to function `%s'",
1756 IDENTIFIER_POINTER (name));
1757 else
1758 error ("too many arguments to function");
1759 break;
1760 }
1761
1762 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
1763 /* Do not use STRIP_NOPS here! We do not want an enumerator with value 0
1764 to convert automatically to a pointer. */
400fbf9f
JW
1765 if (TREE_CODE (val) == NON_LVALUE_EXPR)
1766 val = TREE_OPERAND (val, 0);
1767
207bf485 1768 val = default_function_array_conversion (val);
400fbf9f
JW
1769
1770 val = require_complete_type (val);
1771
1772 if (type != 0)
1773 {
1774 /* Formal parm type is specified by a function prototype. */
1775 tree parmval;
1776
d0f062fb 1777 if (!COMPLETE_TYPE_P (type))
400fbf9f
JW
1778 {
1779 error ("type of formal parameter %d is incomplete", parmnum + 1);
1780 parmval = val;
1781 }
1782 else
1783 {
d45cf215
RS
1784 /* Optionally warn about conversions that
1785 differ from the default conversions. */
03829ad2 1786 if (warn_conversion || warn_traditional)
400fbf9f
JW
1787 {
1788 int formal_prec = TYPE_PRECISION (type);
400fbf9f 1789
aae43c5f 1790 if (INTEGRAL_TYPE_P (type)
400fbf9f 1791 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
754a4d82 1792 warn_for_assignment ("%s as integer rather than floating due to prototype", (char *) 0, name, parmnum + 1);
03829ad2
KG
1793 if (INTEGRAL_TYPE_P (type)
1794 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1795 warn_for_assignment ("%s as integer rather than complex due to prototype", (char *) 0, name, parmnum + 1);
aae43c5f
RK
1796 else if (TREE_CODE (type) == COMPLEX_TYPE
1797 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1798 warn_for_assignment ("%s as complex rather than floating due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f 1799 else if (TREE_CODE (type) == REAL_TYPE
aae43c5f 1800 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
754a4d82 1801 warn_for_assignment ("%s as floating rather than integer due to prototype", (char *) 0, name, parmnum + 1);
03829ad2
KG
1802 else if (TREE_CODE (type) == COMPLEX_TYPE
1803 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
1804 warn_for_assignment ("%s as complex rather than integer due to prototype", (char *) 0, name, parmnum + 1);
aae43c5f
RK
1805 else if (TREE_CODE (type) == REAL_TYPE
1806 && TREE_CODE (TREE_TYPE (val)) == COMPLEX_TYPE)
1807 warn_for_assignment ("%s as floating rather than complex due to prototype", (char *) 0, name, parmnum + 1);
1808 /* ??? At some point, messages should be written about
1809 conversions between complex types, but that's too messy
1810 to do now. */
d45cf215
RS
1811 else if (TREE_CODE (type) == REAL_TYPE
1812 && TREE_CODE (TREE_TYPE (val)) == REAL_TYPE)
1813 {
1814 /* Warn if any argument is passed as `float',
047de90b 1815 since without a prototype it would be `double'. */
d45cf215 1816 if (formal_prec == TYPE_PRECISION (float_type_node))
754a4d82 1817 warn_for_assignment ("%s as `float' rather than `double' due to prototype", (char *) 0, name, parmnum + 1);
d45cf215 1818 }
3ed56f8a
KG
1819 /* Detect integer changing in width or signedness.
1820 These warnings are only activated with
1821 -Wconversion, not with -Wtraditional. */
1822 else if (warn_conversion && INTEGRAL_TYPE_P (type)
aae43c5f 1823 && INTEGRAL_TYPE_P (TREE_TYPE (val)))
400fbf9f 1824 {
d45cf215
RS
1825 tree would_have_been = default_conversion (val);
1826 tree type1 = TREE_TYPE (would_have_been);
1827
754a4d82 1828 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a
NB
1829 && (TYPE_MAIN_VARIANT (type)
1830 == TYPE_MAIN_VARIANT (TREE_TYPE (val))))
754a4d82
RS
1831 /* No warning if function asks for enum
1832 and the actual arg is that enum type. */
1833 ;
1834 else if (formal_prec != TYPE_PRECISION (type1))
1835 warn_for_assignment ("%s with different width due to prototype", (char *) 0, name, parmnum + 1);
d45cf215
RS
1836 else if (TREE_UNSIGNED (type) == TREE_UNSIGNED (type1))
1837 ;
800cd3b9
RS
1838 /* Don't complain if the formal parameter type
1839 is an enum, because we can't tell now whether
1840 the value was an enum--even the same enum. */
1841 else if (TREE_CODE (type) == ENUMERAL_TYPE)
1842 ;
400fbf9f
JW
1843 else if (TREE_CODE (val) == INTEGER_CST
1844 && int_fits_type_p (val, type))
1845 /* Change in signedness doesn't matter
1846 if a constant value is unaffected. */
1847 ;
4bbbc5d9
RS
1848 /* Likewise for a constant in a NOP_EXPR. */
1849 else if (TREE_CODE (val) == NOP_EXPR
1850 && TREE_CODE (TREE_OPERAND (val, 0)) == INTEGER_CST
1851 && int_fits_type_p (TREE_OPERAND (val, 0), type))
1852 ;
ce9895ae
RS
1853 /* If the value is extended from a narrower
1854 unsigned type, it doesn't matter whether we
1855 pass it as signed or unsigned; the value
1856 certainly is the same either way. */
1857 else if (TYPE_PRECISION (TREE_TYPE (val)) < TYPE_PRECISION (type)
1858 && TREE_UNSIGNED (TREE_TYPE (val)))
1859 ;
3ed56f8a
KG
1860 else if (TREE_UNSIGNED (type))
1861 warn_for_assignment ("%s as unsigned due to prototype", (char *) 0, name, parmnum + 1);
1862 else
1863 warn_for_assignment ("%s as signed due to prototype", (char *) 0, name, parmnum + 1);
400fbf9f
JW
1864 }
1865 }
1866
2f6e4e97 1867 parmval = convert_for_assignment (type, val,
0f41302f 1868 (char *) 0, /* arg passing */
9b7267b8 1869 fundecl, name, parmnum + 1);
2f6e4e97 1870
7d473569 1871 if (PROMOTE_PROTOTYPES
b6d6aa84 1872 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
1873 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
1874 parmval = default_conversion (parmval);
400fbf9f 1875 }
8d9bfdc5 1876 result = tree_cons (NULL_TREE, parmval, result);
400fbf9f
JW
1877 }
1878 else if (TREE_CODE (TREE_TYPE (val)) == REAL_TYPE
1879 && (TYPE_PRECISION (TREE_TYPE (val))
1880 < TYPE_PRECISION (double_type_node)))
1881 /* Convert `float' to `double'. */
1882 result = tree_cons (NULL_TREE, convert (double_type_node, val), result);
1883 else
1884 /* Convert `short' and `char' to full-size `int'. */
1885 result = tree_cons (NULL_TREE, default_conversion (val), result);
1886
1887 if (typetail)
1888 typetail = TREE_CHAIN (typetail);
1889 }
1890
1891 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
1892 {
1893 if (name)
1894 error ("too few arguments to function `%s'",
1895 IDENTIFIER_POINTER (name));
1896 else
1897 error ("too few arguments to function");
1898 }
1899
1900 return nreverse (result);
1901}
1902\f
1903/* This is the entry point used by the parser
1904 for binary operators in the input.
1905 In addition to constructing the expression,
1906 we check for operands that were written with other binary operators
1907 in a way that is likely to confuse the user. */
edc7c4ec 1908
400fbf9f 1909tree
2f6e4e97 1910parser_build_binary_op (enum tree_code code, tree arg1, tree arg2)
400fbf9f
JW
1911{
1912 tree result = build_binary_op (code, arg1, arg2, 1);
1913
1914 char class;
1915 char class1 = TREE_CODE_CLASS (TREE_CODE (arg1));
1916 char class2 = TREE_CODE_CLASS (TREE_CODE (arg2));
1917 enum tree_code code1 = ERROR_MARK;
1918 enum tree_code code2 = ERROR_MARK;
1919
58bf601b
HPN
1920 if (TREE_CODE (result) == ERROR_MARK)
1921 return error_mark_node;
1922
686deecb 1923 if (IS_EXPR_CODE_CLASS (class1))
400fbf9f 1924 code1 = C_EXP_ORIGINAL_CODE (arg1);
686deecb 1925 if (IS_EXPR_CODE_CLASS (class2))
400fbf9f
JW
1926 code2 = C_EXP_ORIGINAL_CODE (arg2);
1927
1928 /* Check for cases such as x+y<<z which users are likely
1929 to misinterpret. If parens are used, C_EXP_ORIGINAL_CODE
1930 is cleared to prevent these warnings. */
1931 if (warn_parentheses)
1932 {
1933 if (code == LSHIFT_EXPR || code == RSHIFT_EXPR)
1934 {
1935 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1936 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1937 warning ("suggest parentheses around + or - inside shift");
1938 }
1939
1940 if (code == TRUTH_ORIF_EXPR)
1941 {
1942 if (code1 == TRUTH_ANDIF_EXPR
1943 || code2 == TRUTH_ANDIF_EXPR)
1944 warning ("suggest parentheses around && within ||");
1945 }
1946
1947 if (code == BIT_IOR_EXPR)
1948 {
1949 if (code1 == BIT_AND_EXPR || code1 == BIT_XOR_EXPR
1950 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1951 || code2 == BIT_AND_EXPR || code2 == BIT_XOR_EXPR
1952 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1953 warning ("suggest parentheses around arithmetic in operand of |");
7e9d002a
RK
1954 /* Check cases like x|y==z */
1955 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1956 warning ("suggest parentheses around comparison in operand of |");
400fbf9f
JW
1957 }
1958
1959 if (code == BIT_XOR_EXPR)
1960 {
1961 if (code1 == BIT_AND_EXPR
1962 || code1 == PLUS_EXPR || code1 == MINUS_EXPR
1963 || code2 == BIT_AND_EXPR
1964 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1965 warning ("suggest parentheses around arithmetic in operand of ^");
7e9d002a
RK
1966 /* Check cases like x^y==z */
1967 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1968 warning ("suggest parentheses around comparison in operand of ^");
400fbf9f
JW
1969 }
1970
1971 if (code == BIT_AND_EXPR)
1972 {
1973 if (code1 == PLUS_EXPR || code1 == MINUS_EXPR
1974 || code2 == PLUS_EXPR || code2 == MINUS_EXPR)
1975 warning ("suggest parentheses around + or - in operand of &");
7e9d002a
RK
1976 /* Check cases like x&y==z */
1977 if (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<')
1978 warning ("suggest parentheses around comparison in operand of &");
400fbf9f
JW
1979 }
1980 }
1981
001af587 1982 /* Similarly, check for cases like 1<=i<=10 that are probably errors. */
edc7c4ec 1983 if (TREE_CODE_CLASS (code) == '<' && extra_warnings
001af587
RS
1984 && (TREE_CODE_CLASS (code1) == '<' || TREE_CODE_CLASS (code2) == '<'))
1985 warning ("comparisons like X<=Y<=Z do not have their mathematical meaning");
1986
e58cd767
RS
1987 unsigned_conversion_warning (result, arg1);
1988 unsigned_conversion_warning (result, arg2);
1989 overflow_warning (result);
1990
edc7c4ec
RS
1991 class = TREE_CODE_CLASS (TREE_CODE (result));
1992
400fbf9f
JW
1993 /* Record the code that was specified in the source,
1994 for the sake of warnings about confusing nesting. */
686deecb 1995 if (IS_EXPR_CODE_CLASS (class))
400fbf9f
JW
1996 C_SET_EXP_ORIGINAL_CODE (result, code);
1997 else
1998 {
1999 int flag = TREE_CONSTANT (result);
d11fdb45
RS
2000 /* We used to use NOP_EXPR rather than NON_LVALUE_EXPR
2001 so that convert_for_assignment wouldn't strip it.
2002 That way, we got warnings for things like p = (1 - 1).
2003 But it turns out we should not get those warnings. */
2004 result = build1 (NON_LVALUE_EXPR, TREE_TYPE (result), result);
400fbf9f
JW
2005 C_SET_EXP_ORIGINAL_CODE (result, code);
2006 TREE_CONSTANT (result) = flag;
2007 }
2008
2009 return result;
2010}
2011
2012/* Build a binary-operation expression without default conversions.
2013 CODE is the kind of expression to build.
2014 This function differs from `build' in several ways:
2015 the data type of the result is computed and recorded in it,
2016 warnings are generated if arg data types are invalid,
2017 special handling for addition and subtraction of pointers is known,
2018 and some optimization is done (operations on narrow ints
2019 are done in the narrower type when that gives the same result).
2020 Constant folding is also done before the result is returned.
2021
2022 Note that the operands will never have enumeral types, or function
2023 or array types, because either they will have the default conversions
2024 performed or they have both just been converted to some other type in which
2025 the arithmetic is to be done. */
2026
2027tree
f55ade6e
AJ
2028build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
2029 int convert_p)
400fbf9f
JW
2030{
2031 tree type0, type1;
b3694847 2032 enum tree_code code0, code1;
400fbf9f
JW
2033 tree op0, op1;
2034
2035 /* Expression code to give to the expression when it is built.
2036 Normally this is CODE, which is what the caller asked for,
2037 but in some special cases we change it. */
b3694847 2038 enum tree_code resultcode = code;
400fbf9f
JW
2039
2040 /* Data type in which the computation is to be performed.
2041 In the simplest cases this is the common type of the arguments. */
b3694847 2042 tree result_type = NULL;
400fbf9f
JW
2043
2044 /* Nonzero means operands have already been type-converted
2045 in whatever way is necessary.
2046 Zero means they need to be converted to RESULT_TYPE. */
2047 int converted = 0;
2048
293c9fdd
JM
2049 /* Nonzero means create the expression with this type, rather than
2050 RESULT_TYPE. */
2051 tree build_type = 0;
2052
400fbf9f 2053 /* Nonzero means after finally constructing the expression
293c9fdd 2054 convert it to this type. */
400fbf9f
JW
2055 tree final_type = 0;
2056
2057 /* Nonzero if this is an operation like MIN or MAX which can
2058 safely be computed in short if both args are promoted shorts.
2059 Also implies COMMON.
2060 -1 indicates a bitwise operation; this makes a difference
2061 in the exact conditions for when it is safe to do the operation
2062 in a narrower mode. */
2063 int shorten = 0;
2064
2065 /* Nonzero if this is a comparison operation;
2066 if both args are promoted shorts, compare the original shorts.
2067 Also implies COMMON. */
2068 int short_compare = 0;
2069
2070 /* Nonzero if this is a right-shift operation, which can be computed on the
2071 original short and then promoted if the operand is a promoted short. */
2072 int short_shift = 0;
2073
2074 /* Nonzero means set RESULT_TYPE to the common type of the args. */
2075 int common = 0;
2076
2077 if (convert_p)
2078 {
2079 op0 = default_conversion (orig_op0);
2080 op1 = default_conversion (orig_op1);
2081 }
2082 else
2083 {
2084 op0 = orig_op0;
2085 op1 = orig_op1;
2086 }
2087
2088 type0 = TREE_TYPE (op0);
2089 type1 = TREE_TYPE (op1);
2090
2091 /* The expression codes of the data types of the arguments tell us
2092 whether the arguments are integers, floating, pointers, etc. */
2093 code0 = TREE_CODE (type0);
2094 code1 = TREE_CODE (type1);
2095
fc76e425 2096 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce
RS
2097 STRIP_TYPE_NOPS (op0);
2098 STRIP_TYPE_NOPS (op1);
400fbf9f
JW
2099
2100 /* If an error was already reported for one of the arguments,
2101 avoid reporting another error. */
2102
2103 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
2104 return error_mark_node;
2105
2106 switch (code)
2107 {
2108 case PLUS_EXPR:
2109 /* Handle the pointer + int case. */
2110 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2111 return pointer_int_sum (PLUS_EXPR, op0, op1);
2112 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
2113 return pointer_int_sum (PLUS_EXPR, op1, op0);
2114 else
2115 common = 1;
2116 break;
2117
2118 case MINUS_EXPR:
2119 /* Subtraction of two similar pointers.
2120 We must subtract them as integers, then divide by object size. */
2121 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
1074d9d4 2122 && comp_target_types (type0, type1, 1))
400fbf9f
JW
2123 return pointer_diff (op0, op1);
2124 /* Handle pointer minus int. Just like pointer plus int. */
2125 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2126 return pointer_int_sum (MINUS_EXPR, op0, op1);
2127 else
2128 common = 1;
2129 break;
2130
2131 case MULT_EXPR:
2132 common = 1;
2133 break;
2134
2135 case TRUNC_DIV_EXPR:
2136 case CEIL_DIV_EXPR:
2137 case FLOOR_DIV_EXPR:
2138 case ROUND_DIV_EXPR:
2139 case EXACT_DIV_EXPR:
6c36d76b
NB
2140 /* Floating point division by zero is a legitimate way to obtain
2141 infinities and NaNs. */
2142 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2143 warning ("division by zero");
2144
b6a10c9f 2145 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
cb2a532e 2146 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
b6a10c9f 2147 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
cb2a532e 2148 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f
JW
2149 {
2150 if (!(code0 == INTEGER_TYPE && code1 == INTEGER_TYPE))
2151 resultcode = RDIV_EXPR;
2152 else
05bccae2
RK
2153 /* Although it would be tempting to shorten always here, that
2154 loses on some targets, since the modulo instruction is
2155 undefined if the quotient can't be represented in the
2156 computation mode. We shorten only if unsigned or if
2157 dividing by something we know != -1. */
2158 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
2159 || (TREE_CODE (op1) == INTEGER_CST
2160 && ! integer_all_onesp (op1)));
400fbf9f
JW
2161 common = 1;
2162 }
2163 break;
2164
2165 case BIT_AND_EXPR:
2166 case BIT_ANDTC_EXPR:
2167 case BIT_IOR_EXPR:
2168 case BIT_XOR_EXPR:
2169 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2170 shorten = -1;
34a80643
R
2171 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
2172 common = 1;
400fbf9f
JW
2173 break;
2174
2175 case TRUNC_MOD_EXPR:
047de90b 2176 case FLOOR_MOD_EXPR:
6c36d76b
NB
2177 if (warn_div_by_zero && skip_evaluation == 0 && integer_zerop (op1))
2178 warning ("division by zero");
2179
400fbf9f 2180 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
03d5b1f5
RS
2181 {
2182 /* Although it would be tempting to shorten always here, that loses
2183 on some targets, since the modulo instruction is undefined if the
2184 quotient can't be represented in the computation mode. We shorten
2185 only if unsigned or if dividing by something we know != -1. */
96d8f1d8 2186 shorten = (TREE_UNSIGNED (TREE_TYPE (orig_op0))
03d5b1f5 2187 || (TREE_CODE (op1) == INTEGER_CST
05bccae2 2188 && ! integer_all_onesp (op1)));
03d5b1f5
RS
2189 common = 1;
2190 }
400fbf9f
JW
2191 break;
2192
2193 case TRUTH_ANDIF_EXPR:
2194 case TRUTH_ORIF_EXPR:
2195 case TRUTH_AND_EXPR:
2196 case TRUTH_OR_EXPR:
1eca8b1e 2197 case TRUTH_XOR_EXPR:
b6a10c9f
RS
2198 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
2199 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE)
2200 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
2201 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
400fbf9f
JW
2202 {
2203 /* Result of these operations is always an int,
2204 but that does not mean the operands should be
2205 converted to ints! */
2206 result_type = integer_type_node;
78ef5b89
NB
2207 op0 = c_common_truthvalue_conversion (op0);
2208 op1 = c_common_truthvalue_conversion (op1);
400fbf9f
JW
2209 converted = 1;
2210 }
2211 break;
2212
2213 /* Shift operations: result has same type as first operand;
2214 always convert second operand to int.
2215 Also set SHORT_SHIFT if shifting rightward. */
2216
2217 case RSHIFT_EXPR:
2218 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2219 {
47ee6837 2220 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
400fbf9f 2221 {
ff3225e7 2222 if (tree_int_cst_sgn (op1) < 0)
315da535 2223 warning ("right shift count is negative");
17651386
RS
2224 else
2225 {
05bccae2 2226 if (! integer_zerop (op1))
17651386 2227 short_shift = 1;
05bccae2
RK
2228
2229 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
315da535 2230 warning ("right shift count >= width of type");
17651386 2231 }
400fbf9f 2232 }
05bccae2 2233
f458d1d5 2234 /* Use the type of the value to be shifted. */
d45cf215 2235 result_type = type0;
f458d1d5
ZW
2236 /* Convert the shift-count to an integer, regardless of size
2237 of value being shifted. */
2238 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2239 op1 = convert (integer_type_node, op1);
2240 /* Avoid converting op1 to result_type later. */
2241 converted = 1;
400fbf9f
JW
2242 }
2243 break;
2244
2245 case LSHIFT_EXPR:
2246 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2247 {
47ee6837 2248 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
17651386 2249 {
ff3225e7 2250 if (tree_int_cst_sgn (op1) < 0)
315da535 2251 warning ("left shift count is negative");
05bccae2
RK
2252
2253 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
315da535 2254 warning ("left shift count >= width of type");
17651386 2255 }
05bccae2 2256
f458d1d5 2257 /* Use the type of the value to be shifted. */
d45cf215 2258 result_type = type0;
f458d1d5
ZW
2259 /* Convert the shift-count to an integer, regardless of size
2260 of value being shifted. */
2261 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2262 op1 = convert (integer_type_node, op1);
2263 /* Avoid converting op1 to result_type later. */
2264 converted = 1;
400fbf9f
JW
2265 }
2266 break;
2267
2268 case RROTATE_EXPR:
2269 case LROTATE_EXPR:
2270 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
2271 {
47ee6837 2272 if (TREE_CODE (op1) == INTEGER_CST && skip_evaluation == 0)
17651386 2273 {
ff3225e7 2274 if (tree_int_cst_sgn (op1) < 0)
17651386 2275 warning ("shift count is negative");
05bccae2 2276 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
17651386
RS
2277 warning ("shift count >= width of type");
2278 }
05bccae2 2279
f458d1d5 2280 /* Use the type of the value to be shifted. */
d45cf215 2281 result_type = type0;
f458d1d5
ZW
2282 /* Convert the shift-count to an integer, regardless of size
2283 of value being shifted. */
2284 if (TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
2285 op1 = convert (integer_type_node, op1);
2286 /* Avoid converting op1 to result_type later. */
2287 converted = 1;
400fbf9f
JW
2288 }
2289 break;
2290
2291 case EQ_EXPR:
2292 case NE_EXPR:
b843d210
DZ
2293 if (warn_float_equal && (code0 == REAL_TYPE || code1 == REAL_TYPE))
2294 warning ("comparing floating point with == or != is unsafe");
400fbf9f
JW
2295 /* Result of comparison is always int,
2296 but don't convert the args to int! */
293c9fdd 2297 build_type = integer_type_node;
b6a10c9f 2298 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
cb2a532e
AH
2299 || code0 == COMPLEX_TYPE
2300 || code0 == VECTOR_TYPE)
b6a10c9f 2301 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
cb2a532e
AH
2302 || code1 == COMPLEX_TYPE
2303 || code1 == VECTOR_TYPE))
400fbf9f
JW
2304 short_compare = 1;
2305 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2306 {
b3694847
SS
2307 tree tt0 = TREE_TYPE (type0);
2308 tree tt1 = TREE_TYPE (type1);
400fbf9f 2309 /* Anything compares with void *. void * compares with anything.
d11fdb45
RS
2310 Otherwise, the targets must be compatible
2311 and both must be object or both incomplete. */
1074d9d4 2312 if (comp_target_types (type0, type1, 1))
605a99f6 2313 result_type = common_type (type0, type1);
71653180 2314 else if (VOID_TYPE_P (tt0))
400fbf9f 2315 {
fd5d5b94
RS
2316 /* op0 != orig_op0 detects the case of something
2317 whose value is 0 but which isn't a valid null ptr const. */
2318 if (pedantic && (!integer_zerop (op0) || op0 != orig_op0)
400fbf9f 2319 && TREE_CODE (tt1) == FUNCTION_TYPE)
89abf8d1 2320 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
400fbf9f 2321 }
71653180 2322 else if (VOID_TYPE_P (tt1))
400fbf9f 2323 {
fd5d5b94 2324 if (pedantic && (!integer_zerop (op1) || op1 != orig_op1)
400fbf9f 2325 && TREE_CODE (tt0) == FUNCTION_TYPE)
89abf8d1 2326 pedwarn ("ISO C forbids comparison of `void *' with function pointer");
400fbf9f
JW
2327 }
2328 else
2329 pedwarn ("comparison of distinct pointer types lacks a cast");
605a99f6
JM
2330
2331 if (result_type == NULL_TREE)
2332 result_type = ptr_type_node;
400fbf9f
JW
2333 }
2334 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2335 && integer_zerop (op1))
293c9fdd 2336 result_type = type0;
400fbf9f
JW
2337 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2338 && integer_zerop (op0))
293c9fdd 2339 result_type = type1;
400fbf9f
JW
2340 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2341 {
293c9fdd 2342 result_type = type0;
f458d1d5 2343 pedwarn ("comparison between pointer and integer");
400fbf9f
JW
2344 }
2345 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2346 {
293c9fdd 2347 result_type = type1;
f458d1d5 2348 pedwarn ("comparison between pointer and integer");
400fbf9f 2349 }
400fbf9f
JW
2350 break;
2351
2352 case MAX_EXPR:
2353 case MIN_EXPR:
9db931af
RS
2354 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2355 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
400fbf9f
JW
2356 shorten = 1;
2357 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2358 {
1074d9d4 2359 if (comp_target_types (type0, type1, 1))
605a99f6
JM
2360 {
2361 result_type = common_type (type0, type1);
2f6e4e97 2362 if (pedantic
605a99f6 2363 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
89abf8d1 2364 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
605a99f6
JM
2365 }
2366 else
2367 {
2368 result_type = ptr_type_node;
2369 pedwarn ("comparison of distinct pointer types lacks a cast");
2370 }
400fbf9f
JW
2371 }
2372 break;
2373
2374 case LE_EXPR:
2375 case GE_EXPR:
2376 case LT_EXPR:
2377 case GT_EXPR:
293c9fdd 2378 build_type = integer_type_node;
9db931af
RS
2379 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
2380 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
400fbf9f
JW
2381 short_compare = 1;
2382 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
2383 {
1074d9d4 2384 if (comp_target_types (type0, type1, 1))
605a99f6
JM
2385 {
2386 result_type = common_type (type0, type1);
d0f062fb
NS
2387 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
2388 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
605a99f6 2389 pedwarn ("comparison of complete and incomplete pointers");
2f6e4e97 2390 else if (pedantic
605a99f6 2391 && TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
89abf8d1 2392 pedwarn ("ISO C forbids ordered comparisons of pointers to functions");
605a99f6
JM
2393 }
2394 else
2395 {
2396 result_type = ptr_type_node;
2397 pedwarn ("comparison of distinct pointer types lacks a cast");
2398 }
400fbf9f
JW
2399 }
2400 else if (code0 == POINTER_TYPE && TREE_CODE (op1) == INTEGER_CST
2401 && integer_zerop (op1))
2402 {
293c9fdd 2403 result_type = type0;
ddcf4abc 2404 if (pedantic || extra_warnings)
400fbf9f
JW
2405 pedwarn ("ordered comparison of pointer with integer zero");
2406 }
2407 else if (code1 == POINTER_TYPE && TREE_CODE (op0) == INTEGER_CST
2408 && integer_zerop (op0))
2409 {
293c9fdd 2410 result_type = type1;
400fbf9f
JW
2411 if (pedantic)
2412 pedwarn ("ordered comparison of pointer with integer zero");
2413 }
2414 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
2415 {
293c9fdd 2416 result_type = type0;
f458d1d5 2417 pedwarn ("comparison between pointer and integer");
400fbf9f
JW
2418 }
2419 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
2420 {
293c9fdd 2421 result_type = type1;
f458d1d5 2422 pedwarn ("comparison between pointer and integer");
400fbf9f 2423 }
400fbf9f 2424 break;
1eb8759b
RH
2425
2426 case UNORDERED_EXPR:
2427 case ORDERED_EXPR:
2428 case UNLT_EXPR:
2429 case UNLE_EXPR:
2430 case UNGT_EXPR:
2431 case UNGE_EXPR:
2432 case UNEQ_EXPR:
1eb8759b
RH
2433 build_type = integer_type_node;
2434 if (code0 != REAL_TYPE || code1 != REAL_TYPE)
2435 {
2436 error ("unordered comparison on non-floating point argument");
2437 return error_mark_node;
2438 }
2439 common = 1;
2440 break;
2441
e9a25f70
JL
2442 default:
2443 break;
400fbf9f
JW
2444 }
2445
cb2a532e
AH
2446 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
2447 || code0 == VECTOR_TYPE)
b6a10c9f 2448 &&
cb2a532e
AH
2449 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
2450 || code1 == VECTOR_TYPE))
400fbf9f 2451 {
b6a10c9f
RS
2452 int none_complex = (code0 != COMPLEX_TYPE && code1 != COMPLEX_TYPE);
2453
400fbf9f
JW
2454 if (shorten || common || short_compare)
2455 result_type = common_type (type0, type1);
2456
2457 /* For certain operations (which identify themselves by shorten != 0)
2458 if both args were extended from the same smaller type,
2459 do the arithmetic in that type and then extend.
2460
2461 shorten !=0 and !=1 indicates a bitwise operation.
2462 For them, this optimization is safe only if
2463 both args are zero-extended or both are sign-extended.
2464 Otherwise, we might change the result.
2465 Eg, (short)-1 | (unsigned short)-1 is (int)-1
2466 but calculated in (unsigned short) it would be (unsigned short)-1. */
2467
b6a10c9f 2468 if (shorten && none_complex)
400fbf9f
JW
2469 {
2470 int unsigned0, unsigned1;
2471 tree arg0 = get_narrower (op0, &unsigned0);
2472 tree arg1 = get_narrower (op1, &unsigned1);
2473 /* UNS is 1 if the operation to be done is an unsigned one. */
2474 int uns = TREE_UNSIGNED (result_type);
2475 tree type;
2476
2477 final_type = result_type;
2478
e7951b3f 2479 /* Handle the case that OP0 (or OP1) does not *contain* a conversion
400fbf9f
JW
2480 but it *requires* conversion to FINAL_TYPE. */
2481
e7951b3f
RS
2482 if ((TYPE_PRECISION (TREE_TYPE (op0))
2483 == TYPE_PRECISION (TREE_TYPE (arg0)))
2484 && TREE_TYPE (op0) != final_type)
400fbf9f 2485 unsigned0 = TREE_UNSIGNED (TREE_TYPE (op0));
e7951b3f
RS
2486 if ((TYPE_PRECISION (TREE_TYPE (op1))
2487 == TYPE_PRECISION (TREE_TYPE (arg1)))
2488 && TREE_TYPE (op1) != final_type)
400fbf9f
JW
2489 unsigned1 = TREE_UNSIGNED (TREE_TYPE (op1));
2490
2491 /* Now UNSIGNED0 is 1 if ARG0 zero-extends to FINAL_TYPE. */
2492
2493 /* For bitwise operations, signedness of nominal type
2494 does not matter. Consider only how operands were extended. */
2495 if (shorten == -1)
2496 uns = unsigned0;
2497
2498 /* Note that in all three cases below we refrain from optimizing
2499 an unsigned operation on sign-extended args.
2500 That would not be valid. */
2501
2502 /* Both args variable: if both extended in same way
2503 from same width, do it in that width.
2504 Do it unsigned if args were zero-extended. */
2505 if ((TYPE_PRECISION (TREE_TYPE (arg0))
2506 < TYPE_PRECISION (result_type))
2507 && (TYPE_PRECISION (TREE_TYPE (arg1))
2508 == TYPE_PRECISION (TREE_TYPE (arg0)))
2509 && unsigned0 == unsigned1
2510 && (unsigned0 || !uns))
2511 result_type
ceef8ce4
NB
2512 = c_common_signed_or_unsigned_type
2513 (unsigned0, common_type (TREE_TYPE (arg0), TREE_TYPE (arg1)));
400fbf9f
JW
2514 else if (TREE_CODE (arg0) == INTEGER_CST
2515 && (unsigned1 || !uns)
2516 && (TYPE_PRECISION (TREE_TYPE (arg1))
2517 < TYPE_PRECISION (result_type))
ceef8ce4
NB
2518 && (type
2519 = c_common_signed_or_unsigned_type (unsigned1,
2520 TREE_TYPE (arg1)),
400fbf9f
JW
2521 int_fits_type_p (arg0, type)))
2522 result_type = type;
2523 else if (TREE_CODE (arg1) == INTEGER_CST
2524 && (unsigned0 || !uns)
2525 && (TYPE_PRECISION (TREE_TYPE (arg0))
2526 < TYPE_PRECISION (result_type))
ceef8ce4
NB
2527 && (type
2528 = c_common_signed_or_unsigned_type (unsigned0,
2529 TREE_TYPE (arg0)),
400fbf9f
JW
2530 int_fits_type_p (arg1, type)))
2531 result_type = type;
2532 }
2533
2534 /* Shifts can be shortened if shifting right. */
2535
2536 if (short_shift)
2537 {
2538 int unsigned_arg;
2539 tree arg0 = get_narrower (op0, &unsigned_arg);
2540
2541 final_type = result_type;
2542
2543 if (arg0 == op0 && final_type == TREE_TYPE (op0))
2544 unsigned_arg = TREE_UNSIGNED (TREE_TYPE (op0));
2545
2546 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
6cb70f0c
JW
2547 /* We can shorten only if the shift count is less than the
2548 number of bits in the smaller type size. */
05bccae2 2549 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
91a18fe0
RH
2550 /* We cannot drop an unsigned shift after sign-extension. */
2551 && (!TREE_UNSIGNED (final_type) || unsigned_arg))
400fbf9f
JW
2552 {
2553 /* Do an unsigned shift if the operand was zero-extended. */
2554 result_type
ceef8ce4
NB
2555 = c_common_signed_or_unsigned_type (unsigned_arg,
2556 TREE_TYPE (arg0));
400fbf9f
JW
2557 /* Convert value-to-be-shifted to that type. */
2558 if (TREE_TYPE (op0) != result_type)
2559 op0 = convert (result_type, op0);
2560 converted = 1;
2561 }
2562 }
2563
2564 /* Comparison operations are shortened too but differently.
2565 They identify themselves by setting short_compare = 1. */
2566
75326e8c 2567 if (short_compare)
400fbf9f
JW
2568 {
2569 /* Don't write &op0, etc., because that would prevent op0
2570 from being kept in a register.
2571 Instead, make copies of the our local variables and
2572 pass the copies by reference, then copy them back afterward. */
2573 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
2574 enum tree_code xresultcode = resultcode;
2f6e4e97 2575 tree val
400fbf9f 2576 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
665f2503 2577
400fbf9f
JW
2578 if (val != 0)
2579 return val;
665f2503 2580
293c9fdd
JM
2581 op0 = xop0, op1 = xop1;
2582 converted = 1;
400fbf9f
JW
2583 resultcode = xresultcode;
2584
87f85ea0 2585 if (warn_sign_compare && skip_evaluation == 0)
400fbf9f 2586 {
d2d7ed3e
JM
2587 int op0_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op0));
2588 int op1_signed = ! TREE_UNSIGNED (TREE_TYPE (orig_op1));
64c01f80
DE
2589 int unsignedp0, unsignedp1;
2590 tree primop0 = get_narrower (op0, &unsignedp0);
2591 tree primop1 = get_narrower (op1, &unsignedp1);
2592
912b4fc3
JM
2593 xop0 = orig_op0;
2594 xop1 = orig_op1;
2595 STRIP_TYPE_NOPS (xop0);
2596 STRIP_TYPE_NOPS (xop1);
2597
400fbf9f 2598 /* Give warnings for comparisons between signed and unsigned
2f6e4e97 2599 quantities that may fail.
293c9fdd 2600
665f2503
RK
2601 Do the checking based on the original operand trees, so that
2602 casts will be considered, but default promotions won't be.
2603
2604 Do not warn if the comparison is being done in a signed type,
293c9fdd
JM
2605 since the signed type will only be chosen if it can represent
2606 all the values of the unsigned type. */
2607 if (! TREE_UNSIGNED (result_type))
2608 /* OK */;
cb3ca04e 2609 /* Do not warn if both operands are the same signedness. */
2e14370e
JM
2610 else if (op0_signed == op1_signed)
2611 /* OK */;
293c9fdd 2612 else
cb3ca04e
ZW
2613 {
2614 tree sop, uop;
665f2503 2615
cb3ca04e
ZW
2616 if (op0_signed)
2617 sop = xop0, uop = xop1;
2618 else
2619 sop = xop1, uop = xop0;
2620
3facde26
KG
2621 /* Do not warn if the signed quantity is an
2622 unsuffixed integer literal (or some static
2623 constant expression involving such literals or a
2624 conditional expression involving such literals)
2625 and it is non-negative. */
54c93c30 2626 if (c_tree_expr_nonnegative_p (sop))
cb3ca04e
ZW
2627 /* OK */;
2628 /* Do not warn if the comparison is an equality operation,
2629 the unsigned quantity is an integral constant, and it
2630 would fit in the result if the result were signed. */
2631 else if (TREE_CODE (uop) == INTEGER_CST
2632 && (resultcode == EQ_EXPR || resultcode == NE_EXPR)
ceef8ce4
NB
2633 && int_fits_type_p
2634 (uop, c_common_signed_type (result_type)))
cb3ca04e
ZW
2635 /* OK */;
2636 /* Do not warn if the unsigned quantity is an enumeration
2637 constant and its maximum value would fit in the result
2638 if the result were signed. */
2639 else if (TREE_CODE (uop) == INTEGER_CST
2640 && TREE_CODE (TREE_TYPE (uop)) == ENUMERAL_TYPE
ceef8ce4
NB
2641 && int_fits_type_p
2642 (TYPE_MAX_VALUE (TREE_TYPE(uop)),
2643 c_common_signed_type (result_type)))
cb3ca04e
ZW
2644 /* OK */;
2645 else
2646 warning ("comparison between signed and unsigned");
2647 }
64c01f80
DE
2648
2649 /* Warn if two unsigned values are being compared in a size
2650 larger than their original size, and one (and only one) is the
2651 result of a `~' operator. This comparison will always fail.
2652
2653 Also warn if one operand is a constant, and the constant
2654 does not have all bits set that are set in the ~ operand
2655 when it is extended. */
2656
2657 if ((TREE_CODE (primop0) == BIT_NOT_EXPR)
2658 != (TREE_CODE (primop1) == BIT_NOT_EXPR))
2659 {
2660 if (TREE_CODE (primop0) == BIT_NOT_EXPR)
2661 primop0 = get_narrower (TREE_OPERAND (primop0, 0),
2662 &unsignedp0);
2663 else
2664 primop1 = get_narrower (TREE_OPERAND (primop1, 0),
2665 &unsignedp1);
2f6e4e97 2666
665f2503 2667 if (host_integerp (primop0, 0) || host_integerp (primop1, 0))
64c01f80
DE
2668 {
2669 tree primop;
05bccae2 2670 HOST_WIDE_INT constant, mask;
64c01f80
DE
2671 int unsignedp, bits;
2672
665f2503 2673 if (host_integerp (primop0, 0))
64c01f80
DE
2674 {
2675 primop = primop1;
2676 unsignedp = unsignedp1;
665f2503 2677 constant = tree_low_cst (primop0, 0);
64c01f80
DE
2678 }
2679 else
2680 {
2681 primop = primop0;
2682 unsignedp = unsignedp0;
665f2503 2683 constant = tree_low_cst (primop1, 0);
64c01f80
DE
2684 }
2685
2686 bits = TYPE_PRECISION (TREE_TYPE (primop));
2687 if (bits < TYPE_PRECISION (result_type)
05bccae2 2688 && bits < HOST_BITS_PER_WIDE_INT && unsignedp)
64c01f80 2689 {
05bccae2 2690 mask = (~ (HOST_WIDE_INT) 0) << bits;
64c01f80
DE
2691 if ((mask & constant) != mask)
2692 warning ("comparison of promoted ~unsigned with constant");
2693 }
2694 }
2695 else if (unsignedp0 && unsignedp1
2696 && (TYPE_PRECISION (TREE_TYPE (primop0))
2697 < TYPE_PRECISION (result_type))
2698 && (TYPE_PRECISION (TREE_TYPE (primop1))
2699 < TYPE_PRECISION (result_type)))
2700 warning ("comparison of promoted ~unsigned with unsigned");
2701 }
400fbf9f
JW
2702 }
2703 }
2704 }
2705
2706 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
2707 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
2708 Then the expression will be built.
2709 It will be given type FINAL_TYPE if that is nonzero;
2710 otherwise, it will be given type RESULT_TYPE. */
2711
2712 if (!result_type)
2713 {
2714 binary_op_error (code);
2715 return error_mark_node;
2716 }
2717
2718 if (! converted)
2719 {
2720 if (TREE_TYPE (op0) != result_type)
2f6e4e97 2721 op0 = convert (result_type, op0);
400fbf9f 2722 if (TREE_TYPE (op1) != result_type)
2f6e4e97 2723 op1 = convert (result_type, op1);
400fbf9f
JW
2724 }
2725
293c9fdd
JM
2726 if (build_type == NULL_TREE)
2727 build_type = result_type;
2728
400fbf9f 2729 {
b3694847
SS
2730 tree result = build (resultcode, build_type, op0, op1);
2731 tree folded;
400fbf9f
JW
2732
2733 folded = fold (result);
2734 if (folded == result)
2735 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2736 if (final_type != 0)
2737 return convert (final_type, folded);
2738 return folded;
2739 }
2740}
2741\f
54c93c30
AE
2742
2743/* Return true if `t' is known to be non-negative. */
2744
2745int
2f6e4e97 2746c_tree_expr_nonnegative_p (tree t)
54c93c30
AE
2747{
2748 if (TREE_CODE (t) == STMT_EXPR)
2749 {
6a87d634 2750 t = COMPOUND_BODY (STMT_EXPR_STMT (t));
54c93c30
AE
2751
2752 /* Find the last statement in the chain, ignoring the final
2753 * scope statement */
2f6e4e97 2754 while (TREE_CHAIN (t) != NULL_TREE
54c93c30 2755 && TREE_CODE (TREE_CHAIN (t)) != SCOPE_STMT)
6a87d634 2756 t = TREE_CHAIN (t);
54c93c30
AE
2757 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
2758 }
2759 return tree_expr_nonnegative_p (t);
2760}
2761
400fbf9f
JW
2762/* Return a tree for the difference of pointers OP0 and OP1.
2763 The resulting tree has type int. */
2764
2765static tree
2f6e4e97 2766pointer_diff (tree op0, tree op1)
400fbf9f 2767{
b3694847 2768 tree result, folded;
400fbf9f
JW
2769 tree restype = ptrdiff_type_node;
2770
2771 tree target_type = TREE_TYPE (TREE_TYPE (op0));
95602da1
R
2772 tree con0, con1, lit0, lit1;
2773 tree orig_op1 = op1;
400fbf9f
JW
2774
2775 if (pedantic || warn_pointer_arith)
2776 {
2777 if (TREE_CODE (target_type) == VOID_TYPE)
2778 pedwarn ("pointer of type `void *' used in subtraction");
2779 if (TREE_CODE (target_type) == FUNCTION_TYPE)
2780 pedwarn ("pointer to a function used in subtraction");
2781 }
2782
95602da1
R
2783 /* If the conversion to ptrdiff_type does anything like widening or
2784 converting a partial to an integral mode, we get a convert_expression
2785 that is in the way to do any simplifications.
2786 (fold-const.c doesn't know that the extra bits won't be needed.
2787 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
2788 different mode in place.)
2789 So first try to find a common term here 'by hand'; we want to cover
2790 at least the cases that occur in legal static initializers. */
2791 con0 = TREE_CODE (op0) == NOP_EXPR ? TREE_OPERAND (op0, 0) : op0;
2792 con1 = TREE_CODE (op1) == NOP_EXPR ? TREE_OPERAND (op1, 0) : op1;
2793
2794 if (TREE_CODE (con0) == PLUS_EXPR)
2795 {
2796 lit0 = TREE_OPERAND (con0, 1);
2797 con0 = TREE_OPERAND (con0, 0);
2798 }
2799 else
2800 lit0 = integer_zero_node;
2801
2802 if (TREE_CODE (con1) == PLUS_EXPR)
2803 {
2804 lit1 = TREE_OPERAND (con1, 1);
2805 con1 = TREE_OPERAND (con1, 0);
2806 }
2807 else
2808 lit1 = integer_zero_node;
2809
2810 if (operand_equal_p (con0, con1, 0))
2811 {
2812 op0 = lit0;
2813 op1 = lit1;
2814 }
2815
2816
400fbf9f 2817 /* First do the subtraction as integers;
04044297
MM
2818 then drop through to build the divide operator.
2819 Do not do default conversions on the minus operator
2820 in case restype is a short type. */
400fbf9f
JW
2821
2822 op0 = build_binary_op (MINUS_EXPR, convert (restype, op0),
04044297 2823 convert (restype, op1), 0);
ea8dd784 2824 /* This generates an error if op1 is pointer to incomplete type. */
95602da1 2825 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
ea8dd784 2826 error ("arithmetic on pointer to an incomplete type");
6946afd3 2827
ea8dd784 2828 /* This generates an error if op0 is pointer to incomplete type. */
400fbf9f
JW
2829 op1 = c_size_in_bytes (target_type);
2830
2831 /* Divide by the size, in easiest possible way. */
2832
6946afd3 2833 result = build (EXACT_DIV_EXPR, restype, op0, convert (restype, op1));
400fbf9f
JW
2834
2835 folded = fold (result);
2836 if (folded == result)
2837 TREE_CONSTANT (folded) = TREE_CONSTANT (op0) & TREE_CONSTANT (op1);
2838 return folded;
2839}
2840\f
2841/* Construct and perhaps optimize a tree representation
2842 for a unary operation. CODE, a tree_code, specifies the operation
207bf485
JM
2843 and XARG is the operand.
2844 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
2845 the default promotions (such as from short to int).
2846 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
2847 allows non-lvalues; this is only used to handle conversion of non-lvalue
2848 arrays to pointers in C99. */
400fbf9f
JW
2849
2850tree
2f6e4e97 2851build_unary_op (enum tree_code code, tree xarg, int flag)
400fbf9f
JW
2852{
2853 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
b3694847
SS
2854 tree arg = xarg;
2855 tree argtype = 0;
2856 enum tree_code typecode = TREE_CODE (TREE_TYPE (arg));
400fbf9f 2857 tree val;
207bf485 2858 int noconvert = flag;
400fbf9f
JW
2859
2860 if (typecode == ERROR_MARK)
2861 return error_mark_node;
19552aa5 2862 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
400fbf9f
JW
2863 typecode = INTEGER_TYPE;
2864
2865 switch (code)
2866 {
2867 case CONVERT_EXPR:
2868 /* This is used for unary plus, because a CONVERT_EXPR
2869 is enough to prevent anybody from looking inside for
2870 associativity, but won't generate any code. */
b6a10c9f
RS
2871 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2872 || typecode == COMPLEX_TYPE))
ab87f8c8
JL
2873 {
2874 error ("wrong type argument to unary plus");
2875 return error_mark_node;
2876 }
400fbf9f
JW
2877 else if (!noconvert)
2878 arg = default_conversion (arg);
ae5d234e 2879 arg = non_lvalue (arg);
400fbf9f
JW
2880 break;
2881
2882 case NEGATE_EXPR:
b6a10c9f 2883 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
cb2a532e
AH
2884 || typecode == COMPLEX_TYPE
2885 || typecode == VECTOR_TYPE))
ab87f8c8
JL
2886 {
2887 error ("wrong type argument to unary minus");
2888 return error_mark_node;
2889 }
400fbf9f
JW
2890 else if (!noconvert)
2891 arg = default_conversion (arg);
2892 break;
2893
2894 case BIT_NOT_EXPR:
34a80643
R
2895 if (typecode == INTEGER_TYPE || typecode == VECTOR_TYPE)
2896 {
2897 if (!noconvert)
2898 arg = default_conversion (arg);
2899 }
2900 else if (typecode == COMPLEX_TYPE)
1c2a9b35
RS
2901 {
2902 code = CONJ_EXPR;
60e9d01c
JM
2903 if (pedantic)
2904 pedwarn ("ISO C does not support `~' for complex conjugation");
1c2a9b35
RS
2905 if (!noconvert)
2906 arg = default_conversion (arg);
2907 }
34a80643 2908 else
ab87f8c8
JL
2909 {
2910 error ("wrong type argument to bit-complement");
2911 return error_mark_node;
2912 }
400fbf9f
JW
2913 break;
2914
2915 case ABS_EXPR:
b6a10c9f
RS
2916 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2917 || typecode == COMPLEX_TYPE))
ab87f8c8
JL
2918 {
2919 error ("wrong type argument to abs");
2920 return error_mark_node;
2921 }
400fbf9f
JW
2922 else if (!noconvert)
2923 arg = default_conversion (arg);
2924 break;
2925
1c2a9b35
RS
2926 case CONJ_EXPR:
2927 /* Conjugating a real value is a no-op, but allow it anyway. */
2928 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
2929 || typecode == COMPLEX_TYPE))
ab87f8c8
JL
2930 {
2931 error ("wrong type argument to conjugation");
2932 return error_mark_node;
2933 }
1c2a9b35
RS
2934 else if (!noconvert)
2935 arg = default_conversion (arg);
2936 break;
2937
400fbf9f
JW
2938 case TRUTH_NOT_EXPR:
2939 if (typecode != INTEGER_TYPE
2940 && typecode != REAL_TYPE && typecode != POINTER_TYPE
b6a10c9f 2941 && typecode != COMPLEX_TYPE
400fbf9f
JW
2942 /* These will convert to a pointer. */
2943 && typecode != ARRAY_TYPE && typecode != FUNCTION_TYPE)
2944 {
ab87f8c8
JL
2945 error ("wrong type argument to unary exclamation mark");
2946 return error_mark_node;
400fbf9f 2947 }
78ef5b89 2948 arg = c_common_truthvalue_conversion (arg);
400fbf9f
JW
2949 return invert_truthvalue (arg);
2950
2951 case NOP_EXPR:
2952 break;
b6a10c9f
RS
2953
2954 case REALPART_EXPR:
2955 if (TREE_CODE (arg) == COMPLEX_CST)
2956 return TREE_REALPART (arg);
2957 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2958 return fold (build1 (REALPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2959 else
2960 return arg;
2961
2962 case IMAGPART_EXPR:
2963 if (TREE_CODE (arg) == COMPLEX_CST)
2964 return TREE_IMAGPART (arg);
2965 else if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
2966 return fold (build1 (IMAGPART_EXPR, TREE_TYPE (TREE_TYPE (arg)), arg));
2967 else
2968 return convert (TREE_TYPE (arg), integer_zero_node);
2f6e4e97 2969
400fbf9f
JW
2970 case PREINCREMENT_EXPR:
2971 case POSTINCREMENT_EXPR:
2972 case PREDECREMENT_EXPR:
2973 case POSTDECREMENT_EXPR:
2974 /* Handle complex lvalues (when permitted)
2975 by reduction to simpler cases. */
2976
207bf485 2977 val = unary_complex_lvalue (code, arg, 0);
400fbf9f
JW
2978 if (val != 0)
2979 return val;
2980
b6a10c9f
RS
2981 /* Increment or decrement the real part of the value,
2982 and don't change the imaginary part. */
2983 if (typecode == COMPLEX_TYPE)
2984 {
2985 tree real, imag;
2986
93cc1c69
JM
2987 if (pedantic)
2988 pedwarn ("ISO C does not support `++' and `--' on complex types");
2989
b6a10c9f
RS
2990 arg = stabilize_reference (arg);
2991 real = build_unary_op (REALPART_EXPR, arg, 1);
2992 imag = build_unary_op (IMAGPART_EXPR, arg, 1);
2993 return build (COMPLEX_EXPR, TREE_TYPE (arg),
2994 build_unary_op (code, real, 1), imag);
2995 }
2996
400fbf9f
JW
2997 /* Report invalid types. */
2998
2999 if (typecode != POINTER_TYPE
3000 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
3001 {
53fcdc76
PB
3002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3003 error ("wrong type argument to increment");
3004 else
3005 error ("wrong type argument to decrement");
3006
ab87f8c8 3007 return error_mark_node;
400fbf9f
JW
3008 }
3009
3010 {
b3694847 3011 tree inc;
400fbf9f
JW
3012 tree result_type = TREE_TYPE (arg);
3013
3014 arg = get_unwidened (arg, 0);
3015 argtype = TREE_TYPE (arg);
3016
3017 /* Compute the increment. */
3018
3019 if (typecode == POINTER_TYPE)
3020 {
6bc4e3d0
RS
3021 /* If pointer target is an undefined struct,
3022 we just cannot know how to do the arithmetic. */
d0f062fb 3023 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (result_type)))
53fcdc76
PB
3024 {
3025 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3026 error ("increment of pointer to unknown structure");
3027 else
3028 error ("decrement of pointer to unknown structure");
3029 }
6bc4e3d0
RS
3030 else if ((pedantic || warn_pointer_arith)
3031 && (TREE_CODE (TREE_TYPE (result_type)) == FUNCTION_TYPE
3032 || TREE_CODE (TREE_TYPE (result_type)) == VOID_TYPE))
53fcdc76
PB
3033 {
3034 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3035 pedwarn ("wrong type argument to increment");
3036 else
3037 pedwarn ("wrong type argument to decrement");
3038 }
3039
0e9cff7f 3040 inc = c_size_in_bytes (TREE_TYPE (result_type));
400fbf9f
JW
3041 }
3042 else
3043 inc = integer_one_node;
3044
3045 inc = convert (argtype, inc);
3046
3047 /* Handle incrementing a cast-expression. */
3048
3049 while (1)
3050 switch (TREE_CODE (arg))
3051 {
3052 case NOP_EXPR:
3053 case CONVERT_EXPR:
3054 case FLOAT_EXPR:
3055 case FIX_TRUNC_EXPR:
3056 case FIX_FLOOR_EXPR:
3057 case FIX_ROUND_EXPR:
3058 case FIX_CEIL_EXPR:
ee71df46 3059 pedantic_lvalue_warning (CONVERT_EXPR);
400fbf9f
JW
3060 /* If the real type has the same machine representation
3061 as the type it is cast to, we can make better output
3062 by adding directly to the inside of the cast. */
3063 if ((TREE_CODE (TREE_TYPE (arg))
3064 == TREE_CODE (TREE_TYPE (TREE_OPERAND (arg, 0))))
3065 && (TYPE_MODE (TREE_TYPE (arg))
3066 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (arg, 0)))))
3067 arg = TREE_OPERAND (arg, 0);
3068 else
3069 {
3070 tree incremented, modify, value;
19552aa5
JM
3071 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3072 value = boolean_increment (code, arg);
400fbf9f 3073 else
19552aa5
JM
3074 {
3075 arg = stabilize_reference (arg);
3076 if (code == PREINCREMENT_EXPR || code == PREDECREMENT_EXPR)
3077 value = arg;
3078 else
3079 value = save_expr (arg);
3080 incremented = build (((code == PREINCREMENT_EXPR
3081 || code == POSTINCREMENT_EXPR)
3082 ? PLUS_EXPR : MINUS_EXPR),
3083 argtype, value, inc);
3084 TREE_SIDE_EFFECTS (incremented) = 1;
3085 modify = build_modify_expr (arg, NOP_EXPR, incremented);
3086 value = build (COMPOUND_EXPR, TREE_TYPE (arg), modify, value);
3087 }
400fbf9f
JW
3088 TREE_USED (value) = 1;
3089 return value;
3090 }
3091 break;
3092
3093 default:
3094 goto give_up;
3095 }
3096 give_up:
3097
3098 /* Complain about anything else that is not a true lvalue. */
3099 if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
3100 || code == POSTINCREMENT_EXPR)
ab87f8c8
JL
3101 ? "invalid lvalue in increment"
3102 : "invalid lvalue in decrement")))
400fbf9f
JW
3103 return error_mark_node;
3104
3105 /* Report a read-only lvalue. */
26b3c423 3106 if (TREE_READONLY (arg))
2f6e4e97 3107 readonly_warning (arg,
400fbf9f
JW
3108 ((code == PREINCREMENT_EXPR
3109 || code == POSTINCREMENT_EXPR)
57771fe8 3110 ? "increment" : "decrement"));
400fbf9f 3111
19552aa5
JM
3112 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3113 val = boolean_increment (code, arg);
3114 else
3115 val = build (code, TREE_TYPE (arg), arg, inc);
400fbf9f
JW
3116 TREE_SIDE_EFFECTS (val) = 1;
3117 val = convert (result_type, val);
3118 if (TREE_CODE (val) != code)
3119 TREE_NO_UNUSED_WARNING (val) = 1;
3120 return val;
3121 }
3122
3123 case ADDR_EXPR:
207bf485 3124 /* Note that this operation never does default_conversion. */
400fbf9f
JW
3125
3126 /* Let &* cancel out to simplify resulting code. */
3127 if (TREE_CODE (arg) == INDIRECT_REF)
3128 {
3129 /* Don't let this be an lvalue. */
3130 if (lvalue_p (TREE_OPERAND (arg, 0)))
3131 return non_lvalue (TREE_OPERAND (arg, 0));
3132 return TREE_OPERAND (arg, 0);
3133 }
3134
3135 /* For &x[y], return x+y */
3136 if (TREE_CODE (arg) == ARRAY_REF)
3137 {
dffd7eb6 3138 if (!c_mark_addressable (TREE_OPERAND (arg, 0)))
400fbf9f
JW
3139 return error_mark_node;
3140 return build_binary_op (PLUS_EXPR, TREE_OPERAND (arg, 0),
3141 TREE_OPERAND (arg, 1), 1);
3142 }
3143
3144 /* Handle complex lvalues (when permitted)
3145 by reduction to simpler cases. */
207bf485 3146 val = unary_complex_lvalue (code, arg, flag);
400fbf9f
JW
3147 if (val != 0)
3148 return val;
3149
400fbf9f 3150 /* Anything not already handled and not a true memory reference
207bf485
JM
3151 or a non-lvalue array is an error. */
3152 else if (typecode != FUNCTION_TYPE && !flag
ab87f8c8 3153 && !lvalue_or_else (arg, "invalid lvalue in unary `&'"))
400fbf9f
JW
3154 return error_mark_node;
3155
3156 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3157 argtype = TREE_TYPE (arg);
770ae6cc 3158
3932261a
MM
3159 /* If the lvalue is const or volatile, merge that into the type
3160 to which the address will point. Note that you can't get a
3161 restricted pointer by taking the address of something, so we
3162 only have to deal with `const' and `volatile' here. */
770ae6cc
RK
3163 if ((DECL_P (arg) || TREE_CODE_CLASS (TREE_CODE (arg)) == 'r')
3164 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg)))
3165 argtype = c_build_type_variant (argtype,
3166 TREE_READONLY (arg),
3167 TREE_THIS_VOLATILE (arg));
400fbf9f
JW
3168
3169 argtype = build_pointer_type (argtype);
3170
dffd7eb6 3171 if (!c_mark_addressable (arg))
400fbf9f
JW
3172 return error_mark_node;
3173
3174 {
3175 tree addr;
3176
3177 if (TREE_CODE (arg) == COMPONENT_REF)
3178 {
3179 tree field = TREE_OPERAND (arg, 1);
3180
207bf485 3181 addr = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), flag);
400fbf9f 3182
ef86d2a6 3183 if (DECL_C_BIT_FIELD (field))
400fbf9f
JW
3184 {
3185 error ("attempt to take address of bit-field structure member `%s'",
3186 IDENTIFIER_POINTER (DECL_NAME (field)));
3187 return error_mark_node;
3188 }
3189
770ae6cc
RK
3190 addr = fold (build (PLUS_EXPR, argtype,
3191 convert (argtype, addr),
3192 convert (argtype, byte_position (field))));
400fbf9f
JW
3193 }
3194 else
3195 addr = build1 (code, argtype, arg);
3196
3197 /* Address of a static or external variable or
8706edbc
RS
3198 file-scope function counts as a constant. */
3199 if (staticp (arg)
3200 && ! (TREE_CODE (arg) == FUNCTION_DECL
d1bd0ded 3201 && !C_DECL_FILE_SCOPE (arg)))
7d2d49af 3202 TREE_CONSTANT (addr) = 1;
400fbf9f
JW
3203 return addr;
3204 }
e9a25f70
JL
3205
3206 default:
3207 break;
400fbf9f
JW
3208 }
3209
ab87f8c8
JL
3210 if (argtype == 0)
3211 argtype = TREE_TYPE (arg);
3212 return fold (build1 (code, argtype, arg));
400fbf9f
JW
3213}
3214
400fbf9f
JW
3215/* Return nonzero if REF is an lvalue valid for this language.
3216 Lvalues can be assigned, unless their type has TYPE_READONLY.
1394aabd 3217 Lvalues can have their address taken, unless they have DECL_REGISTER. */
400fbf9f
JW
3218
3219int
2f6e4e97 3220lvalue_p (tree ref)
400fbf9f 3221{
b3694847 3222 enum tree_code code = TREE_CODE (ref);
400fbf9f
JW
3223
3224 switch (code)
3225 {
b6a10c9f
RS
3226 case REALPART_EXPR:
3227 case IMAGPART_EXPR:
400fbf9f
JW
3228 case COMPONENT_REF:
3229 return lvalue_p (TREE_OPERAND (ref, 0));
3230
db3acfa5 3231 case COMPOUND_LITERAL_EXPR:
400fbf9f
JW
3232 case STRING_CST:
3233 return 1;
3234
3235 case INDIRECT_REF:
3236 case ARRAY_REF:
3237 case VAR_DECL:
3238 case PARM_DECL:
3239 case RESULT_DECL:
3240 case ERROR_MARK:
e9a25f70
JL
3241 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3242 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
cff9c407
RK
3243
3244 case BIND_EXPR:
3245 case RTL_EXPR:
e9a25f70
JL
3246 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
3247
3248 default:
3249 return 0;
400fbf9f 3250 }
400fbf9f
JW
3251}
3252
3253/* Return nonzero if REF is an lvalue valid for this language;
3254 otherwise, print an error message and return zero. */
3255
3256int
2f6e4e97 3257lvalue_or_else (tree ref, const char *msgid)
400fbf9f
JW
3258{
3259 int win = lvalue_p (ref);
c5c76735 3260
400fbf9f 3261 if (! win)
913d0833 3262 error ("%s", msgid);
c5c76735 3263
400fbf9f
JW
3264 return win;
3265}
3266
3267/* Apply unary lvalue-demanding operator CODE to the expression ARG
3268 for certain kinds of expressions which are not really lvalues
207bf485
JM
3269 but which we can accept as lvalues. If FLAG is nonzero, then
3270 non-lvalues are OK since we may be converting a non-lvalue array to
3271 a pointer in C99.
400fbf9f
JW
3272
3273 If ARG is not a kind of expression we can handle, return zero. */
2f6e4e97 3274
400fbf9f 3275static tree
2f6e4e97 3276unary_complex_lvalue (enum tree_code code, tree arg, int flag)
400fbf9f
JW
3277{
3278 /* Handle (a, b) used as an "lvalue". */
3279 if (TREE_CODE (arg) == COMPOUND_EXPR)
3280 {
3281 tree real_result = build_unary_op (code, TREE_OPERAND (arg, 1), 0);
e9a25f70
JL
3282
3283 /* If this returns a function type, it isn't really being used as
3284 an lvalue, so don't issue a warning about it. */
207bf485 3285 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
e9a25f70
JL
3286 pedantic_lvalue_warning (COMPOUND_EXPR);
3287
400fbf9f
JW
3288 return build (COMPOUND_EXPR, TREE_TYPE (real_result),
3289 TREE_OPERAND (arg, 0), real_result);
3290 }
3291
3292 /* Handle (a ? b : c) used as an "lvalue". */
3293 if (TREE_CODE (arg) == COND_EXPR)
3294 {
207bf485
JM
3295 if (!flag)
3296 pedantic_lvalue_warning (COND_EXPR);
3297 if (TREE_CODE (TREE_TYPE (arg)) != FUNCTION_TYPE && !flag)
e9a25f70
JL
3298 pedantic_lvalue_warning (COMPOUND_EXPR);
3299
400fbf9f
JW
3300 return (build_conditional_expr
3301 (TREE_OPERAND (arg, 0),
207bf485
JM
3302 build_unary_op (code, TREE_OPERAND (arg, 1), flag),
3303 build_unary_op (code, TREE_OPERAND (arg, 2), flag)));
400fbf9f
JW
3304 }
3305
3306 return 0;
3307}
3308
3309/* If pedantic, warn about improper lvalue. CODE is either COND_EXPR
3310 COMPOUND_EXPR, or CONVERT_EXPR (for casts). */
3311
3312static void
2f6e4e97 3313pedantic_lvalue_warning (enum tree_code code)
400fbf9f
JW
3314{
3315 if (pedantic)
913d0833
KG
3316 switch (code)
3317 {
3318 case COND_EXPR:
89abf8d1 3319 pedwarn ("ISO C forbids use of conditional expressions as lvalues");
913d0833
KG
3320 break;
3321 case COMPOUND_EXPR:
89abf8d1 3322 pedwarn ("ISO C forbids use of compound expressions as lvalues");
913d0833
KG
3323 break;
3324 default:
89abf8d1 3325 pedwarn ("ISO C forbids use of cast expressions as lvalues");
913d0833
KG
3326 break;
3327 }
400fbf9f
JW
3328}
3329\f
3330/* Warn about storing in something that is `const'. */
3331
3332void
2f6e4e97 3333readonly_warning (tree arg, const char *msgid)
400fbf9f 3334{
400fbf9f
JW
3335 if (TREE_CODE (arg) == COMPONENT_REF)
3336 {
3337 if (TYPE_READONLY (TREE_TYPE (TREE_OPERAND (arg, 0))))
ab87f8c8 3338 readonly_warning (TREE_OPERAND (arg, 0), msgid);
400fbf9f 3339 else
ab87f8c8
JL
3340 pedwarn ("%s of read-only member `%s'", _(msgid),
3341 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (arg, 1))));
400fbf9f
JW
3342 }
3343 else if (TREE_CODE (arg) == VAR_DECL)
ab87f8c8
JL
3344 pedwarn ("%s of read-only variable `%s'", _(msgid),
3345 IDENTIFIER_POINTER (DECL_NAME (arg)));
400fbf9f 3346 else
ab87f8c8 3347 pedwarn ("%s of read-only location", _(msgid));
400fbf9f
JW
3348}
3349\f
3350/* Mark EXP saying that we need to be able to take the
3351 address of it; it should not be allocated in a register.
dffd7eb6 3352 Returns true if successful. */
400fbf9f 3353
dffd7eb6 3354bool
2f6e4e97 3355c_mark_addressable (tree exp)
400fbf9f 3356{
b3694847 3357 tree x = exp;
dffd7eb6 3358
400fbf9f
JW
3359 while (1)
3360 switch (TREE_CODE (x))
3361 {
400fbf9f 3362 case COMPONENT_REF:
1598f4da 3363 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
36c336d1 3364 {
b0287a90 3365 error ("cannot take address of bit-field `%s'",
36c336d1 3366 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (x, 1))));
dffd7eb6 3367 return false;
36c336d1 3368 }
1598f4da 3369
0f41302f 3370 /* ... fall through ... */
1598f4da
RK
3371
3372 case ADDR_EXPR:
400fbf9f 3373 case ARRAY_REF:
ce95080d
RS
3374 case REALPART_EXPR:
3375 case IMAGPART_EXPR:
400fbf9f
JW
3376 x = TREE_OPERAND (x, 0);
3377 break;
3378
db3acfa5 3379 case COMPOUND_LITERAL_EXPR:
400fbf9f
JW
3380 case CONSTRUCTOR:
3381 TREE_ADDRESSABLE (x) = 1;
dffd7eb6 3382 return true;
400fbf9f
JW
3383
3384 case VAR_DECL:
3385 case CONST_DECL:
3386 case PARM_DECL:
3387 case RESULT_DECL:
1394aabd
RS
3388 if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
3389 && DECL_NONLOCAL (x))
4bb6d2f8
RS
3390 {
3391 if (TREE_PUBLIC (x))
3392 {
3393 error ("global register variable `%s' used in nested function",
3394 IDENTIFIER_POINTER (DECL_NAME (x)));
dffd7eb6 3395 return false;
4bb6d2f8
RS
3396 }
3397 pedwarn ("register variable `%s' used in nested function",
3398 IDENTIFIER_POINTER (DECL_NAME (x)));
3399 }
1394aabd 3400 else if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x))
400fbf9f
JW
3401 {
3402 if (TREE_PUBLIC (x))
3403 {
3404 error ("address of global register variable `%s' requested",
3405 IDENTIFIER_POINTER (DECL_NAME (x)));
dffd7eb6 3406 return false;
400fbf9f 3407 }
bbbd6700
RK
3408
3409 /* If we are making this addressable due to its having
3410 volatile components, give a different error message. Also
3411 handle the case of an unnamed parameter by not trying
3412 to give the name. */
3413
3414 else if (C_TYPE_FIELDS_VOLATILE (TREE_TYPE (x)))
3415 {
3416 error ("cannot put object with volatile field into register");
dffd7eb6 3417 return false;
bbbd6700
RK
3418 }
3419
400fbf9f
JW
3420 pedwarn ("address of register variable `%s' requested",
3421 IDENTIFIER_POINTER (DECL_NAME (x)));
3422 }
f29a2bd1 3423 put_var_into_stack (x, /*rescan=*/true);
400fbf9f
JW
3424
3425 /* drops in */
3426 case FUNCTION_DECL:
3427 TREE_ADDRESSABLE (x) = 1;
d1bd0ded 3428 /* drops out */
400fbf9f 3429 default:
dffd7eb6 3430 return true;
400fbf9f
JW
3431 }
3432}
3433\f
3434/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
3435
3436tree
2f6e4e97 3437build_conditional_expr (tree ifexp, tree op1, tree op2)
400fbf9f 3438{
b3694847
SS
3439 tree type1;
3440 tree type2;
3441 enum tree_code code1;
3442 enum tree_code code2;
3443 tree result_type = NULL;
fd5d5b94 3444 tree orig_op1 = op1, orig_op2 = op2;
400fbf9f 3445
78ef5b89 3446 ifexp = c_common_truthvalue_conversion (default_conversion (ifexp));
400fbf9f 3447
e855c5ce 3448 /* Promote both alternatives. */
400fbf9f
JW
3449
3450 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
3451 op1 = default_conversion (op1);
3452 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
3453 op2 = default_conversion (op2);
3454
e855c5ce
RS
3455 if (TREE_CODE (ifexp) == ERROR_MARK
3456 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
3457 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
3458 return error_mark_node;
3459
400fbf9f
JW
3460 type1 = TREE_TYPE (op1);
3461 code1 = TREE_CODE (type1);
3462 type2 = TREE_TYPE (op2);
3463 code2 = TREE_CODE (type2);
2f6e4e97 3464
400fbf9f
JW
3465 /* Quickly detect the usual case where op1 and op2 have the same type
3466 after promotion. */
1ad409d2
RS
3467 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
3468 {
3469 if (type1 == type2)
3470 result_type = type1;
3471 else
3472 result_type = TYPE_MAIN_VARIANT (type1);
3473 }
f519a452
GDR
3474 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
3475 || code1 == COMPLEX_TYPE)
3476 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
3477 || code2 == COMPLEX_TYPE))
400fbf9f
JW
3478 {
3479 result_type = common_type (type1, type2);
cb3ca04e
ZW
3480
3481 /* If -Wsign-compare, warn here if type1 and type2 have
3482 different signedness. We'll promote the signed to unsigned
3483 and later code won't know it used to be different.
3484 Do this check on the original types, so that explicit casts
3485 will be considered, but default promotions won't. */
87f85ea0 3486 if (warn_sign_compare && !skip_evaluation)
cb3ca04e
ZW
3487 {
3488 int unsigned_op1 = TREE_UNSIGNED (TREE_TYPE (orig_op1));
3489 int unsigned_op2 = TREE_UNSIGNED (TREE_TYPE (orig_op2));
3490
3491 if (unsigned_op1 ^ unsigned_op2)
3492 {
3493 /* Do not warn if the result type is signed, since the
3494 signed type will only be chosen if it can represent
3495 all the values of the unsigned type. */
3496 if (! TREE_UNSIGNED (result_type))
3497 /* OK */;
3498 /* Do not warn if the signed quantity is an unsuffixed
3499 integer literal (or some static constant expression
3500 involving such literals) and it is non-negative. */
54c93c30
AE
3501 else if ((unsigned_op2 && c_tree_expr_nonnegative_p (op1))
3502 || (unsigned_op1 && c_tree_expr_nonnegative_p (op2)))
cb3ca04e
ZW
3503 /* OK */;
3504 else
3505 warning ("signed and unsigned type in conditional expression");
3506 }
3507 }
400fbf9f
JW
3508 }
3509 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
3510 {
3511 if (pedantic && (code1 != VOID_TYPE || code2 != VOID_TYPE))
89abf8d1 3512 pedwarn ("ISO C forbids conditional expr with only one void side");
400fbf9f
JW
3513 result_type = void_type_node;
3514 }
3515 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
3516 {
1074d9d4 3517 if (comp_target_types (type1, type2, 1))
400fbf9f 3518 result_type = common_type (type1, type2);
fd5d5b94
RS
3519 else if (integer_zerop (op1) && TREE_TYPE (type1) == void_type_node
3520 && TREE_CODE (orig_op1) != NOP_EXPR)
400fbf9f 3521 result_type = qualify_type (type2, type1);
fd5d5b94
RS
3522 else if (integer_zerop (op2) && TREE_TYPE (type2) == void_type_node
3523 && TREE_CODE (orig_op2) != NOP_EXPR)
400fbf9f 3524 result_type = qualify_type (type1, type2);
71653180 3525 else if (VOID_TYPE_P (TREE_TYPE (type1)))
400fbf9f
JW
3526 {
3527 if (pedantic && TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
89abf8d1 3528 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
1d7ff272
JM
3529 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
3530 TREE_TYPE (type2)));
400fbf9f 3531 }
71653180 3532 else if (VOID_TYPE_P (TREE_TYPE (type2)))
400fbf9f
JW
3533 {
3534 if (pedantic && TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
89abf8d1 3535 pedwarn ("ISO C forbids conditional expr between `void *' and function pointer");
1d7ff272
JM
3536 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
3537 TREE_TYPE (type1)));
400fbf9f
JW
3538 }
3539 else
3540 {
3541 pedwarn ("pointer type mismatch in conditional expression");
3542 result_type = build_pointer_type (void_type_node);
3543 }
3544 }
3545 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
3546 {
3547 if (! integer_zerop (op2))
3548 pedwarn ("pointer/integer type mismatch in conditional expression");
3549 else
3550 {
3551 op2 = null_pointer_node;
400fbf9f
JW
3552 }
3553 result_type = type1;
3554 }
3555 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
3556 {
3557 if (!integer_zerop (op1))
3558 pedwarn ("pointer/integer type mismatch in conditional expression");
3559 else
3560 {
3561 op1 = null_pointer_node;
400fbf9f
JW
3562 }
3563 result_type = type2;
3564 }
3565
3566 if (!result_type)
3567 {
3568 if (flag_cond_mismatch)
3569 result_type = void_type_node;
3570 else
3571 {
3572 error ("type mismatch in conditional expression");
3573 return error_mark_node;
3574 }
3575 }
3576
1dfdf85d
RS
3577 /* Merge const and volatile flags of the incoming types. */
3578 result_type
3579 = build_type_variant (result_type,
48c73063
RS
3580 TREE_READONLY (op1) || TREE_READONLY (op2),
3581 TREE_THIS_VOLATILE (op1) || TREE_THIS_VOLATILE (op2));
e58cd767 3582
400fbf9f 3583 if (result_type != TREE_TYPE (op1))
e58cd767 3584 op1 = convert_and_check (result_type, op1);
400fbf9f 3585 if (result_type != TREE_TYPE (op2))
e58cd767 3586 op2 = convert_and_check (result_type, op2);
2f6e4e97 3587
5abb45f2 3588 if (TREE_CODE (ifexp) == INTEGER_CST)
a29f2ec1 3589 return pedantic_non_lvalue (integer_zerop (ifexp) ? op2 : op1);
400fbf9f 3590
400fbf9f
JW
3591 return fold (build (COND_EXPR, result_type, ifexp, op1, op2));
3592}
3593\f
3594/* Given a list of expressions, return a compound expression
3595 that performs them all and returns the value of the last of them. */
3596
3597tree
2f6e4e97 3598build_compound_expr (tree list)
82bde854 3599{
43a5a542 3600 return internal_build_compound_expr (list, TRUE);
82bde854
MM
3601}
3602
3603static tree
2f6e4e97 3604internal_build_compound_expr (tree list, int first_p)
400fbf9f 3605{
b3694847 3606 tree rest;
400fbf9f
JW
3607
3608 if (TREE_CHAIN (list) == 0)
3609 {
207bf485
JM
3610 /* Convert arrays and functions to pointers when there
3611 really is a comma operator. */
3612 if (!first_p)
3613 TREE_VALUE (list)
3614 = default_function_array_conversion (TREE_VALUE (list));
714a0864 3615
439f6027 3616 /* Don't let (0, 0) be null pointer constant. */
82bde854 3617 if (!first_p && integer_zerop (TREE_VALUE (list)))
439f6027
RS
3618 return non_lvalue (TREE_VALUE (list));
3619 return TREE_VALUE (list);
400fbf9f
JW
3620 }
3621
82bde854 3622 rest = internal_build_compound_expr (TREE_CHAIN (list), FALSE);
400fbf9f 3623
0e7c47fa
RK
3624 if (! TREE_SIDE_EFFECTS (TREE_VALUE (list)))
3625 {
3626 /* The left-hand operand of a comma expression is like an expression
65ca2d60 3627 statement: with -Wextra or -Wunused, we should warn if it doesn't have
0e7c47fa 3628 any side-effects, unless it was explicitly cast to (void). */
87f85ea0 3629 if (warn_unused_value
0e7c47fa 3630 && ! (TREE_CODE (TREE_VALUE (list)) == CONVERT_EXPR
71653180 3631 && VOID_TYPE_P (TREE_TYPE (TREE_VALUE (list)))))
0e7c47fa
RK
3632 warning ("left-hand operand of comma expression has no effect");
3633
3634 /* When pedantic, a compound expression can be neither an lvalue
3635 nor an integer constant expression. */
3636 if (! pedantic)
3637 return rest;
3638 }
3639
3640 /* With -Wunused, we should also warn if the left-hand operand does have
3641 side-effects, but computes a value which is not used. For example, in
3642 `foo() + bar(), baz()' the result of the `+' operator is not used,
3643 so we should issue a warning. */
078721e1 3644 else if (warn_unused_value)
0e7c47fa 3645 warn_if_unused_value (TREE_VALUE (list));
400fbf9f
JW
3646
3647 return build (COMPOUND_EXPR, TREE_TYPE (rest), TREE_VALUE (list), rest);
3648}
3649
3650/* Build an expression representing a cast to type TYPE of expression EXPR. */
3651
3652tree
2f6e4e97 3653build_c_cast (tree type, tree expr)
400fbf9f 3654{
b3694847 3655 tree value = expr;
2f6e4e97 3656
400fbf9f
JW
3657 if (type == error_mark_node || expr == error_mark_node)
3658 return error_mark_node;
370ce32a
ZL
3659
3660 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
3661 only in <protocol> qualifications. But when constructing cast expressions,
3662 the protocols do matter and must be kept around. */
37fa72e9 3663 if (!c_dialect_objc () || !objc_is_id (type))
370ce32a 3664 type = TYPE_MAIN_VARIANT (type);
400fbf9f 3665
400fbf9f
JW
3666 if (TREE_CODE (type) == ARRAY_TYPE)
3667 {
3668 error ("cast specifies array type");
3669 return error_mark_node;
3670 }
3671
3672 if (TREE_CODE (type) == FUNCTION_TYPE)
3673 {
3674 error ("cast specifies function type");
3675 return error_mark_node;
3676 }
3677
f375b7a7 3678 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
400fbf9f
JW
3679 {
3680 if (pedantic)
3681 {
3682 if (TREE_CODE (type) == RECORD_TYPE
3683 || TREE_CODE (type) == UNION_TYPE)
89abf8d1 3684 pedwarn ("ISO C forbids casting nonscalar to the same type");
400fbf9f
JW
3685 }
3686 }
3687 else if (TREE_CODE (type) == UNION_TYPE)
3688 {
3689 tree field;
207bf485 3690 value = default_function_array_conversion (value);
0c16ddf7 3691
400fbf9f
JW
3692 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3693 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
d1bd0ded 3694 TYPE_MAIN_VARIANT (TREE_TYPE (value)), COMPARE_STRICT))
400fbf9f
JW
3695 break;
3696
3697 if (field)
3698 {
281ec92f 3699 tree t;
805f961c 3700
400fbf9f 3701 if (pedantic)
89abf8d1 3702 pedwarn ("ISO C forbids casts to union type");
dcf92453
ZW
3703 t = digest_init (type,
3704 build_constructor (type,
3705 build_tree_list (field, value)),
3706 0);
281ec92f
RS
3707 TREE_CONSTANT (t) = TREE_CONSTANT (value);
3708 return t;
400fbf9f
JW
3709 }
3710 error ("cast to union type from type not present in union");
3711 return error_mark_node;
3712 }
3713 else
3714 {
10d5caec 3715 tree otype, ovalue;
53b01f59
RS
3716
3717 /* If casting to void, avoid the error that would come
3718 from default_conversion in the case of a non-lvalue array. */
3719 if (type == void_type_node)
3720 return build1 (CONVERT_EXPR, type, value);
3721
400fbf9f
JW
3722 /* Convert functions and arrays to pointers,
3723 but don't convert any other types. */
207bf485 3724 value = default_function_array_conversion (value);
400fbf9f
JW
3725 otype = TREE_TYPE (value);
3726
d45cf215 3727 /* Optionally warn about potentially worrisome casts. */
400fbf9f
JW
3728
3729 if (warn_cast_qual
3730 && TREE_CODE (type) == POINTER_TYPE
3731 && TREE_CODE (otype) == POINTER_TYPE)
3732 {
f5963e61
JL
3733 tree in_type = type;
3734 tree in_otype = otype;
af702de8
RS
3735 int added = 0;
3736 int discarded = 0;
f5963e61 3737
cd6311ef
ZW
3738 /* Check that the qualifiers on IN_TYPE are a superset of
3739 the qualifiers of IN_OTYPE. The outermost level of
3740 POINTER_TYPE nodes is uninteresting and we stop as soon
3741 as we hit a non-POINTER_TYPE node on either type. */
3742 do
3743 {
3744 in_otype = TREE_TYPE (in_otype);
3745 in_type = TREE_TYPE (in_type);
af702de8
RS
3746
3747 /* GNU C allows cv-qualified function types. 'const'
3748 means the function is very pure, 'volatile' means it
3749 can't return. We need to warn when such qualifiers
3750 are added, not when they're taken away. */
3751 if (TREE_CODE (in_otype) == FUNCTION_TYPE
3752 && TREE_CODE (in_type) == FUNCTION_TYPE)
3753 added |= (TYPE_QUALS (in_type) & ~TYPE_QUALS (in_otype));
3754 else
3755 discarded |= (TYPE_QUALS (in_otype) & ~TYPE_QUALS (in_type));
cd6311ef
ZW
3756 }
3757 while (TREE_CODE (in_type) == POINTER_TYPE
3758 && TREE_CODE (in_otype) == POINTER_TYPE);
3759
af702de8
RS
3760 if (added)
3761 warning ("cast adds new qualifiers to function type");
3762
3763 if (discarded)
3932261a
MM
3764 /* There are qualifiers present in IN_OTYPE that are not
3765 present in IN_TYPE. */
6fffb55c 3766 warning ("cast discards qualifiers from pointer target type");
400fbf9f
JW
3767 }
3768
3769 /* Warn about possible alignment problems. */
d45cf215 3770 if (STRICT_ALIGNMENT && warn_cast_align
400fbf9f
JW
3771 && TREE_CODE (type) == POINTER_TYPE
3772 && TREE_CODE (otype) == POINTER_TYPE
3773 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
3774 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
ec9aa895
RK
3775 /* Don't warn about opaque types, where the actual alignment
3776 restriction is unknown. */
3777 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
3778 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
3779 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
400fbf9f
JW
3780 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
3781 warning ("cast increases required alignment of target type");
400fbf9f
JW
3782
3783 if (TREE_CODE (type) == INTEGER_TYPE
3784 && TREE_CODE (otype) == POINTER_TYPE
c9b7f31c
RS
3785 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
3786 && !TREE_CONSTANT (value))
400fbf9f
JW
3787 warning ("cast from pointer to integer of different size");
3788
796bb373
RK
3789 if (warn_bad_function_cast
3790 && TREE_CODE (value) == CALL_EXPR
3791 && TREE_CODE (type) != TREE_CODE (otype))
3792 warning ("cast does not match function type");
3793
400fbf9f
JW
3794 if (TREE_CODE (type) == POINTER_TYPE
3795 && TREE_CODE (otype) == INTEGER_TYPE
2918ed3c 3796 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
c9b7f31c
RS
3797 /* Don't warn about converting any constant. */
3798 && !TREE_CONSTANT (value))
400fbf9f
JW
3799 warning ("cast to pointer from integer of different size");
3800
73a73768
NS
3801 if (TREE_CODE (type) == POINTER_TYPE
3802 && TREE_CODE (otype) == POINTER_TYPE
3803 && TREE_CODE (expr) == ADDR_EXPR
3804 && DECL_P (TREE_OPERAND (expr, 0))
bf52f899 3805 && flag_strict_aliasing && warn_strict_aliasing
73a73768
NS
3806 && !VOID_TYPE_P (TREE_TYPE (type)))
3807 {
2f6e4e97 3808 /* Casting the address of a decl to non void pointer. Warn
73a73768
NS
3809 if the cast breaks type based aliasing. */
3810 if (!COMPLETE_TYPE_P (TREE_TYPE (type)))
bf52f899 3811 warning ("type-punning to incomplete type might break strict-aliasing rules");
73a73768
NS
3812 else if (!alias_sets_conflict_p
3813 (get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))),
3814 get_alias_set (TREE_TYPE (type))))
bf52f899 3815 warning ("dereferencing type-punned pointer will break strict-aliasing rules");
73a73768 3816 }
096daf41 3817
10d5caec 3818 ovalue = value;
096daf41
JH
3819 /* Replace a nonvolatile const static variable with its value. */
3820 if (optimize && TREE_CODE (value) == VAR_DECL)
3821 value = decl_constant_value (value);
400fbf9f 3822 value = convert (type, value);
e58cd767
RS
3823
3824 /* Ignore any integer overflow caused by the cast. */
3825 if (TREE_CODE (value) == INTEGER_CST)
10d5caec
PE
3826 {
3827 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
3828 TREE_CONSTANT_OVERFLOW (value) = TREE_CONSTANT_OVERFLOW (ovalue);
3829 }
400fbf9f
JW
3830 }
3831
684d9f3b 3832 /* Pedantically, don't let (void *) (FOO *) 0 be a null pointer constant. */
fd5d5b94
RS
3833 if (pedantic && TREE_CODE (value) == INTEGER_CST
3834 && TREE_CODE (expr) == INTEGER_CST
3835 && TREE_CODE (TREE_TYPE (expr)) != INTEGER_TYPE)
3836 value = non_lvalue (value);
3837
3838 /* If pedantic, don't let a cast be an lvalue. */
400fbf9f 3839 if (value == expr && pedantic)
fd5d5b94
RS
3840 value = non_lvalue (value);
3841
400fbf9f
JW
3842 return value;
3843}
15b732b2
NB
3844
3845/* Interpret a cast of expression EXPR to type TYPE. */
3846tree
2f6e4e97 3847c_cast_expr (tree type, tree expr)
15b732b2
NB
3848{
3849 int saved_wsp = warn_strict_prototypes;
3850
3851 /* This avoids warnings about unprototyped casts on
3852 integers. E.g. "#define SIG_DFL (void(*)())0". */
3853 if (TREE_CODE (expr) == INTEGER_CST)
3854 warn_strict_prototypes = 0;
3855 type = groktypename (type);
3856 warn_strict_prototypes = saved_wsp;
3857
3858 return build_c_cast (type, expr);
3859}
3860
400fbf9f
JW
3861\f
3862/* Build an assignment expression of lvalue LHS from value RHS.
3863 MODIFYCODE is the code for a binary operator that we use
3864 to combine the old value of LHS with RHS to get the new value.
3865 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment. */
3866
3867tree
2f6e4e97 3868build_modify_expr (tree lhs, enum tree_code modifycode, tree rhs)
400fbf9f 3869{
b3694847 3870 tree result;
400fbf9f
JW
3871 tree newrhs;
3872 tree lhstype = TREE_TYPE (lhs);
3873 tree olhstype = lhstype;
3874
3875 /* Types that aren't fully specified cannot be used in assignments. */
3876 lhs = require_complete_type (lhs);
3877
3878 /* Avoid duplicate error messages from operands that had errors. */
3879 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
3880 return error_mark_node;
3881
3882 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
3883 /* Do not use STRIP_NOPS here. We do not want an enumerator
3884 whose value is 0 to count as a null pointer constant. */
400fbf9f
JW
3885 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
3886 rhs = TREE_OPERAND (rhs, 0);
3887
3888 newrhs = rhs;
3889
3890 /* Handle control structure constructs used as "lvalues". */
3891
3892 switch (TREE_CODE (lhs))
3893 {
3894 /* Handle (a, b) used as an "lvalue". */
3895 case COMPOUND_EXPR:
3896 pedantic_lvalue_warning (COMPOUND_EXPR);
c5c76735 3897 newrhs = build_modify_expr (TREE_OPERAND (lhs, 1), modifycode, rhs);
19d76e60
RK
3898 if (TREE_CODE (newrhs) == ERROR_MARK)
3899 return error_mark_node;
400fbf9f 3900 return build (COMPOUND_EXPR, lhstype,
19d76e60 3901 TREE_OPERAND (lhs, 0), newrhs);
2f6e4e97 3902
400fbf9f
JW
3903 /* Handle (a ? b : c) used as an "lvalue". */
3904 case COND_EXPR:
3905 pedantic_lvalue_warning (COND_EXPR);
3906 rhs = save_expr (rhs);
3907 {
3908 /* Produce (a ? (b = rhs) : (c = rhs))
3909 except that the RHS goes through a save-expr
3910 so the code to compute it is only emitted once. */
3911 tree cond
3912 = build_conditional_expr (TREE_OPERAND (lhs, 0),
3913 build_modify_expr (TREE_OPERAND (lhs, 1),
3914 modifycode, rhs),
3915 build_modify_expr (TREE_OPERAND (lhs, 2),
3916 modifycode, rhs));
19d76e60
RK
3917 if (TREE_CODE (cond) == ERROR_MARK)
3918 return cond;
400fbf9f
JW
3919 /* Make sure the code to compute the rhs comes out
3920 before the split. */
3921 return build (COMPOUND_EXPR, TREE_TYPE (lhs),
3922 /* But cast it to void to avoid an "unused" error. */
3923 convert (void_type_node, rhs), cond);
3924 }
e9a25f70
JL
3925 default:
3926 break;
400fbf9f
JW
3927 }
3928
3929 /* If a binary op has been requested, combine the old LHS value with the RHS
3930 producing the value we should actually store into the LHS. */
3931
3932 if (modifycode != NOP_EXPR)
3933 {
3934 lhs = stabilize_reference (lhs);
3935 newrhs = build_binary_op (modifycode, lhs, rhs, 1);
3936 }
3937
3938 /* Handle a cast used as an "lvalue".
3939 We have already performed any binary operator using the value as cast.
3940 Now convert the result to the cast type of the lhs,
3941 and then true type of the lhs and store it there;
3942 then convert result back to the cast type to be the value
3943 of the assignment. */
3944
3945 switch (TREE_CODE (lhs))
3946 {
3947 case NOP_EXPR:
3948 case CONVERT_EXPR:
3949 case FLOAT_EXPR:
3950 case FIX_TRUNC_EXPR:
3951 case FIX_FLOOR_EXPR:
3952 case FIX_ROUND_EXPR:
3953 case FIX_CEIL_EXPR:
207bf485 3954 newrhs = default_function_array_conversion (newrhs);
400fbf9f
JW
3955 {
3956 tree inner_lhs = TREE_OPERAND (lhs, 0);
3957 tree result;
3958 result = build_modify_expr (inner_lhs, NOP_EXPR,
3959 convert (TREE_TYPE (inner_lhs),
3960 convert (lhstype, newrhs)));
19d76e60
RK
3961 if (TREE_CODE (result) == ERROR_MARK)
3962 return result;
400fbf9f
JW
3963 pedantic_lvalue_warning (CONVERT_EXPR);
3964 return convert (TREE_TYPE (lhs), result);
3965 }
2f6e4e97 3966
e9a25f70
JL
3967 default:
3968 break;
400fbf9f
JW
3969 }
3970
3971 /* Now we have handled acceptable kinds of LHS that are not truly lvalues.
3972 Reject anything strange now. */
3973
ab87f8c8 3974 if (!lvalue_or_else (lhs, "invalid lvalue in assignment"))
400fbf9f
JW
3975 return error_mark_node;
3976
3977 /* Warn about storing in something that is `const'. */
3978
3979 if (TREE_READONLY (lhs) || TYPE_READONLY (lhstype)
3980 || ((TREE_CODE (lhstype) == RECORD_TYPE
3981 || TREE_CODE (lhstype) == UNION_TYPE)
3982 && C_TYPE_FIELDS_READONLY (lhstype)))
3983 readonly_warning (lhs, "assignment");
3984
3985 /* If storing into a structure or union member,
3986 it has probably been given type `int'.
3987 Compute the type that would go with
3988 the actual amount of storage the member occupies. */
3989
3990 if (TREE_CODE (lhs) == COMPONENT_REF
3991 && (TREE_CODE (lhstype) == INTEGER_TYPE
19552aa5 3992 || TREE_CODE (lhstype) == BOOLEAN_TYPE
400fbf9f
JW
3993 || TREE_CODE (lhstype) == REAL_TYPE
3994 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
3995 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
3996
3997 /* If storing in a field that is in actuality a short or narrower than one,
3998 we must store in the field in its actual type. */
3999
4000 if (lhstype != TREE_TYPE (lhs))
4001 {
4002 lhs = copy_node (lhs);
4003 TREE_TYPE (lhs) = lhstype;
4004 }
4005
4006 /* Convert new value to destination type. */
4007
ab87f8c8 4008 newrhs = convert_for_assignment (lhstype, newrhs, _("assignment"),
9b7267b8 4009 NULL_TREE, NULL_TREE, 0);
400fbf9f
JW
4010 if (TREE_CODE (newrhs) == ERROR_MARK)
4011 return error_mark_node;
4012
bb58bec5
JM
4013 /* Scan operands */
4014
400fbf9f
JW
4015 result = build (MODIFY_EXPR, lhstype, lhs, newrhs);
4016 TREE_SIDE_EFFECTS (result) = 1;
4017
4018 /* If we got the LHS in a different type for storing in,
4019 convert the result back to the nominal type of LHS
4020 so that the value we return always has the same type
4021 as the LHS argument. */
4022
4023 if (olhstype == TREE_TYPE (result))
4024 return result;
ab87f8c8 4025 return convert_for_assignment (olhstype, result, _("assignment"),
9b7267b8 4026 NULL_TREE, NULL_TREE, 0);
400fbf9f
JW
4027}
4028\f
4029/* Convert value RHS to type TYPE as preparation for an assignment
4030 to an lvalue of type TYPE.
4031 The real work of conversion is done by `convert'.
4032 The purpose of this function is to generate error messages
4033 for assignments that are not allowed in C.
4034 ERRTYPE is a string to use in error messages:
4035 "assignment", "return", etc. If it is null, this is parameter passing
ab87f8c8 4036 for a function call (and different error messages are output).
400fbf9f
JW
4037
4038 FUNNAME is the name of the function being called,
4039 as an IDENTIFIER_NODE, or null.
4040 PARMNUM is the number of the argument, for printing in error messages. */
4041
4042static tree
f55ade6e
AJ
4043convert_for_assignment (tree type, tree rhs, const char *errtype,
4044 tree fundecl, tree funname, int parmnum)
400fbf9f 4045{
b3694847
SS
4046 enum tree_code codel = TREE_CODE (type);
4047 tree rhstype;
4048 enum tree_code coder;
400fbf9f
JW
4049
4050 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
4051 /* Do not use STRIP_NOPS here. We do not want an enumerator
4052 whose value is 0 to count as a null pointer constant. */
400fbf9f
JW
4053 if (TREE_CODE (rhs) == NON_LVALUE_EXPR)
4054 rhs = TREE_OPERAND (rhs, 0);
4055
4056 if (TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
4057 || TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE)
4058 rhs = default_conversion (rhs);
8c3a6477 4059 else if (optimize && TREE_CODE (rhs) == VAR_DECL)
2f74f7e9 4060 rhs = decl_constant_value_for_broken_optimization (rhs);
400fbf9f
JW
4061
4062 rhstype = TREE_TYPE (rhs);
4063 coder = TREE_CODE (rhstype);
4064
4065 if (coder == ERROR_MARK)
4066 return error_mark_node;
4067
4068 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
e58cd767
RS
4069 {
4070 overflow_warning (rhs);
1074d9d4
NP
4071 /* Check for Objective-C protocols. This will automatically
4072 issue a warning if there are protocol violations. No need to
4073 use the return value. */
37fa72e9 4074 if (c_dialect_objc ())
0f7866e7 4075 objc_comptypes (type, rhstype, 0);
e58cd767
RS
4076 return rhs;
4077 }
400fbf9f
JW
4078
4079 if (coder == VOID_TYPE)
4080 {
4081 error ("void value not ignored as it ought to be");
4082 return error_mark_node;
4083 }
2f6e4e97 4084 /* A type converts to a reference to it.
9f720c3e
GK
4085 This code doesn't fully support references, it's just for the
4086 special case of va_start and va_copy. */
4087 if (codel == REFERENCE_TYPE
d1bd0ded 4088 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs), COMPARE_STRICT) == 1)
9f720c3e 4089 {
a1652cee
JM
4090 if (!lvalue_p (rhs))
4091 {
4092 error ("cannot pass rvalue to reference parameter");
4093 return error_mark_node;
4094 }
dffd7eb6 4095 if (!c_mark_addressable (rhs))
9f720c3e
GK
4096 return error_mark_node;
4097 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
4098
4099 /* We already know that these two types are compatible, but they
4100 may not be exactly identical. In fact, `TREE_TYPE (type)' is
4101 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
4102 likely to be va_list, a typedef to __builtin_va_list, which
4103 is different enough that it will cause problems later. */
4104 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
4105 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
4106
4107 rhs = build1 (NOP_EXPR, type, rhs);
4108 return rhs;
4109 }
62e1dfcf
NC
4110 /* Some types can interconvert without explicit casts. */
4111 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
c8e4f0e9
AH
4112 && ((*targetm.vector_opaque_p) (type)
4113 || (*targetm.vector_opaque_p) (rhstype)))
62e1dfcf 4114 return convert (type, rhs);
400fbf9f 4115 /* Arithmetic types all interconvert, and enum is treated like int. */
2f6e4e97 4116 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
19552aa5
JM
4117 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
4118 || codel == BOOLEAN_TYPE)
cb2a532e 4119 && (coder == INTEGER_TYPE || coder == REAL_TYPE
19552aa5
JM
4120 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
4121 || coder == BOOLEAN_TYPE))
da3c6115 4122 return convert_and_check (type, rhs);
61179109 4123
7e842ef8
PE
4124 /* Conversion to a transparent union from its member types.
4125 This applies only to function arguments. */
4126 else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
4127 {
4128 tree memb_types;
4129 tree marginal_memb_type = 0;
4130
4131 for (memb_types = TYPE_FIELDS (type); memb_types;
4132 memb_types = TREE_CHAIN (memb_types))
4133 {
4134 tree memb_type = TREE_TYPE (memb_types);
4135
4136 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
d1bd0ded 4137 TYPE_MAIN_VARIANT (rhstype), COMPARE_STRICT))
7e842ef8
PE
4138 break;
4139
4140 if (TREE_CODE (memb_type) != POINTER_TYPE)
4141 continue;
4142
4143 if (coder == POINTER_TYPE)
4144 {
b3694847
SS
4145 tree ttl = TREE_TYPE (memb_type);
4146 tree ttr = TREE_TYPE (rhstype);
7e842ef8
PE
4147
4148 /* Any non-function converts to a [const][volatile] void *
4149 and vice versa; otherwise, targets must be the same.
4150 Meanwhile, the lhs target must have all the qualifiers of
4151 the rhs. */
71653180 4152 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
1074d9d4 4153 || comp_target_types (memb_type, rhstype, 0))
7e842ef8
PE
4154 {
4155 /* If this type won't generate any warnings, use it. */
3932261a
MM
4156 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
4157 || ((TREE_CODE (ttr) == FUNCTION_TYPE
4158 && TREE_CODE (ttl) == FUNCTION_TYPE)
4159 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
4160 == TYPE_QUALS (ttr))
b58c9a79 4161 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
3932261a 4162 == TYPE_QUALS (ttl))))
7e842ef8
PE
4163 break;
4164
4165 /* Keep looking for a better type, but remember this one. */
4166 if (! marginal_memb_type)
4167 marginal_memb_type = memb_type;
4168 }
4169 }
4170
4171 /* Can convert integer zero to any pointer type. */
4172 if (integer_zerop (rhs)
4173 || (TREE_CODE (rhs) == NOP_EXPR
4174 && integer_zerop (TREE_OPERAND (rhs, 0))))
4175 {
4176 rhs = null_pointer_node;
4177 break;
4178 }
4179 }
4180
4181 if (memb_types || marginal_memb_type)
4182 {
4183 if (! memb_types)
4184 {
4185 /* We have only a marginally acceptable member type;
0f41302f 4186 it needs a warning. */
b3694847
SS
4187 tree ttl = TREE_TYPE (marginal_memb_type);
4188 tree ttr = TREE_TYPE (rhstype);
7e842ef8
PE
4189
4190 /* Const and volatile mean something different for function
4191 types, so the usual warnings are not appropriate. */
4192 if (TREE_CODE (ttr) == FUNCTION_TYPE
4193 && TREE_CODE (ttl) == FUNCTION_TYPE)
4194 {
4195 /* Because const and volatile on functions are
4196 restrictions that say the function will not do
4197 certain things, it is okay to use a const or volatile
4198 function where an ordinary one is wanted, but not
4199 vice-versa. */
3932261a
MM
4200 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4201 warn_for_assignment ("%s makes qualified function pointer from unqualified",
ab87f8c8 4202 errtype, funname, parmnum);
7e842ef8 4203 }
3932261a
MM
4204 else if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4205 warn_for_assignment ("%s discards qualifiers from pointer target type",
ab87f8c8 4206 errtype, funname,
3932261a 4207 parmnum);
7e842ef8 4208 }
2f6e4e97 4209
7e842ef8 4210 if (pedantic && ! DECL_IN_SYSTEM_HEADER (fundecl))
89abf8d1 4211 pedwarn ("ISO C prohibits argument conversion to union type");
7e842ef8
PE
4212
4213 return build1 (NOP_EXPR, type, rhs);
4214 }
4215 }
4216
400fbf9f 4217 /* Conversions among pointers */
98d64f69 4218 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
a1652cee 4219 && (coder == codel))
400fbf9f 4220 {
b3694847
SS
4221 tree ttl = TREE_TYPE (type);
4222 tree ttr = TREE_TYPE (rhstype);
6035d635
AH
4223 bool is_opaque_pointer;
4224
4225 /* Opaque pointers are treated like void pointers. */
4226 is_opaque_pointer = ((*targetm.vector_opaque_p) (type)
4227 || (*targetm.vector_opaque_p) (rhstype))
4228 && TREE_CODE (ttl) == VECTOR_TYPE
4229 && TREE_CODE (ttr) == VECTOR_TYPE;
400fbf9f
JW
4230
4231 /* Any non-function converts to a [const][volatile] void *
4232 and vice versa; otherwise, targets must be the same.
4233 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
71653180 4234 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
1074d9d4 4235 || comp_target_types (type, rhstype, 0)
6035d635 4236 || is_opaque_pointer
ceef8ce4
NB
4237 || (c_common_unsigned_type (TYPE_MAIN_VARIANT (ttl))
4238 == c_common_unsigned_type (TYPE_MAIN_VARIANT (ttr))))
400fbf9f
JW
4239 {
4240 if (pedantic
71653180 4241 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
400fbf9f 4242 ||
71653180 4243 (VOID_TYPE_P (ttr)
fd5d5b94
RS
4244 /* Check TREE_CODE to catch cases like (void *) (char *) 0
4245 which are not ANSI null ptr constants. */
4246 && (!integer_zerop (rhs) || TREE_CODE (rhs) == NOP_EXPR)
400fbf9f 4247 && TREE_CODE (ttl) == FUNCTION_TYPE)))
89abf8d1 4248 warn_for_assignment ("ISO C forbids %s between function pointer and `void *'",
ab87f8c8 4249 errtype, funname, parmnum);
400fbf9f
JW
4250 /* Const and volatile mean something different for function types,
4251 so the usual warnings are not appropriate. */
4252 else if (TREE_CODE (ttr) != FUNCTION_TYPE
caf2e8e4 4253 && TREE_CODE (ttl) != FUNCTION_TYPE)
400fbf9f 4254 {
3932261a
MM
4255 if (TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl))
4256 warn_for_assignment ("%s discards qualifiers from pointer target type",
ab87f8c8 4257 errtype, funname, parmnum);
790e9490
RS
4258 /* If this is not a case of ignoring a mismatch in signedness,
4259 no warning. */
71653180 4260 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
1074d9d4 4261 || comp_target_types (type, rhstype, 0))
790e9490
RS
4262 ;
4263 /* If there is a mismatch, do warn. */
4264 else if (pedantic)
4265 warn_for_assignment ("pointer targets in %s differ in signedness",
ab87f8c8 4266 errtype, funname, parmnum);
400fbf9f 4267 }
d949d5df
RK
4268 else if (TREE_CODE (ttl) == FUNCTION_TYPE
4269 && TREE_CODE (ttr) == FUNCTION_TYPE)
400fbf9f
JW
4270 {
4271 /* Because const and volatile on functions are restrictions
4272 that say the function will not do certain things,
4273 it is okay to use a const or volatile function
4274 where an ordinary one is wanted, but not vice-versa. */
3932261a
MM
4275 if (TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr))
4276 warn_for_assignment ("%s makes qualified function pointer from unqualified",
ab87f8c8 4277 errtype, funname, parmnum);
400fbf9f
JW
4278 }
4279 }
400fbf9f
JW
4280 else
4281 warn_for_assignment ("%s from incompatible pointer type",
ab87f8c8 4282 errtype, funname, parmnum);
400fbf9f
JW
4283 return convert (type, rhs);
4284 }
4285 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
4286 {
2918ed3c 4287 /* An explicit constant 0 can convert to a pointer,
f1a2b955
RS
4288 or one that results from arithmetic, even including
4289 a cast to integer type. */
4290 if (! (TREE_CODE (rhs) == INTEGER_CST && integer_zerop (rhs))
4291 &&
4292 ! (TREE_CODE (rhs) == NOP_EXPR
4293 && TREE_CODE (TREE_TYPE (rhs)) == INTEGER_TYPE
4294 && TREE_CODE (TREE_OPERAND (rhs, 0)) == INTEGER_CST
4295 && integer_zerop (TREE_OPERAND (rhs, 0))))
400fbf9f
JW
4296 {
4297 warn_for_assignment ("%s makes pointer from integer without a cast",
ab87f8c8 4298 errtype, funname, parmnum);
400fbf9f
JW
4299 return convert (type, rhs);
4300 }
4301 return null_pointer_node;
4302 }
4303 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
4304 {
4305 warn_for_assignment ("%s makes integer from pointer without a cast",
ab87f8c8 4306 errtype, funname, parmnum);
400fbf9f
JW
4307 return convert (type, rhs);
4308 }
19552aa5
JM
4309 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
4310 return convert (type, rhs);
400fbf9f
JW
4311
4312 if (!errtype)
4313 {
4314 if (funname)
2f6e4e97
AJ
4315 {
4316 tree selector = objc_message_selector ();
4317
4318 if (selector && parmnum > 2)
4319 error ("incompatible type for argument %d of `%s'",
8b40563c 4320 parmnum - 2, IDENTIFIER_POINTER (selector));
2f6e4e97 4321 else
8b40563c
TW
4322 error ("incompatible type for argument %d of `%s'",
4323 parmnum, IDENTIFIER_POINTER (funname));
4324 }
400fbf9f
JW
4325 else
4326 error ("incompatible type for argument %d of indirect function call",
4327 parmnum);
4328 }
4329 else
ab87f8c8 4330 error ("incompatible types in %s", errtype);
400fbf9f
JW
4331
4332 return error_mark_node;
4333}
4334
f735a153
JJ
4335/* Convert VALUE for assignment into inlined parameter PARM. */
4336
4337tree
2f6e4e97 4338c_convert_parm_for_inlining (tree parm, tree value, tree fn)
f735a153
JJ
4339{
4340 tree ret, type;
4341
4342 /* If FN was prototyped, the value has been converted already
4343 in convert_arguments. */
4344 if (! value || TYPE_ARG_TYPES (TREE_TYPE (fn)))
4345 return value;
4346
4347 type = TREE_TYPE (parm);
2f6e4e97 4348 ret = convert_for_assignment (type, value,
f735a153
JJ
4349 (char *) 0 /* arg passing */, fn,
4350 DECL_NAME (fn), 0);
4351 if (PROMOTE_PROTOTYPES
4352 && INTEGRAL_TYPE_P (type)
4353 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
4354 ret = default_conversion (ret);
4355 return ret;
4356}
4357
ab87f8c8 4358/* Print a warning using MSGID.
400fbf9f 4359 It gets OPNAME as its one parameter.
94c1e0e8
MS
4360 if OPNAME is null and ARGNUM is 0, it is replaced by "passing arg of `FUNCTION'".
4361 Otherwise if OPNAME is null, it is replaced by "passing arg ARGNUM of `FUNCTION'".
400fbf9f
JW
4362 FUNCTION and ARGNUM are handled specially if we are building an
4363 Objective-C selector. */
4364
4365static void
f55ade6e
AJ
4366warn_for_assignment (const char *msgid, const char *opname, tree function,
4367 int argnum)
400fbf9f 4368{
400fbf9f
JW
4369 if (opname == 0)
4370 {
0f7866e7 4371 tree selector = objc_message_selector ();
5d5993dd 4372 char * new_opname;
2f6e4e97 4373
400fbf9f
JW
4374 if (selector && argnum > 2)
4375 {
4376 function = selector;
4377 argnum -= 2;
4378 }
94c1e0e8
MS
4379 if (argnum == 0)
4380 {
4381 if (function)
2f6e4e97 4382 {
94c1e0e8
MS
4383 /* Function name is known; supply it. */
4384 const char *const argstring = _("passing arg of `%s'");
4385 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4386 + strlen (argstring) + 1
4387 + 1);
4388 sprintf (new_opname, argstring,
4389 IDENTIFIER_POINTER (function));
4390 }
4391 else
4392 {
4393 /* Function name unknown (call through ptr). */
4394 const char *const argnofun = _("passing arg of pointer to function");
4395 new_opname = (char *) alloca (strlen (argnofun) + 1 + 1);
4396 sprintf (new_opname, argnofun);
4397 }
4398 }
4399 else if (function)
400fbf9f
JW
4400 {
4401 /* Function name is known; supply it. */
27c38fbe 4402 const char *const argstring = _("passing arg %d of `%s'");
5d5993dd
KG
4403 new_opname = (char *) alloca (IDENTIFIER_LENGTH (function)
4404 + strlen (argstring) + 1 + 25
4405 /*%d*/ + 1);
4406 sprintf (new_opname, argstring, argnum,
4407 IDENTIFIER_POINTER (function));
400fbf9f
JW
4408 }
4409 else
4410 {
3ef42a0c 4411 /* Function name unknown (call through ptr); just give arg number. */
27c38fbe 4412 const char *const argnofun = _("passing arg %d of pointer to function");
5d5993dd
KG
4413 new_opname = (char *) alloca (strlen (argnofun) + 1 + 25 /*%d*/ + 1);
4414 sprintf (new_opname, argnofun, argnum);
400fbf9f 4415 }
5d5993dd 4416 opname = new_opname;
400fbf9f 4417 }
ab87f8c8 4418 pedwarn (msgid, opname);
400fbf9f
JW
4419}
4420\f
d9fc6069
JW
4421/* If VALUE is a compound expr all of whose expressions are constant, then
4422 return its value. Otherwise, return error_mark_node.
4423
4424 This is for handling COMPOUND_EXPRs as initializer elements
4425 which is allowed with a warning when -pedantic is specified. */
4426
4427static tree
2f6e4e97 4428valid_compound_expr_initializer (tree value, tree endtype)
d9fc6069
JW
4429{
4430 if (TREE_CODE (value) == COMPOUND_EXPR)
4431 {
4432 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
4433 == error_mark_node)
4434 return error_mark_node;
4435 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
4436 endtype);
4437 }
4438 else if (! TREE_CONSTANT (value)
4439 && ! initializer_constant_valid_p (value, endtype))
4440 return error_mark_node;
4441 else
4442 return value;
4443}
400fbf9f
JW
4444\f
4445/* Perform appropriate conversions on the initial value of a variable,
4446 store it in the declaration DECL,
4447 and print any error messages that are appropriate.
4448 If the init is invalid, store an ERROR_MARK. */
4449
4450void
2f6e4e97 4451store_init_value (tree decl, tree init)
400fbf9f 4452{
b3694847 4453 tree value, type;
400fbf9f
JW
4454
4455 /* If variable's type was invalidly declared, just ignore it. */
4456
4457 type = TREE_TYPE (decl);
4458 if (TREE_CODE (type) == ERROR_MARK)
4459 return;
4460
4461 /* Digest the specified initializer into an expression. */
4462
273cf2e4 4463 value = digest_init (type, init, TREE_STATIC (decl));
400fbf9f
JW
4464
4465 /* Store the expression if valid; else report error. */
4466
cde6e684 4467 if (warn_traditional && !in_system_header
895ea614
KG
4468 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && ! TREE_STATIC (decl))
4469 warning ("traditional C rejects automatic aggregate initialization");
4470
10d5caec
PE
4471 DECL_INITIAL (decl) = value;
4472
26b3c423 4473 /* ANSI wants warnings about out-of-range constant initializers. */
10d5caec 4474 STRIP_TYPE_NOPS (value);
26b3c423 4475 constant_expression_warning (value);
ad47f1e5
JJ
4476
4477 /* Check if we need to set array size from compound literal size. */
4478 if (TREE_CODE (type) == ARRAY_TYPE
4479 && TYPE_DOMAIN (type) == 0
4480 && value != error_mark_node)
4481 {
4482 tree inside_init = init;
4483
4484 if (TREE_CODE (init) == NON_LVALUE_EXPR)
4485 inside_init = TREE_OPERAND (init, 0);
4486 inside_init = fold (inside_init);
4487
4488 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4489 {
4490 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4491
4492 if (TYPE_DOMAIN (TREE_TYPE (decl)))
4493 {
4494 /* For int foo[] = (int [3]){1}; we need to set array size
4495 now since later on array initializer will be just the
4496 brace enclosed list of the compound literal. */
4497 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (decl));
4498 layout_type (type);
4499 layout_decl (decl, 0);
4500 }
4501 }
4502 }
400fbf9f
JW
4503}
4504\f
075fc632 4505/* Methods for storing and printing names for error messages. */
d45cf215
RS
4506
4507/* Implement a spelling stack that allows components of a name to be pushed
4508 and popped. Each element on the stack is this structure. */
4509
4510struct spelling
4511{
4512 int kind;
4513 union
4514 {
4515 int i;
5d5993dd 4516 const char *s;
d45cf215
RS
4517 } u;
4518};
4519
4520#define SPELLING_STRING 1
4521#define SPELLING_MEMBER 2
4522#define SPELLING_BOUNDS 3
4523
4524static struct spelling *spelling; /* Next stack element (unused). */
4525static struct spelling *spelling_base; /* Spelling stack base. */
4526static int spelling_size; /* Size of the spelling stack. */
4527
4528/* Macros to save and restore the spelling stack around push_... functions.
4529 Alternative to SAVE_SPELLING_STACK. */
4530
4531#define SPELLING_DEPTH() (spelling - spelling_base)
0f1e8126 4532#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
d45cf215 4533
d45cf215
RS
4534/* Push an element on the spelling stack with type KIND and assign VALUE
4535 to MEMBER. */
4536
4537#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
4538{ \
4539 int depth = SPELLING_DEPTH (); \
4540 \
4541 if (depth >= spelling_size) \
4542 { \
4543 spelling_size += 10; \
4544 if (spelling_base == 0) \
4545 spelling_base \
4546 = (struct spelling *) xmalloc (spelling_size * sizeof (struct spelling)); \
4547 else \
4548 spelling_base \
4549 = (struct spelling *) xrealloc (spelling_base, \
4550 spelling_size * sizeof (struct spelling)); \
4551 RESTORE_SPELLING_DEPTH (depth); \
4552 } \
4553 \
4554 spelling->kind = (KIND); \
4555 spelling->MEMBER = (VALUE); \
4556 spelling++; \
4557}
4558
4559/* Push STRING on the stack. Printed literally. */
4560
4561static void
2f6e4e97 4562push_string (const char *string)
d45cf215
RS
4563{
4564 PUSH_SPELLING (SPELLING_STRING, string, u.s);
4565}
4566
4567/* Push a member name on the stack. Printed as '.' STRING. */
4568
4569static void
2f6e4e97 4570push_member_name (tree decl)
d45cf215 4571{
83182544 4572 const char *const string
19d76e60 4573 = DECL_NAME (decl) ? IDENTIFIER_POINTER (DECL_NAME (decl)) : "<anonymous>";
d45cf215
RS
4574 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
4575}
4576
4577/* Push an array bounds on the stack. Printed as [BOUNDS]. */
4578
4579static void
2f6e4e97 4580push_array_bounds (int bounds)
d45cf215
RS
4581{
4582 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
4583}
4584
4585/* Compute the maximum size in bytes of the printed spelling. */
4586
4587static int
2f6e4e97 4588spelling_length (void)
d45cf215 4589{
b3694847
SS
4590 int size = 0;
4591 struct spelling *p;
d45cf215
RS
4592
4593 for (p = spelling_base; p < spelling; p++)
4594 {
4595 if (p->kind == SPELLING_BOUNDS)
4596 size += 25;
4597 else
4598 size += strlen (p->u.s) + 1;
4599 }
4600
4601 return size;
4602}
4603
4604/* Print the spelling to BUFFER and return it. */
4605
4606static char *
2f6e4e97 4607print_spelling (char *buffer)
d45cf215 4608{
b3694847
SS
4609 char *d = buffer;
4610 struct spelling *p;
d45cf215
RS
4611
4612 for (p = spelling_base; p < spelling; p++)
4613 if (p->kind == SPELLING_BOUNDS)
4614 {
4615 sprintf (d, "[%d]", p->u.i);
4616 d += strlen (d);
4617 }
4618 else
4619 {
b3694847 4620 const char *s;
d45cf215
RS
4621 if (p->kind == SPELLING_MEMBER)
4622 *d++ = '.';
1d300e19 4623 for (s = p->u.s; (*d = *s++); d++)
d45cf215
RS
4624 ;
4625 }
4626 *d++ = '\0';
4627 return buffer;
4628}
4629
400fbf9f 4630/* Issue an error message for a bad initializer component.
ab87f8c8
JL
4631 MSGID identifies the message.
4632 The component name is taken from the spelling stack. */
400fbf9f
JW
4633
4634void
2f6e4e97 4635error_init (const char *msgid)
400fbf9f 4636{
ab87f8c8 4637 char *ofwhat;
400fbf9f 4638
a094954e 4639 error ("%s", _(msgid));
ab87f8c8 4640 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
400fbf9f 4641 if (*ofwhat)
ab87f8c8 4642 error ("(near initialization for `%s')", ofwhat);
400fbf9f
JW
4643}
4644
4645/* Issue a pedantic warning for a bad initializer component.
ab87f8c8
JL
4646 MSGID identifies the message.
4647 The component name is taken from the spelling stack. */
400fbf9f
JW
4648
4649void
2f6e4e97 4650pedwarn_init (const char *msgid)
400fbf9f 4651{
ab87f8c8 4652 char *ofwhat;
400fbf9f 4653
a094954e 4654 pedwarn ("%s", _(msgid));
ab87f8c8 4655 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
400fbf9f 4656 if (*ofwhat)
ab87f8c8 4657 pedwarn ("(near initialization for `%s')", ofwhat);
400fbf9f 4658}
b71c7f8a
RK
4659
4660/* Issue a warning for a bad initializer component.
ab87f8c8
JL
4661 MSGID identifies the message.
4662 The component name is taken from the spelling stack. */
b71c7f8a
RK
4663
4664static void
2f6e4e97 4665warning_init (const char *msgid)
b71c7f8a 4666{
ab87f8c8 4667 char *ofwhat;
b71c7f8a 4668
a094954e 4669 warning ("%s", _(msgid));
ab87f8c8 4670 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
b71c7f8a 4671 if (*ofwhat)
ab87f8c8 4672 warning ("(near initialization for `%s')", ofwhat);
b71c7f8a 4673}
400fbf9f
JW
4674\f
4675/* Digest the parser output INIT as an initializer for type TYPE.
4676 Return a C expression of type TYPE to represent the initial value.
4677
273cf2e4
ZW
4678 REQUIRE_CONSTANT requests an error if non-constant initializers or
4679 elements are seen. */
400fbf9f 4680
b62acd60 4681static tree
2f6e4e97 4682digest_init (tree type, tree init, int require_constant)
400fbf9f
JW
4683{
4684 enum tree_code code = TREE_CODE (type);
047de90b 4685 tree inside_init = init;
400fbf9f 4686
de9a3171
DA
4687 if (type == error_mark_node
4688 || init == error_mark_node
822baa84 4689 || TREE_TYPE (init) == error_mark_node)
21a427cc 4690 return error_mark_node;
400fbf9f
JW
4691
4692 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
fc76e425
RS
4693 /* Do not use STRIP_NOPS here. We do not want an enumerator
4694 whose value is 0 to count as a null pointer constant. */
400fbf9f 4695 if (TREE_CODE (init) == NON_LVALUE_EXPR)
047de90b 4696 inside_init = TREE_OPERAND (init, 0);
400fbf9f 4697
b13aca19
AM
4698 inside_init = fold (inside_init);
4699
400fbf9f
JW
4700 /* Initialization of an array of chars from a string constant
4701 optionally enclosed in braces. */
4702
4703 if (code == ARRAY_TYPE)
4704 {
4705 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
4706 if ((typ1 == char_type_node
4707 || typ1 == signed_char_type_node
4708 || typ1 == unsigned_char_type_node
4709 || typ1 == unsigned_wchar_type_node
4710 || typ1 == signed_wchar_type_node)
fd5d5b94 4711 && ((inside_init && TREE_CODE (inside_init) == STRING_CST)))
400fbf9f 4712 {
4d65300e 4713 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
d1bd0ded 4714 TYPE_MAIN_VARIANT (type), COMPARE_STRICT))
fd5d5b94 4715 return inside_init;
d11fdb45 4716
fd5d5b94 4717 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
400fbf9f
JW
4718 != char_type_node)
4719 && TYPE_PRECISION (typ1) == TYPE_PRECISION (char_type_node))
4720 {
ab87f8c8 4721 error_init ("char-array initialized from wide string");
400fbf9f
JW
4722 return error_mark_node;
4723 }
fd5d5b94 4724 if ((TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)))
400fbf9f
JW
4725 == char_type_node)
4726 && TYPE_PRECISION (typ1) != TYPE_PRECISION (char_type_node))
4727 {
ab87f8c8 4728 error_init ("int-array initialized from non-wide string");
400fbf9f
JW
4729 return error_mark_node;
4730 }
4731
fd5d5b94 4732 TREE_TYPE (inside_init) = type;
400fbf9f 4733 if (TYPE_DOMAIN (type) != 0
a63f73f8 4734 && TYPE_SIZE (type) != 0
05bccae2 4735 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
fe9ef5d7
RS
4736 /* Subtract 1 (or sizeof (wchar_t))
4737 because it's ok to ignore the terminating null char
400fbf9f 4738 that is counted in the length of the constant. */
05bccae2
RK
4739 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type),
4740 TREE_STRING_LENGTH (inside_init)
4741 - ((TYPE_PRECISION (typ1)
4742 != TYPE_PRECISION (char_type_node))
4743 ? (TYPE_PRECISION (wchar_type_node)
4744 / BITS_PER_UNIT)
4745 : 1)))
4746 pedwarn_init ("initializer-string for array of chars is too long");
4747
fd5d5b94 4748 return inside_init;
400fbf9f
JW
4749 }
4750 }
4751
d744e06e
AH
4752 /* Build a VECTOR_CST from a *constant* vector constructor. If the
4753 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
4754 below and handle as a constructor. */
9938b5d9 4755 if (code == VECTOR_TYPE
d1bd0ded 4756 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT)
9938b5d9
AH
4757 && TREE_CONSTANT (inside_init))
4758 {
4759 if (TREE_CODE (inside_init) == VECTOR_CST
4760 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
d1bd0ded
GK
4761 TYPE_MAIN_VARIANT (type),
4762 COMPARE_STRICT))
9938b5d9
AH
4763 return inside_init;
4764 else
4765 return build_vector (type, CONSTRUCTOR_ELTS (inside_init));
4766 }
d744e06e 4767
de520661
RS
4768 /* Any type can be initialized
4769 from an expression of the same type, optionally with braces. */
400fbf9f 4770
2726966d 4771 if (inside_init && TREE_TYPE (inside_init) != 0
5522c047 4772 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
d1bd0ded 4773 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)
2726966d 4774 || (code == ARRAY_TYPE
d1bd0ded 4775 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
e6834654 4776 || (code == VECTOR_TYPE
d1bd0ded 4777 && comptypes (TREE_TYPE (inside_init), type, COMPARE_STRICT))
3c3fa147 4778 || (code == POINTER_TYPE
3c3fa147
RS
4779 && (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
4780 || TREE_CODE (TREE_TYPE (inside_init)) == FUNCTION_TYPE)
4781 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
d1bd0ded 4782 TREE_TYPE (type), COMPARE_STRICT))))
400fbf9f 4783 {
207bf485
JM
4784 if (code == POINTER_TYPE)
4785 inside_init = default_function_array_conversion (inside_init);
59c83dbf
JJ
4786
4787 if (require_constant && !flag_isoc99
4788 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
4789 {
4790 /* As an extension, allow initializing objects with static storage
4791 duration with compound literals (which are then treated just as
4792 the brace enclosed list they contain). */
4793 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
4794 inside_init = DECL_INITIAL (decl);
4795 }
4796
4797 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
4798 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 4799 {
ab87f8c8 4800 error_init ("array initialized from non-constant array expression");
400fbf9f
JW
4801 return error_mark_node;
4802 }
4803
8c3a6477 4804 if (optimize && TREE_CODE (inside_init) == VAR_DECL)
2f74f7e9 4805 inside_init = decl_constant_value_for_broken_optimization (inside_init);
400fbf9f 4806
d9fc6069
JW
4807 /* Compound expressions can only occur here if -pedantic or
4808 -pedantic-errors is specified. In the later case, we always want
4809 an error. In the former case, we simply want a warning. */
4810 if (require_constant && pedantic
4811 && TREE_CODE (inside_init) == COMPOUND_EXPR)
4812 {
4813 inside_init
4814 = valid_compound_expr_initializer (inside_init,
4815 TREE_TYPE (inside_init));
4816 if (inside_init == error_mark_node)
ab87f8c8 4817 error_init ("initializer element is not constant");
d9fc6069 4818 else
ab87f8c8 4819 pedwarn_init ("initializer element is not constant");
d9fc6069
JW
4820 if (flag_pedantic_errors)
4821 inside_init = error_mark_node;
4822 }
2f6e4e97 4823 else if (require_constant
eb8543b3
MM
4824 && (!TREE_CONSTANT (inside_init)
4825 /* This test catches things like `7 / 0' which
4826 result in an expression for which TREE_CONSTANT
4827 is true, but which is not actually something
4828 that is a legal constant. We really should not
4829 be using this function, because it is a part of
4830 the back-end. Instead, the expression should
4831 already have been turned into ERROR_MARK_NODE. */
4832 || !initializer_constant_valid_p (inside_init,
4833 TREE_TYPE (inside_init))))
400fbf9f 4834 {
ab87f8c8 4835 error_init ("initializer element is not constant");
047de90b 4836 inside_init = error_mark_node;
400fbf9f 4837 }
400fbf9f 4838
047de90b 4839 return inside_init;
400fbf9f
JW
4840 }
4841
400fbf9f
JW
4842 /* Handle scalar types, including conversions. */
4843
4844 if (code == INTEGER_TYPE || code == REAL_TYPE || code == POINTER_TYPE
47f8b473 4845 || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE || code == COMPLEX_TYPE)
400fbf9f 4846 {
e3a12f0c
RS
4847 /* Note that convert_for_assignment calls default_conversion
4848 for arrays and functions. We must not call it in the
4849 case where inside_init is a null pointer constant. */
4850 inside_init
ab87f8c8 4851 = convert_for_assignment (type, init, _("initialization"),
e3a12f0c 4852 NULL_TREE, NULL_TREE, 0);
400fbf9f 4853
047de90b 4854 if (require_constant && ! TREE_CONSTANT (inside_init))
400fbf9f 4855 {
ab87f8c8 4856 error_init ("initializer element is not constant");
047de90b 4857 inside_init = error_mark_node;
400fbf9f 4858 }
f0c70ef0
RS
4859 else if (require_constant
4860 && initializer_constant_valid_p (inside_init, TREE_TYPE (inside_init)) == 0)
400fbf9f 4861 {
ab87f8c8 4862 error_init ("initializer element is not computable at load time");
047de90b 4863 inside_init = error_mark_node;
400fbf9f
JW
4864 }
4865
047de90b 4866 return inside_init;
400fbf9f
JW
4867 }
4868
4869 /* Come here only for records and arrays. */
4870
d0f062fb 4871 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
400fbf9f 4872 {
ab87f8c8 4873 error_init ("variable-sized object may not be initialized");
400fbf9f
JW
4874 return error_mark_node;
4875 }
4876
ab87f8c8 4877 error_init ("invalid initializer");
400fbf9f
JW
4878 return error_mark_node;
4879}
4880\f
de520661 4881/* Handle initializers that use braces. */
400fbf9f 4882
de520661
RS
4883/* Type of object we are accumulating a constructor for.
4884 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
4885static tree constructor_type;
400fbf9f 4886
de520661
RS
4887/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
4888 left to fill. */
4889static tree constructor_fields;
400fbf9f 4890
de520661 4891/* For an ARRAY_TYPE, this is the specified index
665f2503 4892 at which to store the next element we get. */
de520661 4893static tree constructor_index;
400fbf9f 4894
de520661
RS
4895/* For an ARRAY_TYPE, this is the maximum index. */
4896static tree constructor_max_index;
103b7b17 4897
de520661
RS
4898/* For a RECORD_TYPE, this is the first field not yet written out. */
4899static tree constructor_unfilled_fields;
400fbf9f 4900
de520661 4901/* For an ARRAY_TYPE, this is the index of the first element
665f2503 4902 not yet written out. */
de520661
RS
4903static tree constructor_unfilled_index;
4904
b62acd60 4905/* In a RECORD_TYPE, the byte index of the next consecutive field.
665f2503 4906 This is so we can generate gaps between fields, when appropriate. */
b62acd60
RS
4907static tree constructor_bit_index;
4908
de520661
RS
4909/* If we are saving up the elements rather than allocating them,
4910 this is the list of elements so far (in reverse order,
4911 most recent first). */
4912static tree constructor_elements;
4913
8b6a5902
JJ
4914/* 1 if constructor should be incrementally stored into a constructor chain,
4915 0 if all the elements should be kept in AVL tree. */
4916static int constructor_incremental;
4917
de520661
RS
4918/* 1 if so far this constructor's elements are all compile-time constants. */
4919static int constructor_constant;
4920
4921/* 1 if so far this constructor's elements are all valid address constants. */
4922static int constructor_simple;
4923
4924/* 1 if this constructor is erroneous so far. */
4925static int constructor_erroneous;
4926
e5e809f4
JL
4927/* Structure for managing pending initializer elements, organized as an
4928 AVL tree. */
4929
4930struct init_node
4931{
4932 struct init_node *left, *right;
4933 struct init_node *parent;
4934 int balance;
4935 tree purpose;
4936 tree value;
4937};
4938
4939/* Tree of pending elements at this constructor level.
de520661
RS
4940 These are elements encountered out of order
4941 which belong at places we haven't reached yet in actually
4dd7201e
ZW
4942 writing the output.
4943 Will never hold tree nodes across GC runs. */
e5e809f4 4944static struct init_node *constructor_pending_elts;
de520661
RS
4945
4946/* The SPELLING_DEPTH of this constructor. */
4947static int constructor_depth;
4948
cc77d4d5 4949/* 0 if implicitly pushing constructor levels is allowed. */
0f41302f 4950int constructor_no_implicit = 0; /* 0 for C; 1 for some other languages. */
cc77d4d5 4951
de520661
RS
4952static int require_constant_value;
4953static int require_constant_elements;
4954
de520661
RS
4955/* DECL node for which an initializer is being read.
4956 0 means we are reading a constructor expression
4957 such as (struct foo) {...}. */
4958static tree constructor_decl;
4959
4960/* start_init saves the ASMSPEC arg here for really_start_incremental_init. */
520a57c8 4961static const char *constructor_asmspec;
de520661
RS
4962
4963/* Nonzero if this is an initializer for a top-level decl. */
4964static int constructor_top_level;
4965
b621a4dd
ZW
4966/* Nonzero if there were any member designators in this initializer. */
4967static int constructor_designated;
4968
8b6a5902
JJ
4969/* Nesting depth of designator list. */
4970static int designator_depth;
4971
4972/* Nonzero if there were diagnosed errors in this designator list. */
4973static int designator_errorneous;
4974
b62acd60
RS
4975\f
4976/* This stack has a level for each implicit or explicit level of
4977 structuring in the initializer, including the outermost one. It
4978 saves the values of most of the variables above. */
de520661 4979
940ff66d
JJ
4980struct constructor_range_stack;
4981
de520661 4982struct constructor_stack
400fbf9f 4983{
de520661
RS
4984 struct constructor_stack *next;
4985 tree type;
4986 tree fields;
4987 tree index;
de520661
RS
4988 tree max_index;
4989 tree unfilled_index;
4990 tree unfilled_fields;
b62acd60 4991 tree bit_index;
de520661 4992 tree elements;
e5e809f4 4993 struct init_node *pending_elts;
940ff66d 4994 int offset;
de520661 4995 int depth;
790e9490
RS
4996 /* If nonzero, this value should replace the entire
4997 constructor at this level. */
4998 tree replacement_value;
940ff66d 4999 struct constructor_range_stack *range_stack;
de520661
RS
5000 char constant;
5001 char simple;
5002 char implicit;
de520661
RS
5003 char erroneous;
5004 char outer;
8b6a5902 5005 char incremental;
b621a4dd 5006 char designated;
de520661 5007};
d45cf215 5008
de520661 5009struct constructor_stack *constructor_stack;
400fbf9f 5010
8b6a5902
JJ
5011/* This stack represents designators from some range designator up to
5012 the last designator in the list. */
5013
5014struct constructor_range_stack
5015{
5016 struct constructor_range_stack *next, *prev;
5017 struct constructor_stack *stack;
5018 tree range_start;
5019 tree index;
5020 tree range_end;
5021 tree fields;
5022};
5023
5024struct constructor_range_stack *constructor_range_stack;
5025
de520661
RS
5026/* This stack records separate initializers that are nested.
5027 Nested initializers can't happen in ANSI C, but GNU C allows them
5028 in cases like { ... (struct foo) { ... } ... }. */
400fbf9f 5029
de520661
RS
5030struct initializer_stack
5031{
5032 struct initializer_stack *next;
5033 tree decl;
520a57c8 5034 const char *asmspec;
de520661 5035 struct constructor_stack *constructor_stack;
8b6a5902 5036 struct constructor_range_stack *constructor_range_stack;
dea273df 5037 tree elements;
de520661
RS
5038 struct spelling *spelling;
5039 struct spelling *spelling_base;
5040 int spelling_size;
5041 char top_level;
de520661
RS
5042 char require_constant_value;
5043 char require_constant_elements;
de520661
RS
5044};
5045
5046struct initializer_stack *initializer_stack;
5047\f
5048/* Prepare to parse and output the initializer for variable DECL. */
5049
5050void
2f6e4e97 5051start_init (tree decl, tree asmspec_tree, int top_level)
de520661 5052{
5d5993dd 5053 const char *locus;
de520661
RS
5054 struct initializer_stack *p
5055 = (struct initializer_stack *) xmalloc (sizeof (struct initializer_stack));
520a57c8 5056 const char *asmspec = 0;
e28cae4f
RS
5057
5058 if (asmspec_tree)
5059 asmspec = TREE_STRING_POINTER (asmspec_tree);
de520661
RS
5060
5061 p->decl = constructor_decl;
5062 p->asmspec = constructor_asmspec;
de520661
RS
5063 p->require_constant_value = require_constant_value;
5064 p->require_constant_elements = require_constant_elements;
5065 p->constructor_stack = constructor_stack;
8b6a5902 5066 p->constructor_range_stack = constructor_range_stack;
dea273df 5067 p->elements = constructor_elements;
de520661
RS
5068 p->spelling = spelling;
5069 p->spelling_base = spelling_base;
5070 p->spelling_size = spelling_size;
de520661 5071 p->top_level = constructor_top_level;
b62acd60 5072 p->next = initializer_stack;
de520661
RS
5073 initializer_stack = p;
5074
5075 constructor_decl = decl;
de520661 5076 constructor_asmspec = asmspec;
b621a4dd 5077 constructor_designated = 0;
de520661
RS
5078 constructor_top_level = top_level;
5079
5080 if (decl != 0)
3c3fa147 5081 {
de520661 5082 require_constant_value = TREE_STATIC (decl);
f1a2b955 5083 require_constant_elements
8b6a5902 5084 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
f1a2b955
RS
5085 /* For a scalar, you can always use any value to initialize,
5086 even within braces. */
5087 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
5088 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
5089 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
5090 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
de520661 5091 locus = IDENTIFIER_POINTER (DECL_NAME (decl));
3c3fa147 5092 }
400fbf9f 5093 else
de520661
RS
5094 {
5095 require_constant_value = 0;
5096 require_constant_elements = 0;
5097 locus = "(anonymous)";
5098 }
400fbf9f 5099
de520661 5100 constructor_stack = 0;
8b6a5902 5101 constructor_range_stack = 0;
400fbf9f 5102
b71c7f8a
RK
5103 missing_braces_mentioned = 0;
5104
de520661
RS
5105 spelling_base = 0;
5106 spelling_size = 0;
5107 RESTORE_SPELLING_DEPTH (0);
d45cf215 5108
de520661
RS
5109 if (locus)
5110 push_string (locus);
5111}
400fbf9f 5112
de520661 5113void
2f6e4e97 5114finish_init (void)
de520661
RS
5115{
5116 struct initializer_stack *p = initializer_stack;
400fbf9f 5117
de520661
RS
5118 /* Free the whole constructor stack of this initializer. */
5119 while (constructor_stack)
5120 {
5121 struct constructor_stack *q = constructor_stack;
5122 constructor_stack = q->next;
5123 free (q);
5124 }
400fbf9f 5125
8b6a5902
JJ
5126 if (constructor_range_stack)
5127 abort ();
5128
de520661
RS
5129 /* Pop back to the data of the outer initializer (if any). */
5130 constructor_decl = p->decl;
5131 constructor_asmspec = p->asmspec;
de520661
RS
5132 require_constant_value = p->require_constant_value;
5133 require_constant_elements = p->require_constant_elements;
5134 constructor_stack = p->constructor_stack;
8b6a5902 5135 constructor_range_stack = p->constructor_range_stack;
dea273df 5136 constructor_elements = p->elements;
de520661
RS
5137 spelling = p->spelling;
5138 spelling_base = p->spelling_base;
5139 spelling_size = p->spelling_size;
de520661
RS
5140 constructor_top_level = p->top_level;
5141 initializer_stack = p->next;
5142 free (p);
5143}
5144\f
5145/* Call here when we see the initializer is surrounded by braces.
5146 This is instead of a call to push_init_level;
5147 it is matched by a call to pop_init_level.
400fbf9f 5148
de520661
RS
5149 TYPE is the type to initialize, for a constructor expression.
5150 For an initializer for a decl, TYPE is zero. */
5a7ec9d9 5151
de520661 5152void
2f6e4e97 5153really_start_incremental_init (tree type)
de520661
RS
5154{
5155 struct constructor_stack *p
5156 = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
5157
5158 if (type == 0)
5159 type = TREE_TYPE (constructor_decl);
5160
c8e4f0e9
AH
5161 if ((*targetm.vector_opaque_p) (type))
5162 error ("opaque vector types cannot be initialized");
5163
de520661
RS
5164 p->type = constructor_type;
5165 p->fields = constructor_fields;
5166 p->index = constructor_index;
de520661
RS
5167 p->max_index = constructor_max_index;
5168 p->unfilled_index = constructor_unfilled_index;
5169 p->unfilled_fields = constructor_unfilled_fields;
b62acd60 5170 p->bit_index = constructor_bit_index;
5cb7368c 5171 p->elements = constructor_elements;
de520661
RS
5172 p->constant = constructor_constant;
5173 p->simple = constructor_simple;
5174 p->erroneous = constructor_erroneous;
5175 p->pending_elts = constructor_pending_elts;
5176 p->depth = constructor_depth;
790e9490 5177 p->replacement_value = 0;
de520661 5178 p->implicit = 0;
940ff66d 5179 p->range_stack = 0;
de520661 5180 p->outer = 0;
8b6a5902 5181 p->incremental = constructor_incremental;
b621a4dd 5182 p->designated = constructor_designated;
de520661
RS
5183 p->next = 0;
5184 constructor_stack = p;
5185
5186 constructor_constant = 1;
5187 constructor_simple = 1;
5188 constructor_depth = SPELLING_DEPTH ();
5189 constructor_elements = 0;
5190 constructor_pending_elts = 0;
5191 constructor_type = type;
8b6a5902 5192 constructor_incremental = 1;
b621a4dd 5193 constructor_designated = 0;
8b6a5902
JJ
5194 designator_depth = 0;
5195 designator_errorneous = 0;
de520661
RS
5196
5197 if (TREE_CODE (constructor_type) == RECORD_TYPE
5198 || TREE_CODE (constructor_type) == UNION_TYPE)
5199 {
5200 constructor_fields = TYPE_FIELDS (constructor_type);
abc95ed3 5201 /* Skip any nameless bit fields at the beginning. */
ef86d2a6 5202 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
fc623854
RS
5203 && DECL_NAME (constructor_fields) == 0)
5204 constructor_fields = TREE_CHAIN (constructor_fields);
665f2503 5205
de520661 5206 constructor_unfilled_fields = constructor_fields;
770ae6cc 5207 constructor_bit_index = bitsize_zero_node;
de520661
RS
5208 }
5209 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5210 {
de520661 5211 if (TYPE_DOMAIN (constructor_type))
2bede729
PB
5212 {
5213 constructor_max_index
5214 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
ffc5c6a9
RH
5215
5216 /* Detect non-empty initializations of zero-length arrays. */
e7b6a0ee
DD
5217 if (constructor_max_index == NULL_TREE
5218 && TYPE_SIZE (constructor_type))
ffc5c6a9
RH
5219 constructor_max_index = build_int_2 (-1, -1);
5220
39bc99c2 5221 /* constructor_max_index needs to be an INTEGER_CST. Attempts
09da1532 5222 to initialize VLAs will cause a proper error; avoid tree
39bc99c2
JM
5223 checking errors as well by setting a safe value. */
5224 if (constructor_max_index
5225 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5226 constructor_max_index = build_int_2 (-1, -1);
5227
2bede729 5228 constructor_index
665f2503
RK
5229 = convert (bitsizetype,
5230 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
2bede729
PB
5231 }
5232 else
770ae6cc 5233 constructor_index = bitsize_zero_node;
fed3cef0 5234
665f2503 5235 constructor_unfilled_index = constructor_index;
de520661 5236 }
e6834654
SS
5237 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5238 {
5239 /* Vectors are like simple fixed-size arrays. */
5240 constructor_max_index =
5241 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
b7997284 5242 constructor_index = convert (bitsizetype, bitsize_zero_node);
e6834654
SS
5243 constructor_unfilled_index = constructor_index;
5244 }
de520661
RS
5245 else
5246 {
5247 /* Handle the case of int x = {5}; */
5248 constructor_fields = constructor_type;
5249 constructor_unfilled_fields = constructor_type;
5250 }
de520661
RS
5251}
5252\f
5253/* Push down into a subobject, for initialization.
5254 If this is for an explicit set of braces, IMPLICIT is 0.
5255 If it is because the next element belongs at a lower level,
8b6a5902 5256 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 5257
de520661 5258void
2f6e4e97 5259push_init_level (int implicit)
de520661 5260{
94ba5069 5261 struct constructor_stack *p;
8b6a5902 5262 tree value = NULL_TREE;
94ba5069
RS
5263
5264 /* If we've exhausted any levels that didn't have braces,
5265 pop them now. */
5266 while (constructor_stack->implicit)
5267 {
5268 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5269 || TREE_CODE (constructor_type) == UNION_TYPE)
5270 && constructor_fields == 0)
5271 process_init_element (pop_init_level (1));
5272 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
2f6e4e97 5273 && constructor_max_index
94ba5069
RS
5274 && tree_int_cst_lt (constructor_max_index, constructor_index))
5275 process_init_element (pop_init_level (1));
5276 else
5277 break;
5278 }
5279
8b6a5902
JJ
5280 /* Unless this is an explicit brace, we need to preserve previous
5281 content if any. */
5282 if (implicit)
5283 {
5284 if ((TREE_CODE (constructor_type) == RECORD_TYPE
5285 || TREE_CODE (constructor_type) == UNION_TYPE)
5286 && constructor_fields)
5287 value = find_init_member (constructor_fields);
5288 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5289 value = find_init_member (constructor_index);
5290 }
5291
94ba5069 5292 p = (struct constructor_stack *) xmalloc (sizeof (struct constructor_stack));
de520661
RS
5293 p->type = constructor_type;
5294 p->fields = constructor_fields;
5295 p->index = constructor_index;
de520661
RS
5296 p->max_index = constructor_max_index;
5297 p->unfilled_index = constructor_unfilled_index;
5298 p->unfilled_fields = constructor_unfilled_fields;
b62acd60 5299 p->bit_index = constructor_bit_index;
de520661
RS
5300 p->elements = constructor_elements;
5301 p->constant = constructor_constant;
5302 p->simple = constructor_simple;
5303 p->erroneous = constructor_erroneous;
5304 p->pending_elts = constructor_pending_elts;
5305 p->depth = constructor_depth;
790e9490 5306 p->replacement_value = 0;
de520661 5307 p->implicit = implicit;
de520661 5308 p->outer = 0;
8b6a5902 5309 p->incremental = constructor_incremental;
b621a4dd 5310 p->designated = constructor_designated;
de520661 5311 p->next = constructor_stack;
940ff66d 5312 p->range_stack = 0;
de520661
RS
5313 constructor_stack = p;
5314
5315 constructor_constant = 1;
5316 constructor_simple = 1;
5317 constructor_depth = SPELLING_DEPTH ();
5318 constructor_elements = 0;
8b6a5902 5319 constructor_incremental = 1;
b621a4dd 5320 constructor_designated = 0;
de520661 5321 constructor_pending_elts = 0;
8b6a5902
JJ
5322 if (!implicit)
5323 {
940ff66d
JJ
5324 p->range_stack = constructor_range_stack;
5325 constructor_range_stack = 0;
8b6a5902
JJ
5326 designator_depth = 0;
5327 designator_errorneous = 0;
5328 }
de520661 5329
94ba5069
RS
5330 /* Don't die if an entire brace-pair level is superfluous
5331 in the containing level. */
5332 if (constructor_type == 0)
5333 ;
5334 else if (TREE_CODE (constructor_type) == RECORD_TYPE
5335 || TREE_CODE (constructor_type) == UNION_TYPE)
de520661 5336 {
91fa3c30
RS
5337 /* Don't die if there are extra init elts at the end. */
5338 if (constructor_fields == 0)
5339 constructor_type = 0;
5340 else
5341 {
5342 constructor_type = TREE_TYPE (constructor_fields);
19d76e60 5343 push_member_name (constructor_fields);
e4376e63 5344 constructor_depth++;
91fa3c30 5345 }
de520661
RS
5346 }
5347 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5348 {
5349 constructor_type = TREE_TYPE (constructor_type);
665f2503 5350 push_array_bounds (tree_low_cst (constructor_index, 0));
e4376e63 5351 constructor_depth++;
de520661 5352 }
400fbf9f 5353
91fa3c30
RS
5354 if (constructor_type == 0)
5355 {
ab87f8c8 5356 error_init ("extra brace group at end of initializer");
91fa3c30
RS
5357 constructor_fields = 0;
5358 constructor_unfilled_fields = 0;
b71c7f8a 5359 return;
91fa3c30 5360 }
b71c7f8a 5361
8b6a5902
JJ
5362 if (value && TREE_CODE (value) == CONSTRUCTOR)
5363 {
5364 constructor_constant = TREE_CONSTANT (value);
5365 constructor_simple = TREE_STATIC (value);
6d0c8bb1 5366 constructor_elements = CONSTRUCTOR_ELTS (value);
8b6a5902
JJ
5367 if (constructor_elements
5368 && (TREE_CODE (constructor_type) == RECORD_TYPE
5369 || TREE_CODE (constructor_type) == ARRAY_TYPE))
5370 set_nonincremental_init ();
5371 }
5372
5373 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
b71c7f8a
RK
5374 {
5375 missing_braces_mentioned = 1;
ab87f8c8 5376 warning_init ("missing braces around initializer");
b71c7f8a
RK
5377 }
5378
5379 if (TREE_CODE (constructor_type) == RECORD_TYPE
91fa3c30 5380 || TREE_CODE (constructor_type) == UNION_TYPE)
de520661
RS
5381 {
5382 constructor_fields = TYPE_FIELDS (constructor_type);
abc95ed3 5383 /* Skip any nameless bit fields at the beginning. */
ef86d2a6 5384 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
fc623854
RS
5385 && DECL_NAME (constructor_fields) == 0)
5386 constructor_fields = TREE_CHAIN (constructor_fields);
665f2503 5387
de520661 5388 constructor_unfilled_fields = constructor_fields;
770ae6cc 5389 constructor_bit_index = bitsize_zero_node;
de520661 5390 }
6786d201
AH
5391 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
5392 {
5393 /* Vectors are like simple fixed-size arrays. */
5394 constructor_max_index =
5395 build_int_2 (TYPE_VECTOR_SUBPARTS (constructor_type) - 1, 0);
5396 constructor_index = convert (bitsizetype, integer_zero_node);
5397 constructor_unfilled_index = constructor_index;
5398 }
de520661
RS
5399 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5400 {
de520661 5401 if (TYPE_DOMAIN (constructor_type))
2bede729
PB
5402 {
5403 constructor_max_index
5404 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
e7b6a0ee
DD
5405
5406 /* Detect non-empty initializations of zero-length arrays. */
5407 if (constructor_max_index == NULL_TREE
5408 && TYPE_SIZE (constructor_type))
5409 constructor_max_index = build_int_2 (-1, -1);
39bc99c2
JM
5410
5411 /* constructor_max_index needs to be an INTEGER_CST. Attempts
09da1532 5412 to initialize VLAs will cause a proper error; avoid tree
39bc99c2
JM
5413 checking errors as well by setting a safe value. */
5414 if (constructor_max_index
5415 && TREE_CODE (constructor_max_index) != INTEGER_CST)
5416 constructor_max_index = build_int_2 (-1, -1);
e7b6a0ee 5417
2bede729 5418 constructor_index
2f6e4e97 5419 = convert (bitsizetype,
584ef5fe 5420 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
2bede729
PB
5421 }
5422 else
770ae6cc 5423 constructor_index = bitsize_zero_node;
fed3cef0 5424
665f2503 5425 constructor_unfilled_index = constructor_index;
8b6a5902
JJ
5426 if (value && TREE_CODE (value) == STRING_CST)
5427 {
5428 /* We need to split the char/wchar array into individual
5429 characters, so that we don't have to special case it
5430 everywhere. */
5431 set_nonincremental_init_from_string (value);
5432 }
de520661
RS
5433 }
5434 else
5435 {
ab87f8c8 5436 warning_init ("braces around scalar initializer");
de520661
RS
5437 constructor_fields = constructor_type;
5438 constructor_unfilled_fields = constructor_type;
5439 }
5440}
400fbf9f 5441
2f6e4e97 5442/* At the end of an implicit or explicit brace level,
de520661
RS
5443 finish up that level of constructor.
5444 If we were outputting the elements as they are read, return 0
5445 from inner levels (process_init_element ignores that),
5446 but return error_mark_node from the outermost level
5447 (that's what we want to put in DECL_INITIAL).
5448 Otherwise, return a CONSTRUCTOR expression. */
5449
5450tree
2f6e4e97 5451pop_init_level (int implicit)
de520661
RS
5452{
5453 struct constructor_stack *p;
de520661
RS
5454 tree constructor = 0;
5455
5456 if (implicit == 0)
400fbf9f 5457 {
de520661
RS
5458 /* When we come to an explicit close brace,
5459 pop any inner levels that didn't have explicit braces. */
5460 while (constructor_stack->implicit)
5461 process_init_element (pop_init_level (1));
940ff66d
JJ
5462
5463 if (constructor_range_stack)
5464 abort ();
de520661 5465 }
400fbf9f 5466
de520661 5467 p = constructor_stack;
91fa3c30 5468
584ef5fe
RH
5469 /* Error for initializing a flexible array member, or a zero-length
5470 array member in an inappropriate context. */
ffc5c6a9 5471 if (constructor_type && constructor_fields
584ef5fe
RH
5472 && TREE_CODE (constructor_type) == ARRAY_TYPE
5473 && TYPE_DOMAIN (constructor_type)
5474 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
5475 {
ffc5c6a9
RH
5476 /* Silently discard empty initializations. The parser will
5477 already have pedwarned for empty brackets. */
5478 if (integer_zerop (constructor_unfilled_index))
5479 constructor_type = NULL_TREE;
5480 else if (! TYPE_SIZE (constructor_type))
5481 {
5482 if (constructor_depth > 2)
5483 error_init ("initialization of flexible array member in a nested context");
5484 else if (pedantic)
5485 pedwarn_init ("initialization of a flexible array member");
5486
ff7cc307 5487 /* We have already issued an error message for the existence
ffc5c6a9
RH
5488 of a flexible array member not at the end of the structure.
5489 Discard the initializer so that we do not abort later. */
5490 if (TREE_CHAIN (constructor_fields) != NULL_TREE)
5491 constructor_type = NULL_TREE;
5492 }
5493 else
e7b6a0ee
DD
5494 /* Zero-length arrays are no longer special, so we should no longer
5495 get here. */
505ddab6 5496 abort ();
584ef5fe
RH
5497 }
5498
9dfcc8db
BH
5499 /* Warn when some struct elements are implicitly initialized to zero. */
5500 if (extra_warnings
5501 && constructor_type
5502 && TREE_CODE (constructor_type) == RECORD_TYPE
5503 && constructor_unfilled_fields)
5504 {
8b6a5902
JJ
5505 /* Do not warn for flexible array members or zero-length arrays. */
5506 while (constructor_unfilled_fields
5507 && (! DECL_SIZE (constructor_unfilled_fields)
5508 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
5509 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
ffc5c6a9 5510
b621a4dd
ZW
5511 /* Do not warn if this level of the initializer uses member
5512 designators; it is likely to be deliberate. */
5513 if (constructor_unfilled_fields && !constructor_designated)
8b6a5902
JJ
5514 {
5515 push_member_name (constructor_unfilled_fields);
5516 warning_init ("missing initializer");
5517 RESTORE_SPELLING_DEPTH (constructor_depth);
5518 }
9dfcc8db
BH
5519 }
5520
de520661 5521 /* Now output all pending elements. */
8b6a5902 5522 constructor_incremental = 1;
de520661
RS
5523 output_pending_init_elements (1);
5524
5525 /* Pad out the end of the structure. */
790e9490 5526 if (p->replacement_value)
229a2e65
MM
5527 /* If this closes a superfluous brace pair,
5528 just pass out the element between them. */
5529 constructor = p->replacement_value;
91fa3c30
RS
5530 else if (constructor_type == 0)
5531 ;
19d76e60
RK
5532 else if (TREE_CODE (constructor_type) != RECORD_TYPE
5533 && TREE_CODE (constructor_type) != UNION_TYPE
e6834654
SS
5534 && TREE_CODE (constructor_type) != ARRAY_TYPE
5535 && TREE_CODE (constructor_type) != VECTOR_TYPE)
19d76e60
RK
5536 {
5537 /* A nonincremental scalar initializer--just return
5538 the element, after verifying there is just one. */
5539 if (constructor_elements == 0)
5540 {
8b6a5902
JJ
5541 if (!constructor_erroneous)
5542 error_init ("empty scalar initializer");
19d76e60
RK
5543 constructor = error_mark_node;
5544 }
5545 else if (TREE_CHAIN (constructor_elements) != 0)
5546 {
ab87f8c8 5547 error_init ("extra elements in scalar initializer");
19d76e60
RK
5548 constructor = TREE_VALUE (constructor_elements);
5549 }
5550 else
5551 constructor = TREE_VALUE (constructor_elements);
5552 }
229a2e65 5553 else
de520661
RS
5554 {
5555 if (constructor_erroneous)
5556 constructor = error_mark_node;
5557 else
400fbf9f 5558 {
dcf92453
ZW
5559 constructor = build_constructor (constructor_type,
5560 nreverse (constructor_elements));
de520661
RS
5561 if (constructor_constant)
5562 TREE_CONSTANT (constructor) = 1;
5563 if (constructor_constant && constructor_simple)
5564 TREE_STATIC (constructor) = 1;
de520661
RS
5565 }
5566 }
de520661 5567
de520661
RS
5568 constructor_type = p->type;
5569 constructor_fields = p->fields;
5570 constructor_index = p->index;
de520661
RS
5571 constructor_max_index = p->max_index;
5572 constructor_unfilled_index = p->unfilled_index;
5573 constructor_unfilled_fields = p->unfilled_fields;
b62acd60 5574 constructor_bit_index = p->bit_index;
de520661
RS
5575 constructor_elements = p->elements;
5576 constructor_constant = p->constant;
5577 constructor_simple = p->simple;
5578 constructor_erroneous = p->erroneous;
8b6a5902 5579 constructor_incremental = p->incremental;
b621a4dd 5580 constructor_designated = p->designated;
de520661
RS
5581 constructor_pending_elts = p->pending_elts;
5582 constructor_depth = p->depth;
940ff66d
JJ
5583 if (!p->implicit)
5584 constructor_range_stack = p->range_stack;
de520661
RS
5585 RESTORE_SPELLING_DEPTH (constructor_depth);
5586
5587 constructor_stack = p->next;
5588 free (p);
5589
5590 if (constructor == 0)
5591 {
5592 if (constructor_stack == 0)
5593 return error_mark_node;
5594 return NULL_TREE;
5595 }
5596 return constructor;
5597}
5598
8b6a5902 5599/* Common handling for both array range and field name designators.
da7d8304 5600 ARRAY argument is nonzero for array ranges. Returns zero for success. */
8b6a5902
JJ
5601
5602static int
2f6e4e97 5603set_designator (int array)
8b6a5902
JJ
5604{
5605 tree subtype;
5606 enum tree_code subcode;
5607
5608 /* Don't die if an entire brace-pair level is superfluous
5609 in the containing level. */
5610 if (constructor_type == 0)
5611 return 1;
5612
5613 /* If there were errors in this designator list already, bail out silently. */
5614 if (designator_errorneous)
5615 return 1;
5616
5617 if (!designator_depth)
5618 {
5619 if (constructor_range_stack)
5620 abort ();
5621
5622 /* Designator list starts at the level of closest explicit
5623 braces. */
5624 while (constructor_stack->implicit)
5625 process_init_element (pop_init_level (1));
b621a4dd 5626 constructor_designated = 1;
8b6a5902
JJ
5627 return 0;
5628 }
5629
5630 if (constructor_no_implicit)
5631 {
5632 error_init ("initialization designators may not nest");
5633 return 1;
5634 }
5635
5636 if (TREE_CODE (constructor_type) == RECORD_TYPE
5637 || TREE_CODE (constructor_type) == UNION_TYPE)
5638 {
5639 subtype = TREE_TYPE (constructor_fields);
5640 if (subtype != error_mark_node)
5641 subtype = TYPE_MAIN_VARIANT (subtype);
5642 }
5643 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5644 {
5645 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
5646 }
5647 else
5648 abort ();
5649
5650 subcode = TREE_CODE (subtype);
5651 if (array && subcode != ARRAY_TYPE)
5652 {
5653 error_init ("array index in non-array initializer");
5654 return 1;
5655 }
5656 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
5657 {
5658 error_init ("field name not in record or union initializer");
5659 return 1;
5660 }
5661
b621a4dd 5662 constructor_designated = 1;
8b6a5902
JJ
5663 push_init_level (2);
5664 return 0;
5665}
5666
5667/* If there are range designators in designator list, push a new designator
5668 to constructor_range_stack. RANGE_END is end of such stack range or
5669 NULL_TREE if there is no range designator at this level. */
5670
5671static void
2f6e4e97 5672push_range_stack (tree range_end)
8b6a5902
JJ
5673{
5674 struct constructor_range_stack *p;
5675
5676 p = (struct constructor_range_stack *)
5677 ggc_alloc (sizeof (struct constructor_range_stack));
5678 p->prev = constructor_range_stack;
5679 p->next = 0;
5680 p->fields = constructor_fields;
5681 p->range_start = constructor_index;
5682 p->index = constructor_index;
5683 p->stack = constructor_stack;
5684 p->range_end = range_end;
5685 if (constructor_range_stack)
5686 constructor_range_stack->next = p;
5687 constructor_range_stack = p;
5688}
5689
de520661
RS
5690/* Within an array initializer, specify the next index to be initialized.
5691 FIRST is that index. If LAST is nonzero, then initialize a range
5692 of indices, running from FIRST through LAST. */
5693
5694void
2f6e4e97 5695set_init_index (tree first, tree last)
de520661 5696{
8b6a5902
JJ
5697 if (set_designator (1))
5698 return;
5699
5700 designator_errorneous = 1;
5701
19d76e60
RK
5702 while ((TREE_CODE (first) == NOP_EXPR
5703 || TREE_CODE (first) == CONVERT_EXPR
5704 || TREE_CODE (first) == NON_LVALUE_EXPR)
5705 && (TYPE_MODE (TREE_TYPE (first))
5706 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (first, 0)))))
665f2503
RK
5707 first = TREE_OPERAND (first, 0);
5708
19d76e60
RK
5709 if (last)
5710 while ((TREE_CODE (last) == NOP_EXPR
5711 || TREE_CODE (last) == CONVERT_EXPR
5712 || TREE_CODE (last) == NON_LVALUE_EXPR)
5713 && (TYPE_MODE (TREE_TYPE (last))
5714 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (last, 0)))))
665f2503 5715 last = TREE_OPERAND (last, 0);
19d76e60 5716
94ba5069 5717 if (TREE_CODE (first) != INTEGER_CST)
ab87f8c8 5718 error_init ("nonconstant array index in initializer");
94ba5069 5719 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ab87f8c8 5720 error_init ("nonconstant array index in initializer");
8b6a5902 5721 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ab87f8c8 5722 error_init ("array index in non-array initializer");
8b6a5902
JJ
5723 else if (constructor_max_index
5724 && tree_int_cst_lt (constructor_max_index, first))
5725 error_init ("array index in initializer exceeds array bounds");
de520661
RS
5726 else
5727 {
665f2503 5728 constructor_index = convert (bitsizetype, first);
de520661 5729
ce662d4c 5730 if (last)
8b6a5902 5731 {
ce662d4c
JJ
5732 if (tree_int_cst_equal (first, last))
5733 last = 0;
5734 else if (tree_int_cst_lt (last, first))
8b6a5902 5735 {
ce662d4c 5736 error_init ("empty index range in initializer");
8b6a5902
JJ
5737 last = 0;
5738 }
ce662d4c
JJ
5739 else
5740 {
5741 last = convert (bitsizetype, last);
5742 if (constructor_max_index != 0
5743 && tree_int_cst_lt (constructor_max_index, last))
5744 {
5745 error_init ("array index range in initializer exceeds array bounds");
5746 last = 0;
5747 }
5748 }
8b6a5902 5749 }
ce662d4c 5750
8b6a5902
JJ
5751 designator_depth++;
5752 designator_errorneous = 0;
5753 if (constructor_range_stack || last)
5754 push_range_stack (last);
de520661
RS
5755 }
5756}
5757
5758/* Within a struct initializer, specify the next field to be initialized. */
5759
94ba5069 5760void
2f6e4e97 5761set_init_label (tree fieldname)
de520661
RS
5762{
5763 tree tail;
de520661 5764
8b6a5902 5765 if (set_designator (0))
e5cfb88f
RK
5766 return;
5767
8b6a5902
JJ
5768 designator_errorneous = 1;
5769
5770 if (TREE_CODE (constructor_type) != RECORD_TYPE
5771 && TREE_CODE (constructor_type) != UNION_TYPE)
5772 {
5773 error_init ("field name not in record or union initializer");
5774 return;
5775 }
2f6e4e97 5776
de520661
RS
5777 for (tail = TYPE_FIELDS (constructor_type); tail;
5778 tail = TREE_CHAIN (tail))
5779 {
de520661
RS
5780 if (DECL_NAME (tail) == fieldname)
5781 break;
5782 }
5783
5784 if (tail == 0)
5785 error ("unknown field `%s' specified in initializer",
5786 IDENTIFIER_POINTER (fieldname));
de520661 5787 else
8b6a5902
JJ
5788 {
5789 constructor_fields = tail;
5790 designator_depth++;
5791 designator_errorneous = 0;
5792 if (constructor_range_stack)
5793 push_range_stack (NULL_TREE);
5794 }
de520661
RS
5795}
5796\f
e5e809f4 5797/* Add a new initializer to the tree of pending initializers. PURPOSE
2f6e4e97 5798 identifies the initializer, either array index or field in a structure.
e5e809f4
JL
5799 VALUE is the value of that index or field. */
5800
5801static void
2f6e4e97 5802add_pending_init (tree purpose, tree value)
e5e809f4
JL
5803{
5804 struct init_node *p, **q, *r;
5805
5806 q = &constructor_pending_elts;
5807 p = 0;
5808
5809 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
5810 {
5811 while (*q != 0)
5812 {
5813 p = *q;
5814 if (tree_int_cst_lt (purpose, p->purpose))
5815 q = &p->left;
8b6a5902 5816 else if (tree_int_cst_lt (p->purpose, purpose))
e5e809f4
JL
5817 q = &p->right;
5818 else
8b6a5902
JJ
5819 {
5820 if (TREE_SIDE_EFFECTS (p->value))
5821 warning_init ("initialized field with side-effects overwritten");
5822 p->value = value;
5823 return;
5824 }
e5e809f4
JL
5825 }
5826 }
5827 else
5828 {
8b6a5902
JJ
5829 tree bitpos;
5830
5831 bitpos = bit_position (purpose);
e5e809f4
JL
5832 while (*q != NULL)
5833 {
5834 p = *q;
8b6a5902 5835 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
e5e809f4 5836 q = &p->left;
83b091c7 5837 else if (p->purpose != purpose)
e5e809f4
JL
5838 q = &p->right;
5839 else
8b6a5902
JJ
5840 {
5841 if (TREE_SIDE_EFFECTS (p->value))
5842 warning_init ("initialized field with side-effects overwritten");
5843 p->value = value;
5844 return;
5845 }
e5e809f4
JL
5846 }
5847 }
5848
f8a83ee3 5849 r = (struct init_node *) ggc_alloc (sizeof (struct init_node));
e5e809f4
JL
5850 r->purpose = purpose;
5851 r->value = value;
5852
5853 *q = r;
5854 r->parent = p;
5855 r->left = 0;
5856 r->right = 0;
5857 r->balance = 0;
5858
5859 while (p)
5860 {
5861 struct init_node *s;
5862
5863 if (r == p->left)
5864 {
5865 if (p->balance == 0)
5866 p->balance = -1;
5867 else if (p->balance < 0)
5868 {
5869 if (r->balance < 0)
5870 {
ec5c56db 5871 /* L rotation. */
e5e809f4
JL
5872 p->left = r->right;
5873 if (p->left)
5874 p->left->parent = p;
5875 r->right = p;
5876
5877 p->balance = 0;
5878 r->balance = 0;
5879
5880 s = p->parent;
5881 p->parent = r;
5882 r->parent = s;
5883 if (s)
5884 {
5885 if (s->left == p)
5886 s->left = r;
5887 else
5888 s->right = r;
5889 }
5890 else
5891 constructor_pending_elts = r;
5892 }
5893 else
5894 {
ec5c56db 5895 /* LR rotation. */
e5e809f4
JL
5896 struct init_node *t = r->right;
5897
5898 r->right = t->left;
5899 if (r->right)
5900 r->right->parent = r;
5901 t->left = r;
5902
5903 p->left = t->right;
5904 if (p->left)
5905 p->left->parent = p;
5906 t->right = p;
5907
5908 p->balance = t->balance < 0;
5909 r->balance = -(t->balance > 0);
5910 t->balance = 0;
5911
5912 s = p->parent;
5913 p->parent = t;
5914 r->parent = t;
5915 t->parent = s;
5916 if (s)
5917 {
5918 if (s->left == p)
5919 s->left = t;
5920 else
5921 s->right = t;
5922 }
5923 else
5924 constructor_pending_elts = t;
5925 }
5926 break;
5927 }
5928 else
5929 {
5930 /* p->balance == +1; growth of left side balances the node. */
5931 p->balance = 0;
5932 break;
5933 }
5934 }
5935 else /* r == p->right */
5936 {
5937 if (p->balance == 0)
5938 /* Growth propagation from right side. */
5939 p->balance++;
5940 else if (p->balance > 0)
5941 {
5942 if (r->balance > 0)
5943 {
ec5c56db 5944 /* R rotation. */
e5e809f4
JL
5945 p->right = r->left;
5946 if (p->right)
5947 p->right->parent = p;
5948 r->left = p;
5949
5950 p->balance = 0;
5951 r->balance = 0;
5952
5953 s = p->parent;
5954 p->parent = r;
5955 r->parent = s;
5956 if (s)
5957 {
5958 if (s->left == p)
5959 s->left = r;
5960 else
5961 s->right = r;
5962 }
5963 else
5964 constructor_pending_elts = r;
5965 }
5966 else /* r->balance == -1 */
5967 {
5968 /* RL rotation */
5969 struct init_node *t = r->left;
5970
5971 r->left = t->right;
5972 if (r->left)
5973 r->left->parent = r;
5974 t->right = r;
5975
5976 p->right = t->left;
5977 if (p->right)
5978 p->right->parent = p;
5979 t->left = p;
5980
5981 r->balance = (t->balance < 0);
5982 p->balance = -(t->balance > 0);
5983 t->balance = 0;
5984
5985 s = p->parent;
5986 p->parent = t;
5987 r->parent = t;
5988 t->parent = s;
5989 if (s)
5990 {
5991 if (s->left == p)
5992 s->left = t;
5993 else
5994 s->right = t;
5995 }
5996 else
5997 constructor_pending_elts = t;
5998 }
5999 break;
6000 }
6001 else
6002 {
ec5c56db 6003 /* p->balance == -1; growth of right side balances the node. */
e5e809f4
JL
6004 p->balance = 0;
6005 break;
6006 }
6007 }
6008
6009 r = p;
6010 p = p->parent;
6011 }
6012}
6013
8b6a5902 6014/* Build AVL tree from a sorted chain. */
e5e809f4 6015
8b6a5902 6016static void
2f6e4e97 6017set_nonincremental_init (void)
8b6a5902
JJ
6018{
6019 tree chain;
6020
6021 if (TREE_CODE (constructor_type) != RECORD_TYPE
6022 && TREE_CODE (constructor_type) != ARRAY_TYPE)
6023 return;
6024
6025 for (chain = constructor_elements; chain; chain = TREE_CHAIN (chain))
6026 add_pending_init (TREE_PURPOSE (chain), TREE_VALUE (chain));
6027 constructor_elements = 0;
6028 if (TREE_CODE (constructor_type) == RECORD_TYPE)
6029 {
6030 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
6031 /* Skip any nameless bit fields at the beginning. */
6032 while (constructor_unfilled_fields != 0
6033 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6034 && DECL_NAME (constructor_unfilled_fields) == 0)
6035 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
2f6e4e97 6036
8b6a5902
JJ
6037 }
6038 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6039 {
6040 if (TYPE_DOMAIN (constructor_type))
6041 constructor_unfilled_index
6042 = convert (bitsizetype,
6043 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6044 else
6045 constructor_unfilled_index = bitsize_zero_node;
6046 }
6047 constructor_incremental = 0;
6048}
6049
6050/* Build AVL tree from a string constant. */
6051
6052static void
2f6e4e97 6053set_nonincremental_init_from_string (tree str)
8b6a5902
JJ
6054{
6055 tree value, purpose, type;
6056 HOST_WIDE_INT val[2];
6057 const char *p, *end;
6058 int byte, wchar_bytes, charwidth, bitpos;
6059
6060 if (TREE_CODE (constructor_type) != ARRAY_TYPE)
6061 abort ();
6062
6063 if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6064 == TYPE_PRECISION (char_type_node))
6065 wchar_bytes = 1;
6066 else if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str)))
6067 == TYPE_PRECISION (wchar_type_node))
6068 wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
6069 else
6070 abort ();
6071
6072 charwidth = TYPE_PRECISION (char_type_node);
6073 type = TREE_TYPE (constructor_type);
6074 p = TREE_STRING_POINTER (str);
6075 end = p + TREE_STRING_LENGTH (str);
6076
6077 for (purpose = bitsize_zero_node;
6078 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
6079 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
6080 {
6081 if (wchar_bytes == 1)
6082 {
6083 val[1] = (unsigned char) *p++;
6084 val[0] = 0;
6085 }
6086 else
6087 {
6088 val[0] = 0;
6089 val[1] = 0;
6090 for (byte = 0; byte < wchar_bytes; byte++)
6091 {
6092 if (BYTES_BIG_ENDIAN)
6093 bitpos = (wchar_bytes - byte - 1) * charwidth;
6094 else
6095 bitpos = byte * charwidth;
6096 val[bitpos < HOST_BITS_PER_WIDE_INT]
6097 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
6098 << (bitpos % HOST_BITS_PER_WIDE_INT);
6099 }
6100 }
6101
6102 if (!TREE_UNSIGNED (type))
6103 {
6104 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
6105 if (bitpos < HOST_BITS_PER_WIDE_INT)
6106 {
6107 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
6108 {
6109 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
6110 val[0] = -1;
6111 }
6112 }
6113 else if (bitpos == HOST_BITS_PER_WIDE_INT)
6114 {
6115 if (val[1] < 0)
6116 val[0] = -1;
6117 }
6118 else if (val[0] & (((HOST_WIDE_INT) 1)
6119 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
6120 val[0] |= ((HOST_WIDE_INT) -1)
6121 << (bitpos - HOST_BITS_PER_WIDE_INT);
6122 }
6123
6124 value = build_int_2 (val[1], val[0]);
6125 TREE_TYPE (value) = type;
6126 add_pending_init (purpose, value);
6127 }
6128
6129 constructor_incremental = 0;
6130}
6131
6132/* Return value of FIELD in pending initializer or zero if the field was
6133 not initialized yet. */
6134
6135static tree
2f6e4e97 6136find_init_member (tree field)
e5e809f4
JL
6137{
6138 struct init_node *p;
6139
e5e809f4
JL
6140 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6141 {
8b6a5902
JJ
6142 if (constructor_incremental
6143 && tree_int_cst_lt (field, constructor_unfilled_index))
6144 set_nonincremental_init ();
6145
6146 p = constructor_pending_elts;
e5e809f4
JL
6147 while (p)
6148 {
8b6a5902 6149 if (tree_int_cst_lt (field, p->purpose))
e5e809f4 6150 p = p->left;
8b6a5902 6151 else if (tree_int_cst_lt (p->purpose, field))
e5e809f4 6152 p = p->right;
8b6a5902
JJ
6153 else
6154 return p->value;
e5e809f4
JL
6155 }
6156 }
8b6a5902 6157 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 6158 {
8b6a5902
JJ
6159 tree bitpos = bit_position (field);
6160
6161 if (constructor_incremental
6162 && (!constructor_unfilled_fields
6163 || tree_int_cst_lt (bitpos,
6164 bit_position (constructor_unfilled_fields))))
6165 set_nonincremental_init ();
6166
6167 p = constructor_pending_elts;
e5e809f4
JL
6168 while (p)
6169 {
6170 if (field == p->purpose)
8b6a5902
JJ
6171 return p->value;
6172 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
e5e809f4
JL
6173 p = p->left;
6174 else
6175 p = p->right;
6176 }
6177 }
8b6a5902
JJ
6178 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6179 {
6180 if (constructor_elements
6181 && TREE_PURPOSE (constructor_elements) == field)
6182 return TREE_VALUE (constructor_elements);
6183 }
e5e809f4
JL
6184 return 0;
6185}
6186
de520661
RS
6187/* "Output" the next constructor element.
6188 At top level, really output it to assembler code now.
6189 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
6190 TYPE is the data type that the containing data type wants here.
6191 FIELD is the field (a FIELD_DECL) or the index that this element fills.
6192
6193 PENDING if non-nil means output pending elements that belong
6194 right after this element. (PENDING is normally 1;
6195 it is 0 while outputting pending elements, to avoid recursion.) */
6196
34403047 6197static void
2f6e4e97 6198output_init_element (tree value, tree type, tree field, int pending)
de520661 6199{
d5019ba3
R
6200 if (type == error_mark_node)
6201 {
6202 constructor_erroneous = 1;
6203 return;
6204 }
d3ab9753
RS
6205 if (TREE_CODE (TREE_TYPE (value)) == FUNCTION_TYPE
6206 || (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
fd5d5b94
RS
6207 && !(TREE_CODE (value) == STRING_CST
6208 && TREE_CODE (type) == ARRAY_TYPE
6209 && TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE)
1e40eab8 6210 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
d1bd0ded 6211 TYPE_MAIN_VARIANT (type), COMPARE_STRICT)))
d3ab9753
RS
6212 value = default_conversion (value);
6213
e6ecc89b
JJ
6214 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
6215 && require_constant_value && !flag_isoc99 && pending)
6216 {
6217 /* As an extension, allow initializing objects with static storage
6218 duration with compound literals (which are then treated just as
6219 the brace enclosed list they contain). */
6220 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
6221 value = DECL_INITIAL (decl);
6222 }
6223
d3ab9753
RS
6224 if (value == error_mark_node)
6225 constructor_erroneous = 1;
6226 else if (!TREE_CONSTANT (value))
6227 constructor_constant = 0;
4160009f
RK
6228 else if (initializer_constant_valid_p (value, TREE_TYPE (value)) == 0
6229 || ((TREE_CODE (constructor_type) == RECORD_TYPE
6230 || TREE_CODE (constructor_type) == UNION_TYPE)
ef86d2a6
RK
6231 && DECL_C_BIT_FIELD (field)
6232 && TREE_CODE (value) != INTEGER_CST))
d3ab9753
RS
6233 constructor_simple = 0;
6234
de520661
RS
6235 if (require_constant_value && ! TREE_CONSTANT (value))
6236 {
ab87f8c8 6237 error_init ("initializer element is not constant");
de520661
RS
6238 value = error_mark_node;
6239 }
6240 else if (require_constant_elements
6241 && initializer_constant_valid_p (value, TREE_TYPE (value)) == 0)
8b6a5902
JJ
6242 pedwarn ("initializer element is not computable at load time");
6243
6244 /* If this field is empty (and not at the end of structure),
6245 don't do anything other than checking the initializer. */
6246 if (field
6247 && (TREE_TYPE (field) == error_mark_node
6248 || (COMPLETE_TYPE_P (TREE_TYPE (field))
6249 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
6250 && (TREE_CODE (constructor_type) == ARRAY_TYPE
6251 || TREE_CHAIN (field)))))
6252 return;
de520661 6253
273cf2e4 6254 value = digest_init (type, value, require_constant_value);
8b6a5902 6255 if (value == error_mark_node)
de520661 6256 {
8b6a5902
JJ
6257 constructor_erroneous = 1;
6258 return;
400fbf9f 6259 }
400fbf9f 6260
de520661
RS
6261 /* If this element doesn't come next in sequence,
6262 put it on constructor_pending_elts. */
6263 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8b6a5902
JJ
6264 && (!constructor_incremental
6265 || !tree_int_cst_equal (field, constructor_unfilled_index)))
de520661 6266 {
8b6a5902
JJ
6267 if (constructor_incremental
6268 && tree_int_cst_lt (field, constructor_unfilled_index))
6269 set_nonincremental_init ();
6270
822baa84 6271 add_pending_init (field, value);
8b6a5902 6272 return;
de520661 6273 }
76aaaae2 6274 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8b6a5902
JJ
6275 && (!constructor_incremental
6276 || field != constructor_unfilled_fields))
de520661 6277 {
76aaaae2
RS
6278 /* We do this for records but not for unions. In a union,
6279 no matter which field is specified, it can be initialized
6280 right away since it starts at the beginning of the union. */
8b6a5902
JJ
6281 if (constructor_incremental)
6282 {
6283 if (!constructor_unfilled_fields)
6284 set_nonincremental_init ();
6285 else
6286 {
6287 tree bitpos, unfillpos;
6288
6289 bitpos = bit_position (field);
6290 unfillpos = bit_position (constructor_unfilled_fields);
6291
6292 if (tree_int_cst_lt (bitpos, unfillpos))
6293 set_nonincremental_init ();
6294 }
6295 }
6296
822baa84 6297 add_pending_init (field, value);
8b6a5902 6298 return;
de520661 6299 }
8b6a5902
JJ
6300 else if (TREE_CODE (constructor_type) == UNION_TYPE
6301 && constructor_elements)
de520661 6302 {
8b6a5902
JJ
6303 if (TREE_SIDE_EFFECTS (TREE_VALUE (constructor_elements)))
6304 warning_init ("initialized field with side-effects overwritten");
400fbf9f 6305
8b6a5902
JJ
6306 /* We can have just one union field set. */
6307 constructor_elements = 0;
6308 }
c2f4acb7 6309
8b6a5902
JJ
6310 /* Otherwise, output this element either to
6311 constructor_elements or to the assembler file. */
6312
6313 if (field && TREE_CODE (field) == INTEGER_CST)
6314 field = copy_node (field);
6315 constructor_elements
822baa84 6316 = tree_cons (field, value, constructor_elements);
8b6a5902
JJ
6317
6318 /* Advance the variable that indicates sequential elements output. */
6319 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6320 constructor_unfilled_index
6321 = size_binop (PLUS_EXPR, constructor_unfilled_index,
6322 bitsize_one_node);
6323 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
6324 {
6325 constructor_unfilled_fields
6326 = TREE_CHAIN (constructor_unfilled_fields);
de520661 6327
8b6a5902
JJ
6328 /* Skip any nameless bit fields. */
6329 while (constructor_unfilled_fields != 0
6330 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6331 && DECL_NAME (constructor_unfilled_fields) == 0)
6332 constructor_unfilled_fields =
6333 TREE_CHAIN (constructor_unfilled_fields);
de520661 6334 }
8b6a5902
JJ
6335 else if (TREE_CODE (constructor_type) == UNION_TYPE)
6336 constructor_unfilled_fields = 0;
6337
6338 /* Now output any pending elements which have become next. */
6339 if (pending)
6340 output_pending_init_elements (0);
de520661 6341}
400fbf9f 6342
de520661
RS
6343/* Output any pending elements which have become next.
6344 As we output elements, constructor_unfilled_{fields,index}
6345 advances, which may cause other elements to become next;
6346 if so, they too are output.
6347
6348 If ALL is 0, we return when there are
6349 no more pending elements to output now.
6350
6351 If ALL is 1, we output space as necessary so that
6352 we can output all the pending elements. */
6353
6354static void
2f6e4e97 6355output_pending_init_elements (int all)
de520661 6356{
e5e809f4 6357 struct init_node *elt = constructor_pending_elts;
de520661
RS
6358 tree next;
6359
6360 retry:
6361
e5e809f4 6362 /* Look thru the whole pending tree.
de520661
RS
6363 If we find an element that should be output now,
6364 output it. Otherwise, set NEXT to the element
6365 that comes first among those still pending. */
2f6e4e97 6366
de520661 6367 next = 0;
e5e809f4 6368 while (elt)
de520661
RS
6369 {
6370 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6371 {
e5e809f4 6372 if (tree_int_cst_equal (elt->purpose,
de520661 6373 constructor_unfilled_index))
e5e809f4
JL
6374 output_init_element (elt->value,
6375 TREE_TYPE (constructor_type),
6376 constructor_unfilled_index, 0);
6377 else if (tree_int_cst_lt (constructor_unfilled_index,
6378 elt->purpose))
400fbf9f 6379 {
e5e809f4
JL
6380 /* Advance to the next smaller node. */
6381 if (elt->left)
6382 elt = elt->left;
6383 else
6384 {
6385 /* We have reached the smallest node bigger than the
6386 current unfilled index. Fill the space first. */
6387 next = elt->purpose;
6388 break;
6389 }
6390 }
6391 else
6392 {
6393 /* Advance to the next bigger node. */
6394 if (elt->right)
6395 elt = elt->right;
6396 else
6397 {
6398 /* We have reached the biggest node in a subtree. Find
6399 the parent of it, which is the next bigger node. */
6400 while (elt->parent && elt->parent->right == elt)
6401 elt = elt->parent;
6402 elt = elt->parent;
6403 if (elt && tree_int_cst_lt (constructor_unfilled_index,
6404 elt->purpose))
6405 {
6406 next = elt->purpose;
6407 break;
6408 }
6409 }
de520661 6410 }
de520661
RS
6411 }
6412 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6413 || TREE_CODE (constructor_type) == UNION_TYPE)
6414 {
8b6a5902
JJ
6415 tree ctor_unfilled_bitpos, elt_bitpos;
6416
e5e809f4
JL
6417 /* If the current record is complete we are done. */
6418 if (constructor_unfilled_fields == 0)
6419 break;
8b6a5902
JJ
6420
6421 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
6422 elt_bitpos = bit_position (elt->purpose);
6423 /* We can't compare fields here because there might be empty
6424 fields in between. */
6425 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
de520661 6426 {
8b6a5902
JJ
6427 constructor_unfilled_fields = elt->purpose;
6428 output_init_element (elt->value, TREE_TYPE (elt->purpose),
6429 elt->purpose, 0);
400fbf9f 6430 }
8b6a5902 6431 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
e5e809f4
JL
6432 {
6433 /* Advance to the next smaller node. */
6434 if (elt->left)
6435 elt = elt->left;
6436 else
6437 {
6438 /* We have reached the smallest node bigger than the
6439 current unfilled field. Fill the space first. */
6440 next = elt->purpose;
6441 break;
6442 }
6443 }
6444 else
6445 {
6446 /* Advance to the next bigger node. */
6447 if (elt->right)
6448 elt = elt->right;
6449 else
6450 {
6451 /* We have reached the biggest node in a subtree. Find
6452 the parent of it, which is the next bigger node. */
6453 while (elt->parent && elt->parent->right == elt)
6454 elt = elt->parent;
6455 elt = elt->parent;
6456 if (elt
8b6a5902
JJ
6457 && (tree_int_cst_lt (ctor_unfilled_bitpos,
6458 bit_position (elt->purpose))))
e5e809f4
JL
6459 {
6460 next = elt->purpose;
6461 break;
6462 }
6463 }
6464 }
400fbf9f 6465 }
de520661
RS
6466 }
6467
6468 /* Ordinarily return, but not if we want to output all
6469 and there are elements left. */
6470 if (! (all && next != 0))
6471 return;
6472
229a2e65
MM
6473 /* If it's not incremental, just skip over the gap, so that after
6474 jumping to retry we will output the next successive element. */
6475 if (TREE_CODE (constructor_type) == RECORD_TYPE
6476 || TREE_CODE (constructor_type) == UNION_TYPE)
6477 constructor_unfilled_fields = next;
6478 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6479 constructor_unfilled_index = next;
de520661 6480
e5e809f4
JL
6481 /* ELT now points to the node in the pending tree with the next
6482 initializer to output. */
de520661
RS
6483 goto retry;
6484}
6485\f
6486/* Add one non-braced element to the current constructor level.
6487 This adjusts the current position within the constructor's type.
6488 This may also start or terminate implicit levels
6489 to handle a partly-braced initializer.
6490
6491 Once this has found the correct level for the new element,
229a2e65 6492 it calls output_init_element. */
de520661
RS
6493
6494void
2f6e4e97 6495process_init_element (tree value)
de520661 6496{
b62acd60
RS
6497 tree orig_value = value;
6498 int string_flag = value != 0 && TREE_CODE (value) == STRING_CST;
6499
8b6a5902
JJ
6500 designator_depth = 0;
6501 designator_errorneous = 0;
6502
790e9490
RS
6503 /* Handle superfluous braces around string cst as in
6504 char x[] = {"foo"}; */
6505 if (string_flag
d27c148b 6506 && constructor_type
790e9490 6507 && TREE_CODE (constructor_type) == ARRAY_TYPE
61e215dd 6508 && TREE_CODE (TREE_TYPE (constructor_type)) == INTEGER_TYPE
790e9490
RS
6509 && integer_zerop (constructor_unfilled_index))
6510 {
d739a3bc
NS
6511 if (constructor_stack->replacement_value)
6512 error_init ("excess elements in char array initializer");
790e9490
RS
6513 constructor_stack->replacement_value = value;
6514 return;
6515 }
6516
790e9490
RS
6517 if (constructor_stack->replacement_value != 0)
6518 {
ab87f8c8 6519 error_init ("excess elements in struct initializer");
790e9490
RS
6520 return;
6521 }
6522
91fa3c30
RS
6523 /* Ignore elements of a brace group if it is entirely superfluous
6524 and has already been diagnosed. */
6525 if (constructor_type == 0)
6526 return;
6527
de520661
RS
6528 /* If we've exhausted any levels that didn't have braces,
6529 pop them now. */
6530 while (constructor_stack->implicit)
6531 {
6532 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6533 || TREE_CODE (constructor_type) == UNION_TYPE)
6534 && constructor_fields == 0)
6535 process_init_element (pop_init_level (1));
6536 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
ec0bc8b6
RK
6537 && (constructor_max_index == 0
6538 || tree_int_cst_lt (constructor_max_index,
6539 constructor_index)))
de520661 6540 process_init_element (pop_init_level (1));
fe67cf58 6541 else
de520661 6542 break;
400fbf9f
JW
6543 }
6544
8b6a5902
JJ
6545 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
6546 if (constructor_range_stack)
7c94ce7f
JJ
6547 {
6548 /* If value is a compound literal and we'll be just using its
6549 content, don't put it into a SAVE_EXPR. */
6550 if (TREE_CODE (value) != COMPOUND_LITERAL_EXPR
6551 || !require_constant_value
6552 || flag_isoc99)
6553 value = save_expr (value);
6554 }
8b6a5902 6555
de520661 6556 while (1)
400fbf9f 6557 {
de520661 6558 if (TREE_CODE (constructor_type) == RECORD_TYPE)
400fbf9f 6559 {
de520661
RS
6560 tree fieldtype;
6561 enum tree_code fieldcode;
6562
6563 if (constructor_fields == 0)
6564 {
ab87f8c8 6565 pedwarn_init ("excess elements in struct initializer");
de520661
RS
6566 break;
6567 }
6568
1d33b2a9
JW
6569 fieldtype = TREE_TYPE (constructor_fields);
6570 if (fieldtype != error_mark_node)
6571 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
de520661
RS
6572 fieldcode = TREE_CODE (fieldtype);
6573
eba80994
EB
6574 /* Error for non-static initialization of a flexible array member. */
6575 if (fieldcode == ARRAY_TYPE
6576 && !require_constant_value
6577 && TYPE_SIZE (fieldtype) == NULL_TREE
6578 && TREE_CHAIN (constructor_fields) == NULL_TREE)
6579 {
6580 error_init ("non-static initialization of a flexible array member");
6581 break;
6582 }
6583
b62acd60
RS
6584 /* Accept a string constant to initialize a subarray. */
6585 if (value != 0
6586 && fieldcode == ARRAY_TYPE
6587 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6588 && string_flag)
6589 value = orig_value;
6590 /* Otherwise, if we have come to a subaggregate,
6591 and we don't have an element of its type, push into it. */
cc77d4d5 6592 else if (value != 0 && !constructor_no_implicit
ee7204ee 6593 && value != error_mark_node
b62acd60
RS
6594 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6595 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6596 || fieldcode == UNION_TYPE))
de520661
RS
6597 {
6598 push_init_level (1);
6599 continue;
6600 }
6601
6602 if (value)
6603 {
19d76e60 6604 push_member_name (constructor_fields);
de520661
RS
6605 output_init_element (value, fieldtype, constructor_fields, 1);
6606 RESTORE_SPELLING_DEPTH (constructor_depth);
6607 }
6608 else
b62acd60
RS
6609 /* Do the bookkeeping for an element that was
6610 directly output as a constructor. */
6611 {
6612 /* For a record, keep track of end position of last field. */
ffc5c6a9
RH
6613 if (DECL_SIZE (constructor_fields))
6614 constructor_bit_index
6615 = size_binop (PLUS_EXPR,
6616 bit_position (constructor_fields),
6617 DECL_SIZE (constructor_fields));
b62acd60 6618
bbb818c6
EB
6619 /* If the current field was the first one not yet written out,
6620 it isn't now, so update. */
6621 if (constructor_unfilled_fields == constructor_fields)
6622 {
6623 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6624 /* Skip any nameless bit fields. */
6625 while (constructor_unfilled_fields != 0
6626 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
6627 && DECL_NAME (constructor_unfilled_fields) == 0)
6628 constructor_unfilled_fields =
6629 TREE_CHAIN (constructor_unfilled_fields);
6630 }
b62acd60 6631 }
de520661
RS
6632
6633 constructor_fields = TREE_CHAIN (constructor_fields);
abc95ed3 6634 /* Skip any nameless bit fields at the beginning. */
ef86d2a6
RK
6635 while (constructor_fields != 0
6636 && DECL_C_BIT_FIELD (constructor_fields)
fc623854
RS
6637 && DECL_NAME (constructor_fields) == 0)
6638 constructor_fields = TREE_CHAIN (constructor_fields);
400fbf9f 6639 }
8b6a5902 6640 else if (TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 6641 {
de520661
RS
6642 tree fieldtype;
6643 enum tree_code fieldcode;
6644
6645 if (constructor_fields == 0)
6646 {
ab87f8c8 6647 pedwarn_init ("excess elements in union initializer");
de520661
RS
6648 break;
6649 }
6650
1d33b2a9
JW
6651 fieldtype = TREE_TYPE (constructor_fields);
6652 if (fieldtype != error_mark_node)
6653 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
de520661
RS
6654 fieldcode = TREE_CODE (fieldtype);
6655
253b6b82
KG
6656 /* Warn that traditional C rejects initialization of unions.
6657 We skip the warning if the value is zero. This is done
6658 under the assumption that the zero initializer in user
6659 code appears conditioned on e.g. __STDC__ to avoid
6660 "missing initializer" warnings and relies on default
b621a4dd
ZW
6661 initialization to zero in the traditional C case.
6662 We also skip the warning if the initializer is designated,
6663 again on the assumption that this must be conditional on
6664 __STDC__ anyway (and we've already complained about the
6665 member-designator already). */
6666 if (warn_traditional && !in_system_header && !constructor_designated
32892c52 6667 && !(value && (integer_zerop (value) || real_zerop (value))))
253b6b82
KG
6668 warning ("traditional C rejects initialization of unions");
6669
b62acd60
RS
6670 /* Accept a string constant to initialize a subarray. */
6671 if (value != 0
6672 && fieldcode == ARRAY_TYPE
6673 && TREE_CODE (TREE_TYPE (fieldtype)) == INTEGER_TYPE
6674 && string_flag)
6675 value = orig_value;
6676 /* Otherwise, if we have come to a subaggregate,
6677 and we don't have an element of its type, push into it. */
cc77d4d5 6678 else if (value != 0 && !constructor_no_implicit
ee7204ee 6679 && value != error_mark_node
b62acd60
RS
6680 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != fieldtype
6681 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
6682 || fieldcode == UNION_TYPE))
de520661
RS
6683 {
6684 push_init_level (1);
6685 continue;
6686 }
6687
6688 if (value)
6689 {
19d76e60 6690 push_member_name (constructor_fields);
de520661
RS
6691 output_init_element (value, fieldtype, constructor_fields, 1);
6692 RESTORE_SPELLING_DEPTH (constructor_depth);
6693 }
6694 else
94ba5069
RS
6695 /* Do the bookkeeping for an element that was
6696 directly output as a constructor. */
6697 {
665f2503 6698 constructor_bit_index = DECL_SIZE (constructor_fields);
94ba5069
RS
6699 constructor_unfilled_fields = TREE_CHAIN (constructor_fields);
6700 }
de520661
RS
6701
6702 constructor_fields = 0;
400fbf9f 6703 }
8b6a5902 6704 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661
RS
6705 {
6706 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6707 enum tree_code eltcode = TREE_CODE (elttype);
6708
b62acd60
RS
6709 /* Accept a string constant to initialize a subarray. */
6710 if (value != 0
6711 && eltcode == ARRAY_TYPE
6712 && TREE_CODE (TREE_TYPE (elttype)) == INTEGER_TYPE
6713 && string_flag)
6714 value = orig_value;
6715 /* Otherwise, if we have come to a subaggregate,
6716 and we don't have an element of its type, push into it. */
cc77d4d5 6717 else if (value != 0 && !constructor_no_implicit
ee7204ee 6718 && value != error_mark_node
b62acd60
RS
6719 && TYPE_MAIN_VARIANT (TREE_TYPE (value)) != elttype
6720 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
6721 || eltcode == UNION_TYPE))
de520661
RS
6722 {
6723 push_init_level (1);
6724 continue;
6725 }
6726
6727 if (constructor_max_index != 0
4b606faf
WC
6728 && (tree_int_cst_lt (constructor_max_index, constructor_index)
6729 || integer_all_onesp (constructor_max_index)))
de520661 6730 {
ab87f8c8 6731 pedwarn_init ("excess elements in array initializer");
de520661
RS
6732 break;
6733 }
400fbf9f 6734
8b6a5902
JJ
6735 /* Now output the actual element. */
6736 if (value)
ee2990e7 6737 {
8b6a5902
JJ
6738 push_array_bounds (tree_low_cst (constructor_index, 0));
6739 output_init_element (value, elttype, constructor_index, 1);
6740 RESTORE_SPELLING_DEPTH (constructor_depth);
ee2990e7 6741 }
333a5dae 6742
8b6a5902
JJ
6743 constructor_index
6744 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
400fbf9f 6745
8b6a5902
JJ
6746 if (! value)
6747 /* If we are doing the bookkeeping for an element that was
6748 directly output as a constructor, we must update
6749 constructor_unfilled_index. */
6750 constructor_unfilled_index = constructor_index;
de520661 6751 }
e6834654
SS
6752 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6753 {
6754 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
6755
6756 /* Do a basic check of initializer size. Note that vectors
6757 always have a fixed size derived from their type. */
6758 if (tree_int_cst_lt (constructor_max_index, constructor_index))
6759 {
6760 pedwarn_init ("excess elements in vector initializer");
6761 break;
6762 }
6763
6764 /* Now output the actual element. */
6765 if (value)
6766 output_init_element (value, elttype, constructor_index, 1);
6767
6768 constructor_index
6769 = size_binop (PLUS_EXPR, constructor_index, bitsize_one_node);
6770
6771 if (! value)
6772 /* If we are doing the bookkeeping for an element that was
6773 directly output as a constructor, we must update
6774 constructor_unfilled_index. */
6775 constructor_unfilled_index = constructor_index;
6776 }
de520661
RS
6777
6778 /* Handle the sole element allowed in a braced initializer
6779 for a scalar variable. */
8b6a5902 6780 else if (constructor_fields == 0)
de520661 6781 {
ab87f8c8 6782 pedwarn_init ("excess elements in scalar initializer");
de520661
RS
6783 break;
6784 }
8b6a5902
JJ
6785 else
6786 {
6787 if (value)
6788 output_init_element (value, constructor_type, NULL_TREE, 1);
6789 constructor_fields = 0;
6790 }
6791
6792 /* Handle range initializers either at this level or anywhere higher
6793 in the designator stack. */
6794 if (constructor_range_stack)
6795 {
6796 struct constructor_range_stack *p, *range_stack;
6797 int finish = 0;
6798
6799 range_stack = constructor_range_stack;
6800 constructor_range_stack = 0;
6801 while (constructor_stack != range_stack->stack)
6802 {
6803 if (!constructor_stack->implicit)
6804 abort ();
6805 process_init_element (pop_init_level (1));
6806 }
6807 for (p = range_stack;
6808 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
6809 p = p->prev)
6810 {
6811 if (!constructor_stack->implicit)
6812 abort ();
6813 process_init_element (pop_init_level (1));
6814 }
6815
6816 p->index = size_binop (PLUS_EXPR, p->index, bitsize_one_node);
6817 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
6818 finish = 1;
6819
6820 while (1)
6821 {
6822 constructor_index = p->index;
6823 constructor_fields = p->fields;
6824 if (finish && p->range_end && p->index == p->range_start)
6825 {
6826 finish = 0;
6827 p->prev = 0;
6828 }
6829 p = p->next;
6830 if (!p)
6831 break;
6832 push_init_level (2);
6833 p->stack = constructor_stack;
6834 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
6835 p->index = p->range_start;
6836 }
6837
6838 if (!finish)
6839 constructor_range_stack = range_stack;
6840 continue;
6841 }
de520661 6842
de520661 6843 break;
fe67cf58 6844 }
8b6a5902
JJ
6845
6846 constructor_range_stack = 0;
400fbf9f
JW
6847}
6848\f
15b732b2
NB
6849/* Build a simple asm-statement, from one string literal. */
6850tree
2f6e4e97 6851simple_asm_stmt (tree expr)
15b732b2 6852{
a23c9413
NB
6853 STRIP_NOPS (expr);
6854
6855 if (TREE_CODE (expr) == ADDR_EXPR)
6856 expr = TREE_OPERAND (expr, 0);
15b732b2 6857
a23c9413
NB
6858 if (TREE_CODE (expr) == STRING_CST)
6859 {
6860 tree stmt;
6861
4c46ea23
EB
6862 /* Simple asm statements are treated as volatile. */
6863 stmt = add_stmt (build_stmt (ASM_STMT, ridpointers[(int) RID_VOLATILE],
6864 expr, NULL_TREE, NULL_TREE, NULL_TREE));
a23c9413
NB
6865 ASM_INPUT_P (stmt) = 1;
6866 return stmt;
6867 }
6868
6869 error ("argument of `asm' is not a constant string");
6870 return NULL_TREE;
15b732b2
NB
6871}
6872
64094f6a
RH
6873/* Build an asm-statement, whose components are a CV_QUALIFIER, a
6874 STRING, some OUTPUTS, some INPUTS, and some CLOBBERS. */
400fbf9f 6875
64094f6a 6876tree
f55ade6e
AJ
6877build_asm_stmt (tree cv_qualifier, tree string, tree outputs, tree inputs,
6878 tree clobbers)
400fbf9f 6879{
64094f6a 6880 tree tail;
400fbf9f 6881
a23c9413
NB
6882 if (TREE_CODE (string) != STRING_CST)
6883 {
6884 error ("asm template is not a string constant");
6885 return NULL_TREE;
6886 }
6887
64094f6a
RH
6888 if (cv_qualifier != NULL_TREE
6889 && cv_qualifier != ridpointers[(int) RID_VOLATILE])
6890 {
6891 warning ("%s qualifier ignored on asm",
6892 IDENTIFIER_POINTER (cv_qualifier));
6893 cv_qualifier = NULL_TREE;
6894 }
6895
6896 /* We can remove output conversions that change the type,
6897 but not the mode. */
6898 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
c5c76735
JL
6899 {
6900 tree output = TREE_VALUE (tail);
6901
c5c76735 6902 STRIP_NOPS (output);
64094f6a 6903 TREE_VALUE (tail) = output;
c5c76735
JL
6904
6905 /* Allow conversions as LHS here. build_modify_expr as called below
6906 will do the right thing with them. */
6907 while (TREE_CODE (output) == NOP_EXPR
6908 || TREE_CODE (output) == CONVERT_EXPR
6909 || TREE_CODE (output) == FLOAT_EXPR
6910 || TREE_CODE (output) == FIX_TRUNC_EXPR
6911 || TREE_CODE (output) == FIX_FLOOR_EXPR
6912 || TREE_CODE (output) == FIX_ROUND_EXPR
6913 || TREE_CODE (output) == FIX_CEIL_EXPR)
1bef1e7c 6914 output = TREE_OPERAND (output, 0);
c5c76735 6915
64094f6a
RH
6916 lvalue_or_else (TREE_VALUE (tail), "invalid lvalue in asm statement");
6917 }
6918
6919 /* Remove output conversions that change the type but not the mode. */
6920 for (tail = outputs; tail; tail = TREE_CHAIN (tail))
6921 {
6922 tree output = TREE_VALUE (tail);
6923 STRIP_NOPS (output);
6924 TREE_VALUE (tail) = output;
c5c76735 6925 }
400fbf9f 6926
2f6e4e97 6927 /* Perform default conversions on array and function inputs.
64094f6a
RH
6928 Don't do this for other types as it would screw up operands
6929 expected to be in memory. */
6930 for (tail = inputs; tail; tail = TREE_CHAIN (tail))
207bf485 6931 TREE_VALUE (tail) = default_function_array_conversion (TREE_VALUE (tail));
400fbf9f 6932
64094f6a
RH
6933 return add_stmt (build_stmt (ASM_STMT, cv_qualifier, string,
6934 outputs, inputs, clobbers));
6935}
6936
6937/* Expand an ASM statement with operands, handling output operands
6938 that are not variables or INDIRECT_REFS by transforming such
6939 cases into cases that expand_asm_operands can handle.
6940
6941 Arguments are same as for expand_asm_operands. */
6942
6943void
2f6e4e97
AJ
6944c_expand_asm_operands (tree string, tree outputs, tree inputs,
6945 tree clobbers, int vol, const char *filename,
6946 int line)
64094f6a
RH
6947{
6948 int noutputs = list_length (outputs);
b3694847 6949 int i;
64094f6a 6950 /* o[I] is the place that output number I should be written. */
b3694847
SS
6951 tree *o = (tree *) alloca (noutputs * sizeof (tree));
6952 tree tail;
64094f6a
RH
6953
6954 /* Record the contents of OUTPUTS before it is modified. */
6955 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
c9fe6f9f
RS
6956 {
6957 o[i] = TREE_VALUE (tail);
6958 if (o[i] == error_mark_node)
6959 return;
6960 }
8f17b5c5 6961
64094f6a
RH
6962 /* Generate the ASM_OPERANDS insn; store into the TREE_VALUEs of
6963 OUTPUTS some trees for where the values were actually stored. */
400fbf9f
JW
6964 expand_asm_operands (string, outputs, inputs, clobbers, vol, filename, line);
6965
6966 /* Copy all the intermediate outputs into the specified outputs. */
6967 for (i = 0, tail = outputs; tail; tail = TREE_CHAIN (tail), i++)
6968 {
6969 if (o[i] != TREE_VALUE (tail))
6970 {
6971 expand_expr (build_modify_expr (o[i], NOP_EXPR, TREE_VALUE (tail)),
f5a8bfff 6972 NULL_RTX, VOIDmode, EXPAND_NORMAL);
400fbf9f 6973 free_temp_slots ();
39b726dd
RH
6974
6975 /* Restore the original value so that it's correct the next
6976 time we expand this function. */
6977 TREE_VALUE (tail) = o[i];
400fbf9f
JW
6978 }
6979 /* Detect modification of read-only values.
6980 (Otherwise done by build_modify_expr.) */
6981 else
6982 {
6983 tree type = TREE_TYPE (o[i]);
a43ea319
RK
6984 if (TREE_READONLY (o[i])
6985 || TYPE_READONLY (type)
400fbf9f
JW
6986 || ((TREE_CODE (type) == RECORD_TYPE
6987 || TREE_CODE (type) == UNION_TYPE)
6988 && C_TYPE_FIELDS_READONLY (type)))
6989 readonly_warning (o[i], "modification by `asm'");
6990 }
6991 }
6992
6993 /* Those MODIFY_EXPRs could do autoincrements. */
6994 emit_queue ();
6995}
6996\f
6997/* Expand a C `return' statement.
6998 RETVAL is the expression for what to return,
6999 or a null pointer for `return;' with no value. */
7000
64094f6a 7001tree
2f6e4e97 7002c_expand_return (tree retval)
400fbf9f
JW
7003{
7004 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl));
7005
7006 if (TREE_THIS_VOLATILE (current_function_decl))
08bf538e 7007 warning ("function declared `noreturn' has a `return' statement");
400fbf9f
JW
7008
7009 if (!retval)
7010 {
7011 current_function_returns_null = 1;
903f51d9
RH
7012 if ((warn_return_type || flag_isoc99)
7013 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
7014 pedwarn_c99 ("`return' with no value, in function returning non-void");
400fbf9f
JW
7015 }
7016 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
7017 {
7018 current_function_returns_null = 1;
7019 if (pedantic || TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
7020 pedwarn ("`return' with a value, in function returning void");
400fbf9f
JW
7021 }
7022 else
7023 {
ab87f8c8 7024 tree t = convert_for_assignment (valtype, retval, _("return"),
9b7267b8 7025 NULL_TREE, NULL_TREE, 0);
400fbf9f 7026 tree res = DECL_RESULT (current_function_decl);
88a3dbc1 7027 tree inner;
70768eda 7028
5ce89b2e 7029 current_function_returns_value = 1;
70768eda 7030 if (t == error_mark_node)
64094f6a 7031 return NULL_TREE;
70768eda 7032
88a3dbc1
RK
7033 inner = t = convert (TREE_TYPE (res), t);
7034
7035 /* Strip any conversions, additions, and subtractions, and see if
7036 we are returning the address of a local variable. Warn if so. */
abe80e6d 7037 while (1)
88a3dbc1 7038 {
abe80e6d
RK
7039 switch (TREE_CODE (inner))
7040 {
7041 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
7042 case PLUS_EXPR:
7043 inner = TREE_OPERAND (inner, 0);
7044 continue;
7045
7046 case MINUS_EXPR:
7047 /* If the second operand of the MINUS_EXPR has a pointer
7048 type (or is converted from it), this may be valid, so
7049 don't give a warning. */
7050 {
7051 tree op1 = TREE_OPERAND (inner, 1);
7052
7053 while (! POINTER_TYPE_P (TREE_TYPE (op1))
7054 && (TREE_CODE (op1) == NOP_EXPR
7055 || TREE_CODE (op1) == NON_LVALUE_EXPR
7056 || TREE_CODE (op1) == CONVERT_EXPR))
7057 op1 = TREE_OPERAND (op1, 0);
7058
7059 if (POINTER_TYPE_P (TREE_TYPE (op1)))
7060 break;
88a3dbc1 7061
abe80e6d
RK
7062 inner = TREE_OPERAND (inner, 0);
7063 continue;
7064 }
2f6e4e97 7065
abe80e6d
RK
7066 case ADDR_EXPR:
7067 inner = TREE_OPERAND (inner, 0);
88a3dbc1 7068
abe80e6d
RK
7069 while (TREE_CODE_CLASS (TREE_CODE (inner)) == 'r')
7070 inner = TREE_OPERAND (inner, 0);
7071
7072 if (TREE_CODE (inner) == VAR_DECL
7073 && ! DECL_EXTERNAL (inner)
7074 && ! TREE_STATIC (inner)
7075 && DECL_CONTEXT (inner) == current_function_decl)
7076 warning ("function returns address of local variable");
7077 break;
e9a25f70
JL
7078
7079 default:
7080 break;
abe80e6d
RK
7081 }
7082
7083 break;
88a3dbc1
RK
7084 }
7085
56cb9733 7086 retval = build (MODIFY_EXPR, TREE_TYPE (res), res, t);
400fbf9f 7087 }
56cb9733 7088
64094f6a 7089 return add_stmt (build_return_stmt (retval));
400fbf9f
JW
7090}
7091\f
8f17b5c5
MM
7092struct c_switch {
7093 /* The SWITCH_STMT being built. */
7094 tree switch_stmt;
7095 /* A splay-tree mapping the low element of a case range to the high
7096 element, or NULL_TREE if there is no high element. Used to
7097 determine whether or not a new case label duplicates an old case
7098 label. We need a tree, rather than simply a hash table, because
7099 of the GNU case range extension. */
7100 splay_tree cases;
7101 /* The next node on the stack. */
7102 struct c_switch *next;
7103};
7104
7105/* A stack of the currently active switch statements. The innermost
7106 switch statement is on the top of the stack. There is no need to
7107 mark the stack for garbage collection because it is only active
7108 during the processing of the body of a function, and we never
7109 collect at that point. */
7110
7111static struct c_switch *switch_stack;
7112
7113/* Start a C switch statement, testing expression EXP. Return the new
7114 SWITCH_STMT. */
400fbf9f
JW
7115
7116tree
2f6e4e97 7117c_start_case (tree exp)
400fbf9f 7118{
b3694847 7119 enum tree_code code;
6f9fdf4d 7120 tree type, orig_type = error_mark_node;
8f17b5c5 7121 struct c_switch *cs;
e89a9554 7122
8f17b5c5
MM
7123 if (exp != error_mark_node)
7124 {
7125 code = TREE_CODE (TREE_TYPE (exp));
6f9fdf4d 7126 orig_type = TREE_TYPE (exp);
e89a9554 7127
6f9fdf4d 7128 if (! INTEGRAL_TYPE_P (orig_type)
8f17b5c5
MM
7129 && code != ERROR_MARK)
7130 {
7131 error ("switch quantity not an integer");
7132 exp = integer_zero_node;
7133 }
7134 else
7135 {
8f17b5c5 7136 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
400fbf9f 7137
8f17b5c5
MM
7138 if (warn_traditional && !in_system_header
7139 && (type == long_integer_type_node
7140 || type == long_unsigned_type_node))
7141 warning ("`long' switch expression not converted to `int' in ISO C");
7142
7143 exp = default_conversion (exp);
7144 type = TREE_TYPE (exp);
8f17b5c5 7145 }
400fbf9f 7146 }
8f17b5c5
MM
7147
7148 /* Add this new SWITCH_STMT to the stack. */
2ac86f90 7149 cs = (struct c_switch *) xmalloc (sizeof (*cs));
6f9fdf4d 7150 cs->switch_stmt = build_stmt (SWITCH_STMT, exp, NULL_TREE, orig_type);
8f17b5c5
MM
7151 cs->cases = splay_tree_new (case_compare, NULL, NULL);
7152 cs->next = switch_stack;
7153 switch_stack = cs;
7154
7155 return add_stmt (switch_stack->switch_stmt);
7156}
7157
7158/* Process a case label. */
7159
64094f6a 7160tree
2f6e4e97 7161do_case (tree low_value, tree high_value)
8f17b5c5 7162{
64094f6a
RH
7163 tree label = NULL_TREE;
7164
8f17b5c5 7165 if (switch_stack)
64094f6a 7166 {
2ad1815d
EB
7167 bool switch_was_empty_p = (SWITCH_BODY (switch_stack->switch_stmt) == NULL_TREE);
7168
2f6e4e97
AJ
7169 label = c_add_case_label (switch_stack->cases,
7170 SWITCH_COND (switch_stack->switch_stmt),
64094f6a
RH
7171 low_value, high_value);
7172 if (label == error_mark_node)
7173 label = NULL_TREE;
2ad1815d
EB
7174 else if (switch_was_empty_p)
7175 {
7176 /* Attach the first case label to the SWITCH_BODY. */
7177 SWITCH_BODY (switch_stack->switch_stmt) = TREE_CHAIN (switch_stack->switch_stmt);
7178 TREE_CHAIN (switch_stack->switch_stmt) = NULL_TREE;
7179 }
64094f6a 7180 }
8f17b5c5
MM
7181 else if (low_value)
7182 error ("case label not within a switch statement");
400fbf9f 7183 else
8f17b5c5 7184 error ("`default' label not within a switch statement");
64094f6a
RH
7185
7186 return label;
8f17b5c5 7187}
400fbf9f 7188
8f17b5c5 7189/* Finish the switch statement. */
400fbf9f 7190
8f17b5c5 7191void
2f6e4e97 7192c_finish_case (void)
8f17b5c5
MM
7193{
7194 struct c_switch *cs = switch_stack;
400fbf9f 7195
2ad1815d
EB
7196 /* Rechain the next statements to the SWITCH_STMT. */
7197 last_tree = cs->switch_stmt;
400fbf9f 7198
8f17b5c5
MM
7199 /* Pop the stack. */
7200 switch_stack = switch_stack->next;
7201 splay_tree_delete (cs->cases);
7202 free (cs);
400fbf9f 7203}