]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c-typeck.c
Daily bump.
[thirdparty/gcc.git] / gcc / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
0953878d 2 Copyright (C) 1987, 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
7bd11157 3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
068e6bb3 4 Free Software Foundation, Inc.
400fbf9f 5
1322177d 6This file is part of GCC.
400fbf9f 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
400fbf9f 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
400fbf9f
JW
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
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"
400fbf9f 32#include "tree.h"
e57e265b 33#include "langhooks.h"
400fbf9f 34#include "c-tree.h"
29cc57cf 35#include "c-lang.h"
400fbf9f 36#include "flags.h"
e14417fa 37#include "output.h"
ab87f8c8 38#include "intl.h"
672a6f42 39#include "target.h"
325c3691 40#include "tree-iterator.h"
6662d794
MLI
41#include "bitmap.h"
42#include "gimple.h"
61d3ce20 43#include "c-family/c-objc.h"
325c3691 44
2ac2f164
JM
45/* Possible cases of implicit bad conversions. Used to select
46 diagnostic messages in convert_for_assignment. */
47enum impl_conv {
48 ic_argpass,
49 ic_assign,
50 ic_init,
51 ic_return
52};
53
0e3a99ae
AS
54/* Possibe cases of scalar_to_vector conversion. */
55enum stv_conv {
56 stv_error, /* Error occured. */
57 stv_nothing, /* Nothing happened. */
58 stv_firstarg, /* First argument must be expanded. */
59 stv_secondarg /* Second argument must be expanded. */
60};
61
bc4b653b
JM
62/* The level of nesting inside "__alignof__". */
63int in_alignof;
64
65/* The level of nesting inside "sizeof". */
66int in_sizeof;
67
68/* The level of nesting inside "typeof". */
69int in_typeof;
400fbf9f 70
b71c7f8a 71/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 72 message within this initializer. */
b71c7f8a 73static int missing_braces_mentioned;
103b7b17 74
bf730f15
RS
75static int require_constant_value;
76static int require_constant_elements;
77
58f9752a 78static bool null_pointer_constant_p (const_tree);
f55ade6e 79static tree qualify_type (tree, tree);
dc5027f4
JM
80static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
81 bool *);
744aa42f 82static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
83static int function_types_compatible_p (const_tree, const_tree, bool *,
84 bool *);
85static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 86static tree lookup_field (tree, tree);
bbbbb16a
ILT
87static int convert_arguments (tree, VEC(tree,gc) *, VEC(tree,gc) *, tree,
88 tree);
db3927fb 89static tree pointer_diff (location_t, tree, tree);
744aa42f
ILT
90static tree convert_for_assignment (location_t, tree, tree, tree,
91 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
92static tree valid_compound_expr_initializer (tree, tree);
93static void push_string (const char *);
94static void push_member_name (tree);
f55ade6e
AJ
95static int spelling_length (void);
96static char *print_spelling (char *);
683d6ff9 97static void warning_init (int, const char *);
c2255bc4 98static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
a1e3b3d9
LB
99static void output_init_element (tree, tree, bool, tree, tree, int, bool,
100 struct obstack *);
101static void output_pending_init_elements (int, struct obstack *);
102static int set_designator (int, struct obstack *);
103static void push_range_stack (tree, struct obstack *);
104static void add_pending_init (tree, tree, tree, bool, struct obstack *);
105static void set_nonincremental_init (struct obstack *);
106static void set_nonincremental_init_from_string (tree, struct obstack *);
107static tree find_init_member (tree, struct obstack *);
f37acdf9 108static void readonly_warning (tree, enum lvalue_use);
7bd11157 109static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 110static void record_maybe_used_decl (tree);
dc5027f4 111static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
112\f
113/* Return true if EXP is a null pointer constant, false otherwise. */
114
115static bool
58f9752a 116null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
117{
118 /* This should really operate on c_expr structures, but they aren't
119 yet available everywhere required. */
120 tree type = TREE_TYPE (expr);
121 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 122 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
123 && integer_zerop (expr)
124 && (INTEGRAL_TYPE_P (type)
125 || (TREE_CODE (type) == POINTER_TYPE
126 && VOID_TYPE_P (TREE_TYPE (type))
127 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
128}
928c19bb
JM
129
130/* EXPR may appear in an unevaluated part of an integer constant
131 expression, but not in an evaluated part. Wrap it in a
132 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
133 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
134
135static tree
136note_integer_operands (tree expr)
137{
138 tree ret;
139 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
140 {
141 ret = copy_node (expr);
142 TREE_OVERFLOW (ret) = 1;
143 }
144 else
145 {
146 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
147 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
148 }
149 return ret;
150}
151
4d84fe7c
JM
152/* Having checked whether EXPR may appear in an unevaluated part of an
153 integer constant expression and found that it may, remove any
154 C_MAYBE_CONST_EXPR noting this fact and return the resulting
155 expression. */
156
157static inline tree
158remove_c_maybe_const_expr (tree expr)
159{
160 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
161 return C_MAYBE_CONST_EXPR_EXPR (expr);
162 else
163 return expr;
164}
165
f13c9b2c
AP
166\f/* This is a cache to hold if two types are compatible or not. */
167
168struct tagged_tu_seen_cache {
169 const struct tagged_tu_seen_cache * next;
58f9752a
KG
170 const_tree t1;
171 const_tree t2;
f13c9b2c
AP
172 /* The return value of tagged_types_tu_compatible_p if we had seen
173 these two types already. */
174 int val;
175};
176
177static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
178static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
179
400fbf9f
JW
180/* Do `exp = require_complete_type (exp);' to make sure exp
181 does not have an incomplete type. (That includes void types.) */
182
183tree
2f6e4e97 184require_complete_type (tree value)
400fbf9f
JW
185{
186 tree type = TREE_TYPE (value);
187
c3d5c3fa 188 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
189 return error_mark_node;
190
400fbf9f 191 /* First, detect a valid value with a complete type. */
d0f062fb 192 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
193 return value;
194
7a228918 195 c_incomplete_type_error (value, type);
400fbf9f
JW
196 return error_mark_node;
197}
198
199/* Print an error message for invalid use of an incomplete type.
200 VALUE is the expression that was used (or 0 if that isn't known)
201 and TYPE is the type that was invalid. */
202
203void
ac7d7749 204c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 205{
5d5993dd 206 const char *type_code_string;
400fbf9f
JW
207
208 /* Avoid duplicate error message. */
209 if (TREE_CODE (type) == ERROR_MARK)
210 return;
211
212 if (value != 0 && (TREE_CODE (value) == VAR_DECL
213 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 214 error ("%qD has an incomplete type", value);
400fbf9f
JW
215 else
216 {
217 retry:
218 /* We must print an error message. Be clever about what it says. */
219
220 switch (TREE_CODE (type))
221 {
222 case RECORD_TYPE:
ab87f8c8 223 type_code_string = "struct";
400fbf9f
JW
224 break;
225
226 case UNION_TYPE:
ab87f8c8 227 type_code_string = "union";
400fbf9f
JW
228 break;
229
230 case ENUMERAL_TYPE:
ab87f8c8 231 type_code_string = "enum";
400fbf9f
JW
232 break;
233
234 case VOID_TYPE:
235 error ("invalid use of void expression");
236 return;
237
238 case ARRAY_TYPE:
239 if (TYPE_DOMAIN (type))
240 {
fba78abb
RH
241 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
242 {
243 error ("invalid use of flexible array member");
244 return;
245 }
400fbf9f
JW
246 type = TREE_TYPE (type);
247 goto retry;
248 }
249 error ("invalid use of array with unspecified bounds");
250 return;
251
252 default:
366de0ce 253 gcc_unreachable ();
400fbf9f
JW
254 }
255
256 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
257 error ("invalid use of undefined type %<%s %E%>",
258 type_code_string, TYPE_NAME (type));
400fbf9f
JW
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 261 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
262 }
263}
264
ab393bf1
NB
265/* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
267
268tree
2f6e4e97 269c_type_promotes_to (tree type)
ab393bf1
NB
270{
271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 return double_type_node;
273
274 if (c_promoting_integer_type_p (type))
275 {
276 /* Preserve unsignedness if not really getting any wider. */
8df83eae 277 if (TYPE_UNSIGNED (type)
c22cacf3
MS
278 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
279 return unsigned_type_node;
ab393bf1
NB
280 return integer_type_node;
281 }
282
283 return type;
284}
285
36c5e70a
BE
286/* Return true if between two named address spaces, whether there is a superset
287 named address space that encompasses both address spaces. If there is a
288 superset, return which address space is the superset. */
289
290static bool
291addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
292{
293 if (as1 == as2)
294 {
295 *common = as1;
296 return true;
297 }
298 else if (targetm.addr_space.subset_p (as1, as2))
299 {
300 *common = as2;
301 return true;
302 }
303 else if (targetm.addr_space.subset_p (as2, as1))
304 {
305 *common = as1;
306 return true;
307 }
308 else
309 return false;
310}
311
400fbf9f
JW
312/* Return a variant of TYPE which has all the type qualifiers of LIKE
313 as well as those of TYPE. */
314
315static tree
2f6e4e97 316qualify_type (tree type, tree like)
400fbf9f 317{
36c5e70a
BE
318 addr_space_t as_type = TYPE_ADDR_SPACE (type);
319 addr_space_t as_like = TYPE_ADDR_SPACE (like);
320 addr_space_t as_common;
321
322 /* If the two named address spaces are different, determine the common
323 superset address space. If there isn't one, raise an error. */
324 if (!addr_space_superset (as_type, as_like, &as_common))
325 {
326 as_common = as_type;
327 error ("%qT and %qT are in disjoint named address spaces",
328 type, like);
329 }
330
2f6e4e97 331 return c_build_qualified_type (type,
36c5e70a
BE
332 TYPE_QUALS_NO_ADDR_SPACE (type)
333 | TYPE_QUALS_NO_ADDR_SPACE (like)
334 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 335}
52ffd86e
MS
336
337/* Return true iff the given tree T is a variable length array. */
338
339bool
ac7d7749 340c_vla_type_p (const_tree t)
52ffd86e
MS
341{
342 if (TREE_CODE (t) == ARRAY_TYPE
343 && C_TYPE_VARIABLE_SIZE (t))
344 return true;
345 return false;
346}
400fbf9f 347\f
10bc1b1b 348/* Return the composite type of two compatible types.
5305f6d7 349
10bc1b1b
JM
350 We assume that comptypes has already been done and returned
351 nonzero; if that isn't so, this may crash. In particular, we
352 assume that qualifiers match. */
400fbf9f
JW
353
354tree
10bc1b1b 355composite_type (tree t1, tree t2)
400fbf9f 356{
b3694847
SS
357 enum tree_code code1;
358 enum tree_code code2;
4b027d16 359 tree attributes;
400fbf9f
JW
360
361 /* Save time if the two types are the same. */
362
363 if (t1 == t2) return t1;
364
365 /* If one type is nonsense, use the other. */
366 if (t1 == error_mark_node)
367 return t2;
368 if (t2 == error_mark_node)
369 return t1;
370
10bc1b1b
JM
371 code1 = TREE_CODE (t1);
372 code2 = TREE_CODE (t2);
373
d9525bec 374 /* Merge the attributes. */
5fd9b178 375 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 376
10bc1b1b
JM
377 /* If one is an enumerated type and the other is the compatible
378 integer type, the composite type might be either of the two
379 (DR#013 question 3). For consistency, use the enumerated type as
380 the composite type. */
400fbf9f 381
10bc1b1b
JM
382 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
383 return t1;
384 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
385 return t2;
75326e8c 386
366de0ce 387 gcc_assert (code1 == code2);
b6a10c9f 388
400fbf9f
JW
389 switch (code1)
390 {
400fbf9f 391 case POINTER_TYPE:
10bc1b1b 392 /* For two pointers, do this recursively on the target type. */
400fbf9f 393 {
3932261a
MM
394 tree pointed_to_1 = TREE_TYPE (t1);
395 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 396 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 397 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
398 t1 = build_type_attribute_variant (t1, attributes);
399 return qualify_type (t1, t2);
400fbf9f 400 }
400fbf9f
JW
401
402 case ARRAY_TYPE:
403 {
10bc1b1b 404 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
405 int quals;
406 tree unqual_elt;
ca8bdb78
JM
407 tree d1 = TYPE_DOMAIN (t1);
408 tree d2 = TYPE_DOMAIN (t2);
409 bool d1_variable, d2_variable;
410 bool d1_zero, d2_zero;
f6294de7 411 bool t1_complete, t2_complete;
46df2823 412
de46b2fe 413 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
414 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
415 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 416
f6294de7
JM
417 t1_complete = COMPLETE_TYPE_P (t1);
418 t2_complete = COMPLETE_TYPE_P (t2);
419
ca8bdb78
JM
420 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
421 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
422
423 d1_variable = (!d1_zero
424 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
425 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
426 d2_variable = (!d2_zero
427 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
428 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
429 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
430 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 431
400fbf9f 432 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
433 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
434 && (d2_variable || d2_zero || !d1_variable))
4b027d16 435 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
436 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
437 && (d1_variable || d1_zero || !d2_variable))
4b027d16 438 return build_type_attribute_variant (t2, attributes);
c22cacf3 439
de46b2fe
AP
440 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
441 return build_type_attribute_variant (t1, attributes);
442 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
443 return build_type_attribute_variant (t2, attributes);
c22cacf3 444
46df2823
JM
445 /* Merge the element types, and have a size if either arg has
446 one. We may have qualifiers on the element types. To set
447 up TYPE_MAIN_VARIANT correctly, we need to form the
448 composite of the unqualified types and add the qualifiers
449 back at the end. */
450 quals = TYPE_QUALS (strip_array_types (elt));
451 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
452 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
453 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
454 && (d2_variable
455 || d2_zero
456 || !d1_variable))
457 ? t1
458 : t2));
f6294de7
JM
459 /* Ensure a composite type involving a zero-length array type
460 is a zero-length type not an incomplete type. */
461 if (d1_zero && d2_zero
462 && (t1_complete || t2_complete)
463 && !COMPLETE_TYPE_P (t1))
464 {
465 TYPE_SIZE (t1) = bitsize_zero_node;
466 TYPE_SIZE_UNIT (t1) = size_zero_node;
467 }
46df2823 468 t1 = c_build_qualified_type (t1, quals);
de46b2fe 469 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
470 }
471
fcb99e7b
JJ
472 case ENUMERAL_TYPE:
473 case RECORD_TYPE:
474 case UNION_TYPE:
475 if (attributes != NULL)
476 {
477 /* Try harder not to create a new aggregate type. */
478 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
479 return t1;
480 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
481 return t2;
482 }
483 return build_type_attribute_variant (t1, attributes);
484
400fbf9f
JW
485 case FUNCTION_TYPE:
486 /* Function types: prefer the one that specified arg types.
487 If both do, merge the arg types. Also merge the return types. */
488 {
10bc1b1b 489 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
490 tree p1 = TYPE_ARG_TYPES (t1);
491 tree p2 = TYPE_ARG_TYPES (t2);
492 int len;
493 tree newargs, n;
494 int i;
495
496 /* Save space: see if the result is identical to one of the args. */
3f75a254 497 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 498 return build_type_attribute_variant (t1, attributes);
3f75a254 499 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 500 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
501
502 /* Simple way if one arg fails to specify argument types. */
503 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 504 {
fe7080d2
AP
505 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
506 t1 = build_type_attribute_variant (t1, attributes);
507 return qualify_type (t1, t2);
4b027d16 508 }
400fbf9f 509 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
510 {
511 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
512 t1 = build_type_attribute_variant (t1, attributes);
513 return qualify_type (t1, t2);
4b027d16 514 }
400fbf9f
JW
515
516 /* If both args specify argument types, we must merge the two
517 lists, argument by argument. */
2f4e8f2b 518
400fbf9f
JW
519 len = list_length (p1);
520 newargs = 0;
521
522 for (i = 0; i < len; i++)
8d9bfdc5 523 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
524
525 n = newargs;
526
527 for (; p1;
528 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
529 {
530 /* A null type means arg type is not specified.
531 Take whatever the other function type has. */
532 if (TREE_VALUE (p1) == 0)
533 {
534 TREE_VALUE (n) = TREE_VALUE (p2);
535 goto parm_done;
536 }
537 if (TREE_VALUE (p2) == 0)
538 {
539 TREE_VALUE (n) = TREE_VALUE (p1);
540 goto parm_done;
541 }
2f6e4e97 542
400fbf9f
JW
543 /* Given wait (union {union wait *u; int *i} *)
544 and wait (union wait *),
545 prefer union wait * as type of parm. */
546 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
547 && TREE_VALUE (p1) != TREE_VALUE (p2))
548 {
549 tree memb;
58cb41e6
JJ
550 tree mv2 = TREE_VALUE (p2);
551 if (mv2 && mv2 != error_mark_node
552 && TREE_CODE (mv2) != ARRAY_TYPE)
553 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 554 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 555 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
556 {
557 tree mv3 = TREE_TYPE (memb);
558 if (mv3 && mv3 != error_mark_node
559 && TREE_CODE (mv3) != ARRAY_TYPE)
560 mv3 = TYPE_MAIN_VARIANT (mv3);
561 if (comptypes (mv3, mv2))
562 {
563 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
564 TREE_VALUE (p2));
c1771a20 565 pedwarn (input_location, OPT_Wpedantic,
fcf73884 566 "function types not truly compatible in ISO C");
58cb41e6
JJ
567 goto parm_done;
568 }
569 }
400fbf9f
JW
570 }
571 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
572 && TREE_VALUE (p2) != TREE_VALUE (p1))
573 {
574 tree memb;
58cb41e6
JJ
575 tree mv1 = TREE_VALUE (p1);
576 if (mv1 && mv1 != error_mark_node
577 && TREE_CODE (mv1) != ARRAY_TYPE)
578 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 579 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 580 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
581 {
582 tree mv3 = TREE_TYPE (memb);
583 if (mv3 && mv3 != error_mark_node
584 && TREE_CODE (mv3) != ARRAY_TYPE)
585 mv3 = TYPE_MAIN_VARIANT (mv3);
586 if (comptypes (mv3, mv1))
587 {
588 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
589 TREE_VALUE (p1));
c1771a20 590 pedwarn (input_location, OPT_Wpedantic,
fcf73884 591 "function types not truly compatible in ISO C");
58cb41e6
JJ
592 goto parm_done;
593 }
594 }
400fbf9f 595 }
10bc1b1b 596 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
597 parm_done: ;
598 }
599
4b027d16 600 t1 = build_function_type (valtype, newargs);
fe7080d2 601 t1 = qualify_type (t1, t2);
0f41302f 602 /* ... falls through ... */
400fbf9f
JW
603 }
604
605 default:
4b027d16 606 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
607 }
608
609}
10bc1b1b
JM
610
611/* Return the type of a conditional expression between pointers to
612 possibly differently qualified versions of compatible types.
613
614 We assume that comp_target_types has already been done and returned
615 nonzero; if that isn't so, this may crash. */
616
617static tree
618common_pointer_type (tree t1, tree t2)
619{
620 tree attributes;
46df2823
JM
621 tree pointed_to_1, mv1;
622 tree pointed_to_2, mv2;
10bc1b1b 623 tree target;
eb1387a0 624 unsigned target_quals;
36c5e70a
BE
625 addr_space_t as1, as2, as_common;
626 int quals1, quals2;
10bc1b1b
JM
627
628 /* Save time if the two types are the same. */
629
630 if (t1 == t2) return t1;
631
632 /* If one type is nonsense, use the other. */
633 if (t1 == error_mark_node)
634 return t2;
635 if (t2 == error_mark_node)
636 return t1;
637
366de0ce 638 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 639 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
640
641 /* Merge the attributes. */
642 attributes = targetm.merge_type_attributes (t1, t2);
643
644 /* Find the composite type of the target types, and combine the
46df2823
JM
645 qualifiers of the two types' targets. Do not lose qualifiers on
646 array element types by taking the TYPE_MAIN_VARIANT. */
647 mv1 = pointed_to_1 = TREE_TYPE (t1);
648 mv2 = pointed_to_2 = TREE_TYPE (t2);
649 if (TREE_CODE (mv1) != ARRAY_TYPE)
650 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
651 if (TREE_CODE (mv2) != ARRAY_TYPE)
652 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
653 target = composite_type (mv1, mv2);
eb1387a0
RG
654
655 /* For function types do not merge const qualifiers, but drop them
656 if used inconsistently. The middle-end uses these to mark const
657 and noreturn functions. */
36c5e70a
BE
658 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
659 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
660
eb1387a0 661 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 662 target_quals = (quals1 & quals2);
eb1387a0 663 else
36c5e70a
BE
664 target_quals = (quals1 | quals2);
665
666 /* If the two named address spaces are different, determine the common
667 superset address space. This is guaranteed to exist due to the
668 assumption that comp_target_type returned non-zero. */
669 as1 = TYPE_ADDR_SPACE (pointed_to_1);
670 as2 = TYPE_ADDR_SPACE (pointed_to_2);
671 if (!addr_space_superset (as1, as2, &as_common))
672 gcc_unreachable ();
673
674 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
675
eb1387a0 676 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
677 return build_type_attribute_variant (t1, attributes);
678}
679
680/* Return the common type for two arithmetic types under the usual
681 arithmetic conversions. The default conversions have already been
682 applied, and enumerated types converted to their compatible integer
683 types. The resulting type is unqualified and has no attributes.
684
685 This is the type for the result of most arithmetic operations
686 if the operands have the given two types. */
687
ccf7f880
JJ
688static tree
689c_common_type (tree t1, tree t2)
10bc1b1b
JM
690{
691 enum tree_code code1;
692 enum tree_code code2;
693
694 /* If one type is nonsense, use the other. */
695 if (t1 == error_mark_node)
696 return t2;
697 if (t2 == error_mark_node)
698 return t1;
699
700 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
701 t1 = TYPE_MAIN_VARIANT (t1);
702
703 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
704 t2 = TYPE_MAIN_VARIANT (t2);
705
706 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
707 t1 = build_type_attribute_variant (t1, NULL_TREE);
708
709 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
710 t2 = build_type_attribute_variant (t2, NULL_TREE);
711
712 /* Save time if the two types are the same. */
713
714 if (t1 == t2) return t1;
715
716 code1 = TREE_CODE (t1);
717 code2 = TREE_CODE (t2);
718
366de0ce 719 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
720 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
721 || code1 == INTEGER_TYPE);
366de0ce 722 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
723 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
724 || code2 == INTEGER_TYPE);
10bc1b1b 725
5fc89bfd
JJ
726 /* When one operand is a decimal float type, the other operand cannot be
727 a generic float type or a complex type. We also disallow vector types
728 here. */
729 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
730 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
731 {
732 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
733 {
734 error ("can%'t mix operands of decimal float and vector types");
735 return error_mark_node;
736 }
737 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
738 {
739 error ("can%'t mix operands of decimal float and complex types");
740 return error_mark_node;
741 }
742 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
743 {
744 error ("can%'t mix operands of decimal float and other float types");
745 return error_mark_node;
746 }
747 }
748
10bc1b1b
JM
749 /* If one type is a vector type, return that type. (How the usual
750 arithmetic conversions apply to the vector types extension is not
751 precisely specified.) */
752 if (code1 == VECTOR_TYPE)
753 return t1;
754
755 if (code2 == VECTOR_TYPE)
756 return t2;
757
758 /* If one type is complex, form the common type of the non-complex
759 components, then make that complex. Use T1 or T2 if it is the
760 required type. */
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
762 {
763 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
764 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 765 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
766
767 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
768 return t1;
769 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
770 return t2;
771 else
772 return build_complex_type (subtype);
773 }
774
775 /* If only one is real, use it as the result. */
776
777 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
778 return t1;
779
780 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
781 return t2;
782
9a8ce21f
JG
783 /* If both are real and either are decimal floating point types, use
784 the decimal floating point type with the greater precision. */
785
786 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
787 {
788 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
789 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
790 return dfloat128_type_node;
791 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
792 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
793 return dfloat64_type_node;
794 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
795 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
796 return dfloat32_type_node;
797 }
798
ab22c1fa
CF
799 /* Deal with fixed-point types. */
800 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
801 {
802 unsigned int unsignedp = 0, satp = 0;
803 enum machine_mode m1, m2;
804 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
805
806 m1 = TYPE_MODE (t1);
807 m2 = TYPE_MODE (t2);
808
809 /* If one input type is saturating, the result type is saturating. */
810 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
811 satp = 1;
812
813 /* If both fixed-point types are unsigned, the result type is unsigned.
814 When mixing fixed-point and integer types, follow the sign of the
815 fixed-point type.
816 Otherwise, the result type is signed. */
817 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
818 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
819 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
820 && TYPE_UNSIGNED (t1))
821 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t2)))
823 unsignedp = 1;
824
825 /* The result type is signed. */
826 if (unsignedp == 0)
827 {
828 /* If the input type is unsigned, we need to convert to the
829 signed type. */
830 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
831 {
d75d71e0 832 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
833 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
834 mclass = MODE_FRACT;
835 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
836 mclass = MODE_ACCUM;
837 else
838 gcc_unreachable ();
839 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
840 }
841 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
842 {
d75d71e0 843 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
844 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
850 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
851 }
852 }
853
854 if (code1 == FIXED_POINT_TYPE)
855 {
856 fbit1 = GET_MODE_FBIT (m1);
857 ibit1 = GET_MODE_IBIT (m1);
858 }
859 else
860 {
861 fbit1 = 0;
862 /* Signed integers need to subtract one sign bit. */
863 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
864 }
865
866 if (code2 == FIXED_POINT_TYPE)
867 {
868 fbit2 = GET_MODE_FBIT (m2);
869 ibit2 = GET_MODE_IBIT (m2);
870 }
871 else
872 {
873 fbit2 = 0;
874 /* Signed integers need to subtract one sign bit. */
875 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
876 }
877
878 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
879 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
880 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
881 satp);
882 }
883
10bc1b1b
JM
884 /* Both real or both integers; use the one with greater precision. */
885
886 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
887 return t1;
888 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
889 return t2;
890
891 /* Same precision. Prefer long longs to longs to ints when the
892 same precision, following the C99 rules on integer type rank
893 (which are equivalent to the C90 rules for C90 types). */
894
895 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
896 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
897 return long_long_unsigned_type_node;
898
899 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
900 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
901 {
902 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
903 return long_long_unsigned_type_node;
904 else
c22cacf3 905 return long_long_integer_type_node;
10bc1b1b
JM
906 }
907
908 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
910 return long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
914 {
915 /* But preserve unsignedness from the other type,
916 since long cannot hold all the values of an unsigned int. */
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_unsigned_type_node;
919 else
920 return long_integer_type_node;
921 }
922
923 /* Likewise, prefer long double to double even if same size. */
924 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
926 return long_double_type_node;
927
928 /* Otherwise prefer the unsigned one. */
929
930 if (TYPE_UNSIGNED (t1))
931 return t1;
932 else
933 return t2;
934}
400fbf9f 935\f
5922c215
JM
936/* Wrapper around c_common_type that is used by c-common.c and other
937 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
938 are allowed here and are converted to their compatible integer types.
939 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
940 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
941tree
942common_type (tree t1, tree t2)
943{
944 if (TREE_CODE (t1) == ENUMERAL_TYPE)
945 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
946 if (TREE_CODE (t2) == ENUMERAL_TYPE)
947 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
948
949 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
950 if (TREE_CODE (t1) == BOOLEAN_TYPE
951 && TREE_CODE (t2) == BOOLEAN_TYPE)
952 return boolean_type_node;
953
954 /* If either type is BOOLEAN_TYPE, then return the other. */
955 if (TREE_CODE (t1) == BOOLEAN_TYPE)
956 return t2;
957 if (TREE_CODE (t2) == BOOLEAN_TYPE)
958 return t1;
959
ccf7f880
JJ
960 return c_common_type (t1, t2);
961}
f13c9b2c 962
400fbf9f
JW
963/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
964 or various other operations. Return 2 if they are compatible
965 but a warning may be needed if you use them together. */
966
967int
132da1a5 968comptypes (tree type1, tree type2)
f13c9b2c
AP
969{
970 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
971 int val;
972
dc5027f4 973 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
974 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
975
976 return val;
977}
978
979/* Like comptypes, but if it returns non-zero because enum and int are
980 compatible, it sets *ENUM_AND_INT_P to true. */
981
982static int
983comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
984{
985 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
986 int val;
987
dc5027f4
JM
988 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
989 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
990
991 return val;
992}
993
994/* Like comptypes, but if it returns nonzero for different types, it
995 sets *DIFFERENT_TYPES_P to true. */
996
997int
998comptypes_check_different_types (tree type1, tree type2,
999 bool *different_types_p)
1000{
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002 int val;
1003
1004 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1006
f13c9b2c 1007 return val;
c22cacf3
MS
1008}
1009\f
f13c9b2c
AP
1010/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1011 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1012 but a warning may be needed if you use them together. If
1013 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1014 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1015 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1016 NULL, and the types are compatible but different enough not to be
48b0b196 1017 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1018 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1019 false, but may or may not be set if the types are incompatible.
1020 This differs from comptypes, in that we don't free the seen
1021 types. */
f13c9b2c
AP
1022
1023static int
dc5027f4
JM
1024comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1025 bool *different_types_p)
400fbf9f 1026{
58f9752a
KG
1027 const_tree t1 = type1;
1028 const_tree t2 = type2;
4b027d16 1029 int attrval, val;
400fbf9f
JW
1030
1031 /* Suppress errors caused by previously reported errors. */
1032
8d47dfc5
RH
1033 if (t1 == t2 || !t1 || !t2
1034 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1035 return 1;
1036
bca63328
JM
1037 /* Enumerated types are compatible with integer types, but this is
1038 not transitive: two enumerated types in the same translation unit
1039 are compatible with each other only if they are the same type. */
400fbf9f 1040
bca63328 1041 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1042 {
1043 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1044 if (TREE_CODE (t2) != VOID_TYPE)
1045 {
1046 if (enum_and_int_p != NULL)
1047 *enum_and_int_p = true;
1048 if (different_types_p != NULL)
1049 *different_types_p = true;
1050 }
744aa42f 1051 }
bca63328 1052 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1053 {
1054 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1055 if (TREE_CODE (t1) != VOID_TYPE)
1056 {
1057 if (enum_and_int_p != NULL)
1058 *enum_and_int_p = true;
1059 if (different_types_p != NULL)
1060 *different_types_p = true;
1061 }
744aa42f 1062 }
400fbf9f
JW
1063
1064 if (t1 == t2)
1065 return 1;
1066
1067 /* Different classes of types can't be compatible. */
1068
3aeb3655
EC
1069 if (TREE_CODE (t1) != TREE_CODE (t2))
1070 return 0;
400fbf9f 1071
118a3a8b 1072 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1073
3932261a 1074 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1075 return 0;
1076
08632da2
RS
1077 /* Allow for two different type nodes which have essentially the same
1078 definition. Note that we already checked for equality of the type
38e01259 1079 qualifiers (just above). */
400fbf9f 1080
46df2823
JM
1081 if (TREE_CODE (t1) != ARRAY_TYPE
1082 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1083 return 1;
1084
4b027d16 1085 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1086 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1087 return 0;
1088
1089 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1090 val = 0;
1091
400fbf9f
JW
1092 switch (TREE_CODE (t1))
1093 {
1094 case POINTER_TYPE:
106f5de5
UW
1095 /* Do not remove mode or aliasing information. */
1096 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1097 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1098 break;
4b027d16 1099 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1100 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1101 enum_and_int_p, different_types_p));
4b027d16 1102 break;
400fbf9f
JW
1103
1104 case FUNCTION_TYPE:
dc5027f4
JM
1105 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1106 different_types_p);
4b027d16 1107 break;
400fbf9f
JW
1108
1109 case ARRAY_TYPE:
1110 {
400fbf9f
JW
1111 tree d1 = TYPE_DOMAIN (t1);
1112 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1113 bool d1_variable, d2_variable;
1114 bool d1_zero, d2_zero;
4b027d16 1115 val = 1;
400fbf9f
JW
1116
1117 /* Target types must match incl. qualifiers. */
1118 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1119 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1120 enum_and_int_p,
1121 different_types_p)))
400fbf9f
JW
1122 return 0;
1123
dc5027f4
JM
1124 if (different_types_p != NULL
1125 && (d1 == 0) != (d2 == 0))
1126 *different_types_p = true;
400fbf9f 1127 /* Sizes must match unless one is missing or variable. */
3f85558f 1128 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1129 break;
400fbf9f 1130
3f75a254
JM
1131 d1_zero = !TYPE_MAX_VALUE (d1);
1132 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1133
3f75a254 1134 d1_variable = (!d1_zero
3f85558f
RH
1135 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1136 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1137 d2_variable = (!d2_zero
3f85558f
RH
1138 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1139 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1140 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1141 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1142
dc5027f4
JM
1143 if (different_types_p != NULL
1144 && d1_variable != d2_variable)
1145 *different_types_p = true;
3f85558f
RH
1146 if (d1_variable || d2_variable)
1147 break;
1148 if (d1_zero && d2_zero)
1149 break;
1150 if (d1_zero || d2_zero
3f75a254
JM
1151 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1152 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1153 val = 0;
1154
c22cacf3 1155 break;
400fbf9f
JW
1156 }
1157
d1bd0ded 1158 case ENUMERAL_TYPE:
58393038 1159 case RECORD_TYPE:
d1bd0ded 1160 case UNION_TYPE:
766beae1 1161 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1162 {
fcb99e7b
JJ
1163 tree a1 = TYPE_ATTRIBUTES (t1);
1164 tree a2 = TYPE_ATTRIBUTES (t2);
1165
1166 if (! attribute_list_contained (a1, a2)
1167 && ! attribute_list_contained (a2, a1))
1168 break;
1169
f13c9b2c 1170 if (attrval != 2)
dc5027f4
JM
1171 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1172 different_types_p);
1173 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1174 different_types_p);
f13c9b2c 1175 }
4b027d16 1176 break;
e9a25f70 1177
62e1dfcf 1178 case VECTOR_TYPE:
744aa42f
ILT
1179 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1180 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1181 enum_and_int_p, different_types_p));
62e1dfcf
NC
1182 break;
1183
e9a25f70
JL
1184 default:
1185 break;
400fbf9f 1186 }
4b027d16 1187 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1188}
1189
36c5e70a
BE
1190/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1191 their qualifiers, except for named address spaces. If the pointers point to
1192 different named addresses, then we must determine if one address space is a
1193 subset of the other. */
400fbf9f
JW
1194
1195static int
744aa42f 1196comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1197{
392202b0 1198 int val;
36c5e70a
BE
1199 tree mvl = TREE_TYPE (ttl);
1200 tree mvr = TREE_TYPE (ttr);
1201 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1202 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1203 addr_space_t as_common;
744aa42f 1204 bool enum_and_int_p;
8b40563c 1205
36c5e70a
BE
1206 /* Fail if pointers point to incompatible address spaces. */
1207 if (!addr_space_superset (asl, asr, &as_common))
1208 return 0;
1209
46df2823
JM
1210 /* Do not lose qualifiers on element types of array types that are
1211 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823
JM
1212 if (TREE_CODE (mvl) != ARRAY_TYPE)
1213 mvl = TYPE_MAIN_VARIANT (mvl);
1214 if (TREE_CODE (mvr) != ARRAY_TYPE)
1215 mvr = TYPE_MAIN_VARIANT (mvr);
744aa42f
ILT
1216 enum_and_int_p = false;
1217 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1218
fcf73884 1219 if (val == 2)
c1771a20 1220 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1221
1222 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1223 warning_at (location, OPT_Wc___compat,
1224 "pointer target types incompatible in C++");
1225
400fbf9f
JW
1226 return val;
1227}
1228\f
1229/* Subroutines of `comptypes'. */
1230
f75fbaf7
ZW
1231/* Determine whether two trees derive from the same translation unit.
1232 If the CONTEXT chain ends in a null, that tree's context is still
1233 being parsed, so if two trees have context chains ending in null,
766beae1 1234 they're in the same translation unit. */
f75fbaf7 1235int
58f9752a 1236same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1237{
1238 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1239 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1240 {
6615c446
JO
1241 case tcc_declaration:
1242 t1 = DECL_CONTEXT (t1); break;
1243 case tcc_type:
1244 t1 = TYPE_CONTEXT (t1); break;
1245 case tcc_exceptional:
1246 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1247 default: gcc_unreachable ();
766beae1
ZW
1248 }
1249
1250 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1251 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1252 {
6615c446
JO
1253 case tcc_declaration:
1254 t2 = DECL_CONTEXT (t2); break;
1255 case tcc_type:
1256 t2 = TYPE_CONTEXT (t2); break;
1257 case tcc_exceptional:
1258 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1259 default: gcc_unreachable ();
766beae1
ZW
1260 }
1261
1262 return t1 == t2;
1263}
1264
f13c9b2c 1265/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1266
f13c9b2c 1267static struct tagged_tu_seen_cache *
58f9752a 1268alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1269{
cceb1885 1270 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1271 tu->next = tagged_tu_seen_base;
1272 tu->t1 = t1;
1273 tu->t2 = t2;
c22cacf3 1274
f13c9b2c 1275 tagged_tu_seen_base = tu;
c22cacf3 1276
f13c9b2c
AP
1277 /* The C standard says that two structures in different translation
1278 units are compatible with each other only if the types of their
1279 fields are compatible (among other things). We assume that they
1280 are compatible until proven otherwise when building the cache.
1281 An example where this can occur is:
1282 struct a
1283 {
1284 struct a *next;
1285 };
1286 If we are comparing this against a similar struct in another TU,
c83eecad 1287 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1288 loop. */
1289 tu->val = 1;
1290 return tu;
1291}
d1bd0ded 1292
f13c9b2c 1293/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1294
f13c9b2c
AP
1295static void
1296free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1297{
1298 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1299 while (tu != tu_til)
1300 {
741ac903
KG
1301 const struct tagged_tu_seen_cache *const tu1
1302 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1303 tu = tu1->next;
b1d5455a 1304 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1305 }
1306 tagged_tu_seen_base = tu_til;
1307}
d1bd0ded
GK
1308
1309/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1310 compatible. If the two types are not the same (which has been
1311 checked earlier), this can only happen when multiple translation
1312 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1313 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1314 comptypes_internal. */
d1bd0ded
GK
1315
1316static int
744aa42f 1317tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1318 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1319{
1320 tree s1, s2;
1321 bool needs_warning = false;
3aeb3655 1322
d1bd0ded
GK
1323 /* We have to verify that the tags of the types are the same. This
1324 is harder than it looks because this may be a typedef, so we have
1325 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1326 typedef...
1327 In the case of compiler-created builtin structs the TYPE_DECL
1328 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1329 while (TYPE_NAME (t1)
1330 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1331 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1332 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1333
dea984dc
ILT
1334 while (TYPE_NAME (t2)
1335 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1336 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1337 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1338
1339 /* C90 didn't have the requirement that the two tags be the same. */
1340 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1341 return 0;
3aeb3655 1342
d1bd0ded
GK
1343 /* C90 didn't say what happened if one or both of the types were
1344 incomplete; we choose to follow C99 rules here, which is that they
1345 are compatible. */
1346 if (TYPE_SIZE (t1) == NULL
1347 || TYPE_SIZE (t2) == NULL)
1348 return 1;
3aeb3655 1349
d1bd0ded 1350 {
f13c9b2c 1351 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1352 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1353 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1354 return tts_i->val;
d1bd0ded 1355 }
3aeb3655 1356
d1bd0ded
GK
1357 switch (TREE_CODE (t1))
1358 {
1359 case ENUMERAL_TYPE:
1360 {
f13c9b2c 1361 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1362 /* Speed up the case where the type values are in the same order. */
1363 tree tv1 = TYPE_VALUES (t1);
1364 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1365
c22cacf3 1366 if (tv1 == tv2)
f13c9b2c
AP
1367 {
1368 return 1;
1369 }
3aeb3655 1370
c22cacf3
MS
1371 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1372 {
1373 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1374 break;
1375 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1376 {
c22cacf3 1377 tu->val = 0;
f13c9b2c
AP
1378 return 0;
1379 }
c22cacf3 1380 }
3aeb3655 1381
c22cacf3 1382 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1383 {
1384 return 1;
1385 }
c22cacf3 1386 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1387 {
1388 tu->val = 0;
1389 return 0;
1390 }
3aeb3655 1391
d1bd0ded 1392 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1393 {
1394 tu->val = 0;
1395 return 0;
1396 }
3aeb3655 1397
d1bd0ded
GK
1398 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1399 {
1400 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1401 if (s2 == NULL
1402 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1403 {
1404 tu->val = 0;
1405 return 0;
1406 }
d1bd0ded
GK
1407 }
1408 return 1;
1409 }
1410
1411 case UNION_TYPE:
1412 {
f13c9b2c 1413 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1414 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1415 {
1416 tu->val = 0;
1417 return 0;
1418 }
c22cacf3 1419
f13c9b2c
AP
1420 /* Speed up the common case where the fields are in the same order. */
1421 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1422 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1423 {
1424 int result;
c22cacf3 1425
3ae4d3cc 1426 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1427 break;
744aa42f 1428 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1429 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1430
1431 if (result != 1 && !DECL_NAME (s1))
1432 break;
f13c9b2c
AP
1433 if (result == 0)
1434 {
1435 tu->val = 0;
1436 return 0;
1437 }
1438 if (result == 2)
1439 needs_warning = true;
1440
1441 if (TREE_CODE (s1) == FIELD_DECL
1442 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1443 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1444 {
1445 tu->val = 0;
1446 return 0;
1447 }
1448 }
1449 if (!s1 && !s2)
1450 {
1451 tu->val = needs_warning ? 2 : 1;
1452 return tu->val;
1453 }
d1bd0ded 1454
910ad8de 1455 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1456 {
1457 bool ok = false;
3aeb3655 1458
910ad8de 1459 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1460 if (DECL_NAME (s1) == DECL_NAME (s2))
1461 {
1462 int result;
1463
744aa42f 1464 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1465 enum_and_int_p,
1466 different_types_p);
3ae4d3cc
AO
1467
1468 if (result != 1 && !DECL_NAME (s1))
1469 continue;
1470 if (result == 0)
1471 {
1472 tu->val = 0;
1473 return 0;
1474 }
1475 if (result == 2)
1476 needs_warning = true;
1477
1478 if (TREE_CODE (s1) == FIELD_DECL
1479 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1480 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1481 break;
3ae4d3cc
AO
1482
1483 ok = true;
1484 break;
1485 }
3f75a254 1486 if (!ok)
f13c9b2c
AP
1487 {
1488 tu->val = 0;
1489 return 0;
1490 }
d1bd0ded 1491 }
f13c9b2c
AP
1492 tu->val = needs_warning ? 2 : 10;
1493 return tu->val;
d1bd0ded
GK
1494 }
1495
1496 case RECORD_TYPE:
1497 {
c22cacf3 1498 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1499
1500 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1501 s1 && s2;
910ad8de 1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1503 {
1504 int result;
1505 if (TREE_CODE (s1) != TREE_CODE (s2)
1506 || DECL_NAME (s1) != DECL_NAME (s2))
1507 break;
744aa42f 1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1509 enum_and_int_p, different_types_p);
d1bd0ded
GK
1510 if (result == 0)
1511 break;
1512 if (result == 2)
1513 needs_warning = true;
3aeb3655 1514
d1bd0ded
GK
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518 break;
1519 }
d1bd0ded 1520 if (s1 && s2)
f13c9b2c
AP
1521 tu->val = 0;
1522 else
1523 tu->val = needs_warning ? 2 : 1;
1524 return tu->val;
d1bd0ded
GK
1525 }
1526
1527 default:
366de0ce 1528 gcc_unreachable ();
d1bd0ded
GK
1529 }
1530}
1531
400fbf9f
JW
1532/* Return 1 if two function types F1 and F2 are compatible.
1533 If either type specifies no argument types,
1534 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1535 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1536 the other must specify that number of self-promoting arg types.
744aa42f 1537 Otherwise, the argument types must match.
dc5027f4 1538 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1539
1540static int
744aa42f 1541function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1542 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1543{
1544 tree args1, args2;
1545 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1546 int val = 1;
1547 int val1;
a6fdc086
GK
1548 tree ret1, ret2;
1549
1550 ret1 = TREE_TYPE (f1);
1551 ret2 = TREE_TYPE (f2);
1552
e508a019
JM
1553 /* 'volatile' qualifiers on a function's return type used to mean
1554 the function is noreturn. */
1555 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1556 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1557 if (TYPE_VOLATILE (ret1))
1558 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1559 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1560 if (TYPE_VOLATILE (ret2))
1561 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1562 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1563 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1564 if (val == 0)
400fbf9f
JW
1565 return 0;
1566
1567 args1 = TYPE_ARG_TYPES (f1);
1568 args2 = TYPE_ARG_TYPES (f2);
1569
dc5027f4
JM
1570 if (different_types_p != NULL
1571 && (args1 == 0) != (args2 == 0))
1572 *different_types_p = true;
1573
400fbf9f
JW
1574 /* An unspecified parmlist matches any specified parmlist
1575 whose argument types don't need default promotions. */
1576
1577 if (args1 == 0)
1578 {
1579 if (!self_promoting_args_p (args2))
1580 return 0;
1581 /* If one of these types comes from a non-prototype fn definition,
1582 compare that with the other type's arglist.
3176a0c2 1583 If they don't match, ask for a warning (but no error). */
400fbf9f 1584 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1585 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1586 enum_and_int_p, different_types_p))
400fbf9f
JW
1587 val = 2;
1588 return val;
1589 }
1590 if (args2 == 0)
1591 {
1592 if (!self_promoting_args_p (args1))
1593 return 0;
1594 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1595 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1596 enum_and_int_p, different_types_p))
400fbf9f
JW
1597 val = 2;
1598 return val;
1599 }
1600
1601 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1602 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1603 different_types_p);
400fbf9f
JW
1604 return val1 != 1 ? val1 : val;
1605}
1606
744aa42f
ILT
1607/* Check two lists of types for compatibility, returning 0 for
1608 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1609 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1610 comptypes_internal. */
400fbf9f
JW
1611
1612static int
744aa42f 1613type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1614 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1615{
1616 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1617 int val = 1;
9d5f3e49 1618 int newval = 0;
400fbf9f
JW
1619
1620 while (1)
1621 {
46df2823 1622 tree a1, mv1, a2, mv2;
400fbf9f
JW
1623 if (args1 == 0 && args2 == 0)
1624 return val;
1625 /* If one list is shorter than the other,
1626 they fail to match. */
1627 if (args1 == 0 || args2 == 0)
1628 return 0;
46df2823
JM
1629 mv1 = a1 = TREE_VALUE (args1);
1630 mv2 = a2 = TREE_VALUE (args2);
1631 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
1632 mv1 = TYPE_MAIN_VARIANT (mv1);
1633 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
1634 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f
JW
1635 /* A null pointer instead of a type
1636 means there is supposed to be an argument
1637 but nothing is specified about what type it has.
1638 So match anything that self-promotes. */
dc5027f4
JM
1639 if (different_types_p != NULL
1640 && (a1 == 0) != (a2 == 0))
1641 *different_types_p = true;
46df2823 1642 if (a1 == 0)
400fbf9f 1643 {
46df2823 1644 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1645 return 0;
1646 }
46df2823 1647 else if (a2 == 0)
400fbf9f 1648 {
46df2823 1649 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1650 return 0;
1651 }
8f5b6d29 1652 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1653 else if (TREE_CODE (a1) == ERROR_MARK
1654 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1655 ;
dc5027f4
JM
1656 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1657 different_types_p)))
400fbf9f 1658 {
dc5027f4
JM
1659 if (different_types_p != NULL)
1660 *different_types_p = true;
400fbf9f
JW
1661 /* Allow wait (union {union wait *u; int *i} *)
1662 and wait (union wait *) to be compatible. */
46df2823
JM
1663 if (TREE_CODE (a1) == UNION_TYPE
1664 && (TYPE_NAME (a1) == 0
ebf0bf7f 1665 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1666 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1667 && tree_int_cst_equal (TYPE_SIZE (a1),
1668 TYPE_SIZE (a2)))
400fbf9f
JW
1669 {
1670 tree memb;
46df2823 1671 for (memb = TYPE_FIELDS (a1);
910ad8de 1672 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1673 {
1674 tree mv3 = TREE_TYPE (memb);
1675 if (mv3 && mv3 != error_mark_node
1676 && TREE_CODE (mv3) != ARRAY_TYPE)
1677 mv3 = TYPE_MAIN_VARIANT (mv3);
dc5027f4
JM
1678 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1679 different_types_p))
58cb41e6
JJ
1680 break;
1681 }
400fbf9f
JW
1682 if (memb == 0)
1683 return 0;
1684 }
46df2823
JM
1685 else if (TREE_CODE (a2) == UNION_TYPE
1686 && (TYPE_NAME (a2) == 0
ebf0bf7f 1687 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1688 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1689 && tree_int_cst_equal (TYPE_SIZE (a2),
1690 TYPE_SIZE (a1)))
400fbf9f
JW
1691 {
1692 tree memb;
46df2823 1693 for (memb = TYPE_FIELDS (a2);
910ad8de 1694 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1695 {
1696 tree mv3 = TREE_TYPE (memb);
1697 if (mv3 && mv3 != error_mark_node
1698 && TREE_CODE (mv3) != ARRAY_TYPE)
1699 mv3 = TYPE_MAIN_VARIANT (mv3);
dc5027f4
JM
1700 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1701 different_types_p))
58cb41e6
JJ
1702 break;
1703 }
400fbf9f
JW
1704 if (memb == 0)
1705 return 0;
1706 }
1707 else
1708 return 0;
1709 }
1710
1711 /* comptypes said ok, but record if it said to warn. */
1712 if (newval > val)
1713 val = newval;
1714
1715 args1 = TREE_CHAIN (args1);
1716 args2 = TREE_CHAIN (args2);
1717 }
1718}
400fbf9f 1719\f
400fbf9f
JW
1720/* Compute the size to increment a pointer by. */
1721
4e2fb7de 1722static tree
58f9752a 1723c_size_in_bytes (const_tree type)
400fbf9f
JW
1724{
1725 enum tree_code code = TREE_CODE (type);
1726
fed3cef0
RK
1727 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1728 return size_one_node;
1729
d0f062fb 1730 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1731 {
1732 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1733 return size_one_node;
400fbf9f
JW
1734 }
1735
1736 /* Convert in case a char is more than one unit. */
db3927fb
AH
1737 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1738 size_int (TYPE_PRECISION (char_type_node)
1739 / BITS_PER_UNIT));
400fbf9f 1740}
400fbf9f 1741\f
400fbf9f
JW
1742/* Return either DECL or its known constant value (if it has one). */
1743
56cb9733 1744tree
2f6e4e97 1745decl_constant_value (tree decl)
400fbf9f 1746{
a7c1916a 1747 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1748 in a place where a variable is invalid. Note that DECL_INITIAL
1749 isn't valid for a PARM_DECL. */
a7c1916a 1750 current_function_decl != 0
4f976745 1751 && TREE_CODE (decl) != PARM_DECL
3f75a254 1752 && !TREE_THIS_VOLATILE (decl)
83bab8db 1753 && TREE_READONLY (decl)
400fbf9f
JW
1754 && DECL_INITIAL (decl) != 0
1755 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1756 /* This is invalid if initial value is not constant.
1757 If it has either a function call, a memory reference,
1758 or a variable, then re-evaluating it could give different results. */
1759 && TREE_CONSTANT (DECL_INITIAL (decl))
1760 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1761 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1762 return DECL_INITIAL (decl);
1763 return decl;
1764}
1765
f2a71bbc
JM
1766/* Convert the array expression EXP to a pointer. */
1767static tree
c2255bc4 1768array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1769{
f2a71bbc 1770 tree orig_exp = exp;
207bf485 1771 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1772 tree adr;
1773 tree restype = TREE_TYPE (type);
1774 tree ptrtype;
207bf485 1775
f2a71bbc 1776 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1777
f2a71bbc 1778 STRIP_TYPE_NOPS (exp);
207bf485 1779
487a92fe
JM
1780 if (TREE_NO_WARNING (orig_exp))
1781 TREE_NO_WARNING (exp) = 1;
207bf485 1782
f2a71bbc
JM
1783 ptrtype = build_pointer_type (restype);
1784
1785 if (TREE_CODE (exp) == INDIRECT_REF)
1786 return convert (ptrtype, TREE_OPERAND (exp, 0));
1787
c2255bc4 1788 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1789 return convert (ptrtype, adr);
1790}
207bf485 1791
f2a71bbc
JM
1792/* Convert the function expression EXP to a pointer. */
1793static tree
c2255bc4 1794function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1795{
1796 tree orig_exp = exp;
207bf485 1797
f2a71bbc 1798 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1799
f2a71bbc 1800 STRIP_TYPE_NOPS (exp);
207bf485 1801
f2a71bbc
JM
1802 if (TREE_NO_WARNING (orig_exp))
1803 TREE_NO_WARNING (exp) = 1;
207bf485 1804
c2255bc4 1805 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1806}
207bf485 1807
ebfbbdc5
JJ
1808/* Mark EXP as read, not just set, for set but not used -Wunused
1809 warning purposes. */
1810
1811void
1812mark_exp_read (tree exp)
1813{
1814 switch (TREE_CODE (exp))
1815 {
1816 case VAR_DECL:
1817 case PARM_DECL:
1818 DECL_READ_P (exp) = 1;
1819 break;
1820 case ARRAY_REF:
1821 case COMPONENT_REF:
1822 case MODIFY_EXPR:
1823 case REALPART_EXPR:
1824 case IMAGPART_EXPR:
1825 CASE_CONVERT:
1826 case ADDR_EXPR:
1827 mark_exp_read (TREE_OPERAND (exp, 0));
1828 break;
1829 case COMPOUND_EXPR:
82c3c067 1830 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1831 mark_exp_read (TREE_OPERAND (exp, 1));
1832 break;
1833 default:
1834 break;
1835 }
1836}
1837
f2a71bbc
JM
1838/* Perform the default conversion of arrays and functions to pointers.
1839 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1840 return EXP.
1841
1842 LOC is the location of the expression. */
f2a71bbc
JM
1843
1844struct c_expr
c2255bc4 1845default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1846{
1847 tree orig_exp = exp.value;
1848 tree type = TREE_TYPE (exp.value);
1849 enum tree_code code = TREE_CODE (type);
1850
1851 switch (code)
1852 {
1853 case ARRAY_TYPE:
1854 {
1855 bool not_lvalue = false;
1856 bool lvalue_array_p;
1857
1858 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1859 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1860 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1861 {
1862 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1863 not_lvalue = true;
1864 exp.value = TREE_OPERAND (exp.value, 0);
1865 }
1866
1867 if (TREE_NO_WARNING (orig_exp))
1868 TREE_NO_WARNING (exp.value) = 1;
1869
1870 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1871 if (!flag_isoc99 && !lvalue_array_p)
1872 {
1873 /* Before C99, non-lvalue arrays do not decay to pointers.
1874 Normally, using such an array would be invalid; but it can
1875 be used correctly inside sizeof or as a statement expression.
1876 Thus, do not give an error here; an error will result later. */
1877 return exp;
1878 }
1879
c2255bc4 1880 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1881 }
1882 break;
1883 case FUNCTION_TYPE:
c2255bc4 1884 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1885 break;
1886 default:
f2a71bbc 1887 break;
207bf485 1888 }
f2a71bbc 1889
207bf485
JM
1890 return exp;
1891}
1892
ebfbbdc5
JJ
1893struct c_expr
1894default_function_array_read_conversion (location_t loc, struct c_expr exp)
1895{
1896 mark_exp_read (exp.value);
1897 return default_function_array_conversion (loc, exp);
1898}
522ddfa2
JM
1899
1900/* EXP is an expression of integer type. Apply the integer promotions
1901 to it and return the promoted value. */
400fbf9f
JW
1902
1903tree
522ddfa2 1904perform_integral_promotions (tree exp)
400fbf9f 1905{
b3694847
SS
1906 tree type = TREE_TYPE (exp);
1907 enum tree_code code = TREE_CODE (type);
400fbf9f 1908
522ddfa2 1909 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 1910
400fbf9f
JW
1911 /* Normally convert enums to int,
1912 but convert wide enums to something wider. */
1913 if (code == ENUMERAL_TYPE)
1914 {
b0c48229
NB
1915 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
1916 TYPE_PRECISION (integer_type_node)),
1917 ((TYPE_PRECISION (type)
1918 >= TYPE_PRECISION (integer_type_node))
8df83eae 1919 && TYPE_UNSIGNED (type)));
05bccae2 1920
400fbf9f
JW
1921 return convert (type, exp);
1922 }
1923
522ddfa2
JM
1924 /* ??? This should no longer be needed now bit-fields have their
1925 proper types. */
9753f113 1926 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 1927 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 1928 /* If it's thinner than an int, promote it like a
d72040f5 1929 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
1930 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
1931 TYPE_PRECISION (integer_type_node)))
f458d1d5 1932 return convert (integer_type_node, exp);
9753f113 1933
d72040f5 1934 if (c_promoting_integer_type_p (type))
400fbf9f 1935 {
f458d1d5 1936 /* Preserve unsignedness if not really getting any wider. */
8df83eae 1937 if (TYPE_UNSIGNED (type)
f458d1d5 1938 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 1939 return convert (unsigned_type_node, exp);
05bccae2 1940
400fbf9f
JW
1941 return convert (integer_type_node, exp);
1942 }
05bccae2 1943
522ddfa2
JM
1944 return exp;
1945}
1946
1947
1948/* Perform default promotions for C data used in expressions.
46bdb9cf 1949 Enumeral types or short or char are converted to int.
522ddfa2
JM
1950 In addition, manifest constants symbols are replaced by their values. */
1951
1952tree
1953default_conversion (tree exp)
1954{
1955 tree orig_exp;
1956 tree type = TREE_TYPE (exp);
1957 enum tree_code code = TREE_CODE (type);
40449a90 1958 tree promoted_type;
522ddfa2 1959
ebfbbdc5
JJ
1960 mark_exp_read (exp);
1961
46bdb9cf
JM
1962 /* Functions and arrays have been converted during parsing. */
1963 gcc_assert (code != FUNCTION_TYPE);
1964 if (code == ARRAY_TYPE)
1965 return exp;
522ddfa2
JM
1966
1967 /* Constants can be used directly unless they're not loadable. */
1968 if (TREE_CODE (exp) == CONST_DECL)
1969 exp = DECL_INITIAL (exp);
1970
522ddfa2
JM
1971 /* Strip no-op conversions. */
1972 orig_exp = exp;
1973 STRIP_TYPE_NOPS (exp);
1974
1975 if (TREE_NO_WARNING (orig_exp))
1976 TREE_NO_WARNING (exp) = 1;
1977
400fbf9f
JW
1978 if (code == VOID_TYPE)
1979 {
1980 error ("void value not ignored as it ought to be");
1981 return error_mark_node;
1982 }
808d6eaa
JM
1983
1984 exp = require_complete_type (exp);
1985 if (exp == error_mark_node)
1986 return error_mark_node;
1987
40449a90
SL
1988 promoted_type = targetm.promoted_type (type);
1989 if (promoted_type)
1990 return convert (promoted_type, exp);
1991
808d6eaa
JM
1992 if (INTEGRAL_TYPE_P (type))
1993 return perform_integral_promotions (exp);
1994
400fbf9f
JW
1995 return exp;
1996}
1997\f
0fb96aa4 1998/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
1999
2000 If the component name is not found, returns NULL_TREE. Otherwise,
2001 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2002 stepping down the chain to the component, which is in the last
2003 TREE_VALUE of the list. Normally the list is of length one, but if
2004 the component is embedded within (nested) anonymous structures or
2005 unions, the list steps down the chain to the component. */
2f6e4e97 2006
2f2d13da 2007static tree
0fb96aa4 2008lookup_field (tree type, tree component)
2f2d13da
DE
2009{
2010 tree field;
2011
2012 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2013 to the field elements. Use a binary search on this array to quickly
2014 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2015 will always be set for structures which have many elements. */
2016
22a0b85f 2017 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2018 {
2019 int bot, top, half;
d07605f5 2020 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2021
2022 field = TYPE_FIELDS (type);
2023 bot = 0;
d07605f5 2024 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2025 while (top - bot > 1)
2026 {
2f2d13da
DE
2027 half = (top - bot + 1) >> 1;
2028 field = field_array[bot+half];
2029
2030 if (DECL_NAME (field) == NULL_TREE)
2031 {
2032 /* Step through all anon unions in linear fashion. */
2033 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2034 {
2f2d13da 2035 field = field_array[bot++];
a68b98cf
RK
2036 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2037 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2038 {
0fb96aa4 2039 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2040
2041 if (anon)
2042 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2043
2044 /* The Plan 9 compiler permits referring
2045 directly to an anonymous struct/union field
2046 using a typedef name. */
2047 if (flag_plan9_extensions
2048 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2049 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2050 == TYPE_DECL)
2051 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2052 == component))
2053 break;
2f6e4e97 2054 }
2f2d13da
DE
2055 }
2056
2057 /* Entire record is only anon unions. */
2058 if (bot > top)
2059 return NULL_TREE;
2060
2061 /* Restart the binary search, with new lower bound. */
2062 continue;
2063 }
2064
e8b87aac 2065 if (DECL_NAME (field) == component)
2f2d13da 2066 break;
e8b87aac 2067 if (DECL_NAME (field) < component)
2f2d13da
DE
2068 bot += half;
2069 else
2070 top = bot + half;
2071 }
2072
2073 if (DECL_NAME (field_array[bot]) == component)
2074 field = field_array[bot];
2075 else if (DECL_NAME (field) != component)
e9b2c823 2076 return NULL_TREE;
2f2d13da
DE
2077 }
2078 else
2079 {
910ad8de 2080 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2081 {
e9b2c823
NB
2082 if (DECL_NAME (field) == NULL_TREE
2083 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2084 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2085 {
0fb96aa4 2086 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2087
e9b2c823
NB
2088 if (anon)
2089 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2090
2091 /* The Plan 9 compiler permits referring directly to an
2092 anonymous struct/union field using a typedef
2093 name. */
2094 if (flag_plan9_extensions
2095 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2096 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2097 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2098 == component))
2099 break;
2f2d13da
DE
2100 }
2101
2102 if (DECL_NAME (field) == component)
2103 break;
2104 }
e9b2c823
NB
2105
2106 if (field == NULL_TREE)
2107 return NULL_TREE;
2f2d13da
DE
2108 }
2109
e9b2c823 2110 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2111}
2112
c2255bc4
AH
2113/* Make an expression to refer to the COMPONENT field of structure or
2114 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2115 location of the COMPONENT_REF. */
400fbf9f
JW
2116
2117tree
c2255bc4 2118build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2119{
b3694847
SS
2120 tree type = TREE_TYPE (datum);
2121 enum tree_code code = TREE_CODE (type);
2122 tree field = NULL;
2123 tree ref;
1e57bf47 2124 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2125
7a3ea201
RH
2126 if (!objc_is_public (datum, component))
2127 return error_mark_node;
2128
46a88c12 2129 /* Detect Objective-C property syntax object.property. */
668ea4b1 2130 if (c_dialect_objc ()
46a88c12 2131 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2132 return ref;
2133
400fbf9f
JW
2134 /* See if there is a field or component with name COMPONENT. */
2135
2136 if (code == RECORD_TYPE || code == UNION_TYPE)
2137 {
d0f062fb 2138 if (!COMPLETE_TYPE_P (type))
400fbf9f 2139 {
7a228918 2140 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2141 return error_mark_node;
2142 }
2143
0fb96aa4 2144 field = lookup_field (type, component);
400fbf9f
JW
2145
2146 if (!field)
2147 {
c2255bc4 2148 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2149 return error_mark_node;
2150 }
400fbf9f 2151
e9b2c823
NB
2152 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2153 This might be better solved in future the way the C++ front
2154 end does it - by giving the anonymous entities each a
2155 separate name and type, and then have build_component_ref
2156 recursively call itself. We can't do that here. */
46ea50cb 2157 do
19d76e60 2158 {
e9b2c823 2159 tree subdatum = TREE_VALUE (field);
efed193e
JM
2160 int quals;
2161 tree subtype;
1e57bf47 2162 bool use_datum_quals;
e9b2c823
NB
2163
2164 if (TREE_TYPE (subdatum) == error_mark_node)
2165 return error_mark_node;
2166
1e57bf47
JM
2167 /* If this is an rvalue, it does not have qualifiers in C
2168 standard terms and we must avoid propagating such
2169 qualifiers down to a non-lvalue array that is then
2170 converted to a pointer. */
2171 use_datum_quals = (datum_lvalue
2172 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2173
efed193e 2174 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2175 if (use_datum_quals)
2176 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2177 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2178
2179 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2180 NULL_TREE);
c2255bc4 2181 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2182 if (TREE_READONLY (subdatum)
2183 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2184 TREE_READONLY (ref) = 1;
1e57bf47
JM
2185 if (TREE_THIS_VOLATILE (subdatum)
2186 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2187 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2188
2189 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2190 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2191
19d76e60 2192 datum = ref;
46ea50cb
RS
2193
2194 field = TREE_CHAIN (field);
19d76e60 2195 }
46ea50cb 2196 while (field);
19d76e60 2197
400fbf9f
JW
2198 return ref;
2199 }
2200 else if (code != ERROR_MARK)
c2255bc4
AH
2201 error_at (loc,
2202 "request for member %qE in something not a structure or union",
2203 component);
400fbf9f
JW
2204
2205 return error_mark_node;
2206}
2207\f
2208/* Given an expression PTR for a pointer, return an expression
2209 for the value pointed to.
6a3799eb
AH
2210 ERRORSTRING is the name of the operator to appear in error messages.
2211
2212 LOC is the location to use for the generated tree. */
400fbf9f
JW
2213
2214tree
dd865ef6 2215build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2216{
b3694847
SS
2217 tree pointer = default_conversion (ptr);
2218 tree type = TREE_TYPE (pointer);
6a3799eb 2219 tree ref;
400fbf9f
JW
2220
2221 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2222 {
1043771b 2223 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2224 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2225 {
2226 /* If a warning is issued, mark it to avoid duplicates from
2227 the backend. This only needs to be done at
2228 warn_strict_aliasing > 2. */
2229 if (warn_strict_aliasing > 2)
2230 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2231 type, TREE_OPERAND (pointer, 0)))
2232 TREE_NO_WARNING (pointer) = 1;
2233 }
2234
870cc33b 2235 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2236 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2237 == TREE_TYPE (type)))
6a3799eb
AH
2238 {
2239 ref = TREE_OPERAND (pointer, 0);
2240 protected_set_expr_location (ref, loc);
2241 return ref;
2242 }
870cc33b
RS
2243 else
2244 {
2245 tree t = TREE_TYPE (type);
46df2823 2246
984dfd8c 2247 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2248
baae9b65 2249 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2250 {
c9f9eb5d 2251 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2252 return error_mark_node;
2253 }
7d882b83 2254 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2255 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2256
2257 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2258 so that we get the proper error message if the result is used
2259 to assign to. Also, &* is supposed to be a no-op.
2260 And ANSI C seems to specify that the type of the result
2261 should be the const type. */
2262 /* A de-reference of a pointer to const is not a const. It is valid
2263 to change it via some other pointer. */
2264 TREE_READONLY (ref) = TYPE_READONLY (t);
2265 TREE_SIDE_EFFECTS (ref)
271bd540 2266 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2267 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2268 protected_set_expr_location (ref, loc);
870cc33b
RS
2269 return ref;
2270 }
2271 }
400fbf9f 2272 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2273 invalid_indirection_error (loc, type, errstring);
2274
400fbf9f
JW
2275 return error_mark_node;
2276}
2277
2278/* This handles expressions of the form "a[i]", which denotes
2279 an array reference.
2280
2281 This is logically equivalent in C to *(a+i), but we may do it differently.
2282 If A is a variable or a member, we generate a primitive ARRAY_REF.
2283 This avoids forcing the array out of registers, and can work on
2284 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2285 by functions).
2286
30cd1c5d
AS
2287 For vector types, allow vector[i] but not i[vector], and create
2288 *(((type*)&vectortype) + i) for the expression.
2289
6a3799eb 2290 LOC is the location to use for the returned expression. */
400fbf9f
JW
2291
2292tree
c2255bc4 2293build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2294{
6a3799eb 2295 tree ret;
a4ab7973 2296 bool swapped = false;
400fbf9f
JW
2297 if (TREE_TYPE (array) == error_mark_node
2298 || TREE_TYPE (index) == error_mark_node)
2299 return error_mark_node;
2300
a4ab7973 2301 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2302 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2303 /* Allow vector[index] but not index[vector]. */
2304 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2305 {
a4ab7973
JM
2306 tree temp;
2307 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2308 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2309 {
f90e8e2e 2310 error_at (loc,
30cd1c5d
AS
2311 "subscripted value is neither array nor pointer nor vector");
2312
fdeefd49
RS
2313 return error_mark_node;
2314 }
a4ab7973
JM
2315 temp = array;
2316 array = index;
2317 index = temp;
2318 swapped = true;
2319 }
2320
2321 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2322 {
a63068b6 2323 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2324 return error_mark_node;
2325 }
2326
2327 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2328 {
a63068b6 2329 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2330 return error_mark_node;
2331 }
2332
ff6b6641
GDR
2333 /* ??? Existing practice has been to warn only when the char
2334 index is syntactically the index, not for char[array]. */
2335 if (!swapped)
2336 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2337
2338 /* Apply default promotions *after* noticing character types. */
2339 index = default_conversion (index);
2340
2341 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e
AS
2342
2343 /* For vector[index], convert the vector to a
30cd1c5d
AS
2344 pointer of the underlying type. */
2345 if (TREE_CODE (TREE_TYPE (array)) == VECTOR_TYPE)
2346 {
2347 tree type = TREE_TYPE (array);
2348 tree type1;
2349
2350 if (TREE_CODE (index) == INTEGER_CST)
f90e8e2e
AS
2351 if (!host_integerp (index, 1)
2352 || ((unsigned HOST_WIDE_INT) tree_low_cst (index, 1)
30cd1c5d
AS
2353 >= TYPE_VECTOR_SUBPARTS (TREE_TYPE (array))))
2354 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
f90e8e2e 2355
30cd1c5d
AS
2356 c_common_mark_addressable_vec (array);
2357 type = build_qualified_type (TREE_TYPE (type), TYPE_QUALS (type));
2358 type = build_pointer_type (type);
2359 type1 = build_pointer_type (TREE_TYPE (array));
2360 array = build1 (ADDR_EXPR, type1, array);
2361 array = convert (type, array);
2362 }
a4ab7973
JM
2363
2364 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2365 {
e4d35515 2366 tree rval, type;
fdeefd49 2367
400fbf9f
JW
2368 /* An array that is indexed by a non-constant
2369 cannot be stored in a register; we must be able to do
2370 address arithmetic on its address.
2371 Likewise an array of elements of variable size. */
2372 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2373 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2374 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2375 {
dffd7eb6 2376 if (!c_mark_addressable (array))
400fbf9f
JW
2377 return error_mark_node;
2378 }
e6d52559
JW
2379 /* An array that is indexed by a constant value which is not within
2380 the array bounds cannot be stored in a register either; because we
2381 would get a crash in store_bit_field/extract_bit_field when trying
2382 to access a non-existent part of the register. */
2383 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2384 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2385 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2386 {
dffd7eb6 2387 if (!c_mark_addressable (array))
e6d52559
JW
2388 return error_mark_node;
2389 }
400fbf9f 2390
400fbf9f
JW
2391 if (pedantic)
2392 {
2393 tree foo = array;
2394 while (TREE_CODE (foo) == COMPONENT_REF)
2395 foo = TREE_OPERAND (foo, 0);
5baeaac0 2396 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2397 pedwarn (loc, OPT_Wpedantic,
fcf73884 2398 "ISO C forbids subscripting %<register%> array");
3f75a254 2399 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2400 pedwarn (loc, OPT_Wpedantic,
fcf73884 2401 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2402 }
2403
46df2823 2404 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2405 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2406 /* Array ref is const/volatile if the array elements are
c22cacf3 2407 or if the array is. */
400fbf9f
JW
2408 TREE_READONLY (rval)
2409 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2410 | TREE_READONLY (array));
2411 TREE_SIDE_EFFECTS (rval)
2412 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2413 | TREE_SIDE_EFFECTS (array));
2414 TREE_THIS_VOLATILE (rval)
2415 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2416 /* This was added by rms on 16 Nov 91.
2f6e4e97 2417 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2418 in an inline function.
2419 Hope it doesn't break something else. */
2420 | TREE_THIS_VOLATILE (array));
928c19bb 2421 ret = require_complete_type (rval);
6a3799eb
AH
2422 protected_set_expr_location (ret, loc);
2423 return ret;
400fbf9f 2424 }
a4ab7973
JM
2425 else
2426 {
2427 tree ar = default_conversion (array);
400fbf9f 2428
a4ab7973
JM
2429 if (ar == error_mark_node)
2430 return ar;
400fbf9f 2431
a4ab7973
JM
2432 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2433 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2434
ba47d38d 2435 return build_indirect_ref
c9f9eb5d 2436 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2437 RO_ARRAY_INDEXING);
a4ab7973 2438 }
400fbf9f
JW
2439}
2440\f
7e585d16 2441/* Build an external reference to identifier ID. FUN indicates
766beb40 2442 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2443 location of the identifier. This sets *TYPE to the type of the
2444 identifier, which is not the same as the type of the returned value
2445 for CONST_DECLs defined as enum constants. If the type of the
2446 identifier is not available, *TYPE is set to NULL. */
7e585d16 2447tree
c2255bc4 2448build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2449{
2450 tree ref;
2451 tree decl = lookup_name (id);
16b34ad6
ZL
2452
2453 /* In Objective-C, an instance variable (ivar) may be preferred to
2454 whatever lookup_name() found. */
2455 decl = objc_lookup_ivar (decl, id);
7e585d16 2456
6866c6e8 2457 *type = NULL;
339a28b9 2458 if (decl && decl != error_mark_node)
6866c6e8
ILT
2459 {
2460 ref = decl;
2461 *type = TREE_TYPE (ref);
2462 }
339a28b9
ZW
2463 else if (fun)
2464 /* Implicit function declaration. */
c2255bc4 2465 ref = implicitly_declare (loc, id);
339a28b9
ZW
2466 else if (decl == error_mark_node)
2467 /* Don't complain about something that's already been
2468 complained about. */
2469 return error_mark_node;
2470 else
2471 {
c2255bc4 2472 undeclared_variable (loc, id);
339a28b9
ZW
2473 return error_mark_node;
2474 }
7e585d16
ZW
2475
2476 if (TREE_TYPE (ref) == error_mark_node)
2477 return error_mark_node;
2478
339a28b9 2479 if (TREE_DEPRECATED (ref))
9b86d6bb 2480 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2481
ad960f56 2482 /* Recursive call does not count as usage. */
b8698a0f 2483 if (ref != current_function_decl)
ad960f56 2484 {
ad960f56
MLI
2485 TREE_USED (ref) = 1;
2486 }
7e585d16 2487
bc4b653b
JM
2488 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2489 {
2490 if (!in_sizeof && !in_typeof)
2491 C_DECL_USED (ref) = 1;
2492 else if (DECL_INITIAL (ref) == 0
2493 && DECL_EXTERNAL (ref)
2494 && !TREE_PUBLIC (ref))
2495 record_maybe_used_decl (ref);
2496 }
2497
7e585d16
ZW
2498 if (TREE_CODE (ref) == CONST_DECL)
2499 {
6193b8b7 2500 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2501
2502 if (warn_cxx_compat
2503 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2504 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2505 {
2506 warning_at (loc, OPT_Wc___compat,
2507 ("enum constant defined in struct or union "
2508 "is not visible in C++"));
2509 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2510 }
2511
7e585d16
ZW
2512 ref = DECL_INITIAL (ref);
2513 TREE_CONSTANT (ref) = 1;
2514 }
6a29edea 2515 else if (current_function_decl != 0
4b1e44be 2516 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2517 && (TREE_CODE (ref) == VAR_DECL
2518 || TREE_CODE (ref) == PARM_DECL
2519 || TREE_CODE (ref) == FUNCTION_DECL))
2520 {
2521 tree context = decl_function_context (ref);
2f6e4e97 2522
6a29edea
EB
2523 if (context != 0 && context != current_function_decl)
2524 DECL_NONLOCAL (ref) = 1;
2525 }
71113fcd
GK
2526 /* C99 6.7.4p3: An inline definition of a function with external
2527 linkage ... shall not contain a reference to an identifier with
2528 internal linkage. */
2529 else if (current_function_decl != 0
2530 && DECL_DECLARED_INLINE_P (current_function_decl)
2531 && DECL_EXTERNAL (current_function_decl)
2532 && VAR_OR_FUNCTION_DECL_P (ref)
2533 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2534 && ! TREE_PUBLIC (ref)
2535 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2536 record_inline_static (loc, current_function_decl, ref,
2537 csi_internal);
7e585d16
ZW
2538
2539 return ref;
2540}
2541
bc4b653b
JM
2542/* Record details of decls possibly used inside sizeof or typeof. */
2543struct maybe_used_decl
2544{
2545 /* The decl. */
2546 tree decl;
2547 /* The level seen at (in_sizeof + in_typeof). */
2548 int level;
2549 /* The next one at this level or above, or NULL. */
2550 struct maybe_used_decl *next;
2551};
2552
2553static struct maybe_used_decl *maybe_used_decls;
2554
2555/* Record that DECL, an undefined static function reference seen
2556 inside sizeof or typeof, might be used if the operand of sizeof is
2557 a VLA type or the operand of typeof is a variably modified
2558 type. */
2559
4e2fb7de 2560static void
bc4b653b
JM
2561record_maybe_used_decl (tree decl)
2562{
2563 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2564 t->decl = decl;
2565 t->level = in_sizeof + in_typeof;
2566 t->next = maybe_used_decls;
2567 maybe_used_decls = t;
2568}
2569
2570/* Pop the stack of decls possibly used inside sizeof or typeof. If
2571 USED is false, just discard them. If it is true, mark them used
2572 (if no longer inside sizeof or typeof) or move them to the next
2573 level up (if still inside sizeof or typeof). */
2574
2575void
2576pop_maybe_used (bool used)
2577{
2578 struct maybe_used_decl *p = maybe_used_decls;
2579 int cur_level = in_sizeof + in_typeof;
2580 while (p && p->level > cur_level)
2581 {
2582 if (used)
2583 {
2584 if (cur_level == 0)
2585 C_DECL_USED (p->decl) = 1;
2586 else
2587 p->level = cur_level;
2588 }
2589 p = p->next;
2590 }
2591 if (!used || cur_level == 0)
2592 maybe_used_decls = p;
2593}
2594
2595/* Return the result of sizeof applied to EXPR. */
2596
2597struct c_expr
c2255bc4 2598c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2599{
2600 struct c_expr ret;
ad97f4be
JM
2601 if (expr.value == error_mark_node)
2602 {
2603 ret.value = error_mark_node;
2604 ret.original_code = ERROR_MARK;
6866c6e8 2605 ret.original_type = NULL;
ad97f4be
JM
2606 pop_maybe_used (false);
2607 }
2608 else
2609 {
928c19bb
JM
2610 bool expr_const_operands = true;
2611 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2612 &expr_const_operands);
c2255bc4 2613 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
ad97f4be 2614 ret.original_code = ERROR_MARK;
6866c6e8 2615 ret.original_type = NULL;
928c19bb 2616 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2617 {
2618 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2619 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2620 folded_expr, ret.value);
2621 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2622 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2623 }
928c19bb 2624 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2625 }
bc4b653b
JM
2626 return ret;
2627}
2628
2629/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2630 name passed to sizeof (rather than the type itself). LOC is the
2631 location of the original expression. */
bc4b653b
JM
2632
2633struct c_expr
c2255bc4 2634c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2635{
2636 tree type;
2637 struct c_expr ret;
928c19bb
JM
2638 tree type_expr = NULL_TREE;
2639 bool type_expr_const = true;
2640 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2641 ret.value = c_sizeof (loc, type);
bc4b653b 2642 ret.original_code = ERROR_MARK;
6866c6e8 2643 ret.original_type = NULL;
24070fcb
JM
2644 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2645 && c_vla_type_p (type))
928c19bb 2646 {
24070fcb
JM
2647 /* If the type is a [*] array, it is a VLA but is represented as
2648 having a size of zero. In such a case we must ensure that
2649 the result of sizeof does not get folded to a constant by
2650 c_fully_fold, because if the size is evaluated the result is
2651 not constant and so constraints on zero or negative size
2652 arrays must not be applied when this sizeof call is inside
2653 another array declarator. */
2654 if (!type_expr)
2655 type_expr = integer_zero_node;
928c19bb
JM
2656 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2657 type_expr, ret.value);
2658 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2659 }
16464cc1
VR
2660 pop_maybe_used (type != error_mark_node
2661 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2662 return ret;
2663}
2664
400fbf9f 2665/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2666 The function call is at LOC.
400fbf9f
JW
2667 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2668 TREE_VALUE of each node is a parameter-expression.
2669 FUNCTION's data type may be a function type or a pointer-to-function. */
2670
2671tree
c2255bc4 2672build_function_call (location_t loc, tree function, tree params)
bbbbb16a
ILT
2673{
2674 VEC(tree,gc) *vec;
2675 tree ret;
2676
2677 vec = VEC_alloc (tree, gc, list_length (params));
2678 for (; params; params = TREE_CHAIN (params))
2679 VEC_quick_push (tree, vec, TREE_VALUE (params));
c2255bc4 2680 ret = build_function_call_vec (loc, function, vec, NULL);
bbbbb16a
ILT
2681 VEC_free (tree, gc, vec);
2682 return ret;
2683}
2684
ae52741c
MLI
2685/* Give a note about the location of the declaration of DECL. */
2686
2687static void inform_declaration (tree decl)
2688{
2689 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2690 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2691}
2692
bbbbb16a
ILT
2693/* Build a function call to function FUNCTION with parameters PARAMS.
2694 ORIGTYPES, if not NULL, is a vector of types; each element is
2695 either NULL or the original type of the corresponding element in
2696 PARAMS. The original type may differ from TREE_TYPE of the
2697 parameter for enums. FUNCTION's data type may be a function type
2698 or pointer-to-function. This function changes the elements of
2699 PARAMS. */
2700
2701tree
c2255bc4 2702build_function_call_vec (location_t loc, tree function, VEC(tree,gc) *params,
bbbbb16a 2703 VEC(tree,gc) *origtypes)
400fbf9f 2704{
b3694847 2705 tree fntype, fundecl = 0;
4977bab6 2706 tree name = NULL_TREE, result;
c96f4f73 2707 tree tem;
94a0dd7b
SL
2708 int nargs;
2709 tree *argarray;
b8698a0f 2710
400fbf9f 2711
fc76e425 2712 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2713 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2714
2715 /* Convert anything with function type to a pointer-to-function. */
2716 if (TREE_CODE (function) == FUNCTION_DECL)
2717 {
58646b77
PB
2718 /* Implement type-directed function overloading for builtins.
2719 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2720 handle all the type checking. The result is a complete expression
2721 that implements this function call. */
c2255bc4 2722 tem = resolve_overloaded_builtin (loc, function, params);
58646b77
PB
2723 if (tem)
2724 return tem;
48ae6c13 2725
400fbf9f 2726 name = DECL_NAME (function);
0a35513e
AH
2727
2728 if (flag_tm)
2729 tm_malloc_replacement (function);
a5eadacc 2730 fundecl = function;
86951993
AM
2731 /* Atomic functions have type checking/casting already done. They are
2732 often rewritten and don't match the original parameter list. */
2733 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2734 origtypes = NULL;
400fbf9f 2735 }
f2a71bbc 2736 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2737 function = function_to_pointer_conversion (loc, function);
400fbf9f 2738
6e955430
ZL
2739 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2740 expressions, like those used for ObjC messenger dispatches. */
bbbbb16a
ILT
2741 if (!VEC_empty (tree, params))
2742 function = objc_rewrite_function_call (function,
2743 VEC_index (tree, params, 0));
6e955430 2744
928c19bb
JM
2745 function = c_fully_fold (function, false, NULL);
2746
400fbf9f
JW
2747 fntype = TREE_TYPE (function);
2748
2749 if (TREE_CODE (fntype) == ERROR_MARK)
2750 return error_mark_node;
2751
2752 if (!(TREE_CODE (fntype) == POINTER_TYPE
2753 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2754 {
ae52741c
MLI
2755 if (!flag_diagnostics_show_caret)
2756 error_at (loc,
2757 "called object %qE is not a function or function pointer",
2758 function);
2759 else if (DECL_P (function))
2760 {
2761 error_at (loc,
2762 "called object %qD is not a function or function pointer",
2763 function);
2764 inform_declaration (function);
2765 }
2766 else
2767 error_at (loc,
2768 "called object is not a function or function pointer");
400fbf9f
JW
2769 return error_mark_node;
2770 }
2771
5ce89b2e
JM
2772 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2773 current_function_returns_abnormally = 1;
2774
400fbf9f
JW
2775 /* fntype now gets the type of function pointed to. */
2776 fntype = TREE_TYPE (fntype);
2777
ab4194da
JM
2778 /* Convert the parameters to the types declared in the
2779 function prototype, or apply default promotions. */
2780
bbbbb16a
ILT
2781 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2782 function, fundecl);
ab4194da
JM
2783 if (nargs < 0)
2784 return error_mark_node;
2785
c96f4f73
EB
2786 /* Check that the function is called through a compatible prototype.
2787 If it is not, replace the call by a trap, wrapped up in a compound
2788 expression if necessary. This has the nice side-effect to prevent
2789 the tree-inliner from generating invalid assignment trees which may
58393038 2790 blow up in the RTL expander later. */
1043771b 2791 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2792 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2793 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2794 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2795 {
2796 tree return_type = TREE_TYPE (fntype);
e79983f4
MM
2797 tree trap = build_function_call (loc,
2798 builtin_decl_explicit (BUILT_IN_TRAP),
c96f4f73 2799 NULL_TREE);
ab4194da 2800 int i;
c96f4f73
EB
2801
2802 /* This situation leads to run-time undefined behavior. We can't,
2803 therefore, simply error unless we can prove that all possible
2804 executions of the program must execute the code. */
c2255bc4 2805 if (warning_at (loc, 0, "function called through a non-compatible type"))
71205d17
MLI
2806 /* We can, however, treat "undefined" any way we please.
2807 Call abort to encourage the user to fix the program. */
c2255bc4 2808 inform (loc, "if this code is reached, the program will abort");
ab4194da
JM
2809 /* Before the abort, allow the function arguments to exit or
2810 call longjmp. */
2811 for (i = 0; i < nargs; i++)
bbbbb16a
ILT
2812 trap = build2 (COMPOUND_EXPR, void_type_node,
2813 VEC_index (tree, params, i), trap);
bba745c1 2814
c96f4f73 2815 if (VOID_TYPE_P (return_type))
3ce62965
JM
2816 {
2817 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
db3927fb 2818 pedwarn (loc, 0,
3ce62965
JM
2819 "function with qualified void return type called");
2820 return trap;
2821 }
c96f4f73
EB
2822 else
2823 {
2824 tree rhs;
2825
2826 if (AGGREGATE_TYPE_P (return_type))
c2255bc4 2827 rhs = build_compound_literal (loc, return_type,
928c19bb
JM
2828 build_constructor (return_type, 0),
2829 false);
c96f4f73 2830 else
e8160c9a 2831 rhs = build_zero_cst (return_type);
c96f4f73 2832
3ce62965
JM
2833 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2834 trap, rhs));
c96f4f73
EB
2835 }
2836 }
2837
bbbbb16a
ILT
2838 argarray = VEC_address (tree, params);
2839
83322951
RG
2840 /* Check that arguments to builtin functions match the expectations. */
2841 if (fundecl
2842 && DECL_BUILT_IN (fundecl)
2843 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2844 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2845 return error_mark_node;
400fbf9f 2846
83322951 2847 /* Check that the arguments to the function are valid. */
dde05067 2848 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2849
928c19bb
JM
2850 if (name != NULL_TREE
2851 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2852 {
928c19bb 2853 if (require_constant_value)
b8698a0f 2854 result =
db3927fb
AH
2855 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2856 function, nargs, argarray);
928c19bb 2857 else
db3927fb
AH
2858 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2859 function, nargs, argarray);
928c19bb
JM
2860 if (TREE_CODE (result) == NOP_EXPR
2861 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2862 STRIP_TYPE_NOPS (result);
bf730f15
RS
2863 }
2864 else
db3927fb
AH
2865 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2866 function, nargs, argarray);
b0b3afb2 2867
71653180 2868 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2869 {
2870 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2871 pedwarn (loc, 0,
3ce62965
JM
2872 "function with qualified void return type called");
2873 return result;
2874 }
1eb8759b 2875 return require_complete_type (result);
400fbf9f 2876}
f90e8e2e 2877
2205ed25 2878/* Build a VEC_PERM_EXPR if V0, V1 and MASK are not error_mark_nodes
f90e8e2e
AS
2879 and have vector types, V0 has the same type as V1, and the number of
2880 elements of V0, V1, MASK is the same.
2881
2882 In case V1 is a NULL_TREE it is assumed that __builtin_shuffle was
2883 called with two arguments. In this case implementation passes the
2884 first argument twice in order to share the same tree code. This fact
2885 could enable the mask-values being twice the vector length. This is
2886 an implementation accident and this semantics is not guaranteed to
2887 the user. */
2888tree
2205ed25 2889c_build_vec_perm_expr (location_t loc, tree v0, tree v1, tree mask)
f90e8e2e 2890{
2205ed25 2891 tree ret;
f90e8e2e
AS
2892 bool wrap = true;
2893 bool maybe_const = false;
75799b98 2894 bool two_arguments = false;
f90e8e2e
AS
2895
2896 if (v1 == NULL_TREE)
2897 {
2898 two_arguments = true;
2899 v1 = v0;
2900 }
2901
2902 if (v0 == error_mark_node || v1 == error_mark_node
2903 || mask == error_mark_node)
2904 return error_mark_node;
2905
2906 if (TREE_CODE (TREE_TYPE (mask)) != VECTOR_TYPE
2907 || TREE_CODE (TREE_TYPE (TREE_TYPE (mask))) != INTEGER_TYPE)
2908 {
2909 error_at (loc, "__builtin_shuffle last argument must "
2910 "be an integer vector");
2911 return error_mark_node;
2912 }
2913
2914 if (TREE_CODE (TREE_TYPE (v0)) != VECTOR_TYPE
2915 || TREE_CODE (TREE_TYPE (v1)) != VECTOR_TYPE)
2916 {
2917 error_at (loc, "__builtin_shuffle arguments must be vectors");
2918 return error_mark_node;
2919 }
2920
2921 if (TYPE_MAIN_VARIANT (TREE_TYPE (v0)) != TYPE_MAIN_VARIANT (TREE_TYPE (v1)))
2922 {
2923 error_at (loc, "__builtin_shuffle argument vectors must be of "
2924 "the same type");
2925 return error_mark_node;
2926 }
2927
2928 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0))
2929 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))
2930 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1))
2931 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
2932 {
2933 error_at (loc, "__builtin_shuffle number of elements of the "
2934 "argument vector(s) and the mask vector should "
2935 "be the same");
2936 return error_mark_node;
2937 }
2938
2939 if (GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (v0))))
2940 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (mask)))))
2941 {
2942 error_at (loc, "__builtin_shuffle argument vector(s) inner type "
2943 "must have the same size as inner type of the mask");
2944 return error_mark_node;
2945 }
2946
2205ed25 2947 /* Avoid C_MAYBE_CONST_EXPRs inside VEC_PERM_EXPR. */
75799b98 2948 v0 = c_fully_fold (v0, false, &maybe_const);
f90e8e2e
AS
2949 wrap &= maybe_const;
2950
75799b98
RH
2951 if (two_arguments)
2952 v1 = v0 = save_expr (v0);
2953 else
f90e8e2e
AS
2954 {
2955 v1 = c_fully_fold (v1, false, &maybe_const);
2956 wrap &= maybe_const;
2957 }
f90e8e2e
AS
2958
2959 mask = c_fully_fold (mask, false, &maybe_const);
2960 wrap &= maybe_const;
2961
cdbb5ba3 2962 ret = build3_loc (loc, VEC_PERM_EXPR, TREE_TYPE (v0), v0, v1, mask);
f90e8e2e
AS
2963
2964 if (!wrap)
2205ed25 2965 ret = c_wrap_maybe_const (ret, true);
f90e8e2e 2966
2205ed25 2967 return ret;
f90e8e2e 2968}
400fbf9f 2969\f
bbbbb16a
ILT
2970/* Convert the argument expressions in the vector VALUES
2971 to the types in the list TYPELIST.
400fbf9f
JW
2972
2973 If TYPELIST is exhausted, or when an element has NULL as its type,
2974 perform the default conversions.
2975
bbbbb16a
ILT
2976 ORIGTYPES is the original types of the expressions in VALUES. This
2977 holds the type of enum values which have been converted to integral
2978 types. It may be NULL.
400fbf9f 2979
03dafa61
JM
2980 FUNCTION is a tree for the called function. It is used only for
2981 error messages, where it is formatted with %qE.
400fbf9f
JW
2982
2983 This is also where warnings about wrong number of args are generated.
2984
94a0dd7b 2985 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
2986 than the length of VALUES in some error situations), or -1 on
2987 failure. */
94a0dd7b
SL
2988
2989static int
bbbbb16a
ILT
2990convert_arguments (tree typelist, VEC(tree,gc) *values,
2991 VEC(tree,gc) *origtypes, tree function, tree fundecl)
400fbf9f 2992{
bbbbb16a
ILT
2993 tree typetail, val;
2994 unsigned int parmnum;
06302a02 2995 bool error_args = false;
b5d32c25
KG
2996 const bool type_generic = fundecl
2997 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
8ce94e44 2998 bool type_generic_remove_excess_precision = false;
03dafa61 2999 tree selector;
03dafa61 3000
2ac2f164
JM
3001 /* Change pointer to function to the function itself for
3002 diagnostics. */
03dafa61
JM
3003 if (TREE_CODE (function) == ADDR_EXPR
3004 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3005 function = TREE_OPERAND (function, 0);
03dafa61
JM
3006
3007 /* Handle an ObjC selector specially for diagnostics. */
3008 selector = objc_message_selector ();
400fbf9f 3009
8ce94e44
JM
3010 /* For type-generic built-in functions, determine whether excess
3011 precision should be removed (classification) or not
3012 (comparison). */
3013 if (type_generic
3014 && DECL_BUILT_IN (fundecl)
3015 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3016 {
3017 switch (DECL_FUNCTION_CODE (fundecl))
3018 {
3019 case BUILT_IN_ISFINITE:
3020 case BUILT_IN_ISINF:
3021 case BUILT_IN_ISINF_SIGN:
3022 case BUILT_IN_ISNAN:
3023 case BUILT_IN_ISNORMAL:
3024 case BUILT_IN_FPCLASSIFY:
3025 type_generic_remove_excess_precision = true;
3026 break;
3027
3028 default:
3029 type_generic_remove_excess_precision = false;
3030 break;
3031 }
3032 }
3033
400fbf9f 3034 /* Scan the given expressions and types, producing individual
bbbbb16a 3035 converted arguments. */
400fbf9f 3036
bbbbb16a
ILT
3037 for (typetail = typelist, parmnum = 0;
3038 VEC_iterate (tree, values, parmnum, val);
3039 ++parmnum)
400fbf9f 3040 {
b3694847 3041 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3042 tree valtype = TREE_TYPE (val);
03dafa61
JM
3043 tree rname = function;
3044 int argnum = parmnum + 1;
4d3e6fae 3045 const char *invalid_func_diag;
8ce94e44 3046 bool excess_precision = false;
928c19bb 3047 bool npc;
bbbbb16a 3048 tree parmval;
400fbf9f
JW
3049
3050 if (type == void_type_node)
3051 {
19dc6d01
NP
3052 if (selector)
3053 error_at (input_location,
3054 "too many arguments to method %qE", selector);
3055 else
3056 error_at (input_location,
3057 "too many arguments to function %qE", function);
ae52741c 3058 inform_declaration (fundecl);
94a0dd7b 3059 return parmnum;
400fbf9f
JW
3060 }
3061
03dafa61
JM
3062 if (selector && argnum > 2)
3063 {
3064 rname = selector;
3065 argnum -= 2;
3066 }
3067
928c19bb 3068 npc = null_pointer_constant_p (val);
8ce94e44
JM
3069
3070 /* If there is excess precision and a prototype, convert once to
3071 the required type rather than converting via the semantic
3072 type. Likewise without a prototype a float value represented
3073 as long double should be converted once to double. But for
3074 type-generic classification functions excess precision must
3075 be removed here. */
3076 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3077 && (type || !type_generic || !type_generic_remove_excess_precision))
3078 {
3079 val = TREE_OPERAND (val, 0);
3080 excess_precision = true;
3081 }
928c19bb 3082 val = c_fully_fold (val, false, NULL);
ed248cf7 3083 STRIP_TYPE_NOPS (val);
400fbf9f 3084
400fbf9f
JW
3085 val = require_complete_type (val);
3086
3087 if (type != 0)
3088 {
3089 /* Formal parm type is specified by a function prototype. */
400fbf9f 3090
20913689 3091 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
3092 {
3093 error ("type of formal parameter %d is incomplete", parmnum + 1);
3094 parmval = val;
3095 }
3096 else
3097 {
bbbbb16a
ILT
3098 tree origtype;
3099
d45cf215
RS
3100 /* Optionally warn about conversions that
3101 differ from the default conversions. */
05170031 3102 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3103 {
e3a64162 3104 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3105
aae43c5f 3106 if (INTEGRAL_TYPE_P (type)
8ce94e44 3107 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3108 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3109 "rather than floating due to prototype",
3110 argnum, rname);
03829ad2 3111 if (INTEGRAL_TYPE_P (type)
8ce94e44 3112 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3113 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3114 "rather than complex due to prototype",
3115 argnum, rname);
aae43c5f 3116 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3117 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3118 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3119 "rather than floating due to prototype",
3120 argnum, rname);
400fbf9f 3121 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3122 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3123 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3124 "rather than integer due to prototype",
3125 argnum, rname);
03829ad2 3126 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3127 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3128 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3129 "rather than integer due to prototype",
3130 argnum, rname);
aae43c5f 3131 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3132 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3133 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3134 "rather than complex due to prototype",
3135 argnum, rname);
aae43c5f
RK
3136 /* ??? At some point, messages should be written about
3137 conversions between complex types, but that's too messy
3138 to do now. */
d45cf215 3139 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3140 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3141 {
3142 /* Warn if any argument is passed as `float',
047de90b 3143 since without a prototype it would be `double'. */
9a8ce21f
JG
3144 if (formal_prec == TYPE_PRECISION (float_type_node)
3145 && type != dfloat32_type_node)
d4ee4d25 3146 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
3147 "rather than %<double%> due to prototype",
3148 argnum, rname);
9a8ce21f
JG
3149
3150 /* Warn if mismatch between argument and prototype
3151 for decimal float types. Warn of conversions with
3152 binary float types and of precision narrowing due to
3153 prototype. */
8ce94e44 3154 else if (type != valtype
9a8ce21f
JG
3155 && (type == dfloat32_type_node
3156 || type == dfloat64_type_node
c22cacf3 3157 || type == dfloat128_type_node
8ce94e44
JM
3158 || valtype == dfloat32_type_node
3159 || valtype == dfloat64_type_node
3160 || valtype == dfloat128_type_node)
c22cacf3 3161 && (formal_prec
8ce94e44 3162 <= TYPE_PRECISION (valtype)
9a8ce21f 3163 || (type == dfloat128_type_node
8ce94e44 3164 && (valtype
c22cacf3 3165 != dfloat64_type_node
8ce94e44 3166 && (valtype
9a8ce21f
JG
3167 != dfloat32_type_node)))
3168 || (type == dfloat64_type_node
8ce94e44 3169 && (valtype
9a8ce21f
JG
3170 != dfloat32_type_node))))
3171 warning (0, "passing argument %d of %qE as %qT "
3172 "rather than %qT due to prototype",
8ce94e44 3173 argnum, rname, type, valtype);
9a8ce21f 3174
d45cf215 3175 }
3ed56f8a
KG
3176 /* Detect integer changing in width or signedness.
3177 These warnings are only activated with
05170031
MLI
3178 -Wtraditional-conversion, not with -Wtraditional. */
3179 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3180 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3181 {
d45cf215
RS
3182 tree would_have_been = default_conversion (val);
3183 tree type1 = TREE_TYPE (would_have_been);
3184
754a4d82 3185 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3186 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3187 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3188 /* No warning if function asks for enum
3189 and the actual arg is that enum type. */
3190 ;
3191 else if (formal_prec != TYPE_PRECISION (type1))
c2255bc4
AH
3192 warning (OPT_Wtraditional_conversion,
3193 "passing argument %d of %qE "
3176a0c2
DD
3194 "with different width due to prototype",
3195 argnum, rname);
8df83eae 3196 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3197 ;
800cd3b9
RS
3198 /* Don't complain if the formal parameter type
3199 is an enum, because we can't tell now whether
3200 the value was an enum--even the same enum. */
3201 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3202 ;
400fbf9f
JW
3203 else if (TREE_CODE (val) == INTEGER_CST
3204 && int_fits_type_p (val, type))
3205 /* Change in signedness doesn't matter
3206 if a constant value is unaffected. */
3207 ;
ce9895ae
RS
3208 /* If the value is extended from a narrower
3209 unsigned type, it doesn't matter whether we
3210 pass it as signed or unsigned; the value
3211 certainly is the same either way. */
8ce94e44
JM
3212 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3213 && TYPE_UNSIGNED (valtype))
ce9895ae 3214 ;
8df83eae 3215 else if (TYPE_UNSIGNED (type))
c2255bc4
AH
3216 warning (OPT_Wtraditional_conversion,
3217 "passing argument %d of %qE "
3176a0c2
DD
3218 "as unsigned due to prototype",
3219 argnum, rname);
3ed56f8a 3220 else
c2255bc4
AH
3221 warning (OPT_Wtraditional_conversion,
3222 "passing argument %d of %qE "
3176a0c2 3223 "as signed due to prototype", argnum, rname);
400fbf9f
JW
3224 }
3225 }
3226
8ce94e44
JM
3227 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3228 sake of better warnings from convert_and_check. */
3229 if (excess_precision)
3230 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
bbbbb16a
ILT
3231 origtype = (origtypes == NULL
3232 ? NULL_TREE
3233 : VEC_index (tree, origtypes, parmnum));
744aa42f
ILT
3234 parmval = convert_for_assignment (input_location, type, val,
3235 origtype, ic_argpass, npc,
2ac2f164
JM
3236 fundecl, function,
3237 parmnum + 1);
2f6e4e97 3238
61f71b34 3239 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3240 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3241 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3242 parmval = default_conversion (parmval);
400fbf9f 3243 }
400fbf9f 3244 }
8ce94e44
JM
3245 else if (TREE_CODE (valtype) == REAL_TYPE
3246 && (TYPE_PRECISION (valtype)
c22cacf3 3247 < TYPE_PRECISION (double_type_node))
8ce94e44 3248 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3249 {
3250 if (type_generic)
bbbbb16a 3251 parmval = val;
b5d32c25 3252 else
0a0b3574
MM
3253 {
3254 /* Convert `float' to `double'. */
3255 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3256 warning (OPT_Wdouble_promotion,
3257 "implicit conversion from %qT to %qT when passing "
3258 "argument to function",
3259 valtype, double_type_node);
3260 parmval = convert (double_type_node, val);
3261 }
b5d32c25 3262 }
8ce94e44
JM
3263 else if (excess_precision && !type_generic)
3264 /* A "double" argument with excess precision being passed
3265 without a prototype or in variable arguments. */
bbbbb16a 3266 parmval = convert (valtype, val);
c22cacf3
MS
3267 else if ((invalid_func_diag =
3268 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3269 {
3270 error (invalid_func_diag);
94a0dd7b 3271 return -1;
4d3e6fae 3272 }
400fbf9f
JW
3273 else
3274 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3275 parmval = default_conversion (val);
3276
3277 VEC_replace (tree, values, parmnum, parmval);
06302a02
JJ
3278 if (parmval == error_mark_node)
3279 error_args = true;
400fbf9f
JW
3280
3281 if (typetail)
3282 typetail = TREE_CHAIN (typetail);
3283 }
3284
bbbbb16a 3285 gcc_assert (parmnum == VEC_length (tree, values));
94a0dd7b 3286
400fbf9f 3287 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3288 {
f90e8e2e 3289 error_at (input_location,
a98c2819 3290 "too few arguments to function %qE", function);
ae52741c 3291 inform_declaration (fundecl);
94a0dd7b 3292 return -1;
3789b316 3293 }
400fbf9f 3294
06302a02 3295 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3296}
3297\f
43f6dfd3
RS
3298/* This is the entry point used by the parser to build unary operators
3299 in the input. CODE, a tree_code, specifies the unary operator, and
3300 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3301 CONVERT_EXPR for code.
3302
3303 LOC is the location to use for the tree generated.
3304*/
43f6dfd3
RS
3305
3306struct c_expr
c2255bc4 3307parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3308{
3309 struct c_expr result;
3310
c9f9eb5d 3311 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3312 result.original_code = code;
6866c6e8
ILT
3313 result.original_type = NULL;
3314
59c0753d 3315 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3316 overflow_warning (loc, result.value);
59c0753d 3317
43f6dfd3
RS
3318 return result;
3319}
3320
3321/* This is the entry point used by the parser to build binary operators
3322 in the input. CODE, a tree_code, specifies the binary operator, and
3323 ARG1 and ARG2 are the operands. In addition to constructing the
3324 expression, we check for operands that were written with other binary
ba47d38d
AH
3325 operators in a way that is likely to confuse the user.
3326
3327 LOCATION is the location of the binary operator. */
edc7c4ec 3328
487a92fe 3329struct c_expr
ba47d38d
AH
3330parser_build_binary_op (location_t location, enum tree_code code,
3331 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3332{
487a92fe 3333 struct c_expr result;
400fbf9f 3334
487a92fe
JM
3335 enum tree_code code1 = arg1.original_code;
3336 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3337 tree type1 = (arg1.original_type
3338 ? arg1.original_type
3339 : TREE_TYPE (arg1.value));
3340 tree type2 = (arg2.original_type
3341 ? arg2.original_type
3342 : TREE_TYPE (arg2.value));
400fbf9f 3343
ba47d38d
AH
3344 result.value = build_binary_op (location, code,
3345 arg1.value, arg2.value, 1);
487a92fe 3346 result.original_code = code;
6866c6e8 3347 result.original_type = NULL;
58bf601b 3348
487a92fe
JM
3349 if (TREE_CODE (result.value) == ERROR_MARK)
3350 return result;
400fbf9f 3351
ba47d38d
AH
3352 if (location != UNKNOWN_LOCATION)
3353 protected_set_expr_location (result.value, location);
3354
400fbf9f 3355 /* Check for cases such as x+y<<z which users are likely
487a92fe 3356 to misinterpret. */
400fbf9f 3357 if (warn_parentheses)
100d537d 3358 warn_about_parentheses (code, code1, arg1.value, code2, arg2.value);
001af587 3359
ca409efd 3360 if (warn_logical_op)
a243fb4a 3361 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
ca409efd 3362 code1, arg1.value, code2, arg2.value);
63a08740 3363
e994a705
RS
3364 /* Warn about comparisons against string literals, with the exception
3365 of testing for equality or inequality of a string literal with NULL. */
3366 if (code == EQ_EXPR || code == NE_EXPR)
3367 {
3368 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3369 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3370 warning_at (location, OPT_Waddress,
3371 "comparison with string literal results in unspecified behavior");
e994a705
RS
3372 }
3373 else if (TREE_CODE_CLASS (code) == tcc_comparison
3374 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3375 warning_at (location, OPT_Waddress,
3376 "comparison with string literal results in unspecified behavior");
e994a705 3377
b8698a0f
L
3378 if (TREE_OVERFLOW_P (result.value)
3379 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3380 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3381 overflow_warning (location, result.value);
400fbf9f 3382
6866c6e8
ILT
3383 /* Warn about comparisons of different enum types. */
3384 if (warn_enum_compare
3385 && TREE_CODE_CLASS (code) == tcc_comparison
3386 && TREE_CODE (type1) == ENUMERAL_TYPE
3387 && TREE_CODE (type2) == ENUMERAL_TYPE
3388 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3389 warning_at (location, OPT_Wenum_compare,
3390 "comparison between %qT and %qT",
3391 type1, type2);
3392
400fbf9f
JW
3393 return result;
3394}
3e4093b6 3395\f
3e4093b6
RS
3396/* Return a tree for the difference of pointers OP0 and OP1.
3397 The resulting tree has type int. */
293c9fdd 3398
3e4093b6 3399static tree
db3927fb 3400pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3401{
3e4093b6 3402 tree restype = ptrdiff_type_node;
36c5e70a 3403 tree result, inttype;
400fbf9f 3404
36c5e70a
BE
3405 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3406 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3407 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3408 tree con0, con1, lit0, lit1;
3409 tree orig_op1 = op1;
400fbf9f 3410
36c5e70a
BE
3411 /* If the operands point into different address spaces, we need to
3412 explicitly convert them to pointers into the common address space
3413 before we can subtract the numerical address values. */
3414 if (as0 != as1)
3415 {
3416 addr_space_t as_common;
3417 tree common_type;
3418
3419 /* Determine the common superset address space. This is guaranteed
3420 to exist because the caller verified that comp_target_types
3421 returned non-zero. */
3422 if (!addr_space_superset (as0, as1, &as_common))
3423 gcc_unreachable ();
3424
3425 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3426 op0 = convert (common_type, op0);
3427 op1 = convert (common_type, op1);
3428 }
3429
3430 /* Determine integer type to perform computations in. This will usually
3431 be the same as the result type (ptrdiff_t), but may need to be a wider
3432 type if pointers for the address space are wider than ptrdiff_t. */
3433 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3434 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3435 else
3436 inttype = restype;
3437
3438
fcf73884 3439 if (TREE_CODE (target_type) == VOID_TYPE)
c1771a20 3440 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
fcf73884
MLI
3441 "pointer of type %<void *%> used in subtraction");
3442 if (TREE_CODE (target_type) == FUNCTION_TYPE)
c1771a20 3443 pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
fcf73884 3444 "pointer to a function used in subtraction");
400fbf9f 3445
3e4093b6
RS
3446 /* If the conversion to ptrdiff_type does anything like widening or
3447 converting a partial to an integral mode, we get a convert_expression
3448 that is in the way to do any simplifications.
3449 (fold-const.c doesn't know that the extra bits won't be needed.
3450 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3451 different mode in place.)
3452 So first try to find a common term here 'by hand'; we want to cover
3453 at least the cases that occur in legal static initializers. */
1043771b 3454 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3455 && (TYPE_PRECISION (TREE_TYPE (op0))
3456 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3457 con0 = TREE_OPERAND (op0, 0);
3458 else
3459 con0 = op0;
1043771b 3460 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3461 && (TYPE_PRECISION (TREE_TYPE (op1))
3462 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3463 con1 = TREE_OPERAND (op1, 0);
3464 else
3465 con1 = op1;
400fbf9f 3466
5de72424 3467 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3468 {
3469 lit0 = TREE_OPERAND (con0, 1);
3470 con0 = TREE_OPERAND (con0, 0);
3471 }
3472 else
3473 lit0 = integer_zero_node;
400fbf9f 3474
5de72424 3475 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3476 {
3e4093b6
RS
3477 lit1 = TREE_OPERAND (con1, 1);
3478 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3479 }
3480 else
3e4093b6
RS
3481 lit1 = integer_zero_node;
3482
3483 if (operand_equal_p (con0, con1, 0))
400fbf9f 3484 {
3e4093b6
RS
3485 op0 = lit0;
3486 op1 = lit1;
400fbf9f
JW
3487 }
3488
400fbf9f 3489
3e4093b6
RS
3490 /* First do the subtraction as integers;
3491 then drop through to build the divide operator.
3492 Do not do default conversions on the minus operator
3493 in case restype is a short type. */
400fbf9f 3494
db3927fb 3495 op0 = build_binary_op (loc,
36c5e70a
BE
3496 MINUS_EXPR, convert (inttype, op0),
3497 convert (inttype, op1), 0);
3e4093b6
RS
3498 /* This generates an error if op1 is pointer to incomplete type. */
3499 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3500 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3501
3e4093b6
RS
3502 /* This generates an error if op0 is pointer to incomplete type. */
3503 op1 = c_size_in_bytes (target_type);
400fbf9f 3504
3e4093b6 3505 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3506 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3507 op0, convert (inttype, op1));
3508
3509 /* Convert to final result type if necessary. */
3510 return convert (restype, result);
3e4093b6
RS
3511}
3512\f
3513/* Construct and perhaps optimize a tree representation
3514 for a unary operation. CODE, a tree_code, specifies the operation
3515 and XARG is the operand.
3516 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3517 the default promotions (such as from short to int).
3518 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3519 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3520 arrays to pointers in C99.
3521
3522 LOCATION is the location of the operator. */
400fbf9f 3523
3e4093b6 3524tree
c9f9eb5d
AH
3525build_unary_op (location_t location,
3526 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3527{
3528 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3529 tree arg = xarg;
3530 tree argtype = 0;
808d6eaa 3531 enum tree_code typecode;
3e4093b6 3532 tree val;
c9f9eb5d 3533 tree ret = error_mark_node;
8ce94e44 3534 tree eptype = NULL_TREE;
3e4093b6 3535 int noconvert = flag;
4de67c26 3536 const char *invalid_op_diag;
928c19bb
JM
3537 bool int_operands;
3538
3539 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3540 if (int_operands)
3541 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3542
808d6eaa
JM
3543 if (code != ADDR_EXPR)
3544 arg = require_complete_type (arg);
3545
3546 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3547 if (typecode == ERROR_MARK)
3548 return error_mark_node;
3549 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3550 typecode = INTEGER_TYPE;
6c36d76b 3551
4de67c26
JM
3552 if ((invalid_op_diag
3553 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3554 {
c9f9eb5d 3555 error_at (location, invalid_op_diag);
4de67c26
JM
3556 return error_mark_node;
3557 }
3558
8ce94e44
JM
3559 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3560 {
3561 eptype = TREE_TYPE (arg);
3562 arg = TREE_OPERAND (arg, 0);
3563 }
3564
3e4093b6
RS
3565 switch (code)
3566 {
3567 case CONVERT_EXPR:
3568 /* This is used for unary plus, because a CONVERT_EXPR
3569 is enough to prevent anybody from looking inside for
3570 associativity, but won't generate any code. */
3571 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3572 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3573 || typecode == VECTOR_TYPE))
400fbf9f 3574 {
c9f9eb5d 3575 error_at (location, "wrong type argument to unary plus");
3e4093b6 3576 return error_mark_node;
400fbf9f 3577 }
3e4093b6
RS
3578 else if (!noconvert)
3579 arg = default_conversion (arg);
db3927fb 3580 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3581 break;
3582
3e4093b6
RS
3583 case NEGATE_EXPR:
3584 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3585 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3586 || typecode == VECTOR_TYPE))
3587 {
c9f9eb5d 3588 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3589 return error_mark_node;
3590 }
3591 else if (!noconvert)
3592 arg = default_conversion (arg);
400fbf9f
JW
3593 break;
3594
3e4093b6 3595 case BIT_NOT_EXPR:
462643f0
AP
3596 /* ~ works on integer types and non float vectors. */
3597 if (typecode == INTEGER_TYPE
3598 || (typecode == VECTOR_TYPE
3599 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3600 {
3e4093b6
RS
3601 if (!noconvert)
3602 arg = default_conversion (arg);
03d5b1f5 3603 }
3e4093b6 3604 else if (typecode == COMPLEX_TYPE)
400fbf9f 3605 {
3e4093b6 3606 code = CONJ_EXPR;
c1771a20 3607 pedwarn (location, OPT_Wpedantic,
fcf73884 3608 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3609 if (!noconvert)
3610 arg = default_conversion (arg);
3611 }
3612 else
3613 {
c9f9eb5d 3614 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3615 return error_mark_node;
400fbf9f
JW
3616 }
3617 break;
3618
3e4093b6 3619 case ABS_EXPR:
11017cc7 3620 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3621 {
c9f9eb5d 3622 error_at (location, "wrong type argument to abs");
3e4093b6 3623 return error_mark_node;
400fbf9f 3624 }
3e4093b6
RS
3625 else if (!noconvert)
3626 arg = default_conversion (arg);
400fbf9f
JW
3627 break;
3628
3e4093b6
RS
3629 case CONJ_EXPR:
3630 /* Conjugating a real value is a no-op, but allow it anyway. */
3631 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3632 || typecode == COMPLEX_TYPE))
400fbf9f 3633 {
c9f9eb5d 3634 error_at (location, "wrong type argument to conjugation");
3e4093b6 3635 return error_mark_node;
400fbf9f 3636 }
3e4093b6
RS
3637 else if (!noconvert)
3638 arg = default_conversion (arg);
400fbf9f
JW
3639 break;
3640
3e4093b6 3641 case TRUTH_NOT_EXPR:
ab22c1fa 3642 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3643 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3644 && typecode != COMPLEX_TYPE)
400fbf9f 3645 {
c9f9eb5d
AH
3646 error_at (location,
3647 "wrong type argument to unary exclamation mark");
3e4093b6 3648 return error_mark_node;
400fbf9f 3649 }
c9f9eb5d 3650 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3651 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3652 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3653 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3654 location = EXPR_LOCATION (ret);
c9f9eb5d 3655 goto return_build_unary_op;
3e4093b6 3656
3e4093b6 3657 case REALPART_EXPR:
3e4093b6 3658 case IMAGPART_EXPR:
fb52b50a
NF
3659 ret = build_real_imag_expr (location, code, arg);
3660 if (ret == error_mark_node)
3661 return error_mark_node;
8ce94e44
JM
3662 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3663 eptype = TREE_TYPE (eptype);
c9f9eb5d 3664 goto return_build_unary_op;
3e4093b6
RS
3665
3666 case PREINCREMENT_EXPR:
3667 case POSTINCREMENT_EXPR:
3668 case PREDECREMENT_EXPR:
3669 case POSTDECREMENT_EXPR:
3e4093b6 3670
928c19bb
JM
3671 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3672 {
3673 tree inner = build_unary_op (location, code,
3674 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3675 if (inner == error_mark_node)
3676 return error_mark_node;
3677 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3678 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3679 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3680 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3681 goto return_build_unary_op;
3682 }
3683
925e8657
NP
3684 /* Complain about anything that is not a true lvalue. In
3685 Objective-C, skip this check for property_refs. */
f90e8e2e 3686 if (!objc_is_property_ref (arg)
7bd11157
TT
3687 && !lvalue_or_else (location,
3688 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3689 || code == POSTINCREMENT_EXPR)
3690 ? lv_increment
3691 : lv_decrement)))
928c19bb
JM
3692 return error_mark_node;
3693
09639a83
ILT
3694 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3695 {
3696 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3697 warning_at (location, OPT_Wc___compat,
3698 "increment of enumeration value is invalid in C++");
3699 else
3700 warning_at (location, OPT_Wc___compat,
3701 "decrement of enumeration value is invalid in C++");
3702 }
3703
928c19bb
JM
3704 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3705 arg = c_fully_fold (arg, false, NULL);
3706
3e4093b6
RS
3707 /* Increment or decrement the real part of the value,
3708 and don't change the imaginary part. */
3709 if (typecode == COMPLEX_TYPE)
400fbf9f 3710 {
3e4093b6
RS
3711 tree real, imag;
3712
c1771a20 3713 pedwarn (location, OPT_Wpedantic,
509c9d60 3714 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6
RS
3715
3716 arg = stabilize_reference (arg);
c9f9eb5d
AH
3717 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3718 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3719 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
a87db577
AP
3720 if (real == error_mark_node || imag == error_mark_node)
3721 return error_mark_node;
c9f9eb5d
AH
3722 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3723 real, imag);
3724 goto return_build_unary_op;
400fbf9f 3725 }
3e4093b6
RS
3726
3727 /* Report invalid types. */
3728
ab22c1fa 3729 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3730 && typecode != INTEGER_TYPE && typecode != REAL_TYPE)
400fbf9f 3731 {
3e4093b6 3732 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3733 error_at (location, "wrong type argument to increment");
c22cacf3 3734 else
c9f9eb5d 3735 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3736
3737 return error_mark_node;
400fbf9f 3738 }
400fbf9f 3739
3e4093b6
RS
3740 {
3741 tree inc;
400fbf9f 3742
3e4093b6
RS
3743 argtype = TREE_TYPE (arg);
3744
3745 /* Compute the increment. */
3746
3747 if (typecode == POINTER_TYPE)
3748 {
3749 /* If pointer target is an undefined struct,
3750 we just cannot know how to do the arithmetic. */
b70cef5d 3751 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
3752 {
3753 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d
AH
3754 error_at (location,
3755 "increment of pointer to unknown structure");
3e4093b6 3756 else
c9f9eb5d
AH
3757 error_at (location,
3758 "decrement of pointer to unknown structure");
3e4093b6 3759 }
b70cef5d
JJ
3760 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3761 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 3762 {
3e4093b6 3763 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c1771a20 3764 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
fcf73884 3765 "wrong type argument to increment");
3e4093b6 3766 else
c1771a20 3767 pedwarn (location, pedantic ? OPT_Wpedantic : OPT_Wpointer_arith,
fcf73884 3768 "wrong type argument to decrement");
3e4093b6
RS
3769 }
3770
b70cef5d 3771 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 3772 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 3773 }
b70cef5d 3774 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
3775 {
3776 /* For signed fract types, we invert ++ to -- or
3777 -- to ++, and change inc from 1 to -1, because
3778 it is not possible to represent 1 in signed fract constants.
3779 For unsigned fract types, the result always overflows and
3780 we get an undefined (original) or the maximum value. */
3781 if (code == PREINCREMENT_EXPR)
3782 code = PREDECREMENT_EXPR;
3783 else if (code == PREDECREMENT_EXPR)
3784 code = PREINCREMENT_EXPR;
3785 else if (code == POSTINCREMENT_EXPR)
3786 code = POSTDECREMENT_EXPR;
3787 else /* code == POSTDECREMENT_EXPR */
3788 code = POSTINCREMENT_EXPR;
3789
3790 inc = integer_minus_one_node;
3791 inc = convert (argtype, inc);
3792 }
3e4093b6 3793 else
5be014d5
AP
3794 {
3795 inc = integer_one_node;
3796 inc = convert (argtype, inc);
3797 }
3e4093b6 3798
925e8657
NP
3799 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
3800 need to ask Objective-C to build the increment or decrement
3801 expression for it. */
3802 if (objc_is_property_ref (arg))
f90e8e2e 3803 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
3804 arg, inc);
3805
3e4093b6 3806 /* Report a read-only lvalue. */
f37acdf9 3807 if (TYPE_READONLY (argtype))
953ff289
DN
3808 {
3809 readonly_error (arg,
3810 ((code == PREINCREMENT_EXPR
3811 || code == POSTINCREMENT_EXPR)
3812 ? lv_increment : lv_decrement));
3813 return error_mark_node;
3814 }
f37acdf9
JM
3815 else if (TREE_READONLY (arg))
3816 readonly_warning (arg,
3817 ((code == PREINCREMENT_EXPR
3818 || code == POSTINCREMENT_EXPR)
3819 ? lv_increment : lv_decrement));
3e4093b6
RS
3820
3821 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3822 val = boolean_increment (code, arg);
3823 else
53fb4de3 3824 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 3825 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 3826 if (TREE_CODE (val) != code)
6de9cd9a 3827 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
3828 ret = val;
3829 goto return_build_unary_op;
3e4093b6
RS
3830 }
3831
3832 case ADDR_EXPR:
3833 /* Note that this operation never does default_conversion. */
3834
2b4b7036
JM
3835 /* The operand of unary '&' must be an lvalue (which excludes
3836 expressions of type void), or, in C99, the result of a [] or
3837 unary '*' operator. */
3838 if (VOID_TYPE_P (TREE_TYPE (arg))
3839 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
3840 && (TREE_CODE (arg) != INDIRECT_REF
3841 || !flag_isoc99))
3842 pedwarn (location, 0, "taking address of expression of type %<void%>");
3843
3e4093b6
RS
3844 /* Let &* cancel out to simplify resulting code. */
3845 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 3846 {
3e4093b6
RS
3847 /* Don't let this be an lvalue. */
3848 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 3849 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
3850 ret = TREE_OPERAND (arg, 0);
3851 goto return_build_unary_op;
400fbf9f 3852 }
1eb8759b 3853
7c672dfc 3854 /* For &x[y], return x+y */
3e4093b6 3855 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 3856 {
f2a71bbc
JM
3857 tree op0 = TREE_OPERAND (arg, 0);
3858 if (!c_mark_addressable (op0))
3e4093b6 3859 return error_mark_node;
1eb8759b 3860 }
1eb8759b 3861
3e4093b6
RS
3862 /* Anything not already handled and not a true memory reference
3863 or a non-lvalue array is an error. */
3864 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 3865 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 3866 return error_mark_node;
b6a10c9f 3867
928c19bb
JM
3868 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
3869 folding later. */
3870 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3871 {
3872 tree inner = build_unary_op (location, code,
3873 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3874 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3875 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3876 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3877 C_MAYBE_CONST_EXPR_NON_CONST (ret)
3878 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
3879 goto return_build_unary_op;
3880 }
3881
3e4093b6
RS
3882 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
3883 argtype = TREE_TYPE (arg);
400fbf9f 3884
3e4093b6 3885 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 3886 to which the address will point. This is only needed
f2c1da78 3887 for function types. */
6615c446 3888 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
3889 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
3890 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
3891 {
3892 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
3893 int quals = orig_quals;
3894
3895 if (TREE_READONLY (arg))
3896 quals |= TYPE_QUAL_CONST;
3897 if (TREE_THIS_VOLATILE (arg))
3898 quals |= TYPE_QUAL_VOLATILE;
3899
f2c1da78
JM
3900 argtype = c_build_qualified_type (argtype, quals);
3901 }
400fbf9f 3902
3e4093b6
RS
3903 if (!c_mark_addressable (arg))
3904 return error_mark_node;
400fbf9f 3905
abb54d14
JM
3906 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
3907 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 3908
5cc200fc 3909 argtype = build_pointer_type (argtype);
5e55f99d
RH
3910
3911 /* ??? Cope with user tricks that amount to offsetof. Delete this
3912 when we have proper support for integer constant expressions. */
3913 val = get_base_address (arg);
3914 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
3915 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
3916 {
cf9e9959 3917 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 3918 goto return_build_unary_op;
3aa2ddb8 3919 }
5e55f99d 3920
5cc200fc 3921 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 3922
c9f9eb5d
AH
3923 ret = val;
3924 goto return_build_unary_op;
400fbf9f 3925
3e4093b6 3926 default:
1344f9a3 3927 gcc_unreachable ();
3e4093b6 3928 }
400fbf9f 3929
3e4093b6
RS
3930 if (argtype == 0)
3931 argtype = TREE_TYPE (arg);
928c19bb
JM
3932 if (TREE_CODE (arg) == INTEGER_CST)
3933 ret = (require_constant_value
db3927fb
AH
3934 ? fold_build1_initializer_loc (location, code, argtype, arg)
3935 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
3936 else
3937 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
3938 return_build_unary_op:
3939 gcc_assert (ret != error_mark_node);
928c19bb
JM
3940 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
3941 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
3942 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
3943 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
3944 ret = note_integer_operands (ret);
8ce94e44
JM
3945 if (eptype)
3946 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
3947 protected_set_expr_location (ret, location);
3948 return ret;
3e4093b6 3949}
400fbf9f 3950
3e4093b6
RS
3951/* Return nonzero if REF is an lvalue valid for this language.
3952 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 3953 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 3954
1e4ae551 3955bool
58f9752a 3956lvalue_p (const_tree ref)
3e4093b6 3957{
58f9752a 3958 const enum tree_code code = TREE_CODE (ref);
400fbf9f 3959
3e4093b6
RS
3960 switch (code)
3961 {
3962 case REALPART_EXPR:
3963 case IMAGPART_EXPR:
3964 case COMPONENT_REF:
3965 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 3966
928c19bb
JM
3967 case C_MAYBE_CONST_EXPR:
3968 return lvalue_p (TREE_OPERAND (ref, 1));
3969
3e4093b6
RS
3970 case COMPOUND_LITERAL_EXPR:
3971 case STRING_CST:
3972 return 1;
400fbf9f 3973
3e4093b6
RS
3974 case INDIRECT_REF:
3975 case ARRAY_REF:
3976 case VAR_DECL:
3977 case PARM_DECL:
3978 case RESULT_DECL:
3979 case ERROR_MARK:
3980 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
3981 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 3982
3e4093b6 3983 case BIND_EXPR:
3e4093b6 3984 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 3985
3e4093b6
RS
3986 default:
3987 return 0;
3988 }
3989}
400fbf9f 3990\f
f37acdf9
JM
3991/* Give a warning for storing in something that is read-only in GCC
3992 terms but not const in ISO C terms. */
3993
3994static void
3995readonly_warning (tree arg, enum lvalue_use use)
3996{
3997 switch (use)
3998 {
3999 case lv_assign:
4000 warning (0, "assignment of read-only location %qE", arg);
4001 break;
4002 case lv_increment:
4003 warning (0, "increment of read-only location %qE", arg);
4004 break;
4005 case lv_decrement:
4006 warning (0, "decrement of read-only location %qE", arg);
4007 break;
4008 default:
4009 gcc_unreachable ();
4010 }
4011 return;
4012}
4013
37dc0d8d
JM
4014
4015/* Return nonzero if REF is an lvalue valid for this language;
4016 otherwise, print an error message and return zero. USE says
7bd11157
TT
4017 how the lvalue is being used and so selects the error message.
4018 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4019
4020static int
7bd11157 4021lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4022{
4023 int win = lvalue_p (ref);
4024
4025 if (!win)
7bd11157 4026 lvalue_error (loc, use);
37dc0d8d
JM
4027
4028 return win;
4029}
3e4093b6
RS
4030\f
4031/* Mark EXP saying that we need to be able to take the
4032 address of it; it should not be allocated in a register.
4033 Returns true if successful. */
54c93c30 4034
3e4093b6
RS
4035bool
4036c_mark_addressable (tree exp)
400fbf9f 4037{
3e4093b6 4038 tree x = exp;
95602da1 4039
3e4093b6
RS
4040 while (1)
4041 switch (TREE_CODE (x))
4042 {
4043 case COMPONENT_REF:
4044 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4045 {
0039fa55
AN
4046 error
4047 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4048 return false;
4049 }
95602da1 4050
3e4093b6 4051 /* ... fall through ... */
95602da1 4052
3e4093b6
RS
4053 case ADDR_EXPR:
4054 case ARRAY_REF:
4055 case REALPART_EXPR:
4056 case IMAGPART_EXPR:
4057 x = TREE_OPERAND (x, 0);
4058 break;
95602da1 4059
3e4093b6
RS
4060 case COMPOUND_LITERAL_EXPR:
4061 case CONSTRUCTOR:
4062 TREE_ADDRESSABLE (x) = 1;
4063 return true;
95602da1 4064
3e4093b6
RS
4065 case VAR_DECL:
4066 case CONST_DECL:
4067 case PARM_DECL:
4068 case RESULT_DECL:
5baeaac0 4069 if (C_DECL_REGISTER (x)
3e4093b6
RS
4070 && DECL_NONLOCAL (x))
4071 {
e697b20f 4072 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4073 {
0039fa55
AN
4074 error
4075 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4076 return false;
4077 }
509c9d60 4078 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4079 }
5baeaac0 4080 else if (C_DECL_REGISTER (x))
3e4093b6 4081 {
e697b20f 4082 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4083 error ("address of global register variable %qD requested", x);
4084 else
4085 error ("address of register variable %qD requested", x);
4086 return false;
3e4093b6 4087 }
400fbf9f 4088
3e4093b6
RS
4089 /* drops in */
4090 case FUNCTION_DECL:
4091 TREE_ADDRESSABLE (x) = 1;
4092 /* drops out */
4093 default:
4094 return true;
4095 }
4096}
4097\f
2d2e923f
MLI
4098/* Convert EXPR to TYPE, warning about conversion problems with
4099 constants. SEMANTIC_TYPE is the type this conversion would use
4100 without excess precision. If SEMANTIC_TYPE is NULL, this function
4101 is equivalent to convert_and_check. This function is a wrapper that
4102 handles conversions that may be different than
4103 the usual ones because of excess precision. */
4104
4105static tree
4106ep_convert_and_check (tree type, tree expr, tree semantic_type)
4107{
4108 if (TREE_TYPE (expr) == type)
4109 return expr;
4110
4111 if (!semantic_type)
4112 return convert_and_check (type, expr);
4113
4114 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4115 && TREE_TYPE (expr) != semantic_type)
4116 {
4117 /* For integers, we need to check the real conversion, not
4118 the conversion to the excess precision type. */
4119 expr = convert_and_check (semantic_type, expr);
4120 }
4121 /* Result type is the excess precision type, which should be
4122 large enough, so do not check. */
4123 return convert (type, expr);
4124}
4125
928c19bb
JM
4126/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4127 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4128 if folded to an integer constant then the unselected half may
4129 contain arbitrary operations not normally permitted in constant
c2255bc4 4130 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4131
4132tree
744aa42f 4133build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4134 tree op1, tree op1_original_type, tree op2,
4135 tree op2_original_type)
400fbf9f 4136{
3e4093b6
RS
4137 tree type1;
4138 tree type2;
4139 enum tree_code code1;
4140 enum tree_code code2;
4141 tree result_type = NULL;
2d2e923f 4142 tree semantic_result_type = NULL;
3e4093b6 4143 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4144 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4145 bool ifexp_int_operands;
928c19bb 4146 tree ret;
400fbf9f 4147
4d84fe7c
JM
4148 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4149 if (op1_int_operands)
4150 op1 = remove_c_maybe_const_expr (op1);
4151 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4152 if (op2_int_operands)
4153 op2 = remove_c_maybe_const_expr (op2);
4154 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4155 if (ifexp_int_operands)
4156 ifexp = remove_c_maybe_const_expr (ifexp);
4157
3e4093b6
RS
4158 /* Promote both alternatives. */
4159
4160 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4161 op1 = default_conversion (op1);
4162 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4163 op2 = default_conversion (op2);
4164
4165 if (TREE_CODE (ifexp) == ERROR_MARK
4166 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4167 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4168 return error_mark_node;
400fbf9f 4169
3e4093b6
RS
4170 type1 = TREE_TYPE (op1);
4171 code1 = TREE_CODE (type1);
4172 type2 = TREE_TYPE (op2);
4173 code2 = TREE_CODE (type2);
4174
b1adf557
JM
4175 /* C90 does not permit non-lvalue arrays in conditional expressions.
4176 In C99 they will be pointers by now. */
4177 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4178 {
744aa42f 4179 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4180 return error_mark_node;
4181 }
4182
8ce94e44
JM
4183 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4184 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4185 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4186 || code1 == COMPLEX_TYPE)
4187 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4188 || code2 == COMPLEX_TYPE))
4189 {
2d2e923f 4190 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4191 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4192 {
4193 op1 = TREE_OPERAND (op1, 0);
4194 type1 = TREE_TYPE (op1);
4195 gcc_assert (TREE_CODE (type1) == code1);
4196 }
4197 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4198 {
4199 op2 = TREE_OPERAND (op2, 0);
4200 type2 = TREE_TYPE (op2);
4201 gcc_assert (TREE_CODE (type2) == code2);
4202 }
4203 }
4204
d130ae11
ILT
4205 if (warn_cxx_compat)
4206 {
4207 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4208 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4209
4210 if (TREE_CODE (t1) == ENUMERAL_TYPE
4211 && TREE_CODE (t2) == ENUMERAL_TYPE
4212 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4213 warning_at (colon_loc, OPT_Wc___compat,
4214 ("different enum types in conditional is "
4215 "invalid in C++: %qT vs %qT"),
4216 t1, t2);
4217 }
4218
3e4093b6
RS
4219 /* Quickly detect the usual case where op1 and op2 have the same type
4220 after promotion. */
4221 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4222 {
3e4093b6
RS
4223 if (type1 == type2)
4224 result_type = type1;
4225 else
4226 result_type = TYPE_MAIN_VARIANT (type1);
4227 }
4228 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4229 || code1 == COMPLEX_TYPE)
4230 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4231 || code2 == COMPLEX_TYPE))
3e4093b6 4232 {
ccf7f880 4233 result_type = c_common_type (type1, type2);
0a0b3574
MM
4234 do_warn_double_promotion (result_type, type1, type2,
4235 "implicit conversion from %qT to %qT to "
4236 "match other result of conditional",
4237 colon_loc);
400fbf9f 4238
3e4093b6
RS
4239 /* If -Wsign-compare, warn here if type1 and type2 have
4240 different signedness. We'll promote the signed to unsigned
4241 and later code won't know it used to be different.
4242 Do this check on the original types, so that explicit casts
4243 will be considered, but default promotions won't. */
7d882b83 4244 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4245 {
8df83eae
RK
4246 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4247 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4248
3e4093b6
RS
4249 if (unsigned_op1 ^ unsigned_op2)
4250 {
6ac01510
ILT
4251 bool ovf;
4252
3e4093b6
RS
4253 /* Do not warn if the result type is signed, since the
4254 signed type will only be chosen if it can represent
4255 all the values of the unsigned type. */
3f75a254 4256 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4257 /* OK */;
3e4093b6 4258 else
928c19bb
JM
4259 {
4260 bool op1_maybe_const = true;
4261 bool op2_maybe_const = true;
4262
4263 /* Do not warn if the signed quantity is an
4264 unsuffixed integer literal (or some static
4265 constant expression involving such literals) and
4266 it is non-negative. This warning requires the
4267 operands to be folded for best results, so do
4268 that folding in this case even without
4269 warn_sign_compare to avoid warning options
4270 possibly affecting code generation. */
f5178456
RS
4271 c_inhibit_evaluation_warnings
4272 += (ifexp == truthvalue_false_node);
928c19bb
JM
4273 op1 = c_fully_fold (op1, require_constant_value,
4274 &op1_maybe_const);
f5178456
RS
4275 c_inhibit_evaluation_warnings
4276 -= (ifexp == truthvalue_false_node);
4277
4278 c_inhibit_evaluation_warnings
4279 += (ifexp == truthvalue_true_node);
928c19bb
JM
4280 op2 = c_fully_fold (op2, require_constant_value,
4281 &op2_maybe_const);
f5178456
RS
4282 c_inhibit_evaluation_warnings
4283 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4284
4285 if (warn_sign_compare)
4286 {
4287 if ((unsigned_op2
4288 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4289 || (unsigned_op1
4290 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4291 /* OK */;
4292 else
744aa42f
ILT
4293 warning_at (colon_loc, OPT_Wsign_compare,
4294 ("signed and unsigned type in "
4295 "conditional expression"));
928c19bb
JM
4296 }
4297 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4298 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4299 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4300 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4301 }
3e4093b6
RS
4302 }
4303 }
4304 }
4305 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4306 {
fcf73884 4307 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4308 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4309 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4310 result_type = void_type_node;
4311 }
4312 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4313 {
36c5e70a
BE
4314 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4315 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4316 addr_space_t as_common;
4317
744aa42f 4318 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4319 result_type = common_pointer_type (type1, type2);
6aa3c60d 4320 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4321 result_type = type2;
6aa3c60d 4322 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4323 result_type = type1;
4324 else if (!addr_space_superset (as1, as2, &as_common))
4325 {
4326 error_at (colon_loc, "pointers to disjoint address spaces "
4327 "used in conditional expression");
4328 return error_mark_node;
4329 }
3e4093b6 4330 else if (VOID_TYPE_P (TREE_TYPE (type1)))
34a80643 4331 {
fcf73884 4332 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4333 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4334 "ISO C forbids conditional expr between "
bda67431 4335 "%<void *%> and function pointer");
3e4093b6
RS
4336 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4337 TREE_TYPE (type2)));
34a80643 4338 }
3e4093b6 4339 else if (VOID_TYPE_P (TREE_TYPE (type2)))
1c2a9b35 4340 {
fcf73884 4341 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4342 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4343 "ISO C forbids conditional expr between "
bda67431 4344 "%<void *%> and function pointer");
3e4093b6
RS
4345 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4346 TREE_TYPE (type1)));
1c2a9b35 4347 }
b581b85b
NP
4348 /* Objective-C pointer comparisons are a bit more lenient. */
4349 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4350 result_type = objc_common_type (type1, type2);
34a80643 4351 else
ab87f8c8 4352 {
36c5e70a
BE
4353 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4354
b581b85b
NP
4355 pedwarn (colon_loc, 0,
4356 "pointer type mismatch in conditional expression");
36c5e70a
BE
4357 result_type = build_pointer_type
4358 (build_qualified_type (void_type_node, qual));
ab87f8c8 4359 }
3e4093b6
RS
4360 }
4361 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4362 {
6aa3c60d 4363 if (!null_pointer_constant_p (orig_op2))
744aa42f 4364 pedwarn (colon_loc, 0,
509c9d60 4365 "pointer/integer type mismatch in conditional expression");
3e4093b6 4366 else
ab87f8c8 4367 {
3e4093b6 4368 op2 = null_pointer_node;
ab87f8c8 4369 }
3e4093b6
RS
4370 result_type = type1;
4371 }
4372 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4373 {
6aa3c60d 4374 if (!null_pointer_constant_p (orig_op1))
744aa42f 4375 pedwarn (colon_loc, 0,
509c9d60 4376 "pointer/integer type mismatch in conditional expression");
3e4093b6 4377 else
ab87f8c8 4378 {
3e4093b6 4379 op1 = null_pointer_node;
ab87f8c8 4380 }
3e4093b6
RS
4381 result_type = type2;
4382 }
1c2a9b35 4383
3e4093b6
RS
4384 if (!result_type)
4385 {
4386 if (flag_cond_mismatch)
4387 result_type = void_type_node;
4388 else
400fbf9f 4389 {
c2255bc4 4390 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4391 return error_mark_node;
400fbf9f 4392 }
3e4093b6 4393 }
400fbf9f 4394
3e4093b6
RS
4395 /* Merge const and volatile flags of the incoming types. */
4396 result_type
4397 = build_type_variant (result_type,
afbd0665
AS
4398 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4399 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4400
2d2e923f
MLI
4401 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4402 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
b6a10c9f 4403
928c19bb
JM
4404 if (ifexp_bcp && ifexp == truthvalue_true_node)
4405 {
4406 op2_int_operands = true;
4407 op1 = c_fully_fold (op1, require_constant_value, NULL);
4408 }
4409 if (ifexp_bcp && ifexp == truthvalue_false_node)
4410 {
4411 op1_int_operands = true;
4412 op2 = c_fully_fold (op2, require_constant_value, NULL);
4413 }
4d84fe7c 4414 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4415 && op1_int_operands
4416 && op2_int_operands);
4417 if (int_operands)
4418 {
4419 int_const = ((ifexp == truthvalue_true_node
4420 && TREE_CODE (orig_op1) == INTEGER_CST
4421 && !TREE_OVERFLOW (orig_op1))
4422 || (ifexp == truthvalue_false_node
4423 && TREE_CODE (orig_op2) == INTEGER_CST
4424 && !TREE_OVERFLOW (orig_op2)));
4425 }
4426 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4427 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4428 else
4429 {
4430 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4431 if (int_operands)
4432 ret = note_integer_operands (ret);
4433 }
2d2e923f
MLI
4434 if (semantic_result_type)
4435 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4436
c2255bc4 4437 protected_set_expr_location (ret, colon_loc);
928c19bb 4438 return ret;
3e4093b6
RS
4439}
4440\f
487a92fe 4441/* Return a compound expression that performs two expressions and
c2255bc4
AH
4442 returns the value of the second of them.
4443
4444 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4445
3e4093b6 4446tree
c2255bc4 4447build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4448{
4d84fe7c 4449 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4450 tree eptype = NULL_TREE;
928c19bb
JM
4451 tree ret;
4452
4d84fe7c
JM
4453 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4454 if (expr1_int_operands)
4455 expr1 = remove_c_maybe_const_expr (expr1);
4456 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4457 if (expr2_int_operands)
4458 expr2 = remove_c_maybe_const_expr (expr2);
4459
8ce94e44
JM
4460 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4461 expr1 = TREE_OPERAND (expr1, 0);
4462 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4463 {
4464 eptype = TREE_TYPE (expr2);
4465 expr2 = TREE_OPERAND (expr2, 0);
4466 }
4467
3f75a254 4468 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4469 {
4470 /* The left-hand operand of a comma expression is like an expression
c5409249 4471 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4472 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4473 if (warn_unused_value)
47aecf47 4474 {
e14a6540 4475 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4476 && CONVERT_EXPR_P (expr1))
47aecf47 4477 ; /* (void) a, b */
e14a6540
JM
4478 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4479 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4480 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4481 ; /* (void) a, (void) b, c */
4482 else
b8698a0f 4483 warning_at (loc, OPT_Wunused_value,
c2255bc4 4484 "left-hand operand of comma expression has no effect");
47aecf47 4485 }
3e4093b6 4486 }
400fbf9f 4487
3e4093b6
RS
4488 /* With -Wunused, we should also warn if the left-hand operand does have
4489 side-effects, but computes a value which is not used. For example, in
4490 `foo() + bar(), baz()' the result of the `+' operator is not used,
4491 so we should issue a warning. */
4492 else if (warn_unused_value)
c2255bc4 4493 warn_if_unused_value (expr1, loc);
400fbf9f 4494
e63d6886
AP
4495 if (expr2 == error_mark_node)
4496 return error_mark_node;
4497
928c19bb
JM
4498 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4499
4500 if (flag_isoc99
4d84fe7c
JM
4501 && expr1_int_operands
4502 && expr2_int_operands)
928c19bb
JM
4503 ret = note_integer_operands (ret);
4504
8ce94e44
JM
4505 if (eptype)
4506 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4507
c2255bc4 4508 protected_set_expr_location (ret, loc);
928c19bb 4509 return ret;
3e4093b6 4510}
400fbf9f 4511
67165eb3
ILT
4512/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4513 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4514 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4515 of the cast. -Wcast-qual appeared on the command line. Named
4516 address space qualifiers are not handled here, because they result
4517 in different warnings. */
67165eb3
ILT
4518
4519static void
2ee3cb35 4520handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4521{
4522 tree in_type = type;
4523 tree in_otype = otype;
4524 int added = 0;
4525 int discarded = 0;
4526 bool is_const;
4527
4528 /* Check that the qualifiers on IN_TYPE are a superset of the
4529 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4530 nodes is uninteresting and we stop as soon as we hit a
4531 non-POINTER_TYPE node on either type. */
4532 do
4533 {
4534 in_otype = TREE_TYPE (in_otype);
4535 in_type = TREE_TYPE (in_type);
4536
4537 /* GNU C allows cv-qualified function types. 'const' means the
4538 function is very pure, 'volatile' means it can't return. We
4539 need to warn when such qualifiers are added, not when they're
4540 taken away. */
4541 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4542 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4543 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4544 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4545 else
36c5e70a
BE
4546 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4547 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4548 }
4549 while (TREE_CODE (in_type) == POINTER_TYPE
4550 && TREE_CODE (in_otype) == POINTER_TYPE);
4551
4552 if (added)
2ee3cb35
MLI
4553 warning_at (loc, OPT_Wcast_qual,
4554 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4555
4556 if (discarded)
4557 /* There are qualifiers present in IN_OTYPE that are not present
4558 in IN_TYPE. */
2ee3cb35
MLI
4559 warning_at (loc, OPT_Wcast_qual,
4560 "cast discards %q#v qualifier from pointer target type",
4561 discarded);
67165eb3
ILT
4562
4563 if (added || discarded)
4564 return;
4565
4566 /* A cast from **T to const **T is unsafe, because it can cause a
4567 const value to be changed with no additional warning. We only
4568 issue this warning if T is the same on both sides, and we only
4569 issue the warning if there are the same number of pointers on
4570 both sides, as otherwise the cast is clearly unsafe anyhow. A
4571 cast is unsafe when a qualifier is added at one level and const
4572 is not present at all outer levels.
4573
4574 To issue this warning, we check at each level whether the cast
4575 adds new qualifiers not already seen. We don't need to special
4576 case function types, as they won't have the same
4577 TYPE_MAIN_VARIANT. */
4578
4579 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4580 return;
4581 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4582 return;
4583
4584 in_type = type;
4585 in_otype = otype;
4586 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4587 do
4588 {
4589 in_type = TREE_TYPE (in_type);
4590 in_otype = TREE_TYPE (in_otype);
4591 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4592 && !is_const)
4593 {
2ee3cb35
MLI
4594 warning_at (loc, OPT_Wcast_qual,
4595 "to be safe all intermediate pointers in cast from "
4596 "%qT to %qT must be %<const%> qualified",
4597 otype, type);
67165eb3
ILT
4598 break;
4599 }
4600 if (is_const)
4601 is_const = TYPE_READONLY (in_type);
4602 }
4603 while (TREE_CODE (in_type) == POINTER_TYPE);
4604}
4605
b8698a0f 4606/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4607 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4608
3e4093b6 4609tree
c2255bc4 4610build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4611{
8ce94e44
JM
4612 tree value;
4613
4614 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4615 expr = TREE_OPERAND (expr, 0);
4616
4617 value = expr;
400fbf9f 4618
3e4093b6
RS
4619 if (type == error_mark_node || expr == error_mark_node)
4620 return error_mark_node;
400fbf9f 4621
3e4093b6
RS
4622 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4623 only in <protocol> qualifications. But when constructing cast expressions,
4624 the protocols do matter and must be kept around. */
700686fa
ZL
4625 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4626 return build1 (NOP_EXPR, type, expr);
4627
4628 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4629
3e4093b6
RS
4630 if (TREE_CODE (type) == ARRAY_TYPE)
4631 {
c2255bc4 4632 error_at (loc, "cast specifies array type");
3e4093b6
RS
4633 return error_mark_node;
4634 }
400fbf9f 4635
3e4093b6
RS
4636 if (TREE_CODE (type) == FUNCTION_TYPE)
4637 {
c2255bc4 4638 error_at (loc, "cast specifies function type");
3e4093b6
RS
4639 return error_mark_node;
4640 }
400fbf9f 4641
808d6eaa
JM
4642 if (!VOID_TYPE_P (type))
4643 {
4644 value = require_complete_type (value);
4645 if (value == error_mark_node)
4646 return error_mark_node;
4647 }
4648
3e4093b6
RS
4649 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4650 {
fcf73884
MLI
4651 if (TREE_CODE (type) == RECORD_TYPE
4652 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4653 pedwarn (loc, OPT_Wpedantic,
fcf73884 4654 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4655 }
4656 else if (TREE_CODE (type) == UNION_TYPE)
4657 {
4658 tree field;
400fbf9f 4659
910ad8de 4660 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4661 if (TREE_TYPE (field) != error_mark_node
4662 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4663 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4664 break;
4665
4666 if (field)
400fbf9f 4667 {
3e4093b6 4668 tree t;
e616f54d 4669 bool maybe_const = true;
3e4093b6 4670
c1771a20 4671 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4672 t = c_fully_fold (value, false, &maybe_const);
4673 t = build_constructor_single (type, field, t);
4674 if (!maybe_const)
4675 t = c_wrap_maybe_const (t, true);
4676 t = digest_init (loc, type, t,
bbbbb16a 4677 NULL_TREE, false, true, 0);
3e4093b6
RS
4678 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4679 return t;
400fbf9f 4680 }
c2255bc4 4681 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4682 return error_mark_node;
4683 }
4684 else
4685 {
4686 tree otype, ovalue;
400fbf9f 4687
3e4093b6 4688 if (type == void_type_node)
c2255bc4
AH
4689 {
4690 tree t = build1 (CONVERT_EXPR, type, value);
4691 SET_EXPR_LOCATION (t, loc);
4692 return t;
4693 }
400fbf9f 4694
3e4093b6 4695 otype = TREE_TYPE (value);
400fbf9f 4696
3e4093b6 4697 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
4698 if (warn_cast_qual
4699 && TREE_CODE (type) == POINTER_TYPE
4700 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 4701 handle_warn_cast_qual (loc, type, otype);
400fbf9f 4702
36c5e70a
BE
4703 /* Warn about conversions between pointers to disjoint
4704 address spaces. */
4705 if (TREE_CODE (type) == POINTER_TYPE
4706 && TREE_CODE (otype) == POINTER_TYPE
4707 && !null_pointer_constant_p (value))
4708 {
4709 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
4710 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
4711 addr_space_t as_common;
4712
4713 if (!addr_space_superset (as_to, as_from, &as_common))
4714 {
4715 if (ADDR_SPACE_GENERIC_P (as_from))
4716 warning_at (loc, 0, "cast to %s address space pointer "
4717 "from disjoint generic address space pointer",
4718 c_addr_space_name (as_to));
4719
4720 else if (ADDR_SPACE_GENERIC_P (as_to))
4721 warning_at (loc, 0, "cast to generic address space pointer "
4722 "from disjoint %s address space pointer",
4723 c_addr_space_name (as_from));
4724
4725 else
4726 warning_at (loc, 0, "cast to %s address space pointer "
4727 "from disjoint %s address space pointer",
4728 c_addr_space_name (as_to),
4729 c_addr_space_name (as_from));
4730 }
4731 }
4732
3e4093b6 4733 /* Warn about possible alignment problems. */
3176a0c2 4734 if (STRICT_ALIGNMENT
3e4093b6
RS
4735 && TREE_CODE (type) == POINTER_TYPE
4736 && TREE_CODE (otype) == POINTER_TYPE
4737 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
4738 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
4739 /* Don't warn about opaque types, where the actual alignment
4740 restriction is unknown. */
4741 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
4742 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
4743 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
4744 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
4745 warning_at (loc, OPT_Wcast_align,
4746 "cast increases required alignment of target type");
e9a25f70 4747
3176a0c2 4748 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 4749 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 4750 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
4751 /* Unlike conversion of integers to pointers, where the
4752 warning is disabled for converting constants because
4753 of cases such as SIG_*, warn about converting constant
4754 pointers to integers. In some cases it may cause unwanted
8d1c7aef 4755 sign extension, and a warning is appropriate. */
c2255bc4
AH
4756 warning_at (loc, OPT_Wpointer_to_int_cast,
4757 "cast from pointer to integer of different size");
400fbf9f 4758
3176a0c2 4759 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 4760 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
4761 warning_at (loc, OPT_Wbad_function_cast,
4762 "cast from function call of type %qT "
4763 "to non-matching type %qT", otype, type);
400fbf9f 4764
3176a0c2 4765 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
4766 && TREE_CODE (otype) == INTEGER_TYPE
4767 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
4768 /* Don't warn about converting any constant. */
4769 && !TREE_CONSTANT (value))
c2255bc4
AH
4770 warning_at (loc,
4771 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
4772 "of different size");
400fbf9f 4773
79bedddc
SR
4774 if (warn_strict_aliasing <= 2)
4775 strict_aliasing_warning (otype, type, expr);
400fbf9f 4776
3897f229
JM
4777 /* If pedantic, warn for conversions between function and object
4778 pointer types, except for converting a null pointer constant
4779 to function pointer type. */
4780 if (pedantic
4781 && TREE_CODE (type) == POINTER_TYPE
4782 && TREE_CODE (otype) == POINTER_TYPE
4783 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
4784 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 4785 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 4786 "conversion of function pointer to object pointer type");
3897f229
JM
4787
4788 if (pedantic
4789 && TREE_CODE (type) == POINTER_TYPE
4790 && TREE_CODE (otype) == POINTER_TYPE
4791 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
4792 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 4793 && !null_pointer_constant_p (value))
c1771a20 4794 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 4795 "conversion of object pointer to function pointer type");
3897f229 4796
3e4093b6 4797 ovalue = value;
3e4093b6 4798 value = convert (type, value);
400fbf9f 4799
3e4093b6 4800 /* Ignore any integer overflow caused by the cast. */
928c19bb 4801 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 4802 {
8bcd6380 4803 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 4804 {
8bcd6380
RS
4805 if (!TREE_OVERFLOW (value))
4806 {
4807 /* Avoid clobbering a shared constant. */
4808 value = copy_node (value);
4809 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
4810 }
6414bad6 4811 }
8bcd6380 4812 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
4813 /* Reset VALUE's overflow flags, ensuring constant sharing. */
4814 value = build_int_cst_wide (TREE_TYPE (value),
4815 TREE_INT_CST_LOW (value),
4816 TREE_INT_CST_HIGH (value));
3e4093b6
RS
4817 }
4818 }
400fbf9f 4819
53cd18ec
JM
4820 /* Don't let a cast be an lvalue. */
4821 if (value == expr)
db3927fb 4822 value = non_lvalue_loc (loc, value);
e9a25f70 4823
928c19bb
JM
4824 /* Don't allow the results of casting to floating-point or complex
4825 types be confused with actual constants, or casts involving
4826 integer and pointer types other than direct integer-to-integer
4827 and integer-to-pointer be confused with integer constant
4828 expressions and null pointer constants. */
4829 if (TREE_CODE (value) == REAL_CST
4830 || TREE_CODE (value) == COMPLEX_CST
4831 || (TREE_CODE (value) == INTEGER_CST
4832 && !((TREE_CODE (expr) == INTEGER_CST
4833 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
4834 || TREE_CODE (expr) == REAL_CST
4835 || TREE_CODE (expr) == COMPLEX_CST)))
4836 value = build1 (NOP_EXPR, type, value);
4837
c2255bc4
AH
4838 if (CAN_HAVE_LOCATION_P (value))
4839 SET_EXPR_LOCATION (value, loc);
3e4093b6 4840 return value;
400fbf9f
JW
4841}
4842
c2255bc4
AH
4843/* Interpret a cast of expression EXPR to type TYPE. LOC is the
4844 location of the open paren of the cast, or the position of the cast
4845 expr. */
3e4093b6 4846tree
c2255bc4 4847c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 4848{
f8893e47 4849 tree type;
928c19bb
JM
4850 tree type_expr = NULL_TREE;
4851 bool type_expr_const = true;
4852 tree ret;
3e4093b6 4853 int saved_wsp = warn_strict_prototypes;
c5c76735 4854
3e4093b6
RS
4855 /* This avoids warnings about unprototyped casts on
4856 integers. E.g. "#define SIG_DFL (void(*)())0". */
4857 if (TREE_CODE (expr) == INTEGER_CST)
4858 warn_strict_prototypes = 0;
928c19bb 4859 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 4860 warn_strict_prototypes = saved_wsp;
c5c76735 4861
c2255bc4 4862 ret = build_c_cast (loc, type, expr);
928c19bb
JM
4863 if (type_expr)
4864 {
4865 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
4866 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !type_expr_const;
c2255bc4 4867 SET_EXPR_LOCATION (ret, loc);
928c19bb 4868 }
24b97832
ILT
4869
4870 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
4871 SET_EXPR_LOCATION (ret, loc);
4872
9e5b2115
PB
4873 /* C++ does not permits types to be defined in a cast, but it
4874 allows references to incomplete types. */
4875 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
4876 warning_at (loc, OPT_Wc___compat,
4877 "defining a type in a cast is invalid in C++");
4878
928c19bb 4879 return ret;
400fbf9f 4880}
3e4093b6
RS
4881\f
4882/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
4883 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
4884 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
4885 MODIFYCODE is the code for a binary operator that we use
4886 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 4887 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
4888 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
4889 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 4890
c2255bc4
AH
4891 LOCATION is the location of the MODIFYCODE operator.
4892 RHS_LOC is the location of the RHS. */
2f6e4e97 4893
3e4093b6 4894tree
32e8bb8e 4895build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 4896 enum tree_code modifycode,
c2255bc4 4897 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 4898{
3e4093b6
RS
4899 tree result;
4900 tree newrhs;
8ce94e44 4901 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
4902 tree lhstype = TREE_TYPE (lhs);
4903 tree olhstype = lhstype;
928c19bb 4904 bool npc;
e9a25f70 4905
3e4093b6
RS
4906 /* Types that aren't fully specified cannot be used in assignments. */
4907 lhs = require_complete_type (lhs);
e9a25f70 4908
3e4093b6
RS
4909 /* Avoid duplicate error messages from operands that had errors. */
4910 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
4911 return error_mark_node;
400fbf9f 4912
46a88c12 4913 /* For ObjC properties, defer this check. */
7bd11157 4914 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
4915 return error_mark_node;
4916
8ce94e44
JM
4917 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4918 {
4919 rhs_semantic_type = TREE_TYPE (rhs);
4920 rhs = TREE_OPERAND (rhs, 0);
4921 }
4922
3e4093b6 4923 newrhs = rhs;
400fbf9f 4924
928c19bb
JM
4925 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
4926 {
4927 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 4928 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 4929 rhs_origtype);
928c19bb
JM
4930 if (inner == error_mark_node)
4931 return error_mark_node;
4932 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4933 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
4934 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
4935 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
4936 protected_set_expr_location (result, location);
4937 return result;
4938 }
4939
3e4093b6
RS
4940 /* If a binary op has been requested, combine the old LHS value with the RHS
4941 producing the value we should actually store into the LHS. */
4942
4943 if (modifycode != NOP_EXPR)
400fbf9f 4944 {
928c19bb 4945 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 4946 lhs = stabilize_reference (lhs);
c9f9eb5d 4947 newrhs = build_binary_op (location,
ba47d38d 4948 modifycode, lhs, rhs, 1);
bbbbb16a
ILT
4949
4950 /* The original type of the right hand side is no longer
4951 meaningful. */
4952 rhs_origtype = NULL_TREE;
400fbf9f 4953 }
400fbf9f 4954
668ea4b1
IS
4955 if (c_dialect_objc ())
4956 {
46a88c12
NP
4957 /* Check if we are modifying an Objective-C property reference;
4958 if so, we need to generate setter calls. */
4959 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1
IS
4960 if (result)
4961 return result;
46a88c12
NP
4962
4963 /* Else, do the check that we postponed for Objective-C. */
7bd11157 4964 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
4965 return error_mark_node;
4966 }
4967
9bf24266 4968 /* Give an error for storing in something that is 'const'. */
bbbd6700 4969
f37acdf9 4970 if (TYPE_READONLY (lhstype)
3e4093b6
RS
4971 || ((TREE_CODE (lhstype) == RECORD_TYPE
4972 || TREE_CODE (lhstype) == UNION_TYPE)
4973 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289
DN
4974 {
4975 readonly_error (lhs, lv_assign);
4976 return error_mark_node;
4977 }
f37acdf9
JM
4978 else if (TREE_READONLY (lhs))
4979 readonly_warning (lhs, lv_assign);
bbbd6700 4980
3e4093b6
RS
4981 /* If storing into a structure or union member,
4982 it has probably been given type `int'.
4983 Compute the type that would go with
4984 the actual amount of storage the member occupies. */
bbbd6700 4985
3e4093b6
RS
4986 if (TREE_CODE (lhs) == COMPONENT_REF
4987 && (TREE_CODE (lhstype) == INTEGER_TYPE
4988 || TREE_CODE (lhstype) == BOOLEAN_TYPE
4989 || TREE_CODE (lhstype) == REAL_TYPE
4990 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
4991 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 4992
3e4093b6
RS
4993 /* If storing in a field that is in actuality a short or narrower than one,
4994 we must store in the field in its actual type. */
4995
4996 if (lhstype != TREE_TYPE (lhs))
4997 {
4998 lhs = copy_node (lhs);
4999 TREE_TYPE (lhs) = lhstype;
400fbf9f 5000 }
400fbf9f 5001
32e8bb8e
ILT
5002 /* Issue -Wc++-compat warnings about an assignment to an enum type
5003 when LHS does not have its original type. This happens for,
5004 e.g., an enum bitfield in a struct. */
5005 if (warn_cxx_compat
5006 && lhs_origtype != NULL_TREE
5007 && lhs_origtype != lhstype
5008 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5009 {
5010 tree checktype = (rhs_origtype != NULL_TREE
5011 ? rhs_origtype
5012 : TREE_TYPE (rhs));
5013 if (checktype != error_mark_node
5014 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype))
5015 warning_at (location, OPT_Wc___compat,
5016 "enum conversion in assignment is invalid in C++");
5017 }
5018
8ce94e44
JM
5019 /* Convert new value to destination type. Fold it first, then
5020 restore any excess precision information, for the sake of
5021 conversion warnings. */
400fbf9f 5022
928c19bb
JM
5023 npc = null_pointer_constant_p (newrhs);
5024 newrhs = c_fully_fold (newrhs, false, NULL);
8ce94e44
JM
5025 if (rhs_semantic_type)
5026 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
744aa42f
ILT
5027 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5028 ic_assign, npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
5029 if (TREE_CODE (newrhs) == ERROR_MARK)
5030 return error_mark_node;
400fbf9f 5031
6e955430
ZL
5032 /* Emit ObjC write barrier, if necessary. */
5033 if (c_dialect_objc () && flag_objc_gc)
5034 {
5035 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5036 if (result)
c9f9eb5d
AH
5037 {
5038 protected_set_expr_location (result, location);
5039 return result;
5040 }
6e955430
ZL
5041 }
5042
ea4b7848 5043 /* Scan operands. */
400fbf9f 5044
53fb4de3 5045 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
3e4093b6 5046 TREE_SIDE_EFFECTS (result) = 1;
c9f9eb5d 5047 protected_set_expr_location (result, location);
400fbf9f 5048
3e4093b6
RS
5049 /* If we got the LHS in a different type for storing in,
5050 convert the result back to the nominal type of LHS
5051 so that the value we return always has the same type
5052 as the LHS argument. */
e855c5ce 5053
3e4093b6
RS
5054 if (olhstype == TREE_TYPE (result))
5055 return result;
c9f9eb5d 5056
744aa42f
ILT
5057 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5058 ic_assign, false, NULL_TREE, NULL_TREE, 0);
c9f9eb5d
AH
5059 protected_set_expr_location (result, location);
5060 return result;
3e4093b6
RS
5061}
5062\f
478a1c5b
ILT
5063/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5064 This is used to implement -fplan9-extensions. */
5065
5066static bool
5067find_anonymous_field_with_type (tree struct_type, tree type)
5068{
5069 tree field;
5070 bool found;
5071
5072 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5073 || TREE_CODE (struct_type) == UNION_TYPE);
5074 found = false;
5075 for (field = TYPE_FIELDS (struct_type);
5076 field != NULL_TREE;
5077 field = TREE_CHAIN (field))
5078 {
5079 if (DECL_NAME (field) == NULL
5080 && comptypes (type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5081 {
5082 if (found)
5083 return false;
5084 found = true;
5085 }
5086 else if (DECL_NAME (field) == NULL
5087 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5088 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5089 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5090 {
5091 if (found)
5092 return false;
5093 found = true;
5094 }
5095 }
5096 return found;
5097}
5098
5099/* RHS is an expression whose type is pointer to struct. If there is
5100 an anonymous field in RHS with type TYPE, then return a pointer to
5101 that field in RHS. This is used with -fplan9-extensions. This
5102 returns NULL if no conversion could be found. */
5103
5104static tree
5105convert_to_anonymous_field (location_t location, tree type, tree rhs)
5106{
5107 tree rhs_struct_type, lhs_main_type;
5108 tree field, found_field;
5109 bool found_sub_field;
5110 tree ret;
5111
5112 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5113 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5114 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5115 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5116
5117 gcc_assert (POINTER_TYPE_P (type));
5118 lhs_main_type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
5119
5120 found_field = NULL_TREE;
5121 found_sub_field = false;
5122 for (field = TYPE_FIELDS (rhs_struct_type);
5123 field != NULL_TREE;
5124 field = TREE_CHAIN (field))
5125 {
5126 if (DECL_NAME (field) != NULL_TREE
5127 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5128 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5129 continue;
5130 if (comptypes (lhs_main_type, TYPE_MAIN_VARIANT (TREE_TYPE (field))))
5131 {
5132 if (found_field != NULL_TREE)
5133 return NULL_TREE;
5134 found_field = field;
5135 }
5136 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5137 lhs_main_type))
5138 {
5139 if (found_field != NULL_TREE)
5140 return NULL_TREE;
5141 found_field = field;
5142 found_sub_field = true;
5143 }
5144 }
5145
5146 if (found_field == NULL_TREE)
5147 return NULL_TREE;
5148
5149 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5150 build_fold_indirect_ref (rhs), found_field,
5151 NULL_TREE);
5152 ret = build_fold_addr_expr_loc (location, ret);
5153
5154 if (found_sub_field)
5155 {
5156 ret = convert_to_anonymous_field (location, type, ret);
5157 gcc_assert (ret != NULL_TREE);
5158 }
5159
5160 return ret;
5161}
5162
bbbbb16a
ILT
5163/* Convert value RHS to type TYPE as preparation for an assignment to
5164 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5165 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5166 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5167 constant before any folding.
3e4093b6
RS
5168 The real work of conversion is done by `convert'.
5169 The purpose of this function is to generate error messages
5170 for assignments that are not allowed in C.
2ac2f164
JM
5171 ERRTYPE says whether it is argument passing, assignment,
5172 initialization or return.
2f6e4e97 5173
c2255bc4 5174 LOCATION is the location of the RHS.
2ac2f164 5175 FUNCTION is a tree for the function being called.
3e4093b6 5176 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5177
3e4093b6 5178static tree
744aa42f
ILT
5179convert_for_assignment (location_t location, tree type, tree rhs,
5180 tree origtype, enum impl_conv errtype,
5181 bool null_pointer_constant, tree fundecl,
5182 tree function, int parmnum)
3e4093b6
RS
5183{
5184 enum tree_code codel = TREE_CODE (type);
8ce94e44 5185 tree orig_rhs = rhs;
3e4093b6
RS
5186 tree rhstype;
5187 enum tree_code coder;
2ac2f164 5188 tree rname = NULL_TREE;
58393038 5189 bool objc_ok = false;
2ac2f164 5190
6b4ef5c1 5191 if (errtype == ic_argpass)
2ac2f164
JM
5192 {
5193 tree selector;
5194 /* Change pointer to function to the function itself for
5195 diagnostics. */
5196 if (TREE_CODE (function) == ADDR_EXPR
5197 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5198 function = TREE_OPERAND (function, 0);
5199
5200 /* Handle an ObjC selector specially for diagnostics. */
5201 selector = objc_message_selector ();
5202 rname = function;
5203 if (selector && parmnum > 2)
5204 {
5205 rname = selector;
5206 parmnum -= 2;
5207 }
5208 }
5209
5210 /* This macro is used to emit diagnostics to ensure that all format
5211 strings are complete sentences, visible to gettext and checked at
5212 compile time. */
c2255bc4 5213#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5214 do { \
5215 switch (errtype) \
5216 { \
5217 case ic_argpass: \
5218 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
c2255bc4
AH
5219 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5220 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
1e053dfe
MLI
5221 "expected %qT but argument is of type %qT", \
5222 type, rhstype); \
5223 break; \
1e053dfe
MLI
5224 case ic_assign: \
5225 pedwarn (LOCATION, OPT, AS); \
5226 break; \
5227 case ic_init: \
6a8f4e12 5228 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5229 break; \
5230 case ic_return: \
c2255bc4 5231 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5232 break; \
5233 default: \
5234 gcc_unreachable (); \
5235 } \
2ac2f164 5236 } while (0)
cb3ca04e 5237
49706e39
MLI
5238 /* This macro is used to emit diagnostics to ensure that all format
5239 strings are complete sentences, visible to gettext and checked at
5240 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5241 extra parameter to enumerate qualifiers. */
5242
5243#define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5244 do { \
5245 switch (errtype) \
5246 { \
5247 case ic_argpass: \
5248 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5249 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5250 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5251 "expected %qT but argument is of type %qT", \
5252 type, rhstype); \
5253 break; \
5254 case ic_assign: \
5255 pedwarn (LOCATION, OPT, AS, QUALS); \
5256 break; \
5257 case ic_init: \
5258 pedwarn (LOCATION, OPT, IN, QUALS); \
5259 break; \
5260 case ic_return: \
5261 pedwarn (LOCATION, OPT, RE, QUALS); \
5262 break; \
5263 default: \
5264 gcc_unreachable (); \
5265 } \
5266 } while (0)
5267
8ce94e44
JM
5268 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5269 rhs = TREE_OPERAND (rhs, 0);
5270
3e4093b6
RS
5271 rhstype = TREE_TYPE (rhs);
5272 coder = TREE_CODE (rhstype);
5273
5274 if (coder == ERROR_MARK)
5275 return error_mark_node;
5276
58393038
ZL
5277 if (c_dialect_objc ())
5278 {
5279 int parmno;
5280
5281 switch (errtype)
5282 {
5283 case ic_return:
5284 parmno = 0;
5285 break;
5286
5287 case ic_assign:
5288 parmno = -1;
5289 break;
5290
5291 case ic_init:
5292 parmno = -2;
5293 break;
5294
5295 default:
5296 parmno = parmnum;
5297 break;
5298 }
5299
5300 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5301 }
5302
bbbbb16a
ILT
5303 if (warn_cxx_compat)
5304 {
5305 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5306 if (checktype != error_mark_node
5307 && TREE_CODE (type) == ENUMERAL_TYPE
5308 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5309 {
81f40b79
ILT
5310 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5311 G_("enum conversion when passing argument "
5312 "%d of %qE is invalid in C++"),
5313 G_("enum conversion in assignment is "
5314 "invalid in C++"),
5315 G_("enum conversion in initialization is "
5316 "invalid in C++"),
5317 G_("enum conversion in return is "
5318 "invalid in C++"));
bbbbb16a
ILT
5319 }
5320 }
5321
3e4093b6 5322 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5323 return rhs;
3e4093b6
RS
5324
5325 if (coder == VOID_TYPE)
400fbf9f 5326 {
6dcc04b0
JM
5327 /* Except for passing an argument to an unprototyped function,
5328 this is a constraint violation. When passing an argument to
5329 an unprototyped function, it is compile-time undefined;
5330 making it a constraint in that case was rejected in
5331 DR#252. */
c2255bc4 5332 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5333 return error_mark_node;
400fbf9f 5334 }
808d6eaa
JM
5335 rhs = require_complete_type (rhs);
5336 if (rhs == error_mark_node)
5337 return error_mark_node;
3e4093b6
RS
5338 /* A type converts to a reference to it.
5339 This code doesn't fully support references, it's just for the
5340 special case of va_start and va_copy. */
5341 if (codel == REFERENCE_TYPE
132da1a5 5342 && comptypes (TREE_TYPE (type), TREE_TYPE (rhs)) == 1)
400fbf9f 5343 {
3e4093b6 5344 if (!lvalue_p (rhs))
400fbf9f 5345 {
c2255bc4 5346 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5347 return error_mark_node;
400fbf9f 5348 }
3e4093b6
RS
5349 if (!c_mark_addressable (rhs))
5350 return error_mark_node;
5351 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5352 SET_EXPR_LOCATION (rhs, location);
3e4093b6
RS
5353
5354 /* We already know that these two types are compatible, but they
5355 may not be exactly identical. In fact, `TREE_TYPE (type)' is
5356 likely to be __builtin_va_list and `TREE_TYPE (rhs)' is
5357 likely to be va_list, a typedef to __builtin_va_list, which
5358 is different enough that it will cause problems later. */
5359 if (TREE_TYPE (TREE_TYPE (rhs)) != TREE_TYPE (type))
c2255bc4
AH
5360 {
5361 rhs = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (type)), rhs);
5362 SET_EXPR_LOCATION (rhs, location);
5363 }
3e4093b6
RS
5364
5365 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5366 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5367 return rhs;
400fbf9f 5368 }
3e4093b6 5369 /* Some types can interconvert without explicit casts. */
3274deff 5370 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5371 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5372 return convert (type, rhs);
5373 /* Arithmetic types all interconvert, and enum is treated like int. */
5374 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5375 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5376 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5377 || codel == BOOLEAN_TYPE)
5378 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5379 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5380 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5381 || coder == BOOLEAN_TYPE))
928c19bb
JM
5382 {
5383 tree ret;
5384 bool save = in_late_binary_op;
dfe776dd 5385 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5386 in_late_binary_op = true;
8ce94e44 5387 ret = convert_and_check (type, orig_rhs);
dfe776dd 5388 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5389 in_late_binary_op = save;
5390 return ret;
5391 }
400fbf9f 5392
79077aea
JJ
5393 /* Aggregates in different TUs might need conversion. */
5394 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5395 && codel == coder
5396 && comptypes (type, rhstype))
5397 return convert_and_check (type, rhs);
5398
ebf0bf7f 5399 /* Conversion to a transparent union or record from its member types.
3e4093b6 5400 This applies only to function arguments. */
ebf0bf7f
JJ
5401 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5402 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5403 && errtype == ic_argpass)
400fbf9f 5404 {
0257e383 5405 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5406
910ad8de 5407 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5408 {
0257e383 5409 tree memb_type = TREE_TYPE (memb);
400fbf9f 5410
3e4093b6 5411 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5412 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5413 break;
e58cd767 5414
3e4093b6
RS
5415 if (TREE_CODE (memb_type) != POINTER_TYPE)
5416 continue;
2f6e4e97 5417
3e4093b6
RS
5418 if (coder == POINTER_TYPE)
5419 {
5420 tree ttl = TREE_TYPE (memb_type);
5421 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5422
3e4093b6
RS
5423 /* Any non-function converts to a [const][volatile] void *
5424 and vice versa; otherwise, targets must be the same.
5425 Meanwhile, the lhs target must have all the qualifiers of
5426 the rhs. */
5427 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
744aa42f 5428 || comp_target_types (location, memb_type, rhstype))
3e4093b6
RS
5429 {
5430 /* If this type won't generate any warnings, use it. */
5431 if (TYPE_QUALS (ttl) == TYPE_QUALS (ttr)
5432 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5433 && TREE_CODE (ttl) == FUNCTION_TYPE)
5434 ? ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5435 == TYPE_QUALS (ttr))
5436 : ((TYPE_QUALS (ttl) | TYPE_QUALS (ttr))
5437 == TYPE_QUALS (ttl))))
5438 break;
400fbf9f 5439
3e4093b6 5440 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5441 if (!marginal_memb)
5442 marginal_memb = memb;
3e4093b6
RS
5443 }
5444 }
82bde854 5445
3e4093b6 5446 /* Can convert integer zero to any pointer type. */
928c19bb 5447 if (null_pointer_constant)
3e4093b6
RS
5448 {
5449 rhs = null_pointer_node;
5450 break;
5451 }
5452 }
400fbf9f 5453
0257e383 5454 if (memb || marginal_memb)
3e4093b6 5455 {
0257e383 5456 if (!memb)
3e4093b6
RS
5457 {
5458 /* We have only a marginally acceptable member type;
5459 it needs a warning. */
0257e383 5460 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5461 tree ttr = TREE_TYPE (rhstype);
714a0864 5462
3e4093b6
RS
5463 /* Const and volatile mean something different for function
5464 types, so the usual warnings are not appropriate. */
5465 if (TREE_CODE (ttr) == FUNCTION_TYPE
5466 && TREE_CODE (ttl) == FUNCTION_TYPE)
5467 {
5468 /* Because const and volatile on functions are
5469 restrictions that say the function will not do
5470 certain things, it is okay to use a const or volatile
5471 function where an ordinary one is wanted, but not
5472 vice-versa. */
36c5e70a
BE
5473 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5474 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5475 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5476 G_("passing argument %d of %qE "
49706e39 5477 "makes %q#v qualified function "
2ac2f164 5478 "pointer from unqualified"),
49706e39 5479 G_("assignment makes %q#v qualified "
2ac2f164
JM
5480 "function pointer from "
5481 "unqualified"),
49706e39 5482 G_("initialization makes %q#v qualified "
2ac2f164
JM
5483 "function pointer from "
5484 "unqualified"),
49706e39
MLI
5485 G_("return makes %q#v qualified function "
5486 "pointer from unqualified"),
5487 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5488 }
36c5e70a
BE
5489 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5490 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
49706e39 5491 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5492 G_("passing argument %d of %qE discards "
49706e39
MLI
5493 "%qv qualifier from pointer target type"),
5494 G_("assignment discards %qv qualifier "
2ac2f164 5495 "from pointer target type"),
49706e39 5496 G_("initialization discards %qv qualifier "
2ac2f164 5497 "from pointer target type"),
49706e39
MLI
5498 G_("return discards %qv qualifier from "
5499 "pointer target type"),
5500 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5501
5502 memb = marginal_memb;
3e4093b6 5503 }
400fbf9f 5504
fcf73884 5505 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5506 pedwarn (location, OPT_Wpedantic,
fcf73884 5507 "ISO C prohibits argument conversion to union type");
0e7c47fa 5508
db3927fb 5509 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5510 return build_constructor_single (type, memb, rhs);
3e4093b6 5511 }
0e7c47fa
RK
5512 }
5513
3e4093b6
RS
5514 /* Conversions among pointers */
5515 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5516 && (coder == codel))
400fbf9f 5517 {
3e4093b6
RS
5518 tree ttl = TREE_TYPE (type);
5519 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5520 tree mvl = ttl;
5521 tree mvr = ttr;
3e4093b6 5522 bool is_opaque_pointer;
264fa2db 5523 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5524 addr_space_t asl;
5525 addr_space_t asr;
400fbf9f 5526
46df2823
JM
5527 if (TREE_CODE (mvl) != ARRAY_TYPE)
5528 mvl = TYPE_MAIN_VARIANT (mvl);
5529 if (TREE_CODE (mvr) != ARRAY_TYPE)
5530 mvr = TYPE_MAIN_VARIANT (mvr);
3e4093b6 5531 /* Opaque pointers are treated like void pointers. */
f83c7f63 5532 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5533
478a1c5b
ILT
5534 /* The Plan 9 compiler permits a pointer to a struct to be
5535 automatically converted into a pointer to an anonymous field
5536 within the struct. */
5537 if (flag_plan9_extensions
5538 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5539 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5540 && mvl != mvr)
5541 {
5542 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5543 if (new_rhs != NULL_TREE)
5544 {
5545 rhs = new_rhs;
5546 rhstype = TREE_TYPE (rhs);
5547 coder = TREE_CODE (rhstype);
5548 ttr = TREE_TYPE (rhstype);
5549 mvr = TYPE_MAIN_VARIANT (ttr);
5550 }
5551 }
5552
b7e20b53 5553 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5554 for the purpose of reducing the number of false positives, we
5555 tolerate the special case of
b7e20b53 5556
c22cacf3 5557 int *p = NULL;
b7e20b53 5558
c22cacf3 5559 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5560 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c2255bc4
AH
5561 warning_at (location, OPT_Wc___compat,
5562 "request for implicit conversion "
5563 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 5564
36c5e70a
BE
5565 /* See if the pointers point to incompatible address spaces. */
5566 asl = TYPE_ADDR_SPACE (ttl);
5567 asr = TYPE_ADDR_SPACE (ttr);
5568 if (!null_pointer_constant_p (rhs)
5569 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5570 {
5571 switch (errtype)
5572 {
5573 case ic_argpass:
5574 error_at (location, "passing argument %d of %qE from pointer to "
5575 "non-enclosed address space", parmnum, rname);
5576 break;
5577 case ic_assign:
5578 error_at (location, "assignment from pointer to "
5579 "non-enclosed address space");
5580 break;
5581 case ic_init:
5582 error_at (location, "initialization from pointer to "
5583 "non-enclosed address space");
5584 break;
5585 case ic_return:
5586 error_at (location, "return from pointer to "
5587 "non-enclosed address space");
5588 break;
5589 default:
5590 gcc_unreachable ();
5591 }
5592 return error_mark_node;
5593 }
5594
7876a414
KG
5595 /* Check if the right-hand side has a format attribute but the
5596 left-hand side doesn't. */
104f8784
KG
5597 if (warn_missing_format_attribute
5598 && check_missing_format_attribute (type, rhstype))
c22cacf3 5599 {
104f8784
KG
5600 switch (errtype)
5601 {
5602 case ic_argpass:
c2255bc4
AH
5603 warning_at (location, OPT_Wmissing_format_attribute,
5604 "argument %d of %qE might be "
5605 "a candidate for a format attribute",
5606 parmnum, rname);
104f8784
KG
5607 break;
5608 case ic_assign:
c2255bc4
AH
5609 warning_at (location, OPT_Wmissing_format_attribute,
5610 "assignment left-hand side might be "
5611 "a candidate for a format attribute");
104f8784
KG
5612 break;
5613 case ic_init:
c2255bc4
AH
5614 warning_at (location, OPT_Wmissing_format_attribute,
5615 "initialization left-hand side might be "
5616 "a candidate for a format attribute");
104f8784
KG
5617 break;
5618 case ic_return:
c2255bc4
AH
5619 warning_at (location, OPT_Wmissing_format_attribute,
5620 "return type might be "
5621 "a candidate for a format attribute");
104f8784
KG
5622 break;
5623 default:
5624 gcc_unreachable ();
5625 }
7876a414 5626 }
c22cacf3 5627
3e4093b6
RS
5628 /* Any non-function converts to a [const][volatile] void *
5629 and vice versa; otherwise, targets must be the same.
5630 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
5631 if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
744aa42f 5632 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 5633 || is_opaque_pointer
12753674
RE
5634 || (c_common_unsigned_type (mvl)
5635 == c_common_unsigned_type (mvr)))
3e4093b6
RS
5636 {
5637 if (pedantic
5638 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
5639 ||
5640 (VOID_TYPE_P (ttr)
928c19bb 5641 && !null_pointer_constant
3e4093b6 5642 && TREE_CODE (ttl) == FUNCTION_TYPE)))
c1771a20 5643 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
509c9d60 5644 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
5645 "%qE between function pointer "
5646 "and %<void *%>"),
4b794eaf 5647 G_("ISO C forbids assignment between "
2ac2f164 5648 "function pointer and %<void *%>"),
4b794eaf 5649 G_("ISO C forbids initialization between "
2ac2f164 5650 "function pointer and %<void *%>"),
4b794eaf 5651 G_("ISO C forbids return between function "
2ac2f164 5652 "pointer and %<void *%>"));
3e4093b6
RS
5653 /* Const and volatile mean something different for function types,
5654 so the usual warnings are not appropriate. */
5655 else if (TREE_CODE (ttr) != FUNCTION_TYPE
5656 && TREE_CODE (ttl) != FUNCTION_TYPE)
5657 {
36c5e70a
BE
5658 if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5659 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
58393038 5660 {
6c39e757
NP
5661 WARN_FOR_QUALIFIERS (location, 0,
5662 G_("passing argument %d of %qE discards "
5663 "%qv qualifier from pointer target type"),
5664 G_("assignment discards %qv qualifier "
5665 "from pointer target type"),
5666 G_("initialization discards %qv qualifier "
5667 "from pointer target type"),
5668 G_("return discards %qv qualifier from "
5669 "pointer target type"),
5670 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 5671 }
3e4093b6
RS
5672 /* If this is not a case of ignoring a mismatch in signedness,
5673 no warning. */
5674 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 5675 || target_cmp)
3e4093b6
RS
5676 ;
5677 /* If there is a mismatch, do warn. */
f2fd3821 5678 else if (warn_pointer_sign)
c2255bc4 5679 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
509c9d60 5680 G_("pointer targets in passing argument "
2ac2f164 5681 "%d of %qE differ in signedness"),
4b794eaf 5682 G_("pointer targets in assignment "
2ac2f164 5683 "differ in signedness"),
4b794eaf 5684 G_("pointer targets in initialization "
2ac2f164 5685 "differ in signedness"),
4b794eaf 5686 G_("pointer targets in return differ "
2ac2f164 5687 "in signedness"));
3e4093b6
RS
5688 }
5689 else if (TREE_CODE (ttl) == FUNCTION_TYPE
5690 && TREE_CODE (ttr) == FUNCTION_TYPE)
5691 {
5692 /* Because const and volatile on functions are restrictions
5693 that say the function will not do certain things,
5694 it is okay to use a const or volatile function
5695 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
5696 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5697 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5698 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5699 G_("passing argument %d of %qE makes "
49706e39 5700 "%q#v qualified function pointer "
2ac2f164 5701 "from unqualified"),
49706e39 5702 G_("assignment makes %q#v qualified function "
2ac2f164 5703 "pointer from unqualified"),
49706e39 5704 G_("initialization makes %q#v qualified "
2ac2f164 5705 "function pointer from unqualified"),
49706e39
MLI
5706 G_("return makes %q#v qualified function "
5707 "pointer from unqualified"),
5708 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
5709 }
5710 }
5711 else
58393038
ZL
5712 /* Avoid warning about the volatile ObjC EH puts on decls. */
5713 if (!objc_ok)
c2255bc4 5714 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 5715 G_("passing argument %d of %qE from "
58393038 5716 "incompatible pointer type"),
4b794eaf
JJ
5717 G_("assignment from incompatible pointer type"),
5718 G_("initialization from incompatible "
58393038 5719 "pointer type"),
4b794eaf 5720 G_("return from incompatible pointer type"));
58393038 5721
3e4093b6
RS
5722 return convert (type, rhs);
5723 }
b494fd98
EB
5724 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
5725 {
6dcc04b0
JM
5726 /* ??? This should not be an error when inlining calls to
5727 unprototyped functions. */
c2255bc4 5728 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
5729 return error_mark_node;
5730 }
3e4093b6 5731 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 5732 {
3e4093b6
RS
5733 /* An explicit constant 0 can convert to a pointer,
5734 or one that results from arithmetic, even including
5735 a cast to integer type. */
928c19bb 5736 if (!null_pointer_constant)
c2255bc4 5737 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 5738 G_("passing argument %d of %qE makes "
2ac2f164 5739 "pointer from integer without a cast"),
4b794eaf 5740 G_("assignment makes pointer from integer "
2ac2f164 5741 "without a cast"),
4b794eaf 5742 G_("initialization makes pointer from "
2ac2f164 5743 "integer without a cast"),
4b794eaf 5744 G_("return makes pointer from integer "
2ac2f164 5745 "without a cast"));
b3006337
EB
5746
5747 return convert (type, rhs);
400fbf9f 5748 }
3e4093b6 5749 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 5750 {
c2255bc4 5751 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 5752 G_("passing argument %d of %qE makes integer "
2ac2f164 5753 "from pointer without a cast"),
4b794eaf 5754 G_("assignment makes integer from pointer "
2ac2f164 5755 "without a cast"),
4b794eaf 5756 G_("initialization makes integer from pointer "
2ac2f164 5757 "without a cast"),
4b794eaf 5758 G_("return makes integer from pointer "
2ac2f164 5759 "without a cast"));
3e4093b6
RS
5760 return convert (type, rhs);
5761 }
5762 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
5763 {
5764 tree ret;
5765 bool save = in_late_binary_op;
5766 in_late_binary_op = true;
5767 ret = convert (type, rhs);
5768 in_late_binary_op = save;
5769 return ret;
5770 }
400fbf9f 5771
2ac2f164 5772 switch (errtype)
3e4093b6 5773 {
2ac2f164 5774 case ic_argpass:
c2255bc4 5775 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
a7da8b42
MLI
5776 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
5777 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
5778 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
5779 break;
5780 case ic_assign:
c2255bc4
AH
5781 error_at (location, "incompatible types when assigning to type %qT from "
5782 "type %qT", type, rhstype);
2ac2f164
JM
5783 break;
5784 case ic_init:
c2255bc4
AH
5785 error_at (location,
5786 "incompatible types when initializing type %qT using type %qT",
5787 type, rhstype);
2ac2f164
JM
5788 break;
5789 case ic_return:
c2255bc4
AH
5790 error_at (location,
5791 "incompatible types when returning type %qT but %qT was "
5792 "expected", rhstype, type);
2ac2f164
JM
5793 break;
5794 default:
5795 gcc_unreachable ();
400fbf9f 5796 }
53b01f59 5797
3e4093b6
RS
5798 return error_mark_node;
5799}
3e4093b6
RS
5800\f
5801/* If VALUE is a compound expr all of whose expressions are constant, then
5802 return its value. Otherwise, return error_mark_node.
15b732b2 5803
3e4093b6
RS
5804 This is for handling COMPOUND_EXPRs as initializer elements
5805 which is allowed with a warning when -pedantic is specified. */
15b732b2 5806
3e4093b6
RS
5807static tree
5808valid_compound_expr_initializer (tree value, tree endtype)
5809{
5810 if (TREE_CODE (value) == COMPOUND_EXPR)
5811 {
5812 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
5813 == error_mark_node)
5814 return error_mark_node;
5815 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
5816 endtype);
5817 }
116df786 5818 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
5819 return error_mark_node;
5820 else
5821 return value;
15b732b2 5822}
400fbf9f 5823\f
3e4093b6
RS
5824/* Perform appropriate conversions on the initial value of a variable,
5825 store it in the declaration DECL,
5826 and print any error messages that are appropriate.
bbbbb16a 5827 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
5828 If the init is invalid, store an ERROR_MARK.
5829
5830 INIT_LOC is the location of the initial value. */
400fbf9f 5831
3e4093b6 5832void
c2255bc4 5833store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 5834{
3e4093b6 5835 tree value, type;
928c19bb 5836 bool npc = false;
400fbf9f 5837
3e4093b6 5838 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 5839
3e4093b6
RS
5840 type = TREE_TYPE (decl);
5841 if (TREE_CODE (type) == ERROR_MARK)
5842 return;
400fbf9f 5843
3e4093b6 5844 /* Digest the specified initializer into an expression. */
400fbf9f 5845
928c19bb
JM
5846 if (init)
5847 npc = null_pointer_constant_p (init);
c2255bc4
AH
5848 value = digest_init (init_loc, type, init, origtype, npc,
5849 true, TREE_STATIC (decl));
400fbf9f 5850
3e4093b6 5851 /* Store the expression if valid; else report error. */
400fbf9f 5852
3176a0c2 5853 if (!in_system_header
3f75a254 5854 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
5855 warning (OPT_Wtraditional, "traditional C rejects automatic "
5856 "aggregate initialization");
2f6e4e97 5857
3e4093b6 5858 DECL_INITIAL (decl) = value;
400fbf9f 5859
3e4093b6
RS
5860 /* ANSI wants warnings about out-of-range constant initializers. */
5861 STRIP_TYPE_NOPS (value);
b8698a0f 5862 if (TREE_STATIC (decl))
c2658540 5863 constant_expression_warning (value);
400fbf9f 5864
3e4093b6
RS
5865 /* Check if we need to set array size from compound literal size. */
5866 if (TREE_CODE (type) == ARRAY_TYPE
5867 && TYPE_DOMAIN (type) == 0
5868 && value != error_mark_node)
400fbf9f 5869 {
3e4093b6
RS
5870 tree inside_init = init;
5871
ed248cf7 5872 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
5873 inside_init = fold (inside_init);
5874
5875 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
5876 {
8d9f82d5 5877 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 5878
8d9f82d5 5879 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
5880 {
5881 /* For int foo[] = (int [3]){1}; we need to set array size
5882 now since later on array initializer will be just the
5883 brace enclosed list of the compound literal. */
e30ecc5d 5884 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 5885 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 5886 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 5887 layout_type (type);
8d9f82d5 5888 layout_decl (cldecl, 0);
e30ecc5d
JJ
5889 TREE_TYPE (decl)
5890 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
5891 }
5892 }
400fbf9f 5893 }
3e4093b6
RS
5894}
5895\f
5896/* Methods for storing and printing names for error messages. */
400fbf9f 5897
3e4093b6
RS
5898/* Implement a spelling stack that allows components of a name to be pushed
5899 and popped. Each element on the stack is this structure. */
400fbf9f 5900
3e4093b6
RS
5901struct spelling
5902{
5903 int kind;
5904 union
400fbf9f 5905 {
a0f0ab9f 5906 unsigned HOST_WIDE_INT i;
3e4093b6
RS
5907 const char *s;
5908 } u;
5909};
2f6e4e97 5910
3e4093b6
RS
5911#define SPELLING_STRING 1
5912#define SPELLING_MEMBER 2
5913#define SPELLING_BOUNDS 3
400fbf9f 5914
3e4093b6
RS
5915static struct spelling *spelling; /* Next stack element (unused). */
5916static struct spelling *spelling_base; /* Spelling stack base. */
5917static int spelling_size; /* Size of the spelling stack. */
400fbf9f 5918
3e4093b6
RS
5919/* Macros to save and restore the spelling stack around push_... functions.
5920 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 5921
3e4093b6
RS
5922#define SPELLING_DEPTH() (spelling - spelling_base)
5923#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 5924
3e4093b6
RS
5925/* Push an element on the spelling stack with type KIND and assign VALUE
5926 to MEMBER. */
400fbf9f 5927
3e4093b6
RS
5928#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
5929{ \
5930 int depth = SPELLING_DEPTH (); \
5931 \
5932 if (depth >= spelling_size) \
5933 { \
5934 spelling_size += 10; \
cca8ead2
BI
5935 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
5936 spelling_size); \
3e4093b6
RS
5937 RESTORE_SPELLING_DEPTH (depth); \
5938 } \
5939 \
5940 spelling->kind = (KIND); \
5941 spelling->MEMBER = (VALUE); \
5942 spelling++; \
5943}
400fbf9f 5944
3e4093b6 5945/* Push STRING on the stack. Printed literally. */
400fbf9f 5946
3e4093b6
RS
5947static void
5948push_string (const char *string)
5949{
5950 PUSH_SPELLING (SPELLING_STRING, string, u.s);
5951}
400fbf9f 5952
3e4093b6 5953/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 5954
3e4093b6
RS
5955static void
5956push_member_name (tree decl)
5957{
5958 const char *const string
88388a52
JM
5959 = (DECL_NAME (decl)
5960 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
5961 : _("<anonymous>"));
3e4093b6
RS
5962 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
5963}
400fbf9f 5964
3e4093b6 5965/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 5966
3e4093b6 5967static void
a0f0ab9f 5968push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
5969{
5970 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
5971}
bb58bec5 5972
3e4093b6 5973/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 5974
3e4093b6
RS
5975static int
5976spelling_length (void)
5977{
5978 int size = 0;
5979 struct spelling *p;
400fbf9f 5980
3e4093b6
RS
5981 for (p = spelling_base; p < spelling; p++)
5982 {
5983 if (p->kind == SPELLING_BOUNDS)
5984 size += 25;
5985 else
5986 size += strlen (p->u.s) + 1;
5987 }
5988
5989 return size;
400fbf9f 5990}
400fbf9f 5991
3e4093b6 5992/* Print the spelling to BUFFER and return it. */
400fbf9f 5993
3e4093b6
RS
5994static char *
5995print_spelling (char *buffer)
400fbf9f 5996{
3e4093b6
RS
5997 char *d = buffer;
5998 struct spelling *p;
400fbf9f 5999
3e4093b6
RS
6000 for (p = spelling_base; p < spelling; p++)
6001 if (p->kind == SPELLING_BOUNDS)
6002 {
a0f0ab9f 6003 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6004 d += strlen (d);
6005 }
6006 else
6007 {
6008 const char *s;
6009 if (p->kind == SPELLING_MEMBER)
6010 *d++ = '.';
6011 for (s = p->u.s; (*d = *s++); d++)
6012 ;
6013 }
6014 *d++ = '\0';
6015 return buffer;
6016}
400fbf9f 6017
3e4093b6 6018/* Issue an error message for a bad initializer component.
6a8f4e12 6019 GMSGID identifies the message.
3e4093b6 6020 The component name is taken from the spelling stack. */
400fbf9f 6021
3e4093b6 6022void
6a8f4e12 6023error_init (const char *gmsgid)
3e4093b6
RS
6024{
6025 char *ofwhat;
400fbf9f 6026
6a8f4e12
AP
6027 /* The gmsgid may be a format string with %< and %>. */
6028 error (gmsgid);
28dab132 6029 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6030 if (*ofwhat)
bda67431 6031 error ("(near initialization for %qs)", ofwhat);
3e4093b6 6032}
400fbf9f 6033
fcf73884
MLI
6034/* Issue a pedantic warning for a bad initializer component. OPT is
6035 the option OPT_* (from options.h) controlling this warning or 0 if
6a8f4e12 6036 it is unconditionally given. GMSGID identifies the message. The
fcf73884 6037 component name is taken from the spelling stack. */
400fbf9f 6038
3e4093b6 6039void
6a8f4e12 6040pedwarn_init (location_t location, int opt, const char *gmsgid)
3e4093b6
RS
6041{
6042 char *ofwhat;
f90e8e2e 6043
6a8f4e12
AP
6044 /* The gmsgid may be a format string with %< and %>. */
6045 pedwarn (location, opt, gmsgid);
28dab132 6046 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6047 if (*ofwhat)
509c9d60 6048 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
3e4093b6 6049}
9f720c3e 6050
b8698a0f 6051/* Issue a warning for a bad initializer component.
683d6ff9
MLI
6052
6053 OPT is the OPT_W* value corresponding to the warning option that
6a8f4e12 6054 controls this warning. GMSGID identifies the message. The
683d6ff9 6055 component name is taken from the spelling stack. */
61179109 6056
3e4093b6 6057static void
6a8f4e12 6058warning_init (int opt, const char *gmsgid)
3e4093b6
RS
6059{
6060 char *ofwhat;
7e842ef8 6061
6a8f4e12
AP
6062 /* The gmsgid may be a format string with %< and %>. */
6063 warning (opt, gmsgid);
28dab132 6064 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6065 if (*ofwhat)
683d6ff9 6066 warning (opt, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
6067}
6068\f
916c5919
JM
6069/* If TYPE is an array type and EXPR is a parenthesized string
6070 constant, warn if pedantic that EXPR is being used to initialize an
6071 object of type TYPE. */
6072
6073void
6074maybe_warn_string_init (tree type, struct c_expr expr)
6075{
6076 if (pedantic
6077 && TREE_CODE (type) == ARRAY_TYPE
6078 && TREE_CODE (expr.value) == STRING_CST
6079 && expr.original_code != STRING_CST)
c1771a20 6080 pedwarn_init (input_location, OPT_Wpedantic,
fcf73884 6081 "array initialized from parenthesized string constant");
916c5919
JM
6082}
6083
3e4093b6
RS
6084/* Digest the parser output INIT as an initializer for type TYPE.
6085 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6086
bbbbb16a
ILT
6087 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6088
928c19bb
JM
6089 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6090
916c5919
JM
6091 If INIT is a string constant, STRICT_STRING is true if it is
6092 unparenthesized or we should not warn here for it being parenthesized.
6093 For other types of INIT, STRICT_STRING is not used.
6094
c2255bc4
AH
6095 INIT_LOC is the location of the INIT.
6096
3e4093b6
RS
6097 REQUIRE_CONSTANT requests an error if non-constant initializers or
6098 elements are seen. */
7e842ef8 6099
3e4093b6 6100static tree
c2255bc4
AH
6101digest_init (location_t init_loc, tree type, tree init, tree origtype,
6102 bool null_pointer_constant, bool strict_string,
6103 int require_constant)
3e4093b6
RS
6104{
6105 enum tree_code code = TREE_CODE (type);
6106 tree inside_init = init;
8ce94e44 6107 tree semantic_type = NULL_TREE;
928c19bb 6108 bool maybe_const = true;
7e842ef8 6109
3e4093b6 6110 if (type == error_mark_node
f01da1a5 6111 || !init
3e4093b6
RS
6112 || init == error_mark_node
6113 || TREE_TYPE (init) == error_mark_node)
6114 return error_mark_node;
7e842ef8 6115
ed248cf7 6116 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6117
8ce94e44
JM
6118 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6119 {
6120 semantic_type = TREE_TYPE (inside_init);
6121 inside_init = TREE_OPERAND (inside_init, 0);
6122 }
928c19bb
JM
6123 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6124 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6125
3e4093b6
RS
6126 /* Initialization of an array of chars from a string constant
6127 optionally enclosed in braces. */
7e842ef8 6128
197463ae
JM
6129 if (code == ARRAY_TYPE && inside_init
6130 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6
RS
6131 {
6132 tree typ1 = TYPE_MAIN_VARIANT (TREE_TYPE (type));
197463ae
JM
6133 /* Note that an array could be both an array of character type
6134 and an array of wchar_t if wchar_t is signed char or unsigned
6135 char. */
6136 bool char_array = (typ1 == char_type_node
6137 || typ1 == signed_char_type_node
6138 || typ1 == unsigned_char_type_node);
6139 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6140 bool char16_array = !!comptypes (typ1, char16_type_node);
6141 bool char32_array = !!comptypes (typ1, char32_type_node);
6142
6143 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6144 {
916c5919 6145 struct c_expr expr;
c466b2cd 6146 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6147 expr.value = inside_init;
6148 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6149 expr.original_type = NULL;
916c5919
JM
6150 maybe_warn_string_init (type, expr);
6151
a45e580b 6152 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6153 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6154 "initialization of a flexible array member");
6155
3e4093b6 6156 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6157 TYPE_MAIN_VARIANT (type)))
3e4093b6 6158 return inside_init;
7e842ef8 6159
c466b2cd 6160 if (char_array)
3e4093b6 6161 {
c466b2cd
KVH
6162 if (typ2 != char_type_node)
6163 {
6164 error_init ("char-array initialized from wide string");
6165 return error_mark_node;
6166 }
3e4093b6 6167 }
c466b2cd 6168 else
3e4093b6 6169 {
c466b2cd
KVH
6170 if (typ2 == char_type_node)
6171 {
6172 error_init ("wide character array initialized from non-wide "
6173 "string");
6174 return error_mark_node;
6175 }
6176 else if (!comptypes(typ1, typ2))
6177 {
6178 error_init ("wide character array initialized from "
6179 "incompatible wide string");
6180 return error_mark_node;
6181 }
7e842ef8 6182 }
2f6e4e97 6183
3e4093b6
RS
6184 TREE_TYPE (inside_init) = type;
6185 if (TYPE_DOMAIN (type) != 0
6186 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6187 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6188 {
6189 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6190
c466b2cd 6191 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6192 because it's ok to ignore the terminating null char
6193 that is counted in the length of the constant. */
5eb4df45
ILT
6194 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6195 (len
6196 - (TYPE_PRECISION (typ1)
6197 / BITS_PER_UNIT))))
6198 pedwarn_init (init_loc, 0,
6199 ("initializer-string for array of chars "
6200 "is too long"));
6201 else if (warn_cxx_compat
6202 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6203 warning_at (init_loc, OPT_Wc___compat,
6204 ("initializer-string for array chars "
6205 "is too long for C++"));
6206 }
7e842ef8 6207
3e4093b6 6208 return inside_init;
7e842ef8 6209 }
197463ae
JM
6210 else if (INTEGRAL_TYPE_P (typ1))
6211 {
6212 error_init ("array of inappropriate type initialized "
6213 "from string constant");
6214 return error_mark_node;
6215 }
7e842ef8
PE
6216 }
6217
3e4093b6
RS
6218 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6219 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6220 below and handle as a constructor. */
e89be13b
JJ
6221 if (code == VECTOR_TYPE
6222 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6223 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6224 && TREE_CONSTANT (inside_init))
6225 {
6226 if (TREE_CODE (inside_init) == VECTOR_CST
6227 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6228 TYPE_MAIN_VARIANT (type)))
6229 return inside_init;
6230
6231 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6232 {
4038c495
GB
6233 unsigned HOST_WIDE_INT ix;
6234 tree value;
6235 bool constant_p = true;
e89be13b
JJ
6236
6237 /* Iterate through elements and check if all constructor
6238 elements are *_CSTs. */
4038c495
GB
6239 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6240 if (!CONSTANT_CLASS_P (value))
6241 {
6242 constant_p = false;
6243 break;
6244 }
e89be13b 6245
4038c495
GB
6246 if (constant_p)
6247 return build_vector_from_ctor (type,
6248 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6249 }
6250 }
6035d635 6251
ca085fd7
MLI
6252 if (warn_sequence_point)
6253 verify_sequence_points (inside_init);
6254
3e4093b6
RS
6255 /* Any type can be initialized
6256 from an expression of the same type, optionally with braces. */
400fbf9f 6257
3e4093b6
RS
6258 if (inside_init && TREE_TYPE (inside_init) != 0
6259 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6260 TYPE_MAIN_VARIANT (type))
3e4093b6 6261 || (code == ARRAY_TYPE
132da1a5 6262 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6263 || (code == VECTOR_TYPE
132da1a5 6264 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6265 || (code == POINTER_TYPE
3897f229 6266 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6267 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6268 TREE_TYPE (type)))))
3e4093b6
RS
6269 {
6270 if (code == POINTER_TYPE)
b494fd98 6271 {
b494fd98
EB
6272 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6273 {
f2a71bbc
JM
6274 if (TREE_CODE (inside_init) == STRING_CST
6275 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6276 inside_init = array_to_pointer_conversion
6277 (init_loc, inside_init);
f2a71bbc
JM
6278 else
6279 {
6280 error_init ("invalid use of non-lvalue array");
6281 return error_mark_node;
6282 }
b494fd98 6283 }
f2a71bbc 6284 }
b494fd98 6285
bae39a73
NS
6286 if (code == VECTOR_TYPE)
6287 /* Although the types are compatible, we may require a
6288 conversion. */
6289 inside_init = convert (type, inside_init);
3e4093b6 6290
ca58211b
PB
6291 if (require_constant
6292 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6293 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6294 {
3e4093b6
RS
6295 /* As an extension, allow initializing objects with static storage
6296 duration with compound literals (which are then treated just as
ca58211b
PB
6297 the brace enclosed list they contain). Also allow this for
6298 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6299 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6300 inside_init = DECL_INITIAL (decl);
400fbf9f 6301 }
3e4093b6
RS
6302
6303 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6304 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6305 {
3e4093b6
RS
6306 error_init ("array initialized from non-constant array expression");
6307 return error_mark_node;
400fbf9f 6308 }
400fbf9f 6309
c1771a20 6310 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6311 -pedantic-errors is specified. In the later case, we always want
6312 an error. In the former case, we simply want a warning. */
6313 if (require_constant && pedantic
6314 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6315 {
6316 inside_init
6317 = valid_compound_expr_initializer (inside_init,
6318 TREE_TYPE (inside_init));
6319 if (inside_init == error_mark_node)
6320 error_init ("initializer element is not constant");
2f6e4e97 6321 else
c1771a20 6322 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6323 "initializer element is not constant");
3e4093b6
RS
6324 if (flag_pedantic_errors)
6325 inside_init = error_mark_node;
6326 }
6327 else if (require_constant
116df786
RH
6328 && !initializer_constant_valid_p (inside_init,
6329 TREE_TYPE (inside_init)))
3e4093b6
RS
6330 {
6331 error_init ("initializer element is not constant");
6332 inside_init = error_mark_node;
8b40563c 6333 }
928c19bb 6334 else if (require_constant && !maybe_const)
c2255bc4 6335 pedwarn_init (init_loc, 0,
928c19bb 6336 "initializer element is not a constant expression");
f735a153 6337
8fcef540
KG
6338 /* Added to enable additional -Wmissing-format-attribute warnings. */
6339 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
c2255bc4
AH
6340 inside_init = convert_for_assignment (init_loc, type, inside_init,
6341 origtype,
bbbbb16a 6342 ic_init, null_pointer_constant,
928c19bb 6343 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6344 return inside_init;
6345 }
f735a153 6346
3e4093b6 6347 /* Handle scalar types, including conversions. */
400fbf9f 6348
ab22c1fa
CF
6349 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6350 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6351 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6352 {
f2a71bbc
JM
6353 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6354 && (TREE_CODE (init) == STRING_CST
6355 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6356 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6357 if (semantic_type)
6358 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6359 inside_init);
3e4093b6 6360 inside_init
c2255bc4
AH
6361 = convert_for_assignment (init_loc, type, inside_init, origtype,
6362 ic_init, null_pointer_constant,
3e4093b6 6363 NULL_TREE, NULL_TREE, 0);
2f6e4e97 6364
3274deff
JW
6365 /* Check to see if we have already given an error message. */
6366 if (inside_init == error_mark_node)
6367 ;
3f75a254 6368 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6369 {
3e4093b6
RS
6370 error_init ("initializer element is not constant");
6371 inside_init = error_mark_node;
400fbf9f 6372 }
3e4093b6 6373 else if (require_constant
116df786
RH
6374 && !initializer_constant_valid_p (inside_init,
6375 TREE_TYPE (inside_init)))
400fbf9f 6376 {
3e4093b6
RS
6377 error_init ("initializer element is not computable at load time");
6378 inside_init = error_mark_node;
400fbf9f 6379 }
928c19bb 6380 else if (require_constant && !maybe_const)
c2255bc4 6381 pedwarn_init (init_loc, 0,
928c19bb 6382 "initializer element is not a constant expression");
3e4093b6
RS
6383
6384 return inside_init;
400fbf9f 6385 }
d9fc6069 6386
3e4093b6 6387 /* Come here only for records and arrays. */
d9fc6069 6388
3e4093b6 6389 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6390 {
3e4093b6
RS
6391 error_init ("variable-sized object may not be initialized");
6392 return error_mark_node;
d9fc6069 6393 }
3e4093b6
RS
6394
6395 error_init ("invalid initializer");
6396 return error_mark_node;
d9fc6069 6397}
400fbf9f 6398\f
3e4093b6 6399/* Handle initializers that use braces. */
400fbf9f 6400
3e4093b6
RS
6401/* Type of object we are accumulating a constructor for.
6402 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6403static tree constructor_type;
400fbf9f 6404
3e4093b6
RS
6405/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6406 left to fill. */
6407static tree constructor_fields;
400fbf9f 6408
3e4093b6
RS
6409/* For an ARRAY_TYPE, this is the specified index
6410 at which to store the next element we get. */
6411static tree constructor_index;
400fbf9f 6412
3e4093b6
RS
6413/* For an ARRAY_TYPE, this is the maximum index. */
6414static tree constructor_max_index;
400fbf9f 6415
3e4093b6
RS
6416/* For a RECORD_TYPE, this is the first field not yet written out. */
6417static tree constructor_unfilled_fields;
400fbf9f 6418
3e4093b6
RS
6419/* For an ARRAY_TYPE, this is the index of the first element
6420 not yet written out. */
6421static tree constructor_unfilled_index;
895ea614 6422
3e4093b6
RS
6423/* In a RECORD_TYPE, the byte index of the next consecutive field.
6424 This is so we can generate gaps between fields, when appropriate. */
6425static tree constructor_bit_index;
10d5caec 6426
3e4093b6
RS
6427/* If we are saving up the elements rather than allocating them,
6428 this is the list of elements so far (in reverse order,
6429 most recent first). */
4038c495 6430static VEC(constructor_elt,gc) *constructor_elements;
ad47f1e5 6431
3e4093b6
RS
6432/* 1 if constructor should be incrementally stored into a constructor chain,
6433 0 if all the elements should be kept in AVL tree. */
6434static int constructor_incremental;
ad47f1e5 6435
3e4093b6
RS
6436/* 1 if so far this constructor's elements are all compile-time constants. */
6437static int constructor_constant;
ad47f1e5 6438
3e4093b6
RS
6439/* 1 if so far this constructor's elements are all valid address constants. */
6440static int constructor_simple;
ad47f1e5 6441
928c19bb
JM
6442/* 1 if this constructor has an element that cannot be part of a
6443 constant expression. */
6444static int constructor_nonconst;
6445
3e4093b6
RS
6446/* 1 if this constructor is erroneous so far. */
6447static int constructor_erroneous;
d45cf215 6448
3e4093b6
RS
6449/* Structure for managing pending initializer elements, organized as an
6450 AVL tree. */
d45cf215 6451
3e4093b6 6452struct init_node
d45cf215 6453{
3e4093b6
RS
6454 struct init_node *left, *right;
6455 struct init_node *parent;
6456 int balance;
6457 tree purpose;
6458 tree value;
bbbbb16a 6459 tree origtype;
d45cf215
RS
6460};
6461
3e4093b6
RS
6462/* Tree of pending elements at this constructor level.
6463 These are elements encountered out of order
6464 which belong at places we haven't reached yet in actually
6465 writing the output.
6466 Will never hold tree nodes across GC runs. */
6467static struct init_node *constructor_pending_elts;
d45cf215 6468
3e4093b6
RS
6469/* The SPELLING_DEPTH of this constructor. */
6470static int constructor_depth;
d45cf215 6471
3e4093b6
RS
6472/* DECL node for which an initializer is being read.
6473 0 means we are reading a constructor expression
6474 such as (struct foo) {...}. */
6475static tree constructor_decl;
d45cf215 6476
3e4093b6
RS
6477/* Nonzero if this is an initializer for a top-level decl. */
6478static int constructor_top_level;
d45cf215 6479
3e4093b6
RS
6480/* Nonzero if there were any member designators in this initializer. */
6481static int constructor_designated;
d45cf215 6482
3e4093b6
RS
6483/* Nesting depth of designator list. */
6484static int designator_depth;
d45cf215 6485
3e4093b6 6486/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6487static int designator_erroneous;
d45cf215 6488
3e4093b6
RS
6489\f
6490/* This stack has a level for each implicit or explicit level of
6491 structuring in the initializer, including the outermost one. It
6492 saves the values of most of the variables above. */
d45cf215 6493
3e4093b6
RS
6494struct constructor_range_stack;
6495
6496struct constructor_stack
d45cf215 6497{
3e4093b6
RS
6498 struct constructor_stack *next;
6499 tree type;
6500 tree fields;
6501 tree index;
6502 tree max_index;
6503 tree unfilled_index;
6504 tree unfilled_fields;
6505 tree bit_index;
4038c495 6506 VEC(constructor_elt,gc) *elements;
3e4093b6
RS
6507 struct init_node *pending_elts;
6508 int offset;
6509 int depth;
916c5919 6510 /* If value nonzero, this value should replace the entire
3e4093b6 6511 constructor at this level. */
916c5919 6512 struct c_expr replacement_value;
3e4093b6
RS
6513 struct constructor_range_stack *range_stack;
6514 char constant;
6515 char simple;
928c19bb 6516 char nonconst;
3e4093b6
RS
6517 char implicit;
6518 char erroneous;
6519 char outer;
6520 char incremental;
6521 char designated;
6522};
d45cf215 6523
802415d1 6524static struct constructor_stack *constructor_stack;
d45cf215 6525
3e4093b6
RS
6526/* This stack represents designators from some range designator up to
6527 the last designator in the list. */
d45cf215 6528
3e4093b6
RS
6529struct constructor_range_stack
6530{
6531 struct constructor_range_stack *next, *prev;
6532 struct constructor_stack *stack;
6533 tree range_start;
6534 tree index;
6535 tree range_end;
6536 tree fields;
6537};
d45cf215 6538
802415d1 6539static struct constructor_range_stack *constructor_range_stack;
d45cf215 6540
3e4093b6
RS
6541/* This stack records separate initializers that are nested.
6542 Nested initializers can't happen in ANSI C, but GNU C allows them
6543 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6544
3e4093b6 6545struct initializer_stack
d45cf215 6546{
3e4093b6
RS
6547 struct initializer_stack *next;
6548 tree decl;
3e4093b6
RS
6549 struct constructor_stack *constructor_stack;
6550 struct constructor_range_stack *constructor_range_stack;
4038c495 6551 VEC(constructor_elt,gc) *elements;
3e4093b6
RS
6552 struct spelling *spelling;
6553 struct spelling *spelling_base;
6554 int spelling_size;
6555 char top_level;
6556 char require_constant_value;
6557 char require_constant_elements;
6558};
d45cf215 6559
802415d1 6560static struct initializer_stack *initializer_stack;
3e4093b6
RS
6561\f
6562/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6563
6564void
a396f8ae 6565start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6566{
3e4093b6 6567 const char *locus;
cceb1885 6568 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6569
3e4093b6 6570 p->decl = constructor_decl;
3e4093b6
RS
6571 p->require_constant_value = require_constant_value;
6572 p->require_constant_elements = require_constant_elements;
6573 p->constructor_stack = constructor_stack;
6574 p->constructor_range_stack = constructor_range_stack;
6575 p->elements = constructor_elements;
6576 p->spelling = spelling;
6577 p->spelling_base = spelling_base;
6578 p->spelling_size = spelling_size;
6579 p->top_level = constructor_top_level;
6580 p->next = initializer_stack;
6581 initializer_stack = p;
400fbf9f 6582
3e4093b6 6583 constructor_decl = decl;
3e4093b6
RS
6584 constructor_designated = 0;
6585 constructor_top_level = top_level;
400fbf9f 6586
6f17bbcf 6587 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6588 {
6589 require_constant_value = TREE_STATIC (decl);
6590 require_constant_elements
6591 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6592 /* For a scalar, you can always use any value to initialize,
6593 even within braces. */
6594 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6595 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6596 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6597 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6598 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6599 }
6600 else
6601 {
6602 require_constant_value = 0;
6603 require_constant_elements = 0;
88388a52 6604 locus = _("(anonymous)");
3e4093b6 6605 }
b71c7f8a 6606
3e4093b6
RS
6607 constructor_stack = 0;
6608 constructor_range_stack = 0;
b71c7f8a 6609
3e4093b6
RS
6610 missing_braces_mentioned = 0;
6611
6612 spelling_base = 0;
6613 spelling_size = 0;
6614 RESTORE_SPELLING_DEPTH (0);
6615
6616 if (locus)
6617 push_string (locus);
6618}
6619
6620void
6621finish_init (void)
b71c7f8a 6622{
3e4093b6 6623 struct initializer_stack *p = initializer_stack;
b71c7f8a 6624
3e4093b6
RS
6625 /* Free the whole constructor stack of this initializer. */
6626 while (constructor_stack)
6627 {
6628 struct constructor_stack *q = constructor_stack;
6629 constructor_stack = q->next;
6630 free (q);
6631 }
6632
366de0ce 6633 gcc_assert (!constructor_range_stack);
3e4093b6
RS
6634
6635 /* Pop back to the data of the outer initializer (if any). */
36579663 6636 free (spelling_base);
3aeb3655 6637
3e4093b6 6638 constructor_decl = p->decl;
3e4093b6
RS
6639 require_constant_value = p->require_constant_value;
6640 require_constant_elements = p->require_constant_elements;
6641 constructor_stack = p->constructor_stack;
6642 constructor_range_stack = p->constructor_range_stack;
6643 constructor_elements = p->elements;
6644 spelling = p->spelling;
6645 spelling_base = p->spelling_base;
6646 spelling_size = p->spelling_size;
6647 constructor_top_level = p->top_level;
6648 initializer_stack = p->next;
6649 free (p);
b71c7f8a 6650}
400fbf9f 6651\f
3e4093b6
RS
6652/* Call here when we see the initializer is surrounded by braces.
6653 This is instead of a call to push_init_level;
6654 it is matched by a call to pop_init_level.
400fbf9f 6655
3e4093b6
RS
6656 TYPE is the type to initialize, for a constructor expression.
6657 For an initializer for a decl, TYPE is zero. */
400fbf9f 6658
3e4093b6
RS
6659void
6660really_start_incremental_init (tree type)
400fbf9f 6661{
5d038c4c 6662 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 6663
3e4093b6
RS
6664 if (type == 0)
6665 type = TREE_TYPE (constructor_decl);
400fbf9f 6666
b6fc2cdb
PB
6667 if (TREE_CODE (type) == VECTOR_TYPE
6668 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 6669 error ("opaque vector types cannot be initialized");
400fbf9f 6670
3e4093b6
RS
6671 p->type = constructor_type;
6672 p->fields = constructor_fields;
6673 p->index = constructor_index;
6674 p->max_index = constructor_max_index;
6675 p->unfilled_index = constructor_unfilled_index;
6676 p->unfilled_fields = constructor_unfilled_fields;
6677 p->bit_index = constructor_bit_index;
6678 p->elements = constructor_elements;
6679 p->constant = constructor_constant;
6680 p->simple = constructor_simple;
928c19bb 6681 p->nonconst = constructor_nonconst;
3e4093b6
RS
6682 p->erroneous = constructor_erroneous;
6683 p->pending_elts = constructor_pending_elts;
6684 p->depth = constructor_depth;
916c5919
JM
6685 p->replacement_value.value = 0;
6686 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 6687 p->replacement_value.original_type = NULL;
3e4093b6
RS
6688 p->implicit = 0;
6689 p->range_stack = 0;
6690 p->outer = 0;
6691 p->incremental = constructor_incremental;
6692 p->designated = constructor_designated;
6693 p->next = 0;
6694 constructor_stack = p;
b13aca19 6695
3e4093b6
RS
6696 constructor_constant = 1;
6697 constructor_simple = 1;
928c19bb 6698 constructor_nonconst = 0;
3e4093b6
RS
6699 constructor_depth = SPELLING_DEPTH ();
6700 constructor_elements = 0;
6701 constructor_pending_elts = 0;
6702 constructor_type = type;
6703 constructor_incremental = 1;
6704 constructor_designated = 0;
6705 designator_depth = 0;
b06df647 6706 designator_erroneous = 0;
400fbf9f 6707
3e4093b6
RS
6708 if (TREE_CODE (constructor_type) == RECORD_TYPE
6709 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 6710 {
3e4093b6
RS
6711 constructor_fields = TYPE_FIELDS (constructor_type);
6712 /* Skip any nameless bit fields at the beginning. */
6713 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6714 && DECL_NAME (constructor_fields) == 0)
910ad8de 6715 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 6716
3e4093b6
RS
6717 constructor_unfilled_fields = constructor_fields;
6718 constructor_bit_index = bitsize_zero_node;
400fbf9f 6719 }
3e4093b6
RS
6720 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6721 {
6722 if (TYPE_DOMAIN (constructor_type))
6723 {
6724 constructor_max_index
6725 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 6726
3e4093b6
RS
6727 /* Detect non-empty initializations of zero-length arrays. */
6728 if (constructor_max_index == NULL_TREE
6729 && TYPE_SIZE (constructor_type))
9a9d280e 6730 constructor_max_index = integer_minus_one_node;
400fbf9f 6731
3e4093b6
RS
6732 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6733 to initialize VLAs will cause a proper error; avoid tree
6734 checking errors as well by setting a safe value. */
6735 if (constructor_max_index
6736 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 6737 constructor_max_index = integer_minus_one_node;
59c83dbf 6738
3e4093b6
RS
6739 constructor_index
6740 = convert (bitsizetype,
6741 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 6742 }
3e4093b6 6743 else
493179da
JM
6744 {
6745 constructor_index = bitsize_zero_node;
6746 constructor_max_index = NULL_TREE;
6747 }
59c83dbf 6748
3e4093b6
RS
6749 constructor_unfilled_index = constructor_index;
6750 }
6751 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6752 {
6753 /* Vectors are like simple fixed-size arrays. */
6754 constructor_max_index =
c62c040f 6755 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 6756 constructor_index = bitsize_zero_node;
3e4093b6
RS
6757 constructor_unfilled_index = constructor_index;
6758 }
6759 else
6760 {
6761 /* Handle the case of int x = {5}; */
6762 constructor_fields = constructor_type;
6763 constructor_unfilled_fields = constructor_type;
6764 }
6765}
6766\f
6767/* Push down into a subobject, for initialization.
6768 If this is for an explicit set of braces, IMPLICIT is 0.
6769 If it is because the next element belongs at a lower level,
6770 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 6771
3e4093b6 6772void
a1e3b3d9 6773push_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
6774{
6775 struct constructor_stack *p;
6776 tree value = NULL_TREE;
400fbf9f 6777
3e4093b6 6778 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
6779 pop them now. If implicit == 1, this will have been done in
6780 process_init_element; do not repeat it here because in the case
6781 of excess initializers for an empty aggregate this leads to an
6782 infinite cycle of popping a level and immediately recreating
6783 it. */
6784 if (implicit != 1)
3e4093b6 6785 {
472d98b4
JM
6786 while (constructor_stack->implicit)
6787 {
6788 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6789 || TREE_CODE (constructor_type) == UNION_TYPE)
6790 && constructor_fields == 0)
a1e3b3d9
LB
6791 process_init_element (pop_init_level (1, braced_init_obstack),
6792 true, braced_init_obstack);
472d98b4
JM
6793 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
6794 && constructor_max_index
6795 && tree_int_cst_lt (constructor_max_index,
6796 constructor_index))
a1e3b3d9
LB
6797 process_init_element (pop_init_level (1, braced_init_obstack),
6798 true, braced_init_obstack);
472d98b4
JM
6799 else
6800 break;
6801 }
3e4093b6 6802 }
400fbf9f 6803
3e4093b6
RS
6804 /* Unless this is an explicit brace, we need to preserve previous
6805 content if any. */
6806 if (implicit)
6807 {
6808 if ((TREE_CODE (constructor_type) == RECORD_TYPE
6809 || TREE_CODE (constructor_type) == UNION_TYPE)
6810 && constructor_fields)
a1e3b3d9 6811 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 6812 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 6813 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
6814 }
6815
5d038c4c 6816 p = XNEW (struct constructor_stack);
3e4093b6
RS
6817 p->type = constructor_type;
6818 p->fields = constructor_fields;
6819 p->index = constructor_index;
6820 p->max_index = constructor_max_index;
6821 p->unfilled_index = constructor_unfilled_index;
6822 p->unfilled_fields = constructor_unfilled_fields;
6823 p->bit_index = constructor_bit_index;
6824 p->elements = constructor_elements;
6825 p->constant = constructor_constant;
6826 p->simple = constructor_simple;
928c19bb 6827 p->nonconst = constructor_nonconst;
3e4093b6
RS
6828 p->erroneous = constructor_erroneous;
6829 p->pending_elts = constructor_pending_elts;
6830 p->depth = constructor_depth;
916c5919
JM
6831 p->replacement_value.value = 0;
6832 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 6833 p->replacement_value.original_type = NULL;
3e4093b6
RS
6834 p->implicit = implicit;
6835 p->outer = 0;
6836 p->incremental = constructor_incremental;
6837 p->designated = constructor_designated;
6838 p->next = constructor_stack;
6839 p->range_stack = 0;
6840 constructor_stack = p;
400fbf9f 6841
3e4093b6
RS
6842 constructor_constant = 1;
6843 constructor_simple = 1;
928c19bb 6844 constructor_nonconst = 0;
3e4093b6
RS
6845 constructor_depth = SPELLING_DEPTH ();
6846 constructor_elements = 0;
6847 constructor_incremental = 1;
6848 constructor_designated = 0;
6849 constructor_pending_elts = 0;
6850 if (!implicit)
400fbf9f 6851 {
3e4093b6
RS
6852 p->range_stack = constructor_range_stack;
6853 constructor_range_stack = 0;
6854 designator_depth = 0;
b06df647 6855 designator_erroneous = 0;
3e4093b6 6856 }
400fbf9f 6857
3e4093b6
RS
6858 /* Don't die if an entire brace-pair level is superfluous
6859 in the containing level. */
6860 if (constructor_type == 0)
6861 ;
6862 else if (TREE_CODE (constructor_type) == RECORD_TYPE
6863 || TREE_CODE (constructor_type) == UNION_TYPE)
6864 {
6865 /* Don't die if there are extra init elts at the end. */
6866 if (constructor_fields == 0)
6867 constructor_type = 0;
6868 else
400fbf9f 6869 {
3e4093b6
RS
6870 constructor_type = TREE_TYPE (constructor_fields);
6871 push_member_name (constructor_fields);
6872 constructor_depth++;
400fbf9f 6873 }
3e4093b6
RS
6874 }
6875 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6876 {
6877 constructor_type = TREE_TYPE (constructor_type);
a0f0ab9f 6878 push_array_bounds (tree_low_cst (constructor_index, 1));
3e4093b6 6879 constructor_depth++;
400fbf9f
JW
6880 }
6881
3e4093b6 6882 if (constructor_type == 0)
400fbf9f 6883 {
3e4093b6
RS
6884 error_init ("extra brace group at end of initializer");
6885 constructor_fields = 0;
6886 constructor_unfilled_fields = 0;
6887 return;
400fbf9f
JW
6888 }
6889
3e4093b6
RS
6890 if (value && TREE_CODE (value) == CONSTRUCTOR)
6891 {
6892 constructor_constant = TREE_CONSTANT (value);
6893 constructor_simple = TREE_STATIC (value);
928c19bb 6894 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 6895 constructor_elements = CONSTRUCTOR_ELTS (value);
4038c495 6896 if (!VEC_empty (constructor_elt, constructor_elements)
3e4093b6
RS
6897 && (TREE_CODE (constructor_type) == RECORD_TYPE
6898 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 6899 set_nonincremental_init (braced_init_obstack);
3e4093b6 6900 }
400fbf9f 6901
3e4093b6
RS
6902 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
6903 {
6904 missing_braces_mentioned = 1;
683d6ff9 6905 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
3e4093b6 6906 }
400fbf9f 6907
3e4093b6
RS
6908 if (TREE_CODE (constructor_type) == RECORD_TYPE
6909 || TREE_CODE (constructor_type) == UNION_TYPE)
6910 {
6911 constructor_fields = TYPE_FIELDS (constructor_type);
6912 /* Skip any nameless bit fields at the beginning. */
6913 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
6914 && DECL_NAME (constructor_fields) == 0)
f7587ed0 6915 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 6916
3e4093b6
RS
6917 constructor_unfilled_fields = constructor_fields;
6918 constructor_bit_index = bitsize_zero_node;
6919 }
6920 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
6921 {
6922 /* Vectors are like simple fixed-size arrays. */
6923 constructor_max_index =
c62c040f
RG
6924 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
6925 constructor_index = bitsize_int (0);
3e4093b6
RS
6926 constructor_unfilled_index = constructor_index;
6927 }
6928 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
6929 {
6930 if (TYPE_DOMAIN (constructor_type))
6931 {
6932 constructor_max_index
6933 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 6934
3e4093b6
RS
6935 /* Detect non-empty initializations of zero-length arrays. */
6936 if (constructor_max_index == NULL_TREE
6937 && TYPE_SIZE (constructor_type))
9a9d280e 6938 constructor_max_index = integer_minus_one_node;
de520661 6939
3e4093b6
RS
6940 /* constructor_max_index needs to be an INTEGER_CST. Attempts
6941 to initialize VLAs will cause a proper error; avoid tree
6942 checking errors as well by setting a safe value. */
6943 if (constructor_max_index
6944 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 6945 constructor_max_index = integer_minus_one_node;
b62acd60 6946
3e4093b6
RS
6947 constructor_index
6948 = convert (bitsizetype,
6949 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
6950 }
6951 else
6952 constructor_index = bitsize_zero_node;
de520661 6953
3e4093b6
RS
6954 constructor_unfilled_index = constructor_index;
6955 if (value && TREE_CODE (value) == STRING_CST)
6956 {
6957 /* We need to split the char/wchar array into individual
6958 characters, so that we don't have to special case it
6959 everywhere. */
a1e3b3d9 6960 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
6961 }
6962 }
6963 else
6964 {
b4519d39 6965 if (constructor_type != error_mark_node)
683d6ff9 6966 warning_init (0, "braces around scalar initializer");
3e4093b6
RS
6967 constructor_fields = constructor_type;
6968 constructor_unfilled_fields = constructor_type;
6969 }
6970}
8b6a5902 6971
3e4093b6 6972/* At the end of an implicit or explicit brace level,
916c5919
JM
6973 finish up that level of constructor. If a single expression
6974 with redundant braces initialized that level, return the
6975 c_expr structure for that expression. Otherwise, the original_code
6976 element is set to ERROR_MARK.
6977 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 6978 from inner levels (process_init_element ignores that),
916c5919 6979 but return error_mark_node as the value from the outermost level
3e4093b6 6980 (that's what we want to put in DECL_INITIAL).
916c5919 6981 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 6982
916c5919 6983struct c_expr
a1e3b3d9 6984pop_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
6985{
6986 struct constructor_stack *p;
916c5919
JM
6987 struct c_expr ret;
6988 ret.value = 0;
6989 ret.original_code = ERROR_MARK;
6866c6e8 6990 ret.original_type = NULL;
de520661 6991
3e4093b6
RS
6992 if (implicit == 0)
6993 {
6994 /* When we come to an explicit close brace,
6995 pop any inner levels that didn't have explicit braces. */
6996 while (constructor_stack->implicit)
a1e3b3d9
LB
6997 {
6998 process_init_element (pop_init_level (1, braced_init_obstack),
6999 true, braced_init_obstack);
7000 }
366de0ce 7001 gcc_assert (!constructor_range_stack);
3e4093b6 7002 }
e5e809f4 7003
0066ef9c
RH
7004 /* Now output all pending elements. */
7005 constructor_incremental = 1;
a1e3b3d9 7006 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7007
3e4093b6 7008 p = constructor_stack;
e5e809f4 7009
3e4093b6
RS
7010 /* Error for initializing a flexible array member, or a zero-length
7011 array member in an inappropriate context. */
7012 if (constructor_type && constructor_fields
7013 && TREE_CODE (constructor_type) == ARRAY_TYPE
7014 && TYPE_DOMAIN (constructor_type)
3f75a254 7015 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7016 {
7017 /* Silently discard empty initializations. The parser will
7018 already have pedwarned for empty brackets. */
7019 if (integer_zerop (constructor_unfilled_index))
7020 constructor_type = NULL_TREE;
366de0ce 7021 else
3e4093b6 7022 {
366de0ce 7023 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7024
3e4093b6
RS
7025 if (constructor_depth > 2)
7026 error_init ("initialization of flexible array member in a nested context");
fcf73884 7027 else
c1771a20 7028 pedwarn_init (input_location, OPT_Wpedantic,
509c9d60 7029 "initialization of a flexible array member");
de520661 7030
3e4093b6
RS
7031 /* We have already issued an error message for the existence
7032 of a flexible array member not at the end of the structure.
535a42b1 7033 Discard the initializer so that we do not die later. */
910ad8de 7034 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7035 constructor_type = NULL_TREE;
7036 }
3e4093b6 7037 }
de520661 7038
3e4093b6 7039 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7040 if (warn_missing_field_initializers
3e4093b6
RS
7041 && constructor_type
7042 && TREE_CODE (constructor_type) == RECORD_TYPE
7043 && constructor_unfilled_fields)
7044 {
49819fef
AM
7045 bool constructor_zeroinit =
7046 (VEC_length (constructor_elt, constructor_elements) == 1
7047 && integer_zerop
7048 (VEC_index (constructor_elt, constructor_elements, 0)->value));
7049
3e4093b6
RS
7050 /* Do not warn for flexible array members or zero-length arrays. */
7051 while (constructor_unfilled_fields
3f75a254 7052 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7053 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7054 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7055
49819fef
AM
7056 if (constructor_unfilled_fields
7057 /* Do not warn if this level of the initializer uses member
7058 designators; it is likely to be deliberate. */
7059 && !constructor_designated
7060 /* Do not warn about initializing with ` = {0}'. */
7061 && !constructor_zeroinit)
3e4093b6 7062 {
32397f22
MLI
7063 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7064 "missing initializer for field %qD of %qT",
7065 constructor_unfilled_fields,
7066 constructor_type))
7067 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7068 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7069 }
7070 }
de520661 7071
3e4093b6 7072 /* Pad out the end of the structure. */
916c5919 7073 if (p->replacement_value.value)
3e4093b6
RS
7074 /* If this closes a superfluous brace pair,
7075 just pass out the element between them. */
916c5919 7076 ret = p->replacement_value;
3e4093b6
RS
7077 else if (constructor_type == 0)
7078 ;
7079 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7080 && TREE_CODE (constructor_type) != UNION_TYPE
7081 && TREE_CODE (constructor_type) != ARRAY_TYPE
7082 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7083 {
7084 /* A nonincremental scalar initializer--just return
7085 the element, after verifying there is just one. */
4038c495 7086 if (VEC_empty (constructor_elt,constructor_elements))
3e4093b6
RS
7087 {
7088 if (!constructor_erroneous)
7089 error_init ("empty scalar initializer");
916c5919 7090 ret.value = error_mark_node;
3e4093b6 7091 }
4038c495 7092 else if (VEC_length (constructor_elt,constructor_elements) != 1)
3e4093b6
RS
7093 {
7094 error_init ("extra elements in scalar initializer");
4038c495 7095 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
3e4093b6
RS
7096 }
7097 else
4038c495 7098 ret.value = VEC_index (constructor_elt,constructor_elements,0)->value;
3e4093b6
RS
7099 }
7100 else
7101 {
7102 if (constructor_erroneous)
916c5919 7103 ret.value = error_mark_node;
3e4093b6
RS
7104 else
7105 {
916c5919 7106 ret.value = build_constructor (constructor_type,
4038c495 7107 constructor_elements);
3e4093b6 7108 if (constructor_constant)
51eed280 7109 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7110 if (constructor_constant && constructor_simple)
916c5919 7111 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7112 if (constructor_nonconst)
7113 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7114 }
7115 }
de520661 7116
928c19bb
JM
7117 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7118 {
7119 if (constructor_nonconst)
7120 ret.original_code = C_MAYBE_CONST_EXPR;
7121 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7122 ret.original_code = ERROR_MARK;
7123 }
7124
3e4093b6
RS
7125 constructor_type = p->type;
7126 constructor_fields = p->fields;
7127 constructor_index = p->index;
7128 constructor_max_index = p->max_index;
7129 constructor_unfilled_index = p->unfilled_index;
7130 constructor_unfilled_fields = p->unfilled_fields;
7131 constructor_bit_index = p->bit_index;
7132 constructor_elements = p->elements;
7133 constructor_constant = p->constant;
7134 constructor_simple = p->simple;
928c19bb 7135 constructor_nonconst = p->nonconst;
3e4093b6
RS
7136 constructor_erroneous = p->erroneous;
7137 constructor_incremental = p->incremental;
7138 constructor_designated = p->designated;
7139 constructor_pending_elts = p->pending_elts;
7140 constructor_depth = p->depth;
7141 if (!p->implicit)
7142 constructor_range_stack = p->range_stack;
7143 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7144
3e4093b6
RS
7145 constructor_stack = p->next;
7146 free (p);
b621a4dd 7147
5d5e98dc
VR
7148 if (ret.value == 0 && constructor_stack == 0)
7149 ret.value = error_mark_node;
916c5919 7150 return ret;
3e4093b6 7151}
8b6a5902 7152
3e4093b6
RS
7153/* Common handling for both array range and field name designators.
7154 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7155
3e4093b6 7156static int
a1e3b3d9 7157set_designator (int array, struct obstack * braced_init_obstack)
de520661 7158{
3e4093b6
RS
7159 tree subtype;
7160 enum tree_code subcode;
de520661 7161
3e4093b6
RS
7162 /* Don't die if an entire brace-pair level is superfluous
7163 in the containing level. */
7164 if (constructor_type == 0)
7165 return 1;
de520661 7166
366de0ce
NS
7167 /* If there were errors in this designator list already, bail out
7168 silently. */
b06df647 7169 if (designator_erroneous)
3e4093b6 7170 return 1;
e28cae4f 7171
3e4093b6
RS
7172 if (!designator_depth)
7173 {
366de0ce 7174 gcc_assert (!constructor_range_stack);
de520661 7175
3e4093b6
RS
7176 /* Designator list starts at the level of closest explicit
7177 braces. */
7178 while (constructor_stack->implicit)
a1e3b3d9
LB
7179 {
7180 process_init_element (pop_init_level (1, braced_init_obstack),
7181 true, braced_init_obstack);
7182 }
3e4093b6
RS
7183 constructor_designated = 1;
7184 return 0;
7185 }
de520661 7186
366de0ce 7187 switch (TREE_CODE (constructor_type))
3c3fa147 7188 {
366de0ce
NS
7189 case RECORD_TYPE:
7190 case UNION_TYPE:
3e4093b6
RS
7191 subtype = TREE_TYPE (constructor_fields);
7192 if (subtype != error_mark_node)
7193 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7194 break;
7195 case ARRAY_TYPE:
3e4093b6 7196 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7197 break;
7198 default:
7199 gcc_unreachable ();
de520661 7200 }
400fbf9f 7201
3e4093b6
RS
7202 subcode = TREE_CODE (subtype);
7203 if (array && subcode != ARRAY_TYPE)
7204 {
7205 error_init ("array index in non-array initializer");
7206 return 1;
7207 }
7208 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7209 {
7210 error_init ("field name not in record or union initializer");
7211 return 1;
7212 }
d45cf215 7213
3e4093b6 7214 constructor_designated = 1;
a1e3b3d9 7215 push_init_level (2, braced_init_obstack);
3e4093b6 7216 return 0;
de520661 7217}
400fbf9f 7218
3e4093b6
RS
7219/* If there are range designators in designator list, push a new designator
7220 to constructor_range_stack. RANGE_END is end of such stack range or
7221 NULL_TREE if there is no range designator at this level. */
400fbf9f 7222
3e4093b6 7223static void
a1e3b3d9 7224push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7225{
7226 struct constructor_range_stack *p;
400fbf9f 7227
a1e3b3d9
LB
7228 p = (struct constructor_range_stack *)
7229 obstack_alloc (braced_init_obstack,
7230 sizeof (struct constructor_range_stack));
3e4093b6
RS
7231 p->prev = constructor_range_stack;
7232 p->next = 0;
7233 p->fields = constructor_fields;
7234 p->range_start = constructor_index;
7235 p->index = constructor_index;
7236 p->stack = constructor_stack;
7237 p->range_end = range_end;
8b6a5902 7238 if (constructor_range_stack)
3e4093b6
RS
7239 constructor_range_stack->next = p;
7240 constructor_range_stack = p;
de520661 7241}
400fbf9f 7242
3e4093b6
RS
7243/* Within an array initializer, specify the next index to be initialized.
7244 FIRST is that index. If LAST is nonzero, then initialize a range
7245 of indices, running from FIRST through LAST. */
5a7ec9d9 7246
de520661 7247void
a1e3b3d9
LB
7248set_init_index (tree first, tree last,
7249 struct obstack * braced_init_obstack)
de520661 7250{
a1e3b3d9 7251 if (set_designator (1, braced_init_obstack))
3e4093b6 7252 return;
de520661 7253
b06df647 7254 designator_erroneous = 1;
de520661 7255
3ea8cd06
JM
7256 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7257 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7258 {
7259 error_init ("array index in initializer not of integer type");
7260 return;
7261 }
7262
2b6da65c
JM
7263 if (TREE_CODE (first) != INTEGER_CST)
7264 {
7265 first = c_fully_fold (first, false, NULL);
7266 if (TREE_CODE (first) == INTEGER_CST)
c1771a20 7267 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7268 "array index in initializer is not "
7269 "an integer constant expression");
7270 }
7271
7272 if (last && TREE_CODE (last) != INTEGER_CST)
7273 {
7274 last = c_fully_fold (last, false, NULL);
7275 if (TREE_CODE (last) == INTEGER_CST)
c1771a20 7276 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7277 "array index in initializer is not "
7278 "an integer constant expression");
7279 }
7280
3e4093b6
RS
7281 if (TREE_CODE (first) != INTEGER_CST)
7282 error_init ("nonconstant array index in initializer");
7283 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7284 error_init ("nonconstant array index in initializer");
7285 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7286 error_init ("array index in non-array initializer");
622adc7e
MK
7287 else if (tree_int_cst_sgn (first) == -1)
7288 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
7289 else if (constructor_max_index
7290 && tree_int_cst_lt (constructor_max_index, first))
7291 error_init ("array index in initializer exceeds array bounds");
7292 else
de520661 7293 {
928c19bb
JM
7294 constant_expression_warning (first);
7295 if (last)
7296 constant_expression_warning (last);
3e4093b6 7297 constructor_index = convert (bitsizetype, first);
665f2503 7298
3e4093b6 7299 if (last)
2bede729 7300 {
3e4093b6
RS
7301 if (tree_int_cst_equal (first, last))
7302 last = 0;
7303 else if (tree_int_cst_lt (last, first))
7304 {
7305 error_init ("empty index range in initializer");
7306 last = 0;
7307 }
7308 else
7309 {
7310 last = convert (bitsizetype, last);
7311 if (constructor_max_index != 0
7312 && tree_int_cst_lt (constructor_max_index, last))
7313 {
7314 error_init ("array index range in initializer exceeds array bounds");
7315 last = 0;
7316 }
7317 }
2bede729 7318 }
fed3cef0 7319
3e4093b6 7320 designator_depth++;
b06df647 7321 designator_erroneous = 0;
3e4093b6 7322 if (constructor_range_stack || last)
a1e3b3d9 7323 push_range_stack (last, braced_init_obstack);
de520661 7324 }
de520661 7325}
3e4093b6
RS
7326
7327/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7328
de520661 7329void
a1e3b3d9 7330set_init_label (tree fieldname, struct obstack * braced_init_obstack)
de520661 7331{
0fb96aa4 7332 tree field;
94ba5069 7333
a1e3b3d9 7334 if (set_designator (0, braced_init_obstack))
3e4093b6
RS
7335 return;
7336
b06df647 7337 designator_erroneous = 1;
3e4093b6
RS
7338
7339 if (TREE_CODE (constructor_type) != RECORD_TYPE
7340 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7341 {
3e4093b6
RS
7342 error_init ("field name not in record or union initializer");
7343 return;
94ba5069
RS
7344 }
7345
0fb96aa4 7346 field = lookup_field (constructor_type, fieldname);
8b6a5902 7347
0fb96aa4 7348 if (field == 0)
c51a1ba9 7349 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7350 else
0fb96aa4
JM
7351 do
7352 {
7353 constructor_fields = TREE_VALUE (field);
7354 designator_depth++;
7355 designator_erroneous = 0;
7356 if (constructor_range_stack)
7357 push_range_stack (NULL_TREE, braced_init_obstack);
7358 field = TREE_CHAIN (field);
7359 if (field)
7360 {
7361 if (set_designator (0, braced_init_obstack))
7362 return;
7363 }
7364 }
7365 while (field != NULL_TREE);
3e4093b6
RS
7366}
7367\f
7368/* Add a new initializer to the tree of pending initializers. PURPOSE
7369 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7370 VALUE is the value of that index or field. If ORIGTYPE is not
7371 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7372
7373 IMPLICIT is true if value comes from pop_init_level (1),
7374 the new initializer has been merged with the existing one
7375 and thus no warnings should be emitted about overriding an
7376 existing initializer. */
de520661 7377
3e4093b6 7378static void
a1e3b3d9
LB
7379add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7380 struct obstack * braced_init_obstack)
3e4093b6
RS
7381{
7382 struct init_node *p, **q, *r;
7383
7384 q = &constructor_pending_elts;
7385 p = 0;
7386
7387 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7388 {
3e4093b6 7389 while (*q != 0)
91fa3c30 7390 {
3e4093b6
RS
7391 p = *q;
7392 if (tree_int_cst_lt (purpose, p->purpose))
7393 q = &p->left;
7394 else if (tree_int_cst_lt (p->purpose, purpose))
7395 q = &p->right;
7396 else
7397 {
b295aee2
JJ
7398 if (!implicit)
7399 {
7400 if (TREE_SIDE_EFFECTS (p->value))
7401 warning_init (0, "initialized field with side-effects overwritten");
7402 else if (warn_override_init)
7403 warning_init (OPT_Woverride_init, "initialized field overwritten");
7404 }
3e4093b6 7405 p->value = value;
bbbbb16a 7406 p->origtype = origtype;
3e4093b6
RS
7407 return;
7408 }
91fa3c30 7409 }
de520661 7410 }
3e4093b6 7411 else
de520661 7412 {
3e4093b6 7413 tree bitpos;
400fbf9f 7414
3e4093b6
RS
7415 bitpos = bit_position (purpose);
7416 while (*q != NULL)
7417 {
7418 p = *q;
7419 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7420 q = &p->left;
7421 else if (p->purpose != purpose)
7422 q = &p->right;
7423 else
7424 {
b295aee2
JJ
7425 if (!implicit)
7426 {
7427 if (TREE_SIDE_EFFECTS (p->value))
7428 warning_init (0, "initialized field with side-effects overwritten");
7429 else if (warn_override_init)
7430 warning_init (OPT_Woverride_init, "initialized field overwritten");
7431 }
3e4093b6 7432 p->value = value;
bbbbb16a 7433 p->origtype = origtype;
3e4093b6
RS
7434 return;
7435 }
7436 }
91fa3c30 7437 }
b71c7f8a 7438
a1e3b3d9
LB
7439 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7440 sizeof (struct init_node));
3e4093b6
RS
7441 r->purpose = purpose;
7442 r->value = value;
bbbbb16a 7443 r->origtype = origtype;
8b6a5902 7444
3e4093b6
RS
7445 *q = r;
7446 r->parent = p;
7447 r->left = 0;
7448 r->right = 0;
7449 r->balance = 0;
b71c7f8a 7450
3e4093b6 7451 while (p)
de520661 7452 {
3e4093b6 7453 struct init_node *s;
665f2503 7454
3e4093b6 7455 if (r == p->left)
2bede729 7456 {
3e4093b6
RS
7457 if (p->balance == 0)
7458 p->balance = -1;
7459 else if (p->balance < 0)
7460 {
7461 if (r->balance < 0)
7462 {
7463 /* L rotation. */
7464 p->left = r->right;
7465 if (p->left)
7466 p->left->parent = p;
7467 r->right = p;
e7b6a0ee 7468
3e4093b6
RS
7469 p->balance = 0;
7470 r->balance = 0;
39bc99c2 7471
3e4093b6
RS
7472 s = p->parent;
7473 p->parent = r;
7474 r->parent = s;
7475 if (s)
7476 {
7477 if (s->left == p)
7478 s->left = r;
7479 else
7480 s->right = r;
7481 }
7482 else
7483 constructor_pending_elts = r;
7484 }
7485 else
7486 {
7487 /* LR rotation. */
7488 struct init_node *t = r->right;
e7b6a0ee 7489
3e4093b6
RS
7490 r->right = t->left;
7491 if (r->right)
7492 r->right->parent = r;
7493 t->left = r;
7494
7495 p->left = t->right;
7496 if (p->left)
7497 p->left->parent = p;
7498 t->right = p;
7499
7500 p->balance = t->balance < 0;
7501 r->balance = -(t->balance > 0);
7502 t->balance = 0;
7503
7504 s = p->parent;
7505 p->parent = t;
7506 r->parent = t;
7507 t->parent = s;
7508 if (s)
7509 {
7510 if (s->left == p)
7511 s->left = t;
7512 else
7513 s->right = t;
7514 }
7515 else
7516 constructor_pending_elts = t;
7517 }
7518 break;
7519 }
7520 else
7521 {
7522 /* p->balance == +1; growth of left side balances the node. */
7523 p->balance = 0;
7524 break;
7525 }
2bede729 7526 }
3e4093b6
RS
7527 else /* r == p->right */
7528 {
7529 if (p->balance == 0)
7530 /* Growth propagation from right side. */
7531 p->balance++;
7532 else if (p->balance > 0)
7533 {
7534 if (r->balance > 0)
7535 {
7536 /* R rotation. */
7537 p->right = r->left;
7538 if (p->right)
7539 p->right->parent = p;
7540 r->left = p;
7541
7542 p->balance = 0;
7543 r->balance = 0;
7544
7545 s = p->parent;
7546 p->parent = r;
7547 r->parent = s;
7548 if (s)
7549 {
7550 if (s->left == p)
7551 s->left = r;
7552 else
7553 s->right = r;
7554 }
7555 else
7556 constructor_pending_elts = r;
7557 }
7558 else /* r->balance == -1 */
7559 {
7560 /* RL rotation */
7561 struct init_node *t = r->left;
7562
7563 r->left = t->right;
7564 if (r->left)
7565 r->left->parent = r;
7566 t->right = r;
7567
7568 p->right = t->left;
7569 if (p->right)
7570 p->right->parent = p;
7571 t->left = p;
7572
7573 r->balance = (t->balance < 0);
7574 p->balance = -(t->balance > 0);
7575 t->balance = 0;
7576
7577 s = p->parent;
7578 p->parent = t;
7579 r->parent = t;
7580 t->parent = s;
7581 if (s)
7582 {
7583 if (s->left == p)
7584 s->left = t;
7585 else
7586 s->right = t;
7587 }
7588 else
7589 constructor_pending_elts = t;
7590 }
7591 break;
7592 }
7593 else
7594 {
7595 /* p->balance == -1; growth of right side balances the node. */
7596 p->balance = 0;
7597 break;
7598 }
7599 }
7600
7601 r = p;
7602 p = p->parent;
7603 }
7604}
7605
7606/* Build AVL tree from a sorted chain. */
7607
7608static void
a1e3b3d9 7609set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 7610{
4038c495
GB
7611 unsigned HOST_WIDE_INT ix;
7612 tree index, value;
3e4093b6
RS
7613
7614 if (TREE_CODE (constructor_type) != RECORD_TYPE
7615 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7616 return;
7617
4038c495 7618 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
a1e3b3d9 7619 {
fdce1719 7620 add_pending_init (index, value, NULL_TREE, true,
a1e3b3d9
LB
7621 braced_init_obstack);
7622 }
3e4093b6
RS
7623 constructor_elements = 0;
7624 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7625 {
7626 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7627 /* Skip any nameless bit fields at the beginning. */
7628 while (constructor_unfilled_fields != 0
7629 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7630 && DECL_NAME (constructor_unfilled_fields) == 0)
7631 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 7632
de520661 7633 }
3e4093b6 7634 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7635 {
3e4093b6
RS
7636 if (TYPE_DOMAIN (constructor_type))
7637 constructor_unfilled_index
7638 = convert (bitsizetype,
7639 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7640 else
7641 constructor_unfilled_index = bitsize_zero_node;
de520661 7642 }
3e4093b6 7643 constructor_incremental = 0;
de520661 7644}
400fbf9f 7645
3e4093b6 7646/* Build AVL tree from a string constant. */
de520661 7647
3e4093b6 7648static void
a1e3b3d9
LB
7649set_nonincremental_init_from_string (tree str,
7650 struct obstack * braced_init_obstack)
de520661 7651{
3e4093b6
RS
7652 tree value, purpose, type;
7653 HOST_WIDE_INT val[2];
7654 const char *p, *end;
7655 int byte, wchar_bytes, charwidth, bitpos;
de520661 7656
366de0ce 7657 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 7658
c466b2cd 7659 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
7660 charwidth = TYPE_PRECISION (char_type_node);
7661 type = TREE_TYPE (constructor_type);
7662 p = TREE_STRING_POINTER (str);
7663 end = p + TREE_STRING_LENGTH (str);
91fa3c30 7664
3e4093b6
RS
7665 for (purpose = bitsize_zero_node;
7666 p < end && !tree_int_cst_lt (constructor_max_index, purpose);
7667 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 7668 {
3e4093b6 7669 if (wchar_bytes == 1)
ffc5c6a9 7670 {
3e4093b6
RS
7671 val[1] = (unsigned char) *p++;
7672 val[0] = 0;
ffc5c6a9
RH
7673 }
7674 else
3e4093b6
RS
7675 {
7676 val[0] = 0;
7677 val[1] = 0;
7678 for (byte = 0; byte < wchar_bytes; byte++)
7679 {
7680 if (BYTES_BIG_ENDIAN)
7681 bitpos = (wchar_bytes - byte - 1) * charwidth;
7682 else
7683 bitpos = byte * charwidth;
7684 val[bitpos < HOST_BITS_PER_WIDE_INT]
7685 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
7686 << (bitpos % HOST_BITS_PER_WIDE_INT);
7687 }
7688 }
584ef5fe 7689
8df83eae 7690 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
7691 {
7692 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
7693 if (bitpos < HOST_BITS_PER_WIDE_INT)
7694 {
7695 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
7696 {
7697 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
7698 val[0] = -1;
7699 }
7700 }
7701 else if (bitpos == HOST_BITS_PER_WIDE_INT)
7702 {
7703 if (val[1] < 0)
c22cacf3 7704 val[0] = -1;
3e4093b6
RS
7705 }
7706 else if (val[0] & (((HOST_WIDE_INT) 1)
7707 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
7708 val[0] |= ((HOST_WIDE_INT) -1)
7709 << (bitpos - HOST_BITS_PER_WIDE_INT);
7710 }
ffc5c6a9 7711
7d60be94 7712 value = build_int_cst_wide (type, val[1], val[0]);
fdce1719 7713 add_pending_init (purpose, value, NULL_TREE, true,
a1e3b3d9 7714 braced_init_obstack);
9dfcc8db
BH
7715 }
7716
3e4093b6
RS
7717 constructor_incremental = 0;
7718}
de520661 7719
3e4093b6
RS
7720/* Return value of FIELD in pending initializer or zero if the field was
7721 not initialized yet. */
7722
7723static tree
a1e3b3d9 7724find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
7725{
7726 struct init_node *p;
7727
7728 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 7729 {
3e4093b6
RS
7730 if (constructor_incremental
7731 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 7732 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
7733
7734 p = constructor_pending_elts;
7735 while (p)
19d76e60 7736 {
3e4093b6
RS
7737 if (tree_int_cst_lt (field, p->purpose))
7738 p = p->left;
7739 else if (tree_int_cst_lt (p->purpose, field))
7740 p = p->right;
7741 else
7742 return p->value;
19d76e60 7743 }
19d76e60 7744 }
3e4093b6 7745 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 7746 {
3e4093b6 7747 tree bitpos = bit_position (field);
de520661 7748
3e4093b6
RS
7749 if (constructor_incremental
7750 && (!constructor_unfilled_fields
7751 || tree_int_cst_lt (bitpos,
7752 bit_position (constructor_unfilled_fields))))
a1e3b3d9 7753 set_nonincremental_init (braced_init_obstack);
de520661 7754
3e4093b6
RS
7755 p = constructor_pending_elts;
7756 while (p)
7757 {
7758 if (field == p->purpose)
7759 return p->value;
7760 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7761 p = p->left;
7762 else
7763 p = p->right;
7764 }
7765 }
7766 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 7767 {
4038c495
GB
7768 if (!VEC_empty (constructor_elt, constructor_elements)
7769 && (VEC_last (constructor_elt, constructor_elements)->index
7770 == field))
7771 return VEC_last (constructor_elt, constructor_elements)->value;
de520661 7772 }
3e4093b6 7773 return 0;
de520661
RS
7774}
7775
3e4093b6
RS
7776/* "Output" the next constructor element.
7777 At top level, really output it to assembler code now.
7778 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 7779 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
7780 TYPE is the data type that the containing data type wants here.
7781 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
7782 If VALUE is a string constant, STRICT_STRING is true if it is
7783 unparenthesized or we should not warn here for it being parenthesized.
7784 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 7785
3e4093b6
RS
7786 PENDING if non-nil means output pending elements that belong
7787 right after this element. (PENDING is normally 1;
b295aee2
JJ
7788 it is 0 while outputting pending elements, to avoid recursion.)
7789
7790 IMPLICIT is true if value comes from pop_init_level (1),
7791 the new initializer has been merged with the existing one
7792 and thus no warnings should be emitted about overriding an
7793 existing initializer. */
8b6a5902 7794
3e4093b6 7795static void
bbbbb16a 7796output_init_element (tree value, tree origtype, bool strict_string, tree type,
a1e3b3d9
LB
7797 tree field, int pending, bool implicit,
7798 struct obstack * braced_init_obstack)
3e4093b6 7799{
8ce94e44 7800 tree semantic_type = NULL_TREE;
4038c495 7801 constructor_elt *celt;
928c19bb
JM
7802 bool maybe_const = true;
7803 bool npc;
4038c495 7804
0a880880 7805 if (type == error_mark_node || value == error_mark_node)
8b6a5902 7806 {
3e4093b6
RS
7807 constructor_erroneous = 1;
7808 return;
8b6a5902 7809 }
46bdb9cf
JM
7810 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
7811 && (TREE_CODE (value) == STRING_CST
7812 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
7813 && !(TREE_CODE (value) == STRING_CST
7814 && TREE_CODE (type) == ARRAY_TYPE
7815 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
7816 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
7817 TYPE_MAIN_VARIANT (type)))
c2255bc4 7818 value = array_to_pointer_conversion (input_location, value);
8b6a5902 7819
3e4093b6
RS
7820 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
7821 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 7822 {
3e4093b6
RS
7823 /* As an extension, allow initializing objects with static storage
7824 duration with compound literals (which are then treated just as
7825 the brace enclosed list they contain). */
7826 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
7827 value = DECL_INITIAL (decl);
8b6a5902
JJ
7828 }
7829
928c19bb 7830 npc = null_pointer_constant_p (value);
8ce94e44
JM
7831 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
7832 {
7833 semantic_type = TREE_TYPE (value);
7834 value = TREE_OPERAND (value, 0);
7835 }
928c19bb
JM
7836 value = c_fully_fold (value, require_constant_value, &maybe_const);
7837
3e4093b6
RS
7838 if (value == error_mark_node)
7839 constructor_erroneous = 1;
7840 else if (!TREE_CONSTANT (value))
7841 constructor_constant = 0;
116df786 7842 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
7843 || ((TREE_CODE (constructor_type) == RECORD_TYPE
7844 || TREE_CODE (constructor_type) == UNION_TYPE)
7845 && DECL_C_BIT_FIELD (field)
7846 && TREE_CODE (value) != INTEGER_CST))
7847 constructor_simple = 0;
928c19bb
JM
7848 if (!maybe_const)
7849 constructor_nonconst = 1;
3e4093b6 7850
116df786 7851 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 7852 {
116df786
RH
7853 if (require_constant_value)
7854 {
7855 error_init ("initializer element is not constant");
7856 value = error_mark_node;
7857 }
7858 else if (require_constant_elements)
509c9d60
MLI
7859 pedwarn (input_location, 0,
7860 "initializer element is not computable at load time");
8b6a5902 7861 }
928c19bb
JM
7862 else if (!maybe_const
7863 && (require_constant_value || require_constant_elements))
7864 pedwarn_init (input_location, 0,
7865 "initializer element is not a constant expression");
3e4093b6 7866
81f40b79
ILT
7867 /* Issue -Wc++-compat warnings about initializing a bitfield with
7868 enum type. */
7869 if (warn_cxx_compat
7870 && field != NULL_TREE
7871 && TREE_CODE (field) == FIELD_DECL
7872 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
7873 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
7874 != TYPE_MAIN_VARIANT (type))
7875 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
7876 {
7877 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
7878 if (checktype != error_mark_node
7879 && (TYPE_MAIN_VARIANT (checktype)
7880 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
7881 warning_init (OPT_Wc___compat,
7882 "enum conversion in initialization is invalid in C++");
7883 }
7884
3e4093b6
RS
7885 /* If this field is empty (and not at the end of structure),
7886 don't do anything other than checking the initializer. */
7887 if (field
7888 && (TREE_TYPE (field) == error_mark_node
7889 || (COMPLETE_TYPE_P (TREE_TYPE (field))
7890 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
7891 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 7892 || DECL_CHAIN (field)))))
3e4093b6
RS
7893 return;
7894
8ce94e44
JM
7895 if (semantic_type)
7896 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
c2255bc4
AH
7897 value = digest_init (input_location, type, value, origtype, npc,
7898 strict_string, require_constant_value);
3e4093b6 7899 if (value == error_mark_node)
8b6a5902 7900 {
3e4093b6
RS
7901 constructor_erroneous = 1;
7902 return;
8b6a5902 7903 }
928c19bb
JM
7904 if (require_constant_value || require_constant_elements)
7905 constant_expression_warning (value);
8b6a5902 7906
3e4093b6
RS
7907 /* If this element doesn't come next in sequence,
7908 put it on constructor_pending_elts. */
7909 if (TREE_CODE (constructor_type) == ARRAY_TYPE
7910 && (!constructor_incremental
7911 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 7912 {
3e4093b6
RS
7913 if (constructor_incremental
7914 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 7915 set_nonincremental_init (braced_init_obstack);
3e4093b6 7916
a1e3b3d9
LB
7917 add_pending_init (field, value, origtype, implicit,
7918 braced_init_obstack);
3e4093b6 7919 return;
8b6a5902 7920 }
3e4093b6
RS
7921 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7922 && (!constructor_incremental
7923 || field != constructor_unfilled_fields))
8b6a5902 7924 {
3e4093b6
RS
7925 /* We do this for records but not for unions. In a union,
7926 no matter which field is specified, it can be initialized
7927 right away since it starts at the beginning of the union. */
7928 if (constructor_incremental)
7929 {
7930 if (!constructor_unfilled_fields)
a1e3b3d9 7931 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
7932 else
7933 {
7934 tree bitpos, unfillpos;
7935
7936 bitpos = bit_position (field);
7937 unfillpos = bit_position (constructor_unfilled_fields);
7938
7939 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 7940 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
7941 }
7942 }
7943
a1e3b3d9
LB
7944 add_pending_init (field, value, origtype, implicit,
7945 braced_init_obstack);
3e4093b6 7946 return;
8b6a5902 7947 }
3e4093b6 7948 else if (TREE_CODE (constructor_type) == UNION_TYPE
4038c495 7949 && !VEC_empty (constructor_elt, constructor_elements))
3e4093b6 7950 {
b295aee2
JJ
7951 if (!implicit)
7952 {
7953 if (TREE_SIDE_EFFECTS (VEC_last (constructor_elt,
7954 constructor_elements)->value))
7955 warning_init (0,
7956 "initialized field with side-effects overwritten");
7957 else if (warn_override_init)
7958 warning_init (OPT_Woverride_init, "initialized field overwritten");
7959 }
8b6a5902 7960
3e4093b6
RS
7961 /* We can have just one union field set. */
7962 constructor_elements = 0;
7963 }
8b6a5902 7964
3e4093b6
RS
7965 /* Otherwise, output this element either to
7966 constructor_elements or to the assembler file. */
8b6a5902 7967
4038c495
GB
7968 celt = VEC_safe_push (constructor_elt, gc, constructor_elements, NULL);
7969 celt->index = field;
7970 celt->value = value;
8b6a5902 7971
3e4093b6
RS
7972 /* Advance the variable that indicates sequential elements output. */
7973 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7974 constructor_unfilled_index
db3927fb
AH
7975 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
7976 bitsize_one_node);
3e4093b6
RS
7977 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
7978 {
7979 constructor_unfilled_fields
910ad8de 7980 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 7981
3e4093b6
RS
7982 /* Skip any nameless bit fields. */
7983 while (constructor_unfilled_fields != 0
7984 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
7985 && DECL_NAME (constructor_unfilled_fields) == 0)
7986 constructor_unfilled_fields =
910ad8de 7987 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
7988 }
7989 else if (TREE_CODE (constructor_type) == UNION_TYPE)
7990 constructor_unfilled_fields = 0;
de520661 7991
3e4093b6
RS
7992 /* Now output any pending elements which have become next. */
7993 if (pending)
a1e3b3d9 7994 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 7995}
8b6a5902 7996
3e4093b6
RS
7997/* Output any pending elements which have become next.
7998 As we output elements, constructor_unfilled_{fields,index}
7999 advances, which may cause other elements to become next;
8000 if so, they too are output.
8b6a5902 8001
3e4093b6
RS
8002 If ALL is 0, we return when there are
8003 no more pending elements to output now.
665f2503 8004
3e4093b6
RS
8005 If ALL is 1, we output space as necessary so that
8006 we can output all the pending elements. */
3e4093b6 8007static void
a1e3b3d9 8008output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8009{
8010 struct init_node *elt = constructor_pending_elts;
8011 tree next;
de520661 8012
3e4093b6
RS
8013 retry:
8014
ba228239 8015 /* Look through the whole pending tree.
3e4093b6
RS
8016 If we find an element that should be output now,
8017 output it. Otherwise, set NEXT to the element
8018 that comes first among those still pending. */
8019
8020 next = 0;
8021 while (elt)
8022 {
8023 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8024 {
3e4093b6
RS
8025 if (tree_int_cst_equal (elt->purpose,
8026 constructor_unfilled_index))
bbbbb16a 8027 output_init_element (elt->value, elt->origtype, true,
3e4093b6 8028 TREE_TYPE (constructor_type),
a1e3b3d9
LB
8029 constructor_unfilled_index, 0, false,
8030 braced_init_obstack);
3e4093b6
RS
8031 else if (tree_int_cst_lt (constructor_unfilled_index,
8032 elt->purpose))
8b6a5902 8033 {
3e4093b6
RS
8034 /* Advance to the next smaller node. */
8035 if (elt->left)
8036 elt = elt->left;
8037 else
8038 {
8039 /* We have reached the smallest node bigger than the
8040 current unfilled index. Fill the space first. */
8041 next = elt->purpose;
8042 break;
8043 }
8b6a5902 8044 }
ce662d4c
JJ
8045 else
8046 {
3e4093b6
RS
8047 /* Advance to the next bigger node. */
8048 if (elt->right)
8049 elt = elt->right;
8050 else
ce662d4c 8051 {
3e4093b6
RS
8052 /* We have reached the biggest node in a subtree. Find
8053 the parent of it, which is the next bigger node. */
8054 while (elt->parent && elt->parent->right == elt)
8055 elt = elt->parent;
8056 elt = elt->parent;
8057 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8058 elt->purpose))
8059 {
8060 next = elt->purpose;
8061 break;
8062 }
ce662d4c
JJ
8063 }
8064 }
8b6a5902 8065 }
3e4093b6
RS
8066 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8067 || TREE_CODE (constructor_type) == UNION_TYPE)
8068 {
8069 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8070
3e4093b6
RS
8071 /* If the current record is complete we are done. */
8072 if (constructor_unfilled_fields == 0)
8073 break;
de520661 8074
3e4093b6
RS
8075 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8076 elt_bitpos = bit_position (elt->purpose);
8077 /* We can't compare fields here because there might be empty
8078 fields in between. */
8079 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8080 {
8081 constructor_unfilled_fields = elt->purpose;
bbbbb16a
ILT
8082 output_init_element (elt->value, elt->origtype, true,
8083 TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8084 elt->purpose, 0, false,
8085 braced_init_obstack);
3e4093b6
RS
8086 }
8087 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8088 {
8089 /* Advance to the next smaller node. */
8090 if (elt->left)
8091 elt = elt->left;
8092 else
8093 {
8094 /* We have reached the smallest node bigger than the
8095 current unfilled field. Fill the space first. */
8096 next = elt->purpose;
8097 break;
8098 }
8099 }
8100 else
8101 {
8102 /* Advance to the next bigger node. */
8103 if (elt->right)
8104 elt = elt->right;
8105 else
8106 {
8107 /* We have reached the biggest node in a subtree. Find
8108 the parent of it, which is the next bigger node. */
8109 while (elt->parent && elt->parent->right == elt)
8110 elt = elt->parent;
8111 elt = elt->parent;
8112 if (elt
8113 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8114 bit_position (elt->purpose))))
8115 {
8116 next = elt->purpose;
8117 break;
8118 }
8119 }
8120 }
8121 }
8122 }
de520661 8123
3e4093b6
RS
8124 /* Ordinarily return, but not if we want to output all
8125 and there are elements left. */
3f75a254 8126 if (!(all && next != 0))
e5cfb88f
RK
8127 return;
8128
3e4093b6
RS
8129 /* If it's not incremental, just skip over the gap, so that after
8130 jumping to retry we will output the next successive element. */
8131 if (TREE_CODE (constructor_type) == RECORD_TYPE
8132 || TREE_CODE (constructor_type) == UNION_TYPE)
8133 constructor_unfilled_fields = next;
8134 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8135 constructor_unfilled_index = next;
de520661 8136
3e4093b6
RS
8137 /* ELT now points to the node in the pending tree with the next
8138 initializer to output. */
8139 goto retry;
de520661
RS
8140}
8141\f
3e4093b6
RS
8142/* Add one non-braced element to the current constructor level.
8143 This adjusts the current position within the constructor's type.
8144 This may also start or terminate implicit levels
8145 to handle a partly-braced initializer.
e5e809f4 8146
3e4093b6 8147 Once this has found the correct level for the new element,
b295aee2
JJ
8148 it calls output_init_element.
8149
8150 IMPLICIT is true if value comes from pop_init_level (1),
8151 the new initializer has been merged with the existing one
8152 and thus no warnings should be emitted about overriding an
8153 existing initializer. */
3e4093b6
RS
8154
8155void
a1e3b3d9
LB
8156process_init_element (struct c_expr value, bool implicit,
8157 struct obstack * braced_init_obstack)
e5e809f4 8158{
916c5919
JM
8159 tree orig_value = value.value;
8160 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8161 bool strict_string = value.original_code == STRING_CST;
e5e809f4 8162
3e4093b6 8163 designator_depth = 0;
b06df647 8164 designator_erroneous = 0;
e5e809f4 8165
3e4093b6
RS
8166 /* Handle superfluous braces around string cst as in
8167 char x[] = {"foo"}; */
8168 if (string_flag
8169 && constructor_type
8170 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8171 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8172 && integer_zerop (constructor_unfilled_index))
e5e809f4 8173 {
916c5919 8174 if (constructor_stack->replacement_value.value)
c22cacf3 8175 error_init ("excess elements in char array initializer");
3e4093b6
RS
8176 constructor_stack->replacement_value = value;
8177 return;
e5e809f4 8178 }
8b6a5902 8179
916c5919 8180 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
8181 {
8182 error_init ("excess elements in struct initializer");
8183 return;
e5e809f4
JL
8184 }
8185
3e4093b6
RS
8186 /* Ignore elements of a brace group if it is entirely superfluous
8187 and has already been diagnosed. */
8188 if (constructor_type == 0)
8189 return;
e5e809f4 8190
3e4093b6
RS
8191 /* If we've exhausted any levels that didn't have braces,
8192 pop them now. */
8193 while (constructor_stack->implicit)
8194 {
8195 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8196 || TREE_CODE (constructor_type) == UNION_TYPE)
8197 && constructor_fields == 0)
a1e3b3d9
LB
8198 process_init_element (pop_init_level (1, braced_init_obstack),
8199 true, braced_init_obstack);
53650abe
AP
8200 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8201 || TREE_CODE (constructor_type) == VECTOR_TYPE)
3e4093b6
RS
8202 && (constructor_max_index == 0
8203 || tree_int_cst_lt (constructor_max_index,
8204 constructor_index)))
a1e3b3d9
LB
8205 process_init_element (pop_init_level (1, braced_init_obstack),
8206 true, braced_init_obstack);
3e4093b6
RS
8207 else
8208 break;
8209 }
e5e809f4 8210
3e4093b6
RS
8211 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8212 if (constructor_range_stack)
e5e809f4 8213 {
3e4093b6
RS
8214 /* If value is a compound literal and we'll be just using its
8215 content, don't put it into a SAVE_EXPR. */
916c5919 8216 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8217 || !require_constant_value
8218 || flag_isoc99)
8ce94e44
JM
8219 {
8220 tree semantic_type = NULL_TREE;
8221 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8222 {
8223 semantic_type = TREE_TYPE (value.value);
8224 value.value = TREE_OPERAND (value.value, 0);
8225 }
8226 value.value = c_save_expr (value.value);
8227 if (semantic_type)
8228 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8229 value.value);
8230 }
3e4093b6 8231 }
e5e809f4 8232
3e4093b6
RS
8233 while (1)
8234 {
8235 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8236 {
3e4093b6
RS
8237 tree fieldtype;
8238 enum tree_code fieldcode;
e5e809f4 8239
3e4093b6
RS
8240 if (constructor_fields == 0)
8241 {
509c9d60
MLI
8242 pedwarn_init (input_location, 0,
8243 "excess elements in struct initializer");
3e4093b6
RS
8244 break;
8245 }
e5e809f4 8246
3e4093b6
RS
8247 fieldtype = TREE_TYPE (constructor_fields);
8248 if (fieldtype != error_mark_node)
8249 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8250 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8251
3e4093b6
RS
8252 /* Error for non-static initialization of a flexible array member. */
8253 if (fieldcode == ARRAY_TYPE
8254 && !require_constant_value
8255 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8256 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6
RS
8257 {
8258 error_init ("non-static initialization of a flexible array member");
8259 break;
8260 }
e5e809f4 8261
3e4093b6 8262 /* Accept a string constant to initialize a subarray. */
916c5919 8263 if (value.value != 0
3e4093b6 8264 && fieldcode == ARRAY_TYPE
197463ae 8265 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8266 && string_flag)
916c5919 8267 value.value = orig_value;
3e4093b6
RS
8268 /* Otherwise, if we have come to a subaggregate,
8269 and we don't have an element of its type, push into it. */
0953878d 8270 else if (value.value != 0
916c5919
JM
8271 && value.value != error_mark_node
8272 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8273 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8274 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8275 {
a1e3b3d9 8276 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8277 continue;
8278 }
e5e809f4 8279
916c5919 8280 if (value.value)
3e4093b6
RS
8281 {
8282 push_member_name (constructor_fields);
bbbbb16a
ILT
8283 output_init_element (value.value, value.original_type,
8284 strict_string, fieldtype,
a1e3b3d9
LB
8285 constructor_fields, 1, implicit,
8286 braced_init_obstack);
3e4093b6 8287 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8288 }
8289 else
3e4093b6
RS
8290 /* Do the bookkeeping for an element that was
8291 directly output as a constructor. */
e5e809f4 8292 {
3e4093b6
RS
8293 /* For a record, keep track of end position of last field. */
8294 if (DECL_SIZE (constructor_fields))
c22cacf3 8295 constructor_bit_index
db3927fb
AH
8296 = size_binop_loc (input_location, PLUS_EXPR,
8297 bit_position (constructor_fields),
8298 DECL_SIZE (constructor_fields));
3e4093b6
RS
8299
8300 /* If the current field was the first one not yet written out,
8301 it isn't now, so update. */
8302 if (constructor_unfilled_fields == constructor_fields)
8303 {
910ad8de 8304 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8305 /* Skip any nameless bit fields. */
8306 while (constructor_unfilled_fields != 0
8307 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8308 && DECL_NAME (constructor_unfilled_fields) == 0)
8309 constructor_unfilled_fields =
910ad8de 8310 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8311 }
e5e809f4 8312 }
3e4093b6 8313
910ad8de 8314 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8315 /* Skip any nameless bit fields at the beginning. */
8316 while (constructor_fields != 0
8317 && DECL_C_BIT_FIELD (constructor_fields)
8318 && DECL_NAME (constructor_fields) == 0)
910ad8de 8319 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8320 }
3e4093b6 8321 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8322 {
3e4093b6
RS
8323 tree fieldtype;
8324 enum tree_code fieldcode;
e5e809f4 8325
3e4093b6
RS
8326 if (constructor_fields == 0)
8327 {
509c9d60
MLI
8328 pedwarn_init (input_location, 0,
8329 "excess elements in union initializer");
3e4093b6
RS
8330 break;
8331 }
e5e809f4 8332
3e4093b6
RS
8333 fieldtype = TREE_TYPE (constructor_fields);
8334 if (fieldtype != error_mark_node)
8335 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8336 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8337
3e4093b6
RS
8338 /* Warn that traditional C rejects initialization of unions.
8339 We skip the warning if the value is zero. This is done
8340 under the assumption that the zero initializer in user
8341 code appears conditioned on e.g. __STDC__ to avoid
8342 "missing initializer" warnings and relies on default
8343 initialization to zero in the traditional C case.
8344 We also skip the warning if the initializer is designated,
8345 again on the assumption that this must be conditional on
8346 __STDC__ anyway (and we've already complained about the
8347 member-designator already). */
3176a0c2 8348 if (!in_system_header && !constructor_designated
916c5919
JM
8349 && !(value.value && (integer_zerop (value.value)
8350 || real_zerop (value.value))))
3176a0c2
DD
8351 warning (OPT_Wtraditional, "traditional C rejects initialization "
8352 "of unions");
e5e809f4 8353
3e4093b6 8354 /* Accept a string constant to initialize a subarray. */
916c5919 8355 if (value.value != 0
3e4093b6 8356 && fieldcode == ARRAY_TYPE
197463ae 8357 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8358 && string_flag)
916c5919 8359 value.value = orig_value;
3e4093b6
RS
8360 /* Otherwise, if we have come to a subaggregate,
8361 and we don't have an element of its type, push into it. */
0953878d 8362 else if (value.value != 0
916c5919
JM
8363 && value.value != error_mark_node
8364 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8365 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8366 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8367 {
a1e3b3d9 8368 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8369 continue;
8370 }
e5e809f4 8371
916c5919 8372 if (value.value)
3e4093b6
RS
8373 {
8374 push_member_name (constructor_fields);
bbbbb16a
ILT
8375 output_init_element (value.value, value.original_type,
8376 strict_string, fieldtype,
a1e3b3d9
LB
8377 constructor_fields, 1, implicit,
8378 braced_init_obstack);
3e4093b6 8379 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8380 }
8381 else
3e4093b6
RS
8382 /* Do the bookkeeping for an element that was
8383 directly output as a constructor. */
e5e809f4 8384 {
3e4093b6 8385 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8386 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8387 }
e5e809f4 8388
3e4093b6
RS
8389 constructor_fields = 0;
8390 }
8391 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8392 {
8393 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8394 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8395
3e4093b6 8396 /* Accept a string constant to initialize a subarray. */
916c5919 8397 if (value.value != 0
3e4093b6 8398 && eltcode == ARRAY_TYPE
197463ae 8399 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8400 && string_flag)
916c5919 8401 value.value = orig_value;
3e4093b6
RS
8402 /* Otherwise, if we have come to a subaggregate,
8403 and we don't have an element of its type, push into it. */
0953878d 8404 else if (value.value != 0
916c5919
JM
8405 && value.value != error_mark_node
8406 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8407 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8408 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8409 {
a1e3b3d9 8410 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8411 continue;
8412 }
8b6a5902 8413
3e4093b6
RS
8414 if (constructor_max_index != 0
8415 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8416 || integer_all_onesp (constructor_max_index)))
8417 {
509c9d60
MLI
8418 pedwarn_init (input_location, 0,
8419 "excess elements in array initializer");
3e4093b6
RS
8420 break;
8421 }
8b6a5902 8422
3e4093b6 8423 /* Now output the actual element. */
916c5919 8424 if (value.value)
3e4093b6 8425 {
a0f0ab9f 8426 push_array_bounds (tree_low_cst (constructor_index, 1));
bbbbb16a
ILT
8427 output_init_element (value.value, value.original_type,
8428 strict_string, elttype,
a1e3b3d9
LB
8429 constructor_index, 1, implicit,
8430 braced_init_obstack);
3e4093b6
RS
8431 RESTORE_SPELLING_DEPTH (constructor_depth);
8432 }
2f6e4e97 8433
3e4093b6 8434 constructor_index
db3927fb
AH
8435 = size_binop_loc (input_location, PLUS_EXPR,
8436 constructor_index, bitsize_one_node);
8b6a5902 8437
916c5919 8438 if (!value.value)
3e4093b6
RS
8439 /* If we are doing the bookkeeping for an element that was
8440 directly output as a constructor, we must update
8441 constructor_unfilled_index. */
8442 constructor_unfilled_index = constructor_index;
8443 }
8444 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8445 {
8446 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8447
c22cacf3
MS
8448 /* Do a basic check of initializer size. Note that vectors
8449 always have a fixed size derived from their type. */
3e4093b6
RS
8450 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8451 {
509c9d60
MLI
8452 pedwarn_init (input_location, 0,
8453 "excess elements in vector initializer");
3e4093b6
RS
8454 break;
8455 }
8b6a5902 8456
3e4093b6 8457 /* Now output the actual element. */
916c5919 8458 if (value.value)
53650abe
AP
8459 {
8460 if (TREE_CODE (value.value) == VECTOR_CST)
8461 elttype = TYPE_MAIN_VARIANT (constructor_type);
8462 output_init_element (value.value, value.original_type,
8463 strict_string, elttype,
a1e3b3d9
LB
8464 constructor_index, 1, implicit,
8465 braced_init_obstack);
53650abe 8466 }
8b6a5902 8467
3e4093b6 8468 constructor_index
db3927fb
AH
8469 = size_binop_loc (input_location,
8470 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8471
916c5919 8472 if (!value.value)
3e4093b6
RS
8473 /* If we are doing the bookkeeping for an element that was
8474 directly output as a constructor, we must update
8475 constructor_unfilled_index. */
8476 constructor_unfilled_index = constructor_index;
8477 }
8b6a5902 8478
3e4093b6
RS
8479 /* Handle the sole element allowed in a braced initializer
8480 for a scalar variable. */
b4519d39
SB
8481 else if (constructor_type != error_mark_node
8482 && constructor_fields == 0)
8b6a5902 8483 {
509c9d60
MLI
8484 pedwarn_init (input_location, 0,
8485 "excess elements in scalar initializer");
3e4093b6 8486 break;
8b6a5902
JJ
8487 }
8488 else
8489 {
916c5919 8490 if (value.value)
bbbbb16a
ILT
8491 output_init_element (value.value, value.original_type,
8492 strict_string, constructor_type,
a1e3b3d9
LB
8493 NULL_TREE, 1, implicit,
8494 braced_init_obstack);
3e4093b6 8495 constructor_fields = 0;
8b6a5902
JJ
8496 }
8497
3e4093b6
RS
8498 /* Handle range initializers either at this level or anywhere higher
8499 in the designator stack. */
8500 if (constructor_range_stack)
8b6a5902 8501 {
3e4093b6
RS
8502 struct constructor_range_stack *p, *range_stack;
8503 int finish = 0;
8504
8505 range_stack = constructor_range_stack;
8506 constructor_range_stack = 0;
8507 while (constructor_stack != range_stack->stack)
8b6a5902 8508 {
366de0ce 8509 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8510 process_init_element (pop_init_level (1,
8511 braced_init_obstack),
8512 true, braced_init_obstack);
8b6a5902 8513 }
3e4093b6
RS
8514 for (p = range_stack;
8515 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8516 p = p->prev)
8b6a5902 8517 {
366de0ce 8518 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8519 process_init_element (pop_init_level (1, braced_init_obstack),
8520 true, braced_init_obstack);
8b6a5902 8521 }
3e4093b6 8522
db3927fb
AH
8523 p->index = size_binop_loc (input_location,
8524 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8525 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8526 finish = 1;
8527
8528 while (1)
8529 {
8530 constructor_index = p->index;
8531 constructor_fields = p->fields;
8532 if (finish && p->range_end && p->index == p->range_start)
8533 {
8534 finish = 0;
8535 p->prev = 0;
8536 }
8537 p = p->next;
8538 if (!p)
8539 break;
a1e3b3d9 8540 push_init_level (2, braced_init_obstack);
3e4093b6
RS
8541 p->stack = constructor_stack;
8542 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8543 p->index = p->range_start;
8544 }
8545
8546 if (!finish)
8547 constructor_range_stack = range_stack;
8548 continue;
8b6a5902
JJ
8549 }
8550
3e4093b6 8551 break;
8b6a5902
JJ
8552 }
8553
3e4093b6
RS
8554 constructor_range_stack = 0;
8555}
8556\f
9f0e2d86
ZW
8557/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8558 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8559 an ASM_EXPR node). */
3e4093b6 8560tree
9f0e2d86 8561build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 8562{
6de9cd9a
DN
8563 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8564 ASM_VOLATILE_P (args) = 1;
9f0e2d86 8565 return add_stmt (args);
8b6a5902
JJ
8566}
8567
9f0e2d86
ZW
8568/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8569 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8570 SIMPLE indicates whether there was anything at all after the
8571 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 8572 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 8573tree
c2255bc4 8574build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 8575 tree clobbers, tree labels, bool simple)
e5e809f4 8576{
3e4093b6 8577 tree tail;
9f0e2d86 8578 tree args;
6de9cd9a
DN
8579 int i;
8580 const char *constraint;
74f0c611 8581 const char **oconstraints;
6de9cd9a 8582 bool allows_mem, allows_reg, is_inout;
74f0c611 8583 int ninputs, noutputs;
6de9cd9a
DN
8584
8585 ninputs = list_length (inputs);
8586 noutputs = list_length (outputs);
74f0c611
RH
8587 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8588
1c384bf1 8589 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 8590
6de9cd9a
DN
8591 /* Remove output conversions that change the type but not the mode. */
8592 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 8593 {
3e4093b6 8594 tree output = TREE_VALUE (tail);
74f0c611
RH
8595
8596 /* ??? Really, this should not be here. Users should be using a
8597 proper lvalue, dammit. But there's a long history of using casts
8598 in the output operands. In cases like longlong.h, this becomes a
8599 primitive form of typechecking -- if the cast can be removed, then
8600 the output operand had a type of the proper width; otherwise we'll
8601 get an error. Gross, but ... */
3e4093b6 8602 STRIP_NOPS (output);
74f0c611 8603
7bd11157 8604 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 8605 output = error_mark_node;
8b6a5902 8606
5544530a
PB
8607 if (output != error_mark_node
8608 && (TREE_READONLY (output)
8609 || TYPE_READONLY (TREE_TYPE (output))
8610 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8611 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8612 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
8613 readonly_error (output, lv_asm);
8614
6de9cd9a 8615 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
8616 oconstraints[i] = constraint;
8617
8618 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8619 &allows_mem, &allows_reg, &is_inout))
8620 {
8621 /* If the operand is going to end up in memory,
8622 mark it addressable. */
8623 if (!allows_reg && !c_mark_addressable (output))
8624 output = error_mark_node;
bae5cddf
JJ
8625 if (!(!allows_reg && allows_mem)
8626 && output != error_mark_node
8627 && VOID_TYPE_P (TREE_TYPE (output)))
8628 {
8629 error_at (loc, "invalid use of void expression");
8630 output = error_mark_node;
8631 }
74f0c611
RH
8632 }
8633 else
c22cacf3 8634 output = error_mark_node;
3e4093b6 8635
74f0c611 8636 TREE_VALUE (tail) = output;
8b6a5902 8637 }
3e4093b6 8638
74f0c611
RH
8639 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
8640 {
8641 tree input;
8642
8643 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8644 input = TREE_VALUE (tail);
8645
74f0c611
RH
8646 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
8647 oconstraints, &allows_mem, &allows_reg))
8648 {
8649 /* If the operand is going to end up in memory,
8650 mark it addressable. */
b4c33883
AP
8651 if (!allows_reg && allows_mem)
8652 {
8653 /* Strip the nops as we allow this case. FIXME, this really
8654 should be rejected or made deprecated. */
8655 STRIP_NOPS (input);
8656 if (!c_mark_addressable (input))
8657 input = error_mark_node;
bae5cddf
JJ
8658 }
8659 else if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
8660 {
8661 error_at (loc, "invalid use of void expression");
8662 input = error_mark_node;
8663 }
74f0c611
RH
8664 }
8665 else
8666 input = error_mark_node;
8667
8668 TREE_VALUE (tail) = input;
8669 }
3e4093b6 8670
1c384bf1
RH
8671 /* ASMs with labels cannot have outputs. This should have been
8672 enforced by the parser. */
8673 gcc_assert (outputs == NULL || labels == NULL);
8674
8675 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 8676
5544530a
PB
8677 /* asm statements without outputs, including simple ones, are treated
8678 as volatile. */
8679 ASM_INPUT_P (args) = simple;
8680 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 8681
9f0e2d86 8682 return args;
e5e809f4 8683}
3e4093b6 8684\f
c2255bc4
AH
8685/* Generate a goto statement to LABEL. LOC is the location of the
8686 GOTO. */
506e2710
RH
8687
8688tree
c2255bc4 8689c_finish_goto_label (location_t loc, tree label)
506e2710 8690{
e1b7793c 8691 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
8692 if (!decl)
8693 return NULL_TREE;
506e2710 8694 TREE_USED (decl) = 1;
c2255bc4
AH
8695 {
8696 tree t = build1 (GOTO_EXPR, void_type_node, decl);
8697 SET_EXPR_LOCATION (t, loc);
8698 return add_stmt (t);
8699 }
506e2710
RH
8700}
8701
c2255bc4
AH
8702/* Generate a computed goto statement to EXPR. LOC is the location of
8703 the GOTO. */
506e2710
RH
8704
8705tree
c2255bc4 8706c_finish_goto_ptr (location_t loc, tree expr)
506e2710 8707{
c2255bc4 8708 tree t;
c1771a20 8709 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 8710 expr = c_fully_fold (expr, false, NULL);
506e2710 8711 expr = convert (ptr_type_node, expr);
c2255bc4
AH
8712 t = build1 (GOTO_EXPR, void_type_node, expr);
8713 SET_EXPR_LOCATION (t, loc);
8714 return add_stmt (t);
506e2710
RH
8715}
8716
5088b058 8717/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
8718 to return, or a null pointer for `return;' with no value. LOC is
8719 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
8720 is the original type of RETVAL. */
de520661 8721
506e2710 8722tree
c2255bc4 8723c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 8724{
0c9b182b
JJ
8725 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
8726 bool no_warning = false;
928c19bb 8727 bool npc = false;
3e4093b6
RS
8728
8729 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
8730 warning_at (loc, 0,
8731 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 8732
928c19bb
JM
8733 if (retval)
8734 {
8ce94e44 8735 tree semantic_type = NULL_TREE;
928c19bb 8736 npc = null_pointer_constant_p (retval);
8ce94e44
JM
8737 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
8738 {
8739 semantic_type = TREE_TYPE (retval);
8740 retval = TREE_OPERAND (retval, 0);
8741 }
928c19bb 8742 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
8743 if (semantic_type)
8744 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
8745 }
8746
3e4093b6 8747 if (!retval)
de520661 8748 {
3e4093b6
RS
8749 current_function_returns_null = 1;
8750 if ((warn_return_type || flag_isoc99)
8751 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 8752 {
b8698a0f 8753 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 8754 "%<return%> with no value, in "
0c9b182b
JJ
8755 "function returning non-void");
8756 no_warning = true;
8757 }
400fbf9f 8758 }
3e4093b6 8759 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 8760 {
3e4093b6 8761 current_function_returns_null = 1;
2397c575 8762 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 8763 pedwarn (loc, 0,
509c9d60 8764 "%<return%> with a value, in function returning void");
b8698a0f 8765 else
c1771a20 8766 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 8767 "%<return%> with expression, in function returning void");
de520661 8768 }
3e4093b6 8769 else
de520661 8770 {
c2255bc4
AH
8771 tree t = convert_for_assignment (loc, valtype, retval, origtype,
8772 ic_return,
8773 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
8774 tree res = DECL_RESULT (current_function_decl);
8775 tree inner;
8776
8777 current_function_returns_value = 1;
8778 if (t == error_mark_node)
506e2710 8779 return NULL_TREE;
3e4093b6
RS
8780
8781 inner = t = convert (TREE_TYPE (res), t);
8782
8783 /* Strip any conversions, additions, and subtractions, and see if
8784 we are returning the address of a local variable. Warn if so. */
8785 while (1)
8b6a5902 8786 {
3e4093b6 8787 switch (TREE_CODE (inner))
8b6a5902 8788 {
849421a3
JJ
8789 CASE_CONVERT:
8790 case NON_LVALUE_EXPR:
3e4093b6 8791 case PLUS_EXPR:
849421a3 8792 case POINTER_PLUS_EXPR:
3e4093b6
RS
8793 inner = TREE_OPERAND (inner, 0);
8794 continue;
8795
8796 case MINUS_EXPR:
8797 /* If the second operand of the MINUS_EXPR has a pointer
8798 type (or is converted from it), this may be valid, so
8799 don't give a warning. */
8800 {
8801 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 8802
3f75a254 8803 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
8804 && (CONVERT_EXPR_P (op1)
8805 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 8806 op1 = TREE_OPERAND (op1, 0);
8b6a5902 8807
3e4093b6
RS
8808 if (POINTER_TYPE_P (TREE_TYPE (op1)))
8809 break;
8b6a5902 8810
3e4093b6
RS
8811 inner = TREE_OPERAND (inner, 0);
8812 continue;
8813 }
400fbf9f 8814
3e4093b6
RS
8815 case ADDR_EXPR:
8816 inner = TREE_OPERAND (inner, 0);
c2f4acb7 8817
6615c446 8818 while (REFERENCE_CLASS_P (inner)
c22cacf3 8819 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 8820 inner = TREE_OPERAND (inner, 0);
8b6a5902 8821
a2f1f4c3 8822 if (DECL_P (inner)
3f75a254
JM
8823 && !DECL_EXTERNAL (inner)
8824 && !TREE_STATIC (inner)
3e4093b6 8825 && DECL_CONTEXT (inner) == current_function_decl)
c2255bc4
AH
8826 warning_at (loc,
8827 0, "function returns address of local variable");
3e4093b6 8828 break;
8b6a5902 8829
3e4093b6
RS
8830 default:
8831 break;
8832 }
de520661 8833
3e4093b6
RS
8834 break;
8835 }
8836
53fb4de3 8837 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 8838 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
8839
8840 if (warn_sequence_point)
8841 verify_sequence_points (retval);
de520661 8842 }
8b6a5902 8843
c2255bc4 8844 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
8845 TREE_NO_WARNING (ret_stmt) |= no_warning;
8846 return add_stmt (ret_stmt);
de520661 8847}
3e4093b6
RS
8848\f
8849struct c_switch {
604f5adf
ILT
8850 /* The SWITCH_EXPR being built. */
8851 tree switch_expr;
a6c0a76c 8852
89dbed81 8853 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
8854 default conversion is applied. */
8855 tree orig_type;
8856
3e4093b6
RS
8857 /* A splay-tree mapping the low element of a case range to the high
8858 element, or NULL_TREE if there is no high element. Used to
8859 determine whether or not a new case label duplicates an old case
8860 label. We need a tree, rather than simply a hash table, because
8861 of the GNU case range extension. */
8862 splay_tree cases;
a6c0a76c 8863
e1b7793c
ILT
8864 /* The bindings at the point of the switch. This is used for
8865 warnings crossing decls when branching to a case label. */
8866 struct c_spot_bindings *bindings;
187230a7 8867
3e4093b6
RS
8868 /* The next node on the stack. */
8869 struct c_switch *next;
8870};
400fbf9f 8871
3e4093b6
RS
8872/* A stack of the currently active switch statements. The innermost
8873 switch statement is on the top of the stack. There is no need to
8874 mark the stack for garbage collection because it is only active
8875 during the processing of the body of a function, and we never
8876 collect at that point. */
de520661 8877
506e2710 8878struct c_switch *c_switch_stack;
de520661 8879
3e4093b6 8880/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4
AH
8881 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
8882 SWITCH_COND_LOC is the location of the switch's condition. */
de520661 8883
3e4093b6 8884tree
c2255bc4
AH
8885c_start_case (location_t switch_loc,
8886 location_t switch_cond_loc,
8887 tree exp)
de520661 8888{
c58e8676 8889 tree orig_type = error_mark_node;
3e4093b6 8890 struct c_switch *cs;
2f6e4e97 8891
3e4093b6 8892 if (exp != error_mark_node)
de520661 8893 {
3e4093b6
RS
8894 orig_type = TREE_TYPE (exp);
8895
c58e8676 8896 if (!INTEGRAL_TYPE_P (orig_type))
de520661 8897 {
c58e8676
VR
8898 if (orig_type != error_mark_node)
8899 {
c2255bc4 8900 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
8901 orig_type = error_mark_node;
8902 }
3e4093b6 8903 exp = integer_zero_node;
de520661 8904 }
3e4093b6 8905 else
de520661 8906 {
c58e8676 8907 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 8908
3176a0c2 8909 if (!in_system_header
3e4093b6
RS
8910 && (type == long_integer_type_node
8911 || type == long_unsigned_type_node))
c2255bc4
AH
8912 warning_at (switch_cond_loc,
8913 OPT_Wtraditional, "%<long%> switch expression not "
8914 "converted to %<int%> in ISO C");
8b6a5902 8915
928c19bb 8916 exp = c_fully_fold (exp, false, NULL);
3e4093b6 8917 exp = default_conversion (exp);
ca085fd7
MLI
8918
8919 if (warn_sequence_point)
8920 verify_sequence_points (exp);
3e4093b6
RS
8921 }
8922 }
8923
604f5adf 8924 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 8925 cs = XNEW (struct c_switch);
604f5adf 8926 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 8927 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 8928 cs->orig_type = orig_type;
3e4093b6 8929 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 8930 cs->bindings = c_get_switch_bindings ();
506e2710
RH
8931 cs->next = c_switch_stack;
8932 c_switch_stack = cs;
3e4093b6 8933
604f5adf 8934 return add_stmt (cs->switch_expr);
3e4093b6
RS
8935}
8936
c2255bc4 8937/* Process a case label at location LOC. */
3e4093b6
RS
8938
8939tree
c2255bc4 8940do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
8941{
8942 tree label = NULL_TREE;
8943
17cede2e
JM
8944 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
8945 {
8946 low_value = c_fully_fold (low_value, false, NULL);
8947 if (TREE_CODE (low_value) == INTEGER_CST)
c1771a20 8948 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
8949 "case label is not an integer constant expression");
8950 }
8951
8952 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
8953 {
8954 high_value = c_fully_fold (high_value, false, NULL);
8955 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 8956 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
8957 "case label is not an integer constant expression");
8958 }
8959
e1b7793c 8960 if (c_switch_stack == NULL)
187230a7
JM
8961 {
8962 if (low_value)
e1b7793c 8963 error_at (loc, "case label not within a switch statement");
187230a7 8964 else
e1b7793c
ILT
8965 error_at (loc, "%<default%> label not within a switch statement");
8966 return NULL_TREE;
187230a7 8967 }
de520661 8968
e1b7793c
ILT
8969 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
8970 EXPR_LOCATION (c_switch_stack->switch_expr),
8971 loc))
8972 return NULL_TREE;
8973
8974 label = c_add_case_label (loc, c_switch_stack->cases,
8975 SWITCH_COND (c_switch_stack->switch_expr),
8976 c_switch_stack->orig_type,
8977 low_value, high_value);
8978 if (label == error_mark_node)
8979 label = NULL_TREE;
3e4093b6
RS
8980 return label;
8981}
de520661 8982
3e4093b6 8983/* Finish the switch statement. */
de520661 8984
3e4093b6 8985void
325c3691 8986c_finish_case (tree body)
3e4093b6 8987{
506e2710 8988 struct c_switch *cs = c_switch_stack;
fbc315db 8989 location_t switch_location;
3e4093b6 8990
604f5adf 8991 SWITCH_BODY (cs->switch_expr) = body;
325c3691 8992
6de9cd9a 8993 /* Emit warnings as needed. */
c2255bc4 8994 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
8995 c_do_switch_warnings (cs->cases, switch_location,
8996 TREE_TYPE (cs->switch_expr),
8997 SWITCH_COND (cs->switch_expr));
6de9cd9a 8998
3e4093b6 8999 /* Pop the stack. */
506e2710 9000 c_switch_stack = cs->next;
3e4093b6 9001 splay_tree_delete (cs->cases);
e1b7793c 9002 c_release_switch_bindings (cs->bindings);
5d038c4c 9003 XDELETE (cs);
de520661 9004}
325c3691 9005\f
506e2710
RH
9006/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9007 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9008 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9009 statement, and was not surrounded with parenthesis. */
325c3691 9010
9e51cf9d 9011void
506e2710
RH
9012c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9013 tree else_block, bool nested_if)
325c3691 9014{
506e2710 9015 tree stmt;
325c3691 9016
506e2710
RH
9017 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9018 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9019 {
506e2710 9020 tree inner_if = then_block;
16865eaa 9021
61ada8ae 9022 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9023 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9024 it might not be exactly THEN_BLOCK, but should be the last
9025 non-container statement within. */
9026 while (1)
9027 switch (TREE_CODE (inner_if))
9028 {
9029 case COND_EXPR:
9030 goto found;
9031 case BIND_EXPR:
9032 inner_if = BIND_EXPR_BODY (inner_if);
9033 break;
9034 case STATEMENT_LIST:
9035 inner_if = expr_last (then_block);
9036 break;
9037 case TRY_FINALLY_EXPR:
9038 case TRY_CATCH_EXPR:
9039 inner_if = TREE_OPERAND (inner_if, 0);
9040 break;
9041 default:
366de0ce 9042 gcc_unreachable ();
506e2710
RH
9043 }
9044 found:
16865eaa 9045
506e2710 9046 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9047 warning_at (if_locus, OPT_Wparentheses,
9048 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9049 }
16865eaa 9050
2214de30 9051 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9052 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9053 add_stmt (stmt);
325c3691
RH
9054}
9055
506e2710
RH
9056/* Emit a general-purpose loop construct. START_LOCUS is the location of
9057 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9058 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9059 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9060 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9061
9062void
506e2710
RH
9063c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9064 tree blab, tree clab, bool cond_is_first)
325c3691 9065{
506e2710
RH
9066 tree entry = NULL, exit = NULL, t;
9067
28af952a
RS
9068 /* If the condition is zero don't generate a loop construct. */
9069 if (cond && integer_zerop (cond))
9070 {
9071 if (cond_is_first)
9072 {
9073 t = build_and_jump (&blab);
9074 SET_EXPR_LOCATION (t, start_locus);
9075 add_stmt (t);
9076 }
9077 }
9078 else
506e2710
RH
9079 {
9080 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9081
506e2710 9082 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9083 out of the loop, or to the top of it. If there's no exit condition,
9084 then we just build a jump back to the top. */
506e2710 9085 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9086
28af952a 9087 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9088 {
9089 /* Canonicalize the loop condition to the end. This means
9090 generating a branch to the loop condition. Reuse the
9091 continue label, if possible. */
9092 if (cond_is_first)
9093 {
9094 if (incr || !clab)
9095 {
9096 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9097 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9098 }
9099 else
9100 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9101 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9102 add_stmt (t);
9103 }
9104
506e2710 9105 t = build_and_jump (&blab);
506e2710 9106 if (cond_is_first)
db3927fb
AH
9107 exit = fold_build3_loc (start_locus,
9108 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9109 else
db3927fb
AH
9110 exit = fold_build3_loc (input_location,
9111 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9112 }
9113
506e2710
RH
9114 add_stmt (top);
9115 }
c22cacf3 9116
506e2710
RH
9117 if (body)
9118 add_stmt (body);
9119 if (clab)
9120 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9121 if (incr)
9122 add_stmt (incr);
9123 if (entry)
9124 add_stmt (entry);
9125 if (exit)
9126 add_stmt (exit);
9127 if (blab)
9128 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9129}
325c3691
RH
9130
9131tree
c2255bc4 9132c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9133{
089efaa4 9134 bool skip;
506e2710 9135 tree label = *label_p;
325c3691 9136
089efaa4
ILT
9137 /* In switch statements break is sometimes stylistically used after
9138 a return statement. This can lead to spurious warnings about
9139 control reaching the end of a non-void function when it is
9140 inlined. Note that we are calling block_may_fallthru with
9141 language specific tree nodes; this works because
9142 block_may_fallthru returns true when given something it does not
9143 understand. */
9144 skip = !block_may_fallthru (cur_stmt_list);
9145
506e2710 9146 if (!label)
089efaa4
ILT
9147 {
9148 if (!skip)
c2255bc4 9149 *label_p = label = create_artificial_label (loc);
089efaa4 9150 }
953ff289
DN
9151 else if (TREE_CODE (label) == LABEL_DECL)
9152 ;
9153 else switch (TREE_INT_CST_LOW (label))
506e2710 9154 {
953ff289 9155 case 0:
506e2710 9156 if (is_break)
c2255bc4 9157 error_at (loc, "break statement not within loop or switch");
506e2710 9158 else
c2255bc4 9159 error_at (loc, "continue statement not within a loop");
506e2710 9160 return NULL_TREE;
953ff289
DN
9161
9162 case 1:
9163 gcc_assert (is_break);
c2255bc4 9164 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9165 return NULL_TREE;
9166
9167 default:
9168 gcc_unreachable ();
506e2710 9169 }
325c3691 9170
089efaa4
ILT
9171 if (skip)
9172 return NULL_TREE;
9173
2e28e797
JH
9174 if (!is_break)
9175 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9176
53fb4de3 9177 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9178}
9179
506e2710 9180/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9181
9182static void
c2255bc4 9183emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9184{
e6b5a630
RH
9185 if (expr == error_mark_node)
9186 ;
9187 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9188 {
9189 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9190 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9191 }
27f33b15 9192 else
c2255bc4 9193 warn_if_unused_value (expr, loc);
3a5b9284
RH
9194}
9195
506e2710 9196/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9197 diagnostics, but do not call ADD_STMT. LOC is the location of the
9198 statement. */
3a5b9284 9199
506e2710 9200tree
c2255bc4 9201c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9202{
056928b2
JJ
9203 tree exprv;
9204
3a5b9284 9205 if (!expr)
506e2710 9206 return NULL_TREE;
3a5b9284 9207
928c19bb
JM
9208 expr = c_fully_fold (expr, false, NULL);
9209
3a5b9284
RH
9210 if (warn_sequence_point)
9211 verify_sequence_points (expr);
9212
9213 if (TREE_TYPE (expr) != error_mark_node
9214 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9215 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9216 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9217
9218 /* If we're not processing a statement expression, warn about unused values.
9219 Warnings for statement expressions will be emitted later, once we figure
9220 out which is the result. */
9221 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9222 && warn_unused_value)
c2255bc4 9223 emit_side_effect_warnings (loc, expr);
3a5b9284 9224
056928b2
JJ
9225 exprv = expr;
9226 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9227 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9228 while (CONVERT_EXPR_P (exprv))
9229 exprv = TREE_OPERAND (exprv, 0);
9230 if (DECL_P (exprv)
9231 || handled_component_p (exprv)
9232 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9233 mark_exp_read (exprv);
fa8351f8 9234
3a5b9284
RH
9235 /* If the expression is not of a type to which we cannot assign a line
9236 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9237 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9238 {
9239 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9240 SET_EXPR_LOCATION (expr, loc);
9241 }
506e2710
RH
9242
9243 return expr;
9244}
9245
c2255bc4
AH
9246/* Emit an expression as a statement. LOC is the location of the
9247 expression. */
506e2710
RH
9248
9249tree
c2255bc4 9250c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9251{
9252 if (expr)
c2255bc4 9253 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9254 else
9255 return NULL;
3a5b9284
RH
9256}
9257
9258/* Do the opposite and emit a statement as an expression. To begin,
9259 create a new binding level and return it. */
325c3691
RH
9260
9261tree
9262c_begin_stmt_expr (void)
9263{
9264 tree ret;
9265
9266 /* We must force a BLOCK for this level so that, if it is not expanded
9267 later, there is a way to turn off the entire subtree of blocks that
9268 are contained in it. */
9269 keep_next_level ();
9270 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9271
9272 c_bindings_start_stmt_expr (c_switch_stack == NULL
9273 ? NULL
9274 : c_switch_stack->bindings);
325c3691
RH
9275
9276 /* Mark the current statement list as belonging to a statement list. */
9277 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9278
9279 return ret;
9280}
9281
c2255bc4
AH
9282/* LOC is the location of the compound statement to which this body
9283 belongs. */
9284
325c3691 9285tree
c2255bc4 9286c_finish_stmt_expr (location_t loc, tree body)
325c3691 9287{
3a5b9284 9288 tree last, type, tmp, val;
325c3691
RH
9289 tree *last_p;
9290
c2255bc4 9291 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9292
9293 c_bindings_end_stmt_expr (c_switch_stack == NULL
9294 ? NULL
9295 : c_switch_stack->bindings);
325c3691 9296
3a5b9284
RH
9297 /* Locate the last statement in BODY. See c_end_compound_stmt
9298 about always returning a BIND_EXPR. */
9299 last_p = &BIND_EXPR_BODY (body);
9300 last = BIND_EXPR_BODY (body);
9301
9302 continue_searching:
325c3691
RH
9303 if (TREE_CODE (last) == STATEMENT_LIST)
9304 {
3a5b9284
RH
9305 tree_stmt_iterator i;
9306
9307 /* This can happen with degenerate cases like ({ }). No value. */
9308 if (!TREE_SIDE_EFFECTS (last))
9309 return body;
9310
9311 /* If we're supposed to generate side effects warnings, process
9312 all of the statements except the last. */
27f33b15 9313 if (warn_unused_value)
325c3691 9314 {
3a5b9284 9315 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9316 {
9317 location_t tloc;
9318 tree t = tsi_stmt (i);
9319
9320 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9321 emit_side_effect_warnings (tloc, t);
9322 }
325c3691
RH
9323 }
9324 else
3a5b9284
RH
9325 i = tsi_last (last);
9326 last_p = tsi_stmt_ptr (i);
9327 last = *last_p;
325c3691
RH
9328 }
9329
3a5b9284
RH
9330 /* If the end of the list is exception related, then the list was split
9331 by a call to push_cleanup. Continue searching. */
9332 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9333 || TREE_CODE (last) == TRY_CATCH_EXPR)
9334 {
9335 last_p = &TREE_OPERAND (last, 0);
9336 last = *last_p;
9337 goto continue_searching;
9338 }
9339
26d8af35
JM
9340 if (last == error_mark_node)
9341 return last;
9342
3a5b9284
RH
9343 /* In the case that the BIND_EXPR is not necessary, return the
9344 expression out from inside it. */
26d8af35
JM
9345 if (last == BIND_EXPR_BODY (body)
9346 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9347 {
928c19bb
JM
9348 /* Even if this looks constant, do not allow it in a constant
9349 expression. */
e5a94231 9350 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9351 /* Do not warn if the return value of a statement expression is
9352 unused. */
928c19bb 9353 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9354 return last;
9355 }
325c3691
RH
9356
9357 /* Extract the type of said expression. */
9358 type = TREE_TYPE (last);
325c3691 9359
3a5b9284
RH
9360 /* If we're not returning a value at all, then the BIND_EXPR that
9361 we already have is a fine expression to return. */
9362 if (!type || VOID_TYPE_P (type))
9363 return body;
9364
9365 /* Now that we've located the expression containing the value, it seems
9366 silly to make voidify_wrapper_expr repeat the process. Create a
9367 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9368 tmp = create_tmp_var_raw (type, NULL);
9369
9370 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9371 tree_expr_nonnegative_p giving up immediately. */
9372 val = last;
9373 if (TREE_CODE (val) == NOP_EXPR
9374 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9375 val = TREE_OPERAND (val, 0);
9376
53fb4de3 9377 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9378 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9379
c2255bc4
AH
9380 {
9381 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9382 SET_EXPR_LOCATION (t, loc);
9383 return t;
9384 }
325c3691
RH
9385}
9386\f
9387/* Begin and end compound statements. This is as simple as pushing
9388 and popping new statement lists from the tree. */
9389
9390tree
9391c_begin_compound_stmt (bool do_scope)
9392{
9393 tree stmt = push_stmt_list ();
9394 if (do_scope)
4dfa0342 9395 push_scope ();
325c3691
RH
9396 return stmt;
9397}
9398
c2255bc4
AH
9399/* End a compound statement. STMT is the statement. LOC is the
9400 location of the compound statement-- this is usually the location
9401 of the opening brace. */
9402
325c3691 9403tree
c2255bc4 9404c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9405{
9406 tree block = NULL;
9407
9408 if (do_scope)
9409 {
9410 if (c_dialect_objc ())
9411 objc_clear_super_receiver ();
9412 block = pop_scope ();
9413 }
9414
9415 stmt = pop_stmt_list (stmt);
c2255bc4 9416 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9417
9418 /* If this compound statement is nested immediately inside a statement
9419 expression, then force a BIND_EXPR to be created. Otherwise we'll
9420 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9421 STATEMENT_LISTs merge, and thus we can lose track of what statement
9422 was really last. */
38e01f9e 9423 if (building_stmt_list_p ()
325c3691
RH
9424 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9425 && TREE_CODE (stmt) != BIND_EXPR)
9426 {
53fb4de3 9427 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9428 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9429 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9430 }
9431
9432 return stmt;
9433}
5a508662
RH
9434
9435/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9436 when the current scope is exited. EH_ONLY is true when this is not
9437 meant to apply to normal control flow transfer. */
9438
9439void
c2255bc4 9440push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 9441{
3a5b9284
RH
9442 enum tree_code code;
9443 tree stmt, list;
9444 bool stmt_expr;
9445
9446 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 9447 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 9448 add_stmt (stmt);
3a5b9284
RH
9449 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9450 list = push_stmt_list ();
9451 TREE_OPERAND (stmt, 0) = list;
9452 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 9453}
0e3a99ae
AS
9454
9455/* Convert scalar to vector for the range of operations. */
9456static enum stv_conv
9457scalar_to_vector (location_t loc, enum tree_code code, tree op0, tree op1)
9458{
9459 tree type0 = TREE_TYPE (op0);
9460 tree type1 = TREE_TYPE (op1);
9461 bool integer_only_op = false;
9462 enum stv_conv ret = stv_firstarg;
f90e8e2e
AS
9463
9464 gcc_assert (TREE_CODE (type0) == VECTOR_TYPE
0e3a99ae
AS
9465 || TREE_CODE (type1) == VECTOR_TYPE);
9466 switch (code)
9467 {
9468 case RSHIFT_EXPR:
9469 case LSHIFT_EXPR:
9470 if (TREE_CODE (type0) == INTEGER_TYPE
9471 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9472 {
9473 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9474 {
9475 error_at (loc, "conversion of scalar to vector "
9476 "involves truncation");
9477 return stv_error;
9478 }
9479 else
9480 return stv_firstarg;
9481 }
9482 break;
9483
9484 case BIT_IOR_EXPR:
9485 case BIT_XOR_EXPR:
9486 case BIT_AND_EXPR:
9487 integer_only_op = true;
9488 /* ... fall through ... */
f90e8e2e 9489
0e3a99ae
AS
9490 case PLUS_EXPR:
9491 case MINUS_EXPR:
9492 case MULT_EXPR:
9493 case TRUNC_DIV_EXPR:
9494 case TRUNC_MOD_EXPR:
9495 case RDIV_EXPR:
9496 if (TREE_CODE (type0) == VECTOR_TYPE)
9497 {
9498 tree tmp;
9499 ret = stv_secondarg;
9500 /* Swap TYPE0 with TYPE1 and OP0 with OP1 */
9501 tmp = type0; type0 = type1; type1 = tmp;
9502 tmp = op0; op0 = op1; op1 = tmp;
9503 }
9504
9505 if (TREE_CODE (type0) == INTEGER_TYPE
f90e8e2e 9506 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
0e3a99ae
AS
9507 {
9508 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9509 {
9510 error_at (loc, "conversion of scalar to vector "
9511 "involves truncation");
9512 return stv_error;
9513 }
9514 return ret;
9515 }
9516 else if (!integer_only_op
9517 /* Allow integer --> real conversion if safe. */
f90e8e2e 9518 && (TREE_CODE (type0) == REAL_TYPE
0e3a99ae
AS
9519 || TREE_CODE (type0) == INTEGER_TYPE)
9520 && SCALAR_FLOAT_TYPE_P (TREE_TYPE (type1)))
9521 {
9522 if (unsafe_conversion_p (TREE_TYPE (type1), op0, false))
9523 {
9524 error_at (loc, "conversion of scalar to vector "
9525 "involves truncation");
9526 return stv_error;
9527 }
9528 return ret;
9529 }
9530 default:
9531 break;
9532 }
f90e8e2e 9533
0e3a99ae
AS
9534 return stv_nothing;
9535}
325c3691 9536\f
3e4093b6
RS
9537/* Build a binary-operation expression without default conversions.
9538 CODE is the kind of expression to build.
ba47d38d 9539 LOCATION is the operator's location.
3e4093b6
RS
9540 This function differs from `build' in several ways:
9541 the data type of the result is computed and recorded in it,
9542 warnings are generated if arg data types are invalid,
9543 special handling for addition and subtraction of pointers is known,
9544 and some optimization is done (operations on narrow ints
9545 are done in the narrower type when that gives the same result).
9546 Constant folding is also done before the result is returned.
de520661 9547
3e4093b6
RS
9548 Note that the operands will never have enumeral types, or function
9549 or array types, because either they will have the default conversions
9550 performed or they have both just been converted to some other type in which
9551 the arithmetic is to be done. */
9552
9553tree
ba47d38d
AH
9554build_binary_op (location_t location, enum tree_code code,
9555 tree orig_op0, tree orig_op1, int convert_p)
de520661 9556{
8ce94e44
JM
9557 tree type0, type1, orig_type0, orig_type1;
9558 tree eptype;
3e4093b6
RS
9559 enum tree_code code0, code1;
9560 tree op0, op1;
c9f9eb5d 9561 tree ret = error_mark_node;
4de67c26 9562 const char *invalid_op_diag;
4d84fe7c 9563 bool op0_int_operands, op1_int_operands;
928c19bb 9564 bool int_const, int_const_or_overflow, int_operands;
b62acd60 9565
3e4093b6
RS
9566 /* Expression code to give to the expression when it is built.
9567 Normally this is CODE, which is what the caller asked for,
9568 but in some special cases we change it. */
9569 enum tree_code resultcode = code;
8b6a5902 9570
3e4093b6
RS
9571 /* Data type in which the computation is to be performed.
9572 In the simplest cases this is the common type of the arguments. */
9573 tree result_type = NULL;
9574
8ce94e44
JM
9575 /* When the computation is in excess precision, the type of the
9576 final EXCESS_PRECISION_EXPR. */
2d2e923f 9577 tree semantic_result_type = NULL;
8ce94e44 9578
3e4093b6
RS
9579 /* Nonzero means operands have already been type-converted
9580 in whatever way is necessary.
9581 Zero means they need to be converted to RESULT_TYPE. */
9582 int converted = 0;
9583
9584 /* Nonzero means create the expression with this type, rather than
9585 RESULT_TYPE. */
9586 tree build_type = 0;
9587
9588 /* Nonzero means after finally constructing the expression
9589 convert it to this type. */
9590 tree final_type = 0;
9591
9592 /* Nonzero if this is an operation like MIN or MAX which can
9593 safely be computed in short if both args are promoted shorts.
9594 Also implies COMMON.
9595 -1 indicates a bitwise operation; this makes a difference
9596 in the exact conditions for when it is safe to do the operation
9597 in a narrower mode. */
9598 int shorten = 0;
9599
9600 /* Nonzero if this is a comparison operation;
9601 if both args are promoted shorts, compare the original shorts.
9602 Also implies COMMON. */
9603 int short_compare = 0;
9604
9605 /* Nonzero if this is a right-shift operation, which can be computed on the
9606 original short and then promoted if the operand is a promoted short. */
9607 int short_shift = 0;
9608
9609 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9610 int common = 0;
9611
58393038
ZL
9612 /* True means types are compatible as far as ObjC is concerned. */
9613 bool objc_ok;
9614
8ce94e44
JM
9615 /* True means this is an arithmetic operation that may need excess
9616 precision. */
9617 bool may_need_excess_precision;
9618
180f8dbb
JM
9619 /* True means this is a boolean operation that converts both its
9620 operands to truth-values. */
9621 bool boolean_op = false;
9622
ba47d38d
AH
9623 if (location == UNKNOWN_LOCATION)
9624 location = input_location;
9625
4d84fe7c
JM
9626 op0 = orig_op0;
9627 op1 = orig_op1;
9628
9629 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
9630 if (op0_int_operands)
9631 op0 = remove_c_maybe_const_expr (op0);
9632 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
9633 if (op1_int_operands)
9634 op1 = remove_c_maybe_const_expr (op1);
9635 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
9636 if (int_operands)
9637 {
9638 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
9639 && TREE_CODE (orig_op1) == INTEGER_CST);
9640 int_const = (int_const_or_overflow
9641 && !TREE_OVERFLOW (orig_op0)
9642 && !TREE_OVERFLOW (orig_op1));
9643 }
9644 else
9645 int_const = int_const_or_overflow = false;
9646
0e3a99ae 9647 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
9648 if (convert_p
9649 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 9650 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 9651 {
4d84fe7c
JM
9652 op0 = default_conversion (op0);
9653 op1 = default_conversion (op1);
790e9490
RS
9654 }
9655
8ce94e44
JM
9656 orig_type0 = type0 = TREE_TYPE (op0);
9657 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 9658
3e4093b6
RS
9659 /* The expression codes of the data types of the arguments tell us
9660 whether the arguments are integers, floating, pointers, etc. */
9661 code0 = TREE_CODE (type0);
9662 code1 = TREE_CODE (type1);
9663
9664 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
9665 STRIP_TYPE_NOPS (op0);
9666 STRIP_TYPE_NOPS (op1);
9667
9668 /* If an error was already reported for one of the arguments,
9669 avoid reporting another error. */
9670
9671 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
9672 return error_mark_node;
9673
4de67c26
JM
9674 if ((invalid_op_diag
9675 = targetm.invalid_binary_op (code, type0, type1)))
9676 {
ba47d38d 9677 error_at (location, invalid_op_diag);
4de67c26
JM
9678 return error_mark_node;
9679 }
9680
8ce94e44
JM
9681 switch (code)
9682 {
9683 case PLUS_EXPR:
9684 case MINUS_EXPR:
9685 case MULT_EXPR:
9686 case TRUNC_DIV_EXPR:
9687 case CEIL_DIV_EXPR:
9688 case FLOOR_DIV_EXPR:
9689 case ROUND_DIV_EXPR:
9690 case EXACT_DIV_EXPR:
9691 may_need_excess_precision = true;
9692 break;
9693 default:
9694 may_need_excess_precision = false;
9695 break;
9696 }
9697 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
9698 {
9699 op0 = TREE_OPERAND (op0, 0);
9700 type0 = TREE_TYPE (op0);
9701 }
9702 else if (may_need_excess_precision
9703 && (eptype = excess_precision_type (type0)) != NULL_TREE)
9704 {
9705 type0 = eptype;
9706 op0 = convert (eptype, op0);
9707 }
9708 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
9709 {
9710 op1 = TREE_OPERAND (op1, 0);
9711 type1 = TREE_TYPE (op1);
9712 }
9713 else if (may_need_excess_precision
9714 && (eptype = excess_precision_type (type1)) != NULL_TREE)
9715 {
9716 type1 = eptype;
9717 op1 = convert (eptype, op1);
9718 }
9719
58393038
ZL
9720 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
9721
0e3a99ae
AS
9722 /* In case when one of the operands of the binary operation is
9723 a vector and another is a scalar -- convert scalar to vector. */
9724 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
9725 {
9726 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1);
f90e8e2e 9727
0e3a99ae
AS
9728 switch (convert_flag)
9729 {
9730 case stv_error:
9731 return error_mark_node;
9732 case stv_firstarg:
9733 {
9734 bool maybe_const = true;
9735 tree sc;
9736 sc = c_fully_fold (op0, false, &maybe_const);
9737 sc = save_expr (sc);
9738 sc = convert (TREE_TYPE (type1), sc);
9739 op0 = build_vector_from_val (type1, sc);
9740 if (!maybe_const)
9741 op0 = c_wrap_maybe_const (op0, true);
9742 orig_type0 = type0 = TREE_TYPE (op0);
9743 code0 = TREE_CODE (type0);
9744 converted = 1;
9745 break;
9746 }
9747 case stv_secondarg:
9748 {
9749 bool maybe_const = true;
9750 tree sc;
9751 sc = c_fully_fold (op1, false, &maybe_const);
9752 sc = save_expr (sc);
9753 sc = convert (TREE_TYPE (type0), sc);
9754 op1 = build_vector_from_val (type0, sc);
9755 if (!maybe_const)
54b9f838 9756 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
9757 orig_type1 = type1 = TREE_TYPE (op1);
9758 code1 = TREE_CODE (type1);
9759 converted = 1;
9760 break;
9761 }
9762 default:
9763 break;
9764 }
9765 }
9766
3e4093b6 9767 switch (code)
de520661 9768 {
3e4093b6
RS
9769 case PLUS_EXPR:
9770 /* Handle the pointer + int case. */
9771 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 9772 {
db3927fb 9773 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
9774 goto return_build_binary_op;
9775 }
3e4093b6 9776 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 9777 {
db3927fb 9778 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
9779 goto return_build_binary_op;
9780 }
fe67cf58 9781 else
3e4093b6
RS
9782 common = 1;
9783 break;
400fbf9f 9784
3e4093b6
RS
9785 case MINUS_EXPR:
9786 /* Subtraction of two similar pointers.
9787 We must subtract them as integers, then divide by object size. */
9788 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 9789 && comp_target_types (location, type0, type1))
c9f9eb5d 9790 {
db3927fb 9791 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
9792 goto return_build_binary_op;
9793 }
3e4093b6
RS
9794 /* Handle pointer minus int. Just like pointer plus int. */
9795 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 9796 {
db3927fb 9797 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
9798 goto return_build_binary_op;
9799 }
3e4093b6
RS
9800 else
9801 common = 1;
9802 break;
8b6a5902 9803
3e4093b6
RS
9804 case MULT_EXPR:
9805 common = 1;
9806 break;
9807
9808 case TRUNC_DIV_EXPR:
9809 case CEIL_DIV_EXPR:
9810 case FLOOR_DIV_EXPR:
9811 case ROUND_DIV_EXPR:
9812 case EXACT_DIV_EXPR:
c9f9eb5d 9813 warn_for_div_by_zero (location, op1);
3e4093b6
RS
9814
9815 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 9816 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
9817 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
9818 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 9819 || code1 == FIXED_POINT_TYPE
3e4093b6 9820 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 9821 {
5bed876a
AH
9822 enum tree_code tcode0 = code0, tcode1 = code1;
9823
3a021db2 9824 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 9825 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 9826 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 9827 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 9828
ab22c1fa
CF
9829 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
9830 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
9831 resultcode = RDIV_EXPR;
9832 else
9833 /* Although it would be tempting to shorten always here, that
9834 loses on some targets, since the modulo instruction is
9835 undefined if the quotient can't be represented in the
9836 computation mode. We shorten only if unsigned or if
9837 dividing by something we know != -1. */
8df83eae 9838 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 9839 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 9840 && !integer_all_onesp (op1)));
3e4093b6
RS
9841 common = 1;
9842 }
9843 break;
de520661 9844
3e4093b6 9845 case BIT_AND_EXPR:
3e4093b6
RS
9846 case BIT_IOR_EXPR:
9847 case BIT_XOR_EXPR:
9848 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
9849 shorten = -1;
9ef0c8d9
AP
9850 /* Allow vector types which are not floating point types. */
9851 else if (code0 == VECTOR_TYPE
9852 && code1 == VECTOR_TYPE
9853 && !VECTOR_FLOAT_TYPE_P (type0)
9854 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
9855 common = 1;
9856 break;
9857
9858 case TRUNC_MOD_EXPR:
9859 case FLOOR_MOD_EXPR:
c9f9eb5d 9860 warn_for_div_by_zero (location, op1);
de520661 9861
5cfd5d9b
AP
9862 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9863 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9864 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
9865 common = 1;
9866 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
9867 {
9868 /* Although it would be tempting to shorten always here, that loses
9869 on some targets, since the modulo instruction is undefined if the
9870 quotient can't be represented in the computation mode. We shorten
9871 only if unsigned or if dividing by something we know != -1. */
8df83eae 9872 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 9873 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 9874 && !integer_all_onesp (op1)));
3e4093b6
RS
9875 common = 1;
9876 }
9877 break;
de520661 9878
3e4093b6
RS
9879 case TRUTH_ANDIF_EXPR:
9880 case TRUTH_ORIF_EXPR:
9881 case TRUTH_AND_EXPR:
9882 case TRUTH_OR_EXPR:
9883 case TRUTH_XOR_EXPR:
9884 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
9885 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9886 || code0 == FIXED_POINT_TYPE)
3e4093b6 9887 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
9888 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9889 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
9890 {
9891 /* Result of these operations is always an int,
9892 but that does not mean the operands should be
9893 converted to ints! */
9894 result_type = integer_type_node;
ba47d38d
AH
9895 op0 = c_common_truthvalue_conversion (location, op0);
9896 op1 = c_common_truthvalue_conversion (location, op1);
3e4093b6 9897 converted = 1;
180f8dbb 9898 boolean_op = true;
3e4093b6 9899 }
928c19bb
JM
9900 if (code == TRUTH_ANDIF_EXPR)
9901 {
9902 int_const_or_overflow = (int_operands
9903 && TREE_CODE (orig_op0) == INTEGER_CST
9904 && (op0 == truthvalue_false_node
9905 || TREE_CODE (orig_op1) == INTEGER_CST));
9906 int_const = (int_const_or_overflow
9907 && !TREE_OVERFLOW (orig_op0)
9908 && (op0 == truthvalue_false_node
9909 || !TREE_OVERFLOW (orig_op1)));
9910 }
9911 else if (code == TRUTH_ORIF_EXPR)
9912 {
9913 int_const_or_overflow = (int_operands
9914 && TREE_CODE (orig_op0) == INTEGER_CST
9915 && (op0 == truthvalue_true_node
9916 || TREE_CODE (orig_op1) == INTEGER_CST));
9917 int_const = (int_const_or_overflow
9918 && !TREE_OVERFLOW (orig_op0)
9919 && (op0 == truthvalue_true_node
9920 || !TREE_OVERFLOW (orig_op1)));
9921 }
3e4093b6 9922 break;
eba80994 9923
3e4093b6
RS
9924 /* Shift operations: result has same type as first operand;
9925 always convert second operand to int.
9926 Also set SHORT_SHIFT if shifting rightward. */
de520661 9927
3e4093b6 9928 case RSHIFT_EXPR:
f87bd04b
AS
9929 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9930 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9931 {
9932 result_type = type0;
9933 converted = 1;
9934 }
9935 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9936 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9937 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9938 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9939 {
9940 result_type = type0;
9941 converted = 1;
9942 }
9943 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 9944 && code1 == INTEGER_TYPE)
3e4093b6 9945 {
928c19bb 9946 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 9947 {
3e4093b6 9948 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
9949 {
9950 int_const = false;
7d882b83 9951 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
9952 warning (0, "right shift count is negative");
9953 }
3e4093b6 9954 else
bbb818c6 9955 {
3f75a254 9956 if (!integer_zerop (op1))
3e4093b6
RS
9957 short_shift = 1;
9958
9959 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
9960 {
9961 int_const = false;
7d882b83 9962 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
9963 warning (0, "right shift count >= width of type");
9964 }
bbb818c6 9965 }
b62acd60 9966 }
de520661 9967
3e4093b6
RS
9968 /* Use the type of the value to be shifted. */
9969 result_type = type0;
f87bd04b
AS
9970 /* Convert the non vector shift-count to an integer, regardless
9971 of size of value being shifted. */
9972 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
9973 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
9974 op1 = convert (integer_type_node, op1);
9975 /* Avoid converting op1 to result_type later. */
9976 converted = 1;
400fbf9f 9977 }
3e4093b6 9978 break;
253b6b82 9979
3e4093b6 9980 case LSHIFT_EXPR:
f87bd04b
AS
9981 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
9982 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
9983 {
9984 result_type = type0;
9985 converted = 1;
9986 }
9987 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
9988 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
9989 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
9990 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
9991 {
9992 result_type = type0;
9993 converted = 1;
9994 }
9995 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 9996 && code1 == INTEGER_TYPE)
3e4093b6 9997 {
928c19bb 9998 if (TREE_CODE (op1) == INTEGER_CST)
de520661 9999 {
3e4093b6 10000 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10001 {
10002 int_const = false;
7d882b83 10003 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10004 warning (0, "left shift count is negative");
10005 }
de520661 10006
3e4093b6 10007 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10008 {
10009 int_const = false;
7d882b83 10010 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10011 warning (0, "left shift count >= width of type");
10012 }
94ba5069 10013 }
de520661 10014
3e4093b6
RS
10015 /* Use the type of the value to be shifted. */
10016 result_type = type0;
f87bd04b
AS
10017 /* Convert the non vector shift-count to an integer, regardless
10018 of size of value being shifted. */
10019 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10020 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10021 op1 = convert (integer_type_node, op1);
10022 /* Avoid converting op1 to result_type later. */
10023 converted = 1;
400fbf9f 10024 }
3e4093b6 10025 break;
de520661 10026
3e4093b6
RS
10027 case EQ_EXPR:
10028 case NE_EXPR:
d246ab4f
AS
10029 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10030 {
10031 tree intt;
10032 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10033 {
10034 error_at (location, "comparing vectors with different "
10035 "element types");
10036 return error_mark_node;
10037 }
10038
10039 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10040 {
10041 error_at (location, "comparing vectors with different "
10042 "number of elements");
10043 return error_mark_node;
10044 }
10045
10046 /* Always construct signed integer vector type. */
10047 intt = c_common_type_for_size (GET_MODE_BITSIZE
10048 (TYPE_MODE (TREE_TYPE (type0))), 0);
10049 result_type = build_opaque_vector_type (intt,
10050 TYPE_VECTOR_SUBPARTS (type0));
10051 converted = 1;
10052 break;
10053 }
ae311566 10054 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10055 warning_at (location,
10056 OPT_Wfloat_equal,
10057 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10058 /* Result of comparison is always int,
10059 but don't convert the args to int! */
10060 build_type = integer_type_node;
10061 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10062 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10063 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10064 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10065 short_compare = 1;
637f1455
SZ
10066 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10067 {
10068 if (TREE_CODE (op0) == ADDR_EXPR
10069 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10070 {
10071 if (code == EQ_EXPR)
10072 warning_at (location,
10073 OPT_Waddress,
10074 "the comparison will always evaluate as %<false%> "
10075 "for the address of %qD will never be NULL",
10076 TREE_OPERAND (op0, 0));
10077 else
10078 warning_at (location,
10079 OPT_Waddress,
10080 "the comparison will always evaluate as %<true%> "
10081 "for the address of %qD will never be NULL",
10082 TREE_OPERAND (op0, 0));
10083 }
10084 result_type = type0;
10085 }
10086 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10087 {
10088 if (TREE_CODE (op1) == ADDR_EXPR
10089 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10090 {
10091 if (code == EQ_EXPR)
10092 warning_at (location,
f90e8e2e 10093 OPT_Waddress,
637f1455
SZ
10094 "the comparison will always evaluate as %<false%> "
10095 "for the address of %qD will never be NULL",
10096 TREE_OPERAND (op1, 0));
10097 else
10098 warning_at (location,
10099 OPT_Waddress,
10100 "the comparison will always evaluate as %<true%> "
10101 "for the address of %qD will never be NULL",
10102 TREE_OPERAND (op1, 0));
10103 }
10104 result_type = type1;
10105 }
3e4093b6
RS
10106 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10107 {
10108 tree tt0 = TREE_TYPE (type0);
10109 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10110 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10111 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10112 addr_space_t as_common = ADDR_SPACE_GENERIC;
10113
3e4093b6
RS
10114 /* Anything compares with void *. void * compares with anything.
10115 Otherwise, the targets must be compatible
10116 and both must be object or both incomplete. */
744aa42f 10117 if (comp_target_types (location, type0, type1))
10bc1b1b 10118 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10119 else if (!addr_space_superset (as0, as1, &as_common))
10120 {
10121 error_at (location, "comparison of pointers to "
10122 "disjoint address spaces");
10123 return error_mark_node;
10124 }
3e4093b6 10125 else if (VOID_TYPE_P (tt0))
ee2990e7 10126 {
36c5e70a 10127 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10128 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10129 "comparison of %<void *%> with function pointer");
ee2990e7 10130 }
3e4093b6 10131 else if (VOID_TYPE_P (tt1))
e6834654 10132 {
36c5e70a 10133 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10134 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10135 "comparison of %<void *%> with function pointer");
e6834654 10136 }
3e4093b6 10137 else
58393038
ZL
10138 /* Avoid warning about the volatile ObjC EH puts on decls. */
10139 if (!objc_ok)
ba47d38d 10140 pedwarn (location, 0,
509c9d60 10141 "comparison of distinct pointer types lacks a cast");
e6834654 10142
3e4093b6 10143 if (result_type == NULL_TREE)
36c5e70a
BE
10144 {
10145 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10146 result_type = build_pointer_type
10147 (build_qualified_type (void_type_node, qual));
10148 }
e6834654 10149 }
3e4093b6 10150 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10151 {
3e4093b6 10152 result_type = type0;
ba47d38d 10153 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10154 }
3e4093b6 10155 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10156 {
3e4093b6 10157 result_type = type1;
ba47d38d 10158 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10159 }
3e4093b6 10160 break;
8b6a5902 10161
3e4093b6
RS
10162 case LE_EXPR:
10163 case GE_EXPR:
10164 case LT_EXPR:
10165 case GT_EXPR:
d246ab4f
AS
10166 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10167 {
10168 tree intt;
10169 if (TREE_TYPE (type0) != TREE_TYPE (type1))
10170 {
10171 error_at (location, "comparing vectors with different "
10172 "element types");
10173 return error_mark_node;
10174 }
10175
10176 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10177 {
10178 error_at (location, "comparing vectors with different "
10179 "number of elements");
10180 return error_mark_node;
10181 }
10182
10183 /* Always construct signed integer vector type. */
10184 intt = c_common_type_for_size (GET_MODE_BITSIZE
10185 (TYPE_MODE (TREE_TYPE (type0))), 0);
10186 result_type = build_opaque_vector_type (intt,
10187 TYPE_VECTOR_SUBPARTS (type0));
10188 converted = 1;
10189 break;
10190 }
3e4093b6 10191 build_type = integer_type_node;
ab22c1fa
CF
10192 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10193 || code0 == FIXED_POINT_TYPE)
10194 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10195 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10196 short_compare = 1;
10197 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10198 {
36c5e70a
BE
10199 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10200 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10201 addr_space_t as_common;
10202
744aa42f 10203 if (comp_target_types (location, type0, type1))
3e4093b6 10204 {
10bc1b1b 10205 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10206 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10207 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10208 pedwarn (location, 0,
509c9d60 10209 "comparison of complete and incomplete pointers");
fcf73884 10210 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10211 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10212 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10213 else if (null_pointer_constant_p (orig_op0)
10214 || null_pointer_constant_p (orig_op1))
10215 warning_at (location, OPT_Wextra,
10216 "ordered comparison of pointer with null pointer");
10217
3e4093b6 10218 }
36c5e70a
BE
10219 else if (!addr_space_superset (as0, as1, &as_common))
10220 {
10221 error_at (location, "comparison of pointers to "
10222 "disjoint address spaces");
10223 return error_mark_node;
10224 }
3e4093b6
RS
10225 else
10226 {
36c5e70a
BE
10227 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10228 result_type = build_pointer_type
10229 (build_qualified_type (void_type_node, qual));
ba47d38d 10230 pedwarn (location, 0,
509c9d60 10231 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10232 }
10233 }
6aa3c60d 10234 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10235 {
10236 result_type = type0;
fcf73884 10237 if (pedantic)
c1771a20 10238 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10239 "ordered comparison of pointer with integer zero");
10240 else if (extra_warnings)
ba47d38d 10241 warning_at (location, OPT_Wextra,
d42ba3b8 10242 "ordered comparison of pointer with integer zero");
3e4093b6 10243 }
6aa3c60d 10244 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10245 {
10246 result_type = type1;
d42ba3b8 10247 if (pedantic)
c1771a20 10248 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10249 "ordered comparison of pointer with integer zero");
10250 else if (extra_warnings)
10251 warning_at (location, OPT_Wextra,
10252 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10253 }
10254 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10255 {
10256 result_type = type0;
ba47d38d 10257 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10258 }
10259 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10260 {
10261 result_type = type1;
ba47d38d 10262 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10263 }
10264 break;
64094f6a 10265
3e4093b6 10266 default:
37b2f290 10267 gcc_unreachable ();
c9fe6f9f 10268 }
8f17b5c5 10269
e57e265b
PB
10270 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10271 return error_mark_node;
10272
5bed876a
AH
10273 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10274 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
10275 || !same_scalar_type_ignoring_signedness (TREE_TYPE (type0),
10276 TREE_TYPE (type1))))
10277 {
ba47d38d 10278 binary_op_error (location, code, type0, type1);
5bed876a
AH
10279 return error_mark_node;
10280 }
10281
3e4093b6 10282 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10283 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10284 &&
10285 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10286 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10287 {
2ca862e9
JM
10288 bool first_complex = (code0 == COMPLEX_TYPE);
10289 bool second_complex = (code1 == COMPLEX_TYPE);
10290 int none_complex = (!first_complex && !second_complex);
39b726dd 10291
3e4093b6 10292 if (shorten || common || short_compare)
3bf6bfcc
JJ
10293 {
10294 result_type = c_common_type (type0, type1);
0a0b3574
MM
10295 do_warn_double_promotion (result_type, type0, type1,
10296 "implicit conversion from %qT to %qT "
10297 "to match other operand of binary "
10298 "expression",
10299 location);
3bf6bfcc
JJ
10300 if (result_type == error_mark_node)
10301 return error_mark_node;
10302 }
400fbf9f 10303
2ca862e9
JM
10304 if (first_complex != second_complex
10305 && (code == PLUS_EXPR
10306 || code == MINUS_EXPR
10307 || code == MULT_EXPR
10308 || (code == TRUNC_DIV_EXPR && first_complex))
10309 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10310 && flag_signed_zeros)
10311 {
10312 /* An operation on mixed real/complex operands must be
10313 handled specially, but the language-independent code can
10314 more easily optimize the plain complex arithmetic if
10315 -fno-signed-zeros. */
10316 tree real_type = TREE_TYPE (result_type);
10317 tree real, imag;
10318 if (type0 != orig_type0 || type1 != orig_type1)
10319 {
10320 gcc_assert (may_need_excess_precision && common);
2d2e923f 10321 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10322 }
10323 if (first_complex)
10324 {
10325 if (TREE_TYPE (op0) != result_type)
10326 op0 = convert_and_check (result_type, op0);
10327 if (TREE_TYPE (op1) != real_type)
10328 op1 = convert_and_check (real_type, op1);
10329 }
10330 else
10331 {
10332 if (TREE_TYPE (op0) != real_type)
10333 op0 = convert_and_check (real_type, op0);
10334 if (TREE_TYPE (op1) != result_type)
10335 op1 = convert_and_check (result_type, op1);
10336 }
10337 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10338 return error_mark_node;
10339 if (first_complex)
10340 {
10341 op0 = c_save_expr (op0);
10342 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10343 op0, 1);
10344 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10345 op0, 1);
10346 switch (code)
10347 {
10348 case MULT_EXPR:
10349 case TRUNC_DIV_EXPR:
c4603e7c 10350 op1 = c_save_expr (op1);
2ca862e9
JM
10351 imag = build2 (resultcode, real_type, imag, op1);
10352 /* Fall through. */
10353 case PLUS_EXPR:
10354 case MINUS_EXPR:
10355 real = build2 (resultcode, real_type, real, op1);
10356 break;
10357 default:
10358 gcc_unreachable();
10359 }
10360 }
10361 else
10362 {
10363 op1 = c_save_expr (op1);
10364 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10365 op1, 1);
10366 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10367 op1, 1);
10368 switch (code)
10369 {
10370 case MULT_EXPR:
c4603e7c 10371 op0 = c_save_expr (op0);
2ca862e9
JM
10372 imag = build2 (resultcode, real_type, op0, imag);
10373 /* Fall through. */
10374 case PLUS_EXPR:
10375 real = build2 (resultcode, real_type, op0, real);
10376 break;
10377 case MINUS_EXPR:
10378 real = build2 (resultcode, real_type, op0, real);
10379 imag = build1 (NEGATE_EXPR, real_type, imag);
10380 break;
10381 default:
10382 gcc_unreachable();
10383 }
10384 }
10385 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10386 goto return_build_binary_op;
10387 }
10388
3e4093b6
RS
10389 /* For certain operations (which identify themselves by shorten != 0)
10390 if both args were extended from the same smaller type,
10391 do the arithmetic in that type and then extend.
400fbf9f 10392
3e4093b6
RS
10393 shorten !=0 and !=1 indicates a bitwise operation.
10394 For them, this optimization is safe only if
10395 both args are zero-extended or both are sign-extended.
10396 Otherwise, we might change the result.
10397 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10398 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10399
3e4093b6
RS
10400 if (shorten && none_complex)
10401 {
3e4093b6 10402 final_type = result_type;
b8698a0f 10403 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10404 shorten == -1);
3e4093b6 10405 }
88a3dbc1 10406
3e4093b6 10407 /* Shifts can be shortened if shifting right. */
2f6e4e97 10408
3e4093b6
RS
10409 if (short_shift)
10410 {
10411 int unsigned_arg;
10412 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10413
3e4093b6 10414 final_type = result_type;
abe80e6d 10415
3e4093b6 10416 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10417 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10418
3e4093b6 10419 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10420 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10421 /* We can shorten only if the shift count is less than the
10422 number of bits in the smaller type size. */
10423 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10424 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10425 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10426 {
10427 /* Do an unsigned shift if the operand was zero-extended. */
10428 result_type
10429 = c_common_signed_or_unsigned_type (unsigned_arg,
10430 TREE_TYPE (arg0));
10431 /* Convert value-to-be-shifted to that type. */
10432 if (TREE_TYPE (op0) != result_type)
10433 op0 = convert (result_type, op0);
10434 converted = 1;
abe80e6d 10435 }
88a3dbc1
RK
10436 }
10437
3e4093b6
RS
10438 /* Comparison operations are shortened too but differently.
10439 They identify themselves by setting short_compare = 1. */
56cb9733 10440
3e4093b6
RS
10441 if (short_compare)
10442 {
10443 /* Don't write &op0, etc., because that would prevent op0
10444 from being kept in a register.
10445 Instead, make copies of the our local variables and
10446 pass the copies by reference, then copy them back afterward. */
10447 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10448 enum tree_code xresultcode = resultcode;
10449 tree val
10450 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 10451
3e4093b6 10452 if (val != 0)
c9f9eb5d
AH
10453 {
10454 ret = val;
10455 goto return_build_binary_op;
10456 }
8f17b5c5 10457
3e4093b6
RS
10458 op0 = xop0, op1 = xop1;
10459 converted = 1;
10460 resultcode = xresultcode;
8f17b5c5 10461
7d882b83 10462 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10463 {
10464 bool op0_maybe_const = true;
10465 bool op1_maybe_const = true;
10466 tree orig_op0_folded, orig_op1_folded;
10467
10468 if (in_late_binary_op)
10469 {
10470 orig_op0_folded = orig_op0;
10471 orig_op1_folded = orig_op1;
10472 }
10473 else
10474 {
10475 /* Fold for the sake of possible warnings, as in
10476 build_conditional_expr. This requires the
10477 "original" values to be folded, not just op0 and
10478 op1. */
f5178456 10479 c_inhibit_evaluation_warnings++;
928c19bb
JM
10480 op0 = c_fully_fold (op0, require_constant_value,
10481 &op0_maybe_const);
10482 op1 = c_fully_fold (op1, require_constant_value,
10483 &op1_maybe_const);
f5178456 10484 c_inhibit_evaluation_warnings--;
928c19bb
JM
10485 orig_op0_folded = c_fully_fold (orig_op0,
10486 require_constant_value,
10487 NULL);
10488 orig_op1_folded = c_fully_fold (orig_op1,
10489 require_constant_value,
10490 NULL);
10491 }
10492
10493 if (warn_sign_compare)
10494 warn_for_sign_compare (location, orig_op0_folded,
10495 orig_op1_folded, op0, op1,
10496 result_type, resultcode);
5c2f94b4 10497 if (!in_late_binary_op && !int_operands)
928c19bb
JM
10498 {
10499 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 10500 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 10501 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 10502 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 10503 }
3e4093b6 10504 }
2ad1815d 10505 }
64094f6a 10506 }
64094f6a 10507
3e4093b6
RS
10508 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10509 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10510 Then the expression will be built.
10511 It will be given type FINAL_TYPE if that is nonzero;
10512 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 10513
3e4093b6
RS
10514 if (!result_type)
10515 {
ba47d38d 10516 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
10517 return error_mark_node;
10518 }
400fbf9f 10519
3e4093b6 10520 if (build_type == NULL_TREE)
8ce94e44
JM
10521 {
10522 build_type = result_type;
180f8dbb
JM
10523 if ((type0 != orig_type0 || type1 != orig_type1)
10524 && !boolean_op)
8ce94e44
JM
10525 {
10526 gcc_assert (may_need_excess_precision && common);
2d2e923f 10527 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
10528 }
10529 }
400fbf9f 10530
2d2e923f
MLI
10531 if (!converted)
10532 {
10533 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10534 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10535
10536 /* This can happen if one operand has a vector type, and the other
10537 has a different type. */
10538 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10539 return error_mark_node;
10540 }
10541
c9f9eb5d 10542 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
10543 if (int_const_or_overflow)
10544 ret = (require_constant_value
db3927fb
AH
10545 ? fold_build2_initializer_loc (location, resultcode, build_type,
10546 op0, op1)
10547 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
10548 else
10549 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
10550 if (final_type != 0)
10551 ret = convert (final_type, ret);
10552
10553 return_build_binary_op:
10554 gcc_assert (ret != error_mark_node);
928c19bb
JM
10555 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10556 ret = (int_operands
10557 ? note_integer_operands (ret)
10558 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10559 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10560 && !in_late_binary_op)
10561 ret = note_integer_operands (ret);
2d2e923f
MLI
10562 if (semantic_result_type)
10563 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d
AH
10564 protected_set_expr_location (ret, location);
10565 return ret;
400fbf9f 10566}
85498824
JM
10567
10568
10569/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 10570 purpose. LOCATION is the source location for the expression. */
85498824
JM
10571
10572tree
ba47d38d 10573c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 10574{
928c19bb
JM
10575 bool int_const, int_operands;
10576
85498824
JM
10577 switch (TREE_CODE (TREE_TYPE (expr)))
10578 {
10579 case ARRAY_TYPE:
ba47d38d 10580 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
10581 return error_mark_node;
10582
10583 case RECORD_TYPE:
ba47d38d 10584 error_at (location, "used struct type value where scalar is required");
85498824
JM
10585 return error_mark_node;
10586
10587 case UNION_TYPE:
ba47d38d 10588 error_at (location, "used union type value where scalar is required");
85498824
JM
10589 return error_mark_node;
10590
04af8788
NP
10591 case VOID_TYPE:
10592 error_at (location, "void value not ignored as it ought to be");
10593 return error_mark_node;
10594
46bdb9cf
JM
10595 case FUNCTION_TYPE:
10596 gcc_unreachable ();
10597
d246ab4f
AS
10598 case VECTOR_TYPE:
10599 error_at (location, "used vector type where scalar is required");
10600 return error_mark_node;
10601
85498824
JM
10602 default:
10603 break;
10604 }
10605
928c19bb
JM
10606 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
10607 int_operands = EXPR_INT_CONST_OPERANDS (expr);
4d84fe7c
JM
10608 if (int_operands)
10609 expr = remove_c_maybe_const_expr (expr);
928c19bb 10610
04af8788
NP
10611 /* ??? Should we also give an error for vectors rather than leaving
10612 those to give errors later? */
928c19bb
JM
10613 expr = c_common_truthvalue_conversion (location, expr);
10614
10615 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
10616 {
10617 if (TREE_OVERFLOW (expr))
10618 return expr;
10619 else
10620 return note_integer_operands (expr);
10621 }
10622 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
10623 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10624 return expr;
85498824 10625}
73f397d4
JM
10626\f
10627
10628/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
10629 required. */
10630
10631tree
51eed280 10632c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
10633{
10634 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
10635 {
10636 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
10637 /* Executing a compound literal inside a function reinitializes
10638 it. */
10639 if (!TREE_STATIC (decl))
10640 *se = true;
10641 return decl;
10642 }
10643 else
10644 return expr;
10645}
953ff289 10646\f
c0220ea4 10647/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
10648
10649tree
10650c_begin_omp_parallel (void)
10651{
10652 tree block;
10653
10654 keep_next_level ();
10655 block = c_begin_compound_stmt (true);
10656
10657 return block;
10658}
10659
c2255bc4
AH
10660/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
10661 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 10662
953ff289 10663tree
c2255bc4 10664c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
10665{
10666 tree stmt;
10667
c2255bc4 10668 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
10669
10670 stmt = make_node (OMP_PARALLEL);
10671 TREE_TYPE (stmt) = void_type_node;
10672 OMP_PARALLEL_CLAUSES (stmt) = clauses;
10673 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 10674 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
10675
10676 return add_stmt (stmt);
10677}
10678
a68ab351
JJ
10679/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
10680
10681tree
10682c_begin_omp_task (void)
10683{
10684 tree block;
10685
10686 keep_next_level ();
10687 block = c_begin_compound_stmt (true);
10688
10689 return block;
10690}
10691
c2255bc4
AH
10692/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
10693 statement. LOC is the location of the #pragma. */
a68ab351
JJ
10694
10695tree
c2255bc4 10696c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
10697{
10698 tree stmt;
10699
c2255bc4 10700 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
10701
10702 stmt = make_node (OMP_TASK);
10703 TREE_TYPE (stmt) = void_type_node;
10704 OMP_TASK_CLAUSES (stmt) = clauses;
10705 OMP_TASK_BODY (stmt) = block;
c2255bc4 10706 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
10707
10708 return add_stmt (stmt);
10709}
10710
953ff289
DN
10711/* For all elements of CLAUSES, validate them vs OpenMP constraints.
10712 Remove any elements from the list that are invalid. */
10713
10714tree
10715c_finish_omp_clauses (tree clauses)
10716{
10717 bitmap_head generic_head, firstprivate_head, lastprivate_head;
10718 tree c, t, *pc = &clauses;
10719 const char *name;
10720
10721 bitmap_obstack_initialize (NULL);
10722 bitmap_initialize (&generic_head, &bitmap_default_obstack);
10723 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
10724 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
10725
10726 for (pc = &clauses, c = clauses; c ; c = *pc)
10727 {
10728 bool remove = false;
10729 bool need_complete = false;
10730 bool need_implicitly_determined = false;
10731
aaf46ef9 10732 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
10733 {
10734 case OMP_CLAUSE_SHARED:
10735 name = "shared";
10736 need_implicitly_determined = true;
10737 goto check_dup_generic;
10738
10739 case OMP_CLAUSE_PRIVATE:
10740 name = "private";
10741 need_complete = true;
10742 need_implicitly_determined = true;
10743 goto check_dup_generic;
10744
10745 case OMP_CLAUSE_REDUCTION:
10746 name = "reduction";
10747 need_implicitly_determined = true;
10748 t = OMP_CLAUSE_DECL (c);
10749 if (AGGREGATE_TYPE_P (TREE_TYPE (t))
10750 || POINTER_TYPE_P (TREE_TYPE (t)))
10751 {
c2255bc4
AH
10752 error_at (OMP_CLAUSE_LOCATION (c),
10753 "%qE has invalid type for %<reduction%>", t);
953ff289
DN
10754 remove = true;
10755 }
10756 else if (FLOAT_TYPE_P (TREE_TYPE (t)))
10757 {
10758 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
10759 const char *r_name = NULL;
10760
10761 switch (r_code)
10762 {
10763 case PLUS_EXPR:
10764 case MULT_EXPR:
10765 case MINUS_EXPR:
20906c66
JJ
10766 case MIN_EXPR:
10767 case MAX_EXPR:
953ff289
DN
10768 break;
10769 case BIT_AND_EXPR:
10770 r_name = "&";
10771 break;
10772 case BIT_XOR_EXPR:
10773 r_name = "^";
10774 break;
10775 case BIT_IOR_EXPR:
10776 r_name = "|";
10777 break;
10778 case TRUTH_ANDIF_EXPR:
10779 r_name = "&&";
10780 break;
10781 case TRUTH_ORIF_EXPR:
10782 r_name = "||";
10783 break;
10784 default:
10785 gcc_unreachable ();
10786 }
10787 if (r_name)
10788 {
c2255bc4
AH
10789 error_at (OMP_CLAUSE_LOCATION (c),
10790 "%qE has invalid type for %<reduction(%s)%>",
10791 t, r_name);
953ff289
DN
10792 remove = true;
10793 }
10794 }
10795 goto check_dup_generic;
10796
10797 case OMP_CLAUSE_COPYPRIVATE:
10798 name = "copyprivate";
10799 goto check_dup_generic;
10800
10801 case OMP_CLAUSE_COPYIN:
10802 name = "copyin";
10803 t = OMP_CLAUSE_DECL (c);
10804 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
10805 {
c2255bc4
AH
10806 error_at (OMP_CLAUSE_LOCATION (c),
10807 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289
DN
10808 remove = true;
10809 }
10810 goto check_dup_generic;
10811
10812 check_dup_generic:
10813 t = OMP_CLAUSE_DECL (c);
10814 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10815 {
c2255bc4
AH
10816 error_at (OMP_CLAUSE_LOCATION (c),
10817 "%qE is not a variable in clause %qs", t, name);
953ff289
DN
10818 remove = true;
10819 }
10820 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10821 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
10822 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10823 {
c2255bc4
AH
10824 error_at (OMP_CLAUSE_LOCATION (c),
10825 "%qE appears more than once in data clauses", t);
953ff289
DN
10826 remove = true;
10827 }
10828 else
10829 bitmap_set_bit (&generic_head, DECL_UID (t));
10830 break;
10831
10832 case OMP_CLAUSE_FIRSTPRIVATE:
10833 name = "firstprivate";
10834 t = OMP_CLAUSE_DECL (c);
10835 need_complete = true;
10836 need_implicitly_determined = true;
10837 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10838 {
c2255bc4
AH
10839 error_at (OMP_CLAUSE_LOCATION (c),
10840 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
10841 remove = true;
10842 }
10843 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10844 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
10845 {
c2255bc4
AH
10846 error_at (OMP_CLAUSE_LOCATION (c),
10847 "%qE appears more than once in data clauses", t);
953ff289
DN
10848 remove = true;
10849 }
10850 else
10851 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
10852 break;
10853
10854 case OMP_CLAUSE_LASTPRIVATE:
10855 name = "lastprivate";
10856 t = OMP_CLAUSE_DECL (c);
10857 need_complete = true;
10858 need_implicitly_determined = true;
10859 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
10860 {
c2255bc4
AH
10861 error_at (OMP_CLAUSE_LOCATION (c),
10862 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
10863 remove = true;
10864 }
10865 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
10866 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
10867 {
c2255bc4
AH
10868 error_at (OMP_CLAUSE_LOCATION (c),
10869 "%qE appears more than once in data clauses", t);
953ff289
DN
10870 remove = true;
10871 }
10872 else
10873 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
10874 break;
10875
10876 case OMP_CLAUSE_IF:
10877 case OMP_CLAUSE_NUM_THREADS:
10878 case OMP_CLAUSE_SCHEDULE:
10879 case OMP_CLAUSE_NOWAIT:
10880 case OMP_CLAUSE_ORDERED:
10881 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
10882 case OMP_CLAUSE_UNTIED:
10883 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
10884 case OMP_CLAUSE_FINAL:
10885 case OMP_CLAUSE_MERGEABLE:
953ff289
DN
10886 pc = &OMP_CLAUSE_CHAIN (c);
10887 continue;
10888
10889 default:
10890 gcc_unreachable ();
10891 }
10892
10893 if (!remove)
10894 {
10895 t = OMP_CLAUSE_DECL (c);
10896
10897 if (need_complete)
10898 {
10899 t = require_complete_type (t);
10900 if (t == error_mark_node)
10901 remove = true;
10902 }
10903
10904 if (need_implicitly_determined)
10905 {
10906 const char *share_name = NULL;
10907
10908 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
10909 share_name = "threadprivate";
10910 else switch (c_omp_predetermined_sharing (t))
10911 {
10912 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
10913 break;
10914 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
10915 /* const vars may be specified in firstprivate clause. */
10916 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
10917 && TREE_READONLY (t))
10918 break;
953ff289
DN
10919 share_name = "shared";
10920 break;
10921 case OMP_CLAUSE_DEFAULT_PRIVATE:
10922 share_name = "private";
10923 break;
10924 default:
10925 gcc_unreachable ();
10926 }
10927 if (share_name)
10928 {
c2255bc4
AH
10929 error_at (OMP_CLAUSE_LOCATION (c),
10930 "%qE is predetermined %qs for %qs",
10931 t, share_name, name);
953ff289
DN
10932 remove = true;
10933 }
10934 }
10935 }
10936
10937 if (remove)
10938 *pc = OMP_CLAUSE_CHAIN (c);
10939 else
10940 pc = &OMP_CLAUSE_CHAIN (c);
10941 }
10942
10943 bitmap_obstack_release (NULL);
10944 return clauses;
10945}
9ae165a0 10946
0a35513e
AH
10947/* Create a transaction node. */
10948
10949tree
10950c_finish_transaction (location_t loc, tree block, int flags)
10951{
10952 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
10953 if (flags & TM_STMT_ATTR_OUTER)
10954 TRANSACTION_EXPR_OUTER (stmt) = 1;
10955 if (flags & TM_STMT_ATTR_RELAXED)
10956 TRANSACTION_EXPR_RELAXED (stmt) = 1;
10957 return add_stmt (stmt);
10958}
10959
9ae165a0
DG
10960/* Make a variant type in the proper way for C/C++, propagating qualifiers
10961 down to the element type of an array. */
10962
10963tree
10964c_build_qualified_type (tree type, int type_quals)
10965{
10966 if (type == error_mark_node)
10967 return type;
10968
10969 if (TREE_CODE (type) == ARRAY_TYPE)
10970 {
10971 tree t;
10972 tree element_type = c_build_qualified_type (TREE_TYPE (type),
10973 type_quals);
10974
10975 /* See if we already have an identically qualified type. */
10976 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
10977 {
10978 if (TYPE_QUALS (strip_array_types (t)) == type_quals
10979 && TYPE_NAME (t) == TYPE_NAME (type)
10980 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
10981 && attribute_list_equal (TYPE_ATTRIBUTES (t),
10982 TYPE_ATTRIBUTES (type)))
10983 break;
10984 }
10985 if (!t)
10986 {
10987 tree domain = TYPE_DOMAIN (type);
10988
10989 t = build_variant_type_copy (type);
10990 TREE_TYPE (t) = element_type;
10991
10992 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
10993 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
10994 SET_TYPE_STRUCTURAL_EQUALITY (t);
10995 else if (TYPE_CANONICAL (element_type) != element_type
10996 || (domain && TYPE_CANONICAL (domain) != domain))
10997 {
b8698a0f 10998 tree unqualified_canon
9ae165a0 10999 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 11000 domain? TYPE_CANONICAL (domain)
9ae165a0 11001 : NULL_TREE);
b8698a0f 11002 TYPE_CANONICAL (t)
9ae165a0
DG
11003 = c_build_qualified_type (unqualified_canon, type_quals);
11004 }
11005 else
11006 TYPE_CANONICAL (t) = t;
11007 }
11008 return t;
11009 }
11010
11011 /* A restrict-qualified pointer type must be a pointer to object or
11012 incomplete type. Note that the use of POINTER_TYPE_P also allows
11013 REFERENCE_TYPEs, which is appropriate for C++. */
11014 if ((type_quals & TYPE_QUAL_RESTRICT)
11015 && (!POINTER_TYPE_P (type)
11016 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
11017 {
11018 error ("invalid use of %<restrict%>");
11019 type_quals &= ~TYPE_QUAL_RESTRICT;
11020 }
11021
11022 return build_qualified_type (type, type_quals);
11023}
72b5577d
ILT
11024
11025/* Build a VA_ARG_EXPR for the C parser. */
11026
11027tree
c2255bc4 11028c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
11029{
11030 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
11031 warning_at (loc, OPT_Wc___compat,
11032 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 11033 return build_va_arg (loc, expr, type);
72b5577d 11034}