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