]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
5624e564 2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
c7131fb2 31#include "alias.h"
40e23961 32#include "fold-const.h"
d8a2d370
DN
33#include "stor-layout.h"
34#include "trans-mem.h"
35#include "varasm.h"
36#include "stmt.h"
e57e265b 37#include "langhooks.h"
400fbf9f 38#include "c-tree.h"
29cc57cf 39#include "c-lang.h"
400fbf9f 40#include "flags.h"
ab87f8c8 41#include "intl.h"
672a6f42 42#include "target.h"
325c3691 43#include "tree-iterator.h"
6662d794 44#include "bitmap.h"
60393bbc 45#include "predict.h"
60393bbc 46#include "hard-reg-set.h"
60393bbc 47#include "function.h"
2fb9a547 48#include "gimple-expr.h"
45b0be94 49#include "gimplify.h"
acf0174b 50#include "tree-inline.h"
0645c1a2 51#include "omp-low.h"
61d3ce20 52#include "c-family/c-objc.h"
a212e43f 53#include "c-family/c-common.h"
de5a5fa1 54#include "c-family/c-ubsan.h"
12893402 55#include "cilk.h"
41dbbb37 56#include "gomp-constants.h"
325c3691 57
2ac2f164
JM
58/* Possible cases of implicit bad conversions. Used to select
59 diagnostic messages in convert_for_assignment. */
60enum impl_conv {
61 ic_argpass,
62 ic_assign,
63 ic_init,
64 ic_return
65};
66
bc4b653b
JM
67/* The level of nesting inside "__alignof__". */
68int in_alignof;
69
70/* The level of nesting inside "sizeof". */
71int in_sizeof;
72
73/* The level of nesting inside "typeof". */
74int in_typeof;
400fbf9f 75
1a4049e7
JJ
76/* The argument of last parsed sizeof expression, only to be tested
77 if expr.original_code == SIZEOF_EXPR. */
78tree c_last_sizeof_arg;
79
9bac5cbb
G
80/* Nonzero if we might need to print a "missing braces around
81 initializer" message within this initializer. */
82static int found_missing_braces;
103b7b17 83
bf730f15
RS
84static int require_constant_value;
85static int require_constant_elements;
86
58f9752a 87static bool null_pointer_constant_p (const_tree);
f55ade6e 88static tree qualify_type (tree, tree);
dc5027f4
JM
89static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
90 bool *);
744aa42f 91static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
92static int function_types_compatible_p (const_tree, const_tree, bool *,
93 bool *);
94static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 95static tree lookup_field (tree, tree);
81e5eca8
MP
96static int convert_arguments (location_t, vec<location_t>, tree,
97 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
98 tree);
db3927fb 99static tree pointer_diff (location_t, tree, tree);
68fca595 100static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 101 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
102static tree valid_compound_expr_initializer (tree, tree);
103static void push_string (const char *);
104static void push_member_name (tree);
f55ade6e
AJ
105static int spelling_length (void);
106static char *print_spelling (char *);
96b40f8d 107static void warning_init (location_t, int, const char *);
c2255bc4 108static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
109static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
110 bool, struct obstack *);
a1e3b3d9 111static void output_pending_init_elements (int, struct obstack *);
ea58ef42 112static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 113static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
114static void add_pending_init (location_t, tree, tree, tree, bool,
115 struct obstack *);
a1e3b3d9
LB
116static void set_nonincremental_init (struct obstack *);
117static void set_nonincremental_init_from_string (tree, struct obstack *);
118static tree find_init_member (tree, struct obstack *);
f37acdf9 119static void readonly_warning (tree, enum lvalue_use);
7bd11157 120static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 121static void record_maybe_used_decl (tree);
dc5027f4 122static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
123\f
124/* Return true if EXP is a null pointer constant, false otherwise. */
125
126static bool
58f9752a 127null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
128{
129 /* This should really operate on c_expr structures, but they aren't
130 yet available everywhere required. */
131 tree type = TREE_TYPE (expr);
132 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 133 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
134 && integer_zerop (expr)
135 && (INTEGRAL_TYPE_P (type)
136 || (TREE_CODE (type) == POINTER_TYPE
137 && VOID_TYPE_P (TREE_TYPE (type))
138 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
139}
928c19bb
JM
140
141/* EXPR may appear in an unevaluated part of an integer constant
142 expression, but not in an evaluated part. Wrap it in a
143 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
144 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
145
146static tree
147note_integer_operands (tree expr)
148{
149 tree ret;
150 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
151 {
152 ret = copy_node (expr);
153 TREE_OVERFLOW (ret) = 1;
154 }
155 else
156 {
157 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
158 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
159 }
160 return ret;
161}
162
4d84fe7c
JM
163/* Having checked whether EXPR may appear in an unevaluated part of an
164 integer constant expression and found that it may, remove any
165 C_MAYBE_CONST_EXPR noting this fact and return the resulting
166 expression. */
167
168static inline tree
169remove_c_maybe_const_expr (tree expr)
170{
171 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
172 return C_MAYBE_CONST_EXPR_EXPR (expr);
173 else
174 return expr;
175}
176
f13c9b2c
AP
177\f/* This is a cache to hold if two types are compatible or not. */
178
179struct tagged_tu_seen_cache {
180 const struct tagged_tu_seen_cache * next;
58f9752a
KG
181 const_tree t1;
182 const_tree t2;
f13c9b2c
AP
183 /* The return value of tagged_types_tu_compatible_p if we had seen
184 these two types already. */
185 int val;
186};
187
188static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
189static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
190
400fbf9f
JW
191/* Do `exp = require_complete_type (exp);' to make sure exp
192 does not have an incomplete type. (That includes void types.) */
193
194tree
2f6e4e97 195require_complete_type (tree value)
400fbf9f
JW
196{
197 tree type = TREE_TYPE (value);
198
7a0ca710 199 if (error_operand_p (value))
ea0f786b
CB
200 return error_mark_node;
201
400fbf9f 202 /* First, detect a valid value with a complete type. */
d0f062fb 203 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
204 return value;
205
7a228918 206 c_incomplete_type_error (value, type);
400fbf9f
JW
207 return error_mark_node;
208}
209
210/* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
212 and TYPE is the type that was invalid. */
213
214void
ac7d7749 215c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 216{
400fbf9f
JW
217 /* Avoid duplicate error message. */
218 if (TREE_CODE (type) == ERROR_MARK)
219 return;
220
0ae9bd27 221 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
ac9f18db 222 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
223 else
224 {
225 retry:
226 /* We must print an error message. Be clever about what it says. */
227
228 switch (TREE_CODE (type))
229 {
230 case RECORD_TYPE:
400fbf9f 231 case UNION_TYPE:
400fbf9f 232 case ENUMERAL_TYPE:
400fbf9f
JW
233 break;
234
235 case VOID_TYPE:
236 error ("invalid use of void expression");
237 return;
238
239 case ARRAY_TYPE:
240 if (TYPE_DOMAIN (type))
241 {
fba78abb
RH
242 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
243 {
244 error ("invalid use of flexible array member");
245 return;
246 }
400fbf9f
JW
247 type = TREE_TYPE (type);
248 goto retry;
249 }
250 error ("invalid use of array with unspecified bounds");
251 return;
252
253 default:
366de0ce 254 gcc_unreachable ();
400fbf9f
JW
255 }
256
257 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 258 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
259 else
260 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 261 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
262 }
263}
264
ab393bf1
NB
265/* Given a type, apply default promotions wrt unnamed function
266 arguments and return the new type. */
267
268tree
2f6e4e97 269c_type_promotes_to (tree type)
ab393bf1 270{
267bac10 271 tree ret = NULL_TREE;
ab393bf1 272
267bac10
JM
273 if (TYPE_MAIN_VARIANT (type) == float_type_node)
274 ret = double_type_node;
275 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
276 {
277 /* Preserve unsignedness if not really getting any wider. */
8df83eae 278 if (TYPE_UNSIGNED (type)
c22cacf3 279 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
280 ret = unsigned_type_node;
281 else
282 ret = integer_type_node;
ab393bf1
NB
283 }
284
267bac10
JM
285 if (ret != NULL_TREE)
286 return (TYPE_ATOMIC (type)
287 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
288 : ret);
289
ab393bf1
NB
290 return type;
291}
292
36c5e70a
BE
293/* Return true if between two named address spaces, whether there is a superset
294 named address space that encompasses both address spaces. If there is a
295 superset, return which address space is the superset. */
296
297static bool
298addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
299{
300 if (as1 == as2)
301 {
302 *common = as1;
303 return true;
304 }
305 else if (targetm.addr_space.subset_p (as1, as2))
306 {
307 *common = as2;
308 return true;
309 }
310 else if (targetm.addr_space.subset_p (as2, as1))
311 {
312 *common = as1;
313 return true;
314 }
315 else
316 return false;
317}
318
400fbf9f
JW
319/* Return a variant of TYPE which has all the type qualifiers of LIKE
320 as well as those of TYPE. */
321
322static tree
2f6e4e97 323qualify_type (tree type, tree like)
400fbf9f 324{
36c5e70a
BE
325 addr_space_t as_type = TYPE_ADDR_SPACE (type);
326 addr_space_t as_like = TYPE_ADDR_SPACE (like);
327 addr_space_t as_common;
328
329 /* If the two named address spaces are different, determine the common
330 superset address space. If there isn't one, raise an error. */
331 if (!addr_space_superset (as_type, as_like, &as_common))
332 {
333 as_common = as_type;
334 error ("%qT and %qT are in disjoint named address spaces",
335 type, like);
336 }
337
2f6e4e97 338 return c_build_qualified_type (type,
36c5e70a 339 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 340 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 341 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 342}
52ffd86e
MS
343
344/* Return true iff the given tree T is a variable length array. */
345
346bool
ac7d7749 347c_vla_type_p (const_tree t)
52ffd86e
MS
348{
349 if (TREE_CODE (t) == ARRAY_TYPE
350 && C_TYPE_VARIABLE_SIZE (t))
351 return true;
352 return false;
353}
400fbf9f 354\f
10bc1b1b 355/* Return the composite type of two compatible types.
5305f6d7 356
10bc1b1b
JM
357 We assume that comptypes has already been done and returned
358 nonzero; if that isn't so, this may crash. In particular, we
359 assume that qualifiers match. */
400fbf9f
JW
360
361tree
10bc1b1b 362composite_type (tree t1, tree t2)
400fbf9f 363{
b3694847
SS
364 enum tree_code code1;
365 enum tree_code code2;
4b027d16 366 tree attributes;
400fbf9f
JW
367
368 /* Save time if the two types are the same. */
369
370 if (t1 == t2) return t1;
371
372 /* If one type is nonsense, use the other. */
373 if (t1 == error_mark_node)
374 return t2;
375 if (t2 == error_mark_node)
376 return t1;
377
10bc1b1b
JM
378 code1 = TREE_CODE (t1);
379 code2 = TREE_CODE (t2);
380
d9525bec 381 /* Merge the attributes. */
5fd9b178 382 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 383
10bc1b1b
JM
384 /* If one is an enumerated type and the other is the compatible
385 integer type, the composite type might be either of the two
386 (DR#013 question 3). For consistency, use the enumerated type as
387 the composite type. */
400fbf9f 388
10bc1b1b
JM
389 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
390 return t1;
391 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
392 return t2;
75326e8c 393
366de0ce 394 gcc_assert (code1 == code2);
b6a10c9f 395
400fbf9f
JW
396 switch (code1)
397 {
400fbf9f 398 case POINTER_TYPE:
10bc1b1b 399 /* For two pointers, do this recursively on the target type. */
400fbf9f 400 {
3932261a
MM
401 tree pointed_to_1 = TREE_TYPE (t1);
402 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 403 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 404 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
405 t1 = build_type_attribute_variant (t1, attributes);
406 return qualify_type (t1, t2);
400fbf9f 407 }
400fbf9f
JW
408
409 case ARRAY_TYPE:
410 {
10bc1b1b 411 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
412 int quals;
413 tree unqual_elt;
ca8bdb78
JM
414 tree d1 = TYPE_DOMAIN (t1);
415 tree d2 = TYPE_DOMAIN (t2);
416 bool d1_variable, d2_variable;
417 bool d1_zero, d2_zero;
f6294de7 418 bool t1_complete, t2_complete;
46df2823 419
de46b2fe 420 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
421 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
422 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 423
f6294de7
JM
424 t1_complete = COMPLETE_TYPE_P (t1);
425 t2_complete = COMPLETE_TYPE_P (t2);
426
ca8bdb78
JM
427 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
428 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
429
430 d1_variable = (!d1_zero
431 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
432 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
433 d2_variable = (!d2_zero
434 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
435 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
436 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
437 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 438
400fbf9f 439 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
440 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
441 && (d2_variable || d2_zero || !d1_variable))
4b027d16 442 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
443 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
444 && (d1_variable || d1_zero || !d2_variable))
4b027d16 445 return build_type_attribute_variant (t2, attributes);
c22cacf3 446
de46b2fe
AP
447 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t1, attributes);
449 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t2, attributes);
c22cacf3 451
46df2823
JM
452 /* Merge the element types, and have a size if either arg has
453 one. We may have qualifiers on the element types. To set
454 up TYPE_MAIN_VARIANT correctly, we need to form the
455 composite of the unqualified types and add the qualifiers
456 back at the end. */
457 quals = TYPE_QUALS (strip_array_types (elt));
458 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
459 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
460 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
461 && (d2_variable
462 || d2_zero
463 || !d1_variable))
464 ? t1
465 : t2));
f6294de7
JM
466 /* Ensure a composite type involving a zero-length array type
467 is a zero-length type not an incomplete type. */
468 if (d1_zero && d2_zero
469 && (t1_complete || t2_complete)
470 && !COMPLETE_TYPE_P (t1))
471 {
472 TYPE_SIZE (t1) = bitsize_zero_node;
473 TYPE_SIZE_UNIT (t1) = size_zero_node;
474 }
46df2823 475 t1 = c_build_qualified_type (t1, quals);
de46b2fe 476 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
477 }
478
fcb99e7b
JJ
479 case ENUMERAL_TYPE:
480 case RECORD_TYPE:
481 case UNION_TYPE:
482 if (attributes != NULL)
483 {
484 /* Try harder not to create a new aggregate type. */
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
486 return t1;
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
488 return t2;
489 }
490 return build_type_attribute_variant (t1, attributes);
491
400fbf9f
JW
492 case FUNCTION_TYPE:
493 /* Function types: prefer the one that specified arg types.
494 If both do, merge the arg types. Also merge the return types. */
495 {
10bc1b1b 496 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
497 tree p1 = TYPE_ARG_TYPES (t1);
498 tree p2 = TYPE_ARG_TYPES (t2);
499 int len;
500 tree newargs, n;
501 int i;
502
503 /* Save space: see if the result is identical to one of the args. */
3f75a254 504 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 505 return build_type_attribute_variant (t1, attributes);
3f75a254 506 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 507 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
508
509 /* Simple way if one arg fails to specify argument types. */
510 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 511 {
fe7080d2
AP
512 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
513 t1 = build_type_attribute_variant (t1, attributes);
514 return qualify_type (t1, t2);
4b027d16 515 }
400fbf9f 516 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
517 {
518 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
519 t1 = build_type_attribute_variant (t1, attributes);
520 return qualify_type (t1, t2);
4b027d16 521 }
400fbf9f
JW
522
523 /* If both args specify argument types, we must merge the two
524 lists, argument by argument. */
2f4e8f2b 525
400fbf9f
JW
526 len = list_length (p1);
527 newargs = 0;
528
529 for (i = 0; i < len; i++)
8d9bfdc5 530 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
531
532 n = newargs;
533
534 for (; p1;
535 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
536 {
537 /* A null type means arg type is not specified.
538 Take whatever the other function type has. */
539 if (TREE_VALUE (p1) == 0)
540 {
541 TREE_VALUE (n) = TREE_VALUE (p2);
542 goto parm_done;
543 }
544 if (TREE_VALUE (p2) == 0)
545 {
546 TREE_VALUE (n) = TREE_VALUE (p1);
547 goto parm_done;
548 }
2f6e4e97 549
400fbf9f
JW
550 /* Given wait (union {union wait *u; int *i} *)
551 and wait (union wait *),
552 prefer union wait * as type of parm. */
553 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
554 && TREE_VALUE (p1) != TREE_VALUE (p2))
555 {
556 tree memb;
58cb41e6
JJ
557 tree mv2 = TREE_VALUE (p2);
558 if (mv2 && mv2 != error_mark_node
559 && TREE_CODE (mv2) != ARRAY_TYPE)
560 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 561 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 562 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
563 {
564 tree mv3 = TREE_TYPE (memb);
565 if (mv3 && mv3 != error_mark_node
566 && TREE_CODE (mv3) != ARRAY_TYPE)
567 mv3 = TYPE_MAIN_VARIANT (mv3);
568 if (comptypes (mv3, mv2))
569 {
570 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
571 TREE_VALUE (p2));
c1771a20 572 pedwarn (input_location, OPT_Wpedantic,
fcf73884 573 "function types not truly compatible in ISO C");
58cb41e6
JJ
574 goto parm_done;
575 }
576 }
400fbf9f
JW
577 }
578 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
579 && TREE_VALUE (p2) != TREE_VALUE (p1))
580 {
581 tree memb;
58cb41e6
JJ
582 tree mv1 = TREE_VALUE (p1);
583 if (mv1 && mv1 != error_mark_node
584 && TREE_CODE (mv1) != ARRAY_TYPE)
585 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 586 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 587 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
588 {
589 tree mv3 = TREE_TYPE (memb);
590 if (mv3 && mv3 != error_mark_node
591 && TREE_CODE (mv3) != ARRAY_TYPE)
592 mv3 = TYPE_MAIN_VARIANT (mv3);
593 if (comptypes (mv3, mv1))
594 {
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p1));
c1771a20 597 pedwarn (input_location, OPT_Wpedantic,
fcf73884 598 "function types not truly compatible in ISO C");
58cb41e6
JJ
599 goto parm_done;
600 }
601 }
400fbf9f 602 }
10bc1b1b 603 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
604 parm_done: ;
605 }
606
4b027d16 607 t1 = build_function_type (valtype, newargs);
fe7080d2 608 t1 = qualify_type (t1, t2);
0f41302f 609 /* ... falls through ... */
400fbf9f
JW
610 }
611
612 default:
4b027d16 613 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
614 }
615
616}
10bc1b1b
JM
617
618/* Return the type of a conditional expression between pointers to
619 possibly differently qualified versions of compatible types.
620
621 We assume that comp_target_types has already been done and returned
622 nonzero; if that isn't so, this may crash. */
623
624static tree
625common_pointer_type (tree t1, tree t2)
626{
627 tree attributes;
46df2823
JM
628 tree pointed_to_1, mv1;
629 tree pointed_to_2, mv2;
10bc1b1b 630 tree target;
eb1387a0 631 unsigned target_quals;
36c5e70a
BE
632 addr_space_t as1, as2, as_common;
633 int quals1, quals2;
10bc1b1b
JM
634
635 /* Save time if the two types are the same. */
636
637 if (t1 == t2) return t1;
638
639 /* If one type is nonsense, use the other. */
640 if (t1 == error_mark_node)
641 return t2;
642 if (t2 == error_mark_node)
643 return t1;
644
366de0ce 645 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 646 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
647
648 /* Merge the attributes. */
649 attributes = targetm.merge_type_attributes (t1, t2);
650
651 /* Find the composite type of the target types, and combine the
46df2823
JM
652 qualifiers of the two types' targets. Do not lose qualifiers on
653 array element types by taking the TYPE_MAIN_VARIANT. */
654 mv1 = pointed_to_1 = TREE_TYPE (t1);
655 mv2 = pointed_to_2 = TREE_TYPE (t2);
656 if (TREE_CODE (mv1) != ARRAY_TYPE)
657 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
658 if (TREE_CODE (mv2) != ARRAY_TYPE)
659 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
660 target = composite_type (mv1, mv2);
eb1387a0 661
768952be
MU
662 /* Strip array types to get correct qualifier for pointers to arrays */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
665
eb1387a0
RG
666 /* For function types do not merge const qualifiers, but drop them
667 if used inconsistently. The middle-end uses these to mark const
668 and noreturn functions. */
669 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 670 target_quals = (quals1 & quals2);
eb1387a0 671 else
36c5e70a
BE
672 target_quals = (quals1 | quals2);
673
674 /* If the two named address spaces are different, determine the common
675 superset address space. This is guaranteed to exist due to the
676 assumption that comp_target_type returned non-zero. */
677 as1 = TYPE_ADDR_SPACE (pointed_to_1);
678 as2 = TYPE_ADDR_SPACE (pointed_to_2);
679 if (!addr_space_superset (as1, as2, &as_common))
680 gcc_unreachable ();
681
682 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
683
eb1387a0 684 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
685 return build_type_attribute_variant (t1, attributes);
686}
687
688/* Return the common type for two arithmetic types under the usual
689 arithmetic conversions. The default conversions have already been
690 applied, and enumerated types converted to their compatible integer
691 types. The resulting type is unqualified and has no attributes.
692
693 This is the type for the result of most arithmetic operations
694 if the operands have the given two types. */
695
ccf7f880
JJ
696static tree
697c_common_type (tree t1, tree t2)
10bc1b1b
JM
698{
699 enum tree_code code1;
700 enum tree_code code2;
701
702 /* If one type is nonsense, use the other. */
703 if (t1 == error_mark_node)
704 return t2;
705 if (t2 == error_mark_node)
706 return t1;
707
708 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
709 t1 = TYPE_MAIN_VARIANT (t1);
710
711 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
712 t2 = TYPE_MAIN_VARIANT (t2);
713
714 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
715 t1 = build_type_attribute_variant (t1, NULL_TREE);
716
717 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
718 t2 = build_type_attribute_variant (t2, NULL_TREE);
719
720 /* Save time if the two types are the same. */
721
722 if (t1 == t2) return t1;
723
724 code1 = TREE_CODE (t1);
725 code2 = TREE_CODE (t2);
726
366de0ce 727 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
728 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
729 || code1 == INTEGER_TYPE);
366de0ce 730 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
731 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
732 || code2 == INTEGER_TYPE);
10bc1b1b 733
5fc89bfd
JJ
734 /* When one operand is a decimal float type, the other operand cannot be
735 a generic float type or a complex type. We also disallow vector types
736 here. */
737 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
738 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
739 {
740 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
741 {
742 error ("can%'t mix operands of decimal float and vector types");
743 return error_mark_node;
744 }
745 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and complex types");
748 return error_mark_node;
749 }
750 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
751 {
752 error ("can%'t mix operands of decimal float and other float types");
753 return error_mark_node;
754 }
755 }
756
10bc1b1b
JM
757 /* If one type is a vector type, return that type. (How the usual
758 arithmetic conversions apply to the vector types extension is not
759 precisely specified.) */
760 if (code1 == VECTOR_TYPE)
761 return t1;
762
763 if (code2 == VECTOR_TYPE)
764 return t2;
765
766 /* If one type is complex, form the common type of the non-complex
767 components, then make that complex. Use T1 or T2 if it is the
768 required type. */
769 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
770 {
771 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
772 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 773 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
774
775 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
776 return t1;
777 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
778 return t2;
779 else
780 return build_complex_type (subtype);
781 }
782
783 /* If only one is real, use it as the result. */
784
785 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
786 return t1;
787
788 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
789 return t2;
790
9a8ce21f
JG
791 /* If both are real and either are decimal floating point types, use
792 the decimal floating point type with the greater precision. */
793
794 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
795 {
796 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
798 return dfloat128_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
801 return dfloat64_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
804 return dfloat32_type_node;
805 }
806
ab22c1fa
CF
807 /* Deal with fixed-point types. */
808 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
809 {
810 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 811 machine_mode m1, m2;
ab22c1fa
CF
812 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
813
814 m1 = TYPE_MODE (t1);
815 m2 = TYPE_MODE (t2);
816
817 /* If one input type is saturating, the result type is saturating. */
818 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
819 satp = 1;
820
821 /* If both fixed-point types are unsigned, the result type is unsigned.
822 When mixing fixed-point and integer types, follow the sign of the
823 fixed-point type.
824 Otherwise, the result type is signed. */
825 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
826 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
827 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t1))
829 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t2)))
831 unsignedp = 1;
832
833 /* The result type is signed. */
834 if (unsignedp == 0)
835 {
836 /* If the input type is unsigned, we need to convert to the
837 signed type. */
838 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
839 {
d75d71e0 840 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
841 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
848 }
849 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
850 {
d75d71e0 851 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
852 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
853 mclass = MODE_FRACT;
854 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
855 mclass = MODE_ACCUM;
856 else
857 gcc_unreachable ();
858 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
859 }
860 }
861
862 if (code1 == FIXED_POINT_TYPE)
863 {
864 fbit1 = GET_MODE_FBIT (m1);
865 ibit1 = GET_MODE_IBIT (m1);
866 }
867 else
868 {
869 fbit1 = 0;
870 /* Signed integers need to subtract one sign bit. */
871 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
872 }
873
874 if (code2 == FIXED_POINT_TYPE)
875 {
876 fbit2 = GET_MODE_FBIT (m2);
877 ibit2 = GET_MODE_IBIT (m2);
878 }
879 else
880 {
881 fbit2 = 0;
882 /* Signed integers need to subtract one sign bit. */
883 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
884 }
885
886 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
887 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
888 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
889 satp);
890 }
891
10bc1b1b
JM
892 /* Both real or both integers; use the one with greater precision. */
893
894 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
895 return t1;
896 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
897 return t2;
898
899 /* Same precision. Prefer long longs to longs to ints when the
900 same precision, following the C99 rules on integer type rank
901 (which are equivalent to the C90 rules for C90 types). */
902
903 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
905 return long_long_unsigned_type_node;
906
907 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
909 {
910 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
911 return long_long_unsigned_type_node;
912 else
c22cacf3 913 return long_long_integer_type_node;
10bc1b1b
JM
914 }
915
916 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
918 return long_unsigned_type_node;
919
920 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
922 {
923 /* But preserve unsignedness from the other type,
924 since long cannot hold all the values of an unsigned int. */
925 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
926 return long_unsigned_type_node;
927 else
928 return long_integer_type_node;
929 }
930
931 /* Likewise, prefer long double to double even if same size. */
932 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
933 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
934 return long_double_type_node;
935
2531a1d9
JR
936 /* Likewise, prefer double to float even if same size.
937 We got a couple of embedded targets with 32 bit doubles, and the
938 pdp11 might have 64 bit floats. */
939 if (TYPE_MAIN_VARIANT (t1) == double_type_node
940 || TYPE_MAIN_VARIANT (t2) == double_type_node)
941 return double_type_node;
942
10bc1b1b
JM
943 /* Otherwise prefer the unsigned one. */
944
945 if (TYPE_UNSIGNED (t1))
946 return t1;
947 else
948 return t2;
949}
400fbf9f 950\f
5922c215
JM
951/* Wrapper around c_common_type that is used by c-common.c and other
952 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
953 are allowed here and are converted to their compatible integer types.
954 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
955 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
956tree
957common_type (tree t1, tree t2)
958{
959 if (TREE_CODE (t1) == ENUMERAL_TYPE)
960 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
961 if (TREE_CODE (t2) == ENUMERAL_TYPE)
962 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
963
964 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
965 if (TREE_CODE (t1) == BOOLEAN_TYPE
966 && TREE_CODE (t2) == BOOLEAN_TYPE)
967 return boolean_type_node;
968
969 /* If either type is BOOLEAN_TYPE, then return the other. */
970 if (TREE_CODE (t1) == BOOLEAN_TYPE)
971 return t2;
972 if (TREE_CODE (t2) == BOOLEAN_TYPE)
973 return t1;
974
ccf7f880
JJ
975 return c_common_type (t1, t2);
976}
f13c9b2c 977
400fbf9f
JW
978/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
979 or various other operations. Return 2 if they are compatible
980 but a warning may be needed if you use them together. */
981
982int
132da1a5 983comptypes (tree type1, tree type2)
f13c9b2c
AP
984{
985 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
986 int val;
987
dc5027f4 988 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
989 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
990
991 return val;
992}
993
994/* Like comptypes, but if it returns non-zero because enum and int are
995 compatible, it sets *ENUM_AND_INT_P to true. */
996
997static int
998comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
999{
1000 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1001 int val;
1002
dc5027f4
JM
1003 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1004 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1005
1006 return val;
1007}
1008
1009/* Like comptypes, but if it returns nonzero for different types, it
1010 sets *DIFFERENT_TYPES_P to true. */
1011
1012int
1013comptypes_check_different_types (tree type1, tree type2,
1014 bool *different_types_p)
1015{
1016 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1017 int val;
1018
1019 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1020 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1021
f13c9b2c 1022 return val;
c22cacf3
MS
1023}
1024\f
f13c9b2c
AP
1025/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1026 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1027 but a warning may be needed if you use them together. If
1028 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1029 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1030 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1031 NULL, and the types are compatible but different enough not to be
48b0b196 1032 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1033 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1034 false, but may or may not be set if the types are incompatible.
1035 This differs from comptypes, in that we don't free the seen
1036 types. */
f13c9b2c
AP
1037
1038static int
dc5027f4
JM
1039comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1040 bool *different_types_p)
400fbf9f 1041{
58f9752a
KG
1042 const_tree t1 = type1;
1043 const_tree t2 = type2;
4b027d16 1044 int attrval, val;
400fbf9f
JW
1045
1046 /* Suppress errors caused by previously reported errors. */
1047
8d47dfc5
RH
1048 if (t1 == t2 || !t1 || !t2
1049 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1050 return 1;
1051
bca63328
JM
1052 /* Enumerated types are compatible with integer types, but this is
1053 not transitive: two enumerated types in the same translation unit
1054 are compatible with each other only if they are the same type. */
400fbf9f 1055
bca63328 1056 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1057 {
1058 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1059 if (TREE_CODE (t2) != VOID_TYPE)
1060 {
1061 if (enum_and_int_p != NULL)
1062 *enum_and_int_p = true;
1063 if (different_types_p != NULL)
1064 *different_types_p = true;
1065 }
744aa42f 1066 }
bca63328 1067 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1068 {
1069 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1070 if (TREE_CODE (t1) != VOID_TYPE)
1071 {
1072 if (enum_and_int_p != NULL)
1073 *enum_and_int_p = true;
1074 if (different_types_p != NULL)
1075 *different_types_p = true;
1076 }
744aa42f 1077 }
400fbf9f
JW
1078
1079 if (t1 == t2)
1080 return 1;
1081
1082 /* Different classes of types can't be compatible. */
1083
3aeb3655
EC
1084 if (TREE_CODE (t1) != TREE_CODE (t2))
1085 return 0;
400fbf9f 1086
118a3a8b 1087 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1088
3932261a 1089 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1090 return 0;
1091
08632da2
RS
1092 /* Allow for two different type nodes which have essentially the same
1093 definition. Note that we already checked for equality of the type
38e01259 1094 qualifiers (just above). */
400fbf9f 1095
46df2823
JM
1096 if (TREE_CODE (t1) != ARRAY_TYPE
1097 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1098 return 1;
1099
4b027d16 1100 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1101 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1102 return 0;
1103
1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1105 val = 0;
1106
400fbf9f
JW
1107 switch (TREE_CODE (t1))
1108 {
1109 case POINTER_TYPE:
106f5de5
UW
1110 /* Do not remove mode or aliasing information. */
1111 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1112 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1113 break;
4b027d16 1114 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1115 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1116 enum_and_int_p, different_types_p));
4b027d16 1117 break;
400fbf9f
JW
1118
1119 case FUNCTION_TYPE:
dc5027f4
JM
1120 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1121 different_types_p);
4b027d16 1122 break;
400fbf9f
JW
1123
1124 case ARRAY_TYPE:
1125 {
400fbf9f
JW
1126 tree d1 = TYPE_DOMAIN (t1);
1127 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1128 bool d1_variable, d2_variable;
1129 bool d1_zero, d2_zero;
4b027d16 1130 val = 1;
400fbf9f
JW
1131
1132 /* Target types must match incl. qualifiers. */
1133 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1134 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1135 enum_and_int_p,
1136 different_types_p)))
400fbf9f
JW
1137 return 0;
1138
dc5027f4
JM
1139 if (different_types_p != NULL
1140 && (d1 == 0) != (d2 == 0))
1141 *different_types_p = true;
400fbf9f 1142 /* Sizes must match unless one is missing or variable. */
3f85558f 1143 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1144 break;
400fbf9f 1145
3f75a254
JM
1146 d1_zero = !TYPE_MAX_VALUE (d1);
1147 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1148
3f75a254 1149 d1_variable = (!d1_zero
3f85558f
RH
1150 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1151 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1152 d2_variable = (!d2_zero
3f85558f
RH
1153 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1154 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1155 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1156 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1157
dc5027f4
JM
1158 if (different_types_p != NULL
1159 && d1_variable != d2_variable)
1160 *different_types_p = true;
3f85558f
RH
1161 if (d1_variable || d2_variable)
1162 break;
1163 if (d1_zero && d2_zero)
1164 break;
1165 if (d1_zero || d2_zero
3f75a254
JM
1166 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1167 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1168 val = 0;
1169
c22cacf3 1170 break;
400fbf9f
JW
1171 }
1172
d1bd0ded 1173 case ENUMERAL_TYPE:
58393038 1174 case RECORD_TYPE:
d1bd0ded 1175 case UNION_TYPE:
766beae1 1176 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1177 {
fcb99e7b
JJ
1178 tree a1 = TYPE_ATTRIBUTES (t1);
1179 tree a2 = TYPE_ATTRIBUTES (t2);
1180
1181 if (! attribute_list_contained (a1, a2)
1182 && ! attribute_list_contained (a2, a1))
1183 break;
1184
f13c9b2c 1185 if (attrval != 2)
dc5027f4
JM
1186 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1187 different_types_p);
1188 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1189 different_types_p);
f13c9b2c 1190 }
4b027d16 1191 break;
e9a25f70 1192
62e1dfcf 1193 case VECTOR_TYPE:
744aa42f
ILT
1194 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1195 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1196 enum_and_int_p, different_types_p));
62e1dfcf
NC
1197 break;
1198
e9a25f70
JL
1199 default:
1200 break;
400fbf9f 1201 }
4b027d16 1202 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1203}
1204
36c5e70a
BE
1205/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1206 their qualifiers, except for named address spaces. If the pointers point to
1207 different named addresses, then we must determine if one address space is a
1208 subset of the other. */
400fbf9f
JW
1209
1210static int
744aa42f 1211comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1212{
392202b0 1213 int val;
768952be 1214 int val_ped;
36c5e70a
BE
1215 tree mvl = TREE_TYPE (ttl);
1216 tree mvr = TREE_TYPE (ttr);
1217 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1218 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1219 addr_space_t as_common;
744aa42f 1220 bool enum_and_int_p;
8b40563c 1221
36c5e70a
BE
1222 /* Fail if pointers point to incompatible address spaces. */
1223 if (!addr_space_superset (asl, asr, &as_common))
1224 return 0;
1225
768952be
MU
1226 /* For pedantic record result of comptypes on arrays before losing
1227 qualifiers on the element type below. */
1228 val_ped = 1;
1229
1230 if (TREE_CODE (mvl) == ARRAY_TYPE
1231 && TREE_CODE (mvr) == ARRAY_TYPE)
1232 val_ped = comptypes (mvl, mvr);
1233
1234 /* Qualifiers on element types of array types that are
1235 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1236
1237 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1238 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1239 : TYPE_MAIN_VARIANT (mvl));
1240
1241 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1242 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1243 : TYPE_MAIN_VARIANT (mvr));
1244
744aa42f
ILT
1245 enum_and_int_p = false;
1246 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1247
768952be
MU
1248 if (val == 1 && val_ped != 1)
1249 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1250 "are incompatible in ISO C");
1251
fcf73884 1252 if (val == 2)
c1771a20 1253 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1254
1255 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1256 warning_at (location, OPT_Wc___compat,
1257 "pointer target types incompatible in C++");
1258
400fbf9f
JW
1259 return val;
1260}
1261\f
1262/* Subroutines of `comptypes'. */
1263
f75fbaf7
ZW
1264/* Determine whether two trees derive from the same translation unit.
1265 If the CONTEXT chain ends in a null, that tree's context is still
1266 being parsed, so if two trees have context chains ending in null,
766beae1 1267 they're in the same translation unit. */
f75fbaf7 1268int
58f9752a 1269same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1270{
1271 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1272 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1273 {
6615c446
JO
1274 case tcc_declaration:
1275 t1 = DECL_CONTEXT (t1); break;
1276 case tcc_type:
1277 t1 = TYPE_CONTEXT (t1); break;
1278 case tcc_exceptional:
1279 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1280 default: gcc_unreachable ();
766beae1
ZW
1281 }
1282
1283 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1284 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1285 {
6615c446
JO
1286 case tcc_declaration:
1287 t2 = DECL_CONTEXT (t2); break;
1288 case tcc_type:
1289 t2 = TYPE_CONTEXT (t2); break;
1290 case tcc_exceptional:
1291 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1292 default: gcc_unreachable ();
766beae1
ZW
1293 }
1294
1295 return t1 == t2;
1296}
1297
f13c9b2c 1298/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1299
f13c9b2c 1300static struct tagged_tu_seen_cache *
58f9752a 1301alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1302{
cceb1885 1303 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1304 tu->next = tagged_tu_seen_base;
1305 tu->t1 = t1;
1306 tu->t2 = t2;
c22cacf3 1307
f13c9b2c 1308 tagged_tu_seen_base = tu;
c22cacf3 1309
f13c9b2c
AP
1310 /* The C standard says that two structures in different translation
1311 units are compatible with each other only if the types of their
1312 fields are compatible (among other things). We assume that they
1313 are compatible until proven otherwise when building the cache.
1314 An example where this can occur is:
1315 struct a
1316 {
1317 struct a *next;
1318 };
1319 If we are comparing this against a similar struct in another TU,
c83eecad 1320 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1321 loop. */
1322 tu->val = 1;
1323 return tu;
1324}
d1bd0ded 1325
f13c9b2c 1326/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1327
f13c9b2c
AP
1328static void
1329free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1330{
1331 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1332 while (tu != tu_til)
1333 {
741ac903
KG
1334 const struct tagged_tu_seen_cache *const tu1
1335 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1336 tu = tu1->next;
b1d5455a 1337 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1338 }
1339 tagged_tu_seen_base = tu_til;
1340}
d1bd0ded
GK
1341
1342/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1343 compatible. If the two types are not the same (which has been
1344 checked earlier), this can only happen when multiple translation
1345 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1346 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1347 comptypes_internal. */
d1bd0ded
GK
1348
1349static int
744aa42f 1350tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1351 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1352{
1353 tree s1, s2;
1354 bool needs_warning = false;
3aeb3655 1355
d1bd0ded
GK
1356 /* We have to verify that the tags of the types are the same. This
1357 is harder than it looks because this may be a typedef, so we have
1358 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1359 typedef...
1360 In the case of compiler-created builtin structs the TYPE_DECL
1361 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1362 while (TYPE_NAME (t1)
1363 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1364 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1365 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1366
dea984dc
ILT
1367 while (TYPE_NAME (t2)
1368 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1369 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1370 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1371
1372 /* C90 didn't have the requirement that the two tags be the same. */
1373 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1374 return 0;
3aeb3655 1375
d1bd0ded
GK
1376 /* C90 didn't say what happened if one or both of the types were
1377 incomplete; we choose to follow C99 rules here, which is that they
1378 are compatible. */
1379 if (TYPE_SIZE (t1) == NULL
1380 || TYPE_SIZE (t2) == NULL)
1381 return 1;
3aeb3655 1382
d1bd0ded 1383 {
f13c9b2c 1384 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1385 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1386 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1387 return tts_i->val;
d1bd0ded 1388 }
3aeb3655 1389
d1bd0ded
GK
1390 switch (TREE_CODE (t1))
1391 {
1392 case ENUMERAL_TYPE:
1393 {
f13c9b2c 1394 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1395 /* Speed up the case where the type values are in the same order. */
1396 tree tv1 = TYPE_VALUES (t1);
1397 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1398
c22cacf3 1399 if (tv1 == tv2)
f13c9b2c
AP
1400 {
1401 return 1;
1402 }
3aeb3655 1403
c22cacf3
MS
1404 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1405 {
1406 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1407 break;
1408 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1409 {
c22cacf3 1410 tu->val = 0;
f13c9b2c
AP
1411 return 0;
1412 }
c22cacf3 1413 }
3aeb3655 1414
c22cacf3 1415 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1416 {
1417 return 1;
1418 }
c22cacf3 1419 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1420 {
1421 tu->val = 0;
1422 return 0;
1423 }
3aeb3655 1424
d1bd0ded 1425 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1426 {
1427 tu->val = 0;
1428 return 0;
1429 }
3aeb3655 1430
d1bd0ded
GK
1431 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1432 {
1433 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1434 if (s2 == NULL
1435 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1436 {
1437 tu->val = 0;
1438 return 0;
1439 }
d1bd0ded
GK
1440 }
1441 return 1;
1442 }
1443
1444 case UNION_TYPE:
1445 {
f13c9b2c 1446 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1447 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1448 {
1449 tu->val = 0;
1450 return 0;
1451 }
c22cacf3 1452
f13c9b2c
AP
1453 /* Speed up the common case where the fields are in the same order. */
1454 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1455 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1456 {
1457 int result;
c22cacf3 1458
3ae4d3cc 1459 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1460 break;
744aa42f 1461 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1462 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1463
1464 if (result != 1 && !DECL_NAME (s1))
1465 break;
f13c9b2c
AP
1466 if (result == 0)
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
1471 if (result == 2)
1472 needs_warning = true;
1473
1474 if (TREE_CODE (s1) == FIELD_DECL
1475 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1476 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1477 {
1478 tu->val = 0;
1479 return 0;
1480 }
1481 }
1482 if (!s1 && !s2)
1483 {
1484 tu->val = needs_warning ? 2 : 1;
1485 return tu->val;
1486 }
d1bd0ded 1487
910ad8de 1488 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1489 {
1490 bool ok = false;
3aeb3655 1491
910ad8de 1492 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1493 if (DECL_NAME (s1) == DECL_NAME (s2))
1494 {
1495 int result;
1496
744aa42f 1497 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1498 enum_and_int_p,
1499 different_types_p);
3ae4d3cc
AO
1500
1501 if (result != 1 && !DECL_NAME (s1))
1502 continue;
1503 if (result == 0)
1504 {
1505 tu->val = 0;
1506 return 0;
1507 }
1508 if (result == 2)
1509 needs_warning = true;
1510
1511 if (TREE_CODE (s1) == FIELD_DECL
1512 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1513 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1514 break;
3ae4d3cc
AO
1515
1516 ok = true;
1517 break;
1518 }
3f75a254 1519 if (!ok)
f13c9b2c
AP
1520 {
1521 tu->val = 0;
1522 return 0;
1523 }
d1bd0ded 1524 }
f13c9b2c
AP
1525 tu->val = needs_warning ? 2 : 10;
1526 return tu->val;
d1bd0ded
GK
1527 }
1528
1529 case RECORD_TYPE:
1530 {
c22cacf3 1531 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1532
1533 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1534 s1 && s2;
910ad8de 1535 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1536 {
1537 int result;
1538 if (TREE_CODE (s1) != TREE_CODE (s2)
1539 || DECL_NAME (s1) != DECL_NAME (s2))
1540 break;
744aa42f 1541 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1542 enum_and_int_p, different_types_p);
d1bd0ded
GK
1543 if (result == 0)
1544 break;
1545 if (result == 2)
1546 needs_warning = true;
3aeb3655 1547
d1bd0ded
GK
1548 if (TREE_CODE (s1) == FIELD_DECL
1549 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1550 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1551 break;
1552 }
d1bd0ded 1553 if (s1 && s2)
f13c9b2c
AP
1554 tu->val = 0;
1555 else
1556 tu->val = needs_warning ? 2 : 1;
1557 return tu->val;
d1bd0ded
GK
1558 }
1559
1560 default:
366de0ce 1561 gcc_unreachable ();
d1bd0ded
GK
1562 }
1563}
1564
400fbf9f
JW
1565/* Return 1 if two function types F1 and F2 are compatible.
1566 If either type specifies no argument types,
1567 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1568 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1569 the other must specify that number of self-promoting arg types.
744aa42f 1570 Otherwise, the argument types must match.
dc5027f4 1571 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1572
1573static int
744aa42f 1574function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1575 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1576{
1577 tree args1, args2;
1578 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1579 int val = 1;
1580 int val1;
a6fdc086
GK
1581 tree ret1, ret2;
1582
1583 ret1 = TREE_TYPE (f1);
1584 ret2 = TREE_TYPE (f2);
1585
e508a019
JM
1586 /* 'volatile' qualifiers on a function's return type used to mean
1587 the function is noreturn. */
1588 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1589 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1590 if (TYPE_VOLATILE (ret1))
1591 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1592 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1593 if (TYPE_VOLATILE (ret2))
1594 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1595 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1596 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1597 if (val == 0)
400fbf9f
JW
1598 return 0;
1599
1600 args1 = TYPE_ARG_TYPES (f1);
1601 args2 = TYPE_ARG_TYPES (f2);
1602
dc5027f4
JM
1603 if (different_types_p != NULL
1604 && (args1 == 0) != (args2 == 0))
1605 *different_types_p = true;
1606
400fbf9f
JW
1607 /* An unspecified parmlist matches any specified parmlist
1608 whose argument types don't need default promotions. */
1609
1610 if (args1 == 0)
1611 {
1612 if (!self_promoting_args_p (args2))
1613 return 0;
1614 /* If one of these types comes from a non-prototype fn definition,
1615 compare that with the other type's arglist.
3176a0c2 1616 If they don't match, ask for a warning (but no error). */
400fbf9f 1617 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1618 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1619 enum_and_int_p, different_types_p))
400fbf9f
JW
1620 val = 2;
1621 return val;
1622 }
1623 if (args2 == 0)
1624 {
1625 if (!self_promoting_args_p (args1))
1626 return 0;
1627 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1628 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1629 enum_and_int_p, different_types_p))
400fbf9f
JW
1630 val = 2;
1631 return val;
1632 }
1633
1634 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1635 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1636 different_types_p);
400fbf9f
JW
1637 return val1 != 1 ? val1 : val;
1638}
1639
744aa42f
ILT
1640/* Check two lists of types for compatibility, returning 0 for
1641 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1642 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1643 comptypes_internal. */
400fbf9f
JW
1644
1645static int
744aa42f 1646type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1647 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1648{
1649 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1650 int val = 1;
9d5f3e49 1651 int newval = 0;
400fbf9f
JW
1652
1653 while (1)
1654 {
46df2823 1655 tree a1, mv1, a2, mv2;
400fbf9f
JW
1656 if (args1 == 0 && args2 == 0)
1657 return val;
1658 /* If one list is shorter than the other,
1659 they fail to match. */
1660 if (args1 == 0 || args2 == 0)
1661 return 0;
46df2823
JM
1662 mv1 = a1 = TREE_VALUE (args1);
1663 mv2 = a2 = TREE_VALUE (args2);
1664 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1665 mv1 = (TYPE_ATOMIC (mv1)
1666 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1667 TYPE_QUAL_ATOMIC)
1668 : TYPE_MAIN_VARIANT (mv1));
46df2823 1669 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1670 mv2 = (TYPE_ATOMIC (mv2)
1671 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1672 TYPE_QUAL_ATOMIC)
1673 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1674 /* A null pointer instead of a type
1675 means there is supposed to be an argument
1676 but nothing is specified about what type it has.
1677 So match anything that self-promotes. */
dc5027f4
JM
1678 if (different_types_p != NULL
1679 && (a1 == 0) != (a2 == 0))
1680 *different_types_p = true;
46df2823 1681 if (a1 == 0)
400fbf9f 1682 {
46df2823 1683 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1684 return 0;
1685 }
46df2823 1686 else if (a2 == 0)
400fbf9f 1687 {
46df2823 1688 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1689 return 0;
1690 }
8f5b6d29 1691 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1692 else if (TREE_CODE (a1) == ERROR_MARK
1693 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1694 ;
dc5027f4
JM
1695 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1696 different_types_p)))
400fbf9f 1697 {
dc5027f4
JM
1698 if (different_types_p != NULL)
1699 *different_types_p = true;
400fbf9f
JW
1700 /* Allow wait (union {union wait *u; int *i} *)
1701 and wait (union wait *) to be compatible. */
46df2823
JM
1702 if (TREE_CODE (a1) == UNION_TYPE
1703 && (TYPE_NAME (a1) == 0
ebf0bf7f 1704 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1705 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1706 && tree_int_cst_equal (TYPE_SIZE (a1),
1707 TYPE_SIZE (a2)))
400fbf9f
JW
1708 {
1709 tree memb;
46df2823 1710 for (memb = TYPE_FIELDS (a1);
910ad8de 1711 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1712 {
1713 tree mv3 = TREE_TYPE (memb);
1714 if (mv3 && mv3 != error_mark_node
1715 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1716 mv3 = (TYPE_ATOMIC (mv3)
1717 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1718 TYPE_QUAL_ATOMIC)
1719 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1720 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1721 different_types_p))
58cb41e6
JJ
1722 break;
1723 }
400fbf9f
JW
1724 if (memb == 0)
1725 return 0;
1726 }
46df2823
JM
1727 else if (TREE_CODE (a2) == UNION_TYPE
1728 && (TYPE_NAME (a2) == 0
ebf0bf7f 1729 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1730 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1731 && tree_int_cst_equal (TYPE_SIZE (a2),
1732 TYPE_SIZE (a1)))
400fbf9f
JW
1733 {
1734 tree memb;
46df2823 1735 for (memb = TYPE_FIELDS (a2);
910ad8de 1736 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1737 {
1738 tree mv3 = TREE_TYPE (memb);
1739 if (mv3 && mv3 != error_mark_node
1740 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1741 mv3 = (TYPE_ATOMIC (mv3)
1742 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1743 TYPE_QUAL_ATOMIC)
1744 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1745 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1746 different_types_p))
58cb41e6
JJ
1747 break;
1748 }
400fbf9f
JW
1749 if (memb == 0)
1750 return 0;
1751 }
1752 else
1753 return 0;
1754 }
1755
1756 /* comptypes said ok, but record if it said to warn. */
1757 if (newval > val)
1758 val = newval;
1759
1760 args1 = TREE_CHAIN (args1);
1761 args2 = TREE_CHAIN (args2);
1762 }
1763}
400fbf9f 1764\f
a0e24419
MP
1765/* Compute the size to increment a pointer by. When a function type or void
1766 type or incomplete type is passed, size_one_node is returned.
1767 This function does not emit any diagnostics; the caller is responsible
1768 for that. */
400fbf9f 1769
4e2fb7de 1770static tree
58f9752a 1771c_size_in_bytes (const_tree type)
400fbf9f
JW
1772{
1773 enum tree_code code = TREE_CODE (type);
1774
a0e24419
MP
1775 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1776 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1777 return size_one_node;
1778
400fbf9f 1779 /* Convert in case a char is more than one unit. */
db3927fb
AH
1780 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1781 size_int (TYPE_PRECISION (char_type_node)
1782 / BITS_PER_UNIT));
400fbf9f 1783}
400fbf9f 1784\f
400fbf9f
JW
1785/* Return either DECL or its known constant value (if it has one). */
1786
56cb9733 1787tree
2f6e4e97 1788decl_constant_value (tree decl)
400fbf9f 1789{
a7c1916a 1790 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1791 in a place where a variable is invalid. Note that DECL_INITIAL
1792 isn't valid for a PARM_DECL. */
a7c1916a 1793 current_function_decl != 0
4f976745 1794 && TREE_CODE (decl) != PARM_DECL
3f75a254 1795 && !TREE_THIS_VOLATILE (decl)
83bab8db 1796 && TREE_READONLY (decl)
400fbf9f
JW
1797 && DECL_INITIAL (decl) != 0
1798 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1799 /* This is invalid if initial value is not constant.
1800 If it has either a function call, a memory reference,
1801 or a variable, then re-evaluating it could give different results. */
1802 && TREE_CONSTANT (DECL_INITIAL (decl))
1803 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1804 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1805 return DECL_INITIAL (decl);
1806 return decl;
1807}
1808
f2a71bbc
JM
1809/* Convert the array expression EXP to a pointer. */
1810static tree
c2255bc4 1811array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1812{
f2a71bbc 1813 tree orig_exp = exp;
207bf485 1814 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1815 tree adr;
1816 tree restype = TREE_TYPE (type);
1817 tree ptrtype;
207bf485 1818
f2a71bbc 1819 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1820
f2a71bbc 1821 STRIP_TYPE_NOPS (exp);
207bf485 1822
487a92fe
JM
1823 if (TREE_NO_WARNING (orig_exp))
1824 TREE_NO_WARNING (exp) = 1;
207bf485 1825
f2a71bbc
JM
1826 ptrtype = build_pointer_type (restype);
1827
22d03525 1828 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1829 return convert (ptrtype, TREE_OPERAND (exp, 0));
1830
1f37c583
JM
1831 /* In C++ array compound literals are temporary objects unless they are
1832 const or appear in namespace scope, so they are destroyed too soon
1833 to use them for much of anything (c++/53220). */
1834 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1835 {
1836 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1837 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1838 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1839 "converting an array compound literal to a pointer "
1840 "is ill-formed in C++");
1841 }
1842
c2255bc4 1843 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1844 return convert (ptrtype, adr);
1845}
207bf485 1846
f2a71bbc
JM
1847/* Convert the function expression EXP to a pointer. */
1848static tree
c2255bc4 1849function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1850{
1851 tree orig_exp = exp;
207bf485 1852
f2a71bbc 1853 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1854
f2a71bbc 1855 STRIP_TYPE_NOPS (exp);
207bf485 1856
f2a71bbc
JM
1857 if (TREE_NO_WARNING (orig_exp))
1858 TREE_NO_WARNING (exp) = 1;
207bf485 1859
c2255bc4 1860 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1861}
207bf485 1862
ebfbbdc5
JJ
1863/* Mark EXP as read, not just set, for set but not used -Wunused
1864 warning purposes. */
1865
1866void
1867mark_exp_read (tree exp)
1868{
1869 switch (TREE_CODE (exp))
1870 {
1871 case VAR_DECL:
1872 case PARM_DECL:
1873 DECL_READ_P (exp) = 1;
1874 break;
1875 case ARRAY_REF:
1876 case COMPONENT_REF:
1877 case MODIFY_EXPR:
1878 case REALPART_EXPR:
1879 case IMAGPART_EXPR:
1880 CASE_CONVERT:
1881 case ADDR_EXPR:
1882 mark_exp_read (TREE_OPERAND (exp, 0));
1883 break;
1884 case COMPOUND_EXPR:
82c3c067 1885 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1886 mark_exp_read (TREE_OPERAND (exp, 1));
1887 break;
1888 default:
1889 break;
1890 }
1891}
1892
f2a71bbc
JM
1893/* Perform the default conversion of arrays and functions to pointers.
1894 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1895 return EXP.
1896
1897 LOC is the location of the expression. */
f2a71bbc
JM
1898
1899struct c_expr
c2255bc4 1900default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1901{
1902 tree orig_exp = exp.value;
1903 tree type = TREE_TYPE (exp.value);
1904 enum tree_code code = TREE_CODE (type);
1905
1906 switch (code)
1907 {
1908 case ARRAY_TYPE:
1909 {
1910 bool not_lvalue = false;
1911 bool lvalue_array_p;
1912
1913 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1914 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1915 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1916 {
1917 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1918 not_lvalue = true;
1919 exp.value = TREE_OPERAND (exp.value, 0);
1920 }
1921
1922 if (TREE_NO_WARNING (orig_exp))
1923 TREE_NO_WARNING (exp.value) = 1;
1924
1925 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1926 if (!flag_isoc99 && !lvalue_array_p)
1927 {
1928 /* Before C99, non-lvalue arrays do not decay to pointers.
1929 Normally, using such an array would be invalid; but it can
1930 be used correctly inside sizeof or as a statement expression.
1931 Thus, do not give an error here; an error will result later. */
1932 return exp;
1933 }
1934
c2255bc4 1935 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1936 }
1937 break;
1938 case FUNCTION_TYPE:
c2255bc4 1939 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1940 break;
1941 default:
f2a71bbc 1942 break;
207bf485 1943 }
f2a71bbc 1944
207bf485
JM
1945 return exp;
1946}
1947
ebfbbdc5
JJ
1948struct c_expr
1949default_function_array_read_conversion (location_t loc, struct c_expr exp)
1950{
1951 mark_exp_read (exp.value);
1952 return default_function_array_conversion (loc, exp);
1953}
522ddfa2 1954
267bac10
JM
1955/* Return whether EXPR should be treated as an atomic lvalue for the
1956 purposes of load and store handling. */
1957
1958static bool
1959really_atomic_lvalue (tree expr)
1960{
7a0ca710 1961 if (error_operand_p (expr))
267bac10
JM
1962 return false;
1963 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1964 return false;
1965 if (!lvalue_p (expr))
1966 return false;
1967
1968 /* Ignore _Atomic on register variables, since their addresses can't
1969 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1970 sequences wouldn't work. Ignore _Atomic on structures containing
1971 bit-fields, since accessing elements of atomic structures or
1972 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1973 it's undefined at translation time or execution time, and the
1974 normal atomic sequences again wouldn't work. */
1975 while (handled_component_p (expr))
1976 {
1977 if (TREE_CODE (expr) == COMPONENT_REF
1978 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1979 return false;
1980 expr = TREE_OPERAND (expr, 0);
1981 }
1982 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1983 return false;
1984 return true;
1985}
1986
1987/* Convert expression EXP (location LOC) from lvalue to rvalue,
1988 including converting functions and arrays to pointers if CONVERT_P.
1989 If READ_P, also mark the expression as having been read. */
1990
1991struct c_expr
1992convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1993 bool convert_p, bool read_p)
1994{
1995 if (read_p)
1996 mark_exp_read (exp.value);
1997 if (convert_p)
1998 exp = default_function_array_conversion (loc, exp);
1999 if (really_atomic_lvalue (exp.value))
2000 {
2001 vec<tree, va_gc> *params;
2002 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2003 tree expr_type = TREE_TYPE (exp.value);
2004 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2005 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2006
2007 gcc_assert (TYPE_ATOMIC (expr_type));
2008
2009 /* Expansion of a generic atomic load may require an addition
2010 element, so allocate enough to prevent a resize. */
2011 vec_alloc (params, 4);
2012
2013 /* Remove the qualifiers for the rest of the expressions and
2014 create the VAL temp variable to hold the RHS. */
2015 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2016 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2017 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2018 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2019 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2020
2021 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2022 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2023 params->quick_push (expr_addr);
2024 params->quick_push (tmp_addr);
2025 params->quick_push (seq_cst);
8edbfaa6 2026 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2027
cc28fc7f
MP
2028 /* EXPR is always read. */
2029 mark_exp_read (exp.value);
2030
267bac10 2031 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2032 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2033 NULL_TREE, NULL_TREE);
267bac10
JM
2034 }
2035 return exp;
2036}
2037
522ddfa2
JM
2038/* EXP is an expression of integer type. Apply the integer promotions
2039 to it and return the promoted value. */
400fbf9f
JW
2040
2041tree
522ddfa2 2042perform_integral_promotions (tree exp)
400fbf9f 2043{
b3694847
SS
2044 tree type = TREE_TYPE (exp);
2045 enum tree_code code = TREE_CODE (type);
400fbf9f 2046
522ddfa2 2047 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2048
400fbf9f
JW
2049 /* Normally convert enums to int,
2050 but convert wide enums to something wider. */
2051 if (code == ENUMERAL_TYPE)
2052 {
b0c48229
NB
2053 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2054 TYPE_PRECISION (integer_type_node)),
2055 ((TYPE_PRECISION (type)
2056 >= TYPE_PRECISION (integer_type_node))
8df83eae 2057 && TYPE_UNSIGNED (type)));
05bccae2 2058
400fbf9f
JW
2059 return convert (type, exp);
2060 }
2061
522ddfa2
JM
2062 /* ??? This should no longer be needed now bit-fields have their
2063 proper types. */
9753f113 2064 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2065 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2066 /* If it's thinner than an int, promote it like a
d72040f5 2067 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2068 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2069 TYPE_PRECISION (integer_type_node)))
f458d1d5 2070 return convert (integer_type_node, exp);
9753f113 2071
d72040f5 2072 if (c_promoting_integer_type_p (type))
400fbf9f 2073 {
f458d1d5 2074 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2075 if (TYPE_UNSIGNED (type)
f458d1d5 2076 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2077 return convert (unsigned_type_node, exp);
05bccae2 2078
400fbf9f
JW
2079 return convert (integer_type_node, exp);
2080 }
05bccae2 2081
522ddfa2
JM
2082 return exp;
2083}
2084
2085
2086/* Perform default promotions for C data used in expressions.
46bdb9cf 2087 Enumeral types or short or char are converted to int.
522ddfa2
JM
2088 In addition, manifest constants symbols are replaced by their values. */
2089
2090tree
2091default_conversion (tree exp)
2092{
2093 tree orig_exp;
2094 tree type = TREE_TYPE (exp);
2095 enum tree_code code = TREE_CODE (type);
40449a90 2096 tree promoted_type;
522ddfa2 2097
ebfbbdc5
JJ
2098 mark_exp_read (exp);
2099
46bdb9cf
JM
2100 /* Functions and arrays have been converted during parsing. */
2101 gcc_assert (code != FUNCTION_TYPE);
2102 if (code == ARRAY_TYPE)
2103 return exp;
522ddfa2
JM
2104
2105 /* Constants can be used directly unless they're not loadable. */
2106 if (TREE_CODE (exp) == CONST_DECL)
2107 exp = DECL_INITIAL (exp);
2108
522ddfa2
JM
2109 /* Strip no-op conversions. */
2110 orig_exp = exp;
2111 STRIP_TYPE_NOPS (exp);
2112
2113 if (TREE_NO_WARNING (orig_exp))
2114 TREE_NO_WARNING (exp) = 1;
2115
400fbf9f
JW
2116 if (code == VOID_TYPE)
2117 {
5436fa2e
MP
2118 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2119 "void value not ignored as it ought to be");
400fbf9f
JW
2120 return error_mark_node;
2121 }
808d6eaa
JM
2122
2123 exp = require_complete_type (exp);
2124 if (exp == error_mark_node)
2125 return error_mark_node;
2126
40449a90
SL
2127 promoted_type = targetm.promoted_type (type);
2128 if (promoted_type)
2129 return convert (promoted_type, exp);
2130
808d6eaa
JM
2131 if (INTEGRAL_TYPE_P (type))
2132 return perform_integral_promotions (exp);
2133
400fbf9f
JW
2134 return exp;
2135}
2136\f
0fb96aa4 2137/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2138
2139 If the component name is not found, returns NULL_TREE. Otherwise,
2140 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2141 stepping down the chain to the component, which is in the last
2142 TREE_VALUE of the list. Normally the list is of length one, but if
2143 the component is embedded within (nested) anonymous structures or
2144 unions, the list steps down the chain to the component. */
2f6e4e97 2145
2f2d13da 2146static tree
0fb96aa4 2147lookup_field (tree type, tree component)
2f2d13da
DE
2148{
2149 tree field;
2150
2151 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2152 to the field elements. Use a binary search on this array to quickly
2153 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2154 will always be set for structures which have many elements. */
2155
22a0b85f 2156 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2157 {
2158 int bot, top, half;
d07605f5 2159 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2160
2161 field = TYPE_FIELDS (type);
2162 bot = 0;
d07605f5 2163 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2164 while (top - bot > 1)
2165 {
2f2d13da
DE
2166 half = (top - bot + 1) >> 1;
2167 field = field_array[bot+half];
2168
2169 if (DECL_NAME (field) == NULL_TREE)
2170 {
2171 /* Step through all anon unions in linear fashion. */
2172 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2173 {
2f2d13da 2174 field = field_array[bot++];
a68b98cf
RK
2175 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2176 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2177 {
0fb96aa4 2178 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2179
2180 if (anon)
2181 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2182
2183 /* The Plan 9 compiler permits referring
2184 directly to an anonymous struct/union field
2185 using a typedef name. */
2186 if (flag_plan9_extensions
2187 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2188 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2189 == TYPE_DECL)
2190 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2191 == component))
2192 break;
2f6e4e97 2193 }
2f2d13da
DE
2194 }
2195
2196 /* Entire record is only anon unions. */
2197 if (bot > top)
2198 return NULL_TREE;
2199
2200 /* Restart the binary search, with new lower bound. */
2201 continue;
2202 }
2203
e8b87aac 2204 if (DECL_NAME (field) == component)
2f2d13da 2205 break;
e8b87aac 2206 if (DECL_NAME (field) < component)
2f2d13da
DE
2207 bot += half;
2208 else
2209 top = bot + half;
2210 }
2211
2212 if (DECL_NAME (field_array[bot]) == component)
2213 field = field_array[bot];
2214 else if (DECL_NAME (field) != component)
e9b2c823 2215 return NULL_TREE;
2f2d13da
DE
2216 }
2217 else
2218 {
910ad8de 2219 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2220 {
e9b2c823
NB
2221 if (DECL_NAME (field) == NULL_TREE
2222 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2223 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2224 {
0fb96aa4 2225 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2226
e9b2c823
NB
2227 if (anon)
2228 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2229
2230 /* The Plan 9 compiler permits referring directly to an
2231 anonymous struct/union field using a typedef
2232 name. */
2233 if (flag_plan9_extensions
2234 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2235 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2236 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2237 == component))
2238 break;
2f2d13da
DE
2239 }
2240
2241 if (DECL_NAME (field) == component)
2242 break;
2243 }
e9b2c823
NB
2244
2245 if (field == NULL_TREE)
2246 return NULL_TREE;
2f2d13da
DE
2247 }
2248
e9b2c823 2249 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2250}
2251
c2255bc4
AH
2252/* Make an expression to refer to the COMPONENT field of structure or
2253 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2254 location of the COMPONENT_REF. */
400fbf9f
JW
2255
2256tree
c2255bc4 2257build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2258{
b3694847
SS
2259 tree type = TREE_TYPE (datum);
2260 enum tree_code code = TREE_CODE (type);
2261 tree field = NULL;
2262 tree ref;
1e57bf47 2263 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2264
7a3ea201
RH
2265 if (!objc_is_public (datum, component))
2266 return error_mark_node;
2267
46a88c12 2268 /* Detect Objective-C property syntax object.property. */
668ea4b1 2269 if (c_dialect_objc ()
46a88c12 2270 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2271 return ref;
2272
400fbf9f
JW
2273 /* See if there is a field or component with name COMPONENT. */
2274
2275 if (code == RECORD_TYPE || code == UNION_TYPE)
2276 {
d0f062fb 2277 if (!COMPLETE_TYPE_P (type))
400fbf9f 2278 {
7a228918 2279 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2280 return error_mark_node;
2281 }
2282
0fb96aa4 2283 field = lookup_field (type, component);
400fbf9f
JW
2284
2285 if (!field)
2286 {
c2255bc4 2287 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2288 return error_mark_node;
2289 }
400fbf9f 2290
e9b2c823
NB
2291 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2292 This might be better solved in future the way the C++ front
2293 end does it - by giving the anonymous entities each a
2294 separate name and type, and then have build_component_ref
2295 recursively call itself. We can't do that here. */
46ea50cb 2296 do
19d76e60 2297 {
e9b2c823 2298 tree subdatum = TREE_VALUE (field);
efed193e
JM
2299 int quals;
2300 tree subtype;
1e57bf47 2301 bool use_datum_quals;
e9b2c823
NB
2302
2303 if (TREE_TYPE (subdatum) == error_mark_node)
2304 return error_mark_node;
2305
1e57bf47
JM
2306 /* If this is an rvalue, it does not have qualifiers in C
2307 standard terms and we must avoid propagating such
2308 qualifiers down to a non-lvalue array that is then
2309 converted to a pointer. */
2310 use_datum_quals = (datum_lvalue
2311 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2312
efed193e 2313 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2314 if (use_datum_quals)
2315 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2316 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2317
2318 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2319 NULL_TREE);
c2255bc4 2320 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2321 if (TREE_READONLY (subdatum)
2322 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2323 TREE_READONLY (ref) = 1;
1e57bf47
JM
2324 if (TREE_THIS_VOLATILE (subdatum)
2325 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2326 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2327
2328 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2329 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2330
19d76e60 2331 datum = ref;
46ea50cb
RS
2332
2333 field = TREE_CHAIN (field);
19d76e60 2334 }
46ea50cb 2335 while (field);
19d76e60 2336
400fbf9f
JW
2337 return ref;
2338 }
2339 else if (code != ERROR_MARK)
c2255bc4
AH
2340 error_at (loc,
2341 "request for member %qE in something not a structure or union",
2342 component);
400fbf9f
JW
2343
2344 return error_mark_node;
2345}
2346\f
2347/* Given an expression PTR for a pointer, return an expression
2348 for the value pointed to.
6a3799eb
AH
2349 ERRORSTRING is the name of the operator to appear in error messages.
2350
2351 LOC is the location to use for the generated tree. */
400fbf9f
JW
2352
2353tree
dd865ef6 2354build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2355{
b3694847
SS
2356 tree pointer = default_conversion (ptr);
2357 tree type = TREE_TYPE (pointer);
6a3799eb 2358 tree ref;
400fbf9f
JW
2359
2360 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2361 {
1043771b 2362 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2363 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2364 {
2365 /* If a warning is issued, mark it to avoid duplicates from
2366 the backend. This only needs to be done at
2367 warn_strict_aliasing > 2. */
2368 if (warn_strict_aliasing > 2)
2369 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2370 type, TREE_OPERAND (pointer, 0)))
2371 TREE_NO_WARNING (pointer) = 1;
2372 }
2373
870cc33b 2374 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2375 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2376 == TREE_TYPE (type)))
6a3799eb
AH
2377 {
2378 ref = TREE_OPERAND (pointer, 0);
2379 protected_set_expr_location (ref, loc);
2380 return ref;
2381 }
870cc33b
RS
2382 else
2383 {
2384 tree t = TREE_TYPE (type);
46df2823 2385
984dfd8c 2386 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2387
baae9b65 2388 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2389 {
d9b7be2e
MP
2390 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2391 {
2392 error_at (loc, "dereferencing pointer to incomplete type "
2393 "%qT", t);
2394 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2395 }
870cc33b
RS
2396 return error_mark_node;
2397 }
7d882b83 2398 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2399 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2400
2401 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2402 so that we get the proper error message if the result is used
2403 to assign to. Also, &* is supposed to be a no-op.
2404 And ANSI C seems to specify that the type of the result
2405 should be the const type. */
2406 /* A de-reference of a pointer to const is not a const. It is valid
2407 to change it via some other pointer. */
2408 TREE_READONLY (ref) = TYPE_READONLY (t);
2409 TREE_SIDE_EFFECTS (ref)
271bd540 2410 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2411 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2412 protected_set_expr_location (ref, loc);
870cc33b
RS
2413 return ref;
2414 }
2415 }
400fbf9f 2416 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2417 invalid_indirection_error (loc, type, errstring);
2418
400fbf9f
JW
2419 return error_mark_node;
2420}
2421
2422/* This handles expressions of the form "a[i]", which denotes
2423 an array reference.
2424
2425 This is logically equivalent in C to *(a+i), but we may do it differently.
2426 If A is a variable or a member, we generate a primitive ARRAY_REF.
2427 This avoids forcing the array out of registers, and can work on
2428 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2429 by functions).
2430
30cd1c5d
AS
2431 For vector types, allow vector[i] but not i[vector], and create
2432 *(((type*)&vectortype) + i) for the expression.
2433
6a3799eb 2434 LOC is the location to use for the returned expression. */
400fbf9f
JW
2435
2436tree
c2255bc4 2437build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2438{
6a3799eb 2439 tree ret;
a4ab7973 2440 bool swapped = false;
400fbf9f
JW
2441 if (TREE_TYPE (array) == error_mark_node
2442 || TREE_TYPE (index) == error_mark_node)
2443 return error_mark_node;
2444
b72271b9 2445 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2446 {
2447 size_t rank = 0;
2448 if (!find_rank (loc, index, index, true, &rank))
2449 return error_mark_node;
2450 if (rank > 1)
2451 {
2452 error_at (loc, "rank of the array's index is greater than 1");
2453 return error_mark_node;
2454 }
2455 }
a4ab7973 2456 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2457 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2458 /* Allow vector[index] but not index[vector]. */
31521951 2459 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2460 {
a4ab7973
JM
2461 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2462 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2463 {
f90e8e2e 2464 error_at (loc,
30cd1c5d
AS
2465 "subscripted value is neither array nor pointer nor vector");
2466
fdeefd49
RS
2467 return error_mark_node;
2468 }
fab27f52 2469 std::swap (array, index);
a4ab7973
JM
2470 swapped = true;
2471 }
2472
2473 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2474 {
a63068b6 2475 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2476 return error_mark_node;
2477 }
2478
2479 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2480 {
a63068b6 2481 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2482 return error_mark_node;
2483 }
2484
ff6b6641
GDR
2485 /* ??? Existing practice has been to warn only when the char
2486 index is syntactically the index, not for char[array]. */
2487 if (!swapped)
5bd012f8 2488 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2489
2490 /* Apply default promotions *after* noticing character types. */
2491 index = default_conversion (index);
f406ae1f
MP
2492 if (index == error_mark_node)
2493 return error_mark_node;
a4ab7973
JM
2494
2495 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2496
aa7da51a
JJ
2497 bool non_lvalue
2498 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2499
2500 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2501 {
e4d35515 2502 tree rval, type;
fdeefd49 2503
400fbf9f
JW
2504 /* An array that is indexed by a non-constant
2505 cannot be stored in a register; we must be able to do
2506 address arithmetic on its address.
2507 Likewise an array of elements of variable size. */
2508 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2509 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2510 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2511 {
dffd7eb6 2512 if (!c_mark_addressable (array))
400fbf9f
JW
2513 return error_mark_node;
2514 }
e6d52559
JW
2515 /* An array that is indexed by a constant value which is not within
2516 the array bounds cannot be stored in a register either; because we
2517 would get a crash in store_bit_field/extract_bit_field when trying
2518 to access a non-existent part of the register. */
2519 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2520 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2521 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2522 {
dffd7eb6 2523 if (!c_mark_addressable (array))
e6d52559
JW
2524 return error_mark_node;
2525 }
400fbf9f 2526
f3bede71 2527 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2528 {
2529 tree foo = array;
2530 while (TREE_CODE (foo) == COMPONENT_REF)
2531 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2532 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2533 pedwarn (loc, OPT_Wpedantic,
fcf73884 2534 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2535 else if (!lvalue_p (foo))
2536 pedwarn_c90 (loc, OPT_Wpedantic,
2537 "ISO C90 forbids subscripting non-lvalue "
2538 "array");
400fbf9f
JW
2539 }
2540
46df2823 2541 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2542 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2543 /* Array ref is const/volatile if the array elements are
c22cacf3 2544 or if the array is. */
400fbf9f
JW
2545 TREE_READONLY (rval)
2546 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2547 | TREE_READONLY (array));
2548 TREE_SIDE_EFFECTS (rval)
2549 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2550 | TREE_SIDE_EFFECTS (array));
2551 TREE_THIS_VOLATILE (rval)
2552 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2553 /* This was added by rms on 16 Nov 91.
2f6e4e97 2554 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2555 in an inline function.
2556 Hope it doesn't break something else. */
2557 | TREE_THIS_VOLATILE (array));
928c19bb 2558 ret = require_complete_type (rval);
6a3799eb 2559 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2560 if (non_lvalue)
2561 ret = non_lvalue_loc (loc, ret);
6a3799eb 2562 return ret;
400fbf9f 2563 }
a4ab7973
JM
2564 else
2565 {
2566 tree ar = default_conversion (array);
400fbf9f 2567
a4ab7973
JM
2568 if (ar == error_mark_node)
2569 return ar;
400fbf9f 2570
a4ab7973
JM
2571 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2572 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2573
aa7da51a
JJ
2574 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2575 index, 0),
2576 RO_ARRAY_INDEXING);
2577 if (non_lvalue)
2578 ret = non_lvalue_loc (loc, ret);
2579 return ret;
a4ab7973 2580 }
400fbf9f
JW
2581}
2582\f
7e585d16 2583/* Build an external reference to identifier ID. FUN indicates
766beb40 2584 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2585 location of the identifier. This sets *TYPE to the type of the
2586 identifier, which is not the same as the type of the returned value
2587 for CONST_DECLs defined as enum constants. If the type of the
2588 identifier is not available, *TYPE is set to NULL. */
7e585d16 2589tree
c2255bc4 2590build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2591{
2592 tree ref;
2593 tree decl = lookup_name (id);
16b34ad6
ZL
2594
2595 /* In Objective-C, an instance variable (ivar) may be preferred to
2596 whatever lookup_name() found. */
2597 decl = objc_lookup_ivar (decl, id);
7e585d16 2598
6866c6e8 2599 *type = NULL;
339a28b9 2600 if (decl && decl != error_mark_node)
6866c6e8
ILT
2601 {
2602 ref = decl;
2603 *type = TREE_TYPE (ref);
2604 }
339a28b9
ZW
2605 else if (fun)
2606 /* Implicit function declaration. */
c2255bc4 2607 ref = implicitly_declare (loc, id);
339a28b9
ZW
2608 else if (decl == error_mark_node)
2609 /* Don't complain about something that's already been
2610 complained about. */
2611 return error_mark_node;
2612 else
2613 {
c2255bc4 2614 undeclared_variable (loc, id);
339a28b9
ZW
2615 return error_mark_node;
2616 }
7e585d16
ZW
2617
2618 if (TREE_TYPE (ref) == error_mark_node)
2619 return error_mark_node;
2620
339a28b9 2621 if (TREE_DEPRECATED (ref))
9b86d6bb 2622 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2623
ad960f56 2624 /* Recursive call does not count as usage. */
b8698a0f 2625 if (ref != current_function_decl)
ad960f56 2626 {
ad960f56
MLI
2627 TREE_USED (ref) = 1;
2628 }
7e585d16 2629
bc4b653b
JM
2630 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2631 {
2632 if (!in_sizeof && !in_typeof)
2633 C_DECL_USED (ref) = 1;
2634 else if (DECL_INITIAL (ref) == 0
2635 && DECL_EXTERNAL (ref)
2636 && !TREE_PUBLIC (ref))
2637 record_maybe_used_decl (ref);
2638 }
2639
7e585d16
ZW
2640 if (TREE_CODE (ref) == CONST_DECL)
2641 {
6193b8b7 2642 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2643
2644 if (warn_cxx_compat
2645 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2646 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2647 {
2648 warning_at (loc, OPT_Wc___compat,
2649 ("enum constant defined in struct or union "
2650 "is not visible in C++"));
2651 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2652 }
2653
7e585d16
ZW
2654 ref = DECL_INITIAL (ref);
2655 TREE_CONSTANT (ref) = 1;
2656 }
6a29edea 2657 else if (current_function_decl != 0
4b1e44be 2658 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2659 && (VAR_OR_FUNCTION_DECL_P (ref)
2660 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2661 {
2662 tree context = decl_function_context (ref);
2f6e4e97 2663
6a29edea
EB
2664 if (context != 0 && context != current_function_decl)
2665 DECL_NONLOCAL (ref) = 1;
2666 }
71113fcd
GK
2667 /* C99 6.7.4p3: An inline definition of a function with external
2668 linkage ... shall not contain a reference to an identifier with
2669 internal linkage. */
2670 else if (current_function_decl != 0
2671 && DECL_DECLARED_INLINE_P (current_function_decl)
2672 && DECL_EXTERNAL (current_function_decl)
2673 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2674 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2675 && ! TREE_PUBLIC (ref)
2676 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2677 record_inline_static (loc, current_function_decl, ref,
2678 csi_internal);
7e585d16
ZW
2679
2680 return ref;
2681}
2682
bc4b653b
JM
2683/* Record details of decls possibly used inside sizeof or typeof. */
2684struct maybe_used_decl
2685{
2686 /* The decl. */
2687 tree decl;
2688 /* The level seen at (in_sizeof + in_typeof). */
2689 int level;
2690 /* The next one at this level or above, or NULL. */
2691 struct maybe_used_decl *next;
2692};
2693
2694static struct maybe_used_decl *maybe_used_decls;
2695
2696/* Record that DECL, an undefined static function reference seen
2697 inside sizeof or typeof, might be used if the operand of sizeof is
2698 a VLA type or the operand of typeof is a variably modified
2699 type. */
2700
4e2fb7de 2701static void
bc4b653b
JM
2702record_maybe_used_decl (tree decl)
2703{
2704 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2705 t->decl = decl;
2706 t->level = in_sizeof + in_typeof;
2707 t->next = maybe_used_decls;
2708 maybe_used_decls = t;
2709}
2710
2711/* Pop the stack of decls possibly used inside sizeof or typeof. If
2712 USED is false, just discard them. If it is true, mark them used
2713 (if no longer inside sizeof or typeof) or move them to the next
2714 level up (if still inside sizeof or typeof). */
2715
2716void
2717pop_maybe_used (bool used)
2718{
2719 struct maybe_used_decl *p = maybe_used_decls;
2720 int cur_level = in_sizeof + in_typeof;
2721 while (p && p->level > cur_level)
2722 {
2723 if (used)
2724 {
2725 if (cur_level == 0)
2726 C_DECL_USED (p->decl) = 1;
2727 else
2728 p->level = cur_level;
2729 }
2730 p = p->next;
2731 }
2732 if (!used || cur_level == 0)
2733 maybe_used_decls = p;
2734}
2735
2736/* Return the result of sizeof applied to EXPR. */
2737
2738struct c_expr
c2255bc4 2739c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2740{
2741 struct c_expr ret;
ad97f4be
JM
2742 if (expr.value == error_mark_node)
2743 {
2744 ret.value = error_mark_node;
2745 ret.original_code = ERROR_MARK;
6866c6e8 2746 ret.original_type = NULL;
ad97f4be
JM
2747 pop_maybe_used (false);
2748 }
2749 else
2750 {
928c19bb 2751 bool expr_const_operands = true;
773ec47f
MP
2752
2753 if (TREE_CODE (expr.value) == PARM_DECL
2754 && C_ARRAY_PARAMETER (expr.value))
2755 {
2756 if (warning_at (loc, OPT_Wsizeof_array_argument,
2757 "%<sizeof%> on array function parameter %qE will "
2758 "return size of %qT", expr.value,
2759 expr.original_type))
2760 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2761 }
928c19bb
JM
2762 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2763 &expr_const_operands);
c2255bc4 2764 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2765 c_last_sizeof_arg = expr.value;
2766 ret.original_code = SIZEOF_EXPR;
6866c6e8 2767 ret.original_type = NULL;
928c19bb 2768 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2769 {
2770 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2771 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2772 folded_expr, ret.value);
2773 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2774 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2775 }
928c19bb 2776 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2777 }
bc4b653b
JM
2778 return ret;
2779}
2780
2781/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2782 name passed to sizeof (rather than the type itself). LOC is the
2783 location of the original expression. */
bc4b653b
JM
2784
2785struct c_expr
c2255bc4 2786c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2787{
2788 tree type;
2789 struct c_expr ret;
928c19bb
JM
2790 tree type_expr = NULL_TREE;
2791 bool type_expr_const = true;
2792 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2793 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2794 c_last_sizeof_arg = type;
2795 ret.original_code = SIZEOF_EXPR;
6866c6e8 2796 ret.original_type = NULL;
24070fcb
JM
2797 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2798 && c_vla_type_p (type))
928c19bb 2799 {
24070fcb
JM
2800 /* If the type is a [*] array, it is a VLA but is represented as
2801 having a size of zero. In such a case we must ensure that
2802 the result of sizeof does not get folded to a constant by
2803 c_fully_fold, because if the size is evaluated the result is
2804 not constant and so constraints on zero or negative size
2805 arrays must not be applied when this sizeof call is inside
2806 another array declarator. */
2807 if (!type_expr)
2808 type_expr = integer_zero_node;
928c19bb
JM
2809 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2810 type_expr, ret.value);
2811 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2812 }
16464cc1
VR
2813 pop_maybe_used (type != error_mark_node
2814 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2815 return ret;
2816}
2817
400fbf9f 2818/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2819 The function call is at LOC.
400fbf9f
JW
2820 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2821 TREE_VALUE of each node is a parameter-expression.
2822 FUNCTION's data type may be a function type or a pointer-to-function. */
2823
2824tree
c2255bc4 2825build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2826{
9771b263 2827 vec<tree, va_gc> *v;
bbbbb16a
ILT
2828 tree ret;
2829
9771b263 2830 vec_alloc (v, list_length (params));
bbbbb16a 2831 for (; params; params = TREE_CHAIN (params))
9771b263 2832 v->quick_push (TREE_VALUE (params));
8edbfaa6 2833 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2834 vec_free (v);
bbbbb16a
ILT
2835 return ret;
2836}
2837
ae52741c
MLI
2838/* Give a note about the location of the declaration of DECL. */
2839
c7b70a3c
MP
2840static void
2841inform_declaration (tree decl)
ae52741c 2842{
c7b70a3c 2843 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2844 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2845}
2846
bbbbb16a
ILT
2847/* Build a function call to function FUNCTION with parameters PARAMS.
2848 ORIGTYPES, if not NULL, is a vector of types; each element is
2849 either NULL or the original type of the corresponding element in
2850 PARAMS. The original type may differ from TREE_TYPE of the
2851 parameter for enums. FUNCTION's data type may be a function type
2852 or pointer-to-function. This function changes the elements of
2853 PARAMS. */
2854
2855tree
81e5eca8
MP
2856build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2857 tree function, vec<tree, va_gc> *params,
9771b263 2858 vec<tree, va_gc> *origtypes)
400fbf9f 2859{
b3694847 2860 tree fntype, fundecl = 0;
4977bab6 2861 tree name = NULL_TREE, result;
c96f4f73 2862 tree tem;
94a0dd7b
SL
2863 int nargs;
2864 tree *argarray;
b8698a0f 2865
400fbf9f 2866
fc76e425 2867 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2868 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2869
2870 /* Convert anything with function type to a pointer-to-function. */
2871 if (TREE_CODE (function) == FUNCTION_DECL)
2872 {
2873 name = DECL_NAME (function);
0a35513e
AH
2874
2875 if (flag_tm)
2876 tm_malloc_replacement (function);
a5eadacc 2877 fundecl = function;
86951993
AM
2878 /* Atomic functions have type checking/casting already done. They are
2879 often rewritten and don't match the original parameter list. */
2880 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2881 origtypes = NULL;
36536d79 2882
b72271b9 2883 if (flag_cilkplus
36536d79
BI
2884 && is_cilkplus_reduce_builtin (function))
2885 origtypes = NULL;
400fbf9f 2886 }
f2a71bbc 2887 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2888 function = function_to_pointer_conversion (loc, function);
400fbf9f 2889
6e955430
ZL
2890 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2891 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2892 if (params && !params->is_empty ())
2893 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2894
928c19bb
JM
2895 function = c_fully_fold (function, false, NULL);
2896
400fbf9f
JW
2897 fntype = TREE_TYPE (function);
2898
2899 if (TREE_CODE (fntype) == ERROR_MARK)
2900 return error_mark_node;
2901
2902 if (!(TREE_CODE (fntype) == POINTER_TYPE
2903 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2904 {
ae52741c
MLI
2905 if (!flag_diagnostics_show_caret)
2906 error_at (loc,
2907 "called object %qE is not a function or function pointer",
2908 function);
2909 else if (DECL_P (function))
2910 {
2911 error_at (loc,
2912 "called object %qD is not a function or function pointer",
2913 function);
2914 inform_declaration (function);
2915 }
2916 else
2917 error_at (loc,
2918 "called object is not a function or function pointer");
400fbf9f
JW
2919 return error_mark_node;
2920 }
2921
5ce89b2e
JM
2922 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2923 current_function_returns_abnormally = 1;
2924
400fbf9f
JW
2925 /* fntype now gets the type of function pointed to. */
2926 fntype = TREE_TYPE (fntype);
2927
ab4194da
JM
2928 /* Convert the parameters to the types declared in the
2929 function prototype, or apply default promotions. */
2930
81e5eca8
MP
2931 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2932 origtypes, function, fundecl);
ab4194da
JM
2933 if (nargs < 0)
2934 return error_mark_node;
2935
c96f4f73 2936 /* Check that the function is called through a compatible prototype.
fa337f3a 2937 If it is not, warn. */
1043771b 2938 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2939 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2940 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2941 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2942 {
2943 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2944
2945 /* This situation leads to run-time undefined behavior. We can't,
2946 therefore, simply error unless we can prove that all possible
2947 executions of the program must execute the code. */
fa337f3a 2948 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2949
fa337f3a
RB
2950 if (VOID_TYPE_P (return_type)
2951 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2952 pedwarn (loc, 0,
2953 "function with qualified void return type called");
2954 }
c96f4f73 2955
9771b263 2956 argarray = vec_safe_address (params);
bbbbb16a 2957
83322951
RG
2958 /* Check that arguments to builtin functions match the expectations. */
2959 if (fundecl
2960 && DECL_BUILT_IN (fundecl)
2961 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2962 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2963 return error_mark_node;
400fbf9f 2964
83322951 2965 /* Check that the arguments to the function are valid. */
dde05067 2966 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2967
928c19bb
JM
2968 if (name != NULL_TREE
2969 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2970 {
928c19bb 2971 if (require_constant_value)
b8698a0f 2972 result =
db3927fb
AH
2973 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2974 function, nargs, argarray);
928c19bb 2975 else
db3927fb
AH
2976 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2977 function, nargs, argarray);
928c19bb
JM
2978 if (TREE_CODE (result) == NOP_EXPR
2979 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2980 STRIP_TYPE_NOPS (result);
bf730f15
RS
2981 }
2982 else
db3927fb
AH
2983 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2984 function, nargs, argarray);
b0b3afb2 2985
71653180 2986 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2987 {
2988 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2989 pedwarn (loc, 0,
3ce62965
JM
2990 "function with qualified void return type called");
2991 return result;
2992 }
1eb8759b 2993 return require_complete_type (result);
400fbf9f 2994}
8edbfaa6
JJ
2995
2996/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2997
2998tree
2999c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3000 tree function, vec<tree, va_gc> *params,
3001 vec<tree, va_gc> *origtypes)
3002{
3003 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3004 STRIP_TYPE_NOPS (function);
3005
3006 /* Convert anything with function type to a pointer-to-function. */
3007 if (TREE_CODE (function) == FUNCTION_DECL)
3008 {
3009 /* Implement type-directed function overloading for builtins.
3010 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3011 handle all the type checking. The result is a complete expression
3012 that implements this function call. */
3013 tree tem = resolve_overloaded_builtin (loc, function, params);
3014 if (tem)
3015 return tem;
3016 }
3017 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3018}
400fbf9f 3019\f
bbbbb16a
ILT
3020/* Convert the argument expressions in the vector VALUES
3021 to the types in the list TYPELIST.
400fbf9f
JW
3022
3023 If TYPELIST is exhausted, or when an element has NULL as its type,
3024 perform the default conversions.
3025
bbbbb16a
ILT
3026 ORIGTYPES is the original types of the expressions in VALUES. This
3027 holds the type of enum values which have been converted to integral
3028 types. It may be NULL.
400fbf9f 3029
03dafa61
JM
3030 FUNCTION is a tree for the called function. It is used only for
3031 error messages, where it is formatted with %qE.
400fbf9f
JW
3032
3033 This is also where warnings about wrong number of args are generated.
3034
81e5eca8
MP
3035 ARG_LOC are locations of function arguments (if any).
3036
94a0dd7b 3037 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3038 than the length of VALUES in some error situations), or -1 on
3039 failure. */
94a0dd7b
SL
3040
3041static int
81e5eca8
MP
3042convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3043 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3044 tree function, tree fundecl)
400fbf9f 3045{
bbbbb16a
ILT
3046 tree typetail, val;
3047 unsigned int parmnum;
06302a02 3048 bool error_args = false;
b5d32c25 3049 const bool type_generic = fundecl
81e5eca8 3050 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3051 bool type_generic_remove_excess_precision = false;
03dafa61 3052 tree selector;
03dafa61 3053
2ac2f164
JM
3054 /* Change pointer to function to the function itself for
3055 diagnostics. */
03dafa61
JM
3056 if (TREE_CODE (function) == ADDR_EXPR
3057 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3058 function = TREE_OPERAND (function, 0);
03dafa61
JM
3059
3060 /* Handle an ObjC selector specially for diagnostics. */
3061 selector = objc_message_selector ();
400fbf9f 3062
8ce94e44
JM
3063 /* For type-generic built-in functions, determine whether excess
3064 precision should be removed (classification) or not
3065 (comparison). */
3066 if (type_generic
3067 && DECL_BUILT_IN (fundecl)
3068 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3069 {
3070 switch (DECL_FUNCTION_CODE (fundecl))
3071 {
3072 case BUILT_IN_ISFINITE:
3073 case BUILT_IN_ISINF:
3074 case BUILT_IN_ISINF_SIGN:
3075 case BUILT_IN_ISNAN:
3076 case BUILT_IN_ISNORMAL:
3077 case BUILT_IN_FPCLASSIFY:
3078 type_generic_remove_excess_precision = true;
3079 break;
3080
3081 default:
3082 type_generic_remove_excess_precision = false;
3083 break;
3084 }
3085 }
b72271b9 3086 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3087 return vec_safe_length (values);
8ce94e44 3088
400fbf9f 3089 /* Scan the given expressions and types, producing individual
bbbbb16a 3090 converted arguments. */
400fbf9f 3091
bbbbb16a 3092 for (typetail = typelist, parmnum = 0;
9771b263 3093 values && values->iterate (parmnum, &val);
bbbbb16a 3094 ++parmnum)
400fbf9f 3095 {
b3694847 3096 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3097 tree valtype = TREE_TYPE (val);
03dafa61
JM
3098 tree rname = function;
3099 int argnum = parmnum + 1;
4d3e6fae 3100 const char *invalid_func_diag;
8ce94e44 3101 bool excess_precision = false;
928c19bb 3102 bool npc;
bbbbb16a 3103 tree parmval;
5c1bc275
MP
3104 /* Some __atomic_* builtins have additional hidden argument at
3105 position 0. */
3106 location_t ploc
3107 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3108 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3109 : input_location;
400fbf9f
JW
3110
3111 if (type == void_type_node)
3112 {
19dc6d01 3113 if (selector)
68fca595 3114 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3115 else
68fca595 3116 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3117 inform_declaration (fundecl);
d38f7dce 3118 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3119 }
3120
03dafa61
JM
3121 if (selector && argnum > 2)
3122 {
3123 rname = selector;
3124 argnum -= 2;
3125 }
3126
928c19bb 3127 npc = null_pointer_constant_p (val);
8ce94e44
JM
3128
3129 /* If there is excess precision and a prototype, convert once to
3130 the required type rather than converting via the semantic
3131 type. Likewise without a prototype a float value represented
3132 as long double should be converted once to double. But for
3133 type-generic classification functions excess precision must
3134 be removed here. */
3135 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3136 && (type || !type_generic || !type_generic_remove_excess_precision))
3137 {
3138 val = TREE_OPERAND (val, 0);
3139 excess_precision = true;
3140 }
928c19bb 3141 val = c_fully_fold (val, false, NULL);
ed248cf7 3142 STRIP_TYPE_NOPS (val);
400fbf9f 3143
400fbf9f
JW
3144 val = require_complete_type (val);
3145
3146 if (type != 0)
3147 {
3148 /* Formal parm type is specified by a function prototype. */
400fbf9f 3149
20913689 3150 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3151 {
5c1bc275
MP
3152 error_at (ploc, "type of formal parameter %d is incomplete",
3153 parmnum + 1);
400fbf9f
JW
3154 parmval = val;
3155 }
3156 else
3157 {
bbbbb16a
ILT
3158 tree origtype;
3159
d45cf215
RS
3160 /* Optionally warn about conversions that
3161 differ from the default conversions. */
05170031 3162 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3163 {
e3a64162 3164 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3165
aae43c5f 3166 if (INTEGRAL_TYPE_P (type)
8ce94e44 3167 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3168 warning_at (ploc, OPT_Wtraditional_conversion,
3169 "passing argument %d of %qE as integer rather "
3170 "than floating due to prototype",
3171 argnum, rname);
03829ad2 3172 if (INTEGRAL_TYPE_P (type)
8ce94e44 3173 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3174 warning_at (ploc, OPT_Wtraditional_conversion,
3175 "passing argument %d of %qE as integer rather "
3176 "than complex due to prototype",
3177 argnum, rname);
aae43c5f 3178 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3179 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3180 warning_at (ploc, OPT_Wtraditional_conversion,
3181 "passing argument %d of %qE as complex rather "
3182 "than floating due to prototype",
3183 argnum, rname);
400fbf9f 3184 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3185 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3186 warning_at (ploc, OPT_Wtraditional_conversion,
3187 "passing argument %d of %qE as floating rather "
3188 "than integer due to prototype",
3189 argnum, rname);
03829ad2 3190 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3191 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3192 warning_at (ploc, OPT_Wtraditional_conversion,
3193 "passing argument %d of %qE as complex rather "
3194 "than integer due to prototype",
3195 argnum, rname);
aae43c5f 3196 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3197 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3198 warning_at (ploc, OPT_Wtraditional_conversion,
3199 "passing argument %d of %qE as floating rather "
3200 "than complex due to prototype",
3201 argnum, rname);
aae43c5f
RK
3202 /* ??? At some point, messages should be written about
3203 conversions between complex types, but that's too messy
3204 to do now. */
d45cf215 3205 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3206 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3207 {
3208 /* Warn if any argument is passed as `float',
047de90b 3209 since without a prototype it would be `double'. */
9a8ce21f
JG
3210 if (formal_prec == TYPE_PRECISION (float_type_node)
3211 && type != dfloat32_type_node)
5c1bc275
MP
3212 warning_at (ploc, 0,
3213 "passing argument %d of %qE as %<float%> "
3214 "rather than %<double%> due to prototype",
3215 argnum, rname);
9a8ce21f
JG
3216
3217 /* Warn if mismatch between argument and prototype
3218 for decimal float types. Warn of conversions with
3219 binary float types and of precision narrowing due to
3220 prototype. */
8ce94e44 3221 else if (type != valtype
9a8ce21f
JG
3222 && (type == dfloat32_type_node
3223 || type == dfloat64_type_node
c22cacf3 3224 || type == dfloat128_type_node
8ce94e44
JM
3225 || valtype == dfloat32_type_node
3226 || valtype == dfloat64_type_node
3227 || valtype == dfloat128_type_node)
c22cacf3 3228 && (formal_prec
8ce94e44 3229 <= TYPE_PRECISION (valtype)
9a8ce21f 3230 || (type == dfloat128_type_node
8ce94e44 3231 && (valtype
c22cacf3 3232 != dfloat64_type_node
8ce94e44 3233 && (valtype
9a8ce21f
JG
3234 != dfloat32_type_node)))
3235 || (type == dfloat64_type_node
8ce94e44 3236 && (valtype
9a8ce21f 3237 != dfloat32_type_node))))
5c1bc275
MP
3238 warning_at (ploc, 0,
3239 "passing argument %d of %qE as %qT "
3240 "rather than %qT due to prototype",
3241 argnum, rname, type, valtype);
9a8ce21f 3242
d45cf215 3243 }
3ed56f8a
KG
3244 /* Detect integer changing in width or signedness.
3245 These warnings are only activated with
05170031
MLI
3246 -Wtraditional-conversion, not with -Wtraditional. */
3247 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3248 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3249 {
d45cf215
RS
3250 tree would_have_been = default_conversion (val);
3251 tree type1 = TREE_TYPE (would_have_been);
3252
754a4d82 3253 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3254 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3255 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3256 /* No warning if function asks for enum
3257 and the actual arg is that enum type. */
3258 ;
3259 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3260 warning_at (ploc, OPT_Wtraditional_conversion,
3261 "passing argument %d of %qE "
3262 "with different width due to prototype",
3263 argnum, rname);
8df83eae 3264 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3265 ;
800cd3b9
RS
3266 /* Don't complain if the formal parameter type
3267 is an enum, because we can't tell now whether
3268 the value was an enum--even the same enum. */
3269 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3270 ;
400fbf9f
JW
3271 else if (TREE_CODE (val) == INTEGER_CST
3272 && int_fits_type_p (val, type))
3273 /* Change in signedness doesn't matter
3274 if a constant value is unaffected. */
3275 ;
ce9895ae
RS
3276 /* If the value is extended from a narrower
3277 unsigned type, it doesn't matter whether we
3278 pass it as signed or unsigned; the value
3279 certainly is the same either way. */
8ce94e44
JM
3280 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3281 && TYPE_UNSIGNED (valtype))
ce9895ae 3282 ;
8df83eae 3283 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3284 warning_at (ploc, OPT_Wtraditional_conversion,
3285 "passing argument %d of %qE "
3286 "as unsigned due to prototype",
3287 argnum, rname);
3ed56f8a 3288 else
5c1bc275
MP
3289 warning_at (ploc, OPT_Wtraditional_conversion,
3290 "passing argument %d of %qE "
3291 "as signed due to prototype",
3292 argnum, rname);
400fbf9f
JW
3293 }
3294 }
3295
8ce94e44
JM
3296 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3297 sake of better warnings from convert_and_check. */
3298 if (excess_precision)
3299 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3300 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3301 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3302 val, origtype, ic_argpass,
3303 npc, fundecl, function,
2ac2f164 3304 parmnum + 1);
2f6e4e97 3305
61f71b34 3306 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3307 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3308 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3309 parmval = default_conversion (parmval);
400fbf9f 3310 }
400fbf9f 3311 }
8ce94e44
JM
3312 else if (TREE_CODE (valtype) == REAL_TYPE
3313 && (TYPE_PRECISION (valtype)
2531a1d9 3314 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3315 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3316 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3317 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3318 {
3319 if (type_generic)
bbbbb16a 3320 parmval = val;
b5d32c25 3321 else
0a0b3574
MM
3322 {
3323 /* Convert `float' to `double'. */
3324 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3325 warning_at (ploc, OPT_Wdouble_promotion,
3326 "implicit conversion from %qT to %qT when passing "
3327 "argument to function",
3328 valtype, double_type_node);
0a0b3574
MM
3329 parmval = convert (double_type_node, val);
3330 }
b5d32c25 3331 }
8ce94e44
JM
3332 else if (excess_precision && !type_generic)
3333 /* A "double" argument with excess precision being passed
3334 without a prototype or in variable arguments. */
bbbbb16a 3335 parmval = convert (valtype, val);
c22cacf3
MS
3336 else if ((invalid_func_diag =
3337 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3338 {
3339 error (invalid_func_diag);
94a0dd7b 3340 return -1;
4d3e6fae 3341 }
400fbf9f
JW
3342 else
3343 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3344 parmval = default_conversion (val);
3345
9771b263 3346 (*values)[parmnum] = parmval;
06302a02
JJ
3347 if (parmval == error_mark_node)
3348 error_args = true;
400fbf9f
JW
3349
3350 if (typetail)
3351 typetail = TREE_CHAIN (typetail);
3352 }
3353
9771b263 3354 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3355
400fbf9f 3356 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3357 {
68fca595 3358 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3359 inform_declaration (fundecl);
3360 return -1;
3789b316 3361 }
400fbf9f 3362
06302a02 3363 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3364}
3365\f
43f6dfd3
RS
3366/* This is the entry point used by the parser to build unary operators
3367 in the input. CODE, a tree_code, specifies the unary operator, and
3368 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3369 CONVERT_EXPR for code.
3370
3371 LOC is the location to use for the tree generated.
3372*/
43f6dfd3
RS
3373
3374struct c_expr
c2255bc4 3375parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3376{
3377 struct c_expr result;
3378
c9f9eb5d 3379 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3380 result.original_code = code;
6866c6e8
ILT
3381 result.original_type = NULL;
3382
59c0753d 3383 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3384 overflow_warning (loc, result.value);
59c0753d 3385
43f6dfd3
RS
3386 return result;
3387}
3388
3389/* This is the entry point used by the parser to build binary operators
3390 in the input. CODE, a tree_code, specifies the binary operator, and
3391 ARG1 and ARG2 are the operands. In addition to constructing the
3392 expression, we check for operands that were written with other binary
ba47d38d
AH
3393 operators in a way that is likely to confuse the user.
3394
3395 LOCATION is the location of the binary operator. */
edc7c4ec 3396
487a92fe 3397struct c_expr
ba47d38d
AH
3398parser_build_binary_op (location_t location, enum tree_code code,
3399 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3400{
487a92fe 3401 struct c_expr result;
400fbf9f 3402
487a92fe
JM
3403 enum tree_code code1 = arg1.original_code;
3404 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3405 tree type1 = (arg1.original_type
3406 ? arg1.original_type
3407 : TREE_TYPE (arg1.value));
3408 tree type2 = (arg2.original_type
3409 ? arg2.original_type
3410 : TREE_TYPE (arg2.value));
400fbf9f 3411
ba47d38d
AH
3412 result.value = build_binary_op (location, code,
3413 arg1.value, arg2.value, 1);
487a92fe 3414 result.original_code = code;
6866c6e8 3415 result.original_type = NULL;
58bf601b 3416
487a92fe
JM
3417 if (TREE_CODE (result.value) == ERROR_MARK)
3418 return result;
400fbf9f 3419
ba47d38d
AH
3420 if (location != UNKNOWN_LOCATION)
3421 protected_set_expr_location (result.value, location);
3422
400fbf9f 3423 /* Check for cases such as x+y<<z which users are likely
487a92fe 3424 to misinterpret. */
400fbf9f 3425 if (warn_parentheses)
393e8e8b
MP
3426 warn_about_parentheses (location, code, code1, arg1.value, code2,
3427 arg2.value);
001af587 3428
ca409efd 3429 if (warn_logical_op)
393e8e8b 3430 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3431 code1, arg1.value, code2, arg2.value);
63a08740 3432
742938c9 3433 if (warn_logical_not_paren
7ccb1a11 3434 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3435 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3436 && code2 != TRUTH_NOT_EXPR
3437 /* Avoid warning for !!x == y. */
3438 && (TREE_CODE (arg1.value) != NE_EXPR
3439 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3440 {
3441 /* Avoid warning for !b == y where b has _Bool type. */
3442 tree t = integer_zero_node;
3443 if (TREE_CODE (arg1.value) == EQ_EXPR
3444 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3445 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3446 {
3447 t = TREE_OPERAND (arg1.value, 0);
3448 do
3449 {
3450 if (TREE_TYPE (t) != integer_type_node)
3451 break;
3452 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3453 t = C_MAYBE_CONST_EXPR_EXPR (t);
3454 else if (CONVERT_EXPR_P (t))
3455 t = TREE_OPERAND (t, 0);
3456 else
3457 break;
3458 }
3459 while (1);
3460 }
3461 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3462 warn_logical_not_parentheses (location, code, arg2.value);
3463 }
742938c9 3464
e994a705
RS
3465 /* Warn about comparisons against string literals, with the exception
3466 of testing for equality or inequality of a string literal with NULL. */
3467 if (code == EQ_EXPR || code == NE_EXPR)
3468 {
3469 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3470 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3471 warning_at (location, OPT_Waddress,
3472 "comparison with string literal results in unspecified behavior");
e994a705
RS
3473 }
3474 else if (TREE_CODE_CLASS (code) == tcc_comparison
3475 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3476 warning_at (location, OPT_Waddress,
3477 "comparison with string literal results in unspecified behavior");
e994a705 3478
b8698a0f
L
3479 if (TREE_OVERFLOW_P (result.value)
3480 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3481 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3482 overflow_warning (location, result.value);
400fbf9f 3483
6866c6e8
ILT
3484 /* Warn about comparisons of different enum types. */
3485 if (warn_enum_compare
3486 && TREE_CODE_CLASS (code) == tcc_comparison
3487 && TREE_CODE (type1) == ENUMERAL_TYPE
3488 && TREE_CODE (type2) == ENUMERAL_TYPE
3489 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3490 warning_at (location, OPT_Wenum_compare,
3491 "comparison between %qT and %qT",
3492 type1, type2);
3493
400fbf9f
JW
3494 return result;
3495}
3e4093b6 3496\f
3e4093b6
RS
3497/* Return a tree for the difference of pointers OP0 and OP1.
3498 The resulting tree has type int. */
293c9fdd 3499
3e4093b6 3500static tree
db3927fb 3501pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3502{
3e4093b6 3503 tree restype = ptrdiff_type_node;
36c5e70a 3504 tree result, inttype;
400fbf9f 3505
36c5e70a
BE
3506 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3507 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3508 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3509 tree orig_op1 = op1;
400fbf9f 3510
36c5e70a
BE
3511 /* If the operands point into different address spaces, we need to
3512 explicitly convert them to pointers into the common address space
3513 before we can subtract the numerical address values. */
3514 if (as0 != as1)
3515 {
3516 addr_space_t as_common;
3517 tree common_type;
3518
3519 /* Determine the common superset address space. This is guaranteed
3520 to exist because the caller verified that comp_target_types
3521 returned non-zero. */
3522 if (!addr_space_superset (as0, as1, &as_common))
3523 gcc_unreachable ();
3524
3525 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3526 op0 = convert (common_type, op0);
3527 op1 = convert (common_type, op1);
3528 }
3529
3530 /* Determine integer type to perform computations in. This will usually
3531 be the same as the result type (ptrdiff_t), but may need to be a wider
3532 type if pointers for the address space are wider than ptrdiff_t. */
3533 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3534 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3535 else
3536 inttype = restype;
3537
fcf73884 3538 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3539 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3540 "pointer of type %<void *%> used in subtraction");
3541 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3542 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3543 "pointer to a function used in subtraction");
400fbf9f 3544
3e4093b6
RS
3545 /* First do the subtraction as integers;
3546 then drop through to build the divide operator.
3547 Do not do default conversions on the minus operator
3548 in case restype is a short type. */
400fbf9f 3549
db3927fb 3550 op0 = build_binary_op (loc,
36c5e70a
BE
3551 MINUS_EXPR, convert (inttype, op0),
3552 convert (inttype, op1), 0);
3e4093b6
RS
3553 /* This generates an error if op1 is pointer to incomplete type. */
3554 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3555 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3556
3e4093b6 3557 op1 = c_size_in_bytes (target_type);
400fbf9f 3558
f04dda30
MP
3559 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3560 error_at (loc, "arithmetic on pointer to an empty aggregate");
3561
3e4093b6 3562 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3563 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3564 op0, convert (inttype, op1));
3565
3566 /* Convert to final result type if necessary. */
3567 return convert (restype, result);
3e4093b6
RS
3568}
3569\f
267bac10
JM
3570/* Expand atomic compound assignments into an approriate sequence as
3571 specified by the C11 standard section 6.5.16.2.
3572 given
3573 _Atomic T1 E1
3574 T2 E2
3575 E1 op= E2
3576
3577 This sequence is used for all types for which these operations are
3578 supported.
3579
3580 In addition, built-in versions of the 'fe' prefixed routines may
3581 need to be invoked for floating point (real, complex or vector) when
3582 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3583
3584 T1 newval;
3585 T1 old;
3586 T1 *addr
3587 T2 val
3588 fenv_t fenv
3589
3590 addr = &E1;
3591 val = (E2);
3592 __atomic_load (addr, &old, SEQ_CST);
3593 feholdexcept (&fenv);
3594loop:
3595 newval = old op val;
3596 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3597 SEQ_CST))
3598 goto done;
3599 feclearexcept (FE_ALL_EXCEPT);
3600 goto loop:
3601done:
3602 feupdateenv (&fenv);
3603
3604 Also note that the compiler is simply issuing the generic form of
3605 the atomic operations. This requires temp(s) and has their address
3606 taken. The atomic processing is smart enough to figure out when the
3607 size of an object can utilize a lock-free version, and convert the
3608 built-in call to the appropriate lock-free routine. The optimizers
3609 will then dispose of any temps that are no longer required, and
3610 lock-free implementations are utilized as long as there is target
3611 support for the required size.
3612
3613 If the operator is NOP_EXPR, then this is a simple assignment, and
3614 an __atomic_store is issued to perform the assignment rather than
3615 the above loop.
3616
3617*/
3618
3619/* Build an atomic assignment at LOC, expanding into the proper
3620 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3621 the result of the operation, unless RETURN_OLD_P in which case
3622 return the old value of LHS (this is only for postincrement and
3623 postdecrement). */
3624static tree
3625build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3626 tree rhs, bool return_old_p)
3627{
3628 tree fndecl, func_call;
3629 vec<tree, va_gc> *params;
3630 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3631 tree old, old_addr;
3632 tree compound_stmt;
3633 tree stmt, goto_stmt;
3634 tree loop_label, loop_decl, done_label, done_decl;
3635
3636 tree lhs_type = TREE_TYPE (lhs);
3637 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3638 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3639 tree rhs_type = TREE_TYPE (rhs);
3640
3641 gcc_assert (TYPE_ATOMIC (lhs_type));
3642
3643 if (return_old_p)
3644 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3645
3646 /* Allocate enough vector items for a compare_exchange. */
3647 vec_alloc (params, 6);
3648
3649 /* Create a compound statement to hold the sequence of statements
3650 with a loop. */
3651 compound_stmt = c_begin_compound_stmt (false);
3652
3653 /* Fold the RHS if it hasn't already been folded. */
3654 if (modifycode != NOP_EXPR)
3655 rhs = c_fully_fold (rhs, false, NULL);
3656
3657 /* Remove the qualifiers for the rest of the expressions and create
3658 the VAL temp variable to hold the RHS. */
3659 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3660 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3661 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3662 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3663 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3664 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3665 NULL_TREE);
267bac10
JM
3666 SET_EXPR_LOCATION (rhs, loc);
3667 add_stmt (rhs);
3668
3669 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3670 an atomic_store. */
3671 if (modifycode == NOP_EXPR)
3672 {
3673 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3674 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3675 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3676 params->quick_push (lhs_addr);
3677 params->quick_push (rhs);
3678 params->quick_push (seq_cst);
8edbfaa6 3679 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3680 add_stmt (func_call);
3681
3682 /* Finish the compound statement. */
3683 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3684
3685 /* VAL is the value which was stored, return a COMPOUND_STMT of
3686 the statement and that value. */
3687 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3688 }
3689
3690 /* Create the variables and labels required for the op= form. */
5c4abbb8 3691 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3692 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3693 TREE_ADDRESSABLE (old) = 1;
3694 TREE_NO_WARNING (old) = 1;
267bac10 3695
5c4abbb8 3696 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3697 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3698 TREE_ADDRESSABLE (newval) = 1;
3699
3700 loop_decl = create_artificial_label (loc);
3701 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3702
3703 done_decl = create_artificial_label (loc);
3704 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3705
3706 /* __atomic_load (addr, &old, SEQ_CST). */
3707 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3708 params->quick_push (lhs_addr);
3709 params->quick_push (old_addr);
3710 params->quick_push (seq_cst);
8edbfaa6 3711 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3712 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3713 NULL_TREE);
3714 add_stmt (old);
267bac10
JM
3715 params->truncate (0);
3716
3717 /* Create the expressions for floating-point environment
3718 manipulation, if required. */
3719 bool need_fenv = (flag_trapping_math
3720 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3721 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3722 if (need_fenv)
3723 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3724
3725 if (hold_call)
3726 add_stmt (hold_call);
3727
3728 /* loop: */
3729 add_stmt (loop_label);
3730
3731 /* newval = old + val; */
3732 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3733 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3734 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3735 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3736 NULL_TREE, 0);
3737 if (rhs != error_mark_node)
3738 {
5c4abbb8
MP
3739 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3740 NULL_TREE);
267bac10
JM
3741 SET_EXPR_LOCATION (rhs, loc);
3742 add_stmt (rhs);
3743 }
3744
3745 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3746 goto done; */
3747 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3748 params->quick_push (lhs_addr);
3749 params->quick_push (old_addr);
3750 params->quick_push (newval_addr);
3751 params->quick_push (integer_zero_node);
3752 params->quick_push (seq_cst);
3753 params->quick_push (seq_cst);
8edbfaa6 3754 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3755
3756 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3757 SET_EXPR_LOCATION (goto_stmt, loc);
3758
3759 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3760 SET_EXPR_LOCATION (stmt, loc);
3761 add_stmt (stmt);
5c4abbb8 3762
267bac10
JM
3763 if (clear_call)
3764 add_stmt (clear_call);
3765
3766 /* goto loop; */
3767 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3768 SET_EXPR_LOCATION (goto_stmt, loc);
3769 add_stmt (goto_stmt);
5c4abbb8 3770
267bac10
JM
3771 /* done: */
3772 add_stmt (done_label);
3773
3774 if (update_call)
3775 add_stmt (update_call);
3776
3777 /* Finish the compound statement. */
3778 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3779
3780 /* NEWVAL is the value that was successfully stored, return a
3781 COMPOUND_EXPR of the statement and the appropriate value. */
3782 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3783 return_old_p ? old : newval);
3784}
3785
3e4093b6
RS
3786/* Construct and perhaps optimize a tree representation
3787 for a unary operation. CODE, a tree_code, specifies the operation
3788 and XARG is the operand.
3789 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3790 the default promotions (such as from short to int).
3791 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3792 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3793 arrays to pointers in C99.
3794
3795 LOCATION is the location of the operator. */
400fbf9f 3796
3e4093b6 3797tree
c9f9eb5d
AH
3798build_unary_op (location_t location,
3799 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3800{
3801 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3802 tree arg = xarg;
3803 tree argtype = 0;
808d6eaa 3804 enum tree_code typecode;
3e4093b6 3805 tree val;
c9f9eb5d 3806 tree ret = error_mark_node;
8ce94e44 3807 tree eptype = NULL_TREE;
3e4093b6 3808 int noconvert = flag;
4de67c26 3809 const char *invalid_op_diag;
928c19bb
JM
3810 bool int_operands;
3811
3812 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3813 if (int_operands)
3814 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3815
808d6eaa
JM
3816 if (code != ADDR_EXPR)
3817 arg = require_complete_type (arg);
3818
3819 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3820 if (typecode == ERROR_MARK)
3821 return error_mark_node;
3822 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3823 typecode = INTEGER_TYPE;
6c36d76b 3824
4de67c26
JM
3825 if ((invalid_op_diag
3826 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3827 {
c9f9eb5d 3828 error_at (location, invalid_op_diag);
4de67c26
JM
3829 return error_mark_node;
3830 }
3831
8ce94e44
JM
3832 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3833 {
3834 eptype = TREE_TYPE (arg);
3835 arg = TREE_OPERAND (arg, 0);
3836 }
3837
3e4093b6
RS
3838 switch (code)
3839 {
3840 case CONVERT_EXPR:
3841 /* This is used for unary plus, because a CONVERT_EXPR
3842 is enough to prevent anybody from looking inside for
3843 associativity, but won't generate any code. */
3844 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3845 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3846 || typecode == VECTOR_TYPE))
400fbf9f 3847 {
c9f9eb5d 3848 error_at (location, "wrong type argument to unary plus");
3e4093b6 3849 return error_mark_node;
400fbf9f 3850 }
3e4093b6
RS
3851 else if (!noconvert)
3852 arg = default_conversion (arg);
db3927fb 3853 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3854 break;
3855
3e4093b6
RS
3856 case NEGATE_EXPR:
3857 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3858 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3859 || typecode == VECTOR_TYPE))
3860 {
c9f9eb5d 3861 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3862 return error_mark_node;
3863 }
3864 else if (!noconvert)
3865 arg = default_conversion (arg);
400fbf9f
JW
3866 break;
3867
3e4093b6 3868 case BIT_NOT_EXPR:
462643f0
AP
3869 /* ~ works on integer types and non float vectors. */
3870 if (typecode == INTEGER_TYPE
3871 || (typecode == VECTOR_TYPE
3872 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3873 {
3e4093b6
RS
3874 if (!noconvert)
3875 arg = default_conversion (arg);
03d5b1f5 3876 }
3e4093b6 3877 else if (typecode == COMPLEX_TYPE)
400fbf9f 3878 {
3e4093b6 3879 code = CONJ_EXPR;
c1771a20 3880 pedwarn (location, OPT_Wpedantic,
fcf73884 3881 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3882 if (!noconvert)
3883 arg = default_conversion (arg);
3884 }
3885 else
3886 {
c9f9eb5d 3887 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3888 return error_mark_node;
400fbf9f
JW
3889 }
3890 break;
3891
3e4093b6 3892 case ABS_EXPR:
11017cc7 3893 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3894 {
c9f9eb5d 3895 error_at (location, "wrong type argument to abs");
3e4093b6 3896 return error_mark_node;
400fbf9f 3897 }
3e4093b6
RS
3898 else if (!noconvert)
3899 arg = default_conversion (arg);
400fbf9f
JW
3900 break;
3901
3e4093b6
RS
3902 case CONJ_EXPR:
3903 /* Conjugating a real value is a no-op, but allow it anyway. */
3904 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3905 || typecode == COMPLEX_TYPE))
400fbf9f 3906 {
c9f9eb5d 3907 error_at (location, "wrong type argument to conjugation");
3e4093b6 3908 return error_mark_node;
400fbf9f 3909 }
3e4093b6
RS
3910 else if (!noconvert)
3911 arg = default_conversion (arg);
400fbf9f
JW
3912 break;
3913
3e4093b6 3914 case TRUTH_NOT_EXPR:
ab22c1fa 3915 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3916 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3917 && typecode != COMPLEX_TYPE)
400fbf9f 3918 {
c9f9eb5d
AH
3919 error_at (location,
3920 "wrong type argument to unary exclamation mark");
3e4093b6 3921 return error_mark_node;
400fbf9f 3922 }
a27d595d
JM
3923 if (int_operands)
3924 {
3925 arg = c_objc_common_truthvalue_conversion (location, xarg);
3926 arg = remove_c_maybe_const_expr (arg);
3927 }
3928 else
3929 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3930 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3931 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3932 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3933 location = EXPR_LOCATION (ret);
c9f9eb5d 3934 goto return_build_unary_op;
3e4093b6 3935
3e4093b6 3936 case REALPART_EXPR:
3e4093b6 3937 case IMAGPART_EXPR:
fb52b50a
NF
3938 ret = build_real_imag_expr (location, code, arg);
3939 if (ret == error_mark_node)
3940 return error_mark_node;
8ce94e44
JM
3941 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3942 eptype = TREE_TYPE (eptype);
c9f9eb5d 3943 goto return_build_unary_op;
3e4093b6
RS
3944
3945 case PREINCREMENT_EXPR:
3946 case POSTINCREMENT_EXPR:
3947 case PREDECREMENT_EXPR:
3948 case POSTDECREMENT_EXPR:
3e4093b6 3949
928c19bb
JM
3950 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3951 {
3952 tree inner = build_unary_op (location, code,
3953 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3954 if (inner == error_mark_node)
3955 return error_mark_node;
3956 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3957 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3958 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3959 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3960 goto return_build_unary_op;
3961 }
3962
925e8657
NP
3963 /* Complain about anything that is not a true lvalue. In
3964 Objective-C, skip this check for property_refs. */
f90e8e2e 3965 if (!objc_is_property_ref (arg)
7bd11157
TT
3966 && !lvalue_or_else (location,
3967 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3968 || code == POSTINCREMENT_EXPR)
3969 ? lv_increment
3970 : lv_decrement)))
928c19bb
JM
3971 return error_mark_node;
3972
09639a83
ILT
3973 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3974 {
3975 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3976 warning_at (location, OPT_Wc___compat,
3977 "increment of enumeration value is invalid in C++");
3978 else
3979 warning_at (location, OPT_Wc___compat,
3980 "decrement of enumeration value is invalid in C++");
3981 }
3982
928c19bb
JM
3983 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3984 arg = c_fully_fold (arg, false, NULL);
3985
267bac10
JM
3986 bool atomic_op;
3987 atomic_op = really_atomic_lvalue (arg);
3988
3e4093b6
RS
3989 /* Increment or decrement the real part of the value,
3990 and don't change the imaginary part. */
3991 if (typecode == COMPLEX_TYPE)
400fbf9f 3992 {
3e4093b6
RS
3993 tree real, imag;
3994
c1771a20 3995 pedwarn (location, OPT_Wpedantic,
509c9d60 3996 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3997
267bac10
JM
3998 if (!atomic_op)
3999 {
4000 arg = stabilize_reference (arg);
4001 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4002 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4003 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4004 if (real == error_mark_node || imag == error_mark_node)
4005 return error_mark_node;
4006 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4007 real, imag);
4008 goto return_build_unary_op;
4009 }
400fbf9f 4010 }
3e4093b6
RS
4011
4012 /* Report invalid types. */
4013
ab22c1fa 4014 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4015 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4016 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4017 {
3e4093b6 4018 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4019 error_at (location, "wrong type argument to increment");
c22cacf3 4020 else
c9f9eb5d 4021 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4022
4023 return error_mark_node;
400fbf9f 4024 }
400fbf9f 4025
3e4093b6
RS
4026 {
4027 tree inc;
400fbf9f 4028
3e4093b6
RS
4029 argtype = TREE_TYPE (arg);
4030
4031 /* Compute the increment. */
4032
4033 if (typecode == POINTER_TYPE)
4034 {
a0e24419 4035 /* If pointer target is an incomplete type,
3e4093b6 4036 we just cannot know how to do the arithmetic. */
b70cef5d 4037 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4038 {
4039 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4040 error_at (location,
a0e24419
MP
4041 "increment of pointer to an incomplete type %qT",
4042 TREE_TYPE (argtype));
3e4093b6 4043 else
c9f9eb5d 4044 error_at (location,
a0e24419
MP
4045 "decrement of pointer to an incomplete type %qT",
4046 TREE_TYPE (argtype));
3e4093b6 4047 }
b70cef5d
JJ
4048 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4049 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4050 {
3e4093b6 4051 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4052 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4053 "wrong type argument to increment");
3e4093b6 4054 else
44d90fe1 4055 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4056 "wrong type argument to decrement");
3e4093b6
RS
4057 }
4058
b70cef5d 4059 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4060 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4061 }
b70cef5d 4062 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4063 {
4064 /* For signed fract types, we invert ++ to -- or
4065 -- to ++, and change inc from 1 to -1, because
4066 it is not possible to represent 1 in signed fract constants.
4067 For unsigned fract types, the result always overflows and
4068 we get an undefined (original) or the maximum value. */
4069 if (code == PREINCREMENT_EXPR)
4070 code = PREDECREMENT_EXPR;
4071 else if (code == PREDECREMENT_EXPR)
4072 code = PREINCREMENT_EXPR;
4073 else if (code == POSTINCREMENT_EXPR)
4074 code = POSTDECREMENT_EXPR;
4075 else /* code == POSTDECREMENT_EXPR */
4076 code = POSTINCREMENT_EXPR;
4077
4078 inc = integer_minus_one_node;
4079 inc = convert (argtype, inc);
4080 }
3e4093b6 4081 else
5be014d5 4082 {
241b71bb
TV
4083 inc = VECTOR_TYPE_P (argtype)
4084 ? build_one_cst (argtype)
4085 : integer_one_node;
5be014d5
AP
4086 inc = convert (argtype, inc);
4087 }
3e4093b6 4088
925e8657
NP
4089 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4090 need to ask Objective-C to build the increment or decrement
4091 expression for it. */
4092 if (objc_is_property_ref (arg))
f90e8e2e 4093 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4094 arg, inc);
4095
3e4093b6 4096 /* Report a read-only lvalue. */
f37acdf9 4097 if (TYPE_READONLY (argtype))
953ff289 4098 {
c02065fc 4099 readonly_error (location, arg,
953ff289
DN
4100 ((code == PREINCREMENT_EXPR
4101 || code == POSTINCREMENT_EXPR)
4102 ? lv_increment : lv_decrement));
4103 return error_mark_node;
4104 }
f37acdf9
JM
4105 else if (TREE_READONLY (arg))
4106 readonly_warning (arg,
4107 ((code == PREINCREMENT_EXPR
4108 || code == POSTINCREMENT_EXPR)
4109 ? lv_increment : lv_decrement));
3e4093b6 4110
267bac10
JM
4111 /* If the argument is atomic, use the special code sequences for
4112 atomic compound assignment. */
4113 if (atomic_op)
4114 {
4115 arg = stabilize_reference (arg);
4116 ret = build_atomic_assign (location, arg,
4117 ((code == PREINCREMENT_EXPR
4118 || code == POSTINCREMENT_EXPR)
4119 ? PLUS_EXPR
4120 : MINUS_EXPR),
4121 (FRACT_MODE_P (TYPE_MODE (argtype))
4122 ? inc
4123 : integer_one_node),
4124 (code == POSTINCREMENT_EXPR
4125 || code == POSTDECREMENT_EXPR));
4126 goto return_build_unary_op;
4127 }
4128
3e4093b6
RS
4129 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4130 val = boolean_increment (code, arg);
4131 else
53fb4de3 4132 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4133 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4134 if (TREE_CODE (val) != code)
6de9cd9a 4135 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4136 ret = val;
4137 goto return_build_unary_op;
3e4093b6
RS
4138 }
4139
4140 case ADDR_EXPR:
4141 /* Note that this operation never does default_conversion. */
4142
2b4b7036
JM
4143 /* The operand of unary '&' must be an lvalue (which excludes
4144 expressions of type void), or, in C99, the result of a [] or
4145 unary '*' operator. */
4146 if (VOID_TYPE_P (TREE_TYPE (arg))
4147 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4148 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4149 pedwarn (location, 0, "taking address of expression of type %<void%>");
4150
3e4093b6 4151 /* Let &* cancel out to simplify resulting code. */
22d03525 4152 if (INDIRECT_REF_P (arg))
400fbf9f 4153 {
3e4093b6
RS
4154 /* Don't let this be an lvalue. */
4155 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4156 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4157 ret = TREE_OPERAND (arg, 0);
4158 goto return_build_unary_op;
400fbf9f 4159 }
1eb8759b 4160
7c672dfc 4161 /* For &x[y], return x+y */
3e4093b6 4162 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4163 {
f2a71bbc
JM
4164 tree op0 = TREE_OPERAND (arg, 0);
4165 if (!c_mark_addressable (op0))
3e4093b6 4166 return error_mark_node;
1eb8759b 4167 }
1eb8759b 4168
3e4093b6
RS
4169 /* Anything not already handled and not a true memory reference
4170 or a non-lvalue array is an error. */
4171 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4172 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4173 return error_mark_node;
b6a10c9f 4174
928c19bb
JM
4175 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4176 folding later. */
4177 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4178 {
4179 tree inner = build_unary_op (location, code,
4180 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4181 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4182 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4183 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4184 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4185 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4186 goto return_build_unary_op;
4187 }
4188
3e4093b6
RS
4189 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4190 argtype = TREE_TYPE (arg);
400fbf9f 4191
3e4093b6 4192 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4193 to which the address will point. This is only needed
f2c1da78 4194 for function types. */
6615c446 4195 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4196 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4197 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4198 {
4199 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4200 int quals = orig_quals;
4201
4202 if (TREE_READONLY (arg))
4203 quals |= TYPE_QUAL_CONST;
4204 if (TREE_THIS_VOLATILE (arg))
4205 quals |= TYPE_QUAL_VOLATILE;
4206
f2c1da78
JM
4207 argtype = c_build_qualified_type (argtype, quals);
4208 }
400fbf9f 4209
3e4093b6
RS
4210 if (!c_mark_addressable (arg))
4211 return error_mark_node;
400fbf9f 4212
abb54d14
JM
4213 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4214 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4215
5cc200fc 4216 argtype = build_pointer_type (argtype);
5e55f99d
RH
4217
4218 /* ??? Cope with user tricks that amount to offsetof. Delete this
4219 when we have proper support for integer constant expressions. */
4220 val = get_base_address (arg);
22d03525 4221 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4222 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4223 {
cf9e9959 4224 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4225 goto return_build_unary_op;
3aa2ddb8 4226 }
5e55f99d 4227
5cc200fc 4228 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4229
c9f9eb5d
AH
4230 ret = val;
4231 goto return_build_unary_op;
400fbf9f 4232
3e4093b6 4233 default:
1344f9a3 4234 gcc_unreachable ();
3e4093b6 4235 }
400fbf9f 4236
3e4093b6
RS
4237 if (argtype == 0)
4238 argtype = TREE_TYPE (arg);
928c19bb
JM
4239 if (TREE_CODE (arg) == INTEGER_CST)
4240 ret = (require_constant_value
db3927fb
AH
4241 ? fold_build1_initializer_loc (location, code, argtype, arg)
4242 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4243 else
4244 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4245 return_build_unary_op:
4246 gcc_assert (ret != error_mark_node);
928c19bb
JM
4247 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4248 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4249 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4250 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4251 ret = note_integer_operands (ret);
8ce94e44
JM
4252 if (eptype)
4253 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4254 protected_set_expr_location (ret, location);
4255 return ret;
3e4093b6 4256}
400fbf9f 4257
3e4093b6
RS
4258/* Return nonzero if REF is an lvalue valid for this language.
4259 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4260 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4261
1e4ae551 4262bool
58f9752a 4263lvalue_p (const_tree ref)
3e4093b6 4264{
58f9752a 4265 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4266
3e4093b6
RS
4267 switch (code)
4268 {
4269 case REALPART_EXPR:
4270 case IMAGPART_EXPR:
4271 case COMPONENT_REF:
4272 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4273
928c19bb
JM
4274 case C_MAYBE_CONST_EXPR:
4275 return lvalue_p (TREE_OPERAND (ref, 1));
4276
3e4093b6
RS
4277 case COMPOUND_LITERAL_EXPR:
4278 case STRING_CST:
4279 return 1;
400fbf9f 4280
3e4093b6
RS
4281 case INDIRECT_REF:
4282 case ARRAY_REF:
36536d79 4283 case ARRAY_NOTATION_REF:
3e4093b6
RS
4284 case VAR_DECL:
4285 case PARM_DECL:
4286 case RESULT_DECL:
4287 case ERROR_MARK:
4288 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4289 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4290
3e4093b6 4291 case BIND_EXPR:
3e4093b6 4292 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4293
3e4093b6
RS
4294 default:
4295 return 0;
4296 }
4297}
400fbf9f 4298\f
f37acdf9
JM
4299/* Give a warning for storing in something that is read-only in GCC
4300 terms but not const in ISO C terms. */
4301
4302static void
4303readonly_warning (tree arg, enum lvalue_use use)
4304{
4305 switch (use)
4306 {
4307 case lv_assign:
4308 warning (0, "assignment of read-only location %qE", arg);
4309 break;
4310 case lv_increment:
4311 warning (0, "increment of read-only location %qE", arg);
4312 break;
4313 case lv_decrement:
4314 warning (0, "decrement of read-only location %qE", arg);
4315 break;
4316 default:
4317 gcc_unreachable ();
4318 }
4319 return;
4320}
4321
37dc0d8d
JM
4322
4323/* Return nonzero if REF is an lvalue valid for this language;
4324 otherwise, print an error message and return zero. USE says
7bd11157
TT
4325 how the lvalue is being used and so selects the error message.
4326 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4327
4328static int
7bd11157 4329lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4330{
4331 int win = lvalue_p (ref);
4332
4333 if (!win)
7bd11157 4334 lvalue_error (loc, use);
37dc0d8d
JM
4335
4336 return win;
4337}
3e4093b6
RS
4338\f
4339/* Mark EXP saying that we need to be able to take the
4340 address of it; it should not be allocated in a register.
4341 Returns true if successful. */
54c93c30 4342
3e4093b6
RS
4343bool
4344c_mark_addressable (tree exp)
400fbf9f 4345{
3e4093b6 4346 tree x = exp;
95602da1 4347
3e4093b6
RS
4348 while (1)
4349 switch (TREE_CODE (x))
4350 {
4351 case COMPONENT_REF:
4352 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4353 {
0039fa55
AN
4354 error
4355 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4356 return false;
4357 }
95602da1 4358
3e4093b6 4359 /* ... fall through ... */
95602da1 4360
3e4093b6
RS
4361 case ADDR_EXPR:
4362 case ARRAY_REF:
4363 case REALPART_EXPR:
4364 case IMAGPART_EXPR:
4365 x = TREE_OPERAND (x, 0);
4366 break;
95602da1 4367
3e4093b6
RS
4368 case COMPOUND_LITERAL_EXPR:
4369 case CONSTRUCTOR:
4370 TREE_ADDRESSABLE (x) = 1;
4371 return true;
95602da1 4372
3e4093b6
RS
4373 case VAR_DECL:
4374 case CONST_DECL:
4375 case PARM_DECL:
4376 case RESULT_DECL:
5baeaac0 4377 if (C_DECL_REGISTER (x)
3e4093b6
RS
4378 && DECL_NONLOCAL (x))
4379 {
62f9079a 4380 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4381 {
0039fa55
AN
4382 error
4383 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4384 return false;
4385 }
509c9d60 4386 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4387 }
5baeaac0 4388 else if (C_DECL_REGISTER (x))
3e4093b6 4389 {
62f9079a 4390 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4391 error ("address of global register variable %qD requested", x);
4392 else
4393 error ("address of register variable %qD requested", x);
4394 return false;
3e4093b6 4395 }
400fbf9f 4396
3e4093b6
RS
4397 /* drops in */
4398 case FUNCTION_DECL:
4399 TREE_ADDRESSABLE (x) = 1;
4400 /* drops out */
4401 default:
4402 return true;
4403 }
4404}
4405\f
2d2e923f
MLI
4406/* Convert EXPR to TYPE, warning about conversion problems with
4407 constants. SEMANTIC_TYPE is the type this conversion would use
4408 without excess precision. If SEMANTIC_TYPE is NULL, this function
4409 is equivalent to convert_and_check. This function is a wrapper that
4410 handles conversions that may be different than
4411 the usual ones because of excess precision. */
4412
4413static tree
68fca595
MP
4414ep_convert_and_check (location_t loc, tree type, tree expr,
4415 tree semantic_type)
2d2e923f
MLI
4416{
4417 if (TREE_TYPE (expr) == type)
4418 return expr;
4419
4420 if (!semantic_type)
68fca595 4421 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4422
4423 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4424 && TREE_TYPE (expr) != semantic_type)
4425 {
4426 /* For integers, we need to check the real conversion, not
4427 the conversion to the excess precision type. */
68fca595 4428 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4429 }
4430 /* Result type is the excess precision type, which should be
4431 large enough, so do not check. */
4432 return convert (type, expr);
4433}
4434
928c19bb
JM
4435/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4436 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4437 if folded to an integer constant then the unselected half may
4438 contain arbitrary operations not normally permitted in constant
c2255bc4 4439 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4440
4441tree
744aa42f 4442build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4443 tree op1, tree op1_original_type, tree op2,
4444 tree op2_original_type)
400fbf9f 4445{
3e4093b6
RS
4446 tree type1;
4447 tree type2;
4448 enum tree_code code1;
4449 enum tree_code code2;
4450 tree result_type = NULL;
2d2e923f 4451 tree semantic_result_type = NULL;
3e4093b6 4452 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4453 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4454 bool ifexp_int_operands;
928c19bb 4455 tree ret;
400fbf9f 4456
4d84fe7c
JM
4457 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4458 if (op1_int_operands)
4459 op1 = remove_c_maybe_const_expr (op1);
4460 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4461 if (op2_int_operands)
4462 op2 = remove_c_maybe_const_expr (op2);
4463 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4464 if (ifexp_int_operands)
4465 ifexp = remove_c_maybe_const_expr (ifexp);
4466
3e4093b6
RS
4467 /* Promote both alternatives. */
4468
4469 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4470 op1 = default_conversion (op1);
4471 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4472 op2 = default_conversion (op2);
4473
4474 if (TREE_CODE (ifexp) == ERROR_MARK
4475 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4476 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4477 return error_mark_node;
400fbf9f 4478
3e4093b6
RS
4479 type1 = TREE_TYPE (op1);
4480 code1 = TREE_CODE (type1);
4481 type2 = TREE_TYPE (op2);
4482 code2 = TREE_CODE (type2);
4483
b1adf557
JM
4484 /* C90 does not permit non-lvalue arrays in conditional expressions.
4485 In C99 they will be pointers by now. */
4486 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4487 {
744aa42f 4488 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4489 return error_mark_node;
4490 }
4491
8ce94e44
JM
4492 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4493 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4494 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4495 || code1 == COMPLEX_TYPE)
4496 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4497 || code2 == COMPLEX_TYPE))
4498 {
2d2e923f 4499 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4500 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4501 {
4502 op1 = TREE_OPERAND (op1, 0);
4503 type1 = TREE_TYPE (op1);
4504 gcc_assert (TREE_CODE (type1) == code1);
4505 }
4506 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4507 {
4508 op2 = TREE_OPERAND (op2, 0);
4509 type2 = TREE_TYPE (op2);
4510 gcc_assert (TREE_CODE (type2) == code2);
4511 }
4512 }
4513
d130ae11
ILT
4514 if (warn_cxx_compat)
4515 {
4516 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4517 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4518
4519 if (TREE_CODE (t1) == ENUMERAL_TYPE
4520 && TREE_CODE (t2) == ENUMERAL_TYPE
4521 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4522 warning_at (colon_loc, OPT_Wc___compat,
4523 ("different enum types in conditional is "
4524 "invalid in C++: %qT vs %qT"),
4525 t1, t2);
4526 }
4527
3e4093b6
RS
4528 /* Quickly detect the usual case where op1 and op2 have the same type
4529 after promotion. */
4530 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4531 {
3e4093b6
RS
4532 if (type1 == type2)
4533 result_type = type1;
4534 else
4535 result_type = TYPE_MAIN_VARIANT (type1);
4536 }
4537 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4538 || code1 == COMPLEX_TYPE)
4539 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4540 || code2 == COMPLEX_TYPE))
3e4093b6 4541 {
ccf7f880 4542 result_type = c_common_type (type1, type2);
0a0b3574
MM
4543 do_warn_double_promotion (result_type, type1, type2,
4544 "implicit conversion from %qT to %qT to "
4545 "match other result of conditional",
4546 colon_loc);
400fbf9f 4547
3e4093b6
RS
4548 /* If -Wsign-compare, warn here if type1 and type2 have
4549 different signedness. We'll promote the signed to unsigned
4550 and later code won't know it used to be different.
4551 Do this check on the original types, so that explicit casts
4552 will be considered, but default promotions won't. */
7d882b83 4553 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4554 {
8df83eae
RK
4555 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4556 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4557
3e4093b6
RS
4558 if (unsigned_op1 ^ unsigned_op2)
4559 {
6ac01510
ILT
4560 bool ovf;
4561
3e4093b6
RS
4562 /* Do not warn if the result type is signed, since the
4563 signed type will only be chosen if it can represent
4564 all the values of the unsigned type. */
3f75a254 4565 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4566 /* OK */;
3e4093b6 4567 else
928c19bb
JM
4568 {
4569 bool op1_maybe_const = true;
4570 bool op2_maybe_const = true;
4571
4572 /* Do not warn if the signed quantity is an
4573 unsuffixed integer literal (or some static
4574 constant expression involving such literals) and
4575 it is non-negative. This warning requires the
4576 operands to be folded for best results, so do
4577 that folding in this case even without
4578 warn_sign_compare to avoid warning options
4579 possibly affecting code generation. */
f5178456
RS
4580 c_inhibit_evaluation_warnings
4581 += (ifexp == truthvalue_false_node);
928c19bb
JM
4582 op1 = c_fully_fold (op1, require_constant_value,
4583 &op1_maybe_const);
f5178456
RS
4584 c_inhibit_evaluation_warnings
4585 -= (ifexp == truthvalue_false_node);
4586
4587 c_inhibit_evaluation_warnings
4588 += (ifexp == truthvalue_true_node);
928c19bb
JM
4589 op2 = c_fully_fold (op2, require_constant_value,
4590 &op2_maybe_const);
f5178456
RS
4591 c_inhibit_evaluation_warnings
4592 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4593
4594 if (warn_sign_compare)
4595 {
4596 if ((unsigned_op2
4597 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4598 || (unsigned_op1
4599 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4600 /* OK */;
4601 else
744aa42f
ILT
4602 warning_at (colon_loc, OPT_Wsign_compare,
4603 ("signed and unsigned type in "
4604 "conditional expression"));
928c19bb
JM
4605 }
4606 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4607 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4608 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4609 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4610 }
3e4093b6
RS
4611 }
4612 }
4613 }
4614 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4615 {
fcf73884 4616 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4617 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4618 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4619 result_type = void_type_node;
4620 }
4621 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4622 {
36c5e70a
BE
4623 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4624 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4625 addr_space_t as_common;
4626
744aa42f 4627 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4628 result_type = common_pointer_type (type1, type2);
6aa3c60d 4629 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4630 result_type = type2;
6aa3c60d 4631 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4632 result_type = type1;
4633 else if (!addr_space_superset (as1, as2, &as_common))
4634 {
4635 error_at (colon_loc, "pointers to disjoint address spaces "
4636 "used in conditional expression");
4637 return error_mark_node;
4638 }
267bac10
JM
4639 else if (VOID_TYPE_P (TREE_TYPE (type1))
4640 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4641 {
768952be
MU
4642 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4643 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4644 & ~TYPE_QUALS (TREE_TYPE (type1))))
4645 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4646 "pointer to array loses qualifier "
4647 "in conditional expression");
4648
fcf73884 4649 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4650 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4651 "ISO C forbids conditional expr between "
bda67431 4652 "%<void *%> and function pointer");
3e4093b6
RS
4653 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4654 TREE_TYPE (type2)));
34a80643 4655 }
267bac10
JM
4656 else if (VOID_TYPE_P (TREE_TYPE (type2))
4657 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4658 {
768952be
MU
4659 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4660 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4661 & ~TYPE_QUALS (TREE_TYPE (type2))))
4662 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4663 "pointer to array loses qualifier "
4664 "in conditional expression");
4665
fcf73884 4666 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4667 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4668 "ISO C forbids conditional expr between "
bda67431 4669 "%<void *%> and function pointer");
3e4093b6
RS
4670 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4671 TREE_TYPE (type1)));
1c2a9b35 4672 }
b581b85b
NP
4673 /* Objective-C pointer comparisons are a bit more lenient. */
4674 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4675 result_type = objc_common_type (type1, type2);
34a80643 4676 else
ab87f8c8 4677 {
36c5e70a
BE
4678 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4679
b581b85b
NP
4680 pedwarn (colon_loc, 0,
4681 "pointer type mismatch in conditional expression");
36c5e70a
BE
4682 result_type = build_pointer_type
4683 (build_qualified_type (void_type_node, qual));
ab87f8c8 4684 }
3e4093b6
RS
4685 }
4686 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4687 {
6aa3c60d 4688 if (!null_pointer_constant_p (orig_op2))
744aa42f 4689 pedwarn (colon_loc, 0,
509c9d60 4690 "pointer/integer type mismatch in conditional expression");
3e4093b6 4691 else
ab87f8c8 4692 {
3e4093b6 4693 op2 = null_pointer_node;
ab87f8c8 4694 }
3e4093b6
RS
4695 result_type = type1;
4696 }
4697 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4698 {
6aa3c60d 4699 if (!null_pointer_constant_p (orig_op1))
744aa42f 4700 pedwarn (colon_loc, 0,
509c9d60 4701 "pointer/integer type mismatch in conditional expression");
3e4093b6 4702 else
ab87f8c8 4703 {
3e4093b6 4704 op1 = null_pointer_node;
ab87f8c8 4705 }
3e4093b6
RS
4706 result_type = type2;
4707 }
1c2a9b35 4708
3e4093b6
RS
4709 if (!result_type)
4710 {
4711 if (flag_cond_mismatch)
4712 result_type = void_type_node;
4713 else
400fbf9f 4714 {
c2255bc4 4715 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4716 return error_mark_node;
400fbf9f 4717 }
3e4093b6 4718 }
400fbf9f 4719
3e4093b6
RS
4720 /* Merge const and volatile flags of the incoming types. */
4721 result_type
4722 = build_type_variant (result_type,
afbd0665
AS
4723 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4724 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4725
68fca595
MP
4726 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4727 semantic_result_type);
4728 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4729 semantic_result_type);
b6a10c9f 4730
928c19bb
JM
4731 if (ifexp_bcp && ifexp == truthvalue_true_node)
4732 {
4733 op2_int_operands = true;
4734 op1 = c_fully_fold (op1, require_constant_value, NULL);
4735 }
4736 if (ifexp_bcp && ifexp == truthvalue_false_node)
4737 {
4738 op1_int_operands = true;
4739 op2 = c_fully_fold (op2, require_constant_value, NULL);
4740 }
4d84fe7c 4741 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4742 && op1_int_operands
4743 && op2_int_operands);
4744 if (int_operands)
4745 {
4746 int_const = ((ifexp == truthvalue_true_node
4747 && TREE_CODE (orig_op1) == INTEGER_CST
4748 && !TREE_OVERFLOW (orig_op1))
4749 || (ifexp == truthvalue_false_node
4750 && TREE_CODE (orig_op2) == INTEGER_CST
4751 && !TREE_OVERFLOW (orig_op2)));
4752 }
4753 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4754 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4755 else
4756 {
01c7ccbb
JM
4757 if (int_operands)
4758 {
f34f1c87
MP
4759 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4760 nested inside of the expression. */
4761 op1 = c_fully_fold (op1, false, NULL);
4762 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4763 }
928c19bb
JM
4764 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4765 if (int_operands)
4766 ret = note_integer_operands (ret);
4767 }
2d2e923f
MLI
4768 if (semantic_result_type)
4769 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4770
c2255bc4 4771 protected_set_expr_location (ret, colon_loc);
928c19bb 4772 return ret;
3e4093b6
RS
4773}
4774\f
487a92fe 4775/* Return a compound expression that performs two expressions and
c2255bc4
AH
4776 returns the value of the second of them.
4777
4778 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4779
3e4093b6 4780tree
c2255bc4 4781build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4782{
4d84fe7c 4783 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4784 tree eptype = NULL_TREE;
928c19bb
JM
4785 tree ret;
4786
b72271b9 4787 if (flag_cilkplus
939b37da
BI
4788 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4789 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4790 {
4791 error_at (loc,
4792 "spawned function call cannot be part of a comma expression");
4793 return error_mark_node;
4794 }
4d84fe7c
JM
4795 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4796 if (expr1_int_operands)
4797 expr1 = remove_c_maybe_const_expr (expr1);
4798 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4799 if (expr2_int_operands)
4800 expr2 = remove_c_maybe_const_expr (expr2);
4801
8ce94e44
JM
4802 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4803 expr1 = TREE_OPERAND (expr1, 0);
4804 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4805 {
4806 eptype = TREE_TYPE (expr2);
4807 expr2 = TREE_OPERAND (expr2, 0);
4808 }
4809
3f75a254 4810 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4811 {
4812 /* The left-hand operand of a comma expression is like an expression
c5409249 4813 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4814 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4815 if (warn_unused_value)
47aecf47 4816 {
e14a6540 4817 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4818 && CONVERT_EXPR_P (expr1))
47aecf47 4819 ; /* (void) a, b */
e14a6540
JM
4820 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4821 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4822 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4823 ; /* (void) a, (void) b, c */
4824 else
b8698a0f 4825 warning_at (loc, OPT_Wunused_value,
c2255bc4 4826 "left-hand operand of comma expression has no effect");
47aecf47 4827 }
3e4093b6 4828 }
789eadcd
MP
4829 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4830 && warn_unused_value)
4831 {
4832 tree r = expr1;
4833 location_t cloc = loc;
4834 while (TREE_CODE (r) == COMPOUND_EXPR)
4835 {
4836 if (EXPR_HAS_LOCATION (r))
4837 cloc = EXPR_LOCATION (r);
4838 r = TREE_OPERAND (r, 1);
4839 }
4840 if (!TREE_SIDE_EFFECTS (r)
4841 && !VOID_TYPE_P (TREE_TYPE (r))
4842 && !CONVERT_EXPR_P (r))
4843 warning_at (cloc, OPT_Wunused_value,
4844 "right-hand operand of comma expression has no effect");
4845 }
400fbf9f 4846
3e4093b6
RS
4847 /* With -Wunused, we should also warn if the left-hand operand does have
4848 side-effects, but computes a value which is not used. For example, in
4849 `foo() + bar(), baz()' the result of the `+' operator is not used,
4850 so we should issue a warning. */
4851 else if (warn_unused_value)
c2255bc4 4852 warn_if_unused_value (expr1, loc);
400fbf9f 4853
e63d6886
AP
4854 if (expr2 == error_mark_node)
4855 return error_mark_node;
4856
928c19bb
JM
4857 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4858
4859 if (flag_isoc99
4d84fe7c
JM
4860 && expr1_int_operands
4861 && expr2_int_operands)
928c19bb
JM
4862 ret = note_integer_operands (ret);
4863
8ce94e44
JM
4864 if (eptype)
4865 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4866
c2255bc4 4867 protected_set_expr_location (ret, loc);
928c19bb 4868 return ret;
3e4093b6 4869}
400fbf9f 4870
67165eb3
ILT
4871/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4872 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4873 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4874 of the cast. -Wcast-qual appeared on the command line. Named
4875 address space qualifiers are not handled here, because they result
4876 in different warnings. */
67165eb3
ILT
4877
4878static void
2ee3cb35 4879handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4880{
4881 tree in_type = type;
4882 tree in_otype = otype;
4883 int added = 0;
4884 int discarded = 0;
4885 bool is_const;
4886
4887 /* Check that the qualifiers on IN_TYPE are a superset of the
4888 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4889 nodes is uninteresting and we stop as soon as we hit a
4890 non-POINTER_TYPE node on either type. */
4891 do
4892 {
4893 in_otype = TREE_TYPE (in_otype);
4894 in_type = TREE_TYPE (in_type);
4895
4896 /* GNU C allows cv-qualified function types. 'const' means the
4897 function is very pure, 'volatile' means it can't return. We
4898 need to warn when such qualifiers are added, not when they're
4899 taken away. */
4900 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4901 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4902 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4903 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4904 else
36c5e70a
BE
4905 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4906 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4907 }
4908 while (TREE_CODE (in_type) == POINTER_TYPE
4909 && TREE_CODE (in_otype) == POINTER_TYPE);
4910
4911 if (added)
2ee3cb35
MLI
4912 warning_at (loc, OPT_Wcast_qual,
4913 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4914
4915 if (discarded)
4916 /* There are qualifiers present in IN_OTYPE that are not present
4917 in IN_TYPE. */
2ee3cb35 4918 warning_at (loc, OPT_Wcast_qual,
7485aeea 4919 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4920 discarded);
67165eb3
ILT
4921
4922 if (added || discarded)
4923 return;
4924
4925 /* A cast from **T to const **T is unsafe, because it can cause a
4926 const value to be changed with no additional warning. We only
4927 issue this warning if T is the same on both sides, and we only
4928 issue the warning if there are the same number of pointers on
4929 both sides, as otherwise the cast is clearly unsafe anyhow. A
4930 cast is unsafe when a qualifier is added at one level and const
4931 is not present at all outer levels.
4932
4933 To issue this warning, we check at each level whether the cast
4934 adds new qualifiers not already seen. We don't need to special
4935 case function types, as they won't have the same
4936 TYPE_MAIN_VARIANT. */
4937
4938 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4939 return;
4940 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4941 return;
4942
4943 in_type = type;
4944 in_otype = otype;
4945 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4946 do
4947 {
4948 in_type = TREE_TYPE (in_type);
4949 in_otype = TREE_TYPE (in_otype);
4950 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4951 && !is_const)
4952 {
2ee3cb35
MLI
4953 warning_at (loc, OPT_Wcast_qual,
4954 "to be safe all intermediate pointers in cast from "
4955 "%qT to %qT must be %<const%> qualified",
4956 otype, type);
67165eb3
ILT
4957 break;
4958 }
4959 if (is_const)
4960 is_const = TYPE_READONLY (in_type);
4961 }
4962 while (TREE_CODE (in_type) == POINTER_TYPE);
4963}
4964
b8698a0f 4965/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4966 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4967
3e4093b6 4968tree
c2255bc4 4969build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4970{
8ce94e44
JM
4971 tree value;
4972
4973 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4974 expr = TREE_OPERAND (expr, 0);
4975
4976 value = expr;
400fbf9f 4977
3e4093b6
RS
4978 if (type == error_mark_node || expr == error_mark_node)
4979 return error_mark_node;
400fbf9f 4980
3e4093b6
RS
4981 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4982 only in <protocol> qualifications. But when constructing cast expressions,
4983 the protocols do matter and must be kept around. */
700686fa
ZL
4984 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4985 return build1 (NOP_EXPR, type, expr);
4986
4987 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4988
3e4093b6
RS
4989 if (TREE_CODE (type) == ARRAY_TYPE)
4990 {
c2255bc4 4991 error_at (loc, "cast specifies array type");
3e4093b6
RS
4992 return error_mark_node;
4993 }
400fbf9f 4994
3e4093b6
RS
4995 if (TREE_CODE (type) == FUNCTION_TYPE)
4996 {
c2255bc4 4997 error_at (loc, "cast specifies function type");
3e4093b6
RS
4998 return error_mark_node;
4999 }
400fbf9f 5000
808d6eaa
JM
5001 if (!VOID_TYPE_P (type))
5002 {
5003 value = require_complete_type (value);
5004 if (value == error_mark_node)
5005 return error_mark_node;
5006 }
5007
3e4093b6
RS
5008 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5009 {
fcf73884
MLI
5010 if (TREE_CODE (type) == RECORD_TYPE
5011 || TREE_CODE (type) == UNION_TYPE)
c1771a20 5012 pedwarn (loc, OPT_Wpedantic,
fcf73884 5013 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5014
5015 /* Convert to remove any qualifiers from VALUE's type. */
5016 value = convert (type, value);
3e4093b6
RS
5017 }
5018 else if (TREE_CODE (type) == UNION_TYPE)
5019 {
5020 tree field;
400fbf9f 5021
910ad8de 5022 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5023 if (TREE_TYPE (field) != error_mark_node
5024 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5025 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5026 break;
5027
5028 if (field)
400fbf9f 5029 {
3e4093b6 5030 tree t;
e616f54d 5031 bool maybe_const = true;
3e4093b6 5032
c1771a20 5033 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5034 t = c_fully_fold (value, false, &maybe_const);
5035 t = build_constructor_single (type, field, t);
5036 if (!maybe_const)
5037 t = c_wrap_maybe_const (t, true);
5038 t = digest_init (loc, type, t,
bbbbb16a 5039 NULL_TREE, false, true, 0);
3e4093b6
RS
5040 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5041 return t;
400fbf9f 5042 }
c2255bc4 5043 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5044 return error_mark_node;
5045 }
5046 else
5047 {
5048 tree otype, ovalue;
400fbf9f 5049
3e4093b6 5050 if (type == void_type_node)
c2255bc4
AH
5051 {
5052 tree t = build1 (CONVERT_EXPR, type, value);
5053 SET_EXPR_LOCATION (t, loc);
5054 return t;
5055 }
400fbf9f 5056
3e4093b6 5057 otype = TREE_TYPE (value);
400fbf9f 5058
3e4093b6 5059 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5060 if (warn_cast_qual
5061 && TREE_CODE (type) == POINTER_TYPE
5062 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5063 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5064
36c5e70a
BE
5065 /* Warn about conversions between pointers to disjoint
5066 address spaces. */
5067 if (TREE_CODE (type) == POINTER_TYPE
5068 && TREE_CODE (otype) == POINTER_TYPE
5069 && !null_pointer_constant_p (value))
5070 {
5071 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5072 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5073 addr_space_t as_common;
5074
5075 if (!addr_space_superset (as_to, as_from, &as_common))
5076 {
5077 if (ADDR_SPACE_GENERIC_P (as_from))
5078 warning_at (loc, 0, "cast to %s address space pointer "
5079 "from disjoint generic address space pointer",
5080 c_addr_space_name (as_to));
5081
5082 else if (ADDR_SPACE_GENERIC_P (as_to))
5083 warning_at (loc, 0, "cast to generic address space pointer "
5084 "from disjoint %s address space pointer",
5085 c_addr_space_name (as_from));
5086
5087 else
5088 warning_at (loc, 0, "cast to %s address space pointer "
5089 "from disjoint %s address space pointer",
5090 c_addr_space_name (as_to),
5091 c_addr_space_name (as_from));
5092 }
5093 }
5094
3e4093b6 5095 /* Warn about possible alignment problems. */
3176a0c2 5096 if (STRICT_ALIGNMENT
3e4093b6
RS
5097 && TREE_CODE (type) == POINTER_TYPE
5098 && TREE_CODE (otype) == POINTER_TYPE
5099 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5100 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5101 /* Don't warn about opaque types, where the actual alignment
5102 restriction is unknown. */
5103 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5104 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5105 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5106 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5107 warning_at (loc, OPT_Wcast_align,
5108 "cast increases required alignment of target type");
e9a25f70 5109
3176a0c2 5110 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5111 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5112 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5113 /* Unlike conversion of integers to pointers, where the
5114 warning is disabled for converting constants because
5115 of cases such as SIG_*, warn about converting constant
5116 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5117 sign extension, and a warning is appropriate. */
c2255bc4
AH
5118 warning_at (loc, OPT_Wpointer_to_int_cast,
5119 "cast from pointer to integer of different size");
400fbf9f 5120
3176a0c2 5121 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5122 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5123 warning_at (loc, OPT_Wbad_function_cast,
5124 "cast from function call of type %qT "
5125 "to non-matching type %qT", otype, type);
400fbf9f 5126
3176a0c2 5127 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5128 && TREE_CODE (otype) == INTEGER_TYPE
5129 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5130 /* Don't warn about converting any constant. */
5131 && !TREE_CONSTANT (value))
c2255bc4
AH
5132 warning_at (loc,
5133 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5134 "of different size");
400fbf9f 5135
79bedddc
SR
5136 if (warn_strict_aliasing <= 2)
5137 strict_aliasing_warning (otype, type, expr);
400fbf9f 5138
3897f229
JM
5139 /* If pedantic, warn for conversions between function and object
5140 pointer types, except for converting a null pointer constant
5141 to function pointer type. */
5142 if (pedantic
5143 && TREE_CODE (type) == POINTER_TYPE
5144 && TREE_CODE (otype) == POINTER_TYPE
5145 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5146 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5147 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5148 "conversion of function pointer to object pointer type");
3897f229
JM
5149
5150 if (pedantic
5151 && TREE_CODE (type) == POINTER_TYPE
5152 && TREE_CODE (otype) == POINTER_TYPE
5153 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5154 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5155 && !null_pointer_constant_p (value))
c1771a20 5156 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5157 "conversion of object pointer to function pointer type");
3897f229 5158
3e4093b6 5159 ovalue = value;
3e4093b6 5160 value = convert (type, value);
400fbf9f 5161
3e4093b6 5162 /* Ignore any integer overflow caused by the cast. */
928c19bb 5163 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5164 {
8bcd6380 5165 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5166 {
8bcd6380
RS
5167 if (!TREE_OVERFLOW (value))
5168 {
5169 /* Avoid clobbering a shared constant. */
5170 value = copy_node (value);
5171 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5172 }
6414bad6 5173 }
8bcd6380 5174 else if (TREE_OVERFLOW (value))
d8e1f97b 5175 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5176 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5177 }
5178 }
400fbf9f 5179
53cd18ec 5180 /* Don't let a cast be an lvalue. */
9482b620 5181 if (lvalue_p (value))
db3927fb 5182 value = non_lvalue_loc (loc, value);
e9a25f70 5183
928c19bb
JM
5184 /* Don't allow the results of casting to floating-point or complex
5185 types be confused with actual constants, or casts involving
5186 integer and pointer types other than direct integer-to-integer
5187 and integer-to-pointer be confused with integer constant
5188 expressions and null pointer constants. */
5189 if (TREE_CODE (value) == REAL_CST
5190 || TREE_CODE (value) == COMPLEX_CST
5191 || (TREE_CODE (value) == INTEGER_CST
5192 && !((TREE_CODE (expr) == INTEGER_CST
5193 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5194 || TREE_CODE (expr) == REAL_CST
5195 || TREE_CODE (expr) == COMPLEX_CST)))
5196 value = build1 (NOP_EXPR, type, value);
5197
c2255bc4
AH
5198 if (CAN_HAVE_LOCATION_P (value))
5199 SET_EXPR_LOCATION (value, loc);
3e4093b6 5200 return value;
400fbf9f
JW
5201}
5202
c2255bc4
AH
5203/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5204 location of the open paren of the cast, or the position of the cast
5205 expr. */
3e4093b6 5206tree
c2255bc4 5207c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5208{
f8893e47 5209 tree type;
928c19bb
JM
5210 tree type_expr = NULL_TREE;
5211 bool type_expr_const = true;
5212 tree ret;
3e4093b6 5213 int saved_wsp = warn_strict_prototypes;
c5c76735 5214
3e4093b6
RS
5215 /* This avoids warnings about unprototyped casts on
5216 integers. E.g. "#define SIG_DFL (void(*)())0". */
5217 if (TREE_CODE (expr) == INTEGER_CST)
5218 warn_strict_prototypes = 0;
928c19bb 5219 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5220 warn_strict_prototypes = saved_wsp;
c5c76735 5221
c2255bc4 5222 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5223 if (type_expr)
5224 {
9f33203d
JM
5225 bool inner_expr_const = true;
5226 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5227 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5228 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5229 && inner_expr_const);
c2255bc4 5230 SET_EXPR_LOCATION (ret, loc);
928c19bb 5231 }
24b97832
ILT
5232
5233 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5234 SET_EXPR_LOCATION (ret, loc);
5235
9e5b2115
PB
5236 /* C++ does not permits types to be defined in a cast, but it
5237 allows references to incomplete types. */
5238 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5239 warning_at (loc, OPT_Wc___compat,
5240 "defining a type in a cast is invalid in C++");
5241
928c19bb 5242 return ret;
400fbf9f 5243}
3e4093b6
RS
5244\f
5245/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5246 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5247 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5248 MODIFYCODE is the code for a binary operator that we use
5249 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5250 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5251 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5252 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5253
c2255bc4
AH
5254 LOCATION is the location of the MODIFYCODE operator.
5255 RHS_LOC is the location of the RHS. */
2f6e4e97 5256
3e4093b6 5257tree
32e8bb8e 5258build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5259 enum tree_code modifycode,
c2255bc4 5260 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5261{
3e4093b6
RS
5262 tree result;
5263 tree newrhs;
241f845a 5264 tree rhseval = NULL_TREE;
8ce94e44 5265 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5266 tree lhstype = TREE_TYPE (lhs);
5267 tree olhstype = lhstype;
928c19bb 5268 bool npc;
267bac10 5269 bool is_atomic_op;
e9a25f70 5270
3e4093b6
RS
5271 /* Types that aren't fully specified cannot be used in assignments. */
5272 lhs = require_complete_type (lhs);
e9a25f70 5273
3e4093b6
RS
5274 /* Avoid duplicate error messages from operands that had errors. */
5275 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5276 return error_mark_node;
400fbf9f 5277
4c2ecab0
JM
5278 /* Ensure an error for assigning a non-lvalue array to an array in
5279 C90. */
5280 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5281 {
5282 error_at (location, "assignment to expression with array type");
5283 return error_mark_node;
5284 }
5285
46a88c12 5286 /* For ObjC properties, defer this check. */
7bd11157 5287 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5288 return error_mark_node;
5289
267bac10
JM
5290 is_atomic_op = really_atomic_lvalue (lhs);
5291
8ce94e44
JM
5292 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5293 {
5294 rhs_semantic_type = TREE_TYPE (rhs);
5295 rhs = TREE_OPERAND (rhs, 0);
5296 }
5297
3e4093b6 5298 newrhs = rhs;
400fbf9f 5299
928c19bb
JM
5300 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5301 {
5302 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5303 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5304 rhs_origtype);
928c19bb
JM
5305 if (inner == error_mark_node)
5306 return error_mark_node;
5307 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5308 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5309 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5310 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5311 protected_set_expr_location (result, location);
5312 return result;
5313 }
5314
3e4093b6
RS
5315 /* If a binary op has been requested, combine the old LHS value with the RHS
5316 producing the value we should actually store into the LHS. */
5317
5318 if (modifycode != NOP_EXPR)
400fbf9f 5319 {
928c19bb 5320 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5321 lhs = stabilize_reference (lhs);
bbbbb16a 5322
267bac10
JM
5323 /* Construct the RHS for any non-atomic compound assignemnt. */
5324 if (!is_atomic_op)
5325 {
241f845a
JJ
5326 /* If in LHS op= RHS the RHS has side-effects, ensure they
5327 are preevaluated before the rest of the assignment expression's
5328 side-effects, because RHS could contain e.g. function calls
5329 that modify LHS. */
5330 if (TREE_SIDE_EFFECTS (rhs))
5331 {
5332 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5333 rhseval = newrhs;
5334 }
267bac10 5335 newrhs = build_binary_op (location,
241f845a 5336 modifycode, lhs, newrhs, 1);
267bac10
JM
5337
5338 /* The original type of the right hand side is no longer
5339 meaningful. */
5340 rhs_origtype = NULL_TREE;
5341 }
400fbf9f 5342 }
400fbf9f 5343
668ea4b1
IS
5344 if (c_dialect_objc ())
5345 {
46a88c12
NP
5346 /* Check if we are modifying an Objective-C property reference;
5347 if so, we need to generate setter calls. */
5348 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5349 if (result)
241f845a 5350 goto return_result;
46a88c12
NP
5351
5352 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5353 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5354 return error_mark_node;
5355 }
5356
9bf24266 5357 /* Give an error for storing in something that is 'const'. */
bbbd6700 5358
f37acdf9 5359 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5360 || ((TREE_CODE (lhstype) == RECORD_TYPE
5361 || TREE_CODE (lhstype) == UNION_TYPE)
5362 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5363 {
c02065fc 5364 readonly_error (location, lhs, lv_assign);
953ff289
DN
5365 return error_mark_node;
5366 }
f37acdf9
JM
5367 else if (TREE_READONLY (lhs))
5368 readonly_warning (lhs, lv_assign);
bbbd6700 5369
3e4093b6
RS
5370 /* If storing into a structure or union member,
5371 it has probably been given type `int'.
5372 Compute the type that would go with
5373 the actual amount of storage the member occupies. */
bbbd6700 5374
3e4093b6
RS
5375 if (TREE_CODE (lhs) == COMPONENT_REF
5376 && (TREE_CODE (lhstype) == INTEGER_TYPE
5377 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5378 || TREE_CODE (lhstype) == REAL_TYPE
5379 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5380 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5381
3e4093b6
RS
5382 /* If storing in a field that is in actuality a short or narrower than one,
5383 we must store in the field in its actual type. */
5384
5385 if (lhstype != TREE_TYPE (lhs))
5386 {
5387 lhs = copy_node (lhs);
5388 TREE_TYPE (lhs) = lhstype;
400fbf9f 5389 }
400fbf9f 5390
32e8bb8e
ILT
5391 /* Issue -Wc++-compat warnings about an assignment to an enum type
5392 when LHS does not have its original type. This happens for,
5393 e.g., an enum bitfield in a struct. */
5394 if (warn_cxx_compat
5395 && lhs_origtype != NULL_TREE
5396 && lhs_origtype != lhstype
5397 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5398 {
5399 tree checktype = (rhs_origtype != NULL_TREE
5400 ? rhs_origtype
5401 : TREE_TYPE (rhs));
5402 if (checktype != error_mark_node
267bac10
JM
5403 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5404 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5405 warning_at (location, OPT_Wc___compat,
5406 "enum conversion in assignment is invalid in C++");
5407 }
5408
267bac10
JM
5409 /* If the lhs is atomic, remove that qualifier. */
5410 if (is_atomic_op)
5411 {
5412 lhstype = build_qualified_type (lhstype,
5413 (TYPE_QUALS (lhstype)
5414 & ~TYPE_QUAL_ATOMIC));
5415 olhstype = build_qualified_type (olhstype,
5416 (TYPE_QUALS (lhstype)
5417 & ~TYPE_QUAL_ATOMIC));
5418 }
5419
8ce94e44
JM
5420 /* Convert new value to destination type. Fold it first, then
5421 restore any excess precision information, for the sake of
5422 conversion warnings. */
400fbf9f 5423
267bac10
JM
5424 if (!(is_atomic_op && modifycode != NOP_EXPR))
5425 {
5426 npc = null_pointer_constant_p (newrhs);
5427 newrhs = c_fully_fold (newrhs, false, NULL);
5428 if (rhs_semantic_type)
5429 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5430 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5431 rhs_origtype, ic_assign, npc,
5432 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5433 if (TREE_CODE (newrhs) == ERROR_MARK)
5434 return error_mark_node;
5435 }
400fbf9f 5436
6e955430
ZL
5437 /* Emit ObjC write barrier, if necessary. */
5438 if (c_dialect_objc () && flag_objc_gc)
5439 {
5440 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5441 if (result)
c9f9eb5d
AH
5442 {
5443 protected_set_expr_location (result, location);
241f845a 5444 goto return_result;
c9f9eb5d 5445 }
6e955430
ZL
5446 }
5447
ea4b7848 5448 /* Scan operands. */
400fbf9f 5449
267bac10
JM
5450 if (is_atomic_op)
5451 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5452 else
5453 {
5454 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5455 TREE_SIDE_EFFECTS (result) = 1;
5456 protected_set_expr_location (result, location);
5457 }
400fbf9f 5458
3e4093b6
RS
5459 /* If we got the LHS in a different type for storing in,
5460 convert the result back to the nominal type of LHS
5461 so that the value we return always has the same type
5462 as the LHS argument. */
e855c5ce 5463
3e4093b6 5464 if (olhstype == TREE_TYPE (result))
241f845a 5465 goto return_result;
c9f9eb5d 5466
68fca595
MP
5467 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5468 rhs_origtype, ic_assign, false, NULL_TREE,
5469 NULL_TREE, 0);
c9f9eb5d 5470 protected_set_expr_location (result, location);
241f845a
JJ
5471
5472return_result:
5473 if (rhseval)
5474 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5475 return result;
3e4093b6
RS
5476}
5477\f
478a1c5b
ILT
5478/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5479 This is used to implement -fplan9-extensions. */
5480
5481static bool
5482find_anonymous_field_with_type (tree struct_type, tree type)
5483{
5484 tree field;
5485 bool found;
5486
5487 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5488 || TREE_CODE (struct_type) == UNION_TYPE);
5489 found = false;
5490 for (field = TYPE_FIELDS (struct_type);
5491 field != NULL_TREE;
5492 field = TREE_CHAIN (field))
5493 {
267bac10
JM
5494 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5495 ? c_build_qualified_type (TREE_TYPE (field),
5496 TYPE_QUAL_ATOMIC)
5497 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5498 if (DECL_NAME (field) == NULL
267bac10 5499 && comptypes (type, fieldtype))
478a1c5b
ILT
5500 {
5501 if (found)
5502 return false;
5503 found = true;
5504 }
5505 else if (DECL_NAME (field) == NULL
5506 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5507 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5508 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5509 {
5510 if (found)
5511 return false;
5512 found = true;
5513 }
5514 }
5515 return found;
5516}
5517
5518/* RHS is an expression whose type is pointer to struct. If there is
5519 an anonymous field in RHS with type TYPE, then return a pointer to
5520 that field in RHS. This is used with -fplan9-extensions. This
5521 returns NULL if no conversion could be found. */
5522
5523static tree
5524convert_to_anonymous_field (location_t location, tree type, tree rhs)
5525{
5526 tree rhs_struct_type, lhs_main_type;
5527 tree field, found_field;
5528 bool found_sub_field;
5529 tree ret;
5530
5531 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5532 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5533 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5534 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5535
5536 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5537 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5538 ? c_build_qualified_type (TREE_TYPE (type),
5539 TYPE_QUAL_ATOMIC)
5540 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5541
5542 found_field = NULL_TREE;
5543 found_sub_field = false;
5544 for (field = TYPE_FIELDS (rhs_struct_type);
5545 field != NULL_TREE;
5546 field = TREE_CHAIN (field))
5547 {
5548 if (DECL_NAME (field) != NULL_TREE
5549 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5550 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5551 continue;
267bac10
JM
5552 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5553 ? c_build_qualified_type (TREE_TYPE (field),
5554 TYPE_QUAL_ATOMIC)
5555 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5556 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5557 {
5558 if (found_field != NULL_TREE)
5559 return NULL_TREE;
5560 found_field = field;
5561 }
5562 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5563 lhs_main_type))
5564 {
5565 if (found_field != NULL_TREE)
5566 return NULL_TREE;
5567 found_field = field;
5568 found_sub_field = true;
5569 }
5570 }
5571
5572 if (found_field == NULL_TREE)
5573 return NULL_TREE;
5574
5575 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5576 build_fold_indirect_ref (rhs), found_field,
5577 NULL_TREE);
5578 ret = build_fold_addr_expr_loc (location, ret);
5579
5580 if (found_sub_field)
5581 {
5582 ret = convert_to_anonymous_field (location, type, ret);
5583 gcc_assert (ret != NULL_TREE);
5584 }
5585
5586 return ret;
5587}
5588
63bc4e87
MP
5589/* Issue an error message for a bad initializer component.
5590 GMSGID identifies the message.
5591 The component name is taken from the spelling stack. */
5592
5593static void
ea58ef42 5594error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5595{
5596 char *ofwhat;
5597
5598 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5599 error_at (loc, gmsgid);
63bc4e87
MP
5600 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5601 if (*ofwhat)
d7ff7ae5 5602 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5603}
5604
5605/* Issue a pedantic warning for a bad initializer component. OPT is
5606 the option OPT_* (from options.h) controlling this warning or 0 if
5607 it is unconditionally given. GMSGID identifies the message. The
5608 component name is taken from the spelling stack. */
5609
5610static void
5611pedwarn_init (location_t location, int opt, const char *gmsgid)
5612{
5613 char *ofwhat;
d7ff7ae5 5614 bool warned;
63bc4e87
MP
5615
5616 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5617 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5618 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5619 if (*ofwhat && warned)
5620 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5621}
5622
5623/* Issue a warning for a bad initializer component.
5624
5625 OPT is the OPT_W* value corresponding to the warning option that
5626 controls this warning. GMSGID identifies the message. The
5627 component name is taken from the spelling stack. */
5628
5629static void
5630warning_init (location_t loc, int opt, const char *gmsgid)
5631{
5632 char *ofwhat;
d7ff7ae5 5633 bool warned;
63bc4e87
MP
5634
5635 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5636 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5637 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5638 if (*ofwhat && warned)
5639 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5640}
5641\f
5642/* If TYPE is an array type and EXPR is a parenthesized string
5643 constant, warn if pedantic that EXPR is being used to initialize an
5644 object of type TYPE. */
5645
5646void
d033409e 5647maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5648{
5649 if (pedantic
5650 && TREE_CODE (type) == ARRAY_TYPE
5651 && TREE_CODE (expr.value) == STRING_CST
5652 && expr.original_code != STRING_CST)
d033409e 5653 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5654 "array initialized from parenthesized string constant");
5655}
5656
bbbbb16a
ILT
5657/* Convert value RHS to type TYPE as preparation for an assignment to
5658 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5659 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5660 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5661 constant before any folding.
3e4093b6
RS
5662 The real work of conversion is done by `convert'.
5663 The purpose of this function is to generate error messages
5664 for assignments that are not allowed in C.
2ac2f164
JM
5665 ERRTYPE says whether it is argument passing, assignment,
5666 initialization or return.
2f6e4e97 5667
81e5eca8
MP
5668 LOCATION is the location of the assignment, EXPR_LOC is the location of
5669 the RHS or, for a function, location of an argument.
2ac2f164 5670 FUNCTION is a tree for the function being called.
3e4093b6 5671 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5672
3e4093b6 5673static tree
81e5eca8 5674convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5675 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5676 bool null_pointer_constant, tree fundecl,
5677 tree function, int parmnum)
3e4093b6
RS
5678{
5679 enum tree_code codel = TREE_CODE (type);
8ce94e44 5680 tree orig_rhs = rhs;
3e4093b6
RS
5681 tree rhstype;
5682 enum tree_code coder;
2ac2f164 5683 tree rname = NULL_TREE;
58393038 5684 bool objc_ok = false;
2ac2f164 5685
6b4ef5c1 5686 if (errtype == ic_argpass)
2ac2f164
JM
5687 {
5688 tree selector;
5689 /* Change pointer to function to the function itself for
5690 diagnostics. */
5691 if (TREE_CODE (function) == ADDR_EXPR
5692 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5693 function = TREE_OPERAND (function, 0);
5694
5695 /* Handle an ObjC selector specially for diagnostics. */
5696 selector = objc_message_selector ();
5697 rname = function;
5698 if (selector && parmnum > 2)
5699 {
5700 rname = selector;
5701 parmnum -= 2;
5702 }
5703 }
5704
5705 /* This macro is used to emit diagnostics to ensure that all format
5706 strings are complete sentences, visible to gettext and checked at
5707 compile time. */
768952be 5708#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5709 do { \
5710 switch (errtype) \
5711 { \
5712 case ic_argpass: \
5c1bc275 5713 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5714 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5715 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5716 "expected %qT but argument is of type %qT", \
5717 type, rhstype); \
5718 break; \
1e053dfe
MLI
5719 case ic_assign: \
5720 pedwarn (LOCATION, OPT, AS); \
5721 break; \
5722 case ic_init: \
6a8f4e12 5723 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5724 break; \
5725 case ic_return: \
d033409e 5726 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5727 break; \
5728 default: \
5729 gcc_unreachable (); \
5730 } \
2ac2f164 5731 } while (0)
cb3ca04e 5732
49706e39
MLI
5733 /* This macro is used to emit diagnostics to ensure that all format
5734 strings are complete sentences, visible to gettext and checked at
768952be 5735 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5736 extra parameter to enumerate qualifiers. */
768952be 5737#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5738 do { \
5739 switch (errtype) \
5740 { \
5741 case ic_argpass: \
5c1bc275 5742 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5743 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5744 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5745 "expected %qT but argument is of type %qT", \
5746 type, rhstype); \
5747 break; \
5748 case ic_assign: \
5c1bc275 5749 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5750 break; \
5751 case ic_init: \
5c1bc275 5752 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5753 break; \
5754 case ic_return: \
5c1bc275 5755 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5756 break; \
5757 default: \
5758 gcc_unreachable (); \
5759 } \
5760 } while (0)
5761
768952be
MU
5762 /* This macro is used to emit diagnostics to ensure that all format
5763 strings are complete sentences, visible to gettext and checked at
5764 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5765 warning_at instead of pedwarn. */
5766#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5767 do { \
5768 switch (errtype) \
5769 { \
5770 case ic_argpass: \
5771 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5772 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5773 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5774 "expected %qT but argument is of type %qT", \
5775 type, rhstype); \
5776 break; \
5777 case ic_assign: \
5778 warning_at (LOCATION, OPT, AS, QUALS); \
5779 break; \
5780 case ic_init: \
5781 warning_at (LOCATION, OPT, IN, QUALS); \
5782 break; \
5783 case ic_return: \
5784 warning_at (LOCATION, OPT, RE, QUALS); \
5785 break; \
5786 default: \
5787 gcc_unreachable (); \
5788 } \
5789 } while (0)
5790
8ce94e44
JM
5791 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5792 rhs = TREE_OPERAND (rhs, 0);
5793
3e4093b6
RS
5794 rhstype = TREE_TYPE (rhs);
5795 coder = TREE_CODE (rhstype);
5796
5797 if (coder == ERROR_MARK)
5798 return error_mark_node;
5799
58393038
ZL
5800 if (c_dialect_objc ())
5801 {
5802 int parmno;
5803
5804 switch (errtype)
5805 {
5806 case ic_return:
5807 parmno = 0;
5808 break;
5809
5810 case ic_assign:
5811 parmno = -1;
5812 break;
5813
5814 case ic_init:
5815 parmno = -2;
5816 break;
5817
5818 default:
5819 parmno = parmnum;
5820 break;
5821 }
5822
5823 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5824 }
5825
bbbbb16a
ILT
5826 if (warn_cxx_compat)
5827 {
5828 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5829 if (checktype != error_mark_node
5830 && TREE_CODE (type) == ENUMERAL_TYPE
5831 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5832 {
768952be
MU
5833 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5834 G_("enum conversion when passing argument "
5835 "%d of %qE is invalid in C++"),
5836 G_("enum conversion in assignment is "
5837 "invalid in C++"),
5838 G_("enum conversion in initialization is "
5839 "invalid in C++"),
5840 G_("enum conversion in return is "
5841 "invalid in C++"));
bbbbb16a
ILT
5842 }
5843 }
5844
3e4093b6 5845 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5846 return rhs;
3e4093b6
RS
5847
5848 if (coder == VOID_TYPE)
400fbf9f 5849 {
6dcc04b0
JM
5850 /* Except for passing an argument to an unprototyped function,
5851 this is a constraint violation. When passing an argument to
5852 an unprototyped function, it is compile-time undefined;
5853 making it a constraint in that case was rejected in
5854 DR#252. */
c2255bc4 5855 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5856 return error_mark_node;
400fbf9f 5857 }
808d6eaa
JM
5858 rhs = require_complete_type (rhs);
5859 if (rhs == error_mark_node)
5860 return error_mark_node;
cd192ccc
MS
5861 /* A non-reference type can convert to a reference. This handles
5862 va_start, va_copy and possibly port built-ins. */
5863 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5864 {
3e4093b6 5865 if (!lvalue_p (rhs))
400fbf9f 5866 {
c2255bc4 5867 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5868 return error_mark_node;
400fbf9f 5869 }
3e4093b6
RS
5870 if (!c_mark_addressable (rhs))
5871 return error_mark_node;
5872 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5873 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5874
81e5eca8 5875 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5876 build_pointer_type (TREE_TYPE (type)),
5877 rhs, origtype, errtype,
5878 null_pointer_constant, fundecl, function,
5879 parmnum);
cd192ccc
MS
5880 if (rhs == error_mark_node)
5881 return error_mark_node;
3e4093b6
RS
5882
5883 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5884 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5885 return rhs;
400fbf9f 5886 }
3e4093b6 5887 /* Some types can interconvert without explicit casts. */
3274deff 5888 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5889 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5890 return convert (type, rhs);
5891 /* Arithmetic types all interconvert, and enum is treated like int. */
5892 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5893 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5894 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5895 || codel == BOOLEAN_TYPE)
5896 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5897 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5898 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5899 || coder == BOOLEAN_TYPE))
928c19bb
JM
5900 {
5901 tree ret;
5902 bool save = in_late_binary_op;
e5341100
JJ
5903 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5904 || (coder == REAL_TYPE
5905 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5906 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 5907 in_late_binary_op = true;
81e5eca8
MP
5908 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5909 ? expr_loc : location, type, orig_rhs);
e5341100 5910 in_late_binary_op = save;
928c19bb
JM
5911 return ret;
5912 }
400fbf9f 5913
79077aea
JJ
5914 /* Aggregates in different TUs might need conversion. */
5915 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5916 && codel == coder
5917 && comptypes (type, rhstype))
81e5eca8
MP
5918 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5919 ? expr_loc : location, type, rhs);
79077aea 5920
ebf0bf7f 5921 /* Conversion to a transparent union or record from its member types.
3e4093b6 5922 This applies only to function arguments. */
ebf0bf7f
JJ
5923 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5924 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5925 && errtype == ic_argpass)
400fbf9f 5926 {
0257e383 5927 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5928
910ad8de 5929 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5930 {
0257e383 5931 tree memb_type = TREE_TYPE (memb);
400fbf9f 5932
3e4093b6 5933 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5934 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5935 break;
e58cd767 5936
3e4093b6
RS
5937 if (TREE_CODE (memb_type) != POINTER_TYPE)
5938 continue;
2f6e4e97 5939
3e4093b6
RS
5940 if (coder == POINTER_TYPE)
5941 {
5942 tree ttl = TREE_TYPE (memb_type);
5943 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5944
3e4093b6
RS
5945 /* Any non-function converts to a [const][volatile] void *
5946 and vice versa; otherwise, targets must be the same.
5947 Meanwhile, the lhs target must have all the qualifiers of
5948 the rhs. */
267bac10
JM
5949 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5950 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5951 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5952 {
267bac10
JM
5953 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5954 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5955 /* If this type won't generate any warnings, use it. */
267bac10 5956 if (lquals == rquals
3e4093b6
RS
5957 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5958 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5959 ? ((lquals | rquals) == rquals)
5960 : ((lquals | rquals) == lquals)))
3e4093b6 5961 break;
400fbf9f 5962
3e4093b6 5963 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5964 if (!marginal_memb)
5965 marginal_memb = memb;
3e4093b6
RS
5966 }
5967 }
82bde854 5968
3e4093b6 5969 /* Can convert integer zero to any pointer type. */
928c19bb 5970 if (null_pointer_constant)
3e4093b6
RS
5971 {
5972 rhs = null_pointer_node;
5973 break;
5974 }
5975 }
400fbf9f 5976
0257e383 5977 if (memb || marginal_memb)
3e4093b6 5978 {
0257e383 5979 if (!memb)
3e4093b6
RS
5980 {
5981 /* We have only a marginally acceptable member type;
5982 it needs a warning. */
0257e383 5983 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5984 tree ttr = TREE_TYPE (rhstype);
714a0864 5985
3e4093b6
RS
5986 /* Const and volatile mean something different for function
5987 types, so the usual warnings are not appropriate. */
5988 if (TREE_CODE (ttr) == FUNCTION_TYPE
5989 && TREE_CODE (ttl) == FUNCTION_TYPE)
5990 {
5991 /* Because const and volatile on functions are
5992 restrictions that say the function will not do
5993 certain things, it is okay to use a const or volatile
5994 function where an ordinary one is wanted, but not
5995 vice-versa. */
36c5e70a
BE
5996 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5997 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
5998 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
5999 OPT_Wdiscarded_qualifiers,
6000 G_("passing argument %d of %qE "
6001 "makes %q#v qualified function "
6002 "pointer from unqualified"),
6003 G_("assignment makes %q#v qualified "
6004 "function pointer from "
6005 "unqualified"),
6006 G_("initialization makes %q#v qualified "
6007 "function pointer from "
6008 "unqualified"),
6009 G_("return makes %q#v qualified function "
6010 "pointer from unqualified"),
6011 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6012 }
36c5e70a
BE
6013 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6014 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6015 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6016 OPT_Wdiscarded_qualifiers,
6017 G_("passing argument %d of %qE discards "
6018 "%qv qualifier from pointer target type"),
6019 G_("assignment discards %qv qualifier "
6020 "from pointer target type"),
6021 G_("initialization discards %qv qualifier "
6022 "from pointer target type"),
6023 G_("return discards %qv qualifier from "
6024 "pointer target type"),
6025 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6026
6027 memb = marginal_memb;
3e4093b6 6028 }
400fbf9f 6029
fcf73884 6030 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6031 pedwarn (location, OPT_Wpedantic,
fcf73884 6032 "ISO C prohibits argument conversion to union type");
0e7c47fa 6033
db3927fb 6034 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6035 return build_constructor_single (type, memb, rhs);
3e4093b6 6036 }
0e7c47fa
RK
6037 }
6038
3e4093b6
RS
6039 /* Conversions among pointers */
6040 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6041 && (coder == codel))
400fbf9f 6042 {
3e4093b6
RS
6043 tree ttl = TREE_TYPE (type);
6044 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6045 tree mvl = ttl;
6046 tree mvr = ttr;
3e4093b6 6047 bool is_opaque_pointer;
264fa2db 6048 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6049 addr_space_t asl;
6050 addr_space_t asr;
400fbf9f 6051
46df2823 6052 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6053 mvl = (TYPE_ATOMIC (mvl)
6054 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6055 TYPE_QUAL_ATOMIC)
6056 : TYPE_MAIN_VARIANT (mvl));
46df2823 6057 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6058 mvr = (TYPE_ATOMIC (mvr)
6059 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6060 TYPE_QUAL_ATOMIC)
6061 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6062 /* Opaque pointers are treated like void pointers. */
f83c7f63 6063 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6064
478a1c5b
ILT
6065 /* The Plan 9 compiler permits a pointer to a struct to be
6066 automatically converted into a pointer to an anonymous field
6067 within the struct. */
6068 if (flag_plan9_extensions
6069 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6070 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6071 && mvl != mvr)
6072 {
6073 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6074 if (new_rhs != NULL_TREE)
6075 {
6076 rhs = new_rhs;
6077 rhstype = TREE_TYPE (rhs);
6078 coder = TREE_CODE (rhstype);
6079 ttr = TREE_TYPE (rhstype);
6080 mvr = TYPE_MAIN_VARIANT (ttr);
6081 }
6082 }
6083
b7e20b53 6084 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6085 for the purpose of reducing the number of false positives, we
6086 tolerate the special case of
b7e20b53 6087
c22cacf3 6088 int *p = NULL;
b7e20b53 6089
c22cacf3 6090 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6091 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6092 warning_at (errtype == ic_argpass ? expr_loc : location,
6093 OPT_Wc___compat,
6094 "request for implicit conversion "
c2255bc4 6095 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6096
36c5e70a
BE
6097 /* See if the pointers point to incompatible address spaces. */
6098 asl = TYPE_ADDR_SPACE (ttl);
6099 asr = TYPE_ADDR_SPACE (ttr);
6100 if (!null_pointer_constant_p (rhs)
6101 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6102 {
6103 switch (errtype)
6104 {
6105 case ic_argpass:
8ffcdea8 6106 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6107 "non-enclosed address space", parmnum, rname);
6108 break;
6109 case ic_assign:
6110 error_at (location, "assignment from pointer to "
6111 "non-enclosed address space");
6112 break;
6113 case ic_init:
6114 error_at (location, "initialization from pointer to "
6115 "non-enclosed address space");
6116 break;
6117 case ic_return:
6118 error_at (location, "return from pointer to "
6119 "non-enclosed address space");
6120 break;
6121 default:
6122 gcc_unreachable ();
6123 }
6124 return error_mark_node;
6125 }
6126
7876a414
KG
6127 /* Check if the right-hand side has a format attribute but the
6128 left-hand side doesn't. */
90137d8f 6129 if (warn_suggest_attribute_format
104f8784 6130 && check_missing_format_attribute (type, rhstype))
c22cacf3 6131 {
104f8784
KG
6132 switch (errtype)
6133 {
6134 case ic_argpass:
8ffcdea8 6135 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6136 "argument %d of %qE might be "
6137 "a candidate for a format attribute",
6138 parmnum, rname);
104f8784
KG
6139 break;
6140 case ic_assign:
90137d8f 6141 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6142 "assignment left-hand side might be "
6143 "a candidate for a format attribute");
104f8784
KG
6144 break;
6145 case ic_init:
90137d8f 6146 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6147 "initialization left-hand side might be "
6148 "a candidate for a format attribute");
104f8784
KG
6149 break;
6150 case ic_return:
90137d8f 6151 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6152 "return type might be "
6153 "a candidate for a format attribute");
104f8784
KG
6154 break;
6155 default:
6156 gcc_unreachable ();
6157 }
7876a414 6158 }
c22cacf3 6159
3e4093b6
RS
6160 /* Any non-function converts to a [const][volatile] void *
6161 and vice versa; otherwise, targets must be the same.
6162 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6163 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6164 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6165 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6166 || is_opaque_pointer
f8a93a2e
JJ
6167 || ((c_common_unsigned_type (mvl)
6168 == c_common_unsigned_type (mvr))
267bac10
JM
6169 && (c_common_signed_type (mvl)
6170 == c_common_signed_type (mvr))
6171 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6172 {
768952be
MU
6173 /* Warn about loss of qualifers from pointers to arrays with
6174 qualifiers on the element type. */
6175 if (TREE_CODE (ttr) == ARRAY_TYPE)
6176 {
6177 ttr = strip_array_types (ttr);
6178 ttl = strip_array_types (ttl);
6179
6180 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6181 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6182 WARNING_FOR_QUALIFIERS (location, expr_loc,
6183 OPT_Wdiscarded_array_qualifiers,
6184 G_("passing argument %d of %qE discards "
6185 "%qv qualifier from pointer target type"),
6186 G_("assignment discards %qv qualifier "
6187 "from pointer target type"),
6188 G_("initialization discards %qv qualifier "
6189 "from pointer target type"),
6190 G_("return discards %qv qualifier from "
6191 "pointer target type"),
6192 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6193 }
6194 else if (pedantic
3e4093b6
RS
6195 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6196 ||
6197 (VOID_TYPE_P (ttr)
928c19bb 6198 && !null_pointer_constant
3e4093b6 6199 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6200 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6201 G_("ISO C forbids passing argument %d of "
6202 "%qE between function pointer "
6203 "and %<void *%>"),
6204 G_("ISO C forbids assignment between "
6205 "function pointer and %<void *%>"),
6206 G_("ISO C forbids initialization between "
6207 "function pointer and %<void *%>"),
6208 G_("ISO C forbids return between function "
6209 "pointer and %<void *%>"));
3e4093b6
RS
6210 /* Const and volatile mean something different for function types,
6211 so the usual warnings are not appropriate. */
6212 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6213 && TREE_CODE (ttl) != FUNCTION_TYPE)
6214 {
768952be
MU
6215 /* Don't warn about loss of qualifier for conversions from
6216 qualified void* to pointers to arrays with corresponding
6217 qualifier on the element type. */
6218 if (!pedantic)
6219 ttl = strip_array_types (ttl);
6220
267bac10
JM
6221 /* Assignments between atomic and non-atomic objects are OK. */
6222 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6223 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6224 {
768952be
MU
6225 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6226 OPT_Wdiscarded_qualifiers,
6227 G_("passing argument %d of %qE discards "
6228 "%qv qualifier from pointer target type"),
6229 G_("assignment discards %qv qualifier "
6230 "from pointer target type"),
6231 G_("initialization discards %qv qualifier "
6232 "from pointer target type"),
6233 G_("return discards %qv qualifier from "
6234 "pointer target type"),
6235 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6236 }
3e4093b6
RS
6237 /* If this is not a case of ignoring a mismatch in signedness,
6238 no warning. */
6239 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6240 || target_cmp)
3e4093b6
RS
6241 ;
6242 /* If there is a mismatch, do warn. */
f2fd3821 6243 else if (warn_pointer_sign)
768952be
MU
6244 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6245 G_("pointer targets in passing argument "
6246 "%d of %qE differ in signedness"),
6247 G_("pointer targets in assignment "
6248 "differ in signedness"),
6249 G_("pointer targets in initialization "
6250 "differ in signedness"),
6251 G_("pointer targets in return differ "
6252 "in signedness"));
3e4093b6
RS
6253 }
6254 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6255 && TREE_CODE (ttr) == FUNCTION_TYPE)
6256 {
6257 /* Because const and volatile on functions are restrictions
6258 that say the function will not do certain things,
6259 it is okay to use a const or volatile function
6260 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6261 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6262 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6263 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6264 OPT_Wdiscarded_qualifiers,
6265 G_("passing argument %d of %qE makes "
6266 "%q#v qualified function pointer "
6267 "from unqualified"),
6268 G_("assignment makes %q#v qualified function "
6269 "pointer from unqualified"),
6270 G_("initialization makes %q#v qualified "
6271 "function pointer from unqualified"),
6272 G_("return makes %q#v qualified function "
6273 "pointer from unqualified"),
6274 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6275 }
6276 }
6277 else
58393038
ZL
6278 /* Avoid warning about the volatile ObjC EH puts on decls. */
6279 if (!objc_ok)
768952be
MU
6280 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6281 OPT_Wincompatible_pointer_types,
6282 G_("passing argument %d of %qE from "
6283 "incompatible pointer type"),
6284 G_("assignment from incompatible pointer type"),
6285 G_("initialization from incompatible "
6286 "pointer type"),
6287 G_("return from incompatible pointer type"));
58393038 6288
3e4093b6
RS
6289 return convert (type, rhs);
6290 }
b494fd98
EB
6291 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6292 {
6dcc04b0
JM
6293 /* ??? This should not be an error when inlining calls to
6294 unprototyped functions. */
c2255bc4 6295 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6296 return error_mark_node;
6297 }
3e4093b6 6298 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6299 {
3e4093b6
RS
6300 /* An explicit constant 0 can convert to a pointer,
6301 or one that results from arithmetic, even including
6302 a cast to integer type. */
928c19bb 6303 if (!null_pointer_constant)
768952be
MU
6304 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6305 OPT_Wint_conversion,
6306 G_("passing argument %d of %qE makes "
6307 "pointer from integer without a cast"),
6308 G_("assignment makes pointer from integer "
6309 "without a cast"),
6310 G_("initialization makes pointer from "
6311 "integer without a cast"),
6312 G_("return makes pointer from integer "
6313 "without a cast"));
b3006337
EB
6314
6315 return convert (type, rhs);
400fbf9f 6316 }
3e4093b6 6317 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6318 {
768952be
MU
6319 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6320 OPT_Wint_conversion,
6321 G_("passing argument %d of %qE makes integer "
6322 "from pointer without a cast"),
6323 G_("assignment makes integer from pointer "
6324 "without a cast"),
6325 G_("initialization makes integer from pointer "
6326 "without a cast"),
6327 G_("return makes integer from pointer "
6328 "without a cast"));
3e4093b6
RS
6329 return convert (type, rhs);
6330 }
6331 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6332 {
6333 tree ret;
6334 bool save = in_late_binary_op;
6335 in_late_binary_op = true;
6336 ret = convert (type, rhs);
6337 in_late_binary_op = save;
6338 return ret;
6339 }
400fbf9f 6340
2ac2f164 6341 switch (errtype)
3e4093b6 6342 {
2ac2f164 6343 case ic_argpass:
8ffcdea8
MP
6344 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6345 rname);
a7da8b42 6346 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6347 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6348 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6349 break;
6350 case ic_assign:
c2255bc4
AH
6351 error_at (location, "incompatible types when assigning to type %qT from "
6352 "type %qT", type, rhstype);
2ac2f164
JM
6353 break;
6354 case ic_init:
c2255bc4 6355 error_at (location,
8ffcdea8 6356 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6357 type, rhstype);
2ac2f164
JM
6358 break;
6359 case ic_return:
c2255bc4 6360 error_at (location,
8ffcdea8 6361 "incompatible types when returning type %qT but %qT was "
c2255bc4 6362 "expected", rhstype, type);
2ac2f164
JM
6363 break;
6364 default:
6365 gcc_unreachable ();
400fbf9f 6366 }
53b01f59 6367
3e4093b6
RS
6368 return error_mark_node;
6369}
3e4093b6
RS
6370\f
6371/* If VALUE is a compound expr all of whose expressions are constant, then
6372 return its value. Otherwise, return error_mark_node.
15b732b2 6373
3e4093b6
RS
6374 This is for handling COMPOUND_EXPRs as initializer elements
6375 which is allowed with a warning when -pedantic is specified. */
15b732b2 6376
3e4093b6
RS
6377static tree
6378valid_compound_expr_initializer (tree value, tree endtype)
6379{
6380 if (TREE_CODE (value) == COMPOUND_EXPR)
6381 {
6382 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6383 == error_mark_node)
6384 return error_mark_node;
6385 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6386 endtype);
6387 }
116df786 6388 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6389 return error_mark_node;
6390 else
6391 return value;
15b732b2 6392}
400fbf9f 6393\f
3e4093b6
RS
6394/* Perform appropriate conversions on the initial value of a variable,
6395 store it in the declaration DECL,
6396 and print any error messages that are appropriate.
bbbbb16a 6397 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6398 If the init is invalid, store an ERROR_MARK.
6399
6400 INIT_LOC is the location of the initial value. */
400fbf9f 6401
3e4093b6 6402void
c2255bc4 6403store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6404{
3e4093b6 6405 tree value, type;
928c19bb 6406 bool npc = false;
400fbf9f 6407
3e4093b6 6408 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6409
3e4093b6
RS
6410 type = TREE_TYPE (decl);
6411 if (TREE_CODE (type) == ERROR_MARK)
6412 return;
400fbf9f 6413
3e4093b6 6414 /* Digest the specified initializer into an expression. */
400fbf9f 6415
928c19bb
JM
6416 if (init)
6417 npc = null_pointer_constant_p (init);
c2255bc4
AH
6418 value = digest_init (init_loc, type, init, origtype, npc,
6419 true, TREE_STATIC (decl));
400fbf9f 6420
3e4093b6 6421 /* Store the expression if valid; else report error. */
400fbf9f 6422
8400e75e 6423 if (!in_system_header_at (input_location)
3f75a254 6424 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6425 warning (OPT_Wtraditional, "traditional C rejects automatic "
6426 "aggregate initialization");
2f6e4e97 6427
dea63e49
JJ
6428 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6429 DECL_INITIAL (decl) = value;
400fbf9f 6430
3e4093b6
RS
6431 /* ANSI wants warnings about out-of-range constant initializers. */
6432 STRIP_TYPE_NOPS (value);
b8698a0f 6433 if (TREE_STATIC (decl))
c2658540 6434 constant_expression_warning (value);
400fbf9f 6435
3e4093b6
RS
6436 /* Check if we need to set array size from compound literal size. */
6437 if (TREE_CODE (type) == ARRAY_TYPE
6438 && TYPE_DOMAIN (type) == 0
6439 && value != error_mark_node)
400fbf9f 6440 {
3e4093b6
RS
6441 tree inside_init = init;
6442
ed248cf7 6443 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6444 inside_init = fold (inside_init);
6445
6446 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6447 {
8d9f82d5 6448 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6449
8d9f82d5 6450 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6451 {
6452 /* For int foo[] = (int [3]){1}; we need to set array size
6453 now since later on array initializer will be just the
6454 brace enclosed list of the compound literal. */
e30ecc5d 6455 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6456 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6457 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6458 layout_type (type);
8d9f82d5 6459 layout_decl (cldecl, 0);
e30ecc5d
JJ
6460 TREE_TYPE (decl)
6461 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6462 }
6463 }
400fbf9f 6464 }
3e4093b6
RS
6465}
6466\f
6467/* Methods for storing and printing names for error messages. */
400fbf9f 6468
3e4093b6
RS
6469/* Implement a spelling stack that allows components of a name to be pushed
6470 and popped. Each element on the stack is this structure. */
400fbf9f 6471
3e4093b6
RS
6472struct spelling
6473{
6474 int kind;
6475 union
400fbf9f 6476 {
a0f0ab9f 6477 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6478 const char *s;
6479 } u;
6480};
2f6e4e97 6481
3e4093b6
RS
6482#define SPELLING_STRING 1
6483#define SPELLING_MEMBER 2
6484#define SPELLING_BOUNDS 3
400fbf9f 6485
3e4093b6
RS
6486static struct spelling *spelling; /* Next stack element (unused). */
6487static struct spelling *spelling_base; /* Spelling stack base. */
6488static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6489
3e4093b6
RS
6490/* Macros to save and restore the spelling stack around push_... functions.
6491 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6492
3e4093b6
RS
6493#define SPELLING_DEPTH() (spelling - spelling_base)
6494#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6495
3e4093b6
RS
6496/* Push an element on the spelling stack with type KIND and assign VALUE
6497 to MEMBER. */
400fbf9f 6498
3e4093b6
RS
6499#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6500{ \
6501 int depth = SPELLING_DEPTH (); \
6502 \
6503 if (depth >= spelling_size) \
6504 { \
6505 spelling_size += 10; \
cca8ead2
BI
6506 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6507 spelling_size); \
3e4093b6
RS
6508 RESTORE_SPELLING_DEPTH (depth); \
6509 } \
6510 \
6511 spelling->kind = (KIND); \
6512 spelling->MEMBER = (VALUE); \
6513 spelling++; \
6514}
400fbf9f 6515
3e4093b6 6516/* Push STRING on the stack. Printed literally. */
400fbf9f 6517
3e4093b6
RS
6518static void
6519push_string (const char *string)
6520{
6521 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6522}
400fbf9f 6523
3e4093b6 6524/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6525
3e4093b6
RS
6526static void
6527push_member_name (tree decl)
6528{
6529 const char *const string
88388a52
JM
6530 = (DECL_NAME (decl)
6531 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6532 : _("<anonymous>"));
3e4093b6
RS
6533 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6534}
400fbf9f 6535
3e4093b6 6536/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6537
3e4093b6 6538static void
a0f0ab9f 6539push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6540{
6541 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6542}
bb58bec5 6543
3e4093b6 6544/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6545
3e4093b6
RS
6546static int
6547spelling_length (void)
6548{
6549 int size = 0;
6550 struct spelling *p;
400fbf9f 6551
3e4093b6
RS
6552 for (p = spelling_base; p < spelling; p++)
6553 {
6554 if (p->kind == SPELLING_BOUNDS)
6555 size += 25;
6556 else
6557 size += strlen (p->u.s) + 1;
6558 }
6559
6560 return size;
400fbf9f 6561}
400fbf9f 6562
3e4093b6 6563/* Print the spelling to BUFFER and return it. */
400fbf9f 6564
3e4093b6
RS
6565static char *
6566print_spelling (char *buffer)
400fbf9f 6567{
3e4093b6
RS
6568 char *d = buffer;
6569 struct spelling *p;
400fbf9f 6570
3e4093b6
RS
6571 for (p = spelling_base; p < spelling; p++)
6572 if (p->kind == SPELLING_BOUNDS)
6573 {
a0f0ab9f 6574 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6575 d += strlen (d);
6576 }
6577 else
6578 {
6579 const char *s;
6580 if (p->kind == SPELLING_MEMBER)
6581 *d++ = '.';
6582 for (s = p->u.s; (*d = *s++); d++)
6583 ;
6584 }
6585 *d++ = '\0';
6586 return buffer;
6587}
400fbf9f 6588
3e4093b6
RS
6589/* Digest the parser output INIT as an initializer for type TYPE.
6590 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6591
bbbbb16a
ILT
6592 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6593
928c19bb
JM
6594 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6595
916c5919
JM
6596 If INIT is a string constant, STRICT_STRING is true if it is
6597 unparenthesized or we should not warn here for it being parenthesized.
6598 For other types of INIT, STRICT_STRING is not used.
6599
c2255bc4
AH
6600 INIT_LOC is the location of the INIT.
6601
3e4093b6
RS
6602 REQUIRE_CONSTANT requests an error if non-constant initializers or
6603 elements are seen. */
7e842ef8 6604
3e4093b6 6605static tree
c2255bc4
AH
6606digest_init (location_t init_loc, tree type, tree init, tree origtype,
6607 bool null_pointer_constant, bool strict_string,
6608 int require_constant)
3e4093b6
RS
6609{
6610 enum tree_code code = TREE_CODE (type);
6611 tree inside_init = init;
8ce94e44 6612 tree semantic_type = NULL_TREE;
928c19bb 6613 bool maybe_const = true;
7e842ef8 6614
3e4093b6 6615 if (type == error_mark_node
f01da1a5 6616 || !init
7a0ca710 6617 || error_operand_p (init))
3e4093b6 6618 return error_mark_node;
7e842ef8 6619
ed248cf7 6620 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6621
8ce94e44
JM
6622 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6623 {
6624 semantic_type = TREE_TYPE (inside_init);
6625 inside_init = TREE_OPERAND (inside_init, 0);
6626 }
928c19bb
JM
6627 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6628 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6629
3e4093b6
RS
6630 /* Initialization of an array of chars from a string constant
6631 optionally enclosed in braces. */
7e842ef8 6632
197463ae
JM
6633 if (code == ARRAY_TYPE && inside_init
6634 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6635 {
267bac10
JM
6636 tree typ1
6637 = (TYPE_ATOMIC (TREE_TYPE (type))
6638 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6639 TYPE_QUAL_ATOMIC)
6640 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6641 /* Note that an array could be both an array of character type
6642 and an array of wchar_t if wchar_t is signed char or unsigned
6643 char. */
6644 bool char_array = (typ1 == char_type_node
6645 || typ1 == signed_char_type_node
6646 || typ1 == unsigned_char_type_node);
6647 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6648 bool char16_array = !!comptypes (typ1, char16_type_node);
6649 bool char32_array = !!comptypes (typ1, char32_type_node);
6650
6651 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6652 {
916c5919 6653 struct c_expr expr;
c466b2cd 6654 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6655 expr.value = inside_init;
6656 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6657 expr.original_type = NULL;
d033409e 6658 maybe_warn_string_init (init_loc, type, expr);
916c5919 6659
a45e580b 6660 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6661 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6662 "initialization of a flexible array member");
6663
3e4093b6 6664 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6665 TYPE_MAIN_VARIANT (type)))
3e4093b6 6666 return inside_init;
7e842ef8 6667
c466b2cd 6668 if (char_array)
3e4093b6 6669 {
c466b2cd
KVH
6670 if (typ2 != char_type_node)
6671 {
ea58ef42
MP
6672 error_init (init_loc, "char-array initialized from wide "
6673 "string");
c466b2cd
KVH
6674 return error_mark_node;
6675 }
3e4093b6 6676 }
c466b2cd 6677 else
3e4093b6 6678 {
c466b2cd
KVH
6679 if (typ2 == char_type_node)
6680 {
ea58ef42
MP
6681 error_init (init_loc, "wide character array initialized "
6682 "from non-wide string");
c466b2cd
KVH
6683 return error_mark_node;
6684 }
6685 else if (!comptypes(typ1, typ2))
6686 {
ea58ef42
MP
6687 error_init (init_loc, "wide character array initialized "
6688 "from incompatible wide string");
c466b2cd
KVH
6689 return error_mark_node;
6690 }
7e842ef8 6691 }
2f6e4e97 6692
3e4093b6
RS
6693 TREE_TYPE (inside_init) = type;
6694 if (TYPE_DOMAIN (type) != 0
6695 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6696 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6697 {
6698 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6699
c466b2cd 6700 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6701 because it's ok to ignore the terminating null char
6702 that is counted in the length of the constant. */
5eb4df45
ILT
6703 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6704 (len
6705 - (TYPE_PRECISION (typ1)
6706 / BITS_PER_UNIT))))
6707 pedwarn_init (init_loc, 0,
6708 ("initializer-string for array of chars "
6709 "is too long"));
6710 else if (warn_cxx_compat
6711 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6712 warning_at (init_loc, OPT_Wc___compat,
6713 ("initializer-string for array chars "
6714 "is too long for C++"));
6715 }
7e842ef8 6716
3e4093b6 6717 return inside_init;
7e842ef8 6718 }
197463ae
JM
6719 else if (INTEGRAL_TYPE_P (typ1))
6720 {
ea58ef42 6721 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6722 "from string constant");
6723 return error_mark_node;
6724 }
7e842ef8
PE
6725 }
6726
3e4093b6
RS
6727 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6728 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6729 below and handle as a constructor. */
e89be13b 6730 if (code == VECTOR_TYPE
31521951 6731 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 6732 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6733 && TREE_CONSTANT (inside_init))
6734 {
6735 if (TREE_CODE (inside_init) == VECTOR_CST
6736 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6737 TYPE_MAIN_VARIANT (type)))
6738 return inside_init;
6739
6740 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6741 {
4038c495
GB
6742 unsigned HOST_WIDE_INT ix;
6743 tree value;
6744 bool constant_p = true;
e89be13b
JJ
6745
6746 /* Iterate through elements and check if all constructor
6747 elements are *_CSTs. */
4038c495
GB
6748 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6749 if (!CONSTANT_CLASS_P (value))
6750 {
6751 constant_p = false;
6752 break;
6753 }
e89be13b 6754
4038c495
GB
6755 if (constant_p)
6756 return build_vector_from_ctor (type,
6757 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6758 }
6759 }
6035d635 6760
ca085fd7
MLI
6761 if (warn_sequence_point)
6762 verify_sequence_points (inside_init);
6763
3e4093b6
RS
6764 /* Any type can be initialized
6765 from an expression of the same type, optionally with braces. */
400fbf9f 6766
3e4093b6
RS
6767 if (inside_init && TREE_TYPE (inside_init) != 0
6768 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6769 TYPE_MAIN_VARIANT (type))
3e4093b6 6770 || (code == ARRAY_TYPE
132da1a5 6771 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6772 || (code == VECTOR_TYPE
132da1a5 6773 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6774 || (code == POINTER_TYPE
3897f229 6775 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6776 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6777 TREE_TYPE (type)))))
3e4093b6
RS
6778 {
6779 if (code == POINTER_TYPE)
b494fd98 6780 {
b494fd98
EB
6781 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6782 {
f2a71bbc
JM
6783 if (TREE_CODE (inside_init) == STRING_CST
6784 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6785 inside_init = array_to_pointer_conversion
6786 (init_loc, inside_init);
f2a71bbc
JM
6787 else
6788 {
ea58ef42 6789 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6790 return error_mark_node;
6791 }
b494fd98 6792 }
f2a71bbc 6793 }
b494fd98 6794
bae39a73
NS
6795 if (code == VECTOR_TYPE)
6796 /* Although the types are compatible, we may require a
6797 conversion. */
6798 inside_init = convert (type, inside_init);
3e4093b6 6799
ca58211b 6800 if (require_constant
3e4093b6 6801 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6802 {
3e4093b6
RS
6803 /* As an extension, allow initializing objects with static storage
6804 duration with compound literals (which are then treated just as
ca58211b
PB
6805 the brace enclosed list they contain). Also allow this for
6806 vectors, as we can only assign them with compound literals. */
7278465e
MP
6807 if (flag_isoc99 && code != VECTOR_TYPE)
6808 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6809 "is not constant");
3e4093b6
RS
6810 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6811 inside_init = DECL_INITIAL (decl);
400fbf9f 6812 }
3e4093b6
RS
6813
6814 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6815 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6816 {
ea58ef42
MP
6817 error_init (init_loc, "array initialized from non-constant array "
6818 "expression");
3e4093b6 6819 return error_mark_node;
400fbf9f 6820 }
400fbf9f 6821
c1771a20 6822 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6823 -pedantic-errors is specified. In the later case, we always want
6824 an error. In the former case, we simply want a warning. */
6825 if (require_constant && pedantic
6826 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6827 {
6828 inside_init
6829 = valid_compound_expr_initializer (inside_init,
6830 TREE_TYPE (inside_init));
6831 if (inside_init == error_mark_node)
ea58ef42 6832 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6833 else
c1771a20 6834 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6835 "initializer element is not constant");
3e4093b6
RS
6836 if (flag_pedantic_errors)
6837 inside_init = error_mark_node;
6838 }
6839 else if (require_constant
116df786
RH
6840 && !initializer_constant_valid_p (inside_init,
6841 TREE_TYPE (inside_init)))
3e4093b6 6842 {
ea58ef42 6843 error_init (init_loc, "initializer element is not constant");
3e4093b6 6844 inside_init = error_mark_node;
8b40563c 6845 }
928c19bb 6846 else if (require_constant && !maybe_const)
3aa3c9fc 6847 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 6848 "initializer element is not a constant expression");
f735a153 6849
90137d8f 6850 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6851 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6852 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6853 type, inside_init, origtype,
bbbbb16a 6854 ic_init, null_pointer_constant,
928c19bb 6855 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6856 return inside_init;
6857 }
f735a153 6858
3e4093b6 6859 /* Handle scalar types, including conversions. */
400fbf9f 6860
ab22c1fa
CF
6861 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6862 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6863 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6864 {
f2a71bbc
JM
6865 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6866 && (TREE_CODE (init) == STRING_CST
6867 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6868 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6869 if (semantic_type)
6870 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6871 inside_init);
3e4093b6 6872 inside_init
68fca595
MP
6873 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6874 inside_init, origtype, ic_init,
6875 null_pointer_constant, NULL_TREE, NULL_TREE,
6876 0);
2f6e4e97 6877
3274deff
JW
6878 /* Check to see if we have already given an error message. */
6879 if (inside_init == error_mark_node)
6880 ;
3f75a254 6881 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6882 {
ea58ef42 6883 error_init (init_loc, "initializer element is not constant");
3e4093b6 6884 inside_init = error_mark_node;
400fbf9f 6885 }
3e4093b6 6886 else if (require_constant
116df786
RH
6887 && !initializer_constant_valid_p (inside_init,
6888 TREE_TYPE (inside_init)))
400fbf9f 6889 {
ea58ef42
MP
6890 error_init (init_loc, "initializer element is not computable at "
6891 "load time");
3e4093b6 6892 inside_init = error_mark_node;
400fbf9f 6893 }
928c19bb 6894 else if (require_constant && !maybe_const)
c2255bc4 6895 pedwarn_init (init_loc, 0,
928c19bb 6896 "initializer element is not a constant expression");
3e4093b6
RS
6897
6898 return inside_init;
400fbf9f 6899 }
d9fc6069 6900
3e4093b6 6901 /* Come here only for records and arrays. */
d9fc6069 6902
3e4093b6 6903 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6904 {
ea58ef42 6905 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6906 return error_mark_node;
d9fc6069 6907 }
3e4093b6 6908
ea58ef42 6909 error_init (init_loc, "invalid initializer");
3e4093b6 6910 return error_mark_node;
d9fc6069 6911}
400fbf9f 6912\f
3e4093b6 6913/* Handle initializers that use braces. */
400fbf9f 6914
3e4093b6
RS
6915/* Type of object we are accumulating a constructor for.
6916 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6917static tree constructor_type;
400fbf9f 6918
3e4093b6
RS
6919/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6920 left to fill. */
6921static tree constructor_fields;
400fbf9f 6922
3e4093b6
RS
6923/* For an ARRAY_TYPE, this is the specified index
6924 at which to store the next element we get. */
6925static tree constructor_index;
400fbf9f 6926
3e4093b6
RS
6927/* For an ARRAY_TYPE, this is the maximum index. */
6928static tree constructor_max_index;
400fbf9f 6929
3e4093b6
RS
6930/* For a RECORD_TYPE, this is the first field not yet written out. */
6931static tree constructor_unfilled_fields;
400fbf9f 6932
3e4093b6
RS
6933/* For an ARRAY_TYPE, this is the index of the first element
6934 not yet written out. */
6935static tree constructor_unfilled_index;
895ea614 6936
3e4093b6
RS
6937/* In a RECORD_TYPE, the byte index of the next consecutive field.
6938 This is so we can generate gaps between fields, when appropriate. */
6939static tree constructor_bit_index;
10d5caec 6940
3e4093b6
RS
6941/* If we are saving up the elements rather than allocating them,
6942 this is the list of elements so far (in reverse order,
6943 most recent first). */
9771b263 6944static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6945
3e4093b6
RS
6946/* 1 if constructor should be incrementally stored into a constructor chain,
6947 0 if all the elements should be kept in AVL tree. */
6948static int constructor_incremental;
ad47f1e5 6949
3e4093b6
RS
6950/* 1 if so far this constructor's elements are all compile-time constants. */
6951static int constructor_constant;
ad47f1e5 6952
3e4093b6
RS
6953/* 1 if so far this constructor's elements are all valid address constants. */
6954static int constructor_simple;
ad47f1e5 6955
928c19bb
JM
6956/* 1 if this constructor has an element that cannot be part of a
6957 constant expression. */
6958static int constructor_nonconst;
6959
3e4093b6
RS
6960/* 1 if this constructor is erroneous so far. */
6961static int constructor_erroneous;
d45cf215 6962
9bac5cbb
G
6963/* 1 if this constructor is the universal zero initializer { 0 }. */
6964static int constructor_zeroinit;
6965
3e4093b6
RS
6966/* Structure for managing pending initializer elements, organized as an
6967 AVL tree. */
d45cf215 6968
3e4093b6 6969struct init_node
d45cf215 6970{
3e4093b6
RS
6971 struct init_node *left, *right;
6972 struct init_node *parent;
6973 int balance;
6974 tree purpose;
6975 tree value;
bbbbb16a 6976 tree origtype;
d45cf215
RS
6977};
6978
3e4093b6
RS
6979/* Tree of pending elements at this constructor level.
6980 These are elements encountered out of order
6981 which belong at places we haven't reached yet in actually
6982 writing the output.
6983 Will never hold tree nodes across GC runs. */
6984static struct init_node *constructor_pending_elts;
d45cf215 6985
3e4093b6
RS
6986/* The SPELLING_DEPTH of this constructor. */
6987static int constructor_depth;
d45cf215 6988
3e4093b6
RS
6989/* DECL node for which an initializer is being read.
6990 0 means we are reading a constructor expression
6991 such as (struct foo) {...}. */
6992static tree constructor_decl;
d45cf215 6993
3e4093b6
RS
6994/* Nonzero if this is an initializer for a top-level decl. */
6995static int constructor_top_level;
d45cf215 6996
3e4093b6
RS
6997/* Nonzero if there were any member designators in this initializer. */
6998static int constructor_designated;
d45cf215 6999
3e4093b6
RS
7000/* Nesting depth of designator list. */
7001static int designator_depth;
d45cf215 7002
3e4093b6 7003/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7004static int designator_erroneous;
d45cf215 7005
3e4093b6
RS
7006\f
7007/* This stack has a level for each implicit or explicit level of
7008 structuring in the initializer, including the outermost one. It
7009 saves the values of most of the variables above. */
d45cf215 7010
3e4093b6
RS
7011struct constructor_range_stack;
7012
7013struct constructor_stack
d45cf215 7014{
3e4093b6
RS
7015 struct constructor_stack *next;
7016 tree type;
7017 tree fields;
7018 tree index;
7019 tree max_index;
7020 tree unfilled_index;
7021 tree unfilled_fields;
7022 tree bit_index;
9771b263 7023 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7024 struct init_node *pending_elts;
7025 int offset;
7026 int depth;
916c5919 7027 /* If value nonzero, this value should replace the entire
3e4093b6 7028 constructor at this level. */
916c5919 7029 struct c_expr replacement_value;
3e4093b6
RS
7030 struct constructor_range_stack *range_stack;
7031 char constant;
7032 char simple;
928c19bb 7033 char nonconst;
3e4093b6
RS
7034 char implicit;
7035 char erroneous;
7036 char outer;
7037 char incremental;
7038 char designated;
976d5a22 7039 int designator_depth;
3e4093b6 7040};
d45cf215 7041
802415d1 7042static struct constructor_stack *constructor_stack;
d45cf215 7043
3e4093b6
RS
7044/* This stack represents designators from some range designator up to
7045 the last designator in the list. */
d45cf215 7046
3e4093b6
RS
7047struct constructor_range_stack
7048{
7049 struct constructor_range_stack *next, *prev;
7050 struct constructor_stack *stack;
7051 tree range_start;
7052 tree index;
7053 tree range_end;
7054 tree fields;
7055};
d45cf215 7056
802415d1 7057static struct constructor_range_stack *constructor_range_stack;
d45cf215 7058
3e4093b6
RS
7059/* This stack records separate initializers that are nested.
7060 Nested initializers can't happen in ANSI C, but GNU C allows them
7061 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7062
3e4093b6 7063struct initializer_stack
d45cf215 7064{
3e4093b6
RS
7065 struct initializer_stack *next;
7066 tree decl;
3e4093b6
RS
7067 struct constructor_stack *constructor_stack;
7068 struct constructor_range_stack *constructor_range_stack;
9771b263 7069 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7070 struct spelling *spelling;
7071 struct spelling *spelling_base;
7072 int spelling_size;
7073 char top_level;
7074 char require_constant_value;
7075 char require_constant_elements;
7076};
d45cf215 7077
802415d1 7078static struct initializer_stack *initializer_stack;
3e4093b6
RS
7079\f
7080/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7081
7082void
a396f8ae 7083start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7084{
3e4093b6 7085 const char *locus;
cceb1885 7086 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7087
3e4093b6 7088 p->decl = constructor_decl;
3e4093b6
RS
7089 p->require_constant_value = require_constant_value;
7090 p->require_constant_elements = require_constant_elements;
7091 p->constructor_stack = constructor_stack;
7092 p->constructor_range_stack = constructor_range_stack;
7093 p->elements = constructor_elements;
7094 p->spelling = spelling;
7095 p->spelling_base = spelling_base;
7096 p->spelling_size = spelling_size;
7097 p->top_level = constructor_top_level;
7098 p->next = initializer_stack;
7099 initializer_stack = p;
400fbf9f 7100
3e4093b6 7101 constructor_decl = decl;
3e4093b6
RS
7102 constructor_designated = 0;
7103 constructor_top_level = top_level;
400fbf9f 7104
6f17bbcf 7105 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7106 {
7107 require_constant_value = TREE_STATIC (decl);
7108 require_constant_elements
7109 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7110 /* For a scalar, you can always use any value to initialize,
7111 even within braces. */
296a8c2f 7112 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7113 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7114 }
7115 else
7116 {
7117 require_constant_value = 0;
7118 require_constant_elements = 0;
88388a52 7119 locus = _("(anonymous)");
3e4093b6 7120 }
b71c7f8a 7121
3e4093b6
RS
7122 constructor_stack = 0;
7123 constructor_range_stack = 0;
b71c7f8a 7124
9bac5cbb 7125 found_missing_braces = 0;
3e4093b6
RS
7126
7127 spelling_base = 0;
7128 spelling_size = 0;
7129 RESTORE_SPELLING_DEPTH (0);
7130
7131 if (locus)
7132 push_string (locus);
7133}
7134
7135void
7136finish_init (void)
b71c7f8a 7137{
3e4093b6 7138 struct initializer_stack *p = initializer_stack;
b71c7f8a 7139
3e4093b6
RS
7140 /* Free the whole constructor stack of this initializer. */
7141 while (constructor_stack)
7142 {
7143 struct constructor_stack *q = constructor_stack;
7144 constructor_stack = q->next;
7145 free (q);
7146 }
7147
366de0ce 7148 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7149
7150 /* Pop back to the data of the outer initializer (if any). */
36579663 7151 free (spelling_base);
3aeb3655 7152
3e4093b6 7153 constructor_decl = p->decl;
3e4093b6
RS
7154 require_constant_value = p->require_constant_value;
7155 require_constant_elements = p->require_constant_elements;
7156 constructor_stack = p->constructor_stack;
7157 constructor_range_stack = p->constructor_range_stack;
7158 constructor_elements = p->elements;
7159 spelling = p->spelling;
7160 spelling_base = p->spelling_base;
7161 spelling_size = p->spelling_size;
7162 constructor_top_level = p->top_level;
7163 initializer_stack = p->next;
7164 free (p);
b71c7f8a 7165}
400fbf9f 7166\f
3e4093b6
RS
7167/* Call here when we see the initializer is surrounded by braces.
7168 This is instead of a call to push_init_level;
7169 it is matched by a call to pop_init_level.
400fbf9f 7170
3e4093b6
RS
7171 TYPE is the type to initialize, for a constructor expression.
7172 For an initializer for a decl, TYPE is zero. */
400fbf9f 7173
3e4093b6
RS
7174void
7175really_start_incremental_init (tree type)
400fbf9f 7176{
5d038c4c 7177 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7178
3e4093b6
RS
7179 if (type == 0)
7180 type = TREE_TYPE (constructor_decl);
400fbf9f 7181
31521951 7182 if (VECTOR_TYPE_P (type)
b6fc2cdb 7183 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7184 error ("opaque vector types cannot be initialized");
400fbf9f 7185
3e4093b6
RS
7186 p->type = constructor_type;
7187 p->fields = constructor_fields;
7188 p->index = constructor_index;
7189 p->max_index = constructor_max_index;
7190 p->unfilled_index = constructor_unfilled_index;
7191 p->unfilled_fields = constructor_unfilled_fields;
7192 p->bit_index = constructor_bit_index;
7193 p->elements = constructor_elements;
7194 p->constant = constructor_constant;
7195 p->simple = constructor_simple;
928c19bb 7196 p->nonconst = constructor_nonconst;
3e4093b6
RS
7197 p->erroneous = constructor_erroneous;
7198 p->pending_elts = constructor_pending_elts;
7199 p->depth = constructor_depth;
916c5919
JM
7200 p->replacement_value.value = 0;
7201 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7202 p->replacement_value.original_type = NULL;
3e4093b6
RS
7203 p->implicit = 0;
7204 p->range_stack = 0;
7205 p->outer = 0;
7206 p->incremental = constructor_incremental;
7207 p->designated = constructor_designated;
976d5a22 7208 p->designator_depth = designator_depth;
3e4093b6
RS
7209 p->next = 0;
7210 constructor_stack = p;
b13aca19 7211
3e4093b6
RS
7212 constructor_constant = 1;
7213 constructor_simple = 1;
928c19bb 7214 constructor_nonconst = 0;
3e4093b6 7215 constructor_depth = SPELLING_DEPTH ();
9771b263 7216 constructor_elements = NULL;
3e4093b6
RS
7217 constructor_pending_elts = 0;
7218 constructor_type = type;
7219 constructor_incremental = 1;
7220 constructor_designated = 0;
9bac5cbb 7221 constructor_zeroinit = 1;
3e4093b6 7222 designator_depth = 0;
b06df647 7223 designator_erroneous = 0;
400fbf9f 7224
3e4093b6
RS
7225 if (TREE_CODE (constructor_type) == RECORD_TYPE
7226 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7227 {
3e4093b6
RS
7228 constructor_fields = TYPE_FIELDS (constructor_type);
7229 /* Skip any nameless bit fields at the beginning. */
7230 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7231 && DECL_NAME (constructor_fields) == 0)
910ad8de 7232 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7233
3e4093b6
RS
7234 constructor_unfilled_fields = constructor_fields;
7235 constructor_bit_index = bitsize_zero_node;
400fbf9f 7236 }
3e4093b6
RS
7237 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7238 {
7239 if (TYPE_DOMAIN (constructor_type))
7240 {
7241 constructor_max_index
7242 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7243
3e4093b6
RS
7244 /* Detect non-empty initializations of zero-length arrays. */
7245 if (constructor_max_index == NULL_TREE
7246 && TYPE_SIZE (constructor_type))
9a9d280e 7247 constructor_max_index = integer_minus_one_node;
400fbf9f 7248
3e4093b6
RS
7249 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7250 to initialize VLAs will cause a proper error; avoid tree
7251 checking errors as well by setting a safe value. */
7252 if (constructor_max_index
7253 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7254 constructor_max_index = integer_minus_one_node;
59c83dbf 7255
3e4093b6
RS
7256 constructor_index
7257 = convert (bitsizetype,
7258 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7259 }
3e4093b6 7260 else
493179da
JM
7261 {
7262 constructor_index = bitsize_zero_node;
7263 constructor_max_index = NULL_TREE;
7264 }
59c83dbf 7265
3e4093b6
RS
7266 constructor_unfilled_index = constructor_index;
7267 }
31521951 7268 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7269 {
7270 /* Vectors are like simple fixed-size arrays. */
7271 constructor_max_index =
c62c040f 7272 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7273 constructor_index = bitsize_zero_node;
3e4093b6
RS
7274 constructor_unfilled_index = constructor_index;
7275 }
7276 else
7277 {
7278 /* Handle the case of int x = {5}; */
7279 constructor_fields = constructor_type;
7280 constructor_unfilled_fields = constructor_type;
7281 }
7282}
7283\f
7284/* Push down into a subobject, for initialization.
7285 If this is for an explicit set of braces, IMPLICIT is 0.
7286 If it is because the next element belongs at a lower level,
7287 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7288
3e4093b6 7289void
ea58ef42
MP
7290push_init_level (location_t loc, int implicit,
7291 struct obstack *braced_init_obstack)
3e4093b6
RS
7292{
7293 struct constructor_stack *p;
7294 tree value = NULL_TREE;
400fbf9f 7295
3e4093b6 7296 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7297 pop them now. If implicit == 1, this will have been done in
7298 process_init_element; do not repeat it here because in the case
7299 of excess initializers for an empty aggregate this leads to an
7300 infinite cycle of popping a level and immediately recreating
7301 it. */
7302 if (implicit != 1)
3e4093b6 7303 {
472d98b4
JM
7304 while (constructor_stack->implicit)
7305 {
7306 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7307 || TREE_CODE (constructor_type) == UNION_TYPE)
7308 && constructor_fields == 0)
34cf811f 7309 process_init_element (input_location,
ea58ef42 7310 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7311 true, braced_init_obstack);
472d98b4
JM
7312 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7313 && constructor_max_index
7314 && tree_int_cst_lt (constructor_max_index,
7315 constructor_index))
34cf811f 7316 process_init_element (input_location,
ea58ef42 7317 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7318 true, braced_init_obstack);
472d98b4
JM
7319 else
7320 break;
7321 }
3e4093b6 7322 }
400fbf9f 7323
3e4093b6
RS
7324 /* Unless this is an explicit brace, we need to preserve previous
7325 content if any. */
7326 if (implicit)
7327 {
7328 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7329 || TREE_CODE (constructor_type) == UNION_TYPE)
7330 && constructor_fields)
a1e3b3d9 7331 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7332 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7333 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7334 }
7335
5d038c4c 7336 p = XNEW (struct constructor_stack);
3e4093b6
RS
7337 p->type = constructor_type;
7338 p->fields = constructor_fields;
7339 p->index = constructor_index;
7340 p->max_index = constructor_max_index;
7341 p->unfilled_index = constructor_unfilled_index;
7342 p->unfilled_fields = constructor_unfilled_fields;
7343 p->bit_index = constructor_bit_index;
7344 p->elements = constructor_elements;
7345 p->constant = constructor_constant;
7346 p->simple = constructor_simple;
928c19bb 7347 p->nonconst = constructor_nonconst;
3e4093b6
RS
7348 p->erroneous = constructor_erroneous;
7349 p->pending_elts = constructor_pending_elts;
7350 p->depth = constructor_depth;
916c5919
JM
7351 p->replacement_value.value = 0;
7352 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7353 p->replacement_value.original_type = NULL;
3e4093b6
RS
7354 p->implicit = implicit;
7355 p->outer = 0;
7356 p->incremental = constructor_incremental;
7357 p->designated = constructor_designated;
976d5a22 7358 p->designator_depth = designator_depth;
3e4093b6
RS
7359 p->next = constructor_stack;
7360 p->range_stack = 0;
7361 constructor_stack = p;
400fbf9f 7362
3e4093b6
RS
7363 constructor_constant = 1;
7364 constructor_simple = 1;
928c19bb 7365 constructor_nonconst = 0;
3e4093b6 7366 constructor_depth = SPELLING_DEPTH ();
9771b263 7367 constructor_elements = NULL;
3e4093b6
RS
7368 constructor_incremental = 1;
7369 constructor_designated = 0;
7370 constructor_pending_elts = 0;
7371 if (!implicit)
400fbf9f 7372 {
3e4093b6
RS
7373 p->range_stack = constructor_range_stack;
7374 constructor_range_stack = 0;
7375 designator_depth = 0;
b06df647 7376 designator_erroneous = 0;
3e4093b6 7377 }
400fbf9f 7378
3e4093b6
RS
7379 /* Don't die if an entire brace-pair level is superfluous
7380 in the containing level. */
7381 if (constructor_type == 0)
7382 ;
7383 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7384 || TREE_CODE (constructor_type) == UNION_TYPE)
7385 {
7386 /* Don't die if there are extra init elts at the end. */
7387 if (constructor_fields == 0)
7388 constructor_type = 0;
7389 else
400fbf9f 7390 {
3e4093b6
RS
7391 constructor_type = TREE_TYPE (constructor_fields);
7392 push_member_name (constructor_fields);
7393 constructor_depth++;
400fbf9f 7394 }
6a358dcb
MP
7395 /* If upper initializer is designated, then mark this as
7396 designated too to prevent bogus warnings. */
7397 constructor_designated = p->designated;
3e4093b6
RS
7398 }
7399 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7400 {
7401 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7402 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7403 constructor_depth++;
400fbf9f
JW
7404 }
7405
3e4093b6 7406 if (constructor_type == 0)
400fbf9f 7407 {
ea58ef42 7408 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7409 constructor_fields = 0;
7410 constructor_unfilled_fields = 0;
7411 return;
400fbf9f
JW
7412 }
7413
3e4093b6
RS
7414 if (value && TREE_CODE (value) == CONSTRUCTOR)
7415 {
7416 constructor_constant = TREE_CONSTANT (value);
7417 constructor_simple = TREE_STATIC (value);
928c19bb 7418 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7419 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7420 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7421 && (TREE_CODE (constructor_type) == RECORD_TYPE
7422 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7423 set_nonincremental_init (braced_init_obstack);
3e4093b6 7424 }
400fbf9f 7425
9bac5cbb
G
7426 if (implicit == 1)
7427 found_missing_braces = 1;
400fbf9f 7428
3e4093b6
RS
7429 if (TREE_CODE (constructor_type) == RECORD_TYPE
7430 || TREE_CODE (constructor_type) == UNION_TYPE)
7431 {
7432 constructor_fields = TYPE_FIELDS (constructor_type);
7433 /* Skip any nameless bit fields at the beginning. */
7434 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7435 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7436 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7437
3e4093b6
RS
7438 constructor_unfilled_fields = constructor_fields;
7439 constructor_bit_index = bitsize_zero_node;
7440 }
31521951 7441 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7442 {
7443 /* Vectors are like simple fixed-size arrays. */
7444 constructor_max_index =
c62c040f
RG
7445 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7446 constructor_index = bitsize_int (0);
3e4093b6
RS
7447 constructor_unfilled_index = constructor_index;
7448 }
7449 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7450 {
7451 if (TYPE_DOMAIN (constructor_type))
7452 {
7453 constructor_max_index
7454 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7455
3e4093b6
RS
7456 /* Detect non-empty initializations of zero-length arrays. */
7457 if (constructor_max_index == NULL_TREE
7458 && TYPE_SIZE (constructor_type))
9a9d280e 7459 constructor_max_index = integer_minus_one_node;
de520661 7460
3e4093b6
RS
7461 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7462 to initialize VLAs will cause a proper error; avoid tree
7463 checking errors as well by setting a safe value. */
7464 if (constructor_max_index
7465 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7466 constructor_max_index = integer_minus_one_node;
b62acd60 7467
3e4093b6
RS
7468 constructor_index
7469 = convert (bitsizetype,
7470 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7471 }
7472 else
7473 constructor_index = bitsize_zero_node;
de520661 7474
3e4093b6
RS
7475 constructor_unfilled_index = constructor_index;
7476 if (value && TREE_CODE (value) == STRING_CST)
7477 {
7478 /* We need to split the char/wchar array into individual
7479 characters, so that we don't have to special case it
7480 everywhere. */
a1e3b3d9 7481 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7482 }
7483 }
7484 else
7485 {
b4519d39 7486 if (constructor_type != error_mark_node)
96b40f8d 7487 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7488 constructor_fields = constructor_type;
7489 constructor_unfilled_fields = constructor_type;
7490 }
7491}
8b6a5902 7492
3e4093b6 7493/* At the end of an implicit or explicit brace level,
916c5919
JM
7494 finish up that level of constructor. If a single expression
7495 with redundant braces initialized that level, return the
7496 c_expr structure for that expression. Otherwise, the original_code
7497 element is set to ERROR_MARK.
7498 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7499 from inner levels (process_init_element ignores that),
916c5919 7500 but return error_mark_node as the value from the outermost level
3e4093b6 7501 (that's what we want to put in DECL_INITIAL).
916c5919 7502 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7503
916c5919 7504struct c_expr
ea58ef42
MP
7505pop_init_level (location_t loc, int implicit,
7506 struct obstack *braced_init_obstack)
3e4093b6
RS
7507{
7508 struct constructor_stack *p;
916c5919
JM
7509 struct c_expr ret;
7510 ret.value = 0;
7511 ret.original_code = ERROR_MARK;
6866c6e8 7512 ret.original_type = NULL;
de520661 7513
3e4093b6
RS
7514 if (implicit == 0)
7515 {
7516 /* When we come to an explicit close brace,
7517 pop any inner levels that didn't have explicit braces. */
7518 while (constructor_stack->implicit)
34cf811f 7519 process_init_element (input_location,
ea58ef42 7520 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7521 true, braced_init_obstack);
366de0ce 7522 gcc_assert (!constructor_range_stack);
3e4093b6 7523 }
e5e809f4 7524
0066ef9c
RH
7525 /* Now output all pending elements. */
7526 constructor_incremental = 1;
a1e3b3d9 7527 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7528
3e4093b6 7529 p = constructor_stack;
e5e809f4 7530
3e4093b6
RS
7531 /* Error for initializing a flexible array member, or a zero-length
7532 array member in an inappropriate context. */
7533 if (constructor_type && constructor_fields
7534 && TREE_CODE (constructor_type) == ARRAY_TYPE
7535 && TYPE_DOMAIN (constructor_type)
3f75a254 7536 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7537 {
7538 /* Silently discard empty initializations. The parser will
7539 already have pedwarned for empty brackets. */
7540 if (integer_zerop (constructor_unfilled_index))
7541 constructor_type = NULL_TREE;
366de0ce 7542 else
3e4093b6 7543 {
366de0ce 7544 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7545
3e4093b6 7546 if (constructor_depth > 2)
ea58ef42 7547 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7548 else
d033409e 7549 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7550 "initialization of a flexible array member");
de520661 7551
3e4093b6
RS
7552 /* We have already issued an error message for the existence
7553 of a flexible array member not at the end of the structure.
535a42b1 7554 Discard the initializer so that we do not die later. */
910ad8de 7555 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7556 constructor_type = NULL_TREE;
7557 }
3e4093b6 7558 }
de520661 7559
7b33f0c8
MP
7560 switch (vec_safe_length (constructor_elements))
7561 {
7562 case 0:
7563 /* Initialization with { } counts as zeroinit. */
7564 constructor_zeroinit = 1;
7565 break;
7566 case 1:
7567 /* This might be zeroinit as well. */
7568 if (integer_zerop ((*constructor_elements)[0].value))
7569 constructor_zeroinit = 1;
7570 break;
7571 default:
7572 /* If the constructor has more than one element, it can't be { 0 }. */
7573 constructor_zeroinit = 0;
7574 break;
7575 }
9bac5cbb
G
7576
7577 /* Warn when some structs are initialized with direct aggregation. */
7578 if (!implicit && found_missing_braces && warn_missing_braces
7579 && !constructor_zeroinit)
7b33f0c8
MP
7580 warning_init (loc, OPT_Wmissing_braces,
7581 "missing braces around initializer");
9bac5cbb 7582
3e4093b6 7583 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7584 if (warn_missing_field_initializers
3e4093b6
RS
7585 && constructor_type
7586 && TREE_CODE (constructor_type) == RECORD_TYPE
7587 && constructor_unfilled_fields)
7588 {
7589 /* Do not warn for flexible array members or zero-length arrays. */
7590 while (constructor_unfilled_fields
3f75a254 7591 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7592 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7593 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7594
49819fef
AM
7595 if (constructor_unfilled_fields
7596 /* Do not warn if this level of the initializer uses member
7597 designators; it is likely to be deliberate. */
7598 && !constructor_designated
84937de2 7599 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7600 && !constructor_zeroinit)
3e4093b6 7601 {
32397f22
MLI
7602 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7603 "missing initializer for field %qD of %qT",
7604 constructor_unfilled_fields,
7605 constructor_type))
7606 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7607 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7608 }
7609 }
de520661 7610
3e4093b6 7611 /* Pad out the end of the structure. */
916c5919 7612 if (p->replacement_value.value)
3e4093b6
RS
7613 /* If this closes a superfluous brace pair,
7614 just pass out the element between them. */
916c5919 7615 ret = p->replacement_value;
3e4093b6
RS
7616 else if (constructor_type == 0)
7617 ;
7618 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7619 && TREE_CODE (constructor_type) != UNION_TYPE
7620 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 7621 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
7622 {
7623 /* A nonincremental scalar initializer--just return
7624 the element, after verifying there is just one. */
9771b263 7625 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7626 {
7627 if (!constructor_erroneous)
ea58ef42 7628 error_init (loc, "empty scalar initializer");
916c5919 7629 ret.value = error_mark_node;
3e4093b6 7630 }
9771b263 7631 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7632 {
ea58ef42 7633 error_init (loc, "extra elements in scalar initializer");
9771b263 7634 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7635 }
7636 else
9771b263 7637 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7638 }
7639 else
7640 {
7641 if (constructor_erroneous)
916c5919 7642 ret.value = error_mark_node;
3e4093b6
RS
7643 else
7644 {
916c5919 7645 ret.value = build_constructor (constructor_type,
4038c495 7646 constructor_elements);
3e4093b6 7647 if (constructor_constant)
51eed280 7648 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7649 if (constructor_constant && constructor_simple)
916c5919 7650 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7651 if (constructor_nonconst)
7652 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7653 }
7654 }
de520661 7655
928c19bb
JM
7656 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7657 {
7658 if (constructor_nonconst)
7659 ret.original_code = C_MAYBE_CONST_EXPR;
7660 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7661 ret.original_code = ERROR_MARK;
7662 }
7663
3e4093b6
RS
7664 constructor_type = p->type;
7665 constructor_fields = p->fields;
7666 constructor_index = p->index;
7667 constructor_max_index = p->max_index;
7668 constructor_unfilled_index = p->unfilled_index;
7669 constructor_unfilled_fields = p->unfilled_fields;
7670 constructor_bit_index = p->bit_index;
7671 constructor_elements = p->elements;
7672 constructor_constant = p->constant;
7673 constructor_simple = p->simple;
928c19bb 7674 constructor_nonconst = p->nonconst;
3e4093b6
RS
7675 constructor_erroneous = p->erroneous;
7676 constructor_incremental = p->incremental;
7677 constructor_designated = p->designated;
976d5a22 7678 designator_depth = p->designator_depth;
3e4093b6
RS
7679 constructor_pending_elts = p->pending_elts;
7680 constructor_depth = p->depth;
7681 if (!p->implicit)
7682 constructor_range_stack = p->range_stack;
7683 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7684
3e4093b6
RS
7685 constructor_stack = p->next;
7686 free (p);
b621a4dd 7687
5d5e98dc
VR
7688 if (ret.value == 0 && constructor_stack == 0)
7689 ret.value = error_mark_node;
916c5919 7690 return ret;
3e4093b6 7691}
8b6a5902 7692
3e4093b6
RS
7693/* Common handling for both array range and field name designators.
7694 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7695
3e4093b6 7696static int
ea58ef42
MP
7697set_designator (location_t loc, int array,
7698 struct obstack *braced_init_obstack)
de520661 7699{
3e4093b6
RS
7700 tree subtype;
7701 enum tree_code subcode;
de520661 7702
3e4093b6
RS
7703 /* Don't die if an entire brace-pair level is superfluous
7704 in the containing level. */
7705 if (constructor_type == 0)
7706 return 1;
de520661 7707
366de0ce
NS
7708 /* If there were errors in this designator list already, bail out
7709 silently. */
b06df647 7710 if (designator_erroneous)
3e4093b6 7711 return 1;
e28cae4f 7712
3e4093b6
RS
7713 if (!designator_depth)
7714 {
366de0ce 7715 gcc_assert (!constructor_range_stack);
de520661 7716
3e4093b6
RS
7717 /* Designator list starts at the level of closest explicit
7718 braces. */
7719 while (constructor_stack->implicit)
34cf811f 7720 process_init_element (input_location,
ea58ef42 7721 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7722 true, braced_init_obstack);
3e4093b6
RS
7723 constructor_designated = 1;
7724 return 0;
7725 }
de520661 7726
366de0ce 7727 switch (TREE_CODE (constructor_type))
3c3fa147 7728 {
366de0ce
NS
7729 case RECORD_TYPE:
7730 case UNION_TYPE:
3e4093b6
RS
7731 subtype = TREE_TYPE (constructor_fields);
7732 if (subtype != error_mark_node)
7733 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7734 break;
7735 case ARRAY_TYPE:
3e4093b6 7736 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7737 break;
7738 default:
7739 gcc_unreachable ();
de520661 7740 }
400fbf9f 7741
3e4093b6
RS
7742 subcode = TREE_CODE (subtype);
7743 if (array && subcode != ARRAY_TYPE)
7744 {
ea58ef42 7745 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7746 return 1;
7747 }
7748 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7749 {
ea58ef42 7750 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7751 return 1;
7752 }
d45cf215 7753
3e4093b6 7754 constructor_designated = 1;
ea58ef42 7755 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7756 return 0;
de520661 7757}
400fbf9f 7758
3e4093b6
RS
7759/* If there are range designators in designator list, push a new designator
7760 to constructor_range_stack. RANGE_END is end of such stack range or
7761 NULL_TREE if there is no range designator at this level. */
400fbf9f 7762
3e4093b6 7763static void
a1e3b3d9 7764push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7765{
7766 struct constructor_range_stack *p;
400fbf9f 7767
a1e3b3d9
LB
7768 p = (struct constructor_range_stack *)
7769 obstack_alloc (braced_init_obstack,
7770 sizeof (struct constructor_range_stack));
3e4093b6
RS
7771 p->prev = constructor_range_stack;
7772 p->next = 0;
7773 p->fields = constructor_fields;
7774 p->range_start = constructor_index;
7775 p->index = constructor_index;
7776 p->stack = constructor_stack;
7777 p->range_end = range_end;
8b6a5902 7778 if (constructor_range_stack)
3e4093b6
RS
7779 constructor_range_stack->next = p;
7780 constructor_range_stack = p;
de520661 7781}
400fbf9f 7782
3e4093b6
RS
7783/* Within an array initializer, specify the next index to be initialized.
7784 FIRST is that index. If LAST is nonzero, then initialize a range
7785 of indices, running from FIRST through LAST. */
5a7ec9d9 7786
de520661 7787void
ea58ef42 7788set_init_index (location_t loc, tree first, tree last,
d033409e 7789 struct obstack *braced_init_obstack)
de520661 7790{
ea58ef42 7791 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7792 return;
de520661 7793
b06df647 7794 designator_erroneous = 1;
de520661 7795
3ea8cd06
JM
7796 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7797 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7798 {
ea58ef42 7799 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7800 return;
7801 }
7802
2b6da65c
JM
7803 if (TREE_CODE (first) != INTEGER_CST)
7804 {
7805 first = c_fully_fold (first, false, NULL);
7806 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7807 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7808 "array index in initializer is not "
7809 "an integer constant expression");
7810 }
7811
7812 if (last && TREE_CODE (last) != INTEGER_CST)
7813 {
7814 last = c_fully_fold (last, false, NULL);
7815 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7816 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7817 "array index in initializer is not "
7818 "an integer constant expression");
7819 }
7820
3e4093b6 7821 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7822 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7823 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7824 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7825 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7826 error_init (loc, "array index in non-array initializer");
622adc7e 7827 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7828 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7829 else if (constructor_max_index
7830 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7831 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7832 else
de520661 7833 {
928c19bb
JM
7834 constant_expression_warning (first);
7835 if (last)
7836 constant_expression_warning (last);
3e4093b6 7837 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7838 if (tree_int_cst_lt (constructor_index, first))
7839 {
7840 constructor_index = copy_node (constructor_index);
7841 TREE_OVERFLOW (constructor_index) = 1;
7842 }
665f2503 7843
3e4093b6 7844 if (last)
2bede729 7845 {
3e4093b6
RS
7846 if (tree_int_cst_equal (first, last))
7847 last = 0;
7848 else if (tree_int_cst_lt (last, first))
7849 {
ea58ef42 7850 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7851 last = 0;
7852 }
7853 else
7854 {
7855 last = convert (bitsizetype, last);
7856 if (constructor_max_index != 0
7857 && tree_int_cst_lt (constructor_max_index, last))
7858 {
ea58ef42
MP
7859 error_init (loc, "array index range in initializer exceeds "
7860 "array bounds");
3e4093b6
RS
7861 last = 0;
7862 }
7863 }
2bede729 7864 }
fed3cef0 7865
3e4093b6 7866 designator_depth++;
b06df647 7867 designator_erroneous = 0;
3e4093b6 7868 if (constructor_range_stack || last)
a1e3b3d9 7869 push_range_stack (last, braced_init_obstack);
de520661 7870 }
de520661 7871}
3e4093b6
RS
7872
7873/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7874
de520661 7875void
ea58ef42
MP
7876set_init_label (location_t loc, tree fieldname,
7877 struct obstack *braced_init_obstack)
de520661 7878{
0fb96aa4 7879 tree field;
94ba5069 7880
ea58ef42 7881 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7882 return;
7883
b06df647 7884 designator_erroneous = 1;
3e4093b6
RS
7885
7886 if (TREE_CODE (constructor_type) != RECORD_TYPE
7887 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7888 {
ea58ef42 7889 error_init (loc, "field name not in record or union initializer");
3e4093b6 7890 return;
94ba5069
RS
7891 }
7892
0fb96aa4 7893 field = lookup_field (constructor_type, fieldname);
8b6a5902 7894
0fb96aa4 7895 if (field == 0)
9babc352 7896 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 7897 else
0fb96aa4
JM
7898 do
7899 {
7900 constructor_fields = TREE_VALUE (field);
7901 designator_depth++;
7902 designator_erroneous = 0;
7903 if (constructor_range_stack)
7904 push_range_stack (NULL_TREE, braced_init_obstack);
7905 field = TREE_CHAIN (field);
7906 if (field)
7907 {
ea58ef42 7908 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7909 return;
7910 }
7911 }
7912 while (field != NULL_TREE);
3e4093b6
RS
7913}
7914\f
7915/* Add a new initializer to the tree of pending initializers. PURPOSE
7916 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7917 VALUE is the value of that index or field. If ORIGTYPE is not
7918 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7919
7920 IMPLICIT is true if value comes from pop_init_level (1),
7921 the new initializer has been merged with the existing one
7922 and thus no warnings should be emitted about overriding an
7923 existing initializer. */
de520661 7924
3e4093b6 7925static void
96b40f8d
MP
7926add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7927 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7928{
7929 struct init_node *p, **q, *r;
7930
7931 q = &constructor_pending_elts;
7932 p = 0;
7933
7934 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7935 {
3e4093b6 7936 while (*q != 0)
91fa3c30 7937 {
3e4093b6
RS
7938 p = *q;
7939 if (tree_int_cst_lt (purpose, p->purpose))
7940 q = &p->left;
7941 else if (tree_int_cst_lt (p->purpose, purpose))
7942 q = &p->right;
7943 else
7944 {
b295aee2
JJ
7945 if (!implicit)
7946 {
7947 if (TREE_SIDE_EFFECTS (p->value))
755e528f 7948 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
7949 "initialized field with side-effects "
7950 "overwritten");
b295aee2 7951 else if (warn_override_init)
96b40f8d
MP
7952 warning_init (loc, OPT_Woverride_init,
7953 "initialized field overwritten");
b295aee2 7954 }
3e4093b6 7955 p->value = value;
bbbbb16a 7956 p->origtype = origtype;
3e4093b6
RS
7957 return;
7958 }
91fa3c30 7959 }
de520661 7960 }
3e4093b6 7961 else
de520661 7962 {
3e4093b6 7963 tree bitpos;
400fbf9f 7964
3e4093b6
RS
7965 bitpos = bit_position (purpose);
7966 while (*q != NULL)
7967 {
7968 p = *q;
7969 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7970 q = &p->left;
7971 else if (p->purpose != purpose)
7972 q = &p->right;
7973 else
7974 {
b295aee2
JJ
7975 if (!implicit)
7976 {
7977 if (TREE_SIDE_EFFECTS (p->value))
755e528f 7978 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
7979 "initialized field with side-effects "
7980 "overwritten");
b295aee2 7981 else if (warn_override_init)
96b40f8d
MP
7982 warning_init (loc, OPT_Woverride_init,
7983 "initialized field overwritten");
b295aee2 7984 }
3e4093b6 7985 p->value = value;
bbbbb16a 7986 p->origtype = origtype;
3e4093b6
RS
7987 return;
7988 }
7989 }
91fa3c30 7990 }
b71c7f8a 7991
a1e3b3d9
LB
7992 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7993 sizeof (struct init_node));
3e4093b6
RS
7994 r->purpose = purpose;
7995 r->value = value;
bbbbb16a 7996 r->origtype = origtype;
8b6a5902 7997
3e4093b6
RS
7998 *q = r;
7999 r->parent = p;
8000 r->left = 0;
8001 r->right = 0;
8002 r->balance = 0;
b71c7f8a 8003
3e4093b6 8004 while (p)
de520661 8005 {
3e4093b6 8006 struct init_node *s;
665f2503 8007
3e4093b6 8008 if (r == p->left)
2bede729 8009 {
3e4093b6
RS
8010 if (p->balance == 0)
8011 p->balance = -1;
8012 else if (p->balance < 0)
8013 {
8014 if (r->balance < 0)
8015 {
8016 /* L rotation. */
8017 p->left = r->right;
8018 if (p->left)
8019 p->left->parent = p;
8020 r->right = p;
e7b6a0ee 8021
3e4093b6
RS
8022 p->balance = 0;
8023 r->balance = 0;
39bc99c2 8024
3e4093b6
RS
8025 s = p->parent;
8026 p->parent = r;
8027 r->parent = s;
8028 if (s)
8029 {
8030 if (s->left == p)
8031 s->left = r;
8032 else
8033 s->right = r;
8034 }
8035 else
8036 constructor_pending_elts = r;
8037 }
8038 else
8039 {
8040 /* LR rotation. */
8041 struct init_node *t = r->right;
e7b6a0ee 8042
3e4093b6
RS
8043 r->right = t->left;
8044 if (r->right)
8045 r->right->parent = r;
8046 t->left = r;
8047
8048 p->left = t->right;
8049 if (p->left)
8050 p->left->parent = p;
8051 t->right = p;
8052
8053 p->balance = t->balance < 0;
8054 r->balance = -(t->balance > 0);
8055 t->balance = 0;
8056
8057 s = p->parent;
8058 p->parent = t;
8059 r->parent = t;
8060 t->parent = s;
8061 if (s)
8062 {
8063 if (s->left == p)
8064 s->left = t;
8065 else
8066 s->right = t;
8067 }
8068 else
8069 constructor_pending_elts = t;
8070 }
8071 break;
8072 }
8073 else
8074 {
8075 /* p->balance == +1; growth of left side balances the node. */
8076 p->balance = 0;
8077 break;
8078 }
2bede729 8079 }
3e4093b6
RS
8080 else /* r == p->right */
8081 {
8082 if (p->balance == 0)
8083 /* Growth propagation from right side. */
8084 p->balance++;
8085 else if (p->balance > 0)
8086 {
8087 if (r->balance > 0)
8088 {
8089 /* R rotation. */
8090 p->right = r->left;
8091 if (p->right)
8092 p->right->parent = p;
8093 r->left = p;
8094
8095 p->balance = 0;
8096 r->balance = 0;
8097
8098 s = p->parent;
8099 p->parent = r;
8100 r->parent = s;
8101 if (s)
8102 {
8103 if (s->left == p)
8104 s->left = r;
8105 else
8106 s->right = r;
8107 }
8108 else
8109 constructor_pending_elts = r;
8110 }
8111 else /* r->balance == -1 */
8112 {
8113 /* RL rotation */
8114 struct init_node *t = r->left;
8115
8116 r->left = t->right;
8117 if (r->left)
8118 r->left->parent = r;
8119 t->right = r;
8120
8121 p->right = t->left;
8122 if (p->right)
8123 p->right->parent = p;
8124 t->left = p;
8125
8126 r->balance = (t->balance < 0);
8127 p->balance = -(t->balance > 0);
8128 t->balance = 0;
8129
8130 s = p->parent;
8131 p->parent = t;
8132 r->parent = t;
8133 t->parent = s;
8134 if (s)
8135 {
8136 if (s->left == p)
8137 s->left = t;
8138 else
8139 s->right = t;
8140 }
8141 else
8142 constructor_pending_elts = t;
8143 }
8144 break;
8145 }
8146 else
8147 {
8148 /* p->balance == -1; growth of right side balances the node. */
8149 p->balance = 0;
8150 break;
8151 }
8152 }
8153
8154 r = p;
8155 p = p->parent;
8156 }
8157}
8158
8159/* Build AVL tree from a sorted chain. */
8160
8161static void
a1e3b3d9 8162set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8163{
4038c495
GB
8164 unsigned HOST_WIDE_INT ix;
8165 tree index, value;
3e4093b6
RS
8166
8167 if (TREE_CODE (constructor_type) != RECORD_TYPE
8168 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8169 return;
8170
4038c495 8171 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8172 add_pending_init (input_location, index, value, NULL_TREE, true,
8173 braced_init_obstack);
9771b263 8174 constructor_elements = NULL;
3e4093b6
RS
8175 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8176 {
8177 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8178 /* Skip any nameless bit fields at the beginning. */
8179 while (constructor_unfilled_fields != 0
8180 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8181 && DECL_NAME (constructor_unfilled_fields) == 0)
8182 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8183
de520661 8184 }
3e4093b6 8185 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8186 {
3e4093b6
RS
8187 if (TYPE_DOMAIN (constructor_type))
8188 constructor_unfilled_index
8189 = convert (bitsizetype,
8190 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8191 else
8192 constructor_unfilled_index = bitsize_zero_node;
de520661 8193 }
3e4093b6 8194 constructor_incremental = 0;
de520661 8195}
400fbf9f 8196
3e4093b6 8197/* Build AVL tree from a string constant. */
de520661 8198
3e4093b6 8199static void
a1e3b3d9
LB
8200set_nonincremental_init_from_string (tree str,
8201 struct obstack * braced_init_obstack)
de520661 8202{
3e4093b6
RS
8203 tree value, purpose, type;
8204 HOST_WIDE_INT val[2];
8205 const char *p, *end;
8206 int byte, wchar_bytes, charwidth, bitpos;
de520661 8207
366de0ce 8208 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8209
c466b2cd 8210 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8211 charwidth = TYPE_PRECISION (char_type_node);
8212 type = TREE_TYPE (constructor_type);
8213 p = TREE_STRING_POINTER (str);
8214 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8215
3e4093b6 8216 for (purpose = bitsize_zero_node;
8824edff
JJ
8217 p < end
8218 && !(constructor_max_index
8219 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8220 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8221 {
3e4093b6 8222 if (wchar_bytes == 1)
ffc5c6a9 8223 {
807e902e
KZ
8224 val[0] = (unsigned char) *p++;
8225 val[1] = 0;
ffc5c6a9
RH
8226 }
8227 else
3e4093b6 8228 {
3e4093b6 8229 val[1] = 0;
807e902e 8230 val[0] = 0;
3e4093b6
RS
8231 for (byte = 0; byte < wchar_bytes; byte++)
8232 {
8233 if (BYTES_BIG_ENDIAN)
8234 bitpos = (wchar_bytes - byte - 1) * charwidth;
8235 else
8236 bitpos = byte * charwidth;
807e902e 8237 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8238 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8239 << (bitpos % HOST_BITS_PER_WIDE_INT);
8240 }
8241 }
584ef5fe 8242
8df83eae 8243 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8244 {
8245 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8246 if (bitpos < HOST_BITS_PER_WIDE_INT)
8247 {
807e902e 8248 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8249 {
807e902e
KZ
8250 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8251 val[1] = -1;
3e4093b6
RS
8252 }
8253 }
8254 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8255 {
807e902e
KZ
8256 if (val[0] < 0)
8257 val[1] = -1;
3e4093b6 8258 }
807e902e 8259 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8260 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8261 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8262 << (bitpos - HOST_BITS_PER_WIDE_INT);
8263 }
ffc5c6a9 8264
807e902e
KZ
8265 value = wide_int_to_tree (type,
8266 wide_int::from_array (val, 2,
8267 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8268 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8269 braced_init_obstack);
9dfcc8db
BH
8270 }
8271
3e4093b6
RS
8272 constructor_incremental = 0;
8273}
de520661 8274
3e4093b6
RS
8275/* Return value of FIELD in pending initializer or zero if the field was
8276 not initialized yet. */
8277
8278static tree
a1e3b3d9 8279find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8280{
8281 struct init_node *p;
8282
8283 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8284 {
3e4093b6
RS
8285 if (constructor_incremental
8286 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8287 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8288
8289 p = constructor_pending_elts;
8290 while (p)
19d76e60 8291 {
3e4093b6
RS
8292 if (tree_int_cst_lt (field, p->purpose))
8293 p = p->left;
8294 else if (tree_int_cst_lt (p->purpose, field))
8295 p = p->right;
8296 else
8297 return p->value;
19d76e60 8298 }
19d76e60 8299 }
3e4093b6 8300 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8301 {
3e4093b6 8302 tree bitpos = bit_position (field);
de520661 8303
3e4093b6
RS
8304 if (constructor_incremental
8305 && (!constructor_unfilled_fields
8306 || tree_int_cst_lt (bitpos,
8307 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8308 set_nonincremental_init (braced_init_obstack);
de520661 8309
3e4093b6
RS
8310 p = constructor_pending_elts;
8311 while (p)
8312 {
8313 if (field == p->purpose)
8314 return p->value;
8315 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8316 p = p->left;
8317 else
8318 p = p->right;
8319 }
8320 }
8321 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8322 {
9771b263
DN
8323 if (!vec_safe_is_empty (constructor_elements)
8324 && (constructor_elements->last ().index == field))
8325 return constructor_elements->last ().value;
de520661 8326 }
3e4093b6 8327 return 0;
de520661
RS
8328}
8329
3e4093b6
RS
8330/* "Output" the next constructor element.
8331 At top level, really output it to assembler code now.
8332 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8333 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8334 TYPE is the data type that the containing data type wants here.
8335 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8336 If VALUE is a string constant, STRICT_STRING is true if it is
8337 unparenthesized or we should not warn here for it being parenthesized.
8338 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8339
3e4093b6
RS
8340 PENDING if non-nil means output pending elements that belong
8341 right after this element. (PENDING is normally 1;
b295aee2
JJ
8342 it is 0 while outputting pending elements, to avoid recursion.)
8343
8344 IMPLICIT is true if value comes from pop_init_level (1),
8345 the new initializer has been merged with the existing one
8346 and thus no warnings should be emitted about overriding an
8347 existing initializer. */
8b6a5902 8348
3e4093b6 8349static void
34cf811f
MP
8350output_init_element (location_t loc, tree value, tree origtype,
8351 bool strict_string, tree type, tree field, int pending,
8352 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8353{
8ce94e44 8354 tree semantic_type = NULL_TREE;
928c19bb
JM
8355 bool maybe_const = true;
8356 bool npc;
4038c495 8357
0a880880 8358 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8359 {
3e4093b6
RS
8360 constructor_erroneous = 1;
8361 return;
8b6a5902 8362 }
46bdb9cf
JM
8363 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8364 && (TREE_CODE (value) == STRING_CST
8365 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8366 && !(TREE_CODE (value) == STRING_CST
8367 && TREE_CODE (type) == ARRAY_TYPE
8368 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8369 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8370 TYPE_MAIN_VARIANT (type)))
c2255bc4 8371 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8372
3e4093b6 8373 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8374 && require_constant_value && pending)
8b6a5902 8375 {
3e4093b6
RS
8376 /* As an extension, allow initializing objects with static storage
8377 duration with compound literals (which are then treated just as
8378 the brace enclosed list they contain). */
4435bb92
MP
8379 if (flag_isoc99)
8380 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8381 "constant");
3e4093b6
RS
8382 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8383 value = DECL_INITIAL (decl);
8b6a5902
JJ
8384 }
8385
928c19bb 8386 npc = null_pointer_constant_p (value);
8ce94e44
JM
8387 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8388 {
8389 semantic_type = TREE_TYPE (value);
8390 value = TREE_OPERAND (value, 0);
8391 }
928c19bb
JM
8392 value = c_fully_fold (value, require_constant_value, &maybe_const);
8393
3e4093b6
RS
8394 if (value == error_mark_node)
8395 constructor_erroneous = 1;
8396 else if (!TREE_CONSTANT (value))
8397 constructor_constant = 0;
116df786 8398 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8399 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8400 || TREE_CODE (constructor_type) == UNION_TYPE)
8401 && DECL_C_BIT_FIELD (field)
8402 && TREE_CODE (value) != INTEGER_CST))
8403 constructor_simple = 0;
928c19bb
JM
8404 if (!maybe_const)
8405 constructor_nonconst = 1;
3e4093b6 8406
116df786 8407 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8408 {
116df786
RH
8409 if (require_constant_value)
8410 {
ea58ef42 8411 error_init (loc, "initializer element is not constant");
116df786
RH
8412 value = error_mark_node;
8413 }
8414 else if (require_constant_elements)
8337d1db 8415 pedwarn (loc, OPT_Wpedantic,
509c9d60 8416 "initializer element is not computable at load time");
8b6a5902 8417 }
928c19bb
JM
8418 else if (!maybe_const
8419 && (require_constant_value || require_constant_elements))
8337d1db 8420 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8421 "initializer element is not a constant expression");
3e4093b6 8422
81f40b79
ILT
8423 /* Issue -Wc++-compat warnings about initializing a bitfield with
8424 enum type. */
8425 if (warn_cxx_compat
8426 && field != NULL_TREE
8427 && TREE_CODE (field) == FIELD_DECL
8428 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8429 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8430 != TYPE_MAIN_VARIANT (type))
8431 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8432 {
8433 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8434 if (checktype != error_mark_node
8435 && (TYPE_MAIN_VARIANT (checktype)
8436 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8437 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8438 "enum conversion in initialization is invalid in C++");
8439 }
8440
3e4093b6
RS
8441 /* If this field is empty (and not at the end of structure),
8442 don't do anything other than checking the initializer. */
8443 if (field
8444 && (TREE_TYPE (field) == error_mark_node
8445 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8446 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8447 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8448 || DECL_CHAIN (field)))))
3e4093b6
RS
8449 return;
8450
8ce94e44
JM
8451 if (semantic_type)
8452 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8453 value = digest_init (loc, type, value, origtype, npc, strict_string,
8454 require_constant_value);
3e4093b6 8455 if (value == error_mark_node)
8b6a5902 8456 {
3e4093b6
RS
8457 constructor_erroneous = 1;
8458 return;
8b6a5902 8459 }
928c19bb
JM
8460 if (require_constant_value || require_constant_elements)
8461 constant_expression_warning (value);
8b6a5902 8462
3e4093b6
RS
8463 /* If this element doesn't come next in sequence,
8464 put it on constructor_pending_elts. */
8465 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8466 && (!constructor_incremental
8467 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8468 {
3e4093b6
RS
8469 if (constructor_incremental
8470 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8471 set_nonincremental_init (braced_init_obstack);
3e4093b6 8472
96b40f8d 8473 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8474 braced_init_obstack);
3e4093b6 8475 return;
8b6a5902 8476 }
3e4093b6
RS
8477 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8478 && (!constructor_incremental
8479 || field != constructor_unfilled_fields))
8b6a5902 8480 {
3e4093b6
RS
8481 /* We do this for records but not for unions. In a union,
8482 no matter which field is specified, it can be initialized
8483 right away since it starts at the beginning of the union. */
8484 if (constructor_incremental)
8485 {
8486 if (!constructor_unfilled_fields)
a1e3b3d9 8487 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8488 else
8489 {
8490 tree bitpos, unfillpos;
8491
8492 bitpos = bit_position (field);
8493 unfillpos = bit_position (constructor_unfilled_fields);
8494
8495 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8496 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8497 }
8498 }
8499
96b40f8d 8500 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8501 braced_init_obstack);
3e4093b6 8502 return;
8b6a5902 8503 }
3e4093b6 8504 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8505 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8506 {
b295aee2
JJ
8507 if (!implicit)
8508 {
9771b263 8509 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8510 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8511 "initialized field with side-effects overwritten");
8512 else if (warn_override_init)
96b40f8d
MP
8513 warning_init (loc, OPT_Woverride_init,
8514 "initialized field overwritten");
b295aee2 8515 }
8b6a5902 8516
3e4093b6 8517 /* We can have just one union field set. */
9771b263 8518 constructor_elements = NULL;
3e4093b6 8519 }
8b6a5902 8520
3e4093b6
RS
8521 /* Otherwise, output this element either to
8522 constructor_elements or to the assembler file. */
8b6a5902 8523
f32682ca 8524 constructor_elt celt = {field, value};
9771b263 8525 vec_safe_push (constructor_elements, celt);
8b6a5902 8526
3e4093b6
RS
8527 /* Advance the variable that indicates sequential elements output. */
8528 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8529 constructor_unfilled_index
db3927fb
AH
8530 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8531 bitsize_one_node);
3e4093b6
RS
8532 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8533 {
8534 constructor_unfilled_fields
910ad8de 8535 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8536
3e4093b6
RS
8537 /* Skip any nameless bit fields. */
8538 while (constructor_unfilled_fields != 0
8539 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8540 && DECL_NAME (constructor_unfilled_fields) == 0)
8541 constructor_unfilled_fields =
910ad8de 8542 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8543 }
8544 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8545 constructor_unfilled_fields = 0;
de520661 8546
3e4093b6
RS
8547 /* Now output any pending elements which have become next. */
8548 if (pending)
a1e3b3d9 8549 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8550}
8b6a5902 8551
3e4093b6
RS
8552/* Output any pending elements which have become next.
8553 As we output elements, constructor_unfilled_{fields,index}
8554 advances, which may cause other elements to become next;
8555 if so, they too are output.
8b6a5902 8556
3e4093b6
RS
8557 If ALL is 0, we return when there are
8558 no more pending elements to output now.
665f2503 8559
3e4093b6
RS
8560 If ALL is 1, we output space as necessary so that
8561 we can output all the pending elements. */
3e4093b6 8562static void
a1e3b3d9 8563output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8564{
8565 struct init_node *elt = constructor_pending_elts;
8566 tree next;
de520661 8567
3e4093b6
RS
8568 retry:
8569
ba228239 8570 /* Look through the whole pending tree.
3e4093b6
RS
8571 If we find an element that should be output now,
8572 output it. Otherwise, set NEXT to the element
8573 that comes first among those still pending. */
8574
8575 next = 0;
8576 while (elt)
8577 {
8578 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8579 {
3e4093b6
RS
8580 if (tree_int_cst_equal (elt->purpose,
8581 constructor_unfilled_index))
34cf811f
MP
8582 output_init_element (input_location, elt->value, elt->origtype,
8583 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8584 constructor_unfilled_index, 0, false,
8585 braced_init_obstack);
3e4093b6
RS
8586 else if (tree_int_cst_lt (constructor_unfilled_index,
8587 elt->purpose))
8b6a5902 8588 {
3e4093b6
RS
8589 /* Advance to the next smaller node. */
8590 if (elt->left)
8591 elt = elt->left;
8592 else
8593 {
8594 /* We have reached the smallest node bigger than the
8595 current unfilled index. Fill the space first. */
8596 next = elt->purpose;
8597 break;
8598 }
8b6a5902 8599 }
ce662d4c
JJ
8600 else
8601 {
3e4093b6
RS
8602 /* Advance to the next bigger node. */
8603 if (elt->right)
8604 elt = elt->right;
8605 else
ce662d4c 8606 {
3e4093b6
RS
8607 /* We have reached the biggest node in a subtree. Find
8608 the parent of it, which is the next bigger node. */
8609 while (elt->parent && elt->parent->right == elt)
8610 elt = elt->parent;
8611 elt = elt->parent;
8612 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8613 elt->purpose))
8614 {
8615 next = elt->purpose;
8616 break;
8617 }
ce662d4c
JJ
8618 }
8619 }
8b6a5902 8620 }
3e4093b6
RS
8621 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8622 || TREE_CODE (constructor_type) == UNION_TYPE)
8623 {
8624 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8625
3e4093b6
RS
8626 /* If the current record is complete we are done. */
8627 if (constructor_unfilled_fields == 0)
8628 break;
de520661 8629
3e4093b6
RS
8630 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8631 elt_bitpos = bit_position (elt->purpose);
8632 /* We can't compare fields here because there might be empty
8633 fields in between. */
8634 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8635 {
8636 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8637 output_init_element (input_location, elt->value, elt->origtype,
8638 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8639 elt->purpose, 0, false,
8640 braced_init_obstack);
3e4093b6
RS
8641 }
8642 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8643 {
8644 /* Advance to the next smaller node. */
8645 if (elt->left)
8646 elt = elt->left;
8647 else
8648 {
8649 /* We have reached the smallest node bigger than the
8650 current unfilled field. Fill the space first. */
8651 next = elt->purpose;
8652 break;
8653 }
8654 }
8655 else
8656 {
8657 /* Advance to the next bigger node. */
8658 if (elt->right)
8659 elt = elt->right;
8660 else
8661 {
8662 /* We have reached the biggest node in a subtree. Find
8663 the parent of it, which is the next bigger node. */
8664 while (elt->parent && elt->parent->right == elt)
8665 elt = elt->parent;
8666 elt = elt->parent;
8667 if (elt
8668 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8669 bit_position (elt->purpose))))
8670 {
8671 next = elt->purpose;
8672 break;
8673 }
8674 }
8675 }
8676 }
8677 }
de520661 8678
3e4093b6
RS
8679 /* Ordinarily return, but not if we want to output all
8680 and there are elements left. */
3f75a254 8681 if (!(all && next != 0))
e5cfb88f
RK
8682 return;
8683
3e4093b6
RS
8684 /* If it's not incremental, just skip over the gap, so that after
8685 jumping to retry we will output the next successive element. */
8686 if (TREE_CODE (constructor_type) == RECORD_TYPE
8687 || TREE_CODE (constructor_type) == UNION_TYPE)
8688 constructor_unfilled_fields = next;
8689 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8690 constructor_unfilled_index = next;
de520661 8691
3e4093b6
RS
8692 /* ELT now points to the node in the pending tree with the next
8693 initializer to output. */
8694 goto retry;
de520661
RS
8695}
8696\f
3e4093b6
RS
8697/* Add one non-braced element to the current constructor level.
8698 This adjusts the current position within the constructor's type.
8699 This may also start or terminate implicit levels
8700 to handle a partly-braced initializer.
e5e809f4 8701
3e4093b6 8702 Once this has found the correct level for the new element,
b295aee2
JJ
8703 it calls output_init_element.
8704
8705 IMPLICIT is true if value comes from pop_init_level (1),
8706 the new initializer has been merged with the existing one
8707 and thus no warnings should be emitted about overriding an
8708 existing initializer. */
3e4093b6
RS
8709
8710void
34cf811f 8711process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8712 struct obstack * braced_init_obstack)
e5e809f4 8713{
916c5919
JM
8714 tree orig_value = value.value;
8715 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8716 bool strict_string = value.original_code == STRING_CST;
340baef7 8717 bool was_designated = designator_depth != 0;
e5e809f4 8718
3e4093b6 8719 designator_depth = 0;
b06df647 8720 designator_erroneous = 0;
e5e809f4 8721
9bac5cbb
G
8722 if (!implicit && value.value && !integer_zerop (value.value))
8723 constructor_zeroinit = 0;
8724
3e4093b6
RS
8725 /* Handle superfluous braces around string cst as in
8726 char x[] = {"foo"}; */
8727 if (string_flag
8728 && constructor_type
340baef7 8729 && !was_designated
3e4093b6 8730 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8731 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8732 && integer_zerop (constructor_unfilled_index))
e5e809f4 8733 {
916c5919 8734 if (constructor_stack->replacement_value.value)
ea58ef42 8735 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8736 constructor_stack->replacement_value = value;
8737 return;
e5e809f4 8738 }
8b6a5902 8739
916c5919 8740 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8741 {
ea58ef42 8742 error_init (loc, "excess elements in struct initializer");
3e4093b6 8743 return;
e5e809f4
JL
8744 }
8745
3e4093b6
RS
8746 /* Ignore elements of a brace group if it is entirely superfluous
8747 and has already been diagnosed. */
8748 if (constructor_type == 0)
8749 return;
e5e809f4 8750
976d5a22
TT
8751 if (!implicit && warn_designated_init && !was_designated
8752 && TREE_CODE (constructor_type) == RECORD_TYPE
8753 && lookup_attribute ("designated_init",
8754 TYPE_ATTRIBUTES (constructor_type)))
8755 warning_init (loc,
8756 OPT_Wdesignated_init,
8757 "positional initialization of field "
8758 "in %<struct%> declared with %<designated_init%> attribute");
8759
3e4093b6
RS
8760 /* If we've exhausted any levels that didn't have braces,
8761 pop them now. */
8762 while (constructor_stack->implicit)
8763 {
8764 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8765 || TREE_CODE (constructor_type) == UNION_TYPE)
8766 && constructor_fields == 0)
ea58ef42
MP
8767 process_init_element (loc,
8768 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8769 true, braced_init_obstack);
53650abe 8770 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 8771 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
8772 && constructor_max_index
8773 && tree_int_cst_lt (constructor_max_index,
8774 constructor_index))
ea58ef42
MP
8775 process_init_element (loc,
8776 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8777 true, braced_init_obstack);
3e4093b6
RS
8778 else
8779 break;
8780 }
e5e809f4 8781
3e4093b6
RS
8782 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8783 if (constructor_range_stack)
e5e809f4 8784 {
3e4093b6
RS
8785 /* If value is a compound literal and we'll be just using its
8786 content, don't put it into a SAVE_EXPR. */
916c5919 8787 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 8788 || !require_constant_value)
8ce94e44
JM
8789 {
8790 tree semantic_type = NULL_TREE;
8791 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8792 {
8793 semantic_type = TREE_TYPE (value.value);
8794 value.value = TREE_OPERAND (value.value, 0);
8795 }
8796 value.value = c_save_expr (value.value);
8797 if (semantic_type)
8798 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8799 value.value);
8800 }
3e4093b6 8801 }
e5e809f4 8802
3e4093b6
RS
8803 while (1)
8804 {
8805 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8806 {
3e4093b6
RS
8807 tree fieldtype;
8808 enum tree_code fieldcode;
e5e809f4 8809
3e4093b6
RS
8810 if (constructor_fields == 0)
8811 {
ea58ef42 8812 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8813 break;
8814 }
e5e809f4 8815
3e4093b6
RS
8816 fieldtype = TREE_TYPE (constructor_fields);
8817 if (fieldtype != error_mark_node)
8818 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8819 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8820
3e4093b6
RS
8821 /* Error for non-static initialization of a flexible array member. */
8822 if (fieldcode == ARRAY_TYPE
8823 && !require_constant_value
8824 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8825 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8826 {
ea58ef42
MP
8827 error_init (loc, "non-static initialization of a flexible "
8828 "array member");
3e4093b6
RS
8829 break;
8830 }
e5e809f4 8831
2cc901dc
MP
8832 /* Error for initialization of a flexible array member with
8833 a string constant if the structure is in an array. E.g.:
8834 struct S { int x; char y[]; };
8835 struct S s[] = { { 1, "foo" } };
8836 is invalid. */
8837 if (string_flag
8838 && fieldcode == ARRAY_TYPE
8839 && constructor_depth > 1
8840 && TYPE_SIZE (fieldtype) == NULL_TREE
8841 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8842 {
8843 bool in_array_p = false;
8844 for (struct constructor_stack *p = constructor_stack;
8845 p && p->type; p = p->next)
8846 if (TREE_CODE (p->type) == ARRAY_TYPE)
8847 {
8848 in_array_p = true;
8849 break;
8850 }
8851 if (in_array_p)
8852 {
8853 error_init (loc, "initialization of flexible array "
8854 "member in a nested context");
8855 break;
8856 }
8857 }
8858
3e4093b6 8859 /* Accept a string constant to initialize a subarray. */
916c5919 8860 if (value.value != 0
3e4093b6 8861 && fieldcode == ARRAY_TYPE
197463ae 8862 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8863 && string_flag)
916c5919 8864 value.value = orig_value;
3e4093b6
RS
8865 /* Otherwise, if we have come to a subaggregate,
8866 and we don't have an element of its type, push into it. */
0953878d 8867 else if (value.value != 0
916c5919
JM
8868 && value.value != error_mark_node
8869 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8870 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8871 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8872 {
ea58ef42 8873 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8874 continue;
8875 }
e5e809f4 8876
916c5919 8877 if (value.value)
3e4093b6
RS
8878 {
8879 push_member_name (constructor_fields);
34cf811f 8880 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8881 strict_string, fieldtype,
a1e3b3d9
LB
8882 constructor_fields, 1, implicit,
8883 braced_init_obstack);
3e4093b6 8884 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8885 }
8886 else
3e4093b6
RS
8887 /* Do the bookkeeping for an element that was
8888 directly output as a constructor. */
e5e809f4 8889 {
3e4093b6
RS
8890 /* For a record, keep track of end position of last field. */
8891 if (DECL_SIZE (constructor_fields))
c22cacf3 8892 constructor_bit_index
db3927fb
AH
8893 = size_binop_loc (input_location, PLUS_EXPR,
8894 bit_position (constructor_fields),
8895 DECL_SIZE (constructor_fields));
3e4093b6
RS
8896
8897 /* If the current field was the first one not yet written out,
8898 it isn't now, so update. */
8899 if (constructor_unfilled_fields == constructor_fields)
8900 {
910ad8de 8901 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8902 /* Skip any nameless bit fields. */
8903 while (constructor_unfilled_fields != 0
8904 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8905 && DECL_NAME (constructor_unfilled_fields) == 0)
8906 constructor_unfilled_fields =
910ad8de 8907 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8908 }
e5e809f4 8909 }
3e4093b6 8910
910ad8de 8911 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8912 /* Skip any nameless bit fields at the beginning. */
8913 while (constructor_fields != 0
8914 && DECL_C_BIT_FIELD (constructor_fields)
8915 && DECL_NAME (constructor_fields) == 0)
910ad8de 8916 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8917 }
3e4093b6 8918 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8919 {
3e4093b6
RS
8920 tree fieldtype;
8921 enum tree_code fieldcode;
e5e809f4 8922
3e4093b6
RS
8923 if (constructor_fields == 0)
8924 {
d033409e 8925 pedwarn_init (loc, 0,
509c9d60 8926 "excess elements in union initializer");
3e4093b6
RS
8927 break;
8928 }
e5e809f4 8929
3e4093b6
RS
8930 fieldtype = TREE_TYPE (constructor_fields);
8931 if (fieldtype != error_mark_node)
8932 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8933 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8934
3e4093b6
RS
8935 /* Warn that traditional C rejects initialization of unions.
8936 We skip the warning if the value is zero. This is done
8937 under the assumption that the zero initializer in user
8938 code appears conditioned on e.g. __STDC__ to avoid
8939 "missing initializer" warnings and relies on default
8940 initialization to zero in the traditional C case.
8941 We also skip the warning if the initializer is designated,
8942 again on the assumption that this must be conditional on
8943 __STDC__ anyway (and we've already complained about the
8944 member-designator already). */
8400e75e 8945 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8946 && !(value.value && (integer_zerop (value.value)
8947 || real_zerop (value.value))))
3176a0c2
DD
8948 warning (OPT_Wtraditional, "traditional C rejects initialization "
8949 "of unions");
e5e809f4 8950
3e4093b6 8951 /* Accept a string constant to initialize a subarray. */
916c5919 8952 if (value.value != 0
3e4093b6 8953 && fieldcode == ARRAY_TYPE
197463ae 8954 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8955 && string_flag)
916c5919 8956 value.value = orig_value;
3e4093b6
RS
8957 /* Otherwise, if we have come to a subaggregate,
8958 and we don't have an element of its type, push into it. */
0953878d 8959 else if (value.value != 0
916c5919
JM
8960 && value.value != error_mark_node
8961 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8962 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8963 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8964 {
ea58ef42 8965 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8966 continue;
8967 }
e5e809f4 8968
916c5919 8969 if (value.value)
3e4093b6
RS
8970 {
8971 push_member_name (constructor_fields);
34cf811f 8972 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8973 strict_string, fieldtype,
a1e3b3d9
LB
8974 constructor_fields, 1, implicit,
8975 braced_init_obstack);
3e4093b6 8976 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8977 }
8978 else
3e4093b6
RS
8979 /* Do the bookkeeping for an element that was
8980 directly output as a constructor. */
e5e809f4 8981 {
3e4093b6 8982 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8983 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8984 }
e5e809f4 8985
3e4093b6
RS
8986 constructor_fields = 0;
8987 }
8988 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8989 {
8990 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8991 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8992
3e4093b6 8993 /* Accept a string constant to initialize a subarray. */
916c5919 8994 if (value.value != 0
3e4093b6 8995 && eltcode == ARRAY_TYPE
197463ae 8996 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8997 && string_flag)
916c5919 8998 value.value = orig_value;
3e4093b6
RS
8999 /* Otherwise, if we have come to a subaggregate,
9000 and we don't have an element of its type, push into it. */
0953878d 9001 else if (value.value != 0
916c5919
JM
9002 && value.value != error_mark_node
9003 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9004 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9005 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9006 {
ea58ef42 9007 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9008 continue;
9009 }
8b6a5902 9010
3e4093b6
RS
9011 if (constructor_max_index != 0
9012 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9013 || integer_all_onesp (constructor_max_index)))
9014 {
d033409e 9015 pedwarn_init (loc, 0,
509c9d60 9016 "excess elements in array initializer");
3e4093b6
RS
9017 break;
9018 }
8b6a5902 9019
3e4093b6 9020 /* Now output the actual element. */
916c5919 9021 if (value.value)
3e4093b6 9022 {
ae7e9ddd 9023 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9024 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9025 strict_string, elttype,
a1e3b3d9
LB
9026 constructor_index, 1, implicit,
9027 braced_init_obstack);
3e4093b6
RS
9028 RESTORE_SPELLING_DEPTH (constructor_depth);
9029 }
2f6e4e97 9030
3e4093b6 9031 constructor_index
db3927fb
AH
9032 = size_binop_loc (input_location, PLUS_EXPR,
9033 constructor_index, bitsize_one_node);
8b6a5902 9034
916c5919 9035 if (!value.value)
3e4093b6
RS
9036 /* If we are doing the bookkeeping for an element that was
9037 directly output as a constructor, we must update
9038 constructor_unfilled_index. */
9039 constructor_unfilled_index = constructor_index;
9040 }
31521951 9041 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9042 {
9043 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9044
c22cacf3
MS
9045 /* Do a basic check of initializer size. Note that vectors
9046 always have a fixed size derived from their type. */
3e4093b6
RS
9047 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9048 {
d033409e 9049 pedwarn_init (loc, 0,
509c9d60 9050 "excess elements in vector initializer");
3e4093b6
RS
9051 break;
9052 }
8b6a5902 9053
3e4093b6 9054 /* Now output the actual element. */
916c5919 9055 if (value.value)
53650abe
AP
9056 {
9057 if (TREE_CODE (value.value) == VECTOR_CST)
9058 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9059 output_init_element (loc, value.value, value.original_type,
53650abe 9060 strict_string, elttype,
a1e3b3d9
LB
9061 constructor_index, 1, implicit,
9062 braced_init_obstack);
53650abe 9063 }
8b6a5902 9064
3e4093b6 9065 constructor_index
db3927fb
AH
9066 = size_binop_loc (input_location,
9067 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9068
916c5919 9069 if (!value.value)
3e4093b6
RS
9070 /* If we are doing the bookkeeping for an element that was
9071 directly output as a constructor, we must update
9072 constructor_unfilled_index. */
9073 constructor_unfilled_index = constructor_index;
9074 }
8b6a5902 9075
3e4093b6
RS
9076 /* Handle the sole element allowed in a braced initializer
9077 for a scalar variable. */
b4519d39
SB
9078 else if (constructor_type != error_mark_node
9079 && constructor_fields == 0)
8b6a5902 9080 {
d033409e 9081 pedwarn_init (loc, 0,
509c9d60 9082 "excess elements in scalar initializer");
3e4093b6 9083 break;
8b6a5902
JJ
9084 }
9085 else
9086 {
916c5919 9087 if (value.value)
34cf811f 9088 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9089 strict_string, constructor_type,
a1e3b3d9
LB
9090 NULL_TREE, 1, implicit,
9091 braced_init_obstack);
3e4093b6 9092 constructor_fields = 0;
8b6a5902
JJ
9093 }
9094
3e4093b6
RS
9095 /* Handle range initializers either at this level or anywhere higher
9096 in the designator stack. */
9097 if (constructor_range_stack)
8b6a5902 9098 {
3e4093b6
RS
9099 struct constructor_range_stack *p, *range_stack;
9100 int finish = 0;
9101
9102 range_stack = constructor_range_stack;
9103 constructor_range_stack = 0;
9104 while (constructor_stack != range_stack->stack)
8b6a5902 9105 {
366de0ce 9106 gcc_assert (constructor_stack->implicit);
34cf811f 9107 process_init_element (loc,
ea58ef42
MP
9108 pop_init_level (loc, 1,
9109 braced_init_obstack),
a1e3b3d9 9110 true, braced_init_obstack);
8b6a5902 9111 }
3e4093b6
RS
9112 for (p = range_stack;
9113 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9114 p = p->prev)
8b6a5902 9115 {
366de0ce 9116 gcc_assert (constructor_stack->implicit);
34cf811f 9117 process_init_element (loc,
ea58ef42
MP
9118 pop_init_level (loc, 1,
9119 braced_init_obstack),
a1e3b3d9 9120 true, braced_init_obstack);
8b6a5902 9121 }
3e4093b6 9122
db3927fb
AH
9123 p->index = size_binop_loc (input_location,
9124 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9125 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9126 finish = 1;
9127
9128 while (1)
9129 {
9130 constructor_index = p->index;
9131 constructor_fields = p->fields;
9132 if (finish && p->range_end && p->index == p->range_start)
9133 {
9134 finish = 0;
9135 p->prev = 0;
9136 }
9137 p = p->next;
9138 if (!p)
9139 break;
ea58ef42 9140 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9141 p->stack = constructor_stack;
9142 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9143 p->index = p->range_start;
9144 }
9145
9146 if (!finish)
9147 constructor_range_stack = range_stack;
9148 continue;
8b6a5902
JJ
9149 }
9150
3e4093b6 9151 break;
8b6a5902
JJ
9152 }
9153
3e4093b6
RS
9154 constructor_range_stack = 0;
9155}
9156\f
9f0e2d86
ZW
9157/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9158 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9159 an ASM_EXPR node). */
3e4093b6 9160tree
9f0e2d86 9161build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9162{
6de9cd9a
DN
9163 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9164 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9165 return add_stmt (args);
8b6a5902
JJ
9166}
9167
9f0e2d86
ZW
9168/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9169 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9170 SIMPLE indicates whether there was anything at all after the
9171 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9172 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9173tree
c2255bc4 9174build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9175 tree clobbers, tree labels, bool simple)
e5e809f4 9176{
3e4093b6 9177 tree tail;
9f0e2d86 9178 tree args;
6de9cd9a
DN
9179 int i;
9180 const char *constraint;
74f0c611 9181 const char **oconstraints;
6de9cd9a 9182 bool allows_mem, allows_reg, is_inout;
74f0c611 9183 int ninputs, noutputs;
6de9cd9a
DN
9184
9185 ninputs = list_length (inputs);
9186 noutputs = list_length (outputs);
74f0c611
RH
9187 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9188
1c384bf1 9189 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9190
6de9cd9a
DN
9191 /* Remove output conversions that change the type but not the mode. */
9192 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9193 {
3e4093b6 9194 tree output = TREE_VALUE (tail);
74f0c611 9195
eadd3d0d
JJ
9196 output = c_fully_fold (output, false, NULL);
9197
74f0c611
RH
9198 /* ??? Really, this should not be here. Users should be using a
9199 proper lvalue, dammit. But there's a long history of using casts
9200 in the output operands. In cases like longlong.h, this becomes a
9201 primitive form of typechecking -- if the cast can be removed, then
9202 the output operand had a type of the proper width; otherwise we'll
9203 get an error. Gross, but ... */
3e4093b6 9204 STRIP_NOPS (output);
74f0c611 9205
7bd11157 9206 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9207 output = error_mark_node;
8b6a5902 9208
5544530a
PB
9209 if (output != error_mark_node
9210 && (TREE_READONLY (output)
9211 || TYPE_READONLY (TREE_TYPE (output))
9212 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9213 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9214 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9215 readonly_error (loc, output, lv_asm);
5544530a 9216
6de9cd9a 9217 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9218 oconstraints[i] = constraint;
9219
9220 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9221 &allows_mem, &allows_reg, &is_inout))
9222 {
9223 /* If the operand is going to end up in memory,
9224 mark it addressable. */
9225 if (!allows_reg && !c_mark_addressable (output))
9226 output = error_mark_node;
bae5cddf
JJ
9227 if (!(!allows_reg && allows_mem)
9228 && output != error_mark_node
9229 && VOID_TYPE_P (TREE_TYPE (output)))
9230 {
9231 error_at (loc, "invalid use of void expression");
9232 output = error_mark_node;
9233 }
74f0c611
RH
9234 }
9235 else
c22cacf3 9236 output = error_mark_node;
3e4093b6 9237
74f0c611 9238 TREE_VALUE (tail) = output;
8b6a5902 9239 }
3e4093b6 9240
74f0c611
RH
9241 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9242 {
9243 tree input;
9244
9245 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9246 input = TREE_VALUE (tail);
9247
74f0c611
RH
9248 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9249 oconstraints, &allows_mem, &allows_reg))
9250 {
9251 /* If the operand is going to end up in memory,
9252 mark it addressable. */
b4c33883
AP
9253 if (!allows_reg && allows_mem)
9254 {
eadd3d0d
JJ
9255 input = c_fully_fold (input, false, NULL);
9256
b4c33883
AP
9257 /* Strip the nops as we allow this case. FIXME, this really
9258 should be rejected or made deprecated. */
9259 STRIP_NOPS (input);
9260 if (!c_mark_addressable (input))
9261 input = error_mark_node;
bae5cddf 9262 }
eadd3d0d 9263 else
bae5cddf 9264 {
eadd3d0d
JJ
9265 struct c_expr expr;
9266 memset (&expr, 0, sizeof (expr));
9267 expr.value = input;
267bac10 9268 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9269 input = c_fully_fold (expr.value, false, NULL);
9270
9271 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9272 {
9273 error_at (loc, "invalid use of void expression");
9274 input = error_mark_node;
9275 }
bae5cddf 9276 }
74f0c611
RH
9277 }
9278 else
9279 input = error_mark_node;
9280
9281 TREE_VALUE (tail) = input;
9282 }
3e4093b6 9283
1c384bf1
RH
9284 /* ASMs with labels cannot have outputs. This should have been
9285 enforced by the parser. */
9286 gcc_assert (outputs == NULL || labels == NULL);
9287
9288 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9289
5544530a
PB
9290 /* asm statements without outputs, including simple ones, are treated
9291 as volatile. */
9292 ASM_INPUT_P (args) = simple;
9293 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9294
9f0e2d86 9295 return args;
e5e809f4 9296}
3e4093b6 9297\f
c2255bc4
AH
9298/* Generate a goto statement to LABEL. LOC is the location of the
9299 GOTO. */
506e2710
RH
9300
9301tree
c2255bc4 9302c_finish_goto_label (location_t loc, tree label)
506e2710 9303{
e1b7793c 9304 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9305 if (!decl)
9306 return NULL_TREE;
506e2710 9307 TREE_USED (decl) = 1;
c2255bc4
AH
9308 {
9309 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9310 SET_EXPR_LOCATION (t, loc);
9311 return add_stmt (t);
9312 }
506e2710
RH
9313}
9314
c2255bc4
AH
9315/* Generate a computed goto statement to EXPR. LOC is the location of
9316 the GOTO. */
506e2710
RH
9317
9318tree
c2255bc4 9319c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9320{
c2255bc4 9321 tree t;
c1771a20 9322 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9323 expr = c_fully_fold (expr, false, NULL);
506e2710 9324 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9325 t = build1 (GOTO_EXPR, void_type_node, expr);
9326 SET_EXPR_LOCATION (t, loc);
9327 return add_stmt (t);
506e2710
RH
9328}
9329
5088b058 9330/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9331 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9332 the location of the return statement, or the location of the expression,
9333 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9334 is the original type of RETVAL. */
de520661 9335
506e2710 9336tree
c2255bc4 9337c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9338{
0c9b182b
JJ
9339 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9340 bool no_warning = false;
928c19bb 9341 bool npc = false;
36536d79 9342 size_t rank = 0;
3e4093b6
RS
9343
9344 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9345 warning_at (loc, 0,
9346 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9347
b72271b9 9348 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9349 {
9350 /* Array notations are allowed in a return statement if it is inside a
9351 built-in array notation reduction function. */
9352 if (!find_rank (loc, retval, retval, false, &rank))
9353 return error_mark_node;
9354 if (rank >= 1)
9355 {
9356 error_at (loc, "array notation expression cannot be used as a "
9357 "return value");
9358 return error_mark_node;
9359 }
9360 }
3af9c5e9 9361 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9362 {
9363 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9364 "allowed");
9365 return error_mark_node;
9366 }
928c19bb
JM
9367 if (retval)
9368 {
8ce94e44 9369 tree semantic_type = NULL_TREE;
928c19bb 9370 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9371 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9372 {
9373 semantic_type = TREE_TYPE (retval);
9374 retval = TREE_OPERAND (retval, 0);
9375 }
928c19bb 9376 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9377 if (semantic_type)
9378 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9379 }
9380
3e4093b6 9381 if (!retval)
de520661 9382 {
3e4093b6
RS
9383 current_function_returns_null = 1;
9384 if ((warn_return_type || flag_isoc99)
9385 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9386 {
35aff4fb
MP
9387 if (flag_isoc99)
9388 pedwarn (loc, 0, "%<return%> with no value, in "
9389 "function returning non-void");
9390 else
9391 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9392 "in function returning non-void");
0c9b182b
JJ
9393 no_warning = true;
9394 }
400fbf9f 9395 }
3e4093b6 9396 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9397 {
3e4093b6 9398 current_function_returns_null = 1;
2397c575 9399 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9400 pedwarn (loc, 0,
509c9d60 9401 "%<return%> with a value, in function returning void");
b8698a0f 9402 else
c1771a20 9403 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9404 "%<return%> with expression, in function returning void");
de520661 9405 }
3e4093b6 9406 else
de520661 9407 {
68fca595
MP
9408 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9409 retval, origtype, ic_return,
c2255bc4 9410 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9411 tree res = DECL_RESULT (current_function_decl);
9412 tree inner;
9feb29df 9413 bool save;
3e4093b6
RS
9414
9415 current_function_returns_value = 1;
9416 if (t == error_mark_node)
506e2710 9417 return NULL_TREE;
3e4093b6 9418
9feb29df
JJ
9419 save = in_late_binary_op;
9420 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9421 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9422 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9423 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9424 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9425 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9426 in_late_binary_op = true;
3e4093b6 9427 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9428 in_late_binary_op = save;
3e4093b6
RS
9429
9430 /* Strip any conversions, additions, and subtractions, and see if
9431 we are returning the address of a local variable. Warn if so. */
9432 while (1)
8b6a5902 9433 {
3e4093b6 9434 switch (TREE_CODE (inner))
8b6a5902 9435 {
849421a3
JJ
9436 CASE_CONVERT:
9437 case NON_LVALUE_EXPR:
3e4093b6 9438 case PLUS_EXPR:
849421a3 9439 case POINTER_PLUS_EXPR:
3e4093b6
RS
9440 inner = TREE_OPERAND (inner, 0);
9441 continue;
9442
9443 case MINUS_EXPR:
9444 /* If the second operand of the MINUS_EXPR has a pointer
9445 type (or is converted from it), this may be valid, so
9446 don't give a warning. */
9447 {
9448 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9449
3f75a254 9450 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9451 && (CONVERT_EXPR_P (op1)
9452 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9453 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9454
3e4093b6
RS
9455 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9456 break;
8b6a5902 9457
3e4093b6
RS
9458 inner = TREE_OPERAND (inner, 0);
9459 continue;
9460 }
400fbf9f 9461
3e4093b6
RS
9462 case ADDR_EXPR:
9463 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9464
6615c446 9465 while (REFERENCE_CLASS_P (inner)
22d03525 9466 && !INDIRECT_REF_P (inner))
3e4093b6 9467 inner = TREE_OPERAND (inner, 0);
8b6a5902 9468
a2f1f4c3 9469 if (DECL_P (inner)
3f75a254
JM
9470 && !DECL_EXTERNAL (inner)
9471 && !TREE_STATIC (inner)
3e4093b6 9472 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9473 {
9474 if (TREE_CODE (inner) == LABEL_DECL)
9475 warning_at (loc, OPT_Wreturn_local_addr,
9476 "function returns address of label");
9477 else
b4dfdc11
MG
9478 {
9479 warning_at (loc, OPT_Wreturn_local_addr,
9480 "function returns address of local variable");
9481 tree zero = build_zero_cst (TREE_TYPE (res));
9482 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9483 }
19fc9faa 9484 }
3e4093b6 9485 break;
8b6a5902 9486
3e4093b6
RS
9487 default:
9488 break;
9489 }
de520661 9490
3e4093b6
RS
9491 break;
9492 }
9493
53fb4de3 9494 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9495 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9496
9497 if (warn_sequence_point)
9498 verify_sequence_points (retval);
de520661 9499 }
8b6a5902 9500
c2255bc4 9501 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9502 TREE_NO_WARNING (ret_stmt) |= no_warning;
9503 return add_stmt (ret_stmt);
de520661 9504}
3e4093b6
RS
9505\f
9506struct c_switch {
604f5adf
ILT
9507 /* The SWITCH_EXPR being built. */
9508 tree switch_expr;
a6c0a76c 9509
89dbed81 9510 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9511 default conversion is applied. */
9512 tree orig_type;
9513
3e4093b6
RS
9514 /* A splay-tree mapping the low element of a case range to the high
9515 element, or NULL_TREE if there is no high element. Used to
9516 determine whether or not a new case label duplicates an old case
9517 label. We need a tree, rather than simply a hash table, because
9518 of the GNU case range extension. */
9519 splay_tree cases;
a6c0a76c 9520
e1b7793c
ILT
9521 /* The bindings at the point of the switch. This is used for
9522 warnings crossing decls when branching to a case label. */
9523 struct c_spot_bindings *bindings;
187230a7 9524
3e4093b6
RS
9525 /* The next node on the stack. */
9526 struct c_switch *next;
b155cfd9
MP
9527
9528 /* Remember whether the controlling expression had boolean type
9529 before integer promotions for the sake of -Wswitch-bool. */
9530 bool bool_cond_p;
9531
9532 /* Remember whether there was a case value that is outside the
9533 range of the ORIG_TYPE. */
9534 bool outside_range_p;
3e4093b6 9535};
400fbf9f 9536
3e4093b6
RS
9537/* A stack of the currently active switch statements. The innermost
9538 switch statement is on the top of the stack. There is no need to
9539 mark the stack for garbage collection because it is only active
9540 during the processing of the body of a function, and we never
9541 collect at that point. */
de520661 9542
506e2710 9543struct c_switch *c_switch_stack;
de520661 9544
3e4093b6 9545/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9546 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 9547 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 9548 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 9549
3e4093b6 9550tree
c2255bc4
AH
9551c_start_case (location_t switch_loc,
9552 location_t switch_cond_loc,
fedfecef 9553 tree exp, bool explicit_cast_p)
de520661 9554{
c58e8676 9555 tree orig_type = error_mark_node;
b155cfd9 9556 bool bool_cond_p = false;
3e4093b6 9557 struct c_switch *cs;
2f6e4e97 9558
3e4093b6 9559 if (exp != error_mark_node)
de520661 9560 {
3e4093b6
RS
9561 orig_type = TREE_TYPE (exp);
9562
c58e8676 9563 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9564 {
c58e8676
VR
9565 if (orig_type != error_mark_node)
9566 {
c2255bc4 9567 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9568 orig_type = error_mark_node;
9569 }
3e4093b6 9570 exp = integer_zero_node;
de520661 9571 }
3e4093b6 9572 else
de520661 9573 {
c58e8676 9574 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9575 tree e = exp;
9576
9577 /* Warn if the condition has boolean value. */
9578 while (TREE_CODE (e) == COMPOUND_EXPR)
9579 e = TREE_OPERAND (e, 1);
9580
9581 if ((TREE_CODE (type) == BOOLEAN_TYPE
9582 || truth_value_p (TREE_CODE (e)))
9583 /* Explicit cast to int suppresses this warning. */
9584 && !(TREE_CODE (type) == INTEGER_TYPE
9585 && explicit_cast_p))
b155cfd9 9586 bool_cond_p = true;
8b6a5902 9587
8400e75e 9588 if (!in_system_header_at (input_location)
3e4093b6
RS
9589 && (type == long_integer_type_node
9590 || type == long_unsigned_type_node))
c2255bc4
AH
9591 warning_at (switch_cond_loc,
9592 OPT_Wtraditional, "%<long%> switch expression not "
9593 "converted to %<int%> in ISO C");
8b6a5902 9594
928c19bb 9595 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9596 exp = default_conversion (exp);
ca085fd7
MLI
9597
9598 if (warn_sequence_point)
9599 verify_sequence_points (exp);
3e4093b6
RS
9600 }
9601 }
9602
604f5adf 9603 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9604 cs = XNEW (struct c_switch);
604f5adf 9605 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9606 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9607 cs->orig_type = orig_type;
3e4093b6 9608 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9609 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
9610 cs->bool_cond_p = bool_cond_p;
9611 cs->outside_range_p = false;
506e2710
RH
9612 cs->next = c_switch_stack;
9613 c_switch_stack = cs;
3e4093b6 9614
604f5adf 9615 return add_stmt (cs->switch_expr);
3e4093b6
RS
9616}
9617
c2255bc4 9618/* Process a case label at location LOC. */
3e4093b6
RS
9619
9620tree
c2255bc4 9621do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9622{
9623 tree label = NULL_TREE;
9624
17cede2e
JM
9625 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9626 {
9627 low_value = c_fully_fold (low_value, false, NULL);
9628 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9629 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9630 "case label is not an integer constant expression");
9631 }
9632
9633 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9634 {
9635 high_value = c_fully_fold (high_value, false, NULL);
9636 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9637 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9638 "case label is not an integer constant expression");
9639 }
9640
e1b7793c 9641 if (c_switch_stack == NULL)
187230a7
JM
9642 {
9643 if (low_value)
e1b7793c 9644 error_at (loc, "case label not within a switch statement");
187230a7 9645 else
e1b7793c
ILT
9646 error_at (loc, "%<default%> label not within a switch statement");
9647 return NULL_TREE;
187230a7 9648 }
de520661 9649
e1b7793c
ILT
9650 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9651 EXPR_LOCATION (c_switch_stack->switch_expr),
9652 loc))
9653 return NULL_TREE;
9654
9655 label = c_add_case_label (loc, c_switch_stack->cases,
9656 SWITCH_COND (c_switch_stack->switch_expr),
9657 c_switch_stack->orig_type,
b155cfd9
MP
9658 low_value, high_value,
9659 &c_switch_stack->outside_range_p);
e1b7793c
ILT
9660 if (label == error_mark_node)
9661 label = NULL_TREE;
3e4093b6
RS
9662 return label;
9663}
de520661 9664
083e891e
MP
9665/* Finish the switch statement. TYPE is the original type of the
9666 controlling expression of the switch, or NULL_TREE. */
de520661 9667
3e4093b6 9668void
083e891e 9669c_finish_case (tree body, tree type)
3e4093b6 9670{
506e2710 9671 struct c_switch *cs = c_switch_stack;
fbc315db 9672 location_t switch_location;
3e4093b6 9673
604f5adf 9674 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9675
6de9cd9a 9676 /* Emit warnings as needed. */
c2255bc4 9677 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9678 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9679 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
9680 SWITCH_COND (cs->switch_expr),
9681 cs->bool_cond_p, cs->outside_range_p);
6de9cd9a 9682
3e4093b6 9683 /* Pop the stack. */
506e2710 9684 c_switch_stack = cs->next;
3e4093b6 9685 splay_tree_delete (cs->cases);
e1b7793c 9686 c_release_switch_bindings (cs->bindings);
5d038c4c 9687 XDELETE (cs);
de520661 9688}
325c3691 9689\f
506e2710
RH
9690/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9691 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9692 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9693 statement, and was not surrounded with parenthesis. */
325c3691 9694
9e51cf9d 9695void
506e2710
RH
9696c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9697 tree else_block, bool nested_if)
325c3691 9698{
506e2710 9699 tree stmt;
325c3691 9700
25c22937
BI
9701 /* If the condition has array notations, then the rank of the then_block and
9702 else_block must be either 0 or be equal to the rank of the condition. If
9703 the condition does not have array notations then break them up as it is
9704 broken up in a normal expression. */
b72271b9 9705 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9706 {
9707 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9708 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9709 return;
9710 if (then_block
9711 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9712 return;
9713 if (else_block
9714 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9715 return;
9716 if (cond_rank != then_rank && then_rank != 0)
9717 {
9718 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9719 " and the then-block");
9720 return;
9721 }
9722 else if (cond_rank != else_rank && else_rank != 0)
9723 {
9724 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9725 " and the else-block");
9726 return;
9727 }
9728 }
506e2710
RH
9729 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9730 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9731 {
506e2710 9732 tree inner_if = then_block;
16865eaa 9733
61ada8ae 9734 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9735 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9736 it might not be exactly THEN_BLOCK, but should be the last
9737 non-container statement within. */
9738 while (1)
9739 switch (TREE_CODE (inner_if))
9740 {
9741 case COND_EXPR:
9742 goto found;
9743 case BIND_EXPR:
9744 inner_if = BIND_EXPR_BODY (inner_if);
9745 break;
9746 case STATEMENT_LIST:
9747 inner_if = expr_last (then_block);
9748 break;
9749 case TRY_FINALLY_EXPR:
9750 case TRY_CATCH_EXPR:
9751 inner_if = TREE_OPERAND (inner_if, 0);
9752 break;
9753 default:
366de0ce 9754 gcc_unreachable ();
506e2710
RH
9755 }
9756 found:
16865eaa 9757
506e2710 9758 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9759 warning_at (if_locus, OPT_Wparentheses,
9760 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9761 }
16865eaa 9762
2214de30 9763 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9764 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9765 add_stmt (stmt);
325c3691
RH
9766}
9767
506e2710
RH
9768/* Emit a general-purpose loop construct. START_LOCUS is the location of
9769 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9770 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9771 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9772 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9773
9774void
506e2710
RH
9775c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9776 tree blab, tree clab, bool cond_is_first)
325c3691 9777{
506e2710
RH
9778 tree entry = NULL, exit = NULL, t;
9779
e5e44252
AK
9780 /* In theory could forbid cilk spawn for loop increment expression,
9781 but it should work just fine. */
36536d79 9782
28af952a
RS
9783 /* If the condition is zero don't generate a loop construct. */
9784 if (cond && integer_zerop (cond))
9785 {
9786 if (cond_is_first)
9787 {
9788 t = build_and_jump (&blab);
9789 SET_EXPR_LOCATION (t, start_locus);
9790 add_stmt (t);
9791 }
9792 }
9793 else
506e2710
RH
9794 {
9795 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9796
506e2710 9797 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9798 out of the loop, or to the top of it. If there's no exit condition,
9799 then we just build a jump back to the top. */
506e2710 9800 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9801
28af952a 9802 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9803 {
9804 /* Canonicalize the loop condition to the end. This means
9805 generating a branch to the loop condition. Reuse the
9806 continue label, if possible. */
9807 if (cond_is_first)
9808 {
9809 if (incr || !clab)
9810 {
9811 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9812 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9813 }
9814 else
9815 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9816 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9817 add_stmt (t);
9818 }
9819
506e2710 9820 t = build_and_jump (&blab);
506e2710 9821 if (cond_is_first)
db3927fb
AH
9822 exit = fold_build3_loc (start_locus,
9823 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9824 else
db3927fb
AH
9825 exit = fold_build3_loc (input_location,
9826 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9827 }
9828
506e2710
RH
9829 add_stmt (top);
9830 }
c22cacf3 9831
506e2710
RH
9832 if (body)
9833 add_stmt (body);
9834 if (clab)
9835 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9836 if (incr)
9837 add_stmt (incr);
9838 if (entry)
9839 add_stmt (entry);
9840 if (exit)
9841 add_stmt (exit);
9842 if (blab)
9843 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9844}
325c3691
RH
9845
9846tree
c2255bc4 9847c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9848{
089efaa4 9849 bool skip;
506e2710 9850 tree label = *label_p;
325c3691 9851
089efaa4
ILT
9852 /* In switch statements break is sometimes stylistically used after
9853 a return statement. This can lead to spurious warnings about
9854 control reaching the end of a non-void function when it is
9855 inlined. Note that we are calling block_may_fallthru with
9856 language specific tree nodes; this works because
9857 block_may_fallthru returns true when given something it does not
9858 understand. */
9859 skip = !block_may_fallthru (cur_stmt_list);
9860
506e2710 9861 if (!label)
089efaa4
ILT
9862 {
9863 if (!skip)
c2255bc4 9864 *label_p = label = create_artificial_label (loc);
089efaa4 9865 }
953ff289
DN
9866 else if (TREE_CODE (label) == LABEL_DECL)
9867 ;
9868 else switch (TREE_INT_CST_LOW (label))
506e2710 9869 {
953ff289 9870 case 0:
506e2710 9871 if (is_break)
c2255bc4 9872 error_at (loc, "break statement not within loop or switch");
506e2710 9873 else
c2255bc4 9874 error_at (loc, "continue statement not within a loop");
506e2710 9875 return NULL_TREE;
953ff289
DN
9876
9877 case 1:
9878 gcc_assert (is_break);
c2255bc4 9879 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9880 return NULL_TREE;
9881
c02065fc
AH
9882 case 2:
9883 if (is_break)
9884 error ("break statement within %<#pragma simd%> loop body");
9885 else
9886 error ("continue statement within %<#pragma simd%> loop body");
9887 return NULL_TREE;
9888
953ff289
DN
9889 default:
9890 gcc_unreachable ();
506e2710 9891 }
325c3691 9892
089efaa4
ILT
9893 if (skip)
9894 return NULL_TREE;
9895
2e28e797
JH
9896 if (!is_break)
9897 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9898
53fb4de3 9899 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9900}
9901
506e2710 9902/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9903
9904static void
c2255bc4 9905emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9906{
e6b5a630
RH
9907 if (expr == error_mark_node)
9908 ;
9909 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9910 {
9911 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9912 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9913 }
789eadcd
MP
9914 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9915 {
9916 tree r = expr;
9917 location_t cloc = loc;
9918 while (TREE_CODE (r) == COMPOUND_EXPR)
9919 {
9920 if (EXPR_HAS_LOCATION (r))
9921 cloc = EXPR_LOCATION (r);
9922 r = TREE_OPERAND (r, 1);
9923 }
9924 if (!TREE_SIDE_EFFECTS (r)
9925 && !VOID_TYPE_P (TREE_TYPE (r))
9926 && !CONVERT_EXPR_P (r)
cc28fc7f 9927 && !TREE_NO_WARNING (r)
789eadcd
MP
9928 && !TREE_NO_WARNING (expr))
9929 warning_at (cloc, OPT_Wunused_value,
9930 "right-hand operand of comma expression has no effect");
9931 }
27f33b15 9932 else
c2255bc4 9933 warn_if_unused_value (expr, loc);
3a5b9284
RH
9934}
9935
506e2710 9936/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9937 diagnostics, but do not call ADD_STMT. LOC is the location of the
9938 statement. */
3a5b9284 9939
506e2710 9940tree
c2255bc4 9941c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9942{
056928b2
JJ
9943 tree exprv;
9944
3a5b9284 9945 if (!expr)
506e2710 9946 return NULL_TREE;
3a5b9284 9947
928c19bb
JM
9948 expr = c_fully_fold (expr, false, NULL);
9949
3a5b9284
RH
9950 if (warn_sequence_point)
9951 verify_sequence_points (expr);
9952
9953 if (TREE_TYPE (expr) != error_mark_node
9954 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9955 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9956 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9957
9958 /* If we're not processing a statement expression, warn about unused values.
9959 Warnings for statement expressions will be emitted later, once we figure
9960 out which is the result. */
9961 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9962 && warn_unused_value)
c2255bc4 9963 emit_side_effect_warnings (loc, expr);
3a5b9284 9964
056928b2
JJ
9965 exprv = expr;
9966 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9967 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9968 while (CONVERT_EXPR_P (exprv))
9969 exprv = TREE_OPERAND (exprv, 0);
9970 if (DECL_P (exprv)
9971 || handled_component_p (exprv)
9972 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9973 mark_exp_read (exprv);
fa8351f8 9974
3a5b9284
RH
9975 /* If the expression is not of a type to which we cannot assign a line
9976 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9977 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9978 {
9979 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9980 SET_EXPR_LOCATION (expr, loc);
9981 }
506e2710
RH
9982
9983 return expr;
9984}
9985
c2255bc4
AH
9986/* Emit an expression as a statement. LOC is the location of the
9987 expression. */
506e2710
RH
9988
9989tree
c2255bc4 9990c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9991{
9992 if (expr)
c2255bc4 9993 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9994 else
9995 return NULL;
3a5b9284
RH
9996}
9997
9998/* Do the opposite and emit a statement as an expression. To begin,
9999 create a new binding level and return it. */
325c3691
RH
10000
10001tree
10002c_begin_stmt_expr (void)
10003{
10004 tree ret;
10005
10006 /* We must force a BLOCK for this level so that, if it is not expanded
10007 later, there is a way to turn off the entire subtree of blocks that
10008 are contained in it. */
10009 keep_next_level ();
10010 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10011
10012 c_bindings_start_stmt_expr (c_switch_stack == NULL
10013 ? NULL
10014 : c_switch_stack->bindings);
325c3691
RH
10015
10016 /* Mark the current statement list as belonging to a statement list. */
10017 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10018
10019 return ret;
10020}
10021
c2255bc4
AH
10022/* LOC is the location of the compound statement to which this body
10023 belongs. */
10024
325c3691 10025tree
c2255bc4 10026c_finish_stmt_expr (location_t loc, tree body)
325c3691 10027{
3a5b9284 10028 tree last, type, tmp, val;
325c3691
RH
10029 tree *last_p;
10030
c2255bc4 10031 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10032
10033 c_bindings_end_stmt_expr (c_switch_stack == NULL
10034 ? NULL
10035 : c_switch_stack->bindings);
325c3691 10036
3a5b9284
RH
10037 /* Locate the last statement in BODY. See c_end_compound_stmt
10038 about always returning a BIND_EXPR. */
10039 last_p = &BIND_EXPR_BODY (body);
10040 last = BIND_EXPR_BODY (body);
10041
10042 continue_searching:
325c3691
RH
10043 if (TREE_CODE (last) == STATEMENT_LIST)
10044 {
3a5b9284
RH
10045 tree_stmt_iterator i;
10046
10047 /* This can happen with degenerate cases like ({ }). No value. */
10048 if (!TREE_SIDE_EFFECTS (last))
10049 return body;
10050
10051 /* If we're supposed to generate side effects warnings, process
10052 all of the statements except the last. */
27f33b15 10053 if (warn_unused_value)
325c3691 10054 {
3a5b9284 10055 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10056 {
10057 location_t tloc;
10058 tree t = tsi_stmt (i);
10059
10060 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10061 emit_side_effect_warnings (tloc, t);
10062 }
325c3691
RH
10063 }
10064 else
3a5b9284
RH
10065 i = tsi_last (last);
10066 last_p = tsi_stmt_ptr (i);
10067 last = *last_p;
325c3691
RH
10068 }
10069
3a5b9284
RH
10070 /* If the end of the list is exception related, then the list was split
10071 by a call to push_cleanup. Continue searching. */
10072 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10073 || TREE_CODE (last) == TRY_CATCH_EXPR)
10074 {
10075 last_p = &TREE_OPERAND (last, 0);
10076 last = *last_p;
10077 goto continue_searching;
10078 }
10079
26d8af35
JM
10080 if (last == error_mark_node)
10081 return last;
10082
3a5b9284
RH
10083 /* In the case that the BIND_EXPR is not necessary, return the
10084 expression out from inside it. */
26d8af35
JM
10085 if (last == BIND_EXPR_BODY (body)
10086 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10087 {
928c19bb
JM
10088 /* Even if this looks constant, do not allow it in a constant
10089 expression. */
e5a94231 10090 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10091 /* Do not warn if the return value of a statement expression is
10092 unused. */
928c19bb 10093 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10094 return last;
10095 }
325c3691
RH
10096
10097 /* Extract the type of said expression. */
10098 type = TREE_TYPE (last);
325c3691 10099
3a5b9284
RH
10100 /* If we're not returning a value at all, then the BIND_EXPR that
10101 we already have is a fine expression to return. */
10102 if (!type || VOID_TYPE_P (type))
10103 return body;
10104
10105 /* Now that we've located the expression containing the value, it seems
10106 silly to make voidify_wrapper_expr repeat the process. Create a
10107 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10108 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10109
10110 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10111 tree_expr_nonnegative_p giving up immediately. */
10112 val = last;
10113 if (TREE_CODE (val) == NOP_EXPR
10114 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10115 val = TREE_OPERAND (val, 0);
10116
53fb4de3 10117 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10118 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10119
c2255bc4
AH
10120 {
10121 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10122 SET_EXPR_LOCATION (t, loc);
10123 return t;
10124 }
325c3691
RH
10125}
10126\f
10127/* Begin and end compound statements. This is as simple as pushing
10128 and popping new statement lists from the tree. */
10129
10130tree
10131c_begin_compound_stmt (bool do_scope)
10132{
10133 tree stmt = push_stmt_list ();
10134 if (do_scope)
4dfa0342 10135 push_scope ();
325c3691
RH
10136 return stmt;
10137}
10138
c2255bc4
AH
10139/* End a compound statement. STMT is the statement. LOC is the
10140 location of the compound statement-- this is usually the location
10141 of the opening brace. */
10142
325c3691 10143tree
c2255bc4 10144c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10145{
10146 tree block = NULL;
10147
10148 if (do_scope)
10149 {
10150 if (c_dialect_objc ())
10151 objc_clear_super_receiver ();
10152 block = pop_scope ();
10153 }
10154
10155 stmt = pop_stmt_list (stmt);
c2255bc4 10156 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10157
10158 /* If this compound statement is nested immediately inside a statement
10159 expression, then force a BIND_EXPR to be created. Otherwise we'll
10160 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10161 STATEMENT_LISTs merge, and thus we can lose track of what statement
10162 was really last. */
38e01f9e 10163 if (building_stmt_list_p ()
325c3691
RH
10164 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10165 && TREE_CODE (stmt) != BIND_EXPR)
10166 {
53fb4de3 10167 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10168 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10169 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10170 }
10171
10172 return stmt;
10173}
5a508662
RH
10174
10175/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10176 when the current scope is exited. EH_ONLY is true when this is not
10177 meant to apply to normal control flow transfer. */
10178
10179void
c2255bc4 10180push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10181{
3a5b9284
RH
10182 enum tree_code code;
10183 tree stmt, list;
10184 bool stmt_expr;
10185
10186 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10187 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10188 add_stmt (stmt);
3a5b9284
RH
10189 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10190 list = push_stmt_list ();
10191 TREE_OPERAND (stmt, 0) = list;
10192 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10193}
325c3691 10194\f
3e4093b6
RS
10195/* Build a binary-operation expression without default conversions.
10196 CODE is the kind of expression to build.
ba47d38d 10197 LOCATION is the operator's location.
3e4093b6
RS
10198 This function differs from `build' in several ways:
10199 the data type of the result is computed and recorded in it,
10200 warnings are generated if arg data types are invalid,
10201 special handling for addition and subtraction of pointers is known,
10202 and some optimization is done (operations on narrow ints
10203 are done in the narrower type when that gives the same result).
10204 Constant folding is also done before the result is returned.
de520661 10205
3e4093b6
RS
10206 Note that the operands will never have enumeral types, or function
10207 or array types, because either they will have the default conversions
10208 performed or they have both just been converted to some other type in which
10209 the arithmetic is to be done. */
10210
10211tree
ba47d38d
AH
10212build_binary_op (location_t location, enum tree_code code,
10213 tree orig_op0, tree orig_op1, int convert_p)
de520661 10214{
8ce94e44
JM
10215 tree type0, type1, orig_type0, orig_type1;
10216 tree eptype;
3e4093b6
RS
10217 enum tree_code code0, code1;
10218 tree op0, op1;
c9f9eb5d 10219 tree ret = error_mark_node;
4de67c26 10220 const char *invalid_op_diag;
4d84fe7c 10221 bool op0_int_operands, op1_int_operands;
928c19bb 10222 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10223
3e4093b6
RS
10224 /* Expression code to give to the expression when it is built.
10225 Normally this is CODE, which is what the caller asked for,
10226 but in some special cases we change it. */
10227 enum tree_code resultcode = code;
8b6a5902 10228
3e4093b6
RS
10229 /* Data type in which the computation is to be performed.
10230 In the simplest cases this is the common type of the arguments. */
10231 tree result_type = NULL;
10232
8ce94e44
JM
10233 /* When the computation is in excess precision, the type of the
10234 final EXCESS_PRECISION_EXPR. */
2d2e923f 10235 tree semantic_result_type = NULL;
8ce94e44 10236
3e4093b6
RS
10237 /* Nonzero means operands have already been type-converted
10238 in whatever way is necessary.
10239 Zero means they need to be converted to RESULT_TYPE. */
10240 int converted = 0;
10241
10242 /* Nonzero means create the expression with this type, rather than
10243 RESULT_TYPE. */
10244 tree build_type = 0;
10245
10246 /* Nonzero means after finally constructing the expression
10247 convert it to this type. */
10248 tree final_type = 0;
10249
10250 /* Nonzero if this is an operation like MIN or MAX which can
10251 safely be computed in short if both args are promoted shorts.
10252 Also implies COMMON.
10253 -1 indicates a bitwise operation; this makes a difference
10254 in the exact conditions for when it is safe to do the operation
10255 in a narrower mode. */
10256 int shorten = 0;
10257
10258 /* Nonzero if this is a comparison operation;
10259 if both args are promoted shorts, compare the original shorts.
10260 Also implies COMMON. */
10261 int short_compare = 0;
10262
10263 /* Nonzero if this is a right-shift operation, which can be computed on the
10264 original short and then promoted if the operand is a promoted short. */
10265 int short_shift = 0;
10266
10267 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10268 int common = 0;
10269
58393038
ZL
10270 /* True means types are compatible as far as ObjC is concerned. */
10271 bool objc_ok;
10272
8ce94e44
JM
10273 /* True means this is an arithmetic operation that may need excess
10274 precision. */
10275 bool may_need_excess_precision;
10276
180f8dbb
JM
10277 /* True means this is a boolean operation that converts both its
10278 operands to truth-values. */
10279 bool boolean_op = false;
10280
de5a5fa1
MP
10281 /* Remember whether we're doing / or %. */
10282 bool doing_div_or_mod = false;
10283
10284 /* Remember whether we're doing << or >>. */
10285 bool doing_shift = false;
10286
10287 /* Tree holding instrumentation expression. */
10288 tree instrument_expr = NULL;
10289
ba47d38d
AH
10290 if (location == UNKNOWN_LOCATION)
10291 location = input_location;
10292
4d84fe7c
JM
10293 op0 = orig_op0;
10294 op1 = orig_op1;
10295
10296 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10297 if (op0_int_operands)
10298 op0 = remove_c_maybe_const_expr (op0);
10299 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10300 if (op1_int_operands)
10301 op1 = remove_c_maybe_const_expr (op1);
10302 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10303 if (int_operands)
10304 {
10305 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10306 && TREE_CODE (orig_op1) == INTEGER_CST);
10307 int_const = (int_const_or_overflow
10308 && !TREE_OVERFLOW (orig_op0)
10309 && !TREE_OVERFLOW (orig_op1));
10310 }
10311 else
10312 int_const = int_const_or_overflow = false;
10313
0e3a99ae 10314 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 10315 if (convert_p
31521951 10316 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 10317 {
4d84fe7c
JM
10318 op0 = default_conversion (op0);
10319 op1 = default_conversion (op1);
790e9490
RS
10320 }
10321
36536d79
BI
10322 /* When Cilk Plus is enabled and there are array notations inside op0, then
10323 we check to see if there are builtin array notation functions. If
10324 so, then we take on the type of the array notation inside it. */
b72271b9 10325 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10326 orig_type0 = type0 = find_correct_array_notation_type (op0);
10327 else
10328 orig_type0 = type0 = TREE_TYPE (op0);
10329
b72271b9 10330 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10331 orig_type1 = type1 = find_correct_array_notation_type (op1);
10332 else
10333 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10334
3e4093b6
RS
10335 /* The expression codes of the data types of the arguments tell us
10336 whether the arguments are integers, floating, pointers, etc. */
10337 code0 = TREE_CODE (type0);
10338 code1 = TREE_CODE (type1);
10339
10340 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10341 STRIP_TYPE_NOPS (op0);
10342 STRIP_TYPE_NOPS (op1);
10343
10344 /* If an error was already reported for one of the arguments,
10345 avoid reporting another error. */
10346
10347 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10348 return error_mark_node;
10349
4de67c26
JM
10350 if ((invalid_op_diag
10351 = targetm.invalid_binary_op (code, type0, type1)))
10352 {
ba47d38d 10353 error_at (location, invalid_op_diag);
4de67c26
JM
10354 return error_mark_node;
10355 }
10356
8ce94e44
JM
10357 switch (code)
10358 {
10359 case PLUS_EXPR:
10360 case MINUS_EXPR:
10361 case MULT_EXPR:
10362 case TRUNC_DIV_EXPR:
10363 case CEIL_DIV_EXPR:
10364 case FLOOR_DIV_EXPR:
10365 case ROUND_DIV_EXPR:
10366 case EXACT_DIV_EXPR:
10367 may_need_excess_precision = true;
10368 break;
10369 default:
10370 may_need_excess_precision = false;
10371 break;
10372 }
10373 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10374 {
10375 op0 = TREE_OPERAND (op0, 0);
10376 type0 = TREE_TYPE (op0);
10377 }
10378 else if (may_need_excess_precision
10379 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10380 {
10381 type0 = eptype;
10382 op0 = convert (eptype, op0);
10383 }
10384 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10385 {
10386 op1 = TREE_OPERAND (op1, 0);
10387 type1 = TREE_TYPE (op1);
10388 }
10389 else if (may_need_excess_precision
10390 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10391 {
10392 type1 = eptype;
10393 op1 = convert (eptype, op1);
10394 }
10395
58393038
ZL
10396 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10397
0e3a99ae
AS
10398 /* In case when one of the operands of the binary operation is
10399 a vector and another is a scalar -- convert scalar to vector. */
10400 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10401 {
a212e43f
MG
10402 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10403 true);
f90e8e2e 10404
0e3a99ae
AS
10405 switch (convert_flag)
10406 {
10407 case stv_error:
10408 return error_mark_node;
10409 case stv_firstarg:
10410 {
10411 bool maybe_const = true;
10412 tree sc;
10413 sc = c_fully_fold (op0, false, &maybe_const);
10414 sc = save_expr (sc);
10415 sc = convert (TREE_TYPE (type1), sc);
10416 op0 = build_vector_from_val (type1, sc);
10417 if (!maybe_const)
10418 op0 = c_wrap_maybe_const (op0, true);
10419 orig_type0 = type0 = TREE_TYPE (op0);
10420 code0 = TREE_CODE (type0);
10421 converted = 1;
10422 break;
10423 }
10424 case stv_secondarg:
10425 {
10426 bool maybe_const = true;
10427 tree sc;
10428 sc = c_fully_fold (op1, false, &maybe_const);
10429 sc = save_expr (sc);
10430 sc = convert (TREE_TYPE (type0), sc);
10431 op1 = build_vector_from_val (type0, sc);
10432 if (!maybe_const)
54b9f838 10433 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10434 orig_type1 = type1 = TREE_TYPE (op1);
10435 code1 = TREE_CODE (type1);
10436 converted = 1;
10437 break;
10438 }
10439 default:
10440 break;
10441 }
10442 }
10443
3e4093b6 10444 switch (code)
de520661 10445 {
3e4093b6
RS
10446 case PLUS_EXPR:
10447 /* Handle the pointer + int case. */
10448 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10449 {
db3927fb 10450 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10451 goto return_build_binary_op;
10452 }
3e4093b6 10453 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10454 {
db3927fb 10455 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10456 goto return_build_binary_op;
10457 }
fe67cf58 10458 else
3e4093b6
RS
10459 common = 1;
10460 break;
400fbf9f 10461
3e4093b6
RS
10462 case MINUS_EXPR:
10463 /* Subtraction of two similar pointers.
10464 We must subtract them as integers, then divide by object size. */
10465 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10466 && comp_target_types (location, type0, type1))
c9f9eb5d 10467 {
db3927fb 10468 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10469 goto return_build_binary_op;
10470 }
3e4093b6
RS
10471 /* Handle pointer minus int. Just like pointer plus int. */
10472 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10473 {
db3927fb 10474 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10475 goto return_build_binary_op;
10476 }
3e4093b6
RS
10477 else
10478 common = 1;
10479 break;
8b6a5902 10480
3e4093b6
RS
10481 case MULT_EXPR:
10482 common = 1;
10483 break;
10484
10485 case TRUNC_DIV_EXPR:
10486 case CEIL_DIV_EXPR:
10487 case FLOOR_DIV_EXPR:
10488 case ROUND_DIV_EXPR:
10489 case EXACT_DIV_EXPR:
de5a5fa1 10490 doing_div_or_mod = true;
c9f9eb5d 10491 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10492
10493 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10494 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10495 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10496 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10497 || code1 == FIXED_POINT_TYPE
3e4093b6 10498 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10499 {
5bed876a
AH
10500 enum tree_code tcode0 = code0, tcode1 = code1;
10501
3a021db2 10502 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10503 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10504 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10505 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10506
ab22c1fa
CF
10507 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10508 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10509 resultcode = RDIV_EXPR;
10510 else
10511 /* Although it would be tempting to shorten always here, that
10512 loses on some targets, since the modulo instruction is
10513 undefined if the quotient can't be represented in the
10514 computation mode. We shorten only if unsigned or if
10515 dividing by something we know != -1. */
8df83eae 10516 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10517 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10518 && !integer_all_onesp (op1)));
3e4093b6
RS
10519 common = 1;
10520 }
10521 break;
de520661 10522
3e4093b6 10523 case BIT_AND_EXPR:
3e4093b6
RS
10524 case BIT_IOR_EXPR:
10525 case BIT_XOR_EXPR:
10526 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10527 shorten = -1;
9ef0c8d9
AP
10528 /* Allow vector types which are not floating point types. */
10529 else if (code0 == VECTOR_TYPE
10530 && code1 == VECTOR_TYPE
10531 && !VECTOR_FLOAT_TYPE_P (type0)
10532 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10533 common = 1;
10534 break;
10535
10536 case TRUNC_MOD_EXPR:
10537 case FLOOR_MOD_EXPR:
de5a5fa1 10538 doing_div_or_mod = true;
c9f9eb5d 10539 warn_for_div_by_zero (location, op1);
de520661 10540
5cfd5d9b
AP
10541 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10542 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10543 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10544 common = 1;
10545 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10546 {
10547 /* Although it would be tempting to shorten always here, that loses
10548 on some targets, since the modulo instruction is undefined if the
10549 quotient can't be represented in the computation mode. We shorten
10550 only if unsigned or if dividing by something we know != -1. */
8df83eae 10551 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10552 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10553 && !integer_all_onesp (op1)));
3e4093b6
RS
10554 common = 1;
10555 }
10556 break;
de520661 10557
3e4093b6
RS
10558 case TRUTH_ANDIF_EXPR:
10559 case TRUTH_ORIF_EXPR:
10560 case TRUTH_AND_EXPR:
10561 case TRUTH_OR_EXPR:
10562 case TRUTH_XOR_EXPR:
10563 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10564 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10565 || code0 == FIXED_POINT_TYPE)
3e4093b6 10566 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10567 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10568 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10569 {
10570 /* Result of these operations is always an int,
10571 but that does not mean the operands should be
10572 converted to ints! */
10573 result_type = integer_type_node;
a27d595d
JM
10574 if (op0_int_operands)
10575 {
10576 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10577 op0 = remove_c_maybe_const_expr (op0);
10578 }
10579 else
10580 op0 = c_objc_common_truthvalue_conversion (location, op0);
10581 if (op1_int_operands)
10582 {
10583 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10584 op1 = remove_c_maybe_const_expr (op1);
10585 }
10586 else
10587 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10588 converted = 1;
180f8dbb 10589 boolean_op = true;
3e4093b6 10590 }
928c19bb
JM
10591 if (code == TRUTH_ANDIF_EXPR)
10592 {
10593 int_const_or_overflow = (int_operands
10594 && TREE_CODE (orig_op0) == INTEGER_CST
10595 && (op0 == truthvalue_false_node
10596 || TREE_CODE (orig_op1) == INTEGER_CST));
10597 int_const = (int_const_or_overflow
10598 && !TREE_OVERFLOW (orig_op0)
10599 && (op0 == truthvalue_false_node
10600 || !TREE_OVERFLOW (orig_op1)));
10601 }
10602 else if (code == TRUTH_ORIF_EXPR)
10603 {
10604 int_const_or_overflow = (int_operands
10605 && TREE_CODE (orig_op0) == INTEGER_CST
10606 && (op0 == truthvalue_true_node
10607 || TREE_CODE (orig_op1) == INTEGER_CST));
10608 int_const = (int_const_or_overflow
10609 && !TREE_OVERFLOW (orig_op0)
10610 && (op0 == truthvalue_true_node
10611 || !TREE_OVERFLOW (orig_op1)));
10612 }
3e4093b6 10613 break;
eba80994 10614
3e4093b6
RS
10615 /* Shift operations: result has same type as first operand;
10616 always convert second operand to int.
10617 Also set SHORT_SHIFT if shifting rightward. */
de520661 10618
3e4093b6 10619 case RSHIFT_EXPR:
f87bd04b
AS
10620 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10621 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10622 {
10623 result_type = type0;
10624 converted = 1;
10625 }
10626 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10627 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10628 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10629 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10630 {
10631 result_type = type0;
10632 converted = 1;
10633 }
10634 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10635 && code1 == INTEGER_TYPE)
3e4093b6 10636 {
de5a5fa1 10637 doing_shift = true;
928c19bb 10638 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10639 {
3e4093b6 10640 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10641 {
10642 int_const = false;
7d882b83 10643 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10644 warning_at (location, OPT_Wshift_count_negative,
10645 "right shift count is negative");
928c19bb 10646 }
3e4093b6 10647 else
bbb818c6 10648 {
3f75a254 10649 if (!integer_zerop (op1))
3e4093b6
RS
10650 short_shift = 1;
10651
10652 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10653 {
10654 int_const = false;
7d882b83 10655 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10656 warning_at (location, OPT_Wshift_count_overflow,
10657 "right shift count >= width of type");
928c19bb 10658 }
bbb818c6 10659 }
b62acd60 10660 }
de520661 10661
3e4093b6
RS
10662 /* Use the type of the value to be shifted. */
10663 result_type = type0;
3e4093b6
RS
10664 /* Avoid converting op1 to result_type later. */
10665 converted = 1;
400fbf9f 10666 }
3e4093b6 10667 break;
253b6b82 10668
3e4093b6 10669 case LSHIFT_EXPR:
f87bd04b
AS
10670 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10671 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10672 {
10673 result_type = type0;
10674 converted = 1;
10675 }
10676 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10677 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10678 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10679 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10680 {
10681 result_type = type0;
10682 converted = 1;
10683 }
10684 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10685 && code1 == INTEGER_TYPE)
3e4093b6 10686 {
de5a5fa1 10687 doing_shift = true;
0173bd2a
MP
10688 if (TREE_CODE (op0) == INTEGER_CST
10689 && tree_int_cst_sgn (op0) < 0)
10690 {
10691 /* Don't reject a left shift of a negative value in a context
10692 where a constant expression is needed in C90. */
10693 if (flag_isoc99)
10694 int_const = false;
10695 if (c_inhibit_evaluation_warnings == 0)
10696 warning_at (location, OPT_Wshift_negative_value,
10697 "left shift of negative value");
10698 }
928c19bb 10699 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10700 {
3e4093b6 10701 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10702 {
10703 int_const = false;
7d882b83 10704 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10705 warning_at (location, OPT_Wshift_count_negative,
10706 "left shift count is negative");
928c19bb 10707 }
de520661 10708
3e4093b6 10709 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10710 {
10711 int_const = false;
7d882b83 10712 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10713 warning_at (location, OPT_Wshift_count_overflow,
10714 "left shift count >= width of type");
928c19bb 10715 }
94ba5069 10716 }
de520661 10717
3e4093b6
RS
10718 /* Use the type of the value to be shifted. */
10719 result_type = type0;
3e4093b6
RS
10720 /* Avoid converting op1 to result_type later. */
10721 converted = 1;
400fbf9f 10722 }
3e4093b6 10723 break;
de520661 10724
3e4093b6
RS
10725 case EQ_EXPR:
10726 case NE_EXPR:
d246ab4f
AS
10727 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10728 {
10729 tree intt;
0af94e6f 10730 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10731 {
10732 error_at (location, "comparing vectors with different "
10733 "element types");
10734 return error_mark_node;
10735 }
10736
10737 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10738 {
10739 error_at (location, "comparing vectors with different "
10740 "number of elements");
10741 return error_mark_node;
10742 }
10743
10744 /* Always construct signed integer vector type. */
10745 intt = c_common_type_for_size (GET_MODE_BITSIZE
10746 (TYPE_MODE (TREE_TYPE (type0))), 0);
10747 result_type = build_opaque_vector_type (intt,
10748 TYPE_VECTOR_SUBPARTS (type0));
10749 converted = 1;
10750 break;
10751 }
ae311566 10752 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10753 warning_at (location,
10754 OPT_Wfloat_equal,
10755 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10756 /* Result of comparison is always int,
10757 but don't convert the args to int! */
10758 build_type = integer_type_node;
10759 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10760 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10761 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10762 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10763 short_compare = 1;
637f1455
SZ
10764 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10765 {
10766 if (TREE_CODE (op0) == ADDR_EXPR
10767 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10768 {
10769 if (code == EQ_EXPR)
10770 warning_at (location,
10771 OPT_Waddress,
10772 "the comparison will always evaluate as %<false%> "
10773 "for the address of %qD will never be NULL",
10774 TREE_OPERAND (op0, 0));
10775 else
10776 warning_at (location,
10777 OPT_Waddress,
10778 "the comparison will always evaluate as %<true%> "
10779 "for the address of %qD will never be NULL",
10780 TREE_OPERAND (op0, 0));
10781 }
10782 result_type = type0;
10783 }
10784 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10785 {
10786 if (TREE_CODE (op1) == ADDR_EXPR
10787 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10788 {
10789 if (code == EQ_EXPR)
10790 warning_at (location,
f90e8e2e 10791 OPT_Waddress,
637f1455
SZ
10792 "the comparison will always evaluate as %<false%> "
10793 "for the address of %qD will never be NULL",
10794 TREE_OPERAND (op1, 0));
10795 else
10796 warning_at (location,
10797 OPT_Waddress,
10798 "the comparison will always evaluate as %<true%> "
10799 "for the address of %qD will never be NULL",
10800 TREE_OPERAND (op1, 0));
10801 }
10802 result_type = type1;
10803 }
3e4093b6
RS
10804 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10805 {
10806 tree tt0 = TREE_TYPE (type0);
10807 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10808 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10809 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10810 addr_space_t as_common = ADDR_SPACE_GENERIC;
10811
3e4093b6
RS
10812 /* Anything compares with void *. void * compares with anything.
10813 Otherwise, the targets must be compatible
10814 and both must be object or both incomplete. */
744aa42f 10815 if (comp_target_types (location, type0, type1))
10bc1b1b 10816 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10817 else if (!addr_space_superset (as0, as1, &as_common))
10818 {
10819 error_at (location, "comparison of pointers to "
10820 "disjoint address spaces");
10821 return error_mark_node;
10822 }
267bac10 10823 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10824 {
36c5e70a 10825 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10826 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10827 "comparison of %<void *%> with function pointer");
ee2990e7 10828 }
267bac10 10829 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10830 {
36c5e70a 10831 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10832 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10833 "comparison of %<void *%> with function pointer");
e6834654 10834 }
3e4093b6 10835 else
58393038
ZL
10836 /* Avoid warning about the volatile ObjC EH puts on decls. */
10837 if (!objc_ok)
ba47d38d 10838 pedwarn (location, 0,
509c9d60 10839 "comparison of distinct pointer types lacks a cast");
e6834654 10840
3e4093b6 10841 if (result_type == NULL_TREE)
36c5e70a
BE
10842 {
10843 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10844 result_type = build_pointer_type
10845 (build_qualified_type (void_type_node, qual));
10846 }
e6834654 10847 }
3e4093b6 10848 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10849 {
3e4093b6 10850 result_type = type0;
ba47d38d 10851 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10852 }
3e4093b6 10853 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10854 {
3e4093b6 10855 result_type = type1;
ba47d38d 10856 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10857 }
04159acf
MP
10858 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10859 || truth_value_p (TREE_CODE (orig_op0)))
10860 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10861 || truth_value_p (TREE_CODE (orig_op1))))
10862 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10863 break;
8b6a5902 10864
3e4093b6
RS
10865 case LE_EXPR:
10866 case GE_EXPR:
10867 case LT_EXPR:
10868 case GT_EXPR:
d246ab4f
AS
10869 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10870 {
10871 tree intt;
0af94e6f 10872 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10873 {
10874 error_at (location, "comparing vectors with different "
10875 "element types");
10876 return error_mark_node;
10877 }
10878
10879 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10880 {
10881 error_at (location, "comparing vectors with different "
10882 "number of elements");
10883 return error_mark_node;
10884 }
10885
10886 /* Always construct signed integer vector type. */
10887 intt = c_common_type_for_size (GET_MODE_BITSIZE
10888 (TYPE_MODE (TREE_TYPE (type0))), 0);
10889 result_type = build_opaque_vector_type (intt,
10890 TYPE_VECTOR_SUBPARTS (type0));
10891 converted = 1;
10892 break;
10893 }
3e4093b6 10894 build_type = integer_type_node;
ab22c1fa
CF
10895 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10896 || code0 == FIXED_POINT_TYPE)
10897 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10898 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10899 short_compare = 1;
10900 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10901 {
36c5e70a
BE
10902 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10903 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10904 addr_space_t as_common;
10905
744aa42f 10906 if (comp_target_types (location, type0, type1))
3e4093b6 10907 {
10bc1b1b 10908 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10909 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10910 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10911 pedwarn (location, 0,
509c9d60 10912 "comparison of complete and incomplete pointers");
fcf73884 10913 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10914 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10915 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10916 else if (null_pointer_constant_p (orig_op0)
10917 || null_pointer_constant_p (orig_op1))
10918 warning_at (location, OPT_Wextra,
10919 "ordered comparison of pointer with null pointer");
10920
3e4093b6 10921 }
36c5e70a
BE
10922 else if (!addr_space_superset (as0, as1, &as_common))
10923 {
10924 error_at (location, "comparison of pointers to "
10925 "disjoint address spaces");
10926 return error_mark_node;
10927 }
3e4093b6
RS
10928 else
10929 {
36c5e70a
BE
10930 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10931 result_type = build_pointer_type
10932 (build_qualified_type (void_type_node, qual));
ba47d38d 10933 pedwarn (location, 0,
509c9d60 10934 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10935 }
10936 }
6aa3c60d 10937 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10938 {
10939 result_type = type0;
fcf73884 10940 if (pedantic)
c1771a20 10941 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10942 "ordered comparison of pointer with integer zero");
10943 else if (extra_warnings)
ba47d38d 10944 warning_at (location, OPT_Wextra,
d42ba3b8 10945 "ordered comparison of pointer with integer zero");
3e4093b6 10946 }
6aa3c60d 10947 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10948 {
10949 result_type = type1;
d42ba3b8 10950 if (pedantic)
c1771a20 10951 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10952 "ordered comparison of pointer with integer zero");
10953 else if (extra_warnings)
10954 warning_at (location, OPT_Wextra,
10955 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10956 }
10957 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10958 {
10959 result_type = type0;
ba47d38d 10960 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10961 }
10962 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10963 {
10964 result_type = type1;
ba47d38d 10965 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10966 }
04159acf
MP
10967 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10968 || truth_value_p (TREE_CODE (orig_op0)))
10969 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10970 || truth_value_p (TREE_CODE (orig_op1))))
10971 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10972 break;
64094f6a 10973
3e4093b6 10974 default:
37b2f290 10975 gcc_unreachable ();
c9fe6f9f 10976 }
8f17b5c5 10977
e57e265b
PB
10978 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10979 return error_mark_node;
10980
5bed876a
AH
10981 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10982 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10983 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10984 {
ba47d38d 10985 binary_op_error (location, code, type0, type1);
5bed876a
AH
10986 return error_mark_node;
10987 }
10988
3e4093b6 10989 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10990 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10991 &&
10992 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10993 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10994 {
2ca862e9
JM
10995 bool first_complex = (code0 == COMPLEX_TYPE);
10996 bool second_complex = (code1 == COMPLEX_TYPE);
10997 int none_complex = (!first_complex && !second_complex);
39b726dd 10998
3e4093b6 10999 if (shorten || common || short_compare)
3bf6bfcc
JJ
11000 {
11001 result_type = c_common_type (type0, type1);
0a0b3574
MM
11002 do_warn_double_promotion (result_type, type0, type1,
11003 "implicit conversion from %qT to %qT "
11004 "to match other operand of binary "
11005 "expression",
11006 location);
3bf6bfcc
JJ
11007 if (result_type == error_mark_node)
11008 return error_mark_node;
11009 }
400fbf9f 11010
2ca862e9
JM
11011 if (first_complex != second_complex
11012 && (code == PLUS_EXPR
11013 || code == MINUS_EXPR
11014 || code == MULT_EXPR
11015 || (code == TRUNC_DIV_EXPR && first_complex))
11016 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11017 && flag_signed_zeros)
11018 {
11019 /* An operation on mixed real/complex operands must be
11020 handled specially, but the language-independent code can
11021 more easily optimize the plain complex arithmetic if
11022 -fno-signed-zeros. */
11023 tree real_type = TREE_TYPE (result_type);
11024 tree real, imag;
11025 if (type0 != orig_type0 || type1 != orig_type1)
11026 {
11027 gcc_assert (may_need_excess_precision && common);
2d2e923f 11028 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11029 }
11030 if (first_complex)
11031 {
11032 if (TREE_TYPE (op0) != result_type)
68fca595 11033 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11034 if (TREE_TYPE (op1) != real_type)
68fca595 11035 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11036 }
11037 else
11038 {
11039 if (TREE_TYPE (op0) != real_type)
68fca595 11040 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11041 if (TREE_TYPE (op1) != result_type)
68fca595 11042 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11043 }
11044 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11045 return error_mark_node;
11046 if (first_complex)
11047 {
11048 op0 = c_save_expr (op0);
11049 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11050 op0, 1);
11051 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11052 op0, 1);
11053 switch (code)
11054 {
11055 case MULT_EXPR:
11056 case TRUNC_DIV_EXPR:
c4603e7c 11057 op1 = c_save_expr (op1);
2ca862e9
JM
11058 imag = build2 (resultcode, real_type, imag, op1);
11059 /* Fall through. */
11060 case PLUS_EXPR:
11061 case MINUS_EXPR:
11062 real = build2 (resultcode, real_type, real, op1);
11063 break;
11064 default:
11065 gcc_unreachable();
11066 }
11067 }
11068 else
11069 {
11070 op1 = c_save_expr (op1);
11071 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11072 op1, 1);
11073 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11074 op1, 1);
11075 switch (code)
11076 {
11077 case MULT_EXPR:
c4603e7c 11078 op0 = c_save_expr (op0);
2ca862e9
JM
11079 imag = build2 (resultcode, real_type, op0, imag);
11080 /* Fall through. */
11081 case PLUS_EXPR:
11082 real = build2 (resultcode, real_type, op0, real);
11083 break;
11084 case MINUS_EXPR:
11085 real = build2 (resultcode, real_type, op0, real);
11086 imag = build1 (NEGATE_EXPR, real_type, imag);
11087 break;
11088 default:
11089 gcc_unreachable();
11090 }
11091 }
11092 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11093 goto return_build_binary_op;
11094 }
11095
3e4093b6
RS
11096 /* For certain operations (which identify themselves by shorten != 0)
11097 if both args were extended from the same smaller type,
11098 do the arithmetic in that type and then extend.
400fbf9f 11099
3e4093b6
RS
11100 shorten !=0 and !=1 indicates a bitwise operation.
11101 For them, this optimization is safe only if
11102 both args are zero-extended or both are sign-extended.
11103 Otherwise, we might change the result.
11104 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11105 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11106
3e4093b6
RS
11107 if (shorten && none_complex)
11108 {
3e4093b6 11109 final_type = result_type;
b8698a0f 11110 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11111 shorten == -1);
3e4093b6 11112 }
88a3dbc1 11113
3e4093b6 11114 /* Shifts can be shortened if shifting right. */
2f6e4e97 11115
3e4093b6
RS
11116 if (short_shift)
11117 {
11118 int unsigned_arg;
11119 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11120
3e4093b6 11121 final_type = result_type;
abe80e6d 11122
3e4093b6 11123 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11124 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11125
3e4093b6 11126 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11127 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11128 /* We can shorten only if the shift count is less than the
11129 number of bits in the smaller type size. */
11130 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11131 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11132 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11133 {
11134 /* Do an unsigned shift if the operand was zero-extended. */
11135 result_type
11136 = c_common_signed_or_unsigned_type (unsigned_arg,
11137 TREE_TYPE (arg0));
11138 /* Convert value-to-be-shifted to that type. */
11139 if (TREE_TYPE (op0) != result_type)
11140 op0 = convert (result_type, op0);
11141 converted = 1;
abe80e6d 11142 }
88a3dbc1
RK
11143 }
11144
3e4093b6
RS
11145 /* Comparison operations are shortened too but differently.
11146 They identify themselves by setting short_compare = 1. */
56cb9733 11147
3e4093b6
RS
11148 if (short_compare)
11149 {
11150 /* Don't write &op0, etc., because that would prevent op0
11151 from being kept in a register.
11152 Instead, make copies of the our local variables and
11153 pass the copies by reference, then copy them back afterward. */
11154 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11155 enum tree_code xresultcode = resultcode;
11156 tree val
393e8e8b
MP
11157 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11158 &xresultcode);
8f17b5c5 11159
3e4093b6 11160 if (val != 0)
c9f9eb5d
AH
11161 {
11162 ret = val;
11163 goto return_build_binary_op;
11164 }
8f17b5c5 11165
3e4093b6
RS
11166 op0 = xop0, op1 = xop1;
11167 converted = 1;
11168 resultcode = xresultcode;
8f17b5c5 11169
7d882b83 11170 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11171 {
11172 bool op0_maybe_const = true;
11173 bool op1_maybe_const = true;
11174 tree orig_op0_folded, orig_op1_folded;
11175
11176 if (in_late_binary_op)
11177 {
11178 orig_op0_folded = orig_op0;
11179 orig_op1_folded = orig_op1;
11180 }
11181 else
11182 {
11183 /* Fold for the sake of possible warnings, as in
11184 build_conditional_expr. This requires the
11185 "original" values to be folded, not just op0 and
11186 op1. */
f5178456 11187 c_inhibit_evaluation_warnings++;
928c19bb
JM
11188 op0 = c_fully_fold (op0, require_constant_value,
11189 &op0_maybe_const);
11190 op1 = c_fully_fold (op1, require_constant_value,
11191 &op1_maybe_const);
f5178456 11192 c_inhibit_evaluation_warnings--;
928c19bb
JM
11193 orig_op0_folded = c_fully_fold (orig_op0,
11194 require_constant_value,
11195 NULL);
11196 orig_op1_folded = c_fully_fold (orig_op1,
11197 require_constant_value,
11198 NULL);
11199 }
11200
11201 if (warn_sign_compare)
11202 warn_for_sign_compare (location, orig_op0_folded,
11203 orig_op1_folded, op0, op1,
11204 result_type, resultcode);
5c2f94b4 11205 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11206 {
11207 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11208 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11209 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11210 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11211 }
3e4093b6 11212 }
2ad1815d 11213 }
64094f6a 11214 }
64094f6a 11215
3e4093b6
RS
11216 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11217 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11218 Then the expression will be built.
11219 It will be given type FINAL_TYPE if that is nonzero;
11220 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11221
3e4093b6
RS
11222 if (!result_type)
11223 {
ba47d38d 11224 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11225 return error_mark_node;
11226 }
400fbf9f 11227
3e4093b6 11228 if (build_type == NULL_TREE)
8ce94e44
JM
11229 {
11230 build_type = result_type;
180f8dbb
JM
11231 if ((type0 != orig_type0 || type1 != orig_type1)
11232 && !boolean_op)
8ce94e44
JM
11233 {
11234 gcc_assert (may_need_excess_precision && common);
2d2e923f 11235 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11236 }
11237 }
400fbf9f 11238
2d2e923f
MLI
11239 if (!converted)
11240 {
68fca595
MP
11241 op0 = ep_convert_and_check (location, result_type, op0,
11242 semantic_result_type);
11243 op1 = ep_convert_and_check (location, result_type, op1,
11244 semantic_result_type);
2d2e923f
MLI
11245
11246 /* This can happen if one operand has a vector type, and the other
11247 has a different type. */
11248 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11249 return error_mark_node;
11250 }
11251
f8ed5150
MP
11252 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11253 | SANITIZE_FLOAT_DIVIDE))
802ac282 11254 && do_ubsan_in_current_function ()
de5a5fa1
MP
11255 && (doing_div_or_mod || doing_shift))
11256 {
11257 /* OP0 and/or OP1 might have side-effects. */
11258 op0 = c_save_expr (op0);
11259 op1 = c_save_expr (op1);
11260 op0 = c_fully_fold (op0, false, NULL);
11261 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11262 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11263 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11264 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11265 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11266 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11267 }
11268
c9f9eb5d 11269 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11270 if (int_const_or_overflow)
11271 ret = (require_constant_value
db3927fb
AH
11272 ? fold_build2_initializer_loc (location, resultcode, build_type,
11273 op0, op1)
11274 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11275 else
11276 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11277 if (final_type != 0)
11278 ret = convert (final_type, ret);
11279
11280 return_build_binary_op:
11281 gcc_assert (ret != error_mark_node);
928c19bb
JM
11282 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11283 ret = (int_operands
11284 ? note_integer_operands (ret)
11285 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11286 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11287 && !in_late_binary_op)
11288 ret = note_integer_operands (ret);
2d2e923f
MLI
11289 if (semantic_result_type)
11290 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11291 protected_set_expr_location (ret, location);
de5a5fa1 11292
a24d975c 11293 if (instrument_expr != NULL)
de5a5fa1
MP
11294 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11295 instrument_expr, ret);
11296
c9f9eb5d 11297 return ret;
400fbf9f 11298}
85498824
JM
11299
11300
11301/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11302 purpose. LOCATION is the source location for the expression. */
85498824
JM
11303
11304tree
ba47d38d 11305c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11306{
928c19bb
JM
11307 bool int_const, int_operands;
11308
85498824
JM
11309 switch (TREE_CODE (TREE_TYPE (expr)))
11310 {
11311 case ARRAY_TYPE:
ba47d38d 11312 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11313 return error_mark_node;
11314
11315 case RECORD_TYPE:
ba47d38d 11316 error_at (location, "used struct type value where scalar is required");
85498824
JM
11317 return error_mark_node;
11318
11319 case UNION_TYPE:
ba47d38d 11320 error_at (location, "used union type value where scalar is required");
85498824
JM
11321 return error_mark_node;
11322
04af8788
NP
11323 case VOID_TYPE:
11324 error_at (location, "void value not ignored as it ought to be");
11325 return error_mark_node;
11326
46bdb9cf
JM
11327 case FUNCTION_TYPE:
11328 gcc_unreachable ();
11329
d246ab4f
AS
11330 case VECTOR_TYPE:
11331 error_at (location, "used vector type where scalar is required");
11332 return error_mark_node;
11333
85498824
JM
11334 default:
11335 break;
11336 }
11337
928c19bb
JM
11338 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11339 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11340 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11341 {
11342 expr = remove_c_maybe_const_expr (expr);
11343 expr = build2 (NE_EXPR, integer_type_node, expr,
11344 convert (TREE_TYPE (expr), integer_zero_node));
11345 expr = note_integer_operands (expr);
11346 }
11347 else
11348 /* ??? Should we also give an error for vectors rather than leaving
11349 those to give errors later? */
11350 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11351
11352 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11353 {
11354 if (TREE_OVERFLOW (expr))
11355 return expr;
11356 else
11357 return note_integer_operands (expr);
11358 }
11359 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11360 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11361 return expr;
85498824 11362}
73f397d4
JM
11363\f
11364
11365/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11366 required. */
11367
11368tree
51eed280 11369c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11370{
11371 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11372 {
11373 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11374 /* Executing a compound literal inside a function reinitializes
11375 it. */
11376 if (!TREE_STATIC (decl))
11377 *se = true;
11378 return decl;
11379 }
11380 else
11381 return expr;
11382}
953ff289 11383\f
41dbbb37
TS
11384/* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11385 statement. LOC is the location of the OACC_PARALLEL. */
11386
11387tree
11388c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11389{
11390 tree stmt;
11391
11392 block = c_end_compound_stmt (loc, block, true);
11393
11394 stmt = make_node (OACC_PARALLEL);
11395 TREE_TYPE (stmt) = void_type_node;
11396 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11397 OACC_PARALLEL_BODY (stmt) = block;
11398 SET_EXPR_LOCATION (stmt, loc);
11399
11400 return add_stmt (stmt);
11401}
11402
11403/* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11404 statement. LOC is the location of the OACC_KERNELS. */
11405
11406tree
11407c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11408{
11409 tree stmt;
11410
11411 block = c_end_compound_stmt (loc, block, true);
11412
11413 stmt = make_node (OACC_KERNELS);
11414 TREE_TYPE (stmt) = void_type_node;
11415 OACC_KERNELS_CLAUSES (stmt) = clauses;
11416 OACC_KERNELS_BODY (stmt) = block;
11417 SET_EXPR_LOCATION (stmt, loc);
11418
11419 return add_stmt (stmt);
11420}
11421
11422/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11423 statement. LOC is the location of the OACC_DATA. */
11424
11425tree
11426c_finish_oacc_data (location_t loc, tree clauses, tree block)
11427{
11428 tree stmt;
11429
11430 block = c_end_compound_stmt (loc, block, true);
11431
11432 stmt = make_node (OACC_DATA);
11433 TREE_TYPE (stmt) = void_type_node;
11434 OACC_DATA_CLAUSES (stmt) = clauses;
11435 OACC_DATA_BODY (stmt) = block;
11436 SET_EXPR_LOCATION (stmt, loc);
11437
11438 return add_stmt (stmt);
11439}
11440
c0220ea4 11441/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11442
11443tree
11444c_begin_omp_parallel (void)
11445{
11446 tree block;
11447
11448 keep_next_level ();
11449 block = c_begin_compound_stmt (true);
11450
11451 return block;
11452}
11453
c2255bc4
AH
11454/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11455 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11456
953ff289 11457tree
c2255bc4 11458c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11459{
11460 tree stmt;
11461
c2255bc4 11462 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11463
11464 stmt = make_node (OMP_PARALLEL);
11465 TREE_TYPE (stmt) = void_type_node;
11466 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11467 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11468 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11469
11470 return add_stmt (stmt);
11471}
11472
a68ab351
JJ
11473/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11474
11475tree
11476c_begin_omp_task (void)
11477{
11478 tree block;
11479
11480 keep_next_level ();
11481 block = c_begin_compound_stmt (true);
11482
11483 return block;
11484}
11485
c2255bc4
AH
11486/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11487 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11488
11489tree
c2255bc4 11490c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11491{
11492 tree stmt;
11493
c2255bc4 11494 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11495
11496 stmt = make_node (OMP_TASK);
11497 TREE_TYPE (stmt) = void_type_node;
11498 OMP_TASK_CLAUSES (stmt) = clauses;
11499 OMP_TASK_BODY (stmt) = block;
c2255bc4 11500 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11501
11502 return add_stmt (stmt);
11503}
11504
acf0174b
JJ
11505/* Generate GOMP_cancel call for #pragma omp cancel. */
11506
11507void
11508c_finish_omp_cancel (location_t loc, tree clauses)
11509{
11510 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11511 int mask = 0;
11512 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11513 mask = 1;
11514 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11515 mask = 2;
11516 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11517 mask = 4;
11518 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11519 mask = 8;
11520 else
11521 {
11522 error_at (loc, "%<#pragma omp cancel must specify one of "
11523 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11524 "clauses");
11525 return;
11526 }
11527 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11528 if (ifc != NULL_TREE)
11529 {
11530 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11531 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11532 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11533 build_zero_cst (type));
11534 }
11535 else
11536 ifc = boolean_true_node;
11537 tree stmt = build_call_expr_loc (loc, fn, 2,
11538 build_int_cst (integer_type_node, mask),
11539 ifc);
11540 add_stmt (stmt);
11541}
11542
11543/* Generate GOMP_cancellation_point call for
11544 #pragma omp cancellation point. */
11545
11546void
11547c_finish_omp_cancellation_point (location_t loc, tree clauses)
11548{
11549 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11550 int mask = 0;
11551 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11552 mask = 1;
11553 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11554 mask = 2;
11555 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11556 mask = 4;
11557 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11558 mask = 8;
11559 else
11560 {
11561 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11562 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11563 "clauses");
11564 return;
11565 }
11566 tree stmt = build_call_expr_loc (loc, fn, 1,
11567 build_int_cst (integer_type_node, mask));
11568 add_stmt (stmt);
11569}
11570
11571/* Helper function for handle_omp_array_sections. Called recursively
11572 to handle multiple array-section-subscripts. C is the clause,
11573 T current expression (initially OMP_CLAUSE_DECL), which is either
11574 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11575 expression if specified, TREE_VALUE length expression if specified,
11576 TREE_CHAIN is what it has been specified after, or some decl.
11577 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11578 set to true if any of the array-section-subscript could have length
11579 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11580 first array-section-subscript which is known not to have length
11581 of one. Given say:
11582 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11583 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11584 all are or may have length of 1, array-section-subscript [:2] is the
11585 first one knonwn not to have length 1. For array-section-subscript
11586 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11587 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11588 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11589 case though, as some lengths could be zero. */
11590
11591static tree
11592handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11593 bool &maybe_zero_len, unsigned int &first_non_one)
11594{
11595 tree ret, low_bound, length, type;
11596 if (TREE_CODE (t) != TREE_LIST)
11597 {
7a0ca710 11598 if (error_operand_p (t))
acf0174b 11599 return error_mark_node;
0ae9bd27 11600 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
11601 {
11602 if (DECL_P (t))
11603 error_at (OMP_CLAUSE_LOCATION (c),
11604 "%qD is not a variable in %qs clause", t,
11605 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11606 else
11607 error_at (OMP_CLAUSE_LOCATION (c),
11608 "%qE is not a variable in %qs clause", t,
11609 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11610 return error_mark_node;
11611 }
11612 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0ae9bd27 11613 && VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
11614 {
11615 error_at (OMP_CLAUSE_LOCATION (c),
11616 "%qD is threadprivate variable in %qs clause", t,
11617 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11618 return error_mark_node;
11619 }
11620 return t;
11621 }
11622
11623 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11624 maybe_zero_len, first_non_one);
11625 if (ret == error_mark_node || ret == NULL_TREE)
11626 return ret;
11627
11628 type = TREE_TYPE (ret);
11629 low_bound = TREE_PURPOSE (t);
11630 length = TREE_VALUE (t);
11631
11632 if (low_bound == error_mark_node || length == error_mark_node)
11633 return error_mark_node;
11634
11635 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11636 {
11637 error_at (OMP_CLAUSE_LOCATION (c),
11638 "low bound %qE of array section does not have integral type",
11639 low_bound);
11640 return error_mark_node;
11641 }
11642 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11643 {
11644 error_at (OMP_CLAUSE_LOCATION (c),
11645 "length %qE of array section does not have integral type",
11646 length);
11647 return error_mark_node;
11648 }
11649 if (low_bound
11650 && TREE_CODE (low_bound) == INTEGER_CST
11651 && TYPE_PRECISION (TREE_TYPE (low_bound))
11652 > TYPE_PRECISION (sizetype))
11653 low_bound = fold_convert (sizetype, low_bound);
11654 if (length
11655 && TREE_CODE (length) == INTEGER_CST
11656 && TYPE_PRECISION (TREE_TYPE (length))
11657 > TYPE_PRECISION (sizetype))
11658 length = fold_convert (sizetype, length);
11659 if (low_bound == NULL_TREE)
11660 low_bound = integer_zero_node;
11661
11662 if (length != NULL_TREE)
11663 {
11664 if (!integer_nonzerop (length))
11665 maybe_zero_len = true;
11666 if (first_non_one == types.length ()
11667 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11668 first_non_one++;
11669 }
11670 if (TREE_CODE (type) == ARRAY_TYPE)
11671 {
11672 if (length == NULL_TREE
11673 && (TYPE_DOMAIN (type) == NULL_TREE
11674 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11675 {
11676 error_at (OMP_CLAUSE_LOCATION (c),
11677 "for unknown bound array type length expression must "
11678 "be specified");
11679 return error_mark_node;
11680 }
11681 if (TREE_CODE (low_bound) == INTEGER_CST
11682 && tree_int_cst_sgn (low_bound) == -1)
11683 {
11684 error_at (OMP_CLAUSE_LOCATION (c),
11685 "negative low bound in array section in %qs clause",
11686 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11687 return error_mark_node;
11688 }
11689 if (length != NULL_TREE
11690 && TREE_CODE (length) == INTEGER_CST
11691 && tree_int_cst_sgn (length) == -1)
11692 {
11693 error_at (OMP_CLAUSE_LOCATION (c),
11694 "negative length in array section in %qs clause",
11695 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11696 return error_mark_node;
11697 }
11698 if (TYPE_DOMAIN (type)
11699 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11700 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11701 == INTEGER_CST)
11702 {
11703 tree size = size_binop (PLUS_EXPR,
11704 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11705 size_one_node);
11706 if (TREE_CODE (low_bound) == INTEGER_CST)
11707 {
11708 if (tree_int_cst_lt (size, low_bound))
11709 {
11710 error_at (OMP_CLAUSE_LOCATION (c),
11711 "low bound %qE above array section size "
11712 "in %qs clause", low_bound,
11713 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11714 return error_mark_node;
11715 }
11716 if (tree_int_cst_equal (size, low_bound))
11717 maybe_zero_len = true;
11718 else if (length == NULL_TREE
11719 && first_non_one == types.length ()
11720 && tree_int_cst_equal
11721 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11722 low_bound))
11723 first_non_one++;
11724 }
11725 else if (length == NULL_TREE)
11726 {
11727 maybe_zero_len = true;
11728 if (first_non_one == types.length ())
11729 first_non_one++;
11730 }
11731 if (length && TREE_CODE (length) == INTEGER_CST)
11732 {
11733 if (tree_int_cst_lt (size, length))
11734 {
11735 error_at (OMP_CLAUSE_LOCATION (c),
11736 "length %qE above array section size "
11737 "in %qs clause", length,
11738 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11739 return error_mark_node;
11740 }
11741 if (TREE_CODE (low_bound) == INTEGER_CST)
11742 {
11743 tree lbpluslen
11744 = size_binop (PLUS_EXPR,
11745 fold_convert (sizetype, low_bound),
11746 fold_convert (sizetype, length));
11747 if (TREE_CODE (lbpluslen) == INTEGER_CST
11748 && tree_int_cst_lt (size, lbpluslen))
11749 {
11750 error_at (OMP_CLAUSE_LOCATION (c),
11751 "high bound %qE above array section size "
11752 "in %qs clause", lbpluslen,
11753 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11754 return error_mark_node;
11755 }
11756 }
11757 }
11758 }
11759 else if (length == NULL_TREE)
11760 {
11761 maybe_zero_len = true;
11762 if (first_non_one == types.length ())
11763 first_non_one++;
11764 }
11765
11766 /* For [lb:] we will need to evaluate lb more than once. */
11767 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11768 {
11769 tree lb = c_save_expr (low_bound);
11770 if (lb != low_bound)
11771 {
11772 TREE_PURPOSE (t) = lb;
11773 low_bound = lb;
11774 }
11775 }
11776 }
11777 else if (TREE_CODE (type) == POINTER_TYPE)
11778 {
11779 if (length == NULL_TREE)
11780 {
11781 error_at (OMP_CLAUSE_LOCATION (c),
11782 "for pointer type length expression must be specified");
11783 return error_mark_node;
11784 }
11785 /* If there is a pointer type anywhere but in the very first
11786 array-section-subscript, the array section can't be contiguous. */
11787 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11788 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11789 {
11790 error_at (OMP_CLAUSE_LOCATION (c),
11791 "array section is not contiguous in %qs clause",
11792 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11793 return error_mark_node;
11794 }
11795 }
11796 else
11797 {
11798 error_at (OMP_CLAUSE_LOCATION (c),
11799 "%qE does not have pointer or array type", ret);
11800 return error_mark_node;
11801 }
11802 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11803 types.safe_push (TREE_TYPE (ret));
11804 /* We will need to evaluate lb more than once. */
11805 tree lb = c_save_expr (low_bound);
11806 if (lb != low_bound)
11807 {
11808 TREE_PURPOSE (t) = lb;
11809 low_bound = lb;
11810 }
11811 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11812 return ret;
11813}
11814
11815/* Handle array sections for clause C. */
11816
11817static bool
11818handle_omp_array_sections (tree c)
11819{
11820 bool maybe_zero_len = false;
11821 unsigned int first_non_one = 0;
11822 vec<tree> types = vNULL;
11823 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11824 maybe_zero_len, first_non_one);
11825 if (first == error_mark_node)
11826 {
11827 types.release ();
11828 return true;
11829 }
11830 if (first == NULL_TREE)
11831 {
11832 types.release ();
11833 return false;
11834 }
11835 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11836 {
11837 tree t = OMP_CLAUSE_DECL (c);
11838 tree tem = NULL_TREE;
11839 types.release ();
11840 /* Need to evaluate side effects in the length expressions
11841 if any. */
11842 while (TREE_CODE (t) == TREE_LIST)
11843 {
11844 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11845 {
11846 if (tem == NULL_TREE)
11847 tem = TREE_VALUE (t);
11848 else
11849 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11850 TREE_VALUE (t), tem);
11851 }
11852 t = TREE_CHAIN (t);
11853 }
11854 if (tem)
11855 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11856 first = c_fully_fold (first, false, NULL);
11857 OMP_CLAUSE_DECL (c) = first;
11858 }
11859 else
11860 {
11861 unsigned int num = types.length (), i;
11862 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11863 tree condition = NULL_TREE;
11864
11865 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11866 maybe_zero_len = true;
11867
11868 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11869 t = TREE_CHAIN (t))
11870 {
11871 tree low_bound = TREE_PURPOSE (t);
11872 tree length = TREE_VALUE (t);
11873
11874 i--;
11875 if (low_bound
11876 && TREE_CODE (low_bound) == INTEGER_CST
11877 && TYPE_PRECISION (TREE_TYPE (low_bound))
11878 > TYPE_PRECISION (sizetype))
11879 low_bound = fold_convert (sizetype, low_bound);
11880 if (length
11881 && TREE_CODE (length) == INTEGER_CST
11882 && TYPE_PRECISION (TREE_TYPE (length))
11883 > TYPE_PRECISION (sizetype))
11884 length = fold_convert (sizetype, length);
11885 if (low_bound == NULL_TREE)
11886 low_bound = integer_zero_node;
11887 if (!maybe_zero_len && i > first_non_one)
11888 {
11889 if (integer_nonzerop (low_bound))
11890 goto do_warn_noncontiguous;
11891 if (length != NULL_TREE
11892 && TREE_CODE (length) == INTEGER_CST
11893 && TYPE_DOMAIN (types[i])
11894 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11895 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11896 == INTEGER_CST)
11897 {
11898 tree size;
11899 size = size_binop (PLUS_EXPR,
11900 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11901 size_one_node);
11902 if (!tree_int_cst_equal (length, size))
11903 {
11904 do_warn_noncontiguous:
11905 error_at (OMP_CLAUSE_LOCATION (c),
11906 "array section is not contiguous in %qs "
11907 "clause",
11908 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11909 types.release ();
11910 return true;
11911 }
11912 }
11913 if (length != NULL_TREE
11914 && TREE_SIDE_EFFECTS (length))
11915 {
11916 if (side_effects == NULL_TREE)
11917 side_effects = length;
11918 else
11919 side_effects = build2 (COMPOUND_EXPR,
11920 TREE_TYPE (side_effects),
11921 length, side_effects);
11922 }
11923 }
11924 else
11925 {
11926 tree l;
11927
11928 if (i > first_non_one && length && integer_nonzerop (length))
11929 continue;
11930 if (length)
11931 l = fold_convert (sizetype, length);
11932 else
11933 {
11934 l = size_binop (PLUS_EXPR,
11935 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11936 size_one_node);
11937 l = size_binop (MINUS_EXPR, l,
11938 fold_convert (sizetype, low_bound));
11939 }
11940 if (i > first_non_one)
11941 {
11942 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11943 size_zero_node);
11944 if (condition == NULL_TREE)
11945 condition = l;
11946 else
11947 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11948 l, condition);
11949 }
11950 else if (size == NULL_TREE)
11951 {
11952 size = size_in_bytes (TREE_TYPE (types[i]));
11953 size = size_binop (MULT_EXPR, size, l);
11954 if (condition)
11955 size = fold_build3 (COND_EXPR, sizetype, condition,
11956 size, size_zero_node);
11957 }
11958 else
11959 size = size_binop (MULT_EXPR, size, l);
11960 }
11961 }
11962 types.release ();
11963 if (side_effects)
11964 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11965 first = c_fully_fold (first, false, NULL);
11966 OMP_CLAUSE_DECL (c) = first;
11967 if (size)
11968 size = c_fully_fold (size, false, NULL);
11969 OMP_CLAUSE_SIZE (c) = size;
11970 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11971 return false;
41dbbb37 11972 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
acf0174b 11973 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
41dbbb37 11974 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
acf0174b
JJ
11975 if (!c_mark_addressable (t))
11976 return false;
11977 OMP_CLAUSE_DECL (c2) = t;
11978 t = build_fold_addr_expr (first);
11979 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11980 tree ptr = OMP_CLAUSE_DECL (c2);
11981 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11982 ptr = build_fold_addr_expr (ptr);
11983 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11984 ptrdiff_type_node, t,
11985 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11986 ptrdiff_type_node, ptr));
11987 t = c_fully_fold (t, false, NULL);
11988 OMP_CLAUSE_SIZE (c2) = t;
11989 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11990 OMP_CLAUSE_CHAIN (c) = c2;
11991 }
11992 return false;
11993}
11994
11995/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11996 an inline call. But, remap
11997 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11998 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11999
12000static tree
12001c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12002 tree decl, tree placeholder)
12003{
12004 copy_body_data id;
b787e7a2 12005 hash_map<tree, tree> decl_map;
acf0174b 12006
b787e7a2
TS
12007 decl_map.put (omp_decl1, placeholder);
12008 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12009 memset (&id, 0, sizeof (id));
12010 id.src_fn = DECL_CONTEXT (omp_decl1);
12011 id.dst_fn = current_function_decl;
12012 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12013 id.decl_map = &decl_map;
acf0174b
JJ
12014
12015 id.copy_decl = copy_decl_no_change;
12016 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12017 id.transform_new_cfg = true;
12018 id.transform_return_to_modify = false;
12019 id.transform_lang_insert_block = NULL;
12020 id.eh_lp_nr = 0;
12021 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12022 return stmt;
12023}
12024
12025/* Helper function of c_finish_omp_clauses, called via walk_tree.
12026 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12027
12028static tree
12029c_find_omp_placeholder_r (tree *tp, int *, void *data)
12030{
12031 if (*tp == (tree) data)
12032 return *tp;
12033 return NULL_TREE;
12034}
12035
41dbbb37 12036/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12037 Remove any elements from the list that are invalid. */
12038
12039tree
12040c_finish_omp_clauses (tree clauses)
12041{
12042 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 12043 bitmap_head aligned_head;
f3316c6d 12044 tree c, t, *pc;
acf0174b
JJ
12045 bool branch_seen = false;
12046 bool copyprivate_seen = false;
12047 tree *nowait_clause = NULL;
953ff289
DN
12048
12049 bitmap_obstack_initialize (NULL);
12050 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12051 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12052 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12053 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
12054
12055 for (pc = &clauses, c = clauses; c ; c = *pc)
12056 {
12057 bool remove = false;
12058 bool need_complete = false;
12059 bool need_implicitly_determined = false;
12060
aaf46ef9 12061 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12062 {
12063 case OMP_CLAUSE_SHARED:
953ff289
DN
12064 need_implicitly_determined = true;
12065 goto check_dup_generic;
12066
12067 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12068 need_complete = true;
12069 need_implicitly_determined = true;
12070 goto check_dup_generic;
12071
12072 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12073 need_implicitly_determined = true;
12074 t = OMP_CLAUSE_DECL (c);
acf0174b 12075 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
12076 && (FLOAT_TYPE_P (TREE_TYPE (t))
12077 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
12078 {
12079 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12080 const char *r_name = NULL;
12081
12082 switch (r_code)
12083 {
12084 case PLUS_EXPR:
12085 case MULT_EXPR:
12086 case MINUS_EXPR:
652fea39 12087 break;
20906c66 12088 case MIN_EXPR:
652fea39
JJ
12089 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12090 r_name = "min";
12091 break;
20906c66 12092 case MAX_EXPR:
652fea39
JJ
12093 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12094 r_name = "max";
953ff289
DN
12095 break;
12096 case BIT_AND_EXPR:
12097 r_name = "&";
12098 break;
12099 case BIT_XOR_EXPR:
12100 r_name = "^";
12101 break;
12102 case BIT_IOR_EXPR:
12103 r_name = "|";
12104 break;
12105 case TRUTH_ANDIF_EXPR:
652fea39
JJ
12106 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12107 r_name = "&&";
953ff289
DN
12108 break;
12109 case TRUTH_ORIF_EXPR:
652fea39
JJ
12110 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12111 r_name = "||";
953ff289
DN
12112 break;
12113 default:
12114 gcc_unreachable ();
12115 }
12116 if (r_name)
12117 {
c2255bc4
AH
12118 error_at (OMP_CLAUSE_LOCATION (c),
12119 "%qE has invalid type for %<reduction(%s)%>",
12120 t, r_name);
953ff289 12121 remove = true;
ee1d5a02 12122 break;
953ff289
DN
12123 }
12124 }
acf0174b
JJ
12125 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12126 {
12127 error_at (OMP_CLAUSE_LOCATION (c),
12128 "user defined reduction not found for %qD", t);
12129 remove = true;
ee1d5a02 12130 break;
acf0174b
JJ
12131 }
12132 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12133 {
12134 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12135 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12136 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12137 VAR_DECL, NULL_TREE, type);
12138 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12139 DECL_ARTIFICIAL (placeholder) = 1;
12140 DECL_IGNORED_P (placeholder) = 1;
12141 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12142 c_mark_addressable (placeholder);
12143 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12144 c_mark_addressable (OMP_CLAUSE_DECL (c));
12145 OMP_CLAUSE_REDUCTION_MERGE (c)
12146 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12147 TREE_VEC_ELT (list, 0),
12148 TREE_VEC_ELT (list, 1),
12149 OMP_CLAUSE_DECL (c), placeholder);
12150 OMP_CLAUSE_REDUCTION_MERGE (c)
12151 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12152 void_type_node, NULL_TREE,
12153 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12154 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12155 if (TREE_VEC_LENGTH (list) == 6)
12156 {
12157 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12158 c_mark_addressable (OMP_CLAUSE_DECL (c));
12159 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12160 c_mark_addressable (placeholder);
12161 tree init = TREE_VEC_ELT (list, 5);
12162 if (init == error_mark_node)
12163 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12164 OMP_CLAUSE_REDUCTION_INIT (c)
12165 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12166 TREE_VEC_ELT (list, 3),
12167 OMP_CLAUSE_DECL (c), placeholder);
12168 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12169 OMP_CLAUSE_REDUCTION_INIT (c)
12170 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12171 OMP_CLAUSE_REDUCTION_INIT (c));
12172 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12173 c_find_omp_placeholder_r,
12174 placeholder, NULL))
12175 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12176 }
12177 else
12178 {
12179 tree init;
12180 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12181 init = build_constructor (TREE_TYPE (t), NULL);
12182 else
12183 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12184 OMP_CLAUSE_REDUCTION_INIT (c)
12185 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12186 }
12187 OMP_CLAUSE_REDUCTION_INIT (c)
12188 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12189 void_type_node, NULL_TREE,
12190 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12191 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12192 }
953ff289
DN
12193 goto check_dup_generic;
12194
12195 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12196 copyprivate_seen = true;
12197 if (nowait_clause)
12198 {
12199 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12200 "%<nowait%> clause must not be used together "
12201 "with %<copyprivate%>");
12202 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12203 nowait_clause = NULL;
12204 }
953ff289
DN
12205 goto check_dup_generic;
12206
12207 case OMP_CLAUSE_COPYIN:
953ff289 12208 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12209 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 12210 {
c2255bc4
AH
12211 error_at (OMP_CLAUSE_LOCATION (c),
12212 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12213 remove = true;
ee1d5a02 12214 break;
953ff289
DN
12215 }
12216 goto check_dup_generic;
12217
acf0174b
JJ
12218 case OMP_CLAUSE_LINEAR:
12219 t = OMP_CLAUSE_DECL (c);
12220 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12221 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12222 {
12223 error_at (OMP_CLAUSE_LOCATION (c),
12224 "linear clause applied to non-integral non-pointer "
12225 "variable with type %qT", TREE_TYPE (t));
12226 remove = true;
12227 break;
12228 }
12229 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12230 {
12231 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12232 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12233 OMP_CLAUSE_DECL (c), s);
12234 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12235 sizetype, s, OMP_CLAUSE_DECL (c));
12236 if (s == error_mark_node)
12237 s = size_one_node;
12238 OMP_CLAUSE_LINEAR_STEP (c) = s;
12239 }
da6f124d
JJ
12240 else
12241 OMP_CLAUSE_LINEAR_STEP (c)
12242 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12243 goto check_dup_generic;
12244
953ff289
DN
12245 check_dup_generic:
12246 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12247 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12248 {
c2255bc4 12249 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12250 "%qE is not a variable in clause %qs", t,
12251 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12252 remove = true;
12253 }
12254 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12255 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12256 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12257 {
c2255bc4
AH
12258 error_at (OMP_CLAUSE_LOCATION (c),
12259 "%qE appears more than once in data clauses", t);
953ff289
DN
12260 remove = true;
12261 }
12262 else
12263 bitmap_set_bit (&generic_head, DECL_UID (t));
12264 break;
12265
12266 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12267 t = OMP_CLAUSE_DECL (c);
12268 need_complete = true;
12269 need_implicitly_determined = true;
0ae9bd27 12270 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12271 {
c2255bc4
AH
12272 error_at (OMP_CLAUSE_LOCATION (c),
12273 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12274 remove = true;
12275 }
12276 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12277 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12278 {
c2255bc4
AH
12279 error_at (OMP_CLAUSE_LOCATION (c),
12280 "%qE appears more than once in data clauses", t);
953ff289
DN
12281 remove = true;
12282 }
12283 else
12284 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12285 break;
12286
12287 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12288 t = OMP_CLAUSE_DECL (c);
12289 need_complete = true;
12290 need_implicitly_determined = true;
0ae9bd27 12291 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 12292 {
c2255bc4
AH
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12295 remove = true;
12296 }
12297 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12298 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12299 {
c2255bc4
AH
12300 error_at (OMP_CLAUSE_LOCATION (c),
12301 "%qE appears more than once in data clauses", t);
953ff289
DN
12302 remove = true;
12303 }
12304 else
12305 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12306 break;
12307
acf0174b
JJ
12308 case OMP_CLAUSE_ALIGNED:
12309 t = OMP_CLAUSE_DECL (c);
0ae9bd27 12310 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12311 {
12312 error_at (OMP_CLAUSE_LOCATION (c),
12313 "%qE is not a variable in %<aligned%> clause", t);
12314 remove = true;
12315 }
5a9785fb
JJ
12316 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12317 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12318 {
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "%qE in %<aligned%> clause is neither a pointer nor "
12321 "an array", t);
12322 remove = true;
12323 }
acf0174b
JJ
12324 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12325 {
12326 error_at (OMP_CLAUSE_LOCATION (c),
12327 "%qE appears more than once in %<aligned%> clauses",
12328 t);
12329 remove = true;
12330 }
12331 else
12332 bitmap_set_bit (&aligned_head, DECL_UID (t));
12333 break;
12334
12335 case OMP_CLAUSE_DEPEND:
12336 t = OMP_CLAUSE_DECL (c);
12337 if (TREE_CODE (t) == TREE_LIST)
12338 {
12339 if (handle_omp_array_sections (c))
12340 remove = true;
12341 break;
12342 }
12343 if (t == error_mark_node)
12344 remove = true;
0ae9bd27 12345 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12346 {
12347 error_at (OMP_CLAUSE_LOCATION (c),
12348 "%qE is not a variable in %<depend%> clause", t);
12349 remove = true;
12350 }
12351 else if (!c_mark_addressable (t))
12352 remove = true;
12353 break;
12354
12355 case OMP_CLAUSE_MAP:
12356 case OMP_CLAUSE_TO:
12357 case OMP_CLAUSE_FROM:
41dbbb37 12358 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12359 t = OMP_CLAUSE_DECL (c);
12360 if (TREE_CODE (t) == TREE_LIST)
12361 {
12362 if (handle_omp_array_sections (c))
12363 remove = true;
12364 else
12365 {
12366 t = OMP_CLAUSE_DECL (c);
b17a8b07 12367 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12368 {
12369 error_at (OMP_CLAUSE_LOCATION (c),
12370 "array section does not have mappable type "
12371 "in %qs clause",
12372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12373 remove = true;
12374 }
12375 }
12376 break;
12377 }
12378 if (t == error_mark_node)
12379 remove = true;
0ae9bd27 12380 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12381 {
12382 error_at (OMP_CLAUSE_LOCATION (c),
12383 "%qE is not a variable in %qs clause", t,
12384 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12385 remove = true;
12386 }
0ae9bd27 12387 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12388 {
12389 error_at (OMP_CLAUSE_LOCATION (c),
12390 "%qD is threadprivate variable in %qs clause", t,
12391 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12392 remove = true;
12393 }
12394 else if (!c_mark_addressable (t))
12395 remove = true;
b17a8b07 12396 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37
TS
12397 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12398 || (OMP_CLAUSE_MAP_KIND (c)
12399 == GOMP_MAP_FORCE_DEVICEPTR)))
b17a8b07 12400 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12401 {
12402 error_at (OMP_CLAUSE_LOCATION (c),
12403 "%qD does not have a mappable type in %qs clause", t,
12404 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12405 remove = true;
12406 }
12407 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12408 {
12409 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12410 error ("%qD appears more than once in motion clauses", t);
12411 else
12412 error ("%qD appears more than once in map clauses", t);
12413 remove = true;
12414 }
12415 else
12416 bitmap_set_bit (&generic_head, DECL_UID (t));
12417 break;
12418
12419 case OMP_CLAUSE_UNIFORM:
12420 t = OMP_CLAUSE_DECL (c);
12421 if (TREE_CODE (t) != PARM_DECL)
12422 {
12423 if (DECL_P (t))
12424 error_at (OMP_CLAUSE_LOCATION (c),
12425 "%qD is not an argument in %<uniform%> clause", t);
12426 else
12427 error_at (OMP_CLAUSE_LOCATION (c),
12428 "%qE is not an argument in %<uniform%> clause", t);
12429 remove = true;
ee1d5a02 12430 break;
acf0174b 12431 }
ee1d5a02 12432 goto check_dup_generic;
acf0174b
JJ
12433
12434 case OMP_CLAUSE_NOWAIT:
12435 if (copyprivate_seen)
12436 {
12437 error_at (OMP_CLAUSE_LOCATION (c),
12438 "%<nowait%> clause must not be used together "
12439 "with %<copyprivate%>");
12440 remove = true;
12441 break;
12442 }
12443 nowait_clause = pc;
12444 pc = &OMP_CLAUSE_CHAIN (c);
12445 continue;
12446
953ff289
DN
12447 case OMP_CLAUSE_IF:
12448 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12449 case OMP_CLAUSE_NUM_TEAMS:
12450 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12451 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12452 case OMP_CLAUSE_ORDERED:
12453 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12454 case OMP_CLAUSE_UNTIED:
12455 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12456 case OMP_CLAUSE_FINAL:
12457 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12458 case OMP_CLAUSE_SAFELEN:
12459 case OMP_CLAUSE_SIMDLEN:
12460 case OMP_CLAUSE_DEVICE:
12461 case OMP_CLAUSE_DIST_SCHEDULE:
12462 case OMP_CLAUSE_PARALLEL:
12463 case OMP_CLAUSE_FOR:
12464 case OMP_CLAUSE_SECTIONS:
12465 case OMP_CLAUSE_TASKGROUP:
12466 case OMP_CLAUSE_PROC_BIND:
9a771876 12467 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
12468 case OMP_CLAUSE_NUM_GANGS:
12469 case OMP_CLAUSE_NUM_WORKERS:
12470 case OMP_CLAUSE_VECTOR_LENGTH:
12471 case OMP_CLAUSE_ASYNC:
12472 case OMP_CLAUSE_WAIT:
12473 case OMP_CLAUSE_AUTO:
12474 case OMP_CLAUSE_SEQ:
12475 case OMP_CLAUSE_GANG:
12476 case OMP_CLAUSE_WORKER:
12477 case OMP_CLAUSE_VECTOR:
acf0174b
JJ
12478 pc = &OMP_CLAUSE_CHAIN (c);
12479 continue;
12480
12481 case OMP_CLAUSE_INBRANCH:
12482 case OMP_CLAUSE_NOTINBRANCH:
12483 if (branch_seen)
12484 {
12485 error_at (OMP_CLAUSE_LOCATION (c),
12486 "%<inbranch%> clause is incompatible with "
12487 "%<notinbranch%>");
12488 remove = true;
12489 break;
12490 }
12491 branch_seen = true;
953ff289
DN
12492 pc = &OMP_CLAUSE_CHAIN (c);
12493 continue;
12494
12495 default:
12496 gcc_unreachable ();
12497 }
12498
12499 if (!remove)
12500 {
12501 t = OMP_CLAUSE_DECL (c);
12502
12503 if (need_complete)
12504 {
12505 t = require_complete_type (t);
12506 if (t == error_mark_node)
12507 remove = true;
12508 }
12509
12510 if (need_implicitly_determined)
12511 {
12512 const char *share_name = NULL;
12513
0ae9bd27 12514 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
12515 share_name = "threadprivate";
12516 else switch (c_omp_predetermined_sharing (t))
12517 {
12518 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12519 break;
12520 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12521 /* const vars may be specified in firstprivate clause. */
12522 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12523 && TREE_READONLY (t))
12524 break;
953ff289
DN
12525 share_name = "shared";
12526 break;
12527 case OMP_CLAUSE_DEFAULT_PRIVATE:
12528 share_name = "private";
12529 break;
12530 default:
12531 gcc_unreachable ();
12532 }
12533 if (share_name)
12534 {
c2255bc4
AH
12535 error_at (OMP_CLAUSE_LOCATION (c),
12536 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12537 t, share_name,
12538 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12539 remove = true;
12540 }
12541 }
12542 }
12543
12544 if (remove)
12545 *pc = OMP_CLAUSE_CHAIN (c);
12546 else
12547 pc = &OMP_CLAUSE_CHAIN (c);
12548 }
12549
12550 bitmap_obstack_release (NULL);
12551 return clauses;
12552}
9ae165a0 12553
0a35513e
AH
12554/* Create a transaction node. */
12555
12556tree
12557c_finish_transaction (location_t loc, tree block, int flags)
12558{
12559 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12560 if (flags & TM_STMT_ATTR_OUTER)
12561 TRANSACTION_EXPR_OUTER (stmt) = 1;
12562 if (flags & TM_STMT_ATTR_RELAXED)
12563 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12564 return add_stmt (stmt);
12565}
12566
9ae165a0
DG
12567/* Make a variant type in the proper way for C/C++, propagating qualifiers
12568 down to the element type of an array. */
12569
12570tree
12571c_build_qualified_type (tree type, int type_quals)
12572{
12573 if (type == error_mark_node)
12574 return type;
12575
12576 if (TREE_CODE (type) == ARRAY_TYPE)
12577 {
12578 tree t;
12579 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12580 type_quals);
12581
12582 /* See if we already have an identically qualified type. */
12583 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12584 {
12585 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12586 && TYPE_NAME (t) == TYPE_NAME (type)
12587 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12588 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12589 TYPE_ATTRIBUTES (type)))
12590 break;
12591 }
12592 if (!t)
12593 {
12594 tree domain = TYPE_DOMAIN (type);
12595
12596 t = build_variant_type_copy (type);
12597 TREE_TYPE (t) = element_type;
12598
12599 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12600 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12601 SET_TYPE_STRUCTURAL_EQUALITY (t);
12602 else if (TYPE_CANONICAL (element_type) != element_type
12603 || (domain && TYPE_CANONICAL (domain) != domain))
12604 {
b8698a0f 12605 tree unqualified_canon
9ae165a0 12606 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12607 domain? TYPE_CANONICAL (domain)
9ae165a0 12608 : NULL_TREE);
b8698a0f 12609 TYPE_CANONICAL (t)
9ae165a0
DG
12610 = c_build_qualified_type (unqualified_canon, type_quals);
12611 }
12612 else
12613 TYPE_CANONICAL (t) = t;
12614 }
12615 return t;
12616 }
12617
12618 /* A restrict-qualified pointer type must be a pointer to object or
12619 incomplete type. Note that the use of POINTER_TYPE_P also allows
12620 REFERENCE_TYPEs, which is appropriate for C++. */
12621 if ((type_quals & TYPE_QUAL_RESTRICT)
12622 && (!POINTER_TYPE_P (type)
12623 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12624 {
12625 error ("invalid use of %<restrict%>");
12626 type_quals &= ~TYPE_QUAL_RESTRICT;
12627 }
12628
12629 return build_qualified_type (type, type_quals);
12630}
72b5577d
ILT
12631
12632/* Build a VA_ARG_EXPR for the C parser. */
12633
12634tree
c2255bc4 12635c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d 12636{
ec3fba51
MP
12637 if (error_operand_p (type))
12638 return error_mark_node;
12639 else if (!COMPLETE_TYPE_P (type))
4e81b788 12640 {
ec3fba51
MP
12641 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12642 "type %qT", type);
4e81b788
MP
12643 return error_mark_node;
12644 }
ec3fba51
MP
12645 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12646 warning_at (loc, OPT_Wc___compat,
12647 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12648 return build_va_arg (loc, expr, type);
72b5577d 12649}
acf0174b
JJ
12650
12651/* Return truthvalue of whether T1 is the same tree structure as T2.
12652 Return 1 if they are the same. Return 0 if they are different. */
12653
12654bool
12655c_tree_equal (tree t1, tree t2)
12656{
12657 enum tree_code code1, code2;
12658
12659 if (t1 == t2)
12660 return true;
12661 if (!t1 || !t2)
12662 return false;
12663
12664 for (code1 = TREE_CODE (t1);
12665 CONVERT_EXPR_CODE_P (code1)
12666 || code1 == NON_LVALUE_EXPR;
12667 code1 = TREE_CODE (t1))
12668 t1 = TREE_OPERAND (t1, 0);
12669 for (code2 = TREE_CODE (t2);
12670 CONVERT_EXPR_CODE_P (code2)
12671 || code2 == NON_LVALUE_EXPR;
12672 code2 = TREE_CODE (t2))
12673 t2 = TREE_OPERAND (t2, 0);
12674
12675 /* They might have become equal now. */
12676 if (t1 == t2)
12677 return true;
12678
12679 if (code1 != code2)
12680 return false;
12681
12682 switch (code1)
12683 {
12684 case INTEGER_CST:
807e902e 12685 return wi::eq_p (t1, t2);
acf0174b
JJ
12686
12687 case REAL_CST:
12688 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12689
12690 case STRING_CST:
12691 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12692 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12693 TREE_STRING_LENGTH (t1));
12694
12695 case FIXED_CST:
12696 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12697 TREE_FIXED_CST (t2));
12698
12699 case COMPLEX_CST:
12700 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12701 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12702
12703 case VECTOR_CST:
12704 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12705
12706 case CONSTRUCTOR:
12707 /* We need to do this when determining whether or not two
12708 non-type pointer to member function template arguments
12709 are the same. */
12710 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12711 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12712 return false;
12713 {
12714 tree field, value;
12715 unsigned int i;
12716 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12717 {
12718 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12719 if (!c_tree_equal (field, elt2->index)
12720 || !c_tree_equal (value, elt2->value))
12721 return false;
12722 }
12723 }
12724 return true;
12725
12726 case TREE_LIST:
12727 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12728 return false;
12729 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12730 return false;
12731 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12732
12733 case SAVE_EXPR:
12734 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12735
12736 case CALL_EXPR:
12737 {
12738 tree arg1, arg2;
12739 call_expr_arg_iterator iter1, iter2;
12740 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12741 return false;
12742 for (arg1 = first_call_expr_arg (t1, &iter1),
12743 arg2 = first_call_expr_arg (t2, &iter2);
12744 arg1 && arg2;
12745 arg1 = next_call_expr_arg (&iter1),
12746 arg2 = next_call_expr_arg (&iter2))
12747 if (!c_tree_equal (arg1, arg2))
12748 return false;
12749 if (arg1 || arg2)
12750 return false;
12751 return true;
12752 }
12753
12754 case TARGET_EXPR:
12755 {
12756 tree o1 = TREE_OPERAND (t1, 0);
12757 tree o2 = TREE_OPERAND (t2, 0);
12758
12759 /* Special case: if either target is an unallocated VAR_DECL,
12760 it means that it's going to be unified with whatever the
12761 TARGET_EXPR is really supposed to initialize, so treat it
12762 as being equivalent to anything. */
0ae9bd27 12763 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
12764 && !DECL_RTL_SET_P (o1))
12765 /*Nop*/;
0ae9bd27 12766 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
12767 && !DECL_RTL_SET_P (o2))
12768 /*Nop*/;
12769 else if (!c_tree_equal (o1, o2))
12770 return false;
12771
12772 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12773 }
12774
12775 case COMPONENT_REF:
12776 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12777 return false;
12778 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12779
12780 case PARM_DECL:
12781 case VAR_DECL:
12782 case CONST_DECL:
12783 case FIELD_DECL:
12784 case FUNCTION_DECL:
12785 case IDENTIFIER_NODE:
12786 case SSA_NAME:
12787 return false;
12788
12789 case TREE_VEC:
12790 {
12791 unsigned ix;
12792 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12793 return false;
12794 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12795 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12796 TREE_VEC_ELT (t2, ix)))
12797 return false;
12798 return true;
12799 }
12800
12801 default:
12802 break;
12803 }
12804
12805 switch (TREE_CODE_CLASS (code1))
12806 {
12807 case tcc_unary:
12808 case tcc_binary:
12809 case tcc_comparison:
12810 case tcc_expression:
12811 case tcc_vl_exp:
12812 case tcc_reference:
12813 case tcc_statement:
12814 {
12815 int i, n = TREE_OPERAND_LENGTH (t1);
12816
12817 switch (code1)
12818 {
12819 case PREINCREMENT_EXPR:
12820 case PREDECREMENT_EXPR:
12821 case POSTINCREMENT_EXPR:
12822 case POSTDECREMENT_EXPR:
12823 n = 1;
12824 break;
12825 case ARRAY_REF:
12826 n = 2;
12827 break;
12828 default:
12829 break;
12830 }
12831
12832 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12833 && n != TREE_OPERAND_LENGTH (t2))
12834 return false;
12835
12836 for (i = 0; i < n; ++i)
12837 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12838 return false;
12839
12840 return true;
12841 }
12842
12843 case tcc_type:
12844 return comptypes (t1, t2);
12845 default:
12846 gcc_unreachable ();
12847 }
12848 /* We can get here with --disable-checking. */
12849 return false;
12850}
12893402
BI
12851
12852/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12853 spawn-helper and BODY is the newly created body for FNDECL. */
12854
12855void
12856cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12857{
12858 tree list = alloc_stmt_list ();
12859 tree frame = make_cilk_frame (fndecl);
12860 tree dtor = create_cilk_function_exit (frame, false, true);
12861 add_local_decl (cfun, frame);
12862
12863 DECL_SAVED_TREE (fndecl) = list;
12864 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12865 frame);
12866 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12867 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12868
12869 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12870 append_to_statement_list (detach_expr, &body_list);
12871
12872 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12873 body = fold_build_cleanup_point_expr (void_type_node, body);
12874
12875 append_to_statement_list (body, &body_list);
12876 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12877 body_list, dtor), &list);
12878}