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