]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
Daily bump.
[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 28#include "coretypes.h"
2adfab87
AM
29#include "target.h"
30#include "function.h"
31#include "bitmap.h"
2adfab87
AM
32#include "c-tree.h"
33#include "gimple-expr.h"
34#include "predict.h"
d8a2d370
DN
35#include "stor-layout.h"
36#include "trans-mem.h"
37#include "varasm.h"
38#include "stmt.h"
e57e265b 39#include "langhooks.h"
29cc57cf 40#include "c-lang.h"
ab87f8c8 41#include "intl.h"
325c3691 42#include "tree-iterator.h"
45b0be94 43#include "gimplify.h"
acf0174b 44#include "tree-inline.h"
0645c1a2 45#include "omp-low.h"
61d3ce20 46#include "c-family/c-objc.h"
de5a5fa1 47#include "c-family/c-ubsan.h"
12893402 48#include "cilk.h"
41dbbb37 49#include "gomp-constants.h"
277fe616 50#include "spellcheck.h"
325c3691 51
2ac2f164
JM
52/* Possible cases of implicit bad conversions. Used to select
53 diagnostic messages in convert_for_assignment. */
54enum impl_conv {
55 ic_argpass,
56 ic_assign,
57 ic_init,
58 ic_return
59};
60
bc4b653b
JM
61/* The level of nesting inside "__alignof__". */
62int in_alignof;
63
64/* The level of nesting inside "sizeof". */
65int in_sizeof;
66
67/* The level of nesting inside "typeof". */
68int in_typeof;
400fbf9f 69
1a4049e7
JJ
70/* The argument of last parsed sizeof expression, only to be tested
71 if expr.original_code == SIZEOF_EXPR. */
72tree c_last_sizeof_arg;
73
9bac5cbb
G
74/* Nonzero if we might need to print a "missing braces around
75 initializer" message within this initializer. */
76static int found_missing_braces;
103b7b17 77
bf730f15
RS
78static int require_constant_value;
79static int require_constant_elements;
80
58f9752a 81static bool null_pointer_constant_p (const_tree);
f55ade6e 82static tree qualify_type (tree, tree);
dc5027f4
JM
83static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
84 bool *);
744aa42f 85static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
86static int function_types_compatible_p (const_tree, const_tree, bool *,
87 bool *);
88static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 89static tree lookup_field (tree, tree);
81e5eca8
MP
90static int convert_arguments (location_t, vec<location_t>, tree,
91 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
92 tree);
db3927fb 93static tree pointer_diff (location_t, tree, tree);
68fca595 94static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 95 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
96static tree valid_compound_expr_initializer (tree, tree);
97static void push_string (const char *);
98static void push_member_name (tree);
f55ade6e
AJ
99static int spelling_length (void);
100static char *print_spelling (char *);
96b40f8d 101static void warning_init (location_t, int, const char *);
c2255bc4 102static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
103static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
104 bool, struct obstack *);
a1e3b3d9 105static void output_pending_init_elements (int, struct obstack *);
ea58ef42 106static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 107static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
108static void add_pending_init (location_t, tree, tree, tree, bool,
109 struct obstack *);
a1e3b3d9
LB
110static void set_nonincremental_init (struct obstack *);
111static void set_nonincremental_init_from_string (tree, struct obstack *);
112static tree find_init_member (tree, struct obstack *);
f37acdf9 113static void readonly_warning (tree, enum lvalue_use);
7bd11157 114static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 115static void record_maybe_used_decl (tree);
dc5027f4 116static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
117\f
118/* Return true if EXP is a null pointer constant, false otherwise. */
119
120static bool
58f9752a 121null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
122{
123 /* This should really operate on c_expr structures, but they aren't
124 yet available everywhere required. */
125 tree type = TREE_TYPE (expr);
126 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 127 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
128 && integer_zerop (expr)
129 && (INTEGRAL_TYPE_P (type)
130 || (TREE_CODE (type) == POINTER_TYPE
131 && VOID_TYPE_P (TREE_TYPE (type))
132 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
133}
928c19bb
JM
134
135/* EXPR may appear in an unevaluated part of an integer constant
136 expression, but not in an evaluated part. Wrap it in a
137 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
138 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
139
140static tree
141note_integer_operands (tree expr)
142{
143 tree ret;
144 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
145 {
146 ret = copy_node (expr);
147 TREE_OVERFLOW (ret) = 1;
148 }
149 else
150 {
151 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
152 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
153 }
154 return ret;
155}
156
4d84fe7c
JM
157/* Having checked whether EXPR may appear in an unevaluated part of an
158 integer constant expression and found that it may, remove any
159 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 expression. */
161
162static inline tree
163remove_c_maybe_const_expr (tree expr)
164{
165 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
166 return C_MAYBE_CONST_EXPR_EXPR (expr);
167 else
168 return expr;
169}
170
f13c9b2c
AP
171\f/* This is a cache to hold if two types are compatible or not. */
172
173struct tagged_tu_seen_cache {
174 const struct tagged_tu_seen_cache * next;
58f9752a
KG
175 const_tree t1;
176 const_tree t2;
f13c9b2c
AP
177 /* The return value of tagged_types_tu_compatible_p if we had seen
178 these two types already. */
179 int val;
180};
181
182static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
183static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
184
400fbf9f
JW
185/* Do `exp = require_complete_type (exp);' to make sure exp
186 does not have an incomplete type. (That includes void types.) */
187
188tree
2f6e4e97 189require_complete_type (tree value)
400fbf9f
JW
190{
191 tree type = TREE_TYPE (value);
192
7a0ca710 193 if (error_operand_p (value))
ea0f786b
CB
194 return error_mark_node;
195
400fbf9f 196 /* First, detect a valid value with a complete type. */
d0f062fb 197 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
198 return value;
199
7a228918 200 c_incomplete_type_error (value, type);
400fbf9f
JW
201 return error_mark_node;
202}
203
204/* Print an error message for invalid use of an incomplete type.
205 VALUE is the expression that was used (or 0 if that isn't known)
206 and TYPE is the type that was invalid. */
207
208void
ac7d7749 209c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 210{
400fbf9f
JW
211 /* Avoid duplicate error message. */
212 if (TREE_CODE (type) == ERROR_MARK)
213 return;
214
0ae9bd27 215 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
ac9f18db 216 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
217 else
218 {
219 retry:
220 /* We must print an error message. Be clever about what it says. */
221
222 switch (TREE_CODE (type))
223 {
224 case RECORD_TYPE:
400fbf9f 225 case UNION_TYPE:
400fbf9f 226 case ENUMERAL_TYPE:
400fbf9f
JW
227 break;
228
229 case VOID_TYPE:
230 error ("invalid use of void expression");
231 return;
232
233 case ARRAY_TYPE:
234 if (TYPE_DOMAIN (type))
235 {
fba78abb
RH
236 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
237 {
238 error ("invalid use of flexible array member");
239 return;
240 }
400fbf9f
JW
241 type = TREE_TYPE (type);
242 goto retry;
243 }
244 error ("invalid use of array with unspecified bounds");
245 return;
246
247 default:
366de0ce 248 gcc_unreachable ();
400fbf9f
JW
249 }
250
251 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 252 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
253 else
254 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 255 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
256 }
257}
258
ab393bf1
NB
259/* Given a type, apply default promotions wrt unnamed function
260 arguments and return the new type. */
261
262tree
2f6e4e97 263c_type_promotes_to (tree type)
ab393bf1 264{
267bac10 265 tree ret = NULL_TREE;
ab393bf1 266
267bac10
JM
267 if (TYPE_MAIN_VARIANT (type) == float_type_node)
268 ret = double_type_node;
269 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
270 {
271 /* Preserve unsignedness if not really getting any wider. */
8df83eae 272 if (TYPE_UNSIGNED (type)
c22cacf3 273 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
274 ret = unsigned_type_node;
275 else
276 ret = integer_type_node;
ab393bf1
NB
277 }
278
267bac10
JM
279 if (ret != NULL_TREE)
280 return (TYPE_ATOMIC (type)
281 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
282 : ret);
283
ab393bf1
NB
284 return type;
285}
286
36c5e70a
BE
287/* Return true if between two named address spaces, whether there is a superset
288 named address space that encompasses both address spaces. If there is a
289 superset, return which address space is the superset. */
290
291static bool
292addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
293{
294 if (as1 == as2)
295 {
296 *common = as1;
297 return true;
298 }
299 else if (targetm.addr_space.subset_p (as1, as2))
300 {
301 *common = as2;
302 return true;
303 }
304 else if (targetm.addr_space.subset_p (as2, as1))
305 {
306 *common = as1;
307 return true;
308 }
309 else
310 return false;
311}
312
400fbf9f
JW
313/* Return a variant of TYPE which has all the type qualifiers of LIKE
314 as well as those of TYPE. */
315
316static tree
2f6e4e97 317qualify_type (tree type, tree like)
400fbf9f 318{
36c5e70a
BE
319 addr_space_t as_type = TYPE_ADDR_SPACE (type);
320 addr_space_t as_like = TYPE_ADDR_SPACE (like);
321 addr_space_t as_common;
322
323 /* If the two named address spaces are different, determine the common
324 superset address space. If there isn't one, raise an error. */
325 if (!addr_space_superset (as_type, as_like, &as_common))
326 {
327 as_common = as_type;
328 error ("%qT and %qT are in disjoint named address spaces",
329 type, like);
330 }
331
2f6e4e97 332 return c_build_qualified_type (type,
36c5e70a 333 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 334 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 335 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 336}
52ffd86e
MS
337
338/* Return true iff the given tree T is a variable length array. */
339
340bool
ac7d7749 341c_vla_type_p (const_tree t)
52ffd86e
MS
342{
343 if (TREE_CODE (t) == ARRAY_TYPE
344 && C_TYPE_VARIABLE_SIZE (t))
345 return true;
346 return false;
347}
400fbf9f 348\f
10bc1b1b 349/* Return the composite type of two compatible types.
5305f6d7 350
10bc1b1b
JM
351 We assume that comptypes has already been done and returned
352 nonzero; if that isn't so, this may crash. In particular, we
353 assume that qualifiers match. */
400fbf9f
JW
354
355tree
10bc1b1b 356composite_type (tree t1, tree t2)
400fbf9f 357{
b3694847
SS
358 enum tree_code code1;
359 enum tree_code code2;
4b027d16 360 tree attributes;
400fbf9f
JW
361
362 /* Save time if the two types are the same. */
363
364 if (t1 == t2) return t1;
365
366 /* If one type is nonsense, use the other. */
367 if (t1 == error_mark_node)
368 return t2;
369 if (t2 == error_mark_node)
370 return t1;
371
10bc1b1b
JM
372 code1 = TREE_CODE (t1);
373 code2 = TREE_CODE (t2);
374
d9525bec 375 /* Merge the attributes. */
5fd9b178 376 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 377
10bc1b1b
JM
378 /* If one is an enumerated type and the other is the compatible
379 integer type, the composite type might be either of the two
380 (DR#013 question 3). For consistency, use the enumerated type as
381 the composite type. */
400fbf9f 382
10bc1b1b
JM
383 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
384 return t1;
385 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
386 return t2;
75326e8c 387
366de0ce 388 gcc_assert (code1 == code2);
b6a10c9f 389
400fbf9f
JW
390 switch (code1)
391 {
400fbf9f 392 case POINTER_TYPE:
10bc1b1b 393 /* For two pointers, do this recursively on the target type. */
400fbf9f 394 {
3932261a
MM
395 tree pointed_to_1 = TREE_TYPE (t1);
396 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 397 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 398 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
399 t1 = build_type_attribute_variant (t1, attributes);
400 return qualify_type (t1, t2);
400fbf9f 401 }
400fbf9f
JW
402
403 case ARRAY_TYPE:
404 {
10bc1b1b 405 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
406 int quals;
407 tree unqual_elt;
ca8bdb78
JM
408 tree d1 = TYPE_DOMAIN (t1);
409 tree d2 = TYPE_DOMAIN (t2);
410 bool d1_variable, d2_variable;
411 bool d1_zero, d2_zero;
f6294de7 412 bool t1_complete, t2_complete;
46df2823 413
de46b2fe 414 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
415 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
416 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 417
f6294de7
JM
418 t1_complete = COMPLETE_TYPE_P (t1);
419 t2_complete = COMPLETE_TYPE_P (t2);
420
ca8bdb78
JM
421 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
422 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423
424 d1_variable = (!d1_zero
425 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
426 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
427 d2_variable = (!d2_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
430 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
431 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 432
400fbf9f 433 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
434 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
435 && (d2_variable || d2_zero || !d1_variable))
4b027d16 436 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
437 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
438 && (d1_variable || d1_zero || !d2_variable))
4b027d16 439 return build_type_attribute_variant (t2, attributes);
c22cacf3 440
de46b2fe
AP
441 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
442 return build_type_attribute_variant (t1, attributes);
443 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
444 return build_type_attribute_variant (t2, attributes);
c22cacf3 445
46df2823
JM
446 /* Merge the element types, and have a size if either arg has
447 one. We may have qualifiers on the element types. To set
448 up TYPE_MAIN_VARIANT correctly, we need to form the
449 composite of the unqualified types and add the qualifiers
450 back at the end. */
451 quals = TYPE_QUALS (strip_array_types (elt));
452 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
453 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
454 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
455 && (d2_variable
456 || d2_zero
457 || !d1_variable))
458 ? t1
459 : t2));
f6294de7
JM
460 /* Ensure a composite type involving a zero-length array type
461 is a zero-length type not an incomplete type. */
462 if (d1_zero && d2_zero
463 && (t1_complete || t2_complete)
464 && !COMPLETE_TYPE_P (t1))
465 {
466 TYPE_SIZE (t1) = bitsize_zero_node;
467 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 }
46df2823 469 t1 = c_build_qualified_type (t1, quals);
de46b2fe 470 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
471 }
472
fcb99e7b
JJ
473 case ENUMERAL_TYPE:
474 case RECORD_TYPE:
475 case UNION_TYPE:
476 if (attributes != NULL)
477 {
478 /* Try harder not to create a new aggregate type. */
479 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
480 return t1;
481 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
482 return t2;
483 }
484 return build_type_attribute_variant (t1, attributes);
485
400fbf9f
JW
486 case FUNCTION_TYPE:
487 /* Function types: prefer the one that specified arg types.
488 If both do, merge the arg types. Also merge the return types. */
489 {
10bc1b1b 490 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
491 tree p1 = TYPE_ARG_TYPES (t1);
492 tree p2 = TYPE_ARG_TYPES (t2);
493 int len;
494 tree newargs, n;
495 int i;
496
497 /* Save space: see if the result is identical to one of the args. */
3f75a254 498 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 499 return build_type_attribute_variant (t1, attributes);
3f75a254 500 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 501 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
502
503 /* Simple way if one arg fails to specify argument types. */
504 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 505 {
fe7080d2
AP
506 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
507 t1 = build_type_attribute_variant (t1, attributes);
508 return qualify_type (t1, t2);
4b027d16 509 }
400fbf9f 510 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
511 {
512 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
513 t1 = build_type_attribute_variant (t1, attributes);
514 return qualify_type (t1, t2);
4b027d16 515 }
400fbf9f
JW
516
517 /* If both args specify argument types, we must merge the two
518 lists, argument by argument. */
2f4e8f2b 519
400fbf9f
JW
520 len = list_length (p1);
521 newargs = 0;
522
523 for (i = 0; i < len; i++)
8d9bfdc5 524 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
525
526 n = newargs;
527
528 for (; p1;
529 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
530 {
531 /* A null type means arg type is not specified.
532 Take whatever the other function type has. */
533 if (TREE_VALUE (p1) == 0)
534 {
535 TREE_VALUE (n) = TREE_VALUE (p2);
536 goto parm_done;
537 }
538 if (TREE_VALUE (p2) == 0)
539 {
540 TREE_VALUE (n) = TREE_VALUE (p1);
541 goto parm_done;
542 }
2f6e4e97 543
400fbf9f
JW
544 /* Given wait (union {union wait *u; int *i} *)
545 and wait (union wait *),
546 prefer union wait * as type of parm. */
547 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
548 && TREE_VALUE (p1) != TREE_VALUE (p2))
549 {
550 tree memb;
58cb41e6
JJ
551 tree mv2 = TREE_VALUE (p2);
552 if (mv2 && mv2 != error_mark_node
553 && TREE_CODE (mv2) != ARRAY_TYPE)
554 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 555 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 556 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
557 {
558 tree mv3 = TREE_TYPE (memb);
559 if (mv3 && mv3 != error_mark_node
560 && TREE_CODE (mv3) != ARRAY_TYPE)
561 mv3 = TYPE_MAIN_VARIANT (mv3);
562 if (comptypes (mv3, mv2))
563 {
564 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
565 TREE_VALUE (p2));
c1771a20 566 pedwarn (input_location, OPT_Wpedantic,
fcf73884 567 "function types not truly compatible in ISO C");
58cb41e6
JJ
568 goto parm_done;
569 }
570 }
400fbf9f
JW
571 }
572 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
573 && TREE_VALUE (p2) != TREE_VALUE (p1))
574 {
575 tree memb;
58cb41e6
JJ
576 tree mv1 = TREE_VALUE (p1);
577 if (mv1 && mv1 != error_mark_node
578 && TREE_CODE (mv1) != ARRAY_TYPE)
579 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 580 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 581 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
582 {
583 tree mv3 = TREE_TYPE (memb);
584 if (mv3 && mv3 != error_mark_node
585 && TREE_CODE (mv3) != ARRAY_TYPE)
586 mv3 = TYPE_MAIN_VARIANT (mv3);
587 if (comptypes (mv3, mv1))
588 {
589 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
590 TREE_VALUE (p1));
c1771a20 591 pedwarn (input_location, OPT_Wpedantic,
fcf73884 592 "function types not truly compatible in ISO C");
58cb41e6
JJ
593 goto parm_done;
594 }
595 }
400fbf9f 596 }
10bc1b1b 597 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
598 parm_done: ;
599 }
600
4b027d16 601 t1 = build_function_type (valtype, newargs);
fe7080d2 602 t1 = qualify_type (t1, t2);
0f41302f 603 /* ... falls through ... */
400fbf9f
JW
604 }
605
606 default:
4b027d16 607 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
608 }
609
610}
10bc1b1b
JM
611
612/* Return the type of a conditional expression between pointers to
613 possibly differently qualified versions of compatible types.
614
615 We assume that comp_target_types has already been done and returned
616 nonzero; if that isn't so, this may crash. */
617
618static tree
619common_pointer_type (tree t1, tree t2)
620{
621 tree attributes;
46df2823
JM
622 tree pointed_to_1, mv1;
623 tree pointed_to_2, mv2;
10bc1b1b 624 tree target;
eb1387a0 625 unsigned target_quals;
36c5e70a
BE
626 addr_space_t as1, as2, as_common;
627 int quals1, quals2;
10bc1b1b
JM
628
629 /* Save time if the two types are the same. */
630
631 if (t1 == t2) return t1;
632
633 /* If one type is nonsense, use the other. */
634 if (t1 == error_mark_node)
635 return t2;
636 if (t2 == error_mark_node)
637 return t1;
638
366de0ce 639 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 640 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
641
642 /* Merge the attributes. */
643 attributes = targetm.merge_type_attributes (t1, t2);
644
645 /* Find the composite type of the target types, and combine the
46df2823
JM
646 qualifiers of the two types' targets. Do not lose qualifiers on
647 array element types by taking the TYPE_MAIN_VARIANT. */
648 mv1 = pointed_to_1 = TREE_TYPE (t1);
649 mv2 = pointed_to_2 = TREE_TYPE (t2);
650 if (TREE_CODE (mv1) != ARRAY_TYPE)
651 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
652 if (TREE_CODE (mv2) != ARRAY_TYPE)
653 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
654 target = composite_type (mv1, mv2);
eb1387a0 655
768952be
MU
656 /* Strip array types to get correct qualifier for pointers to arrays */
657 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
658 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
659
eb1387a0
RG
660 /* For function types do not merge const qualifiers, but drop them
661 if used inconsistently. The middle-end uses these to mark const
662 and noreturn functions. */
663 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 664 target_quals = (quals1 & quals2);
eb1387a0 665 else
36c5e70a
BE
666 target_quals = (quals1 | quals2);
667
668 /* If the two named address spaces are different, determine the common
669 superset address space. This is guaranteed to exist due to the
670 assumption that comp_target_type returned non-zero. */
671 as1 = TYPE_ADDR_SPACE (pointed_to_1);
672 as2 = TYPE_ADDR_SPACE (pointed_to_2);
673 if (!addr_space_superset (as1, as2, &as_common))
674 gcc_unreachable ();
675
676 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
677
eb1387a0 678 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
679 return build_type_attribute_variant (t1, attributes);
680}
681
682/* Return the common type for two arithmetic types under the usual
683 arithmetic conversions. The default conversions have already been
684 applied, and enumerated types converted to their compatible integer
685 types. The resulting type is unqualified and has no attributes.
686
687 This is the type for the result of most arithmetic operations
688 if the operands have the given two types. */
689
ccf7f880
JJ
690static tree
691c_common_type (tree t1, tree t2)
10bc1b1b
JM
692{
693 enum tree_code code1;
694 enum tree_code code2;
695
696 /* If one type is nonsense, use the other. */
697 if (t1 == error_mark_node)
698 return t2;
699 if (t2 == error_mark_node)
700 return t1;
701
702 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
703 t1 = TYPE_MAIN_VARIANT (t1);
704
705 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
706 t2 = TYPE_MAIN_VARIANT (t2);
707
708 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
709 t1 = build_type_attribute_variant (t1, NULL_TREE);
710
711 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
712 t2 = build_type_attribute_variant (t2, NULL_TREE);
713
714 /* Save time if the two types are the same. */
715
716 if (t1 == t2) return t1;
717
718 code1 = TREE_CODE (t1);
719 code2 = TREE_CODE (t2);
720
366de0ce 721 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
722 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
723 || code1 == INTEGER_TYPE);
366de0ce 724 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
725 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
726 || code2 == INTEGER_TYPE);
10bc1b1b 727
5fc89bfd
JJ
728 /* When one operand is a decimal float type, the other operand cannot be
729 a generic float type or a complex type. We also disallow vector types
730 here. */
731 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
732 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
733 {
734 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
735 {
736 error ("can%'t mix operands of decimal float and vector types");
737 return error_mark_node;
738 }
739 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
740 {
741 error ("can%'t mix operands of decimal float and complex types");
742 return error_mark_node;
743 }
744 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and other float types");
747 return error_mark_node;
748 }
749 }
750
10bc1b1b
JM
751 /* If one type is a vector type, return that type. (How the usual
752 arithmetic conversions apply to the vector types extension is not
753 precisely specified.) */
754 if (code1 == VECTOR_TYPE)
755 return t1;
756
757 if (code2 == VECTOR_TYPE)
758 return t2;
759
760 /* If one type is complex, form the common type of the non-complex
761 components, then make that complex. Use T1 or T2 if it is the
762 required type. */
763 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
764 {
765 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
766 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 767 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
768
769 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
770 return t1;
771 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
772 return t2;
773 else
774 return build_complex_type (subtype);
775 }
776
777 /* If only one is real, use it as the result. */
778
779 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
780 return t1;
781
782 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
783 return t2;
784
9a8ce21f
JG
785 /* If both are real and either are decimal floating point types, use
786 the decimal floating point type with the greater precision. */
787
788 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
789 {
790 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
791 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
792 return dfloat128_type_node;
793 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
794 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
795 return dfloat64_type_node;
796 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
798 return dfloat32_type_node;
799 }
800
ab22c1fa
CF
801 /* Deal with fixed-point types. */
802 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
803 {
804 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 805 machine_mode m1, m2;
ab22c1fa
CF
806 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
807
808 m1 = TYPE_MODE (t1);
809 m2 = TYPE_MODE (t2);
810
811 /* If one input type is saturating, the result type is saturating. */
812 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
813 satp = 1;
814
815 /* If both fixed-point types are unsigned, the result type is unsigned.
816 When mixing fixed-point and integer types, follow the sign of the
817 fixed-point type.
818 Otherwise, the result type is signed. */
819 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
820 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
821 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t1))
823 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t2)))
825 unsignedp = 1;
826
827 /* The result type is signed. */
828 if (unsignedp == 0)
829 {
830 /* If the input type is unsigned, we need to convert to the
831 signed type. */
832 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
833 {
d75d71e0 834 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
835 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
836 mclass = MODE_FRACT;
837 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
838 mclass = MODE_ACCUM;
839 else
840 gcc_unreachable ();
841 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
842 }
843 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
844 {
d75d71e0 845 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
846 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
853 }
854 }
855
856 if (code1 == FIXED_POINT_TYPE)
857 {
858 fbit1 = GET_MODE_FBIT (m1);
859 ibit1 = GET_MODE_IBIT (m1);
860 }
861 else
862 {
863 fbit1 = 0;
864 /* Signed integers need to subtract one sign bit. */
865 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
866 }
867
868 if (code2 == FIXED_POINT_TYPE)
869 {
870 fbit2 = GET_MODE_FBIT (m2);
871 ibit2 = GET_MODE_IBIT (m2);
872 }
873 else
874 {
875 fbit2 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
878 }
879
880 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
881 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
882 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
883 satp);
884 }
885
10bc1b1b
JM
886 /* Both real or both integers; use the one with greater precision. */
887
888 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
889 return t1;
890 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
891 return t2;
892
893 /* Same precision. Prefer long longs to longs to ints when the
894 same precision, following the C99 rules on integer type rank
895 (which are equivalent to the C90 rules for C90 types). */
896
897 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
898 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
899 return long_long_unsigned_type_node;
900
901 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
902 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
903 {
904 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
905 return long_long_unsigned_type_node;
906 else
c22cacf3 907 return long_long_integer_type_node;
10bc1b1b
JM
908 }
909
910 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
911 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
912 return long_unsigned_type_node;
913
914 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
916 {
917 /* But preserve unsignedness from the other type,
918 since long cannot hold all the values of an unsigned int. */
919 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
920 return long_unsigned_type_node;
921 else
922 return long_integer_type_node;
923 }
924
925 /* Likewise, prefer long double to double even if same size. */
926 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
928 return long_double_type_node;
929
2531a1d9
JR
930 /* Likewise, prefer double to float even if same size.
931 We got a couple of embedded targets with 32 bit doubles, and the
932 pdp11 might have 64 bit floats. */
933 if (TYPE_MAIN_VARIANT (t1) == double_type_node
934 || TYPE_MAIN_VARIANT (t2) == double_type_node)
935 return double_type_node;
936
10bc1b1b
JM
937 /* Otherwise prefer the unsigned one. */
938
939 if (TYPE_UNSIGNED (t1))
940 return t1;
941 else
942 return t2;
943}
400fbf9f 944\f
5922c215
JM
945/* Wrapper around c_common_type that is used by c-common.c and other
946 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
947 are allowed here and are converted to their compatible integer types.
948 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
949 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
950tree
951common_type (tree t1, tree t2)
952{
953 if (TREE_CODE (t1) == ENUMERAL_TYPE)
954 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
955 if (TREE_CODE (t2) == ENUMERAL_TYPE)
956 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
957
958 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE
960 && TREE_CODE (t2) == BOOLEAN_TYPE)
961 return boolean_type_node;
962
963 /* If either type is BOOLEAN_TYPE, then return the other. */
964 if (TREE_CODE (t1) == BOOLEAN_TYPE)
965 return t2;
966 if (TREE_CODE (t2) == BOOLEAN_TYPE)
967 return t1;
968
ccf7f880
JJ
969 return c_common_type (t1, t2);
970}
f13c9b2c 971
400fbf9f
JW
972/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
973 or various other operations. Return 2 if they are compatible
974 but a warning may be needed if you use them together. */
975
976int
132da1a5 977comptypes (tree type1, tree type2)
f13c9b2c
AP
978{
979 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
980 int val;
981
dc5027f4 982 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
983 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
984
985 return val;
986}
987
988/* Like comptypes, but if it returns non-zero because enum and int are
989 compatible, it sets *ENUM_AND_INT_P to true. */
990
991static int
992comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
993{
994 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
995 int val;
996
dc5027f4
JM
997 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
998 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
999
1000 return val;
1001}
1002
1003/* Like comptypes, but if it returns nonzero for different types, it
1004 sets *DIFFERENT_TYPES_P to true. */
1005
1006int
1007comptypes_check_different_types (tree type1, tree type2,
1008 bool *different_types_p)
1009{
1010 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1011 int val;
1012
1013 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1014 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1015
f13c9b2c 1016 return val;
c22cacf3
MS
1017}
1018\f
f13c9b2c
AP
1019/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1020 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1021 but a warning may be needed if you use them together. If
1022 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1023 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1024 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1025 NULL, and the types are compatible but different enough not to be
48b0b196 1026 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1027 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1028 false, but may or may not be set if the types are incompatible.
1029 This differs from comptypes, in that we don't free the seen
1030 types. */
f13c9b2c
AP
1031
1032static int
dc5027f4
JM
1033comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1034 bool *different_types_p)
400fbf9f 1035{
58f9752a
KG
1036 const_tree t1 = type1;
1037 const_tree t2 = type2;
4b027d16 1038 int attrval, val;
400fbf9f
JW
1039
1040 /* Suppress errors caused by previously reported errors. */
1041
8d47dfc5
RH
1042 if (t1 == t2 || !t1 || !t2
1043 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1044 return 1;
1045
bca63328
JM
1046 /* Enumerated types are compatible with integer types, but this is
1047 not transitive: two enumerated types in the same translation unit
1048 are compatible with each other only if they are the same type. */
400fbf9f 1049
bca63328 1050 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1051 {
1052 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1053 if (TREE_CODE (t2) != VOID_TYPE)
1054 {
1055 if (enum_and_int_p != NULL)
1056 *enum_and_int_p = true;
1057 if (different_types_p != NULL)
1058 *different_types_p = true;
1059 }
744aa42f 1060 }
bca63328 1061 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1062 {
1063 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1064 if (TREE_CODE (t1) != VOID_TYPE)
1065 {
1066 if (enum_and_int_p != NULL)
1067 *enum_and_int_p = true;
1068 if (different_types_p != NULL)
1069 *different_types_p = true;
1070 }
744aa42f 1071 }
400fbf9f
JW
1072
1073 if (t1 == t2)
1074 return 1;
1075
1076 /* Different classes of types can't be compatible. */
1077
3aeb3655
EC
1078 if (TREE_CODE (t1) != TREE_CODE (t2))
1079 return 0;
400fbf9f 1080
118a3a8b 1081 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1082
3932261a 1083 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1084 return 0;
1085
08632da2
RS
1086 /* Allow for two different type nodes which have essentially the same
1087 definition. Note that we already checked for equality of the type
38e01259 1088 qualifiers (just above). */
400fbf9f 1089
46df2823
JM
1090 if (TREE_CODE (t1) != ARRAY_TYPE
1091 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1092 return 1;
1093
4b027d16 1094 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1095 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1096 return 0;
1097
1098 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1099 val = 0;
1100
400fbf9f
JW
1101 switch (TREE_CODE (t1))
1102 {
1103 case POINTER_TYPE:
106f5de5
UW
1104 /* Do not remove mode or aliasing information. */
1105 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1106 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1107 break;
4b027d16 1108 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1109 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1110 enum_and_int_p, different_types_p));
4b027d16 1111 break;
400fbf9f
JW
1112
1113 case FUNCTION_TYPE:
dc5027f4
JM
1114 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1115 different_types_p);
4b027d16 1116 break;
400fbf9f
JW
1117
1118 case ARRAY_TYPE:
1119 {
400fbf9f
JW
1120 tree d1 = TYPE_DOMAIN (t1);
1121 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1122 bool d1_variable, d2_variable;
1123 bool d1_zero, d2_zero;
4b027d16 1124 val = 1;
400fbf9f
JW
1125
1126 /* Target types must match incl. qualifiers. */
1127 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1128 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1129 enum_and_int_p,
1130 different_types_p)))
400fbf9f
JW
1131 return 0;
1132
dc5027f4
JM
1133 if (different_types_p != NULL
1134 && (d1 == 0) != (d2 == 0))
1135 *different_types_p = true;
400fbf9f 1136 /* Sizes must match unless one is missing or variable. */
3f85558f 1137 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1138 break;
400fbf9f 1139
3f75a254
JM
1140 d1_zero = !TYPE_MAX_VALUE (d1);
1141 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1142
3f75a254 1143 d1_variable = (!d1_zero
3f85558f
RH
1144 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1145 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1146 d2_variable = (!d2_zero
3f85558f
RH
1147 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1148 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1149 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1150 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1151
dc5027f4
JM
1152 if (different_types_p != NULL
1153 && d1_variable != d2_variable)
1154 *different_types_p = true;
3f85558f
RH
1155 if (d1_variable || d2_variable)
1156 break;
1157 if (d1_zero && d2_zero)
1158 break;
1159 if (d1_zero || d2_zero
3f75a254
JM
1160 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1161 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1162 val = 0;
1163
c22cacf3 1164 break;
400fbf9f
JW
1165 }
1166
d1bd0ded 1167 case ENUMERAL_TYPE:
58393038 1168 case RECORD_TYPE:
d1bd0ded 1169 case UNION_TYPE:
766beae1 1170 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1171 {
fcb99e7b
JJ
1172 tree a1 = TYPE_ATTRIBUTES (t1);
1173 tree a2 = TYPE_ATTRIBUTES (t2);
1174
1175 if (! attribute_list_contained (a1, a2)
1176 && ! attribute_list_contained (a2, a1))
1177 break;
1178
f13c9b2c 1179 if (attrval != 2)
dc5027f4
JM
1180 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1181 different_types_p);
1182 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1183 different_types_p);
f13c9b2c 1184 }
4b027d16 1185 break;
e9a25f70 1186
62e1dfcf 1187 case VECTOR_TYPE:
744aa42f
ILT
1188 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1189 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1190 enum_and_int_p, different_types_p));
62e1dfcf
NC
1191 break;
1192
e9a25f70
JL
1193 default:
1194 break;
400fbf9f 1195 }
4b027d16 1196 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1197}
1198
36c5e70a
BE
1199/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1200 their qualifiers, except for named address spaces. If the pointers point to
1201 different named addresses, then we must determine if one address space is a
1202 subset of the other. */
400fbf9f
JW
1203
1204static int
744aa42f 1205comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1206{
392202b0 1207 int val;
768952be 1208 int val_ped;
36c5e70a
BE
1209 tree mvl = TREE_TYPE (ttl);
1210 tree mvr = TREE_TYPE (ttr);
1211 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1212 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1213 addr_space_t as_common;
744aa42f 1214 bool enum_and_int_p;
8b40563c 1215
36c5e70a
BE
1216 /* Fail if pointers point to incompatible address spaces. */
1217 if (!addr_space_superset (asl, asr, &as_common))
1218 return 0;
1219
768952be
MU
1220 /* For pedantic record result of comptypes on arrays before losing
1221 qualifiers on the element type below. */
1222 val_ped = 1;
1223
1224 if (TREE_CODE (mvl) == ARRAY_TYPE
1225 && TREE_CODE (mvr) == ARRAY_TYPE)
1226 val_ped = comptypes (mvl, mvr);
1227
1228 /* Qualifiers on element types of array types that are
1229 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1230
1231 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1232 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1233 : TYPE_MAIN_VARIANT (mvl));
1234
1235 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1236 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1237 : TYPE_MAIN_VARIANT (mvr));
1238
744aa42f
ILT
1239 enum_and_int_p = false;
1240 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1241
768952be
MU
1242 if (val == 1 && val_ped != 1)
1243 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1244 "are incompatible in ISO C");
1245
fcf73884 1246 if (val == 2)
c1771a20 1247 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1248
1249 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1250 warning_at (location, OPT_Wc___compat,
1251 "pointer target types incompatible in C++");
1252
400fbf9f
JW
1253 return val;
1254}
1255\f
1256/* Subroutines of `comptypes'. */
1257
f75fbaf7
ZW
1258/* Determine whether two trees derive from the same translation unit.
1259 If the CONTEXT chain ends in a null, that tree's context is still
1260 being parsed, so if two trees have context chains ending in null,
766beae1 1261 they're in the same translation unit. */
f75fbaf7 1262int
58f9752a 1263same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1264{
1265 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1266 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1267 {
6615c446
JO
1268 case tcc_declaration:
1269 t1 = DECL_CONTEXT (t1); break;
1270 case tcc_type:
1271 t1 = TYPE_CONTEXT (t1); break;
1272 case tcc_exceptional:
1273 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1274 default: gcc_unreachable ();
766beae1
ZW
1275 }
1276
1277 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1278 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1279 {
6615c446
JO
1280 case tcc_declaration:
1281 t2 = DECL_CONTEXT (t2); break;
1282 case tcc_type:
1283 t2 = TYPE_CONTEXT (t2); break;
1284 case tcc_exceptional:
1285 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1286 default: gcc_unreachable ();
766beae1
ZW
1287 }
1288
1289 return t1 == t2;
1290}
1291
f13c9b2c 1292/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1293
f13c9b2c 1294static struct tagged_tu_seen_cache *
58f9752a 1295alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1296{
cceb1885 1297 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1298 tu->next = tagged_tu_seen_base;
1299 tu->t1 = t1;
1300 tu->t2 = t2;
c22cacf3 1301
f13c9b2c 1302 tagged_tu_seen_base = tu;
c22cacf3 1303
f13c9b2c
AP
1304 /* The C standard says that two structures in different translation
1305 units are compatible with each other only if the types of their
1306 fields are compatible (among other things). We assume that they
1307 are compatible until proven otherwise when building the cache.
1308 An example where this can occur is:
1309 struct a
1310 {
1311 struct a *next;
1312 };
1313 If we are comparing this against a similar struct in another TU,
c83eecad 1314 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1315 loop. */
1316 tu->val = 1;
1317 return tu;
1318}
d1bd0ded 1319
f13c9b2c 1320/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1321
f13c9b2c
AP
1322static void
1323free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1324{
1325 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1326 while (tu != tu_til)
1327 {
741ac903
KG
1328 const struct tagged_tu_seen_cache *const tu1
1329 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1330 tu = tu1->next;
b1d5455a 1331 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1332 }
1333 tagged_tu_seen_base = tu_til;
1334}
d1bd0ded
GK
1335
1336/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1337 compatible. If the two types are not the same (which has been
1338 checked earlier), this can only happen when multiple translation
1339 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1340 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1341 comptypes_internal. */
d1bd0ded
GK
1342
1343static int
744aa42f 1344tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1345 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1346{
1347 tree s1, s2;
1348 bool needs_warning = false;
3aeb3655 1349
d1bd0ded
GK
1350 /* We have to verify that the tags of the types are the same. This
1351 is harder than it looks because this may be a typedef, so we have
1352 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1353 typedef...
1354 In the case of compiler-created builtin structs the TYPE_DECL
1355 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1356 while (TYPE_NAME (t1)
1357 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1358 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1359 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1360
dea984dc
ILT
1361 while (TYPE_NAME (t2)
1362 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1363 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1364 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1365
1366 /* C90 didn't have the requirement that the two tags be the same. */
1367 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1368 return 0;
3aeb3655 1369
d1bd0ded
GK
1370 /* C90 didn't say what happened if one or both of the types were
1371 incomplete; we choose to follow C99 rules here, which is that they
1372 are compatible. */
1373 if (TYPE_SIZE (t1) == NULL
1374 || TYPE_SIZE (t2) == NULL)
1375 return 1;
3aeb3655 1376
d1bd0ded 1377 {
f13c9b2c 1378 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1379 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1380 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1381 return tts_i->val;
d1bd0ded 1382 }
3aeb3655 1383
d1bd0ded
GK
1384 switch (TREE_CODE (t1))
1385 {
1386 case ENUMERAL_TYPE:
1387 {
f13c9b2c 1388 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1389 /* Speed up the case where the type values are in the same order. */
1390 tree tv1 = TYPE_VALUES (t1);
1391 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1392
c22cacf3 1393 if (tv1 == tv2)
f13c9b2c
AP
1394 {
1395 return 1;
1396 }
3aeb3655 1397
c22cacf3
MS
1398 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1399 {
1400 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1401 break;
1402 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1403 {
c22cacf3 1404 tu->val = 0;
f13c9b2c
AP
1405 return 0;
1406 }
c22cacf3 1407 }
3aeb3655 1408
c22cacf3 1409 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1410 {
1411 return 1;
1412 }
c22cacf3 1413 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1414 {
1415 tu->val = 0;
1416 return 0;
1417 }
3aeb3655 1418
d1bd0ded 1419 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1420 {
1421 tu->val = 0;
1422 return 0;
1423 }
3aeb3655 1424
d1bd0ded
GK
1425 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1426 {
1427 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1428 if (s2 == NULL
1429 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1430 {
1431 tu->val = 0;
1432 return 0;
1433 }
d1bd0ded
GK
1434 }
1435 return 1;
1436 }
1437
1438 case UNION_TYPE:
1439 {
f13c9b2c 1440 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1441 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1442 {
1443 tu->val = 0;
1444 return 0;
1445 }
c22cacf3 1446
f13c9b2c
AP
1447 /* Speed up the common case where the fields are in the same order. */
1448 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1449 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1450 {
1451 int result;
c22cacf3 1452
3ae4d3cc 1453 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1454 break;
744aa42f 1455 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1456 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1457
1458 if (result != 1 && !DECL_NAME (s1))
1459 break;
f13c9b2c
AP
1460 if (result == 0)
1461 {
1462 tu->val = 0;
1463 return 0;
1464 }
1465 if (result == 2)
1466 needs_warning = true;
1467
1468 if (TREE_CODE (s1) == FIELD_DECL
1469 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1470 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1471 {
1472 tu->val = 0;
1473 return 0;
1474 }
1475 }
1476 if (!s1 && !s2)
1477 {
1478 tu->val = needs_warning ? 2 : 1;
1479 return tu->val;
1480 }
d1bd0ded 1481
910ad8de 1482 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1483 {
1484 bool ok = false;
3aeb3655 1485
910ad8de 1486 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1487 if (DECL_NAME (s1) == DECL_NAME (s2))
1488 {
1489 int result;
1490
744aa42f 1491 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1492 enum_and_int_p,
1493 different_types_p);
3ae4d3cc
AO
1494
1495 if (result != 1 && !DECL_NAME (s1))
1496 continue;
1497 if (result == 0)
1498 {
1499 tu->val = 0;
1500 return 0;
1501 }
1502 if (result == 2)
1503 needs_warning = true;
1504
1505 if (TREE_CODE (s1) == FIELD_DECL
1506 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1507 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1508 break;
3ae4d3cc
AO
1509
1510 ok = true;
1511 break;
1512 }
3f75a254 1513 if (!ok)
f13c9b2c
AP
1514 {
1515 tu->val = 0;
1516 return 0;
1517 }
d1bd0ded 1518 }
f13c9b2c
AP
1519 tu->val = needs_warning ? 2 : 10;
1520 return tu->val;
d1bd0ded
GK
1521 }
1522
1523 case RECORD_TYPE:
1524 {
c22cacf3 1525 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1526
1527 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1528 s1 && s2;
910ad8de 1529 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1530 {
1531 int result;
1532 if (TREE_CODE (s1) != TREE_CODE (s2)
1533 || DECL_NAME (s1) != DECL_NAME (s2))
1534 break;
744aa42f 1535 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1536 enum_and_int_p, different_types_p);
d1bd0ded
GK
1537 if (result == 0)
1538 break;
1539 if (result == 2)
1540 needs_warning = true;
3aeb3655 1541
d1bd0ded
GK
1542 if (TREE_CODE (s1) == FIELD_DECL
1543 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1544 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1545 break;
1546 }
d1bd0ded 1547 if (s1 && s2)
f13c9b2c
AP
1548 tu->val = 0;
1549 else
1550 tu->val = needs_warning ? 2 : 1;
1551 return tu->val;
d1bd0ded
GK
1552 }
1553
1554 default:
366de0ce 1555 gcc_unreachable ();
d1bd0ded
GK
1556 }
1557}
1558
400fbf9f
JW
1559/* Return 1 if two function types F1 and F2 are compatible.
1560 If either type specifies no argument types,
1561 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1562 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1563 the other must specify that number of self-promoting arg types.
744aa42f 1564 Otherwise, the argument types must match.
dc5027f4 1565 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1566
1567static int
744aa42f 1568function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1569 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1570{
1571 tree args1, args2;
1572 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1573 int val = 1;
1574 int val1;
a6fdc086
GK
1575 tree ret1, ret2;
1576
1577 ret1 = TREE_TYPE (f1);
1578 ret2 = TREE_TYPE (f2);
1579
e508a019
JM
1580 /* 'volatile' qualifiers on a function's return type used to mean
1581 the function is noreturn. */
1582 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1583 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1584 if (TYPE_VOLATILE (ret1))
1585 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1586 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1587 if (TYPE_VOLATILE (ret2))
1588 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1589 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1590 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1591 if (val == 0)
400fbf9f
JW
1592 return 0;
1593
1594 args1 = TYPE_ARG_TYPES (f1);
1595 args2 = TYPE_ARG_TYPES (f2);
1596
dc5027f4
JM
1597 if (different_types_p != NULL
1598 && (args1 == 0) != (args2 == 0))
1599 *different_types_p = true;
1600
400fbf9f
JW
1601 /* An unspecified parmlist matches any specified parmlist
1602 whose argument types don't need default promotions. */
1603
1604 if (args1 == 0)
1605 {
1606 if (!self_promoting_args_p (args2))
1607 return 0;
1608 /* If one of these types comes from a non-prototype fn definition,
1609 compare that with the other type's arglist.
3176a0c2 1610 If they don't match, ask for a warning (but no error). */
400fbf9f 1611 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1612 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1613 enum_and_int_p, different_types_p))
400fbf9f
JW
1614 val = 2;
1615 return val;
1616 }
1617 if (args2 == 0)
1618 {
1619 if (!self_promoting_args_p (args1))
1620 return 0;
1621 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1622 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1623 enum_and_int_p, different_types_p))
400fbf9f
JW
1624 val = 2;
1625 return val;
1626 }
1627
1628 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1629 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1630 different_types_p);
400fbf9f
JW
1631 return val1 != 1 ? val1 : val;
1632}
1633
744aa42f
ILT
1634/* Check two lists of types for compatibility, returning 0 for
1635 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1636 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1637 comptypes_internal. */
400fbf9f
JW
1638
1639static int
744aa42f 1640type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1641 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1642{
1643 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1644 int val = 1;
9d5f3e49 1645 int newval = 0;
400fbf9f
JW
1646
1647 while (1)
1648 {
46df2823 1649 tree a1, mv1, a2, mv2;
400fbf9f
JW
1650 if (args1 == 0 && args2 == 0)
1651 return val;
1652 /* If one list is shorter than the other,
1653 they fail to match. */
1654 if (args1 == 0 || args2 == 0)
1655 return 0;
46df2823
JM
1656 mv1 = a1 = TREE_VALUE (args1);
1657 mv2 = a2 = TREE_VALUE (args2);
1658 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1659 mv1 = (TYPE_ATOMIC (mv1)
1660 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1661 TYPE_QUAL_ATOMIC)
1662 : TYPE_MAIN_VARIANT (mv1));
46df2823 1663 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1664 mv2 = (TYPE_ATOMIC (mv2)
1665 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1666 TYPE_QUAL_ATOMIC)
1667 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1668 /* A null pointer instead of a type
1669 means there is supposed to be an argument
1670 but nothing is specified about what type it has.
1671 So match anything that self-promotes. */
dc5027f4
JM
1672 if (different_types_p != NULL
1673 && (a1 == 0) != (a2 == 0))
1674 *different_types_p = true;
46df2823 1675 if (a1 == 0)
400fbf9f 1676 {
46df2823 1677 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1678 return 0;
1679 }
46df2823 1680 else if (a2 == 0)
400fbf9f 1681 {
46df2823 1682 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1683 return 0;
1684 }
8f5b6d29 1685 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1686 else if (TREE_CODE (a1) == ERROR_MARK
1687 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1688 ;
dc5027f4
JM
1689 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1690 different_types_p)))
400fbf9f 1691 {
dc5027f4
JM
1692 if (different_types_p != NULL)
1693 *different_types_p = true;
400fbf9f
JW
1694 /* Allow wait (union {union wait *u; int *i} *)
1695 and wait (union wait *) to be compatible. */
46df2823
JM
1696 if (TREE_CODE (a1) == UNION_TYPE
1697 && (TYPE_NAME (a1) == 0
ebf0bf7f 1698 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1699 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1700 && tree_int_cst_equal (TYPE_SIZE (a1),
1701 TYPE_SIZE (a2)))
400fbf9f
JW
1702 {
1703 tree memb;
46df2823 1704 for (memb = TYPE_FIELDS (a1);
910ad8de 1705 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1706 {
1707 tree mv3 = TREE_TYPE (memb);
1708 if (mv3 && mv3 != error_mark_node
1709 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1710 mv3 = (TYPE_ATOMIC (mv3)
1711 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1712 TYPE_QUAL_ATOMIC)
1713 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1714 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1715 different_types_p))
58cb41e6
JJ
1716 break;
1717 }
400fbf9f
JW
1718 if (memb == 0)
1719 return 0;
1720 }
46df2823
JM
1721 else if (TREE_CODE (a2) == UNION_TYPE
1722 && (TYPE_NAME (a2) == 0
ebf0bf7f 1723 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1724 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1725 && tree_int_cst_equal (TYPE_SIZE (a2),
1726 TYPE_SIZE (a1)))
400fbf9f
JW
1727 {
1728 tree memb;
46df2823 1729 for (memb = TYPE_FIELDS (a2);
910ad8de 1730 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1731 {
1732 tree mv3 = TREE_TYPE (memb);
1733 if (mv3 && mv3 != error_mark_node
1734 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1735 mv3 = (TYPE_ATOMIC (mv3)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1739 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1740 different_types_p))
58cb41e6
JJ
1741 break;
1742 }
400fbf9f
JW
1743 if (memb == 0)
1744 return 0;
1745 }
1746 else
1747 return 0;
1748 }
1749
1750 /* comptypes said ok, but record if it said to warn. */
1751 if (newval > val)
1752 val = newval;
1753
1754 args1 = TREE_CHAIN (args1);
1755 args2 = TREE_CHAIN (args2);
1756 }
1757}
400fbf9f 1758\f
a0e24419
MP
1759/* Compute the size to increment a pointer by. When a function type or void
1760 type or incomplete type is passed, size_one_node is returned.
1761 This function does not emit any diagnostics; the caller is responsible
1762 for that. */
400fbf9f 1763
4e2fb7de 1764static tree
58f9752a 1765c_size_in_bytes (const_tree type)
400fbf9f
JW
1766{
1767 enum tree_code code = TREE_CODE (type);
1768
a0e24419
MP
1769 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1770 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1771 return size_one_node;
1772
400fbf9f 1773 /* Convert in case a char is more than one unit. */
db3927fb
AH
1774 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1775 size_int (TYPE_PRECISION (char_type_node)
1776 / BITS_PER_UNIT));
400fbf9f 1777}
400fbf9f 1778\f
400fbf9f
JW
1779/* Return either DECL or its known constant value (if it has one). */
1780
56cb9733 1781tree
2f6e4e97 1782decl_constant_value (tree decl)
400fbf9f 1783{
a7c1916a 1784 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1785 in a place where a variable is invalid. Note that DECL_INITIAL
1786 isn't valid for a PARM_DECL. */
a7c1916a 1787 current_function_decl != 0
4f976745 1788 && TREE_CODE (decl) != PARM_DECL
3f75a254 1789 && !TREE_THIS_VOLATILE (decl)
83bab8db 1790 && TREE_READONLY (decl)
400fbf9f
JW
1791 && DECL_INITIAL (decl) != 0
1792 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1793 /* This is invalid if initial value is not constant.
1794 If it has either a function call, a memory reference,
1795 or a variable, then re-evaluating it could give different results. */
1796 && TREE_CONSTANT (DECL_INITIAL (decl))
1797 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1798 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1799 return DECL_INITIAL (decl);
1800 return decl;
1801}
1802
f2a71bbc
JM
1803/* Convert the array expression EXP to a pointer. */
1804static tree
c2255bc4 1805array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1806{
f2a71bbc 1807 tree orig_exp = exp;
207bf485 1808 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1809 tree adr;
1810 tree restype = TREE_TYPE (type);
1811 tree ptrtype;
207bf485 1812
f2a71bbc 1813 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1814
f2a71bbc 1815 STRIP_TYPE_NOPS (exp);
207bf485 1816
487a92fe
JM
1817 if (TREE_NO_WARNING (orig_exp))
1818 TREE_NO_WARNING (exp) = 1;
207bf485 1819
f2a71bbc
JM
1820 ptrtype = build_pointer_type (restype);
1821
22d03525 1822 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1823 return convert (ptrtype, TREE_OPERAND (exp, 0));
1824
1f37c583
JM
1825 /* In C++ array compound literals are temporary objects unless they are
1826 const or appear in namespace scope, so they are destroyed too soon
1827 to use them for much of anything (c++/53220). */
1828 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1829 {
1830 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1831 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1832 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1833 "converting an array compound literal to a pointer "
1834 "is ill-formed in C++");
1835 }
1836
c2255bc4 1837 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1838 return convert (ptrtype, adr);
1839}
207bf485 1840
f2a71bbc
JM
1841/* Convert the function expression EXP to a pointer. */
1842static tree
c2255bc4 1843function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1844{
1845 tree orig_exp = exp;
207bf485 1846
f2a71bbc 1847 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1848
f2a71bbc 1849 STRIP_TYPE_NOPS (exp);
207bf485 1850
f2a71bbc
JM
1851 if (TREE_NO_WARNING (orig_exp))
1852 TREE_NO_WARNING (exp) = 1;
207bf485 1853
c2255bc4 1854 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1855}
207bf485 1856
ebfbbdc5
JJ
1857/* Mark EXP as read, not just set, for set but not used -Wunused
1858 warning purposes. */
1859
1860void
1861mark_exp_read (tree exp)
1862{
1863 switch (TREE_CODE (exp))
1864 {
1865 case VAR_DECL:
1866 case PARM_DECL:
1867 DECL_READ_P (exp) = 1;
1868 break;
1869 case ARRAY_REF:
1870 case COMPONENT_REF:
1871 case MODIFY_EXPR:
1872 case REALPART_EXPR:
1873 case IMAGPART_EXPR:
1874 CASE_CONVERT:
1875 case ADDR_EXPR:
1876 mark_exp_read (TREE_OPERAND (exp, 0));
1877 break;
1878 case COMPOUND_EXPR:
82c3c067 1879 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1880 mark_exp_read (TREE_OPERAND (exp, 1));
1881 break;
1882 default:
1883 break;
1884 }
1885}
1886
f2a71bbc
JM
1887/* Perform the default conversion of arrays and functions to pointers.
1888 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1889 return EXP.
1890
1891 LOC is the location of the expression. */
f2a71bbc
JM
1892
1893struct c_expr
c2255bc4 1894default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1895{
1896 tree orig_exp = exp.value;
1897 tree type = TREE_TYPE (exp.value);
1898 enum tree_code code = TREE_CODE (type);
1899
1900 switch (code)
1901 {
1902 case ARRAY_TYPE:
1903 {
1904 bool not_lvalue = false;
1905 bool lvalue_array_p;
1906
1907 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1908 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1909 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1910 {
1911 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1912 not_lvalue = true;
1913 exp.value = TREE_OPERAND (exp.value, 0);
1914 }
1915
1916 if (TREE_NO_WARNING (orig_exp))
1917 TREE_NO_WARNING (exp.value) = 1;
1918
1919 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1920 if (!flag_isoc99 && !lvalue_array_p)
1921 {
1922 /* Before C99, non-lvalue arrays do not decay to pointers.
1923 Normally, using such an array would be invalid; but it can
1924 be used correctly inside sizeof or as a statement expression.
1925 Thus, do not give an error here; an error will result later. */
1926 return exp;
1927 }
1928
c2255bc4 1929 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1930 }
1931 break;
1932 case FUNCTION_TYPE:
c2255bc4 1933 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1934 break;
1935 default:
f2a71bbc 1936 break;
207bf485 1937 }
f2a71bbc 1938
207bf485
JM
1939 return exp;
1940}
1941
ebfbbdc5
JJ
1942struct c_expr
1943default_function_array_read_conversion (location_t loc, struct c_expr exp)
1944{
1945 mark_exp_read (exp.value);
1946 return default_function_array_conversion (loc, exp);
1947}
522ddfa2 1948
267bac10
JM
1949/* Return whether EXPR should be treated as an atomic lvalue for the
1950 purposes of load and store handling. */
1951
1952static bool
1953really_atomic_lvalue (tree expr)
1954{
7a0ca710 1955 if (error_operand_p (expr))
267bac10
JM
1956 return false;
1957 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1958 return false;
1959 if (!lvalue_p (expr))
1960 return false;
1961
1962 /* Ignore _Atomic on register variables, since their addresses can't
1963 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1964 sequences wouldn't work. Ignore _Atomic on structures containing
1965 bit-fields, since accessing elements of atomic structures or
1966 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1967 it's undefined at translation time or execution time, and the
1968 normal atomic sequences again wouldn't work. */
1969 while (handled_component_p (expr))
1970 {
1971 if (TREE_CODE (expr) == COMPONENT_REF
1972 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1973 return false;
1974 expr = TREE_OPERAND (expr, 0);
1975 }
1976 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1977 return false;
1978 return true;
1979}
1980
1981/* Convert expression EXP (location LOC) from lvalue to rvalue,
1982 including converting functions and arrays to pointers if CONVERT_P.
1983 If READ_P, also mark the expression as having been read. */
1984
1985struct c_expr
1986convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1987 bool convert_p, bool read_p)
1988{
1989 if (read_p)
1990 mark_exp_read (exp.value);
1991 if (convert_p)
1992 exp = default_function_array_conversion (loc, exp);
1993 if (really_atomic_lvalue (exp.value))
1994 {
1995 vec<tree, va_gc> *params;
1996 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1997 tree expr_type = TREE_TYPE (exp.value);
1998 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1999 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2000
2001 gcc_assert (TYPE_ATOMIC (expr_type));
2002
2003 /* Expansion of a generic atomic load may require an addition
2004 element, so allocate enough to prevent a resize. */
2005 vec_alloc (params, 4);
2006
2007 /* Remove the qualifiers for the rest of the expressions and
2008 create the VAL temp variable to hold the RHS. */
2009 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2010 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2011 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2012 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2013 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2014
2015 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2016 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2017 params->quick_push (expr_addr);
2018 params->quick_push (tmp_addr);
2019 params->quick_push (seq_cst);
8edbfaa6 2020 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2021
cc28fc7f
MP
2022 /* EXPR is always read. */
2023 mark_exp_read (exp.value);
2024
267bac10 2025 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2026 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2027 NULL_TREE, NULL_TREE);
267bac10
JM
2028 }
2029 return exp;
2030}
2031
522ddfa2
JM
2032/* EXP is an expression of integer type. Apply the integer promotions
2033 to it and return the promoted value. */
400fbf9f
JW
2034
2035tree
522ddfa2 2036perform_integral_promotions (tree exp)
400fbf9f 2037{
b3694847
SS
2038 tree type = TREE_TYPE (exp);
2039 enum tree_code code = TREE_CODE (type);
400fbf9f 2040
522ddfa2 2041 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2042
400fbf9f
JW
2043 /* Normally convert enums to int,
2044 but convert wide enums to something wider. */
2045 if (code == ENUMERAL_TYPE)
2046 {
b0c48229
NB
2047 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2048 TYPE_PRECISION (integer_type_node)),
2049 ((TYPE_PRECISION (type)
2050 >= TYPE_PRECISION (integer_type_node))
8df83eae 2051 && TYPE_UNSIGNED (type)));
05bccae2 2052
400fbf9f
JW
2053 return convert (type, exp);
2054 }
2055
522ddfa2
JM
2056 /* ??? This should no longer be needed now bit-fields have their
2057 proper types. */
9753f113 2058 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2059 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2060 /* If it's thinner than an int, promote it like a
d72040f5 2061 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2062 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2063 TYPE_PRECISION (integer_type_node)))
f458d1d5 2064 return convert (integer_type_node, exp);
9753f113 2065
d72040f5 2066 if (c_promoting_integer_type_p (type))
400fbf9f 2067 {
f458d1d5 2068 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2069 if (TYPE_UNSIGNED (type)
f458d1d5 2070 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2071 return convert (unsigned_type_node, exp);
05bccae2 2072
400fbf9f
JW
2073 return convert (integer_type_node, exp);
2074 }
05bccae2 2075
522ddfa2
JM
2076 return exp;
2077}
2078
2079
2080/* Perform default promotions for C data used in expressions.
46bdb9cf 2081 Enumeral types or short or char are converted to int.
522ddfa2
JM
2082 In addition, manifest constants symbols are replaced by their values. */
2083
2084tree
2085default_conversion (tree exp)
2086{
2087 tree orig_exp;
2088 tree type = TREE_TYPE (exp);
2089 enum tree_code code = TREE_CODE (type);
40449a90 2090 tree promoted_type;
522ddfa2 2091
ebfbbdc5
JJ
2092 mark_exp_read (exp);
2093
46bdb9cf
JM
2094 /* Functions and arrays have been converted during parsing. */
2095 gcc_assert (code != FUNCTION_TYPE);
2096 if (code == ARRAY_TYPE)
2097 return exp;
522ddfa2
JM
2098
2099 /* Constants can be used directly unless they're not loadable. */
2100 if (TREE_CODE (exp) == CONST_DECL)
2101 exp = DECL_INITIAL (exp);
2102
522ddfa2
JM
2103 /* Strip no-op conversions. */
2104 orig_exp = exp;
2105 STRIP_TYPE_NOPS (exp);
2106
2107 if (TREE_NO_WARNING (orig_exp))
2108 TREE_NO_WARNING (exp) = 1;
2109
400fbf9f
JW
2110 if (code == VOID_TYPE)
2111 {
5436fa2e
MP
2112 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2113 "void value not ignored as it ought to be");
400fbf9f
JW
2114 return error_mark_node;
2115 }
808d6eaa
JM
2116
2117 exp = require_complete_type (exp);
2118 if (exp == error_mark_node)
2119 return error_mark_node;
2120
40449a90
SL
2121 promoted_type = targetm.promoted_type (type);
2122 if (promoted_type)
2123 return convert (promoted_type, exp);
2124
808d6eaa
JM
2125 if (INTEGRAL_TYPE_P (type))
2126 return perform_integral_promotions (exp);
2127
400fbf9f
JW
2128 return exp;
2129}
2130\f
0fb96aa4 2131/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2132
2133 If the component name is not found, returns NULL_TREE. Otherwise,
2134 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2135 stepping down the chain to the component, which is in the last
2136 TREE_VALUE of the list. Normally the list is of length one, but if
2137 the component is embedded within (nested) anonymous structures or
2138 unions, the list steps down the chain to the component. */
2f6e4e97 2139
2f2d13da 2140static tree
0fb96aa4 2141lookup_field (tree type, tree component)
2f2d13da
DE
2142{
2143 tree field;
2144
2145 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2146 to the field elements. Use a binary search on this array to quickly
2147 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2148 will always be set for structures which have many elements. */
2149
22a0b85f 2150 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2151 {
2152 int bot, top, half;
d07605f5 2153 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2154
2155 field = TYPE_FIELDS (type);
2156 bot = 0;
d07605f5 2157 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2158 while (top - bot > 1)
2159 {
2f2d13da
DE
2160 half = (top - bot + 1) >> 1;
2161 field = field_array[bot+half];
2162
2163 if (DECL_NAME (field) == NULL_TREE)
2164 {
2165 /* Step through all anon unions in linear fashion. */
2166 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2167 {
2f2d13da 2168 field = field_array[bot++];
3e636daf 2169 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2170 {
0fb96aa4 2171 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2172
2173 if (anon)
2174 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2175
2176 /* The Plan 9 compiler permits referring
2177 directly to an anonymous struct/union field
2178 using a typedef name. */
2179 if (flag_plan9_extensions
2180 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2181 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2182 == TYPE_DECL)
2183 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2184 == component))
2185 break;
2f6e4e97 2186 }
2f2d13da
DE
2187 }
2188
2189 /* Entire record is only anon unions. */
2190 if (bot > top)
2191 return NULL_TREE;
2192
2193 /* Restart the binary search, with new lower bound. */
2194 continue;
2195 }
2196
e8b87aac 2197 if (DECL_NAME (field) == component)
2f2d13da 2198 break;
e8b87aac 2199 if (DECL_NAME (field) < component)
2f2d13da
DE
2200 bot += half;
2201 else
2202 top = bot + half;
2203 }
2204
2205 if (DECL_NAME (field_array[bot]) == component)
2206 field = field_array[bot];
2207 else if (DECL_NAME (field) != component)
e9b2c823 2208 return NULL_TREE;
2f2d13da
DE
2209 }
2210 else
2211 {
910ad8de 2212 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2213 {
e9b2c823 2214 if (DECL_NAME (field) == NULL_TREE
3e636daf 2215 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2216 {
0fb96aa4 2217 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2218
e9b2c823
NB
2219 if (anon)
2220 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2221
2222 /* The Plan 9 compiler permits referring directly to an
2223 anonymous struct/union field using a typedef
2224 name. */
2225 if (flag_plan9_extensions
2226 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2227 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2228 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2229 == component))
2230 break;
2f2d13da
DE
2231 }
2232
2233 if (DECL_NAME (field) == component)
2234 break;
2235 }
e9b2c823
NB
2236
2237 if (field == NULL_TREE)
2238 return NULL_TREE;
2f2d13da
DE
2239 }
2240
e9b2c823 2241 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2242}
2243
277fe616
DM
2244/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2245
2246static void
2247lookup_field_fuzzy_find_candidates (tree type, tree component,
2248 vec<tree> *candidates)
2249{
2250 tree field;
2251 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2252 {
2253 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2254 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2255 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2256 candidates);
277fe616
DM
2257
2258 if (DECL_NAME (field))
2259 candidates->safe_push (DECL_NAME (field));
2260 }
2261}
2262
2263/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2264 rather than returning a TREE_LIST for an exact match. */
2265
2266static tree
2267lookup_field_fuzzy (tree type, tree component)
2268{
2269 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2270
2271 /* First, gather a list of candidates. */
2272 auto_vec <tree> candidates;
2273
2274 lookup_field_fuzzy_find_candidates (type, component,
2275 &candidates);
2276
2277 /* Now determine which is closest. */
2278 int i;
2279 tree identifier;
2280 tree best_identifier = NULL;
2281 edit_distance_t best_distance = MAX_EDIT_DISTANCE;
2282 FOR_EACH_VEC_ELT (candidates, i, identifier)
2283 {
2284 gcc_assert (TREE_CODE (identifier) == IDENTIFIER_NODE);
2285 edit_distance_t dist = levenshtein_distance (component, identifier);
2286 if (dist < best_distance)
2287 {
2288 best_distance = dist;
2289 best_identifier = identifier;
2290 }
2291 }
2292
2293 /* If more than half of the letters were misspelled, the suggestion is
2294 likely to be meaningless. */
2295 if (best_identifier)
2296 {
2297 unsigned int cutoff = MAX (IDENTIFIER_LENGTH (component),
2298 IDENTIFIER_LENGTH (best_identifier)) / 2;
2299 if (best_distance > cutoff)
2300 return NULL;
2301 }
2302
2303 return best_identifier;
2304}
2305
c2255bc4
AH
2306/* Make an expression to refer to the COMPONENT field of structure or
2307 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2308 location of the COMPONENT_REF. */
400fbf9f
JW
2309
2310tree
c2255bc4 2311build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2312{
b3694847
SS
2313 tree type = TREE_TYPE (datum);
2314 enum tree_code code = TREE_CODE (type);
2315 tree field = NULL;
2316 tree ref;
1e57bf47 2317 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2318
7a3ea201
RH
2319 if (!objc_is_public (datum, component))
2320 return error_mark_node;
2321
46a88c12 2322 /* Detect Objective-C property syntax object.property. */
668ea4b1 2323 if (c_dialect_objc ()
46a88c12 2324 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2325 return ref;
2326
400fbf9f
JW
2327 /* See if there is a field or component with name COMPONENT. */
2328
2329 if (code == RECORD_TYPE || code == UNION_TYPE)
2330 {
d0f062fb 2331 if (!COMPLETE_TYPE_P (type))
400fbf9f 2332 {
7a228918 2333 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2334 return error_mark_node;
2335 }
2336
0fb96aa4 2337 field = lookup_field (type, component);
400fbf9f
JW
2338
2339 if (!field)
2340 {
277fe616
DM
2341 tree guessed_id = lookup_field_fuzzy (type, component);
2342 if (guessed_id)
2343 error_at (loc, "%qT has no member named %qE; did you mean %qE?",
2344 type, component, guessed_id);
2345 else
2346 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2347 return error_mark_node;
2348 }
400fbf9f 2349
e9b2c823
NB
2350 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2351 This might be better solved in future the way the C++ front
2352 end does it - by giving the anonymous entities each a
2353 separate name and type, and then have build_component_ref
2354 recursively call itself. We can't do that here. */
46ea50cb 2355 do
19d76e60 2356 {
e9b2c823 2357 tree subdatum = TREE_VALUE (field);
efed193e
JM
2358 int quals;
2359 tree subtype;
1e57bf47 2360 bool use_datum_quals;
e9b2c823
NB
2361
2362 if (TREE_TYPE (subdatum) == error_mark_node)
2363 return error_mark_node;
2364
1e57bf47
JM
2365 /* If this is an rvalue, it does not have qualifiers in C
2366 standard terms and we must avoid propagating such
2367 qualifiers down to a non-lvalue array that is then
2368 converted to a pointer. */
2369 use_datum_quals = (datum_lvalue
2370 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2371
efed193e 2372 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2373 if (use_datum_quals)
2374 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2375 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2376
2377 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2378 NULL_TREE);
c2255bc4 2379 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2380 if (TREE_READONLY (subdatum)
2381 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2382 TREE_READONLY (ref) = 1;
1e57bf47
JM
2383 if (TREE_THIS_VOLATILE (subdatum)
2384 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2385 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2386
2387 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2388 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2389
19d76e60 2390 datum = ref;
46ea50cb
RS
2391
2392 field = TREE_CHAIN (field);
19d76e60 2393 }
46ea50cb 2394 while (field);
19d76e60 2395
400fbf9f
JW
2396 return ref;
2397 }
2398 else if (code != ERROR_MARK)
c2255bc4
AH
2399 error_at (loc,
2400 "request for member %qE in something not a structure or union",
2401 component);
400fbf9f
JW
2402
2403 return error_mark_node;
2404}
2405\f
2406/* Given an expression PTR for a pointer, return an expression
2407 for the value pointed to.
6a3799eb
AH
2408 ERRORSTRING is the name of the operator to appear in error messages.
2409
2410 LOC is the location to use for the generated tree. */
400fbf9f
JW
2411
2412tree
dd865ef6 2413build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2414{
b3694847
SS
2415 tree pointer = default_conversion (ptr);
2416 tree type = TREE_TYPE (pointer);
6a3799eb 2417 tree ref;
400fbf9f
JW
2418
2419 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2420 {
1043771b 2421 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2422 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2423 {
2424 /* If a warning is issued, mark it to avoid duplicates from
2425 the backend. This only needs to be done at
2426 warn_strict_aliasing > 2. */
2427 if (warn_strict_aliasing > 2)
2428 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2429 type, TREE_OPERAND (pointer, 0)))
2430 TREE_NO_WARNING (pointer) = 1;
2431 }
2432
870cc33b 2433 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2434 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2435 == TREE_TYPE (type)))
6a3799eb
AH
2436 {
2437 ref = TREE_OPERAND (pointer, 0);
2438 protected_set_expr_location (ref, loc);
2439 return ref;
2440 }
870cc33b
RS
2441 else
2442 {
2443 tree t = TREE_TYPE (type);
46df2823 2444
984dfd8c 2445 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2446
baae9b65 2447 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2448 {
d9b7be2e
MP
2449 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2450 {
2451 error_at (loc, "dereferencing pointer to incomplete type "
2452 "%qT", t);
2453 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2454 }
870cc33b
RS
2455 return error_mark_node;
2456 }
7d882b83 2457 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2458 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2459
2460 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2461 so that we get the proper error message if the result is used
2462 to assign to. Also, &* is supposed to be a no-op.
2463 And ANSI C seems to specify that the type of the result
2464 should be the const type. */
2465 /* A de-reference of a pointer to const is not a const. It is valid
2466 to change it via some other pointer. */
2467 TREE_READONLY (ref) = TYPE_READONLY (t);
2468 TREE_SIDE_EFFECTS (ref)
271bd540 2469 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2470 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2471 protected_set_expr_location (ref, loc);
870cc33b
RS
2472 return ref;
2473 }
2474 }
400fbf9f 2475 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2476 invalid_indirection_error (loc, type, errstring);
2477
400fbf9f
JW
2478 return error_mark_node;
2479}
2480
2481/* This handles expressions of the form "a[i]", which denotes
2482 an array reference.
2483
2484 This is logically equivalent in C to *(a+i), but we may do it differently.
2485 If A is a variable or a member, we generate a primitive ARRAY_REF.
2486 This avoids forcing the array out of registers, and can work on
2487 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2488 by functions).
2489
30cd1c5d
AS
2490 For vector types, allow vector[i] but not i[vector], and create
2491 *(((type*)&vectortype) + i) for the expression.
2492
6a3799eb 2493 LOC is the location to use for the returned expression. */
400fbf9f
JW
2494
2495tree
c2255bc4 2496build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2497{
6a3799eb 2498 tree ret;
a4ab7973 2499 bool swapped = false;
400fbf9f
JW
2500 if (TREE_TYPE (array) == error_mark_node
2501 || TREE_TYPE (index) == error_mark_node)
2502 return error_mark_node;
2503
b72271b9 2504 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2505 {
2506 size_t rank = 0;
2507 if (!find_rank (loc, index, index, true, &rank))
2508 return error_mark_node;
2509 if (rank > 1)
2510 {
2511 error_at (loc, "rank of the array's index is greater than 1");
2512 return error_mark_node;
2513 }
2514 }
a4ab7973 2515 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2516 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2517 /* Allow vector[index] but not index[vector]. */
31521951 2518 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2519 {
a4ab7973
JM
2520 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2521 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2522 {
f90e8e2e 2523 error_at (loc,
30cd1c5d
AS
2524 "subscripted value is neither array nor pointer nor vector");
2525
fdeefd49
RS
2526 return error_mark_node;
2527 }
fab27f52 2528 std::swap (array, index);
a4ab7973
JM
2529 swapped = true;
2530 }
2531
2532 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2533 {
a63068b6 2534 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2535 return error_mark_node;
2536 }
2537
2538 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2539 {
a63068b6 2540 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2541 return error_mark_node;
2542 }
2543
ff6b6641
GDR
2544 /* ??? Existing practice has been to warn only when the char
2545 index is syntactically the index, not for char[array]. */
2546 if (!swapped)
5bd012f8 2547 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2548
2549 /* Apply default promotions *after* noticing character types. */
2550 index = default_conversion (index);
f406ae1f
MP
2551 if (index == error_mark_node)
2552 return error_mark_node;
a4ab7973
JM
2553
2554 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2555
aa7da51a
JJ
2556 bool non_lvalue
2557 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2558
2559 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2560 {
e4d35515 2561 tree rval, type;
fdeefd49 2562
400fbf9f
JW
2563 /* An array that is indexed by a non-constant
2564 cannot be stored in a register; we must be able to do
2565 address arithmetic on its address.
2566 Likewise an array of elements of variable size. */
2567 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2568 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2569 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2570 {
dffd7eb6 2571 if (!c_mark_addressable (array))
400fbf9f
JW
2572 return error_mark_node;
2573 }
e6d52559
JW
2574 /* An array that is indexed by a constant value which is not within
2575 the array bounds cannot be stored in a register either; because we
2576 would get a crash in store_bit_field/extract_bit_field when trying
2577 to access a non-existent part of the register. */
2578 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2579 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2580 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2581 {
dffd7eb6 2582 if (!c_mark_addressable (array))
e6d52559
JW
2583 return error_mark_node;
2584 }
400fbf9f 2585
f3bede71 2586 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2587 {
2588 tree foo = array;
2589 while (TREE_CODE (foo) == COMPONENT_REF)
2590 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2591 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2592 pedwarn (loc, OPT_Wpedantic,
fcf73884 2593 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2594 else if (!lvalue_p (foo))
2595 pedwarn_c90 (loc, OPT_Wpedantic,
2596 "ISO C90 forbids subscripting non-lvalue "
2597 "array");
400fbf9f
JW
2598 }
2599
46df2823 2600 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2601 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2602 /* Array ref is const/volatile if the array elements are
c22cacf3 2603 or if the array is. */
400fbf9f
JW
2604 TREE_READONLY (rval)
2605 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2606 | TREE_READONLY (array));
2607 TREE_SIDE_EFFECTS (rval)
2608 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2609 | TREE_SIDE_EFFECTS (array));
2610 TREE_THIS_VOLATILE (rval)
2611 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2612 /* This was added by rms on 16 Nov 91.
2f6e4e97 2613 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2614 in an inline function.
2615 Hope it doesn't break something else. */
2616 | TREE_THIS_VOLATILE (array));
928c19bb 2617 ret = require_complete_type (rval);
6a3799eb 2618 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2619 if (non_lvalue)
2620 ret = non_lvalue_loc (loc, ret);
6a3799eb 2621 return ret;
400fbf9f 2622 }
a4ab7973
JM
2623 else
2624 {
2625 tree ar = default_conversion (array);
400fbf9f 2626
a4ab7973
JM
2627 if (ar == error_mark_node)
2628 return ar;
400fbf9f 2629
a4ab7973
JM
2630 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2631 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2632
aa7da51a
JJ
2633 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2634 index, 0),
2635 RO_ARRAY_INDEXING);
2636 if (non_lvalue)
2637 ret = non_lvalue_loc (loc, ret);
2638 return ret;
a4ab7973 2639 }
400fbf9f
JW
2640}
2641\f
7e585d16 2642/* Build an external reference to identifier ID. FUN indicates
766beb40 2643 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2644 location of the identifier. This sets *TYPE to the type of the
2645 identifier, which is not the same as the type of the returned value
2646 for CONST_DECLs defined as enum constants. If the type of the
2647 identifier is not available, *TYPE is set to NULL. */
7e585d16 2648tree
c2255bc4 2649build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2650{
2651 tree ref;
2652 tree decl = lookup_name (id);
16b34ad6
ZL
2653
2654 /* In Objective-C, an instance variable (ivar) may be preferred to
2655 whatever lookup_name() found. */
2656 decl = objc_lookup_ivar (decl, id);
7e585d16 2657
6866c6e8 2658 *type = NULL;
339a28b9 2659 if (decl && decl != error_mark_node)
6866c6e8
ILT
2660 {
2661 ref = decl;
2662 *type = TREE_TYPE (ref);
2663 }
339a28b9
ZW
2664 else if (fun)
2665 /* Implicit function declaration. */
c2255bc4 2666 ref = implicitly_declare (loc, id);
339a28b9
ZW
2667 else if (decl == error_mark_node)
2668 /* Don't complain about something that's already been
2669 complained about. */
2670 return error_mark_node;
2671 else
2672 {
c2255bc4 2673 undeclared_variable (loc, id);
339a28b9
ZW
2674 return error_mark_node;
2675 }
7e585d16
ZW
2676
2677 if (TREE_TYPE (ref) == error_mark_node)
2678 return error_mark_node;
2679
339a28b9 2680 if (TREE_DEPRECATED (ref))
9b86d6bb 2681 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2682
ad960f56 2683 /* Recursive call does not count as usage. */
b8698a0f 2684 if (ref != current_function_decl)
ad960f56 2685 {
ad960f56
MLI
2686 TREE_USED (ref) = 1;
2687 }
7e585d16 2688
bc4b653b
JM
2689 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2690 {
2691 if (!in_sizeof && !in_typeof)
2692 C_DECL_USED (ref) = 1;
2693 else if (DECL_INITIAL (ref) == 0
2694 && DECL_EXTERNAL (ref)
2695 && !TREE_PUBLIC (ref))
2696 record_maybe_used_decl (ref);
2697 }
2698
7e585d16
ZW
2699 if (TREE_CODE (ref) == CONST_DECL)
2700 {
6193b8b7 2701 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2702
2703 if (warn_cxx_compat
2704 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2705 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2706 {
2707 warning_at (loc, OPT_Wc___compat,
2708 ("enum constant defined in struct or union "
2709 "is not visible in C++"));
2710 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2711 }
2712
7e585d16
ZW
2713 ref = DECL_INITIAL (ref);
2714 TREE_CONSTANT (ref) = 1;
2715 }
6a29edea 2716 else if (current_function_decl != 0
4b1e44be 2717 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2718 && (VAR_OR_FUNCTION_DECL_P (ref)
2719 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2720 {
2721 tree context = decl_function_context (ref);
2f6e4e97 2722
6a29edea
EB
2723 if (context != 0 && context != current_function_decl)
2724 DECL_NONLOCAL (ref) = 1;
2725 }
71113fcd
GK
2726 /* C99 6.7.4p3: An inline definition of a function with external
2727 linkage ... shall not contain a reference to an identifier with
2728 internal linkage. */
2729 else if (current_function_decl != 0
2730 && DECL_DECLARED_INLINE_P (current_function_decl)
2731 && DECL_EXTERNAL (current_function_decl)
2732 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2733 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2734 && ! TREE_PUBLIC (ref)
2735 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2736 record_inline_static (loc, current_function_decl, ref,
2737 csi_internal);
7e585d16
ZW
2738
2739 return ref;
2740}
2741
bc4b653b
JM
2742/* Record details of decls possibly used inside sizeof or typeof. */
2743struct maybe_used_decl
2744{
2745 /* The decl. */
2746 tree decl;
2747 /* The level seen at (in_sizeof + in_typeof). */
2748 int level;
2749 /* The next one at this level or above, or NULL. */
2750 struct maybe_used_decl *next;
2751};
2752
2753static struct maybe_used_decl *maybe_used_decls;
2754
2755/* Record that DECL, an undefined static function reference seen
2756 inside sizeof or typeof, might be used if the operand of sizeof is
2757 a VLA type or the operand of typeof is a variably modified
2758 type. */
2759
4e2fb7de 2760static void
bc4b653b
JM
2761record_maybe_used_decl (tree decl)
2762{
2763 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2764 t->decl = decl;
2765 t->level = in_sizeof + in_typeof;
2766 t->next = maybe_used_decls;
2767 maybe_used_decls = t;
2768}
2769
2770/* Pop the stack of decls possibly used inside sizeof or typeof. If
2771 USED is false, just discard them. If it is true, mark them used
2772 (if no longer inside sizeof or typeof) or move them to the next
2773 level up (if still inside sizeof or typeof). */
2774
2775void
2776pop_maybe_used (bool used)
2777{
2778 struct maybe_used_decl *p = maybe_used_decls;
2779 int cur_level = in_sizeof + in_typeof;
2780 while (p && p->level > cur_level)
2781 {
2782 if (used)
2783 {
2784 if (cur_level == 0)
2785 C_DECL_USED (p->decl) = 1;
2786 else
2787 p->level = cur_level;
2788 }
2789 p = p->next;
2790 }
2791 if (!used || cur_level == 0)
2792 maybe_used_decls = p;
2793}
2794
2795/* Return the result of sizeof applied to EXPR. */
2796
2797struct c_expr
c2255bc4 2798c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2799{
2800 struct c_expr ret;
ad97f4be
JM
2801 if (expr.value == error_mark_node)
2802 {
2803 ret.value = error_mark_node;
2804 ret.original_code = ERROR_MARK;
6866c6e8 2805 ret.original_type = NULL;
ad97f4be
JM
2806 pop_maybe_used (false);
2807 }
2808 else
2809 {
928c19bb 2810 bool expr_const_operands = true;
773ec47f
MP
2811
2812 if (TREE_CODE (expr.value) == PARM_DECL
2813 && C_ARRAY_PARAMETER (expr.value))
2814 {
2815 if (warning_at (loc, OPT_Wsizeof_array_argument,
2816 "%<sizeof%> on array function parameter %qE will "
2817 "return size of %qT", expr.value,
2818 expr.original_type))
2819 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2820 }
928c19bb
JM
2821 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2822 &expr_const_operands);
c2255bc4 2823 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2824 c_last_sizeof_arg = expr.value;
2825 ret.original_code = SIZEOF_EXPR;
6866c6e8 2826 ret.original_type = NULL;
928c19bb 2827 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2828 {
2829 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2830 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2831 folded_expr, ret.value);
2832 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2833 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2834 }
928c19bb 2835 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2836 }
bc4b653b
JM
2837 return ret;
2838}
2839
2840/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2841 name passed to sizeof (rather than the type itself). LOC is the
2842 location of the original expression. */
bc4b653b
JM
2843
2844struct c_expr
c2255bc4 2845c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2846{
2847 tree type;
2848 struct c_expr ret;
928c19bb
JM
2849 tree type_expr = NULL_TREE;
2850 bool type_expr_const = true;
2851 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2852 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2853 c_last_sizeof_arg = type;
2854 ret.original_code = SIZEOF_EXPR;
6866c6e8 2855 ret.original_type = NULL;
24070fcb
JM
2856 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2857 && c_vla_type_p (type))
928c19bb 2858 {
24070fcb
JM
2859 /* If the type is a [*] array, it is a VLA but is represented as
2860 having a size of zero. In such a case we must ensure that
2861 the result of sizeof does not get folded to a constant by
2862 c_fully_fold, because if the size is evaluated the result is
2863 not constant and so constraints on zero or negative size
2864 arrays must not be applied when this sizeof call is inside
2865 another array declarator. */
2866 if (!type_expr)
2867 type_expr = integer_zero_node;
928c19bb
JM
2868 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2869 type_expr, ret.value);
2870 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2871 }
16464cc1
VR
2872 pop_maybe_used (type != error_mark_node
2873 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2874 return ret;
2875}
2876
400fbf9f 2877/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2878 The function call is at LOC.
400fbf9f
JW
2879 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2880 TREE_VALUE of each node is a parameter-expression.
2881 FUNCTION's data type may be a function type or a pointer-to-function. */
2882
2883tree
c2255bc4 2884build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2885{
9771b263 2886 vec<tree, va_gc> *v;
bbbbb16a
ILT
2887 tree ret;
2888
9771b263 2889 vec_alloc (v, list_length (params));
bbbbb16a 2890 for (; params; params = TREE_CHAIN (params))
9771b263 2891 v->quick_push (TREE_VALUE (params));
8edbfaa6 2892 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2893 vec_free (v);
bbbbb16a
ILT
2894 return ret;
2895}
2896
ae52741c
MLI
2897/* Give a note about the location of the declaration of DECL. */
2898
c7b70a3c
MP
2899static void
2900inform_declaration (tree decl)
ae52741c 2901{
c7b70a3c 2902 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2903 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2904}
2905
bbbbb16a
ILT
2906/* Build a function call to function FUNCTION with parameters PARAMS.
2907 ORIGTYPES, if not NULL, is a vector of types; each element is
2908 either NULL or the original type of the corresponding element in
2909 PARAMS. The original type may differ from TREE_TYPE of the
2910 parameter for enums. FUNCTION's data type may be a function type
2911 or pointer-to-function. This function changes the elements of
2912 PARAMS. */
2913
2914tree
81e5eca8
MP
2915build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2916 tree function, vec<tree, va_gc> *params,
9771b263 2917 vec<tree, va_gc> *origtypes)
400fbf9f 2918{
b3694847 2919 tree fntype, fundecl = 0;
4977bab6 2920 tree name = NULL_TREE, result;
c96f4f73 2921 tree tem;
94a0dd7b
SL
2922 int nargs;
2923 tree *argarray;
b8698a0f 2924
400fbf9f 2925
fc76e425 2926 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2927 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2928
2929 /* Convert anything with function type to a pointer-to-function. */
2930 if (TREE_CODE (function) == FUNCTION_DECL)
2931 {
2932 name = DECL_NAME (function);
0a35513e
AH
2933
2934 if (flag_tm)
2935 tm_malloc_replacement (function);
a5eadacc 2936 fundecl = function;
86951993
AM
2937 /* Atomic functions have type checking/casting already done. They are
2938 often rewritten and don't match the original parameter list. */
2939 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2940 origtypes = NULL;
36536d79 2941
b72271b9 2942 if (flag_cilkplus
36536d79
BI
2943 && is_cilkplus_reduce_builtin (function))
2944 origtypes = NULL;
400fbf9f 2945 }
f2a71bbc 2946 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2947 function = function_to_pointer_conversion (loc, function);
400fbf9f 2948
6e955430
ZL
2949 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2950 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2951 if (params && !params->is_empty ())
2952 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2953
928c19bb
JM
2954 function = c_fully_fold (function, false, NULL);
2955
400fbf9f
JW
2956 fntype = TREE_TYPE (function);
2957
2958 if (TREE_CODE (fntype) == ERROR_MARK)
2959 return error_mark_node;
2960
2961 if (!(TREE_CODE (fntype) == POINTER_TYPE
2962 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2963 {
ae52741c
MLI
2964 if (!flag_diagnostics_show_caret)
2965 error_at (loc,
2966 "called object %qE is not a function or function pointer",
2967 function);
2968 else if (DECL_P (function))
2969 {
2970 error_at (loc,
2971 "called object %qD is not a function or function pointer",
2972 function);
2973 inform_declaration (function);
2974 }
2975 else
2976 error_at (loc,
2977 "called object is not a function or function pointer");
400fbf9f
JW
2978 return error_mark_node;
2979 }
2980
5ce89b2e
JM
2981 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2982 current_function_returns_abnormally = 1;
2983
400fbf9f
JW
2984 /* fntype now gets the type of function pointed to. */
2985 fntype = TREE_TYPE (fntype);
2986
ab4194da
JM
2987 /* Convert the parameters to the types declared in the
2988 function prototype, or apply default promotions. */
2989
81e5eca8
MP
2990 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2991 origtypes, function, fundecl);
ab4194da
JM
2992 if (nargs < 0)
2993 return error_mark_node;
2994
c96f4f73 2995 /* Check that the function is called through a compatible prototype.
fa337f3a 2996 If it is not, warn. */
1043771b 2997 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2998 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2999 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3000 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3001 {
3002 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3003
3004 /* This situation leads to run-time undefined behavior. We can't,
3005 therefore, simply error unless we can prove that all possible
3006 executions of the program must execute the code. */
fa337f3a 3007 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3008
fa337f3a
RB
3009 if (VOID_TYPE_P (return_type)
3010 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3011 pedwarn (loc, 0,
3012 "function with qualified void return type called");
3013 }
c96f4f73 3014
9771b263 3015 argarray = vec_safe_address (params);
bbbbb16a 3016
83322951
RG
3017 /* Check that arguments to builtin functions match the expectations. */
3018 if (fundecl
3019 && DECL_BUILT_IN (fundecl)
3020 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3021 && !check_builtin_function_arguments (fundecl, nargs, argarray))
3022 return error_mark_node;
400fbf9f 3023
83322951 3024 /* Check that the arguments to the function are valid. */
dde05067 3025 check_function_arguments (fntype, nargs, argarray);
400fbf9f 3026
928c19bb
JM
3027 if (name != NULL_TREE
3028 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3029 {
928c19bb 3030 if (require_constant_value)
b8698a0f 3031 result =
db3927fb
AH
3032 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3033 function, nargs, argarray);
928c19bb 3034 else
db3927fb
AH
3035 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3036 function, nargs, argarray);
928c19bb
JM
3037 if (TREE_CODE (result) == NOP_EXPR
3038 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3039 STRIP_TYPE_NOPS (result);
bf730f15
RS
3040 }
3041 else
db3927fb
AH
3042 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3043 function, nargs, argarray);
b0b3afb2 3044
71653180 3045 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3046 {
3047 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3048 pedwarn (loc, 0,
3ce62965
JM
3049 "function with qualified void return type called");
3050 return result;
3051 }
1eb8759b 3052 return require_complete_type (result);
400fbf9f 3053}
8edbfaa6
JJ
3054
3055/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3056
3057tree
3058c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3059 tree function, vec<tree, va_gc> *params,
3060 vec<tree, va_gc> *origtypes)
3061{
3062 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3063 STRIP_TYPE_NOPS (function);
3064
3065 /* Convert anything with function type to a pointer-to-function. */
3066 if (TREE_CODE (function) == FUNCTION_DECL)
3067 {
3068 /* Implement type-directed function overloading for builtins.
3069 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3070 handle all the type checking. The result is a complete expression
3071 that implements this function call. */
3072 tree tem = resolve_overloaded_builtin (loc, function, params);
3073 if (tem)
3074 return tem;
3075 }
3076 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3077}
400fbf9f 3078\f
bbbbb16a
ILT
3079/* Convert the argument expressions in the vector VALUES
3080 to the types in the list TYPELIST.
400fbf9f
JW
3081
3082 If TYPELIST is exhausted, or when an element has NULL as its type,
3083 perform the default conversions.
3084
bbbbb16a
ILT
3085 ORIGTYPES is the original types of the expressions in VALUES. This
3086 holds the type of enum values which have been converted to integral
3087 types. It may be NULL.
400fbf9f 3088
03dafa61
JM
3089 FUNCTION is a tree for the called function. It is used only for
3090 error messages, where it is formatted with %qE.
400fbf9f
JW
3091
3092 This is also where warnings about wrong number of args are generated.
3093
81e5eca8
MP
3094 ARG_LOC are locations of function arguments (if any).
3095
94a0dd7b 3096 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3097 than the length of VALUES in some error situations), or -1 on
3098 failure. */
94a0dd7b
SL
3099
3100static int
81e5eca8
MP
3101convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3102 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3103 tree function, tree fundecl)
400fbf9f 3104{
bbbbb16a
ILT
3105 tree typetail, val;
3106 unsigned int parmnum;
06302a02 3107 bool error_args = false;
b5d32c25 3108 const bool type_generic = fundecl
81e5eca8 3109 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3110 bool type_generic_remove_excess_precision = false;
03dafa61 3111 tree selector;
03dafa61 3112
2ac2f164
JM
3113 /* Change pointer to function to the function itself for
3114 diagnostics. */
03dafa61
JM
3115 if (TREE_CODE (function) == ADDR_EXPR
3116 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3117 function = TREE_OPERAND (function, 0);
03dafa61
JM
3118
3119 /* Handle an ObjC selector specially for diagnostics. */
3120 selector = objc_message_selector ();
400fbf9f 3121
8ce94e44
JM
3122 /* For type-generic built-in functions, determine whether excess
3123 precision should be removed (classification) or not
3124 (comparison). */
3125 if (type_generic
3126 && DECL_BUILT_IN (fundecl)
3127 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3128 {
3129 switch (DECL_FUNCTION_CODE (fundecl))
3130 {
3131 case BUILT_IN_ISFINITE:
3132 case BUILT_IN_ISINF:
3133 case BUILT_IN_ISINF_SIGN:
3134 case BUILT_IN_ISNAN:
3135 case BUILT_IN_ISNORMAL:
3136 case BUILT_IN_FPCLASSIFY:
3137 type_generic_remove_excess_precision = true;
3138 break;
3139
3140 default:
3141 type_generic_remove_excess_precision = false;
3142 break;
3143 }
3144 }
b72271b9 3145 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3146 return vec_safe_length (values);
8ce94e44 3147
400fbf9f 3148 /* Scan the given expressions and types, producing individual
bbbbb16a 3149 converted arguments. */
400fbf9f 3150
bbbbb16a 3151 for (typetail = typelist, parmnum = 0;
9771b263 3152 values && values->iterate (parmnum, &val);
bbbbb16a 3153 ++parmnum)
400fbf9f 3154 {
b3694847 3155 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3156 tree valtype = TREE_TYPE (val);
03dafa61
JM
3157 tree rname = function;
3158 int argnum = parmnum + 1;
4d3e6fae 3159 const char *invalid_func_diag;
8ce94e44 3160 bool excess_precision = false;
928c19bb 3161 bool npc;
bbbbb16a 3162 tree parmval;
5c1bc275
MP
3163 /* Some __atomic_* builtins have additional hidden argument at
3164 position 0. */
3165 location_t ploc
3166 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3167 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3168 : input_location;
400fbf9f
JW
3169
3170 if (type == void_type_node)
3171 {
19dc6d01 3172 if (selector)
68fca595 3173 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3174 else
68fca595 3175 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3176 inform_declaration (fundecl);
d38f7dce 3177 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3178 }
3179
03dafa61
JM
3180 if (selector && argnum > 2)
3181 {
3182 rname = selector;
3183 argnum -= 2;
3184 }
3185
928c19bb 3186 npc = null_pointer_constant_p (val);
8ce94e44
JM
3187
3188 /* If there is excess precision and a prototype, convert once to
3189 the required type rather than converting via the semantic
3190 type. Likewise without a prototype a float value represented
3191 as long double should be converted once to double. But for
3192 type-generic classification functions excess precision must
3193 be removed here. */
3194 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3195 && (type || !type_generic || !type_generic_remove_excess_precision))
3196 {
3197 val = TREE_OPERAND (val, 0);
3198 excess_precision = true;
3199 }
928c19bb 3200 val = c_fully_fold (val, false, NULL);
ed248cf7 3201 STRIP_TYPE_NOPS (val);
400fbf9f 3202
400fbf9f
JW
3203 val = require_complete_type (val);
3204
3205 if (type != 0)
3206 {
3207 /* Formal parm type is specified by a function prototype. */
400fbf9f 3208
20913689 3209 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3210 {
5c1bc275
MP
3211 error_at (ploc, "type of formal parameter %d is incomplete",
3212 parmnum + 1);
400fbf9f
JW
3213 parmval = val;
3214 }
3215 else
3216 {
bbbbb16a
ILT
3217 tree origtype;
3218
d45cf215
RS
3219 /* Optionally warn about conversions that
3220 differ from the default conversions. */
05170031 3221 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3222 {
e3a64162 3223 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3224
aae43c5f 3225 if (INTEGRAL_TYPE_P (type)
8ce94e44 3226 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3227 warning_at (ploc, OPT_Wtraditional_conversion,
3228 "passing argument %d of %qE as integer rather "
3229 "than floating due to prototype",
3230 argnum, rname);
03829ad2 3231 if (INTEGRAL_TYPE_P (type)
8ce94e44 3232 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3233 warning_at (ploc, OPT_Wtraditional_conversion,
3234 "passing argument %d of %qE as integer rather "
3235 "than complex due to prototype",
3236 argnum, rname);
aae43c5f 3237 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3238 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3239 warning_at (ploc, OPT_Wtraditional_conversion,
3240 "passing argument %d of %qE as complex rather "
3241 "than floating due to prototype",
3242 argnum, rname);
400fbf9f 3243 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3244 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3245 warning_at (ploc, OPT_Wtraditional_conversion,
3246 "passing argument %d of %qE as floating rather "
3247 "than integer due to prototype",
3248 argnum, rname);
03829ad2 3249 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3250 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3251 warning_at (ploc, OPT_Wtraditional_conversion,
3252 "passing argument %d of %qE as complex rather "
3253 "than integer due to prototype",
3254 argnum, rname);
aae43c5f 3255 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3256 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3257 warning_at (ploc, OPT_Wtraditional_conversion,
3258 "passing argument %d of %qE as floating rather "
3259 "than complex due to prototype",
3260 argnum, rname);
aae43c5f
RK
3261 /* ??? At some point, messages should be written about
3262 conversions between complex types, but that's too messy
3263 to do now. */
d45cf215 3264 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3265 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3266 {
3267 /* Warn if any argument is passed as `float',
047de90b 3268 since without a prototype it would be `double'. */
9a8ce21f
JG
3269 if (formal_prec == TYPE_PRECISION (float_type_node)
3270 && type != dfloat32_type_node)
5c1bc275
MP
3271 warning_at (ploc, 0,
3272 "passing argument %d of %qE as %<float%> "
3273 "rather than %<double%> due to prototype",
3274 argnum, rname);
9a8ce21f
JG
3275
3276 /* Warn if mismatch between argument and prototype
3277 for decimal float types. Warn of conversions with
3278 binary float types and of precision narrowing due to
3279 prototype. */
8ce94e44 3280 else if (type != valtype
9a8ce21f
JG
3281 && (type == dfloat32_type_node
3282 || type == dfloat64_type_node
c22cacf3 3283 || type == dfloat128_type_node
8ce94e44
JM
3284 || valtype == dfloat32_type_node
3285 || valtype == dfloat64_type_node
3286 || valtype == dfloat128_type_node)
c22cacf3 3287 && (formal_prec
8ce94e44 3288 <= TYPE_PRECISION (valtype)
9a8ce21f 3289 || (type == dfloat128_type_node
8ce94e44 3290 && (valtype
c22cacf3 3291 != dfloat64_type_node
8ce94e44 3292 && (valtype
9a8ce21f
JG
3293 != dfloat32_type_node)))
3294 || (type == dfloat64_type_node
8ce94e44 3295 && (valtype
9a8ce21f 3296 != dfloat32_type_node))))
5c1bc275
MP
3297 warning_at (ploc, 0,
3298 "passing argument %d of %qE as %qT "
3299 "rather than %qT due to prototype",
3300 argnum, rname, type, valtype);
9a8ce21f 3301
d45cf215 3302 }
3ed56f8a
KG
3303 /* Detect integer changing in width or signedness.
3304 These warnings are only activated with
05170031
MLI
3305 -Wtraditional-conversion, not with -Wtraditional. */
3306 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3307 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3308 {
d45cf215
RS
3309 tree would_have_been = default_conversion (val);
3310 tree type1 = TREE_TYPE (would_have_been);
3311
754a4d82 3312 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3313 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3314 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3315 /* No warning if function asks for enum
3316 and the actual arg is that enum type. */
3317 ;
3318 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3319 warning_at (ploc, OPT_Wtraditional_conversion,
3320 "passing argument %d of %qE "
3321 "with different width due to prototype",
3322 argnum, rname);
8df83eae 3323 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3324 ;
800cd3b9
RS
3325 /* Don't complain if the formal parameter type
3326 is an enum, because we can't tell now whether
3327 the value was an enum--even the same enum. */
3328 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3329 ;
400fbf9f
JW
3330 else if (TREE_CODE (val) == INTEGER_CST
3331 && int_fits_type_p (val, type))
3332 /* Change in signedness doesn't matter
3333 if a constant value is unaffected. */
3334 ;
ce9895ae
RS
3335 /* If the value is extended from a narrower
3336 unsigned type, it doesn't matter whether we
3337 pass it as signed or unsigned; the value
3338 certainly is the same either way. */
8ce94e44
JM
3339 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3340 && TYPE_UNSIGNED (valtype))
ce9895ae 3341 ;
8df83eae 3342 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3343 warning_at (ploc, OPT_Wtraditional_conversion,
3344 "passing argument %d of %qE "
3345 "as unsigned due to prototype",
3346 argnum, rname);
3ed56f8a 3347 else
5c1bc275
MP
3348 warning_at (ploc, OPT_Wtraditional_conversion,
3349 "passing argument %d of %qE "
3350 "as signed due to prototype",
3351 argnum, rname);
400fbf9f
JW
3352 }
3353 }
3354
8ce94e44
JM
3355 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3356 sake of better warnings from convert_and_check. */
3357 if (excess_precision)
3358 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3359 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3360 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3361 val, origtype, ic_argpass,
3362 npc, fundecl, function,
2ac2f164 3363 parmnum + 1);
2f6e4e97 3364
61f71b34 3365 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3366 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3367 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3368 parmval = default_conversion (parmval);
400fbf9f 3369 }
400fbf9f 3370 }
8ce94e44
JM
3371 else if (TREE_CODE (valtype) == REAL_TYPE
3372 && (TYPE_PRECISION (valtype)
2531a1d9 3373 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3374 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3375 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3376 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3377 {
3378 if (type_generic)
bbbbb16a 3379 parmval = val;
b5d32c25 3380 else
0a0b3574
MM
3381 {
3382 /* Convert `float' to `double'. */
3383 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3384 warning_at (ploc, OPT_Wdouble_promotion,
3385 "implicit conversion from %qT to %qT when passing "
3386 "argument to function",
3387 valtype, double_type_node);
0a0b3574
MM
3388 parmval = convert (double_type_node, val);
3389 }
b5d32c25 3390 }
8ce94e44
JM
3391 else if (excess_precision && !type_generic)
3392 /* A "double" argument with excess precision being passed
3393 without a prototype or in variable arguments. */
bbbbb16a 3394 parmval = convert (valtype, val);
c22cacf3
MS
3395 else if ((invalid_func_diag =
3396 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3397 {
3398 error (invalid_func_diag);
94a0dd7b 3399 return -1;
4d3e6fae 3400 }
1807ffc1
MS
3401 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3402 {
3403 return -1;
3404 }
400fbf9f
JW
3405 else
3406 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3407 parmval = default_conversion (val);
3408
9771b263 3409 (*values)[parmnum] = parmval;
06302a02
JJ
3410 if (parmval == error_mark_node)
3411 error_args = true;
400fbf9f
JW
3412
3413 if (typetail)
3414 typetail = TREE_CHAIN (typetail);
3415 }
3416
9771b263 3417 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3418
400fbf9f 3419 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3420 {
68fca595 3421 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3422 inform_declaration (fundecl);
3423 return -1;
3789b316 3424 }
400fbf9f 3425
06302a02 3426 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3427}
3428\f
43f6dfd3
RS
3429/* This is the entry point used by the parser to build unary operators
3430 in the input. CODE, a tree_code, specifies the unary operator, and
3431 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3432 CONVERT_EXPR for code.
3433
3434 LOC is the location to use for the tree generated.
3435*/
43f6dfd3
RS
3436
3437struct c_expr
c2255bc4 3438parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3439{
3440 struct c_expr result;
3441
100d537d 3442 result.original_code = code;
6866c6e8
ILT
3443 result.original_type = NULL;
3444
1807ffc1
MS
3445 if (reject_gcc_builtin (arg.value))
3446 {
3447 result.value = error_mark_node;
3448 }
3449 else
3450 {
3451 result.value = build_unary_op (loc, code, arg.value, 0);
3452
59c0753d 3453 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3454 overflow_warning (loc, result.value);
1807ffc1 3455 }
59c0753d 3456
ebedc9a3
DM
3457 /* We are typically called when parsing a prefix token at LOC acting on
3458 ARG. Reflect this by updating the source range of the result to
3459 start at LOC and end at the end of ARG. */
3460 set_c_expr_source_range (&result,
3461 loc, arg.get_finish ());
3462
43f6dfd3
RS
3463 return result;
3464}
3465
3466/* This is the entry point used by the parser to build binary operators
3467 in the input. CODE, a tree_code, specifies the binary operator, and
3468 ARG1 and ARG2 are the operands. In addition to constructing the
3469 expression, we check for operands that were written with other binary
ba47d38d
AH
3470 operators in a way that is likely to confuse the user.
3471
3472 LOCATION is the location of the binary operator. */
edc7c4ec 3473
487a92fe 3474struct c_expr
ba47d38d
AH
3475parser_build_binary_op (location_t location, enum tree_code code,
3476 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3477{
487a92fe 3478 struct c_expr result;
400fbf9f 3479
487a92fe
JM
3480 enum tree_code code1 = arg1.original_code;
3481 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3482 tree type1 = (arg1.original_type
3483 ? arg1.original_type
3484 : TREE_TYPE (arg1.value));
3485 tree type2 = (arg2.original_type
3486 ? arg2.original_type
3487 : TREE_TYPE (arg2.value));
400fbf9f 3488
ba47d38d
AH
3489 result.value = build_binary_op (location, code,
3490 arg1.value, arg2.value, 1);
487a92fe 3491 result.original_code = code;
6866c6e8 3492 result.original_type = NULL;
58bf601b 3493
487a92fe
JM
3494 if (TREE_CODE (result.value) == ERROR_MARK)
3495 return result;
400fbf9f 3496
ba47d38d
AH
3497 if (location != UNKNOWN_LOCATION)
3498 protected_set_expr_location (result.value, location);
3499
ebedc9a3
DM
3500 set_c_expr_source_range (&result,
3501 arg1.get_start (),
3502 arg2.get_finish ());
3503
400fbf9f 3504 /* Check for cases such as x+y<<z which users are likely
487a92fe 3505 to misinterpret. */
400fbf9f 3506 if (warn_parentheses)
393e8e8b
MP
3507 warn_about_parentheses (location, code, code1, arg1.value, code2,
3508 arg2.value);
001af587 3509
ca409efd 3510 if (warn_logical_op)
393e8e8b 3511 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3512 code1, arg1.value, code2, arg2.value);
63a08740 3513
05b28fd6 3514 if (warn_tautological_compare)
433068cc
MP
3515 {
3516 tree lhs = arg1.value;
3517 tree rhs = arg2.value;
3518 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3519 {
3520 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3521 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3522 lhs = NULL_TREE;
3523 else
3524 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3525 }
3526 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3527 {
3528 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3529 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3530 rhs = NULL_TREE;
3531 else
3532 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3533 }
3534 if (lhs != NULL_TREE && rhs != NULL_TREE)
3535 warn_tautological_cmp (location, code, lhs, rhs);
3536 }
05b28fd6 3537
742938c9 3538 if (warn_logical_not_paren
7ccb1a11 3539 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3540 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3541 && code2 != TRUTH_NOT_EXPR
3542 /* Avoid warning for !!x == y. */
3543 && (TREE_CODE (arg1.value) != NE_EXPR
3544 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3545 {
3546 /* Avoid warning for !b == y where b has _Bool type. */
3547 tree t = integer_zero_node;
3548 if (TREE_CODE (arg1.value) == EQ_EXPR
3549 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3550 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3551 {
3552 t = TREE_OPERAND (arg1.value, 0);
3553 do
3554 {
3555 if (TREE_TYPE (t) != integer_type_node)
3556 break;
3557 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3558 t = C_MAYBE_CONST_EXPR_EXPR (t);
3559 else if (CONVERT_EXPR_P (t))
3560 t = TREE_OPERAND (t, 0);
3561 else
3562 break;
3563 }
3564 while (1);
3565 }
3566 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3567 warn_logical_not_parentheses (location, code, arg2.value);
3568 }
742938c9 3569
e994a705
RS
3570 /* Warn about comparisons against string literals, with the exception
3571 of testing for equality or inequality of a string literal with NULL. */
3572 if (code == EQ_EXPR || code == NE_EXPR)
3573 {
3574 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3575 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3576 warning_at (location, OPT_Waddress,
3577 "comparison with string literal results in unspecified behavior");
e994a705
RS
3578 }
3579 else if (TREE_CODE_CLASS (code) == tcc_comparison
3580 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3581 warning_at (location, OPT_Waddress,
3582 "comparison with string literal results in unspecified behavior");
e994a705 3583
b8698a0f
L
3584 if (TREE_OVERFLOW_P (result.value)
3585 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3586 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3587 overflow_warning (location, result.value);
400fbf9f 3588
6866c6e8
ILT
3589 /* Warn about comparisons of different enum types. */
3590 if (warn_enum_compare
3591 && TREE_CODE_CLASS (code) == tcc_comparison
3592 && TREE_CODE (type1) == ENUMERAL_TYPE
3593 && TREE_CODE (type2) == ENUMERAL_TYPE
3594 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3595 warning_at (location, OPT_Wenum_compare,
3596 "comparison between %qT and %qT",
3597 type1, type2);
3598
400fbf9f
JW
3599 return result;
3600}
3e4093b6 3601\f
3e4093b6
RS
3602/* Return a tree for the difference of pointers OP0 and OP1.
3603 The resulting tree has type int. */
293c9fdd 3604
3e4093b6 3605static tree
db3927fb 3606pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3607{
3e4093b6 3608 tree restype = ptrdiff_type_node;
36c5e70a 3609 tree result, inttype;
400fbf9f 3610
36c5e70a
BE
3611 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3612 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3613 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3614 tree orig_op1 = op1;
400fbf9f 3615
36c5e70a
BE
3616 /* If the operands point into different address spaces, we need to
3617 explicitly convert them to pointers into the common address space
3618 before we can subtract the numerical address values. */
3619 if (as0 != as1)
3620 {
3621 addr_space_t as_common;
3622 tree common_type;
3623
3624 /* Determine the common superset address space. This is guaranteed
3625 to exist because the caller verified that comp_target_types
3626 returned non-zero. */
3627 if (!addr_space_superset (as0, as1, &as_common))
3628 gcc_unreachable ();
3629
3630 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3631 op0 = convert (common_type, op0);
3632 op1 = convert (common_type, op1);
3633 }
3634
3635 /* Determine integer type to perform computations in. This will usually
3636 be the same as the result type (ptrdiff_t), but may need to be a wider
3637 type if pointers for the address space are wider than ptrdiff_t. */
3638 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3639 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3640 else
3641 inttype = restype;
3642
fcf73884 3643 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3644 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3645 "pointer of type %<void *%> used in subtraction");
3646 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3647 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3648 "pointer to a function used in subtraction");
400fbf9f 3649
3e4093b6
RS
3650 /* First do the subtraction as integers;
3651 then drop through to build the divide operator.
3652 Do not do default conversions on the minus operator
3653 in case restype is a short type. */
400fbf9f 3654
db3927fb 3655 op0 = build_binary_op (loc,
36c5e70a
BE
3656 MINUS_EXPR, convert (inttype, op0),
3657 convert (inttype, op1), 0);
3e4093b6
RS
3658 /* This generates an error if op1 is pointer to incomplete type. */
3659 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3660 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3661
3e4093b6 3662 op1 = c_size_in_bytes (target_type);
400fbf9f 3663
f04dda30
MP
3664 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3665 error_at (loc, "arithmetic on pointer to an empty aggregate");
3666
3e4093b6 3667 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3668 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3669 op0, convert (inttype, op1));
3670
3671 /* Convert to final result type if necessary. */
3672 return convert (restype, result);
3e4093b6
RS
3673}
3674\f
267bac10
JM
3675/* Expand atomic compound assignments into an approriate sequence as
3676 specified by the C11 standard section 6.5.16.2.
3677 given
3678 _Atomic T1 E1
3679 T2 E2
3680 E1 op= E2
3681
3682 This sequence is used for all types for which these operations are
3683 supported.
3684
3685 In addition, built-in versions of the 'fe' prefixed routines may
3686 need to be invoked for floating point (real, complex or vector) when
3687 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3688
3689 T1 newval;
3690 T1 old;
3691 T1 *addr
3692 T2 val
3693 fenv_t fenv
3694
3695 addr = &E1;
3696 val = (E2);
3697 __atomic_load (addr, &old, SEQ_CST);
3698 feholdexcept (&fenv);
3699loop:
3700 newval = old op val;
3701 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3702 SEQ_CST))
3703 goto done;
3704 feclearexcept (FE_ALL_EXCEPT);
3705 goto loop:
3706done:
3707 feupdateenv (&fenv);
3708
3709 Also note that the compiler is simply issuing the generic form of
3710 the atomic operations. This requires temp(s) and has their address
3711 taken. The atomic processing is smart enough to figure out when the
3712 size of an object can utilize a lock-free version, and convert the
3713 built-in call to the appropriate lock-free routine. The optimizers
3714 will then dispose of any temps that are no longer required, and
3715 lock-free implementations are utilized as long as there is target
3716 support for the required size.
3717
3718 If the operator is NOP_EXPR, then this is a simple assignment, and
3719 an __atomic_store is issued to perform the assignment rather than
3720 the above loop.
3721
3722*/
3723
3724/* Build an atomic assignment at LOC, expanding into the proper
3725 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3726 the result of the operation, unless RETURN_OLD_P in which case
3727 return the old value of LHS (this is only for postincrement and
3728 postdecrement). */
3729static tree
3730build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3731 tree rhs, bool return_old_p)
3732{
3733 tree fndecl, func_call;
3734 vec<tree, va_gc> *params;
3735 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3736 tree old, old_addr;
3737 tree compound_stmt;
3738 tree stmt, goto_stmt;
3739 tree loop_label, loop_decl, done_label, done_decl;
3740
3741 tree lhs_type = TREE_TYPE (lhs);
3742 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3743 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3744 tree rhs_type = TREE_TYPE (rhs);
3745
3746 gcc_assert (TYPE_ATOMIC (lhs_type));
3747
3748 if (return_old_p)
3749 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3750
3751 /* Allocate enough vector items for a compare_exchange. */
3752 vec_alloc (params, 6);
3753
3754 /* Create a compound statement to hold the sequence of statements
3755 with a loop. */
3756 compound_stmt = c_begin_compound_stmt (false);
3757
3758 /* Fold the RHS if it hasn't already been folded. */
3759 if (modifycode != NOP_EXPR)
3760 rhs = c_fully_fold (rhs, false, NULL);
3761
3762 /* Remove the qualifiers for the rest of the expressions and create
3763 the VAL temp variable to hold the RHS. */
3764 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3765 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3766 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3767 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3768 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3769 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3770 NULL_TREE);
267bac10
JM
3771 SET_EXPR_LOCATION (rhs, loc);
3772 add_stmt (rhs);
3773
3774 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3775 an atomic_store. */
3776 if (modifycode == NOP_EXPR)
3777 {
3778 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3779 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3780 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3781 params->quick_push (lhs_addr);
3782 params->quick_push (rhs);
3783 params->quick_push (seq_cst);
8edbfaa6 3784 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3785 add_stmt (func_call);
3786
3787 /* Finish the compound statement. */
3788 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3789
3790 /* VAL is the value which was stored, return a COMPOUND_STMT of
3791 the statement and that value. */
3792 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3793 }
3794
3795 /* Create the variables and labels required for the op= form. */
5c4abbb8 3796 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3797 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3798 TREE_ADDRESSABLE (old) = 1;
3799 TREE_NO_WARNING (old) = 1;
267bac10 3800
5c4abbb8 3801 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3802 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3803 TREE_ADDRESSABLE (newval) = 1;
3804
3805 loop_decl = create_artificial_label (loc);
3806 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3807
3808 done_decl = create_artificial_label (loc);
3809 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3810
3811 /* __atomic_load (addr, &old, SEQ_CST). */
3812 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3813 params->quick_push (lhs_addr);
3814 params->quick_push (old_addr);
3815 params->quick_push (seq_cst);
8edbfaa6 3816 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3817 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3818 NULL_TREE);
3819 add_stmt (old);
267bac10
JM
3820 params->truncate (0);
3821
3822 /* Create the expressions for floating-point environment
3823 manipulation, if required. */
3824 bool need_fenv = (flag_trapping_math
3825 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3826 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3827 if (need_fenv)
3828 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3829
3830 if (hold_call)
3831 add_stmt (hold_call);
3832
3833 /* loop: */
3834 add_stmt (loop_label);
3835
3836 /* newval = old + val; */
3837 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3838 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3839 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3840 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3841 NULL_TREE, 0);
3842 if (rhs != error_mark_node)
3843 {
5c4abbb8
MP
3844 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3845 NULL_TREE);
267bac10
JM
3846 SET_EXPR_LOCATION (rhs, loc);
3847 add_stmt (rhs);
3848 }
3849
3850 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3851 goto done; */
3852 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3853 params->quick_push (lhs_addr);
3854 params->quick_push (old_addr);
3855 params->quick_push (newval_addr);
3856 params->quick_push (integer_zero_node);
3857 params->quick_push (seq_cst);
3858 params->quick_push (seq_cst);
8edbfaa6 3859 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3860
3861 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3862 SET_EXPR_LOCATION (goto_stmt, loc);
3863
3864 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3865 SET_EXPR_LOCATION (stmt, loc);
3866 add_stmt (stmt);
5c4abbb8 3867
267bac10
JM
3868 if (clear_call)
3869 add_stmt (clear_call);
3870
3871 /* goto loop; */
3872 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3873 SET_EXPR_LOCATION (goto_stmt, loc);
3874 add_stmt (goto_stmt);
5c4abbb8 3875
267bac10
JM
3876 /* done: */
3877 add_stmt (done_label);
3878
3879 if (update_call)
3880 add_stmt (update_call);
3881
3882 /* Finish the compound statement. */
3883 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3884
3885 /* NEWVAL is the value that was successfully stored, return a
3886 COMPOUND_EXPR of the statement and the appropriate value. */
3887 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3888 return_old_p ? old : newval);
3889}
3890
3e4093b6
RS
3891/* Construct and perhaps optimize a tree representation
3892 for a unary operation. CODE, a tree_code, specifies the operation
3893 and XARG is the operand.
3894 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3895 the default promotions (such as from short to int).
3896 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3897 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3898 arrays to pointers in C99.
3899
3900 LOCATION is the location of the operator. */
400fbf9f 3901
3e4093b6 3902tree
c9f9eb5d
AH
3903build_unary_op (location_t location,
3904 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3905{
3906 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3907 tree arg = xarg;
3908 tree argtype = 0;
808d6eaa 3909 enum tree_code typecode;
3e4093b6 3910 tree val;
c9f9eb5d 3911 tree ret = error_mark_node;
8ce94e44 3912 tree eptype = NULL_TREE;
3e4093b6 3913 int noconvert = flag;
4de67c26 3914 const char *invalid_op_diag;
928c19bb
JM
3915 bool int_operands;
3916
3917 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3918 if (int_operands)
3919 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3920
808d6eaa
JM
3921 if (code != ADDR_EXPR)
3922 arg = require_complete_type (arg);
3923
3924 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3925 if (typecode == ERROR_MARK)
3926 return error_mark_node;
3927 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3928 typecode = INTEGER_TYPE;
6c36d76b 3929
4de67c26
JM
3930 if ((invalid_op_diag
3931 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3932 {
c9f9eb5d 3933 error_at (location, invalid_op_diag);
4de67c26
JM
3934 return error_mark_node;
3935 }
3936
8ce94e44
JM
3937 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3938 {
3939 eptype = TREE_TYPE (arg);
3940 arg = TREE_OPERAND (arg, 0);
3941 }
3942
3e4093b6
RS
3943 switch (code)
3944 {
3945 case CONVERT_EXPR:
3946 /* This is used for unary plus, because a CONVERT_EXPR
3947 is enough to prevent anybody from looking inside for
3948 associativity, but won't generate any code. */
3949 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3950 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3951 || typecode == VECTOR_TYPE))
400fbf9f 3952 {
c9f9eb5d 3953 error_at (location, "wrong type argument to unary plus");
3e4093b6 3954 return error_mark_node;
400fbf9f 3955 }
3e4093b6
RS
3956 else if (!noconvert)
3957 arg = default_conversion (arg);
db3927fb 3958 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3959 break;
3960
3e4093b6
RS
3961 case NEGATE_EXPR:
3962 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3963 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3964 || typecode == VECTOR_TYPE))
3965 {
c9f9eb5d 3966 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3967 return error_mark_node;
3968 }
3969 else if (!noconvert)
3970 arg = default_conversion (arg);
400fbf9f
JW
3971 break;
3972
3e4093b6 3973 case BIT_NOT_EXPR:
462643f0
AP
3974 /* ~ works on integer types and non float vectors. */
3975 if (typecode == INTEGER_TYPE
3976 || (typecode == VECTOR_TYPE
3977 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3978 {
3e4093b6
RS
3979 if (!noconvert)
3980 arg = default_conversion (arg);
03d5b1f5 3981 }
3e4093b6 3982 else if (typecode == COMPLEX_TYPE)
400fbf9f 3983 {
3e4093b6 3984 code = CONJ_EXPR;
c1771a20 3985 pedwarn (location, OPT_Wpedantic,
fcf73884 3986 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3987 if (!noconvert)
3988 arg = default_conversion (arg);
3989 }
3990 else
3991 {
c9f9eb5d 3992 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3993 return error_mark_node;
400fbf9f
JW
3994 }
3995 break;
3996
3e4093b6 3997 case ABS_EXPR:
11017cc7 3998 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3999 {
c9f9eb5d 4000 error_at (location, "wrong type argument to abs");
3e4093b6 4001 return error_mark_node;
400fbf9f 4002 }
3e4093b6
RS
4003 else if (!noconvert)
4004 arg = default_conversion (arg);
400fbf9f
JW
4005 break;
4006
3e4093b6
RS
4007 case CONJ_EXPR:
4008 /* Conjugating a real value is a no-op, but allow it anyway. */
4009 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4010 || typecode == COMPLEX_TYPE))
400fbf9f 4011 {
c9f9eb5d 4012 error_at (location, "wrong type argument to conjugation");
3e4093b6 4013 return error_mark_node;
400fbf9f 4014 }
3e4093b6
RS
4015 else if (!noconvert)
4016 arg = default_conversion (arg);
400fbf9f
JW
4017 break;
4018
3e4093b6 4019 case TRUTH_NOT_EXPR:
ab22c1fa 4020 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4021 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4022 && typecode != COMPLEX_TYPE)
400fbf9f 4023 {
c9f9eb5d
AH
4024 error_at (location,
4025 "wrong type argument to unary exclamation mark");
3e4093b6 4026 return error_mark_node;
400fbf9f 4027 }
a27d595d
JM
4028 if (int_operands)
4029 {
4030 arg = c_objc_common_truthvalue_conversion (location, xarg);
4031 arg = remove_c_maybe_const_expr (arg);
4032 }
4033 else
4034 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4035 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4036 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4037 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4038 location = EXPR_LOCATION (ret);
c9f9eb5d 4039 goto return_build_unary_op;
3e4093b6 4040
3e4093b6 4041 case REALPART_EXPR:
3e4093b6 4042 case IMAGPART_EXPR:
fb52b50a
NF
4043 ret = build_real_imag_expr (location, code, arg);
4044 if (ret == error_mark_node)
4045 return error_mark_node;
8ce94e44
JM
4046 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4047 eptype = TREE_TYPE (eptype);
c9f9eb5d 4048 goto return_build_unary_op;
3e4093b6
RS
4049
4050 case PREINCREMENT_EXPR:
4051 case POSTINCREMENT_EXPR:
4052 case PREDECREMENT_EXPR:
4053 case POSTDECREMENT_EXPR:
3e4093b6 4054
928c19bb
JM
4055 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4056 {
4057 tree inner = build_unary_op (location, code,
4058 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4059 if (inner == error_mark_node)
4060 return error_mark_node;
4061 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4062 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4063 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4064 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4065 goto return_build_unary_op;
4066 }
4067
925e8657
NP
4068 /* Complain about anything that is not a true lvalue. In
4069 Objective-C, skip this check for property_refs. */
f90e8e2e 4070 if (!objc_is_property_ref (arg)
7bd11157
TT
4071 && !lvalue_or_else (location,
4072 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4073 || code == POSTINCREMENT_EXPR)
4074 ? lv_increment
4075 : lv_decrement)))
928c19bb
JM
4076 return error_mark_node;
4077
09639a83
ILT
4078 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4079 {
4080 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4081 warning_at (location, OPT_Wc___compat,
4082 "increment of enumeration value is invalid in C++");
4083 else
4084 warning_at (location, OPT_Wc___compat,
4085 "decrement of enumeration value is invalid in C++");
4086 }
4087
928c19bb
JM
4088 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4089 arg = c_fully_fold (arg, false, NULL);
4090
267bac10
JM
4091 bool atomic_op;
4092 atomic_op = really_atomic_lvalue (arg);
4093
3e4093b6
RS
4094 /* Increment or decrement the real part of the value,
4095 and don't change the imaginary part. */
4096 if (typecode == COMPLEX_TYPE)
400fbf9f 4097 {
3e4093b6
RS
4098 tree real, imag;
4099
c1771a20 4100 pedwarn (location, OPT_Wpedantic,
509c9d60 4101 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4102
267bac10
JM
4103 if (!atomic_op)
4104 {
4105 arg = stabilize_reference (arg);
4106 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4107 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4108 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4109 if (real == error_mark_node || imag == error_mark_node)
4110 return error_mark_node;
4111 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4112 real, imag);
4113 goto return_build_unary_op;
4114 }
400fbf9f 4115 }
3e4093b6
RS
4116
4117 /* Report invalid types. */
4118
ab22c1fa 4119 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4120 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4121 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4122 {
3e4093b6 4123 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4124 error_at (location, "wrong type argument to increment");
c22cacf3 4125 else
c9f9eb5d 4126 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4127
4128 return error_mark_node;
400fbf9f 4129 }
400fbf9f 4130
3e4093b6
RS
4131 {
4132 tree inc;
400fbf9f 4133
3e4093b6
RS
4134 argtype = TREE_TYPE (arg);
4135
4136 /* Compute the increment. */
4137
4138 if (typecode == POINTER_TYPE)
4139 {
a0e24419 4140 /* If pointer target is an incomplete type,
3e4093b6 4141 we just cannot know how to do the arithmetic. */
b70cef5d 4142 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4143 {
4144 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4145 error_at (location,
a0e24419
MP
4146 "increment of pointer to an incomplete type %qT",
4147 TREE_TYPE (argtype));
3e4093b6 4148 else
c9f9eb5d 4149 error_at (location,
a0e24419
MP
4150 "decrement of pointer to an incomplete type %qT",
4151 TREE_TYPE (argtype));
3e4093b6 4152 }
b70cef5d
JJ
4153 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4154 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4155 {
3e4093b6 4156 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4157 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4158 "wrong type argument to increment");
3e4093b6 4159 else
44d90fe1 4160 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4161 "wrong type argument to decrement");
3e4093b6
RS
4162 }
4163
b70cef5d 4164 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4165 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4166 }
b70cef5d 4167 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4168 {
4169 /* For signed fract types, we invert ++ to -- or
4170 -- to ++, and change inc from 1 to -1, because
4171 it is not possible to represent 1 in signed fract constants.
4172 For unsigned fract types, the result always overflows and
4173 we get an undefined (original) or the maximum value. */
4174 if (code == PREINCREMENT_EXPR)
4175 code = PREDECREMENT_EXPR;
4176 else if (code == PREDECREMENT_EXPR)
4177 code = PREINCREMENT_EXPR;
4178 else if (code == POSTINCREMENT_EXPR)
4179 code = POSTDECREMENT_EXPR;
4180 else /* code == POSTDECREMENT_EXPR */
4181 code = POSTINCREMENT_EXPR;
4182
4183 inc = integer_minus_one_node;
4184 inc = convert (argtype, inc);
4185 }
3e4093b6 4186 else
5be014d5 4187 {
241b71bb
TV
4188 inc = VECTOR_TYPE_P (argtype)
4189 ? build_one_cst (argtype)
4190 : integer_one_node;
5be014d5
AP
4191 inc = convert (argtype, inc);
4192 }
3e4093b6 4193
925e8657
NP
4194 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4195 need to ask Objective-C to build the increment or decrement
4196 expression for it. */
4197 if (objc_is_property_ref (arg))
f90e8e2e 4198 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4199 arg, inc);
4200
3e4093b6 4201 /* Report a read-only lvalue. */
f37acdf9 4202 if (TYPE_READONLY (argtype))
953ff289 4203 {
c02065fc 4204 readonly_error (location, arg,
953ff289
DN
4205 ((code == PREINCREMENT_EXPR
4206 || code == POSTINCREMENT_EXPR)
4207 ? lv_increment : lv_decrement));
4208 return error_mark_node;
4209 }
f37acdf9
JM
4210 else if (TREE_READONLY (arg))
4211 readonly_warning (arg,
4212 ((code == PREINCREMENT_EXPR
4213 || code == POSTINCREMENT_EXPR)
4214 ? lv_increment : lv_decrement));
3e4093b6 4215
267bac10
JM
4216 /* If the argument is atomic, use the special code sequences for
4217 atomic compound assignment. */
4218 if (atomic_op)
4219 {
4220 arg = stabilize_reference (arg);
4221 ret = build_atomic_assign (location, arg,
4222 ((code == PREINCREMENT_EXPR
4223 || code == POSTINCREMENT_EXPR)
4224 ? PLUS_EXPR
4225 : MINUS_EXPR),
4226 (FRACT_MODE_P (TYPE_MODE (argtype))
4227 ? inc
4228 : integer_one_node),
4229 (code == POSTINCREMENT_EXPR
4230 || code == POSTDECREMENT_EXPR));
4231 goto return_build_unary_op;
4232 }
4233
3e4093b6
RS
4234 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4235 val = boolean_increment (code, arg);
4236 else
53fb4de3 4237 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4238 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4239 if (TREE_CODE (val) != code)
6de9cd9a 4240 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4241 ret = val;
4242 goto return_build_unary_op;
3e4093b6
RS
4243 }
4244
4245 case ADDR_EXPR:
4246 /* Note that this operation never does default_conversion. */
4247
2b4b7036
JM
4248 /* The operand of unary '&' must be an lvalue (which excludes
4249 expressions of type void), or, in C99, the result of a [] or
4250 unary '*' operator. */
4251 if (VOID_TYPE_P (TREE_TYPE (arg))
4252 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4253 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4254 pedwarn (location, 0, "taking address of expression of type %<void%>");
4255
3e4093b6 4256 /* Let &* cancel out to simplify resulting code. */
22d03525 4257 if (INDIRECT_REF_P (arg))
400fbf9f 4258 {
3e4093b6
RS
4259 /* Don't let this be an lvalue. */
4260 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4261 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4262 ret = TREE_OPERAND (arg, 0);
4263 goto return_build_unary_op;
400fbf9f 4264 }
1eb8759b 4265
3e4093b6
RS
4266 /* Anything not already handled and not a true memory reference
4267 or a non-lvalue array is an error. */
ee45a32d
EB
4268 if (typecode != FUNCTION_TYPE && !flag
4269 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4270 return error_mark_node;
b6a10c9f 4271
928c19bb
JM
4272 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4273 folding later. */
4274 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4275 {
4276 tree inner = build_unary_op (location, code,
4277 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4278 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4279 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4280 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4281 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4282 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4283 goto return_build_unary_op;
4284 }
4285
3e4093b6
RS
4286 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4287 argtype = TREE_TYPE (arg);
400fbf9f 4288
3e4093b6 4289 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4290 to which the address will point. This is only needed
f2c1da78 4291 for function types. */
6615c446 4292 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4293 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4294 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4295 {
4296 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4297 int quals = orig_quals;
4298
4299 if (TREE_READONLY (arg))
4300 quals |= TYPE_QUAL_CONST;
4301 if (TREE_THIS_VOLATILE (arg))
4302 quals |= TYPE_QUAL_VOLATILE;
4303
f2c1da78
JM
4304 argtype = c_build_qualified_type (argtype, quals);
4305 }
400fbf9f 4306
ee45a32d
EB
4307 switch (TREE_CODE (arg))
4308 {
4309 case COMPONENT_REF:
4310 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4311 {
4312 error ("cannot take address of bit-field %qD",
4313 TREE_OPERAND (arg, 1));
4314 return error_mark_node;
4315 }
4316
4317 /* ... fall through ... */
4318
4319 case ARRAY_REF:
4320 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4321 {
4322 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4323 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4324 {
4325 error ("cannot take address of scalar with reverse storage "
4326 "order");
4327 return error_mark_node;
4328 }
4329
4330 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4331 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4332 warning (OPT_Wscalar_storage_order, "address of array with "
4333 "reverse scalar storage order requested");
4334 }
4335
4336 default:
4337 break;
4338 }
4339
3e4093b6
RS
4340 if (!c_mark_addressable (arg))
4341 return error_mark_node;
400fbf9f 4342
abb54d14
JM
4343 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4344 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4345
5cc200fc 4346 argtype = build_pointer_type (argtype);
5e55f99d
RH
4347
4348 /* ??? Cope with user tricks that amount to offsetof. Delete this
4349 when we have proper support for integer constant expressions. */
4350 val = get_base_address (arg);
22d03525 4351 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4352 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4353 {
cf9e9959 4354 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4355 goto return_build_unary_op;
3aa2ddb8 4356 }
5e55f99d 4357
5cc200fc 4358 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4359
c9f9eb5d
AH
4360 ret = val;
4361 goto return_build_unary_op;
400fbf9f 4362
3e4093b6 4363 default:
1344f9a3 4364 gcc_unreachable ();
3e4093b6 4365 }
400fbf9f 4366
3e4093b6
RS
4367 if (argtype == 0)
4368 argtype = TREE_TYPE (arg);
928c19bb
JM
4369 if (TREE_CODE (arg) == INTEGER_CST)
4370 ret = (require_constant_value
db3927fb
AH
4371 ? fold_build1_initializer_loc (location, code, argtype, arg)
4372 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4373 else
4374 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4375 return_build_unary_op:
4376 gcc_assert (ret != error_mark_node);
928c19bb
JM
4377 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4378 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4379 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4380 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4381 ret = note_integer_operands (ret);
8ce94e44
JM
4382 if (eptype)
4383 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4384 protected_set_expr_location (ret, location);
4385 return ret;
3e4093b6 4386}
400fbf9f 4387
3e4093b6
RS
4388/* Return nonzero if REF is an lvalue valid for this language.
4389 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4390 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4391
1e4ae551 4392bool
58f9752a 4393lvalue_p (const_tree ref)
3e4093b6 4394{
58f9752a 4395 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4396
3e4093b6
RS
4397 switch (code)
4398 {
4399 case REALPART_EXPR:
4400 case IMAGPART_EXPR:
4401 case COMPONENT_REF:
4402 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4403
928c19bb
JM
4404 case C_MAYBE_CONST_EXPR:
4405 return lvalue_p (TREE_OPERAND (ref, 1));
4406
3e4093b6
RS
4407 case COMPOUND_LITERAL_EXPR:
4408 case STRING_CST:
4409 return 1;
400fbf9f 4410
3e4093b6
RS
4411 case INDIRECT_REF:
4412 case ARRAY_REF:
36536d79 4413 case ARRAY_NOTATION_REF:
3e4093b6
RS
4414 case VAR_DECL:
4415 case PARM_DECL:
4416 case RESULT_DECL:
4417 case ERROR_MARK:
4418 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4419 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4420
3e4093b6 4421 case BIND_EXPR:
3e4093b6 4422 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4423
3e4093b6
RS
4424 default:
4425 return 0;
4426 }
4427}
400fbf9f 4428\f
f37acdf9
JM
4429/* Give a warning for storing in something that is read-only in GCC
4430 terms but not const in ISO C terms. */
4431
4432static void
4433readonly_warning (tree arg, enum lvalue_use use)
4434{
4435 switch (use)
4436 {
4437 case lv_assign:
4438 warning (0, "assignment of read-only location %qE", arg);
4439 break;
4440 case lv_increment:
4441 warning (0, "increment of read-only location %qE", arg);
4442 break;
4443 case lv_decrement:
4444 warning (0, "decrement of read-only location %qE", arg);
4445 break;
4446 default:
4447 gcc_unreachable ();
4448 }
4449 return;
4450}
4451
37dc0d8d
JM
4452
4453/* Return nonzero if REF is an lvalue valid for this language;
4454 otherwise, print an error message and return zero. USE says
7bd11157
TT
4455 how the lvalue is being used and so selects the error message.
4456 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4457
4458static int
7bd11157 4459lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4460{
4461 int win = lvalue_p (ref);
4462
4463 if (!win)
7bd11157 4464 lvalue_error (loc, use);
37dc0d8d
JM
4465
4466 return win;
4467}
3e4093b6
RS
4468\f
4469/* Mark EXP saying that we need to be able to take the
4470 address of it; it should not be allocated in a register.
4471 Returns true if successful. */
54c93c30 4472
3e4093b6
RS
4473bool
4474c_mark_addressable (tree exp)
400fbf9f 4475{
3e4093b6 4476 tree x = exp;
95602da1 4477
3e4093b6
RS
4478 while (1)
4479 switch (TREE_CODE (x))
4480 {
4481 case COMPONENT_REF:
3e4093b6
RS
4482 case ADDR_EXPR:
4483 case ARRAY_REF:
4484 case REALPART_EXPR:
4485 case IMAGPART_EXPR:
4486 x = TREE_OPERAND (x, 0);
4487 break;
95602da1 4488
3e4093b6
RS
4489 case COMPOUND_LITERAL_EXPR:
4490 case CONSTRUCTOR:
4491 TREE_ADDRESSABLE (x) = 1;
4492 return true;
95602da1 4493
3e4093b6
RS
4494 case VAR_DECL:
4495 case CONST_DECL:
4496 case PARM_DECL:
4497 case RESULT_DECL:
5baeaac0 4498 if (C_DECL_REGISTER (x)
3e4093b6
RS
4499 && DECL_NONLOCAL (x))
4500 {
62f9079a 4501 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4502 {
0039fa55
AN
4503 error
4504 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4505 return false;
4506 }
509c9d60 4507 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4508 }
5baeaac0 4509 else if (C_DECL_REGISTER (x))
3e4093b6 4510 {
62f9079a 4511 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4512 error ("address of global register variable %qD requested", x);
4513 else
4514 error ("address of register variable %qD requested", x);
4515 return false;
3e4093b6 4516 }
400fbf9f 4517
3e4093b6
RS
4518 /* drops in */
4519 case FUNCTION_DECL:
4520 TREE_ADDRESSABLE (x) = 1;
4521 /* drops out */
4522 default:
4523 return true;
4524 }
4525}
4526\f
2d2e923f
MLI
4527/* Convert EXPR to TYPE, warning about conversion problems with
4528 constants. SEMANTIC_TYPE is the type this conversion would use
4529 without excess precision. If SEMANTIC_TYPE is NULL, this function
4530 is equivalent to convert_and_check. This function is a wrapper that
4531 handles conversions that may be different than
4532 the usual ones because of excess precision. */
4533
4534static tree
68fca595
MP
4535ep_convert_and_check (location_t loc, tree type, tree expr,
4536 tree semantic_type)
2d2e923f
MLI
4537{
4538 if (TREE_TYPE (expr) == type)
4539 return expr;
4540
4541 if (!semantic_type)
68fca595 4542 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4543
4544 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4545 && TREE_TYPE (expr) != semantic_type)
4546 {
4547 /* For integers, we need to check the real conversion, not
4548 the conversion to the excess precision type. */
68fca595 4549 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4550 }
4551 /* Result type is the excess precision type, which should be
4552 large enough, so do not check. */
4553 return convert (type, expr);
4554}
4555
928c19bb
JM
4556/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4557 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4558 if folded to an integer constant then the unselected half may
4559 contain arbitrary operations not normally permitted in constant
c2255bc4 4560 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4561
4562tree
744aa42f 4563build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4564 tree op1, tree op1_original_type, tree op2,
4565 tree op2_original_type)
400fbf9f 4566{
3e4093b6
RS
4567 tree type1;
4568 tree type2;
4569 enum tree_code code1;
4570 enum tree_code code2;
4571 tree result_type = NULL;
2d2e923f 4572 tree semantic_result_type = NULL;
3e4093b6 4573 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4574 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4575 bool ifexp_int_operands;
928c19bb 4576 tree ret;
400fbf9f 4577
4d84fe7c
JM
4578 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4579 if (op1_int_operands)
4580 op1 = remove_c_maybe_const_expr (op1);
4581 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4582 if (op2_int_operands)
4583 op2 = remove_c_maybe_const_expr (op2);
4584 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4585 if (ifexp_int_operands)
4586 ifexp = remove_c_maybe_const_expr (ifexp);
4587
3e4093b6
RS
4588 /* Promote both alternatives. */
4589
4590 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4591 op1 = default_conversion (op1);
4592 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4593 op2 = default_conversion (op2);
4594
4595 if (TREE_CODE (ifexp) == ERROR_MARK
4596 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4597 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4598 return error_mark_node;
400fbf9f 4599
3e4093b6
RS
4600 type1 = TREE_TYPE (op1);
4601 code1 = TREE_CODE (type1);
4602 type2 = TREE_TYPE (op2);
4603 code2 = TREE_CODE (type2);
4604
1807ffc1
MS
4605 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4606 return error_mark_node;
4607
4608 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4609 return error_mark_node;
4610
b1adf557
JM
4611 /* C90 does not permit non-lvalue arrays in conditional expressions.
4612 In C99 they will be pointers by now. */
4613 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4614 {
744aa42f 4615 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4616 return error_mark_node;
4617 }
4618
8ce94e44
JM
4619 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4620 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4621 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4622 || code1 == COMPLEX_TYPE)
4623 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4624 || code2 == COMPLEX_TYPE))
4625 {
2d2e923f 4626 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4627 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4628 {
4629 op1 = TREE_OPERAND (op1, 0);
4630 type1 = TREE_TYPE (op1);
4631 gcc_assert (TREE_CODE (type1) == code1);
4632 }
4633 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4634 {
4635 op2 = TREE_OPERAND (op2, 0);
4636 type2 = TREE_TYPE (op2);
4637 gcc_assert (TREE_CODE (type2) == code2);
4638 }
4639 }
4640
d130ae11
ILT
4641 if (warn_cxx_compat)
4642 {
4643 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4644 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4645
4646 if (TREE_CODE (t1) == ENUMERAL_TYPE
4647 && TREE_CODE (t2) == ENUMERAL_TYPE
4648 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4649 warning_at (colon_loc, OPT_Wc___compat,
4650 ("different enum types in conditional is "
4651 "invalid in C++: %qT vs %qT"),
4652 t1, t2);
4653 }
4654
3e4093b6
RS
4655 /* Quickly detect the usual case where op1 and op2 have the same type
4656 after promotion. */
4657 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4658 {
3e4093b6
RS
4659 if (type1 == type2)
4660 result_type = type1;
4661 else
4662 result_type = TYPE_MAIN_VARIANT (type1);
4663 }
4664 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4665 || code1 == COMPLEX_TYPE)
4666 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4667 || code2 == COMPLEX_TYPE))
3e4093b6 4668 {
ccf7f880 4669 result_type = c_common_type (type1, type2);
0a0b3574
MM
4670 do_warn_double_promotion (result_type, type1, type2,
4671 "implicit conversion from %qT to %qT to "
4672 "match other result of conditional",
4673 colon_loc);
400fbf9f 4674
3e4093b6
RS
4675 /* If -Wsign-compare, warn here if type1 and type2 have
4676 different signedness. We'll promote the signed to unsigned
4677 and later code won't know it used to be different.
4678 Do this check on the original types, so that explicit casts
4679 will be considered, but default promotions won't. */
7d882b83 4680 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4681 {
8df83eae
RK
4682 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4683 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4684
3e4093b6
RS
4685 if (unsigned_op1 ^ unsigned_op2)
4686 {
6ac01510
ILT
4687 bool ovf;
4688
3e4093b6
RS
4689 /* Do not warn if the result type is signed, since the
4690 signed type will only be chosen if it can represent
4691 all the values of the unsigned type. */
3f75a254 4692 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4693 /* OK */;
3e4093b6 4694 else
928c19bb
JM
4695 {
4696 bool op1_maybe_const = true;
4697 bool op2_maybe_const = true;
4698
4699 /* Do not warn if the signed quantity is an
4700 unsuffixed integer literal (or some static
4701 constant expression involving such literals) and
4702 it is non-negative. This warning requires the
4703 operands to be folded for best results, so do
4704 that folding in this case even without
4705 warn_sign_compare to avoid warning options
4706 possibly affecting code generation. */
f5178456
RS
4707 c_inhibit_evaluation_warnings
4708 += (ifexp == truthvalue_false_node);
928c19bb
JM
4709 op1 = c_fully_fold (op1, require_constant_value,
4710 &op1_maybe_const);
f5178456
RS
4711 c_inhibit_evaluation_warnings
4712 -= (ifexp == truthvalue_false_node);
4713
4714 c_inhibit_evaluation_warnings
4715 += (ifexp == truthvalue_true_node);
928c19bb
JM
4716 op2 = c_fully_fold (op2, require_constant_value,
4717 &op2_maybe_const);
f5178456
RS
4718 c_inhibit_evaluation_warnings
4719 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4720
4721 if (warn_sign_compare)
4722 {
4723 if ((unsigned_op2
4724 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4725 || (unsigned_op1
4726 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4727 /* OK */;
4728 else
744aa42f
ILT
4729 warning_at (colon_loc, OPT_Wsign_compare,
4730 ("signed and unsigned type in "
4731 "conditional expression"));
928c19bb
JM
4732 }
4733 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4734 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4735 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4736 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4737 }
3e4093b6
RS
4738 }
4739 }
4740 }
4741 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4742 {
fcf73884 4743 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4744 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4745 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4746 result_type = void_type_node;
4747 }
4748 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4749 {
36c5e70a
BE
4750 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4751 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4752 addr_space_t as_common;
4753
744aa42f 4754 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4755 result_type = common_pointer_type (type1, type2);
6aa3c60d 4756 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4757 result_type = type2;
6aa3c60d 4758 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4759 result_type = type1;
4760 else if (!addr_space_superset (as1, as2, &as_common))
4761 {
4762 error_at (colon_loc, "pointers to disjoint address spaces "
4763 "used in conditional expression");
4764 return error_mark_node;
4765 }
267bac10
JM
4766 else if (VOID_TYPE_P (TREE_TYPE (type1))
4767 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4768 {
768952be
MU
4769 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4770 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4771 & ~TYPE_QUALS (TREE_TYPE (type1))))
4772 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4773 "pointer to array loses qualifier "
4774 "in conditional expression");
4775
fcf73884 4776 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4777 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4778 "ISO C forbids conditional expr between "
bda67431 4779 "%<void *%> and function pointer");
3e4093b6
RS
4780 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4781 TREE_TYPE (type2)));
34a80643 4782 }
267bac10
JM
4783 else if (VOID_TYPE_P (TREE_TYPE (type2))
4784 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4785 {
768952be
MU
4786 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4787 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4788 & ~TYPE_QUALS (TREE_TYPE (type2))))
4789 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4790 "pointer to array loses qualifier "
4791 "in conditional expression");
4792
fcf73884 4793 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4794 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4795 "ISO C forbids conditional expr between "
bda67431 4796 "%<void *%> and function pointer");
3e4093b6
RS
4797 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4798 TREE_TYPE (type1)));
1c2a9b35 4799 }
b581b85b
NP
4800 /* Objective-C pointer comparisons are a bit more lenient. */
4801 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4802 result_type = objc_common_type (type1, type2);
34a80643 4803 else
ab87f8c8 4804 {
36c5e70a
BE
4805 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4806
b581b85b
NP
4807 pedwarn (colon_loc, 0,
4808 "pointer type mismatch in conditional expression");
36c5e70a
BE
4809 result_type = build_pointer_type
4810 (build_qualified_type (void_type_node, qual));
ab87f8c8 4811 }
3e4093b6
RS
4812 }
4813 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4814 {
6aa3c60d 4815 if (!null_pointer_constant_p (orig_op2))
744aa42f 4816 pedwarn (colon_loc, 0,
509c9d60 4817 "pointer/integer type mismatch in conditional expression");
3e4093b6 4818 else
ab87f8c8 4819 {
3e4093b6 4820 op2 = null_pointer_node;
ab87f8c8 4821 }
3e4093b6
RS
4822 result_type = type1;
4823 }
4824 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4825 {
6aa3c60d 4826 if (!null_pointer_constant_p (orig_op1))
744aa42f 4827 pedwarn (colon_loc, 0,
509c9d60 4828 "pointer/integer type mismatch in conditional expression");
3e4093b6 4829 else
ab87f8c8 4830 {
3e4093b6 4831 op1 = null_pointer_node;
ab87f8c8 4832 }
3e4093b6
RS
4833 result_type = type2;
4834 }
1c2a9b35 4835
3e4093b6
RS
4836 if (!result_type)
4837 {
4838 if (flag_cond_mismatch)
4839 result_type = void_type_node;
4840 else
400fbf9f 4841 {
c2255bc4 4842 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4843 return error_mark_node;
400fbf9f 4844 }
3e4093b6 4845 }
400fbf9f 4846
3e4093b6
RS
4847 /* Merge const and volatile flags of the incoming types. */
4848 result_type
4849 = build_type_variant (result_type,
afbd0665
AS
4850 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4851 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4852
68fca595
MP
4853 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4854 semantic_result_type);
4855 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4856 semantic_result_type);
b6a10c9f 4857
928c19bb
JM
4858 if (ifexp_bcp && ifexp == truthvalue_true_node)
4859 {
4860 op2_int_operands = true;
4861 op1 = c_fully_fold (op1, require_constant_value, NULL);
4862 }
4863 if (ifexp_bcp && ifexp == truthvalue_false_node)
4864 {
4865 op1_int_operands = true;
4866 op2 = c_fully_fold (op2, require_constant_value, NULL);
4867 }
4d84fe7c 4868 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4869 && op1_int_operands
4870 && op2_int_operands);
4871 if (int_operands)
4872 {
4873 int_const = ((ifexp == truthvalue_true_node
4874 && TREE_CODE (orig_op1) == INTEGER_CST
4875 && !TREE_OVERFLOW (orig_op1))
4876 || (ifexp == truthvalue_false_node
4877 && TREE_CODE (orig_op2) == INTEGER_CST
4878 && !TREE_OVERFLOW (orig_op2)));
4879 }
9f47c7e5
IE
4880
4881 /* Need to convert condition operand into a vector mask. */
4882 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
4883 {
4884 tree vectype = TREE_TYPE (ifexp);
4885 tree elem_type = TREE_TYPE (vectype);
4886 tree zero = build_int_cst (elem_type, 0);
4887 tree zero_vec = build_vector_from_val (vectype, zero);
4888 tree cmp_type = build_same_sized_truth_vector_type (vectype);
4889 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
4890 }
4891
928c19bb 4892 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4893 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4894 else
4895 {
01c7ccbb
JM
4896 if (int_operands)
4897 {
f34f1c87
MP
4898 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4899 nested inside of the expression. */
4900 op1 = c_fully_fold (op1, false, NULL);
4901 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4902 }
928c19bb
JM
4903 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4904 if (int_operands)
4905 ret = note_integer_operands (ret);
4906 }
2d2e923f
MLI
4907 if (semantic_result_type)
4908 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4909
c2255bc4 4910 protected_set_expr_location (ret, colon_loc);
928c19bb 4911 return ret;
3e4093b6
RS
4912}
4913\f
487a92fe 4914/* Return a compound expression that performs two expressions and
c2255bc4
AH
4915 returns the value of the second of them.
4916
4917 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4918
3e4093b6 4919tree
c2255bc4 4920build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4921{
4d84fe7c 4922 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4923 tree eptype = NULL_TREE;
928c19bb
JM
4924 tree ret;
4925
b72271b9 4926 if (flag_cilkplus
939b37da
BI
4927 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4928 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4929 {
4930 error_at (loc,
4931 "spawned function call cannot be part of a comma expression");
4932 return error_mark_node;
4933 }
4d84fe7c
JM
4934 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4935 if (expr1_int_operands)
4936 expr1 = remove_c_maybe_const_expr (expr1);
4937 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4938 if (expr2_int_operands)
4939 expr2 = remove_c_maybe_const_expr (expr2);
4940
8ce94e44
JM
4941 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4942 expr1 = TREE_OPERAND (expr1, 0);
4943 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4944 {
4945 eptype = TREE_TYPE (expr2);
4946 expr2 = TREE_OPERAND (expr2, 0);
4947 }
4948
3f75a254 4949 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4950 {
4951 /* The left-hand operand of a comma expression is like an expression
c5409249 4952 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4953 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4954 if (warn_unused_value)
47aecf47 4955 {
e14a6540 4956 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4957 && CONVERT_EXPR_P (expr1))
47aecf47 4958 ; /* (void) a, b */
e14a6540
JM
4959 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4960 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4961 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4962 ; /* (void) a, (void) b, c */
4963 else
b8698a0f 4964 warning_at (loc, OPT_Wunused_value,
c2255bc4 4965 "left-hand operand of comma expression has no effect");
47aecf47 4966 }
3e4093b6 4967 }
789eadcd
MP
4968 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4969 && warn_unused_value)
4970 {
4971 tree r = expr1;
4972 location_t cloc = loc;
4973 while (TREE_CODE (r) == COMPOUND_EXPR)
4974 {
4975 if (EXPR_HAS_LOCATION (r))
4976 cloc = EXPR_LOCATION (r);
4977 r = TREE_OPERAND (r, 1);
4978 }
4979 if (!TREE_SIDE_EFFECTS (r)
4980 && !VOID_TYPE_P (TREE_TYPE (r))
4981 && !CONVERT_EXPR_P (r))
4982 warning_at (cloc, OPT_Wunused_value,
4983 "right-hand operand of comma expression has no effect");
4984 }
400fbf9f 4985
3e4093b6
RS
4986 /* With -Wunused, we should also warn if the left-hand operand does have
4987 side-effects, but computes a value which is not used. For example, in
4988 `foo() + bar(), baz()' the result of the `+' operator is not used,
4989 so we should issue a warning. */
4990 else if (warn_unused_value)
c2255bc4 4991 warn_if_unused_value (expr1, loc);
400fbf9f 4992
e63d6886
AP
4993 if (expr2 == error_mark_node)
4994 return error_mark_node;
4995
928c19bb
JM
4996 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4997
4998 if (flag_isoc99
4d84fe7c
JM
4999 && expr1_int_operands
5000 && expr2_int_operands)
928c19bb
JM
5001 ret = note_integer_operands (ret);
5002
8ce94e44
JM
5003 if (eptype)
5004 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5005
c2255bc4 5006 protected_set_expr_location (ret, loc);
928c19bb 5007 return ret;
3e4093b6 5008}
400fbf9f 5009
67165eb3
ILT
5010/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5011 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5012 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5013 of the cast. -Wcast-qual appeared on the command line. Named
5014 address space qualifiers are not handled here, because they result
5015 in different warnings. */
67165eb3
ILT
5016
5017static void
2ee3cb35 5018handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5019{
5020 tree in_type = type;
5021 tree in_otype = otype;
5022 int added = 0;
5023 int discarded = 0;
5024 bool is_const;
5025
5026 /* Check that the qualifiers on IN_TYPE are a superset of the
5027 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5028 nodes is uninteresting and we stop as soon as we hit a
5029 non-POINTER_TYPE node on either type. */
5030 do
5031 {
5032 in_otype = TREE_TYPE (in_otype);
5033 in_type = TREE_TYPE (in_type);
5034
5035 /* GNU C allows cv-qualified function types. 'const' means the
5036 function is very pure, 'volatile' means it can't return. We
5037 need to warn when such qualifiers are added, not when they're
5038 taken away. */
5039 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5040 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5041 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5042 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5043 else
36c5e70a
BE
5044 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5045 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5046 }
5047 while (TREE_CODE (in_type) == POINTER_TYPE
5048 && TREE_CODE (in_otype) == POINTER_TYPE);
5049
5050 if (added)
2ee3cb35
MLI
5051 warning_at (loc, OPT_Wcast_qual,
5052 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5053
5054 if (discarded)
5055 /* There are qualifiers present in IN_OTYPE that are not present
5056 in IN_TYPE. */
2ee3cb35 5057 warning_at (loc, OPT_Wcast_qual,
7485aeea 5058 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5059 discarded);
67165eb3
ILT
5060
5061 if (added || discarded)
5062 return;
5063
5064 /* A cast from **T to const **T is unsafe, because it can cause a
5065 const value to be changed with no additional warning. We only
5066 issue this warning if T is the same on both sides, and we only
5067 issue the warning if there are the same number of pointers on
5068 both sides, as otherwise the cast is clearly unsafe anyhow. A
5069 cast is unsafe when a qualifier is added at one level and const
5070 is not present at all outer levels.
5071
5072 To issue this warning, we check at each level whether the cast
5073 adds new qualifiers not already seen. We don't need to special
5074 case function types, as they won't have the same
5075 TYPE_MAIN_VARIANT. */
5076
5077 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5078 return;
5079 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5080 return;
5081
5082 in_type = type;
5083 in_otype = otype;
5084 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5085 do
5086 {
5087 in_type = TREE_TYPE (in_type);
5088 in_otype = TREE_TYPE (in_otype);
5089 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5090 && !is_const)
5091 {
2ee3cb35
MLI
5092 warning_at (loc, OPT_Wcast_qual,
5093 "to be safe all intermediate pointers in cast from "
5094 "%qT to %qT must be %<const%> qualified",
5095 otype, type);
67165eb3
ILT
5096 break;
5097 }
5098 if (is_const)
5099 is_const = TYPE_READONLY (in_type);
5100 }
5101 while (TREE_CODE (in_type) == POINTER_TYPE);
5102}
5103
b8698a0f 5104/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5105 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5106
3e4093b6 5107tree
c2255bc4 5108build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5109{
8ce94e44
JM
5110 tree value;
5111
5112 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5113 expr = TREE_OPERAND (expr, 0);
5114
5115 value = expr;
400fbf9f 5116
3e4093b6
RS
5117 if (type == error_mark_node || expr == error_mark_node)
5118 return error_mark_node;
400fbf9f 5119
3e4093b6
RS
5120 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5121 only in <protocol> qualifications. But when constructing cast expressions,
5122 the protocols do matter and must be kept around. */
700686fa
ZL
5123 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5124 return build1 (NOP_EXPR, type, expr);
5125
5126 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5127
3e4093b6
RS
5128 if (TREE_CODE (type) == ARRAY_TYPE)
5129 {
c2255bc4 5130 error_at (loc, "cast specifies array type");
3e4093b6
RS
5131 return error_mark_node;
5132 }
400fbf9f 5133
3e4093b6
RS
5134 if (TREE_CODE (type) == FUNCTION_TYPE)
5135 {
c2255bc4 5136 error_at (loc, "cast specifies function type");
3e4093b6
RS
5137 return error_mark_node;
5138 }
400fbf9f 5139
808d6eaa
JM
5140 if (!VOID_TYPE_P (type))
5141 {
5142 value = require_complete_type (value);
5143 if (value == error_mark_node)
5144 return error_mark_node;
5145 }
5146
3e4093b6
RS
5147 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5148 {
3e636daf 5149 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5150 pedwarn (loc, OPT_Wpedantic,
fcf73884 5151 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5152
5153 /* Convert to remove any qualifiers from VALUE's type. */
5154 value = convert (type, value);
3e4093b6
RS
5155 }
5156 else if (TREE_CODE (type) == UNION_TYPE)
5157 {
5158 tree field;
400fbf9f 5159
910ad8de 5160 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5161 if (TREE_TYPE (field) != error_mark_node
5162 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5163 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5164 break;
5165
5166 if (field)
400fbf9f 5167 {
3e4093b6 5168 tree t;
e616f54d 5169 bool maybe_const = true;
3e4093b6 5170
c1771a20 5171 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5172 t = c_fully_fold (value, false, &maybe_const);
5173 t = build_constructor_single (type, field, t);
5174 if (!maybe_const)
5175 t = c_wrap_maybe_const (t, true);
5176 t = digest_init (loc, type, t,
bbbbb16a 5177 NULL_TREE, false, true, 0);
3e4093b6
RS
5178 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5179 return t;
400fbf9f 5180 }
c2255bc4 5181 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5182 return error_mark_node;
5183 }
5184 else
5185 {
5186 tree otype, ovalue;
400fbf9f 5187
3e4093b6 5188 if (type == void_type_node)
c2255bc4
AH
5189 {
5190 tree t = build1 (CONVERT_EXPR, type, value);
5191 SET_EXPR_LOCATION (t, loc);
5192 return t;
5193 }
400fbf9f 5194
3e4093b6 5195 otype = TREE_TYPE (value);
400fbf9f 5196
3e4093b6 5197 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5198 if (warn_cast_qual
5199 && TREE_CODE (type) == POINTER_TYPE
5200 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5201 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5202
36c5e70a
BE
5203 /* Warn about conversions between pointers to disjoint
5204 address spaces. */
5205 if (TREE_CODE (type) == POINTER_TYPE
5206 && TREE_CODE (otype) == POINTER_TYPE
5207 && !null_pointer_constant_p (value))
5208 {
5209 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5210 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5211 addr_space_t as_common;
5212
5213 if (!addr_space_superset (as_to, as_from, &as_common))
5214 {
5215 if (ADDR_SPACE_GENERIC_P (as_from))
5216 warning_at (loc, 0, "cast to %s address space pointer "
5217 "from disjoint generic address space pointer",
5218 c_addr_space_name (as_to));
5219
5220 else if (ADDR_SPACE_GENERIC_P (as_to))
5221 warning_at (loc, 0, "cast to generic address space pointer "
5222 "from disjoint %s address space pointer",
5223 c_addr_space_name (as_from));
5224
5225 else
5226 warning_at (loc, 0, "cast to %s address space pointer "
5227 "from disjoint %s address space pointer",
5228 c_addr_space_name (as_to),
5229 c_addr_space_name (as_from));
5230 }
5231 }
5232
3e4093b6 5233 /* Warn about possible alignment problems. */
3176a0c2 5234 if (STRICT_ALIGNMENT
3e4093b6
RS
5235 && TREE_CODE (type) == POINTER_TYPE
5236 && TREE_CODE (otype) == POINTER_TYPE
5237 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5238 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5239 /* Don't warn about opaque types, where the actual alignment
5240 restriction is unknown. */
3e636daf 5241 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6
RS
5242 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5243 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5244 warning_at (loc, OPT_Wcast_align,
5245 "cast increases required alignment of target type");
e9a25f70 5246
3176a0c2 5247 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5248 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5249 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5250 /* Unlike conversion of integers to pointers, where the
5251 warning is disabled for converting constants because
5252 of cases such as SIG_*, warn about converting constant
5253 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5254 sign extension, and a warning is appropriate. */
c2255bc4
AH
5255 warning_at (loc, OPT_Wpointer_to_int_cast,
5256 "cast from pointer to integer of different size");
400fbf9f 5257
3176a0c2 5258 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5259 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5260 warning_at (loc, OPT_Wbad_function_cast,
5261 "cast from function call of type %qT "
5262 "to non-matching type %qT", otype, type);
400fbf9f 5263
3176a0c2 5264 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5265 && TREE_CODE (otype) == INTEGER_TYPE
5266 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5267 /* Don't warn about converting any constant. */
5268 && !TREE_CONSTANT (value))
c2255bc4
AH
5269 warning_at (loc,
5270 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5271 "of different size");
400fbf9f 5272
79bedddc
SR
5273 if (warn_strict_aliasing <= 2)
5274 strict_aliasing_warning (otype, type, expr);
400fbf9f 5275
3897f229
JM
5276 /* If pedantic, warn for conversions between function and object
5277 pointer types, except for converting a null pointer constant
5278 to function pointer type. */
5279 if (pedantic
5280 && TREE_CODE (type) == POINTER_TYPE
5281 && TREE_CODE (otype) == POINTER_TYPE
5282 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5283 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5284 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5285 "conversion of function pointer to object pointer type");
3897f229
JM
5286
5287 if (pedantic
5288 && TREE_CODE (type) == POINTER_TYPE
5289 && TREE_CODE (otype) == POINTER_TYPE
5290 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5291 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5292 && !null_pointer_constant_p (value))
c1771a20 5293 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5294 "conversion of object pointer to function pointer type");
3897f229 5295
3e4093b6 5296 ovalue = value;
3e4093b6 5297 value = convert (type, value);
400fbf9f 5298
3e4093b6 5299 /* Ignore any integer overflow caused by the cast. */
928c19bb 5300 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5301 {
8bcd6380 5302 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5303 {
8bcd6380
RS
5304 if (!TREE_OVERFLOW (value))
5305 {
5306 /* Avoid clobbering a shared constant. */
5307 value = copy_node (value);
5308 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5309 }
6414bad6 5310 }
8bcd6380 5311 else if (TREE_OVERFLOW (value))
d8e1f97b 5312 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5313 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5314 }
5315 }
400fbf9f 5316
53cd18ec 5317 /* Don't let a cast be an lvalue. */
9482b620 5318 if (lvalue_p (value))
db3927fb 5319 value = non_lvalue_loc (loc, value);
e9a25f70 5320
928c19bb
JM
5321 /* Don't allow the results of casting to floating-point or complex
5322 types be confused with actual constants, or casts involving
5323 integer and pointer types other than direct integer-to-integer
5324 and integer-to-pointer be confused with integer constant
5325 expressions and null pointer constants. */
5326 if (TREE_CODE (value) == REAL_CST
5327 || TREE_CODE (value) == COMPLEX_CST
5328 || (TREE_CODE (value) == INTEGER_CST
5329 && !((TREE_CODE (expr) == INTEGER_CST
5330 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5331 || TREE_CODE (expr) == REAL_CST
5332 || TREE_CODE (expr) == COMPLEX_CST)))
5333 value = build1 (NOP_EXPR, type, value);
5334
21ba0cea 5335 protected_set_expr_location (value, loc);
3e4093b6 5336 return value;
400fbf9f
JW
5337}
5338
c2255bc4
AH
5339/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5340 location of the open paren of the cast, or the position of the cast
5341 expr. */
3e4093b6 5342tree
c2255bc4 5343c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5344{
f8893e47 5345 tree type;
928c19bb
JM
5346 tree type_expr = NULL_TREE;
5347 bool type_expr_const = true;
5348 tree ret;
3e4093b6 5349 int saved_wsp = warn_strict_prototypes;
c5c76735 5350
3e4093b6
RS
5351 /* This avoids warnings about unprototyped casts on
5352 integers. E.g. "#define SIG_DFL (void(*)())0". */
5353 if (TREE_CODE (expr) == INTEGER_CST)
5354 warn_strict_prototypes = 0;
928c19bb 5355 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5356 warn_strict_prototypes = saved_wsp;
c5c76735 5357
1807ffc1
MS
5358 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5359 && reject_gcc_builtin (expr))
5360 return error_mark_node;
5361
c2255bc4 5362 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5363 if (type_expr)
5364 {
9f33203d
JM
5365 bool inner_expr_const = true;
5366 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5367 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5368 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5369 && inner_expr_const);
c2255bc4 5370 SET_EXPR_LOCATION (ret, loc);
928c19bb 5371 }
24b97832 5372
21ba0cea
MP
5373 if (!EXPR_HAS_LOCATION (ret))
5374 protected_set_expr_location (ret, loc);
24b97832 5375
9e5b2115
PB
5376 /* C++ does not permits types to be defined in a cast, but it
5377 allows references to incomplete types. */
5378 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5379 warning_at (loc, OPT_Wc___compat,
5380 "defining a type in a cast is invalid in C++");
5381
928c19bb 5382 return ret;
400fbf9f 5383}
3e4093b6
RS
5384\f
5385/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5386 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5387 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5388 MODIFYCODE is the code for a binary operator that we use
5389 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5390 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5391 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5392 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5393
c2255bc4
AH
5394 LOCATION is the location of the MODIFYCODE operator.
5395 RHS_LOC is the location of the RHS. */
2f6e4e97 5396
3e4093b6 5397tree
32e8bb8e 5398build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5399 enum tree_code modifycode,
c2255bc4 5400 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5401{
3e4093b6
RS
5402 tree result;
5403 tree newrhs;
241f845a 5404 tree rhseval = NULL_TREE;
8ce94e44 5405 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5406 tree lhstype = TREE_TYPE (lhs);
5407 tree olhstype = lhstype;
928c19bb 5408 bool npc;
267bac10 5409 bool is_atomic_op;
e9a25f70 5410
3e4093b6
RS
5411 /* Types that aren't fully specified cannot be used in assignments. */
5412 lhs = require_complete_type (lhs);
e9a25f70 5413
3e4093b6
RS
5414 /* Avoid duplicate error messages from operands that had errors. */
5415 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5416 return error_mark_node;
400fbf9f 5417
4c2ecab0
JM
5418 /* Ensure an error for assigning a non-lvalue array to an array in
5419 C90. */
5420 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5421 {
5422 error_at (location, "assignment to expression with array type");
5423 return error_mark_node;
5424 }
5425
46a88c12 5426 /* For ObjC properties, defer this check. */
7bd11157 5427 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5428 return error_mark_node;
5429
267bac10
JM
5430 is_atomic_op = really_atomic_lvalue (lhs);
5431
8ce94e44
JM
5432 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5433 {
5434 rhs_semantic_type = TREE_TYPE (rhs);
5435 rhs = TREE_OPERAND (rhs, 0);
5436 }
5437
3e4093b6 5438 newrhs = rhs;
400fbf9f 5439
928c19bb
JM
5440 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5441 {
5442 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5443 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5444 rhs_origtype);
928c19bb
JM
5445 if (inner == error_mark_node)
5446 return error_mark_node;
5447 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5448 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5449 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5450 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5451 protected_set_expr_location (result, location);
5452 return result;
5453 }
5454
3e4093b6
RS
5455 /* If a binary op has been requested, combine the old LHS value with the RHS
5456 producing the value we should actually store into the LHS. */
5457
5458 if (modifycode != NOP_EXPR)
400fbf9f 5459 {
928c19bb 5460 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5461 lhs = stabilize_reference (lhs);
bbbbb16a 5462
267bac10
JM
5463 /* Construct the RHS for any non-atomic compound assignemnt. */
5464 if (!is_atomic_op)
5465 {
241f845a
JJ
5466 /* If in LHS op= RHS the RHS has side-effects, ensure they
5467 are preevaluated before the rest of the assignment expression's
5468 side-effects, because RHS could contain e.g. function calls
5469 that modify LHS. */
5470 if (TREE_SIDE_EFFECTS (rhs))
5471 {
5472 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5473 rhseval = newrhs;
5474 }
267bac10 5475 newrhs = build_binary_op (location,
241f845a 5476 modifycode, lhs, newrhs, 1);
267bac10
JM
5477
5478 /* The original type of the right hand side is no longer
5479 meaningful. */
5480 rhs_origtype = NULL_TREE;
5481 }
400fbf9f 5482 }
400fbf9f 5483
668ea4b1
IS
5484 if (c_dialect_objc ())
5485 {
46a88c12
NP
5486 /* Check if we are modifying an Objective-C property reference;
5487 if so, we need to generate setter calls. */
5488 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5489 if (result)
241f845a 5490 goto return_result;
46a88c12
NP
5491
5492 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5493 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5494 return error_mark_node;
5495 }
5496
9bf24266 5497 /* Give an error for storing in something that is 'const'. */
bbbd6700 5498
f37acdf9 5499 if (TYPE_READONLY (lhstype)
3e636daf 5500 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5501 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5502 {
c02065fc 5503 readonly_error (location, lhs, lv_assign);
953ff289
DN
5504 return error_mark_node;
5505 }
f37acdf9
JM
5506 else if (TREE_READONLY (lhs))
5507 readonly_warning (lhs, lv_assign);
bbbd6700 5508
3e4093b6
RS
5509 /* If storing into a structure or union member,
5510 it has probably been given type `int'.
5511 Compute the type that would go with
5512 the actual amount of storage the member occupies. */
bbbd6700 5513
3e4093b6
RS
5514 if (TREE_CODE (lhs) == COMPONENT_REF
5515 && (TREE_CODE (lhstype) == INTEGER_TYPE
5516 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5517 || TREE_CODE (lhstype) == REAL_TYPE
5518 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5519 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5520
3e4093b6
RS
5521 /* If storing in a field that is in actuality a short or narrower than one,
5522 we must store in the field in its actual type. */
5523
5524 if (lhstype != TREE_TYPE (lhs))
5525 {
5526 lhs = copy_node (lhs);
5527 TREE_TYPE (lhs) = lhstype;
400fbf9f 5528 }
400fbf9f 5529
32e8bb8e
ILT
5530 /* Issue -Wc++-compat warnings about an assignment to an enum type
5531 when LHS does not have its original type. This happens for,
5532 e.g., an enum bitfield in a struct. */
5533 if (warn_cxx_compat
5534 && lhs_origtype != NULL_TREE
5535 && lhs_origtype != lhstype
5536 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5537 {
5538 tree checktype = (rhs_origtype != NULL_TREE
5539 ? rhs_origtype
5540 : TREE_TYPE (rhs));
5541 if (checktype != error_mark_node
267bac10
JM
5542 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5543 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5544 warning_at (location, OPT_Wc___compat,
5545 "enum conversion in assignment is invalid in C++");
5546 }
5547
267bac10
JM
5548 /* If the lhs is atomic, remove that qualifier. */
5549 if (is_atomic_op)
5550 {
5551 lhstype = build_qualified_type (lhstype,
5552 (TYPE_QUALS (lhstype)
5553 & ~TYPE_QUAL_ATOMIC));
5554 olhstype = build_qualified_type (olhstype,
5555 (TYPE_QUALS (lhstype)
5556 & ~TYPE_QUAL_ATOMIC));
5557 }
5558
8ce94e44
JM
5559 /* Convert new value to destination type. Fold it first, then
5560 restore any excess precision information, for the sake of
5561 conversion warnings. */
400fbf9f 5562
267bac10
JM
5563 if (!(is_atomic_op && modifycode != NOP_EXPR))
5564 {
5565 npc = null_pointer_constant_p (newrhs);
5566 newrhs = c_fully_fold (newrhs, false, NULL);
5567 if (rhs_semantic_type)
5568 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5569 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5570 rhs_origtype, ic_assign, npc,
5571 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5572 if (TREE_CODE (newrhs) == ERROR_MARK)
5573 return error_mark_node;
5574 }
400fbf9f 5575
6e955430
ZL
5576 /* Emit ObjC write barrier, if necessary. */
5577 if (c_dialect_objc () && flag_objc_gc)
5578 {
5579 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5580 if (result)
c9f9eb5d
AH
5581 {
5582 protected_set_expr_location (result, location);
241f845a 5583 goto return_result;
c9f9eb5d 5584 }
6e955430
ZL
5585 }
5586
ea4b7848 5587 /* Scan operands. */
400fbf9f 5588
267bac10
JM
5589 if (is_atomic_op)
5590 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5591 else
5592 {
5593 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5594 TREE_SIDE_EFFECTS (result) = 1;
5595 protected_set_expr_location (result, location);
5596 }
400fbf9f 5597
3e4093b6
RS
5598 /* If we got the LHS in a different type for storing in,
5599 convert the result back to the nominal type of LHS
5600 so that the value we return always has the same type
5601 as the LHS argument. */
e855c5ce 5602
3e4093b6 5603 if (olhstype == TREE_TYPE (result))
241f845a 5604 goto return_result;
c9f9eb5d 5605
68fca595
MP
5606 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5607 rhs_origtype, ic_assign, false, NULL_TREE,
5608 NULL_TREE, 0);
c9f9eb5d 5609 protected_set_expr_location (result, location);
241f845a
JJ
5610
5611return_result:
5612 if (rhseval)
5613 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5614 return result;
3e4093b6
RS
5615}
5616\f
478a1c5b
ILT
5617/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5618 This is used to implement -fplan9-extensions. */
5619
5620static bool
5621find_anonymous_field_with_type (tree struct_type, tree type)
5622{
5623 tree field;
5624 bool found;
5625
3e636daf 5626 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5627 found = false;
5628 for (field = TYPE_FIELDS (struct_type);
5629 field != NULL_TREE;
5630 field = TREE_CHAIN (field))
5631 {
267bac10
JM
5632 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5633 ? c_build_qualified_type (TREE_TYPE (field),
5634 TYPE_QUAL_ATOMIC)
5635 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5636 if (DECL_NAME (field) == NULL
267bac10 5637 && comptypes (type, fieldtype))
478a1c5b
ILT
5638 {
5639 if (found)
5640 return false;
5641 found = true;
5642 }
5643 else if (DECL_NAME (field) == NULL
3e636daf 5644 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
5645 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5646 {
5647 if (found)
5648 return false;
5649 found = true;
5650 }
5651 }
5652 return found;
5653}
5654
5655/* RHS is an expression whose type is pointer to struct. If there is
5656 an anonymous field in RHS with type TYPE, then return a pointer to
5657 that field in RHS. This is used with -fplan9-extensions. This
5658 returns NULL if no conversion could be found. */
5659
5660static tree
5661convert_to_anonymous_field (location_t location, tree type, tree rhs)
5662{
5663 tree rhs_struct_type, lhs_main_type;
5664 tree field, found_field;
5665 bool found_sub_field;
5666 tree ret;
5667
5668 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5669 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 5670 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
5671
5672 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5673 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5674 ? c_build_qualified_type (TREE_TYPE (type),
5675 TYPE_QUAL_ATOMIC)
5676 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5677
5678 found_field = NULL_TREE;
5679 found_sub_field = false;
5680 for (field = TYPE_FIELDS (rhs_struct_type);
5681 field != NULL_TREE;
5682 field = TREE_CHAIN (field))
5683 {
5684 if (DECL_NAME (field) != NULL_TREE
3e636daf 5685 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 5686 continue;
267bac10
JM
5687 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5688 ? c_build_qualified_type (TREE_TYPE (field),
5689 TYPE_QUAL_ATOMIC)
5690 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5691 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5692 {
5693 if (found_field != NULL_TREE)
5694 return NULL_TREE;
5695 found_field = field;
5696 }
5697 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5698 lhs_main_type))
5699 {
5700 if (found_field != NULL_TREE)
5701 return NULL_TREE;
5702 found_field = field;
5703 found_sub_field = true;
5704 }
5705 }
5706
5707 if (found_field == NULL_TREE)
5708 return NULL_TREE;
5709
5710 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5711 build_fold_indirect_ref (rhs), found_field,
5712 NULL_TREE);
5713 ret = build_fold_addr_expr_loc (location, ret);
5714
5715 if (found_sub_field)
5716 {
5717 ret = convert_to_anonymous_field (location, type, ret);
5718 gcc_assert (ret != NULL_TREE);
5719 }
5720
5721 return ret;
5722}
5723
63bc4e87
MP
5724/* Issue an error message for a bad initializer component.
5725 GMSGID identifies the message.
5726 The component name is taken from the spelling stack. */
5727
5728static void
ea58ef42 5729error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5730{
5731 char *ofwhat;
5732
5733 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5734 error_at (loc, gmsgid);
63bc4e87
MP
5735 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5736 if (*ofwhat)
d7ff7ae5 5737 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5738}
5739
5740/* Issue a pedantic warning for a bad initializer component. OPT is
5741 the option OPT_* (from options.h) controlling this warning or 0 if
5742 it is unconditionally given. GMSGID identifies the message. The
5743 component name is taken from the spelling stack. */
5744
5745static void
5746pedwarn_init (location_t location, int opt, const char *gmsgid)
5747{
5748 char *ofwhat;
d7ff7ae5 5749 bool warned;
63bc4e87
MP
5750
5751 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5752 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5753 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5754 if (*ofwhat && warned)
5755 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5756}
5757
5758/* Issue a warning for a bad initializer component.
5759
5760 OPT is the OPT_W* value corresponding to the warning option that
5761 controls this warning. GMSGID identifies the message. The
5762 component name is taken from the spelling stack. */
5763
5764static void
5765warning_init (location_t loc, int opt, const char *gmsgid)
5766{
5767 char *ofwhat;
d7ff7ae5 5768 bool warned;
63bc4e87
MP
5769
5770 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5771 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5772 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5773 if (*ofwhat && warned)
5774 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5775}
5776\f
5777/* If TYPE is an array type and EXPR is a parenthesized string
5778 constant, warn if pedantic that EXPR is being used to initialize an
5779 object of type TYPE. */
5780
5781void
d033409e 5782maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5783{
5784 if (pedantic
5785 && TREE_CODE (type) == ARRAY_TYPE
5786 && TREE_CODE (expr.value) == STRING_CST
5787 && expr.original_code != STRING_CST)
d033409e 5788 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5789 "array initialized from parenthesized string constant");
5790}
5791
bbbbb16a
ILT
5792/* Convert value RHS to type TYPE as preparation for an assignment to
5793 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5794 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5795 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5796 constant before any folding.
3e4093b6
RS
5797 The real work of conversion is done by `convert'.
5798 The purpose of this function is to generate error messages
5799 for assignments that are not allowed in C.
2ac2f164
JM
5800 ERRTYPE says whether it is argument passing, assignment,
5801 initialization or return.
2f6e4e97 5802
aabef2de
MP
5803 In the following example, '~' denotes where EXPR_LOC and '^' where
5804 LOCATION point to:
5805
5806 f (var); [ic_argpass]
5807 ^ ~~~
5808 x = var; [ic_assign]
5809 ^ ~~~;
5810 int x = var; [ic_init]
5811 ^^^
5812 return x; [ic_return]
5813 ^
5814
2ac2f164 5815 FUNCTION is a tree for the function being called.
3e4093b6 5816 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5817
3e4093b6 5818static tree
81e5eca8 5819convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5820 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5821 bool null_pointer_constant, tree fundecl,
5822 tree function, int parmnum)
3e4093b6
RS
5823{
5824 enum tree_code codel = TREE_CODE (type);
8ce94e44 5825 tree orig_rhs = rhs;
3e4093b6
RS
5826 tree rhstype;
5827 enum tree_code coder;
2ac2f164 5828 tree rname = NULL_TREE;
58393038 5829 bool objc_ok = false;
2ac2f164 5830
1c7485af
MP
5831 /* Use the expansion point location to handle cases such as user's
5832 function returning a wrong-type macro defined in a system header. */
5833 location = expansion_point_location_if_in_system_header (location);
5834
6b4ef5c1 5835 if (errtype == ic_argpass)
2ac2f164
JM
5836 {
5837 tree selector;
5838 /* Change pointer to function to the function itself for
5839 diagnostics. */
5840 if (TREE_CODE (function) == ADDR_EXPR
5841 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5842 function = TREE_OPERAND (function, 0);
5843
5844 /* Handle an ObjC selector specially for diagnostics. */
5845 selector = objc_message_selector ();
5846 rname = function;
5847 if (selector && parmnum > 2)
5848 {
5849 rname = selector;
5850 parmnum -= 2;
5851 }
5852 }
5853
5854 /* This macro is used to emit diagnostics to ensure that all format
5855 strings are complete sentences, visible to gettext and checked at
5856 compile time. */
768952be 5857#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5858 do { \
5859 switch (errtype) \
5860 { \
5861 case ic_argpass: \
5c1bc275 5862 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5863 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5864 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5865 "expected %qT but argument is of type %qT", \
5866 type, rhstype); \
5867 break; \
1e053dfe
MLI
5868 case ic_assign: \
5869 pedwarn (LOCATION, OPT, AS); \
5870 break; \
5871 case ic_init: \
6a8f4e12 5872 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5873 break; \
5874 case ic_return: \
d033409e 5875 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5876 break; \
5877 default: \
5878 gcc_unreachable (); \
5879 } \
2ac2f164 5880 } while (0)
cb3ca04e 5881
49706e39
MLI
5882 /* This macro is used to emit diagnostics to ensure that all format
5883 strings are complete sentences, visible to gettext and checked at
768952be 5884 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5885 extra parameter to enumerate qualifiers. */
768952be 5886#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5887 do { \
5888 switch (errtype) \
5889 { \
5890 case ic_argpass: \
5c1bc275 5891 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5892 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5893 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5894 "expected %qT but argument is of type %qT", \
5895 type, rhstype); \
5896 break; \
5897 case ic_assign: \
5c1bc275 5898 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5899 break; \
5900 case ic_init: \
5c1bc275 5901 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5902 break; \
5903 case ic_return: \
5c1bc275 5904 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5905 break; \
5906 default: \
5907 gcc_unreachable (); \
5908 } \
5909 } while (0)
5910
768952be
MU
5911 /* This macro is used to emit diagnostics to ensure that all format
5912 strings are complete sentences, visible to gettext and checked at
5913 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5914 warning_at instead of pedwarn. */
5915#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5916 do { \
5917 switch (errtype) \
5918 { \
5919 case ic_argpass: \
5920 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5921 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5922 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5923 "expected %qT but argument is of type %qT", \
5924 type, rhstype); \
5925 break; \
5926 case ic_assign: \
5927 warning_at (LOCATION, OPT, AS, QUALS); \
5928 break; \
5929 case ic_init: \
5930 warning_at (LOCATION, OPT, IN, QUALS); \
5931 break; \
5932 case ic_return: \
5933 warning_at (LOCATION, OPT, RE, QUALS); \
5934 break; \
5935 default: \
5936 gcc_unreachable (); \
5937 } \
5938 } while (0)
5939
8ce94e44
JM
5940 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5941 rhs = TREE_OPERAND (rhs, 0);
5942
3e4093b6
RS
5943 rhstype = TREE_TYPE (rhs);
5944 coder = TREE_CODE (rhstype);
5945
5946 if (coder == ERROR_MARK)
5947 return error_mark_node;
5948
58393038
ZL
5949 if (c_dialect_objc ())
5950 {
5951 int parmno;
5952
5953 switch (errtype)
5954 {
5955 case ic_return:
5956 parmno = 0;
5957 break;
5958
5959 case ic_assign:
5960 parmno = -1;
5961 break;
5962
5963 case ic_init:
5964 parmno = -2;
5965 break;
5966
5967 default:
5968 parmno = parmnum;
5969 break;
5970 }
5971
5972 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5973 }
5974
bbbbb16a
ILT
5975 if (warn_cxx_compat)
5976 {
5977 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5978 if (checktype != error_mark_node
5979 && TREE_CODE (type) == ENUMERAL_TYPE
5980 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5981 {
768952be
MU
5982 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5983 G_("enum conversion when passing argument "
5984 "%d of %qE is invalid in C++"),
5985 G_("enum conversion in assignment is "
5986 "invalid in C++"),
5987 G_("enum conversion in initialization is "
5988 "invalid in C++"),
5989 G_("enum conversion in return is "
5990 "invalid in C++"));
bbbbb16a
ILT
5991 }
5992 }
5993
3e4093b6 5994 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5995 return rhs;
3e4093b6
RS
5996
5997 if (coder == VOID_TYPE)
400fbf9f 5998 {
6dcc04b0
JM
5999 /* Except for passing an argument to an unprototyped function,
6000 this is a constraint violation. When passing an argument to
6001 an unprototyped function, it is compile-time undefined;
6002 making it a constraint in that case was rejected in
6003 DR#252. */
c2255bc4 6004 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6005 return error_mark_node;
400fbf9f 6006 }
808d6eaa
JM
6007 rhs = require_complete_type (rhs);
6008 if (rhs == error_mark_node)
6009 return error_mark_node;
1807ffc1
MS
6010
6011 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6012 return error_mark_node;
6013
cd192ccc
MS
6014 /* A non-reference type can convert to a reference. This handles
6015 va_start, va_copy and possibly port built-ins. */
6016 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6017 {
3e4093b6 6018 if (!lvalue_p (rhs))
400fbf9f 6019 {
c2255bc4 6020 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6021 return error_mark_node;
400fbf9f 6022 }
3e4093b6
RS
6023 if (!c_mark_addressable (rhs))
6024 return error_mark_node;
6025 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6026 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6027
81e5eca8 6028 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6029 build_pointer_type (TREE_TYPE (type)),
6030 rhs, origtype, errtype,
6031 null_pointer_constant, fundecl, function,
6032 parmnum);
cd192ccc
MS
6033 if (rhs == error_mark_node)
6034 return error_mark_node;
3e4093b6
RS
6035
6036 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6037 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6038 return rhs;
400fbf9f 6039 }
3e4093b6 6040 /* Some types can interconvert without explicit casts. */
3274deff 6041 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6042 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6043 return convert (type, rhs);
6044 /* Arithmetic types all interconvert, and enum is treated like int. */
6045 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6046 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6047 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6048 || codel == BOOLEAN_TYPE)
6049 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6050 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6051 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6052 || coder == BOOLEAN_TYPE))
928c19bb
JM
6053 {
6054 tree ret;
6055 bool save = in_late_binary_op;
e5341100
JJ
6056 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6057 || (coder == REAL_TYPE
6058 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6059 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 6060 in_late_binary_op = true;
81e5eca8
MP
6061 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6062 ? expr_loc : location, type, orig_rhs);
e5341100 6063 in_late_binary_op = save;
928c19bb
JM
6064 return ret;
6065 }
400fbf9f 6066
79077aea
JJ
6067 /* Aggregates in different TUs might need conversion. */
6068 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6069 && codel == coder
6070 && comptypes (type, rhstype))
81e5eca8
MP
6071 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6072 ? expr_loc : location, type, rhs);
79077aea 6073
ebf0bf7f 6074 /* Conversion to a transparent union or record from its member types.
3e4093b6 6075 This applies only to function arguments. */
ebf0bf7f
JJ
6076 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6077 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6078 && errtype == ic_argpass)
400fbf9f 6079 {
0257e383 6080 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6081
910ad8de 6082 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6083 {
0257e383 6084 tree memb_type = TREE_TYPE (memb);
400fbf9f 6085
3e4093b6 6086 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6087 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6088 break;
e58cd767 6089
3e4093b6
RS
6090 if (TREE_CODE (memb_type) != POINTER_TYPE)
6091 continue;
2f6e4e97 6092
3e4093b6
RS
6093 if (coder == POINTER_TYPE)
6094 {
6095 tree ttl = TREE_TYPE (memb_type);
6096 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6097
3e4093b6
RS
6098 /* Any non-function converts to a [const][volatile] void *
6099 and vice versa; otherwise, targets must be the same.
6100 Meanwhile, the lhs target must have all the qualifiers of
6101 the rhs. */
267bac10
JM
6102 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6103 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6104 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6105 {
267bac10
JM
6106 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6107 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6108 /* If this type won't generate any warnings, use it. */
267bac10 6109 if (lquals == rquals
3e4093b6
RS
6110 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6111 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6112 ? ((lquals | rquals) == rquals)
6113 : ((lquals | rquals) == lquals)))
3e4093b6 6114 break;
400fbf9f 6115
3e4093b6 6116 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6117 if (!marginal_memb)
6118 marginal_memb = memb;
3e4093b6
RS
6119 }
6120 }
82bde854 6121
3e4093b6 6122 /* Can convert integer zero to any pointer type. */
928c19bb 6123 if (null_pointer_constant)
3e4093b6
RS
6124 {
6125 rhs = null_pointer_node;
6126 break;
6127 }
6128 }
400fbf9f 6129
0257e383 6130 if (memb || marginal_memb)
3e4093b6 6131 {
0257e383 6132 if (!memb)
3e4093b6
RS
6133 {
6134 /* We have only a marginally acceptable member type;
6135 it needs a warning. */
0257e383 6136 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6137 tree ttr = TREE_TYPE (rhstype);
714a0864 6138
3e4093b6
RS
6139 /* Const and volatile mean something different for function
6140 types, so the usual warnings are not appropriate. */
6141 if (TREE_CODE (ttr) == FUNCTION_TYPE
6142 && TREE_CODE (ttl) == FUNCTION_TYPE)
6143 {
6144 /* Because const and volatile on functions are
6145 restrictions that say the function will not do
6146 certain things, it is okay to use a const or volatile
6147 function where an ordinary one is wanted, but not
6148 vice-versa. */
36c5e70a
BE
6149 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6150 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6151 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6152 OPT_Wdiscarded_qualifiers,
6153 G_("passing argument %d of %qE "
6154 "makes %q#v qualified function "
6155 "pointer from unqualified"),
6156 G_("assignment makes %q#v qualified "
6157 "function pointer from "
6158 "unqualified"),
6159 G_("initialization makes %q#v qualified "
6160 "function pointer from "
6161 "unqualified"),
6162 G_("return makes %q#v qualified function "
6163 "pointer from unqualified"),
6164 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6165 }
36c5e70a
BE
6166 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6167 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6168 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6169 OPT_Wdiscarded_qualifiers,
6170 G_("passing argument %d of %qE discards "
6171 "%qv qualifier from pointer target type"),
6172 G_("assignment discards %qv qualifier "
6173 "from pointer target type"),
6174 G_("initialization discards %qv qualifier "
6175 "from pointer target type"),
6176 G_("return discards %qv qualifier from "
6177 "pointer target type"),
6178 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6179
6180 memb = marginal_memb;
3e4093b6 6181 }
400fbf9f 6182
fcf73884 6183 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6184 pedwarn (location, OPT_Wpedantic,
fcf73884 6185 "ISO C prohibits argument conversion to union type");
0e7c47fa 6186
db3927fb 6187 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6188 return build_constructor_single (type, memb, rhs);
3e4093b6 6189 }
0e7c47fa
RK
6190 }
6191
3e4093b6
RS
6192 /* Conversions among pointers */
6193 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6194 && (coder == codel))
400fbf9f 6195 {
3e4093b6
RS
6196 tree ttl = TREE_TYPE (type);
6197 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6198 tree mvl = ttl;
6199 tree mvr = ttr;
3e4093b6 6200 bool is_opaque_pointer;
264fa2db 6201 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6202 addr_space_t asl;
6203 addr_space_t asr;
400fbf9f 6204
46df2823 6205 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6206 mvl = (TYPE_ATOMIC (mvl)
6207 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6208 TYPE_QUAL_ATOMIC)
6209 : TYPE_MAIN_VARIANT (mvl));
46df2823 6210 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6211 mvr = (TYPE_ATOMIC (mvr)
6212 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6213 TYPE_QUAL_ATOMIC)
6214 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6215 /* Opaque pointers are treated like void pointers. */
f83c7f63 6216 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6217
478a1c5b
ILT
6218 /* The Plan 9 compiler permits a pointer to a struct to be
6219 automatically converted into a pointer to an anonymous field
6220 within the struct. */
6221 if (flag_plan9_extensions
3e636daf
MP
6222 && RECORD_OR_UNION_TYPE_P (mvl)
6223 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6224 && mvl != mvr)
6225 {
6226 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6227 if (new_rhs != NULL_TREE)
6228 {
6229 rhs = new_rhs;
6230 rhstype = TREE_TYPE (rhs);
6231 coder = TREE_CODE (rhstype);
6232 ttr = TREE_TYPE (rhstype);
6233 mvr = TYPE_MAIN_VARIANT (ttr);
6234 }
6235 }
6236
b7e20b53 6237 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6238 for the purpose of reducing the number of false positives, we
6239 tolerate the special case of
b7e20b53 6240
c22cacf3 6241 int *p = NULL;
b7e20b53 6242
c22cacf3 6243 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6244 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6245 warning_at (errtype == ic_argpass ? expr_loc : location,
6246 OPT_Wc___compat,
6247 "request for implicit conversion "
c2255bc4 6248 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6249
36c5e70a
BE
6250 /* See if the pointers point to incompatible address spaces. */
6251 asl = TYPE_ADDR_SPACE (ttl);
6252 asr = TYPE_ADDR_SPACE (ttr);
6253 if (!null_pointer_constant_p (rhs)
6254 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6255 {
6256 switch (errtype)
6257 {
6258 case ic_argpass:
8ffcdea8 6259 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6260 "non-enclosed address space", parmnum, rname);
6261 break;
6262 case ic_assign:
6263 error_at (location, "assignment from pointer to "
6264 "non-enclosed address space");
6265 break;
6266 case ic_init:
6267 error_at (location, "initialization from pointer to "
6268 "non-enclosed address space");
6269 break;
6270 case ic_return:
6271 error_at (location, "return from pointer to "
6272 "non-enclosed address space");
6273 break;
6274 default:
6275 gcc_unreachable ();
6276 }
6277 return error_mark_node;
6278 }
6279
7876a414
KG
6280 /* Check if the right-hand side has a format attribute but the
6281 left-hand side doesn't. */
90137d8f 6282 if (warn_suggest_attribute_format
104f8784 6283 && check_missing_format_attribute (type, rhstype))
c22cacf3 6284 {
104f8784
KG
6285 switch (errtype)
6286 {
6287 case ic_argpass:
8ffcdea8 6288 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6289 "argument %d of %qE might be "
6290 "a candidate for a format attribute",
6291 parmnum, rname);
104f8784
KG
6292 break;
6293 case ic_assign:
90137d8f 6294 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6295 "assignment left-hand side might be "
6296 "a candidate for a format attribute");
104f8784
KG
6297 break;
6298 case ic_init:
90137d8f 6299 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6300 "initialization left-hand side might be "
6301 "a candidate for a format attribute");
104f8784
KG
6302 break;
6303 case ic_return:
90137d8f 6304 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6305 "return type might be "
6306 "a candidate for a format attribute");
104f8784
KG
6307 break;
6308 default:
6309 gcc_unreachable ();
6310 }
7876a414 6311 }
c22cacf3 6312
3e4093b6
RS
6313 /* Any non-function converts to a [const][volatile] void *
6314 and vice versa; otherwise, targets must be the same.
6315 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6316 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6317 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6318 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6319 || is_opaque_pointer
f8a93a2e
JJ
6320 || ((c_common_unsigned_type (mvl)
6321 == c_common_unsigned_type (mvr))
267bac10
JM
6322 && (c_common_signed_type (mvl)
6323 == c_common_signed_type (mvr))
6324 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6325 {
768952be
MU
6326 /* Warn about loss of qualifers from pointers to arrays with
6327 qualifiers on the element type. */
6328 if (TREE_CODE (ttr) == ARRAY_TYPE)
6329 {
6330 ttr = strip_array_types (ttr);
6331 ttl = strip_array_types (ttl);
6332
6333 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6334 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6335 WARNING_FOR_QUALIFIERS (location, expr_loc,
6336 OPT_Wdiscarded_array_qualifiers,
6337 G_("passing argument %d of %qE discards "
6338 "%qv qualifier from pointer target type"),
6339 G_("assignment discards %qv qualifier "
6340 "from pointer target type"),
6341 G_("initialization discards %qv qualifier "
6342 "from pointer target type"),
6343 G_("return discards %qv qualifier from "
6344 "pointer target type"),
6345 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6346 }
6347 else if (pedantic
3e4093b6
RS
6348 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6349 ||
6350 (VOID_TYPE_P (ttr)
928c19bb 6351 && !null_pointer_constant
3e4093b6 6352 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6353 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6354 G_("ISO C forbids passing argument %d of "
6355 "%qE between function pointer "
6356 "and %<void *%>"),
6357 G_("ISO C forbids assignment between "
6358 "function pointer and %<void *%>"),
6359 G_("ISO C forbids initialization between "
6360 "function pointer and %<void *%>"),
6361 G_("ISO C forbids return between function "
6362 "pointer and %<void *%>"));
3e4093b6
RS
6363 /* Const and volatile mean something different for function types,
6364 so the usual warnings are not appropriate. */
6365 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6366 && TREE_CODE (ttl) != FUNCTION_TYPE)
6367 {
768952be
MU
6368 /* Don't warn about loss of qualifier for conversions from
6369 qualified void* to pointers to arrays with corresponding
6370 qualifier on the element type. */
6371 if (!pedantic)
6372 ttl = strip_array_types (ttl);
6373
267bac10
JM
6374 /* Assignments between atomic and non-atomic objects are OK. */
6375 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6376 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6377 {
768952be
MU
6378 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6379 OPT_Wdiscarded_qualifiers,
6380 G_("passing argument %d of %qE discards "
6381 "%qv qualifier from pointer target type"),
6382 G_("assignment discards %qv qualifier "
6383 "from pointer target type"),
6384 G_("initialization discards %qv qualifier "
6385 "from pointer target type"),
6386 G_("return discards %qv qualifier from "
6387 "pointer target type"),
6388 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6389 }
3e4093b6
RS
6390 /* If this is not a case of ignoring a mismatch in signedness,
6391 no warning. */
6392 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6393 || target_cmp)
3e4093b6
RS
6394 ;
6395 /* If there is a mismatch, do warn. */
f2fd3821 6396 else if (warn_pointer_sign)
768952be
MU
6397 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6398 G_("pointer targets in passing argument "
6399 "%d of %qE differ in signedness"),
6400 G_("pointer targets in assignment "
6401 "differ in signedness"),
6402 G_("pointer targets in initialization "
6403 "differ in signedness"),
6404 G_("pointer targets in return differ "
6405 "in signedness"));
3e4093b6
RS
6406 }
6407 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6408 && TREE_CODE (ttr) == FUNCTION_TYPE)
6409 {
6410 /* Because const and volatile on functions are restrictions
6411 that say the function will not do certain things,
6412 it is okay to use a const or volatile function
6413 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6414 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6415 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6416 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6417 OPT_Wdiscarded_qualifiers,
6418 G_("passing argument %d of %qE makes "
6419 "%q#v qualified function pointer "
6420 "from unqualified"),
6421 G_("assignment makes %q#v qualified function "
6422 "pointer from unqualified"),
6423 G_("initialization makes %q#v qualified "
6424 "function pointer from unqualified"),
6425 G_("return makes %q#v qualified function "
6426 "pointer from unqualified"),
6427 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6428 }
6429 }
6430 else
58393038
ZL
6431 /* Avoid warning about the volatile ObjC EH puts on decls. */
6432 if (!objc_ok)
768952be
MU
6433 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6434 OPT_Wincompatible_pointer_types,
6435 G_("passing argument %d of %qE from "
6436 "incompatible pointer type"),
6437 G_("assignment from incompatible pointer type"),
6438 G_("initialization from incompatible "
6439 "pointer type"),
6440 G_("return from incompatible pointer type"));
58393038 6441
3e4093b6
RS
6442 return convert (type, rhs);
6443 }
b494fd98
EB
6444 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6445 {
6dcc04b0
JM
6446 /* ??? This should not be an error when inlining calls to
6447 unprototyped functions. */
c2255bc4 6448 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6449 return error_mark_node;
6450 }
3e4093b6 6451 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6452 {
3e4093b6
RS
6453 /* An explicit constant 0 can convert to a pointer,
6454 or one that results from arithmetic, even including
6455 a cast to integer type. */
928c19bb 6456 if (!null_pointer_constant)
768952be
MU
6457 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6458 OPT_Wint_conversion,
6459 G_("passing argument %d of %qE makes "
6460 "pointer from integer without a cast"),
6461 G_("assignment makes pointer from integer "
6462 "without a cast"),
6463 G_("initialization makes pointer from "
6464 "integer without a cast"),
6465 G_("return makes pointer from integer "
6466 "without a cast"));
b3006337
EB
6467
6468 return convert (type, rhs);
400fbf9f 6469 }
3e4093b6 6470 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6471 {
768952be
MU
6472 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6473 OPT_Wint_conversion,
6474 G_("passing argument %d of %qE makes integer "
6475 "from pointer without a cast"),
6476 G_("assignment makes integer from pointer "
6477 "without a cast"),
6478 G_("initialization makes integer from pointer "
6479 "without a cast"),
6480 G_("return makes integer from pointer "
6481 "without a cast"));
3e4093b6
RS
6482 return convert (type, rhs);
6483 }
6484 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6485 {
6486 tree ret;
6487 bool save = in_late_binary_op;
6488 in_late_binary_op = true;
6489 ret = convert (type, rhs);
6490 in_late_binary_op = save;
6491 return ret;
6492 }
400fbf9f 6493
2ac2f164 6494 switch (errtype)
3e4093b6 6495 {
2ac2f164 6496 case ic_argpass:
8ffcdea8
MP
6497 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6498 rname);
a7da8b42 6499 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6500 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6501 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6502 break;
6503 case ic_assign:
c2255bc4
AH
6504 error_at (location, "incompatible types when assigning to type %qT from "
6505 "type %qT", type, rhstype);
2ac2f164
JM
6506 break;
6507 case ic_init:
c2255bc4 6508 error_at (location,
8ffcdea8 6509 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6510 type, rhstype);
2ac2f164
JM
6511 break;
6512 case ic_return:
c2255bc4 6513 error_at (location,
8ffcdea8 6514 "incompatible types when returning type %qT but %qT was "
c2255bc4 6515 "expected", rhstype, type);
2ac2f164
JM
6516 break;
6517 default:
6518 gcc_unreachable ();
400fbf9f 6519 }
53b01f59 6520
3e4093b6
RS
6521 return error_mark_node;
6522}
3e4093b6
RS
6523\f
6524/* If VALUE is a compound expr all of whose expressions are constant, then
6525 return its value. Otherwise, return error_mark_node.
15b732b2 6526
3e4093b6
RS
6527 This is for handling COMPOUND_EXPRs as initializer elements
6528 which is allowed with a warning when -pedantic is specified. */
15b732b2 6529
3e4093b6
RS
6530static tree
6531valid_compound_expr_initializer (tree value, tree endtype)
6532{
6533 if (TREE_CODE (value) == COMPOUND_EXPR)
6534 {
6535 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6536 == error_mark_node)
6537 return error_mark_node;
6538 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6539 endtype);
6540 }
116df786 6541 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6542 return error_mark_node;
6543 else
6544 return value;
15b732b2 6545}
400fbf9f 6546\f
3e4093b6
RS
6547/* Perform appropriate conversions on the initial value of a variable,
6548 store it in the declaration DECL,
6549 and print any error messages that are appropriate.
bbbbb16a 6550 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6551 If the init is invalid, store an ERROR_MARK.
6552
6553 INIT_LOC is the location of the initial value. */
400fbf9f 6554
3e4093b6 6555void
c2255bc4 6556store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6557{
3e4093b6 6558 tree value, type;
928c19bb 6559 bool npc = false;
400fbf9f 6560
3e4093b6 6561 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6562
3e4093b6
RS
6563 type = TREE_TYPE (decl);
6564 if (TREE_CODE (type) == ERROR_MARK)
6565 return;
400fbf9f 6566
3e4093b6 6567 /* Digest the specified initializer into an expression. */
400fbf9f 6568
928c19bb
JM
6569 if (init)
6570 npc = null_pointer_constant_p (init);
c2255bc4
AH
6571 value = digest_init (init_loc, type, init, origtype, npc,
6572 true, TREE_STATIC (decl));
400fbf9f 6573
3e4093b6 6574 /* Store the expression if valid; else report error. */
400fbf9f 6575
8400e75e 6576 if (!in_system_header_at (input_location)
3f75a254 6577 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6578 warning (OPT_Wtraditional, "traditional C rejects automatic "
6579 "aggregate initialization");
2f6e4e97 6580
dea63e49
JJ
6581 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6582 DECL_INITIAL (decl) = value;
400fbf9f 6583
3e4093b6
RS
6584 /* ANSI wants warnings about out-of-range constant initializers. */
6585 STRIP_TYPE_NOPS (value);
b8698a0f 6586 if (TREE_STATIC (decl))
c2658540 6587 constant_expression_warning (value);
400fbf9f 6588
3e4093b6
RS
6589 /* Check if we need to set array size from compound literal size. */
6590 if (TREE_CODE (type) == ARRAY_TYPE
6591 && TYPE_DOMAIN (type) == 0
6592 && value != error_mark_node)
400fbf9f 6593 {
3e4093b6
RS
6594 tree inside_init = init;
6595
ed248cf7 6596 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6597 inside_init = fold (inside_init);
6598
6599 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6600 {
8d9f82d5 6601 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6602
8d9f82d5 6603 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6604 {
6605 /* For int foo[] = (int [3]){1}; we need to set array size
6606 now since later on array initializer will be just the
6607 brace enclosed list of the compound literal. */
e30ecc5d 6608 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6609 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6610 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6611 layout_type (type);
8d9f82d5 6612 layout_decl (cldecl, 0);
e30ecc5d
JJ
6613 TREE_TYPE (decl)
6614 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6615 }
6616 }
400fbf9f 6617 }
3e4093b6
RS
6618}
6619\f
6620/* Methods for storing and printing names for error messages. */
400fbf9f 6621
3e4093b6
RS
6622/* Implement a spelling stack that allows components of a name to be pushed
6623 and popped. Each element on the stack is this structure. */
400fbf9f 6624
3e4093b6
RS
6625struct spelling
6626{
6627 int kind;
6628 union
400fbf9f 6629 {
a0f0ab9f 6630 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6631 const char *s;
6632 } u;
6633};
2f6e4e97 6634
3e4093b6
RS
6635#define SPELLING_STRING 1
6636#define SPELLING_MEMBER 2
6637#define SPELLING_BOUNDS 3
400fbf9f 6638
3e4093b6
RS
6639static struct spelling *spelling; /* Next stack element (unused). */
6640static struct spelling *spelling_base; /* Spelling stack base. */
6641static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6642
3e4093b6
RS
6643/* Macros to save and restore the spelling stack around push_... functions.
6644 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6645
3e4093b6
RS
6646#define SPELLING_DEPTH() (spelling - spelling_base)
6647#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6648
3e4093b6
RS
6649/* Push an element on the spelling stack with type KIND and assign VALUE
6650 to MEMBER. */
400fbf9f 6651
3e4093b6
RS
6652#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6653{ \
6654 int depth = SPELLING_DEPTH (); \
6655 \
6656 if (depth >= spelling_size) \
6657 { \
6658 spelling_size += 10; \
cca8ead2
BI
6659 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6660 spelling_size); \
3e4093b6
RS
6661 RESTORE_SPELLING_DEPTH (depth); \
6662 } \
6663 \
6664 spelling->kind = (KIND); \
6665 spelling->MEMBER = (VALUE); \
6666 spelling++; \
6667}
400fbf9f 6668
3e4093b6 6669/* Push STRING on the stack. Printed literally. */
400fbf9f 6670
3e4093b6
RS
6671static void
6672push_string (const char *string)
6673{
6674 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6675}
400fbf9f 6676
3e4093b6 6677/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6678
3e4093b6
RS
6679static void
6680push_member_name (tree decl)
6681{
6682 const char *const string
88388a52
JM
6683 = (DECL_NAME (decl)
6684 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6685 : _("<anonymous>"));
3e4093b6
RS
6686 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6687}
400fbf9f 6688
3e4093b6 6689/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6690
3e4093b6 6691static void
a0f0ab9f 6692push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6693{
6694 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6695}
bb58bec5 6696
3e4093b6 6697/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6698
3e4093b6
RS
6699static int
6700spelling_length (void)
6701{
6702 int size = 0;
6703 struct spelling *p;
400fbf9f 6704
3e4093b6
RS
6705 for (p = spelling_base; p < spelling; p++)
6706 {
6707 if (p->kind == SPELLING_BOUNDS)
6708 size += 25;
6709 else
6710 size += strlen (p->u.s) + 1;
6711 }
6712
6713 return size;
400fbf9f 6714}
400fbf9f 6715
3e4093b6 6716/* Print the spelling to BUFFER and return it. */
400fbf9f 6717
3e4093b6
RS
6718static char *
6719print_spelling (char *buffer)
400fbf9f 6720{
3e4093b6
RS
6721 char *d = buffer;
6722 struct spelling *p;
400fbf9f 6723
3e4093b6
RS
6724 for (p = spelling_base; p < spelling; p++)
6725 if (p->kind == SPELLING_BOUNDS)
6726 {
a0f0ab9f 6727 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6728 d += strlen (d);
6729 }
6730 else
6731 {
6732 const char *s;
6733 if (p->kind == SPELLING_MEMBER)
6734 *d++ = '.';
6735 for (s = p->u.s; (*d = *s++); d++)
6736 ;
6737 }
6738 *d++ = '\0';
6739 return buffer;
6740}
400fbf9f 6741
3e4093b6
RS
6742/* Digest the parser output INIT as an initializer for type TYPE.
6743 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6744
bbbbb16a
ILT
6745 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6746
928c19bb
JM
6747 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6748
916c5919
JM
6749 If INIT is a string constant, STRICT_STRING is true if it is
6750 unparenthesized or we should not warn here for it being parenthesized.
6751 For other types of INIT, STRICT_STRING is not used.
6752
c2255bc4
AH
6753 INIT_LOC is the location of the INIT.
6754
3e4093b6
RS
6755 REQUIRE_CONSTANT requests an error if non-constant initializers or
6756 elements are seen. */
7e842ef8 6757
3e4093b6 6758static tree
c2255bc4
AH
6759digest_init (location_t init_loc, tree type, tree init, tree origtype,
6760 bool null_pointer_constant, bool strict_string,
6761 int require_constant)
3e4093b6
RS
6762{
6763 enum tree_code code = TREE_CODE (type);
6764 tree inside_init = init;
8ce94e44 6765 tree semantic_type = NULL_TREE;
928c19bb 6766 bool maybe_const = true;
7e842ef8 6767
3e4093b6 6768 if (type == error_mark_node
f01da1a5 6769 || !init
7a0ca710 6770 || error_operand_p (init))
3e4093b6 6771 return error_mark_node;
7e842ef8 6772
ed248cf7 6773 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6774
8ce94e44
JM
6775 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6776 {
6777 semantic_type = TREE_TYPE (inside_init);
6778 inside_init = TREE_OPERAND (inside_init, 0);
6779 }
928c19bb
JM
6780 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6781 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6782
3e4093b6
RS
6783 /* Initialization of an array of chars from a string constant
6784 optionally enclosed in braces. */
7e842ef8 6785
197463ae
JM
6786 if (code == ARRAY_TYPE && inside_init
6787 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6788 {
267bac10
JM
6789 tree typ1
6790 = (TYPE_ATOMIC (TREE_TYPE (type))
6791 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6792 TYPE_QUAL_ATOMIC)
6793 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6794 /* Note that an array could be both an array of character type
6795 and an array of wchar_t if wchar_t is signed char or unsigned
6796 char. */
6797 bool char_array = (typ1 == char_type_node
6798 || typ1 == signed_char_type_node
6799 || typ1 == unsigned_char_type_node);
6800 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6801 bool char16_array = !!comptypes (typ1, char16_type_node);
6802 bool char32_array = !!comptypes (typ1, char32_type_node);
6803
6804 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6805 {
916c5919 6806 struct c_expr expr;
c466b2cd 6807 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6808 expr.value = inside_init;
6809 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6810 expr.original_type = NULL;
d033409e 6811 maybe_warn_string_init (init_loc, type, expr);
916c5919 6812
a45e580b 6813 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6814 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6815 "initialization of a flexible array member");
6816
3e4093b6 6817 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6818 TYPE_MAIN_VARIANT (type)))
3e4093b6 6819 return inside_init;
7e842ef8 6820
c466b2cd 6821 if (char_array)
3e4093b6 6822 {
c466b2cd
KVH
6823 if (typ2 != char_type_node)
6824 {
ea58ef42
MP
6825 error_init (init_loc, "char-array initialized from wide "
6826 "string");
c466b2cd
KVH
6827 return error_mark_node;
6828 }
3e4093b6 6829 }
c466b2cd 6830 else
3e4093b6 6831 {
c466b2cd
KVH
6832 if (typ2 == char_type_node)
6833 {
ea58ef42
MP
6834 error_init (init_loc, "wide character array initialized "
6835 "from non-wide string");
c466b2cd
KVH
6836 return error_mark_node;
6837 }
6838 else if (!comptypes(typ1, typ2))
6839 {
ea58ef42
MP
6840 error_init (init_loc, "wide character array initialized "
6841 "from incompatible wide string");
c466b2cd
KVH
6842 return error_mark_node;
6843 }
7e842ef8 6844 }
2f6e4e97 6845
3e4093b6
RS
6846 TREE_TYPE (inside_init) = type;
6847 if (TYPE_DOMAIN (type) != 0
6848 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6849 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6850 {
6851 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6852
c466b2cd 6853 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6854 because it's ok to ignore the terminating null char
6855 that is counted in the length of the constant. */
5eb4df45
ILT
6856 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6857 (len
6858 - (TYPE_PRECISION (typ1)
6859 / BITS_PER_UNIT))))
6860 pedwarn_init (init_loc, 0,
6861 ("initializer-string for array of chars "
6862 "is too long"));
6863 else if (warn_cxx_compat
6864 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6865 warning_at (init_loc, OPT_Wc___compat,
6866 ("initializer-string for array chars "
6867 "is too long for C++"));
6868 }
7e842ef8 6869
3e4093b6 6870 return inside_init;
7e842ef8 6871 }
197463ae
JM
6872 else if (INTEGRAL_TYPE_P (typ1))
6873 {
ea58ef42 6874 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6875 "from string constant");
6876 return error_mark_node;
6877 }
7e842ef8
PE
6878 }
6879
3e4093b6
RS
6880 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6881 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6882 below and handle as a constructor. */
e89be13b 6883 if (code == VECTOR_TYPE
31521951 6884 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 6885 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6886 && TREE_CONSTANT (inside_init))
6887 {
6888 if (TREE_CODE (inside_init) == VECTOR_CST
6889 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6890 TYPE_MAIN_VARIANT (type)))
6891 return inside_init;
6892
6893 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6894 {
4038c495
GB
6895 unsigned HOST_WIDE_INT ix;
6896 tree value;
6897 bool constant_p = true;
e89be13b
JJ
6898
6899 /* Iterate through elements and check if all constructor
6900 elements are *_CSTs. */
4038c495
GB
6901 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6902 if (!CONSTANT_CLASS_P (value))
6903 {
6904 constant_p = false;
6905 break;
6906 }
e89be13b 6907
4038c495
GB
6908 if (constant_p)
6909 return build_vector_from_ctor (type,
6910 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6911 }
6912 }
6035d635 6913
ca085fd7
MLI
6914 if (warn_sequence_point)
6915 verify_sequence_points (inside_init);
6916
3e4093b6
RS
6917 /* Any type can be initialized
6918 from an expression of the same type, optionally with braces. */
400fbf9f 6919
3e4093b6
RS
6920 if (inside_init && TREE_TYPE (inside_init) != 0
6921 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6922 TYPE_MAIN_VARIANT (type))
3e4093b6 6923 || (code == ARRAY_TYPE
132da1a5 6924 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6925 || (code == VECTOR_TYPE
132da1a5 6926 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6927 || (code == POINTER_TYPE
3897f229 6928 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6929 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6930 TREE_TYPE (type)))))
3e4093b6
RS
6931 {
6932 if (code == POINTER_TYPE)
b494fd98 6933 {
b494fd98
EB
6934 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6935 {
f2a71bbc
JM
6936 if (TREE_CODE (inside_init) == STRING_CST
6937 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6938 inside_init = array_to_pointer_conversion
6939 (init_loc, inside_init);
f2a71bbc
JM
6940 else
6941 {
ea58ef42 6942 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6943 return error_mark_node;
6944 }
b494fd98 6945 }
f2a71bbc 6946 }
b494fd98 6947
bae39a73
NS
6948 if (code == VECTOR_TYPE)
6949 /* Although the types are compatible, we may require a
6950 conversion. */
6951 inside_init = convert (type, inside_init);
3e4093b6 6952
ca58211b 6953 if (require_constant
3e4093b6 6954 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6955 {
3e4093b6
RS
6956 /* As an extension, allow initializing objects with static storage
6957 duration with compound literals (which are then treated just as
ca58211b
PB
6958 the brace enclosed list they contain). Also allow this for
6959 vectors, as we can only assign them with compound literals. */
7278465e
MP
6960 if (flag_isoc99 && code != VECTOR_TYPE)
6961 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6962 "is not constant");
3e4093b6
RS
6963 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6964 inside_init = DECL_INITIAL (decl);
400fbf9f 6965 }
3e4093b6
RS
6966
6967 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6968 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6969 {
ea58ef42
MP
6970 error_init (init_loc, "array initialized from non-constant array "
6971 "expression");
3e4093b6 6972 return error_mark_node;
400fbf9f 6973 }
400fbf9f 6974
c1771a20 6975 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6976 -pedantic-errors is specified. In the later case, we always want
6977 an error. In the former case, we simply want a warning. */
6978 if (require_constant && pedantic
6979 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6980 {
6981 inside_init
6982 = valid_compound_expr_initializer (inside_init,
6983 TREE_TYPE (inside_init));
6984 if (inside_init == error_mark_node)
ea58ef42 6985 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6986 else
c1771a20 6987 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6988 "initializer element is not constant");
3e4093b6
RS
6989 if (flag_pedantic_errors)
6990 inside_init = error_mark_node;
6991 }
6992 else if (require_constant
116df786
RH
6993 && !initializer_constant_valid_p (inside_init,
6994 TREE_TYPE (inside_init)))
3e4093b6 6995 {
ea58ef42 6996 error_init (init_loc, "initializer element is not constant");
3e4093b6 6997 inside_init = error_mark_node;
8b40563c 6998 }
928c19bb 6999 else if (require_constant && !maybe_const)
3aa3c9fc 7000 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7001 "initializer element is not a constant expression");
f735a153 7002
90137d8f 7003 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7004 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7005 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7006 type, inside_init, origtype,
bbbbb16a 7007 ic_init, null_pointer_constant,
928c19bb 7008 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7009 return inside_init;
7010 }
f735a153 7011
3e4093b6 7012 /* Handle scalar types, including conversions. */
400fbf9f 7013
ab22c1fa
CF
7014 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7015 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7016 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7017 {
f2a71bbc
JM
7018 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7019 && (TREE_CODE (init) == STRING_CST
7020 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7021 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7022 if (semantic_type)
7023 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7024 inside_init);
3e4093b6 7025 inside_init
68fca595
MP
7026 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7027 inside_init, origtype, ic_init,
7028 null_pointer_constant, NULL_TREE, NULL_TREE,
7029 0);
2f6e4e97 7030
3274deff
JW
7031 /* Check to see if we have already given an error message. */
7032 if (inside_init == error_mark_node)
7033 ;
3f75a254 7034 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7035 {
ea58ef42 7036 error_init (init_loc, "initializer element is not constant");
3e4093b6 7037 inside_init = error_mark_node;
400fbf9f 7038 }
3e4093b6 7039 else if (require_constant
116df786
RH
7040 && !initializer_constant_valid_p (inside_init,
7041 TREE_TYPE (inside_init)))
400fbf9f 7042 {
ea58ef42
MP
7043 error_init (init_loc, "initializer element is not computable at "
7044 "load time");
3e4093b6 7045 inside_init = error_mark_node;
400fbf9f 7046 }
928c19bb 7047 else if (require_constant && !maybe_const)
451b5e48 7048 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7049 "initializer element is not a constant expression");
3e4093b6
RS
7050
7051 return inside_init;
400fbf9f 7052 }
d9fc6069 7053
3e4093b6 7054 /* Come here only for records and arrays. */
d9fc6069 7055
3e4093b6 7056 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7057 {
ea58ef42 7058 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7059 return error_mark_node;
d9fc6069 7060 }
3e4093b6 7061
ea58ef42 7062 error_init (init_loc, "invalid initializer");
3e4093b6 7063 return error_mark_node;
d9fc6069 7064}
400fbf9f 7065\f
3e4093b6 7066/* Handle initializers that use braces. */
400fbf9f 7067
3e4093b6
RS
7068/* Type of object we are accumulating a constructor for.
7069 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7070static tree constructor_type;
400fbf9f 7071
3e4093b6
RS
7072/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7073 left to fill. */
7074static tree constructor_fields;
400fbf9f 7075
3e4093b6
RS
7076/* For an ARRAY_TYPE, this is the specified index
7077 at which to store the next element we get. */
7078static tree constructor_index;
400fbf9f 7079
3e4093b6
RS
7080/* For an ARRAY_TYPE, this is the maximum index. */
7081static tree constructor_max_index;
400fbf9f 7082
3e4093b6
RS
7083/* For a RECORD_TYPE, this is the first field not yet written out. */
7084static tree constructor_unfilled_fields;
400fbf9f 7085
3e4093b6
RS
7086/* For an ARRAY_TYPE, this is the index of the first element
7087 not yet written out. */
7088static tree constructor_unfilled_index;
895ea614 7089
3e4093b6
RS
7090/* In a RECORD_TYPE, the byte index of the next consecutive field.
7091 This is so we can generate gaps between fields, when appropriate. */
7092static tree constructor_bit_index;
10d5caec 7093
3e4093b6
RS
7094/* If we are saving up the elements rather than allocating them,
7095 this is the list of elements so far (in reverse order,
7096 most recent first). */
9771b263 7097static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7098
3e4093b6
RS
7099/* 1 if constructor should be incrementally stored into a constructor chain,
7100 0 if all the elements should be kept in AVL tree. */
7101static int constructor_incremental;
ad47f1e5 7102
3e4093b6
RS
7103/* 1 if so far this constructor's elements are all compile-time constants. */
7104static int constructor_constant;
ad47f1e5 7105
3e4093b6
RS
7106/* 1 if so far this constructor's elements are all valid address constants. */
7107static int constructor_simple;
ad47f1e5 7108
928c19bb
JM
7109/* 1 if this constructor has an element that cannot be part of a
7110 constant expression. */
7111static int constructor_nonconst;
7112
3e4093b6
RS
7113/* 1 if this constructor is erroneous so far. */
7114static int constructor_erroneous;
d45cf215 7115
9bac5cbb
G
7116/* 1 if this constructor is the universal zero initializer { 0 }. */
7117static int constructor_zeroinit;
7118
3e4093b6
RS
7119/* Structure for managing pending initializer elements, organized as an
7120 AVL tree. */
d45cf215 7121
3e4093b6 7122struct init_node
d45cf215 7123{
3e4093b6
RS
7124 struct init_node *left, *right;
7125 struct init_node *parent;
7126 int balance;
7127 tree purpose;
7128 tree value;
bbbbb16a 7129 tree origtype;
d45cf215
RS
7130};
7131
3e4093b6
RS
7132/* Tree of pending elements at this constructor level.
7133 These are elements encountered out of order
7134 which belong at places we haven't reached yet in actually
7135 writing the output.
7136 Will never hold tree nodes across GC runs. */
7137static struct init_node *constructor_pending_elts;
d45cf215 7138
3e4093b6
RS
7139/* The SPELLING_DEPTH of this constructor. */
7140static int constructor_depth;
d45cf215 7141
3e4093b6
RS
7142/* DECL node for which an initializer is being read.
7143 0 means we are reading a constructor expression
7144 such as (struct foo) {...}. */
7145static tree constructor_decl;
d45cf215 7146
3e4093b6
RS
7147/* Nonzero if this is an initializer for a top-level decl. */
7148static int constructor_top_level;
d45cf215 7149
3e4093b6
RS
7150/* Nonzero if there were any member designators in this initializer. */
7151static int constructor_designated;
d45cf215 7152
3e4093b6
RS
7153/* Nesting depth of designator list. */
7154static int designator_depth;
d45cf215 7155
3e4093b6 7156/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7157static int designator_erroneous;
d45cf215 7158
3e4093b6
RS
7159\f
7160/* This stack has a level for each implicit or explicit level of
7161 structuring in the initializer, including the outermost one. It
7162 saves the values of most of the variables above. */
d45cf215 7163
3e4093b6
RS
7164struct constructor_range_stack;
7165
7166struct constructor_stack
d45cf215 7167{
3e4093b6
RS
7168 struct constructor_stack *next;
7169 tree type;
7170 tree fields;
7171 tree index;
7172 tree max_index;
7173 tree unfilled_index;
7174 tree unfilled_fields;
7175 tree bit_index;
9771b263 7176 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7177 struct init_node *pending_elts;
7178 int offset;
7179 int depth;
916c5919 7180 /* If value nonzero, this value should replace the entire
3e4093b6 7181 constructor at this level. */
916c5919 7182 struct c_expr replacement_value;
3e4093b6
RS
7183 struct constructor_range_stack *range_stack;
7184 char constant;
7185 char simple;
928c19bb 7186 char nonconst;
3e4093b6
RS
7187 char implicit;
7188 char erroneous;
7189 char outer;
7190 char incremental;
7191 char designated;
976d5a22 7192 int designator_depth;
3e4093b6 7193};
d45cf215 7194
802415d1 7195static struct constructor_stack *constructor_stack;
d45cf215 7196
3e4093b6
RS
7197/* This stack represents designators from some range designator up to
7198 the last designator in the list. */
d45cf215 7199
3e4093b6
RS
7200struct constructor_range_stack
7201{
7202 struct constructor_range_stack *next, *prev;
7203 struct constructor_stack *stack;
7204 tree range_start;
7205 tree index;
7206 tree range_end;
7207 tree fields;
7208};
d45cf215 7209
802415d1 7210static struct constructor_range_stack *constructor_range_stack;
d45cf215 7211
3e4093b6
RS
7212/* This stack records separate initializers that are nested.
7213 Nested initializers can't happen in ANSI C, but GNU C allows them
7214 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7215
3e4093b6 7216struct initializer_stack
d45cf215 7217{
3e4093b6
RS
7218 struct initializer_stack *next;
7219 tree decl;
3e4093b6
RS
7220 struct constructor_stack *constructor_stack;
7221 struct constructor_range_stack *constructor_range_stack;
9771b263 7222 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7223 struct spelling *spelling;
7224 struct spelling *spelling_base;
7225 int spelling_size;
7226 char top_level;
7227 char require_constant_value;
7228 char require_constant_elements;
7229};
d45cf215 7230
802415d1 7231static struct initializer_stack *initializer_stack;
3e4093b6
RS
7232\f
7233/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7234
7235void
a396f8ae 7236start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7237{
3e4093b6 7238 const char *locus;
cceb1885 7239 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7240
3e4093b6 7241 p->decl = constructor_decl;
3e4093b6
RS
7242 p->require_constant_value = require_constant_value;
7243 p->require_constant_elements = require_constant_elements;
7244 p->constructor_stack = constructor_stack;
7245 p->constructor_range_stack = constructor_range_stack;
7246 p->elements = constructor_elements;
7247 p->spelling = spelling;
7248 p->spelling_base = spelling_base;
7249 p->spelling_size = spelling_size;
7250 p->top_level = constructor_top_level;
7251 p->next = initializer_stack;
7252 initializer_stack = p;
400fbf9f 7253
3e4093b6 7254 constructor_decl = decl;
3e4093b6
RS
7255 constructor_designated = 0;
7256 constructor_top_level = top_level;
400fbf9f 7257
6f17bbcf 7258 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7259 {
7260 require_constant_value = TREE_STATIC (decl);
7261 require_constant_elements
7262 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7263 /* For a scalar, you can always use any value to initialize,
7264 even within braces. */
296a8c2f 7265 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7266 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7267 }
7268 else
7269 {
7270 require_constant_value = 0;
7271 require_constant_elements = 0;
88388a52 7272 locus = _("(anonymous)");
3e4093b6 7273 }
b71c7f8a 7274
3e4093b6
RS
7275 constructor_stack = 0;
7276 constructor_range_stack = 0;
b71c7f8a 7277
9bac5cbb 7278 found_missing_braces = 0;
3e4093b6
RS
7279
7280 spelling_base = 0;
7281 spelling_size = 0;
7282 RESTORE_SPELLING_DEPTH (0);
7283
7284 if (locus)
7285 push_string (locus);
7286}
7287
7288void
7289finish_init (void)
b71c7f8a 7290{
3e4093b6 7291 struct initializer_stack *p = initializer_stack;
b71c7f8a 7292
3e4093b6
RS
7293 /* Free the whole constructor stack of this initializer. */
7294 while (constructor_stack)
7295 {
7296 struct constructor_stack *q = constructor_stack;
7297 constructor_stack = q->next;
7298 free (q);
7299 }
7300
366de0ce 7301 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7302
7303 /* Pop back to the data of the outer initializer (if any). */
36579663 7304 free (spelling_base);
3aeb3655 7305
3e4093b6 7306 constructor_decl = p->decl;
3e4093b6
RS
7307 require_constant_value = p->require_constant_value;
7308 require_constant_elements = p->require_constant_elements;
7309 constructor_stack = p->constructor_stack;
7310 constructor_range_stack = p->constructor_range_stack;
7311 constructor_elements = p->elements;
7312 spelling = p->spelling;
7313 spelling_base = p->spelling_base;
7314 spelling_size = p->spelling_size;
7315 constructor_top_level = p->top_level;
7316 initializer_stack = p->next;
7317 free (p);
b71c7f8a 7318}
400fbf9f 7319\f
3e4093b6
RS
7320/* Call here when we see the initializer is surrounded by braces.
7321 This is instead of a call to push_init_level;
7322 it is matched by a call to pop_init_level.
400fbf9f 7323
3e4093b6
RS
7324 TYPE is the type to initialize, for a constructor expression.
7325 For an initializer for a decl, TYPE is zero. */
400fbf9f 7326
3e4093b6
RS
7327void
7328really_start_incremental_init (tree type)
400fbf9f 7329{
5d038c4c 7330 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7331
3e4093b6
RS
7332 if (type == 0)
7333 type = TREE_TYPE (constructor_decl);
400fbf9f 7334
31521951 7335 if (VECTOR_TYPE_P (type)
b6fc2cdb 7336 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7337 error ("opaque vector types cannot be initialized");
400fbf9f 7338
3e4093b6
RS
7339 p->type = constructor_type;
7340 p->fields = constructor_fields;
7341 p->index = constructor_index;
7342 p->max_index = constructor_max_index;
7343 p->unfilled_index = constructor_unfilled_index;
7344 p->unfilled_fields = constructor_unfilled_fields;
7345 p->bit_index = constructor_bit_index;
7346 p->elements = constructor_elements;
7347 p->constant = constructor_constant;
7348 p->simple = constructor_simple;
928c19bb 7349 p->nonconst = constructor_nonconst;
3e4093b6
RS
7350 p->erroneous = constructor_erroneous;
7351 p->pending_elts = constructor_pending_elts;
7352 p->depth = constructor_depth;
916c5919
JM
7353 p->replacement_value.value = 0;
7354 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7355 p->replacement_value.original_type = NULL;
3e4093b6
RS
7356 p->implicit = 0;
7357 p->range_stack = 0;
7358 p->outer = 0;
7359 p->incremental = constructor_incremental;
7360 p->designated = constructor_designated;
976d5a22 7361 p->designator_depth = designator_depth;
3e4093b6
RS
7362 p->next = 0;
7363 constructor_stack = p;
b13aca19 7364
3e4093b6
RS
7365 constructor_constant = 1;
7366 constructor_simple = 1;
928c19bb 7367 constructor_nonconst = 0;
3e4093b6 7368 constructor_depth = SPELLING_DEPTH ();
9771b263 7369 constructor_elements = NULL;
3e4093b6
RS
7370 constructor_pending_elts = 0;
7371 constructor_type = type;
7372 constructor_incremental = 1;
7373 constructor_designated = 0;
9bac5cbb 7374 constructor_zeroinit = 1;
3e4093b6 7375 designator_depth = 0;
b06df647 7376 designator_erroneous = 0;
400fbf9f 7377
3e636daf 7378 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7379 {
3e4093b6
RS
7380 constructor_fields = TYPE_FIELDS (constructor_type);
7381 /* Skip any nameless bit fields at the beginning. */
7382 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7383 && DECL_NAME (constructor_fields) == 0)
910ad8de 7384 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7385
3e4093b6
RS
7386 constructor_unfilled_fields = constructor_fields;
7387 constructor_bit_index = bitsize_zero_node;
400fbf9f 7388 }
3e4093b6
RS
7389 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7390 {
7391 if (TYPE_DOMAIN (constructor_type))
7392 {
7393 constructor_max_index
7394 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7395
3e4093b6
RS
7396 /* Detect non-empty initializations of zero-length arrays. */
7397 if (constructor_max_index == NULL_TREE
7398 && TYPE_SIZE (constructor_type))
9a9d280e 7399 constructor_max_index = integer_minus_one_node;
400fbf9f 7400
3e4093b6
RS
7401 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7402 to initialize VLAs will cause a proper error; avoid tree
7403 checking errors as well by setting a safe value. */
7404 if (constructor_max_index
7405 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7406 constructor_max_index = integer_minus_one_node;
59c83dbf 7407
3e4093b6
RS
7408 constructor_index
7409 = convert (bitsizetype,
7410 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7411 }
3e4093b6 7412 else
493179da
JM
7413 {
7414 constructor_index = bitsize_zero_node;
7415 constructor_max_index = NULL_TREE;
7416 }
59c83dbf 7417
3e4093b6
RS
7418 constructor_unfilled_index = constructor_index;
7419 }
31521951 7420 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7421 {
7422 /* Vectors are like simple fixed-size arrays. */
7423 constructor_max_index =
c62c040f 7424 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7425 constructor_index = bitsize_zero_node;
3e4093b6
RS
7426 constructor_unfilled_index = constructor_index;
7427 }
7428 else
7429 {
7430 /* Handle the case of int x = {5}; */
7431 constructor_fields = constructor_type;
7432 constructor_unfilled_fields = constructor_type;
7433 }
7434}
7435\f
7436/* Push down into a subobject, for initialization.
7437 If this is for an explicit set of braces, IMPLICIT is 0.
7438 If it is because the next element belongs at a lower level,
7439 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7440
3e4093b6 7441void
ea58ef42
MP
7442push_init_level (location_t loc, int implicit,
7443 struct obstack *braced_init_obstack)
3e4093b6
RS
7444{
7445 struct constructor_stack *p;
7446 tree value = NULL_TREE;
400fbf9f 7447
3e4093b6 7448 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7449 pop them now. If implicit == 1, this will have been done in
7450 process_init_element; do not repeat it here because in the case
7451 of excess initializers for an empty aggregate this leads to an
7452 infinite cycle of popping a level and immediately recreating
7453 it. */
7454 if (implicit != 1)
3e4093b6 7455 {
472d98b4
JM
7456 while (constructor_stack->implicit)
7457 {
3e636daf 7458 if (RECORD_OR_UNION_TYPE_P (constructor_type)
472d98b4 7459 && constructor_fields == 0)
34cf811f 7460 process_init_element (input_location,
ea58ef42 7461 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7462 true, braced_init_obstack);
472d98b4
JM
7463 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7464 && constructor_max_index
7465 && tree_int_cst_lt (constructor_max_index,
7466 constructor_index))
34cf811f 7467 process_init_element (input_location,
ea58ef42 7468 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7469 true, braced_init_obstack);
472d98b4
JM
7470 else
7471 break;
7472 }
3e4093b6 7473 }
400fbf9f 7474
3e4093b6
RS
7475 /* Unless this is an explicit brace, we need to preserve previous
7476 content if any. */
7477 if (implicit)
7478 {
3e636daf 7479 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7480 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7481 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7482 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7483 }
7484
5d038c4c 7485 p = XNEW (struct constructor_stack);
3e4093b6
RS
7486 p->type = constructor_type;
7487 p->fields = constructor_fields;
7488 p->index = constructor_index;
7489 p->max_index = constructor_max_index;
7490 p->unfilled_index = constructor_unfilled_index;
7491 p->unfilled_fields = constructor_unfilled_fields;
7492 p->bit_index = constructor_bit_index;
7493 p->elements = constructor_elements;
7494 p->constant = constructor_constant;
7495 p->simple = constructor_simple;
928c19bb 7496 p->nonconst = constructor_nonconst;
3e4093b6
RS
7497 p->erroneous = constructor_erroneous;
7498 p->pending_elts = constructor_pending_elts;
7499 p->depth = constructor_depth;
916c5919
JM
7500 p->replacement_value.value = 0;
7501 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7502 p->replacement_value.original_type = NULL;
3e4093b6
RS
7503 p->implicit = implicit;
7504 p->outer = 0;
7505 p->incremental = constructor_incremental;
7506 p->designated = constructor_designated;
976d5a22 7507 p->designator_depth = designator_depth;
3e4093b6
RS
7508 p->next = constructor_stack;
7509 p->range_stack = 0;
7510 constructor_stack = p;
400fbf9f 7511
3e4093b6
RS
7512 constructor_constant = 1;
7513 constructor_simple = 1;
928c19bb 7514 constructor_nonconst = 0;
3e4093b6 7515 constructor_depth = SPELLING_DEPTH ();
9771b263 7516 constructor_elements = NULL;
3e4093b6
RS
7517 constructor_incremental = 1;
7518 constructor_designated = 0;
7519 constructor_pending_elts = 0;
7520 if (!implicit)
400fbf9f 7521 {
3e4093b6
RS
7522 p->range_stack = constructor_range_stack;
7523 constructor_range_stack = 0;
7524 designator_depth = 0;
b06df647 7525 designator_erroneous = 0;
3e4093b6 7526 }
400fbf9f 7527
3e4093b6
RS
7528 /* Don't die if an entire brace-pair level is superfluous
7529 in the containing level. */
7530 if (constructor_type == 0)
7531 ;
3e636daf 7532 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7533 {
7534 /* Don't die if there are extra init elts at the end. */
7535 if (constructor_fields == 0)
7536 constructor_type = 0;
7537 else
400fbf9f 7538 {
3e4093b6
RS
7539 constructor_type = TREE_TYPE (constructor_fields);
7540 push_member_name (constructor_fields);
7541 constructor_depth++;
400fbf9f 7542 }
6a358dcb
MP
7543 /* If upper initializer is designated, then mark this as
7544 designated too to prevent bogus warnings. */
7545 constructor_designated = p->designated;
3e4093b6
RS
7546 }
7547 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7548 {
7549 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7550 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7551 constructor_depth++;
400fbf9f
JW
7552 }
7553
3e4093b6 7554 if (constructor_type == 0)
400fbf9f 7555 {
ea58ef42 7556 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7557 constructor_fields = 0;
7558 constructor_unfilled_fields = 0;
7559 return;
400fbf9f
JW
7560 }
7561
3e4093b6
RS
7562 if (value && TREE_CODE (value) == CONSTRUCTOR)
7563 {
7564 constructor_constant = TREE_CONSTANT (value);
7565 constructor_simple = TREE_STATIC (value);
928c19bb 7566 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7567 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7568 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7569 && (TREE_CODE (constructor_type) == RECORD_TYPE
7570 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7571 set_nonincremental_init (braced_init_obstack);
3e4093b6 7572 }
400fbf9f 7573
9bac5cbb
G
7574 if (implicit == 1)
7575 found_missing_braces = 1;
400fbf9f 7576
3e636daf 7577 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7578 {
7579 constructor_fields = TYPE_FIELDS (constructor_type);
7580 /* Skip any nameless bit fields at the beginning. */
7581 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7582 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7583 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7584
3e4093b6
RS
7585 constructor_unfilled_fields = constructor_fields;
7586 constructor_bit_index = bitsize_zero_node;
7587 }
31521951 7588 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7589 {
7590 /* Vectors are like simple fixed-size arrays. */
7591 constructor_max_index =
c62c040f
RG
7592 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7593 constructor_index = bitsize_int (0);
3e4093b6
RS
7594 constructor_unfilled_index = constructor_index;
7595 }
7596 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7597 {
7598 if (TYPE_DOMAIN (constructor_type))
7599 {
7600 constructor_max_index
7601 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7602
3e4093b6
RS
7603 /* Detect non-empty initializations of zero-length arrays. */
7604 if (constructor_max_index == NULL_TREE
7605 && TYPE_SIZE (constructor_type))
9a9d280e 7606 constructor_max_index = integer_minus_one_node;
de520661 7607
3e4093b6
RS
7608 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7609 to initialize VLAs will cause a proper error; avoid tree
7610 checking errors as well by setting a safe value. */
7611 if (constructor_max_index
7612 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7613 constructor_max_index = integer_minus_one_node;
b62acd60 7614
3e4093b6
RS
7615 constructor_index
7616 = convert (bitsizetype,
7617 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7618 }
7619 else
7620 constructor_index = bitsize_zero_node;
de520661 7621
3e4093b6
RS
7622 constructor_unfilled_index = constructor_index;
7623 if (value && TREE_CODE (value) == STRING_CST)
7624 {
7625 /* We need to split the char/wchar array into individual
7626 characters, so that we don't have to special case it
7627 everywhere. */
a1e3b3d9 7628 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7629 }
7630 }
7631 else
7632 {
b4519d39 7633 if (constructor_type != error_mark_node)
96b40f8d 7634 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7635 constructor_fields = constructor_type;
7636 constructor_unfilled_fields = constructor_type;
7637 }
7638}
8b6a5902 7639
3e4093b6 7640/* At the end of an implicit or explicit brace level,
916c5919
JM
7641 finish up that level of constructor. If a single expression
7642 with redundant braces initialized that level, return the
7643 c_expr structure for that expression. Otherwise, the original_code
7644 element is set to ERROR_MARK.
7645 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7646 from inner levels (process_init_element ignores that),
916c5919 7647 but return error_mark_node as the value from the outermost level
3e4093b6 7648 (that's what we want to put in DECL_INITIAL).
916c5919 7649 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7650
916c5919 7651struct c_expr
ea58ef42
MP
7652pop_init_level (location_t loc, int implicit,
7653 struct obstack *braced_init_obstack)
3e4093b6
RS
7654{
7655 struct constructor_stack *p;
916c5919
JM
7656 struct c_expr ret;
7657 ret.value = 0;
7658 ret.original_code = ERROR_MARK;
6866c6e8 7659 ret.original_type = NULL;
de520661 7660
3e4093b6
RS
7661 if (implicit == 0)
7662 {
7663 /* When we come to an explicit close brace,
7664 pop any inner levels that didn't have explicit braces. */
7665 while (constructor_stack->implicit)
34cf811f 7666 process_init_element (input_location,
ea58ef42 7667 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7668 true, braced_init_obstack);
366de0ce 7669 gcc_assert (!constructor_range_stack);
3e4093b6 7670 }
e5e809f4 7671
0066ef9c
RH
7672 /* Now output all pending elements. */
7673 constructor_incremental = 1;
a1e3b3d9 7674 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7675
3e4093b6 7676 p = constructor_stack;
e5e809f4 7677
3e4093b6
RS
7678 /* Error for initializing a flexible array member, or a zero-length
7679 array member in an inappropriate context. */
7680 if (constructor_type && constructor_fields
7681 && TREE_CODE (constructor_type) == ARRAY_TYPE
7682 && TYPE_DOMAIN (constructor_type)
3f75a254 7683 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7684 {
7685 /* Silently discard empty initializations. The parser will
7686 already have pedwarned for empty brackets. */
7687 if (integer_zerop (constructor_unfilled_index))
7688 constructor_type = NULL_TREE;
366de0ce 7689 else
3e4093b6 7690 {
366de0ce 7691 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7692
3e4093b6 7693 if (constructor_depth > 2)
ea58ef42 7694 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7695 else
d033409e 7696 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7697 "initialization of a flexible array member");
de520661 7698
3e4093b6
RS
7699 /* We have already issued an error message for the existence
7700 of a flexible array member not at the end of the structure.
535a42b1 7701 Discard the initializer so that we do not die later. */
910ad8de 7702 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7703 constructor_type = NULL_TREE;
7704 }
3e4093b6 7705 }
de520661 7706
7b33f0c8
MP
7707 switch (vec_safe_length (constructor_elements))
7708 {
7709 case 0:
7710 /* Initialization with { } counts as zeroinit. */
7711 constructor_zeroinit = 1;
7712 break;
7713 case 1:
7714 /* This might be zeroinit as well. */
7715 if (integer_zerop ((*constructor_elements)[0].value))
7716 constructor_zeroinit = 1;
7717 break;
7718 default:
7719 /* If the constructor has more than one element, it can't be { 0 }. */
7720 constructor_zeroinit = 0;
7721 break;
7722 }
9bac5cbb
G
7723
7724 /* Warn when some structs are initialized with direct aggregation. */
7725 if (!implicit && found_missing_braces && warn_missing_braces
7726 && !constructor_zeroinit)
7b33f0c8
MP
7727 warning_init (loc, OPT_Wmissing_braces,
7728 "missing braces around initializer");
9bac5cbb 7729
3e4093b6 7730 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7731 if (warn_missing_field_initializers
3e4093b6
RS
7732 && constructor_type
7733 && TREE_CODE (constructor_type) == RECORD_TYPE
7734 && constructor_unfilled_fields)
7735 {
7736 /* Do not warn for flexible array members or zero-length arrays. */
7737 while (constructor_unfilled_fields
3f75a254 7738 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7739 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7740 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7741
49819fef
AM
7742 if (constructor_unfilled_fields
7743 /* Do not warn if this level of the initializer uses member
7744 designators; it is likely to be deliberate. */
7745 && !constructor_designated
84937de2 7746 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7747 && !constructor_zeroinit)
3e4093b6 7748 {
32397f22
MLI
7749 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7750 "missing initializer for field %qD of %qT",
7751 constructor_unfilled_fields,
7752 constructor_type))
7753 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7754 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7755 }
7756 }
de520661 7757
3e4093b6 7758 /* Pad out the end of the structure. */
916c5919 7759 if (p->replacement_value.value)
3e4093b6
RS
7760 /* If this closes a superfluous brace pair,
7761 just pass out the element between them. */
916c5919 7762 ret = p->replacement_value;
3e4093b6
RS
7763 else if (constructor_type == 0)
7764 ;
3e636daf 7765 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 7766 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 7767 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7768 {
7769 /* A nonincremental scalar initializer--just return
7770 the element, after verifying there is just one. */
9771b263 7771 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7772 {
7773 if (!constructor_erroneous)
ea58ef42 7774 error_init (loc, "empty scalar initializer");
916c5919 7775 ret.value = error_mark_node;
3e4093b6 7776 }
9771b263 7777 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7778 {
ea58ef42 7779 error_init (loc, "extra elements in scalar initializer");
9771b263 7780 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7781 }
7782 else
9771b263 7783 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7784 }
7785 else
7786 {
7787 if (constructor_erroneous)
916c5919 7788 ret.value = error_mark_node;
3e4093b6
RS
7789 else
7790 {
916c5919 7791 ret.value = build_constructor (constructor_type,
4038c495 7792 constructor_elements);
3e4093b6 7793 if (constructor_constant)
51eed280 7794 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7795 if (constructor_constant && constructor_simple)
916c5919 7796 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7797 if (constructor_nonconst)
7798 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7799 }
7800 }
de520661 7801
928c19bb
JM
7802 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7803 {
7804 if (constructor_nonconst)
7805 ret.original_code = C_MAYBE_CONST_EXPR;
7806 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7807 ret.original_code = ERROR_MARK;
7808 }
7809
3e4093b6
RS
7810 constructor_type = p->type;
7811 constructor_fields = p->fields;
7812 constructor_index = p->index;
7813 constructor_max_index = p->max_index;
7814 constructor_unfilled_index = p->unfilled_index;
7815 constructor_unfilled_fields = p->unfilled_fields;
7816 constructor_bit_index = p->bit_index;
7817 constructor_elements = p->elements;
7818 constructor_constant = p->constant;
7819 constructor_simple = p->simple;
928c19bb 7820 constructor_nonconst = p->nonconst;
3e4093b6
RS
7821 constructor_erroneous = p->erroneous;
7822 constructor_incremental = p->incremental;
7823 constructor_designated = p->designated;
976d5a22 7824 designator_depth = p->designator_depth;
3e4093b6
RS
7825 constructor_pending_elts = p->pending_elts;
7826 constructor_depth = p->depth;
7827 if (!p->implicit)
7828 constructor_range_stack = p->range_stack;
7829 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7830
3e4093b6
RS
7831 constructor_stack = p->next;
7832 free (p);
b621a4dd 7833
5d5e98dc
VR
7834 if (ret.value == 0 && constructor_stack == 0)
7835 ret.value = error_mark_node;
916c5919 7836 return ret;
3e4093b6 7837}
8b6a5902 7838
3e4093b6
RS
7839/* Common handling for both array range and field name designators.
7840 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7841
3e4093b6 7842static int
ea58ef42
MP
7843set_designator (location_t loc, int array,
7844 struct obstack *braced_init_obstack)
de520661 7845{
3e4093b6
RS
7846 tree subtype;
7847 enum tree_code subcode;
de520661 7848
3e4093b6
RS
7849 /* Don't die if an entire brace-pair level is superfluous
7850 in the containing level. */
7851 if (constructor_type == 0)
7852 return 1;
de520661 7853
366de0ce
NS
7854 /* If there were errors in this designator list already, bail out
7855 silently. */
b06df647 7856 if (designator_erroneous)
3e4093b6 7857 return 1;
e28cae4f 7858
3e4093b6
RS
7859 if (!designator_depth)
7860 {
366de0ce 7861 gcc_assert (!constructor_range_stack);
de520661 7862
3e4093b6
RS
7863 /* Designator list starts at the level of closest explicit
7864 braces. */
7865 while (constructor_stack->implicit)
34cf811f 7866 process_init_element (input_location,
ea58ef42 7867 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7868 true, braced_init_obstack);
3e4093b6
RS
7869 constructor_designated = 1;
7870 return 0;
7871 }
de520661 7872
366de0ce 7873 switch (TREE_CODE (constructor_type))
3c3fa147 7874 {
366de0ce
NS
7875 case RECORD_TYPE:
7876 case UNION_TYPE:
3e4093b6
RS
7877 subtype = TREE_TYPE (constructor_fields);
7878 if (subtype != error_mark_node)
7879 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7880 break;
7881 case ARRAY_TYPE:
3e4093b6 7882 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7883 break;
7884 default:
7885 gcc_unreachable ();
de520661 7886 }
400fbf9f 7887
3e4093b6
RS
7888 subcode = TREE_CODE (subtype);
7889 if (array && subcode != ARRAY_TYPE)
7890 {
ea58ef42 7891 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7892 return 1;
7893 }
7894 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7895 {
ea58ef42 7896 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7897 return 1;
7898 }
d45cf215 7899
3e4093b6 7900 constructor_designated = 1;
ea58ef42 7901 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7902 return 0;
de520661 7903}
400fbf9f 7904
3e4093b6
RS
7905/* If there are range designators in designator list, push a new designator
7906 to constructor_range_stack. RANGE_END is end of such stack range or
7907 NULL_TREE if there is no range designator at this level. */
400fbf9f 7908
3e4093b6 7909static void
a1e3b3d9 7910push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7911{
7912 struct constructor_range_stack *p;
400fbf9f 7913
a1e3b3d9
LB
7914 p = (struct constructor_range_stack *)
7915 obstack_alloc (braced_init_obstack,
7916 sizeof (struct constructor_range_stack));
3e4093b6
RS
7917 p->prev = constructor_range_stack;
7918 p->next = 0;
7919 p->fields = constructor_fields;
7920 p->range_start = constructor_index;
7921 p->index = constructor_index;
7922 p->stack = constructor_stack;
7923 p->range_end = range_end;
8b6a5902 7924 if (constructor_range_stack)
3e4093b6
RS
7925 constructor_range_stack->next = p;
7926 constructor_range_stack = p;
de520661 7927}
400fbf9f 7928
3e4093b6
RS
7929/* Within an array initializer, specify the next index to be initialized.
7930 FIRST is that index. If LAST is nonzero, then initialize a range
7931 of indices, running from FIRST through LAST. */
5a7ec9d9 7932
de520661 7933void
ea58ef42 7934set_init_index (location_t loc, tree first, tree last,
d033409e 7935 struct obstack *braced_init_obstack)
de520661 7936{
ea58ef42 7937 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7938 return;
de520661 7939
b06df647 7940 designator_erroneous = 1;
de520661 7941
3ea8cd06
JM
7942 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7943 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7944 {
ea58ef42 7945 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7946 return;
7947 }
7948
2b6da65c
JM
7949 if (TREE_CODE (first) != INTEGER_CST)
7950 {
7951 first = c_fully_fold (first, false, NULL);
7952 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7953 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7954 "array index in initializer is not "
7955 "an integer constant expression");
7956 }
7957
7958 if (last && TREE_CODE (last) != INTEGER_CST)
7959 {
7960 last = c_fully_fold (last, false, NULL);
7961 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7962 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7963 "array index in initializer is not "
7964 "an integer constant expression");
7965 }
7966
3e4093b6 7967 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7968 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7969 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7970 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7971 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7972 error_init (loc, "array index in non-array initializer");
622adc7e 7973 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7974 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7975 else if (constructor_max_index
7976 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7977 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7978 else
de520661 7979 {
928c19bb
JM
7980 constant_expression_warning (first);
7981 if (last)
7982 constant_expression_warning (last);
3e4093b6 7983 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7984 if (tree_int_cst_lt (constructor_index, first))
7985 {
7986 constructor_index = copy_node (constructor_index);
7987 TREE_OVERFLOW (constructor_index) = 1;
7988 }
665f2503 7989
3e4093b6 7990 if (last)
2bede729 7991 {
3e4093b6
RS
7992 if (tree_int_cst_equal (first, last))
7993 last = 0;
7994 else if (tree_int_cst_lt (last, first))
7995 {
ea58ef42 7996 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7997 last = 0;
7998 }
7999 else
8000 {
8001 last = convert (bitsizetype, last);
8002 if (constructor_max_index != 0
8003 && tree_int_cst_lt (constructor_max_index, last))
8004 {
ea58ef42
MP
8005 error_init (loc, "array index range in initializer exceeds "
8006 "array bounds");
3e4093b6
RS
8007 last = 0;
8008 }
8009 }
2bede729 8010 }
fed3cef0 8011
3e4093b6 8012 designator_depth++;
b06df647 8013 designator_erroneous = 0;
3e4093b6 8014 if (constructor_range_stack || last)
a1e3b3d9 8015 push_range_stack (last, braced_init_obstack);
de520661 8016 }
de520661 8017}
3e4093b6
RS
8018
8019/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8020
de520661 8021void
ea58ef42
MP
8022set_init_label (location_t loc, tree fieldname,
8023 struct obstack *braced_init_obstack)
de520661 8024{
0fb96aa4 8025 tree field;
94ba5069 8026
ea58ef42 8027 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
8028 return;
8029
b06df647 8030 designator_erroneous = 1;
3e4093b6 8031
3e636daf 8032 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8033 {
ea58ef42 8034 error_init (loc, "field name not in record or union initializer");
3e4093b6 8035 return;
94ba5069
RS
8036 }
8037
0fb96aa4 8038 field = lookup_field (constructor_type, fieldname);
8b6a5902 8039
0fb96aa4 8040 if (field == 0)
9babc352 8041 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 8042 else
0fb96aa4
JM
8043 do
8044 {
8045 constructor_fields = TREE_VALUE (field);
8046 designator_depth++;
8047 designator_erroneous = 0;
8048 if (constructor_range_stack)
8049 push_range_stack (NULL_TREE, braced_init_obstack);
8050 field = TREE_CHAIN (field);
8051 if (field)
8052 {
ea58ef42 8053 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
8054 return;
8055 }
8056 }
8057 while (field != NULL_TREE);
3e4093b6
RS
8058}
8059\f
8060/* Add a new initializer to the tree of pending initializers. PURPOSE
8061 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8062 VALUE is the value of that index or field. If ORIGTYPE is not
8063 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8064
8065 IMPLICIT is true if value comes from pop_init_level (1),
8066 the new initializer has been merged with the existing one
8067 and thus no warnings should be emitted about overriding an
8068 existing initializer. */
de520661 8069
3e4093b6 8070static void
96b40f8d
MP
8071add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8072 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8073{
8074 struct init_node *p, **q, *r;
8075
8076 q = &constructor_pending_elts;
8077 p = 0;
8078
8079 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8080 {
3e4093b6 8081 while (*q != 0)
91fa3c30 8082 {
3e4093b6
RS
8083 p = *q;
8084 if (tree_int_cst_lt (purpose, p->purpose))
8085 q = &p->left;
8086 else if (tree_int_cst_lt (p->purpose, purpose))
8087 q = &p->right;
8088 else
8089 {
b295aee2
JJ
8090 if (!implicit)
8091 {
8092 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8093 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8094 "initialized field with side-effects "
8095 "overwritten");
b295aee2 8096 else if (warn_override_init)
96b40f8d
MP
8097 warning_init (loc, OPT_Woverride_init,
8098 "initialized field overwritten");
b295aee2 8099 }
3e4093b6 8100 p->value = value;
bbbbb16a 8101 p->origtype = origtype;
3e4093b6
RS
8102 return;
8103 }
91fa3c30 8104 }
de520661 8105 }
3e4093b6 8106 else
de520661 8107 {
3e4093b6 8108 tree bitpos;
400fbf9f 8109
3e4093b6
RS
8110 bitpos = bit_position (purpose);
8111 while (*q != NULL)
8112 {
8113 p = *q;
8114 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8115 q = &p->left;
8116 else if (p->purpose != purpose)
8117 q = &p->right;
8118 else
8119 {
b295aee2
JJ
8120 if (!implicit)
8121 {
8122 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8123 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8124 "initialized field with side-effects "
8125 "overwritten");
b295aee2 8126 else if (warn_override_init)
96b40f8d
MP
8127 warning_init (loc, OPT_Woverride_init,
8128 "initialized field overwritten");
b295aee2 8129 }
3e4093b6 8130 p->value = value;
bbbbb16a 8131 p->origtype = origtype;
3e4093b6
RS
8132 return;
8133 }
8134 }
91fa3c30 8135 }
b71c7f8a 8136
a1e3b3d9
LB
8137 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8138 sizeof (struct init_node));
3e4093b6
RS
8139 r->purpose = purpose;
8140 r->value = value;
bbbbb16a 8141 r->origtype = origtype;
8b6a5902 8142
3e4093b6
RS
8143 *q = r;
8144 r->parent = p;
8145 r->left = 0;
8146 r->right = 0;
8147 r->balance = 0;
b71c7f8a 8148
3e4093b6 8149 while (p)
de520661 8150 {
3e4093b6 8151 struct init_node *s;
665f2503 8152
3e4093b6 8153 if (r == p->left)
2bede729 8154 {
3e4093b6
RS
8155 if (p->balance == 0)
8156 p->balance = -1;
8157 else if (p->balance < 0)
8158 {
8159 if (r->balance < 0)
8160 {
8161 /* L rotation. */
8162 p->left = r->right;
8163 if (p->left)
8164 p->left->parent = p;
8165 r->right = p;
e7b6a0ee 8166
3e4093b6
RS
8167 p->balance = 0;
8168 r->balance = 0;
39bc99c2 8169
3e4093b6
RS
8170 s = p->parent;
8171 p->parent = r;
8172 r->parent = s;
8173 if (s)
8174 {
8175 if (s->left == p)
8176 s->left = r;
8177 else
8178 s->right = r;
8179 }
8180 else
8181 constructor_pending_elts = r;
8182 }
8183 else
8184 {
8185 /* LR rotation. */
8186 struct init_node *t = r->right;
e7b6a0ee 8187
3e4093b6
RS
8188 r->right = t->left;
8189 if (r->right)
8190 r->right->parent = r;
8191 t->left = r;
8192
8193 p->left = t->right;
8194 if (p->left)
8195 p->left->parent = p;
8196 t->right = p;
8197
8198 p->balance = t->balance < 0;
8199 r->balance = -(t->balance > 0);
8200 t->balance = 0;
8201
8202 s = p->parent;
8203 p->parent = t;
8204 r->parent = t;
8205 t->parent = s;
8206 if (s)
8207 {
8208 if (s->left == p)
8209 s->left = t;
8210 else
8211 s->right = t;
8212 }
8213 else
8214 constructor_pending_elts = t;
8215 }
8216 break;
8217 }
8218 else
8219 {
8220 /* p->balance == +1; growth of left side balances the node. */
8221 p->balance = 0;
8222 break;
8223 }
2bede729 8224 }
3e4093b6
RS
8225 else /* r == p->right */
8226 {
8227 if (p->balance == 0)
8228 /* Growth propagation from right side. */
8229 p->balance++;
8230 else if (p->balance > 0)
8231 {
8232 if (r->balance > 0)
8233 {
8234 /* R rotation. */
8235 p->right = r->left;
8236 if (p->right)
8237 p->right->parent = p;
8238 r->left = p;
8239
8240 p->balance = 0;
8241 r->balance = 0;
8242
8243 s = p->parent;
8244 p->parent = r;
8245 r->parent = s;
8246 if (s)
8247 {
8248 if (s->left == p)
8249 s->left = r;
8250 else
8251 s->right = r;
8252 }
8253 else
8254 constructor_pending_elts = r;
8255 }
8256 else /* r->balance == -1 */
8257 {
8258 /* RL rotation */
8259 struct init_node *t = r->left;
8260
8261 r->left = t->right;
8262 if (r->left)
8263 r->left->parent = r;
8264 t->right = r;
8265
8266 p->right = t->left;
8267 if (p->right)
8268 p->right->parent = p;
8269 t->left = p;
8270
8271 r->balance = (t->balance < 0);
8272 p->balance = -(t->balance > 0);
8273 t->balance = 0;
8274
8275 s = p->parent;
8276 p->parent = t;
8277 r->parent = t;
8278 t->parent = s;
8279 if (s)
8280 {
8281 if (s->left == p)
8282 s->left = t;
8283 else
8284 s->right = t;
8285 }
8286 else
8287 constructor_pending_elts = t;
8288 }
8289 break;
8290 }
8291 else
8292 {
8293 /* p->balance == -1; growth of right side balances the node. */
8294 p->balance = 0;
8295 break;
8296 }
8297 }
8298
8299 r = p;
8300 p = p->parent;
8301 }
8302}
8303
8304/* Build AVL tree from a sorted chain. */
8305
8306static void
a1e3b3d9 8307set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8308{
4038c495
GB
8309 unsigned HOST_WIDE_INT ix;
8310 tree index, value;
3e4093b6
RS
8311
8312 if (TREE_CODE (constructor_type) != RECORD_TYPE
8313 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8314 return;
8315
4038c495 8316 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8317 add_pending_init (input_location, index, value, NULL_TREE, true,
8318 braced_init_obstack);
9771b263 8319 constructor_elements = NULL;
3e4093b6
RS
8320 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8321 {
8322 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8323 /* Skip any nameless bit fields at the beginning. */
8324 while (constructor_unfilled_fields != 0
8325 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8326 && DECL_NAME (constructor_unfilled_fields) == 0)
8327 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8328
de520661 8329 }
3e4093b6 8330 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8331 {
3e4093b6
RS
8332 if (TYPE_DOMAIN (constructor_type))
8333 constructor_unfilled_index
8334 = convert (bitsizetype,
8335 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8336 else
8337 constructor_unfilled_index = bitsize_zero_node;
de520661 8338 }
3e4093b6 8339 constructor_incremental = 0;
de520661 8340}
400fbf9f 8341
3e4093b6 8342/* Build AVL tree from a string constant. */
de520661 8343
3e4093b6 8344static void
a1e3b3d9
LB
8345set_nonincremental_init_from_string (tree str,
8346 struct obstack * braced_init_obstack)
de520661 8347{
3e4093b6
RS
8348 tree value, purpose, type;
8349 HOST_WIDE_INT val[2];
8350 const char *p, *end;
8351 int byte, wchar_bytes, charwidth, bitpos;
de520661 8352
366de0ce 8353 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8354
c466b2cd 8355 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8356 charwidth = TYPE_PRECISION (char_type_node);
8357 type = TREE_TYPE (constructor_type);
8358 p = TREE_STRING_POINTER (str);
8359 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8360
3e4093b6 8361 for (purpose = bitsize_zero_node;
8824edff
JJ
8362 p < end
8363 && !(constructor_max_index
8364 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8365 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8366 {
3e4093b6 8367 if (wchar_bytes == 1)
ffc5c6a9 8368 {
807e902e
KZ
8369 val[0] = (unsigned char) *p++;
8370 val[1] = 0;
ffc5c6a9
RH
8371 }
8372 else
3e4093b6 8373 {
3e4093b6 8374 val[1] = 0;
807e902e 8375 val[0] = 0;
3e4093b6
RS
8376 for (byte = 0; byte < wchar_bytes; byte++)
8377 {
8378 if (BYTES_BIG_ENDIAN)
8379 bitpos = (wchar_bytes - byte - 1) * charwidth;
8380 else
8381 bitpos = byte * charwidth;
807e902e 8382 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8383 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8384 << (bitpos % HOST_BITS_PER_WIDE_INT);
8385 }
8386 }
584ef5fe 8387
8df83eae 8388 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8389 {
8390 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8391 if (bitpos < HOST_BITS_PER_WIDE_INT)
8392 {
807e902e 8393 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8394 {
aa256c4a 8395 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8396 val[1] = -1;
3e4093b6
RS
8397 }
8398 }
8399 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8400 {
807e902e
KZ
8401 if (val[0] < 0)
8402 val[1] = -1;
3e4093b6 8403 }
807e902e 8404 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8405 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8406 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8407 }
ffc5c6a9 8408
807e902e
KZ
8409 value = wide_int_to_tree (type,
8410 wide_int::from_array (val, 2,
8411 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8412 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8413 braced_init_obstack);
9dfcc8db
BH
8414 }
8415
3e4093b6
RS
8416 constructor_incremental = 0;
8417}
de520661 8418
3e4093b6
RS
8419/* Return value of FIELD in pending initializer or zero if the field was
8420 not initialized yet. */
8421
8422static tree
a1e3b3d9 8423find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8424{
8425 struct init_node *p;
8426
8427 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8428 {
3e4093b6
RS
8429 if (constructor_incremental
8430 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8431 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8432
8433 p = constructor_pending_elts;
8434 while (p)
19d76e60 8435 {
3e4093b6
RS
8436 if (tree_int_cst_lt (field, p->purpose))
8437 p = p->left;
8438 else if (tree_int_cst_lt (p->purpose, field))
8439 p = p->right;
8440 else
8441 return p->value;
19d76e60 8442 }
19d76e60 8443 }
3e4093b6 8444 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8445 {
3e4093b6 8446 tree bitpos = bit_position (field);
de520661 8447
3e4093b6
RS
8448 if (constructor_incremental
8449 && (!constructor_unfilled_fields
8450 || tree_int_cst_lt (bitpos,
8451 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8452 set_nonincremental_init (braced_init_obstack);
de520661 8453
3e4093b6
RS
8454 p = constructor_pending_elts;
8455 while (p)
8456 {
8457 if (field == p->purpose)
8458 return p->value;
8459 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8460 p = p->left;
8461 else
8462 p = p->right;
8463 }
8464 }
8465 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8466 {
9771b263
DN
8467 if (!vec_safe_is_empty (constructor_elements)
8468 && (constructor_elements->last ().index == field))
8469 return constructor_elements->last ().value;
de520661 8470 }
3e4093b6 8471 return 0;
de520661
RS
8472}
8473
3e4093b6
RS
8474/* "Output" the next constructor element.
8475 At top level, really output it to assembler code now.
8476 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8477 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8478 TYPE is the data type that the containing data type wants here.
8479 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8480 If VALUE is a string constant, STRICT_STRING is true if it is
8481 unparenthesized or we should not warn here for it being parenthesized.
8482 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8483
3e4093b6
RS
8484 PENDING if non-nil means output pending elements that belong
8485 right after this element. (PENDING is normally 1;
b295aee2
JJ
8486 it is 0 while outputting pending elements, to avoid recursion.)
8487
8488 IMPLICIT is true if value comes from pop_init_level (1),
8489 the new initializer has been merged with the existing one
8490 and thus no warnings should be emitted about overriding an
8491 existing initializer. */
8b6a5902 8492
3e4093b6 8493static void
34cf811f
MP
8494output_init_element (location_t loc, tree value, tree origtype,
8495 bool strict_string, tree type, tree field, int pending,
8496 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8497{
8ce94e44 8498 tree semantic_type = NULL_TREE;
928c19bb
JM
8499 bool maybe_const = true;
8500 bool npc;
4038c495 8501
0a880880 8502 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8503 {
3e4093b6
RS
8504 constructor_erroneous = 1;
8505 return;
8b6a5902 8506 }
46bdb9cf
JM
8507 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8508 && (TREE_CODE (value) == STRING_CST
8509 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8510 && !(TREE_CODE (value) == STRING_CST
8511 && TREE_CODE (type) == ARRAY_TYPE
8512 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8513 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8514 TYPE_MAIN_VARIANT (type)))
c2255bc4 8515 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8516
3e4093b6 8517 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8518 && require_constant_value && pending)
8b6a5902 8519 {
3e4093b6
RS
8520 /* As an extension, allow initializing objects with static storage
8521 duration with compound literals (which are then treated just as
8522 the brace enclosed list they contain). */
4435bb92
MP
8523 if (flag_isoc99)
8524 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8525 "constant");
3e4093b6
RS
8526 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8527 value = DECL_INITIAL (decl);
8b6a5902
JJ
8528 }
8529
928c19bb 8530 npc = null_pointer_constant_p (value);
8ce94e44
JM
8531 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8532 {
8533 semantic_type = TREE_TYPE (value);
8534 value = TREE_OPERAND (value, 0);
8535 }
928c19bb
JM
8536 value = c_fully_fold (value, require_constant_value, &maybe_const);
8537
3e4093b6
RS
8538 if (value == error_mark_node)
8539 constructor_erroneous = 1;
8540 else if (!TREE_CONSTANT (value))
8541 constructor_constant = 0;
ee45a32d
EB
8542 else if (!initializer_constant_valid_p (value,
8543 TREE_TYPE (value),
8544 AGGREGATE_TYPE_P (constructor_type)
8545 && TYPE_REVERSE_STORAGE_ORDER
8546 (constructor_type))
3e636daf 8547 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
8548 && DECL_C_BIT_FIELD (field)
8549 && TREE_CODE (value) != INTEGER_CST))
8550 constructor_simple = 0;
928c19bb
JM
8551 if (!maybe_const)
8552 constructor_nonconst = 1;
3e4093b6 8553
116df786 8554 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8555 {
116df786
RH
8556 if (require_constant_value)
8557 {
ea58ef42 8558 error_init (loc, "initializer element is not constant");
116df786
RH
8559 value = error_mark_node;
8560 }
8561 else if (require_constant_elements)
8337d1db 8562 pedwarn (loc, OPT_Wpedantic,
509c9d60 8563 "initializer element is not computable at load time");
8b6a5902 8564 }
928c19bb
JM
8565 else if (!maybe_const
8566 && (require_constant_value || require_constant_elements))
8337d1db 8567 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8568 "initializer element is not a constant expression");
3e4093b6 8569
81f40b79
ILT
8570 /* Issue -Wc++-compat warnings about initializing a bitfield with
8571 enum type. */
8572 if (warn_cxx_compat
8573 && field != NULL_TREE
8574 && TREE_CODE (field) == FIELD_DECL
8575 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8576 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8577 != TYPE_MAIN_VARIANT (type))
8578 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8579 {
8580 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8581 if (checktype != error_mark_node
8582 && (TYPE_MAIN_VARIANT (checktype)
8583 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8584 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8585 "enum conversion in initialization is invalid in C++");
8586 }
8587
3e4093b6
RS
8588 /* If this field is empty (and not at the end of structure),
8589 don't do anything other than checking the initializer. */
8590 if (field
8591 && (TREE_TYPE (field) == error_mark_node
8592 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8593 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8594 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8595 || DECL_CHAIN (field)))))
3e4093b6
RS
8596 return;
8597
8ce94e44
JM
8598 if (semantic_type)
8599 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8600 value = digest_init (loc, type, value, origtype, npc, strict_string,
8601 require_constant_value);
3e4093b6 8602 if (value == error_mark_node)
8b6a5902 8603 {
3e4093b6
RS
8604 constructor_erroneous = 1;
8605 return;
8b6a5902 8606 }
928c19bb
JM
8607 if (require_constant_value || require_constant_elements)
8608 constant_expression_warning (value);
8b6a5902 8609
3e4093b6
RS
8610 /* If this element doesn't come next in sequence,
8611 put it on constructor_pending_elts. */
8612 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8613 && (!constructor_incremental
8614 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8615 {
3e4093b6
RS
8616 if (constructor_incremental
8617 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8618 set_nonincremental_init (braced_init_obstack);
3e4093b6 8619
96b40f8d 8620 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8621 braced_init_obstack);
3e4093b6 8622 return;
8b6a5902 8623 }
3e4093b6
RS
8624 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8625 && (!constructor_incremental
8626 || field != constructor_unfilled_fields))
8b6a5902 8627 {
3e4093b6
RS
8628 /* We do this for records but not for unions. In a union,
8629 no matter which field is specified, it can be initialized
8630 right away since it starts at the beginning of the union. */
8631 if (constructor_incremental)
8632 {
8633 if (!constructor_unfilled_fields)
a1e3b3d9 8634 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8635 else
8636 {
8637 tree bitpos, unfillpos;
8638
8639 bitpos = bit_position (field);
8640 unfillpos = bit_position (constructor_unfilled_fields);
8641
8642 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8643 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8644 }
8645 }
8646
96b40f8d 8647 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8648 braced_init_obstack);
3e4093b6 8649 return;
8b6a5902 8650 }
3e4093b6 8651 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8652 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8653 {
b295aee2
JJ
8654 if (!implicit)
8655 {
9771b263 8656 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8657 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8658 "initialized field with side-effects overwritten");
8659 else if (warn_override_init)
96b40f8d
MP
8660 warning_init (loc, OPT_Woverride_init,
8661 "initialized field overwritten");
b295aee2 8662 }
8b6a5902 8663
3e4093b6 8664 /* We can have just one union field set. */
9771b263 8665 constructor_elements = NULL;
3e4093b6 8666 }
8b6a5902 8667
3e4093b6
RS
8668 /* Otherwise, output this element either to
8669 constructor_elements or to the assembler file. */
8b6a5902 8670
f32682ca 8671 constructor_elt celt = {field, value};
9771b263 8672 vec_safe_push (constructor_elements, celt);
8b6a5902 8673
3e4093b6
RS
8674 /* Advance the variable that indicates sequential elements output. */
8675 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8676 constructor_unfilled_index
db3927fb
AH
8677 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8678 bitsize_one_node);
3e4093b6
RS
8679 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8680 {
8681 constructor_unfilled_fields
910ad8de 8682 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8683
3e4093b6
RS
8684 /* Skip any nameless bit fields. */
8685 while (constructor_unfilled_fields != 0
8686 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8687 && DECL_NAME (constructor_unfilled_fields) == 0)
8688 constructor_unfilled_fields =
910ad8de 8689 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8690 }
8691 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8692 constructor_unfilled_fields = 0;
de520661 8693
3e4093b6
RS
8694 /* Now output any pending elements which have become next. */
8695 if (pending)
a1e3b3d9 8696 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8697}
8b6a5902 8698
3e4093b6
RS
8699/* Output any pending elements which have become next.
8700 As we output elements, constructor_unfilled_{fields,index}
8701 advances, which may cause other elements to become next;
8702 if so, they too are output.
8b6a5902 8703
3e4093b6
RS
8704 If ALL is 0, we return when there are
8705 no more pending elements to output now.
665f2503 8706
3e4093b6
RS
8707 If ALL is 1, we output space as necessary so that
8708 we can output all the pending elements. */
3e4093b6 8709static void
a1e3b3d9 8710output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8711{
8712 struct init_node *elt = constructor_pending_elts;
8713 tree next;
de520661 8714
3e4093b6
RS
8715 retry:
8716
ba228239 8717 /* Look through the whole pending tree.
3e4093b6
RS
8718 If we find an element that should be output now,
8719 output it. Otherwise, set NEXT to the element
8720 that comes first among those still pending. */
8721
8722 next = 0;
8723 while (elt)
8724 {
8725 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8726 {
3e4093b6
RS
8727 if (tree_int_cst_equal (elt->purpose,
8728 constructor_unfilled_index))
34cf811f
MP
8729 output_init_element (input_location, elt->value, elt->origtype,
8730 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8731 constructor_unfilled_index, 0, false,
8732 braced_init_obstack);
3e4093b6
RS
8733 else if (tree_int_cst_lt (constructor_unfilled_index,
8734 elt->purpose))
8b6a5902 8735 {
3e4093b6
RS
8736 /* Advance to the next smaller node. */
8737 if (elt->left)
8738 elt = elt->left;
8739 else
8740 {
8741 /* We have reached the smallest node bigger than the
8742 current unfilled index. Fill the space first. */
8743 next = elt->purpose;
8744 break;
8745 }
8b6a5902 8746 }
ce662d4c
JJ
8747 else
8748 {
3e4093b6
RS
8749 /* Advance to the next bigger node. */
8750 if (elt->right)
8751 elt = elt->right;
8752 else
ce662d4c 8753 {
3e4093b6
RS
8754 /* We have reached the biggest node in a subtree. Find
8755 the parent of it, which is the next bigger node. */
8756 while (elt->parent && elt->parent->right == elt)
8757 elt = elt->parent;
8758 elt = elt->parent;
8759 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8760 elt->purpose))
8761 {
8762 next = elt->purpose;
8763 break;
8764 }
ce662d4c
JJ
8765 }
8766 }
8b6a5902 8767 }
3e636daf 8768 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8769 {
8770 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8771
3e4093b6
RS
8772 /* If the current record is complete we are done. */
8773 if (constructor_unfilled_fields == 0)
8774 break;
de520661 8775
3e4093b6
RS
8776 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8777 elt_bitpos = bit_position (elt->purpose);
8778 /* We can't compare fields here because there might be empty
8779 fields in between. */
8780 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8781 {
8782 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8783 output_init_element (input_location, elt->value, elt->origtype,
8784 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8785 elt->purpose, 0, false,
8786 braced_init_obstack);
3e4093b6
RS
8787 }
8788 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8789 {
8790 /* Advance to the next smaller node. */
8791 if (elt->left)
8792 elt = elt->left;
8793 else
8794 {
8795 /* We have reached the smallest node bigger than the
8796 current unfilled field. Fill the space first. */
8797 next = elt->purpose;
8798 break;
8799 }
8800 }
8801 else
8802 {
8803 /* Advance to the next bigger node. */
8804 if (elt->right)
8805 elt = elt->right;
8806 else
8807 {
8808 /* We have reached the biggest node in a subtree. Find
8809 the parent of it, which is the next bigger node. */
8810 while (elt->parent && elt->parent->right == elt)
8811 elt = elt->parent;
8812 elt = elt->parent;
8813 if (elt
8814 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8815 bit_position (elt->purpose))))
8816 {
8817 next = elt->purpose;
8818 break;
8819 }
8820 }
8821 }
8822 }
8823 }
de520661 8824
3e4093b6
RS
8825 /* Ordinarily return, but not if we want to output all
8826 and there are elements left. */
3f75a254 8827 if (!(all && next != 0))
e5cfb88f
RK
8828 return;
8829
3e4093b6
RS
8830 /* If it's not incremental, just skip over the gap, so that after
8831 jumping to retry we will output the next successive element. */
3e636daf 8832 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8833 constructor_unfilled_fields = next;
8834 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8835 constructor_unfilled_index = next;
de520661 8836
3e4093b6
RS
8837 /* ELT now points to the node in the pending tree with the next
8838 initializer to output. */
8839 goto retry;
de520661
RS
8840}
8841\f
3e4093b6
RS
8842/* Add one non-braced element to the current constructor level.
8843 This adjusts the current position within the constructor's type.
8844 This may also start or terminate implicit levels
8845 to handle a partly-braced initializer.
e5e809f4 8846
3e4093b6 8847 Once this has found the correct level for the new element,
b295aee2
JJ
8848 it calls output_init_element.
8849
8850 IMPLICIT is true if value comes from pop_init_level (1),
8851 the new initializer has been merged with the existing one
8852 and thus no warnings should be emitted about overriding an
8853 existing initializer. */
3e4093b6
RS
8854
8855void
34cf811f 8856process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8857 struct obstack * braced_init_obstack)
e5e809f4 8858{
916c5919
JM
8859 tree orig_value = value.value;
8860 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8861 bool strict_string = value.original_code == STRING_CST;
340baef7 8862 bool was_designated = designator_depth != 0;
e5e809f4 8863
3e4093b6 8864 designator_depth = 0;
b06df647 8865 designator_erroneous = 0;
e5e809f4 8866
9bac5cbb
G
8867 if (!implicit && value.value && !integer_zerop (value.value))
8868 constructor_zeroinit = 0;
8869
3e4093b6
RS
8870 /* Handle superfluous braces around string cst as in
8871 char x[] = {"foo"}; */
8872 if (string_flag
8873 && constructor_type
340baef7 8874 && !was_designated
3e4093b6 8875 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8876 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8877 && integer_zerop (constructor_unfilled_index))
e5e809f4 8878 {
916c5919 8879 if (constructor_stack->replacement_value.value)
ea58ef42 8880 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8881 constructor_stack->replacement_value = value;
8882 return;
e5e809f4 8883 }
8b6a5902 8884
916c5919 8885 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8886 {
ea58ef42 8887 error_init (loc, "excess elements in struct initializer");
3e4093b6 8888 return;
e5e809f4
JL
8889 }
8890
3e4093b6
RS
8891 /* Ignore elements of a brace group if it is entirely superfluous
8892 and has already been diagnosed. */
8893 if (constructor_type == 0)
8894 return;
e5e809f4 8895
976d5a22
TT
8896 if (!implicit && warn_designated_init && !was_designated
8897 && TREE_CODE (constructor_type) == RECORD_TYPE
8898 && lookup_attribute ("designated_init",
8899 TYPE_ATTRIBUTES (constructor_type)))
8900 warning_init (loc,
8901 OPT_Wdesignated_init,
8902 "positional initialization of field "
8903 "in %<struct%> declared with %<designated_init%> attribute");
8904
3e4093b6
RS
8905 /* If we've exhausted any levels that didn't have braces,
8906 pop them now. */
8907 while (constructor_stack->implicit)
8908 {
3e636daf 8909 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8910 && constructor_fields == 0)
ea58ef42
MP
8911 process_init_element (loc,
8912 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8913 true, braced_init_obstack);
53650abe 8914 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 8915 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
8916 && constructor_max_index
8917 && tree_int_cst_lt (constructor_max_index,
8918 constructor_index))
ea58ef42
MP
8919 process_init_element (loc,
8920 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8921 true, braced_init_obstack);
3e4093b6
RS
8922 else
8923 break;
8924 }
e5e809f4 8925
3e4093b6
RS
8926 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8927 if (constructor_range_stack)
e5e809f4 8928 {
3e4093b6
RS
8929 /* If value is a compound literal and we'll be just using its
8930 content, don't put it into a SAVE_EXPR. */
916c5919 8931 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 8932 || !require_constant_value)
8ce94e44
JM
8933 {
8934 tree semantic_type = NULL_TREE;
8935 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8936 {
8937 semantic_type = TREE_TYPE (value.value);
8938 value.value = TREE_OPERAND (value.value, 0);
8939 }
8940 value.value = c_save_expr (value.value);
8941 if (semantic_type)
8942 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8943 value.value);
8944 }
3e4093b6 8945 }
e5e809f4 8946
3e4093b6
RS
8947 while (1)
8948 {
8949 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8950 {
3e4093b6
RS
8951 tree fieldtype;
8952 enum tree_code fieldcode;
e5e809f4 8953
3e4093b6
RS
8954 if (constructor_fields == 0)
8955 {
ea58ef42 8956 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8957 break;
8958 }
e5e809f4 8959
3e4093b6
RS
8960 fieldtype = TREE_TYPE (constructor_fields);
8961 if (fieldtype != error_mark_node)
8962 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8963 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8964
3e4093b6
RS
8965 /* Error for non-static initialization of a flexible array member. */
8966 if (fieldcode == ARRAY_TYPE
8967 && !require_constant_value
8968 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8969 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8970 {
ea58ef42
MP
8971 error_init (loc, "non-static initialization of a flexible "
8972 "array member");
3e4093b6
RS
8973 break;
8974 }
e5e809f4 8975
2cc901dc
MP
8976 /* Error for initialization of a flexible array member with
8977 a string constant if the structure is in an array. E.g.:
8978 struct S { int x; char y[]; };
8979 struct S s[] = { { 1, "foo" } };
8980 is invalid. */
8981 if (string_flag
8982 && fieldcode == ARRAY_TYPE
8983 && constructor_depth > 1
8984 && TYPE_SIZE (fieldtype) == NULL_TREE
8985 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8986 {
8987 bool in_array_p = false;
8988 for (struct constructor_stack *p = constructor_stack;
8989 p && p->type; p = p->next)
8990 if (TREE_CODE (p->type) == ARRAY_TYPE)
8991 {
8992 in_array_p = true;
8993 break;
8994 }
8995 if (in_array_p)
8996 {
8997 error_init (loc, "initialization of flexible array "
8998 "member in a nested context");
8999 break;
9000 }
9001 }
9002
3e4093b6 9003 /* Accept a string constant to initialize a subarray. */
916c5919 9004 if (value.value != 0
3e4093b6 9005 && fieldcode == ARRAY_TYPE
197463ae 9006 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9007 && string_flag)
916c5919 9008 value.value = orig_value;
3e4093b6
RS
9009 /* Otherwise, if we have come to a subaggregate,
9010 and we don't have an element of its type, push into it. */
0953878d 9011 else if (value.value != 0
916c5919
JM
9012 && value.value != error_mark_node
9013 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9014 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9015 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9016 {
ea58ef42 9017 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9018 continue;
9019 }
e5e809f4 9020
916c5919 9021 if (value.value)
3e4093b6
RS
9022 {
9023 push_member_name (constructor_fields);
34cf811f 9024 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9025 strict_string, fieldtype,
a1e3b3d9
LB
9026 constructor_fields, 1, implicit,
9027 braced_init_obstack);
3e4093b6 9028 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9029 }
9030 else
3e4093b6
RS
9031 /* Do the bookkeeping for an element that was
9032 directly output as a constructor. */
e5e809f4 9033 {
3e4093b6
RS
9034 /* For a record, keep track of end position of last field. */
9035 if (DECL_SIZE (constructor_fields))
c22cacf3 9036 constructor_bit_index
db3927fb
AH
9037 = size_binop_loc (input_location, PLUS_EXPR,
9038 bit_position (constructor_fields),
9039 DECL_SIZE (constructor_fields));
3e4093b6
RS
9040
9041 /* If the current field was the first one not yet written out,
9042 it isn't now, so update. */
9043 if (constructor_unfilled_fields == constructor_fields)
9044 {
910ad8de 9045 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9046 /* Skip any nameless bit fields. */
9047 while (constructor_unfilled_fields != 0
9048 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9049 && DECL_NAME (constructor_unfilled_fields) == 0)
9050 constructor_unfilled_fields =
910ad8de 9051 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9052 }
e5e809f4 9053 }
3e4093b6 9054
910ad8de 9055 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9056 /* Skip any nameless bit fields at the beginning. */
9057 while (constructor_fields != 0
9058 && DECL_C_BIT_FIELD (constructor_fields)
9059 && DECL_NAME (constructor_fields) == 0)
910ad8de 9060 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9061 }
3e4093b6 9062 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9063 {
3e4093b6
RS
9064 tree fieldtype;
9065 enum tree_code fieldcode;
e5e809f4 9066
3e4093b6
RS
9067 if (constructor_fields == 0)
9068 {
d033409e 9069 pedwarn_init (loc, 0,
509c9d60 9070 "excess elements in union initializer");
3e4093b6
RS
9071 break;
9072 }
e5e809f4 9073
3e4093b6
RS
9074 fieldtype = TREE_TYPE (constructor_fields);
9075 if (fieldtype != error_mark_node)
9076 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9077 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9078
3e4093b6
RS
9079 /* Warn that traditional C rejects initialization of unions.
9080 We skip the warning if the value is zero. This is done
9081 under the assumption that the zero initializer in user
9082 code appears conditioned on e.g. __STDC__ to avoid
9083 "missing initializer" warnings and relies on default
9084 initialization to zero in the traditional C case.
9085 We also skip the warning if the initializer is designated,
9086 again on the assumption that this must be conditional on
9087 __STDC__ anyway (and we've already complained about the
9088 member-designator already). */
8400e75e 9089 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9090 && !(value.value && (integer_zerop (value.value)
9091 || real_zerop (value.value))))
3176a0c2
DD
9092 warning (OPT_Wtraditional, "traditional C rejects initialization "
9093 "of unions");
e5e809f4 9094
3e4093b6 9095 /* Accept a string constant to initialize a subarray. */
916c5919 9096 if (value.value != 0
3e4093b6 9097 && fieldcode == ARRAY_TYPE
197463ae 9098 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9099 && string_flag)
916c5919 9100 value.value = orig_value;
3e4093b6
RS
9101 /* Otherwise, if we have come to a subaggregate,
9102 and we don't have an element of its type, push into it. */
0953878d 9103 else if (value.value != 0
916c5919
JM
9104 && value.value != error_mark_node
9105 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9106 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9107 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9108 {
ea58ef42 9109 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9110 continue;
9111 }
e5e809f4 9112
916c5919 9113 if (value.value)
3e4093b6
RS
9114 {
9115 push_member_name (constructor_fields);
34cf811f 9116 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9117 strict_string, fieldtype,
a1e3b3d9
LB
9118 constructor_fields, 1, implicit,
9119 braced_init_obstack);
3e4093b6 9120 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9121 }
9122 else
3e4093b6
RS
9123 /* Do the bookkeeping for an element that was
9124 directly output as a constructor. */
e5e809f4 9125 {
3e4093b6 9126 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9127 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9128 }
e5e809f4 9129
3e4093b6
RS
9130 constructor_fields = 0;
9131 }
9132 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9133 {
9134 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9135 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9136
3e4093b6 9137 /* Accept a string constant to initialize a subarray. */
916c5919 9138 if (value.value != 0
3e4093b6 9139 && eltcode == ARRAY_TYPE
197463ae 9140 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9141 && string_flag)
916c5919 9142 value.value = orig_value;
3e4093b6
RS
9143 /* Otherwise, if we have come to a subaggregate,
9144 and we don't have an element of its type, push into it. */
0953878d 9145 else if (value.value != 0
916c5919
JM
9146 && value.value != error_mark_node
9147 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9148 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9149 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9150 {
ea58ef42 9151 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9152 continue;
9153 }
8b6a5902 9154
3e4093b6
RS
9155 if (constructor_max_index != 0
9156 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9157 || integer_all_onesp (constructor_max_index)))
9158 {
d033409e 9159 pedwarn_init (loc, 0,
509c9d60 9160 "excess elements in array initializer");
3e4093b6
RS
9161 break;
9162 }
8b6a5902 9163
3e4093b6 9164 /* Now output the actual element. */
916c5919 9165 if (value.value)
3e4093b6 9166 {
ae7e9ddd 9167 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9168 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9169 strict_string, elttype,
a1e3b3d9
LB
9170 constructor_index, 1, implicit,
9171 braced_init_obstack);
3e4093b6
RS
9172 RESTORE_SPELLING_DEPTH (constructor_depth);
9173 }
2f6e4e97 9174
3e4093b6 9175 constructor_index
db3927fb
AH
9176 = size_binop_loc (input_location, PLUS_EXPR,
9177 constructor_index, bitsize_one_node);
8b6a5902 9178
916c5919 9179 if (!value.value)
3e4093b6
RS
9180 /* If we are doing the bookkeeping for an element that was
9181 directly output as a constructor, we must update
9182 constructor_unfilled_index. */
9183 constructor_unfilled_index = constructor_index;
9184 }
31521951 9185 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9186 {
9187 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9188
c22cacf3
MS
9189 /* Do a basic check of initializer size. Note that vectors
9190 always have a fixed size derived from their type. */
3e4093b6
RS
9191 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9192 {
d033409e 9193 pedwarn_init (loc, 0,
509c9d60 9194 "excess elements in vector initializer");
3e4093b6
RS
9195 break;
9196 }
8b6a5902 9197
3e4093b6 9198 /* Now output the actual element. */
916c5919 9199 if (value.value)
53650abe
AP
9200 {
9201 if (TREE_CODE (value.value) == VECTOR_CST)
9202 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9203 output_init_element (loc, value.value, value.original_type,
53650abe 9204 strict_string, elttype,
a1e3b3d9
LB
9205 constructor_index, 1, implicit,
9206 braced_init_obstack);
53650abe 9207 }
8b6a5902 9208
3e4093b6 9209 constructor_index
db3927fb
AH
9210 = size_binop_loc (input_location,
9211 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9212
916c5919 9213 if (!value.value)
3e4093b6
RS
9214 /* If we are doing the bookkeeping for an element that was
9215 directly output as a constructor, we must update
9216 constructor_unfilled_index. */
9217 constructor_unfilled_index = constructor_index;
9218 }
8b6a5902 9219
3e4093b6
RS
9220 /* Handle the sole element allowed in a braced initializer
9221 for a scalar variable. */
b4519d39
SB
9222 else if (constructor_type != error_mark_node
9223 && constructor_fields == 0)
8b6a5902 9224 {
d033409e 9225 pedwarn_init (loc, 0,
509c9d60 9226 "excess elements in scalar initializer");
3e4093b6 9227 break;
8b6a5902
JJ
9228 }
9229 else
9230 {
916c5919 9231 if (value.value)
34cf811f 9232 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9233 strict_string, constructor_type,
a1e3b3d9
LB
9234 NULL_TREE, 1, implicit,
9235 braced_init_obstack);
3e4093b6 9236 constructor_fields = 0;
8b6a5902
JJ
9237 }
9238
3e4093b6
RS
9239 /* Handle range initializers either at this level or anywhere higher
9240 in the designator stack. */
9241 if (constructor_range_stack)
8b6a5902 9242 {
3e4093b6
RS
9243 struct constructor_range_stack *p, *range_stack;
9244 int finish = 0;
9245
9246 range_stack = constructor_range_stack;
9247 constructor_range_stack = 0;
9248 while (constructor_stack != range_stack->stack)
8b6a5902 9249 {
366de0ce 9250 gcc_assert (constructor_stack->implicit);
34cf811f 9251 process_init_element (loc,
ea58ef42
MP
9252 pop_init_level (loc, 1,
9253 braced_init_obstack),
a1e3b3d9 9254 true, braced_init_obstack);
8b6a5902 9255 }
3e4093b6
RS
9256 for (p = range_stack;
9257 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9258 p = p->prev)
8b6a5902 9259 {
366de0ce 9260 gcc_assert (constructor_stack->implicit);
34cf811f 9261 process_init_element (loc,
ea58ef42
MP
9262 pop_init_level (loc, 1,
9263 braced_init_obstack),
a1e3b3d9 9264 true, braced_init_obstack);
8b6a5902 9265 }
3e4093b6 9266
db3927fb
AH
9267 p->index = size_binop_loc (input_location,
9268 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9269 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9270 finish = 1;
9271
9272 while (1)
9273 {
9274 constructor_index = p->index;
9275 constructor_fields = p->fields;
9276 if (finish && p->range_end && p->index == p->range_start)
9277 {
9278 finish = 0;
9279 p->prev = 0;
9280 }
9281 p = p->next;
9282 if (!p)
9283 break;
ea58ef42 9284 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9285 p->stack = constructor_stack;
9286 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9287 p->index = p->range_start;
9288 }
9289
9290 if (!finish)
9291 constructor_range_stack = range_stack;
9292 continue;
8b6a5902
JJ
9293 }
9294
3e4093b6 9295 break;
8b6a5902
JJ
9296 }
9297
3e4093b6
RS
9298 constructor_range_stack = 0;
9299}
9300\f
9f0e2d86
ZW
9301/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9302 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9303 an ASM_EXPR node). */
3e4093b6 9304tree
9f0e2d86 9305build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9306{
6de9cd9a
DN
9307 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9308 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9309 return add_stmt (args);
8b6a5902
JJ
9310}
9311
9f0e2d86
ZW
9312/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9313 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9314 SIMPLE indicates whether there was anything at all after the
9315 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9316 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9317tree
c2255bc4 9318build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9319 tree clobbers, tree labels, bool simple)
e5e809f4 9320{
3e4093b6 9321 tree tail;
9f0e2d86 9322 tree args;
6de9cd9a
DN
9323 int i;
9324 const char *constraint;
74f0c611 9325 const char **oconstraints;
6de9cd9a 9326 bool allows_mem, allows_reg, is_inout;
74f0c611 9327 int ninputs, noutputs;
6de9cd9a
DN
9328
9329 ninputs = list_length (inputs);
9330 noutputs = list_length (outputs);
74f0c611
RH
9331 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9332
1c384bf1 9333 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9334
6de9cd9a
DN
9335 /* Remove output conversions that change the type but not the mode. */
9336 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9337 {
3e4093b6 9338 tree output = TREE_VALUE (tail);
74f0c611 9339
eadd3d0d
JJ
9340 output = c_fully_fold (output, false, NULL);
9341
74f0c611
RH
9342 /* ??? Really, this should not be here. Users should be using a
9343 proper lvalue, dammit. But there's a long history of using casts
9344 in the output operands. In cases like longlong.h, this becomes a
9345 primitive form of typechecking -- if the cast can be removed, then
9346 the output operand had a type of the proper width; otherwise we'll
9347 get an error. Gross, but ... */
3e4093b6 9348 STRIP_NOPS (output);
74f0c611 9349
7bd11157 9350 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9351 output = error_mark_node;
8b6a5902 9352
5544530a
PB
9353 if (output != error_mark_node
9354 && (TREE_READONLY (output)
9355 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9356 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9357 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9358 readonly_error (loc, output, lv_asm);
5544530a 9359
6de9cd9a 9360 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9361 oconstraints[i] = constraint;
9362
9363 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9364 &allows_mem, &allows_reg, &is_inout))
9365 {
9366 /* If the operand is going to end up in memory,
9367 mark it addressable. */
9368 if (!allows_reg && !c_mark_addressable (output))
9369 output = error_mark_node;
bae5cddf
JJ
9370 if (!(!allows_reg && allows_mem)
9371 && output != error_mark_node
9372 && VOID_TYPE_P (TREE_TYPE (output)))
9373 {
9374 error_at (loc, "invalid use of void expression");
9375 output = error_mark_node;
9376 }
74f0c611
RH
9377 }
9378 else
c22cacf3 9379 output = error_mark_node;
3e4093b6 9380
74f0c611 9381 TREE_VALUE (tail) = output;
8b6a5902 9382 }
3e4093b6 9383
74f0c611
RH
9384 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9385 {
9386 tree input;
9387
9388 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9389 input = TREE_VALUE (tail);
9390
74f0c611
RH
9391 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9392 oconstraints, &allows_mem, &allows_reg))
9393 {
9394 /* If the operand is going to end up in memory,
9395 mark it addressable. */
b4c33883
AP
9396 if (!allows_reg && allows_mem)
9397 {
eadd3d0d
JJ
9398 input = c_fully_fold (input, false, NULL);
9399
b4c33883
AP
9400 /* Strip the nops as we allow this case. FIXME, this really
9401 should be rejected or made deprecated. */
9402 STRIP_NOPS (input);
9403 if (!c_mark_addressable (input))
9404 input = error_mark_node;
bae5cddf 9405 }
eadd3d0d 9406 else
bae5cddf 9407 {
eadd3d0d
JJ
9408 struct c_expr expr;
9409 memset (&expr, 0, sizeof (expr));
9410 expr.value = input;
267bac10 9411 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9412 input = c_fully_fold (expr.value, false, NULL);
9413
9414 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9415 {
9416 error_at (loc, "invalid use of void expression");
9417 input = error_mark_node;
9418 }
bae5cddf 9419 }
74f0c611
RH
9420 }
9421 else
9422 input = error_mark_node;
9423
9424 TREE_VALUE (tail) = input;
9425 }
3e4093b6 9426
1c384bf1
RH
9427 /* ASMs with labels cannot have outputs. This should have been
9428 enforced by the parser. */
9429 gcc_assert (outputs == NULL || labels == NULL);
9430
9431 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9432
5544530a
PB
9433 /* asm statements without outputs, including simple ones, are treated
9434 as volatile. */
9435 ASM_INPUT_P (args) = simple;
9436 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9437
9f0e2d86 9438 return args;
e5e809f4 9439}
3e4093b6 9440\f
c2255bc4
AH
9441/* Generate a goto statement to LABEL. LOC is the location of the
9442 GOTO. */
506e2710
RH
9443
9444tree
c2255bc4 9445c_finish_goto_label (location_t loc, tree label)
506e2710 9446{
e1b7793c 9447 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9448 if (!decl)
9449 return NULL_TREE;
506e2710 9450 TREE_USED (decl) = 1;
c2255bc4
AH
9451 {
9452 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9453 SET_EXPR_LOCATION (t, loc);
9454 return add_stmt (t);
9455 }
506e2710
RH
9456}
9457
c2255bc4
AH
9458/* Generate a computed goto statement to EXPR. LOC is the location of
9459 the GOTO. */
506e2710
RH
9460
9461tree
c2255bc4 9462c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9463{
c2255bc4 9464 tree t;
c1771a20 9465 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9466 expr = c_fully_fold (expr, false, NULL);
506e2710 9467 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9468 t = build1 (GOTO_EXPR, void_type_node, expr);
9469 SET_EXPR_LOCATION (t, loc);
9470 return add_stmt (t);
506e2710
RH
9471}
9472
5088b058 9473/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9474 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9475 the location of the return statement, or the location of the expression,
9476 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9477 is the original type of RETVAL. */
de520661 9478
506e2710 9479tree
c2255bc4 9480c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9481{
0c9b182b
JJ
9482 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9483 bool no_warning = false;
928c19bb 9484 bool npc = false;
36536d79 9485 size_t rank = 0;
3e4093b6 9486
de8ddd5f
MP
9487 /* Use the expansion point to handle cases such as returning NULL
9488 in a function returning void. */
9489 source_location xloc = expansion_point_location_if_in_system_header (loc);
9490
3e4093b6 9491 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9492 warning_at (xloc, 0,
c2255bc4 9493 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9494
b72271b9 9495 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9496 {
9497 /* Array notations are allowed in a return statement if it is inside a
9498 built-in array notation reduction function. */
9499 if (!find_rank (loc, retval, retval, false, &rank))
9500 return error_mark_node;
9501 if (rank >= 1)
9502 {
9503 error_at (loc, "array notation expression cannot be used as a "
9504 "return value");
9505 return error_mark_node;
9506 }
9507 }
3af9c5e9 9508 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9509 {
9510 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9511 "allowed");
9512 return error_mark_node;
9513 }
928c19bb
JM
9514 if (retval)
9515 {
8ce94e44 9516 tree semantic_type = NULL_TREE;
928c19bb 9517 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9518 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9519 {
9520 semantic_type = TREE_TYPE (retval);
9521 retval = TREE_OPERAND (retval, 0);
9522 }
928c19bb 9523 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9524 if (semantic_type)
9525 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9526 }
9527
3e4093b6 9528 if (!retval)
de520661 9529 {
3e4093b6
RS
9530 current_function_returns_null = 1;
9531 if ((warn_return_type || flag_isoc99)
9532 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9533 {
35aff4fb
MP
9534 if (flag_isoc99)
9535 pedwarn (loc, 0, "%<return%> with no value, in "
9536 "function returning non-void");
9537 else
9538 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9539 "in function returning non-void");
0c9b182b
JJ
9540 no_warning = true;
9541 }
400fbf9f 9542 }
3e4093b6 9543 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9544 {
3e4093b6 9545 current_function_returns_null = 1;
2397c575 9546 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
de8ddd5f 9547 pedwarn (xloc, 0,
509c9d60 9548 "%<return%> with a value, in function returning void");
b8698a0f 9549 else
de8ddd5f 9550 pedwarn (xloc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9551 "%<return%> with expression, in function returning void");
de520661 9552 }
3e4093b6 9553 else
de520661 9554 {
68fca595
MP
9555 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9556 retval, origtype, ic_return,
c2255bc4 9557 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9558 tree res = DECL_RESULT (current_function_decl);
9559 tree inner;
9feb29df 9560 bool save;
3e4093b6
RS
9561
9562 current_function_returns_value = 1;
9563 if (t == error_mark_node)
506e2710 9564 return NULL_TREE;
3e4093b6 9565
9feb29df
JJ
9566 save = in_late_binary_op;
9567 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9568 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9569 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9570 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9571 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9572 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9573 in_late_binary_op = true;
3e4093b6 9574 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9575 in_late_binary_op = save;
3e4093b6
RS
9576
9577 /* Strip any conversions, additions, and subtractions, and see if
9578 we are returning the address of a local variable. Warn if so. */
9579 while (1)
8b6a5902 9580 {
3e4093b6 9581 switch (TREE_CODE (inner))
8b6a5902 9582 {
849421a3
JJ
9583 CASE_CONVERT:
9584 case NON_LVALUE_EXPR:
3e4093b6 9585 case PLUS_EXPR:
849421a3 9586 case POINTER_PLUS_EXPR:
3e4093b6
RS
9587 inner = TREE_OPERAND (inner, 0);
9588 continue;
9589
9590 case MINUS_EXPR:
9591 /* If the second operand of the MINUS_EXPR has a pointer
9592 type (or is converted from it), this may be valid, so
9593 don't give a warning. */
9594 {
9595 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9596
3f75a254 9597 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9598 && (CONVERT_EXPR_P (op1)
9599 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9600 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9601
3e4093b6
RS
9602 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9603 break;
8b6a5902 9604
3e4093b6
RS
9605 inner = TREE_OPERAND (inner, 0);
9606 continue;
9607 }
400fbf9f 9608
3e4093b6
RS
9609 case ADDR_EXPR:
9610 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9611
6615c446 9612 while (REFERENCE_CLASS_P (inner)
22d03525 9613 && !INDIRECT_REF_P (inner))
3e4093b6 9614 inner = TREE_OPERAND (inner, 0);
8b6a5902 9615
a2f1f4c3 9616 if (DECL_P (inner)
3f75a254
JM
9617 && !DECL_EXTERNAL (inner)
9618 && !TREE_STATIC (inner)
3e4093b6 9619 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9620 {
9621 if (TREE_CODE (inner) == LABEL_DECL)
9622 warning_at (loc, OPT_Wreturn_local_addr,
9623 "function returns address of label");
9624 else
b4dfdc11
MG
9625 {
9626 warning_at (loc, OPT_Wreturn_local_addr,
9627 "function returns address of local variable");
9628 tree zero = build_zero_cst (TREE_TYPE (res));
9629 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9630 }
19fc9faa 9631 }
3e4093b6 9632 break;
8b6a5902 9633
3e4093b6
RS
9634 default:
9635 break;
9636 }
de520661 9637
3e4093b6
RS
9638 break;
9639 }
9640
53fb4de3 9641 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9642 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9643
9644 if (warn_sequence_point)
9645 verify_sequence_points (retval);
de520661 9646 }
8b6a5902 9647
c2255bc4 9648 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9649 TREE_NO_WARNING (ret_stmt) |= no_warning;
9650 return add_stmt (ret_stmt);
de520661 9651}
3e4093b6
RS
9652\f
9653struct c_switch {
604f5adf
ILT
9654 /* The SWITCH_EXPR being built. */
9655 tree switch_expr;
a6c0a76c 9656
89dbed81 9657 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9658 default conversion is applied. */
9659 tree orig_type;
9660
3e4093b6
RS
9661 /* A splay-tree mapping the low element of a case range to the high
9662 element, or NULL_TREE if there is no high element. Used to
9663 determine whether or not a new case label duplicates an old case
9664 label. We need a tree, rather than simply a hash table, because
9665 of the GNU case range extension. */
9666 splay_tree cases;
a6c0a76c 9667
e1b7793c
ILT
9668 /* The bindings at the point of the switch. This is used for
9669 warnings crossing decls when branching to a case label. */
9670 struct c_spot_bindings *bindings;
187230a7 9671
3e4093b6
RS
9672 /* The next node on the stack. */
9673 struct c_switch *next;
b155cfd9
MP
9674
9675 /* Remember whether the controlling expression had boolean type
9676 before integer promotions for the sake of -Wswitch-bool. */
9677 bool bool_cond_p;
9678
9679 /* Remember whether there was a case value that is outside the
9680 range of the ORIG_TYPE. */
9681 bool outside_range_p;
3e4093b6 9682};
400fbf9f 9683
3e4093b6
RS
9684/* A stack of the currently active switch statements. The innermost
9685 switch statement is on the top of the stack. There is no need to
9686 mark the stack for garbage collection because it is only active
9687 during the processing of the body of a function, and we never
9688 collect at that point. */
de520661 9689
506e2710 9690struct c_switch *c_switch_stack;
de520661 9691
3e4093b6 9692/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9693 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9694 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9695 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9696
3e4093b6 9697tree
c2255bc4
AH
9698c_start_case (location_t switch_loc,
9699 location_t switch_cond_loc,
fedfecef 9700 tree exp, bool explicit_cast_p)
de520661 9701{
c58e8676 9702 tree orig_type = error_mark_node;
b155cfd9 9703 bool bool_cond_p = false;
3e4093b6 9704 struct c_switch *cs;
2f6e4e97 9705
3e4093b6 9706 if (exp != error_mark_node)
de520661 9707 {
3e4093b6
RS
9708 orig_type = TREE_TYPE (exp);
9709
c58e8676 9710 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9711 {
c58e8676
VR
9712 if (orig_type != error_mark_node)
9713 {
c2255bc4 9714 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9715 orig_type = error_mark_node;
9716 }
3e4093b6 9717 exp = integer_zero_node;
de520661 9718 }
3e4093b6 9719 else
de520661 9720 {
c58e8676 9721 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9722 tree e = exp;
9723
9724 /* Warn if the condition has boolean value. */
9725 while (TREE_CODE (e) == COMPOUND_EXPR)
9726 e = TREE_OPERAND (e, 1);
9727
9728 if ((TREE_CODE (type) == BOOLEAN_TYPE
9729 || truth_value_p (TREE_CODE (e)))
9730 /* Explicit cast to int suppresses this warning. */
9731 && !(TREE_CODE (type) == INTEGER_TYPE
9732 && explicit_cast_p))
b155cfd9 9733 bool_cond_p = true;
8b6a5902 9734
8400e75e 9735 if (!in_system_header_at (input_location)
3e4093b6
RS
9736 && (type == long_integer_type_node
9737 || type == long_unsigned_type_node))
c2255bc4
AH
9738 warning_at (switch_cond_loc,
9739 OPT_Wtraditional, "%<long%> switch expression not "
9740 "converted to %<int%> in ISO C");
8b6a5902 9741
928c19bb 9742 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9743 exp = default_conversion (exp);
ca085fd7
MLI
9744
9745 if (warn_sequence_point)
9746 verify_sequence_points (exp);
3e4093b6
RS
9747 }
9748 }
9749
604f5adf 9750 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9751 cs = XNEW (struct c_switch);
604f5adf 9752 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9753 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9754 cs->orig_type = orig_type;
3e4093b6 9755 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9756 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
9757 cs->bool_cond_p = bool_cond_p;
9758 cs->outside_range_p = false;
506e2710
RH
9759 cs->next = c_switch_stack;
9760 c_switch_stack = cs;
3e4093b6 9761
604f5adf 9762 return add_stmt (cs->switch_expr);
3e4093b6
RS
9763}
9764
c2255bc4 9765/* Process a case label at location LOC. */
3e4093b6
RS
9766
9767tree
c2255bc4 9768do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9769{
9770 tree label = NULL_TREE;
9771
17cede2e
JM
9772 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9773 {
9774 low_value = c_fully_fold (low_value, false, NULL);
9775 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9776 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9777 "case label is not an integer constant expression");
9778 }
9779
9780 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9781 {
9782 high_value = c_fully_fold (high_value, false, NULL);
9783 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9784 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9785 "case label is not an integer constant expression");
9786 }
9787
e1b7793c 9788 if (c_switch_stack == NULL)
187230a7
JM
9789 {
9790 if (low_value)
e1b7793c 9791 error_at (loc, "case label not within a switch statement");
187230a7 9792 else
e1b7793c
ILT
9793 error_at (loc, "%<default%> label not within a switch statement");
9794 return NULL_TREE;
187230a7 9795 }
de520661 9796
e1b7793c
ILT
9797 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9798 EXPR_LOCATION (c_switch_stack->switch_expr),
9799 loc))
9800 return NULL_TREE;
9801
9802 label = c_add_case_label (loc, c_switch_stack->cases,
9803 SWITCH_COND (c_switch_stack->switch_expr),
9804 c_switch_stack->orig_type,
b155cfd9
MP
9805 low_value, high_value,
9806 &c_switch_stack->outside_range_p);
e1b7793c
ILT
9807 if (label == error_mark_node)
9808 label = NULL_TREE;
3e4093b6
RS
9809 return label;
9810}
de520661 9811
083e891e
MP
9812/* Finish the switch statement. TYPE is the original type of the
9813 controlling expression of the switch, or NULL_TREE. */
de520661 9814
3e4093b6 9815void
083e891e 9816c_finish_case (tree body, tree type)
3e4093b6 9817{
506e2710 9818 struct c_switch *cs = c_switch_stack;
fbc315db 9819 location_t switch_location;
3e4093b6 9820
604f5adf 9821 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9822
6de9cd9a 9823 /* Emit warnings as needed. */
c2255bc4 9824 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9825 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9826 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
9827 SWITCH_COND (cs->switch_expr),
9828 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 9829
3e4093b6 9830 /* Pop the stack. */
506e2710 9831 c_switch_stack = cs->next;
3e4093b6 9832 splay_tree_delete (cs->cases);
e1b7793c 9833 c_release_switch_bindings (cs->bindings);
5d038c4c 9834 XDELETE (cs);
de520661 9835}
325c3691 9836\f
506e2710
RH
9837/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9838 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9839 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9840 statement, and was not surrounded with parenthesis. */
325c3691 9841
9e51cf9d 9842void
506e2710
RH
9843c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9844 tree else_block, bool nested_if)
325c3691 9845{
506e2710 9846 tree stmt;
325c3691 9847
25c22937
BI
9848 /* If the condition has array notations, then the rank of the then_block and
9849 else_block must be either 0 or be equal to the rank of the condition. If
9850 the condition does not have array notations then break them up as it is
9851 broken up in a normal expression. */
b72271b9 9852 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9853 {
9854 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9855 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9856 return;
9857 if (then_block
9858 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9859 return;
9860 if (else_block
9861 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9862 return;
9863 if (cond_rank != then_rank && then_rank != 0)
9864 {
9865 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9866 " and the then-block");
9867 return;
9868 }
9869 else if (cond_rank != else_rank && else_rank != 0)
9870 {
9871 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9872 " and the else-block");
9873 return;
9874 }
9875 }
506e2710
RH
9876 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9877 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9878 {
506e2710 9879 tree inner_if = then_block;
16865eaa 9880
61ada8ae 9881 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9882 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9883 it might not be exactly THEN_BLOCK, but should be the last
9884 non-container statement within. */
9885 while (1)
9886 switch (TREE_CODE (inner_if))
9887 {
9888 case COND_EXPR:
9889 goto found;
9890 case BIND_EXPR:
9891 inner_if = BIND_EXPR_BODY (inner_if);
9892 break;
9893 case STATEMENT_LIST:
9894 inner_if = expr_last (then_block);
9895 break;
9896 case TRY_FINALLY_EXPR:
9897 case TRY_CATCH_EXPR:
9898 inner_if = TREE_OPERAND (inner_if, 0);
9899 break;
9900 default:
366de0ce 9901 gcc_unreachable ();
506e2710
RH
9902 }
9903 found:
16865eaa 9904
506e2710 9905 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9906 warning_at (if_locus, OPT_Wparentheses,
9907 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9908 }
16865eaa 9909
2214de30 9910 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9911 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9912 add_stmt (stmt);
325c3691
RH
9913}
9914
506e2710
RH
9915/* Emit a general-purpose loop construct. START_LOCUS is the location of
9916 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9917 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9918 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9919 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9920
9921void
506e2710
RH
9922c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9923 tree blab, tree clab, bool cond_is_first)
325c3691 9924{
506e2710
RH
9925 tree entry = NULL, exit = NULL, t;
9926
e5e44252
AK
9927 /* In theory could forbid cilk spawn for loop increment expression,
9928 but it should work just fine. */
36536d79 9929
28af952a
RS
9930 /* If the condition is zero don't generate a loop construct. */
9931 if (cond && integer_zerop (cond))
9932 {
9933 if (cond_is_first)
9934 {
9935 t = build_and_jump (&blab);
9936 SET_EXPR_LOCATION (t, start_locus);
9937 add_stmt (t);
9938 }
9939 }
9940 else
506e2710
RH
9941 {
9942 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9943
506e2710 9944 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9945 out of the loop, or to the top of it. If there's no exit condition,
9946 then we just build a jump back to the top. */
506e2710 9947 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9948
28af952a 9949 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9950 {
9951 /* Canonicalize the loop condition to the end. This means
9952 generating a branch to the loop condition. Reuse the
9953 continue label, if possible. */
9954 if (cond_is_first)
9955 {
9956 if (incr || !clab)
9957 {
9958 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9959 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9960 }
9961 else
9962 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9963 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9964 add_stmt (t);
9965 }
9966
506e2710 9967 t = build_and_jump (&blab);
506e2710 9968 if (cond_is_first)
db3927fb
AH
9969 exit = fold_build3_loc (start_locus,
9970 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9971 else
db3927fb
AH
9972 exit = fold_build3_loc (input_location,
9973 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 9974 }
fc402eec
AA
9975 else
9976 {
9977 /* For the backward-goto's location of an unconditional loop
9978 use the beginning of the body, or, if there is none, the
9979 top of the loop. */
9980 location_t loc = EXPR_LOCATION (expr_first (body));
9981 if (loc == UNKNOWN_LOCATION)
9982 loc = start_locus;
9983 SET_EXPR_LOCATION (exit, loc);
9984 }
c22cacf3 9985
506e2710
RH
9986 add_stmt (top);
9987 }
c22cacf3 9988
506e2710
RH
9989 if (body)
9990 add_stmt (body);
9991 if (clab)
9992 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9993 if (incr)
9994 add_stmt (incr);
9995 if (entry)
9996 add_stmt (entry);
9997 if (exit)
9998 add_stmt (exit);
9999 if (blab)
10000 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10001}
325c3691
RH
10002
10003tree
c2255bc4 10004c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10005{
089efaa4 10006 bool skip;
506e2710 10007 tree label = *label_p;
325c3691 10008
089efaa4
ILT
10009 /* In switch statements break is sometimes stylistically used after
10010 a return statement. This can lead to spurious warnings about
10011 control reaching the end of a non-void function when it is
10012 inlined. Note that we are calling block_may_fallthru with
10013 language specific tree nodes; this works because
10014 block_may_fallthru returns true when given something it does not
10015 understand. */
10016 skip = !block_may_fallthru (cur_stmt_list);
10017
506e2710 10018 if (!label)
089efaa4
ILT
10019 {
10020 if (!skip)
c2255bc4 10021 *label_p = label = create_artificial_label (loc);
089efaa4 10022 }
953ff289
DN
10023 else if (TREE_CODE (label) == LABEL_DECL)
10024 ;
10025 else switch (TREE_INT_CST_LOW (label))
506e2710 10026 {
953ff289 10027 case 0:
506e2710 10028 if (is_break)
c2255bc4 10029 error_at (loc, "break statement not within loop or switch");
506e2710 10030 else
c2255bc4 10031 error_at (loc, "continue statement not within a loop");
506e2710 10032 return NULL_TREE;
953ff289
DN
10033
10034 case 1:
10035 gcc_assert (is_break);
c2255bc4 10036 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10037 return NULL_TREE;
10038
c02065fc
AH
10039 case 2:
10040 if (is_break)
10041 error ("break statement within %<#pragma simd%> loop body");
10042 else
10043 error ("continue statement within %<#pragma simd%> loop body");
10044 return NULL_TREE;
10045
953ff289
DN
10046 default:
10047 gcc_unreachable ();
506e2710 10048 }
325c3691 10049
089efaa4
ILT
10050 if (skip)
10051 return NULL_TREE;
10052
2e28e797
JH
10053 if (!is_break)
10054 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10055
53fb4de3 10056 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10057}
10058
506e2710 10059/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10060
10061static void
c2255bc4 10062emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10063{
e6b5a630
RH
10064 if (expr == error_mark_node)
10065 ;
10066 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10067 {
10068 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10069 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10070 }
789eadcd
MP
10071 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10072 {
10073 tree r = expr;
10074 location_t cloc = loc;
10075 while (TREE_CODE (r) == COMPOUND_EXPR)
10076 {
10077 if (EXPR_HAS_LOCATION (r))
10078 cloc = EXPR_LOCATION (r);
10079 r = TREE_OPERAND (r, 1);
10080 }
10081 if (!TREE_SIDE_EFFECTS (r)
10082 && !VOID_TYPE_P (TREE_TYPE (r))
10083 && !CONVERT_EXPR_P (r)
cc28fc7f 10084 && !TREE_NO_WARNING (r)
789eadcd
MP
10085 && !TREE_NO_WARNING (expr))
10086 warning_at (cloc, OPT_Wunused_value,
10087 "right-hand operand of comma expression has no effect");
10088 }
27f33b15 10089 else
c2255bc4 10090 warn_if_unused_value (expr, loc);
3a5b9284
RH
10091}
10092
506e2710 10093/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10094 diagnostics, but do not call ADD_STMT. LOC is the location of the
10095 statement. */
3a5b9284 10096
506e2710 10097tree
c2255bc4 10098c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10099{
056928b2
JJ
10100 tree exprv;
10101
3a5b9284 10102 if (!expr)
506e2710 10103 return NULL_TREE;
3a5b9284 10104
928c19bb
JM
10105 expr = c_fully_fold (expr, false, NULL);
10106
3a5b9284
RH
10107 if (warn_sequence_point)
10108 verify_sequence_points (expr);
10109
10110 if (TREE_TYPE (expr) != error_mark_node
10111 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10112 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10113 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10114
10115 /* If we're not processing a statement expression, warn about unused values.
10116 Warnings for statement expressions will be emitted later, once we figure
10117 out which is the result. */
10118 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10119 && warn_unused_value)
c2255bc4 10120 emit_side_effect_warnings (loc, expr);
3a5b9284 10121
056928b2
JJ
10122 exprv = expr;
10123 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10124 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10125 while (CONVERT_EXPR_P (exprv))
10126 exprv = TREE_OPERAND (exprv, 0);
10127 if (DECL_P (exprv)
10128 || handled_component_p (exprv)
10129 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10130 mark_exp_read (exprv);
fa8351f8 10131
3a5b9284
RH
10132 /* If the expression is not of a type to which we cannot assign a line
10133 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10134 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10135 {
10136 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10137 SET_EXPR_LOCATION (expr, loc);
10138 }
506e2710
RH
10139
10140 return expr;
10141}
10142
c2255bc4
AH
10143/* Emit an expression as a statement. LOC is the location of the
10144 expression. */
506e2710
RH
10145
10146tree
c2255bc4 10147c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10148{
10149 if (expr)
c2255bc4 10150 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10151 else
10152 return NULL;
3a5b9284
RH
10153}
10154
10155/* Do the opposite and emit a statement as an expression. To begin,
10156 create a new binding level and return it. */
325c3691
RH
10157
10158tree
10159c_begin_stmt_expr (void)
10160{
10161 tree ret;
10162
10163 /* We must force a BLOCK for this level so that, if it is not expanded
10164 later, there is a way to turn off the entire subtree of blocks that
10165 are contained in it. */
10166 keep_next_level ();
10167 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10168
10169 c_bindings_start_stmt_expr (c_switch_stack == NULL
10170 ? NULL
10171 : c_switch_stack->bindings);
325c3691
RH
10172
10173 /* Mark the current statement list as belonging to a statement list. */
10174 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10175
10176 return ret;
10177}
10178
c2255bc4
AH
10179/* LOC is the location of the compound statement to which this body
10180 belongs. */
10181
325c3691 10182tree
c2255bc4 10183c_finish_stmt_expr (location_t loc, tree body)
325c3691 10184{
3a5b9284 10185 tree last, type, tmp, val;
325c3691
RH
10186 tree *last_p;
10187
c2255bc4 10188 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10189
10190 c_bindings_end_stmt_expr (c_switch_stack == NULL
10191 ? NULL
10192 : c_switch_stack->bindings);
325c3691 10193
3a5b9284
RH
10194 /* Locate the last statement in BODY. See c_end_compound_stmt
10195 about always returning a BIND_EXPR. */
10196 last_p = &BIND_EXPR_BODY (body);
10197 last = BIND_EXPR_BODY (body);
10198
10199 continue_searching:
325c3691
RH
10200 if (TREE_CODE (last) == STATEMENT_LIST)
10201 {
3a5b9284
RH
10202 tree_stmt_iterator i;
10203
10204 /* This can happen with degenerate cases like ({ }). No value. */
10205 if (!TREE_SIDE_EFFECTS (last))
10206 return body;
10207
10208 /* If we're supposed to generate side effects warnings, process
10209 all of the statements except the last. */
27f33b15 10210 if (warn_unused_value)
325c3691 10211 {
3a5b9284 10212 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10213 {
10214 location_t tloc;
10215 tree t = tsi_stmt (i);
10216
10217 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10218 emit_side_effect_warnings (tloc, t);
10219 }
325c3691
RH
10220 }
10221 else
3a5b9284
RH
10222 i = tsi_last (last);
10223 last_p = tsi_stmt_ptr (i);
10224 last = *last_p;
325c3691
RH
10225 }
10226
3a5b9284
RH
10227 /* If the end of the list is exception related, then the list was split
10228 by a call to push_cleanup. Continue searching. */
10229 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10230 || TREE_CODE (last) == TRY_CATCH_EXPR)
10231 {
10232 last_p = &TREE_OPERAND (last, 0);
10233 last = *last_p;
10234 goto continue_searching;
10235 }
10236
26d8af35
JM
10237 if (last == error_mark_node)
10238 return last;
10239
3a5b9284
RH
10240 /* In the case that the BIND_EXPR is not necessary, return the
10241 expression out from inside it. */
26d8af35
JM
10242 if (last == BIND_EXPR_BODY (body)
10243 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10244 {
928c19bb
JM
10245 /* Even if this looks constant, do not allow it in a constant
10246 expression. */
e5a94231 10247 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10248 /* Do not warn if the return value of a statement expression is
10249 unused. */
928c19bb 10250 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10251 return last;
10252 }
325c3691
RH
10253
10254 /* Extract the type of said expression. */
10255 type = TREE_TYPE (last);
325c3691 10256
3a5b9284
RH
10257 /* If we're not returning a value at all, then the BIND_EXPR that
10258 we already have is a fine expression to return. */
10259 if (!type || VOID_TYPE_P (type))
10260 return body;
10261
10262 /* Now that we've located the expression containing the value, it seems
10263 silly to make voidify_wrapper_expr repeat the process. Create a
10264 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10265 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10266
10267 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10268 tree_expr_nonnegative_p giving up immediately. */
10269 val = last;
10270 if (TREE_CODE (val) == NOP_EXPR
10271 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10272 val = TREE_OPERAND (val, 0);
10273
53fb4de3 10274 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10275 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10276
c2255bc4
AH
10277 {
10278 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10279 SET_EXPR_LOCATION (t, loc);
10280 return t;
10281 }
325c3691
RH
10282}
10283\f
10284/* Begin and end compound statements. This is as simple as pushing
10285 and popping new statement lists from the tree. */
10286
10287tree
10288c_begin_compound_stmt (bool do_scope)
10289{
10290 tree stmt = push_stmt_list ();
10291 if (do_scope)
4dfa0342 10292 push_scope ();
325c3691
RH
10293 return stmt;
10294}
10295
c2255bc4
AH
10296/* End a compound statement. STMT is the statement. LOC is the
10297 location of the compound statement-- this is usually the location
10298 of the opening brace. */
10299
325c3691 10300tree
c2255bc4 10301c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10302{
10303 tree block = NULL;
10304
10305 if (do_scope)
10306 {
10307 if (c_dialect_objc ())
10308 objc_clear_super_receiver ();
10309 block = pop_scope ();
10310 }
10311
10312 stmt = pop_stmt_list (stmt);
c2255bc4 10313 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10314
10315 /* If this compound statement is nested immediately inside a statement
10316 expression, then force a BIND_EXPR to be created. Otherwise we'll
10317 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10318 STATEMENT_LISTs merge, and thus we can lose track of what statement
10319 was really last. */
38e01f9e 10320 if (building_stmt_list_p ()
325c3691
RH
10321 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10322 && TREE_CODE (stmt) != BIND_EXPR)
10323 {
53fb4de3 10324 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10325 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10326 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10327 }
10328
10329 return stmt;
10330}
5a508662
RH
10331
10332/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10333 when the current scope is exited. EH_ONLY is true when this is not
10334 meant to apply to normal control flow transfer. */
10335
10336void
c2255bc4 10337push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10338{
3a5b9284
RH
10339 enum tree_code code;
10340 tree stmt, list;
10341 bool stmt_expr;
10342
10343 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10344 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10345 add_stmt (stmt);
3a5b9284
RH
10346 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10347 list = push_stmt_list ();
10348 TREE_OPERAND (stmt, 0) = list;
10349 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10350}
325c3691 10351\f
9f47c7e5
IE
10352/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10353 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10354
10355static tree
10356build_vec_cmp (tree_code code, tree type,
10357 tree arg0, tree arg1)
10358{
10359 tree zero_vec = build_zero_cst (type);
10360 tree minus_one_vec = build_minus_one_cst (type);
10361 tree cmp_type = build_same_sized_truth_vector_type (type);
10362 tree cmp = build2 (code, cmp_type, arg0, arg1);
10363 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10364}
10365
3e4093b6
RS
10366/* Build a binary-operation expression without default conversions.
10367 CODE is the kind of expression to build.
ba47d38d 10368 LOCATION is the operator's location.
3e4093b6
RS
10369 This function differs from `build' in several ways:
10370 the data type of the result is computed and recorded in it,
10371 warnings are generated if arg data types are invalid,
10372 special handling for addition and subtraction of pointers is known,
10373 and some optimization is done (operations on narrow ints
10374 are done in the narrower type when that gives the same result).
10375 Constant folding is also done before the result is returned.
de520661 10376
3e4093b6
RS
10377 Note that the operands will never have enumeral types, or function
10378 or array types, because either they will have the default conversions
10379 performed or they have both just been converted to some other type in which
10380 the arithmetic is to be done. */
10381
10382tree
ba47d38d
AH
10383build_binary_op (location_t location, enum tree_code code,
10384 tree orig_op0, tree orig_op1, int convert_p)
de520661 10385{
8ce94e44
JM
10386 tree type0, type1, orig_type0, orig_type1;
10387 tree eptype;
3e4093b6
RS
10388 enum tree_code code0, code1;
10389 tree op0, op1;
c9f9eb5d 10390 tree ret = error_mark_node;
4de67c26 10391 const char *invalid_op_diag;
4d84fe7c 10392 bool op0_int_operands, op1_int_operands;
928c19bb 10393 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10394
3e4093b6
RS
10395 /* Expression code to give to the expression when it is built.
10396 Normally this is CODE, which is what the caller asked for,
10397 but in some special cases we change it. */
10398 enum tree_code resultcode = code;
8b6a5902 10399
3e4093b6
RS
10400 /* Data type in which the computation is to be performed.
10401 In the simplest cases this is the common type of the arguments. */
10402 tree result_type = NULL;
10403
8ce94e44
JM
10404 /* When the computation is in excess precision, the type of the
10405 final EXCESS_PRECISION_EXPR. */
2d2e923f 10406 tree semantic_result_type = NULL;
8ce94e44 10407
3e4093b6
RS
10408 /* Nonzero means operands have already been type-converted
10409 in whatever way is necessary.
10410 Zero means they need to be converted to RESULT_TYPE. */
10411 int converted = 0;
10412
10413 /* Nonzero means create the expression with this type, rather than
10414 RESULT_TYPE. */
10415 tree build_type = 0;
10416
10417 /* Nonzero means after finally constructing the expression
10418 convert it to this type. */
10419 tree final_type = 0;
10420
10421 /* Nonzero if this is an operation like MIN or MAX which can
10422 safely be computed in short if both args are promoted shorts.
10423 Also implies COMMON.
10424 -1 indicates a bitwise operation; this makes a difference
10425 in the exact conditions for when it is safe to do the operation
10426 in a narrower mode. */
10427 int shorten = 0;
10428
10429 /* Nonzero if this is a comparison operation;
10430 if both args are promoted shorts, compare the original shorts.
10431 Also implies COMMON. */
10432 int short_compare = 0;
10433
10434 /* Nonzero if this is a right-shift operation, which can be computed on the
10435 original short and then promoted if the operand is a promoted short. */
10436 int short_shift = 0;
10437
10438 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10439 int common = 0;
10440
58393038
ZL
10441 /* True means types are compatible as far as ObjC is concerned. */
10442 bool objc_ok;
10443
8ce94e44
JM
10444 /* True means this is an arithmetic operation that may need excess
10445 precision. */
10446 bool may_need_excess_precision;
10447
180f8dbb
JM
10448 /* True means this is a boolean operation that converts both its
10449 operands to truth-values. */
10450 bool boolean_op = false;
10451
de5a5fa1
MP
10452 /* Remember whether we're doing / or %. */
10453 bool doing_div_or_mod = false;
10454
10455 /* Remember whether we're doing << or >>. */
10456 bool doing_shift = false;
10457
10458 /* Tree holding instrumentation expression. */
10459 tree instrument_expr = NULL;
10460
ba47d38d
AH
10461 if (location == UNKNOWN_LOCATION)
10462 location = input_location;
10463
4d84fe7c
JM
10464 op0 = orig_op0;
10465 op1 = orig_op1;
10466
10467 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10468 if (op0_int_operands)
10469 op0 = remove_c_maybe_const_expr (op0);
10470 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10471 if (op1_int_operands)
10472 op1 = remove_c_maybe_const_expr (op1);
10473 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10474 if (int_operands)
10475 {
10476 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10477 && TREE_CODE (orig_op1) == INTEGER_CST);
10478 int_const = (int_const_or_overflow
10479 && !TREE_OVERFLOW (orig_op0)
10480 && !TREE_OVERFLOW (orig_op1));
10481 }
10482 else
10483 int_const = int_const_or_overflow = false;
10484
0e3a99ae 10485 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10486 if (convert_p
31521951 10487 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10488 {
4d84fe7c
JM
10489 op0 = default_conversion (op0);
10490 op1 = default_conversion (op1);
790e9490
RS
10491 }
10492
36536d79
BI
10493 /* When Cilk Plus is enabled and there are array notations inside op0, then
10494 we check to see if there are builtin array notation functions. If
10495 so, then we take on the type of the array notation inside it. */
b72271b9 10496 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10497 orig_type0 = type0 = find_correct_array_notation_type (op0);
10498 else
10499 orig_type0 = type0 = TREE_TYPE (op0);
10500
b72271b9 10501 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10502 orig_type1 = type1 = find_correct_array_notation_type (op1);
10503 else
10504 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10505
3e4093b6
RS
10506 /* The expression codes of the data types of the arguments tell us
10507 whether the arguments are integers, floating, pointers, etc. */
10508 code0 = TREE_CODE (type0);
10509 code1 = TREE_CODE (type1);
10510
10511 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10512 STRIP_TYPE_NOPS (op0);
10513 STRIP_TYPE_NOPS (op1);
10514
10515 /* If an error was already reported for one of the arguments,
10516 avoid reporting another error. */
10517
10518 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10519 return error_mark_node;
10520
1807ffc1
MS
10521 if (code0 == POINTER_TYPE
10522 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10523 return error_mark_node;
10524
10525 if (code1 == POINTER_TYPE
10526 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10527 return error_mark_node;
10528
4de67c26
JM
10529 if ((invalid_op_diag
10530 = targetm.invalid_binary_op (code, type0, type1)))
10531 {
ba47d38d 10532 error_at (location, invalid_op_diag);
4de67c26
JM
10533 return error_mark_node;
10534 }
10535
8ce94e44
JM
10536 switch (code)
10537 {
10538 case PLUS_EXPR:
10539 case MINUS_EXPR:
10540 case MULT_EXPR:
10541 case TRUNC_DIV_EXPR:
10542 case CEIL_DIV_EXPR:
10543 case FLOOR_DIV_EXPR:
10544 case ROUND_DIV_EXPR:
10545 case EXACT_DIV_EXPR:
10546 may_need_excess_precision = true;
10547 break;
10548 default:
10549 may_need_excess_precision = false;
10550 break;
10551 }
10552 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10553 {
10554 op0 = TREE_OPERAND (op0, 0);
10555 type0 = TREE_TYPE (op0);
10556 }
10557 else if (may_need_excess_precision
10558 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10559 {
10560 type0 = eptype;
10561 op0 = convert (eptype, op0);
10562 }
10563 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10564 {
10565 op1 = TREE_OPERAND (op1, 0);
10566 type1 = TREE_TYPE (op1);
10567 }
10568 else if (may_need_excess_precision
10569 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10570 {
10571 type1 = eptype;
10572 op1 = convert (eptype, op1);
10573 }
10574
58393038
ZL
10575 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10576
0e3a99ae
AS
10577 /* In case when one of the operands of the binary operation is
10578 a vector and another is a scalar -- convert scalar to vector. */
10579 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10580 {
a212e43f
MG
10581 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10582 true);
f90e8e2e 10583
0e3a99ae
AS
10584 switch (convert_flag)
10585 {
10586 case stv_error:
10587 return error_mark_node;
10588 case stv_firstarg:
10589 {
10590 bool maybe_const = true;
10591 tree sc;
10592 sc = c_fully_fold (op0, false, &maybe_const);
10593 sc = save_expr (sc);
10594 sc = convert (TREE_TYPE (type1), sc);
10595 op0 = build_vector_from_val (type1, sc);
10596 if (!maybe_const)
10597 op0 = c_wrap_maybe_const (op0, true);
10598 orig_type0 = type0 = TREE_TYPE (op0);
10599 code0 = TREE_CODE (type0);
10600 converted = 1;
10601 break;
10602 }
10603 case stv_secondarg:
10604 {
10605 bool maybe_const = true;
10606 tree sc;
10607 sc = c_fully_fold (op1, false, &maybe_const);
10608 sc = save_expr (sc);
10609 sc = convert (TREE_TYPE (type0), sc);
10610 op1 = build_vector_from_val (type0, sc);
10611 if (!maybe_const)
54b9f838 10612 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10613 orig_type1 = type1 = TREE_TYPE (op1);
10614 code1 = TREE_CODE (type1);
10615 converted = 1;
10616 break;
10617 }
10618 default:
10619 break;
10620 }
10621 }
10622
3e4093b6 10623 switch (code)
de520661 10624 {
3e4093b6
RS
10625 case PLUS_EXPR:
10626 /* Handle the pointer + int case. */
10627 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10628 {
db3927fb 10629 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10630 goto return_build_binary_op;
10631 }
3e4093b6 10632 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10633 {
db3927fb 10634 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10635 goto return_build_binary_op;
10636 }
fe67cf58 10637 else
3e4093b6
RS
10638 common = 1;
10639 break;
400fbf9f 10640
3e4093b6
RS
10641 case MINUS_EXPR:
10642 /* Subtraction of two similar pointers.
10643 We must subtract them as integers, then divide by object size. */
10644 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10645 && comp_target_types (location, type0, type1))
c9f9eb5d 10646 {
db3927fb 10647 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10648 goto return_build_binary_op;
10649 }
3e4093b6
RS
10650 /* Handle pointer minus int. Just like pointer plus int. */
10651 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10652 {
db3927fb 10653 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10654 goto return_build_binary_op;
10655 }
3e4093b6
RS
10656 else
10657 common = 1;
10658 break;
8b6a5902 10659
3e4093b6
RS
10660 case MULT_EXPR:
10661 common = 1;
10662 break;
10663
10664 case TRUNC_DIV_EXPR:
10665 case CEIL_DIV_EXPR:
10666 case FLOOR_DIV_EXPR:
10667 case ROUND_DIV_EXPR:
10668 case EXACT_DIV_EXPR:
de5a5fa1 10669 doing_div_or_mod = true;
c9f9eb5d 10670 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10671
10672 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10673 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10674 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10675 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10676 || code1 == FIXED_POINT_TYPE
3e4093b6 10677 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10678 {
5bed876a
AH
10679 enum tree_code tcode0 = code0, tcode1 = code1;
10680
3a021db2 10681 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10682 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10683 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10684 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10685
ab22c1fa
CF
10686 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10687 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10688 resultcode = RDIV_EXPR;
10689 else
10690 /* Although it would be tempting to shorten always here, that
10691 loses on some targets, since the modulo instruction is
10692 undefined if the quotient can't be represented in the
10693 computation mode. We shorten only if unsigned or if
10694 dividing by something we know != -1. */
8df83eae 10695 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10696 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10697 && !integer_all_onesp (op1)));
3e4093b6
RS
10698 common = 1;
10699 }
10700 break;
de520661 10701
3e4093b6 10702 case BIT_AND_EXPR:
3e4093b6
RS
10703 case BIT_IOR_EXPR:
10704 case BIT_XOR_EXPR:
10705 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10706 shorten = -1;
9ef0c8d9
AP
10707 /* Allow vector types which are not floating point types. */
10708 else if (code0 == VECTOR_TYPE
10709 && code1 == VECTOR_TYPE
10710 && !VECTOR_FLOAT_TYPE_P (type0)
10711 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10712 common = 1;
10713 break;
10714
10715 case TRUNC_MOD_EXPR:
10716 case FLOOR_MOD_EXPR:
de5a5fa1 10717 doing_div_or_mod = true;
c9f9eb5d 10718 warn_for_div_by_zero (location, op1);
de520661 10719
5cfd5d9b
AP
10720 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10721 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10722 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10723 common = 1;
10724 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10725 {
10726 /* Although it would be tempting to shorten always here, that loses
10727 on some targets, since the modulo instruction is undefined if the
10728 quotient can't be represented in the computation mode. We shorten
10729 only if unsigned or if dividing by something we know != -1. */
8df83eae 10730 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10731 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10732 && !integer_all_onesp (op1)));
3e4093b6
RS
10733 common = 1;
10734 }
10735 break;
de520661 10736
3e4093b6
RS
10737 case TRUTH_ANDIF_EXPR:
10738 case TRUTH_ORIF_EXPR:
10739 case TRUTH_AND_EXPR:
10740 case TRUTH_OR_EXPR:
10741 case TRUTH_XOR_EXPR:
10742 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10743 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10744 || code0 == FIXED_POINT_TYPE)
3e4093b6 10745 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10746 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10747 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10748 {
10749 /* Result of these operations is always an int,
10750 but that does not mean the operands should be
10751 converted to ints! */
10752 result_type = integer_type_node;
a27d595d
JM
10753 if (op0_int_operands)
10754 {
10755 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10756 op0 = remove_c_maybe_const_expr (op0);
10757 }
10758 else
10759 op0 = c_objc_common_truthvalue_conversion (location, op0);
10760 if (op1_int_operands)
10761 {
10762 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10763 op1 = remove_c_maybe_const_expr (op1);
10764 }
10765 else
10766 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10767 converted = 1;
180f8dbb 10768 boolean_op = true;
3e4093b6 10769 }
928c19bb
JM
10770 if (code == TRUTH_ANDIF_EXPR)
10771 {
10772 int_const_or_overflow = (int_operands
10773 && TREE_CODE (orig_op0) == INTEGER_CST
10774 && (op0 == truthvalue_false_node
10775 || TREE_CODE (orig_op1) == INTEGER_CST));
10776 int_const = (int_const_or_overflow
10777 && !TREE_OVERFLOW (orig_op0)
10778 && (op0 == truthvalue_false_node
10779 || !TREE_OVERFLOW (orig_op1)));
10780 }
10781 else if (code == TRUTH_ORIF_EXPR)
10782 {
10783 int_const_or_overflow = (int_operands
10784 && TREE_CODE (orig_op0) == INTEGER_CST
10785 && (op0 == truthvalue_true_node
10786 || TREE_CODE (orig_op1) == INTEGER_CST));
10787 int_const = (int_const_or_overflow
10788 && !TREE_OVERFLOW (orig_op0)
10789 && (op0 == truthvalue_true_node
10790 || !TREE_OVERFLOW (orig_op1)));
10791 }
3e4093b6 10792 break;
eba80994 10793
3e4093b6
RS
10794 /* Shift operations: result has same type as first operand;
10795 always convert second operand to int.
10796 Also set SHORT_SHIFT if shifting rightward. */
de520661 10797
3e4093b6 10798 case RSHIFT_EXPR:
f87bd04b
AS
10799 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10800 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10801 {
10802 result_type = type0;
10803 converted = 1;
10804 }
10805 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10806 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10807 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10808 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10809 {
10810 result_type = type0;
10811 converted = 1;
10812 }
10813 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10814 && code1 == INTEGER_TYPE)
3e4093b6 10815 {
de5a5fa1 10816 doing_shift = true;
928c19bb 10817 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10818 {
3e4093b6 10819 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10820 {
10821 int_const = false;
7d882b83 10822 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10823 warning_at (location, OPT_Wshift_count_negative,
10824 "right shift count is negative");
928c19bb 10825 }
3e4093b6 10826 else
bbb818c6 10827 {
3f75a254 10828 if (!integer_zerop (op1))
3e4093b6
RS
10829 short_shift = 1;
10830
10831 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10832 {
10833 int_const = false;
7d882b83 10834 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10835 warning_at (location, OPT_Wshift_count_overflow,
10836 "right shift count >= width of type");
928c19bb 10837 }
bbb818c6 10838 }
b62acd60 10839 }
de520661 10840
3e4093b6
RS
10841 /* Use the type of the value to be shifted. */
10842 result_type = type0;
3e4093b6
RS
10843 /* Avoid converting op1 to result_type later. */
10844 converted = 1;
400fbf9f 10845 }
3e4093b6 10846 break;
253b6b82 10847
3e4093b6 10848 case LSHIFT_EXPR:
f87bd04b
AS
10849 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10850 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10851 {
10852 result_type = type0;
10853 converted = 1;
10854 }
10855 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10856 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10857 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10858 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10859 {
10860 result_type = type0;
10861 converted = 1;
10862 }
10863 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10864 && code1 == INTEGER_TYPE)
3e4093b6 10865 {
de5a5fa1 10866 doing_shift = true;
0173bd2a
MP
10867 if (TREE_CODE (op0) == INTEGER_CST
10868 && tree_int_cst_sgn (op0) < 0)
10869 {
10870 /* Don't reject a left shift of a negative value in a context
10871 where a constant expression is needed in C90. */
10872 if (flag_isoc99)
10873 int_const = false;
10874 if (c_inhibit_evaluation_warnings == 0)
10875 warning_at (location, OPT_Wshift_negative_value,
10876 "left shift of negative value");
10877 }
928c19bb 10878 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10879 {
3e4093b6 10880 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10881 {
10882 int_const = false;
7d882b83 10883 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10884 warning_at (location, OPT_Wshift_count_negative,
10885 "left shift count is negative");
928c19bb 10886 }
3e4093b6 10887 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10888 {
10889 int_const = false;
7d882b83 10890 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10891 warning_at (location, OPT_Wshift_count_overflow,
10892 "left shift count >= width of type");
928c19bb 10893 }
451b5e48
MP
10894 else if (TREE_CODE (op0) == INTEGER_CST
10895 && maybe_warn_shift_overflow (location, op0, op1)
10896 && flag_isoc99)
10897 int_const = false;
94ba5069 10898 }
de520661 10899
3e4093b6
RS
10900 /* Use the type of the value to be shifted. */
10901 result_type = type0;
3e4093b6
RS
10902 /* Avoid converting op1 to result_type later. */
10903 converted = 1;
400fbf9f 10904 }
3e4093b6 10905 break;
de520661 10906
3e4093b6
RS
10907 case EQ_EXPR:
10908 case NE_EXPR:
d246ab4f
AS
10909 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10910 {
10911 tree intt;
0af94e6f 10912 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10913 {
10914 error_at (location, "comparing vectors with different "
10915 "element types");
10916 return error_mark_node;
10917 }
10918
10919 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10920 {
10921 error_at (location, "comparing vectors with different "
10922 "number of elements");
10923 return error_mark_node;
10924 }
10925
10926 /* Always construct signed integer vector type. */
10927 intt = c_common_type_for_size (GET_MODE_BITSIZE
10928 (TYPE_MODE (TREE_TYPE (type0))), 0);
10929 result_type = build_opaque_vector_type (intt,
10930 TYPE_VECTOR_SUBPARTS (type0));
10931 converted = 1;
9f47c7e5
IE
10932 ret = build_vec_cmp (resultcode, result_type, op0, op1);
10933 goto return_build_binary_op;
d246ab4f 10934 }
ae311566 10935 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10936 warning_at (location,
10937 OPT_Wfloat_equal,
10938 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10939 /* Result of comparison is always int,
10940 but don't convert the args to int! */
10941 build_type = integer_type_node;
10942 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10943 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10944 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10945 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10946 short_compare = 1;
637f1455
SZ
10947 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10948 {
dbb68221
MW
10949 if (warn_nonnull
10950 && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0))
10951 warning_at (location, OPT_Wnonnull,
10952 "nonnull argument %qD compared to NULL", op0);
10953
637f1455
SZ
10954 if (TREE_CODE (op0) == ADDR_EXPR
10955 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10956 {
10957 if (code == EQ_EXPR)
10958 warning_at (location,
10959 OPT_Waddress,
10960 "the comparison will always evaluate as %<false%> "
10961 "for the address of %qD will never be NULL",
10962 TREE_OPERAND (op0, 0));
10963 else
10964 warning_at (location,
10965 OPT_Waddress,
10966 "the comparison will always evaluate as %<true%> "
10967 "for the address of %qD will never be NULL",
10968 TREE_OPERAND (op0, 0));
10969 }
10970 result_type = type0;
10971 }
10972 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10973 {
dbb68221
MW
10974 if (warn_nonnull
10975 && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1))
10976 warning_at (location, OPT_Wnonnull,
10977 "nonnull argument %qD compared to NULL", op1);
10978
637f1455
SZ
10979 if (TREE_CODE (op1) == ADDR_EXPR
10980 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10981 {
10982 if (code == EQ_EXPR)
10983 warning_at (location,
f90e8e2e 10984 OPT_Waddress,
637f1455
SZ
10985 "the comparison will always evaluate as %<false%> "
10986 "for the address of %qD will never be NULL",
10987 TREE_OPERAND (op1, 0));
10988 else
10989 warning_at (location,
10990 OPT_Waddress,
10991 "the comparison will always evaluate as %<true%> "
10992 "for the address of %qD will never be NULL",
10993 TREE_OPERAND (op1, 0));
10994 }
10995 result_type = type1;
10996 }
3e4093b6
RS
10997 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10998 {
10999 tree tt0 = TREE_TYPE (type0);
11000 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11001 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11002 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11003 addr_space_t as_common = ADDR_SPACE_GENERIC;
11004
3e4093b6
RS
11005 /* Anything compares with void *. void * compares with anything.
11006 Otherwise, the targets must be compatible
11007 and both must be object or both incomplete. */
744aa42f 11008 if (comp_target_types (location, type0, type1))
10bc1b1b 11009 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11010 else if (!addr_space_superset (as0, as1, &as_common))
11011 {
11012 error_at (location, "comparison of pointers to "
11013 "disjoint address spaces");
11014 return error_mark_node;
11015 }
267bac10 11016 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11017 {
36c5e70a 11018 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11019 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11020 "comparison of %<void *%> with function pointer");
ee2990e7 11021 }
267bac10 11022 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11023 {
36c5e70a 11024 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11025 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11026 "comparison of %<void *%> with function pointer");
e6834654 11027 }
3e4093b6 11028 else
58393038
ZL
11029 /* Avoid warning about the volatile ObjC EH puts on decls. */
11030 if (!objc_ok)
ba47d38d 11031 pedwarn (location, 0,
509c9d60 11032 "comparison of distinct pointer types lacks a cast");
e6834654 11033
3e4093b6 11034 if (result_type == NULL_TREE)
36c5e70a
BE
11035 {
11036 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11037 result_type = build_pointer_type
11038 (build_qualified_type (void_type_node, qual));
11039 }
e6834654 11040 }
3e4093b6 11041 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11042 {
3e4093b6 11043 result_type = type0;
ba47d38d 11044 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11045 }
3e4093b6 11046 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11047 {
3e4093b6 11048 result_type = type1;
ba47d38d 11049 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11050 }
04159acf
MP
11051 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11052 || truth_value_p (TREE_CODE (orig_op0)))
11053 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11054 || truth_value_p (TREE_CODE (orig_op1))))
11055 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11056 break;
8b6a5902 11057
3e4093b6
RS
11058 case LE_EXPR:
11059 case GE_EXPR:
11060 case LT_EXPR:
11061 case GT_EXPR:
d246ab4f
AS
11062 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11063 {
11064 tree intt;
0af94e6f 11065 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11066 {
11067 error_at (location, "comparing vectors with different "
11068 "element types");
11069 return error_mark_node;
11070 }
11071
11072 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11073 {
11074 error_at (location, "comparing vectors with different "
11075 "number of elements");
11076 return error_mark_node;
11077 }
11078
11079 /* Always construct signed integer vector type. */
11080 intt = c_common_type_for_size (GET_MODE_BITSIZE
11081 (TYPE_MODE (TREE_TYPE (type0))), 0);
11082 result_type = build_opaque_vector_type (intt,
11083 TYPE_VECTOR_SUBPARTS (type0));
11084 converted = 1;
9f47c7e5
IE
11085 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11086 goto return_build_binary_op;
d246ab4f 11087 }
3e4093b6 11088 build_type = integer_type_node;
ab22c1fa
CF
11089 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11090 || code0 == FIXED_POINT_TYPE)
11091 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11092 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11093 short_compare = 1;
11094 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11095 {
36c5e70a
BE
11096 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11097 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11098 addr_space_t as_common;
11099
744aa42f 11100 if (comp_target_types (location, type0, type1))
3e4093b6 11101 {
10bc1b1b 11102 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11103 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11104 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11105 pedwarn (location, 0,
509c9d60 11106 "comparison of complete and incomplete pointers");
fcf73884 11107 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11108 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11109 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11110 else if (null_pointer_constant_p (orig_op0)
11111 || null_pointer_constant_p (orig_op1))
11112 warning_at (location, OPT_Wextra,
11113 "ordered comparison of pointer with null pointer");
11114
3e4093b6 11115 }
36c5e70a
BE
11116 else if (!addr_space_superset (as0, as1, &as_common))
11117 {
11118 error_at (location, "comparison of pointers to "
11119 "disjoint address spaces");
11120 return error_mark_node;
11121 }
3e4093b6
RS
11122 else
11123 {
36c5e70a
BE
11124 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11125 result_type = build_pointer_type
11126 (build_qualified_type (void_type_node, qual));
ba47d38d 11127 pedwarn (location, 0,
509c9d60 11128 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11129 }
11130 }
6aa3c60d 11131 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11132 {
11133 result_type = type0;
fcf73884 11134 if (pedantic)
c1771a20 11135 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11136 "ordered comparison of pointer with integer zero");
11137 else if (extra_warnings)
ba47d38d 11138 warning_at (location, OPT_Wextra,
d42ba3b8 11139 "ordered comparison of pointer with integer zero");
3e4093b6 11140 }
6aa3c60d 11141 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11142 {
11143 result_type = type1;
d42ba3b8 11144 if (pedantic)
c1771a20 11145 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11146 "ordered comparison of pointer with integer zero");
11147 else if (extra_warnings)
11148 warning_at (location, OPT_Wextra,
11149 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11150 }
11151 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11152 {
11153 result_type = type0;
ba47d38d 11154 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11155 }
11156 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11157 {
11158 result_type = type1;
ba47d38d 11159 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11160 }
04159acf
MP
11161 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11162 || truth_value_p (TREE_CODE (orig_op0)))
11163 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11164 || truth_value_p (TREE_CODE (orig_op1))))
11165 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11166 break;
64094f6a 11167
3e4093b6 11168 default:
37b2f290 11169 gcc_unreachable ();
c9fe6f9f 11170 }
8f17b5c5 11171
e57e265b
PB
11172 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11173 return error_mark_node;
11174
5bed876a
AH
11175 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11176 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11177 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11178 {
ba47d38d 11179 binary_op_error (location, code, type0, type1);
5bed876a
AH
11180 return error_mark_node;
11181 }
11182
3e4093b6 11183 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11184 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11185 &&
11186 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11187 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11188 {
2ca862e9
JM
11189 bool first_complex = (code0 == COMPLEX_TYPE);
11190 bool second_complex = (code1 == COMPLEX_TYPE);
11191 int none_complex = (!first_complex && !second_complex);
39b726dd 11192
3e4093b6 11193 if (shorten || common || short_compare)
3bf6bfcc
JJ
11194 {
11195 result_type = c_common_type (type0, type1);
0a0b3574
MM
11196 do_warn_double_promotion (result_type, type0, type1,
11197 "implicit conversion from %qT to %qT "
11198 "to match other operand of binary "
11199 "expression",
11200 location);
3bf6bfcc
JJ
11201 if (result_type == error_mark_node)
11202 return error_mark_node;
11203 }
400fbf9f 11204
2ca862e9
JM
11205 if (first_complex != second_complex
11206 && (code == PLUS_EXPR
11207 || code == MINUS_EXPR
11208 || code == MULT_EXPR
11209 || (code == TRUNC_DIV_EXPR && first_complex))
11210 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11211 && flag_signed_zeros)
11212 {
11213 /* An operation on mixed real/complex operands must be
11214 handled specially, but the language-independent code can
11215 more easily optimize the plain complex arithmetic if
11216 -fno-signed-zeros. */
11217 tree real_type = TREE_TYPE (result_type);
11218 tree real, imag;
11219 if (type0 != orig_type0 || type1 != orig_type1)
11220 {
11221 gcc_assert (may_need_excess_precision && common);
2d2e923f 11222 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11223 }
11224 if (first_complex)
11225 {
11226 if (TREE_TYPE (op0) != result_type)
68fca595 11227 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11228 if (TREE_TYPE (op1) != real_type)
68fca595 11229 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11230 }
11231 else
11232 {
11233 if (TREE_TYPE (op0) != real_type)
68fca595 11234 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11235 if (TREE_TYPE (op1) != result_type)
68fca595 11236 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11237 }
11238 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11239 return error_mark_node;
11240 if (first_complex)
11241 {
11242 op0 = c_save_expr (op0);
11243 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11244 op0, 1);
11245 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11246 op0, 1);
11247 switch (code)
11248 {
11249 case MULT_EXPR:
11250 case TRUNC_DIV_EXPR:
c4603e7c 11251 op1 = c_save_expr (op1);
2ca862e9
JM
11252 imag = build2 (resultcode, real_type, imag, op1);
11253 /* Fall through. */
11254 case PLUS_EXPR:
11255 case MINUS_EXPR:
11256 real = build2 (resultcode, real_type, real, op1);
11257 break;
11258 default:
11259 gcc_unreachable();
11260 }
11261 }
11262 else
11263 {
11264 op1 = c_save_expr (op1);
11265 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11266 op1, 1);
11267 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11268 op1, 1);
11269 switch (code)
11270 {
11271 case MULT_EXPR:
c4603e7c 11272 op0 = c_save_expr (op0);
2ca862e9
JM
11273 imag = build2 (resultcode, real_type, op0, imag);
11274 /* Fall through. */
11275 case PLUS_EXPR:
11276 real = build2 (resultcode, real_type, op0, real);
11277 break;
11278 case MINUS_EXPR:
11279 real = build2 (resultcode, real_type, op0, real);
11280 imag = build1 (NEGATE_EXPR, real_type, imag);
11281 break;
11282 default:
11283 gcc_unreachable();
11284 }
11285 }
11286 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11287 goto return_build_binary_op;
11288 }
11289
3e4093b6
RS
11290 /* For certain operations (which identify themselves by shorten != 0)
11291 if both args were extended from the same smaller type,
11292 do the arithmetic in that type and then extend.
400fbf9f 11293
3e4093b6
RS
11294 shorten !=0 and !=1 indicates a bitwise operation.
11295 For them, this optimization is safe only if
11296 both args are zero-extended or both are sign-extended.
11297 Otherwise, we might change the result.
11298 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11299 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11300
3e4093b6
RS
11301 if (shorten && none_complex)
11302 {
3e4093b6 11303 final_type = result_type;
b8698a0f 11304 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11305 shorten == -1);
3e4093b6 11306 }
88a3dbc1 11307
3e4093b6 11308 /* Shifts can be shortened if shifting right. */
2f6e4e97 11309
3e4093b6
RS
11310 if (short_shift)
11311 {
11312 int unsigned_arg;
11313 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11314
3e4093b6 11315 final_type = result_type;
abe80e6d 11316
3e4093b6 11317 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11318 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11319
3e4093b6 11320 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11321 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11322 /* We can shorten only if the shift count is less than the
11323 number of bits in the smaller type size. */
11324 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11325 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11326 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11327 {
11328 /* Do an unsigned shift if the operand was zero-extended. */
11329 result_type
11330 = c_common_signed_or_unsigned_type (unsigned_arg,
11331 TREE_TYPE (arg0));
11332 /* Convert value-to-be-shifted to that type. */
11333 if (TREE_TYPE (op0) != result_type)
11334 op0 = convert (result_type, op0);
11335 converted = 1;
abe80e6d 11336 }
88a3dbc1
RK
11337 }
11338
3e4093b6
RS
11339 /* Comparison operations are shortened too but differently.
11340 They identify themselves by setting short_compare = 1. */
56cb9733 11341
3e4093b6
RS
11342 if (short_compare)
11343 {
11344 /* Don't write &op0, etc., because that would prevent op0
11345 from being kept in a register.
11346 Instead, make copies of the our local variables and
11347 pass the copies by reference, then copy them back afterward. */
11348 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11349 enum tree_code xresultcode = resultcode;
11350 tree val
393e8e8b
MP
11351 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11352 &xresultcode);
8f17b5c5 11353
3e4093b6 11354 if (val != 0)
c9f9eb5d
AH
11355 {
11356 ret = val;
11357 goto return_build_binary_op;
11358 }
8f17b5c5 11359
3e4093b6
RS
11360 op0 = xop0, op1 = xop1;
11361 converted = 1;
11362 resultcode = xresultcode;
8f17b5c5 11363
7d882b83 11364 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11365 {
11366 bool op0_maybe_const = true;
11367 bool op1_maybe_const = true;
11368 tree orig_op0_folded, orig_op1_folded;
11369
11370 if (in_late_binary_op)
11371 {
11372 orig_op0_folded = orig_op0;
11373 orig_op1_folded = orig_op1;
11374 }
11375 else
11376 {
11377 /* Fold for the sake of possible warnings, as in
11378 build_conditional_expr. This requires the
11379 "original" values to be folded, not just op0 and
11380 op1. */
f5178456 11381 c_inhibit_evaluation_warnings++;
928c19bb
JM
11382 op0 = c_fully_fold (op0, require_constant_value,
11383 &op0_maybe_const);
11384 op1 = c_fully_fold (op1, require_constant_value,
11385 &op1_maybe_const);
f5178456 11386 c_inhibit_evaluation_warnings--;
928c19bb
JM
11387 orig_op0_folded = c_fully_fold (orig_op0,
11388 require_constant_value,
11389 NULL);
11390 orig_op1_folded = c_fully_fold (orig_op1,
11391 require_constant_value,
11392 NULL);
11393 }
11394
11395 if (warn_sign_compare)
11396 warn_for_sign_compare (location, orig_op0_folded,
11397 orig_op1_folded, op0, op1,
11398 result_type, resultcode);
5c2f94b4 11399 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11400 {
11401 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11402 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11403 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11404 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11405 }
3e4093b6 11406 }
2ad1815d 11407 }
64094f6a 11408 }
64094f6a 11409
3e4093b6
RS
11410 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11411 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11412 Then the expression will be built.
11413 It will be given type FINAL_TYPE if that is nonzero;
11414 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11415
3e4093b6
RS
11416 if (!result_type)
11417 {
ba47d38d 11418 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11419 return error_mark_node;
11420 }
400fbf9f 11421
3e4093b6 11422 if (build_type == NULL_TREE)
8ce94e44
JM
11423 {
11424 build_type = result_type;
180f8dbb
JM
11425 if ((type0 != orig_type0 || type1 != orig_type1)
11426 && !boolean_op)
8ce94e44
JM
11427 {
11428 gcc_assert (may_need_excess_precision && common);
2d2e923f 11429 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11430 }
11431 }
400fbf9f 11432
2d2e923f
MLI
11433 if (!converted)
11434 {
68fca595
MP
11435 op0 = ep_convert_and_check (location, result_type, op0,
11436 semantic_result_type);
11437 op1 = ep_convert_and_check (location, result_type, op1,
11438 semantic_result_type);
2d2e923f
MLI
11439
11440 /* This can happen if one operand has a vector type, and the other
11441 has a different type. */
11442 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11443 return error_mark_node;
11444 }
11445
f8ed5150
MP
11446 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11447 | SANITIZE_FLOAT_DIVIDE))
802ac282 11448 && do_ubsan_in_current_function ()
b2aaf235
MP
11449 && (doing_div_or_mod || doing_shift)
11450 && !require_constant_value)
de5a5fa1
MP
11451 {
11452 /* OP0 and/or OP1 might have side-effects. */
11453 op0 = c_save_expr (op0);
11454 op1 = c_save_expr (op1);
11455 op0 = c_fully_fold (op0, false, NULL);
11456 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11457 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11458 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11459 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11460 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11461 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11462 }
11463
c9f9eb5d 11464 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11465 if (int_const_or_overflow)
11466 ret = (require_constant_value
db3927fb
AH
11467 ? fold_build2_initializer_loc (location, resultcode, build_type,
11468 op0, op1)
11469 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11470 else
11471 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11472 if (final_type != 0)
11473 ret = convert (final_type, ret);
11474
11475 return_build_binary_op:
11476 gcc_assert (ret != error_mark_node);
928c19bb
JM
11477 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11478 ret = (int_operands
11479 ? note_integer_operands (ret)
11480 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11481 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11482 && !in_late_binary_op)
11483 ret = note_integer_operands (ret);
2d2e923f
MLI
11484 if (semantic_result_type)
11485 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11486 protected_set_expr_location (ret, location);
de5a5fa1 11487
a24d975c 11488 if (instrument_expr != NULL)
de5a5fa1
MP
11489 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11490 instrument_expr, ret);
11491
c9f9eb5d 11492 return ret;
400fbf9f 11493}
85498824
JM
11494
11495
11496/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11497 purpose. LOCATION is the source location for the expression. */
85498824
JM
11498
11499tree
ba47d38d 11500c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11501{
928c19bb
JM
11502 bool int_const, int_operands;
11503
85498824
JM
11504 switch (TREE_CODE (TREE_TYPE (expr)))
11505 {
11506 case ARRAY_TYPE:
ba47d38d 11507 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11508 return error_mark_node;
11509
11510 case RECORD_TYPE:
ba47d38d 11511 error_at (location, "used struct type value where scalar is required");
85498824
JM
11512 return error_mark_node;
11513
11514 case UNION_TYPE:
ba47d38d 11515 error_at (location, "used union type value where scalar is required");
85498824
JM
11516 return error_mark_node;
11517
04af8788
NP
11518 case VOID_TYPE:
11519 error_at (location, "void value not ignored as it ought to be");
11520 return error_mark_node;
11521
1807ffc1
MS
11522 case POINTER_TYPE:
11523 if (reject_gcc_builtin (expr))
11524 return error_mark_node;
11525 break;
11526
46bdb9cf
JM
11527 case FUNCTION_TYPE:
11528 gcc_unreachable ();
11529
d246ab4f
AS
11530 case VECTOR_TYPE:
11531 error_at (location, "used vector type where scalar is required");
11532 return error_mark_node;
11533
85498824
JM
11534 default:
11535 break;
11536 }
11537
928c19bb
JM
11538 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11539 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11540 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11541 {
11542 expr = remove_c_maybe_const_expr (expr);
11543 expr = build2 (NE_EXPR, integer_type_node, expr,
11544 convert (TREE_TYPE (expr), integer_zero_node));
11545 expr = note_integer_operands (expr);
11546 }
11547 else
11548 /* ??? Should we also give an error for vectors rather than leaving
11549 those to give errors later? */
11550 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11551
11552 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11553 {
11554 if (TREE_OVERFLOW (expr))
11555 return expr;
11556 else
11557 return note_integer_operands (expr);
11558 }
11559 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11560 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11561 return expr;
85498824 11562}
73f397d4
JM
11563\f
11564
11565/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11566 required. */
11567
11568tree
51eed280 11569c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11570{
11571 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11572 {
11573 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11574 /* Executing a compound literal inside a function reinitializes
11575 it. */
11576 if (!TREE_STATIC (decl))
11577 *se = true;
11578 return decl;
11579 }
11580 else
11581 return expr;
11582}
953ff289 11583\f
88bae6f4
TS
11584/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11585 statement. LOC is the location of the construct. */
41dbbb37
TS
11586
11587tree
88bae6f4
TS
11588c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11589 tree clauses)
41dbbb37 11590{
88bae6f4 11591 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11592
88bae6f4 11593 tree stmt = make_node (code);
41dbbb37 11594 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11595 OMP_BODY (stmt) = body;
11596 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11597 SET_EXPR_LOCATION (stmt, loc);
11598
11599 return add_stmt (stmt);
11600}
11601
11602/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11603 statement. LOC is the location of the OACC_DATA. */
11604
11605tree
11606c_finish_oacc_data (location_t loc, tree clauses, tree block)
11607{
11608 tree stmt;
11609
11610 block = c_end_compound_stmt (loc, block, true);
11611
11612 stmt = make_node (OACC_DATA);
11613 TREE_TYPE (stmt) = void_type_node;
11614 OACC_DATA_CLAUSES (stmt) = clauses;
11615 OACC_DATA_BODY (stmt) = block;
11616 SET_EXPR_LOCATION (stmt, loc);
11617
11618 return add_stmt (stmt);
11619}
11620
c0220ea4 11621/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11622
11623tree
11624c_begin_omp_parallel (void)
11625{
11626 tree block;
11627
11628 keep_next_level ();
11629 block = c_begin_compound_stmt (true);
11630
11631 return block;
11632}
11633
c2255bc4
AH
11634/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11635 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11636
953ff289 11637tree
c2255bc4 11638c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11639{
11640 tree stmt;
11641
c2255bc4 11642 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11643
11644 stmt = make_node (OMP_PARALLEL);
11645 TREE_TYPE (stmt) = void_type_node;
11646 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11647 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11648 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11649
11650 return add_stmt (stmt);
11651}
11652
a68ab351
JJ
11653/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11654
11655tree
11656c_begin_omp_task (void)
11657{
11658 tree block;
11659
11660 keep_next_level ();
11661 block = c_begin_compound_stmt (true);
11662
11663 return block;
11664}
11665
c2255bc4
AH
11666/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11667 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11668
11669tree
c2255bc4 11670c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11671{
11672 tree stmt;
11673
c2255bc4 11674 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11675
11676 stmt = make_node (OMP_TASK);
11677 TREE_TYPE (stmt) = void_type_node;
11678 OMP_TASK_CLAUSES (stmt) = clauses;
11679 OMP_TASK_BODY (stmt) = block;
c2255bc4 11680 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11681
11682 return add_stmt (stmt);
11683}
11684
acf0174b
JJ
11685/* Generate GOMP_cancel call for #pragma omp cancel. */
11686
11687void
11688c_finish_omp_cancel (location_t loc, tree clauses)
11689{
11690 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11691 int mask = 0;
11692 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11693 mask = 1;
11694 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11695 mask = 2;
11696 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11697 mask = 4;
11698 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11699 mask = 8;
11700 else
11701 {
11702 error_at (loc, "%<#pragma omp cancel must specify one of "
11703 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11704 "clauses");
11705 return;
11706 }
11707 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11708 if (ifc != NULL_TREE)
11709 {
11710 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11711 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11712 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11713 build_zero_cst (type));
11714 }
11715 else
11716 ifc = boolean_true_node;
11717 tree stmt = build_call_expr_loc (loc, fn, 2,
11718 build_int_cst (integer_type_node, mask),
11719 ifc);
11720 add_stmt (stmt);
11721}
11722
11723/* Generate GOMP_cancellation_point call for
11724 #pragma omp cancellation point. */
11725
11726void
11727c_finish_omp_cancellation_point (location_t loc, tree clauses)
11728{
11729 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11730 int mask = 0;
11731 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11732 mask = 1;
11733 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11734 mask = 2;
11735 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11736 mask = 4;
11737 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11738 mask = 8;
11739 else
11740 {
11741 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11742 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11743 "clauses");
11744 return;
11745 }
11746 tree stmt = build_call_expr_loc (loc, fn, 1,
11747 build_int_cst (integer_type_node, mask));
11748 add_stmt (stmt);
11749}
11750
11751/* Helper function for handle_omp_array_sections. Called recursively
11752 to handle multiple array-section-subscripts. C is the clause,
11753 T current expression (initially OMP_CLAUSE_DECL), which is either
11754 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11755 expression if specified, TREE_VALUE length expression if specified,
11756 TREE_CHAIN is what it has been specified after, or some decl.
11757 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11758 set to true if any of the array-section-subscript could have length
11759 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11760 first array-section-subscript which is known not to have length
11761 of one. Given say:
11762 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11763 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11764 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 11765 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
11766 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11767 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11768 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11769 case though, as some lengths could be zero. */
11770
11771static tree
11772handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32
JJ
11773 bool &maybe_zero_len, unsigned int &first_non_one,
11774 bool is_omp)
acf0174b
JJ
11775{
11776 tree ret, low_bound, length, type;
11777 if (TREE_CODE (t) != TREE_LIST)
11778 {
7a0ca710 11779 if (error_operand_p (t))
acf0174b 11780 return error_mark_node;
d9a6bd32
JJ
11781 ret = t;
11782 if (TREE_CODE (t) == COMPONENT_REF
11783 && is_omp
11784 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11785 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11786 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11787 {
11788 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11789 {
11790 error_at (OMP_CLAUSE_LOCATION (c),
11791 "bit-field %qE in %qs clause",
11792 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11793 return error_mark_node;
11794 }
11795 while (TREE_CODE (t) == COMPONENT_REF)
11796 {
11797 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11798 {
11799 error_at (OMP_CLAUSE_LOCATION (c),
11800 "%qE is a member of a union", t);
11801 return error_mark_node;
11802 }
11803 t = TREE_OPERAND (t, 0);
11804 }
11805 }
0ae9bd27 11806 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
11807 {
11808 if (DECL_P (t))
11809 error_at (OMP_CLAUSE_LOCATION (c),
11810 "%qD is not a variable in %qs clause", t,
11811 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11812 else
11813 error_at (OMP_CLAUSE_LOCATION (c),
11814 "%qE is not a variable in %qs clause", t,
11815 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11816 return error_mark_node;
11817 }
11818 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0ae9bd27 11819 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
11820 {
11821 error_at (OMP_CLAUSE_LOCATION (c),
11822 "%qD is threadprivate variable in %qs clause", t,
11823 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11824 return error_mark_node;
11825 }
d9a6bd32 11826 return ret;
acf0174b
JJ
11827 }
11828
11829 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
d9a6bd32 11830 maybe_zero_len, first_non_one, is_omp);
acf0174b
JJ
11831 if (ret == error_mark_node || ret == NULL_TREE)
11832 return ret;
11833
11834 type = TREE_TYPE (ret);
11835 low_bound = TREE_PURPOSE (t);
11836 length = TREE_VALUE (t);
11837
11838 if (low_bound == error_mark_node || length == error_mark_node)
11839 return error_mark_node;
11840
11841 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11842 {
11843 error_at (OMP_CLAUSE_LOCATION (c),
11844 "low bound %qE of array section does not have integral type",
11845 low_bound);
11846 return error_mark_node;
11847 }
11848 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11849 {
11850 error_at (OMP_CLAUSE_LOCATION (c),
11851 "length %qE of array section does not have integral type",
11852 length);
11853 return error_mark_node;
11854 }
11855 if (low_bound
11856 && TREE_CODE (low_bound) == INTEGER_CST
11857 && TYPE_PRECISION (TREE_TYPE (low_bound))
11858 > TYPE_PRECISION (sizetype))
11859 low_bound = fold_convert (sizetype, low_bound);
11860 if (length
11861 && TREE_CODE (length) == INTEGER_CST
11862 && TYPE_PRECISION (TREE_TYPE (length))
11863 > TYPE_PRECISION (sizetype))
11864 length = fold_convert (sizetype, length);
11865 if (low_bound == NULL_TREE)
11866 low_bound = integer_zero_node;
11867
11868 if (length != NULL_TREE)
11869 {
11870 if (!integer_nonzerop (length))
d9a6bd32
JJ
11871 {
11872 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11873 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11874 {
11875 if (integer_zerop (length))
11876 {
11877 error_at (OMP_CLAUSE_LOCATION (c),
11878 "zero length array section in %qs clause",
11879 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11880 return error_mark_node;
11881 }
11882 }
11883 else
11884 maybe_zero_len = true;
11885 }
acf0174b
JJ
11886 if (first_non_one == types.length ()
11887 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11888 first_non_one++;
11889 }
11890 if (TREE_CODE (type) == ARRAY_TYPE)
11891 {
11892 if (length == NULL_TREE
11893 && (TYPE_DOMAIN (type) == NULL_TREE
11894 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11895 {
11896 error_at (OMP_CLAUSE_LOCATION (c),
11897 "for unknown bound array type length expression must "
11898 "be specified");
11899 return error_mark_node;
11900 }
11901 if (TREE_CODE (low_bound) == INTEGER_CST
11902 && tree_int_cst_sgn (low_bound) == -1)
11903 {
11904 error_at (OMP_CLAUSE_LOCATION (c),
11905 "negative low bound in array section in %qs clause",
11906 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11907 return error_mark_node;
11908 }
11909 if (length != NULL_TREE
11910 && TREE_CODE (length) == INTEGER_CST
11911 && tree_int_cst_sgn (length) == -1)
11912 {
11913 error_at (OMP_CLAUSE_LOCATION (c),
11914 "negative length in array section in %qs clause",
11915 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11916 return error_mark_node;
11917 }
11918 if (TYPE_DOMAIN (type)
11919 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11920 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11921 == INTEGER_CST)
11922 {
11923 tree size = size_binop (PLUS_EXPR,
11924 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11925 size_one_node);
11926 if (TREE_CODE (low_bound) == INTEGER_CST)
11927 {
11928 if (tree_int_cst_lt (size, low_bound))
11929 {
11930 error_at (OMP_CLAUSE_LOCATION (c),
11931 "low bound %qE above array section size "
11932 "in %qs clause", low_bound,
11933 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11934 return error_mark_node;
11935 }
11936 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
11937 {
11938 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11939 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11940 {
11941 error_at (OMP_CLAUSE_LOCATION (c),
11942 "zero length array section in %qs clause",
11943 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11944 return error_mark_node;
11945 }
11946 maybe_zero_len = true;
11947 }
acf0174b
JJ
11948 else if (length == NULL_TREE
11949 && first_non_one == types.length ()
11950 && tree_int_cst_equal
11951 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11952 low_bound))
11953 first_non_one++;
11954 }
11955 else if (length == NULL_TREE)
11956 {
d9a6bd32
JJ
11957 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11958 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11959 maybe_zero_len = true;
acf0174b
JJ
11960 if (first_non_one == types.length ())
11961 first_non_one++;
11962 }
11963 if (length && TREE_CODE (length) == INTEGER_CST)
11964 {
11965 if (tree_int_cst_lt (size, length))
11966 {
11967 error_at (OMP_CLAUSE_LOCATION (c),
11968 "length %qE above array section size "
11969 "in %qs clause", length,
11970 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11971 return error_mark_node;
11972 }
11973 if (TREE_CODE (low_bound) == INTEGER_CST)
11974 {
11975 tree lbpluslen
11976 = size_binop (PLUS_EXPR,
11977 fold_convert (sizetype, low_bound),
11978 fold_convert (sizetype, length));
11979 if (TREE_CODE (lbpluslen) == INTEGER_CST
11980 && tree_int_cst_lt (size, lbpluslen))
11981 {
11982 error_at (OMP_CLAUSE_LOCATION (c),
11983 "high bound %qE above array section size "
11984 "in %qs clause", lbpluslen,
11985 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11986 return error_mark_node;
11987 }
11988 }
11989 }
11990 }
11991 else if (length == NULL_TREE)
11992 {
d9a6bd32
JJ
11993 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11994 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11995 maybe_zero_len = true;
acf0174b
JJ
11996 if (first_non_one == types.length ())
11997 first_non_one++;
11998 }
11999
12000 /* For [lb:] we will need to evaluate lb more than once. */
12001 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12002 {
12003 tree lb = c_save_expr (low_bound);
12004 if (lb != low_bound)
12005 {
12006 TREE_PURPOSE (t) = lb;
12007 low_bound = lb;
12008 }
12009 }
12010 }
12011 else if (TREE_CODE (type) == POINTER_TYPE)
12012 {
12013 if (length == NULL_TREE)
12014 {
12015 error_at (OMP_CLAUSE_LOCATION (c),
12016 "for pointer type length expression must be specified");
12017 return error_mark_node;
12018 }
d9a6bd32
JJ
12019 if (length != NULL_TREE
12020 && TREE_CODE (length) == INTEGER_CST
12021 && tree_int_cst_sgn (length) == -1)
12022 {
12023 error_at (OMP_CLAUSE_LOCATION (c),
12024 "negative length in array section in %qs clause",
12025 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12026 return error_mark_node;
12027 }
acf0174b
JJ
12028 /* If there is a pointer type anywhere but in the very first
12029 array-section-subscript, the array section can't be contiguous. */
12030 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12031 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12032 {
12033 error_at (OMP_CLAUSE_LOCATION (c),
12034 "array section is not contiguous in %qs clause",
12035 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12036 return error_mark_node;
12037 }
12038 }
12039 else
12040 {
12041 error_at (OMP_CLAUSE_LOCATION (c),
12042 "%qE does not have pointer or array type", ret);
12043 return error_mark_node;
12044 }
12045 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12046 types.safe_push (TREE_TYPE (ret));
12047 /* We will need to evaluate lb more than once. */
12048 tree lb = c_save_expr (low_bound);
12049 if (lb != low_bound)
12050 {
12051 TREE_PURPOSE (t) = lb;
12052 low_bound = lb;
12053 }
12054 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12055 return ret;
12056}
12057
12058/* Handle array sections for clause C. */
12059
12060static bool
d9a6bd32 12061handle_omp_array_sections (tree c, bool is_omp)
acf0174b
JJ
12062{
12063 bool maybe_zero_len = false;
12064 unsigned int first_non_one = 0;
d9a6bd32 12065 auto_vec<tree, 10> types;
acf0174b 12066 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32
JJ
12067 maybe_zero_len, first_non_one,
12068 is_omp);
acf0174b 12069 if (first == error_mark_node)
d9a6bd32 12070 return true;
acf0174b 12071 if (first == NULL_TREE)
d9a6bd32 12072 return false;
acf0174b
JJ
12073 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12074 {
12075 tree t = OMP_CLAUSE_DECL (c);
12076 tree tem = NULL_TREE;
acf0174b
JJ
12077 /* Need to evaluate side effects in the length expressions
12078 if any. */
12079 while (TREE_CODE (t) == TREE_LIST)
12080 {
12081 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12082 {
12083 if (tem == NULL_TREE)
12084 tem = TREE_VALUE (t);
12085 else
12086 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12087 TREE_VALUE (t), tem);
12088 }
12089 t = TREE_CHAIN (t);
12090 }
12091 if (tem)
12092 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12093 first = c_fully_fold (first, false, NULL);
12094 OMP_CLAUSE_DECL (c) = first;
12095 }
12096 else
12097 {
12098 unsigned int num = types.length (), i;
12099 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12100 tree condition = NULL_TREE;
12101
12102 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12103 maybe_zero_len = true;
12104
12105 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12106 t = TREE_CHAIN (t))
12107 {
12108 tree low_bound = TREE_PURPOSE (t);
12109 tree length = TREE_VALUE (t);
12110
12111 i--;
12112 if (low_bound
12113 && TREE_CODE (low_bound) == INTEGER_CST
12114 && TYPE_PRECISION (TREE_TYPE (low_bound))
12115 > TYPE_PRECISION (sizetype))
12116 low_bound = fold_convert (sizetype, low_bound);
12117 if (length
12118 && TREE_CODE (length) == INTEGER_CST
12119 && TYPE_PRECISION (TREE_TYPE (length))
12120 > TYPE_PRECISION (sizetype))
12121 length = fold_convert (sizetype, length);
12122 if (low_bound == NULL_TREE)
12123 low_bound = integer_zero_node;
12124 if (!maybe_zero_len && i > first_non_one)
12125 {
12126 if (integer_nonzerop (low_bound))
12127 goto do_warn_noncontiguous;
12128 if (length != NULL_TREE
12129 && TREE_CODE (length) == INTEGER_CST
12130 && TYPE_DOMAIN (types[i])
12131 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12132 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12133 == INTEGER_CST)
12134 {
12135 tree size;
12136 size = size_binop (PLUS_EXPR,
12137 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12138 size_one_node);
12139 if (!tree_int_cst_equal (length, size))
12140 {
12141 do_warn_noncontiguous:
12142 error_at (OMP_CLAUSE_LOCATION (c),
12143 "array section is not contiguous in %qs "
12144 "clause",
12145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12146 return true;
12147 }
12148 }
12149 if (length != NULL_TREE
12150 && TREE_SIDE_EFFECTS (length))
12151 {
12152 if (side_effects == NULL_TREE)
12153 side_effects = length;
12154 else
12155 side_effects = build2 (COMPOUND_EXPR,
12156 TREE_TYPE (side_effects),
12157 length, side_effects);
12158 }
12159 }
12160 else
12161 {
12162 tree l;
12163
d9a6bd32
JJ
12164 if (i > first_non_one
12165 && ((length && integer_nonzerop (length))
12166 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12167 continue;
12168 if (length)
12169 l = fold_convert (sizetype, length);
12170 else
12171 {
12172 l = size_binop (PLUS_EXPR,
12173 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12174 size_one_node);
12175 l = size_binop (MINUS_EXPR, l,
12176 fold_convert (sizetype, low_bound));
12177 }
12178 if (i > first_non_one)
12179 {
12180 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12181 size_zero_node);
12182 if (condition == NULL_TREE)
12183 condition = l;
12184 else
12185 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12186 l, condition);
12187 }
12188 else if (size == NULL_TREE)
12189 {
12190 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12191 tree eltype = TREE_TYPE (types[num - 1]);
12192 while (TREE_CODE (eltype) == ARRAY_TYPE)
12193 eltype = TREE_TYPE (eltype);
12194 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12195 {
12196 if (integer_zerop (size)
12197 || integer_zerop (size_in_bytes (eltype)))
12198 {
12199 error_at (OMP_CLAUSE_LOCATION (c),
12200 "zero length array section in %qs clause",
12201 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12202 return error_mark_node;
12203 }
12204 size = size_binop (EXACT_DIV_EXPR, size,
12205 size_in_bytes (eltype));
12206 }
acf0174b
JJ
12207 size = size_binop (MULT_EXPR, size, l);
12208 if (condition)
12209 size = fold_build3 (COND_EXPR, sizetype, condition,
12210 size, size_zero_node);
12211 }
12212 else
12213 size = size_binop (MULT_EXPR, size, l);
12214 }
12215 }
acf0174b
JJ
12216 if (side_effects)
12217 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12218 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12219 {
12220 size = size_binop (MINUS_EXPR, size, size_one_node);
12221 size = c_fully_fold (size, false, NULL);
12222 tree index_type = build_index_type (size);
12223 tree eltype = TREE_TYPE (first);
12224 while (TREE_CODE (eltype) == ARRAY_TYPE)
12225 eltype = TREE_TYPE (eltype);
12226 tree type = build_array_type (eltype, index_type);
12227 tree ptype = build_pointer_type (eltype);
12228 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12229 t = build_fold_addr_expr (t);
e01d41e5
JJ
12230 tree t2 = build_fold_addr_expr (first);
12231 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12232 ptrdiff_type_node, t2);
12233 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12234 ptrdiff_type_node, t2,
12235 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12236 ptrdiff_type_node, t));
12237 t2 = c_fully_fold (t2, false, NULL);
12238 if (tree_fits_shwi_p (t2))
12239 t = build2 (MEM_REF, type, t,
12240 build_int_cst (ptype, tree_to_shwi (t2)));
12241 else
12242 {
12243 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12244 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12245 TREE_TYPE (t), t, t2);
12246 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12247 }
d9a6bd32
JJ
12248 OMP_CLAUSE_DECL (c) = t;
12249 return false;
12250 }
acf0174b
JJ
12251 first = c_fully_fold (first, false, NULL);
12252 OMP_CLAUSE_DECL (c) = first;
12253 if (size)
12254 size = c_fully_fold (size, false, NULL);
12255 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12256 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12257 || (TREE_CODE (t) == COMPONENT_REF
12258 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12259 return false;
41dbbb37 12260 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
d9a6bd32
JJ
12261 if (is_omp)
12262 switch (OMP_CLAUSE_MAP_KIND (c))
12263 {
12264 case GOMP_MAP_ALLOC:
12265 case GOMP_MAP_TO:
12266 case GOMP_MAP_FROM:
12267 case GOMP_MAP_TOFROM:
12268 case GOMP_MAP_ALWAYS_TO:
12269 case GOMP_MAP_ALWAYS_FROM:
12270 case GOMP_MAP_ALWAYS_TOFROM:
12271 case GOMP_MAP_RELEASE:
12272 case GOMP_MAP_DELETE:
12273 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12274 break;
12275 default:
12276 break;
12277 }
acf0174b 12278 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e01d41e5
JJ
12279 if (!is_omp)
12280 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12281 else if (TREE_CODE (t) == COMPONENT_REF)
12282 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12283 else
12284 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12285 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12286 && !c_mark_addressable (t))
acf0174b
JJ
12287 return false;
12288 OMP_CLAUSE_DECL (c2) = t;
12289 t = build_fold_addr_expr (first);
12290 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12291 tree ptr = OMP_CLAUSE_DECL (c2);
12292 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12293 ptr = build_fold_addr_expr (ptr);
12294 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12295 ptrdiff_type_node, t,
12296 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12297 ptrdiff_type_node, ptr));
12298 t = c_fully_fold (t, false, NULL);
12299 OMP_CLAUSE_SIZE (c2) = t;
12300 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12301 OMP_CLAUSE_CHAIN (c) = c2;
12302 }
12303 return false;
12304}
12305
12306/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12307 an inline call. But, remap
12308 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12309 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12310
12311static tree
12312c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12313 tree decl, tree placeholder)
12314{
12315 copy_body_data id;
b787e7a2 12316 hash_map<tree, tree> decl_map;
acf0174b 12317
b787e7a2
TS
12318 decl_map.put (omp_decl1, placeholder);
12319 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12320 memset (&id, 0, sizeof (id));
12321 id.src_fn = DECL_CONTEXT (omp_decl1);
12322 id.dst_fn = current_function_decl;
12323 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12324 id.decl_map = &decl_map;
acf0174b
JJ
12325
12326 id.copy_decl = copy_decl_no_change;
12327 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12328 id.transform_new_cfg = true;
12329 id.transform_return_to_modify = false;
12330 id.transform_lang_insert_block = NULL;
12331 id.eh_lp_nr = 0;
12332 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12333 return stmt;
12334}
12335
12336/* Helper function of c_finish_omp_clauses, called via walk_tree.
12337 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12338
12339static tree
12340c_find_omp_placeholder_r (tree *tp, int *, void *data)
12341{
12342 if (*tp == (tree) data)
12343 return *tp;
12344 return NULL_TREE;
12345}
12346
41dbbb37 12347/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12348 Remove any elements from the list that are invalid. */
12349
12350tree
d9a6bd32 12351c_finish_omp_clauses (tree clauses, bool is_omp, bool declare_simd)
953ff289
DN
12352{
12353 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e01d41e5 12354 bitmap_head aligned_head, map_head, map_field_head;
d9a6bd32
JJ
12355 tree c, t, type, *pc;
12356 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12357 bool branch_seen = false;
12358 bool copyprivate_seen = false;
e01d41e5 12359 bool linear_variable_step_check = false;
acf0174b 12360 tree *nowait_clause = NULL;
e01d41e5
JJ
12361 bool ordered_seen = false;
12362 tree schedule_clause = NULL_TREE;
953ff289
DN
12363
12364 bitmap_obstack_initialize (NULL);
12365 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12366 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12367 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12368 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12369 bitmap_initialize (&map_head, &bitmap_default_obstack);
12370 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
953ff289
DN
12371
12372 for (pc = &clauses, c = clauses; c ; c = *pc)
12373 {
12374 bool remove = false;
12375 bool need_complete = false;
12376 bool need_implicitly_determined = false;
12377
aaf46ef9 12378 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12379 {
12380 case OMP_CLAUSE_SHARED:
953ff289
DN
12381 need_implicitly_determined = true;
12382 goto check_dup_generic;
12383
12384 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12385 need_complete = true;
12386 need_implicitly_determined = true;
12387 goto check_dup_generic;
12388
12389 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12390 need_implicitly_determined = true;
12391 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12392 if (TREE_CODE (t) == TREE_LIST)
12393 {
12394 if (handle_omp_array_sections (c, is_omp))
12395 {
12396 remove = true;
12397 break;
12398 }
12399
12400 t = OMP_CLAUSE_DECL (c);
12401 }
12402 t = require_complete_type (t);
12403 if (t == error_mark_node)
12404 {
12405 remove = true;
12406 break;
12407 }
12408 type = TREE_TYPE (t);
12409 if (TREE_CODE (t) == MEM_REF)
12410 type = TREE_TYPE (type);
12411 if (TREE_CODE (type) == ARRAY_TYPE)
12412 {
12413 tree oatype = type;
12414 gcc_assert (TREE_CODE (t) != MEM_REF);
12415 while (TREE_CODE (type) == ARRAY_TYPE)
12416 type = TREE_TYPE (type);
12417 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12418 {
12419 error_at (OMP_CLAUSE_LOCATION (c),
12420 "%qD in %<reduction%> clause is a zero size array",
12421 t);
12422 remove = true;
12423 break;
12424 }
12425 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12426 TYPE_SIZE_UNIT (type));
12427 if (integer_zerop (size))
12428 {
12429 error_at (OMP_CLAUSE_LOCATION (c),
12430 "%qD in %<reduction%> clause is a zero size array",
12431 t);
12432 remove = true;
12433 break;
12434 }
12435 size = size_binop (MINUS_EXPR, size, size_one_node);
12436 tree index_type = build_index_type (size);
12437 tree atype = build_array_type (type, index_type);
12438 tree ptype = build_pointer_type (type);
12439 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12440 t = build_fold_addr_expr (t);
12441 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12442 OMP_CLAUSE_DECL (c) = t;
12443 }
acf0174b 12444 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12445 && (FLOAT_TYPE_P (type)
12446 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12447 {
12448 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12449 const char *r_name = NULL;
12450
12451 switch (r_code)
12452 {
12453 case PLUS_EXPR:
12454 case MULT_EXPR:
12455 case MINUS_EXPR:
652fea39 12456 break;
20906c66 12457 case MIN_EXPR:
d9a6bd32 12458 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12459 r_name = "min";
12460 break;
20906c66 12461 case MAX_EXPR:
d9a6bd32 12462 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12463 r_name = "max";
953ff289
DN
12464 break;
12465 case BIT_AND_EXPR:
12466 r_name = "&";
12467 break;
12468 case BIT_XOR_EXPR:
12469 r_name = "^";
12470 break;
12471 case BIT_IOR_EXPR:
12472 r_name = "|";
12473 break;
12474 case TRUTH_ANDIF_EXPR:
d9a6bd32 12475 if (FLOAT_TYPE_P (type))
652fea39 12476 r_name = "&&";
953ff289
DN
12477 break;
12478 case TRUTH_ORIF_EXPR:
d9a6bd32 12479 if (FLOAT_TYPE_P (type))
652fea39 12480 r_name = "||";
953ff289
DN
12481 break;
12482 default:
12483 gcc_unreachable ();
12484 }
12485 if (r_name)
12486 {
c2255bc4
AH
12487 error_at (OMP_CLAUSE_LOCATION (c),
12488 "%qE has invalid type for %<reduction(%s)%>",
12489 t, r_name);
953ff289 12490 remove = true;
ee1d5a02 12491 break;
953ff289
DN
12492 }
12493 }
acf0174b
JJ
12494 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12495 {
12496 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12497 "user defined reduction not found for %qE", t);
acf0174b 12498 remove = true;
ee1d5a02 12499 break;
acf0174b
JJ
12500 }
12501 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12502 {
12503 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12504 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12505 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12506 VAR_DECL, NULL_TREE, type);
d9a6bd32 12507 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12508 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12509 DECL_ARTIFICIAL (placeholder) = 1;
12510 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12511 if (TREE_CODE (t) == MEM_REF)
12512 {
12513 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12514 VAR_DECL, NULL_TREE, type);
12515 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12516 DECL_ARTIFICIAL (decl_placeholder) = 1;
12517 DECL_IGNORED_P (decl_placeholder) = 1;
12518 }
acf0174b
JJ
12519 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12520 c_mark_addressable (placeholder);
12521 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12522 c_mark_addressable (decl_placeholder ? decl_placeholder
12523 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12524 OMP_CLAUSE_REDUCTION_MERGE (c)
12525 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12526 TREE_VEC_ELT (list, 0),
12527 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12528 decl_placeholder ? decl_placeholder
12529 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12530 OMP_CLAUSE_REDUCTION_MERGE (c)
12531 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12532 void_type_node, NULL_TREE,
d9a6bd32 12533 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12534 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12535 if (TREE_VEC_LENGTH (list) == 6)
12536 {
12537 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12538 c_mark_addressable (decl_placeholder ? decl_placeholder
12539 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12540 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12541 c_mark_addressable (placeholder);
12542 tree init = TREE_VEC_ELT (list, 5);
12543 if (init == error_mark_node)
12544 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12545 OMP_CLAUSE_REDUCTION_INIT (c)
12546 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12547 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12548 decl_placeholder ? decl_placeholder
12549 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12550 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12551 {
12552 tree v = decl_placeholder ? decl_placeholder : t;
12553 OMP_CLAUSE_REDUCTION_INIT (c)
12554 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12555 OMP_CLAUSE_REDUCTION_INIT (c));
12556 }
acf0174b
JJ
12557 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12558 c_find_omp_placeholder_r,
12559 placeholder, NULL))
12560 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12561 }
12562 else
12563 {
12564 tree init;
d9a6bd32
JJ
12565 tree v = decl_placeholder ? decl_placeholder : t;
12566 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12567 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12568 else
d9a6bd32 12569 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12570 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12571 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12572 }
12573 OMP_CLAUSE_REDUCTION_INIT (c)
12574 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12575 void_type_node, NULL_TREE,
12576 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12577 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12578 }
d9a6bd32
JJ
12579 if (TREE_CODE (t) == MEM_REF)
12580 {
12581 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12582 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12583 != INTEGER_CST)
12584 {
12585 sorry ("variable length element type in array "
12586 "%<reduction%> clause");
12587 remove = true;
12588 break;
12589 }
12590 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12591 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12592 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12593 if (TREE_CODE (t) == ADDR_EXPR)
12594 t = TREE_OPERAND (t, 0);
12595 }
12596 goto check_dup_generic_t;
953ff289
DN
12597
12598 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12599 copyprivate_seen = true;
12600 if (nowait_clause)
12601 {
12602 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12603 "%<nowait%> clause must not be used together "
12604 "with %<copyprivate%>");
12605 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12606 nowait_clause = NULL;
12607 }
953ff289
DN
12608 goto check_dup_generic;
12609
12610 case OMP_CLAUSE_COPYIN:
953ff289 12611 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12612 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12613 {
c2255bc4
AH
12614 error_at (OMP_CLAUSE_LOCATION (c),
12615 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12616 remove = true;
ee1d5a02 12617 break;
953ff289
DN
12618 }
12619 goto check_dup_generic;
12620
acf0174b 12621 case OMP_CLAUSE_LINEAR:
d9a6bd32
JJ
12622 if (!declare_simd)
12623 need_implicitly_determined = true;
acf0174b 12624 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12625 if (!declare_simd
12626 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12627 {
12628 error_at (OMP_CLAUSE_LOCATION (c),
12629 "modifier should not be specified in %<linear%> "
12630 "clause on %<simd%> or %<for%> constructs");
12631 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12632 }
acf0174b
JJ
12633 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12634 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12635 {
12636 error_at (OMP_CLAUSE_LOCATION (c),
12637 "linear clause applied to non-integral non-pointer "
12638 "variable with type %qT", TREE_TYPE (t));
12639 remove = true;
12640 break;
12641 }
e01d41e5
JJ
12642 if (declare_simd)
12643 {
12644 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12645 if (TREE_CODE (s) == PARM_DECL)
12646 {
12647 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12648 /* map_head bitmap is used as uniform_head if
12649 declare_simd. */
12650 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12651 linear_variable_step_check = true;
12652 goto check_dup_generic;
12653 }
12654 if (TREE_CODE (s) != INTEGER_CST)
12655 {
12656 error_at (OMP_CLAUSE_LOCATION (c),
12657 "%<linear%> clause step %qE is neither constant "
12658 "nor a parameter", s);
12659 remove = true;
12660 break;
12661 }
12662 }
acf0174b
JJ
12663 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12664 {
12665 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12666 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12667 OMP_CLAUSE_DECL (c), s);
12668 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12669 sizetype, fold_convert (sizetype, s),
12670 fold_convert
12671 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
12672 if (s == error_mark_node)
12673 s = size_one_node;
12674 OMP_CLAUSE_LINEAR_STEP (c) = s;
12675 }
da6f124d
JJ
12676 else
12677 OMP_CLAUSE_LINEAR_STEP (c)
12678 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12679 goto check_dup_generic;
12680
953ff289
DN
12681 check_dup_generic:
12682 t = OMP_CLAUSE_DECL (c);
d9a6bd32 12683 check_dup_generic_t:
0ae9bd27 12684 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12685 {
c2255bc4 12686 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12687 "%qE is not a variable in clause %qs", t,
12688 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12689 remove = true;
12690 }
12691 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12692 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12693 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12694 {
c2255bc4
AH
12695 error_at (OMP_CLAUSE_LOCATION (c),
12696 "%qE appears more than once in data clauses", t);
953ff289
DN
12697 remove = true;
12698 }
e01d41e5
JJ
12699 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12700 && bitmap_bit_p (&map_head, DECL_UID (t)))
12701 {
12702 error ("%qD appears both in data and map clauses", t);
12703 remove = true;
12704 }
953ff289
DN
12705 else
12706 bitmap_set_bit (&generic_head, DECL_UID (t));
12707 break;
12708
12709 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12710 t = OMP_CLAUSE_DECL (c);
12711 need_complete = true;
12712 need_implicitly_determined = true;
0ae9bd27 12713 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12714 {
c2255bc4
AH
12715 error_at (OMP_CLAUSE_LOCATION (c),
12716 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12717 remove = true;
12718 }
12719 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12720 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12721 {
c2255bc4
AH
12722 error_at (OMP_CLAUSE_LOCATION (c),
12723 "%qE appears more than once in data clauses", t);
953ff289
DN
12724 remove = true;
12725 }
e01d41e5
JJ
12726 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12727 {
12728 error ("%qD appears both in data and map clauses", t);
12729 remove = true;
12730 }
953ff289
DN
12731 else
12732 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12733 break;
12734
12735 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12736 t = OMP_CLAUSE_DECL (c);
12737 need_complete = true;
12738 need_implicitly_determined = true;
0ae9bd27 12739 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12740 {
c2255bc4
AH
12741 error_at (OMP_CLAUSE_LOCATION (c),
12742 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12743 remove = true;
12744 }
12745 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12746 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12747 {
c2255bc4
AH
12748 error_at (OMP_CLAUSE_LOCATION (c),
12749 "%qE appears more than once in data clauses", t);
953ff289
DN
12750 remove = true;
12751 }
12752 else
12753 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12754 break;
12755
acf0174b
JJ
12756 case OMP_CLAUSE_ALIGNED:
12757 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12758 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12759 {
12760 error_at (OMP_CLAUSE_LOCATION (c),
12761 "%qE is not a variable in %<aligned%> clause", t);
12762 remove = true;
12763 }
5a9785fb
JJ
12764 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12765 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12766 {
12767 error_at (OMP_CLAUSE_LOCATION (c),
12768 "%qE in %<aligned%> clause is neither a pointer nor "
12769 "an array", t);
12770 remove = true;
12771 }
acf0174b
JJ
12772 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12773 {
12774 error_at (OMP_CLAUSE_LOCATION (c),
12775 "%qE appears more than once in %<aligned%> clauses",
12776 t);
12777 remove = true;
12778 }
12779 else
12780 bitmap_set_bit (&aligned_head, DECL_UID (t));
12781 break;
12782
12783 case OMP_CLAUSE_DEPEND:
12784 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12785 if (t == NULL_TREE)
12786 {
12787 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
12788 == OMP_CLAUSE_DEPEND_SOURCE);
12789 break;
12790 }
12791 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12792 {
12793 gcc_assert (TREE_CODE (t) == TREE_LIST);
12794 for (; t; t = TREE_CHAIN (t))
12795 {
12796 tree decl = TREE_VALUE (t);
12797 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
12798 {
12799 tree offset = TREE_PURPOSE (t);
12800 bool neg = wi::neg_p ((wide_int) offset);
12801 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
12802 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
12803 neg ? MINUS_EXPR : PLUS_EXPR,
12804 decl, offset);
12805 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12806 sizetype,
12807 fold_convert (sizetype, t2),
12808 fold_convert (sizetype, decl));
d9a6bd32
JJ
12809 if (t2 == error_mark_node)
12810 {
12811 remove = true;
12812 break;
12813 }
12814 TREE_PURPOSE (t) = t2;
12815 }
12816 }
12817 break;
12818 }
acf0174b
JJ
12819 if (TREE_CODE (t) == TREE_LIST)
12820 {
d9a6bd32 12821 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12822 remove = true;
12823 break;
12824 }
12825 if (t == error_mark_node)
12826 remove = true;
0ae9bd27 12827 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12828 {
12829 error_at (OMP_CLAUSE_LOCATION (c),
12830 "%qE is not a variable in %<depend%> clause", t);
12831 remove = true;
12832 }
12833 else if (!c_mark_addressable (t))
12834 remove = true;
12835 break;
12836
12837 case OMP_CLAUSE_MAP:
12838 case OMP_CLAUSE_TO:
12839 case OMP_CLAUSE_FROM:
41dbbb37 12840 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12841 t = OMP_CLAUSE_DECL (c);
12842 if (TREE_CODE (t) == TREE_LIST)
12843 {
d9a6bd32 12844 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12845 remove = true;
12846 else
12847 {
12848 t = OMP_CLAUSE_DECL (c);
b17a8b07 12849 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12850 {
12851 error_at (OMP_CLAUSE_LOCATION (c),
12852 "array section does not have mappable type "
12853 "in %qs clause",
12854 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12855 remove = true;
12856 }
d9a6bd32
JJ
12857 while (TREE_CODE (t) == ARRAY_REF)
12858 t = TREE_OPERAND (t, 0);
12859 if (TREE_CODE (t) == COMPONENT_REF
12860 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12861 {
12862 while (TREE_CODE (t) == COMPONENT_REF)
12863 t = TREE_OPERAND (t, 0);
12864 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
12865 break;
12866 if (bitmap_bit_p (&map_head, DECL_UID (t)))
12867 {
12868 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12869 error ("%qD appears more than once in motion"
12870 " clauses", t);
12871 else
12872 error ("%qD appears more than once in map"
12873 " clauses", t);
12874 remove = true;
12875 }
12876 else
12877 {
12878 bitmap_set_bit (&map_head, DECL_UID (t));
12879 bitmap_set_bit (&map_field_head, DECL_UID (t));
12880 }
12881 }
acf0174b
JJ
12882 }
12883 break;
12884 }
12885 if (t == error_mark_node)
d9a6bd32
JJ
12886 {
12887 remove = true;
12888 break;
12889 }
12890 if (TREE_CODE (t) == COMPONENT_REF
12891 && is_omp
12892 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
12893 {
12894 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12895 {
12896 error_at (OMP_CLAUSE_LOCATION (c),
12897 "bit-field %qE in %qs clause",
12898 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12899 remove = true;
12900 }
12901 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12902 {
12903 error_at (OMP_CLAUSE_LOCATION (c),
12904 "%qE does not have a mappable type in %qs clause",
12905 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12906 remove = true;
12907 }
12908 while (TREE_CODE (t) == COMPONENT_REF)
12909 {
12910 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
12911 == UNION_TYPE)
12912 {
12913 error_at (OMP_CLAUSE_LOCATION (c),
12914 "%qE is a member of a union", t);
12915 remove = true;
12916 break;
12917 }
12918 t = TREE_OPERAND (t, 0);
12919 }
12920 if (remove)
12921 break;
12922 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
12923 {
e01d41e5 12924 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
12925 break;
12926 }
12927 }
12928 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12929 {
12930 error_at (OMP_CLAUSE_LOCATION (c),
12931 "%qE is not a variable in %qs clause", t,
12932 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12933 remove = true;
12934 }
0ae9bd27 12935 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12936 {
12937 error_at (OMP_CLAUSE_LOCATION (c),
12938 "%qD is threadprivate variable in %qs clause", t,
12939 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12940 remove = true;
12941 }
e4606348
JJ
12942 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12943 || (OMP_CLAUSE_MAP_KIND (c)
12944 != GOMP_MAP_FIRSTPRIVATE_POINTER))
12945 && !c_mark_addressable (t))
acf0174b 12946 remove = true;
b17a8b07 12947 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 12948 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
12949 || (OMP_CLAUSE_MAP_KIND (c)
12950 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
12951 || (OMP_CLAUSE_MAP_KIND (c)
12952 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 12953 && t == OMP_CLAUSE_DECL (c)
b17a8b07 12954 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12955 {
12956 error_at (OMP_CLAUSE_LOCATION (c),
12957 "%qD does not have a mappable type in %qs clause", t,
12958 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12959 remove = true;
12960 }
d9a6bd32
JJ
12961 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12962 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
12963 {
12964 if (bitmap_bit_p (&generic_head, DECL_UID (t))
12965 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12966 {
12967 error ("%qD appears more than once in data clauses", t);
12968 remove = true;
12969 }
e01d41e5 12970 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 12971 {
e01d41e5
JJ
12972 error ("%qD appears both in data and map clauses", t);
12973 remove = true;
d9a6bd32 12974 }
e01d41e5
JJ
12975 else
12976 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
12977 }
12978 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
12979 {
12980 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12981 error ("%qD appears more than once in motion clauses", t);
12982 else
12983 error ("%qD appears more than once in map clauses", t);
12984 remove = true;
12985 }
e01d41e5
JJ
12986 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12987 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12988 {
12989 error ("%qD appears both in data and map clauses", t);
12990 remove = true;
12991 }
acf0174b 12992 else
d9a6bd32
JJ
12993 {
12994 bitmap_set_bit (&map_head, DECL_UID (t));
12995 if (t != OMP_CLAUSE_DECL (c)
12996 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
12997 bitmap_set_bit (&map_field_head, DECL_UID (t));
12998 }
12999 break;
13000
13001 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13002 case OMP_CLAUSE_LINK:
13003 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13004 if (TREE_CODE (t) == FUNCTION_DECL
13005 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13006 ;
13007 else if (!VAR_P (t))
d9a6bd32 13008 {
e01d41e5
JJ
13009 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13010 error_at (OMP_CLAUSE_LOCATION (c),
13011 "%qE is neither a variable nor a function name in "
13012 "clause %qs", t,
13013 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13014 else
13015 error_at (OMP_CLAUSE_LOCATION (c),
13016 "%qE is not a variable in clause %qs", t,
13017 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13018 remove = true;
13019 }
13020 else if (DECL_THREAD_LOCAL_P (t))
13021 {
13022 error_at (OMP_CLAUSE_LOCATION (c),
13023 "%qD is threadprivate variable in %qs clause", t,
13024 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13025 remove = true;
13026 }
13027 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13028 {
13029 error_at (OMP_CLAUSE_LOCATION (c),
13030 "%qD does not have a mappable type in %qs clause", t,
13031 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13032 remove = true;
13033 }
e01d41e5
JJ
13034 if (remove)
13035 break;
13036 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13037 {
13038 error_at (OMP_CLAUSE_LOCATION (c),
13039 "%qE appears more than once on the same "
13040 "%<declare target%> directive", t);
13041 remove = true;
13042 }
13043 else
13044 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13045 break;
13046
13047 case OMP_CLAUSE_UNIFORM:
13048 t = OMP_CLAUSE_DECL (c);
13049 if (TREE_CODE (t) != PARM_DECL)
13050 {
13051 if (DECL_P (t))
13052 error_at (OMP_CLAUSE_LOCATION (c),
13053 "%qD is not an argument in %<uniform%> clause", t);
13054 else
13055 error_at (OMP_CLAUSE_LOCATION (c),
13056 "%qE is not an argument in %<uniform%> clause", t);
13057 remove = true;
ee1d5a02 13058 break;
acf0174b 13059 }
e01d41e5
JJ
13060 /* map_head bitmap is used as uniform_head if declare_simd. */
13061 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13062 goto check_dup_generic;
acf0174b 13063
d9a6bd32
JJ
13064 case OMP_CLAUSE_IS_DEVICE_PTR:
13065 case OMP_CLAUSE_USE_DEVICE_PTR:
13066 t = OMP_CLAUSE_DECL (c);
13067 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13068 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13069 {
13070 error_at (OMP_CLAUSE_LOCATION (c),
13071 "%qs variable is neither a pointer nor an array",
13072 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13073 remove = true;
13074 }
13075 goto check_dup_generic;
13076
acf0174b
JJ
13077 case OMP_CLAUSE_NOWAIT:
13078 if (copyprivate_seen)
13079 {
13080 error_at (OMP_CLAUSE_LOCATION (c),
13081 "%<nowait%> clause must not be used together "
13082 "with %<copyprivate%>");
13083 remove = true;
13084 break;
13085 }
13086 nowait_clause = pc;
13087 pc = &OMP_CLAUSE_CHAIN (c);
13088 continue;
13089
953ff289
DN
13090 case OMP_CLAUSE_IF:
13091 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13092 case OMP_CLAUSE_NUM_TEAMS:
13093 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13094 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13095 case OMP_CLAUSE_UNTIED:
13096 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13097 case OMP_CLAUSE_FINAL:
13098 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13099 case OMP_CLAUSE_DEVICE:
13100 case OMP_CLAUSE_DIST_SCHEDULE:
13101 case OMP_CLAUSE_PARALLEL:
13102 case OMP_CLAUSE_FOR:
13103 case OMP_CLAUSE_SECTIONS:
13104 case OMP_CLAUSE_TASKGROUP:
13105 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13106 case OMP_CLAUSE_PRIORITY:
13107 case OMP_CLAUSE_GRAINSIZE:
13108 case OMP_CLAUSE_NUM_TASKS:
13109 case OMP_CLAUSE_NOGROUP:
13110 case OMP_CLAUSE_THREADS:
13111 case OMP_CLAUSE_SIMD:
13112 case OMP_CLAUSE_HINT:
13113 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13114 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13115 case OMP_CLAUSE_NUM_GANGS:
13116 case OMP_CLAUSE_NUM_WORKERS:
13117 case OMP_CLAUSE_VECTOR_LENGTH:
13118 case OMP_CLAUSE_ASYNC:
13119 case OMP_CLAUSE_WAIT:
13120 case OMP_CLAUSE_AUTO:
7a5e4956 13121 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13122 case OMP_CLAUSE_SEQ:
13123 case OMP_CLAUSE_GANG:
13124 case OMP_CLAUSE_WORKER:
13125 case OMP_CLAUSE_VECTOR:
7a5e4956 13126 case OMP_CLAUSE_TILE:
acf0174b
JJ
13127 pc = &OMP_CLAUSE_CHAIN (c);
13128 continue;
13129
e01d41e5
JJ
13130 case OMP_CLAUSE_SCHEDULE:
13131 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13132 {
13133 const char *p = NULL;
13134 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13135 {
13136 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13137 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13138 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13139 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13140 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13141 default: gcc_unreachable ();
13142 }
13143 if (p)
13144 {
13145 error_at (OMP_CLAUSE_LOCATION (c),
13146 "%<nonmonotonic%> modifier specified for %qs "
13147 "schedule kind", p);
13148 OMP_CLAUSE_SCHEDULE_KIND (c)
13149 = (enum omp_clause_schedule_kind)
13150 (OMP_CLAUSE_SCHEDULE_KIND (c)
13151 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13152 }
13153 }
13154 schedule_clause = c;
13155 pc = &OMP_CLAUSE_CHAIN (c);
13156 continue;
13157
13158 case OMP_CLAUSE_ORDERED:
13159 ordered_seen = true;
13160 pc = &OMP_CLAUSE_CHAIN (c);
13161 continue;
13162
d9a6bd32
JJ
13163 case OMP_CLAUSE_SAFELEN:
13164 safelen = c;
13165 pc = &OMP_CLAUSE_CHAIN (c);
13166 continue;
13167 case OMP_CLAUSE_SIMDLEN:
13168 simdlen = c;
13169 pc = &OMP_CLAUSE_CHAIN (c);
13170 continue;
13171
acf0174b
JJ
13172 case OMP_CLAUSE_INBRANCH:
13173 case OMP_CLAUSE_NOTINBRANCH:
13174 if (branch_seen)
13175 {
13176 error_at (OMP_CLAUSE_LOCATION (c),
13177 "%<inbranch%> clause is incompatible with "
13178 "%<notinbranch%>");
13179 remove = true;
13180 break;
13181 }
13182 branch_seen = true;
953ff289
DN
13183 pc = &OMP_CLAUSE_CHAIN (c);
13184 continue;
13185
13186 default:
13187 gcc_unreachable ();
13188 }
13189
13190 if (!remove)
13191 {
13192 t = OMP_CLAUSE_DECL (c);
13193
13194 if (need_complete)
13195 {
13196 t = require_complete_type (t);
13197 if (t == error_mark_node)
13198 remove = true;
13199 }
13200
13201 if (need_implicitly_determined)
13202 {
13203 const char *share_name = NULL;
13204
0ae9bd27 13205 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13206 share_name = "threadprivate";
13207 else switch (c_omp_predetermined_sharing (t))
13208 {
13209 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13210 break;
13211 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13212 /* const vars may be specified in firstprivate clause. */
13213 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13214 && TREE_READONLY (t))
13215 break;
953ff289
DN
13216 share_name = "shared";
13217 break;
13218 case OMP_CLAUSE_DEFAULT_PRIVATE:
13219 share_name = "private";
13220 break;
13221 default:
13222 gcc_unreachable ();
13223 }
13224 if (share_name)
13225 {
c2255bc4
AH
13226 error_at (OMP_CLAUSE_LOCATION (c),
13227 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13228 t, share_name,
13229 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13230 remove = true;
13231 }
13232 }
13233 }
13234
13235 if (remove)
13236 *pc = OMP_CLAUSE_CHAIN (c);
13237 else
13238 pc = &OMP_CLAUSE_CHAIN (c);
13239 }
13240
d9a6bd32
JJ
13241 if (simdlen
13242 && safelen
13243 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13244 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13245 {
13246 error_at (OMP_CLAUSE_LOCATION (simdlen),
13247 "%<simdlen%> clause value is bigger than "
13248 "%<safelen%> clause value");
13249 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13250 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13251 }
13252
e01d41e5
JJ
13253 if (ordered_seen
13254 && schedule_clause
13255 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13256 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13257 {
13258 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13259 "%<nonmonotonic%> schedule modifier specified together "
13260 "with %<ordered%> clause");
13261 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13262 = (enum omp_clause_schedule_kind)
13263 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13264 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13265 }
13266
13267 if (linear_variable_step_check)
13268 for (pc = &clauses, c = clauses; c ; c = *pc)
13269 {
13270 bool remove = false;
13271 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13272 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13273 && !bitmap_bit_p (&map_head,
13274 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13275 {
13276 error_at (OMP_CLAUSE_LOCATION (c),
13277 "%<linear%> clause step is a parameter %qD not "
13278 "specified in %<uniform%> clause",
13279 OMP_CLAUSE_LINEAR_STEP (c));
13280 remove = true;
13281 }
13282
13283 if (remove)
13284 *pc = OMP_CLAUSE_CHAIN (c);
13285 else
13286 pc = &OMP_CLAUSE_CHAIN (c);
13287 }
13288
953ff289
DN
13289 bitmap_obstack_release (NULL);
13290 return clauses;
13291}
9ae165a0 13292
0a35513e
AH
13293/* Create a transaction node. */
13294
13295tree
13296c_finish_transaction (location_t loc, tree block, int flags)
13297{
13298 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13299 if (flags & TM_STMT_ATTR_OUTER)
13300 TRANSACTION_EXPR_OUTER (stmt) = 1;
13301 if (flags & TM_STMT_ATTR_RELAXED)
13302 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13303 return add_stmt (stmt);
13304}
13305
9ae165a0
DG
13306/* Make a variant type in the proper way for C/C++, propagating qualifiers
13307 down to the element type of an array. */
13308
13309tree
13310c_build_qualified_type (tree type, int type_quals)
13311{
13312 if (type == error_mark_node)
13313 return type;
13314
13315 if (TREE_CODE (type) == ARRAY_TYPE)
13316 {
13317 tree t;
13318 tree element_type = c_build_qualified_type (TREE_TYPE (type),
13319 type_quals);
13320
13321 /* See if we already have an identically qualified type. */
13322 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13323 {
13324 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13325 && TYPE_NAME (t) == TYPE_NAME (type)
13326 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13327 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13328 TYPE_ATTRIBUTES (type)))
13329 break;
13330 }
13331 if (!t)
13332 {
13333 tree domain = TYPE_DOMAIN (type);
13334
13335 t = build_variant_type_copy (type);
13336 TREE_TYPE (t) = element_type;
13337
13338 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13339 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13340 SET_TYPE_STRUCTURAL_EQUALITY (t);
13341 else if (TYPE_CANONICAL (element_type) != element_type
13342 || (domain && TYPE_CANONICAL (domain) != domain))
13343 {
b8698a0f 13344 tree unqualified_canon
9ae165a0 13345 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13346 domain? TYPE_CANONICAL (domain)
9ae165a0 13347 : NULL_TREE);
ee45a32d
EB
13348 if (TYPE_REVERSE_STORAGE_ORDER (type))
13349 {
13350 unqualified_canon
13351 = build_distinct_type_copy (unqualified_canon);
13352 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13353 }
b8698a0f 13354 TYPE_CANONICAL (t)
9ae165a0
DG
13355 = c_build_qualified_type (unqualified_canon, type_quals);
13356 }
13357 else
13358 TYPE_CANONICAL (t) = t;
13359 }
13360 return t;
13361 }
13362
13363 /* A restrict-qualified pointer type must be a pointer to object or
13364 incomplete type. Note that the use of POINTER_TYPE_P also allows
13365 REFERENCE_TYPEs, which is appropriate for C++. */
13366 if ((type_quals & TYPE_QUAL_RESTRICT)
13367 && (!POINTER_TYPE_P (type)
13368 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13369 {
13370 error ("invalid use of %<restrict%>");
13371 type_quals &= ~TYPE_QUAL_RESTRICT;
13372 }
13373
e922069e
JW
13374 tree var_type = build_qualified_type (type, type_quals);
13375 /* A variant type does not inherit the list of incomplete vars from the
13376 type main variant. */
3e636daf 13377 if (RECORD_OR_UNION_TYPE_P (var_type))
e922069e
JW
13378 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13379 return var_type;
9ae165a0 13380}
72b5577d
ILT
13381
13382/* Build a VA_ARG_EXPR for the C parser. */
13383
13384tree
c2255bc4 13385c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d 13386{
ec3fba51
MP
13387 if (error_operand_p (type))
13388 return error_mark_node;
13389 else if (!COMPLETE_TYPE_P (type))
4e81b788 13390 {
ec3fba51
MP
13391 error_at (loc, "second argument to %<va_arg%> is of incomplete "
13392 "type %qT", type);
4e81b788
MP
13393 return error_mark_node;
13394 }
ec3fba51
MP
13395 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
13396 warning_at (loc, OPT_Wc___compat,
13397 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 13398 return build_va_arg (loc, expr, type);
72b5577d 13399}
acf0174b
JJ
13400
13401/* Return truthvalue of whether T1 is the same tree structure as T2.
13402 Return 1 if they are the same. Return 0 if they are different. */
13403
13404bool
13405c_tree_equal (tree t1, tree t2)
13406{
13407 enum tree_code code1, code2;
13408
13409 if (t1 == t2)
13410 return true;
13411 if (!t1 || !t2)
13412 return false;
13413
13414 for (code1 = TREE_CODE (t1);
13415 CONVERT_EXPR_CODE_P (code1)
13416 || code1 == NON_LVALUE_EXPR;
13417 code1 = TREE_CODE (t1))
13418 t1 = TREE_OPERAND (t1, 0);
13419 for (code2 = TREE_CODE (t2);
13420 CONVERT_EXPR_CODE_P (code2)
13421 || code2 == NON_LVALUE_EXPR;
13422 code2 = TREE_CODE (t2))
13423 t2 = TREE_OPERAND (t2, 0);
13424
13425 /* They might have become equal now. */
13426 if (t1 == t2)
13427 return true;
13428
13429 if (code1 != code2)
13430 return false;
13431
13432 switch (code1)
13433 {
13434 case INTEGER_CST:
807e902e 13435 return wi::eq_p (t1, t2);
acf0174b
JJ
13436
13437 case REAL_CST:
624d31fe 13438 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13439
13440 case STRING_CST:
13441 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13442 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13443 TREE_STRING_LENGTH (t1));
13444
13445 case FIXED_CST:
13446 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13447 TREE_FIXED_CST (t2));
13448
13449 case COMPLEX_CST:
13450 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13451 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13452
13453 case VECTOR_CST:
13454 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13455
13456 case CONSTRUCTOR:
13457 /* We need to do this when determining whether or not two
13458 non-type pointer to member function template arguments
13459 are the same. */
13460 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13461 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13462 return false;
13463 {
13464 tree field, value;
13465 unsigned int i;
13466 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13467 {
13468 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13469 if (!c_tree_equal (field, elt2->index)
13470 || !c_tree_equal (value, elt2->value))
13471 return false;
13472 }
13473 }
13474 return true;
13475
13476 case TREE_LIST:
13477 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13478 return false;
13479 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13480 return false;
13481 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13482
13483 case SAVE_EXPR:
13484 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13485
13486 case CALL_EXPR:
13487 {
13488 tree arg1, arg2;
13489 call_expr_arg_iterator iter1, iter2;
13490 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13491 return false;
13492 for (arg1 = first_call_expr_arg (t1, &iter1),
13493 arg2 = first_call_expr_arg (t2, &iter2);
13494 arg1 && arg2;
13495 arg1 = next_call_expr_arg (&iter1),
13496 arg2 = next_call_expr_arg (&iter2))
13497 if (!c_tree_equal (arg1, arg2))
13498 return false;
13499 if (arg1 || arg2)
13500 return false;
13501 return true;
13502 }
13503
13504 case TARGET_EXPR:
13505 {
13506 tree o1 = TREE_OPERAND (t1, 0);
13507 tree o2 = TREE_OPERAND (t2, 0);
13508
13509 /* Special case: if either target is an unallocated VAR_DECL,
13510 it means that it's going to be unified with whatever the
13511 TARGET_EXPR is really supposed to initialize, so treat it
13512 as being equivalent to anything. */
0ae9bd27 13513 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
13514 && !DECL_RTL_SET_P (o1))
13515 /*Nop*/;
0ae9bd27 13516 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
13517 && !DECL_RTL_SET_P (o2))
13518 /*Nop*/;
13519 else if (!c_tree_equal (o1, o2))
13520 return false;
13521
13522 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13523 }
13524
13525 case COMPONENT_REF:
13526 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13527 return false;
13528 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13529
13530 case PARM_DECL:
13531 case VAR_DECL:
13532 case CONST_DECL:
13533 case FIELD_DECL:
13534 case FUNCTION_DECL:
13535 case IDENTIFIER_NODE:
13536 case SSA_NAME:
13537 return false;
13538
13539 case TREE_VEC:
13540 {
13541 unsigned ix;
13542 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13543 return false;
13544 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13545 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13546 TREE_VEC_ELT (t2, ix)))
13547 return false;
13548 return true;
13549 }
13550
13551 default:
13552 break;
13553 }
13554
13555 switch (TREE_CODE_CLASS (code1))
13556 {
13557 case tcc_unary:
13558 case tcc_binary:
13559 case tcc_comparison:
13560 case tcc_expression:
13561 case tcc_vl_exp:
13562 case tcc_reference:
13563 case tcc_statement:
13564 {
13565 int i, n = TREE_OPERAND_LENGTH (t1);
13566
13567 switch (code1)
13568 {
13569 case PREINCREMENT_EXPR:
13570 case PREDECREMENT_EXPR:
13571 case POSTINCREMENT_EXPR:
13572 case POSTDECREMENT_EXPR:
13573 n = 1;
13574 break;
13575 case ARRAY_REF:
13576 n = 2;
13577 break;
13578 default:
13579 break;
13580 }
13581
13582 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13583 && n != TREE_OPERAND_LENGTH (t2))
13584 return false;
13585
13586 for (i = 0; i < n; ++i)
13587 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13588 return false;
13589
13590 return true;
13591 }
13592
13593 case tcc_type:
13594 return comptypes (t1, t2);
13595 default:
13596 gcc_unreachable ();
13597 }
13598 /* We can get here with --disable-checking. */
13599 return false;
13600}
12893402
BI
13601
13602/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13603 spawn-helper and BODY is the newly created body for FNDECL. */
13604
13605void
13606cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13607{
13608 tree list = alloc_stmt_list ();
13609 tree frame = make_cilk_frame (fndecl);
13610 tree dtor = create_cilk_function_exit (frame, false, true);
13611 add_local_decl (cfun, frame);
13612
13613 DECL_SAVED_TREE (fndecl) = list;
13614 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13615 frame);
13616 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13617 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13618
13619 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13620 append_to_statement_list (detach_expr, &body_list);
13621
13622 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13623 body = fold_build_cleanup_point_expr (void_type_node, body);
13624
13625 append_to_statement_list (body, &body_list);
13626 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13627 body_list, dtor), &list);
13628}
1807ffc1
MS
13629
13630/* Returns true when the function declaration FNDECL is implicit,
13631 introduced as a result of a call to an otherwise undeclared
13632 function, and false otherwise. */
13633
13634bool
13635c_decl_implicit (const_tree fndecl)
13636{
13637 return C_DECL_IMPLICIT (fndecl);
13638}