]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
c-common.c (c_common_attribute_table): Handle "omp declare target link" attribute.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
5624e564 2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6 28#include "coretypes.h"
2adfab87
AM
29#include "target.h"
30#include "function.h"
31#include "bitmap.h"
2adfab87
AM
32#include "c-tree.h"
33#include "gimple-expr.h"
34#include "predict.h"
d8a2d370
DN
35#include "stor-layout.h"
36#include "trans-mem.h"
37#include "varasm.h"
38#include "stmt.h"
e57e265b 39#include "langhooks.h"
29cc57cf 40#include "c-lang.h"
ab87f8c8 41#include "intl.h"
325c3691 42#include "tree-iterator.h"
45b0be94 43#include "gimplify.h"
acf0174b 44#include "tree-inline.h"
0645c1a2 45#include "omp-low.h"
61d3ce20 46#include "c-family/c-objc.h"
de5a5fa1 47#include "c-family/c-ubsan.h"
12893402 48#include "cilk.h"
41dbbb37 49#include "gomp-constants.h"
277fe616 50#include "spellcheck.h"
325c3691 51
2ac2f164
JM
52/* Possible cases of implicit bad conversions. Used to select
53 diagnostic messages in convert_for_assignment. */
54enum impl_conv {
55 ic_argpass,
56 ic_assign,
57 ic_init,
58 ic_return
59};
60
bc4b653b
JM
61/* The level of nesting inside "__alignof__". */
62int in_alignof;
63
64/* The level of nesting inside "sizeof". */
65int in_sizeof;
66
67/* The level of nesting inside "typeof". */
68int in_typeof;
400fbf9f 69
1a4049e7
JJ
70/* The argument of last parsed sizeof expression, only to be tested
71 if expr.original_code == SIZEOF_EXPR. */
72tree c_last_sizeof_arg;
73
9bac5cbb
G
74/* Nonzero if we might need to print a "missing braces around
75 initializer" message within this initializer. */
76static int found_missing_braces;
103b7b17 77
bf730f15
RS
78static int require_constant_value;
79static int require_constant_elements;
80
58f9752a 81static bool null_pointer_constant_p (const_tree);
f55ade6e 82static tree qualify_type (tree, tree);
dc5027f4
JM
83static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
84 bool *);
744aa42f 85static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
86static int function_types_compatible_p (const_tree, const_tree, bool *,
87 bool *);
88static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 89static tree lookup_field (tree, tree);
81e5eca8
MP
90static int convert_arguments (location_t, vec<location_t>, tree,
91 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
92 tree);
db3927fb 93static tree pointer_diff (location_t, tree, tree);
68fca595 94static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 95 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
96static tree valid_compound_expr_initializer (tree, tree);
97static void push_string (const char *);
98static void push_member_name (tree);
f55ade6e
AJ
99static int spelling_length (void);
100static char *print_spelling (char *);
96b40f8d 101static void warning_init (location_t, int, const char *);
c2255bc4 102static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
103static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
104 bool, struct obstack *);
a1e3b3d9 105static void output_pending_init_elements (int, struct obstack *);
ea58ef42 106static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 107static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
108static void add_pending_init (location_t, tree, tree, tree, bool,
109 struct obstack *);
a1e3b3d9
LB
110static void set_nonincremental_init (struct obstack *);
111static void set_nonincremental_init_from_string (tree, struct obstack *);
112static tree find_init_member (tree, struct obstack *);
f37acdf9 113static void readonly_warning (tree, enum lvalue_use);
7bd11157 114static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 115static void record_maybe_used_decl (tree);
dc5027f4 116static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
117\f
118/* Return true if EXP is a null pointer constant, false otherwise. */
119
120static bool
58f9752a 121null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
122{
123 /* This should really operate on c_expr structures, but they aren't
124 yet available everywhere required. */
125 tree type = TREE_TYPE (expr);
126 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 127 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
128 && integer_zerop (expr)
129 && (INTEGRAL_TYPE_P (type)
130 || (TREE_CODE (type) == POINTER_TYPE
131 && VOID_TYPE_P (TREE_TYPE (type))
132 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
133}
928c19bb
JM
134
135/* EXPR may appear in an unevaluated part of an integer constant
136 expression, but not in an evaluated part. Wrap it in a
137 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
138 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
139
140static tree
141note_integer_operands (tree expr)
142{
143 tree ret;
144 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
145 {
146 ret = copy_node (expr);
147 TREE_OVERFLOW (ret) = 1;
148 }
149 else
150 {
151 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
152 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
153 }
154 return ret;
155}
156
4d84fe7c
JM
157/* Having checked whether EXPR may appear in an unevaluated part of an
158 integer constant expression and found that it may, remove any
159 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 expression. */
161
162static inline tree
163remove_c_maybe_const_expr (tree expr)
164{
165 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
166 return C_MAYBE_CONST_EXPR_EXPR (expr);
167 else
168 return expr;
169}
170
f13c9b2c
AP
171\f/* This is a cache to hold if two types are compatible or not. */
172
173struct tagged_tu_seen_cache {
174 const struct tagged_tu_seen_cache * next;
58f9752a
KG
175 const_tree t1;
176 const_tree t2;
f13c9b2c
AP
177 /* The return value of tagged_types_tu_compatible_p if we had seen
178 these two types already. */
179 int val;
180};
181
182static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
183static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
184
400fbf9f
JW
185/* Do `exp = require_complete_type (exp);' to make sure exp
186 does not have an incomplete type. (That includes void types.) */
187
188tree
2f6e4e97 189require_complete_type (tree value)
400fbf9f
JW
190{
191 tree type = TREE_TYPE (value);
192
7a0ca710 193 if (error_operand_p (value))
ea0f786b
CB
194 return error_mark_node;
195
400fbf9f 196 /* First, detect a valid value with a complete type. */
d0f062fb 197 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
198 return value;
199
7a228918 200 c_incomplete_type_error (value, type);
400fbf9f
JW
201 return error_mark_node;
202}
203
204/* Print an error message for invalid use of an incomplete type.
205 VALUE is the expression that was used (or 0 if that isn't known)
206 and TYPE is the type that was invalid. */
207
208void
ac7d7749 209c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 210{
400fbf9f
JW
211 /* Avoid duplicate error message. */
212 if (TREE_CODE (type) == ERROR_MARK)
213 return;
214
0ae9bd27 215 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
ac9f18db 216 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
217 else
218 {
219 retry:
220 /* We must print an error message. Be clever about what it says. */
221
222 switch (TREE_CODE (type))
223 {
224 case RECORD_TYPE:
400fbf9f 225 case UNION_TYPE:
400fbf9f 226 case ENUMERAL_TYPE:
400fbf9f
JW
227 break;
228
229 case VOID_TYPE:
230 error ("invalid use of void expression");
231 return;
232
233 case ARRAY_TYPE:
234 if (TYPE_DOMAIN (type))
235 {
fba78abb
RH
236 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
237 {
238 error ("invalid use of flexible array member");
239 return;
240 }
400fbf9f
JW
241 type = TREE_TYPE (type);
242 goto retry;
243 }
244 error ("invalid use of array with unspecified bounds");
245 return;
246
247 default:
366de0ce 248 gcc_unreachable ();
400fbf9f
JW
249 }
250
251 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 252 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
253 else
254 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 255 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
256 }
257}
258
ab393bf1
NB
259/* Given a type, apply default promotions wrt unnamed function
260 arguments and return the new type. */
261
262tree
2f6e4e97 263c_type_promotes_to (tree type)
ab393bf1 264{
267bac10 265 tree ret = NULL_TREE;
ab393bf1 266
267bac10
JM
267 if (TYPE_MAIN_VARIANT (type) == float_type_node)
268 ret = double_type_node;
269 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
270 {
271 /* Preserve unsignedness if not really getting any wider. */
8df83eae 272 if (TYPE_UNSIGNED (type)
c22cacf3 273 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
274 ret = unsigned_type_node;
275 else
276 ret = integer_type_node;
ab393bf1
NB
277 }
278
267bac10
JM
279 if (ret != NULL_TREE)
280 return (TYPE_ATOMIC (type)
281 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
282 : ret);
283
ab393bf1
NB
284 return type;
285}
286
36c5e70a
BE
287/* Return true if between two named address spaces, whether there is a superset
288 named address space that encompasses both address spaces. If there is a
289 superset, return which address space is the superset. */
290
291static bool
292addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
293{
294 if (as1 == as2)
295 {
296 *common = as1;
297 return true;
298 }
299 else if (targetm.addr_space.subset_p (as1, as2))
300 {
301 *common = as2;
302 return true;
303 }
304 else if (targetm.addr_space.subset_p (as2, as1))
305 {
306 *common = as1;
307 return true;
308 }
309 else
310 return false;
311}
312
400fbf9f
JW
313/* Return a variant of TYPE which has all the type qualifiers of LIKE
314 as well as those of TYPE. */
315
316static tree
2f6e4e97 317qualify_type (tree type, tree like)
400fbf9f 318{
36c5e70a
BE
319 addr_space_t as_type = TYPE_ADDR_SPACE (type);
320 addr_space_t as_like = TYPE_ADDR_SPACE (like);
321 addr_space_t as_common;
322
323 /* If the two named address spaces are different, determine the common
324 superset address space. If there isn't one, raise an error. */
325 if (!addr_space_superset (as_type, as_like, &as_common))
326 {
327 as_common = as_type;
328 error ("%qT and %qT are in disjoint named address spaces",
329 type, like);
330 }
331
2f6e4e97 332 return c_build_qualified_type (type,
36c5e70a 333 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 334 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 335 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 336}
52ffd86e
MS
337
338/* Return true iff the given tree T is a variable length array. */
339
340bool
ac7d7749 341c_vla_type_p (const_tree t)
52ffd86e
MS
342{
343 if (TREE_CODE (t) == ARRAY_TYPE
344 && C_TYPE_VARIABLE_SIZE (t))
345 return true;
346 return false;
347}
400fbf9f 348\f
10bc1b1b 349/* Return the composite type of two compatible types.
5305f6d7 350
10bc1b1b
JM
351 We assume that comptypes has already been done and returned
352 nonzero; if that isn't so, this may crash. In particular, we
353 assume that qualifiers match. */
400fbf9f
JW
354
355tree
10bc1b1b 356composite_type (tree t1, tree t2)
400fbf9f 357{
b3694847
SS
358 enum tree_code code1;
359 enum tree_code code2;
4b027d16 360 tree attributes;
400fbf9f
JW
361
362 /* Save time if the two types are the same. */
363
364 if (t1 == t2) return t1;
365
366 /* If one type is nonsense, use the other. */
367 if (t1 == error_mark_node)
368 return t2;
369 if (t2 == error_mark_node)
370 return t1;
371
10bc1b1b
JM
372 code1 = TREE_CODE (t1);
373 code2 = TREE_CODE (t2);
374
d9525bec 375 /* Merge the attributes. */
5fd9b178 376 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 377
10bc1b1b
JM
378 /* If one is an enumerated type and the other is the compatible
379 integer type, the composite type might be either of the two
380 (DR#013 question 3). For consistency, use the enumerated type as
381 the composite type. */
400fbf9f 382
10bc1b1b
JM
383 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
384 return t1;
385 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
386 return t2;
75326e8c 387
366de0ce 388 gcc_assert (code1 == code2);
b6a10c9f 389
400fbf9f
JW
390 switch (code1)
391 {
400fbf9f 392 case POINTER_TYPE:
10bc1b1b 393 /* For two pointers, do this recursively on the target type. */
400fbf9f 394 {
3932261a
MM
395 tree pointed_to_1 = TREE_TYPE (t1);
396 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 397 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 398 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
399 t1 = build_type_attribute_variant (t1, attributes);
400 return qualify_type (t1, t2);
400fbf9f 401 }
400fbf9f
JW
402
403 case ARRAY_TYPE:
404 {
10bc1b1b 405 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
406 int quals;
407 tree unqual_elt;
ca8bdb78
JM
408 tree d1 = TYPE_DOMAIN (t1);
409 tree d2 = TYPE_DOMAIN (t2);
410 bool d1_variable, d2_variable;
411 bool d1_zero, d2_zero;
f6294de7 412 bool t1_complete, t2_complete;
46df2823 413
de46b2fe 414 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
415 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
416 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 417
f6294de7
JM
418 t1_complete = COMPLETE_TYPE_P (t1);
419 t2_complete = COMPLETE_TYPE_P (t2);
420
ca8bdb78
JM
421 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
422 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
423
424 d1_variable = (!d1_zero
425 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
426 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
427 d2_variable = (!d2_zero
428 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
429 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
430 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
431 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 432
400fbf9f 433 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
434 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
435 && (d2_variable || d2_zero || !d1_variable))
4b027d16 436 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
437 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
438 && (d1_variable || d1_zero || !d2_variable))
4b027d16 439 return build_type_attribute_variant (t2, attributes);
c22cacf3 440
de46b2fe
AP
441 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
442 return build_type_attribute_variant (t1, attributes);
443 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
444 return build_type_attribute_variant (t2, attributes);
c22cacf3 445
46df2823
JM
446 /* Merge the element types, and have a size if either arg has
447 one. We may have qualifiers on the element types. To set
448 up TYPE_MAIN_VARIANT correctly, we need to form the
449 composite of the unqualified types and add the qualifiers
450 back at the end. */
451 quals = TYPE_QUALS (strip_array_types (elt));
452 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
453 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
454 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
455 && (d2_variable
456 || d2_zero
457 || !d1_variable))
458 ? t1
459 : t2));
f6294de7
JM
460 /* Ensure a composite type involving a zero-length array type
461 is a zero-length type not an incomplete type. */
462 if (d1_zero && d2_zero
463 && (t1_complete || t2_complete)
464 && !COMPLETE_TYPE_P (t1))
465 {
466 TYPE_SIZE (t1) = bitsize_zero_node;
467 TYPE_SIZE_UNIT (t1) = size_zero_node;
468 }
46df2823 469 t1 = c_build_qualified_type (t1, quals);
de46b2fe 470 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
471 }
472
fcb99e7b
JJ
473 case ENUMERAL_TYPE:
474 case RECORD_TYPE:
475 case UNION_TYPE:
476 if (attributes != NULL)
477 {
478 /* Try harder not to create a new aggregate type. */
479 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
480 return t1;
481 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
482 return t2;
483 }
484 return build_type_attribute_variant (t1, attributes);
485
400fbf9f
JW
486 case FUNCTION_TYPE:
487 /* Function types: prefer the one that specified arg types.
488 If both do, merge the arg types. Also merge the return types. */
489 {
10bc1b1b 490 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
491 tree p1 = TYPE_ARG_TYPES (t1);
492 tree p2 = TYPE_ARG_TYPES (t2);
493 int len;
494 tree newargs, n;
495 int i;
496
497 /* Save space: see if the result is identical to one of the args. */
3f75a254 498 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 499 return build_type_attribute_variant (t1, attributes);
3f75a254 500 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 501 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
502
503 /* Simple way if one arg fails to specify argument types. */
504 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 505 {
fe7080d2
AP
506 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
507 t1 = build_type_attribute_variant (t1, attributes);
508 return qualify_type (t1, t2);
4b027d16 509 }
400fbf9f 510 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
511 {
512 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
513 t1 = build_type_attribute_variant (t1, attributes);
514 return qualify_type (t1, t2);
4b027d16 515 }
400fbf9f
JW
516
517 /* If both args specify argument types, we must merge the two
518 lists, argument by argument. */
2f4e8f2b 519
400fbf9f
JW
520 len = list_length (p1);
521 newargs = 0;
522
523 for (i = 0; i < len; i++)
8d9bfdc5 524 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
525
526 n = newargs;
527
528 for (; p1;
529 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
530 {
531 /* A null type means arg type is not specified.
532 Take whatever the other function type has. */
533 if (TREE_VALUE (p1) == 0)
534 {
535 TREE_VALUE (n) = TREE_VALUE (p2);
536 goto parm_done;
537 }
538 if (TREE_VALUE (p2) == 0)
539 {
540 TREE_VALUE (n) = TREE_VALUE (p1);
541 goto parm_done;
542 }
2f6e4e97 543
400fbf9f
JW
544 /* Given wait (union {union wait *u; int *i} *)
545 and wait (union wait *),
546 prefer union wait * as type of parm. */
547 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
548 && TREE_VALUE (p1) != TREE_VALUE (p2))
549 {
550 tree memb;
58cb41e6
JJ
551 tree mv2 = TREE_VALUE (p2);
552 if (mv2 && mv2 != error_mark_node
553 && TREE_CODE (mv2) != ARRAY_TYPE)
554 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 555 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 556 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
557 {
558 tree mv3 = TREE_TYPE (memb);
559 if (mv3 && mv3 != error_mark_node
560 && TREE_CODE (mv3) != ARRAY_TYPE)
561 mv3 = TYPE_MAIN_VARIANT (mv3);
562 if (comptypes (mv3, mv2))
563 {
564 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
565 TREE_VALUE (p2));
c1771a20 566 pedwarn (input_location, OPT_Wpedantic,
fcf73884 567 "function types not truly compatible in ISO C");
58cb41e6
JJ
568 goto parm_done;
569 }
570 }
400fbf9f
JW
571 }
572 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
573 && TREE_VALUE (p2) != TREE_VALUE (p1))
574 {
575 tree memb;
58cb41e6
JJ
576 tree mv1 = TREE_VALUE (p1);
577 if (mv1 && mv1 != error_mark_node
578 && TREE_CODE (mv1) != ARRAY_TYPE)
579 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 580 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 581 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
582 {
583 tree mv3 = TREE_TYPE (memb);
584 if (mv3 && mv3 != error_mark_node
585 && TREE_CODE (mv3) != ARRAY_TYPE)
586 mv3 = TYPE_MAIN_VARIANT (mv3);
587 if (comptypes (mv3, mv1))
588 {
589 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
590 TREE_VALUE (p1));
c1771a20 591 pedwarn (input_location, OPT_Wpedantic,
fcf73884 592 "function types not truly compatible in ISO C");
58cb41e6
JJ
593 goto parm_done;
594 }
595 }
400fbf9f 596 }
10bc1b1b 597 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
598 parm_done: ;
599 }
600
4b027d16 601 t1 = build_function_type (valtype, newargs);
fe7080d2 602 t1 = qualify_type (t1, t2);
0f41302f 603 /* ... falls through ... */
400fbf9f
JW
604 }
605
606 default:
4b027d16 607 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
608 }
609
610}
10bc1b1b
JM
611
612/* Return the type of a conditional expression between pointers to
613 possibly differently qualified versions of compatible types.
614
615 We assume that comp_target_types has already been done and returned
616 nonzero; if that isn't so, this may crash. */
617
618static tree
619common_pointer_type (tree t1, tree t2)
620{
621 tree attributes;
46df2823
JM
622 tree pointed_to_1, mv1;
623 tree pointed_to_2, mv2;
10bc1b1b 624 tree target;
eb1387a0 625 unsigned target_quals;
36c5e70a
BE
626 addr_space_t as1, as2, as_common;
627 int quals1, quals2;
10bc1b1b
JM
628
629 /* Save time if the two types are the same. */
630
631 if (t1 == t2) return t1;
632
633 /* If one type is nonsense, use the other. */
634 if (t1 == error_mark_node)
635 return t2;
636 if (t2 == error_mark_node)
637 return t1;
638
366de0ce 639 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 640 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
641
642 /* Merge the attributes. */
643 attributes = targetm.merge_type_attributes (t1, t2);
644
645 /* Find the composite type of the target types, and combine the
46df2823
JM
646 qualifiers of the two types' targets. Do not lose qualifiers on
647 array element types by taking the TYPE_MAIN_VARIANT. */
648 mv1 = pointed_to_1 = TREE_TYPE (t1);
649 mv2 = pointed_to_2 = TREE_TYPE (t2);
650 if (TREE_CODE (mv1) != ARRAY_TYPE)
651 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
652 if (TREE_CODE (mv2) != ARRAY_TYPE)
653 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
654 target = composite_type (mv1, mv2);
eb1387a0 655
768952be
MU
656 /* Strip array types to get correct qualifier for pointers to arrays */
657 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
658 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
659
eb1387a0
RG
660 /* For function types do not merge const qualifiers, but drop them
661 if used inconsistently. The middle-end uses these to mark const
662 and noreturn functions. */
663 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 664 target_quals = (quals1 & quals2);
eb1387a0 665 else
36c5e70a
BE
666 target_quals = (quals1 | quals2);
667
668 /* If the two named address spaces are different, determine the common
669 superset address space. This is guaranteed to exist due to the
670 assumption that comp_target_type returned non-zero. */
671 as1 = TYPE_ADDR_SPACE (pointed_to_1);
672 as2 = TYPE_ADDR_SPACE (pointed_to_2);
673 if (!addr_space_superset (as1, as2, &as_common))
674 gcc_unreachable ();
675
676 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
677
eb1387a0 678 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
679 return build_type_attribute_variant (t1, attributes);
680}
681
682/* Return the common type for two arithmetic types under the usual
683 arithmetic conversions. The default conversions have already been
684 applied, and enumerated types converted to their compatible integer
685 types. The resulting type is unqualified and has no attributes.
686
687 This is the type for the result of most arithmetic operations
688 if the operands have the given two types. */
689
ccf7f880
JJ
690static tree
691c_common_type (tree t1, tree t2)
10bc1b1b
JM
692{
693 enum tree_code code1;
694 enum tree_code code2;
695
696 /* If one type is nonsense, use the other. */
697 if (t1 == error_mark_node)
698 return t2;
699 if (t2 == error_mark_node)
700 return t1;
701
702 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
703 t1 = TYPE_MAIN_VARIANT (t1);
704
705 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
706 t2 = TYPE_MAIN_VARIANT (t2);
707
708 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
709 t1 = build_type_attribute_variant (t1, NULL_TREE);
710
711 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
712 t2 = build_type_attribute_variant (t2, NULL_TREE);
713
714 /* Save time if the two types are the same. */
715
716 if (t1 == t2) return t1;
717
718 code1 = TREE_CODE (t1);
719 code2 = TREE_CODE (t2);
720
366de0ce 721 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
722 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
723 || code1 == INTEGER_TYPE);
366de0ce 724 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
725 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
726 || code2 == INTEGER_TYPE);
10bc1b1b 727
5fc89bfd
JJ
728 /* When one operand is a decimal float type, the other operand cannot be
729 a generic float type or a complex type. We also disallow vector types
730 here. */
731 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
732 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
733 {
734 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
735 {
736 error ("can%'t mix operands of decimal float and vector types");
737 return error_mark_node;
738 }
739 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
740 {
741 error ("can%'t mix operands of decimal float and complex types");
742 return error_mark_node;
743 }
744 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and other float types");
747 return error_mark_node;
748 }
749 }
750
10bc1b1b
JM
751 /* If one type is a vector type, return that type. (How the usual
752 arithmetic conversions apply to the vector types extension is not
753 precisely specified.) */
754 if (code1 == VECTOR_TYPE)
755 return t1;
756
757 if (code2 == VECTOR_TYPE)
758 return t2;
759
760 /* If one type is complex, form the common type of the non-complex
761 components, then make that complex. Use T1 or T2 if it is the
762 required type. */
763 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
764 {
765 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
766 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 767 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
768
769 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
770 return t1;
771 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
772 return t2;
773 else
774 return build_complex_type (subtype);
775 }
776
777 /* If only one is real, use it as the result. */
778
779 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
780 return t1;
781
782 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
783 return t2;
784
9a8ce21f
JG
785 /* If both are real and either are decimal floating point types, use
786 the decimal floating point type with the greater precision. */
787
788 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
789 {
790 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
791 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
792 return dfloat128_type_node;
793 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
794 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
795 return dfloat64_type_node;
796 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
798 return dfloat32_type_node;
799 }
800
ab22c1fa
CF
801 /* Deal with fixed-point types. */
802 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
803 {
804 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 805 machine_mode m1, m2;
ab22c1fa
CF
806 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
807
808 m1 = TYPE_MODE (t1);
809 m2 = TYPE_MODE (t2);
810
811 /* If one input type is saturating, the result type is saturating. */
812 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
813 satp = 1;
814
815 /* If both fixed-point types are unsigned, the result type is unsigned.
816 When mixing fixed-point and integer types, follow the sign of the
817 fixed-point type.
818 Otherwise, the result type is signed. */
819 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
820 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
821 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
822 && TYPE_UNSIGNED (t1))
823 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
824 && TYPE_UNSIGNED (t2)))
825 unsignedp = 1;
826
827 /* The result type is signed. */
828 if (unsignedp == 0)
829 {
830 /* If the input type is unsigned, we need to convert to the
831 signed type. */
832 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
833 {
d75d71e0 834 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
835 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
836 mclass = MODE_FRACT;
837 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
838 mclass = MODE_ACCUM;
839 else
840 gcc_unreachable ();
841 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
842 }
843 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
844 {
d75d71e0 845 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
846 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
847 mclass = MODE_FRACT;
848 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
849 mclass = MODE_ACCUM;
850 else
851 gcc_unreachable ();
852 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
853 }
854 }
855
856 if (code1 == FIXED_POINT_TYPE)
857 {
858 fbit1 = GET_MODE_FBIT (m1);
859 ibit1 = GET_MODE_IBIT (m1);
860 }
861 else
862 {
863 fbit1 = 0;
864 /* Signed integers need to subtract one sign bit. */
865 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
866 }
867
868 if (code2 == FIXED_POINT_TYPE)
869 {
870 fbit2 = GET_MODE_FBIT (m2);
871 ibit2 = GET_MODE_IBIT (m2);
872 }
873 else
874 {
875 fbit2 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
878 }
879
880 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
881 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
882 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
883 satp);
884 }
885
10bc1b1b
JM
886 /* Both real or both integers; use the one with greater precision. */
887
888 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
889 return t1;
890 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
891 return t2;
892
893 /* Same precision. Prefer long longs to longs to ints when the
894 same precision, following the C99 rules on integer type rank
895 (which are equivalent to the C90 rules for C90 types). */
896
897 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
898 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
899 return long_long_unsigned_type_node;
900
901 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
902 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
903 {
904 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
905 return long_long_unsigned_type_node;
906 else
c22cacf3 907 return long_long_integer_type_node;
10bc1b1b
JM
908 }
909
910 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
911 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
912 return long_unsigned_type_node;
913
914 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
916 {
917 /* But preserve unsignedness from the other type,
918 since long cannot hold all the values of an unsigned int. */
919 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
920 return long_unsigned_type_node;
921 else
922 return long_integer_type_node;
923 }
924
925 /* Likewise, prefer long double to double even if same size. */
926 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
928 return long_double_type_node;
929
2531a1d9
JR
930 /* Likewise, prefer double to float even if same size.
931 We got a couple of embedded targets with 32 bit doubles, and the
932 pdp11 might have 64 bit floats. */
933 if (TYPE_MAIN_VARIANT (t1) == double_type_node
934 || TYPE_MAIN_VARIANT (t2) == double_type_node)
935 return double_type_node;
936
10bc1b1b
JM
937 /* Otherwise prefer the unsigned one. */
938
939 if (TYPE_UNSIGNED (t1))
940 return t1;
941 else
942 return t2;
943}
400fbf9f 944\f
5922c215
JM
945/* Wrapper around c_common_type that is used by c-common.c and other
946 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
947 are allowed here and are converted to their compatible integer types.
948 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
949 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
950tree
951common_type (tree t1, tree t2)
952{
953 if (TREE_CODE (t1) == ENUMERAL_TYPE)
954 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
955 if (TREE_CODE (t2) == ENUMERAL_TYPE)
956 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
957
958 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
959 if (TREE_CODE (t1) == BOOLEAN_TYPE
960 && TREE_CODE (t2) == BOOLEAN_TYPE)
961 return boolean_type_node;
962
963 /* If either type is BOOLEAN_TYPE, then return the other. */
964 if (TREE_CODE (t1) == BOOLEAN_TYPE)
965 return t2;
966 if (TREE_CODE (t2) == BOOLEAN_TYPE)
967 return t1;
968
ccf7f880
JJ
969 return c_common_type (t1, t2);
970}
f13c9b2c 971
400fbf9f
JW
972/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
973 or various other operations. Return 2 if they are compatible
974 but a warning may be needed if you use them together. */
975
976int
132da1a5 977comptypes (tree type1, tree type2)
f13c9b2c
AP
978{
979 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
980 int val;
981
dc5027f4 982 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
983 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
984
985 return val;
986}
987
988/* Like comptypes, but if it returns non-zero because enum and int are
989 compatible, it sets *ENUM_AND_INT_P to true. */
990
991static int
992comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
993{
994 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
995 int val;
996
dc5027f4
JM
997 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
998 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
999
1000 return val;
1001}
1002
1003/* Like comptypes, but if it returns nonzero for different types, it
1004 sets *DIFFERENT_TYPES_P to true. */
1005
1006int
1007comptypes_check_different_types (tree type1, tree type2,
1008 bool *different_types_p)
1009{
1010 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1011 int val;
1012
1013 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1014 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1015
f13c9b2c 1016 return val;
c22cacf3
MS
1017}
1018\f
f13c9b2c
AP
1019/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1020 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1021 but a warning may be needed if you use them together. If
1022 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1023 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1024 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1025 NULL, and the types are compatible but different enough not to be
48b0b196 1026 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1027 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1028 false, but may or may not be set if the types are incompatible.
1029 This differs from comptypes, in that we don't free the seen
1030 types. */
f13c9b2c
AP
1031
1032static int
dc5027f4
JM
1033comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1034 bool *different_types_p)
400fbf9f 1035{
58f9752a
KG
1036 const_tree t1 = type1;
1037 const_tree t2 = type2;
4b027d16 1038 int attrval, val;
400fbf9f
JW
1039
1040 /* Suppress errors caused by previously reported errors. */
1041
8d47dfc5
RH
1042 if (t1 == t2 || !t1 || !t2
1043 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1044 return 1;
1045
bca63328
JM
1046 /* Enumerated types are compatible with integer types, but this is
1047 not transitive: two enumerated types in the same translation unit
1048 are compatible with each other only if they are the same type. */
400fbf9f 1049
bca63328 1050 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1051 {
1052 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1053 if (TREE_CODE (t2) != VOID_TYPE)
1054 {
1055 if (enum_and_int_p != NULL)
1056 *enum_and_int_p = true;
1057 if (different_types_p != NULL)
1058 *different_types_p = true;
1059 }
744aa42f 1060 }
bca63328 1061 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1062 {
1063 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1064 if (TREE_CODE (t1) != VOID_TYPE)
1065 {
1066 if (enum_and_int_p != NULL)
1067 *enum_and_int_p = true;
1068 if (different_types_p != NULL)
1069 *different_types_p = true;
1070 }
744aa42f 1071 }
400fbf9f
JW
1072
1073 if (t1 == t2)
1074 return 1;
1075
1076 /* Different classes of types can't be compatible. */
1077
3aeb3655
EC
1078 if (TREE_CODE (t1) != TREE_CODE (t2))
1079 return 0;
400fbf9f 1080
118a3a8b 1081 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1082
3932261a 1083 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1084 return 0;
1085
08632da2
RS
1086 /* Allow for two different type nodes which have essentially the same
1087 definition. Note that we already checked for equality of the type
38e01259 1088 qualifiers (just above). */
400fbf9f 1089
46df2823
JM
1090 if (TREE_CODE (t1) != ARRAY_TYPE
1091 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1092 return 1;
1093
4b027d16 1094 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1095 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1096 return 0;
1097
1098 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1099 val = 0;
1100
400fbf9f
JW
1101 switch (TREE_CODE (t1))
1102 {
1103 case POINTER_TYPE:
106f5de5
UW
1104 /* Do not remove mode or aliasing information. */
1105 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1106 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1107 break;
4b027d16 1108 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1109 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1110 enum_and_int_p, different_types_p));
4b027d16 1111 break;
400fbf9f
JW
1112
1113 case FUNCTION_TYPE:
dc5027f4
JM
1114 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1115 different_types_p);
4b027d16 1116 break;
400fbf9f
JW
1117
1118 case ARRAY_TYPE:
1119 {
400fbf9f
JW
1120 tree d1 = TYPE_DOMAIN (t1);
1121 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1122 bool d1_variable, d2_variable;
1123 bool d1_zero, d2_zero;
4b027d16 1124 val = 1;
400fbf9f
JW
1125
1126 /* Target types must match incl. qualifiers. */
1127 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1128 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1129 enum_and_int_p,
1130 different_types_p)))
400fbf9f
JW
1131 return 0;
1132
dc5027f4
JM
1133 if (different_types_p != NULL
1134 && (d1 == 0) != (d2 == 0))
1135 *different_types_p = true;
400fbf9f 1136 /* Sizes must match unless one is missing or variable. */
3f85558f 1137 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1138 break;
400fbf9f 1139
3f75a254
JM
1140 d1_zero = !TYPE_MAX_VALUE (d1);
1141 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1142
3f75a254 1143 d1_variable = (!d1_zero
3f85558f
RH
1144 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1145 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1146 d2_variable = (!d2_zero
3f85558f
RH
1147 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1148 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1149 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1150 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1151
dc5027f4
JM
1152 if (different_types_p != NULL
1153 && d1_variable != d2_variable)
1154 *different_types_p = true;
3f85558f
RH
1155 if (d1_variable || d2_variable)
1156 break;
1157 if (d1_zero && d2_zero)
1158 break;
1159 if (d1_zero || d2_zero
3f75a254
JM
1160 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1161 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1162 val = 0;
1163
c22cacf3 1164 break;
400fbf9f
JW
1165 }
1166
d1bd0ded 1167 case ENUMERAL_TYPE:
58393038 1168 case RECORD_TYPE:
d1bd0ded 1169 case UNION_TYPE:
766beae1 1170 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1171 {
fcb99e7b
JJ
1172 tree a1 = TYPE_ATTRIBUTES (t1);
1173 tree a2 = TYPE_ATTRIBUTES (t2);
1174
1175 if (! attribute_list_contained (a1, a2)
1176 && ! attribute_list_contained (a2, a1))
1177 break;
1178
f13c9b2c 1179 if (attrval != 2)
dc5027f4
JM
1180 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1181 different_types_p);
1182 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1183 different_types_p);
f13c9b2c 1184 }
4b027d16 1185 break;
e9a25f70 1186
62e1dfcf 1187 case VECTOR_TYPE:
744aa42f
ILT
1188 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1189 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1190 enum_and_int_p, different_types_p));
62e1dfcf
NC
1191 break;
1192
e9a25f70
JL
1193 default:
1194 break;
400fbf9f 1195 }
4b027d16 1196 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1197}
1198
36c5e70a
BE
1199/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1200 their qualifiers, except for named address spaces. If the pointers point to
1201 different named addresses, then we must determine if one address space is a
1202 subset of the other. */
400fbf9f
JW
1203
1204static int
744aa42f 1205comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1206{
392202b0 1207 int val;
768952be 1208 int val_ped;
36c5e70a
BE
1209 tree mvl = TREE_TYPE (ttl);
1210 tree mvr = TREE_TYPE (ttr);
1211 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1212 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1213 addr_space_t as_common;
744aa42f 1214 bool enum_and_int_p;
8b40563c 1215
36c5e70a
BE
1216 /* Fail if pointers point to incompatible address spaces. */
1217 if (!addr_space_superset (asl, asr, &as_common))
1218 return 0;
1219
768952be
MU
1220 /* For pedantic record result of comptypes on arrays before losing
1221 qualifiers on the element type below. */
1222 val_ped = 1;
1223
1224 if (TREE_CODE (mvl) == ARRAY_TYPE
1225 && TREE_CODE (mvr) == ARRAY_TYPE)
1226 val_ped = comptypes (mvl, mvr);
1227
1228 /* Qualifiers on element types of array types that are
1229 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1230
1231 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1232 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1233 : TYPE_MAIN_VARIANT (mvl));
1234
1235 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1236 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1237 : TYPE_MAIN_VARIANT (mvr));
1238
744aa42f
ILT
1239 enum_and_int_p = false;
1240 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1241
768952be
MU
1242 if (val == 1 && val_ped != 1)
1243 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1244 "are incompatible in ISO C");
1245
fcf73884 1246 if (val == 2)
c1771a20 1247 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1248
1249 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1250 warning_at (location, OPT_Wc___compat,
1251 "pointer target types incompatible in C++");
1252
400fbf9f
JW
1253 return val;
1254}
1255\f
1256/* Subroutines of `comptypes'. */
1257
f75fbaf7
ZW
1258/* Determine whether two trees derive from the same translation unit.
1259 If the CONTEXT chain ends in a null, that tree's context is still
1260 being parsed, so if two trees have context chains ending in null,
766beae1 1261 they're in the same translation unit. */
f75fbaf7 1262int
58f9752a 1263same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1264{
1265 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1266 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1267 {
6615c446
JO
1268 case tcc_declaration:
1269 t1 = DECL_CONTEXT (t1); break;
1270 case tcc_type:
1271 t1 = TYPE_CONTEXT (t1); break;
1272 case tcc_exceptional:
1273 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1274 default: gcc_unreachable ();
766beae1
ZW
1275 }
1276
1277 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1278 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1279 {
6615c446
JO
1280 case tcc_declaration:
1281 t2 = DECL_CONTEXT (t2); break;
1282 case tcc_type:
1283 t2 = TYPE_CONTEXT (t2); break;
1284 case tcc_exceptional:
1285 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1286 default: gcc_unreachable ();
766beae1
ZW
1287 }
1288
1289 return t1 == t2;
1290}
1291
f13c9b2c 1292/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1293
f13c9b2c 1294static struct tagged_tu_seen_cache *
58f9752a 1295alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1296{
cceb1885 1297 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1298 tu->next = tagged_tu_seen_base;
1299 tu->t1 = t1;
1300 tu->t2 = t2;
c22cacf3 1301
f13c9b2c 1302 tagged_tu_seen_base = tu;
c22cacf3 1303
f13c9b2c
AP
1304 /* The C standard says that two structures in different translation
1305 units are compatible with each other only if the types of their
1306 fields are compatible (among other things). We assume that they
1307 are compatible until proven otherwise when building the cache.
1308 An example where this can occur is:
1309 struct a
1310 {
1311 struct a *next;
1312 };
1313 If we are comparing this against a similar struct in another TU,
c83eecad 1314 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1315 loop. */
1316 tu->val = 1;
1317 return tu;
1318}
d1bd0ded 1319
f13c9b2c 1320/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1321
f13c9b2c
AP
1322static void
1323free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1324{
1325 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1326 while (tu != tu_til)
1327 {
741ac903
KG
1328 const struct tagged_tu_seen_cache *const tu1
1329 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1330 tu = tu1->next;
b1d5455a 1331 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1332 }
1333 tagged_tu_seen_base = tu_til;
1334}
d1bd0ded
GK
1335
1336/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1337 compatible. If the two types are not the same (which has been
1338 checked earlier), this can only happen when multiple translation
1339 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1340 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1341 comptypes_internal. */
d1bd0ded
GK
1342
1343static int
744aa42f 1344tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1345 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1346{
1347 tree s1, s2;
1348 bool needs_warning = false;
3aeb3655 1349
d1bd0ded
GK
1350 /* We have to verify that the tags of the types are the same. This
1351 is harder than it looks because this may be a typedef, so we have
1352 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1353 typedef...
1354 In the case of compiler-created builtin structs the TYPE_DECL
1355 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1356 while (TYPE_NAME (t1)
1357 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1358 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1359 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1360
dea984dc
ILT
1361 while (TYPE_NAME (t2)
1362 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1363 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1364 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1365
1366 /* C90 didn't have the requirement that the two tags be the same. */
1367 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1368 return 0;
3aeb3655 1369
d1bd0ded
GK
1370 /* C90 didn't say what happened if one or both of the types were
1371 incomplete; we choose to follow C99 rules here, which is that they
1372 are compatible. */
1373 if (TYPE_SIZE (t1) == NULL
1374 || TYPE_SIZE (t2) == NULL)
1375 return 1;
3aeb3655 1376
d1bd0ded 1377 {
f13c9b2c 1378 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1379 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1380 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1381 return tts_i->val;
d1bd0ded 1382 }
3aeb3655 1383
d1bd0ded
GK
1384 switch (TREE_CODE (t1))
1385 {
1386 case ENUMERAL_TYPE:
1387 {
f13c9b2c 1388 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1389 /* Speed up the case where the type values are in the same order. */
1390 tree tv1 = TYPE_VALUES (t1);
1391 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1392
c22cacf3 1393 if (tv1 == tv2)
f13c9b2c
AP
1394 {
1395 return 1;
1396 }
3aeb3655 1397
c22cacf3
MS
1398 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1399 {
1400 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1401 break;
1402 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1403 {
c22cacf3 1404 tu->val = 0;
f13c9b2c
AP
1405 return 0;
1406 }
c22cacf3 1407 }
3aeb3655 1408
c22cacf3 1409 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1410 {
1411 return 1;
1412 }
c22cacf3 1413 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1414 {
1415 tu->val = 0;
1416 return 0;
1417 }
3aeb3655 1418
d1bd0ded 1419 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1420 {
1421 tu->val = 0;
1422 return 0;
1423 }
3aeb3655 1424
d1bd0ded
GK
1425 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1426 {
1427 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1428 if (s2 == NULL
1429 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1430 {
1431 tu->val = 0;
1432 return 0;
1433 }
d1bd0ded
GK
1434 }
1435 return 1;
1436 }
1437
1438 case UNION_TYPE:
1439 {
f13c9b2c 1440 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1441 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1442 {
1443 tu->val = 0;
1444 return 0;
1445 }
c22cacf3 1446
f13c9b2c
AP
1447 /* Speed up the common case where the fields are in the same order. */
1448 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1449 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1450 {
1451 int result;
c22cacf3 1452
3ae4d3cc 1453 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1454 break;
744aa42f 1455 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1456 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1457
1458 if (result != 1 && !DECL_NAME (s1))
1459 break;
f13c9b2c
AP
1460 if (result == 0)
1461 {
1462 tu->val = 0;
1463 return 0;
1464 }
1465 if (result == 2)
1466 needs_warning = true;
1467
1468 if (TREE_CODE (s1) == FIELD_DECL
1469 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1470 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1471 {
1472 tu->val = 0;
1473 return 0;
1474 }
1475 }
1476 if (!s1 && !s2)
1477 {
1478 tu->val = needs_warning ? 2 : 1;
1479 return tu->val;
1480 }
d1bd0ded 1481
910ad8de 1482 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1483 {
1484 bool ok = false;
3aeb3655 1485
910ad8de 1486 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1487 if (DECL_NAME (s1) == DECL_NAME (s2))
1488 {
1489 int result;
1490
744aa42f 1491 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1492 enum_and_int_p,
1493 different_types_p);
3ae4d3cc
AO
1494
1495 if (result != 1 && !DECL_NAME (s1))
1496 continue;
1497 if (result == 0)
1498 {
1499 tu->val = 0;
1500 return 0;
1501 }
1502 if (result == 2)
1503 needs_warning = true;
1504
1505 if (TREE_CODE (s1) == FIELD_DECL
1506 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1507 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1508 break;
3ae4d3cc
AO
1509
1510 ok = true;
1511 break;
1512 }
3f75a254 1513 if (!ok)
f13c9b2c
AP
1514 {
1515 tu->val = 0;
1516 return 0;
1517 }
d1bd0ded 1518 }
f13c9b2c
AP
1519 tu->val = needs_warning ? 2 : 10;
1520 return tu->val;
d1bd0ded
GK
1521 }
1522
1523 case RECORD_TYPE:
1524 {
c22cacf3 1525 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1526
1527 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1528 s1 && s2;
910ad8de 1529 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1530 {
1531 int result;
1532 if (TREE_CODE (s1) != TREE_CODE (s2)
1533 || DECL_NAME (s1) != DECL_NAME (s2))
1534 break;
744aa42f 1535 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1536 enum_and_int_p, different_types_p);
d1bd0ded
GK
1537 if (result == 0)
1538 break;
1539 if (result == 2)
1540 needs_warning = true;
3aeb3655 1541
d1bd0ded
GK
1542 if (TREE_CODE (s1) == FIELD_DECL
1543 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1544 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1545 break;
1546 }
d1bd0ded 1547 if (s1 && s2)
f13c9b2c
AP
1548 tu->val = 0;
1549 else
1550 tu->val = needs_warning ? 2 : 1;
1551 return tu->val;
d1bd0ded
GK
1552 }
1553
1554 default:
366de0ce 1555 gcc_unreachable ();
d1bd0ded
GK
1556 }
1557}
1558
400fbf9f
JW
1559/* Return 1 if two function types F1 and F2 are compatible.
1560 If either type specifies no argument types,
1561 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1562 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1563 the other must specify that number of self-promoting arg types.
744aa42f 1564 Otherwise, the argument types must match.
dc5027f4 1565 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1566
1567static int
744aa42f 1568function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1569 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1570{
1571 tree args1, args2;
1572 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1573 int val = 1;
1574 int val1;
a6fdc086
GK
1575 tree ret1, ret2;
1576
1577 ret1 = TREE_TYPE (f1);
1578 ret2 = TREE_TYPE (f2);
1579
e508a019
JM
1580 /* 'volatile' qualifiers on a function's return type used to mean
1581 the function is noreturn. */
1582 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1583 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1584 if (TYPE_VOLATILE (ret1))
1585 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1586 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1587 if (TYPE_VOLATILE (ret2))
1588 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1589 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1590 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1591 if (val == 0)
400fbf9f
JW
1592 return 0;
1593
1594 args1 = TYPE_ARG_TYPES (f1);
1595 args2 = TYPE_ARG_TYPES (f2);
1596
dc5027f4
JM
1597 if (different_types_p != NULL
1598 && (args1 == 0) != (args2 == 0))
1599 *different_types_p = true;
1600
400fbf9f
JW
1601 /* An unspecified parmlist matches any specified parmlist
1602 whose argument types don't need default promotions. */
1603
1604 if (args1 == 0)
1605 {
1606 if (!self_promoting_args_p (args2))
1607 return 0;
1608 /* If one of these types comes from a non-prototype fn definition,
1609 compare that with the other type's arglist.
3176a0c2 1610 If they don't match, ask for a warning (but no error). */
400fbf9f 1611 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1612 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1613 enum_and_int_p, different_types_p))
400fbf9f
JW
1614 val = 2;
1615 return val;
1616 }
1617 if (args2 == 0)
1618 {
1619 if (!self_promoting_args_p (args1))
1620 return 0;
1621 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1622 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1623 enum_and_int_p, different_types_p))
400fbf9f
JW
1624 val = 2;
1625 return val;
1626 }
1627
1628 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1629 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1630 different_types_p);
400fbf9f
JW
1631 return val1 != 1 ? val1 : val;
1632}
1633
744aa42f
ILT
1634/* Check two lists of types for compatibility, returning 0 for
1635 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1636 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1637 comptypes_internal. */
400fbf9f
JW
1638
1639static int
744aa42f 1640type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1641 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1642{
1643 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1644 int val = 1;
9d5f3e49 1645 int newval = 0;
400fbf9f
JW
1646
1647 while (1)
1648 {
46df2823 1649 tree a1, mv1, a2, mv2;
400fbf9f
JW
1650 if (args1 == 0 && args2 == 0)
1651 return val;
1652 /* If one list is shorter than the other,
1653 they fail to match. */
1654 if (args1 == 0 || args2 == 0)
1655 return 0;
46df2823
JM
1656 mv1 = a1 = TREE_VALUE (args1);
1657 mv2 = a2 = TREE_VALUE (args2);
1658 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1659 mv1 = (TYPE_ATOMIC (mv1)
1660 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1661 TYPE_QUAL_ATOMIC)
1662 : TYPE_MAIN_VARIANT (mv1));
46df2823 1663 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1664 mv2 = (TYPE_ATOMIC (mv2)
1665 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1666 TYPE_QUAL_ATOMIC)
1667 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1668 /* A null pointer instead of a type
1669 means there is supposed to be an argument
1670 but nothing is specified about what type it has.
1671 So match anything that self-promotes. */
dc5027f4
JM
1672 if (different_types_p != NULL
1673 && (a1 == 0) != (a2 == 0))
1674 *different_types_p = true;
46df2823 1675 if (a1 == 0)
400fbf9f 1676 {
46df2823 1677 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1678 return 0;
1679 }
46df2823 1680 else if (a2 == 0)
400fbf9f 1681 {
46df2823 1682 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1683 return 0;
1684 }
8f5b6d29 1685 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1686 else if (TREE_CODE (a1) == ERROR_MARK
1687 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1688 ;
dc5027f4
JM
1689 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1690 different_types_p)))
400fbf9f 1691 {
dc5027f4
JM
1692 if (different_types_p != NULL)
1693 *different_types_p = true;
400fbf9f
JW
1694 /* Allow wait (union {union wait *u; int *i} *)
1695 and wait (union wait *) to be compatible. */
46df2823
JM
1696 if (TREE_CODE (a1) == UNION_TYPE
1697 && (TYPE_NAME (a1) == 0
ebf0bf7f 1698 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1699 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1700 && tree_int_cst_equal (TYPE_SIZE (a1),
1701 TYPE_SIZE (a2)))
400fbf9f
JW
1702 {
1703 tree memb;
46df2823 1704 for (memb = TYPE_FIELDS (a1);
910ad8de 1705 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1706 {
1707 tree mv3 = TREE_TYPE (memb);
1708 if (mv3 && mv3 != error_mark_node
1709 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1710 mv3 = (TYPE_ATOMIC (mv3)
1711 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1712 TYPE_QUAL_ATOMIC)
1713 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1714 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1715 different_types_p))
58cb41e6
JJ
1716 break;
1717 }
400fbf9f
JW
1718 if (memb == 0)
1719 return 0;
1720 }
46df2823
JM
1721 else if (TREE_CODE (a2) == UNION_TYPE
1722 && (TYPE_NAME (a2) == 0
ebf0bf7f 1723 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1724 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1725 && tree_int_cst_equal (TYPE_SIZE (a2),
1726 TYPE_SIZE (a1)))
400fbf9f
JW
1727 {
1728 tree memb;
46df2823 1729 for (memb = TYPE_FIELDS (a2);
910ad8de 1730 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1731 {
1732 tree mv3 = TREE_TYPE (memb);
1733 if (mv3 && mv3 != error_mark_node
1734 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1735 mv3 = (TYPE_ATOMIC (mv3)
1736 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1737 TYPE_QUAL_ATOMIC)
1738 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1739 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1740 different_types_p))
58cb41e6
JJ
1741 break;
1742 }
400fbf9f
JW
1743 if (memb == 0)
1744 return 0;
1745 }
1746 else
1747 return 0;
1748 }
1749
1750 /* comptypes said ok, but record if it said to warn. */
1751 if (newval > val)
1752 val = newval;
1753
1754 args1 = TREE_CHAIN (args1);
1755 args2 = TREE_CHAIN (args2);
1756 }
1757}
400fbf9f 1758\f
a0e24419
MP
1759/* Compute the size to increment a pointer by. When a function type or void
1760 type or incomplete type is passed, size_one_node is returned.
1761 This function does not emit any diagnostics; the caller is responsible
1762 for that. */
400fbf9f 1763
4e2fb7de 1764static tree
58f9752a 1765c_size_in_bytes (const_tree type)
400fbf9f
JW
1766{
1767 enum tree_code code = TREE_CODE (type);
1768
a0e24419
MP
1769 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1770 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1771 return size_one_node;
1772
400fbf9f 1773 /* Convert in case a char is more than one unit. */
db3927fb
AH
1774 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1775 size_int (TYPE_PRECISION (char_type_node)
1776 / BITS_PER_UNIT));
400fbf9f 1777}
400fbf9f 1778\f
400fbf9f
JW
1779/* Return either DECL or its known constant value (if it has one). */
1780
56cb9733 1781tree
2f6e4e97 1782decl_constant_value (tree decl)
400fbf9f 1783{
a7c1916a 1784 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1785 in a place where a variable is invalid. Note that DECL_INITIAL
1786 isn't valid for a PARM_DECL. */
a7c1916a 1787 current_function_decl != 0
4f976745 1788 && TREE_CODE (decl) != PARM_DECL
3f75a254 1789 && !TREE_THIS_VOLATILE (decl)
83bab8db 1790 && TREE_READONLY (decl)
400fbf9f
JW
1791 && DECL_INITIAL (decl) != 0
1792 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1793 /* This is invalid if initial value is not constant.
1794 If it has either a function call, a memory reference,
1795 or a variable, then re-evaluating it could give different results. */
1796 && TREE_CONSTANT (DECL_INITIAL (decl))
1797 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1798 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1799 return DECL_INITIAL (decl);
1800 return decl;
1801}
1802
f2a71bbc
JM
1803/* Convert the array expression EXP to a pointer. */
1804static tree
c2255bc4 1805array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1806{
f2a71bbc 1807 tree orig_exp = exp;
207bf485 1808 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1809 tree adr;
1810 tree restype = TREE_TYPE (type);
1811 tree ptrtype;
207bf485 1812
f2a71bbc 1813 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1814
f2a71bbc 1815 STRIP_TYPE_NOPS (exp);
207bf485 1816
487a92fe
JM
1817 if (TREE_NO_WARNING (orig_exp))
1818 TREE_NO_WARNING (exp) = 1;
207bf485 1819
f2a71bbc
JM
1820 ptrtype = build_pointer_type (restype);
1821
22d03525 1822 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1823 return convert (ptrtype, TREE_OPERAND (exp, 0));
1824
1f37c583
JM
1825 /* In C++ array compound literals are temporary objects unless they are
1826 const or appear in namespace scope, so they are destroyed too soon
1827 to use them for much of anything (c++/53220). */
1828 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1829 {
1830 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1831 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1832 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1833 "converting an array compound literal to a pointer "
1834 "is ill-formed in C++");
1835 }
1836
c2255bc4 1837 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1838 return convert (ptrtype, adr);
1839}
207bf485 1840
f2a71bbc
JM
1841/* Convert the function expression EXP to a pointer. */
1842static tree
c2255bc4 1843function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1844{
1845 tree orig_exp = exp;
207bf485 1846
f2a71bbc 1847 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1848
f2a71bbc 1849 STRIP_TYPE_NOPS (exp);
207bf485 1850
f2a71bbc
JM
1851 if (TREE_NO_WARNING (orig_exp))
1852 TREE_NO_WARNING (exp) = 1;
207bf485 1853
c2255bc4 1854 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1855}
207bf485 1856
ebfbbdc5
JJ
1857/* Mark EXP as read, not just set, for set but not used -Wunused
1858 warning purposes. */
1859
1860void
1861mark_exp_read (tree exp)
1862{
1863 switch (TREE_CODE (exp))
1864 {
1865 case VAR_DECL:
1866 case PARM_DECL:
1867 DECL_READ_P (exp) = 1;
1868 break;
1869 case ARRAY_REF:
1870 case COMPONENT_REF:
1871 case MODIFY_EXPR:
1872 case REALPART_EXPR:
1873 case IMAGPART_EXPR:
1874 CASE_CONVERT:
1875 case ADDR_EXPR:
1876 mark_exp_read (TREE_OPERAND (exp, 0));
1877 break;
1878 case COMPOUND_EXPR:
82c3c067 1879 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1880 mark_exp_read (TREE_OPERAND (exp, 1));
1881 break;
1882 default:
1883 break;
1884 }
1885}
1886
f2a71bbc
JM
1887/* Perform the default conversion of arrays and functions to pointers.
1888 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1889 return EXP.
1890
1891 LOC is the location of the expression. */
f2a71bbc
JM
1892
1893struct c_expr
c2255bc4 1894default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1895{
1896 tree orig_exp = exp.value;
1897 tree type = TREE_TYPE (exp.value);
1898 enum tree_code code = TREE_CODE (type);
1899
1900 switch (code)
1901 {
1902 case ARRAY_TYPE:
1903 {
1904 bool not_lvalue = false;
1905 bool lvalue_array_p;
1906
1907 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1908 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1909 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1910 {
1911 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1912 not_lvalue = true;
1913 exp.value = TREE_OPERAND (exp.value, 0);
1914 }
1915
1916 if (TREE_NO_WARNING (orig_exp))
1917 TREE_NO_WARNING (exp.value) = 1;
1918
1919 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1920 if (!flag_isoc99 && !lvalue_array_p)
1921 {
1922 /* Before C99, non-lvalue arrays do not decay to pointers.
1923 Normally, using such an array would be invalid; but it can
1924 be used correctly inside sizeof or as a statement expression.
1925 Thus, do not give an error here; an error will result later. */
1926 return exp;
1927 }
1928
c2255bc4 1929 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1930 }
1931 break;
1932 case FUNCTION_TYPE:
c2255bc4 1933 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1934 break;
1935 default:
f2a71bbc 1936 break;
207bf485 1937 }
f2a71bbc 1938
207bf485
JM
1939 return exp;
1940}
1941
ebfbbdc5
JJ
1942struct c_expr
1943default_function_array_read_conversion (location_t loc, struct c_expr exp)
1944{
1945 mark_exp_read (exp.value);
1946 return default_function_array_conversion (loc, exp);
1947}
522ddfa2 1948
267bac10
JM
1949/* Return whether EXPR should be treated as an atomic lvalue for the
1950 purposes of load and store handling. */
1951
1952static bool
1953really_atomic_lvalue (tree expr)
1954{
7a0ca710 1955 if (error_operand_p (expr))
267bac10
JM
1956 return false;
1957 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1958 return false;
1959 if (!lvalue_p (expr))
1960 return false;
1961
1962 /* Ignore _Atomic on register variables, since their addresses can't
1963 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1964 sequences wouldn't work. Ignore _Atomic on structures containing
1965 bit-fields, since accessing elements of atomic structures or
1966 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1967 it's undefined at translation time or execution time, and the
1968 normal atomic sequences again wouldn't work. */
1969 while (handled_component_p (expr))
1970 {
1971 if (TREE_CODE (expr) == COMPONENT_REF
1972 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1973 return false;
1974 expr = TREE_OPERAND (expr, 0);
1975 }
1976 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1977 return false;
1978 return true;
1979}
1980
1981/* Convert expression EXP (location LOC) from lvalue to rvalue,
1982 including converting functions and arrays to pointers if CONVERT_P.
1983 If READ_P, also mark the expression as having been read. */
1984
1985struct c_expr
1986convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1987 bool convert_p, bool read_p)
1988{
1989 if (read_p)
1990 mark_exp_read (exp.value);
1991 if (convert_p)
1992 exp = default_function_array_conversion (loc, exp);
1993 if (really_atomic_lvalue (exp.value))
1994 {
1995 vec<tree, va_gc> *params;
1996 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1997 tree expr_type = TREE_TYPE (exp.value);
1998 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1999 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2000
2001 gcc_assert (TYPE_ATOMIC (expr_type));
2002
2003 /* Expansion of a generic atomic load may require an addition
2004 element, so allocate enough to prevent a resize. */
2005 vec_alloc (params, 4);
2006
2007 /* Remove the qualifiers for the rest of the expressions and
2008 create the VAL temp variable to hold the RHS. */
2009 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2010 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2011 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2012 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2013 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2014
2015 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2016 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2017 params->quick_push (expr_addr);
2018 params->quick_push (tmp_addr);
2019 params->quick_push (seq_cst);
8edbfaa6 2020 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2021
cc28fc7f
MP
2022 /* EXPR is always read. */
2023 mark_exp_read (exp.value);
2024
267bac10 2025 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2026 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2027 NULL_TREE, NULL_TREE);
267bac10
JM
2028 }
2029 return exp;
2030}
2031
522ddfa2
JM
2032/* EXP is an expression of integer type. Apply the integer promotions
2033 to it and return the promoted value. */
400fbf9f
JW
2034
2035tree
522ddfa2 2036perform_integral_promotions (tree exp)
400fbf9f 2037{
b3694847
SS
2038 tree type = TREE_TYPE (exp);
2039 enum tree_code code = TREE_CODE (type);
400fbf9f 2040
522ddfa2 2041 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2042
400fbf9f
JW
2043 /* Normally convert enums to int,
2044 but convert wide enums to something wider. */
2045 if (code == ENUMERAL_TYPE)
2046 {
b0c48229
NB
2047 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2048 TYPE_PRECISION (integer_type_node)),
2049 ((TYPE_PRECISION (type)
2050 >= TYPE_PRECISION (integer_type_node))
8df83eae 2051 && TYPE_UNSIGNED (type)));
05bccae2 2052
400fbf9f
JW
2053 return convert (type, exp);
2054 }
2055
522ddfa2
JM
2056 /* ??? This should no longer be needed now bit-fields have their
2057 proper types. */
9753f113 2058 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2059 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2060 /* If it's thinner than an int, promote it like a
d72040f5 2061 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2062 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2063 TYPE_PRECISION (integer_type_node)))
f458d1d5 2064 return convert (integer_type_node, exp);
9753f113 2065
d72040f5 2066 if (c_promoting_integer_type_p (type))
400fbf9f 2067 {
f458d1d5 2068 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2069 if (TYPE_UNSIGNED (type)
f458d1d5 2070 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2071 return convert (unsigned_type_node, exp);
05bccae2 2072
400fbf9f
JW
2073 return convert (integer_type_node, exp);
2074 }
05bccae2 2075
522ddfa2
JM
2076 return exp;
2077}
2078
2079
2080/* Perform default promotions for C data used in expressions.
46bdb9cf 2081 Enumeral types or short or char are converted to int.
522ddfa2
JM
2082 In addition, manifest constants symbols are replaced by their values. */
2083
2084tree
2085default_conversion (tree exp)
2086{
2087 tree orig_exp;
2088 tree type = TREE_TYPE (exp);
2089 enum tree_code code = TREE_CODE (type);
40449a90 2090 tree promoted_type;
522ddfa2 2091
ebfbbdc5
JJ
2092 mark_exp_read (exp);
2093
46bdb9cf
JM
2094 /* Functions and arrays have been converted during parsing. */
2095 gcc_assert (code != FUNCTION_TYPE);
2096 if (code == ARRAY_TYPE)
2097 return exp;
522ddfa2
JM
2098
2099 /* Constants can be used directly unless they're not loadable. */
2100 if (TREE_CODE (exp) == CONST_DECL)
2101 exp = DECL_INITIAL (exp);
2102
522ddfa2
JM
2103 /* Strip no-op conversions. */
2104 orig_exp = exp;
2105 STRIP_TYPE_NOPS (exp);
2106
2107 if (TREE_NO_WARNING (orig_exp))
2108 TREE_NO_WARNING (exp) = 1;
2109
400fbf9f
JW
2110 if (code == VOID_TYPE)
2111 {
5436fa2e
MP
2112 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2113 "void value not ignored as it ought to be");
400fbf9f
JW
2114 return error_mark_node;
2115 }
808d6eaa
JM
2116
2117 exp = require_complete_type (exp);
2118 if (exp == error_mark_node)
2119 return error_mark_node;
2120
40449a90
SL
2121 promoted_type = targetm.promoted_type (type);
2122 if (promoted_type)
2123 return convert (promoted_type, exp);
2124
808d6eaa
JM
2125 if (INTEGRAL_TYPE_P (type))
2126 return perform_integral_promotions (exp);
2127
400fbf9f
JW
2128 return exp;
2129}
2130\f
0fb96aa4 2131/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2132
2133 If the component name is not found, returns NULL_TREE. Otherwise,
2134 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2135 stepping down the chain to the component, which is in the last
2136 TREE_VALUE of the list. Normally the list is of length one, but if
2137 the component is embedded within (nested) anonymous structures or
2138 unions, the list steps down the chain to the component. */
2f6e4e97 2139
2f2d13da 2140static tree
0fb96aa4 2141lookup_field (tree type, tree component)
2f2d13da
DE
2142{
2143 tree field;
2144
2145 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2146 to the field elements. Use a binary search on this array to quickly
2147 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2148 will always be set for structures which have many elements. */
2149
22a0b85f 2150 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2151 {
2152 int bot, top, half;
d07605f5 2153 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2154
2155 field = TYPE_FIELDS (type);
2156 bot = 0;
d07605f5 2157 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2158 while (top - bot > 1)
2159 {
2f2d13da
DE
2160 half = (top - bot + 1) >> 1;
2161 field = field_array[bot+half];
2162
2163 if (DECL_NAME (field) == NULL_TREE)
2164 {
2165 /* Step through all anon unions in linear fashion. */
2166 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2167 {
2f2d13da 2168 field = field_array[bot++];
3e636daf 2169 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2170 {
0fb96aa4 2171 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2172
2173 if (anon)
2174 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2175
2176 /* The Plan 9 compiler permits referring
2177 directly to an anonymous struct/union field
2178 using a typedef name. */
2179 if (flag_plan9_extensions
2180 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2181 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2182 == TYPE_DECL)
2183 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2184 == component))
2185 break;
2f6e4e97 2186 }
2f2d13da
DE
2187 }
2188
2189 /* Entire record is only anon unions. */
2190 if (bot > top)
2191 return NULL_TREE;
2192
2193 /* Restart the binary search, with new lower bound. */
2194 continue;
2195 }
2196
e8b87aac 2197 if (DECL_NAME (field) == component)
2f2d13da 2198 break;
e8b87aac 2199 if (DECL_NAME (field) < component)
2f2d13da
DE
2200 bot += half;
2201 else
2202 top = bot + half;
2203 }
2204
2205 if (DECL_NAME (field_array[bot]) == component)
2206 field = field_array[bot];
2207 else if (DECL_NAME (field) != component)
e9b2c823 2208 return NULL_TREE;
2f2d13da
DE
2209 }
2210 else
2211 {
910ad8de 2212 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2213 {
e9b2c823 2214 if (DECL_NAME (field) == NULL_TREE
3e636daf 2215 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2216 {
0fb96aa4 2217 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2218
e9b2c823
NB
2219 if (anon)
2220 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2221
2222 /* The Plan 9 compiler permits referring directly to an
2223 anonymous struct/union field using a typedef
2224 name. */
2225 if (flag_plan9_extensions
2226 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2227 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2228 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2229 == component))
2230 break;
2f2d13da
DE
2231 }
2232
2233 if (DECL_NAME (field) == component)
2234 break;
2235 }
e9b2c823
NB
2236
2237 if (field == NULL_TREE)
2238 return NULL_TREE;
2f2d13da
DE
2239 }
2240
e9b2c823 2241 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2242}
2243
277fe616
DM
2244/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2245
2246static void
2247lookup_field_fuzzy_find_candidates (tree type, tree component,
2248 vec<tree> *candidates)
2249{
2250 tree field;
2251 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2252 {
2253 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2254 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2255 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2256 candidates);
277fe616
DM
2257
2258 if (DECL_NAME (field))
2259 candidates->safe_push (DECL_NAME (field));
2260 }
2261}
2262
2263/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2264 rather than returning a TREE_LIST for an exact match. */
2265
2266static tree
2267lookup_field_fuzzy (tree type, tree component)
2268{
2269 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2270
2271 /* First, gather a list of candidates. */
2272 auto_vec <tree> candidates;
2273
2274 lookup_field_fuzzy_find_candidates (type, component,
2275 &candidates);
2276
8ece8dfb 2277 return find_closest_identifier (component, &candidates);
277fe616
DM
2278}
2279
a87a86e1
DM
2280/* Support function for build_component_ref's error-handling.
2281
2282 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2283 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2284
2285static bool
2286should_suggest_deref_p (tree datum_type)
2287{
2288 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2289 allows "." for ptrs; we could be handling a failed attempt
2290 to access a property. */
2291 if (c_dialect_objc ())
2292 return false;
2293
2294 /* Only suggest it for pointers... */
2295 if (TREE_CODE (datum_type) != POINTER_TYPE)
2296 return false;
2297
2298 /* ...to structs/unions. */
2299 tree underlying_type = TREE_TYPE (datum_type);
2300 enum tree_code code = TREE_CODE (underlying_type);
2301 if (code == RECORD_TYPE || code == UNION_TYPE)
2302 return true;
2303 else
2304 return false;
2305}
2306
c2255bc4
AH
2307/* Make an expression to refer to the COMPONENT field of structure or
2308 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2309 location of the COMPONENT_REF. */
400fbf9f
JW
2310
2311tree
c2255bc4 2312build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2313{
b3694847
SS
2314 tree type = TREE_TYPE (datum);
2315 enum tree_code code = TREE_CODE (type);
2316 tree field = NULL;
2317 tree ref;
1e57bf47 2318 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2319
7a3ea201
RH
2320 if (!objc_is_public (datum, component))
2321 return error_mark_node;
2322
46a88c12 2323 /* Detect Objective-C property syntax object.property. */
668ea4b1 2324 if (c_dialect_objc ()
46a88c12 2325 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2326 return ref;
2327
400fbf9f
JW
2328 /* See if there is a field or component with name COMPONENT. */
2329
2330 if (code == RECORD_TYPE || code == UNION_TYPE)
2331 {
d0f062fb 2332 if (!COMPLETE_TYPE_P (type))
400fbf9f 2333 {
7a228918 2334 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2335 return error_mark_node;
2336 }
2337
0fb96aa4 2338 field = lookup_field (type, component);
400fbf9f
JW
2339
2340 if (!field)
2341 {
277fe616
DM
2342 tree guessed_id = lookup_field_fuzzy (type, component);
2343 if (guessed_id)
2344 error_at (loc, "%qT has no member named %qE; did you mean %qE?",
2345 type, component, guessed_id);
2346 else
2347 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2348 return error_mark_node;
2349 }
400fbf9f 2350
e9b2c823
NB
2351 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2352 This might be better solved in future the way the C++ front
2353 end does it - by giving the anonymous entities each a
2354 separate name and type, and then have build_component_ref
2355 recursively call itself. We can't do that here. */
46ea50cb 2356 do
19d76e60 2357 {
e9b2c823 2358 tree subdatum = TREE_VALUE (field);
efed193e
JM
2359 int quals;
2360 tree subtype;
1e57bf47 2361 bool use_datum_quals;
e9b2c823
NB
2362
2363 if (TREE_TYPE (subdatum) == error_mark_node)
2364 return error_mark_node;
2365
1e57bf47
JM
2366 /* If this is an rvalue, it does not have qualifiers in C
2367 standard terms and we must avoid propagating such
2368 qualifiers down to a non-lvalue array that is then
2369 converted to a pointer. */
2370 use_datum_quals = (datum_lvalue
2371 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2372
efed193e 2373 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2374 if (use_datum_quals)
2375 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2376 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2377
2378 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2379 NULL_TREE);
c2255bc4 2380 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2381 if (TREE_READONLY (subdatum)
2382 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2383 TREE_READONLY (ref) = 1;
1e57bf47
JM
2384 if (TREE_THIS_VOLATILE (subdatum)
2385 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2386 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2387
2388 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2389 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2390
19d76e60 2391 datum = ref;
46ea50cb
RS
2392
2393 field = TREE_CHAIN (field);
19d76e60 2394 }
46ea50cb 2395 while (field);
19d76e60 2396
400fbf9f
JW
2397 return ref;
2398 }
a87a86e1
DM
2399 else if (should_suggest_deref_p (type))
2400 {
2401 /* Special-case the error message for "ptr.field" for the case
2402 where the user has confused "." vs "->". */
2403 rich_location richloc (line_table, loc);
2404 /* "loc" should be the "." token. */
2405 richloc.add_fixit_replace (source_range::from_location (loc), "->");
2406 error_at_rich_loc (&richloc,
2407 "%qE is a pointer; did you mean to use %<->%>?",
2408 datum);
2409 return error_mark_node;
2410 }
400fbf9f 2411 else if (code != ERROR_MARK)
c2255bc4
AH
2412 error_at (loc,
2413 "request for member %qE in something not a structure or union",
2414 component);
400fbf9f
JW
2415
2416 return error_mark_node;
2417}
2418\f
2419/* Given an expression PTR for a pointer, return an expression
2420 for the value pointed to.
6a3799eb
AH
2421 ERRORSTRING is the name of the operator to appear in error messages.
2422
2423 LOC is the location to use for the generated tree. */
400fbf9f
JW
2424
2425tree
dd865ef6 2426build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2427{
b3694847
SS
2428 tree pointer = default_conversion (ptr);
2429 tree type = TREE_TYPE (pointer);
6a3799eb 2430 tree ref;
400fbf9f
JW
2431
2432 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2433 {
1043771b 2434 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2435 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2436 {
2437 /* If a warning is issued, mark it to avoid duplicates from
2438 the backend. This only needs to be done at
2439 warn_strict_aliasing > 2. */
2440 if (warn_strict_aliasing > 2)
2441 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2442 type, TREE_OPERAND (pointer, 0)))
2443 TREE_NO_WARNING (pointer) = 1;
2444 }
2445
870cc33b 2446 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2447 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2448 == TREE_TYPE (type)))
6a3799eb
AH
2449 {
2450 ref = TREE_OPERAND (pointer, 0);
2451 protected_set_expr_location (ref, loc);
2452 return ref;
2453 }
870cc33b
RS
2454 else
2455 {
2456 tree t = TREE_TYPE (type);
46df2823 2457
984dfd8c 2458 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2459
baae9b65 2460 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2461 {
d9b7be2e
MP
2462 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2463 {
2464 error_at (loc, "dereferencing pointer to incomplete type "
2465 "%qT", t);
2466 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2467 }
870cc33b
RS
2468 return error_mark_node;
2469 }
7d882b83 2470 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2471 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2472
2473 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2474 so that we get the proper error message if the result is used
2475 to assign to. Also, &* is supposed to be a no-op.
2476 And ANSI C seems to specify that the type of the result
2477 should be the const type. */
2478 /* A de-reference of a pointer to const is not a const. It is valid
2479 to change it via some other pointer. */
2480 TREE_READONLY (ref) = TYPE_READONLY (t);
2481 TREE_SIDE_EFFECTS (ref)
271bd540 2482 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2483 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2484 protected_set_expr_location (ref, loc);
870cc33b
RS
2485 return ref;
2486 }
2487 }
400fbf9f 2488 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2489 invalid_indirection_error (loc, type, errstring);
2490
400fbf9f
JW
2491 return error_mark_node;
2492}
2493
2494/* This handles expressions of the form "a[i]", which denotes
2495 an array reference.
2496
2497 This is logically equivalent in C to *(a+i), but we may do it differently.
2498 If A is a variable or a member, we generate a primitive ARRAY_REF.
2499 This avoids forcing the array out of registers, and can work on
2500 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2501 by functions).
2502
30cd1c5d
AS
2503 For vector types, allow vector[i] but not i[vector], and create
2504 *(((type*)&vectortype) + i) for the expression.
2505
6a3799eb 2506 LOC is the location to use for the returned expression. */
400fbf9f
JW
2507
2508tree
c2255bc4 2509build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2510{
6a3799eb 2511 tree ret;
a4ab7973 2512 bool swapped = false;
400fbf9f
JW
2513 if (TREE_TYPE (array) == error_mark_node
2514 || TREE_TYPE (index) == error_mark_node)
2515 return error_mark_node;
2516
b72271b9 2517 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2518 {
2519 size_t rank = 0;
2520 if (!find_rank (loc, index, index, true, &rank))
2521 return error_mark_node;
2522 if (rank > 1)
2523 {
2524 error_at (loc, "rank of the array's index is greater than 1");
2525 return error_mark_node;
2526 }
2527 }
a4ab7973 2528 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2529 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2530 /* Allow vector[index] but not index[vector]. */
31521951 2531 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2532 {
a4ab7973
JM
2533 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2534 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2535 {
f90e8e2e 2536 error_at (loc,
30cd1c5d
AS
2537 "subscripted value is neither array nor pointer nor vector");
2538
fdeefd49
RS
2539 return error_mark_node;
2540 }
fab27f52 2541 std::swap (array, index);
a4ab7973
JM
2542 swapped = true;
2543 }
2544
2545 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2546 {
a63068b6 2547 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2548 return error_mark_node;
2549 }
2550
2551 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2552 {
a63068b6 2553 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2554 return error_mark_node;
2555 }
2556
ff6b6641
GDR
2557 /* ??? Existing practice has been to warn only when the char
2558 index is syntactically the index, not for char[array]. */
2559 if (!swapped)
5bd012f8 2560 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2561
2562 /* Apply default promotions *after* noticing character types. */
2563 index = default_conversion (index);
f406ae1f
MP
2564 if (index == error_mark_node)
2565 return error_mark_node;
a4ab7973
JM
2566
2567 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2568
aa7da51a
JJ
2569 bool non_lvalue
2570 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2571
2572 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2573 {
e4d35515 2574 tree rval, type;
fdeefd49 2575
400fbf9f
JW
2576 /* An array that is indexed by a non-constant
2577 cannot be stored in a register; we must be able to do
2578 address arithmetic on its address.
2579 Likewise an array of elements of variable size. */
2580 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2581 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2582 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2583 {
dffd7eb6 2584 if (!c_mark_addressable (array))
400fbf9f
JW
2585 return error_mark_node;
2586 }
e6d52559
JW
2587 /* An array that is indexed by a constant value which is not within
2588 the array bounds cannot be stored in a register either; because we
2589 would get a crash in store_bit_field/extract_bit_field when trying
2590 to access a non-existent part of the register. */
2591 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2592 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2593 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2594 {
dffd7eb6 2595 if (!c_mark_addressable (array))
e6d52559
JW
2596 return error_mark_node;
2597 }
400fbf9f 2598
f3bede71 2599 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2600 {
2601 tree foo = array;
2602 while (TREE_CODE (foo) == COMPONENT_REF)
2603 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2604 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2605 pedwarn (loc, OPT_Wpedantic,
fcf73884 2606 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2607 else if (!lvalue_p (foo))
2608 pedwarn_c90 (loc, OPT_Wpedantic,
2609 "ISO C90 forbids subscripting non-lvalue "
2610 "array");
400fbf9f
JW
2611 }
2612
46df2823 2613 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2614 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2615 /* Array ref is const/volatile if the array elements are
c22cacf3 2616 or if the array is. */
400fbf9f
JW
2617 TREE_READONLY (rval)
2618 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2619 | TREE_READONLY (array));
2620 TREE_SIDE_EFFECTS (rval)
2621 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2622 | TREE_SIDE_EFFECTS (array));
2623 TREE_THIS_VOLATILE (rval)
2624 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2625 /* This was added by rms on 16 Nov 91.
2f6e4e97 2626 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2627 in an inline function.
2628 Hope it doesn't break something else. */
2629 | TREE_THIS_VOLATILE (array));
928c19bb 2630 ret = require_complete_type (rval);
6a3799eb 2631 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2632 if (non_lvalue)
2633 ret = non_lvalue_loc (loc, ret);
6a3799eb 2634 return ret;
400fbf9f 2635 }
a4ab7973
JM
2636 else
2637 {
2638 tree ar = default_conversion (array);
400fbf9f 2639
a4ab7973
JM
2640 if (ar == error_mark_node)
2641 return ar;
400fbf9f 2642
a4ab7973
JM
2643 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2644 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2645
aa7da51a
JJ
2646 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2647 index, 0),
2648 RO_ARRAY_INDEXING);
2649 if (non_lvalue)
2650 ret = non_lvalue_loc (loc, ret);
2651 return ret;
a4ab7973 2652 }
400fbf9f
JW
2653}
2654\f
7e585d16 2655/* Build an external reference to identifier ID. FUN indicates
766beb40 2656 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2657 location of the identifier. This sets *TYPE to the type of the
2658 identifier, which is not the same as the type of the returned value
2659 for CONST_DECLs defined as enum constants. If the type of the
2660 identifier is not available, *TYPE is set to NULL. */
7e585d16 2661tree
c2255bc4 2662build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2663{
2664 tree ref;
2665 tree decl = lookup_name (id);
16b34ad6
ZL
2666
2667 /* In Objective-C, an instance variable (ivar) may be preferred to
2668 whatever lookup_name() found. */
2669 decl = objc_lookup_ivar (decl, id);
7e585d16 2670
6866c6e8 2671 *type = NULL;
339a28b9 2672 if (decl && decl != error_mark_node)
6866c6e8
ILT
2673 {
2674 ref = decl;
2675 *type = TREE_TYPE (ref);
2676 }
339a28b9
ZW
2677 else if (fun)
2678 /* Implicit function declaration. */
c2255bc4 2679 ref = implicitly_declare (loc, id);
339a28b9
ZW
2680 else if (decl == error_mark_node)
2681 /* Don't complain about something that's already been
2682 complained about. */
2683 return error_mark_node;
2684 else
2685 {
c2255bc4 2686 undeclared_variable (loc, id);
339a28b9
ZW
2687 return error_mark_node;
2688 }
7e585d16
ZW
2689
2690 if (TREE_TYPE (ref) == error_mark_node)
2691 return error_mark_node;
2692
339a28b9 2693 if (TREE_DEPRECATED (ref))
9b86d6bb 2694 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2695
ad960f56 2696 /* Recursive call does not count as usage. */
b8698a0f 2697 if (ref != current_function_decl)
ad960f56 2698 {
ad960f56
MLI
2699 TREE_USED (ref) = 1;
2700 }
7e585d16 2701
bc4b653b
JM
2702 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2703 {
2704 if (!in_sizeof && !in_typeof)
2705 C_DECL_USED (ref) = 1;
2706 else if (DECL_INITIAL (ref) == 0
2707 && DECL_EXTERNAL (ref)
2708 && !TREE_PUBLIC (ref))
2709 record_maybe_used_decl (ref);
2710 }
2711
7e585d16
ZW
2712 if (TREE_CODE (ref) == CONST_DECL)
2713 {
6193b8b7 2714 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2715
2716 if (warn_cxx_compat
2717 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2718 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2719 {
2720 warning_at (loc, OPT_Wc___compat,
2721 ("enum constant defined in struct or union "
2722 "is not visible in C++"));
2723 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2724 }
2725
7e585d16
ZW
2726 ref = DECL_INITIAL (ref);
2727 TREE_CONSTANT (ref) = 1;
2728 }
6a29edea 2729 else if (current_function_decl != 0
4b1e44be 2730 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2731 && (VAR_OR_FUNCTION_DECL_P (ref)
2732 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2733 {
2734 tree context = decl_function_context (ref);
2f6e4e97 2735
6a29edea
EB
2736 if (context != 0 && context != current_function_decl)
2737 DECL_NONLOCAL (ref) = 1;
2738 }
71113fcd
GK
2739 /* C99 6.7.4p3: An inline definition of a function with external
2740 linkage ... shall not contain a reference to an identifier with
2741 internal linkage. */
2742 else if (current_function_decl != 0
2743 && DECL_DECLARED_INLINE_P (current_function_decl)
2744 && DECL_EXTERNAL (current_function_decl)
2745 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2746 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2747 && ! TREE_PUBLIC (ref)
2748 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2749 record_inline_static (loc, current_function_decl, ref,
2750 csi_internal);
7e585d16
ZW
2751
2752 return ref;
2753}
2754
bc4b653b
JM
2755/* Record details of decls possibly used inside sizeof or typeof. */
2756struct maybe_used_decl
2757{
2758 /* The decl. */
2759 tree decl;
2760 /* The level seen at (in_sizeof + in_typeof). */
2761 int level;
2762 /* The next one at this level or above, or NULL. */
2763 struct maybe_used_decl *next;
2764};
2765
2766static struct maybe_used_decl *maybe_used_decls;
2767
2768/* Record that DECL, an undefined static function reference seen
2769 inside sizeof or typeof, might be used if the operand of sizeof is
2770 a VLA type or the operand of typeof is a variably modified
2771 type. */
2772
4e2fb7de 2773static void
bc4b653b
JM
2774record_maybe_used_decl (tree decl)
2775{
2776 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2777 t->decl = decl;
2778 t->level = in_sizeof + in_typeof;
2779 t->next = maybe_used_decls;
2780 maybe_used_decls = t;
2781}
2782
2783/* Pop the stack of decls possibly used inside sizeof or typeof. If
2784 USED is false, just discard them. If it is true, mark them used
2785 (if no longer inside sizeof or typeof) or move them to the next
2786 level up (if still inside sizeof or typeof). */
2787
2788void
2789pop_maybe_used (bool used)
2790{
2791 struct maybe_used_decl *p = maybe_used_decls;
2792 int cur_level = in_sizeof + in_typeof;
2793 while (p && p->level > cur_level)
2794 {
2795 if (used)
2796 {
2797 if (cur_level == 0)
2798 C_DECL_USED (p->decl) = 1;
2799 else
2800 p->level = cur_level;
2801 }
2802 p = p->next;
2803 }
2804 if (!used || cur_level == 0)
2805 maybe_used_decls = p;
2806}
2807
2808/* Return the result of sizeof applied to EXPR. */
2809
2810struct c_expr
c2255bc4 2811c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2812{
2813 struct c_expr ret;
ad97f4be
JM
2814 if (expr.value == error_mark_node)
2815 {
2816 ret.value = error_mark_node;
2817 ret.original_code = ERROR_MARK;
6866c6e8 2818 ret.original_type = NULL;
ad97f4be
JM
2819 pop_maybe_used (false);
2820 }
2821 else
2822 {
928c19bb 2823 bool expr_const_operands = true;
773ec47f
MP
2824
2825 if (TREE_CODE (expr.value) == PARM_DECL
2826 && C_ARRAY_PARAMETER (expr.value))
2827 {
2828 if (warning_at (loc, OPT_Wsizeof_array_argument,
2829 "%<sizeof%> on array function parameter %qE will "
2830 "return size of %qT", expr.value,
2831 expr.original_type))
2832 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2833 }
928c19bb
JM
2834 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2835 &expr_const_operands);
c2255bc4 2836 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2837 c_last_sizeof_arg = expr.value;
2838 ret.original_code = SIZEOF_EXPR;
6866c6e8 2839 ret.original_type = NULL;
928c19bb 2840 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2841 {
2842 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2843 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2844 folded_expr, ret.value);
2845 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2846 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2847 }
928c19bb 2848 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2849 }
bc4b653b
JM
2850 return ret;
2851}
2852
2853/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2854 name passed to sizeof (rather than the type itself). LOC is the
2855 location of the original expression. */
bc4b653b
JM
2856
2857struct c_expr
c2255bc4 2858c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2859{
2860 tree type;
2861 struct c_expr ret;
928c19bb
JM
2862 tree type_expr = NULL_TREE;
2863 bool type_expr_const = true;
2864 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2865 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2866 c_last_sizeof_arg = type;
2867 ret.original_code = SIZEOF_EXPR;
6866c6e8 2868 ret.original_type = NULL;
24070fcb
JM
2869 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2870 && c_vla_type_p (type))
928c19bb 2871 {
24070fcb
JM
2872 /* If the type is a [*] array, it is a VLA but is represented as
2873 having a size of zero. In such a case we must ensure that
2874 the result of sizeof does not get folded to a constant by
2875 c_fully_fold, because if the size is evaluated the result is
2876 not constant and so constraints on zero or negative size
2877 arrays must not be applied when this sizeof call is inside
2878 another array declarator. */
2879 if (!type_expr)
2880 type_expr = integer_zero_node;
928c19bb
JM
2881 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2882 type_expr, ret.value);
2883 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2884 }
16464cc1
VR
2885 pop_maybe_used (type != error_mark_node
2886 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2887 return ret;
2888}
2889
400fbf9f 2890/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2891 The function call is at LOC.
400fbf9f
JW
2892 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2893 TREE_VALUE of each node is a parameter-expression.
2894 FUNCTION's data type may be a function type or a pointer-to-function. */
2895
2896tree
c2255bc4 2897build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2898{
9771b263 2899 vec<tree, va_gc> *v;
bbbbb16a
ILT
2900 tree ret;
2901
9771b263 2902 vec_alloc (v, list_length (params));
bbbbb16a 2903 for (; params; params = TREE_CHAIN (params))
9771b263 2904 v->quick_push (TREE_VALUE (params));
8edbfaa6 2905 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2906 vec_free (v);
bbbbb16a
ILT
2907 return ret;
2908}
2909
ae52741c
MLI
2910/* Give a note about the location of the declaration of DECL. */
2911
c7b70a3c
MP
2912static void
2913inform_declaration (tree decl)
ae52741c 2914{
c7b70a3c 2915 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2916 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2917}
2918
bbbbb16a
ILT
2919/* Build a function call to function FUNCTION with parameters PARAMS.
2920 ORIGTYPES, if not NULL, is a vector of types; each element is
2921 either NULL or the original type of the corresponding element in
2922 PARAMS. The original type may differ from TREE_TYPE of the
2923 parameter for enums. FUNCTION's data type may be a function type
2924 or pointer-to-function. This function changes the elements of
2925 PARAMS. */
2926
2927tree
81e5eca8
MP
2928build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2929 tree function, vec<tree, va_gc> *params,
9771b263 2930 vec<tree, va_gc> *origtypes)
400fbf9f 2931{
b3694847 2932 tree fntype, fundecl = 0;
4977bab6 2933 tree name = NULL_TREE, result;
c96f4f73 2934 tree tem;
94a0dd7b
SL
2935 int nargs;
2936 tree *argarray;
b8698a0f 2937
400fbf9f 2938
fc76e425 2939 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2940 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2941
2942 /* Convert anything with function type to a pointer-to-function. */
2943 if (TREE_CODE (function) == FUNCTION_DECL)
2944 {
2945 name = DECL_NAME (function);
0a35513e
AH
2946
2947 if (flag_tm)
2948 tm_malloc_replacement (function);
a5eadacc 2949 fundecl = function;
86951993
AM
2950 /* Atomic functions have type checking/casting already done. They are
2951 often rewritten and don't match the original parameter list. */
2952 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2953 origtypes = NULL;
36536d79 2954
b72271b9 2955 if (flag_cilkplus
36536d79
BI
2956 && is_cilkplus_reduce_builtin (function))
2957 origtypes = NULL;
400fbf9f 2958 }
f2a71bbc 2959 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2960 function = function_to_pointer_conversion (loc, function);
400fbf9f 2961
6e955430
ZL
2962 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2963 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2964 if (params && !params->is_empty ())
2965 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2966
928c19bb
JM
2967 function = c_fully_fold (function, false, NULL);
2968
400fbf9f
JW
2969 fntype = TREE_TYPE (function);
2970
2971 if (TREE_CODE (fntype) == ERROR_MARK)
2972 return error_mark_node;
2973
2974 if (!(TREE_CODE (fntype) == POINTER_TYPE
2975 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2976 {
ae52741c
MLI
2977 if (!flag_diagnostics_show_caret)
2978 error_at (loc,
2979 "called object %qE is not a function or function pointer",
2980 function);
2981 else if (DECL_P (function))
2982 {
2983 error_at (loc,
2984 "called object %qD is not a function or function pointer",
2985 function);
2986 inform_declaration (function);
2987 }
2988 else
2989 error_at (loc,
2990 "called object is not a function or function pointer");
400fbf9f
JW
2991 return error_mark_node;
2992 }
2993
5ce89b2e
JM
2994 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2995 current_function_returns_abnormally = 1;
2996
400fbf9f
JW
2997 /* fntype now gets the type of function pointed to. */
2998 fntype = TREE_TYPE (fntype);
2999
ab4194da
JM
3000 /* Convert the parameters to the types declared in the
3001 function prototype, or apply default promotions. */
3002
81e5eca8
MP
3003 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3004 origtypes, function, fundecl);
ab4194da
JM
3005 if (nargs < 0)
3006 return error_mark_node;
3007
c96f4f73 3008 /* Check that the function is called through a compatible prototype.
fa337f3a 3009 If it is not, warn. */
1043771b 3010 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3011 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3012 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3013 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3014 {
3015 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3016
3017 /* This situation leads to run-time undefined behavior. We can't,
3018 therefore, simply error unless we can prove that all possible
3019 executions of the program must execute the code. */
fa337f3a 3020 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3021
fa337f3a
RB
3022 if (VOID_TYPE_P (return_type)
3023 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3024 pedwarn (loc, 0,
3025 "function with qualified void return type called");
3026 }
c96f4f73 3027
9771b263 3028 argarray = vec_safe_address (params);
bbbbb16a 3029
83322951
RG
3030 /* Check that arguments to builtin functions match the expectations. */
3031 if (fundecl
3032 && DECL_BUILT_IN (fundecl)
3033 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
3034 && !check_builtin_function_arguments (fundecl, nargs, argarray))
3035 return error_mark_node;
400fbf9f 3036
83322951 3037 /* Check that the arguments to the function are valid. */
dde05067 3038 check_function_arguments (fntype, nargs, argarray);
400fbf9f 3039
928c19bb
JM
3040 if (name != NULL_TREE
3041 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3042 {
928c19bb 3043 if (require_constant_value)
b8698a0f 3044 result =
db3927fb
AH
3045 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3046 function, nargs, argarray);
928c19bb 3047 else
db3927fb
AH
3048 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3049 function, nargs, argarray);
928c19bb
JM
3050 if (TREE_CODE (result) == NOP_EXPR
3051 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3052 STRIP_TYPE_NOPS (result);
bf730f15
RS
3053 }
3054 else
db3927fb
AH
3055 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3056 function, nargs, argarray);
b0b3afb2 3057
71653180 3058 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3059 {
3060 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3061 pedwarn (loc, 0,
3ce62965
JM
3062 "function with qualified void return type called");
3063 return result;
3064 }
1eb8759b 3065 return require_complete_type (result);
400fbf9f 3066}
8edbfaa6
JJ
3067
3068/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3069
3070tree
3071c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3072 tree function, vec<tree, va_gc> *params,
3073 vec<tree, va_gc> *origtypes)
3074{
3075 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3076 STRIP_TYPE_NOPS (function);
3077
3078 /* Convert anything with function type to a pointer-to-function. */
3079 if (TREE_CODE (function) == FUNCTION_DECL)
3080 {
3081 /* Implement type-directed function overloading for builtins.
3082 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3083 handle all the type checking. The result is a complete expression
3084 that implements this function call. */
3085 tree tem = resolve_overloaded_builtin (loc, function, params);
3086 if (tem)
3087 return tem;
3088 }
3089 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3090}
400fbf9f 3091\f
bbbbb16a
ILT
3092/* Convert the argument expressions in the vector VALUES
3093 to the types in the list TYPELIST.
400fbf9f
JW
3094
3095 If TYPELIST is exhausted, or when an element has NULL as its type,
3096 perform the default conversions.
3097
bbbbb16a
ILT
3098 ORIGTYPES is the original types of the expressions in VALUES. This
3099 holds the type of enum values which have been converted to integral
3100 types. It may be NULL.
400fbf9f 3101
03dafa61
JM
3102 FUNCTION is a tree for the called function. It is used only for
3103 error messages, where it is formatted with %qE.
400fbf9f
JW
3104
3105 This is also where warnings about wrong number of args are generated.
3106
81e5eca8
MP
3107 ARG_LOC are locations of function arguments (if any).
3108
94a0dd7b 3109 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3110 than the length of VALUES in some error situations), or -1 on
3111 failure. */
94a0dd7b
SL
3112
3113static int
81e5eca8
MP
3114convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3115 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3116 tree function, tree fundecl)
400fbf9f 3117{
bbbbb16a
ILT
3118 tree typetail, val;
3119 unsigned int parmnum;
06302a02 3120 bool error_args = false;
b5d32c25 3121 const bool type_generic = fundecl
81e5eca8 3122 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3123 bool type_generic_remove_excess_precision = false;
03dafa61 3124 tree selector;
03dafa61 3125
2ac2f164
JM
3126 /* Change pointer to function to the function itself for
3127 diagnostics. */
03dafa61
JM
3128 if (TREE_CODE (function) == ADDR_EXPR
3129 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3130 function = TREE_OPERAND (function, 0);
03dafa61
JM
3131
3132 /* Handle an ObjC selector specially for diagnostics. */
3133 selector = objc_message_selector ();
400fbf9f 3134
8ce94e44
JM
3135 /* For type-generic built-in functions, determine whether excess
3136 precision should be removed (classification) or not
3137 (comparison). */
3138 if (type_generic
3139 && DECL_BUILT_IN (fundecl)
3140 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3141 {
3142 switch (DECL_FUNCTION_CODE (fundecl))
3143 {
3144 case BUILT_IN_ISFINITE:
3145 case BUILT_IN_ISINF:
3146 case BUILT_IN_ISINF_SIGN:
3147 case BUILT_IN_ISNAN:
3148 case BUILT_IN_ISNORMAL:
3149 case BUILT_IN_FPCLASSIFY:
3150 type_generic_remove_excess_precision = true;
3151 break;
3152
3153 default:
3154 type_generic_remove_excess_precision = false;
3155 break;
3156 }
3157 }
b72271b9 3158 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3159 return vec_safe_length (values);
8ce94e44 3160
400fbf9f 3161 /* Scan the given expressions and types, producing individual
bbbbb16a 3162 converted arguments. */
400fbf9f 3163
bbbbb16a 3164 for (typetail = typelist, parmnum = 0;
9771b263 3165 values && values->iterate (parmnum, &val);
bbbbb16a 3166 ++parmnum)
400fbf9f 3167 {
b3694847 3168 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3169 tree valtype = TREE_TYPE (val);
03dafa61
JM
3170 tree rname = function;
3171 int argnum = parmnum + 1;
4d3e6fae 3172 const char *invalid_func_diag;
8ce94e44 3173 bool excess_precision = false;
928c19bb 3174 bool npc;
bbbbb16a 3175 tree parmval;
5c1bc275
MP
3176 /* Some __atomic_* builtins have additional hidden argument at
3177 position 0. */
3178 location_t ploc
3179 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3180 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3181 : input_location;
400fbf9f
JW
3182
3183 if (type == void_type_node)
3184 {
19dc6d01 3185 if (selector)
68fca595 3186 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3187 else
68fca595 3188 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3189 inform_declaration (fundecl);
d38f7dce 3190 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3191 }
3192
03dafa61
JM
3193 if (selector && argnum > 2)
3194 {
3195 rname = selector;
3196 argnum -= 2;
3197 }
3198
928c19bb 3199 npc = null_pointer_constant_p (val);
8ce94e44
JM
3200
3201 /* If there is excess precision and a prototype, convert once to
3202 the required type rather than converting via the semantic
3203 type. Likewise without a prototype a float value represented
3204 as long double should be converted once to double. But for
3205 type-generic classification functions excess precision must
3206 be removed here. */
3207 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3208 && (type || !type_generic || !type_generic_remove_excess_precision))
3209 {
3210 val = TREE_OPERAND (val, 0);
3211 excess_precision = true;
3212 }
928c19bb 3213 val = c_fully_fold (val, false, NULL);
ed248cf7 3214 STRIP_TYPE_NOPS (val);
400fbf9f 3215
400fbf9f
JW
3216 val = require_complete_type (val);
3217
3218 if (type != 0)
3219 {
3220 /* Formal parm type is specified by a function prototype. */
400fbf9f 3221
20913689 3222 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3223 {
5c1bc275
MP
3224 error_at (ploc, "type of formal parameter %d is incomplete",
3225 parmnum + 1);
400fbf9f
JW
3226 parmval = val;
3227 }
3228 else
3229 {
bbbbb16a
ILT
3230 tree origtype;
3231
d45cf215
RS
3232 /* Optionally warn about conversions that
3233 differ from the default conversions. */
05170031 3234 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3235 {
e3a64162 3236 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3237
aae43c5f 3238 if (INTEGRAL_TYPE_P (type)
8ce94e44 3239 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3240 warning_at (ploc, OPT_Wtraditional_conversion,
3241 "passing argument %d of %qE as integer rather "
3242 "than floating due to prototype",
3243 argnum, rname);
03829ad2 3244 if (INTEGRAL_TYPE_P (type)
8ce94e44 3245 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3246 warning_at (ploc, OPT_Wtraditional_conversion,
3247 "passing argument %d of %qE as integer rather "
3248 "than complex due to prototype",
3249 argnum, rname);
aae43c5f 3250 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3251 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3252 warning_at (ploc, OPT_Wtraditional_conversion,
3253 "passing argument %d of %qE as complex rather "
3254 "than floating due to prototype",
3255 argnum, rname);
400fbf9f 3256 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3257 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3258 warning_at (ploc, OPT_Wtraditional_conversion,
3259 "passing argument %d of %qE as floating rather "
3260 "than integer due to prototype",
3261 argnum, rname);
03829ad2 3262 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3263 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3264 warning_at (ploc, OPT_Wtraditional_conversion,
3265 "passing argument %d of %qE as complex rather "
3266 "than integer due to prototype",
3267 argnum, rname);
aae43c5f 3268 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3269 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3270 warning_at (ploc, OPT_Wtraditional_conversion,
3271 "passing argument %d of %qE as floating rather "
3272 "than complex due to prototype",
3273 argnum, rname);
aae43c5f
RK
3274 /* ??? At some point, messages should be written about
3275 conversions between complex types, but that's too messy
3276 to do now. */
d45cf215 3277 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3278 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3279 {
3280 /* Warn if any argument is passed as `float',
047de90b 3281 since without a prototype it would be `double'. */
9a8ce21f
JG
3282 if (formal_prec == TYPE_PRECISION (float_type_node)
3283 && type != dfloat32_type_node)
5c1bc275
MP
3284 warning_at (ploc, 0,
3285 "passing argument %d of %qE as %<float%> "
3286 "rather than %<double%> due to prototype",
3287 argnum, rname);
9a8ce21f
JG
3288
3289 /* Warn if mismatch between argument and prototype
3290 for decimal float types. Warn of conversions with
3291 binary float types and of precision narrowing due to
3292 prototype. */
8ce94e44 3293 else if (type != valtype
9a8ce21f
JG
3294 && (type == dfloat32_type_node
3295 || type == dfloat64_type_node
c22cacf3 3296 || type == dfloat128_type_node
8ce94e44
JM
3297 || valtype == dfloat32_type_node
3298 || valtype == dfloat64_type_node
3299 || valtype == dfloat128_type_node)
c22cacf3 3300 && (formal_prec
8ce94e44 3301 <= TYPE_PRECISION (valtype)
9a8ce21f 3302 || (type == dfloat128_type_node
8ce94e44 3303 && (valtype
c22cacf3 3304 != dfloat64_type_node
8ce94e44 3305 && (valtype
9a8ce21f
JG
3306 != dfloat32_type_node)))
3307 || (type == dfloat64_type_node
8ce94e44 3308 && (valtype
9a8ce21f 3309 != dfloat32_type_node))))
5c1bc275
MP
3310 warning_at (ploc, 0,
3311 "passing argument %d of %qE as %qT "
3312 "rather than %qT due to prototype",
3313 argnum, rname, type, valtype);
9a8ce21f 3314
d45cf215 3315 }
3ed56f8a
KG
3316 /* Detect integer changing in width or signedness.
3317 These warnings are only activated with
05170031
MLI
3318 -Wtraditional-conversion, not with -Wtraditional. */
3319 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3320 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3321 {
d45cf215
RS
3322 tree would_have_been = default_conversion (val);
3323 tree type1 = TREE_TYPE (would_have_been);
3324
754a4d82 3325 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3326 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3327 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3328 /* No warning if function asks for enum
3329 and the actual arg is that enum type. */
3330 ;
3331 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3332 warning_at (ploc, OPT_Wtraditional_conversion,
3333 "passing argument %d of %qE "
3334 "with different width due to prototype",
3335 argnum, rname);
8df83eae 3336 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3337 ;
800cd3b9
RS
3338 /* Don't complain if the formal parameter type
3339 is an enum, because we can't tell now whether
3340 the value was an enum--even the same enum. */
3341 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3342 ;
400fbf9f
JW
3343 else if (TREE_CODE (val) == INTEGER_CST
3344 && int_fits_type_p (val, type))
3345 /* Change in signedness doesn't matter
3346 if a constant value is unaffected. */
3347 ;
ce9895ae
RS
3348 /* If the value is extended from a narrower
3349 unsigned type, it doesn't matter whether we
3350 pass it as signed or unsigned; the value
3351 certainly is the same either way. */
8ce94e44
JM
3352 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3353 && TYPE_UNSIGNED (valtype))
ce9895ae 3354 ;
8df83eae 3355 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3356 warning_at (ploc, OPT_Wtraditional_conversion,
3357 "passing argument %d of %qE "
3358 "as unsigned due to prototype",
3359 argnum, rname);
3ed56f8a 3360 else
5c1bc275
MP
3361 warning_at (ploc, OPT_Wtraditional_conversion,
3362 "passing argument %d of %qE "
3363 "as signed due to prototype",
3364 argnum, rname);
400fbf9f
JW
3365 }
3366 }
3367
8ce94e44
JM
3368 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3369 sake of better warnings from convert_and_check. */
3370 if (excess_precision)
3371 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3372 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3373 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3374 val, origtype, ic_argpass,
3375 npc, fundecl, function,
2ac2f164 3376 parmnum + 1);
2f6e4e97 3377
61f71b34 3378 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3379 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3380 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3381 parmval = default_conversion (parmval);
400fbf9f 3382 }
400fbf9f 3383 }
8ce94e44
JM
3384 else if (TREE_CODE (valtype) == REAL_TYPE
3385 && (TYPE_PRECISION (valtype)
2531a1d9 3386 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3387 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3388 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3389 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3390 {
3391 if (type_generic)
bbbbb16a 3392 parmval = val;
b5d32c25 3393 else
0a0b3574
MM
3394 {
3395 /* Convert `float' to `double'. */
3396 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3397 warning_at (ploc, OPT_Wdouble_promotion,
3398 "implicit conversion from %qT to %qT when passing "
3399 "argument to function",
3400 valtype, double_type_node);
0a0b3574
MM
3401 parmval = convert (double_type_node, val);
3402 }
b5d32c25 3403 }
8ce94e44
JM
3404 else if (excess_precision && !type_generic)
3405 /* A "double" argument with excess precision being passed
3406 without a prototype or in variable arguments. */
bbbbb16a 3407 parmval = convert (valtype, val);
c22cacf3
MS
3408 else if ((invalid_func_diag =
3409 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3410 {
3411 error (invalid_func_diag);
94a0dd7b 3412 return -1;
4d3e6fae 3413 }
1807ffc1
MS
3414 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3415 {
3416 return -1;
3417 }
400fbf9f
JW
3418 else
3419 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3420 parmval = default_conversion (val);
3421
9771b263 3422 (*values)[parmnum] = parmval;
06302a02
JJ
3423 if (parmval == error_mark_node)
3424 error_args = true;
400fbf9f
JW
3425
3426 if (typetail)
3427 typetail = TREE_CHAIN (typetail);
3428 }
3429
9771b263 3430 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3431
400fbf9f 3432 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3433 {
68fca595 3434 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3435 inform_declaration (fundecl);
3436 return -1;
3789b316 3437 }
400fbf9f 3438
06302a02 3439 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3440}
3441\f
43f6dfd3
RS
3442/* This is the entry point used by the parser to build unary operators
3443 in the input. CODE, a tree_code, specifies the unary operator, and
3444 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3445 CONVERT_EXPR for code.
3446
3447 LOC is the location to use for the tree generated.
3448*/
43f6dfd3
RS
3449
3450struct c_expr
c2255bc4 3451parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3452{
3453 struct c_expr result;
3454
100d537d 3455 result.original_code = code;
6866c6e8
ILT
3456 result.original_type = NULL;
3457
1807ffc1
MS
3458 if (reject_gcc_builtin (arg.value))
3459 {
3460 result.value = error_mark_node;
3461 }
3462 else
3463 {
3464 result.value = build_unary_op (loc, code, arg.value, 0);
3465
59c0753d 3466 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3467 overflow_warning (loc, result.value);
1807ffc1 3468 }
59c0753d 3469
ebedc9a3
DM
3470 /* We are typically called when parsing a prefix token at LOC acting on
3471 ARG. Reflect this by updating the source range of the result to
3472 start at LOC and end at the end of ARG. */
3473 set_c_expr_source_range (&result,
3474 loc, arg.get_finish ());
3475
43f6dfd3
RS
3476 return result;
3477}
3478
3479/* This is the entry point used by the parser to build binary operators
3480 in the input. CODE, a tree_code, specifies the binary operator, and
3481 ARG1 and ARG2 are the operands. In addition to constructing the
3482 expression, we check for operands that were written with other binary
ba47d38d
AH
3483 operators in a way that is likely to confuse the user.
3484
3485 LOCATION is the location of the binary operator. */
edc7c4ec 3486
487a92fe 3487struct c_expr
ba47d38d
AH
3488parser_build_binary_op (location_t location, enum tree_code code,
3489 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3490{
487a92fe 3491 struct c_expr result;
400fbf9f 3492
487a92fe
JM
3493 enum tree_code code1 = arg1.original_code;
3494 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3495 tree type1 = (arg1.original_type
3496 ? arg1.original_type
3497 : TREE_TYPE (arg1.value));
3498 tree type2 = (arg2.original_type
3499 ? arg2.original_type
3500 : TREE_TYPE (arg2.value));
400fbf9f 3501
ba47d38d
AH
3502 result.value = build_binary_op (location, code,
3503 arg1.value, arg2.value, 1);
487a92fe 3504 result.original_code = code;
6866c6e8 3505 result.original_type = NULL;
58bf601b 3506
487a92fe
JM
3507 if (TREE_CODE (result.value) == ERROR_MARK)
3508 return result;
400fbf9f 3509
ba47d38d
AH
3510 if (location != UNKNOWN_LOCATION)
3511 protected_set_expr_location (result.value, location);
3512
ebedc9a3
DM
3513 set_c_expr_source_range (&result,
3514 arg1.get_start (),
3515 arg2.get_finish ());
3516
400fbf9f 3517 /* Check for cases such as x+y<<z which users are likely
487a92fe 3518 to misinterpret. */
400fbf9f 3519 if (warn_parentheses)
393e8e8b
MP
3520 warn_about_parentheses (location, code, code1, arg1.value, code2,
3521 arg2.value);
001af587 3522
ca409efd 3523 if (warn_logical_op)
393e8e8b 3524 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3525 code1, arg1.value, code2, arg2.value);
63a08740 3526
05b28fd6 3527 if (warn_tautological_compare)
433068cc
MP
3528 {
3529 tree lhs = arg1.value;
3530 tree rhs = arg2.value;
3531 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3532 {
3533 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3534 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3535 lhs = NULL_TREE;
3536 else
3537 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3538 }
3539 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3540 {
3541 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3542 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3543 rhs = NULL_TREE;
3544 else
3545 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3546 }
3547 if (lhs != NULL_TREE && rhs != NULL_TREE)
3548 warn_tautological_cmp (location, code, lhs, rhs);
3549 }
05b28fd6 3550
742938c9 3551 if (warn_logical_not_paren
7ccb1a11 3552 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3553 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3554 && code2 != TRUTH_NOT_EXPR
3555 /* Avoid warning for !!x == y. */
3556 && (TREE_CODE (arg1.value) != NE_EXPR
3557 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3558 {
3559 /* Avoid warning for !b == y where b has _Bool type. */
3560 tree t = integer_zero_node;
3561 if (TREE_CODE (arg1.value) == EQ_EXPR
3562 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3563 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3564 {
3565 t = TREE_OPERAND (arg1.value, 0);
3566 do
3567 {
3568 if (TREE_TYPE (t) != integer_type_node)
3569 break;
3570 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3571 t = C_MAYBE_CONST_EXPR_EXPR (t);
3572 else if (CONVERT_EXPR_P (t))
3573 t = TREE_OPERAND (t, 0);
3574 else
3575 break;
3576 }
3577 while (1);
3578 }
3579 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3580 warn_logical_not_parentheses (location, code, arg2.value);
3581 }
742938c9 3582
e994a705
RS
3583 /* Warn about comparisons against string literals, with the exception
3584 of testing for equality or inequality of a string literal with NULL. */
3585 if (code == EQ_EXPR || code == NE_EXPR)
3586 {
3587 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3588 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3589 warning_at (location, OPT_Waddress,
3590 "comparison with string literal results in unspecified behavior");
e994a705
RS
3591 }
3592 else if (TREE_CODE_CLASS (code) == tcc_comparison
3593 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3594 warning_at (location, OPT_Waddress,
3595 "comparison with string literal results in unspecified behavior");
e994a705 3596
b8698a0f
L
3597 if (TREE_OVERFLOW_P (result.value)
3598 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3599 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3600 overflow_warning (location, result.value);
400fbf9f 3601
6866c6e8
ILT
3602 /* Warn about comparisons of different enum types. */
3603 if (warn_enum_compare
3604 && TREE_CODE_CLASS (code) == tcc_comparison
3605 && TREE_CODE (type1) == ENUMERAL_TYPE
3606 && TREE_CODE (type2) == ENUMERAL_TYPE
3607 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3608 warning_at (location, OPT_Wenum_compare,
3609 "comparison between %qT and %qT",
3610 type1, type2);
3611
400fbf9f
JW
3612 return result;
3613}
3e4093b6 3614\f
3e4093b6
RS
3615/* Return a tree for the difference of pointers OP0 and OP1.
3616 The resulting tree has type int. */
293c9fdd 3617
3e4093b6 3618static tree
db3927fb 3619pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3620{
3e4093b6 3621 tree restype = ptrdiff_type_node;
36c5e70a 3622 tree result, inttype;
400fbf9f 3623
36c5e70a
BE
3624 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3625 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3626 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3627 tree orig_op1 = op1;
400fbf9f 3628
36c5e70a
BE
3629 /* If the operands point into different address spaces, we need to
3630 explicitly convert them to pointers into the common address space
3631 before we can subtract the numerical address values. */
3632 if (as0 != as1)
3633 {
3634 addr_space_t as_common;
3635 tree common_type;
3636
3637 /* Determine the common superset address space. This is guaranteed
3638 to exist because the caller verified that comp_target_types
3639 returned non-zero. */
3640 if (!addr_space_superset (as0, as1, &as_common))
3641 gcc_unreachable ();
3642
3643 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3644 op0 = convert (common_type, op0);
3645 op1 = convert (common_type, op1);
3646 }
3647
3648 /* Determine integer type to perform computations in. This will usually
3649 be the same as the result type (ptrdiff_t), but may need to be a wider
3650 type if pointers for the address space are wider than ptrdiff_t. */
3651 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3652 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3653 else
3654 inttype = restype;
3655
fcf73884 3656 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3657 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3658 "pointer of type %<void *%> used in subtraction");
3659 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3660 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3661 "pointer to a function used in subtraction");
400fbf9f 3662
3e4093b6
RS
3663 /* First do the subtraction as integers;
3664 then drop through to build the divide operator.
3665 Do not do default conversions on the minus operator
3666 in case restype is a short type. */
400fbf9f 3667
db3927fb 3668 op0 = build_binary_op (loc,
36c5e70a
BE
3669 MINUS_EXPR, convert (inttype, op0),
3670 convert (inttype, op1), 0);
3e4093b6
RS
3671 /* This generates an error if op1 is pointer to incomplete type. */
3672 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3673 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3674
3e4093b6 3675 op1 = c_size_in_bytes (target_type);
400fbf9f 3676
f04dda30
MP
3677 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3678 error_at (loc, "arithmetic on pointer to an empty aggregate");
3679
3e4093b6 3680 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3681 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3682 op0, convert (inttype, op1));
3683
3684 /* Convert to final result type if necessary. */
3685 return convert (restype, result);
3e4093b6
RS
3686}
3687\f
267bac10
JM
3688/* Expand atomic compound assignments into an approriate sequence as
3689 specified by the C11 standard section 6.5.16.2.
3690 given
3691 _Atomic T1 E1
3692 T2 E2
3693 E1 op= E2
3694
3695 This sequence is used for all types for which these operations are
3696 supported.
3697
3698 In addition, built-in versions of the 'fe' prefixed routines may
3699 need to be invoked for floating point (real, complex or vector) when
3700 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3701
3702 T1 newval;
3703 T1 old;
3704 T1 *addr
3705 T2 val
3706 fenv_t fenv
3707
3708 addr = &E1;
3709 val = (E2);
3710 __atomic_load (addr, &old, SEQ_CST);
3711 feholdexcept (&fenv);
3712loop:
3713 newval = old op val;
3714 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3715 SEQ_CST))
3716 goto done;
3717 feclearexcept (FE_ALL_EXCEPT);
3718 goto loop:
3719done:
3720 feupdateenv (&fenv);
3721
3722 Also note that the compiler is simply issuing the generic form of
3723 the atomic operations. This requires temp(s) and has their address
3724 taken. The atomic processing is smart enough to figure out when the
3725 size of an object can utilize a lock-free version, and convert the
3726 built-in call to the appropriate lock-free routine. The optimizers
3727 will then dispose of any temps that are no longer required, and
3728 lock-free implementations are utilized as long as there is target
3729 support for the required size.
3730
3731 If the operator is NOP_EXPR, then this is a simple assignment, and
3732 an __atomic_store is issued to perform the assignment rather than
3733 the above loop.
3734
3735*/
3736
3737/* Build an atomic assignment at LOC, expanding into the proper
3738 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3739 the result of the operation, unless RETURN_OLD_P in which case
3740 return the old value of LHS (this is only for postincrement and
3741 postdecrement). */
3742static tree
3743build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3744 tree rhs, bool return_old_p)
3745{
3746 tree fndecl, func_call;
3747 vec<tree, va_gc> *params;
3748 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3749 tree old, old_addr;
3750 tree compound_stmt;
3751 tree stmt, goto_stmt;
3752 tree loop_label, loop_decl, done_label, done_decl;
3753
3754 tree lhs_type = TREE_TYPE (lhs);
3755 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3756 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3757 tree rhs_type = TREE_TYPE (rhs);
3758
3759 gcc_assert (TYPE_ATOMIC (lhs_type));
3760
3761 if (return_old_p)
3762 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3763
3764 /* Allocate enough vector items for a compare_exchange. */
3765 vec_alloc (params, 6);
3766
3767 /* Create a compound statement to hold the sequence of statements
3768 with a loop. */
3769 compound_stmt = c_begin_compound_stmt (false);
3770
3771 /* Fold the RHS if it hasn't already been folded. */
3772 if (modifycode != NOP_EXPR)
3773 rhs = c_fully_fold (rhs, false, NULL);
3774
3775 /* Remove the qualifiers for the rest of the expressions and create
3776 the VAL temp variable to hold the RHS. */
3777 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3778 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3779 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3780 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3781 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3782 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3783 NULL_TREE);
267bac10
JM
3784 SET_EXPR_LOCATION (rhs, loc);
3785 add_stmt (rhs);
3786
3787 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3788 an atomic_store. */
3789 if (modifycode == NOP_EXPR)
3790 {
3791 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3792 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3793 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3794 params->quick_push (lhs_addr);
3795 params->quick_push (rhs);
3796 params->quick_push (seq_cst);
8edbfaa6 3797 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3798 add_stmt (func_call);
3799
3800 /* Finish the compound statement. */
3801 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3802
3803 /* VAL is the value which was stored, return a COMPOUND_STMT of
3804 the statement and that value. */
3805 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3806 }
3807
3808 /* Create the variables and labels required for the op= form. */
5c4abbb8 3809 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3810 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3811 TREE_ADDRESSABLE (old) = 1;
3812 TREE_NO_WARNING (old) = 1;
267bac10 3813
5c4abbb8 3814 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3815 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3816 TREE_ADDRESSABLE (newval) = 1;
3817
3818 loop_decl = create_artificial_label (loc);
3819 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3820
3821 done_decl = create_artificial_label (loc);
3822 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3823
3824 /* __atomic_load (addr, &old, SEQ_CST). */
3825 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3826 params->quick_push (lhs_addr);
3827 params->quick_push (old_addr);
3828 params->quick_push (seq_cst);
8edbfaa6 3829 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3830 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3831 NULL_TREE);
3832 add_stmt (old);
267bac10
JM
3833 params->truncate (0);
3834
3835 /* Create the expressions for floating-point environment
3836 manipulation, if required. */
3837 bool need_fenv = (flag_trapping_math
3838 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3839 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3840 if (need_fenv)
3841 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3842
3843 if (hold_call)
3844 add_stmt (hold_call);
3845
3846 /* loop: */
3847 add_stmt (loop_label);
3848
3849 /* newval = old + val; */
3850 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3851 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3852 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3853 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3854 NULL_TREE, 0);
3855 if (rhs != error_mark_node)
3856 {
5c4abbb8
MP
3857 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3858 NULL_TREE);
267bac10
JM
3859 SET_EXPR_LOCATION (rhs, loc);
3860 add_stmt (rhs);
3861 }
3862
3863 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3864 goto done; */
3865 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3866 params->quick_push (lhs_addr);
3867 params->quick_push (old_addr);
3868 params->quick_push (newval_addr);
3869 params->quick_push (integer_zero_node);
3870 params->quick_push (seq_cst);
3871 params->quick_push (seq_cst);
8edbfaa6 3872 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3873
3874 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3875 SET_EXPR_LOCATION (goto_stmt, loc);
3876
3877 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3878 SET_EXPR_LOCATION (stmt, loc);
3879 add_stmt (stmt);
5c4abbb8 3880
267bac10
JM
3881 if (clear_call)
3882 add_stmt (clear_call);
3883
3884 /* goto loop; */
3885 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3886 SET_EXPR_LOCATION (goto_stmt, loc);
3887 add_stmt (goto_stmt);
5c4abbb8 3888
267bac10
JM
3889 /* done: */
3890 add_stmt (done_label);
3891
3892 if (update_call)
3893 add_stmt (update_call);
3894
3895 /* Finish the compound statement. */
3896 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3897
3898 /* NEWVAL is the value that was successfully stored, return a
3899 COMPOUND_EXPR of the statement and the appropriate value. */
3900 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3901 return_old_p ? old : newval);
3902}
3903
3e4093b6
RS
3904/* Construct and perhaps optimize a tree representation
3905 for a unary operation. CODE, a tree_code, specifies the operation
3906 and XARG is the operand.
3907 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3908 the default promotions (such as from short to int).
3909 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3910 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3911 arrays to pointers in C99.
3912
3913 LOCATION is the location of the operator. */
400fbf9f 3914
3e4093b6 3915tree
c9f9eb5d
AH
3916build_unary_op (location_t location,
3917 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3918{
3919 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3920 tree arg = xarg;
3921 tree argtype = 0;
808d6eaa 3922 enum tree_code typecode;
3e4093b6 3923 tree val;
c9f9eb5d 3924 tree ret = error_mark_node;
8ce94e44 3925 tree eptype = NULL_TREE;
3e4093b6 3926 int noconvert = flag;
4de67c26 3927 const char *invalid_op_diag;
928c19bb
JM
3928 bool int_operands;
3929
3930 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3931 if (int_operands)
3932 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3933
808d6eaa
JM
3934 if (code != ADDR_EXPR)
3935 arg = require_complete_type (arg);
3936
3937 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3938 if (typecode == ERROR_MARK)
3939 return error_mark_node;
3940 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3941 typecode = INTEGER_TYPE;
6c36d76b 3942
4de67c26
JM
3943 if ((invalid_op_diag
3944 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3945 {
c9f9eb5d 3946 error_at (location, invalid_op_diag);
4de67c26
JM
3947 return error_mark_node;
3948 }
3949
8ce94e44
JM
3950 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3951 {
3952 eptype = TREE_TYPE (arg);
3953 arg = TREE_OPERAND (arg, 0);
3954 }
3955
3e4093b6
RS
3956 switch (code)
3957 {
3958 case CONVERT_EXPR:
3959 /* This is used for unary plus, because a CONVERT_EXPR
3960 is enough to prevent anybody from looking inside for
3961 associativity, but won't generate any code. */
3962 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3963 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3964 || typecode == VECTOR_TYPE))
400fbf9f 3965 {
c9f9eb5d 3966 error_at (location, "wrong type argument to unary plus");
3e4093b6 3967 return error_mark_node;
400fbf9f 3968 }
3e4093b6
RS
3969 else if (!noconvert)
3970 arg = default_conversion (arg);
db3927fb 3971 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3972 break;
3973
3e4093b6
RS
3974 case NEGATE_EXPR:
3975 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3976 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3977 || typecode == VECTOR_TYPE))
3978 {
c9f9eb5d 3979 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3980 return error_mark_node;
3981 }
3982 else if (!noconvert)
3983 arg = default_conversion (arg);
400fbf9f
JW
3984 break;
3985
3e4093b6 3986 case BIT_NOT_EXPR:
462643f0
AP
3987 /* ~ works on integer types and non float vectors. */
3988 if (typecode == INTEGER_TYPE
3989 || (typecode == VECTOR_TYPE
3990 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3991 {
3e4093b6
RS
3992 if (!noconvert)
3993 arg = default_conversion (arg);
03d5b1f5 3994 }
3e4093b6 3995 else if (typecode == COMPLEX_TYPE)
400fbf9f 3996 {
3e4093b6 3997 code = CONJ_EXPR;
c1771a20 3998 pedwarn (location, OPT_Wpedantic,
fcf73884 3999 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4000 if (!noconvert)
4001 arg = default_conversion (arg);
4002 }
4003 else
4004 {
c9f9eb5d 4005 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4006 return error_mark_node;
400fbf9f
JW
4007 }
4008 break;
4009
3e4093b6 4010 case ABS_EXPR:
11017cc7 4011 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4012 {
c9f9eb5d 4013 error_at (location, "wrong type argument to abs");
3e4093b6 4014 return error_mark_node;
400fbf9f 4015 }
3e4093b6
RS
4016 else if (!noconvert)
4017 arg = default_conversion (arg);
400fbf9f
JW
4018 break;
4019
3e4093b6
RS
4020 case CONJ_EXPR:
4021 /* Conjugating a real value is a no-op, but allow it anyway. */
4022 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4023 || typecode == COMPLEX_TYPE))
400fbf9f 4024 {
c9f9eb5d 4025 error_at (location, "wrong type argument to conjugation");
3e4093b6 4026 return error_mark_node;
400fbf9f 4027 }
3e4093b6
RS
4028 else if (!noconvert)
4029 arg = default_conversion (arg);
400fbf9f
JW
4030 break;
4031
3e4093b6 4032 case TRUTH_NOT_EXPR:
ab22c1fa 4033 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4034 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4035 && typecode != COMPLEX_TYPE)
400fbf9f 4036 {
c9f9eb5d
AH
4037 error_at (location,
4038 "wrong type argument to unary exclamation mark");
3e4093b6 4039 return error_mark_node;
400fbf9f 4040 }
a27d595d
JM
4041 if (int_operands)
4042 {
4043 arg = c_objc_common_truthvalue_conversion (location, xarg);
4044 arg = remove_c_maybe_const_expr (arg);
4045 }
4046 else
4047 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4048 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4049 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4050 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4051 location = EXPR_LOCATION (ret);
c9f9eb5d 4052 goto return_build_unary_op;
3e4093b6 4053
3e4093b6 4054 case REALPART_EXPR:
3e4093b6 4055 case IMAGPART_EXPR:
fb52b50a
NF
4056 ret = build_real_imag_expr (location, code, arg);
4057 if (ret == error_mark_node)
4058 return error_mark_node;
8ce94e44
JM
4059 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4060 eptype = TREE_TYPE (eptype);
c9f9eb5d 4061 goto return_build_unary_op;
3e4093b6
RS
4062
4063 case PREINCREMENT_EXPR:
4064 case POSTINCREMENT_EXPR:
4065 case PREDECREMENT_EXPR:
4066 case POSTDECREMENT_EXPR:
3e4093b6 4067
928c19bb
JM
4068 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4069 {
4070 tree inner = build_unary_op (location, code,
4071 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4072 if (inner == error_mark_node)
4073 return error_mark_node;
4074 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4075 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4076 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4077 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4078 goto return_build_unary_op;
4079 }
4080
925e8657
NP
4081 /* Complain about anything that is not a true lvalue. In
4082 Objective-C, skip this check for property_refs. */
f90e8e2e 4083 if (!objc_is_property_ref (arg)
7bd11157
TT
4084 && !lvalue_or_else (location,
4085 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4086 || code == POSTINCREMENT_EXPR)
4087 ? lv_increment
4088 : lv_decrement)))
928c19bb
JM
4089 return error_mark_node;
4090
09639a83
ILT
4091 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4092 {
4093 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4094 warning_at (location, OPT_Wc___compat,
4095 "increment of enumeration value is invalid in C++");
4096 else
4097 warning_at (location, OPT_Wc___compat,
4098 "decrement of enumeration value is invalid in C++");
4099 }
4100
928c19bb
JM
4101 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4102 arg = c_fully_fold (arg, false, NULL);
4103
267bac10
JM
4104 bool atomic_op;
4105 atomic_op = really_atomic_lvalue (arg);
4106
3e4093b6
RS
4107 /* Increment or decrement the real part of the value,
4108 and don't change the imaginary part. */
4109 if (typecode == COMPLEX_TYPE)
400fbf9f 4110 {
3e4093b6
RS
4111 tree real, imag;
4112
c1771a20 4113 pedwarn (location, OPT_Wpedantic,
509c9d60 4114 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4115
267bac10
JM
4116 if (!atomic_op)
4117 {
4118 arg = stabilize_reference (arg);
4119 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4120 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4121 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4122 if (real == error_mark_node || imag == error_mark_node)
4123 return error_mark_node;
4124 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4125 real, imag);
4126 goto return_build_unary_op;
4127 }
400fbf9f 4128 }
3e4093b6
RS
4129
4130 /* Report invalid types. */
4131
ab22c1fa 4132 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4133 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4134 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4135 {
3e4093b6 4136 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4137 error_at (location, "wrong type argument to increment");
c22cacf3 4138 else
c9f9eb5d 4139 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4140
4141 return error_mark_node;
400fbf9f 4142 }
400fbf9f 4143
3e4093b6
RS
4144 {
4145 tree inc;
400fbf9f 4146
3e4093b6
RS
4147 argtype = TREE_TYPE (arg);
4148
4149 /* Compute the increment. */
4150
4151 if (typecode == POINTER_TYPE)
4152 {
a0e24419 4153 /* If pointer target is an incomplete type,
3e4093b6 4154 we just cannot know how to do the arithmetic. */
b70cef5d 4155 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4156 {
4157 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4158 error_at (location,
a0e24419
MP
4159 "increment of pointer to an incomplete type %qT",
4160 TREE_TYPE (argtype));
3e4093b6 4161 else
c9f9eb5d 4162 error_at (location,
a0e24419
MP
4163 "decrement of pointer to an incomplete type %qT",
4164 TREE_TYPE (argtype));
3e4093b6 4165 }
b70cef5d
JJ
4166 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4167 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4168 {
3e4093b6 4169 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4170 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4171 "wrong type argument to increment");
3e4093b6 4172 else
44d90fe1 4173 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4174 "wrong type argument to decrement");
3e4093b6
RS
4175 }
4176
b70cef5d 4177 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4178 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4179 }
b70cef5d 4180 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4181 {
4182 /* For signed fract types, we invert ++ to -- or
4183 -- to ++, and change inc from 1 to -1, because
4184 it is not possible to represent 1 in signed fract constants.
4185 For unsigned fract types, the result always overflows and
4186 we get an undefined (original) or the maximum value. */
4187 if (code == PREINCREMENT_EXPR)
4188 code = PREDECREMENT_EXPR;
4189 else if (code == PREDECREMENT_EXPR)
4190 code = PREINCREMENT_EXPR;
4191 else if (code == POSTINCREMENT_EXPR)
4192 code = POSTDECREMENT_EXPR;
4193 else /* code == POSTDECREMENT_EXPR */
4194 code = POSTINCREMENT_EXPR;
4195
4196 inc = integer_minus_one_node;
4197 inc = convert (argtype, inc);
4198 }
3e4093b6 4199 else
5be014d5 4200 {
241b71bb
TV
4201 inc = VECTOR_TYPE_P (argtype)
4202 ? build_one_cst (argtype)
4203 : integer_one_node;
5be014d5
AP
4204 inc = convert (argtype, inc);
4205 }
3e4093b6 4206
925e8657
NP
4207 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4208 need to ask Objective-C to build the increment or decrement
4209 expression for it. */
4210 if (objc_is_property_ref (arg))
f90e8e2e 4211 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4212 arg, inc);
4213
3e4093b6 4214 /* Report a read-only lvalue. */
f37acdf9 4215 if (TYPE_READONLY (argtype))
953ff289 4216 {
c02065fc 4217 readonly_error (location, arg,
953ff289
DN
4218 ((code == PREINCREMENT_EXPR
4219 || code == POSTINCREMENT_EXPR)
4220 ? lv_increment : lv_decrement));
4221 return error_mark_node;
4222 }
f37acdf9
JM
4223 else if (TREE_READONLY (arg))
4224 readonly_warning (arg,
4225 ((code == PREINCREMENT_EXPR
4226 || code == POSTINCREMENT_EXPR)
4227 ? lv_increment : lv_decrement));
3e4093b6 4228
267bac10
JM
4229 /* If the argument is atomic, use the special code sequences for
4230 atomic compound assignment. */
4231 if (atomic_op)
4232 {
4233 arg = stabilize_reference (arg);
4234 ret = build_atomic_assign (location, arg,
4235 ((code == PREINCREMENT_EXPR
4236 || code == POSTINCREMENT_EXPR)
4237 ? PLUS_EXPR
4238 : MINUS_EXPR),
4239 (FRACT_MODE_P (TYPE_MODE (argtype))
4240 ? inc
4241 : integer_one_node),
4242 (code == POSTINCREMENT_EXPR
4243 || code == POSTDECREMENT_EXPR));
4244 goto return_build_unary_op;
4245 }
4246
3e4093b6
RS
4247 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4248 val = boolean_increment (code, arg);
4249 else
53fb4de3 4250 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4251 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4252 if (TREE_CODE (val) != code)
6de9cd9a 4253 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4254 ret = val;
4255 goto return_build_unary_op;
3e4093b6
RS
4256 }
4257
4258 case ADDR_EXPR:
4259 /* Note that this operation never does default_conversion. */
4260
2b4b7036
JM
4261 /* The operand of unary '&' must be an lvalue (which excludes
4262 expressions of type void), or, in C99, the result of a [] or
4263 unary '*' operator. */
4264 if (VOID_TYPE_P (TREE_TYPE (arg))
4265 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4266 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4267 pedwarn (location, 0, "taking address of expression of type %<void%>");
4268
3e4093b6 4269 /* Let &* cancel out to simplify resulting code. */
22d03525 4270 if (INDIRECT_REF_P (arg))
400fbf9f 4271 {
3e4093b6
RS
4272 /* Don't let this be an lvalue. */
4273 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4274 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4275 ret = TREE_OPERAND (arg, 0);
4276 goto return_build_unary_op;
400fbf9f 4277 }
1eb8759b 4278
3e4093b6
RS
4279 /* Anything not already handled and not a true memory reference
4280 or a non-lvalue array is an error. */
ee45a32d
EB
4281 if (typecode != FUNCTION_TYPE && !flag
4282 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4283 return error_mark_node;
b6a10c9f 4284
928c19bb
JM
4285 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4286 folding later. */
4287 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4288 {
4289 tree inner = build_unary_op (location, code,
4290 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4291 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4292 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4293 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4294 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4295 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4296 goto return_build_unary_op;
4297 }
4298
3e4093b6
RS
4299 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4300 argtype = TREE_TYPE (arg);
400fbf9f 4301
3e4093b6 4302 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4303 to which the address will point. This is only needed
f2c1da78 4304 for function types. */
6615c446 4305 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4306 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4307 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4308 {
4309 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4310 int quals = orig_quals;
4311
4312 if (TREE_READONLY (arg))
4313 quals |= TYPE_QUAL_CONST;
4314 if (TREE_THIS_VOLATILE (arg))
4315 quals |= TYPE_QUAL_VOLATILE;
4316
f2c1da78
JM
4317 argtype = c_build_qualified_type (argtype, quals);
4318 }
400fbf9f 4319
ee45a32d
EB
4320 switch (TREE_CODE (arg))
4321 {
4322 case COMPONENT_REF:
4323 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4324 {
4325 error ("cannot take address of bit-field %qD",
4326 TREE_OPERAND (arg, 1));
4327 return error_mark_node;
4328 }
4329
4330 /* ... fall through ... */
4331
4332 case ARRAY_REF:
4333 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4334 {
4335 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4336 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4337 {
4338 error ("cannot take address of scalar with reverse storage "
4339 "order");
4340 return error_mark_node;
4341 }
4342
4343 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4344 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
4345 warning (OPT_Wscalar_storage_order, "address of array with "
4346 "reverse scalar storage order requested");
4347 }
4348
4349 default:
4350 break;
4351 }
4352
3e4093b6
RS
4353 if (!c_mark_addressable (arg))
4354 return error_mark_node;
400fbf9f 4355
abb54d14
JM
4356 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4357 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4358
5cc200fc 4359 argtype = build_pointer_type (argtype);
5e55f99d
RH
4360
4361 /* ??? Cope with user tricks that amount to offsetof. Delete this
4362 when we have proper support for integer constant expressions. */
4363 val = get_base_address (arg);
22d03525 4364 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4365 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4366 {
cf9e9959 4367 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4368 goto return_build_unary_op;
3aa2ddb8 4369 }
5e55f99d 4370
5cc200fc 4371 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4372
c9f9eb5d
AH
4373 ret = val;
4374 goto return_build_unary_op;
400fbf9f 4375
3e4093b6 4376 default:
1344f9a3 4377 gcc_unreachable ();
3e4093b6 4378 }
400fbf9f 4379
3e4093b6
RS
4380 if (argtype == 0)
4381 argtype = TREE_TYPE (arg);
928c19bb
JM
4382 if (TREE_CODE (arg) == INTEGER_CST)
4383 ret = (require_constant_value
db3927fb
AH
4384 ? fold_build1_initializer_loc (location, code, argtype, arg)
4385 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4386 else
4387 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4388 return_build_unary_op:
4389 gcc_assert (ret != error_mark_node);
928c19bb
JM
4390 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4391 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4392 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4393 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4394 ret = note_integer_operands (ret);
8ce94e44
JM
4395 if (eptype)
4396 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4397 protected_set_expr_location (ret, location);
4398 return ret;
3e4093b6 4399}
400fbf9f 4400
3e4093b6
RS
4401/* Return nonzero if REF is an lvalue valid for this language.
4402 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4403 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4404
1e4ae551 4405bool
58f9752a 4406lvalue_p (const_tree ref)
3e4093b6 4407{
58f9752a 4408 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4409
3e4093b6
RS
4410 switch (code)
4411 {
4412 case REALPART_EXPR:
4413 case IMAGPART_EXPR:
4414 case COMPONENT_REF:
4415 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4416
928c19bb
JM
4417 case C_MAYBE_CONST_EXPR:
4418 return lvalue_p (TREE_OPERAND (ref, 1));
4419
3e4093b6
RS
4420 case COMPOUND_LITERAL_EXPR:
4421 case STRING_CST:
4422 return 1;
400fbf9f 4423
3e4093b6
RS
4424 case INDIRECT_REF:
4425 case ARRAY_REF:
36536d79 4426 case ARRAY_NOTATION_REF:
3e4093b6
RS
4427 case VAR_DECL:
4428 case PARM_DECL:
4429 case RESULT_DECL:
4430 case ERROR_MARK:
4431 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4432 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4433
3e4093b6 4434 case BIND_EXPR:
3e4093b6 4435 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4436
3e4093b6
RS
4437 default:
4438 return 0;
4439 }
4440}
400fbf9f 4441\f
f37acdf9
JM
4442/* Give a warning for storing in something that is read-only in GCC
4443 terms but not const in ISO C terms. */
4444
4445static void
4446readonly_warning (tree arg, enum lvalue_use use)
4447{
4448 switch (use)
4449 {
4450 case lv_assign:
4451 warning (0, "assignment of read-only location %qE", arg);
4452 break;
4453 case lv_increment:
4454 warning (0, "increment of read-only location %qE", arg);
4455 break;
4456 case lv_decrement:
4457 warning (0, "decrement of read-only location %qE", arg);
4458 break;
4459 default:
4460 gcc_unreachable ();
4461 }
4462 return;
4463}
4464
37dc0d8d
JM
4465
4466/* Return nonzero if REF is an lvalue valid for this language;
4467 otherwise, print an error message and return zero. USE says
7bd11157
TT
4468 how the lvalue is being used and so selects the error message.
4469 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4470
4471static int
7bd11157 4472lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4473{
4474 int win = lvalue_p (ref);
4475
4476 if (!win)
7bd11157 4477 lvalue_error (loc, use);
37dc0d8d
JM
4478
4479 return win;
4480}
3e4093b6
RS
4481\f
4482/* Mark EXP saying that we need to be able to take the
4483 address of it; it should not be allocated in a register.
4484 Returns true if successful. */
54c93c30 4485
3e4093b6
RS
4486bool
4487c_mark_addressable (tree exp)
400fbf9f 4488{
3e4093b6 4489 tree x = exp;
95602da1 4490
3e4093b6
RS
4491 while (1)
4492 switch (TREE_CODE (x))
4493 {
4494 case COMPONENT_REF:
3e4093b6
RS
4495 case ADDR_EXPR:
4496 case ARRAY_REF:
4497 case REALPART_EXPR:
4498 case IMAGPART_EXPR:
4499 x = TREE_OPERAND (x, 0);
4500 break;
95602da1 4501
3e4093b6
RS
4502 case COMPOUND_LITERAL_EXPR:
4503 case CONSTRUCTOR:
4504 TREE_ADDRESSABLE (x) = 1;
4505 return true;
95602da1 4506
3e4093b6
RS
4507 case VAR_DECL:
4508 case CONST_DECL:
4509 case PARM_DECL:
4510 case RESULT_DECL:
5baeaac0 4511 if (C_DECL_REGISTER (x)
3e4093b6
RS
4512 && DECL_NONLOCAL (x))
4513 {
62f9079a 4514 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4515 {
0039fa55
AN
4516 error
4517 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4518 return false;
4519 }
509c9d60 4520 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4521 }
5baeaac0 4522 else if (C_DECL_REGISTER (x))
3e4093b6 4523 {
62f9079a 4524 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4525 error ("address of global register variable %qD requested", x);
4526 else
4527 error ("address of register variable %qD requested", x);
4528 return false;
3e4093b6 4529 }
400fbf9f 4530
3e4093b6
RS
4531 /* drops in */
4532 case FUNCTION_DECL:
4533 TREE_ADDRESSABLE (x) = 1;
4534 /* drops out */
4535 default:
4536 return true;
4537 }
4538}
4539\f
2d2e923f
MLI
4540/* Convert EXPR to TYPE, warning about conversion problems with
4541 constants. SEMANTIC_TYPE is the type this conversion would use
4542 without excess precision. If SEMANTIC_TYPE is NULL, this function
4543 is equivalent to convert_and_check. This function is a wrapper that
4544 handles conversions that may be different than
4545 the usual ones because of excess precision. */
4546
4547static tree
68fca595
MP
4548ep_convert_and_check (location_t loc, tree type, tree expr,
4549 tree semantic_type)
2d2e923f
MLI
4550{
4551 if (TREE_TYPE (expr) == type)
4552 return expr;
4553
4554 if (!semantic_type)
68fca595 4555 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4556
4557 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4558 && TREE_TYPE (expr) != semantic_type)
4559 {
4560 /* For integers, we need to check the real conversion, not
4561 the conversion to the excess precision type. */
68fca595 4562 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4563 }
4564 /* Result type is the excess precision type, which should be
4565 large enough, so do not check. */
4566 return convert (type, expr);
4567}
4568
928c19bb
JM
4569/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4570 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4571 if folded to an integer constant then the unselected half may
4572 contain arbitrary operations not normally permitted in constant
c2255bc4 4573 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4574
4575tree
744aa42f 4576build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4577 tree op1, tree op1_original_type, tree op2,
4578 tree op2_original_type)
400fbf9f 4579{
3e4093b6
RS
4580 tree type1;
4581 tree type2;
4582 enum tree_code code1;
4583 enum tree_code code2;
4584 tree result_type = NULL;
2d2e923f 4585 tree semantic_result_type = NULL;
3e4093b6 4586 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4587 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4588 bool ifexp_int_operands;
928c19bb 4589 tree ret;
400fbf9f 4590
4d84fe7c
JM
4591 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4592 if (op1_int_operands)
4593 op1 = remove_c_maybe_const_expr (op1);
4594 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4595 if (op2_int_operands)
4596 op2 = remove_c_maybe_const_expr (op2);
4597 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4598 if (ifexp_int_operands)
4599 ifexp = remove_c_maybe_const_expr (ifexp);
4600
3e4093b6
RS
4601 /* Promote both alternatives. */
4602
4603 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4604 op1 = default_conversion (op1);
4605 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4606 op2 = default_conversion (op2);
4607
4608 if (TREE_CODE (ifexp) == ERROR_MARK
4609 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4610 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4611 return error_mark_node;
400fbf9f 4612
3e4093b6
RS
4613 type1 = TREE_TYPE (op1);
4614 code1 = TREE_CODE (type1);
4615 type2 = TREE_TYPE (op2);
4616 code2 = TREE_CODE (type2);
4617
1807ffc1
MS
4618 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4619 return error_mark_node;
4620
4621 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4622 return error_mark_node;
4623
b1adf557
JM
4624 /* C90 does not permit non-lvalue arrays in conditional expressions.
4625 In C99 they will be pointers by now. */
4626 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4627 {
744aa42f 4628 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4629 return error_mark_node;
4630 }
4631
8ce94e44
JM
4632 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4633 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4634 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4635 || code1 == COMPLEX_TYPE)
4636 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4637 || code2 == COMPLEX_TYPE))
4638 {
2d2e923f 4639 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4640 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4641 {
4642 op1 = TREE_OPERAND (op1, 0);
4643 type1 = TREE_TYPE (op1);
4644 gcc_assert (TREE_CODE (type1) == code1);
4645 }
4646 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4647 {
4648 op2 = TREE_OPERAND (op2, 0);
4649 type2 = TREE_TYPE (op2);
4650 gcc_assert (TREE_CODE (type2) == code2);
4651 }
4652 }
4653
d130ae11
ILT
4654 if (warn_cxx_compat)
4655 {
4656 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4657 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4658
4659 if (TREE_CODE (t1) == ENUMERAL_TYPE
4660 && TREE_CODE (t2) == ENUMERAL_TYPE
4661 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4662 warning_at (colon_loc, OPT_Wc___compat,
4663 ("different enum types in conditional is "
4664 "invalid in C++: %qT vs %qT"),
4665 t1, t2);
4666 }
4667
3e4093b6
RS
4668 /* Quickly detect the usual case where op1 and op2 have the same type
4669 after promotion. */
4670 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4671 {
3e4093b6
RS
4672 if (type1 == type2)
4673 result_type = type1;
4674 else
4675 result_type = TYPE_MAIN_VARIANT (type1);
4676 }
4677 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4678 || code1 == COMPLEX_TYPE)
4679 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4680 || code2 == COMPLEX_TYPE))
3e4093b6 4681 {
ccf7f880 4682 result_type = c_common_type (type1, type2);
0a0b3574
MM
4683 do_warn_double_promotion (result_type, type1, type2,
4684 "implicit conversion from %qT to %qT to "
4685 "match other result of conditional",
4686 colon_loc);
400fbf9f 4687
3e4093b6
RS
4688 /* If -Wsign-compare, warn here if type1 and type2 have
4689 different signedness. We'll promote the signed to unsigned
4690 and later code won't know it used to be different.
4691 Do this check on the original types, so that explicit casts
4692 will be considered, but default promotions won't. */
7d882b83 4693 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4694 {
8df83eae
RK
4695 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4696 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4697
3e4093b6
RS
4698 if (unsigned_op1 ^ unsigned_op2)
4699 {
6ac01510
ILT
4700 bool ovf;
4701
3e4093b6
RS
4702 /* Do not warn if the result type is signed, since the
4703 signed type will only be chosen if it can represent
4704 all the values of the unsigned type. */
3f75a254 4705 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4706 /* OK */;
3e4093b6 4707 else
928c19bb
JM
4708 {
4709 bool op1_maybe_const = true;
4710 bool op2_maybe_const = true;
4711
4712 /* Do not warn if the signed quantity is an
4713 unsuffixed integer literal (or some static
4714 constant expression involving such literals) and
4715 it is non-negative. This warning requires the
4716 operands to be folded for best results, so do
4717 that folding in this case even without
4718 warn_sign_compare to avoid warning options
4719 possibly affecting code generation. */
f5178456
RS
4720 c_inhibit_evaluation_warnings
4721 += (ifexp == truthvalue_false_node);
928c19bb
JM
4722 op1 = c_fully_fold (op1, require_constant_value,
4723 &op1_maybe_const);
f5178456
RS
4724 c_inhibit_evaluation_warnings
4725 -= (ifexp == truthvalue_false_node);
4726
4727 c_inhibit_evaluation_warnings
4728 += (ifexp == truthvalue_true_node);
928c19bb
JM
4729 op2 = c_fully_fold (op2, require_constant_value,
4730 &op2_maybe_const);
f5178456
RS
4731 c_inhibit_evaluation_warnings
4732 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4733
4734 if (warn_sign_compare)
4735 {
4736 if ((unsigned_op2
4737 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4738 || (unsigned_op1
4739 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4740 /* OK */;
4741 else
744aa42f
ILT
4742 warning_at (colon_loc, OPT_Wsign_compare,
4743 ("signed and unsigned type in "
4744 "conditional expression"));
928c19bb
JM
4745 }
4746 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4747 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4748 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4749 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4750 }
3e4093b6
RS
4751 }
4752 }
4753 }
4754 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4755 {
fcf73884 4756 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4757 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4758 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4759 result_type = void_type_node;
4760 }
4761 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4762 {
36c5e70a
BE
4763 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4764 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4765 addr_space_t as_common;
4766
744aa42f 4767 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4768 result_type = common_pointer_type (type1, type2);
6aa3c60d 4769 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4770 result_type = type2;
6aa3c60d 4771 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4772 result_type = type1;
4773 else if (!addr_space_superset (as1, as2, &as_common))
4774 {
4775 error_at (colon_loc, "pointers to disjoint address spaces "
4776 "used in conditional expression");
4777 return error_mark_node;
4778 }
267bac10
JM
4779 else if (VOID_TYPE_P (TREE_TYPE (type1))
4780 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4781 {
768952be
MU
4782 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4783 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4784 & ~TYPE_QUALS (TREE_TYPE (type1))))
4785 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4786 "pointer to array loses qualifier "
4787 "in conditional expression");
4788
fcf73884 4789 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4790 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4791 "ISO C forbids conditional expr between "
bda67431 4792 "%<void *%> and function pointer");
3e4093b6
RS
4793 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4794 TREE_TYPE (type2)));
34a80643 4795 }
267bac10
JM
4796 else if (VOID_TYPE_P (TREE_TYPE (type2))
4797 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4798 {
768952be
MU
4799 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4800 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4801 & ~TYPE_QUALS (TREE_TYPE (type2))))
4802 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4803 "pointer to array loses qualifier "
4804 "in conditional expression");
4805
fcf73884 4806 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4807 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4808 "ISO C forbids conditional expr between "
bda67431 4809 "%<void *%> and function pointer");
3e4093b6
RS
4810 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4811 TREE_TYPE (type1)));
1c2a9b35 4812 }
b581b85b
NP
4813 /* Objective-C pointer comparisons are a bit more lenient. */
4814 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4815 result_type = objc_common_type (type1, type2);
34a80643 4816 else
ab87f8c8 4817 {
36c5e70a
BE
4818 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4819
b581b85b
NP
4820 pedwarn (colon_loc, 0,
4821 "pointer type mismatch in conditional expression");
36c5e70a
BE
4822 result_type = build_pointer_type
4823 (build_qualified_type (void_type_node, qual));
ab87f8c8 4824 }
3e4093b6
RS
4825 }
4826 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4827 {
6aa3c60d 4828 if (!null_pointer_constant_p (orig_op2))
744aa42f 4829 pedwarn (colon_loc, 0,
509c9d60 4830 "pointer/integer type mismatch in conditional expression");
3e4093b6 4831 else
ab87f8c8 4832 {
3e4093b6 4833 op2 = null_pointer_node;
ab87f8c8 4834 }
3e4093b6
RS
4835 result_type = type1;
4836 }
4837 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4838 {
6aa3c60d 4839 if (!null_pointer_constant_p (orig_op1))
744aa42f 4840 pedwarn (colon_loc, 0,
509c9d60 4841 "pointer/integer type mismatch in conditional expression");
3e4093b6 4842 else
ab87f8c8 4843 {
3e4093b6 4844 op1 = null_pointer_node;
ab87f8c8 4845 }
3e4093b6
RS
4846 result_type = type2;
4847 }
1c2a9b35 4848
3e4093b6
RS
4849 if (!result_type)
4850 {
4851 if (flag_cond_mismatch)
4852 result_type = void_type_node;
4853 else
400fbf9f 4854 {
c2255bc4 4855 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4856 return error_mark_node;
400fbf9f 4857 }
3e4093b6 4858 }
400fbf9f 4859
3e4093b6
RS
4860 /* Merge const and volatile flags of the incoming types. */
4861 result_type
4862 = build_type_variant (result_type,
afbd0665
AS
4863 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4864 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4865
68fca595
MP
4866 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4867 semantic_result_type);
4868 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4869 semantic_result_type);
b6a10c9f 4870
928c19bb
JM
4871 if (ifexp_bcp && ifexp == truthvalue_true_node)
4872 {
4873 op2_int_operands = true;
4874 op1 = c_fully_fold (op1, require_constant_value, NULL);
4875 }
4876 if (ifexp_bcp && ifexp == truthvalue_false_node)
4877 {
4878 op1_int_operands = true;
4879 op2 = c_fully_fold (op2, require_constant_value, NULL);
4880 }
4d84fe7c 4881 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4882 && op1_int_operands
4883 && op2_int_operands);
4884 if (int_operands)
4885 {
4886 int_const = ((ifexp == truthvalue_true_node
4887 && TREE_CODE (orig_op1) == INTEGER_CST
4888 && !TREE_OVERFLOW (orig_op1))
4889 || (ifexp == truthvalue_false_node
4890 && TREE_CODE (orig_op2) == INTEGER_CST
4891 && !TREE_OVERFLOW (orig_op2)));
4892 }
9f47c7e5
IE
4893
4894 /* Need to convert condition operand into a vector mask. */
4895 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
4896 {
4897 tree vectype = TREE_TYPE (ifexp);
4898 tree elem_type = TREE_TYPE (vectype);
4899 tree zero = build_int_cst (elem_type, 0);
4900 tree zero_vec = build_vector_from_val (vectype, zero);
4901 tree cmp_type = build_same_sized_truth_vector_type (vectype);
4902 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
4903 }
4904
928c19bb 4905 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4906 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4907 else
4908 {
01c7ccbb
JM
4909 if (int_operands)
4910 {
f34f1c87
MP
4911 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4912 nested inside of the expression. */
4913 op1 = c_fully_fold (op1, false, NULL);
4914 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4915 }
928c19bb
JM
4916 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4917 if (int_operands)
4918 ret = note_integer_operands (ret);
4919 }
2d2e923f
MLI
4920 if (semantic_result_type)
4921 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4922
c2255bc4 4923 protected_set_expr_location (ret, colon_loc);
928c19bb 4924 return ret;
3e4093b6
RS
4925}
4926\f
487a92fe 4927/* Return a compound expression that performs two expressions and
c2255bc4
AH
4928 returns the value of the second of them.
4929
4930 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4931
3e4093b6 4932tree
c2255bc4 4933build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4934{
4d84fe7c 4935 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4936 tree eptype = NULL_TREE;
928c19bb
JM
4937 tree ret;
4938
b72271b9 4939 if (flag_cilkplus
939b37da
BI
4940 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4941 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4942 {
4943 error_at (loc,
4944 "spawned function call cannot be part of a comma expression");
4945 return error_mark_node;
4946 }
4d84fe7c
JM
4947 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4948 if (expr1_int_operands)
4949 expr1 = remove_c_maybe_const_expr (expr1);
4950 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4951 if (expr2_int_operands)
4952 expr2 = remove_c_maybe_const_expr (expr2);
4953
8ce94e44
JM
4954 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4955 expr1 = TREE_OPERAND (expr1, 0);
4956 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4957 {
4958 eptype = TREE_TYPE (expr2);
4959 expr2 = TREE_OPERAND (expr2, 0);
4960 }
4961
3f75a254 4962 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4963 {
4964 /* The left-hand operand of a comma expression is like an expression
c5409249 4965 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4966 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4967 if (warn_unused_value)
47aecf47 4968 {
e14a6540 4969 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4970 && CONVERT_EXPR_P (expr1))
47aecf47 4971 ; /* (void) a, b */
e14a6540
JM
4972 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4973 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4974 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4975 ; /* (void) a, (void) b, c */
4976 else
b8698a0f 4977 warning_at (loc, OPT_Wunused_value,
c2255bc4 4978 "left-hand operand of comma expression has no effect");
47aecf47 4979 }
3e4093b6 4980 }
789eadcd
MP
4981 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4982 && warn_unused_value)
4983 {
4984 tree r = expr1;
4985 location_t cloc = loc;
4986 while (TREE_CODE (r) == COMPOUND_EXPR)
4987 {
4988 if (EXPR_HAS_LOCATION (r))
4989 cloc = EXPR_LOCATION (r);
4990 r = TREE_OPERAND (r, 1);
4991 }
4992 if (!TREE_SIDE_EFFECTS (r)
4993 && !VOID_TYPE_P (TREE_TYPE (r))
4994 && !CONVERT_EXPR_P (r))
4995 warning_at (cloc, OPT_Wunused_value,
4996 "right-hand operand of comma expression has no effect");
4997 }
400fbf9f 4998
3e4093b6
RS
4999 /* With -Wunused, we should also warn if the left-hand operand does have
5000 side-effects, but computes a value which is not used. For example, in
5001 `foo() + bar(), baz()' the result of the `+' operator is not used,
5002 so we should issue a warning. */
5003 else if (warn_unused_value)
c2255bc4 5004 warn_if_unused_value (expr1, loc);
400fbf9f 5005
e63d6886
AP
5006 if (expr2 == error_mark_node)
5007 return error_mark_node;
5008
928c19bb
JM
5009 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5010
5011 if (flag_isoc99
4d84fe7c
JM
5012 && expr1_int_operands
5013 && expr2_int_operands)
928c19bb
JM
5014 ret = note_integer_operands (ret);
5015
8ce94e44
JM
5016 if (eptype)
5017 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5018
c2255bc4 5019 protected_set_expr_location (ret, loc);
928c19bb 5020 return ret;
3e4093b6 5021}
400fbf9f 5022
67165eb3
ILT
5023/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5024 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5025 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5026 of the cast. -Wcast-qual appeared on the command line. Named
5027 address space qualifiers are not handled here, because they result
5028 in different warnings. */
67165eb3
ILT
5029
5030static void
2ee3cb35 5031handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5032{
5033 tree in_type = type;
5034 tree in_otype = otype;
5035 int added = 0;
5036 int discarded = 0;
5037 bool is_const;
5038
5039 /* Check that the qualifiers on IN_TYPE are a superset of the
5040 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5041 nodes is uninteresting and we stop as soon as we hit a
5042 non-POINTER_TYPE node on either type. */
5043 do
5044 {
5045 in_otype = TREE_TYPE (in_otype);
5046 in_type = TREE_TYPE (in_type);
5047
5048 /* GNU C allows cv-qualified function types. 'const' means the
5049 function is very pure, 'volatile' means it can't return. We
5050 need to warn when such qualifiers are added, not when they're
5051 taken away. */
5052 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5053 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5054 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5055 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5056 else
36c5e70a
BE
5057 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5058 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5059 }
5060 while (TREE_CODE (in_type) == POINTER_TYPE
5061 && TREE_CODE (in_otype) == POINTER_TYPE);
5062
5063 if (added)
2ee3cb35
MLI
5064 warning_at (loc, OPT_Wcast_qual,
5065 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5066
5067 if (discarded)
5068 /* There are qualifiers present in IN_OTYPE that are not present
5069 in IN_TYPE. */
2ee3cb35 5070 warning_at (loc, OPT_Wcast_qual,
7485aeea 5071 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5072 discarded);
67165eb3
ILT
5073
5074 if (added || discarded)
5075 return;
5076
5077 /* A cast from **T to const **T is unsafe, because it can cause a
5078 const value to be changed with no additional warning. We only
5079 issue this warning if T is the same on both sides, and we only
5080 issue the warning if there are the same number of pointers on
5081 both sides, as otherwise the cast is clearly unsafe anyhow. A
5082 cast is unsafe when a qualifier is added at one level and const
5083 is not present at all outer levels.
5084
5085 To issue this warning, we check at each level whether the cast
5086 adds new qualifiers not already seen. We don't need to special
5087 case function types, as they won't have the same
5088 TYPE_MAIN_VARIANT. */
5089
5090 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5091 return;
5092 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5093 return;
5094
5095 in_type = type;
5096 in_otype = otype;
5097 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5098 do
5099 {
5100 in_type = TREE_TYPE (in_type);
5101 in_otype = TREE_TYPE (in_otype);
5102 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5103 && !is_const)
5104 {
2ee3cb35
MLI
5105 warning_at (loc, OPT_Wcast_qual,
5106 "to be safe all intermediate pointers in cast from "
5107 "%qT to %qT must be %<const%> qualified",
5108 otype, type);
67165eb3
ILT
5109 break;
5110 }
5111 if (is_const)
5112 is_const = TYPE_READONLY (in_type);
5113 }
5114 while (TREE_CODE (in_type) == POINTER_TYPE);
5115}
5116
b8698a0f 5117/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5118 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5119
3e4093b6 5120tree
c2255bc4 5121build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5122{
8ce94e44
JM
5123 tree value;
5124
5125 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5126 expr = TREE_OPERAND (expr, 0);
5127
5128 value = expr;
400fbf9f 5129
3e4093b6
RS
5130 if (type == error_mark_node || expr == error_mark_node)
5131 return error_mark_node;
400fbf9f 5132
3e4093b6
RS
5133 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5134 only in <protocol> qualifications. But when constructing cast expressions,
5135 the protocols do matter and must be kept around. */
700686fa
ZL
5136 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5137 return build1 (NOP_EXPR, type, expr);
5138
5139 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5140
3e4093b6
RS
5141 if (TREE_CODE (type) == ARRAY_TYPE)
5142 {
c2255bc4 5143 error_at (loc, "cast specifies array type");
3e4093b6
RS
5144 return error_mark_node;
5145 }
400fbf9f 5146
3e4093b6
RS
5147 if (TREE_CODE (type) == FUNCTION_TYPE)
5148 {
c2255bc4 5149 error_at (loc, "cast specifies function type");
3e4093b6
RS
5150 return error_mark_node;
5151 }
400fbf9f 5152
808d6eaa
JM
5153 if (!VOID_TYPE_P (type))
5154 {
5155 value = require_complete_type (value);
5156 if (value == error_mark_node)
5157 return error_mark_node;
5158 }
5159
3e4093b6
RS
5160 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5161 {
3e636daf 5162 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5163 pedwarn (loc, OPT_Wpedantic,
fcf73884 5164 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5165
5166 /* Convert to remove any qualifiers from VALUE's type. */
5167 value = convert (type, value);
3e4093b6
RS
5168 }
5169 else if (TREE_CODE (type) == UNION_TYPE)
5170 {
5171 tree field;
400fbf9f 5172
910ad8de 5173 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5174 if (TREE_TYPE (field) != error_mark_node
5175 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5176 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5177 break;
5178
5179 if (field)
400fbf9f 5180 {
3e4093b6 5181 tree t;
e616f54d 5182 bool maybe_const = true;
3e4093b6 5183
c1771a20 5184 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5185 t = c_fully_fold (value, false, &maybe_const);
5186 t = build_constructor_single (type, field, t);
5187 if (!maybe_const)
5188 t = c_wrap_maybe_const (t, true);
5189 t = digest_init (loc, type, t,
bbbbb16a 5190 NULL_TREE, false, true, 0);
3e4093b6
RS
5191 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5192 return t;
400fbf9f 5193 }
c2255bc4 5194 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5195 return error_mark_node;
5196 }
5197 else
5198 {
5199 tree otype, ovalue;
400fbf9f 5200
3e4093b6 5201 if (type == void_type_node)
c2255bc4
AH
5202 {
5203 tree t = build1 (CONVERT_EXPR, type, value);
5204 SET_EXPR_LOCATION (t, loc);
5205 return t;
5206 }
400fbf9f 5207
3e4093b6 5208 otype = TREE_TYPE (value);
400fbf9f 5209
3e4093b6 5210 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5211 if (warn_cast_qual
5212 && TREE_CODE (type) == POINTER_TYPE
5213 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5214 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5215
36c5e70a
BE
5216 /* Warn about conversions between pointers to disjoint
5217 address spaces. */
5218 if (TREE_CODE (type) == POINTER_TYPE
5219 && TREE_CODE (otype) == POINTER_TYPE
5220 && !null_pointer_constant_p (value))
5221 {
5222 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5223 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5224 addr_space_t as_common;
5225
5226 if (!addr_space_superset (as_to, as_from, &as_common))
5227 {
5228 if (ADDR_SPACE_GENERIC_P (as_from))
5229 warning_at (loc, 0, "cast to %s address space pointer "
5230 "from disjoint generic address space pointer",
5231 c_addr_space_name (as_to));
5232
5233 else if (ADDR_SPACE_GENERIC_P (as_to))
5234 warning_at (loc, 0, "cast to generic address space pointer "
5235 "from disjoint %s address space pointer",
5236 c_addr_space_name (as_from));
5237
5238 else
5239 warning_at (loc, 0, "cast to %s address space pointer "
5240 "from disjoint %s address space pointer",
5241 c_addr_space_name (as_to),
5242 c_addr_space_name (as_from));
5243 }
5244 }
5245
3e4093b6 5246 /* Warn about possible alignment problems. */
3176a0c2 5247 if (STRICT_ALIGNMENT
3e4093b6
RS
5248 && TREE_CODE (type) == POINTER_TYPE
5249 && TREE_CODE (otype) == POINTER_TYPE
5250 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5251 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5252 /* Don't warn about opaque types, where the actual alignment
5253 restriction is unknown. */
3e636daf 5254 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6
RS
5255 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5256 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5257 warning_at (loc, OPT_Wcast_align,
5258 "cast increases required alignment of target type");
e9a25f70 5259
3176a0c2 5260 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5261 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5262 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5263 /* Unlike conversion of integers to pointers, where the
5264 warning is disabled for converting constants because
5265 of cases such as SIG_*, warn about converting constant
5266 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5267 sign extension, and a warning is appropriate. */
c2255bc4
AH
5268 warning_at (loc, OPT_Wpointer_to_int_cast,
5269 "cast from pointer to integer of different size");
400fbf9f 5270
3176a0c2 5271 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5272 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5273 warning_at (loc, OPT_Wbad_function_cast,
5274 "cast from function call of type %qT "
5275 "to non-matching type %qT", otype, type);
400fbf9f 5276
3176a0c2 5277 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5278 && TREE_CODE (otype) == INTEGER_TYPE
5279 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5280 /* Don't warn about converting any constant. */
5281 && !TREE_CONSTANT (value))
c2255bc4
AH
5282 warning_at (loc,
5283 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5284 "of different size");
400fbf9f 5285
79bedddc
SR
5286 if (warn_strict_aliasing <= 2)
5287 strict_aliasing_warning (otype, type, expr);
400fbf9f 5288
3897f229
JM
5289 /* If pedantic, warn for conversions between function and object
5290 pointer types, except for converting a null pointer constant
5291 to function pointer type. */
5292 if (pedantic
5293 && TREE_CODE (type) == POINTER_TYPE
5294 && TREE_CODE (otype) == POINTER_TYPE
5295 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5296 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5297 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5298 "conversion of function pointer to object pointer type");
3897f229
JM
5299
5300 if (pedantic
5301 && TREE_CODE (type) == POINTER_TYPE
5302 && TREE_CODE (otype) == POINTER_TYPE
5303 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5304 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5305 && !null_pointer_constant_p (value))
c1771a20 5306 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5307 "conversion of object pointer to function pointer type");
3897f229 5308
3e4093b6 5309 ovalue = value;
3e4093b6 5310 value = convert (type, value);
400fbf9f 5311
3e4093b6 5312 /* Ignore any integer overflow caused by the cast. */
928c19bb 5313 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5314 {
8bcd6380 5315 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5316 {
8bcd6380
RS
5317 if (!TREE_OVERFLOW (value))
5318 {
5319 /* Avoid clobbering a shared constant. */
5320 value = copy_node (value);
5321 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5322 }
6414bad6 5323 }
8bcd6380 5324 else if (TREE_OVERFLOW (value))
d8e1f97b 5325 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5326 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5327 }
5328 }
400fbf9f 5329
53cd18ec 5330 /* Don't let a cast be an lvalue. */
9482b620 5331 if (lvalue_p (value))
db3927fb 5332 value = non_lvalue_loc (loc, value);
e9a25f70 5333
928c19bb
JM
5334 /* Don't allow the results of casting to floating-point or complex
5335 types be confused with actual constants, or casts involving
5336 integer and pointer types other than direct integer-to-integer
5337 and integer-to-pointer be confused with integer constant
5338 expressions and null pointer constants. */
5339 if (TREE_CODE (value) == REAL_CST
5340 || TREE_CODE (value) == COMPLEX_CST
5341 || (TREE_CODE (value) == INTEGER_CST
5342 && !((TREE_CODE (expr) == INTEGER_CST
5343 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5344 || TREE_CODE (expr) == REAL_CST
5345 || TREE_CODE (expr) == COMPLEX_CST)))
5346 value = build1 (NOP_EXPR, type, value);
5347
21ba0cea 5348 protected_set_expr_location (value, loc);
3e4093b6 5349 return value;
400fbf9f
JW
5350}
5351
c2255bc4
AH
5352/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5353 location of the open paren of the cast, or the position of the cast
5354 expr. */
3e4093b6 5355tree
c2255bc4 5356c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5357{
f8893e47 5358 tree type;
928c19bb
JM
5359 tree type_expr = NULL_TREE;
5360 bool type_expr_const = true;
5361 tree ret;
3e4093b6 5362 int saved_wsp = warn_strict_prototypes;
c5c76735 5363
3e4093b6
RS
5364 /* This avoids warnings about unprototyped casts on
5365 integers. E.g. "#define SIG_DFL (void(*)())0". */
5366 if (TREE_CODE (expr) == INTEGER_CST)
5367 warn_strict_prototypes = 0;
928c19bb 5368 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5369 warn_strict_prototypes = saved_wsp;
c5c76735 5370
1807ffc1
MS
5371 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5372 && reject_gcc_builtin (expr))
5373 return error_mark_node;
5374
c2255bc4 5375 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5376 if (type_expr)
5377 {
9f33203d
JM
5378 bool inner_expr_const = true;
5379 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5380 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5381 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5382 && inner_expr_const);
c2255bc4 5383 SET_EXPR_LOCATION (ret, loc);
928c19bb 5384 }
24b97832 5385
21ba0cea
MP
5386 if (!EXPR_HAS_LOCATION (ret))
5387 protected_set_expr_location (ret, loc);
24b97832 5388
9e5b2115
PB
5389 /* C++ does not permits types to be defined in a cast, but it
5390 allows references to incomplete types. */
5391 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5392 warning_at (loc, OPT_Wc___compat,
5393 "defining a type in a cast is invalid in C++");
5394
928c19bb 5395 return ret;
400fbf9f 5396}
3e4093b6
RS
5397\f
5398/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5399 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5400 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5401 MODIFYCODE is the code for a binary operator that we use
5402 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5403 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5404 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5405 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5406
c2255bc4
AH
5407 LOCATION is the location of the MODIFYCODE operator.
5408 RHS_LOC is the location of the RHS. */
2f6e4e97 5409
3e4093b6 5410tree
32e8bb8e 5411build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5412 enum tree_code modifycode,
c2255bc4 5413 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5414{
3e4093b6
RS
5415 tree result;
5416 tree newrhs;
241f845a 5417 tree rhseval = NULL_TREE;
8ce94e44 5418 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5419 tree lhstype = TREE_TYPE (lhs);
5420 tree olhstype = lhstype;
928c19bb 5421 bool npc;
267bac10 5422 bool is_atomic_op;
e9a25f70 5423
3e4093b6
RS
5424 /* Types that aren't fully specified cannot be used in assignments. */
5425 lhs = require_complete_type (lhs);
e9a25f70 5426
3e4093b6
RS
5427 /* Avoid duplicate error messages from operands that had errors. */
5428 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5429 return error_mark_node;
400fbf9f 5430
4c2ecab0
JM
5431 /* Ensure an error for assigning a non-lvalue array to an array in
5432 C90. */
5433 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5434 {
5435 error_at (location, "assignment to expression with array type");
5436 return error_mark_node;
5437 }
5438
46a88c12 5439 /* For ObjC properties, defer this check. */
7bd11157 5440 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5441 return error_mark_node;
5442
267bac10
JM
5443 is_atomic_op = really_atomic_lvalue (lhs);
5444
8ce94e44
JM
5445 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5446 {
5447 rhs_semantic_type = TREE_TYPE (rhs);
5448 rhs = TREE_OPERAND (rhs, 0);
5449 }
5450
3e4093b6 5451 newrhs = rhs;
400fbf9f 5452
928c19bb
JM
5453 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5454 {
5455 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5456 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5457 rhs_origtype);
928c19bb
JM
5458 if (inner == error_mark_node)
5459 return error_mark_node;
5460 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5461 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5462 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5463 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5464 protected_set_expr_location (result, location);
5465 return result;
5466 }
5467
3e4093b6
RS
5468 /* If a binary op has been requested, combine the old LHS value with the RHS
5469 producing the value we should actually store into the LHS. */
5470
5471 if (modifycode != NOP_EXPR)
400fbf9f 5472 {
928c19bb 5473 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5474 lhs = stabilize_reference (lhs);
bbbbb16a 5475
267bac10
JM
5476 /* Construct the RHS for any non-atomic compound assignemnt. */
5477 if (!is_atomic_op)
5478 {
241f845a
JJ
5479 /* If in LHS op= RHS the RHS has side-effects, ensure they
5480 are preevaluated before the rest of the assignment expression's
5481 side-effects, because RHS could contain e.g. function calls
5482 that modify LHS. */
5483 if (TREE_SIDE_EFFECTS (rhs))
5484 {
5485 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5486 rhseval = newrhs;
5487 }
267bac10 5488 newrhs = build_binary_op (location,
241f845a 5489 modifycode, lhs, newrhs, 1);
267bac10
JM
5490
5491 /* The original type of the right hand side is no longer
5492 meaningful. */
5493 rhs_origtype = NULL_TREE;
5494 }
400fbf9f 5495 }
400fbf9f 5496
668ea4b1
IS
5497 if (c_dialect_objc ())
5498 {
46a88c12
NP
5499 /* Check if we are modifying an Objective-C property reference;
5500 if so, we need to generate setter calls. */
5501 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5502 if (result)
241f845a 5503 goto return_result;
46a88c12
NP
5504
5505 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5506 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5507 return error_mark_node;
5508 }
5509
9bf24266 5510 /* Give an error for storing in something that is 'const'. */
bbbd6700 5511
f37acdf9 5512 if (TYPE_READONLY (lhstype)
3e636daf 5513 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5514 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5515 {
c02065fc 5516 readonly_error (location, lhs, lv_assign);
953ff289
DN
5517 return error_mark_node;
5518 }
f37acdf9
JM
5519 else if (TREE_READONLY (lhs))
5520 readonly_warning (lhs, lv_assign);
bbbd6700 5521
3e4093b6
RS
5522 /* If storing into a structure or union member,
5523 it has probably been given type `int'.
5524 Compute the type that would go with
5525 the actual amount of storage the member occupies. */
bbbd6700 5526
3e4093b6
RS
5527 if (TREE_CODE (lhs) == COMPONENT_REF
5528 && (TREE_CODE (lhstype) == INTEGER_TYPE
5529 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5530 || TREE_CODE (lhstype) == REAL_TYPE
5531 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5532 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5533
3e4093b6
RS
5534 /* If storing in a field that is in actuality a short or narrower than one,
5535 we must store in the field in its actual type. */
5536
5537 if (lhstype != TREE_TYPE (lhs))
5538 {
5539 lhs = copy_node (lhs);
5540 TREE_TYPE (lhs) = lhstype;
400fbf9f 5541 }
400fbf9f 5542
32e8bb8e
ILT
5543 /* Issue -Wc++-compat warnings about an assignment to an enum type
5544 when LHS does not have its original type. This happens for,
5545 e.g., an enum bitfield in a struct. */
5546 if (warn_cxx_compat
5547 && lhs_origtype != NULL_TREE
5548 && lhs_origtype != lhstype
5549 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5550 {
5551 tree checktype = (rhs_origtype != NULL_TREE
5552 ? rhs_origtype
5553 : TREE_TYPE (rhs));
5554 if (checktype != error_mark_node
267bac10
JM
5555 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5556 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5557 warning_at (location, OPT_Wc___compat,
5558 "enum conversion in assignment is invalid in C++");
5559 }
5560
267bac10
JM
5561 /* If the lhs is atomic, remove that qualifier. */
5562 if (is_atomic_op)
5563 {
5564 lhstype = build_qualified_type (lhstype,
5565 (TYPE_QUALS (lhstype)
5566 & ~TYPE_QUAL_ATOMIC));
5567 olhstype = build_qualified_type (olhstype,
5568 (TYPE_QUALS (lhstype)
5569 & ~TYPE_QUAL_ATOMIC));
5570 }
5571
8ce94e44
JM
5572 /* Convert new value to destination type. Fold it first, then
5573 restore any excess precision information, for the sake of
5574 conversion warnings. */
400fbf9f 5575
267bac10
JM
5576 if (!(is_atomic_op && modifycode != NOP_EXPR))
5577 {
5578 npc = null_pointer_constant_p (newrhs);
5579 newrhs = c_fully_fold (newrhs, false, NULL);
5580 if (rhs_semantic_type)
5581 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5582 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5583 rhs_origtype, ic_assign, npc,
5584 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5585 if (TREE_CODE (newrhs) == ERROR_MARK)
5586 return error_mark_node;
5587 }
400fbf9f 5588
6e955430
ZL
5589 /* Emit ObjC write barrier, if necessary. */
5590 if (c_dialect_objc () && flag_objc_gc)
5591 {
5592 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5593 if (result)
c9f9eb5d
AH
5594 {
5595 protected_set_expr_location (result, location);
241f845a 5596 goto return_result;
c9f9eb5d 5597 }
6e955430
ZL
5598 }
5599
ea4b7848 5600 /* Scan operands. */
400fbf9f 5601
267bac10
JM
5602 if (is_atomic_op)
5603 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5604 else
5605 {
5606 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5607 TREE_SIDE_EFFECTS (result) = 1;
5608 protected_set_expr_location (result, location);
5609 }
400fbf9f 5610
3e4093b6
RS
5611 /* If we got the LHS in a different type for storing in,
5612 convert the result back to the nominal type of LHS
5613 so that the value we return always has the same type
5614 as the LHS argument. */
e855c5ce 5615
3e4093b6 5616 if (olhstype == TREE_TYPE (result))
241f845a 5617 goto return_result;
c9f9eb5d 5618
68fca595
MP
5619 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5620 rhs_origtype, ic_assign, false, NULL_TREE,
5621 NULL_TREE, 0);
c9f9eb5d 5622 protected_set_expr_location (result, location);
241f845a
JJ
5623
5624return_result:
5625 if (rhseval)
5626 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5627 return result;
3e4093b6
RS
5628}
5629\f
478a1c5b
ILT
5630/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5631 This is used to implement -fplan9-extensions. */
5632
5633static bool
5634find_anonymous_field_with_type (tree struct_type, tree type)
5635{
5636 tree field;
5637 bool found;
5638
3e636daf 5639 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
5640 found = false;
5641 for (field = TYPE_FIELDS (struct_type);
5642 field != NULL_TREE;
5643 field = TREE_CHAIN (field))
5644 {
267bac10
JM
5645 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5646 ? c_build_qualified_type (TREE_TYPE (field),
5647 TYPE_QUAL_ATOMIC)
5648 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5649 if (DECL_NAME (field) == NULL
267bac10 5650 && comptypes (type, fieldtype))
478a1c5b
ILT
5651 {
5652 if (found)
5653 return false;
5654 found = true;
5655 }
5656 else if (DECL_NAME (field) == NULL
3e636daf 5657 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
5658 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5659 {
5660 if (found)
5661 return false;
5662 found = true;
5663 }
5664 }
5665 return found;
5666}
5667
5668/* RHS is an expression whose type is pointer to struct. If there is
5669 an anonymous field in RHS with type TYPE, then return a pointer to
5670 that field in RHS. This is used with -fplan9-extensions. This
5671 returns NULL if no conversion could be found. */
5672
5673static tree
5674convert_to_anonymous_field (location_t location, tree type, tree rhs)
5675{
5676 tree rhs_struct_type, lhs_main_type;
5677 tree field, found_field;
5678 bool found_sub_field;
5679 tree ret;
5680
5681 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5682 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 5683 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
5684
5685 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5686 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5687 ? c_build_qualified_type (TREE_TYPE (type),
5688 TYPE_QUAL_ATOMIC)
5689 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5690
5691 found_field = NULL_TREE;
5692 found_sub_field = false;
5693 for (field = TYPE_FIELDS (rhs_struct_type);
5694 field != NULL_TREE;
5695 field = TREE_CHAIN (field))
5696 {
5697 if (DECL_NAME (field) != NULL_TREE
3e636daf 5698 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 5699 continue;
267bac10
JM
5700 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5701 ? c_build_qualified_type (TREE_TYPE (field),
5702 TYPE_QUAL_ATOMIC)
5703 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5704 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5705 {
5706 if (found_field != NULL_TREE)
5707 return NULL_TREE;
5708 found_field = field;
5709 }
5710 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5711 lhs_main_type))
5712 {
5713 if (found_field != NULL_TREE)
5714 return NULL_TREE;
5715 found_field = field;
5716 found_sub_field = true;
5717 }
5718 }
5719
5720 if (found_field == NULL_TREE)
5721 return NULL_TREE;
5722
5723 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5724 build_fold_indirect_ref (rhs), found_field,
5725 NULL_TREE);
5726 ret = build_fold_addr_expr_loc (location, ret);
5727
5728 if (found_sub_field)
5729 {
5730 ret = convert_to_anonymous_field (location, type, ret);
5731 gcc_assert (ret != NULL_TREE);
5732 }
5733
5734 return ret;
5735}
5736
63bc4e87
MP
5737/* Issue an error message for a bad initializer component.
5738 GMSGID identifies the message.
5739 The component name is taken from the spelling stack. */
5740
5741static void
ea58ef42 5742error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5743{
5744 char *ofwhat;
5745
5746 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5747 error_at (loc, gmsgid);
63bc4e87
MP
5748 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5749 if (*ofwhat)
d7ff7ae5 5750 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5751}
5752
5753/* Issue a pedantic warning for a bad initializer component. OPT is
5754 the option OPT_* (from options.h) controlling this warning or 0 if
5755 it is unconditionally given. GMSGID identifies the message. The
5756 component name is taken from the spelling stack. */
5757
5758static void
5759pedwarn_init (location_t location, int opt, const char *gmsgid)
5760{
5761 char *ofwhat;
d7ff7ae5 5762 bool warned;
63bc4e87
MP
5763
5764 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5765 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5766 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5767 if (*ofwhat && warned)
5768 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5769}
5770
5771/* Issue a warning for a bad initializer component.
5772
5773 OPT is the OPT_W* value corresponding to the warning option that
5774 controls this warning. GMSGID identifies the message. The
5775 component name is taken from the spelling stack. */
5776
5777static void
5778warning_init (location_t loc, int opt, const char *gmsgid)
5779{
5780 char *ofwhat;
d7ff7ae5 5781 bool warned;
63bc4e87
MP
5782
5783 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5784 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5785 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5786 if (*ofwhat && warned)
5787 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5788}
5789\f
5790/* If TYPE is an array type and EXPR is a parenthesized string
5791 constant, warn if pedantic that EXPR is being used to initialize an
5792 object of type TYPE. */
5793
5794void
d033409e 5795maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5796{
5797 if (pedantic
5798 && TREE_CODE (type) == ARRAY_TYPE
5799 && TREE_CODE (expr.value) == STRING_CST
5800 && expr.original_code != STRING_CST)
d033409e 5801 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5802 "array initialized from parenthesized string constant");
5803}
5804
bbbbb16a
ILT
5805/* Convert value RHS to type TYPE as preparation for an assignment to
5806 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5807 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5808 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5809 constant before any folding.
3e4093b6
RS
5810 The real work of conversion is done by `convert'.
5811 The purpose of this function is to generate error messages
5812 for assignments that are not allowed in C.
2ac2f164
JM
5813 ERRTYPE says whether it is argument passing, assignment,
5814 initialization or return.
2f6e4e97 5815
aabef2de
MP
5816 In the following example, '~' denotes where EXPR_LOC and '^' where
5817 LOCATION point to:
5818
5819 f (var); [ic_argpass]
5820 ^ ~~~
5821 x = var; [ic_assign]
5822 ^ ~~~;
5823 int x = var; [ic_init]
5824 ^^^
5825 return x; [ic_return]
5826 ^
5827
2ac2f164 5828 FUNCTION is a tree for the function being called.
3e4093b6 5829 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5830
3e4093b6 5831static tree
81e5eca8 5832convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5833 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5834 bool null_pointer_constant, tree fundecl,
5835 tree function, int parmnum)
3e4093b6
RS
5836{
5837 enum tree_code codel = TREE_CODE (type);
8ce94e44 5838 tree orig_rhs = rhs;
3e4093b6
RS
5839 tree rhstype;
5840 enum tree_code coder;
2ac2f164 5841 tree rname = NULL_TREE;
58393038 5842 bool objc_ok = false;
2ac2f164 5843
1c7485af
MP
5844 /* Use the expansion point location to handle cases such as user's
5845 function returning a wrong-type macro defined in a system header. */
5846 location = expansion_point_location_if_in_system_header (location);
5847
6b4ef5c1 5848 if (errtype == ic_argpass)
2ac2f164
JM
5849 {
5850 tree selector;
5851 /* Change pointer to function to the function itself for
5852 diagnostics. */
5853 if (TREE_CODE (function) == ADDR_EXPR
5854 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5855 function = TREE_OPERAND (function, 0);
5856
5857 /* Handle an ObjC selector specially for diagnostics. */
5858 selector = objc_message_selector ();
5859 rname = function;
5860 if (selector && parmnum > 2)
5861 {
5862 rname = selector;
5863 parmnum -= 2;
5864 }
5865 }
5866
5867 /* This macro is used to emit diagnostics to ensure that all format
5868 strings are complete sentences, visible to gettext and checked at
5869 compile time. */
768952be 5870#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5871 do { \
5872 switch (errtype) \
5873 { \
5874 case ic_argpass: \
5c1bc275 5875 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5876 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5877 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5878 "expected %qT but argument is of type %qT", \
5879 type, rhstype); \
5880 break; \
1e053dfe
MLI
5881 case ic_assign: \
5882 pedwarn (LOCATION, OPT, AS); \
5883 break; \
5884 case ic_init: \
6a8f4e12 5885 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5886 break; \
5887 case ic_return: \
d033409e 5888 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5889 break; \
5890 default: \
5891 gcc_unreachable (); \
5892 } \
2ac2f164 5893 } while (0)
cb3ca04e 5894
49706e39
MLI
5895 /* This macro is used to emit diagnostics to ensure that all format
5896 strings are complete sentences, visible to gettext and checked at
768952be 5897 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5898 extra parameter to enumerate qualifiers. */
768952be 5899#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5900 do { \
5901 switch (errtype) \
5902 { \
5903 case ic_argpass: \
5c1bc275 5904 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5905 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5906 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5907 "expected %qT but argument is of type %qT", \
5908 type, rhstype); \
5909 break; \
5910 case ic_assign: \
5c1bc275 5911 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5912 break; \
5913 case ic_init: \
5c1bc275 5914 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5915 break; \
5916 case ic_return: \
5c1bc275 5917 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5918 break; \
5919 default: \
5920 gcc_unreachable (); \
5921 } \
5922 } while (0)
5923
768952be
MU
5924 /* This macro is used to emit diagnostics to ensure that all format
5925 strings are complete sentences, visible to gettext and checked at
5926 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5927 warning_at instead of pedwarn. */
5928#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5929 do { \
5930 switch (errtype) \
5931 { \
5932 case ic_argpass: \
5933 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5934 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5935 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5936 "expected %qT but argument is of type %qT", \
5937 type, rhstype); \
5938 break; \
5939 case ic_assign: \
5940 warning_at (LOCATION, OPT, AS, QUALS); \
5941 break; \
5942 case ic_init: \
5943 warning_at (LOCATION, OPT, IN, QUALS); \
5944 break; \
5945 case ic_return: \
5946 warning_at (LOCATION, OPT, RE, QUALS); \
5947 break; \
5948 default: \
5949 gcc_unreachable (); \
5950 } \
5951 } while (0)
5952
8ce94e44
JM
5953 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5954 rhs = TREE_OPERAND (rhs, 0);
5955
3e4093b6
RS
5956 rhstype = TREE_TYPE (rhs);
5957 coder = TREE_CODE (rhstype);
5958
5959 if (coder == ERROR_MARK)
5960 return error_mark_node;
5961
58393038
ZL
5962 if (c_dialect_objc ())
5963 {
5964 int parmno;
5965
5966 switch (errtype)
5967 {
5968 case ic_return:
5969 parmno = 0;
5970 break;
5971
5972 case ic_assign:
5973 parmno = -1;
5974 break;
5975
5976 case ic_init:
5977 parmno = -2;
5978 break;
5979
5980 default:
5981 parmno = parmnum;
5982 break;
5983 }
5984
5985 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5986 }
5987
bbbbb16a
ILT
5988 if (warn_cxx_compat)
5989 {
5990 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5991 if (checktype != error_mark_node
5992 && TREE_CODE (type) == ENUMERAL_TYPE
5993 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5994 {
768952be
MU
5995 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5996 G_("enum conversion when passing argument "
5997 "%d of %qE is invalid in C++"),
5998 G_("enum conversion in assignment is "
5999 "invalid in C++"),
6000 G_("enum conversion in initialization is "
6001 "invalid in C++"),
6002 G_("enum conversion in return is "
6003 "invalid in C++"));
bbbbb16a
ILT
6004 }
6005 }
6006
3e4093b6 6007 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6008 return rhs;
3e4093b6
RS
6009
6010 if (coder == VOID_TYPE)
400fbf9f 6011 {
6dcc04b0
JM
6012 /* Except for passing an argument to an unprototyped function,
6013 this is a constraint violation. When passing an argument to
6014 an unprototyped function, it is compile-time undefined;
6015 making it a constraint in that case was rejected in
6016 DR#252. */
c2255bc4 6017 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6018 return error_mark_node;
400fbf9f 6019 }
808d6eaa
JM
6020 rhs = require_complete_type (rhs);
6021 if (rhs == error_mark_node)
6022 return error_mark_node;
1807ffc1
MS
6023
6024 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6025 return error_mark_node;
6026
cd192ccc
MS
6027 /* A non-reference type can convert to a reference. This handles
6028 va_start, va_copy and possibly port built-ins. */
6029 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6030 {
3e4093b6 6031 if (!lvalue_p (rhs))
400fbf9f 6032 {
c2255bc4 6033 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6034 return error_mark_node;
400fbf9f 6035 }
3e4093b6
RS
6036 if (!c_mark_addressable (rhs))
6037 return error_mark_node;
6038 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6039 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6040
81e5eca8 6041 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6042 build_pointer_type (TREE_TYPE (type)),
6043 rhs, origtype, errtype,
6044 null_pointer_constant, fundecl, function,
6045 parmnum);
cd192ccc
MS
6046 if (rhs == error_mark_node)
6047 return error_mark_node;
3e4093b6
RS
6048
6049 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6050 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6051 return rhs;
400fbf9f 6052 }
3e4093b6 6053 /* Some types can interconvert without explicit casts. */
3274deff 6054 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6055 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6056 return convert (type, rhs);
6057 /* Arithmetic types all interconvert, and enum is treated like int. */
6058 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6059 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6060 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6061 || codel == BOOLEAN_TYPE)
6062 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6063 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6064 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6065 || coder == BOOLEAN_TYPE))
928c19bb
JM
6066 {
6067 tree ret;
6068 bool save = in_late_binary_op;
e5341100
JJ
6069 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6070 || (coder == REAL_TYPE
6071 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6072 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 6073 in_late_binary_op = true;
81e5eca8
MP
6074 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6075 ? expr_loc : location, type, orig_rhs);
e5341100 6076 in_late_binary_op = save;
928c19bb
JM
6077 return ret;
6078 }
400fbf9f 6079
79077aea
JJ
6080 /* Aggregates in different TUs might need conversion. */
6081 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6082 && codel == coder
6083 && comptypes (type, rhstype))
81e5eca8
MP
6084 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6085 ? expr_loc : location, type, rhs);
79077aea 6086
ebf0bf7f 6087 /* Conversion to a transparent union or record from its member types.
3e4093b6 6088 This applies only to function arguments. */
ebf0bf7f
JJ
6089 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6090 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6091 && errtype == ic_argpass)
400fbf9f 6092 {
0257e383 6093 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6094
910ad8de 6095 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6096 {
0257e383 6097 tree memb_type = TREE_TYPE (memb);
400fbf9f 6098
3e4093b6 6099 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6100 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6101 break;
e58cd767 6102
3e4093b6
RS
6103 if (TREE_CODE (memb_type) != POINTER_TYPE)
6104 continue;
2f6e4e97 6105
3e4093b6
RS
6106 if (coder == POINTER_TYPE)
6107 {
6108 tree ttl = TREE_TYPE (memb_type);
6109 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6110
3e4093b6
RS
6111 /* Any non-function converts to a [const][volatile] void *
6112 and vice versa; otherwise, targets must be the same.
6113 Meanwhile, the lhs target must have all the qualifiers of
6114 the rhs. */
267bac10
JM
6115 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6116 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6117 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6118 {
267bac10
JM
6119 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6120 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6121 /* If this type won't generate any warnings, use it. */
267bac10 6122 if (lquals == rquals
3e4093b6
RS
6123 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6124 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6125 ? ((lquals | rquals) == rquals)
6126 : ((lquals | rquals) == lquals)))
3e4093b6 6127 break;
400fbf9f 6128
3e4093b6 6129 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6130 if (!marginal_memb)
6131 marginal_memb = memb;
3e4093b6
RS
6132 }
6133 }
82bde854 6134
3e4093b6 6135 /* Can convert integer zero to any pointer type. */
928c19bb 6136 if (null_pointer_constant)
3e4093b6
RS
6137 {
6138 rhs = null_pointer_node;
6139 break;
6140 }
6141 }
400fbf9f 6142
0257e383 6143 if (memb || marginal_memb)
3e4093b6 6144 {
0257e383 6145 if (!memb)
3e4093b6
RS
6146 {
6147 /* We have only a marginally acceptable member type;
6148 it needs a warning. */
0257e383 6149 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6150 tree ttr = TREE_TYPE (rhstype);
714a0864 6151
3e4093b6
RS
6152 /* Const and volatile mean something different for function
6153 types, so the usual warnings are not appropriate. */
6154 if (TREE_CODE (ttr) == FUNCTION_TYPE
6155 && TREE_CODE (ttl) == FUNCTION_TYPE)
6156 {
6157 /* Because const and volatile on functions are
6158 restrictions that say the function will not do
6159 certain things, it is okay to use a const or volatile
6160 function where an ordinary one is wanted, but not
6161 vice-versa. */
36c5e70a
BE
6162 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6163 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6164 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6165 OPT_Wdiscarded_qualifiers,
6166 G_("passing argument %d of %qE "
6167 "makes %q#v qualified function "
6168 "pointer from unqualified"),
6169 G_("assignment makes %q#v qualified "
6170 "function pointer from "
6171 "unqualified"),
6172 G_("initialization makes %q#v qualified "
6173 "function pointer from "
6174 "unqualified"),
6175 G_("return makes %q#v qualified function "
6176 "pointer from unqualified"),
6177 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6178 }
36c5e70a
BE
6179 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6180 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6181 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6182 OPT_Wdiscarded_qualifiers,
6183 G_("passing argument %d of %qE discards "
6184 "%qv qualifier from pointer target type"),
6185 G_("assignment discards %qv qualifier "
6186 "from pointer target type"),
6187 G_("initialization discards %qv qualifier "
6188 "from pointer target type"),
6189 G_("return discards %qv qualifier from "
6190 "pointer target type"),
6191 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6192
6193 memb = marginal_memb;
3e4093b6 6194 }
400fbf9f 6195
fcf73884 6196 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6197 pedwarn (location, OPT_Wpedantic,
fcf73884 6198 "ISO C prohibits argument conversion to union type");
0e7c47fa 6199
db3927fb 6200 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6201 return build_constructor_single (type, memb, rhs);
3e4093b6 6202 }
0e7c47fa
RK
6203 }
6204
3e4093b6
RS
6205 /* Conversions among pointers */
6206 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6207 && (coder == codel))
400fbf9f 6208 {
3e4093b6
RS
6209 tree ttl = TREE_TYPE (type);
6210 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6211 tree mvl = ttl;
6212 tree mvr = ttr;
3e4093b6 6213 bool is_opaque_pointer;
264fa2db 6214 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6215 addr_space_t asl;
6216 addr_space_t asr;
400fbf9f 6217
46df2823 6218 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6219 mvl = (TYPE_ATOMIC (mvl)
6220 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6221 TYPE_QUAL_ATOMIC)
6222 : TYPE_MAIN_VARIANT (mvl));
46df2823 6223 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6224 mvr = (TYPE_ATOMIC (mvr)
6225 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6226 TYPE_QUAL_ATOMIC)
6227 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6228 /* Opaque pointers are treated like void pointers. */
f83c7f63 6229 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6230
478a1c5b
ILT
6231 /* The Plan 9 compiler permits a pointer to a struct to be
6232 automatically converted into a pointer to an anonymous field
6233 within the struct. */
6234 if (flag_plan9_extensions
3e636daf
MP
6235 && RECORD_OR_UNION_TYPE_P (mvl)
6236 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6237 && mvl != mvr)
6238 {
6239 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6240 if (new_rhs != NULL_TREE)
6241 {
6242 rhs = new_rhs;
6243 rhstype = TREE_TYPE (rhs);
6244 coder = TREE_CODE (rhstype);
6245 ttr = TREE_TYPE (rhstype);
6246 mvr = TYPE_MAIN_VARIANT (ttr);
6247 }
6248 }
6249
b7e20b53 6250 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6251 for the purpose of reducing the number of false positives, we
6252 tolerate the special case of
b7e20b53 6253
c22cacf3 6254 int *p = NULL;
b7e20b53 6255
c22cacf3 6256 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6257 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6258 warning_at (errtype == ic_argpass ? expr_loc : location,
6259 OPT_Wc___compat,
6260 "request for implicit conversion "
c2255bc4 6261 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6262
36c5e70a
BE
6263 /* See if the pointers point to incompatible address spaces. */
6264 asl = TYPE_ADDR_SPACE (ttl);
6265 asr = TYPE_ADDR_SPACE (ttr);
6266 if (!null_pointer_constant_p (rhs)
6267 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6268 {
6269 switch (errtype)
6270 {
6271 case ic_argpass:
8ffcdea8 6272 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6273 "non-enclosed address space", parmnum, rname);
6274 break;
6275 case ic_assign:
6276 error_at (location, "assignment from pointer to "
6277 "non-enclosed address space");
6278 break;
6279 case ic_init:
6280 error_at (location, "initialization from pointer to "
6281 "non-enclosed address space");
6282 break;
6283 case ic_return:
6284 error_at (location, "return from pointer to "
6285 "non-enclosed address space");
6286 break;
6287 default:
6288 gcc_unreachable ();
6289 }
6290 return error_mark_node;
6291 }
6292
7876a414
KG
6293 /* Check if the right-hand side has a format attribute but the
6294 left-hand side doesn't. */
90137d8f 6295 if (warn_suggest_attribute_format
104f8784 6296 && check_missing_format_attribute (type, rhstype))
c22cacf3 6297 {
104f8784
KG
6298 switch (errtype)
6299 {
6300 case ic_argpass:
8ffcdea8 6301 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6302 "argument %d of %qE might be "
6303 "a candidate for a format attribute",
6304 parmnum, rname);
104f8784
KG
6305 break;
6306 case ic_assign:
90137d8f 6307 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6308 "assignment left-hand side might be "
6309 "a candidate for a format attribute");
104f8784
KG
6310 break;
6311 case ic_init:
90137d8f 6312 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6313 "initialization left-hand side might be "
6314 "a candidate for a format attribute");
104f8784
KG
6315 break;
6316 case ic_return:
90137d8f 6317 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6318 "return type might be "
6319 "a candidate for a format attribute");
104f8784
KG
6320 break;
6321 default:
6322 gcc_unreachable ();
6323 }
7876a414 6324 }
c22cacf3 6325
3e4093b6
RS
6326 /* Any non-function converts to a [const][volatile] void *
6327 and vice versa; otherwise, targets must be the same.
6328 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6329 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6330 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6331 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6332 || is_opaque_pointer
f8a93a2e
JJ
6333 || ((c_common_unsigned_type (mvl)
6334 == c_common_unsigned_type (mvr))
267bac10
JM
6335 && (c_common_signed_type (mvl)
6336 == c_common_signed_type (mvr))
6337 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6338 {
768952be
MU
6339 /* Warn about loss of qualifers from pointers to arrays with
6340 qualifiers on the element type. */
6341 if (TREE_CODE (ttr) == ARRAY_TYPE)
6342 {
6343 ttr = strip_array_types (ttr);
6344 ttl = strip_array_types (ttl);
6345
6346 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6347 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6348 WARNING_FOR_QUALIFIERS (location, expr_loc,
6349 OPT_Wdiscarded_array_qualifiers,
6350 G_("passing argument %d of %qE discards "
6351 "%qv qualifier from pointer target type"),
6352 G_("assignment discards %qv qualifier "
6353 "from pointer target type"),
6354 G_("initialization discards %qv qualifier "
6355 "from pointer target type"),
6356 G_("return discards %qv qualifier from "
6357 "pointer target type"),
6358 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6359 }
6360 else if (pedantic
3e4093b6
RS
6361 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6362 ||
6363 (VOID_TYPE_P (ttr)
928c19bb 6364 && !null_pointer_constant
3e4093b6 6365 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6366 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6367 G_("ISO C forbids passing argument %d of "
6368 "%qE between function pointer "
6369 "and %<void *%>"),
6370 G_("ISO C forbids assignment between "
6371 "function pointer and %<void *%>"),
6372 G_("ISO C forbids initialization between "
6373 "function pointer and %<void *%>"),
6374 G_("ISO C forbids return between function "
6375 "pointer and %<void *%>"));
3e4093b6
RS
6376 /* Const and volatile mean something different for function types,
6377 so the usual warnings are not appropriate. */
6378 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6379 && TREE_CODE (ttl) != FUNCTION_TYPE)
6380 {
768952be
MU
6381 /* Don't warn about loss of qualifier for conversions from
6382 qualified void* to pointers to arrays with corresponding
6383 qualifier on the element type. */
6384 if (!pedantic)
6385 ttl = strip_array_types (ttl);
6386
267bac10
JM
6387 /* Assignments between atomic and non-atomic objects are OK. */
6388 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6389 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6390 {
768952be
MU
6391 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6392 OPT_Wdiscarded_qualifiers,
6393 G_("passing argument %d of %qE discards "
6394 "%qv qualifier from pointer target type"),
6395 G_("assignment discards %qv qualifier "
6396 "from pointer target type"),
6397 G_("initialization discards %qv qualifier "
6398 "from pointer target type"),
6399 G_("return discards %qv qualifier from "
6400 "pointer target type"),
6401 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6402 }
3e4093b6
RS
6403 /* If this is not a case of ignoring a mismatch in signedness,
6404 no warning. */
6405 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6406 || target_cmp)
3e4093b6
RS
6407 ;
6408 /* If there is a mismatch, do warn. */
f2fd3821 6409 else if (warn_pointer_sign)
768952be
MU
6410 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6411 G_("pointer targets in passing argument "
6412 "%d of %qE differ in signedness"),
6413 G_("pointer targets in assignment "
6414 "differ in signedness"),
6415 G_("pointer targets in initialization "
6416 "differ in signedness"),
6417 G_("pointer targets in return differ "
6418 "in signedness"));
3e4093b6
RS
6419 }
6420 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6421 && TREE_CODE (ttr) == FUNCTION_TYPE)
6422 {
6423 /* Because const and volatile on functions are restrictions
6424 that say the function will not do certain things,
6425 it is okay to use a const or volatile function
6426 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6427 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6428 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6429 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6430 OPT_Wdiscarded_qualifiers,
6431 G_("passing argument %d of %qE makes "
6432 "%q#v qualified function pointer "
6433 "from unqualified"),
6434 G_("assignment makes %q#v qualified function "
6435 "pointer from unqualified"),
6436 G_("initialization makes %q#v qualified "
6437 "function pointer from unqualified"),
6438 G_("return makes %q#v qualified function "
6439 "pointer from unqualified"),
6440 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6441 }
6442 }
6443 else
58393038
ZL
6444 /* Avoid warning about the volatile ObjC EH puts on decls. */
6445 if (!objc_ok)
768952be
MU
6446 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6447 OPT_Wincompatible_pointer_types,
6448 G_("passing argument %d of %qE from "
6449 "incompatible pointer type"),
6450 G_("assignment from incompatible pointer type"),
6451 G_("initialization from incompatible "
6452 "pointer type"),
6453 G_("return from incompatible pointer type"));
58393038 6454
3e4093b6
RS
6455 return convert (type, rhs);
6456 }
b494fd98
EB
6457 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6458 {
6dcc04b0
JM
6459 /* ??? This should not be an error when inlining calls to
6460 unprototyped functions. */
c2255bc4 6461 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6462 return error_mark_node;
6463 }
3e4093b6 6464 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6465 {
3e4093b6
RS
6466 /* An explicit constant 0 can convert to a pointer,
6467 or one that results from arithmetic, even including
6468 a cast to integer type. */
928c19bb 6469 if (!null_pointer_constant)
768952be
MU
6470 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6471 OPT_Wint_conversion,
6472 G_("passing argument %d of %qE makes "
6473 "pointer from integer without a cast"),
6474 G_("assignment makes pointer from integer "
6475 "without a cast"),
6476 G_("initialization makes pointer from "
6477 "integer without a cast"),
6478 G_("return makes pointer from integer "
6479 "without a cast"));
b3006337
EB
6480
6481 return convert (type, rhs);
400fbf9f 6482 }
3e4093b6 6483 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6484 {
768952be
MU
6485 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6486 OPT_Wint_conversion,
6487 G_("passing argument %d of %qE makes integer "
6488 "from pointer without a cast"),
6489 G_("assignment makes integer from pointer "
6490 "without a cast"),
6491 G_("initialization makes integer from pointer "
6492 "without a cast"),
6493 G_("return makes integer from pointer "
6494 "without a cast"));
3e4093b6
RS
6495 return convert (type, rhs);
6496 }
6497 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6498 {
6499 tree ret;
6500 bool save = in_late_binary_op;
6501 in_late_binary_op = true;
6502 ret = convert (type, rhs);
6503 in_late_binary_op = save;
6504 return ret;
6505 }
400fbf9f 6506
2ac2f164 6507 switch (errtype)
3e4093b6 6508 {
2ac2f164 6509 case ic_argpass:
8ffcdea8
MP
6510 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6511 rname);
a7da8b42 6512 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6513 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6514 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6515 break;
6516 case ic_assign:
c2255bc4
AH
6517 error_at (location, "incompatible types when assigning to type %qT from "
6518 "type %qT", type, rhstype);
2ac2f164
JM
6519 break;
6520 case ic_init:
c2255bc4 6521 error_at (location,
8ffcdea8 6522 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6523 type, rhstype);
2ac2f164
JM
6524 break;
6525 case ic_return:
c2255bc4 6526 error_at (location,
8ffcdea8 6527 "incompatible types when returning type %qT but %qT was "
c2255bc4 6528 "expected", rhstype, type);
2ac2f164
JM
6529 break;
6530 default:
6531 gcc_unreachable ();
400fbf9f 6532 }
53b01f59 6533
3e4093b6
RS
6534 return error_mark_node;
6535}
3e4093b6
RS
6536\f
6537/* If VALUE is a compound expr all of whose expressions are constant, then
6538 return its value. Otherwise, return error_mark_node.
15b732b2 6539
3e4093b6
RS
6540 This is for handling COMPOUND_EXPRs as initializer elements
6541 which is allowed with a warning when -pedantic is specified. */
15b732b2 6542
3e4093b6
RS
6543static tree
6544valid_compound_expr_initializer (tree value, tree endtype)
6545{
6546 if (TREE_CODE (value) == COMPOUND_EXPR)
6547 {
6548 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6549 == error_mark_node)
6550 return error_mark_node;
6551 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6552 endtype);
6553 }
116df786 6554 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6555 return error_mark_node;
6556 else
6557 return value;
15b732b2 6558}
400fbf9f 6559\f
3e4093b6
RS
6560/* Perform appropriate conversions on the initial value of a variable,
6561 store it in the declaration DECL,
6562 and print any error messages that are appropriate.
bbbbb16a 6563 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6564 If the init is invalid, store an ERROR_MARK.
6565
6566 INIT_LOC is the location of the initial value. */
400fbf9f 6567
3e4093b6 6568void
c2255bc4 6569store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6570{
3e4093b6 6571 tree value, type;
928c19bb 6572 bool npc = false;
400fbf9f 6573
3e4093b6 6574 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6575
3e4093b6
RS
6576 type = TREE_TYPE (decl);
6577 if (TREE_CODE (type) == ERROR_MARK)
6578 return;
400fbf9f 6579
3e4093b6 6580 /* Digest the specified initializer into an expression. */
400fbf9f 6581
928c19bb
JM
6582 if (init)
6583 npc = null_pointer_constant_p (init);
c2255bc4
AH
6584 value = digest_init (init_loc, type, init, origtype, npc,
6585 true, TREE_STATIC (decl));
400fbf9f 6586
3e4093b6 6587 /* Store the expression if valid; else report error. */
400fbf9f 6588
8400e75e 6589 if (!in_system_header_at (input_location)
3f75a254 6590 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6591 warning (OPT_Wtraditional, "traditional C rejects automatic "
6592 "aggregate initialization");
2f6e4e97 6593
dea63e49
JJ
6594 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6595 DECL_INITIAL (decl) = value;
400fbf9f 6596
3e4093b6
RS
6597 /* ANSI wants warnings about out-of-range constant initializers. */
6598 STRIP_TYPE_NOPS (value);
b8698a0f 6599 if (TREE_STATIC (decl))
c2658540 6600 constant_expression_warning (value);
400fbf9f 6601
3e4093b6
RS
6602 /* Check if we need to set array size from compound literal size. */
6603 if (TREE_CODE (type) == ARRAY_TYPE
6604 && TYPE_DOMAIN (type) == 0
6605 && value != error_mark_node)
400fbf9f 6606 {
3e4093b6
RS
6607 tree inside_init = init;
6608
ed248cf7 6609 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6610 inside_init = fold (inside_init);
6611
6612 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6613 {
8d9f82d5 6614 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6615
8d9f82d5 6616 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6617 {
6618 /* For int foo[] = (int [3]){1}; we need to set array size
6619 now since later on array initializer will be just the
6620 brace enclosed list of the compound literal. */
e30ecc5d 6621 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6622 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6623 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6624 layout_type (type);
8d9f82d5 6625 layout_decl (cldecl, 0);
e30ecc5d
JJ
6626 TREE_TYPE (decl)
6627 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6628 }
6629 }
400fbf9f 6630 }
3e4093b6
RS
6631}
6632\f
6633/* Methods for storing and printing names for error messages. */
400fbf9f 6634
3e4093b6
RS
6635/* Implement a spelling stack that allows components of a name to be pushed
6636 and popped. Each element on the stack is this structure. */
400fbf9f 6637
3e4093b6
RS
6638struct spelling
6639{
6640 int kind;
6641 union
400fbf9f 6642 {
a0f0ab9f 6643 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6644 const char *s;
6645 } u;
6646};
2f6e4e97 6647
3e4093b6
RS
6648#define SPELLING_STRING 1
6649#define SPELLING_MEMBER 2
6650#define SPELLING_BOUNDS 3
400fbf9f 6651
3e4093b6
RS
6652static struct spelling *spelling; /* Next stack element (unused). */
6653static struct spelling *spelling_base; /* Spelling stack base. */
6654static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6655
3e4093b6
RS
6656/* Macros to save and restore the spelling stack around push_... functions.
6657 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6658
3e4093b6
RS
6659#define SPELLING_DEPTH() (spelling - spelling_base)
6660#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6661
3e4093b6
RS
6662/* Push an element on the spelling stack with type KIND and assign VALUE
6663 to MEMBER. */
400fbf9f 6664
3e4093b6
RS
6665#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6666{ \
6667 int depth = SPELLING_DEPTH (); \
6668 \
6669 if (depth >= spelling_size) \
6670 { \
6671 spelling_size += 10; \
cca8ead2
BI
6672 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6673 spelling_size); \
3e4093b6
RS
6674 RESTORE_SPELLING_DEPTH (depth); \
6675 } \
6676 \
6677 spelling->kind = (KIND); \
6678 spelling->MEMBER = (VALUE); \
6679 spelling++; \
6680}
400fbf9f 6681
3e4093b6 6682/* Push STRING on the stack. Printed literally. */
400fbf9f 6683
3e4093b6
RS
6684static void
6685push_string (const char *string)
6686{
6687 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6688}
400fbf9f 6689
3e4093b6 6690/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6691
3e4093b6
RS
6692static void
6693push_member_name (tree decl)
6694{
6695 const char *const string
88388a52
JM
6696 = (DECL_NAME (decl)
6697 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6698 : _("<anonymous>"));
3e4093b6
RS
6699 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6700}
400fbf9f 6701
3e4093b6 6702/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6703
3e4093b6 6704static void
a0f0ab9f 6705push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6706{
6707 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6708}
bb58bec5 6709
3e4093b6 6710/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6711
3e4093b6
RS
6712static int
6713spelling_length (void)
6714{
6715 int size = 0;
6716 struct spelling *p;
400fbf9f 6717
3e4093b6
RS
6718 for (p = spelling_base; p < spelling; p++)
6719 {
6720 if (p->kind == SPELLING_BOUNDS)
6721 size += 25;
6722 else
6723 size += strlen (p->u.s) + 1;
6724 }
6725
6726 return size;
400fbf9f 6727}
400fbf9f 6728
3e4093b6 6729/* Print the spelling to BUFFER and return it. */
400fbf9f 6730
3e4093b6
RS
6731static char *
6732print_spelling (char *buffer)
400fbf9f 6733{
3e4093b6
RS
6734 char *d = buffer;
6735 struct spelling *p;
400fbf9f 6736
3e4093b6
RS
6737 for (p = spelling_base; p < spelling; p++)
6738 if (p->kind == SPELLING_BOUNDS)
6739 {
a0f0ab9f 6740 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6741 d += strlen (d);
6742 }
6743 else
6744 {
6745 const char *s;
6746 if (p->kind == SPELLING_MEMBER)
6747 *d++ = '.';
6748 for (s = p->u.s; (*d = *s++); d++)
6749 ;
6750 }
6751 *d++ = '\0';
6752 return buffer;
6753}
400fbf9f 6754
3e4093b6
RS
6755/* Digest the parser output INIT as an initializer for type TYPE.
6756 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6757
bbbbb16a
ILT
6758 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6759
928c19bb
JM
6760 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6761
916c5919
JM
6762 If INIT is a string constant, STRICT_STRING is true if it is
6763 unparenthesized or we should not warn here for it being parenthesized.
6764 For other types of INIT, STRICT_STRING is not used.
6765
c2255bc4
AH
6766 INIT_LOC is the location of the INIT.
6767
3e4093b6
RS
6768 REQUIRE_CONSTANT requests an error if non-constant initializers or
6769 elements are seen. */
7e842ef8 6770
3e4093b6 6771static tree
c2255bc4
AH
6772digest_init (location_t init_loc, tree type, tree init, tree origtype,
6773 bool null_pointer_constant, bool strict_string,
6774 int require_constant)
3e4093b6
RS
6775{
6776 enum tree_code code = TREE_CODE (type);
6777 tree inside_init = init;
8ce94e44 6778 tree semantic_type = NULL_TREE;
928c19bb 6779 bool maybe_const = true;
7e842ef8 6780
3e4093b6 6781 if (type == error_mark_node
f01da1a5 6782 || !init
7a0ca710 6783 || error_operand_p (init))
3e4093b6 6784 return error_mark_node;
7e842ef8 6785
ed248cf7 6786 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6787
8ce94e44
JM
6788 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6789 {
6790 semantic_type = TREE_TYPE (inside_init);
6791 inside_init = TREE_OPERAND (inside_init, 0);
6792 }
928c19bb
JM
6793 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6794 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6795
3e4093b6
RS
6796 /* Initialization of an array of chars from a string constant
6797 optionally enclosed in braces. */
7e842ef8 6798
197463ae
JM
6799 if (code == ARRAY_TYPE && inside_init
6800 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6801 {
267bac10
JM
6802 tree typ1
6803 = (TYPE_ATOMIC (TREE_TYPE (type))
6804 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6805 TYPE_QUAL_ATOMIC)
6806 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6807 /* Note that an array could be both an array of character type
6808 and an array of wchar_t if wchar_t is signed char or unsigned
6809 char. */
6810 bool char_array = (typ1 == char_type_node
6811 || typ1 == signed_char_type_node
6812 || typ1 == unsigned_char_type_node);
6813 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6814 bool char16_array = !!comptypes (typ1, char16_type_node);
6815 bool char32_array = !!comptypes (typ1, char32_type_node);
6816
6817 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6818 {
916c5919 6819 struct c_expr expr;
c466b2cd 6820 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6821 expr.value = inside_init;
6822 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6823 expr.original_type = NULL;
d033409e 6824 maybe_warn_string_init (init_loc, type, expr);
916c5919 6825
a45e580b 6826 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6827 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6828 "initialization of a flexible array member");
6829
3e4093b6 6830 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6831 TYPE_MAIN_VARIANT (type)))
3e4093b6 6832 return inside_init;
7e842ef8 6833
c466b2cd 6834 if (char_array)
3e4093b6 6835 {
c466b2cd
KVH
6836 if (typ2 != char_type_node)
6837 {
ea58ef42
MP
6838 error_init (init_loc, "char-array initialized from wide "
6839 "string");
c466b2cd
KVH
6840 return error_mark_node;
6841 }
3e4093b6 6842 }
c466b2cd 6843 else
3e4093b6 6844 {
c466b2cd
KVH
6845 if (typ2 == char_type_node)
6846 {
ea58ef42
MP
6847 error_init (init_loc, "wide character array initialized "
6848 "from non-wide string");
c466b2cd
KVH
6849 return error_mark_node;
6850 }
6851 else if (!comptypes(typ1, typ2))
6852 {
ea58ef42
MP
6853 error_init (init_loc, "wide character array initialized "
6854 "from incompatible wide string");
c466b2cd
KVH
6855 return error_mark_node;
6856 }
7e842ef8 6857 }
2f6e4e97 6858
3e4093b6
RS
6859 TREE_TYPE (inside_init) = type;
6860 if (TYPE_DOMAIN (type) != 0
6861 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6862 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6863 {
6864 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6865
c466b2cd 6866 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6867 because it's ok to ignore the terminating null char
6868 that is counted in the length of the constant. */
5eb4df45
ILT
6869 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6870 (len
6871 - (TYPE_PRECISION (typ1)
6872 / BITS_PER_UNIT))))
6873 pedwarn_init (init_loc, 0,
6874 ("initializer-string for array of chars "
6875 "is too long"));
6876 else if (warn_cxx_compat
6877 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6878 warning_at (init_loc, OPT_Wc___compat,
6879 ("initializer-string for array chars "
6880 "is too long for C++"));
6881 }
7e842ef8 6882
3e4093b6 6883 return inside_init;
7e842ef8 6884 }
197463ae
JM
6885 else if (INTEGRAL_TYPE_P (typ1))
6886 {
ea58ef42 6887 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6888 "from string constant");
6889 return error_mark_node;
6890 }
7e842ef8
PE
6891 }
6892
3e4093b6
RS
6893 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6894 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6895 below and handle as a constructor. */
e89be13b 6896 if (code == VECTOR_TYPE
31521951 6897 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 6898 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6899 && TREE_CONSTANT (inside_init))
6900 {
6901 if (TREE_CODE (inside_init) == VECTOR_CST
6902 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6903 TYPE_MAIN_VARIANT (type)))
6904 return inside_init;
6905
6906 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6907 {
4038c495
GB
6908 unsigned HOST_WIDE_INT ix;
6909 tree value;
6910 bool constant_p = true;
e89be13b
JJ
6911
6912 /* Iterate through elements and check if all constructor
6913 elements are *_CSTs. */
4038c495
GB
6914 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6915 if (!CONSTANT_CLASS_P (value))
6916 {
6917 constant_p = false;
6918 break;
6919 }
e89be13b 6920
4038c495
GB
6921 if (constant_p)
6922 return build_vector_from_ctor (type,
6923 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6924 }
6925 }
6035d635 6926
ca085fd7
MLI
6927 if (warn_sequence_point)
6928 verify_sequence_points (inside_init);
6929
3e4093b6
RS
6930 /* Any type can be initialized
6931 from an expression of the same type, optionally with braces. */
400fbf9f 6932
3e4093b6
RS
6933 if (inside_init && TREE_TYPE (inside_init) != 0
6934 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6935 TYPE_MAIN_VARIANT (type))
3e4093b6 6936 || (code == ARRAY_TYPE
132da1a5 6937 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6938 || (code == VECTOR_TYPE
132da1a5 6939 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6940 || (code == POINTER_TYPE
3897f229 6941 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6942 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6943 TREE_TYPE (type)))))
3e4093b6
RS
6944 {
6945 if (code == POINTER_TYPE)
b494fd98 6946 {
b494fd98
EB
6947 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6948 {
f2a71bbc
JM
6949 if (TREE_CODE (inside_init) == STRING_CST
6950 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6951 inside_init = array_to_pointer_conversion
6952 (init_loc, inside_init);
f2a71bbc
JM
6953 else
6954 {
ea58ef42 6955 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6956 return error_mark_node;
6957 }
b494fd98 6958 }
f2a71bbc 6959 }
b494fd98 6960
bae39a73
NS
6961 if (code == VECTOR_TYPE)
6962 /* Although the types are compatible, we may require a
6963 conversion. */
6964 inside_init = convert (type, inside_init);
3e4093b6 6965
ca58211b 6966 if (require_constant
3e4093b6 6967 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6968 {
3e4093b6
RS
6969 /* As an extension, allow initializing objects with static storage
6970 duration with compound literals (which are then treated just as
ca58211b
PB
6971 the brace enclosed list they contain). Also allow this for
6972 vectors, as we can only assign them with compound literals. */
7278465e
MP
6973 if (flag_isoc99 && code != VECTOR_TYPE)
6974 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6975 "is not constant");
3e4093b6
RS
6976 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6977 inside_init = DECL_INITIAL (decl);
400fbf9f 6978 }
3e4093b6
RS
6979
6980 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6981 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6982 {
ea58ef42
MP
6983 error_init (init_loc, "array initialized from non-constant array "
6984 "expression");
3e4093b6 6985 return error_mark_node;
400fbf9f 6986 }
400fbf9f 6987
c1771a20 6988 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6989 -pedantic-errors is specified. In the later case, we always want
6990 an error. In the former case, we simply want a warning. */
6991 if (require_constant && pedantic
6992 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6993 {
6994 inside_init
6995 = valid_compound_expr_initializer (inside_init,
6996 TREE_TYPE (inside_init));
6997 if (inside_init == error_mark_node)
ea58ef42 6998 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6999 else
c1771a20 7000 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7001 "initializer element is not constant");
3e4093b6
RS
7002 if (flag_pedantic_errors)
7003 inside_init = error_mark_node;
7004 }
7005 else if (require_constant
116df786
RH
7006 && !initializer_constant_valid_p (inside_init,
7007 TREE_TYPE (inside_init)))
3e4093b6 7008 {
ea58ef42 7009 error_init (init_loc, "initializer element is not constant");
3e4093b6 7010 inside_init = error_mark_node;
8b40563c 7011 }
928c19bb 7012 else if (require_constant && !maybe_const)
3aa3c9fc 7013 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7014 "initializer element is not a constant expression");
f735a153 7015
90137d8f 7016 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7017 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7018 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7019 type, inside_init, origtype,
bbbbb16a 7020 ic_init, null_pointer_constant,
928c19bb 7021 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7022 return inside_init;
7023 }
f735a153 7024
3e4093b6 7025 /* Handle scalar types, including conversions. */
400fbf9f 7026
ab22c1fa
CF
7027 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7028 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7029 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7030 {
f2a71bbc
JM
7031 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7032 && (TREE_CODE (init) == STRING_CST
7033 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7034 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7035 if (semantic_type)
7036 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7037 inside_init);
3e4093b6 7038 inside_init
68fca595
MP
7039 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7040 inside_init, origtype, ic_init,
7041 null_pointer_constant, NULL_TREE, NULL_TREE,
7042 0);
2f6e4e97 7043
3274deff
JW
7044 /* Check to see if we have already given an error message. */
7045 if (inside_init == error_mark_node)
7046 ;
3f75a254 7047 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7048 {
ea58ef42 7049 error_init (init_loc, "initializer element is not constant");
3e4093b6 7050 inside_init = error_mark_node;
400fbf9f 7051 }
3e4093b6 7052 else if (require_constant
116df786
RH
7053 && !initializer_constant_valid_p (inside_init,
7054 TREE_TYPE (inside_init)))
400fbf9f 7055 {
ea58ef42
MP
7056 error_init (init_loc, "initializer element is not computable at "
7057 "load time");
3e4093b6 7058 inside_init = error_mark_node;
400fbf9f 7059 }
928c19bb 7060 else if (require_constant && !maybe_const)
451b5e48 7061 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7062 "initializer element is not a constant expression");
3e4093b6
RS
7063
7064 return inside_init;
400fbf9f 7065 }
d9fc6069 7066
3e4093b6 7067 /* Come here only for records and arrays. */
d9fc6069 7068
3e4093b6 7069 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7070 {
ea58ef42 7071 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7072 return error_mark_node;
d9fc6069 7073 }
3e4093b6 7074
ea58ef42 7075 error_init (init_loc, "invalid initializer");
3e4093b6 7076 return error_mark_node;
d9fc6069 7077}
400fbf9f 7078\f
3e4093b6 7079/* Handle initializers that use braces. */
400fbf9f 7080
3e4093b6
RS
7081/* Type of object we are accumulating a constructor for.
7082 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7083static tree constructor_type;
400fbf9f 7084
3e4093b6
RS
7085/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7086 left to fill. */
7087static tree constructor_fields;
400fbf9f 7088
3e4093b6
RS
7089/* For an ARRAY_TYPE, this is the specified index
7090 at which to store the next element we get. */
7091static tree constructor_index;
400fbf9f 7092
3e4093b6
RS
7093/* For an ARRAY_TYPE, this is the maximum index. */
7094static tree constructor_max_index;
400fbf9f 7095
3e4093b6
RS
7096/* For a RECORD_TYPE, this is the first field not yet written out. */
7097static tree constructor_unfilled_fields;
400fbf9f 7098
3e4093b6
RS
7099/* For an ARRAY_TYPE, this is the index of the first element
7100 not yet written out. */
7101static tree constructor_unfilled_index;
895ea614 7102
3e4093b6
RS
7103/* In a RECORD_TYPE, the byte index of the next consecutive field.
7104 This is so we can generate gaps between fields, when appropriate. */
7105static tree constructor_bit_index;
10d5caec 7106
3e4093b6
RS
7107/* If we are saving up the elements rather than allocating them,
7108 this is the list of elements so far (in reverse order,
7109 most recent first). */
9771b263 7110static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7111
3e4093b6
RS
7112/* 1 if constructor should be incrementally stored into a constructor chain,
7113 0 if all the elements should be kept in AVL tree. */
7114static int constructor_incremental;
ad47f1e5 7115
3e4093b6
RS
7116/* 1 if so far this constructor's elements are all compile-time constants. */
7117static int constructor_constant;
ad47f1e5 7118
3e4093b6
RS
7119/* 1 if so far this constructor's elements are all valid address constants. */
7120static int constructor_simple;
ad47f1e5 7121
928c19bb
JM
7122/* 1 if this constructor has an element that cannot be part of a
7123 constant expression. */
7124static int constructor_nonconst;
7125
3e4093b6
RS
7126/* 1 if this constructor is erroneous so far. */
7127static int constructor_erroneous;
d45cf215 7128
9bac5cbb
G
7129/* 1 if this constructor is the universal zero initializer { 0 }. */
7130static int constructor_zeroinit;
7131
3e4093b6
RS
7132/* Structure for managing pending initializer elements, organized as an
7133 AVL tree. */
d45cf215 7134
3e4093b6 7135struct init_node
d45cf215 7136{
3e4093b6
RS
7137 struct init_node *left, *right;
7138 struct init_node *parent;
7139 int balance;
7140 tree purpose;
7141 tree value;
bbbbb16a 7142 tree origtype;
d45cf215
RS
7143};
7144
3e4093b6
RS
7145/* Tree of pending elements at this constructor level.
7146 These are elements encountered out of order
7147 which belong at places we haven't reached yet in actually
7148 writing the output.
7149 Will never hold tree nodes across GC runs. */
7150static struct init_node *constructor_pending_elts;
d45cf215 7151
3e4093b6
RS
7152/* The SPELLING_DEPTH of this constructor. */
7153static int constructor_depth;
d45cf215 7154
3e4093b6
RS
7155/* DECL node for which an initializer is being read.
7156 0 means we are reading a constructor expression
7157 such as (struct foo) {...}. */
7158static tree constructor_decl;
d45cf215 7159
3e4093b6
RS
7160/* Nonzero if this is an initializer for a top-level decl. */
7161static int constructor_top_level;
d45cf215 7162
3e4093b6
RS
7163/* Nonzero if there were any member designators in this initializer. */
7164static int constructor_designated;
d45cf215 7165
3e4093b6
RS
7166/* Nesting depth of designator list. */
7167static int designator_depth;
d45cf215 7168
3e4093b6 7169/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7170static int designator_erroneous;
d45cf215 7171
3e4093b6
RS
7172\f
7173/* This stack has a level for each implicit or explicit level of
7174 structuring in the initializer, including the outermost one. It
7175 saves the values of most of the variables above. */
d45cf215 7176
3e4093b6
RS
7177struct constructor_range_stack;
7178
7179struct constructor_stack
d45cf215 7180{
3e4093b6
RS
7181 struct constructor_stack *next;
7182 tree type;
7183 tree fields;
7184 tree index;
7185 tree max_index;
7186 tree unfilled_index;
7187 tree unfilled_fields;
7188 tree bit_index;
9771b263 7189 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7190 struct init_node *pending_elts;
7191 int offset;
7192 int depth;
916c5919 7193 /* If value nonzero, this value should replace the entire
3e4093b6 7194 constructor at this level. */
916c5919 7195 struct c_expr replacement_value;
3e4093b6
RS
7196 struct constructor_range_stack *range_stack;
7197 char constant;
7198 char simple;
928c19bb 7199 char nonconst;
3e4093b6
RS
7200 char implicit;
7201 char erroneous;
7202 char outer;
7203 char incremental;
7204 char designated;
976d5a22 7205 int designator_depth;
3e4093b6 7206};
d45cf215 7207
802415d1 7208static struct constructor_stack *constructor_stack;
d45cf215 7209
3e4093b6
RS
7210/* This stack represents designators from some range designator up to
7211 the last designator in the list. */
d45cf215 7212
3e4093b6
RS
7213struct constructor_range_stack
7214{
7215 struct constructor_range_stack *next, *prev;
7216 struct constructor_stack *stack;
7217 tree range_start;
7218 tree index;
7219 tree range_end;
7220 tree fields;
7221};
d45cf215 7222
802415d1 7223static struct constructor_range_stack *constructor_range_stack;
d45cf215 7224
3e4093b6
RS
7225/* This stack records separate initializers that are nested.
7226 Nested initializers can't happen in ANSI C, but GNU C allows them
7227 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7228
3e4093b6 7229struct initializer_stack
d45cf215 7230{
3e4093b6
RS
7231 struct initializer_stack *next;
7232 tree decl;
3e4093b6
RS
7233 struct constructor_stack *constructor_stack;
7234 struct constructor_range_stack *constructor_range_stack;
9771b263 7235 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7236 struct spelling *spelling;
7237 struct spelling *spelling_base;
7238 int spelling_size;
7239 char top_level;
7240 char require_constant_value;
7241 char require_constant_elements;
7242};
d45cf215 7243
802415d1 7244static struct initializer_stack *initializer_stack;
3e4093b6
RS
7245\f
7246/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7247
7248void
a396f8ae 7249start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7250{
3e4093b6 7251 const char *locus;
cceb1885 7252 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7253
3e4093b6 7254 p->decl = constructor_decl;
3e4093b6
RS
7255 p->require_constant_value = require_constant_value;
7256 p->require_constant_elements = require_constant_elements;
7257 p->constructor_stack = constructor_stack;
7258 p->constructor_range_stack = constructor_range_stack;
7259 p->elements = constructor_elements;
7260 p->spelling = spelling;
7261 p->spelling_base = spelling_base;
7262 p->spelling_size = spelling_size;
7263 p->top_level = constructor_top_level;
7264 p->next = initializer_stack;
7265 initializer_stack = p;
400fbf9f 7266
3e4093b6 7267 constructor_decl = decl;
3e4093b6
RS
7268 constructor_designated = 0;
7269 constructor_top_level = top_level;
400fbf9f 7270
6f17bbcf 7271 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7272 {
7273 require_constant_value = TREE_STATIC (decl);
7274 require_constant_elements
7275 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7276 /* For a scalar, you can always use any value to initialize,
7277 even within braces. */
296a8c2f 7278 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7279 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7280 }
7281 else
7282 {
7283 require_constant_value = 0;
7284 require_constant_elements = 0;
88388a52 7285 locus = _("(anonymous)");
3e4093b6 7286 }
b71c7f8a 7287
3e4093b6
RS
7288 constructor_stack = 0;
7289 constructor_range_stack = 0;
b71c7f8a 7290
9bac5cbb 7291 found_missing_braces = 0;
3e4093b6
RS
7292
7293 spelling_base = 0;
7294 spelling_size = 0;
7295 RESTORE_SPELLING_DEPTH (0);
7296
7297 if (locus)
7298 push_string (locus);
7299}
7300
7301void
7302finish_init (void)
b71c7f8a 7303{
3e4093b6 7304 struct initializer_stack *p = initializer_stack;
b71c7f8a 7305
3e4093b6
RS
7306 /* Free the whole constructor stack of this initializer. */
7307 while (constructor_stack)
7308 {
7309 struct constructor_stack *q = constructor_stack;
7310 constructor_stack = q->next;
7311 free (q);
7312 }
7313
366de0ce 7314 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7315
7316 /* Pop back to the data of the outer initializer (if any). */
36579663 7317 free (spelling_base);
3aeb3655 7318
3e4093b6 7319 constructor_decl = p->decl;
3e4093b6
RS
7320 require_constant_value = p->require_constant_value;
7321 require_constant_elements = p->require_constant_elements;
7322 constructor_stack = p->constructor_stack;
7323 constructor_range_stack = p->constructor_range_stack;
7324 constructor_elements = p->elements;
7325 spelling = p->spelling;
7326 spelling_base = p->spelling_base;
7327 spelling_size = p->spelling_size;
7328 constructor_top_level = p->top_level;
7329 initializer_stack = p->next;
7330 free (p);
b71c7f8a 7331}
400fbf9f 7332\f
3e4093b6
RS
7333/* Call here when we see the initializer is surrounded by braces.
7334 This is instead of a call to push_init_level;
7335 it is matched by a call to pop_init_level.
400fbf9f 7336
3e4093b6
RS
7337 TYPE is the type to initialize, for a constructor expression.
7338 For an initializer for a decl, TYPE is zero. */
400fbf9f 7339
3e4093b6
RS
7340void
7341really_start_incremental_init (tree type)
400fbf9f 7342{
5d038c4c 7343 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7344
3e4093b6
RS
7345 if (type == 0)
7346 type = TREE_TYPE (constructor_decl);
400fbf9f 7347
31521951 7348 if (VECTOR_TYPE_P (type)
b6fc2cdb 7349 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7350 error ("opaque vector types cannot be initialized");
400fbf9f 7351
3e4093b6
RS
7352 p->type = constructor_type;
7353 p->fields = constructor_fields;
7354 p->index = constructor_index;
7355 p->max_index = constructor_max_index;
7356 p->unfilled_index = constructor_unfilled_index;
7357 p->unfilled_fields = constructor_unfilled_fields;
7358 p->bit_index = constructor_bit_index;
7359 p->elements = constructor_elements;
7360 p->constant = constructor_constant;
7361 p->simple = constructor_simple;
928c19bb 7362 p->nonconst = constructor_nonconst;
3e4093b6
RS
7363 p->erroneous = constructor_erroneous;
7364 p->pending_elts = constructor_pending_elts;
7365 p->depth = constructor_depth;
916c5919
JM
7366 p->replacement_value.value = 0;
7367 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7368 p->replacement_value.original_type = NULL;
3e4093b6
RS
7369 p->implicit = 0;
7370 p->range_stack = 0;
7371 p->outer = 0;
7372 p->incremental = constructor_incremental;
7373 p->designated = constructor_designated;
976d5a22 7374 p->designator_depth = designator_depth;
3e4093b6
RS
7375 p->next = 0;
7376 constructor_stack = p;
b13aca19 7377
3e4093b6
RS
7378 constructor_constant = 1;
7379 constructor_simple = 1;
928c19bb 7380 constructor_nonconst = 0;
3e4093b6 7381 constructor_depth = SPELLING_DEPTH ();
9771b263 7382 constructor_elements = NULL;
3e4093b6
RS
7383 constructor_pending_elts = 0;
7384 constructor_type = type;
7385 constructor_incremental = 1;
7386 constructor_designated = 0;
9bac5cbb 7387 constructor_zeroinit = 1;
3e4093b6 7388 designator_depth = 0;
b06df647 7389 designator_erroneous = 0;
400fbf9f 7390
3e636daf 7391 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 7392 {
3e4093b6
RS
7393 constructor_fields = TYPE_FIELDS (constructor_type);
7394 /* Skip any nameless bit fields at the beginning. */
7395 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7396 && DECL_NAME (constructor_fields) == 0)
910ad8de 7397 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7398
3e4093b6
RS
7399 constructor_unfilled_fields = constructor_fields;
7400 constructor_bit_index = bitsize_zero_node;
400fbf9f 7401 }
3e4093b6
RS
7402 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7403 {
7404 if (TYPE_DOMAIN (constructor_type))
7405 {
7406 constructor_max_index
7407 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7408
3e4093b6
RS
7409 /* Detect non-empty initializations of zero-length arrays. */
7410 if (constructor_max_index == NULL_TREE
7411 && TYPE_SIZE (constructor_type))
9a9d280e 7412 constructor_max_index = integer_minus_one_node;
400fbf9f 7413
3e4093b6
RS
7414 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7415 to initialize VLAs will cause a proper error; avoid tree
7416 checking errors as well by setting a safe value. */
7417 if (constructor_max_index
7418 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7419 constructor_max_index = integer_minus_one_node;
59c83dbf 7420
3e4093b6
RS
7421 constructor_index
7422 = convert (bitsizetype,
7423 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7424 }
3e4093b6 7425 else
493179da
JM
7426 {
7427 constructor_index = bitsize_zero_node;
7428 constructor_max_index = NULL_TREE;
7429 }
59c83dbf 7430
3e4093b6
RS
7431 constructor_unfilled_index = constructor_index;
7432 }
31521951 7433 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7434 {
7435 /* Vectors are like simple fixed-size arrays. */
7436 constructor_max_index =
c62c040f 7437 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7438 constructor_index = bitsize_zero_node;
3e4093b6
RS
7439 constructor_unfilled_index = constructor_index;
7440 }
7441 else
7442 {
7443 /* Handle the case of int x = {5}; */
7444 constructor_fields = constructor_type;
7445 constructor_unfilled_fields = constructor_type;
7446 }
7447}
7448\f
7449/* Push down into a subobject, for initialization.
7450 If this is for an explicit set of braces, IMPLICIT is 0.
7451 If it is because the next element belongs at a lower level,
7452 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7453
3e4093b6 7454void
ea58ef42
MP
7455push_init_level (location_t loc, int implicit,
7456 struct obstack *braced_init_obstack)
3e4093b6
RS
7457{
7458 struct constructor_stack *p;
7459 tree value = NULL_TREE;
400fbf9f 7460
3e4093b6 7461 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7462 pop them now. If implicit == 1, this will have been done in
7463 process_init_element; do not repeat it here because in the case
7464 of excess initializers for an empty aggregate this leads to an
7465 infinite cycle of popping a level and immediately recreating
7466 it. */
7467 if (implicit != 1)
3e4093b6 7468 {
472d98b4
JM
7469 while (constructor_stack->implicit)
7470 {
3e636daf 7471 if (RECORD_OR_UNION_TYPE_P (constructor_type)
472d98b4 7472 && constructor_fields == 0)
34cf811f 7473 process_init_element (input_location,
ea58ef42 7474 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7475 true, braced_init_obstack);
472d98b4
JM
7476 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7477 && constructor_max_index
7478 && tree_int_cst_lt (constructor_max_index,
7479 constructor_index))
34cf811f 7480 process_init_element (input_location,
ea58ef42 7481 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7482 true, braced_init_obstack);
472d98b4
JM
7483 else
7484 break;
7485 }
3e4093b6 7486 }
400fbf9f 7487
3e4093b6
RS
7488 /* Unless this is an explicit brace, we need to preserve previous
7489 content if any. */
7490 if (implicit)
7491 {
3e636daf 7492 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 7493 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7494 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7495 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7496 }
7497
5d038c4c 7498 p = XNEW (struct constructor_stack);
3e4093b6
RS
7499 p->type = constructor_type;
7500 p->fields = constructor_fields;
7501 p->index = constructor_index;
7502 p->max_index = constructor_max_index;
7503 p->unfilled_index = constructor_unfilled_index;
7504 p->unfilled_fields = constructor_unfilled_fields;
7505 p->bit_index = constructor_bit_index;
7506 p->elements = constructor_elements;
7507 p->constant = constructor_constant;
7508 p->simple = constructor_simple;
928c19bb 7509 p->nonconst = constructor_nonconst;
3e4093b6
RS
7510 p->erroneous = constructor_erroneous;
7511 p->pending_elts = constructor_pending_elts;
7512 p->depth = constructor_depth;
916c5919
JM
7513 p->replacement_value.value = 0;
7514 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7515 p->replacement_value.original_type = NULL;
3e4093b6
RS
7516 p->implicit = implicit;
7517 p->outer = 0;
7518 p->incremental = constructor_incremental;
7519 p->designated = constructor_designated;
976d5a22 7520 p->designator_depth = designator_depth;
3e4093b6
RS
7521 p->next = constructor_stack;
7522 p->range_stack = 0;
7523 constructor_stack = p;
400fbf9f 7524
3e4093b6
RS
7525 constructor_constant = 1;
7526 constructor_simple = 1;
928c19bb 7527 constructor_nonconst = 0;
3e4093b6 7528 constructor_depth = SPELLING_DEPTH ();
9771b263 7529 constructor_elements = NULL;
3e4093b6
RS
7530 constructor_incremental = 1;
7531 constructor_designated = 0;
7532 constructor_pending_elts = 0;
7533 if (!implicit)
400fbf9f 7534 {
3e4093b6
RS
7535 p->range_stack = constructor_range_stack;
7536 constructor_range_stack = 0;
7537 designator_depth = 0;
b06df647 7538 designator_erroneous = 0;
3e4093b6 7539 }
400fbf9f 7540
3e4093b6
RS
7541 /* Don't die if an entire brace-pair level is superfluous
7542 in the containing level. */
7543 if (constructor_type == 0)
7544 ;
3e636daf 7545 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7546 {
7547 /* Don't die if there are extra init elts at the end. */
7548 if (constructor_fields == 0)
7549 constructor_type = 0;
7550 else
400fbf9f 7551 {
3e4093b6
RS
7552 constructor_type = TREE_TYPE (constructor_fields);
7553 push_member_name (constructor_fields);
7554 constructor_depth++;
400fbf9f 7555 }
6a358dcb
MP
7556 /* If upper initializer is designated, then mark this as
7557 designated too to prevent bogus warnings. */
7558 constructor_designated = p->designated;
3e4093b6
RS
7559 }
7560 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7561 {
7562 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7563 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7564 constructor_depth++;
400fbf9f
JW
7565 }
7566
3e4093b6 7567 if (constructor_type == 0)
400fbf9f 7568 {
ea58ef42 7569 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7570 constructor_fields = 0;
7571 constructor_unfilled_fields = 0;
7572 return;
400fbf9f
JW
7573 }
7574
3e4093b6
RS
7575 if (value && TREE_CODE (value) == CONSTRUCTOR)
7576 {
7577 constructor_constant = TREE_CONSTANT (value);
7578 constructor_simple = TREE_STATIC (value);
928c19bb 7579 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7580 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7581 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7582 && (TREE_CODE (constructor_type) == RECORD_TYPE
7583 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7584 set_nonincremental_init (braced_init_obstack);
3e4093b6 7585 }
400fbf9f 7586
9bac5cbb
G
7587 if (implicit == 1)
7588 found_missing_braces = 1;
400fbf9f 7589
3e636daf 7590 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
7591 {
7592 constructor_fields = TYPE_FIELDS (constructor_type);
7593 /* Skip any nameless bit fields at the beginning. */
7594 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7595 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7596 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7597
3e4093b6
RS
7598 constructor_unfilled_fields = constructor_fields;
7599 constructor_bit_index = bitsize_zero_node;
7600 }
31521951 7601 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7602 {
7603 /* Vectors are like simple fixed-size arrays. */
7604 constructor_max_index =
c62c040f
RG
7605 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7606 constructor_index = bitsize_int (0);
3e4093b6
RS
7607 constructor_unfilled_index = constructor_index;
7608 }
7609 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7610 {
7611 if (TYPE_DOMAIN (constructor_type))
7612 {
7613 constructor_max_index
7614 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7615
3e4093b6
RS
7616 /* Detect non-empty initializations of zero-length arrays. */
7617 if (constructor_max_index == NULL_TREE
7618 && TYPE_SIZE (constructor_type))
9a9d280e 7619 constructor_max_index = integer_minus_one_node;
de520661 7620
3e4093b6
RS
7621 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7622 to initialize VLAs will cause a proper error; avoid tree
7623 checking errors as well by setting a safe value. */
7624 if (constructor_max_index
7625 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7626 constructor_max_index = integer_minus_one_node;
b62acd60 7627
3e4093b6
RS
7628 constructor_index
7629 = convert (bitsizetype,
7630 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7631 }
7632 else
7633 constructor_index = bitsize_zero_node;
de520661 7634
3e4093b6
RS
7635 constructor_unfilled_index = constructor_index;
7636 if (value && TREE_CODE (value) == STRING_CST)
7637 {
7638 /* We need to split the char/wchar array into individual
7639 characters, so that we don't have to special case it
7640 everywhere. */
a1e3b3d9 7641 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7642 }
7643 }
7644 else
7645 {
b4519d39 7646 if (constructor_type != error_mark_node)
96b40f8d 7647 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7648 constructor_fields = constructor_type;
7649 constructor_unfilled_fields = constructor_type;
7650 }
7651}
8b6a5902 7652
3e4093b6 7653/* At the end of an implicit or explicit brace level,
916c5919
JM
7654 finish up that level of constructor. If a single expression
7655 with redundant braces initialized that level, return the
7656 c_expr structure for that expression. Otherwise, the original_code
7657 element is set to ERROR_MARK.
7658 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7659 from inner levels (process_init_element ignores that),
916c5919 7660 but return error_mark_node as the value from the outermost level
3e4093b6 7661 (that's what we want to put in DECL_INITIAL).
916c5919 7662 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7663
916c5919 7664struct c_expr
ea58ef42
MP
7665pop_init_level (location_t loc, int implicit,
7666 struct obstack *braced_init_obstack)
3e4093b6
RS
7667{
7668 struct constructor_stack *p;
916c5919
JM
7669 struct c_expr ret;
7670 ret.value = 0;
7671 ret.original_code = ERROR_MARK;
6866c6e8 7672 ret.original_type = NULL;
de520661 7673
3e4093b6
RS
7674 if (implicit == 0)
7675 {
7676 /* When we come to an explicit close brace,
7677 pop any inner levels that didn't have explicit braces. */
7678 while (constructor_stack->implicit)
34cf811f 7679 process_init_element (input_location,
ea58ef42 7680 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7681 true, braced_init_obstack);
366de0ce 7682 gcc_assert (!constructor_range_stack);
3e4093b6 7683 }
e5e809f4 7684
0066ef9c
RH
7685 /* Now output all pending elements. */
7686 constructor_incremental = 1;
a1e3b3d9 7687 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7688
3e4093b6 7689 p = constructor_stack;
e5e809f4 7690
3e4093b6
RS
7691 /* Error for initializing a flexible array member, or a zero-length
7692 array member in an inappropriate context. */
7693 if (constructor_type && constructor_fields
7694 && TREE_CODE (constructor_type) == ARRAY_TYPE
7695 && TYPE_DOMAIN (constructor_type)
3f75a254 7696 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7697 {
7698 /* Silently discard empty initializations. The parser will
7699 already have pedwarned for empty brackets. */
7700 if (integer_zerop (constructor_unfilled_index))
7701 constructor_type = NULL_TREE;
366de0ce 7702 else
3e4093b6 7703 {
366de0ce 7704 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7705
3e4093b6 7706 if (constructor_depth > 2)
ea58ef42 7707 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7708 else
d033409e 7709 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7710 "initialization of a flexible array member");
de520661 7711
3e4093b6
RS
7712 /* We have already issued an error message for the existence
7713 of a flexible array member not at the end of the structure.
535a42b1 7714 Discard the initializer so that we do not die later. */
910ad8de 7715 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7716 constructor_type = NULL_TREE;
7717 }
3e4093b6 7718 }
de520661 7719
7b33f0c8
MP
7720 switch (vec_safe_length (constructor_elements))
7721 {
7722 case 0:
7723 /* Initialization with { } counts as zeroinit. */
7724 constructor_zeroinit = 1;
7725 break;
7726 case 1:
7727 /* This might be zeroinit as well. */
7728 if (integer_zerop ((*constructor_elements)[0].value))
7729 constructor_zeroinit = 1;
7730 break;
7731 default:
7732 /* If the constructor has more than one element, it can't be { 0 }. */
7733 constructor_zeroinit = 0;
7734 break;
7735 }
9bac5cbb
G
7736
7737 /* Warn when some structs are initialized with direct aggregation. */
7738 if (!implicit && found_missing_braces && warn_missing_braces
7739 && !constructor_zeroinit)
7b33f0c8
MP
7740 warning_init (loc, OPT_Wmissing_braces,
7741 "missing braces around initializer");
9bac5cbb 7742
3e4093b6 7743 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7744 if (warn_missing_field_initializers
3e4093b6
RS
7745 && constructor_type
7746 && TREE_CODE (constructor_type) == RECORD_TYPE
7747 && constructor_unfilled_fields)
7748 {
7749 /* Do not warn for flexible array members or zero-length arrays. */
7750 while (constructor_unfilled_fields
3f75a254 7751 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7752 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7753 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7754
49819fef
AM
7755 if (constructor_unfilled_fields
7756 /* Do not warn if this level of the initializer uses member
7757 designators; it is likely to be deliberate. */
7758 && !constructor_designated
84937de2 7759 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7760 && !constructor_zeroinit)
3e4093b6 7761 {
32397f22
MLI
7762 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7763 "missing initializer for field %qD of %qT",
7764 constructor_unfilled_fields,
7765 constructor_type))
7766 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7767 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7768 }
7769 }
de520661 7770
3e4093b6 7771 /* Pad out the end of the structure. */
916c5919 7772 if (p->replacement_value.value)
3e4093b6
RS
7773 /* If this closes a superfluous brace pair,
7774 just pass out the element between them. */
916c5919 7775 ret = p->replacement_value;
3e4093b6
RS
7776 else if (constructor_type == 0)
7777 ;
3e636daf 7778 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 7779 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 7780 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7781 {
7782 /* A nonincremental scalar initializer--just return
7783 the element, after verifying there is just one. */
9771b263 7784 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7785 {
7786 if (!constructor_erroneous)
ea58ef42 7787 error_init (loc, "empty scalar initializer");
916c5919 7788 ret.value = error_mark_node;
3e4093b6 7789 }
9771b263 7790 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7791 {
ea58ef42 7792 error_init (loc, "extra elements in scalar initializer");
9771b263 7793 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7794 }
7795 else
9771b263 7796 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7797 }
7798 else
7799 {
7800 if (constructor_erroneous)
916c5919 7801 ret.value = error_mark_node;
3e4093b6
RS
7802 else
7803 {
916c5919 7804 ret.value = build_constructor (constructor_type,
4038c495 7805 constructor_elements);
3e4093b6 7806 if (constructor_constant)
51eed280 7807 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7808 if (constructor_constant && constructor_simple)
916c5919 7809 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7810 if (constructor_nonconst)
7811 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7812 }
7813 }
de520661 7814
928c19bb
JM
7815 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7816 {
7817 if (constructor_nonconst)
7818 ret.original_code = C_MAYBE_CONST_EXPR;
7819 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7820 ret.original_code = ERROR_MARK;
7821 }
7822
3e4093b6
RS
7823 constructor_type = p->type;
7824 constructor_fields = p->fields;
7825 constructor_index = p->index;
7826 constructor_max_index = p->max_index;
7827 constructor_unfilled_index = p->unfilled_index;
7828 constructor_unfilled_fields = p->unfilled_fields;
7829 constructor_bit_index = p->bit_index;
7830 constructor_elements = p->elements;
7831 constructor_constant = p->constant;
7832 constructor_simple = p->simple;
928c19bb 7833 constructor_nonconst = p->nonconst;
3e4093b6
RS
7834 constructor_erroneous = p->erroneous;
7835 constructor_incremental = p->incremental;
7836 constructor_designated = p->designated;
976d5a22 7837 designator_depth = p->designator_depth;
3e4093b6
RS
7838 constructor_pending_elts = p->pending_elts;
7839 constructor_depth = p->depth;
7840 if (!p->implicit)
7841 constructor_range_stack = p->range_stack;
7842 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7843
3e4093b6
RS
7844 constructor_stack = p->next;
7845 free (p);
b621a4dd 7846
5d5e98dc
VR
7847 if (ret.value == 0 && constructor_stack == 0)
7848 ret.value = error_mark_node;
916c5919 7849 return ret;
3e4093b6 7850}
8b6a5902 7851
3e4093b6
RS
7852/* Common handling for both array range and field name designators.
7853 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7854
3e4093b6 7855static int
ea58ef42
MP
7856set_designator (location_t loc, int array,
7857 struct obstack *braced_init_obstack)
de520661 7858{
3e4093b6
RS
7859 tree subtype;
7860 enum tree_code subcode;
de520661 7861
3e4093b6
RS
7862 /* Don't die if an entire brace-pair level is superfluous
7863 in the containing level. */
7864 if (constructor_type == 0)
7865 return 1;
de520661 7866
366de0ce
NS
7867 /* If there were errors in this designator list already, bail out
7868 silently. */
b06df647 7869 if (designator_erroneous)
3e4093b6 7870 return 1;
e28cae4f 7871
3e4093b6
RS
7872 if (!designator_depth)
7873 {
366de0ce 7874 gcc_assert (!constructor_range_stack);
de520661 7875
3e4093b6
RS
7876 /* Designator list starts at the level of closest explicit
7877 braces. */
7878 while (constructor_stack->implicit)
34cf811f 7879 process_init_element (input_location,
ea58ef42 7880 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7881 true, braced_init_obstack);
3e4093b6
RS
7882 constructor_designated = 1;
7883 return 0;
7884 }
de520661 7885
366de0ce 7886 switch (TREE_CODE (constructor_type))
3c3fa147 7887 {
366de0ce
NS
7888 case RECORD_TYPE:
7889 case UNION_TYPE:
3e4093b6
RS
7890 subtype = TREE_TYPE (constructor_fields);
7891 if (subtype != error_mark_node)
7892 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7893 break;
7894 case ARRAY_TYPE:
3e4093b6 7895 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7896 break;
7897 default:
7898 gcc_unreachable ();
de520661 7899 }
400fbf9f 7900
3e4093b6
RS
7901 subcode = TREE_CODE (subtype);
7902 if (array && subcode != ARRAY_TYPE)
7903 {
ea58ef42 7904 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7905 return 1;
7906 }
7907 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7908 {
ea58ef42 7909 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7910 return 1;
7911 }
d45cf215 7912
3e4093b6 7913 constructor_designated = 1;
ea58ef42 7914 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7915 return 0;
de520661 7916}
400fbf9f 7917
3e4093b6
RS
7918/* If there are range designators in designator list, push a new designator
7919 to constructor_range_stack. RANGE_END is end of such stack range or
7920 NULL_TREE if there is no range designator at this level. */
400fbf9f 7921
3e4093b6 7922static void
a1e3b3d9 7923push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7924{
7925 struct constructor_range_stack *p;
400fbf9f 7926
a1e3b3d9
LB
7927 p = (struct constructor_range_stack *)
7928 obstack_alloc (braced_init_obstack,
7929 sizeof (struct constructor_range_stack));
3e4093b6
RS
7930 p->prev = constructor_range_stack;
7931 p->next = 0;
7932 p->fields = constructor_fields;
7933 p->range_start = constructor_index;
7934 p->index = constructor_index;
7935 p->stack = constructor_stack;
7936 p->range_end = range_end;
8b6a5902 7937 if (constructor_range_stack)
3e4093b6
RS
7938 constructor_range_stack->next = p;
7939 constructor_range_stack = p;
de520661 7940}
400fbf9f 7941
3e4093b6
RS
7942/* Within an array initializer, specify the next index to be initialized.
7943 FIRST is that index. If LAST is nonzero, then initialize a range
7944 of indices, running from FIRST through LAST. */
5a7ec9d9 7945
de520661 7946void
ea58ef42 7947set_init_index (location_t loc, tree first, tree last,
d033409e 7948 struct obstack *braced_init_obstack)
de520661 7949{
ea58ef42 7950 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7951 return;
de520661 7952
b06df647 7953 designator_erroneous = 1;
de520661 7954
3ea8cd06
JM
7955 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7956 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7957 {
ea58ef42 7958 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7959 return;
7960 }
7961
2b6da65c
JM
7962 if (TREE_CODE (first) != INTEGER_CST)
7963 {
7964 first = c_fully_fold (first, false, NULL);
7965 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7966 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7967 "array index in initializer is not "
7968 "an integer constant expression");
7969 }
7970
7971 if (last && TREE_CODE (last) != INTEGER_CST)
7972 {
7973 last = c_fully_fold (last, false, NULL);
7974 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7975 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7976 "array index in initializer is not "
7977 "an integer constant expression");
7978 }
7979
3e4093b6 7980 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7981 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7982 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7983 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7984 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7985 error_init (loc, "array index in non-array initializer");
622adc7e 7986 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7987 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7988 else if (constructor_max_index
7989 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7990 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7991 else
de520661 7992 {
928c19bb
JM
7993 constant_expression_warning (first);
7994 if (last)
7995 constant_expression_warning (last);
3e4093b6 7996 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7997 if (tree_int_cst_lt (constructor_index, first))
7998 {
7999 constructor_index = copy_node (constructor_index);
8000 TREE_OVERFLOW (constructor_index) = 1;
8001 }
665f2503 8002
3e4093b6 8003 if (last)
2bede729 8004 {
3e4093b6
RS
8005 if (tree_int_cst_equal (first, last))
8006 last = 0;
8007 else if (tree_int_cst_lt (last, first))
8008 {
ea58ef42 8009 error_init (loc, "empty index range in initializer");
3e4093b6
RS
8010 last = 0;
8011 }
8012 else
8013 {
8014 last = convert (bitsizetype, last);
8015 if (constructor_max_index != 0
8016 && tree_int_cst_lt (constructor_max_index, last))
8017 {
ea58ef42
MP
8018 error_init (loc, "array index range in initializer exceeds "
8019 "array bounds");
3e4093b6
RS
8020 last = 0;
8021 }
8022 }
2bede729 8023 }
fed3cef0 8024
3e4093b6 8025 designator_depth++;
b06df647 8026 designator_erroneous = 0;
3e4093b6 8027 if (constructor_range_stack || last)
a1e3b3d9 8028 push_range_stack (last, braced_init_obstack);
de520661 8029 }
de520661 8030}
3e4093b6
RS
8031
8032/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8033
de520661 8034void
ea58ef42
MP
8035set_init_label (location_t loc, tree fieldname,
8036 struct obstack *braced_init_obstack)
de520661 8037{
0fb96aa4 8038 tree field;
94ba5069 8039
ea58ef42 8040 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
8041 return;
8042
b06df647 8043 designator_erroneous = 1;
3e4093b6 8044
3e636daf 8045 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8046 {
ea58ef42 8047 error_init (loc, "field name not in record or union initializer");
3e4093b6 8048 return;
94ba5069
RS
8049 }
8050
0fb96aa4 8051 field = lookup_field (constructor_type, fieldname);
8b6a5902 8052
0fb96aa4 8053 if (field == 0)
9babc352 8054 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 8055 else
0fb96aa4
JM
8056 do
8057 {
8058 constructor_fields = TREE_VALUE (field);
8059 designator_depth++;
8060 designator_erroneous = 0;
8061 if (constructor_range_stack)
8062 push_range_stack (NULL_TREE, braced_init_obstack);
8063 field = TREE_CHAIN (field);
8064 if (field)
8065 {
ea58ef42 8066 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
8067 return;
8068 }
8069 }
8070 while (field != NULL_TREE);
3e4093b6
RS
8071}
8072\f
8073/* Add a new initializer to the tree of pending initializers. PURPOSE
8074 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8075 VALUE is the value of that index or field. If ORIGTYPE is not
8076 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8077
8078 IMPLICIT is true if value comes from pop_init_level (1),
8079 the new initializer has been merged with the existing one
8080 and thus no warnings should be emitted about overriding an
8081 existing initializer. */
de520661 8082
3e4093b6 8083static void
96b40f8d
MP
8084add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8085 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8086{
8087 struct init_node *p, **q, *r;
8088
8089 q = &constructor_pending_elts;
8090 p = 0;
8091
8092 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8093 {
3e4093b6 8094 while (*q != 0)
91fa3c30 8095 {
3e4093b6
RS
8096 p = *q;
8097 if (tree_int_cst_lt (purpose, p->purpose))
8098 q = &p->left;
8099 else if (tree_int_cst_lt (p->purpose, purpose))
8100 q = &p->right;
8101 else
8102 {
b295aee2
JJ
8103 if (!implicit)
8104 {
8105 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8106 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8107 "initialized field with side-effects "
8108 "overwritten");
b295aee2 8109 else if (warn_override_init)
96b40f8d
MP
8110 warning_init (loc, OPT_Woverride_init,
8111 "initialized field overwritten");
b295aee2 8112 }
3e4093b6 8113 p->value = value;
bbbbb16a 8114 p->origtype = origtype;
3e4093b6
RS
8115 return;
8116 }
91fa3c30 8117 }
de520661 8118 }
3e4093b6 8119 else
de520661 8120 {
3e4093b6 8121 tree bitpos;
400fbf9f 8122
3e4093b6
RS
8123 bitpos = bit_position (purpose);
8124 while (*q != NULL)
8125 {
8126 p = *q;
8127 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8128 q = &p->left;
8129 else if (p->purpose != purpose)
8130 q = &p->right;
8131 else
8132 {
b295aee2
JJ
8133 if (!implicit)
8134 {
8135 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8136 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8137 "initialized field with side-effects "
8138 "overwritten");
b295aee2 8139 else if (warn_override_init)
96b40f8d
MP
8140 warning_init (loc, OPT_Woverride_init,
8141 "initialized field overwritten");
b295aee2 8142 }
3e4093b6 8143 p->value = value;
bbbbb16a 8144 p->origtype = origtype;
3e4093b6
RS
8145 return;
8146 }
8147 }
91fa3c30 8148 }
b71c7f8a 8149
a1e3b3d9
LB
8150 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8151 sizeof (struct init_node));
3e4093b6
RS
8152 r->purpose = purpose;
8153 r->value = value;
bbbbb16a 8154 r->origtype = origtype;
8b6a5902 8155
3e4093b6
RS
8156 *q = r;
8157 r->parent = p;
8158 r->left = 0;
8159 r->right = 0;
8160 r->balance = 0;
b71c7f8a 8161
3e4093b6 8162 while (p)
de520661 8163 {
3e4093b6 8164 struct init_node *s;
665f2503 8165
3e4093b6 8166 if (r == p->left)
2bede729 8167 {
3e4093b6
RS
8168 if (p->balance == 0)
8169 p->balance = -1;
8170 else if (p->balance < 0)
8171 {
8172 if (r->balance < 0)
8173 {
8174 /* L rotation. */
8175 p->left = r->right;
8176 if (p->left)
8177 p->left->parent = p;
8178 r->right = p;
e7b6a0ee 8179
3e4093b6
RS
8180 p->balance = 0;
8181 r->balance = 0;
39bc99c2 8182
3e4093b6
RS
8183 s = p->parent;
8184 p->parent = r;
8185 r->parent = s;
8186 if (s)
8187 {
8188 if (s->left == p)
8189 s->left = r;
8190 else
8191 s->right = r;
8192 }
8193 else
8194 constructor_pending_elts = r;
8195 }
8196 else
8197 {
8198 /* LR rotation. */
8199 struct init_node *t = r->right;
e7b6a0ee 8200
3e4093b6
RS
8201 r->right = t->left;
8202 if (r->right)
8203 r->right->parent = r;
8204 t->left = r;
8205
8206 p->left = t->right;
8207 if (p->left)
8208 p->left->parent = p;
8209 t->right = p;
8210
8211 p->balance = t->balance < 0;
8212 r->balance = -(t->balance > 0);
8213 t->balance = 0;
8214
8215 s = p->parent;
8216 p->parent = t;
8217 r->parent = t;
8218 t->parent = s;
8219 if (s)
8220 {
8221 if (s->left == p)
8222 s->left = t;
8223 else
8224 s->right = t;
8225 }
8226 else
8227 constructor_pending_elts = t;
8228 }
8229 break;
8230 }
8231 else
8232 {
8233 /* p->balance == +1; growth of left side balances the node. */
8234 p->balance = 0;
8235 break;
8236 }
2bede729 8237 }
3e4093b6
RS
8238 else /* r == p->right */
8239 {
8240 if (p->balance == 0)
8241 /* Growth propagation from right side. */
8242 p->balance++;
8243 else if (p->balance > 0)
8244 {
8245 if (r->balance > 0)
8246 {
8247 /* R rotation. */
8248 p->right = r->left;
8249 if (p->right)
8250 p->right->parent = p;
8251 r->left = p;
8252
8253 p->balance = 0;
8254 r->balance = 0;
8255
8256 s = p->parent;
8257 p->parent = r;
8258 r->parent = s;
8259 if (s)
8260 {
8261 if (s->left == p)
8262 s->left = r;
8263 else
8264 s->right = r;
8265 }
8266 else
8267 constructor_pending_elts = r;
8268 }
8269 else /* r->balance == -1 */
8270 {
8271 /* RL rotation */
8272 struct init_node *t = r->left;
8273
8274 r->left = t->right;
8275 if (r->left)
8276 r->left->parent = r;
8277 t->right = r;
8278
8279 p->right = t->left;
8280 if (p->right)
8281 p->right->parent = p;
8282 t->left = p;
8283
8284 r->balance = (t->balance < 0);
8285 p->balance = -(t->balance > 0);
8286 t->balance = 0;
8287
8288 s = p->parent;
8289 p->parent = t;
8290 r->parent = t;
8291 t->parent = s;
8292 if (s)
8293 {
8294 if (s->left == p)
8295 s->left = t;
8296 else
8297 s->right = t;
8298 }
8299 else
8300 constructor_pending_elts = t;
8301 }
8302 break;
8303 }
8304 else
8305 {
8306 /* p->balance == -1; growth of right side balances the node. */
8307 p->balance = 0;
8308 break;
8309 }
8310 }
8311
8312 r = p;
8313 p = p->parent;
8314 }
8315}
8316
8317/* Build AVL tree from a sorted chain. */
8318
8319static void
a1e3b3d9 8320set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8321{
4038c495
GB
8322 unsigned HOST_WIDE_INT ix;
8323 tree index, value;
3e4093b6
RS
8324
8325 if (TREE_CODE (constructor_type) != RECORD_TYPE
8326 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8327 return;
8328
4038c495 8329 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8330 add_pending_init (input_location, index, value, NULL_TREE, true,
8331 braced_init_obstack);
9771b263 8332 constructor_elements = NULL;
3e4093b6
RS
8333 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8334 {
8335 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8336 /* Skip any nameless bit fields at the beginning. */
8337 while (constructor_unfilled_fields != 0
8338 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8339 && DECL_NAME (constructor_unfilled_fields) == 0)
8340 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8341
de520661 8342 }
3e4093b6 8343 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8344 {
3e4093b6
RS
8345 if (TYPE_DOMAIN (constructor_type))
8346 constructor_unfilled_index
8347 = convert (bitsizetype,
8348 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8349 else
8350 constructor_unfilled_index = bitsize_zero_node;
de520661 8351 }
3e4093b6 8352 constructor_incremental = 0;
de520661 8353}
400fbf9f 8354
3e4093b6 8355/* Build AVL tree from a string constant. */
de520661 8356
3e4093b6 8357static void
a1e3b3d9
LB
8358set_nonincremental_init_from_string (tree str,
8359 struct obstack * braced_init_obstack)
de520661 8360{
3e4093b6
RS
8361 tree value, purpose, type;
8362 HOST_WIDE_INT val[2];
8363 const char *p, *end;
8364 int byte, wchar_bytes, charwidth, bitpos;
de520661 8365
366de0ce 8366 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8367
c466b2cd 8368 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8369 charwidth = TYPE_PRECISION (char_type_node);
8370 type = TREE_TYPE (constructor_type);
8371 p = TREE_STRING_POINTER (str);
8372 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8373
3e4093b6 8374 for (purpose = bitsize_zero_node;
8824edff
JJ
8375 p < end
8376 && !(constructor_max_index
8377 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8378 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8379 {
3e4093b6 8380 if (wchar_bytes == 1)
ffc5c6a9 8381 {
807e902e
KZ
8382 val[0] = (unsigned char) *p++;
8383 val[1] = 0;
ffc5c6a9
RH
8384 }
8385 else
3e4093b6 8386 {
3e4093b6 8387 val[1] = 0;
807e902e 8388 val[0] = 0;
3e4093b6
RS
8389 for (byte = 0; byte < wchar_bytes; byte++)
8390 {
8391 if (BYTES_BIG_ENDIAN)
8392 bitpos = (wchar_bytes - byte - 1) * charwidth;
8393 else
8394 bitpos = byte * charwidth;
807e902e 8395 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8396 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8397 << (bitpos % HOST_BITS_PER_WIDE_INT);
8398 }
8399 }
584ef5fe 8400
8df83eae 8401 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8402 {
8403 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8404 if (bitpos < HOST_BITS_PER_WIDE_INT)
8405 {
807e902e 8406 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8407 {
aa256c4a 8408 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 8409 val[1] = -1;
3e4093b6
RS
8410 }
8411 }
8412 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8413 {
807e902e
KZ
8414 if (val[0] < 0)
8415 val[1] = -1;
3e4093b6 8416 }
807e902e 8417 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8418 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 8419 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 8420 }
ffc5c6a9 8421
807e902e
KZ
8422 value = wide_int_to_tree (type,
8423 wide_int::from_array (val, 2,
8424 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8425 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8426 braced_init_obstack);
9dfcc8db
BH
8427 }
8428
3e4093b6
RS
8429 constructor_incremental = 0;
8430}
de520661 8431
3e4093b6
RS
8432/* Return value of FIELD in pending initializer or zero if the field was
8433 not initialized yet. */
8434
8435static tree
a1e3b3d9 8436find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8437{
8438 struct init_node *p;
8439
8440 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8441 {
3e4093b6
RS
8442 if (constructor_incremental
8443 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8444 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8445
8446 p = constructor_pending_elts;
8447 while (p)
19d76e60 8448 {
3e4093b6
RS
8449 if (tree_int_cst_lt (field, p->purpose))
8450 p = p->left;
8451 else if (tree_int_cst_lt (p->purpose, field))
8452 p = p->right;
8453 else
8454 return p->value;
19d76e60 8455 }
19d76e60 8456 }
3e4093b6 8457 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8458 {
3e4093b6 8459 tree bitpos = bit_position (field);
de520661 8460
3e4093b6
RS
8461 if (constructor_incremental
8462 && (!constructor_unfilled_fields
8463 || tree_int_cst_lt (bitpos,
8464 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8465 set_nonincremental_init (braced_init_obstack);
de520661 8466
3e4093b6
RS
8467 p = constructor_pending_elts;
8468 while (p)
8469 {
8470 if (field == p->purpose)
8471 return p->value;
8472 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8473 p = p->left;
8474 else
8475 p = p->right;
8476 }
8477 }
8478 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8479 {
9771b263
DN
8480 if (!vec_safe_is_empty (constructor_elements)
8481 && (constructor_elements->last ().index == field))
8482 return constructor_elements->last ().value;
de520661 8483 }
3e4093b6 8484 return 0;
de520661
RS
8485}
8486
3e4093b6
RS
8487/* "Output" the next constructor element.
8488 At top level, really output it to assembler code now.
8489 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8490 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8491 TYPE is the data type that the containing data type wants here.
8492 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8493 If VALUE is a string constant, STRICT_STRING is true if it is
8494 unparenthesized or we should not warn here for it being parenthesized.
8495 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8496
3e4093b6
RS
8497 PENDING if non-nil means output pending elements that belong
8498 right after this element. (PENDING is normally 1;
b295aee2
JJ
8499 it is 0 while outputting pending elements, to avoid recursion.)
8500
8501 IMPLICIT is true if value comes from pop_init_level (1),
8502 the new initializer has been merged with the existing one
8503 and thus no warnings should be emitted about overriding an
8504 existing initializer. */
8b6a5902 8505
3e4093b6 8506static void
34cf811f
MP
8507output_init_element (location_t loc, tree value, tree origtype,
8508 bool strict_string, tree type, tree field, int pending,
8509 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8510{
8ce94e44 8511 tree semantic_type = NULL_TREE;
928c19bb
JM
8512 bool maybe_const = true;
8513 bool npc;
4038c495 8514
0a880880 8515 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8516 {
3e4093b6
RS
8517 constructor_erroneous = 1;
8518 return;
8b6a5902 8519 }
46bdb9cf
JM
8520 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8521 && (TREE_CODE (value) == STRING_CST
8522 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8523 && !(TREE_CODE (value) == STRING_CST
8524 && TREE_CODE (type) == ARRAY_TYPE
8525 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8526 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8527 TYPE_MAIN_VARIANT (type)))
c2255bc4 8528 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8529
3e4093b6 8530 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8531 && require_constant_value && pending)
8b6a5902 8532 {
3e4093b6
RS
8533 /* As an extension, allow initializing objects with static storage
8534 duration with compound literals (which are then treated just as
8535 the brace enclosed list they contain). */
4435bb92
MP
8536 if (flag_isoc99)
8537 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8538 "constant");
3e4093b6
RS
8539 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8540 value = DECL_INITIAL (decl);
8b6a5902
JJ
8541 }
8542
928c19bb 8543 npc = null_pointer_constant_p (value);
8ce94e44
JM
8544 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8545 {
8546 semantic_type = TREE_TYPE (value);
8547 value = TREE_OPERAND (value, 0);
8548 }
928c19bb
JM
8549 value = c_fully_fold (value, require_constant_value, &maybe_const);
8550
3e4093b6
RS
8551 if (value == error_mark_node)
8552 constructor_erroneous = 1;
8553 else if (!TREE_CONSTANT (value))
8554 constructor_constant = 0;
ee45a32d
EB
8555 else if (!initializer_constant_valid_p (value,
8556 TREE_TYPE (value),
8557 AGGREGATE_TYPE_P (constructor_type)
8558 && TYPE_REVERSE_STORAGE_ORDER
8559 (constructor_type))
3e636daf 8560 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
8561 && DECL_C_BIT_FIELD (field)
8562 && TREE_CODE (value) != INTEGER_CST))
8563 constructor_simple = 0;
928c19bb
JM
8564 if (!maybe_const)
8565 constructor_nonconst = 1;
3e4093b6 8566
116df786 8567 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8568 {
116df786
RH
8569 if (require_constant_value)
8570 {
ea58ef42 8571 error_init (loc, "initializer element is not constant");
116df786
RH
8572 value = error_mark_node;
8573 }
8574 else if (require_constant_elements)
8337d1db 8575 pedwarn (loc, OPT_Wpedantic,
509c9d60 8576 "initializer element is not computable at load time");
8b6a5902 8577 }
928c19bb
JM
8578 else if (!maybe_const
8579 && (require_constant_value || require_constant_elements))
8337d1db 8580 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8581 "initializer element is not a constant expression");
3e4093b6 8582
81f40b79
ILT
8583 /* Issue -Wc++-compat warnings about initializing a bitfield with
8584 enum type. */
8585 if (warn_cxx_compat
8586 && field != NULL_TREE
8587 && TREE_CODE (field) == FIELD_DECL
8588 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8589 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8590 != TYPE_MAIN_VARIANT (type))
8591 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8592 {
8593 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8594 if (checktype != error_mark_node
8595 && (TYPE_MAIN_VARIANT (checktype)
8596 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8597 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8598 "enum conversion in initialization is invalid in C++");
8599 }
8600
3e4093b6
RS
8601 /* If this field is empty (and not at the end of structure),
8602 don't do anything other than checking the initializer. */
8603 if (field
8604 && (TREE_TYPE (field) == error_mark_node
8605 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8606 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8607 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8608 || DECL_CHAIN (field)))))
3e4093b6
RS
8609 return;
8610
8ce94e44
JM
8611 if (semantic_type)
8612 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8613 value = digest_init (loc, type, value, origtype, npc, strict_string,
8614 require_constant_value);
3e4093b6 8615 if (value == error_mark_node)
8b6a5902 8616 {
3e4093b6
RS
8617 constructor_erroneous = 1;
8618 return;
8b6a5902 8619 }
928c19bb
JM
8620 if (require_constant_value || require_constant_elements)
8621 constant_expression_warning (value);
8b6a5902 8622
3e4093b6
RS
8623 /* If this element doesn't come next in sequence,
8624 put it on constructor_pending_elts. */
8625 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8626 && (!constructor_incremental
8627 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8628 {
3e4093b6
RS
8629 if (constructor_incremental
8630 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8631 set_nonincremental_init (braced_init_obstack);
3e4093b6 8632
96b40f8d 8633 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8634 braced_init_obstack);
3e4093b6 8635 return;
8b6a5902 8636 }
3e4093b6
RS
8637 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8638 && (!constructor_incremental
8639 || field != constructor_unfilled_fields))
8b6a5902 8640 {
3e4093b6
RS
8641 /* We do this for records but not for unions. In a union,
8642 no matter which field is specified, it can be initialized
8643 right away since it starts at the beginning of the union. */
8644 if (constructor_incremental)
8645 {
8646 if (!constructor_unfilled_fields)
a1e3b3d9 8647 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8648 else
8649 {
8650 tree bitpos, unfillpos;
8651
8652 bitpos = bit_position (field);
8653 unfillpos = bit_position (constructor_unfilled_fields);
8654
8655 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8656 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8657 }
8658 }
8659
96b40f8d 8660 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8661 braced_init_obstack);
3e4093b6 8662 return;
8b6a5902 8663 }
3e4093b6 8664 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8665 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8666 {
b295aee2
JJ
8667 if (!implicit)
8668 {
9771b263 8669 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8670 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8671 "initialized field with side-effects overwritten");
8672 else if (warn_override_init)
96b40f8d
MP
8673 warning_init (loc, OPT_Woverride_init,
8674 "initialized field overwritten");
b295aee2 8675 }
8b6a5902 8676
3e4093b6 8677 /* We can have just one union field set. */
9771b263 8678 constructor_elements = NULL;
3e4093b6 8679 }
8b6a5902 8680
3e4093b6
RS
8681 /* Otherwise, output this element either to
8682 constructor_elements or to the assembler file. */
8b6a5902 8683
f32682ca 8684 constructor_elt celt = {field, value};
9771b263 8685 vec_safe_push (constructor_elements, celt);
8b6a5902 8686
3e4093b6
RS
8687 /* Advance the variable that indicates sequential elements output. */
8688 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8689 constructor_unfilled_index
db3927fb
AH
8690 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8691 bitsize_one_node);
3e4093b6
RS
8692 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8693 {
8694 constructor_unfilled_fields
910ad8de 8695 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8696
3e4093b6
RS
8697 /* Skip any nameless bit fields. */
8698 while (constructor_unfilled_fields != 0
8699 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8700 && DECL_NAME (constructor_unfilled_fields) == 0)
8701 constructor_unfilled_fields =
910ad8de 8702 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8703 }
8704 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8705 constructor_unfilled_fields = 0;
de520661 8706
3e4093b6
RS
8707 /* Now output any pending elements which have become next. */
8708 if (pending)
a1e3b3d9 8709 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8710}
8b6a5902 8711
3e4093b6
RS
8712/* Output any pending elements which have become next.
8713 As we output elements, constructor_unfilled_{fields,index}
8714 advances, which may cause other elements to become next;
8715 if so, they too are output.
8b6a5902 8716
3e4093b6
RS
8717 If ALL is 0, we return when there are
8718 no more pending elements to output now.
665f2503 8719
3e4093b6
RS
8720 If ALL is 1, we output space as necessary so that
8721 we can output all the pending elements. */
3e4093b6 8722static void
a1e3b3d9 8723output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8724{
8725 struct init_node *elt = constructor_pending_elts;
8726 tree next;
de520661 8727
3e4093b6
RS
8728 retry:
8729
ba228239 8730 /* Look through the whole pending tree.
3e4093b6
RS
8731 If we find an element that should be output now,
8732 output it. Otherwise, set NEXT to the element
8733 that comes first among those still pending. */
8734
8735 next = 0;
8736 while (elt)
8737 {
8738 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8739 {
3e4093b6
RS
8740 if (tree_int_cst_equal (elt->purpose,
8741 constructor_unfilled_index))
34cf811f
MP
8742 output_init_element (input_location, elt->value, elt->origtype,
8743 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8744 constructor_unfilled_index, 0, false,
8745 braced_init_obstack);
3e4093b6
RS
8746 else if (tree_int_cst_lt (constructor_unfilled_index,
8747 elt->purpose))
8b6a5902 8748 {
3e4093b6
RS
8749 /* Advance to the next smaller node. */
8750 if (elt->left)
8751 elt = elt->left;
8752 else
8753 {
8754 /* We have reached the smallest node bigger than the
8755 current unfilled index. Fill the space first. */
8756 next = elt->purpose;
8757 break;
8758 }
8b6a5902 8759 }
ce662d4c
JJ
8760 else
8761 {
3e4093b6
RS
8762 /* Advance to the next bigger node. */
8763 if (elt->right)
8764 elt = elt->right;
8765 else
ce662d4c 8766 {
3e4093b6
RS
8767 /* We have reached the biggest node in a subtree. Find
8768 the parent of it, which is the next bigger node. */
8769 while (elt->parent && elt->parent->right == elt)
8770 elt = elt->parent;
8771 elt = elt->parent;
8772 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8773 elt->purpose))
8774 {
8775 next = elt->purpose;
8776 break;
8777 }
ce662d4c
JJ
8778 }
8779 }
8b6a5902 8780 }
3e636daf 8781 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8782 {
8783 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8784
3e4093b6
RS
8785 /* If the current record is complete we are done. */
8786 if (constructor_unfilled_fields == 0)
8787 break;
de520661 8788
3e4093b6
RS
8789 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8790 elt_bitpos = bit_position (elt->purpose);
8791 /* We can't compare fields here because there might be empty
8792 fields in between. */
8793 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8794 {
8795 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8796 output_init_element (input_location, elt->value, elt->origtype,
8797 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8798 elt->purpose, 0, false,
8799 braced_init_obstack);
3e4093b6
RS
8800 }
8801 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8802 {
8803 /* Advance to the next smaller node. */
8804 if (elt->left)
8805 elt = elt->left;
8806 else
8807 {
8808 /* We have reached the smallest node bigger than the
8809 current unfilled field. Fill the space first. */
8810 next = elt->purpose;
8811 break;
8812 }
8813 }
8814 else
8815 {
8816 /* Advance to the next bigger node. */
8817 if (elt->right)
8818 elt = elt->right;
8819 else
8820 {
8821 /* We have reached the biggest node in a subtree. Find
8822 the parent of it, which is the next bigger node. */
8823 while (elt->parent && elt->parent->right == elt)
8824 elt = elt->parent;
8825 elt = elt->parent;
8826 if (elt
8827 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8828 bit_position (elt->purpose))))
8829 {
8830 next = elt->purpose;
8831 break;
8832 }
8833 }
8834 }
8835 }
8836 }
de520661 8837
3e4093b6
RS
8838 /* Ordinarily return, but not if we want to output all
8839 and there are elements left. */
3f75a254 8840 if (!(all && next != 0))
e5cfb88f
RK
8841 return;
8842
3e4093b6
RS
8843 /* If it's not incremental, just skip over the gap, so that after
8844 jumping to retry we will output the next successive element. */
3e636daf 8845 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8846 constructor_unfilled_fields = next;
8847 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8848 constructor_unfilled_index = next;
de520661 8849
3e4093b6
RS
8850 /* ELT now points to the node in the pending tree with the next
8851 initializer to output. */
8852 goto retry;
de520661
RS
8853}
8854\f
3e4093b6
RS
8855/* Add one non-braced element to the current constructor level.
8856 This adjusts the current position within the constructor's type.
8857 This may also start or terminate implicit levels
8858 to handle a partly-braced initializer.
e5e809f4 8859
3e4093b6 8860 Once this has found the correct level for the new element,
b295aee2
JJ
8861 it calls output_init_element.
8862
8863 IMPLICIT is true if value comes from pop_init_level (1),
8864 the new initializer has been merged with the existing one
8865 and thus no warnings should be emitted about overriding an
8866 existing initializer. */
3e4093b6
RS
8867
8868void
34cf811f 8869process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8870 struct obstack * braced_init_obstack)
e5e809f4 8871{
916c5919
JM
8872 tree orig_value = value.value;
8873 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8874 bool strict_string = value.original_code == STRING_CST;
340baef7 8875 bool was_designated = designator_depth != 0;
e5e809f4 8876
3e4093b6 8877 designator_depth = 0;
b06df647 8878 designator_erroneous = 0;
e5e809f4 8879
9bac5cbb
G
8880 if (!implicit && value.value && !integer_zerop (value.value))
8881 constructor_zeroinit = 0;
8882
3e4093b6
RS
8883 /* Handle superfluous braces around string cst as in
8884 char x[] = {"foo"}; */
8885 if (string_flag
8886 && constructor_type
340baef7 8887 && !was_designated
3e4093b6 8888 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8889 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8890 && integer_zerop (constructor_unfilled_index))
e5e809f4 8891 {
916c5919 8892 if (constructor_stack->replacement_value.value)
ea58ef42 8893 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8894 constructor_stack->replacement_value = value;
8895 return;
e5e809f4 8896 }
8b6a5902 8897
916c5919 8898 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8899 {
ea58ef42 8900 error_init (loc, "excess elements in struct initializer");
3e4093b6 8901 return;
e5e809f4
JL
8902 }
8903
3e4093b6
RS
8904 /* Ignore elements of a brace group if it is entirely superfluous
8905 and has already been diagnosed. */
8906 if (constructor_type == 0)
8907 return;
e5e809f4 8908
976d5a22
TT
8909 if (!implicit && warn_designated_init && !was_designated
8910 && TREE_CODE (constructor_type) == RECORD_TYPE
8911 && lookup_attribute ("designated_init",
8912 TYPE_ATTRIBUTES (constructor_type)))
8913 warning_init (loc,
8914 OPT_Wdesignated_init,
8915 "positional initialization of field "
8916 "in %<struct%> declared with %<designated_init%> attribute");
8917
3e4093b6
RS
8918 /* If we've exhausted any levels that didn't have braces,
8919 pop them now. */
8920 while (constructor_stack->implicit)
8921 {
3e636daf 8922 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8923 && constructor_fields == 0)
ea58ef42
MP
8924 process_init_element (loc,
8925 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8926 true, braced_init_obstack);
53650abe 8927 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 8928 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
8929 && constructor_max_index
8930 && tree_int_cst_lt (constructor_max_index,
8931 constructor_index))
ea58ef42
MP
8932 process_init_element (loc,
8933 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8934 true, braced_init_obstack);
3e4093b6
RS
8935 else
8936 break;
8937 }
e5e809f4 8938
3e4093b6
RS
8939 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8940 if (constructor_range_stack)
e5e809f4 8941 {
3e4093b6
RS
8942 /* If value is a compound literal and we'll be just using its
8943 content, don't put it into a SAVE_EXPR. */
916c5919 8944 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 8945 || !require_constant_value)
8ce94e44
JM
8946 {
8947 tree semantic_type = NULL_TREE;
8948 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8949 {
8950 semantic_type = TREE_TYPE (value.value);
8951 value.value = TREE_OPERAND (value.value, 0);
8952 }
8953 value.value = c_save_expr (value.value);
8954 if (semantic_type)
8955 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8956 value.value);
8957 }
3e4093b6 8958 }
e5e809f4 8959
3e4093b6
RS
8960 while (1)
8961 {
8962 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8963 {
3e4093b6
RS
8964 tree fieldtype;
8965 enum tree_code fieldcode;
e5e809f4 8966
3e4093b6
RS
8967 if (constructor_fields == 0)
8968 {
ea58ef42 8969 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8970 break;
8971 }
e5e809f4 8972
3e4093b6
RS
8973 fieldtype = TREE_TYPE (constructor_fields);
8974 if (fieldtype != error_mark_node)
8975 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8976 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8977
3e4093b6
RS
8978 /* Error for non-static initialization of a flexible array member. */
8979 if (fieldcode == ARRAY_TYPE
8980 && !require_constant_value
8981 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8982 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8983 {
ea58ef42
MP
8984 error_init (loc, "non-static initialization of a flexible "
8985 "array member");
3e4093b6
RS
8986 break;
8987 }
e5e809f4 8988
2cc901dc
MP
8989 /* Error for initialization of a flexible array member with
8990 a string constant if the structure is in an array. E.g.:
8991 struct S { int x; char y[]; };
8992 struct S s[] = { { 1, "foo" } };
8993 is invalid. */
8994 if (string_flag
8995 && fieldcode == ARRAY_TYPE
8996 && constructor_depth > 1
8997 && TYPE_SIZE (fieldtype) == NULL_TREE
8998 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8999 {
9000 bool in_array_p = false;
9001 for (struct constructor_stack *p = constructor_stack;
9002 p && p->type; p = p->next)
9003 if (TREE_CODE (p->type) == ARRAY_TYPE)
9004 {
9005 in_array_p = true;
9006 break;
9007 }
9008 if (in_array_p)
9009 {
9010 error_init (loc, "initialization of flexible array "
9011 "member in a nested context");
9012 break;
9013 }
9014 }
9015
3e4093b6 9016 /* Accept a string constant to initialize a subarray. */
916c5919 9017 if (value.value != 0
3e4093b6 9018 && fieldcode == ARRAY_TYPE
197463ae 9019 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9020 && string_flag)
916c5919 9021 value.value = orig_value;
3e4093b6
RS
9022 /* Otherwise, if we have come to a subaggregate,
9023 and we don't have an element of its type, push into it. */
0953878d 9024 else if (value.value != 0
916c5919
JM
9025 && value.value != error_mark_node
9026 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9027 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9028 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9029 {
ea58ef42 9030 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9031 continue;
9032 }
e5e809f4 9033
916c5919 9034 if (value.value)
3e4093b6
RS
9035 {
9036 push_member_name (constructor_fields);
34cf811f 9037 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9038 strict_string, fieldtype,
a1e3b3d9
LB
9039 constructor_fields, 1, implicit,
9040 braced_init_obstack);
3e4093b6 9041 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9042 }
9043 else
3e4093b6
RS
9044 /* Do the bookkeeping for an element that was
9045 directly output as a constructor. */
e5e809f4 9046 {
3e4093b6
RS
9047 /* For a record, keep track of end position of last field. */
9048 if (DECL_SIZE (constructor_fields))
c22cacf3 9049 constructor_bit_index
db3927fb
AH
9050 = size_binop_loc (input_location, PLUS_EXPR,
9051 bit_position (constructor_fields),
9052 DECL_SIZE (constructor_fields));
3e4093b6
RS
9053
9054 /* If the current field was the first one not yet written out,
9055 it isn't now, so update. */
9056 if (constructor_unfilled_fields == constructor_fields)
9057 {
910ad8de 9058 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9059 /* Skip any nameless bit fields. */
9060 while (constructor_unfilled_fields != 0
9061 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9062 && DECL_NAME (constructor_unfilled_fields) == 0)
9063 constructor_unfilled_fields =
910ad8de 9064 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9065 }
e5e809f4 9066 }
3e4093b6 9067
910ad8de 9068 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9069 /* Skip any nameless bit fields at the beginning. */
9070 while (constructor_fields != 0
9071 && DECL_C_BIT_FIELD (constructor_fields)
9072 && DECL_NAME (constructor_fields) == 0)
910ad8de 9073 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9074 }
3e4093b6 9075 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9076 {
3e4093b6
RS
9077 tree fieldtype;
9078 enum tree_code fieldcode;
e5e809f4 9079
3e4093b6
RS
9080 if (constructor_fields == 0)
9081 {
d033409e 9082 pedwarn_init (loc, 0,
509c9d60 9083 "excess elements in union initializer");
3e4093b6
RS
9084 break;
9085 }
e5e809f4 9086
3e4093b6
RS
9087 fieldtype = TREE_TYPE (constructor_fields);
9088 if (fieldtype != error_mark_node)
9089 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9090 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9091
3e4093b6
RS
9092 /* Warn that traditional C rejects initialization of unions.
9093 We skip the warning if the value is zero. This is done
9094 under the assumption that the zero initializer in user
9095 code appears conditioned on e.g. __STDC__ to avoid
9096 "missing initializer" warnings and relies on default
9097 initialization to zero in the traditional C case.
9098 We also skip the warning if the initializer is designated,
9099 again on the assumption that this must be conditional on
9100 __STDC__ anyway (and we've already complained about the
9101 member-designator already). */
8400e75e 9102 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9103 && !(value.value && (integer_zerop (value.value)
9104 || real_zerop (value.value))))
3176a0c2
DD
9105 warning (OPT_Wtraditional, "traditional C rejects initialization "
9106 "of unions");
e5e809f4 9107
3e4093b6 9108 /* Accept a string constant to initialize a subarray. */
916c5919 9109 if (value.value != 0
3e4093b6 9110 && fieldcode == ARRAY_TYPE
197463ae 9111 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9112 && string_flag)
916c5919 9113 value.value = orig_value;
3e4093b6
RS
9114 /* Otherwise, if we have come to a subaggregate,
9115 and we don't have an element of its type, push into it. */
0953878d 9116 else if (value.value != 0
916c5919
JM
9117 && value.value != error_mark_node
9118 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9119 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9120 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9121 {
ea58ef42 9122 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9123 continue;
9124 }
e5e809f4 9125
916c5919 9126 if (value.value)
3e4093b6
RS
9127 {
9128 push_member_name (constructor_fields);
34cf811f 9129 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9130 strict_string, fieldtype,
a1e3b3d9
LB
9131 constructor_fields, 1, implicit,
9132 braced_init_obstack);
3e4093b6 9133 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9134 }
9135 else
3e4093b6
RS
9136 /* Do the bookkeeping for an element that was
9137 directly output as a constructor. */
e5e809f4 9138 {
3e4093b6 9139 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9140 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9141 }
e5e809f4 9142
3e4093b6
RS
9143 constructor_fields = 0;
9144 }
9145 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9146 {
9147 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9148 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9149
3e4093b6 9150 /* Accept a string constant to initialize a subarray. */
916c5919 9151 if (value.value != 0
3e4093b6 9152 && eltcode == ARRAY_TYPE
197463ae 9153 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9154 && string_flag)
916c5919 9155 value.value = orig_value;
3e4093b6
RS
9156 /* Otherwise, if we have come to a subaggregate,
9157 and we don't have an element of its type, push into it. */
0953878d 9158 else if (value.value != 0
916c5919
JM
9159 && value.value != error_mark_node
9160 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9161 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9162 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9163 {
ea58ef42 9164 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9165 continue;
9166 }
8b6a5902 9167
3e4093b6
RS
9168 if (constructor_max_index != 0
9169 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9170 || integer_all_onesp (constructor_max_index)))
9171 {
d033409e 9172 pedwarn_init (loc, 0,
509c9d60 9173 "excess elements in array initializer");
3e4093b6
RS
9174 break;
9175 }
8b6a5902 9176
3e4093b6 9177 /* Now output the actual element. */
916c5919 9178 if (value.value)
3e4093b6 9179 {
ae7e9ddd 9180 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9181 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9182 strict_string, elttype,
a1e3b3d9
LB
9183 constructor_index, 1, implicit,
9184 braced_init_obstack);
3e4093b6
RS
9185 RESTORE_SPELLING_DEPTH (constructor_depth);
9186 }
2f6e4e97 9187
3e4093b6 9188 constructor_index
db3927fb
AH
9189 = size_binop_loc (input_location, PLUS_EXPR,
9190 constructor_index, bitsize_one_node);
8b6a5902 9191
916c5919 9192 if (!value.value)
3e4093b6
RS
9193 /* If we are doing the bookkeeping for an element that was
9194 directly output as a constructor, we must update
9195 constructor_unfilled_index. */
9196 constructor_unfilled_index = constructor_index;
9197 }
31521951 9198 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9199 {
9200 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9201
c22cacf3
MS
9202 /* Do a basic check of initializer size. Note that vectors
9203 always have a fixed size derived from their type. */
3e4093b6
RS
9204 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9205 {
d033409e 9206 pedwarn_init (loc, 0,
509c9d60 9207 "excess elements in vector initializer");
3e4093b6
RS
9208 break;
9209 }
8b6a5902 9210
3e4093b6 9211 /* Now output the actual element. */
916c5919 9212 if (value.value)
53650abe
AP
9213 {
9214 if (TREE_CODE (value.value) == VECTOR_CST)
9215 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9216 output_init_element (loc, value.value, value.original_type,
53650abe 9217 strict_string, elttype,
a1e3b3d9
LB
9218 constructor_index, 1, implicit,
9219 braced_init_obstack);
53650abe 9220 }
8b6a5902 9221
3e4093b6 9222 constructor_index
db3927fb
AH
9223 = size_binop_loc (input_location,
9224 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9225
916c5919 9226 if (!value.value)
3e4093b6
RS
9227 /* If we are doing the bookkeeping for an element that was
9228 directly output as a constructor, we must update
9229 constructor_unfilled_index. */
9230 constructor_unfilled_index = constructor_index;
9231 }
8b6a5902 9232
3e4093b6
RS
9233 /* Handle the sole element allowed in a braced initializer
9234 for a scalar variable. */
b4519d39
SB
9235 else if (constructor_type != error_mark_node
9236 && constructor_fields == 0)
8b6a5902 9237 {
d033409e 9238 pedwarn_init (loc, 0,
509c9d60 9239 "excess elements in scalar initializer");
3e4093b6 9240 break;
8b6a5902
JJ
9241 }
9242 else
9243 {
916c5919 9244 if (value.value)
34cf811f 9245 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9246 strict_string, constructor_type,
a1e3b3d9
LB
9247 NULL_TREE, 1, implicit,
9248 braced_init_obstack);
3e4093b6 9249 constructor_fields = 0;
8b6a5902
JJ
9250 }
9251
3e4093b6
RS
9252 /* Handle range initializers either at this level or anywhere higher
9253 in the designator stack. */
9254 if (constructor_range_stack)
8b6a5902 9255 {
3e4093b6
RS
9256 struct constructor_range_stack *p, *range_stack;
9257 int finish = 0;
9258
9259 range_stack = constructor_range_stack;
9260 constructor_range_stack = 0;
9261 while (constructor_stack != range_stack->stack)
8b6a5902 9262 {
366de0ce 9263 gcc_assert (constructor_stack->implicit);
34cf811f 9264 process_init_element (loc,
ea58ef42
MP
9265 pop_init_level (loc, 1,
9266 braced_init_obstack),
a1e3b3d9 9267 true, braced_init_obstack);
8b6a5902 9268 }
3e4093b6
RS
9269 for (p = range_stack;
9270 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9271 p = p->prev)
8b6a5902 9272 {
366de0ce 9273 gcc_assert (constructor_stack->implicit);
34cf811f 9274 process_init_element (loc,
ea58ef42
MP
9275 pop_init_level (loc, 1,
9276 braced_init_obstack),
a1e3b3d9 9277 true, braced_init_obstack);
8b6a5902 9278 }
3e4093b6 9279
db3927fb
AH
9280 p->index = size_binop_loc (input_location,
9281 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9282 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9283 finish = 1;
9284
9285 while (1)
9286 {
9287 constructor_index = p->index;
9288 constructor_fields = p->fields;
9289 if (finish && p->range_end && p->index == p->range_start)
9290 {
9291 finish = 0;
9292 p->prev = 0;
9293 }
9294 p = p->next;
9295 if (!p)
9296 break;
ea58ef42 9297 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9298 p->stack = constructor_stack;
9299 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9300 p->index = p->range_start;
9301 }
9302
9303 if (!finish)
9304 constructor_range_stack = range_stack;
9305 continue;
8b6a5902
JJ
9306 }
9307
3e4093b6 9308 break;
8b6a5902
JJ
9309 }
9310
3e4093b6
RS
9311 constructor_range_stack = 0;
9312}
9313\f
9f0e2d86
ZW
9314/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9315 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9316 an ASM_EXPR node). */
3e4093b6 9317tree
9f0e2d86 9318build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9319{
6de9cd9a
DN
9320 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9321 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9322 return add_stmt (args);
8b6a5902
JJ
9323}
9324
9f0e2d86
ZW
9325/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9326 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9327 SIMPLE indicates whether there was anything at all after the
9328 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9329 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9330tree
c2255bc4 9331build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9332 tree clobbers, tree labels, bool simple)
e5e809f4 9333{
3e4093b6 9334 tree tail;
9f0e2d86 9335 tree args;
6de9cd9a
DN
9336 int i;
9337 const char *constraint;
74f0c611 9338 const char **oconstraints;
6de9cd9a 9339 bool allows_mem, allows_reg, is_inout;
74f0c611 9340 int ninputs, noutputs;
6de9cd9a
DN
9341
9342 ninputs = list_length (inputs);
9343 noutputs = list_length (outputs);
74f0c611
RH
9344 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9345
1c384bf1 9346 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9347
6de9cd9a
DN
9348 /* Remove output conversions that change the type but not the mode. */
9349 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9350 {
3e4093b6 9351 tree output = TREE_VALUE (tail);
74f0c611 9352
eadd3d0d
JJ
9353 output = c_fully_fold (output, false, NULL);
9354
74f0c611
RH
9355 /* ??? Really, this should not be here. Users should be using a
9356 proper lvalue, dammit. But there's a long history of using casts
9357 in the output operands. In cases like longlong.h, this becomes a
9358 primitive form of typechecking -- if the cast can be removed, then
9359 the output operand had a type of the proper width; otherwise we'll
9360 get an error. Gross, but ... */
3e4093b6 9361 STRIP_NOPS (output);
74f0c611 9362
7bd11157 9363 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9364 output = error_mark_node;
8b6a5902 9365
5544530a
PB
9366 if (output != error_mark_node
9367 && (TREE_READONLY (output)
9368 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 9369 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 9370 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9371 readonly_error (loc, output, lv_asm);
5544530a 9372
6de9cd9a 9373 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9374 oconstraints[i] = constraint;
9375
9376 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9377 &allows_mem, &allows_reg, &is_inout))
9378 {
9379 /* If the operand is going to end up in memory,
9380 mark it addressable. */
9381 if (!allows_reg && !c_mark_addressable (output))
9382 output = error_mark_node;
bae5cddf
JJ
9383 if (!(!allows_reg && allows_mem)
9384 && output != error_mark_node
9385 && VOID_TYPE_P (TREE_TYPE (output)))
9386 {
9387 error_at (loc, "invalid use of void expression");
9388 output = error_mark_node;
9389 }
74f0c611
RH
9390 }
9391 else
c22cacf3 9392 output = error_mark_node;
3e4093b6 9393
74f0c611 9394 TREE_VALUE (tail) = output;
8b6a5902 9395 }
3e4093b6 9396
74f0c611
RH
9397 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9398 {
9399 tree input;
9400
9401 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9402 input = TREE_VALUE (tail);
9403
74f0c611
RH
9404 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9405 oconstraints, &allows_mem, &allows_reg))
9406 {
9407 /* If the operand is going to end up in memory,
9408 mark it addressable. */
b4c33883
AP
9409 if (!allows_reg && allows_mem)
9410 {
eadd3d0d
JJ
9411 input = c_fully_fold (input, false, NULL);
9412
b4c33883
AP
9413 /* Strip the nops as we allow this case. FIXME, this really
9414 should be rejected or made deprecated. */
9415 STRIP_NOPS (input);
9416 if (!c_mark_addressable (input))
9417 input = error_mark_node;
bae5cddf 9418 }
eadd3d0d 9419 else
bae5cddf 9420 {
eadd3d0d
JJ
9421 struct c_expr expr;
9422 memset (&expr, 0, sizeof (expr));
9423 expr.value = input;
267bac10 9424 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9425 input = c_fully_fold (expr.value, false, NULL);
9426
9427 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9428 {
9429 error_at (loc, "invalid use of void expression");
9430 input = error_mark_node;
9431 }
bae5cddf 9432 }
74f0c611
RH
9433 }
9434 else
9435 input = error_mark_node;
9436
9437 TREE_VALUE (tail) = input;
9438 }
3e4093b6 9439
1c384bf1
RH
9440 /* ASMs with labels cannot have outputs. This should have been
9441 enforced by the parser. */
9442 gcc_assert (outputs == NULL || labels == NULL);
9443
9444 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9445
5544530a
PB
9446 /* asm statements without outputs, including simple ones, are treated
9447 as volatile. */
9448 ASM_INPUT_P (args) = simple;
9449 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9450
9f0e2d86 9451 return args;
e5e809f4 9452}
3e4093b6 9453\f
c2255bc4
AH
9454/* Generate a goto statement to LABEL. LOC is the location of the
9455 GOTO. */
506e2710
RH
9456
9457tree
c2255bc4 9458c_finish_goto_label (location_t loc, tree label)
506e2710 9459{
e1b7793c 9460 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9461 if (!decl)
9462 return NULL_TREE;
506e2710 9463 TREE_USED (decl) = 1;
c2255bc4
AH
9464 {
9465 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9466 SET_EXPR_LOCATION (t, loc);
9467 return add_stmt (t);
9468 }
506e2710
RH
9469}
9470
c2255bc4
AH
9471/* Generate a computed goto statement to EXPR. LOC is the location of
9472 the GOTO. */
506e2710
RH
9473
9474tree
c2255bc4 9475c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9476{
c2255bc4 9477 tree t;
c1771a20 9478 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9479 expr = c_fully_fold (expr, false, NULL);
506e2710 9480 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9481 t = build1 (GOTO_EXPR, void_type_node, expr);
9482 SET_EXPR_LOCATION (t, loc);
9483 return add_stmt (t);
506e2710
RH
9484}
9485
5088b058 9486/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9487 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9488 the location of the return statement, or the location of the expression,
9489 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9490 is the original type of RETVAL. */
de520661 9491
506e2710 9492tree
c2255bc4 9493c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9494{
0c9b182b
JJ
9495 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9496 bool no_warning = false;
928c19bb 9497 bool npc = false;
36536d79 9498 size_t rank = 0;
3e4093b6 9499
de8ddd5f
MP
9500 /* Use the expansion point to handle cases such as returning NULL
9501 in a function returning void. */
9502 source_location xloc = expansion_point_location_if_in_system_header (loc);
9503
3e4093b6 9504 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 9505 warning_at (xloc, 0,
c2255bc4 9506 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9507
b72271b9 9508 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9509 {
9510 /* Array notations are allowed in a return statement if it is inside a
9511 built-in array notation reduction function. */
9512 if (!find_rank (loc, retval, retval, false, &rank))
9513 return error_mark_node;
9514 if (rank >= 1)
9515 {
9516 error_at (loc, "array notation expression cannot be used as a "
9517 "return value");
9518 return error_mark_node;
9519 }
9520 }
3af9c5e9 9521 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9522 {
9523 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9524 "allowed");
9525 return error_mark_node;
9526 }
928c19bb
JM
9527 if (retval)
9528 {
8ce94e44 9529 tree semantic_type = NULL_TREE;
928c19bb 9530 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9531 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9532 {
9533 semantic_type = TREE_TYPE (retval);
9534 retval = TREE_OPERAND (retval, 0);
9535 }
928c19bb 9536 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9537 if (semantic_type)
9538 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9539 }
9540
3e4093b6 9541 if (!retval)
de520661 9542 {
3e4093b6
RS
9543 current_function_returns_null = 1;
9544 if ((warn_return_type || flag_isoc99)
9545 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9546 {
35aff4fb
MP
9547 if (flag_isoc99)
9548 pedwarn (loc, 0, "%<return%> with no value, in "
9549 "function returning non-void");
9550 else
9551 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9552 "in function returning non-void");
0c9b182b
JJ
9553 no_warning = true;
9554 }
400fbf9f 9555 }
3e4093b6 9556 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9557 {
3e4093b6 9558 current_function_returns_null = 1;
2397c575 9559 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
de8ddd5f 9560 pedwarn (xloc, 0,
509c9d60 9561 "%<return%> with a value, in function returning void");
b8698a0f 9562 else
de8ddd5f 9563 pedwarn (xloc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9564 "%<return%> with expression, in function returning void");
de520661 9565 }
3e4093b6 9566 else
de520661 9567 {
68fca595
MP
9568 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9569 retval, origtype, ic_return,
c2255bc4 9570 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9571 tree res = DECL_RESULT (current_function_decl);
9572 tree inner;
9feb29df 9573 bool save;
3e4093b6
RS
9574
9575 current_function_returns_value = 1;
9576 if (t == error_mark_node)
506e2710 9577 return NULL_TREE;
3e4093b6 9578
9feb29df
JJ
9579 save = in_late_binary_op;
9580 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9581 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9582 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9583 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9584 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9585 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9586 in_late_binary_op = true;
3e4093b6 9587 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9588 in_late_binary_op = save;
3e4093b6
RS
9589
9590 /* Strip any conversions, additions, and subtractions, and see if
9591 we are returning the address of a local variable. Warn if so. */
9592 while (1)
8b6a5902 9593 {
3e4093b6 9594 switch (TREE_CODE (inner))
8b6a5902 9595 {
849421a3
JJ
9596 CASE_CONVERT:
9597 case NON_LVALUE_EXPR:
3e4093b6 9598 case PLUS_EXPR:
849421a3 9599 case POINTER_PLUS_EXPR:
3e4093b6
RS
9600 inner = TREE_OPERAND (inner, 0);
9601 continue;
9602
9603 case MINUS_EXPR:
9604 /* If the second operand of the MINUS_EXPR has a pointer
9605 type (or is converted from it), this may be valid, so
9606 don't give a warning. */
9607 {
9608 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9609
3f75a254 9610 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9611 && (CONVERT_EXPR_P (op1)
9612 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9613 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9614
3e4093b6
RS
9615 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9616 break;
8b6a5902 9617
3e4093b6
RS
9618 inner = TREE_OPERAND (inner, 0);
9619 continue;
9620 }
400fbf9f 9621
3e4093b6
RS
9622 case ADDR_EXPR:
9623 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9624
6615c446 9625 while (REFERENCE_CLASS_P (inner)
22d03525 9626 && !INDIRECT_REF_P (inner))
3e4093b6 9627 inner = TREE_OPERAND (inner, 0);
8b6a5902 9628
a2f1f4c3 9629 if (DECL_P (inner)
3f75a254
JM
9630 && !DECL_EXTERNAL (inner)
9631 && !TREE_STATIC (inner)
3e4093b6 9632 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9633 {
9634 if (TREE_CODE (inner) == LABEL_DECL)
9635 warning_at (loc, OPT_Wreturn_local_addr,
9636 "function returns address of label");
9637 else
b4dfdc11
MG
9638 {
9639 warning_at (loc, OPT_Wreturn_local_addr,
9640 "function returns address of local variable");
9641 tree zero = build_zero_cst (TREE_TYPE (res));
9642 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9643 }
19fc9faa 9644 }
3e4093b6 9645 break;
8b6a5902 9646
3e4093b6
RS
9647 default:
9648 break;
9649 }
de520661 9650
3e4093b6
RS
9651 break;
9652 }
9653
53fb4de3 9654 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9655 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9656
9657 if (warn_sequence_point)
9658 verify_sequence_points (retval);
de520661 9659 }
8b6a5902 9660
c2255bc4 9661 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9662 TREE_NO_WARNING (ret_stmt) |= no_warning;
9663 return add_stmt (ret_stmt);
de520661 9664}
3e4093b6
RS
9665\f
9666struct c_switch {
604f5adf
ILT
9667 /* The SWITCH_EXPR being built. */
9668 tree switch_expr;
a6c0a76c 9669
89dbed81 9670 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9671 default conversion is applied. */
9672 tree orig_type;
9673
3e4093b6
RS
9674 /* A splay-tree mapping the low element of a case range to the high
9675 element, or NULL_TREE if there is no high element. Used to
9676 determine whether or not a new case label duplicates an old case
9677 label. We need a tree, rather than simply a hash table, because
9678 of the GNU case range extension. */
9679 splay_tree cases;
a6c0a76c 9680
e1b7793c
ILT
9681 /* The bindings at the point of the switch. This is used for
9682 warnings crossing decls when branching to a case label. */
9683 struct c_spot_bindings *bindings;
187230a7 9684
3e4093b6
RS
9685 /* The next node on the stack. */
9686 struct c_switch *next;
b155cfd9
MP
9687
9688 /* Remember whether the controlling expression had boolean type
9689 before integer promotions for the sake of -Wswitch-bool. */
9690 bool bool_cond_p;
9691
9692 /* Remember whether there was a case value that is outside the
9693 range of the ORIG_TYPE. */
9694 bool outside_range_p;
3e4093b6 9695};
400fbf9f 9696
3e4093b6
RS
9697/* A stack of the currently active switch statements. The innermost
9698 switch statement is on the top of the stack. There is no need to
9699 mark the stack for garbage collection because it is only active
9700 during the processing of the body of a function, and we never
9701 collect at that point. */
de520661 9702
506e2710 9703struct c_switch *c_switch_stack;
de520661 9704
3e4093b6 9705/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9706 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9707 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9708 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9709
3e4093b6 9710tree
c2255bc4
AH
9711c_start_case (location_t switch_loc,
9712 location_t switch_cond_loc,
fedfecef 9713 tree exp, bool explicit_cast_p)
de520661 9714{
c58e8676 9715 tree orig_type = error_mark_node;
b155cfd9 9716 bool bool_cond_p = false;
3e4093b6 9717 struct c_switch *cs;
2f6e4e97 9718
3e4093b6 9719 if (exp != error_mark_node)
de520661 9720 {
3e4093b6
RS
9721 orig_type = TREE_TYPE (exp);
9722
c58e8676 9723 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9724 {
c58e8676
VR
9725 if (orig_type != error_mark_node)
9726 {
c2255bc4 9727 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9728 orig_type = error_mark_node;
9729 }
3e4093b6 9730 exp = integer_zero_node;
de520661 9731 }
3e4093b6 9732 else
de520661 9733 {
c58e8676 9734 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9735 tree e = exp;
9736
9737 /* Warn if the condition has boolean value. */
9738 while (TREE_CODE (e) == COMPOUND_EXPR)
9739 e = TREE_OPERAND (e, 1);
9740
9741 if ((TREE_CODE (type) == BOOLEAN_TYPE
9742 || truth_value_p (TREE_CODE (e)))
9743 /* Explicit cast to int suppresses this warning. */
9744 && !(TREE_CODE (type) == INTEGER_TYPE
9745 && explicit_cast_p))
b155cfd9 9746 bool_cond_p = true;
8b6a5902 9747
8400e75e 9748 if (!in_system_header_at (input_location)
3e4093b6
RS
9749 && (type == long_integer_type_node
9750 || type == long_unsigned_type_node))
c2255bc4
AH
9751 warning_at (switch_cond_loc,
9752 OPT_Wtraditional, "%<long%> switch expression not "
9753 "converted to %<int%> in ISO C");
8b6a5902 9754
928c19bb 9755 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9756 exp = default_conversion (exp);
ca085fd7
MLI
9757
9758 if (warn_sequence_point)
9759 verify_sequence_points (exp);
3e4093b6
RS
9760 }
9761 }
9762
604f5adf 9763 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9764 cs = XNEW (struct c_switch);
604f5adf 9765 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9766 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9767 cs->orig_type = orig_type;
3e4093b6 9768 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9769 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
9770 cs->bool_cond_p = bool_cond_p;
9771 cs->outside_range_p = false;
506e2710
RH
9772 cs->next = c_switch_stack;
9773 c_switch_stack = cs;
3e4093b6 9774
604f5adf 9775 return add_stmt (cs->switch_expr);
3e4093b6
RS
9776}
9777
c2255bc4 9778/* Process a case label at location LOC. */
3e4093b6
RS
9779
9780tree
c2255bc4 9781do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9782{
9783 tree label = NULL_TREE;
9784
17cede2e
JM
9785 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9786 {
9787 low_value = c_fully_fold (low_value, false, NULL);
9788 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9789 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9790 "case label is not an integer constant expression");
9791 }
9792
9793 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9794 {
9795 high_value = c_fully_fold (high_value, false, NULL);
9796 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9797 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9798 "case label is not an integer constant expression");
9799 }
9800
e1b7793c 9801 if (c_switch_stack == NULL)
187230a7
JM
9802 {
9803 if (low_value)
e1b7793c 9804 error_at (loc, "case label not within a switch statement");
187230a7 9805 else
e1b7793c
ILT
9806 error_at (loc, "%<default%> label not within a switch statement");
9807 return NULL_TREE;
187230a7 9808 }
de520661 9809
e1b7793c
ILT
9810 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9811 EXPR_LOCATION (c_switch_stack->switch_expr),
9812 loc))
9813 return NULL_TREE;
9814
9815 label = c_add_case_label (loc, c_switch_stack->cases,
9816 SWITCH_COND (c_switch_stack->switch_expr),
9817 c_switch_stack->orig_type,
b155cfd9
MP
9818 low_value, high_value,
9819 &c_switch_stack->outside_range_p);
e1b7793c
ILT
9820 if (label == error_mark_node)
9821 label = NULL_TREE;
3e4093b6
RS
9822 return label;
9823}
de520661 9824
083e891e
MP
9825/* Finish the switch statement. TYPE is the original type of the
9826 controlling expression of the switch, or NULL_TREE. */
de520661 9827
3e4093b6 9828void
083e891e 9829c_finish_case (tree body, tree type)
3e4093b6 9830{
506e2710 9831 struct c_switch *cs = c_switch_stack;
fbc315db 9832 location_t switch_location;
3e4093b6 9833
604f5adf 9834 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9835
6de9cd9a 9836 /* Emit warnings as needed. */
c2255bc4 9837 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9838 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9839 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
9840 SWITCH_COND (cs->switch_expr),
9841 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 9842
3e4093b6 9843 /* Pop the stack. */
506e2710 9844 c_switch_stack = cs->next;
3e4093b6 9845 splay_tree_delete (cs->cases);
e1b7793c 9846 c_release_switch_bindings (cs->bindings);
5d038c4c 9847 XDELETE (cs);
de520661 9848}
325c3691 9849\f
506e2710
RH
9850/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9851 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9852 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9853 statement, and was not surrounded with parenthesis. */
325c3691 9854
9e51cf9d 9855void
506e2710
RH
9856c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9857 tree else_block, bool nested_if)
325c3691 9858{
506e2710 9859 tree stmt;
325c3691 9860
25c22937
BI
9861 /* If the condition has array notations, then the rank of the then_block and
9862 else_block must be either 0 or be equal to the rank of the condition. If
9863 the condition does not have array notations then break them up as it is
9864 broken up in a normal expression. */
b72271b9 9865 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9866 {
9867 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9868 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9869 return;
9870 if (then_block
9871 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9872 return;
9873 if (else_block
9874 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9875 return;
9876 if (cond_rank != then_rank && then_rank != 0)
9877 {
9878 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9879 " and the then-block");
9880 return;
9881 }
9882 else if (cond_rank != else_rank && else_rank != 0)
9883 {
9884 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9885 " and the else-block");
9886 return;
9887 }
9888 }
506e2710
RH
9889 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9890 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9891 {
506e2710 9892 tree inner_if = then_block;
16865eaa 9893
61ada8ae 9894 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9895 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9896 it might not be exactly THEN_BLOCK, but should be the last
9897 non-container statement within. */
9898 while (1)
9899 switch (TREE_CODE (inner_if))
9900 {
9901 case COND_EXPR:
9902 goto found;
9903 case BIND_EXPR:
9904 inner_if = BIND_EXPR_BODY (inner_if);
9905 break;
9906 case STATEMENT_LIST:
9907 inner_if = expr_last (then_block);
9908 break;
9909 case TRY_FINALLY_EXPR:
9910 case TRY_CATCH_EXPR:
9911 inner_if = TREE_OPERAND (inner_if, 0);
9912 break;
9913 default:
366de0ce 9914 gcc_unreachable ();
506e2710
RH
9915 }
9916 found:
16865eaa 9917
506e2710 9918 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9919 warning_at (if_locus, OPT_Wparentheses,
9920 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9921 }
16865eaa 9922
2214de30 9923 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9924 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9925 add_stmt (stmt);
325c3691
RH
9926}
9927
506e2710
RH
9928/* Emit a general-purpose loop construct. START_LOCUS is the location of
9929 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9930 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9931 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9932 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9933
9934void
506e2710
RH
9935c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9936 tree blab, tree clab, bool cond_is_first)
325c3691 9937{
506e2710
RH
9938 tree entry = NULL, exit = NULL, t;
9939
e5e44252
AK
9940 /* In theory could forbid cilk spawn for loop increment expression,
9941 but it should work just fine. */
36536d79 9942
28af952a
RS
9943 /* If the condition is zero don't generate a loop construct. */
9944 if (cond && integer_zerop (cond))
9945 {
9946 if (cond_is_first)
9947 {
9948 t = build_and_jump (&blab);
9949 SET_EXPR_LOCATION (t, start_locus);
9950 add_stmt (t);
9951 }
9952 }
9953 else
506e2710
RH
9954 {
9955 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9956
506e2710 9957 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9958 out of the loop, or to the top of it. If there's no exit condition,
9959 then we just build a jump back to the top. */
506e2710 9960 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9961
28af952a 9962 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9963 {
9964 /* Canonicalize the loop condition to the end. This means
9965 generating a branch to the loop condition. Reuse the
9966 continue label, if possible. */
9967 if (cond_is_first)
9968 {
9969 if (incr || !clab)
9970 {
9971 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9972 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9973 }
9974 else
9975 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9976 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9977 add_stmt (t);
9978 }
9979
506e2710 9980 t = build_and_jump (&blab);
506e2710 9981 if (cond_is_first)
db3927fb
AH
9982 exit = fold_build3_loc (start_locus,
9983 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9984 else
db3927fb
AH
9985 exit = fold_build3_loc (input_location,
9986 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 9987 }
fc402eec
AA
9988 else
9989 {
9990 /* For the backward-goto's location of an unconditional loop
9991 use the beginning of the body, or, if there is none, the
9992 top of the loop. */
9993 location_t loc = EXPR_LOCATION (expr_first (body));
9994 if (loc == UNKNOWN_LOCATION)
9995 loc = start_locus;
9996 SET_EXPR_LOCATION (exit, loc);
9997 }
c22cacf3 9998
506e2710
RH
9999 add_stmt (top);
10000 }
c22cacf3 10001
506e2710
RH
10002 if (body)
10003 add_stmt (body);
10004 if (clab)
10005 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10006 if (incr)
10007 add_stmt (incr);
10008 if (entry)
10009 add_stmt (entry);
10010 if (exit)
10011 add_stmt (exit);
10012 if (blab)
10013 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10014}
325c3691
RH
10015
10016tree
c2255bc4 10017c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10018{
089efaa4 10019 bool skip;
506e2710 10020 tree label = *label_p;
325c3691 10021
089efaa4
ILT
10022 /* In switch statements break is sometimes stylistically used after
10023 a return statement. This can lead to spurious warnings about
10024 control reaching the end of a non-void function when it is
10025 inlined. Note that we are calling block_may_fallthru with
10026 language specific tree nodes; this works because
10027 block_may_fallthru returns true when given something it does not
10028 understand. */
10029 skip = !block_may_fallthru (cur_stmt_list);
10030
506e2710 10031 if (!label)
089efaa4
ILT
10032 {
10033 if (!skip)
c2255bc4 10034 *label_p = label = create_artificial_label (loc);
089efaa4 10035 }
953ff289
DN
10036 else if (TREE_CODE (label) == LABEL_DECL)
10037 ;
10038 else switch (TREE_INT_CST_LOW (label))
506e2710 10039 {
953ff289 10040 case 0:
506e2710 10041 if (is_break)
c2255bc4 10042 error_at (loc, "break statement not within loop or switch");
506e2710 10043 else
c2255bc4 10044 error_at (loc, "continue statement not within a loop");
506e2710 10045 return NULL_TREE;
953ff289
DN
10046
10047 case 1:
10048 gcc_assert (is_break);
c2255bc4 10049 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10050 return NULL_TREE;
10051
c02065fc
AH
10052 case 2:
10053 if (is_break)
10054 error ("break statement within %<#pragma simd%> loop body");
10055 else
10056 error ("continue statement within %<#pragma simd%> loop body");
10057 return NULL_TREE;
10058
953ff289
DN
10059 default:
10060 gcc_unreachable ();
506e2710 10061 }
325c3691 10062
089efaa4
ILT
10063 if (skip)
10064 return NULL_TREE;
10065
2e28e797
JH
10066 if (!is_break)
10067 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10068
53fb4de3 10069 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10070}
10071
506e2710 10072/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10073
10074static void
c2255bc4 10075emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10076{
e6b5a630
RH
10077 if (expr == error_mark_node)
10078 ;
10079 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10080 {
10081 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10082 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10083 }
789eadcd
MP
10084 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10085 {
10086 tree r = expr;
10087 location_t cloc = loc;
10088 while (TREE_CODE (r) == COMPOUND_EXPR)
10089 {
10090 if (EXPR_HAS_LOCATION (r))
10091 cloc = EXPR_LOCATION (r);
10092 r = TREE_OPERAND (r, 1);
10093 }
10094 if (!TREE_SIDE_EFFECTS (r)
10095 && !VOID_TYPE_P (TREE_TYPE (r))
10096 && !CONVERT_EXPR_P (r)
cc28fc7f 10097 && !TREE_NO_WARNING (r)
789eadcd
MP
10098 && !TREE_NO_WARNING (expr))
10099 warning_at (cloc, OPT_Wunused_value,
10100 "right-hand operand of comma expression has no effect");
10101 }
27f33b15 10102 else
c2255bc4 10103 warn_if_unused_value (expr, loc);
3a5b9284
RH
10104}
10105
506e2710 10106/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10107 diagnostics, but do not call ADD_STMT. LOC is the location of the
10108 statement. */
3a5b9284 10109
506e2710 10110tree
c2255bc4 10111c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10112{
056928b2
JJ
10113 tree exprv;
10114
3a5b9284 10115 if (!expr)
506e2710 10116 return NULL_TREE;
3a5b9284 10117
928c19bb
JM
10118 expr = c_fully_fold (expr, false, NULL);
10119
3a5b9284
RH
10120 if (warn_sequence_point)
10121 verify_sequence_points (expr);
10122
10123 if (TREE_TYPE (expr) != error_mark_node
10124 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10125 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10126 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10127
10128 /* If we're not processing a statement expression, warn about unused values.
10129 Warnings for statement expressions will be emitted later, once we figure
10130 out which is the result. */
10131 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10132 && warn_unused_value)
c2255bc4 10133 emit_side_effect_warnings (loc, expr);
3a5b9284 10134
056928b2
JJ
10135 exprv = expr;
10136 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10137 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10138 while (CONVERT_EXPR_P (exprv))
10139 exprv = TREE_OPERAND (exprv, 0);
10140 if (DECL_P (exprv)
10141 || handled_component_p (exprv)
10142 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10143 mark_exp_read (exprv);
fa8351f8 10144
3a5b9284
RH
10145 /* If the expression is not of a type to which we cannot assign a line
10146 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10147 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10148 {
10149 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10150 SET_EXPR_LOCATION (expr, loc);
10151 }
506e2710
RH
10152
10153 return expr;
10154}
10155
c2255bc4
AH
10156/* Emit an expression as a statement. LOC is the location of the
10157 expression. */
506e2710
RH
10158
10159tree
c2255bc4 10160c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10161{
10162 if (expr)
c2255bc4 10163 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10164 else
10165 return NULL;
3a5b9284
RH
10166}
10167
10168/* Do the opposite and emit a statement as an expression. To begin,
10169 create a new binding level and return it. */
325c3691
RH
10170
10171tree
10172c_begin_stmt_expr (void)
10173{
10174 tree ret;
10175
10176 /* We must force a BLOCK for this level so that, if it is not expanded
10177 later, there is a way to turn off the entire subtree of blocks that
10178 are contained in it. */
10179 keep_next_level ();
10180 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10181
10182 c_bindings_start_stmt_expr (c_switch_stack == NULL
10183 ? NULL
10184 : c_switch_stack->bindings);
325c3691
RH
10185
10186 /* Mark the current statement list as belonging to a statement list. */
10187 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10188
10189 return ret;
10190}
10191
c2255bc4
AH
10192/* LOC is the location of the compound statement to which this body
10193 belongs. */
10194
325c3691 10195tree
c2255bc4 10196c_finish_stmt_expr (location_t loc, tree body)
325c3691 10197{
3a5b9284 10198 tree last, type, tmp, val;
325c3691
RH
10199 tree *last_p;
10200
c2255bc4 10201 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10202
10203 c_bindings_end_stmt_expr (c_switch_stack == NULL
10204 ? NULL
10205 : c_switch_stack->bindings);
325c3691 10206
3a5b9284
RH
10207 /* Locate the last statement in BODY. See c_end_compound_stmt
10208 about always returning a BIND_EXPR. */
10209 last_p = &BIND_EXPR_BODY (body);
10210 last = BIND_EXPR_BODY (body);
10211
10212 continue_searching:
325c3691
RH
10213 if (TREE_CODE (last) == STATEMENT_LIST)
10214 {
3a5b9284
RH
10215 tree_stmt_iterator i;
10216
10217 /* This can happen with degenerate cases like ({ }). No value. */
10218 if (!TREE_SIDE_EFFECTS (last))
10219 return body;
10220
10221 /* If we're supposed to generate side effects warnings, process
10222 all of the statements except the last. */
27f33b15 10223 if (warn_unused_value)
325c3691 10224 {
3a5b9284 10225 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10226 {
10227 location_t tloc;
10228 tree t = tsi_stmt (i);
10229
10230 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10231 emit_side_effect_warnings (tloc, t);
10232 }
325c3691
RH
10233 }
10234 else
3a5b9284
RH
10235 i = tsi_last (last);
10236 last_p = tsi_stmt_ptr (i);
10237 last = *last_p;
325c3691
RH
10238 }
10239
3a5b9284
RH
10240 /* If the end of the list is exception related, then the list was split
10241 by a call to push_cleanup. Continue searching. */
10242 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10243 || TREE_CODE (last) == TRY_CATCH_EXPR)
10244 {
10245 last_p = &TREE_OPERAND (last, 0);
10246 last = *last_p;
10247 goto continue_searching;
10248 }
10249
26d8af35
JM
10250 if (last == error_mark_node)
10251 return last;
10252
3a5b9284
RH
10253 /* In the case that the BIND_EXPR is not necessary, return the
10254 expression out from inside it. */
26d8af35
JM
10255 if (last == BIND_EXPR_BODY (body)
10256 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10257 {
928c19bb
JM
10258 /* Even if this looks constant, do not allow it in a constant
10259 expression. */
e5a94231 10260 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10261 /* Do not warn if the return value of a statement expression is
10262 unused. */
928c19bb 10263 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10264 return last;
10265 }
325c3691
RH
10266
10267 /* Extract the type of said expression. */
10268 type = TREE_TYPE (last);
325c3691 10269
3a5b9284
RH
10270 /* If we're not returning a value at all, then the BIND_EXPR that
10271 we already have is a fine expression to return. */
10272 if (!type || VOID_TYPE_P (type))
10273 return body;
10274
10275 /* Now that we've located the expression containing the value, it seems
10276 silly to make voidify_wrapper_expr repeat the process. Create a
10277 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10278 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10279
10280 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10281 tree_expr_nonnegative_p giving up immediately. */
10282 val = last;
10283 if (TREE_CODE (val) == NOP_EXPR
10284 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10285 val = TREE_OPERAND (val, 0);
10286
53fb4de3 10287 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10288 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10289
c2255bc4
AH
10290 {
10291 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10292 SET_EXPR_LOCATION (t, loc);
10293 return t;
10294 }
325c3691
RH
10295}
10296\f
10297/* Begin and end compound statements. This is as simple as pushing
10298 and popping new statement lists from the tree. */
10299
10300tree
10301c_begin_compound_stmt (bool do_scope)
10302{
10303 tree stmt = push_stmt_list ();
10304 if (do_scope)
4dfa0342 10305 push_scope ();
325c3691
RH
10306 return stmt;
10307}
10308
c2255bc4
AH
10309/* End a compound statement. STMT is the statement. LOC is the
10310 location of the compound statement-- this is usually the location
10311 of the opening brace. */
10312
325c3691 10313tree
c2255bc4 10314c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10315{
10316 tree block = NULL;
10317
10318 if (do_scope)
10319 {
10320 if (c_dialect_objc ())
10321 objc_clear_super_receiver ();
10322 block = pop_scope ();
10323 }
10324
10325 stmt = pop_stmt_list (stmt);
c2255bc4 10326 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10327
10328 /* If this compound statement is nested immediately inside a statement
10329 expression, then force a BIND_EXPR to be created. Otherwise we'll
10330 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10331 STATEMENT_LISTs merge, and thus we can lose track of what statement
10332 was really last. */
38e01f9e 10333 if (building_stmt_list_p ()
325c3691
RH
10334 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10335 && TREE_CODE (stmt) != BIND_EXPR)
10336 {
53fb4de3 10337 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10338 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10339 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10340 }
10341
10342 return stmt;
10343}
5a508662
RH
10344
10345/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10346 when the current scope is exited. EH_ONLY is true when this is not
10347 meant to apply to normal control flow transfer. */
10348
10349void
c2255bc4 10350push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10351{
3a5b9284
RH
10352 enum tree_code code;
10353 tree stmt, list;
10354 bool stmt_expr;
10355
10356 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10357 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10358 add_stmt (stmt);
3a5b9284
RH
10359 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10360 list = push_stmt_list ();
10361 TREE_OPERAND (stmt, 0) = list;
10362 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10363}
325c3691 10364\f
9f47c7e5
IE
10365/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10366 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10367
10368static tree
10369build_vec_cmp (tree_code code, tree type,
10370 tree arg0, tree arg1)
10371{
10372 tree zero_vec = build_zero_cst (type);
10373 tree minus_one_vec = build_minus_one_cst (type);
10374 tree cmp_type = build_same_sized_truth_vector_type (type);
10375 tree cmp = build2 (code, cmp_type, arg0, arg1);
10376 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10377}
10378
3e4093b6
RS
10379/* Build a binary-operation expression without default conversions.
10380 CODE is the kind of expression to build.
ba47d38d 10381 LOCATION is the operator's location.
3e4093b6
RS
10382 This function differs from `build' in several ways:
10383 the data type of the result is computed and recorded in it,
10384 warnings are generated if arg data types are invalid,
10385 special handling for addition and subtraction of pointers is known,
10386 and some optimization is done (operations on narrow ints
10387 are done in the narrower type when that gives the same result).
10388 Constant folding is also done before the result is returned.
de520661 10389
3e4093b6
RS
10390 Note that the operands will never have enumeral types, or function
10391 or array types, because either they will have the default conversions
10392 performed or they have both just been converted to some other type in which
10393 the arithmetic is to be done. */
10394
10395tree
ba47d38d
AH
10396build_binary_op (location_t location, enum tree_code code,
10397 tree orig_op0, tree orig_op1, int convert_p)
de520661 10398{
8ce94e44
JM
10399 tree type0, type1, orig_type0, orig_type1;
10400 tree eptype;
3e4093b6
RS
10401 enum tree_code code0, code1;
10402 tree op0, op1;
c9f9eb5d 10403 tree ret = error_mark_node;
4de67c26 10404 const char *invalid_op_diag;
4d84fe7c 10405 bool op0_int_operands, op1_int_operands;
928c19bb 10406 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10407
3e4093b6
RS
10408 /* Expression code to give to the expression when it is built.
10409 Normally this is CODE, which is what the caller asked for,
10410 but in some special cases we change it. */
10411 enum tree_code resultcode = code;
8b6a5902 10412
3e4093b6
RS
10413 /* Data type in which the computation is to be performed.
10414 In the simplest cases this is the common type of the arguments. */
10415 tree result_type = NULL;
10416
8ce94e44
JM
10417 /* When the computation is in excess precision, the type of the
10418 final EXCESS_PRECISION_EXPR. */
2d2e923f 10419 tree semantic_result_type = NULL;
8ce94e44 10420
3e4093b6
RS
10421 /* Nonzero means operands have already been type-converted
10422 in whatever way is necessary.
10423 Zero means they need to be converted to RESULT_TYPE. */
10424 int converted = 0;
10425
10426 /* Nonzero means create the expression with this type, rather than
10427 RESULT_TYPE. */
10428 tree build_type = 0;
10429
10430 /* Nonzero means after finally constructing the expression
10431 convert it to this type. */
10432 tree final_type = 0;
10433
10434 /* Nonzero if this is an operation like MIN or MAX which can
10435 safely be computed in short if both args are promoted shorts.
10436 Also implies COMMON.
10437 -1 indicates a bitwise operation; this makes a difference
10438 in the exact conditions for when it is safe to do the operation
10439 in a narrower mode. */
10440 int shorten = 0;
10441
10442 /* Nonzero if this is a comparison operation;
10443 if both args are promoted shorts, compare the original shorts.
10444 Also implies COMMON. */
10445 int short_compare = 0;
10446
10447 /* Nonzero if this is a right-shift operation, which can be computed on the
10448 original short and then promoted if the operand is a promoted short. */
10449 int short_shift = 0;
10450
10451 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10452 int common = 0;
10453
58393038
ZL
10454 /* True means types are compatible as far as ObjC is concerned. */
10455 bool objc_ok;
10456
8ce94e44
JM
10457 /* True means this is an arithmetic operation that may need excess
10458 precision. */
10459 bool may_need_excess_precision;
10460
180f8dbb
JM
10461 /* True means this is a boolean operation that converts both its
10462 operands to truth-values. */
10463 bool boolean_op = false;
10464
de5a5fa1
MP
10465 /* Remember whether we're doing / or %. */
10466 bool doing_div_or_mod = false;
10467
10468 /* Remember whether we're doing << or >>. */
10469 bool doing_shift = false;
10470
10471 /* Tree holding instrumentation expression. */
10472 tree instrument_expr = NULL;
10473
ba47d38d
AH
10474 if (location == UNKNOWN_LOCATION)
10475 location = input_location;
10476
4d84fe7c
JM
10477 op0 = orig_op0;
10478 op1 = orig_op1;
10479
10480 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10481 if (op0_int_operands)
10482 op0 = remove_c_maybe_const_expr (op0);
10483 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10484 if (op1_int_operands)
10485 op1 = remove_c_maybe_const_expr (op1);
10486 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10487 if (int_operands)
10488 {
10489 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10490 && TREE_CODE (orig_op1) == INTEGER_CST);
10491 int_const = (int_const_or_overflow
10492 && !TREE_OVERFLOW (orig_op0)
10493 && !TREE_OVERFLOW (orig_op1));
10494 }
10495 else
10496 int_const = int_const_or_overflow = false;
10497
0e3a99ae 10498 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10499 if (convert_p
31521951 10500 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10501 {
4d84fe7c
JM
10502 op0 = default_conversion (op0);
10503 op1 = default_conversion (op1);
790e9490
RS
10504 }
10505
36536d79
BI
10506 /* When Cilk Plus is enabled and there are array notations inside op0, then
10507 we check to see if there are builtin array notation functions. If
10508 so, then we take on the type of the array notation inside it. */
b72271b9 10509 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10510 orig_type0 = type0 = find_correct_array_notation_type (op0);
10511 else
10512 orig_type0 = type0 = TREE_TYPE (op0);
10513
b72271b9 10514 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10515 orig_type1 = type1 = find_correct_array_notation_type (op1);
10516 else
10517 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10518
3e4093b6
RS
10519 /* The expression codes of the data types of the arguments tell us
10520 whether the arguments are integers, floating, pointers, etc. */
10521 code0 = TREE_CODE (type0);
10522 code1 = TREE_CODE (type1);
10523
10524 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10525 STRIP_TYPE_NOPS (op0);
10526 STRIP_TYPE_NOPS (op1);
10527
10528 /* If an error was already reported for one of the arguments,
10529 avoid reporting another error. */
10530
10531 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10532 return error_mark_node;
10533
1807ffc1
MS
10534 if (code0 == POINTER_TYPE
10535 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10536 return error_mark_node;
10537
10538 if (code1 == POINTER_TYPE
10539 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10540 return error_mark_node;
10541
4de67c26
JM
10542 if ((invalid_op_diag
10543 = targetm.invalid_binary_op (code, type0, type1)))
10544 {
ba47d38d 10545 error_at (location, invalid_op_diag);
4de67c26
JM
10546 return error_mark_node;
10547 }
10548
8ce94e44
JM
10549 switch (code)
10550 {
10551 case PLUS_EXPR:
10552 case MINUS_EXPR:
10553 case MULT_EXPR:
10554 case TRUNC_DIV_EXPR:
10555 case CEIL_DIV_EXPR:
10556 case FLOOR_DIV_EXPR:
10557 case ROUND_DIV_EXPR:
10558 case EXACT_DIV_EXPR:
10559 may_need_excess_precision = true;
10560 break;
10561 default:
10562 may_need_excess_precision = false;
10563 break;
10564 }
10565 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10566 {
10567 op0 = TREE_OPERAND (op0, 0);
10568 type0 = TREE_TYPE (op0);
10569 }
10570 else if (may_need_excess_precision
10571 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10572 {
10573 type0 = eptype;
10574 op0 = convert (eptype, op0);
10575 }
10576 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10577 {
10578 op1 = TREE_OPERAND (op1, 0);
10579 type1 = TREE_TYPE (op1);
10580 }
10581 else if (may_need_excess_precision
10582 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10583 {
10584 type1 = eptype;
10585 op1 = convert (eptype, op1);
10586 }
10587
58393038
ZL
10588 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10589
0e3a99ae
AS
10590 /* In case when one of the operands of the binary operation is
10591 a vector and another is a scalar -- convert scalar to vector. */
10592 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10593 {
a212e43f
MG
10594 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10595 true);
f90e8e2e 10596
0e3a99ae
AS
10597 switch (convert_flag)
10598 {
10599 case stv_error:
10600 return error_mark_node;
10601 case stv_firstarg:
10602 {
10603 bool maybe_const = true;
10604 tree sc;
10605 sc = c_fully_fold (op0, false, &maybe_const);
10606 sc = save_expr (sc);
10607 sc = convert (TREE_TYPE (type1), sc);
10608 op0 = build_vector_from_val (type1, sc);
10609 if (!maybe_const)
10610 op0 = c_wrap_maybe_const (op0, true);
10611 orig_type0 = type0 = TREE_TYPE (op0);
10612 code0 = TREE_CODE (type0);
10613 converted = 1;
10614 break;
10615 }
10616 case stv_secondarg:
10617 {
10618 bool maybe_const = true;
10619 tree sc;
10620 sc = c_fully_fold (op1, false, &maybe_const);
10621 sc = save_expr (sc);
10622 sc = convert (TREE_TYPE (type0), sc);
10623 op1 = build_vector_from_val (type0, sc);
10624 if (!maybe_const)
54b9f838 10625 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10626 orig_type1 = type1 = TREE_TYPE (op1);
10627 code1 = TREE_CODE (type1);
10628 converted = 1;
10629 break;
10630 }
10631 default:
10632 break;
10633 }
10634 }
10635
3e4093b6 10636 switch (code)
de520661 10637 {
3e4093b6
RS
10638 case PLUS_EXPR:
10639 /* Handle the pointer + int case. */
10640 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10641 {
db3927fb 10642 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10643 goto return_build_binary_op;
10644 }
3e4093b6 10645 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10646 {
db3927fb 10647 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10648 goto return_build_binary_op;
10649 }
fe67cf58 10650 else
3e4093b6
RS
10651 common = 1;
10652 break;
400fbf9f 10653
3e4093b6
RS
10654 case MINUS_EXPR:
10655 /* Subtraction of two similar pointers.
10656 We must subtract them as integers, then divide by object size. */
10657 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10658 && comp_target_types (location, type0, type1))
c9f9eb5d 10659 {
db3927fb 10660 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10661 goto return_build_binary_op;
10662 }
3e4093b6
RS
10663 /* Handle pointer minus int. Just like pointer plus int. */
10664 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10665 {
db3927fb 10666 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10667 goto return_build_binary_op;
10668 }
3e4093b6
RS
10669 else
10670 common = 1;
10671 break;
8b6a5902 10672
3e4093b6
RS
10673 case MULT_EXPR:
10674 common = 1;
10675 break;
10676
10677 case TRUNC_DIV_EXPR:
10678 case CEIL_DIV_EXPR:
10679 case FLOOR_DIV_EXPR:
10680 case ROUND_DIV_EXPR:
10681 case EXACT_DIV_EXPR:
de5a5fa1 10682 doing_div_or_mod = true;
c9f9eb5d 10683 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10684
10685 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10686 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10687 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10688 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10689 || code1 == FIXED_POINT_TYPE
3e4093b6 10690 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10691 {
5bed876a
AH
10692 enum tree_code tcode0 = code0, tcode1 = code1;
10693
3a021db2 10694 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10695 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10696 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10697 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10698
ab22c1fa
CF
10699 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10700 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10701 resultcode = RDIV_EXPR;
10702 else
10703 /* Although it would be tempting to shorten always here, that
10704 loses on some targets, since the modulo instruction is
10705 undefined if the quotient can't be represented in the
10706 computation mode. We shorten only if unsigned or if
10707 dividing by something we know != -1. */
8df83eae 10708 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10709 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10710 && !integer_all_onesp (op1)));
3e4093b6
RS
10711 common = 1;
10712 }
10713 break;
de520661 10714
3e4093b6 10715 case BIT_AND_EXPR:
3e4093b6
RS
10716 case BIT_IOR_EXPR:
10717 case BIT_XOR_EXPR:
10718 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10719 shorten = -1;
9ef0c8d9
AP
10720 /* Allow vector types which are not floating point types. */
10721 else if (code0 == VECTOR_TYPE
10722 && code1 == VECTOR_TYPE
10723 && !VECTOR_FLOAT_TYPE_P (type0)
10724 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10725 common = 1;
10726 break;
10727
10728 case TRUNC_MOD_EXPR:
10729 case FLOOR_MOD_EXPR:
de5a5fa1 10730 doing_div_or_mod = true;
c9f9eb5d 10731 warn_for_div_by_zero (location, op1);
de520661 10732
5cfd5d9b
AP
10733 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10734 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10735 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10736 common = 1;
10737 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10738 {
10739 /* Although it would be tempting to shorten always here, that loses
10740 on some targets, since the modulo instruction is undefined if the
10741 quotient can't be represented in the computation mode. We shorten
10742 only if unsigned or if dividing by something we know != -1. */
8df83eae 10743 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10744 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10745 && !integer_all_onesp (op1)));
3e4093b6
RS
10746 common = 1;
10747 }
10748 break;
de520661 10749
3e4093b6
RS
10750 case TRUTH_ANDIF_EXPR:
10751 case TRUTH_ORIF_EXPR:
10752 case TRUTH_AND_EXPR:
10753 case TRUTH_OR_EXPR:
10754 case TRUTH_XOR_EXPR:
10755 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10756 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10757 || code0 == FIXED_POINT_TYPE)
3e4093b6 10758 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10759 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10760 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10761 {
10762 /* Result of these operations is always an int,
10763 but that does not mean the operands should be
10764 converted to ints! */
10765 result_type = integer_type_node;
a27d595d
JM
10766 if (op0_int_operands)
10767 {
10768 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10769 op0 = remove_c_maybe_const_expr (op0);
10770 }
10771 else
10772 op0 = c_objc_common_truthvalue_conversion (location, op0);
10773 if (op1_int_operands)
10774 {
10775 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10776 op1 = remove_c_maybe_const_expr (op1);
10777 }
10778 else
10779 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10780 converted = 1;
180f8dbb 10781 boolean_op = true;
3e4093b6 10782 }
928c19bb
JM
10783 if (code == TRUTH_ANDIF_EXPR)
10784 {
10785 int_const_or_overflow = (int_operands
10786 && TREE_CODE (orig_op0) == INTEGER_CST
10787 && (op0 == truthvalue_false_node
10788 || TREE_CODE (orig_op1) == INTEGER_CST));
10789 int_const = (int_const_or_overflow
10790 && !TREE_OVERFLOW (orig_op0)
10791 && (op0 == truthvalue_false_node
10792 || !TREE_OVERFLOW (orig_op1)));
10793 }
10794 else if (code == TRUTH_ORIF_EXPR)
10795 {
10796 int_const_or_overflow = (int_operands
10797 && TREE_CODE (orig_op0) == INTEGER_CST
10798 && (op0 == truthvalue_true_node
10799 || TREE_CODE (orig_op1) == INTEGER_CST));
10800 int_const = (int_const_or_overflow
10801 && !TREE_OVERFLOW (orig_op0)
10802 && (op0 == truthvalue_true_node
10803 || !TREE_OVERFLOW (orig_op1)));
10804 }
3e4093b6 10805 break;
eba80994 10806
3e4093b6
RS
10807 /* Shift operations: result has same type as first operand;
10808 always convert second operand to int.
10809 Also set SHORT_SHIFT if shifting rightward. */
de520661 10810
3e4093b6 10811 case RSHIFT_EXPR:
f87bd04b
AS
10812 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10813 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10814 {
10815 result_type = type0;
10816 converted = 1;
10817 }
10818 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10819 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10820 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10821 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10822 {
10823 result_type = type0;
10824 converted = 1;
10825 }
10826 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10827 && code1 == INTEGER_TYPE)
3e4093b6 10828 {
de5a5fa1 10829 doing_shift = true;
928c19bb 10830 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10831 {
3e4093b6 10832 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10833 {
10834 int_const = false;
7d882b83 10835 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10836 warning_at (location, OPT_Wshift_count_negative,
10837 "right shift count is negative");
928c19bb 10838 }
3e4093b6 10839 else
bbb818c6 10840 {
3f75a254 10841 if (!integer_zerop (op1))
3e4093b6
RS
10842 short_shift = 1;
10843
10844 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10845 {
10846 int_const = false;
7d882b83 10847 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10848 warning_at (location, OPT_Wshift_count_overflow,
10849 "right shift count >= width of type");
928c19bb 10850 }
bbb818c6 10851 }
b62acd60 10852 }
de520661 10853
3e4093b6
RS
10854 /* Use the type of the value to be shifted. */
10855 result_type = type0;
3e4093b6
RS
10856 /* Avoid converting op1 to result_type later. */
10857 converted = 1;
400fbf9f 10858 }
3e4093b6 10859 break;
253b6b82 10860
3e4093b6 10861 case LSHIFT_EXPR:
f87bd04b
AS
10862 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10863 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10864 {
10865 result_type = type0;
10866 converted = 1;
10867 }
10868 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
451b5e48
MP
10869 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10870 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10871 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
f87bd04b
AS
10872 {
10873 result_type = type0;
10874 converted = 1;
10875 }
10876 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
451b5e48 10877 && code1 == INTEGER_TYPE)
3e4093b6 10878 {
de5a5fa1 10879 doing_shift = true;
0173bd2a
MP
10880 if (TREE_CODE (op0) == INTEGER_CST
10881 && tree_int_cst_sgn (op0) < 0)
10882 {
10883 /* Don't reject a left shift of a negative value in a context
10884 where a constant expression is needed in C90. */
10885 if (flag_isoc99)
10886 int_const = false;
10887 if (c_inhibit_evaluation_warnings == 0)
10888 warning_at (location, OPT_Wshift_negative_value,
10889 "left shift of negative value");
10890 }
928c19bb 10891 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10892 {
3e4093b6 10893 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10894 {
10895 int_const = false;
7d882b83 10896 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10897 warning_at (location, OPT_Wshift_count_negative,
10898 "left shift count is negative");
928c19bb 10899 }
3e4093b6 10900 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10901 {
10902 int_const = false;
7d882b83 10903 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10904 warning_at (location, OPT_Wshift_count_overflow,
10905 "left shift count >= width of type");
928c19bb 10906 }
451b5e48
MP
10907 else if (TREE_CODE (op0) == INTEGER_CST
10908 && maybe_warn_shift_overflow (location, op0, op1)
10909 && flag_isoc99)
10910 int_const = false;
94ba5069 10911 }
de520661 10912
3e4093b6
RS
10913 /* Use the type of the value to be shifted. */
10914 result_type = type0;
3e4093b6
RS
10915 /* Avoid converting op1 to result_type later. */
10916 converted = 1;
400fbf9f 10917 }
3e4093b6 10918 break;
de520661 10919
3e4093b6
RS
10920 case EQ_EXPR:
10921 case NE_EXPR:
d246ab4f
AS
10922 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10923 {
10924 tree intt;
0af94e6f 10925 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10926 {
10927 error_at (location, "comparing vectors with different "
10928 "element types");
10929 return error_mark_node;
10930 }
10931
10932 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10933 {
10934 error_at (location, "comparing vectors with different "
10935 "number of elements");
10936 return error_mark_node;
10937 }
10938
10939 /* Always construct signed integer vector type. */
10940 intt = c_common_type_for_size (GET_MODE_BITSIZE
10941 (TYPE_MODE (TREE_TYPE (type0))), 0);
10942 result_type = build_opaque_vector_type (intt,
10943 TYPE_VECTOR_SUBPARTS (type0));
10944 converted = 1;
9f47c7e5
IE
10945 ret = build_vec_cmp (resultcode, result_type, op0, op1);
10946 goto return_build_binary_op;
d246ab4f 10947 }
ae311566 10948 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10949 warning_at (location,
10950 OPT_Wfloat_equal,
10951 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10952 /* Result of comparison is always int,
10953 but don't convert the args to int! */
10954 build_type = integer_type_node;
10955 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10956 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10957 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10958 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10959 short_compare = 1;
637f1455
SZ
10960 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10961 {
dbb68221
MW
10962 if (warn_nonnull
10963 && TREE_CODE (op0) == PARM_DECL && nonnull_arg_p (op0))
10964 warning_at (location, OPT_Wnonnull,
10965 "nonnull argument %qD compared to NULL", op0);
10966
637f1455
SZ
10967 if (TREE_CODE (op0) == ADDR_EXPR
10968 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10969 {
10970 if (code == EQ_EXPR)
10971 warning_at (location,
10972 OPT_Waddress,
10973 "the comparison will always evaluate as %<false%> "
10974 "for the address of %qD will never be NULL",
10975 TREE_OPERAND (op0, 0));
10976 else
10977 warning_at (location,
10978 OPT_Waddress,
10979 "the comparison will always evaluate as %<true%> "
10980 "for the address of %qD will never be NULL",
10981 TREE_OPERAND (op0, 0));
10982 }
10983 result_type = type0;
10984 }
10985 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10986 {
dbb68221
MW
10987 if (warn_nonnull
10988 && TREE_CODE (op1) == PARM_DECL && nonnull_arg_p (op1))
10989 warning_at (location, OPT_Wnonnull,
10990 "nonnull argument %qD compared to NULL", op1);
10991
637f1455
SZ
10992 if (TREE_CODE (op1) == ADDR_EXPR
10993 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10994 {
10995 if (code == EQ_EXPR)
10996 warning_at (location,
f90e8e2e 10997 OPT_Waddress,
637f1455
SZ
10998 "the comparison will always evaluate as %<false%> "
10999 "for the address of %qD will never be NULL",
11000 TREE_OPERAND (op1, 0));
11001 else
11002 warning_at (location,
11003 OPT_Waddress,
11004 "the comparison will always evaluate as %<true%> "
11005 "for the address of %qD will never be NULL",
11006 TREE_OPERAND (op1, 0));
11007 }
11008 result_type = type1;
11009 }
3e4093b6
RS
11010 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11011 {
11012 tree tt0 = TREE_TYPE (type0);
11013 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11014 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11015 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11016 addr_space_t as_common = ADDR_SPACE_GENERIC;
11017
3e4093b6
RS
11018 /* Anything compares with void *. void * compares with anything.
11019 Otherwise, the targets must be compatible
11020 and both must be object or both incomplete. */
744aa42f 11021 if (comp_target_types (location, type0, type1))
10bc1b1b 11022 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11023 else if (!addr_space_superset (as0, as1, &as_common))
11024 {
11025 error_at (location, "comparison of pointers to "
11026 "disjoint address spaces");
11027 return error_mark_node;
11028 }
267bac10 11029 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11030 {
36c5e70a 11031 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11032 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11033 "comparison of %<void *%> with function pointer");
ee2990e7 11034 }
267bac10 11035 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11036 {
36c5e70a 11037 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11038 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11039 "comparison of %<void *%> with function pointer");
e6834654 11040 }
3e4093b6 11041 else
58393038
ZL
11042 /* Avoid warning about the volatile ObjC EH puts on decls. */
11043 if (!objc_ok)
ba47d38d 11044 pedwarn (location, 0,
509c9d60 11045 "comparison of distinct pointer types lacks a cast");
e6834654 11046
3e4093b6 11047 if (result_type == NULL_TREE)
36c5e70a
BE
11048 {
11049 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11050 result_type = build_pointer_type
11051 (build_qualified_type (void_type_node, qual));
11052 }
e6834654 11053 }
3e4093b6 11054 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11055 {
3e4093b6 11056 result_type = type0;
ba47d38d 11057 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11058 }
3e4093b6 11059 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11060 {
3e4093b6 11061 result_type = type1;
ba47d38d 11062 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11063 }
04159acf
MP
11064 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11065 || truth_value_p (TREE_CODE (orig_op0)))
11066 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11067 || truth_value_p (TREE_CODE (orig_op1))))
11068 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11069 break;
8b6a5902 11070
3e4093b6
RS
11071 case LE_EXPR:
11072 case GE_EXPR:
11073 case LT_EXPR:
11074 case GT_EXPR:
d246ab4f
AS
11075 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11076 {
11077 tree intt;
0af94e6f 11078 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11079 {
11080 error_at (location, "comparing vectors with different "
11081 "element types");
11082 return error_mark_node;
11083 }
11084
11085 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11086 {
11087 error_at (location, "comparing vectors with different "
11088 "number of elements");
11089 return error_mark_node;
11090 }
11091
11092 /* Always construct signed integer vector type. */
11093 intt = c_common_type_for_size (GET_MODE_BITSIZE
11094 (TYPE_MODE (TREE_TYPE (type0))), 0);
11095 result_type = build_opaque_vector_type (intt,
11096 TYPE_VECTOR_SUBPARTS (type0));
11097 converted = 1;
9f47c7e5
IE
11098 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11099 goto return_build_binary_op;
d246ab4f 11100 }
3e4093b6 11101 build_type = integer_type_node;
ab22c1fa
CF
11102 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11103 || code0 == FIXED_POINT_TYPE)
11104 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11105 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11106 short_compare = 1;
11107 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11108 {
36c5e70a
BE
11109 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11110 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11111 addr_space_t as_common;
11112
744aa42f 11113 if (comp_target_types (location, type0, type1))
3e4093b6 11114 {
10bc1b1b 11115 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11116 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11117 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11118 pedwarn (location, 0,
509c9d60 11119 "comparison of complete and incomplete pointers");
fcf73884 11120 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11121 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11122 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11123 else if (null_pointer_constant_p (orig_op0)
11124 || null_pointer_constant_p (orig_op1))
11125 warning_at (location, OPT_Wextra,
11126 "ordered comparison of pointer with null pointer");
11127
3e4093b6 11128 }
36c5e70a
BE
11129 else if (!addr_space_superset (as0, as1, &as_common))
11130 {
11131 error_at (location, "comparison of pointers to "
11132 "disjoint address spaces");
11133 return error_mark_node;
11134 }
3e4093b6
RS
11135 else
11136 {
36c5e70a
BE
11137 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11138 result_type = build_pointer_type
11139 (build_qualified_type (void_type_node, qual));
ba47d38d 11140 pedwarn (location, 0,
509c9d60 11141 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11142 }
11143 }
6aa3c60d 11144 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11145 {
11146 result_type = type0;
fcf73884 11147 if (pedantic)
c1771a20 11148 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11149 "ordered comparison of pointer with integer zero");
11150 else if (extra_warnings)
ba47d38d 11151 warning_at (location, OPT_Wextra,
d42ba3b8 11152 "ordered comparison of pointer with integer zero");
3e4093b6 11153 }
6aa3c60d 11154 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11155 {
11156 result_type = type1;
d42ba3b8 11157 if (pedantic)
c1771a20 11158 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11159 "ordered comparison of pointer with integer zero");
11160 else if (extra_warnings)
11161 warning_at (location, OPT_Wextra,
11162 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11163 }
11164 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11165 {
11166 result_type = type0;
ba47d38d 11167 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11168 }
11169 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11170 {
11171 result_type = type1;
ba47d38d 11172 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11173 }
04159acf
MP
11174 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11175 || truth_value_p (TREE_CODE (orig_op0)))
11176 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11177 || truth_value_p (TREE_CODE (orig_op1))))
11178 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11179 break;
64094f6a 11180
3e4093b6 11181 default:
37b2f290 11182 gcc_unreachable ();
c9fe6f9f 11183 }
8f17b5c5 11184
e57e265b
PB
11185 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11186 return error_mark_node;
11187
5bed876a
AH
11188 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11189 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11190 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11191 {
ba47d38d 11192 binary_op_error (location, code, type0, type1);
5bed876a
AH
11193 return error_mark_node;
11194 }
11195
3e4093b6 11196 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11197 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11198 &&
11199 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11200 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11201 {
2ca862e9
JM
11202 bool first_complex = (code0 == COMPLEX_TYPE);
11203 bool second_complex = (code1 == COMPLEX_TYPE);
11204 int none_complex = (!first_complex && !second_complex);
39b726dd 11205
3e4093b6 11206 if (shorten || common || short_compare)
3bf6bfcc
JJ
11207 {
11208 result_type = c_common_type (type0, type1);
0a0b3574
MM
11209 do_warn_double_promotion (result_type, type0, type1,
11210 "implicit conversion from %qT to %qT "
11211 "to match other operand of binary "
11212 "expression",
11213 location);
3bf6bfcc
JJ
11214 if (result_type == error_mark_node)
11215 return error_mark_node;
11216 }
400fbf9f 11217
2ca862e9
JM
11218 if (first_complex != second_complex
11219 && (code == PLUS_EXPR
11220 || code == MINUS_EXPR
11221 || code == MULT_EXPR
11222 || (code == TRUNC_DIV_EXPR && first_complex))
11223 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11224 && flag_signed_zeros)
11225 {
11226 /* An operation on mixed real/complex operands must be
11227 handled specially, but the language-independent code can
11228 more easily optimize the plain complex arithmetic if
11229 -fno-signed-zeros. */
11230 tree real_type = TREE_TYPE (result_type);
11231 tree real, imag;
11232 if (type0 != orig_type0 || type1 != orig_type1)
11233 {
11234 gcc_assert (may_need_excess_precision && common);
2d2e923f 11235 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11236 }
11237 if (first_complex)
11238 {
11239 if (TREE_TYPE (op0) != result_type)
68fca595 11240 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11241 if (TREE_TYPE (op1) != real_type)
68fca595 11242 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11243 }
11244 else
11245 {
11246 if (TREE_TYPE (op0) != real_type)
68fca595 11247 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11248 if (TREE_TYPE (op1) != result_type)
68fca595 11249 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11250 }
11251 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11252 return error_mark_node;
11253 if (first_complex)
11254 {
11255 op0 = c_save_expr (op0);
11256 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11257 op0, 1);
11258 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11259 op0, 1);
11260 switch (code)
11261 {
11262 case MULT_EXPR:
11263 case TRUNC_DIV_EXPR:
c4603e7c 11264 op1 = c_save_expr (op1);
2ca862e9
JM
11265 imag = build2 (resultcode, real_type, imag, op1);
11266 /* Fall through. */
11267 case PLUS_EXPR:
11268 case MINUS_EXPR:
11269 real = build2 (resultcode, real_type, real, op1);
11270 break;
11271 default:
11272 gcc_unreachable();
11273 }
11274 }
11275 else
11276 {
11277 op1 = c_save_expr (op1);
11278 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11279 op1, 1);
11280 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11281 op1, 1);
11282 switch (code)
11283 {
11284 case MULT_EXPR:
c4603e7c 11285 op0 = c_save_expr (op0);
2ca862e9
JM
11286 imag = build2 (resultcode, real_type, op0, imag);
11287 /* Fall through. */
11288 case PLUS_EXPR:
11289 real = build2 (resultcode, real_type, op0, real);
11290 break;
11291 case MINUS_EXPR:
11292 real = build2 (resultcode, real_type, op0, real);
11293 imag = build1 (NEGATE_EXPR, real_type, imag);
11294 break;
11295 default:
11296 gcc_unreachable();
11297 }
11298 }
11299 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11300 goto return_build_binary_op;
11301 }
11302
3e4093b6
RS
11303 /* For certain operations (which identify themselves by shorten != 0)
11304 if both args were extended from the same smaller type,
11305 do the arithmetic in that type and then extend.
400fbf9f 11306
3e4093b6
RS
11307 shorten !=0 and !=1 indicates a bitwise operation.
11308 For them, this optimization is safe only if
11309 both args are zero-extended or both are sign-extended.
11310 Otherwise, we might change the result.
11311 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11312 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11313
3e4093b6
RS
11314 if (shorten && none_complex)
11315 {
3e4093b6 11316 final_type = result_type;
b8698a0f 11317 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11318 shorten == -1);
3e4093b6 11319 }
88a3dbc1 11320
3e4093b6 11321 /* Shifts can be shortened if shifting right. */
2f6e4e97 11322
3e4093b6
RS
11323 if (short_shift)
11324 {
11325 int unsigned_arg;
11326 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11327
3e4093b6 11328 final_type = result_type;
abe80e6d 11329
3e4093b6 11330 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11331 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11332
3e4093b6 11333 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11334 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11335 /* We can shorten only if the shift count is less than the
11336 number of bits in the smaller type size. */
11337 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11338 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11339 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11340 {
11341 /* Do an unsigned shift if the operand was zero-extended. */
11342 result_type
11343 = c_common_signed_or_unsigned_type (unsigned_arg,
11344 TREE_TYPE (arg0));
11345 /* Convert value-to-be-shifted to that type. */
11346 if (TREE_TYPE (op0) != result_type)
11347 op0 = convert (result_type, op0);
11348 converted = 1;
abe80e6d 11349 }
88a3dbc1
RK
11350 }
11351
3e4093b6
RS
11352 /* Comparison operations are shortened too but differently.
11353 They identify themselves by setting short_compare = 1. */
56cb9733 11354
3e4093b6
RS
11355 if (short_compare)
11356 {
11357 /* Don't write &op0, etc., because that would prevent op0
11358 from being kept in a register.
11359 Instead, make copies of the our local variables and
11360 pass the copies by reference, then copy them back afterward. */
11361 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11362 enum tree_code xresultcode = resultcode;
11363 tree val
393e8e8b
MP
11364 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11365 &xresultcode);
8f17b5c5 11366
3e4093b6 11367 if (val != 0)
c9f9eb5d
AH
11368 {
11369 ret = val;
11370 goto return_build_binary_op;
11371 }
8f17b5c5 11372
3e4093b6
RS
11373 op0 = xop0, op1 = xop1;
11374 converted = 1;
11375 resultcode = xresultcode;
8f17b5c5 11376
7d882b83 11377 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11378 {
11379 bool op0_maybe_const = true;
11380 bool op1_maybe_const = true;
11381 tree orig_op0_folded, orig_op1_folded;
11382
11383 if (in_late_binary_op)
11384 {
11385 orig_op0_folded = orig_op0;
11386 orig_op1_folded = orig_op1;
11387 }
11388 else
11389 {
11390 /* Fold for the sake of possible warnings, as in
11391 build_conditional_expr. This requires the
11392 "original" values to be folded, not just op0 and
11393 op1. */
f5178456 11394 c_inhibit_evaluation_warnings++;
928c19bb
JM
11395 op0 = c_fully_fold (op0, require_constant_value,
11396 &op0_maybe_const);
11397 op1 = c_fully_fold (op1, require_constant_value,
11398 &op1_maybe_const);
f5178456 11399 c_inhibit_evaluation_warnings--;
928c19bb
JM
11400 orig_op0_folded = c_fully_fold (orig_op0,
11401 require_constant_value,
11402 NULL);
11403 orig_op1_folded = c_fully_fold (orig_op1,
11404 require_constant_value,
11405 NULL);
11406 }
11407
11408 if (warn_sign_compare)
11409 warn_for_sign_compare (location, orig_op0_folded,
11410 orig_op1_folded, op0, op1,
11411 result_type, resultcode);
5c2f94b4 11412 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11413 {
11414 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11415 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11416 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11417 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11418 }
3e4093b6 11419 }
2ad1815d 11420 }
64094f6a 11421 }
64094f6a 11422
3e4093b6
RS
11423 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11424 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11425 Then the expression will be built.
11426 It will be given type FINAL_TYPE if that is nonzero;
11427 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11428
3e4093b6
RS
11429 if (!result_type)
11430 {
ba47d38d 11431 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11432 return error_mark_node;
11433 }
400fbf9f 11434
3e4093b6 11435 if (build_type == NULL_TREE)
8ce94e44
JM
11436 {
11437 build_type = result_type;
180f8dbb
JM
11438 if ((type0 != orig_type0 || type1 != orig_type1)
11439 && !boolean_op)
8ce94e44
JM
11440 {
11441 gcc_assert (may_need_excess_precision && common);
2d2e923f 11442 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11443 }
11444 }
400fbf9f 11445
2d2e923f
MLI
11446 if (!converted)
11447 {
68fca595
MP
11448 op0 = ep_convert_and_check (location, result_type, op0,
11449 semantic_result_type);
11450 op1 = ep_convert_and_check (location, result_type, op1,
11451 semantic_result_type);
2d2e923f
MLI
11452
11453 /* This can happen if one operand has a vector type, and the other
11454 has a different type. */
11455 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11456 return error_mark_node;
11457 }
11458
f8ed5150
MP
11459 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11460 | SANITIZE_FLOAT_DIVIDE))
802ac282 11461 && do_ubsan_in_current_function ()
b2aaf235
MP
11462 && (doing_div_or_mod || doing_shift)
11463 && !require_constant_value)
de5a5fa1
MP
11464 {
11465 /* OP0 and/or OP1 might have side-effects. */
11466 op0 = c_save_expr (op0);
11467 op1 = c_save_expr (op1);
11468 op0 = c_fully_fold (op0, false, NULL);
11469 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11470 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11471 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11472 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11473 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11474 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11475 }
11476
c9f9eb5d 11477 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11478 if (int_const_or_overflow)
11479 ret = (require_constant_value
db3927fb
AH
11480 ? fold_build2_initializer_loc (location, resultcode, build_type,
11481 op0, op1)
11482 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11483 else
11484 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11485 if (final_type != 0)
11486 ret = convert (final_type, ret);
11487
11488 return_build_binary_op:
11489 gcc_assert (ret != error_mark_node);
928c19bb
JM
11490 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11491 ret = (int_operands
11492 ? note_integer_operands (ret)
11493 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11494 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11495 && !in_late_binary_op)
11496 ret = note_integer_operands (ret);
2d2e923f
MLI
11497 if (semantic_result_type)
11498 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11499 protected_set_expr_location (ret, location);
de5a5fa1 11500
a24d975c 11501 if (instrument_expr != NULL)
de5a5fa1
MP
11502 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11503 instrument_expr, ret);
11504
c9f9eb5d 11505 return ret;
400fbf9f 11506}
85498824
JM
11507
11508
11509/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11510 purpose. LOCATION is the source location for the expression. */
85498824
JM
11511
11512tree
ba47d38d 11513c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11514{
928c19bb
JM
11515 bool int_const, int_operands;
11516
85498824
JM
11517 switch (TREE_CODE (TREE_TYPE (expr)))
11518 {
11519 case ARRAY_TYPE:
ba47d38d 11520 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11521 return error_mark_node;
11522
11523 case RECORD_TYPE:
ba47d38d 11524 error_at (location, "used struct type value where scalar is required");
85498824
JM
11525 return error_mark_node;
11526
11527 case UNION_TYPE:
ba47d38d 11528 error_at (location, "used union type value where scalar is required");
85498824
JM
11529 return error_mark_node;
11530
04af8788
NP
11531 case VOID_TYPE:
11532 error_at (location, "void value not ignored as it ought to be");
11533 return error_mark_node;
11534
1807ffc1
MS
11535 case POINTER_TYPE:
11536 if (reject_gcc_builtin (expr))
11537 return error_mark_node;
11538 break;
11539
46bdb9cf
JM
11540 case FUNCTION_TYPE:
11541 gcc_unreachable ();
11542
d246ab4f
AS
11543 case VECTOR_TYPE:
11544 error_at (location, "used vector type where scalar is required");
11545 return error_mark_node;
11546
85498824
JM
11547 default:
11548 break;
11549 }
11550
928c19bb
JM
11551 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11552 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11553 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11554 {
11555 expr = remove_c_maybe_const_expr (expr);
11556 expr = build2 (NE_EXPR, integer_type_node, expr,
11557 convert (TREE_TYPE (expr), integer_zero_node));
11558 expr = note_integer_operands (expr);
11559 }
11560 else
11561 /* ??? Should we also give an error for vectors rather than leaving
11562 those to give errors later? */
11563 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11564
11565 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11566 {
11567 if (TREE_OVERFLOW (expr))
11568 return expr;
11569 else
11570 return note_integer_operands (expr);
11571 }
11572 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11573 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11574 return expr;
85498824 11575}
73f397d4
JM
11576\f
11577
11578/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11579 required. */
11580
11581tree
51eed280 11582c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11583{
11584 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11585 {
11586 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11587 /* Executing a compound literal inside a function reinitializes
11588 it. */
11589 if (!TREE_STATIC (decl))
11590 *se = true;
11591 return decl;
11592 }
11593 else
11594 return expr;
11595}
953ff289 11596\f
88bae6f4
TS
11597/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11598 statement. LOC is the location of the construct. */
41dbbb37
TS
11599
11600tree
88bae6f4
TS
11601c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11602 tree clauses)
41dbbb37 11603{
88bae6f4 11604 body = c_end_compound_stmt (loc, body, true);
41dbbb37 11605
88bae6f4 11606 tree stmt = make_node (code);
41dbbb37 11607 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
11608 OMP_BODY (stmt) = body;
11609 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
11610 SET_EXPR_LOCATION (stmt, loc);
11611
11612 return add_stmt (stmt);
11613}
11614
11615/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11616 statement. LOC is the location of the OACC_DATA. */
11617
11618tree
11619c_finish_oacc_data (location_t loc, tree clauses, tree block)
11620{
11621 tree stmt;
11622
11623 block = c_end_compound_stmt (loc, block, true);
11624
11625 stmt = make_node (OACC_DATA);
11626 TREE_TYPE (stmt) = void_type_node;
11627 OACC_DATA_CLAUSES (stmt) = clauses;
11628 OACC_DATA_BODY (stmt) = block;
11629 SET_EXPR_LOCATION (stmt, loc);
11630
11631 return add_stmt (stmt);
11632}
11633
37d5ad46
JB
11634/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11635 statement. LOC is the location of the OACC_HOST_DATA. */
11636
11637tree
11638c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11639{
11640 tree stmt;
11641
11642 block = c_end_compound_stmt (loc, block, true);
11643
11644 stmt = make_node (OACC_HOST_DATA);
11645 TREE_TYPE (stmt) = void_type_node;
11646 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11647 OACC_HOST_DATA_BODY (stmt) = block;
11648 SET_EXPR_LOCATION (stmt, loc);
11649
11650 return add_stmt (stmt);
11651}
11652
c0220ea4 11653/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11654
11655tree
11656c_begin_omp_parallel (void)
11657{
11658 tree block;
11659
11660 keep_next_level ();
11661 block = c_begin_compound_stmt (true);
11662
11663 return block;
11664}
11665
c2255bc4
AH
11666/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11667 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11668
953ff289 11669tree
c2255bc4 11670c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11671{
11672 tree stmt;
11673
c2255bc4 11674 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11675
11676 stmt = make_node (OMP_PARALLEL);
11677 TREE_TYPE (stmt) = void_type_node;
11678 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11679 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11680 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11681
11682 return add_stmt (stmt);
11683}
11684
a68ab351
JJ
11685/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11686
11687tree
11688c_begin_omp_task (void)
11689{
11690 tree block;
11691
11692 keep_next_level ();
11693 block = c_begin_compound_stmt (true);
11694
11695 return block;
11696}
11697
c2255bc4
AH
11698/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11699 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11700
11701tree
c2255bc4 11702c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11703{
11704 tree stmt;
11705
c2255bc4 11706 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11707
11708 stmt = make_node (OMP_TASK);
11709 TREE_TYPE (stmt) = void_type_node;
11710 OMP_TASK_CLAUSES (stmt) = clauses;
11711 OMP_TASK_BODY (stmt) = block;
c2255bc4 11712 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11713
11714 return add_stmt (stmt);
11715}
11716
acf0174b
JJ
11717/* Generate GOMP_cancel call for #pragma omp cancel. */
11718
11719void
11720c_finish_omp_cancel (location_t loc, tree clauses)
11721{
11722 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11723 int mask = 0;
11724 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11725 mask = 1;
11726 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11727 mask = 2;
11728 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11729 mask = 4;
11730 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11731 mask = 8;
11732 else
11733 {
11734 error_at (loc, "%<#pragma omp cancel must specify one of "
11735 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11736 "clauses");
11737 return;
11738 }
11739 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11740 if (ifc != NULL_TREE)
11741 {
11742 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11743 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11744 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11745 build_zero_cst (type));
11746 }
11747 else
11748 ifc = boolean_true_node;
11749 tree stmt = build_call_expr_loc (loc, fn, 2,
11750 build_int_cst (integer_type_node, mask),
11751 ifc);
11752 add_stmt (stmt);
11753}
11754
11755/* Generate GOMP_cancellation_point call for
11756 #pragma omp cancellation point. */
11757
11758void
11759c_finish_omp_cancellation_point (location_t loc, tree clauses)
11760{
11761 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11762 int mask = 0;
11763 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11764 mask = 1;
11765 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11766 mask = 2;
11767 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11768 mask = 4;
11769 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11770 mask = 8;
11771 else
11772 {
11773 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11774 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11775 "clauses");
11776 return;
11777 }
11778 tree stmt = build_call_expr_loc (loc, fn, 1,
11779 build_int_cst (integer_type_node, mask));
11780 add_stmt (stmt);
11781}
11782
11783/* Helper function for handle_omp_array_sections. Called recursively
11784 to handle multiple array-section-subscripts. C is the clause,
11785 T current expression (initially OMP_CLAUSE_DECL), which is either
11786 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11787 expression if specified, TREE_VALUE length expression if specified,
11788 TREE_CHAIN is what it has been specified after, or some decl.
11789 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11790 set to true if any of the array-section-subscript could have length
11791 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11792 first array-section-subscript which is known not to have length
11793 of one. Given say:
11794 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11795 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11796 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 11797 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
11798 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11799 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11800 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11801 case though, as some lengths could be zero. */
11802
11803static tree
11804handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32
JJ
11805 bool &maybe_zero_len, unsigned int &first_non_one,
11806 bool is_omp)
acf0174b
JJ
11807{
11808 tree ret, low_bound, length, type;
11809 if (TREE_CODE (t) != TREE_LIST)
11810 {
7a0ca710 11811 if (error_operand_p (t))
acf0174b 11812 return error_mark_node;
d9a6bd32
JJ
11813 ret = t;
11814 if (TREE_CODE (t) == COMPONENT_REF
11815 && is_omp
11816 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
11817 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
11818 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
11819 {
11820 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
11821 {
11822 error_at (OMP_CLAUSE_LOCATION (c),
11823 "bit-field %qE in %qs clause",
11824 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11825 return error_mark_node;
11826 }
11827 while (TREE_CODE (t) == COMPONENT_REF)
11828 {
11829 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
11830 {
11831 error_at (OMP_CLAUSE_LOCATION (c),
11832 "%qE is a member of a union", t);
11833 return error_mark_node;
11834 }
11835 t = TREE_OPERAND (t, 0);
11836 }
11837 }
0ae9bd27 11838 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
11839 {
11840 if (DECL_P (t))
11841 error_at (OMP_CLAUSE_LOCATION (c),
11842 "%qD is not a variable in %qs clause", t,
11843 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11844 else
11845 error_at (OMP_CLAUSE_LOCATION (c),
11846 "%qE is not a variable in %qs clause", t,
11847 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11848 return error_mark_node;
11849 }
11850 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0ae9bd27 11851 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
11852 {
11853 error_at (OMP_CLAUSE_LOCATION (c),
11854 "%qD is threadprivate variable in %qs clause", t,
11855 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11856 return error_mark_node;
11857 }
d9a6bd32 11858 return ret;
acf0174b
JJ
11859 }
11860
11861 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
d9a6bd32 11862 maybe_zero_len, first_non_one, is_omp);
acf0174b
JJ
11863 if (ret == error_mark_node || ret == NULL_TREE)
11864 return ret;
11865
11866 type = TREE_TYPE (ret);
11867 low_bound = TREE_PURPOSE (t);
11868 length = TREE_VALUE (t);
11869
11870 if (low_bound == error_mark_node || length == error_mark_node)
11871 return error_mark_node;
11872
11873 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11874 {
11875 error_at (OMP_CLAUSE_LOCATION (c),
11876 "low bound %qE of array section does not have integral type",
11877 low_bound);
11878 return error_mark_node;
11879 }
11880 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11881 {
11882 error_at (OMP_CLAUSE_LOCATION (c),
11883 "length %qE of array section does not have integral type",
11884 length);
11885 return error_mark_node;
11886 }
11887 if (low_bound
11888 && TREE_CODE (low_bound) == INTEGER_CST
11889 && TYPE_PRECISION (TREE_TYPE (low_bound))
11890 > TYPE_PRECISION (sizetype))
11891 low_bound = fold_convert (sizetype, low_bound);
11892 if (length
11893 && TREE_CODE (length) == INTEGER_CST
11894 && TYPE_PRECISION (TREE_TYPE (length))
11895 > TYPE_PRECISION (sizetype))
11896 length = fold_convert (sizetype, length);
11897 if (low_bound == NULL_TREE)
11898 low_bound = integer_zero_node;
11899
11900 if (length != NULL_TREE)
11901 {
11902 if (!integer_nonzerop (length))
d9a6bd32
JJ
11903 {
11904 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11905 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11906 {
11907 if (integer_zerop (length))
11908 {
11909 error_at (OMP_CLAUSE_LOCATION (c),
11910 "zero length array section in %qs clause",
11911 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11912 return error_mark_node;
11913 }
11914 }
11915 else
11916 maybe_zero_len = true;
11917 }
acf0174b
JJ
11918 if (first_non_one == types.length ()
11919 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11920 first_non_one++;
11921 }
11922 if (TREE_CODE (type) == ARRAY_TYPE)
11923 {
11924 if (length == NULL_TREE
11925 && (TYPE_DOMAIN (type) == NULL_TREE
11926 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11927 {
11928 error_at (OMP_CLAUSE_LOCATION (c),
11929 "for unknown bound array type length expression must "
11930 "be specified");
11931 return error_mark_node;
11932 }
11933 if (TREE_CODE (low_bound) == INTEGER_CST
11934 && tree_int_cst_sgn (low_bound) == -1)
11935 {
11936 error_at (OMP_CLAUSE_LOCATION (c),
11937 "negative low bound in array section in %qs clause",
11938 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11939 return error_mark_node;
11940 }
11941 if (length != NULL_TREE
11942 && TREE_CODE (length) == INTEGER_CST
11943 && tree_int_cst_sgn (length) == -1)
11944 {
11945 error_at (OMP_CLAUSE_LOCATION (c),
11946 "negative length in array section in %qs clause",
11947 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11948 return error_mark_node;
11949 }
11950 if (TYPE_DOMAIN (type)
11951 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11952 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11953 == INTEGER_CST)
11954 {
11955 tree size = size_binop (PLUS_EXPR,
11956 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11957 size_one_node);
11958 if (TREE_CODE (low_bound) == INTEGER_CST)
11959 {
11960 if (tree_int_cst_lt (size, low_bound))
11961 {
11962 error_at (OMP_CLAUSE_LOCATION (c),
11963 "low bound %qE above array section size "
11964 "in %qs clause", low_bound,
11965 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11966 return error_mark_node;
11967 }
11968 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
11969 {
11970 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
11971 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
11972 {
11973 error_at (OMP_CLAUSE_LOCATION (c),
11974 "zero length array section in %qs clause",
11975 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11976 return error_mark_node;
11977 }
11978 maybe_zero_len = true;
11979 }
acf0174b
JJ
11980 else if (length == NULL_TREE
11981 && first_non_one == types.length ()
11982 && tree_int_cst_equal
11983 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11984 low_bound))
11985 first_non_one++;
11986 }
11987 else if (length == NULL_TREE)
11988 {
d9a6bd32
JJ
11989 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11990 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
11991 maybe_zero_len = true;
acf0174b
JJ
11992 if (first_non_one == types.length ())
11993 first_non_one++;
11994 }
11995 if (length && TREE_CODE (length) == INTEGER_CST)
11996 {
11997 if (tree_int_cst_lt (size, length))
11998 {
11999 error_at (OMP_CLAUSE_LOCATION (c),
12000 "length %qE above array section size "
12001 "in %qs clause", length,
12002 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12003 return error_mark_node;
12004 }
12005 if (TREE_CODE (low_bound) == INTEGER_CST)
12006 {
12007 tree lbpluslen
12008 = size_binop (PLUS_EXPR,
12009 fold_convert (sizetype, low_bound),
12010 fold_convert (sizetype, length));
12011 if (TREE_CODE (lbpluslen) == INTEGER_CST
12012 && tree_int_cst_lt (size, lbpluslen))
12013 {
12014 error_at (OMP_CLAUSE_LOCATION (c),
12015 "high bound %qE above array section size "
12016 "in %qs clause", lbpluslen,
12017 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12018 return error_mark_node;
12019 }
12020 }
12021 }
12022 }
12023 else if (length == NULL_TREE)
12024 {
d9a6bd32
JJ
12025 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12026 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12027 maybe_zero_len = true;
acf0174b
JJ
12028 if (first_non_one == types.length ())
12029 first_non_one++;
12030 }
12031
12032 /* For [lb:] we will need to evaluate lb more than once. */
12033 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12034 {
12035 tree lb = c_save_expr (low_bound);
12036 if (lb != low_bound)
12037 {
12038 TREE_PURPOSE (t) = lb;
12039 low_bound = lb;
12040 }
12041 }
12042 }
12043 else if (TREE_CODE (type) == POINTER_TYPE)
12044 {
12045 if (length == NULL_TREE)
12046 {
12047 error_at (OMP_CLAUSE_LOCATION (c),
12048 "for pointer type length expression must be specified");
12049 return error_mark_node;
12050 }
d9a6bd32
JJ
12051 if (length != NULL_TREE
12052 && TREE_CODE (length) == INTEGER_CST
12053 && tree_int_cst_sgn (length) == -1)
12054 {
12055 error_at (OMP_CLAUSE_LOCATION (c),
12056 "negative length in array section in %qs clause",
12057 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12058 return error_mark_node;
12059 }
acf0174b
JJ
12060 /* If there is a pointer type anywhere but in the very first
12061 array-section-subscript, the array section can't be contiguous. */
12062 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12063 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12064 {
12065 error_at (OMP_CLAUSE_LOCATION (c),
12066 "array section is not contiguous in %qs clause",
12067 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12068 return error_mark_node;
12069 }
12070 }
12071 else
12072 {
12073 error_at (OMP_CLAUSE_LOCATION (c),
12074 "%qE does not have pointer or array type", ret);
12075 return error_mark_node;
12076 }
12077 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12078 types.safe_push (TREE_TYPE (ret));
12079 /* We will need to evaluate lb more than once. */
12080 tree lb = c_save_expr (low_bound);
12081 if (lb != low_bound)
12082 {
12083 TREE_PURPOSE (t) = lb;
12084 low_bound = lb;
12085 }
12086 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12087 return ret;
12088}
12089
12090/* Handle array sections for clause C. */
12091
12092static bool
d9a6bd32 12093handle_omp_array_sections (tree c, bool is_omp)
acf0174b
JJ
12094{
12095 bool maybe_zero_len = false;
12096 unsigned int first_non_one = 0;
d9a6bd32 12097 auto_vec<tree, 10> types;
acf0174b 12098 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32
JJ
12099 maybe_zero_len, first_non_one,
12100 is_omp);
acf0174b 12101 if (first == error_mark_node)
d9a6bd32 12102 return true;
acf0174b 12103 if (first == NULL_TREE)
d9a6bd32 12104 return false;
acf0174b
JJ
12105 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12106 {
12107 tree t = OMP_CLAUSE_DECL (c);
12108 tree tem = NULL_TREE;
acf0174b
JJ
12109 /* Need to evaluate side effects in the length expressions
12110 if any. */
12111 while (TREE_CODE (t) == TREE_LIST)
12112 {
12113 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12114 {
12115 if (tem == NULL_TREE)
12116 tem = TREE_VALUE (t);
12117 else
12118 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12119 TREE_VALUE (t), tem);
12120 }
12121 t = TREE_CHAIN (t);
12122 }
12123 if (tem)
12124 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12125 first = c_fully_fold (first, false, NULL);
12126 OMP_CLAUSE_DECL (c) = first;
12127 }
12128 else
12129 {
12130 unsigned int num = types.length (), i;
12131 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12132 tree condition = NULL_TREE;
12133
12134 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12135 maybe_zero_len = true;
12136
12137 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12138 t = TREE_CHAIN (t))
12139 {
12140 tree low_bound = TREE_PURPOSE (t);
12141 tree length = TREE_VALUE (t);
12142
12143 i--;
12144 if (low_bound
12145 && TREE_CODE (low_bound) == INTEGER_CST
12146 && TYPE_PRECISION (TREE_TYPE (low_bound))
12147 > TYPE_PRECISION (sizetype))
12148 low_bound = fold_convert (sizetype, low_bound);
12149 if (length
12150 && TREE_CODE (length) == INTEGER_CST
12151 && TYPE_PRECISION (TREE_TYPE (length))
12152 > TYPE_PRECISION (sizetype))
12153 length = fold_convert (sizetype, length);
12154 if (low_bound == NULL_TREE)
12155 low_bound = integer_zero_node;
12156 if (!maybe_zero_len && i > first_non_one)
12157 {
12158 if (integer_nonzerop (low_bound))
12159 goto do_warn_noncontiguous;
12160 if (length != NULL_TREE
12161 && TREE_CODE (length) == INTEGER_CST
12162 && TYPE_DOMAIN (types[i])
12163 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12164 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12165 == INTEGER_CST)
12166 {
12167 tree size;
12168 size = size_binop (PLUS_EXPR,
12169 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12170 size_one_node);
12171 if (!tree_int_cst_equal (length, size))
12172 {
12173 do_warn_noncontiguous:
12174 error_at (OMP_CLAUSE_LOCATION (c),
12175 "array section is not contiguous in %qs "
12176 "clause",
12177 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12178 return true;
12179 }
12180 }
12181 if (length != NULL_TREE
12182 && TREE_SIDE_EFFECTS (length))
12183 {
12184 if (side_effects == NULL_TREE)
12185 side_effects = length;
12186 else
12187 side_effects = build2 (COMPOUND_EXPR,
12188 TREE_TYPE (side_effects),
12189 length, side_effects);
12190 }
12191 }
12192 else
12193 {
12194 tree l;
12195
d9a6bd32
JJ
12196 if (i > first_non_one
12197 && ((length && integer_nonzerop (length))
12198 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12199 continue;
12200 if (length)
12201 l = fold_convert (sizetype, length);
12202 else
12203 {
12204 l = size_binop (PLUS_EXPR,
12205 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12206 size_one_node);
12207 l = size_binop (MINUS_EXPR, l,
12208 fold_convert (sizetype, low_bound));
12209 }
12210 if (i > first_non_one)
12211 {
12212 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12213 size_zero_node);
12214 if (condition == NULL_TREE)
12215 condition = l;
12216 else
12217 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12218 l, condition);
12219 }
12220 else if (size == NULL_TREE)
12221 {
12222 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
12223 tree eltype = TREE_TYPE (types[num - 1]);
12224 while (TREE_CODE (eltype) == ARRAY_TYPE)
12225 eltype = TREE_TYPE (eltype);
12226 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12227 {
12228 if (integer_zerop (size)
12229 || integer_zerop (size_in_bytes (eltype)))
12230 {
12231 error_at (OMP_CLAUSE_LOCATION (c),
12232 "zero length array section in %qs clause",
12233 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12234 return error_mark_node;
12235 }
12236 size = size_binop (EXACT_DIV_EXPR, size,
12237 size_in_bytes (eltype));
12238 }
acf0174b
JJ
12239 size = size_binop (MULT_EXPR, size, l);
12240 if (condition)
12241 size = fold_build3 (COND_EXPR, sizetype, condition,
12242 size, size_zero_node);
12243 }
12244 else
12245 size = size_binop (MULT_EXPR, size, l);
12246 }
12247 }
acf0174b
JJ
12248 if (side_effects)
12249 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
12250 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12251 {
12252 size = size_binop (MINUS_EXPR, size, size_one_node);
12253 size = c_fully_fold (size, false, NULL);
12254 tree index_type = build_index_type (size);
12255 tree eltype = TREE_TYPE (first);
12256 while (TREE_CODE (eltype) == ARRAY_TYPE)
12257 eltype = TREE_TYPE (eltype);
12258 tree type = build_array_type (eltype, index_type);
12259 tree ptype = build_pointer_type (eltype);
12260 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12261 t = build_fold_addr_expr (t);
e01d41e5
JJ
12262 tree t2 = build_fold_addr_expr (first);
12263 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12264 ptrdiff_type_node, t2);
12265 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12266 ptrdiff_type_node, t2,
12267 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12268 ptrdiff_type_node, t));
12269 t2 = c_fully_fold (t2, false, NULL);
12270 if (tree_fits_shwi_p (t2))
12271 t = build2 (MEM_REF, type, t,
12272 build_int_cst (ptype, tree_to_shwi (t2)));
12273 else
12274 {
12275 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12276 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12277 TREE_TYPE (t), t, t2);
12278 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12279 }
d9a6bd32
JJ
12280 OMP_CLAUSE_DECL (c) = t;
12281 return false;
12282 }
acf0174b
JJ
12283 first = c_fully_fold (first, false, NULL);
12284 OMP_CLAUSE_DECL (c) = first;
12285 if (size)
12286 size = c_fully_fold (size, false, NULL);
12287 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
12288 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12289 || (TREE_CODE (t) == COMPONENT_REF
12290 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 12291 return false;
41dbbb37 12292 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
d9a6bd32
JJ
12293 if (is_omp)
12294 switch (OMP_CLAUSE_MAP_KIND (c))
12295 {
12296 case GOMP_MAP_ALLOC:
12297 case GOMP_MAP_TO:
12298 case GOMP_MAP_FROM:
12299 case GOMP_MAP_TOFROM:
12300 case GOMP_MAP_ALWAYS_TO:
12301 case GOMP_MAP_ALWAYS_FROM:
12302 case GOMP_MAP_ALWAYS_TOFROM:
12303 case GOMP_MAP_RELEASE:
12304 case GOMP_MAP_DELETE:
12305 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12306 break;
12307 default:
12308 break;
12309 }
acf0174b 12310 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e01d41e5
JJ
12311 if (!is_omp)
12312 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12313 else if (TREE_CODE (t) == COMPONENT_REF)
12314 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12315 else
12316 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12317 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12318 && !c_mark_addressable (t))
acf0174b
JJ
12319 return false;
12320 OMP_CLAUSE_DECL (c2) = t;
12321 t = build_fold_addr_expr (first);
12322 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12323 tree ptr = OMP_CLAUSE_DECL (c2);
12324 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12325 ptr = build_fold_addr_expr (ptr);
12326 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12327 ptrdiff_type_node, t,
12328 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12329 ptrdiff_type_node, ptr));
12330 t = c_fully_fold (t, false, NULL);
12331 OMP_CLAUSE_SIZE (c2) = t;
12332 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12333 OMP_CLAUSE_CHAIN (c) = c2;
12334 }
12335 return false;
12336}
12337
12338/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12339 an inline call. But, remap
12340 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12341 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12342
12343static tree
12344c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12345 tree decl, tree placeholder)
12346{
12347 copy_body_data id;
b787e7a2 12348 hash_map<tree, tree> decl_map;
acf0174b 12349
b787e7a2
TS
12350 decl_map.put (omp_decl1, placeholder);
12351 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12352 memset (&id, 0, sizeof (id));
12353 id.src_fn = DECL_CONTEXT (omp_decl1);
12354 id.dst_fn = current_function_decl;
12355 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12356 id.decl_map = &decl_map;
acf0174b
JJ
12357
12358 id.copy_decl = copy_decl_no_change;
12359 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12360 id.transform_new_cfg = true;
12361 id.transform_return_to_modify = false;
12362 id.transform_lang_insert_block = NULL;
12363 id.eh_lp_nr = 0;
12364 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12365 return stmt;
12366}
12367
12368/* Helper function of c_finish_omp_clauses, called via walk_tree.
12369 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12370
12371static tree
12372c_find_omp_placeholder_r (tree *tp, int *, void *data)
12373{
12374 if (*tp == (tree) data)
12375 return *tp;
12376 return NULL_TREE;
12377}
12378
41dbbb37 12379/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12380 Remove any elements from the list that are invalid. */
12381
12382tree
d9a6bd32 12383c_finish_omp_clauses (tree clauses, bool is_omp, bool declare_simd)
953ff289
DN
12384{
12385 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e01d41e5 12386 bitmap_head aligned_head, map_head, map_field_head;
d9a6bd32
JJ
12387 tree c, t, type, *pc;
12388 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
12389 bool branch_seen = false;
12390 bool copyprivate_seen = false;
e01d41e5 12391 bool linear_variable_step_check = false;
acf0174b 12392 tree *nowait_clause = NULL;
e01d41e5
JJ
12393 bool ordered_seen = false;
12394 tree schedule_clause = NULL_TREE;
953ff289
DN
12395
12396 bitmap_obstack_initialize (NULL);
12397 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12398 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12399 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12400 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
12401 bitmap_initialize (&map_head, &bitmap_default_obstack);
12402 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
953ff289
DN
12403
12404 for (pc = &clauses, c = clauses; c ; c = *pc)
12405 {
12406 bool remove = false;
12407 bool need_complete = false;
12408 bool need_implicitly_determined = false;
12409
aaf46ef9 12410 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12411 {
12412 case OMP_CLAUSE_SHARED:
953ff289
DN
12413 need_implicitly_determined = true;
12414 goto check_dup_generic;
12415
12416 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12417 need_complete = true;
12418 need_implicitly_determined = true;
12419 goto check_dup_generic;
12420
12421 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12422 need_implicitly_determined = true;
12423 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12424 if (TREE_CODE (t) == TREE_LIST)
12425 {
12426 if (handle_omp_array_sections (c, is_omp))
12427 {
12428 remove = true;
12429 break;
12430 }
12431
12432 t = OMP_CLAUSE_DECL (c);
12433 }
12434 t = require_complete_type (t);
12435 if (t == error_mark_node)
12436 {
12437 remove = true;
12438 break;
12439 }
12440 type = TREE_TYPE (t);
12441 if (TREE_CODE (t) == MEM_REF)
12442 type = TREE_TYPE (type);
12443 if (TREE_CODE (type) == ARRAY_TYPE)
12444 {
12445 tree oatype = type;
12446 gcc_assert (TREE_CODE (t) != MEM_REF);
12447 while (TREE_CODE (type) == ARRAY_TYPE)
12448 type = TREE_TYPE (type);
12449 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12450 {
12451 error_at (OMP_CLAUSE_LOCATION (c),
12452 "%qD in %<reduction%> clause is a zero size array",
12453 t);
12454 remove = true;
12455 break;
12456 }
12457 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12458 TYPE_SIZE_UNIT (type));
12459 if (integer_zerop (size))
12460 {
12461 error_at (OMP_CLAUSE_LOCATION (c),
12462 "%qD in %<reduction%> clause is a zero size array",
12463 t);
12464 remove = true;
12465 break;
12466 }
12467 size = size_binop (MINUS_EXPR, size, size_one_node);
12468 tree index_type = build_index_type (size);
12469 tree atype = build_array_type (type, index_type);
12470 tree ptype = build_pointer_type (type);
12471 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12472 t = build_fold_addr_expr (t);
12473 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12474 OMP_CLAUSE_DECL (c) = t;
12475 }
acf0174b 12476 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
12477 && (FLOAT_TYPE_P (type)
12478 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
12479 {
12480 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12481 const char *r_name = NULL;
12482
12483 switch (r_code)
12484 {
12485 case PLUS_EXPR:
12486 case MULT_EXPR:
12487 case MINUS_EXPR:
652fea39 12488 break;
20906c66 12489 case MIN_EXPR:
d9a6bd32 12490 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
12491 r_name = "min";
12492 break;
20906c66 12493 case MAX_EXPR:
d9a6bd32 12494 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 12495 r_name = "max";
953ff289
DN
12496 break;
12497 case BIT_AND_EXPR:
12498 r_name = "&";
12499 break;
12500 case BIT_XOR_EXPR:
12501 r_name = "^";
12502 break;
12503 case BIT_IOR_EXPR:
12504 r_name = "|";
12505 break;
12506 case TRUTH_ANDIF_EXPR:
d9a6bd32 12507 if (FLOAT_TYPE_P (type))
652fea39 12508 r_name = "&&";
953ff289
DN
12509 break;
12510 case TRUTH_ORIF_EXPR:
d9a6bd32 12511 if (FLOAT_TYPE_P (type))
652fea39 12512 r_name = "||";
953ff289
DN
12513 break;
12514 default:
12515 gcc_unreachable ();
12516 }
12517 if (r_name)
12518 {
c2255bc4
AH
12519 error_at (OMP_CLAUSE_LOCATION (c),
12520 "%qE has invalid type for %<reduction(%s)%>",
12521 t, r_name);
953ff289 12522 remove = true;
ee1d5a02 12523 break;
953ff289
DN
12524 }
12525 }
acf0174b
JJ
12526 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12527 {
12528 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 12529 "user defined reduction not found for %qE", t);
acf0174b 12530 remove = true;
ee1d5a02 12531 break;
acf0174b
JJ
12532 }
12533 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12534 {
12535 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 12536 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
12537 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12538 VAR_DECL, NULL_TREE, type);
d9a6bd32 12539 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
12540 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12541 DECL_ARTIFICIAL (placeholder) = 1;
12542 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
12543 if (TREE_CODE (t) == MEM_REF)
12544 {
12545 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12546 VAR_DECL, NULL_TREE, type);
12547 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12548 DECL_ARTIFICIAL (decl_placeholder) = 1;
12549 DECL_IGNORED_P (decl_placeholder) = 1;
12550 }
acf0174b
JJ
12551 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12552 c_mark_addressable (placeholder);
12553 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
12554 c_mark_addressable (decl_placeholder ? decl_placeholder
12555 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12556 OMP_CLAUSE_REDUCTION_MERGE (c)
12557 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12558 TREE_VEC_ELT (list, 0),
12559 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
12560 decl_placeholder ? decl_placeholder
12561 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
12562 OMP_CLAUSE_REDUCTION_MERGE (c)
12563 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12564 void_type_node, NULL_TREE,
d9a6bd32 12565 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
12566 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12567 if (TREE_VEC_LENGTH (list) == 6)
12568 {
12569 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
12570 c_mark_addressable (decl_placeholder ? decl_placeholder
12571 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
12572 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12573 c_mark_addressable (placeholder);
12574 tree init = TREE_VEC_ELT (list, 5);
12575 if (init == error_mark_node)
12576 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12577 OMP_CLAUSE_REDUCTION_INIT (c)
12578 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12579 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
12580 decl_placeholder ? decl_placeholder
12581 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 12582 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
12583 {
12584 tree v = decl_placeholder ? decl_placeholder : t;
12585 OMP_CLAUSE_REDUCTION_INIT (c)
12586 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12587 OMP_CLAUSE_REDUCTION_INIT (c));
12588 }
acf0174b
JJ
12589 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12590 c_find_omp_placeholder_r,
12591 placeholder, NULL))
12592 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12593 }
12594 else
12595 {
12596 tree init;
d9a6bd32
JJ
12597 tree v = decl_placeholder ? decl_placeholder : t;
12598 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12599 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 12600 else
d9a6bd32 12601 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 12602 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 12603 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
12604 }
12605 OMP_CLAUSE_REDUCTION_INIT (c)
12606 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12607 void_type_node, NULL_TREE,
12608 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12609 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12610 }
d9a6bd32
JJ
12611 if (TREE_CODE (t) == MEM_REF)
12612 {
12613 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12614 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12615 != INTEGER_CST)
12616 {
12617 sorry ("variable length element type in array "
12618 "%<reduction%> clause");
12619 remove = true;
12620 break;
12621 }
12622 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
12623 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12624 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
12625 if (TREE_CODE (t) == ADDR_EXPR)
12626 t = TREE_OPERAND (t, 0);
12627 }
12628 goto check_dup_generic_t;
953ff289
DN
12629
12630 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12631 copyprivate_seen = true;
12632 if (nowait_clause)
12633 {
12634 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12635 "%<nowait%> clause must not be used together "
12636 "with %<copyprivate%>");
12637 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12638 nowait_clause = NULL;
12639 }
953ff289
DN
12640 goto check_dup_generic;
12641
12642 case OMP_CLAUSE_COPYIN:
953ff289 12643 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12644 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12645 {
c2255bc4
AH
12646 error_at (OMP_CLAUSE_LOCATION (c),
12647 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12648 remove = true;
ee1d5a02 12649 break;
953ff289
DN
12650 }
12651 goto check_dup_generic;
12652
acf0174b 12653 case OMP_CLAUSE_LINEAR:
d9a6bd32
JJ
12654 if (!declare_simd)
12655 need_implicitly_determined = true;
acf0174b 12656 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12657 if (!declare_simd
12658 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
12659 {
12660 error_at (OMP_CLAUSE_LOCATION (c),
12661 "modifier should not be specified in %<linear%> "
12662 "clause on %<simd%> or %<for%> constructs");
12663 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
12664 }
acf0174b
JJ
12665 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12666 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12667 {
12668 error_at (OMP_CLAUSE_LOCATION (c),
12669 "linear clause applied to non-integral non-pointer "
12670 "variable with type %qT", TREE_TYPE (t));
12671 remove = true;
12672 break;
12673 }
e01d41e5
JJ
12674 if (declare_simd)
12675 {
12676 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12677 if (TREE_CODE (s) == PARM_DECL)
12678 {
12679 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
12680 /* map_head bitmap is used as uniform_head if
12681 declare_simd. */
12682 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
12683 linear_variable_step_check = true;
12684 goto check_dup_generic;
12685 }
12686 if (TREE_CODE (s) != INTEGER_CST)
12687 {
12688 error_at (OMP_CLAUSE_LOCATION (c),
12689 "%<linear%> clause step %qE is neither constant "
12690 "nor a parameter", s);
12691 remove = true;
12692 break;
12693 }
12694 }
acf0174b
JJ
12695 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12696 {
12697 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12698 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12699 OMP_CLAUSE_DECL (c), s);
12700 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12701 sizetype, fold_convert (sizetype, s),
12702 fold_convert
12703 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
12704 if (s == error_mark_node)
12705 s = size_one_node;
12706 OMP_CLAUSE_LINEAR_STEP (c) = s;
12707 }
da6f124d
JJ
12708 else
12709 OMP_CLAUSE_LINEAR_STEP (c)
12710 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12711 goto check_dup_generic;
12712
953ff289
DN
12713 check_dup_generic:
12714 t = OMP_CLAUSE_DECL (c);
d9a6bd32 12715 check_dup_generic_t:
0ae9bd27 12716 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12717 {
c2255bc4 12718 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12719 "%qE is not a variable in clause %qs", t,
12720 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12721 remove = true;
12722 }
12723 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12724 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12725 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12726 {
c2255bc4
AH
12727 error_at (OMP_CLAUSE_LOCATION (c),
12728 "%qE appears more than once in data clauses", t);
953ff289
DN
12729 remove = true;
12730 }
e01d41e5
JJ
12731 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
12732 && bitmap_bit_p (&map_head, DECL_UID (t)))
12733 {
12734 error ("%qD appears both in data and map clauses", t);
12735 remove = true;
12736 }
953ff289
DN
12737 else
12738 bitmap_set_bit (&generic_head, DECL_UID (t));
12739 break;
12740
12741 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12742 t = OMP_CLAUSE_DECL (c);
12743 need_complete = true;
12744 need_implicitly_determined = true;
0ae9bd27 12745 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12746 {
c2255bc4
AH
12747 error_at (OMP_CLAUSE_LOCATION (c),
12748 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12749 remove = true;
12750 }
12751 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12752 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12753 {
c2255bc4
AH
12754 error_at (OMP_CLAUSE_LOCATION (c),
12755 "%qE appears more than once in data clauses", t);
953ff289
DN
12756 remove = true;
12757 }
e01d41e5
JJ
12758 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
12759 {
12760 error ("%qD appears both in data and map clauses", t);
12761 remove = true;
12762 }
953ff289
DN
12763 else
12764 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12765 break;
12766
12767 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12768 t = OMP_CLAUSE_DECL (c);
12769 need_complete = true;
12770 need_implicitly_determined = true;
0ae9bd27 12771 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12772 {
c2255bc4
AH
12773 error_at (OMP_CLAUSE_LOCATION (c),
12774 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12775 remove = true;
12776 }
12777 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12778 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12779 {
c2255bc4
AH
12780 error_at (OMP_CLAUSE_LOCATION (c),
12781 "%qE appears more than once in data clauses", t);
953ff289
DN
12782 remove = true;
12783 }
12784 else
12785 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12786 break;
12787
acf0174b
JJ
12788 case OMP_CLAUSE_ALIGNED:
12789 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12790 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12791 {
12792 error_at (OMP_CLAUSE_LOCATION (c),
12793 "%qE is not a variable in %<aligned%> clause", t);
12794 remove = true;
12795 }
5a9785fb
JJ
12796 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12797 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12798 {
12799 error_at (OMP_CLAUSE_LOCATION (c),
12800 "%qE in %<aligned%> clause is neither a pointer nor "
12801 "an array", t);
12802 remove = true;
12803 }
acf0174b
JJ
12804 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12805 {
12806 error_at (OMP_CLAUSE_LOCATION (c),
12807 "%qE appears more than once in %<aligned%> clauses",
12808 t);
12809 remove = true;
12810 }
12811 else
12812 bitmap_set_bit (&aligned_head, DECL_UID (t));
12813 break;
12814
12815 case OMP_CLAUSE_DEPEND:
12816 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
12817 if (t == NULL_TREE)
12818 {
12819 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
12820 == OMP_CLAUSE_DEPEND_SOURCE);
12821 break;
12822 }
12823 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
12824 {
12825 gcc_assert (TREE_CODE (t) == TREE_LIST);
12826 for (; t; t = TREE_CHAIN (t))
12827 {
12828 tree decl = TREE_VALUE (t);
12829 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
12830 {
12831 tree offset = TREE_PURPOSE (t);
12832 bool neg = wi::neg_p ((wide_int) offset);
12833 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
12834 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
12835 neg ? MINUS_EXPR : PLUS_EXPR,
12836 decl, offset);
12837 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
12838 sizetype,
12839 fold_convert (sizetype, t2),
12840 fold_convert (sizetype, decl));
d9a6bd32
JJ
12841 if (t2 == error_mark_node)
12842 {
12843 remove = true;
12844 break;
12845 }
12846 TREE_PURPOSE (t) = t2;
12847 }
12848 }
12849 break;
12850 }
acf0174b
JJ
12851 if (TREE_CODE (t) == TREE_LIST)
12852 {
d9a6bd32 12853 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12854 remove = true;
12855 break;
12856 }
12857 if (t == error_mark_node)
12858 remove = true;
0ae9bd27 12859 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12860 {
12861 error_at (OMP_CLAUSE_LOCATION (c),
12862 "%qE is not a variable in %<depend%> clause", t);
12863 remove = true;
12864 }
12865 else if (!c_mark_addressable (t))
12866 remove = true;
12867 break;
12868
12869 case OMP_CLAUSE_MAP:
12870 case OMP_CLAUSE_TO:
12871 case OMP_CLAUSE_FROM:
41dbbb37 12872 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12873 t = OMP_CLAUSE_DECL (c);
12874 if (TREE_CODE (t) == TREE_LIST)
12875 {
d9a6bd32 12876 if (handle_omp_array_sections (c, is_omp))
acf0174b
JJ
12877 remove = true;
12878 else
12879 {
12880 t = OMP_CLAUSE_DECL (c);
b17a8b07 12881 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12882 {
12883 error_at (OMP_CLAUSE_LOCATION (c),
12884 "array section does not have mappable type "
12885 "in %qs clause",
12886 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12887 remove = true;
12888 }
d9a6bd32
JJ
12889 while (TREE_CODE (t) == ARRAY_REF)
12890 t = TREE_OPERAND (t, 0);
12891 if (TREE_CODE (t) == COMPONENT_REF
12892 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12893 {
12894 while (TREE_CODE (t) == COMPONENT_REF)
12895 t = TREE_OPERAND (t, 0);
12896 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
12897 break;
12898 if (bitmap_bit_p (&map_head, DECL_UID (t)))
12899 {
12900 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12901 error ("%qD appears more than once in motion"
12902 " clauses", t);
12903 else
12904 error ("%qD appears more than once in map"
12905 " clauses", t);
12906 remove = true;
12907 }
12908 else
12909 {
12910 bitmap_set_bit (&map_head, DECL_UID (t));
12911 bitmap_set_bit (&map_field_head, DECL_UID (t));
12912 }
12913 }
acf0174b
JJ
12914 }
12915 break;
12916 }
12917 if (t == error_mark_node)
d9a6bd32
JJ
12918 {
12919 remove = true;
12920 break;
12921 }
12922 if (TREE_CODE (t) == COMPONENT_REF
12923 && is_omp
12924 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
12925 {
12926 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12927 {
12928 error_at (OMP_CLAUSE_LOCATION (c),
12929 "bit-field %qE in %qs clause",
12930 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12931 remove = true;
12932 }
12933 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
12934 {
12935 error_at (OMP_CLAUSE_LOCATION (c),
12936 "%qE does not have a mappable type in %qs clause",
12937 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12938 remove = true;
12939 }
12940 while (TREE_CODE (t) == COMPONENT_REF)
12941 {
12942 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
12943 == UNION_TYPE)
12944 {
12945 error_at (OMP_CLAUSE_LOCATION (c),
12946 "%qE is a member of a union", t);
12947 remove = true;
12948 break;
12949 }
12950 t = TREE_OPERAND (t, 0);
12951 }
12952 if (remove)
12953 break;
12954 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
12955 {
e01d41e5 12956 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
12957 break;
12958 }
12959 }
12960 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12961 {
12962 error_at (OMP_CLAUSE_LOCATION (c),
12963 "%qE is not a variable in %qs clause", t,
12964 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12965 remove = true;
12966 }
0ae9bd27 12967 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12968 {
12969 error_at (OMP_CLAUSE_LOCATION (c),
12970 "%qD is threadprivate variable in %qs clause", t,
12971 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12972 remove = true;
12973 }
e4606348
JJ
12974 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12975 || (OMP_CLAUSE_MAP_KIND (c)
12976 != GOMP_MAP_FIRSTPRIVATE_POINTER))
12977 && !c_mark_addressable (t))
acf0174b 12978 remove = true;
b17a8b07 12979 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 12980 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
12981 || (OMP_CLAUSE_MAP_KIND (c)
12982 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
12983 || (OMP_CLAUSE_MAP_KIND (c)
12984 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 12985 && t == OMP_CLAUSE_DECL (c)
b17a8b07 12986 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12987 {
12988 error_at (OMP_CLAUSE_LOCATION (c),
12989 "%qD does not have a mappable type in %qs clause", t,
12990 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12991 remove = true;
12992 }
d9a6bd32
JJ
12993 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12994 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
12995 {
12996 if (bitmap_bit_p (&generic_head, DECL_UID (t))
12997 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12998 {
12999 error ("%qD appears more than once in data clauses", t);
13000 remove = true;
13001 }
e01d41e5 13002 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13003 {
e01d41e5
JJ
13004 error ("%qD appears both in data and map clauses", t);
13005 remove = true;
d9a6bd32 13006 }
e01d41e5
JJ
13007 else
13008 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13009 }
13010 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13011 {
13012 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13013 error ("%qD appears more than once in motion clauses", t);
13014 else
13015 error ("%qD appears more than once in map clauses", t);
13016 remove = true;
13017 }
e01d41e5
JJ
13018 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13019 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13020 {
13021 error ("%qD appears both in data and map clauses", t);
13022 remove = true;
13023 }
acf0174b 13024 else
d9a6bd32
JJ
13025 {
13026 bitmap_set_bit (&map_head, DECL_UID (t));
13027 if (t != OMP_CLAUSE_DECL (c)
13028 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13029 bitmap_set_bit (&map_field_head, DECL_UID (t));
13030 }
13031 break;
13032
13033 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13034 case OMP_CLAUSE_LINK:
13035 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13036 if (TREE_CODE (t) == FUNCTION_DECL
13037 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13038 ;
13039 else if (!VAR_P (t))
d9a6bd32 13040 {
e01d41e5
JJ
13041 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13042 error_at (OMP_CLAUSE_LOCATION (c),
13043 "%qE is neither a variable nor a function name in "
13044 "clause %qs", t,
13045 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13046 else
13047 error_at (OMP_CLAUSE_LOCATION (c),
13048 "%qE is not a variable in clause %qs", t,
13049 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13050 remove = true;
13051 }
13052 else if (DECL_THREAD_LOCAL_P (t))
13053 {
13054 error_at (OMP_CLAUSE_LOCATION (c),
13055 "%qD is threadprivate variable in %qs clause", t,
13056 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13057 remove = true;
13058 }
13059 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13060 {
13061 error_at (OMP_CLAUSE_LOCATION (c),
13062 "%qD does not have a mappable type in %qs clause", t,
13063 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13064 remove = true;
13065 }
e01d41e5
JJ
13066 if (remove)
13067 break;
13068 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13069 {
13070 error_at (OMP_CLAUSE_LOCATION (c),
13071 "%qE appears more than once on the same "
13072 "%<declare target%> directive", t);
13073 remove = true;
13074 }
13075 else
13076 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13077 break;
13078
13079 case OMP_CLAUSE_UNIFORM:
13080 t = OMP_CLAUSE_DECL (c);
13081 if (TREE_CODE (t) != PARM_DECL)
13082 {
13083 if (DECL_P (t))
13084 error_at (OMP_CLAUSE_LOCATION (c),
13085 "%qD is not an argument in %<uniform%> clause", t);
13086 else
13087 error_at (OMP_CLAUSE_LOCATION (c),
13088 "%qE is not an argument in %<uniform%> clause", t);
13089 remove = true;
ee1d5a02 13090 break;
acf0174b 13091 }
e01d41e5
JJ
13092 /* map_head bitmap is used as uniform_head if declare_simd. */
13093 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13094 goto check_dup_generic;
acf0174b 13095
37d5ad46 13096 case OMP_CLAUSE_USE_DEVICE:
d9a6bd32
JJ
13097 case OMP_CLAUSE_IS_DEVICE_PTR:
13098 case OMP_CLAUSE_USE_DEVICE_PTR:
13099 t = OMP_CLAUSE_DECL (c);
13100 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13101 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13102 {
13103 error_at (OMP_CLAUSE_LOCATION (c),
13104 "%qs variable is neither a pointer nor an array",
13105 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13106 remove = true;
13107 }
13108 goto check_dup_generic;
13109
acf0174b
JJ
13110 case OMP_CLAUSE_NOWAIT:
13111 if (copyprivate_seen)
13112 {
13113 error_at (OMP_CLAUSE_LOCATION (c),
13114 "%<nowait%> clause must not be used together "
13115 "with %<copyprivate%>");
13116 remove = true;
13117 break;
13118 }
13119 nowait_clause = pc;
13120 pc = &OMP_CLAUSE_CHAIN (c);
13121 continue;
13122
953ff289
DN
13123 case OMP_CLAUSE_IF:
13124 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
13125 case OMP_CLAUSE_NUM_TEAMS:
13126 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 13127 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
13128 case OMP_CLAUSE_UNTIED:
13129 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
13130 case OMP_CLAUSE_FINAL:
13131 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
13132 case OMP_CLAUSE_DEVICE:
13133 case OMP_CLAUSE_DIST_SCHEDULE:
13134 case OMP_CLAUSE_PARALLEL:
13135 case OMP_CLAUSE_FOR:
13136 case OMP_CLAUSE_SECTIONS:
13137 case OMP_CLAUSE_TASKGROUP:
13138 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
13139 case OMP_CLAUSE_PRIORITY:
13140 case OMP_CLAUSE_GRAINSIZE:
13141 case OMP_CLAUSE_NUM_TASKS:
13142 case OMP_CLAUSE_NOGROUP:
13143 case OMP_CLAUSE_THREADS:
13144 case OMP_CLAUSE_SIMD:
13145 case OMP_CLAUSE_HINT:
13146 case OMP_CLAUSE_DEFAULTMAP:
9a771876 13147 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
13148 case OMP_CLAUSE_NUM_GANGS:
13149 case OMP_CLAUSE_NUM_WORKERS:
13150 case OMP_CLAUSE_VECTOR_LENGTH:
13151 case OMP_CLAUSE_ASYNC:
13152 case OMP_CLAUSE_WAIT:
13153 case OMP_CLAUSE_AUTO:
7a5e4956 13154 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
13155 case OMP_CLAUSE_SEQ:
13156 case OMP_CLAUSE_GANG:
13157 case OMP_CLAUSE_WORKER:
13158 case OMP_CLAUSE_VECTOR:
7a5e4956 13159 case OMP_CLAUSE_TILE:
acf0174b
JJ
13160 pc = &OMP_CLAUSE_CHAIN (c);
13161 continue;
13162
e01d41e5
JJ
13163 case OMP_CLAUSE_SCHEDULE:
13164 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13165 {
13166 const char *p = NULL;
13167 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13168 {
13169 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13170 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13171 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13172 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13173 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13174 default: gcc_unreachable ();
13175 }
13176 if (p)
13177 {
13178 error_at (OMP_CLAUSE_LOCATION (c),
13179 "%<nonmonotonic%> modifier specified for %qs "
13180 "schedule kind", p);
13181 OMP_CLAUSE_SCHEDULE_KIND (c)
13182 = (enum omp_clause_schedule_kind)
13183 (OMP_CLAUSE_SCHEDULE_KIND (c)
13184 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13185 }
13186 }
13187 schedule_clause = c;
13188 pc = &OMP_CLAUSE_CHAIN (c);
13189 continue;
13190
13191 case OMP_CLAUSE_ORDERED:
13192 ordered_seen = true;
13193 pc = &OMP_CLAUSE_CHAIN (c);
13194 continue;
13195
d9a6bd32
JJ
13196 case OMP_CLAUSE_SAFELEN:
13197 safelen = c;
13198 pc = &OMP_CLAUSE_CHAIN (c);
13199 continue;
13200 case OMP_CLAUSE_SIMDLEN:
13201 simdlen = c;
13202 pc = &OMP_CLAUSE_CHAIN (c);
13203 continue;
13204
acf0174b
JJ
13205 case OMP_CLAUSE_INBRANCH:
13206 case OMP_CLAUSE_NOTINBRANCH:
13207 if (branch_seen)
13208 {
13209 error_at (OMP_CLAUSE_LOCATION (c),
13210 "%<inbranch%> clause is incompatible with "
13211 "%<notinbranch%>");
13212 remove = true;
13213 break;
13214 }
13215 branch_seen = true;
953ff289
DN
13216 pc = &OMP_CLAUSE_CHAIN (c);
13217 continue;
13218
13219 default:
13220 gcc_unreachable ();
13221 }
13222
13223 if (!remove)
13224 {
13225 t = OMP_CLAUSE_DECL (c);
13226
13227 if (need_complete)
13228 {
13229 t = require_complete_type (t);
13230 if (t == error_mark_node)
13231 remove = true;
13232 }
13233
13234 if (need_implicitly_determined)
13235 {
13236 const char *share_name = NULL;
13237
0ae9bd27 13238 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
13239 share_name = "threadprivate";
13240 else switch (c_omp_predetermined_sharing (t))
13241 {
13242 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13243 break;
13244 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
13245 /* const vars may be specified in firstprivate clause. */
13246 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13247 && TREE_READONLY (t))
13248 break;
953ff289
DN
13249 share_name = "shared";
13250 break;
13251 case OMP_CLAUSE_DEFAULT_PRIVATE:
13252 share_name = "private";
13253 break;
13254 default:
13255 gcc_unreachable ();
13256 }
13257 if (share_name)
13258 {
c2255bc4
AH
13259 error_at (OMP_CLAUSE_LOCATION (c),
13260 "%qE is predetermined %qs for %qs",
acf0174b
JJ
13261 t, share_name,
13262 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13263 remove = true;
13264 }
13265 }
13266 }
13267
13268 if (remove)
13269 *pc = OMP_CLAUSE_CHAIN (c);
13270 else
13271 pc = &OMP_CLAUSE_CHAIN (c);
13272 }
13273
d9a6bd32
JJ
13274 if (simdlen
13275 && safelen
13276 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13277 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13278 {
13279 error_at (OMP_CLAUSE_LOCATION (simdlen),
13280 "%<simdlen%> clause value is bigger than "
13281 "%<safelen%> clause value");
13282 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13283 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13284 }
13285
e01d41e5
JJ
13286 if (ordered_seen
13287 && schedule_clause
13288 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13289 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13290 {
13291 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13292 "%<nonmonotonic%> schedule modifier specified together "
13293 "with %<ordered%> clause");
13294 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13295 = (enum omp_clause_schedule_kind)
13296 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13297 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13298 }
13299
13300 if (linear_variable_step_check)
13301 for (pc = &clauses, c = clauses; c ; c = *pc)
13302 {
13303 bool remove = false;
13304 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13305 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13306 && !bitmap_bit_p (&map_head,
13307 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13308 {
13309 error_at (OMP_CLAUSE_LOCATION (c),
13310 "%<linear%> clause step is a parameter %qD not "
13311 "specified in %<uniform%> clause",
13312 OMP_CLAUSE_LINEAR_STEP (c));
13313 remove = true;
13314 }
13315
13316 if (remove)
13317 *pc = OMP_CLAUSE_CHAIN (c);
13318 else
13319 pc = &OMP_CLAUSE_CHAIN (c);
13320 }
13321
953ff289
DN
13322 bitmap_obstack_release (NULL);
13323 return clauses;
13324}
9ae165a0 13325
0a35513e
AH
13326/* Create a transaction node. */
13327
13328tree
13329c_finish_transaction (location_t loc, tree block, int flags)
13330{
13331 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13332 if (flags & TM_STMT_ATTR_OUTER)
13333 TRANSACTION_EXPR_OUTER (stmt) = 1;
13334 if (flags & TM_STMT_ATTR_RELAXED)
13335 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13336 return add_stmt (stmt);
13337}
13338
9ae165a0 13339/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
13340 down to the element type of an array. If ORIG_QUAL_TYPE is not
13341 NULL, then it should be used as the qualified type
13342 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13343 preserve information about the typedef name from which an array
13344 type was derived). */
9ae165a0
DG
13345
13346tree
e9e32ee6
JM
13347c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13348 size_t orig_qual_indirect)
9ae165a0
DG
13349{
13350 if (type == error_mark_node)
13351 return type;
13352
13353 if (TREE_CODE (type) == ARRAY_TYPE)
13354 {
13355 tree t;
13356 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
13357 type_quals, orig_qual_type,
13358 orig_qual_indirect - 1);
9ae165a0
DG
13359
13360 /* See if we already have an identically qualified type. */
e9e32ee6
JM
13361 if (orig_qual_type && orig_qual_indirect == 0)
13362 t = orig_qual_type;
13363 else
13364 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13365 {
13366 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13367 && TYPE_NAME (t) == TYPE_NAME (type)
13368 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13369 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13370 TYPE_ATTRIBUTES (type)))
13371 break;
13372 }
9ae165a0
DG
13373 if (!t)
13374 {
13375 tree domain = TYPE_DOMAIN (type);
13376
13377 t = build_variant_type_copy (type);
13378 TREE_TYPE (t) = element_type;
13379
13380 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13381 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13382 SET_TYPE_STRUCTURAL_EQUALITY (t);
13383 else if (TYPE_CANONICAL (element_type) != element_type
13384 || (domain && TYPE_CANONICAL (domain) != domain))
13385 {
b8698a0f 13386 tree unqualified_canon
9ae165a0 13387 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 13388 domain? TYPE_CANONICAL (domain)
9ae165a0 13389 : NULL_TREE);
ee45a32d
EB
13390 if (TYPE_REVERSE_STORAGE_ORDER (type))
13391 {
13392 unqualified_canon
13393 = build_distinct_type_copy (unqualified_canon);
13394 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13395 }
b8698a0f 13396 TYPE_CANONICAL (t)
9ae165a0
DG
13397 = c_build_qualified_type (unqualified_canon, type_quals);
13398 }
13399 else
13400 TYPE_CANONICAL (t) = t;
13401 }
13402 return t;
13403 }
13404
13405 /* A restrict-qualified pointer type must be a pointer to object or
13406 incomplete type. Note that the use of POINTER_TYPE_P also allows
13407 REFERENCE_TYPEs, which is appropriate for C++. */
13408 if ((type_quals & TYPE_QUAL_RESTRICT)
13409 && (!POINTER_TYPE_P (type)
13410 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13411 {
13412 error ("invalid use of %<restrict%>");
13413 type_quals &= ~TYPE_QUAL_RESTRICT;
13414 }
13415
e9e32ee6
JM
13416 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13417 ? orig_qual_type
13418 : build_qualified_type (type, type_quals));
e922069e
JW
13419 /* A variant type does not inherit the list of incomplete vars from the
13420 type main variant. */
3e636daf 13421 if (RECORD_OR_UNION_TYPE_P (var_type))
e922069e
JW
13422 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13423 return var_type;
9ae165a0 13424}
72b5577d
ILT
13425
13426/* Build a VA_ARG_EXPR for the C parser. */
13427
13428tree
f187980b 13429c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 13430{
ec3fba51
MP
13431 if (error_operand_p (type))
13432 return error_mark_node;
f187980b
EB
13433 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13434 order because it takes the address of the expression. */
13435 else if (handled_component_p (expr)
13436 && reverse_storage_order_for_component_p (expr))
13437 {
13438 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13439 return error_mark_node;
13440 }
ec3fba51 13441 else if (!COMPLETE_TYPE_P (type))
4e81b788 13442 {
f187980b 13443 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 13444 "type %qT", type);
4e81b788
MP
13445 return error_mark_node;
13446 }
ec3fba51 13447 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 13448 warning_at (loc2, OPT_Wc___compat,
ec3fba51 13449 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 13450 return build_va_arg (loc2, expr, type);
72b5577d 13451}
acf0174b
JJ
13452
13453/* Return truthvalue of whether T1 is the same tree structure as T2.
13454 Return 1 if they are the same. Return 0 if they are different. */
13455
13456bool
13457c_tree_equal (tree t1, tree t2)
13458{
13459 enum tree_code code1, code2;
13460
13461 if (t1 == t2)
13462 return true;
13463 if (!t1 || !t2)
13464 return false;
13465
13466 for (code1 = TREE_CODE (t1);
13467 CONVERT_EXPR_CODE_P (code1)
13468 || code1 == NON_LVALUE_EXPR;
13469 code1 = TREE_CODE (t1))
13470 t1 = TREE_OPERAND (t1, 0);
13471 for (code2 = TREE_CODE (t2);
13472 CONVERT_EXPR_CODE_P (code2)
13473 || code2 == NON_LVALUE_EXPR;
13474 code2 = TREE_CODE (t2))
13475 t2 = TREE_OPERAND (t2, 0);
13476
13477 /* They might have become equal now. */
13478 if (t1 == t2)
13479 return true;
13480
13481 if (code1 != code2)
13482 return false;
13483
13484 switch (code1)
13485 {
13486 case INTEGER_CST:
807e902e 13487 return wi::eq_p (t1, t2);
acf0174b
JJ
13488
13489 case REAL_CST:
624d31fe 13490 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
13491
13492 case STRING_CST:
13493 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13494 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13495 TREE_STRING_LENGTH (t1));
13496
13497 case FIXED_CST:
13498 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13499 TREE_FIXED_CST (t2));
13500
13501 case COMPLEX_CST:
13502 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13503 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13504
13505 case VECTOR_CST:
13506 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13507
13508 case CONSTRUCTOR:
13509 /* We need to do this when determining whether or not two
13510 non-type pointer to member function template arguments
13511 are the same. */
13512 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13513 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13514 return false;
13515 {
13516 tree field, value;
13517 unsigned int i;
13518 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13519 {
13520 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13521 if (!c_tree_equal (field, elt2->index)
13522 || !c_tree_equal (value, elt2->value))
13523 return false;
13524 }
13525 }
13526 return true;
13527
13528 case TREE_LIST:
13529 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13530 return false;
13531 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
13532 return false;
13533 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
13534
13535 case SAVE_EXPR:
13536 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13537
13538 case CALL_EXPR:
13539 {
13540 tree arg1, arg2;
13541 call_expr_arg_iterator iter1, iter2;
13542 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
13543 return false;
13544 for (arg1 = first_call_expr_arg (t1, &iter1),
13545 arg2 = first_call_expr_arg (t2, &iter2);
13546 arg1 && arg2;
13547 arg1 = next_call_expr_arg (&iter1),
13548 arg2 = next_call_expr_arg (&iter2))
13549 if (!c_tree_equal (arg1, arg2))
13550 return false;
13551 if (arg1 || arg2)
13552 return false;
13553 return true;
13554 }
13555
13556 case TARGET_EXPR:
13557 {
13558 tree o1 = TREE_OPERAND (t1, 0);
13559 tree o2 = TREE_OPERAND (t2, 0);
13560
13561 /* Special case: if either target is an unallocated VAR_DECL,
13562 it means that it's going to be unified with whatever the
13563 TARGET_EXPR is really supposed to initialize, so treat it
13564 as being equivalent to anything. */
0ae9bd27 13565 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
13566 && !DECL_RTL_SET_P (o1))
13567 /*Nop*/;
0ae9bd27 13568 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
13569 && !DECL_RTL_SET_P (o2))
13570 /*Nop*/;
13571 else if (!c_tree_equal (o1, o2))
13572 return false;
13573
13574 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
13575 }
13576
13577 case COMPONENT_REF:
13578 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
13579 return false;
13580 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
13581
13582 case PARM_DECL:
13583 case VAR_DECL:
13584 case CONST_DECL:
13585 case FIELD_DECL:
13586 case FUNCTION_DECL:
13587 case IDENTIFIER_NODE:
13588 case SSA_NAME:
13589 return false;
13590
13591 case TREE_VEC:
13592 {
13593 unsigned ix;
13594 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
13595 return false;
13596 for (ix = TREE_VEC_LENGTH (t1); ix--;)
13597 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
13598 TREE_VEC_ELT (t2, ix)))
13599 return false;
13600 return true;
13601 }
13602
13603 default:
13604 break;
13605 }
13606
13607 switch (TREE_CODE_CLASS (code1))
13608 {
13609 case tcc_unary:
13610 case tcc_binary:
13611 case tcc_comparison:
13612 case tcc_expression:
13613 case tcc_vl_exp:
13614 case tcc_reference:
13615 case tcc_statement:
13616 {
13617 int i, n = TREE_OPERAND_LENGTH (t1);
13618
13619 switch (code1)
13620 {
13621 case PREINCREMENT_EXPR:
13622 case PREDECREMENT_EXPR:
13623 case POSTINCREMENT_EXPR:
13624 case POSTDECREMENT_EXPR:
13625 n = 1;
13626 break;
13627 case ARRAY_REF:
13628 n = 2;
13629 break;
13630 default:
13631 break;
13632 }
13633
13634 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
13635 && n != TREE_OPERAND_LENGTH (t2))
13636 return false;
13637
13638 for (i = 0; i < n; ++i)
13639 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
13640 return false;
13641
13642 return true;
13643 }
13644
13645 case tcc_type:
13646 return comptypes (t1, t2);
13647 default:
13648 gcc_unreachable ();
13649 }
13650 /* We can get here with --disable-checking. */
13651 return false;
13652}
12893402
BI
13653
13654/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
13655 spawn-helper and BODY is the newly created body for FNDECL. */
13656
13657void
13658cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
13659{
13660 tree list = alloc_stmt_list ();
13661 tree frame = make_cilk_frame (fndecl);
13662 tree dtor = create_cilk_function_exit (frame, false, true);
13663 add_local_decl (cfun, frame);
13664
13665 DECL_SAVED_TREE (fndecl) = list;
13666 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
13667 frame);
13668 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
13669 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
13670
13671 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
13672 append_to_statement_list (detach_expr, &body_list);
13673
13674 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
13675 body = fold_build_cleanup_point_expr (void_type_node, body);
13676
13677 append_to_statement_list (body, &body_list);
13678 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
13679 body_list, dtor), &list);
13680}
1807ffc1
MS
13681
13682/* Returns true when the function declaration FNDECL is implicit,
13683 introduced as a result of a call to an otherwise undeclared
13684 function, and false otherwise. */
13685
13686bool
13687c_decl_implicit (const_tree fndecl)
13688{
13689 return C_DECL_IMPLICIT (fndecl);
13690}