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