]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
[AArch64] Use __aarch64_vget_lane* macros for getting the lane in some lane multiply...
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
23a5b65a 2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
d8a2d370
DN
31#include "stor-layout.h"
32#include "trans-mem.h"
33#include "varasm.h"
34#include "stmt.h"
e57e265b 35#include "langhooks.h"
400fbf9f 36#include "c-tree.h"
29cc57cf 37#include "c-lang.h"
400fbf9f 38#include "flags.h"
ab87f8c8 39#include "intl.h"
672a6f42 40#include "target.h"
325c3691 41#include "tree-iterator.h"
6662d794 42#include "bitmap.h"
2fb9a547
AM
43#include "basic-block.h"
44#include "gimple-expr.h"
45b0be94 45#include "gimplify.h"
acf0174b 46#include "tree-inline.h"
0645c1a2 47#include "omp-low.h"
61d3ce20 48#include "c-family/c-objc.h"
a212e43f 49#include "c-family/c-common.h"
de5a5fa1 50#include "c-family/c-ubsan.h"
12893402 51#include "cilk.h"
807e902e 52#include "wide-int.h"
325c3691 53
2ac2f164
JM
54/* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61};
62
bc4b653b
JM
63/* The level of nesting inside "__alignof__". */
64int in_alignof;
65
66/* The level of nesting inside "sizeof". */
67int in_sizeof;
68
69/* The level of nesting inside "typeof". */
70int in_typeof;
400fbf9f 71
1a4049e7
JJ
72/* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74tree c_last_sizeof_arg;
75
9bac5cbb
G
76/* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78static int found_missing_braces;
103b7b17 79
bf730f15
RS
80static int require_constant_value;
81static int require_constant_elements;
82
58f9752a 83static bool null_pointer_constant_p (const_tree);
f55ade6e 84static tree qualify_type (tree, tree);
dc5027f4
JM
85static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
744aa42f 87static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
88static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 91static tree lookup_field (tree, tree);
81e5eca8
MP
92static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
db3927fb 95static tree pointer_diff (location_t, tree, tree);
68fca595 96static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 97 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
98static tree valid_compound_expr_initializer (tree, tree);
99static void push_string (const char *);
100static void push_member_name (tree);
f55ade6e
AJ
101static int spelling_length (void);
102static char *print_spelling (char *);
96b40f8d 103static void warning_init (location_t, int, const char *);
c2255bc4 104static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
105static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
a1e3b3d9 107static void output_pending_init_elements (int, struct obstack *);
ea58ef42 108static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 109static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
110static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
a1e3b3d9
LB
112static void set_nonincremental_init (struct obstack *);
113static void set_nonincremental_init_from_string (tree, struct obstack *);
114static tree find_init_member (tree, struct obstack *);
f37acdf9 115static void readonly_warning (tree, enum lvalue_use);
7bd11157 116static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 117static void record_maybe_used_decl (tree);
dc5027f4 118static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
119\f
120/* Return true if EXP is a null pointer constant, false otherwise. */
121
122static bool
58f9752a 123null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
124{
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 129 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
135}
928c19bb
JM
136
137/* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141
142static tree
143note_integer_operands (tree expr)
144{
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 {
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
150 }
151 else
152 {
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 }
156 return ret;
157}
158
4d84fe7c
JM
159/* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
163
164static inline tree
165remove_c_maybe_const_expr (tree expr)
166{
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
171}
172
f13c9b2c
AP
173\f/* This is a cache to hold if two types are compatible or not. */
174
175struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
58f9752a
KG
177 const_tree t1;
178 const_tree t2;
f13c9b2c
AP
179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
182};
183
184static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186
400fbf9f
JW
187/* Do `exp = require_complete_type (exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.) */
189
190tree
2f6e4e97 191require_complete_type (tree value)
400fbf9f
JW
192{
193 tree type = TREE_TYPE (value);
194
c3d5c3fa 195 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
196 return error_mark_node;
197
400fbf9f 198 /* First, detect a valid value with a complete type. */
d0f062fb 199 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
200 return value;
201
7a228918 202 c_incomplete_type_error (value, type);
400fbf9f
JW
203 return error_mark_node;
204}
205
206/* Print an error message for invalid use of an incomplete type.
207 VALUE is the expression that was used (or 0 if that isn't known)
208 and TYPE is the type that was invalid. */
209
210void
ac7d7749 211c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 212{
5d5993dd 213 const char *type_code_string;
400fbf9f
JW
214
215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
217 return;
218
219 if (value != 0 && (TREE_CODE (value) == VAR_DECL
220 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 221 error ("%qD has an incomplete type", value);
400fbf9f
JW
222 else
223 {
224 retry:
225 /* We must print an error message. Be clever about what it says. */
226
227 switch (TREE_CODE (type))
228 {
229 case RECORD_TYPE:
ab87f8c8 230 type_code_string = "struct";
400fbf9f
JW
231 break;
232
233 case UNION_TYPE:
ab87f8c8 234 type_code_string = "union";
400fbf9f
JW
235 break;
236
237 case ENUMERAL_TYPE:
ab87f8c8 238 type_code_string = "enum";
400fbf9f
JW
239 break;
240
241 case VOID_TYPE:
242 error ("invalid use of void expression");
243 return;
244
245 case ARRAY_TYPE:
246 if (TYPE_DOMAIN (type))
247 {
fba78abb
RH
248 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
249 {
250 error ("invalid use of flexible array member");
251 return;
252 }
400fbf9f
JW
253 type = TREE_TYPE (type);
254 goto retry;
255 }
256 error ("invalid use of array with unspecified bounds");
257 return;
258
259 default:
366de0ce 260 gcc_unreachable ();
400fbf9f
JW
261 }
262
263 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
264 error ("invalid use of undefined type %<%s %E%>",
265 type_code_string, TYPE_NAME (type));
400fbf9f
JW
266 else
267 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 268 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
269 }
270}
271
ab393bf1
NB
272/* Given a type, apply default promotions wrt unnamed function
273 arguments and return the new type. */
274
275tree
2f6e4e97 276c_type_promotes_to (tree type)
ab393bf1 277{
267bac10 278 tree ret = NULL_TREE;
ab393bf1 279
267bac10
JM
280 if (TYPE_MAIN_VARIANT (type) == float_type_node)
281 ret = double_type_node;
282 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
283 {
284 /* Preserve unsignedness if not really getting any wider. */
8df83eae 285 if (TYPE_UNSIGNED (type)
c22cacf3 286 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
287 ret = unsigned_type_node;
288 else
289 ret = integer_type_node;
ab393bf1
NB
290 }
291
267bac10
JM
292 if (ret != NULL_TREE)
293 return (TYPE_ATOMIC (type)
294 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
295 : ret);
296
ab393bf1
NB
297 return type;
298}
299
36c5e70a
BE
300/* Return true if between two named address spaces, whether there is a superset
301 named address space that encompasses both address spaces. If there is a
302 superset, return which address space is the superset. */
303
304static bool
305addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
306{
307 if (as1 == as2)
308 {
309 *common = as1;
310 return true;
311 }
312 else if (targetm.addr_space.subset_p (as1, as2))
313 {
314 *common = as2;
315 return true;
316 }
317 else if (targetm.addr_space.subset_p (as2, as1))
318 {
319 *common = as1;
320 return true;
321 }
322 else
323 return false;
324}
325
400fbf9f
JW
326/* Return a variant of TYPE which has all the type qualifiers of LIKE
327 as well as those of TYPE. */
328
329static tree
2f6e4e97 330qualify_type (tree type, tree like)
400fbf9f 331{
36c5e70a
BE
332 addr_space_t as_type = TYPE_ADDR_SPACE (type);
333 addr_space_t as_like = TYPE_ADDR_SPACE (like);
334 addr_space_t as_common;
335
336 /* If the two named address spaces are different, determine the common
337 superset address space. If there isn't one, raise an error. */
338 if (!addr_space_superset (as_type, as_like, &as_common))
339 {
340 as_common = as_type;
341 error ("%qT and %qT are in disjoint named address spaces",
342 type, like);
343 }
344
2f6e4e97 345 return c_build_qualified_type (type,
36c5e70a 346 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 347 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 348 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 349}
52ffd86e
MS
350
351/* Return true iff the given tree T is a variable length array. */
352
353bool
ac7d7749 354c_vla_type_p (const_tree t)
52ffd86e
MS
355{
356 if (TREE_CODE (t) == ARRAY_TYPE
357 && C_TYPE_VARIABLE_SIZE (t))
358 return true;
359 return false;
360}
400fbf9f 361\f
10bc1b1b 362/* Return the composite type of two compatible types.
5305f6d7 363
10bc1b1b
JM
364 We assume that comptypes has already been done and returned
365 nonzero; if that isn't so, this may crash. In particular, we
366 assume that qualifiers match. */
400fbf9f
JW
367
368tree
10bc1b1b 369composite_type (tree t1, tree t2)
400fbf9f 370{
b3694847
SS
371 enum tree_code code1;
372 enum tree_code code2;
4b027d16 373 tree attributes;
400fbf9f
JW
374
375 /* Save time if the two types are the same. */
376
377 if (t1 == t2) return t1;
378
379 /* If one type is nonsense, use the other. */
380 if (t1 == error_mark_node)
381 return t2;
382 if (t2 == error_mark_node)
383 return t1;
384
10bc1b1b
JM
385 code1 = TREE_CODE (t1);
386 code2 = TREE_CODE (t2);
387
d9525bec 388 /* Merge the attributes. */
5fd9b178 389 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 390
10bc1b1b
JM
391 /* If one is an enumerated type and the other is the compatible
392 integer type, the composite type might be either of the two
393 (DR#013 question 3). For consistency, use the enumerated type as
394 the composite type. */
400fbf9f 395
10bc1b1b
JM
396 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
397 return t1;
398 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
399 return t2;
75326e8c 400
366de0ce 401 gcc_assert (code1 == code2);
b6a10c9f 402
400fbf9f
JW
403 switch (code1)
404 {
400fbf9f 405 case POINTER_TYPE:
10bc1b1b 406 /* For two pointers, do this recursively on the target type. */
400fbf9f 407 {
3932261a
MM
408 tree pointed_to_1 = TREE_TYPE (t1);
409 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 410 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 411 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
412 t1 = build_type_attribute_variant (t1, attributes);
413 return qualify_type (t1, t2);
400fbf9f 414 }
400fbf9f
JW
415
416 case ARRAY_TYPE:
417 {
10bc1b1b 418 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
419 int quals;
420 tree unqual_elt;
ca8bdb78
JM
421 tree d1 = TYPE_DOMAIN (t1);
422 tree d2 = TYPE_DOMAIN (t2);
423 bool d1_variable, d2_variable;
424 bool d1_zero, d2_zero;
f6294de7 425 bool t1_complete, t2_complete;
46df2823 426
de46b2fe 427 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
428 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
429 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 430
f6294de7
JM
431 t1_complete = COMPLETE_TYPE_P (t1);
432 t2_complete = COMPLETE_TYPE_P (t2);
433
ca8bdb78
JM
434 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
435 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
436
437 d1_variable = (!d1_zero
438 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
439 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
440 d2_variable = (!d2_zero
441 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
442 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
443 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
444 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 445
400fbf9f 446 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
447 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
448 && (d2_variable || d2_zero || !d1_variable))
4b027d16 449 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
450 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
451 && (d1_variable || d1_zero || !d2_variable))
4b027d16 452 return build_type_attribute_variant (t2, attributes);
c22cacf3 453
de46b2fe
AP
454 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
455 return build_type_attribute_variant (t1, attributes);
456 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
457 return build_type_attribute_variant (t2, attributes);
c22cacf3 458
46df2823
JM
459 /* Merge the element types, and have a size if either arg has
460 one. We may have qualifiers on the element types. To set
461 up TYPE_MAIN_VARIANT correctly, we need to form the
462 composite of the unqualified types and add the qualifiers
463 back at the end. */
464 quals = TYPE_QUALS (strip_array_types (elt));
465 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
466 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
467 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
468 && (d2_variable
469 || d2_zero
470 || !d1_variable))
471 ? t1
472 : t2));
f6294de7
JM
473 /* Ensure a composite type involving a zero-length array type
474 is a zero-length type not an incomplete type. */
475 if (d1_zero && d2_zero
476 && (t1_complete || t2_complete)
477 && !COMPLETE_TYPE_P (t1))
478 {
479 TYPE_SIZE (t1) = bitsize_zero_node;
480 TYPE_SIZE_UNIT (t1) = size_zero_node;
481 }
46df2823 482 t1 = c_build_qualified_type (t1, quals);
de46b2fe 483 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
484 }
485
fcb99e7b
JJ
486 case ENUMERAL_TYPE:
487 case RECORD_TYPE:
488 case UNION_TYPE:
489 if (attributes != NULL)
490 {
491 /* Try harder not to create a new aggregate type. */
492 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
493 return t1;
494 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
495 return t2;
496 }
497 return build_type_attribute_variant (t1, attributes);
498
400fbf9f
JW
499 case FUNCTION_TYPE:
500 /* Function types: prefer the one that specified arg types.
501 If both do, merge the arg types. Also merge the return types. */
502 {
10bc1b1b 503 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
504 tree p1 = TYPE_ARG_TYPES (t1);
505 tree p2 = TYPE_ARG_TYPES (t2);
506 int len;
507 tree newargs, n;
508 int i;
509
510 /* Save space: see if the result is identical to one of the args. */
3f75a254 511 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 512 return build_type_attribute_variant (t1, attributes);
3f75a254 513 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 514 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
515
516 /* Simple way if one arg fails to specify argument types. */
517 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 518 {
fe7080d2
AP
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
4b027d16 522 }
400fbf9f 523 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
524 {
525 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
526 t1 = build_type_attribute_variant (t1, attributes);
527 return qualify_type (t1, t2);
4b027d16 528 }
400fbf9f
JW
529
530 /* If both args specify argument types, we must merge the two
531 lists, argument by argument. */
2f4e8f2b 532
400fbf9f
JW
533 len = list_length (p1);
534 newargs = 0;
535
536 for (i = 0; i < len; i++)
8d9bfdc5 537 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
538
539 n = newargs;
540
541 for (; p1;
542 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
543 {
544 /* A null type means arg type is not specified.
545 Take whatever the other function type has. */
546 if (TREE_VALUE (p1) == 0)
547 {
548 TREE_VALUE (n) = TREE_VALUE (p2);
549 goto parm_done;
550 }
551 if (TREE_VALUE (p2) == 0)
552 {
553 TREE_VALUE (n) = TREE_VALUE (p1);
554 goto parm_done;
555 }
2f6e4e97 556
400fbf9f
JW
557 /* Given wait (union {union wait *u; int *i} *)
558 and wait (union wait *),
559 prefer union wait * as type of parm. */
560 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
561 && TREE_VALUE (p1) != TREE_VALUE (p2))
562 {
563 tree memb;
58cb41e6
JJ
564 tree mv2 = TREE_VALUE (p2);
565 if (mv2 && mv2 != error_mark_node
566 && TREE_CODE (mv2) != ARRAY_TYPE)
567 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 568 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 569 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
570 {
571 tree mv3 = TREE_TYPE (memb);
572 if (mv3 && mv3 != error_mark_node
573 && TREE_CODE (mv3) != ARRAY_TYPE)
574 mv3 = TYPE_MAIN_VARIANT (mv3);
575 if (comptypes (mv3, mv2))
576 {
577 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
578 TREE_VALUE (p2));
c1771a20 579 pedwarn (input_location, OPT_Wpedantic,
fcf73884 580 "function types not truly compatible in ISO C");
58cb41e6
JJ
581 goto parm_done;
582 }
583 }
400fbf9f
JW
584 }
585 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
586 && TREE_VALUE (p2) != TREE_VALUE (p1))
587 {
588 tree memb;
58cb41e6
JJ
589 tree mv1 = TREE_VALUE (p1);
590 if (mv1 && mv1 != error_mark_node
591 && TREE_CODE (mv1) != ARRAY_TYPE)
592 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 593 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 594 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
595 {
596 tree mv3 = TREE_TYPE (memb);
597 if (mv3 && mv3 != error_mark_node
598 && TREE_CODE (mv3) != ARRAY_TYPE)
599 mv3 = TYPE_MAIN_VARIANT (mv3);
600 if (comptypes (mv3, mv1))
601 {
602 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
603 TREE_VALUE (p1));
c1771a20 604 pedwarn (input_location, OPT_Wpedantic,
fcf73884 605 "function types not truly compatible in ISO C");
58cb41e6
JJ
606 goto parm_done;
607 }
608 }
400fbf9f 609 }
10bc1b1b 610 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
611 parm_done: ;
612 }
613
4b027d16 614 t1 = build_function_type (valtype, newargs);
fe7080d2 615 t1 = qualify_type (t1, t2);
0f41302f 616 /* ... falls through ... */
400fbf9f
JW
617 }
618
619 default:
4b027d16 620 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
621 }
622
623}
10bc1b1b
JM
624
625/* Return the type of a conditional expression between pointers to
626 possibly differently qualified versions of compatible types.
627
628 We assume that comp_target_types has already been done and returned
629 nonzero; if that isn't so, this may crash. */
630
631static tree
632common_pointer_type (tree t1, tree t2)
633{
634 tree attributes;
46df2823
JM
635 tree pointed_to_1, mv1;
636 tree pointed_to_2, mv2;
10bc1b1b 637 tree target;
eb1387a0 638 unsigned target_quals;
36c5e70a
BE
639 addr_space_t as1, as2, as_common;
640 int quals1, quals2;
10bc1b1b
JM
641
642 /* Save time if the two types are the same. */
643
644 if (t1 == t2) return t1;
645
646 /* If one type is nonsense, use the other. */
647 if (t1 == error_mark_node)
648 return t2;
649 if (t2 == error_mark_node)
650 return t1;
651
366de0ce 652 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 653 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
654
655 /* Merge the attributes. */
656 attributes = targetm.merge_type_attributes (t1, t2);
657
658 /* Find the composite type of the target types, and combine the
46df2823
JM
659 qualifiers of the two types' targets. Do not lose qualifiers on
660 array element types by taking the TYPE_MAIN_VARIANT. */
661 mv1 = pointed_to_1 = TREE_TYPE (t1);
662 mv2 = pointed_to_2 = TREE_TYPE (t2);
663 if (TREE_CODE (mv1) != ARRAY_TYPE)
664 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
665 if (TREE_CODE (mv2) != ARRAY_TYPE)
666 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
667 target = composite_type (mv1, mv2);
eb1387a0
RG
668
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
36c5e70a
BE
672 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
673 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
674
eb1387a0 675 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 676 target_quals = (quals1 & quals2);
eb1387a0 677 else
36c5e70a
BE
678 target_quals = (quals1 | quals2);
679
680 /* If the two named address spaces are different, determine the common
681 superset address space. This is guaranteed to exist due to the
682 assumption that comp_target_type returned non-zero. */
683 as1 = TYPE_ADDR_SPACE (pointed_to_1);
684 as2 = TYPE_ADDR_SPACE (pointed_to_2);
685 if (!addr_space_superset (as1, as2, &as_common))
686 gcc_unreachable ();
687
688 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
689
eb1387a0 690 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
691 return build_type_attribute_variant (t1, attributes);
692}
693
694/* Return the common type for two arithmetic types under the usual
695 arithmetic conversions. The default conversions have already been
696 applied, and enumerated types converted to their compatible integer
697 types. The resulting type is unqualified and has no attributes.
698
699 This is the type for the result of most arithmetic operations
700 if the operands have the given two types. */
701
ccf7f880
JJ
702static tree
703c_common_type (tree t1, tree t2)
10bc1b1b
JM
704{
705 enum tree_code code1;
706 enum tree_code code2;
707
708 /* If one type is nonsense, use the other. */
709 if (t1 == error_mark_node)
710 return t2;
711 if (t2 == error_mark_node)
712 return t1;
713
714 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
715 t1 = TYPE_MAIN_VARIANT (t1);
716
717 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
718 t2 = TYPE_MAIN_VARIANT (t2);
719
720 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
721 t1 = build_type_attribute_variant (t1, NULL_TREE);
722
723 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
724 t2 = build_type_attribute_variant (t2, NULL_TREE);
725
726 /* Save time if the two types are the same. */
727
728 if (t1 == t2) return t1;
729
730 code1 = TREE_CODE (t1);
731 code2 = TREE_CODE (t2);
732
366de0ce 733 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
734 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
735 || code1 == INTEGER_TYPE);
366de0ce 736 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
737 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
738 || code2 == INTEGER_TYPE);
10bc1b1b 739
5fc89bfd
JJ
740 /* When one operand is a decimal float type, the other operand cannot be
741 a generic float type or a complex type. We also disallow vector types
742 here. */
743 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
744 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
745 {
746 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
747 {
748 error ("can%'t mix operands of decimal float and vector types");
749 return error_mark_node;
750 }
751 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
752 {
753 error ("can%'t mix operands of decimal float and complex types");
754 return error_mark_node;
755 }
756 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
757 {
758 error ("can%'t mix operands of decimal float and other float types");
759 return error_mark_node;
760 }
761 }
762
10bc1b1b
JM
763 /* If one type is a vector type, return that type. (How the usual
764 arithmetic conversions apply to the vector types extension is not
765 precisely specified.) */
766 if (code1 == VECTOR_TYPE)
767 return t1;
768
769 if (code2 == VECTOR_TYPE)
770 return t2;
771
772 /* If one type is complex, form the common type of the non-complex
773 components, then make that complex. Use T1 or T2 if it is the
774 required type. */
775 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
776 {
777 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
778 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 779 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
780
781 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
782 return t1;
783 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
784 return t2;
785 else
786 return build_complex_type (subtype);
787 }
788
789 /* If only one is real, use it as the result. */
790
791 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
792 return t1;
793
794 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
795 return t2;
796
9a8ce21f
JG
797 /* If both are real and either are decimal floating point types, use
798 the decimal floating point type with the greater precision. */
799
800 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
801 {
802 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
804 return dfloat128_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
807 return dfloat64_type_node;
808 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
809 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
810 return dfloat32_type_node;
811 }
812
ab22c1fa
CF
813 /* Deal with fixed-point types. */
814 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
815 {
816 unsigned int unsignedp = 0, satp = 0;
817 enum machine_mode m1, m2;
818 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
819
820 m1 = TYPE_MODE (t1);
821 m2 = TYPE_MODE (t2);
822
823 /* If one input type is saturating, the result type is saturating. */
824 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
825 satp = 1;
826
827 /* If both fixed-point types are unsigned, the result type is unsigned.
828 When mixing fixed-point and integer types, follow the sign of the
829 fixed-point type.
830 Otherwise, the result type is signed. */
831 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
832 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
833 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t1))
835 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
836 && TYPE_UNSIGNED (t2)))
837 unsignedp = 1;
838
839 /* The result type is signed. */
840 if (unsignedp == 0)
841 {
842 /* If the input type is unsigned, we need to convert to the
843 signed type. */
844 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
845 {
d75d71e0 846 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
847 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
848 mclass = MODE_FRACT;
849 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
850 mclass = MODE_ACCUM;
851 else
852 gcc_unreachable ();
853 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
854 }
855 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
856 {
d75d71e0 857 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
858 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
859 mclass = MODE_FRACT;
860 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
861 mclass = MODE_ACCUM;
862 else
863 gcc_unreachable ();
864 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
865 }
866 }
867
868 if (code1 == FIXED_POINT_TYPE)
869 {
870 fbit1 = GET_MODE_FBIT (m1);
871 ibit1 = GET_MODE_IBIT (m1);
872 }
873 else
874 {
875 fbit1 = 0;
876 /* Signed integers need to subtract one sign bit. */
877 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
878 }
879
880 if (code2 == FIXED_POINT_TYPE)
881 {
882 fbit2 = GET_MODE_FBIT (m2);
883 ibit2 = GET_MODE_IBIT (m2);
884 }
885 else
886 {
887 fbit2 = 0;
888 /* Signed integers need to subtract one sign bit. */
889 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
890 }
891
892 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
893 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
894 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
895 satp);
896 }
897
10bc1b1b
JM
898 /* Both real or both integers; use the one with greater precision. */
899
900 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
901 return t1;
902 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
903 return t2;
904
905 /* Same precision. Prefer long longs to longs to ints when the
906 same precision, following the C99 rules on integer type rank
907 (which are equivalent to the C90 rules for C90 types). */
908
909 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
910 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
911 return long_long_unsigned_type_node;
912
913 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
914 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
915 {
916 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
917 return long_long_unsigned_type_node;
918 else
c22cacf3 919 return long_long_integer_type_node;
10bc1b1b
JM
920 }
921
922 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
923 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
924 return long_unsigned_type_node;
925
926 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
927 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
928 {
929 /* But preserve unsignedness from the other type,
930 since long cannot hold all the values of an unsigned int. */
931 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
932 return long_unsigned_type_node;
933 else
934 return long_integer_type_node;
935 }
936
937 /* Likewise, prefer long double to double even if same size. */
938 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
939 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
940 return long_double_type_node;
941
2531a1d9
JR
942 /* Likewise, prefer double to float even if same size.
943 We got a couple of embedded targets with 32 bit doubles, and the
944 pdp11 might have 64 bit floats. */
945 if (TYPE_MAIN_VARIANT (t1) == double_type_node
946 || TYPE_MAIN_VARIANT (t2) == double_type_node)
947 return double_type_node;
948
10bc1b1b
JM
949 /* Otherwise prefer the unsigned one. */
950
951 if (TYPE_UNSIGNED (t1))
952 return t1;
953 else
954 return t2;
955}
400fbf9f 956\f
5922c215
JM
957/* Wrapper around c_common_type that is used by c-common.c and other
958 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
959 are allowed here and are converted to their compatible integer types.
960 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
961 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
962tree
963common_type (tree t1, tree t2)
964{
965 if (TREE_CODE (t1) == ENUMERAL_TYPE)
966 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
967 if (TREE_CODE (t2) == ENUMERAL_TYPE)
968 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
969
970 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
971 if (TREE_CODE (t1) == BOOLEAN_TYPE
972 && TREE_CODE (t2) == BOOLEAN_TYPE)
973 return boolean_type_node;
974
975 /* If either type is BOOLEAN_TYPE, then return the other. */
976 if (TREE_CODE (t1) == BOOLEAN_TYPE)
977 return t2;
978 if (TREE_CODE (t2) == BOOLEAN_TYPE)
979 return t1;
980
ccf7f880
JJ
981 return c_common_type (t1, t2);
982}
f13c9b2c 983
400fbf9f
JW
984/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
985 or various other operations. Return 2 if they are compatible
986 but a warning may be needed if you use them together. */
987
988int
132da1a5 989comptypes (tree type1, tree type2)
f13c9b2c
AP
990{
991 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
992 int val;
993
dc5027f4 994 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
995 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
996
997 return val;
998}
999
1000/* Like comptypes, but if it returns non-zero because enum and int are
1001 compatible, it sets *ENUM_AND_INT_P to true. */
1002
1003static int
1004comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1005{
1006 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1007 int val;
1008
dc5027f4
JM
1009 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1010 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1011
1012 return val;
1013}
1014
1015/* Like comptypes, but if it returns nonzero for different types, it
1016 sets *DIFFERENT_TYPES_P to true. */
1017
1018int
1019comptypes_check_different_types (tree type1, tree type2,
1020 bool *different_types_p)
1021{
1022 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1023 int val;
1024
1025 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1026 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1027
f13c9b2c 1028 return val;
c22cacf3
MS
1029}
1030\f
f13c9b2c
AP
1031/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1032 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1033 but a warning may be needed if you use them together. If
1034 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1035 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1036 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1037 NULL, and the types are compatible but different enough not to be
48b0b196 1038 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1039 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1040 false, but may or may not be set if the types are incompatible.
1041 This differs from comptypes, in that we don't free the seen
1042 types. */
f13c9b2c
AP
1043
1044static int
dc5027f4
JM
1045comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1046 bool *different_types_p)
400fbf9f 1047{
58f9752a
KG
1048 const_tree t1 = type1;
1049 const_tree t2 = type2;
4b027d16 1050 int attrval, val;
400fbf9f
JW
1051
1052 /* Suppress errors caused by previously reported errors. */
1053
8d47dfc5
RH
1054 if (t1 == t2 || !t1 || !t2
1055 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1056 return 1;
1057
bca63328
JM
1058 /* Enumerated types are compatible with integer types, but this is
1059 not transitive: two enumerated types in the same translation unit
1060 are compatible with each other only if they are the same type. */
400fbf9f 1061
bca63328 1062 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1063 {
1064 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1065 if (TREE_CODE (t2) != VOID_TYPE)
1066 {
1067 if (enum_and_int_p != NULL)
1068 *enum_and_int_p = true;
1069 if (different_types_p != NULL)
1070 *different_types_p = true;
1071 }
744aa42f 1072 }
bca63328 1073 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1074 {
1075 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1076 if (TREE_CODE (t1) != VOID_TYPE)
1077 {
1078 if (enum_and_int_p != NULL)
1079 *enum_and_int_p = true;
1080 if (different_types_p != NULL)
1081 *different_types_p = true;
1082 }
744aa42f 1083 }
400fbf9f
JW
1084
1085 if (t1 == t2)
1086 return 1;
1087
1088 /* Different classes of types can't be compatible. */
1089
3aeb3655
EC
1090 if (TREE_CODE (t1) != TREE_CODE (t2))
1091 return 0;
400fbf9f 1092
118a3a8b 1093 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1094
3932261a 1095 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1096 return 0;
1097
08632da2
RS
1098 /* Allow for two different type nodes which have essentially the same
1099 definition. Note that we already checked for equality of the type
38e01259 1100 qualifiers (just above). */
400fbf9f 1101
46df2823
JM
1102 if (TREE_CODE (t1) != ARRAY_TYPE
1103 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1104 return 1;
1105
4b027d16 1106 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1107 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1108 return 0;
1109
1110 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1111 val = 0;
1112
400fbf9f
JW
1113 switch (TREE_CODE (t1))
1114 {
1115 case POINTER_TYPE:
106f5de5
UW
1116 /* Do not remove mode or aliasing information. */
1117 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1118 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1119 break;
4b027d16 1120 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1121 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1122 enum_and_int_p, different_types_p));
4b027d16 1123 break;
400fbf9f
JW
1124
1125 case FUNCTION_TYPE:
dc5027f4
JM
1126 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1127 different_types_p);
4b027d16 1128 break;
400fbf9f
JW
1129
1130 case ARRAY_TYPE:
1131 {
400fbf9f
JW
1132 tree d1 = TYPE_DOMAIN (t1);
1133 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1134 bool d1_variable, d2_variable;
1135 bool d1_zero, d2_zero;
4b027d16 1136 val = 1;
400fbf9f
JW
1137
1138 /* Target types must match incl. qualifiers. */
1139 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1140 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1141 enum_and_int_p,
1142 different_types_p)))
400fbf9f
JW
1143 return 0;
1144
dc5027f4
JM
1145 if (different_types_p != NULL
1146 && (d1 == 0) != (d2 == 0))
1147 *different_types_p = true;
400fbf9f 1148 /* Sizes must match unless one is missing or variable. */
3f85558f 1149 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1150 break;
400fbf9f 1151
3f75a254
JM
1152 d1_zero = !TYPE_MAX_VALUE (d1);
1153 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1154
3f75a254 1155 d1_variable = (!d1_zero
3f85558f
RH
1156 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1157 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1158 d2_variable = (!d2_zero
3f85558f
RH
1159 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1160 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1161 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1162 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1163
dc5027f4
JM
1164 if (different_types_p != NULL
1165 && d1_variable != d2_variable)
1166 *different_types_p = true;
3f85558f
RH
1167 if (d1_variable || d2_variable)
1168 break;
1169 if (d1_zero && d2_zero)
1170 break;
1171 if (d1_zero || d2_zero
3f75a254
JM
1172 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1173 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1174 val = 0;
1175
c22cacf3 1176 break;
400fbf9f
JW
1177 }
1178
d1bd0ded 1179 case ENUMERAL_TYPE:
58393038 1180 case RECORD_TYPE:
d1bd0ded 1181 case UNION_TYPE:
766beae1 1182 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1183 {
fcb99e7b
JJ
1184 tree a1 = TYPE_ATTRIBUTES (t1);
1185 tree a2 = TYPE_ATTRIBUTES (t2);
1186
1187 if (! attribute_list_contained (a1, a2)
1188 && ! attribute_list_contained (a2, a1))
1189 break;
1190
f13c9b2c 1191 if (attrval != 2)
dc5027f4
JM
1192 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1193 different_types_p);
1194 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1195 different_types_p);
f13c9b2c 1196 }
4b027d16 1197 break;
e9a25f70 1198
62e1dfcf 1199 case VECTOR_TYPE:
744aa42f
ILT
1200 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1201 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1202 enum_and_int_p, different_types_p));
62e1dfcf
NC
1203 break;
1204
e9a25f70
JL
1205 default:
1206 break;
400fbf9f 1207 }
4b027d16 1208 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1209}
1210
36c5e70a
BE
1211/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1212 their qualifiers, except for named address spaces. If the pointers point to
1213 different named addresses, then we must determine if one address space is a
1214 subset of the other. */
400fbf9f
JW
1215
1216static int
744aa42f 1217comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1218{
392202b0 1219 int val;
36c5e70a
BE
1220 tree mvl = TREE_TYPE (ttl);
1221 tree mvr = TREE_TYPE (ttr);
1222 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1223 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1224 addr_space_t as_common;
744aa42f 1225 bool enum_and_int_p;
8b40563c 1226
36c5e70a
BE
1227 /* Fail if pointers point to incompatible address spaces. */
1228 if (!addr_space_superset (asl, asr, &as_common))
1229 return 0;
1230
46df2823
JM
1231 /* Do not lose qualifiers on element types of array types that are
1232 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1233 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1234 mvl = (TYPE_ATOMIC (mvl)
1235 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1236 : TYPE_MAIN_VARIANT (mvl));
46df2823 1237 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1238 mvr = (TYPE_ATOMIC (mvr)
1239 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1240 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1241 enum_and_int_p = false;
1242 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1243
fcf73884 1244 if (val == 2)
c1771a20 1245 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1246
1247 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1248 warning_at (location, OPT_Wc___compat,
1249 "pointer target types incompatible in C++");
1250
400fbf9f
JW
1251 return val;
1252}
1253\f
1254/* Subroutines of `comptypes'. */
1255
f75fbaf7
ZW
1256/* Determine whether two trees derive from the same translation unit.
1257 If the CONTEXT chain ends in a null, that tree's context is still
1258 being parsed, so if two trees have context chains ending in null,
766beae1 1259 they're in the same translation unit. */
f75fbaf7 1260int
58f9752a 1261same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1262{
1263 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1264 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1265 {
6615c446
JO
1266 case tcc_declaration:
1267 t1 = DECL_CONTEXT (t1); break;
1268 case tcc_type:
1269 t1 = TYPE_CONTEXT (t1); break;
1270 case tcc_exceptional:
1271 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1272 default: gcc_unreachable ();
766beae1
ZW
1273 }
1274
1275 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1276 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1277 {
6615c446
JO
1278 case tcc_declaration:
1279 t2 = DECL_CONTEXT (t2); break;
1280 case tcc_type:
1281 t2 = TYPE_CONTEXT (t2); break;
1282 case tcc_exceptional:
1283 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1284 default: gcc_unreachable ();
766beae1
ZW
1285 }
1286
1287 return t1 == t2;
1288}
1289
f13c9b2c 1290/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1291
f13c9b2c 1292static struct tagged_tu_seen_cache *
58f9752a 1293alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1294{
cceb1885 1295 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1296 tu->next = tagged_tu_seen_base;
1297 tu->t1 = t1;
1298 tu->t2 = t2;
c22cacf3 1299
f13c9b2c 1300 tagged_tu_seen_base = tu;
c22cacf3 1301
f13c9b2c
AP
1302 /* The C standard says that two structures in different translation
1303 units are compatible with each other only if the types of their
1304 fields are compatible (among other things). We assume that they
1305 are compatible until proven otherwise when building the cache.
1306 An example where this can occur is:
1307 struct a
1308 {
1309 struct a *next;
1310 };
1311 If we are comparing this against a similar struct in another TU,
c83eecad 1312 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1313 loop. */
1314 tu->val = 1;
1315 return tu;
1316}
d1bd0ded 1317
f13c9b2c 1318/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1319
f13c9b2c
AP
1320static void
1321free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1322{
1323 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1324 while (tu != tu_til)
1325 {
741ac903
KG
1326 const struct tagged_tu_seen_cache *const tu1
1327 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1328 tu = tu1->next;
b1d5455a 1329 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1330 }
1331 tagged_tu_seen_base = tu_til;
1332}
d1bd0ded
GK
1333
1334/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1335 compatible. If the two types are not the same (which has been
1336 checked earlier), this can only happen when multiple translation
1337 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1338 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1339 comptypes_internal. */
d1bd0ded
GK
1340
1341static int
744aa42f 1342tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1343 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1344{
1345 tree s1, s2;
1346 bool needs_warning = false;
3aeb3655 1347
d1bd0ded
GK
1348 /* We have to verify that the tags of the types are the same. This
1349 is harder than it looks because this may be a typedef, so we have
1350 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1351 typedef...
1352 In the case of compiler-created builtin structs the TYPE_DECL
1353 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1354 while (TYPE_NAME (t1)
1355 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1356 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1357 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1358
dea984dc
ILT
1359 while (TYPE_NAME (t2)
1360 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1361 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1362 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1363
1364 /* C90 didn't have the requirement that the two tags be the same. */
1365 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1366 return 0;
3aeb3655 1367
d1bd0ded
GK
1368 /* C90 didn't say what happened if one or both of the types were
1369 incomplete; we choose to follow C99 rules here, which is that they
1370 are compatible. */
1371 if (TYPE_SIZE (t1) == NULL
1372 || TYPE_SIZE (t2) == NULL)
1373 return 1;
3aeb3655 1374
d1bd0ded 1375 {
f13c9b2c 1376 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1377 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1378 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1379 return tts_i->val;
d1bd0ded 1380 }
3aeb3655 1381
d1bd0ded
GK
1382 switch (TREE_CODE (t1))
1383 {
1384 case ENUMERAL_TYPE:
1385 {
f13c9b2c 1386 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1387 /* Speed up the case where the type values are in the same order. */
1388 tree tv1 = TYPE_VALUES (t1);
1389 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1390
c22cacf3 1391 if (tv1 == tv2)
f13c9b2c
AP
1392 {
1393 return 1;
1394 }
3aeb3655 1395
c22cacf3
MS
1396 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1397 {
1398 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1399 break;
1400 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1401 {
c22cacf3 1402 tu->val = 0;
f13c9b2c
AP
1403 return 0;
1404 }
c22cacf3 1405 }
3aeb3655 1406
c22cacf3 1407 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1408 {
1409 return 1;
1410 }
c22cacf3 1411 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1412 {
1413 tu->val = 0;
1414 return 0;
1415 }
3aeb3655 1416
d1bd0ded 1417 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1418 {
1419 tu->val = 0;
1420 return 0;
1421 }
3aeb3655 1422
d1bd0ded
GK
1423 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1424 {
1425 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1426 if (s2 == NULL
1427 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1428 {
1429 tu->val = 0;
1430 return 0;
1431 }
d1bd0ded
GK
1432 }
1433 return 1;
1434 }
1435
1436 case UNION_TYPE:
1437 {
f13c9b2c 1438 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1439 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1440 {
1441 tu->val = 0;
1442 return 0;
1443 }
c22cacf3 1444
f13c9b2c
AP
1445 /* Speed up the common case where the fields are in the same order. */
1446 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1447 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1448 {
1449 int result;
c22cacf3 1450
3ae4d3cc 1451 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1452 break;
744aa42f 1453 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1454 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1455
1456 if (result != 1 && !DECL_NAME (s1))
1457 break;
f13c9b2c
AP
1458 if (result == 0)
1459 {
1460 tu->val = 0;
1461 return 0;
1462 }
1463 if (result == 2)
1464 needs_warning = true;
1465
1466 if (TREE_CODE (s1) == FIELD_DECL
1467 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1468 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1469 {
1470 tu->val = 0;
1471 return 0;
1472 }
1473 }
1474 if (!s1 && !s2)
1475 {
1476 tu->val = needs_warning ? 2 : 1;
1477 return tu->val;
1478 }
d1bd0ded 1479
910ad8de 1480 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1481 {
1482 bool ok = false;
3aeb3655 1483
910ad8de 1484 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1485 if (DECL_NAME (s1) == DECL_NAME (s2))
1486 {
1487 int result;
1488
744aa42f 1489 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1490 enum_and_int_p,
1491 different_types_p);
3ae4d3cc
AO
1492
1493 if (result != 1 && !DECL_NAME (s1))
1494 continue;
1495 if (result == 0)
1496 {
1497 tu->val = 0;
1498 return 0;
1499 }
1500 if (result == 2)
1501 needs_warning = true;
1502
1503 if (TREE_CODE (s1) == FIELD_DECL
1504 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1505 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1506 break;
3ae4d3cc
AO
1507
1508 ok = true;
1509 break;
1510 }
3f75a254 1511 if (!ok)
f13c9b2c
AP
1512 {
1513 tu->val = 0;
1514 return 0;
1515 }
d1bd0ded 1516 }
f13c9b2c
AP
1517 tu->val = needs_warning ? 2 : 10;
1518 return tu->val;
d1bd0ded
GK
1519 }
1520
1521 case RECORD_TYPE:
1522 {
c22cacf3 1523 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1524
1525 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1526 s1 && s2;
910ad8de 1527 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1528 {
1529 int result;
1530 if (TREE_CODE (s1) != TREE_CODE (s2)
1531 || DECL_NAME (s1) != DECL_NAME (s2))
1532 break;
744aa42f 1533 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1534 enum_and_int_p, different_types_p);
d1bd0ded
GK
1535 if (result == 0)
1536 break;
1537 if (result == 2)
1538 needs_warning = true;
3aeb3655 1539
d1bd0ded
GK
1540 if (TREE_CODE (s1) == FIELD_DECL
1541 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1542 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1543 break;
1544 }
d1bd0ded 1545 if (s1 && s2)
f13c9b2c
AP
1546 tu->val = 0;
1547 else
1548 tu->val = needs_warning ? 2 : 1;
1549 return tu->val;
d1bd0ded
GK
1550 }
1551
1552 default:
366de0ce 1553 gcc_unreachable ();
d1bd0ded
GK
1554 }
1555}
1556
400fbf9f
JW
1557/* Return 1 if two function types F1 and F2 are compatible.
1558 If either type specifies no argument types,
1559 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1560 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1561 the other must specify that number of self-promoting arg types.
744aa42f 1562 Otherwise, the argument types must match.
dc5027f4 1563 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1564
1565static int
744aa42f 1566function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1567 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1568{
1569 tree args1, args2;
1570 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1571 int val = 1;
1572 int val1;
a6fdc086
GK
1573 tree ret1, ret2;
1574
1575 ret1 = TREE_TYPE (f1);
1576 ret2 = TREE_TYPE (f2);
1577
e508a019
JM
1578 /* 'volatile' qualifiers on a function's return type used to mean
1579 the function is noreturn. */
1580 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1581 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1582 if (TYPE_VOLATILE (ret1))
1583 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1584 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1585 if (TYPE_VOLATILE (ret2))
1586 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1587 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1588 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1589 if (val == 0)
400fbf9f
JW
1590 return 0;
1591
1592 args1 = TYPE_ARG_TYPES (f1);
1593 args2 = TYPE_ARG_TYPES (f2);
1594
dc5027f4
JM
1595 if (different_types_p != NULL
1596 && (args1 == 0) != (args2 == 0))
1597 *different_types_p = true;
1598
400fbf9f
JW
1599 /* An unspecified parmlist matches any specified parmlist
1600 whose argument types don't need default promotions. */
1601
1602 if (args1 == 0)
1603 {
1604 if (!self_promoting_args_p (args2))
1605 return 0;
1606 /* If one of these types comes from a non-prototype fn definition,
1607 compare that with the other type's arglist.
3176a0c2 1608 If they don't match, ask for a warning (but no error). */
400fbf9f 1609 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1610 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1611 enum_and_int_p, different_types_p))
400fbf9f
JW
1612 val = 2;
1613 return val;
1614 }
1615 if (args2 == 0)
1616 {
1617 if (!self_promoting_args_p (args1))
1618 return 0;
1619 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1620 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1621 enum_and_int_p, different_types_p))
400fbf9f
JW
1622 val = 2;
1623 return val;
1624 }
1625
1626 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1627 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1628 different_types_p);
400fbf9f
JW
1629 return val1 != 1 ? val1 : val;
1630}
1631
744aa42f
ILT
1632/* Check two lists of types for compatibility, returning 0 for
1633 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1634 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1635 comptypes_internal. */
400fbf9f
JW
1636
1637static int
744aa42f 1638type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1639 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1640{
1641 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1642 int val = 1;
9d5f3e49 1643 int newval = 0;
400fbf9f
JW
1644
1645 while (1)
1646 {
46df2823 1647 tree a1, mv1, a2, mv2;
400fbf9f
JW
1648 if (args1 == 0 && args2 == 0)
1649 return val;
1650 /* If one list is shorter than the other,
1651 they fail to match. */
1652 if (args1 == 0 || args2 == 0)
1653 return 0;
46df2823
JM
1654 mv1 = a1 = TREE_VALUE (args1);
1655 mv2 = a2 = TREE_VALUE (args2);
1656 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1657 mv1 = (TYPE_ATOMIC (mv1)
1658 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1659 TYPE_QUAL_ATOMIC)
1660 : TYPE_MAIN_VARIANT (mv1));
46df2823 1661 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1662 mv2 = (TYPE_ATOMIC (mv2)
1663 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1664 TYPE_QUAL_ATOMIC)
1665 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1666 /* A null pointer instead of a type
1667 means there is supposed to be an argument
1668 but nothing is specified about what type it has.
1669 So match anything that self-promotes. */
dc5027f4
JM
1670 if (different_types_p != NULL
1671 && (a1 == 0) != (a2 == 0))
1672 *different_types_p = true;
46df2823 1673 if (a1 == 0)
400fbf9f 1674 {
46df2823 1675 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1676 return 0;
1677 }
46df2823 1678 else if (a2 == 0)
400fbf9f 1679 {
46df2823 1680 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1681 return 0;
1682 }
8f5b6d29 1683 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1684 else if (TREE_CODE (a1) == ERROR_MARK
1685 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1686 ;
dc5027f4
JM
1687 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1688 different_types_p)))
400fbf9f 1689 {
dc5027f4
JM
1690 if (different_types_p != NULL)
1691 *different_types_p = true;
400fbf9f
JW
1692 /* Allow wait (union {union wait *u; int *i} *)
1693 and wait (union wait *) to be compatible. */
46df2823
JM
1694 if (TREE_CODE (a1) == UNION_TYPE
1695 && (TYPE_NAME (a1) == 0
ebf0bf7f 1696 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1697 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1698 && tree_int_cst_equal (TYPE_SIZE (a1),
1699 TYPE_SIZE (a2)))
400fbf9f
JW
1700 {
1701 tree memb;
46df2823 1702 for (memb = TYPE_FIELDS (a1);
910ad8de 1703 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1704 {
1705 tree mv3 = TREE_TYPE (memb);
1706 if (mv3 && mv3 != error_mark_node
1707 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1708 mv3 = (TYPE_ATOMIC (mv3)
1709 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1710 TYPE_QUAL_ATOMIC)
1711 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1712 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1713 different_types_p))
58cb41e6
JJ
1714 break;
1715 }
400fbf9f
JW
1716 if (memb == 0)
1717 return 0;
1718 }
46df2823
JM
1719 else if (TREE_CODE (a2) == UNION_TYPE
1720 && (TYPE_NAME (a2) == 0
ebf0bf7f 1721 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1722 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1723 && tree_int_cst_equal (TYPE_SIZE (a2),
1724 TYPE_SIZE (a1)))
400fbf9f
JW
1725 {
1726 tree memb;
46df2823 1727 for (memb = TYPE_FIELDS (a2);
910ad8de 1728 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1729 {
1730 tree mv3 = TREE_TYPE (memb);
1731 if (mv3 && mv3 != error_mark_node
1732 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1733 mv3 = (TYPE_ATOMIC (mv3)
1734 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1735 TYPE_QUAL_ATOMIC)
1736 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1737 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1738 different_types_p))
58cb41e6
JJ
1739 break;
1740 }
400fbf9f
JW
1741 if (memb == 0)
1742 return 0;
1743 }
1744 else
1745 return 0;
1746 }
1747
1748 /* comptypes said ok, but record if it said to warn. */
1749 if (newval > val)
1750 val = newval;
1751
1752 args1 = TREE_CHAIN (args1);
1753 args2 = TREE_CHAIN (args2);
1754 }
1755}
400fbf9f 1756\f
a0e24419
MP
1757/* Compute the size to increment a pointer by. When a function type or void
1758 type or incomplete type is passed, size_one_node is returned.
1759 This function does not emit any diagnostics; the caller is responsible
1760 for that. */
400fbf9f 1761
4e2fb7de 1762static tree
58f9752a 1763c_size_in_bytes (const_tree type)
400fbf9f
JW
1764{
1765 enum tree_code code = TREE_CODE (type);
1766
a0e24419
MP
1767 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1768 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1769 return size_one_node;
1770
400fbf9f 1771 /* Convert in case a char is more than one unit. */
db3927fb
AH
1772 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1773 size_int (TYPE_PRECISION (char_type_node)
1774 / BITS_PER_UNIT));
400fbf9f 1775}
400fbf9f 1776\f
400fbf9f
JW
1777/* Return either DECL or its known constant value (if it has one). */
1778
56cb9733 1779tree
2f6e4e97 1780decl_constant_value (tree decl)
400fbf9f 1781{
a7c1916a 1782 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1783 in a place where a variable is invalid. Note that DECL_INITIAL
1784 isn't valid for a PARM_DECL. */
a7c1916a 1785 current_function_decl != 0
4f976745 1786 && TREE_CODE (decl) != PARM_DECL
3f75a254 1787 && !TREE_THIS_VOLATILE (decl)
83bab8db 1788 && TREE_READONLY (decl)
400fbf9f
JW
1789 && DECL_INITIAL (decl) != 0
1790 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1791 /* This is invalid if initial value is not constant.
1792 If it has either a function call, a memory reference,
1793 or a variable, then re-evaluating it could give different results. */
1794 && TREE_CONSTANT (DECL_INITIAL (decl))
1795 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1796 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1797 return DECL_INITIAL (decl);
1798 return decl;
1799}
1800
f2a71bbc
JM
1801/* Convert the array expression EXP to a pointer. */
1802static tree
c2255bc4 1803array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1804{
f2a71bbc 1805 tree orig_exp = exp;
207bf485 1806 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1807 tree adr;
1808 tree restype = TREE_TYPE (type);
1809 tree ptrtype;
207bf485 1810
f2a71bbc 1811 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1812
f2a71bbc 1813 STRIP_TYPE_NOPS (exp);
207bf485 1814
487a92fe
JM
1815 if (TREE_NO_WARNING (orig_exp))
1816 TREE_NO_WARNING (exp) = 1;
207bf485 1817
f2a71bbc
JM
1818 ptrtype = build_pointer_type (restype);
1819
1820 if (TREE_CODE (exp) == INDIRECT_REF)
1821 return convert (ptrtype, TREE_OPERAND (exp, 0));
1822
1f37c583
JM
1823 /* In C++ array compound literals are temporary objects unless they are
1824 const or appear in namespace scope, so they are destroyed too soon
1825 to use them for much of anything (c++/53220). */
1826 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1827 {
1828 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1829 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1830 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1831 "converting an array compound literal to a pointer "
1832 "is ill-formed in C++");
1833 }
1834
c2255bc4 1835 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1836 return convert (ptrtype, adr);
1837}
207bf485 1838
f2a71bbc
JM
1839/* Convert the function expression EXP to a pointer. */
1840static tree
c2255bc4 1841function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1842{
1843 tree orig_exp = exp;
207bf485 1844
f2a71bbc 1845 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1846
f2a71bbc 1847 STRIP_TYPE_NOPS (exp);
207bf485 1848
f2a71bbc
JM
1849 if (TREE_NO_WARNING (orig_exp))
1850 TREE_NO_WARNING (exp) = 1;
207bf485 1851
c2255bc4 1852 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1853}
207bf485 1854
ebfbbdc5
JJ
1855/* Mark EXP as read, not just set, for set but not used -Wunused
1856 warning purposes. */
1857
1858void
1859mark_exp_read (tree exp)
1860{
1861 switch (TREE_CODE (exp))
1862 {
1863 case VAR_DECL:
1864 case PARM_DECL:
1865 DECL_READ_P (exp) = 1;
1866 break;
1867 case ARRAY_REF:
1868 case COMPONENT_REF:
1869 case MODIFY_EXPR:
1870 case REALPART_EXPR:
1871 case IMAGPART_EXPR:
1872 CASE_CONVERT:
1873 case ADDR_EXPR:
1874 mark_exp_read (TREE_OPERAND (exp, 0));
1875 break;
1876 case COMPOUND_EXPR:
82c3c067 1877 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1878 mark_exp_read (TREE_OPERAND (exp, 1));
1879 break;
1880 default:
1881 break;
1882 }
1883}
1884
f2a71bbc
JM
1885/* Perform the default conversion of arrays and functions to pointers.
1886 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1887 return EXP.
1888
1889 LOC is the location of the expression. */
f2a71bbc
JM
1890
1891struct c_expr
c2255bc4 1892default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1893{
1894 tree orig_exp = exp.value;
1895 tree type = TREE_TYPE (exp.value);
1896 enum tree_code code = TREE_CODE (type);
1897
1898 switch (code)
1899 {
1900 case ARRAY_TYPE:
1901 {
1902 bool not_lvalue = false;
1903 bool lvalue_array_p;
1904
1905 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1906 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1907 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1908 {
1909 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1910 not_lvalue = true;
1911 exp.value = TREE_OPERAND (exp.value, 0);
1912 }
1913
1914 if (TREE_NO_WARNING (orig_exp))
1915 TREE_NO_WARNING (exp.value) = 1;
1916
1917 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1918 if (!flag_isoc99 && !lvalue_array_p)
1919 {
1920 /* Before C99, non-lvalue arrays do not decay to pointers.
1921 Normally, using such an array would be invalid; but it can
1922 be used correctly inside sizeof or as a statement expression.
1923 Thus, do not give an error here; an error will result later. */
1924 return exp;
1925 }
1926
c2255bc4 1927 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1928 }
1929 break;
1930 case FUNCTION_TYPE:
c2255bc4 1931 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1932 break;
1933 default:
f2a71bbc 1934 break;
207bf485 1935 }
f2a71bbc 1936
207bf485
JM
1937 return exp;
1938}
1939
ebfbbdc5
JJ
1940struct c_expr
1941default_function_array_read_conversion (location_t loc, struct c_expr exp)
1942{
1943 mark_exp_read (exp.value);
1944 return default_function_array_conversion (loc, exp);
1945}
522ddfa2 1946
267bac10
JM
1947/* Return whether EXPR should be treated as an atomic lvalue for the
1948 purposes of load and store handling. */
1949
1950static bool
1951really_atomic_lvalue (tree expr)
1952{
1953 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1954 return false;
1955 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1956 return false;
1957 if (!lvalue_p (expr))
1958 return false;
1959
1960 /* Ignore _Atomic on register variables, since their addresses can't
1961 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1962 sequences wouldn't work. Ignore _Atomic on structures containing
1963 bit-fields, since accessing elements of atomic structures or
1964 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1965 it's undefined at translation time or execution time, and the
1966 normal atomic sequences again wouldn't work. */
1967 while (handled_component_p (expr))
1968 {
1969 if (TREE_CODE (expr) == COMPONENT_REF
1970 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1971 return false;
1972 expr = TREE_OPERAND (expr, 0);
1973 }
1974 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1975 return false;
1976 return true;
1977}
1978
1979/* Convert expression EXP (location LOC) from lvalue to rvalue,
1980 including converting functions and arrays to pointers if CONVERT_P.
1981 If READ_P, also mark the expression as having been read. */
1982
1983struct c_expr
1984convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1985 bool convert_p, bool read_p)
1986{
1987 if (read_p)
1988 mark_exp_read (exp.value);
1989 if (convert_p)
1990 exp = default_function_array_conversion (loc, exp);
1991 if (really_atomic_lvalue (exp.value))
1992 {
1993 vec<tree, va_gc> *params;
1994 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1995 tree expr_type = TREE_TYPE (exp.value);
1996 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1997 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1998
1999 gcc_assert (TYPE_ATOMIC (expr_type));
2000
2001 /* Expansion of a generic atomic load may require an addition
2002 element, so allocate enough to prevent a resize. */
2003 vec_alloc (params, 4);
2004
2005 /* Remove the qualifiers for the rest of the expressions and
2006 create the VAL temp variable to hold the RHS. */
2007 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2008 tmp = create_tmp_var (nonatomic_type, NULL);
2009 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2010 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2011 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2012
2013 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2014 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2015 params->quick_push (expr_addr);
2016 params->quick_push (tmp_addr);
2017 params->quick_push (seq_cst);
8edbfaa6 2018 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2019
cc28fc7f
MP
2020 /* EXPR is always read. */
2021 mark_exp_read (exp.value);
2022
267bac10
JM
2023 /* Return tmp which contains the value loaded. */
2024 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2025 }
2026 return exp;
2027}
2028
522ddfa2
JM
2029/* EXP is an expression of integer type. Apply the integer promotions
2030 to it and return the promoted value. */
400fbf9f
JW
2031
2032tree
522ddfa2 2033perform_integral_promotions (tree exp)
400fbf9f 2034{
b3694847
SS
2035 tree type = TREE_TYPE (exp);
2036 enum tree_code code = TREE_CODE (type);
400fbf9f 2037
522ddfa2 2038 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2039
400fbf9f
JW
2040 /* Normally convert enums to int,
2041 but convert wide enums to something wider. */
2042 if (code == ENUMERAL_TYPE)
2043 {
b0c48229
NB
2044 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2045 TYPE_PRECISION (integer_type_node)),
2046 ((TYPE_PRECISION (type)
2047 >= TYPE_PRECISION (integer_type_node))
8df83eae 2048 && TYPE_UNSIGNED (type)));
05bccae2 2049
400fbf9f
JW
2050 return convert (type, exp);
2051 }
2052
522ddfa2
JM
2053 /* ??? This should no longer be needed now bit-fields have their
2054 proper types. */
9753f113 2055 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2056 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2057 /* If it's thinner than an int, promote it like a
d72040f5 2058 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2059 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2060 TYPE_PRECISION (integer_type_node)))
f458d1d5 2061 return convert (integer_type_node, exp);
9753f113 2062
d72040f5 2063 if (c_promoting_integer_type_p (type))
400fbf9f 2064 {
f458d1d5 2065 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2066 if (TYPE_UNSIGNED (type)
f458d1d5 2067 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2068 return convert (unsigned_type_node, exp);
05bccae2 2069
400fbf9f
JW
2070 return convert (integer_type_node, exp);
2071 }
05bccae2 2072
522ddfa2
JM
2073 return exp;
2074}
2075
2076
2077/* Perform default promotions for C data used in expressions.
46bdb9cf 2078 Enumeral types or short or char are converted to int.
522ddfa2
JM
2079 In addition, manifest constants symbols are replaced by their values. */
2080
2081tree
2082default_conversion (tree exp)
2083{
2084 tree orig_exp;
2085 tree type = TREE_TYPE (exp);
2086 enum tree_code code = TREE_CODE (type);
40449a90 2087 tree promoted_type;
522ddfa2 2088
ebfbbdc5
JJ
2089 mark_exp_read (exp);
2090
46bdb9cf
JM
2091 /* Functions and arrays have been converted during parsing. */
2092 gcc_assert (code != FUNCTION_TYPE);
2093 if (code == ARRAY_TYPE)
2094 return exp;
522ddfa2
JM
2095
2096 /* Constants can be used directly unless they're not loadable. */
2097 if (TREE_CODE (exp) == CONST_DECL)
2098 exp = DECL_INITIAL (exp);
2099
522ddfa2
JM
2100 /* Strip no-op conversions. */
2101 orig_exp = exp;
2102 STRIP_TYPE_NOPS (exp);
2103
2104 if (TREE_NO_WARNING (orig_exp))
2105 TREE_NO_WARNING (exp) = 1;
2106
400fbf9f
JW
2107 if (code == VOID_TYPE)
2108 {
5436fa2e
MP
2109 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2110 "void value not ignored as it ought to be");
400fbf9f
JW
2111 return error_mark_node;
2112 }
808d6eaa
JM
2113
2114 exp = require_complete_type (exp);
2115 if (exp == error_mark_node)
2116 return error_mark_node;
2117
40449a90
SL
2118 promoted_type = targetm.promoted_type (type);
2119 if (promoted_type)
2120 return convert (promoted_type, exp);
2121
808d6eaa
JM
2122 if (INTEGRAL_TYPE_P (type))
2123 return perform_integral_promotions (exp);
2124
400fbf9f
JW
2125 return exp;
2126}
2127\f
0fb96aa4 2128/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2129
2130 If the component name is not found, returns NULL_TREE. Otherwise,
2131 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2132 stepping down the chain to the component, which is in the last
2133 TREE_VALUE of the list. Normally the list is of length one, but if
2134 the component is embedded within (nested) anonymous structures or
2135 unions, the list steps down the chain to the component. */
2f6e4e97 2136
2f2d13da 2137static tree
0fb96aa4 2138lookup_field (tree type, tree component)
2f2d13da
DE
2139{
2140 tree field;
2141
2142 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2143 to the field elements. Use a binary search on this array to quickly
2144 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2145 will always be set for structures which have many elements. */
2146
22a0b85f 2147 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2148 {
2149 int bot, top, half;
d07605f5 2150 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2151
2152 field = TYPE_FIELDS (type);
2153 bot = 0;
d07605f5 2154 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2155 while (top - bot > 1)
2156 {
2f2d13da
DE
2157 half = (top - bot + 1) >> 1;
2158 field = field_array[bot+half];
2159
2160 if (DECL_NAME (field) == NULL_TREE)
2161 {
2162 /* Step through all anon unions in linear fashion. */
2163 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2164 {
2f2d13da 2165 field = field_array[bot++];
a68b98cf
RK
2166 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2167 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2168 {
0fb96aa4 2169 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2170
2171 if (anon)
2172 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2173
2174 /* The Plan 9 compiler permits referring
2175 directly to an anonymous struct/union field
2176 using a typedef name. */
2177 if (flag_plan9_extensions
2178 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2179 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2180 == TYPE_DECL)
2181 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2182 == component))
2183 break;
2f6e4e97 2184 }
2f2d13da
DE
2185 }
2186
2187 /* Entire record is only anon unions. */
2188 if (bot > top)
2189 return NULL_TREE;
2190
2191 /* Restart the binary search, with new lower bound. */
2192 continue;
2193 }
2194
e8b87aac 2195 if (DECL_NAME (field) == component)
2f2d13da 2196 break;
e8b87aac 2197 if (DECL_NAME (field) < component)
2f2d13da
DE
2198 bot += half;
2199 else
2200 top = bot + half;
2201 }
2202
2203 if (DECL_NAME (field_array[bot]) == component)
2204 field = field_array[bot];
2205 else if (DECL_NAME (field) != component)
e9b2c823 2206 return NULL_TREE;
2f2d13da
DE
2207 }
2208 else
2209 {
910ad8de 2210 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2211 {
e9b2c823
NB
2212 if (DECL_NAME (field) == NULL_TREE
2213 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2214 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2215 {
0fb96aa4 2216 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2217
e9b2c823
NB
2218 if (anon)
2219 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2220
2221 /* The Plan 9 compiler permits referring directly to an
2222 anonymous struct/union field using a typedef
2223 name. */
2224 if (flag_plan9_extensions
2225 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2226 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2227 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2228 == component))
2229 break;
2f2d13da
DE
2230 }
2231
2232 if (DECL_NAME (field) == component)
2233 break;
2234 }
e9b2c823
NB
2235
2236 if (field == NULL_TREE)
2237 return NULL_TREE;
2f2d13da
DE
2238 }
2239
e9b2c823 2240 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2241}
2242
c2255bc4
AH
2243/* Make an expression to refer to the COMPONENT field of structure or
2244 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2245 location of the COMPONENT_REF. */
400fbf9f
JW
2246
2247tree
c2255bc4 2248build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2249{
b3694847
SS
2250 tree type = TREE_TYPE (datum);
2251 enum tree_code code = TREE_CODE (type);
2252 tree field = NULL;
2253 tree ref;
1e57bf47 2254 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2255
7a3ea201
RH
2256 if (!objc_is_public (datum, component))
2257 return error_mark_node;
2258
46a88c12 2259 /* Detect Objective-C property syntax object.property. */
668ea4b1 2260 if (c_dialect_objc ()
46a88c12 2261 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2262 return ref;
2263
400fbf9f
JW
2264 /* See if there is a field or component with name COMPONENT. */
2265
2266 if (code == RECORD_TYPE || code == UNION_TYPE)
2267 {
d0f062fb 2268 if (!COMPLETE_TYPE_P (type))
400fbf9f 2269 {
7a228918 2270 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2271 return error_mark_node;
2272 }
2273
0fb96aa4 2274 field = lookup_field (type, component);
400fbf9f
JW
2275
2276 if (!field)
2277 {
c2255bc4 2278 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2279 return error_mark_node;
2280 }
400fbf9f 2281
e9b2c823
NB
2282 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2283 This might be better solved in future the way the C++ front
2284 end does it - by giving the anonymous entities each a
2285 separate name and type, and then have build_component_ref
2286 recursively call itself. We can't do that here. */
46ea50cb 2287 do
19d76e60 2288 {
e9b2c823 2289 tree subdatum = TREE_VALUE (field);
efed193e
JM
2290 int quals;
2291 tree subtype;
1e57bf47 2292 bool use_datum_quals;
e9b2c823
NB
2293
2294 if (TREE_TYPE (subdatum) == error_mark_node)
2295 return error_mark_node;
2296
1e57bf47
JM
2297 /* If this is an rvalue, it does not have qualifiers in C
2298 standard terms and we must avoid propagating such
2299 qualifiers down to a non-lvalue array that is then
2300 converted to a pointer. */
2301 use_datum_quals = (datum_lvalue
2302 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2303
efed193e 2304 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2305 if (use_datum_quals)
2306 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2307 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2308
2309 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2310 NULL_TREE);
c2255bc4 2311 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2312 if (TREE_READONLY (subdatum)
2313 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2314 TREE_READONLY (ref) = 1;
1e57bf47
JM
2315 if (TREE_THIS_VOLATILE (subdatum)
2316 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2317 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2318
2319 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2320 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2321
19d76e60 2322 datum = ref;
46ea50cb
RS
2323
2324 field = TREE_CHAIN (field);
19d76e60 2325 }
46ea50cb 2326 while (field);
19d76e60 2327
400fbf9f
JW
2328 return ref;
2329 }
2330 else if (code != ERROR_MARK)
c2255bc4
AH
2331 error_at (loc,
2332 "request for member %qE in something not a structure or union",
2333 component);
400fbf9f
JW
2334
2335 return error_mark_node;
2336}
2337\f
2338/* Given an expression PTR for a pointer, return an expression
2339 for the value pointed to.
6a3799eb
AH
2340 ERRORSTRING is the name of the operator to appear in error messages.
2341
2342 LOC is the location to use for the generated tree. */
400fbf9f
JW
2343
2344tree
dd865ef6 2345build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2346{
b3694847
SS
2347 tree pointer = default_conversion (ptr);
2348 tree type = TREE_TYPE (pointer);
6a3799eb 2349 tree ref;
400fbf9f
JW
2350
2351 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2352 {
1043771b 2353 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2354 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2355 {
2356 /* If a warning is issued, mark it to avoid duplicates from
2357 the backend. This only needs to be done at
2358 warn_strict_aliasing > 2. */
2359 if (warn_strict_aliasing > 2)
2360 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2361 type, TREE_OPERAND (pointer, 0)))
2362 TREE_NO_WARNING (pointer) = 1;
2363 }
2364
870cc33b 2365 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2366 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2367 == TREE_TYPE (type)))
6a3799eb
AH
2368 {
2369 ref = TREE_OPERAND (pointer, 0);
2370 protected_set_expr_location (ref, loc);
2371 return ref;
2372 }
870cc33b
RS
2373 else
2374 {
2375 tree t = TREE_TYPE (type);
46df2823 2376
984dfd8c 2377 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2378
baae9b65 2379 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2380 {
c9f9eb5d 2381 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2382 return error_mark_node;
2383 }
7d882b83 2384 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2385 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2386
2387 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2388 so that we get the proper error message if the result is used
2389 to assign to. Also, &* is supposed to be a no-op.
2390 And ANSI C seems to specify that the type of the result
2391 should be the const type. */
2392 /* A de-reference of a pointer to const is not a const. It is valid
2393 to change it via some other pointer. */
2394 TREE_READONLY (ref) = TYPE_READONLY (t);
2395 TREE_SIDE_EFFECTS (ref)
271bd540 2396 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2397 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2398 protected_set_expr_location (ref, loc);
870cc33b
RS
2399 return ref;
2400 }
2401 }
400fbf9f 2402 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2403 invalid_indirection_error (loc, type, errstring);
2404
400fbf9f
JW
2405 return error_mark_node;
2406}
2407
2408/* This handles expressions of the form "a[i]", which denotes
2409 an array reference.
2410
2411 This is logically equivalent in C to *(a+i), but we may do it differently.
2412 If A is a variable or a member, we generate a primitive ARRAY_REF.
2413 This avoids forcing the array out of registers, and can work on
2414 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2415 by functions).
2416
30cd1c5d
AS
2417 For vector types, allow vector[i] but not i[vector], and create
2418 *(((type*)&vectortype) + i) for the expression.
2419
6a3799eb 2420 LOC is the location to use for the returned expression. */
400fbf9f
JW
2421
2422tree
c2255bc4 2423build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2424{
6a3799eb 2425 tree ret;
a4ab7973 2426 bool swapped = false;
400fbf9f
JW
2427 if (TREE_TYPE (array) == error_mark_node
2428 || TREE_TYPE (index) == error_mark_node)
2429 return error_mark_node;
2430
b72271b9 2431 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2432 {
2433 size_t rank = 0;
2434 if (!find_rank (loc, index, index, true, &rank))
2435 return error_mark_node;
2436 if (rank > 1)
2437 {
2438 error_at (loc, "rank of the array's index is greater than 1");
2439 return error_mark_node;
2440 }
2441 }
a4ab7973 2442 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2443 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2444 /* Allow vector[index] but not index[vector]. */
2445 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2446 {
a4ab7973
JM
2447 tree temp;
2448 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2449 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2450 {
f90e8e2e 2451 error_at (loc,
30cd1c5d
AS
2452 "subscripted value is neither array nor pointer nor vector");
2453
fdeefd49
RS
2454 return error_mark_node;
2455 }
a4ab7973
JM
2456 temp = array;
2457 array = index;
2458 index = temp;
2459 swapped = true;
2460 }
2461
2462 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2463 {
a63068b6 2464 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2465 return error_mark_node;
2466 }
2467
2468 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2469 {
a63068b6 2470 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2471 return error_mark_node;
2472 }
2473
ff6b6641
GDR
2474 /* ??? Existing practice has been to warn only when the char
2475 index is syntactically the index, not for char[array]. */
2476 if (!swapped)
2477 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2478
2479 /* Apply default promotions *after* noticing character types. */
2480 index = default_conversion (index);
2481
2482 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2483
7edaa4d2 2484 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2485
2486 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2487 {
e4d35515 2488 tree rval, type;
fdeefd49 2489
400fbf9f
JW
2490 /* An array that is indexed by a non-constant
2491 cannot be stored in a register; we must be able to do
2492 address arithmetic on its address.
2493 Likewise an array of elements of variable size. */
2494 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2495 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2496 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2497 {
dffd7eb6 2498 if (!c_mark_addressable (array))
400fbf9f
JW
2499 return error_mark_node;
2500 }
e6d52559
JW
2501 /* An array that is indexed by a constant value which is not within
2502 the array bounds cannot be stored in a register either; because we
2503 would get a crash in store_bit_field/extract_bit_field when trying
2504 to access a non-existent part of the register. */
2505 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2506 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2507 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2508 {
dffd7eb6 2509 if (!c_mark_addressable (array))
e6d52559
JW
2510 return error_mark_node;
2511 }
400fbf9f 2512
f3bede71 2513 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2514 {
2515 tree foo = array;
2516 while (TREE_CODE (foo) == COMPONENT_REF)
2517 foo = TREE_OPERAND (foo, 0);
5baeaac0 2518 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2519 pedwarn (loc, OPT_Wpedantic,
fcf73884 2520 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2521 else if (!lvalue_p (foo))
2522 pedwarn_c90 (loc, OPT_Wpedantic,
2523 "ISO C90 forbids subscripting non-lvalue "
2524 "array");
400fbf9f
JW
2525 }
2526
46df2823 2527 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2528 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2529 /* Array ref is const/volatile if the array elements are
c22cacf3 2530 or if the array is. */
400fbf9f
JW
2531 TREE_READONLY (rval)
2532 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2533 | TREE_READONLY (array));
2534 TREE_SIDE_EFFECTS (rval)
2535 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2536 | TREE_SIDE_EFFECTS (array));
2537 TREE_THIS_VOLATILE (rval)
2538 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2539 /* This was added by rms on 16 Nov 91.
2f6e4e97 2540 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2541 in an inline function.
2542 Hope it doesn't break something else. */
2543 | TREE_THIS_VOLATILE (array));
928c19bb 2544 ret = require_complete_type (rval);
6a3799eb
AH
2545 protected_set_expr_location (ret, loc);
2546 return ret;
400fbf9f 2547 }
a4ab7973
JM
2548 else
2549 {
2550 tree ar = default_conversion (array);
400fbf9f 2551
a4ab7973
JM
2552 if (ar == error_mark_node)
2553 return ar;
400fbf9f 2554
a4ab7973
JM
2555 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2556 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2557
ba47d38d 2558 return build_indirect_ref
c9f9eb5d 2559 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2560 RO_ARRAY_INDEXING);
a4ab7973 2561 }
400fbf9f
JW
2562}
2563\f
7e585d16 2564/* Build an external reference to identifier ID. FUN indicates
766beb40 2565 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2566 location of the identifier. This sets *TYPE to the type of the
2567 identifier, which is not the same as the type of the returned value
2568 for CONST_DECLs defined as enum constants. If the type of the
2569 identifier is not available, *TYPE is set to NULL. */
7e585d16 2570tree
c2255bc4 2571build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2572{
2573 tree ref;
2574 tree decl = lookup_name (id);
16b34ad6
ZL
2575
2576 /* In Objective-C, an instance variable (ivar) may be preferred to
2577 whatever lookup_name() found. */
2578 decl = objc_lookup_ivar (decl, id);
7e585d16 2579
6866c6e8 2580 *type = NULL;
339a28b9 2581 if (decl && decl != error_mark_node)
6866c6e8
ILT
2582 {
2583 ref = decl;
2584 *type = TREE_TYPE (ref);
2585 }
339a28b9
ZW
2586 else if (fun)
2587 /* Implicit function declaration. */
c2255bc4 2588 ref = implicitly_declare (loc, id);
339a28b9
ZW
2589 else if (decl == error_mark_node)
2590 /* Don't complain about something that's already been
2591 complained about. */
2592 return error_mark_node;
2593 else
2594 {
c2255bc4 2595 undeclared_variable (loc, id);
339a28b9
ZW
2596 return error_mark_node;
2597 }
7e585d16
ZW
2598
2599 if (TREE_TYPE (ref) == error_mark_node)
2600 return error_mark_node;
2601
339a28b9 2602 if (TREE_DEPRECATED (ref))
9b86d6bb 2603 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2604
ad960f56 2605 /* Recursive call does not count as usage. */
b8698a0f 2606 if (ref != current_function_decl)
ad960f56 2607 {
ad960f56
MLI
2608 TREE_USED (ref) = 1;
2609 }
7e585d16 2610
bc4b653b
JM
2611 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2612 {
2613 if (!in_sizeof && !in_typeof)
2614 C_DECL_USED (ref) = 1;
2615 else if (DECL_INITIAL (ref) == 0
2616 && DECL_EXTERNAL (ref)
2617 && !TREE_PUBLIC (ref))
2618 record_maybe_used_decl (ref);
2619 }
2620
7e585d16
ZW
2621 if (TREE_CODE (ref) == CONST_DECL)
2622 {
6193b8b7 2623 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2624
2625 if (warn_cxx_compat
2626 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2627 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2628 {
2629 warning_at (loc, OPT_Wc___compat,
2630 ("enum constant defined in struct or union "
2631 "is not visible in C++"));
2632 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2633 }
2634
7e585d16
ZW
2635 ref = DECL_INITIAL (ref);
2636 TREE_CONSTANT (ref) = 1;
2637 }
6a29edea 2638 else if (current_function_decl != 0
4b1e44be 2639 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2640 && (TREE_CODE (ref) == VAR_DECL
2641 || TREE_CODE (ref) == PARM_DECL
2642 || TREE_CODE (ref) == FUNCTION_DECL))
2643 {
2644 tree context = decl_function_context (ref);
2f6e4e97 2645
6a29edea
EB
2646 if (context != 0 && context != current_function_decl)
2647 DECL_NONLOCAL (ref) = 1;
2648 }
71113fcd
GK
2649 /* C99 6.7.4p3: An inline definition of a function with external
2650 linkage ... shall not contain a reference to an identifier with
2651 internal linkage. */
2652 else if (current_function_decl != 0
2653 && DECL_DECLARED_INLINE_P (current_function_decl)
2654 && DECL_EXTERNAL (current_function_decl)
2655 && VAR_OR_FUNCTION_DECL_P (ref)
2656 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2657 && ! TREE_PUBLIC (ref)
2658 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2659 record_inline_static (loc, current_function_decl, ref,
2660 csi_internal);
7e585d16
ZW
2661
2662 return ref;
2663}
2664
bc4b653b
JM
2665/* Record details of decls possibly used inside sizeof or typeof. */
2666struct maybe_used_decl
2667{
2668 /* The decl. */
2669 tree decl;
2670 /* The level seen at (in_sizeof + in_typeof). */
2671 int level;
2672 /* The next one at this level or above, or NULL. */
2673 struct maybe_used_decl *next;
2674};
2675
2676static struct maybe_used_decl *maybe_used_decls;
2677
2678/* Record that DECL, an undefined static function reference seen
2679 inside sizeof or typeof, might be used if the operand of sizeof is
2680 a VLA type or the operand of typeof is a variably modified
2681 type. */
2682
4e2fb7de 2683static void
bc4b653b
JM
2684record_maybe_used_decl (tree decl)
2685{
2686 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2687 t->decl = decl;
2688 t->level = in_sizeof + in_typeof;
2689 t->next = maybe_used_decls;
2690 maybe_used_decls = t;
2691}
2692
2693/* Pop the stack of decls possibly used inside sizeof or typeof. If
2694 USED is false, just discard them. If it is true, mark them used
2695 (if no longer inside sizeof or typeof) or move them to the next
2696 level up (if still inside sizeof or typeof). */
2697
2698void
2699pop_maybe_used (bool used)
2700{
2701 struct maybe_used_decl *p = maybe_used_decls;
2702 int cur_level = in_sizeof + in_typeof;
2703 while (p && p->level > cur_level)
2704 {
2705 if (used)
2706 {
2707 if (cur_level == 0)
2708 C_DECL_USED (p->decl) = 1;
2709 else
2710 p->level = cur_level;
2711 }
2712 p = p->next;
2713 }
2714 if (!used || cur_level == 0)
2715 maybe_used_decls = p;
2716}
2717
2718/* Return the result of sizeof applied to EXPR. */
2719
2720struct c_expr
c2255bc4 2721c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2722{
2723 struct c_expr ret;
ad97f4be
JM
2724 if (expr.value == error_mark_node)
2725 {
2726 ret.value = error_mark_node;
2727 ret.original_code = ERROR_MARK;
6866c6e8 2728 ret.original_type = NULL;
ad97f4be
JM
2729 pop_maybe_used (false);
2730 }
2731 else
2732 {
928c19bb 2733 bool expr_const_operands = true;
773ec47f
MP
2734
2735 if (TREE_CODE (expr.value) == PARM_DECL
2736 && C_ARRAY_PARAMETER (expr.value))
2737 {
2738 if (warning_at (loc, OPT_Wsizeof_array_argument,
2739 "%<sizeof%> on array function parameter %qE will "
2740 "return size of %qT", expr.value,
2741 expr.original_type))
2742 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2743 }
928c19bb
JM
2744 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2745 &expr_const_operands);
c2255bc4 2746 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2747 c_last_sizeof_arg = expr.value;
2748 ret.original_code = SIZEOF_EXPR;
6866c6e8 2749 ret.original_type = NULL;
928c19bb 2750 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2751 {
2752 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2753 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2754 folded_expr, ret.value);
2755 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2756 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2757 }
928c19bb 2758 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2759 }
bc4b653b
JM
2760 return ret;
2761}
2762
2763/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2764 name passed to sizeof (rather than the type itself). LOC is the
2765 location of the original expression. */
bc4b653b
JM
2766
2767struct c_expr
c2255bc4 2768c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2769{
2770 tree type;
2771 struct c_expr ret;
928c19bb
JM
2772 tree type_expr = NULL_TREE;
2773 bool type_expr_const = true;
2774 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2775 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2776 c_last_sizeof_arg = type;
2777 ret.original_code = SIZEOF_EXPR;
6866c6e8 2778 ret.original_type = NULL;
24070fcb
JM
2779 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2780 && c_vla_type_p (type))
928c19bb 2781 {
24070fcb
JM
2782 /* If the type is a [*] array, it is a VLA but is represented as
2783 having a size of zero. In such a case we must ensure that
2784 the result of sizeof does not get folded to a constant by
2785 c_fully_fold, because if the size is evaluated the result is
2786 not constant and so constraints on zero or negative size
2787 arrays must not be applied when this sizeof call is inside
2788 another array declarator. */
2789 if (!type_expr)
2790 type_expr = integer_zero_node;
928c19bb
JM
2791 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2792 type_expr, ret.value);
2793 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2794 }
16464cc1
VR
2795 pop_maybe_used (type != error_mark_node
2796 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2797 return ret;
2798}
2799
400fbf9f 2800/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2801 The function call is at LOC.
400fbf9f
JW
2802 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2803 TREE_VALUE of each node is a parameter-expression.
2804 FUNCTION's data type may be a function type or a pointer-to-function. */
2805
2806tree
c2255bc4 2807build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2808{
9771b263 2809 vec<tree, va_gc> *v;
bbbbb16a
ILT
2810 tree ret;
2811
9771b263 2812 vec_alloc (v, list_length (params));
bbbbb16a 2813 for (; params; params = TREE_CHAIN (params))
9771b263 2814 v->quick_push (TREE_VALUE (params));
8edbfaa6 2815 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2816 vec_free (v);
bbbbb16a
ILT
2817 return ret;
2818}
2819
ae52741c
MLI
2820/* Give a note about the location of the declaration of DECL. */
2821
2822static void inform_declaration (tree decl)
2823{
2824 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2825 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2826}
2827
bbbbb16a
ILT
2828/* Build a function call to function FUNCTION with parameters PARAMS.
2829 ORIGTYPES, if not NULL, is a vector of types; each element is
2830 either NULL or the original type of the corresponding element in
2831 PARAMS. The original type may differ from TREE_TYPE of the
2832 parameter for enums. FUNCTION's data type may be a function type
2833 or pointer-to-function. This function changes the elements of
2834 PARAMS. */
2835
2836tree
81e5eca8
MP
2837build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2838 tree function, vec<tree, va_gc> *params,
9771b263 2839 vec<tree, va_gc> *origtypes)
400fbf9f 2840{
b3694847 2841 tree fntype, fundecl = 0;
4977bab6 2842 tree name = NULL_TREE, result;
c96f4f73 2843 tree tem;
94a0dd7b
SL
2844 int nargs;
2845 tree *argarray;
b8698a0f 2846
400fbf9f 2847
fc76e425 2848 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2849 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2850
2851 /* Convert anything with function type to a pointer-to-function. */
2852 if (TREE_CODE (function) == FUNCTION_DECL)
2853 {
2854 name = DECL_NAME (function);
0a35513e
AH
2855
2856 if (flag_tm)
2857 tm_malloc_replacement (function);
a5eadacc 2858 fundecl = function;
86951993
AM
2859 /* Atomic functions have type checking/casting already done. They are
2860 often rewritten and don't match the original parameter list. */
2861 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2862 origtypes = NULL;
36536d79 2863
b72271b9 2864 if (flag_cilkplus
36536d79
BI
2865 && is_cilkplus_reduce_builtin (function))
2866 origtypes = NULL;
400fbf9f 2867 }
f2a71bbc 2868 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2869 function = function_to_pointer_conversion (loc, function);
400fbf9f 2870
6e955430
ZL
2871 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2872 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2873 if (params && !params->is_empty ())
2874 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2875
928c19bb
JM
2876 function = c_fully_fold (function, false, NULL);
2877
400fbf9f
JW
2878 fntype = TREE_TYPE (function);
2879
2880 if (TREE_CODE (fntype) == ERROR_MARK)
2881 return error_mark_node;
2882
2883 if (!(TREE_CODE (fntype) == POINTER_TYPE
2884 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2885 {
ae52741c
MLI
2886 if (!flag_diagnostics_show_caret)
2887 error_at (loc,
2888 "called object %qE is not a function or function pointer",
2889 function);
2890 else if (DECL_P (function))
2891 {
2892 error_at (loc,
2893 "called object %qD is not a function or function pointer",
2894 function);
2895 inform_declaration (function);
2896 }
2897 else
2898 error_at (loc,
2899 "called object is not a function or function pointer");
400fbf9f
JW
2900 return error_mark_node;
2901 }
2902
5ce89b2e
JM
2903 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2904 current_function_returns_abnormally = 1;
2905
400fbf9f
JW
2906 /* fntype now gets the type of function pointed to. */
2907 fntype = TREE_TYPE (fntype);
2908
ab4194da
JM
2909 /* Convert the parameters to the types declared in the
2910 function prototype, or apply default promotions. */
2911
81e5eca8
MP
2912 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2913 origtypes, function, fundecl);
ab4194da
JM
2914 if (nargs < 0)
2915 return error_mark_node;
2916
c96f4f73 2917 /* Check that the function is called through a compatible prototype.
fa337f3a 2918 If it is not, warn. */
1043771b 2919 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2920 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2921 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2922 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2923 {
2924 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2925
2926 /* This situation leads to run-time undefined behavior. We can't,
2927 therefore, simply error unless we can prove that all possible
2928 executions of the program must execute the code. */
fa337f3a 2929 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2930
fa337f3a
RB
2931 if (VOID_TYPE_P (return_type)
2932 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2933 pedwarn (loc, 0,
2934 "function with qualified void return type called");
2935 }
c96f4f73 2936
9771b263 2937 argarray = vec_safe_address (params);
bbbbb16a 2938
83322951
RG
2939 /* Check that arguments to builtin functions match the expectations. */
2940 if (fundecl
2941 && DECL_BUILT_IN (fundecl)
2942 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2943 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2944 return error_mark_node;
400fbf9f 2945
83322951 2946 /* Check that the arguments to the function are valid. */
dde05067 2947 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2948
928c19bb
JM
2949 if (name != NULL_TREE
2950 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2951 {
928c19bb 2952 if (require_constant_value)
b8698a0f 2953 result =
db3927fb
AH
2954 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2955 function, nargs, argarray);
928c19bb 2956 else
db3927fb
AH
2957 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2958 function, nargs, argarray);
928c19bb
JM
2959 if (TREE_CODE (result) == NOP_EXPR
2960 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2961 STRIP_TYPE_NOPS (result);
bf730f15
RS
2962 }
2963 else
db3927fb
AH
2964 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2965 function, nargs, argarray);
b0b3afb2 2966
71653180 2967 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2968 {
2969 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2970 pedwarn (loc, 0,
3ce62965
JM
2971 "function with qualified void return type called");
2972 return result;
2973 }
1eb8759b 2974 return require_complete_type (result);
400fbf9f 2975}
8edbfaa6
JJ
2976
2977/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2978
2979tree
2980c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2981 tree function, vec<tree, va_gc> *params,
2982 vec<tree, va_gc> *origtypes)
2983{
2984 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2985 STRIP_TYPE_NOPS (function);
2986
2987 /* Convert anything with function type to a pointer-to-function. */
2988 if (TREE_CODE (function) == FUNCTION_DECL)
2989 {
2990 /* Implement type-directed function overloading for builtins.
2991 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2992 handle all the type checking. The result is a complete expression
2993 that implements this function call. */
2994 tree tem = resolve_overloaded_builtin (loc, function, params);
2995 if (tem)
2996 return tem;
2997 }
2998 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
2999}
400fbf9f 3000\f
bbbbb16a
ILT
3001/* Convert the argument expressions in the vector VALUES
3002 to the types in the list TYPELIST.
400fbf9f
JW
3003
3004 If TYPELIST is exhausted, or when an element has NULL as its type,
3005 perform the default conversions.
3006
bbbbb16a
ILT
3007 ORIGTYPES is the original types of the expressions in VALUES. This
3008 holds the type of enum values which have been converted to integral
3009 types. It may be NULL.
400fbf9f 3010
03dafa61
JM
3011 FUNCTION is a tree for the called function. It is used only for
3012 error messages, where it is formatted with %qE.
400fbf9f
JW
3013
3014 This is also where warnings about wrong number of args are generated.
3015
81e5eca8
MP
3016 ARG_LOC are locations of function arguments (if any).
3017
94a0dd7b 3018 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3019 than the length of VALUES in some error situations), or -1 on
3020 failure. */
94a0dd7b
SL
3021
3022static int
81e5eca8
MP
3023convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3024 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3025 tree function, tree fundecl)
400fbf9f 3026{
bbbbb16a
ILT
3027 tree typetail, val;
3028 unsigned int parmnum;
06302a02 3029 bool error_args = false;
b5d32c25 3030 const bool type_generic = fundecl
81e5eca8 3031 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3032 bool type_generic_remove_excess_precision = false;
03dafa61 3033 tree selector;
03dafa61 3034
2ac2f164
JM
3035 /* Change pointer to function to the function itself for
3036 diagnostics. */
03dafa61
JM
3037 if (TREE_CODE (function) == ADDR_EXPR
3038 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3039 function = TREE_OPERAND (function, 0);
03dafa61
JM
3040
3041 /* Handle an ObjC selector specially for diagnostics. */
3042 selector = objc_message_selector ();
400fbf9f 3043
8ce94e44
JM
3044 /* For type-generic built-in functions, determine whether excess
3045 precision should be removed (classification) or not
3046 (comparison). */
3047 if (type_generic
3048 && DECL_BUILT_IN (fundecl)
3049 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3050 {
3051 switch (DECL_FUNCTION_CODE (fundecl))
3052 {
3053 case BUILT_IN_ISFINITE:
3054 case BUILT_IN_ISINF:
3055 case BUILT_IN_ISINF_SIGN:
3056 case BUILT_IN_ISNAN:
3057 case BUILT_IN_ISNORMAL:
3058 case BUILT_IN_FPCLASSIFY:
3059 type_generic_remove_excess_precision = true;
3060 break;
3061
3062 default:
3063 type_generic_remove_excess_precision = false;
3064 break;
3065 }
3066 }
b72271b9 3067 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3068 return vec_safe_length (values);
8ce94e44 3069
400fbf9f 3070 /* Scan the given expressions and types, producing individual
bbbbb16a 3071 converted arguments. */
400fbf9f 3072
bbbbb16a 3073 for (typetail = typelist, parmnum = 0;
9771b263 3074 values && values->iterate (parmnum, &val);
bbbbb16a 3075 ++parmnum)
400fbf9f 3076 {
b3694847 3077 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3078 tree valtype = TREE_TYPE (val);
03dafa61
JM
3079 tree rname = function;
3080 int argnum = parmnum + 1;
4d3e6fae 3081 const char *invalid_func_diag;
8ce94e44 3082 bool excess_precision = false;
928c19bb 3083 bool npc;
bbbbb16a 3084 tree parmval;
5c1bc275
MP
3085 /* Some __atomic_* builtins have additional hidden argument at
3086 position 0. */
3087 location_t ploc
3088 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3089 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3090 : input_location;
400fbf9f
JW
3091
3092 if (type == void_type_node)
3093 {
19dc6d01 3094 if (selector)
68fca595 3095 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3096 else
68fca595 3097 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3098 inform_declaration (fundecl);
94a0dd7b 3099 return parmnum;
400fbf9f
JW
3100 }
3101
03dafa61
JM
3102 if (selector && argnum > 2)
3103 {
3104 rname = selector;
3105 argnum -= 2;
3106 }
3107
928c19bb 3108 npc = null_pointer_constant_p (val);
8ce94e44
JM
3109
3110 /* If there is excess precision and a prototype, convert once to
3111 the required type rather than converting via the semantic
3112 type. Likewise without a prototype a float value represented
3113 as long double should be converted once to double. But for
3114 type-generic classification functions excess precision must
3115 be removed here. */
3116 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3117 && (type || !type_generic || !type_generic_remove_excess_precision))
3118 {
3119 val = TREE_OPERAND (val, 0);
3120 excess_precision = true;
3121 }
928c19bb 3122 val = c_fully_fold (val, false, NULL);
ed248cf7 3123 STRIP_TYPE_NOPS (val);
400fbf9f 3124
400fbf9f
JW
3125 val = require_complete_type (val);
3126
3127 if (type != 0)
3128 {
3129 /* Formal parm type is specified by a function prototype. */
400fbf9f 3130
20913689 3131 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3132 {
5c1bc275
MP
3133 error_at (ploc, "type of formal parameter %d is incomplete",
3134 parmnum + 1);
400fbf9f
JW
3135 parmval = val;
3136 }
3137 else
3138 {
bbbbb16a
ILT
3139 tree origtype;
3140
d45cf215
RS
3141 /* Optionally warn about conversions that
3142 differ from the default conversions. */
05170031 3143 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3144 {
e3a64162 3145 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3146
aae43c5f 3147 if (INTEGRAL_TYPE_P (type)
8ce94e44 3148 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3149 warning_at (ploc, OPT_Wtraditional_conversion,
3150 "passing argument %d of %qE as integer rather "
3151 "than floating due to prototype",
3152 argnum, rname);
03829ad2 3153 if (INTEGRAL_TYPE_P (type)
8ce94e44 3154 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3155 warning_at (ploc, OPT_Wtraditional_conversion,
3156 "passing argument %d of %qE as integer rather "
3157 "than complex due to prototype",
3158 argnum, rname);
aae43c5f 3159 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3160 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3161 warning_at (ploc, OPT_Wtraditional_conversion,
3162 "passing argument %d of %qE as complex rather "
3163 "than floating due to prototype",
3164 argnum, rname);
400fbf9f 3165 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3166 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3167 warning_at (ploc, OPT_Wtraditional_conversion,
3168 "passing argument %d of %qE as floating rather "
3169 "than integer due to prototype",
3170 argnum, rname);
03829ad2 3171 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3172 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3173 warning_at (ploc, OPT_Wtraditional_conversion,
3174 "passing argument %d of %qE as complex rather "
3175 "than integer due to prototype",
3176 argnum, rname);
aae43c5f 3177 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3178 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3179 warning_at (ploc, OPT_Wtraditional_conversion,
3180 "passing argument %d of %qE as floating rather "
3181 "than complex due to prototype",
3182 argnum, rname);
aae43c5f
RK
3183 /* ??? At some point, messages should be written about
3184 conversions between complex types, but that's too messy
3185 to do now. */
d45cf215 3186 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3187 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3188 {
3189 /* Warn if any argument is passed as `float',
047de90b 3190 since without a prototype it would be `double'. */
9a8ce21f
JG
3191 if (formal_prec == TYPE_PRECISION (float_type_node)
3192 && type != dfloat32_type_node)
5c1bc275
MP
3193 warning_at (ploc, 0,
3194 "passing argument %d of %qE as %<float%> "
3195 "rather than %<double%> due to prototype",
3196 argnum, rname);
9a8ce21f
JG
3197
3198 /* Warn if mismatch between argument and prototype
3199 for decimal float types. Warn of conversions with
3200 binary float types and of precision narrowing due to
3201 prototype. */
8ce94e44 3202 else if (type != valtype
9a8ce21f
JG
3203 && (type == dfloat32_type_node
3204 || type == dfloat64_type_node
c22cacf3 3205 || type == dfloat128_type_node
8ce94e44
JM
3206 || valtype == dfloat32_type_node
3207 || valtype == dfloat64_type_node
3208 || valtype == dfloat128_type_node)
c22cacf3 3209 && (formal_prec
8ce94e44 3210 <= TYPE_PRECISION (valtype)
9a8ce21f 3211 || (type == dfloat128_type_node
8ce94e44 3212 && (valtype
c22cacf3 3213 != dfloat64_type_node
8ce94e44 3214 && (valtype
9a8ce21f
JG
3215 != dfloat32_type_node)))
3216 || (type == dfloat64_type_node
8ce94e44 3217 && (valtype
9a8ce21f 3218 != dfloat32_type_node))))
5c1bc275
MP
3219 warning_at (ploc, 0,
3220 "passing argument %d of %qE as %qT "
3221 "rather than %qT due to prototype",
3222 argnum, rname, type, valtype);
9a8ce21f 3223
d45cf215 3224 }
3ed56f8a
KG
3225 /* Detect integer changing in width or signedness.
3226 These warnings are only activated with
05170031
MLI
3227 -Wtraditional-conversion, not with -Wtraditional. */
3228 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3229 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3230 {
d45cf215
RS
3231 tree would_have_been = default_conversion (val);
3232 tree type1 = TREE_TYPE (would_have_been);
3233
754a4d82 3234 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3235 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3236 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3237 /* No warning if function asks for enum
3238 and the actual arg is that enum type. */
3239 ;
3240 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3241 warning_at (ploc, OPT_Wtraditional_conversion,
3242 "passing argument %d of %qE "
3243 "with different width due to prototype",
3244 argnum, rname);
8df83eae 3245 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3246 ;
800cd3b9
RS
3247 /* Don't complain if the formal parameter type
3248 is an enum, because we can't tell now whether
3249 the value was an enum--even the same enum. */
3250 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3251 ;
400fbf9f
JW
3252 else if (TREE_CODE (val) == INTEGER_CST
3253 && int_fits_type_p (val, type))
3254 /* Change in signedness doesn't matter
3255 if a constant value is unaffected. */
3256 ;
ce9895ae
RS
3257 /* If the value is extended from a narrower
3258 unsigned type, it doesn't matter whether we
3259 pass it as signed or unsigned; the value
3260 certainly is the same either way. */
8ce94e44
JM
3261 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3262 && TYPE_UNSIGNED (valtype))
ce9895ae 3263 ;
8df83eae 3264 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3265 warning_at (ploc, OPT_Wtraditional_conversion,
3266 "passing argument %d of %qE "
3267 "as unsigned due to prototype",
3268 argnum, rname);
3ed56f8a 3269 else
5c1bc275
MP
3270 warning_at (ploc, OPT_Wtraditional_conversion,
3271 "passing argument %d of %qE "
3272 "as signed due to prototype",
3273 argnum, rname);
400fbf9f
JW
3274 }
3275 }
3276
8ce94e44
JM
3277 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3278 sake of better warnings from convert_and_check. */
3279 if (excess_precision)
3280 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3281 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3282 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3283 val, origtype, ic_argpass,
3284 npc, fundecl, function,
2ac2f164 3285 parmnum + 1);
2f6e4e97 3286
61f71b34 3287 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3288 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3289 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3290 parmval = default_conversion (parmval);
400fbf9f 3291 }
400fbf9f 3292 }
8ce94e44
JM
3293 else if (TREE_CODE (valtype) == REAL_TYPE
3294 && (TYPE_PRECISION (valtype)
2531a1d9 3295 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3296 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3297 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3298 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3299 {
3300 if (type_generic)
bbbbb16a 3301 parmval = val;
b5d32c25 3302 else
0a0b3574
MM
3303 {
3304 /* Convert `float' to `double'. */
3305 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3306 warning_at (ploc, OPT_Wdouble_promotion,
3307 "implicit conversion from %qT to %qT when passing "
3308 "argument to function",
3309 valtype, double_type_node);
0a0b3574
MM
3310 parmval = convert (double_type_node, val);
3311 }
b5d32c25 3312 }
8ce94e44
JM
3313 else if (excess_precision && !type_generic)
3314 /* A "double" argument with excess precision being passed
3315 without a prototype or in variable arguments. */
bbbbb16a 3316 parmval = convert (valtype, val);
c22cacf3
MS
3317 else if ((invalid_func_diag =
3318 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3319 {
3320 error (invalid_func_diag);
94a0dd7b 3321 return -1;
4d3e6fae 3322 }
400fbf9f
JW
3323 else
3324 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3325 parmval = default_conversion (val);
3326
9771b263 3327 (*values)[parmnum] = parmval;
06302a02
JJ
3328 if (parmval == error_mark_node)
3329 error_args = true;
400fbf9f
JW
3330
3331 if (typetail)
3332 typetail = TREE_CHAIN (typetail);
3333 }
3334
9771b263 3335 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3336
400fbf9f 3337 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3338 {
68fca595 3339 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3340 inform_declaration (fundecl);
3341 return -1;
3789b316 3342 }
400fbf9f 3343
06302a02 3344 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3345}
3346\f
43f6dfd3
RS
3347/* This is the entry point used by the parser to build unary operators
3348 in the input. CODE, a tree_code, specifies the unary operator, and
3349 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3350 CONVERT_EXPR for code.
3351
3352 LOC is the location to use for the tree generated.
3353*/
43f6dfd3
RS
3354
3355struct c_expr
c2255bc4 3356parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3357{
3358 struct c_expr result;
3359
c9f9eb5d 3360 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3361 result.original_code = code;
6866c6e8
ILT
3362 result.original_type = NULL;
3363
59c0753d 3364 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3365 overflow_warning (loc, result.value);
59c0753d 3366
43f6dfd3
RS
3367 return result;
3368}
3369
3370/* This is the entry point used by the parser to build binary operators
3371 in the input. CODE, a tree_code, specifies the binary operator, and
3372 ARG1 and ARG2 are the operands. In addition to constructing the
3373 expression, we check for operands that were written with other binary
ba47d38d
AH
3374 operators in a way that is likely to confuse the user.
3375
3376 LOCATION is the location of the binary operator. */
edc7c4ec 3377
487a92fe 3378struct c_expr
ba47d38d
AH
3379parser_build_binary_op (location_t location, enum tree_code code,
3380 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3381{
487a92fe 3382 struct c_expr result;
400fbf9f 3383
487a92fe
JM
3384 enum tree_code code1 = arg1.original_code;
3385 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3386 tree type1 = (arg1.original_type
3387 ? arg1.original_type
3388 : TREE_TYPE (arg1.value));
3389 tree type2 = (arg2.original_type
3390 ? arg2.original_type
3391 : TREE_TYPE (arg2.value));
400fbf9f 3392
ba47d38d
AH
3393 result.value = build_binary_op (location, code,
3394 arg1.value, arg2.value, 1);
487a92fe 3395 result.original_code = code;
6866c6e8 3396 result.original_type = NULL;
58bf601b 3397
487a92fe
JM
3398 if (TREE_CODE (result.value) == ERROR_MARK)
3399 return result;
400fbf9f 3400
ba47d38d
AH
3401 if (location != UNKNOWN_LOCATION)
3402 protected_set_expr_location (result.value, location);
3403
400fbf9f 3404 /* Check for cases such as x+y<<z which users are likely
487a92fe 3405 to misinterpret. */
400fbf9f 3406 if (warn_parentheses)
393e8e8b
MP
3407 warn_about_parentheses (location, code, code1, arg1.value, code2,
3408 arg2.value);
001af587 3409
ca409efd 3410 if (warn_logical_op)
393e8e8b 3411 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3412 code1, arg1.value, code2, arg2.value);
63a08740 3413
742938c9 3414 if (warn_logical_not_paren
47c2554f
MP
3415 && code1 == TRUTH_NOT_EXPR
3416 && code2 != TRUTH_NOT_EXPR)
59ea0364 3417 warn_logical_not_parentheses (location, code, arg2.value);
742938c9 3418
e994a705
RS
3419 /* Warn about comparisons against string literals, with the exception
3420 of testing for equality or inequality of a string literal with NULL. */
3421 if (code == EQ_EXPR || code == NE_EXPR)
3422 {
3423 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3424 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3425 warning_at (location, OPT_Waddress,
3426 "comparison with string literal results in unspecified behavior");
e994a705
RS
3427 }
3428 else if (TREE_CODE_CLASS (code) == tcc_comparison
3429 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3430 warning_at (location, OPT_Waddress,
3431 "comparison with string literal results in unspecified behavior");
e994a705 3432
b8698a0f
L
3433 if (TREE_OVERFLOW_P (result.value)
3434 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3435 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3436 overflow_warning (location, result.value);
400fbf9f 3437
6866c6e8
ILT
3438 /* Warn about comparisons of different enum types. */
3439 if (warn_enum_compare
3440 && TREE_CODE_CLASS (code) == tcc_comparison
3441 && TREE_CODE (type1) == ENUMERAL_TYPE
3442 && TREE_CODE (type2) == ENUMERAL_TYPE
3443 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3444 warning_at (location, OPT_Wenum_compare,
3445 "comparison between %qT and %qT",
3446 type1, type2);
3447
400fbf9f
JW
3448 return result;
3449}
3e4093b6 3450\f
3e4093b6
RS
3451/* Return a tree for the difference of pointers OP0 and OP1.
3452 The resulting tree has type int. */
293c9fdd 3453
3e4093b6 3454static tree
db3927fb 3455pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3456{
3e4093b6 3457 tree restype = ptrdiff_type_node;
36c5e70a 3458 tree result, inttype;
400fbf9f 3459
36c5e70a
BE
3460 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3461 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3462 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3463 tree orig_op1 = op1;
400fbf9f 3464
36c5e70a
BE
3465 /* If the operands point into different address spaces, we need to
3466 explicitly convert them to pointers into the common address space
3467 before we can subtract the numerical address values. */
3468 if (as0 != as1)
3469 {
3470 addr_space_t as_common;
3471 tree common_type;
3472
3473 /* Determine the common superset address space. This is guaranteed
3474 to exist because the caller verified that comp_target_types
3475 returned non-zero. */
3476 if (!addr_space_superset (as0, as1, &as_common))
3477 gcc_unreachable ();
3478
3479 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3480 op0 = convert (common_type, op0);
3481 op1 = convert (common_type, op1);
3482 }
3483
3484 /* Determine integer type to perform computations in. This will usually
3485 be the same as the result type (ptrdiff_t), but may need to be a wider
3486 type if pointers for the address space are wider than ptrdiff_t. */
3487 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3488 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3489 else
3490 inttype = restype;
3491
fcf73884 3492 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3493 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3494 "pointer of type %<void *%> used in subtraction");
3495 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3496 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3497 "pointer to a function used in subtraction");
400fbf9f 3498
3e4093b6
RS
3499 /* First do the subtraction as integers;
3500 then drop through to build the divide operator.
3501 Do not do default conversions on the minus operator
3502 in case restype is a short type. */
400fbf9f 3503
db3927fb 3504 op0 = build_binary_op (loc,
36c5e70a
BE
3505 MINUS_EXPR, convert (inttype, op0),
3506 convert (inttype, op1), 0);
3e4093b6
RS
3507 /* This generates an error if op1 is pointer to incomplete type. */
3508 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3509 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3510
3e4093b6 3511 op1 = c_size_in_bytes (target_type);
400fbf9f 3512
f04dda30
MP
3513 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3514 error_at (loc, "arithmetic on pointer to an empty aggregate");
3515
3e4093b6 3516 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3517 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3518 op0, convert (inttype, op1));
3519
3520 /* Convert to final result type if necessary. */
3521 return convert (restype, result);
3e4093b6
RS
3522}
3523\f
267bac10
JM
3524/* Expand atomic compound assignments into an approriate sequence as
3525 specified by the C11 standard section 6.5.16.2.
3526 given
3527 _Atomic T1 E1
3528 T2 E2
3529 E1 op= E2
3530
3531 This sequence is used for all types for which these operations are
3532 supported.
3533
3534 In addition, built-in versions of the 'fe' prefixed routines may
3535 need to be invoked for floating point (real, complex or vector) when
3536 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3537
3538 T1 newval;
3539 T1 old;
3540 T1 *addr
3541 T2 val
3542 fenv_t fenv
3543
3544 addr = &E1;
3545 val = (E2);
3546 __atomic_load (addr, &old, SEQ_CST);
3547 feholdexcept (&fenv);
3548loop:
3549 newval = old op val;
3550 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3551 SEQ_CST))
3552 goto done;
3553 feclearexcept (FE_ALL_EXCEPT);
3554 goto loop:
3555done:
3556 feupdateenv (&fenv);
3557
3558 Also note that the compiler is simply issuing the generic form of
3559 the atomic operations. This requires temp(s) and has their address
3560 taken. The atomic processing is smart enough to figure out when the
3561 size of an object can utilize a lock-free version, and convert the
3562 built-in call to the appropriate lock-free routine. The optimizers
3563 will then dispose of any temps that are no longer required, and
3564 lock-free implementations are utilized as long as there is target
3565 support for the required size.
3566
3567 If the operator is NOP_EXPR, then this is a simple assignment, and
3568 an __atomic_store is issued to perform the assignment rather than
3569 the above loop.
3570
3571*/
3572
3573/* Build an atomic assignment at LOC, expanding into the proper
3574 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3575 the result of the operation, unless RETURN_OLD_P in which case
3576 return the old value of LHS (this is only for postincrement and
3577 postdecrement). */
3578static tree
3579build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3580 tree rhs, bool return_old_p)
3581{
3582 tree fndecl, func_call;
3583 vec<tree, va_gc> *params;
3584 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3585 tree old, old_addr;
3586 tree compound_stmt;
3587 tree stmt, goto_stmt;
3588 tree loop_label, loop_decl, done_label, done_decl;
3589
3590 tree lhs_type = TREE_TYPE (lhs);
3591 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3592 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3593 tree rhs_type = TREE_TYPE (rhs);
3594
3595 gcc_assert (TYPE_ATOMIC (lhs_type));
3596
3597 if (return_old_p)
3598 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3599
3600 /* Allocate enough vector items for a compare_exchange. */
3601 vec_alloc (params, 6);
3602
3603 /* Create a compound statement to hold the sequence of statements
3604 with a loop. */
3605 compound_stmt = c_begin_compound_stmt (false);
3606
3607 /* Fold the RHS if it hasn't already been folded. */
3608 if (modifycode != NOP_EXPR)
3609 rhs = c_fully_fold (rhs, false, NULL);
3610
3611 /* Remove the qualifiers for the rest of the expressions and create
3612 the VAL temp variable to hold the RHS. */
3613 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3614 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3615 val = create_tmp_var (nonatomic_rhs_type, NULL);
3616 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3617 TREE_NO_WARNING (val) = 1;
267bac10
JM
3618 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3619 SET_EXPR_LOCATION (rhs, loc);
3620 add_stmt (rhs);
3621
3622 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3623 an atomic_store. */
3624 if (modifycode == NOP_EXPR)
3625 {
3626 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3627 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3628 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3629 params->quick_push (lhs_addr);
3630 params->quick_push (rhs);
3631 params->quick_push (seq_cst);
8edbfaa6 3632 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3633 add_stmt (func_call);
3634
3635 /* Finish the compound statement. */
3636 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3637
3638 /* VAL is the value which was stored, return a COMPOUND_STMT of
3639 the statement and that value. */
3640 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3641 }
3642
3643 /* Create the variables and labels required for the op= form. */
3644 old = create_tmp_var (nonatomic_lhs_type, NULL);
3645 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3646 TREE_ADDRESSABLE (old) = 1;
3647 TREE_NO_WARNING (old) = 1;
267bac10
JM
3648
3649 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3650 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3651 TREE_ADDRESSABLE (newval) = 1;
3652
3653 loop_decl = create_artificial_label (loc);
3654 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3655
3656 done_decl = create_artificial_label (loc);
3657 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3658
3659 /* __atomic_load (addr, &old, SEQ_CST). */
3660 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3661 params->quick_push (lhs_addr);
3662 params->quick_push (old_addr);
3663 params->quick_push (seq_cst);
8edbfaa6 3664 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3665 add_stmt (func_call);
3666 params->truncate (0);
3667
3668 /* Create the expressions for floating-point environment
3669 manipulation, if required. */
3670 bool need_fenv = (flag_trapping_math
3671 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3672 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3673 if (need_fenv)
3674 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3675
3676 if (hold_call)
3677 add_stmt (hold_call);
3678
3679 /* loop: */
3680 add_stmt (loop_label);
3681
3682 /* newval = old + val; */
3683 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3684 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3685 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3686 NULL_TREE, 0);
3687 if (rhs != error_mark_node)
3688 {
3689 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3690 SET_EXPR_LOCATION (rhs, loc);
3691 add_stmt (rhs);
3692 }
3693
3694 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3695 goto done; */
3696 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3697 params->quick_push (lhs_addr);
3698 params->quick_push (old_addr);
3699 params->quick_push (newval_addr);
3700 params->quick_push (integer_zero_node);
3701 params->quick_push (seq_cst);
3702 params->quick_push (seq_cst);
8edbfaa6 3703 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3704
3705 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3706 SET_EXPR_LOCATION (goto_stmt, loc);
3707
3708 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3709 SET_EXPR_LOCATION (stmt, loc);
3710 add_stmt (stmt);
3711
3712 if (clear_call)
3713 add_stmt (clear_call);
3714
3715 /* goto loop; */
3716 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3717 SET_EXPR_LOCATION (goto_stmt, loc);
3718 add_stmt (goto_stmt);
3719
3720 /* done: */
3721 add_stmt (done_label);
3722
3723 if (update_call)
3724 add_stmt (update_call);
3725
3726 /* Finish the compound statement. */
3727 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3728
3729 /* NEWVAL is the value that was successfully stored, return a
3730 COMPOUND_EXPR of the statement and the appropriate value. */
3731 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3732 return_old_p ? old : newval);
3733}
3734
3e4093b6
RS
3735/* Construct and perhaps optimize a tree representation
3736 for a unary operation. CODE, a tree_code, specifies the operation
3737 and XARG is the operand.
3738 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3739 the default promotions (such as from short to int).
3740 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3741 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3742 arrays to pointers in C99.
3743
3744 LOCATION is the location of the operator. */
400fbf9f 3745
3e4093b6 3746tree
c9f9eb5d
AH
3747build_unary_op (location_t location,
3748 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3749{
3750 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3751 tree arg = xarg;
3752 tree argtype = 0;
808d6eaa 3753 enum tree_code typecode;
3e4093b6 3754 tree val;
c9f9eb5d 3755 tree ret = error_mark_node;
8ce94e44 3756 tree eptype = NULL_TREE;
3e4093b6 3757 int noconvert = flag;
4de67c26 3758 const char *invalid_op_diag;
928c19bb
JM
3759 bool int_operands;
3760
3761 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3762 if (int_operands)
3763 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3764
808d6eaa
JM
3765 if (code != ADDR_EXPR)
3766 arg = require_complete_type (arg);
3767
3768 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3769 if (typecode == ERROR_MARK)
3770 return error_mark_node;
3771 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3772 typecode = INTEGER_TYPE;
6c36d76b 3773
4de67c26
JM
3774 if ((invalid_op_diag
3775 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3776 {
c9f9eb5d 3777 error_at (location, invalid_op_diag);
4de67c26
JM
3778 return error_mark_node;
3779 }
3780
8ce94e44
JM
3781 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3782 {
3783 eptype = TREE_TYPE (arg);
3784 arg = TREE_OPERAND (arg, 0);
3785 }
3786
3e4093b6
RS
3787 switch (code)
3788 {
3789 case CONVERT_EXPR:
3790 /* This is used for unary plus, because a CONVERT_EXPR
3791 is enough to prevent anybody from looking inside for
3792 associativity, but won't generate any code. */
3793 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3794 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3795 || typecode == VECTOR_TYPE))
400fbf9f 3796 {
c9f9eb5d 3797 error_at (location, "wrong type argument to unary plus");
3e4093b6 3798 return error_mark_node;
400fbf9f 3799 }
3e4093b6
RS
3800 else if (!noconvert)
3801 arg = default_conversion (arg);
db3927fb 3802 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3803 break;
3804
3e4093b6
RS
3805 case NEGATE_EXPR:
3806 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3807 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3808 || typecode == VECTOR_TYPE))
3809 {
c9f9eb5d 3810 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3811 return error_mark_node;
3812 }
3813 else if (!noconvert)
3814 arg = default_conversion (arg);
400fbf9f
JW
3815 break;
3816
3e4093b6 3817 case BIT_NOT_EXPR:
462643f0
AP
3818 /* ~ works on integer types and non float vectors. */
3819 if (typecode == INTEGER_TYPE
3820 || (typecode == VECTOR_TYPE
3821 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3822 {
3e4093b6
RS
3823 if (!noconvert)
3824 arg = default_conversion (arg);
03d5b1f5 3825 }
3e4093b6 3826 else if (typecode == COMPLEX_TYPE)
400fbf9f 3827 {
3e4093b6 3828 code = CONJ_EXPR;
c1771a20 3829 pedwarn (location, OPT_Wpedantic,
fcf73884 3830 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3831 if (!noconvert)
3832 arg = default_conversion (arg);
3833 }
3834 else
3835 {
c9f9eb5d 3836 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3837 return error_mark_node;
400fbf9f
JW
3838 }
3839 break;
3840
3e4093b6 3841 case ABS_EXPR:
11017cc7 3842 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3843 {
c9f9eb5d 3844 error_at (location, "wrong type argument to abs");
3e4093b6 3845 return error_mark_node;
400fbf9f 3846 }
3e4093b6
RS
3847 else if (!noconvert)
3848 arg = default_conversion (arg);
400fbf9f
JW
3849 break;
3850
3e4093b6
RS
3851 case CONJ_EXPR:
3852 /* Conjugating a real value is a no-op, but allow it anyway. */
3853 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3854 || typecode == COMPLEX_TYPE))
400fbf9f 3855 {
c9f9eb5d 3856 error_at (location, "wrong type argument to conjugation");
3e4093b6 3857 return error_mark_node;
400fbf9f 3858 }
3e4093b6
RS
3859 else if (!noconvert)
3860 arg = default_conversion (arg);
400fbf9f
JW
3861 break;
3862
3e4093b6 3863 case TRUTH_NOT_EXPR:
ab22c1fa 3864 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3865 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3866 && typecode != COMPLEX_TYPE)
400fbf9f 3867 {
c9f9eb5d
AH
3868 error_at (location,
3869 "wrong type argument to unary exclamation mark");
3e4093b6 3870 return error_mark_node;
400fbf9f 3871 }
a27d595d
JM
3872 if (int_operands)
3873 {
3874 arg = c_objc_common_truthvalue_conversion (location, xarg);
3875 arg = remove_c_maybe_const_expr (arg);
3876 }
3877 else
3878 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3879 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3880 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3881 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3882 location = EXPR_LOCATION (ret);
c9f9eb5d 3883 goto return_build_unary_op;
3e4093b6 3884
3e4093b6 3885 case REALPART_EXPR:
3e4093b6 3886 case IMAGPART_EXPR:
fb52b50a
NF
3887 ret = build_real_imag_expr (location, code, arg);
3888 if (ret == error_mark_node)
3889 return error_mark_node;
8ce94e44
JM
3890 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3891 eptype = TREE_TYPE (eptype);
c9f9eb5d 3892 goto return_build_unary_op;
3e4093b6
RS
3893
3894 case PREINCREMENT_EXPR:
3895 case POSTINCREMENT_EXPR:
3896 case PREDECREMENT_EXPR:
3897 case POSTDECREMENT_EXPR:
3e4093b6 3898
928c19bb
JM
3899 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3900 {
3901 tree inner = build_unary_op (location, code,
3902 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3903 if (inner == error_mark_node)
3904 return error_mark_node;
3905 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3906 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3907 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3908 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3909 goto return_build_unary_op;
3910 }
3911
925e8657
NP
3912 /* Complain about anything that is not a true lvalue. In
3913 Objective-C, skip this check for property_refs. */
f90e8e2e 3914 if (!objc_is_property_ref (arg)
7bd11157
TT
3915 && !lvalue_or_else (location,
3916 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3917 || code == POSTINCREMENT_EXPR)
3918 ? lv_increment
3919 : lv_decrement)))
928c19bb
JM
3920 return error_mark_node;
3921
09639a83
ILT
3922 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3923 {
3924 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3925 warning_at (location, OPT_Wc___compat,
3926 "increment of enumeration value is invalid in C++");
3927 else
3928 warning_at (location, OPT_Wc___compat,
3929 "decrement of enumeration value is invalid in C++");
3930 }
3931
928c19bb
JM
3932 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3933 arg = c_fully_fold (arg, false, NULL);
3934
267bac10
JM
3935 bool atomic_op;
3936 atomic_op = really_atomic_lvalue (arg);
3937
3e4093b6
RS
3938 /* Increment or decrement the real part of the value,
3939 and don't change the imaginary part. */
3940 if (typecode == COMPLEX_TYPE)
400fbf9f 3941 {
3e4093b6
RS
3942 tree real, imag;
3943
c1771a20 3944 pedwarn (location, OPT_Wpedantic,
509c9d60 3945 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3946
267bac10
JM
3947 if (!atomic_op)
3948 {
3949 arg = stabilize_reference (arg);
3950 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3951 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3952 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3953 if (real == error_mark_node || imag == error_mark_node)
3954 return error_mark_node;
3955 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3956 real, imag);
3957 goto return_build_unary_op;
3958 }
400fbf9f 3959 }
3e4093b6
RS
3960
3961 /* Report invalid types. */
3962
ab22c1fa 3963 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3964 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 3965 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 3966 {
3e4093b6 3967 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3968 error_at (location, "wrong type argument to increment");
c22cacf3 3969 else
c9f9eb5d 3970 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3971
3972 return error_mark_node;
400fbf9f 3973 }
400fbf9f 3974
3e4093b6
RS
3975 {
3976 tree inc;
400fbf9f 3977
3e4093b6
RS
3978 argtype = TREE_TYPE (arg);
3979
3980 /* Compute the increment. */
3981
3982 if (typecode == POINTER_TYPE)
3983 {
a0e24419 3984 /* If pointer target is an incomplete type,
3e4093b6 3985 we just cannot know how to do the arithmetic. */
b70cef5d 3986 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
3987 {
3988 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3989 error_at (location,
a0e24419
MP
3990 "increment of pointer to an incomplete type %qT",
3991 TREE_TYPE (argtype));
3e4093b6 3992 else
c9f9eb5d 3993 error_at (location,
a0e24419
MP
3994 "decrement of pointer to an incomplete type %qT",
3995 TREE_TYPE (argtype));
3e4093b6 3996 }
b70cef5d
JJ
3997 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
3998 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 3999 {
3e4093b6 4000 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4001 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4002 "wrong type argument to increment");
3e4093b6 4003 else
44d90fe1 4004 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4005 "wrong type argument to decrement");
3e4093b6
RS
4006 }
4007
b70cef5d 4008 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4009 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4010 }
b70cef5d 4011 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4012 {
4013 /* For signed fract types, we invert ++ to -- or
4014 -- to ++, and change inc from 1 to -1, because
4015 it is not possible to represent 1 in signed fract constants.
4016 For unsigned fract types, the result always overflows and
4017 we get an undefined (original) or the maximum value. */
4018 if (code == PREINCREMENT_EXPR)
4019 code = PREDECREMENT_EXPR;
4020 else if (code == PREDECREMENT_EXPR)
4021 code = PREINCREMENT_EXPR;
4022 else if (code == POSTINCREMENT_EXPR)
4023 code = POSTDECREMENT_EXPR;
4024 else /* code == POSTDECREMENT_EXPR */
4025 code = POSTINCREMENT_EXPR;
4026
4027 inc = integer_minus_one_node;
4028 inc = convert (argtype, inc);
4029 }
3e4093b6 4030 else
5be014d5 4031 {
241b71bb
TV
4032 inc = VECTOR_TYPE_P (argtype)
4033 ? build_one_cst (argtype)
4034 : integer_one_node;
5be014d5
AP
4035 inc = convert (argtype, inc);
4036 }
3e4093b6 4037
925e8657
NP
4038 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4039 need to ask Objective-C to build the increment or decrement
4040 expression for it. */
4041 if (objc_is_property_ref (arg))
f90e8e2e 4042 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4043 arg, inc);
4044
3e4093b6 4045 /* Report a read-only lvalue. */
f37acdf9 4046 if (TYPE_READONLY (argtype))
953ff289 4047 {
c02065fc 4048 readonly_error (location, arg,
953ff289
DN
4049 ((code == PREINCREMENT_EXPR
4050 || code == POSTINCREMENT_EXPR)
4051 ? lv_increment : lv_decrement));
4052 return error_mark_node;
4053 }
f37acdf9
JM
4054 else if (TREE_READONLY (arg))
4055 readonly_warning (arg,
4056 ((code == PREINCREMENT_EXPR
4057 || code == POSTINCREMENT_EXPR)
4058 ? lv_increment : lv_decrement));
3e4093b6 4059
267bac10
JM
4060 /* If the argument is atomic, use the special code sequences for
4061 atomic compound assignment. */
4062 if (atomic_op)
4063 {
4064 arg = stabilize_reference (arg);
4065 ret = build_atomic_assign (location, arg,
4066 ((code == PREINCREMENT_EXPR
4067 || code == POSTINCREMENT_EXPR)
4068 ? PLUS_EXPR
4069 : MINUS_EXPR),
4070 (FRACT_MODE_P (TYPE_MODE (argtype))
4071 ? inc
4072 : integer_one_node),
4073 (code == POSTINCREMENT_EXPR
4074 || code == POSTDECREMENT_EXPR));
4075 goto return_build_unary_op;
4076 }
4077
3e4093b6
RS
4078 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4079 val = boolean_increment (code, arg);
4080 else
53fb4de3 4081 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4082 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4083 if (TREE_CODE (val) != code)
6de9cd9a 4084 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4085 ret = val;
4086 goto return_build_unary_op;
3e4093b6
RS
4087 }
4088
4089 case ADDR_EXPR:
4090 /* Note that this operation never does default_conversion. */
4091
2b4b7036
JM
4092 /* The operand of unary '&' must be an lvalue (which excludes
4093 expressions of type void), or, in C99, the result of a [] or
4094 unary '*' operator. */
4095 if (VOID_TYPE_P (TREE_TYPE (arg))
4096 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4097 && (TREE_CODE (arg) != INDIRECT_REF
4098 || !flag_isoc99))
4099 pedwarn (location, 0, "taking address of expression of type %<void%>");
4100
3e4093b6
RS
4101 /* Let &* cancel out to simplify resulting code. */
4102 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4103 {
3e4093b6
RS
4104 /* Don't let this be an lvalue. */
4105 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4106 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4107 ret = TREE_OPERAND (arg, 0);
4108 goto return_build_unary_op;
400fbf9f 4109 }
1eb8759b 4110
7c672dfc 4111 /* For &x[y], return x+y */
3e4093b6 4112 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4113 {
f2a71bbc
JM
4114 tree op0 = TREE_OPERAND (arg, 0);
4115 if (!c_mark_addressable (op0))
3e4093b6 4116 return error_mark_node;
1eb8759b 4117 }
1eb8759b 4118
3e4093b6
RS
4119 /* Anything not already handled and not a true memory reference
4120 or a non-lvalue array is an error. */
4121 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4122 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4123 return error_mark_node;
b6a10c9f 4124
928c19bb
JM
4125 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4126 folding later. */
4127 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4128 {
4129 tree inner = build_unary_op (location, code,
4130 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4131 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4132 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4133 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4134 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4135 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4136 goto return_build_unary_op;
4137 }
4138
3e4093b6
RS
4139 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4140 argtype = TREE_TYPE (arg);
400fbf9f 4141
3e4093b6 4142 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4143 to which the address will point. This is only needed
f2c1da78 4144 for function types. */
6615c446 4145 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4146 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4147 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4148 {
4149 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4150 int quals = orig_quals;
4151
4152 if (TREE_READONLY (arg))
4153 quals |= TYPE_QUAL_CONST;
4154 if (TREE_THIS_VOLATILE (arg))
4155 quals |= TYPE_QUAL_VOLATILE;
4156
f2c1da78
JM
4157 argtype = c_build_qualified_type (argtype, quals);
4158 }
400fbf9f 4159
3e4093b6
RS
4160 if (!c_mark_addressable (arg))
4161 return error_mark_node;
400fbf9f 4162
abb54d14
JM
4163 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4164 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4165
5cc200fc 4166 argtype = build_pointer_type (argtype);
5e55f99d
RH
4167
4168 /* ??? Cope with user tricks that amount to offsetof. Delete this
4169 when we have proper support for integer constant expressions. */
4170 val = get_base_address (arg);
4171 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4172 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4173 {
cf9e9959 4174 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4175 goto return_build_unary_op;
3aa2ddb8 4176 }
5e55f99d 4177
5cc200fc 4178 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4179
c9f9eb5d
AH
4180 ret = val;
4181 goto return_build_unary_op;
400fbf9f 4182
3e4093b6 4183 default:
1344f9a3 4184 gcc_unreachable ();
3e4093b6 4185 }
400fbf9f 4186
3e4093b6
RS
4187 if (argtype == 0)
4188 argtype = TREE_TYPE (arg);
928c19bb
JM
4189 if (TREE_CODE (arg) == INTEGER_CST)
4190 ret = (require_constant_value
db3927fb
AH
4191 ? fold_build1_initializer_loc (location, code, argtype, arg)
4192 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4193 else
4194 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4195 return_build_unary_op:
4196 gcc_assert (ret != error_mark_node);
928c19bb
JM
4197 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4198 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4199 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4200 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4201 ret = note_integer_operands (ret);
8ce94e44
JM
4202 if (eptype)
4203 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4204 protected_set_expr_location (ret, location);
4205 return ret;
3e4093b6 4206}
400fbf9f 4207
3e4093b6
RS
4208/* Return nonzero if REF is an lvalue valid for this language.
4209 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4210 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4211
1e4ae551 4212bool
58f9752a 4213lvalue_p (const_tree ref)
3e4093b6 4214{
58f9752a 4215 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4216
3e4093b6
RS
4217 switch (code)
4218 {
4219 case REALPART_EXPR:
4220 case IMAGPART_EXPR:
4221 case COMPONENT_REF:
4222 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4223
928c19bb
JM
4224 case C_MAYBE_CONST_EXPR:
4225 return lvalue_p (TREE_OPERAND (ref, 1));
4226
3e4093b6
RS
4227 case COMPOUND_LITERAL_EXPR:
4228 case STRING_CST:
4229 return 1;
400fbf9f 4230
3e4093b6
RS
4231 case INDIRECT_REF:
4232 case ARRAY_REF:
36536d79 4233 case ARRAY_NOTATION_REF:
3e4093b6
RS
4234 case VAR_DECL:
4235 case PARM_DECL:
4236 case RESULT_DECL:
4237 case ERROR_MARK:
4238 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4239 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4240
3e4093b6 4241 case BIND_EXPR:
3e4093b6 4242 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4243
3e4093b6
RS
4244 default:
4245 return 0;
4246 }
4247}
400fbf9f 4248\f
f37acdf9
JM
4249/* Give a warning for storing in something that is read-only in GCC
4250 terms but not const in ISO C terms. */
4251
4252static void
4253readonly_warning (tree arg, enum lvalue_use use)
4254{
4255 switch (use)
4256 {
4257 case lv_assign:
4258 warning (0, "assignment of read-only location %qE", arg);
4259 break;
4260 case lv_increment:
4261 warning (0, "increment of read-only location %qE", arg);
4262 break;
4263 case lv_decrement:
4264 warning (0, "decrement of read-only location %qE", arg);
4265 break;
4266 default:
4267 gcc_unreachable ();
4268 }
4269 return;
4270}
4271
37dc0d8d
JM
4272
4273/* Return nonzero if REF is an lvalue valid for this language;
4274 otherwise, print an error message and return zero. USE says
7bd11157
TT
4275 how the lvalue is being used and so selects the error message.
4276 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4277
4278static int
7bd11157 4279lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4280{
4281 int win = lvalue_p (ref);
4282
4283 if (!win)
7bd11157 4284 lvalue_error (loc, use);
37dc0d8d
JM
4285
4286 return win;
4287}
3e4093b6
RS
4288\f
4289/* Mark EXP saying that we need to be able to take the
4290 address of it; it should not be allocated in a register.
4291 Returns true if successful. */
54c93c30 4292
3e4093b6
RS
4293bool
4294c_mark_addressable (tree exp)
400fbf9f 4295{
3e4093b6 4296 tree x = exp;
95602da1 4297
3e4093b6
RS
4298 while (1)
4299 switch (TREE_CODE (x))
4300 {
4301 case COMPONENT_REF:
4302 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4303 {
0039fa55
AN
4304 error
4305 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4306 return false;
4307 }
95602da1 4308
3e4093b6 4309 /* ... fall through ... */
95602da1 4310
3e4093b6
RS
4311 case ADDR_EXPR:
4312 case ARRAY_REF:
4313 case REALPART_EXPR:
4314 case IMAGPART_EXPR:
4315 x = TREE_OPERAND (x, 0);
4316 break;
95602da1 4317
3e4093b6
RS
4318 case COMPOUND_LITERAL_EXPR:
4319 case CONSTRUCTOR:
4320 TREE_ADDRESSABLE (x) = 1;
4321 return true;
95602da1 4322
3e4093b6
RS
4323 case VAR_DECL:
4324 case CONST_DECL:
4325 case PARM_DECL:
4326 case RESULT_DECL:
5baeaac0 4327 if (C_DECL_REGISTER (x)
3e4093b6
RS
4328 && DECL_NONLOCAL (x))
4329 {
e697b20f 4330 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4331 {
0039fa55
AN
4332 error
4333 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4334 return false;
4335 }
509c9d60 4336 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4337 }
5baeaac0 4338 else if (C_DECL_REGISTER (x))
3e4093b6 4339 {
e697b20f 4340 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4341 error ("address of global register variable %qD requested", x);
4342 else
4343 error ("address of register variable %qD requested", x);
4344 return false;
3e4093b6 4345 }
400fbf9f 4346
3e4093b6
RS
4347 /* drops in */
4348 case FUNCTION_DECL:
4349 TREE_ADDRESSABLE (x) = 1;
4350 /* drops out */
4351 default:
4352 return true;
4353 }
4354}
4355\f
2d2e923f
MLI
4356/* Convert EXPR to TYPE, warning about conversion problems with
4357 constants. SEMANTIC_TYPE is the type this conversion would use
4358 without excess precision. If SEMANTIC_TYPE is NULL, this function
4359 is equivalent to convert_and_check. This function is a wrapper that
4360 handles conversions that may be different than
4361 the usual ones because of excess precision. */
4362
4363static tree
68fca595
MP
4364ep_convert_and_check (location_t loc, tree type, tree expr,
4365 tree semantic_type)
2d2e923f
MLI
4366{
4367 if (TREE_TYPE (expr) == type)
4368 return expr;
4369
4370 if (!semantic_type)
68fca595 4371 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4372
4373 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4374 && TREE_TYPE (expr) != semantic_type)
4375 {
4376 /* For integers, we need to check the real conversion, not
4377 the conversion to the excess precision type. */
68fca595 4378 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4379 }
4380 /* Result type is the excess precision type, which should be
4381 large enough, so do not check. */
4382 return convert (type, expr);
4383}
4384
928c19bb
JM
4385/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4386 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4387 if folded to an integer constant then the unselected half may
4388 contain arbitrary operations not normally permitted in constant
c2255bc4 4389 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4390
4391tree
744aa42f 4392build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4393 tree op1, tree op1_original_type, tree op2,
4394 tree op2_original_type)
400fbf9f 4395{
3e4093b6
RS
4396 tree type1;
4397 tree type2;
4398 enum tree_code code1;
4399 enum tree_code code2;
4400 tree result_type = NULL;
2d2e923f 4401 tree semantic_result_type = NULL;
3e4093b6 4402 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4403 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4404 bool ifexp_int_operands;
928c19bb 4405 tree ret;
400fbf9f 4406
4d84fe7c
JM
4407 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4408 if (op1_int_operands)
4409 op1 = remove_c_maybe_const_expr (op1);
4410 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4411 if (op2_int_operands)
4412 op2 = remove_c_maybe_const_expr (op2);
4413 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4414 if (ifexp_int_operands)
4415 ifexp = remove_c_maybe_const_expr (ifexp);
4416
3e4093b6
RS
4417 /* Promote both alternatives. */
4418
4419 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4420 op1 = default_conversion (op1);
4421 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4422 op2 = default_conversion (op2);
4423
4424 if (TREE_CODE (ifexp) == ERROR_MARK
4425 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4426 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4427 return error_mark_node;
400fbf9f 4428
3e4093b6
RS
4429 type1 = TREE_TYPE (op1);
4430 code1 = TREE_CODE (type1);
4431 type2 = TREE_TYPE (op2);
4432 code2 = TREE_CODE (type2);
4433
b1adf557
JM
4434 /* C90 does not permit non-lvalue arrays in conditional expressions.
4435 In C99 they will be pointers by now. */
4436 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4437 {
744aa42f 4438 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4439 return error_mark_node;
4440 }
4441
8ce94e44
JM
4442 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4443 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4444 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4445 || code1 == COMPLEX_TYPE)
4446 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4447 || code2 == COMPLEX_TYPE))
4448 {
2d2e923f 4449 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4450 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4451 {
4452 op1 = TREE_OPERAND (op1, 0);
4453 type1 = TREE_TYPE (op1);
4454 gcc_assert (TREE_CODE (type1) == code1);
4455 }
4456 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4457 {
4458 op2 = TREE_OPERAND (op2, 0);
4459 type2 = TREE_TYPE (op2);
4460 gcc_assert (TREE_CODE (type2) == code2);
4461 }
4462 }
4463
d130ae11
ILT
4464 if (warn_cxx_compat)
4465 {
4466 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4467 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4468
4469 if (TREE_CODE (t1) == ENUMERAL_TYPE
4470 && TREE_CODE (t2) == ENUMERAL_TYPE
4471 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4472 warning_at (colon_loc, OPT_Wc___compat,
4473 ("different enum types in conditional is "
4474 "invalid in C++: %qT vs %qT"),
4475 t1, t2);
4476 }
4477
3e4093b6
RS
4478 /* Quickly detect the usual case where op1 and op2 have the same type
4479 after promotion. */
4480 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4481 {
3e4093b6
RS
4482 if (type1 == type2)
4483 result_type = type1;
4484 else
4485 result_type = TYPE_MAIN_VARIANT (type1);
4486 }
4487 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4488 || code1 == COMPLEX_TYPE)
4489 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4490 || code2 == COMPLEX_TYPE))
3e4093b6 4491 {
ccf7f880 4492 result_type = c_common_type (type1, type2);
0a0b3574
MM
4493 do_warn_double_promotion (result_type, type1, type2,
4494 "implicit conversion from %qT to %qT to "
4495 "match other result of conditional",
4496 colon_loc);
400fbf9f 4497
3e4093b6
RS
4498 /* If -Wsign-compare, warn here if type1 and type2 have
4499 different signedness. We'll promote the signed to unsigned
4500 and later code won't know it used to be different.
4501 Do this check on the original types, so that explicit casts
4502 will be considered, but default promotions won't. */
7d882b83 4503 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4504 {
8df83eae
RK
4505 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4506 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4507
3e4093b6
RS
4508 if (unsigned_op1 ^ unsigned_op2)
4509 {
6ac01510
ILT
4510 bool ovf;
4511
3e4093b6
RS
4512 /* Do not warn if the result type is signed, since the
4513 signed type will only be chosen if it can represent
4514 all the values of the unsigned type. */
3f75a254 4515 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4516 /* OK */;
3e4093b6 4517 else
928c19bb
JM
4518 {
4519 bool op1_maybe_const = true;
4520 bool op2_maybe_const = true;
4521
4522 /* Do not warn if the signed quantity is an
4523 unsuffixed integer literal (or some static
4524 constant expression involving such literals) and
4525 it is non-negative. This warning requires the
4526 operands to be folded for best results, so do
4527 that folding in this case even without
4528 warn_sign_compare to avoid warning options
4529 possibly affecting code generation. */
f5178456
RS
4530 c_inhibit_evaluation_warnings
4531 += (ifexp == truthvalue_false_node);
928c19bb
JM
4532 op1 = c_fully_fold (op1, require_constant_value,
4533 &op1_maybe_const);
f5178456
RS
4534 c_inhibit_evaluation_warnings
4535 -= (ifexp == truthvalue_false_node);
4536
4537 c_inhibit_evaluation_warnings
4538 += (ifexp == truthvalue_true_node);
928c19bb
JM
4539 op2 = c_fully_fold (op2, require_constant_value,
4540 &op2_maybe_const);
f5178456
RS
4541 c_inhibit_evaluation_warnings
4542 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4543
4544 if (warn_sign_compare)
4545 {
4546 if ((unsigned_op2
4547 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4548 || (unsigned_op1
4549 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4550 /* OK */;
4551 else
744aa42f
ILT
4552 warning_at (colon_loc, OPT_Wsign_compare,
4553 ("signed and unsigned type in "
4554 "conditional expression"));
928c19bb
JM
4555 }
4556 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4557 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4558 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4559 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4560 }
3e4093b6
RS
4561 }
4562 }
4563 }
4564 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4565 {
fcf73884 4566 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4567 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4568 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4569 result_type = void_type_node;
4570 }
4571 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4572 {
36c5e70a
BE
4573 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4574 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4575 addr_space_t as_common;
4576
744aa42f 4577 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4578 result_type = common_pointer_type (type1, type2);
6aa3c60d 4579 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4580 result_type = type2;
6aa3c60d 4581 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4582 result_type = type1;
4583 else if (!addr_space_superset (as1, as2, &as_common))
4584 {
4585 error_at (colon_loc, "pointers to disjoint address spaces "
4586 "used in conditional expression");
4587 return error_mark_node;
4588 }
267bac10
JM
4589 else if (VOID_TYPE_P (TREE_TYPE (type1))
4590 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4591 {
fcf73884 4592 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4593 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4594 "ISO C forbids conditional expr between "
bda67431 4595 "%<void *%> and function pointer");
3e4093b6
RS
4596 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4597 TREE_TYPE (type2)));
34a80643 4598 }
267bac10
JM
4599 else if (VOID_TYPE_P (TREE_TYPE (type2))
4600 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4601 {
fcf73884 4602 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4603 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4604 "ISO C forbids conditional expr between "
bda67431 4605 "%<void *%> and function pointer");
3e4093b6
RS
4606 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4607 TREE_TYPE (type1)));
1c2a9b35 4608 }
b581b85b
NP
4609 /* Objective-C pointer comparisons are a bit more lenient. */
4610 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4611 result_type = objc_common_type (type1, type2);
34a80643 4612 else
ab87f8c8 4613 {
36c5e70a
BE
4614 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4615
b581b85b
NP
4616 pedwarn (colon_loc, 0,
4617 "pointer type mismatch in conditional expression");
36c5e70a
BE
4618 result_type = build_pointer_type
4619 (build_qualified_type (void_type_node, qual));
ab87f8c8 4620 }
3e4093b6
RS
4621 }
4622 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4623 {
6aa3c60d 4624 if (!null_pointer_constant_p (orig_op2))
744aa42f 4625 pedwarn (colon_loc, 0,
509c9d60 4626 "pointer/integer type mismatch in conditional expression");
3e4093b6 4627 else
ab87f8c8 4628 {
3e4093b6 4629 op2 = null_pointer_node;
ab87f8c8 4630 }
3e4093b6
RS
4631 result_type = type1;
4632 }
4633 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4634 {
6aa3c60d 4635 if (!null_pointer_constant_p (orig_op1))
744aa42f 4636 pedwarn (colon_loc, 0,
509c9d60 4637 "pointer/integer type mismatch in conditional expression");
3e4093b6 4638 else
ab87f8c8 4639 {
3e4093b6 4640 op1 = null_pointer_node;
ab87f8c8 4641 }
3e4093b6
RS
4642 result_type = type2;
4643 }
1c2a9b35 4644
3e4093b6
RS
4645 if (!result_type)
4646 {
4647 if (flag_cond_mismatch)
4648 result_type = void_type_node;
4649 else
400fbf9f 4650 {
c2255bc4 4651 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4652 return error_mark_node;
400fbf9f 4653 }
3e4093b6 4654 }
400fbf9f 4655
3e4093b6
RS
4656 /* Merge const and volatile flags of the incoming types. */
4657 result_type
4658 = build_type_variant (result_type,
afbd0665
AS
4659 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4660 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4661
68fca595
MP
4662 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4663 semantic_result_type);
4664 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4665 semantic_result_type);
b6a10c9f 4666
928c19bb
JM
4667 if (ifexp_bcp && ifexp == truthvalue_true_node)
4668 {
4669 op2_int_operands = true;
4670 op1 = c_fully_fold (op1, require_constant_value, NULL);
4671 }
4672 if (ifexp_bcp && ifexp == truthvalue_false_node)
4673 {
4674 op1_int_operands = true;
4675 op2 = c_fully_fold (op2, require_constant_value, NULL);
4676 }
4d84fe7c 4677 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4678 && op1_int_operands
4679 && op2_int_operands);
4680 if (int_operands)
4681 {
4682 int_const = ((ifexp == truthvalue_true_node
4683 && TREE_CODE (orig_op1) == INTEGER_CST
4684 && !TREE_OVERFLOW (orig_op1))
4685 || (ifexp == truthvalue_false_node
4686 && TREE_CODE (orig_op2) == INTEGER_CST
4687 && !TREE_OVERFLOW (orig_op2)));
4688 }
4689 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4690 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4691 else
4692 {
01c7ccbb
JM
4693 if (int_operands)
4694 {
f34f1c87
MP
4695 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4696 nested inside of the expression. */
4697 op1 = c_fully_fold (op1, false, NULL);
4698 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4699 }
928c19bb
JM
4700 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4701 if (int_operands)
4702 ret = note_integer_operands (ret);
4703 }
2d2e923f
MLI
4704 if (semantic_result_type)
4705 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4706
c2255bc4 4707 protected_set_expr_location (ret, colon_loc);
928c19bb 4708 return ret;
3e4093b6
RS
4709}
4710\f
487a92fe 4711/* Return a compound expression that performs two expressions and
c2255bc4
AH
4712 returns the value of the second of them.
4713
4714 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4715
3e4093b6 4716tree
c2255bc4 4717build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4718{
4d84fe7c 4719 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4720 tree eptype = NULL_TREE;
928c19bb
JM
4721 tree ret;
4722
b72271b9 4723 if (flag_cilkplus
939b37da
BI
4724 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4725 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4726 {
4727 error_at (loc,
4728 "spawned function call cannot be part of a comma expression");
4729 return error_mark_node;
4730 }
4d84fe7c
JM
4731 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4732 if (expr1_int_operands)
4733 expr1 = remove_c_maybe_const_expr (expr1);
4734 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4735 if (expr2_int_operands)
4736 expr2 = remove_c_maybe_const_expr (expr2);
4737
8ce94e44
JM
4738 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4739 expr1 = TREE_OPERAND (expr1, 0);
4740 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4741 {
4742 eptype = TREE_TYPE (expr2);
4743 expr2 = TREE_OPERAND (expr2, 0);
4744 }
4745
3f75a254 4746 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4747 {
4748 /* The left-hand operand of a comma expression is like an expression
c5409249 4749 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4750 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4751 if (warn_unused_value)
47aecf47 4752 {
e14a6540 4753 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4754 && CONVERT_EXPR_P (expr1))
47aecf47 4755 ; /* (void) a, b */
e14a6540
JM
4756 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4757 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4758 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4759 ; /* (void) a, (void) b, c */
4760 else
b8698a0f 4761 warning_at (loc, OPT_Wunused_value,
c2255bc4 4762 "left-hand operand of comma expression has no effect");
47aecf47 4763 }
3e4093b6 4764 }
789eadcd
MP
4765 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4766 && warn_unused_value)
4767 {
4768 tree r = expr1;
4769 location_t cloc = loc;
4770 while (TREE_CODE (r) == COMPOUND_EXPR)
4771 {
4772 if (EXPR_HAS_LOCATION (r))
4773 cloc = EXPR_LOCATION (r);
4774 r = TREE_OPERAND (r, 1);
4775 }
4776 if (!TREE_SIDE_EFFECTS (r)
4777 && !VOID_TYPE_P (TREE_TYPE (r))
4778 && !CONVERT_EXPR_P (r))
4779 warning_at (cloc, OPT_Wunused_value,
4780 "right-hand operand of comma expression has no effect");
4781 }
400fbf9f 4782
3e4093b6
RS
4783 /* With -Wunused, we should also warn if the left-hand operand does have
4784 side-effects, but computes a value which is not used. For example, in
4785 `foo() + bar(), baz()' the result of the `+' operator is not used,
4786 so we should issue a warning. */
4787 else if (warn_unused_value)
c2255bc4 4788 warn_if_unused_value (expr1, loc);
400fbf9f 4789
e63d6886
AP
4790 if (expr2 == error_mark_node)
4791 return error_mark_node;
4792
928c19bb
JM
4793 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4794
4795 if (flag_isoc99
4d84fe7c
JM
4796 && expr1_int_operands
4797 && expr2_int_operands)
928c19bb
JM
4798 ret = note_integer_operands (ret);
4799
8ce94e44
JM
4800 if (eptype)
4801 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4802
c2255bc4 4803 protected_set_expr_location (ret, loc);
928c19bb 4804 return ret;
3e4093b6 4805}
400fbf9f 4806
67165eb3
ILT
4807/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4808 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4809 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4810 of the cast. -Wcast-qual appeared on the command line. Named
4811 address space qualifiers are not handled here, because they result
4812 in different warnings. */
67165eb3
ILT
4813
4814static void
2ee3cb35 4815handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4816{
4817 tree in_type = type;
4818 tree in_otype = otype;
4819 int added = 0;
4820 int discarded = 0;
4821 bool is_const;
4822
4823 /* Check that the qualifiers on IN_TYPE are a superset of the
4824 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4825 nodes is uninteresting and we stop as soon as we hit a
4826 non-POINTER_TYPE node on either type. */
4827 do
4828 {
4829 in_otype = TREE_TYPE (in_otype);
4830 in_type = TREE_TYPE (in_type);
4831
4832 /* GNU C allows cv-qualified function types. 'const' means the
4833 function is very pure, 'volatile' means it can't return. We
4834 need to warn when such qualifiers are added, not when they're
4835 taken away. */
4836 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4837 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4838 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4839 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4840 else
36c5e70a
BE
4841 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4842 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4843 }
4844 while (TREE_CODE (in_type) == POINTER_TYPE
4845 && TREE_CODE (in_otype) == POINTER_TYPE);
4846
4847 if (added)
2ee3cb35
MLI
4848 warning_at (loc, OPT_Wcast_qual,
4849 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4850
4851 if (discarded)
4852 /* There are qualifiers present in IN_OTYPE that are not present
4853 in IN_TYPE. */
2ee3cb35 4854 warning_at (loc, OPT_Wcast_qual,
7485aeea 4855 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4856 discarded);
67165eb3
ILT
4857
4858 if (added || discarded)
4859 return;
4860
4861 /* A cast from **T to const **T is unsafe, because it can cause a
4862 const value to be changed with no additional warning. We only
4863 issue this warning if T is the same on both sides, and we only
4864 issue the warning if there are the same number of pointers on
4865 both sides, as otherwise the cast is clearly unsafe anyhow. A
4866 cast is unsafe when a qualifier is added at one level and const
4867 is not present at all outer levels.
4868
4869 To issue this warning, we check at each level whether the cast
4870 adds new qualifiers not already seen. We don't need to special
4871 case function types, as they won't have the same
4872 TYPE_MAIN_VARIANT. */
4873
4874 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4875 return;
4876 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4877 return;
4878
4879 in_type = type;
4880 in_otype = otype;
4881 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4882 do
4883 {
4884 in_type = TREE_TYPE (in_type);
4885 in_otype = TREE_TYPE (in_otype);
4886 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4887 && !is_const)
4888 {
2ee3cb35
MLI
4889 warning_at (loc, OPT_Wcast_qual,
4890 "to be safe all intermediate pointers in cast from "
4891 "%qT to %qT must be %<const%> qualified",
4892 otype, type);
67165eb3
ILT
4893 break;
4894 }
4895 if (is_const)
4896 is_const = TYPE_READONLY (in_type);
4897 }
4898 while (TREE_CODE (in_type) == POINTER_TYPE);
4899}
4900
b8698a0f 4901/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4902 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4903
3e4093b6 4904tree
c2255bc4 4905build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4906{
8ce94e44
JM
4907 tree value;
4908
4909 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4910 expr = TREE_OPERAND (expr, 0);
4911
4912 value = expr;
400fbf9f 4913
3e4093b6
RS
4914 if (type == error_mark_node || expr == error_mark_node)
4915 return error_mark_node;
400fbf9f 4916
3e4093b6
RS
4917 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4918 only in <protocol> qualifications. But when constructing cast expressions,
4919 the protocols do matter and must be kept around. */
700686fa
ZL
4920 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4921 return build1 (NOP_EXPR, type, expr);
4922
4923 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4924
3e4093b6
RS
4925 if (TREE_CODE (type) == ARRAY_TYPE)
4926 {
c2255bc4 4927 error_at (loc, "cast specifies array type");
3e4093b6
RS
4928 return error_mark_node;
4929 }
400fbf9f 4930
3e4093b6
RS
4931 if (TREE_CODE (type) == FUNCTION_TYPE)
4932 {
c2255bc4 4933 error_at (loc, "cast specifies function type");
3e4093b6
RS
4934 return error_mark_node;
4935 }
400fbf9f 4936
808d6eaa
JM
4937 if (!VOID_TYPE_P (type))
4938 {
4939 value = require_complete_type (value);
4940 if (value == error_mark_node)
4941 return error_mark_node;
4942 }
4943
3e4093b6
RS
4944 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4945 {
fcf73884
MLI
4946 if (TREE_CODE (type) == RECORD_TYPE
4947 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4948 pedwarn (loc, OPT_Wpedantic,
fcf73884 4949 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
4950
4951 /* Convert to remove any qualifiers from VALUE's type. */
4952 value = convert (type, value);
3e4093b6
RS
4953 }
4954 else if (TREE_CODE (type) == UNION_TYPE)
4955 {
4956 tree field;
400fbf9f 4957
910ad8de 4958 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4959 if (TREE_TYPE (field) != error_mark_node
4960 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4961 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4962 break;
4963
4964 if (field)
400fbf9f 4965 {
3e4093b6 4966 tree t;
e616f54d 4967 bool maybe_const = true;
3e4093b6 4968
c1771a20 4969 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4970 t = c_fully_fold (value, false, &maybe_const);
4971 t = build_constructor_single (type, field, t);
4972 if (!maybe_const)
4973 t = c_wrap_maybe_const (t, true);
4974 t = digest_init (loc, type, t,
bbbbb16a 4975 NULL_TREE, false, true, 0);
3e4093b6
RS
4976 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4977 return t;
400fbf9f 4978 }
c2255bc4 4979 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4980 return error_mark_node;
4981 }
4982 else
4983 {
4984 tree otype, ovalue;
400fbf9f 4985
3e4093b6 4986 if (type == void_type_node)
c2255bc4
AH
4987 {
4988 tree t = build1 (CONVERT_EXPR, type, value);
4989 SET_EXPR_LOCATION (t, loc);
4990 return t;
4991 }
400fbf9f 4992
3e4093b6 4993 otype = TREE_TYPE (value);
400fbf9f 4994
3e4093b6 4995 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
4996 if (warn_cast_qual
4997 && TREE_CODE (type) == POINTER_TYPE
4998 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 4999 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5000
36c5e70a
BE
5001 /* Warn about conversions between pointers to disjoint
5002 address spaces. */
5003 if (TREE_CODE (type) == POINTER_TYPE
5004 && TREE_CODE (otype) == POINTER_TYPE
5005 && !null_pointer_constant_p (value))
5006 {
5007 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5008 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5009 addr_space_t as_common;
5010
5011 if (!addr_space_superset (as_to, as_from, &as_common))
5012 {
5013 if (ADDR_SPACE_GENERIC_P (as_from))
5014 warning_at (loc, 0, "cast to %s address space pointer "
5015 "from disjoint generic address space pointer",
5016 c_addr_space_name (as_to));
5017
5018 else if (ADDR_SPACE_GENERIC_P (as_to))
5019 warning_at (loc, 0, "cast to generic address space pointer "
5020 "from disjoint %s address space pointer",
5021 c_addr_space_name (as_from));
5022
5023 else
5024 warning_at (loc, 0, "cast to %s address space pointer "
5025 "from disjoint %s address space pointer",
5026 c_addr_space_name (as_to),
5027 c_addr_space_name (as_from));
5028 }
5029 }
5030
3e4093b6 5031 /* Warn about possible alignment problems. */
3176a0c2 5032 if (STRICT_ALIGNMENT
3e4093b6
RS
5033 && TREE_CODE (type) == POINTER_TYPE
5034 && TREE_CODE (otype) == POINTER_TYPE
5035 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5036 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5037 /* Don't warn about opaque types, where the actual alignment
5038 restriction is unknown. */
5039 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5040 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5041 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5042 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5043 warning_at (loc, OPT_Wcast_align,
5044 "cast increases required alignment of target type");
e9a25f70 5045
3176a0c2 5046 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5047 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5048 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5049 /* Unlike conversion of integers to pointers, where the
5050 warning is disabled for converting constants because
5051 of cases such as SIG_*, warn about converting constant
5052 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5053 sign extension, and a warning is appropriate. */
c2255bc4
AH
5054 warning_at (loc, OPT_Wpointer_to_int_cast,
5055 "cast from pointer to integer of different size");
400fbf9f 5056
3176a0c2 5057 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5058 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5059 warning_at (loc, OPT_Wbad_function_cast,
5060 "cast from function call of type %qT "
5061 "to non-matching type %qT", otype, type);
400fbf9f 5062
3176a0c2 5063 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5064 && TREE_CODE (otype) == INTEGER_TYPE
5065 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5066 /* Don't warn about converting any constant. */
5067 && !TREE_CONSTANT (value))
c2255bc4
AH
5068 warning_at (loc,
5069 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5070 "of different size");
400fbf9f 5071
79bedddc
SR
5072 if (warn_strict_aliasing <= 2)
5073 strict_aliasing_warning (otype, type, expr);
400fbf9f 5074
3897f229
JM
5075 /* If pedantic, warn for conversions between function and object
5076 pointer types, except for converting a null pointer constant
5077 to function pointer type. */
5078 if (pedantic
5079 && TREE_CODE (type) == POINTER_TYPE
5080 && TREE_CODE (otype) == POINTER_TYPE
5081 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5082 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5083 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5084 "conversion of function pointer to object pointer type");
3897f229
JM
5085
5086 if (pedantic
5087 && TREE_CODE (type) == POINTER_TYPE
5088 && TREE_CODE (otype) == POINTER_TYPE
5089 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5090 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5091 && !null_pointer_constant_p (value))
c1771a20 5092 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5093 "conversion of object pointer to function pointer type");
3897f229 5094
3e4093b6 5095 ovalue = value;
3e4093b6 5096 value = convert (type, value);
400fbf9f 5097
3e4093b6 5098 /* Ignore any integer overflow caused by the cast. */
928c19bb 5099 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5100 {
8bcd6380 5101 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5102 {
8bcd6380
RS
5103 if (!TREE_OVERFLOW (value))
5104 {
5105 /* Avoid clobbering a shared constant. */
5106 value = copy_node (value);
5107 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5108 }
6414bad6 5109 }
8bcd6380 5110 else if (TREE_OVERFLOW (value))
d8e1f97b 5111 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5112 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5113 }
5114 }
400fbf9f 5115
53cd18ec
JM
5116 /* Don't let a cast be an lvalue. */
5117 if (value == expr)
db3927fb 5118 value = non_lvalue_loc (loc, value);
e9a25f70 5119
928c19bb
JM
5120 /* Don't allow the results of casting to floating-point or complex
5121 types be confused with actual constants, or casts involving
5122 integer and pointer types other than direct integer-to-integer
5123 and integer-to-pointer be confused with integer constant
5124 expressions and null pointer constants. */
5125 if (TREE_CODE (value) == REAL_CST
5126 || TREE_CODE (value) == COMPLEX_CST
5127 || (TREE_CODE (value) == INTEGER_CST
5128 && !((TREE_CODE (expr) == INTEGER_CST
5129 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5130 || TREE_CODE (expr) == REAL_CST
5131 || TREE_CODE (expr) == COMPLEX_CST)))
5132 value = build1 (NOP_EXPR, type, value);
5133
c2255bc4
AH
5134 if (CAN_HAVE_LOCATION_P (value))
5135 SET_EXPR_LOCATION (value, loc);
3e4093b6 5136 return value;
400fbf9f
JW
5137}
5138
c2255bc4
AH
5139/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5140 location of the open paren of the cast, or the position of the cast
5141 expr. */
3e4093b6 5142tree
c2255bc4 5143c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5144{
f8893e47 5145 tree type;
928c19bb
JM
5146 tree type_expr = NULL_TREE;
5147 bool type_expr_const = true;
5148 tree ret;
3e4093b6 5149 int saved_wsp = warn_strict_prototypes;
c5c76735 5150
3e4093b6
RS
5151 /* This avoids warnings about unprototyped casts on
5152 integers. E.g. "#define SIG_DFL (void(*)())0". */
5153 if (TREE_CODE (expr) == INTEGER_CST)
5154 warn_strict_prototypes = 0;
928c19bb 5155 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5156 warn_strict_prototypes = saved_wsp;
c5c76735 5157
c2255bc4 5158 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5159 if (type_expr)
5160 {
9f33203d
JM
5161 bool inner_expr_const = true;
5162 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5163 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5164 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5165 && inner_expr_const);
c2255bc4 5166 SET_EXPR_LOCATION (ret, loc);
928c19bb 5167 }
24b97832
ILT
5168
5169 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5170 SET_EXPR_LOCATION (ret, loc);
5171
9e5b2115
PB
5172 /* C++ does not permits types to be defined in a cast, but it
5173 allows references to incomplete types. */
5174 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5175 warning_at (loc, OPT_Wc___compat,
5176 "defining a type in a cast is invalid in C++");
5177
928c19bb 5178 return ret;
400fbf9f 5179}
3e4093b6
RS
5180\f
5181/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5182 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5183 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5184 MODIFYCODE is the code for a binary operator that we use
5185 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5186 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5187 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5188 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5189
c2255bc4
AH
5190 LOCATION is the location of the MODIFYCODE operator.
5191 RHS_LOC is the location of the RHS. */
2f6e4e97 5192
3e4093b6 5193tree
32e8bb8e 5194build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5195 enum tree_code modifycode,
c2255bc4 5196 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5197{
3e4093b6
RS
5198 tree result;
5199 tree newrhs;
241f845a 5200 tree rhseval = NULL_TREE;
8ce94e44 5201 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5202 tree lhstype = TREE_TYPE (lhs);
5203 tree olhstype = lhstype;
928c19bb 5204 bool npc;
267bac10 5205 bool is_atomic_op;
e9a25f70 5206
3e4093b6
RS
5207 /* Types that aren't fully specified cannot be used in assignments. */
5208 lhs = require_complete_type (lhs);
e9a25f70 5209
3e4093b6
RS
5210 /* Avoid duplicate error messages from operands that had errors. */
5211 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5212 return error_mark_node;
400fbf9f 5213
4c2ecab0
JM
5214 /* Ensure an error for assigning a non-lvalue array to an array in
5215 C90. */
5216 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5217 {
5218 error_at (location, "assignment to expression with array type");
5219 return error_mark_node;
5220 }
5221
46a88c12 5222 /* For ObjC properties, defer this check. */
7bd11157 5223 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5224 return error_mark_node;
5225
267bac10
JM
5226 is_atomic_op = really_atomic_lvalue (lhs);
5227
8ce94e44
JM
5228 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5229 {
5230 rhs_semantic_type = TREE_TYPE (rhs);
5231 rhs = TREE_OPERAND (rhs, 0);
5232 }
5233
3e4093b6 5234 newrhs = rhs;
400fbf9f 5235
928c19bb
JM
5236 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5237 {
5238 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5239 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5240 rhs_origtype);
928c19bb
JM
5241 if (inner == error_mark_node)
5242 return error_mark_node;
5243 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5244 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5245 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5246 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5247 protected_set_expr_location (result, location);
5248 return result;
5249 }
5250
3e4093b6
RS
5251 /* If a binary op has been requested, combine the old LHS value with the RHS
5252 producing the value we should actually store into the LHS. */
5253
5254 if (modifycode != NOP_EXPR)
400fbf9f 5255 {
928c19bb 5256 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5257 lhs = stabilize_reference (lhs);
bbbbb16a 5258
267bac10
JM
5259 /* Construct the RHS for any non-atomic compound assignemnt. */
5260 if (!is_atomic_op)
5261 {
241f845a
JJ
5262 /* If in LHS op= RHS the RHS has side-effects, ensure they
5263 are preevaluated before the rest of the assignment expression's
5264 side-effects, because RHS could contain e.g. function calls
5265 that modify LHS. */
5266 if (TREE_SIDE_EFFECTS (rhs))
5267 {
5268 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5269 rhseval = newrhs;
5270 }
267bac10 5271 newrhs = build_binary_op (location,
241f845a 5272 modifycode, lhs, newrhs, 1);
267bac10
JM
5273
5274 /* The original type of the right hand side is no longer
5275 meaningful. */
5276 rhs_origtype = NULL_TREE;
5277 }
400fbf9f 5278 }
400fbf9f 5279
668ea4b1
IS
5280 if (c_dialect_objc ())
5281 {
46a88c12
NP
5282 /* Check if we are modifying an Objective-C property reference;
5283 if so, we need to generate setter calls. */
5284 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5285 if (result)
241f845a 5286 goto return_result;
46a88c12
NP
5287
5288 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5289 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5290 return error_mark_node;
5291 }
5292
9bf24266 5293 /* Give an error for storing in something that is 'const'. */
bbbd6700 5294
f37acdf9 5295 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5296 || ((TREE_CODE (lhstype) == RECORD_TYPE
5297 || TREE_CODE (lhstype) == UNION_TYPE)
5298 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5299 {
c02065fc 5300 readonly_error (location, lhs, lv_assign);
953ff289
DN
5301 return error_mark_node;
5302 }
f37acdf9
JM
5303 else if (TREE_READONLY (lhs))
5304 readonly_warning (lhs, lv_assign);
bbbd6700 5305
3e4093b6
RS
5306 /* If storing into a structure or union member,
5307 it has probably been given type `int'.
5308 Compute the type that would go with
5309 the actual amount of storage the member occupies. */
bbbd6700 5310
3e4093b6
RS
5311 if (TREE_CODE (lhs) == COMPONENT_REF
5312 && (TREE_CODE (lhstype) == INTEGER_TYPE
5313 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5314 || TREE_CODE (lhstype) == REAL_TYPE
5315 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5316 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5317
3e4093b6
RS
5318 /* If storing in a field that is in actuality a short or narrower than one,
5319 we must store in the field in its actual type. */
5320
5321 if (lhstype != TREE_TYPE (lhs))
5322 {
5323 lhs = copy_node (lhs);
5324 TREE_TYPE (lhs) = lhstype;
400fbf9f 5325 }
400fbf9f 5326
32e8bb8e
ILT
5327 /* Issue -Wc++-compat warnings about an assignment to an enum type
5328 when LHS does not have its original type. This happens for,
5329 e.g., an enum bitfield in a struct. */
5330 if (warn_cxx_compat
5331 && lhs_origtype != NULL_TREE
5332 && lhs_origtype != lhstype
5333 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5334 {
5335 tree checktype = (rhs_origtype != NULL_TREE
5336 ? rhs_origtype
5337 : TREE_TYPE (rhs));
5338 if (checktype != error_mark_node
267bac10
JM
5339 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5340 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5341 warning_at (location, OPT_Wc___compat,
5342 "enum conversion in assignment is invalid in C++");
5343 }
5344
267bac10
JM
5345 /* If the lhs is atomic, remove that qualifier. */
5346 if (is_atomic_op)
5347 {
5348 lhstype = build_qualified_type (lhstype,
5349 (TYPE_QUALS (lhstype)
5350 & ~TYPE_QUAL_ATOMIC));
5351 olhstype = build_qualified_type (olhstype,
5352 (TYPE_QUALS (lhstype)
5353 & ~TYPE_QUAL_ATOMIC));
5354 }
5355
8ce94e44
JM
5356 /* Convert new value to destination type. Fold it first, then
5357 restore any excess precision information, for the sake of
5358 conversion warnings. */
400fbf9f 5359
267bac10
JM
5360 if (!(is_atomic_op && modifycode != NOP_EXPR))
5361 {
5362 npc = null_pointer_constant_p (newrhs);
5363 newrhs = c_fully_fold (newrhs, false, NULL);
5364 if (rhs_semantic_type)
5365 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5366 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5367 rhs_origtype, ic_assign, npc,
5368 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5369 if (TREE_CODE (newrhs) == ERROR_MARK)
5370 return error_mark_node;
5371 }
400fbf9f 5372
6e955430
ZL
5373 /* Emit ObjC write barrier, if necessary. */
5374 if (c_dialect_objc () && flag_objc_gc)
5375 {
5376 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5377 if (result)
c9f9eb5d
AH
5378 {
5379 protected_set_expr_location (result, location);
241f845a 5380 goto return_result;
c9f9eb5d 5381 }
6e955430
ZL
5382 }
5383
ea4b7848 5384 /* Scan operands. */
400fbf9f 5385
267bac10
JM
5386 if (is_atomic_op)
5387 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5388 else
5389 {
5390 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5391 TREE_SIDE_EFFECTS (result) = 1;
5392 protected_set_expr_location (result, location);
5393 }
400fbf9f 5394
3e4093b6
RS
5395 /* If we got the LHS in a different type for storing in,
5396 convert the result back to the nominal type of LHS
5397 so that the value we return always has the same type
5398 as the LHS argument. */
e855c5ce 5399
3e4093b6 5400 if (olhstype == TREE_TYPE (result))
241f845a 5401 goto return_result;
c9f9eb5d 5402
68fca595
MP
5403 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5404 rhs_origtype, ic_assign, false, NULL_TREE,
5405 NULL_TREE, 0);
c9f9eb5d 5406 protected_set_expr_location (result, location);
241f845a
JJ
5407
5408return_result:
5409 if (rhseval)
5410 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5411 return result;
3e4093b6
RS
5412}
5413\f
478a1c5b
ILT
5414/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5415 This is used to implement -fplan9-extensions. */
5416
5417static bool
5418find_anonymous_field_with_type (tree struct_type, tree type)
5419{
5420 tree field;
5421 bool found;
5422
5423 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5424 || TREE_CODE (struct_type) == UNION_TYPE);
5425 found = false;
5426 for (field = TYPE_FIELDS (struct_type);
5427 field != NULL_TREE;
5428 field = TREE_CHAIN (field))
5429 {
267bac10
JM
5430 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5431 ? c_build_qualified_type (TREE_TYPE (field),
5432 TYPE_QUAL_ATOMIC)
5433 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5434 if (DECL_NAME (field) == NULL
267bac10 5435 && comptypes (type, fieldtype))
478a1c5b
ILT
5436 {
5437 if (found)
5438 return false;
5439 found = true;
5440 }
5441 else if (DECL_NAME (field) == NULL
5442 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5443 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5444 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5445 {
5446 if (found)
5447 return false;
5448 found = true;
5449 }
5450 }
5451 return found;
5452}
5453
5454/* RHS is an expression whose type is pointer to struct. If there is
5455 an anonymous field in RHS with type TYPE, then return a pointer to
5456 that field in RHS. This is used with -fplan9-extensions. This
5457 returns NULL if no conversion could be found. */
5458
5459static tree
5460convert_to_anonymous_field (location_t location, tree type, tree rhs)
5461{
5462 tree rhs_struct_type, lhs_main_type;
5463 tree field, found_field;
5464 bool found_sub_field;
5465 tree ret;
5466
5467 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5468 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5469 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5470 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5471
5472 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5473 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5474 ? c_build_qualified_type (TREE_TYPE (type),
5475 TYPE_QUAL_ATOMIC)
5476 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5477
5478 found_field = NULL_TREE;
5479 found_sub_field = false;
5480 for (field = TYPE_FIELDS (rhs_struct_type);
5481 field != NULL_TREE;
5482 field = TREE_CHAIN (field))
5483 {
5484 if (DECL_NAME (field) != NULL_TREE
5485 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5486 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5487 continue;
267bac10
JM
5488 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5489 ? c_build_qualified_type (TREE_TYPE (field),
5490 TYPE_QUAL_ATOMIC)
5491 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5492 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5493 {
5494 if (found_field != NULL_TREE)
5495 return NULL_TREE;
5496 found_field = field;
5497 }
5498 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5499 lhs_main_type))
5500 {
5501 if (found_field != NULL_TREE)
5502 return NULL_TREE;
5503 found_field = field;
5504 found_sub_field = true;
5505 }
5506 }
5507
5508 if (found_field == NULL_TREE)
5509 return NULL_TREE;
5510
5511 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5512 build_fold_indirect_ref (rhs), found_field,
5513 NULL_TREE);
5514 ret = build_fold_addr_expr_loc (location, ret);
5515
5516 if (found_sub_field)
5517 {
5518 ret = convert_to_anonymous_field (location, type, ret);
5519 gcc_assert (ret != NULL_TREE);
5520 }
5521
5522 return ret;
5523}
5524
63bc4e87
MP
5525/* Issue an error message for a bad initializer component.
5526 GMSGID identifies the message.
5527 The component name is taken from the spelling stack. */
5528
5529static void
ea58ef42 5530error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5531{
5532 char *ofwhat;
5533
5534 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5535 error_at (loc, gmsgid);
63bc4e87
MP
5536 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5537 if (*ofwhat)
d7ff7ae5 5538 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5539}
5540
5541/* Issue a pedantic warning for a bad initializer component. OPT is
5542 the option OPT_* (from options.h) controlling this warning or 0 if
5543 it is unconditionally given. GMSGID identifies the message. The
5544 component name is taken from the spelling stack. */
5545
5546static void
5547pedwarn_init (location_t location, int opt, const char *gmsgid)
5548{
5549 char *ofwhat;
d7ff7ae5 5550 bool warned;
63bc4e87
MP
5551
5552 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5553 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5554 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5555 if (*ofwhat && warned)
5556 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5557}
5558
5559/* Issue a warning for a bad initializer component.
5560
5561 OPT is the OPT_W* value corresponding to the warning option that
5562 controls this warning. GMSGID identifies the message. The
5563 component name is taken from the spelling stack. */
5564
5565static void
5566warning_init (location_t loc, int opt, const char *gmsgid)
5567{
5568 char *ofwhat;
d7ff7ae5 5569 bool warned;
63bc4e87
MP
5570
5571 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5572 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5573 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5574 if (*ofwhat && warned)
5575 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5576}
5577\f
5578/* If TYPE is an array type and EXPR is a parenthesized string
5579 constant, warn if pedantic that EXPR is being used to initialize an
5580 object of type TYPE. */
5581
5582void
d033409e 5583maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5584{
5585 if (pedantic
5586 && TREE_CODE (type) == ARRAY_TYPE
5587 && TREE_CODE (expr.value) == STRING_CST
5588 && expr.original_code != STRING_CST)
d033409e 5589 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5590 "array initialized from parenthesized string constant");
5591}
5592
bbbbb16a
ILT
5593/* Convert value RHS to type TYPE as preparation for an assignment to
5594 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5595 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5596 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5597 constant before any folding.
3e4093b6
RS
5598 The real work of conversion is done by `convert'.
5599 The purpose of this function is to generate error messages
5600 for assignments that are not allowed in C.
2ac2f164
JM
5601 ERRTYPE says whether it is argument passing, assignment,
5602 initialization or return.
2f6e4e97 5603
81e5eca8
MP
5604 LOCATION is the location of the assignment, EXPR_LOC is the location of
5605 the RHS or, for a function, location of an argument.
2ac2f164 5606 FUNCTION is a tree for the function being called.
3e4093b6 5607 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5608
3e4093b6 5609static tree
81e5eca8 5610convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5611 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5612 bool null_pointer_constant, tree fundecl,
5613 tree function, int parmnum)
3e4093b6
RS
5614{
5615 enum tree_code codel = TREE_CODE (type);
8ce94e44 5616 tree orig_rhs = rhs;
3e4093b6
RS
5617 tree rhstype;
5618 enum tree_code coder;
2ac2f164 5619 tree rname = NULL_TREE;
58393038 5620 bool objc_ok = false;
2ac2f164 5621
6b4ef5c1 5622 if (errtype == ic_argpass)
2ac2f164
JM
5623 {
5624 tree selector;
5625 /* Change pointer to function to the function itself for
5626 diagnostics. */
5627 if (TREE_CODE (function) == ADDR_EXPR
5628 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5629 function = TREE_OPERAND (function, 0);
5630
5631 /* Handle an ObjC selector specially for diagnostics. */
5632 selector = objc_message_selector ();
5633 rname = function;
5634 if (selector && parmnum > 2)
5635 {
5636 rname = selector;
5637 parmnum -= 2;
5638 }
5639 }
5640
5641 /* This macro is used to emit diagnostics to ensure that all format
5642 strings are complete sentences, visible to gettext and checked at
5643 compile time. */
5c1bc275 5644#define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5645 do { \
5646 switch (errtype) \
5647 { \
5648 case ic_argpass: \
5c1bc275 5649 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5650 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5651 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5652 "expected %qT but argument is of type %qT", \
5653 type, rhstype); \
5654 break; \
1e053dfe
MLI
5655 case ic_assign: \
5656 pedwarn (LOCATION, OPT, AS); \
5657 break; \
5658 case ic_init: \
6a8f4e12 5659 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5660 break; \
5661 case ic_return: \
d033409e 5662 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5663 break; \
5664 default: \
5665 gcc_unreachable (); \
5666 } \
2ac2f164 5667 } while (0)
cb3ca04e 5668
49706e39
MLI
5669 /* This macro is used to emit diagnostics to ensure that all format
5670 strings are complete sentences, visible to gettext and checked at
5671 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5672 extra parameter to enumerate qualifiers. */
5673
5c1bc275 5674#define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5675 do { \
5676 switch (errtype) \
5677 { \
5678 case ic_argpass: \
5c1bc275 5679 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5680 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5681 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5682 "expected %qT but argument is of type %qT", \
5683 type, rhstype); \
5684 break; \
5685 case ic_assign: \
5c1bc275 5686 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5687 break; \
5688 case ic_init: \
5c1bc275 5689 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5690 break; \
5691 case ic_return: \
5c1bc275 5692 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5693 break; \
5694 default: \
5695 gcc_unreachable (); \
5696 } \
5697 } while (0)
5698
8ce94e44
JM
5699 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5700 rhs = TREE_OPERAND (rhs, 0);
5701
3e4093b6
RS
5702 rhstype = TREE_TYPE (rhs);
5703 coder = TREE_CODE (rhstype);
5704
5705 if (coder == ERROR_MARK)
5706 return error_mark_node;
5707
58393038
ZL
5708 if (c_dialect_objc ())
5709 {
5710 int parmno;
5711
5712 switch (errtype)
5713 {
5714 case ic_return:
5715 parmno = 0;
5716 break;
5717
5718 case ic_assign:
5719 parmno = -1;
5720 break;
5721
5722 case ic_init:
5723 parmno = -2;
5724 break;
5725
5726 default:
5727 parmno = parmnum;
5728 break;
5729 }
5730
5731 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5732 }
5733
bbbbb16a
ILT
5734 if (warn_cxx_compat)
5735 {
5736 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5737 if (checktype != error_mark_node
5738 && TREE_CODE (type) == ENUMERAL_TYPE
5739 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5740 {
5c1bc275 5741 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
81f40b79
ILT
5742 G_("enum conversion when passing argument "
5743 "%d of %qE is invalid in C++"),
5744 G_("enum conversion in assignment is "
5745 "invalid in C++"),
5746 G_("enum conversion in initialization is "
5747 "invalid in C++"),
5748 G_("enum conversion in return is "
5749 "invalid in C++"));
bbbbb16a
ILT
5750 }
5751 }
5752
3e4093b6 5753 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5754 return rhs;
3e4093b6
RS
5755
5756 if (coder == VOID_TYPE)
400fbf9f 5757 {
6dcc04b0
JM
5758 /* Except for passing an argument to an unprototyped function,
5759 this is a constraint violation. When passing an argument to
5760 an unprototyped function, it is compile-time undefined;
5761 making it a constraint in that case was rejected in
5762 DR#252. */
c2255bc4 5763 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5764 return error_mark_node;
400fbf9f 5765 }
808d6eaa
JM
5766 rhs = require_complete_type (rhs);
5767 if (rhs == error_mark_node)
5768 return error_mark_node;
cd192ccc
MS
5769 /* A non-reference type can convert to a reference. This handles
5770 va_start, va_copy and possibly port built-ins. */
5771 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5772 {
3e4093b6 5773 if (!lvalue_p (rhs))
400fbf9f 5774 {
c2255bc4 5775 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5776 return error_mark_node;
400fbf9f 5777 }
3e4093b6
RS
5778 if (!c_mark_addressable (rhs))
5779 return error_mark_node;
5780 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5781 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5782
81e5eca8 5783 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5784 build_pointer_type (TREE_TYPE (type)),
5785 rhs, origtype, errtype,
5786 null_pointer_constant, fundecl, function,
5787 parmnum);
cd192ccc
MS
5788 if (rhs == error_mark_node)
5789 return error_mark_node;
3e4093b6
RS
5790
5791 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5792 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5793 return rhs;
400fbf9f 5794 }
3e4093b6 5795 /* Some types can interconvert without explicit casts. */
3274deff 5796 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5797 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5798 return convert (type, rhs);
5799 /* Arithmetic types all interconvert, and enum is treated like int. */
5800 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5801 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5802 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5803 || codel == BOOLEAN_TYPE)
5804 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5805 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5806 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5807 || coder == BOOLEAN_TYPE))
928c19bb
JM
5808 {
5809 tree ret;
5810 bool save = in_late_binary_op;
dfe776dd 5811 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5812 in_late_binary_op = true;
81e5eca8
MP
5813 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5814 ? expr_loc : location, type, orig_rhs);
dfe776dd 5815 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5816 in_late_binary_op = save;
5817 return ret;
5818 }
400fbf9f 5819
79077aea
JJ
5820 /* Aggregates in different TUs might need conversion. */
5821 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5822 && codel == coder
5823 && comptypes (type, rhstype))
81e5eca8
MP
5824 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5825 ? expr_loc : location, type, rhs);
79077aea 5826
ebf0bf7f 5827 /* Conversion to a transparent union or record from its member types.
3e4093b6 5828 This applies only to function arguments. */
ebf0bf7f
JJ
5829 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5830 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5831 && errtype == ic_argpass)
400fbf9f 5832 {
0257e383 5833 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5834
910ad8de 5835 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5836 {
0257e383 5837 tree memb_type = TREE_TYPE (memb);
400fbf9f 5838
3e4093b6 5839 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5840 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5841 break;
e58cd767 5842
3e4093b6
RS
5843 if (TREE_CODE (memb_type) != POINTER_TYPE)
5844 continue;
2f6e4e97 5845
3e4093b6
RS
5846 if (coder == POINTER_TYPE)
5847 {
5848 tree ttl = TREE_TYPE (memb_type);
5849 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5850
3e4093b6
RS
5851 /* Any non-function converts to a [const][volatile] void *
5852 and vice versa; otherwise, targets must be the same.
5853 Meanwhile, the lhs target must have all the qualifiers of
5854 the rhs. */
267bac10
JM
5855 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5856 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5857 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5858 {
267bac10
JM
5859 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5860 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5861 /* If this type won't generate any warnings, use it. */
267bac10 5862 if (lquals == rquals
3e4093b6
RS
5863 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5864 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5865 ? ((lquals | rquals) == rquals)
5866 : ((lquals | rquals) == lquals)))
3e4093b6 5867 break;
400fbf9f 5868
3e4093b6 5869 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5870 if (!marginal_memb)
5871 marginal_memb = memb;
3e4093b6
RS
5872 }
5873 }
82bde854 5874
3e4093b6 5875 /* Can convert integer zero to any pointer type. */
928c19bb 5876 if (null_pointer_constant)
3e4093b6
RS
5877 {
5878 rhs = null_pointer_node;
5879 break;
5880 }
5881 }
400fbf9f 5882
0257e383 5883 if (memb || marginal_memb)
3e4093b6 5884 {
0257e383 5885 if (!memb)
3e4093b6
RS
5886 {
5887 /* We have only a marginally acceptable member type;
5888 it needs a warning. */
0257e383 5889 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5890 tree ttr = TREE_TYPE (rhstype);
714a0864 5891
3e4093b6
RS
5892 /* Const and volatile mean something different for function
5893 types, so the usual warnings are not appropriate. */
5894 if (TREE_CODE (ttr) == FUNCTION_TYPE
5895 && TREE_CODE (ttl) == FUNCTION_TYPE)
5896 {
5897 /* Because const and volatile on functions are
5898 restrictions that say the function will not do
5899 certain things, it is okay to use a const or volatile
5900 function where an ordinary one is wanted, but not
5901 vice-versa. */
36c5e70a
BE
5902 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5903 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5c1bc275
MP
5904 WARN_FOR_QUALIFIERS (location, expr_loc,
5905 OPT_Wdiscarded_qualifiers,
509c9d60 5906 G_("passing argument %d of %qE "
49706e39 5907 "makes %q#v qualified function "
2ac2f164 5908 "pointer from unqualified"),
49706e39 5909 G_("assignment makes %q#v qualified "
2ac2f164
JM
5910 "function pointer from "
5911 "unqualified"),
49706e39 5912 G_("initialization makes %q#v qualified "
2ac2f164
JM
5913 "function pointer from "
5914 "unqualified"),
49706e39
MLI
5915 G_("return makes %q#v qualified function "
5916 "pointer from unqualified"),
5917 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5918 }
36c5e70a
BE
5919 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5920 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5c1bc275
MP
5921 WARN_FOR_QUALIFIERS (location, expr_loc,
5922 OPT_Wdiscarded_qualifiers,
509c9d60 5923 G_("passing argument %d of %qE discards "
49706e39
MLI
5924 "%qv qualifier from pointer target type"),
5925 G_("assignment discards %qv qualifier "
2ac2f164 5926 "from pointer target type"),
49706e39 5927 G_("initialization discards %qv qualifier "
2ac2f164 5928 "from pointer target type"),
49706e39
MLI
5929 G_("return discards %qv qualifier from "
5930 "pointer target type"),
5931 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5932
5933 memb = marginal_memb;
3e4093b6 5934 }
400fbf9f 5935
fcf73884 5936 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5937 pedwarn (location, OPT_Wpedantic,
fcf73884 5938 "ISO C prohibits argument conversion to union type");
0e7c47fa 5939
db3927fb 5940 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5941 return build_constructor_single (type, memb, rhs);
3e4093b6 5942 }
0e7c47fa
RK
5943 }
5944
3e4093b6
RS
5945 /* Conversions among pointers */
5946 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5947 && (coder == codel))
400fbf9f 5948 {
3e4093b6
RS
5949 tree ttl = TREE_TYPE (type);
5950 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5951 tree mvl = ttl;
5952 tree mvr = ttr;
3e4093b6 5953 bool is_opaque_pointer;
264fa2db 5954 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5955 addr_space_t asl;
5956 addr_space_t asr;
400fbf9f 5957
46df2823 5958 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5959 mvl = (TYPE_ATOMIC (mvl)
5960 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5961 TYPE_QUAL_ATOMIC)
5962 : TYPE_MAIN_VARIANT (mvl));
46df2823 5963 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5964 mvr = (TYPE_ATOMIC (mvr)
5965 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5966 TYPE_QUAL_ATOMIC)
5967 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5968 /* Opaque pointers are treated like void pointers. */
f83c7f63 5969 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5970
478a1c5b
ILT
5971 /* The Plan 9 compiler permits a pointer to a struct to be
5972 automatically converted into a pointer to an anonymous field
5973 within the struct. */
5974 if (flag_plan9_extensions
5975 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5976 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5977 && mvl != mvr)
5978 {
5979 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5980 if (new_rhs != NULL_TREE)
5981 {
5982 rhs = new_rhs;
5983 rhstype = TREE_TYPE (rhs);
5984 coder = TREE_CODE (rhstype);
5985 ttr = TREE_TYPE (rhstype);
5986 mvr = TYPE_MAIN_VARIANT (ttr);
5987 }
5988 }
5989
b7e20b53 5990 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5991 for the purpose of reducing the number of false positives, we
5992 tolerate the special case of
b7e20b53 5993
c22cacf3 5994 int *p = NULL;
b7e20b53 5995
c22cacf3 5996 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5997 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
5998 warning_at (errtype == ic_argpass ? expr_loc : location,
5999 OPT_Wc___compat,
6000 "request for implicit conversion "
c2255bc4 6001 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6002
36c5e70a
BE
6003 /* See if the pointers point to incompatible address spaces. */
6004 asl = TYPE_ADDR_SPACE (ttl);
6005 asr = TYPE_ADDR_SPACE (ttr);
6006 if (!null_pointer_constant_p (rhs)
6007 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6008 {
6009 switch (errtype)
6010 {
6011 case ic_argpass:
8ffcdea8 6012 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6013 "non-enclosed address space", parmnum, rname);
6014 break;
6015 case ic_assign:
6016 error_at (location, "assignment from pointer to "
6017 "non-enclosed address space");
6018 break;
6019 case ic_init:
6020 error_at (location, "initialization from pointer to "
6021 "non-enclosed address space");
6022 break;
6023 case ic_return:
6024 error_at (location, "return from pointer to "
6025 "non-enclosed address space");
6026 break;
6027 default:
6028 gcc_unreachable ();
6029 }
6030 return error_mark_node;
6031 }
6032
7876a414
KG
6033 /* Check if the right-hand side has a format attribute but the
6034 left-hand side doesn't. */
90137d8f 6035 if (warn_suggest_attribute_format
104f8784 6036 && check_missing_format_attribute (type, rhstype))
c22cacf3 6037 {
104f8784
KG
6038 switch (errtype)
6039 {
6040 case ic_argpass:
8ffcdea8 6041 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6042 "argument %d of %qE might be "
6043 "a candidate for a format attribute",
6044 parmnum, rname);
104f8784
KG
6045 break;
6046 case ic_assign:
90137d8f 6047 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6048 "assignment left-hand side might be "
6049 "a candidate for a format attribute");
104f8784
KG
6050 break;
6051 case ic_init:
90137d8f 6052 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6053 "initialization left-hand side might be "
6054 "a candidate for a format attribute");
104f8784
KG
6055 break;
6056 case ic_return:
90137d8f 6057 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6058 "return type might be "
6059 "a candidate for a format attribute");
104f8784
KG
6060 break;
6061 default:
6062 gcc_unreachable ();
6063 }
7876a414 6064 }
c22cacf3 6065
3e4093b6
RS
6066 /* Any non-function converts to a [const][volatile] void *
6067 and vice versa; otherwise, targets must be the same.
6068 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6069 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6070 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6071 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6072 || is_opaque_pointer
f8a93a2e
JJ
6073 || ((c_common_unsigned_type (mvl)
6074 == c_common_unsigned_type (mvr))
267bac10
JM
6075 && (c_common_signed_type (mvl)
6076 == c_common_signed_type (mvr))
6077 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
6078 {
6079 if (pedantic
6080 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6081 ||
6082 (VOID_TYPE_P (ttr)
928c19bb 6083 && !null_pointer_constant
3e4093b6 6084 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5c1bc275 6085 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
509c9d60 6086 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
6087 "%qE between function pointer "
6088 "and %<void *%>"),
4b794eaf 6089 G_("ISO C forbids assignment between "
2ac2f164 6090 "function pointer and %<void *%>"),
4b794eaf 6091 G_("ISO C forbids initialization between "
2ac2f164 6092 "function pointer and %<void *%>"),
4b794eaf 6093 G_("ISO C forbids return between function "
2ac2f164 6094 "pointer and %<void *%>"));
3e4093b6
RS
6095 /* Const and volatile mean something different for function types,
6096 so the usual warnings are not appropriate. */
6097 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6098 && TREE_CODE (ttl) != FUNCTION_TYPE)
6099 {
267bac10
JM
6100 /* Assignments between atomic and non-atomic objects are OK. */
6101 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6102 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6103 {
5c1bc275
MP
6104 WARN_FOR_QUALIFIERS (location, expr_loc,
6105 OPT_Wdiscarded_qualifiers,
6c39e757
NP
6106 G_("passing argument %d of %qE discards "
6107 "%qv qualifier from pointer target type"),
6108 G_("assignment discards %qv qualifier "
6109 "from pointer target type"),
6110 G_("initialization discards %qv qualifier "
6111 "from pointer target type"),
6112 G_("return discards %qv qualifier from "
6113 "pointer target type"),
6114 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6115 }
3e4093b6
RS
6116 /* If this is not a case of ignoring a mismatch in signedness,
6117 no warning. */
6118 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6119 || target_cmp)
3e4093b6
RS
6120 ;
6121 /* If there is a mismatch, do warn. */
f2fd3821 6122 else if (warn_pointer_sign)
5c1bc275 6123 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
509c9d60 6124 G_("pointer targets in passing argument "
2ac2f164 6125 "%d of %qE differ in signedness"),
4b794eaf 6126 G_("pointer targets in assignment "
2ac2f164 6127 "differ in signedness"),
4b794eaf 6128 G_("pointer targets in initialization "
2ac2f164 6129 "differ in signedness"),
4b794eaf 6130 G_("pointer targets in return differ "
2ac2f164 6131 "in signedness"));
3e4093b6
RS
6132 }
6133 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6134 && TREE_CODE (ttr) == FUNCTION_TYPE)
6135 {
6136 /* Because const and volatile on functions are restrictions
6137 that say the function will not do certain things,
6138 it is okay to use a const or volatile function
6139 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6140 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6141 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5c1bc275
MP
6142 WARN_FOR_QUALIFIERS (location, expr_loc,
6143 OPT_Wdiscarded_qualifiers,
509c9d60 6144 G_("passing argument %d of %qE makes "
49706e39 6145 "%q#v qualified function pointer "
2ac2f164 6146 "from unqualified"),
49706e39 6147 G_("assignment makes %q#v qualified function "
2ac2f164 6148 "pointer from unqualified"),
49706e39 6149 G_("initialization makes %q#v qualified "
2ac2f164 6150 "function pointer from unqualified"),
49706e39
MLI
6151 G_("return makes %q#v qualified function "
6152 "pointer from unqualified"),
6153 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6154 }
6155 }
6156 else
58393038
ZL
6157 /* Avoid warning about the volatile ObjC EH puts on decls. */
6158 if (!objc_ok)
d5c3d343
MP
6159 WARN_FOR_ASSIGNMENT (location, expr_loc,
6160 OPT_Wincompatible_pointer_types,
509c9d60 6161 G_("passing argument %d of %qE from "
58393038 6162 "incompatible pointer type"),
4b794eaf
JJ
6163 G_("assignment from incompatible pointer type"),
6164 G_("initialization from incompatible "
58393038 6165 "pointer type"),
4b794eaf 6166 G_("return from incompatible pointer type"));
58393038 6167
3e4093b6
RS
6168 return convert (type, rhs);
6169 }
b494fd98
EB
6170 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6171 {
6dcc04b0
JM
6172 /* ??? This should not be an error when inlining calls to
6173 unprototyped functions. */
c2255bc4 6174 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6175 return error_mark_node;
6176 }
3e4093b6 6177 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6178 {
3e4093b6
RS
6179 /* An explicit constant 0 can convert to a pointer,
6180 or one that results from arithmetic, even including
6181 a cast to integer type. */
928c19bb 6182 if (!null_pointer_constant)
52ec0ea3
MP
6183 WARN_FOR_ASSIGNMENT (location, expr_loc,
6184 OPT_Wint_conversion,
509c9d60 6185 G_("passing argument %d of %qE makes "
2ac2f164 6186 "pointer from integer without a cast"),
4b794eaf 6187 G_("assignment makes pointer from integer "
2ac2f164 6188 "without a cast"),
4b794eaf 6189 G_("initialization makes pointer from "
2ac2f164 6190 "integer without a cast"),
4b794eaf 6191 G_("return makes pointer from integer "
2ac2f164 6192 "without a cast"));
b3006337
EB
6193
6194 return convert (type, rhs);
400fbf9f 6195 }
3e4093b6 6196 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6197 {
52ec0ea3
MP
6198 WARN_FOR_ASSIGNMENT (location, expr_loc,
6199 OPT_Wint_conversion,
509c9d60 6200 G_("passing argument %d of %qE makes integer "
2ac2f164 6201 "from pointer without a cast"),
4b794eaf 6202 G_("assignment makes integer from pointer "
2ac2f164 6203 "without a cast"),
4b794eaf 6204 G_("initialization makes integer from pointer "
2ac2f164 6205 "without a cast"),
4b794eaf 6206 G_("return makes integer from pointer "
2ac2f164 6207 "without a cast"));
3e4093b6
RS
6208 return convert (type, rhs);
6209 }
6210 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6211 {
6212 tree ret;
6213 bool save = in_late_binary_op;
6214 in_late_binary_op = true;
6215 ret = convert (type, rhs);
6216 in_late_binary_op = save;
6217 return ret;
6218 }
400fbf9f 6219
2ac2f164 6220 switch (errtype)
3e4093b6 6221 {
2ac2f164 6222 case ic_argpass:
8ffcdea8
MP
6223 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6224 rname);
a7da8b42 6225 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6226 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6227 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6228 break;
6229 case ic_assign:
c2255bc4
AH
6230 error_at (location, "incompatible types when assigning to type %qT from "
6231 "type %qT", type, rhstype);
2ac2f164
JM
6232 break;
6233 case ic_init:
c2255bc4 6234 error_at (location,
8ffcdea8 6235 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6236 type, rhstype);
2ac2f164
JM
6237 break;
6238 case ic_return:
c2255bc4 6239 error_at (location,
8ffcdea8 6240 "incompatible types when returning type %qT but %qT was "
c2255bc4 6241 "expected", rhstype, type);
2ac2f164
JM
6242 break;
6243 default:
6244 gcc_unreachable ();
400fbf9f 6245 }
53b01f59 6246
3e4093b6
RS
6247 return error_mark_node;
6248}
3e4093b6
RS
6249\f
6250/* If VALUE is a compound expr all of whose expressions are constant, then
6251 return its value. Otherwise, return error_mark_node.
15b732b2 6252
3e4093b6
RS
6253 This is for handling COMPOUND_EXPRs as initializer elements
6254 which is allowed with a warning when -pedantic is specified. */
15b732b2 6255
3e4093b6
RS
6256static tree
6257valid_compound_expr_initializer (tree value, tree endtype)
6258{
6259 if (TREE_CODE (value) == COMPOUND_EXPR)
6260 {
6261 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6262 == error_mark_node)
6263 return error_mark_node;
6264 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6265 endtype);
6266 }
116df786 6267 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6268 return error_mark_node;
6269 else
6270 return value;
15b732b2 6271}
400fbf9f 6272\f
3e4093b6
RS
6273/* Perform appropriate conversions on the initial value of a variable,
6274 store it in the declaration DECL,
6275 and print any error messages that are appropriate.
bbbbb16a 6276 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6277 If the init is invalid, store an ERROR_MARK.
6278
6279 INIT_LOC is the location of the initial value. */
400fbf9f 6280
3e4093b6 6281void
c2255bc4 6282store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6283{
3e4093b6 6284 tree value, type;
928c19bb 6285 bool npc = false;
400fbf9f 6286
3e4093b6 6287 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6288
3e4093b6
RS
6289 type = TREE_TYPE (decl);
6290 if (TREE_CODE (type) == ERROR_MARK)
6291 return;
400fbf9f 6292
3e4093b6 6293 /* Digest the specified initializer into an expression. */
400fbf9f 6294
928c19bb
JM
6295 if (init)
6296 npc = null_pointer_constant_p (init);
c2255bc4
AH
6297 value = digest_init (init_loc, type, init, origtype, npc,
6298 true, TREE_STATIC (decl));
400fbf9f 6299
3e4093b6 6300 /* Store the expression if valid; else report error. */
400fbf9f 6301
8400e75e 6302 if (!in_system_header_at (input_location)
3f75a254 6303 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6304 warning (OPT_Wtraditional, "traditional C rejects automatic "
6305 "aggregate initialization");
2f6e4e97 6306
3e4093b6 6307 DECL_INITIAL (decl) = value;
400fbf9f 6308
3e4093b6
RS
6309 /* ANSI wants warnings about out-of-range constant initializers. */
6310 STRIP_TYPE_NOPS (value);
b8698a0f 6311 if (TREE_STATIC (decl))
c2658540 6312 constant_expression_warning (value);
400fbf9f 6313
3e4093b6
RS
6314 /* Check if we need to set array size from compound literal size. */
6315 if (TREE_CODE (type) == ARRAY_TYPE
6316 && TYPE_DOMAIN (type) == 0
6317 && value != error_mark_node)
400fbf9f 6318 {
3e4093b6
RS
6319 tree inside_init = init;
6320
ed248cf7 6321 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6322 inside_init = fold (inside_init);
6323
6324 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6325 {
8d9f82d5 6326 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6327
8d9f82d5 6328 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6329 {
6330 /* For int foo[] = (int [3]){1}; we need to set array size
6331 now since later on array initializer will be just the
6332 brace enclosed list of the compound literal. */
e30ecc5d 6333 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6334 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6335 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6336 layout_type (type);
8d9f82d5 6337 layout_decl (cldecl, 0);
e30ecc5d
JJ
6338 TREE_TYPE (decl)
6339 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6340 }
6341 }
400fbf9f 6342 }
3e4093b6
RS
6343}
6344\f
6345/* Methods for storing and printing names for error messages. */
400fbf9f 6346
3e4093b6
RS
6347/* Implement a spelling stack that allows components of a name to be pushed
6348 and popped. Each element on the stack is this structure. */
400fbf9f 6349
3e4093b6
RS
6350struct spelling
6351{
6352 int kind;
6353 union
400fbf9f 6354 {
a0f0ab9f 6355 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6356 const char *s;
6357 } u;
6358};
2f6e4e97 6359
3e4093b6
RS
6360#define SPELLING_STRING 1
6361#define SPELLING_MEMBER 2
6362#define SPELLING_BOUNDS 3
400fbf9f 6363
3e4093b6
RS
6364static struct spelling *spelling; /* Next stack element (unused). */
6365static struct spelling *spelling_base; /* Spelling stack base. */
6366static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6367
3e4093b6
RS
6368/* Macros to save and restore the spelling stack around push_... functions.
6369 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6370
3e4093b6
RS
6371#define SPELLING_DEPTH() (spelling - spelling_base)
6372#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6373
3e4093b6
RS
6374/* Push an element on the spelling stack with type KIND and assign VALUE
6375 to MEMBER. */
400fbf9f 6376
3e4093b6
RS
6377#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6378{ \
6379 int depth = SPELLING_DEPTH (); \
6380 \
6381 if (depth >= spelling_size) \
6382 { \
6383 spelling_size += 10; \
cca8ead2
BI
6384 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6385 spelling_size); \
3e4093b6
RS
6386 RESTORE_SPELLING_DEPTH (depth); \
6387 } \
6388 \
6389 spelling->kind = (KIND); \
6390 spelling->MEMBER = (VALUE); \
6391 spelling++; \
6392}
400fbf9f 6393
3e4093b6 6394/* Push STRING on the stack. Printed literally. */
400fbf9f 6395
3e4093b6
RS
6396static void
6397push_string (const char *string)
6398{
6399 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6400}
400fbf9f 6401
3e4093b6 6402/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6403
3e4093b6
RS
6404static void
6405push_member_name (tree decl)
6406{
6407 const char *const string
88388a52
JM
6408 = (DECL_NAME (decl)
6409 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6410 : _("<anonymous>"));
3e4093b6
RS
6411 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6412}
400fbf9f 6413
3e4093b6 6414/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6415
3e4093b6 6416static void
a0f0ab9f 6417push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6418{
6419 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6420}
bb58bec5 6421
3e4093b6 6422/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6423
3e4093b6
RS
6424static int
6425spelling_length (void)
6426{
6427 int size = 0;
6428 struct spelling *p;
400fbf9f 6429
3e4093b6
RS
6430 for (p = spelling_base; p < spelling; p++)
6431 {
6432 if (p->kind == SPELLING_BOUNDS)
6433 size += 25;
6434 else
6435 size += strlen (p->u.s) + 1;
6436 }
6437
6438 return size;
400fbf9f 6439}
400fbf9f 6440
3e4093b6 6441/* Print the spelling to BUFFER and return it. */
400fbf9f 6442
3e4093b6
RS
6443static char *
6444print_spelling (char *buffer)
400fbf9f 6445{
3e4093b6
RS
6446 char *d = buffer;
6447 struct spelling *p;
400fbf9f 6448
3e4093b6
RS
6449 for (p = spelling_base; p < spelling; p++)
6450 if (p->kind == SPELLING_BOUNDS)
6451 {
a0f0ab9f 6452 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6453 d += strlen (d);
6454 }
6455 else
6456 {
6457 const char *s;
6458 if (p->kind == SPELLING_MEMBER)
6459 *d++ = '.';
6460 for (s = p->u.s; (*d = *s++); d++)
6461 ;
6462 }
6463 *d++ = '\0';
6464 return buffer;
6465}
400fbf9f 6466
3e4093b6
RS
6467/* Digest the parser output INIT as an initializer for type TYPE.
6468 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6469
bbbbb16a
ILT
6470 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6471
928c19bb
JM
6472 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6473
916c5919
JM
6474 If INIT is a string constant, STRICT_STRING is true if it is
6475 unparenthesized or we should not warn here for it being parenthesized.
6476 For other types of INIT, STRICT_STRING is not used.
6477
c2255bc4
AH
6478 INIT_LOC is the location of the INIT.
6479
3e4093b6
RS
6480 REQUIRE_CONSTANT requests an error if non-constant initializers or
6481 elements are seen. */
7e842ef8 6482
3e4093b6 6483static tree
c2255bc4
AH
6484digest_init (location_t init_loc, tree type, tree init, tree origtype,
6485 bool null_pointer_constant, bool strict_string,
6486 int require_constant)
3e4093b6
RS
6487{
6488 enum tree_code code = TREE_CODE (type);
6489 tree inside_init = init;
8ce94e44 6490 tree semantic_type = NULL_TREE;
928c19bb 6491 bool maybe_const = true;
7e842ef8 6492
3e4093b6 6493 if (type == error_mark_node
f01da1a5 6494 || !init
3e4093b6
RS
6495 || init == error_mark_node
6496 || TREE_TYPE (init) == error_mark_node)
6497 return error_mark_node;
7e842ef8 6498
ed248cf7 6499 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6500
8ce94e44
JM
6501 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6502 {
6503 semantic_type = TREE_TYPE (inside_init);
6504 inside_init = TREE_OPERAND (inside_init, 0);
6505 }
928c19bb
JM
6506 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6507 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6508
3e4093b6
RS
6509 /* Initialization of an array of chars from a string constant
6510 optionally enclosed in braces. */
7e842ef8 6511
197463ae
JM
6512 if (code == ARRAY_TYPE && inside_init
6513 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6514 {
267bac10
JM
6515 tree typ1
6516 = (TYPE_ATOMIC (TREE_TYPE (type))
6517 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6518 TYPE_QUAL_ATOMIC)
6519 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6520 /* Note that an array could be both an array of character type
6521 and an array of wchar_t if wchar_t is signed char or unsigned
6522 char. */
6523 bool char_array = (typ1 == char_type_node
6524 || typ1 == signed_char_type_node
6525 || typ1 == unsigned_char_type_node);
6526 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6527 bool char16_array = !!comptypes (typ1, char16_type_node);
6528 bool char32_array = !!comptypes (typ1, char32_type_node);
6529
6530 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6531 {
916c5919 6532 struct c_expr expr;
c466b2cd 6533 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6534 expr.value = inside_init;
6535 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6536 expr.original_type = NULL;
d033409e 6537 maybe_warn_string_init (init_loc, type, expr);
916c5919 6538
a45e580b 6539 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6540 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6541 "initialization of a flexible array member");
6542
3e4093b6 6543 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6544 TYPE_MAIN_VARIANT (type)))
3e4093b6 6545 return inside_init;
7e842ef8 6546
c466b2cd 6547 if (char_array)
3e4093b6 6548 {
c466b2cd
KVH
6549 if (typ2 != char_type_node)
6550 {
ea58ef42
MP
6551 error_init (init_loc, "char-array initialized from wide "
6552 "string");
c466b2cd
KVH
6553 return error_mark_node;
6554 }
3e4093b6 6555 }
c466b2cd 6556 else
3e4093b6 6557 {
c466b2cd
KVH
6558 if (typ2 == char_type_node)
6559 {
ea58ef42
MP
6560 error_init (init_loc, "wide character array initialized "
6561 "from non-wide string");
c466b2cd
KVH
6562 return error_mark_node;
6563 }
6564 else if (!comptypes(typ1, typ2))
6565 {
ea58ef42
MP
6566 error_init (init_loc, "wide character array initialized "
6567 "from incompatible wide string");
c466b2cd
KVH
6568 return error_mark_node;
6569 }
7e842ef8 6570 }
2f6e4e97 6571
3e4093b6
RS
6572 TREE_TYPE (inside_init) = type;
6573 if (TYPE_DOMAIN (type) != 0
6574 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6575 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6576 {
6577 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6578
c466b2cd 6579 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6580 because it's ok to ignore the terminating null char
6581 that is counted in the length of the constant. */
5eb4df45
ILT
6582 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6583 (len
6584 - (TYPE_PRECISION (typ1)
6585 / BITS_PER_UNIT))))
6586 pedwarn_init (init_loc, 0,
6587 ("initializer-string for array of chars "
6588 "is too long"));
6589 else if (warn_cxx_compat
6590 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6591 warning_at (init_loc, OPT_Wc___compat,
6592 ("initializer-string for array chars "
6593 "is too long for C++"));
6594 }
7e842ef8 6595
3e4093b6 6596 return inside_init;
7e842ef8 6597 }
197463ae
JM
6598 else if (INTEGRAL_TYPE_P (typ1))
6599 {
ea58ef42 6600 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6601 "from string constant");
6602 return error_mark_node;
6603 }
7e842ef8
PE
6604 }
6605
3e4093b6
RS
6606 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6607 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6608 below and handle as a constructor. */
e89be13b
JJ
6609 if (code == VECTOR_TYPE
6610 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6611 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6612 && TREE_CONSTANT (inside_init))
6613 {
6614 if (TREE_CODE (inside_init) == VECTOR_CST
6615 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6616 TYPE_MAIN_VARIANT (type)))
6617 return inside_init;
6618
6619 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6620 {
4038c495
GB
6621 unsigned HOST_WIDE_INT ix;
6622 tree value;
6623 bool constant_p = true;
e89be13b
JJ
6624
6625 /* Iterate through elements and check if all constructor
6626 elements are *_CSTs. */
4038c495
GB
6627 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6628 if (!CONSTANT_CLASS_P (value))
6629 {
6630 constant_p = false;
6631 break;
6632 }
e89be13b 6633
4038c495
GB
6634 if (constant_p)
6635 return build_vector_from_ctor (type,
6636 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6637 }
6638 }
6035d635 6639
ca085fd7
MLI
6640 if (warn_sequence_point)
6641 verify_sequence_points (inside_init);
6642
3e4093b6
RS
6643 /* Any type can be initialized
6644 from an expression of the same type, optionally with braces. */
400fbf9f 6645
3e4093b6
RS
6646 if (inside_init && TREE_TYPE (inside_init) != 0
6647 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6648 TYPE_MAIN_VARIANT (type))
3e4093b6 6649 || (code == ARRAY_TYPE
132da1a5 6650 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6651 || (code == VECTOR_TYPE
132da1a5 6652 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6653 || (code == POINTER_TYPE
3897f229 6654 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6655 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6656 TREE_TYPE (type)))))
3e4093b6
RS
6657 {
6658 if (code == POINTER_TYPE)
b494fd98 6659 {
b494fd98
EB
6660 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6661 {
f2a71bbc
JM
6662 if (TREE_CODE (inside_init) == STRING_CST
6663 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6664 inside_init = array_to_pointer_conversion
6665 (init_loc, inside_init);
f2a71bbc
JM
6666 else
6667 {
ea58ef42 6668 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6669 return error_mark_node;
6670 }
b494fd98 6671 }
f2a71bbc 6672 }
b494fd98 6673
bae39a73
NS
6674 if (code == VECTOR_TYPE)
6675 /* Although the types are compatible, we may require a
6676 conversion. */
6677 inside_init = convert (type, inside_init);
3e4093b6 6678
ca58211b
PB
6679 if (require_constant
6680 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6681 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6682 {
3e4093b6
RS
6683 /* As an extension, allow initializing objects with static storage
6684 duration with compound literals (which are then treated just as
ca58211b
PB
6685 the brace enclosed list they contain). Also allow this for
6686 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6687 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6688 inside_init = DECL_INITIAL (decl);
400fbf9f 6689 }
3e4093b6
RS
6690
6691 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6692 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6693 {
ea58ef42
MP
6694 error_init (init_loc, "array initialized from non-constant array "
6695 "expression");
3e4093b6 6696 return error_mark_node;
400fbf9f 6697 }
400fbf9f 6698
c1771a20 6699 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6700 -pedantic-errors is specified. In the later case, we always want
6701 an error. In the former case, we simply want a warning. */
6702 if (require_constant && pedantic
6703 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6704 {
6705 inside_init
6706 = valid_compound_expr_initializer (inside_init,
6707 TREE_TYPE (inside_init));
6708 if (inside_init == error_mark_node)
ea58ef42 6709 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6710 else
c1771a20 6711 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6712 "initializer element is not constant");
3e4093b6
RS
6713 if (flag_pedantic_errors)
6714 inside_init = error_mark_node;
6715 }
6716 else if (require_constant
116df786
RH
6717 && !initializer_constant_valid_p (inside_init,
6718 TREE_TYPE (inside_init)))
3e4093b6 6719 {
ea58ef42 6720 error_init (init_loc, "initializer element is not constant");
3e4093b6 6721 inside_init = error_mark_node;
8b40563c 6722 }
928c19bb 6723 else if (require_constant && !maybe_const)
c2255bc4 6724 pedwarn_init (init_loc, 0,
928c19bb 6725 "initializer element is not a constant expression");
f735a153 6726
90137d8f 6727 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6728 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6729 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6730 type, inside_init, origtype,
bbbbb16a 6731 ic_init, null_pointer_constant,
928c19bb 6732 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6733 return inside_init;
6734 }
f735a153 6735
3e4093b6 6736 /* Handle scalar types, including conversions. */
400fbf9f 6737
ab22c1fa
CF
6738 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6739 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6740 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6741 {
f2a71bbc
JM
6742 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6743 && (TREE_CODE (init) == STRING_CST
6744 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6745 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6746 if (semantic_type)
6747 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6748 inside_init);
3e4093b6 6749 inside_init
68fca595
MP
6750 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6751 inside_init, origtype, ic_init,
6752 null_pointer_constant, NULL_TREE, NULL_TREE,
6753 0);
2f6e4e97 6754
3274deff
JW
6755 /* Check to see if we have already given an error message. */
6756 if (inside_init == error_mark_node)
6757 ;
3f75a254 6758 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6759 {
ea58ef42 6760 error_init (init_loc, "initializer element is not constant");
3e4093b6 6761 inside_init = error_mark_node;
400fbf9f 6762 }
3e4093b6 6763 else if (require_constant
116df786
RH
6764 && !initializer_constant_valid_p (inside_init,
6765 TREE_TYPE (inside_init)))
400fbf9f 6766 {
ea58ef42
MP
6767 error_init (init_loc, "initializer element is not computable at "
6768 "load time");
3e4093b6 6769 inside_init = error_mark_node;
400fbf9f 6770 }
928c19bb 6771 else if (require_constant && !maybe_const)
c2255bc4 6772 pedwarn_init (init_loc, 0,
928c19bb 6773 "initializer element is not a constant expression");
3e4093b6
RS
6774
6775 return inside_init;
400fbf9f 6776 }
d9fc6069 6777
3e4093b6 6778 /* Come here only for records and arrays. */
d9fc6069 6779
3e4093b6 6780 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6781 {
ea58ef42 6782 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6783 return error_mark_node;
d9fc6069 6784 }
3e4093b6 6785
ea58ef42 6786 error_init (init_loc, "invalid initializer");
3e4093b6 6787 return error_mark_node;
d9fc6069 6788}
400fbf9f 6789\f
3e4093b6 6790/* Handle initializers that use braces. */
400fbf9f 6791
3e4093b6
RS
6792/* Type of object we are accumulating a constructor for.
6793 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6794static tree constructor_type;
400fbf9f 6795
3e4093b6
RS
6796/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6797 left to fill. */
6798static tree constructor_fields;
400fbf9f 6799
3e4093b6
RS
6800/* For an ARRAY_TYPE, this is the specified index
6801 at which to store the next element we get. */
6802static tree constructor_index;
400fbf9f 6803
3e4093b6
RS
6804/* For an ARRAY_TYPE, this is the maximum index. */
6805static tree constructor_max_index;
400fbf9f 6806
3e4093b6
RS
6807/* For a RECORD_TYPE, this is the first field not yet written out. */
6808static tree constructor_unfilled_fields;
400fbf9f 6809
3e4093b6
RS
6810/* For an ARRAY_TYPE, this is the index of the first element
6811 not yet written out. */
6812static tree constructor_unfilled_index;
895ea614 6813
3e4093b6
RS
6814/* In a RECORD_TYPE, the byte index of the next consecutive field.
6815 This is so we can generate gaps between fields, when appropriate. */
6816static tree constructor_bit_index;
10d5caec 6817
3e4093b6
RS
6818/* If we are saving up the elements rather than allocating them,
6819 this is the list of elements so far (in reverse order,
6820 most recent first). */
9771b263 6821static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6822
3e4093b6
RS
6823/* 1 if constructor should be incrementally stored into a constructor chain,
6824 0 if all the elements should be kept in AVL tree. */
6825static int constructor_incremental;
ad47f1e5 6826
3e4093b6
RS
6827/* 1 if so far this constructor's elements are all compile-time constants. */
6828static int constructor_constant;
ad47f1e5 6829
3e4093b6
RS
6830/* 1 if so far this constructor's elements are all valid address constants. */
6831static int constructor_simple;
ad47f1e5 6832
928c19bb
JM
6833/* 1 if this constructor has an element that cannot be part of a
6834 constant expression. */
6835static int constructor_nonconst;
6836
3e4093b6
RS
6837/* 1 if this constructor is erroneous so far. */
6838static int constructor_erroneous;
d45cf215 6839
9bac5cbb
G
6840/* 1 if this constructor is the universal zero initializer { 0 }. */
6841static int constructor_zeroinit;
6842
3e4093b6
RS
6843/* Structure for managing pending initializer elements, organized as an
6844 AVL tree. */
d45cf215 6845
3e4093b6 6846struct init_node
d45cf215 6847{
3e4093b6
RS
6848 struct init_node *left, *right;
6849 struct init_node *parent;
6850 int balance;
6851 tree purpose;
6852 tree value;
bbbbb16a 6853 tree origtype;
d45cf215
RS
6854};
6855
3e4093b6
RS
6856/* Tree of pending elements at this constructor level.
6857 These are elements encountered out of order
6858 which belong at places we haven't reached yet in actually
6859 writing the output.
6860 Will never hold tree nodes across GC runs. */
6861static struct init_node *constructor_pending_elts;
d45cf215 6862
3e4093b6
RS
6863/* The SPELLING_DEPTH of this constructor. */
6864static int constructor_depth;
d45cf215 6865
3e4093b6
RS
6866/* DECL node for which an initializer is being read.
6867 0 means we are reading a constructor expression
6868 such as (struct foo) {...}. */
6869static tree constructor_decl;
d45cf215 6870
3e4093b6
RS
6871/* Nonzero if this is an initializer for a top-level decl. */
6872static int constructor_top_level;
d45cf215 6873
3e4093b6
RS
6874/* Nonzero if there were any member designators in this initializer. */
6875static int constructor_designated;
d45cf215 6876
3e4093b6
RS
6877/* Nesting depth of designator list. */
6878static int designator_depth;
d45cf215 6879
3e4093b6 6880/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6881static int designator_erroneous;
d45cf215 6882
3e4093b6
RS
6883\f
6884/* This stack has a level for each implicit or explicit level of
6885 structuring in the initializer, including the outermost one. It
6886 saves the values of most of the variables above. */
d45cf215 6887
3e4093b6
RS
6888struct constructor_range_stack;
6889
6890struct constructor_stack
d45cf215 6891{
3e4093b6
RS
6892 struct constructor_stack *next;
6893 tree type;
6894 tree fields;
6895 tree index;
6896 tree max_index;
6897 tree unfilled_index;
6898 tree unfilled_fields;
6899 tree bit_index;
9771b263 6900 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6901 struct init_node *pending_elts;
6902 int offset;
6903 int depth;
916c5919 6904 /* If value nonzero, this value should replace the entire
3e4093b6 6905 constructor at this level. */
916c5919 6906 struct c_expr replacement_value;
3e4093b6
RS
6907 struct constructor_range_stack *range_stack;
6908 char constant;
6909 char simple;
928c19bb 6910 char nonconst;
3e4093b6
RS
6911 char implicit;
6912 char erroneous;
6913 char outer;
6914 char incremental;
6915 char designated;
976d5a22 6916 int designator_depth;
3e4093b6 6917};
d45cf215 6918
802415d1 6919static struct constructor_stack *constructor_stack;
d45cf215 6920
3e4093b6
RS
6921/* This stack represents designators from some range designator up to
6922 the last designator in the list. */
d45cf215 6923
3e4093b6
RS
6924struct constructor_range_stack
6925{
6926 struct constructor_range_stack *next, *prev;
6927 struct constructor_stack *stack;
6928 tree range_start;
6929 tree index;
6930 tree range_end;
6931 tree fields;
6932};
d45cf215 6933
802415d1 6934static struct constructor_range_stack *constructor_range_stack;
d45cf215 6935
3e4093b6
RS
6936/* This stack records separate initializers that are nested.
6937 Nested initializers can't happen in ANSI C, but GNU C allows them
6938 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6939
3e4093b6 6940struct initializer_stack
d45cf215 6941{
3e4093b6
RS
6942 struct initializer_stack *next;
6943 tree decl;
3e4093b6
RS
6944 struct constructor_stack *constructor_stack;
6945 struct constructor_range_stack *constructor_range_stack;
9771b263 6946 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6947 struct spelling *spelling;
6948 struct spelling *spelling_base;
6949 int spelling_size;
6950 char top_level;
6951 char require_constant_value;
6952 char require_constant_elements;
6953};
d45cf215 6954
802415d1 6955static struct initializer_stack *initializer_stack;
3e4093b6
RS
6956\f
6957/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6958
6959void
a396f8ae 6960start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6961{
3e4093b6 6962 const char *locus;
cceb1885 6963 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6964
3e4093b6 6965 p->decl = constructor_decl;
3e4093b6
RS
6966 p->require_constant_value = require_constant_value;
6967 p->require_constant_elements = require_constant_elements;
6968 p->constructor_stack = constructor_stack;
6969 p->constructor_range_stack = constructor_range_stack;
6970 p->elements = constructor_elements;
6971 p->spelling = spelling;
6972 p->spelling_base = spelling_base;
6973 p->spelling_size = spelling_size;
6974 p->top_level = constructor_top_level;
6975 p->next = initializer_stack;
6976 initializer_stack = p;
400fbf9f 6977
3e4093b6 6978 constructor_decl = decl;
3e4093b6
RS
6979 constructor_designated = 0;
6980 constructor_top_level = top_level;
400fbf9f 6981
6f17bbcf 6982 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6983 {
6984 require_constant_value = TREE_STATIC (decl);
6985 require_constant_elements
6986 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6987 /* For a scalar, you can always use any value to initialize,
6988 even within braces. */
6989 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6990 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6991 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6992 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6993 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6994 }
6995 else
6996 {
6997 require_constant_value = 0;
6998 require_constant_elements = 0;
88388a52 6999 locus = _("(anonymous)");
3e4093b6 7000 }
b71c7f8a 7001
3e4093b6
RS
7002 constructor_stack = 0;
7003 constructor_range_stack = 0;
b71c7f8a 7004
9bac5cbb 7005 found_missing_braces = 0;
3e4093b6
RS
7006
7007 spelling_base = 0;
7008 spelling_size = 0;
7009 RESTORE_SPELLING_DEPTH (0);
7010
7011 if (locus)
7012 push_string (locus);
7013}
7014
7015void
7016finish_init (void)
b71c7f8a 7017{
3e4093b6 7018 struct initializer_stack *p = initializer_stack;
b71c7f8a 7019
3e4093b6
RS
7020 /* Free the whole constructor stack of this initializer. */
7021 while (constructor_stack)
7022 {
7023 struct constructor_stack *q = constructor_stack;
7024 constructor_stack = q->next;
7025 free (q);
7026 }
7027
366de0ce 7028 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7029
7030 /* Pop back to the data of the outer initializer (if any). */
36579663 7031 free (spelling_base);
3aeb3655 7032
3e4093b6 7033 constructor_decl = p->decl;
3e4093b6
RS
7034 require_constant_value = p->require_constant_value;
7035 require_constant_elements = p->require_constant_elements;
7036 constructor_stack = p->constructor_stack;
7037 constructor_range_stack = p->constructor_range_stack;
7038 constructor_elements = p->elements;
7039 spelling = p->spelling;
7040 spelling_base = p->spelling_base;
7041 spelling_size = p->spelling_size;
7042 constructor_top_level = p->top_level;
7043 initializer_stack = p->next;
7044 free (p);
b71c7f8a 7045}
400fbf9f 7046\f
3e4093b6
RS
7047/* Call here when we see the initializer is surrounded by braces.
7048 This is instead of a call to push_init_level;
7049 it is matched by a call to pop_init_level.
400fbf9f 7050
3e4093b6
RS
7051 TYPE is the type to initialize, for a constructor expression.
7052 For an initializer for a decl, TYPE is zero. */
400fbf9f 7053
3e4093b6
RS
7054void
7055really_start_incremental_init (tree type)
400fbf9f 7056{
5d038c4c 7057 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7058
3e4093b6
RS
7059 if (type == 0)
7060 type = TREE_TYPE (constructor_decl);
400fbf9f 7061
b6fc2cdb
PB
7062 if (TREE_CODE (type) == VECTOR_TYPE
7063 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7064 error ("opaque vector types cannot be initialized");
400fbf9f 7065
3e4093b6
RS
7066 p->type = constructor_type;
7067 p->fields = constructor_fields;
7068 p->index = constructor_index;
7069 p->max_index = constructor_max_index;
7070 p->unfilled_index = constructor_unfilled_index;
7071 p->unfilled_fields = constructor_unfilled_fields;
7072 p->bit_index = constructor_bit_index;
7073 p->elements = constructor_elements;
7074 p->constant = constructor_constant;
7075 p->simple = constructor_simple;
928c19bb 7076 p->nonconst = constructor_nonconst;
3e4093b6
RS
7077 p->erroneous = constructor_erroneous;
7078 p->pending_elts = constructor_pending_elts;
7079 p->depth = constructor_depth;
916c5919
JM
7080 p->replacement_value.value = 0;
7081 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7082 p->replacement_value.original_type = NULL;
3e4093b6
RS
7083 p->implicit = 0;
7084 p->range_stack = 0;
7085 p->outer = 0;
7086 p->incremental = constructor_incremental;
7087 p->designated = constructor_designated;
976d5a22 7088 p->designator_depth = designator_depth;
3e4093b6
RS
7089 p->next = 0;
7090 constructor_stack = p;
b13aca19 7091
3e4093b6
RS
7092 constructor_constant = 1;
7093 constructor_simple = 1;
928c19bb 7094 constructor_nonconst = 0;
3e4093b6 7095 constructor_depth = SPELLING_DEPTH ();
9771b263 7096 constructor_elements = NULL;
3e4093b6
RS
7097 constructor_pending_elts = 0;
7098 constructor_type = type;
7099 constructor_incremental = 1;
7100 constructor_designated = 0;
9bac5cbb 7101 constructor_zeroinit = 1;
3e4093b6 7102 designator_depth = 0;
b06df647 7103 designator_erroneous = 0;
400fbf9f 7104
3e4093b6
RS
7105 if (TREE_CODE (constructor_type) == RECORD_TYPE
7106 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7107 {
3e4093b6
RS
7108 constructor_fields = TYPE_FIELDS (constructor_type);
7109 /* Skip any nameless bit fields at the beginning. */
7110 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7111 && DECL_NAME (constructor_fields) == 0)
910ad8de 7112 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7113
3e4093b6
RS
7114 constructor_unfilled_fields = constructor_fields;
7115 constructor_bit_index = bitsize_zero_node;
400fbf9f 7116 }
3e4093b6
RS
7117 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7118 {
7119 if (TYPE_DOMAIN (constructor_type))
7120 {
7121 constructor_max_index
7122 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7123
3e4093b6
RS
7124 /* Detect non-empty initializations of zero-length arrays. */
7125 if (constructor_max_index == NULL_TREE
7126 && TYPE_SIZE (constructor_type))
9a9d280e 7127 constructor_max_index = integer_minus_one_node;
400fbf9f 7128
3e4093b6
RS
7129 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7130 to initialize VLAs will cause a proper error; avoid tree
7131 checking errors as well by setting a safe value. */
7132 if (constructor_max_index
7133 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7134 constructor_max_index = integer_minus_one_node;
59c83dbf 7135
3e4093b6
RS
7136 constructor_index
7137 = convert (bitsizetype,
7138 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7139 }
3e4093b6 7140 else
493179da
JM
7141 {
7142 constructor_index = bitsize_zero_node;
7143 constructor_max_index = NULL_TREE;
7144 }
59c83dbf 7145
3e4093b6
RS
7146 constructor_unfilled_index = constructor_index;
7147 }
7148 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7149 {
7150 /* Vectors are like simple fixed-size arrays. */
7151 constructor_max_index =
c62c040f 7152 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7153 constructor_index = bitsize_zero_node;
3e4093b6
RS
7154 constructor_unfilled_index = constructor_index;
7155 }
7156 else
7157 {
7158 /* Handle the case of int x = {5}; */
7159 constructor_fields = constructor_type;
7160 constructor_unfilled_fields = constructor_type;
7161 }
7162}
7163\f
7164/* Push down into a subobject, for initialization.
7165 If this is for an explicit set of braces, IMPLICIT is 0.
7166 If it is because the next element belongs at a lower level,
7167 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7168
3e4093b6 7169void
ea58ef42
MP
7170push_init_level (location_t loc, int implicit,
7171 struct obstack *braced_init_obstack)
3e4093b6
RS
7172{
7173 struct constructor_stack *p;
7174 tree value = NULL_TREE;
400fbf9f 7175
3e4093b6 7176 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7177 pop them now. If implicit == 1, this will have been done in
7178 process_init_element; do not repeat it here because in the case
7179 of excess initializers for an empty aggregate this leads to an
7180 infinite cycle of popping a level and immediately recreating
7181 it. */
7182 if (implicit != 1)
3e4093b6 7183 {
472d98b4
JM
7184 while (constructor_stack->implicit)
7185 {
7186 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7187 || TREE_CODE (constructor_type) == UNION_TYPE)
7188 && constructor_fields == 0)
34cf811f 7189 process_init_element (input_location,
ea58ef42 7190 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7191 true, braced_init_obstack);
472d98b4
JM
7192 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7193 && constructor_max_index
7194 && tree_int_cst_lt (constructor_max_index,
7195 constructor_index))
34cf811f 7196 process_init_element (input_location,
ea58ef42 7197 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7198 true, braced_init_obstack);
472d98b4
JM
7199 else
7200 break;
7201 }
3e4093b6 7202 }
400fbf9f 7203
3e4093b6
RS
7204 /* Unless this is an explicit brace, we need to preserve previous
7205 content if any. */
7206 if (implicit)
7207 {
7208 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7209 || TREE_CODE (constructor_type) == UNION_TYPE)
7210 && constructor_fields)
a1e3b3d9 7211 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7212 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7213 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7214 }
7215
5d038c4c 7216 p = XNEW (struct constructor_stack);
3e4093b6
RS
7217 p->type = constructor_type;
7218 p->fields = constructor_fields;
7219 p->index = constructor_index;
7220 p->max_index = constructor_max_index;
7221 p->unfilled_index = constructor_unfilled_index;
7222 p->unfilled_fields = constructor_unfilled_fields;
7223 p->bit_index = constructor_bit_index;
7224 p->elements = constructor_elements;
7225 p->constant = constructor_constant;
7226 p->simple = constructor_simple;
928c19bb 7227 p->nonconst = constructor_nonconst;
3e4093b6
RS
7228 p->erroneous = constructor_erroneous;
7229 p->pending_elts = constructor_pending_elts;
7230 p->depth = constructor_depth;
916c5919
JM
7231 p->replacement_value.value = 0;
7232 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7233 p->replacement_value.original_type = NULL;
3e4093b6
RS
7234 p->implicit = implicit;
7235 p->outer = 0;
7236 p->incremental = constructor_incremental;
7237 p->designated = constructor_designated;
976d5a22 7238 p->designator_depth = designator_depth;
3e4093b6
RS
7239 p->next = constructor_stack;
7240 p->range_stack = 0;
7241 constructor_stack = p;
400fbf9f 7242
3e4093b6
RS
7243 constructor_constant = 1;
7244 constructor_simple = 1;
928c19bb 7245 constructor_nonconst = 0;
3e4093b6 7246 constructor_depth = SPELLING_DEPTH ();
9771b263 7247 constructor_elements = NULL;
3e4093b6
RS
7248 constructor_incremental = 1;
7249 constructor_designated = 0;
7250 constructor_pending_elts = 0;
7251 if (!implicit)
400fbf9f 7252 {
3e4093b6
RS
7253 p->range_stack = constructor_range_stack;
7254 constructor_range_stack = 0;
7255 designator_depth = 0;
b06df647 7256 designator_erroneous = 0;
3e4093b6 7257 }
400fbf9f 7258
3e4093b6
RS
7259 /* Don't die if an entire brace-pair level is superfluous
7260 in the containing level. */
7261 if (constructor_type == 0)
7262 ;
7263 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7264 || TREE_CODE (constructor_type) == UNION_TYPE)
7265 {
7266 /* Don't die if there are extra init elts at the end. */
7267 if (constructor_fields == 0)
7268 constructor_type = 0;
7269 else
400fbf9f 7270 {
3e4093b6
RS
7271 constructor_type = TREE_TYPE (constructor_fields);
7272 push_member_name (constructor_fields);
7273 constructor_depth++;
400fbf9f 7274 }
6a358dcb
MP
7275 /* If upper initializer is designated, then mark this as
7276 designated too to prevent bogus warnings. */
7277 constructor_designated = p->designated;
3e4093b6
RS
7278 }
7279 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7280 {
7281 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7282 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7283 constructor_depth++;
400fbf9f
JW
7284 }
7285
3e4093b6 7286 if (constructor_type == 0)
400fbf9f 7287 {
ea58ef42 7288 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7289 constructor_fields = 0;
7290 constructor_unfilled_fields = 0;
7291 return;
400fbf9f
JW
7292 }
7293
3e4093b6
RS
7294 if (value && TREE_CODE (value) == CONSTRUCTOR)
7295 {
7296 constructor_constant = TREE_CONSTANT (value);
7297 constructor_simple = TREE_STATIC (value);
928c19bb 7298 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7299 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7300 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7301 && (TREE_CODE (constructor_type) == RECORD_TYPE
7302 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7303 set_nonincremental_init (braced_init_obstack);
3e4093b6 7304 }
400fbf9f 7305
9bac5cbb
G
7306 if (implicit == 1)
7307 found_missing_braces = 1;
400fbf9f 7308
3e4093b6
RS
7309 if (TREE_CODE (constructor_type) == RECORD_TYPE
7310 || TREE_CODE (constructor_type) == UNION_TYPE)
7311 {
7312 constructor_fields = TYPE_FIELDS (constructor_type);
7313 /* Skip any nameless bit fields at the beginning. */
7314 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7315 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7316 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7317
3e4093b6
RS
7318 constructor_unfilled_fields = constructor_fields;
7319 constructor_bit_index = bitsize_zero_node;
7320 }
7321 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7322 {
7323 /* Vectors are like simple fixed-size arrays. */
7324 constructor_max_index =
c62c040f
RG
7325 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7326 constructor_index = bitsize_int (0);
3e4093b6
RS
7327 constructor_unfilled_index = constructor_index;
7328 }
7329 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7330 {
7331 if (TYPE_DOMAIN (constructor_type))
7332 {
7333 constructor_max_index
7334 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7335
3e4093b6
RS
7336 /* Detect non-empty initializations of zero-length arrays. */
7337 if (constructor_max_index == NULL_TREE
7338 && TYPE_SIZE (constructor_type))
9a9d280e 7339 constructor_max_index = integer_minus_one_node;
de520661 7340
3e4093b6
RS
7341 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7342 to initialize VLAs will cause a proper error; avoid tree
7343 checking errors as well by setting a safe value. */
7344 if (constructor_max_index
7345 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7346 constructor_max_index = integer_minus_one_node;
b62acd60 7347
3e4093b6
RS
7348 constructor_index
7349 = convert (bitsizetype,
7350 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7351 }
7352 else
7353 constructor_index = bitsize_zero_node;
de520661 7354
3e4093b6
RS
7355 constructor_unfilled_index = constructor_index;
7356 if (value && TREE_CODE (value) == STRING_CST)
7357 {
7358 /* We need to split the char/wchar array into individual
7359 characters, so that we don't have to special case it
7360 everywhere. */
a1e3b3d9 7361 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7362 }
7363 }
7364 else
7365 {
b4519d39 7366 if (constructor_type != error_mark_node)
96b40f8d 7367 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7368 constructor_fields = constructor_type;
7369 constructor_unfilled_fields = constructor_type;
7370 }
7371}
8b6a5902 7372
3e4093b6 7373/* At the end of an implicit or explicit brace level,
916c5919
JM
7374 finish up that level of constructor. If a single expression
7375 with redundant braces initialized that level, return the
7376 c_expr structure for that expression. Otherwise, the original_code
7377 element is set to ERROR_MARK.
7378 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7379 from inner levels (process_init_element ignores that),
916c5919 7380 but return error_mark_node as the value from the outermost level
3e4093b6 7381 (that's what we want to put in DECL_INITIAL).
916c5919 7382 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7383
916c5919 7384struct c_expr
ea58ef42
MP
7385pop_init_level (location_t loc, int implicit,
7386 struct obstack *braced_init_obstack)
3e4093b6
RS
7387{
7388 struct constructor_stack *p;
916c5919
JM
7389 struct c_expr ret;
7390 ret.value = 0;
7391 ret.original_code = ERROR_MARK;
6866c6e8 7392 ret.original_type = NULL;
de520661 7393
3e4093b6
RS
7394 if (implicit == 0)
7395 {
7396 /* When we come to an explicit close brace,
7397 pop any inner levels that didn't have explicit braces. */
7398 while (constructor_stack->implicit)
34cf811f 7399 process_init_element (input_location,
ea58ef42 7400 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7401 true, braced_init_obstack);
366de0ce 7402 gcc_assert (!constructor_range_stack);
3e4093b6 7403 }
e5e809f4 7404
0066ef9c
RH
7405 /* Now output all pending elements. */
7406 constructor_incremental = 1;
a1e3b3d9 7407 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7408
3e4093b6 7409 p = constructor_stack;
e5e809f4 7410
3e4093b6
RS
7411 /* Error for initializing a flexible array member, or a zero-length
7412 array member in an inappropriate context. */
7413 if (constructor_type && constructor_fields
7414 && TREE_CODE (constructor_type) == ARRAY_TYPE
7415 && TYPE_DOMAIN (constructor_type)
3f75a254 7416 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7417 {
7418 /* Silently discard empty initializations. The parser will
7419 already have pedwarned for empty brackets. */
7420 if (integer_zerop (constructor_unfilled_index))
7421 constructor_type = NULL_TREE;
366de0ce 7422 else
3e4093b6 7423 {
366de0ce 7424 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7425
3e4093b6 7426 if (constructor_depth > 2)
ea58ef42 7427 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7428 else
d033409e 7429 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7430 "initialization of a flexible array member");
de520661 7431
3e4093b6
RS
7432 /* We have already issued an error message for the existence
7433 of a flexible array member not at the end of the structure.
535a42b1 7434 Discard the initializer so that we do not die later. */
910ad8de 7435 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7436 constructor_type = NULL_TREE;
7437 }
3e4093b6 7438 }
de520661 7439
9bac5cbb
G
7440 if (vec_safe_length (constructor_elements) != 1)
7441 constructor_zeroinit = 0;
7442
7443 /* Warn when some structs are initialized with direct aggregation. */
7444 if (!implicit && found_missing_braces && warn_missing_braces
7445 && !constructor_zeroinit)
7446 {
7447 warning_init (loc, OPT_Wmissing_braces,
7448 "missing braces around initializer");
7449 }
7450
3e4093b6 7451 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7452 if (warn_missing_field_initializers
3e4093b6
RS
7453 && constructor_type
7454 && TREE_CODE (constructor_type) == RECORD_TYPE
7455 && constructor_unfilled_fields)
7456 {
7457 /* Do not warn for flexible array members or zero-length arrays. */
7458 while (constructor_unfilled_fields
3f75a254 7459 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7460 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7461 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7462
49819fef
AM
7463 if (constructor_unfilled_fields
7464 /* Do not warn if this level of the initializer uses member
7465 designators; it is likely to be deliberate. */
7466 && !constructor_designated
7467 /* Do not warn about initializing with ` = {0}'. */
7468 && !constructor_zeroinit)
3e4093b6 7469 {
32397f22
MLI
7470 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7471 "missing initializer for field %qD of %qT",
7472 constructor_unfilled_fields,
7473 constructor_type))
7474 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7475 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7476 }
7477 }
de520661 7478
3e4093b6 7479 /* Pad out the end of the structure. */
916c5919 7480 if (p->replacement_value.value)
3e4093b6
RS
7481 /* If this closes a superfluous brace pair,
7482 just pass out the element between them. */
916c5919 7483 ret = p->replacement_value;
3e4093b6
RS
7484 else if (constructor_type == 0)
7485 ;
7486 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7487 && TREE_CODE (constructor_type) != UNION_TYPE
7488 && TREE_CODE (constructor_type) != ARRAY_TYPE
7489 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7490 {
7491 /* A nonincremental scalar initializer--just return
7492 the element, after verifying there is just one. */
9771b263 7493 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7494 {
7495 if (!constructor_erroneous)
ea58ef42 7496 error_init (loc, "empty scalar initializer");
916c5919 7497 ret.value = error_mark_node;
3e4093b6 7498 }
9771b263 7499 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7500 {
ea58ef42 7501 error_init (loc, "extra elements in scalar initializer");
9771b263 7502 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7503 }
7504 else
9771b263 7505 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7506 }
7507 else
7508 {
7509 if (constructor_erroneous)
916c5919 7510 ret.value = error_mark_node;
3e4093b6
RS
7511 else
7512 {
916c5919 7513 ret.value = build_constructor (constructor_type,
4038c495 7514 constructor_elements);
3e4093b6 7515 if (constructor_constant)
51eed280 7516 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7517 if (constructor_constant && constructor_simple)
916c5919 7518 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7519 if (constructor_nonconst)
7520 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7521 }
7522 }
de520661 7523
928c19bb
JM
7524 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7525 {
7526 if (constructor_nonconst)
7527 ret.original_code = C_MAYBE_CONST_EXPR;
7528 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7529 ret.original_code = ERROR_MARK;
7530 }
7531
3e4093b6
RS
7532 constructor_type = p->type;
7533 constructor_fields = p->fields;
7534 constructor_index = p->index;
7535 constructor_max_index = p->max_index;
7536 constructor_unfilled_index = p->unfilled_index;
7537 constructor_unfilled_fields = p->unfilled_fields;
7538 constructor_bit_index = p->bit_index;
7539 constructor_elements = p->elements;
7540 constructor_constant = p->constant;
7541 constructor_simple = p->simple;
928c19bb 7542 constructor_nonconst = p->nonconst;
3e4093b6
RS
7543 constructor_erroneous = p->erroneous;
7544 constructor_incremental = p->incremental;
7545 constructor_designated = p->designated;
976d5a22 7546 designator_depth = p->designator_depth;
3e4093b6
RS
7547 constructor_pending_elts = p->pending_elts;
7548 constructor_depth = p->depth;
7549 if (!p->implicit)
7550 constructor_range_stack = p->range_stack;
7551 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7552
3e4093b6
RS
7553 constructor_stack = p->next;
7554 free (p);
b621a4dd 7555
5d5e98dc
VR
7556 if (ret.value == 0 && constructor_stack == 0)
7557 ret.value = error_mark_node;
916c5919 7558 return ret;
3e4093b6 7559}
8b6a5902 7560
3e4093b6
RS
7561/* Common handling for both array range and field name designators.
7562 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7563
3e4093b6 7564static int
ea58ef42
MP
7565set_designator (location_t loc, int array,
7566 struct obstack *braced_init_obstack)
de520661 7567{
3e4093b6
RS
7568 tree subtype;
7569 enum tree_code subcode;
de520661 7570
3e4093b6
RS
7571 /* Don't die if an entire brace-pair level is superfluous
7572 in the containing level. */
7573 if (constructor_type == 0)
7574 return 1;
de520661 7575
366de0ce
NS
7576 /* If there were errors in this designator list already, bail out
7577 silently. */
b06df647 7578 if (designator_erroneous)
3e4093b6 7579 return 1;
e28cae4f 7580
3e4093b6
RS
7581 if (!designator_depth)
7582 {
366de0ce 7583 gcc_assert (!constructor_range_stack);
de520661 7584
3e4093b6
RS
7585 /* Designator list starts at the level of closest explicit
7586 braces. */
7587 while (constructor_stack->implicit)
34cf811f 7588 process_init_element (input_location,
ea58ef42 7589 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7590 true, braced_init_obstack);
3e4093b6
RS
7591 constructor_designated = 1;
7592 return 0;
7593 }
de520661 7594
366de0ce 7595 switch (TREE_CODE (constructor_type))
3c3fa147 7596 {
366de0ce
NS
7597 case RECORD_TYPE:
7598 case UNION_TYPE:
3e4093b6
RS
7599 subtype = TREE_TYPE (constructor_fields);
7600 if (subtype != error_mark_node)
7601 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7602 break;
7603 case ARRAY_TYPE:
3e4093b6 7604 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7605 break;
7606 default:
7607 gcc_unreachable ();
de520661 7608 }
400fbf9f 7609
3e4093b6
RS
7610 subcode = TREE_CODE (subtype);
7611 if (array && subcode != ARRAY_TYPE)
7612 {
ea58ef42 7613 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7614 return 1;
7615 }
7616 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7617 {
ea58ef42 7618 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7619 return 1;
7620 }
d45cf215 7621
3e4093b6 7622 constructor_designated = 1;
ea58ef42 7623 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7624 return 0;
de520661 7625}
400fbf9f 7626
3e4093b6
RS
7627/* If there are range designators in designator list, push a new designator
7628 to constructor_range_stack. RANGE_END is end of such stack range or
7629 NULL_TREE if there is no range designator at this level. */
400fbf9f 7630
3e4093b6 7631static void
a1e3b3d9 7632push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7633{
7634 struct constructor_range_stack *p;
400fbf9f 7635
a1e3b3d9
LB
7636 p = (struct constructor_range_stack *)
7637 obstack_alloc (braced_init_obstack,
7638 sizeof (struct constructor_range_stack));
3e4093b6
RS
7639 p->prev = constructor_range_stack;
7640 p->next = 0;
7641 p->fields = constructor_fields;
7642 p->range_start = constructor_index;
7643 p->index = constructor_index;
7644 p->stack = constructor_stack;
7645 p->range_end = range_end;
8b6a5902 7646 if (constructor_range_stack)
3e4093b6
RS
7647 constructor_range_stack->next = p;
7648 constructor_range_stack = p;
de520661 7649}
400fbf9f 7650
3e4093b6
RS
7651/* Within an array initializer, specify the next index to be initialized.
7652 FIRST is that index. If LAST is nonzero, then initialize a range
7653 of indices, running from FIRST through LAST. */
5a7ec9d9 7654
de520661 7655void
ea58ef42 7656set_init_index (location_t loc, tree first, tree last,
d033409e 7657 struct obstack *braced_init_obstack)
de520661 7658{
ea58ef42 7659 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7660 return;
de520661 7661
b06df647 7662 designator_erroneous = 1;
de520661 7663
3ea8cd06
JM
7664 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7665 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7666 {
ea58ef42 7667 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7668 return;
7669 }
7670
2b6da65c
JM
7671 if (TREE_CODE (first) != INTEGER_CST)
7672 {
7673 first = c_fully_fold (first, false, NULL);
7674 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7675 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7676 "array index in initializer is not "
7677 "an integer constant expression");
7678 }
7679
7680 if (last && TREE_CODE (last) != INTEGER_CST)
7681 {
7682 last = c_fully_fold (last, false, NULL);
7683 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7684 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7685 "array index in initializer is not "
7686 "an integer constant expression");
7687 }
7688
3e4093b6 7689 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7690 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7691 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7692 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7693 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7694 error_init (loc, "array index in non-array initializer");
622adc7e 7695 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7696 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7697 else if (constructor_max_index
7698 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7699 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7700 else
de520661 7701 {
928c19bb
JM
7702 constant_expression_warning (first);
7703 if (last)
7704 constant_expression_warning (last);
3e4093b6 7705 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7706 if (tree_int_cst_lt (constructor_index, first))
7707 {
7708 constructor_index = copy_node (constructor_index);
7709 TREE_OVERFLOW (constructor_index) = 1;
7710 }
665f2503 7711
3e4093b6 7712 if (last)
2bede729 7713 {
3e4093b6
RS
7714 if (tree_int_cst_equal (first, last))
7715 last = 0;
7716 else if (tree_int_cst_lt (last, first))
7717 {
ea58ef42 7718 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7719 last = 0;
7720 }
7721 else
7722 {
7723 last = convert (bitsizetype, last);
7724 if (constructor_max_index != 0
7725 && tree_int_cst_lt (constructor_max_index, last))
7726 {
ea58ef42
MP
7727 error_init (loc, "array index range in initializer exceeds "
7728 "array bounds");
3e4093b6
RS
7729 last = 0;
7730 }
7731 }
2bede729 7732 }
fed3cef0 7733
3e4093b6 7734 designator_depth++;
b06df647 7735 designator_erroneous = 0;
3e4093b6 7736 if (constructor_range_stack || last)
a1e3b3d9 7737 push_range_stack (last, braced_init_obstack);
de520661 7738 }
de520661 7739}
3e4093b6
RS
7740
7741/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7742
de520661 7743void
ea58ef42
MP
7744set_init_label (location_t loc, tree fieldname,
7745 struct obstack *braced_init_obstack)
de520661 7746{
0fb96aa4 7747 tree field;
94ba5069 7748
ea58ef42 7749 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7750 return;
7751
b06df647 7752 designator_erroneous = 1;
3e4093b6
RS
7753
7754 if (TREE_CODE (constructor_type) != RECORD_TYPE
7755 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7756 {
ea58ef42 7757 error_init (loc, "field name not in record or union initializer");
3e4093b6 7758 return;
94ba5069
RS
7759 }
7760
0fb96aa4 7761 field = lookup_field (constructor_type, fieldname);
8b6a5902 7762
0fb96aa4 7763 if (field == 0)
c51a1ba9 7764 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7765 else
0fb96aa4
JM
7766 do
7767 {
7768 constructor_fields = TREE_VALUE (field);
7769 designator_depth++;
7770 designator_erroneous = 0;
7771 if (constructor_range_stack)
7772 push_range_stack (NULL_TREE, braced_init_obstack);
7773 field = TREE_CHAIN (field);
7774 if (field)
7775 {
ea58ef42 7776 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7777 return;
7778 }
7779 }
7780 while (field != NULL_TREE);
3e4093b6
RS
7781}
7782\f
7783/* Add a new initializer to the tree of pending initializers. PURPOSE
7784 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7785 VALUE is the value of that index or field. If ORIGTYPE is not
7786 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7787
7788 IMPLICIT is true if value comes from pop_init_level (1),
7789 the new initializer has been merged with the existing one
7790 and thus no warnings should be emitted about overriding an
7791 existing initializer. */
de520661 7792
3e4093b6 7793static void
96b40f8d
MP
7794add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7795 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7796{
7797 struct init_node *p, **q, *r;
7798
7799 q = &constructor_pending_elts;
7800 p = 0;
7801
7802 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7803 {
3e4093b6 7804 while (*q != 0)
91fa3c30 7805 {
3e4093b6
RS
7806 p = *q;
7807 if (tree_int_cst_lt (purpose, p->purpose))
7808 q = &p->left;
7809 else if (tree_int_cst_lt (p->purpose, purpose))
7810 q = &p->right;
7811 else
7812 {
b295aee2
JJ
7813 if (!implicit)
7814 {
7815 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7816 warning_init (loc, 0,
7817 "initialized field with side-effects "
7818 "overwritten");
b295aee2 7819 else if (warn_override_init)
96b40f8d
MP
7820 warning_init (loc, OPT_Woverride_init,
7821 "initialized field overwritten");
b295aee2 7822 }
3e4093b6 7823 p->value = value;
bbbbb16a 7824 p->origtype = origtype;
3e4093b6
RS
7825 return;
7826 }
91fa3c30 7827 }
de520661 7828 }
3e4093b6 7829 else
de520661 7830 {
3e4093b6 7831 tree bitpos;
400fbf9f 7832
3e4093b6
RS
7833 bitpos = bit_position (purpose);
7834 while (*q != NULL)
7835 {
7836 p = *q;
7837 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7838 q = &p->left;
7839 else if (p->purpose != purpose)
7840 q = &p->right;
7841 else
7842 {
b295aee2
JJ
7843 if (!implicit)
7844 {
7845 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7846 warning_init (loc, 0,
7847 "initialized field with side-effects "
7848 "overwritten");
b295aee2 7849 else if (warn_override_init)
96b40f8d
MP
7850 warning_init (loc, OPT_Woverride_init,
7851 "initialized field overwritten");
b295aee2 7852 }
3e4093b6 7853 p->value = value;
bbbbb16a 7854 p->origtype = origtype;
3e4093b6
RS
7855 return;
7856 }
7857 }
91fa3c30 7858 }
b71c7f8a 7859
a1e3b3d9
LB
7860 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7861 sizeof (struct init_node));
3e4093b6
RS
7862 r->purpose = purpose;
7863 r->value = value;
bbbbb16a 7864 r->origtype = origtype;
8b6a5902 7865
3e4093b6
RS
7866 *q = r;
7867 r->parent = p;
7868 r->left = 0;
7869 r->right = 0;
7870 r->balance = 0;
b71c7f8a 7871
3e4093b6 7872 while (p)
de520661 7873 {
3e4093b6 7874 struct init_node *s;
665f2503 7875
3e4093b6 7876 if (r == p->left)
2bede729 7877 {
3e4093b6
RS
7878 if (p->balance == 0)
7879 p->balance = -1;
7880 else if (p->balance < 0)
7881 {
7882 if (r->balance < 0)
7883 {
7884 /* L rotation. */
7885 p->left = r->right;
7886 if (p->left)
7887 p->left->parent = p;
7888 r->right = p;
e7b6a0ee 7889
3e4093b6
RS
7890 p->balance = 0;
7891 r->balance = 0;
39bc99c2 7892
3e4093b6
RS
7893 s = p->parent;
7894 p->parent = r;
7895 r->parent = s;
7896 if (s)
7897 {
7898 if (s->left == p)
7899 s->left = r;
7900 else
7901 s->right = r;
7902 }
7903 else
7904 constructor_pending_elts = r;
7905 }
7906 else
7907 {
7908 /* LR rotation. */
7909 struct init_node *t = r->right;
e7b6a0ee 7910
3e4093b6
RS
7911 r->right = t->left;
7912 if (r->right)
7913 r->right->parent = r;
7914 t->left = r;
7915
7916 p->left = t->right;
7917 if (p->left)
7918 p->left->parent = p;
7919 t->right = p;
7920
7921 p->balance = t->balance < 0;
7922 r->balance = -(t->balance > 0);
7923 t->balance = 0;
7924
7925 s = p->parent;
7926 p->parent = t;
7927 r->parent = t;
7928 t->parent = s;
7929 if (s)
7930 {
7931 if (s->left == p)
7932 s->left = t;
7933 else
7934 s->right = t;
7935 }
7936 else
7937 constructor_pending_elts = t;
7938 }
7939 break;
7940 }
7941 else
7942 {
7943 /* p->balance == +1; growth of left side balances the node. */
7944 p->balance = 0;
7945 break;
7946 }
2bede729 7947 }
3e4093b6
RS
7948 else /* r == p->right */
7949 {
7950 if (p->balance == 0)
7951 /* Growth propagation from right side. */
7952 p->balance++;
7953 else if (p->balance > 0)
7954 {
7955 if (r->balance > 0)
7956 {
7957 /* R rotation. */
7958 p->right = r->left;
7959 if (p->right)
7960 p->right->parent = p;
7961 r->left = p;
7962
7963 p->balance = 0;
7964 r->balance = 0;
7965
7966 s = p->parent;
7967 p->parent = r;
7968 r->parent = s;
7969 if (s)
7970 {
7971 if (s->left == p)
7972 s->left = r;
7973 else
7974 s->right = r;
7975 }
7976 else
7977 constructor_pending_elts = r;
7978 }
7979 else /* r->balance == -1 */
7980 {
7981 /* RL rotation */
7982 struct init_node *t = r->left;
7983
7984 r->left = t->right;
7985 if (r->left)
7986 r->left->parent = r;
7987 t->right = r;
7988
7989 p->right = t->left;
7990 if (p->right)
7991 p->right->parent = p;
7992 t->left = p;
7993
7994 r->balance = (t->balance < 0);
7995 p->balance = -(t->balance > 0);
7996 t->balance = 0;
7997
7998 s = p->parent;
7999 p->parent = t;
8000 r->parent = t;
8001 t->parent = s;
8002 if (s)
8003 {
8004 if (s->left == p)
8005 s->left = t;
8006 else
8007 s->right = t;
8008 }
8009 else
8010 constructor_pending_elts = t;
8011 }
8012 break;
8013 }
8014 else
8015 {
8016 /* p->balance == -1; growth of right side balances the node. */
8017 p->balance = 0;
8018 break;
8019 }
8020 }
8021
8022 r = p;
8023 p = p->parent;
8024 }
8025}
8026
8027/* Build AVL tree from a sorted chain. */
8028
8029static void
a1e3b3d9 8030set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8031{
4038c495
GB
8032 unsigned HOST_WIDE_INT ix;
8033 tree index, value;
3e4093b6
RS
8034
8035 if (TREE_CODE (constructor_type) != RECORD_TYPE
8036 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8037 return;
8038
4038c495 8039 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8040 add_pending_init (input_location, index, value, NULL_TREE, true,
8041 braced_init_obstack);
9771b263 8042 constructor_elements = NULL;
3e4093b6
RS
8043 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8044 {
8045 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8046 /* Skip any nameless bit fields at the beginning. */
8047 while (constructor_unfilled_fields != 0
8048 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8049 && DECL_NAME (constructor_unfilled_fields) == 0)
8050 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8051
de520661 8052 }
3e4093b6 8053 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8054 {
3e4093b6
RS
8055 if (TYPE_DOMAIN (constructor_type))
8056 constructor_unfilled_index
8057 = convert (bitsizetype,
8058 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8059 else
8060 constructor_unfilled_index = bitsize_zero_node;
de520661 8061 }
3e4093b6 8062 constructor_incremental = 0;
de520661 8063}
400fbf9f 8064
3e4093b6 8065/* Build AVL tree from a string constant. */
de520661 8066
3e4093b6 8067static void
a1e3b3d9
LB
8068set_nonincremental_init_from_string (tree str,
8069 struct obstack * braced_init_obstack)
de520661 8070{
3e4093b6
RS
8071 tree value, purpose, type;
8072 HOST_WIDE_INT val[2];
8073 const char *p, *end;
8074 int byte, wchar_bytes, charwidth, bitpos;
de520661 8075
366de0ce 8076 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8077
c466b2cd 8078 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8079 charwidth = TYPE_PRECISION (char_type_node);
8080 type = TREE_TYPE (constructor_type);
8081 p = TREE_STRING_POINTER (str);
8082 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8083
3e4093b6 8084 for (purpose = bitsize_zero_node;
8824edff
JJ
8085 p < end
8086 && !(constructor_max_index
8087 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8088 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8089 {
3e4093b6 8090 if (wchar_bytes == 1)
ffc5c6a9 8091 {
807e902e
KZ
8092 val[0] = (unsigned char) *p++;
8093 val[1] = 0;
ffc5c6a9
RH
8094 }
8095 else
3e4093b6 8096 {
3e4093b6 8097 val[1] = 0;
807e902e 8098 val[0] = 0;
3e4093b6
RS
8099 for (byte = 0; byte < wchar_bytes; byte++)
8100 {
8101 if (BYTES_BIG_ENDIAN)
8102 bitpos = (wchar_bytes - byte - 1) * charwidth;
8103 else
8104 bitpos = byte * charwidth;
807e902e 8105 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8106 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8107 << (bitpos % HOST_BITS_PER_WIDE_INT);
8108 }
8109 }
584ef5fe 8110
8df83eae 8111 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8112 {
8113 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8114 if (bitpos < HOST_BITS_PER_WIDE_INT)
8115 {
807e902e 8116 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8117 {
807e902e
KZ
8118 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8119 val[1] = -1;
3e4093b6
RS
8120 }
8121 }
8122 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8123 {
807e902e
KZ
8124 if (val[0] < 0)
8125 val[1] = -1;
3e4093b6 8126 }
807e902e 8127 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8128 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8129 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8130 << (bitpos - HOST_BITS_PER_WIDE_INT);
8131 }
ffc5c6a9 8132
807e902e
KZ
8133 value = wide_int_to_tree (type,
8134 wide_int::from_array (val, 2,
8135 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8136 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8137 braced_init_obstack);
9dfcc8db
BH
8138 }
8139
3e4093b6
RS
8140 constructor_incremental = 0;
8141}
de520661 8142
3e4093b6
RS
8143/* Return value of FIELD in pending initializer or zero if the field was
8144 not initialized yet. */
8145
8146static tree
a1e3b3d9 8147find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8148{
8149 struct init_node *p;
8150
8151 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8152 {
3e4093b6
RS
8153 if (constructor_incremental
8154 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8155 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8156
8157 p = constructor_pending_elts;
8158 while (p)
19d76e60 8159 {
3e4093b6
RS
8160 if (tree_int_cst_lt (field, p->purpose))
8161 p = p->left;
8162 else if (tree_int_cst_lt (p->purpose, field))
8163 p = p->right;
8164 else
8165 return p->value;
19d76e60 8166 }
19d76e60 8167 }
3e4093b6 8168 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8169 {
3e4093b6 8170 tree bitpos = bit_position (field);
de520661 8171
3e4093b6
RS
8172 if (constructor_incremental
8173 && (!constructor_unfilled_fields
8174 || tree_int_cst_lt (bitpos,
8175 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8176 set_nonincremental_init (braced_init_obstack);
de520661 8177
3e4093b6
RS
8178 p = constructor_pending_elts;
8179 while (p)
8180 {
8181 if (field == p->purpose)
8182 return p->value;
8183 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8184 p = p->left;
8185 else
8186 p = p->right;
8187 }
8188 }
8189 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8190 {
9771b263
DN
8191 if (!vec_safe_is_empty (constructor_elements)
8192 && (constructor_elements->last ().index == field))
8193 return constructor_elements->last ().value;
de520661 8194 }
3e4093b6 8195 return 0;
de520661
RS
8196}
8197
3e4093b6
RS
8198/* "Output" the next constructor element.
8199 At top level, really output it to assembler code now.
8200 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8201 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8202 TYPE is the data type that the containing data type wants here.
8203 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8204 If VALUE is a string constant, STRICT_STRING is true if it is
8205 unparenthesized or we should not warn here for it being parenthesized.
8206 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8207
3e4093b6
RS
8208 PENDING if non-nil means output pending elements that belong
8209 right after this element. (PENDING is normally 1;
b295aee2
JJ
8210 it is 0 while outputting pending elements, to avoid recursion.)
8211
8212 IMPLICIT is true if value comes from pop_init_level (1),
8213 the new initializer has been merged with the existing one
8214 and thus no warnings should be emitted about overriding an
8215 existing initializer. */
8b6a5902 8216
3e4093b6 8217static void
34cf811f
MP
8218output_init_element (location_t loc, tree value, tree origtype,
8219 bool strict_string, tree type, tree field, int pending,
8220 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8221{
8ce94e44 8222 tree semantic_type = NULL_TREE;
928c19bb
JM
8223 bool maybe_const = true;
8224 bool npc;
4038c495 8225
0a880880 8226 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8227 {
3e4093b6
RS
8228 constructor_erroneous = 1;
8229 return;
8b6a5902 8230 }
46bdb9cf
JM
8231 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8232 && (TREE_CODE (value) == STRING_CST
8233 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8234 && !(TREE_CODE (value) == STRING_CST
8235 && TREE_CODE (type) == ARRAY_TYPE
8236 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8237 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8238 TYPE_MAIN_VARIANT (type)))
c2255bc4 8239 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8240
3e4093b6
RS
8241 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8242 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 8243 {
3e4093b6
RS
8244 /* As an extension, allow initializing objects with static storage
8245 duration with compound literals (which are then treated just as
8246 the brace enclosed list they contain). */
8247 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8248 value = DECL_INITIAL (decl);
8b6a5902
JJ
8249 }
8250
928c19bb 8251 npc = null_pointer_constant_p (value);
8ce94e44
JM
8252 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8253 {
8254 semantic_type = TREE_TYPE (value);
8255 value = TREE_OPERAND (value, 0);
8256 }
928c19bb
JM
8257 value = c_fully_fold (value, require_constant_value, &maybe_const);
8258
3e4093b6
RS
8259 if (value == error_mark_node)
8260 constructor_erroneous = 1;
8261 else if (!TREE_CONSTANT (value))
8262 constructor_constant = 0;
116df786 8263 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8264 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8265 || TREE_CODE (constructor_type) == UNION_TYPE)
8266 && DECL_C_BIT_FIELD (field)
8267 && TREE_CODE (value) != INTEGER_CST))
8268 constructor_simple = 0;
928c19bb
JM
8269 if (!maybe_const)
8270 constructor_nonconst = 1;
3e4093b6 8271
116df786 8272 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8273 {
116df786
RH
8274 if (require_constant_value)
8275 {
ea58ef42 8276 error_init (loc, "initializer element is not constant");
116df786
RH
8277 value = error_mark_node;
8278 }
8279 else if (require_constant_elements)
8337d1db 8280 pedwarn (loc, OPT_Wpedantic,
509c9d60 8281 "initializer element is not computable at load time");
8b6a5902 8282 }
928c19bb
JM
8283 else if (!maybe_const
8284 && (require_constant_value || require_constant_elements))
8337d1db 8285 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8286 "initializer element is not a constant expression");
3e4093b6 8287
81f40b79
ILT
8288 /* Issue -Wc++-compat warnings about initializing a bitfield with
8289 enum type. */
8290 if (warn_cxx_compat
8291 && field != NULL_TREE
8292 && TREE_CODE (field) == FIELD_DECL
8293 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8294 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8295 != TYPE_MAIN_VARIANT (type))
8296 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8297 {
8298 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8299 if (checktype != error_mark_node
8300 && (TYPE_MAIN_VARIANT (checktype)
8301 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8302 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8303 "enum conversion in initialization is invalid in C++");
8304 }
8305
3e4093b6
RS
8306 /* If this field is empty (and not at the end of structure),
8307 don't do anything other than checking the initializer. */
8308 if (field
8309 && (TREE_TYPE (field) == error_mark_node
8310 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8311 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8312 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8313 || DECL_CHAIN (field)))))
3e4093b6
RS
8314 return;
8315
8ce94e44
JM
8316 if (semantic_type)
8317 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8318 value = digest_init (loc, type, value, origtype, npc, strict_string,
8319 require_constant_value);
3e4093b6 8320 if (value == error_mark_node)
8b6a5902 8321 {
3e4093b6
RS
8322 constructor_erroneous = 1;
8323 return;
8b6a5902 8324 }
928c19bb
JM
8325 if (require_constant_value || require_constant_elements)
8326 constant_expression_warning (value);
8b6a5902 8327
3e4093b6
RS
8328 /* If this element doesn't come next in sequence,
8329 put it on constructor_pending_elts. */
8330 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8331 && (!constructor_incremental
8332 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8333 {
3e4093b6
RS
8334 if (constructor_incremental
8335 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8336 set_nonincremental_init (braced_init_obstack);
3e4093b6 8337
96b40f8d 8338 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8339 braced_init_obstack);
3e4093b6 8340 return;
8b6a5902 8341 }
3e4093b6
RS
8342 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8343 && (!constructor_incremental
8344 || field != constructor_unfilled_fields))
8b6a5902 8345 {
3e4093b6
RS
8346 /* We do this for records but not for unions. In a union,
8347 no matter which field is specified, it can be initialized
8348 right away since it starts at the beginning of the union. */
8349 if (constructor_incremental)
8350 {
8351 if (!constructor_unfilled_fields)
a1e3b3d9 8352 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8353 else
8354 {
8355 tree bitpos, unfillpos;
8356
8357 bitpos = bit_position (field);
8358 unfillpos = bit_position (constructor_unfilled_fields);
8359
8360 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8361 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8362 }
8363 }
8364
96b40f8d 8365 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8366 braced_init_obstack);
3e4093b6 8367 return;
8b6a5902 8368 }
3e4093b6 8369 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8370 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8371 {
b295aee2
JJ
8372 if (!implicit)
8373 {
9771b263 8374 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
96b40f8d 8375 warning_init (loc, 0,
b295aee2
JJ
8376 "initialized field with side-effects overwritten");
8377 else if (warn_override_init)
96b40f8d
MP
8378 warning_init (loc, OPT_Woverride_init,
8379 "initialized field overwritten");
b295aee2 8380 }
8b6a5902 8381
3e4093b6 8382 /* We can have just one union field set. */
9771b263 8383 constructor_elements = NULL;
3e4093b6 8384 }
8b6a5902 8385
3e4093b6
RS
8386 /* Otherwise, output this element either to
8387 constructor_elements or to the assembler file. */
8b6a5902 8388
f32682ca 8389 constructor_elt celt = {field, value};
9771b263 8390 vec_safe_push (constructor_elements, celt);
8b6a5902 8391
3e4093b6
RS
8392 /* Advance the variable that indicates sequential elements output. */
8393 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8394 constructor_unfilled_index
db3927fb
AH
8395 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8396 bitsize_one_node);
3e4093b6
RS
8397 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8398 {
8399 constructor_unfilled_fields
910ad8de 8400 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8401
3e4093b6
RS
8402 /* Skip any nameless bit fields. */
8403 while (constructor_unfilled_fields != 0
8404 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8405 && DECL_NAME (constructor_unfilled_fields) == 0)
8406 constructor_unfilled_fields =
910ad8de 8407 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8408 }
8409 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8410 constructor_unfilled_fields = 0;
de520661 8411
3e4093b6
RS
8412 /* Now output any pending elements which have become next. */
8413 if (pending)
a1e3b3d9 8414 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8415}
8b6a5902 8416
3e4093b6
RS
8417/* Output any pending elements which have become next.
8418 As we output elements, constructor_unfilled_{fields,index}
8419 advances, which may cause other elements to become next;
8420 if so, they too are output.
8b6a5902 8421
3e4093b6
RS
8422 If ALL is 0, we return when there are
8423 no more pending elements to output now.
665f2503 8424
3e4093b6
RS
8425 If ALL is 1, we output space as necessary so that
8426 we can output all the pending elements. */
3e4093b6 8427static void
a1e3b3d9 8428output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8429{
8430 struct init_node *elt = constructor_pending_elts;
8431 tree next;
de520661 8432
3e4093b6
RS
8433 retry:
8434
ba228239 8435 /* Look through the whole pending tree.
3e4093b6
RS
8436 If we find an element that should be output now,
8437 output it. Otherwise, set NEXT to the element
8438 that comes first among those still pending. */
8439
8440 next = 0;
8441 while (elt)
8442 {
8443 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8444 {
3e4093b6
RS
8445 if (tree_int_cst_equal (elt->purpose,
8446 constructor_unfilled_index))
34cf811f
MP
8447 output_init_element (input_location, elt->value, elt->origtype,
8448 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8449 constructor_unfilled_index, 0, false,
8450 braced_init_obstack);
3e4093b6
RS
8451 else if (tree_int_cst_lt (constructor_unfilled_index,
8452 elt->purpose))
8b6a5902 8453 {
3e4093b6
RS
8454 /* Advance to the next smaller node. */
8455 if (elt->left)
8456 elt = elt->left;
8457 else
8458 {
8459 /* We have reached the smallest node bigger than the
8460 current unfilled index. Fill the space first. */
8461 next = elt->purpose;
8462 break;
8463 }
8b6a5902 8464 }
ce662d4c
JJ
8465 else
8466 {
3e4093b6
RS
8467 /* Advance to the next bigger node. */
8468 if (elt->right)
8469 elt = elt->right;
8470 else
ce662d4c 8471 {
3e4093b6
RS
8472 /* We have reached the biggest node in a subtree. Find
8473 the parent of it, which is the next bigger node. */
8474 while (elt->parent && elt->parent->right == elt)
8475 elt = elt->parent;
8476 elt = elt->parent;
8477 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8478 elt->purpose))
8479 {
8480 next = elt->purpose;
8481 break;
8482 }
ce662d4c
JJ
8483 }
8484 }
8b6a5902 8485 }
3e4093b6
RS
8486 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8487 || TREE_CODE (constructor_type) == UNION_TYPE)
8488 {
8489 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8490
3e4093b6
RS
8491 /* If the current record is complete we are done. */
8492 if (constructor_unfilled_fields == 0)
8493 break;
de520661 8494
3e4093b6
RS
8495 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8496 elt_bitpos = bit_position (elt->purpose);
8497 /* We can't compare fields here because there might be empty
8498 fields in between. */
8499 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8500 {
8501 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8502 output_init_element (input_location, elt->value, elt->origtype,
8503 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8504 elt->purpose, 0, false,
8505 braced_init_obstack);
3e4093b6
RS
8506 }
8507 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8508 {
8509 /* Advance to the next smaller node. */
8510 if (elt->left)
8511 elt = elt->left;
8512 else
8513 {
8514 /* We have reached the smallest node bigger than the
8515 current unfilled field. Fill the space first. */
8516 next = elt->purpose;
8517 break;
8518 }
8519 }
8520 else
8521 {
8522 /* Advance to the next bigger node. */
8523 if (elt->right)
8524 elt = elt->right;
8525 else
8526 {
8527 /* We have reached the biggest node in a subtree. Find
8528 the parent of it, which is the next bigger node. */
8529 while (elt->parent && elt->parent->right == elt)
8530 elt = elt->parent;
8531 elt = elt->parent;
8532 if (elt
8533 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8534 bit_position (elt->purpose))))
8535 {
8536 next = elt->purpose;
8537 break;
8538 }
8539 }
8540 }
8541 }
8542 }
de520661 8543
3e4093b6
RS
8544 /* Ordinarily return, but not if we want to output all
8545 and there are elements left. */
3f75a254 8546 if (!(all && next != 0))
e5cfb88f
RK
8547 return;
8548
3e4093b6
RS
8549 /* If it's not incremental, just skip over the gap, so that after
8550 jumping to retry we will output the next successive element. */
8551 if (TREE_CODE (constructor_type) == RECORD_TYPE
8552 || TREE_CODE (constructor_type) == UNION_TYPE)
8553 constructor_unfilled_fields = next;
8554 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8555 constructor_unfilled_index = next;
de520661 8556
3e4093b6
RS
8557 /* ELT now points to the node in the pending tree with the next
8558 initializer to output. */
8559 goto retry;
de520661
RS
8560}
8561\f
3e4093b6
RS
8562/* Add one non-braced element to the current constructor level.
8563 This adjusts the current position within the constructor's type.
8564 This may also start or terminate implicit levels
8565 to handle a partly-braced initializer.
e5e809f4 8566
3e4093b6 8567 Once this has found the correct level for the new element,
b295aee2
JJ
8568 it calls output_init_element.
8569
8570 IMPLICIT is true if value comes from pop_init_level (1),
8571 the new initializer has been merged with the existing one
8572 and thus no warnings should be emitted about overriding an
8573 existing initializer. */
3e4093b6
RS
8574
8575void
34cf811f 8576process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8577 struct obstack * braced_init_obstack)
e5e809f4 8578{
916c5919
JM
8579 tree orig_value = value.value;
8580 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8581 bool strict_string = value.original_code == STRING_CST;
340baef7 8582 bool was_designated = designator_depth != 0;
e5e809f4 8583
3e4093b6 8584 designator_depth = 0;
b06df647 8585 designator_erroneous = 0;
e5e809f4 8586
9bac5cbb
G
8587 if (!implicit && value.value && !integer_zerop (value.value))
8588 constructor_zeroinit = 0;
8589
3e4093b6
RS
8590 /* Handle superfluous braces around string cst as in
8591 char x[] = {"foo"}; */
8592 if (string_flag
8593 && constructor_type
340baef7 8594 && !was_designated
3e4093b6 8595 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8596 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8597 && integer_zerop (constructor_unfilled_index))
e5e809f4 8598 {
916c5919 8599 if (constructor_stack->replacement_value.value)
ea58ef42 8600 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8601 constructor_stack->replacement_value = value;
8602 return;
e5e809f4 8603 }
8b6a5902 8604
916c5919 8605 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8606 {
ea58ef42 8607 error_init (loc, "excess elements in struct initializer");
3e4093b6 8608 return;
e5e809f4
JL
8609 }
8610
3e4093b6
RS
8611 /* Ignore elements of a brace group if it is entirely superfluous
8612 and has already been diagnosed. */
8613 if (constructor_type == 0)
8614 return;
e5e809f4 8615
976d5a22
TT
8616 if (!implicit && warn_designated_init && !was_designated
8617 && TREE_CODE (constructor_type) == RECORD_TYPE
8618 && lookup_attribute ("designated_init",
8619 TYPE_ATTRIBUTES (constructor_type)))
8620 warning_init (loc,
8621 OPT_Wdesignated_init,
8622 "positional initialization of field "
8623 "in %<struct%> declared with %<designated_init%> attribute");
8624
3e4093b6
RS
8625 /* If we've exhausted any levels that didn't have braces,
8626 pop them now. */
8627 while (constructor_stack->implicit)
8628 {
8629 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8630 || TREE_CODE (constructor_type) == UNION_TYPE)
8631 && constructor_fields == 0)
ea58ef42
MP
8632 process_init_element (loc,
8633 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8634 true, braced_init_obstack);
53650abe
AP
8635 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8636 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8637 && constructor_max_index
8638 && tree_int_cst_lt (constructor_max_index,
8639 constructor_index))
ea58ef42
MP
8640 process_init_element (loc,
8641 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8642 true, braced_init_obstack);
3e4093b6
RS
8643 else
8644 break;
8645 }
e5e809f4 8646
3e4093b6
RS
8647 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8648 if (constructor_range_stack)
e5e809f4 8649 {
3e4093b6
RS
8650 /* If value is a compound literal and we'll be just using its
8651 content, don't put it into a SAVE_EXPR. */
916c5919 8652 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8653 || !require_constant_value
8654 || flag_isoc99)
8ce94e44
JM
8655 {
8656 tree semantic_type = NULL_TREE;
8657 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8658 {
8659 semantic_type = TREE_TYPE (value.value);
8660 value.value = TREE_OPERAND (value.value, 0);
8661 }
8662 value.value = c_save_expr (value.value);
8663 if (semantic_type)
8664 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8665 value.value);
8666 }
3e4093b6 8667 }
e5e809f4 8668
3e4093b6
RS
8669 while (1)
8670 {
8671 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8672 {
3e4093b6
RS
8673 tree fieldtype;
8674 enum tree_code fieldcode;
e5e809f4 8675
3e4093b6
RS
8676 if (constructor_fields == 0)
8677 {
ea58ef42 8678 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8679 break;
8680 }
e5e809f4 8681
3e4093b6
RS
8682 fieldtype = TREE_TYPE (constructor_fields);
8683 if (fieldtype != error_mark_node)
8684 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8685 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8686
3e4093b6
RS
8687 /* Error for non-static initialization of a flexible array member. */
8688 if (fieldcode == ARRAY_TYPE
8689 && !require_constant_value
8690 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8691 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8692 {
ea58ef42
MP
8693 error_init (loc, "non-static initialization of a flexible "
8694 "array member");
3e4093b6
RS
8695 break;
8696 }
e5e809f4 8697
3e4093b6 8698 /* Accept a string constant to initialize a subarray. */
916c5919 8699 if (value.value != 0
3e4093b6 8700 && fieldcode == ARRAY_TYPE
197463ae 8701 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8702 && string_flag)
916c5919 8703 value.value = orig_value;
3e4093b6
RS
8704 /* Otherwise, if we have come to a subaggregate,
8705 and we don't have an element of its type, push into it. */
0953878d 8706 else if (value.value != 0
916c5919
JM
8707 && value.value != error_mark_node
8708 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8709 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8710 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8711 {
ea58ef42 8712 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8713 continue;
8714 }
e5e809f4 8715
916c5919 8716 if (value.value)
3e4093b6
RS
8717 {
8718 push_member_name (constructor_fields);
34cf811f 8719 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8720 strict_string, fieldtype,
a1e3b3d9
LB
8721 constructor_fields, 1, implicit,
8722 braced_init_obstack);
3e4093b6 8723 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8724 }
8725 else
3e4093b6
RS
8726 /* Do the bookkeeping for an element that was
8727 directly output as a constructor. */
e5e809f4 8728 {
3e4093b6
RS
8729 /* For a record, keep track of end position of last field. */
8730 if (DECL_SIZE (constructor_fields))
c22cacf3 8731 constructor_bit_index
db3927fb
AH
8732 = size_binop_loc (input_location, PLUS_EXPR,
8733 bit_position (constructor_fields),
8734 DECL_SIZE (constructor_fields));
3e4093b6
RS
8735
8736 /* If the current field was the first one not yet written out,
8737 it isn't now, so update. */
8738 if (constructor_unfilled_fields == constructor_fields)
8739 {
910ad8de 8740 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8741 /* Skip any nameless bit fields. */
8742 while (constructor_unfilled_fields != 0
8743 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8744 && DECL_NAME (constructor_unfilled_fields) == 0)
8745 constructor_unfilled_fields =
910ad8de 8746 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8747 }
e5e809f4 8748 }
3e4093b6 8749
910ad8de 8750 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8751 /* Skip any nameless bit fields at the beginning. */
8752 while (constructor_fields != 0
8753 && DECL_C_BIT_FIELD (constructor_fields)
8754 && DECL_NAME (constructor_fields) == 0)
910ad8de 8755 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8756 }
3e4093b6 8757 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8758 {
3e4093b6
RS
8759 tree fieldtype;
8760 enum tree_code fieldcode;
e5e809f4 8761
3e4093b6
RS
8762 if (constructor_fields == 0)
8763 {
d033409e 8764 pedwarn_init (loc, 0,
509c9d60 8765 "excess elements in union initializer");
3e4093b6
RS
8766 break;
8767 }
e5e809f4 8768
3e4093b6
RS
8769 fieldtype = TREE_TYPE (constructor_fields);
8770 if (fieldtype != error_mark_node)
8771 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8772 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8773
3e4093b6
RS
8774 /* Warn that traditional C rejects initialization of unions.
8775 We skip the warning if the value is zero. This is done
8776 under the assumption that the zero initializer in user
8777 code appears conditioned on e.g. __STDC__ to avoid
8778 "missing initializer" warnings and relies on default
8779 initialization to zero in the traditional C case.
8780 We also skip the warning if the initializer is designated,
8781 again on the assumption that this must be conditional on
8782 __STDC__ anyway (and we've already complained about the
8783 member-designator already). */
8400e75e 8784 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8785 && !(value.value && (integer_zerop (value.value)
8786 || real_zerop (value.value))))
3176a0c2
DD
8787 warning (OPT_Wtraditional, "traditional C rejects initialization "
8788 "of unions");
e5e809f4 8789
3e4093b6 8790 /* Accept a string constant to initialize a subarray. */
916c5919 8791 if (value.value != 0
3e4093b6 8792 && fieldcode == ARRAY_TYPE
197463ae 8793 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8794 && string_flag)
916c5919 8795 value.value = orig_value;
3e4093b6
RS
8796 /* Otherwise, if we have come to a subaggregate,
8797 and we don't have an element of its type, push into it. */
0953878d 8798 else if (value.value != 0
916c5919
JM
8799 && value.value != error_mark_node
8800 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8801 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8802 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8803 {
ea58ef42 8804 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8805 continue;
8806 }
e5e809f4 8807
916c5919 8808 if (value.value)
3e4093b6
RS
8809 {
8810 push_member_name (constructor_fields);
34cf811f 8811 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8812 strict_string, fieldtype,
a1e3b3d9
LB
8813 constructor_fields, 1, implicit,
8814 braced_init_obstack);
3e4093b6 8815 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8816 }
8817 else
3e4093b6
RS
8818 /* Do the bookkeeping for an element that was
8819 directly output as a constructor. */
e5e809f4 8820 {
3e4093b6 8821 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8822 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8823 }
e5e809f4 8824
3e4093b6
RS
8825 constructor_fields = 0;
8826 }
8827 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8828 {
8829 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8830 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8831
3e4093b6 8832 /* Accept a string constant to initialize a subarray. */
916c5919 8833 if (value.value != 0
3e4093b6 8834 && eltcode == ARRAY_TYPE
197463ae 8835 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8836 && string_flag)
916c5919 8837 value.value = orig_value;
3e4093b6
RS
8838 /* Otherwise, if we have come to a subaggregate,
8839 and we don't have an element of its type, push into it. */
0953878d 8840 else if (value.value != 0
916c5919
JM
8841 && value.value != error_mark_node
8842 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8843 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8844 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8845 {
ea58ef42 8846 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8847 continue;
8848 }
8b6a5902 8849
3e4093b6
RS
8850 if (constructor_max_index != 0
8851 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8852 || integer_all_onesp (constructor_max_index)))
8853 {
d033409e 8854 pedwarn_init (loc, 0,
509c9d60 8855 "excess elements in array initializer");
3e4093b6
RS
8856 break;
8857 }
8b6a5902 8858
3e4093b6 8859 /* Now output the actual element. */
916c5919 8860 if (value.value)
3e4093b6 8861 {
ae7e9ddd 8862 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 8863 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8864 strict_string, elttype,
a1e3b3d9
LB
8865 constructor_index, 1, implicit,
8866 braced_init_obstack);
3e4093b6
RS
8867 RESTORE_SPELLING_DEPTH (constructor_depth);
8868 }
2f6e4e97 8869
3e4093b6 8870 constructor_index
db3927fb
AH
8871 = size_binop_loc (input_location, PLUS_EXPR,
8872 constructor_index, bitsize_one_node);
8b6a5902 8873
916c5919 8874 if (!value.value)
3e4093b6
RS
8875 /* If we are doing the bookkeeping for an element that was
8876 directly output as a constructor, we must update
8877 constructor_unfilled_index. */
8878 constructor_unfilled_index = constructor_index;
8879 }
8880 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8881 {
8882 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8883
c22cacf3
MS
8884 /* Do a basic check of initializer size. Note that vectors
8885 always have a fixed size derived from their type. */
3e4093b6
RS
8886 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8887 {
d033409e 8888 pedwarn_init (loc, 0,
509c9d60 8889 "excess elements in vector initializer");
3e4093b6
RS
8890 break;
8891 }
8b6a5902 8892
3e4093b6 8893 /* Now output the actual element. */
916c5919 8894 if (value.value)
53650abe
AP
8895 {
8896 if (TREE_CODE (value.value) == VECTOR_CST)
8897 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 8898 output_init_element (loc, value.value, value.original_type,
53650abe 8899 strict_string, elttype,
a1e3b3d9
LB
8900 constructor_index, 1, implicit,
8901 braced_init_obstack);
53650abe 8902 }
8b6a5902 8903
3e4093b6 8904 constructor_index
db3927fb
AH
8905 = size_binop_loc (input_location,
8906 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8907
916c5919 8908 if (!value.value)
3e4093b6
RS
8909 /* If we are doing the bookkeeping for an element that was
8910 directly output as a constructor, we must update
8911 constructor_unfilled_index. */
8912 constructor_unfilled_index = constructor_index;
8913 }
8b6a5902 8914
3e4093b6
RS
8915 /* Handle the sole element allowed in a braced initializer
8916 for a scalar variable. */
b4519d39
SB
8917 else if (constructor_type != error_mark_node
8918 && constructor_fields == 0)
8b6a5902 8919 {
d033409e 8920 pedwarn_init (loc, 0,
509c9d60 8921 "excess elements in scalar initializer");
3e4093b6 8922 break;
8b6a5902
JJ
8923 }
8924 else
8925 {
916c5919 8926 if (value.value)
34cf811f 8927 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8928 strict_string, constructor_type,
a1e3b3d9
LB
8929 NULL_TREE, 1, implicit,
8930 braced_init_obstack);
3e4093b6 8931 constructor_fields = 0;
8b6a5902
JJ
8932 }
8933
3e4093b6
RS
8934 /* Handle range initializers either at this level or anywhere higher
8935 in the designator stack. */
8936 if (constructor_range_stack)
8b6a5902 8937 {
3e4093b6
RS
8938 struct constructor_range_stack *p, *range_stack;
8939 int finish = 0;
8940
8941 range_stack = constructor_range_stack;
8942 constructor_range_stack = 0;
8943 while (constructor_stack != range_stack->stack)
8b6a5902 8944 {
366de0ce 8945 gcc_assert (constructor_stack->implicit);
34cf811f 8946 process_init_element (loc,
ea58ef42
MP
8947 pop_init_level (loc, 1,
8948 braced_init_obstack),
a1e3b3d9 8949 true, braced_init_obstack);
8b6a5902 8950 }
3e4093b6
RS
8951 for (p = range_stack;
8952 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8953 p = p->prev)
8b6a5902 8954 {
366de0ce 8955 gcc_assert (constructor_stack->implicit);
34cf811f 8956 process_init_element (loc,
ea58ef42
MP
8957 pop_init_level (loc, 1,
8958 braced_init_obstack),
a1e3b3d9 8959 true, braced_init_obstack);
8b6a5902 8960 }
3e4093b6 8961
db3927fb
AH
8962 p->index = size_binop_loc (input_location,
8963 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8964 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8965 finish = 1;
8966
8967 while (1)
8968 {
8969 constructor_index = p->index;
8970 constructor_fields = p->fields;
8971 if (finish && p->range_end && p->index == p->range_start)
8972 {
8973 finish = 0;
8974 p->prev = 0;
8975 }
8976 p = p->next;
8977 if (!p)
8978 break;
ea58ef42 8979 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
8980 p->stack = constructor_stack;
8981 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8982 p->index = p->range_start;
8983 }
8984
8985 if (!finish)
8986 constructor_range_stack = range_stack;
8987 continue;
8b6a5902
JJ
8988 }
8989
3e4093b6 8990 break;
8b6a5902
JJ
8991 }
8992
3e4093b6
RS
8993 constructor_range_stack = 0;
8994}
8995\f
9f0e2d86
ZW
8996/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8997 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8998 an ASM_EXPR node). */
3e4093b6 8999tree
9f0e2d86 9000build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9001{
6de9cd9a
DN
9002 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9003 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9004 return add_stmt (args);
8b6a5902
JJ
9005}
9006
9f0e2d86
ZW
9007/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9008 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9009 SIMPLE indicates whether there was anything at all after the
9010 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9011 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9012tree
c2255bc4 9013build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9014 tree clobbers, tree labels, bool simple)
e5e809f4 9015{
3e4093b6 9016 tree tail;
9f0e2d86 9017 tree args;
6de9cd9a
DN
9018 int i;
9019 const char *constraint;
74f0c611 9020 const char **oconstraints;
6de9cd9a 9021 bool allows_mem, allows_reg, is_inout;
74f0c611 9022 int ninputs, noutputs;
6de9cd9a
DN
9023
9024 ninputs = list_length (inputs);
9025 noutputs = list_length (outputs);
74f0c611
RH
9026 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9027
1c384bf1 9028 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9029
6de9cd9a
DN
9030 /* Remove output conversions that change the type but not the mode. */
9031 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9032 {
3e4093b6 9033 tree output = TREE_VALUE (tail);
74f0c611 9034
eadd3d0d
JJ
9035 output = c_fully_fold (output, false, NULL);
9036
74f0c611
RH
9037 /* ??? Really, this should not be here. Users should be using a
9038 proper lvalue, dammit. But there's a long history of using casts
9039 in the output operands. In cases like longlong.h, this becomes a
9040 primitive form of typechecking -- if the cast can be removed, then
9041 the output operand had a type of the proper width; otherwise we'll
9042 get an error. Gross, but ... */
3e4093b6 9043 STRIP_NOPS (output);
74f0c611 9044
7bd11157 9045 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9046 output = error_mark_node;
8b6a5902 9047
5544530a
PB
9048 if (output != error_mark_node
9049 && (TREE_READONLY (output)
9050 || TYPE_READONLY (TREE_TYPE (output))
9051 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9052 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9053 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9054 readonly_error (loc, output, lv_asm);
5544530a 9055
6de9cd9a 9056 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9057 oconstraints[i] = constraint;
9058
9059 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9060 &allows_mem, &allows_reg, &is_inout))
9061 {
9062 /* If the operand is going to end up in memory,
9063 mark it addressable. */
9064 if (!allows_reg && !c_mark_addressable (output))
9065 output = error_mark_node;
bae5cddf
JJ
9066 if (!(!allows_reg && allows_mem)
9067 && output != error_mark_node
9068 && VOID_TYPE_P (TREE_TYPE (output)))
9069 {
9070 error_at (loc, "invalid use of void expression");
9071 output = error_mark_node;
9072 }
74f0c611
RH
9073 }
9074 else
c22cacf3 9075 output = error_mark_node;
3e4093b6 9076
74f0c611 9077 TREE_VALUE (tail) = output;
8b6a5902 9078 }
3e4093b6 9079
74f0c611
RH
9080 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9081 {
9082 tree input;
9083
9084 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9085 input = TREE_VALUE (tail);
9086
74f0c611
RH
9087 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9088 oconstraints, &allows_mem, &allows_reg))
9089 {
9090 /* If the operand is going to end up in memory,
9091 mark it addressable. */
b4c33883
AP
9092 if (!allows_reg && allows_mem)
9093 {
eadd3d0d
JJ
9094 input = c_fully_fold (input, false, NULL);
9095
b4c33883
AP
9096 /* Strip the nops as we allow this case. FIXME, this really
9097 should be rejected or made deprecated. */
9098 STRIP_NOPS (input);
9099 if (!c_mark_addressable (input))
9100 input = error_mark_node;
bae5cddf 9101 }
eadd3d0d 9102 else
bae5cddf 9103 {
eadd3d0d
JJ
9104 struct c_expr expr;
9105 memset (&expr, 0, sizeof (expr));
9106 expr.value = input;
267bac10 9107 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9108 input = c_fully_fold (expr.value, false, NULL);
9109
9110 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9111 {
9112 error_at (loc, "invalid use of void expression");
9113 input = error_mark_node;
9114 }
bae5cddf 9115 }
74f0c611
RH
9116 }
9117 else
9118 input = error_mark_node;
9119
9120 TREE_VALUE (tail) = input;
9121 }
3e4093b6 9122
1c384bf1
RH
9123 /* ASMs with labels cannot have outputs. This should have been
9124 enforced by the parser. */
9125 gcc_assert (outputs == NULL || labels == NULL);
9126
9127 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9128
5544530a
PB
9129 /* asm statements without outputs, including simple ones, are treated
9130 as volatile. */
9131 ASM_INPUT_P (args) = simple;
9132 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9133
9f0e2d86 9134 return args;
e5e809f4 9135}
3e4093b6 9136\f
c2255bc4
AH
9137/* Generate a goto statement to LABEL. LOC is the location of the
9138 GOTO. */
506e2710
RH
9139
9140tree
c2255bc4 9141c_finish_goto_label (location_t loc, tree label)
506e2710 9142{
e1b7793c 9143 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9144 if (!decl)
9145 return NULL_TREE;
506e2710 9146 TREE_USED (decl) = 1;
c2255bc4
AH
9147 {
9148 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9149 SET_EXPR_LOCATION (t, loc);
9150 return add_stmt (t);
9151 }
506e2710
RH
9152}
9153
c2255bc4
AH
9154/* Generate a computed goto statement to EXPR. LOC is the location of
9155 the GOTO. */
506e2710
RH
9156
9157tree
c2255bc4 9158c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9159{
c2255bc4 9160 tree t;
c1771a20 9161 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9162 expr = c_fully_fold (expr, false, NULL);
506e2710 9163 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9164 t = build1 (GOTO_EXPR, void_type_node, expr);
9165 SET_EXPR_LOCATION (t, loc);
9166 return add_stmt (t);
506e2710
RH
9167}
9168
5088b058 9169/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9170 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9171 the location of the return statement, or the location of the expression,
9172 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9173 is the original type of RETVAL. */
de520661 9174
506e2710 9175tree
c2255bc4 9176c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9177{
0c9b182b
JJ
9178 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9179 bool no_warning = false;
928c19bb 9180 bool npc = false;
36536d79 9181 size_t rank = 0;
3e4093b6
RS
9182
9183 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9184 warning_at (loc, 0,
9185 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9186
b72271b9 9187 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9188 {
9189 /* Array notations are allowed in a return statement if it is inside a
9190 built-in array notation reduction function. */
9191 if (!find_rank (loc, retval, retval, false, &rank))
9192 return error_mark_node;
9193 if (rank >= 1)
9194 {
9195 error_at (loc, "array notation expression cannot be used as a "
9196 "return value");
9197 return error_mark_node;
9198 }
9199 }
3af9c5e9 9200 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9201 {
9202 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9203 "allowed");
9204 return error_mark_node;
9205 }
928c19bb
JM
9206 if (retval)
9207 {
8ce94e44 9208 tree semantic_type = NULL_TREE;
928c19bb 9209 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9210 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9211 {
9212 semantic_type = TREE_TYPE (retval);
9213 retval = TREE_OPERAND (retval, 0);
9214 }
928c19bb 9215 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9216 if (semantic_type)
9217 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9218 }
9219
3e4093b6 9220 if (!retval)
de520661 9221 {
3e4093b6
RS
9222 current_function_returns_null = 1;
9223 if ((warn_return_type || flag_isoc99)
9224 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9225 {
35aff4fb
MP
9226 if (flag_isoc99)
9227 pedwarn (loc, 0, "%<return%> with no value, in "
9228 "function returning non-void");
9229 else
9230 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9231 "in function returning non-void");
0c9b182b
JJ
9232 no_warning = true;
9233 }
400fbf9f 9234 }
3e4093b6 9235 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9236 {
3e4093b6 9237 current_function_returns_null = 1;
2397c575 9238 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9239 pedwarn (loc, 0,
509c9d60 9240 "%<return%> with a value, in function returning void");
b8698a0f 9241 else
c1771a20 9242 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9243 "%<return%> with expression, in function returning void");
de520661 9244 }
3e4093b6 9245 else
de520661 9246 {
68fca595
MP
9247 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9248 retval, origtype, ic_return,
c2255bc4 9249 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9250 tree res = DECL_RESULT (current_function_decl);
9251 tree inner;
9feb29df 9252 bool save;
3e4093b6
RS
9253
9254 current_function_returns_value = 1;
9255 if (t == error_mark_node)
506e2710 9256 return NULL_TREE;
3e4093b6 9257
9feb29df
JJ
9258 save = in_late_binary_op;
9259 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9260 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9261 in_late_binary_op = true;
3e4093b6 9262 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9263 in_late_binary_op = save;
3e4093b6
RS
9264
9265 /* Strip any conversions, additions, and subtractions, and see if
9266 we are returning the address of a local variable. Warn if so. */
9267 while (1)
8b6a5902 9268 {
3e4093b6 9269 switch (TREE_CODE (inner))
8b6a5902 9270 {
849421a3
JJ
9271 CASE_CONVERT:
9272 case NON_LVALUE_EXPR:
3e4093b6 9273 case PLUS_EXPR:
849421a3 9274 case POINTER_PLUS_EXPR:
3e4093b6
RS
9275 inner = TREE_OPERAND (inner, 0);
9276 continue;
9277
9278 case MINUS_EXPR:
9279 /* If the second operand of the MINUS_EXPR has a pointer
9280 type (or is converted from it), this may be valid, so
9281 don't give a warning. */
9282 {
9283 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9284
3f75a254 9285 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9286 && (CONVERT_EXPR_P (op1)
9287 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9288 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9289
3e4093b6
RS
9290 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9291 break;
8b6a5902 9292
3e4093b6
RS
9293 inner = TREE_OPERAND (inner, 0);
9294 continue;
9295 }
400fbf9f 9296
3e4093b6
RS
9297 case ADDR_EXPR:
9298 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9299
6615c446 9300 while (REFERENCE_CLASS_P (inner)
c22cacf3 9301 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9302 inner = TREE_OPERAND (inner, 0);
8b6a5902 9303
a2f1f4c3 9304 if (DECL_P (inner)
3f75a254
JM
9305 && !DECL_EXTERNAL (inner)
9306 && !TREE_STATIC (inner)
3e4093b6 9307 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9308 {
9309 if (TREE_CODE (inner) == LABEL_DECL)
9310 warning_at (loc, OPT_Wreturn_local_addr,
9311 "function returns address of label");
9312 else
b4dfdc11
MG
9313 {
9314 warning_at (loc, OPT_Wreturn_local_addr,
9315 "function returns address of local variable");
9316 tree zero = build_zero_cst (TREE_TYPE (res));
9317 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9318 }
19fc9faa 9319 }
3e4093b6 9320 break;
8b6a5902 9321
3e4093b6
RS
9322 default:
9323 break;
9324 }
de520661 9325
3e4093b6
RS
9326 break;
9327 }
9328
53fb4de3 9329 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9330 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9331
9332 if (warn_sequence_point)
9333 verify_sequence_points (retval);
de520661 9334 }
8b6a5902 9335
c2255bc4 9336 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9337 TREE_NO_WARNING (ret_stmt) |= no_warning;
9338 return add_stmt (ret_stmt);
de520661 9339}
3e4093b6
RS
9340\f
9341struct c_switch {
604f5adf
ILT
9342 /* The SWITCH_EXPR being built. */
9343 tree switch_expr;
a6c0a76c 9344
89dbed81 9345 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9346 default conversion is applied. */
9347 tree orig_type;
9348
3e4093b6
RS
9349 /* A splay-tree mapping the low element of a case range to the high
9350 element, or NULL_TREE if there is no high element. Used to
9351 determine whether or not a new case label duplicates an old case
9352 label. We need a tree, rather than simply a hash table, because
9353 of the GNU case range extension. */
9354 splay_tree cases;
a6c0a76c 9355
e1b7793c
ILT
9356 /* The bindings at the point of the switch. This is used for
9357 warnings crossing decls when branching to a case label. */
9358 struct c_spot_bindings *bindings;
187230a7 9359
3e4093b6
RS
9360 /* The next node on the stack. */
9361 struct c_switch *next;
9362};
400fbf9f 9363
3e4093b6
RS
9364/* A stack of the currently active switch statements. The innermost
9365 switch statement is on the top of the stack. There is no need to
9366 mark the stack for garbage collection because it is only active
9367 during the processing of the body of a function, and we never
9368 collect at that point. */
de520661 9369
506e2710 9370struct c_switch *c_switch_stack;
de520661 9371
3e4093b6 9372/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9373 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9374 SWITCH_COND_LOC is the location of the switch's condition.
9375 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9376
3e4093b6 9377tree
c2255bc4
AH
9378c_start_case (location_t switch_loc,
9379 location_t switch_cond_loc,
fedfecef 9380 tree exp, bool explicit_cast_p)
de520661 9381{
c58e8676 9382 tree orig_type = error_mark_node;
3e4093b6 9383 struct c_switch *cs;
2f6e4e97 9384
3e4093b6 9385 if (exp != error_mark_node)
de520661 9386 {
3e4093b6
RS
9387 orig_type = TREE_TYPE (exp);
9388
c58e8676 9389 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9390 {
c58e8676
VR
9391 if (orig_type != error_mark_node)
9392 {
c2255bc4 9393 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9394 orig_type = error_mark_node;
9395 }
3e4093b6 9396 exp = integer_zero_node;
de520661 9397 }
3e4093b6 9398 else
de520661 9399 {
c58e8676 9400 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9401 tree e = exp;
9402
9403 /* Warn if the condition has boolean value. */
9404 while (TREE_CODE (e) == COMPOUND_EXPR)
9405 e = TREE_OPERAND (e, 1);
9406
9407 if ((TREE_CODE (type) == BOOLEAN_TYPE
9408 || truth_value_p (TREE_CODE (e)))
9409 /* Explicit cast to int suppresses this warning. */
9410 && !(TREE_CODE (type) == INTEGER_TYPE
9411 && explicit_cast_p))
9412 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9413 "switch condition has boolean value");
8b6a5902 9414
8400e75e 9415 if (!in_system_header_at (input_location)
3e4093b6
RS
9416 && (type == long_integer_type_node
9417 || type == long_unsigned_type_node))
c2255bc4
AH
9418 warning_at (switch_cond_loc,
9419 OPT_Wtraditional, "%<long%> switch expression not "
9420 "converted to %<int%> in ISO C");
8b6a5902 9421
928c19bb 9422 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9423 exp = default_conversion (exp);
ca085fd7
MLI
9424
9425 if (warn_sequence_point)
9426 verify_sequence_points (exp);
3e4093b6
RS
9427 }
9428 }
9429
604f5adf 9430 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9431 cs = XNEW (struct c_switch);
604f5adf 9432 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9433 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9434 cs->orig_type = orig_type;
3e4093b6 9435 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9436 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9437 cs->next = c_switch_stack;
9438 c_switch_stack = cs;
3e4093b6 9439
604f5adf 9440 return add_stmt (cs->switch_expr);
3e4093b6
RS
9441}
9442
c2255bc4 9443/* Process a case label at location LOC. */
3e4093b6
RS
9444
9445tree
c2255bc4 9446do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9447{
9448 tree label = NULL_TREE;
9449
17cede2e
JM
9450 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9451 {
9452 low_value = c_fully_fold (low_value, false, NULL);
9453 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9454 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9455 "case label is not an integer constant expression");
9456 }
9457
9458 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9459 {
9460 high_value = c_fully_fold (high_value, false, NULL);
9461 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9462 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9463 "case label is not an integer constant expression");
9464 }
9465
e1b7793c 9466 if (c_switch_stack == NULL)
187230a7
JM
9467 {
9468 if (low_value)
e1b7793c 9469 error_at (loc, "case label not within a switch statement");
187230a7 9470 else
e1b7793c
ILT
9471 error_at (loc, "%<default%> label not within a switch statement");
9472 return NULL_TREE;
187230a7 9473 }
de520661 9474
e1b7793c
ILT
9475 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9476 EXPR_LOCATION (c_switch_stack->switch_expr),
9477 loc))
9478 return NULL_TREE;
9479
9480 label = c_add_case_label (loc, c_switch_stack->cases,
9481 SWITCH_COND (c_switch_stack->switch_expr),
9482 c_switch_stack->orig_type,
9483 low_value, high_value);
9484 if (label == error_mark_node)
9485 label = NULL_TREE;
3e4093b6
RS
9486 return label;
9487}
de520661 9488
3e4093b6 9489/* Finish the switch statement. */
de520661 9490
3e4093b6 9491void
325c3691 9492c_finish_case (tree body)
3e4093b6 9493{
506e2710 9494 struct c_switch *cs = c_switch_stack;
fbc315db 9495 location_t switch_location;
3e4093b6 9496
604f5adf 9497 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9498
6de9cd9a 9499 /* Emit warnings as needed. */
c2255bc4 9500 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
9501 c_do_switch_warnings (cs->cases, switch_location,
9502 TREE_TYPE (cs->switch_expr),
9503 SWITCH_COND (cs->switch_expr));
6de9cd9a 9504
3e4093b6 9505 /* Pop the stack. */
506e2710 9506 c_switch_stack = cs->next;
3e4093b6 9507 splay_tree_delete (cs->cases);
e1b7793c 9508 c_release_switch_bindings (cs->bindings);
5d038c4c 9509 XDELETE (cs);
de520661 9510}
325c3691 9511\f
506e2710
RH
9512/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9513 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9514 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9515 statement, and was not surrounded with parenthesis. */
325c3691 9516
9e51cf9d 9517void
506e2710
RH
9518c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9519 tree else_block, bool nested_if)
325c3691 9520{
506e2710 9521 tree stmt;
325c3691 9522
25c22937
BI
9523 /* If the condition has array notations, then the rank of the then_block and
9524 else_block must be either 0 or be equal to the rank of the condition. If
9525 the condition does not have array notations then break them up as it is
9526 broken up in a normal expression. */
b72271b9 9527 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9528 {
9529 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9530 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9531 return;
9532 if (then_block
9533 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9534 return;
9535 if (else_block
9536 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9537 return;
9538 if (cond_rank != then_rank && then_rank != 0)
9539 {
9540 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9541 " and the then-block");
9542 return;
9543 }
9544 else if (cond_rank != else_rank && else_rank != 0)
9545 {
9546 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9547 " and the else-block");
9548 return;
9549 }
9550 }
506e2710
RH
9551 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9552 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9553 {
506e2710 9554 tree inner_if = then_block;
16865eaa 9555
61ada8ae 9556 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9557 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9558 it might not be exactly THEN_BLOCK, but should be the last
9559 non-container statement within. */
9560 while (1)
9561 switch (TREE_CODE (inner_if))
9562 {
9563 case COND_EXPR:
9564 goto found;
9565 case BIND_EXPR:
9566 inner_if = BIND_EXPR_BODY (inner_if);
9567 break;
9568 case STATEMENT_LIST:
9569 inner_if = expr_last (then_block);
9570 break;
9571 case TRY_FINALLY_EXPR:
9572 case TRY_CATCH_EXPR:
9573 inner_if = TREE_OPERAND (inner_if, 0);
9574 break;
9575 default:
366de0ce 9576 gcc_unreachable ();
506e2710
RH
9577 }
9578 found:
16865eaa 9579
506e2710 9580 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9581 warning_at (if_locus, OPT_Wparentheses,
9582 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9583 }
16865eaa 9584
2214de30 9585 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9586 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9587 add_stmt (stmt);
325c3691
RH
9588}
9589
506e2710
RH
9590/* Emit a general-purpose loop construct. START_LOCUS is the location of
9591 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9592 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9593 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9594 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9595
9596void
506e2710
RH
9597c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9598 tree blab, tree clab, bool cond_is_first)
325c3691 9599{
506e2710
RH
9600 tree entry = NULL, exit = NULL, t;
9601
b72271b9 9602 if (flag_cilkplus && contains_array_notation_expr (cond))
36536d79
BI
9603 {
9604 error_at (start_locus, "array notation expression cannot be used in a "
9605 "loop%'s condition");
9606 return;
9607 }
9608
28af952a
RS
9609 /* If the condition is zero don't generate a loop construct. */
9610 if (cond && integer_zerop (cond))
9611 {
9612 if (cond_is_first)
9613 {
9614 t = build_and_jump (&blab);
9615 SET_EXPR_LOCATION (t, start_locus);
9616 add_stmt (t);
9617 }
9618 }
9619 else
506e2710
RH
9620 {
9621 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9622
506e2710 9623 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9624 out of the loop, or to the top of it. If there's no exit condition,
9625 then we just build a jump back to the top. */
506e2710 9626 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9627
28af952a 9628 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9629 {
9630 /* Canonicalize the loop condition to the end. This means
9631 generating a branch to the loop condition. Reuse the
9632 continue label, if possible. */
9633 if (cond_is_first)
9634 {
9635 if (incr || !clab)
9636 {
9637 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9638 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9639 }
9640 else
9641 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9642 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9643 add_stmt (t);
9644 }
9645
506e2710 9646 t = build_and_jump (&blab);
506e2710 9647 if (cond_is_first)
db3927fb
AH
9648 exit = fold_build3_loc (start_locus,
9649 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9650 else
db3927fb
AH
9651 exit = fold_build3_loc (input_location,
9652 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9653 }
9654
506e2710
RH
9655 add_stmt (top);
9656 }
c22cacf3 9657
506e2710
RH
9658 if (body)
9659 add_stmt (body);
9660 if (clab)
9661 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9662 if (incr)
9663 add_stmt (incr);
9664 if (entry)
9665 add_stmt (entry);
9666 if (exit)
9667 add_stmt (exit);
9668 if (blab)
9669 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9670}
325c3691
RH
9671
9672tree
c2255bc4 9673c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9674{
089efaa4 9675 bool skip;
506e2710 9676 tree label = *label_p;
325c3691 9677
089efaa4
ILT
9678 /* In switch statements break is sometimes stylistically used after
9679 a return statement. This can lead to spurious warnings about
9680 control reaching the end of a non-void function when it is
9681 inlined. Note that we are calling block_may_fallthru with
9682 language specific tree nodes; this works because
9683 block_may_fallthru returns true when given something it does not
9684 understand. */
9685 skip = !block_may_fallthru (cur_stmt_list);
9686
506e2710 9687 if (!label)
089efaa4
ILT
9688 {
9689 if (!skip)
c2255bc4 9690 *label_p = label = create_artificial_label (loc);
089efaa4 9691 }
953ff289
DN
9692 else if (TREE_CODE (label) == LABEL_DECL)
9693 ;
9694 else switch (TREE_INT_CST_LOW (label))
506e2710 9695 {
953ff289 9696 case 0:
506e2710 9697 if (is_break)
c2255bc4 9698 error_at (loc, "break statement not within loop or switch");
506e2710 9699 else
c2255bc4 9700 error_at (loc, "continue statement not within a loop");
506e2710 9701 return NULL_TREE;
953ff289
DN
9702
9703 case 1:
9704 gcc_assert (is_break);
c2255bc4 9705 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9706 return NULL_TREE;
9707
c02065fc
AH
9708 case 2:
9709 if (is_break)
9710 error ("break statement within %<#pragma simd%> loop body");
9711 else
9712 error ("continue statement within %<#pragma simd%> loop body");
9713 return NULL_TREE;
9714
953ff289
DN
9715 default:
9716 gcc_unreachable ();
506e2710 9717 }
325c3691 9718
089efaa4
ILT
9719 if (skip)
9720 return NULL_TREE;
9721
2e28e797
JH
9722 if (!is_break)
9723 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9724
53fb4de3 9725 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9726}
9727
506e2710 9728/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9729
9730static void
c2255bc4 9731emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9732{
e6b5a630
RH
9733 if (expr == error_mark_node)
9734 ;
9735 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9736 {
9737 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9738 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9739 }
789eadcd
MP
9740 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9741 {
9742 tree r = expr;
9743 location_t cloc = loc;
9744 while (TREE_CODE (r) == COMPOUND_EXPR)
9745 {
9746 if (EXPR_HAS_LOCATION (r))
9747 cloc = EXPR_LOCATION (r);
9748 r = TREE_OPERAND (r, 1);
9749 }
9750 if (!TREE_SIDE_EFFECTS (r)
9751 && !VOID_TYPE_P (TREE_TYPE (r))
9752 && !CONVERT_EXPR_P (r)
cc28fc7f 9753 && !TREE_NO_WARNING (r)
789eadcd
MP
9754 && !TREE_NO_WARNING (expr))
9755 warning_at (cloc, OPT_Wunused_value,
9756 "right-hand operand of comma expression has no effect");
9757 }
27f33b15 9758 else
c2255bc4 9759 warn_if_unused_value (expr, loc);
3a5b9284
RH
9760}
9761
506e2710 9762/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9763 diagnostics, but do not call ADD_STMT. LOC is the location of the
9764 statement. */
3a5b9284 9765
506e2710 9766tree
c2255bc4 9767c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9768{
056928b2
JJ
9769 tree exprv;
9770
3a5b9284 9771 if (!expr)
506e2710 9772 return NULL_TREE;
3a5b9284 9773
928c19bb
JM
9774 expr = c_fully_fold (expr, false, NULL);
9775
3a5b9284
RH
9776 if (warn_sequence_point)
9777 verify_sequence_points (expr);
9778
9779 if (TREE_TYPE (expr) != error_mark_node
9780 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9781 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9782 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9783
9784 /* If we're not processing a statement expression, warn about unused values.
9785 Warnings for statement expressions will be emitted later, once we figure
9786 out which is the result. */
9787 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9788 && warn_unused_value)
c2255bc4 9789 emit_side_effect_warnings (loc, expr);
3a5b9284 9790
056928b2
JJ
9791 exprv = expr;
9792 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9793 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9794 while (CONVERT_EXPR_P (exprv))
9795 exprv = TREE_OPERAND (exprv, 0);
9796 if (DECL_P (exprv)
9797 || handled_component_p (exprv)
9798 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9799 mark_exp_read (exprv);
fa8351f8 9800
3a5b9284
RH
9801 /* If the expression is not of a type to which we cannot assign a line
9802 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9803 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9804 {
9805 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9806 SET_EXPR_LOCATION (expr, loc);
9807 }
506e2710
RH
9808
9809 return expr;
9810}
9811
c2255bc4
AH
9812/* Emit an expression as a statement. LOC is the location of the
9813 expression. */
506e2710
RH
9814
9815tree
c2255bc4 9816c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9817{
9818 if (expr)
c2255bc4 9819 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9820 else
9821 return NULL;
3a5b9284
RH
9822}
9823
9824/* Do the opposite and emit a statement as an expression. To begin,
9825 create a new binding level and return it. */
325c3691
RH
9826
9827tree
9828c_begin_stmt_expr (void)
9829{
9830 tree ret;
9831
9832 /* We must force a BLOCK for this level so that, if it is not expanded
9833 later, there is a way to turn off the entire subtree of blocks that
9834 are contained in it. */
9835 keep_next_level ();
9836 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9837
9838 c_bindings_start_stmt_expr (c_switch_stack == NULL
9839 ? NULL
9840 : c_switch_stack->bindings);
325c3691
RH
9841
9842 /* Mark the current statement list as belonging to a statement list. */
9843 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9844
9845 return ret;
9846}
9847
c2255bc4
AH
9848/* LOC is the location of the compound statement to which this body
9849 belongs. */
9850
325c3691 9851tree
c2255bc4 9852c_finish_stmt_expr (location_t loc, tree body)
325c3691 9853{
3a5b9284 9854 tree last, type, tmp, val;
325c3691
RH
9855 tree *last_p;
9856
c2255bc4 9857 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9858
9859 c_bindings_end_stmt_expr (c_switch_stack == NULL
9860 ? NULL
9861 : c_switch_stack->bindings);
325c3691 9862
3a5b9284
RH
9863 /* Locate the last statement in BODY. See c_end_compound_stmt
9864 about always returning a BIND_EXPR. */
9865 last_p = &BIND_EXPR_BODY (body);
9866 last = BIND_EXPR_BODY (body);
9867
9868 continue_searching:
325c3691
RH
9869 if (TREE_CODE (last) == STATEMENT_LIST)
9870 {
3a5b9284
RH
9871 tree_stmt_iterator i;
9872
9873 /* This can happen with degenerate cases like ({ }). No value. */
9874 if (!TREE_SIDE_EFFECTS (last))
9875 return body;
9876
9877 /* If we're supposed to generate side effects warnings, process
9878 all of the statements except the last. */
27f33b15 9879 if (warn_unused_value)
325c3691 9880 {
3a5b9284 9881 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9882 {
9883 location_t tloc;
9884 tree t = tsi_stmt (i);
9885
9886 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9887 emit_side_effect_warnings (tloc, t);
9888 }
325c3691
RH
9889 }
9890 else
3a5b9284
RH
9891 i = tsi_last (last);
9892 last_p = tsi_stmt_ptr (i);
9893 last = *last_p;
325c3691
RH
9894 }
9895
3a5b9284
RH
9896 /* If the end of the list is exception related, then the list was split
9897 by a call to push_cleanup. Continue searching. */
9898 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9899 || TREE_CODE (last) == TRY_CATCH_EXPR)
9900 {
9901 last_p = &TREE_OPERAND (last, 0);
9902 last = *last_p;
9903 goto continue_searching;
9904 }
9905
26d8af35
JM
9906 if (last == error_mark_node)
9907 return last;
9908
3a5b9284
RH
9909 /* In the case that the BIND_EXPR is not necessary, return the
9910 expression out from inside it. */
26d8af35
JM
9911 if (last == BIND_EXPR_BODY (body)
9912 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9913 {
928c19bb
JM
9914 /* Even if this looks constant, do not allow it in a constant
9915 expression. */
e5a94231 9916 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9917 /* Do not warn if the return value of a statement expression is
9918 unused. */
928c19bb 9919 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9920 return last;
9921 }
325c3691
RH
9922
9923 /* Extract the type of said expression. */
9924 type = TREE_TYPE (last);
325c3691 9925
3a5b9284
RH
9926 /* If we're not returning a value at all, then the BIND_EXPR that
9927 we already have is a fine expression to return. */
9928 if (!type || VOID_TYPE_P (type))
9929 return body;
9930
9931 /* Now that we've located the expression containing the value, it seems
9932 silly to make voidify_wrapper_expr repeat the process. Create a
9933 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9934 tmp = create_tmp_var_raw (type, NULL);
9935
9936 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9937 tree_expr_nonnegative_p giving up immediately. */
9938 val = last;
9939 if (TREE_CODE (val) == NOP_EXPR
9940 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9941 val = TREE_OPERAND (val, 0);
9942
53fb4de3 9943 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9944 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9945
c2255bc4
AH
9946 {
9947 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9948 SET_EXPR_LOCATION (t, loc);
9949 return t;
9950 }
325c3691
RH
9951}
9952\f
9953/* Begin and end compound statements. This is as simple as pushing
9954 and popping new statement lists from the tree. */
9955
9956tree
9957c_begin_compound_stmt (bool do_scope)
9958{
9959 tree stmt = push_stmt_list ();
9960 if (do_scope)
4dfa0342 9961 push_scope ();
325c3691
RH
9962 return stmt;
9963}
9964
c2255bc4
AH
9965/* End a compound statement. STMT is the statement. LOC is the
9966 location of the compound statement-- this is usually the location
9967 of the opening brace. */
9968
325c3691 9969tree
c2255bc4 9970c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9971{
9972 tree block = NULL;
9973
9974 if (do_scope)
9975 {
9976 if (c_dialect_objc ())
9977 objc_clear_super_receiver ();
9978 block = pop_scope ();
9979 }
9980
9981 stmt = pop_stmt_list (stmt);
c2255bc4 9982 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9983
9984 /* If this compound statement is nested immediately inside a statement
9985 expression, then force a BIND_EXPR to be created. Otherwise we'll
9986 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9987 STATEMENT_LISTs merge, and thus we can lose track of what statement
9988 was really last. */
38e01f9e 9989 if (building_stmt_list_p ()
325c3691
RH
9990 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9991 && TREE_CODE (stmt) != BIND_EXPR)
9992 {
53fb4de3 9993 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9994 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9995 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9996 }
9997
9998 return stmt;
9999}
5a508662
RH
10000
10001/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10002 when the current scope is exited. EH_ONLY is true when this is not
10003 meant to apply to normal control flow transfer. */
10004
10005void
c2255bc4 10006push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10007{
3a5b9284
RH
10008 enum tree_code code;
10009 tree stmt, list;
10010 bool stmt_expr;
10011
10012 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10013 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10014 add_stmt (stmt);
3a5b9284
RH
10015 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10016 list = push_stmt_list ();
10017 TREE_OPERAND (stmt, 0) = list;
10018 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10019}
325c3691 10020\f
3e4093b6
RS
10021/* Build a binary-operation expression without default conversions.
10022 CODE is the kind of expression to build.
ba47d38d 10023 LOCATION is the operator's location.
3e4093b6
RS
10024 This function differs from `build' in several ways:
10025 the data type of the result is computed and recorded in it,
10026 warnings are generated if arg data types are invalid,
10027 special handling for addition and subtraction of pointers is known,
10028 and some optimization is done (operations on narrow ints
10029 are done in the narrower type when that gives the same result).
10030 Constant folding is also done before the result is returned.
de520661 10031
3e4093b6
RS
10032 Note that the operands will never have enumeral types, or function
10033 or array types, because either they will have the default conversions
10034 performed or they have both just been converted to some other type in which
10035 the arithmetic is to be done. */
10036
10037tree
ba47d38d
AH
10038build_binary_op (location_t location, enum tree_code code,
10039 tree orig_op0, tree orig_op1, int convert_p)
de520661 10040{
8ce94e44
JM
10041 tree type0, type1, orig_type0, orig_type1;
10042 tree eptype;
3e4093b6
RS
10043 enum tree_code code0, code1;
10044 tree op0, op1;
c9f9eb5d 10045 tree ret = error_mark_node;
4de67c26 10046 const char *invalid_op_diag;
4d84fe7c 10047 bool op0_int_operands, op1_int_operands;
928c19bb 10048 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10049
3e4093b6
RS
10050 /* Expression code to give to the expression when it is built.
10051 Normally this is CODE, which is what the caller asked for,
10052 but in some special cases we change it. */
10053 enum tree_code resultcode = code;
8b6a5902 10054
3e4093b6
RS
10055 /* Data type in which the computation is to be performed.
10056 In the simplest cases this is the common type of the arguments. */
10057 tree result_type = NULL;
10058
8ce94e44
JM
10059 /* When the computation is in excess precision, the type of the
10060 final EXCESS_PRECISION_EXPR. */
2d2e923f 10061 tree semantic_result_type = NULL;
8ce94e44 10062
3e4093b6
RS
10063 /* Nonzero means operands have already been type-converted
10064 in whatever way is necessary.
10065 Zero means they need to be converted to RESULT_TYPE. */
10066 int converted = 0;
10067
10068 /* Nonzero means create the expression with this type, rather than
10069 RESULT_TYPE. */
10070 tree build_type = 0;
10071
10072 /* Nonzero means after finally constructing the expression
10073 convert it to this type. */
10074 tree final_type = 0;
10075
10076 /* Nonzero if this is an operation like MIN or MAX which can
10077 safely be computed in short if both args are promoted shorts.
10078 Also implies COMMON.
10079 -1 indicates a bitwise operation; this makes a difference
10080 in the exact conditions for when it is safe to do the operation
10081 in a narrower mode. */
10082 int shorten = 0;
10083
10084 /* Nonzero if this is a comparison operation;
10085 if both args are promoted shorts, compare the original shorts.
10086 Also implies COMMON. */
10087 int short_compare = 0;
10088
10089 /* Nonzero if this is a right-shift operation, which can be computed on the
10090 original short and then promoted if the operand is a promoted short. */
10091 int short_shift = 0;
10092
10093 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10094 int common = 0;
10095
58393038
ZL
10096 /* True means types are compatible as far as ObjC is concerned. */
10097 bool objc_ok;
10098
8ce94e44
JM
10099 /* True means this is an arithmetic operation that may need excess
10100 precision. */
10101 bool may_need_excess_precision;
10102
180f8dbb
JM
10103 /* True means this is a boolean operation that converts both its
10104 operands to truth-values. */
10105 bool boolean_op = false;
10106
de5a5fa1
MP
10107 /* Remember whether we're doing / or %. */
10108 bool doing_div_or_mod = false;
10109
10110 /* Remember whether we're doing << or >>. */
10111 bool doing_shift = false;
10112
10113 /* Tree holding instrumentation expression. */
10114 tree instrument_expr = NULL;
10115
ba47d38d
AH
10116 if (location == UNKNOWN_LOCATION)
10117 location = input_location;
10118
4d84fe7c
JM
10119 op0 = orig_op0;
10120 op1 = orig_op1;
10121
10122 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10123 if (op0_int_operands)
10124 op0 = remove_c_maybe_const_expr (op0);
10125 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10126 if (op1_int_operands)
10127 op1 = remove_c_maybe_const_expr (op1);
10128 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10129 if (int_operands)
10130 {
10131 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10132 && TREE_CODE (orig_op1) == INTEGER_CST);
10133 int_const = (int_const_or_overflow
10134 && !TREE_OVERFLOW (orig_op0)
10135 && !TREE_OVERFLOW (orig_op1));
10136 }
10137 else
10138 int_const = int_const_or_overflow = false;
10139
0e3a99ae 10140 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10141 if (convert_p
10142 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10143 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10144 {
4d84fe7c
JM
10145 op0 = default_conversion (op0);
10146 op1 = default_conversion (op1);
790e9490
RS
10147 }
10148
36536d79
BI
10149 /* When Cilk Plus is enabled and there are array notations inside op0, then
10150 we check to see if there are builtin array notation functions. If
10151 so, then we take on the type of the array notation inside it. */
b72271b9 10152 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10153 orig_type0 = type0 = find_correct_array_notation_type (op0);
10154 else
10155 orig_type0 = type0 = TREE_TYPE (op0);
10156
b72271b9 10157 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10158 orig_type1 = type1 = find_correct_array_notation_type (op1);
10159 else
10160 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10161
3e4093b6
RS
10162 /* The expression codes of the data types of the arguments tell us
10163 whether the arguments are integers, floating, pointers, etc. */
10164 code0 = TREE_CODE (type0);
10165 code1 = TREE_CODE (type1);
10166
10167 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10168 STRIP_TYPE_NOPS (op0);
10169 STRIP_TYPE_NOPS (op1);
10170
10171 /* If an error was already reported for one of the arguments,
10172 avoid reporting another error. */
10173
10174 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10175 return error_mark_node;
10176
4de67c26
JM
10177 if ((invalid_op_diag
10178 = targetm.invalid_binary_op (code, type0, type1)))
10179 {
ba47d38d 10180 error_at (location, invalid_op_diag);
4de67c26
JM
10181 return error_mark_node;
10182 }
10183
8ce94e44
JM
10184 switch (code)
10185 {
10186 case PLUS_EXPR:
10187 case MINUS_EXPR:
10188 case MULT_EXPR:
10189 case TRUNC_DIV_EXPR:
10190 case CEIL_DIV_EXPR:
10191 case FLOOR_DIV_EXPR:
10192 case ROUND_DIV_EXPR:
10193 case EXACT_DIV_EXPR:
10194 may_need_excess_precision = true;
10195 break;
10196 default:
10197 may_need_excess_precision = false;
10198 break;
10199 }
10200 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10201 {
10202 op0 = TREE_OPERAND (op0, 0);
10203 type0 = TREE_TYPE (op0);
10204 }
10205 else if (may_need_excess_precision
10206 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10207 {
10208 type0 = eptype;
10209 op0 = convert (eptype, op0);
10210 }
10211 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10212 {
10213 op1 = TREE_OPERAND (op1, 0);
10214 type1 = TREE_TYPE (op1);
10215 }
10216 else if (may_need_excess_precision
10217 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10218 {
10219 type1 = eptype;
10220 op1 = convert (eptype, op1);
10221 }
10222
58393038
ZL
10223 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10224
0e3a99ae
AS
10225 /* In case when one of the operands of the binary operation is
10226 a vector and another is a scalar -- convert scalar to vector. */
10227 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10228 {
a212e43f
MG
10229 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10230 true);
f90e8e2e 10231
0e3a99ae
AS
10232 switch (convert_flag)
10233 {
10234 case stv_error:
10235 return error_mark_node;
10236 case stv_firstarg:
10237 {
10238 bool maybe_const = true;
10239 tree sc;
10240 sc = c_fully_fold (op0, false, &maybe_const);
10241 sc = save_expr (sc);
10242 sc = convert (TREE_TYPE (type1), sc);
10243 op0 = build_vector_from_val (type1, sc);
10244 if (!maybe_const)
10245 op0 = c_wrap_maybe_const (op0, true);
10246 orig_type0 = type0 = TREE_TYPE (op0);
10247 code0 = TREE_CODE (type0);
10248 converted = 1;
10249 break;
10250 }
10251 case stv_secondarg:
10252 {
10253 bool maybe_const = true;
10254 tree sc;
10255 sc = c_fully_fold (op1, false, &maybe_const);
10256 sc = save_expr (sc);
10257 sc = convert (TREE_TYPE (type0), sc);
10258 op1 = build_vector_from_val (type0, sc);
10259 if (!maybe_const)
54b9f838 10260 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10261 orig_type1 = type1 = TREE_TYPE (op1);
10262 code1 = TREE_CODE (type1);
10263 converted = 1;
10264 break;
10265 }
10266 default:
10267 break;
10268 }
10269 }
10270
3e4093b6 10271 switch (code)
de520661 10272 {
3e4093b6
RS
10273 case PLUS_EXPR:
10274 /* Handle the pointer + int case. */
10275 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10276 {
db3927fb 10277 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10278 goto return_build_binary_op;
10279 }
3e4093b6 10280 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10281 {
db3927fb 10282 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10283 goto return_build_binary_op;
10284 }
fe67cf58 10285 else
3e4093b6
RS
10286 common = 1;
10287 break;
400fbf9f 10288
3e4093b6
RS
10289 case MINUS_EXPR:
10290 /* Subtraction of two similar pointers.
10291 We must subtract them as integers, then divide by object size. */
10292 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10293 && comp_target_types (location, type0, type1))
c9f9eb5d 10294 {
db3927fb 10295 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10296 goto return_build_binary_op;
10297 }
3e4093b6
RS
10298 /* Handle pointer minus int. Just like pointer plus int. */
10299 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10300 {
db3927fb 10301 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10302 goto return_build_binary_op;
10303 }
3e4093b6
RS
10304 else
10305 common = 1;
10306 break;
8b6a5902 10307
3e4093b6
RS
10308 case MULT_EXPR:
10309 common = 1;
10310 break;
10311
10312 case TRUNC_DIV_EXPR:
10313 case CEIL_DIV_EXPR:
10314 case FLOOR_DIV_EXPR:
10315 case ROUND_DIV_EXPR:
10316 case EXACT_DIV_EXPR:
de5a5fa1 10317 doing_div_or_mod = true;
c9f9eb5d 10318 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10319
10320 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10321 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10322 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10323 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10324 || code1 == FIXED_POINT_TYPE
3e4093b6 10325 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10326 {
5bed876a
AH
10327 enum tree_code tcode0 = code0, tcode1 = code1;
10328
3a021db2 10329 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10330 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10331 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10332 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10333
ab22c1fa
CF
10334 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10335 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10336 resultcode = RDIV_EXPR;
10337 else
10338 /* Although it would be tempting to shorten always here, that
10339 loses on some targets, since the modulo instruction is
10340 undefined if the quotient can't be represented in the
10341 computation mode. We shorten only if unsigned or if
10342 dividing by something we know != -1. */
8df83eae 10343 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10344 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10345 && !integer_all_onesp (op1)));
3e4093b6
RS
10346 common = 1;
10347 }
10348 break;
de520661 10349
3e4093b6 10350 case BIT_AND_EXPR:
3e4093b6
RS
10351 case BIT_IOR_EXPR:
10352 case BIT_XOR_EXPR:
10353 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10354 shorten = -1;
9ef0c8d9
AP
10355 /* Allow vector types which are not floating point types. */
10356 else if (code0 == VECTOR_TYPE
10357 && code1 == VECTOR_TYPE
10358 && !VECTOR_FLOAT_TYPE_P (type0)
10359 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10360 common = 1;
10361 break;
10362
10363 case TRUNC_MOD_EXPR:
10364 case FLOOR_MOD_EXPR:
de5a5fa1 10365 doing_div_or_mod = true;
c9f9eb5d 10366 warn_for_div_by_zero (location, op1);
de520661 10367
5cfd5d9b
AP
10368 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10369 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10370 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10371 common = 1;
10372 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10373 {
10374 /* Although it would be tempting to shorten always here, that loses
10375 on some targets, since the modulo instruction is undefined if the
10376 quotient can't be represented in the computation mode. We shorten
10377 only if unsigned or if dividing by something we know != -1. */
8df83eae 10378 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10379 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10380 && !integer_all_onesp (op1)));
3e4093b6
RS
10381 common = 1;
10382 }
10383 break;
de520661 10384
3e4093b6
RS
10385 case TRUTH_ANDIF_EXPR:
10386 case TRUTH_ORIF_EXPR:
10387 case TRUTH_AND_EXPR:
10388 case TRUTH_OR_EXPR:
10389 case TRUTH_XOR_EXPR:
10390 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10391 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10392 || code0 == FIXED_POINT_TYPE)
3e4093b6 10393 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10394 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10395 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10396 {
10397 /* Result of these operations is always an int,
10398 but that does not mean the operands should be
10399 converted to ints! */
10400 result_type = integer_type_node;
a27d595d
JM
10401 if (op0_int_operands)
10402 {
10403 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10404 op0 = remove_c_maybe_const_expr (op0);
10405 }
10406 else
10407 op0 = c_objc_common_truthvalue_conversion (location, op0);
10408 if (op1_int_operands)
10409 {
10410 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10411 op1 = remove_c_maybe_const_expr (op1);
10412 }
10413 else
10414 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10415 converted = 1;
180f8dbb 10416 boolean_op = true;
3e4093b6 10417 }
928c19bb
JM
10418 if (code == TRUTH_ANDIF_EXPR)
10419 {
10420 int_const_or_overflow = (int_operands
10421 && TREE_CODE (orig_op0) == INTEGER_CST
10422 && (op0 == truthvalue_false_node
10423 || TREE_CODE (orig_op1) == INTEGER_CST));
10424 int_const = (int_const_or_overflow
10425 && !TREE_OVERFLOW (orig_op0)
10426 && (op0 == truthvalue_false_node
10427 || !TREE_OVERFLOW (orig_op1)));
10428 }
10429 else if (code == TRUTH_ORIF_EXPR)
10430 {
10431 int_const_or_overflow = (int_operands
10432 && TREE_CODE (orig_op0) == INTEGER_CST
10433 && (op0 == truthvalue_true_node
10434 || TREE_CODE (orig_op1) == INTEGER_CST));
10435 int_const = (int_const_or_overflow
10436 && !TREE_OVERFLOW (orig_op0)
10437 && (op0 == truthvalue_true_node
10438 || !TREE_OVERFLOW (orig_op1)));
10439 }
3e4093b6 10440 break;
eba80994 10441
3e4093b6
RS
10442 /* Shift operations: result has same type as first operand;
10443 always convert second operand to int.
10444 Also set SHORT_SHIFT if shifting rightward. */
de520661 10445
3e4093b6 10446 case RSHIFT_EXPR:
f87bd04b
AS
10447 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10448 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10449 {
10450 result_type = type0;
10451 converted = 1;
10452 }
10453 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10454 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10455 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10456 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10457 {
10458 result_type = type0;
10459 converted = 1;
10460 }
10461 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10462 && code1 == INTEGER_TYPE)
3e4093b6 10463 {
de5a5fa1 10464 doing_shift = true;
928c19bb 10465 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10466 {
3e4093b6 10467 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10468 {
10469 int_const = false;
7d882b83 10470 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f 10471 warning_at (location, 0, "right shift count is negative");
928c19bb 10472 }
3e4093b6 10473 else
bbb818c6 10474 {
3f75a254 10475 if (!integer_zerop (op1))
3e4093b6
RS
10476 short_shift = 1;
10477
10478 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10479 {
10480 int_const = false;
7d882b83 10481 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f
MP
10482 warning_at (location, 0, "right shift count >= width "
10483 "of type");
928c19bb 10484 }
bbb818c6 10485 }
b62acd60 10486 }
de520661 10487
3e4093b6
RS
10488 /* Use the type of the value to be shifted. */
10489 result_type = type0;
f87bd04b
AS
10490 /* Convert the non vector shift-count to an integer, regardless
10491 of size of value being shifted. */
10492 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10493 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10494 op1 = convert (integer_type_node, op1);
10495 /* Avoid converting op1 to result_type later. */
10496 converted = 1;
400fbf9f 10497 }
3e4093b6 10498 break;
253b6b82 10499
3e4093b6 10500 case LSHIFT_EXPR:
f87bd04b
AS
10501 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10502 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10503 {
10504 result_type = type0;
10505 converted = 1;
10506 }
10507 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10508 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10509 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10510 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10511 {
10512 result_type = type0;
10513 converted = 1;
10514 }
10515 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10516 && code1 == INTEGER_TYPE)
3e4093b6 10517 {
de5a5fa1 10518 doing_shift = true;
928c19bb 10519 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10520 {
3e4093b6 10521 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10522 {
10523 int_const = false;
7d882b83 10524 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f 10525 warning_at (location, 0, "left shift count is negative");
928c19bb 10526 }
de520661 10527
3e4093b6 10528 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10529 {
10530 int_const = false;
7d882b83 10531 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f
MP
10532 warning_at (location, 0, "left shift count >= width of "
10533 "type");
928c19bb 10534 }
94ba5069 10535 }
de520661 10536
3e4093b6
RS
10537 /* Use the type of the value to be shifted. */
10538 result_type = type0;
f87bd04b
AS
10539 /* Convert the non vector shift-count to an integer, regardless
10540 of size of value being shifted. */
10541 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10542 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10543 op1 = convert (integer_type_node, op1);
10544 /* Avoid converting op1 to result_type later. */
10545 converted = 1;
400fbf9f 10546 }
3e4093b6 10547 break;
de520661 10548
3e4093b6
RS
10549 case EQ_EXPR:
10550 case NE_EXPR:
d246ab4f
AS
10551 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10552 {
10553 tree intt;
0af94e6f 10554 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10555 {
10556 error_at (location, "comparing vectors with different "
10557 "element types");
10558 return error_mark_node;
10559 }
10560
10561 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10562 {
10563 error_at (location, "comparing vectors with different "
10564 "number of elements");
10565 return error_mark_node;
10566 }
10567
10568 /* Always construct signed integer vector type. */
10569 intt = c_common_type_for_size (GET_MODE_BITSIZE
10570 (TYPE_MODE (TREE_TYPE (type0))), 0);
10571 result_type = build_opaque_vector_type (intt,
10572 TYPE_VECTOR_SUBPARTS (type0));
10573 converted = 1;
10574 break;
10575 }
ae311566 10576 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10577 warning_at (location,
10578 OPT_Wfloat_equal,
10579 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10580 /* Result of comparison is always int,
10581 but don't convert the args to int! */
10582 build_type = integer_type_node;
10583 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10584 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10585 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10586 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10587 short_compare = 1;
637f1455
SZ
10588 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10589 {
10590 if (TREE_CODE (op0) == ADDR_EXPR
10591 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10592 {
10593 if (code == EQ_EXPR)
10594 warning_at (location,
10595 OPT_Waddress,
10596 "the comparison will always evaluate as %<false%> "
10597 "for the address of %qD will never be NULL",
10598 TREE_OPERAND (op0, 0));
10599 else
10600 warning_at (location,
10601 OPT_Waddress,
10602 "the comparison will always evaluate as %<true%> "
10603 "for the address of %qD will never be NULL",
10604 TREE_OPERAND (op0, 0));
10605 }
10606 result_type = type0;
10607 }
10608 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10609 {
10610 if (TREE_CODE (op1) == ADDR_EXPR
10611 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10612 {
10613 if (code == EQ_EXPR)
10614 warning_at (location,
f90e8e2e 10615 OPT_Waddress,
637f1455
SZ
10616 "the comparison will always evaluate as %<false%> "
10617 "for the address of %qD will never be NULL",
10618 TREE_OPERAND (op1, 0));
10619 else
10620 warning_at (location,
10621 OPT_Waddress,
10622 "the comparison will always evaluate as %<true%> "
10623 "for the address of %qD will never be NULL",
10624 TREE_OPERAND (op1, 0));
10625 }
10626 result_type = type1;
10627 }
3e4093b6
RS
10628 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10629 {
10630 tree tt0 = TREE_TYPE (type0);
10631 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10632 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10633 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10634 addr_space_t as_common = ADDR_SPACE_GENERIC;
10635
3e4093b6
RS
10636 /* Anything compares with void *. void * compares with anything.
10637 Otherwise, the targets must be compatible
10638 and both must be object or both incomplete. */
744aa42f 10639 if (comp_target_types (location, type0, type1))
10bc1b1b 10640 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10641 else if (!addr_space_superset (as0, as1, &as_common))
10642 {
10643 error_at (location, "comparison of pointers to "
10644 "disjoint address spaces");
10645 return error_mark_node;
10646 }
267bac10 10647 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10648 {
36c5e70a 10649 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10650 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10651 "comparison of %<void *%> with function pointer");
ee2990e7 10652 }
267bac10 10653 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10654 {
36c5e70a 10655 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10656 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10657 "comparison of %<void *%> with function pointer");
e6834654 10658 }
3e4093b6 10659 else
58393038
ZL
10660 /* Avoid warning about the volatile ObjC EH puts on decls. */
10661 if (!objc_ok)
ba47d38d 10662 pedwarn (location, 0,
509c9d60 10663 "comparison of distinct pointer types lacks a cast");
e6834654 10664
3e4093b6 10665 if (result_type == NULL_TREE)
36c5e70a
BE
10666 {
10667 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10668 result_type = build_pointer_type
10669 (build_qualified_type (void_type_node, qual));
10670 }
e6834654 10671 }
3e4093b6 10672 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10673 {
3e4093b6 10674 result_type = type0;
ba47d38d 10675 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10676 }
3e4093b6 10677 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10678 {
3e4093b6 10679 result_type = type1;
ba47d38d 10680 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10681 }
04159acf
MP
10682 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10683 || truth_value_p (TREE_CODE (orig_op0)))
10684 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10685 || truth_value_p (TREE_CODE (orig_op1))))
10686 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10687 break;
8b6a5902 10688
3e4093b6
RS
10689 case LE_EXPR:
10690 case GE_EXPR:
10691 case LT_EXPR:
10692 case GT_EXPR:
d246ab4f
AS
10693 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10694 {
10695 tree intt;
0af94e6f 10696 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10697 {
10698 error_at (location, "comparing vectors with different "
10699 "element types");
10700 return error_mark_node;
10701 }
10702
10703 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10704 {
10705 error_at (location, "comparing vectors with different "
10706 "number of elements");
10707 return error_mark_node;
10708 }
10709
10710 /* Always construct signed integer vector type. */
10711 intt = c_common_type_for_size (GET_MODE_BITSIZE
10712 (TYPE_MODE (TREE_TYPE (type0))), 0);
10713 result_type = build_opaque_vector_type (intt,
10714 TYPE_VECTOR_SUBPARTS (type0));
10715 converted = 1;
10716 break;
10717 }
3e4093b6 10718 build_type = integer_type_node;
ab22c1fa
CF
10719 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10720 || code0 == FIXED_POINT_TYPE)
10721 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10722 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10723 short_compare = 1;
10724 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10725 {
36c5e70a
BE
10726 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10727 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10728 addr_space_t as_common;
10729
744aa42f 10730 if (comp_target_types (location, type0, type1))
3e4093b6 10731 {
10bc1b1b 10732 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10733 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10734 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10735 pedwarn (location, 0,
509c9d60 10736 "comparison of complete and incomplete pointers");
fcf73884 10737 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10738 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10739 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10740 else if (null_pointer_constant_p (orig_op0)
10741 || null_pointer_constant_p (orig_op1))
10742 warning_at (location, OPT_Wextra,
10743 "ordered comparison of pointer with null pointer");
10744
3e4093b6 10745 }
36c5e70a
BE
10746 else if (!addr_space_superset (as0, as1, &as_common))
10747 {
10748 error_at (location, "comparison of pointers to "
10749 "disjoint address spaces");
10750 return error_mark_node;
10751 }
3e4093b6
RS
10752 else
10753 {
36c5e70a
BE
10754 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10755 result_type = build_pointer_type
10756 (build_qualified_type (void_type_node, qual));
ba47d38d 10757 pedwarn (location, 0,
509c9d60 10758 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10759 }
10760 }
6aa3c60d 10761 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10762 {
10763 result_type = type0;
fcf73884 10764 if (pedantic)
c1771a20 10765 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10766 "ordered comparison of pointer with integer zero");
10767 else if (extra_warnings)
ba47d38d 10768 warning_at (location, OPT_Wextra,
d42ba3b8 10769 "ordered comparison of pointer with integer zero");
3e4093b6 10770 }
6aa3c60d 10771 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10772 {
10773 result_type = type1;
d42ba3b8 10774 if (pedantic)
c1771a20 10775 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10776 "ordered comparison of pointer with integer zero");
10777 else if (extra_warnings)
10778 warning_at (location, OPT_Wextra,
10779 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10780 }
10781 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10782 {
10783 result_type = type0;
ba47d38d 10784 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10785 }
10786 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10787 {
10788 result_type = type1;
ba47d38d 10789 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10790 }
04159acf
MP
10791 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10792 || truth_value_p (TREE_CODE (orig_op0)))
10793 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10794 || truth_value_p (TREE_CODE (orig_op1))))
10795 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10796 break;
64094f6a 10797
3e4093b6 10798 default:
37b2f290 10799 gcc_unreachable ();
c9fe6f9f 10800 }
8f17b5c5 10801
e57e265b
PB
10802 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10803 return error_mark_node;
10804
5bed876a
AH
10805 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10806 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10807 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10808 {
ba47d38d 10809 binary_op_error (location, code, type0, type1);
5bed876a
AH
10810 return error_mark_node;
10811 }
10812
3e4093b6 10813 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10814 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10815 &&
10816 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10817 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10818 {
2ca862e9
JM
10819 bool first_complex = (code0 == COMPLEX_TYPE);
10820 bool second_complex = (code1 == COMPLEX_TYPE);
10821 int none_complex = (!first_complex && !second_complex);
39b726dd 10822
3e4093b6 10823 if (shorten || common || short_compare)
3bf6bfcc
JJ
10824 {
10825 result_type = c_common_type (type0, type1);
0a0b3574
MM
10826 do_warn_double_promotion (result_type, type0, type1,
10827 "implicit conversion from %qT to %qT "
10828 "to match other operand of binary "
10829 "expression",
10830 location);
3bf6bfcc
JJ
10831 if (result_type == error_mark_node)
10832 return error_mark_node;
10833 }
400fbf9f 10834
2ca862e9
JM
10835 if (first_complex != second_complex
10836 && (code == PLUS_EXPR
10837 || code == MINUS_EXPR
10838 || code == MULT_EXPR
10839 || (code == TRUNC_DIV_EXPR && first_complex))
10840 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10841 && flag_signed_zeros)
10842 {
10843 /* An operation on mixed real/complex operands must be
10844 handled specially, but the language-independent code can
10845 more easily optimize the plain complex arithmetic if
10846 -fno-signed-zeros. */
10847 tree real_type = TREE_TYPE (result_type);
10848 tree real, imag;
10849 if (type0 != orig_type0 || type1 != orig_type1)
10850 {
10851 gcc_assert (may_need_excess_precision && common);
2d2e923f 10852 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10853 }
10854 if (first_complex)
10855 {
10856 if (TREE_TYPE (op0) != result_type)
68fca595 10857 op0 = convert_and_check (location, result_type, op0);
2ca862e9 10858 if (TREE_TYPE (op1) != real_type)
68fca595 10859 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
10860 }
10861 else
10862 {
10863 if (TREE_TYPE (op0) != real_type)
68fca595 10864 op0 = convert_and_check (location, real_type, op0);
2ca862e9 10865 if (TREE_TYPE (op1) != result_type)
68fca595 10866 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
10867 }
10868 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10869 return error_mark_node;
10870 if (first_complex)
10871 {
10872 op0 = c_save_expr (op0);
10873 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10874 op0, 1);
10875 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10876 op0, 1);
10877 switch (code)
10878 {
10879 case MULT_EXPR:
10880 case TRUNC_DIV_EXPR:
c4603e7c 10881 op1 = c_save_expr (op1);
2ca862e9
JM
10882 imag = build2 (resultcode, real_type, imag, op1);
10883 /* Fall through. */
10884 case PLUS_EXPR:
10885 case MINUS_EXPR:
10886 real = build2 (resultcode, real_type, real, op1);
10887 break;
10888 default:
10889 gcc_unreachable();
10890 }
10891 }
10892 else
10893 {
10894 op1 = c_save_expr (op1);
10895 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10896 op1, 1);
10897 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10898 op1, 1);
10899 switch (code)
10900 {
10901 case MULT_EXPR:
c4603e7c 10902 op0 = c_save_expr (op0);
2ca862e9
JM
10903 imag = build2 (resultcode, real_type, op0, imag);
10904 /* Fall through. */
10905 case PLUS_EXPR:
10906 real = build2 (resultcode, real_type, op0, real);
10907 break;
10908 case MINUS_EXPR:
10909 real = build2 (resultcode, real_type, op0, real);
10910 imag = build1 (NEGATE_EXPR, real_type, imag);
10911 break;
10912 default:
10913 gcc_unreachable();
10914 }
10915 }
10916 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10917 goto return_build_binary_op;
10918 }
10919
3e4093b6
RS
10920 /* For certain operations (which identify themselves by shorten != 0)
10921 if both args were extended from the same smaller type,
10922 do the arithmetic in that type and then extend.
400fbf9f 10923
3e4093b6
RS
10924 shorten !=0 and !=1 indicates a bitwise operation.
10925 For them, this optimization is safe only if
10926 both args are zero-extended or both are sign-extended.
10927 Otherwise, we might change the result.
10928 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10929 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10930
3e4093b6
RS
10931 if (shorten && none_complex)
10932 {
3e4093b6 10933 final_type = result_type;
b8698a0f 10934 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10935 shorten == -1);
3e4093b6 10936 }
88a3dbc1 10937
3e4093b6 10938 /* Shifts can be shortened if shifting right. */
2f6e4e97 10939
3e4093b6
RS
10940 if (short_shift)
10941 {
10942 int unsigned_arg;
10943 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10944
3e4093b6 10945 final_type = result_type;
abe80e6d 10946
3e4093b6 10947 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10948 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10949
3e4093b6 10950 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10951 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10952 /* We can shorten only if the shift count is less than the
10953 number of bits in the smaller type size. */
10954 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10955 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10956 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10957 {
10958 /* Do an unsigned shift if the operand was zero-extended. */
10959 result_type
10960 = c_common_signed_or_unsigned_type (unsigned_arg,
10961 TREE_TYPE (arg0));
10962 /* Convert value-to-be-shifted to that type. */
10963 if (TREE_TYPE (op0) != result_type)
10964 op0 = convert (result_type, op0);
10965 converted = 1;
abe80e6d 10966 }
88a3dbc1
RK
10967 }
10968
3e4093b6
RS
10969 /* Comparison operations are shortened too but differently.
10970 They identify themselves by setting short_compare = 1. */
56cb9733 10971
3e4093b6
RS
10972 if (short_compare)
10973 {
10974 /* Don't write &op0, etc., because that would prevent op0
10975 from being kept in a register.
10976 Instead, make copies of the our local variables and
10977 pass the copies by reference, then copy them back afterward. */
10978 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10979 enum tree_code xresultcode = resultcode;
10980 tree val
393e8e8b
MP
10981 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10982 &xresultcode);
8f17b5c5 10983
3e4093b6 10984 if (val != 0)
c9f9eb5d
AH
10985 {
10986 ret = val;
10987 goto return_build_binary_op;
10988 }
8f17b5c5 10989
3e4093b6
RS
10990 op0 = xop0, op1 = xop1;
10991 converted = 1;
10992 resultcode = xresultcode;
8f17b5c5 10993
7d882b83 10994 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10995 {
10996 bool op0_maybe_const = true;
10997 bool op1_maybe_const = true;
10998 tree orig_op0_folded, orig_op1_folded;
10999
11000 if (in_late_binary_op)
11001 {
11002 orig_op0_folded = orig_op0;
11003 orig_op1_folded = orig_op1;
11004 }
11005 else
11006 {
11007 /* Fold for the sake of possible warnings, as in
11008 build_conditional_expr. This requires the
11009 "original" values to be folded, not just op0 and
11010 op1. */
f5178456 11011 c_inhibit_evaluation_warnings++;
928c19bb
JM
11012 op0 = c_fully_fold (op0, require_constant_value,
11013 &op0_maybe_const);
11014 op1 = c_fully_fold (op1, require_constant_value,
11015 &op1_maybe_const);
f5178456 11016 c_inhibit_evaluation_warnings--;
928c19bb
JM
11017 orig_op0_folded = c_fully_fold (orig_op0,
11018 require_constant_value,
11019 NULL);
11020 orig_op1_folded = c_fully_fold (orig_op1,
11021 require_constant_value,
11022 NULL);
11023 }
11024
11025 if (warn_sign_compare)
11026 warn_for_sign_compare (location, orig_op0_folded,
11027 orig_op1_folded, op0, op1,
11028 result_type, resultcode);
5c2f94b4 11029 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11030 {
11031 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11032 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11033 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11034 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11035 }
3e4093b6 11036 }
2ad1815d 11037 }
64094f6a 11038 }
64094f6a 11039
3e4093b6
RS
11040 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11041 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11042 Then the expression will be built.
11043 It will be given type FINAL_TYPE if that is nonzero;
11044 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11045
3e4093b6
RS
11046 if (!result_type)
11047 {
ba47d38d 11048 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11049 return error_mark_node;
11050 }
400fbf9f 11051
3e4093b6 11052 if (build_type == NULL_TREE)
8ce94e44
JM
11053 {
11054 build_type = result_type;
180f8dbb
JM
11055 if ((type0 != orig_type0 || type1 != orig_type1)
11056 && !boolean_op)
8ce94e44
JM
11057 {
11058 gcc_assert (may_need_excess_precision && common);
2d2e923f 11059 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11060 }
11061 }
400fbf9f 11062
2d2e923f
MLI
11063 if (!converted)
11064 {
68fca595
MP
11065 op0 = ep_convert_and_check (location, result_type, op0,
11066 semantic_result_type);
11067 op1 = ep_convert_and_check (location, result_type, op1,
11068 semantic_result_type);
2d2e923f
MLI
11069
11070 /* This can happen if one operand has a vector type, and the other
11071 has a different type. */
11072 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11073 return error_mark_node;
11074 }
11075
f8ed5150
MP
11076 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11077 | SANITIZE_FLOAT_DIVIDE))
de5a5fa1 11078 && current_function_decl != 0
ce6923c5
MP
11079 && !lookup_attribute ("no_sanitize_undefined",
11080 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
11081 && (doing_div_or_mod || doing_shift))
11082 {
11083 /* OP0 and/or OP1 might have side-effects. */
11084 op0 = c_save_expr (op0);
11085 op1 = c_save_expr (op1);
11086 op0 = c_fully_fold (op0, false, NULL);
11087 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11088 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11089 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11090 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11091 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11092 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11093 }
11094
c9f9eb5d 11095 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11096 if (int_const_or_overflow)
11097 ret = (require_constant_value
db3927fb
AH
11098 ? fold_build2_initializer_loc (location, resultcode, build_type,
11099 op0, op1)
11100 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11101 else
11102 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11103 if (final_type != 0)
11104 ret = convert (final_type, ret);
11105
11106 return_build_binary_op:
11107 gcc_assert (ret != error_mark_node);
928c19bb
JM
11108 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11109 ret = (int_operands
11110 ? note_integer_operands (ret)
11111 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11112 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11113 && !in_late_binary_op)
11114 ret = note_integer_operands (ret);
2d2e923f
MLI
11115 if (semantic_result_type)
11116 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11117 protected_set_expr_location (ret, location);
de5a5fa1 11118
a24d975c 11119 if (instrument_expr != NULL)
de5a5fa1
MP
11120 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11121 instrument_expr, ret);
11122
c9f9eb5d 11123 return ret;
400fbf9f 11124}
85498824
JM
11125
11126
11127/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11128 purpose. LOCATION is the source location for the expression. */
85498824
JM
11129
11130tree
ba47d38d 11131c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11132{
928c19bb
JM
11133 bool int_const, int_operands;
11134
85498824
JM
11135 switch (TREE_CODE (TREE_TYPE (expr)))
11136 {
11137 case ARRAY_TYPE:
ba47d38d 11138 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11139 return error_mark_node;
11140
11141 case RECORD_TYPE:
ba47d38d 11142 error_at (location, "used struct type value where scalar is required");
85498824
JM
11143 return error_mark_node;
11144
11145 case UNION_TYPE:
ba47d38d 11146 error_at (location, "used union type value where scalar is required");
85498824
JM
11147 return error_mark_node;
11148
04af8788
NP
11149 case VOID_TYPE:
11150 error_at (location, "void value not ignored as it ought to be");
11151 return error_mark_node;
11152
46bdb9cf
JM
11153 case FUNCTION_TYPE:
11154 gcc_unreachable ();
11155
d246ab4f
AS
11156 case VECTOR_TYPE:
11157 error_at (location, "used vector type where scalar is required");
11158 return error_mark_node;
11159
85498824
JM
11160 default:
11161 break;
11162 }
11163
928c19bb
JM
11164 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11165 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11166 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11167 {
11168 expr = remove_c_maybe_const_expr (expr);
11169 expr = build2 (NE_EXPR, integer_type_node, expr,
11170 convert (TREE_TYPE (expr), integer_zero_node));
11171 expr = note_integer_operands (expr);
11172 }
11173 else
11174 /* ??? Should we also give an error for vectors rather than leaving
11175 those to give errors later? */
11176 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11177
11178 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11179 {
11180 if (TREE_OVERFLOW (expr))
11181 return expr;
11182 else
11183 return note_integer_operands (expr);
11184 }
11185 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11186 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11187 return expr;
85498824 11188}
73f397d4
JM
11189\f
11190
11191/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11192 required. */
11193
11194tree
51eed280 11195c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11196{
11197 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11198 {
11199 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11200 /* Executing a compound literal inside a function reinitializes
11201 it. */
11202 if (!TREE_STATIC (decl))
11203 *se = true;
11204 return decl;
11205 }
11206 else
11207 return expr;
11208}
953ff289 11209\f
c0220ea4 11210/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11211
11212tree
11213c_begin_omp_parallel (void)
11214{
11215 tree block;
11216
11217 keep_next_level ();
11218 block = c_begin_compound_stmt (true);
11219
11220 return block;
11221}
11222
c2255bc4
AH
11223/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11224 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11225
953ff289 11226tree
c2255bc4 11227c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11228{
11229 tree stmt;
11230
c2255bc4 11231 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11232
11233 stmt = make_node (OMP_PARALLEL);
11234 TREE_TYPE (stmt) = void_type_node;
11235 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11236 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11237 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11238
11239 return add_stmt (stmt);
11240}
11241
a68ab351
JJ
11242/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11243
11244tree
11245c_begin_omp_task (void)
11246{
11247 tree block;
11248
11249 keep_next_level ();
11250 block = c_begin_compound_stmt (true);
11251
11252 return block;
11253}
11254
c2255bc4
AH
11255/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11256 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11257
11258tree
c2255bc4 11259c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11260{
11261 tree stmt;
11262
c2255bc4 11263 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11264
11265 stmt = make_node (OMP_TASK);
11266 TREE_TYPE (stmt) = void_type_node;
11267 OMP_TASK_CLAUSES (stmt) = clauses;
11268 OMP_TASK_BODY (stmt) = block;
c2255bc4 11269 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11270
11271 return add_stmt (stmt);
11272}
11273
acf0174b
JJ
11274/* Generate GOMP_cancel call for #pragma omp cancel. */
11275
11276void
11277c_finish_omp_cancel (location_t loc, tree clauses)
11278{
11279 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11280 int mask = 0;
11281 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11282 mask = 1;
11283 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11284 mask = 2;
11285 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11286 mask = 4;
11287 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11288 mask = 8;
11289 else
11290 {
11291 error_at (loc, "%<#pragma omp cancel must specify one of "
11292 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11293 "clauses");
11294 return;
11295 }
11296 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11297 if (ifc != NULL_TREE)
11298 {
11299 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11300 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11301 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11302 build_zero_cst (type));
11303 }
11304 else
11305 ifc = boolean_true_node;
11306 tree stmt = build_call_expr_loc (loc, fn, 2,
11307 build_int_cst (integer_type_node, mask),
11308 ifc);
11309 add_stmt (stmt);
11310}
11311
11312/* Generate GOMP_cancellation_point call for
11313 #pragma omp cancellation point. */
11314
11315void
11316c_finish_omp_cancellation_point (location_t loc, tree clauses)
11317{
11318 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11319 int mask = 0;
11320 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11321 mask = 1;
11322 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11323 mask = 2;
11324 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11325 mask = 4;
11326 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11327 mask = 8;
11328 else
11329 {
11330 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11331 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11332 "clauses");
11333 return;
11334 }
11335 tree stmt = build_call_expr_loc (loc, fn, 1,
11336 build_int_cst (integer_type_node, mask));
11337 add_stmt (stmt);
11338}
11339
11340/* Helper function for handle_omp_array_sections. Called recursively
11341 to handle multiple array-section-subscripts. C is the clause,
11342 T current expression (initially OMP_CLAUSE_DECL), which is either
11343 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11344 expression if specified, TREE_VALUE length expression if specified,
11345 TREE_CHAIN is what it has been specified after, or some decl.
11346 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11347 set to true if any of the array-section-subscript could have length
11348 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11349 first array-section-subscript which is known not to have length
11350 of one. Given say:
11351 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11352 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11353 all are or may have length of 1, array-section-subscript [:2] is the
11354 first one knonwn not to have length 1. For array-section-subscript
11355 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11356 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11357 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11358 case though, as some lengths could be zero. */
11359
11360static tree
11361handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11362 bool &maybe_zero_len, unsigned int &first_non_one)
11363{
11364 tree ret, low_bound, length, type;
11365 if (TREE_CODE (t) != TREE_LIST)
11366 {
11367 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11368 return error_mark_node;
11369 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11370 {
11371 if (DECL_P (t))
11372 error_at (OMP_CLAUSE_LOCATION (c),
11373 "%qD is not a variable in %qs clause", t,
11374 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11375 else
11376 error_at (OMP_CLAUSE_LOCATION (c),
11377 "%qE is not a variable in %qs clause", t,
11378 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11379 return error_mark_node;
11380 }
11381 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11382 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11383 {
11384 error_at (OMP_CLAUSE_LOCATION (c),
11385 "%qD is threadprivate variable in %qs clause", t,
11386 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11387 return error_mark_node;
11388 }
11389 return t;
11390 }
11391
11392 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11393 maybe_zero_len, first_non_one);
11394 if (ret == error_mark_node || ret == NULL_TREE)
11395 return ret;
11396
11397 type = TREE_TYPE (ret);
11398 low_bound = TREE_PURPOSE (t);
11399 length = TREE_VALUE (t);
11400
11401 if (low_bound == error_mark_node || length == error_mark_node)
11402 return error_mark_node;
11403
11404 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11405 {
11406 error_at (OMP_CLAUSE_LOCATION (c),
11407 "low bound %qE of array section does not have integral type",
11408 low_bound);
11409 return error_mark_node;
11410 }
11411 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11412 {
11413 error_at (OMP_CLAUSE_LOCATION (c),
11414 "length %qE of array section does not have integral type",
11415 length);
11416 return error_mark_node;
11417 }
11418 if (low_bound
11419 && TREE_CODE (low_bound) == INTEGER_CST
11420 && TYPE_PRECISION (TREE_TYPE (low_bound))
11421 > TYPE_PRECISION (sizetype))
11422 low_bound = fold_convert (sizetype, low_bound);
11423 if (length
11424 && TREE_CODE (length) == INTEGER_CST
11425 && TYPE_PRECISION (TREE_TYPE (length))
11426 > TYPE_PRECISION (sizetype))
11427 length = fold_convert (sizetype, length);
11428 if (low_bound == NULL_TREE)
11429 low_bound = integer_zero_node;
11430
11431 if (length != NULL_TREE)
11432 {
11433 if (!integer_nonzerop (length))
11434 maybe_zero_len = true;
11435 if (first_non_one == types.length ()
11436 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11437 first_non_one++;
11438 }
11439 if (TREE_CODE (type) == ARRAY_TYPE)
11440 {
11441 if (length == NULL_TREE
11442 && (TYPE_DOMAIN (type) == NULL_TREE
11443 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11444 {
11445 error_at (OMP_CLAUSE_LOCATION (c),
11446 "for unknown bound array type length expression must "
11447 "be specified");
11448 return error_mark_node;
11449 }
11450 if (TREE_CODE (low_bound) == INTEGER_CST
11451 && tree_int_cst_sgn (low_bound) == -1)
11452 {
11453 error_at (OMP_CLAUSE_LOCATION (c),
11454 "negative low bound in array section in %qs clause",
11455 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11456 return error_mark_node;
11457 }
11458 if (length != NULL_TREE
11459 && TREE_CODE (length) == INTEGER_CST
11460 && tree_int_cst_sgn (length) == -1)
11461 {
11462 error_at (OMP_CLAUSE_LOCATION (c),
11463 "negative length in array section in %qs clause",
11464 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11465 return error_mark_node;
11466 }
11467 if (TYPE_DOMAIN (type)
11468 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11469 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11470 == INTEGER_CST)
11471 {
11472 tree size = size_binop (PLUS_EXPR,
11473 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11474 size_one_node);
11475 if (TREE_CODE (low_bound) == INTEGER_CST)
11476 {
11477 if (tree_int_cst_lt (size, low_bound))
11478 {
11479 error_at (OMP_CLAUSE_LOCATION (c),
11480 "low bound %qE above array section size "
11481 "in %qs clause", low_bound,
11482 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11483 return error_mark_node;
11484 }
11485 if (tree_int_cst_equal (size, low_bound))
11486 maybe_zero_len = true;
11487 else if (length == NULL_TREE
11488 && first_non_one == types.length ()
11489 && tree_int_cst_equal
11490 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11491 low_bound))
11492 first_non_one++;
11493 }
11494 else if (length == NULL_TREE)
11495 {
11496 maybe_zero_len = true;
11497 if (first_non_one == types.length ())
11498 first_non_one++;
11499 }
11500 if (length && TREE_CODE (length) == INTEGER_CST)
11501 {
11502 if (tree_int_cst_lt (size, length))
11503 {
11504 error_at (OMP_CLAUSE_LOCATION (c),
11505 "length %qE above array section size "
11506 "in %qs clause", length,
11507 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11508 return error_mark_node;
11509 }
11510 if (TREE_CODE (low_bound) == INTEGER_CST)
11511 {
11512 tree lbpluslen
11513 = size_binop (PLUS_EXPR,
11514 fold_convert (sizetype, low_bound),
11515 fold_convert (sizetype, length));
11516 if (TREE_CODE (lbpluslen) == INTEGER_CST
11517 && tree_int_cst_lt (size, lbpluslen))
11518 {
11519 error_at (OMP_CLAUSE_LOCATION (c),
11520 "high bound %qE above array section size "
11521 "in %qs clause", lbpluslen,
11522 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11523 return error_mark_node;
11524 }
11525 }
11526 }
11527 }
11528 else if (length == NULL_TREE)
11529 {
11530 maybe_zero_len = true;
11531 if (first_non_one == types.length ())
11532 first_non_one++;
11533 }
11534
11535 /* For [lb:] we will need to evaluate lb more than once. */
11536 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11537 {
11538 tree lb = c_save_expr (low_bound);
11539 if (lb != low_bound)
11540 {
11541 TREE_PURPOSE (t) = lb;
11542 low_bound = lb;
11543 }
11544 }
11545 }
11546 else if (TREE_CODE (type) == POINTER_TYPE)
11547 {
11548 if (length == NULL_TREE)
11549 {
11550 error_at (OMP_CLAUSE_LOCATION (c),
11551 "for pointer type length expression must be specified");
11552 return error_mark_node;
11553 }
11554 /* If there is a pointer type anywhere but in the very first
11555 array-section-subscript, the array section can't be contiguous. */
11556 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11557 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11558 {
11559 error_at (OMP_CLAUSE_LOCATION (c),
11560 "array section is not contiguous in %qs clause",
11561 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11562 return error_mark_node;
11563 }
11564 }
11565 else
11566 {
11567 error_at (OMP_CLAUSE_LOCATION (c),
11568 "%qE does not have pointer or array type", ret);
11569 return error_mark_node;
11570 }
11571 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11572 types.safe_push (TREE_TYPE (ret));
11573 /* We will need to evaluate lb more than once. */
11574 tree lb = c_save_expr (low_bound);
11575 if (lb != low_bound)
11576 {
11577 TREE_PURPOSE (t) = lb;
11578 low_bound = lb;
11579 }
11580 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11581 return ret;
11582}
11583
11584/* Handle array sections for clause C. */
11585
11586static bool
11587handle_omp_array_sections (tree c)
11588{
11589 bool maybe_zero_len = false;
11590 unsigned int first_non_one = 0;
11591 vec<tree> types = vNULL;
11592 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11593 maybe_zero_len, first_non_one);
11594 if (first == error_mark_node)
11595 {
11596 types.release ();
11597 return true;
11598 }
11599 if (first == NULL_TREE)
11600 {
11601 types.release ();
11602 return false;
11603 }
11604 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11605 {
11606 tree t = OMP_CLAUSE_DECL (c);
11607 tree tem = NULL_TREE;
11608 types.release ();
11609 /* Need to evaluate side effects in the length expressions
11610 if any. */
11611 while (TREE_CODE (t) == TREE_LIST)
11612 {
11613 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11614 {
11615 if (tem == NULL_TREE)
11616 tem = TREE_VALUE (t);
11617 else
11618 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11619 TREE_VALUE (t), tem);
11620 }
11621 t = TREE_CHAIN (t);
11622 }
11623 if (tem)
11624 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11625 first = c_fully_fold (first, false, NULL);
11626 OMP_CLAUSE_DECL (c) = first;
11627 }
11628 else
11629 {
11630 unsigned int num = types.length (), i;
11631 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11632 tree condition = NULL_TREE;
11633
11634 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11635 maybe_zero_len = true;
11636
11637 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11638 t = TREE_CHAIN (t))
11639 {
11640 tree low_bound = TREE_PURPOSE (t);
11641 tree length = TREE_VALUE (t);
11642
11643 i--;
11644 if (low_bound
11645 && TREE_CODE (low_bound) == INTEGER_CST
11646 && TYPE_PRECISION (TREE_TYPE (low_bound))
11647 > TYPE_PRECISION (sizetype))
11648 low_bound = fold_convert (sizetype, low_bound);
11649 if (length
11650 && TREE_CODE (length) == INTEGER_CST
11651 && TYPE_PRECISION (TREE_TYPE (length))
11652 > TYPE_PRECISION (sizetype))
11653 length = fold_convert (sizetype, length);
11654 if (low_bound == NULL_TREE)
11655 low_bound = integer_zero_node;
11656 if (!maybe_zero_len && i > first_non_one)
11657 {
11658 if (integer_nonzerop (low_bound))
11659 goto do_warn_noncontiguous;
11660 if (length != NULL_TREE
11661 && TREE_CODE (length) == INTEGER_CST
11662 && TYPE_DOMAIN (types[i])
11663 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11664 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11665 == INTEGER_CST)
11666 {
11667 tree size;
11668 size = size_binop (PLUS_EXPR,
11669 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11670 size_one_node);
11671 if (!tree_int_cst_equal (length, size))
11672 {
11673 do_warn_noncontiguous:
11674 error_at (OMP_CLAUSE_LOCATION (c),
11675 "array section is not contiguous in %qs "
11676 "clause",
11677 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11678 types.release ();
11679 return true;
11680 }
11681 }
11682 if (length != NULL_TREE
11683 && TREE_SIDE_EFFECTS (length))
11684 {
11685 if (side_effects == NULL_TREE)
11686 side_effects = length;
11687 else
11688 side_effects = build2 (COMPOUND_EXPR,
11689 TREE_TYPE (side_effects),
11690 length, side_effects);
11691 }
11692 }
11693 else
11694 {
11695 tree l;
11696
11697 if (i > first_non_one && length && integer_nonzerop (length))
11698 continue;
11699 if (length)
11700 l = fold_convert (sizetype, length);
11701 else
11702 {
11703 l = size_binop (PLUS_EXPR,
11704 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11705 size_one_node);
11706 l = size_binop (MINUS_EXPR, l,
11707 fold_convert (sizetype, low_bound));
11708 }
11709 if (i > first_non_one)
11710 {
11711 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11712 size_zero_node);
11713 if (condition == NULL_TREE)
11714 condition = l;
11715 else
11716 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11717 l, condition);
11718 }
11719 else if (size == NULL_TREE)
11720 {
11721 size = size_in_bytes (TREE_TYPE (types[i]));
11722 size = size_binop (MULT_EXPR, size, l);
11723 if (condition)
11724 size = fold_build3 (COND_EXPR, sizetype, condition,
11725 size, size_zero_node);
11726 }
11727 else
11728 size = size_binop (MULT_EXPR, size, l);
11729 }
11730 }
11731 types.release ();
11732 if (side_effects)
11733 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11734 first = c_fully_fold (first, false, NULL);
11735 OMP_CLAUSE_DECL (c) = first;
11736 if (size)
11737 size = c_fully_fold (size, false, NULL);
11738 OMP_CLAUSE_SIZE (c) = size;
11739 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11740 return false;
11741 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11742 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11743 if (!c_mark_addressable (t))
11744 return false;
11745 OMP_CLAUSE_DECL (c2) = t;
11746 t = build_fold_addr_expr (first);
11747 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11748 tree ptr = OMP_CLAUSE_DECL (c2);
11749 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11750 ptr = build_fold_addr_expr (ptr);
11751 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11752 ptrdiff_type_node, t,
11753 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11754 ptrdiff_type_node, ptr));
11755 t = c_fully_fold (t, false, NULL);
11756 OMP_CLAUSE_SIZE (c2) = t;
11757 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11758 OMP_CLAUSE_CHAIN (c) = c2;
11759 }
11760 return false;
11761}
11762
11763/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11764 an inline call. But, remap
11765 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11766 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11767
11768static tree
11769c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11770 tree decl, tree placeholder)
11771{
11772 copy_body_data id;
b787e7a2 11773 hash_map<tree, tree> decl_map;
acf0174b 11774
b787e7a2
TS
11775 decl_map.put (omp_decl1, placeholder);
11776 decl_map.put (omp_decl2, decl);
acf0174b
JJ
11777 memset (&id, 0, sizeof (id));
11778 id.src_fn = DECL_CONTEXT (omp_decl1);
11779 id.dst_fn = current_function_decl;
11780 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 11781 id.decl_map = &decl_map;
acf0174b
JJ
11782
11783 id.copy_decl = copy_decl_no_change;
11784 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11785 id.transform_new_cfg = true;
11786 id.transform_return_to_modify = false;
11787 id.transform_lang_insert_block = NULL;
11788 id.eh_lp_nr = 0;
11789 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
11790 return stmt;
11791}
11792
11793/* Helper function of c_finish_omp_clauses, called via walk_tree.
11794 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11795
11796static tree
11797c_find_omp_placeholder_r (tree *tp, int *, void *data)
11798{
11799 if (*tp == (tree) data)
11800 return *tp;
11801 return NULL_TREE;
11802}
11803
953ff289
DN
11804/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11805 Remove any elements from the list that are invalid. */
11806
11807tree
11808c_finish_omp_clauses (tree clauses)
11809{
11810 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11811 bitmap_head aligned_head;
f3316c6d 11812 tree c, t, *pc;
acf0174b
JJ
11813 bool branch_seen = false;
11814 bool copyprivate_seen = false;
11815 tree *nowait_clause = NULL;
953ff289
DN
11816
11817 bitmap_obstack_initialize (NULL);
11818 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11819 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11820 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11821 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11822
11823 for (pc = &clauses, c = clauses; c ; c = *pc)
11824 {
11825 bool remove = false;
11826 bool need_complete = false;
11827 bool need_implicitly_determined = false;
11828
aaf46ef9 11829 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11830 {
11831 case OMP_CLAUSE_SHARED:
953ff289
DN
11832 need_implicitly_determined = true;
11833 goto check_dup_generic;
11834
11835 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11836 need_complete = true;
11837 need_implicitly_determined = true;
11838 goto check_dup_generic;
11839
11840 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11841 need_implicitly_determined = true;
11842 t = OMP_CLAUSE_DECL (c);
acf0174b 11843 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11844 && (FLOAT_TYPE_P (TREE_TYPE (t))
11845 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11846 {
11847 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11848 const char *r_name = NULL;
11849
11850 switch (r_code)
11851 {
11852 case PLUS_EXPR:
11853 case MULT_EXPR:
11854 case MINUS_EXPR:
652fea39 11855 break;
20906c66 11856 case MIN_EXPR:
652fea39
JJ
11857 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11858 r_name = "min";
11859 break;
20906c66 11860 case MAX_EXPR:
652fea39
JJ
11861 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11862 r_name = "max";
953ff289
DN
11863 break;
11864 case BIT_AND_EXPR:
11865 r_name = "&";
11866 break;
11867 case BIT_XOR_EXPR:
11868 r_name = "^";
11869 break;
11870 case BIT_IOR_EXPR:
11871 r_name = "|";
11872 break;
11873 case TRUTH_ANDIF_EXPR:
652fea39
JJ
11874 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11875 r_name = "&&";
953ff289
DN
11876 break;
11877 case TRUTH_ORIF_EXPR:
652fea39
JJ
11878 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11879 r_name = "||";
953ff289
DN
11880 break;
11881 default:
11882 gcc_unreachable ();
11883 }
11884 if (r_name)
11885 {
c2255bc4
AH
11886 error_at (OMP_CLAUSE_LOCATION (c),
11887 "%qE has invalid type for %<reduction(%s)%>",
11888 t, r_name);
953ff289 11889 remove = true;
ee1d5a02 11890 break;
953ff289
DN
11891 }
11892 }
acf0174b
JJ
11893 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11894 {
11895 error_at (OMP_CLAUSE_LOCATION (c),
11896 "user defined reduction not found for %qD", t);
11897 remove = true;
ee1d5a02 11898 break;
acf0174b
JJ
11899 }
11900 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11901 {
11902 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11903 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11904 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11905 VAR_DECL, NULL_TREE, type);
11906 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11907 DECL_ARTIFICIAL (placeholder) = 1;
11908 DECL_IGNORED_P (placeholder) = 1;
11909 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11910 c_mark_addressable (placeholder);
11911 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11912 c_mark_addressable (OMP_CLAUSE_DECL (c));
11913 OMP_CLAUSE_REDUCTION_MERGE (c)
11914 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11915 TREE_VEC_ELT (list, 0),
11916 TREE_VEC_ELT (list, 1),
11917 OMP_CLAUSE_DECL (c), placeholder);
11918 OMP_CLAUSE_REDUCTION_MERGE (c)
11919 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11920 void_type_node, NULL_TREE,
11921 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11922 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11923 if (TREE_VEC_LENGTH (list) == 6)
11924 {
11925 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11926 c_mark_addressable (OMP_CLAUSE_DECL (c));
11927 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11928 c_mark_addressable (placeholder);
11929 tree init = TREE_VEC_ELT (list, 5);
11930 if (init == error_mark_node)
11931 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11932 OMP_CLAUSE_REDUCTION_INIT (c)
11933 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11934 TREE_VEC_ELT (list, 3),
11935 OMP_CLAUSE_DECL (c), placeholder);
11936 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11937 OMP_CLAUSE_REDUCTION_INIT (c)
11938 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11939 OMP_CLAUSE_REDUCTION_INIT (c));
11940 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11941 c_find_omp_placeholder_r,
11942 placeholder, NULL))
11943 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11944 }
11945 else
11946 {
11947 tree init;
11948 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11949 init = build_constructor (TREE_TYPE (t), NULL);
11950 else
11951 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11952 OMP_CLAUSE_REDUCTION_INIT (c)
11953 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11954 }
11955 OMP_CLAUSE_REDUCTION_INIT (c)
11956 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11957 void_type_node, NULL_TREE,
11958 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11959 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11960 }
953ff289
DN
11961 goto check_dup_generic;
11962
11963 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11964 copyprivate_seen = true;
11965 if (nowait_clause)
11966 {
11967 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11968 "%<nowait%> clause must not be used together "
11969 "with %<copyprivate%>");
11970 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11971 nowait_clause = NULL;
11972 }
953ff289
DN
11973 goto check_dup_generic;
11974
11975 case OMP_CLAUSE_COPYIN:
953ff289
DN
11976 t = OMP_CLAUSE_DECL (c);
11977 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11978 {
c2255bc4
AH
11979 error_at (OMP_CLAUSE_LOCATION (c),
11980 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 11981 remove = true;
ee1d5a02 11982 break;
953ff289
DN
11983 }
11984 goto check_dup_generic;
11985
acf0174b
JJ
11986 case OMP_CLAUSE_LINEAR:
11987 t = OMP_CLAUSE_DECL (c);
11988 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11989 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11990 {
11991 error_at (OMP_CLAUSE_LOCATION (c),
11992 "linear clause applied to non-integral non-pointer "
11993 "variable with type %qT", TREE_TYPE (t));
11994 remove = true;
11995 break;
11996 }
11997 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11998 {
11999 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12000 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12001 OMP_CLAUSE_DECL (c), s);
12002 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12003 sizetype, s, OMP_CLAUSE_DECL (c));
12004 if (s == error_mark_node)
12005 s = size_one_node;
12006 OMP_CLAUSE_LINEAR_STEP (c) = s;
12007 }
da6f124d
JJ
12008 else
12009 OMP_CLAUSE_LINEAR_STEP (c)
12010 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12011 goto check_dup_generic;
12012
953ff289
DN
12013 check_dup_generic:
12014 t = OMP_CLAUSE_DECL (c);
12015 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12016 {
c2255bc4 12017 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12018 "%qE is not a variable in clause %qs", t,
12019 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12020 remove = true;
12021 }
12022 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12023 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12024 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12025 {
c2255bc4
AH
12026 error_at (OMP_CLAUSE_LOCATION (c),
12027 "%qE appears more than once in data clauses", t);
953ff289
DN
12028 remove = true;
12029 }
12030 else
12031 bitmap_set_bit (&generic_head, DECL_UID (t));
12032 break;
12033
12034 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12035 t = OMP_CLAUSE_DECL (c);
12036 need_complete = true;
12037 need_implicitly_determined = true;
12038 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12039 {
c2255bc4
AH
12040 error_at (OMP_CLAUSE_LOCATION (c),
12041 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12042 remove = true;
12043 }
12044 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12045 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12046 {
c2255bc4
AH
12047 error_at (OMP_CLAUSE_LOCATION (c),
12048 "%qE appears more than once in data clauses", t);
953ff289
DN
12049 remove = true;
12050 }
12051 else
12052 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12053 break;
12054
12055 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12056 t = OMP_CLAUSE_DECL (c);
12057 need_complete = true;
12058 need_implicitly_determined = true;
12059 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12060 {
c2255bc4
AH
12061 error_at (OMP_CLAUSE_LOCATION (c),
12062 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12063 remove = true;
12064 }
12065 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12066 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12067 {
c2255bc4
AH
12068 error_at (OMP_CLAUSE_LOCATION (c),
12069 "%qE appears more than once in data clauses", t);
953ff289
DN
12070 remove = true;
12071 }
12072 else
12073 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12074 break;
12075
acf0174b
JJ
12076 case OMP_CLAUSE_ALIGNED:
12077 t = OMP_CLAUSE_DECL (c);
12078 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12079 {
12080 error_at (OMP_CLAUSE_LOCATION (c),
12081 "%qE is not a variable in %<aligned%> clause", t);
12082 remove = true;
12083 }
5a9785fb
JJ
12084 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12085 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12086 {
12087 error_at (OMP_CLAUSE_LOCATION (c),
12088 "%qE in %<aligned%> clause is neither a pointer nor "
12089 "an array", t);
12090 remove = true;
12091 }
acf0174b
JJ
12092 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12093 {
12094 error_at (OMP_CLAUSE_LOCATION (c),
12095 "%qE appears more than once in %<aligned%> clauses",
12096 t);
12097 remove = true;
12098 }
12099 else
12100 bitmap_set_bit (&aligned_head, DECL_UID (t));
12101 break;
12102
12103 case OMP_CLAUSE_DEPEND:
12104 t = OMP_CLAUSE_DECL (c);
12105 if (TREE_CODE (t) == TREE_LIST)
12106 {
12107 if (handle_omp_array_sections (c))
12108 remove = true;
12109 break;
12110 }
12111 if (t == error_mark_node)
12112 remove = true;
12113 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12114 {
12115 error_at (OMP_CLAUSE_LOCATION (c),
12116 "%qE is not a variable in %<depend%> clause", t);
12117 remove = true;
12118 }
12119 else if (!c_mark_addressable (t))
12120 remove = true;
12121 break;
12122
12123 case OMP_CLAUSE_MAP:
12124 case OMP_CLAUSE_TO:
12125 case OMP_CLAUSE_FROM:
12126 t = OMP_CLAUSE_DECL (c);
12127 if (TREE_CODE (t) == TREE_LIST)
12128 {
12129 if (handle_omp_array_sections (c))
12130 remove = true;
12131 else
12132 {
12133 t = OMP_CLAUSE_DECL (c);
b17a8b07 12134 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12135 {
12136 error_at (OMP_CLAUSE_LOCATION (c),
12137 "array section does not have mappable type "
12138 "in %qs clause",
12139 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12140 remove = true;
12141 }
12142 }
12143 break;
12144 }
12145 if (t == error_mark_node)
12146 remove = true;
12147 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12148 {
12149 error_at (OMP_CLAUSE_LOCATION (c),
12150 "%qE is not a variable in %qs clause", t,
12151 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12152 remove = true;
12153 }
12154 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12155 {
12156 error_at (OMP_CLAUSE_LOCATION (c),
12157 "%qD is threadprivate variable in %qs clause", t,
12158 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12159 remove = true;
12160 }
12161 else if (!c_mark_addressable (t))
12162 remove = true;
b17a8b07
TS
12163 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12164 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12165 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12166 {
12167 error_at (OMP_CLAUSE_LOCATION (c),
12168 "%qD does not have a mappable type in %qs clause", t,
12169 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12170 remove = true;
12171 }
12172 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12173 {
12174 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12175 error ("%qD appears more than once in motion clauses", t);
12176 else
12177 error ("%qD appears more than once in map clauses", t);
12178 remove = true;
12179 }
12180 else
12181 bitmap_set_bit (&generic_head, DECL_UID (t));
12182 break;
12183
12184 case OMP_CLAUSE_UNIFORM:
12185 t = OMP_CLAUSE_DECL (c);
12186 if (TREE_CODE (t) != PARM_DECL)
12187 {
12188 if (DECL_P (t))
12189 error_at (OMP_CLAUSE_LOCATION (c),
12190 "%qD is not an argument in %<uniform%> clause", t);
12191 else
12192 error_at (OMP_CLAUSE_LOCATION (c),
12193 "%qE is not an argument in %<uniform%> clause", t);
12194 remove = true;
ee1d5a02 12195 break;
acf0174b 12196 }
ee1d5a02 12197 goto check_dup_generic;
acf0174b
JJ
12198
12199 case OMP_CLAUSE_NOWAIT:
12200 if (copyprivate_seen)
12201 {
12202 error_at (OMP_CLAUSE_LOCATION (c),
12203 "%<nowait%> clause must not be used together "
12204 "with %<copyprivate%>");
12205 remove = true;
12206 break;
12207 }
12208 nowait_clause = pc;
12209 pc = &OMP_CLAUSE_CHAIN (c);
12210 continue;
12211
953ff289
DN
12212 case OMP_CLAUSE_IF:
12213 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12214 case OMP_CLAUSE_NUM_TEAMS:
12215 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12216 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12217 case OMP_CLAUSE_ORDERED:
12218 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12219 case OMP_CLAUSE_UNTIED:
12220 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12221 case OMP_CLAUSE_FINAL:
12222 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12223 case OMP_CLAUSE_SAFELEN:
12224 case OMP_CLAUSE_SIMDLEN:
12225 case OMP_CLAUSE_DEVICE:
12226 case OMP_CLAUSE_DIST_SCHEDULE:
12227 case OMP_CLAUSE_PARALLEL:
12228 case OMP_CLAUSE_FOR:
12229 case OMP_CLAUSE_SECTIONS:
12230 case OMP_CLAUSE_TASKGROUP:
12231 case OMP_CLAUSE_PROC_BIND:
9a771876 12232 case OMP_CLAUSE__CILK_FOR_COUNT_:
acf0174b
JJ
12233 pc = &OMP_CLAUSE_CHAIN (c);
12234 continue;
12235
12236 case OMP_CLAUSE_INBRANCH:
12237 case OMP_CLAUSE_NOTINBRANCH:
12238 if (branch_seen)
12239 {
12240 error_at (OMP_CLAUSE_LOCATION (c),
12241 "%<inbranch%> clause is incompatible with "
12242 "%<notinbranch%>");
12243 remove = true;
12244 break;
12245 }
12246 branch_seen = true;
953ff289
DN
12247 pc = &OMP_CLAUSE_CHAIN (c);
12248 continue;
12249
12250 default:
12251 gcc_unreachable ();
12252 }
12253
12254 if (!remove)
12255 {
12256 t = OMP_CLAUSE_DECL (c);
12257
12258 if (need_complete)
12259 {
12260 t = require_complete_type (t);
12261 if (t == error_mark_node)
12262 remove = true;
12263 }
12264
12265 if (need_implicitly_determined)
12266 {
12267 const char *share_name = NULL;
12268
12269 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12270 share_name = "threadprivate";
12271 else switch (c_omp_predetermined_sharing (t))
12272 {
12273 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12274 break;
12275 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12276 /* const vars may be specified in firstprivate clause. */
12277 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12278 && TREE_READONLY (t))
12279 break;
953ff289
DN
12280 share_name = "shared";
12281 break;
12282 case OMP_CLAUSE_DEFAULT_PRIVATE:
12283 share_name = "private";
12284 break;
12285 default:
12286 gcc_unreachable ();
12287 }
12288 if (share_name)
12289 {
c2255bc4
AH
12290 error_at (OMP_CLAUSE_LOCATION (c),
12291 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12292 t, share_name,
12293 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12294 remove = true;
12295 }
12296 }
12297 }
12298
12299 if (remove)
12300 *pc = OMP_CLAUSE_CHAIN (c);
12301 else
12302 pc = &OMP_CLAUSE_CHAIN (c);
12303 }
12304
12305 bitmap_obstack_release (NULL);
12306 return clauses;
12307}
9ae165a0 12308
0a35513e
AH
12309/* Create a transaction node. */
12310
12311tree
12312c_finish_transaction (location_t loc, tree block, int flags)
12313{
12314 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12315 if (flags & TM_STMT_ATTR_OUTER)
12316 TRANSACTION_EXPR_OUTER (stmt) = 1;
12317 if (flags & TM_STMT_ATTR_RELAXED)
12318 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12319 return add_stmt (stmt);
12320}
12321
9ae165a0
DG
12322/* Make a variant type in the proper way for C/C++, propagating qualifiers
12323 down to the element type of an array. */
12324
12325tree
12326c_build_qualified_type (tree type, int type_quals)
12327{
12328 if (type == error_mark_node)
12329 return type;
12330
12331 if (TREE_CODE (type) == ARRAY_TYPE)
12332 {
12333 tree t;
12334 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12335 type_quals);
12336
12337 /* See if we already have an identically qualified type. */
12338 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12339 {
12340 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12341 && TYPE_NAME (t) == TYPE_NAME (type)
12342 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12343 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12344 TYPE_ATTRIBUTES (type)))
12345 break;
12346 }
12347 if (!t)
12348 {
12349 tree domain = TYPE_DOMAIN (type);
12350
12351 t = build_variant_type_copy (type);
12352 TREE_TYPE (t) = element_type;
12353
12354 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12355 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12356 SET_TYPE_STRUCTURAL_EQUALITY (t);
12357 else if (TYPE_CANONICAL (element_type) != element_type
12358 || (domain && TYPE_CANONICAL (domain) != domain))
12359 {
b8698a0f 12360 tree unqualified_canon
9ae165a0 12361 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12362 domain? TYPE_CANONICAL (domain)
9ae165a0 12363 : NULL_TREE);
b8698a0f 12364 TYPE_CANONICAL (t)
9ae165a0
DG
12365 = c_build_qualified_type (unqualified_canon, type_quals);
12366 }
12367 else
12368 TYPE_CANONICAL (t) = t;
12369 }
12370 return t;
12371 }
12372
12373 /* A restrict-qualified pointer type must be a pointer to object or
12374 incomplete type. Note that the use of POINTER_TYPE_P also allows
12375 REFERENCE_TYPEs, which is appropriate for C++. */
12376 if ((type_quals & TYPE_QUAL_RESTRICT)
12377 && (!POINTER_TYPE_P (type)
12378 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12379 {
12380 error ("invalid use of %<restrict%>");
12381 type_quals &= ~TYPE_QUAL_RESTRICT;
12382 }
12383
12384 return build_qualified_type (type, type_quals);
12385}
72b5577d
ILT
12386
12387/* Build a VA_ARG_EXPR for the C parser. */
12388
12389tree
c2255bc4 12390c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12391{
12392 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12393 warning_at (loc, OPT_Wc___compat,
12394 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12395 return build_va_arg (loc, expr, type);
72b5577d 12396}
acf0174b
JJ
12397
12398/* Return truthvalue of whether T1 is the same tree structure as T2.
12399 Return 1 if they are the same. Return 0 if they are different. */
12400
12401bool
12402c_tree_equal (tree t1, tree t2)
12403{
12404 enum tree_code code1, code2;
12405
12406 if (t1 == t2)
12407 return true;
12408 if (!t1 || !t2)
12409 return false;
12410
12411 for (code1 = TREE_CODE (t1);
12412 CONVERT_EXPR_CODE_P (code1)
12413 || code1 == NON_LVALUE_EXPR;
12414 code1 = TREE_CODE (t1))
12415 t1 = TREE_OPERAND (t1, 0);
12416 for (code2 = TREE_CODE (t2);
12417 CONVERT_EXPR_CODE_P (code2)
12418 || code2 == NON_LVALUE_EXPR;
12419 code2 = TREE_CODE (t2))
12420 t2 = TREE_OPERAND (t2, 0);
12421
12422 /* They might have become equal now. */
12423 if (t1 == t2)
12424 return true;
12425
12426 if (code1 != code2)
12427 return false;
12428
12429 switch (code1)
12430 {
12431 case INTEGER_CST:
807e902e 12432 return wi::eq_p (t1, t2);
acf0174b
JJ
12433
12434 case REAL_CST:
12435 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12436
12437 case STRING_CST:
12438 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12439 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12440 TREE_STRING_LENGTH (t1));
12441
12442 case FIXED_CST:
12443 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12444 TREE_FIXED_CST (t2));
12445
12446 case COMPLEX_CST:
12447 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12448 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12449
12450 case VECTOR_CST:
12451 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12452
12453 case CONSTRUCTOR:
12454 /* We need to do this when determining whether or not two
12455 non-type pointer to member function template arguments
12456 are the same. */
12457 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12458 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12459 return false;
12460 {
12461 tree field, value;
12462 unsigned int i;
12463 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12464 {
12465 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12466 if (!c_tree_equal (field, elt2->index)
12467 || !c_tree_equal (value, elt2->value))
12468 return false;
12469 }
12470 }
12471 return true;
12472
12473 case TREE_LIST:
12474 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12475 return false;
12476 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12477 return false;
12478 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12479
12480 case SAVE_EXPR:
12481 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12482
12483 case CALL_EXPR:
12484 {
12485 tree arg1, arg2;
12486 call_expr_arg_iterator iter1, iter2;
12487 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12488 return false;
12489 for (arg1 = first_call_expr_arg (t1, &iter1),
12490 arg2 = first_call_expr_arg (t2, &iter2);
12491 arg1 && arg2;
12492 arg1 = next_call_expr_arg (&iter1),
12493 arg2 = next_call_expr_arg (&iter2))
12494 if (!c_tree_equal (arg1, arg2))
12495 return false;
12496 if (arg1 || arg2)
12497 return false;
12498 return true;
12499 }
12500
12501 case TARGET_EXPR:
12502 {
12503 tree o1 = TREE_OPERAND (t1, 0);
12504 tree o2 = TREE_OPERAND (t2, 0);
12505
12506 /* Special case: if either target is an unallocated VAR_DECL,
12507 it means that it's going to be unified with whatever the
12508 TARGET_EXPR is really supposed to initialize, so treat it
12509 as being equivalent to anything. */
12510 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12511 && !DECL_RTL_SET_P (o1))
12512 /*Nop*/;
12513 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12514 && !DECL_RTL_SET_P (o2))
12515 /*Nop*/;
12516 else if (!c_tree_equal (o1, o2))
12517 return false;
12518
12519 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12520 }
12521
12522 case COMPONENT_REF:
12523 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12524 return false;
12525 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12526
12527 case PARM_DECL:
12528 case VAR_DECL:
12529 case CONST_DECL:
12530 case FIELD_DECL:
12531 case FUNCTION_DECL:
12532 case IDENTIFIER_NODE:
12533 case SSA_NAME:
12534 return false;
12535
12536 case TREE_VEC:
12537 {
12538 unsigned ix;
12539 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12540 return false;
12541 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12542 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12543 TREE_VEC_ELT (t2, ix)))
12544 return false;
12545 return true;
12546 }
12547
12548 default:
12549 break;
12550 }
12551
12552 switch (TREE_CODE_CLASS (code1))
12553 {
12554 case tcc_unary:
12555 case tcc_binary:
12556 case tcc_comparison:
12557 case tcc_expression:
12558 case tcc_vl_exp:
12559 case tcc_reference:
12560 case tcc_statement:
12561 {
12562 int i, n = TREE_OPERAND_LENGTH (t1);
12563
12564 switch (code1)
12565 {
12566 case PREINCREMENT_EXPR:
12567 case PREDECREMENT_EXPR:
12568 case POSTINCREMENT_EXPR:
12569 case POSTDECREMENT_EXPR:
12570 n = 1;
12571 break;
12572 case ARRAY_REF:
12573 n = 2;
12574 break;
12575 default:
12576 break;
12577 }
12578
12579 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12580 && n != TREE_OPERAND_LENGTH (t2))
12581 return false;
12582
12583 for (i = 0; i < n; ++i)
12584 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12585 return false;
12586
12587 return true;
12588 }
12589
12590 case tcc_type:
12591 return comptypes (t1, t2);
12592 default:
12593 gcc_unreachable ();
12594 }
12595 /* We can get here with --disable-checking. */
12596 return false;
12597}
12893402
BI
12598
12599/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12600 spawn-helper and BODY is the newly created body for FNDECL. */
12601
12602void
12603cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12604{
12605 tree list = alloc_stmt_list ();
12606 tree frame = make_cilk_frame (fndecl);
12607 tree dtor = create_cilk_function_exit (frame, false, true);
12608 add_local_decl (cfun, frame);
12609
12610 DECL_SAVED_TREE (fndecl) = list;
12611 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12612 frame);
12613 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12614 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12615
12616 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12617 append_to_statement_list (detach_expr, &body_list);
12618
12619 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12620 body = fold_build_cleanup_point_expr (void_type_node, body);
12621
12622 append_to_statement_list (body, &body_list);
12623 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12624 body_list, dtor), &list);
12625}