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