]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
cgraph.h (cgraph_rtl_info): Move to rtl.h
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
5624e564 2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
40e23961 30#include "symtab.h"
40e23961 31#include "alias.h"
400fbf9f 32#include "tree.h"
40e23961 33#include "fold-const.h"
d8a2d370
DN
34#include "stor-layout.h"
35#include "trans-mem.h"
36#include "varasm.h"
37#include "stmt.h"
e57e265b 38#include "langhooks.h"
400fbf9f 39#include "c-tree.h"
29cc57cf 40#include "c-lang.h"
400fbf9f 41#include "flags.h"
ab87f8c8 42#include "intl.h"
672a6f42 43#include "target.h"
325c3691 44#include "tree-iterator.h"
6662d794 45#include "bitmap.h"
60393bbc 46#include "predict.h"
60393bbc 47#include "hard-reg-set.h"
60393bbc 48#include "function.h"
2fb9a547 49#include "gimple-expr.h"
45b0be94 50#include "gimplify.h"
acf0174b 51#include "tree-inline.h"
0645c1a2 52#include "omp-low.h"
61d3ce20 53#include "c-family/c-objc.h"
a212e43f 54#include "c-family/c-common.h"
de5a5fa1 55#include "c-family/c-ubsan.h"
12893402 56#include "cilk.h"
41dbbb37 57#include "gomp-constants.h"
325c3691 58
2ac2f164
JM
59/* Possible cases of implicit bad conversions. Used to select
60 diagnostic messages in convert_for_assignment. */
61enum impl_conv {
62 ic_argpass,
63 ic_assign,
64 ic_init,
65 ic_return
66};
67
bc4b653b
JM
68/* The level of nesting inside "__alignof__". */
69int in_alignof;
70
71/* The level of nesting inside "sizeof". */
72int in_sizeof;
73
74/* The level of nesting inside "typeof". */
75int in_typeof;
400fbf9f 76
1a4049e7
JJ
77/* The argument of last parsed sizeof expression, only to be tested
78 if expr.original_code == SIZEOF_EXPR. */
79tree c_last_sizeof_arg;
80
9bac5cbb
G
81/* Nonzero if we might need to print a "missing braces around
82 initializer" message within this initializer. */
83static int found_missing_braces;
103b7b17 84
bf730f15
RS
85static int require_constant_value;
86static int require_constant_elements;
87
58f9752a 88static bool null_pointer_constant_p (const_tree);
f55ade6e 89static tree qualify_type (tree, tree);
dc5027f4
JM
90static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
91 bool *);
744aa42f 92static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
93static int function_types_compatible_p (const_tree, const_tree, bool *,
94 bool *);
95static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 96static tree lookup_field (tree, tree);
81e5eca8
MP
97static int convert_arguments (location_t, vec<location_t>, tree,
98 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
99 tree);
db3927fb 100static tree pointer_diff (location_t, tree, tree);
68fca595 101static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 102 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
103static tree valid_compound_expr_initializer (tree, tree);
104static void push_string (const char *);
105static void push_member_name (tree);
f55ade6e
AJ
106static int spelling_length (void);
107static char *print_spelling (char *);
96b40f8d 108static void warning_init (location_t, int, const char *);
c2255bc4 109static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
110static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
111 bool, struct obstack *);
a1e3b3d9 112static void output_pending_init_elements (int, struct obstack *);
ea58ef42 113static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 114static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
115static void add_pending_init (location_t, tree, tree, tree, bool,
116 struct obstack *);
a1e3b3d9
LB
117static void set_nonincremental_init (struct obstack *);
118static void set_nonincremental_init_from_string (tree, struct obstack *);
119static tree find_init_member (tree, struct obstack *);
f37acdf9 120static void readonly_warning (tree, enum lvalue_use);
7bd11157 121static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 122static void record_maybe_used_decl (tree);
dc5027f4 123static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
124\f
125/* Return true if EXP is a null pointer constant, false otherwise. */
126
127static bool
58f9752a 128null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
129{
130 /* This should really operate on c_expr structures, but they aren't
131 yet available everywhere required. */
132 tree type = TREE_TYPE (expr);
133 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 134 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
135 && integer_zerop (expr)
136 && (INTEGRAL_TYPE_P (type)
137 || (TREE_CODE (type) == POINTER_TYPE
138 && VOID_TYPE_P (TREE_TYPE (type))
139 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
140}
928c19bb
JM
141
142/* EXPR may appear in an unevaluated part of an integer constant
143 expression, but not in an evaluated part. Wrap it in a
144 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
145 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
146
147static tree
148note_integer_operands (tree expr)
149{
150 tree ret;
151 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
152 {
153 ret = copy_node (expr);
154 TREE_OVERFLOW (ret) = 1;
155 }
156 else
157 {
158 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
159 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
160 }
161 return ret;
162}
163
4d84fe7c
JM
164/* Having checked whether EXPR may appear in an unevaluated part of an
165 integer constant expression and found that it may, remove any
166 C_MAYBE_CONST_EXPR noting this fact and return the resulting
167 expression. */
168
169static inline tree
170remove_c_maybe_const_expr (tree expr)
171{
172 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
173 return C_MAYBE_CONST_EXPR_EXPR (expr);
174 else
175 return expr;
176}
177
f13c9b2c
AP
178\f/* This is a cache to hold if two types are compatible or not. */
179
180struct tagged_tu_seen_cache {
181 const struct tagged_tu_seen_cache * next;
58f9752a
KG
182 const_tree t1;
183 const_tree t2;
f13c9b2c
AP
184 /* The return value of tagged_types_tu_compatible_p if we had seen
185 these two types already. */
186 int val;
187};
188
189static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
190static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
191
400fbf9f
JW
192/* Do `exp = require_complete_type (exp);' to make sure exp
193 does not have an incomplete type. (That includes void types.) */
194
195tree
2f6e4e97 196require_complete_type (tree value)
400fbf9f
JW
197{
198 tree type = TREE_TYPE (value);
199
7a0ca710 200 if (error_operand_p (value))
ea0f786b
CB
201 return error_mark_node;
202
400fbf9f 203 /* First, detect a valid value with a complete type. */
d0f062fb 204 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
205 return value;
206
7a228918 207 c_incomplete_type_error (value, type);
400fbf9f
JW
208 return error_mark_node;
209}
210
211/* Print an error message for invalid use of an incomplete type.
212 VALUE is the expression that was used (or 0 if that isn't known)
213 and TYPE is the type that was invalid. */
214
215void
ac7d7749 216c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 217{
400fbf9f
JW
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
221
222 if (value != 0 && (TREE_CODE (value) == VAR_DECL
223 || TREE_CODE (value) == PARM_DECL))
ac9f18db 224 error ("%qD has an incomplete type %qT", value, type);
400fbf9f
JW
225 else
226 {
227 retry:
228 /* We must print an error message. Be clever about what it says. */
229
230 switch (TREE_CODE (type))
231 {
232 case RECORD_TYPE:
400fbf9f 233 case UNION_TYPE:
400fbf9f 234 case ENUMERAL_TYPE:
400fbf9f
JW
235 break;
236
237 case VOID_TYPE:
238 error ("invalid use of void expression");
239 return;
240
241 case ARRAY_TYPE:
242 if (TYPE_DOMAIN (type))
243 {
fba78abb
RH
244 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
245 {
246 error ("invalid use of flexible array member");
247 return;
248 }
400fbf9f
JW
249 type = TREE_TYPE (type);
250 goto retry;
251 }
252 error ("invalid use of array with unspecified bounds");
253 return;
254
255 default:
366de0ce 256 gcc_unreachable ();
400fbf9f
JW
257 }
258
259 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
ac9f18db 260 error ("invalid use of undefined type %qT", type);
400fbf9f
JW
261 else
262 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
ac9f18db 263 error ("invalid use of incomplete typedef %qT", type);
400fbf9f
JW
264 }
265}
266
ab393bf1
NB
267/* Given a type, apply default promotions wrt unnamed function
268 arguments and return the new type. */
269
270tree
2f6e4e97 271c_type_promotes_to (tree type)
ab393bf1 272{
267bac10 273 tree ret = NULL_TREE;
ab393bf1 274
267bac10
JM
275 if (TYPE_MAIN_VARIANT (type) == float_type_node)
276 ret = double_type_node;
277 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
278 {
279 /* Preserve unsignedness if not really getting any wider. */
8df83eae 280 if (TYPE_UNSIGNED (type)
c22cacf3 281 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
282 ret = unsigned_type_node;
283 else
284 ret = integer_type_node;
ab393bf1
NB
285 }
286
267bac10
JM
287 if (ret != NULL_TREE)
288 return (TYPE_ATOMIC (type)
289 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
290 : ret);
291
ab393bf1
NB
292 return type;
293}
294
36c5e70a
BE
295/* Return true if between two named address spaces, whether there is a superset
296 named address space that encompasses both address spaces. If there is a
297 superset, return which address space is the superset. */
298
299static bool
300addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
301{
302 if (as1 == as2)
303 {
304 *common = as1;
305 return true;
306 }
307 else if (targetm.addr_space.subset_p (as1, as2))
308 {
309 *common = as2;
310 return true;
311 }
312 else if (targetm.addr_space.subset_p (as2, as1))
313 {
314 *common = as1;
315 return true;
316 }
317 else
318 return false;
319}
320
400fbf9f
JW
321/* Return a variant of TYPE which has all the type qualifiers of LIKE
322 as well as those of TYPE. */
323
324static tree
2f6e4e97 325qualify_type (tree type, tree like)
400fbf9f 326{
36c5e70a
BE
327 addr_space_t as_type = TYPE_ADDR_SPACE (type);
328 addr_space_t as_like = TYPE_ADDR_SPACE (like);
329 addr_space_t as_common;
330
331 /* If the two named address spaces are different, determine the common
332 superset address space. If there isn't one, raise an error. */
333 if (!addr_space_superset (as_type, as_like, &as_common))
334 {
335 as_common = as_type;
336 error ("%qT and %qT are in disjoint named address spaces",
337 type, like);
338 }
339
2f6e4e97 340 return c_build_qualified_type (type,
36c5e70a 341 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 342 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 343 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 344}
52ffd86e
MS
345
346/* Return true iff the given tree T is a variable length array. */
347
348bool
ac7d7749 349c_vla_type_p (const_tree t)
52ffd86e
MS
350{
351 if (TREE_CODE (t) == ARRAY_TYPE
352 && C_TYPE_VARIABLE_SIZE (t))
353 return true;
354 return false;
355}
400fbf9f 356\f
10bc1b1b 357/* Return the composite type of two compatible types.
5305f6d7 358
10bc1b1b
JM
359 We assume that comptypes has already been done and returned
360 nonzero; if that isn't so, this may crash. In particular, we
361 assume that qualifiers match. */
400fbf9f
JW
362
363tree
10bc1b1b 364composite_type (tree t1, tree t2)
400fbf9f 365{
b3694847
SS
366 enum tree_code code1;
367 enum tree_code code2;
4b027d16 368 tree attributes;
400fbf9f
JW
369
370 /* Save time if the two types are the same. */
371
372 if (t1 == t2) return t1;
373
374 /* If one type is nonsense, use the other. */
375 if (t1 == error_mark_node)
376 return t2;
377 if (t2 == error_mark_node)
378 return t1;
379
10bc1b1b
JM
380 code1 = TREE_CODE (t1);
381 code2 = TREE_CODE (t2);
382
d9525bec 383 /* Merge the attributes. */
5fd9b178 384 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 385
10bc1b1b
JM
386 /* If one is an enumerated type and the other is the compatible
387 integer type, the composite type might be either of the two
388 (DR#013 question 3). For consistency, use the enumerated type as
389 the composite type. */
400fbf9f 390
10bc1b1b
JM
391 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
392 return t1;
393 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
394 return t2;
75326e8c 395
366de0ce 396 gcc_assert (code1 == code2);
b6a10c9f 397
400fbf9f
JW
398 switch (code1)
399 {
400fbf9f 400 case POINTER_TYPE:
10bc1b1b 401 /* For two pointers, do this recursively on the target type. */
400fbf9f 402 {
3932261a
MM
403 tree pointed_to_1 = TREE_TYPE (t1);
404 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 405 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 406 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
407 t1 = build_type_attribute_variant (t1, attributes);
408 return qualify_type (t1, t2);
400fbf9f 409 }
400fbf9f
JW
410
411 case ARRAY_TYPE:
412 {
10bc1b1b 413 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
414 int quals;
415 tree unqual_elt;
ca8bdb78
JM
416 tree d1 = TYPE_DOMAIN (t1);
417 tree d2 = TYPE_DOMAIN (t2);
418 bool d1_variable, d2_variable;
419 bool d1_zero, d2_zero;
f6294de7 420 bool t1_complete, t2_complete;
46df2823 421
de46b2fe 422 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
423 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
424 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 425
f6294de7
JM
426 t1_complete = COMPLETE_TYPE_P (t1);
427 t2_complete = COMPLETE_TYPE_P (t2);
428
ca8bdb78
JM
429 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
430 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
431
432 d1_variable = (!d1_zero
433 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
434 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
435 d2_variable = (!d2_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
438 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
439 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 440
400fbf9f 441 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
442 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
443 && (d2_variable || d2_zero || !d1_variable))
4b027d16 444 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
445 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
446 && (d1_variable || d1_zero || !d2_variable))
4b027d16 447 return build_type_attribute_variant (t2, attributes);
c22cacf3 448
de46b2fe
AP
449 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
450 return build_type_attribute_variant (t1, attributes);
451 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
452 return build_type_attribute_variant (t2, attributes);
c22cacf3 453
46df2823
JM
454 /* Merge the element types, and have a size if either arg has
455 one. We may have qualifiers on the element types. To set
456 up TYPE_MAIN_VARIANT correctly, we need to form the
457 composite of the unqualified types and add the qualifiers
458 back at the end. */
459 quals = TYPE_QUALS (strip_array_types (elt));
460 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
461 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
462 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
463 && (d2_variable
464 || d2_zero
465 || !d1_variable))
466 ? t1
467 : t2));
f6294de7
JM
468 /* Ensure a composite type involving a zero-length array type
469 is a zero-length type not an incomplete type. */
470 if (d1_zero && d2_zero
471 && (t1_complete || t2_complete)
472 && !COMPLETE_TYPE_P (t1))
473 {
474 TYPE_SIZE (t1) = bitsize_zero_node;
475 TYPE_SIZE_UNIT (t1) = size_zero_node;
476 }
46df2823 477 t1 = c_build_qualified_type (t1, quals);
de46b2fe 478 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
479 }
480
fcb99e7b
JJ
481 case ENUMERAL_TYPE:
482 case RECORD_TYPE:
483 case UNION_TYPE:
484 if (attributes != NULL)
485 {
486 /* Try harder not to create a new aggregate type. */
487 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
488 return t1;
489 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
490 return t2;
491 }
492 return build_type_attribute_variant (t1, attributes);
493
400fbf9f
JW
494 case FUNCTION_TYPE:
495 /* Function types: prefer the one that specified arg types.
496 If both do, merge the arg types. Also merge the return types. */
497 {
10bc1b1b 498 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
499 tree p1 = TYPE_ARG_TYPES (t1);
500 tree p2 = TYPE_ARG_TYPES (t2);
501 int len;
502 tree newargs, n;
503 int i;
504
505 /* Save space: see if the result is identical to one of the args. */
3f75a254 506 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 507 return build_type_attribute_variant (t1, attributes);
3f75a254 508 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 509 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
510
511 /* Simple way if one arg fails to specify argument types. */
512 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 513 {
fe7080d2
AP
514 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
515 t1 = build_type_attribute_variant (t1, attributes);
516 return qualify_type (t1, t2);
4b027d16 517 }
400fbf9f 518 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
519 {
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
4b027d16 523 }
400fbf9f
JW
524
525 /* If both args specify argument types, we must merge the two
526 lists, argument by argument. */
2f4e8f2b 527
400fbf9f
JW
528 len = list_length (p1);
529 newargs = 0;
530
531 for (i = 0; i < len; i++)
8d9bfdc5 532 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
533
534 n = newargs;
535
536 for (; p1;
537 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
538 {
539 /* A null type means arg type is not specified.
540 Take whatever the other function type has. */
541 if (TREE_VALUE (p1) == 0)
542 {
543 TREE_VALUE (n) = TREE_VALUE (p2);
544 goto parm_done;
545 }
546 if (TREE_VALUE (p2) == 0)
547 {
548 TREE_VALUE (n) = TREE_VALUE (p1);
549 goto parm_done;
550 }
2f6e4e97 551
400fbf9f
JW
552 /* Given wait (union {union wait *u; int *i} *)
553 and wait (union wait *),
554 prefer union wait * as type of parm. */
555 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
556 && TREE_VALUE (p1) != TREE_VALUE (p2))
557 {
558 tree memb;
58cb41e6
JJ
559 tree mv2 = TREE_VALUE (p2);
560 if (mv2 && mv2 != error_mark_node
561 && TREE_CODE (mv2) != ARRAY_TYPE)
562 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 563 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 564 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
565 {
566 tree mv3 = TREE_TYPE (memb);
567 if (mv3 && mv3 != error_mark_node
568 && TREE_CODE (mv3) != ARRAY_TYPE)
569 mv3 = TYPE_MAIN_VARIANT (mv3);
570 if (comptypes (mv3, mv2))
571 {
572 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
573 TREE_VALUE (p2));
c1771a20 574 pedwarn (input_location, OPT_Wpedantic,
fcf73884 575 "function types not truly compatible in ISO C");
58cb41e6
JJ
576 goto parm_done;
577 }
578 }
400fbf9f
JW
579 }
580 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
581 && TREE_VALUE (p2) != TREE_VALUE (p1))
582 {
583 tree memb;
58cb41e6
JJ
584 tree mv1 = TREE_VALUE (p1);
585 if (mv1 && mv1 != error_mark_node
586 && TREE_CODE (mv1) != ARRAY_TYPE)
587 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 588 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 589 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
590 {
591 tree mv3 = TREE_TYPE (memb);
592 if (mv3 && mv3 != error_mark_node
593 && TREE_CODE (mv3) != ARRAY_TYPE)
594 mv3 = TYPE_MAIN_VARIANT (mv3);
595 if (comptypes (mv3, mv1))
596 {
597 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
598 TREE_VALUE (p1));
c1771a20 599 pedwarn (input_location, OPT_Wpedantic,
fcf73884 600 "function types not truly compatible in ISO C");
58cb41e6
JJ
601 goto parm_done;
602 }
603 }
400fbf9f 604 }
10bc1b1b 605 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
606 parm_done: ;
607 }
608
4b027d16 609 t1 = build_function_type (valtype, newargs);
fe7080d2 610 t1 = qualify_type (t1, t2);
0f41302f 611 /* ... falls through ... */
400fbf9f
JW
612 }
613
614 default:
4b027d16 615 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
616 }
617
618}
10bc1b1b
JM
619
620/* Return the type of a conditional expression between pointers to
621 possibly differently qualified versions of compatible types.
622
623 We assume that comp_target_types has already been done and returned
624 nonzero; if that isn't so, this may crash. */
625
626static tree
627common_pointer_type (tree t1, tree t2)
628{
629 tree attributes;
46df2823
JM
630 tree pointed_to_1, mv1;
631 tree pointed_to_2, mv2;
10bc1b1b 632 tree target;
eb1387a0 633 unsigned target_quals;
36c5e70a
BE
634 addr_space_t as1, as2, as_common;
635 int quals1, quals2;
10bc1b1b
JM
636
637 /* Save time if the two types are the same. */
638
639 if (t1 == t2) return t1;
640
641 /* If one type is nonsense, use the other. */
642 if (t1 == error_mark_node)
643 return t2;
644 if (t2 == error_mark_node)
645 return t1;
646
366de0ce 647 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 648 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
649
650 /* Merge the attributes. */
651 attributes = targetm.merge_type_attributes (t1, t2);
652
653 /* Find the composite type of the target types, and combine the
46df2823
JM
654 qualifiers of the two types' targets. Do not lose qualifiers on
655 array element types by taking the TYPE_MAIN_VARIANT. */
656 mv1 = pointed_to_1 = TREE_TYPE (t1);
657 mv2 = pointed_to_2 = TREE_TYPE (t2);
658 if (TREE_CODE (mv1) != ARRAY_TYPE)
659 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
660 if (TREE_CODE (mv2) != ARRAY_TYPE)
661 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
662 target = composite_type (mv1, mv2);
eb1387a0 663
768952be
MU
664 /* Strip array types to get correct qualifier for pointers to arrays */
665 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
666 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
667
eb1387a0
RG
668 /* For function types do not merge const qualifiers, but drop them
669 if used inconsistently. The middle-end uses these to mark const
670 and noreturn functions. */
671 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 672 target_quals = (quals1 & quals2);
eb1387a0 673 else
36c5e70a
BE
674 target_quals = (quals1 | quals2);
675
676 /* If the two named address spaces are different, determine the common
677 superset address space. This is guaranteed to exist due to the
678 assumption that comp_target_type returned non-zero. */
679 as1 = TYPE_ADDR_SPACE (pointed_to_1);
680 as2 = TYPE_ADDR_SPACE (pointed_to_2);
681 if (!addr_space_superset (as1, as2, &as_common))
682 gcc_unreachable ();
683
684 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
685
eb1387a0 686 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
687 return build_type_attribute_variant (t1, attributes);
688}
689
690/* Return the common type for two arithmetic types under the usual
691 arithmetic conversions. The default conversions have already been
692 applied, and enumerated types converted to their compatible integer
693 types. The resulting type is unqualified and has no attributes.
694
695 This is the type for the result of most arithmetic operations
696 if the operands have the given two types. */
697
ccf7f880
JJ
698static tree
699c_common_type (tree t1, tree t2)
10bc1b1b
JM
700{
701 enum tree_code code1;
702 enum tree_code code2;
703
704 /* If one type is nonsense, use the other. */
705 if (t1 == error_mark_node)
706 return t2;
707 if (t2 == error_mark_node)
708 return t1;
709
710 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
711 t1 = TYPE_MAIN_VARIANT (t1);
712
713 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
714 t2 = TYPE_MAIN_VARIANT (t2);
715
716 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
717 t1 = build_type_attribute_variant (t1, NULL_TREE);
718
719 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
720 t2 = build_type_attribute_variant (t2, NULL_TREE);
721
722 /* Save time if the two types are the same. */
723
724 if (t1 == t2) return t1;
725
726 code1 = TREE_CODE (t1);
727 code2 = TREE_CODE (t2);
728
366de0ce 729 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
730 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
731 || code1 == INTEGER_TYPE);
366de0ce 732 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
733 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
734 || code2 == INTEGER_TYPE);
10bc1b1b 735
5fc89bfd
JJ
736 /* When one operand is a decimal float type, the other operand cannot be
737 a generic float type or a complex type. We also disallow vector types
738 here. */
739 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
740 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
741 {
742 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
743 {
744 error ("can%'t mix operands of decimal float and vector types");
745 return error_mark_node;
746 }
747 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
748 {
749 error ("can%'t mix operands of decimal float and complex types");
750 return error_mark_node;
751 }
752 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
753 {
754 error ("can%'t mix operands of decimal float and other float types");
755 return error_mark_node;
756 }
757 }
758
10bc1b1b
JM
759 /* If one type is a vector type, return that type. (How the usual
760 arithmetic conversions apply to the vector types extension is not
761 precisely specified.) */
762 if (code1 == VECTOR_TYPE)
763 return t1;
764
765 if (code2 == VECTOR_TYPE)
766 return t2;
767
768 /* If one type is complex, form the common type of the non-complex
769 components, then make that complex. Use T1 or T2 if it is the
770 required type. */
771 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
772 {
773 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
774 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 775 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
776
777 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
778 return t1;
779 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
780 return t2;
781 else
782 return build_complex_type (subtype);
783 }
784
785 /* If only one is real, use it as the result. */
786
787 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
788 return t1;
789
790 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
791 return t2;
792
9a8ce21f
JG
793 /* If both are real and either are decimal floating point types, use
794 the decimal floating point type with the greater precision. */
795
796 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
797 {
798 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
799 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
800 return dfloat128_type_node;
801 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
802 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
803 return dfloat64_type_node;
804 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
805 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
806 return dfloat32_type_node;
807 }
808
ab22c1fa
CF
809 /* Deal with fixed-point types. */
810 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
811 {
812 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 813 machine_mode m1, m2;
ab22c1fa
CF
814 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
815
816 m1 = TYPE_MODE (t1);
817 m2 = TYPE_MODE (t2);
818
819 /* If one input type is saturating, the result type is saturating. */
820 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
821 satp = 1;
822
823 /* If both fixed-point types are unsigned, the result type is unsigned.
824 When mixing fixed-point and integer types, follow the sign of the
825 fixed-point type.
826 Otherwise, the result type is signed. */
827 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
828 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
829 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t1))
831 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t2)))
833 unsignedp = 1;
834
835 /* The result type is signed. */
836 if (unsignedp == 0)
837 {
838 /* If the input type is unsigned, we need to convert to the
839 signed type. */
840 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
841 {
d75d71e0 842 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
843 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
844 mclass = MODE_FRACT;
845 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
846 mclass = MODE_ACCUM;
847 else
848 gcc_unreachable ();
849 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
850 }
851 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
852 {
d75d71e0 853 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
854 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
855 mclass = MODE_FRACT;
856 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
857 mclass = MODE_ACCUM;
858 else
859 gcc_unreachable ();
860 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
861 }
862 }
863
864 if (code1 == FIXED_POINT_TYPE)
865 {
866 fbit1 = GET_MODE_FBIT (m1);
867 ibit1 = GET_MODE_IBIT (m1);
868 }
869 else
870 {
871 fbit1 = 0;
872 /* Signed integers need to subtract one sign bit. */
873 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
874 }
875
876 if (code2 == FIXED_POINT_TYPE)
877 {
878 fbit2 = GET_MODE_FBIT (m2);
879 ibit2 = GET_MODE_IBIT (m2);
880 }
881 else
882 {
883 fbit2 = 0;
884 /* Signed integers need to subtract one sign bit. */
885 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
886 }
887
888 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
889 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
890 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
891 satp);
892 }
893
10bc1b1b
JM
894 /* Both real or both integers; use the one with greater precision. */
895
896 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
897 return t1;
898 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
899 return t2;
900
901 /* Same precision. Prefer long longs to longs to ints when the
902 same precision, following the C99 rules on integer type rank
903 (which are equivalent to the C90 rules for C90 types). */
904
905 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
906 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
907 return long_long_unsigned_type_node;
908
909 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
911 {
912 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
913 return long_long_unsigned_type_node;
914 else
c22cacf3 915 return long_long_integer_type_node;
10bc1b1b
JM
916 }
917
918 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
919 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
920 return long_unsigned_type_node;
921
922 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
924 {
925 /* But preserve unsignedness from the other type,
926 since long cannot hold all the values of an unsigned int. */
927 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
928 return long_unsigned_type_node;
929 else
930 return long_integer_type_node;
931 }
932
933 /* Likewise, prefer long double to double even if same size. */
934 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
935 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
936 return long_double_type_node;
937
2531a1d9
JR
938 /* Likewise, prefer double to float even if same size.
939 We got a couple of embedded targets with 32 bit doubles, and the
940 pdp11 might have 64 bit floats. */
941 if (TYPE_MAIN_VARIANT (t1) == double_type_node
942 || TYPE_MAIN_VARIANT (t2) == double_type_node)
943 return double_type_node;
944
10bc1b1b
JM
945 /* Otherwise prefer the unsigned one. */
946
947 if (TYPE_UNSIGNED (t1))
948 return t1;
949 else
950 return t2;
951}
400fbf9f 952\f
5922c215
JM
953/* Wrapper around c_common_type that is used by c-common.c and other
954 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
955 are allowed here and are converted to their compatible integer types.
956 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
957 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
958tree
959common_type (tree t1, tree t2)
960{
961 if (TREE_CODE (t1) == ENUMERAL_TYPE)
962 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
963 if (TREE_CODE (t2) == ENUMERAL_TYPE)
964 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
965
966 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
967 if (TREE_CODE (t1) == BOOLEAN_TYPE
968 && TREE_CODE (t2) == BOOLEAN_TYPE)
969 return boolean_type_node;
970
971 /* If either type is BOOLEAN_TYPE, then return the other. */
972 if (TREE_CODE (t1) == BOOLEAN_TYPE)
973 return t2;
974 if (TREE_CODE (t2) == BOOLEAN_TYPE)
975 return t1;
976
ccf7f880
JJ
977 return c_common_type (t1, t2);
978}
f13c9b2c 979
400fbf9f
JW
980/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
981 or various other operations. Return 2 if they are compatible
982 but a warning may be needed if you use them together. */
983
984int
132da1a5 985comptypes (tree type1, tree type2)
f13c9b2c
AP
986{
987 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
988 int val;
989
dc5027f4 990 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
991 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
992
993 return val;
994}
995
996/* Like comptypes, but if it returns non-zero because enum and int are
997 compatible, it sets *ENUM_AND_INT_P to true. */
998
999static int
1000comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1001{
1002 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1003 int val;
1004
dc5027f4
JM
1005 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1006 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1007
1008 return val;
1009}
1010
1011/* Like comptypes, but if it returns nonzero for different types, it
1012 sets *DIFFERENT_TYPES_P to true. */
1013
1014int
1015comptypes_check_different_types (tree type1, tree type2,
1016 bool *different_types_p)
1017{
1018 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1019 int val;
1020
1021 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1022 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1023
f13c9b2c 1024 return val;
c22cacf3
MS
1025}
1026\f
f13c9b2c
AP
1027/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1028 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1029 but a warning may be needed if you use them together. If
1030 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1031 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1032 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1033 NULL, and the types are compatible but different enough not to be
48b0b196 1034 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1035 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1036 false, but may or may not be set if the types are incompatible.
1037 This differs from comptypes, in that we don't free the seen
1038 types. */
f13c9b2c
AP
1039
1040static int
dc5027f4
JM
1041comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1042 bool *different_types_p)
400fbf9f 1043{
58f9752a
KG
1044 const_tree t1 = type1;
1045 const_tree t2 = type2;
4b027d16 1046 int attrval, val;
400fbf9f
JW
1047
1048 /* Suppress errors caused by previously reported errors. */
1049
8d47dfc5
RH
1050 if (t1 == t2 || !t1 || !t2
1051 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1052 return 1;
1053
bca63328
JM
1054 /* Enumerated types are compatible with integer types, but this is
1055 not transitive: two enumerated types in the same translation unit
1056 are compatible with each other only if they are the same type. */
400fbf9f 1057
bca63328 1058 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1059 {
1060 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1061 if (TREE_CODE (t2) != VOID_TYPE)
1062 {
1063 if (enum_and_int_p != NULL)
1064 *enum_and_int_p = true;
1065 if (different_types_p != NULL)
1066 *different_types_p = true;
1067 }
744aa42f 1068 }
bca63328 1069 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1070 {
1071 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1072 if (TREE_CODE (t1) != VOID_TYPE)
1073 {
1074 if (enum_and_int_p != NULL)
1075 *enum_and_int_p = true;
1076 if (different_types_p != NULL)
1077 *different_types_p = true;
1078 }
744aa42f 1079 }
400fbf9f
JW
1080
1081 if (t1 == t2)
1082 return 1;
1083
1084 /* Different classes of types can't be compatible. */
1085
3aeb3655
EC
1086 if (TREE_CODE (t1) != TREE_CODE (t2))
1087 return 0;
400fbf9f 1088
118a3a8b 1089 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1090
3932261a 1091 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1092 return 0;
1093
08632da2
RS
1094 /* Allow for two different type nodes which have essentially the same
1095 definition. Note that we already checked for equality of the type
38e01259 1096 qualifiers (just above). */
400fbf9f 1097
46df2823
JM
1098 if (TREE_CODE (t1) != ARRAY_TYPE
1099 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1100 return 1;
1101
4b027d16 1102 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1103 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1104 return 0;
1105
1106 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1107 val = 0;
1108
400fbf9f
JW
1109 switch (TREE_CODE (t1))
1110 {
1111 case POINTER_TYPE:
106f5de5
UW
1112 /* Do not remove mode or aliasing information. */
1113 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1114 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1115 break;
4b027d16 1116 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1117 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1118 enum_and_int_p, different_types_p));
4b027d16 1119 break;
400fbf9f
JW
1120
1121 case FUNCTION_TYPE:
dc5027f4
JM
1122 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1123 different_types_p);
4b027d16 1124 break;
400fbf9f
JW
1125
1126 case ARRAY_TYPE:
1127 {
400fbf9f
JW
1128 tree d1 = TYPE_DOMAIN (t1);
1129 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1130 bool d1_variable, d2_variable;
1131 bool d1_zero, d2_zero;
4b027d16 1132 val = 1;
400fbf9f
JW
1133
1134 /* Target types must match incl. qualifiers. */
1135 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1136 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1137 enum_and_int_p,
1138 different_types_p)))
400fbf9f
JW
1139 return 0;
1140
dc5027f4
JM
1141 if (different_types_p != NULL
1142 && (d1 == 0) != (d2 == 0))
1143 *different_types_p = true;
400fbf9f 1144 /* Sizes must match unless one is missing or variable. */
3f85558f 1145 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1146 break;
400fbf9f 1147
3f75a254
JM
1148 d1_zero = !TYPE_MAX_VALUE (d1);
1149 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1150
3f75a254 1151 d1_variable = (!d1_zero
3f85558f
RH
1152 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1153 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1154 d2_variable = (!d2_zero
3f85558f
RH
1155 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1156 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1157 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1158 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1159
dc5027f4
JM
1160 if (different_types_p != NULL
1161 && d1_variable != d2_variable)
1162 *different_types_p = true;
3f85558f
RH
1163 if (d1_variable || d2_variable)
1164 break;
1165 if (d1_zero && d2_zero)
1166 break;
1167 if (d1_zero || d2_zero
3f75a254
JM
1168 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1169 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1170 val = 0;
1171
c22cacf3 1172 break;
400fbf9f
JW
1173 }
1174
d1bd0ded 1175 case ENUMERAL_TYPE:
58393038 1176 case RECORD_TYPE:
d1bd0ded 1177 case UNION_TYPE:
766beae1 1178 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1179 {
fcb99e7b
JJ
1180 tree a1 = TYPE_ATTRIBUTES (t1);
1181 tree a2 = TYPE_ATTRIBUTES (t2);
1182
1183 if (! attribute_list_contained (a1, a2)
1184 && ! attribute_list_contained (a2, a1))
1185 break;
1186
f13c9b2c 1187 if (attrval != 2)
dc5027f4
JM
1188 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1189 different_types_p);
1190 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1191 different_types_p);
f13c9b2c 1192 }
4b027d16 1193 break;
e9a25f70 1194
62e1dfcf 1195 case VECTOR_TYPE:
744aa42f
ILT
1196 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1197 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1198 enum_and_int_p, different_types_p));
62e1dfcf
NC
1199 break;
1200
e9a25f70
JL
1201 default:
1202 break;
400fbf9f 1203 }
4b027d16 1204 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1205}
1206
36c5e70a
BE
1207/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1208 their qualifiers, except for named address spaces. If the pointers point to
1209 different named addresses, then we must determine if one address space is a
1210 subset of the other. */
400fbf9f
JW
1211
1212static int
744aa42f 1213comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1214{
392202b0 1215 int val;
768952be 1216 int val_ped;
36c5e70a
BE
1217 tree mvl = TREE_TYPE (ttl);
1218 tree mvr = TREE_TYPE (ttr);
1219 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1220 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1221 addr_space_t as_common;
744aa42f 1222 bool enum_and_int_p;
8b40563c 1223
36c5e70a
BE
1224 /* Fail if pointers point to incompatible address spaces. */
1225 if (!addr_space_superset (asl, asr, &as_common))
1226 return 0;
1227
768952be
MU
1228 /* For pedantic record result of comptypes on arrays before losing
1229 qualifiers on the element type below. */
1230 val_ped = 1;
1231
1232 if (TREE_CODE (mvl) == ARRAY_TYPE
1233 && TREE_CODE (mvr) == ARRAY_TYPE)
1234 val_ped = comptypes (mvl, mvr);
1235
1236 /* Qualifiers on element types of array types that are
1237 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1238
1239 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1240 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1241 : TYPE_MAIN_VARIANT (mvl));
1242
1243 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1244 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1245 : TYPE_MAIN_VARIANT (mvr));
1246
744aa42f
ILT
1247 enum_and_int_p = false;
1248 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1249
768952be
MU
1250 if (val == 1 && val_ped != 1)
1251 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1252 "are incompatible in ISO C");
1253
fcf73884 1254 if (val == 2)
c1771a20 1255 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1256
1257 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1258 warning_at (location, OPT_Wc___compat,
1259 "pointer target types incompatible in C++");
1260
400fbf9f
JW
1261 return val;
1262}
1263\f
1264/* Subroutines of `comptypes'. */
1265
f75fbaf7
ZW
1266/* Determine whether two trees derive from the same translation unit.
1267 If the CONTEXT chain ends in a null, that tree's context is still
1268 being parsed, so if two trees have context chains ending in null,
766beae1 1269 they're in the same translation unit. */
f75fbaf7 1270int
58f9752a 1271same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1272{
1273 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1274 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1275 {
6615c446
JO
1276 case tcc_declaration:
1277 t1 = DECL_CONTEXT (t1); break;
1278 case tcc_type:
1279 t1 = TYPE_CONTEXT (t1); break;
1280 case tcc_exceptional:
1281 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1282 default: gcc_unreachable ();
766beae1
ZW
1283 }
1284
1285 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1286 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1287 {
6615c446
JO
1288 case tcc_declaration:
1289 t2 = DECL_CONTEXT (t2); break;
1290 case tcc_type:
1291 t2 = TYPE_CONTEXT (t2); break;
1292 case tcc_exceptional:
1293 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1294 default: gcc_unreachable ();
766beae1
ZW
1295 }
1296
1297 return t1 == t2;
1298}
1299
f13c9b2c 1300/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1301
f13c9b2c 1302static struct tagged_tu_seen_cache *
58f9752a 1303alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1304{
cceb1885 1305 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1306 tu->next = tagged_tu_seen_base;
1307 tu->t1 = t1;
1308 tu->t2 = t2;
c22cacf3 1309
f13c9b2c 1310 tagged_tu_seen_base = tu;
c22cacf3 1311
f13c9b2c
AP
1312 /* The C standard says that two structures in different translation
1313 units are compatible with each other only if the types of their
1314 fields are compatible (among other things). We assume that they
1315 are compatible until proven otherwise when building the cache.
1316 An example where this can occur is:
1317 struct a
1318 {
1319 struct a *next;
1320 };
1321 If we are comparing this against a similar struct in another TU,
c83eecad 1322 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1323 loop. */
1324 tu->val = 1;
1325 return tu;
1326}
d1bd0ded 1327
f13c9b2c 1328/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1329
f13c9b2c
AP
1330static void
1331free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1332{
1333 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1334 while (tu != tu_til)
1335 {
741ac903
KG
1336 const struct tagged_tu_seen_cache *const tu1
1337 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1338 tu = tu1->next;
b1d5455a 1339 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1340 }
1341 tagged_tu_seen_base = tu_til;
1342}
d1bd0ded
GK
1343
1344/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1345 compatible. If the two types are not the same (which has been
1346 checked earlier), this can only happen when multiple translation
1347 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1348 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1349 comptypes_internal. */
d1bd0ded
GK
1350
1351static int
744aa42f 1352tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1353 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1354{
1355 tree s1, s2;
1356 bool needs_warning = false;
3aeb3655 1357
d1bd0ded
GK
1358 /* We have to verify that the tags of the types are the same. This
1359 is harder than it looks because this may be a typedef, so we have
1360 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1361 typedef...
1362 In the case of compiler-created builtin structs the TYPE_DECL
1363 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1364 while (TYPE_NAME (t1)
1365 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1366 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1367 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1368
dea984dc
ILT
1369 while (TYPE_NAME (t2)
1370 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1371 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1372 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1373
1374 /* C90 didn't have the requirement that the two tags be the same. */
1375 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1376 return 0;
3aeb3655 1377
d1bd0ded
GK
1378 /* C90 didn't say what happened if one or both of the types were
1379 incomplete; we choose to follow C99 rules here, which is that they
1380 are compatible. */
1381 if (TYPE_SIZE (t1) == NULL
1382 || TYPE_SIZE (t2) == NULL)
1383 return 1;
3aeb3655 1384
d1bd0ded 1385 {
f13c9b2c 1386 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1387 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1388 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1389 return tts_i->val;
d1bd0ded 1390 }
3aeb3655 1391
d1bd0ded
GK
1392 switch (TREE_CODE (t1))
1393 {
1394 case ENUMERAL_TYPE:
1395 {
f13c9b2c 1396 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1397 /* Speed up the case where the type values are in the same order. */
1398 tree tv1 = TYPE_VALUES (t1);
1399 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1400
c22cacf3 1401 if (tv1 == tv2)
f13c9b2c
AP
1402 {
1403 return 1;
1404 }
3aeb3655 1405
c22cacf3
MS
1406 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1407 {
1408 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1409 break;
1410 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1411 {
c22cacf3 1412 tu->val = 0;
f13c9b2c
AP
1413 return 0;
1414 }
c22cacf3 1415 }
3aeb3655 1416
c22cacf3 1417 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1418 {
1419 return 1;
1420 }
c22cacf3 1421 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1422 {
1423 tu->val = 0;
1424 return 0;
1425 }
3aeb3655 1426
d1bd0ded 1427 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1428 {
1429 tu->val = 0;
1430 return 0;
1431 }
3aeb3655 1432
d1bd0ded
GK
1433 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1434 {
1435 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1436 if (s2 == NULL
1437 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1438 {
1439 tu->val = 0;
1440 return 0;
1441 }
d1bd0ded
GK
1442 }
1443 return 1;
1444 }
1445
1446 case UNION_TYPE:
1447 {
f13c9b2c 1448 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1449 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1450 {
1451 tu->val = 0;
1452 return 0;
1453 }
c22cacf3 1454
f13c9b2c
AP
1455 /* Speed up the common case where the fields are in the same order. */
1456 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1457 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1458 {
1459 int result;
c22cacf3 1460
3ae4d3cc 1461 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1462 break;
744aa42f 1463 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1464 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1465
1466 if (result != 1 && !DECL_NAME (s1))
1467 break;
f13c9b2c
AP
1468 if (result == 0)
1469 {
1470 tu->val = 0;
1471 return 0;
1472 }
1473 if (result == 2)
1474 needs_warning = true;
1475
1476 if (TREE_CODE (s1) == FIELD_DECL
1477 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1478 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1479 {
1480 tu->val = 0;
1481 return 0;
1482 }
1483 }
1484 if (!s1 && !s2)
1485 {
1486 tu->val = needs_warning ? 2 : 1;
1487 return tu->val;
1488 }
d1bd0ded 1489
910ad8de 1490 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1491 {
1492 bool ok = false;
3aeb3655 1493
910ad8de 1494 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1495 if (DECL_NAME (s1) == DECL_NAME (s2))
1496 {
1497 int result;
1498
744aa42f 1499 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1500 enum_and_int_p,
1501 different_types_p);
3ae4d3cc
AO
1502
1503 if (result != 1 && !DECL_NAME (s1))
1504 continue;
1505 if (result == 0)
1506 {
1507 tu->val = 0;
1508 return 0;
1509 }
1510 if (result == 2)
1511 needs_warning = true;
1512
1513 if (TREE_CODE (s1) == FIELD_DECL
1514 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1515 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1516 break;
3ae4d3cc
AO
1517
1518 ok = true;
1519 break;
1520 }
3f75a254 1521 if (!ok)
f13c9b2c
AP
1522 {
1523 tu->val = 0;
1524 return 0;
1525 }
d1bd0ded 1526 }
f13c9b2c
AP
1527 tu->val = needs_warning ? 2 : 10;
1528 return tu->val;
d1bd0ded
GK
1529 }
1530
1531 case RECORD_TYPE:
1532 {
c22cacf3 1533 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1534
1535 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1536 s1 && s2;
910ad8de 1537 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1538 {
1539 int result;
1540 if (TREE_CODE (s1) != TREE_CODE (s2)
1541 || DECL_NAME (s1) != DECL_NAME (s2))
1542 break;
744aa42f 1543 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1544 enum_and_int_p, different_types_p);
d1bd0ded
GK
1545 if (result == 0)
1546 break;
1547 if (result == 2)
1548 needs_warning = true;
3aeb3655 1549
d1bd0ded
GK
1550 if (TREE_CODE (s1) == FIELD_DECL
1551 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1552 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1553 break;
1554 }
d1bd0ded 1555 if (s1 && s2)
f13c9b2c
AP
1556 tu->val = 0;
1557 else
1558 tu->val = needs_warning ? 2 : 1;
1559 return tu->val;
d1bd0ded
GK
1560 }
1561
1562 default:
366de0ce 1563 gcc_unreachable ();
d1bd0ded
GK
1564 }
1565}
1566
400fbf9f
JW
1567/* Return 1 if two function types F1 and F2 are compatible.
1568 If either type specifies no argument types,
1569 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1570 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1571 the other must specify that number of self-promoting arg types.
744aa42f 1572 Otherwise, the argument types must match.
dc5027f4 1573 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1574
1575static int
744aa42f 1576function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1577 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1578{
1579 tree args1, args2;
1580 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1581 int val = 1;
1582 int val1;
a6fdc086
GK
1583 tree ret1, ret2;
1584
1585 ret1 = TREE_TYPE (f1);
1586 ret2 = TREE_TYPE (f2);
1587
e508a019
JM
1588 /* 'volatile' qualifiers on a function's return type used to mean
1589 the function is noreturn. */
1590 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1591 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1592 if (TYPE_VOLATILE (ret1))
1593 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1594 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1595 if (TYPE_VOLATILE (ret2))
1596 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1597 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1598 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1599 if (val == 0)
400fbf9f
JW
1600 return 0;
1601
1602 args1 = TYPE_ARG_TYPES (f1);
1603 args2 = TYPE_ARG_TYPES (f2);
1604
dc5027f4
JM
1605 if (different_types_p != NULL
1606 && (args1 == 0) != (args2 == 0))
1607 *different_types_p = true;
1608
400fbf9f
JW
1609 /* An unspecified parmlist matches any specified parmlist
1610 whose argument types don't need default promotions. */
1611
1612 if (args1 == 0)
1613 {
1614 if (!self_promoting_args_p (args2))
1615 return 0;
1616 /* If one of these types comes from a non-prototype fn definition,
1617 compare that with the other type's arglist.
3176a0c2 1618 If they don't match, ask for a warning (but no error). */
400fbf9f 1619 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1620 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1621 enum_and_int_p, different_types_p))
400fbf9f
JW
1622 val = 2;
1623 return val;
1624 }
1625 if (args2 == 0)
1626 {
1627 if (!self_promoting_args_p (args1))
1628 return 0;
1629 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1630 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1631 enum_and_int_p, different_types_p))
400fbf9f
JW
1632 val = 2;
1633 return val;
1634 }
1635
1636 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1637 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1638 different_types_p);
400fbf9f
JW
1639 return val1 != 1 ? val1 : val;
1640}
1641
744aa42f
ILT
1642/* Check two lists of types for compatibility, returning 0 for
1643 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1644 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1645 comptypes_internal. */
400fbf9f
JW
1646
1647static int
744aa42f 1648type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1649 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1650{
1651 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1652 int val = 1;
9d5f3e49 1653 int newval = 0;
400fbf9f
JW
1654
1655 while (1)
1656 {
46df2823 1657 tree a1, mv1, a2, mv2;
400fbf9f
JW
1658 if (args1 == 0 && args2 == 0)
1659 return val;
1660 /* If one list is shorter than the other,
1661 they fail to match. */
1662 if (args1 == 0 || args2 == 0)
1663 return 0;
46df2823
JM
1664 mv1 = a1 = TREE_VALUE (args1);
1665 mv2 = a2 = TREE_VALUE (args2);
1666 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1667 mv1 = (TYPE_ATOMIC (mv1)
1668 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1669 TYPE_QUAL_ATOMIC)
1670 : TYPE_MAIN_VARIANT (mv1));
46df2823 1671 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1672 mv2 = (TYPE_ATOMIC (mv2)
1673 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1674 TYPE_QUAL_ATOMIC)
1675 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1676 /* A null pointer instead of a type
1677 means there is supposed to be an argument
1678 but nothing is specified about what type it has.
1679 So match anything that self-promotes. */
dc5027f4
JM
1680 if (different_types_p != NULL
1681 && (a1 == 0) != (a2 == 0))
1682 *different_types_p = true;
46df2823 1683 if (a1 == 0)
400fbf9f 1684 {
46df2823 1685 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1686 return 0;
1687 }
46df2823 1688 else if (a2 == 0)
400fbf9f 1689 {
46df2823 1690 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1691 return 0;
1692 }
8f5b6d29 1693 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1694 else if (TREE_CODE (a1) == ERROR_MARK
1695 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1696 ;
dc5027f4
JM
1697 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1698 different_types_p)))
400fbf9f 1699 {
dc5027f4
JM
1700 if (different_types_p != NULL)
1701 *different_types_p = true;
400fbf9f
JW
1702 /* Allow wait (union {union wait *u; int *i} *)
1703 and wait (union wait *) to be compatible. */
46df2823
JM
1704 if (TREE_CODE (a1) == UNION_TYPE
1705 && (TYPE_NAME (a1) == 0
ebf0bf7f 1706 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1707 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1708 && tree_int_cst_equal (TYPE_SIZE (a1),
1709 TYPE_SIZE (a2)))
400fbf9f
JW
1710 {
1711 tree memb;
46df2823 1712 for (memb = TYPE_FIELDS (a1);
910ad8de 1713 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1714 {
1715 tree mv3 = TREE_TYPE (memb);
1716 if (mv3 && mv3 != error_mark_node
1717 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1718 mv3 = (TYPE_ATOMIC (mv3)
1719 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1720 TYPE_QUAL_ATOMIC)
1721 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1722 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1723 different_types_p))
58cb41e6
JJ
1724 break;
1725 }
400fbf9f
JW
1726 if (memb == 0)
1727 return 0;
1728 }
46df2823
JM
1729 else if (TREE_CODE (a2) == UNION_TYPE
1730 && (TYPE_NAME (a2) == 0
ebf0bf7f 1731 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1732 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1733 && tree_int_cst_equal (TYPE_SIZE (a2),
1734 TYPE_SIZE (a1)))
400fbf9f
JW
1735 {
1736 tree memb;
46df2823 1737 for (memb = TYPE_FIELDS (a2);
910ad8de 1738 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1739 {
1740 tree mv3 = TREE_TYPE (memb);
1741 if (mv3 && mv3 != error_mark_node
1742 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1743 mv3 = (TYPE_ATOMIC (mv3)
1744 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1745 TYPE_QUAL_ATOMIC)
1746 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1747 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1748 different_types_p))
58cb41e6
JJ
1749 break;
1750 }
400fbf9f
JW
1751 if (memb == 0)
1752 return 0;
1753 }
1754 else
1755 return 0;
1756 }
1757
1758 /* comptypes said ok, but record if it said to warn. */
1759 if (newval > val)
1760 val = newval;
1761
1762 args1 = TREE_CHAIN (args1);
1763 args2 = TREE_CHAIN (args2);
1764 }
1765}
400fbf9f 1766\f
a0e24419
MP
1767/* Compute the size to increment a pointer by. When a function type or void
1768 type or incomplete type is passed, size_one_node is returned.
1769 This function does not emit any diagnostics; the caller is responsible
1770 for that. */
400fbf9f 1771
4e2fb7de 1772static tree
58f9752a 1773c_size_in_bytes (const_tree type)
400fbf9f
JW
1774{
1775 enum tree_code code = TREE_CODE (type);
1776
a0e24419
MP
1777 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1778 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1779 return size_one_node;
1780
400fbf9f 1781 /* Convert in case a char is more than one unit. */
db3927fb
AH
1782 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1783 size_int (TYPE_PRECISION (char_type_node)
1784 / BITS_PER_UNIT));
400fbf9f 1785}
400fbf9f 1786\f
400fbf9f
JW
1787/* Return either DECL or its known constant value (if it has one). */
1788
56cb9733 1789tree
2f6e4e97 1790decl_constant_value (tree decl)
400fbf9f 1791{
a7c1916a 1792 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1793 in a place where a variable is invalid. Note that DECL_INITIAL
1794 isn't valid for a PARM_DECL. */
a7c1916a 1795 current_function_decl != 0
4f976745 1796 && TREE_CODE (decl) != PARM_DECL
3f75a254 1797 && !TREE_THIS_VOLATILE (decl)
83bab8db 1798 && TREE_READONLY (decl)
400fbf9f
JW
1799 && DECL_INITIAL (decl) != 0
1800 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1801 /* This is invalid if initial value is not constant.
1802 If it has either a function call, a memory reference,
1803 or a variable, then re-evaluating it could give different results. */
1804 && TREE_CONSTANT (DECL_INITIAL (decl))
1805 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1806 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1807 return DECL_INITIAL (decl);
1808 return decl;
1809}
1810
f2a71bbc
JM
1811/* Convert the array expression EXP to a pointer. */
1812static tree
c2255bc4 1813array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1814{
f2a71bbc 1815 tree orig_exp = exp;
207bf485 1816 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1817 tree adr;
1818 tree restype = TREE_TYPE (type);
1819 tree ptrtype;
207bf485 1820
f2a71bbc 1821 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1822
f2a71bbc 1823 STRIP_TYPE_NOPS (exp);
207bf485 1824
487a92fe
JM
1825 if (TREE_NO_WARNING (orig_exp))
1826 TREE_NO_WARNING (exp) = 1;
207bf485 1827
f2a71bbc
JM
1828 ptrtype = build_pointer_type (restype);
1829
1830 if (TREE_CODE (exp) == INDIRECT_REF)
1831 return convert (ptrtype, TREE_OPERAND (exp, 0));
1832
1f37c583
JM
1833 /* In C++ array compound literals are temporary objects unless they are
1834 const or appear in namespace scope, so they are destroyed too soon
1835 to use them for much of anything (c++/53220). */
1836 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1837 {
1838 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1839 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1840 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1841 "converting an array compound literal to a pointer "
1842 "is ill-formed in C++");
1843 }
1844
c2255bc4 1845 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1846 return convert (ptrtype, adr);
1847}
207bf485 1848
f2a71bbc
JM
1849/* Convert the function expression EXP to a pointer. */
1850static tree
c2255bc4 1851function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1852{
1853 tree orig_exp = exp;
207bf485 1854
f2a71bbc 1855 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1856
f2a71bbc 1857 STRIP_TYPE_NOPS (exp);
207bf485 1858
f2a71bbc
JM
1859 if (TREE_NO_WARNING (orig_exp))
1860 TREE_NO_WARNING (exp) = 1;
207bf485 1861
c2255bc4 1862 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1863}
207bf485 1864
ebfbbdc5
JJ
1865/* Mark EXP as read, not just set, for set but not used -Wunused
1866 warning purposes. */
1867
1868void
1869mark_exp_read (tree exp)
1870{
1871 switch (TREE_CODE (exp))
1872 {
1873 case VAR_DECL:
1874 case PARM_DECL:
1875 DECL_READ_P (exp) = 1;
1876 break;
1877 case ARRAY_REF:
1878 case COMPONENT_REF:
1879 case MODIFY_EXPR:
1880 case REALPART_EXPR:
1881 case IMAGPART_EXPR:
1882 CASE_CONVERT:
1883 case ADDR_EXPR:
1884 mark_exp_read (TREE_OPERAND (exp, 0));
1885 break;
1886 case COMPOUND_EXPR:
82c3c067 1887 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1888 mark_exp_read (TREE_OPERAND (exp, 1));
1889 break;
1890 default:
1891 break;
1892 }
1893}
1894
f2a71bbc
JM
1895/* Perform the default conversion of arrays and functions to pointers.
1896 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1897 return EXP.
1898
1899 LOC is the location of the expression. */
f2a71bbc
JM
1900
1901struct c_expr
c2255bc4 1902default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1903{
1904 tree orig_exp = exp.value;
1905 tree type = TREE_TYPE (exp.value);
1906 enum tree_code code = TREE_CODE (type);
1907
1908 switch (code)
1909 {
1910 case ARRAY_TYPE:
1911 {
1912 bool not_lvalue = false;
1913 bool lvalue_array_p;
1914
1915 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1916 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1917 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1918 {
1919 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1920 not_lvalue = true;
1921 exp.value = TREE_OPERAND (exp.value, 0);
1922 }
1923
1924 if (TREE_NO_WARNING (orig_exp))
1925 TREE_NO_WARNING (exp.value) = 1;
1926
1927 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1928 if (!flag_isoc99 && !lvalue_array_p)
1929 {
1930 /* Before C99, non-lvalue arrays do not decay to pointers.
1931 Normally, using such an array would be invalid; but it can
1932 be used correctly inside sizeof or as a statement expression.
1933 Thus, do not give an error here; an error will result later. */
1934 return exp;
1935 }
1936
c2255bc4 1937 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1938 }
1939 break;
1940 case FUNCTION_TYPE:
c2255bc4 1941 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1942 break;
1943 default:
f2a71bbc 1944 break;
207bf485 1945 }
f2a71bbc 1946
207bf485
JM
1947 return exp;
1948}
1949
ebfbbdc5
JJ
1950struct c_expr
1951default_function_array_read_conversion (location_t loc, struct c_expr exp)
1952{
1953 mark_exp_read (exp.value);
1954 return default_function_array_conversion (loc, exp);
1955}
522ddfa2 1956
267bac10
JM
1957/* Return whether EXPR should be treated as an atomic lvalue for the
1958 purposes of load and store handling. */
1959
1960static bool
1961really_atomic_lvalue (tree expr)
1962{
7a0ca710 1963 if (error_operand_p (expr))
267bac10
JM
1964 return false;
1965 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1966 return false;
1967 if (!lvalue_p (expr))
1968 return false;
1969
1970 /* Ignore _Atomic on register variables, since their addresses can't
1971 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1972 sequences wouldn't work. Ignore _Atomic on structures containing
1973 bit-fields, since accessing elements of atomic structures or
1974 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1975 it's undefined at translation time or execution time, and the
1976 normal atomic sequences again wouldn't work. */
1977 while (handled_component_p (expr))
1978 {
1979 if (TREE_CODE (expr) == COMPONENT_REF
1980 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1981 return false;
1982 expr = TREE_OPERAND (expr, 0);
1983 }
1984 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1985 return false;
1986 return true;
1987}
1988
1989/* Convert expression EXP (location LOC) from lvalue to rvalue,
1990 including converting functions and arrays to pointers if CONVERT_P.
1991 If READ_P, also mark the expression as having been read. */
1992
1993struct c_expr
1994convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1995 bool convert_p, bool read_p)
1996{
1997 if (read_p)
1998 mark_exp_read (exp.value);
1999 if (convert_p)
2000 exp = default_function_array_conversion (loc, exp);
2001 if (really_atomic_lvalue (exp.value))
2002 {
2003 vec<tree, va_gc> *params;
2004 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2005 tree expr_type = TREE_TYPE (exp.value);
2006 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2007 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2008
2009 gcc_assert (TYPE_ATOMIC (expr_type));
2010
2011 /* Expansion of a generic atomic load may require an addition
2012 element, so allocate enough to prevent a resize. */
2013 vec_alloc (params, 4);
2014
2015 /* Remove the qualifiers for the rest of the expressions and
2016 create the VAL temp variable to hold the RHS. */
2017 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2018 tmp = create_tmp_var_raw (nonatomic_type);
267bac10
JM
2019 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2020 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2021 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2022
2023 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2024 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2025 params->quick_push (expr_addr);
2026 params->quick_push (tmp_addr);
2027 params->quick_push (seq_cst);
8edbfaa6 2028 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2029
cc28fc7f
MP
2030 /* EXPR is always read. */
2031 mark_exp_read (exp.value);
2032
267bac10 2033 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2034 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2035 NULL_TREE, NULL_TREE);
267bac10
JM
2036 }
2037 return exp;
2038}
2039
522ddfa2
JM
2040/* EXP is an expression of integer type. Apply the integer promotions
2041 to it and return the promoted value. */
400fbf9f
JW
2042
2043tree
522ddfa2 2044perform_integral_promotions (tree exp)
400fbf9f 2045{
b3694847
SS
2046 tree type = TREE_TYPE (exp);
2047 enum tree_code code = TREE_CODE (type);
400fbf9f 2048
522ddfa2 2049 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2050
400fbf9f
JW
2051 /* Normally convert enums to int,
2052 but convert wide enums to something wider. */
2053 if (code == ENUMERAL_TYPE)
2054 {
b0c48229
NB
2055 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2056 TYPE_PRECISION (integer_type_node)),
2057 ((TYPE_PRECISION (type)
2058 >= TYPE_PRECISION (integer_type_node))
8df83eae 2059 && TYPE_UNSIGNED (type)));
05bccae2 2060
400fbf9f
JW
2061 return convert (type, exp);
2062 }
2063
522ddfa2
JM
2064 /* ??? This should no longer be needed now bit-fields have their
2065 proper types. */
9753f113 2066 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2067 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2068 /* If it's thinner than an int, promote it like a
d72040f5 2069 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2070 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2071 TYPE_PRECISION (integer_type_node)))
f458d1d5 2072 return convert (integer_type_node, exp);
9753f113 2073
d72040f5 2074 if (c_promoting_integer_type_p (type))
400fbf9f 2075 {
f458d1d5 2076 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2077 if (TYPE_UNSIGNED (type)
f458d1d5 2078 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2079 return convert (unsigned_type_node, exp);
05bccae2 2080
400fbf9f
JW
2081 return convert (integer_type_node, exp);
2082 }
05bccae2 2083
522ddfa2
JM
2084 return exp;
2085}
2086
2087
2088/* Perform default promotions for C data used in expressions.
46bdb9cf 2089 Enumeral types or short or char are converted to int.
522ddfa2
JM
2090 In addition, manifest constants symbols are replaced by their values. */
2091
2092tree
2093default_conversion (tree exp)
2094{
2095 tree orig_exp;
2096 tree type = TREE_TYPE (exp);
2097 enum tree_code code = TREE_CODE (type);
40449a90 2098 tree promoted_type;
522ddfa2 2099
ebfbbdc5
JJ
2100 mark_exp_read (exp);
2101
46bdb9cf
JM
2102 /* Functions and arrays have been converted during parsing. */
2103 gcc_assert (code != FUNCTION_TYPE);
2104 if (code == ARRAY_TYPE)
2105 return exp;
522ddfa2
JM
2106
2107 /* Constants can be used directly unless they're not loadable. */
2108 if (TREE_CODE (exp) == CONST_DECL)
2109 exp = DECL_INITIAL (exp);
2110
522ddfa2
JM
2111 /* Strip no-op conversions. */
2112 orig_exp = exp;
2113 STRIP_TYPE_NOPS (exp);
2114
2115 if (TREE_NO_WARNING (orig_exp))
2116 TREE_NO_WARNING (exp) = 1;
2117
400fbf9f
JW
2118 if (code == VOID_TYPE)
2119 {
5436fa2e
MP
2120 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2121 "void value not ignored as it ought to be");
400fbf9f
JW
2122 return error_mark_node;
2123 }
808d6eaa
JM
2124
2125 exp = require_complete_type (exp);
2126 if (exp == error_mark_node)
2127 return error_mark_node;
2128
40449a90
SL
2129 promoted_type = targetm.promoted_type (type);
2130 if (promoted_type)
2131 return convert (promoted_type, exp);
2132
808d6eaa
JM
2133 if (INTEGRAL_TYPE_P (type))
2134 return perform_integral_promotions (exp);
2135
400fbf9f
JW
2136 return exp;
2137}
2138\f
0fb96aa4 2139/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2140
2141 If the component name is not found, returns NULL_TREE. Otherwise,
2142 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2143 stepping down the chain to the component, which is in the last
2144 TREE_VALUE of the list. Normally the list is of length one, but if
2145 the component is embedded within (nested) anonymous structures or
2146 unions, the list steps down the chain to the component. */
2f6e4e97 2147
2f2d13da 2148static tree
0fb96aa4 2149lookup_field (tree type, tree component)
2f2d13da
DE
2150{
2151 tree field;
2152
2153 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2154 to the field elements. Use a binary search on this array to quickly
2155 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2156 will always be set for structures which have many elements. */
2157
22a0b85f 2158 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2159 {
2160 int bot, top, half;
d07605f5 2161 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2162
2163 field = TYPE_FIELDS (type);
2164 bot = 0;
d07605f5 2165 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2166 while (top - bot > 1)
2167 {
2f2d13da
DE
2168 half = (top - bot + 1) >> 1;
2169 field = field_array[bot+half];
2170
2171 if (DECL_NAME (field) == NULL_TREE)
2172 {
2173 /* Step through all anon unions in linear fashion. */
2174 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2175 {
2f2d13da 2176 field = field_array[bot++];
a68b98cf
RK
2177 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2178 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2179 {
0fb96aa4 2180 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2181
2182 if (anon)
2183 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2184
2185 /* The Plan 9 compiler permits referring
2186 directly to an anonymous struct/union field
2187 using a typedef name. */
2188 if (flag_plan9_extensions
2189 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2190 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2191 == TYPE_DECL)
2192 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2193 == component))
2194 break;
2f6e4e97 2195 }
2f2d13da
DE
2196 }
2197
2198 /* Entire record is only anon unions. */
2199 if (bot > top)
2200 return NULL_TREE;
2201
2202 /* Restart the binary search, with new lower bound. */
2203 continue;
2204 }
2205
e8b87aac 2206 if (DECL_NAME (field) == component)
2f2d13da 2207 break;
e8b87aac 2208 if (DECL_NAME (field) < component)
2f2d13da
DE
2209 bot += half;
2210 else
2211 top = bot + half;
2212 }
2213
2214 if (DECL_NAME (field_array[bot]) == component)
2215 field = field_array[bot];
2216 else if (DECL_NAME (field) != component)
e9b2c823 2217 return NULL_TREE;
2f2d13da
DE
2218 }
2219 else
2220 {
910ad8de 2221 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2222 {
e9b2c823
NB
2223 if (DECL_NAME (field) == NULL_TREE
2224 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2225 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2226 {
0fb96aa4 2227 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2228
e9b2c823
NB
2229 if (anon)
2230 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2231
2232 /* The Plan 9 compiler permits referring directly to an
2233 anonymous struct/union field using a typedef
2234 name. */
2235 if (flag_plan9_extensions
2236 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2237 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2238 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2239 == component))
2240 break;
2f2d13da
DE
2241 }
2242
2243 if (DECL_NAME (field) == component)
2244 break;
2245 }
e9b2c823
NB
2246
2247 if (field == NULL_TREE)
2248 return NULL_TREE;
2f2d13da
DE
2249 }
2250
e9b2c823 2251 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2252}
2253
c2255bc4
AH
2254/* Make an expression to refer to the COMPONENT field of structure or
2255 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2256 location of the COMPONENT_REF. */
400fbf9f
JW
2257
2258tree
c2255bc4 2259build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2260{
b3694847
SS
2261 tree type = TREE_TYPE (datum);
2262 enum tree_code code = TREE_CODE (type);
2263 tree field = NULL;
2264 tree ref;
1e57bf47 2265 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2266
7a3ea201
RH
2267 if (!objc_is_public (datum, component))
2268 return error_mark_node;
2269
46a88c12 2270 /* Detect Objective-C property syntax object.property. */
668ea4b1 2271 if (c_dialect_objc ()
46a88c12 2272 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2273 return ref;
2274
400fbf9f
JW
2275 /* See if there is a field or component with name COMPONENT. */
2276
2277 if (code == RECORD_TYPE || code == UNION_TYPE)
2278 {
d0f062fb 2279 if (!COMPLETE_TYPE_P (type))
400fbf9f 2280 {
7a228918 2281 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2282 return error_mark_node;
2283 }
2284
0fb96aa4 2285 field = lookup_field (type, component);
400fbf9f
JW
2286
2287 if (!field)
2288 {
c2255bc4 2289 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2290 return error_mark_node;
2291 }
400fbf9f 2292
e9b2c823
NB
2293 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2294 This might be better solved in future the way the C++ front
2295 end does it - by giving the anonymous entities each a
2296 separate name and type, and then have build_component_ref
2297 recursively call itself. We can't do that here. */
46ea50cb 2298 do
19d76e60 2299 {
e9b2c823 2300 tree subdatum = TREE_VALUE (field);
efed193e
JM
2301 int quals;
2302 tree subtype;
1e57bf47 2303 bool use_datum_quals;
e9b2c823
NB
2304
2305 if (TREE_TYPE (subdatum) == error_mark_node)
2306 return error_mark_node;
2307
1e57bf47
JM
2308 /* If this is an rvalue, it does not have qualifiers in C
2309 standard terms and we must avoid propagating such
2310 qualifiers down to a non-lvalue array that is then
2311 converted to a pointer. */
2312 use_datum_quals = (datum_lvalue
2313 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2314
efed193e 2315 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2316 if (use_datum_quals)
2317 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2318 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2319
2320 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2321 NULL_TREE);
c2255bc4 2322 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2323 if (TREE_READONLY (subdatum)
2324 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2325 TREE_READONLY (ref) = 1;
1e57bf47
JM
2326 if (TREE_THIS_VOLATILE (subdatum)
2327 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2328 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2329
2330 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2331 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2332
19d76e60 2333 datum = ref;
46ea50cb
RS
2334
2335 field = TREE_CHAIN (field);
19d76e60 2336 }
46ea50cb 2337 while (field);
19d76e60 2338
400fbf9f
JW
2339 return ref;
2340 }
2341 else if (code != ERROR_MARK)
c2255bc4
AH
2342 error_at (loc,
2343 "request for member %qE in something not a structure or union",
2344 component);
400fbf9f
JW
2345
2346 return error_mark_node;
2347}
2348\f
2349/* Given an expression PTR for a pointer, return an expression
2350 for the value pointed to.
6a3799eb
AH
2351 ERRORSTRING is the name of the operator to appear in error messages.
2352
2353 LOC is the location to use for the generated tree. */
400fbf9f
JW
2354
2355tree
dd865ef6 2356build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2357{
b3694847
SS
2358 tree pointer = default_conversion (ptr);
2359 tree type = TREE_TYPE (pointer);
6a3799eb 2360 tree ref;
400fbf9f
JW
2361
2362 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2363 {
1043771b 2364 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2365 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2366 {
2367 /* If a warning is issued, mark it to avoid duplicates from
2368 the backend. This only needs to be done at
2369 warn_strict_aliasing > 2. */
2370 if (warn_strict_aliasing > 2)
2371 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2372 type, TREE_OPERAND (pointer, 0)))
2373 TREE_NO_WARNING (pointer) = 1;
2374 }
2375
870cc33b 2376 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2377 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2378 == TREE_TYPE (type)))
6a3799eb
AH
2379 {
2380 ref = TREE_OPERAND (pointer, 0);
2381 protected_set_expr_location (ref, loc);
2382 return ref;
2383 }
870cc33b
RS
2384 else
2385 {
2386 tree t = TREE_TYPE (type);
46df2823 2387
984dfd8c 2388 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2389
baae9b65 2390 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2391 {
d9b7be2e
MP
2392 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2393 {
2394 error_at (loc, "dereferencing pointer to incomplete type "
2395 "%qT", t);
2396 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2397 }
870cc33b
RS
2398 return error_mark_node;
2399 }
7d882b83 2400 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2401 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2402
2403 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2404 so that we get the proper error message if the result is used
2405 to assign to. Also, &* is supposed to be a no-op.
2406 And ANSI C seems to specify that the type of the result
2407 should be the const type. */
2408 /* A de-reference of a pointer to const is not a const. It is valid
2409 to change it via some other pointer. */
2410 TREE_READONLY (ref) = TYPE_READONLY (t);
2411 TREE_SIDE_EFFECTS (ref)
271bd540 2412 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2413 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2414 protected_set_expr_location (ref, loc);
870cc33b
RS
2415 return ref;
2416 }
2417 }
400fbf9f 2418 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2419 invalid_indirection_error (loc, type, errstring);
2420
400fbf9f
JW
2421 return error_mark_node;
2422}
2423
2424/* This handles expressions of the form "a[i]", which denotes
2425 an array reference.
2426
2427 This is logically equivalent in C to *(a+i), but we may do it differently.
2428 If A is a variable or a member, we generate a primitive ARRAY_REF.
2429 This avoids forcing the array out of registers, and can work on
2430 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2431 by functions).
2432
30cd1c5d
AS
2433 For vector types, allow vector[i] but not i[vector], and create
2434 *(((type*)&vectortype) + i) for the expression.
2435
6a3799eb 2436 LOC is the location to use for the returned expression. */
400fbf9f
JW
2437
2438tree
c2255bc4 2439build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2440{
6a3799eb 2441 tree ret;
a4ab7973 2442 bool swapped = false;
400fbf9f
JW
2443 if (TREE_TYPE (array) == error_mark_node
2444 || TREE_TYPE (index) == error_mark_node)
2445 return error_mark_node;
2446
b72271b9 2447 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2448 {
2449 size_t rank = 0;
2450 if (!find_rank (loc, index, index, true, &rank))
2451 return error_mark_node;
2452 if (rank > 1)
2453 {
2454 error_at (loc, "rank of the array's index is greater than 1");
2455 return error_mark_node;
2456 }
2457 }
a4ab7973 2458 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2459 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2460 /* Allow vector[index] but not index[vector]. */
2461 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2462 {
a4ab7973
JM
2463 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2464 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2465 {
f90e8e2e 2466 error_at (loc,
30cd1c5d
AS
2467 "subscripted value is neither array nor pointer nor vector");
2468
fdeefd49
RS
2469 return error_mark_node;
2470 }
fab27f52 2471 std::swap (array, index);
a4ab7973
JM
2472 swapped = true;
2473 }
2474
2475 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2476 {
a63068b6 2477 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2478 return error_mark_node;
2479 }
2480
2481 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2482 {
a63068b6 2483 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2484 return error_mark_node;
2485 }
2486
ff6b6641
GDR
2487 /* ??? Existing practice has been to warn only when the char
2488 index is syntactically the index, not for char[array]. */
2489 if (!swapped)
5bd012f8 2490 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2491
2492 /* Apply default promotions *after* noticing character types. */
2493 index = default_conversion (index);
f406ae1f
MP
2494 if (index == error_mark_node)
2495 return error_mark_node;
a4ab7973
JM
2496
2497 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2498
aa7da51a
JJ
2499 bool non_lvalue
2500 = convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2501
2502 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2503 {
e4d35515 2504 tree rval, type;
fdeefd49 2505
400fbf9f
JW
2506 /* An array that is indexed by a non-constant
2507 cannot be stored in a register; we must be able to do
2508 address arithmetic on its address.
2509 Likewise an array of elements of variable size. */
2510 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2511 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2512 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2513 {
dffd7eb6 2514 if (!c_mark_addressable (array))
400fbf9f
JW
2515 return error_mark_node;
2516 }
e6d52559
JW
2517 /* An array that is indexed by a constant value which is not within
2518 the array bounds cannot be stored in a register either; because we
2519 would get a crash in store_bit_field/extract_bit_field when trying
2520 to access a non-existent part of the register. */
2521 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2522 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2523 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2524 {
dffd7eb6 2525 if (!c_mark_addressable (array))
e6d52559
JW
2526 return error_mark_node;
2527 }
400fbf9f 2528
f3bede71 2529 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2530 {
2531 tree foo = array;
2532 while (TREE_CODE (foo) == COMPONENT_REF)
2533 foo = TREE_OPERAND (foo, 0);
5baeaac0 2534 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2535 pedwarn (loc, OPT_Wpedantic,
fcf73884 2536 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2537 else if (!lvalue_p (foo))
2538 pedwarn_c90 (loc, OPT_Wpedantic,
2539 "ISO C90 forbids subscripting non-lvalue "
2540 "array");
400fbf9f
JW
2541 }
2542
46df2823 2543 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2544 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2545 /* Array ref is const/volatile if the array elements are
c22cacf3 2546 or if the array is. */
400fbf9f
JW
2547 TREE_READONLY (rval)
2548 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2549 | TREE_READONLY (array));
2550 TREE_SIDE_EFFECTS (rval)
2551 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2552 | TREE_SIDE_EFFECTS (array));
2553 TREE_THIS_VOLATILE (rval)
2554 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2555 /* This was added by rms on 16 Nov 91.
2f6e4e97 2556 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2557 in an inline function.
2558 Hope it doesn't break something else. */
2559 | TREE_THIS_VOLATILE (array));
928c19bb 2560 ret = require_complete_type (rval);
6a3799eb 2561 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2562 if (non_lvalue)
2563 ret = non_lvalue_loc (loc, ret);
6a3799eb 2564 return ret;
400fbf9f 2565 }
a4ab7973
JM
2566 else
2567 {
2568 tree ar = default_conversion (array);
400fbf9f 2569
a4ab7973
JM
2570 if (ar == error_mark_node)
2571 return ar;
400fbf9f 2572
a4ab7973
JM
2573 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2574 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2575
aa7da51a
JJ
2576 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2577 index, 0),
2578 RO_ARRAY_INDEXING);
2579 if (non_lvalue)
2580 ret = non_lvalue_loc (loc, ret);
2581 return ret;
a4ab7973 2582 }
400fbf9f
JW
2583}
2584\f
7e585d16 2585/* Build an external reference to identifier ID. FUN indicates
766beb40 2586 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2587 location of the identifier. This sets *TYPE to the type of the
2588 identifier, which is not the same as the type of the returned value
2589 for CONST_DECLs defined as enum constants. If the type of the
2590 identifier is not available, *TYPE is set to NULL. */
7e585d16 2591tree
c2255bc4 2592build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2593{
2594 tree ref;
2595 tree decl = lookup_name (id);
16b34ad6
ZL
2596
2597 /* In Objective-C, an instance variable (ivar) may be preferred to
2598 whatever lookup_name() found. */
2599 decl = objc_lookup_ivar (decl, id);
7e585d16 2600
6866c6e8 2601 *type = NULL;
339a28b9 2602 if (decl && decl != error_mark_node)
6866c6e8
ILT
2603 {
2604 ref = decl;
2605 *type = TREE_TYPE (ref);
2606 }
339a28b9
ZW
2607 else if (fun)
2608 /* Implicit function declaration. */
c2255bc4 2609 ref = implicitly_declare (loc, id);
339a28b9
ZW
2610 else if (decl == error_mark_node)
2611 /* Don't complain about something that's already been
2612 complained about. */
2613 return error_mark_node;
2614 else
2615 {
c2255bc4 2616 undeclared_variable (loc, id);
339a28b9
ZW
2617 return error_mark_node;
2618 }
7e585d16
ZW
2619
2620 if (TREE_TYPE (ref) == error_mark_node)
2621 return error_mark_node;
2622
339a28b9 2623 if (TREE_DEPRECATED (ref))
9b86d6bb 2624 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2625
ad960f56 2626 /* Recursive call does not count as usage. */
b8698a0f 2627 if (ref != current_function_decl)
ad960f56 2628 {
ad960f56
MLI
2629 TREE_USED (ref) = 1;
2630 }
7e585d16 2631
bc4b653b
JM
2632 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2633 {
2634 if (!in_sizeof && !in_typeof)
2635 C_DECL_USED (ref) = 1;
2636 else if (DECL_INITIAL (ref) == 0
2637 && DECL_EXTERNAL (ref)
2638 && !TREE_PUBLIC (ref))
2639 record_maybe_used_decl (ref);
2640 }
2641
7e585d16
ZW
2642 if (TREE_CODE (ref) == CONST_DECL)
2643 {
6193b8b7 2644 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2645
2646 if (warn_cxx_compat
2647 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2648 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2649 {
2650 warning_at (loc, OPT_Wc___compat,
2651 ("enum constant defined in struct or union "
2652 "is not visible in C++"));
2653 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2654 }
2655
7e585d16
ZW
2656 ref = DECL_INITIAL (ref);
2657 TREE_CONSTANT (ref) = 1;
2658 }
6a29edea 2659 else if (current_function_decl != 0
4b1e44be 2660 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2661 && (VAR_OR_FUNCTION_DECL_P (ref)
2662 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2663 {
2664 tree context = decl_function_context (ref);
2f6e4e97 2665
6a29edea
EB
2666 if (context != 0 && context != current_function_decl)
2667 DECL_NONLOCAL (ref) = 1;
2668 }
71113fcd
GK
2669 /* C99 6.7.4p3: An inline definition of a function with external
2670 linkage ... shall not contain a reference to an identifier with
2671 internal linkage. */
2672 else if (current_function_decl != 0
2673 && DECL_DECLARED_INLINE_P (current_function_decl)
2674 && DECL_EXTERNAL (current_function_decl)
2675 && VAR_OR_FUNCTION_DECL_P (ref)
2676 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2677 && ! TREE_PUBLIC (ref)
2678 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2679 record_inline_static (loc, current_function_decl, ref,
2680 csi_internal);
7e585d16
ZW
2681
2682 return ref;
2683}
2684
bc4b653b
JM
2685/* Record details of decls possibly used inside sizeof or typeof. */
2686struct maybe_used_decl
2687{
2688 /* The decl. */
2689 tree decl;
2690 /* The level seen at (in_sizeof + in_typeof). */
2691 int level;
2692 /* The next one at this level or above, or NULL. */
2693 struct maybe_used_decl *next;
2694};
2695
2696static struct maybe_used_decl *maybe_used_decls;
2697
2698/* Record that DECL, an undefined static function reference seen
2699 inside sizeof or typeof, might be used if the operand of sizeof is
2700 a VLA type or the operand of typeof is a variably modified
2701 type. */
2702
4e2fb7de 2703static void
bc4b653b
JM
2704record_maybe_used_decl (tree decl)
2705{
2706 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2707 t->decl = decl;
2708 t->level = in_sizeof + in_typeof;
2709 t->next = maybe_used_decls;
2710 maybe_used_decls = t;
2711}
2712
2713/* Pop the stack of decls possibly used inside sizeof or typeof. If
2714 USED is false, just discard them. If it is true, mark them used
2715 (if no longer inside sizeof or typeof) or move them to the next
2716 level up (if still inside sizeof or typeof). */
2717
2718void
2719pop_maybe_used (bool used)
2720{
2721 struct maybe_used_decl *p = maybe_used_decls;
2722 int cur_level = in_sizeof + in_typeof;
2723 while (p && p->level > cur_level)
2724 {
2725 if (used)
2726 {
2727 if (cur_level == 0)
2728 C_DECL_USED (p->decl) = 1;
2729 else
2730 p->level = cur_level;
2731 }
2732 p = p->next;
2733 }
2734 if (!used || cur_level == 0)
2735 maybe_used_decls = p;
2736}
2737
2738/* Return the result of sizeof applied to EXPR. */
2739
2740struct c_expr
c2255bc4 2741c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2742{
2743 struct c_expr ret;
ad97f4be
JM
2744 if (expr.value == error_mark_node)
2745 {
2746 ret.value = error_mark_node;
2747 ret.original_code = ERROR_MARK;
6866c6e8 2748 ret.original_type = NULL;
ad97f4be
JM
2749 pop_maybe_used (false);
2750 }
2751 else
2752 {
928c19bb 2753 bool expr_const_operands = true;
773ec47f
MP
2754
2755 if (TREE_CODE (expr.value) == PARM_DECL
2756 && C_ARRAY_PARAMETER (expr.value))
2757 {
2758 if (warning_at (loc, OPT_Wsizeof_array_argument,
2759 "%<sizeof%> on array function parameter %qE will "
2760 "return size of %qT", expr.value,
2761 expr.original_type))
2762 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2763 }
928c19bb
JM
2764 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2765 &expr_const_operands);
c2255bc4 2766 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2767 c_last_sizeof_arg = expr.value;
2768 ret.original_code = SIZEOF_EXPR;
6866c6e8 2769 ret.original_type = NULL;
928c19bb 2770 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2771 {
2772 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2773 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2774 folded_expr, ret.value);
2775 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2776 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2777 }
928c19bb 2778 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2779 }
bc4b653b
JM
2780 return ret;
2781}
2782
2783/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2784 name passed to sizeof (rather than the type itself). LOC is the
2785 location of the original expression. */
bc4b653b
JM
2786
2787struct c_expr
c2255bc4 2788c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2789{
2790 tree type;
2791 struct c_expr ret;
928c19bb
JM
2792 tree type_expr = NULL_TREE;
2793 bool type_expr_const = true;
2794 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2795 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2796 c_last_sizeof_arg = type;
2797 ret.original_code = SIZEOF_EXPR;
6866c6e8 2798 ret.original_type = NULL;
24070fcb
JM
2799 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2800 && c_vla_type_p (type))
928c19bb 2801 {
24070fcb
JM
2802 /* If the type is a [*] array, it is a VLA but is represented as
2803 having a size of zero. In such a case we must ensure that
2804 the result of sizeof does not get folded to a constant by
2805 c_fully_fold, because if the size is evaluated the result is
2806 not constant and so constraints on zero or negative size
2807 arrays must not be applied when this sizeof call is inside
2808 another array declarator. */
2809 if (!type_expr)
2810 type_expr = integer_zero_node;
928c19bb
JM
2811 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2812 type_expr, ret.value);
2813 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2814 }
16464cc1
VR
2815 pop_maybe_used (type != error_mark_node
2816 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2817 return ret;
2818}
2819
400fbf9f 2820/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2821 The function call is at LOC.
400fbf9f
JW
2822 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2823 TREE_VALUE of each node is a parameter-expression.
2824 FUNCTION's data type may be a function type or a pointer-to-function. */
2825
2826tree
c2255bc4 2827build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2828{
9771b263 2829 vec<tree, va_gc> *v;
bbbbb16a
ILT
2830 tree ret;
2831
9771b263 2832 vec_alloc (v, list_length (params));
bbbbb16a 2833 for (; params; params = TREE_CHAIN (params))
9771b263 2834 v->quick_push (TREE_VALUE (params));
8edbfaa6 2835 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2836 vec_free (v);
bbbbb16a
ILT
2837 return ret;
2838}
2839
ae52741c
MLI
2840/* Give a note about the location of the declaration of DECL. */
2841
c7b70a3c
MP
2842static void
2843inform_declaration (tree decl)
ae52741c 2844{
c7b70a3c 2845 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
2846 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2847}
2848
bbbbb16a
ILT
2849/* Build a function call to function FUNCTION with parameters PARAMS.
2850 ORIGTYPES, if not NULL, is a vector of types; each element is
2851 either NULL or the original type of the corresponding element in
2852 PARAMS. The original type may differ from TREE_TYPE of the
2853 parameter for enums. FUNCTION's data type may be a function type
2854 or pointer-to-function. This function changes the elements of
2855 PARAMS. */
2856
2857tree
81e5eca8
MP
2858build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2859 tree function, vec<tree, va_gc> *params,
9771b263 2860 vec<tree, va_gc> *origtypes)
400fbf9f 2861{
b3694847 2862 tree fntype, fundecl = 0;
4977bab6 2863 tree name = NULL_TREE, result;
c96f4f73 2864 tree tem;
94a0dd7b
SL
2865 int nargs;
2866 tree *argarray;
b8698a0f 2867
400fbf9f 2868
fc76e425 2869 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2870 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2871
2872 /* Convert anything with function type to a pointer-to-function. */
2873 if (TREE_CODE (function) == FUNCTION_DECL)
2874 {
2875 name = DECL_NAME (function);
0a35513e
AH
2876
2877 if (flag_tm)
2878 tm_malloc_replacement (function);
a5eadacc 2879 fundecl = function;
86951993
AM
2880 /* Atomic functions have type checking/casting already done. They are
2881 often rewritten and don't match the original parameter list. */
2882 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2883 origtypes = NULL;
36536d79 2884
b72271b9 2885 if (flag_cilkplus
36536d79
BI
2886 && is_cilkplus_reduce_builtin (function))
2887 origtypes = NULL;
400fbf9f 2888 }
f2a71bbc 2889 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2890 function = function_to_pointer_conversion (loc, function);
400fbf9f 2891
6e955430
ZL
2892 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2893 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2894 if (params && !params->is_empty ())
2895 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2896
928c19bb
JM
2897 function = c_fully_fold (function, false, NULL);
2898
400fbf9f
JW
2899 fntype = TREE_TYPE (function);
2900
2901 if (TREE_CODE (fntype) == ERROR_MARK)
2902 return error_mark_node;
2903
2904 if (!(TREE_CODE (fntype) == POINTER_TYPE
2905 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2906 {
ae52741c
MLI
2907 if (!flag_diagnostics_show_caret)
2908 error_at (loc,
2909 "called object %qE is not a function or function pointer",
2910 function);
2911 else if (DECL_P (function))
2912 {
2913 error_at (loc,
2914 "called object %qD is not a function or function pointer",
2915 function);
2916 inform_declaration (function);
2917 }
2918 else
2919 error_at (loc,
2920 "called object is not a function or function pointer");
400fbf9f
JW
2921 return error_mark_node;
2922 }
2923
5ce89b2e
JM
2924 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2925 current_function_returns_abnormally = 1;
2926
400fbf9f
JW
2927 /* fntype now gets the type of function pointed to. */
2928 fntype = TREE_TYPE (fntype);
2929
ab4194da
JM
2930 /* Convert the parameters to the types declared in the
2931 function prototype, or apply default promotions. */
2932
81e5eca8
MP
2933 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2934 origtypes, function, fundecl);
ab4194da
JM
2935 if (nargs < 0)
2936 return error_mark_node;
2937
c96f4f73 2938 /* Check that the function is called through a compatible prototype.
fa337f3a 2939 If it is not, warn. */
1043771b 2940 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2941 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2942 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2943 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2944 {
2945 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2946
2947 /* This situation leads to run-time undefined behavior. We can't,
2948 therefore, simply error unless we can prove that all possible
2949 executions of the program must execute the code. */
fa337f3a 2950 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2951
fa337f3a
RB
2952 if (VOID_TYPE_P (return_type)
2953 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2954 pedwarn (loc, 0,
2955 "function with qualified void return type called");
2956 }
c96f4f73 2957
9771b263 2958 argarray = vec_safe_address (params);
bbbbb16a 2959
83322951
RG
2960 /* Check that arguments to builtin functions match the expectations. */
2961 if (fundecl
2962 && DECL_BUILT_IN (fundecl)
2963 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2964 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2965 return error_mark_node;
400fbf9f 2966
83322951 2967 /* Check that the arguments to the function are valid. */
dde05067 2968 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2969
928c19bb
JM
2970 if (name != NULL_TREE
2971 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2972 {
928c19bb 2973 if (require_constant_value)
b8698a0f 2974 result =
db3927fb
AH
2975 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2976 function, nargs, argarray);
928c19bb 2977 else
db3927fb
AH
2978 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2979 function, nargs, argarray);
928c19bb
JM
2980 if (TREE_CODE (result) == NOP_EXPR
2981 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2982 STRIP_TYPE_NOPS (result);
bf730f15
RS
2983 }
2984 else
db3927fb
AH
2985 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2986 function, nargs, argarray);
b0b3afb2 2987
71653180 2988 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2989 {
2990 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2991 pedwarn (loc, 0,
3ce62965
JM
2992 "function with qualified void return type called");
2993 return result;
2994 }
1eb8759b 2995 return require_complete_type (result);
400fbf9f 2996}
8edbfaa6
JJ
2997
2998/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2999
3000tree
3001c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3002 tree function, vec<tree, va_gc> *params,
3003 vec<tree, va_gc> *origtypes)
3004{
3005 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3006 STRIP_TYPE_NOPS (function);
3007
3008 /* Convert anything with function type to a pointer-to-function. */
3009 if (TREE_CODE (function) == FUNCTION_DECL)
3010 {
3011 /* Implement type-directed function overloading for builtins.
3012 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3013 handle all the type checking. The result is a complete expression
3014 that implements this function call. */
3015 tree tem = resolve_overloaded_builtin (loc, function, params);
3016 if (tem)
3017 return tem;
3018 }
3019 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3020}
400fbf9f 3021\f
bbbbb16a
ILT
3022/* Convert the argument expressions in the vector VALUES
3023 to the types in the list TYPELIST.
400fbf9f
JW
3024
3025 If TYPELIST is exhausted, or when an element has NULL as its type,
3026 perform the default conversions.
3027
bbbbb16a
ILT
3028 ORIGTYPES is the original types of the expressions in VALUES. This
3029 holds the type of enum values which have been converted to integral
3030 types. It may be NULL.
400fbf9f 3031
03dafa61
JM
3032 FUNCTION is a tree for the called function. It is used only for
3033 error messages, where it is formatted with %qE.
400fbf9f
JW
3034
3035 This is also where warnings about wrong number of args are generated.
3036
81e5eca8
MP
3037 ARG_LOC are locations of function arguments (if any).
3038
94a0dd7b 3039 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3040 than the length of VALUES in some error situations), or -1 on
3041 failure. */
94a0dd7b
SL
3042
3043static int
81e5eca8
MP
3044convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3045 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3046 tree function, tree fundecl)
400fbf9f 3047{
bbbbb16a
ILT
3048 tree typetail, val;
3049 unsigned int parmnum;
06302a02 3050 bool error_args = false;
b5d32c25 3051 const bool type_generic = fundecl
81e5eca8 3052 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3053 bool type_generic_remove_excess_precision = false;
03dafa61 3054 tree selector;
03dafa61 3055
2ac2f164
JM
3056 /* Change pointer to function to the function itself for
3057 diagnostics. */
03dafa61
JM
3058 if (TREE_CODE (function) == ADDR_EXPR
3059 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3060 function = TREE_OPERAND (function, 0);
03dafa61
JM
3061
3062 /* Handle an ObjC selector specially for diagnostics. */
3063 selector = objc_message_selector ();
400fbf9f 3064
8ce94e44
JM
3065 /* For type-generic built-in functions, determine whether excess
3066 precision should be removed (classification) or not
3067 (comparison). */
3068 if (type_generic
3069 && DECL_BUILT_IN (fundecl)
3070 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3071 {
3072 switch (DECL_FUNCTION_CODE (fundecl))
3073 {
3074 case BUILT_IN_ISFINITE:
3075 case BUILT_IN_ISINF:
3076 case BUILT_IN_ISINF_SIGN:
3077 case BUILT_IN_ISNAN:
3078 case BUILT_IN_ISNORMAL:
3079 case BUILT_IN_FPCLASSIFY:
3080 type_generic_remove_excess_precision = true;
3081 break;
3082
3083 default:
3084 type_generic_remove_excess_precision = false;
3085 break;
3086 }
3087 }
b72271b9 3088 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3089 return vec_safe_length (values);
8ce94e44 3090
400fbf9f 3091 /* Scan the given expressions and types, producing individual
bbbbb16a 3092 converted arguments. */
400fbf9f 3093
bbbbb16a 3094 for (typetail = typelist, parmnum = 0;
9771b263 3095 values && values->iterate (parmnum, &val);
bbbbb16a 3096 ++parmnum)
400fbf9f 3097 {
b3694847 3098 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3099 tree valtype = TREE_TYPE (val);
03dafa61
JM
3100 tree rname = function;
3101 int argnum = parmnum + 1;
4d3e6fae 3102 const char *invalid_func_diag;
8ce94e44 3103 bool excess_precision = false;
928c19bb 3104 bool npc;
bbbbb16a 3105 tree parmval;
5c1bc275
MP
3106 /* Some __atomic_* builtins have additional hidden argument at
3107 position 0. */
3108 location_t ploc
3109 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3110 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3111 : input_location;
400fbf9f
JW
3112
3113 if (type == void_type_node)
3114 {
19dc6d01 3115 if (selector)
68fca595 3116 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3117 else
68fca595 3118 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3119 inform_declaration (fundecl);
d38f7dce 3120 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3121 }
3122
03dafa61
JM
3123 if (selector && argnum > 2)
3124 {
3125 rname = selector;
3126 argnum -= 2;
3127 }
3128
928c19bb 3129 npc = null_pointer_constant_p (val);
8ce94e44
JM
3130
3131 /* If there is excess precision and a prototype, convert once to
3132 the required type rather than converting via the semantic
3133 type. Likewise without a prototype a float value represented
3134 as long double should be converted once to double. But for
3135 type-generic classification functions excess precision must
3136 be removed here. */
3137 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3138 && (type || !type_generic || !type_generic_remove_excess_precision))
3139 {
3140 val = TREE_OPERAND (val, 0);
3141 excess_precision = true;
3142 }
928c19bb 3143 val = c_fully_fold (val, false, NULL);
ed248cf7 3144 STRIP_TYPE_NOPS (val);
400fbf9f 3145
400fbf9f
JW
3146 val = require_complete_type (val);
3147
3148 if (type != 0)
3149 {
3150 /* Formal parm type is specified by a function prototype. */
400fbf9f 3151
20913689 3152 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3153 {
5c1bc275
MP
3154 error_at (ploc, "type of formal parameter %d is incomplete",
3155 parmnum + 1);
400fbf9f
JW
3156 parmval = val;
3157 }
3158 else
3159 {
bbbbb16a
ILT
3160 tree origtype;
3161
d45cf215
RS
3162 /* Optionally warn about conversions that
3163 differ from the default conversions. */
05170031 3164 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3165 {
e3a64162 3166 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3167
aae43c5f 3168 if (INTEGRAL_TYPE_P (type)
8ce94e44 3169 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3170 warning_at (ploc, OPT_Wtraditional_conversion,
3171 "passing argument %d of %qE as integer rather "
3172 "than floating due to prototype",
3173 argnum, rname);
03829ad2 3174 if (INTEGRAL_TYPE_P (type)
8ce94e44 3175 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3176 warning_at (ploc, OPT_Wtraditional_conversion,
3177 "passing argument %d of %qE as integer rather "
3178 "than complex due to prototype",
3179 argnum, rname);
aae43c5f 3180 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3181 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3182 warning_at (ploc, OPT_Wtraditional_conversion,
3183 "passing argument %d of %qE as complex rather "
3184 "than floating due to prototype",
3185 argnum, rname);
400fbf9f 3186 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3187 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3188 warning_at (ploc, OPT_Wtraditional_conversion,
3189 "passing argument %d of %qE as floating rather "
3190 "than integer due to prototype",
3191 argnum, rname);
03829ad2 3192 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3193 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3194 warning_at (ploc, OPT_Wtraditional_conversion,
3195 "passing argument %d of %qE as complex rather "
3196 "than integer due to prototype",
3197 argnum, rname);
aae43c5f 3198 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3199 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3200 warning_at (ploc, OPT_Wtraditional_conversion,
3201 "passing argument %d of %qE as floating rather "
3202 "than complex due to prototype",
3203 argnum, rname);
aae43c5f
RK
3204 /* ??? At some point, messages should be written about
3205 conversions between complex types, but that's too messy
3206 to do now. */
d45cf215 3207 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3208 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3209 {
3210 /* Warn if any argument is passed as `float',
047de90b 3211 since without a prototype it would be `double'. */
9a8ce21f
JG
3212 if (formal_prec == TYPE_PRECISION (float_type_node)
3213 && type != dfloat32_type_node)
5c1bc275
MP
3214 warning_at (ploc, 0,
3215 "passing argument %d of %qE as %<float%> "
3216 "rather than %<double%> due to prototype",
3217 argnum, rname);
9a8ce21f
JG
3218
3219 /* Warn if mismatch between argument and prototype
3220 for decimal float types. Warn of conversions with
3221 binary float types and of precision narrowing due to
3222 prototype. */
8ce94e44 3223 else if (type != valtype
9a8ce21f
JG
3224 && (type == dfloat32_type_node
3225 || type == dfloat64_type_node
c22cacf3 3226 || type == dfloat128_type_node
8ce94e44
JM
3227 || valtype == dfloat32_type_node
3228 || valtype == dfloat64_type_node
3229 || valtype == dfloat128_type_node)
c22cacf3 3230 && (formal_prec
8ce94e44 3231 <= TYPE_PRECISION (valtype)
9a8ce21f 3232 || (type == dfloat128_type_node
8ce94e44 3233 && (valtype
c22cacf3 3234 != dfloat64_type_node
8ce94e44 3235 && (valtype
9a8ce21f
JG
3236 != dfloat32_type_node)))
3237 || (type == dfloat64_type_node
8ce94e44 3238 && (valtype
9a8ce21f 3239 != dfloat32_type_node))))
5c1bc275
MP
3240 warning_at (ploc, 0,
3241 "passing argument %d of %qE as %qT "
3242 "rather than %qT due to prototype",
3243 argnum, rname, type, valtype);
9a8ce21f 3244
d45cf215 3245 }
3ed56f8a
KG
3246 /* Detect integer changing in width or signedness.
3247 These warnings are only activated with
05170031
MLI
3248 -Wtraditional-conversion, not with -Wtraditional. */
3249 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3250 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3251 {
d45cf215
RS
3252 tree would_have_been = default_conversion (val);
3253 tree type1 = TREE_TYPE (would_have_been);
3254
754a4d82 3255 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3256 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3257 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3258 /* No warning if function asks for enum
3259 and the actual arg is that enum type. */
3260 ;
3261 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3262 warning_at (ploc, OPT_Wtraditional_conversion,
3263 "passing argument %d of %qE "
3264 "with different width due to prototype",
3265 argnum, rname);
8df83eae 3266 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3267 ;
800cd3b9
RS
3268 /* Don't complain if the formal parameter type
3269 is an enum, because we can't tell now whether
3270 the value was an enum--even the same enum. */
3271 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3272 ;
400fbf9f
JW
3273 else if (TREE_CODE (val) == INTEGER_CST
3274 && int_fits_type_p (val, type))
3275 /* Change in signedness doesn't matter
3276 if a constant value is unaffected. */
3277 ;
ce9895ae
RS
3278 /* If the value is extended from a narrower
3279 unsigned type, it doesn't matter whether we
3280 pass it as signed or unsigned; the value
3281 certainly is the same either way. */
8ce94e44
JM
3282 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3283 && TYPE_UNSIGNED (valtype))
ce9895ae 3284 ;
8df83eae 3285 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3286 warning_at (ploc, OPT_Wtraditional_conversion,
3287 "passing argument %d of %qE "
3288 "as unsigned due to prototype",
3289 argnum, rname);
3ed56f8a 3290 else
5c1bc275
MP
3291 warning_at (ploc, OPT_Wtraditional_conversion,
3292 "passing argument %d of %qE "
3293 "as signed due to prototype",
3294 argnum, rname);
400fbf9f
JW
3295 }
3296 }
3297
8ce94e44
JM
3298 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3299 sake of better warnings from convert_and_check. */
3300 if (excess_precision)
3301 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3302 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3303 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3304 val, origtype, ic_argpass,
3305 npc, fundecl, function,
2ac2f164 3306 parmnum + 1);
2f6e4e97 3307
61f71b34 3308 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3309 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3310 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3311 parmval = default_conversion (parmval);
400fbf9f 3312 }
400fbf9f 3313 }
8ce94e44
JM
3314 else if (TREE_CODE (valtype) == REAL_TYPE
3315 && (TYPE_PRECISION (valtype)
2531a1d9 3316 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3317 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3318 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3319 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3320 {
3321 if (type_generic)
bbbbb16a 3322 parmval = val;
b5d32c25 3323 else
0a0b3574
MM
3324 {
3325 /* Convert `float' to `double'. */
3326 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3327 warning_at (ploc, OPT_Wdouble_promotion,
3328 "implicit conversion from %qT to %qT when passing "
3329 "argument to function",
3330 valtype, double_type_node);
0a0b3574
MM
3331 parmval = convert (double_type_node, val);
3332 }
b5d32c25 3333 }
8ce94e44
JM
3334 else if (excess_precision && !type_generic)
3335 /* A "double" argument with excess precision being passed
3336 without a prototype or in variable arguments. */
bbbbb16a 3337 parmval = convert (valtype, val);
c22cacf3
MS
3338 else if ((invalid_func_diag =
3339 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3340 {
3341 error (invalid_func_diag);
94a0dd7b 3342 return -1;
4d3e6fae 3343 }
400fbf9f
JW
3344 else
3345 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3346 parmval = default_conversion (val);
3347
9771b263 3348 (*values)[parmnum] = parmval;
06302a02
JJ
3349 if (parmval == error_mark_node)
3350 error_args = true;
400fbf9f
JW
3351
3352 if (typetail)
3353 typetail = TREE_CHAIN (typetail);
3354 }
3355
9771b263 3356 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3357
400fbf9f 3358 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3359 {
68fca595 3360 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3361 inform_declaration (fundecl);
3362 return -1;
3789b316 3363 }
400fbf9f 3364
06302a02 3365 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3366}
3367\f
43f6dfd3
RS
3368/* This is the entry point used by the parser to build unary operators
3369 in the input. CODE, a tree_code, specifies the unary operator, and
3370 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3371 CONVERT_EXPR for code.
3372
3373 LOC is the location to use for the tree generated.
3374*/
43f6dfd3
RS
3375
3376struct c_expr
c2255bc4 3377parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3378{
3379 struct c_expr result;
3380
c9f9eb5d 3381 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3382 result.original_code = code;
6866c6e8
ILT
3383 result.original_type = NULL;
3384
59c0753d 3385 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3386 overflow_warning (loc, result.value);
59c0753d 3387
43f6dfd3
RS
3388 return result;
3389}
3390
3391/* This is the entry point used by the parser to build binary operators
3392 in the input. CODE, a tree_code, specifies the binary operator, and
3393 ARG1 and ARG2 are the operands. In addition to constructing the
3394 expression, we check for operands that were written with other binary
ba47d38d
AH
3395 operators in a way that is likely to confuse the user.
3396
3397 LOCATION is the location of the binary operator. */
edc7c4ec 3398
487a92fe 3399struct c_expr
ba47d38d
AH
3400parser_build_binary_op (location_t location, enum tree_code code,
3401 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3402{
487a92fe 3403 struct c_expr result;
400fbf9f 3404
487a92fe
JM
3405 enum tree_code code1 = arg1.original_code;
3406 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3407 tree type1 = (arg1.original_type
3408 ? arg1.original_type
3409 : TREE_TYPE (arg1.value));
3410 tree type2 = (arg2.original_type
3411 ? arg2.original_type
3412 : TREE_TYPE (arg2.value));
400fbf9f 3413
ba47d38d
AH
3414 result.value = build_binary_op (location, code,
3415 arg1.value, arg2.value, 1);
487a92fe 3416 result.original_code = code;
6866c6e8 3417 result.original_type = NULL;
58bf601b 3418
487a92fe
JM
3419 if (TREE_CODE (result.value) == ERROR_MARK)
3420 return result;
400fbf9f 3421
ba47d38d
AH
3422 if (location != UNKNOWN_LOCATION)
3423 protected_set_expr_location (result.value, location);
3424
400fbf9f 3425 /* Check for cases such as x+y<<z which users are likely
487a92fe 3426 to misinterpret. */
400fbf9f 3427 if (warn_parentheses)
393e8e8b
MP
3428 warn_about_parentheses (location, code, code1, arg1.value, code2,
3429 arg2.value);
001af587 3430
ca409efd 3431 if (warn_logical_op)
393e8e8b 3432 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3433 code1, arg1.value, code2, arg2.value);
63a08740 3434
742938c9 3435 if (warn_logical_not_paren
7ccb1a11 3436 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3437 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3438 && code2 != TRUTH_NOT_EXPR
3439 /* Avoid warning for !!x == y. */
3440 && (TREE_CODE (arg1.value) != NE_EXPR
3441 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3442 {
3443 /* Avoid warning for !b == y where b has _Bool type. */
3444 tree t = integer_zero_node;
3445 if (TREE_CODE (arg1.value) == EQ_EXPR
3446 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3447 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3448 {
3449 t = TREE_OPERAND (arg1.value, 0);
3450 do
3451 {
3452 if (TREE_TYPE (t) != integer_type_node)
3453 break;
3454 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3455 t = C_MAYBE_CONST_EXPR_EXPR (t);
3456 else if (CONVERT_EXPR_P (t))
3457 t = TREE_OPERAND (t, 0);
3458 else
3459 break;
3460 }
3461 while (1);
3462 }
3463 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
3464 warn_logical_not_parentheses (location, code, arg2.value);
3465 }
742938c9 3466
e994a705
RS
3467 /* Warn about comparisons against string literals, with the exception
3468 of testing for equality or inequality of a string literal with NULL. */
3469 if (code == EQ_EXPR || code == NE_EXPR)
3470 {
3471 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3472 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3473 warning_at (location, OPT_Waddress,
3474 "comparison with string literal results in unspecified behavior");
e994a705
RS
3475 }
3476 else if (TREE_CODE_CLASS (code) == tcc_comparison
3477 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3478 warning_at (location, OPT_Waddress,
3479 "comparison with string literal results in unspecified behavior");
e994a705 3480
b8698a0f
L
3481 if (TREE_OVERFLOW_P (result.value)
3482 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3483 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3484 overflow_warning (location, result.value);
400fbf9f 3485
6866c6e8
ILT
3486 /* Warn about comparisons of different enum types. */
3487 if (warn_enum_compare
3488 && TREE_CODE_CLASS (code) == tcc_comparison
3489 && TREE_CODE (type1) == ENUMERAL_TYPE
3490 && TREE_CODE (type2) == ENUMERAL_TYPE
3491 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3492 warning_at (location, OPT_Wenum_compare,
3493 "comparison between %qT and %qT",
3494 type1, type2);
3495
400fbf9f
JW
3496 return result;
3497}
3e4093b6 3498\f
3e4093b6
RS
3499/* Return a tree for the difference of pointers OP0 and OP1.
3500 The resulting tree has type int. */
293c9fdd 3501
3e4093b6 3502static tree
db3927fb 3503pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3504{
3e4093b6 3505 tree restype = ptrdiff_type_node;
36c5e70a 3506 tree result, inttype;
400fbf9f 3507
36c5e70a
BE
3508 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3509 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3510 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3511 tree orig_op1 = op1;
400fbf9f 3512
36c5e70a
BE
3513 /* If the operands point into different address spaces, we need to
3514 explicitly convert them to pointers into the common address space
3515 before we can subtract the numerical address values. */
3516 if (as0 != as1)
3517 {
3518 addr_space_t as_common;
3519 tree common_type;
3520
3521 /* Determine the common superset address space. This is guaranteed
3522 to exist because the caller verified that comp_target_types
3523 returned non-zero. */
3524 if (!addr_space_superset (as0, as1, &as_common))
3525 gcc_unreachable ();
3526
3527 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3528 op0 = convert (common_type, op0);
3529 op1 = convert (common_type, op1);
3530 }
3531
3532 /* Determine integer type to perform computations in. This will usually
3533 be the same as the result type (ptrdiff_t), but may need to be a wider
3534 type if pointers for the address space are wider than ptrdiff_t. */
3535 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3536 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3537 else
3538 inttype = restype;
3539
fcf73884 3540 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3541 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3542 "pointer of type %<void *%> used in subtraction");
3543 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3544 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3545 "pointer to a function used in subtraction");
400fbf9f 3546
3e4093b6
RS
3547 /* First do the subtraction as integers;
3548 then drop through to build the divide operator.
3549 Do not do default conversions on the minus operator
3550 in case restype is a short type. */
400fbf9f 3551
db3927fb 3552 op0 = build_binary_op (loc,
36c5e70a
BE
3553 MINUS_EXPR, convert (inttype, op0),
3554 convert (inttype, op1), 0);
3e4093b6
RS
3555 /* This generates an error if op1 is pointer to incomplete type. */
3556 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3557 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3558
3e4093b6 3559 op1 = c_size_in_bytes (target_type);
400fbf9f 3560
f04dda30
MP
3561 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3562 error_at (loc, "arithmetic on pointer to an empty aggregate");
3563
3e4093b6 3564 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3565 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3566 op0, convert (inttype, op1));
3567
3568 /* Convert to final result type if necessary. */
3569 return convert (restype, result);
3e4093b6
RS
3570}
3571\f
267bac10
JM
3572/* Expand atomic compound assignments into an approriate sequence as
3573 specified by the C11 standard section 6.5.16.2.
3574 given
3575 _Atomic T1 E1
3576 T2 E2
3577 E1 op= E2
3578
3579 This sequence is used for all types for which these operations are
3580 supported.
3581
3582 In addition, built-in versions of the 'fe' prefixed routines may
3583 need to be invoked for floating point (real, complex or vector) when
3584 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3585
3586 T1 newval;
3587 T1 old;
3588 T1 *addr
3589 T2 val
3590 fenv_t fenv
3591
3592 addr = &E1;
3593 val = (E2);
3594 __atomic_load (addr, &old, SEQ_CST);
3595 feholdexcept (&fenv);
3596loop:
3597 newval = old op val;
3598 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3599 SEQ_CST))
3600 goto done;
3601 feclearexcept (FE_ALL_EXCEPT);
3602 goto loop:
3603done:
3604 feupdateenv (&fenv);
3605
3606 Also note that the compiler is simply issuing the generic form of
3607 the atomic operations. This requires temp(s) and has their address
3608 taken. The atomic processing is smart enough to figure out when the
3609 size of an object can utilize a lock-free version, and convert the
3610 built-in call to the appropriate lock-free routine. The optimizers
3611 will then dispose of any temps that are no longer required, and
3612 lock-free implementations are utilized as long as there is target
3613 support for the required size.
3614
3615 If the operator is NOP_EXPR, then this is a simple assignment, and
3616 an __atomic_store is issued to perform the assignment rather than
3617 the above loop.
3618
3619*/
3620
3621/* Build an atomic assignment at LOC, expanding into the proper
3622 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3623 the result of the operation, unless RETURN_OLD_P in which case
3624 return the old value of LHS (this is only for postincrement and
3625 postdecrement). */
3626static tree
3627build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3628 tree rhs, bool return_old_p)
3629{
3630 tree fndecl, func_call;
3631 vec<tree, va_gc> *params;
3632 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3633 tree old, old_addr;
3634 tree compound_stmt;
3635 tree stmt, goto_stmt;
3636 tree loop_label, loop_decl, done_label, done_decl;
3637
3638 tree lhs_type = TREE_TYPE (lhs);
3639 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3640 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3641 tree rhs_type = TREE_TYPE (rhs);
3642
3643 gcc_assert (TYPE_ATOMIC (lhs_type));
3644
3645 if (return_old_p)
3646 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3647
3648 /* Allocate enough vector items for a compare_exchange. */
3649 vec_alloc (params, 6);
3650
3651 /* Create a compound statement to hold the sequence of statements
3652 with a loop. */
3653 compound_stmt = c_begin_compound_stmt (false);
3654
3655 /* Fold the RHS if it hasn't already been folded. */
3656 if (modifycode != NOP_EXPR)
3657 rhs = c_fully_fold (rhs, false, NULL);
3658
3659 /* Remove the qualifiers for the rest of the expressions and create
3660 the VAL temp variable to hold the RHS. */
3661 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3662 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
5c4abbb8 3663 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3664 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3665 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3666 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3667 NULL_TREE);
267bac10
JM
3668 SET_EXPR_LOCATION (rhs, loc);
3669 add_stmt (rhs);
3670
3671 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3672 an atomic_store. */
3673 if (modifycode == NOP_EXPR)
3674 {
3675 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3676 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3677 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3678 params->quick_push (lhs_addr);
3679 params->quick_push (rhs);
3680 params->quick_push (seq_cst);
8edbfaa6 3681 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3682 add_stmt (func_call);
3683
3684 /* Finish the compound statement. */
3685 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3686
3687 /* VAL is the value which was stored, return a COMPOUND_STMT of
3688 the statement and that value. */
3689 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3690 }
3691
3692 /* Create the variables and labels required for the op= form. */
5c4abbb8 3693 old = create_tmp_var_raw (nonatomic_lhs_type);
267bac10 3694 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3695 TREE_ADDRESSABLE (old) = 1;
3696 TREE_NO_WARNING (old) = 1;
267bac10 3697
5c4abbb8 3698 newval = create_tmp_var_raw (nonatomic_lhs_type);
267bac10
JM
3699 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3700 TREE_ADDRESSABLE (newval) = 1;
3701
3702 loop_decl = create_artificial_label (loc);
3703 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3704
3705 done_decl = create_artificial_label (loc);
3706 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3707
3708 /* __atomic_load (addr, &old, SEQ_CST). */
3709 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3710 params->quick_push (lhs_addr);
3711 params->quick_push (old_addr);
3712 params->quick_push (seq_cst);
8edbfaa6 3713 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
3714 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
3715 NULL_TREE);
3716 add_stmt (old);
267bac10
JM
3717 params->truncate (0);
3718
3719 /* Create the expressions for floating-point environment
3720 manipulation, if required. */
3721 bool need_fenv = (flag_trapping_math
3722 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3723 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3724 if (need_fenv)
3725 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3726
3727 if (hold_call)
3728 add_stmt (hold_call);
3729
3730 /* loop: */
3731 add_stmt (loop_label);
3732
3733 /* newval = old + val; */
3734 rhs = build_binary_op (loc, modifycode, old, val, 1);
5c4abbb8 3735 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
3736 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3737 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3738 NULL_TREE, 0);
3739 if (rhs != error_mark_node)
3740 {
5c4abbb8
MP
3741 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
3742 NULL_TREE);
267bac10
JM
3743 SET_EXPR_LOCATION (rhs, loc);
3744 add_stmt (rhs);
3745 }
3746
3747 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3748 goto done; */
3749 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3750 params->quick_push (lhs_addr);
3751 params->quick_push (old_addr);
3752 params->quick_push (newval_addr);
3753 params->quick_push (integer_zero_node);
3754 params->quick_push (seq_cst);
3755 params->quick_push (seq_cst);
8edbfaa6 3756 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3757
3758 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3759 SET_EXPR_LOCATION (goto_stmt, loc);
3760
3761 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3762 SET_EXPR_LOCATION (stmt, loc);
3763 add_stmt (stmt);
5c4abbb8 3764
267bac10
JM
3765 if (clear_call)
3766 add_stmt (clear_call);
3767
3768 /* goto loop; */
3769 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3770 SET_EXPR_LOCATION (goto_stmt, loc);
3771 add_stmt (goto_stmt);
5c4abbb8 3772
267bac10
JM
3773 /* done: */
3774 add_stmt (done_label);
3775
3776 if (update_call)
3777 add_stmt (update_call);
3778
3779 /* Finish the compound statement. */
3780 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3781
3782 /* NEWVAL is the value that was successfully stored, return a
3783 COMPOUND_EXPR of the statement and the appropriate value. */
3784 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3785 return_old_p ? old : newval);
3786}
3787
3e4093b6
RS
3788/* Construct and perhaps optimize a tree representation
3789 for a unary operation. CODE, a tree_code, specifies the operation
3790 and XARG is the operand.
3791 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3792 the default promotions (such as from short to int).
3793 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3794 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3795 arrays to pointers in C99.
3796
3797 LOCATION is the location of the operator. */
400fbf9f 3798
3e4093b6 3799tree
c9f9eb5d
AH
3800build_unary_op (location_t location,
3801 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3802{
3803 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3804 tree arg = xarg;
3805 tree argtype = 0;
808d6eaa 3806 enum tree_code typecode;
3e4093b6 3807 tree val;
c9f9eb5d 3808 tree ret = error_mark_node;
8ce94e44 3809 tree eptype = NULL_TREE;
3e4093b6 3810 int noconvert = flag;
4de67c26 3811 const char *invalid_op_diag;
928c19bb
JM
3812 bool int_operands;
3813
3814 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3815 if (int_operands)
3816 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3817
808d6eaa
JM
3818 if (code != ADDR_EXPR)
3819 arg = require_complete_type (arg);
3820
3821 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3822 if (typecode == ERROR_MARK)
3823 return error_mark_node;
3824 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3825 typecode = INTEGER_TYPE;
6c36d76b 3826
4de67c26
JM
3827 if ((invalid_op_diag
3828 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3829 {
c9f9eb5d 3830 error_at (location, invalid_op_diag);
4de67c26
JM
3831 return error_mark_node;
3832 }
3833
8ce94e44
JM
3834 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3835 {
3836 eptype = TREE_TYPE (arg);
3837 arg = TREE_OPERAND (arg, 0);
3838 }
3839
3e4093b6
RS
3840 switch (code)
3841 {
3842 case CONVERT_EXPR:
3843 /* This is used for unary plus, because a CONVERT_EXPR
3844 is enough to prevent anybody from looking inside for
3845 associativity, but won't generate any code. */
3846 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3847 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3848 || typecode == VECTOR_TYPE))
400fbf9f 3849 {
c9f9eb5d 3850 error_at (location, "wrong type argument to unary plus");
3e4093b6 3851 return error_mark_node;
400fbf9f 3852 }
3e4093b6
RS
3853 else if (!noconvert)
3854 arg = default_conversion (arg);
db3927fb 3855 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3856 break;
3857
3e4093b6
RS
3858 case NEGATE_EXPR:
3859 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3860 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3861 || typecode == VECTOR_TYPE))
3862 {
c9f9eb5d 3863 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3864 return error_mark_node;
3865 }
3866 else if (!noconvert)
3867 arg = default_conversion (arg);
400fbf9f
JW
3868 break;
3869
3e4093b6 3870 case BIT_NOT_EXPR:
462643f0
AP
3871 /* ~ works on integer types and non float vectors. */
3872 if (typecode == INTEGER_TYPE
3873 || (typecode == VECTOR_TYPE
3874 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3875 {
3e4093b6
RS
3876 if (!noconvert)
3877 arg = default_conversion (arg);
03d5b1f5 3878 }
3e4093b6 3879 else if (typecode == COMPLEX_TYPE)
400fbf9f 3880 {
3e4093b6 3881 code = CONJ_EXPR;
c1771a20 3882 pedwarn (location, OPT_Wpedantic,
fcf73884 3883 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3884 if (!noconvert)
3885 arg = default_conversion (arg);
3886 }
3887 else
3888 {
c9f9eb5d 3889 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3890 return error_mark_node;
400fbf9f
JW
3891 }
3892 break;
3893
3e4093b6 3894 case ABS_EXPR:
11017cc7 3895 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3896 {
c9f9eb5d 3897 error_at (location, "wrong type argument to abs");
3e4093b6 3898 return error_mark_node;
400fbf9f 3899 }
3e4093b6
RS
3900 else if (!noconvert)
3901 arg = default_conversion (arg);
400fbf9f
JW
3902 break;
3903
3e4093b6
RS
3904 case CONJ_EXPR:
3905 /* Conjugating a real value is a no-op, but allow it anyway. */
3906 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3907 || typecode == COMPLEX_TYPE))
400fbf9f 3908 {
c9f9eb5d 3909 error_at (location, "wrong type argument to conjugation");
3e4093b6 3910 return error_mark_node;
400fbf9f 3911 }
3e4093b6
RS
3912 else if (!noconvert)
3913 arg = default_conversion (arg);
400fbf9f
JW
3914 break;
3915
3e4093b6 3916 case TRUTH_NOT_EXPR:
ab22c1fa 3917 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3918 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3919 && typecode != COMPLEX_TYPE)
400fbf9f 3920 {
c9f9eb5d
AH
3921 error_at (location,
3922 "wrong type argument to unary exclamation mark");
3e4093b6 3923 return error_mark_node;
400fbf9f 3924 }
a27d595d
JM
3925 if (int_operands)
3926 {
3927 arg = c_objc_common_truthvalue_conversion (location, xarg);
3928 arg = remove_c_maybe_const_expr (arg);
3929 }
3930 else
3931 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3932 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3933 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3934 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3935 location = EXPR_LOCATION (ret);
c9f9eb5d 3936 goto return_build_unary_op;
3e4093b6 3937
3e4093b6 3938 case REALPART_EXPR:
3e4093b6 3939 case IMAGPART_EXPR:
fb52b50a
NF
3940 ret = build_real_imag_expr (location, code, arg);
3941 if (ret == error_mark_node)
3942 return error_mark_node;
8ce94e44
JM
3943 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3944 eptype = TREE_TYPE (eptype);
c9f9eb5d 3945 goto return_build_unary_op;
3e4093b6
RS
3946
3947 case PREINCREMENT_EXPR:
3948 case POSTINCREMENT_EXPR:
3949 case PREDECREMENT_EXPR:
3950 case POSTDECREMENT_EXPR:
3e4093b6 3951
928c19bb
JM
3952 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3953 {
3954 tree inner = build_unary_op (location, code,
3955 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3956 if (inner == error_mark_node)
3957 return error_mark_node;
3958 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3959 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3960 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3961 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3962 goto return_build_unary_op;
3963 }
3964
925e8657
NP
3965 /* Complain about anything that is not a true lvalue. In
3966 Objective-C, skip this check for property_refs. */
f90e8e2e 3967 if (!objc_is_property_ref (arg)
7bd11157
TT
3968 && !lvalue_or_else (location,
3969 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3970 || code == POSTINCREMENT_EXPR)
3971 ? lv_increment
3972 : lv_decrement)))
928c19bb
JM
3973 return error_mark_node;
3974
09639a83
ILT
3975 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3976 {
3977 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3978 warning_at (location, OPT_Wc___compat,
3979 "increment of enumeration value is invalid in C++");
3980 else
3981 warning_at (location, OPT_Wc___compat,
3982 "decrement of enumeration value is invalid in C++");
3983 }
3984
928c19bb
JM
3985 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3986 arg = c_fully_fold (arg, false, NULL);
3987
267bac10
JM
3988 bool atomic_op;
3989 atomic_op = really_atomic_lvalue (arg);
3990
3e4093b6
RS
3991 /* Increment or decrement the real part of the value,
3992 and don't change the imaginary part. */
3993 if (typecode == COMPLEX_TYPE)
400fbf9f 3994 {
3e4093b6
RS
3995 tree real, imag;
3996
c1771a20 3997 pedwarn (location, OPT_Wpedantic,
509c9d60 3998 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3999
267bac10
JM
4000 if (!atomic_op)
4001 {
4002 arg = stabilize_reference (arg);
4003 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
4004 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
4005 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
4006 if (real == error_mark_node || imag == error_mark_node)
4007 return error_mark_node;
4008 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4009 real, imag);
4010 goto return_build_unary_op;
4011 }
400fbf9f 4012 }
3e4093b6
RS
4013
4014 /* Report invalid types. */
4015
ab22c1fa 4016 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4017 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4018 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4019 {
3e4093b6 4020 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4021 error_at (location, "wrong type argument to increment");
c22cacf3 4022 else
c9f9eb5d 4023 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4024
4025 return error_mark_node;
400fbf9f 4026 }
400fbf9f 4027
3e4093b6
RS
4028 {
4029 tree inc;
400fbf9f 4030
3e4093b6
RS
4031 argtype = TREE_TYPE (arg);
4032
4033 /* Compute the increment. */
4034
4035 if (typecode == POINTER_TYPE)
4036 {
a0e24419 4037 /* If pointer target is an incomplete type,
3e4093b6 4038 we just cannot know how to do the arithmetic. */
b70cef5d 4039 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4040 {
4041 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4042 error_at (location,
a0e24419
MP
4043 "increment of pointer to an incomplete type %qT",
4044 TREE_TYPE (argtype));
3e4093b6 4045 else
c9f9eb5d 4046 error_at (location,
a0e24419
MP
4047 "decrement of pointer to an incomplete type %qT",
4048 TREE_TYPE (argtype));
3e4093b6 4049 }
b70cef5d
JJ
4050 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4051 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4052 {
3e4093b6 4053 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4054 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4055 "wrong type argument to increment");
3e4093b6 4056 else
44d90fe1 4057 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4058 "wrong type argument to decrement");
3e4093b6
RS
4059 }
4060
b70cef5d 4061 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4062 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4063 }
b70cef5d 4064 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4065 {
4066 /* For signed fract types, we invert ++ to -- or
4067 -- to ++, and change inc from 1 to -1, because
4068 it is not possible to represent 1 in signed fract constants.
4069 For unsigned fract types, the result always overflows and
4070 we get an undefined (original) or the maximum value. */
4071 if (code == PREINCREMENT_EXPR)
4072 code = PREDECREMENT_EXPR;
4073 else if (code == PREDECREMENT_EXPR)
4074 code = PREINCREMENT_EXPR;
4075 else if (code == POSTINCREMENT_EXPR)
4076 code = POSTDECREMENT_EXPR;
4077 else /* code == POSTDECREMENT_EXPR */
4078 code = POSTINCREMENT_EXPR;
4079
4080 inc = integer_minus_one_node;
4081 inc = convert (argtype, inc);
4082 }
3e4093b6 4083 else
5be014d5 4084 {
241b71bb
TV
4085 inc = VECTOR_TYPE_P (argtype)
4086 ? build_one_cst (argtype)
4087 : integer_one_node;
5be014d5
AP
4088 inc = convert (argtype, inc);
4089 }
3e4093b6 4090
925e8657
NP
4091 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4092 need to ask Objective-C to build the increment or decrement
4093 expression for it. */
4094 if (objc_is_property_ref (arg))
f90e8e2e 4095 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4096 arg, inc);
4097
3e4093b6 4098 /* Report a read-only lvalue. */
f37acdf9 4099 if (TYPE_READONLY (argtype))
953ff289 4100 {
c02065fc 4101 readonly_error (location, arg,
953ff289
DN
4102 ((code == PREINCREMENT_EXPR
4103 || code == POSTINCREMENT_EXPR)
4104 ? lv_increment : lv_decrement));
4105 return error_mark_node;
4106 }
f37acdf9
JM
4107 else if (TREE_READONLY (arg))
4108 readonly_warning (arg,
4109 ((code == PREINCREMENT_EXPR
4110 || code == POSTINCREMENT_EXPR)
4111 ? lv_increment : lv_decrement));
3e4093b6 4112
267bac10
JM
4113 /* If the argument is atomic, use the special code sequences for
4114 atomic compound assignment. */
4115 if (atomic_op)
4116 {
4117 arg = stabilize_reference (arg);
4118 ret = build_atomic_assign (location, arg,
4119 ((code == PREINCREMENT_EXPR
4120 || code == POSTINCREMENT_EXPR)
4121 ? PLUS_EXPR
4122 : MINUS_EXPR),
4123 (FRACT_MODE_P (TYPE_MODE (argtype))
4124 ? inc
4125 : integer_one_node),
4126 (code == POSTINCREMENT_EXPR
4127 || code == POSTDECREMENT_EXPR));
4128 goto return_build_unary_op;
4129 }
4130
3e4093b6
RS
4131 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4132 val = boolean_increment (code, arg);
4133 else
53fb4de3 4134 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4135 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4136 if (TREE_CODE (val) != code)
6de9cd9a 4137 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4138 ret = val;
4139 goto return_build_unary_op;
3e4093b6
RS
4140 }
4141
4142 case ADDR_EXPR:
4143 /* Note that this operation never does default_conversion. */
4144
2b4b7036
JM
4145 /* The operand of unary '&' must be an lvalue (which excludes
4146 expressions of type void), or, in C99, the result of a [] or
4147 unary '*' operator. */
4148 if (VOID_TYPE_P (TREE_TYPE (arg))
4149 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4150 && (TREE_CODE (arg) != INDIRECT_REF
4151 || !flag_isoc99))
4152 pedwarn (location, 0, "taking address of expression of type %<void%>");
4153
3e4093b6
RS
4154 /* Let &* cancel out to simplify resulting code. */
4155 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4156 {
3e4093b6
RS
4157 /* Don't let this be an lvalue. */
4158 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4159 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4160 ret = TREE_OPERAND (arg, 0);
4161 goto return_build_unary_op;
400fbf9f 4162 }
1eb8759b 4163
7c672dfc 4164 /* For &x[y], return x+y */
3e4093b6 4165 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4166 {
f2a71bbc
JM
4167 tree op0 = TREE_OPERAND (arg, 0);
4168 if (!c_mark_addressable (op0))
3e4093b6 4169 return error_mark_node;
1eb8759b 4170 }
1eb8759b 4171
3e4093b6
RS
4172 /* Anything not already handled and not a true memory reference
4173 or a non-lvalue array is an error. */
4174 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4175 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4176 return error_mark_node;
b6a10c9f 4177
928c19bb
JM
4178 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4179 folding later. */
4180 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4181 {
4182 tree inner = build_unary_op (location, code,
4183 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4184 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4185 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4186 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4187 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4188 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4189 goto return_build_unary_op;
4190 }
4191
3e4093b6
RS
4192 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4193 argtype = TREE_TYPE (arg);
400fbf9f 4194
3e4093b6 4195 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4196 to which the address will point. This is only needed
f2c1da78 4197 for function types. */
6615c446 4198 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4199 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4200 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4201 {
4202 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4203 int quals = orig_quals;
4204
4205 if (TREE_READONLY (arg))
4206 quals |= TYPE_QUAL_CONST;
4207 if (TREE_THIS_VOLATILE (arg))
4208 quals |= TYPE_QUAL_VOLATILE;
4209
f2c1da78
JM
4210 argtype = c_build_qualified_type (argtype, quals);
4211 }
400fbf9f 4212
3e4093b6
RS
4213 if (!c_mark_addressable (arg))
4214 return error_mark_node;
400fbf9f 4215
abb54d14
JM
4216 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4217 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4218
5cc200fc 4219 argtype = build_pointer_type (argtype);
5e55f99d
RH
4220
4221 /* ??? Cope with user tricks that amount to offsetof. Delete this
4222 when we have proper support for integer constant expressions. */
4223 val = get_base_address (arg);
4224 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4225 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4226 {
cf9e9959 4227 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4228 goto return_build_unary_op;
3aa2ddb8 4229 }
5e55f99d 4230
5cc200fc 4231 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4232
c9f9eb5d
AH
4233 ret = val;
4234 goto return_build_unary_op;
400fbf9f 4235
3e4093b6 4236 default:
1344f9a3 4237 gcc_unreachable ();
3e4093b6 4238 }
400fbf9f 4239
3e4093b6
RS
4240 if (argtype == 0)
4241 argtype = TREE_TYPE (arg);
928c19bb
JM
4242 if (TREE_CODE (arg) == INTEGER_CST)
4243 ret = (require_constant_value
db3927fb
AH
4244 ? fold_build1_initializer_loc (location, code, argtype, arg)
4245 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4246 else
4247 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4248 return_build_unary_op:
4249 gcc_assert (ret != error_mark_node);
928c19bb
JM
4250 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4251 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4252 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4253 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4254 ret = note_integer_operands (ret);
8ce94e44
JM
4255 if (eptype)
4256 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4257 protected_set_expr_location (ret, location);
4258 return ret;
3e4093b6 4259}
400fbf9f 4260
3e4093b6
RS
4261/* Return nonzero if REF is an lvalue valid for this language.
4262 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4263 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4264
1e4ae551 4265bool
58f9752a 4266lvalue_p (const_tree ref)
3e4093b6 4267{
58f9752a 4268 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4269
3e4093b6
RS
4270 switch (code)
4271 {
4272 case REALPART_EXPR:
4273 case IMAGPART_EXPR:
4274 case COMPONENT_REF:
4275 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4276
928c19bb
JM
4277 case C_MAYBE_CONST_EXPR:
4278 return lvalue_p (TREE_OPERAND (ref, 1));
4279
3e4093b6
RS
4280 case COMPOUND_LITERAL_EXPR:
4281 case STRING_CST:
4282 return 1;
400fbf9f 4283
3e4093b6
RS
4284 case INDIRECT_REF:
4285 case ARRAY_REF:
36536d79 4286 case ARRAY_NOTATION_REF:
3e4093b6
RS
4287 case VAR_DECL:
4288 case PARM_DECL:
4289 case RESULT_DECL:
4290 case ERROR_MARK:
4291 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4292 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4293
3e4093b6 4294 case BIND_EXPR:
3e4093b6 4295 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4296
3e4093b6
RS
4297 default:
4298 return 0;
4299 }
4300}
400fbf9f 4301\f
f37acdf9
JM
4302/* Give a warning for storing in something that is read-only in GCC
4303 terms but not const in ISO C terms. */
4304
4305static void
4306readonly_warning (tree arg, enum lvalue_use use)
4307{
4308 switch (use)
4309 {
4310 case lv_assign:
4311 warning (0, "assignment of read-only location %qE", arg);
4312 break;
4313 case lv_increment:
4314 warning (0, "increment of read-only location %qE", arg);
4315 break;
4316 case lv_decrement:
4317 warning (0, "decrement of read-only location %qE", arg);
4318 break;
4319 default:
4320 gcc_unreachable ();
4321 }
4322 return;
4323}
4324
37dc0d8d
JM
4325
4326/* Return nonzero if REF is an lvalue valid for this language;
4327 otherwise, print an error message and return zero. USE says
7bd11157
TT
4328 how the lvalue is being used and so selects the error message.
4329 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4330
4331static int
7bd11157 4332lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4333{
4334 int win = lvalue_p (ref);
4335
4336 if (!win)
7bd11157 4337 lvalue_error (loc, use);
37dc0d8d
JM
4338
4339 return win;
4340}
3e4093b6
RS
4341\f
4342/* Mark EXP saying that we need to be able to take the
4343 address of it; it should not be allocated in a register.
4344 Returns true if successful. */
54c93c30 4345
3e4093b6
RS
4346bool
4347c_mark_addressable (tree exp)
400fbf9f 4348{
3e4093b6 4349 tree x = exp;
95602da1 4350
3e4093b6
RS
4351 while (1)
4352 switch (TREE_CODE (x))
4353 {
4354 case COMPONENT_REF:
4355 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4356 {
0039fa55
AN
4357 error
4358 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4359 return false;
4360 }
95602da1 4361
3e4093b6 4362 /* ... fall through ... */
95602da1 4363
3e4093b6
RS
4364 case ADDR_EXPR:
4365 case ARRAY_REF:
4366 case REALPART_EXPR:
4367 case IMAGPART_EXPR:
4368 x = TREE_OPERAND (x, 0);
4369 break;
95602da1 4370
3e4093b6
RS
4371 case COMPOUND_LITERAL_EXPR:
4372 case CONSTRUCTOR:
4373 TREE_ADDRESSABLE (x) = 1;
4374 return true;
95602da1 4375
3e4093b6
RS
4376 case VAR_DECL:
4377 case CONST_DECL:
4378 case PARM_DECL:
4379 case RESULT_DECL:
5baeaac0 4380 if (C_DECL_REGISTER (x)
3e4093b6
RS
4381 && DECL_NONLOCAL (x))
4382 {
e697b20f 4383 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4384 {
0039fa55
AN
4385 error
4386 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4387 return false;
4388 }
509c9d60 4389 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4390 }
5baeaac0 4391 else if (C_DECL_REGISTER (x))
3e4093b6 4392 {
e697b20f 4393 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4394 error ("address of global register variable %qD requested", x);
4395 else
4396 error ("address of register variable %qD requested", x);
4397 return false;
3e4093b6 4398 }
400fbf9f 4399
3e4093b6
RS
4400 /* drops in */
4401 case FUNCTION_DECL:
4402 TREE_ADDRESSABLE (x) = 1;
4403 /* drops out */
4404 default:
4405 return true;
4406 }
4407}
4408\f
2d2e923f
MLI
4409/* Convert EXPR to TYPE, warning about conversion problems with
4410 constants. SEMANTIC_TYPE is the type this conversion would use
4411 without excess precision. If SEMANTIC_TYPE is NULL, this function
4412 is equivalent to convert_and_check. This function is a wrapper that
4413 handles conversions that may be different than
4414 the usual ones because of excess precision. */
4415
4416static tree
68fca595
MP
4417ep_convert_and_check (location_t loc, tree type, tree expr,
4418 tree semantic_type)
2d2e923f
MLI
4419{
4420 if (TREE_TYPE (expr) == type)
4421 return expr;
4422
4423 if (!semantic_type)
68fca595 4424 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4425
4426 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4427 && TREE_TYPE (expr) != semantic_type)
4428 {
4429 /* For integers, we need to check the real conversion, not
4430 the conversion to the excess precision type. */
68fca595 4431 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4432 }
4433 /* Result type is the excess precision type, which should be
4434 large enough, so do not check. */
4435 return convert (type, expr);
4436}
4437
928c19bb
JM
4438/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4439 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4440 if folded to an integer constant then the unselected half may
4441 contain arbitrary operations not normally permitted in constant
c2255bc4 4442 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4443
4444tree
744aa42f 4445build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4446 tree op1, tree op1_original_type, tree op2,
4447 tree op2_original_type)
400fbf9f 4448{
3e4093b6
RS
4449 tree type1;
4450 tree type2;
4451 enum tree_code code1;
4452 enum tree_code code2;
4453 tree result_type = NULL;
2d2e923f 4454 tree semantic_result_type = NULL;
3e4093b6 4455 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4456 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4457 bool ifexp_int_operands;
928c19bb 4458 tree ret;
400fbf9f 4459
4d84fe7c
JM
4460 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4461 if (op1_int_operands)
4462 op1 = remove_c_maybe_const_expr (op1);
4463 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4464 if (op2_int_operands)
4465 op2 = remove_c_maybe_const_expr (op2);
4466 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4467 if (ifexp_int_operands)
4468 ifexp = remove_c_maybe_const_expr (ifexp);
4469
3e4093b6
RS
4470 /* Promote both alternatives. */
4471
4472 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4473 op1 = default_conversion (op1);
4474 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4475 op2 = default_conversion (op2);
4476
4477 if (TREE_CODE (ifexp) == ERROR_MARK
4478 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4479 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4480 return error_mark_node;
400fbf9f 4481
3e4093b6
RS
4482 type1 = TREE_TYPE (op1);
4483 code1 = TREE_CODE (type1);
4484 type2 = TREE_TYPE (op2);
4485 code2 = TREE_CODE (type2);
4486
b1adf557
JM
4487 /* C90 does not permit non-lvalue arrays in conditional expressions.
4488 In C99 they will be pointers by now. */
4489 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4490 {
744aa42f 4491 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4492 return error_mark_node;
4493 }
4494
8ce94e44
JM
4495 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4496 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4497 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4498 || code1 == COMPLEX_TYPE)
4499 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4500 || code2 == COMPLEX_TYPE))
4501 {
2d2e923f 4502 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4503 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4504 {
4505 op1 = TREE_OPERAND (op1, 0);
4506 type1 = TREE_TYPE (op1);
4507 gcc_assert (TREE_CODE (type1) == code1);
4508 }
4509 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4510 {
4511 op2 = TREE_OPERAND (op2, 0);
4512 type2 = TREE_TYPE (op2);
4513 gcc_assert (TREE_CODE (type2) == code2);
4514 }
4515 }
4516
d130ae11
ILT
4517 if (warn_cxx_compat)
4518 {
4519 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4520 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4521
4522 if (TREE_CODE (t1) == ENUMERAL_TYPE
4523 && TREE_CODE (t2) == ENUMERAL_TYPE
4524 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4525 warning_at (colon_loc, OPT_Wc___compat,
4526 ("different enum types in conditional is "
4527 "invalid in C++: %qT vs %qT"),
4528 t1, t2);
4529 }
4530
3e4093b6
RS
4531 /* Quickly detect the usual case where op1 and op2 have the same type
4532 after promotion. */
4533 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4534 {
3e4093b6
RS
4535 if (type1 == type2)
4536 result_type = type1;
4537 else
4538 result_type = TYPE_MAIN_VARIANT (type1);
4539 }
4540 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4541 || code1 == COMPLEX_TYPE)
4542 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4543 || code2 == COMPLEX_TYPE))
3e4093b6 4544 {
ccf7f880 4545 result_type = c_common_type (type1, type2);
0a0b3574
MM
4546 do_warn_double_promotion (result_type, type1, type2,
4547 "implicit conversion from %qT to %qT to "
4548 "match other result of conditional",
4549 colon_loc);
400fbf9f 4550
3e4093b6
RS
4551 /* If -Wsign-compare, warn here if type1 and type2 have
4552 different signedness. We'll promote the signed to unsigned
4553 and later code won't know it used to be different.
4554 Do this check on the original types, so that explicit casts
4555 will be considered, but default promotions won't. */
7d882b83 4556 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4557 {
8df83eae
RK
4558 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4559 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4560
3e4093b6
RS
4561 if (unsigned_op1 ^ unsigned_op2)
4562 {
6ac01510
ILT
4563 bool ovf;
4564
3e4093b6
RS
4565 /* Do not warn if the result type is signed, since the
4566 signed type will only be chosen if it can represent
4567 all the values of the unsigned type. */
3f75a254 4568 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4569 /* OK */;
3e4093b6 4570 else
928c19bb
JM
4571 {
4572 bool op1_maybe_const = true;
4573 bool op2_maybe_const = true;
4574
4575 /* Do not warn if the signed quantity is an
4576 unsuffixed integer literal (or some static
4577 constant expression involving such literals) and
4578 it is non-negative. This warning requires the
4579 operands to be folded for best results, so do
4580 that folding in this case even without
4581 warn_sign_compare to avoid warning options
4582 possibly affecting code generation. */
f5178456
RS
4583 c_inhibit_evaluation_warnings
4584 += (ifexp == truthvalue_false_node);
928c19bb
JM
4585 op1 = c_fully_fold (op1, require_constant_value,
4586 &op1_maybe_const);
f5178456
RS
4587 c_inhibit_evaluation_warnings
4588 -= (ifexp == truthvalue_false_node);
4589
4590 c_inhibit_evaluation_warnings
4591 += (ifexp == truthvalue_true_node);
928c19bb
JM
4592 op2 = c_fully_fold (op2, require_constant_value,
4593 &op2_maybe_const);
f5178456
RS
4594 c_inhibit_evaluation_warnings
4595 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4596
4597 if (warn_sign_compare)
4598 {
4599 if ((unsigned_op2
4600 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4601 || (unsigned_op1
4602 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4603 /* OK */;
4604 else
744aa42f
ILT
4605 warning_at (colon_loc, OPT_Wsign_compare,
4606 ("signed and unsigned type in "
4607 "conditional expression"));
928c19bb
JM
4608 }
4609 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4610 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4611 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4612 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4613 }
3e4093b6
RS
4614 }
4615 }
4616 }
4617 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4618 {
fcf73884 4619 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4620 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4621 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4622 result_type = void_type_node;
4623 }
4624 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4625 {
36c5e70a
BE
4626 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4627 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4628 addr_space_t as_common;
4629
744aa42f 4630 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4631 result_type = common_pointer_type (type1, type2);
6aa3c60d 4632 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4633 result_type = type2;
6aa3c60d 4634 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4635 result_type = type1;
4636 else if (!addr_space_superset (as1, as2, &as_common))
4637 {
4638 error_at (colon_loc, "pointers to disjoint address spaces "
4639 "used in conditional expression");
4640 return error_mark_node;
4641 }
267bac10
JM
4642 else if (VOID_TYPE_P (TREE_TYPE (type1))
4643 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4644 {
768952be
MU
4645 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
4646 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
4647 & ~TYPE_QUALS (TREE_TYPE (type1))))
4648 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4649 "pointer to array loses qualifier "
4650 "in conditional expression");
4651
fcf73884 4652 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4653 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4654 "ISO C forbids conditional expr between "
bda67431 4655 "%<void *%> and function pointer");
3e4093b6
RS
4656 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4657 TREE_TYPE (type2)));
34a80643 4658 }
267bac10
JM
4659 else if (VOID_TYPE_P (TREE_TYPE (type2))
4660 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4661 {
768952be
MU
4662 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
4663 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
4664 & ~TYPE_QUALS (TREE_TYPE (type2))))
4665 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
4666 "pointer to array loses qualifier "
4667 "in conditional expression");
4668
fcf73884 4669 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4670 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4671 "ISO C forbids conditional expr between "
bda67431 4672 "%<void *%> and function pointer");
3e4093b6
RS
4673 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4674 TREE_TYPE (type1)));
1c2a9b35 4675 }
b581b85b
NP
4676 /* Objective-C pointer comparisons are a bit more lenient. */
4677 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4678 result_type = objc_common_type (type1, type2);
34a80643 4679 else
ab87f8c8 4680 {
36c5e70a
BE
4681 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4682
b581b85b
NP
4683 pedwarn (colon_loc, 0,
4684 "pointer type mismatch in conditional expression");
36c5e70a
BE
4685 result_type = build_pointer_type
4686 (build_qualified_type (void_type_node, qual));
ab87f8c8 4687 }
3e4093b6
RS
4688 }
4689 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4690 {
6aa3c60d 4691 if (!null_pointer_constant_p (orig_op2))
744aa42f 4692 pedwarn (colon_loc, 0,
509c9d60 4693 "pointer/integer type mismatch in conditional expression");
3e4093b6 4694 else
ab87f8c8 4695 {
3e4093b6 4696 op2 = null_pointer_node;
ab87f8c8 4697 }
3e4093b6
RS
4698 result_type = type1;
4699 }
4700 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4701 {
6aa3c60d 4702 if (!null_pointer_constant_p (orig_op1))
744aa42f 4703 pedwarn (colon_loc, 0,
509c9d60 4704 "pointer/integer type mismatch in conditional expression");
3e4093b6 4705 else
ab87f8c8 4706 {
3e4093b6 4707 op1 = null_pointer_node;
ab87f8c8 4708 }
3e4093b6
RS
4709 result_type = type2;
4710 }
1c2a9b35 4711
3e4093b6
RS
4712 if (!result_type)
4713 {
4714 if (flag_cond_mismatch)
4715 result_type = void_type_node;
4716 else
400fbf9f 4717 {
c2255bc4 4718 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4719 return error_mark_node;
400fbf9f 4720 }
3e4093b6 4721 }
400fbf9f 4722
3e4093b6
RS
4723 /* Merge const and volatile flags of the incoming types. */
4724 result_type
4725 = build_type_variant (result_type,
afbd0665
AS
4726 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4727 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4728
68fca595
MP
4729 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4730 semantic_result_type);
4731 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4732 semantic_result_type);
b6a10c9f 4733
928c19bb
JM
4734 if (ifexp_bcp && ifexp == truthvalue_true_node)
4735 {
4736 op2_int_operands = true;
4737 op1 = c_fully_fold (op1, require_constant_value, NULL);
4738 }
4739 if (ifexp_bcp && ifexp == truthvalue_false_node)
4740 {
4741 op1_int_operands = true;
4742 op2 = c_fully_fold (op2, require_constant_value, NULL);
4743 }
4d84fe7c 4744 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4745 && op1_int_operands
4746 && op2_int_operands);
4747 if (int_operands)
4748 {
4749 int_const = ((ifexp == truthvalue_true_node
4750 && TREE_CODE (orig_op1) == INTEGER_CST
4751 && !TREE_OVERFLOW (orig_op1))
4752 || (ifexp == truthvalue_false_node
4753 && TREE_CODE (orig_op2) == INTEGER_CST
4754 && !TREE_OVERFLOW (orig_op2)));
4755 }
4756 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4757 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4758 else
4759 {
01c7ccbb
JM
4760 if (int_operands)
4761 {
f34f1c87
MP
4762 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4763 nested inside of the expression. */
4764 op1 = c_fully_fold (op1, false, NULL);
4765 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4766 }
928c19bb
JM
4767 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4768 if (int_operands)
4769 ret = note_integer_operands (ret);
4770 }
2d2e923f
MLI
4771 if (semantic_result_type)
4772 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4773
c2255bc4 4774 protected_set_expr_location (ret, colon_loc);
928c19bb 4775 return ret;
3e4093b6
RS
4776}
4777\f
487a92fe 4778/* Return a compound expression that performs two expressions and
c2255bc4
AH
4779 returns the value of the second of them.
4780
4781 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4782
3e4093b6 4783tree
c2255bc4 4784build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4785{
4d84fe7c 4786 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4787 tree eptype = NULL_TREE;
928c19bb
JM
4788 tree ret;
4789
b72271b9 4790 if (flag_cilkplus
939b37da
BI
4791 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4792 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4793 {
4794 error_at (loc,
4795 "spawned function call cannot be part of a comma expression");
4796 return error_mark_node;
4797 }
4d84fe7c
JM
4798 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4799 if (expr1_int_operands)
4800 expr1 = remove_c_maybe_const_expr (expr1);
4801 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4802 if (expr2_int_operands)
4803 expr2 = remove_c_maybe_const_expr (expr2);
4804
8ce94e44
JM
4805 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4806 expr1 = TREE_OPERAND (expr1, 0);
4807 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4808 {
4809 eptype = TREE_TYPE (expr2);
4810 expr2 = TREE_OPERAND (expr2, 0);
4811 }
4812
3f75a254 4813 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4814 {
4815 /* The left-hand operand of a comma expression is like an expression
c5409249 4816 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4817 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4818 if (warn_unused_value)
47aecf47 4819 {
e14a6540 4820 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4821 && CONVERT_EXPR_P (expr1))
47aecf47 4822 ; /* (void) a, b */
e14a6540
JM
4823 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4824 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4825 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4826 ; /* (void) a, (void) b, c */
4827 else
b8698a0f 4828 warning_at (loc, OPT_Wunused_value,
c2255bc4 4829 "left-hand operand of comma expression has no effect");
47aecf47 4830 }
3e4093b6 4831 }
789eadcd
MP
4832 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4833 && warn_unused_value)
4834 {
4835 tree r = expr1;
4836 location_t cloc = loc;
4837 while (TREE_CODE (r) == COMPOUND_EXPR)
4838 {
4839 if (EXPR_HAS_LOCATION (r))
4840 cloc = EXPR_LOCATION (r);
4841 r = TREE_OPERAND (r, 1);
4842 }
4843 if (!TREE_SIDE_EFFECTS (r)
4844 && !VOID_TYPE_P (TREE_TYPE (r))
4845 && !CONVERT_EXPR_P (r))
4846 warning_at (cloc, OPT_Wunused_value,
4847 "right-hand operand of comma expression has no effect");
4848 }
400fbf9f 4849
3e4093b6
RS
4850 /* With -Wunused, we should also warn if the left-hand operand does have
4851 side-effects, but computes a value which is not used. For example, in
4852 `foo() + bar(), baz()' the result of the `+' operator is not used,
4853 so we should issue a warning. */
4854 else if (warn_unused_value)
c2255bc4 4855 warn_if_unused_value (expr1, loc);
400fbf9f 4856
e63d6886
AP
4857 if (expr2 == error_mark_node)
4858 return error_mark_node;
4859
928c19bb
JM
4860 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4861
4862 if (flag_isoc99
4d84fe7c
JM
4863 && expr1_int_operands
4864 && expr2_int_operands)
928c19bb
JM
4865 ret = note_integer_operands (ret);
4866
8ce94e44
JM
4867 if (eptype)
4868 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4869
c2255bc4 4870 protected_set_expr_location (ret, loc);
928c19bb 4871 return ret;
3e4093b6 4872}
400fbf9f 4873
67165eb3
ILT
4874/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4875 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4876 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4877 of the cast. -Wcast-qual appeared on the command line. Named
4878 address space qualifiers are not handled here, because they result
4879 in different warnings. */
67165eb3
ILT
4880
4881static void
2ee3cb35 4882handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4883{
4884 tree in_type = type;
4885 tree in_otype = otype;
4886 int added = 0;
4887 int discarded = 0;
4888 bool is_const;
4889
4890 /* Check that the qualifiers on IN_TYPE are a superset of the
4891 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4892 nodes is uninteresting and we stop as soon as we hit a
4893 non-POINTER_TYPE node on either type. */
4894 do
4895 {
4896 in_otype = TREE_TYPE (in_otype);
4897 in_type = TREE_TYPE (in_type);
4898
4899 /* GNU C allows cv-qualified function types. 'const' means the
4900 function is very pure, 'volatile' means it can't return. We
4901 need to warn when such qualifiers are added, not when they're
4902 taken away. */
4903 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4904 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4905 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4906 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4907 else
36c5e70a
BE
4908 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4909 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4910 }
4911 while (TREE_CODE (in_type) == POINTER_TYPE
4912 && TREE_CODE (in_otype) == POINTER_TYPE);
4913
4914 if (added)
2ee3cb35
MLI
4915 warning_at (loc, OPT_Wcast_qual,
4916 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4917
4918 if (discarded)
4919 /* There are qualifiers present in IN_OTYPE that are not present
4920 in IN_TYPE. */
2ee3cb35 4921 warning_at (loc, OPT_Wcast_qual,
7485aeea 4922 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4923 discarded);
67165eb3
ILT
4924
4925 if (added || discarded)
4926 return;
4927
4928 /* A cast from **T to const **T is unsafe, because it can cause a
4929 const value to be changed with no additional warning. We only
4930 issue this warning if T is the same on both sides, and we only
4931 issue the warning if there are the same number of pointers on
4932 both sides, as otherwise the cast is clearly unsafe anyhow. A
4933 cast is unsafe when a qualifier is added at one level and const
4934 is not present at all outer levels.
4935
4936 To issue this warning, we check at each level whether the cast
4937 adds new qualifiers not already seen. We don't need to special
4938 case function types, as they won't have the same
4939 TYPE_MAIN_VARIANT. */
4940
4941 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4942 return;
4943 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4944 return;
4945
4946 in_type = type;
4947 in_otype = otype;
4948 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4949 do
4950 {
4951 in_type = TREE_TYPE (in_type);
4952 in_otype = TREE_TYPE (in_otype);
4953 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4954 && !is_const)
4955 {
2ee3cb35
MLI
4956 warning_at (loc, OPT_Wcast_qual,
4957 "to be safe all intermediate pointers in cast from "
4958 "%qT to %qT must be %<const%> qualified",
4959 otype, type);
67165eb3
ILT
4960 break;
4961 }
4962 if (is_const)
4963 is_const = TYPE_READONLY (in_type);
4964 }
4965 while (TREE_CODE (in_type) == POINTER_TYPE);
4966}
4967
b8698a0f 4968/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4969 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4970
3e4093b6 4971tree
c2255bc4 4972build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4973{
8ce94e44
JM
4974 tree value;
4975
4976 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4977 expr = TREE_OPERAND (expr, 0);
4978
4979 value = expr;
400fbf9f 4980
3e4093b6
RS
4981 if (type == error_mark_node || expr == error_mark_node)
4982 return error_mark_node;
400fbf9f 4983
3e4093b6
RS
4984 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4985 only in <protocol> qualifications. But when constructing cast expressions,
4986 the protocols do matter and must be kept around. */
700686fa
ZL
4987 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4988 return build1 (NOP_EXPR, type, expr);
4989
4990 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4991
3e4093b6
RS
4992 if (TREE_CODE (type) == ARRAY_TYPE)
4993 {
c2255bc4 4994 error_at (loc, "cast specifies array type");
3e4093b6
RS
4995 return error_mark_node;
4996 }
400fbf9f 4997
3e4093b6
RS
4998 if (TREE_CODE (type) == FUNCTION_TYPE)
4999 {
c2255bc4 5000 error_at (loc, "cast specifies function type");
3e4093b6
RS
5001 return error_mark_node;
5002 }
400fbf9f 5003
808d6eaa
JM
5004 if (!VOID_TYPE_P (type))
5005 {
5006 value = require_complete_type (value);
5007 if (value == error_mark_node)
5008 return error_mark_node;
5009 }
5010
3e4093b6
RS
5011 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5012 {
fcf73884
MLI
5013 if (TREE_CODE (type) == RECORD_TYPE
5014 || TREE_CODE (type) == UNION_TYPE)
c1771a20 5015 pedwarn (loc, OPT_Wpedantic,
fcf73884 5016 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5017
5018 /* Convert to remove any qualifiers from VALUE's type. */
5019 value = convert (type, value);
3e4093b6
RS
5020 }
5021 else if (TREE_CODE (type) == UNION_TYPE)
5022 {
5023 tree field;
400fbf9f 5024
910ad8de 5025 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5026 if (TREE_TYPE (field) != error_mark_node
5027 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5028 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5029 break;
5030
5031 if (field)
400fbf9f 5032 {
3e4093b6 5033 tree t;
e616f54d 5034 bool maybe_const = true;
3e4093b6 5035
c1771a20 5036 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5037 t = c_fully_fold (value, false, &maybe_const);
5038 t = build_constructor_single (type, field, t);
5039 if (!maybe_const)
5040 t = c_wrap_maybe_const (t, true);
5041 t = digest_init (loc, type, t,
bbbbb16a 5042 NULL_TREE, false, true, 0);
3e4093b6
RS
5043 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5044 return t;
400fbf9f 5045 }
c2255bc4 5046 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5047 return error_mark_node;
5048 }
5049 else
5050 {
5051 tree otype, ovalue;
400fbf9f 5052
3e4093b6 5053 if (type == void_type_node)
c2255bc4
AH
5054 {
5055 tree t = build1 (CONVERT_EXPR, type, value);
5056 SET_EXPR_LOCATION (t, loc);
5057 return t;
5058 }
400fbf9f 5059
3e4093b6 5060 otype = TREE_TYPE (value);
400fbf9f 5061
3e4093b6 5062 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5063 if (warn_cast_qual
5064 && TREE_CODE (type) == POINTER_TYPE
5065 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5066 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5067
36c5e70a
BE
5068 /* Warn about conversions between pointers to disjoint
5069 address spaces. */
5070 if (TREE_CODE (type) == POINTER_TYPE
5071 && TREE_CODE (otype) == POINTER_TYPE
5072 && !null_pointer_constant_p (value))
5073 {
5074 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5075 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5076 addr_space_t as_common;
5077
5078 if (!addr_space_superset (as_to, as_from, &as_common))
5079 {
5080 if (ADDR_SPACE_GENERIC_P (as_from))
5081 warning_at (loc, 0, "cast to %s address space pointer "
5082 "from disjoint generic address space pointer",
5083 c_addr_space_name (as_to));
5084
5085 else if (ADDR_SPACE_GENERIC_P (as_to))
5086 warning_at (loc, 0, "cast to generic address space pointer "
5087 "from disjoint %s address space pointer",
5088 c_addr_space_name (as_from));
5089
5090 else
5091 warning_at (loc, 0, "cast to %s address space pointer "
5092 "from disjoint %s address space pointer",
5093 c_addr_space_name (as_to),
5094 c_addr_space_name (as_from));
5095 }
5096 }
5097
3e4093b6 5098 /* Warn about possible alignment problems. */
3176a0c2 5099 if (STRICT_ALIGNMENT
3e4093b6
RS
5100 && TREE_CODE (type) == POINTER_TYPE
5101 && TREE_CODE (otype) == POINTER_TYPE
5102 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5103 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5104 /* Don't warn about opaque types, where the actual alignment
5105 restriction is unknown. */
5106 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5107 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5108 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5109 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5110 warning_at (loc, OPT_Wcast_align,
5111 "cast increases required alignment of target type");
e9a25f70 5112
3176a0c2 5113 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5114 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5115 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5116 /* Unlike conversion of integers to pointers, where the
5117 warning is disabled for converting constants because
5118 of cases such as SIG_*, warn about converting constant
5119 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5120 sign extension, and a warning is appropriate. */
c2255bc4
AH
5121 warning_at (loc, OPT_Wpointer_to_int_cast,
5122 "cast from pointer to integer of different size");
400fbf9f 5123
3176a0c2 5124 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5125 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5126 warning_at (loc, OPT_Wbad_function_cast,
5127 "cast from function call of type %qT "
5128 "to non-matching type %qT", otype, type);
400fbf9f 5129
3176a0c2 5130 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5131 && TREE_CODE (otype) == INTEGER_TYPE
5132 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5133 /* Don't warn about converting any constant. */
5134 && !TREE_CONSTANT (value))
c2255bc4
AH
5135 warning_at (loc,
5136 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5137 "of different size");
400fbf9f 5138
79bedddc
SR
5139 if (warn_strict_aliasing <= 2)
5140 strict_aliasing_warning (otype, type, expr);
400fbf9f 5141
3897f229
JM
5142 /* If pedantic, warn for conversions between function and object
5143 pointer types, except for converting a null pointer constant
5144 to function pointer type. */
5145 if (pedantic
5146 && TREE_CODE (type) == POINTER_TYPE
5147 && TREE_CODE (otype) == POINTER_TYPE
5148 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5149 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5150 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5151 "conversion of function pointer to object pointer type");
3897f229
JM
5152
5153 if (pedantic
5154 && TREE_CODE (type) == POINTER_TYPE
5155 && TREE_CODE (otype) == POINTER_TYPE
5156 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5157 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5158 && !null_pointer_constant_p (value))
c1771a20 5159 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5160 "conversion of object pointer to function pointer type");
3897f229 5161
3e4093b6 5162 ovalue = value;
3e4093b6 5163 value = convert (type, value);
400fbf9f 5164
3e4093b6 5165 /* Ignore any integer overflow caused by the cast. */
928c19bb 5166 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5167 {
8bcd6380 5168 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5169 {
8bcd6380
RS
5170 if (!TREE_OVERFLOW (value))
5171 {
5172 /* Avoid clobbering a shared constant. */
5173 value = copy_node (value);
5174 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5175 }
6414bad6 5176 }
8bcd6380 5177 else if (TREE_OVERFLOW (value))
d8e1f97b 5178 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5179 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5180 }
5181 }
400fbf9f 5182
53cd18ec 5183 /* Don't let a cast be an lvalue. */
9482b620 5184 if (lvalue_p (value))
db3927fb 5185 value = non_lvalue_loc (loc, value);
e9a25f70 5186
928c19bb
JM
5187 /* Don't allow the results of casting to floating-point or complex
5188 types be confused with actual constants, or casts involving
5189 integer and pointer types other than direct integer-to-integer
5190 and integer-to-pointer be confused with integer constant
5191 expressions and null pointer constants. */
5192 if (TREE_CODE (value) == REAL_CST
5193 || TREE_CODE (value) == COMPLEX_CST
5194 || (TREE_CODE (value) == INTEGER_CST
5195 && !((TREE_CODE (expr) == INTEGER_CST
5196 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5197 || TREE_CODE (expr) == REAL_CST
5198 || TREE_CODE (expr) == COMPLEX_CST)))
5199 value = build1 (NOP_EXPR, type, value);
5200
c2255bc4
AH
5201 if (CAN_HAVE_LOCATION_P (value))
5202 SET_EXPR_LOCATION (value, loc);
3e4093b6 5203 return value;
400fbf9f
JW
5204}
5205
c2255bc4
AH
5206/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5207 location of the open paren of the cast, or the position of the cast
5208 expr. */
3e4093b6 5209tree
c2255bc4 5210c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5211{
f8893e47 5212 tree type;
928c19bb
JM
5213 tree type_expr = NULL_TREE;
5214 bool type_expr_const = true;
5215 tree ret;
3e4093b6 5216 int saved_wsp = warn_strict_prototypes;
c5c76735 5217
3e4093b6
RS
5218 /* This avoids warnings about unprototyped casts on
5219 integers. E.g. "#define SIG_DFL (void(*)())0". */
5220 if (TREE_CODE (expr) == INTEGER_CST)
5221 warn_strict_prototypes = 0;
928c19bb 5222 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5223 warn_strict_prototypes = saved_wsp;
c5c76735 5224
c2255bc4 5225 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5226 if (type_expr)
5227 {
9f33203d
JM
5228 bool inner_expr_const = true;
5229 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5230 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5231 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5232 && inner_expr_const);
c2255bc4 5233 SET_EXPR_LOCATION (ret, loc);
928c19bb 5234 }
24b97832
ILT
5235
5236 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5237 SET_EXPR_LOCATION (ret, loc);
5238
9e5b2115
PB
5239 /* C++ does not permits types to be defined in a cast, but it
5240 allows references to incomplete types. */
5241 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5242 warning_at (loc, OPT_Wc___compat,
5243 "defining a type in a cast is invalid in C++");
5244
928c19bb 5245 return ret;
400fbf9f 5246}
3e4093b6
RS
5247\f
5248/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5249 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5250 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5251 MODIFYCODE is the code for a binary operator that we use
5252 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5253 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5254 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5255 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5256
c2255bc4
AH
5257 LOCATION is the location of the MODIFYCODE operator.
5258 RHS_LOC is the location of the RHS. */
2f6e4e97 5259
3e4093b6 5260tree
32e8bb8e 5261build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5262 enum tree_code modifycode,
c2255bc4 5263 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5264{
3e4093b6
RS
5265 tree result;
5266 tree newrhs;
241f845a 5267 tree rhseval = NULL_TREE;
8ce94e44 5268 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5269 tree lhstype = TREE_TYPE (lhs);
5270 tree olhstype = lhstype;
928c19bb 5271 bool npc;
267bac10 5272 bool is_atomic_op;
e9a25f70 5273
3e4093b6
RS
5274 /* Types that aren't fully specified cannot be used in assignments. */
5275 lhs = require_complete_type (lhs);
e9a25f70 5276
3e4093b6
RS
5277 /* Avoid duplicate error messages from operands that had errors. */
5278 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5279 return error_mark_node;
400fbf9f 5280
4c2ecab0
JM
5281 /* Ensure an error for assigning a non-lvalue array to an array in
5282 C90. */
5283 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5284 {
5285 error_at (location, "assignment to expression with array type");
5286 return error_mark_node;
5287 }
5288
46a88c12 5289 /* For ObjC properties, defer this check. */
7bd11157 5290 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5291 return error_mark_node;
5292
267bac10
JM
5293 is_atomic_op = really_atomic_lvalue (lhs);
5294
8ce94e44
JM
5295 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5296 {
5297 rhs_semantic_type = TREE_TYPE (rhs);
5298 rhs = TREE_OPERAND (rhs, 0);
5299 }
5300
3e4093b6 5301 newrhs = rhs;
400fbf9f 5302
928c19bb
JM
5303 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5304 {
5305 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5306 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5307 rhs_origtype);
928c19bb
JM
5308 if (inner == error_mark_node)
5309 return error_mark_node;
5310 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5311 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5312 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5313 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5314 protected_set_expr_location (result, location);
5315 return result;
5316 }
5317
3e4093b6
RS
5318 /* If a binary op has been requested, combine the old LHS value with the RHS
5319 producing the value we should actually store into the LHS. */
5320
5321 if (modifycode != NOP_EXPR)
400fbf9f 5322 {
928c19bb 5323 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5324 lhs = stabilize_reference (lhs);
bbbbb16a 5325
267bac10
JM
5326 /* Construct the RHS for any non-atomic compound assignemnt. */
5327 if (!is_atomic_op)
5328 {
241f845a
JJ
5329 /* If in LHS op= RHS the RHS has side-effects, ensure they
5330 are preevaluated before the rest of the assignment expression's
5331 side-effects, because RHS could contain e.g. function calls
5332 that modify LHS. */
5333 if (TREE_SIDE_EFFECTS (rhs))
5334 {
5335 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5336 rhseval = newrhs;
5337 }
267bac10 5338 newrhs = build_binary_op (location,
241f845a 5339 modifycode, lhs, newrhs, 1);
267bac10
JM
5340
5341 /* The original type of the right hand side is no longer
5342 meaningful. */
5343 rhs_origtype = NULL_TREE;
5344 }
400fbf9f 5345 }
400fbf9f 5346
668ea4b1
IS
5347 if (c_dialect_objc ())
5348 {
46a88c12
NP
5349 /* Check if we are modifying an Objective-C property reference;
5350 if so, we need to generate setter calls. */
5351 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5352 if (result)
241f845a 5353 goto return_result;
46a88c12
NP
5354
5355 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5356 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5357 return error_mark_node;
5358 }
5359
9bf24266 5360 /* Give an error for storing in something that is 'const'. */
bbbd6700 5361
f37acdf9 5362 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5363 || ((TREE_CODE (lhstype) == RECORD_TYPE
5364 || TREE_CODE (lhstype) == UNION_TYPE)
5365 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5366 {
c02065fc 5367 readonly_error (location, lhs, lv_assign);
953ff289
DN
5368 return error_mark_node;
5369 }
f37acdf9
JM
5370 else if (TREE_READONLY (lhs))
5371 readonly_warning (lhs, lv_assign);
bbbd6700 5372
3e4093b6
RS
5373 /* If storing into a structure or union member,
5374 it has probably been given type `int'.
5375 Compute the type that would go with
5376 the actual amount of storage the member occupies. */
bbbd6700 5377
3e4093b6
RS
5378 if (TREE_CODE (lhs) == COMPONENT_REF
5379 && (TREE_CODE (lhstype) == INTEGER_TYPE
5380 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5381 || TREE_CODE (lhstype) == REAL_TYPE
5382 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5383 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5384
3e4093b6
RS
5385 /* If storing in a field that is in actuality a short or narrower than one,
5386 we must store in the field in its actual type. */
5387
5388 if (lhstype != TREE_TYPE (lhs))
5389 {
5390 lhs = copy_node (lhs);
5391 TREE_TYPE (lhs) = lhstype;
400fbf9f 5392 }
400fbf9f 5393
32e8bb8e
ILT
5394 /* Issue -Wc++-compat warnings about an assignment to an enum type
5395 when LHS does not have its original type. This happens for,
5396 e.g., an enum bitfield in a struct. */
5397 if (warn_cxx_compat
5398 && lhs_origtype != NULL_TREE
5399 && lhs_origtype != lhstype
5400 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5401 {
5402 tree checktype = (rhs_origtype != NULL_TREE
5403 ? rhs_origtype
5404 : TREE_TYPE (rhs));
5405 if (checktype != error_mark_node
267bac10
JM
5406 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5407 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5408 warning_at (location, OPT_Wc___compat,
5409 "enum conversion in assignment is invalid in C++");
5410 }
5411
267bac10
JM
5412 /* If the lhs is atomic, remove that qualifier. */
5413 if (is_atomic_op)
5414 {
5415 lhstype = build_qualified_type (lhstype,
5416 (TYPE_QUALS (lhstype)
5417 & ~TYPE_QUAL_ATOMIC));
5418 olhstype = build_qualified_type (olhstype,
5419 (TYPE_QUALS (lhstype)
5420 & ~TYPE_QUAL_ATOMIC));
5421 }
5422
8ce94e44
JM
5423 /* Convert new value to destination type. Fold it first, then
5424 restore any excess precision information, for the sake of
5425 conversion warnings. */
400fbf9f 5426
267bac10
JM
5427 if (!(is_atomic_op && modifycode != NOP_EXPR))
5428 {
5429 npc = null_pointer_constant_p (newrhs);
5430 newrhs = c_fully_fold (newrhs, false, NULL);
5431 if (rhs_semantic_type)
5432 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5433 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5434 rhs_origtype, ic_assign, npc,
5435 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5436 if (TREE_CODE (newrhs) == ERROR_MARK)
5437 return error_mark_node;
5438 }
400fbf9f 5439
6e955430
ZL
5440 /* Emit ObjC write barrier, if necessary. */
5441 if (c_dialect_objc () && flag_objc_gc)
5442 {
5443 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5444 if (result)
c9f9eb5d
AH
5445 {
5446 protected_set_expr_location (result, location);
241f845a 5447 goto return_result;
c9f9eb5d 5448 }
6e955430
ZL
5449 }
5450
ea4b7848 5451 /* Scan operands. */
400fbf9f 5452
267bac10
JM
5453 if (is_atomic_op)
5454 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5455 else
5456 {
5457 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5458 TREE_SIDE_EFFECTS (result) = 1;
5459 protected_set_expr_location (result, location);
5460 }
400fbf9f 5461
3e4093b6
RS
5462 /* If we got the LHS in a different type for storing in,
5463 convert the result back to the nominal type of LHS
5464 so that the value we return always has the same type
5465 as the LHS argument. */
e855c5ce 5466
3e4093b6 5467 if (olhstype == TREE_TYPE (result))
241f845a 5468 goto return_result;
c9f9eb5d 5469
68fca595
MP
5470 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5471 rhs_origtype, ic_assign, false, NULL_TREE,
5472 NULL_TREE, 0);
c9f9eb5d 5473 protected_set_expr_location (result, location);
241f845a
JJ
5474
5475return_result:
5476 if (rhseval)
5477 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5478 return result;
3e4093b6
RS
5479}
5480\f
478a1c5b
ILT
5481/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5482 This is used to implement -fplan9-extensions. */
5483
5484static bool
5485find_anonymous_field_with_type (tree struct_type, tree type)
5486{
5487 tree field;
5488 bool found;
5489
5490 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5491 || TREE_CODE (struct_type) == UNION_TYPE);
5492 found = false;
5493 for (field = TYPE_FIELDS (struct_type);
5494 field != NULL_TREE;
5495 field = TREE_CHAIN (field))
5496 {
267bac10
JM
5497 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5498 ? c_build_qualified_type (TREE_TYPE (field),
5499 TYPE_QUAL_ATOMIC)
5500 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5501 if (DECL_NAME (field) == NULL
267bac10 5502 && comptypes (type, fieldtype))
478a1c5b
ILT
5503 {
5504 if (found)
5505 return false;
5506 found = true;
5507 }
5508 else if (DECL_NAME (field) == NULL
5509 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5510 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5511 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5512 {
5513 if (found)
5514 return false;
5515 found = true;
5516 }
5517 }
5518 return found;
5519}
5520
5521/* RHS is an expression whose type is pointer to struct. If there is
5522 an anonymous field in RHS with type TYPE, then return a pointer to
5523 that field in RHS. This is used with -fplan9-extensions. This
5524 returns NULL if no conversion could be found. */
5525
5526static tree
5527convert_to_anonymous_field (location_t location, tree type, tree rhs)
5528{
5529 tree rhs_struct_type, lhs_main_type;
5530 tree field, found_field;
5531 bool found_sub_field;
5532 tree ret;
5533
5534 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5535 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5536 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5537 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5538
5539 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5540 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5541 ? c_build_qualified_type (TREE_TYPE (type),
5542 TYPE_QUAL_ATOMIC)
5543 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5544
5545 found_field = NULL_TREE;
5546 found_sub_field = false;
5547 for (field = TYPE_FIELDS (rhs_struct_type);
5548 field != NULL_TREE;
5549 field = TREE_CHAIN (field))
5550 {
5551 if (DECL_NAME (field) != NULL_TREE
5552 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5553 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5554 continue;
267bac10
JM
5555 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5556 ? c_build_qualified_type (TREE_TYPE (field),
5557 TYPE_QUAL_ATOMIC)
5558 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5559 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5560 {
5561 if (found_field != NULL_TREE)
5562 return NULL_TREE;
5563 found_field = field;
5564 }
5565 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5566 lhs_main_type))
5567 {
5568 if (found_field != NULL_TREE)
5569 return NULL_TREE;
5570 found_field = field;
5571 found_sub_field = true;
5572 }
5573 }
5574
5575 if (found_field == NULL_TREE)
5576 return NULL_TREE;
5577
5578 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5579 build_fold_indirect_ref (rhs), found_field,
5580 NULL_TREE);
5581 ret = build_fold_addr_expr_loc (location, ret);
5582
5583 if (found_sub_field)
5584 {
5585 ret = convert_to_anonymous_field (location, type, ret);
5586 gcc_assert (ret != NULL_TREE);
5587 }
5588
5589 return ret;
5590}
5591
63bc4e87
MP
5592/* Issue an error message for a bad initializer component.
5593 GMSGID identifies the message.
5594 The component name is taken from the spelling stack. */
5595
5596static void
ea58ef42 5597error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5598{
5599 char *ofwhat;
5600
5601 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5602 error_at (loc, gmsgid);
63bc4e87
MP
5603 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5604 if (*ofwhat)
d7ff7ae5 5605 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5606}
5607
5608/* Issue a pedantic warning for a bad initializer component. OPT is
5609 the option OPT_* (from options.h) controlling this warning or 0 if
5610 it is unconditionally given. GMSGID identifies the message. The
5611 component name is taken from the spelling stack. */
5612
5613static void
5614pedwarn_init (location_t location, int opt, const char *gmsgid)
5615{
5616 char *ofwhat;
d7ff7ae5 5617 bool warned;
63bc4e87
MP
5618
5619 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5620 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5621 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5622 if (*ofwhat && warned)
5623 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5624}
5625
5626/* Issue a warning for a bad initializer component.
5627
5628 OPT is the OPT_W* value corresponding to the warning option that
5629 controls this warning. GMSGID identifies the message. The
5630 component name is taken from the spelling stack. */
5631
5632static void
5633warning_init (location_t loc, int opt, const char *gmsgid)
5634{
5635 char *ofwhat;
d7ff7ae5 5636 bool warned;
63bc4e87
MP
5637
5638 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5639 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5640 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5641 if (*ofwhat && warned)
5642 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5643}
5644\f
5645/* If TYPE is an array type and EXPR is a parenthesized string
5646 constant, warn if pedantic that EXPR is being used to initialize an
5647 object of type TYPE. */
5648
5649void
d033409e 5650maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5651{
5652 if (pedantic
5653 && TREE_CODE (type) == ARRAY_TYPE
5654 && TREE_CODE (expr.value) == STRING_CST
5655 && expr.original_code != STRING_CST)
d033409e 5656 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5657 "array initialized from parenthesized string constant");
5658}
5659
bbbbb16a
ILT
5660/* Convert value RHS to type TYPE as preparation for an assignment to
5661 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5662 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5663 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5664 constant before any folding.
3e4093b6
RS
5665 The real work of conversion is done by `convert'.
5666 The purpose of this function is to generate error messages
5667 for assignments that are not allowed in C.
2ac2f164
JM
5668 ERRTYPE says whether it is argument passing, assignment,
5669 initialization or return.
2f6e4e97 5670
81e5eca8
MP
5671 LOCATION is the location of the assignment, EXPR_LOC is the location of
5672 the RHS or, for a function, location of an argument.
2ac2f164 5673 FUNCTION is a tree for the function being called.
3e4093b6 5674 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5675
3e4093b6 5676static tree
81e5eca8 5677convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5678 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5679 bool null_pointer_constant, tree fundecl,
5680 tree function, int parmnum)
3e4093b6
RS
5681{
5682 enum tree_code codel = TREE_CODE (type);
8ce94e44 5683 tree orig_rhs = rhs;
3e4093b6
RS
5684 tree rhstype;
5685 enum tree_code coder;
2ac2f164 5686 tree rname = NULL_TREE;
58393038 5687 bool objc_ok = false;
2ac2f164 5688
6b4ef5c1 5689 if (errtype == ic_argpass)
2ac2f164
JM
5690 {
5691 tree selector;
5692 /* Change pointer to function to the function itself for
5693 diagnostics. */
5694 if (TREE_CODE (function) == ADDR_EXPR
5695 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5696 function = TREE_OPERAND (function, 0);
5697
5698 /* Handle an ObjC selector specially for diagnostics. */
5699 selector = objc_message_selector ();
5700 rname = function;
5701 if (selector && parmnum > 2)
5702 {
5703 rname = selector;
5704 parmnum -= 2;
5705 }
5706 }
5707
5708 /* This macro is used to emit diagnostics to ensure that all format
5709 strings are complete sentences, visible to gettext and checked at
5710 compile time. */
768952be 5711#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5712 do { \
5713 switch (errtype) \
5714 { \
5715 case ic_argpass: \
5c1bc275 5716 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5717 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5718 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5719 "expected %qT but argument is of type %qT", \
5720 type, rhstype); \
5721 break; \
1e053dfe
MLI
5722 case ic_assign: \
5723 pedwarn (LOCATION, OPT, AS); \
5724 break; \
5725 case ic_init: \
6a8f4e12 5726 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5727 break; \
5728 case ic_return: \
d033409e 5729 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5730 break; \
5731 default: \
5732 gcc_unreachable (); \
5733 } \
2ac2f164 5734 } while (0)
cb3ca04e 5735
49706e39
MLI
5736 /* This macro is used to emit diagnostics to ensure that all format
5737 strings are complete sentences, visible to gettext and checked at
768952be 5738 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 5739 extra parameter to enumerate qualifiers. */
768952be 5740#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5741 do { \
5742 switch (errtype) \
5743 { \
5744 case ic_argpass: \
5c1bc275 5745 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5746 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5747 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5748 "expected %qT but argument is of type %qT", \
5749 type, rhstype); \
5750 break; \
5751 case ic_assign: \
5c1bc275 5752 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5753 break; \
5754 case ic_init: \
5c1bc275 5755 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5756 break; \
5757 case ic_return: \
5c1bc275 5758 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5759 break; \
5760 default: \
5761 gcc_unreachable (); \
5762 } \
5763 } while (0)
5764
768952be
MU
5765 /* This macro is used to emit diagnostics to ensure that all format
5766 strings are complete sentences, visible to gettext and checked at
5767 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
5768 warning_at instead of pedwarn. */
5769#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
5770 do { \
5771 switch (errtype) \
5772 { \
5773 case ic_argpass: \
5774 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
5775 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5776 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
5777 "expected %qT but argument is of type %qT", \
5778 type, rhstype); \
5779 break; \
5780 case ic_assign: \
5781 warning_at (LOCATION, OPT, AS, QUALS); \
5782 break; \
5783 case ic_init: \
5784 warning_at (LOCATION, OPT, IN, QUALS); \
5785 break; \
5786 case ic_return: \
5787 warning_at (LOCATION, OPT, RE, QUALS); \
5788 break; \
5789 default: \
5790 gcc_unreachable (); \
5791 } \
5792 } while (0)
5793
8ce94e44
JM
5794 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5795 rhs = TREE_OPERAND (rhs, 0);
5796
3e4093b6
RS
5797 rhstype = TREE_TYPE (rhs);
5798 coder = TREE_CODE (rhstype);
5799
5800 if (coder == ERROR_MARK)
5801 return error_mark_node;
5802
58393038
ZL
5803 if (c_dialect_objc ())
5804 {
5805 int parmno;
5806
5807 switch (errtype)
5808 {
5809 case ic_return:
5810 parmno = 0;
5811 break;
5812
5813 case ic_assign:
5814 parmno = -1;
5815 break;
5816
5817 case ic_init:
5818 parmno = -2;
5819 break;
5820
5821 default:
5822 parmno = parmnum;
5823 break;
5824 }
5825
5826 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5827 }
5828
bbbbb16a
ILT
5829 if (warn_cxx_compat)
5830 {
5831 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5832 if (checktype != error_mark_node
5833 && TREE_CODE (type) == ENUMERAL_TYPE
5834 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5835 {
768952be
MU
5836 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
5837 G_("enum conversion when passing argument "
5838 "%d of %qE is invalid in C++"),
5839 G_("enum conversion in assignment is "
5840 "invalid in C++"),
5841 G_("enum conversion in initialization is "
5842 "invalid in C++"),
5843 G_("enum conversion in return is "
5844 "invalid in C++"));
bbbbb16a
ILT
5845 }
5846 }
5847
3e4093b6 5848 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5849 return rhs;
3e4093b6
RS
5850
5851 if (coder == VOID_TYPE)
400fbf9f 5852 {
6dcc04b0
JM
5853 /* Except for passing an argument to an unprototyped function,
5854 this is a constraint violation. When passing an argument to
5855 an unprototyped function, it is compile-time undefined;
5856 making it a constraint in that case was rejected in
5857 DR#252. */
c2255bc4 5858 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5859 return error_mark_node;
400fbf9f 5860 }
808d6eaa
JM
5861 rhs = require_complete_type (rhs);
5862 if (rhs == error_mark_node)
5863 return error_mark_node;
cd192ccc
MS
5864 /* A non-reference type can convert to a reference. This handles
5865 va_start, va_copy and possibly port built-ins. */
5866 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5867 {
3e4093b6 5868 if (!lvalue_p (rhs))
400fbf9f 5869 {
c2255bc4 5870 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5871 return error_mark_node;
400fbf9f 5872 }
3e4093b6
RS
5873 if (!c_mark_addressable (rhs))
5874 return error_mark_node;
5875 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5876 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5877
81e5eca8 5878 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5879 build_pointer_type (TREE_TYPE (type)),
5880 rhs, origtype, errtype,
5881 null_pointer_constant, fundecl, function,
5882 parmnum);
cd192ccc
MS
5883 if (rhs == error_mark_node)
5884 return error_mark_node;
3e4093b6
RS
5885
5886 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5887 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5888 return rhs;
400fbf9f 5889 }
3e4093b6 5890 /* Some types can interconvert without explicit casts. */
3274deff 5891 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5892 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5893 return convert (type, rhs);
5894 /* Arithmetic types all interconvert, and enum is treated like int. */
5895 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5896 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5897 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5898 || codel == BOOLEAN_TYPE)
5899 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5900 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5901 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5902 || coder == BOOLEAN_TYPE))
928c19bb
JM
5903 {
5904 tree ret;
5905 bool save = in_late_binary_op;
e5341100
JJ
5906 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
5907 || (coder == REAL_TYPE
5908 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
5909 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
928c19bb 5910 in_late_binary_op = true;
81e5eca8
MP
5911 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5912 ? expr_loc : location, type, orig_rhs);
e5341100 5913 in_late_binary_op = save;
928c19bb
JM
5914 return ret;
5915 }
400fbf9f 5916
79077aea
JJ
5917 /* Aggregates in different TUs might need conversion. */
5918 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5919 && codel == coder
5920 && comptypes (type, rhstype))
81e5eca8
MP
5921 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5922 ? expr_loc : location, type, rhs);
79077aea 5923
ebf0bf7f 5924 /* Conversion to a transparent union or record from its member types.
3e4093b6 5925 This applies only to function arguments. */
ebf0bf7f
JJ
5926 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5927 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5928 && errtype == ic_argpass)
400fbf9f 5929 {
0257e383 5930 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5931
910ad8de 5932 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5933 {
0257e383 5934 tree memb_type = TREE_TYPE (memb);
400fbf9f 5935
3e4093b6 5936 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5937 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5938 break;
e58cd767 5939
3e4093b6
RS
5940 if (TREE_CODE (memb_type) != POINTER_TYPE)
5941 continue;
2f6e4e97 5942
3e4093b6
RS
5943 if (coder == POINTER_TYPE)
5944 {
5945 tree ttl = TREE_TYPE (memb_type);
5946 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5947
3e4093b6
RS
5948 /* Any non-function converts to a [const][volatile] void *
5949 and vice versa; otherwise, targets must be the same.
5950 Meanwhile, the lhs target must have all the qualifiers of
5951 the rhs. */
267bac10
JM
5952 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5953 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5954 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5955 {
267bac10
JM
5956 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5957 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5958 /* If this type won't generate any warnings, use it. */
267bac10 5959 if (lquals == rquals
3e4093b6
RS
5960 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5961 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5962 ? ((lquals | rquals) == rquals)
5963 : ((lquals | rquals) == lquals)))
3e4093b6 5964 break;
400fbf9f 5965
3e4093b6 5966 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5967 if (!marginal_memb)
5968 marginal_memb = memb;
3e4093b6
RS
5969 }
5970 }
82bde854 5971
3e4093b6 5972 /* Can convert integer zero to any pointer type. */
928c19bb 5973 if (null_pointer_constant)
3e4093b6
RS
5974 {
5975 rhs = null_pointer_node;
5976 break;
5977 }
5978 }
400fbf9f 5979
0257e383 5980 if (memb || marginal_memb)
3e4093b6 5981 {
0257e383 5982 if (!memb)
3e4093b6
RS
5983 {
5984 /* We have only a marginally acceptable member type;
5985 it needs a warning. */
0257e383 5986 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5987 tree ttr = TREE_TYPE (rhstype);
714a0864 5988
3e4093b6
RS
5989 /* Const and volatile mean something different for function
5990 types, so the usual warnings are not appropriate. */
5991 if (TREE_CODE (ttr) == FUNCTION_TYPE
5992 && TREE_CODE (ttl) == FUNCTION_TYPE)
5993 {
5994 /* Because const and volatile on functions are
5995 restrictions that say the function will not do
5996 certain things, it is okay to use a const or volatile
5997 function where an ordinary one is wanted, but not
5998 vice-versa. */
36c5e70a
BE
5999 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6000 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6001 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6002 OPT_Wdiscarded_qualifiers,
6003 G_("passing argument %d of %qE "
6004 "makes %q#v qualified function "
6005 "pointer from unqualified"),
6006 G_("assignment makes %q#v qualified "
6007 "function pointer from "
6008 "unqualified"),
6009 G_("initialization makes %q#v qualified "
6010 "function pointer from "
6011 "unqualified"),
6012 G_("return makes %q#v qualified function "
6013 "pointer from unqualified"),
6014 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6015 }
36c5e70a
BE
6016 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6017 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6018 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6019 OPT_Wdiscarded_qualifiers,
6020 G_("passing argument %d of %qE discards "
6021 "%qv qualifier from pointer target type"),
6022 G_("assignment discards %qv qualifier "
6023 "from pointer target type"),
6024 G_("initialization discards %qv qualifier "
6025 "from pointer target type"),
6026 G_("return discards %qv qualifier from "
6027 "pointer target type"),
6028 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6029
6030 memb = marginal_memb;
3e4093b6 6031 }
400fbf9f 6032
fcf73884 6033 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6034 pedwarn (location, OPT_Wpedantic,
fcf73884 6035 "ISO C prohibits argument conversion to union type");
0e7c47fa 6036
db3927fb 6037 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6038 return build_constructor_single (type, memb, rhs);
3e4093b6 6039 }
0e7c47fa
RK
6040 }
6041
3e4093b6
RS
6042 /* Conversions among pointers */
6043 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6044 && (coder == codel))
400fbf9f 6045 {
3e4093b6
RS
6046 tree ttl = TREE_TYPE (type);
6047 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6048 tree mvl = ttl;
6049 tree mvr = ttr;
3e4093b6 6050 bool is_opaque_pointer;
264fa2db 6051 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6052 addr_space_t asl;
6053 addr_space_t asr;
400fbf9f 6054
46df2823 6055 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6056 mvl = (TYPE_ATOMIC (mvl)
6057 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6058 TYPE_QUAL_ATOMIC)
6059 : TYPE_MAIN_VARIANT (mvl));
46df2823 6060 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6061 mvr = (TYPE_ATOMIC (mvr)
6062 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6063 TYPE_QUAL_ATOMIC)
6064 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6065 /* Opaque pointers are treated like void pointers. */
f83c7f63 6066 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6067
478a1c5b
ILT
6068 /* The Plan 9 compiler permits a pointer to a struct to be
6069 automatically converted into a pointer to an anonymous field
6070 within the struct. */
6071 if (flag_plan9_extensions
6072 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6073 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6074 && mvl != mvr)
6075 {
6076 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6077 if (new_rhs != NULL_TREE)
6078 {
6079 rhs = new_rhs;
6080 rhstype = TREE_TYPE (rhs);
6081 coder = TREE_CODE (rhstype);
6082 ttr = TREE_TYPE (rhstype);
6083 mvr = TYPE_MAIN_VARIANT (ttr);
6084 }
6085 }
6086
b7e20b53 6087 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6088 for the purpose of reducing the number of false positives, we
6089 tolerate the special case of
b7e20b53 6090
c22cacf3 6091 int *p = NULL;
b7e20b53 6092
c22cacf3 6093 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6094 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6095 warning_at (errtype == ic_argpass ? expr_loc : location,
6096 OPT_Wc___compat,
6097 "request for implicit conversion "
c2255bc4 6098 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6099
36c5e70a
BE
6100 /* See if the pointers point to incompatible address spaces. */
6101 asl = TYPE_ADDR_SPACE (ttl);
6102 asr = TYPE_ADDR_SPACE (ttr);
6103 if (!null_pointer_constant_p (rhs)
6104 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6105 {
6106 switch (errtype)
6107 {
6108 case ic_argpass:
8ffcdea8 6109 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6110 "non-enclosed address space", parmnum, rname);
6111 break;
6112 case ic_assign:
6113 error_at (location, "assignment from pointer to "
6114 "non-enclosed address space");
6115 break;
6116 case ic_init:
6117 error_at (location, "initialization from pointer to "
6118 "non-enclosed address space");
6119 break;
6120 case ic_return:
6121 error_at (location, "return from pointer to "
6122 "non-enclosed address space");
6123 break;
6124 default:
6125 gcc_unreachable ();
6126 }
6127 return error_mark_node;
6128 }
6129
7876a414
KG
6130 /* Check if the right-hand side has a format attribute but the
6131 left-hand side doesn't. */
90137d8f 6132 if (warn_suggest_attribute_format
104f8784 6133 && check_missing_format_attribute (type, rhstype))
c22cacf3 6134 {
104f8784
KG
6135 switch (errtype)
6136 {
6137 case ic_argpass:
8ffcdea8 6138 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6139 "argument %d of %qE might be "
6140 "a candidate for a format attribute",
6141 parmnum, rname);
104f8784
KG
6142 break;
6143 case ic_assign:
90137d8f 6144 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6145 "assignment left-hand side might be "
6146 "a candidate for a format attribute");
104f8784
KG
6147 break;
6148 case ic_init:
90137d8f 6149 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6150 "initialization left-hand side might be "
6151 "a candidate for a format attribute");
104f8784
KG
6152 break;
6153 case ic_return:
90137d8f 6154 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6155 "return type might be "
6156 "a candidate for a format attribute");
104f8784
KG
6157 break;
6158 default:
6159 gcc_unreachable ();
6160 }
7876a414 6161 }
c22cacf3 6162
3e4093b6
RS
6163 /* Any non-function converts to a [const][volatile] void *
6164 and vice versa; otherwise, targets must be the same.
6165 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6166 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6167 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6168 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6169 || is_opaque_pointer
f8a93a2e
JJ
6170 || ((c_common_unsigned_type (mvl)
6171 == c_common_unsigned_type (mvr))
267bac10
JM
6172 && (c_common_signed_type (mvl)
6173 == c_common_signed_type (mvr))
6174 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6175 {
768952be
MU
6176 /* Warn about loss of qualifers from pointers to arrays with
6177 qualifiers on the element type. */
6178 if (TREE_CODE (ttr) == ARRAY_TYPE)
6179 {
6180 ttr = strip_array_types (ttr);
6181 ttl = strip_array_types (ttl);
6182
6183 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6184 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6185 WARNING_FOR_QUALIFIERS (location, expr_loc,
6186 OPT_Wdiscarded_array_qualifiers,
6187 G_("passing argument %d of %qE discards "
6188 "%qv qualifier from pointer target type"),
6189 G_("assignment discards %qv qualifier "
6190 "from pointer target type"),
6191 G_("initialization discards %qv qualifier "
6192 "from pointer target type"),
6193 G_("return discards %qv qualifier from "
6194 "pointer target type"),
6195 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6196 }
6197 else if (pedantic
3e4093b6
RS
6198 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6199 ||
6200 (VOID_TYPE_P (ttr)
928c19bb 6201 && !null_pointer_constant
3e4093b6 6202 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6203 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6204 G_("ISO C forbids passing argument %d of "
6205 "%qE between function pointer "
6206 "and %<void *%>"),
6207 G_("ISO C forbids assignment between "
6208 "function pointer and %<void *%>"),
6209 G_("ISO C forbids initialization between "
6210 "function pointer and %<void *%>"),
6211 G_("ISO C forbids return between function "
6212 "pointer and %<void *%>"));
3e4093b6
RS
6213 /* Const and volatile mean something different for function types,
6214 so the usual warnings are not appropriate. */
6215 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6216 && TREE_CODE (ttl) != FUNCTION_TYPE)
6217 {
768952be
MU
6218 /* Don't warn about loss of qualifier for conversions from
6219 qualified void* to pointers to arrays with corresponding
6220 qualifier on the element type. */
6221 if (!pedantic)
6222 ttl = strip_array_types (ttl);
6223
267bac10
JM
6224 /* Assignments between atomic and non-atomic objects are OK. */
6225 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6226 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6227 {
768952be
MU
6228 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6229 OPT_Wdiscarded_qualifiers,
6230 G_("passing argument %d of %qE discards "
6231 "%qv qualifier from pointer target type"),
6232 G_("assignment discards %qv qualifier "
6233 "from pointer target type"),
6234 G_("initialization discards %qv qualifier "
6235 "from pointer target type"),
6236 G_("return discards %qv qualifier from "
6237 "pointer target type"),
6238 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6239 }
3e4093b6
RS
6240 /* If this is not a case of ignoring a mismatch in signedness,
6241 no warning. */
6242 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6243 || target_cmp)
3e4093b6
RS
6244 ;
6245 /* If there is a mismatch, do warn. */
f2fd3821 6246 else if (warn_pointer_sign)
768952be
MU
6247 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6248 G_("pointer targets in passing argument "
6249 "%d of %qE differ in signedness"),
6250 G_("pointer targets in assignment "
6251 "differ in signedness"),
6252 G_("pointer targets in initialization "
6253 "differ in signedness"),
6254 G_("pointer targets in return differ "
6255 "in signedness"));
3e4093b6
RS
6256 }
6257 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6258 && TREE_CODE (ttr) == FUNCTION_TYPE)
6259 {
6260 /* Because const and volatile on functions are restrictions
6261 that say the function will not do certain things,
6262 it is okay to use a const or volatile function
6263 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6264 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6265 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6266 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6267 OPT_Wdiscarded_qualifiers,
6268 G_("passing argument %d of %qE makes "
6269 "%q#v qualified function pointer "
6270 "from unqualified"),
6271 G_("assignment makes %q#v qualified function "
6272 "pointer from unqualified"),
6273 G_("initialization makes %q#v qualified "
6274 "function pointer from unqualified"),
6275 G_("return makes %q#v qualified function "
6276 "pointer from unqualified"),
6277 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6278 }
6279 }
6280 else
58393038
ZL
6281 /* Avoid warning about the volatile ObjC EH puts on decls. */
6282 if (!objc_ok)
768952be
MU
6283 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6284 OPT_Wincompatible_pointer_types,
6285 G_("passing argument %d of %qE from "
6286 "incompatible pointer type"),
6287 G_("assignment from incompatible pointer type"),
6288 G_("initialization from incompatible "
6289 "pointer type"),
6290 G_("return from incompatible pointer type"));
58393038 6291
3e4093b6
RS
6292 return convert (type, rhs);
6293 }
b494fd98
EB
6294 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6295 {
6dcc04b0
JM
6296 /* ??? This should not be an error when inlining calls to
6297 unprototyped functions. */
c2255bc4 6298 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6299 return error_mark_node;
6300 }
3e4093b6 6301 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6302 {
3e4093b6
RS
6303 /* An explicit constant 0 can convert to a pointer,
6304 or one that results from arithmetic, even including
6305 a cast to integer type. */
928c19bb 6306 if (!null_pointer_constant)
768952be
MU
6307 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6308 OPT_Wint_conversion,
6309 G_("passing argument %d of %qE makes "
6310 "pointer from integer without a cast"),
6311 G_("assignment makes pointer from integer "
6312 "without a cast"),
6313 G_("initialization makes pointer from "
6314 "integer without a cast"),
6315 G_("return makes pointer from integer "
6316 "without a cast"));
b3006337
EB
6317
6318 return convert (type, rhs);
400fbf9f 6319 }
3e4093b6 6320 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6321 {
768952be
MU
6322 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6323 OPT_Wint_conversion,
6324 G_("passing argument %d of %qE makes integer "
6325 "from pointer without a cast"),
6326 G_("assignment makes integer from pointer "
6327 "without a cast"),
6328 G_("initialization makes integer from pointer "
6329 "without a cast"),
6330 G_("return makes integer from pointer "
6331 "without a cast"));
3e4093b6
RS
6332 return convert (type, rhs);
6333 }
6334 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6335 {
6336 tree ret;
6337 bool save = in_late_binary_op;
6338 in_late_binary_op = true;
6339 ret = convert (type, rhs);
6340 in_late_binary_op = save;
6341 return ret;
6342 }
400fbf9f 6343
2ac2f164 6344 switch (errtype)
3e4093b6 6345 {
2ac2f164 6346 case ic_argpass:
8ffcdea8
MP
6347 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6348 rname);
a7da8b42 6349 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6350 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6351 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6352 break;
6353 case ic_assign:
c2255bc4
AH
6354 error_at (location, "incompatible types when assigning to type %qT from "
6355 "type %qT", type, rhstype);
2ac2f164
JM
6356 break;
6357 case ic_init:
c2255bc4 6358 error_at (location,
8ffcdea8 6359 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6360 type, rhstype);
2ac2f164
JM
6361 break;
6362 case ic_return:
c2255bc4 6363 error_at (location,
8ffcdea8 6364 "incompatible types when returning type %qT but %qT was "
c2255bc4 6365 "expected", rhstype, type);
2ac2f164
JM
6366 break;
6367 default:
6368 gcc_unreachable ();
400fbf9f 6369 }
53b01f59 6370
3e4093b6
RS
6371 return error_mark_node;
6372}
3e4093b6
RS
6373\f
6374/* If VALUE is a compound expr all of whose expressions are constant, then
6375 return its value. Otherwise, return error_mark_node.
15b732b2 6376
3e4093b6
RS
6377 This is for handling COMPOUND_EXPRs as initializer elements
6378 which is allowed with a warning when -pedantic is specified. */
15b732b2 6379
3e4093b6
RS
6380static tree
6381valid_compound_expr_initializer (tree value, tree endtype)
6382{
6383 if (TREE_CODE (value) == COMPOUND_EXPR)
6384 {
6385 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6386 == error_mark_node)
6387 return error_mark_node;
6388 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6389 endtype);
6390 }
116df786 6391 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6392 return error_mark_node;
6393 else
6394 return value;
15b732b2 6395}
400fbf9f 6396\f
3e4093b6
RS
6397/* Perform appropriate conversions on the initial value of a variable,
6398 store it in the declaration DECL,
6399 and print any error messages that are appropriate.
bbbbb16a 6400 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6401 If the init is invalid, store an ERROR_MARK.
6402
6403 INIT_LOC is the location of the initial value. */
400fbf9f 6404
3e4093b6 6405void
c2255bc4 6406store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6407{
3e4093b6 6408 tree value, type;
928c19bb 6409 bool npc = false;
400fbf9f 6410
3e4093b6 6411 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6412
3e4093b6
RS
6413 type = TREE_TYPE (decl);
6414 if (TREE_CODE (type) == ERROR_MARK)
6415 return;
400fbf9f 6416
3e4093b6 6417 /* Digest the specified initializer into an expression. */
400fbf9f 6418
928c19bb
JM
6419 if (init)
6420 npc = null_pointer_constant_p (init);
c2255bc4
AH
6421 value = digest_init (init_loc, type, init, origtype, npc,
6422 true, TREE_STATIC (decl));
400fbf9f 6423
3e4093b6 6424 /* Store the expression if valid; else report error. */
400fbf9f 6425
8400e75e 6426 if (!in_system_header_at (input_location)
3f75a254 6427 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6428 warning (OPT_Wtraditional, "traditional C rejects automatic "
6429 "aggregate initialization");
2f6e4e97 6430
dea63e49
JJ
6431 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6432 DECL_INITIAL (decl) = value;
400fbf9f 6433
3e4093b6
RS
6434 /* ANSI wants warnings about out-of-range constant initializers. */
6435 STRIP_TYPE_NOPS (value);
b8698a0f 6436 if (TREE_STATIC (decl))
c2658540 6437 constant_expression_warning (value);
400fbf9f 6438
3e4093b6
RS
6439 /* Check if we need to set array size from compound literal size. */
6440 if (TREE_CODE (type) == ARRAY_TYPE
6441 && TYPE_DOMAIN (type) == 0
6442 && value != error_mark_node)
400fbf9f 6443 {
3e4093b6
RS
6444 tree inside_init = init;
6445
ed248cf7 6446 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6447 inside_init = fold (inside_init);
6448
6449 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6450 {
8d9f82d5 6451 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6452
8d9f82d5 6453 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6454 {
6455 /* For int foo[] = (int [3]){1}; we need to set array size
6456 now since later on array initializer will be just the
6457 brace enclosed list of the compound literal. */
e30ecc5d 6458 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6459 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6460 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6461 layout_type (type);
8d9f82d5 6462 layout_decl (cldecl, 0);
e30ecc5d
JJ
6463 TREE_TYPE (decl)
6464 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6465 }
6466 }
400fbf9f 6467 }
3e4093b6
RS
6468}
6469\f
6470/* Methods for storing and printing names for error messages. */
400fbf9f 6471
3e4093b6
RS
6472/* Implement a spelling stack that allows components of a name to be pushed
6473 and popped. Each element on the stack is this structure. */
400fbf9f 6474
3e4093b6
RS
6475struct spelling
6476{
6477 int kind;
6478 union
400fbf9f 6479 {
a0f0ab9f 6480 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6481 const char *s;
6482 } u;
6483};
2f6e4e97 6484
3e4093b6
RS
6485#define SPELLING_STRING 1
6486#define SPELLING_MEMBER 2
6487#define SPELLING_BOUNDS 3
400fbf9f 6488
3e4093b6
RS
6489static struct spelling *spelling; /* Next stack element (unused). */
6490static struct spelling *spelling_base; /* Spelling stack base. */
6491static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6492
3e4093b6
RS
6493/* Macros to save and restore the spelling stack around push_... functions.
6494 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6495
3e4093b6
RS
6496#define SPELLING_DEPTH() (spelling - spelling_base)
6497#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6498
3e4093b6
RS
6499/* Push an element on the spelling stack with type KIND and assign VALUE
6500 to MEMBER. */
400fbf9f 6501
3e4093b6
RS
6502#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6503{ \
6504 int depth = SPELLING_DEPTH (); \
6505 \
6506 if (depth >= spelling_size) \
6507 { \
6508 spelling_size += 10; \
cca8ead2
BI
6509 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6510 spelling_size); \
3e4093b6
RS
6511 RESTORE_SPELLING_DEPTH (depth); \
6512 } \
6513 \
6514 spelling->kind = (KIND); \
6515 spelling->MEMBER = (VALUE); \
6516 spelling++; \
6517}
400fbf9f 6518
3e4093b6 6519/* Push STRING on the stack. Printed literally. */
400fbf9f 6520
3e4093b6
RS
6521static void
6522push_string (const char *string)
6523{
6524 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6525}
400fbf9f 6526
3e4093b6 6527/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6528
3e4093b6
RS
6529static void
6530push_member_name (tree decl)
6531{
6532 const char *const string
88388a52
JM
6533 = (DECL_NAME (decl)
6534 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6535 : _("<anonymous>"));
3e4093b6
RS
6536 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6537}
400fbf9f 6538
3e4093b6 6539/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6540
3e4093b6 6541static void
a0f0ab9f 6542push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6543{
6544 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6545}
bb58bec5 6546
3e4093b6 6547/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6548
3e4093b6
RS
6549static int
6550spelling_length (void)
6551{
6552 int size = 0;
6553 struct spelling *p;
400fbf9f 6554
3e4093b6
RS
6555 for (p = spelling_base; p < spelling; p++)
6556 {
6557 if (p->kind == SPELLING_BOUNDS)
6558 size += 25;
6559 else
6560 size += strlen (p->u.s) + 1;
6561 }
6562
6563 return size;
400fbf9f 6564}
400fbf9f 6565
3e4093b6 6566/* Print the spelling to BUFFER and return it. */
400fbf9f 6567
3e4093b6
RS
6568static char *
6569print_spelling (char *buffer)
400fbf9f 6570{
3e4093b6
RS
6571 char *d = buffer;
6572 struct spelling *p;
400fbf9f 6573
3e4093b6
RS
6574 for (p = spelling_base; p < spelling; p++)
6575 if (p->kind == SPELLING_BOUNDS)
6576 {
a0f0ab9f 6577 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6578 d += strlen (d);
6579 }
6580 else
6581 {
6582 const char *s;
6583 if (p->kind == SPELLING_MEMBER)
6584 *d++ = '.';
6585 for (s = p->u.s; (*d = *s++); d++)
6586 ;
6587 }
6588 *d++ = '\0';
6589 return buffer;
6590}
400fbf9f 6591
3e4093b6
RS
6592/* Digest the parser output INIT as an initializer for type TYPE.
6593 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6594
bbbbb16a
ILT
6595 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6596
928c19bb
JM
6597 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6598
916c5919
JM
6599 If INIT is a string constant, STRICT_STRING is true if it is
6600 unparenthesized or we should not warn here for it being parenthesized.
6601 For other types of INIT, STRICT_STRING is not used.
6602
c2255bc4
AH
6603 INIT_LOC is the location of the INIT.
6604
3e4093b6
RS
6605 REQUIRE_CONSTANT requests an error if non-constant initializers or
6606 elements are seen. */
7e842ef8 6607
3e4093b6 6608static tree
c2255bc4
AH
6609digest_init (location_t init_loc, tree type, tree init, tree origtype,
6610 bool null_pointer_constant, bool strict_string,
6611 int require_constant)
3e4093b6
RS
6612{
6613 enum tree_code code = TREE_CODE (type);
6614 tree inside_init = init;
8ce94e44 6615 tree semantic_type = NULL_TREE;
928c19bb 6616 bool maybe_const = true;
7e842ef8 6617
3e4093b6 6618 if (type == error_mark_node
f01da1a5 6619 || !init
7a0ca710 6620 || error_operand_p (init))
3e4093b6 6621 return error_mark_node;
7e842ef8 6622
ed248cf7 6623 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6624
8ce94e44
JM
6625 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6626 {
6627 semantic_type = TREE_TYPE (inside_init);
6628 inside_init = TREE_OPERAND (inside_init, 0);
6629 }
928c19bb
JM
6630 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6631 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6632
3e4093b6
RS
6633 /* Initialization of an array of chars from a string constant
6634 optionally enclosed in braces. */
7e842ef8 6635
197463ae
JM
6636 if (code == ARRAY_TYPE && inside_init
6637 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6638 {
267bac10
JM
6639 tree typ1
6640 = (TYPE_ATOMIC (TREE_TYPE (type))
6641 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6642 TYPE_QUAL_ATOMIC)
6643 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6644 /* Note that an array could be both an array of character type
6645 and an array of wchar_t if wchar_t is signed char or unsigned
6646 char. */
6647 bool char_array = (typ1 == char_type_node
6648 || typ1 == signed_char_type_node
6649 || typ1 == unsigned_char_type_node);
6650 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6651 bool char16_array = !!comptypes (typ1, char16_type_node);
6652 bool char32_array = !!comptypes (typ1, char32_type_node);
6653
6654 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6655 {
916c5919 6656 struct c_expr expr;
c466b2cd 6657 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6658 expr.value = inside_init;
6659 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6660 expr.original_type = NULL;
d033409e 6661 maybe_warn_string_init (init_loc, type, expr);
916c5919 6662
a45e580b 6663 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6664 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6665 "initialization of a flexible array member");
6666
3e4093b6 6667 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6668 TYPE_MAIN_VARIANT (type)))
3e4093b6 6669 return inside_init;
7e842ef8 6670
c466b2cd 6671 if (char_array)
3e4093b6 6672 {
c466b2cd
KVH
6673 if (typ2 != char_type_node)
6674 {
ea58ef42
MP
6675 error_init (init_loc, "char-array initialized from wide "
6676 "string");
c466b2cd
KVH
6677 return error_mark_node;
6678 }
3e4093b6 6679 }
c466b2cd 6680 else
3e4093b6 6681 {
c466b2cd
KVH
6682 if (typ2 == char_type_node)
6683 {
ea58ef42
MP
6684 error_init (init_loc, "wide character array initialized "
6685 "from non-wide string");
c466b2cd
KVH
6686 return error_mark_node;
6687 }
6688 else if (!comptypes(typ1, typ2))
6689 {
ea58ef42
MP
6690 error_init (init_loc, "wide character array initialized "
6691 "from incompatible wide string");
c466b2cd
KVH
6692 return error_mark_node;
6693 }
7e842ef8 6694 }
2f6e4e97 6695
3e4093b6
RS
6696 TREE_TYPE (inside_init) = type;
6697 if (TYPE_DOMAIN (type) != 0
6698 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6699 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6700 {
6701 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6702
c466b2cd 6703 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6704 because it's ok to ignore the terminating null char
6705 that is counted in the length of the constant. */
5eb4df45
ILT
6706 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6707 (len
6708 - (TYPE_PRECISION (typ1)
6709 / BITS_PER_UNIT))))
6710 pedwarn_init (init_loc, 0,
6711 ("initializer-string for array of chars "
6712 "is too long"));
6713 else if (warn_cxx_compat
6714 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6715 warning_at (init_loc, OPT_Wc___compat,
6716 ("initializer-string for array chars "
6717 "is too long for C++"));
6718 }
7e842ef8 6719
3e4093b6 6720 return inside_init;
7e842ef8 6721 }
197463ae
JM
6722 else if (INTEGRAL_TYPE_P (typ1))
6723 {
ea58ef42 6724 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6725 "from string constant");
6726 return error_mark_node;
6727 }
7e842ef8
PE
6728 }
6729
3e4093b6
RS
6730 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6731 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6732 below and handle as a constructor. */
e89be13b
JJ
6733 if (code == VECTOR_TYPE
6734 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6735 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6736 && TREE_CONSTANT (inside_init))
6737 {
6738 if (TREE_CODE (inside_init) == VECTOR_CST
6739 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6740 TYPE_MAIN_VARIANT (type)))
6741 return inside_init;
6742
6743 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6744 {
4038c495
GB
6745 unsigned HOST_WIDE_INT ix;
6746 tree value;
6747 bool constant_p = true;
e89be13b
JJ
6748
6749 /* Iterate through elements and check if all constructor
6750 elements are *_CSTs. */
4038c495
GB
6751 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6752 if (!CONSTANT_CLASS_P (value))
6753 {
6754 constant_p = false;
6755 break;
6756 }
e89be13b 6757
4038c495
GB
6758 if (constant_p)
6759 return build_vector_from_ctor (type,
6760 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6761 }
6762 }
6035d635 6763
ca085fd7
MLI
6764 if (warn_sequence_point)
6765 verify_sequence_points (inside_init);
6766
3e4093b6
RS
6767 /* Any type can be initialized
6768 from an expression of the same type, optionally with braces. */
400fbf9f 6769
3e4093b6
RS
6770 if (inside_init && TREE_TYPE (inside_init) != 0
6771 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6772 TYPE_MAIN_VARIANT (type))
3e4093b6 6773 || (code == ARRAY_TYPE
132da1a5 6774 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6775 || (code == VECTOR_TYPE
132da1a5 6776 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6777 || (code == POINTER_TYPE
3897f229 6778 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6779 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6780 TREE_TYPE (type)))))
3e4093b6
RS
6781 {
6782 if (code == POINTER_TYPE)
b494fd98 6783 {
b494fd98
EB
6784 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6785 {
f2a71bbc
JM
6786 if (TREE_CODE (inside_init) == STRING_CST
6787 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6788 inside_init = array_to_pointer_conversion
6789 (init_loc, inside_init);
f2a71bbc
JM
6790 else
6791 {
ea58ef42 6792 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6793 return error_mark_node;
6794 }
b494fd98 6795 }
f2a71bbc 6796 }
b494fd98 6797
bae39a73
NS
6798 if (code == VECTOR_TYPE)
6799 /* Although the types are compatible, we may require a
6800 conversion. */
6801 inside_init = convert (type, inside_init);
3e4093b6 6802
ca58211b 6803 if (require_constant
3e4093b6 6804 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6805 {
3e4093b6
RS
6806 /* As an extension, allow initializing objects with static storage
6807 duration with compound literals (which are then treated just as
ca58211b
PB
6808 the brace enclosed list they contain). Also allow this for
6809 vectors, as we can only assign them with compound literals. */
7278465e
MP
6810 if (flag_isoc99 && code != VECTOR_TYPE)
6811 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6812 "is not constant");
3e4093b6
RS
6813 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6814 inside_init = DECL_INITIAL (decl);
400fbf9f 6815 }
3e4093b6
RS
6816
6817 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6818 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6819 {
ea58ef42
MP
6820 error_init (init_loc, "array initialized from non-constant array "
6821 "expression");
3e4093b6 6822 return error_mark_node;
400fbf9f 6823 }
400fbf9f 6824
c1771a20 6825 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6826 -pedantic-errors is specified. In the later case, we always want
6827 an error. In the former case, we simply want a warning. */
6828 if (require_constant && pedantic
6829 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6830 {
6831 inside_init
6832 = valid_compound_expr_initializer (inside_init,
6833 TREE_TYPE (inside_init));
6834 if (inside_init == error_mark_node)
ea58ef42 6835 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6836 else
c1771a20 6837 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6838 "initializer element is not constant");
3e4093b6
RS
6839 if (flag_pedantic_errors)
6840 inside_init = error_mark_node;
6841 }
6842 else if (require_constant
116df786
RH
6843 && !initializer_constant_valid_p (inside_init,
6844 TREE_TYPE (inside_init)))
3e4093b6 6845 {
ea58ef42 6846 error_init (init_loc, "initializer element is not constant");
3e4093b6 6847 inside_init = error_mark_node;
8b40563c 6848 }
928c19bb 6849 else if (require_constant && !maybe_const)
3aa3c9fc 6850 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 6851 "initializer element is not a constant expression");
f735a153 6852
90137d8f 6853 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6854 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6855 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6856 type, inside_init, origtype,
bbbbb16a 6857 ic_init, null_pointer_constant,
928c19bb 6858 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6859 return inside_init;
6860 }
f735a153 6861
3e4093b6 6862 /* Handle scalar types, including conversions. */
400fbf9f 6863
ab22c1fa
CF
6864 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6865 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6866 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6867 {
f2a71bbc
JM
6868 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6869 && (TREE_CODE (init) == STRING_CST
6870 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6871 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6872 if (semantic_type)
6873 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6874 inside_init);
3e4093b6 6875 inside_init
68fca595
MP
6876 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6877 inside_init, origtype, ic_init,
6878 null_pointer_constant, NULL_TREE, NULL_TREE,
6879 0);
2f6e4e97 6880
3274deff
JW
6881 /* Check to see if we have already given an error message. */
6882 if (inside_init == error_mark_node)
6883 ;
3f75a254 6884 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6885 {
ea58ef42 6886 error_init (init_loc, "initializer element is not constant");
3e4093b6 6887 inside_init = error_mark_node;
400fbf9f 6888 }
3e4093b6 6889 else if (require_constant
116df786
RH
6890 && !initializer_constant_valid_p (inside_init,
6891 TREE_TYPE (inside_init)))
400fbf9f 6892 {
ea58ef42
MP
6893 error_init (init_loc, "initializer element is not computable at "
6894 "load time");
3e4093b6 6895 inside_init = error_mark_node;
400fbf9f 6896 }
928c19bb 6897 else if (require_constant && !maybe_const)
c2255bc4 6898 pedwarn_init (init_loc, 0,
928c19bb 6899 "initializer element is not a constant expression");
3e4093b6
RS
6900
6901 return inside_init;
400fbf9f 6902 }
d9fc6069 6903
3e4093b6 6904 /* Come here only for records and arrays. */
d9fc6069 6905
3e4093b6 6906 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6907 {
ea58ef42 6908 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6909 return error_mark_node;
d9fc6069 6910 }
3e4093b6 6911
ea58ef42 6912 error_init (init_loc, "invalid initializer");
3e4093b6 6913 return error_mark_node;
d9fc6069 6914}
400fbf9f 6915\f
3e4093b6 6916/* Handle initializers that use braces. */
400fbf9f 6917
3e4093b6
RS
6918/* Type of object we are accumulating a constructor for.
6919 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6920static tree constructor_type;
400fbf9f 6921
3e4093b6
RS
6922/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6923 left to fill. */
6924static tree constructor_fields;
400fbf9f 6925
3e4093b6
RS
6926/* For an ARRAY_TYPE, this is the specified index
6927 at which to store the next element we get. */
6928static tree constructor_index;
400fbf9f 6929
3e4093b6
RS
6930/* For an ARRAY_TYPE, this is the maximum index. */
6931static tree constructor_max_index;
400fbf9f 6932
3e4093b6
RS
6933/* For a RECORD_TYPE, this is the first field not yet written out. */
6934static tree constructor_unfilled_fields;
400fbf9f 6935
3e4093b6
RS
6936/* For an ARRAY_TYPE, this is the index of the first element
6937 not yet written out. */
6938static tree constructor_unfilled_index;
895ea614 6939
3e4093b6
RS
6940/* In a RECORD_TYPE, the byte index of the next consecutive field.
6941 This is so we can generate gaps between fields, when appropriate. */
6942static tree constructor_bit_index;
10d5caec 6943
3e4093b6
RS
6944/* If we are saving up the elements rather than allocating them,
6945 this is the list of elements so far (in reverse order,
6946 most recent first). */
9771b263 6947static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6948
3e4093b6
RS
6949/* 1 if constructor should be incrementally stored into a constructor chain,
6950 0 if all the elements should be kept in AVL tree. */
6951static int constructor_incremental;
ad47f1e5 6952
3e4093b6
RS
6953/* 1 if so far this constructor's elements are all compile-time constants. */
6954static int constructor_constant;
ad47f1e5 6955
3e4093b6
RS
6956/* 1 if so far this constructor's elements are all valid address constants. */
6957static int constructor_simple;
ad47f1e5 6958
928c19bb
JM
6959/* 1 if this constructor has an element that cannot be part of a
6960 constant expression. */
6961static int constructor_nonconst;
6962
3e4093b6
RS
6963/* 1 if this constructor is erroneous so far. */
6964static int constructor_erroneous;
d45cf215 6965
9bac5cbb
G
6966/* 1 if this constructor is the universal zero initializer { 0 }. */
6967static int constructor_zeroinit;
6968
3e4093b6
RS
6969/* Structure for managing pending initializer elements, organized as an
6970 AVL tree. */
d45cf215 6971
3e4093b6 6972struct init_node
d45cf215 6973{
3e4093b6
RS
6974 struct init_node *left, *right;
6975 struct init_node *parent;
6976 int balance;
6977 tree purpose;
6978 tree value;
bbbbb16a 6979 tree origtype;
d45cf215
RS
6980};
6981
3e4093b6
RS
6982/* Tree of pending elements at this constructor level.
6983 These are elements encountered out of order
6984 which belong at places we haven't reached yet in actually
6985 writing the output.
6986 Will never hold tree nodes across GC runs. */
6987static struct init_node *constructor_pending_elts;
d45cf215 6988
3e4093b6
RS
6989/* The SPELLING_DEPTH of this constructor. */
6990static int constructor_depth;
d45cf215 6991
3e4093b6
RS
6992/* DECL node for which an initializer is being read.
6993 0 means we are reading a constructor expression
6994 such as (struct foo) {...}. */
6995static tree constructor_decl;
d45cf215 6996
3e4093b6
RS
6997/* Nonzero if this is an initializer for a top-level decl. */
6998static int constructor_top_level;
d45cf215 6999
3e4093b6
RS
7000/* Nonzero if there were any member designators in this initializer. */
7001static int constructor_designated;
d45cf215 7002
3e4093b6
RS
7003/* Nesting depth of designator list. */
7004static int designator_depth;
d45cf215 7005
3e4093b6 7006/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7007static int designator_erroneous;
d45cf215 7008
3e4093b6
RS
7009\f
7010/* This stack has a level for each implicit or explicit level of
7011 structuring in the initializer, including the outermost one. It
7012 saves the values of most of the variables above. */
d45cf215 7013
3e4093b6
RS
7014struct constructor_range_stack;
7015
7016struct constructor_stack
d45cf215 7017{
3e4093b6
RS
7018 struct constructor_stack *next;
7019 tree type;
7020 tree fields;
7021 tree index;
7022 tree max_index;
7023 tree unfilled_index;
7024 tree unfilled_fields;
7025 tree bit_index;
9771b263 7026 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7027 struct init_node *pending_elts;
7028 int offset;
7029 int depth;
916c5919 7030 /* If value nonzero, this value should replace the entire
3e4093b6 7031 constructor at this level. */
916c5919 7032 struct c_expr replacement_value;
3e4093b6
RS
7033 struct constructor_range_stack *range_stack;
7034 char constant;
7035 char simple;
928c19bb 7036 char nonconst;
3e4093b6
RS
7037 char implicit;
7038 char erroneous;
7039 char outer;
7040 char incremental;
7041 char designated;
976d5a22 7042 int designator_depth;
3e4093b6 7043};
d45cf215 7044
802415d1 7045static struct constructor_stack *constructor_stack;
d45cf215 7046
3e4093b6
RS
7047/* This stack represents designators from some range designator up to
7048 the last designator in the list. */
d45cf215 7049
3e4093b6
RS
7050struct constructor_range_stack
7051{
7052 struct constructor_range_stack *next, *prev;
7053 struct constructor_stack *stack;
7054 tree range_start;
7055 tree index;
7056 tree range_end;
7057 tree fields;
7058};
d45cf215 7059
802415d1 7060static struct constructor_range_stack *constructor_range_stack;
d45cf215 7061
3e4093b6
RS
7062/* This stack records separate initializers that are nested.
7063 Nested initializers can't happen in ANSI C, but GNU C allows them
7064 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7065
3e4093b6 7066struct initializer_stack
d45cf215 7067{
3e4093b6
RS
7068 struct initializer_stack *next;
7069 tree decl;
3e4093b6
RS
7070 struct constructor_stack *constructor_stack;
7071 struct constructor_range_stack *constructor_range_stack;
9771b263 7072 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7073 struct spelling *spelling;
7074 struct spelling *spelling_base;
7075 int spelling_size;
7076 char top_level;
7077 char require_constant_value;
7078 char require_constant_elements;
7079};
d45cf215 7080
802415d1 7081static struct initializer_stack *initializer_stack;
3e4093b6
RS
7082\f
7083/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7084
7085void
a396f8ae 7086start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 7087{
3e4093b6 7088 const char *locus;
cceb1885 7089 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7090
3e4093b6 7091 p->decl = constructor_decl;
3e4093b6
RS
7092 p->require_constant_value = require_constant_value;
7093 p->require_constant_elements = require_constant_elements;
7094 p->constructor_stack = constructor_stack;
7095 p->constructor_range_stack = constructor_range_stack;
7096 p->elements = constructor_elements;
7097 p->spelling = spelling;
7098 p->spelling_base = spelling_base;
7099 p->spelling_size = spelling_size;
7100 p->top_level = constructor_top_level;
7101 p->next = initializer_stack;
7102 initializer_stack = p;
400fbf9f 7103
3e4093b6 7104 constructor_decl = decl;
3e4093b6
RS
7105 constructor_designated = 0;
7106 constructor_top_level = top_level;
400fbf9f 7107
6f17bbcf 7108 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7109 {
7110 require_constant_value = TREE_STATIC (decl);
7111 require_constant_elements
7112 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7113 /* For a scalar, you can always use any value to initialize,
7114 even within braces. */
296a8c2f 7115 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7116 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7117 }
7118 else
7119 {
7120 require_constant_value = 0;
7121 require_constant_elements = 0;
88388a52 7122 locus = _("(anonymous)");
3e4093b6 7123 }
b71c7f8a 7124
3e4093b6
RS
7125 constructor_stack = 0;
7126 constructor_range_stack = 0;
b71c7f8a 7127
9bac5cbb 7128 found_missing_braces = 0;
3e4093b6
RS
7129
7130 spelling_base = 0;
7131 spelling_size = 0;
7132 RESTORE_SPELLING_DEPTH (0);
7133
7134 if (locus)
7135 push_string (locus);
7136}
7137
7138void
7139finish_init (void)
b71c7f8a 7140{
3e4093b6 7141 struct initializer_stack *p = initializer_stack;
b71c7f8a 7142
3e4093b6
RS
7143 /* Free the whole constructor stack of this initializer. */
7144 while (constructor_stack)
7145 {
7146 struct constructor_stack *q = constructor_stack;
7147 constructor_stack = q->next;
7148 free (q);
7149 }
7150
366de0ce 7151 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7152
7153 /* Pop back to the data of the outer initializer (if any). */
36579663 7154 free (spelling_base);
3aeb3655 7155
3e4093b6 7156 constructor_decl = p->decl;
3e4093b6
RS
7157 require_constant_value = p->require_constant_value;
7158 require_constant_elements = p->require_constant_elements;
7159 constructor_stack = p->constructor_stack;
7160 constructor_range_stack = p->constructor_range_stack;
7161 constructor_elements = p->elements;
7162 spelling = p->spelling;
7163 spelling_base = p->spelling_base;
7164 spelling_size = p->spelling_size;
7165 constructor_top_level = p->top_level;
7166 initializer_stack = p->next;
7167 free (p);
b71c7f8a 7168}
400fbf9f 7169\f
3e4093b6
RS
7170/* Call here when we see the initializer is surrounded by braces.
7171 This is instead of a call to push_init_level;
7172 it is matched by a call to pop_init_level.
400fbf9f 7173
3e4093b6
RS
7174 TYPE is the type to initialize, for a constructor expression.
7175 For an initializer for a decl, TYPE is zero. */
400fbf9f 7176
3e4093b6
RS
7177void
7178really_start_incremental_init (tree type)
400fbf9f 7179{
5d038c4c 7180 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7181
3e4093b6
RS
7182 if (type == 0)
7183 type = TREE_TYPE (constructor_decl);
400fbf9f 7184
b6fc2cdb
PB
7185 if (TREE_CODE (type) == VECTOR_TYPE
7186 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7187 error ("opaque vector types cannot be initialized");
400fbf9f 7188
3e4093b6
RS
7189 p->type = constructor_type;
7190 p->fields = constructor_fields;
7191 p->index = constructor_index;
7192 p->max_index = constructor_max_index;
7193 p->unfilled_index = constructor_unfilled_index;
7194 p->unfilled_fields = constructor_unfilled_fields;
7195 p->bit_index = constructor_bit_index;
7196 p->elements = constructor_elements;
7197 p->constant = constructor_constant;
7198 p->simple = constructor_simple;
928c19bb 7199 p->nonconst = constructor_nonconst;
3e4093b6
RS
7200 p->erroneous = constructor_erroneous;
7201 p->pending_elts = constructor_pending_elts;
7202 p->depth = constructor_depth;
916c5919
JM
7203 p->replacement_value.value = 0;
7204 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7205 p->replacement_value.original_type = NULL;
3e4093b6
RS
7206 p->implicit = 0;
7207 p->range_stack = 0;
7208 p->outer = 0;
7209 p->incremental = constructor_incremental;
7210 p->designated = constructor_designated;
976d5a22 7211 p->designator_depth = designator_depth;
3e4093b6
RS
7212 p->next = 0;
7213 constructor_stack = p;
b13aca19 7214
3e4093b6
RS
7215 constructor_constant = 1;
7216 constructor_simple = 1;
928c19bb 7217 constructor_nonconst = 0;
3e4093b6 7218 constructor_depth = SPELLING_DEPTH ();
9771b263 7219 constructor_elements = NULL;
3e4093b6
RS
7220 constructor_pending_elts = 0;
7221 constructor_type = type;
7222 constructor_incremental = 1;
7223 constructor_designated = 0;
9bac5cbb 7224 constructor_zeroinit = 1;
3e4093b6 7225 designator_depth = 0;
b06df647 7226 designator_erroneous = 0;
400fbf9f 7227
3e4093b6
RS
7228 if (TREE_CODE (constructor_type) == RECORD_TYPE
7229 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7230 {
3e4093b6
RS
7231 constructor_fields = TYPE_FIELDS (constructor_type);
7232 /* Skip any nameless bit fields at the beginning. */
7233 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7234 && DECL_NAME (constructor_fields) == 0)
910ad8de 7235 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7236
3e4093b6
RS
7237 constructor_unfilled_fields = constructor_fields;
7238 constructor_bit_index = bitsize_zero_node;
400fbf9f 7239 }
3e4093b6
RS
7240 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7241 {
7242 if (TYPE_DOMAIN (constructor_type))
7243 {
7244 constructor_max_index
7245 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7246
3e4093b6
RS
7247 /* Detect non-empty initializations of zero-length arrays. */
7248 if (constructor_max_index == NULL_TREE
7249 && TYPE_SIZE (constructor_type))
9a9d280e 7250 constructor_max_index = integer_minus_one_node;
400fbf9f 7251
3e4093b6
RS
7252 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7253 to initialize VLAs will cause a proper error; avoid tree
7254 checking errors as well by setting a safe value. */
7255 if (constructor_max_index
7256 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7257 constructor_max_index = integer_minus_one_node;
59c83dbf 7258
3e4093b6
RS
7259 constructor_index
7260 = convert (bitsizetype,
7261 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7262 }
3e4093b6 7263 else
493179da
JM
7264 {
7265 constructor_index = bitsize_zero_node;
7266 constructor_max_index = NULL_TREE;
7267 }
59c83dbf 7268
3e4093b6
RS
7269 constructor_unfilled_index = constructor_index;
7270 }
7271 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7272 {
7273 /* Vectors are like simple fixed-size arrays. */
7274 constructor_max_index =
c62c040f 7275 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7276 constructor_index = bitsize_zero_node;
3e4093b6
RS
7277 constructor_unfilled_index = constructor_index;
7278 }
7279 else
7280 {
7281 /* Handle the case of int x = {5}; */
7282 constructor_fields = constructor_type;
7283 constructor_unfilled_fields = constructor_type;
7284 }
7285}
7286\f
7287/* Push down into a subobject, for initialization.
7288 If this is for an explicit set of braces, IMPLICIT is 0.
7289 If it is because the next element belongs at a lower level,
7290 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7291
3e4093b6 7292void
ea58ef42
MP
7293push_init_level (location_t loc, int implicit,
7294 struct obstack *braced_init_obstack)
3e4093b6
RS
7295{
7296 struct constructor_stack *p;
7297 tree value = NULL_TREE;
400fbf9f 7298
3e4093b6 7299 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7300 pop them now. If implicit == 1, this will have been done in
7301 process_init_element; do not repeat it here because in the case
7302 of excess initializers for an empty aggregate this leads to an
7303 infinite cycle of popping a level and immediately recreating
7304 it. */
7305 if (implicit != 1)
3e4093b6 7306 {
472d98b4
JM
7307 while (constructor_stack->implicit)
7308 {
7309 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7310 || TREE_CODE (constructor_type) == UNION_TYPE)
7311 && constructor_fields == 0)
34cf811f 7312 process_init_element (input_location,
ea58ef42 7313 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7314 true, braced_init_obstack);
472d98b4
JM
7315 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7316 && constructor_max_index
7317 && tree_int_cst_lt (constructor_max_index,
7318 constructor_index))
34cf811f 7319 process_init_element (input_location,
ea58ef42 7320 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7321 true, braced_init_obstack);
472d98b4
JM
7322 else
7323 break;
7324 }
3e4093b6 7325 }
400fbf9f 7326
3e4093b6
RS
7327 /* Unless this is an explicit brace, we need to preserve previous
7328 content if any. */
7329 if (implicit)
7330 {
7331 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7332 || TREE_CODE (constructor_type) == UNION_TYPE)
7333 && constructor_fields)
a1e3b3d9 7334 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7335 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7336 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7337 }
7338
5d038c4c 7339 p = XNEW (struct constructor_stack);
3e4093b6
RS
7340 p->type = constructor_type;
7341 p->fields = constructor_fields;
7342 p->index = constructor_index;
7343 p->max_index = constructor_max_index;
7344 p->unfilled_index = constructor_unfilled_index;
7345 p->unfilled_fields = constructor_unfilled_fields;
7346 p->bit_index = constructor_bit_index;
7347 p->elements = constructor_elements;
7348 p->constant = constructor_constant;
7349 p->simple = constructor_simple;
928c19bb 7350 p->nonconst = constructor_nonconst;
3e4093b6
RS
7351 p->erroneous = constructor_erroneous;
7352 p->pending_elts = constructor_pending_elts;
7353 p->depth = constructor_depth;
916c5919
JM
7354 p->replacement_value.value = 0;
7355 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7356 p->replacement_value.original_type = NULL;
3e4093b6
RS
7357 p->implicit = implicit;
7358 p->outer = 0;
7359 p->incremental = constructor_incremental;
7360 p->designated = constructor_designated;
976d5a22 7361 p->designator_depth = designator_depth;
3e4093b6
RS
7362 p->next = constructor_stack;
7363 p->range_stack = 0;
7364 constructor_stack = p;
400fbf9f 7365
3e4093b6
RS
7366 constructor_constant = 1;
7367 constructor_simple = 1;
928c19bb 7368 constructor_nonconst = 0;
3e4093b6 7369 constructor_depth = SPELLING_DEPTH ();
9771b263 7370 constructor_elements = NULL;
3e4093b6
RS
7371 constructor_incremental = 1;
7372 constructor_designated = 0;
7373 constructor_pending_elts = 0;
7374 if (!implicit)
400fbf9f 7375 {
3e4093b6
RS
7376 p->range_stack = constructor_range_stack;
7377 constructor_range_stack = 0;
7378 designator_depth = 0;
b06df647 7379 designator_erroneous = 0;
3e4093b6 7380 }
400fbf9f 7381
3e4093b6
RS
7382 /* Don't die if an entire brace-pair level is superfluous
7383 in the containing level. */
7384 if (constructor_type == 0)
7385 ;
7386 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7387 || TREE_CODE (constructor_type) == UNION_TYPE)
7388 {
7389 /* Don't die if there are extra init elts at the end. */
7390 if (constructor_fields == 0)
7391 constructor_type = 0;
7392 else
400fbf9f 7393 {
3e4093b6
RS
7394 constructor_type = TREE_TYPE (constructor_fields);
7395 push_member_name (constructor_fields);
7396 constructor_depth++;
400fbf9f 7397 }
6a358dcb
MP
7398 /* If upper initializer is designated, then mark this as
7399 designated too to prevent bogus warnings. */
7400 constructor_designated = p->designated;
3e4093b6
RS
7401 }
7402 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7403 {
7404 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7405 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7406 constructor_depth++;
400fbf9f
JW
7407 }
7408
3e4093b6 7409 if (constructor_type == 0)
400fbf9f 7410 {
ea58ef42 7411 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7412 constructor_fields = 0;
7413 constructor_unfilled_fields = 0;
7414 return;
400fbf9f
JW
7415 }
7416
3e4093b6
RS
7417 if (value && TREE_CODE (value) == CONSTRUCTOR)
7418 {
7419 constructor_constant = TREE_CONSTANT (value);
7420 constructor_simple = TREE_STATIC (value);
928c19bb 7421 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7422 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7423 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7424 && (TREE_CODE (constructor_type) == RECORD_TYPE
7425 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7426 set_nonincremental_init (braced_init_obstack);
3e4093b6 7427 }
400fbf9f 7428
9bac5cbb
G
7429 if (implicit == 1)
7430 found_missing_braces = 1;
400fbf9f 7431
3e4093b6
RS
7432 if (TREE_CODE (constructor_type) == RECORD_TYPE
7433 || TREE_CODE (constructor_type) == UNION_TYPE)
7434 {
7435 constructor_fields = TYPE_FIELDS (constructor_type);
7436 /* Skip any nameless bit fields at the beginning. */
7437 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7438 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7439 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7440
3e4093b6
RS
7441 constructor_unfilled_fields = constructor_fields;
7442 constructor_bit_index = bitsize_zero_node;
7443 }
7444 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7445 {
7446 /* Vectors are like simple fixed-size arrays. */
7447 constructor_max_index =
c62c040f
RG
7448 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7449 constructor_index = bitsize_int (0);
3e4093b6
RS
7450 constructor_unfilled_index = constructor_index;
7451 }
7452 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7453 {
7454 if (TYPE_DOMAIN (constructor_type))
7455 {
7456 constructor_max_index
7457 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7458
3e4093b6
RS
7459 /* Detect non-empty initializations of zero-length arrays. */
7460 if (constructor_max_index == NULL_TREE
7461 && TYPE_SIZE (constructor_type))
9a9d280e 7462 constructor_max_index = integer_minus_one_node;
de520661 7463
3e4093b6
RS
7464 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7465 to initialize VLAs will cause a proper error; avoid tree
7466 checking errors as well by setting a safe value. */
7467 if (constructor_max_index
7468 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7469 constructor_max_index = integer_minus_one_node;
b62acd60 7470
3e4093b6
RS
7471 constructor_index
7472 = convert (bitsizetype,
7473 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7474 }
7475 else
7476 constructor_index = bitsize_zero_node;
de520661 7477
3e4093b6
RS
7478 constructor_unfilled_index = constructor_index;
7479 if (value && TREE_CODE (value) == STRING_CST)
7480 {
7481 /* We need to split the char/wchar array into individual
7482 characters, so that we don't have to special case it
7483 everywhere. */
a1e3b3d9 7484 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7485 }
7486 }
7487 else
7488 {
b4519d39 7489 if (constructor_type != error_mark_node)
96b40f8d 7490 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7491 constructor_fields = constructor_type;
7492 constructor_unfilled_fields = constructor_type;
7493 }
7494}
8b6a5902 7495
3e4093b6 7496/* At the end of an implicit or explicit brace level,
916c5919
JM
7497 finish up that level of constructor. If a single expression
7498 with redundant braces initialized that level, return the
7499 c_expr structure for that expression. Otherwise, the original_code
7500 element is set to ERROR_MARK.
7501 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7502 from inner levels (process_init_element ignores that),
916c5919 7503 but return error_mark_node as the value from the outermost level
3e4093b6 7504 (that's what we want to put in DECL_INITIAL).
916c5919 7505 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7506
916c5919 7507struct c_expr
ea58ef42
MP
7508pop_init_level (location_t loc, int implicit,
7509 struct obstack *braced_init_obstack)
3e4093b6
RS
7510{
7511 struct constructor_stack *p;
916c5919
JM
7512 struct c_expr ret;
7513 ret.value = 0;
7514 ret.original_code = ERROR_MARK;
6866c6e8 7515 ret.original_type = NULL;
de520661 7516
3e4093b6
RS
7517 if (implicit == 0)
7518 {
7519 /* When we come to an explicit close brace,
7520 pop any inner levels that didn't have explicit braces. */
7521 while (constructor_stack->implicit)
34cf811f 7522 process_init_element (input_location,
ea58ef42 7523 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7524 true, braced_init_obstack);
366de0ce 7525 gcc_assert (!constructor_range_stack);
3e4093b6 7526 }
e5e809f4 7527
0066ef9c
RH
7528 /* Now output all pending elements. */
7529 constructor_incremental = 1;
a1e3b3d9 7530 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7531
3e4093b6 7532 p = constructor_stack;
e5e809f4 7533
3e4093b6
RS
7534 /* Error for initializing a flexible array member, or a zero-length
7535 array member in an inappropriate context. */
7536 if (constructor_type && constructor_fields
7537 && TREE_CODE (constructor_type) == ARRAY_TYPE
7538 && TYPE_DOMAIN (constructor_type)
3f75a254 7539 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7540 {
7541 /* Silently discard empty initializations. The parser will
7542 already have pedwarned for empty brackets. */
7543 if (integer_zerop (constructor_unfilled_index))
7544 constructor_type = NULL_TREE;
366de0ce 7545 else
3e4093b6 7546 {
366de0ce 7547 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7548
3e4093b6 7549 if (constructor_depth > 2)
ea58ef42 7550 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7551 else
d033409e 7552 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7553 "initialization of a flexible array member");
de520661 7554
3e4093b6
RS
7555 /* We have already issued an error message for the existence
7556 of a flexible array member not at the end of the structure.
535a42b1 7557 Discard the initializer so that we do not die later. */
910ad8de 7558 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7559 constructor_type = NULL_TREE;
7560 }
3e4093b6 7561 }
de520661 7562
7b33f0c8
MP
7563 switch (vec_safe_length (constructor_elements))
7564 {
7565 case 0:
7566 /* Initialization with { } counts as zeroinit. */
7567 constructor_zeroinit = 1;
7568 break;
7569 case 1:
7570 /* This might be zeroinit as well. */
7571 if (integer_zerop ((*constructor_elements)[0].value))
7572 constructor_zeroinit = 1;
7573 break;
7574 default:
7575 /* If the constructor has more than one element, it can't be { 0 }. */
7576 constructor_zeroinit = 0;
7577 break;
7578 }
9bac5cbb
G
7579
7580 /* Warn when some structs are initialized with direct aggregation. */
7581 if (!implicit && found_missing_braces && warn_missing_braces
7582 && !constructor_zeroinit)
7b33f0c8
MP
7583 warning_init (loc, OPT_Wmissing_braces,
7584 "missing braces around initializer");
9bac5cbb 7585
3e4093b6 7586 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7587 if (warn_missing_field_initializers
3e4093b6
RS
7588 && constructor_type
7589 && TREE_CODE (constructor_type) == RECORD_TYPE
7590 && constructor_unfilled_fields)
7591 {
7592 /* Do not warn for flexible array members or zero-length arrays. */
7593 while (constructor_unfilled_fields
3f75a254 7594 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7595 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7596 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7597
49819fef
AM
7598 if (constructor_unfilled_fields
7599 /* Do not warn if this level of the initializer uses member
7600 designators; it is likely to be deliberate. */
7601 && !constructor_designated
84937de2 7602 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7603 && !constructor_zeroinit)
3e4093b6 7604 {
32397f22
MLI
7605 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7606 "missing initializer for field %qD of %qT",
7607 constructor_unfilled_fields,
7608 constructor_type))
7609 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7610 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7611 }
7612 }
de520661 7613
3e4093b6 7614 /* Pad out the end of the structure. */
916c5919 7615 if (p->replacement_value.value)
3e4093b6
RS
7616 /* If this closes a superfluous brace pair,
7617 just pass out the element between them. */
916c5919 7618 ret = p->replacement_value;
3e4093b6
RS
7619 else if (constructor_type == 0)
7620 ;
7621 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7622 && TREE_CODE (constructor_type) != UNION_TYPE
7623 && TREE_CODE (constructor_type) != ARRAY_TYPE
7624 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7625 {
7626 /* A nonincremental scalar initializer--just return
7627 the element, after verifying there is just one. */
9771b263 7628 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7629 {
7630 if (!constructor_erroneous)
ea58ef42 7631 error_init (loc, "empty scalar initializer");
916c5919 7632 ret.value = error_mark_node;
3e4093b6 7633 }
9771b263 7634 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7635 {
ea58ef42 7636 error_init (loc, "extra elements in scalar initializer");
9771b263 7637 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7638 }
7639 else
9771b263 7640 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7641 }
7642 else
7643 {
7644 if (constructor_erroneous)
916c5919 7645 ret.value = error_mark_node;
3e4093b6
RS
7646 else
7647 {
916c5919 7648 ret.value = build_constructor (constructor_type,
4038c495 7649 constructor_elements);
3e4093b6 7650 if (constructor_constant)
51eed280 7651 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7652 if (constructor_constant && constructor_simple)
916c5919 7653 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7654 if (constructor_nonconst)
7655 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7656 }
7657 }
de520661 7658
928c19bb
JM
7659 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7660 {
7661 if (constructor_nonconst)
7662 ret.original_code = C_MAYBE_CONST_EXPR;
7663 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7664 ret.original_code = ERROR_MARK;
7665 }
7666
3e4093b6
RS
7667 constructor_type = p->type;
7668 constructor_fields = p->fields;
7669 constructor_index = p->index;
7670 constructor_max_index = p->max_index;
7671 constructor_unfilled_index = p->unfilled_index;
7672 constructor_unfilled_fields = p->unfilled_fields;
7673 constructor_bit_index = p->bit_index;
7674 constructor_elements = p->elements;
7675 constructor_constant = p->constant;
7676 constructor_simple = p->simple;
928c19bb 7677 constructor_nonconst = p->nonconst;
3e4093b6
RS
7678 constructor_erroneous = p->erroneous;
7679 constructor_incremental = p->incremental;
7680 constructor_designated = p->designated;
976d5a22 7681 designator_depth = p->designator_depth;
3e4093b6
RS
7682 constructor_pending_elts = p->pending_elts;
7683 constructor_depth = p->depth;
7684 if (!p->implicit)
7685 constructor_range_stack = p->range_stack;
7686 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7687
3e4093b6
RS
7688 constructor_stack = p->next;
7689 free (p);
b621a4dd 7690
5d5e98dc
VR
7691 if (ret.value == 0 && constructor_stack == 0)
7692 ret.value = error_mark_node;
916c5919 7693 return ret;
3e4093b6 7694}
8b6a5902 7695
3e4093b6
RS
7696/* Common handling for both array range and field name designators.
7697 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7698
3e4093b6 7699static int
ea58ef42
MP
7700set_designator (location_t loc, int array,
7701 struct obstack *braced_init_obstack)
de520661 7702{
3e4093b6
RS
7703 tree subtype;
7704 enum tree_code subcode;
de520661 7705
3e4093b6
RS
7706 /* Don't die if an entire brace-pair level is superfluous
7707 in the containing level. */
7708 if (constructor_type == 0)
7709 return 1;
de520661 7710
366de0ce
NS
7711 /* If there were errors in this designator list already, bail out
7712 silently. */
b06df647 7713 if (designator_erroneous)
3e4093b6 7714 return 1;
e28cae4f 7715
3e4093b6
RS
7716 if (!designator_depth)
7717 {
366de0ce 7718 gcc_assert (!constructor_range_stack);
de520661 7719
3e4093b6
RS
7720 /* Designator list starts at the level of closest explicit
7721 braces. */
7722 while (constructor_stack->implicit)
34cf811f 7723 process_init_element (input_location,
ea58ef42 7724 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7725 true, braced_init_obstack);
3e4093b6
RS
7726 constructor_designated = 1;
7727 return 0;
7728 }
de520661 7729
366de0ce 7730 switch (TREE_CODE (constructor_type))
3c3fa147 7731 {
366de0ce
NS
7732 case RECORD_TYPE:
7733 case UNION_TYPE:
3e4093b6
RS
7734 subtype = TREE_TYPE (constructor_fields);
7735 if (subtype != error_mark_node)
7736 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7737 break;
7738 case ARRAY_TYPE:
3e4093b6 7739 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7740 break;
7741 default:
7742 gcc_unreachable ();
de520661 7743 }
400fbf9f 7744
3e4093b6
RS
7745 subcode = TREE_CODE (subtype);
7746 if (array && subcode != ARRAY_TYPE)
7747 {
ea58ef42 7748 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7749 return 1;
7750 }
7751 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7752 {
ea58ef42 7753 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7754 return 1;
7755 }
d45cf215 7756
3e4093b6 7757 constructor_designated = 1;
ea58ef42 7758 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7759 return 0;
de520661 7760}
400fbf9f 7761
3e4093b6
RS
7762/* If there are range designators in designator list, push a new designator
7763 to constructor_range_stack. RANGE_END is end of such stack range or
7764 NULL_TREE if there is no range designator at this level. */
400fbf9f 7765
3e4093b6 7766static void
a1e3b3d9 7767push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7768{
7769 struct constructor_range_stack *p;
400fbf9f 7770
a1e3b3d9
LB
7771 p = (struct constructor_range_stack *)
7772 obstack_alloc (braced_init_obstack,
7773 sizeof (struct constructor_range_stack));
3e4093b6
RS
7774 p->prev = constructor_range_stack;
7775 p->next = 0;
7776 p->fields = constructor_fields;
7777 p->range_start = constructor_index;
7778 p->index = constructor_index;
7779 p->stack = constructor_stack;
7780 p->range_end = range_end;
8b6a5902 7781 if (constructor_range_stack)
3e4093b6
RS
7782 constructor_range_stack->next = p;
7783 constructor_range_stack = p;
de520661 7784}
400fbf9f 7785
3e4093b6
RS
7786/* Within an array initializer, specify the next index to be initialized.
7787 FIRST is that index. If LAST is nonzero, then initialize a range
7788 of indices, running from FIRST through LAST. */
5a7ec9d9 7789
de520661 7790void
ea58ef42 7791set_init_index (location_t loc, tree first, tree last,
d033409e 7792 struct obstack *braced_init_obstack)
de520661 7793{
ea58ef42 7794 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7795 return;
de520661 7796
b06df647 7797 designator_erroneous = 1;
de520661 7798
3ea8cd06
JM
7799 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7800 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7801 {
ea58ef42 7802 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7803 return;
7804 }
7805
2b6da65c
JM
7806 if (TREE_CODE (first) != INTEGER_CST)
7807 {
7808 first = c_fully_fold (first, false, NULL);
7809 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7810 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7811 "array index in initializer is not "
7812 "an integer constant expression");
7813 }
7814
7815 if (last && TREE_CODE (last) != INTEGER_CST)
7816 {
7817 last = c_fully_fold (last, false, NULL);
7818 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7819 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7820 "array index in initializer is not "
7821 "an integer constant expression");
7822 }
7823
3e4093b6 7824 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7825 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7826 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7827 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7828 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7829 error_init (loc, "array index in non-array initializer");
622adc7e 7830 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7831 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7832 else if (constructor_max_index
7833 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7834 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7835 else
de520661 7836 {
928c19bb
JM
7837 constant_expression_warning (first);
7838 if (last)
7839 constant_expression_warning (last);
3e4093b6 7840 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7841 if (tree_int_cst_lt (constructor_index, first))
7842 {
7843 constructor_index = copy_node (constructor_index);
7844 TREE_OVERFLOW (constructor_index) = 1;
7845 }
665f2503 7846
3e4093b6 7847 if (last)
2bede729 7848 {
3e4093b6
RS
7849 if (tree_int_cst_equal (first, last))
7850 last = 0;
7851 else if (tree_int_cst_lt (last, first))
7852 {
ea58ef42 7853 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7854 last = 0;
7855 }
7856 else
7857 {
7858 last = convert (bitsizetype, last);
7859 if (constructor_max_index != 0
7860 && tree_int_cst_lt (constructor_max_index, last))
7861 {
ea58ef42
MP
7862 error_init (loc, "array index range in initializer exceeds "
7863 "array bounds");
3e4093b6
RS
7864 last = 0;
7865 }
7866 }
2bede729 7867 }
fed3cef0 7868
3e4093b6 7869 designator_depth++;
b06df647 7870 designator_erroneous = 0;
3e4093b6 7871 if (constructor_range_stack || last)
a1e3b3d9 7872 push_range_stack (last, braced_init_obstack);
de520661 7873 }
de520661 7874}
3e4093b6
RS
7875
7876/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7877
de520661 7878void
ea58ef42
MP
7879set_init_label (location_t loc, tree fieldname,
7880 struct obstack *braced_init_obstack)
de520661 7881{
0fb96aa4 7882 tree field;
94ba5069 7883
ea58ef42 7884 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7885 return;
7886
b06df647 7887 designator_erroneous = 1;
3e4093b6
RS
7888
7889 if (TREE_CODE (constructor_type) != RECORD_TYPE
7890 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7891 {
ea58ef42 7892 error_init (loc, "field name not in record or union initializer");
3e4093b6 7893 return;
94ba5069
RS
7894 }
7895
0fb96aa4 7896 field = lookup_field (constructor_type, fieldname);
8b6a5902 7897
0fb96aa4 7898 if (field == 0)
9babc352 7899 error_at (loc, "unknown field %qE specified in initializer", fieldname);
3e4093b6 7900 else
0fb96aa4
JM
7901 do
7902 {
7903 constructor_fields = TREE_VALUE (field);
7904 designator_depth++;
7905 designator_erroneous = 0;
7906 if (constructor_range_stack)
7907 push_range_stack (NULL_TREE, braced_init_obstack);
7908 field = TREE_CHAIN (field);
7909 if (field)
7910 {
ea58ef42 7911 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7912 return;
7913 }
7914 }
7915 while (field != NULL_TREE);
3e4093b6
RS
7916}
7917\f
7918/* Add a new initializer to the tree of pending initializers. PURPOSE
7919 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7920 VALUE is the value of that index or field. If ORIGTYPE is not
7921 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7922
7923 IMPLICIT is true if value comes from pop_init_level (1),
7924 the new initializer has been merged with the existing one
7925 and thus no warnings should be emitted about overriding an
7926 existing initializer. */
de520661 7927
3e4093b6 7928static void
96b40f8d
MP
7929add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7930 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7931{
7932 struct init_node *p, **q, *r;
7933
7934 q = &constructor_pending_elts;
7935 p = 0;
7936
7937 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7938 {
3e4093b6 7939 while (*q != 0)
91fa3c30 7940 {
3e4093b6
RS
7941 p = *q;
7942 if (tree_int_cst_lt (purpose, p->purpose))
7943 q = &p->left;
7944 else if (tree_int_cst_lt (p->purpose, purpose))
7945 q = &p->right;
7946 else
7947 {
b295aee2
JJ
7948 if (!implicit)
7949 {
7950 if (TREE_SIDE_EFFECTS (p->value))
755e528f 7951 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
7952 "initialized field with side-effects "
7953 "overwritten");
b295aee2 7954 else if (warn_override_init)
96b40f8d
MP
7955 warning_init (loc, OPT_Woverride_init,
7956 "initialized field overwritten");
b295aee2 7957 }
3e4093b6 7958 p->value = value;
bbbbb16a 7959 p->origtype = origtype;
3e4093b6
RS
7960 return;
7961 }
91fa3c30 7962 }
de520661 7963 }
3e4093b6 7964 else
de520661 7965 {
3e4093b6 7966 tree bitpos;
400fbf9f 7967
3e4093b6
RS
7968 bitpos = bit_position (purpose);
7969 while (*q != NULL)
7970 {
7971 p = *q;
7972 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7973 q = &p->left;
7974 else if (p->purpose != purpose)
7975 q = &p->right;
7976 else
7977 {
b295aee2
JJ
7978 if (!implicit)
7979 {
7980 if (TREE_SIDE_EFFECTS (p->value))
755e528f 7981 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
7982 "initialized field with side-effects "
7983 "overwritten");
b295aee2 7984 else if (warn_override_init)
96b40f8d
MP
7985 warning_init (loc, OPT_Woverride_init,
7986 "initialized field overwritten");
b295aee2 7987 }
3e4093b6 7988 p->value = value;
bbbbb16a 7989 p->origtype = origtype;
3e4093b6
RS
7990 return;
7991 }
7992 }
91fa3c30 7993 }
b71c7f8a 7994
a1e3b3d9
LB
7995 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7996 sizeof (struct init_node));
3e4093b6
RS
7997 r->purpose = purpose;
7998 r->value = value;
bbbbb16a 7999 r->origtype = origtype;
8b6a5902 8000
3e4093b6
RS
8001 *q = r;
8002 r->parent = p;
8003 r->left = 0;
8004 r->right = 0;
8005 r->balance = 0;
b71c7f8a 8006
3e4093b6 8007 while (p)
de520661 8008 {
3e4093b6 8009 struct init_node *s;
665f2503 8010
3e4093b6 8011 if (r == p->left)
2bede729 8012 {
3e4093b6
RS
8013 if (p->balance == 0)
8014 p->balance = -1;
8015 else if (p->balance < 0)
8016 {
8017 if (r->balance < 0)
8018 {
8019 /* L rotation. */
8020 p->left = r->right;
8021 if (p->left)
8022 p->left->parent = p;
8023 r->right = p;
e7b6a0ee 8024
3e4093b6
RS
8025 p->balance = 0;
8026 r->balance = 0;
39bc99c2 8027
3e4093b6
RS
8028 s = p->parent;
8029 p->parent = r;
8030 r->parent = s;
8031 if (s)
8032 {
8033 if (s->left == p)
8034 s->left = r;
8035 else
8036 s->right = r;
8037 }
8038 else
8039 constructor_pending_elts = r;
8040 }
8041 else
8042 {
8043 /* LR rotation. */
8044 struct init_node *t = r->right;
e7b6a0ee 8045
3e4093b6
RS
8046 r->right = t->left;
8047 if (r->right)
8048 r->right->parent = r;
8049 t->left = r;
8050
8051 p->left = t->right;
8052 if (p->left)
8053 p->left->parent = p;
8054 t->right = p;
8055
8056 p->balance = t->balance < 0;
8057 r->balance = -(t->balance > 0);
8058 t->balance = 0;
8059
8060 s = p->parent;
8061 p->parent = t;
8062 r->parent = t;
8063 t->parent = s;
8064 if (s)
8065 {
8066 if (s->left == p)
8067 s->left = t;
8068 else
8069 s->right = t;
8070 }
8071 else
8072 constructor_pending_elts = t;
8073 }
8074 break;
8075 }
8076 else
8077 {
8078 /* p->balance == +1; growth of left side balances the node. */
8079 p->balance = 0;
8080 break;
8081 }
2bede729 8082 }
3e4093b6
RS
8083 else /* r == p->right */
8084 {
8085 if (p->balance == 0)
8086 /* Growth propagation from right side. */
8087 p->balance++;
8088 else if (p->balance > 0)
8089 {
8090 if (r->balance > 0)
8091 {
8092 /* R rotation. */
8093 p->right = r->left;
8094 if (p->right)
8095 p->right->parent = p;
8096 r->left = p;
8097
8098 p->balance = 0;
8099 r->balance = 0;
8100
8101 s = p->parent;
8102 p->parent = r;
8103 r->parent = s;
8104 if (s)
8105 {
8106 if (s->left == p)
8107 s->left = r;
8108 else
8109 s->right = r;
8110 }
8111 else
8112 constructor_pending_elts = r;
8113 }
8114 else /* r->balance == -1 */
8115 {
8116 /* RL rotation */
8117 struct init_node *t = r->left;
8118
8119 r->left = t->right;
8120 if (r->left)
8121 r->left->parent = r;
8122 t->right = r;
8123
8124 p->right = t->left;
8125 if (p->right)
8126 p->right->parent = p;
8127 t->left = p;
8128
8129 r->balance = (t->balance < 0);
8130 p->balance = -(t->balance > 0);
8131 t->balance = 0;
8132
8133 s = p->parent;
8134 p->parent = t;
8135 r->parent = t;
8136 t->parent = s;
8137 if (s)
8138 {
8139 if (s->left == p)
8140 s->left = t;
8141 else
8142 s->right = t;
8143 }
8144 else
8145 constructor_pending_elts = t;
8146 }
8147 break;
8148 }
8149 else
8150 {
8151 /* p->balance == -1; growth of right side balances the node. */
8152 p->balance = 0;
8153 break;
8154 }
8155 }
8156
8157 r = p;
8158 p = p->parent;
8159 }
8160}
8161
8162/* Build AVL tree from a sorted chain. */
8163
8164static void
a1e3b3d9 8165set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8166{
4038c495
GB
8167 unsigned HOST_WIDE_INT ix;
8168 tree index, value;
3e4093b6
RS
8169
8170 if (TREE_CODE (constructor_type) != RECORD_TYPE
8171 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8172 return;
8173
4038c495 8174 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8175 add_pending_init (input_location, index, value, NULL_TREE, true,
8176 braced_init_obstack);
9771b263 8177 constructor_elements = NULL;
3e4093b6
RS
8178 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8179 {
8180 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8181 /* Skip any nameless bit fields at the beginning. */
8182 while (constructor_unfilled_fields != 0
8183 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8184 && DECL_NAME (constructor_unfilled_fields) == 0)
8185 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8186
de520661 8187 }
3e4093b6 8188 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8189 {
3e4093b6
RS
8190 if (TYPE_DOMAIN (constructor_type))
8191 constructor_unfilled_index
8192 = convert (bitsizetype,
8193 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8194 else
8195 constructor_unfilled_index = bitsize_zero_node;
de520661 8196 }
3e4093b6 8197 constructor_incremental = 0;
de520661 8198}
400fbf9f 8199
3e4093b6 8200/* Build AVL tree from a string constant. */
de520661 8201
3e4093b6 8202static void
a1e3b3d9
LB
8203set_nonincremental_init_from_string (tree str,
8204 struct obstack * braced_init_obstack)
de520661 8205{
3e4093b6
RS
8206 tree value, purpose, type;
8207 HOST_WIDE_INT val[2];
8208 const char *p, *end;
8209 int byte, wchar_bytes, charwidth, bitpos;
de520661 8210
366de0ce 8211 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8212
c466b2cd 8213 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8214 charwidth = TYPE_PRECISION (char_type_node);
8215 type = TREE_TYPE (constructor_type);
8216 p = TREE_STRING_POINTER (str);
8217 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8218
3e4093b6 8219 for (purpose = bitsize_zero_node;
8824edff
JJ
8220 p < end
8221 && !(constructor_max_index
8222 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8223 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8224 {
3e4093b6 8225 if (wchar_bytes == 1)
ffc5c6a9 8226 {
807e902e
KZ
8227 val[0] = (unsigned char) *p++;
8228 val[1] = 0;
ffc5c6a9
RH
8229 }
8230 else
3e4093b6 8231 {
3e4093b6 8232 val[1] = 0;
807e902e 8233 val[0] = 0;
3e4093b6
RS
8234 for (byte = 0; byte < wchar_bytes; byte++)
8235 {
8236 if (BYTES_BIG_ENDIAN)
8237 bitpos = (wchar_bytes - byte - 1) * charwidth;
8238 else
8239 bitpos = byte * charwidth;
807e902e 8240 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8241 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8242 << (bitpos % HOST_BITS_PER_WIDE_INT);
8243 }
8244 }
584ef5fe 8245
8df83eae 8246 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8247 {
8248 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8249 if (bitpos < HOST_BITS_PER_WIDE_INT)
8250 {
807e902e 8251 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8252 {
807e902e
KZ
8253 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8254 val[1] = -1;
3e4093b6
RS
8255 }
8256 }
8257 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8258 {
807e902e
KZ
8259 if (val[0] < 0)
8260 val[1] = -1;
3e4093b6 8261 }
807e902e 8262 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8263 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8264 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8265 << (bitpos - HOST_BITS_PER_WIDE_INT);
8266 }
ffc5c6a9 8267
807e902e
KZ
8268 value = wide_int_to_tree (type,
8269 wide_int::from_array (val, 2,
8270 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8271 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8272 braced_init_obstack);
9dfcc8db
BH
8273 }
8274
3e4093b6
RS
8275 constructor_incremental = 0;
8276}
de520661 8277
3e4093b6
RS
8278/* Return value of FIELD in pending initializer or zero if the field was
8279 not initialized yet. */
8280
8281static tree
a1e3b3d9 8282find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8283{
8284 struct init_node *p;
8285
8286 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8287 {
3e4093b6
RS
8288 if (constructor_incremental
8289 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8290 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8291
8292 p = constructor_pending_elts;
8293 while (p)
19d76e60 8294 {
3e4093b6
RS
8295 if (tree_int_cst_lt (field, p->purpose))
8296 p = p->left;
8297 else if (tree_int_cst_lt (p->purpose, field))
8298 p = p->right;
8299 else
8300 return p->value;
19d76e60 8301 }
19d76e60 8302 }
3e4093b6 8303 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8304 {
3e4093b6 8305 tree bitpos = bit_position (field);
de520661 8306
3e4093b6
RS
8307 if (constructor_incremental
8308 && (!constructor_unfilled_fields
8309 || tree_int_cst_lt (bitpos,
8310 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8311 set_nonincremental_init (braced_init_obstack);
de520661 8312
3e4093b6
RS
8313 p = constructor_pending_elts;
8314 while (p)
8315 {
8316 if (field == p->purpose)
8317 return p->value;
8318 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8319 p = p->left;
8320 else
8321 p = p->right;
8322 }
8323 }
8324 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8325 {
9771b263
DN
8326 if (!vec_safe_is_empty (constructor_elements)
8327 && (constructor_elements->last ().index == field))
8328 return constructor_elements->last ().value;
de520661 8329 }
3e4093b6 8330 return 0;
de520661
RS
8331}
8332
3e4093b6
RS
8333/* "Output" the next constructor element.
8334 At top level, really output it to assembler code now.
8335 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8336 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8337 TYPE is the data type that the containing data type wants here.
8338 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8339 If VALUE is a string constant, STRICT_STRING is true if it is
8340 unparenthesized or we should not warn here for it being parenthesized.
8341 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8342
3e4093b6
RS
8343 PENDING if non-nil means output pending elements that belong
8344 right after this element. (PENDING is normally 1;
b295aee2
JJ
8345 it is 0 while outputting pending elements, to avoid recursion.)
8346
8347 IMPLICIT is true if value comes from pop_init_level (1),
8348 the new initializer has been merged with the existing one
8349 and thus no warnings should be emitted about overriding an
8350 existing initializer. */
8b6a5902 8351
3e4093b6 8352static void
34cf811f
MP
8353output_init_element (location_t loc, tree value, tree origtype,
8354 bool strict_string, tree type, tree field, int pending,
8355 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8356{
8ce94e44 8357 tree semantic_type = NULL_TREE;
928c19bb
JM
8358 bool maybe_const = true;
8359 bool npc;
4038c495 8360
0a880880 8361 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8362 {
3e4093b6
RS
8363 constructor_erroneous = 1;
8364 return;
8b6a5902 8365 }
46bdb9cf
JM
8366 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8367 && (TREE_CODE (value) == STRING_CST
8368 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8369 && !(TREE_CODE (value) == STRING_CST
8370 && TREE_CODE (type) == ARRAY_TYPE
8371 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8372 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8373 TYPE_MAIN_VARIANT (type)))
c2255bc4 8374 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8375
3e4093b6 8376 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8377 && require_constant_value && pending)
8b6a5902 8378 {
3e4093b6
RS
8379 /* As an extension, allow initializing objects with static storage
8380 duration with compound literals (which are then treated just as
8381 the brace enclosed list they contain). */
4435bb92
MP
8382 if (flag_isoc99)
8383 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8384 "constant");
3e4093b6
RS
8385 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8386 value = DECL_INITIAL (decl);
8b6a5902
JJ
8387 }
8388
928c19bb 8389 npc = null_pointer_constant_p (value);
8ce94e44
JM
8390 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8391 {
8392 semantic_type = TREE_TYPE (value);
8393 value = TREE_OPERAND (value, 0);
8394 }
928c19bb
JM
8395 value = c_fully_fold (value, require_constant_value, &maybe_const);
8396
3e4093b6
RS
8397 if (value == error_mark_node)
8398 constructor_erroneous = 1;
8399 else if (!TREE_CONSTANT (value))
8400 constructor_constant = 0;
116df786 8401 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8402 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8403 || TREE_CODE (constructor_type) == UNION_TYPE)
8404 && DECL_C_BIT_FIELD (field)
8405 && TREE_CODE (value) != INTEGER_CST))
8406 constructor_simple = 0;
928c19bb
JM
8407 if (!maybe_const)
8408 constructor_nonconst = 1;
3e4093b6 8409
116df786 8410 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8411 {
116df786
RH
8412 if (require_constant_value)
8413 {
ea58ef42 8414 error_init (loc, "initializer element is not constant");
116df786
RH
8415 value = error_mark_node;
8416 }
8417 else if (require_constant_elements)
8337d1db 8418 pedwarn (loc, OPT_Wpedantic,
509c9d60 8419 "initializer element is not computable at load time");
8b6a5902 8420 }
928c19bb
JM
8421 else if (!maybe_const
8422 && (require_constant_value || require_constant_elements))
8337d1db 8423 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8424 "initializer element is not a constant expression");
3e4093b6 8425
81f40b79
ILT
8426 /* Issue -Wc++-compat warnings about initializing a bitfield with
8427 enum type. */
8428 if (warn_cxx_compat
8429 && field != NULL_TREE
8430 && TREE_CODE (field) == FIELD_DECL
8431 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8432 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8433 != TYPE_MAIN_VARIANT (type))
8434 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8435 {
8436 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8437 if (checktype != error_mark_node
8438 && (TYPE_MAIN_VARIANT (checktype)
8439 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8440 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8441 "enum conversion in initialization is invalid in C++");
8442 }
8443
3e4093b6
RS
8444 /* If this field is empty (and not at the end of structure),
8445 don't do anything other than checking the initializer. */
8446 if (field
8447 && (TREE_TYPE (field) == error_mark_node
8448 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8449 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8450 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8451 || DECL_CHAIN (field)))))
3e4093b6
RS
8452 return;
8453
8ce94e44
JM
8454 if (semantic_type)
8455 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8456 value = digest_init (loc, type, value, origtype, npc, strict_string,
8457 require_constant_value);
3e4093b6 8458 if (value == error_mark_node)
8b6a5902 8459 {
3e4093b6
RS
8460 constructor_erroneous = 1;
8461 return;
8b6a5902 8462 }
928c19bb
JM
8463 if (require_constant_value || require_constant_elements)
8464 constant_expression_warning (value);
8b6a5902 8465
3e4093b6
RS
8466 /* If this element doesn't come next in sequence,
8467 put it on constructor_pending_elts. */
8468 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8469 && (!constructor_incremental
8470 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8471 {
3e4093b6
RS
8472 if (constructor_incremental
8473 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8474 set_nonincremental_init (braced_init_obstack);
3e4093b6 8475
96b40f8d 8476 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8477 braced_init_obstack);
3e4093b6 8478 return;
8b6a5902 8479 }
3e4093b6
RS
8480 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8481 && (!constructor_incremental
8482 || field != constructor_unfilled_fields))
8b6a5902 8483 {
3e4093b6
RS
8484 /* We do this for records but not for unions. In a union,
8485 no matter which field is specified, it can be initialized
8486 right away since it starts at the beginning of the union. */
8487 if (constructor_incremental)
8488 {
8489 if (!constructor_unfilled_fields)
a1e3b3d9 8490 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8491 else
8492 {
8493 tree bitpos, unfillpos;
8494
8495 bitpos = bit_position (field);
8496 unfillpos = bit_position (constructor_unfilled_fields);
8497
8498 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8499 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8500 }
8501 }
8502
96b40f8d 8503 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8504 braced_init_obstack);
3e4093b6 8505 return;
8b6a5902 8506 }
3e4093b6 8507 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8508 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8509 {
b295aee2
JJ
8510 if (!implicit)
8511 {
9771b263 8512 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 8513 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
8514 "initialized field with side-effects overwritten");
8515 else if (warn_override_init)
96b40f8d
MP
8516 warning_init (loc, OPT_Woverride_init,
8517 "initialized field overwritten");
b295aee2 8518 }
8b6a5902 8519
3e4093b6 8520 /* We can have just one union field set. */
9771b263 8521 constructor_elements = NULL;
3e4093b6 8522 }
8b6a5902 8523
3e4093b6
RS
8524 /* Otherwise, output this element either to
8525 constructor_elements or to the assembler file. */
8b6a5902 8526
f32682ca 8527 constructor_elt celt = {field, value};
9771b263 8528 vec_safe_push (constructor_elements, celt);
8b6a5902 8529
3e4093b6
RS
8530 /* Advance the variable that indicates sequential elements output. */
8531 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8532 constructor_unfilled_index
db3927fb
AH
8533 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8534 bitsize_one_node);
3e4093b6
RS
8535 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8536 {
8537 constructor_unfilled_fields
910ad8de 8538 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8539
3e4093b6
RS
8540 /* Skip any nameless bit fields. */
8541 while (constructor_unfilled_fields != 0
8542 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8543 && DECL_NAME (constructor_unfilled_fields) == 0)
8544 constructor_unfilled_fields =
910ad8de 8545 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8546 }
8547 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8548 constructor_unfilled_fields = 0;
de520661 8549
3e4093b6
RS
8550 /* Now output any pending elements which have become next. */
8551 if (pending)
a1e3b3d9 8552 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8553}
8b6a5902 8554
3e4093b6
RS
8555/* Output any pending elements which have become next.
8556 As we output elements, constructor_unfilled_{fields,index}
8557 advances, which may cause other elements to become next;
8558 if so, they too are output.
8b6a5902 8559
3e4093b6
RS
8560 If ALL is 0, we return when there are
8561 no more pending elements to output now.
665f2503 8562
3e4093b6
RS
8563 If ALL is 1, we output space as necessary so that
8564 we can output all the pending elements. */
3e4093b6 8565static void
a1e3b3d9 8566output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8567{
8568 struct init_node *elt = constructor_pending_elts;
8569 tree next;
de520661 8570
3e4093b6
RS
8571 retry:
8572
ba228239 8573 /* Look through the whole pending tree.
3e4093b6
RS
8574 If we find an element that should be output now,
8575 output it. Otherwise, set NEXT to the element
8576 that comes first among those still pending. */
8577
8578 next = 0;
8579 while (elt)
8580 {
8581 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8582 {
3e4093b6
RS
8583 if (tree_int_cst_equal (elt->purpose,
8584 constructor_unfilled_index))
34cf811f
MP
8585 output_init_element (input_location, elt->value, elt->origtype,
8586 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8587 constructor_unfilled_index, 0, false,
8588 braced_init_obstack);
3e4093b6
RS
8589 else if (tree_int_cst_lt (constructor_unfilled_index,
8590 elt->purpose))
8b6a5902 8591 {
3e4093b6
RS
8592 /* Advance to the next smaller node. */
8593 if (elt->left)
8594 elt = elt->left;
8595 else
8596 {
8597 /* We have reached the smallest node bigger than the
8598 current unfilled index. Fill the space first. */
8599 next = elt->purpose;
8600 break;
8601 }
8b6a5902 8602 }
ce662d4c
JJ
8603 else
8604 {
3e4093b6
RS
8605 /* Advance to the next bigger node. */
8606 if (elt->right)
8607 elt = elt->right;
8608 else
ce662d4c 8609 {
3e4093b6
RS
8610 /* We have reached the biggest node in a subtree. Find
8611 the parent of it, which is the next bigger node. */
8612 while (elt->parent && elt->parent->right == elt)
8613 elt = elt->parent;
8614 elt = elt->parent;
8615 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8616 elt->purpose))
8617 {
8618 next = elt->purpose;
8619 break;
8620 }
ce662d4c
JJ
8621 }
8622 }
8b6a5902 8623 }
3e4093b6
RS
8624 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8625 || TREE_CODE (constructor_type) == UNION_TYPE)
8626 {
8627 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8628
3e4093b6
RS
8629 /* If the current record is complete we are done. */
8630 if (constructor_unfilled_fields == 0)
8631 break;
de520661 8632
3e4093b6
RS
8633 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8634 elt_bitpos = bit_position (elt->purpose);
8635 /* We can't compare fields here because there might be empty
8636 fields in between. */
8637 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8638 {
8639 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8640 output_init_element (input_location, elt->value, elt->origtype,
8641 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8642 elt->purpose, 0, false,
8643 braced_init_obstack);
3e4093b6
RS
8644 }
8645 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8646 {
8647 /* Advance to the next smaller node. */
8648 if (elt->left)
8649 elt = elt->left;
8650 else
8651 {
8652 /* We have reached the smallest node bigger than the
8653 current unfilled field. Fill the space first. */
8654 next = elt->purpose;
8655 break;
8656 }
8657 }
8658 else
8659 {
8660 /* Advance to the next bigger node. */
8661 if (elt->right)
8662 elt = elt->right;
8663 else
8664 {
8665 /* We have reached the biggest node in a subtree. Find
8666 the parent of it, which is the next bigger node. */
8667 while (elt->parent && elt->parent->right == elt)
8668 elt = elt->parent;
8669 elt = elt->parent;
8670 if (elt
8671 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8672 bit_position (elt->purpose))))
8673 {
8674 next = elt->purpose;
8675 break;
8676 }
8677 }
8678 }
8679 }
8680 }
de520661 8681
3e4093b6
RS
8682 /* Ordinarily return, but not if we want to output all
8683 and there are elements left. */
3f75a254 8684 if (!(all && next != 0))
e5cfb88f
RK
8685 return;
8686
3e4093b6
RS
8687 /* If it's not incremental, just skip over the gap, so that after
8688 jumping to retry we will output the next successive element. */
8689 if (TREE_CODE (constructor_type) == RECORD_TYPE
8690 || TREE_CODE (constructor_type) == UNION_TYPE)
8691 constructor_unfilled_fields = next;
8692 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8693 constructor_unfilled_index = next;
de520661 8694
3e4093b6
RS
8695 /* ELT now points to the node in the pending tree with the next
8696 initializer to output. */
8697 goto retry;
de520661
RS
8698}
8699\f
3e4093b6
RS
8700/* Add one non-braced element to the current constructor level.
8701 This adjusts the current position within the constructor's type.
8702 This may also start or terminate implicit levels
8703 to handle a partly-braced initializer.
e5e809f4 8704
3e4093b6 8705 Once this has found the correct level for the new element,
b295aee2
JJ
8706 it calls output_init_element.
8707
8708 IMPLICIT is true if value comes from pop_init_level (1),
8709 the new initializer has been merged with the existing one
8710 and thus no warnings should be emitted about overriding an
8711 existing initializer. */
3e4093b6
RS
8712
8713void
34cf811f 8714process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8715 struct obstack * braced_init_obstack)
e5e809f4 8716{
916c5919
JM
8717 tree orig_value = value.value;
8718 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8719 bool strict_string = value.original_code == STRING_CST;
340baef7 8720 bool was_designated = designator_depth != 0;
e5e809f4 8721
3e4093b6 8722 designator_depth = 0;
b06df647 8723 designator_erroneous = 0;
e5e809f4 8724
9bac5cbb
G
8725 if (!implicit && value.value && !integer_zerop (value.value))
8726 constructor_zeroinit = 0;
8727
3e4093b6
RS
8728 /* Handle superfluous braces around string cst as in
8729 char x[] = {"foo"}; */
8730 if (string_flag
8731 && constructor_type
340baef7 8732 && !was_designated
3e4093b6 8733 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8734 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8735 && integer_zerop (constructor_unfilled_index))
e5e809f4 8736 {
916c5919 8737 if (constructor_stack->replacement_value.value)
ea58ef42 8738 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8739 constructor_stack->replacement_value = value;
8740 return;
e5e809f4 8741 }
8b6a5902 8742
916c5919 8743 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8744 {
ea58ef42 8745 error_init (loc, "excess elements in struct initializer");
3e4093b6 8746 return;
e5e809f4
JL
8747 }
8748
3e4093b6
RS
8749 /* Ignore elements of a brace group if it is entirely superfluous
8750 and has already been diagnosed. */
8751 if (constructor_type == 0)
8752 return;
e5e809f4 8753
976d5a22
TT
8754 if (!implicit && warn_designated_init && !was_designated
8755 && TREE_CODE (constructor_type) == RECORD_TYPE
8756 && lookup_attribute ("designated_init",
8757 TYPE_ATTRIBUTES (constructor_type)))
8758 warning_init (loc,
8759 OPT_Wdesignated_init,
8760 "positional initialization of field "
8761 "in %<struct%> declared with %<designated_init%> attribute");
8762
3e4093b6
RS
8763 /* If we've exhausted any levels that didn't have braces,
8764 pop them now. */
8765 while (constructor_stack->implicit)
8766 {
8767 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8768 || TREE_CODE (constructor_type) == UNION_TYPE)
8769 && constructor_fields == 0)
ea58ef42
MP
8770 process_init_element (loc,
8771 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8772 true, braced_init_obstack);
53650abe
AP
8773 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8774 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8775 && constructor_max_index
8776 && tree_int_cst_lt (constructor_max_index,
8777 constructor_index))
ea58ef42
MP
8778 process_init_element (loc,
8779 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8780 true, braced_init_obstack);
3e4093b6
RS
8781 else
8782 break;
8783 }
e5e809f4 8784
3e4093b6
RS
8785 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8786 if (constructor_range_stack)
e5e809f4 8787 {
3e4093b6
RS
8788 /* If value is a compound literal and we'll be just using its
8789 content, don't put it into a SAVE_EXPR. */
916c5919 8790 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 8791 || !require_constant_value)
8ce94e44
JM
8792 {
8793 tree semantic_type = NULL_TREE;
8794 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8795 {
8796 semantic_type = TREE_TYPE (value.value);
8797 value.value = TREE_OPERAND (value.value, 0);
8798 }
8799 value.value = c_save_expr (value.value);
8800 if (semantic_type)
8801 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8802 value.value);
8803 }
3e4093b6 8804 }
e5e809f4 8805
3e4093b6
RS
8806 while (1)
8807 {
8808 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8809 {
3e4093b6
RS
8810 tree fieldtype;
8811 enum tree_code fieldcode;
e5e809f4 8812
3e4093b6
RS
8813 if (constructor_fields == 0)
8814 {
ea58ef42 8815 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8816 break;
8817 }
e5e809f4 8818
3e4093b6
RS
8819 fieldtype = TREE_TYPE (constructor_fields);
8820 if (fieldtype != error_mark_node)
8821 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8822 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8823
3e4093b6
RS
8824 /* Error for non-static initialization of a flexible array member. */
8825 if (fieldcode == ARRAY_TYPE
8826 && !require_constant_value
8827 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8828 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8829 {
ea58ef42
MP
8830 error_init (loc, "non-static initialization of a flexible "
8831 "array member");
3e4093b6
RS
8832 break;
8833 }
e5e809f4 8834
2cc901dc
MP
8835 /* Error for initialization of a flexible array member with
8836 a string constant if the structure is in an array. E.g.:
8837 struct S { int x; char y[]; };
8838 struct S s[] = { { 1, "foo" } };
8839 is invalid. */
8840 if (string_flag
8841 && fieldcode == ARRAY_TYPE
8842 && constructor_depth > 1
8843 && TYPE_SIZE (fieldtype) == NULL_TREE
8844 && DECL_CHAIN (constructor_fields) == NULL_TREE)
8845 {
8846 bool in_array_p = false;
8847 for (struct constructor_stack *p = constructor_stack;
8848 p && p->type; p = p->next)
8849 if (TREE_CODE (p->type) == ARRAY_TYPE)
8850 {
8851 in_array_p = true;
8852 break;
8853 }
8854 if (in_array_p)
8855 {
8856 error_init (loc, "initialization of flexible array "
8857 "member in a nested context");
8858 break;
8859 }
8860 }
8861
3e4093b6 8862 /* Accept a string constant to initialize a subarray. */
916c5919 8863 if (value.value != 0
3e4093b6 8864 && fieldcode == ARRAY_TYPE
197463ae 8865 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8866 && string_flag)
916c5919 8867 value.value = orig_value;
3e4093b6
RS
8868 /* Otherwise, if we have come to a subaggregate,
8869 and we don't have an element of its type, push into it. */
0953878d 8870 else if (value.value != 0
916c5919
JM
8871 && value.value != error_mark_node
8872 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8873 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8874 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8875 {
ea58ef42 8876 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8877 continue;
8878 }
e5e809f4 8879
916c5919 8880 if (value.value)
3e4093b6
RS
8881 {
8882 push_member_name (constructor_fields);
34cf811f 8883 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8884 strict_string, fieldtype,
a1e3b3d9
LB
8885 constructor_fields, 1, implicit,
8886 braced_init_obstack);
3e4093b6 8887 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8888 }
8889 else
3e4093b6
RS
8890 /* Do the bookkeeping for an element that was
8891 directly output as a constructor. */
e5e809f4 8892 {
3e4093b6
RS
8893 /* For a record, keep track of end position of last field. */
8894 if (DECL_SIZE (constructor_fields))
c22cacf3 8895 constructor_bit_index
db3927fb
AH
8896 = size_binop_loc (input_location, PLUS_EXPR,
8897 bit_position (constructor_fields),
8898 DECL_SIZE (constructor_fields));
3e4093b6
RS
8899
8900 /* If the current field was the first one not yet written out,
8901 it isn't now, so update. */
8902 if (constructor_unfilled_fields == constructor_fields)
8903 {
910ad8de 8904 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8905 /* Skip any nameless bit fields. */
8906 while (constructor_unfilled_fields != 0
8907 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8908 && DECL_NAME (constructor_unfilled_fields) == 0)
8909 constructor_unfilled_fields =
910ad8de 8910 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8911 }
e5e809f4 8912 }
3e4093b6 8913
910ad8de 8914 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8915 /* Skip any nameless bit fields at the beginning. */
8916 while (constructor_fields != 0
8917 && DECL_C_BIT_FIELD (constructor_fields)
8918 && DECL_NAME (constructor_fields) == 0)
910ad8de 8919 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8920 }
3e4093b6 8921 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8922 {
3e4093b6
RS
8923 tree fieldtype;
8924 enum tree_code fieldcode;
e5e809f4 8925
3e4093b6
RS
8926 if (constructor_fields == 0)
8927 {
d033409e 8928 pedwarn_init (loc, 0,
509c9d60 8929 "excess elements in union initializer");
3e4093b6
RS
8930 break;
8931 }
e5e809f4 8932
3e4093b6
RS
8933 fieldtype = TREE_TYPE (constructor_fields);
8934 if (fieldtype != error_mark_node)
8935 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8936 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8937
3e4093b6
RS
8938 /* Warn that traditional C rejects initialization of unions.
8939 We skip the warning if the value is zero. This is done
8940 under the assumption that the zero initializer in user
8941 code appears conditioned on e.g. __STDC__ to avoid
8942 "missing initializer" warnings and relies on default
8943 initialization to zero in the traditional C case.
8944 We also skip the warning if the initializer is designated,
8945 again on the assumption that this must be conditional on
8946 __STDC__ anyway (and we've already complained about the
8947 member-designator already). */
8400e75e 8948 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8949 && !(value.value && (integer_zerop (value.value)
8950 || real_zerop (value.value))))
3176a0c2
DD
8951 warning (OPT_Wtraditional, "traditional C rejects initialization "
8952 "of unions");
e5e809f4 8953
3e4093b6 8954 /* Accept a string constant to initialize a subarray. */
916c5919 8955 if (value.value != 0
3e4093b6 8956 && fieldcode == ARRAY_TYPE
197463ae 8957 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8958 && string_flag)
916c5919 8959 value.value = orig_value;
3e4093b6
RS
8960 /* Otherwise, if we have come to a subaggregate,
8961 and we don't have an element of its type, push into it. */
0953878d 8962 else if (value.value != 0
916c5919
JM
8963 && value.value != error_mark_node
8964 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8965 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8966 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8967 {
ea58ef42 8968 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8969 continue;
8970 }
e5e809f4 8971
916c5919 8972 if (value.value)
3e4093b6
RS
8973 {
8974 push_member_name (constructor_fields);
34cf811f 8975 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8976 strict_string, fieldtype,
a1e3b3d9
LB
8977 constructor_fields, 1, implicit,
8978 braced_init_obstack);
3e4093b6 8979 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8980 }
8981 else
3e4093b6
RS
8982 /* Do the bookkeeping for an element that was
8983 directly output as a constructor. */
e5e809f4 8984 {
3e4093b6 8985 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8986 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8987 }
e5e809f4 8988
3e4093b6
RS
8989 constructor_fields = 0;
8990 }
8991 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8992 {
8993 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8994 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8995
3e4093b6 8996 /* Accept a string constant to initialize a subarray. */
916c5919 8997 if (value.value != 0
3e4093b6 8998 && eltcode == ARRAY_TYPE
197463ae 8999 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9000 && string_flag)
916c5919 9001 value.value = orig_value;
3e4093b6
RS
9002 /* Otherwise, if we have come to a subaggregate,
9003 and we don't have an element of its type, push into it. */
0953878d 9004 else if (value.value != 0
916c5919
JM
9005 && value.value != error_mark_node
9006 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9007 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9008 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9009 {
ea58ef42 9010 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9011 continue;
9012 }
8b6a5902 9013
3e4093b6
RS
9014 if (constructor_max_index != 0
9015 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9016 || integer_all_onesp (constructor_max_index)))
9017 {
d033409e 9018 pedwarn_init (loc, 0,
509c9d60 9019 "excess elements in array initializer");
3e4093b6
RS
9020 break;
9021 }
8b6a5902 9022
3e4093b6 9023 /* Now output the actual element. */
916c5919 9024 if (value.value)
3e4093b6 9025 {
ae7e9ddd 9026 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9027 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9028 strict_string, elttype,
a1e3b3d9
LB
9029 constructor_index, 1, implicit,
9030 braced_init_obstack);
3e4093b6
RS
9031 RESTORE_SPELLING_DEPTH (constructor_depth);
9032 }
2f6e4e97 9033
3e4093b6 9034 constructor_index
db3927fb
AH
9035 = size_binop_loc (input_location, PLUS_EXPR,
9036 constructor_index, bitsize_one_node);
8b6a5902 9037
916c5919 9038 if (!value.value)
3e4093b6
RS
9039 /* If we are doing the bookkeeping for an element that was
9040 directly output as a constructor, we must update
9041 constructor_unfilled_index. */
9042 constructor_unfilled_index = constructor_index;
9043 }
9044 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
9045 {
9046 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9047
c22cacf3
MS
9048 /* Do a basic check of initializer size. Note that vectors
9049 always have a fixed size derived from their type. */
3e4093b6
RS
9050 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9051 {
d033409e 9052 pedwarn_init (loc, 0,
509c9d60 9053 "excess elements in vector initializer");
3e4093b6
RS
9054 break;
9055 }
8b6a5902 9056
3e4093b6 9057 /* Now output the actual element. */
916c5919 9058 if (value.value)
53650abe
AP
9059 {
9060 if (TREE_CODE (value.value) == VECTOR_CST)
9061 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9062 output_init_element (loc, value.value, value.original_type,
53650abe 9063 strict_string, elttype,
a1e3b3d9
LB
9064 constructor_index, 1, implicit,
9065 braced_init_obstack);
53650abe 9066 }
8b6a5902 9067
3e4093b6 9068 constructor_index
db3927fb
AH
9069 = size_binop_loc (input_location,
9070 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9071
916c5919 9072 if (!value.value)
3e4093b6
RS
9073 /* If we are doing the bookkeeping for an element that was
9074 directly output as a constructor, we must update
9075 constructor_unfilled_index. */
9076 constructor_unfilled_index = constructor_index;
9077 }
8b6a5902 9078
3e4093b6
RS
9079 /* Handle the sole element allowed in a braced initializer
9080 for a scalar variable. */
b4519d39
SB
9081 else if (constructor_type != error_mark_node
9082 && constructor_fields == 0)
8b6a5902 9083 {
d033409e 9084 pedwarn_init (loc, 0,
509c9d60 9085 "excess elements in scalar initializer");
3e4093b6 9086 break;
8b6a5902
JJ
9087 }
9088 else
9089 {
916c5919 9090 if (value.value)
34cf811f 9091 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9092 strict_string, constructor_type,
a1e3b3d9
LB
9093 NULL_TREE, 1, implicit,
9094 braced_init_obstack);
3e4093b6 9095 constructor_fields = 0;
8b6a5902
JJ
9096 }
9097
3e4093b6
RS
9098 /* Handle range initializers either at this level or anywhere higher
9099 in the designator stack. */
9100 if (constructor_range_stack)
8b6a5902 9101 {
3e4093b6
RS
9102 struct constructor_range_stack *p, *range_stack;
9103 int finish = 0;
9104
9105 range_stack = constructor_range_stack;
9106 constructor_range_stack = 0;
9107 while (constructor_stack != range_stack->stack)
8b6a5902 9108 {
366de0ce 9109 gcc_assert (constructor_stack->implicit);
34cf811f 9110 process_init_element (loc,
ea58ef42
MP
9111 pop_init_level (loc, 1,
9112 braced_init_obstack),
a1e3b3d9 9113 true, braced_init_obstack);
8b6a5902 9114 }
3e4093b6
RS
9115 for (p = range_stack;
9116 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9117 p = p->prev)
8b6a5902 9118 {
366de0ce 9119 gcc_assert (constructor_stack->implicit);
34cf811f 9120 process_init_element (loc,
ea58ef42
MP
9121 pop_init_level (loc, 1,
9122 braced_init_obstack),
a1e3b3d9 9123 true, braced_init_obstack);
8b6a5902 9124 }
3e4093b6 9125
db3927fb
AH
9126 p->index = size_binop_loc (input_location,
9127 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
9128 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9129 finish = 1;
9130
9131 while (1)
9132 {
9133 constructor_index = p->index;
9134 constructor_fields = p->fields;
9135 if (finish && p->range_end && p->index == p->range_start)
9136 {
9137 finish = 0;
9138 p->prev = 0;
9139 }
9140 p = p->next;
9141 if (!p)
9142 break;
ea58ef42 9143 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9144 p->stack = constructor_stack;
9145 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9146 p->index = p->range_start;
9147 }
9148
9149 if (!finish)
9150 constructor_range_stack = range_stack;
9151 continue;
8b6a5902
JJ
9152 }
9153
3e4093b6 9154 break;
8b6a5902
JJ
9155 }
9156
3e4093b6
RS
9157 constructor_range_stack = 0;
9158}
9159\f
9f0e2d86
ZW
9160/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9161 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9162 an ASM_EXPR node). */
3e4093b6 9163tree
9f0e2d86 9164build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9165{
6de9cd9a
DN
9166 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9167 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9168 return add_stmt (args);
8b6a5902
JJ
9169}
9170
9f0e2d86
ZW
9171/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9172 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9173 SIMPLE indicates whether there was anything at all after the
9174 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9175 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9176tree
c2255bc4 9177build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9178 tree clobbers, tree labels, bool simple)
e5e809f4 9179{
3e4093b6 9180 tree tail;
9f0e2d86 9181 tree args;
6de9cd9a
DN
9182 int i;
9183 const char *constraint;
74f0c611 9184 const char **oconstraints;
6de9cd9a 9185 bool allows_mem, allows_reg, is_inout;
74f0c611 9186 int ninputs, noutputs;
6de9cd9a
DN
9187
9188 ninputs = list_length (inputs);
9189 noutputs = list_length (outputs);
74f0c611
RH
9190 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9191
1c384bf1 9192 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9193
6de9cd9a
DN
9194 /* Remove output conversions that change the type but not the mode. */
9195 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9196 {
3e4093b6 9197 tree output = TREE_VALUE (tail);
74f0c611 9198
eadd3d0d
JJ
9199 output = c_fully_fold (output, false, NULL);
9200
74f0c611
RH
9201 /* ??? Really, this should not be here. Users should be using a
9202 proper lvalue, dammit. But there's a long history of using casts
9203 in the output operands. In cases like longlong.h, this becomes a
9204 primitive form of typechecking -- if the cast can be removed, then
9205 the output operand had a type of the proper width; otherwise we'll
9206 get an error. Gross, but ... */
3e4093b6 9207 STRIP_NOPS (output);
74f0c611 9208
7bd11157 9209 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9210 output = error_mark_node;
8b6a5902 9211
5544530a
PB
9212 if (output != error_mark_node
9213 && (TREE_READONLY (output)
9214 || TYPE_READONLY (TREE_TYPE (output))
9215 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9216 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9217 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9218 readonly_error (loc, output, lv_asm);
5544530a 9219
6de9cd9a 9220 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9221 oconstraints[i] = constraint;
9222
9223 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9224 &allows_mem, &allows_reg, &is_inout))
9225 {
9226 /* If the operand is going to end up in memory,
9227 mark it addressable. */
9228 if (!allows_reg && !c_mark_addressable (output))
9229 output = error_mark_node;
bae5cddf
JJ
9230 if (!(!allows_reg && allows_mem)
9231 && output != error_mark_node
9232 && VOID_TYPE_P (TREE_TYPE (output)))
9233 {
9234 error_at (loc, "invalid use of void expression");
9235 output = error_mark_node;
9236 }
74f0c611
RH
9237 }
9238 else
c22cacf3 9239 output = error_mark_node;
3e4093b6 9240
74f0c611 9241 TREE_VALUE (tail) = output;
8b6a5902 9242 }
3e4093b6 9243
74f0c611
RH
9244 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9245 {
9246 tree input;
9247
9248 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9249 input = TREE_VALUE (tail);
9250
74f0c611
RH
9251 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9252 oconstraints, &allows_mem, &allows_reg))
9253 {
9254 /* If the operand is going to end up in memory,
9255 mark it addressable. */
b4c33883
AP
9256 if (!allows_reg && allows_mem)
9257 {
eadd3d0d
JJ
9258 input = c_fully_fold (input, false, NULL);
9259
b4c33883
AP
9260 /* Strip the nops as we allow this case. FIXME, this really
9261 should be rejected or made deprecated. */
9262 STRIP_NOPS (input);
9263 if (!c_mark_addressable (input))
9264 input = error_mark_node;
bae5cddf 9265 }
eadd3d0d 9266 else
bae5cddf 9267 {
eadd3d0d
JJ
9268 struct c_expr expr;
9269 memset (&expr, 0, sizeof (expr));
9270 expr.value = input;
267bac10 9271 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9272 input = c_fully_fold (expr.value, false, NULL);
9273
9274 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9275 {
9276 error_at (loc, "invalid use of void expression");
9277 input = error_mark_node;
9278 }
bae5cddf 9279 }
74f0c611
RH
9280 }
9281 else
9282 input = error_mark_node;
9283
9284 TREE_VALUE (tail) = input;
9285 }
3e4093b6 9286
1c384bf1
RH
9287 /* ASMs with labels cannot have outputs. This should have been
9288 enforced by the parser. */
9289 gcc_assert (outputs == NULL || labels == NULL);
9290
9291 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9292
5544530a
PB
9293 /* asm statements without outputs, including simple ones, are treated
9294 as volatile. */
9295 ASM_INPUT_P (args) = simple;
9296 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9297
9f0e2d86 9298 return args;
e5e809f4 9299}
3e4093b6 9300\f
c2255bc4
AH
9301/* Generate a goto statement to LABEL. LOC is the location of the
9302 GOTO. */
506e2710
RH
9303
9304tree
c2255bc4 9305c_finish_goto_label (location_t loc, tree label)
506e2710 9306{
e1b7793c 9307 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9308 if (!decl)
9309 return NULL_TREE;
506e2710 9310 TREE_USED (decl) = 1;
c2255bc4
AH
9311 {
9312 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9313 SET_EXPR_LOCATION (t, loc);
9314 return add_stmt (t);
9315 }
506e2710
RH
9316}
9317
c2255bc4
AH
9318/* Generate a computed goto statement to EXPR. LOC is the location of
9319 the GOTO. */
506e2710
RH
9320
9321tree
c2255bc4 9322c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9323{
c2255bc4 9324 tree t;
c1771a20 9325 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9326 expr = c_fully_fold (expr, false, NULL);
506e2710 9327 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9328 t = build1 (GOTO_EXPR, void_type_node, expr);
9329 SET_EXPR_LOCATION (t, loc);
9330 return add_stmt (t);
506e2710
RH
9331}
9332
5088b058 9333/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9334 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9335 the location of the return statement, or the location of the expression,
9336 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9337 is the original type of RETVAL. */
de520661 9338
506e2710 9339tree
c2255bc4 9340c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9341{
0c9b182b
JJ
9342 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9343 bool no_warning = false;
928c19bb 9344 bool npc = false;
36536d79 9345 size_t rank = 0;
3e4093b6
RS
9346
9347 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9348 warning_at (loc, 0,
9349 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9350
b72271b9 9351 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9352 {
9353 /* Array notations are allowed in a return statement if it is inside a
9354 built-in array notation reduction function. */
9355 if (!find_rank (loc, retval, retval, false, &rank))
9356 return error_mark_node;
9357 if (rank >= 1)
9358 {
9359 error_at (loc, "array notation expression cannot be used as a "
9360 "return value");
9361 return error_mark_node;
9362 }
9363 }
3af9c5e9 9364 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9365 {
9366 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9367 "allowed");
9368 return error_mark_node;
9369 }
928c19bb
JM
9370 if (retval)
9371 {
8ce94e44 9372 tree semantic_type = NULL_TREE;
928c19bb 9373 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9374 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9375 {
9376 semantic_type = TREE_TYPE (retval);
9377 retval = TREE_OPERAND (retval, 0);
9378 }
928c19bb 9379 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9380 if (semantic_type)
9381 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9382 }
9383
3e4093b6 9384 if (!retval)
de520661 9385 {
3e4093b6
RS
9386 current_function_returns_null = 1;
9387 if ((warn_return_type || flag_isoc99)
9388 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9389 {
35aff4fb
MP
9390 if (flag_isoc99)
9391 pedwarn (loc, 0, "%<return%> with no value, in "
9392 "function returning non-void");
9393 else
9394 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9395 "in function returning non-void");
0c9b182b
JJ
9396 no_warning = true;
9397 }
400fbf9f 9398 }
3e4093b6 9399 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9400 {
3e4093b6 9401 current_function_returns_null = 1;
2397c575 9402 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9403 pedwarn (loc, 0,
509c9d60 9404 "%<return%> with a value, in function returning void");
b8698a0f 9405 else
c1771a20 9406 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9407 "%<return%> with expression, in function returning void");
de520661 9408 }
3e4093b6 9409 else
de520661 9410 {
68fca595
MP
9411 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9412 retval, origtype, ic_return,
c2255bc4 9413 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9414 tree res = DECL_RESULT (current_function_decl);
9415 tree inner;
9feb29df 9416 bool save;
3e4093b6
RS
9417
9418 current_function_returns_value = 1;
9419 if (t == error_mark_node)
506e2710 9420 return NULL_TREE;
3e4093b6 9421
9feb29df
JJ
9422 save = in_late_binary_op;
9423 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
9424 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9425 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9426 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9427 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9428 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
9feb29df 9429 in_late_binary_op = true;
3e4093b6 9430 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9431 in_late_binary_op = save;
3e4093b6
RS
9432
9433 /* Strip any conversions, additions, and subtractions, and see if
9434 we are returning the address of a local variable. Warn if so. */
9435 while (1)
8b6a5902 9436 {
3e4093b6 9437 switch (TREE_CODE (inner))
8b6a5902 9438 {
849421a3
JJ
9439 CASE_CONVERT:
9440 case NON_LVALUE_EXPR:
3e4093b6 9441 case PLUS_EXPR:
849421a3 9442 case POINTER_PLUS_EXPR:
3e4093b6
RS
9443 inner = TREE_OPERAND (inner, 0);
9444 continue;
9445
9446 case MINUS_EXPR:
9447 /* If the second operand of the MINUS_EXPR has a pointer
9448 type (or is converted from it), this may be valid, so
9449 don't give a warning. */
9450 {
9451 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9452
3f75a254 9453 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9454 && (CONVERT_EXPR_P (op1)
9455 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9456 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9457
3e4093b6
RS
9458 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9459 break;
8b6a5902 9460
3e4093b6
RS
9461 inner = TREE_OPERAND (inner, 0);
9462 continue;
9463 }
400fbf9f 9464
3e4093b6
RS
9465 case ADDR_EXPR:
9466 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9467
6615c446 9468 while (REFERENCE_CLASS_P (inner)
c22cacf3 9469 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9470 inner = TREE_OPERAND (inner, 0);
8b6a5902 9471
a2f1f4c3 9472 if (DECL_P (inner)
3f75a254
JM
9473 && !DECL_EXTERNAL (inner)
9474 && !TREE_STATIC (inner)
3e4093b6 9475 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9476 {
9477 if (TREE_CODE (inner) == LABEL_DECL)
9478 warning_at (loc, OPT_Wreturn_local_addr,
9479 "function returns address of label");
9480 else
b4dfdc11
MG
9481 {
9482 warning_at (loc, OPT_Wreturn_local_addr,
9483 "function returns address of local variable");
9484 tree zero = build_zero_cst (TREE_TYPE (res));
9485 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9486 }
19fc9faa 9487 }
3e4093b6 9488 break;
8b6a5902 9489
3e4093b6
RS
9490 default:
9491 break;
9492 }
de520661 9493
3e4093b6
RS
9494 break;
9495 }
9496
53fb4de3 9497 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9498 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9499
9500 if (warn_sequence_point)
9501 verify_sequence_points (retval);
de520661 9502 }
8b6a5902 9503
c2255bc4 9504 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9505 TREE_NO_WARNING (ret_stmt) |= no_warning;
9506 return add_stmt (ret_stmt);
de520661 9507}
3e4093b6
RS
9508\f
9509struct c_switch {
604f5adf
ILT
9510 /* The SWITCH_EXPR being built. */
9511 tree switch_expr;
a6c0a76c 9512
89dbed81 9513 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9514 default conversion is applied. */
9515 tree orig_type;
9516
3e4093b6
RS
9517 /* A splay-tree mapping the low element of a case range to the high
9518 element, or NULL_TREE if there is no high element. Used to
9519 determine whether or not a new case label duplicates an old case
9520 label. We need a tree, rather than simply a hash table, because
9521 of the GNU case range extension. */
9522 splay_tree cases;
a6c0a76c 9523
e1b7793c
ILT
9524 /* The bindings at the point of the switch. This is used for
9525 warnings crossing decls when branching to a case label. */
9526 struct c_spot_bindings *bindings;
187230a7 9527
3e4093b6
RS
9528 /* The next node on the stack. */
9529 struct c_switch *next;
9530};
400fbf9f 9531
3e4093b6
RS
9532/* A stack of the currently active switch statements. The innermost
9533 switch statement is on the top of the stack. There is no need to
9534 mark the stack for garbage collection because it is only active
9535 during the processing of the body of a function, and we never
9536 collect at that point. */
de520661 9537
506e2710 9538struct c_switch *c_switch_stack;
de520661 9539
3e4093b6 9540/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9541 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9542 SWITCH_COND_LOC is the location of the switch's condition.
9543 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9544
3e4093b6 9545tree
c2255bc4
AH
9546c_start_case (location_t switch_loc,
9547 location_t switch_cond_loc,
fedfecef 9548 tree exp, bool explicit_cast_p)
de520661 9549{
c58e8676 9550 tree orig_type = error_mark_node;
3e4093b6 9551 struct c_switch *cs;
2f6e4e97 9552
3e4093b6 9553 if (exp != error_mark_node)
de520661 9554 {
3e4093b6
RS
9555 orig_type = TREE_TYPE (exp);
9556
c58e8676 9557 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9558 {
c58e8676
VR
9559 if (orig_type != error_mark_node)
9560 {
c2255bc4 9561 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9562 orig_type = error_mark_node;
9563 }
3e4093b6 9564 exp = integer_zero_node;
de520661 9565 }
3e4093b6 9566 else
de520661 9567 {
c58e8676 9568 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9569 tree e = exp;
9570
9571 /* Warn if the condition has boolean value. */
9572 while (TREE_CODE (e) == COMPOUND_EXPR)
9573 e = TREE_OPERAND (e, 1);
9574
9575 if ((TREE_CODE (type) == BOOLEAN_TYPE
9576 || truth_value_p (TREE_CODE (e)))
9577 /* Explicit cast to int suppresses this warning. */
9578 && !(TREE_CODE (type) == INTEGER_TYPE
9579 && explicit_cast_p))
9580 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9581 "switch condition has boolean value");
8b6a5902 9582
8400e75e 9583 if (!in_system_header_at (input_location)
3e4093b6
RS
9584 && (type == long_integer_type_node
9585 || type == long_unsigned_type_node))
c2255bc4
AH
9586 warning_at (switch_cond_loc,
9587 OPT_Wtraditional, "%<long%> switch expression not "
9588 "converted to %<int%> in ISO C");
8b6a5902 9589
928c19bb 9590 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9591 exp = default_conversion (exp);
ca085fd7
MLI
9592
9593 if (warn_sequence_point)
9594 verify_sequence_points (exp);
3e4093b6
RS
9595 }
9596 }
9597
604f5adf 9598 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9599 cs = XNEW (struct c_switch);
604f5adf 9600 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9601 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9602 cs->orig_type = orig_type;
3e4093b6 9603 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9604 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9605 cs->next = c_switch_stack;
9606 c_switch_stack = cs;
3e4093b6 9607
604f5adf 9608 return add_stmt (cs->switch_expr);
3e4093b6
RS
9609}
9610
c2255bc4 9611/* Process a case label at location LOC. */
3e4093b6
RS
9612
9613tree
c2255bc4 9614do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9615{
9616 tree label = NULL_TREE;
9617
17cede2e
JM
9618 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9619 {
9620 low_value = c_fully_fold (low_value, false, NULL);
9621 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9622 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9623 "case label is not an integer constant expression");
9624 }
9625
9626 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9627 {
9628 high_value = c_fully_fold (high_value, false, NULL);
9629 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9630 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9631 "case label is not an integer constant expression");
9632 }
9633
e1b7793c 9634 if (c_switch_stack == NULL)
187230a7
JM
9635 {
9636 if (low_value)
e1b7793c 9637 error_at (loc, "case label not within a switch statement");
187230a7 9638 else
e1b7793c
ILT
9639 error_at (loc, "%<default%> label not within a switch statement");
9640 return NULL_TREE;
187230a7 9641 }
de520661 9642
e1b7793c
ILT
9643 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9644 EXPR_LOCATION (c_switch_stack->switch_expr),
9645 loc))
9646 return NULL_TREE;
9647
9648 label = c_add_case_label (loc, c_switch_stack->cases,
9649 SWITCH_COND (c_switch_stack->switch_expr),
9650 c_switch_stack->orig_type,
9651 low_value, high_value);
9652 if (label == error_mark_node)
9653 label = NULL_TREE;
3e4093b6
RS
9654 return label;
9655}
de520661 9656
083e891e
MP
9657/* Finish the switch statement. TYPE is the original type of the
9658 controlling expression of the switch, or NULL_TREE. */
de520661 9659
3e4093b6 9660void
083e891e 9661c_finish_case (tree body, tree type)
3e4093b6 9662{
506e2710 9663 struct c_switch *cs = c_switch_stack;
fbc315db 9664 location_t switch_location;
3e4093b6 9665
604f5adf 9666 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9667
6de9cd9a 9668 /* Emit warnings as needed. */
c2255bc4 9669 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9670 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9671 type ? type : TREE_TYPE (cs->switch_expr),
fbc315db 9672 SWITCH_COND (cs->switch_expr));
6de9cd9a 9673
3e4093b6 9674 /* Pop the stack. */
506e2710 9675 c_switch_stack = cs->next;
3e4093b6 9676 splay_tree_delete (cs->cases);
e1b7793c 9677 c_release_switch_bindings (cs->bindings);
5d038c4c 9678 XDELETE (cs);
de520661 9679}
325c3691 9680\f
506e2710
RH
9681/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9682 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9683 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9684 statement, and was not surrounded with parenthesis. */
325c3691 9685
9e51cf9d 9686void
506e2710
RH
9687c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9688 tree else_block, bool nested_if)
325c3691 9689{
506e2710 9690 tree stmt;
325c3691 9691
25c22937
BI
9692 /* If the condition has array notations, then the rank of the then_block and
9693 else_block must be either 0 or be equal to the rank of the condition. If
9694 the condition does not have array notations then break them up as it is
9695 broken up in a normal expression. */
b72271b9 9696 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9697 {
9698 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9699 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9700 return;
9701 if (then_block
9702 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9703 return;
9704 if (else_block
9705 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9706 return;
9707 if (cond_rank != then_rank && then_rank != 0)
9708 {
9709 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9710 " and the then-block");
9711 return;
9712 }
9713 else if (cond_rank != else_rank && else_rank != 0)
9714 {
9715 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9716 " and the else-block");
9717 return;
9718 }
9719 }
506e2710
RH
9720 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9721 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9722 {
506e2710 9723 tree inner_if = then_block;
16865eaa 9724
61ada8ae 9725 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9726 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9727 it might not be exactly THEN_BLOCK, but should be the last
9728 non-container statement within. */
9729 while (1)
9730 switch (TREE_CODE (inner_if))
9731 {
9732 case COND_EXPR:
9733 goto found;
9734 case BIND_EXPR:
9735 inner_if = BIND_EXPR_BODY (inner_if);
9736 break;
9737 case STATEMENT_LIST:
9738 inner_if = expr_last (then_block);
9739 break;
9740 case TRY_FINALLY_EXPR:
9741 case TRY_CATCH_EXPR:
9742 inner_if = TREE_OPERAND (inner_if, 0);
9743 break;
9744 default:
366de0ce 9745 gcc_unreachable ();
506e2710
RH
9746 }
9747 found:
16865eaa 9748
506e2710 9749 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9750 warning_at (if_locus, OPT_Wparentheses,
9751 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9752 }
16865eaa 9753
2214de30 9754 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9755 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9756 add_stmt (stmt);
325c3691
RH
9757}
9758
506e2710
RH
9759/* Emit a general-purpose loop construct. START_LOCUS is the location of
9760 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9761 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9762 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9763 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9764
9765void
506e2710
RH
9766c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9767 tree blab, tree clab, bool cond_is_first)
325c3691 9768{
506e2710
RH
9769 tree entry = NULL, exit = NULL, t;
9770
e5e44252
AK
9771 /* In theory could forbid cilk spawn for loop increment expression,
9772 but it should work just fine. */
36536d79 9773
28af952a
RS
9774 /* If the condition is zero don't generate a loop construct. */
9775 if (cond && integer_zerop (cond))
9776 {
9777 if (cond_is_first)
9778 {
9779 t = build_and_jump (&blab);
9780 SET_EXPR_LOCATION (t, start_locus);
9781 add_stmt (t);
9782 }
9783 }
9784 else
506e2710
RH
9785 {
9786 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9787
506e2710 9788 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9789 out of the loop, or to the top of it. If there's no exit condition,
9790 then we just build a jump back to the top. */
506e2710 9791 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9792
28af952a 9793 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9794 {
9795 /* Canonicalize the loop condition to the end. This means
9796 generating a branch to the loop condition. Reuse the
9797 continue label, if possible. */
9798 if (cond_is_first)
9799 {
9800 if (incr || !clab)
9801 {
9802 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9803 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9804 }
9805 else
9806 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9807 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9808 add_stmt (t);
9809 }
9810
506e2710 9811 t = build_and_jump (&blab);
506e2710 9812 if (cond_is_first)
db3927fb
AH
9813 exit = fold_build3_loc (start_locus,
9814 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9815 else
db3927fb
AH
9816 exit = fold_build3_loc (input_location,
9817 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9818 }
9819
506e2710
RH
9820 add_stmt (top);
9821 }
c22cacf3 9822
506e2710
RH
9823 if (body)
9824 add_stmt (body);
9825 if (clab)
9826 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9827 if (incr)
9828 add_stmt (incr);
9829 if (entry)
9830 add_stmt (entry);
9831 if (exit)
9832 add_stmt (exit);
9833 if (blab)
9834 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9835}
325c3691
RH
9836
9837tree
c2255bc4 9838c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9839{
089efaa4 9840 bool skip;
506e2710 9841 tree label = *label_p;
325c3691 9842
089efaa4
ILT
9843 /* In switch statements break is sometimes stylistically used after
9844 a return statement. This can lead to spurious warnings about
9845 control reaching the end of a non-void function when it is
9846 inlined. Note that we are calling block_may_fallthru with
9847 language specific tree nodes; this works because
9848 block_may_fallthru returns true when given something it does not
9849 understand. */
9850 skip = !block_may_fallthru (cur_stmt_list);
9851
506e2710 9852 if (!label)
089efaa4
ILT
9853 {
9854 if (!skip)
c2255bc4 9855 *label_p = label = create_artificial_label (loc);
089efaa4 9856 }
953ff289
DN
9857 else if (TREE_CODE (label) == LABEL_DECL)
9858 ;
9859 else switch (TREE_INT_CST_LOW (label))
506e2710 9860 {
953ff289 9861 case 0:
506e2710 9862 if (is_break)
c2255bc4 9863 error_at (loc, "break statement not within loop or switch");
506e2710 9864 else
c2255bc4 9865 error_at (loc, "continue statement not within a loop");
506e2710 9866 return NULL_TREE;
953ff289
DN
9867
9868 case 1:
9869 gcc_assert (is_break);
c2255bc4 9870 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9871 return NULL_TREE;
9872
c02065fc
AH
9873 case 2:
9874 if (is_break)
9875 error ("break statement within %<#pragma simd%> loop body");
9876 else
9877 error ("continue statement within %<#pragma simd%> loop body");
9878 return NULL_TREE;
9879
953ff289
DN
9880 default:
9881 gcc_unreachable ();
506e2710 9882 }
325c3691 9883
089efaa4
ILT
9884 if (skip)
9885 return NULL_TREE;
9886
2e28e797
JH
9887 if (!is_break)
9888 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9889
53fb4de3 9890 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9891}
9892
506e2710 9893/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9894
9895static void
c2255bc4 9896emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9897{
e6b5a630
RH
9898 if (expr == error_mark_node)
9899 ;
9900 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9901 {
9902 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9903 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9904 }
789eadcd
MP
9905 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9906 {
9907 tree r = expr;
9908 location_t cloc = loc;
9909 while (TREE_CODE (r) == COMPOUND_EXPR)
9910 {
9911 if (EXPR_HAS_LOCATION (r))
9912 cloc = EXPR_LOCATION (r);
9913 r = TREE_OPERAND (r, 1);
9914 }
9915 if (!TREE_SIDE_EFFECTS (r)
9916 && !VOID_TYPE_P (TREE_TYPE (r))
9917 && !CONVERT_EXPR_P (r)
cc28fc7f 9918 && !TREE_NO_WARNING (r)
789eadcd
MP
9919 && !TREE_NO_WARNING (expr))
9920 warning_at (cloc, OPT_Wunused_value,
9921 "right-hand operand of comma expression has no effect");
9922 }
27f33b15 9923 else
c2255bc4 9924 warn_if_unused_value (expr, loc);
3a5b9284
RH
9925}
9926
506e2710 9927/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9928 diagnostics, but do not call ADD_STMT. LOC is the location of the
9929 statement. */
3a5b9284 9930
506e2710 9931tree
c2255bc4 9932c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9933{
056928b2
JJ
9934 tree exprv;
9935
3a5b9284 9936 if (!expr)
506e2710 9937 return NULL_TREE;
3a5b9284 9938
928c19bb
JM
9939 expr = c_fully_fold (expr, false, NULL);
9940
3a5b9284
RH
9941 if (warn_sequence_point)
9942 verify_sequence_points (expr);
9943
9944 if (TREE_TYPE (expr) != error_mark_node
9945 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9946 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9947 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9948
9949 /* If we're not processing a statement expression, warn about unused values.
9950 Warnings for statement expressions will be emitted later, once we figure
9951 out which is the result. */
9952 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9953 && warn_unused_value)
c2255bc4 9954 emit_side_effect_warnings (loc, expr);
3a5b9284 9955
056928b2
JJ
9956 exprv = expr;
9957 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9958 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9959 while (CONVERT_EXPR_P (exprv))
9960 exprv = TREE_OPERAND (exprv, 0);
9961 if (DECL_P (exprv)
9962 || handled_component_p (exprv)
9963 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9964 mark_exp_read (exprv);
fa8351f8 9965
3a5b9284
RH
9966 /* If the expression is not of a type to which we cannot assign a line
9967 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9968 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9969 {
9970 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9971 SET_EXPR_LOCATION (expr, loc);
9972 }
506e2710
RH
9973
9974 return expr;
9975}
9976
c2255bc4
AH
9977/* Emit an expression as a statement. LOC is the location of the
9978 expression. */
506e2710
RH
9979
9980tree
c2255bc4 9981c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9982{
9983 if (expr)
c2255bc4 9984 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9985 else
9986 return NULL;
3a5b9284
RH
9987}
9988
9989/* Do the opposite and emit a statement as an expression. To begin,
9990 create a new binding level and return it. */
325c3691
RH
9991
9992tree
9993c_begin_stmt_expr (void)
9994{
9995 tree ret;
9996
9997 /* We must force a BLOCK for this level so that, if it is not expanded
9998 later, there is a way to turn off the entire subtree of blocks that
9999 are contained in it. */
10000 keep_next_level ();
10001 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10002
10003 c_bindings_start_stmt_expr (c_switch_stack == NULL
10004 ? NULL
10005 : c_switch_stack->bindings);
325c3691
RH
10006
10007 /* Mark the current statement list as belonging to a statement list. */
10008 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10009
10010 return ret;
10011}
10012
c2255bc4
AH
10013/* LOC is the location of the compound statement to which this body
10014 belongs. */
10015
325c3691 10016tree
c2255bc4 10017c_finish_stmt_expr (location_t loc, tree body)
325c3691 10018{
3a5b9284 10019 tree last, type, tmp, val;
325c3691
RH
10020 tree *last_p;
10021
c2255bc4 10022 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10023
10024 c_bindings_end_stmt_expr (c_switch_stack == NULL
10025 ? NULL
10026 : c_switch_stack->bindings);
325c3691 10027
3a5b9284
RH
10028 /* Locate the last statement in BODY. See c_end_compound_stmt
10029 about always returning a BIND_EXPR. */
10030 last_p = &BIND_EXPR_BODY (body);
10031 last = BIND_EXPR_BODY (body);
10032
10033 continue_searching:
325c3691
RH
10034 if (TREE_CODE (last) == STATEMENT_LIST)
10035 {
3a5b9284
RH
10036 tree_stmt_iterator i;
10037
10038 /* This can happen with degenerate cases like ({ }). No value. */
10039 if (!TREE_SIDE_EFFECTS (last))
10040 return body;
10041
10042 /* If we're supposed to generate side effects warnings, process
10043 all of the statements except the last. */
27f33b15 10044 if (warn_unused_value)
325c3691 10045 {
3a5b9284 10046 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
10047 {
10048 location_t tloc;
10049 tree t = tsi_stmt (i);
10050
10051 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10052 emit_side_effect_warnings (tloc, t);
10053 }
325c3691
RH
10054 }
10055 else
3a5b9284
RH
10056 i = tsi_last (last);
10057 last_p = tsi_stmt_ptr (i);
10058 last = *last_p;
325c3691
RH
10059 }
10060
3a5b9284
RH
10061 /* If the end of the list is exception related, then the list was split
10062 by a call to push_cleanup. Continue searching. */
10063 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10064 || TREE_CODE (last) == TRY_CATCH_EXPR)
10065 {
10066 last_p = &TREE_OPERAND (last, 0);
10067 last = *last_p;
10068 goto continue_searching;
10069 }
10070
26d8af35
JM
10071 if (last == error_mark_node)
10072 return last;
10073
3a5b9284
RH
10074 /* In the case that the BIND_EXPR is not necessary, return the
10075 expression out from inside it. */
26d8af35
JM
10076 if (last == BIND_EXPR_BODY (body)
10077 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10078 {
928c19bb
JM
10079 /* Even if this looks constant, do not allow it in a constant
10080 expression. */
e5a94231 10081 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10082 /* Do not warn if the return value of a statement expression is
10083 unused. */
928c19bb 10084 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10085 return last;
10086 }
325c3691
RH
10087
10088 /* Extract the type of said expression. */
10089 type = TREE_TYPE (last);
325c3691 10090
3a5b9284
RH
10091 /* If we're not returning a value at all, then the BIND_EXPR that
10092 we already have is a fine expression to return. */
10093 if (!type || VOID_TYPE_P (type))
10094 return body;
10095
10096 /* Now that we've located the expression containing the value, it seems
10097 silly to make voidify_wrapper_expr repeat the process. Create a
10098 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10099 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10100
10101 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10102 tree_expr_nonnegative_p giving up immediately. */
10103 val = last;
10104 if (TREE_CODE (val) == NOP_EXPR
10105 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10106 val = TREE_OPERAND (val, 0);
10107
53fb4de3 10108 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10109 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10110
c2255bc4
AH
10111 {
10112 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10113 SET_EXPR_LOCATION (t, loc);
10114 return t;
10115 }
325c3691
RH
10116}
10117\f
10118/* Begin and end compound statements. This is as simple as pushing
10119 and popping new statement lists from the tree. */
10120
10121tree
10122c_begin_compound_stmt (bool do_scope)
10123{
10124 tree stmt = push_stmt_list ();
10125 if (do_scope)
4dfa0342 10126 push_scope ();
325c3691
RH
10127 return stmt;
10128}
10129
c2255bc4
AH
10130/* End a compound statement. STMT is the statement. LOC is the
10131 location of the compound statement-- this is usually the location
10132 of the opening brace. */
10133
325c3691 10134tree
c2255bc4 10135c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10136{
10137 tree block = NULL;
10138
10139 if (do_scope)
10140 {
10141 if (c_dialect_objc ())
10142 objc_clear_super_receiver ();
10143 block = pop_scope ();
10144 }
10145
10146 stmt = pop_stmt_list (stmt);
c2255bc4 10147 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10148
10149 /* If this compound statement is nested immediately inside a statement
10150 expression, then force a BIND_EXPR to be created. Otherwise we'll
10151 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10152 STATEMENT_LISTs merge, and thus we can lose track of what statement
10153 was really last. */
38e01f9e 10154 if (building_stmt_list_p ()
325c3691
RH
10155 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10156 && TREE_CODE (stmt) != BIND_EXPR)
10157 {
53fb4de3 10158 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10159 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10160 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10161 }
10162
10163 return stmt;
10164}
5a508662
RH
10165
10166/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10167 when the current scope is exited. EH_ONLY is true when this is not
10168 meant to apply to normal control flow transfer. */
10169
10170void
c2255bc4 10171push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10172{
3a5b9284
RH
10173 enum tree_code code;
10174 tree stmt, list;
10175 bool stmt_expr;
10176
10177 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10178 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10179 add_stmt (stmt);
3a5b9284
RH
10180 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10181 list = push_stmt_list ();
10182 TREE_OPERAND (stmt, 0) = list;
10183 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10184}
325c3691 10185\f
3e4093b6
RS
10186/* Build a binary-operation expression without default conversions.
10187 CODE is the kind of expression to build.
ba47d38d 10188 LOCATION is the operator's location.
3e4093b6
RS
10189 This function differs from `build' in several ways:
10190 the data type of the result is computed and recorded in it,
10191 warnings are generated if arg data types are invalid,
10192 special handling for addition and subtraction of pointers is known,
10193 and some optimization is done (operations on narrow ints
10194 are done in the narrower type when that gives the same result).
10195 Constant folding is also done before the result is returned.
de520661 10196
3e4093b6
RS
10197 Note that the operands will never have enumeral types, or function
10198 or array types, because either they will have the default conversions
10199 performed or they have both just been converted to some other type in which
10200 the arithmetic is to be done. */
10201
10202tree
ba47d38d
AH
10203build_binary_op (location_t location, enum tree_code code,
10204 tree orig_op0, tree orig_op1, int convert_p)
de520661 10205{
8ce94e44
JM
10206 tree type0, type1, orig_type0, orig_type1;
10207 tree eptype;
3e4093b6
RS
10208 enum tree_code code0, code1;
10209 tree op0, op1;
c9f9eb5d 10210 tree ret = error_mark_node;
4de67c26 10211 const char *invalid_op_diag;
4d84fe7c 10212 bool op0_int_operands, op1_int_operands;
928c19bb 10213 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10214
3e4093b6
RS
10215 /* Expression code to give to the expression when it is built.
10216 Normally this is CODE, which is what the caller asked for,
10217 but in some special cases we change it. */
10218 enum tree_code resultcode = code;
8b6a5902 10219
3e4093b6
RS
10220 /* Data type in which the computation is to be performed.
10221 In the simplest cases this is the common type of the arguments. */
10222 tree result_type = NULL;
10223
8ce94e44
JM
10224 /* When the computation is in excess precision, the type of the
10225 final EXCESS_PRECISION_EXPR. */
2d2e923f 10226 tree semantic_result_type = NULL;
8ce94e44 10227
3e4093b6
RS
10228 /* Nonzero means operands have already been type-converted
10229 in whatever way is necessary.
10230 Zero means they need to be converted to RESULT_TYPE. */
10231 int converted = 0;
10232
10233 /* Nonzero means create the expression with this type, rather than
10234 RESULT_TYPE. */
10235 tree build_type = 0;
10236
10237 /* Nonzero means after finally constructing the expression
10238 convert it to this type. */
10239 tree final_type = 0;
10240
10241 /* Nonzero if this is an operation like MIN or MAX which can
10242 safely be computed in short if both args are promoted shorts.
10243 Also implies COMMON.
10244 -1 indicates a bitwise operation; this makes a difference
10245 in the exact conditions for when it is safe to do the operation
10246 in a narrower mode. */
10247 int shorten = 0;
10248
10249 /* Nonzero if this is a comparison operation;
10250 if both args are promoted shorts, compare the original shorts.
10251 Also implies COMMON. */
10252 int short_compare = 0;
10253
10254 /* Nonzero if this is a right-shift operation, which can be computed on the
10255 original short and then promoted if the operand is a promoted short. */
10256 int short_shift = 0;
10257
10258 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10259 int common = 0;
10260
58393038
ZL
10261 /* True means types are compatible as far as ObjC is concerned. */
10262 bool objc_ok;
10263
8ce94e44
JM
10264 /* True means this is an arithmetic operation that may need excess
10265 precision. */
10266 bool may_need_excess_precision;
10267
180f8dbb
JM
10268 /* True means this is a boolean operation that converts both its
10269 operands to truth-values. */
10270 bool boolean_op = false;
10271
de5a5fa1
MP
10272 /* Remember whether we're doing / or %. */
10273 bool doing_div_or_mod = false;
10274
10275 /* Remember whether we're doing << or >>. */
10276 bool doing_shift = false;
10277
10278 /* Tree holding instrumentation expression. */
10279 tree instrument_expr = NULL;
10280
ba47d38d
AH
10281 if (location == UNKNOWN_LOCATION)
10282 location = input_location;
10283
4d84fe7c
JM
10284 op0 = orig_op0;
10285 op1 = orig_op1;
10286
10287 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10288 if (op0_int_operands)
10289 op0 = remove_c_maybe_const_expr (op0);
10290 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10291 if (op1_int_operands)
10292 op1 = remove_c_maybe_const_expr (op1);
10293 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10294 if (int_operands)
10295 {
10296 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10297 && TREE_CODE (orig_op1) == INTEGER_CST);
10298 int_const = (int_const_or_overflow
10299 && !TREE_OVERFLOW (orig_op0)
10300 && !TREE_OVERFLOW (orig_op1));
10301 }
10302 else
10303 int_const = int_const_or_overflow = false;
10304
0e3a99ae 10305 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10306 if (convert_p
10307 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10308 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10309 {
4d84fe7c
JM
10310 op0 = default_conversion (op0);
10311 op1 = default_conversion (op1);
790e9490
RS
10312 }
10313
36536d79
BI
10314 /* When Cilk Plus is enabled and there are array notations inside op0, then
10315 we check to see if there are builtin array notation functions. If
10316 so, then we take on the type of the array notation inside it. */
b72271b9 10317 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10318 orig_type0 = type0 = find_correct_array_notation_type (op0);
10319 else
10320 orig_type0 = type0 = TREE_TYPE (op0);
10321
b72271b9 10322 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10323 orig_type1 = type1 = find_correct_array_notation_type (op1);
10324 else
10325 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10326
3e4093b6
RS
10327 /* The expression codes of the data types of the arguments tell us
10328 whether the arguments are integers, floating, pointers, etc. */
10329 code0 = TREE_CODE (type0);
10330 code1 = TREE_CODE (type1);
10331
10332 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10333 STRIP_TYPE_NOPS (op0);
10334 STRIP_TYPE_NOPS (op1);
10335
10336 /* If an error was already reported for one of the arguments,
10337 avoid reporting another error. */
10338
10339 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10340 return error_mark_node;
10341
4de67c26
JM
10342 if ((invalid_op_diag
10343 = targetm.invalid_binary_op (code, type0, type1)))
10344 {
ba47d38d 10345 error_at (location, invalid_op_diag);
4de67c26
JM
10346 return error_mark_node;
10347 }
10348
8ce94e44
JM
10349 switch (code)
10350 {
10351 case PLUS_EXPR:
10352 case MINUS_EXPR:
10353 case MULT_EXPR:
10354 case TRUNC_DIV_EXPR:
10355 case CEIL_DIV_EXPR:
10356 case FLOOR_DIV_EXPR:
10357 case ROUND_DIV_EXPR:
10358 case EXACT_DIV_EXPR:
10359 may_need_excess_precision = true;
10360 break;
10361 default:
10362 may_need_excess_precision = false;
10363 break;
10364 }
10365 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10366 {
10367 op0 = TREE_OPERAND (op0, 0);
10368 type0 = TREE_TYPE (op0);
10369 }
10370 else if (may_need_excess_precision
10371 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10372 {
10373 type0 = eptype;
10374 op0 = convert (eptype, op0);
10375 }
10376 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10377 {
10378 op1 = TREE_OPERAND (op1, 0);
10379 type1 = TREE_TYPE (op1);
10380 }
10381 else if (may_need_excess_precision
10382 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10383 {
10384 type1 = eptype;
10385 op1 = convert (eptype, op1);
10386 }
10387
58393038
ZL
10388 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10389
0e3a99ae
AS
10390 /* In case when one of the operands of the binary operation is
10391 a vector and another is a scalar -- convert scalar to vector. */
10392 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10393 {
a212e43f
MG
10394 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10395 true);
f90e8e2e 10396
0e3a99ae
AS
10397 switch (convert_flag)
10398 {
10399 case stv_error:
10400 return error_mark_node;
10401 case stv_firstarg:
10402 {
10403 bool maybe_const = true;
10404 tree sc;
10405 sc = c_fully_fold (op0, false, &maybe_const);
10406 sc = save_expr (sc);
10407 sc = convert (TREE_TYPE (type1), sc);
10408 op0 = build_vector_from_val (type1, sc);
10409 if (!maybe_const)
10410 op0 = c_wrap_maybe_const (op0, true);
10411 orig_type0 = type0 = TREE_TYPE (op0);
10412 code0 = TREE_CODE (type0);
10413 converted = 1;
10414 break;
10415 }
10416 case stv_secondarg:
10417 {
10418 bool maybe_const = true;
10419 tree sc;
10420 sc = c_fully_fold (op1, false, &maybe_const);
10421 sc = save_expr (sc);
10422 sc = convert (TREE_TYPE (type0), sc);
10423 op1 = build_vector_from_val (type0, sc);
10424 if (!maybe_const)
54b9f838 10425 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10426 orig_type1 = type1 = TREE_TYPE (op1);
10427 code1 = TREE_CODE (type1);
10428 converted = 1;
10429 break;
10430 }
10431 default:
10432 break;
10433 }
10434 }
10435
3e4093b6 10436 switch (code)
de520661 10437 {
3e4093b6
RS
10438 case PLUS_EXPR:
10439 /* Handle the pointer + int case. */
10440 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10441 {
db3927fb 10442 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10443 goto return_build_binary_op;
10444 }
3e4093b6 10445 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10446 {
db3927fb 10447 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10448 goto return_build_binary_op;
10449 }
fe67cf58 10450 else
3e4093b6
RS
10451 common = 1;
10452 break;
400fbf9f 10453
3e4093b6
RS
10454 case MINUS_EXPR:
10455 /* Subtraction of two similar pointers.
10456 We must subtract them as integers, then divide by object size. */
10457 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10458 && comp_target_types (location, type0, type1))
c9f9eb5d 10459 {
db3927fb 10460 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10461 goto return_build_binary_op;
10462 }
3e4093b6
RS
10463 /* Handle pointer minus int. Just like pointer plus int. */
10464 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10465 {
db3927fb 10466 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10467 goto return_build_binary_op;
10468 }
3e4093b6
RS
10469 else
10470 common = 1;
10471 break;
8b6a5902 10472
3e4093b6
RS
10473 case MULT_EXPR:
10474 common = 1;
10475 break;
10476
10477 case TRUNC_DIV_EXPR:
10478 case CEIL_DIV_EXPR:
10479 case FLOOR_DIV_EXPR:
10480 case ROUND_DIV_EXPR:
10481 case EXACT_DIV_EXPR:
de5a5fa1 10482 doing_div_or_mod = true;
c9f9eb5d 10483 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10484
10485 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10486 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10487 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10488 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10489 || code1 == FIXED_POINT_TYPE
3e4093b6 10490 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10491 {
5bed876a
AH
10492 enum tree_code tcode0 = code0, tcode1 = code1;
10493
3a021db2 10494 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10495 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10496 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10497 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10498
ab22c1fa
CF
10499 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10500 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10501 resultcode = RDIV_EXPR;
10502 else
10503 /* Although it would be tempting to shorten always here, that
10504 loses on some targets, since the modulo instruction is
10505 undefined if the quotient can't be represented in the
10506 computation mode. We shorten only if unsigned or if
10507 dividing by something we know != -1. */
8df83eae 10508 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10509 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10510 && !integer_all_onesp (op1)));
3e4093b6
RS
10511 common = 1;
10512 }
10513 break;
de520661 10514
3e4093b6 10515 case BIT_AND_EXPR:
3e4093b6
RS
10516 case BIT_IOR_EXPR:
10517 case BIT_XOR_EXPR:
10518 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10519 shorten = -1;
9ef0c8d9
AP
10520 /* Allow vector types which are not floating point types. */
10521 else if (code0 == VECTOR_TYPE
10522 && code1 == VECTOR_TYPE
10523 && !VECTOR_FLOAT_TYPE_P (type0)
10524 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10525 common = 1;
10526 break;
10527
10528 case TRUNC_MOD_EXPR:
10529 case FLOOR_MOD_EXPR:
de5a5fa1 10530 doing_div_or_mod = true;
c9f9eb5d 10531 warn_for_div_by_zero (location, op1);
de520661 10532
5cfd5d9b
AP
10533 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10534 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10535 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10536 common = 1;
10537 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10538 {
10539 /* Although it would be tempting to shorten always here, that loses
10540 on some targets, since the modulo instruction is undefined if the
10541 quotient can't be represented in the computation mode. We shorten
10542 only if unsigned or if dividing by something we know != -1. */
8df83eae 10543 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10544 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10545 && !integer_all_onesp (op1)));
3e4093b6
RS
10546 common = 1;
10547 }
10548 break;
de520661 10549
3e4093b6
RS
10550 case TRUTH_ANDIF_EXPR:
10551 case TRUTH_ORIF_EXPR:
10552 case TRUTH_AND_EXPR:
10553 case TRUTH_OR_EXPR:
10554 case TRUTH_XOR_EXPR:
10555 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10556 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10557 || code0 == FIXED_POINT_TYPE)
3e4093b6 10558 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10559 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10560 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10561 {
10562 /* Result of these operations is always an int,
10563 but that does not mean the operands should be
10564 converted to ints! */
10565 result_type = integer_type_node;
a27d595d
JM
10566 if (op0_int_operands)
10567 {
10568 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10569 op0 = remove_c_maybe_const_expr (op0);
10570 }
10571 else
10572 op0 = c_objc_common_truthvalue_conversion (location, op0);
10573 if (op1_int_operands)
10574 {
10575 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10576 op1 = remove_c_maybe_const_expr (op1);
10577 }
10578 else
10579 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10580 converted = 1;
180f8dbb 10581 boolean_op = true;
3e4093b6 10582 }
928c19bb
JM
10583 if (code == TRUTH_ANDIF_EXPR)
10584 {
10585 int_const_or_overflow = (int_operands
10586 && TREE_CODE (orig_op0) == INTEGER_CST
10587 && (op0 == truthvalue_false_node
10588 || TREE_CODE (orig_op1) == INTEGER_CST));
10589 int_const = (int_const_or_overflow
10590 && !TREE_OVERFLOW (orig_op0)
10591 && (op0 == truthvalue_false_node
10592 || !TREE_OVERFLOW (orig_op1)));
10593 }
10594 else if (code == TRUTH_ORIF_EXPR)
10595 {
10596 int_const_or_overflow = (int_operands
10597 && TREE_CODE (orig_op0) == INTEGER_CST
10598 && (op0 == truthvalue_true_node
10599 || TREE_CODE (orig_op1) == INTEGER_CST));
10600 int_const = (int_const_or_overflow
10601 && !TREE_OVERFLOW (orig_op0)
10602 && (op0 == truthvalue_true_node
10603 || !TREE_OVERFLOW (orig_op1)));
10604 }
3e4093b6 10605 break;
eba80994 10606
3e4093b6
RS
10607 /* Shift operations: result has same type as first operand;
10608 always convert second operand to int.
10609 Also set SHORT_SHIFT if shifting rightward. */
de520661 10610
3e4093b6 10611 case RSHIFT_EXPR:
f87bd04b
AS
10612 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10613 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10614 {
10615 result_type = type0;
10616 converted = 1;
10617 }
10618 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10619 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10620 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10621 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10622 {
10623 result_type = type0;
10624 converted = 1;
10625 }
10626 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10627 && code1 == INTEGER_TYPE)
3e4093b6 10628 {
de5a5fa1 10629 doing_shift = true;
928c19bb 10630 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10631 {
3e4093b6 10632 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10633 {
10634 int_const = false;
7d882b83 10635 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10636 warning_at (location, OPT_Wshift_count_negative,
10637 "right shift count is negative");
928c19bb 10638 }
3e4093b6 10639 else
bbb818c6 10640 {
3f75a254 10641 if (!integer_zerop (op1))
3e4093b6
RS
10642 short_shift = 1;
10643
10644 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10645 {
10646 int_const = false;
7d882b83 10647 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10648 warning_at (location, OPT_Wshift_count_overflow,
10649 "right shift count >= width of type");
928c19bb 10650 }
bbb818c6 10651 }
b62acd60 10652 }
de520661 10653
3e4093b6
RS
10654 /* Use the type of the value to be shifted. */
10655 result_type = type0;
3e4093b6
RS
10656 /* Avoid converting op1 to result_type later. */
10657 converted = 1;
400fbf9f 10658 }
3e4093b6 10659 break;
253b6b82 10660
3e4093b6 10661 case LSHIFT_EXPR:
f87bd04b
AS
10662 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10663 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10664 {
10665 result_type = type0;
10666 converted = 1;
10667 }
10668 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10669 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10670 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10671 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10672 {
10673 result_type = type0;
10674 converted = 1;
10675 }
10676 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10677 && code1 == INTEGER_TYPE)
3e4093b6 10678 {
de5a5fa1 10679 doing_shift = true;
0173bd2a
MP
10680 if (TREE_CODE (op0) == INTEGER_CST
10681 && tree_int_cst_sgn (op0) < 0)
10682 {
10683 /* Don't reject a left shift of a negative value in a context
10684 where a constant expression is needed in C90. */
10685 if (flag_isoc99)
10686 int_const = false;
10687 if (c_inhibit_evaluation_warnings == 0)
10688 warning_at (location, OPT_Wshift_negative_value,
10689 "left shift of negative value");
10690 }
928c19bb 10691 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10692 {
3e4093b6 10693 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10694 {
10695 int_const = false;
7d882b83 10696 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10697 warning_at (location, OPT_Wshift_count_negative,
10698 "left shift count is negative");
928c19bb 10699 }
de520661 10700
3e4093b6 10701 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10702 {
10703 int_const = false;
7d882b83 10704 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10705 warning_at (location, OPT_Wshift_count_overflow,
10706 "left shift count >= width of type");
928c19bb 10707 }
94ba5069 10708 }
de520661 10709
3e4093b6
RS
10710 /* Use the type of the value to be shifted. */
10711 result_type = type0;
3e4093b6
RS
10712 /* Avoid converting op1 to result_type later. */
10713 converted = 1;
400fbf9f 10714 }
3e4093b6 10715 break;
de520661 10716
3e4093b6
RS
10717 case EQ_EXPR:
10718 case NE_EXPR:
d246ab4f
AS
10719 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10720 {
10721 tree intt;
0af94e6f 10722 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10723 {
10724 error_at (location, "comparing vectors with different "
10725 "element types");
10726 return error_mark_node;
10727 }
10728
10729 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10730 {
10731 error_at (location, "comparing vectors with different "
10732 "number of elements");
10733 return error_mark_node;
10734 }
10735
10736 /* Always construct signed integer vector type. */
10737 intt = c_common_type_for_size (GET_MODE_BITSIZE
10738 (TYPE_MODE (TREE_TYPE (type0))), 0);
10739 result_type = build_opaque_vector_type (intt,
10740 TYPE_VECTOR_SUBPARTS (type0));
10741 converted = 1;
10742 break;
10743 }
ae311566 10744 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10745 warning_at (location,
10746 OPT_Wfloat_equal,
10747 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10748 /* Result of comparison is always int,
10749 but don't convert the args to int! */
10750 build_type = integer_type_node;
10751 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10752 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10753 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10754 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10755 short_compare = 1;
637f1455
SZ
10756 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10757 {
10758 if (TREE_CODE (op0) == ADDR_EXPR
10759 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10760 {
10761 if (code == EQ_EXPR)
10762 warning_at (location,
10763 OPT_Waddress,
10764 "the comparison will always evaluate as %<false%> "
10765 "for the address of %qD will never be NULL",
10766 TREE_OPERAND (op0, 0));
10767 else
10768 warning_at (location,
10769 OPT_Waddress,
10770 "the comparison will always evaluate as %<true%> "
10771 "for the address of %qD will never be NULL",
10772 TREE_OPERAND (op0, 0));
10773 }
10774 result_type = type0;
10775 }
10776 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10777 {
10778 if (TREE_CODE (op1) == ADDR_EXPR
10779 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10780 {
10781 if (code == EQ_EXPR)
10782 warning_at (location,
f90e8e2e 10783 OPT_Waddress,
637f1455
SZ
10784 "the comparison will always evaluate as %<false%> "
10785 "for the address of %qD will never be NULL",
10786 TREE_OPERAND (op1, 0));
10787 else
10788 warning_at (location,
10789 OPT_Waddress,
10790 "the comparison will always evaluate as %<true%> "
10791 "for the address of %qD will never be NULL",
10792 TREE_OPERAND (op1, 0));
10793 }
10794 result_type = type1;
10795 }
3e4093b6
RS
10796 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10797 {
10798 tree tt0 = TREE_TYPE (type0);
10799 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10800 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10801 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10802 addr_space_t as_common = ADDR_SPACE_GENERIC;
10803
3e4093b6
RS
10804 /* Anything compares with void *. void * compares with anything.
10805 Otherwise, the targets must be compatible
10806 and both must be object or both incomplete. */
744aa42f 10807 if (comp_target_types (location, type0, type1))
10bc1b1b 10808 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10809 else if (!addr_space_superset (as0, as1, &as_common))
10810 {
10811 error_at (location, "comparison of pointers to "
10812 "disjoint address spaces");
10813 return error_mark_node;
10814 }
267bac10 10815 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10816 {
36c5e70a 10817 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10818 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10819 "comparison of %<void *%> with function pointer");
ee2990e7 10820 }
267bac10 10821 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10822 {
36c5e70a 10823 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10824 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10825 "comparison of %<void *%> with function pointer");
e6834654 10826 }
3e4093b6 10827 else
58393038
ZL
10828 /* Avoid warning about the volatile ObjC EH puts on decls. */
10829 if (!objc_ok)
ba47d38d 10830 pedwarn (location, 0,
509c9d60 10831 "comparison of distinct pointer types lacks a cast");
e6834654 10832
3e4093b6 10833 if (result_type == NULL_TREE)
36c5e70a
BE
10834 {
10835 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10836 result_type = build_pointer_type
10837 (build_qualified_type (void_type_node, qual));
10838 }
e6834654 10839 }
3e4093b6 10840 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10841 {
3e4093b6 10842 result_type = type0;
ba47d38d 10843 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10844 }
3e4093b6 10845 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10846 {
3e4093b6 10847 result_type = type1;
ba47d38d 10848 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10849 }
04159acf
MP
10850 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10851 || truth_value_p (TREE_CODE (orig_op0)))
10852 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10853 || truth_value_p (TREE_CODE (orig_op1))))
10854 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10855 break;
8b6a5902 10856
3e4093b6
RS
10857 case LE_EXPR:
10858 case GE_EXPR:
10859 case LT_EXPR:
10860 case GT_EXPR:
d246ab4f
AS
10861 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10862 {
10863 tree intt;
0af94e6f 10864 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10865 {
10866 error_at (location, "comparing vectors with different "
10867 "element types");
10868 return error_mark_node;
10869 }
10870
10871 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10872 {
10873 error_at (location, "comparing vectors with different "
10874 "number of elements");
10875 return error_mark_node;
10876 }
10877
10878 /* Always construct signed integer vector type. */
10879 intt = c_common_type_for_size (GET_MODE_BITSIZE
10880 (TYPE_MODE (TREE_TYPE (type0))), 0);
10881 result_type = build_opaque_vector_type (intt,
10882 TYPE_VECTOR_SUBPARTS (type0));
10883 converted = 1;
10884 break;
10885 }
3e4093b6 10886 build_type = integer_type_node;
ab22c1fa
CF
10887 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10888 || code0 == FIXED_POINT_TYPE)
10889 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10890 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10891 short_compare = 1;
10892 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10893 {
36c5e70a
BE
10894 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10895 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10896 addr_space_t as_common;
10897
744aa42f 10898 if (comp_target_types (location, type0, type1))
3e4093b6 10899 {
10bc1b1b 10900 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10901 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10902 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10903 pedwarn (location, 0,
509c9d60 10904 "comparison of complete and incomplete pointers");
fcf73884 10905 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10906 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10907 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10908 else if (null_pointer_constant_p (orig_op0)
10909 || null_pointer_constant_p (orig_op1))
10910 warning_at (location, OPT_Wextra,
10911 "ordered comparison of pointer with null pointer");
10912
3e4093b6 10913 }
36c5e70a
BE
10914 else if (!addr_space_superset (as0, as1, &as_common))
10915 {
10916 error_at (location, "comparison of pointers to "
10917 "disjoint address spaces");
10918 return error_mark_node;
10919 }
3e4093b6
RS
10920 else
10921 {
36c5e70a
BE
10922 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10923 result_type = build_pointer_type
10924 (build_qualified_type (void_type_node, qual));
ba47d38d 10925 pedwarn (location, 0,
509c9d60 10926 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10927 }
10928 }
6aa3c60d 10929 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10930 {
10931 result_type = type0;
fcf73884 10932 if (pedantic)
c1771a20 10933 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10934 "ordered comparison of pointer with integer zero");
10935 else if (extra_warnings)
ba47d38d 10936 warning_at (location, OPT_Wextra,
d42ba3b8 10937 "ordered comparison of pointer with integer zero");
3e4093b6 10938 }
6aa3c60d 10939 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10940 {
10941 result_type = type1;
d42ba3b8 10942 if (pedantic)
c1771a20 10943 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10944 "ordered comparison of pointer with integer zero");
10945 else if (extra_warnings)
10946 warning_at (location, OPT_Wextra,
10947 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10948 }
10949 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10950 {
10951 result_type = type0;
ba47d38d 10952 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10953 }
10954 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10955 {
10956 result_type = type1;
ba47d38d 10957 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10958 }
04159acf
MP
10959 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10960 || truth_value_p (TREE_CODE (orig_op0)))
10961 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10962 || truth_value_p (TREE_CODE (orig_op1))))
10963 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10964 break;
64094f6a 10965
3e4093b6 10966 default:
37b2f290 10967 gcc_unreachable ();
c9fe6f9f 10968 }
8f17b5c5 10969
e57e265b
PB
10970 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10971 return error_mark_node;
10972
5bed876a
AH
10973 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10974 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10975 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10976 {
ba47d38d 10977 binary_op_error (location, code, type0, type1);
5bed876a
AH
10978 return error_mark_node;
10979 }
10980
3e4093b6 10981 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10982 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10983 &&
10984 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10985 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10986 {
2ca862e9
JM
10987 bool first_complex = (code0 == COMPLEX_TYPE);
10988 bool second_complex = (code1 == COMPLEX_TYPE);
10989 int none_complex = (!first_complex && !second_complex);
39b726dd 10990
3e4093b6 10991 if (shorten || common || short_compare)
3bf6bfcc
JJ
10992 {
10993 result_type = c_common_type (type0, type1);
0a0b3574
MM
10994 do_warn_double_promotion (result_type, type0, type1,
10995 "implicit conversion from %qT to %qT "
10996 "to match other operand of binary "
10997 "expression",
10998 location);
3bf6bfcc
JJ
10999 if (result_type == error_mark_node)
11000 return error_mark_node;
11001 }
400fbf9f 11002
2ca862e9
JM
11003 if (first_complex != second_complex
11004 && (code == PLUS_EXPR
11005 || code == MINUS_EXPR
11006 || code == MULT_EXPR
11007 || (code == TRUNC_DIV_EXPR && first_complex))
11008 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11009 && flag_signed_zeros)
11010 {
11011 /* An operation on mixed real/complex operands must be
11012 handled specially, but the language-independent code can
11013 more easily optimize the plain complex arithmetic if
11014 -fno-signed-zeros. */
11015 tree real_type = TREE_TYPE (result_type);
11016 tree real, imag;
11017 if (type0 != orig_type0 || type1 != orig_type1)
11018 {
11019 gcc_assert (may_need_excess_precision && common);
2d2e923f 11020 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11021 }
11022 if (first_complex)
11023 {
11024 if (TREE_TYPE (op0) != result_type)
68fca595 11025 op0 = convert_and_check (location, result_type, op0);
2ca862e9 11026 if (TREE_TYPE (op1) != real_type)
68fca595 11027 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
11028 }
11029 else
11030 {
11031 if (TREE_TYPE (op0) != real_type)
68fca595 11032 op0 = convert_and_check (location, real_type, op0);
2ca862e9 11033 if (TREE_TYPE (op1) != result_type)
68fca595 11034 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
11035 }
11036 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11037 return error_mark_node;
11038 if (first_complex)
11039 {
11040 op0 = c_save_expr (op0);
11041 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
11042 op0, 1);
11043 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
11044 op0, 1);
11045 switch (code)
11046 {
11047 case MULT_EXPR:
11048 case TRUNC_DIV_EXPR:
c4603e7c 11049 op1 = c_save_expr (op1);
2ca862e9
JM
11050 imag = build2 (resultcode, real_type, imag, op1);
11051 /* Fall through. */
11052 case PLUS_EXPR:
11053 case MINUS_EXPR:
11054 real = build2 (resultcode, real_type, real, op1);
11055 break;
11056 default:
11057 gcc_unreachable();
11058 }
11059 }
11060 else
11061 {
11062 op1 = c_save_expr (op1);
11063 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
11064 op1, 1);
11065 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
11066 op1, 1);
11067 switch (code)
11068 {
11069 case MULT_EXPR:
c4603e7c 11070 op0 = c_save_expr (op0);
2ca862e9
JM
11071 imag = build2 (resultcode, real_type, op0, imag);
11072 /* Fall through. */
11073 case PLUS_EXPR:
11074 real = build2 (resultcode, real_type, op0, real);
11075 break;
11076 case MINUS_EXPR:
11077 real = build2 (resultcode, real_type, op0, real);
11078 imag = build1 (NEGATE_EXPR, real_type, imag);
11079 break;
11080 default:
11081 gcc_unreachable();
11082 }
11083 }
11084 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11085 goto return_build_binary_op;
11086 }
11087
3e4093b6
RS
11088 /* For certain operations (which identify themselves by shorten != 0)
11089 if both args were extended from the same smaller type,
11090 do the arithmetic in that type and then extend.
400fbf9f 11091
3e4093b6
RS
11092 shorten !=0 and !=1 indicates a bitwise operation.
11093 For them, this optimization is safe only if
11094 both args are zero-extended or both are sign-extended.
11095 Otherwise, we might change the result.
11096 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11097 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 11098
3e4093b6
RS
11099 if (shorten && none_complex)
11100 {
3e4093b6 11101 final_type = result_type;
b8698a0f 11102 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 11103 shorten == -1);
3e4093b6 11104 }
88a3dbc1 11105
3e4093b6 11106 /* Shifts can be shortened if shifting right. */
2f6e4e97 11107
3e4093b6
RS
11108 if (short_shift)
11109 {
11110 int unsigned_arg;
11111 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 11112
3e4093b6 11113 final_type = result_type;
abe80e6d 11114
3e4093b6 11115 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 11116 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 11117
3e4093b6 11118 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 11119 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
11120 /* We can shorten only if the shift count is less than the
11121 number of bits in the smaller type size. */
11122 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11123 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 11124 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
11125 {
11126 /* Do an unsigned shift if the operand was zero-extended. */
11127 result_type
11128 = c_common_signed_or_unsigned_type (unsigned_arg,
11129 TREE_TYPE (arg0));
11130 /* Convert value-to-be-shifted to that type. */
11131 if (TREE_TYPE (op0) != result_type)
11132 op0 = convert (result_type, op0);
11133 converted = 1;
abe80e6d 11134 }
88a3dbc1
RK
11135 }
11136
3e4093b6
RS
11137 /* Comparison operations are shortened too but differently.
11138 They identify themselves by setting short_compare = 1. */
56cb9733 11139
3e4093b6
RS
11140 if (short_compare)
11141 {
11142 /* Don't write &op0, etc., because that would prevent op0
11143 from being kept in a register.
11144 Instead, make copies of the our local variables and
11145 pass the copies by reference, then copy them back afterward. */
11146 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11147 enum tree_code xresultcode = resultcode;
11148 tree val
393e8e8b
MP
11149 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11150 &xresultcode);
8f17b5c5 11151
3e4093b6 11152 if (val != 0)
c9f9eb5d
AH
11153 {
11154 ret = val;
11155 goto return_build_binary_op;
11156 }
8f17b5c5 11157
3e4093b6
RS
11158 op0 = xop0, op1 = xop1;
11159 converted = 1;
11160 resultcode = xresultcode;
8f17b5c5 11161
7d882b83 11162 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11163 {
11164 bool op0_maybe_const = true;
11165 bool op1_maybe_const = true;
11166 tree orig_op0_folded, orig_op1_folded;
11167
11168 if (in_late_binary_op)
11169 {
11170 orig_op0_folded = orig_op0;
11171 orig_op1_folded = orig_op1;
11172 }
11173 else
11174 {
11175 /* Fold for the sake of possible warnings, as in
11176 build_conditional_expr. This requires the
11177 "original" values to be folded, not just op0 and
11178 op1. */
f5178456 11179 c_inhibit_evaluation_warnings++;
928c19bb
JM
11180 op0 = c_fully_fold (op0, require_constant_value,
11181 &op0_maybe_const);
11182 op1 = c_fully_fold (op1, require_constant_value,
11183 &op1_maybe_const);
f5178456 11184 c_inhibit_evaluation_warnings--;
928c19bb
JM
11185 orig_op0_folded = c_fully_fold (orig_op0,
11186 require_constant_value,
11187 NULL);
11188 orig_op1_folded = c_fully_fold (orig_op1,
11189 require_constant_value,
11190 NULL);
11191 }
11192
11193 if (warn_sign_compare)
11194 warn_for_sign_compare (location, orig_op0_folded,
11195 orig_op1_folded, op0, op1,
11196 result_type, resultcode);
5c2f94b4 11197 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11198 {
11199 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11200 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11201 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11202 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11203 }
3e4093b6 11204 }
2ad1815d 11205 }
64094f6a 11206 }
64094f6a 11207
3e4093b6
RS
11208 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11209 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11210 Then the expression will be built.
11211 It will be given type FINAL_TYPE if that is nonzero;
11212 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11213
3e4093b6
RS
11214 if (!result_type)
11215 {
ba47d38d 11216 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11217 return error_mark_node;
11218 }
400fbf9f 11219
3e4093b6 11220 if (build_type == NULL_TREE)
8ce94e44
JM
11221 {
11222 build_type = result_type;
180f8dbb
JM
11223 if ((type0 != orig_type0 || type1 != orig_type1)
11224 && !boolean_op)
8ce94e44
JM
11225 {
11226 gcc_assert (may_need_excess_precision && common);
2d2e923f 11227 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11228 }
11229 }
400fbf9f 11230
2d2e923f
MLI
11231 if (!converted)
11232 {
68fca595
MP
11233 op0 = ep_convert_and_check (location, result_type, op0,
11234 semantic_result_type);
11235 op1 = ep_convert_and_check (location, result_type, op1,
11236 semantic_result_type);
2d2e923f
MLI
11237
11238 /* This can happen if one operand has a vector type, and the other
11239 has a different type. */
11240 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11241 return error_mark_node;
11242 }
11243
f8ed5150
MP
11244 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11245 | SANITIZE_FLOAT_DIVIDE))
802ac282 11246 && do_ubsan_in_current_function ()
de5a5fa1
MP
11247 && (doing_div_or_mod || doing_shift))
11248 {
11249 /* OP0 and/or OP1 might have side-effects. */
11250 op0 = c_save_expr (op0);
11251 op1 = c_save_expr (op1);
11252 op0 = c_fully_fold (op0, false, NULL);
11253 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11254 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11255 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11256 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11257 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11258 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11259 }
11260
c9f9eb5d 11261 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11262 if (int_const_or_overflow)
11263 ret = (require_constant_value
db3927fb
AH
11264 ? fold_build2_initializer_loc (location, resultcode, build_type,
11265 op0, op1)
11266 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11267 else
11268 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11269 if (final_type != 0)
11270 ret = convert (final_type, ret);
11271
11272 return_build_binary_op:
11273 gcc_assert (ret != error_mark_node);
928c19bb
JM
11274 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11275 ret = (int_operands
11276 ? note_integer_operands (ret)
11277 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11278 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11279 && !in_late_binary_op)
11280 ret = note_integer_operands (ret);
2d2e923f
MLI
11281 if (semantic_result_type)
11282 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11283 protected_set_expr_location (ret, location);
de5a5fa1 11284
a24d975c 11285 if (instrument_expr != NULL)
de5a5fa1
MP
11286 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11287 instrument_expr, ret);
11288
c9f9eb5d 11289 return ret;
400fbf9f 11290}
85498824
JM
11291
11292
11293/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11294 purpose. LOCATION is the source location for the expression. */
85498824
JM
11295
11296tree
ba47d38d 11297c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11298{
928c19bb
JM
11299 bool int_const, int_operands;
11300
85498824
JM
11301 switch (TREE_CODE (TREE_TYPE (expr)))
11302 {
11303 case ARRAY_TYPE:
ba47d38d 11304 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11305 return error_mark_node;
11306
11307 case RECORD_TYPE:
ba47d38d 11308 error_at (location, "used struct type value where scalar is required");
85498824
JM
11309 return error_mark_node;
11310
11311 case UNION_TYPE:
ba47d38d 11312 error_at (location, "used union type value where scalar is required");
85498824
JM
11313 return error_mark_node;
11314
04af8788
NP
11315 case VOID_TYPE:
11316 error_at (location, "void value not ignored as it ought to be");
11317 return error_mark_node;
11318
46bdb9cf
JM
11319 case FUNCTION_TYPE:
11320 gcc_unreachable ();
11321
d246ab4f
AS
11322 case VECTOR_TYPE:
11323 error_at (location, "used vector type where scalar is required");
11324 return error_mark_node;
11325
85498824
JM
11326 default:
11327 break;
11328 }
11329
928c19bb
JM
11330 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11331 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11332 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11333 {
11334 expr = remove_c_maybe_const_expr (expr);
11335 expr = build2 (NE_EXPR, integer_type_node, expr,
11336 convert (TREE_TYPE (expr), integer_zero_node));
11337 expr = note_integer_operands (expr);
11338 }
11339 else
11340 /* ??? Should we also give an error for vectors rather than leaving
11341 those to give errors later? */
11342 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11343
11344 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11345 {
11346 if (TREE_OVERFLOW (expr))
11347 return expr;
11348 else
11349 return note_integer_operands (expr);
11350 }
11351 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11352 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11353 return expr;
85498824 11354}
73f397d4
JM
11355\f
11356
11357/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11358 required. */
11359
11360tree
51eed280 11361c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11362{
11363 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11364 {
11365 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11366 /* Executing a compound literal inside a function reinitializes
11367 it. */
11368 if (!TREE_STATIC (decl))
11369 *se = true;
11370 return decl;
11371 }
11372 else
11373 return expr;
11374}
953ff289 11375\f
41dbbb37
TS
11376/* Generate OACC_PARALLEL, with CLAUSES and BLOCK as its compound
11377 statement. LOC is the location of the OACC_PARALLEL. */
11378
11379tree
11380c_finish_oacc_parallel (location_t loc, tree clauses, tree block)
11381{
11382 tree stmt;
11383
11384 block = c_end_compound_stmt (loc, block, true);
11385
11386 stmt = make_node (OACC_PARALLEL);
11387 TREE_TYPE (stmt) = void_type_node;
11388 OACC_PARALLEL_CLAUSES (stmt) = clauses;
11389 OACC_PARALLEL_BODY (stmt) = block;
11390 SET_EXPR_LOCATION (stmt, loc);
11391
11392 return add_stmt (stmt);
11393}
11394
11395/* Generate OACC_KERNELS, with CLAUSES and BLOCK as its compound
11396 statement. LOC is the location of the OACC_KERNELS. */
11397
11398tree
11399c_finish_oacc_kernels (location_t loc, tree clauses, tree block)
11400{
11401 tree stmt;
11402
11403 block = c_end_compound_stmt (loc, block, true);
11404
11405 stmt = make_node (OACC_KERNELS);
11406 TREE_TYPE (stmt) = void_type_node;
11407 OACC_KERNELS_CLAUSES (stmt) = clauses;
11408 OACC_KERNELS_BODY (stmt) = block;
11409 SET_EXPR_LOCATION (stmt, loc);
11410
11411 return add_stmt (stmt);
11412}
11413
11414/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11415 statement. LOC is the location of the OACC_DATA. */
11416
11417tree
11418c_finish_oacc_data (location_t loc, tree clauses, tree block)
11419{
11420 tree stmt;
11421
11422 block = c_end_compound_stmt (loc, block, true);
11423
11424 stmt = make_node (OACC_DATA);
11425 TREE_TYPE (stmt) = void_type_node;
11426 OACC_DATA_CLAUSES (stmt) = clauses;
11427 OACC_DATA_BODY (stmt) = block;
11428 SET_EXPR_LOCATION (stmt, loc);
11429
11430 return add_stmt (stmt);
11431}
11432
c0220ea4 11433/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11434
11435tree
11436c_begin_omp_parallel (void)
11437{
11438 tree block;
11439
11440 keep_next_level ();
11441 block = c_begin_compound_stmt (true);
11442
11443 return block;
11444}
11445
c2255bc4
AH
11446/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11447 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11448
953ff289 11449tree
c2255bc4 11450c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11451{
11452 tree stmt;
11453
c2255bc4 11454 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11455
11456 stmt = make_node (OMP_PARALLEL);
11457 TREE_TYPE (stmt) = void_type_node;
11458 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11459 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11460 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11461
11462 return add_stmt (stmt);
11463}
11464
a68ab351
JJ
11465/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11466
11467tree
11468c_begin_omp_task (void)
11469{
11470 tree block;
11471
11472 keep_next_level ();
11473 block = c_begin_compound_stmt (true);
11474
11475 return block;
11476}
11477
c2255bc4
AH
11478/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11479 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11480
11481tree
c2255bc4 11482c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11483{
11484 tree stmt;
11485
c2255bc4 11486 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11487
11488 stmt = make_node (OMP_TASK);
11489 TREE_TYPE (stmt) = void_type_node;
11490 OMP_TASK_CLAUSES (stmt) = clauses;
11491 OMP_TASK_BODY (stmt) = block;
c2255bc4 11492 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11493
11494 return add_stmt (stmt);
11495}
11496
acf0174b
JJ
11497/* Generate GOMP_cancel call for #pragma omp cancel. */
11498
11499void
11500c_finish_omp_cancel (location_t loc, tree clauses)
11501{
11502 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11503 int mask = 0;
11504 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11505 mask = 1;
11506 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11507 mask = 2;
11508 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11509 mask = 4;
11510 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11511 mask = 8;
11512 else
11513 {
11514 error_at (loc, "%<#pragma omp cancel must specify one of "
11515 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11516 "clauses");
11517 return;
11518 }
11519 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11520 if (ifc != NULL_TREE)
11521 {
11522 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11523 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11524 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11525 build_zero_cst (type));
11526 }
11527 else
11528 ifc = boolean_true_node;
11529 tree stmt = build_call_expr_loc (loc, fn, 2,
11530 build_int_cst (integer_type_node, mask),
11531 ifc);
11532 add_stmt (stmt);
11533}
11534
11535/* Generate GOMP_cancellation_point call for
11536 #pragma omp cancellation point. */
11537
11538void
11539c_finish_omp_cancellation_point (location_t loc, tree clauses)
11540{
11541 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11542 int mask = 0;
11543 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11544 mask = 1;
11545 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11546 mask = 2;
11547 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11548 mask = 4;
11549 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11550 mask = 8;
11551 else
11552 {
11553 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11554 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11555 "clauses");
11556 return;
11557 }
11558 tree stmt = build_call_expr_loc (loc, fn, 1,
11559 build_int_cst (integer_type_node, mask));
11560 add_stmt (stmt);
11561}
11562
11563/* Helper function for handle_omp_array_sections. Called recursively
11564 to handle multiple array-section-subscripts. C is the clause,
11565 T current expression (initially OMP_CLAUSE_DECL), which is either
11566 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11567 expression if specified, TREE_VALUE length expression if specified,
11568 TREE_CHAIN is what it has been specified after, or some decl.
11569 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11570 set to true if any of the array-section-subscript could have length
11571 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11572 first array-section-subscript which is known not to have length
11573 of one. Given say:
11574 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11575 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11576 all are or may have length of 1, array-section-subscript [:2] is the
11577 first one knonwn not to have length 1. For array-section-subscript
11578 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11579 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11580 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11581 case though, as some lengths could be zero. */
11582
11583static tree
11584handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11585 bool &maybe_zero_len, unsigned int &first_non_one)
11586{
11587 tree ret, low_bound, length, type;
11588 if (TREE_CODE (t) != TREE_LIST)
11589 {
7a0ca710 11590 if (error_operand_p (t))
acf0174b
JJ
11591 return error_mark_node;
11592 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11593 {
11594 if (DECL_P (t))
11595 error_at (OMP_CLAUSE_LOCATION (c),
11596 "%qD is not a variable in %qs clause", t,
11597 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11598 else
11599 error_at (OMP_CLAUSE_LOCATION (c),
11600 "%qE is not a variable in %qs clause", t,
11601 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11602 return error_mark_node;
11603 }
11604 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11605 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11606 {
11607 error_at (OMP_CLAUSE_LOCATION (c),
11608 "%qD is threadprivate variable in %qs clause", t,
11609 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11610 return error_mark_node;
11611 }
11612 return t;
11613 }
11614
11615 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11616 maybe_zero_len, first_non_one);
11617 if (ret == error_mark_node || ret == NULL_TREE)
11618 return ret;
11619
11620 type = TREE_TYPE (ret);
11621 low_bound = TREE_PURPOSE (t);
11622 length = TREE_VALUE (t);
11623
11624 if (low_bound == error_mark_node || length == error_mark_node)
11625 return error_mark_node;
11626
11627 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11628 {
11629 error_at (OMP_CLAUSE_LOCATION (c),
11630 "low bound %qE of array section does not have integral type",
11631 low_bound);
11632 return error_mark_node;
11633 }
11634 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11635 {
11636 error_at (OMP_CLAUSE_LOCATION (c),
11637 "length %qE of array section does not have integral type",
11638 length);
11639 return error_mark_node;
11640 }
11641 if (low_bound
11642 && TREE_CODE (low_bound) == INTEGER_CST
11643 && TYPE_PRECISION (TREE_TYPE (low_bound))
11644 > TYPE_PRECISION (sizetype))
11645 low_bound = fold_convert (sizetype, low_bound);
11646 if (length
11647 && TREE_CODE (length) == INTEGER_CST
11648 && TYPE_PRECISION (TREE_TYPE (length))
11649 > TYPE_PRECISION (sizetype))
11650 length = fold_convert (sizetype, length);
11651 if (low_bound == NULL_TREE)
11652 low_bound = integer_zero_node;
11653
11654 if (length != NULL_TREE)
11655 {
11656 if (!integer_nonzerop (length))
11657 maybe_zero_len = true;
11658 if (first_non_one == types.length ()
11659 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11660 first_non_one++;
11661 }
11662 if (TREE_CODE (type) == ARRAY_TYPE)
11663 {
11664 if (length == NULL_TREE
11665 && (TYPE_DOMAIN (type) == NULL_TREE
11666 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11667 {
11668 error_at (OMP_CLAUSE_LOCATION (c),
11669 "for unknown bound array type length expression must "
11670 "be specified");
11671 return error_mark_node;
11672 }
11673 if (TREE_CODE (low_bound) == INTEGER_CST
11674 && tree_int_cst_sgn (low_bound) == -1)
11675 {
11676 error_at (OMP_CLAUSE_LOCATION (c),
11677 "negative low bound in array section in %qs clause",
11678 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11679 return error_mark_node;
11680 }
11681 if (length != NULL_TREE
11682 && TREE_CODE (length) == INTEGER_CST
11683 && tree_int_cst_sgn (length) == -1)
11684 {
11685 error_at (OMP_CLAUSE_LOCATION (c),
11686 "negative length in array section in %qs clause",
11687 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11688 return error_mark_node;
11689 }
11690 if (TYPE_DOMAIN (type)
11691 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11692 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11693 == INTEGER_CST)
11694 {
11695 tree size = size_binop (PLUS_EXPR,
11696 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11697 size_one_node);
11698 if (TREE_CODE (low_bound) == INTEGER_CST)
11699 {
11700 if (tree_int_cst_lt (size, low_bound))
11701 {
11702 error_at (OMP_CLAUSE_LOCATION (c),
11703 "low bound %qE above array section size "
11704 "in %qs clause", low_bound,
11705 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11706 return error_mark_node;
11707 }
11708 if (tree_int_cst_equal (size, low_bound))
11709 maybe_zero_len = true;
11710 else if (length == NULL_TREE
11711 && first_non_one == types.length ()
11712 && tree_int_cst_equal
11713 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11714 low_bound))
11715 first_non_one++;
11716 }
11717 else if (length == NULL_TREE)
11718 {
11719 maybe_zero_len = true;
11720 if (first_non_one == types.length ())
11721 first_non_one++;
11722 }
11723 if (length && TREE_CODE (length) == INTEGER_CST)
11724 {
11725 if (tree_int_cst_lt (size, length))
11726 {
11727 error_at (OMP_CLAUSE_LOCATION (c),
11728 "length %qE above array section size "
11729 "in %qs clause", length,
11730 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11731 return error_mark_node;
11732 }
11733 if (TREE_CODE (low_bound) == INTEGER_CST)
11734 {
11735 tree lbpluslen
11736 = size_binop (PLUS_EXPR,
11737 fold_convert (sizetype, low_bound),
11738 fold_convert (sizetype, length));
11739 if (TREE_CODE (lbpluslen) == INTEGER_CST
11740 && tree_int_cst_lt (size, lbpluslen))
11741 {
11742 error_at (OMP_CLAUSE_LOCATION (c),
11743 "high bound %qE above array section size "
11744 "in %qs clause", lbpluslen,
11745 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11746 return error_mark_node;
11747 }
11748 }
11749 }
11750 }
11751 else if (length == NULL_TREE)
11752 {
11753 maybe_zero_len = true;
11754 if (first_non_one == types.length ())
11755 first_non_one++;
11756 }
11757
11758 /* For [lb:] we will need to evaluate lb more than once. */
11759 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11760 {
11761 tree lb = c_save_expr (low_bound);
11762 if (lb != low_bound)
11763 {
11764 TREE_PURPOSE (t) = lb;
11765 low_bound = lb;
11766 }
11767 }
11768 }
11769 else if (TREE_CODE (type) == POINTER_TYPE)
11770 {
11771 if (length == NULL_TREE)
11772 {
11773 error_at (OMP_CLAUSE_LOCATION (c),
11774 "for pointer type length expression must be specified");
11775 return error_mark_node;
11776 }
11777 /* If there is a pointer type anywhere but in the very first
11778 array-section-subscript, the array section can't be contiguous. */
11779 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11780 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11781 {
11782 error_at (OMP_CLAUSE_LOCATION (c),
11783 "array section is not contiguous in %qs clause",
11784 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11785 return error_mark_node;
11786 }
11787 }
11788 else
11789 {
11790 error_at (OMP_CLAUSE_LOCATION (c),
11791 "%qE does not have pointer or array type", ret);
11792 return error_mark_node;
11793 }
11794 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11795 types.safe_push (TREE_TYPE (ret));
11796 /* We will need to evaluate lb more than once. */
11797 tree lb = c_save_expr (low_bound);
11798 if (lb != low_bound)
11799 {
11800 TREE_PURPOSE (t) = lb;
11801 low_bound = lb;
11802 }
11803 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11804 return ret;
11805}
11806
11807/* Handle array sections for clause C. */
11808
11809static bool
11810handle_omp_array_sections (tree c)
11811{
11812 bool maybe_zero_len = false;
11813 unsigned int first_non_one = 0;
11814 vec<tree> types = vNULL;
11815 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11816 maybe_zero_len, first_non_one);
11817 if (first == error_mark_node)
11818 {
11819 types.release ();
11820 return true;
11821 }
11822 if (first == NULL_TREE)
11823 {
11824 types.release ();
11825 return false;
11826 }
11827 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11828 {
11829 tree t = OMP_CLAUSE_DECL (c);
11830 tree tem = NULL_TREE;
11831 types.release ();
11832 /* Need to evaluate side effects in the length expressions
11833 if any. */
11834 while (TREE_CODE (t) == TREE_LIST)
11835 {
11836 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11837 {
11838 if (tem == NULL_TREE)
11839 tem = TREE_VALUE (t);
11840 else
11841 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11842 TREE_VALUE (t), tem);
11843 }
11844 t = TREE_CHAIN (t);
11845 }
11846 if (tem)
11847 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11848 first = c_fully_fold (first, false, NULL);
11849 OMP_CLAUSE_DECL (c) = first;
11850 }
11851 else
11852 {
11853 unsigned int num = types.length (), i;
11854 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11855 tree condition = NULL_TREE;
11856
11857 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11858 maybe_zero_len = true;
11859
11860 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11861 t = TREE_CHAIN (t))
11862 {
11863 tree low_bound = TREE_PURPOSE (t);
11864 tree length = TREE_VALUE (t);
11865
11866 i--;
11867 if (low_bound
11868 && TREE_CODE (low_bound) == INTEGER_CST
11869 && TYPE_PRECISION (TREE_TYPE (low_bound))
11870 > TYPE_PRECISION (sizetype))
11871 low_bound = fold_convert (sizetype, low_bound);
11872 if (length
11873 && TREE_CODE (length) == INTEGER_CST
11874 && TYPE_PRECISION (TREE_TYPE (length))
11875 > TYPE_PRECISION (sizetype))
11876 length = fold_convert (sizetype, length);
11877 if (low_bound == NULL_TREE)
11878 low_bound = integer_zero_node;
11879 if (!maybe_zero_len && i > first_non_one)
11880 {
11881 if (integer_nonzerop (low_bound))
11882 goto do_warn_noncontiguous;
11883 if (length != NULL_TREE
11884 && TREE_CODE (length) == INTEGER_CST
11885 && TYPE_DOMAIN (types[i])
11886 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11887 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11888 == INTEGER_CST)
11889 {
11890 tree size;
11891 size = size_binop (PLUS_EXPR,
11892 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11893 size_one_node);
11894 if (!tree_int_cst_equal (length, size))
11895 {
11896 do_warn_noncontiguous:
11897 error_at (OMP_CLAUSE_LOCATION (c),
11898 "array section is not contiguous in %qs "
11899 "clause",
11900 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11901 types.release ();
11902 return true;
11903 }
11904 }
11905 if (length != NULL_TREE
11906 && TREE_SIDE_EFFECTS (length))
11907 {
11908 if (side_effects == NULL_TREE)
11909 side_effects = length;
11910 else
11911 side_effects = build2 (COMPOUND_EXPR,
11912 TREE_TYPE (side_effects),
11913 length, side_effects);
11914 }
11915 }
11916 else
11917 {
11918 tree l;
11919
11920 if (i > first_non_one && length && integer_nonzerop (length))
11921 continue;
11922 if (length)
11923 l = fold_convert (sizetype, length);
11924 else
11925 {
11926 l = size_binop (PLUS_EXPR,
11927 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11928 size_one_node);
11929 l = size_binop (MINUS_EXPR, l,
11930 fold_convert (sizetype, low_bound));
11931 }
11932 if (i > first_non_one)
11933 {
11934 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11935 size_zero_node);
11936 if (condition == NULL_TREE)
11937 condition = l;
11938 else
11939 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11940 l, condition);
11941 }
11942 else if (size == NULL_TREE)
11943 {
11944 size = size_in_bytes (TREE_TYPE (types[i]));
11945 size = size_binop (MULT_EXPR, size, l);
11946 if (condition)
11947 size = fold_build3 (COND_EXPR, sizetype, condition,
11948 size, size_zero_node);
11949 }
11950 else
11951 size = size_binop (MULT_EXPR, size, l);
11952 }
11953 }
11954 types.release ();
11955 if (side_effects)
11956 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11957 first = c_fully_fold (first, false, NULL);
11958 OMP_CLAUSE_DECL (c) = first;
11959 if (size)
11960 size = c_fully_fold (size, false, NULL);
11961 OMP_CLAUSE_SIZE (c) = size;
11962 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11963 return false;
41dbbb37 11964 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
acf0174b 11965 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
41dbbb37 11966 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
acf0174b
JJ
11967 if (!c_mark_addressable (t))
11968 return false;
11969 OMP_CLAUSE_DECL (c2) = t;
11970 t = build_fold_addr_expr (first);
11971 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11972 tree ptr = OMP_CLAUSE_DECL (c2);
11973 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11974 ptr = build_fold_addr_expr (ptr);
11975 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11976 ptrdiff_type_node, t,
11977 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11978 ptrdiff_type_node, ptr));
11979 t = c_fully_fold (t, false, NULL);
11980 OMP_CLAUSE_SIZE (c2) = t;
11981 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11982 OMP_CLAUSE_CHAIN (c) = c2;
11983 }
11984 return false;
11985}
11986
11987/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11988 an inline call. But, remap
11989 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11990 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11991
11992static tree
11993c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11994 tree decl, tree placeholder)
11995{
11996 copy_body_data id;
b787e7a2 11997 hash_map<tree, tree> decl_map;
acf0174b 11998
b787e7a2
TS
11999 decl_map.put (omp_decl1, placeholder);
12000 decl_map.put (omp_decl2, decl);
acf0174b
JJ
12001 memset (&id, 0, sizeof (id));
12002 id.src_fn = DECL_CONTEXT (omp_decl1);
12003 id.dst_fn = current_function_decl;
12004 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 12005 id.decl_map = &decl_map;
acf0174b
JJ
12006
12007 id.copy_decl = copy_decl_no_change;
12008 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12009 id.transform_new_cfg = true;
12010 id.transform_return_to_modify = false;
12011 id.transform_lang_insert_block = NULL;
12012 id.eh_lp_nr = 0;
12013 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
12014 return stmt;
12015}
12016
12017/* Helper function of c_finish_omp_clauses, called via walk_tree.
12018 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12019
12020static tree
12021c_find_omp_placeholder_r (tree *tp, int *, void *data)
12022{
12023 if (*tp == (tree) data)
12024 return *tp;
12025 return NULL_TREE;
12026}
12027
41dbbb37 12028/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
12029 Remove any elements from the list that are invalid. */
12030
12031tree
12032c_finish_omp_clauses (tree clauses)
12033{
12034 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 12035 bitmap_head aligned_head;
f3316c6d 12036 tree c, t, *pc;
acf0174b
JJ
12037 bool branch_seen = false;
12038 bool copyprivate_seen = false;
12039 tree *nowait_clause = NULL;
953ff289
DN
12040
12041 bitmap_obstack_initialize (NULL);
12042 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12043 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12044 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 12045 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
12046
12047 for (pc = &clauses, c = clauses; c ; c = *pc)
12048 {
12049 bool remove = false;
12050 bool need_complete = false;
12051 bool need_implicitly_determined = false;
12052
aaf46ef9 12053 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
12054 {
12055 case OMP_CLAUSE_SHARED:
953ff289
DN
12056 need_implicitly_determined = true;
12057 goto check_dup_generic;
12058
12059 case OMP_CLAUSE_PRIVATE:
953ff289
DN
12060 need_complete = true;
12061 need_implicitly_determined = true;
12062 goto check_dup_generic;
12063
12064 case OMP_CLAUSE_REDUCTION:
953ff289
DN
12065 need_implicitly_determined = true;
12066 t = OMP_CLAUSE_DECL (c);
acf0174b 12067 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
12068 && (FLOAT_TYPE_P (TREE_TYPE (t))
12069 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
12070 {
12071 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12072 const char *r_name = NULL;
12073
12074 switch (r_code)
12075 {
12076 case PLUS_EXPR:
12077 case MULT_EXPR:
12078 case MINUS_EXPR:
652fea39 12079 break;
20906c66 12080 case MIN_EXPR:
652fea39
JJ
12081 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12082 r_name = "min";
12083 break;
20906c66 12084 case MAX_EXPR:
652fea39
JJ
12085 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
12086 r_name = "max";
953ff289
DN
12087 break;
12088 case BIT_AND_EXPR:
12089 r_name = "&";
12090 break;
12091 case BIT_XOR_EXPR:
12092 r_name = "^";
12093 break;
12094 case BIT_IOR_EXPR:
12095 r_name = "|";
12096 break;
12097 case TRUTH_ANDIF_EXPR:
652fea39
JJ
12098 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12099 r_name = "&&";
953ff289
DN
12100 break;
12101 case TRUTH_ORIF_EXPR:
652fea39
JJ
12102 if (FLOAT_TYPE_P (TREE_TYPE (t)))
12103 r_name = "||";
953ff289
DN
12104 break;
12105 default:
12106 gcc_unreachable ();
12107 }
12108 if (r_name)
12109 {
c2255bc4
AH
12110 error_at (OMP_CLAUSE_LOCATION (c),
12111 "%qE has invalid type for %<reduction(%s)%>",
12112 t, r_name);
953ff289 12113 remove = true;
ee1d5a02 12114 break;
953ff289
DN
12115 }
12116 }
acf0174b
JJ
12117 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12118 {
12119 error_at (OMP_CLAUSE_LOCATION (c),
12120 "user defined reduction not found for %qD", t);
12121 remove = true;
ee1d5a02 12122 break;
acf0174b
JJ
12123 }
12124 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12125 {
12126 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
12127 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12128 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12129 VAR_DECL, NULL_TREE, type);
12130 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12131 DECL_ARTIFICIAL (placeholder) = 1;
12132 DECL_IGNORED_P (placeholder) = 1;
12133 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12134 c_mark_addressable (placeholder);
12135 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
12136 c_mark_addressable (OMP_CLAUSE_DECL (c));
12137 OMP_CLAUSE_REDUCTION_MERGE (c)
12138 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12139 TREE_VEC_ELT (list, 0),
12140 TREE_VEC_ELT (list, 1),
12141 OMP_CLAUSE_DECL (c), placeholder);
12142 OMP_CLAUSE_REDUCTION_MERGE (c)
12143 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12144 void_type_node, NULL_TREE,
12145 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
12146 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12147 if (TREE_VEC_LENGTH (list) == 6)
12148 {
12149 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
12150 c_mark_addressable (OMP_CLAUSE_DECL (c));
12151 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12152 c_mark_addressable (placeholder);
12153 tree init = TREE_VEC_ELT (list, 5);
12154 if (init == error_mark_node)
12155 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12156 OMP_CLAUSE_REDUCTION_INIT (c)
12157 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12158 TREE_VEC_ELT (list, 3),
12159 OMP_CLAUSE_DECL (c), placeholder);
12160 if (TREE_VEC_ELT (list, 5) == error_mark_node)
12161 OMP_CLAUSE_REDUCTION_INIT (c)
12162 = build2 (INIT_EXPR, TREE_TYPE (t), t,
12163 OMP_CLAUSE_REDUCTION_INIT (c));
12164 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12165 c_find_omp_placeholder_r,
12166 placeholder, NULL))
12167 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12168 }
12169 else
12170 {
12171 tree init;
12172 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
12173 init = build_constructor (TREE_TYPE (t), NULL);
12174 else
12175 init = fold_convert (TREE_TYPE (t), integer_zero_node);
12176 OMP_CLAUSE_REDUCTION_INIT (c)
12177 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
12178 }
12179 OMP_CLAUSE_REDUCTION_INIT (c)
12180 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12181 void_type_node, NULL_TREE,
12182 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12183 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12184 }
953ff289
DN
12185 goto check_dup_generic;
12186
12187 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
12188 copyprivate_seen = true;
12189 if (nowait_clause)
12190 {
12191 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12192 "%<nowait%> clause must not be used together "
12193 "with %<copyprivate%>");
12194 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12195 nowait_clause = NULL;
12196 }
953ff289
DN
12197 goto check_dup_generic;
12198
12199 case OMP_CLAUSE_COPYIN:
953ff289
DN
12200 t = OMP_CLAUSE_DECL (c);
12201 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
12202 {
c2255bc4
AH
12203 error_at (OMP_CLAUSE_LOCATION (c),
12204 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12205 remove = true;
ee1d5a02 12206 break;
953ff289
DN
12207 }
12208 goto check_dup_generic;
12209
acf0174b
JJ
12210 case OMP_CLAUSE_LINEAR:
12211 t = OMP_CLAUSE_DECL (c);
12212 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12213 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12214 {
12215 error_at (OMP_CLAUSE_LOCATION (c),
12216 "linear clause applied to non-integral non-pointer "
12217 "variable with type %qT", TREE_TYPE (t));
12218 remove = true;
12219 break;
12220 }
12221 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12222 {
12223 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12224 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12225 OMP_CLAUSE_DECL (c), s);
12226 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12227 sizetype, s, OMP_CLAUSE_DECL (c));
12228 if (s == error_mark_node)
12229 s = size_one_node;
12230 OMP_CLAUSE_LINEAR_STEP (c) = s;
12231 }
da6f124d
JJ
12232 else
12233 OMP_CLAUSE_LINEAR_STEP (c)
12234 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12235 goto check_dup_generic;
12236
953ff289
DN
12237 check_dup_generic:
12238 t = OMP_CLAUSE_DECL (c);
12239 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12240 {
c2255bc4 12241 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12242 "%qE is not a variable in clause %qs", t,
12243 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12244 remove = true;
12245 }
12246 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12247 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12248 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12249 {
c2255bc4
AH
12250 error_at (OMP_CLAUSE_LOCATION (c),
12251 "%qE appears more than once in data clauses", t);
953ff289
DN
12252 remove = true;
12253 }
12254 else
12255 bitmap_set_bit (&generic_head, DECL_UID (t));
12256 break;
12257
12258 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12259 t = OMP_CLAUSE_DECL (c);
12260 need_complete = true;
12261 need_implicitly_determined = true;
12262 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12263 {
c2255bc4
AH
12264 error_at (OMP_CLAUSE_LOCATION (c),
12265 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12266 remove = true;
12267 }
12268 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12269 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12270 {
c2255bc4
AH
12271 error_at (OMP_CLAUSE_LOCATION (c),
12272 "%qE appears more than once in data clauses", t);
953ff289
DN
12273 remove = true;
12274 }
12275 else
12276 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12277 break;
12278
12279 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12280 t = OMP_CLAUSE_DECL (c);
12281 need_complete = true;
12282 need_implicitly_determined = true;
12283 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12284 {
c2255bc4
AH
12285 error_at (OMP_CLAUSE_LOCATION (c),
12286 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12287 remove = true;
12288 }
12289 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12290 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12291 {
c2255bc4
AH
12292 error_at (OMP_CLAUSE_LOCATION (c),
12293 "%qE appears more than once in data clauses", t);
953ff289
DN
12294 remove = true;
12295 }
12296 else
12297 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12298 break;
12299
acf0174b
JJ
12300 case OMP_CLAUSE_ALIGNED:
12301 t = OMP_CLAUSE_DECL (c);
12302 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12303 {
12304 error_at (OMP_CLAUSE_LOCATION (c),
12305 "%qE is not a variable in %<aligned%> clause", t);
12306 remove = true;
12307 }
5a9785fb
JJ
12308 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12309 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12310 {
12311 error_at (OMP_CLAUSE_LOCATION (c),
12312 "%qE in %<aligned%> clause is neither a pointer nor "
12313 "an array", t);
12314 remove = true;
12315 }
acf0174b
JJ
12316 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12317 {
12318 error_at (OMP_CLAUSE_LOCATION (c),
12319 "%qE appears more than once in %<aligned%> clauses",
12320 t);
12321 remove = true;
12322 }
12323 else
12324 bitmap_set_bit (&aligned_head, DECL_UID (t));
12325 break;
12326
12327 case OMP_CLAUSE_DEPEND:
12328 t = OMP_CLAUSE_DECL (c);
12329 if (TREE_CODE (t) == TREE_LIST)
12330 {
12331 if (handle_omp_array_sections (c))
12332 remove = true;
12333 break;
12334 }
12335 if (t == error_mark_node)
12336 remove = true;
12337 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12338 {
12339 error_at (OMP_CLAUSE_LOCATION (c),
12340 "%qE is not a variable in %<depend%> clause", t);
12341 remove = true;
12342 }
12343 else if (!c_mark_addressable (t))
12344 remove = true;
12345 break;
12346
12347 case OMP_CLAUSE_MAP:
12348 case OMP_CLAUSE_TO:
12349 case OMP_CLAUSE_FROM:
41dbbb37 12350 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
12351 t = OMP_CLAUSE_DECL (c);
12352 if (TREE_CODE (t) == TREE_LIST)
12353 {
12354 if (handle_omp_array_sections (c))
12355 remove = true;
12356 else
12357 {
12358 t = OMP_CLAUSE_DECL (c);
b17a8b07 12359 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12360 {
12361 error_at (OMP_CLAUSE_LOCATION (c),
12362 "array section does not have mappable type "
12363 "in %qs clause",
12364 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12365 remove = true;
12366 }
12367 }
12368 break;
12369 }
12370 if (t == error_mark_node)
12371 remove = true;
12372 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12373 {
12374 error_at (OMP_CLAUSE_LOCATION (c),
12375 "%qE is not a variable in %qs clause", t,
12376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12377 remove = true;
12378 }
12379 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12380 {
12381 error_at (OMP_CLAUSE_LOCATION (c),
12382 "%qD is threadprivate variable in %qs clause", t,
12383 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12384 remove = true;
12385 }
12386 else if (!c_mark_addressable (t))
12387 remove = true;
b17a8b07 12388 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37
TS
12389 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
12390 || (OMP_CLAUSE_MAP_KIND (c)
12391 == GOMP_MAP_FORCE_DEVICEPTR)))
b17a8b07 12392 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12393 {
12394 error_at (OMP_CLAUSE_LOCATION (c),
12395 "%qD does not have a mappable type in %qs clause", t,
12396 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12397 remove = true;
12398 }
12399 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12400 {
12401 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12402 error ("%qD appears more than once in motion clauses", t);
12403 else
12404 error ("%qD appears more than once in map clauses", t);
12405 remove = true;
12406 }
12407 else
12408 bitmap_set_bit (&generic_head, DECL_UID (t));
12409 break;
12410
12411 case OMP_CLAUSE_UNIFORM:
12412 t = OMP_CLAUSE_DECL (c);
12413 if (TREE_CODE (t) != PARM_DECL)
12414 {
12415 if (DECL_P (t))
12416 error_at (OMP_CLAUSE_LOCATION (c),
12417 "%qD is not an argument in %<uniform%> clause", t);
12418 else
12419 error_at (OMP_CLAUSE_LOCATION (c),
12420 "%qE is not an argument in %<uniform%> clause", t);
12421 remove = true;
ee1d5a02 12422 break;
acf0174b 12423 }
ee1d5a02 12424 goto check_dup_generic;
acf0174b
JJ
12425
12426 case OMP_CLAUSE_NOWAIT:
12427 if (copyprivate_seen)
12428 {
12429 error_at (OMP_CLAUSE_LOCATION (c),
12430 "%<nowait%> clause must not be used together "
12431 "with %<copyprivate%>");
12432 remove = true;
12433 break;
12434 }
12435 nowait_clause = pc;
12436 pc = &OMP_CLAUSE_CHAIN (c);
12437 continue;
12438
953ff289
DN
12439 case OMP_CLAUSE_IF:
12440 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12441 case OMP_CLAUSE_NUM_TEAMS:
12442 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12443 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12444 case OMP_CLAUSE_ORDERED:
12445 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12446 case OMP_CLAUSE_UNTIED:
12447 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12448 case OMP_CLAUSE_FINAL:
12449 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12450 case OMP_CLAUSE_SAFELEN:
12451 case OMP_CLAUSE_SIMDLEN:
12452 case OMP_CLAUSE_DEVICE:
12453 case OMP_CLAUSE_DIST_SCHEDULE:
12454 case OMP_CLAUSE_PARALLEL:
12455 case OMP_CLAUSE_FOR:
12456 case OMP_CLAUSE_SECTIONS:
12457 case OMP_CLAUSE_TASKGROUP:
12458 case OMP_CLAUSE_PROC_BIND:
9a771876 12459 case OMP_CLAUSE__CILK_FOR_COUNT_:
41dbbb37
TS
12460 case OMP_CLAUSE_NUM_GANGS:
12461 case OMP_CLAUSE_NUM_WORKERS:
12462 case OMP_CLAUSE_VECTOR_LENGTH:
12463 case OMP_CLAUSE_ASYNC:
12464 case OMP_CLAUSE_WAIT:
12465 case OMP_CLAUSE_AUTO:
12466 case OMP_CLAUSE_SEQ:
12467 case OMP_CLAUSE_GANG:
12468 case OMP_CLAUSE_WORKER:
12469 case OMP_CLAUSE_VECTOR:
acf0174b
JJ
12470 pc = &OMP_CLAUSE_CHAIN (c);
12471 continue;
12472
12473 case OMP_CLAUSE_INBRANCH:
12474 case OMP_CLAUSE_NOTINBRANCH:
12475 if (branch_seen)
12476 {
12477 error_at (OMP_CLAUSE_LOCATION (c),
12478 "%<inbranch%> clause is incompatible with "
12479 "%<notinbranch%>");
12480 remove = true;
12481 break;
12482 }
12483 branch_seen = true;
953ff289
DN
12484 pc = &OMP_CLAUSE_CHAIN (c);
12485 continue;
12486
12487 default:
12488 gcc_unreachable ();
12489 }
12490
12491 if (!remove)
12492 {
12493 t = OMP_CLAUSE_DECL (c);
12494
12495 if (need_complete)
12496 {
12497 t = require_complete_type (t);
12498 if (t == error_mark_node)
12499 remove = true;
12500 }
12501
12502 if (need_implicitly_determined)
12503 {
12504 const char *share_name = NULL;
12505
12506 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12507 share_name = "threadprivate";
12508 else switch (c_omp_predetermined_sharing (t))
12509 {
12510 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12511 break;
12512 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12513 /* const vars may be specified in firstprivate clause. */
12514 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12515 && TREE_READONLY (t))
12516 break;
953ff289
DN
12517 share_name = "shared";
12518 break;
12519 case OMP_CLAUSE_DEFAULT_PRIVATE:
12520 share_name = "private";
12521 break;
12522 default:
12523 gcc_unreachable ();
12524 }
12525 if (share_name)
12526 {
c2255bc4
AH
12527 error_at (OMP_CLAUSE_LOCATION (c),
12528 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12529 t, share_name,
12530 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12531 remove = true;
12532 }
12533 }
12534 }
12535
12536 if (remove)
12537 *pc = OMP_CLAUSE_CHAIN (c);
12538 else
12539 pc = &OMP_CLAUSE_CHAIN (c);
12540 }
12541
12542 bitmap_obstack_release (NULL);
12543 return clauses;
12544}
9ae165a0 12545
0a35513e
AH
12546/* Create a transaction node. */
12547
12548tree
12549c_finish_transaction (location_t loc, tree block, int flags)
12550{
12551 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12552 if (flags & TM_STMT_ATTR_OUTER)
12553 TRANSACTION_EXPR_OUTER (stmt) = 1;
12554 if (flags & TM_STMT_ATTR_RELAXED)
12555 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12556 return add_stmt (stmt);
12557}
12558
9ae165a0
DG
12559/* Make a variant type in the proper way for C/C++, propagating qualifiers
12560 down to the element type of an array. */
12561
12562tree
12563c_build_qualified_type (tree type, int type_quals)
12564{
12565 if (type == error_mark_node)
12566 return type;
12567
12568 if (TREE_CODE (type) == ARRAY_TYPE)
12569 {
12570 tree t;
12571 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12572 type_quals);
12573
12574 /* See if we already have an identically qualified type. */
12575 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12576 {
12577 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12578 && TYPE_NAME (t) == TYPE_NAME (type)
12579 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12580 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12581 TYPE_ATTRIBUTES (type)))
12582 break;
12583 }
12584 if (!t)
12585 {
12586 tree domain = TYPE_DOMAIN (type);
12587
12588 t = build_variant_type_copy (type);
12589 TREE_TYPE (t) = element_type;
12590
12591 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12592 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12593 SET_TYPE_STRUCTURAL_EQUALITY (t);
12594 else if (TYPE_CANONICAL (element_type) != element_type
12595 || (domain && TYPE_CANONICAL (domain) != domain))
12596 {
b8698a0f 12597 tree unqualified_canon
9ae165a0 12598 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12599 domain? TYPE_CANONICAL (domain)
9ae165a0 12600 : NULL_TREE);
b8698a0f 12601 TYPE_CANONICAL (t)
9ae165a0
DG
12602 = c_build_qualified_type (unqualified_canon, type_quals);
12603 }
12604 else
12605 TYPE_CANONICAL (t) = t;
12606 }
12607 return t;
12608 }
12609
12610 /* A restrict-qualified pointer type must be a pointer to object or
12611 incomplete type. Note that the use of POINTER_TYPE_P also allows
12612 REFERENCE_TYPEs, which is appropriate for C++. */
12613 if ((type_quals & TYPE_QUAL_RESTRICT)
12614 && (!POINTER_TYPE_P (type)
12615 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12616 {
12617 error ("invalid use of %<restrict%>");
12618 type_quals &= ~TYPE_QUAL_RESTRICT;
12619 }
12620
12621 return build_qualified_type (type, type_quals);
12622}
72b5577d
ILT
12623
12624/* Build a VA_ARG_EXPR for the C parser. */
12625
12626tree
c2255bc4 12627c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d 12628{
ec3fba51
MP
12629 if (error_operand_p (type))
12630 return error_mark_node;
12631 else if (!COMPLETE_TYPE_P (type))
4e81b788 12632 {
ec3fba51
MP
12633 error_at (loc, "second argument to %<va_arg%> is of incomplete "
12634 "type %qT", type);
4e81b788
MP
12635 return error_mark_node;
12636 }
ec3fba51
MP
12637 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12638 warning_at (loc, OPT_Wc___compat,
12639 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12640 return build_va_arg (loc, expr, type);
72b5577d 12641}
acf0174b
JJ
12642
12643/* Return truthvalue of whether T1 is the same tree structure as T2.
12644 Return 1 if they are the same. Return 0 if they are different. */
12645
12646bool
12647c_tree_equal (tree t1, tree t2)
12648{
12649 enum tree_code code1, code2;
12650
12651 if (t1 == t2)
12652 return true;
12653 if (!t1 || !t2)
12654 return false;
12655
12656 for (code1 = TREE_CODE (t1);
12657 CONVERT_EXPR_CODE_P (code1)
12658 || code1 == NON_LVALUE_EXPR;
12659 code1 = TREE_CODE (t1))
12660 t1 = TREE_OPERAND (t1, 0);
12661 for (code2 = TREE_CODE (t2);
12662 CONVERT_EXPR_CODE_P (code2)
12663 || code2 == NON_LVALUE_EXPR;
12664 code2 = TREE_CODE (t2))
12665 t2 = TREE_OPERAND (t2, 0);
12666
12667 /* They might have become equal now. */
12668 if (t1 == t2)
12669 return true;
12670
12671 if (code1 != code2)
12672 return false;
12673
12674 switch (code1)
12675 {
12676 case INTEGER_CST:
807e902e 12677 return wi::eq_p (t1, t2);
acf0174b
JJ
12678
12679 case REAL_CST:
12680 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12681
12682 case STRING_CST:
12683 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12684 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12685 TREE_STRING_LENGTH (t1));
12686
12687 case FIXED_CST:
12688 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12689 TREE_FIXED_CST (t2));
12690
12691 case COMPLEX_CST:
12692 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12693 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12694
12695 case VECTOR_CST:
12696 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12697
12698 case CONSTRUCTOR:
12699 /* We need to do this when determining whether or not two
12700 non-type pointer to member function template arguments
12701 are the same. */
12702 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12703 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12704 return false;
12705 {
12706 tree field, value;
12707 unsigned int i;
12708 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12709 {
12710 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12711 if (!c_tree_equal (field, elt2->index)
12712 || !c_tree_equal (value, elt2->value))
12713 return false;
12714 }
12715 }
12716 return true;
12717
12718 case TREE_LIST:
12719 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12720 return false;
12721 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12722 return false;
12723 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12724
12725 case SAVE_EXPR:
12726 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12727
12728 case CALL_EXPR:
12729 {
12730 tree arg1, arg2;
12731 call_expr_arg_iterator iter1, iter2;
12732 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12733 return false;
12734 for (arg1 = first_call_expr_arg (t1, &iter1),
12735 arg2 = first_call_expr_arg (t2, &iter2);
12736 arg1 && arg2;
12737 arg1 = next_call_expr_arg (&iter1),
12738 arg2 = next_call_expr_arg (&iter2))
12739 if (!c_tree_equal (arg1, arg2))
12740 return false;
12741 if (arg1 || arg2)
12742 return false;
12743 return true;
12744 }
12745
12746 case TARGET_EXPR:
12747 {
12748 tree o1 = TREE_OPERAND (t1, 0);
12749 tree o2 = TREE_OPERAND (t2, 0);
12750
12751 /* Special case: if either target is an unallocated VAR_DECL,
12752 it means that it's going to be unified with whatever the
12753 TARGET_EXPR is really supposed to initialize, so treat it
12754 as being equivalent to anything. */
12755 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12756 && !DECL_RTL_SET_P (o1))
12757 /*Nop*/;
12758 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12759 && !DECL_RTL_SET_P (o2))
12760 /*Nop*/;
12761 else if (!c_tree_equal (o1, o2))
12762 return false;
12763
12764 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12765 }
12766
12767 case COMPONENT_REF:
12768 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12769 return false;
12770 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12771
12772 case PARM_DECL:
12773 case VAR_DECL:
12774 case CONST_DECL:
12775 case FIELD_DECL:
12776 case FUNCTION_DECL:
12777 case IDENTIFIER_NODE:
12778 case SSA_NAME:
12779 return false;
12780
12781 case TREE_VEC:
12782 {
12783 unsigned ix;
12784 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12785 return false;
12786 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12787 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12788 TREE_VEC_ELT (t2, ix)))
12789 return false;
12790 return true;
12791 }
12792
12793 default:
12794 break;
12795 }
12796
12797 switch (TREE_CODE_CLASS (code1))
12798 {
12799 case tcc_unary:
12800 case tcc_binary:
12801 case tcc_comparison:
12802 case tcc_expression:
12803 case tcc_vl_exp:
12804 case tcc_reference:
12805 case tcc_statement:
12806 {
12807 int i, n = TREE_OPERAND_LENGTH (t1);
12808
12809 switch (code1)
12810 {
12811 case PREINCREMENT_EXPR:
12812 case PREDECREMENT_EXPR:
12813 case POSTINCREMENT_EXPR:
12814 case POSTDECREMENT_EXPR:
12815 n = 1;
12816 break;
12817 case ARRAY_REF:
12818 n = 2;
12819 break;
12820 default:
12821 break;
12822 }
12823
12824 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12825 && n != TREE_OPERAND_LENGTH (t2))
12826 return false;
12827
12828 for (i = 0; i < n; ++i)
12829 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12830 return false;
12831
12832 return true;
12833 }
12834
12835 case tcc_type:
12836 return comptypes (t1, t2);
12837 default:
12838 gcc_unreachable ();
12839 }
12840 /* We can get here with --disable-checking. */
12841 return false;
12842}
12893402
BI
12843
12844/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12845 spawn-helper and BODY is the newly created body for FNDECL. */
12846
12847void
12848cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12849{
12850 tree list = alloc_stmt_list ();
12851 tree frame = make_cilk_frame (fndecl);
12852 tree dtor = create_cilk_function_exit (frame, false, true);
12853 add_local_decl (cfun, frame);
12854
12855 DECL_SAVED_TREE (fndecl) = list;
12856 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12857 frame);
12858 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12859 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12860
12861 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12862 append_to_statement_list (detach_expr, &body_list);
12863
12864 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12865 body = fold_build_cleanup_point_expr (void_type_node, body);
12866
12867 append_to_statement_list (body, &body_list);
12868 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12869 body_list, dtor), &list);
12870}