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