]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
gcov-tool.c (do_merge): Refactore to remove int ret.
[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
MC
30#include "hash-set.h"
31#include "vec.h"
32#include "symtab.h"
33#include "input.h"
34#include "alias.h"
35#include "double-int.h"
36#include "machmode.h"
37#include "inchash.h"
38#include "real.h"
39#include "fixed-value.h"
400fbf9f 40#include "tree.h"
40e23961 41#include "fold-const.h"
d8a2d370
DN
42#include "stor-layout.h"
43#include "trans-mem.h"
44#include "varasm.h"
45#include "stmt.h"
e57e265b 46#include "langhooks.h"
400fbf9f 47#include "c-tree.h"
29cc57cf 48#include "c-lang.h"
400fbf9f 49#include "flags.h"
ab87f8c8 50#include "intl.h"
672a6f42 51#include "target.h"
325c3691 52#include "tree-iterator.h"
6662d794 53#include "bitmap.h"
60393bbc
AM
54#include "predict.h"
55#include "vec.h"
56#include "hashtab.h"
57#include "hash-set.h"
58#include "machmode.h"
59#include "hard-reg-set.h"
60#include "input.h"
61#include "function.h"
2fb9a547 62#include "gimple-expr.h"
45b0be94 63#include "gimplify.h"
acf0174b 64#include "tree-inline.h"
0645c1a2 65#include "omp-low.h"
61d3ce20 66#include "c-family/c-objc.h"
a212e43f 67#include "c-family/c-common.h"
de5a5fa1 68#include "c-family/c-ubsan.h"
12893402 69#include "cilk.h"
807e902e 70#include "wide-int.h"
41dbbb37 71#include "gomp-constants.h"
325c3691 72
2ac2f164
JM
73/* Possible cases of implicit bad conversions. Used to select
74 diagnostic messages in convert_for_assignment. */
75enum impl_conv {
76 ic_argpass,
77 ic_assign,
78 ic_init,
79 ic_return
80};
81
bc4b653b
JM
82/* The level of nesting inside "__alignof__". */
83int in_alignof;
84
85/* The level of nesting inside "sizeof". */
86int in_sizeof;
87
88/* The level of nesting inside "typeof". */
89int in_typeof;
400fbf9f 90
1a4049e7
JJ
91/* The argument of last parsed sizeof expression, only to be tested
92 if expr.original_code == SIZEOF_EXPR. */
93tree c_last_sizeof_arg;
94
9bac5cbb
G
95/* Nonzero if we might need to print a "missing braces around
96 initializer" message within this initializer. */
97static int found_missing_braces;
103b7b17 98
bf730f15
RS
99static int require_constant_value;
100static int require_constant_elements;
101
58f9752a 102static bool null_pointer_constant_p (const_tree);
f55ade6e 103static tree qualify_type (tree, tree);
dc5027f4
JM
104static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
105 bool *);
744aa42f 106static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
107static int function_types_compatible_p (const_tree, const_tree, bool *,
108 bool *);
109static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 110static tree lookup_field (tree, tree);
81e5eca8
MP
111static int convert_arguments (location_t, vec<location_t>, tree,
112 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
113 tree);
db3927fb 114static tree pointer_diff (location_t, tree, tree);
68fca595 115static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 116 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
117static tree valid_compound_expr_initializer (tree, tree);
118static void push_string (const char *);
119static void push_member_name (tree);
f55ade6e
AJ
120static int spelling_length (void);
121static char *print_spelling (char *);
96b40f8d 122static void warning_init (location_t, int, const char *);
c2255bc4 123static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
124static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
125 bool, struct obstack *);
a1e3b3d9 126static void output_pending_init_elements (int, struct obstack *);
ea58ef42 127static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 128static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
129static void add_pending_init (location_t, tree, tree, tree, bool,
130 struct obstack *);
a1e3b3d9
LB
131static void set_nonincremental_init (struct obstack *);
132static void set_nonincremental_init_from_string (tree, struct obstack *);
133static tree find_init_member (tree, struct obstack *);
f37acdf9 134static void readonly_warning (tree, enum lvalue_use);
7bd11157 135static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 136static void record_maybe_used_decl (tree);
dc5027f4 137static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
138\f
139/* Return true if EXP is a null pointer constant, false otherwise. */
140
141static bool
58f9752a 142null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
143{
144 /* This should really operate on c_expr structures, but they aren't
145 yet available everywhere required. */
146 tree type = TREE_TYPE (expr);
147 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 148 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
149 && integer_zerop (expr)
150 && (INTEGRAL_TYPE_P (type)
151 || (TREE_CODE (type) == POINTER_TYPE
152 && VOID_TYPE_P (TREE_TYPE (type))
153 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
154}
928c19bb
JM
155
156/* EXPR may appear in an unevaluated part of an integer constant
157 expression, but not in an evaluated part. Wrap it in a
158 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
159 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
160
161static tree
162note_integer_operands (tree expr)
163{
164 tree ret;
165 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
166 {
167 ret = copy_node (expr);
168 TREE_OVERFLOW (ret) = 1;
169 }
170 else
171 {
172 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
173 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
174 }
175 return ret;
176}
177
4d84fe7c
JM
178/* Having checked whether EXPR may appear in an unevaluated part of an
179 integer constant expression and found that it may, remove any
180 C_MAYBE_CONST_EXPR noting this fact and return the resulting
181 expression. */
182
183static inline tree
184remove_c_maybe_const_expr (tree expr)
185{
186 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
187 return C_MAYBE_CONST_EXPR_EXPR (expr);
188 else
189 return expr;
190}
191
f13c9b2c
AP
192\f/* This is a cache to hold if two types are compatible or not. */
193
194struct tagged_tu_seen_cache {
195 const struct tagged_tu_seen_cache * next;
58f9752a
KG
196 const_tree t1;
197 const_tree t2;
f13c9b2c
AP
198 /* The return value of tagged_types_tu_compatible_p if we had seen
199 these two types already. */
200 int val;
201};
202
203static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
204static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
205
400fbf9f
JW
206/* Do `exp = require_complete_type (exp);' to make sure exp
207 does not have an incomplete type. (That includes void types.) */
208
209tree
2f6e4e97 210require_complete_type (tree value)
400fbf9f
JW
211{
212 tree type = TREE_TYPE (value);
213
7a0ca710 214 if (error_operand_p (value))
ea0f786b
CB
215 return error_mark_node;
216
400fbf9f 217 /* First, detect a valid value with a complete type. */
d0f062fb 218 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
219 return value;
220
7a228918 221 c_incomplete_type_error (value, type);
400fbf9f
JW
222 return error_mark_node;
223}
224
225/* Print an error message for invalid use of an incomplete type.
226 VALUE is the expression that was used (or 0 if that isn't known)
227 and TYPE is the type that was invalid. */
228
229void
ac7d7749 230c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 231{
400fbf9f
JW
232 /* Avoid duplicate error message. */
233 if (TREE_CODE (type) == ERROR_MARK)
234 return;
235
236 if (value != 0 && (TREE_CODE (value) == VAR_DECL
237 || TREE_CODE (value) == PARM_DECL))
ac9f18db 238 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
239 else
240 {
241 retry:
242 /* We must print an error message. Be clever about what it says. */
243
244 switch (TREE_CODE (type))
245 {
246 case RECORD_TYPE:
400fbf9f 247 case UNION_TYPE:
400fbf9f 248 case ENUMERAL_TYPE:
400fbf9f
JW
249 break;
250
251 case VOID_TYPE:
252 error ("invalid use of void expression");
253 return;
254
255 case ARRAY_TYPE:
256 if (TYPE_DOMAIN (type))
257 {
fba78abb
RH
258 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
259 {
260 error ("invalid use of flexible array member");
261 return;
262 }
400fbf9f
JW
263 type = TREE_TYPE (type);
264 goto retry;
265 }
266 error ("invalid use of array with unspecified bounds");
267 return;
268
269 default:
366de0ce 270 gcc_unreachable ();
400fbf9f
JW
271 }
272
273 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 274 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
275 else
276 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 277 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
278 }
279}
280
ab393bf1
NB
281/* Given a type, apply default promotions wrt unnamed function
282 arguments and return the new type. */
283
284tree
2f6e4e97 285c_type_promotes_to (tree type)
ab393bf1 286{
267bac10 287 tree ret = NULL_TREE;
ab393bf1 288
267bac10
JM
289 if (TYPE_MAIN_VARIANT (type) == float_type_node)
290 ret = double_type_node;
291 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
292 {
293 /* Preserve unsignedness if not really getting any wider. */
8df83eae 294 if (TYPE_UNSIGNED (type)
c22cacf3 295 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
296 ret = unsigned_type_node;
297 else
298 ret = integer_type_node;
ab393bf1
NB
299 }
300
267bac10
JM
301 if (ret != NULL_TREE)
302 return (TYPE_ATOMIC (type)
303 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
304 : ret);
305
ab393bf1
NB
306 return type;
307}
308
36c5e70a
BE
309/* Return true if between two named address spaces, whether there is a superset
310 named address space that encompasses both address spaces. If there is a
311 superset, return which address space is the superset. */
312
313static bool
314addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
315{
316 if (as1 == as2)
317 {
318 *common = as1;
319 return true;
320 }
321 else if (targetm.addr_space.subset_p (as1, as2))
322 {
323 *common = as2;
324 return true;
325 }
326 else if (targetm.addr_space.subset_p (as2, as1))
327 {
328 *common = as1;
329 return true;
330 }
331 else
332 return false;
333}
334
400fbf9f
JW
335/* Return a variant of TYPE which has all the type qualifiers of LIKE
336 as well as those of TYPE. */
337
338static tree
2f6e4e97 339qualify_type (tree type, tree like)
400fbf9f 340{
36c5e70a
BE
341 addr_space_t as_type = TYPE_ADDR_SPACE (type);
342 addr_space_t as_like = TYPE_ADDR_SPACE (like);
343 addr_space_t as_common;
344
345 /* If the two named address spaces are different, determine the common
346 superset address space. If there isn't one, raise an error. */
347 if (!addr_space_superset (as_type, as_like, &as_common))
348 {
349 as_common = as_type;
350 error ("%qT and %qT are in disjoint named address spaces",
351 type, like);
352 }
353
2f6e4e97 354 return c_build_qualified_type (type,
36c5e70a 355 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 356 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 357 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 358}
52ffd86e
MS
359
360/* Return true iff the given tree T is a variable length array. */
361
362bool
ac7d7749 363c_vla_type_p (const_tree t)
52ffd86e
MS
364{
365 if (TREE_CODE (t) == ARRAY_TYPE
366 && C_TYPE_VARIABLE_SIZE (t))
367 return true;
368 return false;
369}
400fbf9f 370\f
10bc1b1b 371/* Return the composite type of two compatible types.
5305f6d7 372
10bc1b1b
JM
373 We assume that comptypes has already been done and returned
374 nonzero; if that isn't so, this may crash. In particular, we
375 assume that qualifiers match. */
400fbf9f
JW
376
377tree
10bc1b1b 378composite_type (tree t1, tree t2)
400fbf9f 379{
b3694847
SS
380 enum tree_code code1;
381 enum tree_code code2;
4b027d16 382 tree attributes;
400fbf9f
JW
383
384 /* Save time if the two types are the same. */
385
386 if (t1 == t2) return t1;
387
388 /* If one type is nonsense, use the other. */
389 if (t1 == error_mark_node)
390 return t2;
391 if (t2 == error_mark_node)
392 return t1;
393
10bc1b1b
JM
394 code1 = TREE_CODE (t1);
395 code2 = TREE_CODE (t2);
396
d9525bec 397 /* Merge the attributes. */
5fd9b178 398 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 399
10bc1b1b
JM
400 /* If one is an enumerated type and the other is the compatible
401 integer type, the composite type might be either of the two
402 (DR#013 question 3). For consistency, use the enumerated type as
403 the composite type. */
400fbf9f 404
10bc1b1b
JM
405 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
406 return t1;
407 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
408 return t2;
75326e8c 409
366de0ce 410 gcc_assert (code1 == code2);
b6a10c9f 411
400fbf9f
JW
412 switch (code1)
413 {
400fbf9f 414 case POINTER_TYPE:
10bc1b1b 415 /* For two pointers, do this recursively on the target type. */
400fbf9f 416 {
3932261a
MM
417 tree pointed_to_1 = TREE_TYPE (t1);
418 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 419 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 420 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
421 t1 = build_type_attribute_variant (t1, attributes);
422 return qualify_type (t1, t2);
400fbf9f 423 }
400fbf9f
JW
424
425 case ARRAY_TYPE:
426 {
10bc1b1b 427 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
428 int quals;
429 tree unqual_elt;
ca8bdb78
JM
430 tree d1 = TYPE_DOMAIN (t1);
431 tree d2 = TYPE_DOMAIN (t2);
432 bool d1_variable, d2_variable;
433 bool d1_zero, d2_zero;
f6294de7 434 bool t1_complete, t2_complete;
46df2823 435
de46b2fe 436 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
437 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
438 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 439
f6294de7
JM
440 t1_complete = COMPLETE_TYPE_P (t1);
441 t2_complete = COMPLETE_TYPE_P (t2);
442
ca8bdb78
JM
443 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
444 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
445
446 d1_variable = (!d1_zero
447 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
448 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
449 d2_variable = (!d2_zero
450 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
451 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
452 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
453 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 454
400fbf9f 455 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
456 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
457 && (d2_variable || d2_zero || !d1_variable))
4b027d16 458 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
459 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
460 && (d1_variable || d1_zero || !d2_variable))
4b027d16 461 return build_type_attribute_variant (t2, attributes);
c22cacf3 462
de46b2fe
AP
463 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t1, attributes);
465 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
466 return build_type_attribute_variant (t2, attributes);
c22cacf3 467
46df2823
JM
468 /* Merge the element types, and have a size if either arg has
469 one. We may have qualifiers on the element types. To set
470 up TYPE_MAIN_VARIANT correctly, we need to form the
471 composite of the unqualified types and add the qualifiers
472 back at the end. */
473 quals = TYPE_QUALS (strip_array_types (elt));
474 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
475 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
476 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
477 && (d2_variable
478 || d2_zero
479 || !d1_variable))
480 ? t1
481 : t2));
f6294de7
JM
482 /* Ensure a composite type involving a zero-length array type
483 is a zero-length type not an incomplete type. */
484 if (d1_zero && d2_zero
485 && (t1_complete || t2_complete)
486 && !COMPLETE_TYPE_P (t1))
487 {
488 TYPE_SIZE (t1) = bitsize_zero_node;
489 TYPE_SIZE_UNIT (t1) = size_zero_node;
490 }
46df2823 491 t1 = c_build_qualified_type (t1, quals);
de46b2fe 492 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
493 }
494
fcb99e7b
JJ
495 case ENUMERAL_TYPE:
496 case RECORD_TYPE:
497 case UNION_TYPE:
498 if (attributes != NULL)
499 {
500 /* Try harder not to create a new aggregate type. */
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
502 return t1;
503 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
504 return t2;
505 }
506 return build_type_attribute_variant (t1, attributes);
507
400fbf9f
JW
508 case FUNCTION_TYPE:
509 /* Function types: prefer the one that specified arg types.
510 If both do, merge the arg types. Also merge the return types. */
511 {
10bc1b1b 512 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
513 tree p1 = TYPE_ARG_TYPES (t1);
514 tree p2 = TYPE_ARG_TYPES (t2);
515 int len;
516 tree newargs, n;
517 int i;
518
519 /* Save space: see if the result is identical to one of the args. */
3f75a254 520 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 521 return build_type_attribute_variant (t1, attributes);
3f75a254 522 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 523 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
524
525 /* Simple way if one arg fails to specify argument types. */
526 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 527 {
fe7080d2
AP
528 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
529 t1 = build_type_attribute_variant (t1, attributes);
530 return qualify_type (t1, t2);
4b027d16 531 }
400fbf9f 532 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
533 {
534 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
535 t1 = build_type_attribute_variant (t1, attributes);
536 return qualify_type (t1, t2);
4b027d16 537 }
400fbf9f
JW
538
539 /* If both args specify argument types, we must merge the two
540 lists, argument by argument. */
2f4e8f2b 541
400fbf9f
JW
542 len = list_length (p1);
543 newargs = 0;
544
545 for (i = 0; i < len; i++)
8d9bfdc5 546 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
547
548 n = newargs;
549
550 for (; p1;
551 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
552 {
553 /* A null type means arg type is not specified.
554 Take whatever the other function type has. */
555 if (TREE_VALUE (p1) == 0)
556 {
557 TREE_VALUE (n) = TREE_VALUE (p2);
558 goto parm_done;
559 }
560 if (TREE_VALUE (p2) == 0)
561 {
562 TREE_VALUE (n) = TREE_VALUE (p1);
563 goto parm_done;
564 }
2f6e4e97 565
400fbf9f
JW
566 /* Given wait (union {union wait *u; int *i} *)
567 and wait (union wait *),
568 prefer union wait * as type of parm. */
569 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
570 && TREE_VALUE (p1) != TREE_VALUE (p2))
571 {
572 tree memb;
58cb41e6
JJ
573 tree mv2 = TREE_VALUE (p2);
574 if (mv2 && mv2 != error_mark_node
575 && TREE_CODE (mv2) != ARRAY_TYPE)
576 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 577 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 578 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
579 {
580 tree mv3 = TREE_TYPE (memb);
581 if (mv3 && mv3 != error_mark_node
582 && TREE_CODE (mv3) != ARRAY_TYPE)
583 mv3 = TYPE_MAIN_VARIANT (mv3);
584 if (comptypes (mv3, mv2))
585 {
586 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
587 TREE_VALUE (p2));
c1771a20 588 pedwarn (input_location, OPT_Wpedantic,
fcf73884 589 "function types not truly compatible in ISO C");
58cb41e6
JJ
590 goto parm_done;
591 }
592 }
400fbf9f
JW
593 }
594 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
595 && TREE_VALUE (p2) != TREE_VALUE (p1))
596 {
597 tree memb;
58cb41e6
JJ
598 tree mv1 = TREE_VALUE (p1);
599 if (mv1 && mv1 != error_mark_node
600 && TREE_CODE (mv1) != ARRAY_TYPE)
601 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 602 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 603 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
604 {
605 tree mv3 = TREE_TYPE (memb);
606 if (mv3 && mv3 != error_mark_node
607 && TREE_CODE (mv3) != ARRAY_TYPE)
608 mv3 = TYPE_MAIN_VARIANT (mv3);
609 if (comptypes (mv3, mv1))
610 {
611 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
612 TREE_VALUE (p1));
c1771a20 613 pedwarn (input_location, OPT_Wpedantic,
fcf73884 614 "function types not truly compatible in ISO C");
58cb41e6
JJ
615 goto parm_done;
616 }
617 }
400fbf9f 618 }
10bc1b1b 619 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
620 parm_done: ;
621 }
622
4b027d16 623 t1 = build_function_type (valtype, newargs);
fe7080d2 624 t1 = qualify_type (t1, t2);
0f41302f 625 /* ... falls through ... */
400fbf9f
JW
626 }
627
628 default:
4b027d16 629 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
630 }
631
632}
10bc1b1b
JM
633
634/* Return the type of a conditional expression between pointers to
635 possibly differently qualified versions of compatible types.
636
637 We assume that comp_target_types has already been done and returned
638 nonzero; if that isn't so, this may crash. */
639
640static tree
641common_pointer_type (tree t1, tree t2)
642{
643 tree attributes;
46df2823
JM
644 tree pointed_to_1, mv1;
645 tree pointed_to_2, mv2;
10bc1b1b 646 tree target;
eb1387a0 647 unsigned target_quals;
36c5e70a
BE
648 addr_space_t as1, as2, as_common;
649 int quals1, quals2;
10bc1b1b
JM
650
651 /* Save time if the two types are the same. */
652
653 if (t1 == t2) return t1;
654
655 /* If one type is nonsense, use the other. */
656 if (t1 == error_mark_node)
657 return t2;
658 if (t2 == error_mark_node)
659 return t1;
660
366de0ce 661 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 662 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
663
664 /* Merge the attributes. */
665 attributes = targetm.merge_type_attributes (t1, t2);
666
667 /* Find the composite type of the target types, and combine the
46df2823
JM
668 qualifiers of the two types' targets. Do not lose qualifiers on
669 array element types by taking the TYPE_MAIN_VARIANT. */
670 mv1 = pointed_to_1 = TREE_TYPE (t1);
671 mv2 = pointed_to_2 = TREE_TYPE (t2);
672 if (TREE_CODE (mv1) != ARRAY_TYPE)
673 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
674 if (TREE_CODE (mv2) != ARRAY_TYPE)
675 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
676 target = composite_type (mv1, mv2);
eb1387a0 677
768952be
MU
678 /* Strip array types to get correct qualifier for pointers to arrays */
679 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
680 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
681
eb1387a0
RG
682 /* For function types do not merge const qualifiers, but drop them
683 if used inconsistently. The middle-end uses these to mark const
684 and noreturn functions. */
685 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 686 target_quals = (quals1 & quals2);
eb1387a0 687 else
36c5e70a
BE
688 target_quals = (quals1 | quals2);
689
690 /* If the two named address spaces are different, determine the common
691 superset address space. This is guaranteed to exist due to the
692 assumption that comp_target_type returned non-zero. */
693 as1 = TYPE_ADDR_SPACE (pointed_to_1);
694 as2 = TYPE_ADDR_SPACE (pointed_to_2);
695 if (!addr_space_superset (as1, as2, &as_common))
696 gcc_unreachable ();
697
698 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
699
eb1387a0 700 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
701 return build_type_attribute_variant (t1, attributes);
702}
703
704/* Return the common type for two arithmetic types under the usual
705 arithmetic conversions. The default conversions have already been
706 applied, and enumerated types converted to their compatible integer
707 types. The resulting type is unqualified and has no attributes.
708
709 This is the type for the result of most arithmetic operations
710 if the operands have the given two types. */
711
ccf7f880
JJ
712static tree
713c_common_type (tree t1, tree t2)
10bc1b1b
JM
714{
715 enum tree_code code1;
716 enum tree_code code2;
717
718 /* If one type is nonsense, use the other. */
719 if (t1 == error_mark_node)
720 return t2;
721 if (t2 == error_mark_node)
722 return t1;
723
724 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
725 t1 = TYPE_MAIN_VARIANT (t1);
726
727 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
728 t2 = TYPE_MAIN_VARIANT (t2);
729
730 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
731 t1 = build_type_attribute_variant (t1, NULL_TREE);
732
733 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
734 t2 = build_type_attribute_variant (t2, NULL_TREE);
735
736 /* Save time if the two types are the same. */
737
738 if (t1 == t2) return t1;
739
740 code1 = TREE_CODE (t1);
741 code2 = TREE_CODE (t2);
742
366de0ce 743 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
744 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
745 || code1 == INTEGER_TYPE);
366de0ce 746 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
747 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
748 || code2 == INTEGER_TYPE);
10bc1b1b 749
5fc89bfd
JJ
750 /* When one operand is a decimal float type, the other operand cannot be
751 a generic float type or a complex type. We also disallow vector types
752 here. */
753 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
754 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
755 {
756 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
757 {
758 error ("can%'t mix operands of decimal float and vector types");
759 return error_mark_node;
760 }
761 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
762 {
763 error ("can%'t mix operands of decimal float and complex types");
764 return error_mark_node;
765 }
766 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
767 {
768 error ("can%'t mix operands of decimal float and other float types");
769 return error_mark_node;
770 }
771 }
772
10bc1b1b
JM
773 /* If one type is a vector type, return that type. (How the usual
774 arithmetic conversions apply to the vector types extension is not
775 precisely specified.) */
776 if (code1 == VECTOR_TYPE)
777 return t1;
778
779 if (code2 == VECTOR_TYPE)
780 return t2;
781
782 /* If one type is complex, form the common type of the non-complex
783 components, then make that complex. Use T1 or T2 if it is the
784 required type. */
785 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
786 {
787 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
788 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 789 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
790
791 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
792 return t1;
793 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
794 return t2;
795 else
796 return build_complex_type (subtype);
797 }
798
799 /* If only one is real, use it as the result. */
800
801 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
802 return t1;
803
804 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
805 return t2;
806
9a8ce21f
JG
807 /* If both are real and either are decimal floating point types, use
808 the decimal floating point type with the greater precision. */
809
810 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
811 {
812 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
813 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
814 return dfloat128_type_node;
815 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
816 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
817 return dfloat64_type_node;
818 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
819 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
820 return dfloat32_type_node;
821 }
822
ab22c1fa
CF
823 /* Deal with fixed-point types. */
824 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
825 {
826 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 827 machine_mode m1, m2;
ab22c1fa
CF
828 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
829
830 m1 = TYPE_MODE (t1);
831 m2 = TYPE_MODE (t2);
832
833 /* If one input type is saturating, the result type is saturating. */
834 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
835 satp = 1;
836
837 /* If both fixed-point types are unsigned, the result type is unsigned.
838 When mixing fixed-point and integer types, follow the sign of the
839 fixed-point type.
840 Otherwise, the result type is signed. */
841 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
842 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
843 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
844 && TYPE_UNSIGNED (t1))
845 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
846 && TYPE_UNSIGNED (t2)))
847 unsignedp = 1;
848
849 /* The result type is signed. */
850 if (unsignedp == 0)
851 {
852 /* If the input type is unsigned, we need to convert to the
853 signed type. */
854 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
855 {
d75d71e0 856 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
857 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
858 mclass = MODE_FRACT;
859 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
860 mclass = MODE_ACCUM;
861 else
862 gcc_unreachable ();
863 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
864 }
865 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
866 {
d75d71e0 867 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
868 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
869 mclass = MODE_FRACT;
870 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
871 mclass = MODE_ACCUM;
872 else
873 gcc_unreachable ();
874 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
875 }
876 }
877
878 if (code1 == FIXED_POINT_TYPE)
879 {
880 fbit1 = GET_MODE_FBIT (m1);
881 ibit1 = GET_MODE_IBIT (m1);
882 }
883 else
884 {
885 fbit1 = 0;
886 /* Signed integers need to subtract one sign bit. */
887 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
888 }
889
890 if (code2 == FIXED_POINT_TYPE)
891 {
892 fbit2 = GET_MODE_FBIT (m2);
893 ibit2 = GET_MODE_IBIT (m2);
894 }
895 else
896 {
897 fbit2 = 0;
898 /* Signed integers need to subtract one sign bit. */
899 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
900 }
901
902 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
903 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
904 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
905 satp);
906 }
907
10bc1b1b
JM
908 /* Both real or both integers; use the one with greater precision. */
909
910 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
911 return t1;
912 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
913 return t2;
914
915 /* Same precision. Prefer long longs to longs to ints when the
916 same precision, following the C99 rules on integer type rank
917 (which are equivalent to the C90 rules for C90 types). */
918
919 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
920 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
921 return long_long_unsigned_type_node;
922
923 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
924 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
925 {
926 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
927 return long_long_unsigned_type_node;
928 else
c22cacf3 929 return long_long_integer_type_node;
10bc1b1b
JM
930 }
931
932 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
933 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
934 return long_unsigned_type_node;
935
936 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
937 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
938 {
939 /* But preserve unsignedness from the other type,
940 since long cannot hold all the values of an unsigned int. */
941 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
942 return long_unsigned_type_node;
943 else
944 return long_integer_type_node;
945 }
946
947 /* Likewise, prefer long double to double even if same size. */
948 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
949 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
950 return long_double_type_node;
951
2531a1d9
JR
952 /* Likewise, prefer double to float even if same size.
953 We got a couple of embedded targets with 32 bit doubles, and the
954 pdp11 might have 64 bit floats. */
955 if (TYPE_MAIN_VARIANT (t1) == double_type_node
956 || TYPE_MAIN_VARIANT (t2) == double_type_node)
957 return double_type_node;
958
10bc1b1b
JM
959 /* Otherwise prefer the unsigned one. */
960
961 if (TYPE_UNSIGNED (t1))
962 return t1;
963 else
964 return t2;
965}
400fbf9f 966\f
5922c215
JM
967/* Wrapper around c_common_type that is used by c-common.c and other
968 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
969 are allowed here and are converted to their compatible integer types.
970 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
971 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
972tree
973common_type (tree t1, tree t2)
974{
975 if (TREE_CODE (t1) == ENUMERAL_TYPE)
976 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
977 if (TREE_CODE (t2) == ENUMERAL_TYPE)
978 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
979
980 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
981 if (TREE_CODE (t1) == BOOLEAN_TYPE
982 && TREE_CODE (t2) == BOOLEAN_TYPE)
983 return boolean_type_node;
984
985 /* If either type is BOOLEAN_TYPE, then return the other. */
986 if (TREE_CODE (t1) == BOOLEAN_TYPE)
987 return t2;
988 if (TREE_CODE (t2) == BOOLEAN_TYPE)
989 return t1;
990
ccf7f880
JJ
991 return c_common_type (t1, t2);
992}
f13c9b2c 993
400fbf9f
JW
994/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
995 or various other operations. Return 2 if they are compatible
996 but a warning may be needed if you use them together. */
997
998int
132da1a5 999comptypes (tree type1, tree type2)
f13c9b2c
AP
1000{
1001 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1002 int val;
1003
dc5027f4 1004 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
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 non-zero because enum and int are
1011 compatible, it sets *ENUM_AND_INT_P to true. */
1012
1013static int
1014comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1015{
1016 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1017 int val;
1018
dc5027f4
JM
1019 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1020 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1021
1022 return val;
1023}
1024
1025/* Like comptypes, but if it returns nonzero for different types, it
1026 sets *DIFFERENT_TYPES_P to true. */
1027
1028int
1029comptypes_check_different_types (tree type1, tree type2,
1030 bool *different_types_p)
1031{
1032 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1033 int val;
1034
1035 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1036 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1037
f13c9b2c 1038 return val;
c22cacf3
MS
1039}
1040\f
f13c9b2c
AP
1041/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1042 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1043 but a warning may be needed if you use them together. If
1044 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1045 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1046 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1047 NULL, and the types are compatible but different enough not to be
48b0b196 1048 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1049 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1050 false, but may or may not be set if the types are incompatible.
1051 This differs from comptypes, in that we don't free the seen
1052 types. */
f13c9b2c
AP
1053
1054static int
dc5027f4
JM
1055comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1056 bool *different_types_p)
400fbf9f 1057{
58f9752a
KG
1058 const_tree t1 = type1;
1059 const_tree t2 = type2;
4b027d16 1060 int attrval, val;
400fbf9f
JW
1061
1062 /* Suppress errors caused by previously reported errors. */
1063
8d47dfc5
RH
1064 if (t1 == t2 || !t1 || !t2
1065 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1066 return 1;
1067
bca63328
JM
1068 /* Enumerated types are compatible with integer types, but this is
1069 not transitive: two enumerated types in the same translation unit
1070 are compatible with each other only if they are the same type. */
400fbf9f 1071
bca63328 1072 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1073 {
1074 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1075 if (TREE_CODE (t2) != VOID_TYPE)
1076 {
1077 if (enum_and_int_p != NULL)
1078 *enum_and_int_p = true;
1079 if (different_types_p != NULL)
1080 *different_types_p = true;
1081 }
744aa42f 1082 }
bca63328 1083 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1084 {
1085 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1086 if (TREE_CODE (t1) != VOID_TYPE)
1087 {
1088 if (enum_and_int_p != NULL)
1089 *enum_and_int_p = true;
1090 if (different_types_p != NULL)
1091 *different_types_p = true;
1092 }
744aa42f 1093 }
400fbf9f
JW
1094
1095 if (t1 == t2)
1096 return 1;
1097
1098 /* Different classes of types can't be compatible. */
1099
3aeb3655
EC
1100 if (TREE_CODE (t1) != TREE_CODE (t2))
1101 return 0;
400fbf9f 1102
118a3a8b 1103 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1104
3932261a 1105 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1106 return 0;
1107
08632da2
RS
1108 /* Allow for two different type nodes which have essentially the same
1109 definition. Note that we already checked for equality of the type
38e01259 1110 qualifiers (just above). */
400fbf9f 1111
46df2823
JM
1112 if (TREE_CODE (t1) != ARRAY_TYPE
1113 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1114 return 1;
1115
4b027d16 1116 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1117 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1118 return 0;
1119
1120 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1121 val = 0;
1122
400fbf9f
JW
1123 switch (TREE_CODE (t1))
1124 {
1125 case POINTER_TYPE:
106f5de5
UW
1126 /* Do not remove mode or aliasing information. */
1127 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1128 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1129 break;
4b027d16 1130 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1131 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1132 enum_and_int_p, different_types_p));
4b027d16 1133 break;
400fbf9f
JW
1134
1135 case FUNCTION_TYPE:
dc5027f4
JM
1136 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1137 different_types_p);
4b027d16 1138 break;
400fbf9f
JW
1139
1140 case ARRAY_TYPE:
1141 {
400fbf9f
JW
1142 tree d1 = TYPE_DOMAIN (t1);
1143 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1144 bool d1_variable, d2_variable;
1145 bool d1_zero, d2_zero;
4b027d16 1146 val = 1;
400fbf9f
JW
1147
1148 /* Target types must match incl. qualifiers. */
1149 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1150 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1151 enum_and_int_p,
1152 different_types_p)))
400fbf9f
JW
1153 return 0;
1154
dc5027f4
JM
1155 if (different_types_p != NULL
1156 && (d1 == 0) != (d2 == 0))
1157 *different_types_p = true;
400fbf9f 1158 /* Sizes must match unless one is missing or variable. */
3f85558f 1159 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1160 break;
400fbf9f 1161
3f75a254
JM
1162 d1_zero = !TYPE_MAX_VALUE (d1);
1163 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1164
3f75a254 1165 d1_variable = (!d1_zero
3f85558f
RH
1166 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1167 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1168 d2_variable = (!d2_zero
3f85558f
RH
1169 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1170 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1171 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1172 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1173
dc5027f4
JM
1174 if (different_types_p != NULL
1175 && d1_variable != d2_variable)
1176 *different_types_p = true;
3f85558f
RH
1177 if (d1_variable || d2_variable)
1178 break;
1179 if (d1_zero && d2_zero)
1180 break;
1181 if (d1_zero || d2_zero
3f75a254
JM
1182 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1183 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1184 val = 0;
1185
c22cacf3 1186 break;
400fbf9f
JW
1187 }
1188
d1bd0ded 1189 case ENUMERAL_TYPE:
58393038 1190 case RECORD_TYPE:
d1bd0ded 1191 case UNION_TYPE:
766beae1 1192 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1193 {
fcb99e7b
JJ
1194 tree a1 = TYPE_ATTRIBUTES (t1);
1195 tree a2 = TYPE_ATTRIBUTES (t2);
1196
1197 if (! attribute_list_contained (a1, a2)
1198 && ! attribute_list_contained (a2, a1))
1199 break;
1200
f13c9b2c 1201 if (attrval != 2)
dc5027f4
JM
1202 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1203 different_types_p);
1204 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1205 different_types_p);
f13c9b2c 1206 }
4b027d16 1207 break;
e9a25f70 1208
62e1dfcf 1209 case VECTOR_TYPE:
744aa42f
ILT
1210 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1211 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1212 enum_and_int_p, different_types_p));
62e1dfcf
NC
1213 break;
1214
e9a25f70
JL
1215 default:
1216 break;
400fbf9f 1217 }
4b027d16 1218 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1219}
1220
36c5e70a
BE
1221/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1222 their qualifiers, except for named address spaces. If the pointers point to
1223 different named addresses, then we must determine if one address space is a
1224 subset of the other. */
400fbf9f
JW
1225
1226static int
744aa42f 1227comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1228{
392202b0 1229 int val;
768952be 1230 int val_ped;
36c5e70a
BE
1231 tree mvl = TREE_TYPE (ttl);
1232 tree mvr = TREE_TYPE (ttr);
1233 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1234 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1235 addr_space_t as_common;
744aa42f 1236 bool enum_and_int_p;
8b40563c 1237
36c5e70a
BE
1238 /* Fail if pointers point to incompatible address spaces. */
1239 if (!addr_space_superset (asl, asr, &as_common))
1240 return 0;
1241
768952be
MU
1242 /* For pedantic record result of comptypes on arrays before losing
1243 qualifiers on the element type below. */
1244 val_ped = 1;
1245
1246 if (TREE_CODE (mvl) == ARRAY_TYPE
1247 && TREE_CODE (mvr) == ARRAY_TYPE)
1248 val_ped = comptypes (mvl, mvr);
1249
1250 /* Qualifiers on element types of array types that are
1251 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1252
1253 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1254 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1255 : TYPE_MAIN_VARIANT (mvl));
1256
1257 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1258 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1259 : TYPE_MAIN_VARIANT (mvr));
1260
744aa42f
ILT
1261 enum_and_int_p = false;
1262 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1263
768952be
MU
1264 if (val == 1 && val_ped != 1)
1265 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1266 "are incompatible in ISO C");
1267
fcf73884 1268 if (val == 2)
c1771a20 1269 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1270
1271 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1272 warning_at (location, OPT_Wc___compat,
1273 "pointer target types incompatible in C++");
1274
400fbf9f
JW
1275 return val;
1276}
1277\f
1278/* Subroutines of `comptypes'. */
1279
f75fbaf7
ZW
1280/* Determine whether two trees derive from the same translation unit.
1281 If the CONTEXT chain ends in a null, that tree's context is still
1282 being parsed, so if two trees have context chains ending in null,
766beae1 1283 they're in the same translation unit. */
f75fbaf7 1284int
58f9752a 1285same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1286{
1287 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1288 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1289 {
6615c446
JO
1290 case tcc_declaration:
1291 t1 = DECL_CONTEXT (t1); break;
1292 case tcc_type:
1293 t1 = TYPE_CONTEXT (t1); break;
1294 case tcc_exceptional:
1295 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1296 default: gcc_unreachable ();
766beae1
ZW
1297 }
1298
1299 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1300 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1301 {
6615c446
JO
1302 case tcc_declaration:
1303 t2 = DECL_CONTEXT (t2); break;
1304 case tcc_type:
1305 t2 = TYPE_CONTEXT (t2); break;
1306 case tcc_exceptional:
1307 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1308 default: gcc_unreachable ();
766beae1
ZW
1309 }
1310
1311 return t1 == t2;
1312}
1313
f13c9b2c 1314/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1315
f13c9b2c 1316static struct tagged_tu_seen_cache *
58f9752a 1317alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1318{
cceb1885 1319 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1320 tu->next = tagged_tu_seen_base;
1321 tu->t1 = t1;
1322 tu->t2 = t2;
c22cacf3 1323
f13c9b2c 1324 tagged_tu_seen_base = tu;
c22cacf3 1325
f13c9b2c
AP
1326 /* The C standard says that two structures in different translation
1327 units are compatible with each other only if the types of their
1328 fields are compatible (among other things). We assume that they
1329 are compatible until proven otherwise when building the cache.
1330 An example where this can occur is:
1331 struct a
1332 {
1333 struct a *next;
1334 };
1335 If we are comparing this against a similar struct in another TU,
c83eecad 1336 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1337 loop. */
1338 tu->val = 1;
1339 return tu;
1340}
d1bd0ded 1341
f13c9b2c 1342/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1343
f13c9b2c
AP
1344static void
1345free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1346{
1347 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1348 while (tu != tu_til)
1349 {
741ac903
KG
1350 const struct tagged_tu_seen_cache *const tu1
1351 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1352 tu = tu1->next;
b1d5455a 1353 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1354 }
1355 tagged_tu_seen_base = tu_til;
1356}
d1bd0ded
GK
1357
1358/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1359 compatible. If the two types are not the same (which has been
1360 checked earlier), this can only happen when multiple translation
1361 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1362 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1363 comptypes_internal. */
d1bd0ded
GK
1364
1365static int
744aa42f 1366tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1367 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1368{
1369 tree s1, s2;
1370 bool needs_warning = false;
3aeb3655 1371
d1bd0ded
GK
1372 /* We have to verify that the tags of the types are the same. This
1373 is harder than it looks because this may be a typedef, so we have
1374 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1375 typedef...
1376 In the case of compiler-created builtin structs the TYPE_DECL
1377 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1378 while (TYPE_NAME (t1)
1379 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1380 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1381 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1382
dea984dc
ILT
1383 while (TYPE_NAME (t2)
1384 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1385 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1386 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1387
1388 /* C90 didn't have the requirement that the two tags be the same. */
1389 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1390 return 0;
3aeb3655 1391
d1bd0ded
GK
1392 /* C90 didn't say what happened if one or both of the types were
1393 incomplete; we choose to follow C99 rules here, which is that they
1394 are compatible. */
1395 if (TYPE_SIZE (t1) == NULL
1396 || TYPE_SIZE (t2) == NULL)
1397 return 1;
3aeb3655 1398
d1bd0ded 1399 {
f13c9b2c 1400 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1401 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1402 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1403 return tts_i->val;
d1bd0ded 1404 }
3aeb3655 1405
d1bd0ded
GK
1406 switch (TREE_CODE (t1))
1407 {
1408 case ENUMERAL_TYPE:
1409 {
f13c9b2c 1410 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1411 /* Speed up the case where the type values are in the same order. */
1412 tree tv1 = TYPE_VALUES (t1);
1413 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1414
c22cacf3 1415 if (tv1 == tv2)
f13c9b2c
AP
1416 {
1417 return 1;
1418 }
3aeb3655 1419
c22cacf3
MS
1420 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1421 {
1422 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1423 break;
1424 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1425 {
c22cacf3 1426 tu->val = 0;
f13c9b2c
AP
1427 return 0;
1428 }
c22cacf3 1429 }
3aeb3655 1430
c22cacf3 1431 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1432 {
1433 return 1;
1434 }
c22cacf3 1435 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1436 {
1437 tu->val = 0;
1438 return 0;
1439 }
3aeb3655 1440
d1bd0ded 1441 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1442 {
1443 tu->val = 0;
1444 return 0;
1445 }
3aeb3655 1446
d1bd0ded
GK
1447 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1448 {
1449 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1450 if (s2 == NULL
1451 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1452 {
1453 tu->val = 0;
1454 return 0;
1455 }
d1bd0ded
GK
1456 }
1457 return 1;
1458 }
1459
1460 case UNION_TYPE:
1461 {
f13c9b2c 1462 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1463 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1464 {
1465 tu->val = 0;
1466 return 0;
1467 }
c22cacf3 1468
f13c9b2c
AP
1469 /* Speed up the common case where the fields are in the same order. */
1470 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1471 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1472 {
1473 int result;
c22cacf3 1474
3ae4d3cc 1475 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1476 break;
744aa42f 1477 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1478 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1479
1480 if (result != 1 && !DECL_NAME (s1))
1481 break;
f13c9b2c
AP
1482 if (result == 0)
1483 {
1484 tu->val = 0;
1485 return 0;
1486 }
1487 if (result == 2)
1488 needs_warning = true;
1489
1490 if (TREE_CODE (s1) == FIELD_DECL
1491 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1492 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1493 {
1494 tu->val = 0;
1495 return 0;
1496 }
1497 }
1498 if (!s1 && !s2)
1499 {
1500 tu->val = needs_warning ? 2 : 1;
1501 return tu->val;
1502 }
d1bd0ded 1503
910ad8de 1504 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1505 {
1506 bool ok = false;
3aeb3655 1507
910ad8de 1508 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1509 if (DECL_NAME (s1) == DECL_NAME (s2))
1510 {
1511 int result;
1512
744aa42f 1513 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1514 enum_and_int_p,
1515 different_types_p);
3ae4d3cc
AO
1516
1517 if (result != 1 && !DECL_NAME (s1))
1518 continue;
1519 if (result == 0)
1520 {
1521 tu->val = 0;
1522 return 0;
1523 }
1524 if (result == 2)
1525 needs_warning = true;
1526
1527 if (TREE_CODE (s1) == FIELD_DECL
1528 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1529 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1530 break;
3ae4d3cc
AO
1531
1532 ok = true;
1533 break;
1534 }
3f75a254 1535 if (!ok)
f13c9b2c
AP
1536 {
1537 tu->val = 0;
1538 return 0;
1539 }
d1bd0ded 1540 }
f13c9b2c
AP
1541 tu->val = needs_warning ? 2 : 10;
1542 return tu->val;
d1bd0ded
GK
1543 }
1544
1545 case RECORD_TYPE:
1546 {
c22cacf3 1547 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1548
1549 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1550 s1 && s2;
910ad8de 1551 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1552 {
1553 int result;
1554 if (TREE_CODE (s1) != TREE_CODE (s2)
1555 || DECL_NAME (s1) != DECL_NAME (s2))
1556 break;
744aa42f 1557 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1558 enum_and_int_p, different_types_p);
d1bd0ded
GK
1559 if (result == 0)
1560 break;
1561 if (result == 2)
1562 needs_warning = true;
3aeb3655 1563
d1bd0ded
GK
1564 if (TREE_CODE (s1) == FIELD_DECL
1565 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1566 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1567 break;
1568 }
d1bd0ded 1569 if (s1 && s2)
f13c9b2c
AP
1570 tu->val = 0;
1571 else
1572 tu->val = needs_warning ? 2 : 1;
1573 return tu->val;
d1bd0ded
GK
1574 }
1575
1576 default:
366de0ce 1577 gcc_unreachable ();
d1bd0ded
GK
1578 }
1579}
1580
400fbf9f
JW
1581/* Return 1 if two function types F1 and F2 are compatible.
1582 If either type specifies no argument types,
1583 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1584 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1585 the other must specify that number of self-promoting arg types.
744aa42f 1586 Otherwise, the argument types must match.
dc5027f4 1587 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1588
1589static int
744aa42f 1590function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1591 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1592{
1593 tree args1, args2;
1594 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1595 int val = 1;
1596 int val1;
a6fdc086
GK
1597 tree ret1, ret2;
1598
1599 ret1 = TREE_TYPE (f1);
1600 ret2 = TREE_TYPE (f2);
1601
e508a019
JM
1602 /* 'volatile' qualifiers on a function's return type used to mean
1603 the function is noreturn. */
1604 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1605 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1606 if (TYPE_VOLATILE (ret1))
1607 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1608 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1609 if (TYPE_VOLATILE (ret2))
1610 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1611 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1612 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1613 if (val == 0)
400fbf9f
JW
1614 return 0;
1615
1616 args1 = TYPE_ARG_TYPES (f1);
1617 args2 = TYPE_ARG_TYPES (f2);
1618
dc5027f4
JM
1619 if (different_types_p != NULL
1620 && (args1 == 0) != (args2 == 0))
1621 *different_types_p = true;
1622
400fbf9f
JW
1623 /* An unspecified parmlist matches any specified parmlist
1624 whose argument types don't need default promotions. */
1625
1626 if (args1 == 0)
1627 {
1628 if (!self_promoting_args_p (args2))
1629 return 0;
1630 /* If one of these types comes from a non-prototype fn definition,
1631 compare that with the other type's arglist.
3176a0c2 1632 If they don't match, ask for a warning (but no error). */
400fbf9f 1633 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1634 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1635 enum_and_int_p, different_types_p))
400fbf9f
JW
1636 val = 2;
1637 return val;
1638 }
1639 if (args2 == 0)
1640 {
1641 if (!self_promoting_args_p (args1))
1642 return 0;
1643 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1644 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1645 enum_and_int_p, different_types_p))
400fbf9f
JW
1646 val = 2;
1647 return val;
1648 }
1649
1650 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1651 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1652 different_types_p);
400fbf9f
JW
1653 return val1 != 1 ? val1 : val;
1654}
1655
744aa42f
ILT
1656/* Check two lists of types for compatibility, returning 0 for
1657 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1658 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1659 comptypes_internal. */
400fbf9f
JW
1660
1661static int
744aa42f 1662type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1663 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1664{
1665 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1666 int val = 1;
9d5f3e49 1667 int newval = 0;
400fbf9f
JW
1668
1669 while (1)
1670 {
46df2823 1671 tree a1, mv1, a2, mv2;
400fbf9f
JW
1672 if (args1 == 0 && args2 == 0)
1673 return val;
1674 /* If one list is shorter than the other,
1675 they fail to match. */
1676 if (args1 == 0 || args2 == 0)
1677 return 0;
46df2823
JM
1678 mv1 = a1 = TREE_VALUE (args1);
1679 mv2 = a2 = TREE_VALUE (args2);
1680 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1681 mv1 = (TYPE_ATOMIC (mv1)
1682 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1683 TYPE_QUAL_ATOMIC)
1684 : TYPE_MAIN_VARIANT (mv1));
46df2823 1685 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1686 mv2 = (TYPE_ATOMIC (mv2)
1687 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1688 TYPE_QUAL_ATOMIC)
1689 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1690 /* A null pointer instead of a type
1691 means there is supposed to be an argument
1692 but nothing is specified about what type it has.
1693 So match anything that self-promotes. */
dc5027f4
JM
1694 if (different_types_p != NULL
1695 && (a1 == 0) != (a2 == 0))
1696 *different_types_p = true;
46df2823 1697 if (a1 == 0)
400fbf9f 1698 {
46df2823 1699 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1700 return 0;
1701 }
46df2823 1702 else if (a2 == 0)
400fbf9f 1703 {
46df2823 1704 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1705 return 0;
1706 }
8f5b6d29 1707 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1708 else if (TREE_CODE (a1) == ERROR_MARK
1709 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1710 ;
dc5027f4
JM
1711 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1712 different_types_p)))
400fbf9f 1713 {
dc5027f4
JM
1714 if (different_types_p != NULL)
1715 *different_types_p = true;
400fbf9f
JW
1716 /* Allow wait (union {union wait *u; int *i} *)
1717 and wait (union wait *) to be compatible. */
46df2823
JM
1718 if (TREE_CODE (a1) == UNION_TYPE
1719 && (TYPE_NAME (a1) == 0
ebf0bf7f 1720 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1721 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1722 && tree_int_cst_equal (TYPE_SIZE (a1),
1723 TYPE_SIZE (a2)))
400fbf9f
JW
1724 {
1725 tree memb;
46df2823 1726 for (memb = TYPE_FIELDS (a1);
910ad8de 1727 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1728 {
1729 tree mv3 = TREE_TYPE (memb);
1730 if (mv3 && mv3 != error_mark_node
1731 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1732 mv3 = (TYPE_ATOMIC (mv3)
1733 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1734 TYPE_QUAL_ATOMIC)
1735 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1736 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1737 different_types_p))
58cb41e6
JJ
1738 break;
1739 }
400fbf9f
JW
1740 if (memb == 0)
1741 return 0;
1742 }
46df2823
JM
1743 else if (TREE_CODE (a2) == UNION_TYPE
1744 && (TYPE_NAME (a2) == 0
ebf0bf7f 1745 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1746 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a2),
1748 TYPE_SIZE (a1)))
400fbf9f
JW
1749 {
1750 tree memb;
46df2823 1751 for (memb = TYPE_FIELDS (a2);
910ad8de 1752 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1753 {
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1759 TYPE_QUAL_ATOMIC)
1760 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1761 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1762 different_types_p))
58cb41e6
JJ
1763 break;
1764 }
400fbf9f
JW
1765 if (memb == 0)
1766 return 0;
1767 }
1768 else
1769 return 0;
1770 }
1771
1772 /* comptypes said ok, but record if it said to warn. */
1773 if (newval > val)
1774 val = newval;
1775
1776 args1 = TREE_CHAIN (args1);
1777 args2 = TREE_CHAIN (args2);
1778 }
1779}
400fbf9f 1780\f
a0e24419
MP
1781/* Compute the size to increment a pointer by. When a function type or void
1782 type or incomplete type is passed, size_one_node is returned.
1783 This function does not emit any diagnostics; the caller is responsible
1784 for that. */
400fbf9f 1785
4e2fb7de 1786static tree
58f9752a 1787c_size_in_bytes (const_tree type)
400fbf9f
JW
1788{
1789 enum tree_code code = TREE_CODE (type);
1790
a0e24419
MP
1791 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1792 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1793 return size_one_node;
1794
400fbf9f 1795 /* Convert in case a char is more than one unit. */
db3927fb
AH
1796 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1797 size_int (TYPE_PRECISION (char_type_node)
1798 / BITS_PER_UNIT));
400fbf9f 1799}
400fbf9f 1800\f
400fbf9f
JW
1801/* Return either DECL or its known constant value (if it has one). */
1802
56cb9733 1803tree
2f6e4e97 1804decl_constant_value (tree decl)
400fbf9f 1805{
a7c1916a 1806 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1807 in a place where a variable is invalid. Note that DECL_INITIAL
1808 isn't valid for a PARM_DECL. */
a7c1916a 1809 current_function_decl != 0
4f976745 1810 && TREE_CODE (decl) != PARM_DECL
3f75a254 1811 && !TREE_THIS_VOLATILE (decl)
83bab8db 1812 && TREE_READONLY (decl)
400fbf9f
JW
1813 && DECL_INITIAL (decl) != 0
1814 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1815 /* This is invalid if initial value is not constant.
1816 If it has either a function call, a memory reference,
1817 or a variable, then re-evaluating it could give different results. */
1818 && TREE_CONSTANT (DECL_INITIAL (decl))
1819 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1820 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1821 return DECL_INITIAL (decl);
1822 return decl;
1823}
1824
f2a71bbc
JM
1825/* Convert the array expression EXP to a pointer. */
1826static tree
c2255bc4 1827array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1828{
f2a71bbc 1829 tree orig_exp = exp;
207bf485 1830 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1831 tree adr;
1832 tree restype = TREE_TYPE (type);
1833 tree ptrtype;
207bf485 1834
f2a71bbc 1835 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1836
f2a71bbc 1837 STRIP_TYPE_NOPS (exp);
207bf485 1838
487a92fe
JM
1839 if (TREE_NO_WARNING (orig_exp))
1840 TREE_NO_WARNING (exp) = 1;
207bf485 1841
f2a71bbc
JM
1842 ptrtype = build_pointer_type (restype);
1843
1844 if (TREE_CODE (exp) == INDIRECT_REF)
1845 return convert (ptrtype, TREE_OPERAND (exp, 0));
1846
1f37c583
JM
1847 /* In C++ array compound literals are temporary objects unless they are
1848 const or appear in namespace scope, so they are destroyed too soon
1849 to use them for much of anything (c++/53220). */
1850 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1851 {
1852 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1853 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1854 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1855 "converting an array compound literal to a pointer "
1856 "is ill-formed in C++");
1857 }
1858
c2255bc4 1859 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1860 return convert (ptrtype, adr);
1861}
207bf485 1862
f2a71bbc
JM
1863/* Convert the function expression EXP to a pointer. */
1864static tree
c2255bc4 1865function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1866{
1867 tree orig_exp = exp;
207bf485 1868
f2a71bbc 1869 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1870
f2a71bbc 1871 STRIP_TYPE_NOPS (exp);
207bf485 1872
f2a71bbc
JM
1873 if (TREE_NO_WARNING (orig_exp))
1874 TREE_NO_WARNING (exp) = 1;
207bf485 1875
c2255bc4 1876 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1877}
207bf485 1878
ebfbbdc5
JJ
1879/* Mark EXP as read, not just set, for set but not used -Wunused
1880 warning purposes. */
1881
1882void
1883mark_exp_read (tree exp)
1884{
1885 switch (TREE_CODE (exp))
1886 {
1887 case VAR_DECL:
1888 case PARM_DECL:
1889 DECL_READ_P (exp) = 1;
1890 break;
1891 case ARRAY_REF:
1892 case COMPONENT_REF:
1893 case MODIFY_EXPR:
1894 case REALPART_EXPR:
1895 case IMAGPART_EXPR:
1896 CASE_CONVERT:
1897 case ADDR_EXPR:
1898 mark_exp_read (TREE_OPERAND (exp, 0));
1899 break;
1900 case COMPOUND_EXPR:
82c3c067 1901 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1902 mark_exp_read (TREE_OPERAND (exp, 1));
1903 break;
1904 default:
1905 break;
1906 }
1907}
1908
f2a71bbc
JM
1909/* Perform the default conversion of arrays and functions to pointers.
1910 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1911 return EXP.
1912
1913 LOC is the location of the expression. */
f2a71bbc
JM
1914
1915struct c_expr
c2255bc4 1916default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1917{
1918 tree orig_exp = exp.value;
1919 tree type = TREE_TYPE (exp.value);
1920 enum tree_code code = TREE_CODE (type);
1921
1922 switch (code)
1923 {
1924 case ARRAY_TYPE:
1925 {
1926 bool not_lvalue = false;
1927 bool lvalue_array_p;
1928
1929 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1930 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1931 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1932 {
1933 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1934 not_lvalue = true;
1935 exp.value = TREE_OPERAND (exp.value, 0);
1936 }
1937
1938 if (TREE_NO_WARNING (orig_exp))
1939 TREE_NO_WARNING (exp.value) = 1;
1940
1941 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1942 if (!flag_isoc99 && !lvalue_array_p)
1943 {
1944 /* Before C99, non-lvalue arrays do not decay to pointers.
1945 Normally, using such an array would be invalid; but it can
1946 be used correctly inside sizeof or as a statement expression.
1947 Thus, do not give an error here; an error will result later. */
1948 return exp;
1949 }
1950
c2255bc4 1951 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1952 }
1953 break;
1954 case FUNCTION_TYPE:
c2255bc4 1955 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1956 break;
1957 default:
f2a71bbc 1958 break;
207bf485 1959 }
f2a71bbc 1960
207bf485
JM
1961 return exp;
1962}
1963
ebfbbdc5
JJ
1964struct c_expr
1965default_function_array_read_conversion (location_t loc, struct c_expr exp)
1966{
1967 mark_exp_read (exp.value);
1968 return default_function_array_conversion (loc, exp);
1969}
522ddfa2 1970
267bac10
JM
1971/* Return whether EXPR should be treated as an atomic lvalue for the
1972 purposes of load and store handling. */
1973
1974static bool
1975really_atomic_lvalue (tree expr)
1976{
7a0ca710 1977 if (error_operand_p (expr))
267bac10
JM
1978 return false;
1979 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1980 return false;
1981 if (!lvalue_p (expr))
1982 return false;
1983
1984 /* Ignore _Atomic on register variables, since their addresses can't
1985 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1986 sequences wouldn't work. Ignore _Atomic on structures containing
1987 bit-fields, since accessing elements of atomic structures or
1988 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1989 it's undefined at translation time or execution time, and the
1990 normal atomic sequences again wouldn't work. */
1991 while (handled_component_p (expr))
1992 {
1993 if (TREE_CODE (expr) == COMPONENT_REF
1994 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1995 return false;
1996 expr = TREE_OPERAND (expr, 0);
1997 }
1998 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1999 return false;
2000 return true;
2001}
2002
2003/* Convert expression EXP (location LOC) from lvalue to rvalue,
2004 including converting functions and arrays to pointers if CONVERT_P.
2005 If READ_P, also mark the expression as having been read. */
2006
2007struct c_expr
2008convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2009 bool convert_p, bool read_p)
2010{
2011 if (read_p)
2012 mark_exp_read (exp.value);
2013 if (convert_p)
2014 exp = default_function_array_conversion (loc, exp);
2015 if (really_atomic_lvalue (exp.value))
2016 {
2017 vec<tree, va_gc> *params;
2018 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2019 tree expr_type = TREE_TYPE (exp.value);
2020 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2021 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2022
2023 gcc_assert (TYPE_ATOMIC (expr_type));
2024
2025 /* Expansion of a generic atomic load may require an addition
2026 element, so allocate enough to prevent a resize. */
2027 vec_alloc (params, 4);
2028
2029 /* Remove the qualifiers for the rest of the expressions and
2030 create the VAL temp variable to hold the RHS. */
2031 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2032 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2033 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2034 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2035 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2036
2037 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2038 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2039 params->quick_push (expr_addr);
2040 params->quick_push (tmp_addr);
2041 params->quick_push (seq_cst);
8edbfaa6 2042 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2043
cc28fc7f
MP
2044 /* EXPR is always read. */
2045 mark_exp_read (exp.value);
2046
267bac10 2047 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2048 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2049 NULL_TREE, NULL_TREE);
267bac10
JM
2050 }
2051 return exp;
2052}
2053
522ddfa2
JM
2054/* EXP is an expression of integer type. Apply the integer promotions
2055 to it and return the promoted value. */
400fbf9f
JW
2056
2057tree
522ddfa2 2058perform_integral_promotions (tree exp)
400fbf9f 2059{
b3694847
SS
2060 tree type = TREE_TYPE (exp);
2061 enum tree_code code = TREE_CODE (type);
400fbf9f 2062
522ddfa2 2063 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2064
400fbf9f
JW
2065 /* Normally convert enums to int,
2066 but convert wide enums to something wider. */
2067 if (code == ENUMERAL_TYPE)
2068 {
b0c48229
NB
2069 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2070 TYPE_PRECISION (integer_type_node)),
2071 ((TYPE_PRECISION (type)
2072 >= TYPE_PRECISION (integer_type_node))
8df83eae 2073 && TYPE_UNSIGNED (type)));
05bccae2 2074
400fbf9f
JW
2075 return convert (type, exp);
2076 }
2077
522ddfa2
JM
2078 /* ??? This should no longer be needed now bit-fields have their
2079 proper types. */
9753f113 2080 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2081 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2082 /* If it's thinner than an int, promote it like a
d72040f5 2083 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2084 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2085 TYPE_PRECISION (integer_type_node)))
f458d1d5 2086 return convert (integer_type_node, exp);
9753f113 2087
d72040f5 2088 if (c_promoting_integer_type_p (type))
400fbf9f 2089 {
f458d1d5 2090 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2091 if (TYPE_UNSIGNED (type)
f458d1d5 2092 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2093 return convert (unsigned_type_node, exp);
05bccae2 2094
400fbf9f
JW
2095 return convert (integer_type_node, exp);
2096 }
05bccae2 2097
522ddfa2
JM
2098 return exp;
2099}
2100
2101
2102/* Perform default promotions for C data used in expressions.
46bdb9cf 2103 Enumeral types or short or char are converted to int.
522ddfa2
JM
2104 In addition, manifest constants symbols are replaced by their values. */
2105
2106tree
2107default_conversion (tree exp)
2108{
2109 tree orig_exp;
2110 tree type = TREE_TYPE (exp);
2111 enum tree_code code = TREE_CODE (type);
40449a90 2112 tree promoted_type;
522ddfa2 2113
ebfbbdc5
JJ
2114 mark_exp_read (exp);
2115
46bdb9cf
JM
2116 /* Functions and arrays have been converted during parsing. */
2117 gcc_assert (code != FUNCTION_TYPE);
2118 if (code == ARRAY_TYPE)
2119 return exp;
522ddfa2
JM
2120
2121 /* Constants can be used directly unless they're not loadable. */
2122 if (TREE_CODE (exp) == CONST_DECL)
2123 exp = DECL_INITIAL (exp);
2124
522ddfa2
JM
2125 /* Strip no-op conversions. */
2126 orig_exp = exp;
2127 STRIP_TYPE_NOPS (exp);
2128
2129 if (TREE_NO_WARNING (orig_exp))
2130 TREE_NO_WARNING (exp) = 1;
2131
400fbf9f
JW
2132 if (code == VOID_TYPE)
2133 {
5436fa2e
MP
2134 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2135 "void value not ignored as it ought to be");
400fbf9f
JW
2136 return error_mark_node;
2137 }
808d6eaa
JM
2138
2139 exp = require_complete_type (exp);
2140 if (exp == error_mark_node)
2141 return error_mark_node;
2142
40449a90
SL
2143 promoted_type = targetm.promoted_type (type);
2144 if (promoted_type)
2145 return convert (promoted_type, exp);
2146
808d6eaa
JM
2147 if (INTEGRAL_TYPE_P (type))
2148 return perform_integral_promotions (exp);
2149
400fbf9f
JW
2150 return exp;
2151}
2152\f
0fb96aa4 2153/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2154
2155 If the component name is not found, returns NULL_TREE. Otherwise,
2156 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2157 stepping down the chain to the component, which is in the last
2158 TREE_VALUE of the list. Normally the list is of length one, but if
2159 the component is embedded within (nested) anonymous structures or
2160 unions, the list steps down the chain to the component. */
2f6e4e97 2161
2f2d13da 2162static tree
0fb96aa4 2163lookup_field (tree type, tree component)
2f2d13da
DE
2164{
2165 tree field;
2166
2167 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2168 to the field elements. Use a binary search on this array to quickly
2169 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2170 will always be set for structures which have many elements. */
2171
22a0b85f 2172 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2173 {
2174 int bot, top, half;
d07605f5 2175 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2176
2177 field = TYPE_FIELDS (type);
2178 bot = 0;
d07605f5 2179 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2180 while (top - bot > 1)
2181 {
2f2d13da
DE
2182 half = (top - bot + 1) >> 1;
2183 field = field_array[bot+half];
2184
2185 if (DECL_NAME (field) == NULL_TREE)
2186 {
2187 /* Step through all anon unions in linear fashion. */
2188 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2189 {
2f2d13da 2190 field = field_array[bot++];
a68b98cf
RK
2191 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2192 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2193 {
0fb96aa4 2194 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2195
2196 if (anon)
2197 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2198
2199 /* The Plan 9 compiler permits referring
2200 directly to an anonymous struct/union field
2201 using a typedef name. */
2202 if (flag_plan9_extensions
2203 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2204 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2205 == TYPE_DECL)
2206 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2207 == component))
2208 break;
2f6e4e97 2209 }
2f2d13da
DE
2210 }
2211
2212 /* Entire record is only anon unions. */
2213 if (bot > top)
2214 return NULL_TREE;
2215
2216 /* Restart the binary search, with new lower bound. */
2217 continue;
2218 }
2219
e8b87aac 2220 if (DECL_NAME (field) == component)
2f2d13da 2221 break;
e8b87aac 2222 if (DECL_NAME (field) < component)
2f2d13da
DE
2223 bot += half;
2224 else
2225 top = bot + half;
2226 }
2227
2228 if (DECL_NAME (field_array[bot]) == component)
2229 field = field_array[bot];
2230 else if (DECL_NAME (field) != component)
e9b2c823 2231 return NULL_TREE;
2f2d13da
DE
2232 }
2233 else
2234 {
910ad8de 2235 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2236 {
e9b2c823
NB
2237 if (DECL_NAME (field) == NULL_TREE
2238 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2239 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2240 {
0fb96aa4 2241 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2242
e9b2c823
NB
2243 if (anon)
2244 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2245
2246 /* The Plan 9 compiler permits referring directly to an
2247 anonymous struct/union field using a typedef
2248 name. */
2249 if (flag_plan9_extensions
2250 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2251 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2252 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2253 == component))
2254 break;
2f2d13da
DE
2255 }
2256
2257 if (DECL_NAME (field) == component)
2258 break;
2259 }
e9b2c823
NB
2260
2261 if (field == NULL_TREE)
2262 return NULL_TREE;
2f2d13da
DE
2263 }
2264
e9b2c823 2265 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2266}
2267
c2255bc4
AH
2268/* Make an expression to refer to the COMPONENT field of structure or
2269 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2270 location of the COMPONENT_REF. */
400fbf9f
JW
2271
2272tree
c2255bc4 2273build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2274{
b3694847
SS
2275 tree type = TREE_TYPE (datum);
2276 enum tree_code code = TREE_CODE (type);
2277 tree field = NULL;
2278 tree ref;
1e57bf47 2279 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2280
7a3ea201
RH
2281 if (!objc_is_public (datum, component))
2282 return error_mark_node;
2283
46a88c12 2284 /* Detect Objective-C property syntax object.property. */
668ea4b1 2285 if (c_dialect_objc ()
46a88c12 2286 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2287 return ref;
2288
400fbf9f
JW
2289 /* See if there is a field or component with name COMPONENT. */
2290
2291 if (code == RECORD_TYPE || code == UNION_TYPE)
2292 {
d0f062fb 2293 if (!COMPLETE_TYPE_P (type))
400fbf9f 2294 {
7a228918 2295 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2296 return error_mark_node;
2297 }
2298
0fb96aa4 2299 field = lookup_field (type, component);
400fbf9f
JW
2300
2301 if (!field)
2302 {
c2255bc4 2303 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2304 return error_mark_node;
2305 }
400fbf9f 2306
e9b2c823
NB
2307 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2308 This might be better solved in future the way the C++ front
2309 end does it - by giving the anonymous entities each a
2310 separate name and type, and then have build_component_ref
2311 recursively call itself. We can't do that here. */
46ea50cb 2312 do
19d76e60 2313 {
e9b2c823 2314 tree subdatum = TREE_VALUE (field);
efed193e
JM
2315 int quals;
2316 tree subtype;
1e57bf47 2317 bool use_datum_quals;
e9b2c823
NB
2318
2319 if (TREE_TYPE (subdatum) == error_mark_node)
2320 return error_mark_node;
2321
1e57bf47
JM
2322 /* If this is an rvalue, it does not have qualifiers in C
2323 standard terms and we must avoid propagating such
2324 qualifiers down to a non-lvalue array that is then
2325 converted to a pointer. */
2326 use_datum_quals = (datum_lvalue
2327 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2328
efed193e 2329 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2330 if (use_datum_quals)
2331 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2332 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2333
2334 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2335 NULL_TREE);
c2255bc4 2336 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2337 if (TREE_READONLY (subdatum)
2338 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2339 TREE_READONLY (ref) = 1;
1e57bf47
JM
2340 if (TREE_THIS_VOLATILE (subdatum)
2341 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2342 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2343
2344 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2345 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2346
19d76e60 2347 datum = ref;
46ea50cb
RS
2348
2349 field = TREE_CHAIN (field);
19d76e60 2350 }
46ea50cb 2351 while (field);
19d76e60 2352
400fbf9f
JW
2353 return ref;
2354 }
2355 else if (code != ERROR_MARK)
c2255bc4
AH
2356 error_at (loc,
2357 "request for member %qE in something not a structure or union",
2358 component);
400fbf9f
JW
2359
2360 return error_mark_node;
2361}
2362\f
2363/* Given an expression PTR for a pointer, return an expression
2364 for the value pointed to.
6a3799eb
AH
2365 ERRORSTRING is the name of the operator to appear in error messages.
2366
2367 LOC is the location to use for the generated tree. */
400fbf9f
JW
2368
2369tree
dd865ef6 2370build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2371{
b3694847
SS
2372 tree pointer = default_conversion (ptr);
2373 tree type = TREE_TYPE (pointer);
6a3799eb 2374 tree ref;
400fbf9f
JW
2375
2376 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2377 {
1043771b 2378 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2379 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2380 {
2381 /* If a warning is issued, mark it to avoid duplicates from
2382 the backend. This only needs to be done at
2383 warn_strict_aliasing > 2. */
2384 if (warn_strict_aliasing > 2)
2385 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2386 type, TREE_OPERAND (pointer, 0)))
2387 TREE_NO_WARNING (pointer) = 1;
2388 }
2389
870cc33b 2390 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2391 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2392 == TREE_TYPE (type)))
6a3799eb
AH
2393 {
2394 ref = TREE_OPERAND (pointer, 0);
2395 protected_set_expr_location (ref, loc);
2396 return ref;
2397 }
870cc33b
RS
2398 else
2399 {
2400 tree t = TREE_TYPE (type);
46df2823 2401
984dfd8c 2402 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2403
baae9b65 2404 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2405 {
d9b7be2e
MP
2406 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2407 {
2408 error_at (loc, "dereferencing pointer to incomplete type "
2409 "%qT", t);
2410 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2411 }
870cc33b
RS
2412 return error_mark_node;
2413 }
7d882b83 2414 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2415 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2416
2417 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2418 so that we get the proper error message if the result is used
2419 to assign to. Also, &* is supposed to be a no-op.
2420 And ANSI C seems to specify that the type of the result
2421 should be the const type. */
2422 /* A de-reference of a pointer to const is not a const. It is valid
2423 to change it via some other pointer. */
2424 TREE_READONLY (ref) = TYPE_READONLY (t);
2425 TREE_SIDE_EFFECTS (ref)
271bd540 2426 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2427 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2428 protected_set_expr_location (ref, loc);
870cc33b
RS
2429 return ref;
2430 }
2431 }
400fbf9f 2432 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2433 invalid_indirection_error (loc, type, errstring);
2434
400fbf9f
JW
2435 return error_mark_node;
2436}
2437
2438/* This handles expressions of the form "a[i]", which denotes
2439 an array reference.
2440
2441 This is logically equivalent in C to *(a+i), but we may do it differently.
2442 If A is a variable or a member, we generate a primitive ARRAY_REF.
2443 This avoids forcing the array out of registers, and can work on
2444 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2445 by functions).
2446
30cd1c5d
AS
2447 For vector types, allow vector[i] but not i[vector], and create
2448 *(((type*)&vectortype) + i) for the expression.
2449
6a3799eb 2450 LOC is the location to use for the returned expression. */
400fbf9f
JW
2451
2452tree
c2255bc4 2453build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2454{
6a3799eb 2455 tree ret;
a4ab7973 2456 bool swapped = false;
400fbf9f
JW
2457 if (TREE_TYPE (array) == error_mark_node
2458 || TREE_TYPE (index) == error_mark_node)
2459 return error_mark_node;
2460
b72271b9 2461 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2462 {
2463 size_t rank = 0;
2464 if (!find_rank (loc, index, index, true, &rank))
2465 return error_mark_node;
2466 if (rank > 1)
2467 {
2468 error_at (loc, "rank of the array's index is greater than 1");
2469 return error_mark_node;
2470 }
2471 }
a4ab7973 2472 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2473 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2474 /* Allow vector[index] but not index[vector]. */
2475 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2476 {
a4ab7973
JM
2477 tree temp;
2478 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2479 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2480 {
f90e8e2e 2481 error_at (loc,
30cd1c5d
AS
2482 "subscripted value is neither array nor pointer nor vector");
2483
fdeefd49
RS
2484 return error_mark_node;
2485 }
a4ab7973
JM
2486 temp = array;
2487 array = index;
2488 index = temp;
2489 swapped = true;
2490 }
2491
2492 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2493 {
a63068b6 2494 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2495 return error_mark_node;
2496 }
2497
2498 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2499 {
a63068b6 2500 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2501 return error_mark_node;
2502 }
2503
ff6b6641
GDR
2504 /* ??? Existing practice has been to warn only when the char
2505 index is syntactically the index, not for char[array]. */
2506 if (!swapped)
5bd012f8 2507 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2508
2509 /* Apply default promotions *after* noticing character types. */
2510 index = default_conversion (index);
f406ae1f
MP
2511 if (index == error_mark_node)
2512 return error_mark_node;
a4ab7973
JM
2513
2514 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2515
aa7da51a
JJ
2516 bool non_lvalue
2517 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2518
2519 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2520 {
e4d35515 2521 tree rval, type;
fdeefd49 2522
400fbf9f
JW
2523 /* An array that is indexed by a non-constant
2524 cannot be stored in a register; we must be able to do
2525 address arithmetic on its address.
2526 Likewise an array of elements of variable size. */
2527 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2528 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2529 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2530 {
dffd7eb6 2531 if (!c_mark_addressable (array))
400fbf9f
JW
2532 return error_mark_node;
2533 }
e6d52559
JW
2534 /* An array that is indexed by a constant value which is not within
2535 the array bounds cannot be stored in a register either; because we
2536 would get a crash in store_bit_field/extract_bit_field when trying
2537 to access a non-existent part of the register. */
2538 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2539 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2540 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2541 {
dffd7eb6 2542 if (!c_mark_addressable (array))
e6d52559
JW
2543 return error_mark_node;
2544 }
400fbf9f 2545
f3bede71 2546 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2547 {
2548 tree foo = array;
2549 while (TREE_CODE (foo) == COMPONENT_REF)
2550 foo = TREE_OPERAND (foo, 0);
5baeaac0 2551 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2552 pedwarn (loc, OPT_Wpedantic,
fcf73884 2553 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2554 else if (!lvalue_p (foo))
2555 pedwarn_c90 (loc, OPT_Wpedantic,
2556 "ISO C90 forbids subscripting non-lvalue "
2557 "array");
400fbf9f
JW
2558 }
2559
46df2823 2560 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2561 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2562 /* Array ref is const/volatile if the array elements are
c22cacf3 2563 or if the array is. */
400fbf9f
JW
2564 TREE_READONLY (rval)
2565 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2566 | TREE_READONLY (array));
2567 TREE_SIDE_EFFECTS (rval)
2568 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2569 | TREE_SIDE_EFFECTS (array));
2570 TREE_THIS_VOLATILE (rval)
2571 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2572 /* This was added by rms on 16 Nov 91.
2f6e4e97 2573 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2574 in an inline function.
2575 Hope it doesn't break something else. */
2576 | TREE_THIS_VOLATILE (array));
928c19bb 2577 ret = require_complete_type (rval);
6a3799eb 2578 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2579 if (non_lvalue)
2580 ret = non_lvalue_loc (loc, ret);
6a3799eb 2581 return ret;
400fbf9f 2582 }
a4ab7973
JM
2583 else
2584 {
2585 tree ar = default_conversion (array);
400fbf9f 2586
a4ab7973
JM
2587 if (ar == error_mark_node)
2588 return ar;
400fbf9f 2589
a4ab7973
JM
2590 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2591 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2592
aa7da51a
JJ
2593 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2594 index, 0),
2595 RO_ARRAY_INDEXING);
2596 if (non_lvalue)
2597 ret = non_lvalue_loc (loc, ret);
2598 return ret;
a4ab7973 2599 }
400fbf9f
JW
2600}
2601\f
7e585d16 2602/* Build an external reference to identifier ID. FUN indicates
766beb40 2603 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2604 location of the identifier. This sets *TYPE to the type of the
2605 identifier, which is not the same as the type of the returned value
2606 for CONST_DECLs defined as enum constants. If the type of the
2607 identifier is not available, *TYPE is set to NULL. */
7e585d16 2608tree
c2255bc4 2609build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2610{
2611 tree ref;
2612 tree decl = lookup_name (id);
16b34ad6
ZL
2613
2614 /* In Objective-C, an instance variable (ivar) may be preferred to
2615 whatever lookup_name() found. */
2616 decl = objc_lookup_ivar (decl, id);
7e585d16 2617
6866c6e8 2618 *type = NULL;
339a28b9 2619 if (decl && decl != error_mark_node)
6866c6e8
ILT
2620 {
2621 ref = decl;
2622 *type = TREE_TYPE (ref);
2623 }
339a28b9
ZW
2624 else if (fun)
2625 /* Implicit function declaration. */
c2255bc4 2626 ref = implicitly_declare (loc, id);
339a28b9
ZW
2627 else if (decl == error_mark_node)
2628 /* Don't complain about something that's already been
2629 complained about. */
2630 return error_mark_node;
2631 else
2632 {
c2255bc4 2633 undeclared_variable (loc, id);
339a28b9
ZW
2634 return error_mark_node;
2635 }
7e585d16
ZW
2636
2637 if (TREE_TYPE (ref) == error_mark_node)
2638 return error_mark_node;
2639
339a28b9 2640 if (TREE_DEPRECATED (ref))
9b86d6bb 2641 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2642
ad960f56 2643 /* Recursive call does not count as usage. */
b8698a0f 2644 if (ref != current_function_decl)
ad960f56 2645 {
ad960f56
MLI
2646 TREE_USED (ref) = 1;
2647 }
7e585d16 2648
bc4b653b
JM
2649 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2650 {
2651 if (!in_sizeof && !in_typeof)
2652 C_DECL_USED (ref) = 1;
2653 else if (DECL_INITIAL (ref) == 0
2654 && DECL_EXTERNAL (ref)
2655 && !TREE_PUBLIC (ref))
2656 record_maybe_used_decl (ref);
2657 }
2658
7e585d16
ZW
2659 if (TREE_CODE (ref) == CONST_DECL)
2660 {
6193b8b7 2661 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2662
2663 if (warn_cxx_compat
2664 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2665 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2666 {
2667 warning_at (loc, OPT_Wc___compat,
2668 ("enum constant defined in struct or union "
2669 "is not visible in C++"));
2670 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2671 }
2672
7e585d16
ZW
2673 ref = DECL_INITIAL (ref);
2674 TREE_CONSTANT (ref) = 1;
2675 }
6a29edea 2676 else if (current_function_decl != 0
4b1e44be 2677 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2678 && (TREE_CODE (ref) == VAR_DECL
2679 || TREE_CODE (ref) == PARM_DECL
2680 || TREE_CODE (ref) == FUNCTION_DECL))
2681 {
2682 tree context = decl_function_context (ref);
2f6e4e97 2683
6a29edea
EB
2684 if (context != 0 && context != current_function_decl)
2685 DECL_NONLOCAL (ref) = 1;
2686 }
71113fcd
GK
2687 /* C99 6.7.4p3: An inline definition of a function with external
2688 linkage ... shall not contain a reference to an identifier with
2689 internal linkage. */
2690 else if (current_function_decl != 0
2691 && DECL_DECLARED_INLINE_P (current_function_decl)
2692 && DECL_EXTERNAL (current_function_decl)
2693 && VAR_OR_FUNCTION_DECL_P (ref)
2694 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2695 && ! TREE_PUBLIC (ref)
2696 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2697 record_inline_static (loc, current_function_decl, ref,
2698 csi_internal);
7e585d16
ZW
2699
2700 return ref;
2701}
2702
bc4b653b
JM
2703/* Record details of decls possibly used inside sizeof or typeof. */
2704struct maybe_used_decl
2705{
2706 /* The decl. */
2707 tree decl;
2708 /* The level seen at (in_sizeof + in_typeof). */
2709 int level;
2710 /* The next one at this level or above, or NULL. */
2711 struct maybe_used_decl *next;
2712};
2713
2714static struct maybe_used_decl *maybe_used_decls;
2715
2716/* Record that DECL, an undefined static function reference seen
2717 inside sizeof or typeof, might be used if the operand of sizeof is
2718 a VLA type or the operand of typeof is a variably modified
2719 type. */
2720
4e2fb7de 2721static void
bc4b653b
JM
2722record_maybe_used_decl (tree decl)
2723{
2724 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2725 t->decl = decl;
2726 t->level = in_sizeof + in_typeof;
2727 t->next = maybe_used_decls;
2728 maybe_used_decls = t;
2729}
2730
2731/* Pop the stack of decls possibly used inside sizeof or typeof. If
2732 USED is false, just discard them. If it is true, mark them used
2733 (if no longer inside sizeof or typeof) or move them to the next
2734 level up (if still inside sizeof or typeof). */
2735
2736void
2737pop_maybe_used (bool used)
2738{
2739 struct maybe_used_decl *p = maybe_used_decls;
2740 int cur_level = in_sizeof + in_typeof;
2741 while (p && p->level > cur_level)
2742 {
2743 if (used)
2744 {
2745 if (cur_level == 0)
2746 C_DECL_USED (p->decl) = 1;
2747 else
2748 p->level = cur_level;
2749 }
2750 p = p->next;
2751 }
2752 if (!used || cur_level == 0)
2753 maybe_used_decls = p;
2754}
2755
2756/* Return the result of sizeof applied to EXPR. */
2757
2758struct c_expr
c2255bc4 2759c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2760{
2761 struct c_expr ret;
ad97f4be
JM
2762 if (expr.value == error_mark_node)
2763 {
2764 ret.value = error_mark_node;
2765 ret.original_code = ERROR_MARK;
6866c6e8 2766 ret.original_type = NULL;
ad97f4be
JM
2767 pop_maybe_used (false);
2768 }
2769 else
2770 {
928c19bb 2771 bool expr_const_operands = true;
773ec47f
MP
2772
2773 if (TREE_CODE (expr.value) == PARM_DECL
2774 && C_ARRAY_PARAMETER (expr.value))
2775 {
2776 if (warning_at (loc, OPT_Wsizeof_array_argument,
2777 "%<sizeof%> on array function parameter %qE will "
2778 "return size of %qT", expr.value,
2779 expr.original_type))
2780 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2781 }
928c19bb
JM
2782 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2783 &expr_const_operands);
c2255bc4 2784 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2785 c_last_sizeof_arg = expr.value;
2786 ret.original_code = SIZEOF_EXPR;
6866c6e8 2787 ret.original_type = NULL;
928c19bb 2788 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2789 {
2790 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2791 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2792 folded_expr, ret.value);
2793 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2794 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2795 }
928c19bb 2796 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2797 }
bc4b653b
JM
2798 return ret;
2799}
2800
2801/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2802 name passed to sizeof (rather than the type itself). LOC is the
2803 location of the original expression. */
bc4b653b
JM
2804
2805struct c_expr
c2255bc4 2806c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2807{
2808 tree type;
2809 struct c_expr ret;
928c19bb
JM
2810 tree type_expr = NULL_TREE;
2811 bool type_expr_const = true;
2812 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2813 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2814 c_last_sizeof_arg = type;
2815 ret.original_code = SIZEOF_EXPR;
6866c6e8 2816 ret.original_type = NULL;
24070fcb
JM
2817 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2818 && c_vla_type_p (type))
928c19bb 2819 {
24070fcb
JM
2820 /* If the type is a [*] array, it is a VLA but is represented as
2821 having a size of zero. In such a case we must ensure that
2822 the result of sizeof does not get folded to a constant by
2823 c_fully_fold, because if the size is evaluated the result is
2824 not constant and so constraints on zero or negative size
2825 arrays must not be applied when this sizeof call is inside
2826 another array declarator. */
2827 if (!type_expr)
2828 type_expr = integer_zero_node;
928c19bb
JM
2829 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2830 type_expr, ret.value);
2831 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2832 }
16464cc1
VR
2833 pop_maybe_used (type != error_mark_node
2834 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2835 return ret;
2836}
2837
400fbf9f 2838/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2839 The function call is at LOC.
400fbf9f
JW
2840 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2841 TREE_VALUE of each node is a parameter-expression.
2842 FUNCTION's data type may be a function type or a pointer-to-function. */
2843
2844tree
c2255bc4 2845build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2846{
9771b263 2847 vec<tree, va_gc> *v;
bbbbb16a
ILT
2848 tree ret;
2849
9771b263 2850 vec_alloc (v, list_length (params));
bbbbb16a 2851 for (; params; params = TREE_CHAIN (params))
9771b263 2852 v->quick_push (TREE_VALUE (params));
8edbfaa6 2853 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2854 vec_free (v);
bbbbb16a
ILT
2855 return ret;
2856}
2857
ae52741c
MLI
2858/* Give a note about the location of the declaration of DECL. */
2859
2860static void inform_declaration (tree decl)
2861{
2862 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2863 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2864}
2865
bbbbb16a
ILT
2866/* Build a function call to function FUNCTION with parameters PARAMS.
2867 ORIGTYPES, if not NULL, is a vector of types; each element is
2868 either NULL or the original type of the corresponding element in
2869 PARAMS. The original type may differ from TREE_TYPE of the
2870 parameter for enums. FUNCTION's data type may be a function type
2871 or pointer-to-function. This function changes the elements of
2872 PARAMS. */
2873
2874tree
81e5eca8
MP
2875build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2876 tree function, vec<tree, va_gc> *params,
9771b263 2877 vec<tree, va_gc> *origtypes)
400fbf9f 2878{
b3694847 2879 tree fntype, fundecl = 0;
4977bab6 2880 tree name = NULL_TREE, result;
c96f4f73 2881 tree tem;
94a0dd7b
SL
2882 int nargs;
2883 tree *argarray;
b8698a0f 2884
400fbf9f 2885
fc76e425 2886 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2887 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2888
2889 /* Convert anything with function type to a pointer-to-function. */
2890 if (TREE_CODE (function) == FUNCTION_DECL)
2891 {
2892 name = DECL_NAME (function);
0a35513e
AH
2893
2894 if (flag_tm)
2895 tm_malloc_replacement (function);
a5eadacc 2896 fundecl = function;
86951993
AM
2897 /* Atomic functions have type checking/casting already done. They are
2898 often rewritten and don't match the original parameter list. */
2899 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2900 origtypes = NULL;
36536d79 2901
b72271b9 2902 if (flag_cilkplus
36536d79
BI
2903 && is_cilkplus_reduce_builtin (function))
2904 origtypes = NULL;
400fbf9f 2905 }
f2a71bbc 2906 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2907 function = function_to_pointer_conversion (loc, function);
400fbf9f 2908
6e955430
ZL
2909 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2910 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2911 if (params && !params->is_empty ())
2912 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2913
928c19bb
JM
2914 function = c_fully_fold (function, false, NULL);
2915
400fbf9f
JW
2916 fntype = TREE_TYPE (function);
2917
2918 if (TREE_CODE (fntype) == ERROR_MARK)
2919 return error_mark_node;
2920
2921 if (!(TREE_CODE (fntype) == POINTER_TYPE
2922 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2923 {
ae52741c
MLI
2924 if (!flag_diagnostics_show_caret)
2925 error_at (loc,
2926 "called object %qE is not a function or function pointer",
2927 function);
2928 else if (DECL_P (function))
2929 {
2930 error_at (loc,
2931 "called object %qD is not a function or function pointer",
2932 function);
2933 inform_declaration (function);
2934 }
2935 else
2936 error_at (loc,
2937 "called object is not a function or function pointer");
400fbf9f
JW
2938 return error_mark_node;
2939 }
2940
5ce89b2e
JM
2941 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2942 current_function_returns_abnormally = 1;
2943
400fbf9f
JW
2944 /* fntype now gets the type of function pointed to. */
2945 fntype = TREE_TYPE (fntype);
2946
ab4194da
JM
2947 /* Convert the parameters to the types declared in the
2948 function prototype, or apply default promotions. */
2949
81e5eca8
MP
2950 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2951 origtypes, function, fundecl);
ab4194da
JM
2952 if (nargs < 0)
2953 return error_mark_node;
2954
c96f4f73 2955 /* Check that the function is called through a compatible prototype.
fa337f3a 2956 If it is not, warn. */
1043771b 2957 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2958 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2959 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2960 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2961 {
2962 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2963
2964 /* This situation leads to run-time undefined behavior. We can't,
2965 therefore, simply error unless we can prove that all possible
2966 executions of the program must execute the code. */
fa337f3a 2967 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2968
fa337f3a
RB
2969 if (VOID_TYPE_P (return_type)
2970 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2971 pedwarn (loc, 0,
2972 "function with qualified void return type called");
2973 }
c96f4f73 2974
9771b263 2975 argarray = vec_safe_address (params);
bbbbb16a 2976
83322951
RG
2977 /* Check that arguments to builtin functions match the expectations. */
2978 if (fundecl
2979 && DECL_BUILT_IN (fundecl)
2980 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2981 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2982 return error_mark_node;
400fbf9f 2983
83322951 2984 /* Check that the arguments to the function are valid. */
dde05067 2985 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2986
928c19bb
JM
2987 if (name != NULL_TREE
2988 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2989 {
928c19bb 2990 if (require_constant_value)
b8698a0f 2991 result =
db3927fb
AH
2992 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2993 function, nargs, argarray);
928c19bb 2994 else
db3927fb
AH
2995 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2996 function, nargs, argarray);
928c19bb
JM
2997 if (TREE_CODE (result) == NOP_EXPR
2998 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2999 STRIP_TYPE_NOPS (result);
bf730f15
RS
3000 }
3001 else
db3927fb
AH
3002 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3003 function, nargs, argarray);
b0b3afb2 3004
71653180 3005 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3006 {
3007 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3008 pedwarn (loc, 0,
3ce62965
JM
3009 "function with qualified void return type called");
3010 return result;
3011 }
1eb8759b 3012 return require_complete_type (result);
400fbf9f 3013}
8edbfaa6
JJ
3014
3015/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3016
3017tree
3018c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3019 tree function, vec<tree, va_gc> *params,
3020 vec<tree, va_gc> *origtypes)
3021{
3022 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3023 STRIP_TYPE_NOPS (function);
3024
3025 /* Convert anything with function type to a pointer-to-function. */
3026 if (TREE_CODE (function) == FUNCTION_DECL)
3027 {
3028 /* Implement type-directed function overloading for builtins.
3029 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3030 handle all the type checking. The result is a complete expression
3031 that implements this function call. */
3032 tree tem = resolve_overloaded_builtin (loc, function, params);
3033 if (tem)
3034 return tem;
3035 }
3036 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3037}
400fbf9f 3038\f
bbbbb16a
ILT
3039/* Convert the argument expressions in the vector VALUES
3040 to the types in the list TYPELIST.
400fbf9f
JW
3041
3042 If TYPELIST is exhausted, or when an element has NULL as its type,
3043 perform the default conversions.
3044
bbbbb16a
ILT
3045 ORIGTYPES is the original types of the expressions in VALUES. This
3046 holds the type of enum values which have been converted to integral
3047 types. It may be NULL.
400fbf9f 3048
03dafa61
JM
3049 FUNCTION is a tree for the called function. It is used only for
3050 error messages, where it is formatted with %qE.
400fbf9f
JW
3051
3052 This is also where warnings about wrong number of args are generated.
3053
81e5eca8
MP
3054 ARG_LOC are locations of function arguments (if any).
3055
94a0dd7b 3056 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3057 than the length of VALUES in some error situations), or -1 on
3058 failure. */
94a0dd7b
SL
3059
3060static int
81e5eca8
MP
3061convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3062 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3063 tree function, tree fundecl)
400fbf9f 3064{
bbbbb16a
ILT
3065 tree typetail, val;
3066 unsigned int parmnum;
06302a02 3067 bool error_args = false;
b5d32c25 3068 const bool type_generic = fundecl
81e5eca8 3069 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3070 bool type_generic_remove_excess_precision = false;
03dafa61 3071 tree selector;
03dafa61 3072
2ac2f164
JM
3073 /* Change pointer to function to the function itself for
3074 diagnostics. */
03dafa61
JM
3075 if (TREE_CODE (function) == ADDR_EXPR
3076 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3077 function = TREE_OPERAND (function, 0);
03dafa61
JM
3078
3079 /* Handle an ObjC selector specially for diagnostics. */
3080 selector = objc_message_selector ();
400fbf9f 3081
8ce94e44
JM
3082 /* For type-generic built-in functions, determine whether excess
3083 precision should be removed (classification) or not
3084 (comparison). */
3085 if (type_generic
3086 && DECL_BUILT_IN (fundecl)
3087 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3088 {
3089 switch (DECL_FUNCTION_CODE (fundecl))
3090 {
3091 case BUILT_IN_ISFINITE:
3092 case BUILT_IN_ISINF:
3093 case BUILT_IN_ISINF_SIGN:
3094 case BUILT_IN_ISNAN:
3095 case BUILT_IN_ISNORMAL:
3096 case BUILT_IN_FPCLASSIFY:
3097 type_generic_remove_excess_precision = true;
3098 break;
3099
3100 default:
3101 type_generic_remove_excess_precision = false;
3102 break;
3103 }
3104 }
b72271b9 3105 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3106 return vec_safe_length (values);
8ce94e44 3107
400fbf9f 3108 /* Scan the given expressions and types, producing individual
bbbbb16a 3109 converted arguments. */
400fbf9f 3110
bbbbb16a 3111 for (typetail = typelist, parmnum = 0;
9771b263 3112 values && values->iterate (parmnum, &val);
bbbbb16a 3113 ++parmnum)
400fbf9f 3114 {
b3694847 3115 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3116 tree valtype = TREE_TYPE (val);
03dafa61
JM
3117 tree rname = function;
3118 int argnum = parmnum + 1;
4d3e6fae 3119 const char *invalid_func_diag;
8ce94e44 3120 bool excess_precision = false;
928c19bb 3121 bool npc;
bbbbb16a 3122 tree parmval;
5c1bc275
MP
3123 /* Some __atomic_* builtins have additional hidden argument at
3124 position 0. */
3125 location_t ploc
3126 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3127 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3128 : input_location;
400fbf9f
JW
3129
3130 if (type == void_type_node)
3131 {
19dc6d01 3132 if (selector)
68fca595 3133 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3134 else
68fca595 3135 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3136 inform_declaration (fundecl);
d38f7dce 3137 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3138 }
3139
03dafa61
JM
3140 if (selector && argnum > 2)
3141 {
3142 rname = selector;
3143 argnum -= 2;
3144 }
3145
928c19bb 3146 npc = null_pointer_constant_p (val);
8ce94e44
JM
3147
3148 /* If there is excess precision and a prototype, convert once to
3149 the required type rather than converting via the semantic
3150 type. Likewise without a prototype a float value represented
3151 as long double should be converted once to double. But for
3152 type-generic classification functions excess precision must
3153 be removed here. */
3154 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3155 && (type || !type_generic || !type_generic_remove_excess_precision))
3156 {
3157 val = TREE_OPERAND (val, 0);
3158 excess_precision = true;
3159 }
928c19bb 3160 val = c_fully_fold (val, false, NULL);
ed248cf7 3161 STRIP_TYPE_NOPS (val);
400fbf9f 3162
400fbf9f
JW
3163 val = require_complete_type (val);
3164
3165 if (type != 0)
3166 {
3167 /* Formal parm type is specified by a function prototype. */
400fbf9f 3168
20913689 3169 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3170 {
5c1bc275
MP
3171 error_at (ploc, "type of formal parameter %d is incomplete",
3172 parmnum + 1);
400fbf9f
JW
3173 parmval = val;
3174 }
3175 else
3176 {
bbbbb16a
ILT
3177 tree origtype;
3178
d45cf215
RS
3179 /* Optionally warn about conversions that
3180 differ from the default conversions. */
05170031 3181 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3182 {
e3a64162 3183 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3184
aae43c5f 3185 if (INTEGRAL_TYPE_P (type)
8ce94e44 3186 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3187 warning_at (ploc, OPT_Wtraditional_conversion,
3188 "passing argument %d of %qE as integer rather "
3189 "than floating due to prototype",
3190 argnum, rname);
03829ad2 3191 if (INTEGRAL_TYPE_P (type)
8ce94e44 3192 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3193 warning_at (ploc, OPT_Wtraditional_conversion,
3194 "passing argument %d of %qE as integer rather "
3195 "than complex due to prototype",
3196 argnum, rname);
aae43c5f 3197 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3198 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3199 warning_at (ploc, OPT_Wtraditional_conversion,
3200 "passing argument %d of %qE as complex rather "
3201 "than floating due to prototype",
3202 argnum, rname);
400fbf9f 3203 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3204 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3205 warning_at (ploc, OPT_Wtraditional_conversion,
3206 "passing argument %d of %qE as floating rather "
3207 "than integer due to prototype",
3208 argnum, rname);
03829ad2 3209 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3210 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3211 warning_at (ploc, OPT_Wtraditional_conversion,
3212 "passing argument %d of %qE as complex rather "
3213 "than integer due to prototype",
3214 argnum, rname);
aae43c5f 3215 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3216 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3217 warning_at (ploc, OPT_Wtraditional_conversion,
3218 "passing argument %d of %qE as floating rather "
3219 "than complex due to prototype",
3220 argnum, rname);
aae43c5f
RK
3221 /* ??? At some point, messages should be written about
3222 conversions between complex types, but that's too messy
3223 to do now. */
d45cf215 3224 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3225 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3226 {
3227 /* Warn if any argument is passed as `float',
047de90b 3228 since without a prototype it would be `double'. */
9a8ce21f
JG
3229 if (formal_prec == TYPE_PRECISION (float_type_node)
3230 && type != dfloat32_type_node)
5c1bc275
MP
3231 warning_at (ploc, 0,
3232 "passing argument %d of %qE as %<float%> "
3233 "rather than %<double%> due to prototype",
3234 argnum, rname);
9a8ce21f
JG
3235
3236 /* Warn if mismatch between argument and prototype
3237 for decimal float types. Warn of conversions with
3238 binary float types and of precision narrowing due to
3239 prototype. */
8ce94e44 3240 else if (type != valtype
9a8ce21f
JG
3241 && (type == dfloat32_type_node
3242 || type == dfloat64_type_node
c22cacf3 3243 || type == dfloat128_type_node
8ce94e44
JM
3244 || valtype == dfloat32_type_node
3245 || valtype == dfloat64_type_node
3246 || valtype == dfloat128_type_node)
c22cacf3 3247 && (formal_prec
8ce94e44 3248 <= TYPE_PRECISION (valtype)
9a8ce21f 3249 || (type == dfloat128_type_node
8ce94e44 3250 && (valtype
c22cacf3 3251 != dfloat64_type_node
8ce94e44 3252 && (valtype
9a8ce21f
JG
3253 != dfloat32_type_node)))
3254 || (type == dfloat64_type_node
8ce94e44 3255 && (valtype
9a8ce21f 3256 != dfloat32_type_node))))
5c1bc275
MP
3257 warning_at (ploc, 0,
3258 "passing argument %d of %qE as %qT "
3259 "rather than %qT due to prototype",
3260 argnum, rname, type, valtype);
9a8ce21f 3261
d45cf215 3262 }
3ed56f8a
KG
3263 /* Detect integer changing in width or signedness.
3264 These warnings are only activated with
05170031
MLI
3265 -Wtraditional-conversion, not with -Wtraditional. */
3266 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3267 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3268 {
d45cf215
RS
3269 tree would_have_been = default_conversion (val);
3270 tree type1 = TREE_TYPE (would_have_been);
3271
754a4d82 3272 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3273 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3274 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3275 /* No warning if function asks for enum
3276 and the actual arg is that enum type. */
3277 ;
3278 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3279 warning_at (ploc, OPT_Wtraditional_conversion,
3280 "passing argument %d of %qE "
3281 "with different width due to prototype",
3282 argnum, rname);
8df83eae 3283 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3284 ;
800cd3b9
RS
3285 /* Don't complain if the formal parameter type
3286 is an enum, because we can't tell now whether
3287 the value was an enum--even the same enum. */
3288 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3289 ;
400fbf9f
JW
3290 else if (TREE_CODE (val) == INTEGER_CST
3291 && int_fits_type_p (val, type))
3292 /* Change in signedness doesn't matter
3293 if a constant value is unaffected. */
3294 ;
ce9895ae
RS
3295 /* If the value is extended from a narrower
3296 unsigned type, it doesn't matter whether we
3297 pass it as signed or unsigned; the value
3298 certainly is the same either way. */
8ce94e44
JM
3299 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3300 && TYPE_UNSIGNED (valtype))
ce9895ae 3301 ;
8df83eae 3302 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3303 warning_at (ploc, OPT_Wtraditional_conversion,
3304 "passing argument %d of %qE "
3305 "as unsigned due to prototype",
3306 argnum, rname);
3ed56f8a 3307 else
5c1bc275
MP
3308 warning_at (ploc, OPT_Wtraditional_conversion,
3309 "passing argument %d of %qE "
3310 "as signed due to prototype",
3311 argnum, rname);
400fbf9f
JW
3312 }
3313 }
3314
8ce94e44
JM
3315 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3316 sake of better warnings from convert_and_check. */
3317 if (excess_precision)
3318 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3319 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3320 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3321 val, origtype, ic_argpass,
3322 npc, fundecl, function,
2ac2f164 3323 parmnum + 1);
2f6e4e97 3324
61f71b34 3325 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3326 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3327 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3328 parmval = default_conversion (parmval);
400fbf9f 3329 }
400fbf9f 3330 }
8ce94e44
JM
3331 else if (TREE_CODE (valtype) == REAL_TYPE
3332 && (TYPE_PRECISION (valtype)
2531a1d9 3333 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3334 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3335 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3336 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3337 {
3338 if (type_generic)
bbbbb16a 3339 parmval = val;
b5d32c25 3340 else
0a0b3574
MM
3341 {
3342 /* Convert `float' to `double'. */
3343 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3344 warning_at (ploc, OPT_Wdouble_promotion,
3345 "implicit conversion from %qT to %qT when passing "
3346 "argument to function",
3347 valtype, double_type_node);
0a0b3574
MM
3348 parmval = convert (double_type_node, val);
3349 }
b5d32c25 3350 }
8ce94e44
JM
3351 else if (excess_precision && !type_generic)
3352 /* A "double" argument with excess precision being passed
3353 without a prototype or in variable arguments. */
bbbbb16a 3354 parmval = convert (valtype, val);
c22cacf3
MS
3355 else if ((invalid_func_diag =
3356 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3357 {
3358 error (invalid_func_diag);
94a0dd7b 3359 return -1;
4d3e6fae 3360 }
400fbf9f
JW
3361 else
3362 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3363 parmval = default_conversion (val);
3364
9771b263 3365 (*values)[parmnum] = parmval;
06302a02
JJ
3366 if (parmval == error_mark_node)
3367 error_args = true;
400fbf9f
JW
3368
3369 if (typetail)
3370 typetail = TREE_CHAIN (typetail);
3371 }
3372
9771b263 3373 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3374
400fbf9f 3375 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3376 {
68fca595 3377 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3378 inform_declaration (fundecl);
3379 return -1;
3789b316 3380 }
400fbf9f 3381
06302a02 3382 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3383}
3384\f
43f6dfd3
RS
3385/* This is the entry point used by the parser to build unary operators
3386 in the input. CODE, a tree_code, specifies the unary operator, and
3387 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3388 CONVERT_EXPR for code.
3389
3390 LOC is the location to use for the tree generated.
3391*/
43f6dfd3
RS
3392
3393struct c_expr
c2255bc4 3394parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3395{
3396 struct c_expr result;
3397
c9f9eb5d 3398 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3399 result.original_code = code;
6866c6e8
ILT
3400 result.original_type = NULL;
3401
59c0753d 3402 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3403 overflow_warning (loc, result.value);
59c0753d 3404
43f6dfd3
RS
3405 return result;
3406}
3407
3408/* This is the entry point used by the parser to build binary operators
3409 in the input. CODE, a tree_code, specifies the binary operator, and
3410 ARG1 and ARG2 are the operands. In addition to constructing the
3411 expression, we check for operands that were written with other binary
ba47d38d
AH
3412 operators in a way that is likely to confuse the user.
3413
3414 LOCATION is the location of the binary operator. */
edc7c4ec 3415
487a92fe 3416struct c_expr
ba47d38d
AH
3417parser_build_binary_op (location_t location, enum tree_code code,
3418 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3419{
487a92fe 3420 struct c_expr result;
400fbf9f 3421
487a92fe
JM
3422 enum tree_code code1 = arg1.original_code;
3423 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3424 tree type1 = (arg1.original_type
3425 ? arg1.original_type
3426 : TREE_TYPE (arg1.value));
3427 tree type2 = (arg2.original_type
3428 ? arg2.original_type
3429 : TREE_TYPE (arg2.value));
400fbf9f 3430
ba47d38d
AH
3431 result.value = build_binary_op (location, code,
3432 arg1.value, arg2.value, 1);
487a92fe 3433 result.original_code = code;
6866c6e8 3434 result.original_type = NULL;
58bf601b 3435
487a92fe
JM
3436 if (TREE_CODE (result.value) == ERROR_MARK)
3437 return result;
400fbf9f 3438
ba47d38d
AH
3439 if (location != UNKNOWN_LOCATION)
3440 protected_set_expr_location (result.value, location);
3441
400fbf9f 3442 /* Check for cases such as x+y<<z which users are likely
487a92fe 3443 to misinterpret. */
400fbf9f 3444 if (warn_parentheses)
393e8e8b
MP
3445 warn_about_parentheses (location, code, code1, arg1.value, code2,
3446 arg2.value);
001af587 3447
ca409efd 3448 if (warn_logical_op)
393e8e8b 3449 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3450 code1, arg1.value, code2, arg2.value);
63a08740 3451
742938c9 3452 if (warn_logical_not_paren
7ccb1a11 3453 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3454 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3455 && code2 != TRUTH_NOT_EXPR
3456 /* Avoid warning for !!x == y. */
3457 && (TREE_CODE (arg1.value) != NE_EXPR
3458 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3459 {
3460 /* Avoid warning for !b == y where b has _Bool type. */
3461 tree t = integer_zero_node;
3462 if (TREE_CODE (arg1.value) == EQ_EXPR
3463 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3464 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3465 {
3466 t = TREE_OPERAND (arg1.value, 0);
3467 do
3468 {
3469 if (TREE_TYPE (t) != integer_type_node)
3470 break;
3471 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3472 t = C_MAYBE_CONST_EXPR_EXPR (t);
3473 else if (CONVERT_EXPR_P (t))
3474 t = TREE_OPERAND (t, 0);
3475 else
3476 break;
3477 }
3478 while (1);
3479 }
3480 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3481 warn_logical_not_parentheses (location, code, arg2.value);
3482 }
742938c9 3483
e994a705
RS
3484 /* Warn about comparisons against string literals, with the exception
3485 of testing for equality or inequality of a string literal with NULL. */
3486 if (code == EQ_EXPR || code == NE_EXPR)
3487 {
3488 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3489 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3490 warning_at (location, OPT_Waddress,
3491 "comparison with string literal results in unspecified behavior");
e994a705
RS
3492 }
3493 else if (TREE_CODE_CLASS (code) == tcc_comparison
3494 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3495 warning_at (location, OPT_Waddress,
3496 "comparison with string literal results in unspecified behavior");
e994a705 3497
b8698a0f
L
3498 if (TREE_OVERFLOW_P (result.value)
3499 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3500 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3501 overflow_warning (location, result.value);
400fbf9f 3502
6866c6e8
ILT
3503 /* Warn about comparisons of different enum types. */
3504 if (warn_enum_compare
3505 && TREE_CODE_CLASS (code) == tcc_comparison
3506 && TREE_CODE (type1) == ENUMERAL_TYPE
3507 && TREE_CODE (type2) == ENUMERAL_TYPE
3508 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3509 warning_at (location, OPT_Wenum_compare,
3510 "comparison between %qT and %qT",
3511 type1, type2);
3512
400fbf9f
JW
3513 return result;
3514}
3e4093b6 3515\f
3e4093b6
RS
3516/* Return a tree for the difference of pointers OP0 and OP1.
3517 The resulting tree has type int. */
293c9fdd 3518
3e4093b6 3519static tree
db3927fb 3520pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3521{
3e4093b6 3522 tree restype = ptrdiff_type_node;
36c5e70a 3523 tree result, inttype;
400fbf9f 3524
36c5e70a
BE
3525 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3526 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3527 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3528 tree orig_op1 = op1;
400fbf9f 3529
36c5e70a
BE
3530 /* If the operands point into different address spaces, we need to
3531 explicitly convert them to pointers into the common address space
3532 before we can subtract the numerical address values. */
3533 if (as0 != as1)
3534 {
3535 addr_space_t as_common;
3536 tree common_type;
3537
3538 /* Determine the common superset address space. This is guaranteed
3539 to exist because the caller verified that comp_target_types
3540 returned non-zero. */
3541 if (!addr_space_superset (as0, as1, &as_common))
3542 gcc_unreachable ();
3543
3544 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3545 op0 = convert (common_type, op0);
3546 op1 = convert (common_type, op1);
3547 }
3548
3549 /* Determine integer type to perform computations in. This will usually
3550 be the same as the result type (ptrdiff_t), but may need to be a wider
3551 type if pointers for the address space are wider than ptrdiff_t. */
3552 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3553 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3554 else
3555 inttype = restype;
3556
fcf73884 3557 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3558 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3559 "pointer of type %<void *%> used in subtraction");
3560 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3561 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3562 "pointer to a function used in subtraction");
400fbf9f 3563
3e4093b6
RS
3564 /* First do the subtraction as integers;
3565 then drop through to build the divide operator.
3566 Do not do default conversions on the minus operator
3567 in case restype is a short type. */
400fbf9f 3568
db3927fb 3569 op0 = build_binary_op (loc,
36c5e70a
BE
3570 MINUS_EXPR, convert (inttype, op0),
3571 convert (inttype, op1), 0);
3e4093b6
RS
3572 /* This generates an error if op1 is pointer to incomplete type. */
3573 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3574 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3575
3e4093b6 3576 op1 = c_size_in_bytes (target_type);
400fbf9f 3577
f04dda30
MP
3578 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3579 error_at (loc, "arithmetic on pointer to an empty aggregate");
3580
3e4093b6 3581 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3582 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3583 op0, convert (inttype, op1));
3584
3585 /* Convert to final result type if necessary. */
3586 return convert (restype, result);
3e4093b6
RS
3587}
3588\f
267bac10
JM
3589/* Expand atomic compound assignments into an approriate sequence as
3590 specified by the C11 standard section 6.5.16.2.
3591 given
3592 _Atomic T1 E1
3593 T2 E2
3594 E1 op= E2
3595
3596 This sequence is used for all types for which these operations are
3597 supported.
3598
3599 In addition, built-in versions of the 'fe' prefixed routines may
3600 need to be invoked for floating point (real, complex or vector) when
3601 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3602
3603 T1 newval;
3604 T1 old;
3605 T1 *addr
3606 T2 val
3607 fenv_t fenv
3608
3609 addr = &E1;
3610 val = (E2);
3611 __atomic_load (addr, &old, SEQ_CST);
3612 feholdexcept (&fenv);
3613loop:
3614 newval = old op val;
3615 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3616 SEQ_CST))
3617 goto done;
3618 feclearexcept (FE_ALL_EXCEPT);
3619 goto loop:
3620done:
3621 feupdateenv (&fenv);
3622
3623 Also note that the compiler is simply issuing the generic form of
3624 the atomic operations. This requires temp(s) and has their address
3625 taken. The atomic processing is smart enough to figure out when the
3626 size of an object can utilize a lock-free version, and convert the
3627 built-in call to the appropriate lock-free routine. The optimizers
3628 will then dispose of any temps that are no longer required, and
3629 lock-free implementations are utilized as long as there is target
3630 support for the required size.
3631
3632 If the operator is NOP_EXPR, then this is a simple assignment, and
3633 an __atomic_store is issued to perform the assignment rather than
3634 the above loop.
3635
3636*/
3637
3638/* Build an atomic assignment at LOC, expanding into the proper
3639 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3640 the result of the operation, unless RETURN_OLD_P in which case
3641 return the old value of LHS (this is only for postincrement and
3642 postdecrement). */
3643static tree
3644build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3645 tree rhs, bool return_old_p)
3646{
3647 tree fndecl, func_call;
3648 vec<tree, va_gc> *params;
3649 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3650 tree old, old_addr;
3651 tree compound_stmt;
3652 tree stmt, goto_stmt;
3653 tree loop_label, loop_decl, done_label, done_decl;
3654
3655 tree lhs_type = TREE_TYPE (lhs);
3656 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3657 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3658 tree rhs_type = TREE_TYPE (rhs);
3659
3660 gcc_assert (TYPE_ATOMIC (lhs_type));
3661
3662 if (return_old_p)
3663 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3664
3665 /* Allocate enough vector items for a compare_exchange. */
3666 vec_alloc (params, 6);
3667
3668 /* Create a compound statement to hold the sequence of statements
3669 with a loop. */
3670 compound_stmt = c_begin_compound_stmt (false);
3671
3672 /* Fold the RHS if it hasn't already been folded. */
3673 if (modifycode != NOP_EXPR)
3674 rhs = c_fully_fold (rhs, false, NULL);
3675
3676 /* Remove the qualifiers for the rest of the expressions and create
3677 the VAL temp variable to hold the RHS. */
3678 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3679 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3680 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3681 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3682 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3683 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3684 NULL_TREE);
267bac10
JM
3685 SET_EXPR_LOCATION (rhs, loc);
3686 add_stmt (rhs);
3687
3688 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3689 an atomic_store. */
3690 if (modifycode == NOP_EXPR)
3691 {
3692 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3693 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3694 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3695 params->quick_push (lhs_addr);
3696 params->quick_push (rhs);
3697 params->quick_push (seq_cst);
8edbfaa6 3698 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3699 add_stmt (func_call);
3700
3701 /* Finish the compound statement. */
3702 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3703
3704 /* VAL is the value which was stored, return a COMPOUND_STMT of
3705 the statement and that value. */
3706 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3707 }
3708
3709 /* Create the variables and labels required for the op= form. */
5c4abbb8 3710 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3711 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3712 TREE_ADDRESSABLE (old) = 1;
3713 TREE_NO_WARNING (old) = 1;
267bac10 3714
5c4abbb8 3715 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3716 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3717 TREE_ADDRESSABLE (newval) = 1;
3718
3719 loop_decl = create_artificial_label (loc);
3720 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3721
3722 done_decl = create_artificial_label (loc);
3723 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3724
3725 /* __atomic_load (addr, &old, SEQ_CST). */
3726 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3727 params->quick_push (lhs_addr);
3728 params->quick_push (old_addr);
3729 params->quick_push (seq_cst);
8edbfaa6 3730 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3731 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3732 NULL_TREE);
3733 add_stmt (old);
267bac10
JM
3734 params->truncate (0);
3735
3736 /* Create the expressions for floating-point environment
3737 manipulation, if required. */
3738 bool need_fenv = (flag_trapping_math
3739 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3740 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3741 if (need_fenv)
3742 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3743
3744 if (hold_call)
3745 add_stmt (hold_call);
3746
3747 /* loop: */
3748 add_stmt (loop_label);
3749
3750 /* newval = old + val; */
3751 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3752 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3753 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3754 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3755 NULL_TREE, 0);
3756 if (rhs != error_mark_node)
3757 {
5c4abbb8
MP
3758 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3759 NULL_TREE);
267bac10
JM
3760 SET_EXPR_LOCATION (rhs, loc);
3761 add_stmt (rhs);
3762 }
3763
3764 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3765 goto done; */
3766 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3767 params->quick_push (lhs_addr);
3768 params->quick_push (old_addr);
3769 params->quick_push (newval_addr);
3770 params->quick_push (integer_zero_node);
3771 params->quick_push (seq_cst);
3772 params->quick_push (seq_cst);
8edbfaa6 3773 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3774
3775 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3776 SET_EXPR_LOCATION (goto_stmt, loc);
3777
3778 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3779 SET_EXPR_LOCATION (stmt, loc);
3780 add_stmt (stmt);
5c4abbb8 3781
267bac10
JM
3782 if (clear_call)
3783 add_stmt (clear_call);
3784
3785 /* goto loop; */
3786 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3787 SET_EXPR_LOCATION (goto_stmt, loc);
3788 add_stmt (goto_stmt);
5c4abbb8 3789
267bac10
JM
3790 /* done: */
3791 add_stmt (done_label);
3792
3793 if (update_call)
3794 add_stmt (update_call);
3795
3796 /* Finish the compound statement. */
3797 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3798
3799 /* NEWVAL is the value that was successfully stored, return a
3800 COMPOUND_EXPR of the statement and the appropriate value. */
3801 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3802 return_old_p ? old : newval);
3803}
3804
3e4093b6
RS
3805/* Construct and perhaps optimize a tree representation
3806 for a unary operation. CODE, a tree_code, specifies the operation
3807 and XARG is the operand.
3808 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3809 the default promotions (such as from short to int).
3810 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3811 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3812 arrays to pointers in C99.
3813
3814 LOCATION is the location of the operator. */
400fbf9f 3815
3e4093b6 3816tree
c9f9eb5d
AH
3817build_unary_op (location_t location,
3818 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3819{
3820 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3821 tree arg = xarg;
3822 tree argtype = 0;
808d6eaa 3823 enum tree_code typecode;
3e4093b6 3824 tree val;
c9f9eb5d 3825 tree ret = error_mark_node;
8ce94e44 3826 tree eptype = NULL_TREE;
3e4093b6 3827 int noconvert = flag;
4de67c26 3828 const char *invalid_op_diag;
928c19bb
JM
3829 bool int_operands;
3830
3831 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3832 if (int_operands)
3833 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3834
808d6eaa
JM
3835 if (code != ADDR_EXPR)
3836 arg = require_complete_type (arg);
3837
3838 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3839 if (typecode == ERROR_MARK)
3840 return error_mark_node;
3841 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3842 typecode = INTEGER_TYPE;
6c36d76b 3843
4de67c26
JM
3844 if ((invalid_op_diag
3845 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3846 {
c9f9eb5d 3847 error_at (location, invalid_op_diag);
4de67c26
JM
3848 return error_mark_node;
3849 }
3850
8ce94e44
JM
3851 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3852 {
3853 eptype = TREE_TYPE (arg);
3854 arg = TREE_OPERAND (arg, 0);
3855 }
3856
3e4093b6
RS
3857 switch (code)
3858 {
3859 case CONVERT_EXPR:
3860 /* This is used for unary plus, because a CONVERT_EXPR
3861 is enough to prevent anybody from looking inside for
3862 associativity, but won't generate any code. */
3863 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3864 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3865 || typecode == VECTOR_TYPE))
400fbf9f 3866 {
c9f9eb5d 3867 error_at (location, "wrong type argument to unary plus");
3e4093b6 3868 return error_mark_node;
400fbf9f 3869 }
3e4093b6
RS
3870 else if (!noconvert)
3871 arg = default_conversion (arg);
db3927fb 3872 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3873 break;
3874
3e4093b6
RS
3875 case NEGATE_EXPR:
3876 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3877 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3878 || typecode == VECTOR_TYPE))
3879 {
c9f9eb5d 3880 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3881 return error_mark_node;
3882 }
3883 else if (!noconvert)
3884 arg = default_conversion (arg);
400fbf9f
JW
3885 break;
3886
3e4093b6 3887 case BIT_NOT_EXPR:
462643f0
AP
3888 /* ~ works on integer types and non float vectors. */
3889 if (typecode == INTEGER_TYPE
3890 || (typecode == VECTOR_TYPE
3891 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3892 {
3e4093b6
RS
3893 if (!noconvert)
3894 arg = default_conversion (arg);
03d5b1f5 3895 }
3e4093b6 3896 else if (typecode == COMPLEX_TYPE)
400fbf9f 3897 {
3e4093b6 3898 code = CONJ_EXPR;
c1771a20 3899 pedwarn (location, OPT_Wpedantic,
fcf73884 3900 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3901 if (!noconvert)
3902 arg = default_conversion (arg);
3903 }
3904 else
3905 {
c9f9eb5d 3906 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3907 return error_mark_node;
400fbf9f
JW
3908 }
3909 break;
3910
3e4093b6 3911 case ABS_EXPR:
11017cc7 3912 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3913 {
c9f9eb5d 3914 error_at (location, "wrong type argument to abs");
3e4093b6 3915 return error_mark_node;
400fbf9f 3916 }
3e4093b6
RS
3917 else if (!noconvert)
3918 arg = default_conversion (arg);
400fbf9f
JW
3919 break;
3920
3e4093b6
RS
3921 case CONJ_EXPR:
3922 /* Conjugating a real value is a no-op, but allow it anyway. */
3923 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3924 || typecode == COMPLEX_TYPE))
400fbf9f 3925 {
c9f9eb5d 3926 error_at (location, "wrong type argument to conjugation");
3e4093b6 3927 return error_mark_node;
400fbf9f 3928 }
3e4093b6
RS
3929 else if (!noconvert)
3930 arg = default_conversion (arg);
400fbf9f
JW
3931 break;
3932
3e4093b6 3933 case TRUTH_NOT_EXPR:
ab22c1fa 3934 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3935 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3936 && typecode != COMPLEX_TYPE)
400fbf9f 3937 {
c9f9eb5d
AH
3938 error_at (location,
3939 "wrong type argument to unary exclamation mark");
3e4093b6 3940 return error_mark_node;
400fbf9f 3941 }
a27d595d
JM
3942 if (int_operands)
3943 {
3944 arg = c_objc_common_truthvalue_conversion (location, xarg);
3945 arg = remove_c_maybe_const_expr (arg);
3946 }
3947 else
3948 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3949 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3950 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3951 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3952 location = EXPR_LOCATION (ret);
c9f9eb5d 3953 goto return_build_unary_op;
3e4093b6 3954
3e4093b6 3955 case REALPART_EXPR:
3e4093b6 3956 case IMAGPART_EXPR:
fb52b50a
NF
3957 ret = build_real_imag_expr (location, code, arg);
3958 if (ret == error_mark_node)
3959 return error_mark_node;
8ce94e44
JM
3960 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3961 eptype = TREE_TYPE (eptype);
c9f9eb5d 3962 goto return_build_unary_op;
3e4093b6
RS
3963
3964 case PREINCREMENT_EXPR:
3965 case POSTINCREMENT_EXPR:
3966 case PREDECREMENT_EXPR:
3967 case POSTDECREMENT_EXPR:
3e4093b6 3968
928c19bb
JM
3969 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3970 {
3971 tree inner = build_unary_op (location, code,
3972 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3973 if (inner == error_mark_node)
3974 return error_mark_node;
3975 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3976 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3977 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3978 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3979 goto return_build_unary_op;
3980 }
3981
925e8657
NP
3982 /* Complain about anything that is not a true lvalue. In
3983 Objective-C, skip this check for property_refs. */
f90e8e2e 3984 if (!objc_is_property_ref (arg)
7bd11157
TT
3985 && !lvalue_or_else (location,
3986 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3987 || code == POSTINCREMENT_EXPR)
3988 ? lv_increment
3989 : lv_decrement)))
928c19bb
JM
3990 return error_mark_node;
3991
09639a83
ILT
3992 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3993 {
3994 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3995 warning_at (location, OPT_Wc___compat,
3996 "increment of enumeration value is invalid in C++");
3997 else
3998 warning_at (location, OPT_Wc___compat,
3999 "decrement of enumeration value is invalid in C++");
4000 }
4001
928c19bb
JM
4002 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4003 arg = c_fully_fold (arg, false, NULL);
4004
267bac10
JM
4005 bool atomic_op;
4006 atomic_op = really_atomic_lvalue (arg);
4007
3e4093b6
RS
4008 /* Increment or decrement the real part of the value,
4009 and don't change the imaginary part. */
4010 if (typecode == COMPLEX_TYPE)
400fbf9f 4011 {
3e4093b6
RS
4012 tree real, imag;
4013
c1771a20 4014 pedwarn (location, OPT_Wpedantic,
509c9d60 4015 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4016
267bac10
JM
4017 if (!atomic_op)
4018 {
4019 arg = stabilize_reference (arg);
4020 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4021 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4022 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4023 if (real == error_mark_node || imag == error_mark_node)
4024 return error_mark_node;
4025 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4026 real, imag);
4027 goto return_build_unary_op;
4028 }
400fbf9f 4029 }
3e4093b6
RS
4030
4031 /* Report invalid types. */
4032
ab22c1fa 4033 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4034 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4035 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4036 {
3e4093b6 4037 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4038 error_at (location, "wrong type argument to increment");
c22cacf3 4039 else
c9f9eb5d 4040 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4041
4042 return error_mark_node;
400fbf9f 4043 }
400fbf9f 4044
3e4093b6
RS
4045 {
4046 tree inc;
400fbf9f 4047
3e4093b6
RS
4048 argtype = TREE_TYPE (arg);
4049
4050 /* Compute the increment. */
4051
4052 if (typecode == POINTER_TYPE)
4053 {
a0e24419 4054 /* If pointer target is an incomplete type,
3e4093b6 4055 we just cannot know how to do the arithmetic. */
b70cef5d 4056 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4057 {
4058 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4059 error_at (location,
a0e24419
MP
4060 "increment of pointer to an incomplete type %qT",
4061 TREE_TYPE (argtype));
3e4093b6 4062 else
c9f9eb5d 4063 error_at (location,
a0e24419
MP
4064 "decrement of pointer to an incomplete type %qT",
4065 TREE_TYPE (argtype));
3e4093b6 4066 }
b70cef5d
JJ
4067 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4068 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4069 {
3e4093b6 4070 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4071 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4072 "wrong type argument to increment");
3e4093b6 4073 else
44d90fe1 4074 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4075 "wrong type argument to decrement");
3e4093b6
RS
4076 }
4077
b70cef5d 4078 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4079 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4080 }
b70cef5d 4081 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4082 {
4083 /* For signed fract types, we invert ++ to -- or
4084 -- to ++, and change inc from 1 to -1, because
4085 it is not possible to represent 1 in signed fract constants.
4086 For unsigned fract types, the result always overflows and
4087 we get an undefined (original) or the maximum value. */
4088 if (code == PREINCREMENT_EXPR)
4089 code = PREDECREMENT_EXPR;
4090 else if (code == PREDECREMENT_EXPR)
4091 code = PREINCREMENT_EXPR;
4092 else if (code == POSTINCREMENT_EXPR)
4093 code = POSTDECREMENT_EXPR;
4094 else /* code == POSTDECREMENT_EXPR */
4095 code = POSTINCREMENT_EXPR;
4096
4097 inc = integer_minus_one_node;
4098 inc = convert (argtype, inc);
4099 }
3e4093b6 4100 else
5be014d5 4101 {
241b71bb
TV
4102 inc = VECTOR_TYPE_P (argtype)
4103 ? build_one_cst (argtype)
4104 : integer_one_node;
5be014d5
AP
4105 inc = convert (argtype, inc);
4106 }
3e4093b6 4107
925e8657
NP
4108 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4109 need to ask Objective-C to build the increment or decrement
4110 expression for it. */
4111 if (objc_is_property_ref (arg))
f90e8e2e 4112 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4113 arg, inc);
4114
3e4093b6 4115 /* Report a read-only lvalue. */
f37acdf9 4116 if (TYPE_READONLY (argtype))
953ff289 4117 {
c02065fc 4118 readonly_error (location, arg,
953ff289
DN
4119 ((code == PREINCREMENT_EXPR
4120 || code == POSTINCREMENT_EXPR)
4121 ? lv_increment : lv_decrement));
4122 return error_mark_node;
4123 }
f37acdf9
JM
4124 else if (TREE_READONLY (arg))
4125 readonly_warning (arg,
4126 ((code == PREINCREMENT_EXPR
4127 || code == POSTINCREMENT_EXPR)
4128 ? lv_increment : lv_decrement));
3e4093b6 4129
267bac10
JM
4130 /* If the argument is atomic, use the special code sequences for
4131 atomic compound assignment. */
4132 if (atomic_op)
4133 {
4134 arg = stabilize_reference (arg);
4135 ret = build_atomic_assign (location, arg,
4136 ((code == PREINCREMENT_EXPR
4137 || code == POSTINCREMENT_EXPR)
4138 ? PLUS_EXPR
4139 : MINUS_EXPR),
4140 (FRACT_MODE_P (TYPE_MODE (argtype))
4141 ? inc
4142 : integer_one_node),
4143 (code == POSTINCREMENT_EXPR
4144 || code == POSTDECREMENT_EXPR));
4145 goto return_build_unary_op;
4146 }
4147
3e4093b6
RS
4148 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4149 val = boolean_increment (code, arg);
4150 else
53fb4de3 4151 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4152 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4153 if (TREE_CODE (val) != code)
6de9cd9a 4154 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4155 ret = val;
4156 goto return_build_unary_op;
3e4093b6
RS
4157 }
4158
4159 case ADDR_EXPR:
4160 /* Note that this operation never does default_conversion. */
4161
2b4b7036
JM
4162 /* The operand of unary '&' must be an lvalue (which excludes
4163 expressions of type void), or, in C99, the result of a [] or
4164 unary '*' operator. */
4165 if (VOID_TYPE_P (TREE_TYPE (arg))
4166 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4167 && (TREE_CODE (arg) != INDIRECT_REF
4168 || !flag_isoc99))
4169 pedwarn (location, 0, "taking address of expression of type %<void%>");
4170
3e4093b6
RS
4171 /* Let &* cancel out to simplify resulting code. */
4172 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4173 {
3e4093b6
RS
4174 /* Don't let this be an lvalue. */
4175 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4176 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4177 ret = TREE_OPERAND (arg, 0);
4178 goto return_build_unary_op;
400fbf9f 4179 }
1eb8759b 4180
7c672dfc 4181 /* For &x[y], return x+y */
3e4093b6 4182 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4183 {
f2a71bbc
JM
4184 tree op0 = TREE_OPERAND (arg, 0);
4185 if (!c_mark_addressable (op0))
3e4093b6 4186 return error_mark_node;
1eb8759b 4187 }
1eb8759b 4188
3e4093b6
RS
4189 /* Anything not already handled and not a true memory reference
4190 or a non-lvalue array is an error. */
4191 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4192 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4193 return error_mark_node;
b6a10c9f 4194
928c19bb
JM
4195 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4196 folding later. */
4197 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4198 {
4199 tree inner = build_unary_op (location, code,
4200 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4201 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4202 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4203 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4204 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4205 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4206 goto return_build_unary_op;
4207 }
4208
3e4093b6
RS
4209 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4210 argtype = TREE_TYPE (arg);
400fbf9f 4211
3e4093b6 4212 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4213 to which the address will point. This is only needed
f2c1da78 4214 for function types. */
6615c446 4215 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4216 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4217 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4218 {
4219 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4220 int quals = orig_quals;
4221
4222 if (TREE_READONLY (arg))
4223 quals |= TYPE_QUAL_CONST;
4224 if (TREE_THIS_VOLATILE (arg))
4225 quals |= TYPE_QUAL_VOLATILE;
4226
f2c1da78
JM
4227 argtype = c_build_qualified_type (argtype, quals);
4228 }
400fbf9f 4229
3e4093b6
RS
4230 if (!c_mark_addressable (arg))
4231 return error_mark_node;
400fbf9f 4232
abb54d14
JM
4233 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4234 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4235
5cc200fc 4236 argtype = build_pointer_type (argtype);
5e55f99d
RH
4237
4238 /* ??? Cope with user tricks that amount to offsetof. Delete this
4239 when we have proper support for integer constant expressions. */
4240 val = get_base_address (arg);
4241 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4242 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4243 {
cf9e9959 4244 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4245 goto return_build_unary_op;
3aa2ddb8 4246 }
5e55f99d 4247
5cc200fc 4248 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4249
c9f9eb5d
AH
4250 ret = val;
4251 goto return_build_unary_op;
400fbf9f 4252
3e4093b6 4253 default:
1344f9a3 4254 gcc_unreachable ();
3e4093b6 4255 }
400fbf9f 4256
3e4093b6
RS
4257 if (argtype == 0)
4258 argtype = TREE_TYPE (arg);
928c19bb
JM
4259 if (TREE_CODE (arg) == INTEGER_CST)
4260 ret = (require_constant_value
db3927fb
AH
4261 ? fold_build1_initializer_loc (location, code, argtype, arg)
4262 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4263 else
4264 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4265 return_build_unary_op:
4266 gcc_assert (ret != error_mark_node);
928c19bb
JM
4267 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4268 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4269 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4270 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4271 ret = note_integer_operands (ret);
8ce94e44
JM
4272 if (eptype)
4273 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4274 protected_set_expr_location (ret, location);
4275 return ret;
3e4093b6 4276}
400fbf9f 4277
3e4093b6
RS
4278/* Return nonzero if REF is an lvalue valid for this language.
4279 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4280 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4281
1e4ae551 4282bool
58f9752a 4283lvalue_p (const_tree ref)
3e4093b6 4284{
58f9752a 4285 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4286
3e4093b6
RS
4287 switch (code)
4288 {
4289 case REALPART_EXPR:
4290 case IMAGPART_EXPR:
4291 case COMPONENT_REF:
4292 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4293
928c19bb
JM
4294 case C_MAYBE_CONST_EXPR:
4295 return lvalue_p (TREE_OPERAND (ref, 1));
4296
3e4093b6
RS
4297 case COMPOUND_LITERAL_EXPR:
4298 case STRING_CST:
4299 return 1;
400fbf9f 4300
3e4093b6
RS
4301 case INDIRECT_REF:
4302 case ARRAY_REF:
36536d79 4303 case ARRAY_NOTATION_REF:
3e4093b6
RS
4304 case VAR_DECL:
4305 case PARM_DECL:
4306 case RESULT_DECL:
4307 case ERROR_MARK:
4308 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4309 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4310
3e4093b6 4311 case BIND_EXPR:
3e4093b6 4312 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4313
3e4093b6
RS
4314 default:
4315 return 0;
4316 }
4317}
400fbf9f 4318\f
f37acdf9
JM
4319/* Give a warning for storing in something that is read-only in GCC
4320 terms but not const in ISO C terms. */
4321
4322static void
4323readonly_warning (tree arg, enum lvalue_use use)
4324{
4325 switch (use)
4326 {
4327 case lv_assign:
4328 warning (0, "assignment of read-only location %qE", arg);
4329 break;
4330 case lv_increment:
4331 warning (0, "increment of read-only location %qE", arg);
4332 break;
4333 case lv_decrement:
4334 warning (0, "decrement of read-only location %qE", arg);
4335 break;
4336 default:
4337 gcc_unreachable ();
4338 }
4339 return;
4340}
4341
37dc0d8d
JM
4342
4343/* Return nonzero if REF is an lvalue valid for this language;
4344 otherwise, print an error message and return zero. USE says
7bd11157
TT
4345 how the lvalue is being used and so selects the error message.
4346 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4347
4348static int
7bd11157 4349lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4350{
4351 int win = lvalue_p (ref);
4352
4353 if (!win)
7bd11157 4354 lvalue_error (loc, use);
37dc0d8d
JM
4355
4356 return win;
4357}
3e4093b6
RS
4358\f
4359/* Mark EXP saying that we need to be able to take the
4360 address of it; it should not be allocated in a register.
4361 Returns true if successful. */
54c93c30 4362
3e4093b6
RS
4363bool
4364c_mark_addressable (tree exp)
400fbf9f 4365{
3e4093b6 4366 tree x = exp;
95602da1 4367
3e4093b6
RS
4368 while (1)
4369 switch (TREE_CODE (x))
4370 {
4371 case COMPONENT_REF:
4372 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4373 {
0039fa55
AN
4374 error
4375 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4376 return false;
4377 }
95602da1 4378
3e4093b6 4379 /* ... fall through ... */
95602da1 4380
3e4093b6
RS
4381 case ADDR_EXPR:
4382 case ARRAY_REF:
4383 case REALPART_EXPR:
4384 case IMAGPART_EXPR:
4385 x = TREE_OPERAND (x, 0);
4386 break;
95602da1 4387
3e4093b6
RS
4388 case COMPOUND_LITERAL_EXPR:
4389 case CONSTRUCTOR:
4390 TREE_ADDRESSABLE (x) = 1;
4391 return true;
95602da1 4392
3e4093b6
RS
4393 case VAR_DECL:
4394 case CONST_DECL:
4395 case PARM_DECL:
4396 case RESULT_DECL:
5baeaac0 4397 if (C_DECL_REGISTER (x)
3e4093b6
RS
4398 && DECL_NONLOCAL (x))
4399 {
e697b20f 4400 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4401 {
0039fa55
AN
4402 error
4403 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4404 return false;
4405 }
509c9d60 4406 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4407 }
5baeaac0 4408 else if (C_DECL_REGISTER (x))
3e4093b6 4409 {
e697b20f 4410 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4411 error ("address of global register variable %qD requested", x);
4412 else
4413 error ("address of register variable %qD requested", x);
4414 return false;
3e4093b6 4415 }
400fbf9f 4416
3e4093b6
RS
4417 /* drops in */
4418 case FUNCTION_DECL:
4419 TREE_ADDRESSABLE (x) = 1;
4420 /* drops out */
4421 default:
4422 return true;
4423 }
4424}
4425\f
2d2e923f
MLI
4426/* Convert EXPR to TYPE, warning about conversion problems with
4427 constants. SEMANTIC_TYPE is the type this conversion would use
4428 without excess precision. If SEMANTIC_TYPE is NULL, this function
4429 is equivalent to convert_and_check. This function is a wrapper that
4430 handles conversions that may be different than
4431 the usual ones because of excess precision. */
4432
4433static tree
68fca595
MP
4434ep_convert_and_check (location_t loc, tree type, tree expr,
4435 tree semantic_type)
2d2e923f
MLI
4436{
4437 if (TREE_TYPE (expr) == type)
4438 return expr;
4439
4440 if (!semantic_type)
68fca595 4441 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4442
4443 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4444 && TREE_TYPE (expr) != semantic_type)
4445 {
4446 /* For integers, we need to check the real conversion, not
4447 the conversion to the excess precision type. */
68fca595 4448 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4449 }
4450 /* Result type is the excess precision type, which should be
4451 large enough, so do not check. */
4452 return convert (type, expr);
4453}
4454
928c19bb
JM
4455/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4456 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4457 if folded to an integer constant then the unselected half may
4458 contain arbitrary operations not normally permitted in constant
c2255bc4 4459 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4460
4461tree
744aa42f 4462build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4463 tree op1, tree op1_original_type, tree op2,
4464 tree op2_original_type)
400fbf9f 4465{
3e4093b6
RS
4466 tree type1;
4467 tree type2;
4468 enum tree_code code1;
4469 enum tree_code code2;
4470 tree result_type = NULL;
2d2e923f 4471 tree semantic_result_type = NULL;
3e4093b6 4472 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4473 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4474 bool ifexp_int_operands;
928c19bb 4475 tree ret;
400fbf9f 4476
4d84fe7c
JM
4477 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4478 if (op1_int_operands)
4479 op1 = remove_c_maybe_const_expr (op1);
4480 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4481 if (op2_int_operands)
4482 op2 = remove_c_maybe_const_expr (op2);
4483 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4484 if (ifexp_int_operands)
4485 ifexp = remove_c_maybe_const_expr (ifexp);
4486
3e4093b6
RS
4487 /* Promote both alternatives. */
4488
4489 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4490 op1 = default_conversion (op1);
4491 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4492 op2 = default_conversion (op2);
4493
4494 if (TREE_CODE (ifexp) == ERROR_MARK
4495 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4496 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4497 return error_mark_node;
400fbf9f 4498
3e4093b6
RS
4499 type1 = TREE_TYPE (op1);
4500 code1 = TREE_CODE (type1);
4501 type2 = TREE_TYPE (op2);
4502 code2 = TREE_CODE (type2);
4503
b1adf557
JM
4504 /* C90 does not permit non-lvalue arrays in conditional expressions.
4505 In C99 they will be pointers by now. */
4506 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4507 {
744aa42f 4508 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4509 return error_mark_node;
4510 }
4511
8ce94e44
JM
4512 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4513 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4514 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4515 || code1 == COMPLEX_TYPE)
4516 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4517 || code2 == COMPLEX_TYPE))
4518 {
2d2e923f 4519 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4520 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4521 {
4522 op1 = TREE_OPERAND (op1, 0);
4523 type1 = TREE_TYPE (op1);
4524 gcc_assert (TREE_CODE (type1) == code1);
4525 }
4526 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4527 {
4528 op2 = TREE_OPERAND (op2, 0);
4529 type2 = TREE_TYPE (op2);
4530 gcc_assert (TREE_CODE (type2) == code2);
4531 }
4532 }
4533
d130ae11
ILT
4534 if (warn_cxx_compat)
4535 {
4536 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4537 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4538
4539 if (TREE_CODE (t1) == ENUMERAL_TYPE
4540 && TREE_CODE (t2) == ENUMERAL_TYPE
4541 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4542 warning_at (colon_loc, OPT_Wc___compat,
4543 ("different enum types in conditional is "
4544 "invalid in C++: %qT vs %qT"),
4545 t1, t2);
4546 }
4547
3e4093b6
RS
4548 /* Quickly detect the usual case where op1 and op2 have the same type
4549 after promotion. */
4550 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4551 {
3e4093b6
RS
4552 if (type1 == type2)
4553 result_type = type1;
4554 else
4555 result_type = TYPE_MAIN_VARIANT (type1);
4556 }
4557 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4558 || code1 == COMPLEX_TYPE)
4559 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4560 || code2 == COMPLEX_TYPE))
3e4093b6 4561 {
ccf7f880 4562 result_type = c_common_type (type1, type2);
0a0b3574
MM
4563 do_warn_double_promotion (result_type, type1, type2,
4564 "implicit conversion from %qT to %qT to "
4565 "match other result of conditional",
4566 colon_loc);
400fbf9f 4567
3e4093b6
RS
4568 /* If -Wsign-compare, warn here if type1 and type2 have
4569 different signedness. We'll promote the signed to unsigned
4570 and later code won't know it used to be different.
4571 Do this check on the original types, so that explicit casts
4572 will be considered, but default promotions won't. */
7d882b83 4573 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4574 {
8df83eae
RK
4575 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4576 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4577
3e4093b6
RS
4578 if (unsigned_op1 ^ unsigned_op2)
4579 {
6ac01510
ILT
4580 bool ovf;
4581
3e4093b6
RS
4582 /* Do not warn if the result type is signed, since the
4583 signed type will only be chosen if it can represent
4584 all the values of the unsigned type. */
3f75a254 4585 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4586 /* OK */;
3e4093b6 4587 else
928c19bb
JM
4588 {
4589 bool op1_maybe_const = true;
4590 bool op2_maybe_const = true;
4591
4592 /* Do not warn if the signed quantity is an
4593 unsuffixed integer literal (or some static
4594 constant expression involving such literals) and
4595 it is non-negative. This warning requires the
4596 operands to be folded for best results, so do
4597 that folding in this case even without
4598 warn_sign_compare to avoid warning options
4599 possibly affecting code generation. */
f5178456
RS
4600 c_inhibit_evaluation_warnings
4601 += (ifexp == truthvalue_false_node);
928c19bb
JM
4602 op1 = c_fully_fold (op1, require_constant_value,
4603 &op1_maybe_const);
f5178456
RS
4604 c_inhibit_evaluation_warnings
4605 -= (ifexp == truthvalue_false_node);
4606
4607 c_inhibit_evaluation_warnings
4608 += (ifexp == truthvalue_true_node);
928c19bb
JM
4609 op2 = c_fully_fold (op2, require_constant_value,
4610 &op2_maybe_const);
f5178456
RS
4611 c_inhibit_evaluation_warnings
4612 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4613
4614 if (warn_sign_compare)
4615 {
4616 if ((unsigned_op2
4617 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4618 || (unsigned_op1
4619 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4620 /* OK */;
4621 else
744aa42f
ILT
4622 warning_at (colon_loc, OPT_Wsign_compare,
4623 ("signed and unsigned type in "
4624 "conditional expression"));
928c19bb
JM
4625 }
4626 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4627 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4628 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4629 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4630 }
3e4093b6
RS
4631 }
4632 }
4633 }
4634 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4635 {
fcf73884 4636 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4637 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4638 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4639 result_type = void_type_node;
4640 }
4641 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4642 {
36c5e70a
BE
4643 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4644 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4645 addr_space_t as_common;
4646
744aa42f 4647 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4648 result_type = common_pointer_type (type1, type2);
6aa3c60d 4649 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4650 result_type = type2;
6aa3c60d 4651 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4652 result_type = type1;
4653 else if (!addr_space_superset (as1, as2, &as_common))
4654 {
4655 error_at (colon_loc, "pointers to disjoint address spaces "
4656 "used in conditional expression");
4657 return error_mark_node;
4658 }
267bac10
JM
4659 else if (VOID_TYPE_P (TREE_TYPE (type1))
4660 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4661 {
768952be
MU
4662 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4663 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4664 & ~TYPE_QUALS (TREE_TYPE (type1))))
4665 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4666 "pointer to array loses qualifier "
4667 "in conditional expression");
4668
fcf73884 4669 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4670 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4671 "ISO C forbids conditional expr between "
bda67431 4672 "%<void *%> and function pointer");
3e4093b6
RS
4673 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4674 TREE_TYPE (type2)));
34a80643 4675 }
267bac10
JM
4676 else if (VOID_TYPE_P (TREE_TYPE (type2))
4677 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4678 {
768952be
MU
4679 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4680 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4681 & ~TYPE_QUALS (TREE_TYPE (type2))))
4682 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4683 "pointer to array loses qualifier "
4684 "in conditional expression");
4685
fcf73884 4686 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4687 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4688 "ISO C forbids conditional expr between "
bda67431 4689 "%<void *%> and function pointer");
3e4093b6
RS
4690 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4691 TREE_TYPE (type1)));
1c2a9b35 4692 }
b581b85b
NP
4693 /* Objective-C pointer comparisons are a bit more lenient. */
4694 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4695 result_type = objc_common_type (type1, type2);
34a80643 4696 else
ab87f8c8 4697 {
36c5e70a
BE
4698 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4699
b581b85b
NP
4700 pedwarn (colon_loc, 0,
4701 "pointer type mismatch in conditional expression");
36c5e70a
BE
4702 result_type = build_pointer_type
4703 (build_qualified_type (void_type_node, qual));
ab87f8c8 4704 }
3e4093b6
RS
4705 }
4706 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4707 {
6aa3c60d 4708 if (!null_pointer_constant_p (orig_op2))
744aa42f 4709 pedwarn (colon_loc, 0,
509c9d60 4710 "pointer/integer type mismatch in conditional expression");
3e4093b6 4711 else
ab87f8c8 4712 {
3e4093b6 4713 op2 = null_pointer_node;
ab87f8c8 4714 }
3e4093b6
RS
4715 result_type = type1;
4716 }
4717 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4718 {
6aa3c60d 4719 if (!null_pointer_constant_p (orig_op1))
744aa42f 4720 pedwarn (colon_loc, 0,
509c9d60 4721 "pointer/integer type mismatch in conditional expression");
3e4093b6 4722 else
ab87f8c8 4723 {
3e4093b6 4724 op1 = null_pointer_node;
ab87f8c8 4725 }
3e4093b6
RS
4726 result_type = type2;
4727 }
1c2a9b35 4728
3e4093b6
RS
4729 if (!result_type)
4730 {
4731 if (flag_cond_mismatch)
4732 result_type = void_type_node;
4733 else
400fbf9f 4734 {
c2255bc4 4735 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4736 return error_mark_node;
400fbf9f 4737 }
3e4093b6 4738 }
400fbf9f 4739
3e4093b6
RS
4740 /* Merge const and volatile flags of the incoming types. */
4741 result_type
4742 = build_type_variant (result_type,
afbd0665
AS
4743 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4744 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4745
68fca595
MP
4746 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4747 semantic_result_type);
4748 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4749 semantic_result_type);
b6a10c9f 4750
928c19bb
JM
4751 if (ifexp_bcp && ifexp == truthvalue_true_node)
4752 {
4753 op2_int_operands = true;
4754 op1 = c_fully_fold (op1, require_constant_value, NULL);
4755 }
4756 if (ifexp_bcp && ifexp == truthvalue_false_node)
4757 {
4758 op1_int_operands = true;
4759 op2 = c_fully_fold (op2, require_constant_value, NULL);
4760 }
4d84fe7c 4761 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4762 && op1_int_operands
4763 && op2_int_operands);
4764 if (int_operands)
4765 {
4766 int_const = ((ifexp == truthvalue_true_node
4767 && TREE_CODE (orig_op1) == INTEGER_CST
4768 && !TREE_OVERFLOW (orig_op1))
4769 || (ifexp == truthvalue_false_node
4770 && TREE_CODE (orig_op2) == INTEGER_CST
4771 && !TREE_OVERFLOW (orig_op2)));
4772 }
4773 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4774 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4775 else
4776 {
01c7ccbb
JM
4777 if (int_operands)
4778 {
f34f1c87
MP
4779 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4780 nested inside of the expression. */
4781 op1 = c_fully_fold (op1, false, NULL);
4782 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4783 }
928c19bb
JM
4784 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4785 if (int_operands)
4786 ret = note_integer_operands (ret);
4787 }
2d2e923f
MLI
4788 if (semantic_result_type)
4789 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4790
c2255bc4 4791 protected_set_expr_location (ret, colon_loc);
928c19bb 4792 return ret;
3e4093b6
RS
4793}
4794\f
487a92fe 4795/* Return a compound expression that performs two expressions and
c2255bc4
AH
4796 returns the value of the second of them.
4797
4798 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4799
3e4093b6 4800tree
c2255bc4 4801build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4802{
4d84fe7c 4803 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4804 tree eptype = NULL_TREE;
928c19bb
JM
4805 tree ret;
4806
b72271b9 4807 if (flag_cilkplus
939b37da
BI
4808 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4809 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4810 {
4811 error_at (loc,
4812 "spawned function call cannot be part of a comma expression");
4813 return error_mark_node;
4814 }
4d84fe7c
JM
4815 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4816 if (expr1_int_operands)
4817 expr1 = remove_c_maybe_const_expr (expr1);
4818 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4819 if (expr2_int_operands)
4820 expr2 = remove_c_maybe_const_expr (expr2);
4821
8ce94e44
JM
4822 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4823 expr1 = TREE_OPERAND (expr1, 0);
4824 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4825 {
4826 eptype = TREE_TYPE (expr2);
4827 expr2 = TREE_OPERAND (expr2, 0);
4828 }
4829
3f75a254 4830 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4831 {
4832 /* The left-hand operand of a comma expression is like an expression
c5409249 4833 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4834 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4835 if (warn_unused_value)
47aecf47 4836 {
e14a6540 4837 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4838 && CONVERT_EXPR_P (expr1))
47aecf47 4839 ; /* (void) a, b */
e14a6540
JM
4840 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4841 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4842 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4843 ; /* (void) a, (void) b, c */
4844 else
b8698a0f 4845 warning_at (loc, OPT_Wunused_value,
c2255bc4 4846 "left-hand operand of comma expression has no effect");
47aecf47 4847 }
3e4093b6 4848 }
789eadcd
MP
4849 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4850 && warn_unused_value)
4851 {
4852 tree r = expr1;
4853 location_t cloc = loc;
4854 while (TREE_CODE (r) == COMPOUND_EXPR)
4855 {
4856 if (EXPR_HAS_LOCATION (r))
4857 cloc = EXPR_LOCATION (r);
4858 r = TREE_OPERAND (r, 1);
4859 }
4860 if (!TREE_SIDE_EFFECTS (r)
4861 && !VOID_TYPE_P (TREE_TYPE (r))
4862 && !CONVERT_EXPR_P (r))
4863 warning_at (cloc, OPT_Wunused_value,
4864 "right-hand operand of comma expression has no effect");
4865 }
400fbf9f 4866
3e4093b6
RS
4867 /* With -Wunused, we should also warn if the left-hand operand does have
4868 side-effects, but computes a value which is not used. For example, in
4869 `foo() + bar(), baz()' the result of the `+' operator is not used,
4870 so we should issue a warning. */
4871 else if (warn_unused_value)
c2255bc4 4872 warn_if_unused_value (expr1, loc);
400fbf9f 4873
e63d6886
AP
4874 if (expr2 == error_mark_node)
4875 return error_mark_node;
4876
928c19bb
JM
4877 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4878
4879 if (flag_isoc99
4d84fe7c
JM
4880 && expr1_int_operands
4881 && expr2_int_operands)
928c19bb
JM
4882 ret = note_integer_operands (ret);
4883
8ce94e44
JM
4884 if (eptype)
4885 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4886
c2255bc4 4887 protected_set_expr_location (ret, loc);
928c19bb 4888 return ret;
3e4093b6 4889}
400fbf9f 4890
67165eb3
ILT
4891/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4892 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4893 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4894 of the cast. -Wcast-qual appeared on the command line. Named
4895 address space qualifiers are not handled here, because they result
4896 in different warnings. */
67165eb3
ILT
4897
4898static void
2ee3cb35 4899handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4900{
4901 tree in_type = type;
4902 tree in_otype = otype;
4903 int added = 0;
4904 int discarded = 0;
4905 bool is_const;
4906
4907 /* Check that the qualifiers on IN_TYPE are a superset of the
4908 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4909 nodes is uninteresting and we stop as soon as we hit a
4910 non-POINTER_TYPE node on either type. */
4911 do
4912 {
4913 in_otype = TREE_TYPE (in_otype);
4914 in_type = TREE_TYPE (in_type);
4915
4916 /* GNU C allows cv-qualified function types. 'const' means the
4917 function is very pure, 'volatile' means it can't return. We
4918 need to warn when such qualifiers are added, not when they're
4919 taken away. */
4920 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4921 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4922 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4923 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4924 else
36c5e70a
BE
4925 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4926 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4927 }
4928 while (TREE_CODE (in_type) == POINTER_TYPE
4929 && TREE_CODE (in_otype) == POINTER_TYPE);
4930
4931 if (added)
2ee3cb35
MLI
4932 warning_at (loc, OPT_Wcast_qual,
4933 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4934
4935 if (discarded)
4936 /* There are qualifiers present in IN_OTYPE that are not present
4937 in IN_TYPE. */
2ee3cb35 4938 warning_at (loc, OPT_Wcast_qual,
7485aeea 4939 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4940 discarded);
67165eb3
ILT
4941
4942 if (added || discarded)
4943 return;
4944
4945 /* A cast from **T to const **T is unsafe, because it can cause a
4946 const value to be changed with no additional warning. We only
4947 issue this warning if T is the same on both sides, and we only
4948 issue the warning if there are the same number of pointers on
4949 both sides, as otherwise the cast is clearly unsafe anyhow. A
4950 cast is unsafe when a qualifier is added at one level and const
4951 is not present at all outer levels.
4952
4953 To issue this warning, we check at each level whether the cast
4954 adds new qualifiers not already seen. We don't need to special
4955 case function types, as they won't have the same
4956 TYPE_MAIN_VARIANT. */
4957
4958 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4959 return;
4960 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4961 return;
4962
4963 in_type = type;
4964 in_otype = otype;
4965 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4966 do
4967 {
4968 in_type = TREE_TYPE (in_type);
4969 in_otype = TREE_TYPE (in_otype);
4970 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4971 && !is_const)
4972 {
2ee3cb35
MLI
4973 warning_at (loc, OPT_Wcast_qual,
4974 "to be safe all intermediate pointers in cast from "
4975 "%qT to %qT must be %<const%> qualified",
4976 otype, type);
67165eb3
ILT
4977 break;
4978 }
4979 if (is_const)
4980 is_const = TYPE_READONLY (in_type);
4981 }
4982 while (TREE_CODE (in_type) == POINTER_TYPE);
4983}
4984
b8698a0f 4985/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4986 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4987
3e4093b6 4988tree
c2255bc4 4989build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4990{
8ce94e44
JM
4991 tree value;
4992
4993 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4994 expr = TREE_OPERAND (expr, 0);
4995
4996 value = expr;
400fbf9f 4997
3e4093b6
RS
4998 if (type == error_mark_node || expr == error_mark_node)
4999 return error_mark_node;
400fbf9f 5000
3e4093b6
RS
5001 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5002 only in <protocol> qualifications. But when constructing cast expressions,
5003 the protocols do matter and must be kept around. */
700686fa
ZL
5004 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5005 return build1 (NOP_EXPR, type, expr);
5006
5007 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5008
3e4093b6
RS
5009 if (TREE_CODE (type) == ARRAY_TYPE)
5010 {
c2255bc4 5011 error_at (loc, "cast specifies array type");
3e4093b6
RS
5012 return error_mark_node;
5013 }
400fbf9f 5014
3e4093b6
RS
5015 if (TREE_CODE (type) == FUNCTION_TYPE)
5016 {
c2255bc4 5017 error_at (loc, "cast specifies function type");
3e4093b6
RS
5018 return error_mark_node;
5019 }
400fbf9f 5020
808d6eaa
JM
5021 if (!VOID_TYPE_P (type))
5022 {
5023 value = require_complete_type (value);
5024 if (value == error_mark_node)
5025 return error_mark_node;
5026 }
5027
3e4093b6
RS
5028 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5029 {
fcf73884
MLI
5030 if (TREE_CODE (type) == RECORD_TYPE
5031 || TREE_CODE (type) == UNION_TYPE)
c1771a20 5032 pedwarn (loc, OPT_Wpedantic,
fcf73884 5033 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5034
5035 /* Convert to remove any qualifiers from VALUE's type. */
5036 value = convert (type, value);
3e4093b6
RS
5037 }
5038 else if (TREE_CODE (type) == UNION_TYPE)
5039 {
5040 tree field;
400fbf9f 5041
910ad8de 5042 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5043 if (TREE_TYPE (field) != error_mark_node
5044 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5045 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5046 break;
5047
5048 if (field)
400fbf9f 5049 {
3e4093b6 5050 tree t;
e616f54d 5051 bool maybe_const = true;
3e4093b6 5052
c1771a20 5053 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5054 t = c_fully_fold (value, false, &maybe_const);
5055 t = build_constructor_single (type, field, t);
5056 if (!maybe_const)
5057 t = c_wrap_maybe_const (t, true);
5058 t = digest_init (loc, type, t,
bbbbb16a 5059 NULL_TREE, false, true, 0);
3e4093b6
RS
5060 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5061 return t;
400fbf9f 5062 }
c2255bc4 5063 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5064 return error_mark_node;
5065 }
5066 else
5067 {
5068 tree otype, ovalue;
400fbf9f 5069
3e4093b6 5070 if (type == void_type_node)
c2255bc4
AH
5071 {
5072 tree t = build1 (CONVERT_EXPR, type, value);
5073 SET_EXPR_LOCATION (t, loc);
5074 return t;
5075 }
400fbf9f 5076
3e4093b6 5077 otype = TREE_TYPE (value);
400fbf9f 5078
3e4093b6 5079 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5080 if (warn_cast_qual
5081 && TREE_CODE (type) == POINTER_TYPE
5082 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5083 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5084
36c5e70a
BE
5085 /* Warn about conversions between pointers to disjoint
5086 address spaces. */
5087 if (TREE_CODE (type) == POINTER_TYPE
5088 && TREE_CODE (otype) == POINTER_TYPE
5089 && !null_pointer_constant_p (value))
5090 {
5091 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5092 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5093 addr_space_t as_common;
5094
5095 if (!addr_space_superset (as_to, as_from, &as_common))
5096 {
5097 if (ADDR_SPACE_GENERIC_P (as_from))
5098 warning_at (loc, 0, "cast to %s address space pointer "
5099 "from disjoint generic address space pointer",
5100 c_addr_space_name (as_to));
5101
5102 else if (ADDR_SPACE_GENERIC_P (as_to))
5103 warning_at (loc, 0, "cast to generic address space pointer "
5104 "from disjoint %s address space pointer",
5105 c_addr_space_name (as_from));
5106
5107 else
5108 warning_at (loc, 0, "cast to %s address space pointer "
5109 "from disjoint %s address space pointer",
5110 c_addr_space_name (as_to),
5111 c_addr_space_name (as_from));
5112 }
5113 }
5114
3e4093b6 5115 /* Warn about possible alignment problems. */
3176a0c2 5116 if (STRICT_ALIGNMENT
3e4093b6
RS
5117 && TREE_CODE (type) == POINTER_TYPE
5118 && TREE_CODE (otype) == POINTER_TYPE
5119 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5120 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5121 /* Don't warn about opaque types, where the actual alignment
5122 restriction is unknown. */
5123 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5124 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5125 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5126 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5127 warning_at (loc, OPT_Wcast_align,
5128 "cast increases required alignment of target type");
e9a25f70 5129
3176a0c2 5130 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5131 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5132 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5133 /* Unlike conversion of integers to pointers, where the
5134 warning is disabled for converting constants because
5135 of cases such as SIG_*, warn about converting constant
5136 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5137 sign extension, and a warning is appropriate. */
c2255bc4
AH
5138 warning_at (loc, OPT_Wpointer_to_int_cast,
5139 "cast from pointer to integer of different size");
400fbf9f 5140
3176a0c2 5141 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5142 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5143 warning_at (loc, OPT_Wbad_function_cast,
5144 "cast from function call of type %qT "
5145 "to non-matching type %qT", otype, type);
400fbf9f 5146
3176a0c2 5147 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5148 && TREE_CODE (otype) == INTEGER_TYPE
5149 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5150 /* Don't warn about converting any constant. */
5151 && !TREE_CONSTANT (value))
c2255bc4
AH
5152 warning_at (loc,
5153 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5154 "of different size");
400fbf9f 5155
79bedddc
SR
5156 if (warn_strict_aliasing <= 2)
5157 strict_aliasing_warning (otype, type, expr);
400fbf9f 5158
3897f229
JM
5159 /* If pedantic, warn for conversions between function and object
5160 pointer types, except for converting a null pointer constant
5161 to function pointer type. */
5162 if (pedantic
5163 && TREE_CODE (type) == POINTER_TYPE
5164 && TREE_CODE (otype) == POINTER_TYPE
5165 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5166 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5167 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5168 "conversion of function pointer to object pointer type");
3897f229
JM
5169
5170 if (pedantic
5171 && TREE_CODE (type) == POINTER_TYPE
5172 && TREE_CODE (otype) == POINTER_TYPE
5173 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5174 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5175 && !null_pointer_constant_p (value))
c1771a20 5176 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5177 "conversion of object pointer to function pointer type");
3897f229 5178
3e4093b6 5179 ovalue = value;
3e4093b6 5180 value = convert (type, value);
400fbf9f 5181
3e4093b6 5182 /* Ignore any integer overflow caused by the cast. */
928c19bb 5183 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5184 {
8bcd6380 5185 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5186 {
8bcd6380
RS
5187 if (!TREE_OVERFLOW (value))
5188 {
5189 /* Avoid clobbering a shared constant. */
5190 value = copy_node (value);
5191 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5192 }
6414bad6 5193 }
8bcd6380 5194 else if (TREE_OVERFLOW (value))
d8e1f97b 5195 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5196 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5197 }
5198 }
400fbf9f 5199
53cd18ec
JM
5200 /* Don't let a cast be an lvalue. */
5201 if (value == expr)
db3927fb 5202 value = non_lvalue_loc (loc, value);
e9a25f70 5203
928c19bb
JM
5204 /* Don't allow the results of casting to floating-point or complex
5205 types be confused with actual constants, or casts involving
5206 integer and pointer types other than direct integer-to-integer
5207 and integer-to-pointer be confused with integer constant
5208 expressions and null pointer constants. */
5209 if (TREE_CODE (value) == REAL_CST
5210 || TREE_CODE (value) == COMPLEX_CST
5211 || (TREE_CODE (value) == INTEGER_CST
5212 && !((TREE_CODE (expr) == INTEGER_CST
5213 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5214 || TREE_CODE (expr) == REAL_CST
5215 || TREE_CODE (expr) == COMPLEX_CST)))
5216 value = build1 (NOP_EXPR, type, value);
5217
c2255bc4
AH
5218 if (CAN_HAVE_LOCATION_P (value))
5219 SET_EXPR_LOCATION (value, loc);
3e4093b6 5220 return value;
400fbf9f
JW
5221}
5222
c2255bc4
AH
5223/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5224 location of the open paren of the cast, or the position of the cast
5225 expr. */
3e4093b6 5226tree
c2255bc4 5227c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5228{
f8893e47 5229 tree type;
928c19bb
JM
5230 tree type_expr = NULL_TREE;
5231 bool type_expr_const = true;
5232 tree ret;
3e4093b6 5233 int saved_wsp = warn_strict_prototypes;
c5c76735 5234
3e4093b6
RS
5235 /* This avoids warnings about unprototyped casts on
5236 integers. E.g. "#define SIG_DFL (void(*)())0". */
5237 if (TREE_CODE (expr) == INTEGER_CST)
5238 warn_strict_prototypes = 0;
928c19bb 5239 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5240 warn_strict_prototypes = saved_wsp;
c5c76735 5241
c2255bc4 5242 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5243 if (type_expr)
5244 {
9f33203d
JM
5245 bool inner_expr_const = true;
5246 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5247 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5248 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5249 && inner_expr_const);
c2255bc4 5250 SET_EXPR_LOCATION (ret, loc);
928c19bb 5251 }
24b97832
ILT
5252
5253 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5254 SET_EXPR_LOCATION (ret, loc);
5255
9e5b2115
PB
5256 /* C++ does not permits types to be defined in a cast, but it
5257 allows references to incomplete types. */
5258 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5259 warning_at (loc, OPT_Wc___compat,
5260 "defining a type in a cast is invalid in C++");
5261
928c19bb 5262 return ret;
400fbf9f 5263}
3e4093b6
RS
5264\f
5265/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5266 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5267 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5268 MODIFYCODE is the code for a binary operator that we use
5269 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5270 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5271 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5272 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5273
c2255bc4
AH
5274 LOCATION is the location of the MODIFYCODE operator.
5275 RHS_LOC is the location of the RHS. */
2f6e4e97 5276
3e4093b6 5277tree
32e8bb8e 5278build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5279 enum tree_code modifycode,
c2255bc4 5280 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5281{
3e4093b6
RS
5282 tree result;
5283 tree newrhs;
241f845a 5284 tree rhseval = NULL_TREE;
8ce94e44 5285 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5286 tree lhstype = TREE_TYPE (lhs);
5287 tree olhstype = lhstype;
928c19bb 5288 bool npc;
267bac10 5289 bool is_atomic_op;
e9a25f70 5290
3e4093b6
RS
5291 /* Types that aren't fully specified cannot be used in assignments. */
5292 lhs = require_complete_type (lhs);
e9a25f70 5293
3e4093b6
RS
5294 /* Avoid duplicate error messages from operands that had errors. */
5295 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5296 return error_mark_node;
400fbf9f 5297
4c2ecab0
JM
5298 /* Ensure an error for assigning a non-lvalue array to an array in
5299 C90. */
5300 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5301 {
5302 error_at (location, "assignment to expression with array type");
5303 return error_mark_node;
5304 }
5305
46a88c12 5306 /* For ObjC properties, defer this check. */
7bd11157 5307 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5308 return error_mark_node;
5309
267bac10
JM
5310 is_atomic_op = really_atomic_lvalue (lhs);
5311
8ce94e44
JM
5312 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5313 {
5314 rhs_semantic_type = TREE_TYPE (rhs);
5315 rhs = TREE_OPERAND (rhs, 0);
5316 }
5317
3e4093b6 5318 newrhs = rhs;
400fbf9f 5319
928c19bb
JM
5320 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5321 {
5322 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5323 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5324 rhs_origtype);
928c19bb
JM
5325 if (inner == error_mark_node)
5326 return error_mark_node;
5327 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5328 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5329 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5330 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5331 protected_set_expr_location (result, location);
5332 return result;
5333 }
5334
3e4093b6
RS
5335 /* If a binary op has been requested, combine the old LHS value with the RHS
5336 producing the value we should actually store into the LHS. */
5337
5338 if (modifycode != NOP_EXPR)
400fbf9f 5339 {
928c19bb 5340 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5341 lhs = stabilize_reference (lhs);
bbbbb16a 5342
267bac10
JM
5343 /* Construct the RHS for any non-atomic compound assignemnt. */
5344 if (!is_atomic_op)
5345 {
241f845a
JJ
5346 /* If in LHS op= RHS the RHS has side-effects, ensure they
5347 are preevaluated before the rest of the assignment expression's
5348 side-effects, because RHS could contain e.g. function calls
5349 that modify LHS. */
5350 if (TREE_SIDE_EFFECTS (rhs))
5351 {
5352 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5353 rhseval = newrhs;
5354 }
267bac10 5355 newrhs = build_binary_op (location,
241f845a 5356 modifycode, lhs, newrhs, 1);
267bac10
JM
5357
5358 /* The original type of the right hand side is no longer
5359 meaningful. */
5360 rhs_origtype = NULL_TREE;
5361 }
400fbf9f 5362 }
400fbf9f 5363
668ea4b1
IS
5364 if (c_dialect_objc ())
5365 {
46a88c12
NP
5366 /* Check if we are modifying an Objective-C property reference;
5367 if so, we need to generate setter calls. */
5368 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5369 if (result)
241f845a 5370 goto return_result;
46a88c12
NP
5371
5372 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5373 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5374 return error_mark_node;
5375 }
5376
9bf24266 5377 /* Give an error for storing in something that is 'const'. */
bbbd6700 5378
f37acdf9 5379 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5380 || ((TREE_CODE (lhstype) == RECORD_TYPE
5381 || TREE_CODE (lhstype) == UNION_TYPE)
5382 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5383 {
c02065fc 5384 readonly_error (location, lhs, lv_assign);
953ff289
DN
5385 return error_mark_node;
5386 }
f37acdf9
JM
5387 else if (TREE_READONLY (lhs))
5388 readonly_warning (lhs, lv_assign);
bbbd6700 5389
3e4093b6
RS
5390 /* If storing into a structure or union member,
5391 it has probably been given type `int'.
5392 Compute the type that would go with
5393 the actual amount of storage the member occupies. */
bbbd6700 5394
3e4093b6
RS
5395 if (TREE_CODE (lhs) == COMPONENT_REF
5396 && (TREE_CODE (lhstype) == INTEGER_TYPE
5397 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5398 || TREE_CODE (lhstype) == REAL_TYPE
5399 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5400 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5401
3e4093b6
RS
5402 /* If storing in a field that is in actuality a short or narrower than one,
5403 we must store in the field in its actual type. */
5404
5405 if (lhstype != TREE_TYPE (lhs))
5406 {
5407 lhs = copy_node (lhs);
5408 TREE_TYPE (lhs) = lhstype;
400fbf9f 5409 }
400fbf9f 5410
32e8bb8e
ILT
5411 /* Issue -Wc++-compat warnings about an assignment to an enum type
5412 when LHS does not have its original type. This happens for,
5413 e.g., an enum bitfield in a struct. */
5414 if (warn_cxx_compat
5415 && lhs_origtype != NULL_TREE
5416 && lhs_origtype != lhstype
5417 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5418 {
5419 tree checktype = (rhs_origtype != NULL_TREE
5420 ? rhs_origtype
5421 : TREE_TYPE (rhs));
5422 if (checktype != error_mark_node
267bac10
JM
5423 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5424 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5425 warning_at (location, OPT_Wc___compat,
5426 "enum conversion in assignment is invalid in C++");
5427 }
5428
267bac10
JM
5429 /* If the lhs is atomic, remove that qualifier. */
5430 if (is_atomic_op)
5431 {
5432 lhstype = build_qualified_type (lhstype,
5433 (TYPE_QUALS (lhstype)
5434 & ~TYPE_QUAL_ATOMIC));
5435 olhstype = build_qualified_type (olhstype,
5436 (TYPE_QUALS (lhstype)
5437 & ~TYPE_QUAL_ATOMIC));
5438 }
5439
8ce94e44
JM
5440 /* Convert new value to destination type. Fold it first, then
5441 restore any excess precision information, for the sake of
5442 conversion warnings. */
400fbf9f 5443
267bac10
JM
5444 if (!(is_atomic_op && modifycode != NOP_EXPR))
5445 {
5446 npc = null_pointer_constant_p (newrhs);
5447 newrhs = c_fully_fold (newrhs, false, NULL);
5448 if (rhs_semantic_type)
5449 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5450 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5451 rhs_origtype, ic_assign, npc,
5452 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5453 if (TREE_CODE (newrhs) == ERROR_MARK)
5454 return error_mark_node;
5455 }
400fbf9f 5456
6e955430
ZL
5457 /* Emit ObjC write barrier, if necessary. */
5458 if (c_dialect_objc () && flag_objc_gc)
5459 {
5460 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5461 if (result)
c9f9eb5d
AH
5462 {
5463 protected_set_expr_location (result, location);
241f845a 5464 goto return_result;
c9f9eb5d 5465 }
6e955430
ZL
5466 }
5467
ea4b7848 5468 /* Scan operands. */
400fbf9f 5469
267bac10
JM
5470 if (is_atomic_op)
5471 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5472 else
5473 {
5474 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5475 TREE_SIDE_EFFECTS (result) = 1;
5476 protected_set_expr_location (result, location);
5477 }
400fbf9f 5478
3e4093b6
RS
5479 /* If we got the LHS in a different type for storing in,
5480 convert the result back to the nominal type of LHS
5481 so that the value we return always has the same type
5482 as the LHS argument. */
e855c5ce 5483
3e4093b6 5484 if (olhstype == TREE_TYPE (result))
241f845a 5485 goto return_result;
c9f9eb5d 5486
68fca595
MP
5487 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5488 rhs_origtype, ic_assign, false, NULL_TREE,
5489 NULL_TREE, 0);
c9f9eb5d 5490 protected_set_expr_location (result, location);
241f845a
JJ
5491
5492return_result:
5493 if (rhseval)
5494 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5495 return result;
3e4093b6
RS
5496}
5497\f
478a1c5b
ILT
5498/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5499 This is used to implement -fplan9-extensions. */
5500
5501static bool
5502find_anonymous_field_with_type (tree struct_type, tree type)
5503{
5504 tree field;
5505 bool found;
5506
5507 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5508 || TREE_CODE (struct_type) == UNION_TYPE);
5509 found = false;
5510 for (field = TYPE_FIELDS (struct_type);
5511 field != NULL_TREE;
5512 field = TREE_CHAIN (field))
5513 {
267bac10
JM
5514 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5515 ? c_build_qualified_type (TREE_TYPE (field),
5516 TYPE_QUAL_ATOMIC)
5517 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5518 if (DECL_NAME (field) == NULL
267bac10 5519 && comptypes (type, fieldtype))
478a1c5b
ILT
5520 {
5521 if (found)
5522 return false;
5523 found = true;
5524 }
5525 else if (DECL_NAME (field) == NULL
5526 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5527 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5528 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5529 {
5530 if (found)
5531 return false;
5532 found = true;
5533 }
5534 }
5535 return found;
5536}
5537
5538/* RHS is an expression whose type is pointer to struct. If there is
5539 an anonymous field in RHS with type TYPE, then return a pointer to
5540 that field in RHS. This is used with -fplan9-extensions. This
5541 returns NULL if no conversion could be found. */
5542
5543static tree
5544convert_to_anonymous_field (location_t location, tree type, tree rhs)
5545{
5546 tree rhs_struct_type, lhs_main_type;
5547 tree field, found_field;
5548 bool found_sub_field;
5549 tree ret;
5550
5551 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5552 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5553 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5554 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5555
5556 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5557 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5558 ? c_build_qualified_type (TREE_TYPE (type),
5559 TYPE_QUAL_ATOMIC)
5560 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5561
5562 found_field = NULL_TREE;
5563 found_sub_field = false;
5564 for (field = TYPE_FIELDS (rhs_struct_type);
5565 field != NULL_TREE;
5566 field = TREE_CHAIN (field))
5567 {
5568 if (DECL_NAME (field) != NULL_TREE
5569 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5570 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5571 continue;
267bac10
JM
5572 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5573 ? c_build_qualified_type (TREE_TYPE (field),
5574 TYPE_QUAL_ATOMIC)
5575 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5576 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5577 {
5578 if (found_field != NULL_TREE)
5579 return NULL_TREE;
5580 found_field = field;
5581 }
5582 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5583 lhs_main_type))
5584 {
5585 if (found_field != NULL_TREE)
5586 return NULL_TREE;
5587 found_field = field;
5588 found_sub_field = true;
5589 }
5590 }
5591
5592 if (found_field == NULL_TREE)
5593 return NULL_TREE;
5594
5595 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5596 build_fold_indirect_ref (rhs), found_field,
5597 NULL_TREE);
5598 ret = build_fold_addr_expr_loc (location, ret);
5599
5600 if (found_sub_field)
5601 {
5602 ret = convert_to_anonymous_field (location, type, ret);
5603 gcc_assert (ret != NULL_TREE);
5604 }
5605
5606 return ret;
5607}
5608
63bc4e87
MP
5609/* Issue an error message for a bad initializer component.
5610 GMSGID identifies the message.
5611 The component name is taken from the spelling stack. */
5612
5613static void
ea58ef42 5614error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5615{
5616 char *ofwhat;
5617
5618 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5619 error_at (loc, gmsgid);
63bc4e87
MP
5620 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5621 if (*ofwhat)
d7ff7ae5 5622 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5623}
5624
5625/* Issue a pedantic warning for a bad initializer component. OPT is
5626 the option OPT_* (from options.h) controlling this warning or 0 if
5627 it is unconditionally given. GMSGID identifies the message. The
5628 component name is taken from the spelling stack. */
5629
5630static void
5631pedwarn_init (location_t location, 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 = pedwarn (location, opt, gmsgid);
63bc4e87 5638 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5639 if (*ofwhat && warned)
5640 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5641}
5642
5643/* Issue a warning for a bad initializer component.
5644
5645 OPT is the OPT_W* value corresponding to the warning option that
5646 controls this warning. GMSGID identifies the message. The
5647 component name is taken from the spelling stack. */
5648
5649static void
5650warning_init (location_t loc, int opt, const char *gmsgid)
5651{
5652 char *ofwhat;
d7ff7ae5 5653 bool warned;
63bc4e87
MP
5654
5655 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5656 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5657 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5658 if (*ofwhat && warned)
5659 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5660}
5661\f
5662/* If TYPE is an array type and EXPR is a parenthesized string
5663 constant, warn if pedantic that EXPR is being used to initialize an
5664 object of type TYPE. */
5665
5666void
d033409e 5667maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5668{
5669 if (pedantic
5670 && TREE_CODE (type) == ARRAY_TYPE
5671 && TREE_CODE (expr.value) == STRING_CST
5672 && expr.original_code != STRING_CST)
d033409e 5673 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5674 "array initialized from parenthesized string constant");
5675}
5676
bbbbb16a
ILT
5677/* Convert value RHS to type TYPE as preparation for an assignment to
5678 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5679 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5680 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5681 constant before any folding.
3e4093b6
RS
5682 The real work of conversion is done by `convert'.
5683 The purpose of this function is to generate error messages
5684 for assignments that are not allowed in C.
2ac2f164
JM
5685 ERRTYPE says whether it is argument passing, assignment,
5686 initialization or return.
2f6e4e97 5687
81e5eca8
MP
5688 LOCATION is the location of the assignment, EXPR_LOC is the location of
5689 the RHS or, for a function, location of an argument.
2ac2f164 5690 FUNCTION is a tree for the function being called.
3e4093b6 5691 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5692
3e4093b6 5693static tree
81e5eca8 5694convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5695 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5696 bool null_pointer_constant, tree fundecl,
5697 tree function, int parmnum)
3e4093b6
RS
5698{
5699 enum tree_code codel = TREE_CODE (type);
8ce94e44 5700 tree orig_rhs = rhs;
3e4093b6
RS
5701 tree rhstype;
5702 enum tree_code coder;
2ac2f164 5703 tree rname = NULL_TREE;
58393038 5704 bool objc_ok = false;
2ac2f164 5705
6b4ef5c1 5706 if (errtype == ic_argpass)
2ac2f164
JM
5707 {
5708 tree selector;
5709 /* Change pointer to function to the function itself for
5710 diagnostics. */
5711 if (TREE_CODE (function) == ADDR_EXPR
5712 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5713 function = TREE_OPERAND (function, 0);
5714
5715 /* Handle an ObjC selector specially for diagnostics. */
5716 selector = objc_message_selector ();
5717 rname = function;
5718 if (selector && parmnum > 2)
5719 {
5720 rname = selector;
5721 parmnum -= 2;
5722 }
5723 }
5724
5725 /* This macro is used to emit diagnostics to ensure that all format
5726 strings are complete sentences, visible to gettext and checked at
5727 compile time. */
768952be 5728#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5729 do { \
5730 switch (errtype) \
5731 { \
5732 case ic_argpass: \
5c1bc275 5733 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5734 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5735 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5736 "expected %qT but argument is of type %qT", \
5737 type, rhstype); \
5738 break; \
1e053dfe
MLI
5739 case ic_assign: \
5740 pedwarn (LOCATION, OPT, AS); \
5741 break; \
5742 case ic_init: \
6a8f4e12 5743 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5744 break; \
5745 case ic_return: \
d033409e 5746 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5747 break; \
5748 default: \
5749 gcc_unreachable (); \
5750 } \
2ac2f164 5751 } while (0)
cb3ca04e 5752
49706e39
MLI
5753 /* This macro is used to emit diagnostics to ensure that all format
5754 strings are complete sentences, visible to gettext and checked at
768952be 5755 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5756 extra parameter to enumerate qualifiers. */
768952be 5757#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5758 do { \
5759 switch (errtype) \
5760 { \
5761 case ic_argpass: \
5c1bc275 5762 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5763 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5764 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5765 "expected %qT but argument is of type %qT", \
5766 type, rhstype); \
5767 break; \
5768 case ic_assign: \
5c1bc275 5769 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5770 break; \
5771 case ic_init: \
5c1bc275 5772 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5773 break; \
5774 case ic_return: \
5c1bc275 5775 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5776 break; \
5777 default: \
5778 gcc_unreachable (); \
5779 } \
5780 } while (0)
5781
768952be
MU
5782 /* This macro is used to emit diagnostics to ensure that all format
5783 strings are complete sentences, visible to gettext and checked at
5784 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5785 warning_at instead of pedwarn. */
5786#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5787 do { \
5788 switch (errtype) \
5789 { \
5790 case ic_argpass: \
5791 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5792 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5793 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5794 "expected %qT but argument is of type %qT", \
5795 type, rhstype); \
5796 break; \
5797 case ic_assign: \
5798 warning_at (LOCATION, OPT, AS, QUALS); \
5799 break; \
5800 case ic_init: \
5801 warning_at (LOCATION, OPT, IN, QUALS); \
5802 break; \
5803 case ic_return: \
5804 warning_at (LOCATION, OPT, RE, QUALS); \
5805 break; \
5806 default: \
5807 gcc_unreachable (); \
5808 } \
5809 } while (0)
5810
8ce94e44
JM
5811 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5812 rhs = TREE_OPERAND (rhs, 0);
5813
3e4093b6
RS
5814 rhstype = TREE_TYPE (rhs);
5815 coder = TREE_CODE (rhstype);
5816
5817 if (coder == ERROR_MARK)
5818 return error_mark_node;
5819
58393038
ZL
5820 if (c_dialect_objc ())
5821 {
5822 int parmno;
5823
5824 switch (errtype)
5825 {
5826 case ic_return:
5827 parmno = 0;
5828 break;
5829
5830 case ic_assign:
5831 parmno = -1;
5832 break;
5833
5834 case ic_init:
5835 parmno = -2;
5836 break;
5837
5838 default:
5839 parmno = parmnum;
5840 break;
5841 }
5842
5843 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5844 }
5845
bbbbb16a
ILT
5846 if (warn_cxx_compat)
5847 {
5848 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5849 if (checktype != error_mark_node
5850 && TREE_CODE (type) == ENUMERAL_TYPE
5851 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5852 {
768952be
MU
5853 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5854 G_("enum conversion when passing argument "
5855 "%d of %qE is invalid in C++"),
5856 G_("enum conversion in assignment is "
5857 "invalid in C++"),
5858 G_("enum conversion in initialization is "
5859 "invalid in C++"),
5860 G_("enum conversion in return is "
5861 "invalid in C++"));
bbbbb16a
ILT
5862 }
5863 }
5864
3e4093b6 5865 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5866 return rhs;
3e4093b6
RS
5867
5868 if (coder == VOID_TYPE)
400fbf9f 5869 {
6dcc04b0
JM
5870 /* Except for passing an argument to an unprototyped function,
5871 this is a constraint violation. When passing an argument to
5872 an unprototyped function, it is compile-time undefined;
5873 making it a constraint in that case was rejected in
5874 DR#252. */
c2255bc4 5875 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5876 return error_mark_node;
400fbf9f 5877 }
808d6eaa
JM
5878 rhs = require_complete_type (rhs);
5879 if (rhs == error_mark_node)
5880 return error_mark_node;
cd192ccc
MS
5881 /* A non-reference type can convert to a reference. This handles
5882 va_start, va_copy and possibly port built-ins. */
5883 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5884 {
3e4093b6 5885 if (!lvalue_p (rhs))
400fbf9f 5886 {
c2255bc4 5887 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5888 return error_mark_node;
400fbf9f 5889 }
3e4093b6
RS
5890 if (!c_mark_addressable (rhs))
5891 return error_mark_node;
5892 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5893 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5894
81e5eca8 5895 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5896 build_pointer_type (TREE_TYPE (type)),
5897 rhs, origtype, errtype,
5898 null_pointer_constant, fundecl, function,
5899 parmnum);
cd192ccc
MS
5900 if (rhs == error_mark_node)
5901 return error_mark_node;
3e4093b6
RS
5902
5903 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5904 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5905 return rhs;
400fbf9f 5906 }
3e4093b6 5907 /* Some types can interconvert without explicit casts. */
3274deff 5908 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5909 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5910 return convert (type, rhs);
5911 /* Arithmetic types all interconvert, and enum is treated like int. */
5912 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5913 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5914 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5915 || codel == BOOLEAN_TYPE)
5916 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5917 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5918 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5919 || coder == BOOLEAN_TYPE))
928c19bb
JM
5920 {
5921 tree ret;
5922 bool save = in_late_binary_op;
e5341100
JJ
5923 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5924 || (coder == REAL_TYPE
5925 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5926 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 5927 in_late_binary_op = true;
81e5eca8
MP
5928 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5929 ? expr_loc : location, type, orig_rhs);
e5341100 5930 in_late_binary_op = save;
928c19bb
JM
5931 return ret;
5932 }
400fbf9f 5933
79077aea
JJ
5934 /* Aggregates in different TUs might need conversion. */
5935 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5936 && codel == coder
5937 && comptypes (type, rhstype))
81e5eca8
MP
5938 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5939 ? expr_loc : location, type, rhs);
79077aea 5940
ebf0bf7f 5941 /* Conversion to a transparent union or record from its member types.
3e4093b6 5942 This applies only to function arguments. */
ebf0bf7f
JJ
5943 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5944 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5945 && errtype == ic_argpass)
400fbf9f 5946 {
0257e383 5947 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5948
910ad8de 5949 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5950 {
0257e383 5951 tree memb_type = TREE_TYPE (memb);
400fbf9f 5952
3e4093b6 5953 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5954 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5955 break;
e58cd767 5956
3e4093b6
RS
5957 if (TREE_CODE (memb_type) != POINTER_TYPE)
5958 continue;
2f6e4e97 5959
3e4093b6
RS
5960 if (coder == POINTER_TYPE)
5961 {
5962 tree ttl = TREE_TYPE (memb_type);
5963 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5964
3e4093b6
RS
5965 /* Any non-function converts to a [const][volatile] void *
5966 and vice versa; otherwise, targets must be the same.
5967 Meanwhile, the lhs target must have all the qualifiers of
5968 the rhs. */
267bac10
JM
5969 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5970 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5971 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5972 {
267bac10
JM
5973 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5974 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5975 /* If this type won't generate any warnings, use it. */
267bac10 5976 if (lquals == rquals
3e4093b6
RS
5977 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5978 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5979 ? ((lquals | rquals) == rquals)
5980 : ((lquals | rquals) == lquals)))
3e4093b6 5981 break;
400fbf9f 5982
3e4093b6 5983 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5984 if (!marginal_memb)
5985 marginal_memb = memb;
3e4093b6
RS
5986 }
5987 }
82bde854 5988
3e4093b6 5989 /* Can convert integer zero to any pointer type. */
928c19bb 5990 if (null_pointer_constant)
3e4093b6
RS
5991 {
5992 rhs = null_pointer_node;
5993 break;
5994 }
5995 }
400fbf9f 5996
0257e383 5997 if (memb || marginal_memb)
3e4093b6 5998 {
0257e383 5999 if (!memb)
3e4093b6
RS
6000 {
6001 /* We have only a marginally acceptable member type;
6002 it needs a warning. */
0257e383 6003 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6004 tree ttr = TREE_TYPE (rhstype);
714a0864 6005
3e4093b6
RS
6006 /* Const and volatile mean something different for function
6007 types, so the usual warnings are not appropriate. */
6008 if (TREE_CODE (ttr) == FUNCTION_TYPE
6009 && TREE_CODE (ttl) == FUNCTION_TYPE)
6010 {
6011 /* Because const and volatile on functions are
6012 restrictions that say the function will not do
6013 certain things, it is okay to use a const or volatile
6014 function where an ordinary one is wanted, but not
6015 vice-versa. */
36c5e70a
BE
6016 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6017 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6018 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6019 OPT_Wdiscarded_qualifiers,
6020 G_("passing argument %d of %qE "
6021 "makes %q#v qualified function "
6022 "pointer from unqualified"),
6023 G_("assignment makes %q#v qualified "
6024 "function pointer from "
6025 "unqualified"),
6026 G_("initialization makes %q#v qualified "
6027 "function pointer from "
6028 "unqualified"),
6029 G_("return makes %q#v qualified function "
6030 "pointer from unqualified"),
6031 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6032 }
36c5e70a
BE
6033 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6034 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6035 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6036 OPT_Wdiscarded_qualifiers,
6037 G_("passing argument %d of %qE discards "
6038 "%qv qualifier from pointer target type"),
6039 G_("assignment discards %qv qualifier "
6040 "from pointer target type"),
6041 G_("initialization discards %qv qualifier "
6042 "from pointer target type"),
6043 G_("return discards %qv qualifier from "
6044 "pointer target type"),
6045 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6046
6047 memb = marginal_memb;
3e4093b6 6048 }
400fbf9f 6049
fcf73884 6050 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6051 pedwarn (location, OPT_Wpedantic,
fcf73884 6052 "ISO C prohibits argument conversion to union type");
0e7c47fa 6053
db3927fb 6054 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6055 return build_constructor_single (type, memb, rhs);
3e4093b6 6056 }
0e7c47fa
RK
6057 }
6058
3e4093b6
RS
6059 /* Conversions among pointers */
6060 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6061 && (coder == codel))
400fbf9f 6062 {
3e4093b6
RS
6063 tree ttl = TREE_TYPE (type);
6064 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6065 tree mvl = ttl;
6066 tree mvr = ttr;
3e4093b6 6067 bool is_opaque_pointer;
264fa2db 6068 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6069 addr_space_t asl;
6070 addr_space_t asr;
400fbf9f 6071
46df2823 6072 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6073 mvl = (TYPE_ATOMIC (mvl)
6074 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6075 TYPE_QUAL_ATOMIC)
6076 : TYPE_MAIN_VARIANT (mvl));
46df2823 6077 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6078 mvr = (TYPE_ATOMIC (mvr)
6079 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6080 TYPE_QUAL_ATOMIC)
6081 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6082 /* Opaque pointers are treated like void pointers. */
f83c7f63 6083 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6084
478a1c5b
ILT
6085 /* The Plan 9 compiler permits a pointer to a struct to be
6086 automatically converted into a pointer to an anonymous field
6087 within the struct. */
6088 if (flag_plan9_extensions
6089 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6090 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6091 && mvl != mvr)
6092 {
6093 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6094 if (new_rhs != NULL_TREE)
6095 {
6096 rhs = new_rhs;
6097 rhstype = TREE_TYPE (rhs);
6098 coder = TREE_CODE (rhstype);
6099 ttr = TREE_TYPE (rhstype);
6100 mvr = TYPE_MAIN_VARIANT (ttr);
6101 }
6102 }
6103
b7e20b53 6104 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6105 for the purpose of reducing the number of false positives, we
6106 tolerate the special case of
b7e20b53 6107
c22cacf3 6108 int *p = NULL;
b7e20b53 6109
c22cacf3 6110 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6111 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6112 warning_at (errtype == ic_argpass ? expr_loc : location,
6113 OPT_Wc___compat,
6114 "request for implicit conversion "
c2255bc4 6115 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6116
36c5e70a
BE
6117 /* See if the pointers point to incompatible address spaces. */
6118 asl = TYPE_ADDR_SPACE (ttl);
6119 asr = TYPE_ADDR_SPACE (ttr);
6120 if (!null_pointer_constant_p (rhs)
6121 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6122 {
6123 switch (errtype)
6124 {
6125 case ic_argpass:
8ffcdea8 6126 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6127 "non-enclosed address space", parmnum, rname);
6128 break;
6129 case ic_assign:
6130 error_at (location, "assignment from pointer to "
6131 "non-enclosed address space");
6132 break;
6133 case ic_init:
6134 error_at (location, "initialization from pointer to "
6135 "non-enclosed address space");
6136 break;
6137 case ic_return:
6138 error_at (location, "return from pointer to "
6139 "non-enclosed address space");
6140 break;
6141 default:
6142 gcc_unreachable ();
6143 }
6144 return error_mark_node;
6145 }
6146
7876a414
KG
6147 /* Check if the right-hand side has a format attribute but the
6148 left-hand side doesn't. */
90137d8f 6149 if (warn_suggest_attribute_format
104f8784 6150 && check_missing_format_attribute (type, rhstype))
c22cacf3 6151 {
104f8784
KG
6152 switch (errtype)
6153 {
6154 case ic_argpass:
8ffcdea8 6155 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6156 "argument %d of %qE might be "
6157 "a candidate for a format attribute",
6158 parmnum, rname);
104f8784
KG
6159 break;
6160 case ic_assign:
90137d8f 6161 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6162 "assignment left-hand side might be "
6163 "a candidate for a format attribute");
104f8784
KG
6164 break;
6165 case ic_init:
90137d8f 6166 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6167 "initialization left-hand side might be "
6168 "a candidate for a format attribute");
104f8784
KG
6169 break;
6170 case ic_return:
90137d8f 6171 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6172 "return type might be "
6173 "a candidate for a format attribute");
104f8784
KG
6174 break;
6175 default:
6176 gcc_unreachable ();
6177 }
7876a414 6178 }
c22cacf3 6179
3e4093b6
RS
6180 /* Any non-function converts to a [const][volatile] void *
6181 and vice versa; otherwise, targets must be the same.
6182 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6183 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6184 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6185 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6186 || is_opaque_pointer
f8a93a2e
JJ
6187 || ((c_common_unsigned_type (mvl)
6188 == c_common_unsigned_type (mvr))
267bac10
JM
6189 && (c_common_signed_type (mvl)
6190 == c_common_signed_type (mvr))
6191 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6192 {
768952be
MU
6193 /* Warn about loss of qualifers from pointers to arrays with
6194 qualifiers on the element type. */
6195 if (TREE_CODE (ttr) == ARRAY_TYPE)
6196 {
6197 ttr = strip_array_types (ttr);
6198 ttl = strip_array_types (ttl);
6199
6200 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6201 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6202 WARNING_FOR_QUALIFIERS (location, expr_loc,
6203 OPT_Wdiscarded_array_qualifiers,
6204 G_("passing argument %d of %qE discards "
6205 "%qv qualifier from pointer target type"),
6206 G_("assignment discards %qv qualifier "
6207 "from pointer target type"),
6208 G_("initialization discards %qv qualifier "
6209 "from pointer target type"),
6210 G_("return discards %qv qualifier from "
6211 "pointer target type"),
6212 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6213 }
6214 else if (pedantic
3e4093b6
RS
6215 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6216 ||
6217 (VOID_TYPE_P (ttr)
928c19bb 6218 && !null_pointer_constant
3e4093b6 6219 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6220 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6221 G_("ISO C forbids passing argument %d of "
6222 "%qE between function pointer "
6223 "and %<void *%>"),
6224 G_("ISO C forbids assignment between "
6225 "function pointer and %<void *%>"),
6226 G_("ISO C forbids initialization between "
6227 "function pointer and %<void *%>"),
6228 G_("ISO C forbids return between function "
6229 "pointer and %<void *%>"));
3e4093b6
RS
6230 /* Const and volatile mean something different for function types,
6231 so the usual warnings are not appropriate. */
6232 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6233 && TREE_CODE (ttl) != FUNCTION_TYPE)
6234 {
768952be
MU
6235 /* Don't warn about loss of qualifier for conversions from
6236 qualified void* to pointers to arrays with corresponding
6237 qualifier on the element type. */
6238 if (!pedantic)
6239 ttl = strip_array_types (ttl);
6240
267bac10
JM
6241 /* Assignments between atomic and non-atomic objects are OK. */
6242 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6243 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6244 {
768952be
MU
6245 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6246 OPT_Wdiscarded_qualifiers,
6247 G_("passing argument %d of %qE discards "
6248 "%qv qualifier from pointer target type"),
6249 G_("assignment discards %qv qualifier "
6250 "from pointer target type"),
6251 G_("initialization discards %qv qualifier "
6252 "from pointer target type"),
6253 G_("return discards %qv qualifier from "
6254 "pointer target type"),
6255 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6256 }
3e4093b6
RS
6257 /* If this is not a case of ignoring a mismatch in signedness,
6258 no warning. */
6259 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6260 || target_cmp)
3e4093b6
RS
6261 ;
6262 /* If there is a mismatch, do warn. */
f2fd3821 6263 else if (warn_pointer_sign)
768952be
MU
6264 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6265 G_("pointer targets in passing argument "
6266 "%d of %qE differ in signedness"),
6267 G_("pointer targets in assignment "
6268 "differ in signedness"),
6269 G_("pointer targets in initialization "
6270 "differ in signedness"),
6271 G_("pointer targets in return differ "
6272 "in signedness"));
3e4093b6
RS
6273 }
6274 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6275 && TREE_CODE (ttr) == FUNCTION_TYPE)
6276 {
6277 /* Because const and volatile on functions are restrictions
6278 that say the function will not do certain things,
6279 it is okay to use a const or volatile function
6280 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6281 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6282 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6283 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6284 OPT_Wdiscarded_qualifiers,
6285 G_("passing argument %d of %qE makes "
6286 "%q#v qualified function pointer "
6287 "from unqualified"),
6288 G_("assignment makes %q#v qualified function "
6289 "pointer from unqualified"),
6290 G_("initialization makes %q#v qualified "
6291 "function pointer from unqualified"),
6292 G_("return makes %q#v qualified function "
6293 "pointer from unqualified"),
6294 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6295 }
6296 }
6297 else
58393038
ZL
6298 /* Avoid warning about the volatile ObjC EH puts on decls. */
6299 if (!objc_ok)
768952be
MU
6300 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6301 OPT_Wincompatible_pointer_types,
6302 G_("passing argument %d of %qE from "
6303 "incompatible pointer type"),
6304 G_("assignment from incompatible pointer type"),
6305 G_("initialization from incompatible "
6306 "pointer type"),
6307 G_("return from incompatible pointer type"));
58393038 6308
3e4093b6
RS
6309 return convert (type, rhs);
6310 }
b494fd98
EB
6311 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6312 {
6dcc04b0
JM
6313 /* ??? This should not be an error when inlining calls to
6314 unprototyped functions. */
c2255bc4 6315 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6316 return error_mark_node;
6317 }
3e4093b6 6318 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6319 {
3e4093b6
RS
6320 /* An explicit constant 0 can convert to a pointer,
6321 or one that results from arithmetic, even including
6322 a cast to integer type. */
928c19bb 6323 if (!null_pointer_constant)
768952be
MU
6324 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6325 OPT_Wint_conversion,
6326 G_("passing argument %d of %qE makes "
6327 "pointer from integer without a cast"),
6328 G_("assignment makes pointer from integer "
6329 "without a cast"),
6330 G_("initialization makes pointer from "
6331 "integer without a cast"),
6332 G_("return makes pointer from integer "
6333 "without a cast"));
b3006337
EB
6334
6335 return convert (type, rhs);
400fbf9f 6336 }
3e4093b6 6337 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6338 {
768952be
MU
6339 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6340 OPT_Wint_conversion,
6341 G_("passing argument %d of %qE makes integer "
6342 "from pointer without a cast"),
6343 G_("assignment makes integer from pointer "
6344 "without a cast"),
6345 G_("initialization makes integer from pointer "
6346 "without a cast"),
6347 G_("return makes integer from pointer "
6348 "without a cast"));
3e4093b6
RS
6349 return convert (type, rhs);
6350 }
6351 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6352 {
6353 tree ret;
6354 bool save = in_late_binary_op;
6355 in_late_binary_op = true;
6356 ret = convert (type, rhs);
6357 in_late_binary_op = save;
6358 return ret;
6359 }
400fbf9f 6360
2ac2f164 6361 switch (errtype)
3e4093b6 6362 {
2ac2f164 6363 case ic_argpass:
8ffcdea8
MP
6364 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6365 rname);
a7da8b42 6366 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6367 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6368 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6369 break;
6370 case ic_assign:
c2255bc4
AH
6371 error_at (location, "incompatible types when assigning to type %qT from "
6372 "type %qT", type, rhstype);
2ac2f164
JM
6373 break;
6374 case ic_init:
c2255bc4 6375 error_at (location,
8ffcdea8 6376 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6377 type, rhstype);
2ac2f164
JM
6378 break;
6379 case ic_return:
c2255bc4 6380 error_at (location,
8ffcdea8 6381 "incompatible types when returning type %qT but %qT was "
c2255bc4 6382 "expected", rhstype, type);
2ac2f164
JM
6383 break;
6384 default:
6385 gcc_unreachable ();
400fbf9f 6386 }
53b01f59 6387
3e4093b6
RS
6388 return error_mark_node;
6389}
3e4093b6
RS
6390\f
6391/* If VALUE is a compound expr all of whose expressions are constant, then
6392 return its value. Otherwise, return error_mark_node.
15b732b2 6393
3e4093b6
RS
6394 This is for handling COMPOUND_EXPRs as initializer elements
6395 which is allowed with a warning when -pedantic is specified. */
15b732b2 6396
3e4093b6
RS
6397static tree
6398valid_compound_expr_initializer (tree value, tree endtype)
6399{
6400 if (TREE_CODE (value) == COMPOUND_EXPR)
6401 {
6402 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6403 == error_mark_node)
6404 return error_mark_node;
6405 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6406 endtype);
6407 }
116df786 6408 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6409 return error_mark_node;
6410 else
6411 return value;
15b732b2 6412}
400fbf9f 6413\f
3e4093b6
RS
6414/* Perform appropriate conversions on the initial value of a variable,
6415 store it in the declaration DECL,
6416 and print any error messages that are appropriate.
bbbbb16a 6417 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6418 If the init is invalid, store an ERROR_MARK.
6419
6420 INIT_LOC is the location of the initial value. */
400fbf9f 6421
3e4093b6 6422void
c2255bc4 6423store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6424{
3e4093b6 6425 tree value, type;
928c19bb 6426 bool npc = false;
400fbf9f 6427
3e4093b6 6428 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6429
3e4093b6
RS
6430 type = TREE_TYPE (decl);
6431 if (TREE_CODE (type) == ERROR_MARK)
6432 return;
400fbf9f 6433
3e4093b6 6434 /* Digest the specified initializer into an expression. */
400fbf9f 6435
928c19bb
JM
6436 if (init)
6437 npc = null_pointer_constant_p (init);
c2255bc4
AH
6438 value = digest_init (init_loc, type, init, origtype, npc,
6439 true, TREE_STATIC (decl));
400fbf9f 6440
3e4093b6 6441 /* Store the expression if valid; else report error. */
400fbf9f 6442
8400e75e 6443 if (!in_system_header_at (input_location)
3f75a254 6444 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6445 warning (OPT_Wtraditional, "traditional C rejects automatic "
6446 "aggregate initialization");
2f6e4e97 6447
dea63e49
JJ
6448 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6449 DECL_INITIAL (decl) = value;
400fbf9f 6450
3e4093b6
RS
6451 /* ANSI wants warnings about out-of-range constant initializers. */
6452 STRIP_TYPE_NOPS (value);
b8698a0f 6453 if (TREE_STATIC (decl))
c2658540 6454 constant_expression_warning (value);
400fbf9f 6455
3e4093b6
RS
6456 /* Check if we need to set array size from compound literal size. */
6457 if (TREE_CODE (type) == ARRAY_TYPE
6458 && TYPE_DOMAIN (type) == 0
6459 && value != error_mark_node)
400fbf9f 6460 {
3e4093b6
RS
6461 tree inside_init = init;
6462
ed248cf7 6463 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6464 inside_init = fold (inside_init);
6465
6466 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6467 {
8d9f82d5 6468 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6469
8d9f82d5 6470 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6471 {
6472 /* For int foo[] = (int [3]){1}; we need to set array size
6473 now since later on array initializer will be just the
6474 brace enclosed list of the compound literal. */
e30ecc5d 6475 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6476 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6477 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6478 layout_type (type);
8d9f82d5 6479 layout_decl (cldecl, 0);
e30ecc5d
JJ
6480 TREE_TYPE (decl)
6481 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6482 }
6483 }
400fbf9f 6484 }
3e4093b6
RS
6485}
6486\f
6487/* Methods for storing and printing names for error messages. */
400fbf9f 6488
3e4093b6
RS
6489/* Implement a spelling stack that allows components of a name to be pushed
6490 and popped. Each element on the stack is this structure. */
400fbf9f 6491
3e4093b6
RS
6492struct spelling
6493{
6494 int kind;
6495 union
400fbf9f 6496 {
a0f0ab9f 6497 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6498 const char *s;
6499 } u;
6500};
2f6e4e97 6501
3e4093b6
RS
6502#define SPELLING_STRING 1
6503#define SPELLING_MEMBER 2
6504#define SPELLING_BOUNDS 3
400fbf9f 6505
3e4093b6
RS
6506static struct spelling *spelling; /* Next stack element (unused). */
6507static struct spelling *spelling_base; /* Spelling stack base. */
6508static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6509
3e4093b6
RS
6510/* Macros to save and restore the spelling stack around push_... functions.
6511 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6512
3e4093b6
RS
6513#define SPELLING_DEPTH() (spelling - spelling_base)
6514#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6515
3e4093b6
RS
6516/* Push an element on the spelling stack with type KIND and assign VALUE
6517 to MEMBER. */
400fbf9f 6518
3e4093b6
RS
6519#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6520{ \
6521 int depth = SPELLING_DEPTH (); \
6522 \
6523 if (depth >= spelling_size) \
6524 { \
6525 spelling_size += 10; \
cca8ead2
BI
6526 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6527 spelling_size); \
3e4093b6
RS
6528 RESTORE_SPELLING_DEPTH (depth); \
6529 } \
6530 \
6531 spelling->kind = (KIND); \
6532 spelling->MEMBER = (VALUE); \
6533 spelling++; \
6534}
400fbf9f 6535
3e4093b6 6536/* Push STRING on the stack. Printed literally. */
400fbf9f 6537
3e4093b6
RS
6538static void
6539push_string (const char *string)
6540{
6541 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6542}
400fbf9f 6543
3e4093b6 6544/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6545
3e4093b6
RS
6546static void
6547push_member_name (tree decl)
6548{
6549 const char *const string
88388a52
JM
6550 = (DECL_NAME (decl)
6551 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6552 : _("<anonymous>"));
3e4093b6
RS
6553 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6554}
400fbf9f 6555
3e4093b6 6556/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6557
3e4093b6 6558static void
a0f0ab9f 6559push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6560{
6561 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6562}
bb58bec5 6563
3e4093b6 6564/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6565
3e4093b6
RS
6566static int
6567spelling_length (void)
6568{
6569 int size = 0;
6570 struct spelling *p;
400fbf9f 6571
3e4093b6
RS
6572 for (p = spelling_base; p < spelling; p++)
6573 {
6574 if (p->kind == SPELLING_BOUNDS)
6575 size += 25;
6576 else
6577 size += strlen (p->u.s) + 1;
6578 }
6579
6580 return size;
400fbf9f 6581}
400fbf9f 6582
3e4093b6 6583/* Print the spelling to BUFFER and return it. */
400fbf9f 6584
3e4093b6
RS
6585static char *
6586print_spelling (char *buffer)
400fbf9f 6587{
3e4093b6
RS
6588 char *d = buffer;
6589 struct spelling *p;
400fbf9f 6590
3e4093b6
RS
6591 for (p = spelling_base; p < spelling; p++)
6592 if (p->kind == SPELLING_BOUNDS)
6593 {
a0f0ab9f 6594 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6595 d += strlen (d);
6596 }
6597 else
6598 {
6599 const char *s;
6600 if (p->kind == SPELLING_MEMBER)
6601 *d++ = '.';
6602 for (s = p->u.s; (*d = *s++); d++)
6603 ;
6604 }
6605 *d++ = '\0';
6606 return buffer;
6607}
400fbf9f 6608
3e4093b6
RS
6609/* Digest the parser output INIT as an initializer for type TYPE.
6610 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6611
bbbbb16a
ILT
6612 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6613
928c19bb
JM
6614 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6615
916c5919
JM
6616 If INIT is a string constant, STRICT_STRING is true if it is
6617 unparenthesized or we should not warn here for it being parenthesized.
6618 For other types of INIT, STRICT_STRING is not used.
6619
c2255bc4
AH
6620 INIT_LOC is the location of the INIT.
6621
3e4093b6
RS
6622 REQUIRE_CONSTANT requests an error if non-constant initializers or
6623 elements are seen. */
7e842ef8 6624
3e4093b6 6625static tree
c2255bc4
AH
6626digest_init (location_t init_loc, tree type, tree init, tree origtype,
6627 bool null_pointer_constant, bool strict_string,
6628 int require_constant)
3e4093b6
RS
6629{
6630 enum tree_code code = TREE_CODE (type);
6631 tree inside_init = init;
8ce94e44 6632 tree semantic_type = NULL_TREE;
928c19bb 6633 bool maybe_const = true;
7e842ef8 6634
3e4093b6 6635 if (type == error_mark_node
f01da1a5 6636 || !init
7a0ca710 6637 || error_operand_p (init))
3e4093b6 6638 return error_mark_node;
7e842ef8 6639
ed248cf7 6640 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6641
8ce94e44
JM
6642 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6643 {
6644 semantic_type = TREE_TYPE (inside_init);
6645 inside_init = TREE_OPERAND (inside_init, 0);
6646 }
928c19bb
JM
6647 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6648 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6649
3e4093b6
RS
6650 /* Initialization of an array of chars from a string constant
6651 optionally enclosed in braces. */
7e842ef8 6652
197463ae
JM
6653 if (code == ARRAY_TYPE && inside_init
6654 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6655 {
267bac10
JM
6656 tree typ1
6657 = (TYPE_ATOMIC (TREE_TYPE (type))
6658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6659 TYPE_QUAL_ATOMIC)
6660 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6661 /* Note that an array could be both an array of character type
6662 and an array of wchar_t if wchar_t is signed char or unsigned
6663 char. */
6664 bool char_array = (typ1 == char_type_node
6665 || typ1 == signed_char_type_node
6666 || typ1 == unsigned_char_type_node);
6667 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6668 bool char16_array = !!comptypes (typ1, char16_type_node);
6669 bool char32_array = !!comptypes (typ1, char32_type_node);
6670
6671 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6672 {
916c5919 6673 struct c_expr expr;
c466b2cd 6674 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6675 expr.value = inside_init;
6676 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6677 expr.original_type = NULL;
d033409e 6678 maybe_warn_string_init (init_loc, type, expr);
916c5919 6679
a45e580b 6680 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6681 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6682 "initialization of a flexible array member");
6683
3e4093b6 6684 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6685 TYPE_MAIN_VARIANT (type)))
3e4093b6 6686 return inside_init;
7e842ef8 6687
c466b2cd 6688 if (char_array)
3e4093b6 6689 {
c466b2cd
KVH
6690 if (typ2 != char_type_node)
6691 {
ea58ef42
MP
6692 error_init (init_loc, "char-array initialized from wide "
6693 "string");
c466b2cd
KVH
6694 return error_mark_node;
6695 }
3e4093b6 6696 }
c466b2cd 6697 else
3e4093b6 6698 {
c466b2cd
KVH
6699 if (typ2 == char_type_node)
6700 {
ea58ef42
MP
6701 error_init (init_loc, "wide character array initialized "
6702 "from non-wide string");
c466b2cd
KVH
6703 return error_mark_node;
6704 }
6705 else if (!comptypes(typ1, typ2))
6706 {
ea58ef42
MP
6707 error_init (init_loc, "wide character array initialized "
6708 "from incompatible wide string");
c466b2cd
KVH
6709 return error_mark_node;
6710 }
7e842ef8 6711 }
2f6e4e97 6712
3e4093b6
RS
6713 TREE_TYPE (inside_init) = type;
6714 if (TYPE_DOMAIN (type) != 0
6715 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6716 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6717 {
6718 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6719
c466b2cd 6720 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6721 because it's ok to ignore the terminating null char
6722 that is counted in the length of the constant. */
5eb4df45
ILT
6723 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6724 (len
6725 - (TYPE_PRECISION (typ1)
6726 / BITS_PER_UNIT))))
6727 pedwarn_init (init_loc, 0,
6728 ("initializer-string for array of chars "
6729 "is too long"));
6730 else if (warn_cxx_compat
6731 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6732 warning_at (init_loc, OPT_Wc___compat,
6733 ("initializer-string for array chars "
6734 "is too long for C++"));
6735 }
7e842ef8 6736
3e4093b6 6737 return inside_init;
7e842ef8 6738 }
197463ae
JM
6739 else if (INTEGRAL_TYPE_P (typ1))
6740 {
ea58ef42 6741 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6742 "from string constant");
6743 return error_mark_node;
6744 }
7e842ef8
PE
6745 }
6746
3e4093b6
RS
6747 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6748 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6749 below and handle as a constructor. */
e89be13b
JJ
6750 if (code == VECTOR_TYPE
6751 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6752 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6753 && TREE_CONSTANT (inside_init))
6754 {
6755 if (TREE_CODE (inside_init) == VECTOR_CST
6756 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6757 TYPE_MAIN_VARIANT (type)))
6758 return inside_init;
6759
6760 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6761 {
4038c495
GB
6762 unsigned HOST_WIDE_INT ix;
6763 tree value;
6764 bool constant_p = true;
e89be13b
JJ
6765
6766 /* Iterate through elements and check if all constructor
6767 elements are *_CSTs. */
4038c495
GB
6768 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6769 if (!CONSTANT_CLASS_P (value))
6770 {
6771 constant_p = false;
6772 break;
6773 }
e89be13b 6774
4038c495
GB
6775 if (constant_p)
6776 return build_vector_from_ctor (type,
6777 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6778 }
6779 }
6035d635 6780
ca085fd7
MLI
6781 if (warn_sequence_point)
6782 verify_sequence_points (inside_init);
6783
3e4093b6
RS
6784 /* Any type can be initialized
6785 from an expression of the same type, optionally with braces. */
400fbf9f 6786
3e4093b6
RS
6787 if (inside_init && TREE_TYPE (inside_init) != 0
6788 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6789 TYPE_MAIN_VARIANT (type))
3e4093b6 6790 || (code == ARRAY_TYPE
132da1a5 6791 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6792 || (code == VECTOR_TYPE
132da1a5 6793 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6794 || (code == POINTER_TYPE
3897f229 6795 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6796 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6797 TREE_TYPE (type)))))
3e4093b6
RS
6798 {
6799 if (code == POINTER_TYPE)
b494fd98 6800 {
b494fd98
EB
6801 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6802 {
f2a71bbc
JM
6803 if (TREE_CODE (inside_init) == STRING_CST
6804 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6805 inside_init = array_to_pointer_conversion
6806 (init_loc, inside_init);
f2a71bbc
JM
6807 else
6808 {
ea58ef42 6809 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6810 return error_mark_node;
6811 }
b494fd98 6812 }
f2a71bbc 6813 }
b494fd98 6814
bae39a73
NS
6815 if (code == VECTOR_TYPE)
6816 /* Although the types are compatible, we may require a
6817 conversion. */
6818 inside_init = convert (type, inside_init);
3e4093b6 6819
ca58211b 6820 if (require_constant
3e4093b6 6821 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6822 {
3e4093b6
RS
6823 /* As an extension, allow initializing objects with static storage
6824 duration with compound literals (which are then treated just as
ca58211b
PB
6825 the brace enclosed list they contain). Also allow this for
6826 vectors, as we can only assign them with compound literals. */
7278465e
MP
6827 if (flag_isoc99 && code != VECTOR_TYPE)
6828 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6829 "is not constant");
3e4093b6
RS
6830 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6831 inside_init = DECL_INITIAL (decl);
400fbf9f 6832 }
3e4093b6
RS
6833
6834 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6835 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6836 {
ea58ef42
MP
6837 error_init (init_loc, "array initialized from non-constant array "
6838 "expression");
3e4093b6 6839 return error_mark_node;
400fbf9f 6840 }
400fbf9f 6841
c1771a20 6842 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6843 -pedantic-errors is specified. In the later case, we always want
6844 an error. In the former case, we simply want a warning. */
6845 if (require_constant && pedantic
6846 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6847 {
6848 inside_init
6849 = valid_compound_expr_initializer (inside_init,
6850 TREE_TYPE (inside_init));
6851 if (inside_init == error_mark_node)
ea58ef42 6852 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6853 else
c1771a20 6854 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6855 "initializer element is not constant");
3e4093b6
RS
6856 if (flag_pedantic_errors)
6857 inside_init = error_mark_node;
6858 }
6859 else if (require_constant
116df786
RH
6860 && !initializer_constant_valid_p (inside_init,
6861 TREE_TYPE (inside_init)))
3e4093b6 6862 {
ea58ef42 6863 error_init (init_loc, "initializer element is not constant");
3e4093b6 6864 inside_init = error_mark_node;
8b40563c 6865 }
928c19bb 6866 else if (require_constant && !maybe_const)
c2255bc4 6867 pedwarn_init (init_loc, 0,
928c19bb 6868 "initializer element is not a constant expression");
f735a153 6869
90137d8f 6870 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6871 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6872 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6873 type, inside_init, origtype,
bbbbb16a 6874 ic_init, null_pointer_constant,
928c19bb 6875 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6876 return inside_init;
6877 }
f735a153 6878
3e4093b6 6879 /* Handle scalar types, including conversions. */
400fbf9f 6880
ab22c1fa
CF
6881 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6882 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6883 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6884 {
f2a71bbc
JM
6885 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6886 && (TREE_CODE (init) == STRING_CST
6887 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6888 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6889 if (semantic_type)
6890 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6891 inside_init);
3e4093b6 6892 inside_init
68fca595
MP
6893 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6894 inside_init, origtype, ic_init,
6895 null_pointer_constant, NULL_TREE, NULL_TREE,
6896 0);
2f6e4e97 6897
3274deff
JW
6898 /* Check to see if we have already given an error message. */
6899 if (inside_init == error_mark_node)
6900 ;
3f75a254 6901 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6902 {
ea58ef42 6903 error_init (init_loc, "initializer element is not constant");
3e4093b6 6904 inside_init = error_mark_node;
400fbf9f 6905 }
3e4093b6 6906 else if (require_constant
116df786
RH
6907 && !initializer_constant_valid_p (inside_init,
6908 TREE_TYPE (inside_init)))
400fbf9f 6909 {
ea58ef42
MP
6910 error_init (init_loc, "initializer element is not computable at "
6911 "load time");
3e4093b6 6912 inside_init = error_mark_node;
400fbf9f 6913 }
928c19bb 6914 else if (require_constant && !maybe_const)
c2255bc4 6915 pedwarn_init (init_loc, 0,
928c19bb 6916 "initializer element is not a constant expression");
3e4093b6
RS
6917
6918 return inside_init;
400fbf9f 6919 }
d9fc6069 6920
3e4093b6 6921 /* Come here only for records and arrays. */
d9fc6069 6922
3e4093b6 6923 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6924 {
ea58ef42 6925 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6926 return error_mark_node;
d9fc6069 6927 }
3e4093b6 6928
ea58ef42 6929 error_init (init_loc, "invalid initializer");
3e4093b6 6930 return error_mark_node;
d9fc6069 6931}
400fbf9f 6932\f
3e4093b6 6933/* Handle initializers that use braces. */
400fbf9f 6934
3e4093b6
RS
6935/* Type of object we are accumulating a constructor for.
6936 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6937static tree constructor_type;
400fbf9f 6938
3e4093b6
RS
6939/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6940 left to fill. */
6941static tree constructor_fields;
400fbf9f 6942
3e4093b6
RS
6943/* For an ARRAY_TYPE, this is the specified index
6944 at which to store the next element we get. */
6945static tree constructor_index;
400fbf9f 6946
3e4093b6
RS
6947/* For an ARRAY_TYPE, this is the maximum index. */
6948static tree constructor_max_index;
400fbf9f 6949
3e4093b6
RS
6950/* For a RECORD_TYPE, this is the first field not yet written out. */
6951static tree constructor_unfilled_fields;
400fbf9f 6952
3e4093b6
RS
6953/* For an ARRAY_TYPE, this is the index of the first element
6954 not yet written out. */
6955static tree constructor_unfilled_index;
895ea614 6956
3e4093b6
RS
6957/* In a RECORD_TYPE, the byte index of the next consecutive field.
6958 This is so we can generate gaps between fields, when appropriate. */
6959static tree constructor_bit_index;
10d5caec 6960
3e4093b6
RS
6961/* If we are saving up the elements rather than allocating them,
6962 this is the list of elements so far (in reverse order,
6963 most recent first). */
9771b263 6964static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6965
3e4093b6
RS
6966/* 1 if constructor should be incrementally stored into a constructor chain,
6967 0 if all the elements should be kept in AVL tree. */
6968static int constructor_incremental;
ad47f1e5 6969
3e4093b6
RS
6970/* 1 if so far this constructor's elements are all compile-time constants. */
6971static int constructor_constant;
ad47f1e5 6972
3e4093b6
RS
6973/* 1 if so far this constructor's elements are all valid address constants. */
6974static int constructor_simple;
ad47f1e5 6975
928c19bb
JM
6976/* 1 if this constructor has an element that cannot be part of a
6977 constant expression. */
6978static int constructor_nonconst;
6979
3e4093b6
RS
6980/* 1 if this constructor is erroneous so far. */
6981static int constructor_erroneous;
d45cf215 6982
9bac5cbb
G
6983/* 1 if this constructor is the universal zero initializer { 0 }. */
6984static int constructor_zeroinit;
6985
3e4093b6
RS
6986/* Structure for managing pending initializer elements, organized as an
6987 AVL tree. */
d45cf215 6988
3e4093b6 6989struct init_node
d45cf215 6990{
3e4093b6
RS
6991 struct init_node *left, *right;
6992 struct init_node *parent;
6993 int balance;
6994 tree purpose;
6995 tree value;
bbbbb16a 6996 tree origtype;
d45cf215
RS
6997};
6998
3e4093b6
RS
6999/* Tree of pending elements at this constructor level.
7000 These are elements encountered out of order
7001 which belong at places we haven't reached yet in actually
7002 writing the output.
7003 Will never hold tree nodes across GC runs. */
7004static struct init_node *constructor_pending_elts;
d45cf215 7005
3e4093b6
RS
7006/* The SPELLING_DEPTH of this constructor. */
7007static int constructor_depth;
d45cf215 7008
3e4093b6
RS
7009/* DECL node for which an initializer is being read.
7010 0 means we are reading a constructor expression
7011 such as (struct foo) {...}. */
7012static tree constructor_decl;
d45cf215 7013
3e4093b6
RS
7014/* Nonzero if this is an initializer for a top-level decl. */
7015static int constructor_top_level;
d45cf215 7016
3e4093b6
RS
7017/* Nonzero if there were any member designators in this initializer. */
7018static int constructor_designated;
d45cf215 7019
3e4093b6
RS
7020/* Nesting depth of designator list. */
7021static int designator_depth;
d45cf215 7022
3e4093b6 7023/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7024static int designator_erroneous;
d45cf215 7025
3e4093b6
RS
7026\f
7027/* This stack has a level for each implicit or explicit level of
7028 structuring in the initializer, including the outermost one. It
7029 saves the values of most of the variables above. */
d45cf215 7030
3e4093b6
RS
7031struct constructor_range_stack;
7032
7033struct constructor_stack
d45cf215 7034{
3e4093b6
RS
7035 struct constructor_stack *next;
7036 tree type;
7037 tree fields;
7038 tree index;
7039 tree max_index;
7040 tree unfilled_index;
7041 tree unfilled_fields;
7042 tree bit_index;
9771b263 7043 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7044 struct init_node *pending_elts;
7045 int offset;
7046 int depth;
916c5919 7047 /* If value nonzero, this value should replace the entire
3e4093b6 7048 constructor at this level. */
916c5919 7049 struct c_expr replacement_value;
3e4093b6
RS
7050 struct constructor_range_stack *range_stack;
7051 char constant;
7052 char simple;
928c19bb 7053 char nonconst;
3e4093b6
RS
7054 char implicit;
7055 char erroneous;
7056 char outer;
7057 char incremental;
7058 char designated;
976d5a22 7059 int designator_depth;
3e4093b6 7060};
d45cf215 7061
802415d1 7062static struct constructor_stack *constructor_stack;
d45cf215 7063
3e4093b6
RS
7064/* This stack represents designators from some range designator up to
7065 the last designator in the list. */
d45cf215 7066
3e4093b6
RS
7067struct constructor_range_stack
7068{
7069 struct constructor_range_stack *next, *prev;
7070 struct constructor_stack *stack;
7071 tree range_start;
7072 tree index;
7073 tree range_end;
7074 tree fields;
7075};
d45cf215 7076
802415d1 7077static struct constructor_range_stack *constructor_range_stack;
d45cf215 7078
3e4093b6
RS
7079/* This stack records separate initializers that are nested.
7080 Nested initializers can't happen in ANSI C, but GNU C allows them
7081 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7082
3e4093b6 7083struct initializer_stack
d45cf215 7084{
3e4093b6
RS
7085 struct initializer_stack *next;
7086 tree decl;
3e4093b6
RS
7087 struct constructor_stack *constructor_stack;
7088 struct constructor_range_stack *constructor_range_stack;
9771b263 7089 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7090 struct spelling *spelling;
7091 struct spelling *spelling_base;
7092 int spelling_size;
7093 char top_level;
7094 char require_constant_value;
7095 char require_constant_elements;
7096};
d45cf215 7097
802415d1 7098static struct initializer_stack *initializer_stack;
3e4093b6
RS
7099\f
7100/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7101
7102void
a396f8ae 7103start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7104{
3e4093b6 7105 const char *locus;
cceb1885 7106 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7107
3e4093b6 7108 p->decl = constructor_decl;
3e4093b6
RS
7109 p->require_constant_value = require_constant_value;
7110 p->require_constant_elements = require_constant_elements;
7111 p->constructor_stack = constructor_stack;
7112 p->constructor_range_stack = constructor_range_stack;
7113 p->elements = constructor_elements;
7114 p->spelling = spelling;
7115 p->spelling_base = spelling_base;
7116 p->spelling_size = spelling_size;
7117 p->top_level = constructor_top_level;
7118 p->next = initializer_stack;
7119 initializer_stack = p;
400fbf9f 7120
3e4093b6 7121 constructor_decl = decl;
3e4093b6
RS
7122 constructor_designated = 0;
7123 constructor_top_level = top_level;
400fbf9f 7124
6f17bbcf 7125 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7126 {
7127 require_constant_value = TREE_STATIC (decl);
7128 require_constant_elements
7129 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7130 /* For a scalar, you can always use any value to initialize,
7131 even within braces. */
7132 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7133 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7134 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7135 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 7136 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7137 }
7138 else
7139 {
7140 require_constant_value = 0;
7141 require_constant_elements = 0;
88388a52 7142 locus = _("(anonymous)");
3e4093b6 7143 }
b71c7f8a 7144
3e4093b6
RS
7145 constructor_stack = 0;
7146 constructor_range_stack = 0;
b71c7f8a 7147
9bac5cbb 7148 found_missing_braces = 0;
3e4093b6
RS
7149
7150 spelling_base = 0;
7151 spelling_size = 0;
7152 RESTORE_SPELLING_DEPTH (0);
7153
7154 if (locus)
7155 push_string (locus);
7156}
7157
7158void
7159finish_init (void)
b71c7f8a 7160{
3e4093b6 7161 struct initializer_stack *p = initializer_stack;
b71c7f8a 7162
3e4093b6
RS
7163 /* Free the whole constructor stack of this initializer. */
7164 while (constructor_stack)
7165 {
7166 struct constructor_stack *q = constructor_stack;
7167 constructor_stack = q->next;
7168 free (q);
7169 }
7170
366de0ce 7171 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7172
7173 /* Pop back to the data of the outer initializer (if any). */
36579663 7174 free (spelling_base);
3aeb3655 7175
3e4093b6 7176 constructor_decl = p->decl;
3e4093b6
RS
7177 require_constant_value = p->require_constant_value;
7178 require_constant_elements = p->require_constant_elements;
7179 constructor_stack = p->constructor_stack;
7180 constructor_range_stack = p->constructor_range_stack;
7181 constructor_elements = p->elements;
7182 spelling = p->spelling;
7183 spelling_base = p->spelling_base;
7184 spelling_size = p->spelling_size;
7185 constructor_top_level = p->top_level;
7186 initializer_stack = p->next;
7187 free (p);
b71c7f8a 7188}
400fbf9f 7189\f
3e4093b6
RS
7190/* Call here when we see the initializer is surrounded by braces.
7191 This is instead of a call to push_init_level;
7192 it is matched by a call to pop_init_level.
400fbf9f 7193
3e4093b6
RS
7194 TYPE is the type to initialize, for a constructor expression.
7195 For an initializer for a decl, TYPE is zero. */
400fbf9f 7196
3e4093b6
RS
7197void
7198really_start_incremental_init (tree type)
400fbf9f 7199{
5d038c4c 7200 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7201
3e4093b6
RS
7202 if (type == 0)
7203 type = TREE_TYPE (constructor_decl);
400fbf9f 7204
b6fc2cdb
PB
7205 if (TREE_CODE (type) == VECTOR_TYPE
7206 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7207 error ("opaque vector types cannot be initialized");
400fbf9f 7208
3e4093b6
RS
7209 p->type = constructor_type;
7210 p->fields = constructor_fields;
7211 p->index = constructor_index;
7212 p->max_index = constructor_max_index;
7213 p->unfilled_index = constructor_unfilled_index;
7214 p->unfilled_fields = constructor_unfilled_fields;
7215 p->bit_index = constructor_bit_index;
7216 p->elements = constructor_elements;
7217 p->constant = constructor_constant;
7218 p->simple = constructor_simple;
928c19bb 7219 p->nonconst = constructor_nonconst;
3e4093b6
RS
7220 p->erroneous = constructor_erroneous;
7221 p->pending_elts = constructor_pending_elts;
7222 p->depth = constructor_depth;
916c5919
JM
7223 p->replacement_value.value = 0;
7224 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7225 p->replacement_value.original_type = NULL;
3e4093b6
RS
7226 p->implicit = 0;
7227 p->range_stack = 0;
7228 p->outer = 0;
7229 p->incremental = constructor_incremental;
7230 p->designated = constructor_designated;
976d5a22 7231 p->designator_depth = designator_depth;
3e4093b6
RS
7232 p->next = 0;
7233 constructor_stack = p;
b13aca19 7234
3e4093b6
RS
7235 constructor_constant = 1;
7236 constructor_simple = 1;
928c19bb 7237 constructor_nonconst = 0;
3e4093b6 7238 constructor_depth = SPELLING_DEPTH ();
9771b263 7239 constructor_elements = NULL;
3e4093b6
RS
7240 constructor_pending_elts = 0;
7241 constructor_type = type;
7242 constructor_incremental = 1;
7243 constructor_designated = 0;
9bac5cbb 7244 constructor_zeroinit = 1;
3e4093b6 7245 designator_depth = 0;
b06df647 7246 designator_erroneous = 0;
400fbf9f 7247
3e4093b6
RS
7248 if (TREE_CODE (constructor_type) == RECORD_TYPE
7249 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7250 {
3e4093b6
RS
7251 constructor_fields = TYPE_FIELDS (constructor_type);
7252 /* Skip any nameless bit fields at the beginning. */
7253 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7254 && DECL_NAME (constructor_fields) == 0)
910ad8de 7255 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7256
3e4093b6
RS
7257 constructor_unfilled_fields = constructor_fields;
7258 constructor_bit_index = bitsize_zero_node;
400fbf9f 7259 }
3e4093b6
RS
7260 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7261 {
7262 if (TYPE_DOMAIN (constructor_type))
7263 {
7264 constructor_max_index
7265 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7266
3e4093b6
RS
7267 /* Detect non-empty initializations of zero-length arrays. */
7268 if (constructor_max_index == NULL_TREE
7269 && TYPE_SIZE (constructor_type))
9a9d280e 7270 constructor_max_index = integer_minus_one_node;
400fbf9f 7271
3e4093b6
RS
7272 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7273 to initialize VLAs will cause a proper error; avoid tree
7274 checking errors as well by setting a safe value. */
7275 if (constructor_max_index
7276 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7277 constructor_max_index = integer_minus_one_node;
59c83dbf 7278
3e4093b6
RS
7279 constructor_index
7280 = convert (bitsizetype,
7281 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7282 }
3e4093b6 7283 else
493179da
JM
7284 {
7285 constructor_index = bitsize_zero_node;
7286 constructor_max_index = NULL_TREE;
7287 }
59c83dbf 7288
3e4093b6
RS
7289 constructor_unfilled_index = constructor_index;
7290 }
7291 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7292 {
7293 /* Vectors are like simple fixed-size arrays. */
7294 constructor_max_index =
c62c040f 7295 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7296 constructor_index = bitsize_zero_node;
3e4093b6
RS
7297 constructor_unfilled_index = constructor_index;
7298 }
7299 else
7300 {
7301 /* Handle the case of int x = {5}; */
7302 constructor_fields = constructor_type;
7303 constructor_unfilled_fields = constructor_type;
7304 }
7305}
7306\f
7307/* Push down into a subobject, for initialization.
7308 If this is for an explicit set of braces, IMPLICIT is 0.
7309 If it is because the next element belongs at a lower level,
7310 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7311
3e4093b6 7312void
ea58ef42
MP
7313push_init_level (location_t loc, int implicit,
7314 struct obstack *braced_init_obstack)
3e4093b6
RS
7315{
7316 struct constructor_stack *p;
7317 tree value = NULL_TREE;
400fbf9f 7318
3e4093b6 7319 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7320 pop them now. If implicit == 1, this will have been done in
7321 process_init_element; do not repeat it here because in the case
7322 of excess initializers for an empty aggregate this leads to an
7323 infinite cycle of popping a level and immediately recreating
7324 it. */
7325 if (implicit != 1)
3e4093b6 7326 {
472d98b4
JM
7327 while (constructor_stack->implicit)
7328 {
7329 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7330 || TREE_CODE (constructor_type) == UNION_TYPE)
7331 && constructor_fields == 0)
34cf811f 7332 process_init_element (input_location,
ea58ef42 7333 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7334 true, braced_init_obstack);
472d98b4
JM
7335 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7336 && constructor_max_index
7337 && tree_int_cst_lt (constructor_max_index,
7338 constructor_index))
34cf811f 7339 process_init_element (input_location,
ea58ef42 7340 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7341 true, braced_init_obstack);
472d98b4
JM
7342 else
7343 break;
7344 }
3e4093b6 7345 }
400fbf9f 7346
3e4093b6
RS
7347 /* Unless this is an explicit brace, we need to preserve previous
7348 content if any. */
7349 if (implicit)
7350 {
7351 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7352 || TREE_CODE (constructor_type) == UNION_TYPE)
7353 && constructor_fields)
a1e3b3d9 7354 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7355 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7356 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7357 }
7358
5d038c4c 7359 p = XNEW (struct constructor_stack);
3e4093b6
RS
7360 p->type = constructor_type;
7361 p->fields = constructor_fields;
7362 p->index = constructor_index;
7363 p->max_index = constructor_max_index;
7364 p->unfilled_index = constructor_unfilled_index;
7365 p->unfilled_fields = constructor_unfilled_fields;
7366 p->bit_index = constructor_bit_index;
7367 p->elements = constructor_elements;
7368 p->constant = constructor_constant;
7369 p->simple = constructor_simple;
928c19bb 7370 p->nonconst = constructor_nonconst;
3e4093b6
RS
7371 p->erroneous = constructor_erroneous;
7372 p->pending_elts = constructor_pending_elts;
7373 p->depth = constructor_depth;
916c5919
JM
7374 p->replacement_value.value = 0;
7375 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7376 p->replacement_value.original_type = NULL;
3e4093b6
RS
7377 p->implicit = implicit;
7378 p->outer = 0;
7379 p->incremental = constructor_incremental;
7380 p->designated = constructor_designated;
976d5a22 7381 p->designator_depth = designator_depth;
3e4093b6
RS
7382 p->next = constructor_stack;
7383 p->range_stack = 0;
7384 constructor_stack = p;
400fbf9f 7385
3e4093b6
RS
7386 constructor_constant = 1;
7387 constructor_simple = 1;
928c19bb 7388 constructor_nonconst = 0;
3e4093b6 7389 constructor_depth = SPELLING_DEPTH ();
9771b263 7390 constructor_elements = NULL;
3e4093b6
RS
7391 constructor_incremental = 1;
7392 constructor_designated = 0;
7393 constructor_pending_elts = 0;
7394 if (!implicit)
400fbf9f 7395 {
3e4093b6
RS
7396 p->range_stack = constructor_range_stack;
7397 constructor_range_stack = 0;
7398 designator_depth = 0;
b06df647 7399 designator_erroneous = 0;
3e4093b6 7400 }
400fbf9f 7401
3e4093b6
RS
7402 /* Don't die if an entire brace-pair level is superfluous
7403 in the containing level. */
7404 if (constructor_type == 0)
7405 ;
7406 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7407 || TREE_CODE (constructor_type) == UNION_TYPE)
7408 {
7409 /* Don't die if there are extra init elts at the end. */
7410 if (constructor_fields == 0)
7411 constructor_type = 0;
7412 else
400fbf9f 7413 {
3e4093b6
RS
7414 constructor_type = TREE_TYPE (constructor_fields);
7415 push_member_name (constructor_fields);
7416 constructor_depth++;
400fbf9f 7417 }
6a358dcb
MP
7418 /* If upper initializer is designated, then mark this as
7419 designated too to prevent bogus warnings. */
7420 constructor_designated = p->designated;
3e4093b6
RS
7421 }
7422 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7423 {
7424 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7425 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7426 constructor_depth++;
400fbf9f
JW
7427 }
7428
3e4093b6 7429 if (constructor_type == 0)
400fbf9f 7430 {
ea58ef42 7431 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7432 constructor_fields = 0;
7433 constructor_unfilled_fields = 0;
7434 return;
400fbf9f
JW
7435 }
7436
3e4093b6
RS
7437 if (value && TREE_CODE (value) == CONSTRUCTOR)
7438 {
7439 constructor_constant = TREE_CONSTANT (value);
7440 constructor_simple = TREE_STATIC (value);
928c19bb 7441 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7442 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7443 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7444 && (TREE_CODE (constructor_type) == RECORD_TYPE
7445 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7446 set_nonincremental_init (braced_init_obstack);
3e4093b6 7447 }
400fbf9f 7448
9bac5cbb
G
7449 if (implicit == 1)
7450 found_missing_braces = 1;
400fbf9f 7451
3e4093b6
RS
7452 if (TREE_CODE (constructor_type) == RECORD_TYPE
7453 || TREE_CODE (constructor_type) == UNION_TYPE)
7454 {
7455 constructor_fields = TYPE_FIELDS (constructor_type);
7456 /* Skip any nameless bit fields at the beginning. */
7457 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7458 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7459 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7460
3e4093b6
RS
7461 constructor_unfilled_fields = constructor_fields;
7462 constructor_bit_index = bitsize_zero_node;
7463 }
7464 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7465 {
7466 /* Vectors are like simple fixed-size arrays. */
7467 constructor_max_index =
c62c040f
RG
7468 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7469 constructor_index = bitsize_int (0);
3e4093b6
RS
7470 constructor_unfilled_index = constructor_index;
7471 }
7472 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7473 {
7474 if (TYPE_DOMAIN (constructor_type))
7475 {
7476 constructor_max_index
7477 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7478
3e4093b6
RS
7479 /* Detect non-empty initializations of zero-length arrays. */
7480 if (constructor_max_index == NULL_TREE
7481 && TYPE_SIZE (constructor_type))
9a9d280e 7482 constructor_max_index = integer_minus_one_node;
de520661 7483
3e4093b6
RS
7484 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7485 to initialize VLAs will cause a proper error; avoid tree
7486 checking errors as well by setting a safe value. */
7487 if (constructor_max_index
7488 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7489 constructor_max_index = integer_minus_one_node;
b62acd60 7490
3e4093b6
RS
7491 constructor_index
7492 = convert (bitsizetype,
7493 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7494 }
7495 else
7496 constructor_index = bitsize_zero_node;
de520661 7497
3e4093b6
RS
7498 constructor_unfilled_index = constructor_index;
7499 if (value && TREE_CODE (value) == STRING_CST)
7500 {
7501 /* We need to split the char/wchar array into individual
7502 characters, so that we don't have to special case it
7503 everywhere. */
a1e3b3d9 7504 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7505 }
7506 }
7507 else
7508 {
b4519d39 7509 if (constructor_type != error_mark_node)
96b40f8d 7510 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7511 constructor_fields = constructor_type;
7512 constructor_unfilled_fields = constructor_type;
7513 }
7514}
8b6a5902 7515
3e4093b6 7516/* At the end of an implicit or explicit brace level,
916c5919
JM
7517 finish up that level of constructor. If a single expression
7518 with redundant braces initialized that level, return the
7519 c_expr structure for that expression. Otherwise, the original_code
7520 element is set to ERROR_MARK.
7521 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7522 from inner levels (process_init_element ignores that),
916c5919 7523 but return error_mark_node as the value from the outermost level
3e4093b6 7524 (that's what we want to put in DECL_INITIAL).
916c5919 7525 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7526
916c5919 7527struct c_expr
ea58ef42
MP
7528pop_init_level (location_t loc, int implicit,
7529 struct obstack *braced_init_obstack)
3e4093b6
RS
7530{
7531 struct constructor_stack *p;
916c5919
JM
7532 struct c_expr ret;
7533 ret.value = 0;
7534 ret.original_code = ERROR_MARK;
6866c6e8 7535 ret.original_type = NULL;
de520661 7536
3e4093b6
RS
7537 if (implicit == 0)
7538 {
7539 /* When we come to an explicit close brace,
7540 pop any inner levels that didn't have explicit braces. */
7541 while (constructor_stack->implicit)
34cf811f 7542 process_init_element (input_location,
ea58ef42 7543 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7544 true, braced_init_obstack);
366de0ce 7545 gcc_assert (!constructor_range_stack);
3e4093b6 7546 }
e5e809f4 7547
0066ef9c
RH
7548 /* Now output all pending elements. */
7549 constructor_incremental = 1;
a1e3b3d9 7550 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7551
3e4093b6 7552 p = constructor_stack;
e5e809f4 7553
3e4093b6
RS
7554 /* Error for initializing a flexible array member, or a zero-length
7555 array member in an inappropriate context. */
7556 if (constructor_type && constructor_fields
7557 && TREE_CODE (constructor_type) == ARRAY_TYPE
7558 && TYPE_DOMAIN (constructor_type)
3f75a254 7559 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7560 {
7561 /* Silently discard empty initializations. The parser will
7562 already have pedwarned for empty brackets. */
7563 if (integer_zerop (constructor_unfilled_index))
7564 constructor_type = NULL_TREE;
366de0ce 7565 else
3e4093b6 7566 {
366de0ce 7567 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7568
3e4093b6 7569 if (constructor_depth > 2)
ea58ef42 7570 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7571 else
d033409e 7572 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7573 "initialization of a flexible array member");
de520661 7574
3e4093b6
RS
7575 /* We have already issued an error message for the existence
7576 of a flexible array member not at the end of the structure.
535a42b1 7577 Discard the initializer so that we do not die later. */
910ad8de 7578 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7579 constructor_type = NULL_TREE;
7580 }
3e4093b6 7581 }
de520661 7582
7b33f0c8
MP
7583 switch (vec_safe_length (constructor_elements))
7584 {
7585 case 0:
7586 /* Initialization with { } counts as zeroinit. */
7587 constructor_zeroinit = 1;
7588 break;
7589 case 1:
7590 /* This might be zeroinit as well. */
7591 if (integer_zerop ((*constructor_elements)[0].value))
7592 constructor_zeroinit = 1;
7593 break;
7594 default:
7595 /* If the constructor has more than one element, it can't be { 0 }. */
7596 constructor_zeroinit = 0;
7597 break;
7598 }
9bac5cbb
G
7599
7600 /* Warn when some structs are initialized with direct aggregation. */
7601 if (!implicit && found_missing_braces && warn_missing_braces
7602 && !constructor_zeroinit)
7b33f0c8
MP
7603 warning_init (loc, OPT_Wmissing_braces,
7604 "missing braces around initializer");
9bac5cbb 7605
3e4093b6 7606 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7607 if (warn_missing_field_initializers
3e4093b6
RS
7608 && constructor_type
7609 && TREE_CODE (constructor_type) == RECORD_TYPE
7610 && constructor_unfilled_fields)
7611 {
7612 /* Do not warn for flexible array members or zero-length arrays. */
7613 while (constructor_unfilled_fields
3f75a254 7614 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7615 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7616 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7617
49819fef
AM
7618 if (constructor_unfilled_fields
7619 /* Do not warn if this level of the initializer uses member
7620 designators; it is likely to be deliberate. */
7621 && !constructor_designated
84937de2 7622 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7623 && !constructor_zeroinit)
3e4093b6 7624 {
32397f22
MLI
7625 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7626 "missing initializer for field %qD of %qT",
7627 constructor_unfilled_fields,
7628 constructor_type))
7629 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7630 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7631 }
7632 }
de520661 7633
3e4093b6 7634 /* Pad out the end of the structure. */
916c5919 7635 if (p->replacement_value.value)
3e4093b6
RS
7636 /* If this closes a superfluous brace pair,
7637 just pass out the element between them. */
916c5919 7638 ret = p->replacement_value;
3e4093b6
RS
7639 else if (constructor_type == 0)
7640 ;
7641 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7642 && TREE_CODE (constructor_type) != UNION_TYPE
7643 && TREE_CODE (constructor_type) != ARRAY_TYPE
7644 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7645 {
7646 /* A nonincremental scalar initializer--just return
7647 the element, after verifying there is just one. */
9771b263 7648 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7649 {
7650 if (!constructor_erroneous)
ea58ef42 7651 error_init (loc, "empty scalar initializer");
916c5919 7652 ret.value = error_mark_node;
3e4093b6 7653 }
9771b263 7654 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7655 {
ea58ef42 7656 error_init (loc, "extra elements in scalar initializer");
9771b263 7657 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7658 }
7659 else
9771b263 7660 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7661 }
7662 else
7663 {
7664 if (constructor_erroneous)
916c5919 7665 ret.value = error_mark_node;
3e4093b6
RS
7666 else
7667 {
916c5919 7668 ret.value = build_constructor (constructor_type,
4038c495 7669 constructor_elements);
3e4093b6 7670 if (constructor_constant)
51eed280 7671 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7672 if (constructor_constant && constructor_simple)
916c5919 7673 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7674 if (constructor_nonconst)
7675 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7676 }
7677 }
de520661 7678
928c19bb
JM
7679 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7680 {
7681 if (constructor_nonconst)
7682 ret.original_code = C_MAYBE_CONST_EXPR;
7683 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7684 ret.original_code = ERROR_MARK;
7685 }
7686
3e4093b6
RS
7687 constructor_type = p->type;
7688 constructor_fields = p->fields;
7689 constructor_index = p->index;
7690 constructor_max_index = p->max_index;
7691 constructor_unfilled_index = p->unfilled_index;
7692 constructor_unfilled_fields = p->unfilled_fields;
7693 constructor_bit_index = p->bit_index;
7694 constructor_elements = p->elements;
7695 constructor_constant = p->constant;
7696 constructor_simple = p->simple;
928c19bb 7697 constructor_nonconst = p->nonconst;
3e4093b6
RS
7698 constructor_erroneous = p->erroneous;
7699 constructor_incremental = p->incremental;
7700 constructor_designated = p->designated;
976d5a22 7701 designator_depth = p->designator_depth;
3e4093b6
RS
7702 constructor_pending_elts = p->pending_elts;
7703 constructor_depth = p->depth;
7704 if (!p->implicit)
7705 constructor_range_stack = p->range_stack;
7706 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7707
3e4093b6
RS
7708 constructor_stack = p->next;
7709 free (p);
b621a4dd 7710
5d5e98dc
VR
7711 if (ret.value == 0 && constructor_stack == 0)
7712 ret.value = error_mark_node;
916c5919 7713 return ret;
3e4093b6 7714}
8b6a5902 7715
3e4093b6
RS
7716/* Common handling for both array range and field name designators.
7717 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7718
3e4093b6 7719static int
ea58ef42
MP
7720set_designator (location_t loc, int array,
7721 struct obstack *braced_init_obstack)
de520661 7722{
3e4093b6
RS
7723 tree subtype;
7724 enum tree_code subcode;
de520661 7725
3e4093b6
RS
7726 /* Don't die if an entire brace-pair level is superfluous
7727 in the containing level. */
7728 if (constructor_type == 0)
7729 return 1;
de520661 7730
366de0ce
NS
7731 /* If there were errors in this designator list already, bail out
7732 silently. */
b06df647 7733 if (designator_erroneous)
3e4093b6 7734 return 1;
e28cae4f 7735
3e4093b6
RS
7736 if (!designator_depth)
7737 {
366de0ce 7738 gcc_assert (!constructor_range_stack);
de520661 7739
3e4093b6
RS
7740 /* Designator list starts at the level of closest explicit
7741 braces. */
7742 while (constructor_stack->implicit)
34cf811f 7743 process_init_element (input_location,
ea58ef42 7744 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7745 true, braced_init_obstack);
3e4093b6
RS
7746 constructor_designated = 1;
7747 return 0;
7748 }
de520661 7749
366de0ce 7750 switch (TREE_CODE (constructor_type))
3c3fa147 7751 {
366de0ce
NS
7752 case RECORD_TYPE:
7753 case UNION_TYPE:
3e4093b6
RS
7754 subtype = TREE_TYPE (constructor_fields);
7755 if (subtype != error_mark_node)
7756 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7757 break;
7758 case ARRAY_TYPE:
3e4093b6 7759 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7760 break;
7761 default:
7762 gcc_unreachable ();
de520661 7763 }
400fbf9f 7764
3e4093b6
RS
7765 subcode = TREE_CODE (subtype);
7766 if (array && subcode != ARRAY_TYPE)
7767 {
ea58ef42 7768 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7769 return 1;
7770 }
7771 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7772 {
ea58ef42 7773 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7774 return 1;
7775 }
d45cf215 7776
3e4093b6 7777 constructor_designated = 1;
ea58ef42 7778 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7779 return 0;
de520661 7780}
400fbf9f 7781
3e4093b6
RS
7782/* If there are range designators in designator list, push a new designator
7783 to constructor_range_stack. RANGE_END is end of such stack range or
7784 NULL_TREE if there is no range designator at this level. */
400fbf9f 7785
3e4093b6 7786static void
a1e3b3d9 7787push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7788{
7789 struct constructor_range_stack *p;
400fbf9f 7790
a1e3b3d9
LB
7791 p = (struct constructor_range_stack *)
7792 obstack_alloc (braced_init_obstack,
7793 sizeof (struct constructor_range_stack));
3e4093b6
RS
7794 p->prev = constructor_range_stack;
7795 p->next = 0;
7796 p->fields = constructor_fields;
7797 p->range_start = constructor_index;
7798 p->index = constructor_index;
7799 p->stack = constructor_stack;
7800 p->range_end = range_end;
8b6a5902 7801 if (constructor_range_stack)
3e4093b6
RS
7802 constructor_range_stack->next = p;
7803 constructor_range_stack = p;
de520661 7804}
400fbf9f 7805
3e4093b6
RS
7806/* Within an array initializer, specify the next index to be initialized.
7807 FIRST is that index. If LAST is nonzero, then initialize a range
7808 of indices, running from FIRST through LAST. */
5a7ec9d9 7809
de520661 7810void
ea58ef42 7811set_init_index (location_t loc, tree first, tree last,
d033409e 7812 struct obstack *braced_init_obstack)
de520661 7813{
ea58ef42 7814 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7815 return;
de520661 7816
b06df647 7817 designator_erroneous = 1;
de520661 7818
3ea8cd06
JM
7819 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7820 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7821 {
ea58ef42 7822 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7823 return;
7824 }
7825
2b6da65c
JM
7826 if (TREE_CODE (first) != INTEGER_CST)
7827 {
7828 first = c_fully_fold (first, false, NULL);
7829 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7830 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7831 "array index in initializer is not "
7832 "an integer constant expression");
7833 }
7834
7835 if (last && TREE_CODE (last) != INTEGER_CST)
7836 {
7837 last = c_fully_fold (last, false, NULL);
7838 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7839 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7840 "array index in initializer is not "
7841 "an integer constant expression");
7842 }
7843
3e4093b6 7844 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7845 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7846 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7847 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7848 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7849 error_init (loc, "array index in non-array initializer");
622adc7e 7850 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7851 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7852 else if (constructor_max_index
7853 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7854 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7855 else
de520661 7856 {
928c19bb
JM
7857 constant_expression_warning (first);
7858 if (last)
7859 constant_expression_warning (last);
3e4093b6 7860 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7861 if (tree_int_cst_lt (constructor_index, first))
7862 {
7863 constructor_index = copy_node (constructor_index);
7864 TREE_OVERFLOW (constructor_index) = 1;
7865 }
665f2503 7866
3e4093b6 7867 if (last)
2bede729 7868 {
3e4093b6
RS
7869 if (tree_int_cst_equal (first, last))
7870 last = 0;
7871 else if (tree_int_cst_lt (last, first))
7872 {
ea58ef42 7873 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7874 last = 0;
7875 }
7876 else
7877 {
7878 last = convert (bitsizetype, last);
7879 if (constructor_max_index != 0
7880 && tree_int_cst_lt (constructor_max_index, last))
7881 {
ea58ef42
MP
7882 error_init (loc, "array index range in initializer exceeds "
7883 "array bounds");
3e4093b6
RS
7884 last = 0;
7885 }
7886 }
2bede729 7887 }
fed3cef0 7888
3e4093b6 7889 designator_depth++;
b06df647 7890 designator_erroneous = 0;
3e4093b6 7891 if (constructor_range_stack || last)
a1e3b3d9 7892 push_range_stack (last, braced_init_obstack);
de520661 7893 }
de520661 7894}
3e4093b6
RS
7895
7896/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7897
de520661 7898void
ea58ef42
MP
7899set_init_label (location_t loc, tree fieldname,
7900 struct obstack *braced_init_obstack)
de520661 7901{
0fb96aa4 7902 tree field;
94ba5069 7903
ea58ef42 7904 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7905 return;
7906
b06df647 7907 designator_erroneous = 1;
3e4093b6
RS
7908
7909 if (TREE_CODE (constructor_type) != RECORD_TYPE
7910 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7911 {
ea58ef42 7912 error_init (loc, "field name not in record or union initializer");
3e4093b6 7913 return;
94ba5069
RS
7914 }
7915
0fb96aa4 7916 field = lookup_field (constructor_type, fieldname);
8b6a5902 7917
0fb96aa4 7918 if (field == 0)
9babc352 7919 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 7920 else
0fb96aa4
JM
7921 do
7922 {
7923 constructor_fields = TREE_VALUE (field);
7924 designator_depth++;
7925 designator_erroneous = 0;
7926 if (constructor_range_stack)
7927 push_range_stack (NULL_TREE, braced_init_obstack);
7928 field = TREE_CHAIN (field);
7929 if (field)
7930 {
ea58ef42 7931 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7932 return;
7933 }
7934 }
7935 while (field != NULL_TREE);
3e4093b6
RS
7936}
7937\f
7938/* Add a new initializer to the tree of pending initializers. PURPOSE
7939 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7940 VALUE is the value of that index or field. If ORIGTYPE is not
7941 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7942
7943 IMPLICIT is true if value comes from pop_init_level (1),
7944 the new initializer has been merged with the existing one
7945 and thus no warnings should be emitted about overriding an
7946 existing initializer. */
de520661 7947
3e4093b6 7948static void
96b40f8d
MP
7949add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7950 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7951{
7952 struct init_node *p, **q, *r;
7953
7954 q = &constructor_pending_elts;
7955 p = 0;
7956
7957 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7958 {
3e4093b6 7959 while (*q != 0)
91fa3c30 7960 {
3e4093b6
RS
7961 p = *q;
7962 if (tree_int_cst_lt (purpose, p->purpose))
7963 q = &p->left;
7964 else if (tree_int_cst_lt (p->purpose, purpose))
7965 q = &p->right;
7966 else
7967 {
b295aee2
JJ
7968 if (!implicit)
7969 {
7970 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7971 warning_init (loc, 0,
7972 "initialized field with side-effects "
7973 "overwritten");
b295aee2 7974 else if (warn_override_init)
96b40f8d
MP
7975 warning_init (loc, OPT_Woverride_init,
7976 "initialized field overwritten");
b295aee2 7977 }
3e4093b6 7978 p->value = value;
bbbbb16a 7979 p->origtype = origtype;
3e4093b6
RS
7980 return;
7981 }
91fa3c30 7982 }
de520661 7983 }
3e4093b6 7984 else
de520661 7985 {
3e4093b6 7986 tree bitpos;
400fbf9f 7987
3e4093b6
RS
7988 bitpos = bit_position (purpose);
7989 while (*q != NULL)
7990 {
7991 p = *q;
7992 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7993 q = &p->left;
7994 else if (p->purpose != purpose)
7995 q = &p->right;
7996 else
7997 {
b295aee2
JJ
7998 if (!implicit)
7999 {
8000 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
8001 warning_init (loc, 0,
8002 "initialized field with side-effects "
8003 "overwritten");
b295aee2 8004 else if (warn_override_init)
96b40f8d
MP
8005 warning_init (loc, OPT_Woverride_init,
8006 "initialized field overwritten");
b295aee2 8007 }
3e4093b6 8008 p->value = value;
bbbbb16a 8009 p->origtype = origtype;
3e4093b6
RS
8010 return;
8011 }
8012 }
91fa3c30 8013 }
b71c7f8a 8014
a1e3b3d9
LB
8015 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8016 sizeof (struct init_node));
3e4093b6
RS
8017 r->purpose = purpose;
8018 r->value = value;
bbbbb16a 8019 r->origtype = origtype;
8b6a5902 8020
3e4093b6
RS
8021 *q = r;
8022 r->parent = p;
8023 r->left = 0;
8024 r->right = 0;
8025 r->balance = 0;
b71c7f8a 8026
3e4093b6 8027 while (p)
de520661 8028 {
3e4093b6 8029 struct init_node *s;
665f2503 8030
3e4093b6 8031 if (r == p->left)
2bede729 8032 {
3e4093b6
RS
8033 if (p->balance == 0)
8034 p->balance = -1;
8035 else if (p->balance < 0)
8036 {
8037 if (r->balance < 0)
8038 {
8039 /* L rotation. */
8040 p->left = r->right;
8041 if (p->left)
8042 p->left->parent = p;
8043 r->right = p;
e7b6a0ee 8044
3e4093b6
RS
8045 p->balance = 0;
8046 r->balance = 0;
39bc99c2 8047
3e4093b6
RS
8048 s = p->parent;
8049 p->parent = r;
8050 r->parent = s;
8051 if (s)
8052 {
8053 if (s->left == p)
8054 s->left = r;
8055 else
8056 s->right = r;
8057 }
8058 else
8059 constructor_pending_elts = r;
8060 }
8061 else
8062 {
8063 /* LR rotation. */
8064 struct init_node *t = r->right;
e7b6a0ee 8065
3e4093b6
RS
8066 r->right = t->left;
8067 if (r->right)
8068 r->right->parent = r;
8069 t->left = r;
8070
8071 p->left = t->right;
8072 if (p->left)
8073 p->left->parent = p;
8074 t->right = p;
8075
8076 p->balance = t->balance < 0;
8077 r->balance = -(t->balance > 0);
8078 t->balance = 0;
8079
8080 s = p->parent;
8081 p->parent = t;
8082 r->parent = t;
8083 t->parent = s;
8084 if (s)
8085 {
8086 if (s->left == p)
8087 s->left = t;
8088 else
8089 s->right = t;
8090 }
8091 else
8092 constructor_pending_elts = t;
8093 }
8094 break;
8095 }
8096 else
8097 {
8098 /* p->balance == +1; growth of left side balances the node. */
8099 p->balance = 0;
8100 break;
8101 }
2bede729 8102 }
3e4093b6
RS
8103 else /* r == p->right */
8104 {
8105 if (p->balance == 0)
8106 /* Growth propagation from right side. */
8107 p->balance++;
8108 else if (p->balance > 0)
8109 {
8110 if (r->balance > 0)
8111 {
8112 /* R rotation. */
8113 p->right = r->left;
8114 if (p->right)
8115 p->right->parent = p;
8116 r->left = p;
8117
8118 p->balance = 0;
8119 r->balance = 0;
8120
8121 s = p->parent;
8122 p->parent = r;
8123 r->parent = s;
8124 if (s)
8125 {
8126 if (s->left == p)
8127 s->left = r;
8128 else
8129 s->right = r;
8130 }
8131 else
8132 constructor_pending_elts = r;
8133 }
8134 else /* r->balance == -1 */
8135 {
8136 /* RL rotation */
8137 struct init_node *t = r->left;
8138
8139 r->left = t->right;
8140 if (r->left)
8141 r->left->parent = r;
8142 t->right = r;
8143
8144 p->right = t->left;
8145 if (p->right)
8146 p->right->parent = p;
8147 t->left = p;
8148
8149 r->balance = (t->balance < 0);
8150 p->balance = -(t->balance > 0);
8151 t->balance = 0;
8152
8153 s = p->parent;
8154 p->parent = t;
8155 r->parent = t;
8156 t->parent = s;
8157 if (s)
8158 {
8159 if (s->left == p)
8160 s->left = t;
8161 else
8162 s->right = t;
8163 }
8164 else
8165 constructor_pending_elts = t;
8166 }
8167 break;
8168 }
8169 else
8170 {
8171 /* p->balance == -1; growth of right side balances the node. */
8172 p->balance = 0;
8173 break;
8174 }
8175 }
8176
8177 r = p;
8178 p = p->parent;
8179 }
8180}
8181
8182/* Build AVL tree from a sorted chain. */
8183
8184static void
a1e3b3d9 8185set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8186{
4038c495
GB
8187 unsigned HOST_WIDE_INT ix;
8188 tree index, value;
3e4093b6
RS
8189
8190 if (TREE_CODE (constructor_type) != RECORD_TYPE
8191 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8192 return;
8193
4038c495 8194 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8195 add_pending_init (input_location, index, value, NULL_TREE, true,
8196 braced_init_obstack);
9771b263 8197 constructor_elements = NULL;
3e4093b6
RS
8198 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8199 {
8200 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8201 /* Skip any nameless bit fields at the beginning. */
8202 while (constructor_unfilled_fields != 0
8203 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8204 && DECL_NAME (constructor_unfilled_fields) == 0)
8205 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8206
de520661 8207 }
3e4093b6 8208 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8209 {
3e4093b6
RS
8210 if (TYPE_DOMAIN (constructor_type))
8211 constructor_unfilled_index
8212 = convert (bitsizetype,
8213 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8214 else
8215 constructor_unfilled_index = bitsize_zero_node;
de520661 8216 }
3e4093b6 8217 constructor_incremental = 0;
de520661 8218}
400fbf9f 8219
3e4093b6 8220/* Build AVL tree from a string constant. */
de520661 8221
3e4093b6 8222static void
a1e3b3d9
LB
8223set_nonincremental_init_from_string (tree str,
8224 struct obstack * braced_init_obstack)
de520661 8225{
3e4093b6
RS
8226 tree value, purpose, type;
8227 HOST_WIDE_INT val[2];
8228 const char *p, *end;
8229 int byte, wchar_bytes, charwidth, bitpos;
de520661 8230
366de0ce 8231 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8232
c466b2cd 8233 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8234 charwidth = TYPE_PRECISION (char_type_node);
8235 type = TREE_TYPE (constructor_type);
8236 p = TREE_STRING_POINTER (str);
8237 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8238
3e4093b6 8239 for (purpose = bitsize_zero_node;
8824edff
JJ
8240 p < end
8241 && !(constructor_max_index
8242 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8243 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8244 {
3e4093b6 8245 if (wchar_bytes == 1)
ffc5c6a9 8246 {
807e902e
KZ
8247 val[0] = (unsigned char) *p++;
8248 val[1] = 0;
ffc5c6a9
RH
8249 }
8250 else
3e4093b6 8251 {
3e4093b6 8252 val[1] = 0;
807e902e 8253 val[0] = 0;
3e4093b6
RS
8254 for (byte = 0; byte < wchar_bytes; byte++)
8255 {
8256 if (BYTES_BIG_ENDIAN)
8257 bitpos = (wchar_bytes - byte - 1) * charwidth;
8258 else
8259 bitpos = byte * charwidth;
807e902e 8260 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8261 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8262 << (bitpos % HOST_BITS_PER_WIDE_INT);
8263 }
8264 }
584ef5fe 8265
8df83eae 8266 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8267 {
8268 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8269 if (bitpos < HOST_BITS_PER_WIDE_INT)
8270 {
807e902e 8271 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8272 {
807e902e
KZ
8273 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8274 val[1] = -1;
3e4093b6
RS
8275 }
8276 }
8277 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8278 {
807e902e
KZ
8279 if (val[0] < 0)
8280 val[1] = -1;
3e4093b6 8281 }
807e902e 8282 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8283 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8284 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8285 << (bitpos - HOST_BITS_PER_WIDE_INT);
8286 }
ffc5c6a9 8287
807e902e
KZ
8288 value = wide_int_to_tree (type,
8289 wide_int::from_array (val, 2,
8290 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8291 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8292 braced_init_obstack);
9dfcc8db
BH
8293 }
8294
3e4093b6
RS
8295 constructor_incremental = 0;
8296}
de520661 8297
3e4093b6
RS
8298/* Return value of FIELD in pending initializer or zero if the field was
8299 not initialized yet. */
8300
8301static tree
a1e3b3d9 8302find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8303{
8304 struct init_node *p;
8305
8306 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8307 {
3e4093b6
RS
8308 if (constructor_incremental
8309 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8310 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8311
8312 p = constructor_pending_elts;
8313 while (p)
19d76e60 8314 {
3e4093b6
RS
8315 if (tree_int_cst_lt (field, p->purpose))
8316 p = p->left;
8317 else if (tree_int_cst_lt (p->purpose, field))
8318 p = p->right;
8319 else
8320 return p->value;
19d76e60 8321 }
19d76e60 8322 }
3e4093b6 8323 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8324 {
3e4093b6 8325 tree bitpos = bit_position (field);
de520661 8326
3e4093b6
RS
8327 if (constructor_incremental
8328 && (!constructor_unfilled_fields
8329 || tree_int_cst_lt (bitpos,
8330 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8331 set_nonincremental_init (braced_init_obstack);
de520661 8332
3e4093b6
RS
8333 p = constructor_pending_elts;
8334 while (p)
8335 {
8336 if (field == p->purpose)
8337 return p->value;
8338 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8339 p = p->left;
8340 else
8341 p = p->right;
8342 }
8343 }
8344 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8345 {
9771b263
DN
8346 if (!vec_safe_is_empty (constructor_elements)
8347 && (constructor_elements->last ().index == field))
8348 return constructor_elements->last ().value;
de520661 8349 }
3e4093b6 8350 return 0;
de520661
RS
8351}
8352
3e4093b6
RS
8353/* "Output" the next constructor element.
8354 At top level, really output it to assembler code now.
8355 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8356 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8357 TYPE is the data type that the containing data type wants here.
8358 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8359 If VALUE is a string constant, STRICT_STRING is true if it is
8360 unparenthesized or we should not warn here for it being parenthesized.
8361 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8362
3e4093b6
RS
8363 PENDING if non-nil means output pending elements that belong
8364 right after this element. (PENDING is normally 1;
b295aee2
JJ
8365 it is 0 while outputting pending elements, to avoid recursion.)
8366
8367 IMPLICIT is true if value comes from pop_init_level (1),
8368 the new initializer has been merged with the existing one
8369 and thus no warnings should be emitted about overriding an
8370 existing initializer. */
8b6a5902 8371
3e4093b6 8372static void
34cf811f
MP
8373output_init_element (location_t loc, tree value, tree origtype,
8374 bool strict_string, tree type, tree field, int pending,
8375 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8376{
8ce94e44 8377 tree semantic_type = NULL_TREE;
928c19bb
JM
8378 bool maybe_const = true;
8379 bool npc;
4038c495 8380
0a880880 8381 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8382 {
3e4093b6
RS
8383 constructor_erroneous = 1;
8384 return;
8b6a5902 8385 }
46bdb9cf
JM
8386 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8387 && (TREE_CODE (value) == STRING_CST
8388 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8389 && !(TREE_CODE (value) == STRING_CST
8390 && TREE_CODE (type) == ARRAY_TYPE
8391 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8392 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8393 TYPE_MAIN_VARIANT (type)))
c2255bc4 8394 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8395
3e4093b6 8396 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8397 && require_constant_value && pending)
8b6a5902 8398 {
3e4093b6
RS
8399 /* As an extension, allow initializing objects with static storage
8400 duration with compound literals (which are then treated just as
8401 the brace enclosed list they contain). */
4435bb92
MP
8402 if (flag_isoc99)
8403 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8404 "constant");
3e4093b6
RS
8405 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8406 value = DECL_INITIAL (decl);
8b6a5902
JJ
8407 }
8408
928c19bb 8409 npc = null_pointer_constant_p (value);
8ce94e44
JM
8410 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8411 {
8412 semantic_type = TREE_TYPE (value);
8413 value = TREE_OPERAND (value, 0);
8414 }
928c19bb
JM
8415 value = c_fully_fold (value, require_constant_value, &maybe_const);
8416
3e4093b6
RS
8417 if (value == error_mark_node)
8418 constructor_erroneous = 1;
8419 else if (!TREE_CONSTANT (value))
8420 constructor_constant = 0;
116df786 8421 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8422 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8423 || TREE_CODE (constructor_type) == UNION_TYPE)
8424 && DECL_C_BIT_FIELD (field)
8425 && TREE_CODE (value) != INTEGER_CST))
8426 constructor_simple = 0;
928c19bb
JM
8427 if (!maybe_const)
8428 constructor_nonconst = 1;
3e4093b6 8429
116df786 8430 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8431 {
116df786
RH
8432 if (require_constant_value)
8433 {
ea58ef42 8434 error_init (loc, "initializer element is not constant");
116df786
RH
8435 value = error_mark_node;
8436 }
8437 else if (require_constant_elements)
8337d1db 8438 pedwarn (loc, OPT_Wpedantic,
509c9d60 8439 "initializer element is not computable at load time");
8b6a5902 8440 }
928c19bb
JM
8441 else if (!maybe_const
8442 && (require_constant_value || require_constant_elements))
8337d1db 8443 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8444 "initializer element is not a constant expression");
3e4093b6 8445
81f40b79
ILT
8446 /* Issue -Wc++-compat warnings about initializing a bitfield with
8447 enum type. */
8448 if (warn_cxx_compat
8449 && field != NULL_TREE
8450 && TREE_CODE (field) == FIELD_DECL
8451 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8452 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8453 != TYPE_MAIN_VARIANT (type))
8454 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8455 {
8456 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8457 if (checktype != error_mark_node
8458 && (TYPE_MAIN_VARIANT (checktype)
8459 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8460 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8461 "enum conversion in initialization is invalid in C++");
8462 }
8463
3e4093b6
RS
8464 /* If this field is empty (and not at the end of structure),
8465 don't do anything other than checking the initializer. */
8466 if (field
8467 && (TREE_TYPE (field) == error_mark_node
8468 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8469 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8470 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8471 || DECL_CHAIN (field)))))
3e4093b6
RS
8472 return;
8473
8ce94e44
JM
8474 if (semantic_type)
8475 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8476 value = digest_init (loc, type, value, origtype, npc, strict_string,
8477 require_constant_value);
3e4093b6 8478 if (value == error_mark_node)
8b6a5902 8479 {
3e4093b6
RS
8480 constructor_erroneous = 1;
8481 return;
8b6a5902 8482 }
928c19bb
JM
8483 if (require_constant_value || require_constant_elements)
8484 constant_expression_warning (value);
8b6a5902 8485
3e4093b6
RS
8486 /* If this element doesn't come next in sequence,
8487 put it on constructor_pending_elts. */
8488 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8489 && (!constructor_incremental
8490 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8491 {
3e4093b6
RS
8492 if (constructor_incremental
8493 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8494 set_nonincremental_init (braced_init_obstack);
3e4093b6 8495
96b40f8d 8496 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8497 braced_init_obstack);
3e4093b6 8498 return;
8b6a5902 8499 }
3e4093b6
RS
8500 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8501 && (!constructor_incremental
8502 || field != constructor_unfilled_fields))
8b6a5902 8503 {
3e4093b6
RS
8504 /* We do this for records but not for unions. In a union,
8505 no matter which field is specified, it can be initialized
8506 right away since it starts at the beginning of the union. */
8507 if (constructor_incremental)
8508 {
8509 if (!constructor_unfilled_fields)
a1e3b3d9 8510 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8511 else
8512 {
8513 tree bitpos, unfillpos;
8514
8515 bitpos = bit_position (field);
8516 unfillpos = bit_position (constructor_unfilled_fields);
8517
8518 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8519 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8520 }
8521 }
8522
96b40f8d 8523 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8524 braced_init_obstack);
3e4093b6 8525 return;
8b6a5902 8526 }
3e4093b6 8527 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8528 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8529 {
b295aee2
JJ
8530 if (!implicit)
8531 {
9771b263 8532 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
96b40f8d 8533 warning_init (loc, 0,
b295aee2
JJ
8534 "initialized field with side-effects overwritten");
8535 else if (warn_override_init)
96b40f8d
MP
8536 warning_init (loc, OPT_Woverride_init,
8537 "initialized field overwritten");
b295aee2 8538 }
8b6a5902 8539
3e4093b6 8540 /* We can have just one union field set. */
9771b263 8541 constructor_elements = NULL;
3e4093b6 8542 }
8b6a5902 8543
3e4093b6
RS
8544 /* Otherwise, output this element either to
8545 constructor_elements or to the assembler file. */
8b6a5902 8546
f32682ca 8547 constructor_elt celt = {field, value};
9771b263 8548 vec_safe_push (constructor_elements, celt);
8b6a5902 8549
3e4093b6
RS
8550 /* Advance the variable that indicates sequential elements output. */
8551 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8552 constructor_unfilled_index
db3927fb
AH
8553 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8554 bitsize_one_node);
3e4093b6
RS
8555 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8556 {
8557 constructor_unfilled_fields
910ad8de 8558 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8559
3e4093b6
RS
8560 /* Skip any nameless bit fields. */
8561 while (constructor_unfilled_fields != 0
8562 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8563 && DECL_NAME (constructor_unfilled_fields) == 0)
8564 constructor_unfilled_fields =
910ad8de 8565 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8566 }
8567 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8568 constructor_unfilled_fields = 0;
de520661 8569
3e4093b6
RS
8570 /* Now output any pending elements which have become next. */
8571 if (pending)
a1e3b3d9 8572 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8573}
8b6a5902 8574
3e4093b6
RS
8575/* Output any pending elements which have become next.
8576 As we output elements, constructor_unfilled_{fields,index}
8577 advances, which may cause other elements to become next;
8578 if so, they too are output.
8b6a5902 8579
3e4093b6
RS
8580 If ALL is 0, we return when there are
8581 no more pending elements to output now.
665f2503 8582
3e4093b6
RS
8583 If ALL is 1, we output space as necessary so that
8584 we can output all the pending elements. */
3e4093b6 8585static void
a1e3b3d9 8586output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8587{
8588 struct init_node *elt = constructor_pending_elts;
8589 tree next;
de520661 8590
3e4093b6
RS
8591 retry:
8592
ba228239 8593 /* Look through the whole pending tree.
3e4093b6
RS
8594 If we find an element that should be output now,
8595 output it. Otherwise, set NEXT to the element
8596 that comes first among those still pending. */
8597
8598 next = 0;
8599 while (elt)
8600 {
8601 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8602 {
3e4093b6
RS
8603 if (tree_int_cst_equal (elt->purpose,
8604 constructor_unfilled_index))
34cf811f
MP
8605 output_init_element (input_location, elt->value, elt->origtype,
8606 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8607 constructor_unfilled_index, 0, false,
8608 braced_init_obstack);
3e4093b6
RS
8609 else if (tree_int_cst_lt (constructor_unfilled_index,
8610 elt->purpose))
8b6a5902 8611 {
3e4093b6
RS
8612 /* Advance to the next smaller node. */
8613 if (elt->left)
8614 elt = elt->left;
8615 else
8616 {
8617 /* We have reached the smallest node bigger than the
8618 current unfilled index. Fill the space first. */
8619 next = elt->purpose;
8620 break;
8621 }
8b6a5902 8622 }
ce662d4c
JJ
8623 else
8624 {
3e4093b6
RS
8625 /* Advance to the next bigger node. */
8626 if (elt->right)
8627 elt = elt->right;
8628 else
ce662d4c 8629 {
3e4093b6
RS
8630 /* We have reached the biggest node in a subtree. Find
8631 the parent of it, which is the next bigger node. */
8632 while (elt->parent && elt->parent->right == elt)
8633 elt = elt->parent;
8634 elt = elt->parent;
8635 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8636 elt->purpose))
8637 {
8638 next = elt->purpose;
8639 break;
8640 }
ce662d4c
JJ
8641 }
8642 }
8b6a5902 8643 }
3e4093b6
RS
8644 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8645 || TREE_CODE (constructor_type) == UNION_TYPE)
8646 {
8647 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8648
3e4093b6
RS
8649 /* If the current record is complete we are done. */
8650 if (constructor_unfilled_fields == 0)
8651 break;
de520661 8652
3e4093b6
RS
8653 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8654 elt_bitpos = bit_position (elt->purpose);
8655 /* We can't compare fields here because there might be empty
8656 fields in between. */
8657 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8658 {
8659 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8660 output_init_element (input_location, elt->value, elt->origtype,
8661 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8662 elt->purpose, 0, false,
8663 braced_init_obstack);
3e4093b6
RS
8664 }
8665 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8666 {
8667 /* Advance to the next smaller node. */
8668 if (elt->left)
8669 elt = elt->left;
8670 else
8671 {
8672 /* We have reached the smallest node bigger than the
8673 current unfilled field. Fill the space first. */
8674 next = elt->purpose;
8675 break;
8676 }
8677 }
8678 else
8679 {
8680 /* Advance to the next bigger node. */
8681 if (elt->right)
8682 elt = elt->right;
8683 else
8684 {
8685 /* We have reached the biggest node in a subtree. Find
8686 the parent of it, which is the next bigger node. */
8687 while (elt->parent && elt->parent->right == elt)
8688 elt = elt->parent;
8689 elt = elt->parent;
8690 if (elt
8691 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8692 bit_position (elt->purpose))))
8693 {
8694 next = elt->purpose;
8695 break;
8696 }
8697 }
8698 }
8699 }
8700 }
de520661 8701
3e4093b6
RS
8702 /* Ordinarily return, but not if we want to output all
8703 and there are elements left. */
3f75a254 8704 if (!(all && next != 0))
e5cfb88f
RK
8705 return;
8706
3e4093b6
RS
8707 /* If it's not incremental, just skip over the gap, so that after
8708 jumping to retry we will output the next successive element. */
8709 if (TREE_CODE (constructor_type) == RECORD_TYPE
8710 || TREE_CODE (constructor_type) == UNION_TYPE)
8711 constructor_unfilled_fields = next;
8712 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8713 constructor_unfilled_index = next;
de520661 8714
3e4093b6
RS
8715 /* ELT now points to the node in the pending tree with the next
8716 initializer to output. */
8717 goto retry;
de520661
RS
8718}
8719\f
3e4093b6
RS
8720/* Add one non-braced element to the current constructor level.
8721 This adjusts the current position within the constructor's type.
8722 This may also start or terminate implicit levels
8723 to handle a partly-braced initializer.
e5e809f4 8724
3e4093b6 8725 Once this has found the correct level for the new element,
b295aee2
JJ
8726 it calls output_init_element.
8727
8728 IMPLICIT is true if value comes from pop_init_level (1),
8729 the new initializer has been merged with the existing one
8730 and thus no warnings should be emitted about overriding an
8731 existing initializer. */
3e4093b6
RS
8732
8733void
34cf811f 8734process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8735 struct obstack * braced_init_obstack)
e5e809f4 8736{
916c5919
JM
8737 tree orig_value = value.value;
8738 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8739 bool strict_string = value.original_code == STRING_CST;
340baef7 8740 bool was_designated = designator_depth != 0;
e5e809f4 8741
3e4093b6 8742 designator_depth = 0;
b06df647 8743 designator_erroneous = 0;
e5e809f4 8744
9bac5cbb
G
8745 if (!implicit && value.value && !integer_zerop (value.value))
8746 constructor_zeroinit = 0;
8747
3e4093b6
RS
8748 /* Handle superfluous braces around string cst as in
8749 char x[] = {"foo"}; */
8750 if (string_flag
8751 && constructor_type
340baef7 8752 && !was_designated
3e4093b6 8753 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8754 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8755 && integer_zerop (constructor_unfilled_index))
e5e809f4 8756 {
916c5919 8757 if (constructor_stack->replacement_value.value)
ea58ef42 8758 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8759 constructor_stack->replacement_value = value;
8760 return;
e5e809f4 8761 }
8b6a5902 8762
916c5919 8763 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8764 {
ea58ef42 8765 error_init (loc, "excess elements in struct initializer");
3e4093b6 8766 return;
e5e809f4
JL
8767 }
8768
3e4093b6
RS
8769 /* Ignore elements of a brace group if it is entirely superfluous
8770 and has already been diagnosed. */
8771 if (constructor_type == 0)
8772 return;
e5e809f4 8773
976d5a22
TT
8774 if (!implicit && warn_designated_init && !was_designated
8775 && TREE_CODE (constructor_type) == RECORD_TYPE
8776 && lookup_attribute ("designated_init",
8777 TYPE_ATTRIBUTES (constructor_type)))
8778 warning_init (loc,
8779 OPT_Wdesignated_init,
8780 "positional initialization of field "
8781 "in %<struct%> declared with %<designated_init%> attribute");
8782
3e4093b6
RS
8783 /* If we've exhausted any levels that didn't have braces,
8784 pop them now. */
8785 while (constructor_stack->implicit)
8786 {
8787 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8788 || TREE_CODE (constructor_type) == UNION_TYPE)
8789 && constructor_fields == 0)
ea58ef42
MP
8790 process_init_element (loc,
8791 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8792 true, braced_init_obstack);
53650abe
AP
8793 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8794 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8795 && constructor_max_index
8796 && tree_int_cst_lt (constructor_max_index,
8797 constructor_index))
ea58ef42
MP
8798 process_init_element (loc,
8799 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8800 true, braced_init_obstack);
3e4093b6
RS
8801 else
8802 break;
8803 }
e5e809f4 8804
3e4093b6
RS
8805 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8806 if (constructor_range_stack)
e5e809f4 8807 {
3e4093b6
RS
8808 /* If value is a compound literal and we'll be just using its
8809 content, don't put it into a SAVE_EXPR. */
916c5919 8810 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 8811 || !require_constant_value)
8ce94e44
JM
8812 {
8813 tree semantic_type = NULL_TREE;
8814 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8815 {
8816 semantic_type = TREE_TYPE (value.value);
8817 value.value = TREE_OPERAND (value.value, 0);
8818 }
8819 value.value = c_save_expr (value.value);
8820 if (semantic_type)
8821 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8822 value.value);
8823 }
3e4093b6 8824 }
e5e809f4 8825
3e4093b6
RS
8826 while (1)
8827 {
8828 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8829 {
3e4093b6
RS
8830 tree fieldtype;
8831 enum tree_code fieldcode;
e5e809f4 8832
3e4093b6
RS
8833 if (constructor_fields == 0)
8834 {
ea58ef42 8835 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8836 break;
8837 }
e5e809f4 8838
3e4093b6
RS
8839 fieldtype = TREE_TYPE (constructor_fields);
8840 if (fieldtype != error_mark_node)
8841 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8842 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8843
3e4093b6
RS
8844 /* Error for non-static initialization of a flexible array member. */
8845 if (fieldcode == ARRAY_TYPE
8846 && !require_constant_value
8847 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8848 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8849 {
ea58ef42
MP
8850 error_init (loc, "non-static initialization of a flexible "
8851 "array member");
3e4093b6
RS
8852 break;
8853 }
e5e809f4 8854
2cc901dc
MP
8855 /* Error for initialization of a flexible array member with
8856 a string constant if the structure is in an array. E.g.:
8857 struct S { int x; char y[]; };
8858 struct S s[] = { { 1, "foo" } };
8859 is invalid. */
8860 if (string_flag
8861 && fieldcode == ARRAY_TYPE
8862 && constructor_depth > 1
8863 && TYPE_SIZE (fieldtype) == NULL_TREE
8864 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8865 {
8866 bool in_array_p = false;
8867 for (struct constructor_stack *p = constructor_stack;
8868 p && p->type; p = p->next)
8869 if (TREE_CODE (p->type) == ARRAY_TYPE)
8870 {
8871 in_array_p = true;
8872 break;
8873 }
8874 if (in_array_p)
8875 {
8876 error_init (loc, "initialization of flexible array "
8877 "member in a nested context");
8878 break;
8879 }
8880 }
8881
3e4093b6 8882 /* Accept a string constant to initialize a subarray. */
916c5919 8883 if (value.value != 0
3e4093b6 8884 && fieldcode == ARRAY_TYPE
197463ae 8885 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8886 && string_flag)
916c5919 8887 value.value = orig_value;
3e4093b6
RS
8888 /* Otherwise, if we have come to a subaggregate,
8889 and we don't have an element of its type, push into it. */
0953878d 8890 else if (value.value != 0
916c5919
JM
8891 && value.value != error_mark_node
8892 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8893 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8894 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8895 {
ea58ef42 8896 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8897 continue;
8898 }
e5e809f4 8899
916c5919 8900 if (value.value)
3e4093b6
RS
8901 {
8902 push_member_name (constructor_fields);
34cf811f 8903 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8904 strict_string, fieldtype,
a1e3b3d9
LB
8905 constructor_fields, 1, implicit,
8906 braced_init_obstack);
3e4093b6 8907 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8908 }
8909 else
3e4093b6
RS
8910 /* Do the bookkeeping for an element that was
8911 directly output as a constructor. */
e5e809f4 8912 {
3e4093b6
RS
8913 /* For a record, keep track of end position of last field. */
8914 if (DECL_SIZE (constructor_fields))
c22cacf3 8915 constructor_bit_index
db3927fb
AH
8916 = size_binop_loc (input_location, PLUS_EXPR,
8917 bit_position (constructor_fields),
8918 DECL_SIZE (constructor_fields));
3e4093b6
RS
8919
8920 /* If the current field was the first one not yet written out,
8921 it isn't now, so update. */
8922 if (constructor_unfilled_fields == constructor_fields)
8923 {
910ad8de 8924 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8925 /* Skip any nameless bit fields. */
8926 while (constructor_unfilled_fields != 0
8927 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8928 && DECL_NAME (constructor_unfilled_fields) == 0)
8929 constructor_unfilled_fields =
910ad8de 8930 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8931 }
e5e809f4 8932 }
3e4093b6 8933
910ad8de 8934 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8935 /* Skip any nameless bit fields at the beginning. */
8936 while (constructor_fields != 0
8937 && DECL_C_BIT_FIELD (constructor_fields)
8938 && DECL_NAME (constructor_fields) == 0)
910ad8de 8939 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8940 }
3e4093b6 8941 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8942 {
3e4093b6
RS
8943 tree fieldtype;
8944 enum tree_code fieldcode;
e5e809f4 8945
3e4093b6
RS
8946 if (constructor_fields == 0)
8947 {
d033409e 8948 pedwarn_init (loc, 0,
509c9d60 8949 "excess elements in union initializer");
3e4093b6
RS
8950 break;
8951 }
e5e809f4 8952
3e4093b6
RS
8953 fieldtype = TREE_TYPE (constructor_fields);
8954 if (fieldtype != error_mark_node)
8955 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8956 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8957
3e4093b6
RS
8958 /* Warn that traditional C rejects initialization of unions.
8959 We skip the warning if the value is zero. This is done
8960 under the assumption that the zero initializer in user
8961 code appears conditioned on e.g. __STDC__ to avoid
8962 "missing initializer" warnings and relies on default
8963 initialization to zero in the traditional C case.
8964 We also skip the warning if the initializer is designated,
8965 again on the assumption that this must be conditional on
8966 __STDC__ anyway (and we've already complained about the
8967 member-designator already). */
8400e75e 8968 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8969 && !(value.value && (integer_zerop (value.value)
8970 || real_zerop (value.value))))
3176a0c2
DD
8971 warning (OPT_Wtraditional, "traditional C rejects initialization "
8972 "of unions");
e5e809f4 8973
3e4093b6 8974 /* Accept a string constant to initialize a subarray. */
916c5919 8975 if (value.value != 0
3e4093b6 8976 && fieldcode == ARRAY_TYPE
197463ae 8977 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8978 && string_flag)
916c5919 8979 value.value = orig_value;
3e4093b6
RS
8980 /* Otherwise, if we have come to a subaggregate,
8981 and we don't have an element of its type, push into it. */
0953878d 8982 else if (value.value != 0
916c5919
JM
8983 && value.value != error_mark_node
8984 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8985 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8986 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8987 {
ea58ef42 8988 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8989 continue;
8990 }
e5e809f4 8991
916c5919 8992 if (value.value)
3e4093b6
RS
8993 {
8994 push_member_name (constructor_fields);
34cf811f 8995 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8996 strict_string, fieldtype,
a1e3b3d9
LB
8997 constructor_fields, 1, implicit,
8998 braced_init_obstack);
3e4093b6 8999 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9000 }
9001 else
3e4093b6
RS
9002 /* Do the bookkeeping for an element that was
9003 directly output as a constructor. */
e5e809f4 9004 {
3e4093b6 9005 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9006 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9007 }
e5e809f4 9008
3e4093b6
RS
9009 constructor_fields = 0;
9010 }
9011 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9012 {
9013 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9014 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9015
3e4093b6 9016 /* Accept a string constant to initialize a subarray. */
916c5919 9017 if (value.value != 0
3e4093b6 9018 && eltcode == ARRAY_TYPE
197463ae 9019 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9020 && string_flag)
916c5919 9021 value.value = orig_value;
3e4093b6
RS
9022 /* Otherwise, if we have come to a subaggregate,
9023 and we don't have an element of its type, push into it. */
0953878d 9024 else if (value.value != 0
916c5919
JM
9025 && value.value != error_mark_node
9026 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9027 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9028 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9029 {
ea58ef42 9030 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9031 continue;
9032 }
8b6a5902 9033
3e4093b6
RS
9034 if (constructor_max_index != 0
9035 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9036 || integer_all_onesp (constructor_max_index)))
9037 {
d033409e 9038 pedwarn_init (loc, 0,
509c9d60 9039 "excess elements in array initializer");
3e4093b6
RS
9040 break;
9041 }
8b6a5902 9042
3e4093b6 9043 /* Now output the actual element. */
916c5919 9044 if (value.value)
3e4093b6 9045 {
ae7e9ddd 9046 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9047 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9048 strict_string, elttype,
a1e3b3d9
LB
9049 constructor_index, 1, implicit,
9050 braced_init_obstack);
3e4093b6
RS
9051 RESTORE_SPELLING_DEPTH (constructor_depth);
9052 }
2f6e4e97 9053
3e4093b6 9054 constructor_index
db3927fb
AH
9055 = size_binop_loc (input_location, PLUS_EXPR,
9056 constructor_index, bitsize_one_node);
8b6a5902 9057
916c5919 9058 if (!value.value)
3e4093b6
RS
9059 /* If we are doing the bookkeeping for an element that was
9060 directly output as a constructor, we must update
9061 constructor_unfilled_index. */
9062 constructor_unfilled_index = constructor_index;
9063 }
9064 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9065 {
9066 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9067
c22cacf3
MS
9068 /* Do a basic check of initializer size. Note that vectors
9069 always have a fixed size derived from their type. */
3e4093b6
RS
9070 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9071 {
d033409e 9072 pedwarn_init (loc, 0,
509c9d60 9073 "excess elements in vector initializer");
3e4093b6
RS
9074 break;
9075 }
8b6a5902 9076
3e4093b6 9077 /* Now output the actual element. */
916c5919 9078 if (value.value)
53650abe
AP
9079 {
9080 if (TREE_CODE (value.value) == VECTOR_CST)
9081 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9082 output_init_element (loc, value.value, value.original_type,
53650abe 9083 strict_string, elttype,
a1e3b3d9
LB
9084 constructor_index, 1, implicit,
9085 braced_init_obstack);
53650abe 9086 }
8b6a5902 9087
3e4093b6 9088 constructor_index
db3927fb
AH
9089 = size_binop_loc (input_location,
9090 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9091
916c5919 9092 if (!value.value)
3e4093b6
RS
9093 /* If we are doing the bookkeeping for an element that was
9094 directly output as a constructor, we must update
9095 constructor_unfilled_index. */
9096 constructor_unfilled_index = constructor_index;
9097 }
8b6a5902 9098
3e4093b6
RS
9099 /* Handle the sole element allowed in a braced initializer
9100 for a scalar variable. */
b4519d39
SB
9101 else if (constructor_type != error_mark_node
9102 && constructor_fields == 0)
8b6a5902 9103 {
d033409e 9104 pedwarn_init (loc, 0,
509c9d60 9105 "excess elements in scalar initializer");
3e4093b6 9106 break;
8b6a5902
JJ
9107 }
9108 else
9109 {
916c5919 9110 if (value.value)
34cf811f 9111 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9112 strict_string, constructor_type,
a1e3b3d9
LB
9113 NULL_TREE, 1, implicit,
9114 braced_init_obstack);
3e4093b6 9115 constructor_fields = 0;
8b6a5902
JJ
9116 }
9117
3e4093b6
RS
9118 /* Handle range initializers either at this level or anywhere higher
9119 in the designator stack. */
9120 if (constructor_range_stack)
8b6a5902 9121 {
3e4093b6
RS
9122 struct constructor_range_stack *p, *range_stack;
9123 int finish = 0;
9124
9125 range_stack = constructor_range_stack;
9126 constructor_range_stack = 0;
9127 while (constructor_stack != range_stack->stack)
8b6a5902 9128 {
366de0ce 9129 gcc_assert (constructor_stack->implicit);
34cf811f 9130 process_init_element (loc,
ea58ef42
MP
9131 pop_init_level (loc, 1,
9132 braced_init_obstack),
a1e3b3d9 9133 true, braced_init_obstack);
8b6a5902 9134 }
3e4093b6
RS
9135 for (p = range_stack;
9136 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9137 p = p->prev)
8b6a5902 9138 {
366de0ce 9139 gcc_assert (constructor_stack->implicit);
34cf811f 9140 process_init_element (loc,
ea58ef42
MP
9141 pop_init_level (loc, 1,
9142 braced_init_obstack),
a1e3b3d9 9143 true, braced_init_obstack);
8b6a5902 9144 }
3e4093b6 9145
db3927fb
AH
9146 p->index = size_binop_loc (input_location,
9147 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9148 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9149 finish = 1;
9150
9151 while (1)
9152 {
9153 constructor_index = p->index;
9154 constructor_fields = p->fields;
9155 if (finish && p->range_end && p->index == p->range_start)
9156 {
9157 finish = 0;
9158 p->prev = 0;
9159 }
9160 p = p->next;
9161 if (!p)
9162 break;
ea58ef42 9163 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9164 p->stack = constructor_stack;
9165 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9166 p->index = p->range_start;
9167 }
9168
9169 if (!finish)
9170 constructor_range_stack = range_stack;
9171 continue;
8b6a5902
JJ
9172 }
9173
3e4093b6 9174 break;
8b6a5902
JJ
9175 }
9176
3e4093b6
RS
9177 constructor_range_stack = 0;
9178}
9179\f
9f0e2d86
ZW
9180/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9181 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9182 an ASM_EXPR node). */
3e4093b6 9183tree
9f0e2d86 9184build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9185{
6de9cd9a
DN
9186 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9187 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9188 return add_stmt (args);
8b6a5902
JJ
9189}
9190
9f0e2d86
ZW
9191/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9192 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9193 SIMPLE indicates whether there was anything at all after the
9194 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9195 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9196tree
c2255bc4 9197build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9198 tree clobbers, tree labels, bool simple)
e5e809f4 9199{
3e4093b6 9200 tree tail;
9f0e2d86 9201 tree args;
6de9cd9a
DN
9202 int i;
9203 const char *constraint;
74f0c611 9204 const char **oconstraints;
6de9cd9a 9205 bool allows_mem, allows_reg, is_inout;
74f0c611 9206 int ninputs, noutputs;
6de9cd9a
DN
9207
9208 ninputs = list_length (inputs);
9209 noutputs = list_length (outputs);
74f0c611
RH
9210 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9211
1c384bf1 9212 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9213
6de9cd9a
DN
9214 /* Remove output conversions that change the type but not the mode. */
9215 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9216 {
3e4093b6 9217 tree output = TREE_VALUE (tail);
74f0c611 9218
eadd3d0d
JJ
9219 output = c_fully_fold (output, false, NULL);
9220
74f0c611
RH
9221 /* ??? Really, this should not be here. Users should be using a
9222 proper lvalue, dammit. But there's a long history of using casts
9223 in the output operands. In cases like longlong.h, this becomes a
9224 primitive form of typechecking -- if the cast can be removed, then
9225 the output operand had a type of the proper width; otherwise we'll
9226 get an error. Gross, but ... */
3e4093b6 9227 STRIP_NOPS (output);
74f0c611 9228
7bd11157 9229 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9230 output = error_mark_node;
8b6a5902 9231
5544530a
PB
9232 if (output != error_mark_node
9233 && (TREE_READONLY (output)
9234 || TYPE_READONLY (TREE_TYPE (output))
9235 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9236 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9237 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9238 readonly_error (loc, output, lv_asm);
5544530a 9239
6de9cd9a 9240 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9241 oconstraints[i] = constraint;
9242
9243 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9244 &allows_mem, &allows_reg, &is_inout))
9245 {
9246 /* If the operand is going to end up in memory,
9247 mark it addressable. */
9248 if (!allows_reg && !c_mark_addressable (output))
9249 output = error_mark_node;
bae5cddf
JJ
9250 if (!(!allows_reg && allows_mem)
9251 && output != error_mark_node
9252 && VOID_TYPE_P (TREE_TYPE (output)))
9253 {
9254 error_at (loc, "invalid use of void expression");
9255 output = error_mark_node;
9256 }
74f0c611
RH
9257 }
9258 else
c22cacf3 9259 output = error_mark_node;
3e4093b6 9260
74f0c611 9261 TREE_VALUE (tail) = output;
8b6a5902 9262 }
3e4093b6 9263
74f0c611
RH
9264 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9265 {
9266 tree input;
9267
9268 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9269 input = TREE_VALUE (tail);
9270
74f0c611
RH
9271 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9272 oconstraints, &allows_mem, &allows_reg))
9273 {
9274 /* If the operand is going to end up in memory,
9275 mark it addressable. */
b4c33883
AP
9276 if (!allows_reg && allows_mem)
9277 {
eadd3d0d
JJ
9278 input = c_fully_fold (input, false, NULL);
9279
b4c33883
AP
9280 /* Strip the nops as we allow this case. FIXME, this really
9281 should be rejected or made deprecated. */
9282 STRIP_NOPS (input);
9283 if (!c_mark_addressable (input))
9284 input = error_mark_node;
bae5cddf 9285 }
eadd3d0d 9286 else
bae5cddf 9287 {
eadd3d0d
JJ
9288 struct c_expr expr;
9289 memset (&expr, 0, sizeof (expr));
9290 expr.value = input;
267bac10 9291 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9292 input = c_fully_fold (expr.value, false, NULL);
9293
9294 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9295 {
9296 error_at (loc, "invalid use of void expression");
9297 input = error_mark_node;
9298 }
bae5cddf 9299 }
74f0c611
RH
9300 }
9301 else
9302 input = error_mark_node;
9303
9304 TREE_VALUE (tail) = input;
9305 }
3e4093b6 9306
1c384bf1
RH
9307 /* ASMs with labels cannot have outputs. This should have been
9308 enforced by the parser. */
9309 gcc_assert (outputs == NULL || labels == NULL);
9310
9311 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9312
5544530a
PB
9313 /* asm statements without outputs, including simple ones, are treated
9314 as volatile. */
9315 ASM_INPUT_P (args) = simple;
9316 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9317
9f0e2d86 9318 return args;
e5e809f4 9319}
3e4093b6 9320\f
c2255bc4
AH
9321/* Generate a goto statement to LABEL. LOC is the location of the
9322 GOTO. */
506e2710
RH
9323
9324tree
c2255bc4 9325c_finish_goto_label (location_t loc, tree label)
506e2710 9326{
e1b7793c 9327 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9328 if (!decl)
9329 return NULL_TREE;
506e2710 9330 TREE_USED (decl) = 1;
c2255bc4
AH
9331 {
9332 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9333 SET_EXPR_LOCATION (t, loc);
9334 return add_stmt (t);
9335 }
506e2710
RH
9336}
9337
c2255bc4
AH
9338/* Generate a computed goto statement to EXPR. LOC is the location of
9339 the GOTO. */
506e2710
RH
9340
9341tree
c2255bc4 9342c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9343{
c2255bc4 9344 tree t;
c1771a20 9345 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9346 expr = c_fully_fold (expr, false, NULL);
506e2710 9347 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9348 t = build1 (GOTO_EXPR, void_type_node, expr);
9349 SET_EXPR_LOCATION (t, loc);
9350 return add_stmt (t);
506e2710
RH
9351}
9352
5088b058 9353/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9354 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9355 the location of the return statement, or the location of the expression,
9356 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9357 is the original type of RETVAL. */
de520661 9358
506e2710 9359tree
c2255bc4 9360c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9361{
0c9b182b
JJ
9362 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9363 bool no_warning = false;
928c19bb 9364 bool npc = false;
36536d79 9365 size_t rank = 0;
3e4093b6
RS
9366
9367 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9368 warning_at (loc, 0,
9369 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9370
b72271b9 9371 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9372 {
9373 /* Array notations are allowed in a return statement if it is inside a
9374 built-in array notation reduction function. */
9375 if (!find_rank (loc, retval, retval, false, &rank))
9376 return error_mark_node;
9377 if (rank >= 1)
9378 {
9379 error_at (loc, "array notation expression cannot be used as a "
9380 "return value");
9381 return error_mark_node;
9382 }
9383 }
3af9c5e9 9384 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9385 {
9386 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9387 "allowed");
9388 return error_mark_node;
9389 }
928c19bb
JM
9390 if (retval)
9391 {
8ce94e44 9392 tree semantic_type = NULL_TREE;
928c19bb 9393 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9394 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9395 {
9396 semantic_type = TREE_TYPE (retval);
9397 retval = TREE_OPERAND (retval, 0);
9398 }
928c19bb 9399 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9400 if (semantic_type)
9401 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9402 }
9403
3e4093b6 9404 if (!retval)
de520661 9405 {
3e4093b6
RS
9406 current_function_returns_null = 1;
9407 if ((warn_return_type || flag_isoc99)
9408 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9409 {
35aff4fb
MP
9410 if (flag_isoc99)
9411 pedwarn (loc, 0, "%<return%> with no value, in "
9412 "function returning non-void");
9413 else
9414 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9415 "in function returning non-void");
0c9b182b
JJ
9416 no_warning = true;
9417 }
400fbf9f 9418 }
3e4093b6 9419 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9420 {
3e4093b6 9421 current_function_returns_null = 1;
2397c575 9422 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9423 pedwarn (loc, 0,
509c9d60 9424 "%<return%> with a value, in function returning void");
b8698a0f 9425 else
c1771a20 9426 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9427 "%<return%> with expression, in function returning void");
de520661 9428 }
3e4093b6 9429 else
de520661 9430 {
68fca595
MP
9431 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9432 retval, origtype, ic_return,
c2255bc4 9433 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9434 tree res = DECL_RESULT (current_function_decl);
9435 tree inner;
9feb29df 9436 bool save;
3e4093b6
RS
9437
9438 current_function_returns_value = 1;
9439 if (t == error_mark_node)
506e2710 9440 return NULL_TREE;
3e4093b6 9441
9feb29df
JJ
9442 save = in_late_binary_op;
9443 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9444 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9445 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9446 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9447 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9448 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9449 in_late_binary_op = true;
3e4093b6 9450 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9451 in_late_binary_op = save;
3e4093b6
RS
9452
9453 /* Strip any conversions, additions, and subtractions, and see if
9454 we are returning the address of a local variable. Warn if so. */
9455 while (1)
8b6a5902 9456 {
3e4093b6 9457 switch (TREE_CODE (inner))
8b6a5902 9458 {
849421a3
JJ
9459 CASE_CONVERT:
9460 case NON_LVALUE_EXPR:
3e4093b6 9461 case PLUS_EXPR:
849421a3 9462 case POINTER_PLUS_EXPR:
3e4093b6
RS
9463 inner = TREE_OPERAND (inner, 0);
9464 continue;
9465
9466 case MINUS_EXPR:
9467 /* If the second operand of the MINUS_EXPR has a pointer
9468 type (or is converted from it), this may be valid, so
9469 don't give a warning. */
9470 {
9471 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9472
3f75a254 9473 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9474 && (CONVERT_EXPR_P (op1)
9475 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9476 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9477
3e4093b6
RS
9478 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9479 break;
8b6a5902 9480
3e4093b6
RS
9481 inner = TREE_OPERAND (inner, 0);
9482 continue;
9483 }
400fbf9f 9484
3e4093b6
RS
9485 case ADDR_EXPR:
9486 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9487
6615c446 9488 while (REFERENCE_CLASS_P (inner)
c22cacf3 9489 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9490 inner = TREE_OPERAND (inner, 0);
8b6a5902 9491
a2f1f4c3 9492 if (DECL_P (inner)
3f75a254
JM
9493 && !DECL_EXTERNAL (inner)
9494 && !TREE_STATIC (inner)
3e4093b6 9495 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9496 {
9497 if (TREE_CODE (inner) == LABEL_DECL)
9498 warning_at (loc, OPT_Wreturn_local_addr,
9499 "function returns address of label");
9500 else
b4dfdc11
MG
9501 {
9502 warning_at (loc, OPT_Wreturn_local_addr,
9503 "function returns address of local variable");
9504 tree zero = build_zero_cst (TREE_TYPE (res));
9505 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9506 }
19fc9faa 9507 }
3e4093b6 9508 break;
8b6a5902 9509
3e4093b6
RS
9510 default:
9511 break;
9512 }
de520661 9513
3e4093b6
RS
9514 break;
9515 }
9516
53fb4de3 9517 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9518 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9519
9520 if (warn_sequence_point)
9521 verify_sequence_points (retval);
de520661 9522 }
8b6a5902 9523
c2255bc4 9524 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9525 TREE_NO_WARNING (ret_stmt) |= no_warning;
9526 return add_stmt (ret_stmt);
de520661 9527}
3e4093b6
RS
9528\f
9529struct c_switch {
604f5adf
ILT
9530 /* The SWITCH_EXPR being built. */
9531 tree switch_expr;
a6c0a76c 9532
89dbed81 9533 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9534 default conversion is applied. */
9535 tree orig_type;
9536
3e4093b6
RS
9537 /* A splay-tree mapping the low element of a case range to the high
9538 element, or NULL_TREE if there is no high element. Used to
9539 determine whether or not a new case label duplicates an old case
9540 label. We need a tree, rather than simply a hash table, because
9541 of the GNU case range extension. */
9542 splay_tree cases;
a6c0a76c 9543
e1b7793c
ILT
9544 /* The bindings at the point of the switch. This is used for
9545 warnings crossing decls when branching to a case label. */
9546 struct c_spot_bindings *bindings;
187230a7 9547
3e4093b6
RS
9548 /* The next node on the stack. */
9549 struct c_switch *next;
9550};
400fbf9f 9551
3e4093b6
RS
9552/* A stack of the currently active switch statements. The innermost
9553 switch statement is on the top of the stack. There is no need to
9554 mark the stack for garbage collection because it is only active
9555 during the processing of the body of a function, and we never
9556 collect at that point. */
de520661 9557
506e2710 9558struct c_switch *c_switch_stack;
de520661 9559
3e4093b6 9560/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9561 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9562 SWITCH_COND_LOC is the location of the switch's condition.
9563 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9564
3e4093b6 9565tree
c2255bc4
AH
9566c_start_case (location_t switch_loc,
9567 location_t switch_cond_loc,
fedfecef 9568 tree exp, bool explicit_cast_p)
de520661 9569{
c58e8676 9570 tree orig_type = error_mark_node;
3e4093b6 9571 struct c_switch *cs;
2f6e4e97 9572
3e4093b6 9573 if (exp != error_mark_node)
de520661 9574 {
3e4093b6
RS
9575 orig_type = TREE_TYPE (exp);
9576
c58e8676 9577 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9578 {
c58e8676
VR
9579 if (orig_type != error_mark_node)
9580 {
c2255bc4 9581 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9582 orig_type = error_mark_node;
9583 }
3e4093b6 9584 exp = integer_zero_node;
de520661 9585 }
3e4093b6 9586 else
de520661 9587 {
c58e8676 9588 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9589 tree e = exp;
9590
9591 /* Warn if the condition has boolean value. */
9592 while (TREE_CODE (e) == COMPOUND_EXPR)
9593 e = TREE_OPERAND (e, 1);
9594
9595 if ((TREE_CODE (type) == BOOLEAN_TYPE
9596 || truth_value_p (TREE_CODE (e)))
9597 /* Explicit cast to int suppresses this warning. */
9598 && !(TREE_CODE (type) == INTEGER_TYPE
9599 && explicit_cast_p))
9600 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9601 "switch condition has boolean value");
8b6a5902 9602
8400e75e 9603 if (!in_system_header_at (input_location)
3e4093b6
RS
9604 && (type == long_integer_type_node
9605 || type == long_unsigned_type_node))
c2255bc4
AH
9606 warning_at (switch_cond_loc,
9607 OPT_Wtraditional, "%<long%> switch expression not "
9608 "converted to %<int%> in ISO C");
8b6a5902 9609
928c19bb 9610 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9611 exp = default_conversion (exp);
ca085fd7
MLI
9612
9613 if (warn_sequence_point)
9614 verify_sequence_points (exp);
3e4093b6
RS
9615 }
9616 }
9617
604f5adf 9618 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9619 cs = XNEW (struct c_switch);
604f5adf 9620 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9621 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9622 cs->orig_type = orig_type;
3e4093b6 9623 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9624 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9625 cs->next = c_switch_stack;
9626 c_switch_stack = cs;
3e4093b6 9627
604f5adf 9628 return add_stmt (cs->switch_expr);
3e4093b6
RS
9629}
9630
c2255bc4 9631/* Process a case label at location LOC. */
3e4093b6
RS
9632
9633tree
c2255bc4 9634do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9635{
9636 tree label = NULL_TREE;
9637
17cede2e
JM
9638 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9639 {
9640 low_value = c_fully_fold (low_value, false, NULL);
9641 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9642 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9643 "case label is not an integer constant expression");
9644 }
9645
9646 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9647 {
9648 high_value = c_fully_fold (high_value, false, NULL);
9649 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9650 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9651 "case label is not an integer constant expression");
9652 }
9653
e1b7793c 9654 if (c_switch_stack == NULL)
187230a7
JM
9655 {
9656 if (low_value)
e1b7793c 9657 error_at (loc, "case label not within a switch statement");
187230a7 9658 else
e1b7793c
ILT
9659 error_at (loc, "%<default%> label not within a switch statement");
9660 return NULL_TREE;
187230a7 9661 }
de520661 9662
e1b7793c
ILT
9663 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9664 EXPR_LOCATION (c_switch_stack->switch_expr),
9665 loc))
9666 return NULL_TREE;
9667
9668 label = c_add_case_label (loc, c_switch_stack->cases,
9669 SWITCH_COND (c_switch_stack->switch_expr),
9670 c_switch_stack->orig_type,
9671 low_value, high_value);
9672 if (label == error_mark_node)
9673 label = NULL_TREE;
3e4093b6
RS
9674 return label;
9675}
de520661 9676
083e891e
MP
9677/* Finish the switch statement. TYPE is the original type of the
9678 controlling expression of the switch, or NULL_TREE. */
de520661 9679
3e4093b6 9680void
083e891e 9681c_finish_case (tree body, tree type)
3e4093b6 9682{
506e2710 9683 struct c_switch *cs = c_switch_stack;
fbc315db 9684 location_t switch_location;
3e4093b6 9685
604f5adf 9686 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9687
6de9cd9a 9688 /* Emit warnings as needed. */
c2255bc4 9689 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9690 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9691 type ? type : TREE_TYPE (cs->switch_expr),
fbc315db 9692 SWITCH_COND (cs->switch_expr));
6de9cd9a 9693
3e4093b6 9694 /* Pop the stack. */
506e2710 9695 c_switch_stack = cs->next;
3e4093b6 9696 splay_tree_delete (cs->cases);
e1b7793c 9697 c_release_switch_bindings (cs->bindings);
5d038c4c 9698 XDELETE (cs);
de520661 9699}
325c3691 9700\f
506e2710
RH
9701/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9702 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9703 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9704 statement, and was not surrounded with parenthesis. */
325c3691 9705
9e51cf9d 9706void
506e2710
RH
9707c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9708 tree else_block, bool nested_if)
325c3691 9709{
506e2710 9710 tree stmt;
325c3691 9711
25c22937
BI
9712 /* If the condition has array notations, then the rank of the then_block and
9713 else_block must be either 0 or be equal to the rank of the condition. If
9714 the condition does not have array notations then break them up as it is
9715 broken up in a normal expression. */
b72271b9 9716 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9717 {
9718 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9719 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9720 return;
9721 if (then_block
9722 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9723 return;
9724 if (else_block
9725 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9726 return;
9727 if (cond_rank != then_rank && then_rank != 0)
9728 {
9729 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9730 " and the then-block");
9731 return;
9732 }
9733 else if (cond_rank != else_rank && else_rank != 0)
9734 {
9735 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9736 " and the else-block");
9737 return;
9738 }
9739 }
506e2710
RH
9740 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9741 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9742 {
506e2710 9743 tree inner_if = then_block;
16865eaa 9744
61ada8ae 9745 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9746 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9747 it might not be exactly THEN_BLOCK, but should be the last
9748 non-container statement within. */
9749 while (1)
9750 switch (TREE_CODE (inner_if))
9751 {
9752 case COND_EXPR:
9753 goto found;
9754 case BIND_EXPR:
9755 inner_if = BIND_EXPR_BODY (inner_if);
9756 break;
9757 case STATEMENT_LIST:
9758 inner_if = expr_last (then_block);
9759 break;
9760 case TRY_FINALLY_EXPR:
9761 case TRY_CATCH_EXPR:
9762 inner_if = TREE_OPERAND (inner_if, 0);
9763 break;
9764 default:
366de0ce 9765 gcc_unreachable ();
506e2710
RH
9766 }
9767 found:
16865eaa 9768
506e2710 9769 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9770 warning_at (if_locus, OPT_Wparentheses,
9771 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9772 }
16865eaa 9773
2214de30 9774 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9775 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9776 add_stmt (stmt);
325c3691
RH
9777}
9778
506e2710
RH
9779/* Emit a general-purpose loop construct. START_LOCUS is the location of
9780 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9781 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9782 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9783 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9784
9785void
506e2710
RH
9786c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9787 tree blab, tree clab, bool cond_is_first)
325c3691 9788{
506e2710
RH
9789 tree entry = NULL, exit = NULL, t;
9790
e5e44252
AK
9791 /* In theory could forbid cilk spawn for loop increment expression,
9792 but it should work just fine. */
36536d79 9793
28af952a
RS
9794 /* If the condition is zero don't generate a loop construct. */
9795 if (cond && integer_zerop (cond))
9796 {
9797 if (cond_is_first)
9798 {
9799 t = build_and_jump (&blab);
9800 SET_EXPR_LOCATION (t, start_locus);
9801 add_stmt (t);
9802 }
9803 }
9804 else
506e2710
RH
9805 {
9806 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9807
506e2710 9808 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9809 out of the loop, or to the top of it. If there's no exit condition,
9810 then we just build a jump back to the top. */
506e2710 9811 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9812
28af952a 9813 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9814 {
9815 /* Canonicalize the loop condition to the end. This means
9816 generating a branch to the loop condition. Reuse the
9817 continue label, if possible. */
9818 if (cond_is_first)
9819 {
9820 if (incr || !clab)
9821 {
9822 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9823 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9824 }
9825 else
9826 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9827 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9828 add_stmt (t);
9829 }
9830
506e2710 9831 t = build_and_jump (&blab);
506e2710 9832 if (cond_is_first)
db3927fb
AH
9833 exit = fold_build3_loc (start_locus,
9834 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9835 else
db3927fb
AH
9836 exit = fold_build3_loc (input_location,
9837 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9838 }
9839
506e2710
RH
9840 add_stmt (top);
9841 }
c22cacf3 9842
506e2710
RH
9843 if (body)
9844 add_stmt (body);
9845 if (clab)
9846 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9847 if (incr)
9848 add_stmt (incr);
9849 if (entry)
9850 add_stmt (entry);
9851 if (exit)
9852 add_stmt (exit);
9853 if (blab)
9854 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9855}
325c3691
RH
9856
9857tree
c2255bc4 9858c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9859{
089efaa4 9860 bool skip;
506e2710 9861 tree label = *label_p;
325c3691 9862
089efaa4
ILT
9863 /* In switch statements break is sometimes stylistically used after
9864 a return statement. This can lead to spurious warnings about
9865 control reaching the end of a non-void function when it is
9866 inlined. Note that we are calling block_may_fallthru with
9867 language specific tree nodes; this works because
9868 block_may_fallthru returns true when given something it does not
9869 understand. */
9870 skip = !block_may_fallthru (cur_stmt_list);
9871
506e2710 9872 if (!label)
089efaa4
ILT
9873 {
9874 if (!skip)
c2255bc4 9875 *label_p = label = create_artificial_label (loc);
089efaa4 9876 }
953ff289
DN
9877 else if (TREE_CODE (label) == LABEL_DECL)
9878 ;
9879 else switch (TREE_INT_CST_LOW (label))
506e2710 9880 {
953ff289 9881 case 0:
506e2710 9882 if (is_break)
c2255bc4 9883 error_at (loc, "break statement not within loop or switch");
506e2710 9884 else
c2255bc4 9885 error_at (loc, "continue statement not within a loop");
506e2710 9886 return NULL_TREE;
953ff289
DN
9887
9888 case 1:
9889 gcc_assert (is_break);
c2255bc4 9890 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9891 return NULL_TREE;
9892
c02065fc
AH
9893 case 2:
9894 if (is_break)
9895 error ("break statement within %<#pragma simd%> loop body");
9896 else
9897 error ("continue statement within %<#pragma simd%> loop body");
9898 return NULL_TREE;
9899
953ff289
DN
9900 default:
9901 gcc_unreachable ();
506e2710 9902 }
325c3691 9903
089efaa4
ILT
9904 if (skip)
9905 return NULL_TREE;
9906
2e28e797
JH
9907 if (!is_break)
9908 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9909
53fb4de3 9910 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9911}
9912
506e2710 9913/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9914
9915static void
c2255bc4 9916emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9917{
e6b5a630
RH
9918 if (expr == error_mark_node)
9919 ;
9920 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9921 {
9922 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9923 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9924 }
789eadcd
MP
9925 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9926 {
9927 tree r = expr;
9928 location_t cloc = loc;
9929 while (TREE_CODE (r) == COMPOUND_EXPR)
9930 {
9931 if (EXPR_HAS_LOCATION (r))
9932 cloc = EXPR_LOCATION (r);
9933 r = TREE_OPERAND (r, 1);
9934 }
9935 if (!TREE_SIDE_EFFECTS (r)
9936 && !VOID_TYPE_P (TREE_TYPE (r))
9937 && !CONVERT_EXPR_P (r)
cc28fc7f 9938 && !TREE_NO_WARNING (r)
789eadcd
MP
9939 && !TREE_NO_WARNING (expr))
9940 warning_at (cloc, OPT_Wunused_value,
9941 "right-hand operand of comma expression has no effect");
9942 }
27f33b15 9943 else
c2255bc4 9944 warn_if_unused_value (expr, loc);
3a5b9284
RH
9945}
9946
506e2710 9947/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9948 diagnostics, but do not call ADD_STMT. LOC is the location of the
9949 statement. */
3a5b9284 9950
506e2710 9951tree
c2255bc4 9952c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9953{
056928b2
JJ
9954 tree exprv;
9955
3a5b9284 9956 if (!expr)
506e2710 9957 return NULL_TREE;
3a5b9284 9958
928c19bb
JM
9959 expr = c_fully_fold (expr, false, NULL);
9960
3a5b9284
RH
9961 if (warn_sequence_point)
9962 verify_sequence_points (expr);
9963
9964 if (TREE_TYPE (expr) != error_mark_node
9965 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9966 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9967 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9968
9969 /* If we're not processing a statement expression, warn about unused values.
9970 Warnings for statement expressions will be emitted later, once we figure
9971 out which is the result. */
9972 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9973 && warn_unused_value)
c2255bc4 9974 emit_side_effect_warnings (loc, expr);
3a5b9284 9975
056928b2
JJ
9976 exprv = expr;
9977 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9978 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9979 while (CONVERT_EXPR_P (exprv))
9980 exprv = TREE_OPERAND (exprv, 0);
9981 if (DECL_P (exprv)
9982 || handled_component_p (exprv)
9983 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9984 mark_exp_read (exprv);
fa8351f8 9985
3a5b9284
RH
9986 /* If the expression is not of a type to which we cannot assign a line
9987 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9988 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9989 {
9990 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9991 SET_EXPR_LOCATION (expr, loc);
9992 }
506e2710
RH
9993
9994 return expr;
9995}
9996
c2255bc4
AH
9997/* Emit an expression as a statement. LOC is the location of the
9998 expression. */
506e2710
RH
9999
10000tree
c2255bc4 10001c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10002{
10003 if (expr)
c2255bc4 10004 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10005 else
10006 return NULL;
3a5b9284
RH
10007}
10008
10009/* Do the opposite and emit a statement as an expression. To begin,
10010 create a new binding level and return it. */
325c3691
RH
10011
10012tree
10013c_begin_stmt_expr (void)
10014{
10015 tree ret;
10016
10017 /* We must force a BLOCK for this level so that, if it is not expanded
10018 later, there is a way to turn off the entire subtree of blocks that
10019 are contained in it. */
10020 keep_next_level ();
10021 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10022
10023 c_bindings_start_stmt_expr (c_switch_stack == NULL
10024 ? NULL
10025 : c_switch_stack->bindings);
325c3691
RH
10026
10027 /* Mark the current statement list as belonging to a statement list. */
10028 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10029
10030 return ret;
10031}
10032
c2255bc4
AH
10033/* LOC is the location of the compound statement to which this body
10034 belongs. */
10035
325c3691 10036tree
c2255bc4 10037c_finish_stmt_expr (location_t loc, tree body)
325c3691 10038{
3a5b9284 10039 tree last, type, tmp, val;
325c3691
RH
10040 tree *last_p;
10041
c2255bc4 10042 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10043
10044 c_bindings_end_stmt_expr (c_switch_stack == NULL
10045 ? NULL
10046 : c_switch_stack->bindings);
325c3691 10047
3a5b9284
RH
10048 /* Locate the last statement in BODY. See c_end_compound_stmt
10049 about always returning a BIND_EXPR. */
10050 last_p = &BIND_EXPR_BODY (body);
10051 last = BIND_EXPR_BODY (body);
10052
10053 continue_searching:
325c3691
RH
10054 if (TREE_CODE (last) == STATEMENT_LIST)
10055 {
3a5b9284
RH
10056 tree_stmt_iterator i;
10057
10058 /* This can happen with degenerate cases like ({ }). No value. */
10059 if (!TREE_SIDE_EFFECTS (last))
10060 return body;
10061
10062 /* If we're supposed to generate side effects warnings, process
10063 all of the statements except the last. */
27f33b15 10064 if (warn_unused_value)
325c3691 10065 {
3a5b9284 10066 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10067 {
10068 location_t tloc;
10069 tree t = tsi_stmt (i);
10070
10071 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10072 emit_side_effect_warnings (tloc, t);
10073 }
325c3691
RH
10074 }
10075 else
3a5b9284
RH
10076 i = tsi_last (last);
10077 last_p = tsi_stmt_ptr (i);
10078 last = *last_p;
325c3691
RH
10079 }
10080
3a5b9284
RH
10081 /* If the end of the list is exception related, then the list was split
10082 by a call to push_cleanup. Continue searching. */
10083 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10084 || TREE_CODE (last) == TRY_CATCH_EXPR)
10085 {
10086 last_p = &TREE_OPERAND (last, 0);
10087 last = *last_p;
10088 goto continue_searching;
10089 }
10090
26d8af35
JM
10091 if (last == error_mark_node)
10092 return last;
10093
3a5b9284
RH
10094 /* In the case that the BIND_EXPR is not necessary, return the
10095 expression out from inside it. */
26d8af35
JM
10096 if (last == BIND_EXPR_BODY (body)
10097 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10098 {
928c19bb
JM
10099 /* Even if this looks constant, do not allow it in a constant
10100 expression. */
e5a94231 10101 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10102 /* Do not warn if the return value of a statement expression is
10103 unused. */
928c19bb 10104 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10105 return last;
10106 }
325c3691
RH
10107
10108 /* Extract the type of said expression. */
10109 type = TREE_TYPE (last);
325c3691 10110
3a5b9284
RH
10111 /* If we're not returning a value at all, then the BIND_EXPR that
10112 we already have is a fine expression to return. */
10113 if (!type || VOID_TYPE_P (type))
10114 return body;
10115
10116 /* Now that we've located the expression containing the value, it seems
10117 silly to make voidify_wrapper_expr repeat the process. Create a
10118 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10119 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10120
10121 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10122 tree_expr_nonnegative_p giving up immediately. */
10123 val = last;
10124 if (TREE_CODE (val) == NOP_EXPR
10125 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10126 val = TREE_OPERAND (val, 0);
10127
53fb4de3 10128 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10129 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10130
c2255bc4
AH
10131 {
10132 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10133 SET_EXPR_LOCATION (t, loc);
10134 return t;
10135 }
325c3691
RH
10136}
10137\f
10138/* Begin and end compound statements. This is as simple as pushing
10139 and popping new statement lists from the tree. */
10140
10141tree
10142c_begin_compound_stmt (bool do_scope)
10143{
10144 tree stmt = push_stmt_list ();
10145 if (do_scope)
4dfa0342 10146 push_scope ();
325c3691
RH
10147 return stmt;
10148}
10149
c2255bc4
AH
10150/* End a compound statement. STMT is the statement. LOC is the
10151 location of the compound statement-- this is usually the location
10152 of the opening brace. */
10153
325c3691 10154tree
c2255bc4 10155c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10156{
10157 tree block = NULL;
10158
10159 if (do_scope)
10160 {
10161 if (c_dialect_objc ())
10162 objc_clear_super_receiver ();
10163 block = pop_scope ();
10164 }
10165
10166 stmt = pop_stmt_list (stmt);
c2255bc4 10167 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10168
10169 /* If this compound statement is nested immediately inside a statement
10170 expression, then force a BIND_EXPR to be created. Otherwise we'll
10171 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10172 STATEMENT_LISTs merge, and thus we can lose track of what statement
10173 was really last. */
38e01f9e 10174 if (building_stmt_list_p ()
325c3691
RH
10175 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10176 && TREE_CODE (stmt) != BIND_EXPR)
10177 {
53fb4de3 10178 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10179 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10180 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10181 }
10182
10183 return stmt;
10184}
5a508662
RH
10185
10186/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10187 when the current scope is exited. EH_ONLY is true when this is not
10188 meant to apply to normal control flow transfer. */
10189
10190void
c2255bc4 10191push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10192{
3a5b9284
RH
10193 enum tree_code code;
10194 tree stmt, list;
10195 bool stmt_expr;
10196
10197 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10198 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10199 add_stmt (stmt);
3a5b9284
RH
10200 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10201 list = push_stmt_list ();
10202 TREE_OPERAND (stmt, 0) = list;
10203 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10204}
325c3691 10205\f
3e4093b6
RS
10206/* Build a binary-operation expression without default conversions.
10207 CODE is the kind of expression to build.
ba47d38d 10208 LOCATION is the operator's location.
3e4093b6
RS
10209 This function differs from `build' in several ways:
10210 the data type of the result is computed and recorded in it,
10211 warnings are generated if arg data types are invalid,
10212 special handling for addition and subtraction of pointers is known,
10213 and some optimization is done (operations on narrow ints
10214 are done in the narrower type when that gives the same result).
10215 Constant folding is also done before the result is returned.
de520661 10216
3e4093b6
RS
10217 Note that the operands will never have enumeral types, or function
10218 or array types, because either they will have the default conversions
10219 performed or they have both just been converted to some other type in which
10220 the arithmetic is to be done. */
10221
10222tree
ba47d38d
AH
10223build_binary_op (location_t location, enum tree_code code,
10224 tree orig_op0, tree orig_op1, int convert_p)
de520661 10225{
8ce94e44
JM
10226 tree type0, type1, orig_type0, orig_type1;
10227 tree eptype;
3e4093b6
RS
10228 enum tree_code code0, code1;
10229 tree op0, op1;
c9f9eb5d 10230 tree ret = error_mark_node;
4de67c26 10231 const char *invalid_op_diag;
4d84fe7c 10232 bool op0_int_operands, op1_int_operands;
928c19bb 10233 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10234
3e4093b6
RS
10235 /* Expression code to give to the expression when it is built.
10236 Normally this is CODE, which is what the caller asked for,
10237 but in some special cases we change it. */
10238 enum tree_code resultcode = code;
8b6a5902 10239
3e4093b6
RS
10240 /* Data type in which the computation is to be performed.
10241 In the simplest cases this is the common type of the arguments. */
10242 tree result_type = NULL;
10243
8ce94e44
JM
10244 /* When the computation is in excess precision, the type of the
10245 final EXCESS_PRECISION_EXPR. */
2d2e923f 10246 tree semantic_result_type = NULL;
8ce94e44 10247
3e4093b6
RS
10248 /* Nonzero means operands have already been type-converted
10249 in whatever way is necessary.
10250 Zero means they need to be converted to RESULT_TYPE. */
10251 int converted = 0;
10252
10253 /* Nonzero means create the expression with this type, rather than
10254 RESULT_TYPE. */
10255 tree build_type = 0;
10256
10257 /* Nonzero means after finally constructing the expression
10258 convert it to this type. */
10259 tree final_type = 0;
10260
10261 /* Nonzero if this is an operation like MIN or MAX which can
10262 safely be computed in short if both args are promoted shorts.
10263 Also implies COMMON.
10264 -1 indicates a bitwise operation; this makes a difference
10265 in the exact conditions for when it is safe to do the operation
10266 in a narrower mode. */
10267 int shorten = 0;
10268
10269 /* Nonzero if this is a comparison operation;
10270 if both args are promoted shorts, compare the original shorts.
10271 Also implies COMMON. */
10272 int short_compare = 0;
10273
10274 /* Nonzero if this is a right-shift operation, which can be computed on the
10275 original short and then promoted if the operand is a promoted short. */
10276 int short_shift = 0;
10277
10278 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10279 int common = 0;
10280
58393038
ZL
10281 /* True means types are compatible as far as ObjC is concerned. */
10282 bool objc_ok;
10283
8ce94e44
JM
10284 /* True means this is an arithmetic operation that may need excess
10285 precision. */
10286 bool may_need_excess_precision;
10287
180f8dbb
JM
10288 /* True means this is a boolean operation that converts both its
10289 operands to truth-values. */
10290 bool boolean_op = false;
10291
de5a5fa1
MP
10292 /* Remember whether we're doing / or %. */
10293 bool doing_div_or_mod = false;
10294
10295 /* Remember whether we're doing << or >>. */
10296 bool doing_shift = false;
10297
10298 /* Tree holding instrumentation expression. */
10299 tree instrument_expr = NULL;
10300
ba47d38d
AH
10301 if (location == UNKNOWN_LOCATION)
10302 location = input_location;
10303
4d84fe7c
JM
10304 op0 = orig_op0;
10305 op1 = orig_op1;
10306
10307 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10308 if (op0_int_operands)
10309 op0 = remove_c_maybe_const_expr (op0);
10310 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10311 if (op1_int_operands)
10312 op1 = remove_c_maybe_const_expr (op1);
10313 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10314 if (int_operands)
10315 {
10316 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10317 && TREE_CODE (orig_op1) == INTEGER_CST);
10318 int_const = (int_const_or_overflow
10319 && !TREE_OVERFLOW (orig_op0)
10320 && !TREE_OVERFLOW (orig_op1));
10321 }
10322 else
10323 int_const = int_const_or_overflow = false;
10324
0e3a99ae 10325 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10326 if (convert_p
10327 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10328 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10329 {
4d84fe7c
JM
10330 op0 = default_conversion (op0);
10331 op1 = default_conversion (op1);
790e9490
RS
10332 }
10333
36536d79
BI
10334 /* When Cilk Plus is enabled and there are array notations inside op0, then
10335 we check to see if there are builtin array notation functions. If
10336 so, then we take on the type of the array notation inside it. */
b72271b9 10337 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10338 orig_type0 = type0 = find_correct_array_notation_type (op0);
10339 else
10340 orig_type0 = type0 = TREE_TYPE (op0);
10341
b72271b9 10342 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10343 orig_type1 = type1 = find_correct_array_notation_type (op1);
10344 else
10345 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10346
3e4093b6
RS
10347 /* The expression codes of the data types of the arguments tell us
10348 whether the arguments are integers, floating, pointers, etc. */
10349 code0 = TREE_CODE (type0);
10350 code1 = TREE_CODE (type1);
10351
10352 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10353 STRIP_TYPE_NOPS (op0);
10354 STRIP_TYPE_NOPS (op1);
10355
10356 /* If an error was already reported for one of the arguments,
10357 avoid reporting another error. */
10358
10359 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10360 return error_mark_node;
10361
4de67c26
JM
10362 if ((invalid_op_diag
10363 = targetm.invalid_binary_op (code, type0, type1)))
10364 {
ba47d38d 10365 error_at (location, invalid_op_diag);
4de67c26
JM
10366 return error_mark_node;
10367 }
10368
8ce94e44
JM
10369 switch (code)
10370 {
10371 case PLUS_EXPR:
10372 case MINUS_EXPR:
10373 case MULT_EXPR:
10374 case TRUNC_DIV_EXPR:
10375 case CEIL_DIV_EXPR:
10376 case FLOOR_DIV_EXPR:
10377 case ROUND_DIV_EXPR:
10378 case EXACT_DIV_EXPR:
10379 may_need_excess_precision = true;
10380 break;
10381 default:
10382 may_need_excess_precision = false;
10383 break;
10384 }
10385 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10386 {
10387 op0 = TREE_OPERAND (op0, 0);
10388 type0 = TREE_TYPE (op0);
10389 }
10390 else if (may_need_excess_precision
10391 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10392 {
10393 type0 = eptype;
10394 op0 = convert (eptype, op0);
10395 }
10396 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10397 {
10398 op1 = TREE_OPERAND (op1, 0);
10399 type1 = TREE_TYPE (op1);
10400 }
10401 else if (may_need_excess_precision
10402 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10403 {
10404 type1 = eptype;
10405 op1 = convert (eptype, op1);
10406 }
10407
58393038
ZL
10408 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10409
0e3a99ae
AS
10410 /* In case when one of the operands of the binary operation is
10411 a vector and another is a scalar -- convert scalar to vector. */
10412 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10413 {
a212e43f
MG
10414 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10415 true);
f90e8e2e 10416
0e3a99ae
AS
10417 switch (convert_flag)
10418 {
10419 case stv_error:
10420 return error_mark_node;
10421 case stv_firstarg:
10422 {
10423 bool maybe_const = true;
10424 tree sc;
10425 sc = c_fully_fold (op0, false, &maybe_const);
10426 sc = save_expr (sc);
10427 sc = convert (TREE_TYPE (type1), sc);
10428 op0 = build_vector_from_val (type1, sc);
10429 if (!maybe_const)
10430 op0 = c_wrap_maybe_const (op0, true);
10431 orig_type0 = type0 = TREE_TYPE (op0);
10432 code0 = TREE_CODE (type0);
10433 converted = 1;
10434 break;
10435 }
10436 case stv_secondarg:
10437 {
10438 bool maybe_const = true;
10439 tree sc;
10440 sc = c_fully_fold (op1, false, &maybe_const);
10441 sc = save_expr (sc);
10442 sc = convert (TREE_TYPE (type0), sc);
10443 op1 = build_vector_from_val (type0, sc);
10444 if (!maybe_const)
54b9f838 10445 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10446 orig_type1 = type1 = TREE_TYPE (op1);
10447 code1 = TREE_CODE (type1);
10448 converted = 1;
10449 break;
10450 }
10451 default:
10452 break;
10453 }
10454 }
10455
3e4093b6 10456 switch (code)
de520661 10457 {
3e4093b6
RS
10458 case PLUS_EXPR:
10459 /* Handle the pointer + int case. */
10460 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10461 {
db3927fb 10462 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10463 goto return_build_binary_op;
10464 }
3e4093b6 10465 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10466 {
db3927fb 10467 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10468 goto return_build_binary_op;
10469 }
fe67cf58 10470 else
3e4093b6
RS
10471 common = 1;
10472 break;
400fbf9f 10473
3e4093b6
RS
10474 case MINUS_EXPR:
10475 /* Subtraction of two similar pointers.
10476 We must subtract them as integers, then divide by object size. */
10477 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10478 && comp_target_types (location, type0, type1))
c9f9eb5d 10479 {
db3927fb 10480 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10481 goto return_build_binary_op;
10482 }
3e4093b6
RS
10483 /* Handle pointer minus int. Just like pointer plus int. */
10484 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10485 {
db3927fb 10486 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10487 goto return_build_binary_op;
10488 }
3e4093b6
RS
10489 else
10490 common = 1;
10491 break;
8b6a5902 10492
3e4093b6
RS
10493 case MULT_EXPR:
10494 common = 1;
10495 break;
10496
10497 case TRUNC_DIV_EXPR:
10498 case CEIL_DIV_EXPR:
10499 case FLOOR_DIV_EXPR:
10500 case ROUND_DIV_EXPR:
10501 case EXACT_DIV_EXPR:
de5a5fa1 10502 doing_div_or_mod = true;
c9f9eb5d 10503 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10504
10505 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10506 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10507 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10508 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10509 || code1 == FIXED_POINT_TYPE
3e4093b6 10510 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10511 {
5bed876a
AH
10512 enum tree_code tcode0 = code0, tcode1 = code1;
10513
3a021db2 10514 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10515 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10516 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10517 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10518
ab22c1fa
CF
10519 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10520 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10521 resultcode = RDIV_EXPR;
10522 else
10523 /* Although it would be tempting to shorten always here, that
10524 loses on some targets, since the modulo instruction is
10525 undefined if the quotient can't be represented in the
10526 computation mode. We shorten only if unsigned or if
10527 dividing by something we know != -1. */
8df83eae 10528 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10529 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10530 && !integer_all_onesp (op1)));
3e4093b6
RS
10531 common = 1;
10532 }
10533 break;
de520661 10534
3e4093b6 10535 case BIT_AND_EXPR:
3e4093b6
RS
10536 case BIT_IOR_EXPR:
10537 case BIT_XOR_EXPR:
10538 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10539 shorten = -1;
9ef0c8d9
AP
10540 /* Allow vector types which are not floating point types. */
10541 else if (code0 == VECTOR_TYPE
10542 && code1 == VECTOR_TYPE
10543 && !VECTOR_FLOAT_TYPE_P (type0)
10544 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10545 common = 1;
10546 break;
10547
10548 case TRUNC_MOD_EXPR:
10549 case FLOOR_MOD_EXPR:
de5a5fa1 10550 doing_div_or_mod = true;
c9f9eb5d 10551 warn_for_div_by_zero (location, op1);
de520661 10552
5cfd5d9b
AP
10553 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10554 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10555 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10556 common = 1;
10557 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10558 {
10559 /* Although it would be tempting to shorten always here, that loses
10560 on some targets, since the modulo instruction is undefined if the
10561 quotient can't be represented in the computation mode. We shorten
10562 only if unsigned or if dividing by something we know != -1. */
8df83eae 10563 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10564 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10565 && !integer_all_onesp (op1)));
3e4093b6
RS
10566 common = 1;
10567 }
10568 break;
de520661 10569
3e4093b6
RS
10570 case TRUTH_ANDIF_EXPR:
10571 case TRUTH_ORIF_EXPR:
10572 case TRUTH_AND_EXPR:
10573 case TRUTH_OR_EXPR:
10574 case TRUTH_XOR_EXPR:
10575 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10576 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10577 || code0 == FIXED_POINT_TYPE)
3e4093b6 10578 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10579 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10580 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10581 {
10582 /* Result of these operations is always an int,
10583 but that does not mean the operands should be
10584 converted to ints! */
10585 result_type = integer_type_node;
a27d595d
JM
10586 if (op0_int_operands)
10587 {
10588 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10589 op0 = remove_c_maybe_const_expr (op0);
10590 }
10591 else
10592 op0 = c_objc_common_truthvalue_conversion (location, op0);
10593 if (op1_int_operands)
10594 {
10595 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10596 op1 = remove_c_maybe_const_expr (op1);
10597 }
10598 else
10599 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10600 converted = 1;
180f8dbb 10601 boolean_op = true;
3e4093b6 10602 }
928c19bb
JM
10603 if (code == TRUTH_ANDIF_EXPR)
10604 {
10605 int_const_or_overflow = (int_operands
10606 && TREE_CODE (orig_op0) == INTEGER_CST
10607 && (op0 == truthvalue_false_node
10608 || TREE_CODE (orig_op1) == INTEGER_CST));
10609 int_const = (int_const_or_overflow
10610 && !TREE_OVERFLOW (orig_op0)
10611 && (op0 == truthvalue_false_node
10612 || !TREE_OVERFLOW (orig_op1)));
10613 }
10614 else if (code == TRUTH_ORIF_EXPR)
10615 {
10616 int_const_or_overflow = (int_operands
10617 && TREE_CODE (orig_op0) == INTEGER_CST
10618 && (op0 == truthvalue_true_node
10619 || TREE_CODE (orig_op1) == INTEGER_CST));
10620 int_const = (int_const_or_overflow
10621 && !TREE_OVERFLOW (orig_op0)
10622 && (op0 == truthvalue_true_node
10623 || !TREE_OVERFLOW (orig_op1)));
10624 }
3e4093b6 10625 break;
eba80994 10626
3e4093b6
RS
10627 /* Shift operations: result has same type as first operand;
10628 always convert second operand to int.
10629 Also set SHORT_SHIFT if shifting rightward. */
de520661 10630
3e4093b6 10631 case RSHIFT_EXPR:
f87bd04b
AS
10632 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10633 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10634 {
10635 result_type = type0;
10636 converted = 1;
10637 }
10638 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10639 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10640 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10641 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10642 {
10643 result_type = type0;
10644 converted = 1;
10645 }
10646 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10647 && code1 == INTEGER_TYPE)
3e4093b6 10648 {
de5a5fa1 10649 doing_shift = true;
928c19bb 10650 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10651 {
3e4093b6 10652 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10653 {
10654 int_const = false;
7d882b83 10655 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10656 warning_at (location, OPT_Wshift_count_negative,
10657 "right shift count is negative");
928c19bb 10658 }
3e4093b6 10659 else
bbb818c6 10660 {
3f75a254 10661 if (!integer_zerop (op1))
3e4093b6
RS
10662 short_shift = 1;
10663
10664 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10665 {
10666 int_const = false;
7d882b83 10667 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10668 warning_at (location, OPT_Wshift_count_overflow,
10669 "right shift count >= width of type");
928c19bb 10670 }
bbb818c6 10671 }
b62acd60 10672 }
de520661 10673
3e4093b6
RS
10674 /* Use the type of the value to be shifted. */
10675 result_type = type0;
3e4093b6
RS
10676 /* Avoid converting op1 to result_type later. */
10677 converted = 1;
400fbf9f 10678 }
3e4093b6 10679 break;
253b6b82 10680
3e4093b6 10681 case LSHIFT_EXPR:
f87bd04b
AS
10682 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10683 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10684 {
10685 result_type = type0;
10686 converted = 1;
10687 }
10688 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10689 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10690 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10691 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10692 {
10693 result_type = type0;
10694 converted = 1;
10695 }
10696 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10697 && code1 == INTEGER_TYPE)
3e4093b6 10698 {
de5a5fa1 10699 doing_shift = true;
928c19bb 10700 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10701 {
3e4093b6 10702 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10703 {
10704 int_const = false;
7d882b83 10705 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10706 warning_at (location, OPT_Wshift_count_negative,
10707 "left shift count is negative");
928c19bb 10708 }
de520661 10709
3e4093b6 10710 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10711 {
10712 int_const = false;
7d882b83 10713 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10714 warning_at (location, OPT_Wshift_count_overflow,
10715 "left shift count >= width of type");
928c19bb 10716 }
94ba5069 10717 }
de520661 10718
3e4093b6
RS
10719 /* Use the type of the value to be shifted. */
10720 result_type = type0;
3e4093b6
RS
10721 /* Avoid converting op1 to result_type later. */
10722 converted = 1;
400fbf9f 10723 }
3e4093b6 10724 break;
de520661 10725
3e4093b6
RS
10726 case EQ_EXPR:
10727 case NE_EXPR:
d246ab4f
AS
10728 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10729 {
10730 tree intt;
0af94e6f 10731 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10732 {
10733 error_at (location, "comparing vectors with different "
10734 "element types");
10735 return error_mark_node;
10736 }
10737
10738 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10739 {
10740 error_at (location, "comparing vectors with different "
10741 "number of elements");
10742 return error_mark_node;
10743 }
10744
10745 /* Always construct signed integer vector type. */
10746 intt = c_common_type_for_size (GET_MODE_BITSIZE
10747 (TYPE_MODE (TREE_TYPE (type0))), 0);
10748 result_type = build_opaque_vector_type (intt,
10749 TYPE_VECTOR_SUBPARTS (type0));
10750 converted = 1;
10751 break;
10752 }
ae311566 10753 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10754 warning_at (location,
10755 OPT_Wfloat_equal,
10756 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10757 /* Result of comparison is always int,
10758 but don't convert the args to int! */
10759 build_type = integer_type_node;
10760 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10761 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10762 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10763 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10764 short_compare = 1;
637f1455
SZ
10765 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10766 {
10767 if (TREE_CODE (op0) == ADDR_EXPR
10768 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10769 {
10770 if (code == EQ_EXPR)
10771 warning_at (location,
10772 OPT_Waddress,
10773 "the comparison will always evaluate as %<false%> "
10774 "for the address of %qD will never be NULL",
10775 TREE_OPERAND (op0, 0));
10776 else
10777 warning_at (location,
10778 OPT_Waddress,
10779 "the comparison will always evaluate as %<true%> "
10780 "for the address of %qD will never be NULL",
10781 TREE_OPERAND (op0, 0));
10782 }
10783 result_type = type0;
10784 }
10785 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10786 {
10787 if (TREE_CODE (op1) == ADDR_EXPR
10788 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10789 {
10790 if (code == EQ_EXPR)
10791 warning_at (location,
f90e8e2e 10792 OPT_Waddress,
637f1455
SZ
10793 "the comparison will always evaluate as %<false%> "
10794 "for the address of %qD will never be NULL",
10795 TREE_OPERAND (op1, 0));
10796 else
10797 warning_at (location,
10798 OPT_Waddress,
10799 "the comparison will always evaluate as %<true%> "
10800 "for the address of %qD will never be NULL",
10801 TREE_OPERAND (op1, 0));
10802 }
10803 result_type = type1;
10804 }
3e4093b6
RS
10805 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10806 {
10807 tree tt0 = TREE_TYPE (type0);
10808 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10809 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10810 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10811 addr_space_t as_common = ADDR_SPACE_GENERIC;
10812
3e4093b6
RS
10813 /* Anything compares with void *. void * compares with anything.
10814 Otherwise, the targets must be compatible
10815 and both must be object or both incomplete. */
744aa42f 10816 if (comp_target_types (location, type0, type1))
10bc1b1b 10817 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10818 else if (!addr_space_superset (as0, as1, &as_common))
10819 {
10820 error_at (location, "comparison of pointers to "
10821 "disjoint address spaces");
10822 return error_mark_node;
10823 }
267bac10 10824 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10825 {
36c5e70a 10826 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10827 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10828 "comparison of %<void *%> with function pointer");
ee2990e7 10829 }
267bac10 10830 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10831 {
36c5e70a 10832 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10833 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10834 "comparison of %<void *%> with function pointer");
e6834654 10835 }
3e4093b6 10836 else
58393038
ZL
10837 /* Avoid warning about the volatile ObjC EH puts on decls. */
10838 if (!objc_ok)
ba47d38d 10839 pedwarn (location, 0,
509c9d60 10840 "comparison of distinct pointer types lacks a cast");
e6834654 10841
3e4093b6 10842 if (result_type == NULL_TREE)
36c5e70a
BE
10843 {
10844 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10845 result_type = build_pointer_type
10846 (build_qualified_type (void_type_node, qual));
10847 }
e6834654 10848 }
3e4093b6 10849 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10850 {
3e4093b6 10851 result_type = type0;
ba47d38d 10852 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10853 }
3e4093b6 10854 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10855 {
3e4093b6 10856 result_type = type1;
ba47d38d 10857 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10858 }
04159acf
MP
10859 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10860 || truth_value_p (TREE_CODE (orig_op0)))
10861 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10862 || truth_value_p (TREE_CODE (orig_op1))))
10863 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10864 break;
8b6a5902 10865
3e4093b6
RS
10866 case LE_EXPR:
10867 case GE_EXPR:
10868 case LT_EXPR:
10869 case GT_EXPR:
d246ab4f
AS
10870 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10871 {
10872 tree intt;
0af94e6f 10873 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10874 {
10875 error_at (location, "comparing vectors with different "
10876 "element types");
10877 return error_mark_node;
10878 }
10879
10880 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10881 {
10882 error_at (location, "comparing vectors with different "
10883 "number of elements");
10884 return error_mark_node;
10885 }
10886
10887 /* Always construct signed integer vector type. */
10888 intt = c_common_type_for_size (GET_MODE_BITSIZE
10889 (TYPE_MODE (TREE_TYPE (type0))), 0);
10890 result_type = build_opaque_vector_type (intt,
10891 TYPE_VECTOR_SUBPARTS (type0));
10892 converted = 1;
10893 break;
10894 }
3e4093b6 10895 build_type = integer_type_node;
ab22c1fa
CF
10896 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10897 || code0 == FIXED_POINT_TYPE)
10898 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10899 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10900 short_compare = 1;
10901 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10902 {
36c5e70a
BE
10903 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10904 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10905 addr_space_t as_common;
10906
744aa42f 10907 if (comp_target_types (location, type0, type1))
3e4093b6 10908 {
10bc1b1b 10909 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10910 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10911 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10912 pedwarn (location, 0,
509c9d60 10913 "comparison of complete and incomplete pointers");
fcf73884 10914 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10915 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10916 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10917 else if (null_pointer_constant_p (orig_op0)
10918 || null_pointer_constant_p (orig_op1))
10919 warning_at (location, OPT_Wextra,
10920 "ordered comparison of pointer with null pointer");
10921
3e4093b6 10922 }
36c5e70a
BE
10923 else if (!addr_space_superset (as0, as1, &as_common))
10924 {
10925 error_at (location, "comparison of pointers to "
10926 "disjoint address spaces");
10927 return error_mark_node;
10928 }
3e4093b6
RS
10929 else
10930 {
36c5e70a
BE
10931 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10932 result_type = build_pointer_type
10933 (build_qualified_type (void_type_node, qual));
ba47d38d 10934 pedwarn (location, 0,
509c9d60 10935 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10936 }
10937 }
6aa3c60d 10938 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10939 {
10940 result_type = type0;
fcf73884 10941 if (pedantic)
c1771a20 10942 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10943 "ordered comparison of pointer with integer zero");
10944 else if (extra_warnings)
ba47d38d 10945 warning_at (location, OPT_Wextra,
d42ba3b8 10946 "ordered comparison of pointer with integer zero");
3e4093b6 10947 }
6aa3c60d 10948 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10949 {
10950 result_type = type1;
d42ba3b8 10951 if (pedantic)
c1771a20 10952 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10953 "ordered comparison of pointer with integer zero");
10954 else if (extra_warnings)
10955 warning_at (location, OPT_Wextra,
10956 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10957 }
10958 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10959 {
10960 result_type = type0;
ba47d38d 10961 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10962 }
10963 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10964 {
10965 result_type = type1;
ba47d38d 10966 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10967 }
04159acf
MP
10968 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10969 || truth_value_p (TREE_CODE (orig_op0)))
10970 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10971 || truth_value_p (TREE_CODE (orig_op1))))
10972 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10973 break;
64094f6a 10974
3e4093b6 10975 default:
37b2f290 10976 gcc_unreachable ();
c9fe6f9f 10977 }
8f17b5c5 10978
e57e265b
PB
10979 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10980 return error_mark_node;
10981
5bed876a
AH
10982 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10983 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10984 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10985 {
ba47d38d 10986 binary_op_error (location, code, type0, type1);
5bed876a
AH
10987 return error_mark_node;
10988 }
10989
3e4093b6 10990 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10991 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10992 &&
10993 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10994 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10995 {
2ca862e9
JM
10996 bool first_complex = (code0 == COMPLEX_TYPE);
10997 bool second_complex = (code1 == COMPLEX_TYPE);
10998 int none_complex = (!first_complex && !second_complex);
39b726dd 10999
3e4093b6 11000 if (shorten || common || short_compare)
3bf6bfcc
JJ
11001 {
11002 result_type = c_common_type (type0, type1);
0a0b3574
MM
11003 do_warn_double_promotion (result_type, type0, type1,
11004 "implicit conversion from %qT to %qT "
11005 "to match other operand of binary "
11006 "expression",
11007 location);
3bf6bfcc
JJ
11008 if (result_type == error_mark_node)
11009 return error_mark_node;
11010 }
400fbf9f 11011
2ca862e9
JM
11012 if (first_complex != second_complex
11013 && (code == PLUS_EXPR
11014 || code == MINUS_EXPR
11015 || code == MULT_EXPR
11016 || (code == TRUNC_DIV_EXPR && first_complex))
11017 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11018 && flag_signed_zeros)
11019 {
11020 /* An operation on mixed real/complex operands must be
11021 handled specially, but the language-independent code can
11022 more easily optimize the plain complex arithmetic if
11023 -fno-signed-zeros. */
11024 tree real_type = TREE_TYPE (result_type);
11025 tree real, imag;
11026 if (type0 != orig_type0 || type1 != orig_type1)
11027 {
11028 gcc_assert (may_need_excess_precision && common);
2d2e923f 11029 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11030 }
11031 if (first_complex)
11032 {
11033 if (TREE_TYPE (op0) != result_type)
68fca595 11034 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11035 if (TREE_TYPE (op1) != real_type)
68fca595 11036 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11037 }
11038 else
11039 {
11040 if (TREE_TYPE (op0) != real_type)
68fca595 11041 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11042 if (TREE_TYPE (op1) != result_type)
68fca595 11043 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11044 }
11045 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11046 return error_mark_node;
11047 if (first_complex)
11048 {
11049 op0 = c_save_expr (op0);
11050 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11051 op0, 1);
11052 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11053 op0, 1);
11054 switch (code)
11055 {
11056 case MULT_EXPR:
11057 case TRUNC_DIV_EXPR:
c4603e7c 11058 op1 = c_save_expr (op1);
2ca862e9
JM
11059 imag = build2 (resultcode, real_type, imag, op1);
11060 /* Fall through. */
11061 case PLUS_EXPR:
11062 case MINUS_EXPR:
11063 real = build2 (resultcode, real_type, real, op1);
11064 break;
11065 default:
11066 gcc_unreachable();
11067 }
11068 }
11069 else
11070 {
11071 op1 = c_save_expr (op1);
11072 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11073 op1, 1);
11074 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11075 op1, 1);
11076 switch (code)
11077 {
11078 case MULT_EXPR:
c4603e7c 11079 op0 = c_save_expr (op0);
2ca862e9
JM
11080 imag = build2 (resultcode, real_type, op0, imag);
11081 /* Fall through. */
11082 case PLUS_EXPR:
11083 real = build2 (resultcode, real_type, op0, real);
11084 break;
11085 case MINUS_EXPR:
11086 real = build2 (resultcode, real_type, op0, real);
11087 imag = build1 (NEGATE_EXPR, real_type, imag);
11088 break;
11089 default:
11090 gcc_unreachable();
11091 }
11092 }
11093 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11094 goto return_build_binary_op;
11095 }
11096
3e4093b6
RS
11097 /* For certain operations (which identify themselves by shorten != 0)
11098 if both args were extended from the same smaller type,
11099 do the arithmetic in that type and then extend.
400fbf9f 11100
3e4093b6
RS
11101 shorten !=0 and !=1 indicates a bitwise operation.
11102 For them, this optimization is safe only if
11103 both args are zero-extended or both are sign-extended.
11104 Otherwise, we might change the result.
11105 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11106 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11107
3e4093b6
RS
11108 if (shorten && none_complex)
11109 {
3e4093b6 11110 final_type = result_type;
b8698a0f 11111 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11112 shorten == -1);
3e4093b6 11113 }
88a3dbc1 11114
3e4093b6 11115 /* Shifts can be shortened if shifting right. */
2f6e4e97 11116
3e4093b6
RS
11117 if (short_shift)
11118 {
11119 int unsigned_arg;
11120 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11121
3e4093b6 11122 final_type = result_type;
abe80e6d 11123
3e4093b6 11124 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11125 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11126
3e4093b6 11127 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11128 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11129 /* We can shorten only if the shift count is less than the
11130 number of bits in the smaller type size. */
11131 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11132 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11133 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11134 {
11135 /* Do an unsigned shift if the operand was zero-extended. */
11136 result_type
11137 = c_common_signed_or_unsigned_type (unsigned_arg,
11138 TREE_TYPE (arg0));
11139 /* Convert value-to-be-shifted to that type. */
11140 if (TREE_TYPE (op0) != result_type)
11141 op0 = convert (result_type, op0);
11142 converted = 1;
abe80e6d 11143 }
88a3dbc1
RK
11144 }
11145
3e4093b6
RS
11146 /* Comparison operations are shortened too but differently.
11147 They identify themselves by setting short_compare = 1. */
56cb9733 11148
3e4093b6
RS
11149 if (short_compare)
11150 {
11151 /* Don't write &op0, etc., because that would prevent op0
11152 from being kept in a register.
11153 Instead, make copies of the our local variables and
11154 pass the copies by reference, then copy them back afterward. */
11155 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11156 enum tree_code xresultcode = resultcode;
11157 tree val
393e8e8b
MP
11158 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11159 &xresultcode);
8f17b5c5 11160
3e4093b6 11161 if (val != 0)
c9f9eb5d
AH
11162 {
11163 ret = val;
11164 goto return_build_binary_op;
11165 }
8f17b5c5 11166
3e4093b6
RS
11167 op0 = xop0, op1 = xop1;
11168 converted = 1;
11169 resultcode = xresultcode;
8f17b5c5 11170
7d882b83 11171 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11172 {
11173 bool op0_maybe_const = true;
11174 bool op1_maybe_const = true;
11175 tree orig_op0_folded, orig_op1_folded;
11176
11177 if (in_late_binary_op)
11178 {
11179 orig_op0_folded = orig_op0;
11180 orig_op1_folded = orig_op1;
11181 }
11182 else
11183 {
11184 /* Fold for the sake of possible warnings, as in
11185 build_conditional_expr. This requires the
11186 "original" values to be folded, not just op0 and
11187 op1. */
f5178456 11188 c_inhibit_evaluation_warnings++;
928c19bb
JM
11189 op0 = c_fully_fold (op0, require_constant_value,
11190 &op0_maybe_const);
11191 op1 = c_fully_fold (op1, require_constant_value,
11192 &op1_maybe_const);
f5178456 11193 c_inhibit_evaluation_warnings--;
928c19bb
JM
11194 orig_op0_folded = c_fully_fold (orig_op0,
11195 require_constant_value,
11196 NULL);
11197 orig_op1_folded = c_fully_fold (orig_op1,
11198 require_constant_value,
11199 NULL);
11200 }
11201
11202 if (warn_sign_compare)
11203 warn_for_sign_compare (location, orig_op0_folded,
11204 orig_op1_folded, op0, op1,
11205 result_type, resultcode);
5c2f94b4 11206 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11207 {
11208 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11209 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11210 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11211 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11212 }
3e4093b6 11213 }
2ad1815d 11214 }
64094f6a 11215 }
64094f6a 11216
3e4093b6
RS
11217 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11218 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11219 Then the expression will be built.
11220 It will be given type FINAL_TYPE if that is nonzero;
11221 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11222
3e4093b6
RS
11223 if (!result_type)
11224 {
ba47d38d 11225 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11226 return error_mark_node;
11227 }
400fbf9f 11228
3e4093b6 11229 if (build_type == NULL_TREE)
8ce94e44
JM
11230 {
11231 build_type = result_type;
180f8dbb
JM
11232 if ((type0 != orig_type0 || type1 != orig_type1)
11233 && !boolean_op)
8ce94e44
JM
11234 {
11235 gcc_assert (may_need_excess_precision && common);
2d2e923f 11236 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11237 }
11238 }
400fbf9f 11239
2d2e923f
MLI
11240 if (!converted)
11241 {
68fca595
MP
11242 op0 = ep_convert_and_check (location, result_type, op0,
11243 semantic_result_type);
11244 op1 = ep_convert_and_check (location, result_type, op1,
11245 semantic_result_type);
2d2e923f
MLI
11246
11247 /* This can happen if one operand has a vector type, and the other
11248 has a different type. */
11249 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11250 return error_mark_node;
11251 }
11252
f8ed5150
MP
11253 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11254 | SANITIZE_FLOAT_DIVIDE))
802ac282 11255 && do_ubsan_in_current_function ()
de5a5fa1
MP
11256 && (doing_div_or_mod || doing_shift))
11257 {
11258 /* OP0 and/or OP1 might have side-effects. */
11259 op0 = c_save_expr (op0);
11260 op1 = c_save_expr (op1);
11261 op0 = c_fully_fold (op0, false, NULL);
11262 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11263 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11264 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11265 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11266 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11267 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11268 }
11269
c9f9eb5d 11270 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11271 if (int_const_or_overflow)
11272 ret = (require_constant_value
db3927fb
AH
11273 ? fold_build2_initializer_loc (location, resultcode, build_type,
11274 op0, op1)
11275 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11276 else
11277 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11278 if (final_type != 0)
11279 ret = convert (final_type, ret);
11280
11281 return_build_binary_op:
11282 gcc_assert (ret != error_mark_node);
928c19bb
JM
11283 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11284 ret = (int_operands
11285 ? note_integer_operands (ret)
11286 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11287 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11288 && !in_late_binary_op)
11289 ret = note_integer_operands (ret);
2d2e923f
MLI
11290 if (semantic_result_type)
11291 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11292 protected_set_expr_location (ret, location);
de5a5fa1 11293
a24d975c 11294 if (instrument_expr != NULL)
de5a5fa1
MP
11295 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11296 instrument_expr, ret);
11297
c9f9eb5d 11298 return ret;
400fbf9f 11299}
85498824
JM
11300
11301
11302/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11303 purpose. LOCATION is the source location for the expression. */
85498824
JM
11304
11305tree
ba47d38d 11306c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11307{
928c19bb
JM
11308 bool int_const, int_operands;
11309
85498824
JM
11310 switch (TREE_CODE (TREE_TYPE (expr)))
11311 {
11312 case ARRAY_TYPE:
ba47d38d 11313 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11314 return error_mark_node;
11315
11316 case RECORD_TYPE:
ba47d38d 11317 error_at (location, "used struct type value where scalar is required");
85498824
JM
11318 return error_mark_node;
11319
11320 case UNION_TYPE:
ba47d38d 11321 error_at (location, "used union type value where scalar is required");
85498824
JM
11322 return error_mark_node;
11323
04af8788
NP
11324 case VOID_TYPE:
11325 error_at (location, "void value not ignored as it ought to be");
11326 return error_mark_node;
11327
46bdb9cf
JM
11328 case FUNCTION_TYPE:
11329 gcc_unreachable ();
11330
d246ab4f
AS
11331 case VECTOR_TYPE:
11332 error_at (location, "used vector type where scalar is required");
11333 return error_mark_node;
11334
85498824
JM
11335 default:
11336 break;
11337 }
11338
928c19bb
JM
11339 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11340 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11341 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11342 {
11343 expr = remove_c_maybe_const_expr (expr);
11344 expr = build2 (NE_EXPR, integer_type_node, expr,
11345 convert (TREE_TYPE (expr), integer_zero_node));
11346 expr = note_integer_operands (expr);
11347 }
11348 else
11349 /* ??? Should we also give an error for vectors rather than leaving
11350 those to give errors later? */
11351 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11352
11353 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11354 {
11355 if (TREE_OVERFLOW (expr))
11356 return expr;
11357 else
11358 return note_integer_operands (expr);
11359 }
11360 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11361 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11362 return expr;
85498824 11363}
73f397d4
JM
11364\f
11365
11366/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11367 required. */
11368
11369tree
51eed280 11370c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11371{
11372 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11373 {
11374 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11375 /* Executing a compound literal inside a function reinitializes
11376 it. */
11377 if (!TREE_STATIC (decl))
11378 *se = true;
11379 return decl;
11380 }
11381 else
11382 return expr;
11383}
953ff289 11384\f
41dbbb37
TS
11385/* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11386 statement. LOC is the location of the OACC_PARALLEL. */
11387
11388tree
11389c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11390{
11391 tree stmt;
11392
11393 block = c_end_compound_stmt (loc, block, true);
11394
11395 stmt = make_node (OACC_PARALLEL);
11396 TREE_TYPE (stmt) = void_type_node;
11397 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11398 OACC_PARALLEL_BODY (stmt) = block;
11399 SET_EXPR_LOCATION (stmt, loc);
11400
11401 return add_stmt (stmt);
11402}
11403
11404/* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11405 statement. LOC is the location of the OACC_KERNELS. */
11406
11407tree
11408c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11409{
11410 tree stmt;
11411
11412 block = c_end_compound_stmt (loc, block, true);
11413
11414 stmt = make_node (OACC_KERNELS);
11415 TREE_TYPE (stmt) = void_type_node;
11416 OACC_KERNELS_CLAUSES (stmt) = clauses;
11417 OACC_KERNELS_BODY (stmt) = block;
11418 SET_EXPR_LOCATION (stmt, loc);
11419
11420 return add_stmt (stmt);
11421}
11422
11423/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11424 statement. LOC is the location of the OACC_DATA. */
11425
11426tree
11427c_finish_oacc_data (location_t loc, tree clauses, tree block)
11428{
11429 tree stmt;
11430
11431 block = c_end_compound_stmt (loc, block, true);
11432
11433 stmt = make_node (OACC_DATA);
11434 TREE_TYPE (stmt) = void_type_node;
11435 OACC_DATA_CLAUSES (stmt) = clauses;
11436 OACC_DATA_BODY (stmt) = block;
11437 SET_EXPR_LOCATION (stmt, loc);
11438
11439 return add_stmt (stmt);
11440}
11441
c0220ea4 11442/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11443
11444tree
11445c_begin_omp_parallel (void)
11446{
11447 tree block;
11448
11449 keep_next_level ();
11450 block = c_begin_compound_stmt (true);
11451
11452 return block;
11453}
11454
c2255bc4
AH
11455/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11456 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11457
953ff289 11458tree
c2255bc4 11459c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11460{
11461 tree stmt;
11462
c2255bc4 11463 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11464
11465 stmt = make_node (OMP_PARALLEL);
11466 TREE_TYPE (stmt) = void_type_node;
11467 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11468 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11469 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11470
11471 return add_stmt (stmt);
11472}
11473
a68ab351
JJ
11474/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11475
11476tree
11477c_begin_omp_task (void)
11478{
11479 tree block;
11480
11481 keep_next_level ();
11482 block = c_begin_compound_stmt (true);
11483
11484 return block;
11485}
11486
c2255bc4
AH
11487/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11488 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11489
11490tree
c2255bc4 11491c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11492{
11493 tree stmt;
11494
c2255bc4 11495 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11496
11497 stmt = make_node (OMP_TASK);
11498 TREE_TYPE (stmt) = void_type_node;
11499 OMP_TASK_CLAUSES (stmt) = clauses;
11500 OMP_TASK_BODY (stmt) = block;
c2255bc4 11501 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11502
11503 return add_stmt (stmt);
11504}
11505
acf0174b
JJ
11506/* Generate GOMP_cancel call for #pragma omp cancel. */
11507
11508void
11509c_finish_omp_cancel (location_t loc, tree clauses)
11510{
11511 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11512 int mask = 0;
11513 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11514 mask = 1;
11515 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11516 mask = 2;
11517 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11518 mask = 4;
11519 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11520 mask = 8;
11521 else
11522 {
11523 error_at (loc, "%<#pragma omp cancel must specify one of "
11524 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11525 "clauses");
11526 return;
11527 }
11528 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11529 if (ifc != NULL_TREE)
11530 {
11531 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11532 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11533 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11534 build_zero_cst (type));
11535 }
11536 else
11537 ifc = boolean_true_node;
11538 tree stmt = build_call_expr_loc (loc, fn, 2,
11539 build_int_cst (integer_type_node, mask),
11540 ifc);
11541 add_stmt (stmt);
11542}
11543
11544/* Generate GOMP_cancellation_point call for
11545 #pragma omp cancellation point. */
11546
11547void
11548c_finish_omp_cancellation_point (location_t loc, tree clauses)
11549{
11550 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11551 int mask = 0;
11552 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11553 mask = 1;
11554 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11555 mask = 2;
11556 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11557 mask = 4;
11558 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11559 mask = 8;
11560 else
11561 {
11562 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11563 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11564 "clauses");
11565 return;
11566 }
11567 tree stmt = build_call_expr_loc (loc, fn, 1,
11568 build_int_cst (integer_type_node, mask));
11569 add_stmt (stmt);
11570}
11571
11572/* Helper function for handle_omp_array_sections. Called recursively
11573 to handle multiple array-section-subscripts. C is the clause,
11574 T current expression (initially OMP_CLAUSE_DECL), which is either
11575 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11576 expression if specified, TREE_VALUE length expression if specified,
11577 TREE_CHAIN is what it has been specified after, or some decl.
11578 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11579 set to true if any of the array-section-subscript could have length
11580 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11581 first array-section-subscript which is known not to have length
11582 of one. Given say:
11583 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11584 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11585 all are or may have length of 1, array-section-subscript [:2] is the
11586 first one knonwn not to have length 1. For array-section-subscript
11587 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11588 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11589 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11590 case though, as some lengths could be zero. */
11591
11592static tree
11593handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11594 bool &maybe_zero_len, unsigned int &first_non_one)
11595{
11596 tree ret, low_bound, length, type;
11597 if (TREE_CODE (t) != TREE_LIST)
11598 {
7a0ca710 11599 if (error_operand_p (t))
acf0174b
JJ
11600 return error_mark_node;
11601 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11602 {
11603 if (DECL_P (t))
11604 error_at (OMP_CLAUSE_LOCATION (c),
11605 "%qD is not a variable in %qs clause", t,
11606 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11607 else
11608 error_at (OMP_CLAUSE_LOCATION (c),
11609 "%qE is not a variable in %qs clause", t,
11610 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11611 return error_mark_node;
11612 }
11613 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11614 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11615 {
11616 error_at (OMP_CLAUSE_LOCATION (c),
11617 "%qD is threadprivate variable in %qs clause", t,
11618 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11619 return error_mark_node;
11620 }
11621 return t;
11622 }
11623
11624 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11625 maybe_zero_len, first_non_one);
11626 if (ret == error_mark_node || ret == NULL_TREE)
11627 return ret;
11628
11629 type = TREE_TYPE (ret);
11630 low_bound = TREE_PURPOSE (t);
11631 length = TREE_VALUE (t);
11632
11633 if (low_bound == error_mark_node || length == error_mark_node)
11634 return error_mark_node;
11635
11636 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11637 {
11638 error_at (OMP_CLAUSE_LOCATION (c),
11639 "low bound %qE of array section does not have integral type",
11640 low_bound);
11641 return error_mark_node;
11642 }
11643 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11644 {
11645 error_at (OMP_CLAUSE_LOCATION (c),
11646 "length %qE of array section does not have integral type",
11647 length);
11648 return error_mark_node;
11649 }
11650 if (low_bound
11651 && TREE_CODE (low_bound) == INTEGER_CST
11652 && TYPE_PRECISION (TREE_TYPE (low_bound))
11653 > TYPE_PRECISION (sizetype))
11654 low_bound = fold_convert (sizetype, low_bound);
11655 if (length
11656 && TREE_CODE (length) == INTEGER_CST
11657 && TYPE_PRECISION (TREE_TYPE (length))
11658 > TYPE_PRECISION (sizetype))
11659 length = fold_convert (sizetype, length);
11660 if (low_bound == NULL_TREE)
11661 low_bound = integer_zero_node;
11662
11663 if (length != NULL_TREE)
11664 {
11665 if (!integer_nonzerop (length))
11666 maybe_zero_len = true;
11667 if (first_non_one == types.length ()
11668 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11669 first_non_one++;
11670 }
11671 if (TREE_CODE (type) == ARRAY_TYPE)
11672 {
11673 if (length == NULL_TREE
11674 && (TYPE_DOMAIN (type) == NULL_TREE
11675 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11676 {
11677 error_at (OMP_CLAUSE_LOCATION (c),
11678 "for unknown bound array type length expression must "
11679 "be specified");
11680 return error_mark_node;
11681 }
11682 if (TREE_CODE (low_bound) == INTEGER_CST
11683 && tree_int_cst_sgn (low_bound) == -1)
11684 {
11685 error_at (OMP_CLAUSE_LOCATION (c),
11686 "negative low bound in array section in %qs clause",
11687 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11688 return error_mark_node;
11689 }
11690 if (length != NULL_TREE
11691 && TREE_CODE (length) == INTEGER_CST
11692 && tree_int_cst_sgn (length) == -1)
11693 {
11694 error_at (OMP_CLAUSE_LOCATION (c),
11695 "negative length in array section in %qs clause",
11696 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11697 return error_mark_node;
11698 }
11699 if (TYPE_DOMAIN (type)
11700 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11701 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11702 == INTEGER_CST)
11703 {
11704 tree size = size_binop (PLUS_EXPR,
11705 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11706 size_one_node);
11707 if (TREE_CODE (low_bound) == INTEGER_CST)
11708 {
11709 if (tree_int_cst_lt (size, low_bound))
11710 {
11711 error_at (OMP_CLAUSE_LOCATION (c),
11712 "low bound %qE above array section size "
11713 "in %qs clause", low_bound,
11714 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11715 return error_mark_node;
11716 }
11717 if (tree_int_cst_equal (size, low_bound))
11718 maybe_zero_len = true;
11719 else if (length == NULL_TREE
11720 && first_non_one == types.length ()
11721 && tree_int_cst_equal
11722 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11723 low_bound))
11724 first_non_one++;
11725 }
11726 else if (length == NULL_TREE)
11727 {
11728 maybe_zero_len = true;
11729 if (first_non_one == types.length ())
11730 first_non_one++;
11731 }
11732 if (length && TREE_CODE (length) == INTEGER_CST)
11733 {
11734 if (tree_int_cst_lt (size, length))
11735 {
11736 error_at (OMP_CLAUSE_LOCATION (c),
11737 "length %qE above array section size "
11738 "in %qs clause", length,
11739 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11740 return error_mark_node;
11741 }
11742 if (TREE_CODE (low_bound) == INTEGER_CST)
11743 {
11744 tree lbpluslen
11745 = size_binop (PLUS_EXPR,
11746 fold_convert (sizetype, low_bound),
11747 fold_convert (sizetype, length));
11748 if (TREE_CODE (lbpluslen) == INTEGER_CST
11749 && tree_int_cst_lt (size, lbpluslen))
11750 {
11751 error_at (OMP_CLAUSE_LOCATION (c),
11752 "high bound %qE above array section size "
11753 "in %qs clause", lbpluslen,
11754 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11755 return error_mark_node;
11756 }
11757 }
11758 }
11759 }
11760 else if (length == NULL_TREE)
11761 {
11762 maybe_zero_len = true;
11763 if (first_non_one == types.length ())
11764 first_non_one++;
11765 }
11766
11767 /* For [lb:] we will need to evaluate lb more than once. */
11768 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11769 {
11770 tree lb = c_save_expr (low_bound);
11771 if (lb != low_bound)
11772 {
11773 TREE_PURPOSE (t) = lb;
11774 low_bound = lb;
11775 }
11776 }
11777 }
11778 else if (TREE_CODE (type) == POINTER_TYPE)
11779 {
11780 if (length == NULL_TREE)
11781 {
11782 error_at (OMP_CLAUSE_LOCATION (c),
11783 "for pointer type length expression must be specified");
11784 return error_mark_node;
11785 }
11786 /* If there is a pointer type anywhere but in the very first
11787 array-section-subscript, the array section can't be contiguous. */
11788 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11789 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11790 {
11791 error_at (OMP_CLAUSE_LOCATION (c),
11792 "array section is not contiguous in %qs clause",
11793 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11794 return error_mark_node;
11795 }
11796 }
11797 else
11798 {
11799 error_at (OMP_CLAUSE_LOCATION (c),
11800 "%qE does not have pointer or array type", ret);
11801 return error_mark_node;
11802 }
11803 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11804 types.safe_push (TREE_TYPE (ret));
11805 /* We will need to evaluate lb more than once. */
11806 tree lb = c_save_expr (low_bound);
11807 if (lb != low_bound)
11808 {
11809 TREE_PURPOSE (t) = lb;
11810 low_bound = lb;
11811 }
11812 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11813 return ret;
11814}
11815
11816/* Handle array sections for clause C. */
11817
11818static bool
11819handle_omp_array_sections (tree c)
11820{
11821 bool maybe_zero_len = false;
11822 unsigned int first_non_one = 0;
11823 vec<tree> types = vNULL;
11824 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11825 maybe_zero_len, first_non_one);
11826 if (first == error_mark_node)
11827 {
11828 types.release ();
11829 return true;
11830 }
11831 if (first == NULL_TREE)
11832 {
11833 types.release ();
11834 return false;
11835 }
11836 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11837 {
11838 tree t = OMP_CLAUSE_DECL (c);
11839 tree tem = NULL_TREE;
11840 types.release ();
11841 /* Need to evaluate side effects in the length expressions
11842 if any. */
11843 while (TREE_CODE (t) == TREE_LIST)
11844 {
11845 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11846 {
11847 if (tem == NULL_TREE)
11848 tem = TREE_VALUE (t);
11849 else
11850 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11851 TREE_VALUE (t), tem);
11852 }
11853 t = TREE_CHAIN (t);
11854 }
11855 if (tem)
11856 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11857 first = c_fully_fold (first, false, NULL);
11858 OMP_CLAUSE_DECL (c) = first;
11859 }
11860 else
11861 {
11862 unsigned int num = types.length (), i;
11863 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11864 tree condition = NULL_TREE;
11865
11866 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11867 maybe_zero_len = true;
11868
11869 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11870 t = TREE_CHAIN (t))
11871 {
11872 tree low_bound = TREE_PURPOSE (t);
11873 tree length = TREE_VALUE (t);
11874
11875 i--;
11876 if (low_bound
11877 && TREE_CODE (low_bound) == INTEGER_CST
11878 && TYPE_PRECISION (TREE_TYPE (low_bound))
11879 > TYPE_PRECISION (sizetype))
11880 low_bound = fold_convert (sizetype, low_bound);
11881 if (length
11882 && TREE_CODE (length) == INTEGER_CST
11883 && TYPE_PRECISION (TREE_TYPE (length))
11884 > TYPE_PRECISION (sizetype))
11885 length = fold_convert (sizetype, length);
11886 if (low_bound == NULL_TREE)
11887 low_bound = integer_zero_node;
11888 if (!maybe_zero_len && i > first_non_one)
11889 {
11890 if (integer_nonzerop (low_bound))
11891 goto do_warn_noncontiguous;
11892 if (length != NULL_TREE
11893 && TREE_CODE (length) == INTEGER_CST
11894 && TYPE_DOMAIN (types[i])
11895 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11896 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11897 == INTEGER_CST)
11898 {
11899 tree size;
11900 size = size_binop (PLUS_EXPR,
11901 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11902 size_one_node);
11903 if (!tree_int_cst_equal (length, size))
11904 {
11905 do_warn_noncontiguous:
11906 error_at (OMP_CLAUSE_LOCATION (c),
11907 "array section is not contiguous in %qs "
11908 "clause",
11909 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11910 types.release ();
11911 return true;
11912 }
11913 }
11914 if (length != NULL_TREE
11915 && TREE_SIDE_EFFECTS (length))
11916 {
11917 if (side_effects == NULL_TREE)
11918 side_effects = length;
11919 else
11920 side_effects = build2 (COMPOUND_EXPR,
11921 TREE_TYPE (side_effects),
11922 length, side_effects);
11923 }
11924 }
11925 else
11926 {
11927 tree l;
11928
11929 if (i > first_non_one && length && integer_nonzerop (length))
11930 continue;
11931 if (length)
11932 l = fold_convert (sizetype, length);
11933 else
11934 {
11935 l = size_binop (PLUS_EXPR,
11936 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11937 size_one_node);
11938 l = size_binop (MINUS_EXPR, l,
11939 fold_convert (sizetype, low_bound));
11940 }
11941 if (i > first_non_one)
11942 {
11943 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11944 size_zero_node);
11945 if (condition == NULL_TREE)
11946 condition = l;
11947 else
11948 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11949 l, condition);
11950 }
11951 else if (size == NULL_TREE)
11952 {
11953 size = size_in_bytes (TREE_TYPE (types[i]));
11954 size = size_binop (MULT_EXPR, size, l);
11955 if (condition)
11956 size = fold_build3 (COND_EXPR, sizetype, condition,
11957 size, size_zero_node);
11958 }
11959 else
11960 size = size_binop (MULT_EXPR, size, l);
11961 }
11962 }
11963 types.release ();
11964 if (side_effects)
11965 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11966 first = c_fully_fold (first, false, NULL);
11967 OMP_CLAUSE_DECL (c) = first;
11968 if (size)
11969 size = c_fully_fold (size, false, NULL);
11970 OMP_CLAUSE_SIZE (c) = size;
11971 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11972 return false;
41dbbb37 11973 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
acf0174b 11974 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
41dbbb37 11975 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
acf0174b
JJ
11976 if (!c_mark_addressable (t))
11977 return false;
11978 OMP_CLAUSE_DECL (c2) = t;
11979 t = build_fold_addr_expr (first);
11980 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11981 tree ptr = OMP_CLAUSE_DECL (c2);
11982 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11983 ptr = build_fold_addr_expr (ptr);
11984 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11985 ptrdiff_type_node, t,
11986 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11987 ptrdiff_type_node, ptr));
11988 t = c_fully_fold (t, false, NULL);
11989 OMP_CLAUSE_SIZE (c2) = t;
11990 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11991 OMP_CLAUSE_CHAIN (c) = c2;
11992 }
11993 return false;
11994}
11995
11996/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11997 an inline call. But, remap
11998 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11999 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12000
12001static tree
12002c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12003 tree decl, tree placeholder)
12004{
12005 copy_body_data id;
b787e7a2 12006 hash_map<tree, tree> decl_map;
acf0174b 12007
b787e7a2
TS
12008 decl_map.put (omp_decl1, placeholder);
12009 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12010 memset (&id, 0, sizeof (id));
12011 id.src_fn = DECL_CONTEXT (omp_decl1);
12012 id.dst_fn = current_function_decl;
12013 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12014 id.decl_map = &decl_map;
acf0174b
JJ
12015
12016 id.copy_decl = copy_decl_no_change;
12017 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12018 id.transform_new_cfg = true;
12019 id.transform_return_to_modify = false;
12020 id.transform_lang_insert_block = NULL;
12021 id.eh_lp_nr = 0;
12022 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12023 return stmt;
12024}
12025
12026/* Helper function of c_finish_omp_clauses, called via walk_tree.
12027 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12028
12029static tree
12030c_find_omp_placeholder_r (tree *tp, int *, void *data)
12031{
12032 if (*tp == (tree) data)
12033 return *tp;
12034 return NULL_TREE;
12035}
12036
41dbbb37 12037/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12038 Remove any elements from the list that are invalid. */
12039
12040tree
12041c_finish_omp_clauses (tree clauses)
12042{
12043 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 12044 bitmap_head aligned_head;
f3316c6d 12045 tree c, t, *pc;
acf0174b
JJ
12046 bool branch_seen = false;
12047 bool copyprivate_seen = false;
12048 tree *nowait_clause = NULL;
953ff289
DN
12049
12050 bitmap_obstack_initialize (NULL);
12051 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12052 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12053 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12054 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
12055
12056 for (pc = &clauses, c = clauses; c ; c = *pc)
12057 {
12058 bool remove = false;
12059 bool need_complete = false;
12060 bool need_implicitly_determined = false;
12061
aaf46ef9 12062 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12063 {
12064 case OMP_CLAUSE_SHARED:
953ff289
DN
12065 need_implicitly_determined = true;
12066 goto check_dup_generic;
12067
12068 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12069 need_complete = true;
12070 need_implicitly_determined = true;
12071 goto check_dup_generic;
12072
12073 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12074 need_implicitly_determined = true;
12075 t = OMP_CLAUSE_DECL (c);
acf0174b 12076 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
12077 && (FLOAT_TYPE_P (TREE_TYPE (t))
12078 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
12079 {
12080 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12081 const char *r_name = NULL;
12082
12083 switch (r_code)
12084 {
12085 case PLUS_EXPR:
12086 case MULT_EXPR:
12087 case MINUS_EXPR:
652fea39 12088 break;
20906c66 12089 case MIN_EXPR:
652fea39
JJ
12090 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12091 r_name = "min";
12092 break;
20906c66 12093 case MAX_EXPR:
652fea39
JJ
12094 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12095 r_name = "max";
953ff289
DN
12096 break;
12097 case BIT_AND_EXPR:
12098 r_name = "&";
12099 break;
12100 case BIT_XOR_EXPR:
12101 r_name = "^";
12102 break;
12103 case BIT_IOR_EXPR:
12104 r_name = "|";
12105 break;
12106 case TRUTH_ANDIF_EXPR:
652fea39
JJ
12107 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12108 r_name = "&&";
953ff289
DN
12109 break;
12110 case TRUTH_ORIF_EXPR:
652fea39
JJ
12111 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12112 r_name = "||";
953ff289
DN
12113 break;
12114 default:
12115 gcc_unreachable ();
12116 }
12117 if (r_name)
12118 {
c2255bc4
AH
12119 error_at (OMP_CLAUSE_LOCATION (c),
12120 "%qE has invalid type for %<reduction(%s)%>",
12121 t, r_name);
953ff289 12122 remove = true;
ee1d5a02 12123 break;
953ff289
DN
12124 }
12125 }
acf0174b
JJ
12126 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12127 {
12128 error_at (OMP_CLAUSE_LOCATION (c),
12129 "user defined reduction not found for %qD", t);
12130 remove = true;
ee1d5a02 12131 break;
acf0174b
JJ
12132 }
12133 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12134 {
12135 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12136 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12137 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12138 VAR_DECL, NULL_TREE, type);
12139 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12140 DECL_ARTIFICIAL (placeholder) = 1;
12141 DECL_IGNORED_P (placeholder) = 1;
12142 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12143 c_mark_addressable (placeholder);
12144 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12145 c_mark_addressable (OMP_CLAUSE_DECL (c));
12146 OMP_CLAUSE_REDUCTION_MERGE (c)
12147 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12148 TREE_VEC_ELT (list, 0),
12149 TREE_VEC_ELT (list, 1),
12150 OMP_CLAUSE_DECL (c), placeholder);
12151 OMP_CLAUSE_REDUCTION_MERGE (c)
12152 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12153 void_type_node, NULL_TREE,
12154 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12155 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12156 if (TREE_VEC_LENGTH (list) == 6)
12157 {
12158 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12159 c_mark_addressable (OMP_CLAUSE_DECL (c));
12160 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12161 c_mark_addressable (placeholder);
12162 tree init = TREE_VEC_ELT (list, 5);
12163 if (init == error_mark_node)
12164 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12165 OMP_CLAUSE_REDUCTION_INIT (c)
12166 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12167 TREE_VEC_ELT (list, 3),
12168 OMP_CLAUSE_DECL (c), placeholder);
12169 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12170 OMP_CLAUSE_REDUCTION_INIT (c)
12171 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12172 OMP_CLAUSE_REDUCTION_INIT (c));
12173 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12174 c_find_omp_placeholder_r,
12175 placeholder, NULL))
12176 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12177 }
12178 else
12179 {
12180 tree init;
12181 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12182 init = build_constructor (TREE_TYPE (t), NULL);
12183 else
12184 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12185 OMP_CLAUSE_REDUCTION_INIT (c)
12186 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12187 }
12188 OMP_CLAUSE_REDUCTION_INIT (c)
12189 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12190 void_type_node, NULL_TREE,
12191 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12192 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12193 }
953ff289
DN
12194 goto check_dup_generic;
12195
12196 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12197 copyprivate_seen = true;
12198 if (nowait_clause)
12199 {
12200 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12201 "%<nowait%> clause must not be used together "
12202 "with %<copyprivate%>");
12203 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12204 nowait_clause = NULL;
12205 }
953ff289
DN
12206 goto check_dup_generic;
12207
12208 case OMP_CLAUSE_COPYIN:
953ff289
DN
12209 t = OMP_CLAUSE_DECL (c);
12210 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12211 {
c2255bc4
AH
12212 error_at (OMP_CLAUSE_LOCATION (c),
12213 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12214 remove = true;
ee1d5a02 12215 break;
953ff289
DN
12216 }
12217 goto check_dup_generic;
12218
acf0174b
JJ
12219 case OMP_CLAUSE_LINEAR:
12220 t = OMP_CLAUSE_DECL (c);
12221 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12222 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12223 {
12224 error_at (OMP_CLAUSE_LOCATION (c),
12225 "linear clause applied to non-integral non-pointer "
12226 "variable with type %qT", TREE_TYPE (t));
12227 remove = true;
12228 break;
12229 }
12230 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12231 {
12232 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12233 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12234 OMP_CLAUSE_DECL (c), s);
12235 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12236 sizetype, s, OMP_CLAUSE_DECL (c));
12237 if (s == error_mark_node)
12238 s = size_one_node;
12239 OMP_CLAUSE_LINEAR_STEP (c) = s;
12240 }
da6f124d
JJ
12241 else
12242 OMP_CLAUSE_LINEAR_STEP (c)
12243 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12244 goto check_dup_generic;
12245
953ff289
DN
12246 check_dup_generic:
12247 t = OMP_CLAUSE_DECL (c);
12248 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12249 {
c2255bc4 12250 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12251 "%qE is not a variable in clause %qs", t,
12252 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12253 remove = true;
12254 }
12255 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12256 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12257 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12258 {
c2255bc4
AH
12259 error_at (OMP_CLAUSE_LOCATION (c),
12260 "%qE appears more than once in data clauses", t);
953ff289
DN
12261 remove = true;
12262 }
12263 else
12264 bitmap_set_bit (&generic_head, DECL_UID (t));
12265 break;
12266
12267 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12268 t = OMP_CLAUSE_DECL (c);
12269 need_complete = true;
12270 need_implicitly_determined = true;
12271 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12272 {
c2255bc4
AH
12273 error_at (OMP_CLAUSE_LOCATION (c),
12274 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12275 remove = true;
12276 }
12277 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12278 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12279 {
c2255bc4
AH
12280 error_at (OMP_CLAUSE_LOCATION (c),
12281 "%qE appears more than once in data clauses", t);
953ff289
DN
12282 remove = true;
12283 }
12284 else
12285 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12286 break;
12287
12288 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12289 t = OMP_CLAUSE_DECL (c);
12290 need_complete = true;
12291 need_implicitly_determined = true;
12292 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12293 {
c2255bc4
AH
12294 error_at (OMP_CLAUSE_LOCATION (c),
12295 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12296 remove = true;
12297 }
12298 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12299 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12300 {
c2255bc4
AH
12301 error_at (OMP_CLAUSE_LOCATION (c),
12302 "%qE appears more than once in data clauses", t);
953ff289
DN
12303 remove = true;
12304 }
12305 else
12306 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12307 break;
12308
acf0174b
JJ
12309 case OMP_CLAUSE_ALIGNED:
12310 t = OMP_CLAUSE_DECL (c);
12311 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12312 {
12313 error_at (OMP_CLAUSE_LOCATION (c),
12314 "%qE is not a variable in %<aligned%> clause", t);
12315 remove = true;
12316 }
5a9785fb
JJ
12317 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12318 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12319 {
12320 error_at (OMP_CLAUSE_LOCATION (c),
12321 "%qE in %<aligned%> clause is neither a pointer nor "
12322 "an array", t);
12323 remove = true;
12324 }
acf0174b
JJ
12325 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12326 {
12327 error_at (OMP_CLAUSE_LOCATION (c),
12328 "%qE appears more than once in %<aligned%> clauses",
12329 t);
12330 remove = true;
12331 }
12332 else
12333 bitmap_set_bit (&aligned_head, DECL_UID (t));
12334 break;
12335
12336 case OMP_CLAUSE_DEPEND:
12337 t = OMP_CLAUSE_DECL (c);
12338 if (TREE_CODE (t) == TREE_LIST)
12339 {
12340 if (handle_omp_array_sections (c))
12341 remove = true;
12342 break;
12343 }
12344 if (t == error_mark_node)
12345 remove = true;
12346 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12347 {
12348 error_at (OMP_CLAUSE_LOCATION (c),
12349 "%qE is not a variable in %<depend%> clause", t);
12350 remove = true;
12351 }
12352 else if (!c_mark_addressable (t))
12353 remove = true;
12354 break;
12355
12356 case OMP_CLAUSE_MAP:
12357 case OMP_CLAUSE_TO:
12358 case OMP_CLAUSE_FROM:
41dbbb37 12359 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12360 t = OMP_CLAUSE_DECL (c);
12361 if (TREE_CODE (t) == TREE_LIST)
12362 {
12363 if (handle_omp_array_sections (c))
12364 remove = true;
12365 else
12366 {
12367 t = OMP_CLAUSE_DECL (c);
b17a8b07 12368 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12369 {
12370 error_at (OMP_CLAUSE_LOCATION (c),
12371 "array section does not have mappable type "
12372 "in %qs clause",
12373 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12374 remove = true;
12375 }
12376 }
12377 break;
12378 }
12379 if (t == error_mark_node)
12380 remove = true;
12381 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12382 {
12383 error_at (OMP_CLAUSE_LOCATION (c),
12384 "%qE is not a variable in %qs clause", t,
12385 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12386 remove = true;
12387 }
12388 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12389 {
12390 error_at (OMP_CLAUSE_LOCATION (c),
12391 "%qD is threadprivate variable in %qs clause", t,
12392 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12393 remove = true;
12394 }
12395 else if (!c_mark_addressable (t))
12396 remove = true;
b17a8b07 12397 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37
TS
12398 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12399 || (OMP_CLAUSE_MAP_KIND (c)
12400 == GOMP_MAP_FORCE_DEVICEPTR)))
b17a8b07 12401 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12402 {
12403 error_at (OMP_CLAUSE_LOCATION (c),
12404 "%qD does not have a mappable type in %qs clause", t,
12405 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12406 remove = true;
12407 }
12408 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12409 {
12410 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12411 error ("%qD appears more than once in motion clauses", t);
12412 else
12413 error ("%qD appears more than once in map clauses", t);
12414 remove = true;
12415 }
12416 else
12417 bitmap_set_bit (&generic_head, DECL_UID (t));
12418 break;
12419
12420 case OMP_CLAUSE_UNIFORM:
12421 t = OMP_CLAUSE_DECL (c);
12422 if (TREE_CODE (t) != PARM_DECL)
12423 {
12424 if (DECL_P (t))
12425 error_at (OMP_CLAUSE_LOCATION (c),
12426 "%qD is not an argument in %<uniform%> clause", t);
12427 else
12428 error_at (OMP_CLAUSE_LOCATION (c),
12429 "%qE is not an argument in %<uniform%> clause", t);
12430 remove = true;
ee1d5a02 12431 break;
acf0174b 12432 }
ee1d5a02 12433 goto check_dup_generic;
acf0174b
JJ
12434
12435 case OMP_CLAUSE_NOWAIT:
12436 if (copyprivate_seen)
12437 {
12438 error_at (OMP_CLAUSE_LOCATION (c),
12439 "%<nowait%> clause must not be used together "
12440 "with %<copyprivate%>");
12441 remove = true;
12442 break;
12443 }
12444 nowait_clause = pc;
12445 pc = &OMP_CLAUSE_CHAIN (c);
12446 continue;
12447
953ff289
DN
12448 case OMP_CLAUSE_IF:
12449 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12450 case OMP_CLAUSE_NUM_TEAMS:
12451 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12452 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12453 case OMP_CLAUSE_ORDERED:
12454 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12455 case OMP_CLAUSE_UNTIED:
12456 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12457 case OMP_CLAUSE_FINAL:
12458 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12459 case OMP_CLAUSE_SAFELEN:
12460 case OMP_CLAUSE_SIMDLEN:
12461 case OMP_CLAUSE_DEVICE:
12462 case OMP_CLAUSE_DIST_SCHEDULE:
12463 case OMP_CLAUSE_PARALLEL:
12464 case OMP_CLAUSE_FOR:
12465 case OMP_CLAUSE_SECTIONS:
12466 case OMP_CLAUSE_TASKGROUP:
12467 case OMP_CLAUSE_PROC_BIND:
9a771876 12468 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
12469 case OMP_CLAUSE_NUM_GANGS:
12470 case OMP_CLAUSE_NUM_WORKERS:
12471 case OMP_CLAUSE_VECTOR_LENGTH:
12472 case OMP_CLAUSE_ASYNC:
12473 case OMP_CLAUSE_WAIT:
12474 case OMP_CLAUSE_AUTO:
12475 case OMP_CLAUSE_SEQ:
12476 case OMP_CLAUSE_GANG:
12477 case OMP_CLAUSE_WORKER:
12478 case OMP_CLAUSE_VECTOR:
acf0174b
JJ
12479 pc = &OMP_CLAUSE_CHAIN (c);
12480 continue;
12481
12482 case OMP_CLAUSE_INBRANCH:
12483 case OMP_CLAUSE_NOTINBRANCH:
12484 if (branch_seen)
12485 {
12486 error_at (OMP_CLAUSE_LOCATION (c),
12487 "%<inbranch%> clause is incompatible with "
12488 "%<notinbranch%>");
12489 remove = true;
12490 break;
12491 }
12492 branch_seen = true;
953ff289
DN
12493 pc = &OMP_CLAUSE_CHAIN (c);
12494 continue;
12495
12496 default:
12497 gcc_unreachable ();
12498 }
12499
12500 if (!remove)
12501 {
12502 t = OMP_CLAUSE_DECL (c);
12503
12504 if (need_complete)
12505 {
12506 t = require_complete_type (t);
12507 if (t == error_mark_node)
12508 remove = true;
12509 }
12510
12511 if (need_implicitly_determined)
12512 {
12513 const char *share_name = NULL;
12514
12515 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12516 share_name = "threadprivate";
12517 else switch (c_omp_predetermined_sharing (t))
12518 {
12519 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12520 break;
12521 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12522 /* const vars may be specified in firstprivate clause. */
12523 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12524 && TREE_READONLY (t))
12525 break;
953ff289
DN
12526 share_name = "shared";
12527 break;
12528 case OMP_CLAUSE_DEFAULT_PRIVATE:
12529 share_name = "private";
12530 break;
12531 default:
12532 gcc_unreachable ();
12533 }
12534 if (share_name)
12535 {
c2255bc4
AH
12536 error_at (OMP_CLAUSE_LOCATION (c),
12537 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12538 t, share_name,
12539 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12540 remove = true;
12541 }
12542 }
12543 }
12544
12545 if (remove)
12546 *pc = OMP_CLAUSE_CHAIN (c);
12547 else
12548 pc = &OMP_CLAUSE_CHAIN (c);
12549 }
12550
12551 bitmap_obstack_release (NULL);
12552 return clauses;
12553}
9ae165a0 12554
0a35513e
AH
12555/* Create a transaction node. */
12556
12557tree
12558c_finish_transaction (location_t loc, tree block, int flags)
12559{
12560 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12561 if (flags & TM_STMT_ATTR_OUTER)
12562 TRANSACTION_EXPR_OUTER (stmt) = 1;
12563 if (flags & TM_STMT_ATTR_RELAXED)
12564 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12565 return add_stmt (stmt);
12566}
12567
9ae165a0
DG
12568/* Make a variant type in the proper way for C/C++, propagating qualifiers
12569 down to the element type of an array. */
12570
12571tree
12572c_build_qualified_type (tree type, int type_quals)
12573{
12574 if (type == error_mark_node)
12575 return type;
12576
12577 if (TREE_CODE (type) == ARRAY_TYPE)
12578 {
12579 tree t;
12580 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12581 type_quals);
12582
12583 /* See if we already have an identically qualified type. */
12584 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12585 {
12586 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12587 && TYPE_NAME (t) == TYPE_NAME (type)
12588 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12589 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12590 TYPE_ATTRIBUTES (type)))
12591 break;
12592 }
12593 if (!t)
12594 {
12595 tree domain = TYPE_DOMAIN (type);
12596
12597 t = build_variant_type_copy (type);
12598 TREE_TYPE (t) = element_type;
12599
12600 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12601 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12602 SET_TYPE_STRUCTURAL_EQUALITY (t);
12603 else if (TYPE_CANONICAL (element_type) != element_type
12604 || (domain && TYPE_CANONICAL (domain) != domain))
12605 {
b8698a0f 12606 tree unqualified_canon
9ae165a0 12607 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12608 domain? TYPE_CANONICAL (domain)
9ae165a0 12609 : NULL_TREE);
b8698a0f 12610 TYPE_CANONICAL (t)
9ae165a0
DG
12611 = c_build_qualified_type (unqualified_canon, type_quals);
12612 }
12613 else
12614 TYPE_CANONICAL (t) = t;
12615 }
12616 return t;
12617 }
12618
12619 /* A restrict-qualified pointer type must be a pointer to object or
12620 incomplete type. Note that the use of POINTER_TYPE_P also allows
12621 REFERENCE_TYPEs, which is appropriate for C++. */
12622 if ((type_quals & TYPE_QUAL_RESTRICT)
12623 && (!POINTER_TYPE_P (type)
12624 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12625 {
12626 error ("invalid use of %<restrict%>");
12627 type_quals &= ~TYPE_QUAL_RESTRICT;
12628 }
12629
12630 return build_qualified_type (type, type_quals);
12631}
72b5577d
ILT
12632
12633/* Build a VA_ARG_EXPR for the C parser. */
12634
12635tree
c2255bc4 12636c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d 12637{
ec3fba51
MP
12638 if (error_operand_p (type))
12639 return error_mark_node;
12640 else if (!COMPLETE_TYPE_P (type))
4e81b788 12641 {
ec3fba51
MP
12642 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12643 "type %qT", type);
4e81b788
MP
12644 return error_mark_node;
12645 }
ec3fba51
MP
12646 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12647 warning_at (loc, OPT_Wc___compat,
12648 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12649 return build_va_arg (loc, expr, type);
72b5577d 12650}
acf0174b
JJ
12651
12652/* Return truthvalue of whether T1 is the same tree structure as T2.
12653 Return 1 if they are the same. Return 0 if they are different. */
12654
12655bool
12656c_tree_equal (tree t1, tree t2)
12657{
12658 enum tree_code code1, code2;
12659
12660 if (t1 == t2)
12661 return true;
12662 if (!t1 || !t2)
12663 return false;
12664
12665 for (code1 = TREE_CODE (t1);
12666 CONVERT_EXPR_CODE_P (code1)
12667 || code1 == NON_LVALUE_EXPR;
12668 code1 = TREE_CODE (t1))
12669 t1 = TREE_OPERAND (t1, 0);
12670 for (code2 = TREE_CODE (t2);
12671 CONVERT_EXPR_CODE_P (code2)
12672 || code2 == NON_LVALUE_EXPR;
12673 code2 = TREE_CODE (t2))
12674 t2 = TREE_OPERAND (t2, 0);
12675
12676 /* They might have become equal now. */
12677 if (t1 == t2)
12678 return true;
12679
12680 if (code1 != code2)
12681 return false;
12682
12683 switch (code1)
12684 {
12685 case INTEGER_CST:
807e902e 12686 return wi::eq_p (t1, t2);
acf0174b
JJ
12687
12688 case REAL_CST:
12689 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12690
12691 case STRING_CST:
12692 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12693 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12694 TREE_STRING_LENGTH (t1));
12695
12696 case FIXED_CST:
12697 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12698 TREE_FIXED_CST (t2));
12699
12700 case COMPLEX_CST:
12701 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12702 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12703
12704 case VECTOR_CST:
12705 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12706
12707 case CONSTRUCTOR:
12708 /* We need to do this when determining whether or not two
12709 non-type pointer to member function template arguments
12710 are the same. */
12711 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12712 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12713 return false;
12714 {
12715 tree field, value;
12716 unsigned int i;
12717 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12718 {
12719 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12720 if (!c_tree_equal (field, elt2->index)
12721 || !c_tree_equal (value, elt2->value))
12722 return false;
12723 }
12724 }
12725 return true;
12726
12727 case TREE_LIST:
12728 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12729 return false;
12730 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12731 return false;
12732 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12733
12734 case SAVE_EXPR:
12735 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12736
12737 case CALL_EXPR:
12738 {
12739 tree arg1, arg2;
12740 call_expr_arg_iterator iter1, iter2;
12741 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12742 return false;
12743 for (arg1 = first_call_expr_arg (t1, &iter1),
12744 arg2 = first_call_expr_arg (t2, &iter2);
12745 arg1 && arg2;
12746 arg1 = next_call_expr_arg (&iter1),
12747 arg2 = next_call_expr_arg (&iter2))
12748 if (!c_tree_equal (arg1, arg2))
12749 return false;
12750 if (arg1 || arg2)
12751 return false;
12752 return true;
12753 }
12754
12755 case TARGET_EXPR:
12756 {
12757 tree o1 = TREE_OPERAND (t1, 0);
12758 tree o2 = TREE_OPERAND (t2, 0);
12759
12760 /* Special case: if either target is an unallocated VAR_DECL,
12761 it means that it's going to be unified with whatever the
12762 TARGET_EXPR is really supposed to initialize, so treat it
12763 as being equivalent to anything. */
12764 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12765 && !DECL_RTL_SET_P (o1))
12766 /*Nop*/;
12767 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12768 && !DECL_RTL_SET_P (o2))
12769 /*Nop*/;
12770 else if (!c_tree_equal (o1, o2))
12771 return false;
12772
12773 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12774 }
12775
12776 case COMPONENT_REF:
12777 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12778 return false;
12779 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12780
12781 case PARM_DECL:
12782 case VAR_DECL:
12783 case CONST_DECL:
12784 case FIELD_DECL:
12785 case FUNCTION_DECL:
12786 case IDENTIFIER_NODE:
12787 case SSA_NAME:
12788 return false;
12789
12790 case TREE_VEC:
12791 {
12792 unsigned ix;
12793 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12794 return false;
12795 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12796 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12797 TREE_VEC_ELT (t2, ix)))
12798 return false;
12799 return true;
12800 }
12801
12802 default:
12803 break;
12804 }
12805
12806 switch (TREE_CODE_CLASS (code1))
12807 {
12808 case tcc_unary:
12809 case tcc_binary:
12810 case tcc_comparison:
12811 case tcc_expression:
12812 case tcc_vl_exp:
12813 case tcc_reference:
12814 case tcc_statement:
12815 {
12816 int i, n = TREE_OPERAND_LENGTH (t1);
12817
12818 switch (code1)
12819 {
12820 case PREINCREMENT_EXPR:
12821 case PREDECREMENT_EXPR:
12822 case POSTINCREMENT_EXPR:
12823 case POSTDECREMENT_EXPR:
12824 n = 1;
12825 break;
12826 case ARRAY_REF:
12827 n = 2;
12828 break;
12829 default:
12830 break;
12831 }
12832
12833 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12834 && n != TREE_OPERAND_LENGTH (t2))
12835 return false;
12836
12837 for (i = 0; i < n; ++i)
12838 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12839 return false;
12840
12841 return true;
12842 }
12843
12844 case tcc_type:
12845 return comptypes (t1, t2);
12846 default:
12847 gcc_unreachable ();
12848 }
12849 /* We can get here with --disable-checking. */
12850 return false;
12851}
12893402
BI
12852
12853/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12854 spawn-helper and BODY is the newly created body for FNDECL. */
12855
12856void
12857cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12858{
12859 tree list = alloc_stmt_list ();
12860 tree frame = make_cilk_frame (fndecl);
12861 tree dtor = create_cilk_function_exit (frame, false, true);
12862 add_local_decl (cfun, frame);
12863
12864 DECL_SAVED_TREE (fndecl) = list;
12865 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12866 frame);
12867 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12868 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12869
12870 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12871 append_to_statement_list (detach_expr, &body_list);
12872
12873 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12874 body = fold_build_cleanup_point_expr (void_type_node, body);
12875
12876 append_to_statement_list (body, &body_list);
12877 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12878 body_list, dtor), &list);
12879}