]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR c++/66216 (Defaulted Operators and constructors not working with aligned attribute)
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
5624e564 2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
40e23961 30#include "symtab.h"
40e23961 31#include "alias.h"
400fbf9f 32#include "tree.h"
40e23961 33#include "fold-const.h"
d8a2d370
DN
34#include "stor-layout.h"
35#include "trans-mem.h"
36#include "varasm.h"
37#include "stmt.h"
e57e265b 38#include "langhooks.h"
400fbf9f 39#include "c-tree.h"
29cc57cf 40#include "c-lang.h"
400fbf9f 41#include "flags.h"
ab87f8c8 42#include "intl.h"
672a6f42 43#include "target.h"
325c3691 44#include "tree-iterator.h"
6662d794 45#include "bitmap.h"
60393bbc 46#include "predict.h"
60393bbc 47#include "hard-reg-set.h"
60393bbc 48#include "function.h"
2fb9a547 49#include "gimple-expr.h"
45b0be94 50#include "gimplify.h"
acf0174b 51#include "tree-inline.h"
0645c1a2 52#include "omp-low.h"
61d3ce20 53#include "c-family/c-objc.h"
a212e43f 54#include "c-family/c-common.h"
de5a5fa1 55#include "c-family/c-ubsan.h"
12893402 56#include "cilk.h"
41dbbb37 57#include "gomp-constants.h"
325c3691 58
2ac2f164
JM
59/* Possible cases of implicit bad conversions. Used to select
60 diagnostic messages in convert_for_assignment. */
61enum impl_conv {
62 ic_argpass,
63 ic_assign,
64 ic_init,
65 ic_return
66};
67
bc4b653b
JM
68/* The level of nesting inside "__alignof__". */
69int in_alignof;
70
71/* The level of nesting inside "sizeof". */
72int in_sizeof;
73
74/* The level of nesting inside "typeof". */
75int in_typeof;
400fbf9f 76
1a4049e7
JJ
77/* The argument of last parsed sizeof expression, only to be tested
78 if expr.original_code == SIZEOF_EXPR. */
79tree c_last_sizeof_arg;
80
9bac5cbb
G
81/* Nonzero if we might need to print a "missing braces around
82 initializer" message within this initializer. */
83static int found_missing_braces;
103b7b17 84
bf730f15
RS
85static int require_constant_value;
86static int require_constant_elements;
87
58f9752a 88static bool null_pointer_constant_p (const_tree);
f55ade6e 89static tree qualify_type (tree, tree);
dc5027f4
JM
90static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
91 bool *);
744aa42f 92static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
93static int function_types_compatible_p (const_tree, const_tree, bool *,
94 bool *);
95static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 96static tree lookup_field (tree, tree);
81e5eca8
MP
97static int convert_arguments (location_t, vec<location_t>, tree,
98 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
99 tree);
db3927fb 100static tree pointer_diff (location_t, tree, tree);
68fca595 101static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 102 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
103static tree valid_compound_expr_initializer (tree, tree);
104static void push_string (const char *);
105static void push_member_name (tree);
f55ade6e
AJ
106static int spelling_length (void);
107static char *print_spelling (char *);
96b40f8d 108static void warning_init (location_t, int, const char *);
c2255bc4 109static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
110static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
111 bool, struct obstack *);
a1e3b3d9 112static void output_pending_init_elements (int, struct obstack *);
ea58ef42 113static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 114static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
115static void add_pending_init (location_t, tree, tree, tree, bool,
116 struct obstack *);
a1e3b3d9
LB
117static void set_nonincremental_init (struct obstack *);
118static void set_nonincremental_init_from_string (tree, struct obstack *);
119static tree find_init_member (tree, struct obstack *);
f37acdf9 120static void readonly_warning (tree, enum lvalue_use);
7bd11157 121static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 122static void record_maybe_used_decl (tree);
dc5027f4 123static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
124\f
125/* Return true if EXP is a null pointer constant, false otherwise. */
126
127static bool
58f9752a 128null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
129{
130 /* This should really operate on c_expr structures, but they aren't
131 yet available everywhere required. */
132 tree type = TREE_TYPE (expr);
133 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 134 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
135 && integer_zerop (expr)
136 && (INTEGRAL_TYPE_P (type)
137 || (TREE_CODE (type) == POINTER_TYPE
138 && VOID_TYPE_P (TREE_TYPE (type))
139 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
140}
928c19bb
JM
141
142/* EXPR may appear in an unevaluated part of an integer constant
143 expression, but not in an evaluated part. Wrap it in a
144 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
145 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
146
147static tree
148note_integer_operands (tree expr)
149{
150 tree ret;
151 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
152 {
153 ret = copy_node (expr);
154 TREE_OVERFLOW (ret) = 1;
155 }
156 else
157 {
158 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
159 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
160 }
161 return ret;
162}
163
4d84fe7c
JM
164/* Having checked whether EXPR may appear in an unevaluated part of an
165 integer constant expression and found that it may, remove any
166 C_MAYBE_CONST_EXPR noting this fact and return the resulting
167 expression. */
168
169static inline tree
170remove_c_maybe_const_expr (tree expr)
171{
172 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
173 return C_MAYBE_CONST_EXPR_EXPR (expr);
174 else
175 return expr;
176}
177
f13c9b2c
AP
178\f/* This is a cache to hold if two types are compatible or not. */
179
180struct tagged_tu_seen_cache {
181 const struct tagged_tu_seen_cache * next;
58f9752a
KG
182 const_tree t1;
183 const_tree t2;
f13c9b2c
AP
184 /* The return value of tagged_types_tu_compatible_p if we had seen
185 these two types already. */
186 int val;
187};
188
189static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
190static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
191
400fbf9f
JW
192/* Do `exp = require_complete_type (exp);' to make sure exp
193 does not have an incomplete type. (That includes void types.) */
194
195tree
2f6e4e97 196require_complete_type (tree value)
400fbf9f
JW
197{
198 tree type = TREE_TYPE (value);
199
7a0ca710 200 if (error_operand_p (value))
ea0f786b
CB
201 return error_mark_node;
202
400fbf9f 203 /* First, detect a valid value with a complete type. */
d0f062fb 204 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
205 return value;
206
7a228918 207 c_incomplete_type_error (value, type);
400fbf9f
JW
208 return error_mark_node;
209}
210
211/* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. */
214
215void
ac7d7749 216c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 217{
400fbf9f
JW
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
221
0ae9bd27 222 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
ac9f18db 223 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
224 else
225 {
226 retry:
227 /* We must print an error message. Be clever about what it says. */
228
229 switch (TREE_CODE (type))
230 {
231 case RECORD_TYPE:
400fbf9f 232 case UNION_TYPE:
400fbf9f 233 case ENUMERAL_TYPE:
400fbf9f
JW
234 break;
235
236 case VOID_TYPE:
237 error ("invalid use of void expression");
238 return;
239
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
242 {
fba78abb
RH
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 {
245 error ("invalid use of flexible array member");
246 return;
247 }
400fbf9f
JW
248 type = TREE_TYPE (type);
249 goto retry;
250 }
251 error ("invalid use of array with unspecified bounds");
252 return;
253
254 default:
366de0ce 255 gcc_unreachable ();
400fbf9f
JW
256 }
257
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 259 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 262 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
263 }
264}
265
ab393bf1
NB
266/* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
268
269tree
2f6e4e97 270c_type_promotes_to (tree type)
ab393bf1 271{
267bac10 272 tree ret = NULL_TREE;
ab393bf1 273
267bac10
JM
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
277 {
278 /* Preserve unsignedness if not really getting any wider. */
8df83eae 279 if (TYPE_UNSIGNED (type)
c22cacf3 280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
ab393bf1
NB
284 }
285
267bac10
JM
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
290
ab393bf1
NB
291 return type;
292}
293
36c5e70a
BE
294/* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
297
298static bool
299addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
300{
301 if (as1 == as2)
302 {
303 *common = as1;
304 return true;
305 }
306 else if (targetm.addr_space.subset_p (as1, as2))
307 {
308 *common = as2;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as2, as1))
312 {
313 *common = as1;
314 return true;
315 }
316 else
317 return false;
318}
319
400fbf9f
JW
320/* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
322
323static tree
2f6e4e97 324qualify_type (tree type, tree like)
400fbf9f 325{
36c5e70a
BE
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
329
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
333 {
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
337 }
338
2f6e4e97 339 return c_build_qualified_type (type,
36c5e70a 340 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 342 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 343}
52ffd86e
MS
344
345/* Return true iff the given tree T is a variable length array. */
346
347bool
ac7d7749 348c_vla_type_p (const_tree t)
52ffd86e
MS
349{
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
354}
400fbf9f 355\f
10bc1b1b 356/* Return the composite type of two compatible types.
5305f6d7 357
10bc1b1b
JM
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
400fbf9f
JW
361
362tree
10bc1b1b 363composite_type (tree t1, tree t2)
400fbf9f 364{
b3694847
SS
365 enum tree_code code1;
366 enum tree_code code2;
4b027d16 367 tree attributes;
400fbf9f
JW
368
369 /* Save time if the two types are the same. */
370
371 if (t1 == t2) return t1;
372
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
378
10bc1b1b
JM
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
381
d9525bec 382 /* Merge the attributes. */
5fd9b178 383 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 384
10bc1b1b
JM
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
400fbf9f 389
10bc1b1b
JM
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
75326e8c 394
366de0ce 395 gcc_assert (code1 == code2);
b6a10c9f 396
400fbf9f
JW
397 switch (code1)
398 {
400fbf9f 399 case POINTER_TYPE:
10bc1b1b 400 /* For two pointers, do this recursively on the target type. */
400fbf9f 401 {
3932261a
MM
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 404 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
400fbf9f 408 }
400fbf9f
JW
409
410 case ARRAY_TYPE:
411 {
10bc1b1b 412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
413 int quals;
414 tree unqual_elt;
ca8bdb78
JM
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
f6294de7 419 bool t1_complete, t2_complete;
46df2823 420
de46b2fe 421 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 424
f6294de7
JM
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
427
ca8bdb78
JM
428 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
430
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 439
400fbf9f 440 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
4b027d16 443 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
4b027d16 446 return build_type_attribute_variant (t2, attributes);
c22cacf3 447
de46b2fe
AP
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
c22cacf3 452
46df2823
JM
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
f6294de7
JM
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
472 {
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
475 }
46df2823 476 t1 = c_build_qualified_type (t1, quals);
de46b2fe 477 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
478 }
479
fcb99e7b
JJ
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
484 {
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
490 }
491 return build_type_attribute_variant (t1, attributes);
492
400fbf9f
JW
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
496 {
10bc1b1b 497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
503
504 /* Save space: see if the result is identical to one of the args. */
3f75a254 505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 506 return build_type_attribute_variant (t1, attributes);
3f75a254 507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 508 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
509
510 /* Simple way if one arg fails to specify argument types. */
511 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 512 {
fe7080d2
AP
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
4b027d16 516 }
400fbf9f 517 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
518 {
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
4b027d16 522 }
400fbf9f
JW
523
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
2f4e8f2b 526
400fbf9f
JW
527 len = list_length (p1);
528 newargs = 0;
529
530 for (i = 0; i < len; i++)
8d9bfdc5 531 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
532
533 n = newargs;
534
535 for (; p1;
536 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
537 {
538 /* A null type means arg type is not specified.
539 Take whatever the other function type has. */
540 if (TREE_VALUE (p1) == 0)
541 {
542 TREE_VALUE (n) = TREE_VALUE (p2);
543 goto parm_done;
544 }
545 if (TREE_VALUE (p2) == 0)
546 {
547 TREE_VALUE (n) = TREE_VALUE (p1);
548 goto parm_done;
549 }
2f6e4e97 550
400fbf9f
JW
551 /* Given wait (union {union wait *u; int *i} *)
552 and wait (union wait *),
553 prefer union wait * as type of parm. */
554 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
555 && TREE_VALUE (p1) != TREE_VALUE (p2))
556 {
557 tree memb;
58cb41e6
JJ
558 tree mv2 = TREE_VALUE (p2);
559 if (mv2 && mv2 != error_mark_node
560 && TREE_CODE (mv2) != ARRAY_TYPE)
561 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 562 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 563 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
564 {
565 tree mv3 = TREE_TYPE (memb);
566 if (mv3 && mv3 != error_mark_node
567 && TREE_CODE (mv3) != ARRAY_TYPE)
568 mv3 = TYPE_MAIN_VARIANT (mv3);
569 if (comptypes (mv3, mv2))
570 {
571 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
572 TREE_VALUE (p2));
c1771a20 573 pedwarn (input_location, OPT_Wpedantic,
fcf73884 574 "function types not truly compatible in ISO C");
58cb41e6
JJ
575 goto parm_done;
576 }
577 }
400fbf9f
JW
578 }
579 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
580 && TREE_VALUE (p2) != TREE_VALUE (p1))
581 {
582 tree memb;
58cb41e6
JJ
583 tree mv1 = TREE_VALUE (p1);
584 if (mv1 && mv1 != error_mark_node
585 && TREE_CODE (mv1) != ARRAY_TYPE)
586 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 587 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 588 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
589 {
590 tree mv3 = TREE_TYPE (memb);
591 if (mv3 && mv3 != error_mark_node
592 && TREE_CODE (mv3) != ARRAY_TYPE)
593 mv3 = TYPE_MAIN_VARIANT (mv3);
594 if (comptypes (mv3, mv1))
595 {
596 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
597 TREE_VALUE (p1));
c1771a20 598 pedwarn (input_location, OPT_Wpedantic,
fcf73884 599 "function types not truly compatible in ISO C");
58cb41e6
JJ
600 goto parm_done;
601 }
602 }
400fbf9f 603 }
10bc1b1b 604 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
605 parm_done: ;
606 }
607
4b027d16 608 t1 = build_function_type (valtype, newargs);
fe7080d2 609 t1 = qualify_type (t1, t2);
0f41302f 610 /* ... falls through ... */
400fbf9f
JW
611 }
612
613 default:
4b027d16 614 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
615 }
616
617}
10bc1b1b
JM
618
619/* Return the type of a conditional expression between pointers to
620 possibly differently qualified versions of compatible types.
621
622 We assume that comp_target_types has already been done and returned
623 nonzero; if that isn't so, this may crash. */
624
625static tree
626common_pointer_type (tree t1, tree t2)
627{
628 tree attributes;
46df2823
JM
629 tree pointed_to_1, mv1;
630 tree pointed_to_2, mv2;
10bc1b1b 631 tree target;
eb1387a0 632 unsigned target_quals;
36c5e70a
BE
633 addr_space_t as1, as2, as_common;
634 int quals1, quals2;
10bc1b1b
JM
635
636 /* Save time if the two types are the same. */
637
638 if (t1 == t2) return t1;
639
640 /* If one type is nonsense, use the other. */
641 if (t1 == error_mark_node)
642 return t2;
643 if (t2 == error_mark_node)
644 return t1;
645
366de0ce 646 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 647 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
648
649 /* Merge the attributes. */
650 attributes = targetm.merge_type_attributes (t1, t2);
651
652 /* Find the composite type of the target types, and combine the
46df2823
JM
653 qualifiers of the two types' targets. Do not lose qualifiers on
654 array element types by taking the TYPE_MAIN_VARIANT. */
655 mv1 = pointed_to_1 = TREE_TYPE (t1);
656 mv2 = pointed_to_2 = TREE_TYPE (t2);
657 if (TREE_CODE (mv1) != ARRAY_TYPE)
658 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
659 if (TREE_CODE (mv2) != ARRAY_TYPE)
660 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
661 target = composite_type (mv1, mv2);
eb1387a0 662
768952be
MU
663 /* Strip array types to get correct qualifier for pointers to arrays */
664 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
665 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
666
eb1387a0
RG
667 /* For function types do not merge const qualifiers, but drop them
668 if used inconsistently. The middle-end uses these to mark const
669 and noreturn functions. */
670 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 671 target_quals = (quals1 & quals2);
eb1387a0 672 else
36c5e70a
BE
673 target_quals = (quals1 | quals2);
674
675 /* If the two named address spaces are different, determine the common
676 superset address space. This is guaranteed to exist due to the
677 assumption that comp_target_type returned non-zero. */
678 as1 = TYPE_ADDR_SPACE (pointed_to_1);
679 as2 = TYPE_ADDR_SPACE (pointed_to_2);
680 if (!addr_space_superset (as1, as2, &as_common))
681 gcc_unreachable ();
682
683 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
684
eb1387a0 685 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
686 return build_type_attribute_variant (t1, attributes);
687}
688
689/* Return the common type for two arithmetic types under the usual
690 arithmetic conversions. The default conversions have already been
691 applied, and enumerated types converted to their compatible integer
692 types. The resulting type is unqualified and has no attributes.
693
694 This is the type for the result of most arithmetic operations
695 if the operands have the given two types. */
696
ccf7f880
JJ
697static tree
698c_common_type (tree t1, tree t2)
10bc1b1b
JM
699{
700 enum tree_code code1;
701 enum tree_code code2;
702
703 /* If one type is nonsense, use the other. */
704 if (t1 == error_mark_node)
705 return t2;
706 if (t2 == error_mark_node)
707 return t1;
708
709 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
710 t1 = TYPE_MAIN_VARIANT (t1);
711
712 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
713 t2 = TYPE_MAIN_VARIANT (t2);
714
715 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
716 t1 = build_type_attribute_variant (t1, NULL_TREE);
717
718 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
719 t2 = build_type_attribute_variant (t2, NULL_TREE);
720
721 /* Save time if the two types are the same. */
722
723 if (t1 == t2) return t1;
724
725 code1 = TREE_CODE (t1);
726 code2 = TREE_CODE (t2);
727
366de0ce 728 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
729 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
730 || code1 == INTEGER_TYPE);
366de0ce 731 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
732 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
733 || code2 == INTEGER_TYPE);
10bc1b1b 734
5fc89bfd
JJ
735 /* When one operand is a decimal float type, the other operand cannot be
736 a generic float type or a complex type. We also disallow vector types
737 here. */
738 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
739 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
740 {
741 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
742 {
743 error ("can%'t mix operands of decimal float and vector types");
744 return error_mark_node;
745 }
746 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
747 {
748 error ("can%'t mix operands of decimal float and complex types");
749 return error_mark_node;
750 }
751 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
752 {
753 error ("can%'t mix operands of decimal float and other float types");
754 return error_mark_node;
755 }
756 }
757
10bc1b1b
JM
758 /* If one type is a vector type, return that type. (How the usual
759 arithmetic conversions apply to the vector types extension is not
760 precisely specified.) */
761 if (code1 == VECTOR_TYPE)
762 return t1;
763
764 if (code2 == VECTOR_TYPE)
765 return t2;
766
767 /* If one type is complex, form the common type of the non-complex
768 components, then make that complex. Use T1 or T2 if it is the
769 required type. */
770 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
771 {
772 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
773 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 774 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
775
776 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
777 return t1;
778 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
779 return t2;
780 else
781 return build_complex_type (subtype);
782 }
783
784 /* If only one is real, use it as the result. */
785
786 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
787 return t1;
788
789 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
790 return t2;
791
9a8ce21f
JG
792 /* If both are real and either are decimal floating point types, use
793 the decimal floating point type with the greater precision. */
794
795 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
796 {
797 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
798 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
799 return dfloat128_type_node;
800 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
802 return dfloat64_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
805 return dfloat32_type_node;
806 }
807
ab22c1fa
CF
808 /* Deal with fixed-point types. */
809 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
810 {
811 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 812 machine_mode m1, m2;
ab22c1fa
CF
813 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
814
815 m1 = TYPE_MODE (t1);
816 m2 = TYPE_MODE (t2);
817
818 /* If one input type is saturating, the result type is saturating. */
819 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
820 satp = 1;
821
822 /* If both fixed-point types are unsigned, the result type is unsigned.
823 When mixing fixed-point and integer types, follow the sign of the
824 fixed-point type.
825 Otherwise, the result type is signed. */
826 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
827 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
828 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
829 && TYPE_UNSIGNED (t1))
830 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t2)))
832 unsignedp = 1;
833
834 /* The result type is signed. */
835 if (unsignedp == 0)
836 {
837 /* If the input type is unsigned, we need to convert to the
838 signed type. */
839 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
840 {
d75d71e0 841 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
842 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
843 mclass = MODE_FRACT;
844 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
845 mclass = MODE_ACCUM;
846 else
847 gcc_unreachable ();
848 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
849 }
850 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
851 {
d75d71e0 852 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
853 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
854 mclass = MODE_FRACT;
855 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
856 mclass = MODE_ACCUM;
857 else
858 gcc_unreachable ();
859 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
860 }
861 }
862
863 if (code1 == FIXED_POINT_TYPE)
864 {
865 fbit1 = GET_MODE_FBIT (m1);
866 ibit1 = GET_MODE_IBIT (m1);
867 }
868 else
869 {
870 fbit1 = 0;
871 /* Signed integers need to subtract one sign bit. */
872 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
873 }
874
875 if (code2 == FIXED_POINT_TYPE)
876 {
877 fbit2 = GET_MODE_FBIT (m2);
878 ibit2 = GET_MODE_IBIT (m2);
879 }
880 else
881 {
882 fbit2 = 0;
883 /* Signed integers need to subtract one sign bit. */
884 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
885 }
886
887 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
888 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
889 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
890 satp);
891 }
892
10bc1b1b
JM
893 /* Both real or both integers; use the one with greater precision. */
894
895 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
896 return t1;
897 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
898 return t2;
899
900 /* Same precision. Prefer long longs to longs to ints when the
901 same precision, following the C99 rules on integer type rank
902 (which are equivalent to the C90 rules for C90 types). */
903
904 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
905 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
906 return long_long_unsigned_type_node;
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
910 {
911 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
912 return long_long_unsigned_type_node;
913 else
c22cacf3 914 return long_long_integer_type_node;
10bc1b1b
JM
915 }
916
917 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
918 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
919 return long_unsigned_type_node;
920
921 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
923 {
924 /* But preserve unsignedness from the other type,
925 since long cannot hold all the values of an unsigned int. */
926 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
927 return long_unsigned_type_node;
928 else
929 return long_integer_type_node;
930 }
931
932 /* Likewise, prefer long double to double even if same size. */
933 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
935 return long_double_type_node;
936
2531a1d9
JR
937 /* Likewise, prefer double to float even if same size.
938 We got a couple of embedded targets with 32 bit doubles, and the
939 pdp11 might have 64 bit floats. */
940 if (TYPE_MAIN_VARIANT (t1) == double_type_node
941 || TYPE_MAIN_VARIANT (t2) == double_type_node)
942 return double_type_node;
943
10bc1b1b
JM
944 /* Otherwise prefer the unsigned one. */
945
946 if (TYPE_UNSIGNED (t1))
947 return t1;
948 else
949 return t2;
950}
400fbf9f 951\f
5922c215
JM
952/* Wrapper around c_common_type that is used by c-common.c and other
953 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
954 are allowed here and are converted to their compatible integer types.
955 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
956 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
957tree
958common_type (tree t1, tree t2)
959{
960 if (TREE_CODE (t1) == ENUMERAL_TYPE)
961 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
962 if (TREE_CODE (t2) == ENUMERAL_TYPE)
963 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
964
965 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
966 if (TREE_CODE (t1) == BOOLEAN_TYPE
967 && TREE_CODE (t2) == BOOLEAN_TYPE)
968 return boolean_type_node;
969
970 /* If either type is BOOLEAN_TYPE, then return the other. */
971 if (TREE_CODE (t1) == BOOLEAN_TYPE)
972 return t2;
973 if (TREE_CODE (t2) == BOOLEAN_TYPE)
974 return t1;
975
ccf7f880
JJ
976 return c_common_type (t1, t2);
977}
f13c9b2c 978
400fbf9f
JW
979/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
980 or various other operations. Return 2 if they are compatible
981 but a warning may be needed if you use them together. */
982
983int
132da1a5 984comptypes (tree type1, tree type2)
f13c9b2c
AP
985{
986 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
987 int val;
988
dc5027f4 989 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
990 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
991
992 return val;
993}
994
995/* Like comptypes, but if it returns non-zero because enum and int are
996 compatible, it sets *ENUM_AND_INT_P to true. */
997
998static int
999comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1000{
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002 int val;
1003
dc5027f4
JM
1004 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1005 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1006
1007 return val;
1008}
1009
1010/* Like comptypes, but if it returns nonzero for different types, it
1011 sets *DIFFERENT_TYPES_P to true. */
1012
1013int
1014comptypes_check_different_types (tree type1, tree type2,
1015 bool *different_types_p)
1016{
1017 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1018 int val;
1019
1020 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1021 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1022
f13c9b2c 1023 return val;
c22cacf3
MS
1024}
1025\f
f13c9b2c
AP
1026/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1027 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1028 but a warning may be needed if you use them together. If
1029 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1030 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1031 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1032 NULL, and the types are compatible but different enough not to be
48b0b196 1033 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1034 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1035 false, but may or may not be set if the types are incompatible.
1036 This differs from comptypes, in that we don't free the seen
1037 types. */
f13c9b2c
AP
1038
1039static int
dc5027f4
JM
1040comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1041 bool *different_types_p)
400fbf9f 1042{
58f9752a
KG
1043 const_tree t1 = type1;
1044 const_tree t2 = type2;
4b027d16 1045 int attrval, val;
400fbf9f
JW
1046
1047 /* Suppress errors caused by previously reported errors. */
1048
8d47dfc5
RH
1049 if (t1 == t2 || !t1 || !t2
1050 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1051 return 1;
1052
bca63328
JM
1053 /* Enumerated types are compatible with integer types, but this is
1054 not transitive: two enumerated types in the same translation unit
1055 are compatible with each other only if they are the same type. */
400fbf9f 1056
bca63328 1057 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1058 {
1059 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1060 if (TREE_CODE (t2) != VOID_TYPE)
1061 {
1062 if (enum_and_int_p != NULL)
1063 *enum_and_int_p = true;
1064 if (different_types_p != NULL)
1065 *different_types_p = true;
1066 }
744aa42f 1067 }
bca63328 1068 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1069 {
1070 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1071 if (TREE_CODE (t1) != VOID_TYPE)
1072 {
1073 if (enum_and_int_p != NULL)
1074 *enum_and_int_p = true;
1075 if (different_types_p != NULL)
1076 *different_types_p = true;
1077 }
744aa42f 1078 }
400fbf9f
JW
1079
1080 if (t1 == t2)
1081 return 1;
1082
1083 /* Different classes of types can't be compatible. */
1084
3aeb3655
EC
1085 if (TREE_CODE (t1) != TREE_CODE (t2))
1086 return 0;
400fbf9f 1087
118a3a8b 1088 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1089
3932261a 1090 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1091 return 0;
1092
08632da2
RS
1093 /* Allow for two different type nodes which have essentially the same
1094 definition. Note that we already checked for equality of the type
38e01259 1095 qualifiers (just above). */
400fbf9f 1096
46df2823
JM
1097 if (TREE_CODE (t1) != ARRAY_TYPE
1098 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1099 return 1;
1100
4b027d16 1101 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1102 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1103 return 0;
1104
1105 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1106 val = 0;
1107
400fbf9f
JW
1108 switch (TREE_CODE (t1))
1109 {
1110 case POINTER_TYPE:
106f5de5
UW
1111 /* Do not remove mode or aliasing information. */
1112 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1113 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1114 break;
4b027d16 1115 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1116 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1117 enum_and_int_p, different_types_p));
4b027d16 1118 break;
400fbf9f
JW
1119
1120 case FUNCTION_TYPE:
dc5027f4
JM
1121 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1122 different_types_p);
4b027d16 1123 break;
400fbf9f
JW
1124
1125 case ARRAY_TYPE:
1126 {
400fbf9f
JW
1127 tree d1 = TYPE_DOMAIN (t1);
1128 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1129 bool d1_variable, d2_variable;
1130 bool d1_zero, d2_zero;
4b027d16 1131 val = 1;
400fbf9f
JW
1132
1133 /* Target types must match incl. qualifiers. */
1134 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1135 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1136 enum_and_int_p,
1137 different_types_p)))
400fbf9f
JW
1138 return 0;
1139
dc5027f4
JM
1140 if (different_types_p != NULL
1141 && (d1 == 0) != (d2 == 0))
1142 *different_types_p = true;
400fbf9f 1143 /* Sizes must match unless one is missing or variable. */
3f85558f 1144 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1145 break;
400fbf9f 1146
3f75a254
JM
1147 d1_zero = !TYPE_MAX_VALUE (d1);
1148 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1149
3f75a254 1150 d1_variable = (!d1_zero
3f85558f
RH
1151 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1152 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1153 d2_variable = (!d2_zero
3f85558f
RH
1154 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1155 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1156 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1157 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1158
dc5027f4
JM
1159 if (different_types_p != NULL
1160 && d1_variable != d2_variable)
1161 *different_types_p = true;
3f85558f
RH
1162 if (d1_variable || d2_variable)
1163 break;
1164 if (d1_zero && d2_zero)
1165 break;
1166 if (d1_zero || d2_zero
3f75a254
JM
1167 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1168 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1169 val = 0;
1170
c22cacf3 1171 break;
400fbf9f
JW
1172 }
1173
d1bd0ded 1174 case ENUMERAL_TYPE:
58393038 1175 case RECORD_TYPE:
d1bd0ded 1176 case UNION_TYPE:
766beae1 1177 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1178 {
fcb99e7b
JJ
1179 tree a1 = TYPE_ATTRIBUTES (t1);
1180 tree a2 = TYPE_ATTRIBUTES (t2);
1181
1182 if (! attribute_list_contained (a1, a2)
1183 && ! attribute_list_contained (a2, a1))
1184 break;
1185
f13c9b2c 1186 if (attrval != 2)
dc5027f4
JM
1187 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1188 different_types_p);
1189 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1190 different_types_p);
f13c9b2c 1191 }
4b027d16 1192 break;
e9a25f70 1193
62e1dfcf 1194 case VECTOR_TYPE:
744aa42f
ILT
1195 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1196 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1197 enum_and_int_p, different_types_p));
62e1dfcf
NC
1198 break;
1199
e9a25f70
JL
1200 default:
1201 break;
400fbf9f 1202 }
4b027d16 1203 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1204}
1205
36c5e70a
BE
1206/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1207 their qualifiers, except for named address spaces. If the pointers point to
1208 different named addresses, then we must determine if one address space is a
1209 subset of the other. */
400fbf9f
JW
1210
1211static int
744aa42f 1212comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1213{
392202b0 1214 int val;
768952be 1215 int val_ped;
36c5e70a
BE
1216 tree mvl = TREE_TYPE (ttl);
1217 tree mvr = TREE_TYPE (ttr);
1218 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1219 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1220 addr_space_t as_common;
744aa42f 1221 bool enum_and_int_p;
8b40563c 1222
36c5e70a
BE
1223 /* Fail if pointers point to incompatible address spaces. */
1224 if (!addr_space_superset (asl, asr, &as_common))
1225 return 0;
1226
768952be
MU
1227 /* For pedantic record result of comptypes on arrays before losing
1228 qualifiers on the element type below. */
1229 val_ped = 1;
1230
1231 if (TREE_CODE (mvl) == ARRAY_TYPE
1232 && TREE_CODE (mvr) == ARRAY_TYPE)
1233 val_ped = comptypes (mvl, mvr);
1234
1235 /* Qualifiers on element types of array types that are
1236 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1237
1238 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1239 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1240 : TYPE_MAIN_VARIANT (mvl));
1241
1242 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1243 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1244 : TYPE_MAIN_VARIANT (mvr));
1245
744aa42f
ILT
1246 enum_and_int_p = false;
1247 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1248
768952be
MU
1249 if (val == 1 && val_ped != 1)
1250 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1251 "are incompatible in ISO C");
1252
fcf73884 1253 if (val == 2)
c1771a20 1254 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1255
1256 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1257 warning_at (location, OPT_Wc___compat,
1258 "pointer target types incompatible in C++");
1259
400fbf9f
JW
1260 return val;
1261}
1262\f
1263/* Subroutines of `comptypes'. */
1264
f75fbaf7
ZW
1265/* Determine whether two trees derive from the same translation unit.
1266 If the CONTEXT chain ends in a null, that tree's context is still
1267 being parsed, so if two trees have context chains ending in null,
766beae1 1268 they're in the same translation unit. */
f75fbaf7 1269int
58f9752a 1270same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1271{
1272 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1273 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1274 {
6615c446
JO
1275 case tcc_declaration:
1276 t1 = DECL_CONTEXT (t1); break;
1277 case tcc_type:
1278 t1 = TYPE_CONTEXT (t1); break;
1279 case tcc_exceptional:
1280 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1281 default: gcc_unreachable ();
766beae1
ZW
1282 }
1283
1284 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1285 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1286 {
6615c446
JO
1287 case tcc_declaration:
1288 t2 = DECL_CONTEXT (t2); break;
1289 case tcc_type:
1290 t2 = TYPE_CONTEXT (t2); break;
1291 case tcc_exceptional:
1292 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1293 default: gcc_unreachable ();
766beae1
ZW
1294 }
1295
1296 return t1 == t2;
1297}
1298
f13c9b2c 1299/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1300
f13c9b2c 1301static struct tagged_tu_seen_cache *
58f9752a 1302alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1303{
cceb1885 1304 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1305 tu->next = tagged_tu_seen_base;
1306 tu->t1 = t1;
1307 tu->t2 = t2;
c22cacf3 1308
f13c9b2c 1309 tagged_tu_seen_base = tu;
c22cacf3 1310
f13c9b2c
AP
1311 /* The C standard says that two structures in different translation
1312 units are compatible with each other only if the types of their
1313 fields are compatible (among other things). We assume that they
1314 are compatible until proven otherwise when building the cache.
1315 An example where this can occur is:
1316 struct a
1317 {
1318 struct a *next;
1319 };
1320 If we are comparing this against a similar struct in another TU,
c83eecad 1321 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1322 loop. */
1323 tu->val = 1;
1324 return tu;
1325}
d1bd0ded 1326
f13c9b2c 1327/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1328
f13c9b2c
AP
1329static void
1330free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1331{
1332 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1333 while (tu != tu_til)
1334 {
741ac903
KG
1335 const struct tagged_tu_seen_cache *const tu1
1336 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1337 tu = tu1->next;
b1d5455a 1338 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1339 }
1340 tagged_tu_seen_base = tu_til;
1341}
d1bd0ded
GK
1342
1343/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1344 compatible. If the two types are not the same (which has been
1345 checked earlier), this can only happen when multiple translation
1346 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1347 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1348 comptypes_internal. */
d1bd0ded
GK
1349
1350static int
744aa42f 1351tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1352 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1353{
1354 tree s1, s2;
1355 bool needs_warning = false;
3aeb3655 1356
d1bd0ded
GK
1357 /* We have to verify that the tags of the types are the same. This
1358 is harder than it looks because this may be a typedef, so we have
1359 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1360 typedef...
1361 In the case of compiler-created builtin structs the TYPE_DECL
1362 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1363 while (TYPE_NAME (t1)
1364 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1365 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1366 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1367
dea984dc
ILT
1368 while (TYPE_NAME (t2)
1369 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1370 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1371 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1372
1373 /* C90 didn't have the requirement that the two tags be the same. */
1374 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1375 return 0;
3aeb3655 1376
d1bd0ded
GK
1377 /* C90 didn't say what happened if one or both of the types were
1378 incomplete; we choose to follow C99 rules here, which is that they
1379 are compatible. */
1380 if (TYPE_SIZE (t1) == NULL
1381 || TYPE_SIZE (t2) == NULL)
1382 return 1;
3aeb3655 1383
d1bd0ded 1384 {
f13c9b2c 1385 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1386 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1387 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1388 return tts_i->val;
d1bd0ded 1389 }
3aeb3655 1390
d1bd0ded
GK
1391 switch (TREE_CODE (t1))
1392 {
1393 case ENUMERAL_TYPE:
1394 {
f13c9b2c 1395 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1396 /* Speed up the case where the type values are in the same order. */
1397 tree tv1 = TYPE_VALUES (t1);
1398 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1399
c22cacf3 1400 if (tv1 == tv2)
f13c9b2c
AP
1401 {
1402 return 1;
1403 }
3aeb3655 1404
c22cacf3
MS
1405 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1406 {
1407 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1408 break;
1409 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1410 {
c22cacf3 1411 tu->val = 0;
f13c9b2c
AP
1412 return 0;
1413 }
c22cacf3 1414 }
3aeb3655 1415
c22cacf3 1416 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1417 {
1418 return 1;
1419 }
c22cacf3 1420 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1421 {
1422 tu->val = 0;
1423 return 0;
1424 }
3aeb3655 1425
d1bd0ded 1426 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1427 {
1428 tu->val = 0;
1429 return 0;
1430 }
3aeb3655 1431
d1bd0ded
GK
1432 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1433 {
1434 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1435 if (s2 == NULL
1436 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1437 {
1438 tu->val = 0;
1439 return 0;
1440 }
d1bd0ded
GK
1441 }
1442 return 1;
1443 }
1444
1445 case UNION_TYPE:
1446 {
f13c9b2c 1447 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1448 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1449 {
1450 tu->val = 0;
1451 return 0;
1452 }
c22cacf3 1453
f13c9b2c
AP
1454 /* Speed up the common case where the fields are in the same order. */
1455 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1456 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1457 {
1458 int result;
c22cacf3 1459
3ae4d3cc 1460 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1461 break;
744aa42f 1462 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1463 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1464
1465 if (result != 1 && !DECL_NAME (s1))
1466 break;
f13c9b2c
AP
1467 if (result == 0)
1468 {
1469 tu->val = 0;
1470 return 0;
1471 }
1472 if (result == 2)
1473 needs_warning = true;
1474
1475 if (TREE_CODE (s1) == FIELD_DECL
1476 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1477 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1478 {
1479 tu->val = 0;
1480 return 0;
1481 }
1482 }
1483 if (!s1 && !s2)
1484 {
1485 tu->val = needs_warning ? 2 : 1;
1486 return tu->val;
1487 }
d1bd0ded 1488
910ad8de 1489 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1490 {
1491 bool ok = false;
3aeb3655 1492
910ad8de 1493 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1494 if (DECL_NAME (s1) == DECL_NAME (s2))
1495 {
1496 int result;
1497
744aa42f 1498 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1499 enum_and_int_p,
1500 different_types_p);
3ae4d3cc
AO
1501
1502 if (result != 1 && !DECL_NAME (s1))
1503 continue;
1504 if (result == 0)
1505 {
1506 tu->val = 0;
1507 return 0;
1508 }
1509 if (result == 2)
1510 needs_warning = true;
1511
1512 if (TREE_CODE (s1) == FIELD_DECL
1513 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1514 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1515 break;
3ae4d3cc
AO
1516
1517 ok = true;
1518 break;
1519 }
3f75a254 1520 if (!ok)
f13c9b2c
AP
1521 {
1522 tu->val = 0;
1523 return 0;
1524 }
d1bd0ded 1525 }
f13c9b2c
AP
1526 tu->val = needs_warning ? 2 : 10;
1527 return tu->val;
d1bd0ded
GK
1528 }
1529
1530 case RECORD_TYPE:
1531 {
c22cacf3 1532 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1533
1534 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1535 s1 && s2;
910ad8de 1536 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1537 {
1538 int result;
1539 if (TREE_CODE (s1) != TREE_CODE (s2)
1540 || DECL_NAME (s1) != DECL_NAME (s2))
1541 break;
744aa42f 1542 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1543 enum_and_int_p, different_types_p);
d1bd0ded
GK
1544 if (result == 0)
1545 break;
1546 if (result == 2)
1547 needs_warning = true;
3aeb3655 1548
d1bd0ded
GK
1549 if (TREE_CODE (s1) == FIELD_DECL
1550 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1551 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1552 break;
1553 }
d1bd0ded 1554 if (s1 && s2)
f13c9b2c
AP
1555 tu->val = 0;
1556 else
1557 tu->val = needs_warning ? 2 : 1;
1558 return tu->val;
d1bd0ded
GK
1559 }
1560
1561 default:
366de0ce 1562 gcc_unreachable ();
d1bd0ded
GK
1563 }
1564}
1565
400fbf9f
JW
1566/* Return 1 if two function types F1 and F2 are compatible.
1567 If either type specifies no argument types,
1568 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1569 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1570 the other must specify that number of self-promoting arg types.
744aa42f 1571 Otherwise, the argument types must match.
dc5027f4 1572 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1573
1574static int
744aa42f 1575function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1576 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1577{
1578 tree args1, args2;
1579 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1580 int val = 1;
1581 int val1;
a6fdc086
GK
1582 tree ret1, ret2;
1583
1584 ret1 = TREE_TYPE (f1);
1585 ret2 = TREE_TYPE (f2);
1586
e508a019
JM
1587 /* 'volatile' qualifiers on a function's return type used to mean
1588 the function is noreturn. */
1589 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1590 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1591 if (TYPE_VOLATILE (ret1))
1592 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1593 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1594 if (TYPE_VOLATILE (ret2))
1595 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1596 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1597 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1598 if (val == 0)
400fbf9f
JW
1599 return 0;
1600
1601 args1 = TYPE_ARG_TYPES (f1);
1602 args2 = TYPE_ARG_TYPES (f2);
1603
dc5027f4
JM
1604 if (different_types_p != NULL
1605 && (args1 == 0) != (args2 == 0))
1606 *different_types_p = true;
1607
400fbf9f
JW
1608 /* An unspecified parmlist matches any specified parmlist
1609 whose argument types don't need default promotions. */
1610
1611 if (args1 == 0)
1612 {
1613 if (!self_promoting_args_p (args2))
1614 return 0;
1615 /* If one of these types comes from a non-prototype fn definition,
1616 compare that with the other type's arglist.
3176a0c2 1617 If they don't match, ask for a warning (but no error). */
400fbf9f 1618 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1619 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1620 enum_and_int_p, different_types_p))
400fbf9f
JW
1621 val = 2;
1622 return val;
1623 }
1624 if (args2 == 0)
1625 {
1626 if (!self_promoting_args_p (args1))
1627 return 0;
1628 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1629 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1630 enum_and_int_p, different_types_p))
400fbf9f
JW
1631 val = 2;
1632 return val;
1633 }
1634
1635 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1636 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1637 different_types_p);
400fbf9f
JW
1638 return val1 != 1 ? val1 : val;
1639}
1640
744aa42f
ILT
1641/* Check two lists of types for compatibility, returning 0 for
1642 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1643 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1644 comptypes_internal. */
400fbf9f
JW
1645
1646static int
744aa42f 1647type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1648 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1649{
1650 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1651 int val = 1;
9d5f3e49 1652 int newval = 0;
400fbf9f
JW
1653
1654 while (1)
1655 {
46df2823 1656 tree a1, mv1, a2, mv2;
400fbf9f
JW
1657 if (args1 == 0 && args2 == 0)
1658 return val;
1659 /* If one list is shorter than the other,
1660 they fail to match. */
1661 if (args1 == 0 || args2 == 0)
1662 return 0;
46df2823
JM
1663 mv1 = a1 = TREE_VALUE (args1);
1664 mv2 = a2 = TREE_VALUE (args2);
1665 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1666 mv1 = (TYPE_ATOMIC (mv1)
1667 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1668 TYPE_QUAL_ATOMIC)
1669 : TYPE_MAIN_VARIANT (mv1));
46df2823 1670 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1671 mv2 = (TYPE_ATOMIC (mv2)
1672 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1673 TYPE_QUAL_ATOMIC)
1674 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1675 /* A null pointer instead of a type
1676 means there is supposed to be an argument
1677 but nothing is specified about what type it has.
1678 So match anything that self-promotes. */
dc5027f4
JM
1679 if (different_types_p != NULL
1680 && (a1 == 0) != (a2 == 0))
1681 *different_types_p = true;
46df2823 1682 if (a1 == 0)
400fbf9f 1683 {
46df2823 1684 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1685 return 0;
1686 }
46df2823 1687 else if (a2 == 0)
400fbf9f 1688 {
46df2823 1689 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1690 return 0;
1691 }
8f5b6d29 1692 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1693 else if (TREE_CODE (a1) == ERROR_MARK
1694 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1695 ;
dc5027f4
JM
1696 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1697 different_types_p)))
400fbf9f 1698 {
dc5027f4
JM
1699 if (different_types_p != NULL)
1700 *different_types_p = true;
400fbf9f
JW
1701 /* Allow wait (union {union wait *u; int *i} *)
1702 and wait (union wait *) to be compatible. */
46df2823
JM
1703 if (TREE_CODE (a1) == UNION_TYPE
1704 && (TYPE_NAME (a1) == 0
ebf0bf7f 1705 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1706 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1707 && tree_int_cst_equal (TYPE_SIZE (a1),
1708 TYPE_SIZE (a2)))
400fbf9f
JW
1709 {
1710 tree memb;
46df2823 1711 for (memb = TYPE_FIELDS (a1);
910ad8de 1712 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1713 {
1714 tree mv3 = TREE_TYPE (memb);
1715 if (mv3 && mv3 != error_mark_node
1716 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1717 mv3 = (TYPE_ATOMIC (mv3)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1721 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1722 different_types_p))
58cb41e6
JJ
1723 break;
1724 }
400fbf9f
JW
1725 if (memb == 0)
1726 return 0;
1727 }
46df2823
JM
1728 else if (TREE_CODE (a2) == UNION_TYPE
1729 && (TYPE_NAME (a2) == 0
ebf0bf7f 1730 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1731 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1732 && tree_int_cst_equal (TYPE_SIZE (a2),
1733 TYPE_SIZE (a1)))
400fbf9f
JW
1734 {
1735 tree memb;
46df2823 1736 for (memb = TYPE_FIELDS (a2);
910ad8de 1737 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1738 {
1739 tree mv3 = TREE_TYPE (memb);
1740 if (mv3 && mv3 != error_mark_node
1741 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1742 mv3 = (TYPE_ATOMIC (mv3)
1743 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1744 TYPE_QUAL_ATOMIC)
1745 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1746 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1747 different_types_p))
58cb41e6
JJ
1748 break;
1749 }
400fbf9f
JW
1750 if (memb == 0)
1751 return 0;
1752 }
1753 else
1754 return 0;
1755 }
1756
1757 /* comptypes said ok, but record if it said to warn. */
1758 if (newval > val)
1759 val = newval;
1760
1761 args1 = TREE_CHAIN (args1);
1762 args2 = TREE_CHAIN (args2);
1763 }
1764}
400fbf9f 1765\f
a0e24419
MP
1766/* Compute the size to increment a pointer by. When a function type or void
1767 type or incomplete type is passed, size_one_node is returned.
1768 This function does not emit any diagnostics; the caller is responsible
1769 for that. */
400fbf9f 1770
4e2fb7de 1771static tree
58f9752a 1772c_size_in_bytes (const_tree type)
400fbf9f
JW
1773{
1774 enum tree_code code = TREE_CODE (type);
1775
a0e24419
MP
1776 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1777 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1778 return size_one_node;
1779
400fbf9f 1780 /* Convert in case a char is more than one unit. */
db3927fb
AH
1781 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1782 size_int (TYPE_PRECISION (char_type_node)
1783 / BITS_PER_UNIT));
400fbf9f 1784}
400fbf9f 1785\f
400fbf9f
JW
1786/* Return either DECL or its known constant value (if it has one). */
1787
56cb9733 1788tree
2f6e4e97 1789decl_constant_value (tree decl)
400fbf9f 1790{
a7c1916a 1791 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1792 in a place where a variable is invalid. Note that DECL_INITIAL
1793 isn't valid for a PARM_DECL. */
a7c1916a 1794 current_function_decl != 0
4f976745 1795 && TREE_CODE (decl) != PARM_DECL
3f75a254 1796 && !TREE_THIS_VOLATILE (decl)
83bab8db 1797 && TREE_READONLY (decl)
400fbf9f
JW
1798 && DECL_INITIAL (decl) != 0
1799 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1800 /* This is invalid if initial value is not constant.
1801 If it has either a function call, a memory reference,
1802 or a variable, then re-evaluating it could give different results. */
1803 && TREE_CONSTANT (DECL_INITIAL (decl))
1804 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1805 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1806 return DECL_INITIAL (decl);
1807 return decl;
1808}
1809
f2a71bbc
JM
1810/* Convert the array expression EXP to a pointer. */
1811static tree
c2255bc4 1812array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1813{
f2a71bbc 1814 tree orig_exp = exp;
207bf485 1815 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1816 tree adr;
1817 tree restype = TREE_TYPE (type);
1818 tree ptrtype;
207bf485 1819
f2a71bbc 1820 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1821
f2a71bbc 1822 STRIP_TYPE_NOPS (exp);
207bf485 1823
487a92fe
JM
1824 if (TREE_NO_WARNING (orig_exp))
1825 TREE_NO_WARNING (exp) = 1;
207bf485 1826
f2a71bbc
JM
1827 ptrtype = build_pointer_type (restype);
1828
22d03525 1829 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1830 return convert (ptrtype, TREE_OPERAND (exp, 0));
1831
1f37c583
JM
1832 /* In C++ array compound literals are temporary objects unless they are
1833 const or appear in namespace scope, so they are destroyed too soon
1834 to use them for much of anything (c++/53220). */
1835 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1836 {
1837 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1838 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1839 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1840 "converting an array compound literal to a pointer "
1841 "is ill-formed in C++");
1842 }
1843
c2255bc4 1844 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1845 return convert (ptrtype, adr);
1846}
207bf485 1847
f2a71bbc
JM
1848/* Convert the function expression EXP to a pointer. */
1849static tree
c2255bc4 1850function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1851{
1852 tree orig_exp = exp;
207bf485 1853
f2a71bbc 1854 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1855
f2a71bbc 1856 STRIP_TYPE_NOPS (exp);
207bf485 1857
f2a71bbc
JM
1858 if (TREE_NO_WARNING (orig_exp))
1859 TREE_NO_WARNING (exp) = 1;
207bf485 1860
c2255bc4 1861 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1862}
207bf485 1863
ebfbbdc5
JJ
1864/* Mark EXP as read, not just set, for set but not used -Wunused
1865 warning purposes. */
1866
1867void
1868mark_exp_read (tree exp)
1869{
1870 switch (TREE_CODE (exp))
1871 {
1872 case VAR_DECL:
1873 case PARM_DECL:
1874 DECL_READ_P (exp) = 1;
1875 break;
1876 case ARRAY_REF:
1877 case COMPONENT_REF:
1878 case MODIFY_EXPR:
1879 case REALPART_EXPR:
1880 case IMAGPART_EXPR:
1881 CASE_CONVERT:
1882 case ADDR_EXPR:
1883 mark_exp_read (TREE_OPERAND (exp, 0));
1884 break;
1885 case COMPOUND_EXPR:
82c3c067 1886 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1887 mark_exp_read (TREE_OPERAND (exp, 1));
1888 break;
1889 default:
1890 break;
1891 }
1892}
1893
f2a71bbc
JM
1894/* Perform the default conversion of arrays and functions to pointers.
1895 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1896 return EXP.
1897
1898 LOC is the location of the expression. */
f2a71bbc
JM
1899
1900struct c_expr
c2255bc4 1901default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1902{
1903 tree orig_exp = exp.value;
1904 tree type = TREE_TYPE (exp.value);
1905 enum tree_code code = TREE_CODE (type);
1906
1907 switch (code)
1908 {
1909 case ARRAY_TYPE:
1910 {
1911 bool not_lvalue = false;
1912 bool lvalue_array_p;
1913
1914 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1915 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1916 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1917 {
1918 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1919 not_lvalue = true;
1920 exp.value = TREE_OPERAND (exp.value, 0);
1921 }
1922
1923 if (TREE_NO_WARNING (orig_exp))
1924 TREE_NO_WARNING (exp.value) = 1;
1925
1926 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1927 if (!flag_isoc99 && !lvalue_array_p)
1928 {
1929 /* Before C99, non-lvalue arrays do not decay to pointers.
1930 Normally, using such an array would be invalid; but it can
1931 be used correctly inside sizeof or as a statement expression.
1932 Thus, do not give an error here; an error will result later. */
1933 return exp;
1934 }
1935
c2255bc4 1936 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1937 }
1938 break;
1939 case FUNCTION_TYPE:
c2255bc4 1940 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1941 break;
1942 default:
f2a71bbc 1943 break;
207bf485 1944 }
f2a71bbc 1945
207bf485
JM
1946 return exp;
1947}
1948
ebfbbdc5
JJ
1949struct c_expr
1950default_function_array_read_conversion (location_t loc, struct c_expr exp)
1951{
1952 mark_exp_read (exp.value);
1953 return default_function_array_conversion (loc, exp);
1954}
522ddfa2 1955
267bac10
JM
1956/* Return whether EXPR should be treated as an atomic lvalue for the
1957 purposes of load and store handling. */
1958
1959static bool
1960really_atomic_lvalue (tree expr)
1961{
7a0ca710 1962 if (error_operand_p (expr))
267bac10
JM
1963 return false;
1964 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1965 return false;
1966 if (!lvalue_p (expr))
1967 return false;
1968
1969 /* Ignore _Atomic on register variables, since their addresses can't
1970 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1971 sequences wouldn't work. Ignore _Atomic on structures containing
1972 bit-fields, since accessing elements of atomic structures or
1973 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1974 it's undefined at translation time or execution time, and the
1975 normal atomic sequences again wouldn't work. */
1976 while (handled_component_p (expr))
1977 {
1978 if (TREE_CODE (expr) == COMPONENT_REF
1979 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1980 return false;
1981 expr = TREE_OPERAND (expr, 0);
1982 }
1983 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1984 return false;
1985 return true;
1986}
1987
1988/* Convert expression EXP (location LOC) from lvalue to rvalue,
1989 including converting functions and arrays to pointers if CONVERT_P.
1990 If READ_P, also mark the expression as having been read. */
1991
1992struct c_expr
1993convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1994 bool convert_p, bool read_p)
1995{
1996 if (read_p)
1997 mark_exp_read (exp.value);
1998 if (convert_p)
1999 exp = default_function_array_conversion (loc, exp);
2000 if (really_atomic_lvalue (exp.value))
2001 {
2002 vec<tree, va_gc> *params;
2003 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2004 tree expr_type = TREE_TYPE (exp.value);
2005 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2006 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2007
2008 gcc_assert (TYPE_ATOMIC (expr_type));
2009
2010 /* Expansion of a generic atomic load may require an addition
2011 element, so allocate enough to prevent a resize. */
2012 vec_alloc (params, 4);
2013
2014 /* Remove the qualifiers for the rest of the expressions and
2015 create the VAL temp variable to hold the RHS. */
2016 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2017 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2018 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2019 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2020 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2021
2022 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2023 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2024 params->quick_push (expr_addr);
2025 params->quick_push (tmp_addr);
2026 params->quick_push (seq_cst);
8edbfaa6 2027 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2028
cc28fc7f
MP
2029 /* EXPR is always read. */
2030 mark_exp_read (exp.value);
2031
267bac10 2032 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2033 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2034 NULL_TREE, NULL_TREE);
267bac10
JM
2035 }
2036 return exp;
2037}
2038
522ddfa2
JM
2039/* EXP is an expression of integer type. Apply the integer promotions
2040 to it and return the promoted value. */
400fbf9f
JW
2041
2042tree
522ddfa2 2043perform_integral_promotions (tree exp)
400fbf9f 2044{
b3694847
SS
2045 tree type = TREE_TYPE (exp);
2046 enum tree_code code = TREE_CODE (type);
400fbf9f 2047
522ddfa2 2048 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2049
400fbf9f
JW
2050 /* Normally convert enums to int,
2051 but convert wide enums to something wider. */
2052 if (code == ENUMERAL_TYPE)
2053 {
b0c48229
NB
2054 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2055 TYPE_PRECISION (integer_type_node)),
2056 ((TYPE_PRECISION (type)
2057 >= TYPE_PRECISION (integer_type_node))
8df83eae 2058 && TYPE_UNSIGNED (type)));
05bccae2 2059
400fbf9f
JW
2060 return convert (type, exp);
2061 }
2062
522ddfa2
JM
2063 /* ??? This should no longer be needed now bit-fields have their
2064 proper types. */
9753f113 2065 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2066 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2067 /* If it's thinner than an int, promote it like a
d72040f5 2068 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2069 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2070 TYPE_PRECISION (integer_type_node)))
f458d1d5 2071 return convert (integer_type_node, exp);
9753f113 2072
d72040f5 2073 if (c_promoting_integer_type_p (type))
400fbf9f 2074 {
f458d1d5 2075 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2076 if (TYPE_UNSIGNED (type)
f458d1d5 2077 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2078 return convert (unsigned_type_node, exp);
05bccae2 2079
400fbf9f
JW
2080 return convert (integer_type_node, exp);
2081 }
05bccae2 2082
522ddfa2
JM
2083 return exp;
2084}
2085
2086
2087/* Perform default promotions for C data used in expressions.
46bdb9cf 2088 Enumeral types or short or char are converted to int.
522ddfa2
JM
2089 In addition, manifest constants symbols are replaced by their values. */
2090
2091tree
2092default_conversion (tree exp)
2093{
2094 tree orig_exp;
2095 tree type = TREE_TYPE (exp);
2096 enum tree_code code = TREE_CODE (type);
40449a90 2097 tree promoted_type;
522ddfa2 2098
ebfbbdc5
JJ
2099 mark_exp_read (exp);
2100
46bdb9cf
JM
2101 /* Functions and arrays have been converted during parsing. */
2102 gcc_assert (code != FUNCTION_TYPE);
2103 if (code == ARRAY_TYPE)
2104 return exp;
522ddfa2
JM
2105
2106 /* Constants can be used directly unless they're not loadable. */
2107 if (TREE_CODE (exp) == CONST_DECL)
2108 exp = DECL_INITIAL (exp);
2109
522ddfa2
JM
2110 /* Strip no-op conversions. */
2111 orig_exp = exp;
2112 STRIP_TYPE_NOPS (exp);
2113
2114 if (TREE_NO_WARNING (orig_exp))
2115 TREE_NO_WARNING (exp) = 1;
2116
400fbf9f
JW
2117 if (code == VOID_TYPE)
2118 {
5436fa2e
MP
2119 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2120 "void value not ignored as it ought to be");
400fbf9f
JW
2121 return error_mark_node;
2122 }
808d6eaa
JM
2123
2124 exp = require_complete_type (exp);
2125 if (exp == error_mark_node)
2126 return error_mark_node;
2127
40449a90
SL
2128 promoted_type = targetm.promoted_type (type);
2129 if (promoted_type)
2130 return convert (promoted_type, exp);
2131
808d6eaa
JM
2132 if (INTEGRAL_TYPE_P (type))
2133 return perform_integral_promotions (exp);
2134
400fbf9f
JW
2135 return exp;
2136}
2137\f
0fb96aa4 2138/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2139
2140 If the component name is not found, returns NULL_TREE. Otherwise,
2141 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2142 stepping down the chain to the component, which is in the last
2143 TREE_VALUE of the list. Normally the list is of length one, but if
2144 the component is embedded within (nested) anonymous structures or
2145 unions, the list steps down the chain to the component. */
2f6e4e97 2146
2f2d13da 2147static tree
0fb96aa4 2148lookup_field (tree type, tree component)
2f2d13da
DE
2149{
2150 tree field;
2151
2152 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2153 to the field elements. Use a binary search on this array to quickly
2154 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2155 will always be set for structures which have many elements. */
2156
22a0b85f 2157 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2158 {
2159 int bot, top, half;
d07605f5 2160 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2161
2162 field = TYPE_FIELDS (type);
2163 bot = 0;
d07605f5 2164 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2165 while (top - bot > 1)
2166 {
2f2d13da
DE
2167 half = (top - bot + 1) >> 1;
2168 field = field_array[bot+half];
2169
2170 if (DECL_NAME (field) == NULL_TREE)
2171 {
2172 /* Step through all anon unions in linear fashion. */
2173 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2174 {
2f2d13da 2175 field = field_array[bot++];
a68b98cf
RK
2176 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2177 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2178 {
0fb96aa4 2179 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2180
2181 if (anon)
2182 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2183
2184 /* The Plan 9 compiler permits referring
2185 directly to an anonymous struct/union field
2186 using a typedef name. */
2187 if (flag_plan9_extensions
2188 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2189 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2190 == TYPE_DECL)
2191 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2192 == component))
2193 break;
2f6e4e97 2194 }
2f2d13da
DE
2195 }
2196
2197 /* Entire record is only anon unions. */
2198 if (bot > top)
2199 return NULL_TREE;
2200
2201 /* Restart the binary search, with new lower bound. */
2202 continue;
2203 }
2204
e8b87aac 2205 if (DECL_NAME (field) == component)
2f2d13da 2206 break;
e8b87aac 2207 if (DECL_NAME (field) < component)
2f2d13da
DE
2208 bot += half;
2209 else
2210 top = bot + half;
2211 }
2212
2213 if (DECL_NAME (field_array[bot]) == component)
2214 field = field_array[bot];
2215 else if (DECL_NAME (field) != component)
e9b2c823 2216 return NULL_TREE;
2f2d13da
DE
2217 }
2218 else
2219 {
910ad8de 2220 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2221 {
e9b2c823
NB
2222 if (DECL_NAME (field) == NULL_TREE
2223 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2224 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2225 {
0fb96aa4 2226 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2227
e9b2c823
NB
2228 if (anon)
2229 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2230
2231 /* The Plan 9 compiler permits referring directly to an
2232 anonymous struct/union field using a typedef
2233 name. */
2234 if (flag_plan9_extensions
2235 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2236 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2237 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2238 == component))
2239 break;
2f2d13da
DE
2240 }
2241
2242 if (DECL_NAME (field) == component)
2243 break;
2244 }
e9b2c823
NB
2245
2246 if (field == NULL_TREE)
2247 return NULL_TREE;
2f2d13da
DE
2248 }
2249
e9b2c823 2250 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2251}
2252
c2255bc4
AH
2253/* Make an expression to refer to the COMPONENT field of structure or
2254 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2255 location of the COMPONENT_REF. */
400fbf9f
JW
2256
2257tree
c2255bc4 2258build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2259{
b3694847
SS
2260 tree type = TREE_TYPE (datum);
2261 enum tree_code code = TREE_CODE (type);
2262 tree field = NULL;
2263 tree ref;
1e57bf47 2264 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2265
7a3ea201
RH
2266 if (!objc_is_public (datum, component))
2267 return error_mark_node;
2268
46a88c12 2269 /* Detect Objective-C property syntax object.property. */
668ea4b1 2270 if (c_dialect_objc ()
46a88c12 2271 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2272 return ref;
2273
400fbf9f
JW
2274 /* See if there is a field or component with name COMPONENT. */
2275
2276 if (code == RECORD_TYPE || code == UNION_TYPE)
2277 {
d0f062fb 2278 if (!COMPLETE_TYPE_P (type))
400fbf9f 2279 {
7a228918 2280 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2281 return error_mark_node;
2282 }
2283
0fb96aa4 2284 field = lookup_field (type, component);
400fbf9f
JW
2285
2286 if (!field)
2287 {
c2255bc4 2288 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2289 return error_mark_node;
2290 }
400fbf9f 2291
e9b2c823
NB
2292 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2293 This might be better solved in future the way the C++ front
2294 end does it - by giving the anonymous entities each a
2295 separate name and type, and then have build_component_ref
2296 recursively call itself. We can't do that here. */
46ea50cb 2297 do
19d76e60 2298 {
e9b2c823 2299 tree subdatum = TREE_VALUE (field);
efed193e
JM
2300 int quals;
2301 tree subtype;
1e57bf47 2302 bool use_datum_quals;
e9b2c823
NB
2303
2304 if (TREE_TYPE (subdatum) == error_mark_node)
2305 return error_mark_node;
2306
1e57bf47
JM
2307 /* If this is an rvalue, it does not have qualifiers in C
2308 standard terms and we must avoid propagating such
2309 qualifiers down to a non-lvalue array that is then
2310 converted to a pointer. */
2311 use_datum_quals = (datum_lvalue
2312 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2313
efed193e 2314 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2315 if (use_datum_quals)
2316 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2317 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2318
2319 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2320 NULL_TREE);
c2255bc4 2321 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2322 if (TREE_READONLY (subdatum)
2323 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2324 TREE_READONLY (ref) = 1;
1e57bf47
JM
2325 if (TREE_THIS_VOLATILE (subdatum)
2326 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2327 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2328
2329 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2330 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2331
19d76e60 2332 datum = ref;
46ea50cb
RS
2333
2334 field = TREE_CHAIN (field);
19d76e60 2335 }
46ea50cb 2336 while (field);
19d76e60 2337
400fbf9f
JW
2338 return ref;
2339 }
2340 else if (code != ERROR_MARK)
c2255bc4
AH
2341 error_at (loc,
2342 "request for member %qE in something not a structure or union",
2343 component);
400fbf9f
JW
2344
2345 return error_mark_node;
2346}
2347\f
2348/* Given an expression PTR for a pointer, return an expression
2349 for the value pointed to.
6a3799eb
AH
2350 ERRORSTRING is the name of the operator to appear in error messages.
2351
2352 LOC is the location to use for the generated tree. */
400fbf9f
JW
2353
2354tree
dd865ef6 2355build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2356{
b3694847
SS
2357 tree pointer = default_conversion (ptr);
2358 tree type = TREE_TYPE (pointer);
6a3799eb 2359 tree ref;
400fbf9f
JW
2360
2361 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2362 {
1043771b 2363 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2364 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2365 {
2366 /* If a warning is issued, mark it to avoid duplicates from
2367 the backend. This only needs to be done at
2368 warn_strict_aliasing > 2. */
2369 if (warn_strict_aliasing > 2)
2370 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2371 type, TREE_OPERAND (pointer, 0)))
2372 TREE_NO_WARNING (pointer) = 1;
2373 }
2374
870cc33b 2375 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2376 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2377 == TREE_TYPE (type)))
6a3799eb
AH
2378 {
2379 ref = TREE_OPERAND (pointer, 0);
2380 protected_set_expr_location (ref, loc);
2381 return ref;
2382 }
870cc33b
RS
2383 else
2384 {
2385 tree t = TREE_TYPE (type);
46df2823 2386
984dfd8c 2387 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2388
baae9b65 2389 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2390 {
d9b7be2e
MP
2391 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2392 {
2393 error_at (loc, "dereferencing pointer to incomplete type "
2394 "%qT", t);
2395 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2396 }
870cc33b
RS
2397 return error_mark_node;
2398 }
7d882b83 2399 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2400 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2401
2402 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2403 so that we get the proper error message if the result is used
2404 to assign to. Also, &* is supposed to be a no-op.
2405 And ANSI C seems to specify that the type of the result
2406 should be the const type. */
2407 /* A de-reference of a pointer to const is not a const. It is valid
2408 to change it via some other pointer. */
2409 TREE_READONLY (ref) = TYPE_READONLY (t);
2410 TREE_SIDE_EFFECTS (ref)
271bd540 2411 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2412 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2413 protected_set_expr_location (ref, loc);
870cc33b
RS
2414 return ref;
2415 }
2416 }
400fbf9f 2417 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2418 invalid_indirection_error (loc, type, errstring);
2419
400fbf9f
JW
2420 return error_mark_node;
2421}
2422
2423/* This handles expressions of the form "a[i]", which denotes
2424 an array reference.
2425
2426 This is logically equivalent in C to *(a+i), but we may do it differently.
2427 If A is a variable or a member, we generate a primitive ARRAY_REF.
2428 This avoids forcing the array out of registers, and can work on
2429 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2430 by functions).
2431
30cd1c5d
AS
2432 For vector types, allow vector[i] but not i[vector], and create
2433 *(((type*)&vectortype) + i) for the expression.
2434
6a3799eb 2435 LOC is the location to use for the returned expression. */
400fbf9f
JW
2436
2437tree
c2255bc4 2438build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2439{
6a3799eb 2440 tree ret;
a4ab7973 2441 bool swapped = false;
400fbf9f
JW
2442 if (TREE_TYPE (array) == error_mark_node
2443 || TREE_TYPE (index) == error_mark_node)
2444 return error_mark_node;
2445
b72271b9 2446 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2447 {
2448 size_t rank = 0;
2449 if (!find_rank (loc, index, index, true, &rank))
2450 return error_mark_node;
2451 if (rank > 1)
2452 {
2453 error_at (loc, "rank of the array's index is greater than 1");
2454 return error_mark_node;
2455 }
2456 }
a4ab7973 2457 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2458 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2459 /* Allow vector[index] but not index[vector]. */
2460 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2461 {
a4ab7973
JM
2462 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2463 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2464 {
f90e8e2e 2465 error_at (loc,
30cd1c5d
AS
2466 "subscripted value is neither array nor pointer nor vector");
2467
fdeefd49
RS
2468 return error_mark_node;
2469 }
fab27f52 2470 std::swap (array, index);
a4ab7973
JM
2471 swapped = true;
2472 }
2473
2474 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2475 {
a63068b6 2476 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2477 return error_mark_node;
2478 }
2479
2480 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2481 {
a63068b6 2482 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2483 return error_mark_node;
2484 }
2485
ff6b6641
GDR
2486 /* ??? Existing practice has been to warn only when the char
2487 index is syntactically the index, not for char[array]. */
2488 if (!swapped)
5bd012f8 2489 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2490
2491 /* Apply default promotions *after* noticing character types. */
2492 index = default_conversion (index);
f406ae1f
MP
2493 if (index == error_mark_node)
2494 return error_mark_node;
a4ab7973
JM
2495
2496 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2497
aa7da51a
JJ
2498 bool non_lvalue
2499 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2500
2501 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2502 {
e4d35515 2503 tree rval, type;
fdeefd49 2504
400fbf9f
JW
2505 /* An array that is indexed by a non-constant
2506 cannot be stored in a register; we must be able to do
2507 address arithmetic on its address.
2508 Likewise an array of elements of variable size. */
2509 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2510 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2511 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2512 {
dffd7eb6 2513 if (!c_mark_addressable (array))
400fbf9f
JW
2514 return error_mark_node;
2515 }
e6d52559
JW
2516 /* An array that is indexed by a constant value which is not within
2517 the array bounds cannot be stored in a register either; because we
2518 would get a crash in store_bit_field/extract_bit_field when trying
2519 to access a non-existent part of the register. */
2520 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2521 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2522 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2523 {
dffd7eb6 2524 if (!c_mark_addressable (array))
e6d52559
JW
2525 return error_mark_node;
2526 }
400fbf9f 2527
f3bede71 2528 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2529 {
2530 tree foo = array;
2531 while (TREE_CODE (foo) == COMPONENT_REF)
2532 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2533 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2534 pedwarn (loc, OPT_Wpedantic,
fcf73884 2535 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2536 else if (!lvalue_p (foo))
2537 pedwarn_c90 (loc, OPT_Wpedantic,
2538 "ISO C90 forbids subscripting non-lvalue "
2539 "array");
400fbf9f
JW
2540 }
2541
46df2823 2542 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2543 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2544 /* Array ref is const/volatile if the array elements are
c22cacf3 2545 or if the array is. */
400fbf9f
JW
2546 TREE_READONLY (rval)
2547 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2548 | TREE_READONLY (array));
2549 TREE_SIDE_EFFECTS (rval)
2550 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2551 | TREE_SIDE_EFFECTS (array));
2552 TREE_THIS_VOLATILE (rval)
2553 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2554 /* This was added by rms on 16 Nov 91.
2f6e4e97 2555 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2556 in an inline function.
2557 Hope it doesn't break something else. */
2558 | TREE_THIS_VOLATILE (array));
928c19bb 2559 ret = require_complete_type (rval);
6a3799eb 2560 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2561 if (non_lvalue)
2562 ret = non_lvalue_loc (loc, ret);
6a3799eb 2563 return ret;
400fbf9f 2564 }
a4ab7973
JM
2565 else
2566 {
2567 tree ar = default_conversion (array);
400fbf9f 2568
a4ab7973
JM
2569 if (ar == error_mark_node)
2570 return ar;
400fbf9f 2571
a4ab7973
JM
2572 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2573 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2574
aa7da51a
JJ
2575 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2576 index, 0),
2577 RO_ARRAY_INDEXING);
2578 if (non_lvalue)
2579 ret = non_lvalue_loc (loc, ret);
2580 return ret;
a4ab7973 2581 }
400fbf9f
JW
2582}
2583\f
7e585d16 2584/* Build an external reference to identifier ID. FUN indicates
766beb40 2585 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2586 location of the identifier. This sets *TYPE to the type of the
2587 identifier, which is not the same as the type of the returned value
2588 for CONST_DECLs defined as enum constants. If the type of the
2589 identifier is not available, *TYPE is set to NULL. */
7e585d16 2590tree
c2255bc4 2591build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2592{
2593 tree ref;
2594 tree decl = lookup_name (id);
16b34ad6
ZL
2595
2596 /* In Objective-C, an instance variable (ivar) may be preferred to
2597 whatever lookup_name() found. */
2598 decl = objc_lookup_ivar (decl, id);
7e585d16 2599
6866c6e8 2600 *type = NULL;
339a28b9 2601 if (decl && decl != error_mark_node)
6866c6e8
ILT
2602 {
2603 ref = decl;
2604 *type = TREE_TYPE (ref);
2605 }
339a28b9
ZW
2606 else if (fun)
2607 /* Implicit function declaration. */
c2255bc4 2608 ref = implicitly_declare (loc, id);
339a28b9
ZW
2609 else if (decl == error_mark_node)
2610 /* Don't complain about something that's already been
2611 complained about. */
2612 return error_mark_node;
2613 else
2614 {
c2255bc4 2615 undeclared_variable (loc, id);
339a28b9
ZW
2616 return error_mark_node;
2617 }
7e585d16
ZW
2618
2619 if (TREE_TYPE (ref) == error_mark_node)
2620 return error_mark_node;
2621
339a28b9 2622 if (TREE_DEPRECATED (ref))
9b86d6bb 2623 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2624
ad960f56 2625 /* Recursive call does not count as usage. */
b8698a0f 2626 if (ref != current_function_decl)
ad960f56 2627 {
ad960f56
MLI
2628 TREE_USED (ref) = 1;
2629 }
7e585d16 2630
bc4b653b
JM
2631 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2632 {
2633 if (!in_sizeof && !in_typeof)
2634 C_DECL_USED (ref) = 1;
2635 else if (DECL_INITIAL (ref) == 0
2636 && DECL_EXTERNAL (ref)
2637 && !TREE_PUBLIC (ref))
2638 record_maybe_used_decl (ref);
2639 }
2640
7e585d16
ZW
2641 if (TREE_CODE (ref) == CONST_DECL)
2642 {
6193b8b7 2643 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2644
2645 if (warn_cxx_compat
2646 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2647 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2648 {
2649 warning_at (loc, OPT_Wc___compat,
2650 ("enum constant defined in struct or union "
2651 "is not visible in C++"));
2652 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2653 }
2654
7e585d16
ZW
2655 ref = DECL_INITIAL (ref);
2656 TREE_CONSTANT (ref) = 1;
2657 }
6a29edea 2658 else if (current_function_decl != 0
4b1e44be 2659 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2660 && (VAR_OR_FUNCTION_DECL_P (ref)
2661 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2662 {
2663 tree context = decl_function_context (ref);
2f6e4e97 2664
6a29edea
EB
2665 if (context != 0 && context != current_function_decl)
2666 DECL_NONLOCAL (ref) = 1;
2667 }
71113fcd
GK
2668 /* C99 6.7.4p3: An inline definition of a function with external
2669 linkage ... shall not contain a reference to an identifier with
2670 internal linkage. */
2671 else if (current_function_decl != 0
2672 && DECL_DECLARED_INLINE_P (current_function_decl)
2673 && DECL_EXTERNAL (current_function_decl)
2674 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2675 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2676 && ! TREE_PUBLIC (ref)
2677 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2678 record_inline_static (loc, current_function_decl, ref,
2679 csi_internal);
7e585d16
ZW
2680
2681 return ref;
2682}
2683
bc4b653b
JM
2684/* Record details of decls possibly used inside sizeof or typeof. */
2685struct maybe_used_decl
2686{
2687 /* The decl. */
2688 tree decl;
2689 /* The level seen at (in_sizeof + in_typeof). */
2690 int level;
2691 /* The next one at this level or above, or NULL. */
2692 struct maybe_used_decl *next;
2693};
2694
2695static struct maybe_used_decl *maybe_used_decls;
2696
2697/* Record that DECL, an undefined static function reference seen
2698 inside sizeof or typeof, might be used if the operand of sizeof is
2699 a VLA type or the operand of typeof is a variably modified
2700 type. */
2701
4e2fb7de 2702static void
bc4b653b
JM
2703record_maybe_used_decl (tree decl)
2704{
2705 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2706 t->decl = decl;
2707 t->level = in_sizeof + in_typeof;
2708 t->next = maybe_used_decls;
2709 maybe_used_decls = t;
2710}
2711
2712/* Pop the stack of decls possibly used inside sizeof or typeof. If
2713 USED is false, just discard them. If it is true, mark them used
2714 (if no longer inside sizeof or typeof) or move them to the next
2715 level up (if still inside sizeof or typeof). */
2716
2717void
2718pop_maybe_used (bool used)
2719{
2720 struct maybe_used_decl *p = maybe_used_decls;
2721 int cur_level = in_sizeof + in_typeof;
2722 while (p && p->level > cur_level)
2723 {
2724 if (used)
2725 {
2726 if (cur_level == 0)
2727 C_DECL_USED (p->decl) = 1;
2728 else
2729 p->level = cur_level;
2730 }
2731 p = p->next;
2732 }
2733 if (!used || cur_level == 0)
2734 maybe_used_decls = p;
2735}
2736
2737/* Return the result of sizeof applied to EXPR. */
2738
2739struct c_expr
c2255bc4 2740c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2741{
2742 struct c_expr ret;
ad97f4be
JM
2743 if (expr.value == error_mark_node)
2744 {
2745 ret.value = error_mark_node;
2746 ret.original_code = ERROR_MARK;
6866c6e8 2747 ret.original_type = NULL;
ad97f4be
JM
2748 pop_maybe_used (false);
2749 }
2750 else
2751 {
928c19bb 2752 bool expr_const_operands = true;
773ec47f
MP
2753
2754 if (TREE_CODE (expr.value) == PARM_DECL
2755 && C_ARRAY_PARAMETER (expr.value))
2756 {
2757 if (warning_at (loc, OPT_Wsizeof_array_argument,
2758 "%<sizeof%> on array function parameter %qE will "
2759 "return size of %qT", expr.value,
2760 expr.original_type))
2761 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2762 }
928c19bb
JM
2763 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2764 &expr_const_operands);
c2255bc4 2765 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2766 c_last_sizeof_arg = expr.value;
2767 ret.original_code = SIZEOF_EXPR;
6866c6e8 2768 ret.original_type = NULL;
928c19bb 2769 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2770 {
2771 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2772 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2773 folded_expr, ret.value);
2774 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2775 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2776 }
928c19bb 2777 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2778 }
bc4b653b
JM
2779 return ret;
2780}
2781
2782/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2783 name passed to sizeof (rather than the type itself). LOC is the
2784 location of the original expression. */
bc4b653b
JM
2785
2786struct c_expr
c2255bc4 2787c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2788{
2789 tree type;
2790 struct c_expr ret;
928c19bb
JM
2791 tree type_expr = NULL_TREE;
2792 bool type_expr_const = true;
2793 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2794 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2795 c_last_sizeof_arg = type;
2796 ret.original_code = SIZEOF_EXPR;
6866c6e8 2797 ret.original_type = NULL;
24070fcb
JM
2798 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2799 && c_vla_type_p (type))
928c19bb 2800 {
24070fcb
JM
2801 /* If the type is a [*] array, it is a VLA but is represented as
2802 having a size of zero. In such a case we must ensure that
2803 the result of sizeof does not get folded to a constant by
2804 c_fully_fold, because if the size is evaluated the result is
2805 not constant and so constraints on zero or negative size
2806 arrays must not be applied when this sizeof call is inside
2807 another array declarator. */
2808 if (!type_expr)
2809 type_expr = integer_zero_node;
928c19bb
JM
2810 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2811 type_expr, ret.value);
2812 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2813 }
16464cc1
VR
2814 pop_maybe_used (type != error_mark_node
2815 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2816 return ret;
2817}
2818
400fbf9f 2819/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2820 The function call is at LOC.
400fbf9f
JW
2821 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2822 TREE_VALUE of each node is a parameter-expression.
2823 FUNCTION's data type may be a function type or a pointer-to-function. */
2824
2825tree
c2255bc4 2826build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2827{
9771b263 2828 vec<tree, va_gc> *v;
bbbbb16a
ILT
2829 tree ret;
2830
9771b263 2831 vec_alloc (v, list_length (params));
bbbbb16a 2832 for (; params; params = TREE_CHAIN (params))
9771b263 2833 v->quick_push (TREE_VALUE (params));
8edbfaa6 2834 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2835 vec_free (v);
bbbbb16a
ILT
2836 return ret;
2837}
2838
ae52741c
MLI
2839/* Give a note about the location of the declaration of DECL. */
2840
c7b70a3c
MP
2841static void
2842inform_declaration (tree decl)
ae52741c 2843{
c7b70a3c 2844 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2845 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2846}
2847
bbbbb16a
ILT
2848/* Build a function call to function FUNCTION with parameters PARAMS.
2849 ORIGTYPES, if not NULL, is a vector of types; each element is
2850 either NULL or the original type of the corresponding element in
2851 PARAMS. The original type may differ from TREE_TYPE of the
2852 parameter for enums. FUNCTION's data type may be a function type
2853 or pointer-to-function. This function changes the elements of
2854 PARAMS. */
2855
2856tree
81e5eca8
MP
2857build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2858 tree function, vec<tree, va_gc> *params,
9771b263 2859 vec<tree, va_gc> *origtypes)
400fbf9f 2860{
b3694847 2861 tree fntype, fundecl = 0;
4977bab6 2862 tree name = NULL_TREE, result;
c96f4f73 2863 tree tem;
94a0dd7b
SL
2864 int nargs;
2865 tree *argarray;
b8698a0f 2866
400fbf9f 2867
fc76e425 2868 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2869 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2870
2871 /* Convert anything with function type to a pointer-to-function. */
2872 if (TREE_CODE (function) == FUNCTION_DECL)
2873 {
2874 name = DECL_NAME (function);
0a35513e
AH
2875
2876 if (flag_tm)
2877 tm_malloc_replacement (function);
a5eadacc 2878 fundecl = function;
86951993
AM
2879 /* Atomic functions have type checking/casting already done. They are
2880 often rewritten and don't match the original parameter list. */
2881 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2882 origtypes = NULL;
36536d79 2883
b72271b9 2884 if (flag_cilkplus
36536d79
BI
2885 && is_cilkplus_reduce_builtin (function))
2886 origtypes = NULL;
400fbf9f 2887 }
f2a71bbc 2888 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2889 function = function_to_pointer_conversion (loc, function);
400fbf9f 2890
6e955430
ZL
2891 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2892 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2893 if (params && !params->is_empty ())
2894 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2895
928c19bb
JM
2896 function = c_fully_fold (function, false, NULL);
2897
400fbf9f
JW
2898 fntype = TREE_TYPE (function);
2899
2900 if (TREE_CODE (fntype) == ERROR_MARK)
2901 return error_mark_node;
2902
2903 if (!(TREE_CODE (fntype) == POINTER_TYPE
2904 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2905 {
ae52741c
MLI
2906 if (!flag_diagnostics_show_caret)
2907 error_at (loc,
2908 "called object %qE is not a function or function pointer",
2909 function);
2910 else if (DECL_P (function))
2911 {
2912 error_at (loc,
2913 "called object %qD is not a function or function pointer",
2914 function);
2915 inform_declaration (function);
2916 }
2917 else
2918 error_at (loc,
2919 "called object is not a function or function pointer");
400fbf9f
JW
2920 return error_mark_node;
2921 }
2922
5ce89b2e
JM
2923 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2924 current_function_returns_abnormally = 1;
2925
400fbf9f
JW
2926 /* fntype now gets the type of function pointed to. */
2927 fntype = TREE_TYPE (fntype);
2928
ab4194da
JM
2929 /* Convert the parameters to the types declared in the
2930 function prototype, or apply default promotions. */
2931
81e5eca8
MP
2932 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2933 origtypes, function, fundecl);
ab4194da
JM
2934 if (nargs < 0)
2935 return error_mark_node;
2936
c96f4f73 2937 /* Check that the function is called through a compatible prototype.
fa337f3a 2938 If it is not, warn. */
1043771b 2939 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2940 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2941 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2942 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2943 {
2944 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2945
2946 /* This situation leads to run-time undefined behavior. We can't,
2947 therefore, simply error unless we can prove that all possible
2948 executions of the program must execute the code. */
fa337f3a 2949 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2950
fa337f3a
RB
2951 if (VOID_TYPE_P (return_type)
2952 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2953 pedwarn (loc, 0,
2954 "function with qualified void return type called");
2955 }
c96f4f73 2956
9771b263 2957 argarray = vec_safe_address (params);
bbbbb16a 2958
83322951
RG
2959 /* Check that arguments to builtin functions match the expectations. */
2960 if (fundecl
2961 && DECL_BUILT_IN (fundecl)
2962 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2963 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2964 return error_mark_node;
400fbf9f 2965
83322951 2966 /* Check that the arguments to the function are valid. */
dde05067 2967 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2968
928c19bb
JM
2969 if (name != NULL_TREE
2970 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2971 {
928c19bb 2972 if (require_constant_value)
b8698a0f 2973 result =
db3927fb
AH
2974 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2975 function, nargs, argarray);
928c19bb 2976 else
db3927fb
AH
2977 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2978 function, nargs, argarray);
928c19bb
JM
2979 if (TREE_CODE (result) == NOP_EXPR
2980 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2981 STRIP_TYPE_NOPS (result);
bf730f15
RS
2982 }
2983 else
db3927fb
AH
2984 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2985 function, nargs, argarray);
b0b3afb2 2986
71653180 2987 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2988 {
2989 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2990 pedwarn (loc, 0,
3ce62965
JM
2991 "function with qualified void return type called");
2992 return result;
2993 }
1eb8759b 2994 return require_complete_type (result);
400fbf9f 2995}
8edbfaa6
JJ
2996
2997/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2998
2999tree
3000c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3001 tree function, vec<tree, va_gc> *params,
3002 vec<tree, va_gc> *origtypes)
3003{
3004 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3005 STRIP_TYPE_NOPS (function);
3006
3007 /* Convert anything with function type to a pointer-to-function. */
3008 if (TREE_CODE (function) == FUNCTION_DECL)
3009 {
3010 /* Implement type-directed function overloading for builtins.
3011 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3012 handle all the type checking. The result is a complete expression
3013 that implements this function call. */
3014 tree tem = resolve_overloaded_builtin (loc, function, params);
3015 if (tem)
3016 return tem;
3017 }
3018 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3019}
400fbf9f 3020\f
bbbbb16a
ILT
3021/* Convert the argument expressions in the vector VALUES
3022 to the types in the list TYPELIST.
400fbf9f
JW
3023
3024 If TYPELIST is exhausted, or when an element has NULL as its type,
3025 perform the default conversions.
3026
bbbbb16a
ILT
3027 ORIGTYPES is the original types of the expressions in VALUES. This
3028 holds the type of enum values which have been converted to integral
3029 types. It may be NULL.
400fbf9f 3030
03dafa61
JM
3031 FUNCTION is a tree for the called function. It is used only for
3032 error messages, where it is formatted with %qE.
400fbf9f
JW
3033
3034 This is also where warnings about wrong number of args are generated.
3035
81e5eca8
MP
3036 ARG_LOC are locations of function arguments (if any).
3037
94a0dd7b 3038 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3039 than the length of VALUES in some error situations), or -1 on
3040 failure. */
94a0dd7b
SL
3041
3042static int
81e5eca8
MP
3043convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3044 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3045 tree function, tree fundecl)
400fbf9f 3046{
bbbbb16a
ILT
3047 tree typetail, val;
3048 unsigned int parmnum;
06302a02 3049 bool error_args = false;
b5d32c25 3050 const bool type_generic = fundecl
81e5eca8 3051 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3052 bool type_generic_remove_excess_precision = false;
03dafa61 3053 tree selector;
03dafa61 3054
2ac2f164
JM
3055 /* Change pointer to function to the function itself for
3056 diagnostics. */
03dafa61
JM
3057 if (TREE_CODE (function) == ADDR_EXPR
3058 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3059 function = TREE_OPERAND (function, 0);
03dafa61
JM
3060
3061 /* Handle an ObjC selector specially for diagnostics. */
3062 selector = objc_message_selector ();
400fbf9f 3063
8ce94e44
JM
3064 /* For type-generic built-in functions, determine whether excess
3065 precision should be removed (classification) or not
3066 (comparison). */
3067 if (type_generic
3068 && DECL_BUILT_IN (fundecl)
3069 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3070 {
3071 switch (DECL_FUNCTION_CODE (fundecl))
3072 {
3073 case BUILT_IN_ISFINITE:
3074 case BUILT_IN_ISINF:
3075 case BUILT_IN_ISINF_SIGN:
3076 case BUILT_IN_ISNAN:
3077 case BUILT_IN_ISNORMAL:
3078 case BUILT_IN_FPCLASSIFY:
3079 type_generic_remove_excess_precision = true;
3080 break;
3081
3082 default:
3083 type_generic_remove_excess_precision = false;
3084 break;
3085 }
3086 }
b72271b9 3087 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3088 return vec_safe_length (values);
8ce94e44 3089
400fbf9f 3090 /* Scan the given expressions and types, producing individual
bbbbb16a 3091 converted arguments. */
400fbf9f 3092
bbbbb16a 3093 for (typetail = typelist, parmnum = 0;
9771b263 3094 values && values->iterate (parmnum, &val);
bbbbb16a 3095 ++parmnum)
400fbf9f 3096 {
b3694847 3097 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3098 tree valtype = TREE_TYPE (val);
03dafa61
JM
3099 tree rname = function;
3100 int argnum = parmnum + 1;
4d3e6fae 3101 const char *invalid_func_diag;
8ce94e44 3102 bool excess_precision = false;
928c19bb 3103 bool npc;
bbbbb16a 3104 tree parmval;
5c1bc275
MP
3105 /* Some __atomic_* builtins have additional hidden argument at
3106 position 0. */
3107 location_t ploc
3108 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3109 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3110 : input_location;
400fbf9f
JW
3111
3112 if (type == void_type_node)
3113 {
19dc6d01 3114 if (selector)
68fca595 3115 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3116 else
68fca595 3117 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3118 inform_declaration (fundecl);
d38f7dce 3119 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3120 }
3121
03dafa61
JM
3122 if (selector && argnum > 2)
3123 {
3124 rname = selector;
3125 argnum -= 2;
3126 }
3127
928c19bb 3128 npc = null_pointer_constant_p (val);
8ce94e44
JM
3129
3130 /* If there is excess precision and a prototype, convert once to
3131 the required type rather than converting via the semantic
3132 type. Likewise without a prototype a float value represented
3133 as long double should be converted once to double. But for
3134 type-generic classification functions excess precision must
3135 be removed here. */
3136 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3137 && (type || !type_generic || !type_generic_remove_excess_precision))
3138 {
3139 val = TREE_OPERAND (val, 0);
3140 excess_precision = true;
3141 }
928c19bb 3142 val = c_fully_fold (val, false, NULL);
ed248cf7 3143 STRIP_TYPE_NOPS (val);
400fbf9f 3144
400fbf9f
JW
3145 val = require_complete_type (val);
3146
3147 if (type != 0)
3148 {
3149 /* Formal parm type is specified by a function prototype. */
400fbf9f 3150
20913689 3151 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3152 {
5c1bc275
MP
3153 error_at (ploc, "type of formal parameter %d is incomplete",
3154 parmnum + 1);
400fbf9f
JW
3155 parmval = val;
3156 }
3157 else
3158 {
bbbbb16a
ILT
3159 tree origtype;
3160
d45cf215
RS
3161 /* Optionally warn about conversions that
3162 differ from the default conversions. */
05170031 3163 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3164 {
e3a64162 3165 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3166
aae43c5f 3167 if (INTEGRAL_TYPE_P (type)
8ce94e44 3168 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3169 warning_at (ploc, OPT_Wtraditional_conversion,
3170 "passing argument %d of %qE as integer rather "
3171 "than floating due to prototype",
3172 argnum, rname);
03829ad2 3173 if (INTEGRAL_TYPE_P (type)
8ce94e44 3174 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3175 warning_at (ploc, OPT_Wtraditional_conversion,
3176 "passing argument %d of %qE as integer rather "
3177 "than complex due to prototype",
3178 argnum, rname);
aae43c5f 3179 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3180 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3181 warning_at (ploc, OPT_Wtraditional_conversion,
3182 "passing argument %d of %qE as complex rather "
3183 "than floating due to prototype",
3184 argnum, rname);
400fbf9f 3185 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3186 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3187 warning_at (ploc, OPT_Wtraditional_conversion,
3188 "passing argument %d of %qE as floating rather "
3189 "than integer due to prototype",
3190 argnum, rname);
03829ad2 3191 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3192 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3193 warning_at (ploc, OPT_Wtraditional_conversion,
3194 "passing argument %d of %qE as complex rather "
3195 "than integer due to prototype",
3196 argnum, rname);
aae43c5f 3197 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3198 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3199 warning_at (ploc, OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE as floating rather "
3201 "than complex due to prototype",
3202 argnum, rname);
aae43c5f
RK
3203 /* ??? At some point, messages should be written about
3204 conversions between complex types, but that's too messy
3205 to do now. */
d45cf215 3206 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3207 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3208 {
3209 /* Warn if any argument is passed as `float',
047de90b 3210 since without a prototype it would be `double'. */
9a8ce21f
JG
3211 if (formal_prec == TYPE_PRECISION (float_type_node)
3212 && type != dfloat32_type_node)
5c1bc275
MP
3213 warning_at (ploc, 0,
3214 "passing argument %d of %qE as %<float%> "
3215 "rather than %<double%> due to prototype",
3216 argnum, rname);
9a8ce21f
JG
3217
3218 /* Warn if mismatch between argument and prototype
3219 for decimal float types. Warn of conversions with
3220 binary float types and of precision narrowing due to
3221 prototype. */
8ce94e44 3222 else if (type != valtype
9a8ce21f
JG
3223 && (type == dfloat32_type_node
3224 || type == dfloat64_type_node
c22cacf3 3225 || type == dfloat128_type_node
8ce94e44
JM
3226 || valtype == dfloat32_type_node
3227 || valtype == dfloat64_type_node
3228 || valtype == dfloat128_type_node)
c22cacf3 3229 && (formal_prec
8ce94e44 3230 <= TYPE_PRECISION (valtype)
9a8ce21f 3231 || (type == dfloat128_type_node
8ce94e44 3232 && (valtype
c22cacf3 3233 != dfloat64_type_node
8ce94e44 3234 && (valtype
9a8ce21f
JG
3235 != dfloat32_type_node)))
3236 || (type == dfloat64_type_node
8ce94e44 3237 && (valtype
9a8ce21f 3238 != dfloat32_type_node))))
5c1bc275
MP
3239 warning_at (ploc, 0,
3240 "passing argument %d of %qE as %qT "
3241 "rather than %qT due to prototype",
3242 argnum, rname, type, valtype);
9a8ce21f 3243
d45cf215 3244 }
3ed56f8a
KG
3245 /* Detect integer changing in width or signedness.
3246 These warnings are only activated with
05170031
MLI
3247 -Wtraditional-conversion, not with -Wtraditional. */
3248 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3249 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3250 {
d45cf215
RS
3251 tree would_have_been = default_conversion (val);
3252 tree type1 = TREE_TYPE (would_have_been);
3253
754a4d82 3254 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3255 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3256 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3257 /* No warning if function asks for enum
3258 and the actual arg is that enum type. */
3259 ;
3260 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3261 warning_at (ploc, OPT_Wtraditional_conversion,
3262 "passing argument %d of %qE "
3263 "with different width due to prototype",
3264 argnum, rname);
8df83eae 3265 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3266 ;
800cd3b9
RS
3267 /* Don't complain if the formal parameter type
3268 is an enum, because we can't tell now whether
3269 the value was an enum--even the same enum. */
3270 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3271 ;
400fbf9f
JW
3272 else if (TREE_CODE (val) == INTEGER_CST
3273 && int_fits_type_p (val, type))
3274 /* Change in signedness doesn't matter
3275 if a constant value is unaffected. */
3276 ;
ce9895ae
RS
3277 /* If the value is extended from a narrower
3278 unsigned type, it doesn't matter whether we
3279 pass it as signed or unsigned; the value
3280 certainly is the same either way. */
8ce94e44
JM
3281 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3282 && TYPE_UNSIGNED (valtype))
ce9895ae 3283 ;
8df83eae 3284 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3285 warning_at (ploc, OPT_Wtraditional_conversion,
3286 "passing argument %d of %qE "
3287 "as unsigned due to prototype",
3288 argnum, rname);
3ed56f8a 3289 else
5c1bc275
MP
3290 warning_at (ploc, OPT_Wtraditional_conversion,
3291 "passing argument %d of %qE "
3292 "as signed due to prototype",
3293 argnum, rname);
400fbf9f
JW
3294 }
3295 }
3296
8ce94e44
JM
3297 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3298 sake of better warnings from convert_and_check. */
3299 if (excess_precision)
3300 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3301 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3302 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3303 val, origtype, ic_argpass,
3304 npc, fundecl, function,
2ac2f164 3305 parmnum + 1);
2f6e4e97 3306
61f71b34 3307 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3308 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3309 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3310 parmval = default_conversion (parmval);
400fbf9f 3311 }
400fbf9f 3312 }
8ce94e44
JM
3313 else if (TREE_CODE (valtype) == REAL_TYPE
3314 && (TYPE_PRECISION (valtype)
2531a1d9 3315 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3316 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3317 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3318 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3319 {
3320 if (type_generic)
bbbbb16a 3321 parmval = val;
b5d32c25 3322 else
0a0b3574
MM
3323 {
3324 /* Convert `float' to `double'. */
3325 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3326 warning_at (ploc, OPT_Wdouble_promotion,
3327 "implicit conversion from %qT to %qT when passing "
3328 "argument to function",
3329 valtype, double_type_node);
0a0b3574
MM
3330 parmval = convert (double_type_node, val);
3331 }
b5d32c25 3332 }
8ce94e44
JM
3333 else if (excess_precision && !type_generic)
3334 /* A "double" argument with excess precision being passed
3335 without a prototype or in variable arguments. */
bbbbb16a 3336 parmval = convert (valtype, val);
c22cacf3
MS
3337 else if ((invalid_func_diag =
3338 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3339 {
3340 error (invalid_func_diag);
94a0dd7b 3341 return -1;
4d3e6fae 3342 }
400fbf9f
JW
3343 else
3344 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3345 parmval = default_conversion (val);
3346
9771b263 3347 (*values)[parmnum] = parmval;
06302a02
JJ
3348 if (parmval == error_mark_node)
3349 error_args = true;
400fbf9f
JW
3350
3351 if (typetail)
3352 typetail = TREE_CHAIN (typetail);
3353 }
3354
9771b263 3355 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3356
400fbf9f 3357 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3358 {
68fca595 3359 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3360 inform_declaration (fundecl);
3361 return -1;
3789b316 3362 }
400fbf9f 3363
06302a02 3364 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3365}
3366\f
43f6dfd3
RS
3367/* This is the entry point used by the parser to build unary operators
3368 in the input. CODE, a tree_code, specifies the unary operator, and
3369 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3370 CONVERT_EXPR for code.
3371
3372 LOC is the location to use for the tree generated.
3373*/
43f6dfd3
RS
3374
3375struct c_expr
c2255bc4 3376parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3377{
3378 struct c_expr result;
3379
c9f9eb5d 3380 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3381 result.original_code = code;
6866c6e8
ILT
3382 result.original_type = NULL;
3383
59c0753d 3384 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3385 overflow_warning (loc, result.value);
59c0753d 3386
43f6dfd3
RS
3387 return result;
3388}
3389
3390/* This is the entry point used by the parser to build binary operators
3391 in the input. CODE, a tree_code, specifies the binary operator, and
3392 ARG1 and ARG2 are the operands. In addition to constructing the
3393 expression, we check for operands that were written with other binary
ba47d38d
AH
3394 operators in a way that is likely to confuse the user.
3395
3396 LOCATION is the location of the binary operator. */
edc7c4ec 3397
487a92fe 3398struct c_expr
ba47d38d
AH
3399parser_build_binary_op (location_t location, enum tree_code code,
3400 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3401{
487a92fe 3402 struct c_expr result;
400fbf9f 3403
487a92fe
JM
3404 enum tree_code code1 = arg1.original_code;
3405 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3406 tree type1 = (arg1.original_type
3407 ? arg1.original_type
3408 : TREE_TYPE (arg1.value));
3409 tree type2 = (arg2.original_type
3410 ? arg2.original_type
3411 : TREE_TYPE (arg2.value));
400fbf9f 3412
ba47d38d
AH
3413 result.value = build_binary_op (location, code,
3414 arg1.value, arg2.value, 1);
487a92fe 3415 result.original_code = code;
6866c6e8 3416 result.original_type = NULL;
58bf601b 3417
487a92fe
JM
3418 if (TREE_CODE (result.value) == ERROR_MARK)
3419 return result;
400fbf9f 3420
ba47d38d
AH
3421 if (location != UNKNOWN_LOCATION)
3422 protected_set_expr_location (result.value, location);
3423
400fbf9f 3424 /* Check for cases such as x+y<<z which users are likely
487a92fe 3425 to misinterpret. */
400fbf9f 3426 if (warn_parentheses)
393e8e8b
MP
3427 warn_about_parentheses (location, code, code1, arg1.value, code2,
3428 arg2.value);
001af587 3429
ca409efd 3430 if (warn_logical_op)
393e8e8b 3431 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3432 code1, arg1.value, code2, arg2.value);
63a08740 3433
742938c9 3434 if (warn_logical_not_paren
7ccb1a11 3435 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3436 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3437 && code2 != TRUTH_NOT_EXPR
3438 /* Avoid warning for !!x == y. */
3439 && (TREE_CODE (arg1.value) != NE_EXPR
3440 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3441 {
3442 /* Avoid warning for !b == y where b has _Bool type. */
3443 tree t = integer_zero_node;
3444 if (TREE_CODE (arg1.value) == EQ_EXPR
3445 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3446 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3447 {
3448 t = TREE_OPERAND (arg1.value, 0);
3449 do
3450 {
3451 if (TREE_TYPE (t) != integer_type_node)
3452 break;
3453 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3454 t = C_MAYBE_CONST_EXPR_EXPR (t);
3455 else if (CONVERT_EXPR_P (t))
3456 t = TREE_OPERAND (t, 0);
3457 else
3458 break;
3459 }
3460 while (1);
3461 }
3462 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3463 warn_logical_not_parentheses (location, code, arg2.value);
3464 }
742938c9 3465
e994a705
RS
3466 /* Warn about comparisons against string literals, with the exception
3467 of testing for equality or inequality of a string literal with NULL. */
3468 if (code == EQ_EXPR || code == NE_EXPR)
3469 {
3470 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3471 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3472 warning_at (location, OPT_Waddress,
3473 "comparison with string literal results in unspecified behavior");
e994a705
RS
3474 }
3475 else if (TREE_CODE_CLASS (code) == tcc_comparison
3476 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3477 warning_at (location, OPT_Waddress,
3478 "comparison with string literal results in unspecified behavior");
e994a705 3479
b8698a0f
L
3480 if (TREE_OVERFLOW_P (result.value)
3481 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3482 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3483 overflow_warning (location, result.value);
400fbf9f 3484
6866c6e8
ILT
3485 /* Warn about comparisons of different enum types. */
3486 if (warn_enum_compare
3487 && TREE_CODE_CLASS (code) == tcc_comparison
3488 && TREE_CODE (type1) == ENUMERAL_TYPE
3489 && TREE_CODE (type2) == ENUMERAL_TYPE
3490 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3491 warning_at (location, OPT_Wenum_compare,
3492 "comparison between %qT and %qT",
3493 type1, type2);
3494
400fbf9f
JW
3495 return result;
3496}
3e4093b6 3497\f
3e4093b6
RS
3498/* Return a tree for the difference of pointers OP0 and OP1.
3499 The resulting tree has type int. */
293c9fdd 3500
3e4093b6 3501static tree
db3927fb 3502pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3503{
3e4093b6 3504 tree restype = ptrdiff_type_node;
36c5e70a 3505 tree result, inttype;
400fbf9f 3506
36c5e70a
BE
3507 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3508 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3509 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3510 tree orig_op1 = op1;
400fbf9f 3511
36c5e70a
BE
3512 /* If the operands point into different address spaces, we need to
3513 explicitly convert them to pointers into the common address space
3514 before we can subtract the numerical address values. */
3515 if (as0 != as1)
3516 {
3517 addr_space_t as_common;
3518 tree common_type;
3519
3520 /* Determine the common superset address space. This is guaranteed
3521 to exist because the caller verified that comp_target_types
3522 returned non-zero. */
3523 if (!addr_space_superset (as0, as1, &as_common))
3524 gcc_unreachable ();
3525
3526 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3527 op0 = convert (common_type, op0);
3528 op1 = convert (common_type, op1);
3529 }
3530
3531 /* Determine integer type to perform computations in. This will usually
3532 be the same as the result type (ptrdiff_t), but may need to be a wider
3533 type if pointers for the address space are wider than ptrdiff_t. */
3534 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3535 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3536 else
3537 inttype = restype;
3538
fcf73884 3539 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3540 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3541 "pointer of type %<void *%> used in subtraction");
3542 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3543 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3544 "pointer to a function used in subtraction");
400fbf9f 3545
3e4093b6
RS
3546 /* First do the subtraction as integers;
3547 then drop through to build the divide operator.
3548 Do not do default conversions on the minus operator
3549 in case restype is a short type. */
400fbf9f 3550
db3927fb 3551 op0 = build_binary_op (loc,
36c5e70a
BE
3552 MINUS_EXPR, convert (inttype, op0),
3553 convert (inttype, op1), 0);
3e4093b6
RS
3554 /* This generates an error if op1 is pointer to incomplete type. */
3555 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3556 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3557
3e4093b6 3558 op1 = c_size_in_bytes (target_type);
400fbf9f 3559
f04dda30
MP
3560 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3561 error_at (loc, "arithmetic on pointer to an empty aggregate");
3562
3e4093b6 3563 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3564 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3565 op0, convert (inttype, op1));
3566
3567 /* Convert to final result type if necessary. */
3568 return convert (restype, result);
3e4093b6
RS
3569}
3570\f
267bac10
JM
3571/* Expand atomic compound assignments into an approriate sequence as
3572 specified by the C11 standard section 6.5.16.2.
3573 given
3574 _Atomic T1 E1
3575 T2 E2
3576 E1 op= E2
3577
3578 This sequence is used for all types for which these operations are
3579 supported.
3580
3581 In addition, built-in versions of the 'fe' prefixed routines may
3582 need to be invoked for floating point (real, complex or vector) when
3583 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3584
3585 T1 newval;
3586 T1 old;
3587 T1 *addr
3588 T2 val
3589 fenv_t fenv
3590
3591 addr = &E1;
3592 val = (E2);
3593 __atomic_load (addr, &old, SEQ_CST);
3594 feholdexcept (&fenv);
3595loop:
3596 newval = old op val;
3597 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3598 SEQ_CST))
3599 goto done;
3600 feclearexcept (FE_ALL_EXCEPT);
3601 goto loop:
3602done:
3603 feupdateenv (&fenv);
3604
3605 Also note that the compiler is simply issuing the generic form of
3606 the atomic operations. This requires temp(s) and has their address
3607 taken. The atomic processing is smart enough to figure out when the
3608 size of an object can utilize a lock-free version, and convert the
3609 built-in call to the appropriate lock-free routine. The optimizers
3610 will then dispose of any temps that are no longer required, and
3611 lock-free implementations are utilized as long as there is target
3612 support for the required size.
3613
3614 If the operator is NOP_EXPR, then this is a simple assignment, and
3615 an __atomic_store is issued to perform the assignment rather than
3616 the above loop.
3617
3618*/
3619
3620/* Build an atomic assignment at LOC, expanding into the proper
3621 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3622 the result of the operation, unless RETURN_OLD_P in which case
3623 return the old value of LHS (this is only for postincrement and
3624 postdecrement). */
3625static tree
3626build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3627 tree rhs, bool return_old_p)
3628{
3629 tree fndecl, func_call;
3630 vec<tree, va_gc> *params;
3631 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3632 tree old, old_addr;
3633 tree compound_stmt;
3634 tree stmt, goto_stmt;
3635 tree loop_label, loop_decl, done_label, done_decl;
3636
3637 tree lhs_type = TREE_TYPE (lhs);
3638 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3639 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3640 tree rhs_type = TREE_TYPE (rhs);
3641
3642 gcc_assert (TYPE_ATOMIC (lhs_type));
3643
3644 if (return_old_p)
3645 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3646
3647 /* Allocate enough vector items for a compare_exchange. */
3648 vec_alloc (params, 6);
3649
3650 /* Create a compound statement to hold the sequence of statements
3651 with a loop. */
3652 compound_stmt = c_begin_compound_stmt (false);
3653
3654 /* Fold the RHS if it hasn't already been folded. */
3655 if (modifycode != NOP_EXPR)
3656 rhs = c_fully_fold (rhs, false, NULL);
3657
3658 /* Remove the qualifiers for the rest of the expressions and create
3659 the VAL temp variable to hold the RHS. */
3660 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3661 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3662 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3663 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3664 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3665 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3666 NULL_TREE);
267bac10
JM
3667 SET_EXPR_LOCATION (rhs, loc);
3668 add_stmt (rhs);
3669
3670 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3671 an atomic_store. */
3672 if (modifycode == NOP_EXPR)
3673 {
3674 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3675 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3676 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3677 params->quick_push (lhs_addr);
3678 params->quick_push (rhs);
3679 params->quick_push (seq_cst);
8edbfaa6 3680 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3681 add_stmt (func_call);
3682
3683 /* Finish the compound statement. */
3684 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3685
3686 /* VAL is the value which was stored, return a COMPOUND_STMT of
3687 the statement and that value. */
3688 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3689 }
3690
3691 /* Create the variables and labels required for the op= form. */
5c4abbb8 3692 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3693 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3694 TREE_ADDRESSABLE (old) = 1;
3695 TREE_NO_WARNING (old) = 1;
267bac10 3696
5c4abbb8 3697 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3698 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3699 TREE_ADDRESSABLE (newval) = 1;
3700
3701 loop_decl = create_artificial_label (loc);
3702 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3703
3704 done_decl = create_artificial_label (loc);
3705 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3706
3707 /* __atomic_load (addr, &old, SEQ_CST). */
3708 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3709 params->quick_push (lhs_addr);
3710 params->quick_push (old_addr);
3711 params->quick_push (seq_cst);
8edbfaa6 3712 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3713 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3714 NULL_TREE);
3715 add_stmt (old);
267bac10
JM
3716 params->truncate (0);
3717
3718 /* Create the expressions for floating-point environment
3719 manipulation, if required. */
3720 bool need_fenv = (flag_trapping_math
3721 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3722 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3723 if (need_fenv)
3724 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3725
3726 if (hold_call)
3727 add_stmt (hold_call);
3728
3729 /* loop: */
3730 add_stmt (loop_label);
3731
3732 /* newval = old + val; */
3733 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3734 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3735 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3736 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3737 NULL_TREE, 0);
3738 if (rhs != error_mark_node)
3739 {
5c4abbb8
MP
3740 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3741 NULL_TREE);
267bac10
JM
3742 SET_EXPR_LOCATION (rhs, loc);
3743 add_stmt (rhs);
3744 }
3745
3746 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3747 goto done; */
3748 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3749 params->quick_push (lhs_addr);
3750 params->quick_push (old_addr);
3751 params->quick_push (newval_addr);
3752 params->quick_push (integer_zero_node);
3753 params->quick_push (seq_cst);
3754 params->quick_push (seq_cst);
8edbfaa6 3755 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3756
3757 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3758 SET_EXPR_LOCATION (goto_stmt, loc);
3759
3760 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3761 SET_EXPR_LOCATION (stmt, loc);
3762 add_stmt (stmt);
5c4abbb8 3763
267bac10
JM
3764 if (clear_call)
3765 add_stmt (clear_call);
3766
3767 /* goto loop; */
3768 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3769 SET_EXPR_LOCATION (goto_stmt, loc);
3770 add_stmt (goto_stmt);
5c4abbb8 3771
267bac10
JM
3772 /* done: */
3773 add_stmt (done_label);
3774
3775 if (update_call)
3776 add_stmt (update_call);
3777
3778 /* Finish the compound statement. */
3779 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3780
3781 /* NEWVAL is the value that was successfully stored, return a
3782 COMPOUND_EXPR of the statement and the appropriate value. */
3783 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3784 return_old_p ? old : newval);
3785}
3786
3e4093b6
RS
3787/* Construct and perhaps optimize a tree representation
3788 for a unary operation. CODE, a tree_code, specifies the operation
3789 and XARG is the operand.
3790 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3791 the default promotions (such as from short to int).
3792 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3793 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3794 arrays to pointers in C99.
3795
3796 LOCATION is the location of the operator. */
400fbf9f 3797
3e4093b6 3798tree
c9f9eb5d
AH
3799build_unary_op (location_t location,
3800 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3801{
3802 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3803 tree arg = xarg;
3804 tree argtype = 0;
808d6eaa 3805 enum tree_code typecode;
3e4093b6 3806 tree val;
c9f9eb5d 3807 tree ret = error_mark_node;
8ce94e44 3808 tree eptype = NULL_TREE;
3e4093b6 3809 int noconvert = flag;
4de67c26 3810 const char *invalid_op_diag;
928c19bb
JM
3811 bool int_operands;
3812
3813 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3814 if (int_operands)
3815 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3816
808d6eaa
JM
3817 if (code != ADDR_EXPR)
3818 arg = require_complete_type (arg);
3819
3820 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3821 if (typecode == ERROR_MARK)
3822 return error_mark_node;
3823 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3824 typecode = INTEGER_TYPE;
6c36d76b 3825
4de67c26
JM
3826 if ((invalid_op_diag
3827 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3828 {
c9f9eb5d 3829 error_at (location, invalid_op_diag);
4de67c26
JM
3830 return error_mark_node;
3831 }
3832
8ce94e44
JM
3833 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3834 {
3835 eptype = TREE_TYPE (arg);
3836 arg = TREE_OPERAND (arg, 0);
3837 }
3838
3e4093b6
RS
3839 switch (code)
3840 {
3841 case CONVERT_EXPR:
3842 /* This is used for unary plus, because a CONVERT_EXPR
3843 is enough to prevent anybody from looking inside for
3844 associativity, but won't generate any code. */
3845 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3846 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3847 || typecode == VECTOR_TYPE))
400fbf9f 3848 {
c9f9eb5d 3849 error_at (location, "wrong type argument to unary plus");
3e4093b6 3850 return error_mark_node;
400fbf9f 3851 }
3e4093b6
RS
3852 else if (!noconvert)
3853 arg = default_conversion (arg);
db3927fb 3854 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3855 break;
3856
3e4093b6
RS
3857 case NEGATE_EXPR:
3858 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3859 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3860 || typecode == VECTOR_TYPE))
3861 {
c9f9eb5d 3862 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3863 return error_mark_node;
3864 }
3865 else if (!noconvert)
3866 arg = default_conversion (arg);
400fbf9f
JW
3867 break;
3868
3e4093b6 3869 case BIT_NOT_EXPR:
462643f0
AP
3870 /* ~ works on integer types and non float vectors. */
3871 if (typecode == INTEGER_TYPE
3872 || (typecode == VECTOR_TYPE
3873 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3874 {
3e4093b6
RS
3875 if (!noconvert)
3876 arg = default_conversion (arg);
03d5b1f5 3877 }
3e4093b6 3878 else if (typecode == COMPLEX_TYPE)
400fbf9f 3879 {
3e4093b6 3880 code = CONJ_EXPR;
c1771a20 3881 pedwarn (location, OPT_Wpedantic,
fcf73884 3882 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3883 if (!noconvert)
3884 arg = default_conversion (arg);
3885 }
3886 else
3887 {
c9f9eb5d 3888 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3889 return error_mark_node;
400fbf9f
JW
3890 }
3891 break;
3892
3e4093b6 3893 case ABS_EXPR:
11017cc7 3894 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3895 {
c9f9eb5d 3896 error_at (location, "wrong type argument to abs");
3e4093b6 3897 return error_mark_node;
400fbf9f 3898 }
3e4093b6
RS
3899 else if (!noconvert)
3900 arg = default_conversion (arg);
400fbf9f
JW
3901 break;
3902
3e4093b6
RS
3903 case CONJ_EXPR:
3904 /* Conjugating a real value is a no-op, but allow it anyway. */
3905 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3906 || typecode == COMPLEX_TYPE))
400fbf9f 3907 {
c9f9eb5d 3908 error_at (location, "wrong type argument to conjugation");
3e4093b6 3909 return error_mark_node;
400fbf9f 3910 }
3e4093b6
RS
3911 else if (!noconvert)
3912 arg = default_conversion (arg);
400fbf9f
JW
3913 break;
3914
3e4093b6 3915 case TRUTH_NOT_EXPR:
ab22c1fa 3916 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3917 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3918 && typecode != COMPLEX_TYPE)
400fbf9f 3919 {
c9f9eb5d
AH
3920 error_at (location,
3921 "wrong type argument to unary exclamation mark");
3e4093b6 3922 return error_mark_node;
400fbf9f 3923 }
a27d595d
JM
3924 if (int_operands)
3925 {
3926 arg = c_objc_common_truthvalue_conversion (location, xarg);
3927 arg = remove_c_maybe_const_expr (arg);
3928 }
3929 else
3930 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3931 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3932 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3933 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3934 location = EXPR_LOCATION (ret);
c9f9eb5d 3935 goto return_build_unary_op;
3e4093b6 3936
3e4093b6 3937 case REALPART_EXPR:
3e4093b6 3938 case IMAGPART_EXPR:
fb52b50a
NF
3939 ret = build_real_imag_expr (location, code, arg);
3940 if (ret == error_mark_node)
3941 return error_mark_node;
8ce94e44
JM
3942 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3943 eptype = TREE_TYPE (eptype);
c9f9eb5d 3944 goto return_build_unary_op;
3e4093b6
RS
3945
3946 case PREINCREMENT_EXPR:
3947 case POSTINCREMENT_EXPR:
3948 case PREDECREMENT_EXPR:
3949 case POSTDECREMENT_EXPR:
3e4093b6 3950
928c19bb
JM
3951 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3952 {
3953 tree inner = build_unary_op (location, code,
3954 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3955 if (inner == error_mark_node)
3956 return error_mark_node;
3957 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3958 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3959 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3960 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3961 goto return_build_unary_op;
3962 }
3963
925e8657
NP
3964 /* Complain about anything that is not a true lvalue. In
3965 Objective-C, skip this check for property_refs. */
f90e8e2e 3966 if (!objc_is_property_ref (arg)
7bd11157
TT
3967 && !lvalue_or_else (location,
3968 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3969 || code == POSTINCREMENT_EXPR)
3970 ? lv_increment
3971 : lv_decrement)))
928c19bb
JM
3972 return error_mark_node;
3973
09639a83
ILT
3974 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3975 {
3976 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3977 warning_at (location, OPT_Wc___compat,
3978 "increment of enumeration value is invalid in C++");
3979 else
3980 warning_at (location, OPT_Wc___compat,
3981 "decrement of enumeration value is invalid in C++");
3982 }
3983
928c19bb
JM
3984 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3985 arg = c_fully_fold (arg, false, NULL);
3986
267bac10
JM
3987 bool atomic_op;
3988 atomic_op = really_atomic_lvalue (arg);
3989
3e4093b6
RS
3990 /* Increment or decrement the real part of the value,
3991 and don't change the imaginary part. */
3992 if (typecode == COMPLEX_TYPE)
400fbf9f 3993 {
3e4093b6
RS
3994 tree real, imag;
3995
c1771a20 3996 pedwarn (location, OPT_Wpedantic,
509c9d60 3997 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3998
267bac10
JM
3999 if (!atomic_op)
4000 {
4001 arg = stabilize_reference (arg);
4002 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4003 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4004 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4005 if (real == error_mark_node || imag == error_mark_node)
4006 return error_mark_node;
4007 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4008 real, imag);
4009 goto return_build_unary_op;
4010 }
400fbf9f 4011 }
3e4093b6
RS
4012
4013 /* Report invalid types. */
4014
ab22c1fa 4015 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4016 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4017 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4018 {
3e4093b6 4019 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4020 error_at (location, "wrong type argument to increment");
c22cacf3 4021 else
c9f9eb5d 4022 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4023
4024 return error_mark_node;
400fbf9f 4025 }
400fbf9f 4026
3e4093b6
RS
4027 {
4028 tree inc;
400fbf9f 4029
3e4093b6
RS
4030 argtype = TREE_TYPE (arg);
4031
4032 /* Compute the increment. */
4033
4034 if (typecode == POINTER_TYPE)
4035 {
a0e24419 4036 /* If pointer target is an incomplete type,
3e4093b6 4037 we just cannot know how to do the arithmetic. */
b70cef5d 4038 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4039 {
4040 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4041 error_at (location,
a0e24419
MP
4042 "increment of pointer to an incomplete type %qT",
4043 TREE_TYPE (argtype));
3e4093b6 4044 else
c9f9eb5d 4045 error_at (location,
a0e24419
MP
4046 "decrement of pointer to an incomplete type %qT",
4047 TREE_TYPE (argtype));
3e4093b6 4048 }
b70cef5d
JJ
4049 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4050 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4051 {
3e4093b6 4052 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4053 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4054 "wrong type argument to increment");
3e4093b6 4055 else
44d90fe1 4056 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4057 "wrong type argument to decrement");
3e4093b6
RS
4058 }
4059
b70cef5d 4060 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4061 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4062 }
b70cef5d 4063 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4064 {
4065 /* For signed fract types, we invert ++ to -- or
4066 -- to ++, and change inc from 1 to -1, because
4067 it is not possible to represent 1 in signed fract constants.
4068 For unsigned fract types, the result always overflows and
4069 we get an undefined (original) or the maximum value. */
4070 if (code == PREINCREMENT_EXPR)
4071 code = PREDECREMENT_EXPR;
4072 else if (code == PREDECREMENT_EXPR)
4073 code = PREINCREMENT_EXPR;
4074 else if (code == POSTINCREMENT_EXPR)
4075 code = POSTDECREMENT_EXPR;
4076 else /* code == POSTDECREMENT_EXPR */
4077 code = POSTINCREMENT_EXPR;
4078
4079 inc = integer_minus_one_node;
4080 inc = convert (argtype, inc);
4081 }
3e4093b6 4082 else
5be014d5 4083 {
241b71bb
TV
4084 inc = VECTOR_TYPE_P (argtype)
4085 ? build_one_cst (argtype)
4086 : integer_one_node;
5be014d5
AP
4087 inc = convert (argtype, inc);
4088 }
3e4093b6 4089
925e8657
NP
4090 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4091 need to ask Objective-C to build the increment or decrement
4092 expression for it. */
4093 if (objc_is_property_ref (arg))
f90e8e2e 4094 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4095 arg, inc);
4096
3e4093b6 4097 /* Report a read-only lvalue. */
f37acdf9 4098 if (TYPE_READONLY (argtype))
953ff289 4099 {
c02065fc 4100 readonly_error (location, arg,
953ff289
DN
4101 ((code == PREINCREMENT_EXPR
4102 || code == POSTINCREMENT_EXPR)
4103 ? lv_increment : lv_decrement));
4104 return error_mark_node;
4105 }
f37acdf9
JM
4106 else if (TREE_READONLY (arg))
4107 readonly_warning (arg,
4108 ((code == PREINCREMENT_EXPR
4109 || code == POSTINCREMENT_EXPR)
4110 ? lv_increment : lv_decrement));
3e4093b6 4111
267bac10
JM
4112 /* If the argument is atomic, use the special code sequences for
4113 atomic compound assignment. */
4114 if (atomic_op)
4115 {
4116 arg = stabilize_reference (arg);
4117 ret = build_atomic_assign (location, arg,
4118 ((code == PREINCREMENT_EXPR
4119 || code == POSTINCREMENT_EXPR)
4120 ? PLUS_EXPR
4121 : MINUS_EXPR),
4122 (FRACT_MODE_P (TYPE_MODE (argtype))
4123 ? inc
4124 : integer_one_node),
4125 (code == POSTINCREMENT_EXPR
4126 || code == POSTDECREMENT_EXPR));
4127 goto return_build_unary_op;
4128 }
4129
3e4093b6
RS
4130 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4131 val = boolean_increment (code, arg);
4132 else
53fb4de3 4133 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4134 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4135 if (TREE_CODE (val) != code)
6de9cd9a 4136 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4137 ret = val;
4138 goto return_build_unary_op;
3e4093b6
RS
4139 }
4140
4141 case ADDR_EXPR:
4142 /* Note that this operation never does default_conversion. */
4143
2b4b7036
JM
4144 /* The operand of unary '&' must be an lvalue (which excludes
4145 expressions of type void), or, in C99, the result of a [] or
4146 unary '*' operator. */
4147 if (VOID_TYPE_P (TREE_TYPE (arg))
4148 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4149 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4150 pedwarn (location, 0, "taking address of expression of type %<void%>");
4151
3e4093b6 4152 /* Let &* cancel out to simplify resulting code. */
22d03525 4153 if (INDIRECT_REF_P (arg))
400fbf9f 4154 {
3e4093b6
RS
4155 /* Don't let this be an lvalue. */
4156 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4157 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4158 ret = TREE_OPERAND (arg, 0);
4159 goto return_build_unary_op;
400fbf9f 4160 }
1eb8759b 4161
7c672dfc 4162 /* For &x[y], return x+y */
3e4093b6 4163 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4164 {
f2a71bbc
JM
4165 tree op0 = TREE_OPERAND (arg, 0);
4166 if (!c_mark_addressable (op0))
3e4093b6 4167 return error_mark_node;
1eb8759b 4168 }
1eb8759b 4169
3e4093b6
RS
4170 /* Anything not already handled and not a true memory reference
4171 or a non-lvalue array is an error. */
4172 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4173 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4174 return error_mark_node;
b6a10c9f 4175
928c19bb
JM
4176 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4177 folding later. */
4178 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4179 {
4180 tree inner = build_unary_op (location, code,
4181 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4182 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4183 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4184 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4185 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4186 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4187 goto return_build_unary_op;
4188 }
4189
3e4093b6
RS
4190 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4191 argtype = TREE_TYPE (arg);
400fbf9f 4192
3e4093b6 4193 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4194 to which the address will point. This is only needed
f2c1da78 4195 for function types. */
6615c446 4196 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4197 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4198 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4199 {
4200 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4201 int quals = orig_quals;
4202
4203 if (TREE_READONLY (arg))
4204 quals |= TYPE_QUAL_CONST;
4205 if (TREE_THIS_VOLATILE (arg))
4206 quals |= TYPE_QUAL_VOLATILE;
4207
f2c1da78
JM
4208 argtype = c_build_qualified_type (argtype, quals);
4209 }
400fbf9f 4210
3e4093b6
RS
4211 if (!c_mark_addressable (arg))
4212 return error_mark_node;
400fbf9f 4213
abb54d14
JM
4214 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4215 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4216
5cc200fc 4217 argtype = build_pointer_type (argtype);
5e55f99d
RH
4218
4219 /* ??? Cope with user tricks that amount to offsetof. Delete this
4220 when we have proper support for integer constant expressions. */
4221 val = get_base_address (arg);
22d03525 4222 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4223 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4224 {
cf9e9959 4225 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4226 goto return_build_unary_op;
3aa2ddb8 4227 }
5e55f99d 4228
5cc200fc 4229 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4230
c9f9eb5d
AH
4231 ret = val;
4232 goto return_build_unary_op;
400fbf9f 4233
3e4093b6 4234 default:
1344f9a3 4235 gcc_unreachable ();
3e4093b6 4236 }
400fbf9f 4237
3e4093b6
RS
4238 if (argtype == 0)
4239 argtype = TREE_TYPE (arg);
928c19bb
JM
4240 if (TREE_CODE (arg) == INTEGER_CST)
4241 ret = (require_constant_value
db3927fb
AH
4242 ? fold_build1_initializer_loc (location, code, argtype, arg)
4243 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4244 else
4245 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4246 return_build_unary_op:
4247 gcc_assert (ret != error_mark_node);
928c19bb
JM
4248 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4249 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4250 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4251 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4252 ret = note_integer_operands (ret);
8ce94e44
JM
4253 if (eptype)
4254 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4255 protected_set_expr_location (ret, location);
4256 return ret;
3e4093b6 4257}
400fbf9f 4258
3e4093b6
RS
4259/* Return nonzero if REF is an lvalue valid for this language.
4260 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4261 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4262
1e4ae551 4263bool
58f9752a 4264lvalue_p (const_tree ref)
3e4093b6 4265{
58f9752a 4266 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4267
3e4093b6
RS
4268 switch (code)
4269 {
4270 case REALPART_EXPR:
4271 case IMAGPART_EXPR:
4272 case COMPONENT_REF:
4273 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4274
928c19bb
JM
4275 case C_MAYBE_CONST_EXPR:
4276 return lvalue_p (TREE_OPERAND (ref, 1));
4277
3e4093b6
RS
4278 case COMPOUND_LITERAL_EXPR:
4279 case STRING_CST:
4280 return 1;
400fbf9f 4281
3e4093b6
RS
4282 case INDIRECT_REF:
4283 case ARRAY_REF:
36536d79 4284 case ARRAY_NOTATION_REF:
3e4093b6
RS
4285 case VAR_DECL:
4286 case PARM_DECL:
4287 case RESULT_DECL:
4288 case ERROR_MARK:
4289 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4290 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4291
3e4093b6 4292 case BIND_EXPR:
3e4093b6 4293 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4294
3e4093b6
RS
4295 default:
4296 return 0;
4297 }
4298}
400fbf9f 4299\f
f37acdf9
JM
4300/* Give a warning for storing in something that is read-only in GCC
4301 terms but not const in ISO C terms. */
4302
4303static void
4304readonly_warning (tree arg, enum lvalue_use use)
4305{
4306 switch (use)
4307 {
4308 case lv_assign:
4309 warning (0, "assignment of read-only location %qE", arg);
4310 break;
4311 case lv_increment:
4312 warning (0, "increment of read-only location %qE", arg);
4313 break;
4314 case lv_decrement:
4315 warning (0, "decrement of read-only location %qE", arg);
4316 break;
4317 default:
4318 gcc_unreachable ();
4319 }
4320 return;
4321}
4322
37dc0d8d
JM
4323
4324/* Return nonzero if REF is an lvalue valid for this language;
4325 otherwise, print an error message and return zero. USE says
7bd11157
TT
4326 how the lvalue is being used and so selects the error message.
4327 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4328
4329static int
7bd11157 4330lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4331{
4332 int win = lvalue_p (ref);
4333
4334 if (!win)
7bd11157 4335 lvalue_error (loc, use);
37dc0d8d
JM
4336
4337 return win;
4338}
3e4093b6
RS
4339\f
4340/* Mark EXP saying that we need to be able to take the
4341 address of it; it should not be allocated in a register.
4342 Returns true if successful. */
54c93c30 4343
3e4093b6
RS
4344bool
4345c_mark_addressable (tree exp)
400fbf9f 4346{
3e4093b6 4347 tree x = exp;
95602da1 4348
3e4093b6
RS
4349 while (1)
4350 switch (TREE_CODE (x))
4351 {
4352 case COMPONENT_REF:
4353 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4354 {
0039fa55
AN
4355 error
4356 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4357 return false;
4358 }
95602da1 4359
3e4093b6 4360 /* ... fall through ... */
95602da1 4361
3e4093b6
RS
4362 case ADDR_EXPR:
4363 case ARRAY_REF:
4364 case REALPART_EXPR:
4365 case IMAGPART_EXPR:
4366 x = TREE_OPERAND (x, 0);
4367 break;
95602da1 4368
3e4093b6
RS
4369 case COMPOUND_LITERAL_EXPR:
4370 case CONSTRUCTOR:
4371 TREE_ADDRESSABLE (x) = 1;
4372 return true;
95602da1 4373
3e4093b6
RS
4374 case VAR_DECL:
4375 case CONST_DECL:
4376 case PARM_DECL:
4377 case RESULT_DECL:
5baeaac0 4378 if (C_DECL_REGISTER (x)
3e4093b6
RS
4379 && DECL_NONLOCAL (x))
4380 {
62f9079a 4381 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4382 {
0039fa55
AN
4383 error
4384 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4385 return false;
4386 }
509c9d60 4387 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4388 }
5baeaac0 4389 else if (C_DECL_REGISTER (x))
3e4093b6 4390 {
62f9079a 4391 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4392 error ("address of global register variable %qD requested", x);
4393 else
4394 error ("address of register variable %qD requested", x);
4395 return false;
3e4093b6 4396 }
400fbf9f 4397
3e4093b6
RS
4398 /* drops in */
4399 case FUNCTION_DECL:
4400 TREE_ADDRESSABLE (x) = 1;
4401 /* drops out */
4402 default:
4403 return true;
4404 }
4405}
4406\f
2d2e923f
MLI
4407/* Convert EXPR to TYPE, warning about conversion problems with
4408 constants. SEMANTIC_TYPE is the type this conversion would use
4409 without excess precision. If SEMANTIC_TYPE is NULL, this function
4410 is equivalent to convert_and_check. This function is a wrapper that
4411 handles conversions that may be different than
4412 the usual ones because of excess precision. */
4413
4414static tree
68fca595
MP
4415ep_convert_and_check (location_t loc, tree type, tree expr,
4416 tree semantic_type)
2d2e923f
MLI
4417{
4418 if (TREE_TYPE (expr) == type)
4419 return expr;
4420
4421 if (!semantic_type)
68fca595 4422 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4423
4424 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4425 && TREE_TYPE (expr) != semantic_type)
4426 {
4427 /* For integers, we need to check the real conversion, not
4428 the conversion to the excess precision type. */
68fca595 4429 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4430 }
4431 /* Result type is the excess precision type, which should be
4432 large enough, so do not check. */
4433 return convert (type, expr);
4434}
4435
928c19bb
JM
4436/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4437 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4438 if folded to an integer constant then the unselected half may
4439 contain arbitrary operations not normally permitted in constant
c2255bc4 4440 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4441
4442tree
744aa42f 4443build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4444 tree op1, tree op1_original_type, tree op2,
4445 tree op2_original_type)
400fbf9f 4446{
3e4093b6
RS
4447 tree type1;
4448 tree type2;
4449 enum tree_code code1;
4450 enum tree_code code2;
4451 tree result_type = NULL;
2d2e923f 4452 tree semantic_result_type = NULL;
3e4093b6 4453 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4454 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4455 bool ifexp_int_operands;
928c19bb 4456 tree ret;
400fbf9f 4457
4d84fe7c
JM
4458 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4459 if (op1_int_operands)
4460 op1 = remove_c_maybe_const_expr (op1);
4461 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4462 if (op2_int_operands)
4463 op2 = remove_c_maybe_const_expr (op2);
4464 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4465 if (ifexp_int_operands)
4466 ifexp = remove_c_maybe_const_expr (ifexp);
4467
3e4093b6
RS
4468 /* Promote both alternatives. */
4469
4470 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4471 op1 = default_conversion (op1);
4472 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4473 op2 = default_conversion (op2);
4474
4475 if (TREE_CODE (ifexp) == ERROR_MARK
4476 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4477 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4478 return error_mark_node;
400fbf9f 4479
3e4093b6
RS
4480 type1 = TREE_TYPE (op1);
4481 code1 = TREE_CODE (type1);
4482 type2 = TREE_TYPE (op2);
4483 code2 = TREE_CODE (type2);
4484
b1adf557
JM
4485 /* C90 does not permit non-lvalue arrays in conditional expressions.
4486 In C99 they will be pointers by now. */
4487 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4488 {
744aa42f 4489 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4490 return error_mark_node;
4491 }
4492
8ce94e44
JM
4493 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4494 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4495 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4496 || code1 == COMPLEX_TYPE)
4497 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4498 || code2 == COMPLEX_TYPE))
4499 {
2d2e923f 4500 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4501 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4502 {
4503 op1 = TREE_OPERAND (op1, 0);
4504 type1 = TREE_TYPE (op1);
4505 gcc_assert (TREE_CODE (type1) == code1);
4506 }
4507 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4508 {
4509 op2 = TREE_OPERAND (op2, 0);
4510 type2 = TREE_TYPE (op2);
4511 gcc_assert (TREE_CODE (type2) == code2);
4512 }
4513 }
4514
d130ae11
ILT
4515 if (warn_cxx_compat)
4516 {
4517 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4518 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4519
4520 if (TREE_CODE (t1) == ENUMERAL_TYPE
4521 && TREE_CODE (t2) == ENUMERAL_TYPE
4522 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4523 warning_at (colon_loc, OPT_Wc___compat,
4524 ("different enum types in conditional is "
4525 "invalid in C++: %qT vs %qT"),
4526 t1, t2);
4527 }
4528
3e4093b6
RS
4529 /* Quickly detect the usual case where op1 and op2 have the same type
4530 after promotion. */
4531 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4532 {
3e4093b6
RS
4533 if (type1 == type2)
4534 result_type = type1;
4535 else
4536 result_type = TYPE_MAIN_VARIANT (type1);
4537 }
4538 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4539 || code1 == COMPLEX_TYPE)
4540 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4541 || code2 == COMPLEX_TYPE))
3e4093b6 4542 {
ccf7f880 4543 result_type = c_common_type (type1, type2);
0a0b3574
MM
4544 do_warn_double_promotion (result_type, type1, type2,
4545 "implicit conversion from %qT to %qT to "
4546 "match other result of conditional",
4547 colon_loc);
400fbf9f 4548
3e4093b6
RS
4549 /* If -Wsign-compare, warn here if type1 and type2 have
4550 different signedness. We'll promote the signed to unsigned
4551 and later code won't know it used to be different.
4552 Do this check on the original types, so that explicit casts
4553 will be considered, but default promotions won't. */
7d882b83 4554 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4555 {
8df83eae
RK
4556 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4557 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4558
3e4093b6
RS
4559 if (unsigned_op1 ^ unsigned_op2)
4560 {
6ac01510
ILT
4561 bool ovf;
4562
3e4093b6
RS
4563 /* Do not warn if the result type is signed, since the
4564 signed type will only be chosen if it can represent
4565 all the values of the unsigned type. */
3f75a254 4566 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4567 /* OK */;
3e4093b6 4568 else
928c19bb
JM
4569 {
4570 bool op1_maybe_const = true;
4571 bool op2_maybe_const = true;
4572
4573 /* Do not warn if the signed quantity is an
4574 unsuffixed integer literal (or some static
4575 constant expression involving such literals) and
4576 it is non-negative. This warning requires the
4577 operands to be folded for best results, so do
4578 that folding in this case even without
4579 warn_sign_compare to avoid warning options
4580 possibly affecting code generation. */
f5178456
RS
4581 c_inhibit_evaluation_warnings
4582 += (ifexp == truthvalue_false_node);
928c19bb
JM
4583 op1 = c_fully_fold (op1, require_constant_value,
4584 &op1_maybe_const);
f5178456
RS
4585 c_inhibit_evaluation_warnings
4586 -= (ifexp == truthvalue_false_node);
4587
4588 c_inhibit_evaluation_warnings
4589 += (ifexp == truthvalue_true_node);
928c19bb
JM
4590 op2 = c_fully_fold (op2, require_constant_value,
4591 &op2_maybe_const);
f5178456
RS
4592 c_inhibit_evaluation_warnings
4593 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4594
4595 if (warn_sign_compare)
4596 {
4597 if ((unsigned_op2
4598 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4599 || (unsigned_op1
4600 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4601 /* OK */;
4602 else
744aa42f
ILT
4603 warning_at (colon_loc, OPT_Wsign_compare,
4604 ("signed and unsigned type in "
4605 "conditional expression"));
928c19bb
JM
4606 }
4607 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4608 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4609 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4610 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4611 }
3e4093b6
RS
4612 }
4613 }
4614 }
4615 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4616 {
fcf73884 4617 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4618 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4619 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4620 result_type = void_type_node;
4621 }
4622 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4623 {
36c5e70a
BE
4624 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4625 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4626 addr_space_t as_common;
4627
744aa42f 4628 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4629 result_type = common_pointer_type (type1, type2);
6aa3c60d 4630 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4631 result_type = type2;
6aa3c60d 4632 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4633 result_type = type1;
4634 else if (!addr_space_superset (as1, as2, &as_common))
4635 {
4636 error_at (colon_loc, "pointers to disjoint address spaces "
4637 "used in conditional expression");
4638 return error_mark_node;
4639 }
267bac10
JM
4640 else if (VOID_TYPE_P (TREE_TYPE (type1))
4641 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4642 {
768952be
MU
4643 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4644 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4645 & ~TYPE_QUALS (TREE_TYPE (type1))))
4646 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4647 "pointer to array loses qualifier "
4648 "in conditional expression");
4649
fcf73884 4650 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4651 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4652 "ISO C forbids conditional expr between "
bda67431 4653 "%<void *%> and function pointer");
3e4093b6
RS
4654 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4655 TREE_TYPE (type2)));
34a80643 4656 }
267bac10
JM
4657 else if (VOID_TYPE_P (TREE_TYPE (type2))
4658 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4659 {
768952be
MU
4660 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4661 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4662 & ~TYPE_QUALS (TREE_TYPE (type2))))
4663 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4664 "pointer to array loses qualifier "
4665 "in conditional expression");
4666
fcf73884 4667 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4668 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4669 "ISO C forbids conditional expr between "
bda67431 4670 "%<void *%> and function pointer");
3e4093b6
RS
4671 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4672 TREE_TYPE (type1)));
1c2a9b35 4673 }
b581b85b
NP
4674 /* Objective-C pointer comparisons are a bit more lenient. */
4675 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4676 result_type = objc_common_type (type1, type2);
34a80643 4677 else
ab87f8c8 4678 {
36c5e70a
BE
4679 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4680
b581b85b
NP
4681 pedwarn (colon_loc, 0,
4682 "pointer type mismatch in conditional expression");
36c5e70a
BE
4683 result_type = build_pointer_type
4684 (build_qualified_type (void_type_node, qual));
ab87f8c8 4685 }
3e4093b6
RS
4686 }
4687 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4688 {
6aa3c60d 4689 if (!null_pointer_constant_p (orig_op2))
744aa42f 4690 pedwarn (colon_loc, 0,
509c9d60 4691 "pointer/integer type mismatch in conditional expression");
3e4093b6 4692 else
ab87f8c8 4693 {
3e4093b6 4694 op2 = null_pointer_node;
ab87f8c8 4695 }
3e4093b6
RS
4696 result_type = type1;
4697 }
4698 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4699 {
6aa3c60d 4700 if (!null_pointer_constant_p (orig_op1))
744aa42f 4701 pedwarn (colon_loc, 0,
509c9d60 4702 "pointer/integer type mismatch in conditional expression");
3e4093b6 4703 else
ab87f8c8 4704 {
3e4093b6 4705 op1 = null_pointer_node;
ab87f8c8 4706 }
3e4093b6
RS
4707 result_type = type2;
4708 }
1c2a9b35 4709
3e4093b6
RS
4710 if (!result_type)
4711 {
4712 if (flag_cond_mismatch)
4713 result_type = void_type_node;
4714 else
400fbf9f 4715 {
c2255bc4 4716 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4717 return error_mark_node;
400fbf9f 4718 }
3e4093b6 4719 }
400fbf9f 4720
3e4093b6
RS
4721 /* Merge const and volatile flags of the incoming types. */
4722 result_type
4723 = build_type_variant (result_type,
afbd0665
AS
4724 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4725 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4726
68fca595
MP
4727 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4728 semantic_result_type);
4729 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4730 semantic_result_type);
b6a10c9f 4731
928c19bb
JM
4732 if (ifexp_bcp && ifexp == truthvalue_true_node)
4733 {
4734 op2_int_operands = true;
4735 op1 = c_fully_fold (op1, require_constant_value, NULL);
4736 }
4737 if (ifexp_bcp && ifexp == truthvalue_false_node)
4738 {
4739 op1_int_operands = true;
4740 op2 = c_fully_fold (op2, require_constant_value, NULL);
4741 }
4d84fe7c 4742 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4743 && op1_int_operands
4744 && op2_int_operands);
4745 if (int_operands)
4746 {
4747 int_const = ((ifexp == truthvalue_true_node
4748 && TREE_CODE (orig_op1) == INTEGER_CST
4749 && !TREE_OVERFLOW (orig_op1))
4750 || (ifexp == truthvalue_false_node
4751 && TREE_CODE (orig_op2) == INTEGER_CST
4752 && !TREE_OVERFLOW (orig_op2)));
4753 }
4754 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4755 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4756 else
4757 {
01c7ccbb
JM
4758 if (int_operands)
4759 {
f34f1c87
MP
4760 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4761 nested inside of the expression. */
4762 op1 = c_fully_fold (op1, false, NULL);
4763 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4764 }
928c19bb
JM
4765 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4766 if (int_operands)
4767 ret = note_integer_operands (ret);
4768 }
2d2e923f
MLI
4769 if (semantic_result_type)
4770 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4771
c2255bc4 4772 protected_set_expr_location (ret, colon_loc);
928c19bb 4773 return ret;
3e4093b6
RS
4774}
4775\f
487a92fe 4776/* Return a compound expression that performs two expressions and
c2255bc4
AH
4777 returns the value of the second of them.
4778
4779 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4780
3e4093b6 4781tree
c2255bc4 4782build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4783{
4d84fe7c 4784 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4785 tree eptype = NULL_TREE;
928c19bb
JM
4786 tree ret;
4787
b72271b9 4788 if (flag_cilkplus
939b37da
BI
4789 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4790 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4791 {
4792 error_at (loc,
4793 "spawned function call cannot be part of a comma expression");
4794 return error_mark_node;
4795 }
4d84fe7c
JM
4796 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4797 if (expr1_int_operands)
4798 expr1 = remove_c_maybe_const_expr (expr1);
4799 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4800 if (expr2_int_operands)
4801 expr2 = remove_c_maybe_const_expr (expr2);
4802
8ce94e44
JM
4803 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4804 expr1 = TREE_OPERAND (expr1, 0);
4805 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4806 {
4807 eptype = TREE_TYPE (expr2);
4808 expr2 = TREE_OPERAND (expr2, 0);
4809 }
4810
3f75a254 4811 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4812 {
4813 /* The left-hand operand of a comma expression is like an expression
c5409249 4814 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4815 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4816 if (warn_unused_value)
47aecf47 4817 {
e14a6540 4818 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4819 && CONVERT_EXPR_P (expr1))
47aecf47 4820 ; /* (void) a, b */
e14a6540
JM
4821 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4822 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4823 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4824 ; /* (void) a, (void) b, c */
4825 else
b8698a0f 4826 warning_at (loc, OPT_Wunused_value,
c2255bc4 4827 "left-hand operand of comma expression has no effect");
47aecf47 4828 }
3e4093b6 4829 }
789eadcd
MP
4830 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4831 && warn_unused_value)
4832 {
4833 tree r = expr1;
4834 location_t cloc = loc;
4835 while (TREE_CODE (r) == COMPOUND_EXPR)
4836 {
4837 if (EXPR_HAS_LOCATION (r))
4838 cloc = EXPR_LOCATION (r);
4839 r = TREE_OPERAND (r, 1);
4840 }
4841 if (!TREE_SIDE_EFFECTS (r)
4842 && !VOID_TYPE_P (TREE_TYPE (r))
4843 && !CONVERT_EXPR_P (r))
4844 warning_at (cloc, OPT_Wunused_value,
4845 "right-hand operand of comma expression has no effect");
4846 }
400fbf9f 4847
3e4093b6
RS
4848 /* With -Wunused, we should also warn if the left-hand operand does have
4849 side-effects, but computes a value which is not used. For example, in
4850 `foo() + bar(), baz()' the result of the `+' operator is not used,
4851 so we should issue a warning. */
4852 else if (warn_unused_value)
c2255bc4 4853 warn_if_unused_value (expr1, loc);
400fbf9f 4854
e63d6886
AP
4855 if (expr2 == error_mark_node)
4856 return error_mark_node;
4857
928c19bb
JM
4858 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4859
4860 if (flag_isoc99
4d84fe7c
JM
4861 && expr1_int_operands
4862 && expr2_int_operands)
928c19bb
JM
4863 ret = note_integer_operands (ret);
4864
8ce94e44
JM
4865 if (eptype)
4866 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4867
c2255bc4 4868 protected_set_expr_location (ret, loc);
928c19bb 4869 return ret;
3e4093b6 4870}
400fbf9f 4871
67165eb3
ILT
4872/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4873 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4874 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4875 of the cast. -Wcast-qual appeared on the command line. Named
4876 address space qualifiers are not handled here, because they result
4877 in different warnings. */
67165eb3
ILT
4878
4879static void
2ee3cb35 4880handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4881{
4882 tree in_type = type;
4883 tree in_otype = otype;
4884 int added = 0;
4885 int discarded = 0;
4886 bool is_const;
4887
4888 /* Check that the qualifiers on IN_TYPE are a superset of the
4889 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4890 nodes is uninteresting and we stop as soon as we hit a
4891 non-POINTER_TYPE node on either type. */
4892 do
4893 {
4894 in_otype = TREE_TYPE (in_otype);
4895 in_type = TREE_TYPE (in_type);
4896
4897 /* GNU C allows cv-qualified function types. 'const' means the
4898 function is very pure, 'volatile' means it can't return. We
4899 need to warn when such qualifiers are added, not when they're
4900 taken away. */
4901 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4902 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4903 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4904 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4905 else
36c5e70a
BE
4906 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4907 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4908 }
4909 while (TREE_CODE (in_type) == POINTER_TYPE
4910 && TREE_CODE (in_otype) == POINTER_TYPE);
4911
4912 if (added)
2ee3cb35
MLI
4913 warning_at (loc, OPT_Wcast_qual,
4914 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4915
4916 if (discarded)
4917 /* There are qualifiers present in IN_OTYPE that are not present
4918 in IN_TYPE. */
2ee3cb35 4919 warning_at (loc, OPT_Wcast_qual,
7485aeea 4920 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4921 discarded);
67165eb3
ILT
4922
4923 if (added || discarded)
4924 return;
4925
4926 /* A cast from **T to const **T is unsafe, because it can cause a
4927 const value to be changed with no additional warning. We only
4928 issue this warning if T is the same on both sides, and we only
4929 issue the warning if there are the same number of pointers on
4930 both sides, as otherwise the cast is clearly unsafe anyhow. A
4931 cast is unsafe when a qualifier is added at one level and const
4932 is not present at all outer levels.
4933
4934 To issue this warning, we check at each level whether the cast
4935 adds new qualifiers not already seen. We don't need to special
4936 case function types, as they won't have the same
4937 TYPE_MAIN_VARIANT. */
4938
4939 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4940 return;
4941 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4942 return;
4943
4944 in_type = type;
4945 in_otype = otype;
4946 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4947 do
4948 {
4949 in_type = TREE_TYPE (in_type);
4950 in_otype = TREE_TYPE (in_otype);
4951 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4952 && !is_const)
4953 {
2ee3cb35
MLI
4954 warning_at (loc, OPT_Wcast_qual,
4955 "to be safe all intermediate pointers in cast from "
4956 "%qT to %qT must be %<const%> qualified",
4957 otype, type);
67165eb3
ILT
4958 break;
4959 }
4960 if (is_const)
4961 is_const = TYPE_READONLY (in_type);
4962 }
4963 while (TREE_CODE (in_type) == POINTER_TYPE);
4964}
4965
b8698a0f 4966/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4967 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4968
3e4093b6 4969tree
c2255bc4 4970build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4971{
8ce94e44
JM
4972 tree value;
4973
4974 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4975 expr = TREE_OPERAND (expr, 0);
4976
4977 value = expr;
400fbf9f 4978
3e4093b6
RS
4979 if (type == error_mark_node || expr == error_mark_node)
4980 return error_mark_node;
400fbf9f 4981
3e4093b6
RS
4982 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4983 only in <protocol> qualifications. But when constructing cast expressions,
4984 the protocols do matter and must be kept around. */
700686fa
ZL
4985 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4986 return build1 (NOP_EXPR, type, expr);
4987
4988 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4989
3e4093b6
RS
4990 if (TREE_CODE (type) == ARRAY_TYPE)
4991 {
c2255bc4 4992 error_at (loc, "cast specifies array type");
3e4093b6
RS
4993 return error_mark_node;
4994 }
400fbf9f 4995
3e4093b6
RS
4996 if (TREE_CODE (type) == FUNCTION_TYPE)
4997 {
c2255bc4 4998 error_at (loc, "cast specifies function type");
3e4093b6
RS
4999 return error_mark_node;
5000 }
400fbf9f 5001
808d6eaa
JM
5002 if (!VOID_TYPE_P (type))
5003 {
5004 value = require_complete_type (value);
5005 if (value == error_mark_node)
5006 return error_mark_node;
5007 }
5008
3e4093b6
RS
5009 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5010 {
fcf73884
MLI
5011 if (TREE_CODE (type) == RECORD_TYPE
5012 || TREE_CODE (type) == UNION_TYPE)
c1771a20 5013 pedwarn (loc, OPT_Wpedantic,
fcf73884 5014 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5015
5016 /* Convert to remove any qualifiers from VALUE's type. */
5017 value = convert (type, value);
3e4093b6
RS
5018 }
5019 else if (TREE_CODE (type) == UNION_TYPE)
5020 {
5021 tree field;
400fbf9f 5022
910ad8de 5023 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5024 if (TREE_TYPE (field) != error_mark_node
5025 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5026 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5027 break;
5028
5029 if (field)
400fbf9f 5030 {
3e4093b6 5031 tree t;
e616f54d 5032 bool maybe_const = true;
3e4093b6 5033
c1771a20 5034 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5035 t = c_fully_fold (value, false, &maybe_const);
5036 t = build_constructor_single (type, field, t);
5037 if (!maybe_const)
5038 t = c_wrap_maybe_const (t, true);
5039 t = digest_init (loc, type, t,
bbbbb16a 5040 NULL_TREE, false, true, 0);
3e4093b6
RS
5041 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5042 return t;
400fbf9f 5043 }
c2255bc4 5044 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5045 return error_mark_node;
5046 }
5047 else
5048 {
5049 tree otype, ovalue;
400fbf9f 5050
3e4093b6 5051 if (type == void_type_node)
c2255bc4
AH
5052 {
5053 tree t = build1 (CONVERT_EXPR, type, value);
5054 SET_EXPR_LOCATION (t, loc);
5055 return t;
5056 }
400fbf9f 5057
3e4093b6 5058 otype = TREE_TYPE (value);
400fbf9f 5059
3e4093b6 5060 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5061 if (warn_cast_qual
5062 && TREE_CODE (type) == POINTER_TYPE
5063 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5064 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5065
36c5e70a
BE
5066 /* Warn about conversions between pointers to disjoint
5067 address spaces. */
5068 if (TREE_CODE (type) == POINTER_TYPE
5069 && TREE_CODE (otype) == POINTER_TYPE
5070 && !null_pointer_constant_p (value))
5071 {
5072 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5073 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5074 addr_space_t as_common;
5075
5076 if (!addr_space_superset (as_to, as_from, &as_common))
5077 {
5078 if (ADDR_SPACE_GENERIC_P (as_from))
5079 warning_at (loc, 0, "cast to %s address space pointer "
5080 "from disjoint generic address space pointer",
5081 c_addr_space_name (as_to));
5082
5083 else if (ADDR_SPACE_GENERIC_P (as_to))
5084 warning_at (loc, 0, "cast to generic address space pointer "
5085 "from disjoint %s address space pointer",
5086 c_addr_space_name (as_from));
5087
5088 else
5089 warning_at (loc, 0, "cast to %s address space pointer "
5090 "from disjoint %s address space pointer",
5091 c_addr_space_name (as_to),
5092 c_addr_space_name (as_from));
5093 }
5094 }
5095
3e4093b6 5096 /* Warn about possible alignment problems. */
3176a0c2 5097 if (STRICT_ALIGNMENT
3e4093b6
RS
5098 && TREE_CODE (type) == POINTER_TYPE
5099 && TREE_CODE (otype) == POINTER_TYPE
5100 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5101 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5102 /* Don't warn about opaque types, where the actual alignment
5103 restriction is unknown. */
5104 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5105 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5106 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5107 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5108 warning_at (loc, OPT_Wcast_align,
5109 "cast increases required alignment of target type");
e9a25f70 5110
3176a0c2 5111 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5112 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5113 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5114 /* Unlike conversion of integers to pointers, where the
5115 warning is disabled for converting constants because
5116 of cases such as SIG_*, warn about converting constant
5117 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5118 sign extension, and a warning is appropriate. */
c2255bc4
AH
5119 warning_at (loc, OPT_Wpointer_to_int_cast,
5120 "cast from pointer to integer of different size");
400fbf9f 5121
3176a0c2 5122 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5123 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5124 warning_at (loc, OPT_Wbad_function_cast,
5125 "cast from function call of type %qT "
5126 "to non-matching type %qT", otype, type);
400fbf9f 5127
3176a0c2 5128 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5129 && TREE_CODE (otype) == INTEGER_TYPE
5130 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5131 /* Don't warn about converting any constant. */
5132 && !TREE_CONSTANT (value))
c2255bc4
AH
5133 warning_at (loc,
5134 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5135 "of different size");
400fbf9f 5136
79bedddc
SR
5137 if (warn_strict_aliasing <= 2)
5138 strict_aliasing_warning (otype, type, expr);
400fbf9f 5139
3897f229
JM
5140 /* If pedantic, warn for conversions between function and object
5141 pointer types, except for converting a null pointer constant
5142 to function pointer type. */
5143 if (pedantic
5144 && TREE_CODE (type) == POINTER_TYPE
5145 && TREE_CODE (otype) == POINTER_TYPE
5146 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5147 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5148 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5149 "conversion of function pointer to object pointer type");
3897f229
JM
5150
5151 if (pedantic
5152 && TREE_CODE (type) == POINTER_TYPE
5153 && TREE_CODE (otype) == POINTER_TYPE
5154 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5155 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5156 && !null_pointer_constant_p (value))
c1771a20 5157 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5158 "conversion of object pointer to function pointer type");
3897f229 5159
3e4093b6 5160 ovalue = value;
3e4093b6 5161 value = convert (type, value);
400fbf9f 5162
3e4093b6 5163 /* Ignore any integer overflow caused by the cast. */
928c19bb 5164 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5165 {
8bcd6380 5166 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5167 {
8bcd6380
RS
5168 if (!TREE_OVERFLOW (value))
5169 {
5170 /* Avoid clobbering a shared constant. */
5171 value = copy_node (value);
5172 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5173 }
6414bad6 5174 }
8bcd6380 5175 else if (TREE_OVERFLOW (value))
d8e1f97b 5176 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5177 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5178 }
5179 }
400fbf9f 5180
53cd18ec 5181 /* Don't let a cast be an lvalue. */
9482b620 5182 if (lvalue_p (value))
db3927fb 5183 value = non_lvalue_loc (loc, value);
e9a25f70 5184
928c19bb
JM
5185 /* Don't allow the results of casting to floating-point or complex
5186 types be confused with actual constants, or casts involving
5187 integer and pointer types other than direct integer-to-integer
5188 and integer-to-pointer be confused with integer constant
5189 expressions and null pointer constants. */
5190 if (TREE_CODE (value) == REAL_CST
5191 || TREE_CODE (value) == COMPLEX_CST
5192 || (TREE_CODE (value) == INTEGER_CST
5193 && !((TREE_CODE (expr) == INTEGER_CST
5194 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5195 || TREE_CODE (expr) == REAL_CST
5196 || TREE_CODE (expr) == COMPLEX_CST)))
5197 value = build1 (NOP_EXPR, type, value);
5198
c2255bc4
AH
5199 if (CAN_HAVE_LOCATION_P (value))
5200 SET_EXPR_LOCATION (value, loc);
3e4093b6 5201 return value;
400fbf9f
JW
5202}
5203
c2255bc4
AH
5204/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5205 location of the open paren of the cast, or the position of the cast
5206 expr. */
3e4093b6 5207tree
c2255bc4 5208c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5209{
f8893e47 5210 tree type;
928c19bb
JM
5211 tree type_expr = NULL_TREE;
5212 bool type_expr_const = true;
5213 tree ret;
3e4093b6 5214 int saved_wsp = warn_strict_prototypes;
c5c76735 5215
3e4093b6
RS
5216 /* This avoids warnings about unprototyped casts on
5217 integers. E.g. "#define SIG_DFL (void(*)())0". */
5218 if (TREE_CODE (expr) == INTEGER_CST)
5219 warn_strict_prototypes = 0;
928c19bb 5220 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5221 warn_strict_prototypes = saved_wsp;
c5c76735 5222
c2255bc4 5223 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5224 if (type_expr)
5225 {
9f33203d
JM
5226 bool inner_expr_const = true;
5227 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5228 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5229 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5230 && inner_expr_const);
c2255bc4 5231 SET_EXPR_LOCATION (ret, loc);
928c19bb 5232 }
24b97832
ILT
5233
5234 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5235 SET_EXPR_LOCATION (ret, loc);
5236
9e5b2115
PB
5237 /* C++ does not permits types to be defined in a cast, but it
5238 allows references to incomplete types. */
5239 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5240 warning_at (loc, OPT_Wc___compat,
5241 "defining a type in a cast is invalid in C++");
5242
928c19bb 5243 return ret;
400fbf9f 5244}
3e4093b6
RS
5245\f
5246/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5247 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5248 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5249 MODIFYCODE is the code for a binary operator that we use
5250 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5251 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5252 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5253 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5254
c2255bc4
AH
5255 LOCATION is the location of the MODIFYCODE operator.
5256 RHS_LOC is the location of the RHS. */
2f6e4e97 5257
3e4093b6 5258tree
32e8bb8e 5259build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5260 enum tree_code modifycode,
c2255bc4 5261 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5262{
3e4093b6
RS
5263 tree result;
5264 tree newrhs;
241f845a 5265 tree rhseval = NULL_TREE;
8ce94e44 5266 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5267 tree lhstype = TREE_TYPE (lhs);
5268 tree olhstype = lhstype;
928c19bb 5269 bool npc;
267bac10 5270 bool is_atomic_op;
e9a25f70 5271
3e4093b6
RS
5272 /* Types that aren't fully specified cannot be used in assignments. */
5273 lhs = require_complete_type (lhs);
e9a25f70 5274
3e4093b6
RS
5275 /* Avoid duplicate error messages from operands that had errors. */
5276 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5277 return error_mark_node;
400fbf9f 5278
4c2ecab0
JM
5279 /* Ensure an error for assigning a non-lvalue array to an array in
5280 C90. */
5281 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5282 {
5283 error_at (location, "assignment to expression with array type");
5284 return error_mark_node;
5285 }
5286
46a88c12 5287 /* For ObjC properties, defer this check. */
7bd11157 5288 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5289 return error_mark_node;
5290
267bac10
JM
5291 is_atomic_op = really_atomic_lvalue (lhs);
5292
8ce94e44
JM
5293 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5294 {
5295 rhs_semantic_type = TREE_TYPE (rhs);
5296 rhs = TREE_OPERAND (rhs, 0);
5297 }
5298
3e4093b6 5299 newrhs = rhs;
400fbf9f 5300
928c19bb
JM
5301 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5302 {
5303 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5304 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5305 rhs_origtype);
928c19bb
JM
5306 if (inner == error_mark_node)
5307 return error_mark_node;
5308 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5309 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5310 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5311 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5312 protected_set_expr_location (result, location);
5313 return result;
5314 }
5315
3e4093b6
RS
5316 /* If a binary op has been requested, combine the old LHS value with the RHS
5317 producing the value we should actually store into the LHS. */
5318
5319 if (modifycode != NOP_EXPR)
400fbf9f 5320 {
928c19bb 5321 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5322 lhs = stabilize_reference (lhs);
bbbbb16a 5323
267bac10
JM
5324 /* Construct the RHS for any non-atomic compound assignemnt. */
5325 if (!is_atomic_op)
5326 {
241f845a
JJ
5327 /* If in LHS op= RHS the RHS has side-effects, ensure they
5328 are preevaluated before the rest of the assignment expression's
5329 side-effects, because RHS could contain e.g. function calls
5330 that modify LHS. */
5331 if (TREE_SIDE_EFFECTS (rhs))
5332 {
5333 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5334 rhseval = newrhs;
5335 }
267bac10 5336 newrhs = build_binary_op (location,
241f845a 5337 modifycode, lhs, newrhs, 1);
267bac10
JM
5338
5339 /* The original type of the right hand side is no longer
5340 meaningful. */
5341 rhs_origtype = NULL_TREE;
5342 }
400fbf9f 5343 }
400fbf9f 5344
668ea4b1
IS
5345 if (c_dialect_objc ())
5346 {
46a88c12
NP
5347 /* Check if we are modifying an Objective-C property reference;
5348 if so, we need to generate setter calls. */
5349 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5350 if (result)
241f845a 5351 goto return_result;
46a88c12
NP
5352
5353 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5354 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5355 return error_mark_node;
5356 }
5357
9bf24266 5358 /* Give an error for storing in something that is 'const'. */
bbbd6700 5359
f37acdf9 5360 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5361 || ((TREE_CODE (lhstype) == RECORD_TYPE
5362 || TREE_CODE (lhstype) == UNION_TYPE)
5363 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5364 {
c02065fc 5365 readonly_error (location, lhs, lv_assign);
953ff289
DN
5366 return error_mark_node;
5367 }
f37acdf9
JM
5368 else if (TREE_READONLY (lhs))
5369 readonly_warning (lhs, lv_assign);
bbbd6700 5370
3e4093b6
RS
5371 /* If storing into a structure or union member,
5372 it has probably been given type `int'.
5373 Compute the type that would go with
5374 the actual amount of storage the member occupies. */
bbbd6700 5375
3e4093b6
RS
5376 if (TREE_CODE (lhs) == COMPONENT_REF
5377 && (TREE_CODE (lhstype) == INTEGER_TYPE
5378 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5379 || TREE_CODE (lhstype) == REAL_TYPE
5380 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5381 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5382
3e4093b6
RS
5383 /* If storing in a field that is in actuality a short or narrower than one,
5384 we must store in the field in its actual type. */
5385
5386 if (lhstype != TREE_TYPE (lhs))
5387 {
5388 lhs = copy_node (lhs);
5389 TREE_TYPE (lhs) = lhstype;
400fbf9f 5390 }
400fbf9f 5391
32e8bb8e
ILT
5392 /* Issue -Wc++-compat warnings about an assignment to an enum type
5393 when LHS does not have its original type. This happens for,
5394 e.g., an enum bitfield in a struct. */
5395 if (warn_cxx_compat
5396 && lhs_origtype != NULL_TREE
5397 && lhs_origtype != lhstype
5398 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5399 {
5400 tree checktype = (rhs_origtype != NULL_TREE
5401 ? rhs_origtype
5402 : TREE_TYPE (rhs));
5403 if (checktype != error_mark_node
267bac10
JM
5404 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5405 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5406 warning_at (location, OPT_Wc___compat,
5407 "enum conversion in assignment is invalid in C++");
5408 }
5409
267bac10
JM
5410 /* If the lhs is atomic, remove that qualifier. */
5411 if (is_atomic_op)
5412 {
5413 lhstype = build_qualified_type (lhstype,
5414 (TYPE_QUALS (lhstype)
5415 & ~TYPE_QUAL_ATOMIC));
5416 olhstype = build_qualified_type (olhstype,
5417 (TYPE_QUALS (lhstype)
5418 & ~TYPE_QUAL_ATOMIC));
5419 }
5420
8ce94e44
JM
5421 /* Convert new value to destination type. Fold it first, then
5422 restore any excess precision information, for the sake of
5423 conversion warnings. */
400fbf9f 5424
267bac10
JM
5425 if (!(is_atomic_op && modifycode != NOP_EXPR))
5426 {
5427 npc = null_pointer_constant_p (newrhs);
5428 newrhs = c_fully_fold (newrhs, false, NULL);
5429 if (rhs_semantic_type)
5430 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5431 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5432 rhs_origtype, ic_assign, npc,
5433 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5434 if (TREE_CODE (newrhs) == ERROR_MARK)
5435 return error_mark_node;
5436 }
400fbf9f 5437
6e955430
ZL
5438 /* Emit ObjC write barrier, if necessary. */
5439 if (c_dialect_objc () && flag_objc_gc)
5440 {
5441 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5442 if (result)
c9f9eb5d
AH
5443 {
5444 protected_set_expr_location (result, location);
241f845a 5445 goto return_result;
c9f9eb5d 5446 }
6e955430
ZL
5447 }
5448
ea4b7848 5449 /* Scan operands. */
400fbf9f 5450
267bac10
JM
5451 if (is_atomic_op)
5452 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5453 else
5454 {
5455 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5456 TREE_SIDE_EFFECTS (result) = 1;
5457 protected_set_expr_location (result, location);
5458 }
400fbf9f 5459
3e4093b6
RS
5460 /* If we got the LHS in a different type for storing in,
5461 convert the result back to the nominal type of LHS
5462 so that the value we return always has the same type
5463 as the LHS argument. */
e855c5ce 5464
3e4093b6 5465 if (olhstype == TREE_TYPE (result))
241f845a 5466 goto return_result;
c9f9eb5d 5467
68fca595
MP
5468 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5469 rhs_origtype, ic_assign, false, NULL_TREE,
5470 NULL_TREE, 0);
c9f9eb5d 5471 protected_set_expr_location (result, location);
241f845a
JJ
5472
5473return_result:
5474 if (rhseval)
5475 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5476 return result;
3e4093b6
RS
5477}
5478\f
478a1c5b
ILT
5479/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5480 This is used to implement -fplan9-extensions. */
5481
5482static bool
5483find_anonymous_field_with_type (tree struct_type, tree type)
5484{
5485 tree field;
5486 bool found;
5487
5488 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5489 || TREE_CODE (struct_type) == UNION_TYPE);
5490 found = false;
5491 for (field = TYPE_FIELDS (struct_type);
5492 field != NULL_TREE;
5493 field = TREE_CHAIN (field))
5494 {
267bac10
JM
5495 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5496 ? c_build_qualified_type (TREE_TYPE (field),
5497 TYPE_QUAL_ATOMIC)
5498 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5499 if (DECL_NAME (field) == NULL
267bac10 5500 && comptypes (type, fieldtype))
478a1c5b
ILT
5501 {
5502 if (found)
5503 return false;
5504 found = true;
5505 }
5506 else if (DECL_NAME (field) == NULL
5507 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5508 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5509 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5510 {
5511 if (found)
5512 return false;
5513 found = true;
5514 }
5515 }
5516 return found;
5517}
5518
5519/* RHS is an expression whose type is pointer to struct. If there is
5520 an anonymous field in RHS with type TYPE, then return a pointer to
5521 that field in RHS. This is used with -fplan9-extensions. This
5522 returns NULL if no conversion could be found. */
5523
5524static tree
5525convert_to_anonymous_field (location_t location, tree type, tree rhs)
5526{
5527 tree rhs_struct_type, lhs_main_type;
5528 tree field, found_field;
5529 bool found_sub_field;
5530 tree ret;
5531
5532 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5533 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5534 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5535 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5536
5537 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5538 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5539 ? c_build_qualified_type (TREE_TYPE (type),
5540 TYPE_QUAL_ATOMIC)
5541 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5542
5543 found_field = NULL_TREE;
5544 found_sub_field = false;
5545 for (field = TYPE_FIELDS (rhs_struct_type);
5546 field != NULL_TREE;
5547 field = TREE_CHAIN (field))
5548 {
5549 if (DECL_NAME (field) != NULL_TREE
5550 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5551 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5552 continue;
267bac10
JM
5553 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5554 ? c_build_qualified_type (TREE_TYPE (field),
5555 TYPE_QUAL_ATOMIC)
5556 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5557 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5558 {
5559 if (found_field != NULL_TREE)
5560 return NULL_TREE;
5561 found_field = field;
5562 }
5563 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5564 lhs_main_type))
5565 {
5566 if (found_field != NULL_TREE)
5567 return NULL_TREE;
5568 found_field = field;
5569 found_sub_field = true;
5570 }
5571 }
5572
5573 if (found_field == NULL_TREE)
5574 return NULL_TREE;
5575
5576 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5577 build_fold_indirect_ref (rhs), found_field,
5578 NULL_TREE);
5579 ret = build_fold_addr_expr_loc (location, ret);
5580
5581 if (found_sub_field)
5582 {
5583 ret = convert_to_anonymous_field (location, type, ret);
5584 gcc_assert (ret != NULL_TREE);
5585 }
5586
5587 return ret;
5588}
5589
63bc4e87
MP
5590/* Issue an error message for a bad initializer component.
5591 GMSGID identifies the message.
5592 The component name is taken from the spelling stack. */
5593
5594static void
ea58ef42 5595error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5596{
5597 char *ofwhat;
5598
5599 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5600 error_at (loc, gmsgid);
63bc4e87
MP
5601 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5602 if (*ofwhat)
d7ff7ae5 5603 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5604}
5605
5606/* Issue a pedantic warning for a bad initializer component. OPT is
5607 the option OPT_* (from options.h) controlling this warning or 0 if
5608 it is unconditionally given. GMSGID identifies the message. The
5609 component name is taken from the spelling stack. */
5610
5611static void
5612pedwarn_init (location_t location, int opt, const char *gmsgid)
5613{
5614 char *ofwhat;
d7ff7ae5 5615 bool warned;
63bc4e87
MP
5616
5617 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5618 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5619 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5620 if (*ofwhat && warned)
5621 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5622}
5623
5624/* Issue a warning for a bad initializer component.
5625
5626 OPT is the OPT_W* value corresponding to the warning option that
5627 controls this warning. GMSGID identifies the message. The
5628 component name is taken from the spelling stack. */
5629
5630static void
5631warning_init (location_t loc, int opt, const char *gmsgid)
5632{
5633 char *ofwhat;
d7ff7ae5 5634 bool warned;
63bc4e87
MP
5635
5636 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5637 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5638 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5639 if (*ofwhat && warned)
5640 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5641}
5642\f
5643/* If TYPE is an array type and EXPR is a parenthesized string
5644 constant, warn if pedantic that EXPR is being used to initialize an
5645 object of type TYPE. */
5646
5647void
d033409e 5648maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5649{
5650 if (pedantic
5651 && TREE_CODE (type) == ARRAY_TYPE
5652 && TREE_CODE (expr.value) == STRING_CST
5653 && expr.original_code != STRING_CST)
d033409e 5654 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5655 "array initialized from parenthesized string constant");
5656}
5657
bbbbb16a
ILT
5658/* Convert value RHS to type TYPE as preparation for an assignment to
5659 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5660 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5661 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5662 constant before any folding.
3e4093b6
RS
5663 The real work of conversion is done by `convert'.
5664 The purpose of this function is to generate error messages
5665 for assignments that are not allowed in C.
2ac2f164
JM
5666 ERRTYPE says whether it is argument passing, assignment,
5667 initialization or return.
2f6e4e97 5668
81e5eca8
MP
5669 LOCATION is the location of the assignment, EXPR_LOC is the location of
5670 the RHS or, for a function, location of an argument.
2ac2f164 5671 FUNCTION is a tree for the function being called.
3e4093b6 5672 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5673
3e4093b6 5674static tree
81e5eca8 5675convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5676 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5677 bool null_pointer_constant, tree fundecl,
5678 tree function, int parmnum)
3e4093b6
RS
5679{
5680 enum tree_code codel = TREE_CODE (type);
8ce94e44 5681 tree orig_rhs = rhs;
3e4093b6
RS
5682 tree rhstype;
5683 enum tree_code coder;
2ac2f164 5684 tree rname = NULL_TREE;
58393038 5685 bool objc_ok = false;
2ac2f164 5686
6b4ef5c1 5687 if (errtype == ic_argpass)
2ac2f164
JM
5688 {
5689 tree selector;
5690 /* Change pointer to function to the function itself for
5691 diagnostics. */
5692 if (TREE_CODE (function) == ADDR_EXPR
5693 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5694 function = TREE_OPERAND (function, 0);
5695
5696 /* Handle an ObjC selector specially for diagnostics. */
5697 selector = objc_message_selector ();
5698 rname = function;
5699 if (selector && parmnum > 2)
5700 {
5701 rname = selector;
5702 parmnum -= 2;
5703 }
5704 }
5705
5706 /* This macro is used to emit diagnostics to ensure that all format
5707 strings are complete sentences, visible to gettext and checked at
5708 compile time. */
768952be 5709#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5710 do { \
5711 switch (errtype) \
5712 { \
5713 case ic_argpass: \
5c1bc275 5714 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5715 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5716 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5717 "expected %qT but argument is of type %qT", \
5718 type, rhstype); \
5719 break; \
1e053dfe
MLI
5720 case ic_assign: \
5721 pedwarn (LOCATION, OPT, AS); \
5722 break; \
5723 case ic_init: \
6a8f4e12 5724 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5725 break; \
5726 case ic_return: \
d033409e 5727 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5728 break; \
5729 default: \
5730 gcc_unreachable (); \
5731 } \
2ac2f164 5732 } while (0)
cb3ca04e 5733
49706e39
MLI
5734 /* This macro is used to emit diagnostics to ensure that all format
5735 strings are complete sentences, visible to gettext and checked at
768952be 5736 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5737 extra parameter to enumerate qualifiers. */
768952be 5738#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5739 do { \
5740 switch (errtype) \
5741 { \
5742 case ic_argpass: \
5c1bc275 5743 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5744 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5745 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5746 "expected %qT but argument is of type %qT", \
5747 type, rhstype); \
5748 break; \
5749 case ic_assign: \
5c1bc275 5750 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5751 break; \
5752 case ic_init: \
5c1bc275 5753 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5754 break; \
5755 case ic_return: \
5c1bc275 5756 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5757 break; \
5758 default: \
5759 gcc_unreachable (); \
5760 } \
5761 } while (0)
5762
768952be
MU
5763 /* This macro is used to emit diagnostics to ensure that all format
5764 strings are complete sentences, visible to gettext and checked at
5765 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5766 warning_at instead of pedwarn. */
5767#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5768 do { \
5769 switch (errtype) \
5770 { \
5771 case ic_argpass: \
5772 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5773 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5774 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5775 "expected %qT but argument is of type %qT", \
5776 type, rhstype); \
5777 break; \
5778 case ic_assign: \
5779 warning_at (LOCATION, OPT, AS, QUALS); \
5780 break; \
5781 case ic_init: \
5782 warning_at (LOCATION, OPT, IN, QUALS); \
5783 break; \
5784 case ic_return: \
5785 warning_at (LOCATION, OPT, RE, QUALS); \
5786 break; \
5787 default: \
5788 gcc_unreachable (); \
5789 } \
5790 } while (0)
5791
8ce94e44
JM
5792 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5793 rhs = TREE_OPERAND (rhs, 0);
5794
3e4093b6
RS
5795 rhstype = TREE_TYPE (rhs);
5796 coder = TREE_CODE (rhstype);
5797
5798 if (coder == ERROR_MARK)
5799 return error_mark_node;
5800
58393038
ZL
5801 if (c_dialect_objc ())
5802 {
5803 int parmno;
5804
5805 switch (errtype)
5806 {
5807 case ic_return:
5808 parmno = 0;
5809 break;
5810
5811 case ic_assign:
5812 parmno = -1;
5813 break;
5814
5815 case ic_init:
5816 parmno = -2;
5817 break;
5818
5819 default:
5820 parmno = parmnum;
5821 break;
5822 }
5823
5824 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5825 }
5826
bbbbb16a
ILT
5827 if (warn_cxx_compat)
5828 {
5829 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5830 if (checktype != error_mark_node
5831 && TREE_CODE (type) == ENUMERAL_TYPE
5832 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5833 {
768952be
MU
5834 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5835 G_("enum conversion when passing argument "
5836 "%d of %qE is invalid in C++"),
5837 G_("enum conversion in assignment is "
5838 "invalid in C++"),
5839 G_("enum conversion in initialization is "
5840 "invalid in C++"),
5841 G_("enum conversion in return is "
5842 "invalid in C++"));
bbbbb16a
ILT
5843 }
5844 }
5845
3e4093b6 5846 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5847 return rhs;
3e4093b6
RS
5848
5849 if (coder == VOID_TYPE)
400fbf9f 5850 {
6dcc04b0
JM
5851 /* Except for passing an argument to an unprototyped function,
5852 this is a constraint violation. When passing an argument to
5853 an unprototyped function, it is compile-time undefined;
5854 making it a constraint in that case was rejected in
5855 DR#252. */
c2255bc4 5856 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5857 return error_mark_node;
400fbf9f 5858 }
808d6eaa
JM
5859 rhs = require_complete_type (rhs);
5860 if (rhs == error_mark_node)
5861 return error_mark_node;
cd192ccc
MS
5862 /* A non-reference type can convert to a reference. This handles
5863 va_start, va_copy and possibly port built-ins. */
5864 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5865 {
3e4093b6 5866 if (!lvalue_p (rhs))
400fbf9f 5867 {
c2255bc4 5868 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5869 return error_mark_node;
400fbf9f 5870 }
3e4093b6
RS
5871 if (!c_mark_addressable (rhs))
5872 return error_mark_node;
5873 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5874 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5875
81e5eca8 5876 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5877 build_pointer_type (TREE_TYPE (type)),
5878 rhs, origtype, errtype,
5879 null_pointer_constant, fundecl, function,
5880 parmnum);
cd192ccc
MS
5881 if (rhs == error_mark_node)
5882 return error_mark_node;
3e4093b6
RS
5883
5884 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5885 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5886 return rhs;
400fbf9f 5887 }
3e4093b6 5888 /* Some types can interconvert without explicit casts. */
3274deff 5889 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5890 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5891 return convert (type, rhs);
5892 /* Arithmetic types all interconvert, and enum is treated like int. */
5893 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5894 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5895 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5896 || codel == BOOLEAN_TYPE)
5897 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5898 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5899 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5900 || coder == BOOLEAN_TYPE))
928c19bb
JM
5901 {
5902 tree ret;
5903 bool save = in_late_binary_op;
e5341100
JJ
5904 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5905 || (coder == REAL_TYPE
5906 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5907 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 5908 in_late_binary_op = true;
81e5eca8
MP
5909 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5910 ? expr_loc : location, type, orig_rhs);
e5341100 5911 in_late_binary_op = save;
928c19bb
JM
5912 return ret;
5913 }
400fbf9f 5914
79077aea
JJ
5915 /* Aggregates in different TUs might need conversion. */
5916 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5917 && codel == coder
5918 && comptypes (type, rhstype))
81e5eca8
MP
5919 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5920 ? expr_loc : location, type, rhs);
79077aea 5921
ebf0bf7f 5922 /* Conversion to a transparent union or record from its member types.
3e4093b6 5923 This applies only to function arguments. */
ebf0bf7f
JJ
5924 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5925 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5926 && errtype == ic_argpass)
400fbf9f 5927 {
0257e383 5928 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5929
910ad8de 5930 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5931 {
0257e383 5932 tree memb_type = TREE_TYPE (memb);
400fbf9f 5933
3e4093b6 5934 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5935 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5936 break;
e58cd767 5937
3e4093b6
RS
5938 if (TREE_CODE (memb_type) != POINTER_TYPE)
5939 continue;
2f6e4e97 5940
3e4093b6
RS
5941 if (coder == POINTER_TYPE)
5942 {
5943 tree ttl = TREE_TYPE (memb_type);
5944 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5945
3e4093b6
RS
5946 /* Any non-function converts to a [const][volatile] void *
5947 and vice versa; otherwise, targets must be the same.
5948 Meanwhile, the lhs target must have all the qualifiers of
5949 the rhs. */
267bac10
JM
5950 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5951 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5952 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5953 {
267bac10
JM
5954 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5955 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5956 /* If this type won't generate any warnings, use it. */
267bac10 5957 if (lquals == rquals
3e4093b6
RS
5958 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5959 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5960 ? ((lquals | rquals) == rquals)
5961 : ((lquals | rquals) == lquals)))
3e4093b6 5962 break;
400fbf9f 5963
3e4093b6 5964 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5965 if (!marginal_memb)
5966 marginal_memb = memb;
3e4093b6
RS
5967 }
5968 }
82bde854 5969
3e4093b6 5970 /* Can convert integer zero to any pointer type. */
928c19bb 5971 if (null_pointer_constant)
3e4093b6
RS
5972 {
5973 rhs = null_pointer_node;
5974 break;
5975 }
5976 }
400fbf9f 5977
0257e383 5978 if (memb || marginal_memb)
3e4093b6 5979 {
0257e383 5980 if (!memb)
3e4093b6
RS
5981 {
5982 /* We have only a marginally acceptable member type;
5983 it needs a warning. */
0257e383 5984 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5985 tree ttr = TREE_TYPE (rhstype);
714a0864 5986
3e4093b6
RS
5987 /* Const and volatile mean something different for function
5988 types, so the usual warnings are not appropriate. */
5989 if (TREE_CODE (ttr) == FUNCTION_TYPE
5990 && TREE_CODE (ttl) == FUNCTION_TYPE)
5991 {
5992 /* Because const and volatile on functions are
5993 restrictions that say the function will not do
5994 certain things, it is okay to use a const or volatile
5995 function where an ordinary one is wanted, but not
5996 vice-versa. */
36c5e70a
BE
5997 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5998 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
5999 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6000 OPT_Wdiscarded_qualifiers,
6001 G_("passing argument %d of %qE "
6002 "makes %q#v qualified function "
6003 "pointer from unqualified"),
6004 G_("assignment makes %q#v qualified "
6005 "function pointer from "
6006 "unqualified"),
6007 G_("initialization makes %q#v qualified "
6008 "function pointer from "
6009 "unqualified"),
6010 G_("return makes %q#v qualified function "
6011 "pointer from unqualified"),
6012 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6013 }
36c5e70a
BE
6014 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6015 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6016 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6017 OPT_Wdiscarded_qualifiers,
6018 G_("passing argument %d of %qE discards "
6019 "%qv qualifier from pointer target type"),
6020 G_("assignment discards %qv qualifier "
6021 "from pointer target type"),
6022 G_("initialization discards %qv qualifier "
6023 "from pointer target type"),
6024 G_("return discards %qv qualifier from "
6025 "pointer target type"),
6026 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6027
6028 memb = marginal_memb;
3e4093b6 6029 }
400fbf9f 6030
fcf73884 6031 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6032 pedwarn (location, OPT_Wpedantic,
fcf73884 6033 "ISO C prohibits argument conversion to union type");
0e7c47fa 6034
db3927fb 6035 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6036 return build_constructor_single (type, memb, rhs);
3e4093b6 6037 }
0e7c47fa
RK
6038 }
6039
3e4093b6
RS
6040 /* Conversions among pointers */
6041 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6042 && (coder == codel))
400fbf9f 6043 {
3e4093b6
RS
6044 tree ttl = TREE_TYPE (type);
6045 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6046 tree mvl = ttl;
6047 tree mvr = ttr;
3e4093b6 6048 bool is_opaque_pointer;
264fa2db 6049 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6050 addr_space_t asl;
6051 addr_space_t asr;
400fbf9f 6052
46df2823 6053 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6054 mvl = (TYPE_ATOMIC (mvl)
6055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6056 TYPE_QUAL_ATOMIC)
6057 : TYPE_MAIN_VARIANT (mvl));
46df2823 6058 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6059 mvr = (TYPE_ATOMIC (mvr)
6060 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6061 TYPE_QUAL_ATOMIC)
6062 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6063 /* Opaque pointers are treated like void pointers. */
f83c7f63 6064 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6065
478a1c5b
ILT
6066 /* The Plan 9 compiler permits a pointer to a struct to be
6067 automatically converted into a pointer to an anonymous field
6068 within the struct. */
6069 if (flag_plan9_extensions
6070 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6071 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6072 && mvl != mvr)
6073 {
6074 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6075 if (new_rhs != NULL_TREE)
6076 {
6077 rhs = new_rhs;
6078 rhstype = TREE_TYPE (rhs);
6079 coder = TREE_CODE (rhstype);
6080 ttr = TREE_TYPE (rhstype);
6081 mvr = TYPE_MAIN_VARIANT (ttr);
6082 }
6083 }
6084
b7e20b53 6085 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6086 for the purpose of reducing the number of false positives, we
6087 tolerate the special case of
b7e20b53 6088
c22cacf3 6089 int *p = NULL;
b7e20b53 6090
c22cacf3 6091 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6092 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6093 warning_at (errtype == ic_argpass ? expr_loc : location,
6094 OPT_Wc___compat,
6095 "request for implicit conversion "
c2255bc4 6096 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6097
36c5e70a
BE
6098 /* See if the pointers point to incompatible address spaces. */
6099 asl = TYPE_ADDR_SPACE (ttl);
6100 asr = TYPE_ADDR_SPACE (ttr);
6101 if (!null_pointer_constant_p (rhs)
6102 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6103 {
6104 switch (errtype)
6105 {
6106 case ic_argpass:
8ffcdea8 6107 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6108 "non-enclosed address space", parmnum, rname);
6109 break;
6110 case ic_assign:
6111 error_at (location, "assignment from pointer to "
6112 "non-enclosed address space");
6113 break;
6114 case ic_init:
6115 error_at (location, "initialization from pointer to "
6116 "non-enclosed address space");
6117 break;
6118 case ic_return:
6119 error_at (location, "return from pointer to "
6120 "non-enclosed address space");
6121 break;
6122 default:
6123 gcc_unreachable ();
6124 }
6125 return error_mark_node;
6126 }
6127
7876a414
KG
6128 /* Check if the right-hand side has a format attribute but the
6129 left-hand side doesn't. */
90137d8f 6130 if (warn_suggest_attribute_format
104f8784 6131 && check_missing_format_attribute (type, rhstype))
c22cacf3 6132 {
104f8784
KG
6133 switch (errtype)
6134 {
6135 case ic_argpass:
8ffcdea8 6136 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6137 "argument %d of %qE might be "
6138 "a candidate for a format attribute",
6139 parmnum, rname);
104f8784
KG
6140 break;
6141 case ic_assign:
90137d8f 6142 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6143 "assignment left-hand side might be "
6144 "a candidate for a format attribute");
104f8784
KG
6145 break;
6146 case ic_init:
90137d8f 6147 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6148 "initialization left-hand side might be "
6149 "a candidate for a format attribute");
104f8784
KG
6150 break;
6151 case ic_return:
90137d8f 6152 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6153 "return type might be "
6154 "a candidate for a format attribute");
104f8784
KG
6155 break;
6156 default:
6157 gcc_unreachable ();
6158 }
7876a414 6159 }
c22cacf3 6160
3e4093b6
RS
6161 /* Any non-function converts to a [const][volatile] void *
6162 and vice versa; otherwise, targets must be the same.
6163 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6164 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6165 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6166 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6167 || is_opaque_pointer
f8a93a2e
JJ
6168 || ((c_common_unsigned_type (mvl)
6169 == c_common_unsigned_type (mvr))
267bac10
JM
6170 && (c_common_signed_type (mvl)
6171 == c_common_signed_type (mvr))
6172 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6173 {
768952be
MU
6174 /* Warn about loss of qualifers from pointers to arrays with
6175 qualifiers on the element type. */
6176 if (TREE_CODE (ttr) == ARRAY_TYPE)
6177 {
6178 ttr = strip_array_types (ttr);
6179 ttl = strip_array_types (ttl);
6180
6181 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6182 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6183 WARNING_FOR_QUALIFIERS (location, expr_loc,
6184 OPT_Wdiscarded_array_qualifiers,
6185 G_("passing argument %d of %qE discards "
6186 "%qv qualifier from pointer target type"),
6187 G_("assignment discards %qv qualifier "
6188 "from pointer target type"),
6189 G_("initialization discards %qv qualifier "
6190 "from pointer target type"),
6191 G_("return discards %qv qualifier from "
6192 "pointer target type"),
6193 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6194 }
6195 else if (pedantic
3e4093b6
RS
6196 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6197 ||
6198 (VOID_TYPE_P (ttr)
928c19bb 6199 && !null_pointer_constant
3e4093b6 6200 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6201 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6202 G_("ISO C forbids passing argument %d of "
6203 "%qE between function pointer "
6204 "and %<void *%>"),
6205 G_("ISO C forbids assignment between "
6206 "function pointer and %<void *%>"),
6207 G_("ISO C forbids initialization between "
6208 "function pointer and %<void *%>"),
6209 G_("ISO C forbids return between function "
6210 "pointer and %<void *%>"));
3e4093b6
RS
6211 /* Const and volatile mean something different for function types,
6212 so the usual warnings are not appropriate. */
6213 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6214 && TREE_CODE (ttl) != FUNCTION_TYPE)
6215 {
768952be
MU
6216 /* Don't warn about loss of qualifier for conversions from
6217 qualified void* to pointers to arrays with corresponding
6218 qualifier on the element type. */
6219 if (!pedantic)
6220 ttl = strip_array_types (ttl);
6221
267bac10
JM
6222 /* Assignments between atomic and non-atomic objects are OK. */
6223 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6224 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6225 {
768952be
MU
6226 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6227 OPT_Wdiscarded_qualifiers,
6228 G_("passing argument %d of %qE discards "
6229 "%qv qualifier from pointer target type"),
6230 G_("assignment discards %qv qualifier "
6231 "from pointer target type"),
6232 G_("initialization discards %qv qualifier "
6233 "from pointer target type"),
6234 G_("return discards %qv qualifier from "
6235 "pointer target type"),
6236 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6237 }
3e4093b6
RS
6238 /* If this is not a case of ignoring a mismatch in signedness,
6239 no warning. */
6240 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6241 || target_cmp)
3e4093b6
RS
6242 ;
6243 /* If there is a mismatch, do warn. */
f2fd3821 6244 else if (warn_pointer_sign)
768952be
MU
6245 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6246 G_("pointer targets in passing argument "
6247 "%d of %qE differ in signedness"),
6248 G_("pointer targets in assignment "
6249 "differ in signedness"),
6250 G_("pointer targets in initialization "
6251 "differ in signedness"),
6252 G_("pointer targets in return differ "
6253 "in signedness"));
3e4093b6
RS
6254 }
6255 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6256 && TREE_CODE (ttr) == FUNCTION_TYPE)
6257 {
6258 /* Because const and volatile on functions are restrictions
6259 that say the function will not do certain things,
6260 it is okay to use a const or volatile function
6261 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6262 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6263 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6264 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6265 OPT_Wdiscarded_qualifiers,
6266 G_("passing argument %d of %qE makes "
6267 "%q#v qualified function pointer "
6268 "from unqualified"),
6269 G_("assignment makes %q#v qualified function "
6270 "pointer from unqualified"),
6271 G_("initialization makes %q#v qualified "
6272 "function pointer from unqualified"),
6273 G_("return makes %q#v qualified function "
6274 "pointer from unqualified"),
6275 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6276 }
6277 }
6278 else
58393038
ZL
6279 /* Avoid warning about the volatile ObjC EH puts on decls. */
6280 if (!objc_ok)
768952be
MU
6281 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6282 OPT_Wincompatible_pointer_types,
6283 G_("passing argument %d of %qE from "
6284 "incompatible pointer type"),
6285 G_("assignment from incompatible pointer type"),
6286 G_("initialization from incompatible "
6287 "pointer type"),
6288 G_("return from incompatible pointer type"));
58393038 6289
3e4093b6
RS
6290 return convert (type, rhs);
6291 }
b494fd98
EB
6292 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6293 {
6dcc04b0
JM
6294 /* ??? This should not be an error when inlining calls to
6295 unprototyped functions. */
c2255bc4 6296 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6297 return error_mark_node;
6298 }
3e4093b6 6299 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6300 {
3e4093b6
RS
6301 /* An explicit constant 0 can convert to a pointer,
6302 or one that results from arithmetic, even including
6303 a cast to integer type. */
928c19bb 6304 if (!null_pointer_constant)
768952be
MU
6305 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6306 OPT_Wint_conversion,
6307 G_("passing argument %d of %qE makes "
6308 "pointer from integer without a cast"),
6309 G_("assignment makes pointer from integer "
6310 "without a cast"),
6311 G_("initialization makes pointer from "
6312 "integer without a cast"),
6313 G_("return makes pointer from integer "
6314 "without a cast"));
b3006337
EB
6315
6316 return convert (type, rhs);
400fbf9f 6317 }
3e4093b6 6318 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6319 {
768952be
MU
6320 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6321 OPT_Wint_conversion,
6322 G_("passing argument %d of %qE makes integer "
6323 "from pointer without a cast"),
6324 G_("assignment makes integer from pointer "
6325 "without a cast"),
6326 G_("initialization makes integer from pointer "
6327 "without a cast"),
6328 G_("return makes integer from pointer "
6329 "without a cast"));
3e4093b6
RS
6330 return convert (type, rhs);
6331 }
6332 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6333 {
6334 tree ret;
6335 bool save = in_late_binary_op;
6336 in_late_binary_op = true;
6337 ret = convert (type, rhs);
6338 in_late_binary_op = save;
6339 return ret;
6340 }
400fbf9f 6341
2ac2f164 6342 switch (errtype)
3e4093b6 6343 {
2ac2f164 6344 case ic_argpass:
8ffcdea8
MP
6345 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6346 rname);
a7da8b42 6347 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6348 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6349 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6350 break;
6351 case ic_assign:
c2255bc4
AH
6352 error_at (location, "incompatible types when assigning to type %qT from "
6353 "type %qT", type, rhstype);
2ac2f164
JM
6354 break;
6355 case ic_init:
c2255bc4 6356 error_at (location,
8ffcdea8 6357 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6358 type, rhstype);
2ac2f164
JM
6359 break;
6360 case ic_return:
c2255bc4 6361 error_at (location,
8ffcdea8 6362 "incompatible types when returning type %qT but %qT was "
c2255bc4 6363 "expected", rhstype, type);
2ac2f164
JM
6364 break;
6365 default:
6366 gcc_unreachable ();
400fbf9f 6367 }
53b01f59 6368
3e4093b6
RS
6369 return error_mark_node;
6370}
3e4093b6
RS
6371\f
6372/* If VALUE is a compound expr all of whose expressions are constant, then
6373 return its value. Otherwise, return error_mark_node.
15b732b2 6374
3e4093b6
RS
6375 This is for handling COMPOUND_EXPRs as initializer elements
6376 which is allowed with a warning when -pedantic is specified. */
15b732b2 6377
3e4093b6
RS
6378static tree
6379valid_compound_expr_initializer (tree value, tree endtype)
6380{
6381 if (TREE_CODE (value) == COMPOUND_EXPR)
6382 {
6383 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6384 == error_mark_node)
6385 return error_mark_node;
6386 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6387 endtype);
6388 }
116df786 6389 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6390 return error_mark_node;
6391 else
6392 return value;
15b732b2 6393}
400fbf9f 6394\f
3e4093b6
RS
6395/* Perform appropriate conversions on the initial value of a variable,
6396 store it in the declaration DECL,
6397 and print any error messages that are appropriate.
bbbbb16a 6398 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6399 If the init is invalid, store an ERROR_MARK.
6400
6401 INIT_LOC is the location of the initial value. */
400fbf9f 6402
3e4093b6 6403void
c2255bc4 6404store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6405{
3e4093b6 6406 tree value, type;
928c19bb 6407 bool npc = false;
400fbf9f 6408
3e4093b6 6409 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6410
3e4093b6
RS
6411 type = TREE_TYPE (decl);
6412 if (TREE_CODE (type) == ERROR_MARK)
6413 return;
400fbf9f 6414
3e4093b6 6415 /* Digest the specified initializer into an expression. */
400fbf9f 6416
928c19bb
JM
6417 if (init)
6418 npc = null_pointer_constant_p (init);
c2255bc4
AH
6419 value = digest_init (init_loc, type, init, origtype, npc,
6420 true, TREE_STATIC (decl));
400fbf9f 6421
3e4093b6 6422 /* Store the expression if valid; else report error. */
400fbf9f 6423
8400e75e 6424 if (!in_system_header_at (input_location)
3f75a254 6425 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6426 warning (OPT_Wtraditional, "traditional C rejects automatic "
6427 "aggregate initialization");
2f6e4e97 6428
dea63e49
JJ
6429 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6430 DECL_INITIAL (decl) = value;
400fbf9f 6431
3e4093b6
RS
6432 /* ANSI wants warnings about out-of-range constant initializers. */
6433 STRIP_TYPE_NOPS (value);
b8698a0f 6434 if (TREE_STATIC (decl))
c2658540 6435 constant_expression_warning (value);
400fbf9f 6436
3e4093b6
RS
6437 /* Check if we need to set array size from compound literal size. */
6438 if (TREE_CODE (type) == ARRAY_TYPE
6439 && TYPE_DOMAIN (type) == 0
6440 && value != error_mark_node)
400fbf9f 6441 {
3e4093b6
RS
6442 tree inside_init = init;
6443
ed248cf7 6444 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6445 inside_init = fold (inside_init);
6446
6447 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6448 {
8d9f82d5 6449 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6450
8d9f82d5 6451 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6452 {
6453 /* For int foo[] = (int [3]){1}; we need to set array size
6454 now since later on array initializer will be just the
6455 brace enclosed list of the compound literal. */
e30ecc5d 6456 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6457 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6458 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6459 layout_type (type);
8d9f82d5 6460 layout_decl (cldecl, 0);
e30ecc5d
JJ
6461 TREE_TYPE (decl)
6462 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6463 }
6464 }
400fbf9f 6465 }
3e4093b6
RS
6466}
6467\f
6468/* Methods for storing and printing names for error messages. */
400fbf9f 6469
3e4093b6
RS
6470/* Implement a spelling stack that allows components of a name to be pushed
6471 and popped. Each element on the stack is this structure. */
400fbf9f 6472
3e4093b6
RS
6473struct spelling
6474{
6475 int kind;
6476 union
400fbf9f 6477 {
a0f0ab9f 6478 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6479 const char *s;
6480 } u;
6481};
2f6e4e97 6482
3e4093b6
RS
6483#define SPELLING_STRING 1
6484#define SPELLING_MEMBER 2
6485#define SPELLING_BOUNDS 3
400fbf9f 6486
3e4093b6
RS
6487static struct spelling *spelling; /* Next stack element (unused). */
6488static struct spelling *spelling_base; /* Spelling stack base. */
6489static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6490
3e4093b6
RS
6491/* Macros to save and restore the spelling stack around push_... functions.
6492 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6493
3e4093b6
RS
6494#define SPELLING_DEPTH() (spelling - spelling_base)
6495#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6496
3e4093b6
RS
6497/* Push an element on the spelling stack with type KIND and assign VALUE
6498 to MEMBER. */
400fbf9f 6499
3e4093b6
RS
6500#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6501{ \
6502 int depth = SPELLING_DEPTH (); \
6503 \
6504 if (depth >= spelling_size) \
6505 { \
6506 spelling_size += 10; \
cca8ead2
BI
6507 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6508 spelling_size); \
3e4093b6
RS
6509 RESTORE_SPELLING_DEPTH (depth); \
6510 } \
6511 \
6512 spelling->kind = (KIND); \
6513 spelling->MEMBER = (VALUE); \
6514 spelling++; \
6515}
400fbf9f 6516
3e4093b6 6517/* Push STRING on the stack. Printed literally. */
400fbf9f 6518
3e4093b6
RS
6519static void
6520push_string (const char *string)
6521{
6522 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6523}
400fbf9f 6524
3e4093b6 6525/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6526
3e4093b6
RS
6527static void
6528push_member_name (tree decl)
6529{
6530 const char *const string
88388a52
JM
6531 = (DECL_NAME (decl)
6532 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6533 : _("<anonymous>"));
3e4093b6
RS
6534 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6535}
400fbf9f 6536
3e4093b6 6537/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6538
3e4093b6 6539static void
a0f0ab9f 6540push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6541{
6542 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6543}
bb58bec5 6544
3e4093b6 6545/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6546
3e4093b6
RS
6547static int
6548spelling_length (void)
6549{
6550 int size = 0;
6551 struct spelling *p;
400fbf9f 6552
3e4093b6
RS
6553 for (p = spelling_base; p < spelling; p++)
6554 {
6555 if (p->kind == SPELLING_BOUNDS)
6556 size += 25;
6557 else
6558 size += strlen (p->u.s) + 1;
6559 }
6560
6561 return size;
400fbf9f 6562}
400fbf9f 6563
3e4093b6 6564/* Print the spelling to BUFFER and return it. */
400fbf9f 6565
3e4093b6
RS
6566static char *
6567print_spelling (char *buffer)
400fbf9f 6568{
3e4093b6
RS
6569 char *d = buffer;
6570 struct spelling *p;
400fbf9f 6571
3e4093b6
RS
6572 for (p = spelling_base; p < spelling; p++)
6573 if (p->kind == SPELLING_BOUNDS)
6574 {
a0f0ab9f 6575 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6576 d += strlen (d);
6577 }
6578 else
6579 {
6580 const char *s;
6581 if (p->kind == SPELLING_MEMBER)
6582 *d++ = '.';
6583 for (s = p->u.s; (*d = *s++); d++)
6584 ;
6585 }
6586 *d++ = '\0';
6587 return buffer;
6588}
400fbf9f 6589
3e4093b6
RS
6590/* Digest the parser output INIT as an initializer for type TYPE.
6591 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6592
bbbbb16a
ILT
6593 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6594
928c19bb
JM
6595 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6596
916c5919
JM
6597 If INIT is a string constant, STRICT_STRING is true if it is
6598 unparenthesized or we should not warn here for it being parenthesized.
6599 For other types of INIT, STRICT_STRING is not used.
6600
c2255bc4
AH
6601 INIT_LOC is the location of the INIT.
6602
3e4093b6
RS
6603 REQUIRE_CONSTANT requests an error if non-constant initializers or
6604 elements are seen. */
7e842ef8 6605
3e4093b6 6606static tree
c2255bc4
AH
6607digest_init (location_t init_loc, tree type, tree init, tree origtype,
6608 bool null_pointer_constant, bool strict_string,
6609 int require_constant)
3e4093b6
RS
6610{
6611 enum tree_code code = TREE_CODE (type);
6612 tree inside_init = init;
8ce94e44 6613 tree semantic_type = NULL_TREE;
928c19bb 6614 bool maybe_const = true;
7e842ef8 6615
3e4093b6 6616 if (type == error_mark_node
f01da1a5 6617 || !init
7a0ca710 6618 || error_operand_p (init))
3e4093b6 6619 return error_mark_node;
7e842ef8 6620
ed248cf7 6621 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6622
8ce94e44
JM
6623 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6624 {
6625 semantic_type = TREE_TYPE (inside_init);
6626 inside_init = TREE_OPERAND (inside_init, 0);
6627 }
928c19bb
JM
6628 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6629 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6630
3e4093b6
RS
6631 /* Initialization of an array of chars from a string constant
6632 optionally enclosed in braces. */
7e842ef8 6633
197463ae
JM
6634 if (code == ARRAY_TYPE && inside_init
6635 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6636 {
267bac10
JM
6637 tree typ1
6638 = (TYPE_ATOMIC (TREE_TYPE (type))
6639 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6640 TYPE_QUAL_ATOMIC)
6641 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6642 /* Note that an array could be both an array of character type
6643 and an array of wchar_t if wchar_t is signed char or unsigned
6644 char. */
6645 bool char_array = (typ1 == char_type_node
6646 || typ1 == signed_char_type_node
6647 || typ1 == unsigned_char_type_node);
6648 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6649 bool char16_array = !!comptypes (typ1, char16_type_node);
6650 bool char32_array = !!comptypes (typ1, char32_type_node);
6651
6652 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6653 {
916c5919 6654 struct c_expr expr;
c466b2cd 6655 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6656 expr.value = inside_init;
6657 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6658 expr.original_type = NULL;
d033409e 6659 maybe_warn_string_init (init_loc, type, expr);
916c5919 6660
a45e580b 6661 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6662 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6663 "initialization of a flexible array member");
6664
3e4093b6 6665 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6666 TYPE_MAIN_VARIANT (type)))
3e4093b6 6667 return inside_init;
7e842ef8 6668
c466b2cd 6669 if (char_array)
3e4093b6 6670 {
c466b2cd
KVH
6671 if (typ2 != char_type_node)
6672 {
ea58ef42
MP
6673 error_init (init_loc, "char-array initialized from wide "
6674 "string");
c466b2cd
KVH
6675 return error_mark_node;
6676 }
3e4093b6 6677 }
c466b2cd 6678 else
3e4093b6 6679 {
c466b2cd
KVH
6680 if (typ2 == char_type_node)
6681 {
ea58ef42
MP
6682 error_init (init_loc, "wide character array initialized "
6683 "from non-wide string");
c466b2cd
KVH
6684 return error_mark_node;
6685 }
6686 else if (!comptypes(typ1, typ2))
6687 {
ea58ef42
MP
6688 error_init (init_loc, "wide character array initialized "
6689 "from incompatible wide string");
c466b2cd
KVH
6690 return error_mark_node;
6691 }
7e842ef8 6692 }
2f6e4e97 6693
3e4093b6
RS
6694 TREE_TYPE (inside_init) = type;
6695 if (TYPE_DOMAIN (type) != 0
6696 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6697 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6698 {
6699 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6700
c466b2cd 6701 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6702 because it's ok to ignore the terminating null char
6703 that is counted in the length of the constant. */
5eb4df45
ILT
6704 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6705 (len
6706 - (TYPE_PRECISION (typ1)
6707 / BITS_PER_UNIT))))
6708 pedwarn_init (init_loc, 0,
6709 ("initializer-string for array of chars "
6710 "is too long"));
6711 else if (warn_cxx_compat
6712 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6713 warning_at (init_loc, OPT_Wc___compat,
6714 ("initializer-string for array chars "
6715 "is too long for C++"));
6716 }
7e842ef8 6717
3e4093b6 6718 return inside_init;
7e842ef8 6719 }
197463ae
JM
6720 else if (INTEGRAL_TYPE_P (typ1))
6721 {
ea58ef42 6722 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6723 "from string constant");
6724 return error_mark_node;
6725 }
7e842ef8
PE
6726 }
6727
3e4093b6
RS
6728 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6729 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6730 below and handle as a constructor. */
e89be13b
JJ
6731 if (code == VECTOR_TYPE
6732 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6733 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6734 && TREE_CONSTANT (inside_init))
6735 {
6736 if (TREE_CODE (inside_init) == VECTOR_CST
6737 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6738 TYPE_MAIN_VARIANT (type)))
6739 return inside_init;
6740
6741 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6742 {
4038c495
GB
6743 unsigned HOST_WIDE_INT ix;
6744 tree value;
6745 bool constant_p = true;
e89be13b
JJ
6746
6747 /* Iterate through elements and check if all constructor
6748 elements are *_CSTs. */
4038c495
GB
6749 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6750 if (!CONSTANT_CLASS_P (value))
6751 {
6752 constant_p = false;
6753 break;
6754 }
e89be13b 6755
4038c495
GB
6756 if (constant_p)
6757 return build_vector_from_ctor (type,
6758 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6759 }
6760 }
6035d635 6761
ca085fd7
MLI
6762 if (warn_sequence_point)
6763 verify_sequence_points (inside_init);
6764
3e4093b6
RS
6765 /* Any type can be initialized
6766 from an expression of the same type, optionally with braces. */
400fbf9f 6767
3e4093b6
RS
6768 if (inside_init && TREE_TYPE (inside_init) != 0
6769 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6770 TYPE_MAIN_VARIANT (type))
3e4093b6 6771 || (code == ARRAY_TYPE
132da1a5 6772 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6773 || (code == VECTOR_TYPE
132da1a5 6774 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6775 || (code == POINTER_TYPE
3897f229 6776 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6777 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6778 TREE_TYPE (type)))))
3e4093b6
RS
6779 {
6780 if (code == POINTER_TYPE)
b494fd98 6781 {
b494fd98
EB
6782 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6783 {
f2a71bbc
JM
6784 if (TREE_CODE (inside_init) == STRING_CST
6785 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6786 inside_init = array_to_pointer_conversion
6787 (init_loc, inside_init);
f2a71bbc
JM
6788 else
6789 {
ea58ef42 6790 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6791 return error_mark_node;
6792 }
b494fd98 6793 }
f2a71bbc 6794 }
b494fd98 6795
bae39a73
NS
6796 if (code == VECTOR_TYPE)
6797 /* Although the types are compatible, we may require a
6798 conversion. */
6799 inside_init = convert (type, inside_init);
3e4093b6 6800
ca58211b 6801 if (require_constant
3e4093b6 6802 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6803 {
3e4093b6
RS
6804 /* As an extension, allow initializing objects with static storage
6805 duration with compound literals (which are then treated just as
ca58211b
PB
6806 the brace enclosed list they contain). Also allow this for
6807 vectors, as we can only assign them with compound literals. */
7278465e
MP
6808 if (flag_isoc99 && code != VECTOR_TYPE)
6809 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6810 "is not constant");
3e4093b6
RS
6811 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6812 inside_init = DECL_INITIAL (decl);
400fbf9f 6813 }
3e4093b6
RS
6814
6815 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6816 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6817 {
ea58ef42
MP
6818 error_init (init_loc, "array initialized from non-constant array "
6819 "expression");
3e4093b6 6820 return error_mark_node;
400fbf9f 6821 }
400fbf9f 6822
c1771a20 6823 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6824 -pedantic-errors is specified. In the later case, we always want
6825 an error. In the former case, we simply want a warning. */
6826 if (require_constant && pedantic
6827 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6828 {
6829 inside_init
6830 = valid_compound_expr_initializer (inside_init,
6831 TREE_TYPE (inside_init));
6832 if (inside_init == error_mark_node)
ea58ef42 6833 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6834 else
c1771a20 6835 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6836 "initializer element is not constant");
3e4093b6
RS
6837 if (flag_pedantic_errors)
6838 inside_init = error_mark_node;
6839 }
6840 else if (require_constant
116df786
RH
6841 && !initializer_constant_valid_p (inside_init,
6842 TREE_TYPE (inside_init)))
3e4093b6 6843 {
ea58ef42 6844 error_init (init_loc, "initializer element is not constant");
3e4093b6 6845 inside_init = error_mark_node;
8b40563c 6846 }
928c19bb 6847 else if (require_constant && !maybe_const)
3aa3c9fc 6848 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 6849 "initializer element is not a constant expression");
f735a153 6850
90137d8f 6851 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6852 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6853 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6854 type, inside_init, origtype,
bbbbb16a 6855 ic_init, null_pointer_constant,
928c19bb 6856 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6857 return inside_init;
6858 }
f735a153 6859
3e4093b6 6860 /* Handle scalar types, including conversions. */
400fbf9f 6861
ab22c1fa
CF
6862 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6863 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6864 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6865 {
f2a71bbc
JM
6866 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6867 && (TREE_CODE (init) == STRING_CST
6868 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6869 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6870 if (semantic_type)
6871 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6872 inside_init);
3e4093b6 6873 inside_init
68fca595
MP
6874 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6875 inside_init, origtype, ic_init,
6876 null_pointer_constant, NULL_TREE, NULL_TREE,
6877 0);
2f6e4e97 6878
3274deff
JW
6879 /* Check to see if we have already given an error message. */
6880 if (inside_init == error_mark_node)
6881 ;
3f75a254 6882 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6883 {
ea58ef42 6884 error_init (init_loc, "initializer element is not constant");
3e4093b6 6885 inside_init = error_mark_node;
400fbf9f 6886 }
3e4093b6 6887 else if (require_constant
116df786
RH
6888 && !initializer_constant_valid_p (inside_init,
6889 TREE_TYPE (inside_init)))
400fbf9f 6890 {
ea58ef42
MP
6891 error_init (init_loc, "initializer element is not computable at "
6892 "load time");
3e4093b6 6893 inside_init = error_mark_node;
400fbf9f 6894 }
928c19bb 6895 else if (require_constant && !maybe_const)
c2255bc4 6896 pedwarn_init (init_loc, 0,
928c19bb 6897 "initializer element is not a constant expression");
3e4093b6
RS
6898
6899 return inside_init;
400fbf9f 6900 }
d9fc6069 6901
3e4093b6 6902 /* Come here only for records and arrays. */
d9fc6069 6903
3e4093b6 6904 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6905 {
ea58ef42 6906 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6907 return error_mark_node;
d9fc6069 6908 }
3e4093b6 6909
ea58ef42 6910 error_init (init_loc, "invalid initializer");
3e4093b6 6911 return error_mark_node;
d9fc6069 6912}
400fbf9f 6913\f
3e4093b6 6914/* Handle initializers that use braces. */
400fbf9f 6915
3e4093b6
RS
6916/* Type of object we are accumulating a constructor for.
6917 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6918static tree constructor_type;
400fbf9f 6919
3e4093b6
RS
6920/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6921 left to fill. */
6922static tree constructor_fields;
400fbf9f 6923
3e4093b6
RS
6924/* For an ARRAY_TYPE, this is the specified index
6925 at which to store the next element we get. */
6926static tree constructor_index;
400fbf9f 6927
3e4093b6
RS
6928/* For an ARRAY_TYPE, this is the maximum index. */
6929static tree constructor_max_index;
400fbf9f 6930
3e4093b6
RS
6931/* For a RECORD_TYPE, this is the first field not yet written out. */
6932static tree constructor_unfilled_fields;
400fbf9f 6933
3e4093b6
RS
6934/* For an ARRAY_TYPE, this is the index of the first element
6935 not yet written out. */
6936static tree constructor_unfilled_index;
895ea614 6937
3e4093b6
RS
6938/* In a RECORD_TYPE, the byte index of the next consecutive field.
6939 This is so we can generate gaps between fields, when appropriate. */
6940static tree constructor_bit_index;
10d5caec 6941
3e4093b6
RS
6942/* If we are saving up the elements rather than allocating them,
6943 this is the list of elements so far (in reverse order,
6944 most recent first). */
9771b263 6945static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6946
3e4093b6
RS
6947/* 1 if constructor should be incrementally stored into a constructor chain,
6948 0 if all the elements should be kept in AVL tree. */
6949static int constructor_incremental;
ad47f1e5 6950
3e4093b6
RS
6951/* 1 if so far this constructor's elements are all compile-time constants. */
6952static int constructor_constant;
ad47f1e5 6953
3e4093b6
RS
6954/* 1 if so far this constructor's elements are all valid address constants. */
6955static int constructor_simple;
ad47f1e5 6956
928c19bb
JM
6957/* 1 if this constructor has an element that cannot be part of a
6958 constant expression. */
6959static int constructor_nonconst;
6960
3e4093b6
RS
6961/* 1 if this constructor is erroneous so far. */
6962static int constructor_erroneous;
d45cf215 6963
9bac5cbb
G
6964/* 1 if this constructor is the universal zero initializer { 0 }. */
6965static int constructor_zeroinit;
6966
3e4093b6
RS
6967/* Structure for managing pending initializer elements, organized as an
6968 AVL tree. */
d45cf215 6969
3e4093b6 6970struct init_node
d45cf215 6971{
3e4093b6
RS
6972 struct init_node *left, *right;
6973 struct init_node *parent;
6974 int balance;
6975 tree purpose;
6976 tree value;
bbbbb16a 6977 tree origtype;
d45cf215
RS
6978};
6979
3e4093b6
RS
6980/* Tree of pending elements at this constructor level.
6981 These are elements encountered out of order
6982 which belong at places we haven't reached yet in actually
6983 writing the output.
6984 Will never hold tree nodes across GC runs. */
6985static struct init_node *constructor_pending_elts;
d45cf215 6986
3e4093b6
RS
6987/* The SPELLING_DEPTH of this constructor. */
6988static int constructor_depth;
d45cf215 6989
3e4093b6
RS
6990/* DECL node for which an initializer is being read.
6991 0 means we are reading a constructor expression
6992 such as (struct foo) {...}. */
6993static tree constructor_decl;
d45cf215 6994
3e4093b6
RS
6995/* Nonzero if this is an initializer for a top-level decl. */
6996static int constructor_top_level;
d45cf215 6997
3e4093b6
RS
6998/* Nonzero if there were any member designators in this initializer. */
6999static int constructor_designated;
d45cf215 7000
3e4093b6
RS
7001/* Nesting depth of designator list. */
7002static int designator_depth;
d45cf215 7003
3e4093b6 7004/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7005static int designator_erroneous;
d45cf215 7006
3e4093b6
RS
7007\f
7008/* This stack has a level for each implicit or explicit level of
7009 structuring in the initializer, including the outermost one. It
7010 saves the values of most of the variables above. */
d45cf215 7011
3e4093b6
RS
7012struct constructor_range_stack;
7013
7014struct constructor_stack
d45cf215 7015{
3e4093b6
RS
7016 struct constructor_stack *next;
7017 tree type;
7018 tree fields;
7019 tree index;
7020 tree max_index;
7021 tree unfilled_index;
7022 tree unfilled_fields;
7023 tree bit_index;
9771b263 7024 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7025 struct init_node *pending_elts;
7026 int offset;
7027 int depth;
916c5919 7028 /* If value nonzero, this value should replace the entire
3e4093b6 7029 constructor at this level. */
916c5919 7030 struct c_expr replacement_value;
3e4093b6
RS
7031 struct constructor_range_stack *range_stack;
7032 char constant;
7033 char simple;
928c19bb 7034 char nonconst;
3e4093b6
RS
7035 char implicit;
7036 char erroneous;
7037 char outer;
7038 char incremental;
7039 char designated;
976d5a22 7040 int designator_depth;
3e4093b6 7041};
d45cf215 7042
802415d1 7043static struct constructor_stack *constructor_stack;
d45cf215 7044
3e4093b6
RS
7045/* This stack represents designators from some range designator up to
7046 the last designator in the list. */
d45cf215 7047
3e4093b6
RS
7048struct constructor_range_stack
7049{
7050 struct constructor_range_stack *next, *prev;
7051 struct constructor_stack *stack;
7052 tree range_start;
7053 tree index;
7054 tree range_end;
7055 tree fields;
7056};
d45cf215 7057
802415d1 7058static struct constructor_range_stack *constructor_range_stack;
d45cf215 7059
3e4093b6
RS
7060/* This stack records separate initializers that are nested.
7061 Nested initializers can't happen in ANSI C, but GNU C allows them
7062 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7063
3e4093b6 7064struct initializer_stack
d45cf215 7065{
3e4093b6
RS
7066 struct initializer_stack *next;
7067 tree decl;
3e4093b6
RS
7068 struct constructor_stack *constructor_stack;
7069 struct constructor_range_stack *constructor_range_stack;
9771b263 7070 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7071 struct spelling *spelling;
7072 struct spelling *spelling_base;
7073 int spelling_size;
7074 char top_level;
7075 char require_constant_value;
7076 char require_constant_elements;
7077};
d45cf215 7078
802415d1 7079static struct initializer_stack *initializer_stack;
3e4093b6
RS
7080\f
7081/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7082
7083void
a396f8ae 7084start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7085{
3e4093b6 7086 const char *locus;
cceb1885 7087 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7088
3e4093b6 7089 p->decl = constructor_decl;
3e4093b6
RS
7090 p->require_constant_value = require_constant_value;
7091 p->require_constant_elements = require_constant_elements;
7092 p->constructor_stack = constructor_stack;
7093 p->constructor_range_stack = constructor_range_stack;
7094 p->elements = constructor_elements;
7095 p->spelling = spelling;
7096 p->spelling_base = spelling_base;
7097 p->spelling_size = spelling_size;
7098 p->top_level = constructor_top_level;
7099 p->next = initializer_stack;
7100 initializer_stack = p;
400fbf9f 7101
3e4093b6 7102 constructor_decl = decl;
3e4093b6
RS
7103 constructor_designated = 0;
7104 constructor_top_level = top_level;
400fbf9f 7105
6f17bbcf 7106 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7107 {
7108 require_constant_value = TREE_STATIC (decl);
7109 require_constant_elements
7110 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7111 /* For a scalar, you can always use any value to initialize,
7112 even within braces. */
296a8c2f 7113 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7114 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7115 }
7116 else
7117 {
7118 require_constant_value = 0;
7119 require_constant_elements = 0;
88388a52 7120 locus = _("(anonymous)");
3e4093b6 7121 }
b71c7f8a 7122
3e4093b6
RS
7123 constructor_stack = 0;
7124 constructor_range_stack = 0;
b71c7f8a 7125
9bac5cbb 7126 found_missing_braces = 0;
3e4093b6
RS
7127
7128 spelling_base = 0;
7129 spelling_size = 0;
7130 RESTORE_SPELLING_DEPTH (0);
7131
7132 if (locus)
7133 push_string (locus);
7134}
7135
7136void
7137finish_init (void)
b71c7f8a 7138{
3e4093b6 7139 struct initializer_stack *p = initializer_stack;
b71c7f8a 7140
3e4093b6
RS
7141 /* Free the whole constructor stack of this initializer. */
7142 while (constructor_stack)
7143 {
7144 struct constructor_stack *q = constructor_stack;
7145 constructor_stack = q->next;
7146 free (q);
7147 }
7148
366de0ce 7149 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7150
7151 /* Pop back to the data of the outer initializer (if any). */
36579663 7152 free (spelling_base);
3aeb3655 7153
3e4093b6 7154 constructor_decl = p->decl;
3e4093b6
RS
7155 require_constant_value = p->require_constant_value;
7156 require_constant_elements = p->require_constant_elements;
7157 constructor_stack = p->constructor_stack;
7158 constructor_range_stack = p->constructor_range_stack;
7159 constructor_elements = p->elements;
7160 spelling = p->spelling;
7161 spelling_base = p->spelling_base;
7162 spelling_size = p->spelling_size;
7163 constructor_top_level = p->top_level;
7164 initializer_stack = p->next;
7165 free (p);
b71c7f8a 7166}
400fbf9f 7167\f
3e4093b6
RS
7168/* Call here when we see the initializer is surrounded by braces.
7169 This is instead of a call to push_init_level;
7170 it is matched by a call to pop_init_level.
400fbf9f 7171
3e4093b6
RS
7172 TYPE is the type to initialize, for a constructor expression.
7173 For an initializer for a decl, TYPE is zero. */
400fbf9f 7174
3e4093b6
RS
7175void
7176really_start_incremental_init (tree type)
400fbf9f 7177{
5d038c4c 7178 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7179
3e4093b6
RS
7180 if (type == 0)
7181 type = TREE_TYPE (constructor_decl);
400fbf9f 7182
b6fc2cdb
PB
7183 if (TREE_CODE (type) == VECTOR_TYPE
7184 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7185 error ("opaque vector types cannot be initialized");
400fbf9f 7186
3e4093b6
RS
7187 p->type = constructor_type;
7188 p->fields = constructor_fields;
7189 p->index = constructor_index;
7190 p->max_index = constructor_max_index;
7191 p->unfilled_index = constructor_unfilled_index;
7192 p->unfilled_fields = constructor_unfilled_fields;
7193 p->bit_index = constructor_bit_index;
7194 p->elements = constructor_elements;
7195 p->constant = constructor_constant;
7196 p->simple = constructor_simple;
928c19bb 7197 p->nonconst = constructor_nonconst;
3e4093b6
RS
7198 p->erroneous = constructor_erroneous;
7199 p->pending_elts = constructor_pending_elts;
7200 p->depth = constructor_depth;
916c5919
JM
7201 p->replacement_value.value = 0;
7202 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7203 p->replacement_value.original_type = NULL;
3e4093b6
RS
7204 p->implicit = 0;
7205 p->range_stack = 0;
7206 p->outer = 0;
7207 p->incremental = constructor_incremental;
7208 p->designated = constructor_designated;
976d5a22 7209 p->designator_depth = designator_depth;
3e4093b6
RS
7210 p->next = 0;
7211 constructor_stack = p;
b13aca19 7212
3e4093b6
RS
7213 constructor_constant = 1;
7214 constructor_simple = 1;
928c19bb 7215 constructor_nonconst = 0;
3e4093b6 7216 constructor_depth = SPELLING_DEPTH ();
9771b263 7217 constructor_elements = NULL;
3e4093b6
RS
7218 constructor_pending_elts = 0;
7219 constructor_type = type;
7220 constructor_incremental = 1;
7221 constructor_designated = 0;
9bac5cbb 7222 constructor_zeroinit = 1;
3e4093b6 7223 designator_depth = 0;
b06df647 7224 designator_erroneous = 0;
400fbf9f 7225
3e4093b6
RS
7226 if (TREE_CODE (constructor_type) == RECORD_TYPE
7227 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7228 {
3e4093b6
RS
7229 constructor_fields = TYPE_FIELDS (constructor_type);
7230 /* Skip any nameless bit fields at the beginning. */
7231 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7232 && DECL_NAME (constructor_fields) == 0)
910ad8de 7233 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7234
3e4093b6
RS
7235 constructor_unfilled_fields = constructor_fields;
7236 constructor_bit_index = bitsize_zero_node;
400fbf9f 7237 }
3e4093b6
RS
7238 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7239 {
7240 if (TYPE_DOMAIN (constructor_type))
7241 {
7242 constructor_max_index
7243 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7244
3e4093b6
RS
7245 /* Detect non-empty initializations of zero-length arrays. */
7246 if (constructor_max_index == NULL_TREE
7247 && TYPE_SIZE (constructor_type))
9a9d280e 7248 constructor_max_index = integer_minus_one_node;
400fbf9f 7249
3e4093b6
RS
7250 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7251 to initialize VLAs will cause a proper error; avoid tree
7252 checking errors as well by setting a safe value. */
7253 if (constructor_max_index
7254 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7255 constructor_max_index = integer_minus_one_node;
59c83dbf 7256
3e4093b6
RS
7257 constructor_index
7258 = convert (bitsizetype,
7259 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7260 }
3e4093b6 7261 else
493179da
JM
7262 {
7263 constructor_index = bitsize_zero_node;
7264 constructor_max_index = NULL_TREE;
7265 }
59c83dbf 7266
3e4093b6
RS
7267 constructor_unfilled_index = constructor_index;
7268 }
7269 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7270 {
7271 /* Vectors are like simple fixed-size arrays. */
7272 constructor_max_index =
c62c040f 7273 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7274 constructor_index = bitsize_zero_node;
3e4093b6
RS
7275 constructor_unfilled_index = constructor_index;
7276 }
7277 else
7278 {
7279 /* Handle the case of int x = {5}; */
7280 constructor_fields = constructor_type;
7281 constructor_unfilled_fields = constructor_type;
7282 }
7283}
7284\f
7285/* Push down into a subobject, for initialization.
7286 If this is for an explicit set of braces, IMPLICIT is 0.
7287 If it is because the next element belongs at a lower level,
7288 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7289
3e4093b6 7290void
ea58ef42
MP
7291push_init_level (location_t loc, int implicit,
7292 struct obstack *braced_init_obstack)
3e4093b6
RS
7293{
7294 struct constructor_stack *p;
7295 tree value = NULL_TREE;
400fbf9f 7296
3e4093b6 7297 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7298 pop them now. If implicit == 1, this will have been done in
7299 process_init_element; do not repeat it here because in the case
7300 of excess initializers for an empty aggregate this leads to an
7301 infinite cycle of popping a level and immediately recreating
7302 it. */
7303 if (implicit != 1)
3e4093b6 7304 {
472d98b4
JM
7305 while (constructor_stack->implicit)
7306 {
7307 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7308 || TREE_CODE (constructor_type) == UNION_TYPE)
7309 && constructor_fields == 0)
34cf811f 7310 process_init_element (input_location,
ea58ef42 7311 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7312 true, braced_init_obstack);
472d98b4
JM
7313 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7314 && constructor_max_index
7315 && tree_int_cst_lt (constructor_max_index,
7316 constructor_index))
34cf811f 7317 process_init_element (input_location,
ea58ef42 7318 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7319 true, braced_init_obstack);
472d98b4
JM
7320 else
7321 break;
7322 }
3e4093b6 7323 }
400fbf9f 7324
3e4093b6
RS
7325 /* Unless this is an explicit brace, we need to preserve previous
7326 content if any. */
7327 if (implicit)
7328 {
7329 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7330 || TREE_CODE (constructor_type) == UNION_TYPE)
7331 && constructor_fields)
a1e3b3d9 7332 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7333 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7334 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7335 }
7336
5d038c4c 7337 p = XNEW (struct constructor_stack);
3e4093b6
RS
7338 p->type = constructor_type;
7339 p->fields = constructor_fields;
7340 p->index = constructor_index;
7341 p->max_index = constructor_max_index;
7342 p->unfilled_index = constructor_unfilled_index;
7343 p->unfilled_fields = constructor_unfilled_fields;
7344 p->bit_index = constructor_bit_index;
7345 p->elements = constructor_elements;
7346 p->constant = constructor_constant;
7347 p->simple = constructor_simple;
928c19bb 7348 p->nonconst = constructor_nonconst;
3e4093b6
RS
7349 p->erroneous = constructor_erroneous;
7350 p->pending_elts = constructor_pending_elts;
7351 p->depth = constructor_depth;
916c5919
JM
7352 p->replacement_value.value = 0;
7353 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7354 p->replacement_value.original_type = NULL;
3e4093b6
RS
7355 p->implicit = implicit;
7356 p->outer = 0;
7357 p->incremental = constructor_incremental;
7358 p->designated = constructor_designated;
976d5a22 7359 p->designator_depth = designator_depth;
3e4093b6
RS
7360 p->next = constructor_stack;
7361 p->range_stack = 0;
7362 constructor_stack = p;
400fbf9f 7363
3e4093b6
RS
7364 constructor_constant = 1;
7365 constructor_simple = 1;
928c19bb 7366 constructor_nonconst = 0;
3e4093b6 7367 constructor_depth = SPELLING_DEPTH ();
9771b263 7368 constructor_elements = NULL;
3e4093b6
RS
7369 constructor_incremental = 1;
7370 constructor_designated = 0;
7371 constructor_pending_elts = 0;
7372 if (!implicit)
400fbf9f 7373 {
3e4093b6
RS
7374 p->range_stack = constructor_range_stack;
7375 constructor_range_stack = 0;
7376 designator_depth = 0;
b06df647 7377 designator_erroneous = 0;
3e4093b6 7378 }
400fbf9f 7379
3e4093b6
RS
7380 /* Don't die if an entire brace-pair level is superfluous
7381 in the containing level. */
7382 if (constructor_type == 0)
7383 ;
7384 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7385 || TREE_CODE (constructor_type) == UNION_TYPE)
7386 {
7387 /* Don't die if there are extra init elts at the end. */
7388 if (constructor_fields == 0)
7389 constructor_type = 0;
7390 else
400fbf9f 7391 {
3e4093b6
RS
7392 constructor_type = TREE_TYPE (constructor_fields);
7393 push_member_name (constructor_fields);
7394 constructor_depth++;
400fbf9f 7395 }
6a358dcb
MP
7396 /* If upper initializer is designated, then mark this as
7397 designated too to prevent bogus warnings. */
7398 constructor_designated = p->designated;
3e4093b6
RS
7399 }
7400 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7401 {
7402 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7403 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7404 constructor_depth++;
400fbf9f
JW
7405 }
7406
3e4093b6 7407 if (constructor_type == 0)
400fbf9f 7408 {
ea58ef42 7409 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7410 constructor_fields = 0;
7411 constructor_unfilled_fields = 0;
7412 return;
400fbf9f
JW
7413 }
7414
3e4093b6
RS
7415 if (value && TREE_CODE (value) == CONSTRUCTOR)
7416 {
7417 constructor_constant = TREE_CONSTANT (value);
7418 constructor_simple = TREE_STATIC (value);
928c19bb 7419 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7420 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7421 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7422 && (TREE_CODE (constructor_type) == RECORD_TYPE
7423 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7424 set_nonincremental_init (braced_init_obstack);
3e4093b6 7425 }
400fbf9f 7426
9bac5cbb
G
7427 if (implicit == 1)
7428 found_missing_braces = 1;
400fbf9f 7429
3e4093b6
RS
7430 if (TREE_CODE (constructor_type) == RECORD_TYPE
7431 || TREE_CODE (constructor_type) == UNION_TYPE)
7432 {
7433 constructor_fields = TYPE_FIELDS (constructor_type);
7434 /* Skip any nameless bit fields at the beginning. */
7435 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7436 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7437 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7438
3e4093b6
RS
7439 constructor_unfilled_fields = constructor_fields;
7440 constructor_bit_index = bitsize_zero_node;
7441 }
7442 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7443 {
7444 /* Vectors are like simple fixed-size arrays. */
7445 constructor_max_index =
c62c040f
RG
7446 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7447 constructor_index = bitsize_int (0);
3e4093b6
RS
7448 constructor_unfilled_index = constructor_index;
7449 }
7450 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7451 {
7452 if (TYPE_DOMAIN (constructor_type))
7453 {
7454 constructor_max_index
7455 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7456
3e4093b6
RS
7457 /* Detect non-empty initializations of zero-length arrays. */
7458 if (constructor_max_index == NULL_TREE
7459 && TYPE_SIZE (constructor_type))
9a9d280e 7460 constructor_max_index = integer_minus_one_node;
de520661 7461
3e4093b6
RS
7462 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7463 to initialize VLAs will cause a proper error; avoid tree
7464 checking errors as well by setting a safe value. */
7465 if (constructor_max_index
7466 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7467 constructor_max_index = integer_minus_one_node;
b62acd60 7468
3e4093b6
RS
7469 constructor_index
7470 = convert (bitsizetype,
7471 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7472 }
7473 else
7474 constructor_index = bitsize_zero_node;
de520661 7475
3e4093b6
RS
7476 constructor_unfilled_index = constructor_index;
7477 if (value && TREE_CODE (value) == STRING_CST)
7478 {
7479 /* We need to split the char/wchar array into individual
7480 characters, so that we don't have to special case it
7481 everywhere. */
a1e3b3d9 7482 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7483 }
7484 }
7485 else
7486 {
b4519d39 7487 if (constructor_type != error_mark_node)
96b40f8d 7488 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7489 constructor_fields = constructor_type;
7490 constructor_unfilled_fields = constructor_type;
7491 }
7492}
8b6a5902 7493
3e4093b6 7494/* At the end of an implicit or explicit brace level,
916c5919
JM
7495 finish up that level of constructor. If a single expression
7496 with redundant braces initialized that level, return the
7497 c_expr structure for that expression. Otherwise, the original_code
7498 element is set to ERROR_MARK.
7499 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7500 from inner levels (process_init_element ignores that),
916c5919 7501 but return error_mark_node as the value from the outermost level
3e4093b6 7502 (that's what we want to put in DECL_INITIAL).
916c5919 7503 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7504
916c5919 7505struct c_expr
ea58ef42
MP
7506pop_init_level (location_t loc, int implicit,
7507 struct obstack *braced_init_obstack)
3e4093b6
RS
7508{
7509 struct constructor_stack *p;
916c5919
JM
7510 struct c_expr ret;
7511 ret.value = 0;
7512 ret.original_code = ERROR_MARK;
6866c6e8 7513 ret.original_type = NULL;
de520661 7514
3e4093b6
RS
7515 if (implicit == 0)
7516 {
7517 /* When we come to an explicit close brace,
7518 pop any inner levels that didn't have explicit braces. */
7519 while (constructor_stack->implicit)
34cf811f 7520 process_init_element (input_location,
ea58ef42 7521 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7522 true, braced_init_obstack);
366de0ce 7523 gcc_assert (!constructor_range_stack);
3e4093b6 7524 }
e5e809f4 7525
0066ef9c
RH
7526 /* Now output all pending elements. */
7527 constructor_incremental = 1;
a1e3b3d9 7528 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7529
3e4093b6 7530 p = constructor_stack;
e5e809f4 7531
3e4093b6
RS
7532 /* Error for initializing a flexible array member, or a zero-length
7533 array member in an inappropriate context. */
7534 if (constructor_type && constructor_fields
7535 && TREE_CODE (constructor_type) == ARRAY_TYPE
7536 && TYPE_DOMAIN (constructor_type)
3f75a254 7537 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7538 {
7539 /* Silently discard empty initializations. The parser will
7540 already have pedwarned for empty brackets. */
7541 if (integer_zerop (constructor_unfilled_index))
7542 constructor_type = NULL_TREE;
366de0ce 7543 else
3e4093b6 7544 {
366de0ce 7545 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7546
3e4093b6 7547 if (constructor_depth > 2)
ea58ef42 7548 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7549 else
d033409e 7550 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7551 "initialization of a flexible array member");
de520661 7552
3e4093b6
RS
7553 /* We have already issued an error message for the existence
7554 of a flexible array member not at the end of the structure.
535a42b1 7555 Discard the initializer so that we do not die later. */
910ad8de 7556 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7557 constructor_type = NULL_TREE;
7558 }
3e4093b6 7559 }
de520661 7560
7b33f0c8
MP
7561 switch (vec_safe_length (constructor_elements))
7562 {
7563 case 0:
7564 /* Initialization with { } counts as zeroinit. */
7565 constructor_zeroinit = 1;
7566 break;
7567 case 1:
7568 /* This might be zeroinit as well. */
7569 if (integer_zerop ((*constructor_elements)[0].value))
7570 constructor_zeroinit = 1;
7571 break;
7572 default:
7573 /* If the constructor has more than one element, it can't be { 0 }. */
7574 constructor_zeroinit = 0;
7575 break;
7576 }
9bac5cbb
G
7577
7578 /* Warn when some structs are initialized with direct aggregation. */
7579 if (!implicit && found_missing_braces && warn_missing_braces
7580 && !constructor_zeroinit)
7b33f0c8
MP
7581 warning_init (loc, OPT_Wmissing_braces,
7582 "missing braces around initializer");
9bac5cbb 7583
3e4093b6 7584 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7585 if (warn_missing_field_initializers
3e4093b6
RS
7586 && constructor_type
7587 && TREE_CODE (constructor_type) == RECORD_TYPE
7588 && constructor_unfilled_fields)
7589 {
7590 /* Do not warn for flexible array members or zero-length arrays. */
7591 while (constructor_unfilled_fields
3f75a254 7592 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7593 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7594 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7595
49819fef
AM
7596 if (constructor_unfilled_fields
7597 /* Do not warn if this level of the initializer uses member
7598 designators; it is likely to be deliberate. */
7599 && !constructor_designated
84937de2 7600 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7601 && !constructor_zeroinit)
3e4093b6 7602 {
32397f22
MLI
7603 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7604 "missing initializer for field %qD of %qT",
7605 constructor_unfilled_fields,
7606 constructor_type))
7607 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7608 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7609 }
7610 }
de520661 7611
3e4093b6 7612 /* Pad out the end of the structure. */
916c5919 7613 if (p->replacement_value.value)
3e4093b6
RS
7614 /* If this closes a superfluous brace pair,
7615 just pass out the element between them. */
916c5919 7616 ret = p->replacement_value;
3e4093b6
RS
7617 else if (constructor_type == 0)
7618 ;
7619 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7620 && TREE_CODE (constructor_type) != UNION_TYPE
7621 && TREE_CODE (constructor_type) != ARRAY_TYPE
7622 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7623 {
7624 /* A nonincremental scalar initializer--just return
7625 the element, after verifying there is just one. */
9771b263 7626 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7627 {
7628 if (!constructor_erroneous)
ea58ef42 7629 error_init (loc, "empty scalar initializer");
916c5919 7630 ret.value = error_mark_node;
3e4093b6 7631 }
9771b263 7632 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7633 {
ea58ef42 7634 error_init (loc, "extra elements in scalar initializer");
9771b263 7635 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7636 }
7637 else
9771b263 7638 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7639 }
7640 else
7641 {
7642 if (constructor_erroneous)
916c5919 7643 ret.value = error_mark_node;
3e4093b6
RS
7644 else
7645 {
916c5919 7646 ret.value = build_constructor (constructor_type,
4038c495 7647 constructor_elements);
3e4093b6 7648 if (constructor_constant)
51eed280 7649 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7650 if (constructor_constant && constructor_simple)
916c5919 7651 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7652 if (constructor_nonconst)
7653 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7654 }
7655 }
de520661 7656
928c19bb
JM
7657 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7658 {
7659 if (constructor_nonconst)
7660 ret.original_code = C_MAYBE_CONST_EXPR;
7661 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7662 ret.original_code = ERROR_MARK;
7663 }
7664
3e4093b6
RS
7665 constructor_type = p->type;
7666 constructor_fields = p->fields;
7667 constructor_index = p->index;
7668 constructor_max_index = p->max_index;
7669 constructor_unfilled_index = p->unfilled_index;
7670 constructor_unfilled_fields = p->unfilled_fields;
7671 constructor_bit_index = p->bit_index;
7672 constructor_elements = p->elements;
7673 constructor_constant = p->constant;
7674 constructor_simple = p->simple;
928c19bb 7675 constructor_nonconst = p->nonconst;
3e4093b6
RS
7676 constructor_erroneous = p->erroneous;
7677 constructor_incremental = p->incremental;
7678 constructor_designated = p->designated;
976d5a22 7679 designator_depth = p->designator_depth;
3e4093b6
RS
7680 constructor_pending_elts = p->pending_elts;
7681 constructor_depth = p->depth;
7682 if (!p->implicit)
7683 constructor_range_stack = p->range_stack;
7684 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7685
3e4093b6
RS
7686 constructor_stack = p->next;
7687 free (p);
b621a4dd 7688
5d5e98dc
VR
7689 if (ret.value == 0 && constructor_stack == 0)
7690 ret.value = error_mark_node;
916c5919 7691 return ret;
3e4093b6 7692}
8b6a5902 7693
3e4093b6
RS
7694/* Common handling for both array range and field name designators.
7695 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7696
3e4093b6 7697static int
ea58ef42
MP
7698set_designator (location_t loc, int array,
7699 struct obstack *braced_init_obstack)
de520661 7700{
3e4093b6
RS
7701 tree subtype;
7702 enum tree_code subcode;
de520661 7703
3e4093b6
RS
7704 /* Don't die if an entire brace-pair level is superfluous
7705 in the containing level. */
7706 if (constructor_type == 0)
7707 return 1;
de520661 7708
366de0ce
NS
7709 /* If there were errors in this designator list already, bail out
7710 silently. */
b06df647 7711 if (designator_erroneous)
3e4093b6 7712 return 1;
e28cae4f 7713
3e4093b6
RS
7714 if (!designator_depth)
7715 {
366de0ce 7716 gcc_assert (!constructor_range_stack);
de520661 7717
3e4093b6
RS
7718 /* Designator list starts at the level of closest explicit
7719 braces. */
7720 while (constructor_stack->implicit)
34cf811f 7721 process_init_element (input_location,
ea58ef42 7722 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7723 true, braced_init_obstack);
3e4093b6
RS
7724 constructor_designated = 1;
7725 return 0;
7726 }
de520661 7727
366de0ce 7728 switch (TREE_CODE (constructor_type))
3c3fa147 7729 {
366de0ce
NS
7730 case RECORD_TYPE:
7731 case UNION_TYPE:
3e4093b6
RS
7732 subtype = TREE_TYPE (constructor_fields);
7733 if (subtype != error_mark_node)
7734 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7735 break;
7736 case ARRAY_TYPE:
3e4093b6 7737 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7738 break;
7739 default:
7740 gcc_unreachable ();
de520661 7741 }
400fbf9f 7742
3e4093b6
RS
7743 subcode = TREE_CODE (subtype);
7744 if (array && subcode != ARRAY_TYPE)
7745 {
ea58ef42 7746 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7747 return 1;
7748 }
7749 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7750 {
ea58ef42 7751 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7752 return 1;
7753 }
d45cf215 7754
3e4093b6 7755 constructor_designated = 1;
ea58ef42 7756 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7757 return 0;
de520661 7758}
400fbf9f 7759
3e4093b6
RS
7760/* If there are range designators in designator list, push a new designator
7761 to constructor_range_stack. RANGE_END is end of such stack range or
7762 NULL_TREE if there is no range designator at this level. */
400fbf9f 7763
3e4093b6 7764static void
a1e3b3d9 7765push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7766{
7767 struct constructor_range_stack *p;
400fbf9f 7768
a1e3b3d9
LB
7769 p = (struct constructor_range_stack *)
7770 obstack_alloc (braced_init_obstack,
7771 sizeof (struct constructor_range_stack));
3e4093b6
RS
7772 p->prev = constructor_range_stack;
7773 p->next = 0;
7774 p->fields = constructor_fields;
7775 p->range_start = constructor_index;
7776 p->index = constructor_index;
7777 p->stack = constructor_stack;
7778 p->range_end = range_end;
8b6a5902 7779 if (constructor_range_stack)
3e4093b6
RS
7780 constructor_range_stack->next = p;
7781 constructor_range_stack = p;
de520661 7782}
400fbf9f 7783
3e4093b6
RS
7784/* Within an array initializer, specify the next index to be initialized.
7785 FIRST is that index. If LAST is nonzero, then initialize a range
7786 of indices, running from FIRST through LAST. */
5a7ec9d9 7787
de520661 7788void
ea58ef42 7789set_init_index (location_t loc, tree first, tree last,
d033409e 7790 struct obstack *braced_init_obstack)
de520661 7791{
ea58ef42 7792 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7793 return;
de520661 7794
b06df647 7795 designator_erroneous = 1;
de520661 7796
3ea8cd06
JM
7797 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7798 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7799 {
ea58ef42 7800 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7801 return;
7802 }
7803
2b6da65c
JM
7804 if (TREE_CODE (first) != INTEGER_CST)
7805 {
7806 first = c_fully_fold (first, false, NULL);
7807 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7808 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7809 "array index in initializer is not "
7810 "an integer constant expression");
7811 }
7812
7813 if (last && TREE_CODE (last) != INTEGER_CST)
7814 {
7815 last = c_fully_fold (last, false, NULL);
7816 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7817 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7818 "array index in initializer is not "
7819 "an integer constant expression");
7820 }
7821
3e4093b6 7822 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7823 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7824 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7825 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7826 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7827 error_init (loc, "array index in non-array initializer");
622adc7e 7828 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7829 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7830 else if (constructor_max_index
7831 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7832 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7833 else
de520661 7834 {
928c19bb
JM
7835 constant_expression_warning (first);
7836 if (last)
7837 constant_expression_warning (last);
3e4093b6 7838 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7839 if (tree_int_cst_lt (constructor_index, first))
7840 {
7841 constructor_index = copy_node (constructor_index);
7842 TREE_OVERFLOW (constructor_index) = 1;
7843 }
665f2503 7844
3e4093b6 7845 if (last)
2bede729 7846 {
3e4093b6
RS
7847 if (tree_int_cst_equal (first, last))
7848 last = 0;
7849 else if (tree_int_cst_lt (last, first))
7850 {
ea58ef42 7851 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7852 last = 0;
7853 }
7854 else
7855 {
7856 last = convert (bitsizetype, last);
7857 if (constructor_max_index != 0
7858 && tree_int_cst_lt (constructor_max_index, last))
7859 {
ea58ef42
MP
7860 error_init (loc, "array index range in initializer exceeds "
7861 "array bounds");
3e4093b6
RS
7862 last = 0;
7863 }
7864 }
2bede729 7865 }
fed3cef0 7866
3e4093b6 7867 designator_depth++;
b06df647 7868 designator_erroneous = 0;
3e4093b6 7869 if (constructor_range_stack || last)
a1e3b3d9 7870 push_range_stack (last, braced_init_obstack);
de520661 7871 }
de520661 7872}
3e4093b6
RS
7873
7874/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7875
de520661 7876void
ea58ef42
MP
7877set_init_label (location_t loc, tree fieldname,
7878 struct obstack *braced_init_obstack)
de520661 7879{
0fb96aa4 7880 tree field;
94ba5069 7881
ea58ef42 7882 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7883 return;
7884
b06df647 7885 designator_erroneous = 1;
3e4093b6
RS
7886
7887 if (TREE_CODE (constructor_type) != RECORD_TYPE
7888 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7889 {
ea58ef42 7890 error_init (loc, "field name not in record or union initializer");
3e4093b6 7891 return;
94ba5069
RS
7892 }
7893
0fb96aa4 7894 field = lookup_field (constructor_type, fieldname);
8b6a5902 7895
0fb96aa4 7896 if (field == 0)
9babc352 7897 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 7898 else
0fb96aa4
JM
7899 do
7900 {
7901 constructor_fields = TREE_VALUE (field);
7902 designator_depth++;
7903 designator_erroneous = 0;
7904 if (constructor_range_stack)
7905 push_range_stack (NULL_TREE, braced_init_obstack);
7906 field = TREE_CHAIN (field);
7907 if (field)
7908 {
ea58ef42 7909 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7910 return;
7911 }
7912 }
7913 while (field != NULL_TREE);
3e4093b6
RS
7914}
7915\f
7916/* Add a new initializer to the tree of pending initializers. PURPOSE
7917 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7918 VALUE is the value of that index or field. If ORIGTYPE is not
7919 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7920
7921 IMPLICIT is true if value comes from pop_init_level (1),
7922 the new initializer has been merged with the existing one
7923 and thus no warnings should be emitted about overriding an
7924 existing initializer. */
de520661 7925
3e4093b6 7926static void
96b40f8d
MP
7927add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7928 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7929{
7930 struct init_node *p, **q, *r;
7931
7932 q = &constructor_pending_elts;
7933 p = 0;
7934
7935 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7936 {
3e4093b6 7937 while (*q != 0)
91fa3c30 7938 {
3e4093b6
RS
7939 p = *q;
7940 if (tree_int_cst_lt (purpose, p->purpose))
7941 q = &p->left;
7942 else if (tree_int_cst_lt (p->purpose, purpose))
7943 q = &p->right;
7944 else
7945 {
b295aee2
JJ
7946 if (!implicit)
7947 {
7948 if (TREE_SIDE_EFFECTS (p->value))
755e528f 7949 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
7950 "initialized field with side-effects "
7951 "overwritten");
b295aee2 7952 else if (warn_override_init)
96b40f8d
MP
7953 warning_init (loc, OPT_Woverride_init,
7954 "initialized field overwritten");
b295aee2 7955 }
3e4093b6 7956 p->value = value;
bbbbb16a 7957 p->origtype = origtype;
3e4093b6
RS
7958 return;
7959 }
91fa3c30 7960 }
de520661 7961 }
3e4093b6 7962 else
de520661 7963 {
3e4093b6 7964 tree bitpos;
400fbf9f 7965
3e4093b6
RS
7966 bitpos = bit_position (purpose);
7967 while (*q != NULL)
7968 {
7969 p = *q;
7970 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7971 q = &p->left;
7972 else if (p->purpose != purpose)
7973 q = &p->right;
7974 else
7975 {
b295aee2
JJ
7976 if (!implicit)
7977 {
7978 if (TREE_SIDE_EFFECTS (p->value))
755e528f 7979 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
7980 "initialized field with side-effects "
7981 "overwritten");
b295aee2 7982 else if (warn_override_init)
96b40f8d
MP
7983 warning_init (loc, OPT_Woverride_init,
7984 "initialized field overwritten");
b295aee2 7985 }
3e4093b6 7986 p->value = value;
bbbbb16a 7987 p->origtype = origtype;
3e4093b6
RS
7988 return;
7989 }
7990 }
91fa3c30 7991 }
b71c7f8a 7992
a1e3b3d9
LB
7993 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7994 sizeof (struct init_node));
3e4093b6
RS
7995 r->purpose = purpose;
7996 r->value = value;
bbbbb16a 7997 r->origtype = origtype;
8b6a5902 7998
3e4093b6
RS
7999 *q = r;
8000 r->parent = p;
8001 r->left = 0;
8002 r->right = 0;
8003 r->balance = 0;
b71c7f8a 8004
3e4093b6 8005 while (p)
de520661 8006 {
3e4093b6 8007 struct init_node *s;
665f2503 8008
3e4093b6 8009 if (r == p->left)
2bede729 8010 {
3e4093b6
RS
8011 if (p->balance == 0)
8012 p->balance = -1;
8013 else if (p->balance < 0)
8014 {
8015 if (r->balance < 0)
8016 {
8017 /* L rotation. */
8018 p->left = r->right;
8019 if (p->left)
8020 p->left->parent = p;
8021 r->right = p;
e7b6a0ee 8022
3e4093b6
RS
8023 p->balance = 0;
8024 r->balance = 0;
39bc99c2 8025
3e4093b6
RS
8026 s = p->parent;
8027 p->parent = r;
8028 r->parent = s;
8029 if (s)
8030 {
8031 if (s->left == p)
8032 s->left = r;
8033 else
8034 s->right = r;
8035 }
8036 else
8037 constructor_pending_elts = r;
8038 }
8039 else
8040 {
8041 /* LR rotation. */
8042 struct init_node *t = r->right;
e7b6a0ee 8043
3e4093b6
RS
8044 r->right = t->left;
8045 if (r->right)
8046 r->right->parent = r;
8047 t->left = r;
8048
8049 p->left = t->right;
8050 if (p->left)
8051 p->left->parent = p;
8052 t->right = p;
8053
8054 p->balance = t->balance < 0;
8055 r->balance = -(t->balance > 0);
8056 t->balance = 0;
8057
8058 s = p->parent;
8059 p->parent = t;
8060 r->parent = t;
8061 t->parent = s;
8062 if (s)
8063 {
8064 if (s->left == p)
8065 s->left = t;
8066 else
8067 s->right = t;
8068 }
8069 else
8070 constructor_pending_elts = t;
8071 }
8072 break;
8073 }
8074 else
8075 {
8076 /* p->balance == +1; growth of left side balances the node. */
8077 p->balance = 0;
8078 break;
8079 }
2bede729 8080 }
3e4093b6
RS
8081 else /* r == p->right */
8082 {
8083 if (p->balance == 0)
8084 /* Growth propagation from right side. */
8085 p->balance++;
8086 else if (p->balance > 0)
8087 {
8088 if (r->balance > 0)
8089 {
8090 /* R rotation. */
8091 p->right = r->left;
8092 if (p->right)
8093 p->right->parent = p;
8094 r->left = p;
8095
8096 p->balance = 0;
8097 r->balance = 0;
8098
8099 s = p->parent;
8100 p->parent = r;
8101 r->parent = s;
8102 if (s)
8103 {
8104 if (s->left == p)
8105 s->left = r;
8106 else
8107 s->right = r;
8108 }
8109 else
8110 constructor_pending_elts = r;
8111 }
8112 else /* r->balance == -1 */
8113 {
8114 /* RL rotation */
8115 struct init_node *t = r->left;
8116
8117 r->left = t->right;
8118 if (r->left)
8119 r->left->parent = r;
8120 t->right = r;
8121
8122 p->right = t->left;
8123 if (p->right)
8124 p->right->parent = p;
8125 t->left = p;
8126
8127 r->balance = (t->balance < 0);
8128 p->balance = -(t->balance > 0);
8129 t->balance = 0;
8130
8131 s = p->parent;
8132 p->parent = t;
8133 r->parent = t;
8134 t->parent = s;
8135 if (s)
8136 {
8137 if (s->left == p)
8138 s->left = t;
8139 else
8140 s->right = t;
8141 }
8142 else
8143 constructor_pending_elts = t;
8144 }
8145 break;
8146 }
8147 else
8148 {
8149 /* p->balance == -1; growth of right side balances the node. */
8150 p->balance = 0;
8151 break;
8152 }
8153 }
8154
8155 r = p;
8156 p = p->parent;
8157 }
8158}
8159
8160/* Build AVL tree from a sorted chain. */
8161
8162static void
a1e3b3d9 8163set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8164{
4038c495
GB
8165 unsigned HOST_WIDE_INT ix;
8166 tree index, value;
3e4093b6
RS
8167
8168 if (TREE_CODE (constructor_type) != RECORD_TYPE
8169 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8170 return;
8171
4038c495 8172 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8173 add_pending_init (input_location, index, value, NULL_TREE, true,
8174 braced_init_obstack);
9771b263 8175 constructor_elements = NULL;
3e4093b6
RS
8176 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8177 {
8178 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8179 /* Skip any nameless bit fields at the beginning. */
8180 while (constructor_unfilled_fields != 0
8181 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8182 && DECL_NAME (constructor_unfilled_fields) == 0)
8183 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8184
de520661 8185 }
3e4093b6 8186 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8187 {
3e4093b6
RS
8188 if (TYPE_DOMAIN (constructor_type))
8189 constructor_unfilled_index
8190 = convert (bitsizetype,
8191 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8192 else
8193 constructor_unfilled_index = bitsize_zero_node;
de520661 8194 }
3e4093b6 8195 constructor_incremental = 0;
de520661 8196}
400fbf9f 8197
3e4093b6 8198/* Build AVL tree from a string constant. */
de520661 8199
3e4093b6 8200static void
a1e3b3d9
LB
8201set_nonincremental_init_from_string (tree str,
8202 struct obstack * braced_init_obstack)
de520661 8203{
3e4093b6
RS
8204 tree value, purpose, type;
8205 HOST_WIDE_INT val[2];
8206 const char *p, *end;
8207 int byte, wchar_bytes, charwidth, bitpos;
de520661 8208
366de0ce 8209 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8210
c466b2cd 8211 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8212 charwidth = TYPE_PRECISION (char_type_node);
8213 type = TREE_TYPE (constructor_type);
8214 p = TREE_STRING_POINTER (str);
8215 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8216
3e4093b6 8217 for (purpose = bitsize_zero_node;
8824edff
JJ
8218 p < end
8219 && !(constructor_max_index
8220 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8221 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8222 {
3e4093b6 8223 if (wchar_bytes == 1)
ffc5c6a9 8224 {
807e902e
KZ
8225 val[0] = (unsigned char) *p++;
8226 val[1] = 0;
ffc5c6a9
RH
8227 }
8228 else
3e4093b6 8229 {
3e4093b6 8230 val[1] = 0;
807e902e 8231 val[0] = 0;
3e4093b6
RS
8232 for (byte = 0; byte < wchar_bytes; byte++)
8233 {
8234 if (BYTES_BIG_ENDIAN)
8235 bitpos = (wchar_bytes - byte - 1) * charwidth;
8236 else
8237 bitpos = byte * charwidth;
807e902e 8238 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8239 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8240 << (bitpos % HOST_BITS_PER_WIDE_INT);
8241 }
8242 }
584ef5fe 8243
8df83eae 8244 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8245 {
8246 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8247 if (bitpos < HOST_BITS_PER_WIDE_INT)
8248 {
807e902e 8249 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8250 {
807e902e
KZ
8251 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8252 val[1] = -1;
3e4093b6
RS
8253 }
8254 }
8255 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8256 {
807e902e
KZ
8257 if (val[0] < 0)
8258 val[1] = -1;
3e4093b6 8259 }
807e902e 8260 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8261 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8262 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8263 << (bitpos - HOST_BITS_PER_WIDE_INT);
8264 }
ffc5c6a9 8265
807e902e
KZ
8266 value = wide_int_to_tree (type,
8267 wide_int::from_array (val, 2,
8268 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8269 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8270 braced_init_obstack);
9dfcc8db
BH
8271 }
8272
3e4093b6
RS
8273 constructor_incremental = 0;
8274}
de520661 8275
3e4093b6
RS
8276/* Return value of FIELD in pending initializer or zero if the field was
8277 not initialized yet. */
8278
8279static tree
a1e3b3d9 8280find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8281{
8282 struct init_node *p;
8283
8284 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8285 {
3e4093b6
RS
8286 if (constructor_incremental
8287 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8288 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8289
8290 p = constructor_pending_elts;
8291 while (p)
19d76e60 8292 {
3e4093b6
RS
8293 if (tree_int_cst_lt (field, p->purpose))
8294 p = p->left;
8295 else if (tree_int_cst_lt (p->purpose, field))
8296 p = p->right;
8297 else
8298 return p->value;
19d76e60 8299 }
19d76e60 8300 }
3e4093b6 8301 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8302 {
3e4093b6 8303 tree bitpos = bit_position (field);
de520661 8304
3e4093b6
RS
8305 if (constructor_incremental
8306 && (!constructor_unfilled_fields
8307 || tree_int_cst_lt (bitpos,
8308 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8309 set_nonincremental_init (braced_init_obstack);
de520661 8310
3e4093b6
RS
8311 p = constructor_pending_elts;
8312 while (p)
8313 {
8314 if (field == p->purpose)
8315 return p->value;
8316 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8317 p = p->left;
8318 else
8319 p = p->right;
8320 }
8321 }
8322 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8323 {
9771b263
DN
8324 if (!vec_safe_is_empty (constructor_elements)
8325 && (constructor_elements->last ().index == field))
8326 return constructor_elements->last ().value;
de520661 8327 }
3e4093b6 8328 return 0;
de520661
RS
8329}
8330
3e4093b6
RS
8331/* "Output" the next constructor element.
8332 At top level, really output it to assembler code now.
8333 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8334 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8335 TYPE is the data type that the containing data type wants here.
8336 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8337 If VALUE is a string constant, STRICT_STRING is true if it is
8338 unparenthesized or we should not warn here for it being parenthesized.
8339 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8340
3e4093b6
RS
8341 PENDING if non-nil means output pending elements that belong
8342 right after this element. (PENDING is normally 1;
b295aee2
JJ
8343 it is 0 while outputting pending elements, to avoid recursion.)
8344
8345 IMPLICIT is true if value comes from pop_init_level (1),
8346 the new initializer has been merged with the existing one
8347 and thus no warnings should be emitted about overriding an
8348 existing initializer. */
8b6a5902 8349
3e4093b6 8350static void
34cf811f
MP
8351output_init_element (location_t loc, tree value, tree origtype,
8352 bool strict_string, tree type, tree field, int pending,
8353 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8354{
8ce94e44 8355 tree semantic_type = NULL_TREE;
928c19bb
JM
8356 bool maybe_const = true;
8357 bool npc;
4038c495 8358
0a880880 8359 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8360 {
3e4093b6
RS
8361 constructor_erroneous = 1;
8362 return;
8b6a5902 8363 }
46bdb9cf
JM
8364 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8365 && (TREE_CODE (value) == STRING_CST
8366 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8367 && !(TREE_CODE (value) == STRING_CST
8368 && TREE_CODE (type) == ARRAY_TYPE
8369 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8370 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8371 TYPE_MAIN_VARIANT (type)))
c2255bc4 8372 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8373
3e4093b6 8374 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8375 && require_constant_value && pending)
8b6a5902 8376 {
3e4093b6
RS
8377 /* As an extension, allow initializing objects with static storage
8378 duration with compound literals (which are then treated just as
8379 the brace enclosed list they contain). */
4435bb92
MP
8380 if (flag_isoc99)
8381 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8382 "constant");
3e4093b6
RS
8383 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8384 value = DECL_INITIAL (decl);
8b6a5902
JJ
8385 }
8386
928c19bb 8387 npc = null_pointer_constant_p (value);
8ce94e44
JM
8388 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8389 {
8390 semantic_type = TREE_TYPE (value);
8391 value = TREE_OPERAND (value, 0);
8392 }
928c19bb
JM
8393 value = c_fully_fold (value, require_constant_value, &maybe_const);
8394
3e4093b6
RS
8395 if (value == error_mark_node)
8396 constructor_erroneous = 1;
8397 else if (!TREE_CONSTANT (value))
8398 constructor_constant = 0;
116df786 8399 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8400 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8401 || TREE_CODE (constructor_type) == UNION_TYPE)
8402 && DECL_C_BIT_FIELD (field)
8403 && TREE_CODE (value) != INTEGER_CST))
8404 constructor_simple = 0;
928c19bb
JM
8405 if (!maybe_const)
8406 constructor_nonconst = 1;
3e4093b6 8407
116df786 8408 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8409 {
116df786
RH
8410 if (require_constant_value)
8411 {
ea58ef42 8412 error_init (loc, "initializer element is not constant");
116df786
RH
8413 value = error_mark_node;
8414 }
8415 else if (require_constant_elements)
8337d1db 8416 pedwarn (loc, OPT_Wpedantic,
509c9d60 8417 "initializer element is not computable at load time");
8b6a5902 8418 }
928c19bb
JM
8419 else if (!maybe_const
8420 && (require_constant_value || require_constant_elements))
8337d1db 8421 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8422 "initializer element is not a constant expression");
3e4093b6 8423
81f40b79
ILT
8424 /* Issue -Wc++-compat warnings about initializing a bitfield with
8425 enum type. */
8426 if (warn_cxx_compat
8427 && field != NULL_TREE
8428 && TREE_CODE (field) == FIELD_DECL
8429 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8430 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8431 != TYPE_MAIN_VARIANT (type))
8432 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8433 {
8434 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8435 if (checktype != error_mark_node
8436 && (TYPE_MAIN_VARIANT (checktype)
8437 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8438 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8439 "enum conversion in initialization is invalid in C++");
8440 }
8441
3e4093b6
RS
8442 /* If this field is empty (and not at the end of structure),
8443 don't do anything other than checking the initializer. */
8444 if (field
8445 && (TREE_TYPE (field) == error_mark_node
8446 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8447 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8448 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8449 || DECL_CHAIN (field)))))
3e4093b6
RS
8450 return;
8451
8ce94e44
JM
8452 if (semantic_type)
8453 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8454 value = digest_init (loc, type, value, origtype, npc, strict_string,
8455 require_constant_value);
3e4093b6 8456 if (value == error_mark_node)
8b6a5902 8457 {
3e4093b6
RS
8458 constructor_erroneous = 1;
8459 return;
8b6a5902 8460 }
928c19bb
JM
8461 if (require_constant_value || require_constant_elements)
8462 constant_expression_warning (value);
8b6a5902 8463
3e4093b6
RS
8464 /* If this element doesn't come next in sequence,
8465 put it on constructor_pending_elts. */
8466 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8467 && (!constructor_incremental
8468 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8469 {
3e4093b6
RS
8470 if (constructor_incremental
8471 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8472 set_nonincremental_init (braced_init_obstack);
3e4093b6 8473
96b40f8d 8474 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8475 braced_init_obstack);
3e4093b6 8476 return;
8b6a5902 8477 }
3e4093b6
RS
8478 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8479 && (!constructor_incremental
8480 || field != constructor_unfilled_fields))
8b6a5902 8481 {
3e4093b6
RS
8482 /* We do this for records but not for unions. In a union,
8483 no matter which field is specified, it can be initialized
8484 right away since it starts at the beginning of the union. */
8485 if (constructor_incremental)
8486 {
8487 if (!constructor_unfilled_fields)
a1e3b3d9 8488 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8489 else
8490 {
8491 tree bitpos, unfillpos;
8492
8493 bitpos = bit_position (field);
8494 unfillpos = bit_position (constructor_unfilled_fields);
8495
8496 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8497 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8498 }
8499 }
8500
96b40f8d 8501 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8502 braced_init_obstack);
3e4093b6 8503 return;
8b6a5902 8504 }
3e4093b6 8505 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8506 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8507 {
b295aee2
JJ
8508 if (!implicit)
8509 {
9771b263 8510 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8511 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8512 "initialized field with side-effects overwritten");
8513 else if (warn_override_init)
96b40f8d
MP
8514 warning_init (loc, OPT_Woverride_init,
8515 "initialized field overwritten");
b295aee2 8516 }
8b6a5902 8517
3e4093b6 8518 /* We can have just one union field set. */
9771b263 8519 constructor_elements = NULL;
3e4093b6 8520 }
8b6a5902 8521
3e4093b6
RS
8522 /* Otherwise, output this element either to
8523 constructor_elements or to the assembler file. */
8b6a5902 8524
f32682ca 8525 constructor_elt celt = {field, value};
9771b263 8526 vec_safe_push (constructor_elements, celt);
8b6a5902 8527
3e4093b6
RS
8528 /* Advance the variable that indicates sequential elements output. */
8529 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8530 constructor_unfilled_index
db3927fb
AH
8531 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8532 bitsize_one_node);
3e4093b6
RS
8533 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8534 {
8535 constructor_unfilled_fields
910ad8de 8536 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8537
3e4093b6
RS
8538 /* Skip any nameless bit fields. */
8539 while (constructor_unfilled_fields != 0
8540 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8541 && DECL_NAME (constructor_unfilled_fields) == 0)
8542 constructor_unfilled_fields =
910ad8de 8543 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8544 }
8545 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8546 constructor_unfilled_fields = 0;
de520661 8547
3e4093b6
RS
8548 /* Now output any pending elements which have become next. */
8549 if (pending)
a1e3b3d9 8550 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8551}
8b6a5902 8552
3e4093b6
RS
8553/* Output any pending elements which have become next.
8554 As we output elements, constructor_unfilled_{fields,index}
8555 advances, which may cause other elements to become next;
8556 if so, they too are output.
8b6a5902 8557
3e4093b6
RS
8558 If ALL is 0, we return when there are
8559 no more pending elements to output now.
665f2503 8560
3e4093b6
RS
8561 If ALL is 1, we output space as necessary so that
8562 we can output all the pending elements. */
3e4093b6 8563static void
a1e3b3d9 8564output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8565{
8566 struct init_node *elt = constructor_pending_elts;
8567 tree next;
de520661 8568
3e4093b6
RS
8569 retry:
8570
ba228239 8571 /* Look through the whole pending tree.
3e4093b6
RS
8572 If we find an element that should be output now,
8573 output it. Otherwise, set NEXT to the element
8574 that comes first among those still pending. */
8575
8576 next = 0;
8577 while (elt)
8578 {
8579 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8580 {
3e4093b6
RS
8581 if (tree_int_cst_equal (elt->purpose,
8582 constructor_unfilled_index))
34cf811f
MP
8583 output_init_element (input_location, elt->value, elt->origtype,
8584 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8585 constructor_unfilled_index, 0, false,
8586 braced_init_obstack);
3e4093b6
RS
8587 else if (tree_int_cst_lt (constructor_unfilled_index,
8588 elt->purpose))
8b6a5902 8589 {
3e4093b6
RS
8590 /* Advance to the next smaller node. */
8591 if (elt->left)
8592 elt = elt->left;
8593 else
8594 {
8595 /* We have reached the smallest node bigger than the
8596 current unfilled index. Fill the space first. */
8597 next = elt->purpose;
8598 break;
8599 }
8b6a5902 8600 }
ce662d4c
JJ
8601 else
8602 {
3e4093b6
RS
8603 /* Advance to the next bigger node. */
8604 if (elt->right)
8605 elt = elt->right;
8606 else
ce662d4c 8607 {
3e4093b6
RS
8608 /* We have reached the biggest node in a subtree. Find
8609 the parent of it, which is the next bigger node. */
8610 while (elt->parent && elt->parent->right == elt)
8611 elt = elt->parent;
8612 elt = elt->parent;
8613 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8614 elt->purpose))
8615 {
8616 next = elt->purpose;
8617 break;
8618 }
ce662d4c
JJ
8619 }
8620 }
8b6a5902 8621 }
3e4093b6
RS
8622 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8623 || TREE_CODE (constructor_type) == UNION_TYPE)
8624 {
8625 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8626
3e4093b6
RS
8627 /* If the current record is complete we are done. */
8628 if (constructor_unfilled_fields == 0)
8629 break;
de520661 8630
3e4093b6
RS
8631 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8632 elt_bitpos = bit_position (elt->purpose);
8633 /* We can't compare fields here because there might be empty
8634 fields in between. */
8635 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8636 {
8637 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8638 output_init_element (input_location, elt->value, elt->origtype,
8639 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8640 elt->purpose, 0, false,
8641 braced_init_obstack);
3e4093b6
RS
8642 }
8643 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8644 {
8645 /* Advance to the next smaller node. */
8646 if (elt->left)
8647 elt = elt->left;
8648 else
8649 {
8650 /* We have reached the smallest node bigger than the
8651 current unfilled field. Fill the space first. */
8652 next = elt->purpose;
8653 break;
8654 }
8655 }
8656 else
8657 {
8658 /* Advance to the next bigger node. */
8659 if (elt->right)
8660 elt = elt->right;
8661 else
8662 {
8663 /* We have reached the biggest node in a subtree. Find
8664 the parent of it, which is the next bigger node. */
8665 while (elt->parent && elt->parent->right == elt)
8666 elt = elt->parent;
8667 elt = elt->parent;
8668 if (elt
8669 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8670 bit_position (elt->purpose))))
8671 {
8672 next = elt->purpose;
8673 break;
8674 }
8675 }
8676 }
8677 }
8678 }
de520661 8679
3e4093b6
RS
8680 /* Ordinarily return, but not if we want to output all
8681 and there are elements left. */
3f75a254 8682 if (!(all && next != 0))
e5cfb88f
RK
8683 return;
8684
3e4093b6
RS
8685 /* If it's not incremental, just skip over the gap, so that after
8686 jumping to retry we will output the next successive element. */
8687 if (TREE_CODE (constructor_type) == RECORD_TYPE
8688 || TREE_CODE (constructor_type) == UNION_TYPE)
8689 constructor_unfilled_fields = next;
8690 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8691 constructor_unfilled_index = next;
de520661 8692
3e4093b6
RS
8693 /* ELT now points to the node in the pending tree with the next
8694 initializer to output. */
8695 goto retry;
de520661
RS
8696}
8697\f
3e4093b6
RS
8698/* Add one non-braced element to the current constructor level.
8699 This adjusts the current position within the constructor's type.
8700 This may also start or terminate implicit levels
8701 to handle a partly-braced initializer.
e5e809f4 8702
3e4093b6 8703 Once this has found the correct level for the new element,
b295aee2
JJ
8704 it calls output_init_element.
8705
8706 IMPLICIT is true if value comes from pop_init_level (1),
8707 the new initializer has been merged with the existing one
8708 and thus no warnings should be emitted about overriding an
8709 existing initializer. */
3e4093b6
RS
8710
8711void
34cf811f 8712process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8713 struct obstack * braced_init_obstack)
e5e809f4 8714{
916c5919
JM
8715 tree orig_value = value.value;
8716 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8717 bool strict_string = value.original_code == STRING_CST;
340baef7 8718 bool was_designated = designator_depth != 0;
e5e809f4 8719
3e4093b6 8720 designator_depth = 0;
b06df647 8721 designator_erroneous = 0;
e5e809f4 8722
9bac5cbb
G
8723 if (!implicit && value.value && !integer_zerop (value.value))
8724 constructor_zeroinit = 0;
8725
3e4093b6
RS
8726 /* Handle superfluous braces around string cst as in
8727 char x[] = {"foo"}; */
8728 if (string_flag
8729 && constructor_type
340baef7 8730 && !was_designated
3e4093b6 8731 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8732 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8733 && integer_zerop (constructor_unfilled_index))
e5e809f4 8734 {
916c5919 8735 if (constructor_stack->replacement_value.value)
ea58ef42 8736 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8737 constructor_stack->replacement_value = value;
8738 return;
e5e809f4 8739 }
8b6a5902 8740
916c5919 8741 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8742 {
ea58ef42 8743 error_init (loc, "excess elements in struct initializer");
3e4093b6 8744 return;
e5e809f4
JL
8745 }
8746
3e4093b6
RS
8747 /* Ignore elements of a brace group if it is entirely superfluous
8748 and has already been diagnosed. */
8749 if (constructor_type == 0)
8750 return;
e5e809f4 8751
976d5a22
TT
8752 if (!implicit && warn_designated_init && !was_designated
8753 && TREE_CODE (constructor_type) == RECORD_TYPE
8754 && lookup_attribute ("designated_init",
8755 TYPE_ATTRIBUTES (constructor_type)))
8756 warning_init (loc,
8757 OPT_Wdesignated_init,
8758 "positional initialization of field "
8759 "in %<struct%> declared with %<designated_init%> attribute");
8760
3e4093b6
RS
8761 /* If we've exhausted any levels that didn't have braces,
8762 pop them now. */
8763 while (constructor_stack->implicit)
8764 {
8765 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8766 || TREE_CODE (constructor_type) == UNION_TYPE)
8767 && constructor_fields == 0)
ea58ef42
MP
8768 process_init_element (loc,
8769 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8770 true, braced_init_obstack);
53650abe
AP
8771 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8772 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8773 && constructor_max_index
8774 && tree_int_cst_lt (constructor_max_index,
8775 constructor_index))
ea58ef42
MP
8776 process_init_element (loc,
8777 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8778 true, braced_init_obstack);
3e4093b6
RS
8779 else
8780 break;
8781 }
e5e809f4 8782
3e4093b6
RS
8783 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8784 if (constructor_range_stack)
e5e809f4 8785 {
3e4093b6
RS
8786 /* If value is a compound literal and we'll be just using its
8787 content, don't put it into a SAVE_EXPR. */
916c5919 8788 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 8789 || !require_constant_value)
8ce94e44
JM
8790 {
8791 tree semantic_type = NULL_TREE;
8792 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8793 {
8794 semantic_type = TREE_TYPE (value.value);
8795 value.value = TREE_OPERAND (value.value, 0);
8796 }
8797 value.value = c_save_expr (value.value);
8798 if (semantic_type)
8799 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8800 value.value);
8801 }
3e4093b6 8802 }
e5e809f4 8803
3e4093b6
RS
8804 while (1)
8805 {
8806 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8807 {
3e4093b6
RS
8808 tree fieldtype;
8809 enum tree_code fieldcode;
e5e809f4 8810
3e4093b6
RS
8811 if (constructor_fields == 0)
8812 {
ea58ef42 8813 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8814 break;
8815 }
e5e809f4 8816
3e4093b6
RS
8817 fieldtype = TREE_TYPE (constructor_fields);
8818 if (fieldtype != error_mark_node)
8819 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8820 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8821
3e4093b6
RS
8822 /* Error for non-static initialization of a flexible array member. */
8823 if (fieldcode == ARRAY_TYPE
8824 && !require_constant_value
8825 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8826 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8827 {
ea58ef42
MP
8828 error_init (loc, "non-static initialization of a flexible "
8829 "array member");
3e4093b6
RS
8830 break;
8831 }
e5e809f4 8832
2cc901dc
MP
8833 /* Error for initialization of a flexible array member with
8834 a string constant if the structure is in an array. E.g.:
8835 struct S { int x; char y[]; };
8836 struct S s[] = { { 1, "foo" } };
8837 is invalid. */
8838 if (string_flag
8839 && fieldcode == ARRAY_TYPE
8840 && constructor_depth > 1
8841 && TYPE_SIZE (fieldtype) == NULL_TREE
8842 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8843 {
8844 bool in_array_p = false;
8845 for (struct constructor_stack *p = constructor_stack;
8846 p && p->type; p = p->next)
8847 if (TREE_CODE (p->type) == ARRAY_TYPE)
8848 {
8849 in_array_p = true;
8850 break;
8851 }
8852 if (in_array_p)
8853 {
8854 error_init (loc, "initialization of flexible array "
8855 "member in a nested context");
8856 break;
8857 }
8858 }
8859
3e4093b6 8860 /* Accept a string constant to initialize a subarray. */
916c5919 8861 if (value.value != 0
3e4093b6 8862 && fieldcode == ARRAY_TYPE
197463ae 8863 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8864 && string_flag)
916c5919 8865 value.value = orig_value;
3e4093b6
RS
8866 /* Otherwise, if we have come to a subaggregate,
8867 and we don't have an element of its type, push into it. */
0953878d 8868 else if (value.value != 0
916c5919
JM
8869 && value.value != error_mark_node
8870 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8871 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8872 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8873 {
ea58ef42 8874 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8875 continue;
8876 }
e5e809f4 8877
916c5919 8878 if (value.value)
3e4093b6
RS
8879 {
8880 push_member_name (constructor_fields);
34cf811f 8881 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8882 strict_string, fieldtype,
a1e3b3d9
LB
8883 constructor_fields, 1, implicit,
8884 braced_init_obstack);
3e4093b6 8885 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8886 }
8887 else
3e4093b6
RS
8888 /* Do the bookkeeping for an element that was
8889 directly output as a constructor. */
e5e809f4 8890 {
3e4093b6
RS
8891 /* For a record, keep track of end position of last field. */
8892 if (DECL_SIZE (constructor_fields))
c22cacf3 8893 constructor_bit_index
db3927fb
AH
8894 = size_binop_loc (input_location, PLUS_EXPR,
8895 bit_position (constructor_fields),
8896 DECL_SIZE (constructor_fields));
3e4093b6
RS
8897
8898 /* If the current field was the first one not yet written out,
8899 it isn't now, so update. */
8900 if (constructor_unfilled_fields == constructor_fields)
8901 {
910ad8de 8902 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8903 /* Skip any nameless bit fields. */
8904 while (constructor_unfilled_fields != 0
8905 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8906 && DECL_NAME (constructor_unfilled_fields) == 0)
8907 constructor_unfilled_fields =
910ad8de 8908 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8909 }
e5e809f4 8910 }
3e4093b6 8911
910ad8de 8912 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8913 /* Skip any nameless bit fields at the beginning. */
8914 while (constructor_fields != 0
8915 && DECL_C_BIT_FIELD (constructor_fields)
8916 && DECL_NAME (constructor_fields) == 0)
910ad8de 8917 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8918 }
3e4093b6 8919 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8920 {
3e4093b6
RS
8921 tree fieldtype;
8922 enum tree_code fieldcode;
e5e809f4 8923
3e4093b6
RS
8924 if (constructor_fields == 0)
8925 {
d033409e 8926 pedwarn_init (loc, 0,
509c9d60 8927 "excess elements in union initializer");
3e4093b6
RS
8928 break;
8929 }
e5e809f4 8930
3e4093b6
RS
8931 fieldtype = TREE_TYPE (constructor_fields);
8932 if (fieldtype != error_mark_node)
8933 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8934 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8935
3e4093b6
RS
8936 /* Warn that traditional C rejects initialization of unions.
8937 We skip the warning if the value is zero. This is done
8938 under the assumption that the zero initializer in user
8939 code appears conditioned on e.g. __STDC__ to avoid
8940 "missing initializer" warnings and relies on default
8941 initialization to zero in the traditional C case.
8942 We also skip the warning if the initializer is designated,
8943 again on the assumption that this must be conditional on
8944 __STDC__ anyway (and we've already complained about the
8945 member-designator already). */
8400e75e 8946 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8947 && !(value.value && (integer_zerop (value.value)
8948 || real_zerop (value.value))))
3176a0c2
DD
8949 warning (OPT_Wtraditional, "traditional C rejects initialization "
8950 "of unions");
e5e809f4 8951
3e4093b6 8952 /* Accept a string constant to initialize a subarray. */
916c5919 8953 if (value.value != 0
3e4093b6 8954 && fieldcode == ARRAY_TYPE
197463ae 8955 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8956 && string_flag)
916c5919 8957 value.value = orig_value;
3e4093b6
RS
8958 /* Otherwise, if we have come to a subaggregate,
8959 and we don't have an element of its type, push into it. */
0953878d 8960 else if (value.value != 0
916c5919
JM
8961 && value.value != error_mark_node
8962 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8963 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8964 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8965 {
ea58ef42 8966 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8967 continue;
8968 }
e5e809f4 8969
916c5919 8970 if (value.value)
3e4093b6
RS
8971 {
8972 push_member_name (constructor_fields);
34cf811f 8973 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8974 strict_string, fieldtype,
a1e3b3d9
LB
8975 constructor_fields, 1, implicit,
8976 braced_init_obstack);
3e4093b6 8977 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8978 }
8979 else
3e4093b6
RS
8980 /* Do the bookkeeping for an element that was
8981 directly output as a constructor. */
e5e809f4 8982 {
3e4093b6 8983 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8984 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8985 }
e5e809f4 8986
3e4093b6
RS
8987 constructor_fields = 0;
8988 }
8989 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8990 {
8991 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8992 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8993
3e4093b6 8994 /* Accept a string constant to initialize a subarray. */
916c5919 8995 if (value.value != 0
3e4093b6 8996 && eltcode == ARRAY_TYPE
197463ae 8997 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8998 && string_flag)
916c5919 8999 value.value = orig_value;
3e4093b6
RS
9000 /* Otherwise, if we have come to a subaggregate,
9001 and we don't have an element of its type, push into it. */
0953878d 9002 else if (value.value != 0
916c5919
JM
9003 && value.value != error_mark_node
9004 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9005 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9006 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9007 {
ea58ef42 9008 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9009 continue;
9010 }
8b6a5902 9011
3e4093b6
RS
9012 if (constructor_max_index != 0
9013 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9014 || integer_all_onesp (constructor_max_index)))
9015 {
d033409e 9016 pedwarn_init (loc, 0,
509c9d60 9017 "excess elements in array initializer");
3e4093b6
RS
9018 break;
9019 }
8b6a5902 9020
3e4093b6 9021 /* Now output the actual element. */
916c5919 9022 if (value.value)
3e4093b6 9023 {
ae7e9ddd 9024 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9025 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9026 strict_string, elttype,
a1e3b3d9
LB
9027 constructor_index, 1, implicit,
9028 braced_init_obstack);
3e4093b6
RS
9029 RESTORE_SPELLING_DEPTH (constructor_depth);
9030 }
2f6e4e97 9031
3e4093b6 9032 constructor_index
db3927fb
AH
9033 = size_binop_loc (input_location, PLUS_EXPR,
9034 constructor_index, bitsize_one_node);
8b6a5902 9035
916c5919 9036 if (!value.value)
3e4093b6
RS
9037 /* If we are doing the bookkeeping for an element that was
9038 directly output as a constructor, we must update
9039 constructor_unfilled_index. */
9040 constructor_unfilled_index = constructor_index;
9041 }
9042 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9043 {
9044 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9045
c22cacf3
MS
9046 /* Do a basic check of initializer size. Note that vectors
9047 always have a fixed size derived from their type. */
3e4093b6
RS
9048 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9049 {
d033409e 9050 pedwarn_init (loc, 0,
509c9d60 9051 "excess elements in vector initializer");
3e4093b6
RS
9052 break;
9053 }
8b6a5902 9054
3e4093b6 9055 /* Now output the actual element. */
916c5919 9056 if (value.value)
53650abe
AP
9057 {
9058 if (TREE_CODE (value.value) == VECTOR_CST)
9059 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9060 output_init_element (loc, value.value, value.original_type,
53650abe 9061 strict_string, elttype,
a1e3b3d9
LB
9062 constructor_index, 1, implicit,
9063 braced_init_obstack);
53650abe 9064 }
8b6a5902 9065
3e4093b6 9066 constructor_index
db3927fb
AH
9067 = size_binop_loc (input_location,
9068 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9069
916c5919 9070 if (!value.value)
3e4093b6
RS
9071 /* If we are doing the bookkeeping for an element that was
9072 directly output as a constructor, we must update
9073 constructor_unfilled_index. */
9074 constructor_unfilled_index = constructor_index;
9075 }
8b6a5902 9076
3e4093b6
RS
9077 /* Handle the sole element allowed in a braced initializer
9078 for a scalar variable. */
b4519d39
SB
9079 else if (constructor_type != error_mark_node
9080 && constructor_fields == 0)
8b6a5902 9081 {
d033409e 9082 pedwarn_init (loc, 0,
509c9d60 9083 "excess elements in scalar initializer");
3e4093b6 9084 break;
8b6a5902
JJ
9085 }
9086 else
9087 {
916c5919 9088 if (value.value)
34cf811f 9089 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9090 strict_string, constructor_type,
a1e3b3d9
LB
9091 NULL_TREE, 1, implicit,
9092 braced_init_obstack);
3e4093b6 9093 constructor_fields = 0;
8b6a5902
JJ
9094 }
9095
3e4093b6
RS
9096 /* Handle range initializers either at this level or anywhere higher
9097 in the designator stack. */
9098 if (constructor_range_stack)
8b6a5902 9099 {
3e4093b6
RS
9100 struct constructor_range_stack *p, *range_stack;
9101 int finish = 0;
9102
9103 range_stack = constructor_range_stack;
9104 constructor_range_stack = 0;
9105 while (constructor_stack != range_stack->stack)
8b6a5902 9106 {
366de0ce 9107 gcc_assert (constructor_stack->implicit);
34cf811f 9108 process_init_element (loc,
ea58ef42
MP
9109 pop_init_level (loc, 1,
9110 braced_init_obstack),
a1e3b3d9 9111 true, braced_init_obstack);
8b6a5902 9112 }
3e4093b6
RS
9113 for (p = range_stack;
9114 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9115 p = p->prev)
8b6a5902 9116 {
366de0ce 9117 gcc_assert (constructor_stack->implicit);
34cf811f 9118 process_init_element (loc,
ea58ef42
MP
9119 pop_init_level (loc, 1,
9120 braced_init_obstack),
a1e3b3d9 9121 true, braced_init_obstack);
8b6a5902 9122 }
3e4093b6 9123
db3927fb
AH
9124 p->index = size_binop_loc (input_location,
9125 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9126 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9127 finish = 1;
9128
9129 while (1)
9130 {
9131 constructor_index = p->index;
9132 constructor_fields = p->fields;
9133 if (finish && p->range_end && p->index == p->range_start)
9134 {
9135 finish = 0;
9136 p->prev = 0;
9137 }
9138 p = p->next;
9139 if (!p)
9140 break;
ea58ef42 9141 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9142 p->stack = constructor_stack;
9143 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9144 p->index = p->range_start;
9145 }
9146
9147 if (!finish)
9148 constructor_range_stack = range_stack;
9149 continue;
8b6a5902
JJ
9150 }
9151
3e4093b6 9152 break;
8b6a5902
JJ
9153 }
9154
3e4093b6
RS
9155 constructor_range_stack = 0;
9156}
9157\f
9f0e2d86
ZW
9158/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9159 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9160 an ASM_EXPR node). */
3e4093b6 9161tree
9f0e2d86 9162build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9163{
6de9cd9a
DN
9164 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9165 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9166 return add_stmt (args);
8b6a5902
JJ
9167}
9168
9f0e2d86
ZW
9169/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9170 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9171 SIMPLE indicates whether there was anything at all after the
9172 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9173 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9174tree
c2255bc4 9175build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9176 tree clobbers, tree labels, bool simple)
e5e809f4 9177{
3e4093b6 9178 tree tail;
9f0e2d86 9179 tree args;
6de9cd9a
DN
9180 int i;
9181 const char *constraint;
74f0c611 9182 const char **oconstraints;
6de9cd9a 9183 bool allows_mem, allows_reg, is_inout;
74f0c611 9184 int ninputs, noutputs;
6de9cd9a
DN
9185
9186 ninputs = list_length (inputs);
9187 noutputs = list_length (outputs);
74f0c611
RH
9188 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9189
1c384bf1 9190 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9191
6de9cd9a
DN
9192 /* Remove output conversions that change the type but not the mode. */
9193 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9194 {
3e4093b6 9195 tree output = TREE_VALUE (tail);
74f0c611 9196
eadd3d0d
JJ
9197 output = c_fully_fold (output, false, NULL);
9198
74f0c611
RH
9199 /* ??? Really, this should not be here. Users should be using a
9200 proper lvalue, dammit. But there's a long history of using casts
9201 in the output operands. In cases like longlong.h, this becomes a
9202 primitive form of typechecking -- if the cast can be removed, then
9203 the output operand had a type of the proper width; otherwise we'll
9204 get an error. Gross, but ... */
3e4093b6 9205 STRIP_NOPS (output);
74f0c611 9206
7bd11157 9207 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9208 output = error_mark_node;
8b6a5902 9209
5544530a
PB
9210 if (output != error_mark_node
9211 && (TREE_READONLY (output)
9212 || TYPE_READONLY (TREE_TYPE (output))
9213 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9214 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9215 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9216 readonly_error (loc, output, lv_asm);
5544530a 9217
6de9cd9a 9218 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9219 oconstraints[i] = constraint;
9220
9221 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9222 &allows_mem, &allows_reg, &is_inout))
9223 {
9224 /* If the operand is going to end up in memory,
9225 mark it addressable. */
9226 if (!allows_reg && !c_mark_addressable (output))
9227 output = error_mark_node;
bae5cddf
JJ
9228 if (!(!allows_reg && allows_mem)
9229 && output != error_mark_node
9230 && VOID_TYPE_P (TREE_TYPE (output)))
9231 {
9232 error_at (loc, "invalid use of void expression");
9233 output = error_mark_node;
9234 }
74f0c611
RH
9235 }
9236 else
c22cacf3 9237 output = error_mark_node;
3e4093b6 9238
74f0c611 9239 TREE_VALUE (tail) = output;
8b6a5902 9240 }
3e4093b6 9241
74f0c611
RH
9242 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9243 {
9244 tree input;
9245
9246 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9247 input = TREE_VALUE (tail);
9248
74f0c611
RH
9249 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9250 oconstraints, &allows_mem, &allows_reg))
9251 {
9252 /* If the operand is going to end up in memory,
9253 mark it addressable. */
b4c33883
AP
9254 if (!allows_reg && allows_mem)
9255 {
eadd3d0d
JJ
9256 input = c_fully_fold (input, false, NULL);
9257
b4c33883
AP
9258 /* Strip the nops as we allow this case. FIXME, this really
9259 should be rejected or made deprecated. */
9260 STRIP_NOPS (input);
9261 if (!c_mark_addressable (input))
9262 input = error_mark_node;
bae5cddf 9263 }
eadd3d0d 9264 else
bae5cddf 9265 {
eadd3d0d
JJ
9266 struct c_expr expr;
9267 memset (&expr, 0, sizeof (expr));
9268 expr.value = input;
267bac10 9269 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9270 input = c_fully_fold (expr.value, false, NULL);
9271
9272 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9273 {
9274 error_at (loc, "invalid use of void expression");
9275 input = error_mark_node;
9276 }
bae5cddf 9277 }
74f0c611
RH
9278 }
9279 else
9280 input = error_mark_node;
9281
9282 TREE_VALUE (tail) = input;
9283 }
3e4093b6 9284
1c384bf1
RH
9285 /* ASMs with labels cannot have outputs. This should have been
9286 enforced by the parser. */
9287 gcc_assert (outputs == NULL || labels == NULL);
9288
9289 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9290
5544530a
PB
9291 /* asm statements without outputs, including simple ones, are treated
9292 as volatile. */
9293 ASM_INPUT_P (args) = simple;
9294 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9295
9f0e2d86 9296 return args;
e5e809f4 9297}
3e4093b6 9298\f
c2255bc4
AH
9299/* Generate a goto statement to LABEL. LOC is the location of the
9300 GOTO. */
506e2710
RH
9301
9302tree
c2255bc4 9303c_finish_goto_label (location_t loc, tree label)
506e2710 9304{
e1b7793c 9305 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9306 if (!decl)
9307 return NULL_TREE;
506e2710 9308 TREE_USED (decl) = 1;
c2255bc4
AH
9309 {
9310 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9311 SET_EXPR_LOCATION (t, loc);
9312 return add_stmt (t);
9313 }
506e2710
RH
9314}
9315
c2255bc4
AH
9316/* Generate a computed goto statement to EXPR. LOC is the location of
9317 the GOTO. */
506e2710
RH
9318
9319tree
c2255bc4 9320c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9321{
c2255bc4 9322 tree t;
c1771a20 9323 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9324 expr = c_fully_fold (expr, false, NULL);
506e2710 9325 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9326 t = build1 (GOTO_EXPR, void_type_node, expr);
9327 SET_EXPR_LOCATION (t, loc);
9328 return add_stmt (t);
506e2710
RH
9329}
9330
5088b058 9331/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9332 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9333 the location of the return statement, or the location of the expression,
9334 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9335 is the original type of RETVAL. */
de520661 9336
506e2710 9337tree
c2255bc4 9338c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9339{
0c9b182b
JJ
9340 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9341 bool no_warning = false;
928c19bb 9342 bool npc = false;
36536d79 9343 size_t rank = 0;
3e4093b6
RS
9344
9345 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9346 warning_at (loc, 0,
9347 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9348
b72271b9 9349 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9350 {
9351 /* Array notations are allowed in a return statement if it is inside a
9352 built-in array notation reduction function. */
9353 if (!find_rank (loc, retval, retval, false, &rank))
9354 return error_mark_node;
9355 if (rank >= 1)
9356 {
9357 error_at (loc, "array notation expression cannot be used as a "
9358 "return value");
9359 return error_mark_node;
9360 }
9361 }
3af9c5e9 9362 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9363 {
9364 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9365 "allowed");
9366 return error_mark_node;
9367 }
928c19bb
JM
9368 if (retval)
9369 {
8ce94e44 9370 tree semantic_type = NULL_TREE;
928c19bb 9371 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9372 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9373 {
9374 semantic_type = TREE_TYPE (retval);
9375 retval = TREE_OPERAND (retval, 0);
9376 }
928c19bb 9377 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9378 if (semantic_type)
9379 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9380 }
9381
3e4093b6 9382 if (!retval)
de520661 9383 {
3e4093b6
RS
9384 current_function_returns_null = 1;
9385 if ((warn_return_type || flag_isoc99)
9386 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9387 {
35aff4fb
MP
9388 if (flag_isoc99)
9389 pedwarn (loc, 0, "%<return%> with no value, in "
9390 "function returning non-void");
9391 else
9392 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9393 "in function returning non-void");
0c9b182b
JJ
9394 no_warning = true;
9395 }
400fbf9f 9396 }
3e4093b6 9397 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9398 {
3e4093b6 9399 current_function_returns_null = 1;
2397c575 9400 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9401 pedwarn (loc, 0,
509c9d60 9402 "%<return%> with a value, in function returning void");
b8698a0f 9403 else
c1771a20 9404 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9405 "%<return%> with expression, in function returning void");
de520661 9406 }
3e4093b6 9407 else
de520661 9408 {
68fca595
MP
9409 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9410 retval, origtype, ic_return,
c2255bc4 9411 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9412 tree res = DECL_RESULT (current_function_decl);
9413 tree inner;
9feb29df 9414 bool save;
3e4093b6
RS
9415
9416 current_function_returns_value = 1;
9417 if (t == error_mark_node)
506e2710 9418 return NULL_TREE;
3e4093b6 9419
9feb29df
JJ
9420 save = in_late_binary_op;
9421 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9422 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9423 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9424 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9425 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9426 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9427 in_late_binary_op = true;
3e4093b6 9428 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9429 in_late_binary_op = save;
3e4093b6
RS
9430
9431 /* Strip any conversions, additions, and subtractions, and see if
9432 we are returning the address of a local variable. Warn if so. */
9433 while (1)
8b6a5902 9434 {
3e4093b6 9435 switch (TREE_CODE (inner))
8b6a5902 9436 {
849421a3
JJ
9437 CASE_CONVERT:
9438 case NON_LVALUE_EXPR:
3e4093b6 9439 case PLUS_EXPR:
849421a3 9440 case POINTER_PLUS_EXPR:
3e4093b6
RS
9441 inner = TREE_OPERAND (inner, 0);
9442 continue;
9443
9444 case MINUS_EXPR:
9445 /* If the second operand of the MINUS_EXPR has a pointer
9446 type (or is converted from it), this may be valid, so
9447 don't give a warning. */
9448 {
9449 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9450
3f75a254 9451 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9452 && (CONVERT_EXPR_P (op1)
9453 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9454 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9455
3e4093b6
RS
9456 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9457 break;
8b6a5902 9458
3e4093b6
RS
9459 inner = TREE_OPERAND (inner, 0);
9460 continue;
9461 }
400fbf9f 9462
3e4093b6
RS
9463 case ADDR_EXPR:
9464 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9465
6615c446 9466 while (REFERENCE_CLASS_P (inner)
22d03525 9467 && !INDIRECT_REF_P (inner))
3e4093b6 9468 inner = TREE_OPERAND (inner, 0);
8b6a5902 9469
a2f1f4c3 9470 if (DECL_P (inner)
3f75a254
JM
9471 && !DECL_EXTERNAL (inner)
9472 && !TREE_STATIC (inner)
3e4093b6 9473 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9474 {
9475 if (TREE_CODE (inner) == LABEL_DECL)
9476 warning_at (loc, OPT_Wreturn_local_addr,
9477 "function returns address of label");
9478 else
b4dfdc11
MG
9479 {
9480 warning_at (loc, OPT_Wreturn_local_addr,
9481 "function returns address of local variable");
9482 tree zero = build_zero_cst (TREE_TYPE (res));
9483 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9484 }
19fc9faa 9485 }
3e4093b6 9486 break;
8b6a5902 9487
3e4093b6
RS
9488 default:
9489 break;
9490 }
de520661 9491
3e4093b6
RS
9492 break;
9493 }
9494
53fb4de3 9495 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9496 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9497
9498 if (warn_sequence_point)
9499 verify_sequence_points (retval);
de520661 9500 }
8b6a5902 9501
c2255bc4 9502 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9503 TREE_NO_WARNING (ret_stmt) |= no_warning;
9504 return add_stmt (ret_stmt);
de520661 9505}
3e4093b6
RS
9506\f
9507struct c_switch {
604f5adf
ILT
9508 /* The SWITCH_EXPR being built. */
9509 tree switch_expr;
a6c0a76c 9510
89dbed81 9511 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9512 default conversion is applied. */
9513 tree orig_type;
9514
3e4093b6
RS
9515 /* A splay-tree mapping the low element of a case range to the high
9516 element, or NULL_TREE if there is no high element. Used to
9517 determine whether or not a new case label duplicates an old case
9518 label. We need a tree, rather than simply a hash table, because
9519 of the GNU case range extension. */
9520 splay_tree cases;
a6c0a76c 9521
e1b7793c
ILT
9522 /* The bindings at the point of the switch. This is used for
9523 warnings crossing decls when branching to a case label. */
9524 struct c_spot_bindings *bindings;
187230a7 9525
3e4093b6
RS
9526 /* The next node on the stack. */
9527 struct c_switch *next;
9528};
400fbf9f 9529
3e4093b6
RS
9530/* A stack of the currently active switch statements. The innermost
9531 switch statement is on the top of the stack. There is no need to
9532 mark the stack for garbage collection because it is only active
9533 during the processing of the body of a function, and we never
9534 collect at that point. */
de520661 9535
506e2710 9536struct c_switch *c_switch_stack;
de520661 9537
3e4093b6 9538/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9539 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9540 SWITCH_COND_LOC is the location of the switch's condition.
9541 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9542
3e4093b6 9543tree
c2255bc4
AH
9544c_start_case (location_t switch_loc,
9545 location_t switch_cond_loc,
fedfecef 9546 tree exp, bool explicit_cast_p)
de520661 9547{
c58e8676 9548 tree orig_type = error_mark_node;
3e4093b6 9549 struct c_switch *cs;
2f6e4e97 9550
3e4093b6 9551 if (exp != error_mark_node)
de520661 9552 {
3e4093b6
RS
9553 orig_type = TREE_TYPE (exp);
9554
c58e8676 9555 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9556 {
c58e8676
VR
9557 if (orig_type != error_mark_node)
9558 {
c2255bc4 9559 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9560 orig_type = error_mark_node;
9561 }
3e4093b6 9562 exp = integer_zero_node;
de520661 9563 }
3e4093b6 9564 else
de520661 9565 {
c58e8676 9566 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9567 tree e = exp;
9568
9569 /* Warn if the condition has boolean value. */
9570 while (TREE_CODE (e) == COMPOUND_EXPR)
9571 e = TREE_OPERAND (e, 1);
9572
9573 if ((TREE_CODE (type) == BOOLEAN_TYPE
9574 || truth_value_p (TREE_CODE (e)))
9575 /* Explicit cast to int suppresses this warning. */
9576 && !(TREE_CODE (type) == INTEGER_TYPE
9577 && explicit_cast_p))
9578 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9579 "switch condition has boolean value");
8b6a5902 9580
8400e75e 9581 if (!in_system_header_at (input_location)
3e4093b6
RS
9582 && (type == long_integer_type_node
9583 || type == long_unsigned_type_node))
c2255bc4
AH
9584 warning_at (switch_cond_loc,
9585 OPT_Wtraditional, "%<long%> switch expression not "
9586 "converted to %<int%> in ISO C");
8b6a5902 9587
928c19bb 9588 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9589 exp = default_conversion (exp);
ca085fd7
MLI
9590
9591 if (warn_sequence_point)
9592 verify_sequence_points (exp);
3e4093b6
RS
9593 }
9594 }
9595
604f5adf 9596 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9597 cs = XNEW (struct c_switch);
604f5adf 9598 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9599 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9600 cs->orig_type = orig_type;
3e4093b6 9601 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9602 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9603 cs->next = c_switch_stack;
9604 c_switch_stack = cs;
3e4093b6 9605
604f5adf 9606 return add_stmt (cs->switch_expr);
3e4093b6
RS
9607}
9608
c2255bc4 9609/* Process a case label at location LOC. */
3e4093b6
RS
9610
9611tree
c2255bc4 9612do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9613{
9614 tree label = NULL_TREE;
9615
17cede2e
JM
9616 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9617 {
9618 low_value = c_fully_fold (low_value, false, NULL);
9619 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9620 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9621 "case label is not an integer constant expression");
9622 }
9623
9624 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9625 {
9626 high_value = c_fully_fold (high_value, false, NULL);
9627 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9628 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9629 "case label is not an integer constant expression");
9630 }
9631
e1b7793c 9632 if (c_switch_stack == NULL)
187230a7
JM
9633 {
9634 if (low_value)
e1b7793c 9635 error_at (loc, "case label not within a switch statement");
187230a7 9636 else
e1b7793c
ILT
9637 error_at (loc, "%<default%> label not within a switch statement");
9638 return NULL_TREE;
187230a7 9639 }
de520661 9640
e1b7793c
ILT
9641 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9642 EXPR_LOCATION (c_switch_stack->switch_expr),
9643 loc))
9644 return NULL_TREE;
9645
9646 label = c_add_case_label (loc, c_switch_stack->cases,
9647 SWITCH_COND (c_switch_stack->switch_expr),
9648 c_switch_stack->orig_type,
9649 low_value, high_value);
9650 if (label == error_mark_node)
9651 label = NULL_TREE;
3e4093b6
RS
9652 return label;
9653}
de520661 9654
083e891e
MP
9655/* Finish the switch statement. TYPE is the original type of the
9656 controlling expression of the switch, or NULL_TREE. */
de520661 9657
3e4093b6 9658void
083e891e 9659c_finish_case (tree body, tree type)
3e4093b6 9660{
506e2710 9661 struct c_switch *cs = c_switch_stack;
fbc315db 9662 location_t switch_location;
3e4093b6 9663
604f5adf 9664 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9665
6de9cd9a 9666 /* Emit warnings as needed. */
c2255bc4 9667 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9668 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9669 type ? type : TREE_TYPE (cs->switch_expr),
fbc315db 9670 SWITCH_COND (cs->switch_expr));
6de9cd9a 9671
3e4093b6 9672 /* Pop the stack. */
506e2710 9673 c_switch_stack = cs->next;
3e4093b6 9674 splay_tree_delete (cs->cases);
e1b7793c 9675 c_release_switch_bindings (cs->bindings);
5d038c4c 9676 XDELETE (cs);
de520661 9677}
325c3691 9678\f
506e2710
RH
9679/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9680 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9681 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9682 statement, and was not surrounded with parenthesis. */
325c3691 9683
9e51cf9d 9684void
506e2710
RH
9685c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9686 tree else_block, bool nested_if)
325c3691 9687{
506e2710 9688 tree stmt;
325c3691 9689
25c22937
BI
9690 /* If the condition has array notations, then the rank of the then_block and
9691 else_block must be either 0 or be equal to the rank of the condition. If
9692 the condition does not have array notations then break them up as it is
9693 broken up in a normal expression. */
b72271b9 9694 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9695 {
9696 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9697 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9698 return;
9699 if (then_block
9700 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9701 return;
9702 if (else_block
9703 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9704 return;
9705 if (cond_rank != then_rank && then_rank != 0)
9706 {
9707 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9708 " and the then-block");
9709 return;
9710 }
9711 else if (cond_rank != else_rank && else_rank != 0)
9712 {
9713 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9714 " and the else-block");
9715 return;
9716 }
9717 }
506e2710
RH
9718 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9719 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9720 {
506e2710 9721 tree inner_if = then_block;
16865eaa 9722
61ada8ae 9723 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9724 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9725 it might not be exactly THEN_BLOCK, but should be the last
9726 non-container statement within. */
9727 while (1)
9728 switch (TREE_CODE (inner_if))
9729 {
9730 case COND_EXPR:
9731 goto found;
9732 case BIND_EXPR:
9733 inner_if = BIND_EXPR_BODY (inner_if);
9734 break;
9735 case STATEMENT_LIST:
9736 inner_if = expr_last (then_block);
9737 break;
9738 case TRY_FINALLY_EXPR:
9739 case TRY_CATCH_EXPR:
9740 inner_if = TREE_OPERAND (inner_if, 0);
9741 break;
9742 default:
366de0ce 9743 gcc_unreachable ();
506e2710
RH
9744 }
9745 found:
16865eaa 9746
506e2710 9747 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9748 warning_at (if_locus, OPT_Wparentheses,
9749 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9750 }
16865eaa 9751
2214de30 9752 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9753 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9754 add_stmt (stmt);
325c3691
RH
9755}
9756
506e2710
RH
9757/* Emit a general-purpose loop construct. START_LOCUS is the location of
9758 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9759 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9760 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9761 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9762
9763void
506e2710
RH
9764c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9765 tree blab, tree clab, bool cond_is_first)
325c3691 9766{
506e2710
RH
9767 tree entry = NULL, exit = NULL, t;
9768
e5e44252
AK
9769 /* In theory could forbid cilk spawn for loop increment expression,
9770 but it should work just fine. */
36536d79 9771
28af952a
RS
9772 /* If the condition is zero don't generate a loop construct. */
9773 if (cond && integer_zerop (cond))
9774 {
9775 if (cond_is_first)
9776 {
9777 t = build_and_jump (&blab);
9778 SET_EXPR_LOCATION (t, start_locus);
9779 add_stmt (t);
9780 }
9781 }
9782 else
506e2710
RH
9783 {
9784 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9785
506e2710 9786 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9787 out of the loop, or to the top of it. If there's no exit condition,
9788 then we just build a jump back to the top. */
506e2710 9789 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9790
28af952a 9791 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9792 {
9793 /* Canonicalize the loop condition to the end. This means
9794 generating a branch to the loop condition. Reuse the
9795 continue label, if possible. */
9796 if (cond_is_first)
9797 {
9798 if (incr || !clab)
9799 {
9800 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9801 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9802 }
9803 else
9804 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9805 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9806 add_stmt (t);
9807 }
9808
506e2710 9809 t = build_and_jump (&blab);
506e2710 9810 if (cond_is_first)
db3927fb
AH
9811 exit = fold_build3_loc (start_locus,
9812 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9813 else
db3927fb
AH
9814 exit = fold_build3_loc (input_location,
9815 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9816 }
9817
506e2710
RH
9818 add_stmt (top);
9819 }
c22cacf3 9820
506e2710
RH
9821 if (body)
9822 add_stmt (body);
9823 if (clab)
9824 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9825 if (incr)
9826 add_stmt (incr);
9827 if (entry)
9828 add_stmt (entry);
9829 if (exit)
9830 add_stmt (exit);
9831 if (blab)
9832 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9833}
325c3691
RH
9834
9835tree
c2255bc4 9836c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9837{
089efaa4 9838 bool skip;
506e2710 9839 tree label = *label_p;
325c3691 9840
089efaa4
ILT
9841 /* In switch statements break is sometimes stylistically used after
9842 a return statement. This can lead to spurious warnings about
9843 control reaching the end of a non-void function when it is
9844 inlined. Note that we are calling block_may_fallthru with
9845 language specific tree nodes; this works because
9846 block_may_fallthru returns true when given something it does not
9847 understand. */
9848 skip = !block_may_fallthru (cur_stmt_list);
9849
506e2710 9850 if (!label)
089efaa4
ILT
9851 {
9852 if (!skip)
c2255bc4 9853 *label_p = label = create_artificial_label (loc);
089efaa4 9854 }
953ff289
DN
9855 else if (TREE_CODE (label) == LABEL_DECL)
9856 ;
9857 else switch (TREE_INT_CST_LOW (label))
506e2710 9858 {
953ff289 9859 case 0:
506e2710 9860 if (is_break)
c2255bc4 9861 error_at (loc, "break statement not within loop or switch");
506e2710 9862 else
c2255bc4 9863 error_at (loc, "continue statement not within a loop");
506e2710 9864 return NULL_TREE;
953ff289
DN
9865
9866 case 1:
9867 gcc_assert (is_break);
c2255bc4 9868 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9869 return NULL_TREE;
9870
c02065fc
AH
9871 case 2:
9872 if (is_break)
9873 error ("break statement within %<#pragma simd%> loop body");
9874 else
9875 error ("continue statement within %<#pragma simd%> loop body");
9876 return NULL_TREE;
9877
953ff289
DN
9878 default:
9879 gcc_unreachable ();
506e2710 9880 }
325c3691 9881
089efaa4
ILT
9882 if (skip)
9883 return NULL_TREE;
9884
2e28e797
JH
9885 if (!is_break)
9886 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9887
53fb4de3 9888 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9889}
9890
506e2710 9891/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9892
9893static void
c2255bc4 9894emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9895{
e6b5a630
RH
9896 if (expr == error_mark_node)
9897 ;
9898 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9899 {
9900 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9901 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9902 }
789eadcd
MP
9903 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9904 {
9905 tree r = expr;
9906 location_t cloc = loc;
9907 while (TREE_CODE (r) == COMPOUND_EXPR)
9908 {
9909 if (EXPR_HAS_LOCATION (r))
9910 cloc = EXPR_LOCATION (r);
9911 r = TREE_OPERAND (r, 1);
9912 }
9913 if (!TREE_SIDE_EFFECTS (r)
9914 && !VOID_TYPE_P (TREE_TYPE (r))
9915 && !CONVERT_EXPR_P (r)
cc28fc7f 9916 && !TREE_NO_WARNING (r)
789eadcd
MP
9917 && !TREE_NO_WARNING (expr))
9918 warning_at (cloc, OPT_Wunused_value,
9919 "right-hand operand of comma expression has no effect");
9920 }
27f33b15 9921 else
c2255bc4 9922 warn_if_unused_value (expr, loc);
3a5b9284
RH
9923}
9924
506e2710 9925/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9926 diagnostics, but do not call ADD_STMT. LOC is the location of the
9927 statement. */
3a5b9284 9928
506e2710 9929tree
c2255bc4 9930c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9931{
056928b2
JJ
9932 tree exprv;
9933
3a5b9284 9934 if (!expr)
506e2710 9935 return NULL_TREE;
3a5b9284 9936
928c19bb
JM
9937 expr = c_fully_fold (expr, false, NULL);
9938
3a5b9284
RH
9939 if (warn_sequence_point)
9940 verify_sequence_points (expr);
9941
9942 if (TREE_TYPE (expr) != error_mark_node
9943 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9944 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9945 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9946
9947 /* If we're not processing a statement expression, warn about unused values.
9948 Warnings for statement expressions will be emitted later, once we figure
9949 out which is the result. */
9950 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9951 && warn_unused_value)
c2255bc4 9952 emit_side_effect_warnings (loc, expr);
3a5b9284 9953
056928b2
JJ
9954 exprv = expr;
9955 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9956 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9957 while (CONVERT_EXPR_P (exprv))
9958 exprv = TREE_OPERAND (exprv, 0);
9959 if (DECL_P (exprv)
9960 || handled_component_p (exprv)
9961 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9962 mark_exp_read (exprv);
fa8351f8 9963
3a5b9284
RH
9964 /* If the expression is not of a type to which we cannot assign a line
9965 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9966 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9967 {
9968 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9969 SET_EXPR_LOCATION (expr, loc);
9970 }
506e2710
RH
9971
9972 return expr;
9973}
9974
c2255bc4
AH
9975/* Emit an expression as a statement. LOC is the location of the
9976 expression. */
506e2710
RH
9977
9978tree
c2255bc4 9979c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9980{
9981 if (expr)
c2255bc4 9982 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9983 else
9984 return NULL;
3a5b9284
RH
9985}
9986
9987/* Do the opposite and emit a statement as an expression. To begin,
9988 create a new binding level and return it. */
325c3691
RH
9989
9990tree
9991c_begin_stmt_expr (void)
9992{
9993 tree ret;
9994
9995 /* We must force a BLOCK for this level so that, if it is not expanded
9996 later, there is a way to turn off the entire subtree of blocks that
9997 are contained in it. */
9998 keep_next_level ();
9999 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10000
10001 c_bindings_start_stmt_expr (c_switch_stack == NULL
10002 ? NULL
10003 : c_switch_stack->bindings);
325c3691
RH
10004
10005 /* Mark the current statement list as belonging to a statement list. */
10006 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10007
10008 return ret;
10009}
10010
c2255bc4
AH
10011/* LOC is the location of the compound statement to which this body
10012 belongs. */
10013
325c3691 10014tree
c2255bc4 10015c_finish_stmt_expr (location_t loc, tree body)
325c3691 10016{
3a5b9284 10017 tree last, type, tmp, val;
325c3691
RH
10018 tree *last_p;
10019
c2255bc4 10020 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10021
10022 c_bindings_end_stmt_expr (c_switch_stack == NULL
10023 ? NULL
10024 : c_switch_stack->bindings);
325c3691 10025
3a5b9284
RH
10026 /* Locate the last statement in BODY. See c_end_compound_stmt
10027 about always returning a BIND_EXPR. */
10028 last_p = &BIND_EXPR_BODY (body);
10029 last = BIND_EXPR_BODY (body);
10030
10031 continue_searching:
325c3691
RH
10032 if (TREE_CODE (last) == STATEMENT_LIST)
10033 {
3a5b9284
RH
10034 tree_stmt_iterator i;
10035
10036 /* This can happen with degenerate cases like ({ }). No value. */
10037 if (!TREE_SIDE_EFFECTS (last))
10038 return body;
10039
10040 /* If we're supposed to generate side effects warnings, process
10041 all of the statements except the last. */
27f33b15 10042 if (warn_unused_value)
325c3691 10043 {
3a5b9284 10044 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10045 {
10046 location_t tloc;
10047 tree t = tsi_stmt (i);
10048
10049 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10050 emit_side_effect_warnings (tloc, t);
10051 }
325c3691
RH
10052 }
10053 else
3a5b9284
RH
10054 i = tsi_last (last);
10055 last_p = tsi_stmt_ptr (i);
10056 last = *last_p;
325c3691
RH
10057 }
10058
3a5b9284
RH
10059 /* If the end of the list is exception related, then the list was split
10060 by a call to push_cleanup. Continue searching. */
10061 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10062 || TREE_CODE (last) == TRY_CATCH_EXPR)
10063 {
10064 last_p = &TREE_OPERAND (last, 0);
10065 last = *last_p;
10066 goto continue_searching;
10067 }
10068
26d8af35
JM
10069 if (last == error_mark_node)
10070 return last;
10071
3a5b9284
RH
10072 /* In the case that the BIND_EXPR is not necessary, return the
10073 expression out from inside it. */
26d8af35
JM
10074 if (last == BIND_EXPR_BODY (body)
10075 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10076 {
928c19bb
JM
10077 /* Even if this looks constant, do not allow it in a constant
10078 expression. */
e5a94231 10079 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10080 /* Do not warn if the return value of a statement expression is
10081 unused. */
928c19bb 10082 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10083 return last;
10084 }
325c3691
RH
10085
10086 /* Extract the type of said expression. */
10087 type = TREE_TYPE (last);
325c3691 10088
3a5b9284
RH
10089 /* If we're not returning a value at all, then the BIND_EXPR that
10090 we already have is a fine expression to return. */
10091 if (!type || VOID_TYPE_P (type))
10092 return body;
10093
10094 /* Now that we've located the expression containing the value, it seems
10095 silly to make voidify_wrapper_expr repeat the process. Create a
10096 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10097 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10098
10099 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10100 tree_expr_nonnegative_p giving up immediately. */
10101 val = last;
10102 if (TREE_CODE (val) == NOP_EXPR
10103 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10104 val = TREE_OPERAND (val, 0);
10105
53fb4de3 10106 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10107 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10108
c2255bc4
AH
10109 {
10110 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10111 SET_EXPR_LOCATION (t, loc);
10112 return t;
10113 }
325c3691
RH
10114}
10115\f
10116/* Begin and end compound statements. This is as simple as pushing
10117 and popping new statement lists from the tree. */
10118
10119tree
10120c_begin_compound_stmt (bool do_scope)
10121{
10122 tree stmt = push_stmt_list ();
10123 if (do_scope)
4dfa0342 10124 push_scope ();
325c3691
RH
10125 return stmt;
10126}
10127
c2255bc4
AH
10128/* End a compound statement. STMT is the statement. LOC is the
10129 location of the compound statement-- this is usually the location
10130 of the opening brace. */
10131
325c3691 10132tree
c2255bc4 10133c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10134{
10135 tree block = NULL;
10136
10137 if (do_scope)
10138 {
10139 if (c_dialect_objc ())
10140 objc_clear_super_receiver ();
10141 block = pop_scope ();
10142 }
10143
10144 stmt = pop_stmt_list (stmt);
c2255bc4 10145 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10146
10147 /* If this compound statement is nested immediately inside a statement
10148 expression, then force a BIND_EXPR to be created. Otherwise we'll
10149 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10150 STATEMENT_LISTs merge, and thus we can lose track of what statement
10151 was really last. */
38e01f9e 10152 if (building_stmt_list_p ()
325c3691
RH
10153 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10154 && TREE_CODE (stmt) != BIND_EXPR)
10155 {
53fb4de3 10156 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10157 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10158 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10159 }
10160
10161 return stmt;
10162}
5a508662
RH
10163
10164/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10165 when the current scope is exited. EH_ONLY is true when this is not
10166 meant to apply to normal control flow transfer. */
10167
10168void
c2255bc4 10169push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10170{
3a5b9284
RH
10171 enum tree_code code;
10172 tree stmt, list;
10173 bool stmt_expr;
10174
10175 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10176 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10177 add_stmt (stmt);
3a5b9284
RH
10178 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10179 list = push_stmt_list ();
10180 TREE_OPERAND (stmt, 0) = list;
10181 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10182}
325c3691 10183\f
3e4093b6
RS
10184/* Build a binary-operation expression without default conversions.
10185 CODE is the kind of expression to build.
ba47d38d 10186 LOCATION is the operator's location.
3e4093b6
RS
10187 This function differs from `build' in several ways:
10188 the data type of the result is computed and recorded in it,
10189 warnings are generated if arg data types are invalid,
10190 special handling for addition and subtraction of pointers is known,
10191 and some optimization is done (operations on narrow ints
10192 are done in the narrower type when that gives the same result).
10193 Constant folding is also done before the result is returned.
de520661 10194
3e4093b6
RS
10195 Note that the operands will never have enumeral types, or function
10196 or array types, because either they will have the default conversions
10197 performed or they have both just been converted to some other type in which
10198 the arithmetic is to be done. */
10199
10200tree
ba47d38d
AH
10201build_binary_op (location_t location, enum tree_code code,
10202 tree orig_op0, tree orig_op1, int convert_p)
de520661 10203{
8ce94e44
JM
10204 tree type0, type1, orig_type0, orig_type1;
10205 tree eptype;
3e4093b6
RS
10206 enum tree_code code0, code1;
10207 tree op0, op1;
c9f9eb5d 10208 tree ret = error_mark_node;
4de67c26 10209 const char *invalid_op_diag;
4d84fe7c 10210 bool op0_int_operands, op1_int_operands;
928c19bb 10211 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10212
3e4093b6
RS
10213 /* Expression code to give to the expression when it is built.
10214 Normally this is CODE, which is what the caller asked for,
10215 but in some special cases we change it. */
10216 enum tree_code resultcode = code;
8b6a5902 10217
3e4093b6
RS
10218 /* Data type in which the computation is to be performed.
10219 In the simplest cases this is the common type of the arguments. */
10220 tree result_type = NULL;
10221
8ce94e44
JM
10222 /* When the computation is in excess precision, the type of the
10223 final EXCESS_PRECISION_EXPR. */
2d2e923f 10224 tree semantic_result_type = NULL;
8ce94e44 10225
3e4093b6
RS
10226 /* Nonzero means operands have already been type-converted
10227 in whatever way is necessary.
10228 Zero means they need to be converted to RESULT_TYPE. */
10229 int converted = 0;
10230
10231 /* Nonzero means create the expression with this type, rather than
10232 RESULT_TYPE. */
10233 tree build_type = 0;
10234
10235 /* Nonzero means after finally constructing the expression
10236 convert it to this type. */
10237 tree final_type = 0;
10238
10239 /* Nonzero if this is an operation like MIN or MAX which can
10240 safely be computed in short if both args are promoted shorts.
10241 Also implies COMMON.
10242 -1 indicates a bitwise operation; this makes a difference
10243 in the exact conditions for when it is safe to do the operation
10244 in a narrower mode. */
10245 int shorten = 0;
10246
10247 /* Nonzero if this is a comparison operation;
10248 if both args are promoted shorts, compare the original shorts.
10249 Also implies COMMON. */
10250 int short_compare = 0;
10251
10252 /* Nonzero if this is a right-shift operation, which can be computed on the
10253 original short and then promoted if the operand is a promoted short. */
10254 int short_shift = 0;
10255
10256 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10257 int common = 0;
10258
58393038
ZL
10259 /* True means types are compatible as far as ObjC is concerned. */
10260 bool objc_ok;
10261
8ce94e44
JM
10262 /* True means this is an arithmetic operation that may need excess
10263 precision. */
10264 bool may_need_excess_precision;
10265
180f8dbb
JM
10266 /* True means this is a boolean operation that converts both its
10267 operands to truth-values. */
10268 bool boolean_op = false;
10269
de5a5fa1
MP
10270 /* Remember whether we're doing / or %. */
10271 bool doing_div_or_mod = false;
10272
10273 /* Remember whether we're doing << or >>. */
10274 bool doing_shift = false;
10275
10276 /* Tree holding instrumentation expression. */
10277 tree instrument_expr = NULL;
10278
ba47d38d
AH
10279 if (location == UNKNOWN_LOCATION)
10280 location = input_location;
10281
4d84fe7c
JM
10282 op0 = orig_op0;
10283 op1 = orig_op1;
10284
10285 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10286 if (op0_int_operands)
10287 op0 = remove_c_maybe_const_expr (op0);
10288 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10289 if (op1_int_operands)
10290 op1 = remove_c_maybe_const_expr (op1);
10291 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10292 if (int_operands)
10293 {
10294 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10295 && TREE_CODE (orig_op1) == INTEGER_CST);
10296 int_const = (int_const_or_overflow
10297 && !TREE_OVERFLOW (orig_op0)
10298 && !TREE_OVERFLOW (orig_op1));
10299 }
10300 else
10301 int_const = int_const_or_overflow = false;
10302
0e3a99ae 10303 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10304 if (convert_p
10305 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10306 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10307 {
4d84fe7c
JM
10308 op0 = default_conversion (op0);
10309 op1 = default_conversion (op1);
790e9490
RS
10310 }
10311
36536d79
BI
10312 /* When Cilk Plus is enabled and there are array notations inside op0, then
10313 we check to see if there are builtin array notation functions. If
10314 so, then we take on the type of the array notation inside it. */
b72271b9 10315 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10316 orig_type0 = type0 = find_correct_array_notation_type (op0);
10317 else
10318 orig_type0 = type0 = TREE_TYPE (op0);
10319
b72271b9 10320 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10321 orig_type1 = type1 = find_correct_array_notation_type (op1);
10322 else
10323 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10324
3e4093b6
RS
10325 /* The expression codes of the data types of the arguments tell us
10326 whether the arguments are integers, floating, pointers, etc. */
10327 code0 = TREE_CODE (type0);
10328 code1 = TREE_CODE (type1);
10329
10330 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10331 STRIP_TYPE_NOPS (op0);
10332 STRIP_TYPE_NOPS (op1);
10333
10334 /* If an error was already reported for one of the arguments,
10335 avoid reporting another error. */
10336
10337 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10338 return error_mark_node;
10339
4de67c26
JM
10340 if ((invalid_op_diag
10341 = targetm.invalid_binary_op (code, type0, type1)))
10342 {
ba47d38d 10343 error_at (location, invalid_op_diag);
4de67c26
JM
10344 return error_mark_node;
10345 }
10346
8ce94e44
JM
10347 switch (code)
10348 {
10349 case PLUS_EXPR:
10350 case MINUS_EXPR:
10351 case MULT_EXPR:
10352 case TRUNC_DIV_EXPR:
10353 case CEIL_DIV_EXPR:
10354 case FLOOR_DIV_EXPR:
10355 case ROUND_DIV_EXPR:
10356 case EXACT_DIV_EXPR:
10357 may_need_excess_precision = true;
10358 break;
10359 default:
10360 may_need_excess_precision = false;
10361 break;
10362 }
10363 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10364 {
10365 op0 = TREE_OPERAND (op0, 0);
10366 type0 = TREE_TYPE (op0);
10367 }
10368 else if (may_need_excess_precision
10369 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10370 {
10371 type0 = eptype;
10372 op0 = convert (eptype, op0);
10373 }
10374 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10375 {
10376 op1 = TREE_OPERAND (op1, 0);
10377 type1 = TREE_TYPE (op1);
10378 }
10379 else if (may_need_excess_precision
10380 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10381 {
10382 type1 = eptype;
10383 op1 = convert (eptype, op1);
10384 }
10385
58393038
ZL
10386 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10387
0e3a99ae
AS
10388 /* In case when one of the operands of the binary operation is
10389 a vector and another is a scalar -- convert scalar to vector. */
10390 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10391 {
a212e43f
MG
10392 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10393 true);
f90e8e2e 10394
0e3a99ae
AS
10395 switch (convert_flag)
10396 {
10397 case stv_error:
10398 return error_mark_node;
10399 case stv_firstarg:
10400 {
10401 bool maybe_const = true;
10402 tree sc;
10403 sc = c_fully_fold (op0, false, &maybe_const);
10404 sc = save_expr (sc);
10405 sc = convert (TREE_TYPE (type1), sc);
10406 op0 = build_vector_from_val (type1, sc);
10407 if (!maybe_const)
10408 op0 = c_wrap_maybe_const (op0, true);
10409 orig_type0 = type0 = TREE_TYPE (op0);
10410 code0 = TREE_CODE (type0);
10411 converted = 1;
10412 break;
10413 }
10414 case stv_secondarg:
10415 {
10416 bool maybe_const = true;
10417 tree sc;
10418 sc = c_fully_fold (op1, false, &maybe_const);
10419 sc = save_expr (sc);
10420 sc = convert (TREE_TYPE (type0), sc);
10421 op1 = build_vector_from_val (type0, sc);
10422 if (!maybe_const)
54b9f838 10423 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10424 orig_type1 = type1 = TREE_TYPE (op1);
10425 code1 = TREE_CODE (type1);
10426 converted = 1;
10427 break;
10428 }
10429 default:
10430 break;
10431 }
10432 }
10433
3e4093b6 10434 switch (code)
de520661 10435 {
3e4093b6
RS
10436 case PLUS_EXPR:
10437 /* Handle the pointer + int case. */
10438 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10439 {
db3927fb 10440 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10441 goto return_build_binary_op;
10442 }
3e4093b6 10443 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10444 {
db3927fb 10445 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10446 goto return_build_binary_op;
10447 }
fe67cf58 10448 else
3e4093b6
RS
10449 common = 1;
10450 break;
400fbf9f 10451
3e4093b6
RS
10452 case MINUS_EXPR:
10453 /* Subtraction of two similar pointers.
10454 We must subtract them as integers, then divide by object size. */
10455 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10456 && comp_target_types (location, type0, type1))
c9f9eb5d 10457 {
db3927fb 10458 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10459 goto return_build_binary_op;
10460 }
3e4093b6
RS
10461 /* Handle pointer minus int. Just like pointer plus int. */
10462 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10463 {
db3927fb 10464 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10465 goto return_build_binary_op;
10466 }
3e4093b6
RS
10467 else
10468 common = 1;
10469 break;
8b6a5902 10470
3e4093b6
RS
10471 case MULT_EXPR:
10472 common = 1;
10473 break;
10474
10475 case TRUNC_DIV_EXPR:
10476 case CEIL_DIV_EXPR:
10477 case FLOOR_DIV_EXPR:
10478 case ROUND_DIV_EXPR:
10479 case EXACT_DIV_EXPR:
de5a5fa1 10480 doing_div_or_mod = true;
c9f9eb5d 10481 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10482
10483 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10484 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10485 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10486 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10487 || code1 == FIXED_POINT_TYPE
3e4093b6 10488 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10489 {
5bed876a
AH
10490 enum tree_code tcode0 = code0, tcode1 = code1;
10491
3a021db2 10492 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10493 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10494 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10495 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10496
ab22c1fa
CF
10497 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10498 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10499 resultcode = RDIV_EXPR;
10500 else
10501 /* Although it would be tempting to shorten always here, that
10502 loses on some targets, since the modulo instruction is
10503 undefined if the quotient can't be represented in the
10504 computation mode. We shorten only if unsigned or if
10505 dividing by something we know != -1. */
8df83eae 10506 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10507 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10508 && !integer_all_onesp (op1)));
3e4093b6
RS
10509 common = 1;
10510 }
10511 break;
de520661 10512
3e4093b6 10513 case BIT_AND_EXPR:
3e4093b6
RS
10514 case BIT_IOR_EXPR:
10515 case BIT_XOR_EXPR:
10516 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10517 shorten = -1;
9ef0c8d9
AP
10518 /* Allow vector types which are not floating point types. */
10519 else if (code0 == VECTOR_TYPE
10520 && code1 == VECTOR_TYPE
10521 && !VECTOR_FLOAT_TYPE_P (type0)
10522 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10523 common = 1;
10524 break;
10525
10526 case TRUNC_MOD_EXPR:
10527 case FLOOR_MOD_EXPR:
de5a5fa1 10528 doing_div_or_mod = true;
c9f9eb5d 10529 warn_for_div_by_zero (location, op1);
de520661 10530
5cfd5d9b
AP
10531 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10532 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10533 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10534 common = 1;
10535 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10536 {
10537 /* Although it would be tempting to shorten always here, that loses
10538 on some targets, since the modulo instruction is undefined if the
10539 quotient can't be represented in the computation mode. We shorten
10540 only if unsigned or if dividing by something we know != -1. */
8df83eae 10541 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10542 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10543 && !integer_all_onesp (op1)));
3e4093b6
RS
10544 common = 1;
10545 }
10546 break;
de520661 10547
3e4093b6
RS
10548 case TRUTH_ANDIF_EXPR:
10549 case TRUTH_ORIF_EXPR:
10550 case TRUTH_AND_EXPR:
10551 case TRUTH_OR_EXPR:
10552 case TRUTH_XOR_EXPR:
10553 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10554 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10555 || code0 == FIXED_POINT_TYPE)
3e4093b6 10556 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10557 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10558 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10559 {
10560 /* Result of these operations is always an int,
10561 but that does not mean the operands should be
10562 converted to ints! */
10563 result_type = integer_type_node;
a27d595d
JM
10564 if (op0_int_operands)
10565 {
10566 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10567 op0 = remove_c_maybe_const_expr (op0);
10568 }
10569 else
10570 op0 = c_objc_common_truthvalue_conversion (location, op0);
10571 if (op1_int_operands)
10572 {
10573 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10574 op1 = remove_c_maybe_const_expr (op1);
10575 }
10576 else
10577 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10578 converted = 1;
180f8dbb 10579 boolean_op = true;
3e4093b6 10580 }
928c19bb
JM
10581 if (code == TRUTH_ANDIF_EXPR)
10582 {
10583 int_const_or_overflow = (int_operands
10584 && TREE_CODE (orig_op0) == INTEGER_CST
10585 && (op0 == truthvalue_false_node
10586 || TREE_CODE (orig_op1) == INTEGER_CST));
10587 int_const = (int_const_or_overflow
10588 && !TREE_OVERFLOW (orig_op0)
10589 && (op0 == truthvalue_false_node
10590 || !TREE_OVERFLOW (orig_op1)));
10591 }
10592 else if (code == TRUTH_ORIF_EXPR)
10593 {
10594 int_const_or_overflow = (int_operands
10595 && TREE_CODE (orig_op0) == INTEGER_CST
10596 && (op0 == truthvalue_true_node
10597 || TREE_CODE (orig_op1) == INTEGER_CST));
10598 int_const = (int_const_or_overflow
10599 && !TREE_OVERFLOW (orig_op0)
10600 && (op0 == truthvalue_true_node
10601 || !TREE_OVERFLOW (orig_op1)));
10602 }
3e4093b6 10603 break;
eba80994 10604
3e4093b6
RS
10605 /* Shift operations: result has same type as first operand;
10606 always convert second operand to int.
10607 Also set SHORT_SHIFT if shifting rightward. */
de520661 10608
3e4093b6 10609 case RSHIFT_EXPR:
f87bd04b
AS
10610 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10611 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10612 {
10613 result_type = type0;
10614 converted = 1;
10615 }
10616 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10617 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10618 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10619 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10620 {
10621 result_type = type0;
10622 converted = 1;
10623 }
10624 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10625 && code1 == INTEGER_TYPE)
3e4093b6 10626 {
de5a5fa1 10627 doing_shift = true;
928c19bb 10628 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10629 {
3e4093b6 10630 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10631 {
10632 int_const = false;
7d882b83 10633 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10634 warning_at (location, OPT_Wshift_count_negative,
10635 "right shift count is negative");
928c19bb 10636 }
3e4093b6 10637 else
bbb818c6 10638 {
3f75a254 10639 if (!integer_zerop (op1))
3e4093b6
RS
10640 short_shift = 1;
10641
10642 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10643 {
10644 int_const = false;
7d882b83 10645 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10646 warning_at (location, OPT_Wshift_count_overflow,
10647 "right shift count >= width of type");
928c19bb 10648 }
bbb818c6 10649 }
b62acd60 10650 }
de520661 10651
3e4093b6
RS
10652 /* Use the type of the value to be shifted. */
10653 result_type = type0;
3e4093b6
RS
10654 /* Avoid converting op1 to result_type later. */
10655 converted = 1;
400fbf9f 10656 }
3e4093b6 10657 break;
253b6b82 10658
3e4093b6 10659 case LSHIFT_EXPR:
f87bd04b
AS
10660 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10661 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10662 {
10663 result_type = type0;
10664 converted = 1;
10665 }
10666 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10667 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10668 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10669 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10670 {
10671 result_type = type0;
10672 converted = 1;
10673 }
10674 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10675 && code1 == INTEGER_TYPE)
3e4093b6 10676 {
de5a5fa1 10677 doing_shift = true;
0173bd2a
MP
10678 if (TREE_CODE (op0) == INTEGER_CST
10679 && tree_int_cst_sgn (op0) < 0)
10680 {
10681 /* Don't reject a left shift of a negative value in a context
10682 where a constant expression is needed in C90. */
10683 if (flag_isoc99)
10684 int_const = false;
10685 if (c_inhibit_evaluation_warnings == 0)
10686 warning_at (location, OPT_Wshift_negative_value,
10687 "left shift of negative value");
10688 }
928c19bb 10689 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10690 {
3e4093b6 10691 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10692 {
10693 int_const = false;
7d882b83 10694 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10695 warning_at (location, OPT_Wshift_count_negative,
10696 "left shift count is negative");
928c19bb 10697 }
de520661 10698
3e4093b6 10699 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10700 {
10701 int_const = false;
7d882b83 10702 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10703 warning_at (location, OPT_Wshift_count_overflow,
10704 "left shift count >= width of type");
928c19bb 10705 }
94ba5069 10706 }
de520661 10707
3e4093b6
RS
10708 /* Use the type of the value to be shifted. */
10709 result_type = type0;
3e4093b6
RS
10710 /* Avoid converting op1 to result_type later. */
10711 converted = 1;
400fbf9f 10712 }
3e4093b6 10713 break;
de520661 10714
3e4093b6
RS
10715 case EQ_EXPR:
10716 case NE_EXPR:
d246ab4f
AS
10717 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10718 {
10719 tree intt;
0af94e6f 10720 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10721 {
10722 error_at (location, "comparing vectors with different "
10723 "element types");
10724 return error_mark_node;
10725 }
10726
10727 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10728 {
10729 error_at (location, "comparing vectors with different "
10730 "number of elements");
10731 return error_mark_node;
10732 }
10733
10734 /* Always construct signed integer vector type. */
10735 intt = c_common_type_for_size (GET_MODE_BITSIZE
10736 (TYPE_MODE (TREE_TYPE (type0))), 0);
10737 result_type = build_opaque_vector_type (intt,
10738 TYPE_VECTOR_SUBPARTS (type0));
10739 converted = 1;
10740 break;
10741 }
ae311566 10742 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10743 warning_at (location,
10744 OPT_Wfloat_equal,
10745 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10746 /* Result of comparison is always int,
10747 but don't convert the args to int! */
10748 build_type = integer_type_node;
10749 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10750 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10751 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10752 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10753 short_compare = 1;
637f1455
SZ
10754 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10755 {
10756 if (TREE_CODE (op0) == ADDR_EXPR
10757 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10758 {
10759 if (code == EQ_EXPR)
10760 warning_at (location,
10761 OPT_Waddress,
10762 "the comparison will always evaluate as %<false%> "
10763 "for the address of %qD will never be NULL",
10764 TREE_OPERAND (op0, 0));
10765 else
10766 warning_at (location,
10767 OPT_Waddress,
10768 "the comparison will always evaluate as %<true%> "
10769 "for the address of %qD will never be NULL",
10770 TREE_OPERAND (op0, 0));
10771 }
10772 result_type = type0;
10773 }
10774 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10775 {
10776 if (TREE_CODE (op1) == ADDR_EXPR
10777 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10778 {
10779 if (code == EQ_EXPR)
10780 warning_at (location,
f90e8e2e 10781 OPT_Waddress,
637f1455
SZ
10782 "the comparison will always evaluate as %<false%> "
10783 "for the address of %qD will never be NULL",
10784 TREE_OPERAND (op1, 0));
10785 else
10786 warning_at (location,
10787 OPT_Waddress,
10788 "the comparison will always evaluate as %<true%> "
10789 "for the address of %qD will never be NULL",
10790 TREE_OPERAND (op1, 0));
10791 }
10792 result_type = type1;
10793 }
3e4093b6
RS
10794 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10795 {
10796 tree tt0 = TREE_TYPE (type0);
10797 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10798 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10799 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10800 addr_space_t as_common = ADDR_SPACE_GENERIC;
10801
3e4093b6
RS
10802 /* Anything compares with void *. void * compares with anything.
10803 Otherwise, the targets must be compatible
10804 and both must be object or both incomplete. */
744aa42f 10805 if (comp_target_types (location, type0, type1))
10bc1b1b 10806 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10807 else if (!addr_space_superset (as0, as1, &as_common))
10808 {
10809 error_at (location, "comparison of pointers to "
10810 "disjoint address spaces");
10811 return error_mark_node;
10812 }
267bac10 10813 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10814 {
36c5e70a 10815 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10816 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10817 "comparison of %<void *%> with function pointer");
ee2990e7 10818 }
267bac10 10819 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10820 {
36c5e70a 10821 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10822 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10823 "comparison of %<void *%> with function pointer");
e6834654 10824 }
3e4093b6 10825 else
58393038
ZL
10826 /* Avoid warning about the volatile ObjC EH puts on decls. */
10827 if (!objc_ok)
ba47d38d 10828 pedwarn (location, 0,
509c9d60 10829 "comparison of distinct pointer types lacks a cast");
e6834654 10830
3e4093b6 10831 if (result_type == NULL_TREE)
36c5e70a
BE
10832 {
10833 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10834 result_type = build_pointer_type
10835 (build_qualified_type (void_type_node, qual));
10836 }
e6834654 10837 }
3e4093b6 10838 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10839 {
3e4093b6 10840 result_type = type0;
ba47d38d 10841 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10842 }
3e4093b6 10843 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10844 {
3e4093b6 10845 result_type = type1;
ba47d38d 10846 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10847 }
04159acf
MP
10848 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10849 || truth_value_p (TREE_CODE (orig_op0)))
10850 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10851 || truth_value_p (TREE_CODE (orig_op1))))
10852 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10853 break;
8b6a5902 10854
3e4093b6
RS
10855 case LE_EXPR:
10856 case GE_EXPR:
10857 case LT_EXPR:
10858 case GT_EXPR:
d246ab4f
AS
10859 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10860 {
10861 tree intt;
0af94e6f 10862 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10863 {
10864 error_at (location, "comparing vectors with different "
10865 "element types");
10866 return error_mark_node;
10867 }
10868
10869 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10870 {
10871 error_at (location, "comparing vectors with different "
10872 "number of elements");
10873 return error_mark_node;
10874 }
10875
10876 /* Always construct signed integer vector type. */
10877 intt = c_common_type_for_size (GET_MODE_BITSIZE
10878 (TYPE_MODE (TREE_TYPE (type0))), 0);
10879 result_type = build_opaque_vector_type (intt,
10880 TYPE_VECTOR_SUBPARTS (type0));
10881 converted = 1;
10882 break;
10883 }
3e4093b6 10884 build_type = integer_type_node;
ab22c1fa
CF
10885 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10886 || code0 == FIXED_POINT_TYPE)
10887 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10888 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10889 short_compare = 1;
10890 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10891 {
36c5e70a
BE
10892 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10893 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10894 addr_space_t as_common;
10895
744aa42f 10896 if (comp_target_types (location, type0, type1))
3e4093b6 10897 {
10bc1b1b 10898 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10899 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10900 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10901 pedwarn (location, 0,
509c9d60 10902 "comparison of complete and incomplete pointers");
fcf73884 10903 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10904 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10905 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10906 else if (null_pointer_constant_p (orig_op0)
10907 || null_pointer_constant_p (orig_op1))
10908 warning_at (location, OPT_Wextra,
10909 "ordered comparison of pointer with null pointer");
10910
3e4093b6 10911 }
36c5e70a
BE
10912 else if (!addr_space_superset (as0, as1, &as_common))
10913 {
10914 error_at (location, "comparison of pointers to "
10915 "disjoint address spaces");
10916 return error_mark_node;
10917 }
3e4093b6
RS
10918 else
10919 {
36c5e70a
BE
10920 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10921 result_type = build_pointer_type
10922 (build_qualified_type (void_type_node, qual));
ba47d38d 10923 pedwarn (location, 0,
509c9d60 10924 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10925 }
10926 }
6aa3c60d 10927 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10928 {
10929 result_type = type0;
fcf73884 10930 if (pedantic)
c1771a20 10931 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10932 "ordered comparison of pointer with integer zero");
10933 else if (extra_warnings)
ba47d38d 10934 warning_at (location, OPT_Wextra,
d42ba3b8 10935 "ordered comparison of pointer with integer zero");
3e4093b6 10936 }
6aa3c60d 10937 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10938 {
10939 result_type = type1;
d42ba3b8 10940 if (pedantic)
c1771a20 10941 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10942 "ordered comparison of pointer with integer zero");
10943 else if (extra_warnings)
10944 warning_at (location, OPT_Wextra,
10945 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10946 }
10947 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10948 {
10949 result_type = type0;
ba47d38d 10950 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10951 }
10952 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10953 {
10954 result_type = type1;
ba47d38d 10955 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10956 }
04159acf
MP
10957 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10958 || truth_value_p (TREE_CODE (orig_op0)))
10959 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10960 || truth_value_p (TREE_CODE (orig_op1))))
10961 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10962 break;
64094f6a 10963
3e4093b6 10964 default:
37b2f290 10965 gcc_unreachable ();
c9fe6f9f 10966 }
8f17b5c5 10967
e57e265b
PB
10968 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10969 return error_mark_node;
10970
5bed876a
AH
10971 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10972 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10973 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10974 {
ba47d38d 10975 binary_op_error (location, code, type0, type1);
5bed876a
AH
10976 return error_mark_node;
10977 }
10978
3e4093b6 10979 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10980 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10981 &&
10982 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10983 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10984 {
2ca862e9
JM
10985 bool first_complex = (code0 == COMPLEX_TYPE);
10986 bool second_complex = (code1 == COMPLEX_TYPE);
10987 int none_complex = (!first_complex && !second_complex);
39b726dd 10988
3e4093b6 10989 if (shorten || common || short_compare)
3bf6bfcc
JJ
10990 {
10991 result_type = c_common_type (type0, type1);
0a0b3574
MM
10992 do_warn_double_promotion (result_type, type0, type1,
10993 "implicit conversion from %qT to %qT "
10994 "to match other operand of binary "
10995 "expression",
10996 location);
3bf6bfcc
JJ
10997 if (result_type == error_mark_node)
10998 return error_mark_node;
10999 }
400fbf9f 11000
2ca862e9
JM
11001 if (first_complex != second_complex
11002 && (code == PLUS_EXPR
11003 || code == MINUS_EXPR
11004 || code == MULT_EXPR
11005 || (code == TRUNC_DIV_EXPR && first_complex))
11006 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11007 && flag_signed_zeros)
11008 {
11009 /* An operation on mixed real/complex operands must be
11010 handled specially, but the language-independent code can
11011 more easily optimize the plain complex arithmetic if
11012 -fno-signed-zeros. */
11013 tree real_type = TREE_TYPE (result_type);
11014 tree real, imag;
11015 if (type0 != orig_type0 || type1 != orig_type1)
11016 {
11017 gcc_assert (may_need_excess_precision && common);
2d2e923f 11018 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11019 }
11020 if (first_complex)
11021 {
11022 if (TREE_TYPE (op0) != result_type)
68fca595 11023 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11024 if (TREE_TYPE (op1) != real_type)
68fca595 11025 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11026 }
11027 else
11028 {
11029 if (TREE_TYPE (op0) != real_type)
68fca595 11030 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11031 if (TREE_TYPE (op1) != result_type)
68fca595 11032 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11033 }
11034 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11035 return error_mark_node;
11036 if (first_complex)
11037 {
11038 op0 = c_save_expr (op0);
11039 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11040 op0, 1);
11041 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11042 op0, 1);
11043 switch (code)
11044 {
11045 case MULT_EXPR:
11046 case TRUNC_DIV_EXPR:
c4603e7c 11047 op1 = c_save_expr (op1);
2ca862e9
JM
11048 imag = build2 (resultcode, real_type, imag, op1);
11049 /* Fall through. */
11050 case PLUS_EXPR:
11051 case MINUS_EXPR:
11052 real = build2 (resultcode, real_type, real, op1);
11053 break;
11054 default:
11055 gcc_unreachable();
11056 }
11057 }
11058 else
11059 {
11060 op1 = c_save_expr (op1);
11061 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11062 op1, 1);
11063 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11064 op1, 1);
11065 switch (code)
11066 {
11067 case MULT_EXPR:
c4603e7c 11068 op0 = c_save_expr (op0);
2ca862e9
JM
11069 imag = build2 (resultcode, real_type, op0, imag);
11070 /* Fall through. */
11071 case PLUS_EXPR:
11072 real = build2 (resultcode, real_type, op0, real);
11073 break;
11074 case MINUS_EXPR:
11075 real = build2 (resultcode, real_type, op0, real);
11076 imag = build1 (NEGATE_EXPR, real_type, imag);
11077 break;
11078 default:
11079 gcc_unreachable();
11080 }
11081 }
11082 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11083 goto return_build_binary_op;
11084 }
11085
3e4093b6
RS
11086 /* For certain operations (which identify themselves by shorten != 0)
11087 if both args were extended from the same smaller type,
11088 do the arithmetic in that type and then extend.
400fbf9f 11089
3e4093b6
RS
11090 shorten !=0 and !=1 indicates a bitwise operation.
11091 For them, this optimization is safe only if
11092 both args are zero-extended or both are sign-extended.
11093 Otherwise, we might change the result.
11094 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11095 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11096
3e4093b6
RS
11097 if (shorten && none_complex)
11098 {
3e4093b6 11099 final_type = result_type;
b8698a0f 11100 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11101 shorten == -1);
3e4093b6 11102 }
88a3dbc1 11103
3e4093b6 11104 /* Shifts can be shortened if shifting right. */
2f6e4e97 11105
3e4093b6
RS
11106 if (short_shift)
11107 {
11108 int unsigned_arg;
11109 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11110
3e4093b6 11111 final_type = result_type;
abe80e6d 11112
3e4093b6 11113 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11114 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11115
3e4093b6 11116 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11117 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11118 /* We can shorten only if the shift count is less than the
11119 number of bits in the smaller type size. */
11120 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11121 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11122 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11123 {
11124 /* Do an unsigned shift if the operand was zero-extended. */
11125 result_type
11126 = c_common_signed_or_unsigned_type (unsigned_arg,
11127 TREE_TYPE (arg0));
11128 /* Convert value-to-be-shifted to that type. */
11129 if (TREE_TYPE (op0) != result_type)
11130 op0 = convert (result_type, op0);
11131 converted = 1;
abe80e6d 11132 }
88a3dbc1
RK
11133 }
11134
3e4093b6
RS
11135 /* Comparison operations are shortened too but differently.
11136 They identify themselves by setting short_compare = 1. */
56cb9733 11137
3e4093b6
RS
11138 if (short_compare)
11139 {
11140 /* Don't write &op0, etc., because that would prevent op0
11141 from being kept in a register.
11142 Instead, make copies of the our local variables and
11143 pass the copies by reference, then copy them back afterward. */
11144 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11145 enum tree_code xresultcode = resultcode;
11146 tree val
393e8e8b
MP
11147 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11148 &xresultcode);
8f17b5c5 11149
3e4093b6 11150 if (val != 0)
c9f9eb5d
AH
11151 {
11152 ret = val;
11153 goto return_build_binary_op;
11154 }
8f17b5c5 11155
3e4093b6
RS
11156 op0 = xop0, op1 = xop1;
11157 converted = 1;
11158 resultcode = xresultcode;
8f17b5c5 11159
7d882b83 11160 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11161 {
11162 bool op0_maybe_const = true;
11163 bool op1_maybe_const = true;
11164 tree orig_op0_folded, orig_op1_folded;
11165
11166 if (in_late_binary_op)
11167 {
11168 orig_op0_folded = orig_op0;
11169 orig_op1_folded = orig_op1;
11170 }
11171 else
11172 {
11173 /* Fold for the sake of possible warnings, as in
11174 build_conditional_expr. This requires the
11175 "original" values to be folded, not just op0 and
11176 op1. */
f5178456 11177 c_inhibit_evaluation_warnings++;
928c19bb
JM
11178 op0 = c_fully_fold (op0, require_constant_value,
11179 &op0_maybe_const);
11180 op1 = c_fully_fold (op1, require_constant_value,
11181 &op1_maybe_const);
f5178456 11182 c_inhibit_evaluation_warnings--;
928c19bb
JM
11183 orig_op0_folded = c_fully_fold (orig_op0,
11184 require_constant_value,
11185 NULL);
11186 orig_op1_folded = c_fully_fold (orig_op1,
11187 require_constant_value,
11188 NULL);
11189 }
11190
11191 if (warn_sign_compare)
11192 warn_for_sign_compare (location, orig_op0_folded,
11193 orig_op1_folded, op0, op1,
11194 result_type, resultcode);
5c2f94b4 11195 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11196 {
11197 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11198 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11199 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11200 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11201 }
3e4093b6 11202 }
2ad1815d 11203 }
64094f6a 11204 }
64094f6a 11205
3e4093b6
RS
11206 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11207 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11208 Then the expression will be built.
11209 It will be given type FINAL_TYPE if that is nonzero;
11210 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11211
3e4093b6
RS
11212 if (!result_type)
11213 {
ba47d38d 11214 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11215 return error_mark_node;
11216 }
400fbf9f 11217
3e4093b6 11218 if (build_type == NULL_TREE)
8ce94e44
JM
11219 {
11220 build_type = result_type;
180f8dbb
JM
11221 if ((type0 != orig_type0 || type1 != orig_type1)
11222 && !boolean_op)
8ce94e44
JM
11223 {
11224 gcc_assert (may_need_excess_precision && common);
2d2e923f 11225 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11226 }
11227 }
400fbf9f 11228
2d2e923f
MLI
11229 if (!converted)
11230 {
68fca595
MP
11231 op0 = ep_convert_and_check (location, result_type, op0,
11232 semantic_result_type);
11233 op1 = ep_convert_and_check (location, result_type, op1,
11234 semantic_result_type);
2d2e923f
MLI
11235
11236 /* This can happen if one operand has a vector type, and the other
11237 has a different type. */
11238 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11239 return error_mark_node;
11240 }
11241
f8ed5150
MP
11242 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11243 | SANITIZE_FLOAT_DIVIDE))
802ac282 11244 && do_ubsan_in_current_function ()
de5a5fa1
MP
11245 && (doing_div_or_mod || doing_shift))
11246 {
11247 /* OP0 and/or OP1 might have side-effects. */
11248 op0 = c_save_expr (op0);
11249 op1 = c_save_expr (op1);
11250 op0 = c_fully_fold (op0, false, NULL);
11251 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11252 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11253 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11254 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11255 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11256 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11257 }
11258
c9f9eb5d 11259 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11260 if (int_const_or_overflow)
11261 ret = (require_constant_value
db3927fb
AH
11262 ? fold_build2_initializer_loc (location, resultcode, build_type,
11263 op0, op1)
11264 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11265 else
11266 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11267 if (final_type != 0)
11268 ret = convert (final_type, ret);
11269
11270 return_build_binary_op:
11271 gcc_assert (ret != error_mark_node);
928c19bb
JM
11272 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11273 ret = (int_operands
11274 ? note_integer_operands (ret)
11275 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11276 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11277 && !in_late_binary_op)
11278 ret = note_integer_operands (ret);
2d2e923f
MLI
11279 if (semantic_result_type)
11280 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11281 protected_set_expr_location (ret, location);
de5a5fa1 11282
a24d975c 11283 if (instrument_expr != NULL)
de5a5fa1
MP
11284 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11285 instrument_expr, ret);
11286
c9f9eb5d 11287 return ret;
400fbf9f 11288}
85498824
JM
11289
11290
11291/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11292 purpose. LOCATION is the source location for the expression. */
85498824
JM
11293
11294tree
ba47d38d 11295c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11296{
928c19bb
JM
11297 bool int_const, int_operands;
11298
85498824
JM
11299 switch (TREE_CODE (TREE_TYPE (expr)))
11300 {
11301 case ARRAY_TYPE:
ba47d38d 11302 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11303 return error_mark_node;
11304
11305 case RECORD_TYPE:
ba47d38d 11306 error_at (location, "used struct type value where scalar is required");
85498824
JM
11307 return error_mark_node;
11308
11309 case UNION_TYPE:
ba47d38d 11310 error_at (location, "used union type value where scalar is required");
85498824
JM
11311 return error_mark_node;
11312
04af8788
NP
11313 case VOID_TYPE:
11314 error_at (location, "void value not ignored as it ought to be");
11315 return error_mark_node;
11316
46bdb9cf
JM
11317 case FUNCTION_TYPE:
11318 gcc_unreachable ();
11319
d246ab4f
AS
11320 case VECTOR_TYPE:
11321 error_at (location, "used vector type where scalar is required");
11322 return error_mark_node;
11323
85498824
JM
11324 default:
11325 break;
11326 }
11327
928c19bb
JM
11328 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11329 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11330 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11331 {
11332 expr = remove_c_maybe_const_expr (expr);
11333 expr = build2 (NE_EXPR, integer_type_node, expr,
11334 convert (TREE_TYPE (expr), integer_zero_node));
11335 expr = note_integer_operands (expr);
11336 }
11337 else
11338 /* ??? Should we also give an error for vectors rather than leaving
11339 those to give errors later? */
11340 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11341
11342 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11343 {
11344 if (TREE_OVERFLOW (expr))
11345 return expr;
11346 else
11347 return note_integer_operands (expr);
11348 }
11349 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11350 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11351 return expr;
85498824 11352}
73f397d4
JM
11353\f
11354
11355/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11356 required. */
11357
11358tree
51eed280 11359c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11360{
11361 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11362 {
11363 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11364 /* Executing a compound literal inside a function reinitializes
11365 it. */
11366 if (!TREE_STATIC (decl))
11367 *se = true;
11368 return decl;
11369 }
11370 else
11371 return expr;
11372}
953ff289 11373\f
41dbbb37
TS
11374/* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11375 statement. LOC is the location of the OACC_PARALLEL. */
11376
11377tree
11378c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11379{
11380 tree stmt;
11381
11382 block = c_end_compound_stmt (loc, block, true);
11383
11384 stmt = make_node (OACC_PARALLEL);
11385 TREE_TYPE (stmt) = void_type_node;
11386 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11387 OACC_PARALLEL_BODY (stmt) = block;
11388 SET_EXPR_LOCATION (stmt, loc);
11389
11390 return add_stmt (stmt);
11391}
11392
11393/* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11394 statement. LOC is the location of the OACC_KERNELS. */
11395
11396tree
11397c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11398{
11399 tree stmt;
11400
11401 block = c_end_compound_stmt (loc, block, true);
11402
11403 stmt = make_node (OACC_KERNELS);
11404 TREE_TYPE (stmt) = void_type_node;
11405 OACC_KERNELS_CLAUSES (stmt) = clauses;
11406 OACC_KERNELS_BODY (stmt) = block;
11407 SET_EXPR_LOCATION (stmt, loc);
11408
11409 return add_stmt (stmt);
11410}
11411
11412/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11413 statement. LOC is the location of the OACC_DATA. */
11414
11415tree
11416c_finish_oacc_data (location_t loc, tree clauses, tree block)
11417{
11418 tree stmt;
11419
11420 block = c_end_compound_stmt (loc, block, true);
11421
11422 stmt = make_node (OACC_DATA);
11423 TREE_TYPE (stmt) = void_type_node;
11424 OACC_DATA_CLAUSES (stmt) = clauses;
11425 OACC_DATA_BODY (stmt) = block;
11426 SET_EXPR_LOCATION (stmt, loc);
11427
11428 return add_stmt (stmt);
11429}
11430
c0220ea4 11431/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11432
11433tree
11434c_begin_omp_parallel (void)
11435{
11436 tree block;
11437
11438 keep_next_level ();
11439 block = c_begin_compound_stmt (true);
11440
11441 return block;
11442}
11443
c2255bc4
AH
11444/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11445 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11446
953ff289 11447tree
c2255bc4 11448c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11449{
11450 tree stmt;
11451
c2255bc4 11452 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11453
11454 stmt = make_node (OMP_PARALLEL);
11455 TREE_TYPE (stmt) = void_type_node;
11456 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11457 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11458 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11459
11460 return add_stmt (stmt);
11461}
11462
a68ab351
JJ
11463/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11464
11465tree
11466c_begin_omp_task (void)
11467{
11468 tree block;
11469
11470 keep_next_level ();
11471 block = c_begin_compound_stmt (true);
11472
11473 return block;
11474}
11475
c2255bc4
AH
11476/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11477 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11478
11479tree
c2255bc4 11480c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11481{
11482 tree stmt;
11483
c2255bc4 11484 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11485
11486 stmt = make_node (OMP_TASK);
11487 TREE_TYPE (stmt) = void_type_node;
11488 OMP_TASK_CLAUSES (stmt) = clauses;
11489 OMP_TASK_BODY (stmt) = block;
c2255bc4 11490 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11491
11492 return add_stmt (stmt);
11493}
11494
acf0174b
JJ
11495/* Generate GOMP_cancel call for #pragma omp cancel. */
11496
11497void
11498c_finish_omp_cancel (location_t loc, tree clauses)
11499{
11500 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11501 int mask = 0;
11502 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11503 mask = 1;
11504 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11505 mask = 2;
11506 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11507 mask = 4;
11508 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11509 mask = 8;
11510 else
11511 {
11512 error_at (loc, "%<#pragma omp cancel must specify one of "
11513 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11514 "clauses");
11515 return;
11516 }
11517 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11518 if (ifc != NULL_TREE)
11519 {
11520 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11521 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11522 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11523 build_zero_cst (type));
11524 }
11525 else
11526 ifc = boolean_true_node;
11527 tree stmt = build_call_expr_loc (loc, fn, 2,
11528 build_int_cst (integer_type_node, mask),
11529 ifc);
11530 add_stmt (stmt);
11531}
11532
11533/* Generate GOMP_cancellation_point call for
11534 #pragma omp cancellation point. */
11535
11536void
11537c_finish_omp_cancellation_point (location_t loc, tree clauses)
11538{
11539 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11540 int mask = 0;
11541 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11542 mask = 1;
11543 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11544 mask = 2;
11545 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11546 mask = 4;
11547 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11548 mask = 8;
11549 else
11550 {
11551 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11552 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11553 "clauses");
11554 return;
11555 }
11556 tree stmt = build_call_expr_loc (loc, fn, 1,
11557 build_int_cst (integer_type_node, mask));
11558 add_stmt (stmt);
11559}
11560
11561/* Helper function for handle_omp_array_sections. Called recursively
11562 to handle multiple array-section-subscripts. C is the clause,
11563 T current expression (initially OMP_CLAUSE_DECL), which is either
11564 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11565 expression if specified, TREE_VALUE length expression if specified,
11566 TREE_CHAIN is what it has been specified after, or some decl.
11567 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11568 set to true if any of the array-section-subscript could have length
11569 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11570 first array-section-subscript which is known not to have length
11571 of one. Given say:
11572 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11573 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11574 all are or may have length of 1, array-section-subscript [:2] is the
11575 first one knonwn not to have length 1. For array-section-subscript
11576 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11577 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11578 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11579 case though, as some lengths could be zero. */
11580
11581static tree
11582handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11583 bool &maybe_zero_len, unsigned int &first_non_one)
11584{
11585 tree ret, low_bound, length, type;
11586 if (TREE_CODE (t) != TREE_LIST)
11587 {
7a0ca710 11588 if (error_operand_p (t))
acf0174b 11589 return error_mark_node;
0ae9bd27 11590 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
11591 {
11592 if (DECL_P (t))
11593 error_at (OMP_CLAUSE_LOCATION (c),
11594 "%qD is not a variable in %qs clause", t,
11595 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11596 else
11597 error_at (OMP_CLAUSE_LOCATION (c),
11598 "%qE is not a variable in %qs clause", t,
11599 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11600 return error_mark_node;
11601 }
11602 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0ae9bd27 11603 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
11604 {
11605 error_at (OMP_CLAUSE_LOCATION (c),
11606 "%qD is threadprivate variable in %qs clause", t,
11607 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11608 return error_mark_node;
11609 }
11610 return t;
11611 }
11612
11613 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11614 maybe_zero_len, first_non_one);
11615 if (ret == error_mark_node || ret == NULL_TREE)
11616 return ret;
11617
11618 type = TREE_TYPE (ret);
11619 low_bound = TREE_PURPOSE (t);
11620 length = TREE_VALUE (t);
11621
11622 if (low_bound == error_mark_node || length == error_mark_node)
11623 return error_mark_node;
11624
11625 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11626 {
11627 error_at (OMP_CLAUSE_LOCATION (c),
11628 "low bound %qE of array section does not have integral type",
11629 low_bound);
11630 return error_mark_node;
11631 }
11632 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11633 {
11634 error_at (OMP_CLAUSE_LOCATION (c),
11635 "length %qE of array section does not have integral type",
11636 length);
11637 return error_mark_node;
11638 }
11639 if (low_bound
11640 && TREE_CODE (low_bound) == INTEGER_CST
11641 && TYPE_PRECISION (TREE_TYPE (low_bound))
11642 > TYPE_PRECISION (sizetype))
11643 low_bound = fold_convert (sizetype, low_bound);
11644 if (length
11645 && TREE_CODE (length) == INTEGER_CST
11646 && TYPE_PRECISION (TREE_TYPE (length))
11647 > TYPE_PRECISION (sizetype))
11648 length = fold_convert (sizetype, length);
11649 if (low_bound == NULL_TREE)
11650 low_bound = integer_zero_node;
11651
11652 if (length != NULL_TREE)
11653 {
11654 if (!integer_nonzerop (length))
11655 maybe_zero_len = true;
11656 if (first_non_one == types.length ()
11657 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11658 first_non_one++;
11659 }
11660 if (TREE_CODE (type) == ARRAY_TYPE)
11661 {
11662 if (length == NULL_TREE
11663 && (TYPE_DOMAIN (type) == NULL_TREE
11664 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11665 {
11666 error_at (OMP_CLAUSE_LOCATION (c),
11667 "for unknown bound array type length expression must "
11668 "be specified");
11669 return error_mark_node;
11670 }
11671 if (TREE_CODE (low_bound) == INTEGER_CST
11672 && tree_int_cst_sgn (low_bound) == -1)
11673 {
11674 error_at (OMP_CLAUSE_LOCATION (c),
11675 "negative low bound in array section in %qs clause",
11676 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11677 return error_mark_node;
11678 }
11679 if (length != NULL_TREE
11680 && TREE_CODE (length) == INTEGER_CST
11681 && tree_int_cst_sgn (length) == -1)
11682 {
11683 error_at (OMP_CLAUSE_LOCATION (c),
11684 "negative length in array section in %qs clause",
11685 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11686 return error_mark_node;
11687 }
11688 if (TYPE_DOMAIN (type)
11689 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11690 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11691 == INTEGER_CST)
11692 {
11693 tree size = size_binop (PLUS_EXPR,
11694 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11695 size_one_node);
11696 if (TREE_CODE (low_bound) == INTEGER_CST)
11697 {
11698 if (tree_int_cst_lt (size, low_bound))
11699 {
11700 error_at (OMP_CLAUSE_LOCATION (c),
11701 "low bound %qE above array section size "
11702 "in %qs clause", low_bound,
11703 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11704 return error_mark_node;
11705 }
11706 if (tree_int_cst_equal (size, low_bound))
11707 maybe_zero_len = true;
11708 else if (length == NULL_TREE
11709 && first_non_one == types.length ()
11710 && tree_int_cst_equal
11711 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11712 low_bound))
11713 first_non_one++;
11714 }
11715 else if (length == NULL_TREE)
11716 {
11717 maybe_zero_len = true;
11718 if (first_non_one == types.length ())
11719 first_non_one++;
11720 }
11721 if (length && TREE_CODE (length) == INTEGER_CST)
11722 {
11723 if (tree_int_cst_lt (size, length))
11724 {
11725 error_at (OMP_CLAUSE_LOCATION (c),
11726 "length %qE above array section size "
11727 "in %qs clause", length,
11728 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11729 return error_mark_node;
11730 }
11731 if (TREE_CODE (low_bound) == INTEGER_CST)
11732 {
11733 tree lbpluslen
11734 = size_binop (PLUS_EXPR,
11735 fold_convert (sizetype, low_bound),
11736 fold_convert (sizetype, length));
11737 if (TREE_CODE (lbpluslen) == INTEGER_CST
11738 && tree_int_cst_lt (size, lbpluslen))
11739 {
11740 error_at (OMP_CLAUSE_LOCATION (c),
11741 "high bound %qE above array section size "
11742 "in %qs clause", lbpluslen,
11743 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11744 return error_mark_node;
11745 }
11746 }
11747 }
11748 }
11749 else if (length == NULL_TREE)
11750 {
11751 maybe_zero_len = true;
11752 if (first_non_one == types.length ())
11753 first_non_one++;
11754 }
11755
11756 /* For [lb:] we will need to evaluate lb more than once. */
11757 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11758 {
11759 tree lb = c_save_expr (low_bound);
11760 if (lb != low_bound)
11761 {
11762 TREE_PURPOSE (t) = lb;
11763 low_bound = lb;
11764 }
11765 }
11766 }
11767 else if (TREE_CODE (type) == POINTER_TYPE)
11768 {
11769 if (length == NULL_TREE)
11770 {
11771 error_at (OMP_CLAUSE_LOCATION (c),
11772 "for pointer type length expression must be specified");
11773 return error_mark_node;
11774 }
11775 /* If there is a pointer type anywhere but in the very first
11776 array-section-subscript, the array section can't be contiguous. */
11777 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11778 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11779 {
11780 error_at (OMP_CLAUSE_LOCATION (c),
11781 "array section is not contiguous in %qs clause",
11782 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11783 return error_mark_node;
11784 }
11785 }
11786 else
11787 {
11788 error_at (OMP_CLAUSE_LOCATION (c),
11789 "%qE does not have pointer or array type", ret);
11790 return error_mark_node;
11791 }
11792 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11793 types.safe_push (TREE_TYPE (ret));
11794 /* We will need to evaluate lb more than once. */
11795 tree lb = c_save_expr (low_bound);
11796 if (lb != low_bound)
11797 {
11798 TREE_PURPOSE (t) = lb;
11799 low_bound = lb;
11800 }
11801 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11802 return ret;
11803}
11804
11805/* Handle array sections for clause C. */
11806
11807static bool
11808handle_omp_array_sections (tree c)
11809{
11810 bool maybe_zero_len = false;
11811 unsigned int first_non_one = 0;
11812 vec<tree> types = vNULL;
11813 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11814 maybe_zero_len, first_non_one);
11815 if (first == error_mark_node)
11816 {
11817 types.release ();
11818 return true;
11819 }
11820 if (first == NULL_TREE)
11821 {
11822 types.release ();
11823 return false;
11824 }
11825 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11826 {
11827 tree t = OMP_CLAUSE_DECL (c);
11828 tree tem = NULL_TREE;
11829 types.release ();
11830 /* Need to evaluate side effects in the length expressions
11831 if any. */
11832 while (TREE_CODE (t) == TREE_LIST)
11833 {
11834 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11835 {
11836 if (tem == NULL_TREE)
11837 tem = TREE_VALUE (t);
11838 else
11839 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11840 TREE_VALUE (t), tem);
11841 }
11842 t = TREE_CHAIN (t);
11843 }
11844 if (tem)
11845 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11846 first = c_fully_fold (first, false, NULL);
11847 OMP_CLAUSE_DECL (c) = first;
11848 }
11849 else
11850 {
11851 unsigned int num = types.length (), i;
11852 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11853 tree condition = NULL_TREE;
11854
11855 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11856 maybe_zero_len = true;
11857
11858 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11859 t = TREE_CHAIN (t))
11860 {
11861 tree low_bound = TREE_PURPOSE (t);
11862 tree length = TREE_VALUE (t);
11863
11864 i--;
11865 if (low_bound
11866 && TREE_CODE (low_bound) == INTEGER_CST
11867 && TYPE_PRECISION (TREE_TYPE (low_bound))
11868 > TYPE_PRECISION (sizetype))
11869 low_bound = fold_convert (sizetype, low_bound);
11870 if (length
11871 && TREE_CODE (length) == INTEGER_CST
11872 && TYPE_PRECISION (TREE_TYPE (length))
11873 > TYPE_PRECISION (sizetype))
11874 length = fold_convert (sizetype, length);
11875 if (low_bound == NULL_TREE)
11876 low_bound = integer_zero_node;
11877 if (!maybe_zero_len && i > first_non_one)
11878 {
11879 if (integer_nonzerop (low_bound))
11880 goto do_warn_noncontiguous;
11881 if (length != NULL_TREE
11882 && TREE_CODE (length) == INTEGER_CST
11883 && TYPE_DOMAIN (types[i])
11884 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11885 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11886 == INTEGER_CST)
11887 {
11888 tree size;
11889 size = size_binop (PLUS_EXPR,
11890 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11891 size_one_node);
11892 if (!tree_int_cst_equal (length, size))
11893 {
11894 do_warn_noncontiguous:
11895 error_at (OMP_CLAUSE_LOCATION (c),
11896 "array section is not contiguous in %qs "
11897 "clause",
11898 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11899 types.release ();
11900 return true;
11901 }
11902 }
11903 if (length != NULL_TREE
11904 && TREE_SIDE_EFFECTS (length))
11905 {
11906 if (side_effects == NULL_TREE)
11907 side_effects = length;
11908 else
11909 side_effects = build2 (COMPOUND_EXPR,
11910 TREE_TYPE (side_effects),
11911 length, side_effects);
11912 }
11913 }
11914 else
11915 {
11916 tree l;
11917
11918 if (i > first_non_one && length && integer_nonzerop (length))
11919 continue;
11920 if (length)
11921 l = fold_convert (sizetype, length);
11922 else
11923 {
11924 l = size_binop (PLUS_EXPR,
11925 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11926 size_one_node);
11927 l = size_binop (MINUS_EXPR, l,
11928 fold_convert (sizetype, low_bound));
11929 }
11930 if (i > first_non_one)
11931 {
11932 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11933 size_zero_node);
11934 if (condition == NULL_TREE)
11935 condition = l;
11936 else
11937 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11938 l, condition);
11939 }
11940 else if (size == NULL_TREE)
11941 {
11942 size = size_in_bytes (TREE_TYPE (types[i]));
11943 size = size_binop (MULT_EXPR, size, l);
11944 if (condition)
11945 size = fold_build3 (COND_EXPR, sizetype, condition,
11946 size, size_zero_node);
11947 }
11948 else
11949 size = size_binop (MULT_EXPR, size, l);
11950 }
11951 }
11952 types.release ();
11953 if (side_effects)
11954 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11955 first = c_fully_fold (first, false, NULL);
11956 OMP_CLAUSE_DECL (c) = first;
11957 if (size)
11958 size = c_fully_fold (size, false, NULL);
11959 OMP_CLAUSE_SIZE (c) = size;
11960 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11961 return false;
41dbbb37 11962 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
acf0174b 11963 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
41dbbb37 11964 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
acf0174b
JJ
11965 if (!c_mark_addressable (t))
11966 return false;
11967 OMP_CLAUSE_DECL (c2) = t;
11968 t = build_fold_addr_expr (first);
11969 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11970 tree ptr = OMP_CLAUSE_DECL (c2);
11971 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11972 ptr = build_fold_addr_expr (ptr);
11973 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11974 ptrdiff_type_node, t,
11975 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11976 ptrdiff_type_node, ptr));
11977 t = c_fully_fold (t, false, NULL);
11978 OMP_CLAUSE_SIZE (c2) = t;
11979 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11980 OMP_CLAUSE_CHAIN (c) = c2;
11981 }
11982 return false;
11983}
11984
11985/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11986 an inline call. But, remap
11987 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11988 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11989
11990static tree
11991c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11992 tree decl, tree placeholder)
11993{
11994 copy_body_data id;
b787e7a2 11995 hash_map<tree, tree> decl_map;
acf0174b 11996
b787e7a2
TS
11997 decl_map.put (omp_decl1, placeholder);
11998 decl_map.put (omp_decl2, decl);
acf0174b
JJ
11999 memset (&id, 0, sizeof (id));
12000 id.src_fn = DECL_CONTEXT (omp_decl1);
12001 id.dst_fn = current_function_decl;
12002 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12003 id.decl_map = &decl_map;
acf0174b
JJ
12004
12005 id.copy_decl = copy_decl_no_change;
12006 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12007 id.transform_new_cfg = true;
12008 id.transform_return_to_modify = false;
12009 id.transform_lang_insert_block = NULL;
12010 id.eh_lp_nr = 0;
12011 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12012 return stmt;
12013}
12014
12015/* Helper function of c_finish_omp_clauses, called via walk_tree.
12016 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12017
12018static tree
12019c_find_omp_placeholder_r (tree *tp, int *, void *data)
12020{
12021 if (*tp == (tree) data)
12022 return *tp;
12023 return NULL_TREE;
12024}
12025
41dbbb37 12026/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12027 Remove any elements from the list that are invalid. */
12028
12029tree
12030c_finish_omp_clauses (tree clauses)
12031{
12032 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 12033 bitmap_head aligned_head;
f3316c6d 12034 tree c, t, *pc;
acf0174b
JJ
12035 bool branch_seen = false;
12036 bool copyprivate_seen = false;
12037 tree *nowait_clause = NULL;
953ff289
DN
12038
12039 bitmap_obstack_initialize (NULL);
12040 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12041 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12042 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12043 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
12044
12045 for (pc = &clauses, c = clauses; c ; c = *pc)
12046 {
12047 bool remove = false;
12048 bool need_complete = false;
12049 bool need_implicitly_determined = false;
12050
aaf46ef9 12051 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12052 {
12053 case OMP_CLAUSE_SHARED:
953ff289
DN
12054 need_implicitly_determined = true;
12055 goto check_dup_generic;
12056
12057 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12058 need_complete = true;
12059 need_implicitly_determined = true;
12060 goto check_dup_generic;
12061
12062 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12063 need_implicitly_determined = true;
12064 t = OMP_CLAUSE_DECL (c);
acf0174b 12065 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
12066 && (FLOAT_TYPE_P (TREE_TYPE (t))
12067 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
12068 {
12069 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12070 const char *r_name = NULL;
12071
12072 switch (r_code)
12073 {
12074 case PLUS_EXPR:
12075 case MULT_EXPR:
12076 case MINUS_EXPR:
652fea39 12077 break;
20906c66 12078 case MIN_EXPR:
652fea39
JJ
12079 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12080 r_name = "min";
12081 break;
20906c66 12082 case MAX_EXPR:
652fea39
JJ
12083 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12084 r_name = "max";
953ff289
DN
12085 break;
12086 case BIT_AND_EXPR:
12087 r_name = "&";
12088 break;
12089 case BIT_XOR_EXPR:
12090 r_name = "^";
12091 break;
12092 case BIT_IOR_EXPR:
12093 r_name = "|";
12094 break;
12095 case TRUTH_ANDIF_EXPR:
652fea39
JJ
12096 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12097 r_name = "&&";
953ff289
DN
12098 break;
12099 case TRUTH_ORIF_EXPR:
652fea39
JJ
12100 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12101 r_name = "||";
953ff289
DN
12102 break;
12103 default:
12104 gcc_unreachable ();
12105 }
12106 if (r_name)
12107 {
c2255bc4
AH
12108 error_at (OMP_CLAUSE_LOCATION (c),
12109 "%qE has invalid type for %<reduction(%s)%>",
12110 t, r_name);
953ff289 12111 remove = true;
ee1d5a02 12112 break;
953ff289
DN
12113 }
12114 }
acf0174b
JJ
12115 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12116 {
12117 error_at (OMP_CLAUSE_LOCATION (c),
12118 "user defined reduction not found for %qD", t);
12119 remove = true;
ee1d5a02 12120 break;
acf0174b
JJ
12121 }
12122 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12123 {
12124 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12125 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12126 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12127 VAR_DECL, NULL_TREE, type);
12128 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12129 DECL_ARTIFICIAL (placeholder) = 1;
12130 DECL_IGNORED_P (placeholder) = 1;
12131 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12132 c_mark_addressable (placeholder);
12133 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12134 c_mark_addressable (OMP_CLAUSE_DECL (c));
12135 OMP_CLAUSE_REDUCTION_MERGE (c)
12136 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12137 TREE_VEC_ELT (list, 0),
12138 TREE_VEC_ELT (list, 1),
12139 OMP_CLAUSE_DECL (c), placeholder);
12140 OMP_CLAUSE_REDUCTION_MERGE (c)
12141 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12142 void_type_node, NULL_TREE,
12143 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12144 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12145 if (TREE_VEC_LENGTH (list) == 6)
12146 {
12147 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12148 c_mark_addressable (OMP_CLAUSE_DECL (c));
12149 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12150 c_mark_addressable (placeholder);
12151 tree init = TREE_VEC_ELT (list, 5);
12152 if (init == error_mark_node)
12153 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12154 OMP_CLAUSE_REDUCTION_INIT (c)
12155 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12156 TREE_VEC_ELT (list, 3),
12157 OMP_CLAUSE_DECL (c), placeholder);
12158 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12159 OMP_CLAUSE_REDUCTION_INIT (c)
12160 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12161 OMP_CLAUSE_REDUCTION_INIT (c));
12162 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12163 c_find_omp_placeholder_r,
12164 placeholder, NULL))
12165 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12166 }
12167 else
12168 {
12169 tree init;
12170 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12171 init = build_constructor (TREE_TYPE (t), NULL);
12172 else
12173 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12174 OMP_CLAUSE_REDUCTION_INIT (c)
12175 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12176 }
12177 OMP_CLAUSE_REDUCTION_INIT (c)
12178 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12179 void_type_node, NULL_TREE,
12180 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12181 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12182 }
953ff289
DN
12183 goto check_dup_generic;
12184
12185 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12186 copyprivate_seen = true;
12187 if (nowait_clause)
12188 {
12189 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12190 "%<nowait%> clause must not be used together "
12191 "with %<copyprivate%>");
12192 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12193 nowait_clause = NULL;
12194 }
953ff289
DN
12195 goto check_dup_generic;
12196
12197 case OMP_CLAUSE_COPYIN:
953ff289 12198 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12199 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12200 {
c2255bc4
AH
12201 error_at (OMP_CLAUSE_LOCATION (c),
12202 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12203 remove = true;
ee1d5a02 12204 break;
953ff289
DN
12205 }
12206 goto check_dup_generic;
12207
acf0174b
JJ
12208 case OMP_CLAUSE_LINEAR:
12209 t = OMP_CLAUSE_DECL (c);
12210 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12211 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12212 {
12213 error_at (OMP_CLAUSE_LOCATION (c),
12214 "linear clause applied to non-integral non-pointer "
12215 "variable with type %qT", TREE_TYPE (t));
12216 remove = true;
12217 break;
12218 }
12219 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12220 {
12221 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12222 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12223 OMP_CLAUSE_DECL (c), s);
12224 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12225 sizetype, s, OMP_CLAUSE_DECL (c));
12226 if (s == error_mark_node)
12227 s = size_one_node;
12228 OMP_CLAUSE_LINEAR_STEP (c) = s;
12229 }
da6f124d
JJ
12230 else
12231 OMP_CLAUSE_LINEAR_STEP (c)
12232 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12233 goto check_dup_generic;
12234
953ff289
DN
12235 check_dup_generic:
12236 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12237 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12238 {
c2255bc4 12239 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12240 "%qE is not a variable in clause %qs", t,
12241 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12242 remove = true;
12243 }
12244 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12245 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12246 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12247 {
c2255bc4
AH
12248 error_at (OMP_CLAUSE_LOCATION (c),
12249 "%qE appears more than once in data clauses", t);
953ff289
DN
12250 remove = true;
12251 }
12252 else
12253 bitmap_set_bit (&generic_head, DECL_UID (t));
12254 break;
12255
12256 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12257 t = OMP_CLAUSE_DECL (c);
12258 need_complete = true;
12259 need_implicitly_determined = true;
0ae9bd27 12260 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12261 {
c2255bc4
AH
12262 error_at (OMP_CLAUSE_LOCATION (c),
12263 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12264 remove = true;
12265 }
12266 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12267 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12268 {
c2255bc4
AH
12269 error_at (OMP_CLAUSE_LOCATION (c),
12270 "%qE appears more than once in data clauses", t);
953ff289
DN
12271 remove = true;
12272 }
12273 else
12274 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12275 break;
12276
12277 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12278 t = OMP_CLAUSE_DECL (c);
12279 need_complete = true;
12280 need_implicitly_determined = true;
0ae9bd27 12281 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12282 {
c2255bc4
AH
12283 error_at (OMP_CLAUSE_LOCATION (c),
12284 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12285 remove = true;
12286 }
12287 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12288 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12289 {
c2255bc4
AH
12290 error_at (OMP_CLAUSE_LOCATION (c),
12291 "%qE appears more than once in data clauses", t);
953ff289
DN
12292 remove = true;
12293 }
12294 else
12295 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12296 break;
12297
acf0174b
JJ
12298 case OMP_CLAUSE_ALIGNED:
12299 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12300 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12301 {
12302 error_at (OMP_CLAUSE_LOCATION (c),
12303 "%qE is not a variable in %<aligned%> clause", t);
12304 remove = true;
12305 }
5a9785fb
JJ
12306 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12307 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12308 {
12309 error_at (OMP_CLAUSE_LOCATION (c),
12310 "%qE in %<aligned%> clause is neither a pointer nor "
12311 "an array", t);
12312 remove = true;
12313 }
acf0174b
JJ
12314 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12315 {
12316 error_at (OMP_CLAUSE_LOCATION (c),
12317 "%qE appears more than once in %<aligned%> clauses",
12318 t);
12319 remove = true;
12320 }
12321 else
12322 bitmap_set_bit (&aligned_head, DECL_UID (t));
12323 break;
12324
12325 case OMP_CLAUSE_DEPEND:
12326 t = OMP_CLAUSE_DECL (c);
12327 if (TREE_CODE (t) == TREE_LIST)
12328 {
12329 if (handle_omp_array_sections (c))
12330 remove = true;
12331 break;
12332 }
12333 if (t == error_mark_node)
12334 remove = true;
0ae9bd27 12335 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12336 {
12337 error_at (OMP_CLAUSE_LOCATION (c),
12338 "%qE is not a variable in %<depend%> clause", t);
12339 remove = true;
12340 }
12341 else if (!c_mark_addressable (t))
12342 remove = true;
12343 break;
12344
12345 case OMP_CLAUSE_MAP:
12346 case OMP_CLAUSE_TO:
12347 case OMP_CLAUSE_FROM:
41dbbb37 12348 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12349 t = OMP_CLAUSE_DECL (c);
12350 if (TREE_CODE (t) == TREE_LIST)
12351 {
12352 if (handle_omp_array_sections (c))
12353 remove = true;
12354 else
12355 {
12356 t = OMP_CLAUSE_DECL (c);
b17a8b07 12357 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12358 {
12359 error_at (OMP_CLAUSE_LOCATION (c),
12360 "array section does not have mappable type "
12361 "in %qs clause",
12362 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12363 remove = true;
12364 }
12365 }
12366 break;
12367 }
12368 if (t == error_mark_node)
12369 remove = true;
0ae9bd27 12370 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12371 {
12372 error_at (OMP_CLAUSE_LOCATION (c),
12373 "%qE is not a variable in %qs clause", t,
12374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12375 remove = true;
12376 }
0ae9bd27 12377 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12378 {
12379 error_at (OMP_CLAUSE_LOCATION (c),
12380 "%qD is threadprivate variable in %qs clause", t,
12381 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12382 remove = true;
12383 }
12384 else if (!c_mark_addressable (t))
12385 remove = true;
b17a8b07 12386 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37
TS
12387 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12388 || (OMP_CLAUSE_MAP_KIND (c)
12389 == GOMP_MAP_FORCE_DEVICEPTR)))
b17a8b07 12390 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12391 {
12392 error_at (OMP_CLAUSE_LOCATION (c),
12393 "%qD does not have a mappable type in %qs clause", t,
12394 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12395 remove = true;
12396 }
12397 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12398 {
12399 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12400 error ("%qD appears more than once in motion clauses", t);
12401 else
12402 error ("%qD appears more than once in map clauses", t);
12403 remove = true;
12404 }
12405 else
12406 bitmap_set_bit (&generic_head, DECL_UID (t));
12407 break;
12408
12409 case OMP_CLAUSE_UNIFORM:
12410 t = OMP_CLAUSE_DECL (c);
12411 if (TREE_CODE (t) != PARM_DECL)
12412 {
12413 if (DECL_P (t))
12414 error_at (OMP_CLAUSE_LOCATION (c),
12415 "%qD is not an argument in %<uniform%> clause", t);
12416 else
12417 error_at (OMP_CLAUSE_LOCATION (c),
12418 "%qE is not an argument in %<uniform%> clause", t);
12419 remove = true;
ee1d5a02 12420 break;
acf0174b 12421 }
ee1d5a02 12422 goto check_dup_generic;
acf0174b
JJ
12423
12424 case OMP_CLAUSE_NOWAIT:
12425 if (copyprivate_seen)
12426 {
12427 error_at (OMP_CLAUSE_LOCATION (c),
12428 "%<nowait%> clause must not be used together "
12429 "with %<copyprivate%>");
12430 remove = true;
12431 break;
12432 }
12433 nowait_clause = pc;
12434 pc = &OMP_CLAUSE_CHAIN (c);
12435 continue;
12436
953ff289
DN
12437 case OMP_CLAUSE_IF:
12438 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12439 case OMP_CLAUSE_NUM_TEAMS:
12440 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12441 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12442 case OMP_CLAUSE_ORDERED:
12443 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12444 case OMP_CLAUSE_UNTIED:
12445 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12446 case OMP_CLAUSE_FINAL:
12447 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12448 case OMP_CLAUSE_SAFELEN:
12449 case OMP_CLAUSE_SIMDLEN:
12450 case OMP_CLAUSE_DEVICE:
12451 case OMP_CLAUSE_DIST_SCHEDULE:
12452 case OMP_CLAUSE_PARALLEL:
12453 case OMP_CLAUSE_FOR:
12454 case OMP_CLAUSE_SECTIONS:
12455 case OMP_CLAUSE_TASKGROUP:
12456 case OMP_CLAUSE_PROC_BIND:
9a771876 12457 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
12458 case OMP_CLAUSE_NUM_GANGS:
12459 case OMP_CLAUSE_NUM_WORKERS:
12460 case OMP_CLAUSE_VECTOR_LENGTH:
12461 case OMP_CLAUSE_ASYNC:
12462 case OMP_CLAUSE_WAIT:
12463 case OMP_CLAUSE_AUTO:
12464 case OMP_CLAUSE_SEQ:
12465 case OMP_CLAUSE_GANG:
12466 case OMP_CLAUSE_WORKER:
12467 case OMP_CLAUSE_VECTOR:
acf0174b
JJ
12468 pc = &OMP_CLAUSE_CHAIN (c);
12469 continue;
12470
12471 case OMP_CLAUSE_INBRANCH:
12472 case OMP_CLAUSE_NOTINBRANCH:
12473 if (branch_seen)
12474 {
12475 error_at (OMP_CLAUSE_LOCATION (c),
12476 "%<inbranch%> clause is incompatible with "
12477 "%<notinbranch%>");
12478 remove = true;
12479 break;
12480 }
12481 branch_seen = true;
953ff289
DN
12482 pc = &OMP_CLAUSE_CHAIN (c);
12483 continue;
12484
12485 default:
12486 gcc_unreachable ();
12487 }
12488
12489 if (!remove)
12490 {
12491 t = OMP_CLAUSE_DECL (c);
12492
12493 if (need_complete)
12494 {
12495 t = require_complete_type (t);
12496 if (t == error_mark_node)
12497 remove = true;
12498 }
12499
12500 if (need_implicitly_determined)
12501 {
12502 const char *share_name = NULL;
12503
0ae9bd27 12504 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
12505 share_name = "threadprivate";
12506 else switch (c_omp_predetermined_sharing (t))
12507 {
12508 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12509 break;
12510 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12511 /* const vars may be specified in firstprivate clause. */
12512 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12513 && TREE_READONLY (t))
12514 break;
953ff289
DN
12515 share_name = "shared";
12516 break;
12517 case OMP_CLAUSE_DEFAULT_PRIVATE:
12518 share_name = "private";
12519 break;
12520 default:
12521 gcc_unreachable ();
12522 }
12523 if (share_name)
12524 {
c2255bc4
AH
12525 error_at (OMP_CLAUSE_LOCATION (c),
12526 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12527 t, share_name,
12528 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12529 remove = true;
12530 }
12531 }
12532 }
12533
12534 if (remove)
12535 *pc = OMP_CLAUSE_CHAIN (c);
12536 else
12537 pc = &OMP_CLAUSE_CHAIN (c);
12538 }
12539
12540 bitmap_obstack_release (NULL);
12541 return clauses;
12542}
9ae165a0 12543
0a35513e
AH
12544/* Create a transaction node. */
12545
12546tree
12547c_finish_transaction (location_t loc, tree block, int flags)
12548{
12549 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12550 if (flags & TM_STMT_ATTR_OUTER)
12551 TRANSACTION_EXPR_OUTER (stmt) = 1;
12552 if (flags & TM_STMT_ATTR_RELAXED)
12553 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12554 return add_stmt (stmt);
12555}
12556
9ae165a0
DG
12557/* Make a variant type in the proper way for C/C++, propagating qualifiers
12558 down to the element type of an array. */
12559
12560tree
12561c_build_qualified_type (tree type, int type_quals)
12562{
12563 if (type == error_mark_node)
12564 return type;
12565
12566 if (TREE_CODE (type) == ARRAY_TYPE)
12567 {
12568 tree t;
12569 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12570 type_quals);
12571
12572 /* See if we already have an identically qualified type. */
12573 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12574 {
12575 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12576 && TYPE_NAME (t) == TYPE_NAME (type)
12577 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12578 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12579 TYPE_ATTRIBUTES (type)))
12580 break;
12581 }
12582 if (!t)
12583 {
12584 tree domain = TYPE_DOMAIN (type);
12585
12586 t = build_variant_type_copy (type);
12587 TREE_TYPE (t) = element_type;
12588
12589 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12590 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12591 SET_TYPE_STRUCTURAL_EQUALITY (t);
12592 else if (TYPE_CANONICAL (element_type) != element_type
12593 || (domain && TYPE_CANONICAL (domain) != domain))
12594 {
b8698a0f 12595 tree unqualified_canon
9ae165a0 12596 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12597 domain? TYPE_CANONICAL (domain)
9ae165a0 12598 : NULL_TREE);
b8698a0f 12599 TYPE_CANONICAL (t)
9ae165a0
DG
12600 = c_build_qualified_type (unqualified_canon, type_quals);
12601 }
12602 else
12603 TYPE_CANONICAL (t) = t;
12604 }
12605 return t;
12606 }
12607
12608 /* A restrict-qualified pointer type must be a pointer to object or
12609 incomplete type. Note that the use of POINTER_TYPE_P also allows
12610 REFERENCE_TYPEs, which is appropriate for C++. */
12611 if ((type_quals & TYPE_QUAL_RESTRICT)
12612 && (!POINTER_TYPE_P (type)
12613 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12614 {
12615 error ("invalid use of %<restrict%>");
12616 type_quals &= ~TYPE_QUAL_RESTRICT;
12617 }
12618
12619 return build_qualified_type (type, type_quals);
12620}
72b5577d
ILT
12621
12622/* Build a VA_ARG_EXPR for the C parser. */
12623
12624tree
c2255bc4 12625c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d 12626{
ec3fba51
MP
12627 if (error_operand_p (type))
12628 return error_mark_node;
12629 else if (!COMPLETE_TYPE_P (type))
4e81b788 12630 {
ec3fba51
MP
12631 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12632 "type %qT", type);
4e81b788
MP
12633 return error_mark_node;
12634 }
ec3fba51
MP
12635 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12636 warning_at (loc, OPT_Wc___compat,
12637 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12638 return build_va_arg (loc, expr, type);
72b5577d 12639}
acf0174b
JJ
12640
12641/* Return truthvalue of whether T1 is the same tree structure as T2.
12642 Return 1 if they are the same. Return 0 if they are different. */
12643
12644bool
12645c_tree_equal (tree t1, tree t2)
12646{
12647 enum tree_code code1, code2;
12648
12649 if (t1 == t2)
12650 return true;
12651 if (!t1 || !t2)
12652 return false;
12653
12654 for (code1 = TREE_CODE (t1);
12655 CONVERT_EXPR_CODE_P (code1)
12656 || code1 == NON_LVALUE_EXPR;
12657 code1 = TREE_CODE (t1))
12658 t1 = TREE_OPERAND (t1, 0);
12659 for (code2 = TREE_CODE (t2);
12660 CONVERT_EXPR_CODE_P (code2)
12661 || code2 == NON_LVALUE_EXPR;
12662 code2 = TREE_CODE (t2))
12663 t2 = TREE_OPERAND (t2, 0);
12664
12665 /* They might have become equal now. */
12666 if (t1 == t2)
12667 return true;
12668
12669 if (code1 != code2)
12670 return false;
12671
12672 switch (code1)
12673 {
12674 case INTEGER_CST:
807e902e 12675 return wi::eq_p (t1, t2);
acf0174b
JJ
12676
12677 case REAL_CST:
12678 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12679
12680 case STRING_CST:
12681 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12682 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12683 TREE_STRING_LENGTH (t1));
12684
12685 case FIXED_CST:
12686 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12687 TREE_FIXED_CST (t2));
12688
12689 case COMPLEX_CST:
12690 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12691 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12692
12693 case VECTOR_CST:
12694 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12695
12696 case CONSTRUCTOR:
12697 /* We need to do this when determining whether or not two
12698 non-type pointer to member function template arguments
12699 are the same. */
12700 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12701 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12702 return false;
12703 {
12704 tree field, value;
12705 unsigned int i;
12706 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12707 {
12708 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12709 if (!c_tree_equal (field, elt2->index)
12710 || !c_tree_equal (value, elt2->value))
12711 return false;
12712 }
12713 }
12714 return true;
12715
12716 case TREE_LIST:
12717 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12718 return false;
12719 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12720 return false;
12721 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12722
12723 case SAVE_EXPR:
12724 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12725
12726 case CALL_EXPR:
12727 {
12728 tree arg1, arg2;
12729 call_expr_arg_iterator iter1, iter2;
12730 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12731 return false;
12732 for (arg1 = first_call_expr_arg (t1, &iter1),
12733 arg2 = first_call_expr_arg (t2, &iter2);
12734 arg1 && arg2;
12735 arg1 = next_call_expr_arg (&iter1),
12736 arg2 = next_call_expr_arg (&iter2))
12737 if (!c_tree_equal (arg1, arg2))
12738 return false;
12739 if (arg1 || arg2)
12740 return false;
12741 return true;
12742 }
12743
12744 case TARGET_EXPR:
12745 {
12746 tree o1 = TREE_OPERAND (t1, 0);
12747 tree o2 = TREE_OPERAND (t2, 0);
12748
12749 /* Special case: if either target is an unallocated VAR_DECL,
12750 it means that it's going to be unified with whatever the
12751 TARGET_EXPR is really supposed to initialize, so treat it
12752 as being equivalent to anything. */
0ae9bd27 12753 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
12754 && !DECL_RTL_SET_P (o1))
12755 /*Nop*/;
0ae9bd27 12756 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
12757 && !DECL_RTL_SET_P (o2))
12758 /*Nop*/;
12759 else if (!c_tree_equal (o1, o2))
12760 return false;
12761
12762 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12763 }
12764
12765 case COMPONENT_REF:
12766 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12767 return false;
12768 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12769
12770 case PARM_DECL:
12771 case VAR_DECL:
12772 case CONST_DECL:
12773 case FIELD_DECL:
12774 case FUNCTION_DECL:
12775 case IDENTIFIER_NODE:
12776 case SSA_NAME:
12777 return false;
12778
12779 case TREE_VEC:
12780 {
12781 unsigned ix;
12782 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12783 return false;
12784 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12785 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12786 TREE_VEC_ELT (t2, ix)))
12787 return false;
12788 return true;
12789 }
12790
12791 default:
12792 break;
12793 }
12794
12795 switch (TREE_CODE_CLASS (code1))
12796 {
12797 case tcc_unary:
12798 case tcc_binary:
12799 case tcc_comparison:
12800 case tcc_expression:
12801 case tcc_vl_exp:
12802 case tcc_reference:
12803 case tcc_statement:
12804 {
12805 int i, n = TREE_OPERAND_LENGTH (t1);
12806
12807 switch (code1)
12808 {
12809 case PREINCREMENT_EXPR:
12810 case PREDECREMENT_EXPR:
12811 case POSTINCREMENT_EXPR:
12812 case POSTDECREMENT_EXPR:
12813 n = 1;
12814 break;
12815 case ARRAY_REF:
12816 n = 2;
12817 break;
12818 default:
12819 break;
12820 }
12821
12822 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12823 && n != TREE_OPERAND_LENGTH (t2))
12824 return false;
12825
12826 for (i = 0; i < n; ++i)
12827 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12828 return false;
12829
12830 return true;
12831 }
12832
12833 case tcc_type:
12834 return comptypes (t1, t2);
12835 default:
12836 gcc_unreachable ();
12837 }
12838 /* We can get here with --disable-checking. */
12839 return false;
12840}
12893402
BI
12841
12842/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12843 spawn-helper and BODY is the newly created body for FNDECL. */
12844
12845void
12846cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12847{
12848 tree list = alloc_stmt_list ();
12849 tree frame = make_cilk_frame (fndecl);
12850 tree dtor = create_cilk_function_exit (frame, false, true);
12851 add_local_decl (cfun, frame);
12852
12853 DECL_SAVED_TREE (fndecl) = list;
12854 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12855 frame);
12856 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12857 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12858
12859 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12860 append_to_statement_list (detach_expr, &body_list);
12861
12862 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12863 body = fold_build_cleanup_point_expr (void_type_node, body);
12864
12865 append_to_statement_list (body, &body_list);
12866 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12867 body_list, dtor), &list);
12868}