]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
combine-stack-adj.c (combine_stack_adjustments_for_block): Remove ARG_SIZE note when...
[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 "pointer-set.h"
44#include "basic-block.h"
45#include "gimple-expr.h"
45b0be94 46#include "gimplify.h"
acf0174b 47#include "tree-inline.h"
0645c1a2 48#include "omp-low.h"
61d3ce20 49#include "c-family/c-objc.h"
a212e43f 50#include "c-family/c-common.h"
de5a5fa1 51#include "c-family/c-ubsan.h"
12893402 52#include "cilk.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
b71c7f8a 76/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 77 message within this initializer. */
b71c7f8a 78static int missing_braces_mentioned;
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);
9771b263
DN
92static int convert_arguments (tree, vec<tree, va_gc> *, vec<tree, va_gc> *,
93 tree, tree);
db3927fb 94static tree pointer_diff (location_t, tree, tree);
744aa42f
ILT
95static tree convert_for_assignment (location_t, tree, tree, tree,
96 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
97static tree valid_compound_expr_initializer (tree, tree);
98static void push_string (const char *);
99static void push_member_name (tree);
f55ade6e
AJ
100static int spelling_length (void);
101static char *print_spelling (char *);
683d6ff9 102static void warning_init (int, const char *);
c2255bc4 103static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
a1e3b3d9
LB
104static void output_init_element (tree, tree, bool, tree, tree, int, bool,
105 struct obstack *);
106static void output_pending_init_elements (int, struct obstack *);
107static int set_designator (int, struct obstack *);
108static void push_range_stack (tree, struct obstack *);
109static void add_pending_init (tree, tree, tree, bool, struct obstack *);
110static void set_nonincremental_init (struct obstack *);
111static void set_nonincremental_init_from_string (tree, struct obstack *);
112static tree find_init_member (tree, struct obstack *);
f37acdf9 113static void readonly_warning (tree, enum lvalue_use);
7bd11157 114static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 115static void record_maybe_used_decl (tree);
dc5027f4 116static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
117\f
118/* Return true if EXP is a null pointer constant, false otherwise. */
119
120static bool
58f9752a 121null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
122{
123 /* This should really operate on c_expr structures, but they aren't
124 yet available everywhere required. */
125 tree type = TREE_TYPE (expr);
126 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 127 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
128 && integer_zerop (expr)
129 && (INTEGRAL_TYPE_P (type)
130 || (TREE_CODE (type) == POINTER_TYPE
131 && VOID_TYPE_P (TREE_TYPE (type))
132 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
133}
928c19bb
JM
134
135/* EXPR may appear in an unevaluated part of an integer constant
136 expression, but not in an evaluated part. Wrap it in a
137 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
138 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
139
140static tree
141note_integer_operands (tree expr)
142{
143 tree ret;
144 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
145 {
146 ret = copy_node (expr);
147 TREE_OVERFLOW (ret) = 1;
148 }
149 else
150 {
151 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
152 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
153 }
154 return ret;
155}
156
4d84fe7c
JM
157/* Having checked whether EXPR may appear in an unevaluated part of an
158 integer constant expression and found that it may, remove any
159 C_MAYBE_CONST_EXPR noting this fact and return the resulting
160 expression. */
161
162static inline tree
163remove_c_maybe_const_expr (tree expr)
164{
165 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
166 return C_MAYBE_CONST_EXPR_EXPR (expr);
167 else
168 return expr;
169}
170
f13c9b2c
AP
171\f/* This is a cache to hold if two types are compatible or not. */
172
173struct tagged_tu_seen_cache {
174 const struct tagged_tu_seen_cache * next;
58f9752a
KG
175 const_tree t1;
176 const_tree t2;
f13c9b2c
AP
177 /* The return value of tagged_types_tu_compatible_p if we had seen
178 these two types already. */
179 int val;
180};
181
182static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
183static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
184
400fbf9f
JW
185/* Do `exp = require_complete_type (exp);' to make sure exp
186 does not have an incomplete type. (That includes void types.) */
187
188tree
2f6e4e97 189require_complete_type (tree value)
400fbf9f
JW
190{
191 tree type = TREE_TYPE (value);
192
c3d5c3fa 193 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
194 return error_mark_node;
195
400fbf9f 196 /* First, detect a valid value with a complete type. */
d0f062fb 197 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
198 return value;
199
7a228918 200 c_incomplete_type_error (value, type);
400fbf9f
JW
201 return error_mark_node;
202}
203
204/* Print an error message for invalid use of an incomplete type.
205 VALUE is the expression that was used (or 0 if that isn't known)
206 and TYPE is the type that was invalid. */
207
208void
ac7d7749 209c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 210{
5d5993dd 211 const char *type_code_string;
400fbf9f
JW
212
213 /* Avoid duplicate error message. */
214 if (TREE_CODE (type) == ERROR_MARK)
215 return;
216
217 if (value != 0 && (TREE_CODE (value) == VAR_DECL
218 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 219 error ("%qD has an incomplete type", value);
400fbf9f
JW
220 else
221 {
222 retry:
223 /* We must print an error message. Be clever about what it says. */
224
225 switch (TREE_CODE (type))
226 {
227 case RECORD_TYPE:
ab87f8c8 228 type_code_string = "struct";
400fbf9f
JW
229 break;
230
231 case UNION_TYPE:
ab87f8c8 232 type_code_string = "union";
400fbf9f
JW
233 break;
234
235 case ENUMERAL_TYPE:
ab87f8c8 236 type_code_string = "enum";
400fbf9f
JW
237 break;
238
239 case VOID_TYPE:
240 error ("invalid use of void expression");
241 return;
242
243 case ARRAY_TYPE:
244 if (TYPE_DOMAIN (type))
245 {
fba78abb
RH
246 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
247 {
248 error ("invalid use of flexible array member");
249 return;
250 }
400fbf9f
JW
251 type = TREE_TYPE (type);
252 goto retry;
253 }
254 error ("invalid use of array with unspecified bounds");
255 return;
256
257 default:
366de0ce 258 gcc_unreachable ();
400fbf9f
JW
259 }
260
261 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
262 error ("invalid use of undefined type %<%s %E%>",
263 type_code_string, TYPE_NAME (type));
400fbf9f
JW
264 else
265 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 266 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
267 }
268}
269
ab393bf1
NB
270/* Given a type, apply default promotions wrt unnamed function
271 arguments and return the new type. */
272
273tree
2f6e4e97 274c_type_promotes_to (tree type)
ab393bf1 275{
267bac10 276 tree ret = NULL_TREE;
ab393bf1 277
267bac10
JM
278 if (TYPE_MAIN_VARIANT (type) == float_type_node)
279 ret = double_type_node;
280 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
281 {
282 /* Preserve unsignedness if not really getting any wider. */
8df83eae 283 if (TYPE_UNSIGNED (type)
c22cacf3 284 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
285 ret = unsigned_type_node;
286 else
287 ret = integer_type_node;
ab393bf1
NB
288 }
289
267bac10
JM
290 if (ret != NULL_TREE)
291 return (TYPE_ATOMIC (type)
292 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
293 : ret);
294
ab393bf1
NB
295 return type;
296}
297
36c5e70a
BE
298/* Return true if between two named address spaces, whether there is a superset
299 named address space that encompasses both address spaces. If there is a
300 superset, return which address space is the superset. */
301
302static bool
303addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
304{
305 if (as1 == as2)
306 {
307 *common = as1;
308 return true;
309 }
310 else if (targetm.addr_space.subset_p (as1, as2))
311 {
312 *common = as2;
313 return true;
314 }
315 else if (targetm.addr_space.subset_p (as2, as1))
316 {
317 *common = as1;
318 return true;
319 }
320 else
321 return false;
322}
323
400fbf9f
JW
324/* Return a variant of TYPE which has all the type qualifiers of LIKE
325 as well as those of TYPE. */
326
327static tree
2f6e4e97 328qualify_type (tree type, tree like)
400fbf9f 329{
36c5e70a
BE
330 addr_space_t as_type = TYPE_ADDR_SPACE (type);
331 addr_space_t as_like = TYPE_ADDR_SPACE (like);
332 addr_space_t as_common;
333
334 /* If the two named address spaces are different, determine the common
335 superset address space. If there isn't one, raise an error. */
336 if (!addr_space_superset (as_type, as_like, &as_common))
337 {
338 as_common = as_type;
339 error ("%qT and %qT are in disjoint named address spaces",
340 type, like);
341 }
342
2f6e4e97 343 return c_build_qualified_type (type,
36c5e70a 344 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 345 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 346 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 347}
52ffd86e
MS
348
349/* Return true iff the given tree T is a variable length array. */
350
351bool
ac7d7749 352c_vla_type_p (const_tree t)
52ffd86e
MS
353{
354 if (TREE_CODE (t) == ARRAY_TYPE
355 && C_TYPE_VARIABLE_SIZE (t))
356 return true;
357 return false;
358}
400fbf9f 359\f
10bc1b1b 360/* Return the composite type of two compatible types.
5305f6d7 361
10bc1b1b
JM
362 We assume that comptypes has already been done and returned
363 nonzero; if that isn't so, this may crash. In particular, we
364 assume that qualifiers match. */
400fbf9f
JW
365
366tree
10bc1b1b 367composite_type (tree t1, tree t2)
400fbf9f 368{
b3694847
SS
369 enum tree_code code1;
370 enum tree_code code2;
4b027d16 371 tree attributes;
400fbf9f
JW
372
373 /* Save time if the two types are the same. */
374
375 if (t1 == t2) return t1;
376
377 /* If one type is nonsense, use the other. */
378 if (t1 == error_mark_node)
379 return t2;
380 if (t2 == error_mark_node)
381 return t1;
382
10bc1b1b
JM
383 code1 = TREE_CODE (t1);
384 code2 = TREE_CODE (t2);
385
d9525bec 386 /* Merge the attributes. */
5fd9b178 387 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 388
10bc1b1b
JM
389 /* If one is an enumerated type and the other is the compatible
390 integer type, the composite type might be either of the two
391 (DR#013 question 3). For consistency, use the enumerated type as
392 the composite type. */
400fbf9f 393
10bc1b1b
JM
394 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
395 return t1;
396 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
397 return t2;
75326e8c 398
366de0ce 399 gcc_assert (code1 == code2);
b6a10c9f 400
400fbf9f
JW
401 switch (code1)
402 {
400fbf9f 403 case POINTER_TYPE:
10bc1b1b 404 /* For two pointers, do this recursively on the target type. */
400fbf9f 405 {
3932261a
MM
406 tree pointed_to_1 = TREE_TYPE (t1);
407 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 408 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 409 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
410 t1 = build_type_attribute_variant (t1, attributes);
411 return qualify_type (t1, t2);
400fbf9f 412 }
400fbf9f
JW
413
414 case ARRAY_TYPE:
415 {
10bc1b1b 416 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
417 int quals;
418 tree unqual_elt;
ca8bdb78
JM
419 tree d1 = TYPE_DOMAIN (t1);
420 tree d2 = TYPE_DOMAIN (t2);
421 bool d1_variable, d2_variable;
422 bool d1_zero, d2_zero;
f6294de7 423 bool t1_complete, t2_complete;
46df2823 424
de46b2fe 425 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
426 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
427 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 428
f6294de7
JM
429 t1_complete = COMPLETE_TYPE_P (t1);
430 t2_complete = COMPLETE_TYPE_P (t2);
431
ca8bdb78
JM
432 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
433 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
434
435 d1_variable = (!d1_zero
436 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
437 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
438 d2_variable = (!d2_zero
439 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
440 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
441 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
442 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 443
400fbf9f 444 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
445 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
446 && (d2_variable || d2_zero || !d1_variable))
4b027d16 447 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
448 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
449 && (d1_variable || d1_zero || !d2_variable))
4b027d16 450 return build_type_attribute_variant (t2, attributes);
c22cacf3 451
de46b2fe
AP
452 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
453 return build_type_attribute_variant (t1, attributes);
454 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
455 return build_type_attribute_variant (t2, attributes);
c22cacf3 456
46df2823
JM
457 /* Merge the element types, and have a size if either arg has
458 one. We may have qualifiers on the element types. To set
459 up TYPE_MAIN_VARIANT correctly, we need to form the
460 composite of the unqualified types and add the qualifiers
461 back at the end. */
462 quals = TYPE_QUALS (strip_array_types (elt));
463 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
464 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
465 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
466 && (d2_variable
467 || d2_zero
468 || !d1_variable))
469 ? t1
470 : t2));
f6294de7
JM
471 /* Ensure a composite type involving a zero-length array type
472 is a zero-length type not an incomplete type. */
473 if (d1_zero && d2_zero
474 && (t1_complete || t2_complete)
475 && !COMPLETE_TYPE_P (t1))
476 {
477 TYPE_SIZE (t1) = bitsize_zero_node;
478 TYPE_SIZE_UNIT (t1) = size_zero_node;
479 }
46df2823 480 t1 = c_build_qualified_type (t1, quals);
de46b2fe 481 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
482 }
483
fcb99e7b
JJ
484 case ENUMERAL_TYPE:
485 case RECORD_TYPE:
486 case UNION_TYPE:
487 if (attributes != NULL)
488 {
489 /* Try harder not to create a new aggregate type. */
490 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
491 return t1;
492 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
493 return t2;
494 }
495 return build_type_attribute_variant (t1, attributes);
496
400fbf9f
JW
497 case FUNCTION_TYPE:
498 /* Function types: prefer the one that specified arg types.
499 If both do, merge the arg types. Also merge the return types. */
500 {
10bc1b1b 501 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
502 tree p1 = TYPE_ARG_TYPES (t1);
503 tree p2 = TYPE_ARG_TYPES (t2);
504 int len;
505 tree newargs, n;
506 int i;
507
508 /* Save space: see if the result is identical to one of the args. */
3f75a254 509 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 510 return build_type_attribute_variant (t1, attributes);
3f75a254 511 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 512 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
513
514 /* Simple way if one arg fails to specify argument types. */
515 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 516 {
fe7080d2
AP
517 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
518 t1 = build_type_attribute_variant (t1, attributes);
519 return qualify_type (t1, t2);
4b027d16 520 }
400fbf9f 521 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
522 {
523 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
524 t1 = build_type_attribute_variant (t1, attributes);
525 return qualify_type (t1, t2);
4b027d16 526 }
400fbf9f
JW
527
528 /* If both args specify argument types, we must merge the two
529 lists, argument by argument. */
2f4e8f2b 530
400fbf9f
JW
531 len = list_length (p1);
532 newargs = 0;
533
534 for (i = 0; i < len; i++)
8d9bfdc5 535 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
536
537 n = newargs;
538
539 for (; p1;
540 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
541 {
542 /* A null type means arg type is not specified.
543 Take whatever the other function type has. */
544 if (TREE_VALUE (p1) == 0)
545 {
546 TREE_VALUE (n) = TREE_VALUE (p2);
547 goto parm_done;
548 }
549 if (TREE_VALUE (p2) == 0)
550 {
551 TREE_VALUE (n) = TREE_VALUE (p1);
552 goto parm_done;
553 }
2f6e4e97 554
400fbf9f
JW
555 /* Given wait (union {union wait *u; int *i} *)
556 and wait (union wait *),
557 prefer union wait * as type of parm. */
558 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
559 && TREE_VALUE (p1) != TREE_VALUE (p2))
560 {
561 tree memb;
58cb41e6
JJ
562 tree mv2 = TREE_VALUE (p2);
563 if (mv2 && mv2 != error_mark_node
564 && TREE_CODE (mv2) != ARRAY_TYPE)
565 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 566 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 567 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
568 {
569 tree mv3 = TREE_TYPE (memb);
570 if (mv3 && mv3 != error_mark_node
571 && TREE_CODE (mv3) != ARRAY_TYPE)
572 mv3 = TYPE_MAIN_VARIANT (mv3);
573 if (comptypes (mv3, mv2))
574 {
575 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
576 TREE_VALUE (p2));
c1771a20 577 pedwarn (input_location, OPT_Wpedantic,
fcf73884 578 "function types not truly compatible in ISO C");
58cb41e6
JJ
579 goto parm_done;
580 }
581 }
400fbf9f
JW
582 }
583 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
584 && TREE_VALUE (p2) != TREE_VALUE (p1))
585 {
586 tree memb;
58cb41e6
JJ
587 tree mv1 = TREE_VALUE (p1);
588 if (mv1 && mv1 != error_mark_node
589 && TREE_CODE (mv1) != ARRAY_TYPE)
590 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 591 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 592 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
593 {
594 tree mv3 = TREE_TYPE (memb);
595 if (mv3 && mv3 != error_mark_node
596 && TREE_CODE (mv3) != ARRAY_TYPE)
597 mv3 = TYPE_MAIN_VARIANT (mv3);
598 if (comptypes (mv3, mv1))
599 {
600 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
601 TREE_VALUE (p1));
c1771a20 602 pedwarn (input_location, OPT_Wpedantic,
fcf73884 603 "function types not truly compatible in ISO C");
58cb41e6
JJ
604 goto parm_done;
605 }
606 }
400fbf9f 607 }
10bc1b1b 608 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
609 parm_done: ;
610 }
611
4b027d16 612 t1 = build_function_type (valtype, newargs);
fe7080d2 613 t1 = qualify_type (t1, t2);
0f41302f 614 /* ... falls through ... */
400fbf9f
JW
615 }
616
617 default:
4b027d16 618 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
619 }
620
621}
10bc1b1b
JM
622
623/* Return the type of a conditional expression between pointers to
624 possibly differently qualified versions of compatible types.
625
626 We assume that comp_target_types has already been done and returned
627 nonzero; if that isn't so, this may crash. */
628
629static tree
630common_pointer_type (tree t1, tree t2)
631{
632 tree attributes;
46df2823
JM
633 tree pointed_to_1, mv1;
634 tree pointed_to_2, mv2;
10bc1b1b 635 tree target;
eb1387a0 636 unsigned target_quals;
36c5e70a
BE
637 addr_space_t as1, as2, as_common;
638 int quals1, quals2;
10bc1b1b
JM
639
640 /* Save time if the two types are the same. */
641
642 if (t1 == t2) return t1;
643
644 /* If one type is nonsense, use the other. */
645 if (t1 == error_mark_node)
646 return t2;
647 if (t2 == error_mark_node)
648 return t1;
649
366de0ce 650 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 651 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
652
653 /* Merge the attributes. */
654 attributes = targetm.merge_type_attributes (t1, t2);
655
656 /* Find the composite type of the target types, and combine the
46df2823
JM
657 qualifiers of the two types' targets. Do not lose qualifiers on
658 array element types by taking the TYPE_MAIN_VARIANT. */
659 mv1 = pointed_to_1 = TREE_TYPE (t1);
660 mv2 = pointed_to_2 = TREE_TYPE (t2);
661 if (TREE_CODE (mv1) != ARRAY_TYPE)
662 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
663 if (TREE_CODE (mv2) != ARRAY_TYPE)
664 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
665 target = composite_type (mv1, mv2);
eb1387a0
RG
666
667 /* For function types do not merge const qualifiers, but drop them
668 if used inconsistently. The middle-end uses these to mark const
669 and noreturn functions. */
36c5e70a
BE
670 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
671 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
672
eb1387a0 673 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 674 target_quals = (quals1 & quals2);
eb1387a0 675 else
36c5e70a
BE
676 target_quals = (quals1 | quals2);
677
678 /* If the two named address spaces are different, determine the common
679 superset address space. This is guaranteed to exist due to the
680 assumption that comp_target_type returned non-zero. */
681 as1 = TYPE_ADDR_SPACE (pointed_to_1);
682 as2 = TYPE_ADDR_SPACE (pointed_to_2);
683 if (!addr_space_superset (as1, as2, &as_common))
684 gcc_unreachable ();
685
686 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
687
eb1387a0 688 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
689 return build_type_attribute_variant (t1, attributes);
690}
691
692/* Return the common type for two arithmetic types under the usual
693 arithmetic conversions. The default conversions have already been
694 applied, and enumerated types converted to their compatible integer
695 types. The resulting type is unqualified and has no attributes.
696
697 This is the type for the result of most arithmetic operations
698 if the operands have the given two types. */
699
ccf7f880
JJ
700static tree
701c_common_type (tree t1, tree t2)
10bc1b1b
JM
702{
703 enum tree_code code1;
704 enum tree_code code2;
705
706 /* If one type is nonsense, use the other. */
707 if (t1 == error_mark_node)
708 return t2;
709 if (t2 == error_mark_node)
710 return t1;
711
712 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
713 t1 = TYPE_MAIN_VARIANT (t1);
714
715 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
716 t2 = TYPE_MAIN_VARIANT (t2);
717
718 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
719 t1 = build_type_attribute_variant (t1, NULL_TREE);
720
721 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
722 t2 = build_type_attribute_variant (t2, NULL_TREE);
723
724 /* Save time if the two types are the same. */
725
726 if (t1 == t2) return t1;
727
728 code1 = TREE_CODE (t1);
729 code2 = TREE_CODE (t2);
730
366de0ce 731 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
732 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
733 || code1 == INTEGER_TYPE);
366de0ce 734 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
735 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
736 || code2 == INTEGER_TYPE);
10bc1b1b 737
5fc89bfd
JJ
738 /* When one operand is a decimal float type, the other operand cannot be
739 a generic float type or a complex type. We also disallow vector types
740 here. */
741 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
742 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
743 {
744 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
745 {
746 error ("can%'t mix operands of decimal float and vector types");
747 return error_mark_node;
748 }
749 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
750 {
751 error ("can%'t mix operands of decimal float and complex types");
752 return error_mark_node;
753 }
754 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
755 {
756 error ("can%'t mix operands of decimal float and other float types");
757 return error_mark_node;
758 }
759 }
760
10bc1b1b
JM
761 /* If one type is a vector type, return that type. (How the usual
762 arithmetic conversions apply to the vector types extension is not
763 precisely specified.) */
764 if (code1 == VECTOR_TYPE)
765 return t1;
766
767 if (code2 == VECTOR_TYPE)
768 return t2;
769
770 /* If one type is complex, form the common type of the non-complex
771 components, then make that complex. Use T1 or T2 if it is the
772 required type. */
773 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
774 {
775 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
776 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 777 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
778
779 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
780 return t1;
781 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
782 return t2;
783 else
784 return build_complex_type (subtype);
785 }
786
787 /* If only one is real, use it as the result. */
788
789 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
790 return t1;
791
792 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
793 return t2;
794
9a8ce21f
JG
795 /* If both are real and either are decimal floating point types, use
796 the decimal floating point type with the greater precision. */
797
798 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
799 {
800 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
801 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
802 return dfloat128_type_node;
803 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
805 return dfloat64_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
808 return dfloat32_type_node;
809 }
810
ab22c1fa
CF
811 /* Deal with fixed-point types. */
812 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
813 {
814 unsigned int unsignedp = 0, satp = 0;
815 enum machine_mode m1, m2;
816 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
817
818 m1 = TYPE_MODE (t1);
819 m2 = TYPE_MODE (t2);
820
821 /* If one input type is saturating, the result type is saturating. */
822 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
823 satp = 1;
824
825 /* If both fixed-point types are unsigned, the result type is unsigned.
826 When mixing fixed-point and integer types, follow the sign of the
827 fixed-point type.
828 Otherwise, the result type is signed. */
829 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
830 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
831 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
832 && TYPE_UNSIGNED (t1))
833 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
834 && TYPE_UNSIGNED (t2)))
835 unsignedp = 1;
836
837 /* The result type is signed. */
838 if (unsignedp == 0)
839 {
840 /* If the input type is unsigned, we need to convert to the
841 signed type. */
842 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
843 {
d75d71e0 844 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
845 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
846 mclass = MODE_FRACT;
847 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
848 mclass = MODE_ACCUM;
849 else
850 gcc_unreachable ();
851 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
852 }
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
854 {
d75d71e0 855 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
862 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
863 }
864 }
865
866 if (code1 == FIXED_POINT_TYPE)
867 {
868 fbit1 = GET_MODE_FBIT (m1);
869 ibit1 = GET_MODE_IBIT (m1);
870 }
871 else
872 {
873 fbit1 = 0;
874 /* Signed integers need to subtract one sign bit. */
875 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
876 }
877
878 if (code2 == FIXED_POINT_TYPE)
879 {
880 fbit2 = GET_MODE_FBIT (m2);
881 ibit2 = GET_MODE_IBIT (m2);
882 }
883 else
884 {
885 fbit2 = 0;
886 /* Signed integers need to subtract one sign bit. */
887 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
888 }
889
890 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
891 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
892 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
893 satp);
894 }
895
10bc1b1b
JM
896 /* Both real or both integers; use the one with greater precision. */
897
898 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
899 return t1;
900 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
901 return t2;
902
903 /* Same precision. Prefer long longs to longs to ints when the
904 same precision, following the C99 rules on integer type rank
905 (which are equivalent to the C90 rules for C90 types). */
906
907 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
909 return long_long_unsigned_type_node;
910
911 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
912 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
913 {
914 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
915 return long_long_unsigned_type_node;
916 else
c22cacf3 917 return long_long_integer_type_node;
10bc1b1b
JM
918 }
919
920 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
922 return long_unsigned_type_node;
923
924 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
925 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
926 {
927 /* But preserve unsignedness from the other type,
928 since long cannot hold all the values of an unsigned int. */
929 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
930 return long_unsigned_type_node;
931 else
932 return long_integer_type_node;
933 }
934
935 /* Likewise, prefer long double to double even if same size. */
936 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
937 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
938 return long_double_type_node;
939
2531a1d9
JR
940 /* Likewise, prefer double to float even if same size.
941 We got a couple of embedded targets with 32 bit doubles, and the
942 pdp11 might have 64 bit floats. */
943 if (TYPE_MAIN_VARIANT (t1) == double_type_node
944 || TYPE_MAIN_VARIANT (t2) == double_type_node)
945 return double_type_node;
946
10bc1b1b
JM
947 /* Otherwise prefer the unsigned one. */
948
949 if (TYPE_UNSIGNED (t1))
950 return t1;
951 else
952 return t2;
953}
400fbf9f 954\f
5922c215
JM
955/* Wrapper around c_common_type that is used by c-common.c and other
956 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
957 are allowed here and are converted to their compatible integer types.
958 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
959 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
960tree
961common_type (tree t1, tree t2)
962{
963 if (TREE_CODE (t1) == ENUMERAL_TYPE)
964 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
965 if (TREE_CODE (t2) == ENUMERAL_TYPE)
966 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
967
968 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
969 if (TREE_CODE (t1) == BOOLEAN_TYPE
970 && TREE_CODE (t2) == BOOLEAN_TYPE)
971 return boolean_type_node;
972
973 /* If either type is BOOLEAN_TYPE, then return the other. */
974 if (TREE_CODE (t1) == BOOLEAN_TYPE)
975 return t2;
976 if (TREE_CODE (t2) == BOOLEAN_TYPE)
977 return t1;
978
ccf7f880
JJ
979 return c_common_type (t1, t2);
980}
f13c9b2c 981
400fbf9f
JW
982/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
983 or various other operations. Return 2 if they are compatible
984 but a warning may be needed if you use them together. */
985
986int
132da1a5 987comptypes (tree type1, tree type2)
f13c9b2c
AP
988{
989 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
990 int val;
991
dc5027f4 992 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
993 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
994
995 return val;
996}
997
998/* Like comptypes, but if it returns non-zero because enum and int are
999 compatible, it sets *ENUM_AND_INT_P to true. */
1000
1001static int
1002comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1003{
1004 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1005 int val;
1006
dc5027f4
JM
1007 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1008 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1009
1010 return val;
1011}
1012
1013/* Like comptypes, but if it returns nonzero for different types, it
1014 sets *DIFFERENT_TYPES_P to true. */
1015
1016int
1017comptypes_check_different_types (tree type1, tree type2,
1018 bool *different_types_p)
1019{
1020 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1021 int val;
1022
1023 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1024 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1025
f13c9b2c 1026 return val;
c22cacf3
MS
1027}
1028\f
f13c9b2c
AP
1029/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1030 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1031 but a warning may be needed if you use them together. If
1032 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1033 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1034 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1035 NULL, and the types are compatible but different enough not to be
48b0b196 1036 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1037 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1038 false, but may or may not be set if the types are incompatible.
1039 This differs from comptypes, in that we don't free the seen
1040 types. */
f13c9b2c
AP
1041
1042static int
dc5027f4
JM
1043comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1044 bool *different_types_p)
400fbf9f 1045{
58f9752a
KG
1046 const_tree t1 = type1;
1047 const_tree t2 = type2;
4b027d16 1048 int attrval, val;
400fbf9f
JW
1049
1050 /* Suppress errors caused by previously reported errors. */
1051
8d47dfc5
RH
1052 if (t1 == t2 || !t1 || !t2
1053 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1054 return 1;
1055
bca63328
JM
1056 /* Enumerated types are compatible with integer types, but this is
1057 not transitive: two enumerated types in the same translation unit
1058 are compatible with each other only if they are the same type. */
400fbf9f 1059
bca63328 1060 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1061 {
1062 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1063 if (TREE_CODE (t2) != VOID_TYPE)
1064 {
1065 if (enum_and_int_p != NULL)
1066 *enum_and_int_p = true;
1067 if (different_types_p != NULL)
1068 *different_types_p = true;
1069 }
744aa42f 1070 }
bca63328 1071 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1072 {
1073 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1074 if (TREE_CODE (t1) != VOID_TYPE)
1075 {
1076 if (enum_and_int_p != NULL)
1077 *enum_and_int_p = true;
1078 if (different_types_p != NULL)
1079 *different_types_p = true;
1080 }
744aa42f 1081 }
400fbf9f
JW
1082
1083 if (t1 == t2)
1084 return 1;
1085
1086 /* Different classes of types can't be compatible. */
1087
3aeb3655
EC
1088 if (TREE_CODE (t1) != TREE_CODE (t2))
1089 return 0;
400fbf9f 1090
118a3a8b 1091 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1092
3932261a 1093 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1094 return 0;
1095
08632da2
RS
1096 /* Allow for two different type nodes which have essentially the same
1097 definition. Note that we already checked for equality of the type
38e01259 1098 qualifiers (just above). */
400fbf9f 1099
46df2823
JM
1100 if (TREE_CODE (t1) != ARRAY_TYPE
1101 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1102 return 1;
1103
4b027d16 1104 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1105 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1106 return 0;
1107
1108 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1109 val = 0;
1110
400fbf9f
JW
1111 switch (TREE_CODE (t1))
1112 {
1113 case POINTER_TYPE:
106f5de5
UW
1114 /* Do not remove mode or aliasing information. */
1115 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1116 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1117 break;
4b027d16 1118 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1119 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1120 enum_and_int_p, different_types_p));
4b027d16 1121 break;
400fbf9f
JW
1122
1123 case FUNCTION_TYPE:
dc5027f4
JM
1124 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1125 different_types_p);
4b027d16 1126 break;
400fbf9f
JW
1127
1128 case ARRAY_TYPE:
1129 {
400fbf9f
JW
1130 tree d1 = TYPE_DOMAIN (t1);
1131 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1132 bool d1_variable, d2_variable;
1133 bool d1_zero, d2_zero;
4b027d16 1134 val = 1;
400fbf9f
JW
1135
1136 /* Target types must match incl. qualifiers. */
1137 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1138 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1139 enum_and_int_p,
1140 different_types_p)))
400fbf9f
JW
1141 return 0;
1142
dc5027f4
JM
1143 if (different_types_p != NULL
1144 && (d1 == 0) != (d2 == 0))
1145 *different_types_p = true;
400fbf9f 1146 /* Sizes must match unless one is missing or variable. */
3f85558f 1147 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1148 break;
400fbf9f 1149
3f75a254
JM
1150 d1_zero = !TYPE_MAX_VALUE (d1);
1151 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1152
3f75a254 1153 d1_variable = (!d1_zero
3f85558f
RH
1154 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1155 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1156 d2_variable = (!d2_zero
3f85558f
RH
1157 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1158 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1159 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1160 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1161
dc5027f4
JM
1162 if (different_types_p != NULL
1163 && d1_variable != d2_variable)
1164 *different_types_p = true;
3f85558f
RH
1165 if (d1_variable || d2_variable)
1166 break;
1167 if (d1_zero && d2_zero)
1168 break;
1169 if (d1_zero || d2_zero
3f75a254
JM
1170 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1171 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1172 val = 0;
1173
c22cacf3 1174 break;
400fbf9f
JW
1175 }
1176
d1bd0ded 1177 case ENUMERAL_TYPE:
58393038 1178 case RECORD_TYPE:
d1bd0ded 1179 case UNION_TYPE:
766beae1 1180 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1181 {
fcb99e7b
JJ
1182 tree a1 = TYPE_ATTRIBUTES (t1);
1183 tree a2 = TYPE_ATTRIBUTES (t2);
1184
1185 if (! attribute_list_contained (a1, a2)
1186 && ! attribute_list_contained (a2, a1))
1187 break;
1188
f13c9b2c 1189 if (attrval != 2)
dc5027f4
JM
1190 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1191 different_types_p);
1192 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1193 different_types_p);
f13c9b2c 1194 }
4b027d16 1195 break;
e9a25f70 1196
62e1dfcf 1197 case VECTOR_TYPE:
744aa42f
ILT
1198 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1199 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1200 enum_and_int_p, different_types_p));
62e1dfcf
NC
1201 break;
1202
e9a25f70
JL
1203 default:
1204 break;
400fbf9f 1205 }
4b027d16 1206 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1207}
1208
36c5e70a
BE
1209/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1210 their qualifiers, except for named address spaces. If the pointers point to
1211 different named addresses, then we must determine if one address space is a
1212 subset of the other. */
400fbf9f
JW
1213
1214static int
744aa42f 1215comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1216{
392202b0 1217 int val;
36c5e70a
BE
1218 tree mvl = TREE_TYPE (ttl);
1219 tree mvr = TREE_TYPE (ttr);
1220 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1221 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1222 addr_space_t as_common;
744aa42f 1223 bool enum_and_int_p;
8b40563c 1224
36c5e70a
BE
1225 /* Fail if pointers point to incompatible address spaces. */
1226 if (!addr_space_superset (asl, asr, &as_common))
1227 return 0;
1228
46df2823
JM
1229 /* Do not lose qualifiers on element types of array types that are
1230 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1231 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1232 mvl = (TYPE_ATOMIC (mvl)
1233 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1234 : TYPE_MAIN_VARIANT (mvl));
46df2823 1235 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1236 mvr = (TYPE_ATOMIC (mvr)
1237 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1238 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1239 enum_and_int_p = false;
1240 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1241
fcf73884 1242 if (val == 2)
c1771a20 1243 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1244
1245 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1246 warning_at (location, OPT_Wc___compat,
1247 "pointer target types incompatible in C++");
1248
400fbf9f
JW
1249 return val;
1250}
1251\f
1252/* Subroutines of `comptypes'. */
1253
f75fbaf7
ZW
1254/* Determine whether two trees derive from the same translation unit.
1255 If the CONTEXT chain ends in a null, that tree's context is still
1256 being parsed, so if two trees have context chains ending in null,
766beae1 1257 they're in the same translation unit. */
f75fbaf7 1258int
58f9752a 1259same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1260{
1261 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1262 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1263 {
6615c446
JO
1264 case tcc_declaration:
1265 t1 = DECL_CONTEXT (t1); break;
1266 case tcc_type:
1267 t1 = TYPE_CONTEXT (t1); break;
1268 case tcc_exceptional:
1269 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1270 default: gcc_unreachable ();
766beae1
ZW
1271 }
1272
1273 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1274 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1275 {
6615c446
JO
1276 case tcc_declaration:
1277 t2 = DECL_CONTEXT (t2); break;
1278 case tcc_type:
1279 t2 = TYPE_CONTEXT (t2); break;
1280 case tcc_exceptional:
1281 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1282 default: gcc_unreachable ();
766beae1
ZW
1283 }
1284
1285 return t1 == t2;
1286}
1287
f13c9b2c 1288/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1289
f13c9b2c 1290static struct tagged_tu_seen_cache *
58f9752a 1291alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1292{
cceb1885 1293 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1294 tu->next = tagged_tu_seen_base;
1295 tu->t1 = t1;
1296 tu->t2 = t2;
c22cacf3 1297
f13c9b2c 1298 tagged_tu_seen_base = tu;
c22cacf3 1299
f13c9b2c
AP
1300 /* The C standard says that two structures in different translation
1301 units are compatible with each other only if the types of their
1302 fields are compatible (among other things). We assume that they
1303 are compatible until proven otherwise when building the cache.
1304 An example where this can occur is:
1305 struct a
1306 {
1307 struct a *next;
1308 };
1309 If we are comparing this against a similar struct in another TU,
c83eecad 1310 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1311 loop. */
1312 tu->val = 1;
1313 return tu;
1314}
d1bd0ded 1315
f13c9b2c 1316/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1317
f13c9b2c
AP
1318static void
1319free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1320{
1321 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1322 while (tu != tu_til)
1323 {
741ac903
KG
1324 const struct tagged_tu_seen_cache *const tu1
1325 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1326 tu = tu1->next;
b1d5455a 1327 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1328 }
1329 tagged_tu_seen_base = tu_til;
1330}
d1bd0ded
GK
1331
1332/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1333 compatible. If the two types are not the same (which has been
1334 checked earlier), this can only happen when multiple translation
1335 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1336 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1337 comptypes_internal. */
d1bd0ded
GK
1338
1339static int
744aa42f 1340tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1341 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1342{
1343 tree s1, s2;
1344 bool needs_warning = false;
3aeb3655 1345
d1bd0ded
GK
1346 /* We have to verify that the tags of the types are the same. This
1347 is harder than it looks because this may be a typedef, so we have
1348 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1349 typedef...
1350 In the case of compiler-created builtin structs the TYPE_DECL
1351 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1352 while (TYPE_NAME (t1)
1353 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1354 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1355 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1356
dea984dc
ILT
1357 while (TYPE_NAME (t2)
1358 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1359 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1360 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1361
1362 /* C90 didn't have the requirement that the two tags be the same. */
1363 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1364 return 0;
3aeb3655 1365
d1bd0ded
GK
1366 /* C90 didn't say what happened if one or both of the types were
1367 incomplete; we choose to follow C99 rules here, which is that they
1368 are compatible. */
1369 if (TYPE_SIZE (t1) == NULL
1370 || TYPE_SIZE (t2) == NULL)
1371 return 1;
3aeb3655 1372
d1bd0ded 1373 {
f13c9b2c 1374 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1375 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1376 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1377 return tts_i->val;
d1bd0ded 1378 }
3aeb3655 1379
d1bd0ded
GK
1380 switch (TREE_CODE (t1))
1381 {
1382 case ENUMERAL_TYPE:
1383 {
f13c9b2c 1384 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1385 /* Speed up the case where the type values are in the same order. */
1386 tree tv1 = TYPE_VALUES (t1);
1387 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1388
c22cacf3 1389 if (tv1 == tv2)
f13c9b2c
AP
1390 {
1391 return 1;
1392 }
3aeb3655 1393
c22cacf3
MS
1394 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1395 {
1396 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1397 break;
1398 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1399 {
c22cacf3 1400 tu->val = 0;
f13c9b2c
AP
1401 return 0;
1402 }
c22cacf3 1403 }
3aeb3655 1404
c22cacf3 1405 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1406 {
1407 return 1;
1408 }
c22cacf3 1409 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1410 {
1411 tu->val = 0;
1412 return 0;
1413 }
3aeb3655 1414
d1bd0ded 1415 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1416 {
1417 tu->val = 0;
1418 return 0;
1419 }
3aeb3655 1420
d1bd0ded
GK
1421 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1422 {
1423 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1424 if (s2 == NULL
1425 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1426 {
1427 tu->val = 0;
1428 return 0;
1429 }
d1bd0ded
GK
1430 }
1431 return 1;
1432 }
1433
1434 case UNION_TYPE:
1435 {
f13c9b2c 1436 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1437 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1438 {
1439 tu->val = 0;
1440 return 0;
1441 }
c22cacf3 1442
f13c9b2c
AP
1443 /* Speed up the common case where the fields are in the same order. */
1444 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1445 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1446 {
1447 int result;
c22cacf3 1448
3ae4d3cc 1449 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1450 break;
744aa42f 1451 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1452 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1453
1454 if (result != 1 && !DECL_NAME (s1))
1455 break;
f13c9b2c
AP
1456 if (result == 0)
1457 {
1458 tu->val = 0;
1459 return 0;
1460 }
1461 if (result == 2)
1462 needs_warning = true;
1463
1464 if (TREE_CODE (s1) == FIELD_DECL
1465 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1466 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
1471 }
1472 if (!s1 && !s2)
1473 {
1474 tu->val = needs_warning ? 2 : 1;
1475 return tu->val;
1476 }
d1bd0ded 1477
910ad8de 1478 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1479 {
1480 bool ok = false;
3aeb3655 1481
910ad8de 1482 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1483 if (DECL_NAME (s1) == DECL_NAME (s2))
1484 {
1485 int result;
1486
744aa42f 1487 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1488 enum_and_int_p,
1489 different_types_p);
3ae4d3cc
AO
1490
1491 if (result != 1 && !DECL_NAME (s1))
1492 continue;
1493 if (result == 0)
1494 {
1495 tu->val = 0;
1496 return 0;
1497 }
1498 if (result == 2)
1499 needs_warning = true;
1500
1501 if (TREE_CODE (s1) == FIELD_DECL
1502 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1503 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1504 break;
3ae4d3cc
AO
1505
1506 ok = true;
1507 break;
1508 }
3f75a254 1509 if (!ok)
f13c9b2c
AP
1510 {
1511 tu->val = 0;
1512 return 0;
1513 }
d1bd0ded 1514 }
f13c9b2c
AP
1515 tu->val = needs_warning ? 2 : 10;
1516 return tu->val;
d1bd0ded
GK
1517 }
1518
1519 case RECORD_TYPE:
1520 {
c22cacf3 1521 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1522
1523 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1524 s1 && s2;
910ad8de 1525 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1526 {
1527 int result;
1528 if (TREE_CODE (s1) != TREE_CODE (s2)
1529 || DECL_NAME (s1) != DECL_NAME (s2))
1530 break;
744aa42f 1531 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1532 enum_and_int_p, different_types_p);
d1bd0ded
GK
1533 if (result == 0)
1534 break;
1535 if (result == 2)
1536 needs_warning = true;
3aeb3655 1537
d1bd0ded
GK
1538 if (TREE_CODE (s1) == FIELD_DECL
1539 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1540 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1541 break;
1542 }
d1bd0ded 1543 if (s1 && s2)
f13c9b2c
AP
1544 tu->val = 0;
1545 else
1546 tu->val = needs_warning ? 2 : 1;
1547 return tu->val;
d1bd0ded
GK
1548 }
1549
1550 default:
366de0ce 1551 gcc_unreachable ();
d1bd0ded
GK
1552 }
1553}
1554
400fbf9f
JW
1555/* Return 1 if two function types F1 and F2 are compatible.
1556 If either type specifies no argument types,
1557 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1558 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1559 the other must specify that number of self-promoting arg types.
744aa42f 1560 Otherwise, the argument types must match.
dc5027f4 1561 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1562
1563static int
744aa42f 1564function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1565 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1566{
1567 tree args1, args2;
1568 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1569 int val = 1;
1570 int val1;
a6fdc086
GK
1571 tree ret1, ret2;
1572
1573 ret1 = TREE_TYPE (f1);
1574 ret2 = TREE_TYPE (f2);
1575
e508a019
JM
1576 /* 'volatile' qualifiers on a function's return type used to mean
1577 the function is noreturn. */
1578 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1579 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1580 if (TYPE_VOLATILE (ret1))
1581 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1582 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1583 if (TYPE_VOLATILE (ret2))
1584 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1585 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1586 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1587 if (val == 0)
400fbf9f
JW
1588 return 0;
1589
1590 args1 = TYPE_ARG_TYPES (f1);
1591 args2 = TYPE_ARG_TYPES (f2);
1592
dc5027f4
JM
1593 if (different_types_p != NULL
1594 && (args1 == 0) != (args2 == 0))
1595 *different_types_p = true;
1596
400fbf9f
JW
1597 /* An unspecified parmlist matches any specified parmlist
1598 whose argument types don't need default promotions. */
1599
1600 if (args1 == 0)
1601 {
1602 if (!self_promoting_args_p (args2))
1603 return 0;
1604 /* If one of these types comes from a non-prototype fn definition,
1605 compare that with the other type's arglist.
3176a0c2 1606 If they don't match, ask for a warning (but no error). */
400fbf9f 1607 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1608 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1609 enum_and_int_p, different_types_p))
400fbf9f
JW
1610 val = 2;
1611 return val;
1612 }
1613 if (args2 == 0)
1614 {
1615 if (!self_promoting_args_p (args1))
1616 return 0;
1617 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1618 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1619 enum_and_int_p, different_types_p))
400fbf9f
JW
1620 val = 2;
1621 return val;
1622 }
1623
1624 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1625 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1626 different_types_p);
400fbf9f
JW
1627 return val1 != 1 ? val1 : val;
1628}
1629
744aa42f
ILT
1630/* Check two lists of types for compatibility, returning 0 for
1631 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1632 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1633 comptypes_internal. */
400fbf9f
JW
1634
1635static int
744aa42f 1636type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1637 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1638{
1639 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1640 int val = 1;
9d5f3e49 1641 int newval = 0;
400fbf9f
JW
1642
1643 while (1)
1644 {
46df2823 1645 tree a1, mv1, a2, mv2;
400fbf9f
JW
1646 if (args1 == 0 && args2 == 0)
1647 return val;
1648 /* If one list is shorter than the other,
1649 they fail to match. */
1650 if (args1 == 0 || args2 == 0)
1651 return 0;
46df2823
JM
1652 mv1 = a1 = TREE_VALUE (args1);
1653 mv2 = a2 = TREE_VALUE (args2);
1654 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1655 mv1 = (TYPE_ATOMIC (mv1)
1656 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1657 TYPE_QUAL_ATOMIC)
1658 : TYPE_MAIN_VARIANT (mv1));
46df2823 1659 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1660 mv2 = (TYPE_ATOMIC (mv2)
1661 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1662 TYPE_QUAL_ATOMIC)
1663 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1664 /* A null pointer instead of a type
1665 means there is supposed to be an argument
1666 but nothing is specified about what type it has.
1667 So match anything that self-promotes. */
dc5027f4
JM
1668 if (different_types_p != NULL
1669 && (a1 == 0) != (a2 == 0))
1670 *different_types_p = true;
46df2823 1671 if (a1 == 0)
400fbf9f 1672 {
46df2823 1673 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1674 return 0;
1675 }
46df2823 1676 else if (a2 == 0)
400fbf9f 1677 {
46df2823 1678 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1679 return 0;
1680 }
8f5b6d29 1681 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1682 else if (TREE_CODE (a1) == ERROR_MARK
1683 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1684 ;
dc5027f4
JM
1685 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1686 different_types_p)))
400fbf9f 1687 {
dc5027f4
JM
1688 if (different_types_p != NULL)
1689 *different_types_p = true;
400fbf9f
JW
1690 /* Allow wait (union {union wait *u; int *i} *)
1691 and wait (union wait *) to be compatible. */
46df2823
JM
1692 if (TREE_CODE (a1) == UNION_TYPE
1693 && (TYPE_NAME (a1) == 0
ebf0bf7f 1694 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1695 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1696 && tree_int_cst_equal (TYPE_SIZE (a1),
1697 TYPE_SIZE (a2)))
400fbf9f
JW
1698 {
1699 tree memb;
46df2823 1700 for (memb = TYPE_FIELDS (a1);
910ad8de 1701 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1702 {
1703 tree mv3 = TREE_TYPE (memb);
1704 if (mv3 && mv3 != error_mark_node
1705 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1706 mv3 = (TYPE_ATOMIC (mv3)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1708 TYPE_QUAL_ATOMIC)
1709 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1710 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1711 different_types_p))
58cb41e6
JJ
1712 break;
1713 }
400fbf9f
JW
1714 if (memb == 0)
1715 return 0;
1716 }
46df2823
JM
1717 else if (TREE_CODE (a2) == UNION_TYPE
1718 && (TYPE_NAME (a2) == 0
ebf0bf7f 1719 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1720 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1721 && tree_int_cst_equal (TYPE_SIZE (a2),
1722 TYPE_SIZE (a1)))
400fbf9f
JW
1723 {
1724 tree memb;
46df2823 1725 for (memb = TYPE_FIELDS (a2);
910ad8de 1726 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1727 {
1728 tree mv3 = TREE_TYPE (memb);
1729 if (mv3 && mv3 != error_mark_node
1730 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1731 mv3 = (TYPE_ATOMIC (mv3)
1732 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1733 TYPE_QUAL_ATOMIC)
1734 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1735 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1736 different_types_p))
58cb41e6
JJ
1737 break;
1738 }
400fbf9f
JW
1739 if (memb == 0)
1740 return 0;
1741 }
1742 else
1743 return 0;
1744 }
1745
1746 /* comptypes said ok, but record if it said to warn. */
1747 if (newval > val)
1748 val = newval;
1749
1750 args1 = TREE_CHAIN (args1);
1751 args2 = TREE_CHAIN (args2);
1752 }
1753}
400fbf9f 1754\f
400fbf9f
JW
1755/* Compute the size to increment a pointer by. */
1756
4e2fb7de 1757static tree
58f9752a 1758c_size_in_bytes (const_tree type)
400fbf9f
JW
1759{
1760 enum tree_code code = TREE_CODE (type);
1761
fed3cef0
RK
1762 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK)
1763 return size_one_node;
1764
d0f062fb 1765 if (!COMPLETE_OR_VOID_TYPE_P (type))
400fbf9f
JW
1766 {
1767 error ("arithmetic on pointer to an incomplete type");
fed3cef0 1768 return size_one_node;
400fbf9f
JW
1769 }
1770
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;
2011
2012 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2013 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2014 params->quick_push (expr_addr);
2015 params->quick_push (tmp_addr);
2016 params->quick_push (seq_cst);
2017 func_call = build_function_call_vec (loc, fndecl, params, NULL);
2018
2019 /* Return tmp which contains the value loaded. */
2020 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2021 }
2022 return exp;
2023}
2024
522ddfa2
JM
2025/* EXP is an expression of integer type. Apply the integer promotions
2026 to it and return the promoted value. */
400fbf9f
JW
2027
2028tree
522ddfa2 2029perform_integral_promotions (tree exp)
400fbf9f 2030{
b3694847
SS
2031 tree type = TREE_TYPE (exp);
2032 enum tree_code code = TREE_CODE (type);
400fbf9f 2033
522ddfa2 2034 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2035
400fbf9f
JW
2036 /* Normally convert enums to int,
2037 but convert wide enums to something wider. */
2038 if (code == ENUMERAL_TYPE)
2039 {
b0c48229
NB
2040 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2041 TYPE_PRECISION (integer_type_node)),
2042 ((TYPE_PRECISION (type)
2043 >= TYPE_PRECISION (integer_type_node))
8df83eae 2044 && TYPE_UNSIGNED (type)));
05bccae2 2045
400fbf9f
JW
2046 return convert (type, exp);
2047 }
2048
522ddfa2
JM
2049 /* ??? This should no longer be needed now bit-fields have their
2050 proper types. */
9753f113 2051 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2052 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2053 /* If it's thinner than an int, promote it like a
d72040f5 2054 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2055 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2056 TYPE_PRECISION (integer_type_node)))
f458d1d5 2057 return convert (integer_type_node, exp);
9753f113 2058
d72040f5 2059 if (c_promoting_integer_type_p (type))
400fbf9f 2060 {
f458d1d5 2061 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2062 if (TYPE_UNSIGNED (type)
f458d1d5 2063 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2064 return convert (unsigned_type_node, exp);
05bccae2 2065
400fbf9f
JW
2066 return convert (integer_type_node, exp);
2067 }
05bccae2 2068
522ddfa2
JM
2069 return exp;
2070}
2071
2072
2073/* Perform default promotions for C data used in expressions.
46bdb9cf 2074 Enumeral types or short or char are converted to int.
522ddfa2
JM
2075 In addition, manifest constants symbols are replaced by their values. */
2076
2077tree
2078default_conversion (tree exp)
2079{
2080 tree orig_exp;
2081 tree type = TREE_TYPE (exp);
2082 enum tree_code code = TREE_CODE (type);
40449a90 2083 tree promoted_type;
522ddfa2 2084
ebfbbdc5
JJ
2085 mark_exp_read (exp);
2086
46bdb9cf
JM
2087 /* Functions and arrays have been converted during parsing. */
2088 gcc_assert (code != FUNCTION_TYPE);
2089 if (code == ARRAY_TYPE)
2090 return exp;
522ddfa2
JM
2091
2092 /* Constants can be used directly unless they're not loadable. */
2093 if (TREE_CODE (exp) == CONST_DECL)
2094 exp = DECL_INITIAL (exp);
2095
522ddfa2
JM
2096 /* Strip no-op conversions. */
2097 orig_exp = exp;
2098 STRIP_TYPE_NOPS (exp);
2099
2100 if (TREE_NO_WARNING (orig_exp))
2101 TREE_NO_WARNING (exp) = 1;
2102
400fbf9f
JW
2103 if (code == VOID_TYPE)
2104 {
2105 error ("void value not ignored as it ought to be");
2106 return error_mark_node;
2107 }
808d6eaa
JM
2108
2109 exp = require_complete_type (exp);
2110 if (exp == error_mark_node)
2111 return error_mark_node;
2112
40449a90
SL
2113 promoted_type = targetm.promoted_type (type);
2114 if (promoted_type)
2115 return convert (promoted_type, exp);
2116
808d6eaa
JM
2117 if (INTEGRAL_TYPE_P (type))
2118 return perform_integral_promotions (exp);
2119
400fbf9f
JW
2120 return exp;
2121}
2122\f
0fb96aa4 2123/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2124
2125 If the component name is not found, returns NULL_TREE. Otherwise,
2126 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2127 stepping down the chain to the component, which is in the last
2128 TREE_VALUE of the list. Normally the list is of length one, but if
2129 the component is embedded within (nested) anonymous structures or
2130 unions, the list steps down the chain to the component. */
2f6e4e97 2131
2f2d13da 2132static tree
0fb96aa4 2133lookup_field (tree type, tree component)
2f2d13da
DE
2134{
2135 tree field;
2136
2137 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2138 to the field elements. Use a binary search on this array to quickly
2139 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2140 will always be set for structures which have many elements. */
2141
22a0b85f 2142 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2143 {
2144 int bot, top, half;
d07605f5 2145 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2146
2147 field = TYPE_FIELDS (type);
2148 bot = 0;
d07605f5 2149 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2150 while (top - bot > 1)
2151 {
2f2d13da
DE
2152 half = (top - bot + 1) >> 1;
2153 field = field_array[bot+half];
2154
2155 if (DECL_NAME (field) == NULL_TREE)
2156 {
2157 /* Step through all anon unions in linear fashion. */
2158 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2159 {
2f2d13da 2160 field = field_array[bot++];
a68b98cf
RK
2161 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2162 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2163 {
0fb96aa4 2164 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2165
2166 if (anon)
2167 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2168
2169 /* The Plan 9 compiler permits referring
2170 directly to an anonymous struct/union field
2171 using a typedef name. */
2172 if (flag_plan9_extensions
2173 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2174 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2175 == TYPE_DECL)
2176 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2177 == component))
2178 break;
2f6e4e97 2179 }
2f2d13da
DE
2180 }
2181
2182 /* Entire record is only anon unions. */
2183 if (bot > top)
2184 return NULL_TREE;
2185
2186 /* Restart the binary search, with new lower bound. */
2187 continue;
2188 }
2189
e8b87aac 2190 if (DECL_NAME (field) == component)
2f2d13da 2191 break;
e8b87aac 2192 if (DECL_NAME (field) < component)
2f2d13da
DE
2193 bot += half;
2194 else
2195 top = bot + half;
2196 }
2197
2198 if (DECL_NAME (field_array[bot]) == component)
2199 field = field_array[bot];
2200 else if (DECL_NAME (field) != component)
e9b2c823 2201 return NULL_TREE;
2f2d13da
DE
2202 }
2203 else
2204 {
910ad8de 2205 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2206 {
e9b2c823
NB
2207 if (DECL_NAME (field) == NULL_TREE
2208 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2209 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2210 {
0fb96aa4 2211 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2212
e9b2c823
NB
2213 if (anon)
2214 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2215
2216 /* The Plan 9 compiler permits referring directly to an
2217 anonymous struct/union field using a typedef
2218 name. */
2219 if (flag_plan9_extensions
2220 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2221 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2222 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2223 == component))
2224 break;
2f2d13da
DE
2225 }
2226
2227 if (DECL_NAME (field) == component)
2228 break;
2229 }
e9b2c823
NB
2230
2231 if (field == NULL_TREE)
2232 return NULL_TREE;
2f2d13da
DE
2233 }
2234
e9b2c823 2235 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2236}
2237
c2255bc4
AH
2238/* Make an expression to refer to the COMPONENT field of structure or
2239 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2240 location of the COMPONENT_REF. */
400fbf9f
JW
2241
2242tree
c2255bc4 2243build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2244{
b3694847
SS
2245 tree type = TREE_TYPE (datum);
2246 enum tree_code code = TREE_CODE (type);
2247 tree field = NULL;
2248 tree ref;
1e57bf47 2249 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2250
7a3ea201
RH
2251 if (!objc_is_public (datum, component))
2252 return error_mark_node;
2253
46a88c12 2254 /* Detect Objective-C property syntax object.property. */
668ea4b1 2255 if (c_dialect_objc ()
46a88c12 2256 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2257 return ref;
2258
400fbf9f
JW
2259 /* See if there is a field or component with name COMPONENT. */
2260
2261 if (code == RECORD_TYPE || code == UNION_TYPE)
2262 {
d0f062fb 2263 if (!COMPLETE_TYPE_P (type))
400fbf9f 2264 {
7a228918 2265 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2266 return error_mark_node;
2267 }
2268
0fb96aa4 2269 field = lookup_field (type, component);
400fbf9f
JW
2270
2271 if (!field)
2272 {
c2255bc4 2273 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2274 return error_mark_node;
2275 }
400fbf9f 2276
e9b2c823
NB
2277 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2278 This might be better solved in future the way the C++ front
2279 end does it - by giving the anonymous entities each a
2280 separate name and type, and then have build_component_ref
2281 recursively call itself. We can't do that here. */
46ea50cb 2282 do
19d76e60 2283 {
e9b2c823 2284 tree subdatum = TREE_VALUE (field);
efed193e
JM
2285 int quals;
2286 tree subtype;
1e57bf47 2287 bool use_datum_quals;
e9b2c823
NB
2288
2289 if (TREE_TYPE (subdatum) == error_mark_node)
2290 return error_mark_node;
2291
1e57bf47
JM
2292 /* If this is an rvalue, it does not have qualifiers in C
2293 standard terms and we must avoid propagating such
2294 qualifiers down to a non-lvalue array that is then
2295 converted to a pointer. */
2296 use_datum_quals = (datum_lvalue
2297 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2298
efed193e 2299 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2300 if (use_datum_quals)
2301 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2302 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2303
2304 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2305 NULL_TREE);
c2255bc4 2306 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2307 if (TREE_READONLY (subdatum)
2308 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2309 TREE_READONLY (ref) = 1;
1e57bf47
JM
2310 if (TREE_THIS_VOLATILE (subdatum)
2311 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2312 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2313
2314 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2315 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2316
19d76e60 2317 datum = ref;
46ea50cb
RS
2318
2319 field = TREE_CHAIN (field);
19d76e60 2320 }
46ea50cb 2321 while (field);
19d76e60 2322
400fbf9f
JW
2323 return ref;
2324 }
2325 else if (code != ERROR_MARK)
c2255bc4
AH
2326 error_at (loc,
2327 "request for member %qE in something not a structure or union",
2328 component);
400fbf9f
JW
2329
2330 return error_mark_node;
2331}
2332\f
2333/* Given an expression PTR for a pointer, return an expression
2334 for the value pointed to.
6a3799eb
AH
2335 ERRORSTRING is the name of the operator to appear in error messages.
2336
2337 LOC is the location to use for the generated tree. */
400fbf9f
JW
2338
2339tree
dd865ef6 2340build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2341{
b3694847
SS
2342 tree pointer = default_conversion (ptr);
2343 tree type = TREE_TYPE (pointer);
6a3799eb 2344 tree ref;
400fbf9f
JW
2345
2346 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2347 {
1043771b 2348 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2349 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2350 {
2351 /* If a warning is issued, mark it to avoid duplicates from
2352 the backend. This only needs to be done at
2353 warn_strict_aliasing > 2. */
2354 if (warn_strict_aliasing > 2)
2355 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2356 type, TREE_OPERAND (pointer, 0)))
2357 TREE_NO_WARNING (pointer) = 1;
2358 }
2359
870cc33b 2360 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2361 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2362 == TREE_TYPE (type)))
6a3799eb
AH
2363 {
2364 ref = TREE_OPERAND (pointer, 0);
2365 protected_set_expr_location (ref, loc);
2366 return ref;
2367 }
870cc33b
RS
2368 else
2369 {
2370 tree t = TREE_TYPE (type);
46df2823 2371
984dfd8c 2372 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2373
baae9b65 2374 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2375 {
c9f9eb5d 2376 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2377 return error_mark_node;
2378 }
7d882b83 2379 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2380 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2381
2382 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2383 so that we get the proper error message if the result is used
2384 to assign to. Also, &* is supposed to be a no-op.
2385 And ANSI C seems to specify that the type of the result
2386 should be the const type. */
2387 /* A de-reference of a pointer to const is not a const. It is valid
2388 to change it via some other pointer. */
2389 TREE_READONLY (ref) = TYPE_READONLY (t);
2390 TREE_SIDE_EFFECTS (ref)
271bd540 2391 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2392 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2393 protected_set_expr_location (ref, loc);
870cc33b
RS
2394 return ref;
2395 }
2396 }
400fbf9f 2397 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2398 invalid_indirection_error (loc, type, errstring);
2399
400fbf9f
JW
2400 return error_mark_node;
2401}
2402
2403/* This handles expressions of the form "a[i]", which denotes
2404 an array reference.
2405
2406 This is logically equivalent in C to *(a+i), but we may do it differently.
2407 If A is a variable or a member, we generate a primitive ARRAY_REF.
2408 This avoids forcing the array out of registers, and can work on
2409 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2410 by functions).
2411
30cd1c5d
AS
2412 For vector types, allow vector[i] but not i[vector], and create
2413 *(((type*)&vectortype) + i) for the expression.
2414
6a3799eb 2415 LOC is the location to use for the returned expression. */
400fbf9f
JW
2416
2417tree
c2255bc4 2418build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2419{
6a3799eb 2420 tree ret;
a4ab7973 2421 bool swapped = false;
400fbf9f
JW
2422 if (TREE_TYPE (array) == error_mark_node
2423 || TREE_TYPE (index) == error_mark_node)
2424 return error_mark_node;
2425
36536d79
BI
2426 if (flag_enable_cilkplus && contains_array_notation_expr (index))
2427 {
2428 size_t rank = 0;
2429 if (!find_rank (loc, index, index, true, &rank))
2430 return error_mark_node;
2431 if (rank > 1)
2432 {
2433 error_at (loc, "rank of the array's index is greater than 1");
2434 return error_mark_node;
2435 }
2436 }
a4ab7973 2437 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2438 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2439 /* Allow vector[index] but not index[vector]. */
2440 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2441 {
a4ab7973
JM
2442 tree temp;
2443 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2444 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2445 {
f90e8e2e 2446 error_at (loc,
30cd1c5d
AS
2447 "subscripted value is neither array nor pointer nor vector");
2448
fdeefd49
RS
2449 return error_mark_node;
2450 }
a4ab7973
JM
2451 temp = array;
2452 array = index;
2453 index = temp;
2454 swapped = true;
2455 }
2456
2457 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2458 {
a63068b6 2459 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2460 return error_mark_node;
2461 }
2462
2463 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2464 {
a63068b6 2465 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2466 return error_mark_node;
2467 }
2468
ff6b6641
GDR
2469 /* ??? Existing practice has been to warn only when the char
2470 index is syntactically the index, not for char[array]. */
2471 if (!swapped)
2472 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2473
2474 /* Apply default promotions *after* noticing character types. */
2475 index = default_conversion (index);
2476
2477 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2478
7edaa4d2 2479 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2480
2481 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2482 {
e4d35515 2483 tree rval, type;
fdeefd49 2484
400fbf9f
JW
2485 /* An array that is indexed by a non-constant
2486 cannot be stored in a register; we must be able to do
2487 address arithmetic on its address.
2488 Likewise an array of elements of variable size. */
2489 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2490 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2491 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2492 {
dffd7eb6 2493 if (!c_mark_addressable (array))
400fbf9f
JW
2494 return error_mark_node;
2495 }
e6d52559
JW
2496 /* An array that is indexed by a constant value which is not within
2497 the array bounds cannot be stored in a register either; because we
2498 would get a crash in store_bit_field/extract_bit_field when trying
2499 to access a non-existent part of the register. */
2500 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2501 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2502 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2503 {
dffd7eb6 2504 if (!c_mark_addressable (array))
e6d52559
JW
2505 return error_mark_node;
2506 }
400fbf9f 2507
400fbf9f
JW
2508 if (pedantic)
2509 {
2510 tree foo = array;
2511 while (TREE_CODE (foo) == COMPONENT_REF)
2512 foo = TREE_OPERAND (foo, 0);
5baeaac0 2513 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2514 pedwarn (loc, OPT_Wpedantic,
fcf73884 2515 "ISO C forbids subscripting %<register%> array");
3f75a254 2516 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2517 pedwarn (loc, OPT_Wpedantic,
fcf73884 2518 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2519 }
2520
46df2823 2521 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2522 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2523 /* Array ref is const/volatile if the array elements are
c22cacf3 2524 or if the array is. */
400fbf9f
JW
2525 TREE_READONLY (rval)
2526 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2527 | TREE_READONLY (array));
2528 TREE_SIDE_EFFECTS (rval)
2529 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2530 | TREE_SIDE_EFFECTS (array));
2531 TREE_THIS_VOLATILE (rval)
2532 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2533 /* This was added by rms on 16 Nov 91.
2f6e4e97 2534 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2535 in an inline function.
2536 Hope it doesn't break something else. */
2537 | TREE_THIS_VOLATILE (array));
928c19bb 2538 ret = require_complete_type (rval);
6a3799eb
AH
2539 protected_set_expr_location (ret, loc);
2540 return ret;
400fbf9f 2541 }
a4ab7973
JM
2542 else
2543 {
2544 tree ar = default_conversion (array);
400fbf9f 2545
a4ab7973
JM
2546 if (ar == error_mark_node)
2547 return ar;
400fbf9f 2548
a4ab7973
JM
2549 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2550 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2551
ba47d38d 2552 return build_indirect_ref
c9f9eb5d 2553 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2554 RO_ARRAY_INDEXING);
a4ab7973 2555 }
400fbf9f
JW
2556}
2557\f
7e585d16 2558/* Build an external reference to identifier ID. FUN indicates
766beb40 2559 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2560 location of the identifier. This sets *TYPE to the type of the
2561 identifier, which is not the same as the type of the returned value
2562 for CONST_DECLs defined as enum constants. If the type of the
2563 identifier is not available, *TYPE is set to NULL. */
7e585d16 2564tree
c2255bc4 2565build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2566{
2567 tree ref;
2568 tree decl = lookup_name (id);
16b34ad6
ZL
2569
2570 /* In Objective-C, an instance variable (ivar) may be preferred to
2571 whatever lookup_name() found. */
2572 decl = objc_lookup_ivar (decl, id);
7e585d16 2573
6866c6e8 2574 *type = NULL;
339a28b9 2575 if (decl && decl != error_mark_node)
6866c6e8
ILT
2576 {
2577 ref = decl;
2578 *type = TREE_TYPE (ref);
2579 }
339a28b9
ZW
2580 else if (fun)
2581 /* Implicit function declaration. */
c2255bc4 2582 ref = implicitly_declare (loc, id);
339a28b9
ZW
2583 else if (decl == error_mark_node)
2584 /* Don't complain about something that's already been
2585 complained about. */
2586 return error_mark_node;
2587 else
2588 {
c2255bc4 2589 undeclared_variable (loc, id);
339a28b9
ZW
2590 return error_mark_node;
2591 }
7e585d16
ZW
2592
2593 if (TREE_TYPE (ref) == error_mark_node)
2594 return error_mark_node;
2595
339a28b9 2596 if (TREE_DEPRECATED (ref))
9b86d6bb 2597 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2598
ad960f56 2599 /* Recursive call does not count as usage. */
b8698a0f 2600 if (ref != current_function_decl)
ad960f56 2601 {
ad960f56
MLI
2602 TREE_USED (ref) = 1;
2603 }
7e585d16 2604
bc4b653b
JM
2605 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2606 {
2607 if (!in_sizeof && !in_typeof)
2608 C_DECL_USED (ref) = 1;
2609 else if (DECL_INITIAL (ref) == 0
2610 && DECL_EXTERNAL (ref)
2611 && !TREE_PUBLIC (ref))
2612 record_maybe_used_decl (ref);
2613 }
2614
7e585d16
ZW
2615 if (TREE_CODE (ref) == CONST_DECL)
2616 {
6193b8b7 2617 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2618
2619 if (warn_cxx_compat
2620 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2621 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2622 {
2623 warning_at (loc, OPT_Wc___compat,
2624 ("enum constant defined in struct or union "
2625 "is not visible in C++"));
2626 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2627 }
2628
7e585d16
ZW
2629 ref = DECL_INITIAL (ref);
2630 TREE_CONSTANT (ref) = 1;
2631 }
6a29edea 2632 else if (current_function_decl != 0
4b1e44be 2633 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2634 && (TREE_CODE (ref) == VAR_DECL
2635 || TREE_CODE (ref) == PARM_DECL
2636 || TREE_CODE (ref) == FUNCTION_DECL))
2637 {
2638 tree context = decl_function_context (ref);
2f6e4e97 2639
6a29edea
EB
2640 if (context != 0 && context != current_function_decl)
2641 DECL_NONLOCAL (ref) = 1;
2642 }
71113fcd
GK
2643 /* C99 6.7.4p3: An inline definition of a function with external
2644 linkage ... shall not contain a reference to an identifier with
2645 internal linkage. */
2646 else if (current_function_decl != 0
2647 && DECL_DECLARED_INLINE_P (current_function_decl)
2648 && DECL_EXTERNAL (current_function_decl)
2649 && VAR_OR_FUNCTION_DECL_P (ref)
2650 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2651 && ! TREE_PUBLIC (ref)
2652 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2653 record_inline_static (loc, current_function_decl, ref,
2654 csi_internal);
7e585d16
ZW
2655
2656 return ref;
2657}
2658
bc4b653b
JM
2659/* Record details of decls possibly used inside sizeof or typeof. */
2660struct maybe_used_decl
2661{
2662 /* The decl. */
2663 tree decl;
2664 /* The level seen at (in_sizeof + in_typeof). */
2665 int level;
2666 /* The next one at this level or above, or NULL. */
2667 struct maybe_used_decl *next;
2668};
2669
2670static struct maybe_used_decl *maybe_used_decls;
2671
2672/* Record that DECL, an undefined static function reference seen
2673 inside sizeof or typeof, might be used if the operand of sizeof is
2674 a VLA type or the operand of typeof is a variably modified
2675 type. */
2676
4e2fb7de 2677static void
bc4b653b
JM
2678record_maybe_used_decl (tree decl)
2679{
2680 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2681 t->decl = decl;
2682 t->level = in_sizeof + in_typeof;
2683 t->next = maybe_used_decls;
2684 maybe_used_decls = t;
2685}
2686
2687/* Pop the stack of decls possibly used inside sizeof or typeof. If
2688 USED is false, just discard them. If it is true, mark them used
2689 (if no longer inside sizeof or typeof) or move them to the next
2690 level up (if still inside sizeof or typeof). */
2691
2692void
2693pop_maybe_used (bool used)
2694{
2695 struct maybe_used_decl *p = maybe_used_decls;
2696 int cur_level = in_sizeof + in_typeof;
2697 while (p && p->level > cur_level)
2698 {
2699 if (used)
2700 {
2701 if (cur_level == 0)
2702 C_DECL_USED (p->decl) = 1;
2703 else
2704 p->level = cur_level;
2705 }
2706 p = p->next;
2707 }
2708 if (!used || cur_level == 0)
2709 maybe_used_decls = p;
2710}
2711
2712/* Return the result of sizeof applied to EXPR. */
2713
2714struct c_expr
c2255bc4 2715c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2716{
2717 struct c_expr ret;
ad97f4be
JM
2718 if (expr.value == error_mark_node)
2719 {
2720 ret.value = error_mark_node;
2721 ret.original_code = ERROR_MARK;
6866c6e8 2722 ret.original_type = NULL;
ad97f4be
JM
2723 pop_maybe_used (false);
2724 }
2725 else
2726 {
928c19bb
JM
2727 bool expr_const_operands = true;
2728 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2729 &expr_const_operands);
c2255bc4 2730 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2731 c_last_sizeof_arg = expr.value;
2732 ret.original_code = SIZEOF_EXPR;
6866c6e8 2733 ret.original_type = NULL;
928c19bb 2734 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2735 {
2736 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2737 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2738 folded_expr, ret.value);
2739 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2740 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2741 }
928c19bb 2742 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2743 }
bc4b653b
JM
2744 return ret;
2745}
2746
2747/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2748 name passed to sizeof (rather than the type itself). LOC is the
2749 location of the original expression. */
bc4b653b
JM
2750
2751struct c_expr
c2255bc4 2752c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2753{
2754 tree type;
2755 struct c_expr ret;
928c19bb
JM
2756 tree type_expr = NULL_TREE;
2757 bool type_expr_const = true;
2758 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2759 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2760 c_last_sizeof_arg = type;
2761 ret.original_code = SIZEOF_EXPR;
6866c6e8 2762 ret.original_type = NULL;
24070fcb
JM
2763 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2764 && c_vla_type_p (type))
928c19bb 2765 {
24070fcb
JM
2766 /* If the type is a [*] array, it is a VLA but is represented as
2767 having a size of zero. In such a case we must ensure that
2768 the result of sizeof does not get folded to a constant by
2769 c_fully_fold, because if the size is evaluated the result is
2770 not constant and so constraints on zero or negative size
2771 arrays must not be applied when this sizeof call is inside
2772 another array declarator. */
2773 if (!type_expr)
2774 type_expr = integer_zero_node;
928c19bb
JM
2775 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2776 type_expr, ret.value);
2777 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2778 }
16464cc1
VR
2779 pop_maybe_used (type != error_mark_node
2780 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2781 return ret;
2782}
2783
400fbf9f 2784/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2785 The function call is at LOC.
400fbf9f
JW
2786 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2787 TREE_VALUE of each node is a parameter-expression.
2788 FUNCTION's data type may be a function type or a pointer-to-function. */
2789
2790tree
c2255bc4 2791build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2792{
9771b263 2793 vec<tree, va_gc> *v;
bbbbb16a
ILT
2794 tree ret;
2795
9771b263 2796 vec_alloc (v, list_length (params));
bbbbb16a 2797 for (; params; params = TREE_CHAIN (params))
9771b263
DN
2798 v->quick_push (TREE_VALUE (params));
2799 ret = build_function_call_vec (loc, function, v, NULL);
2800 vec_free (v);
bbbbb16a
ILT
2801 return ret;
2802}
2803
ae52741c
MLI
2804/* Give a note about the location of the declaration of DECL. */
2805
2806static void inform_declaration (tree decl)
2807{
2808 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2809 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2810}
2811
bbbbb16a
ILT
2812/* Build a function call to function FUNCTION with parameters PARAMS.
2813 ORIGTYPES, if not NULL, is a vector of types; each element is
2814 either NULL or the original type of the corresponding element in
2815 PARAMS. The original type may differ from TREE_TYPE of the
2816 parameter for enums. FUNCTION's data type may be a function type
2817 or pointer-to-function. This function changes the elements of
2818 PARAMS. */
2819
2820tree
9771b263
DN
2821build_function_call_vec (location_t loc, tree function,
2822 vec<tree, va_gc> *params,
2823 vec<tree, va_gc> *origtypes)
400fbf9f 2824{
b3694847 2825 tree fntype, fundecl = 0;
4977bab6 2826 tree name = NULL_TREE, result;
c96f4f73 2827 tree tem;
94a0dd7b
SL
2828 int nargs;
2829 tree *argarray;
b8698a0f 2830
400fbf9f 2831
fc76e425 2832 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2833 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2834
2835 /* Convert anything with function type to a pointer-to-function. */
2836 if (TREE_CODE (function) == FUNCTION_DECL)
2837 {
58646b77
PB
2838 /* Implement type-directed function overloading for builtins.
2839 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2840 handle all the type checking. The result is a complete expression
2841 that implements this function call. */
c2255bc4 2842 tem = resolve_overloaded_builtin (loc, function, params);
58646b77
PB
2843 if (tem)
2844 return tem;
48ae6c13 2845
400fbf9f 2846 name = DECL_NAME (function);
0a35513e
AH
2847
2848 if (flag_tm)
2849 tm_malloc_replacement (function);
a5eadacc 2850 fundecl = function;
86951993
AM
2851 /* Atomic functions have type checking/casting already done. They are
2852 often rewritten and don't match the original parameter list. */
2853 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2854 origtypes = NULL;
36536d79
BI
2855
2856 if (flag_enable_cilkplus
2857 && is_cilkplus_reduce_builtin (function))
2858 origtypes = NULL;
400fbf9f 2859 }
f2a71bbc 2860 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2861 function = function_to_pointer_conversion (loc, function);
400fbf9f 2862
6e955430
ZL
2863 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2864 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2865 if (params && !params->is_empty ())
2866 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2867
928c19bb
JM
2868 function = c_fully_fold (function, false, NULL);
2869
400fbf9f
JW
2870 fntype = TREE_TYPE (function);
2871
2872 if (TREE_CODE (fntype) == ERROR_MARK)
2873 return error_mark_node;
2874
2875 if (!(TREE_CODE (fntype) == POINTER_TYPE
2876 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2877 {
ae52741c
MLI
2878 if (!flag_diagnostics_show_caret)
2879 error_at (loc,
2880 "called object %qE is not a function or function pointer",
2881 function);
2882 else if (DECL_P (function))
2883 {
2884 error_at (loc,
2885 "called object %qD is not a function or function pointer",
2886 function);
2887 inform_declaration (function);
2888 }
2889 else
2890 error_at (loc,
2891 "called object is not a function or function pointer");
400fbf9f
JW
2892 return error_mark_node;
2893 }
2894
5ce89b2e
JM
2895 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2896 current_function_returns_abnormally = 1;
2897
400fbf9f
JW
2898 /* fntype now gets the type of function pointed to. */
2899 fntype = TREE_TYPE (fntype);
2900
ab4194da
JM
2901 /* Convert the parameters to the types declared in the
2902 function prototype, or apply default promotions. */
2903
bbbbb16a
ILT
2904 nargs = convert_arguments (TYPE_ARG_TYPES (fntype), params, origtypes,
2905 function, fundecl);
ab4194da
JM
2906 if (nargs < 0)
2907 return error_mark_node;
2908
c96f4f73
EB
2909 /* Check that the function is called through a compatible prototype.
2910 If it is not, replace the call by a trap, wrapped up in a compound
2911 expression if necessary. This has the nice side-effect to prevent
2912 the tree-inliner from generating invalid assignment trees which may
58393038 2913 blow up in the RTL expander later. */
1043771b 2914 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2915 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2916 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2917 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2918 {
2919 tree return_type = TREE_TYPE (fntype);
e79983f4
MM
2920 tree trap = build_function_call (loc,
2921 builtin_decl_explicit (BUILT_IN_TRAP),
c96f4f73 2922 NULL_TREE);
ab4194da 2923 int i;
c96f4f73
EB
2924
2925 /* This situation leads to run-time undefined behavior. We can't,
2926 therefore, simply error unless we can prove that all possible
2927 executions of the program must execute the code. */
c2255bc4 2928 if (warning_at (loc, 0, "function called through a non-compatible type"))
71205d17
MLI
2929 /* We can, however, treat "undefined" any way we please.
2930 Call abort to encourage the user to fix the program. */
c2255bc4 2931 inform (loc, "if this code is reached, the program will abort");
ab4194da
JM
2932 /* Before the abort, allow the function arguments to exit or
2933 call longjmp. */
2934 for (i = 0; i < nargs; i++)
9771b263 2935 trap = build2 (COMPOUND_EXPR, void_type_node, (*params)[i], trap);
bba745c1 2936
c96f4f73 2937 if (VOID_TYPE_P (return_type))
3ce62965
JM
2938 {
2939 if (TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
db3927fb 2940 pedwarn (loc, 0,
3ce62965
JM
2941 "function with qualified void return type called");
2942 return trap;
2943 }
c96f4f73
EB
2944 else
2945 {
2946 tree rhs;
2947
2948 if (AGGREGATE_TYPE_P (return_type))
c2255bc4 2949 rhs = build_compound_literal (loc, return_type,
9771b263
DN
2950 build_constructor (return_type,
2951 NULL),
928c19bb 2952 false);
c96f4f73 2953 else
e8160c9a 2954 rhs = build_zero_cst (return_type);
c96f4f73 2955
3ce62965
JM
2956 return require_complete_type (build2 (COMPOUND_EXPR, return_type,
2957 trap, rhs));
c96f4f73
EB
2958 }
2959 }
2960
9771b263 2961 argarray = vec_safe_address (params);
bbbbb16a 2962
83322951
RG
2963 /* Check that arguments to builtin functions match the expectations. */
2964 if (fundecl
2965 && DECL_BUILT_IN (fundecl)
2966 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2967 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2968 return error_mark_node;
400fbf9f 2969
83322951 2970 /* Check that the arguments to the function are valid. */
dde05067 2971 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2972
928c19bb
JM
2973 if (name != NULL_TREE
2974 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2975 {
928c19bb 2976 if (require_constant_value)
b8698a0f 2977 result =
db3927fb
AH
2978 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2979 function, nargs, argarray);
928c19bb 2980 else
db3927fb
AH
2981 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2982 function, nargs, argarray);
928c19bb
JM
2983 if (TREE_CODE (result) == NOP_EXPR
2984 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2985 STRIP_TYPE_NOPS (result);
bf730f15
RS
2986 }
2987 else
db3927fb
AH
2988 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2989 function, nargs, argarray);
b0b3afb2 2990
71653180 2991 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2992 {
2993 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2994 pedwarn (loc, 0,
3ce62965
JM
2995 "function with qualified void return type called");
2996 return result;
2997 }
1eb8759b 2998 return require_complete_type (result);
400fbf9f
JW
2999}
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
94a0dd7b 3016 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3017 than the length of VALUES in some error situations), or -1 on
3018 failure. */
94a0dd7b
SL
3019
3020static int
9771b263
DN
3021convert_arguments (tree typelist, vec<tree, va_gc> *values,
3022 vec<tree, va_gc> *origtypes, tree function, tree fundecl)
400fbf9f 3023{
bbbbb16a
ILT
3024 tree typetail, val;
3025 unsigned int parmnum;
06302a02 3026 bool error_args = false;
b5d32c25
KG
3027 const bool type_generic = fundecl
3028 && lookup_attribute ("type generic", TYPE_ATTRIBUTES(TREE_TYPE (fundecl)));
8ce94e44 3029 bool type_generic_remove_excess_precision = false;
03dafa61 3030 tree selector;
03dafa61 3031
2ac2f164
JM
3032 /* Change pointer to function to the function itself for
3033 diagnostics. */
03dafa61
JM
3034 if (TREE_CODE (function) == ADDR_EXPR
3035 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3036 function = TREE_OPERAND (function, 0);
03dafa61
JM
3037
3038 /* Handle an ObjC selector specially for diagnostics. */
3039 selector = objc_message_selector ();
400fbf9f 3040
8ce94e44
JM
3041 /* For type-generic built-in functions, determine whether excess
3042 precision should be removed (classification) or not
3043 (comparison). */
3044 if (type_generic
3045 && DECL_BUILT_IN (fundecl)
3046 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3047 {
3048 switch (DECL_FUNCTION_CODE (fundecl))
3049 {
3050 case BUILT_IN_ISFINITE:
3051 case BUILT_IN_ISINF:
3052 case BUILT_IN_ISINF_SIGN:
3053 case BUILT_IN_ISNAN:
3054 case BUILT_IN_ISNORMAL:
3055 case BUILT_IN_FPCLASSIFY:
3056 type_generic_remove_excess_precision = true;
3057 break;
3058
3059 default:
3060 type_generic_remove_excess_precision = false;
3061 break;
3062 }
3063 }
6d6efbb3
BI
3064 if (flag_enable_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
3065 return vec_safe_length (values);
8ce94e44 3066
400fbf9f 3067 /* Scan the given expressions and types, producing individual
bbbbb16a 3068 converted arguments. */
400fbf9f 3069
bbbbb16a 3070 for (typetail = typelist, parmnum = 0;
9771b263 3071 values && values->iterate (parmnum, &val);
bbbbb16a 3072 ++parmnum)
400fbf9f 3073 {
b3694847 3074 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3075 tree valtype = TREE_TYPE (val);
03dafa61
JM
3076 tree rname = function;
3077 int argnum = parmnum + 1;
4d3e6fae 3078 const char *invalid_func_diag;
8ce94e44 3079 bool excess_precision = false;
928c19bb 3080 bool npc;
bbbbb16a 3081 tree parmval;
400fbf9f
JW
3082
3083 if (type == void_type_node)
3084 {
19dc6d01
NP
3085 if (selector)
3086 error_at (input_location,
3087 "too many arguments to method %qE", selector);
3088 else
3089 error_at (input_location,
3090 "too many arguments to function %qE", function);
ae52741c 3091 inform_declaration (fundecl);
94a0dd7b 3092 return parmnum;
400fbf9f
JW
3093 }
3094
03dafa61
JM
3095 if (selector && argnum > 2)
3096 {
3097 rname = selector;
3098 argnum -= 2;
3099 }
3100
928c19bb 3101 npc = null_pointer_constant_p (val);
8ce94e44
JM
3102
3103 /* If there is excess precision and a prototype, convert once to
3104 the required type rather than converting via the semantic
3105 type. Likewise without a prototype a float value represented
3106 as long double should be converted once to double. But for
3107 type-generic classification functions excess precision must
3108 be removed here. */
3109 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3110 && (type || !type_generic || !type_generic_remove_excess_precision))
3111 {
3112 val = TREE_OPERAND (val, 0);
3113 excess_precision = true;
3114 }
928c19bb 3115 val = c_fully_fold (val, false, NULL);
ed248cf7 3116 STRIP_TYPE_NOPS (val);
400fbf9f 3117
400fbf9f
JW
3118 val = require_complete_type (val);
3119
3120 if (type != 0)
3121 {
3122 /* Formal parm type is specified by a function prototype. */
400fbf9f 3123
20913689 3124 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f
JW
3125 {
3126 error ("type of formal parameter %d is incomplete", parmnum + 1);
3127 parmval = val;
3128 }
3129 else
3130 {
bbbbb16a
ILT
3131 tree origtype;
3132
d45cf215
RS
3133 /* Optionally warn about conversions that
3134 differ from the default conversions. */
05170031 3135 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3136 {
e3a64162 3137 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3138
aae43c5f 3139 if (INTEGRAL_TYPE_P (type)
8ce94e44 3140 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3141 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3142 "rather than floating due to prototype",
3143 argnum, rname);
03829ad2 3144 if (INTEGRAL_TYPE_P (type)
8ce94e44 3145 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3146 warning (0, "passing argument %d of %qE as integer "
03dafa61
JM
3147 "rather than complex due to prototype",
3148 argnum, rname);
aae43c5f 3149 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3150 && TREE_CODE (valtype) == REAL_TYPE)
d4ee4d25 3151 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3152 "rather than floating due to prototype",
3153 argnum, rname);
400fbf9f 3154 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3155 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3156 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3157 "rather than integer due to prototype",
3158 argnum, rname);
03829ad2 3159 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3160 && INTEGRAL_TYPE_P (valtype))
d4ee4d25 3161 warning (0, "passing argument %d of %qE as complex "
03dafa61
JM
3162 "rather than integer due to prototype",
3163 argnum, rname);
aae43c5f 3164 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3165 && TREE_CODE (valtype) == COMPLEX_TYPE)
d4ee4d25 3166 warning (0, "passing argument %d of %qE as floating "
03dafa61
JM
3167 "rather than complex due to prototype",
3168 argnum, rname);
aae43c5f
RK
3169 /* ??? At some point, messages should be written about
3170 conversions between complex types, but that's too messy
3171 to do now. */
d45cf215 3172 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3173 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3174 {
3175 /* Warn if any argument is passed as `float',
047de90b 3176 since without a prototype it would be `double'. */
9a8ce21f
JG
3177 if (formal_prec == TYPE_PRECISION (float_type_node)
3178 && type != dfloat32_type_node)
d4ee4d25 3179 warning (0, "passing argument %d of %qE as %<float%> "
03dafa61
JM
3180 "rather than %<double%> due to prototype",
3181 argnum, rname);
9a8ce21f
JG
3182
3183 /* Warn if mismatch between argument and prototype
3184 for decimal float types. Warn of conversions with
3185 binary float types and of precision narrowing due to
3186 prototype. */
8ce94e44 3187 else if (type != valtype
9a8ce21f
JG
3188 && (type == dfloat32_type_node
3189 || type == dfloat64_type_node
c22cacf3 3190 || type == dfloat128_type_node
8ce94e44
JM
3191 || valtype == dfloat32_type_node
3192 || valtype == dfloat64_type_node
3193 || valtype == dfloat128_type_node)
c22cacf3 3194 && (formal_prec
8ce94e44 3195 <= TYPE_PRECISION (valtype)
9a8ce21f 3196 || (type == dfloat128_type_node
8ce94e44 3197 && (valtype
c22cacf3 3198 != dfloat64_type_node
8ce94e44 3199 && (valtype
9a8ce21f
JG
3200 != dfloat32_type_node)))
3201 || (type == dfloat64_type_node
8ce94e44 3202 && (valtype
9a8ce21f
JG
3203 != dfloat32_type_node))))
3204 warning (0, "passing argument %d of %qE as %qT "
3205 "rather than %qT due to prototype",
8ce94e44 3206 argnum, rname, type, valtype);
9a8ce21f 3207
d45cf215 3208 }
3ed56f8a
KG
3209 /* Detect integer changing in width or signedness.
3210 These warnings are only activated with
05170031
MLI
3211 -Wtraditional-conversion, not with -Wtraditional. */
3212 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3213 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3214 {
d45cf215
RS
3215 tree would_have_been = default_conversion (val);
3216 tree type1 = TREE_TYPE (would_have_been);
3217
754a4d82 3218 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3219 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3220 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3221 /* No warning if function asks for enum
3222 and the actual arg is that enum type. */
3223 ;
3224 else if (formal_prec != TYPE_PRECISION (type1))
c2255bc4
AH
3225 warning (OPT_Wtraditional_conversion,
3226 "passing argument %d of %qE "
3176a0c2
DD
3227 "with different width due to prototype",
3228 argnum, rname);
8df83eae 3229 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3230 ;
800cd3b9
RS
3231 /* Don't complain if the formal parameter type
3232 is an enum, because we can't tell now whether
3233 the value was an enum--even the same enum. */
3234 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3235 ;
400fbf9f
JW
3236 else if (TREE_CODE (val) == INTEGER_CST
3237 && int_fits_type_p (val, type))
3238 /* Change in signedness doesn't matter
3239 if a constant value is unaffected. */
3240 ;
ce9895ae
RS
3241 /* If the value is extended from a narrower
3242 unsigned type, it doesn't matter whether we
3243 pass it as signed or unsigned; the value
3244 certainly is the same either way. */
8ce94e44
JM
3245 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3246 && TYPE_UNSIGNED (valtype))
ce9895ae 3247 ;
8df83eae 3248 else if (TYPE_UNSIGNED (type))
c2255bc4
AH
3249 warning (OPT_Wtraditional_conversion,
3250 "passing argument %d of %qE "
3176a0c2
DD
3251 "as unsigned due to prototype",
3252 argnum, rname);
3ed56f8a 3253 else
c2255bc4
AH
3254 warning (OPT_Wtraditional_conversion,
3255 "passing argument %d of %qE "
3176a0c2 3256 "as signed due to prototype", argnum, rname);
400fbf9f
JW
3257 }
3258 }
3259
8ce94e44
JM
3260 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3261 sake of better warnings from convert_and_check. */
3262 if (excess_precision)
3263 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3264 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
744aa42f
ILT
3265 parmval = convert_for_assignment (input_location, type, val,
3266 origtype, ic_argpass, npc,
2ac2f164
JM
3267 fundecl, function,
3268 parmnum + 1);
2f6e4e97 3269
61f71b34 3270 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3271 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3272 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3273 parmval = default_conversion (parmval);
400fbf9f 3274 }
400fbf9f 3275 }
8ce94e44
JM
3276 else if (TREE_CODE (valtype) == REAL_TYPE
3277 && (TYPE_PRECISION (valtype)
2531a1d9 3278 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3279 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3280 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3281 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3282 {
3283 if (type_generic)
bbbbb16a 3284 parmval = val;
b5d32c25 3285 else
0a0b3574
MM
3286 {
3287 /* Convert `float' to `double'. */
3288 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
3289 warning (OPT_Wdouble_promotion,
3290 "implicit conversion from %qT to %qT when passing "
3291 "argument to function",
3292 valtype, double_type_node);
3293 parmval = convert (double_type_node, val);
3294 }
b5d32c25 3295 }
8ce94e44
JM
3296 else if (excess_precision && !type_generic)
3297 /* A "double" argument with excess precision being passed
3298 without a prototype or in variable arguments. */
bbbbb16a 3299 parmval = convert (valtype, val);
c22cacf3
MS
3300 else if ((invalid_func_diag =
3301 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3302 {
3303 error (invalid_func_diag);
94a0dd7b 3304 return -1;
4d3e6fae 3305 }
400fbf9f
JW
3306 else
3307 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3308 parmval = default_conversion (val);
3309
9771b263 3310 (*values)[parmnum] = parmval;
06302a02
JJ
3311 if (parmval == error_mark_node)
3312 error_args = true;
400fbf9f
JW
3313
3314 if (typetail)
3315 typetail = TREE_CHAIN (typetail);
3316 }
3317
9771b263 3318 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3319
400fbf9f 3320 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3321 {
6d6efbb3
BI
3322 error_at (input_location,
3323 "too few arguments to function %qE", function);
3324 inform_declaration (fundecl);
3325 return -1;
3789b316 3326 }
400fbf9f 3327
06302a02 3328 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3329}
3330\f
43f6dfd3
RS
3331/* This is the entry point used by the parser to build unary operators
3332 in the input. CODE, a tree_code, specifies the unary operator, and
3333 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3334 CONVERT_EXPR for code.
3335
3336 LOC is the location to use for the tree generated.
3337*/
43f6dfd3
RS
3338
3339struct c_expr
c2255bc4 3340parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3341{
3342 struct c_expr result;
3343
c9f9eb5d 3344 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3345 result.original_code = code;
6866c6e8
ILT
3346 result.original_type = NULL;
3347
59c0753d 3348 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3349 overflow_warning (loc, result.value);
59c0753d 3350
43f6dfd3
RS
3351 return result;
3352}
3353
3354/* This is the entry point used by the parser to build binary operators
3355 in the input. CODE, a tree_code, specifies the binary operator, and
3356 ARG1 and ARG2 are the operands. In addition to constructing the
3357 expression, we check for operands that were written with other binary
ba47d38d
AH
3358 operators in a way that is likely to confuse the user.
3359
3360 LOCATION is the location of the binary operator. */
edc7c4ec 3361
487a92fe 3362struct c_expr
ba47d38d
AH
3363parser_build_binary_op (location_t location, enum tree_code code,
3364 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3365{
487a92fe 3366 struct c_expr result;
400fbf9f 3367
487a92fe
JM
3368 enum tree_code code1 = arg1.original_code;
3369 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3370 tree type1 = (arg1.original_type
3371 ? arg1.original_type
3372 : TREE_TYPE (arg1.value));
3373 tree type2 = (arg2.original_type
3374 ? arg2.original_type
3375 : TREE_TYPE (arg2.value));
400fbf9f 3376
ba47d38d
AH
3377 result.value = build_binary_op (location, code,
3378 arg1.value, arg2.value, 1);
487a92fe 3379 result.original_code = code;
6866c6e8 3380 result.original_type = NULL;
58bf601b 3381
487a92fe
JM
3382 if (TREE_CODE (result.value) == ERROR_MARK)
3383 return result;
400fbf9f 3384
ba47d38d
AH
3385 if (location != UNKNOWN_LOCATION)
3386 protected_set_expr_location (result.value, location);
3387
400fbf9f 3388 /* Check for cases such as x+y<<z which users are likely
487a92fe 3389 to misinterpret. */
400fbf9f 3390 if (warn_parentheses)
5d9de0d0
PC
3391 warn_about_parentheses (input_location, code,
3392 code1, arg1.value, code2, arg2.value);
001af587 3393
ca409efd 3394 if (warn_logical_op)
a243fb4a 3395 warn_logical_operator (input_location, code, TREE_TYPE (result.value),
ca409efd 3396 code1, arg1.value, code2, arg2.value);
63a08740 3397
e994a705
RS
3398 /* Warn about comparisons against string literals, with the exception
3399 of testing for equality or inequality of a string literal with NULL. */
3400 if (code == EQ_EXPR || code == NE_EXPR)
3401 {
3402 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3403 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3404 warning_at (location, OPT_Waddress,
3405 "comparison with string literal results in unspecified behavior");
e994a705
RS
3406 }
3407 else if (TREE_CODE_CLASS (code) == tcc_comparison
3408 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3409 warning_at (location, OPT_Waddress,
3410 "comparison with string literal results in unspecified behavior");
e994a705 3411
b8698a0f
L
3412 if (TREE_OVERFLOW_P (result.value)
3413 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3414 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3415 overflow_warning (location, result.value);
400fbf9f 3416
6866c6e8
ILT
3417 /* Warn about comparisons of different enum types. */
3418 if (warn_enum_compare
3419 && TREE_CODE_CLASS (code) == tcc_comparison
3420 && TREE_CODE (type1) == ENUMERAL_TYPE
3421 && TREE_CODE (type2) == ENUMERAL_TYPE
3422 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3423 warning_at (location, OPT_Wenum_compare,
3424 "comparison between %qT and %qT",
3425 type1, type2);
3426
400fbf9f
JW
3427 return result;
3428}
3e4093b6 3429\f
3e4093b6
RS
3430/* Return a tree for the difference of pointers OP0 and OP1.
3431 The resulting tree has type int. */
293c9fdd 3432
3e4093b6 3433static tree
db3927fb 3434pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3435{
3e4093b6 3436 tree restype = ptrdiff_type_node;
36c5e70a 3437 tree result, inttype;
400fbf9f 3438
36c5e70a
BE
3439 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3440 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3441 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3442 tree con0, con1, lit0, lit1;
3443 tree orig_op1 = op1;
400fbf9f 3444
36c5e70a
BE
3445 /* If the operands point into different address spaces, we need to
3446 explicitly convert them to pointers into the common address space
3447 before we can subtract the numerical address values. */
3448 if (as0 != as1)
3449 {
3450 addr_space_t as_common;
3451 tree common_type;
3452
3453 /* Determine the common superset address space. This is guaranteed
3454 to exist because the caller verified that comp_target_types
3455 returned non-zero. */
3456 if (!addr_space_superset (as0, as1, &as_common))
3457 gcc_unreachable ();
3458
3459 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3460 op0 = convert (common_type, op0);
3461 op1 = convert (common_type, op1);
3462 }
3463
3464 /* Determine integer type to perform computations in. This will usually
3465 be the same as the result type (ptrdiff_t), but may need to be a wider
3466 type if pointers for the address space are wider than ptrdiff_t. */
3467 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3468 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3469 else
3470 inttype = restype;
3471
3472
fcf73884 3473 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3474 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3475 "pointer of type %<void *%> used in subtraction");
3476 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3477 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3478 "pointer to a function used in subtraction");
400fbf9f 3479
3e4093b6
RS
3480 /* If the conversion to ptrdiff_type does anything like widening or
3481 converting a partial to an integral mode, we get a convert_expression
3482 that is in the way to do any simplifications.
3483 (fold-const.c doesn't know that the extra bits won't be needed.
3484 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3485 different mode in place.)
3486 So first try to find a common term here 'by hand'; we want to cover
3487 at least the cases that occur in legal static initializers. */
1043771b 3488 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3489 && (TYPE_PRECISION (TREE_TYPE (op0))
3490 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3491 con0 = TREE_OPERAND (op0, 0);
3492 else
3493 con0 = op0;
1043771b 3494 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3495 && (TYPE_PRECISION (TREE_TYPE (op1))
3496 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3497 con1 = TREE_OPERAND (op1, 0);
3498 else
3499 con1 = op1;
400fbf9f 3500
5de72424 3501 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3502 {
3503 lit0 = TREE_OPERAND (con0, 1);
3504 con0 = TREE_OPERAND (con0, 0);
3505 }
3506 else
3507 lit0 = integer_zero_node;
400fbf9f 3508
5de72424 3509 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3510 {
3e4093b6
RS
3511 lit1 = TREE_OPERAND (con1, 1);
3512 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3513 }
3514 else
3e4093b6
RS
3515 lit1 = integer_zero_node;
3516
3517 if (operand_equal_p (con0, con1, 0))
400fbf9f 3518 {
3e4093b6
RS
3519 op0 = lit0;
3520 op1 = lit1;
400fbf9f
JW
3521 }
3522
400fbf9f 3523
3e4093b6
RS
3524 /* First do the subtraction as integers;
3525 then drop through to build the divide operator.
3526 Do not do default conversions on the minus operator
3527 in case restype is a short type. */
400fbf9f 3528
db3927fb 3529 op0 = build_binary_op (loc,
36c5e70a
BE
3530 MINUS_EXPR, convert (inttype, op0),
3531 convert (inttype, op1), 0);
3e4093b6
RS
3532 /* This generates an error if op1 is pointer to incomplete type. */
3533 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3534 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3535
3e4093b6
RS
3536 /* This generates an error if op0 is pointer to incomplete type. */
3537 op1 = c_size_in_bytes (target_type);
400fbf9f 3538
3e4093b6 3539 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3540 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3541 op0, convert (inttype, op1));
3542
3543 /* Convert to final result type if necessary. */
3544 return convert (restype, result);
3e4093b6
RS
3545}
3546\f
267bac10
JM
3547/* Expand atomic compound assignments into an approriate sequence as
3548 specified by the C11 standard section 6.5.16.2.
3549 given
3550 _Atomic T1 E1
3551 T2 E2
3552 E1 op= E2
3553
3554 This sequence is used for all types for which these operations are
3555 supported.
3556
3557 In addition, built-in versions of the 'fe' prefixed routines may
3558 need to be invoked for floating point (real, complex or vector) when
3559 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3560
3561 T1 newval;
3562 T1 old;
3563 T1 *addr
3564 T2 val
3565 fenv_t fenv
3566
3567 addr = &E1;
3568 val = (E2);
3569 __atomic_load (addr, &old, SEQ_CST);
3570 feholdexcept (&fenv);
3571loop:
3572 newval = old op val;
3573 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3574 SEQ_CST))
3575 goto done;
3576 feclearexcept (FE_ALL_EXCEPT);
3577 goto loop:
3578done:
3579 feupdateenv (&fenv);
3580
3581 Also note that the compiler is simply issuing the generic form of
3582 the atomic operations. This requires temp(s) and has their address
3583 taken. The atomic processing is smart enough to figure out when the
3584 size of an object can utilize a lock-free version, and convert the
3585 built-in call to the appropriate lock-free routine. The optimizers
3586 will then dispose of any temps that are no longer required, and
3587 lock-free implementations are utilized as long as there is target
3588 support for the required size.
3589
3590 If the operator is NOP_EXPR, then this is a simple assignment, and
3591 an __atomic_store is issued to perform the assignment rather than
3592 the above loop.
3593
3594*/
3595
3596/* Build an atomic assignment at LOC, expanding into the proper
3597 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3598 the result of the operation, unless RETURN_OLD_P in which case
3599 return the old value of LHS (this is only for postincrement and
3600 postdecrement). */
3601static tree
3602build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3603 tree rhs, bool return_old_p)
3604{
3605 tree fndecl, func_call;
3606 vec<tree, va_gc> *params;
3607 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3608 tree old, old_addr;
3609 tree compound_stmt;
3610 tree stmt, goto_stmt;
3611 tree loop_label, loop_decl, done_label, done_decl;
3612
3613 tree lhs_type = TREE_TYPE (lhs);
3614 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3615 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3616 tree rhs_type = TREE_TYPE (rhs);
3617
3618 gcc_assert (TYPE_ATOMIC (lhs_type));
3619
3620 if (return_old_p)
3621 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3622
3623 /* Allocate enough vector items for a compare_exchange. */
3624 vec_alloc (params, 6);
3625
3626 /* Create a compound statement to hold the sequence of statements
3627 with a loop. */
3628 compound_stmt = c_begin_compound_stmt (false);
3629
3630 /* Fold the RHS if it hasn't already been folded. */
3631 if (modifycode != NOP_EXPR)
3632 rhs = c_fully_fold (rhs, false, NULL);
3633
3634 /* Remove the qualifiers for the rest of the expressions and create
3635 the VAL temp variable to hold the RHS. */
3636 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3637 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3638 val = create_tmp_var (nonatomic_rhs_type, NULL);
3639 TREE_ADDRESSABLE (val) = 1;
3640 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3641 SET_EXPR_LOCATION (rhs, loc);
3642 add_stmt (rhs);
3643
3644 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3645 an atomic_store. */
3646 if (modifycode == NOP_EXPR)
3647 {
3648 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3649 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3650 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3651 params->quick_push (lhs_addr);
3652 params->quick_push (rhs);
3653 params->quick_push (seq_cst);
3654 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3655 add_stmt (func_call);
3656
3657 /* Finish the compound statement. */
3658 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3659
3660 /* VAL is the value which was stored, return a COMPOUND_STMT of
3661 the statement and that value. */
3662 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3663 }
3664
3665 /* Create the variables and labels required for the op= form. */
3666 old = create_tmp_var (nonatomic_lhs_type, NULL);
3667 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
3668 TREE_ADDRESSABLE (val) = 1;
3669
3670 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3671 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3672 TREE_ADDRESSABLE (newval) = 1;
3673
3674 loop_decl = create_artificial_label (loc);
3675 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3676
3677 done_decl = create_artificial_label (loc);
3678 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3679
3680 /* __atomic_load (addr, &old, SEQ_CST). */
3681 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3682 params->quick_push (lhs_addr);
3683 params->quick_push (old_addr);
3684 params->quick_push (seq_cst);
3685 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3686 add_stmt (func_call);
3687 params->truncate (0);
3688
3689 /* Create the expressions for floating-point environment
3690 manipulation, if required. */
3691 bool need_fenv = (flag_trapping_math
3692 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3693 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3694 if (need_fenv)
3695 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3696
3697 if (hold_call)
3698 add_stmt (hold_call);
3699
3700 /* loop: */
3701 add_stmt (loop_label);
3702
3703 /* newval = old + val; */
3704 rhs = build_binary_op (loc, modifycode, old, val, 1);
3705 rhs = convert_for_assignment (loc, nonatomic_lhs_type, rhs, NULL_TREE,
3706 ic_assign, false, NULL_TREE,
3707 NULL_TREE, 0);
3708 if (rhs != error_mark_node)
3709 {
3710 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3711 SET_EXPR_LOCATION (rhs, loc);
3712 add_stmt (rhs);
3713 }
3714
3715 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3716 goto done; */
3717 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3718 params->quick_push (lhs_addr);
3719 params->quick_push (old_addr);
3720 params->quick_push (newval_addr);
3721 params->quick_push (integer_zero_node);
3722 params->quick_push (seq_cst);
3723 params->quick_push (seq_cst);
3724 func_call = build_function_call_vec (loc, fndecl, params, NULL);
3725
3726 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3727 SET_EXPR_LOCATION (goto_stmt, loc);
3728
3729 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3730 SET_EXPR_LOCATION (stmt, loc);
3731 add_stmt (stmt);
3732
3733 if (clear_call)
3734 add_stmt (clear_call);
3735
3736 /* goto loop; */
3737 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3738 SET_EXPR_LOCATION (goto_stmt, loc);
3739 add_stmt (goto_stmt);
3740
3741 /* done: */
3742 add_stmt (done_label);
3743
3744 if (update_call)
3745 add_stmt (update_call);
3746
3747 /* Finish the compound statement. */
3748 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3749
3750 /* NEWVAL is the value that was successfully stored, return a
3751 COMPOUND_EXPR of the statement and the appropriate value. */
3752 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3753 return_old_p ? old : newval);
3754}
3755
3e4093b6
RS
3756/* Construct and perhaps optimize a tree representation
3757 for a unary operation. CODE, a tree_code, specifies the operation
3758 and XARG is the operand.
3759 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3760 the default promotions (such as from short to int).
3761 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3762 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3763 arrays to pointers in C99.
3764
3765 LOCATION is the location of the operator. */
400fbf9f 3766
3e4093b6 3767tree
c9f9eb5d
AH
3768build_unary_op (location_t location,
3769 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3770{
3771 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3772 tree arg = xarg;
3773 tree argtype = 0;
808d6eaa 3774 enum tree_code typecode;
3e4093b6 3775 tree val;
c9f9eb5d 3776 tree ret = error_mark_node;
8ce94e44 3777 tree eptype = NULL_TREE;
3e4093b6 3778 int noconvert = flag;
4de67c26 3779 const char *invalid_op_diag;
928c19bb
JM
3780 bool int_operands;
3781
3782 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3783 if (int_operands)
3784 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3785
808d6eaa
JM
3786 if (code != ADDR_EXPR)
3787 arg = require_complete_type (arg);
3788
3789 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3790 if (typecode == ERROR_MARK)
3791 return error_mark_node;
3792 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3793 typecode = INTEGER_TYPE;
6c36d76b 3794
4de67c26
JM
3795 if ((invalid_op_diag
3796 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3797 {
c9f9eb5d 3798 error_at (location, invalid_op_diag);
4de67c26
JM
3799 return error_mark_node;
3800 }
3801
8ce94e44
JM
3802 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3803 {
3804 eptype = TREE_TYPE (arg);
3805 arg = TREE_OPERAND (arg, 0);
3806 }
3807
3e4093b6
RS
3808 switch (code)
3809 {
3810 case CONVERT_EXPR:
3811 /* This is used for unary plus, because a CONVERT_EXPR
3812 is enough to prevent anybody from looking inside for
3813 associativity, but won't generate any code. */
3814 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3815 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3816 || typecode == VECTOR_TYPE))
400fbf9f 3817 {
c9f9eb5d 3818 error_at (location, "wrong type argument to unary plus");
3e4093b6 3819 return error_mark_node;
400fbf9f 3820 }
3e4093b6
RS
3821 else if (!noconvert)
3822 arg = default_conversion (arg);
db3927fb 3823 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3824 break;
3825
3e4093b6
RS
3826 case NEGATE_EXPR:
3827 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3828 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3829 || typecode == VECTOR_TYPE))
3830 {
c9f9eb5d 3831 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3832 return error_mark_node;
3833 }
3834 else if (!noconvert)
3835 arg = default_conversion (arg);
400fbf9f
JW
3836 break;
3837
3e4093b6 3838 case BIT_NOT_EXPR:
462643f0
AP
3839 /* ~ works on integer types and non float vectors. */
3840 if (typecode == INTEGER_TYPE
3841 || (typecode == VECTOR_TYPE
3842 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3843 {
3e4093b6
RS
3844 if (!noconvert)
3845 arg = default_conversion (arg);
03d5b1f5 3846 }
3e4093b6 3847 else if (typecode == COMPLEX_TYPE)
400fbf9f 3848 {
3e4093b6 3849 code = CONJ_EXPR;
c1771a20 3850 pedwarn (location, OPT_Wpedantic,
fcf73884 3851 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3852 if (!noconvert)
3853 arg = default_conversion (arg);
3854 }
3855 else
3856 {
c9f9eb5d 3857 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3858 return error_mark_node;
400fbf9f
JW
3859 }
3860 break;
3861
3e4093b6 3862 case ABS_EXPR:
11017cc7 3863 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3864 {
c9f9eb5d 3865 error_at (location, "wrong type argument to abs");
3e4093b6 3866 return error_mark_node;
400fbf9f 3867 }
3e4093b6
RS
3868 else if (!noconvert)
3869 arg = default_conversion (arg);
400fbf9f
JW
3870 break;
3871
3e4093b6
RS
3872 case CONJ_EXPR:
3873 /* Conjugating a real value is a no-op, but allow it anyway. */
3874 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3875 || typecode == COMPLEX_TYPE))
400fbf9f 3876 {
c9f9eb5d 3877 error_at (location, "wrong type argument to conjugation");
3e4093b6 3878 return error_mark_node;
400fbf9f 3879 }
3e4093b6
RS
3880 else if (!noconvert)
3881 arg = default_conversion (arg);
400fbf9f
JW
3882 break;
3883
3e4093b6 3884 case TRUTH_NOT_EXPR:
ab22c1fa 3885 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3886 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3887 && typecode != COMPLEX_TYPE)
400fbf9f 3888 {
c9f9eb5d
AH
3889 error_at (location,
3890 "wrong type argument to unary exclamation mark");
3e4093b6 3891 return error_mark_node;
400fbf9f 3892 }
a27d595d
JM
3893 if (int_operands)
3894 {
3895 arg = c_objc_common_truthvalue_conversion (location, xarg);
3896 arg = remove_c_maybe_const_expr (arg);
3897 }
3898 else
3899 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3900 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3901 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3902 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3903 location = EXPR_LOCATION (ret);
c9f9eb5d 3904 goto return_build_unary_op;
3e4093b6 3905
3e4093b6 3906 case REALPART_EXPR:
3e4093b6 3907 case IMAGPART_EXPR:
fb52b50a
NF
3908 ret = build_real_imag_expr (location, code, arg);
3909 if (ret == error_mark_node)
3910 return error_mark_node;
8ce94e44
JM
3911 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3912 eptype = TREE_TYPE (eptype);
c9f9eb5d 3913 goto return_build_unary_op;
3e4093b6
RS
3914
3915 case PREINCREMENT_EXPR:
3916 case POSTINCREMENT_EXPR:
3917 case PREDECREMENT_EXPR:
3918 case POSTDECREMENT_EXPR:
3e4093b6 3919
928c19bb
JM
3920 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3921 {
3922 tree inner = build_unary_op (location, code,
3923 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3924 if (inner == error_mark_node)
3925 return error_mark_node;
3926 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3927 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3928 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3929 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3930 goto return_build_unary_op;
3931 }
3932
925e8657
NP
3933 /* Complain about anything that is not a true lvalue. In
3934 Objective-C, skip this check for property_refs. */
f90e8e2e 3935 if (!objc_is_property_ref (arg)
7bd11157
TT
3936 && !lvalue_or_else (location,
3937 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3938 || code == POSTINCREMENT_EXPR)
3939 ? lv_increment
3940 : lv_decrement)))
928c19bb
JM
3941 return error_mark_node;
3942
09639a83
ILT
3943 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3944 {
3945 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3946 warning_at (location, OPT_Wc___compat,
3947 "increment of enumeration value is invalid in C++");
3948 else
3949 warning_at (location, OPT_Wc___compat,
3950 "decrement of enumeration value is invalid in C++");
3951 }
3952
928c19bb
JM
3953 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3954 arg = c_fully_fold (arg, false, NULL);
3955
267bac10
JM
3956 bool atomic_op;
3957 atomic_op = really_atomic_lvalue (arg);
3958
3e4093b6
RS
3959 /* Increment or decrement the real part of the value,
3960 and don't change the imaginary part. */
3961 if (typecode == COMPLEX_TYPE)
400fbf9f 3962 {
3e4093b6
RS
3963 tree real, imag;
3964
c1771a20 3965 pedwarn (location, OPT_Wpedantic,
509c9d60 3966 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3967
267bac10
JM
3968 if (!atomic_op)
3969 {
3970 arg = stabilize_reference (arg);
3971 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3972 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3973 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3974 if (real == error_mark_node || imag == error_mark_node)
3975 return error_mark_node;
3976 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3977 real, imag);
3978 goto return_build_unary_op;
3979 }
400fbf9f 3980 }
3e4093b6
RS
3981
3982 /* Report invalid types. */
3983
ab22c1fa 3984 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3985 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 3986 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 3987 {
3e4093b6 3988 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3989 error_at (location, "wrong type argument to increment");
c22cacf3 3990 else
c9f9eb5d 3991 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3992
3993 return error_mark_node;
400fbf9f 3994 }
400fbf9f 3995
3e4093b6
RS
3996 {
3997 tree inc;
400fbf9f 3998
3e4093b6
RS
3999 argtype = TREE_TYPE (arg);
4000
4001 /* Compute the increment. */
4002
4003 if (typecode == POINTER_TYPE)
4004 {
4005 /* If pointer target is an undefined struct,
4006 we just cannot know how to do the arithmetic. */
b70cef5d 4007 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4008 {
4009 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d
AH
4010 error_at (location,
4011 "increment of pointer to unknown structure");
3e4093b6 4012 else
c9f9eb5d
AH
4013 error_at (location,
4014 "decrement of pointer to unknown structure");
3e4093b6 4015 }
b70cef5d
JJ
4016 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4017 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4018 {
3e4093b6 4019 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4020 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4021 "wrong type argument to increment");
3e4093b6 4022 else
44d90fe1 4023 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4024 "wrong type argument to decrement");
3e4093b6
RS
4025 }
4026
b70cef5d 4027 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4028 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4029 }
b70cef5d 4030 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4031 {
4032 /* For signed fract types, we invert ++ to -- or
4033 -- to ++, and change inc from 1 to -1, because
4034 it is not possible to represent 1 in signed fract constants.
4035 For unsigned fract types, the result always overflows and
4036 we get an undefined (original) or the maximum value. */
4037 if (code == PREINCREMENT_EXPR)
4038 code = PREDECREMENT_EXPR;
4039 else if (code == PREDECREMENT_EXPR)
4040 code = PREINCREMENT_EXPR;
4041 else if (code == POSTINCREMENT_EXPR)
4042 code = POSTDECREMENT_EXPR;
4043 else /* code == POSTDECREMENT_EXPR */
4044 code = POSTINCREMENT_EXPR;
4045
4046 inc = integer_minus_one_node;
4047 inc = convert (argtype, inc);
4048 }
3e4093b6 4049 else
5be014d5 4050 {
241b71bb
TV
4051 inc = VECTOR_TYPE_P (argtype)
4052 ? build_one_cst (argtype)
4053 : integer_one_node;
5be014d5
AP
4054 inc = convert (argtype, inc);
4055 }
3e4093b6 4056
925e8657
NP
4057 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4058 need to ask Objective-C to build the increment or decrement
4059 expression for it. */
4060 if (objc_is_property_ref (arg))
f90e8e2e 4061 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4062 arg, inc);
4063
3e4093b6 4064 /* Report a read-only lvalue. */
f37acdf9 4065 if (TYPE_READONLY (argtype))
953ff289 4066 {
c02065fc 4067 readonly_error (location, arg,
953ff289
DN
4068 ((code == PREINCREMENT_EXPR
4069 || code == POSTINCREMENT_EXPR)
4070 ? lv_increment : lv_decrement));
4071 return error_mark_node;
4072 }
f37acdf9
JM
4073 else if (TREE_READONLY (arg))
4074 readonly_warning (arg,
4075 ((code == PREINCREMENT_EXPR
4076 || code == POSTINCREMENT_EXPR)
4077 ? lv_increment : lv_decrement));
3e4093b6 4078
267bac10
JM
4079 /* If the argument is atomic, use the special code sequences for
4080 atomic compound assignment. */
4081 if (atomic_op)
4082 {
4083 arg = stabilize_reference (arg);
4084 ret = build_atomic_assign (location, arg,
4085 ((code == PREINCREMENT_EXPR
4086 || code == POSTINCREMENT_EXPR)
4087 ? PLUS_EXPR
4088 : MINUS_EXPR),
4089 (FRACT_MODE_P (TYPE_MODE (argtype))
4090 ? inc
4091 : integer_one_node),
4092 (code == POSTINCREMENT_EXPR
4093 || code == POSTDECREMENT_EXPR));
4094 goto return_build_unary_op;
4095 }
4096
3e4093b6
RS
4097 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4098 val = boolean_increment (code, arg);
4099 else
53fb4de3 4100 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4101 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4102 if (TREE_CODE (val) != code)
6de9cd9a 4103 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4104 ret = val;
4105 goto return_build_unary_op;
3e4093b6
RS
4106 }
4107
4108 case ADDR_EXPR:
4109 /* Note that this operation never does default_conversion. */
4110
2b4b7036
JM
4111 /* The operand of unary '&' must be an lvalue (which excludes
4112 expressions of type void), or, in C99, the result of a [] or
4113 unary '*' operator. */
4114 if (VOID_TYPE_P (TREE_TYPE (arg))
4115 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4116 && (TREE_CODE (arg) != INDIRECT_REF
4117 || !flag_isoc99))
4118 pedwarn (location, 0, "taking address of expression of type %<void%>");
4119
3e4093b6
RS
4120 /* Let &* cancel out to simplify resulting code. */
4121 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4122 {
3e4093b6
RS
4123 /* Don't let this be an lvalue. */
4124 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4125 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4126 ret = TREE_OPERAND (arg, 0);
4127 goto return_build_unary_op;
400fbf9f 4128 }
1eb8759b 4129
7c672dfc 4130 /* For &x[y], return x+y */
3e4093b6 4131 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4132 {
f2a71bbc
JM
4133 tree op0 = TREE_OPERAND (arg, 0);
4134 if (!c_mark_addressable (op0))
3e4093b6 4135 return error_mark_node;
1eb8759b 4136 }
1eb8759b 4137
3e4093b6
RS
4138 /* Anything not already handled and not a true memory reference
4139 or a non-lvalue array is an error. */
4140 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4141 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4142 return error_mark_node;
b6a10c9f 4143
928c19bb
JM
4144 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4145 folding later. */
4146 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4147 {
4148 tree inner = build_unary_op (location, code,
4149 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4150 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4151 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4152 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4153 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4154 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4155 goto return_build_unary_op;
4156 }
4157
3e4093b6
RS
4158 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4159 argtype = TREE_TYPE (arg);
400fbf9f 4160
3e4093b6 4161 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4162 to which the address will point. This is only needed
f2c1da78 4163 for function types. */
6615c446 4164 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4165 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4166 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4167 {
4168 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4169 int quals = orig_quals;
4170
4171 if (TREE_READONLY (arg))
4172 quals |= TYPE_QUAL_CONST;
4173 if (TREE_THIS_VOLATILE (arg))
4174 quals |= TYPE_QUAL_VOLATILE;
4175
f2c1da78
JM
4176 argtype = c_build_qualified_type (argtype, quals);
4177 }
400fbf9f 4178
3e4093b6
RS
4179 if (!c_mark_addressable (arg))
4180 return error_mark_node;
400fbf9f 4181
abb54d14
JM
4182 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4183 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4184
5cc200fc 4185 argtype = build_pointer_type (argtype);
5e55f99d
RH
4186
4187 /* ??? Cope with user tricks that amount to offsetof. Delete this
4188 when we have proper support for integer constant expressions. */
4189 val = get_base_address (arg);
4190 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4191 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4192 {
cf9e9959 4193 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4194 goto return_build_unary_op;
3aa2ddb8 4195 }
5e55f99d 4196
5cc200fc 4197 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4198
c9f9eb5d
AH
4199 ret = val;
4200 goto return_build_unary_op;
400fbf9f 4201
3e4093b6 4202 default:
1344f9a3 4203 gcc_unreachable ();
3e4093b6 4204 }
400fbf9f 4205
3e4093b6
RS
4206 if (argtype == 0)
4207 argtype = TREE_TYPE (arg);
928c19bb
JM
4208 if (TREE_CODE (arg) == INTEGER_CST)
4209 ret = (require_constant_value
db3927fb
AH
4210 ? fold_build1_initializer_loc (location, code, argtype, arg)
4211 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4212 else
4213 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4214 return_build_unary_op:
4215 gcc_assert (ret != error_mark_node);
928c19bb
JM
4216 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4217 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4218 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4219 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4220 ret = note_integer_operands (ret);
8ce94e44
JM
4221 if (eptype)
4222 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4223 protected_set_expr_location (ret, location);
4224 return ret;
3e4093b6 4225}
400fbf9f 4226
3e4093b6
RS
4227/* Return nonzero if REF is an lvalue valid for this language.
4228 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4229 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4230
1e4ae551 4231bool
58f9752a 4232lvalue_p (const_tree ref)
3e4093b6 4233{
58f9752a 4234 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4235
3e4093b6
RS
4236 switch (code)
4237 {
4238 case REALPART_EXPR:
4239 case IMAGPART_EXPR:
4240 case COMPONENT_REF:
4241 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4242
928c19bb
JM
4243 case C_MAYBE_CONST_EXPR:
4244 return lvalue_p (TREE_OPERAND (ref, 1));
4245
3e4093b6
RS
4246 case COMPOUND_LITERAL_EXPR:
4247 case STRING_CST:
4248 return 1;
400fbf9f 4249
3e4093b6
RS
4250 case INDIRECT_REF:
4251 case ARRAY_REF:
36536d79 4252 case ARRAY_NOTATION_REF:
3e4093b6
RS
4253 case VAR_DECL:
4254 case PARM_DECL:
4255 case RESULT_DECL:
4256 case ERROR_MARK:
4257 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4258 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4259
3e4093b6 4260 case BIND_EXPR:
3e4093b6 4261 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4262
3e4093b6
RS
4263 default:
4264 return 0;
4265 }
4266}
400fbf9f 4267\f
f37acdf9
JM
4268/* Give a warning for storing in something that is read-only in GCC
4269 terms but not const in ISO C terms. */
4270
4271static void
4272readonly_warning (tree arg, enum lvalue_use use)
4273{
4274 switch (use)
4275 {
4276 case lv_assign:
4277 warning (0, "assignment of read-only location %qE", arg);
4278 break;
4279 case lv_increment:
4280 warning (0, "increment of read-only location %qE", arg);
4281 break;
4282 case lv_decrement:
4283 warning (0, "decrement of read-only location %qE", arg);
4284 break;
4285 default:
4286 gcc_unreachable ();
4287 }
4288 return;
4289}
4290
37dc0d8d
JM
4291
4292/* Return nonzero if REF is an lvalue valid for this language;
4293 otherwise, print an error message and return zero. USE says
7bd11157
TT
4294 how the lvalue is being used and so selects the error message.
4295 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4296
4297static int
7bd11157 4298lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4299{
4300 int win = lvalue_p (ref);
4301
4302 if (!win)
7bd11157 4303 lvalue_error (loc, use);
37dc0d8d
JM
4304
4305 return win;
4306}
3e4093b6
RS
4307\f
4308/* Mark EXP saying that we need to be able to take the
4309 address of it; it should not be allocated in a register.
4310 Returns true if successful. */
54c93c30 4311
3e4093b6
RS
4312bool
4313c_mark_addressable (tree exp)
400fbf9f 4314{
3e4093b6 4315 tree x = exp;
95602da1 4316
3e4093b6
RS
4317 while (1)
4318 switch (TREE_CODE (x))
4319 {
4320 case COMPONENT_REF:
4321 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4322 {
0039fa55
AN
4323 error
4324 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4325 return false;
4326 }
95602da1 4327
3e4093b6 4328 /* ... fall through ... */
95602da1 4329
3e4093b6
RS
4330 case ADDR_EXPR:
4331 case ARRAY_REF:
4332 case REALPART_EXPR:
4333 case IMAGPART_EXPR:
4334 x = TREE_OPERAND (x, 0);
4335 break;
95602da1 4336
3e4093b6
RS
4337 case COMPOUND_LITERAL_EXPR:
4338 case CONSTRUCTOR:
4339 TREE_ADDRESSABLE (x) = 1;
4340 return true;
95602da1 4341
3e4093b6
RS
4342 case VAR_DECL:
4343 case CONST_DECL:
4344 case PARM_DECL:
4345 case RESULT_DECL:
5baeaac0 4346 if (C_DECL_REGISTER (x)
3e4093b6
RS
4347 && DECL_NONLOCAL (x))
4348 {
e697b20f 4349 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4350 {
0039fa55
AN
4351 error
4352 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4353 return false;
4354 }
509c9d60 4355 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4356 }
5baeaac0 4357 else if (C_DECL_REGISTER (x))
3e4093b6 4358 {
e697b20f 4359 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4360 error ("address of global register variable %qD requested", x);
4361 else
4362 error ("address of register variable %qD requested", x);
4363 return false;
3e4093b6 4364 }
400fbf9f 4365
3e4093b6
RS
4366 /* drops in */
4367 case FUNCTION_DECL:
4368 TREE_ADDRESSABLE (x) = 1;
4369 /* drops out */
4370 default:
4371 return true;
4372 }
4373}
4374\f
2d2e923f
MLI
4375/* Convert EXPR to TYPE, warning about conversion problems with
4376 constants. SEMANTIC_TYPE is the type this conversion would use
4377 without excess precision. If SEMANTIC_TYPE is NULL, this function
4378 is equivalent to convert_and_check. This function is a wrapper that
4379 handles conversions that may be different than
4380 the usual ones because of excess precision. */
4381
4382static tree
4383ep_convert_and_check (tree type, tree expr, tree semantic_type)
4384{
4385 if (TREE_TYPE (expr) == type)
4386 return expr;
4387
4388 if (!semantic_type)
4389 return convert_and_check (type, expr);
4390
4391 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4392 && TREE_TYPE (expr) != semantic_type)
4393 {
4394 /* For integers, we need to check the real conversion, not
4395 the conversion to the excess precision type. */
4396 expr = convert_and_check (semantic_type, expr);
4397 }
4398 /* Result type is the excess precision type, which should be
4399 large enough, so do not check. */
4400 return convert (type, expr);
4401}
4402
928c19bb
JM
4403/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4404 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4405 if folded to an integer constant then the unselected half may
4406 contain arbitrary operations not normally permitted in constant
c2255bc4 4407 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4408
4409tree
744aa42f 4410build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4411 tree op1, tree op1_original_type, tree op2,
4412 tree op2_original_type)
400fbf9f 4413{
3e4093b6
RS
4414 tree type1;
4415 tree type2;
4416 enum tree_code code1;
4417 enum tree_code code2;
4418 tree result_type = NULL;
2d2e923f 4419 tree semantic_result_type = NULL;
3e4093b6 4420 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4421 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4422 bool ifexp_int_operands;
928c19bb 4423 tree ret;
400fbf9f 4424
4d84fe7c
JM
4425 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4426 if (op1_int_operands)
4427 op1 = remove_c_maybe_const_expr (op1);
4428 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4429 if (op2_int_operands)
4430 op2 = remove_c_maybe_const_expr (op2);
4431 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4432 if (ifexp_int_operands)
4433 ifexp = remove_c_maybe_const_expr (ifexp);
4434
3e4093b6
RS
4435 /* Promote both alternatives. */
4436
4437 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4438 op1 = default_conversion (op1);
4439 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4440 op2 = default_conversion (op2);
4441
4442 if (TREE_CODE (ifexp) == ERROR_MARK
4443 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4444 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4445 return error_mark_node;
400fbf9f 4446
3e4093b6
RS
4447 type1 = TREE_TYPE (op1);
4448 code1 = TREE_CODE (type1);
4449 type2 = TREE_TYPE (op2);
4450 code2 = TREE_CODE (type2);
4451
b1adf557
JM
4452 /* C90 does not permit non-lvalue arrays in conditional expressions.
4453 In C99 they will be pointers by now. */
4454 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4455 {
744aa42f 4456 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4457 return error_mark_node;
4458 }
4459
8ce94e44
JM
4460 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4461 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4462 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4463 || code1 == COMPLEX_TYPE)
4464 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4465 || code2 == COMPLEX_TYPE))
4466 {
2d2e923f 4467 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4468 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4469 {
4470 op1 = TREE_OPERAND (op1, 0);
4471 type1 = TREE_TYPE (op1);
4472 gcc_assert (TREE_CODE (type1) == code1);
4473 }
4474 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4475 {
4476 op2 = TREE_OPERAND (op2, 0);
4477 type2 = TREE_TYPE (op2);
4478 gcc_assert (TREE_CODE (type2) == code2);
4479 }
4480 }
4481
d130ae11
ILT
4482 if (warn_cxx_compat)
4483 {
4484 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4485 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4486
4487 if (TREE_CODE (t1) == ENUMERAL_TYPE
4488 && TREE_CODE (t2) == ENUMERAL_TYPE
4489 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4490 warning_at (colon_loc, OPT_Wc___compat,
4491 ("different enum types in conditional is "
4492 "invalid in C++: %qT vs %qT"),
4493 t1, t2);
4494 }
4495
3e4093b6
RS
4496 /* Quickly detect the usual case where op1 and op2 have the same type
4497 after promotion. */
4498 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4499 {
3e4093b6
RS
4500 if (type1 == type2)
4501 result_type = type1;
4502 else
4503 result_type = TYPE_MAIN_VARIANT (type1);
4504 }
4505 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4506 || code1 == COMPLEX_TYPE)
4507 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4508 || code2 == COMPLEX_TYPE))
3e4093b6 4509 {
ccf7f880 4510 result_type = c_common_type (type1, type2);
0a0b3574
MM
4511 do_warn_double_promotion (result_type, type1, type2,
4512 "implicit conversion from %qT to %qT to "
4513 "match other result of conditional",
4514 colon_loc);
400fbf9f 4515
3e4093b6
RS
4516 /* If -Wsign-compare, warn here if type1 and type2 have
4517 different signedness. We'll promote the signed to unsigned
4518 and later code won't know it used to be different.
4519 Do this check on the original types, so that explicit casts
4520 will be considered, but default promotions won't. */
7d882b83 4521 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4522 {
8df83eae
RK
4523 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4524 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4525
3e4093b6
RS
4526 if (unsigned_op1 ^ unsigned_op2)
4527 {
6ac01510
ILT
4528 bool ovf;
4529
3e4093b6
RS
4530 /* Do not warn if the result type is signed, since the
4531 signed type will only be chosen if it can represent
4532 all the values of the unsigned type. */
3f75a254 4533 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4534 /* OK */;
3e4093b6 4535 else
928c19bb
JM
4536 {
4537 bool op1_maybe_const = true;
4538 bool op2_maybe_const = true;
4539
4540 /* Do not warn if the signed quantity is an
4541 unsuffixed integer literal (or some static
4542 constant expression involving such literals) and
4543 it is non-negative. This warning requires the
4544 operands to be folded for best results, so do
4545 that folding in this case even without
4546 warn_sign_compare to avoid warning options
4547 possibly affecting code generation. */
f5178456
RS
4548 c_inhibit_evaluation_warnings
4549 += (ifexp == truthvalue_false_node);
928c19bb
JM
4550 op1 = c_fully_fold (op1, require_constant_value,
4551 &op1_maybe_const);
f5178456
RS
4552 c_inhibit_evaluation_warnings
4553 -= (ifexp == truthvalue_false_node);
4554
4555 c_inhibit_evaluation_warnings
4556 += (ifexp == truthvalue_true_node);
928c19bb
JM
4557 op2 = c_fully_fold (op2, require_constant_value,
4558 &op2_maybe_const);
f5178456
RS
4559 c_inhibit_evaluation_warnings
4560 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4561
4562 if (warn_sign_compare)
4563 {
4564 if ((unsigned_op2
4565 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4566 || (unsigned_op1
4567 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4568 /* OK */;
4569 else
744aa42f
ILT
4570 warning_at (colon_loc, OPT_Wsign_compare,
4571 ("signed and unsigned type in "
4572 "conditional expression"));
928c19bb
JM
4573 }
4574 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4575 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4576 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4577 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4578 }
3e4093b6
RS
4579 }
4580 }
4581 }
4582 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4583 {
fcf73884 4584 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4585 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4586 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4587 result_type = void_type_node;
4588 }
4589 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4590 {
36c5e70a
BE
4591 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4592 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4593 addr_space_t as_common;
4594
744aa42f 4595 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4596 result_type = common_pointer_type (type1, type2);
6aa3c60d 4597 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4598 result_type = type2;
6aa3c60d 4599 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4600 result_type = type1;
4601 else if (!addr_space_superset (as1, as2, &as_common))
4602 {
4603 error_at (colon_loc, "pointers to disjoint address spaces "
4604 "used in conditional expression");
4605 return error_mark_node;
4606 }
267bac10
JM
4607 else if (VOID_TYPE_P (TREE_TYPE (type1))
4608 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4609 {
fcf73884 4610 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4611 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4612 "ISO C forbids conditional expr between "
bda67431 4613 "%<void *%> and function pointer");
3e4093b6
RS
4614 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4615 TREE_TYPE (type2)));
34a80643 4616 }
267bac10
JM
4617 else if (VOID_TYPE_P (TREE_TYPE (type2))
4618 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4619 {
fcf73884 4620 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4621 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4622 "ISO C forbids conditional expr between "
bda67431 4623 "%<void *%> and function pointer");
3e4093b6
RS
4624 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4625 TREE_TYPE (type1)));
1c2a9b35 4626 }
b581b85b
NP
4627 /* Objective-C pointer comparisons are a bit more lenient. */
4628 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4629 result_type = objc_common_type (type1, type2);
34a80643 4630 else
ab87f8c8 4631 {
36c5e70a
BE
4632 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4633
b581b85b
NP
4634 pedwarn (colon_loc, 0,
4635 "pointer type mismatch in conditional expression");
36c5e70a
BE
4636 result_type = build_pointer_type
4637 (build_qualified_type (void_type_node, qual));
ab87f8c8 4638 }
3e4093b6
RS
4639 }
4640 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4641 {
6aa3c60d 4642 if (!null_pointer_constant_p (orig_op2))
744aa42f 4643 pedwarn (colon_loc, 0,
509c9d60 4644 "pointer/integer type mismatch in conditional expression");
3e4093b6 4645 else
ab87f8c8 4646 {
3e4093b6 4647 op2 = null_pointer_node;
ab87f8c8 4648 }
3e4093b6
RS
4649 result_type = type1;
4650 }
4651 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4652 {
6aa3c60d 4653 if (!null_pointer_constant_p (orig_op1))
744aa42f 4654 pedwarn (colon_loc, 0,
509c9d60 4655 "pointer/integer type mismatch in conditional expression");
3e4093b6 4656 else
ab87f8c8 4657 {
3e4093b6 4658 op1 = null_pointer_node;
ab87f8c8 4659 }
3e4093b6
RS
4660 result_type = type2;
4661 }
1c2a9b35 4662
3e4093b6
RS
4663 if (!result_type)
4664 {
4665 if (flag_cond_mismatch)
4666 result_type = void_type_node;
4667 else
400fbf9f 4668 {
c2255bc4 4669 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4670 return error_mark_node;
400fbf9f 4671 }
3e4093b6 4672 }
400fbf9f 4673
3e4093b6
RS
4674 /* Merge const and volatile flags of the incoming types. */
4675 result_type
4676 = build_type_variant (result_type,
afbd0665
AS
4677 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4678 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4679
2d2e923f
MLI
4680 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
4681 op2 = ep_convert_and_check (result_type, op2, semantic_result_type);
b6a10c9f 4682
928c19bb
JM
4683 if (ifexp_bcp && ifexp == truthvalue_true_node)
4684 {
4685 op2_int_operands = true;
4686 op1 = c_fully_fold (op1, require_constant_value, NULL);
4687 }
4688 if (ifexp_bcp && ifexp == truthvalue_false_node)
4689 {
4690 op1_int_operands = true;
4691 op2 = c_fully_fold (op2, require_constant_value, NULL);
4692 }
4d84fe7c 4693 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4694 && op1_int_operands
4695 && op2_int_operands);
4696 if (int_operands)
4697 {
4698 int_const = ((ifexp == truthvalue_true_node
4699 && TREE_CODE (orig_op1) == INTEGER_CST
4700 && !TREE_OVERFLOW (orig_op1))
4701 || (ifexp == truthvalue_false_node
4702 && TREE_CODE (orig_op2) == INTEGER_CST
4703 && !TREE_OVERFLOW (orig_op2)));
4704 }
4705 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4706 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4707 else
4708 {
01c7ccbb
JM
4709 if (int_operands)
4710 {
4711 op1 = remove_c_maybe_const_expr (op1);
4712 op2 = remove_c_maybe_const_expr (op2);
4713 }
928c19bb
JM
4714 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4715 if (int_operands)
4716 ret = note_integer_operands (ret);
4717 }
2d2e923f
MLI
4718 if (semantic_result_type)
4719 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4720
c2255bc4 4721 protected_set_expr_location (ret, colon_loc);
928c19bb 4722 return ret;
3e4093b6
RS
4723}
4724\f
487a92fe 4725/* Return a compound expression that performs two expressions and
c2255bc4
AH
4726 returns the value of the second of them.
4727
4728 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4729
3e4093b6 4730tree
c2255bc4 4731build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4732{
4d84fe7c 4733 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4734 tree eptype = NULL_TREE;
928c19bb
JM
4735 tree ret;
4736
939b37da
BI
4737 if (flag_enable_cilkplus
4738 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4739 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4740 {
4741 error_at (loc,
4742 "spawned function call cannot be part of a comma expression");
4743 return error_mark_node;
4744 }
4d84fe7c
JM
4745 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4746 if (expr1_int_operands)
4747 expr1 = remove_c_maybe_const_expr (expr1);
4748 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4749 if (expr2_int_operands)
4750 expr2 = remove_c_maybe_const_expr (expr2);
4751
8ce94e44
JM
4752 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4753 expr1 = TREE_OPERAND (expr1, 0);
4754 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4755 {
4756 eptype = TREE_TYPE (expr2);
4757 expr2 = TREE_OPERAND (expr2, 0);
4758 }
4759
3f75a254 4760 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4761 {
4762 /* The left-hand operand of a comma expression is like an expression
c5409249 4763 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4764 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4765 if (warn_unused_value)
47aecf47 4766 {
e14a6540 4767 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4768 && CONVERT_EXPR_P (expr1))
47aecf47 4769 ; /* (void) a, b */
e14a6540
JM
4770 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4771 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4772 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4773 ; /* (void) a, (void) b, c */
4774 else
b8698a0f 4775 warning_at (loc, OPT_Wunused_value,
c2255bc4 4776 "left-hand operand of comma expression has no effect");
47aecf47 4777 }
3e4093b6 4778 }
400fbf9f 4779
3e4093b6
RS
4780 /* With -Wunused, we should also warn if the left-hand operand does have
4781 side-effects, but computes a value which is not used. For example, in
4782 `foo() + bar(), baz()' the result of the `+' operator is not used,
4783 so we should issue a warning. */
4784 else if (warn_unused_value)
c2255bc4 4785 warn_if_unused_value (expr1, loc);
400fbf9f 4786
e63d6886
AP
4787 if (expr2 == error_mark_node)
4788 return error_mark_node;
4789
928c19bb
JM
4790 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4791
4792 if (flag_isoc99
4d84fe7c
JM
4793 && expr1_int_operands
4794 && expr2_int_operands)
928c19bb
JM
4795 ret = note_integer_operands (ret);
4796
8ce94e44
JM
4797 if (eptype)
4798 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4799
c2255bc4 4800 protected_set_expr_location (ret, loc);
928c19bb 4801 return ret;
3e4093b6 4802}
400fbf9f 4803
67165eb3
ILT
4804/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4805 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4806 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4807 of the cast. -Wcast-qual appeared on the command line. Named
4808 address space qualifiers are not handled here, because they result
4809 in different warnings. */
67165eb3
ILT
4810
4811static void
2ee3cb35 4812handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4813{
4814 tree in_type = type;
4815 tree in_otype = otype;
4816 int added = 0;
4817 int discarded = 0;
4818 bool is_const;
4819
4820 /* Check that the qualifiers on IN_TYPE are a superset of the
4821 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4822 nodes is uninteresting and we stop as soon as we hit a
4823 non-POINTER_TYPE node on either type. */
4824 do
4825 {
4826 in_otype = TREE_TYPE (in_otype);
4827 in_type = TREE_TYPE (in_type);
4828
4829 /* GNU C allows cv-qualified function types. 'const' means the
4830 function is very pure, 'volatile' means it can't return. We
4831 need to warn when such qualifiers are added, not when they're
4832 taken away. */
4833 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4834 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4835 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4836 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4837 else
36c5e70a
BE
4838 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4839 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4840 }
4841 while (TREE_CODE (in_type) == POINTER_TYPE
4842 && TREE_CODE (in_otype) == POINTER_TYPE);
4843
4844 if (added)
2ee3cb35
MLI
4845 warning_at (loc, OPT_Wcast_qual,
4846 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4847
4848 if (discarded)
4849 /* There are qualifiers present in IN_OTYPE that are not present
4850 in IN_TYPE. */
2ee3cb35
MLI
4851 warning_at (loc, OPT_Wcast_qual,
4852 "cast discards %q#v qualifier from pointer target type",
4853 discarded);
67165eb3
ILT
4854
4855 if (added || discarded)
4856 return;
4857
4858 /* A cast from **T to const **T is unsafe, because it can cause a
4859 const value to be changed with no additional warning. We only
4860 issue this warning if T is the same on both sides, and we only
4861 issue the warning if there are the same number of pointers on
4862 both sides, as otherwise the cast is clearly unsafe anyhow. A
4863 cast is unsafe when a qualifier is added at one level and const
4864 is not present at all outer levels.
4865
4866 To issue this warning, we check at each level whether the cast
4867 adds new qualifiers not already seen. We don't need to special
4868 case function types, as they won't have the same
4869 TYPE_MAIN_VARIANT. */
4870
4871 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4872 return;
4873 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4874 return;
4875
4876 in_type = type;
4877 in_otype = otype;
4878 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4879 do
4880 {
4881 in_type = TREE_TYPE (in_type);
4882 in_otype = TREE_TYPE (in_otype);
4883 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4884 && !is_const)
4885 {
2ee3cb35
MLI
4886 warning_at (loc, OPT_Wcast_qual,
4887 "to be safe all intermediate pointers in cast from "
4888 "%qT to %qT must be %<const%> qualified",
4889 otype, type);
67165eb3
ILT
4890 break;
4891 }
4892 if (is_const)
4893 is_const = TYPE_READONLY (in_type);
4894 }
4895 while (TREE_CODE (in_type) == POINTER_TYPE);
4896}
4897
b8698a0f 4898/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4899 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4900
3e4093b6 4901tree
c2255bc4 4902build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4903{
8ce94e44
JM
4904 tree value;
4905
4906 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4907 expr = TREE_OPERAND (expr, 0);
4908
4909 value = expr;
400fbf9f 4910
3e4093b6
RS
4911 if (type == error_mark_node || expr == error_mark_node)
4912 return error_mark_node;
400fbf9f 4913
3e4093b6
RS
4914 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4915 only in <protocol> qualifications. But when constructing cast expressions,
4916 the protocols do matter and must be kept around. */
700686fa
ZL
4917 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4918 return build1 (NOP_EXPR, type, expr);
4919
4920 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4921
3e4093b6
RS
4922 if (TREE_CODE (type) == ARRAY_TYPE)
4923 {
c2255bc4 4924 error_at (loc, "cast specifies array type");
3e4093b6
RS
4925 return error_mark_node;
4926 }
400fbf9f 4927
3e4093b6
RS
4928 if (TREE_CODE (type) == FUNCTION_TYPE)
4929 {
c2255bc4 4930 error_at (loc, "cast specifies function type");
3e4093b6
RS
4931 return error_mark_node;
4932 }
400fbf9f 4933
808d6eaa
JM
4934 if (!VOID_TYPE_P (type))
4935 {
4936 value = require_complete_type (value);
4937 if (value == error_mark_node)
4938 return error_mark_node;
4939 }
4940
3e4093b6
RS
4941 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4942 {
fcf73884
MLI
4943 if (TREE_CODE (type) == RECORD_TYPE
4944 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4945 pedwarn (loc, OPT_Wpedantic,
fcf73884 4946 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4947 }
4948 else if (TREE_CODE (type) == UNION_TYPE)
4949 {
4950 tree field;
400fbf9f 4951
910ad8de 4952 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4953 if (TREE_TYPE (field) != error_mark_node
4954 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4955 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4956 break;
4957
4958 if (field)
400fbf9f 4959 {
3e4093b6 4960 tree t;
e616f54d 4961 bool maybe_const = true;
3e4093b6 4962
c1771a20 4963 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4964 t = c_fully_fold (value, false, &maybe_const);
4965 t = build_constructor_single (type, field, t);
4966 if (!maybe_const)
4967 t = c_wrap_maybe_const (t, true);
4968 t = digest_init (loc, type, t,
bbbbb16a 4969 NULL_TREE, false, true, 0);
3e4093b6
RS
4970 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4971 return t;
400fbf9f 4972 }
c2255bc4 4973 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4974 return error_mark_node;
4975 }
4976 else
4977 {
4978 tree otype, ovalue;
400fbf9f 4979
3e4093b6 4980 if (type == void_type_node)
c2255bc4
AH
4981 {
4982 tree t = build1 (CONVERT_EXPR, type, value);
4983 SET_EXPR_LOCATION (t, loc);
4984 return t;
4985 }
400fbf9f 4986
3e4093b6 4987 otype = TREE_TYPE (value);
400fbf9f 4988
3e4093b6 4989 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
4990 if (warn_cast_qual
4991 && TREE_CODE (type) == POINTER_TYPE
4992 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 4993 handle_warn_cast_qual (loc, type, otype);
400fbf9f 4994
36c5e70a
BE
4995 /* Warn about conversions between pointers to disjoint
4996 address spaces. */
4997 if (TREE_CODE (type) == POINTER_TYPE
4998 && TREE_CODE (otype) == POINTER_TYPE
4999 && !null_pointer_constant_p (value))
5000 {
5001 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5002 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5003 addr_space_t as_common;
5004
5005 if (!addr_space_superset (as_to, as_from, &as_common))
5006 {
5007 if (ADDR_SPACE_GENERIC_P (as_from))
5008 warning_at (loc, 0, "cast to %s address space pointer "
5009 "from disjoint generic address space pointer",
5010 c_addr_space_name (as_to));
5011
5012 else if (ADDR_SPACE_GENERIC_P (as_to))
5013 warning_at (loc, 0, "cast to generic address space pointer "
5014 "from disjoint %s address space pointer",
5015 c_addr_space_name (as_from));
5016
5017 else
5018 warning_at (loc, 0, "cast to %s address space pointer "
5019 "from disjoint %s address space pointer",
5020 c_addr_space_name (as_to),
5021 c_addr_space_name (as_from));
5022 }
5023 }
5024
3e4093b6 5025 /* Warn about possible alignment problems. */
3176a0c2 5026 if (STRICT_ALIGNMENT
3e4093b6
RS
5027 && TREE_CODE (type) == POINTER_TYPE
5028 && TREE_CODE (otype) == POINTER_TYPE
5029 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5030 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5031 /* Don't warn about opaque types, where the actual alignment
5032 restriction is unknown. */
5033 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5034 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5035 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5036 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5037 warning_at (loc, OPT_Wcast_align,
5038 "cast increases required alignment of target type");
e9a25f70 5039
3176a0c2 5040 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5041 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5042 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5043 /* Unlike conversion of integers to pointers, where the
5044 warning is disabled for converting constants because
5045 of cases such as SIG_*, warn about converting constant
5046 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5047 sign extension, and a warning is appropriate. */
c2255bc4
AH
5048 warning_at (loc, OPT_Wpointer_to_int_cast,
5049 "cast from pointer to integer of different size");
400fbf9f 5050
3176a0c2 5051 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5052 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5053 warning_at (loc, OPT_Wbad_function_cast,
5054 "cast from function call of type %qT "
5055 "to non-matching type %qT", otype, type);
400fbf9f 5056
3176a0c2 5057 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5058 && TREE_CODE (otype) == INTEGER_TYPE
5059 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5060 /* Don't warn about converting any constant. */
5061 && !TREE_CONSTANT (value))
c2255bc4
AH
5062 warning_at (loc,
5063 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5064 "of different size");
400fbf9f 5065
79bedddc
SR
5066 if (warn_strict_aliasing <= 2)
5067 strict_aliasing_warning (otype, type, expr);
400fbf9f 5068
3897f229
JM
5069 /* If pedantic, warn for conversions between function and object
5070 pointer types, except for converting a null pointer constant
5071 to function pointer type. */
5072 if (pedantic
5073 && TREE_CODE (type) == POINTER_TYPE
5074 && TREE_CODE (otype) == POINTER_TYPE
5075 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5076 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5077 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5078 "conversion of function pointer to object pointer type");
3897f229
JM
5079
5080 if (pedantic
5081 && TREE_CODE (type) == POINTER_TYPE
5082 && TREE_CODE (otype) == POINTER_TYPE
5083 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5084 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5085 && !null_pointer_constant_p (value))
c1771a20 5086 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5087 "conversion of object pointer to function pointer type");
3897f229 5088
3e4093b6 5089 ovalue = value;
3e4093b6 5090 value = convert (type, value);
400fbf9f 5091
3e4093b6 5092 /* Ignore any integer overflow caused by the cast. */
928c19bb 5093 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5094 {
8bcd6380 5095 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5096 {
8bcd6380
RS
5097 if (!TREE_OVERFLOW (value))
5098 {
5099 /* Avoid clobbering a shared constant. */
5100 value = copy_node (value);
5101 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5102 }
6414bad6 5103 }
8bcd6380 5104 else if (TREE_OVERFLOW (value))
d8e1f97b
PB
5105 /* Reset VALUE's overflow flags, ensuring constant sharing. */
5106 value = build_int_cst_wide (TREE_TYPE (value),
5107 TREE_INT_CST_LOW (value),
5108 TREE_INT_CST_HIGH (value));
3e4093b6
RS
5109 }
5110 }
400fbf9f 5111
53cd18ec
JM
5112 /* Don't let a cast be an lvalue. */
5113 if (value == expr)
db3927fb 5114 value = non_lvalue_loc (loc, value);
e9a25f70 5115
928c19bb
JM
5116 /* Don't allow the results of casting to floating-point or complex
5117 types be confused with actual constants, or casts involving
5118 integer and pointer types other than direct integer-to-integer
5119 and integer-to-pointer be confused with integer constant
5120 expressions and null pointer constants. */
5121 if (TREE_CODE (value) == REAL_CST
5122 || TREE_CODE (value) == COMPLEX_CST
5123 || (TREE_CODE (value) == INTEGER_CST
5124 && !((TREE_CODE (expr) == INTEGER_CST
5125 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5126 || TREE_CODE (expr) == REAL_CST
5127 || TREE_CODE (expr) == COMPLEX_CST)))
5128 value = build1 (NOP_EXPR, type, value);
5129
c2255bc4
AH
5130 if (CAN_HAVE_LOCATION_P (value))
5131 SET_EXPR_LOCATION (value, loc);
3e4093b6 5132 return value;
400fbf9f
JW
5133}
5134
c2255bc4
AH
5135/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5136 location of the open paren of the cast, or the position of the cast
5137 expr. */
3e4093b6 5138tree
c2255bc4 5139c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5140{
f8893e47 5141 tree type;
928c19bb
JM
5142 tree type_expr = NULL_TREE;
5143 bool type_expr_const = true;
5144 tree ret;
3e4093b6 5145 int saved_wsp = warn_strict_prototypes;
c5c76735 5146
3e4093b6
RS
5147 /* This avoids warnings about unprototyped casts on
5148 integers. E.g. "#define SIG_DFL (void(*)())0". */
5149 if (TREE_CODE (expr) == INTEGER_CST)
5150 warn_strict_prototypes = 0;
928c19bb 5151 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5152 warn_strict_prototypes = saved_wsp;
c5c76735 5153
c2255bc4 5154 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5155 if (type_expr)
5156 {
9f33203d
JM
5157 bool inner_expr_const = true;
5158 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5159 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5160 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5161 && inner_expr_const);
c2255bc4 5162 SET_EXPR_LOCATION (ret, loc);
928c19bb 5163 }
24b97832
ILT
5164
5165 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5166 SET_EXPR_LOCATION (ret, loc);
5167
9e5b2115
PB
5168 /* C++ does not permits types to be defined in a cast, but it
5169 allows references to incomplete types. */
5170 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5171 warning_at (loc, OPT_Wc___compat,
5172 "defining a type in a cast is invalid in C++");
5173
928c19bb 5174 return ret;
400fbf9f 5175}
3e4093b6
RS
5176\f
5177/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5178 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5179 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5180 MODIFYCODE is the code for a binary operator that we use
5181 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5182 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5183 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5184 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5185
c2255bc4
AH
5186 LOCATION is the location of the MODIFYCODE operator.
5187 RHS_LOC is the location of the RHS. */
2f6e4e97 5188
3e4093b6 5189tree
32e8bb8e 5190build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5191 enum tree_code modifycode,
c2255bc4 5192 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5193{
3e4093b6
RS
5194 tree result;
5195 tree newrhs;
241f845a 5196 tree rhseval = NULL_TREE;
8ce94e44 5197 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5198 tree lhstype = TREE_TYPE (lhs);
5199 tree olhstype = lhstype;
928c19bb 5200 bool npc;
267bac10 5201 bool is_atomic_op;
e9a25f70 5202
3e4093b6
RS
5203 /* Types that aren't fully specified cannot be used in assignments. */
5204 lhs = require_complete_type (lhs);
e9a25f70 5205
3e4093b6
RS
5206 /* Avoid duplicate error messages from operands that had errors. */
5207 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5208 return error_mark_node;
400fbf9f 5209
4c2ecab0
JM
5210 /* Ensure an error for assigning a non-lvalue array to an array in
5211 C90. */
5212 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5213 {
5214 error_at (location, "assignment to expression with array type");
5215 return error_mark_node;
5216 }
5217
46a88c12 5218 /* For ObjC properties, defer this check. */
7bd11157 5219 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5220 return error_mark_node;
5221
267bac10
JM
5222 is_atomic_op = really_atomic_lvalue (lhs);
5223
8ce94e44
JM
5224 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5225 {
5226 rhs_semantic_type = TREE_TYPE (rhs);
5227 rhs = TREE_OPERAND (rhs, 0);
5228 }
5229
3e4093b6 5230 newrhs = rhs;
400fbf9f 5231
928c19bb
JM
5232 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5233 {
5234 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5235 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5236 rhs_origtype);
928c19bb
JM
5237 if (inner == error_mark_node)
5238 return error_mark_node;
5239 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5240 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5241 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5242 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5243 protected_set_expr_location (result, location);
5244 return result;
5245 }
5246
3e4093b6
RS
5247 /* If a binary op has been requested, combine the old LHS value with the RHS
5248 producing the value we should actually store into the LHS. */
5249
5250 if (modifycode != NOP_EXPR)
400fbf9f 5251 {
928c19bb 5252 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5253 lhs = stabilize_reference (lhs);
bbbbb16a 5254
267bac10
JM
5255 /* Construct the RHS for any non-atomic compound assignemnt. */
5256 if (!is_atomic_op)
5257 {
241f845a
JJ
5258 /* If in LHS op= RHS the RHS has side-effects, ensure they
5259 are preevaluated before the rest of the assignment expression's
5260 side-effects, because RHS could contain e.g. function calls
5261 that modify LHS. */
5262 if (TREE_SIDE_EFFECTS (rhs))
5263 {
5264 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5265 rhseval = newrhs;
5266 }
267bac10 5267 newrhs = build_binary_op (location,
241f845a 5268 modifycode, lhs, newrhs, 1);
267bac10
JM
5269
5270 /* The original type of the right hand side is no longer
5271 meaningful. */
5272 rhs_origtype = NULL_TREE;
5273 }
400fbf9f 5274 }
400fbf9f 5275
668ea4b1
IS
5276 if (c_dialect_objc ())
5277 {
46a88c12
NP
5278 /* Check if we are modifying an Objective-C property reference;
5279 if so, we need to generate setter calls. */
5280 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5281 if (result)
241f845a 5282 goto return_result;
46a88c12
NP
5283
5284 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5285 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5286 return error_mark_node;
5287 }
5288
9bf24266 5289 /* Give an error for storing in something that is 'const'. */
bbbd6700 5290
f37acdf9 5291 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5292 || ((TREE_CODE (lhstype) == RECORD_TYPE
5293 || TREE_CODE (lhstype) == UNION_TYPE)
5294 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5295 {
c02065fc 5296 readonly_error (location, lhs, lv_assign);
953ff289
DN
5297 return error_mark_node;
5298 }
f37acdf9
JM
5299 else if (TREE_READONLY (lhs))
5300 readonly_warning (lhs, lv_assign);
bbbd6700 5301
3e4093b6
RS
5302 /* If storing into a structure or union member,
5303 it has probably been given type `int'.
5304 Compute the type that would go with
5305 the actual amount of storage the member occupies. */
bbbd6700 5306
3e4093b6
RS
5307 if (TREE_CODE (lhs) == COMPONENT_REF
5308 && (TREE_CODE (lhstype) == INTEGER_TYPE
5309 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5310 || TREE_CODE (lhstype) == REAL_TYPE
5311 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5312 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5313
3e4093b6
RS
5314 /* If storing in a field that is in actuality a short or narrower than one,
5315 we must store in the field in its actual type. */
5316
5317 if (lhstype != TREE_TYPE (lhs))
5318 {
5319 lhs = copy_node (lhs);
5320 TREE_TYPE (lhs) = lhstype;
400fbf9f 5321 }
400fbf9f 5322
32e8bb8e
ILT
5323 /* Issue -Wc++-compat warnings about an assignment to an enum type
5324 when LHS does not have its original type. This happens for,
5325 e.g., an enum bitfield in a struct. */
5326 if (warn_cxx_compat
5327 && lhs_origtype != NULL_TREE
5328 && lhs_origtype != lhstype
5329 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5330 {
5331 tree checktype = (rhs_origtype != NULL_TREE
5332 ? rhs_origtype
5333 : TREE_TYPE (rhs));
5334 if (checktype != error_mark_node
267bac10
JM
5335 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5336 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5337 warning_at (location, OPT_Wc___compat,
5338 "enum conversion in assignment is invalid in C++");
5339 }
5340
267bac10
JM
5341 /* If the lhs is atomic, remove that qualifier. */
5342 if (is_atomic_op)
5343 {
5344 lhstype = build_qualified_type (lhstype,
5345 (TYPE_QUALS (lhstype)
5346 & ~TYPE_QUAL_ATOMIC));
5347 olhstype = build_qualified_type (olhstype,
5348 (TYPE_QUALS (lhstype)
5349 & ~TYPE_QUAL_ATOMIC));
5350 }
5351
8ce94e44
JM
5352 /* Convert new value to destination type. Fold it first, then
5353 restore any excess precision information, for the sake of
5354 conversion warnings. */
400fbf9f 5355
267bac10
JM
5356 if (!(is_atomic_op && modifycode != NOP_EXPR))
5357 {
5358 npc = null_pointer_constant_p (newrhs);
5359 newrhs = c_fully_fold (newrhs, false, NULL);
5360 if (rhs_semantic_type)
5361 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
5362 newrhs = convert_for_assignment (location, lhstype, newrhs, rhs_origtype,
5363 ic_assign, npc, NULL_TREE,
5364 NULL_TREE, 0);
5365 if (TREE_CODE (newrhs) == ERROR_MARK)
5366 return error_mark_node;
5367 }
400fbf9f 5368
6e955430
ZL
5369 /* Emit ObjC write barrier, if necessary. */
5370 if (c_dialect_objc () && flag_objc_gc)
5371 {
5372 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5373 if (result)
c9f9eb5d
AH
5374 {
5375 protected_set_expr_location (result, location);
241f845a 5376 goto return_result;
c9f9eb5d 5377 }
6e955430
ZL
5378 }
5379
ea4b7848 5380 /* Scan operands. */
400fbf9f 5381
267bac10
JM
5382 if (is_atomic_op)
5383 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5384 else
5385 {
5386 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5387 TREE_SIDE_EFFECTS (result) = 1;
5388 protected_set_expr_location (result, location);
5389 }
400fbf9f 5390
3e4093b6
RS
5391 /* If we got the LHS in a different type for storing in,
5392 convert the result back to the nominal type of LHS
5393 so that the value we return always has the same type
5394 as the LHS argument. */
e855c5ce 5395
3e4093b6 5396 if (olhstype == TREE_TYPE (result))
241f845a 5397 goto return_result;
c9f9eb5d 5398
744aa42f
ILT
5399 result = convert_for_assignment (location, olhstype, result, rhs_origtype,
5400 ic_assign, false, NULL_TREE, NULL_TREE, 0);
c9f9eb5d 5401 protected_set_expr_location (result, location);
241f845a
JJ
5402
5403return_result:
5404 if (rhseval)
5405 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5406 return result;
3e4093b6
RS
5407}
5408\f
478a1c5b
ILT
5409/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5410 This is used to implement -fplan9-extensions. */
5411
5412static bool
5413find_anonymous_field_with_type (tree struct_type, tree type)
5414{
5415 tree field;
5416 bool found;
5417
5418 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5419 || TREE_CODE (struct_type) == UNION_TYPE);
5420 found = false;
5421 for (field = TYPE_FIELDS (struct_type);
5422 field != NULL_TREE;
5423 field = TREE_CHAIN (field))
5424 {
267bac10
JM
5425 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5426 ? c_build_qualified_type (TREE_TYPE (field),
5427 TYPE_QUAL_ATOMIC)
5428 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5429 if (DECL_NAME (field) == NULL
267bac10 5430 && comptypes (type, fieldtype))
478a1c5b
ILT
5431 {
5432 if (found)
5433 return false;
5434 found = true;
5435 }
5436 else if (DECL_NAME (field) == NULL
5437 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5438 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5439 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5440 {
5441 if (found)
5442 return false;
5443 found = true;
5444 }
5445 }
5446 return found;
5447}
5448
5449/* RHS is an expression whose type is pointer to struct. If there is
5450 an anonymous field in RHS with type TYPE, then return a pointer to
5451 that field in RHS. This is used with -fplan9-extensions. This
5452 returns NULL if no conversion could be found. */
5453
5454static tree
5455convert_to_anonymous_field (location_t location, tree type, tree rhs)
5456{
5457 tree rhs_struct_type, lhs_main_type;
5458 tree field, found_field;
5459 bool found_sub_field;
5460 tree ret;
5461
5462 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5463 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5464 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5465 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5466
5467 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5468 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5469 ? c_build_qualified_type (TREE_TYPE (type),
5470 TYPE_QUAL_ATOMIC)
5471 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5472
5473 found_field = NULL_TREE;
5474 found_sub_field = false;
5475 for (field = TYPE_FIELDS (rhs_struct_type);
5476 field != NULL_TREE;
5477 field = TREE_CHAIN (field))
5478 {
5479 if (DECL_NAME (field) != NULL_TREE
5480 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5481 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5482 continue;
267bac10
JM
5483 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5484 ? c_build_qualified_type (TREE_TYPE (field),
5485 TYPE_QUAL_ATOMIC)
5486 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5487 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5488 {
5489 if (found_field != NULL_TREE)
5490 return NULL_TREE;
5491 found_field = field;
5492 }
5493 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5494 lhs_main_type))
5495 {
5496 if (found_field != NULL_TREE)
5497 return NULL_TREE;
5498 found_field = field;
5499 found_sub_field = true;
5500 }
5501 }
5502
5503 if (found_field == NULL_TREE)
5504 return NULL_TREE;
5505
5506 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5507 build_fold_indirect_ref (rhs), found_field,
5508 NULL_TREE);
5509 ret = build_fold_addr_expr_loc (location, ret);
5510
5511 if (found_sub_field)
5512 {
5513 ret = convert_to_anonymous_field (location, type, ret);
5514 gcc_assert (ret != NULL_TREE);
5515 }
5516
5517 return ret;
5518}
5519
bbbbb16a
ILT
5520/* Convert value RHS to type TYPE as preparation for an assignment to
5521 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5522 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5523 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5524 constant before any folding.
3e4093b6
RS
5525 The real work of conversion is done by `convert'.
5526 The purpose of this function is to generate error messages
5527 for assignments that are not allowed in C.
2ac2f164
JM
5528 ERRTYPE says whether it is argument passing, assignment,
5529 initialization or return.
2f6e4e97 5530
c2255bc4 5531 LOCATION is the location of the RHS.
2ac2f164 5532 FUNCTION is a tree for the function being called.
3e4093b6 5533 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5534
3e4093b6 5535static tree
744aa42f
ILT
5536convert_for_assignment (location_t location, tree type, tree rhs,
5537 tree origtype, enum impl_conv errtype,
5538 bool null_pointer_constant, tree fundecl,
5539 tree function, int parmnum)
3e4093b6
RS
5540{
5541 enum tree_code codel = TREE_CODE (type);
8ce94e44 5542 tree orig_rhs = rhs;
3e4093b6
RS
5543 tree rhstype;
5544 enum tree_code coder;
2ac2f164 5545 tree rname = NULL_TREE;
58393038 5546 bool objc_ok = false;
2ac2f164 5547
6b4ef5c1 5548 if (errtype == ic_argpass)
2ac2f164
JM
5549 {
5550 tree selector;
5551 /* Change pointer to function to the function itself for
5552 diagnostics. */
5553 if (TREE_CODE (function) == ADDR_EXPR
5554 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5555 function = TREE_OPERAND (function, 0);
5556
5557 /* Handle an ObjC selector specially for diagnostics. */
5558 selector = objc_message_selector ();
5559 rname = function;
5560 if (selector && parmnum > 2)
5561 {
5562 rname = selector;
5563 parmnum -= 2;
5564 }
5565 }
5566
5567 /* This macro is used to emit diagnostics to ensure that all format
5568 strings are complete sentences, visible to gettext and checked at
5569 compile time. */
c2255bc4 5570#define WARN_FOR_ASSIGNMENT(LOCATION, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5571 do { \
5572 switch (errtype) \
5573 { \
5574 case ic_argpass: \
5575 if (pedwarn (LOCATION, OPT, AR, parmnum, rname)) \
c2255bc4
AH
5576 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5577 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
1e053dfe
MLI
5578 "expected %qT but argument is of type %qT", \
5579 type, rhstype); \
5580 break; \
1e053dfe
MLI
5581 case ic_assign: \
5582 pedwarn (LOCATION, OPT, AS); \
5583 break; \
5584 case ic_init: \
6a8f4e12 5585 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5586 break; \
5587 case ic_return: \
c2255bc4 5588 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5589 break; \
5590 default: \
5591 gcc_unreachable (); \
5592 } \
2ac2f164 5593 } while (0)
cb3ca04e 5594
49706e39
MLI
5595 /* This macro is used to emit diagnostics to ensure that all format
5596 strings are complete sentences, visible to gettext and checked at
5597 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5598 extra parameter to enumerate qualifiers. */
5599
5600#define WARN_FOR_QUALIFIERS(LOCATION, OPT, AR, AS, IN, RE, QUALS) \
5601 do { \
5602 switch (errtype) \
5603 { \
5604 case ic_argpass: \
5605 if (pedwarn (LOCATION, OPT, AR, parmnum, rname, QUALS)) \
5606 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5607 ? DECL_SOURCE_LOCATION (fundecl) : LOCATION, \
5608 "expected %qT but argument is of type %qT", \
5609 type, rhstype); \
5610 break; \
5611 case ic_assign: \
5612 pedwarn (LOCATION, OPT, AS, QUALS); \
5613 break; \
5614 case ic_init: \
5615 pedwarn (LOCATION, OPT, IN, QUALS); \
5616 break; \
5617 case ic_return: \
5618 pedwarn (LOCATION, OPT, RE, QUALS); \
5619 break; \
5620 default: \
5621 gcc_unreachable (); \
5622 } \
5623 } while (0)
5624
8ce94e44
JM
5625 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5626 rhs = TREE_OPERAND (rhs, 0);
5627
3e4093b6
RS
5628 rhstype = TREE_TYPE (rhs);
5629 coder = TREE_CODE (rhstype);
5630
5631 if (coder == ERROR_MARK)
5632 return error_mark_node;
5633
58393038
ZL
5634 if (c_dialect_objc ())
5635 {
5636 int parmno;
5637
5638 switch (errtype)
5639 {
5640 case ic_return:
5641 parmno = 0;
5642 break;
5643
5644 case ic_assign:
5645 parmno = -1;
5646 break;
5647
5648 case ic_init:
5649 parmno = -2;
5650 break;
5651
5652 default:
5653 parmno = parmnum;
5654 break;
5655 }
5656
5657 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5658 }
5659
bbbbb16a
ILT
5660 if (warn_cxx_compat)
5661 {
5662 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5663 if (checktype != error_mark_node
5664 && TREE_CODE (type) == ENUMERAL_TYPE
5665 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5666 {
81f40b79
ILT
5667 WARN_FOR_ASSIGNMENT (input_location, OPT_Wc___compat,
5668 G_("enum conversion when passing argument "
5669 "%d of %qE is invalid in C++"),
5670 G_("enum conversion in assignment is "
5671 "invalid in C++"),
5672 G_("enum conversion in initialization is "
5673 "invalid in C++"),
5674 G_("enum conversion in return is "
5675 "invalid in C++"));
bbbbb16a
ILT
5676 }
5677 }
5678
3e4093b6 5679 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5680 return rhs;
3e4093b6
RS
5681
5682 if (coder == VOID_TYPE)
400fbf9f 5683 {
6dcc04b0
JM
5684 /* Except for passing an argument to an unprototyped function,
5685 this is a constraint violation. When passing an argument to
5686 an unprototyped function, it is compile-time undefined;
5687 making it a constraint in that case was rejected in
5688 DR#252. */
c2255bc4 5689 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5690 return error_mark_node;
400fbf9f 5691 }
808d6eaa
JM
5692 rhs = require_complete_type (rhs);
5693 if (rhs == error_mark_node)
5694 return error_mark_node;
cd192ccc
MS
5695 /* A non-reference type can convert to a reference. This handles
5696 va_start, va_copy and possibly port built-ins. */
5697 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5698 {
3e4093b6 5699 if (!lvalue_p (rhs))
400fbf9f 5700 {
c2255bc4 5701 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5702 return error_mark_node;
400fbf9f 5703 }
3e4093b6
RS
5704 if (!c_mark_addressable (rhs))
5705 return error_mark_node;
5706 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5707 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5708
cd192ccc
MS
5709 rhs = convert_for_assignment (location, build_pointer_type (TREE_TYPE (type)),
5710 rhs, origtype, errtype, null_pointer_constant,
5711 fundecl, function, parmnum);
5712 if (rhs == error_mark_node)
5713 return error_mark_node;
3e4093b6
RS
5714
5715 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5716 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5717 return rhs;
400fbf9f 5718 }
3e4093b6 5719 /* Some types can interconvert without explicit casts. */
3274deff 5720 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5721 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5722 return convert (type, rhs);
5723 /* Arithmetic types all interconvert, and enum is treated like int. */
5724 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5725 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5726 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5727 || codel == BOOLEAN_TYPE)
5728 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5729 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5730 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5731 || coder == BOOLEAN_TYPE))
928c19bb
JM
5732 {
5733 tree ret;
5734 bool save = in_late_binary_op;
dfe776dd 5735 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5736 in_late_binary_op = true;
8ce94e44 5737 ret = convert_and_check (type, orig_rhs);
dfe776dd 5738 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5739 in_late_binary_op = save;
5740 return ret;
5741 }
400fbf9f 5742
79077aea
JJ
5743 /* Aggregates in different TUs might need conversion. */
5744 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5745 && codel == coder
5746 && comptypes (type, rhstype))
5747 return convert_and_check (type, rhs);
5748
ebf0bf7f 5749 /* Conversion to a transparent union or record from its member types.
3e4093b6 5750 This applies only to function arguments. */
ebf0bf7f
JJ
5751 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5752 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5753 && errtype == ic_argpass)
400fbf9f 5754 {
0257e383 5755 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5756
910ad8de 5757 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5758 {
0257e383 5759 tree memb_type = TREE_TYPE (memb);
400fbf9f 5760
3e4093b6 5761 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5762 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5763 break;
e58cd767 5764
3e4093b6
RS
5765 if (TREE_CODE (memb_type) != POINTER_TYPE)
5766 continue;
2f6e4e97 5767
3e4093b6
RS
5768 if (coder == POINTER_TYPE)
5769 {
5770 tree ttl = TREE_TYPE (memb_type);
5771 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5772
3e4093b6
RS
5773 /* Any non-function converts to a [const][volatile] void *
5774 and vice versa; otherwise, targets must be the same.
5775 Meanwhile, the lhs target must have all the qualifiers of
5776 the rhs. */
267bac10
JM
5777 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5778 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5779 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5780 {
267bac10
JM
5781 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5782 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5783 /* If this type won't generate any warnings, use it. */
267bac10 5784 if (lquals == rquals
3e4093b6
RS
5785 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5786 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5787 ? ((lquals | rquals) == rquals)
5788 : ((lquals | rquals) == lquals)))
3e4093b6 5789 break;
400fbf9f 5790
3e4093b6 5791 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5792 if (!marginal_memb)
5793 marginal_memb = memb;
3e4093b6
RS
5794 }
5795 }
82bde854 5796
3e4093b6 5797 /* Can convert integer zero to any pointer type. */
928c19bb 5798 if (null_pointer_constant)
3e4093b6
RS
5799 {
5800 rhs = null_pointer_node;
5801 break;
5802 }
5803 }
400fbf9f 5804
0257e383 5805 if (memb || marginal_memb)
3e4093b6 5806 {
0257e383 5807 if (!memb)
3e4093b6
RS
5808 {
5809 /* We have only a marginally acceptable member type;
5810 it needs a warning. */
0257e383 5811 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5812 tree ttr = TREE_TYPE (rhstype);
714a0864 5813
3e4093b6
RS
5814 /* Const and volatile mean something different for function
5815 types, so the usual warnings are not appropriate. */
5816 if (TREE_CODE (ttr) == FUNCTION_TYPE
5817 && TREE_CODE (ttl) == FUNCTION_TYPE)
5818 {
5819 /* Because const and volatile on functions are
5820 restrictions that say the function will not do
5821 certain things, it is okay to use a const or volatile
5822 function where an ordinary one is wanted, but not
5823 vice-versa. */
36c5e70a
BE
5824 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5825 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 5826 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5827 G_("passing argument %d of %qE "
49706e39 5828 "makes %q#v qualified function "
2ac2f164 5829 "pointer from unqualified"),
49706e39 5830 G_("assignment makes %q#v qualified "
2ac2f164
JM
5831 "function pointer from "
5832 "unqualified"),
49706e39 5833 G_("initialization makes %q#v qualified "
2ac2f164
JM
5834 "function pointer from "
5835 "unqualified"),
49706e39
MLI
5836 G_("return makes %q#v qualified function "
5837 "pointer from unqualified"),
5838 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5839 }
36c5e70a
BE
5840 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5841 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
49706e39 5842 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 5843 G_("passing argument %d of %qE discards "
49706e39
MLI
5844 "%qv qualifier from pointer target type"),
5845 G_("assignment discards %qv qualifier "
2ac2f164 5846 "from pointer target type"),
49706e39 5847 G_("initialization discards %qv qualifier "
2ac2f164 5848 "from pointer target type"),
49706e39
MLI
5849 G_("return discards %qv qualifier from "
5850 "pointer target type"),
5851 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5852
5853 memb = marginal_memb;
3e4093b6 5854 }
400fbf9f 5855
fcf73884 5856 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5857 pedwarn (location, OPT_Wpedantic,
fcf73884 5858 "ISO C prohibits argument conversion to union type");
0e7c47fa 5859
db3927fb 5860 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5861 return build_constructor_single (type, memb, rhs);
3e4093b6 5862 }
0e7c47fa
RK
5863 }
5864
3e4093b6
RS
5865 /* Conversions among pointers */
5866 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5867 && (coder == codel))
400fbf9f 5868 {
3e4093b6
RS
5869 tree ttl = TREE_TYPE (type);
5870 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5871 tree mvl = ttl;
5872 tree mvr = ttr;
3e4093b6 5873 bool is_opaque_pointer;
264fa2db 5874 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5875 addr_space_t asl;
5876 addr_space_t asr;
400fbf9f 5877
46df2823 5878 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5879 mvl = (TYPE_ATOMIC (mvl)
5880 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5881 TYPE_QUAL_ATOMIC)
5882 : TYPE_MAIN_VARIANT (mvl));
46df2823 5883 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5884 mvr = (TYPE_ATOMIC (mvr)
5885 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5886 TYPE_QUAL_ATOMIC)
5887 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5888 /* Opaque pointers are treated like void pointers. */
f83c7f63 5889 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5890
478a1c5b
ILT
5891 /* The Plan 9 compiler permits a pointer to a struct to be
5892 automatically converted into a pointer to an anonymous field
5893 within the struct. */
5894 if (flag_plan9_extensions
5895 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5896 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5897 && mvl != mvr)
5898 {
5899 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5900 if (new_rhs != NULL_TREE)
5901 {
5902 rhs = new_rhs;
5903 rhstype = TREE_TYPE (rhs);
5904 coder = TREE_CODE (rhstype);
5905 ttr = TREE_TYPE (rhstype);
5906 mvr = TYPE_MAIN_VARIANT (ttr);
5907 }
5908 }
5909
b7e20b53 5910 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
5911 for the purpose of reducing the number of false positives, we
5912 tolerate the special case of
b7e20b53 5913
c22cacf3 5914 int *p = NULL;
b7e20b53 5915
c22cacf3 5916 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 5917 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
c2255bc4
AH
5918 warning_at (location, OPT_Wc___compat,
5919 "request for implicit conversion "
5920 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 5921
36c5e70a
BE
5922 /* See if the pointers point to incompatible address spaces. */
5923 asl = TYPE_ADDR_SPACE (ttl);
5924 asr = TYPE_ADDR_SPACE (ttr);
5925 if (!null_pointer_constant_p (rhs)
5926 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
5927 {
5928 switch (errtype)
5929 {
5930 case ic_argpass:
5931 error_at (location, "passing argument %d of %qE from pointer to "
5932 "non-enclosed address space", parmnum, rname);
5933 break;
5934 case ic_assign:
5935 error_at (location, "assignment from pointer to "
5936 "non-enclosed address space");
5937 break;
5938 case ic_init:
5939 error_at (location, "initialization from pointer to "
5940 "non-enclosed address space");
5941 break;
5942 case ic_return:
5943 error_at (location, "return from pointer to "
5944 "non-enclosed address space");
5945 break;
5946 default:
5947 gcc_unreachable ();
5948 }
5949 return error_mark_node;
5950 }
5951
7876a414
KG
5952 /* Check if the right-hand side has a format attribute but the
5953 left-hand side doesn't. */
90137d8f 5954 if (warn_suggest_attribute_format
104f8784 5955 && check_missing_format_attribute (type, rhstype))
c22cacf3 5956 {
104f8784
KG
5957 switch (errtype)
5958 {
5959 case ic_argpass:
90137d8f 5960 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5961 "argument %d of %qE might be "
5962 "a candidate for a format attribute",
5963 parmnum, rname);
104f8784
KG
5964 break;
5965 case ic_assign:
90137d8f 5966 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5967 "assignment left-hand side might be "
5968 "a candidate for a format attribute");
104f8784
KG
5969 break;
5970 case ic_init:
90137d8f 5971 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5972 "initialization left-hand side might be "
5973 "a candidate for a format attribute");
104f8784
KG
5974 break;
5975 case ic_return:
90137d8f 5976 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
5977 "return type might be "
5978 "a candidate for a format attribute");
104f8784
KG
5979 break;
5980 default:
5981 gcc_unreachable ();
5982 }
7876a414 5983 }
c22cacf3 5984
3e4093b6
RS
5985 /* Any non-function converts to a [const][volatile] void *
5986 and vice versa; otherwise, targets must be the same.
5987 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
5988 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5989 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5990 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 5991 || is_opaque_pointer
f8a93a2e
JJ
5992 || ((c_common_unsigned_type (mvl)
5993 == c_common_unsigned_type (mvr))
267bac10
JM
5994 && (c_common_signed_type (mvl)
5995 == c_common_signed_type (mvr))
5996 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
5997 {
5998 if (pedantic
5999 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6000 ||
6001 (VOID_TYPE_P (ttr)
928c19bb 6002 && !null_pointer_constant
3e4093b6 6003 && TREE_CODE (ttl) == FUNCTION_TYPE)))
c1771a20 6004 WARN_FOR_ASSIGNMENT (location, OPT_Wpedantic,
509c9d60 6005 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
6006 "%qE between function pointer "
6007 "and %<void *%>"),
4b794eaf 6008 G_("ISO C forbids assignment between "
2ac2f164 6009 "function pointer and %<void *%>"),
4b794eaf 6010 G_("ISO C forbids initialization between "
2ac2f164 6011 "function pointer and %<void *%>"),
4b794eaf 6012 G_("ISO C forbids return between function "
2ac2f164 6013 "pointer and %<void *%>"));
3e4093b6
RS
6014 /* Const and volatile mean something different for function types,
6015 so the usual warnings are not appropriate. */
6016 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6017 && TREE_CODE (ttl) != FUNCTION_TYPE)
6018 {
267bac10
JM
6019 /* Assignments between atomic and non-atomic objects are OK. */
6020 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6021 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6022 {
6c39e757
NP
6023 WARN_FOR_QUALIFIERS (location, 0,
6024 G_("passing argument %d of %qE discards "
6025 "%qv qualifier from pointer target type"),
6026 G_("assignment discards %qv qualifier "
6027 "from pointer target type"),
6028 G_("initialization discards %qv qualifier "
6029 "from pointer target type"),
6030 G_("return discards %qv qualifier from "
6031 "pointer target type"),
6032 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6033 }
3e4093b6
RS
6034 /* If this is not a case of ignoring a mismatch in signedness,
6035 no warning. */
6036 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6037 || target_cmp)
3e4093b6
RS
6038 ;
6039 /* If there is a mismatch, do warn. */
f2fd3821 6040 else if (warn_pointer_sign)
c2255bc4 6041 WARN_FOR_ASSIGNMENT (location, OPT_Wpointer_sign,
509c9d60 6042 G_("pointer targets in passing argument "
2ac2f164 6043 "%d of %qE differ in signedness"),
4b794eaf 6044 G_("pointer targets in assignment "
2ac2f164 6045 "differ in signedness"),
4b794eaf 6046 G_("pointer targets in initialization "
2ac2f164 6047 "differ in signedness"),
4b794eaf 6048 G_("pointer targets in return differ "
2ac2f164 6049 "in signedness"));
3e4093b6
RS
6050 }
6051 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6052 && TREE_CODE (ttr) == FUNCTION_TYPE)
6053 {
6054 /* Because const and volatile on functions are restrictions
6055 that say the function will not do certain things,
6056 it is okay to use a const or volatile function
6057 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6058 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6059 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
49706e39 6060 WARN_FOR_QUALIFIERS (location, 0,
509c9d60 6061 G_("passing argument %d of %qE makes "
49706e39 6062 "%q#v qualified function pointer "
2ac2f164 6063 "from unqualified"),
49706e39 6064 G_("assignment makes %q#v qualified function "
2ac2f164 6065 "pointer from unqualified"),
49706e39 6066 G_("initialization makes %q#v qualified "
2ac2f164 6067 "function pointer from unqualified"),
49706e39
MLI
6068 G_("return makes %q#v qualified function "
6069 "pointer from unqualified"),
6070 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6071 }
6072 }
6073 else
58393038
ZL
6074 /* Avoid warning about the volatile ObjC EH puts on decls. */
6075 if (!objc_ok)
c2255bc4 6076 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6077 G_("passing argument %d of %qE from "
58393038 6078 "incompatible pointer type"),
4b794eaf
JJ
6079 G_("assignment from incompatible pointer type"),
6080 G_("initialization from incompatible "
58393038 6081 "pointer type"),
4b794eaf 6082 G_("return from incompatible pointer type"));
58393038 6083
3e4093b6
RS
6084 return convert (type, rhs);
6085 }
b494fd98
EB
6086 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6087 {
6dcc04b0
JM
6088 /* ??? This should not be an error when inlining calls to
6089 unprototyped functions. */
c2255bc4 6090 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6091 return error_mark_node;
6092 }
3e4093b6 6093 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6094 {
3e4093b6
RS
6095 /* An explicit constant 0 can convert to a pointer,
6096 or one that results from arithmetic, even including
6097 a cast to integer type. */
928c19bb 6098 if (!null_pointer_constant)
c2255bc4 6099 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6100 G_("passing argument %d of %qE makes "
2ac2f164 6101 "pointer from integer without a cast"),
4b794eaf 6102 G_("assignment makes pointer from integer "
2ac2f164 6103 "without a cast"),
4b794eaf 6104 G_("initialization makes pointer from "
2ac2f164 6105 "integer without a cast"),
4b794eaf 6106 G_("return makes pointer from integer "
2ac2f164 6107 "without a cast"));
b3006337
EB
6108
6109 return convert (type, rhs);
400fbf9f 6110 }
3e4093b6 6111 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6112 {
c2255bc4 6113 WARN_FOR_ASSIGNMENT (location, 0,
509c9d60 6114 G_("passing argument %d of %qE makes integer "
2ac2f164 6115 "from pointer without a cast"),
4b794eaf 6116 G_("assignment makes integer from pointer "
2ac2f164 6117 "without a cast"),
4b794eaf 6118 G_("initialization makes integer from pointer "
2ac2f164 6119 "without a cast"),
4b794eaf 6120 G_("return makes integer from pointer "
2ac2f164 6121 "without a cast"));
3e4093b6
RS
6122 return convert (type, rhs);
6123 }
6124 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6125 {
6126 tree ret;
6127 bool save = in_late_binary_op;
6128 in_late_binary_op = true;
6129 ret = convert (type, rhs);
6130 in_late_binary_op = save;
6131 return ret;
6132 }
400fbf9f 6133
2ac2f164 6134 switch (errtype)
3e4093b6 6135 {
2ac2f164 6136 case ic_argpass:
c2255bc4 6137 error_at (location, "incompatible type for argument %d of %qE", parmnum, rname);
a7da8b42
MLI
6138 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6139 ? DECL_SOURCE_LOCATION (fundecl) : input_location,
6140 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6141 break;
6142 case ic_assign:
c2255bc4
AH
6143 error_at (location, "incompatible types when assigning to type %qT from "
6144 "type %qT", type, rhstype);
2ac2f164
JM
6145 break;
6146 case ic_init:
c2255bc4
AH
6147 error_at (location,
6148 "incompatible types when initializing type %qT using type %qT",
6149 type, rhstype);
2ac2f164
JM
6150 break;
6151 case ic_return:
c2255bc4
AH
6152 error_at (location,
6153 "incompatible types when returning type %qT but %qT was "
6154 "expected", rhstype, type);
2ac2f164
JM
6155 break;
6156 default:
6157 gcc_unreachable ();
400fbf9f 6158 }
53b01f59 6159
3e4093b6
RS
6160 return error_mark_node;
6161}
3e4093b6
RS
6162\f
6163/* If VALUE is a compound expr all of whose expressions are constant, then
6164 return its value. Otherwise, return error_mark_node.
15b732b2 6165
3e4093b6
RS
6166 This is for handling COMPOUND_EXPRs as initializer elements
6167 which is allowed with a warning when -pedantic is specified. */
15b732b2 6168
3e4093b6
RS
6169static tree
6170valid_compound_expr_initializer (tree value, tree endtype)
6171{
6172 if (TREE_CODE (value) == COMPOUND_EXPR)
6173 {
6174 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6175 == error_mark_node)
6176 return error_mark_node;
6177 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6178 endtype);
6179 }
116df786 6180 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6181 return error_mark_node;
6182 else
6183 return value;
15b732b2 6184}
400fbf9f 6185\f
3e4093b6
RS
6186/* Perform appropriate conversions on the initial value of a variable,
6187 store it in the declaration DECL,
6188 and print any error messages that are appropriate.
bbbbb16a 6189 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6190 If the init is invalid, store an ERROR_MARK.
6191
6192 INIT_LOC is the location of the initial value. */
400fbf9f 6193
3e4093b6 6194void
c2255bc4 6195store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6196{
3e4093b6 6197 tree value, type;
928c19bb 6198 bool npc = false;
400fbf9f 6199
3e4093b6 6200 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6201
3e4093b6
RS
6202 type = TREE_TYPE (decl);
6203 if (TREE_CODE (type) == ERROR_MARK)
6204 return;
400fbf9f 6205
3e4093b6 6206 /* Digest the specified initializer into an expression. */
400fbf9f 6207
928c19bb
JM
6208 if (init)
6209 npc = null_pointer_constant_p (init);
c2255bc4
AH
6210 value = digest_init (init_loc, type, init, origtype, npc,
6211 true, TREE_STATIC (decl));
400fbf9f 6212
3e4093b6 6213 /* Store the expression if valid; else report error. */
400fbf9f 6214
8400e75e 6215 if (!in_system_header_at (input_location)
3f75a254 6216 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6217 warning (OPT_Wtraditional, "traditional C rejects automatic "
6218 "aggregate initialization");
2f6e4e97 6219
3e4093b6 6220 DECL_INITIAL (decl) = value;
400fbf9f 6221
3e4093b6
RS
6222 /* ANSI wants warnings about out-of-range constant initializers. */
6223 STRIP_TYPE_NOPS (value);
b8698a0f 6224 if (TREE_STATIC (decl))
c2658540 6225 constant_expression_warning (value);
400fbf9f 6226
3e4093b6
RS
6227 /* Check if we need to set array size from compound literal size. */
6228 if (TREE_CODE (type) == ARRAY_TYPE
6229 && TYPE_DOMAIN (type) == 0
6230 && value != error_mark_node)
400fbf9f 6231 {
3e4093b6
RS
6232 tree inside_init = init;
6233
ed248cf7 6234 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6235 inside_init = fold (inside_init);
6236
6237 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6238 {
8d9f82d5 6239 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6240
8d9f82d5 6241 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6242 {
6243 /* For int foo[] = (int [3]){1}; we need to set array size
6244 now since later on array initializer will be just the
6245 brace enclosed list of the compound literal. */
e30ecc5d 6246 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6247 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6248 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6249 layout_type (type);
8d9f82d5 6250 layout_decl (cldecl, 0);
e30ecc5d
JJ
6251 TREE_TYPE (decl)
6252 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6253 }
6254 }
400fbf9f 6255 }
3e4093b6
RS
6256}
6257\f
6258/* Methods for storing and printing names for error messages. */
400fbf9f 6259
3e4093b6
RS
6260/* Implement a spelling stack that allows components of a name to be pushed
6261 and popped. Each element on the stack is this structure. */
400fbf9f 6262
3e4093b6
RS
6263struct spelling
6264{
6265 int kind;
6266 union
400fbf9f 6267 {
a0f0ab9f 6268 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6269 const char *s;
6270 } u;
6271};
2f6e4e97 6272
3e4093b6
RS
6273#define SPELLING_STRING 1
6274#define SPELLING_MEMBER 2
6275#define SPELLING_BOUNDS 3
400fbf9f 6276
3e4093b6
RS
6277static struct spelling *spelling; /* Next stack element (unused). */
6278static struct spelling *spelling_base; /* Spelling stack base. */
6279static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6280
3e4093b6
RS
6281/* Macros to save and restore the spelling stack around push_... functions.
6282 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6283
3e4093b6
RS
6284#define SPELLING_DEPTH() (spelling - spelling_base)
6285#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6286
3e4093b6
RS
6287/* Push an element on the spelling stack with type KIND and assign VALUE
6288 to MEMBER. */
400fbf9f 6289
3e4093b6
RS
6290#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6291{ \
6292 int depth = SPELLING_DEPTH (); \
6293 \
6294 if (depth >= spelling_size) \
6295 { \
6296 spelling_size += 10; \
cca8ead2
BI
6297 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6298 spelling_size); \
3e4093b6
RS
6299 RESTORE_SPELLING_DEPTH (depth); \
6300 } \
6301 \
6302 spelling->kind = (KIND); \
6303 spelling->MEMBER = (VALUE); \
6304 spelling++; \
6305}
400fbf9f 6306
3e4093b6 6307/* Push STRING on the stack. Printed literally. */
400fbf9f 6308
3e4093b6
RS
6309static void
6310push_string (const char *string)
6311{
6312 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6313}
400fbf9f 6314
3e4093b6 6315/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6316
3e4093b6
RS
6317static void
6318push_member_name (tree decl)
6319{
6320 const char *const string
88388a52
JM
6321 = (DECL_NAME (decl)
6322 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6323 : _("<anonymous>"));
3e4093b6
RS
6324 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6325}
400fbf9f 6326
3e4093b6 6327/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6328
3e4093b6 6329static void
a0f0ab9f 6330push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6331{
6332 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6333}
bb58bec5 6334
3e4093b6 6335/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6336
3e4093b6
RS
6337static int
6338spelling_length (void)
6339{
6340 int size = 0;
6341 struct spelling *p;
400fbf9f 6342
3e4093b6
RS
6343 for (p = spelling_base; p < spelling; p++)
6344 {
6345 if (p->kind == SPELLING_BOUNDS)
6346 size += 25;
6347 else
6348 size += strlen (p->u.s) + 1;
6349 }
6350
6351 return size;
400fbf9f 6352}
400fbf9f 6353
3e4093b6 6354/* Print the spelling to BUFFER and return it. */
400fbf9f 6355
3e4093b6
RS
6356static char *
6357print_spelling (char *buffer)
400fbf9f 6358{
3e4093b6
RS
6359 char *d = buffer;
6360 struct spelling *p;
400fbf9f 6361
3e4093b6
RS
6362 for (p = spelling_base; p < spelling; p++)
6363 if (p->kind == SPELLING_BOUNDS)
6364 {
a0f0ab9f 6365 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6366 d += strlen (d);
6367 }
6368 else
6369 {
6370 const char *s;
6371 if (p->kind == SPELLING_MEMBER)
6372 *d++ = '.';
6373 for (s = p->u.s; (*d = *s++); d++)
6374 ;
6375 }
6376 *d++ = '\0';
6377 return buffer;
6378}
400fbf9f 6379
3e4093b6 6380/* Issue an error message for a bad initializer component.
6a8f4e12 6381 GMSGID identifies the message.
3e4093b6 6382 The component name is taken from the spelling stack. */
400fbf9f 6383
3e4093b6 6384void
6a8f4e12 6385error_init (const char *gmsgid)
3e4093b6
RS
6386{
6387 char *ofwhat;
400fbf9f 6388
6a8f4e12
AP
6389 /* The gmsgid may be a format string with %< and %>. */
6390 error (gmsgid);
28dab132 6391 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6392 if (*ofwhat)
bda67431 6393 error ("(near initialization for %qs)", ofwhat);
3e4093b6 6394}
400fbf9f 6395
fcf73884
MLI
6396/* Issue a pedantic warning for a bad initializer component. OPT is
6397 the option OPT_* (from options.h) controlling this warning or 0 if
6a8f4e12 6398 it is unconditionally given. GMSGID identifies the message. The
fcf73884 6399 component name is taken from the spelling stack. */
400fbf9f 6400
3e4093b6 6401void
6a8f4e12 6402pedwarn_init (location_t location, int opt, const char *gmsgid)
3e4093b6
RS
6403{
6404 char *ofwhat;
f90e8e2e 6405
6a8f4e12
AP
6406 /* The gmsgid may be a format string with %< and %>. */
6407 pedwarn (location, opt, gmsgid);
28dab132 6408 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6409 if (*ofwhat)
509c9d60 6410 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
3e4093b6 6411}
9f720c3e 6412
b8698a0f 6413/* Issue a warning for a bad initializer component.
683d6ff9
MLI
6414
6415 OPT is the OPT_W* value corresponding to the warning option that
6a8f4e12 6416 controls this warning. GMSGID identifies the message. The
683d6ff9 6417 component name is taken from the spelling stack. */
61179109 6418
3e4093b6 6419static void
6a8f4e12 6420warning_init (int opt, const char *gmsgid)
3e4093b6
RS
6421{
6422 char *ofwhat;
7e842ef8 6423
6a8f4e12
AP
6424 /* The gmsgid may be a format string with %< and %>. */
6425 warning (opt, gmsgid);
28dab132 6426 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
3e4093b6 6427 if (*ofwhat)
683d6ff9 6428 warning (opt, "(near initialization for %qs)", ofwhat);
3e4093b6
RS
6429}
6430\f
916c5919
JM
6431/* If TYPE is an array type and EXPR is a parenthesized string
6432 constant, warn if pedantic that EXPR is being used to initialize an
6433 object of type TYPE. */
6434
6435void
6436maybe_warn_string_init (tree type, struct c_expr expr)
6437{
6438 if (pedantic
6439 && TREE_CODE (type) == ARRAY_TYPE
6440 && TREE_CODE (expr.value) == STRING_CST
6441 && expr.original_code != STRING_CST)
c1771a20 6442 pedwarn_init (input_location, OPT_Wpedantic,
fcf73884 6443 "array initialized from parenthesized string constant");
916c5919
JM
6444}
6445
3e4093b6
RS
6446/* Digest the parser output INIT as an initializer for type TYPE.
6447 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6448
bbbbb16a
ILT
6449 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6450
928c19bb
JM
6451 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6452
916c5919
JM
6453 If INIT is a string constant, STRICT_STRING is true if it is
6454 unparenthesized or we should not warn here for it being parenthesized.
6455 For other types of INIT, STRICT_STRING is not used.
6456
c2255bc4
AH
6457 INIT_LOC is the location of the INIT.
6458
3e4093b6
RS
6459 REQUIRE_CONSTANT requests an error if non-constant initializers or
6460 elements are seen. */
7e842ef8 6461
3e4093b6 6462static tree
c2255bc4
AH
6463digest_init (location_t init_loc, tree type, tree init, tree origtype,
6464 bool null_pointer_constant, bool strict_string,
6465 int require_constant)
3e4093b6
RS
6466{
6467 enum tree_code code = TREE_CODE (type);
6468 tree inside_init = init;
8ce94e44 6469 tree semantic_type = NULL_TREE;
928c19bb 6470 bool maybe_const = true;
7e842ef8 6471
3e4093b6 6472 if (type == error_mark_node
f01da1a5 6473 || !init
3e4093b6
RS
6474 || init == error_mark_node
6475 || TREE_TYPE (init) == error_mark_node)
6476 return error_mark_node;
7e842ef8 6477
ed248cf7 6478 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6479
8ce94e44
JM
6480 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6481 {
6482 semantic_type = TREE_TYPE (inside_init);
6483 inside_init = TREE_OPERAND (inside_init, 0);
6484 }
928c19bb
JM
6485 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6486 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6487
3e4093b6
RS
6488 /* Initialization of an array of chars from a string constant
6489 optionally enclosed in braces. */
7e842ef8 6490
197463ae
JM
6491 if (code == ARRAY_TYPE && inside_init
6492 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6493 {
267bac10
JM
6494 tree typ1
6495 = (TYPE_ATOMIC (TREE_TYPE (type))
6496 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6497 TYPE_QUAL_ATOMIC)
6498 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6499 /* Note that an array could be both an array of character type
6500 and an array of wchar_t if wchar_t is signed char or unsigned
6501 char. */
6502 bool char_array = (typ1 == char_type_node
6503 || typ1 == signed_char_type_node
6504 || typ1 == unsigned_char_type_node);
6505 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6506 bool char16_array = !!comptypes (typ1, char16_type_node);
6507 bool char32_array = !!comptypes (typ1, char32_type_node);
6508
6509 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6510 {
916c5919 6511 struct c_expr expr;
c466b2cd 6512 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6513 expr.value = inside_init;
6514 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6515 expr.original_type = NULL;
916c5919
JM
6516 maybe_warn_string_init (type, expr);
6517
a45e580b 6518 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6519 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6520 "initialization of a flexible array member");
6521
3e4093b6 6522 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6523 TYPE_MAIN_VARIANT (type)))
3e4093b6 6524 return inside_init;
7e842ef8 6525
c466b2cd 6526 if (char_array)
3e4093b6 6527 {
c466b2cd
KVH
6528 if (typ2 != char_type_node)
6529 {
6530 error_init ("char-array initialized from wide string");
6531 return error_mark_node;
6532 }
3e4093b6 6533 }
c466b2cd 6534 else
3e4093b6 6535 {
c466b2cd
KVH
6536 if (typ2 == char_type_node)
6537 {
6538 error_init ("wide character array initialized from non-wide "
6539 "string");
6540 return error_mark_node;
6541 }
6542 else if (!comptypes(typ1, typ2))
6543 {
6544 error_init ("wide character array initialized from "
6545 "incompatible wide string");
6546 return error_mark_node;
6547 }
7e842ef8 6548 }
2f6e4e97 6549
3e4093b6
RS
6550 TREE_TYPE (inside_init) = type;
6551 if (TYPE_DOMAIN (type) != 0
6552 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6553 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6554 {
6555 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6556
c466b2cd 6557 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6558 because it's ok to ignore the terminating null char
6559 that is counted in the length of the constant. */
5eb4df45
ILT
6560 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6561 (len
6562 - (TYPE_PRECISION (typ1)
6563 / BITS_PER_UNIT))))
6564 pedwarn_init (init_loc, 0,
6565 ("initializer-string for array of chars "
6566 "is too long"));
6567 else if (warn_cxx_compat
6568 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6569 warning_at (init_loc, OPT_Wc___compat,
6570 ("initializer-string for array chars "
6571 "is too long for C++"));
6572 }
7e842ef8 6573
3e4093b6 6574 return inside_init;
7e842ef8 6575 }
197463ae
JM
6576 else if (INTEGRAL_TYPE_P (typ1))
6577 {
6578 error_init ("array of inappropriate type initialized "
6579 "from string constant");
6580 return error_mark_node;
6581 }
7e842ef8
PE
6582 }
6583
3e4093b6
RS
6584 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6585 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6586 below and handle as a constructor. */
e89be13b
JJ
6587 if (code == VECTOR_TYPE
6588 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6589 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6590 && TREE_CONSTANT (inside_init))
6591 {
6592 if (TREE_CODE (inside_init) == VECTOR_CST
6593 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6594 TYPE_MAIN_VARIANT (type)))
6595 return inside_init;
6596
6597 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6598 {
4038c495
GB
6599 unsigned HOST_WIDE_INT ix;
6600 tree value;
6601 bool constant_p = true;
e89be13b
JJ
6602
6603 /* Iterate through elements and check if all constructor
6604 elements are *_CSTs. */
4038c495
GB
6605 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6606 if (!CONSTANT_CLASS_P (value))
6607 {
6608 constant_p = false;
6609 break;
6610 }
e89be13b 6611
4038c495
GB
6612 if (constant_p)
6613 return build_vector_from_ctor (type,
6614 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6615 }
6616 }
6035d635 6617
ca085fd7
MLI
6618 if (warn_sequence_point)
6619 verify_sequence_points (inside_init);
6620
3e4093b6
RS
6621 /* Any type can be initialized
6622 from an expression of the same type, optionally with braces. */
400fbf9f 6623
3e4093b6
RS
6624 if (inside_init && TREE_TYPE (inside_init) != 0
6625 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6626 TYPE_MAIN_VARIANT (type))
3e4093b6 6627 || (code == ARRAY_TYPE
132da1a5 6628 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6629 || (code == VECTOR_TYPE
132da1a5 6630 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6631 || (code == POINTER_TYPE
3897f229 6632 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6633 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6634 TREE_TYPE (type)))))
3e4093b6
RS
6635 {
6636 if (code == POINTER_TYPE)
b494fd98 6637 {
b494fd98
EB
6638 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6639 {
f2a71bbc
JM
6640 if (TREE_CODE (inside_init) == STRING_CST
6641 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6642 inside_init = array_to_pointer_conversion
6643 (init_loc, inside_init);
f2a71bbc
JM
6644 else
6645 {
6646 error_init ("invalid use of non-lvalue array");
6647 return error_mark_node;
6648 }
b494fd98 6649 }
f2a71bbc 6650 }
b494fd98 6651
bae39a73
NS
6652 if (code == VECTOR_TYPE)
6653 /* Although the types are compatible, we may require a
6654 conversion. */
6655 inside_init = convert (type, inside_init);
3e4093b6 6656
ca58211b
PB
6657 if (require_constant
6658 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6659 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6660 {
3e4093b6
RS
6661 /* As an extension, allow initializing objects with static storage
6662 duration with compound literals (which are then treated just as
ca58211b
PB
6663 the brace enclosed list they contain). Also allow this for
6664 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6665 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6666 inside_init = DECL_INITIAL (decl);
400fbf9f 6667 }
3e4093b6
RS
6668
6669 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6670 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6671 {
3e4093b6
RS
6672 error_init ("array initialized from non-constant array expression");
6673 return error_mark_node;
400fbf9f 6674 }
400fbf9f 6675
c1771a20 6676 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6677 -pedantic-errors is specified. In the later case, we always want
6678 an error. In the former case, we simply want a warning. */
6679 if (require_constant && pedantic
6680 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6681 {
6682 inside_init
6683 = valid_compound_expr_initializer (inside_init,
6684 TREE_TYPE (inside_init));
6685 if (inside_init == error_mark_node)
6686 error_init ("initializer element is not constant");
2f6e4e97 6687 else
c1771a20 6688 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6689 "initializer element is not constant");
3e4093b6
RS
6690 if (flag_pedantic_errors)
6691 inside_init = error_mark_node;
6692 }
6693 else if (require_constant
116df786
RH
6694 && !initializer_constant_valid_p (inside_init,
6695 TREE_TYPE (inside_init)))
3e4093b6
RS
6696 {
6697 error_init ("initializer element is not constant");
6698 inside_init = error_mark_node;
8b40563c 6699 }
928c19bb 6700 else if (require_constant && !maybe_const)
c2255bc4 6701 pedwarn_init (init_loc, 0,
928c19bb 6702 "initializer element is not a constant expression");
f735a153 6703
90137d8f 6704 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6705 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
c2255bc4
AH
6706 inside_init = convert_for_assignment (init_loc, type, inside_init,
6707 origtype,
bbbbb16a 6708 ic_init, null_pointer_constant,
928c19bb 6709 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6710 return inside_init;
6711 }
f735a153 6712
3e4093b6 6713 /* Handle scalar types, including conversions. */
400fbf9f 6714
ab22c1fa
CF
6715 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6716 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6717 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6718 {
f2a71bbc
JM
6719 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6720 && (TREE_CODE (init) == STRING_CST
6721 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6722 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6723 if (semantic_type)
6724 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6725 inside_init);
3e4093b6 6726 inside_init
c2255bc4
AH
6727 = convert_for_assignment (init_loc, type, inside_init, origtype,
6728 ic_init, null_pointer_constant,
3e4093b6 6729 NULL_TREE, NULL_TREE, 0);
2f6e4e97 6730
3274deff
JW
6731 /* Check to see if we have already given an error message. */
6732 if (inside_init == error_mark_node)
6733 ;
3f75a254 6734 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6735 {
3e4093b6
RS
6736 error_init ("initializer element is not constant");
6737 inside_init = error_mark_node;
400fbf9f 6738 }
3e4093b6 6739 else if (require_constant
116df786
RH
6740 && !initializer_constant_valid_p (inside_init,
6741 TREE_TYPE (inside_init)))
400fbf9f 6742 {
3e4093b6
RS
6743 error_init ("initializer element is not computable at load time");
6744 inside_init = error_mark_node;
400fbf9f 6745 }
928c19bb 6746 else if (require_constant && !maybe_const)
c2255bc4 6747 pedwarn_init (init_loc, 0,
928c19bb 6748 "initializer element is not a constant expression");
3e4093b6
RS
6749
6750 return inside_init;
400fbf9f 6751 }
d9fc6069 6752
3e4093b6 6753 /* Come here only for records and arrays. */
d9fc6069 6754
3e4093b6 6755 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6756 {
3e4093b6
RS
6757 error_init ("variable-sized object may not be initialized");
6758 return error_mark_node;
d9fc6069 6759 }
3e4093b6
RS
6760
6761 error_init ("invalid initializer");
6762 return error_mark_node;
d9fc6069 6763}
400fbf9f 6764\f
3e4093b6 6765/* Handle initializers that use braces. */
400fbf9f 6766
3e4093b6
RS
6767/* Type of object we are accumulating a constructor for.
6768 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6769static tree constructor_type;
400fbf9f 6770
3e4093b6
RS
6771/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6772 left to fill. */
6773static tree constructor_fields;
400fbf9f 6774
3e4093b6
RS
6775/* For an ARRAY_TYPE, this is the specified index
6776 at which to store the next element we get. */
6777static tree constructor_index;
400fbf9f 6778
3e4093b6
RS
6779/* For an ARRAY_TYPE, this is the maximum index. */
6780static tree constructor_max_index;
400fbf9f 6781
3e4093b6
RS
6782/* For a RECORD_TYPE, this is the first field not yet written out. */
6783static tree constructor_unfilled_fields;
400fbf9f 6784
3e4093b6
RS
6785/* For an ARRAY_TYPE, this is the index of the first element
6786 not yet written out. */
6787static tree constructor_unfilled_index;
895ea614 6788
3e4093b6
RS
6789/* In a RECORD_TYPE, the byte index of the next consecutive field.
6790 This is so we can generate gaps between fields, when appropriate. */
6791static tree constructor_bit_index;
10d5caec 6792
3e4093b6
RS
6793/* If we are saving up the elements rather than allocating them,
6794 this is the list of elements so far (in reverse order,
6795 most recent first). */
9771b263 6796static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6797
3e4093b6
RS
6798/* 1 if constructor should be incrementally stored into a constructor chain,
6799 0 if all the elements should be kept in AVL tree. */
6800static int constructor_incremental;
ad47f1e5 6801
3e4093b6
RS
6802/* 1 if so far this constructor's elements are all compile-time constants. */
6803static int constructor_constant;
ad47f1e5 6804
3e4093b6
RS
6805/* 1 if so far this constructor's elements are all valid address constants. */
6806static int constructor_simple;
ad47f1e5 6807
928c19bb
JM
6808/* 1 if this constructor has an element that cannot be part of a
6809 constant expression. */
6810static int constructor_nonconst;
6811
3e4093b6
RS
6812/* 1 if this constructor is erroneous so far. */
6813static int constructor_erroneous;
d45cf215 6814
3e4093b6
RS
6815/* Structure for managing pending initializer elements, organized as an
6816 AVL tree. */
d45cf215 6817
3e4093b6 6818struct init_node
d45cf215 6819{
3e4093b6
RS
6820 struct init_node *left, *right;
6821 struct init_node *parent;
6822 int balance;
6823 tree purpose;
6824 tree value;
bbbbb16a 6825 tree origtype;
d45cf215
RS
6826};
6827
3e4093b6
RS
6828/* Tree of pending elements at this constructor level.
6829 These are elements encountered out of order
6830 which belong at places we haven't reached yet in actually
6831 writing the output.
6832 Will never hold tree nodes across GC runs. */
6833static struct init_node *constructor_pending_elts;
d45cf215 6834
3e4093b6
RS
6835/* The SPELLING_DEPTH of this constructor. */
6836static int constructor_depth;
d45cf215 6837
3e4093b6
RS
6838/* DECL node for which an initializer is being read.
6839 0 means we are reading a constructor expression
6840 such as (struct foo) {...}. */
6841static tree constructor_decl;
d45cf215 6842
3e4093b6
RS
6843/* Nonzero if this is an initializer for a top-level decl. */
6844static int constructor_top_level;
d45cf215 6845
3e4093b6
RS
6846/* Nonzero if there were any member designators in this initializer. */
6847static int constructor_designated;
d45cf215 6848
3e4093b6
RS
6849/* Nesting depth of designator list. */
6850static int designator_depth;
d45cf215 6851
3e4093b6 6852/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6853static int designator_erroneous;
d45cf215 6854
3e4093b6
RS
6855\f
6856/* This stack has a level for each implicit or explicit level of
6857 structuring in the initializer, including the outermost one. It
6858 saves the values of most of the variables above. */
d45cf215 6859
3e4093b6
RS
6860struct constructor_range_stack;
6861
6862struct constructor_stack
d45cf215 6863{
3e4093b6
RS
6864 struct constructor_stack *next;
6865 tree type;
6866 tree fields;
6867 tree index;
6868 tree max_index;
6869 tree unfilled_index;
6870 tree unfilled_fields;
6871 tree bit_index;
9771b263 6872 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6873 struct init_node *pending_elts;
6874 int offset;
6875 int depth;
916c5919 6876 /* If value nonzero, this value should replace the entire
3e4093b6 6877 constructor at this level. */
916c5919 6878 struct c_expr replacement_value;
3e4093b6
RS
6879 struct constructor_range_stack *range_stack;
6880 char constant;
6881 char simple;
928c19bb 6882 char nonconst;
3e4093b6
RS
6883 char implicit;
6884 char erroneous;
6885 char outer;
6886 char incremental;
6887 char designated;
6888};
d45cf215 6889
802415d1 6890static struct constructor_stack *constructor_stack;
d45cf215 6891
3e4093b6
RS
6892/* This stack represents designators from some range designator up to
6893 the last designator in the list. */
d45cf215 6894
3e4093b6
RS
6895struct constructor_range_stack
6896{
6897 struct constructor_range_stack *next, *prev;
6898 struct constructor_stack *stack;
6899 tree range_start;
6900 tree index;
6901 tree range_end;
6902 tree fields;
6903};
d45cf215 6904
802415d1 6905static struct constructor_range_stack *constructor_range_stack;
d45cf215 6906
3e4093b6
RS
6907/* This stack records separate initializers that are nested.
6908 Nested initializers can't happen in ANSI C, but GNU C allows them
6909 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6910
3e4093b6 6911struct initializer_stack
d45cf215 6912{
3e4093b6
RS
6913 struct initializer_stack *next;
6914 tree decl;
3e4093b6
RS
6915 struct constructor_stack *constructor_stack;
6916 struct constructor_range_stack *constructor_range_stack;
9771b263 6917 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6918 struct spelling *spelling;
6919 struct spelling *spelling_base;
6920 int spelling_size;
6921 char top_level;
6922 char require_constant_value;
6923 char require_constant_elements;
6924};
d45cf215 6925
802415d1 6926static struct initializer_stack *initializer_stack;
3e4093b6
RS
6927\f
6928/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6929
6930void
a396f8ae 6931start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6932{
3e4093b6 6933 const char *locus;
cceb1885 6934 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6935
3e4093b6 6936 p->decl = constructor_decl;
3e4093b6
RS
6937 p->require_constant_value = require_constant_value;
6938 p->require_constant_elements = require_constant_elements;
6939 p->constructor_stack = constructor_stack;
6940 p->constructor_range_stack = constructor_range_stack;
6941 p->elements = constructor_elements;
6942 p->spelling = spelling;
6943 p->spelling_base = spelling_base;
6944 p->spelling_size = spelling_size;
6945 p->top_level = constructor_top_level;
6946 p->next = initializer_stack;
6947 initializer_stack = p;
400fbf9f 6948
3e4093b6 6949 constructor_decl = decl;
3e4093b6
RS
6950 constructor_designated = 0;
6951 constructor_top_level = top_level;
400fbf9f 6952
6f17bbcf 6953 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6954 {
6955 require_constant_value = TREE_STATIC (decl);
6956 require_constant_elements
6957 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
6958 /* For a scalar, you can always use any value to initialize,
6959 even within braces. */
6960 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
6961 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
6962 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
6963 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 6964 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
6965 }
6966 else
6967 {
6968 require_constant_value = 0;
6969 require_constant_elements = 0;
88388a52 6970 locus = _("(anonymous)");
3e4093b6 6971 }
b71c7f8a 6972
3e4093b6
RS
6973 constructor_stack = 0;
6974 constructor_range_stack = 0;
b71c7f8a 6975
3e4093b6
RS
6976 missing_braces_mentioned = 0;
6977
6978 spelling_base = 0;
6979 spelling_size = 0;
6980 RESTORE_SPELLING_DEPTH (0);
6981
6982 if (locus)
6983 push_string (locus);
6984}
6985
6986void
6987finish_init (void)
b71c7f8a 6988{
3e4093b6 6989 struct initializer_stack *p = initializer_stack;
b71c7f8a 6990
3e4093b6
RS
6991 /* Free the whole constructor stack of this initializer. */
6992 while (constructor_stack)
6993 {
6994 struct constructor_stack *q = constructor_stack;
6995 constructor_stack = q->next;
6996 free (q);
6997 }
6998
366de0ce 6999 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7000
7001 /* Pop back to the data of the outer initializer (if any). */
36579663 7002 free (spelling_base);
3aeb3655 7003
3e4093b6 7004 constructor_decl = p->decl;
3e4093b6
RS
7005 require_constant_value = p->require_constant_value;
7006 require_constant_elements = p->require_constant_elements;
7007 constructor_stack = p->constructor_stack;
7008 constructor_range_stack = p->constructor_range_stack;
7009 constructor_elements = p->elements;
7010 spelling = p->spelling;
7011 spelling_base = p->spelling_base;
7012 spelling_size = p->spelling_size;
7013 constructor_top_level = p->top_level;
7014 initializer_stack = p->next;
7015 free (p);
b71c7f8a 7016}
400fbf9f 7017\f
3e4093b6
RS
7018/* Call here when we see the initializer is surrounded by braces.
7019 This is instead of a call to push_init_level;
7020 it is matched by a call to pop_init_level.
400fbf9f 7021
3e4093b6
RS
7022 TYPE is the type to initialize, for a constructor expression.
7023 For an initializer for a decl, TYPE is zero. */
400fbf9f 7024
3e4093b6
RS
7025void
7026really_start_incremental_init (tree type)
400fbf9f 7027{
5d038c4c 7028 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7029
3e4093b6
RS
7030 if (type == 0)
7031 type = TREE_TYPE (constructor_decl);
400fbf9f 7032
b6fc2cdb
PB
7033 if (TREE_CODE (type) == VECTOR_TYPE
7034 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7035 error ("opaque vector types cannot be initialized");
400fbf9f 7036
3e4093b6
RS
7037 p->type = constructor_type;
7038 p->fields = constructor_fields;
7039 p->index = constructor_index;
7040 p->max_index = constructor_max_index;
7041 p->unfilled_index = constructor_unfilled_index;
7042 p->unfilled_fields = constructor_unfilled_fields;
7043 p->bit_index = constructor_bit_index;
7044 p->elements = constructor_elements;
7045 p->constant = constructor_constant;
7046 p->simple = constructor_simple;
928c19bb 7047 p->nonconst = constructor_nonconst;
3e4093b6
RS
7048 p->erroneous = constructor_erroneous;
7049 p->pending_elts = constructor_pending_elts;
7050 p->depth = constructor_depth;
916c5919
JM
7051 p->replacement_value.value = 0;
7052 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7053 p->replacement_value.original_type = NULL;
3e4093b6
RS
7054 p->implicit = 0;
7055 p->range_stack = 0;
7056 p->outer = 0;
7057 p->incremental = constructor_incremental;
7058 p->designated = constructor_designated;
7059 p->next = 0;
7060 constructor_stack = p;
b13aca19 7061
3e4093b6
RS
7062 constructor_constant = 1;
7063 constructor_simple = 1;
928c19bb 7064 constructor_nonconst = 0;
3e4093b6 7065 constructor_depth = SPELLING_DEPTH ();
9771b263 7066 constructor_elements = NULL;
3e4093b6
RS
7067 constructor_pending_elts = 0;
7068 constructor_type = type;
7069 constructor_incremental = 1;
7070 constructor_designated = 0;
7071 designator_depth = 0;
b06df647 7072 designator_erroneous = 0;
400fbf9f 7073
3e4093b6
RS
7074 if (TREE_CODE (constructor_type) == RECORD_TYPE
7075 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7076 {
3e4093b6
RS
7077 constructor_fields = TYPE_FIELDS (constructor_type);
7078 /* Skip any nameless bit fields at the beginning. */
7079 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7080 && DECL_NAME (constructor_fields) == 0)
910ad8de 7081 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7082
3e4093b6
RS
7083 constructor_unfilled_fields = constructor_fields;
7084 constructor_bit_index = bitsize_zero_node;
400fbf9f 7085 }
3e4093b6
RS
7086 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7087 {
7088 if (TYPE_DOMAIN (constructor_type))
7089 {
7090 constructor_max_index
7091 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7092
3e4093b6
RS
7093 /* Detect non-empty initializations of zero-length arrays. */
7094 if (constructor_max_index == NULL_TREE
7095 && TYPE_SIZE (constructor_type))
9a9d280e 7096 constructor_max_index = integer_minus_one_node;
400fbf9f 7097
3e4093b6
RS
7098 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7099 to initialize VLAs will cause a proper error; avoid tree
7100 checking errors as well by setting a safe value. */
7101 if (constructor_max_index
7102 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7103 constructor_max_index = integer_minus_one_node;
59c83dbf 7104
3e4093b6
RS
7105 constructor_index
7106 = convert (bitsizetype,
7107 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7108 }
3e4093b6 7109 else
493179da
JM
7110 {
7111 constructor_index = bitsize_zero_node;
7112 constructor_max_index = NULL_TREE;
7113 }
59c83dbf 7114
3e4093b6
RS
7115 constructor_unfilled_index = constructor_index;
7116 }
7117 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7118 {
7119 /* Vectors are like simple fixed-size arrays. */
7120 constructor_max_index =
c62c040f 7121 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7122 constructor_index = bitsize_zero_node;
3e4093b6
RS
7123 constructor_unfilled_index = constructor_index;
7124 }
7125 else
7126 {
7127 /* Handle the case of int x = {5}; */
7128 constructor_fields = constructor_type;
7129 constructor_unfilled_fields = constructor_type;
7130 }
7131}
7132\f
7133/* Push down into a subobject, for initialization.
7134 If this is for an explicit set of braces, IMPLICIT is 0.
7135 If it is because the next element belongs at a lower level,
7136 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7137
3e4093b6 7138void
a1e3b3d9 7139push_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7140{
7141 struct constructor_stack *p;
7142 tree value = NULL_TREE;
400fbf9f 7143
3e4093b6 7144 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7145 pop them now. If implicit == 1, this will have been done in
7146 process_init_element; do not repeat it here because in the case
7147 of excess initializers for an empty aggregate this leads to an
7148 infinite cycle of popping a level and immediately recreating
7149 it. */
7150 if (implicit != 1)
3e4093b6 7151 {
472d98b4
JM
7152 while (constructor_stack->implicit)
7153 {
7154 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7155 || TREE_CODE (constructor_type) == UNION_TYPE)
7156 && constructor_fields == 0)
a1e3b3d9
LB
7157 process_init_element (pop_init_level (1, braced_init_obstack),
7158 true, braced_init_obstack);
472d98b4
JM
7159 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7160 && constructor_max_index
7161 && tree_int_cst_lt (constructor_max_index,
7162 constructor_index))
a1e3b3d9
LB
7163 process_init_element (pop_init_level (1, braced_init_obstack),
7164 true, braced_init_obstack);
472d98b4
JM
7165 else
7166 break;
7167 }
3e4093b6 7168 }
400fbf9f 7169
3e4093b6
RS
7170 /* Unless this is an explicit brace, we need to preserve previous
7171 content if any. */
7172 if (implicit)
7173 {
7174 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7175 || TREE_CODE (constructor_type) == UNION_TYPE)
7176 && constructor_fields)
a1e3b3d9 7177 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7178 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7179 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7180 }
7181
5d038c4c 7182 p = XNEW (struct constructor_stack);
3e4093b6
RS
7183 p->type = constructor_type;
7184 p->fields = constructor_fields;
7185 p->index = constructor_index;
7186 p->max_index = constructor_max_index;
7187 p->unfilled_index = constructor_unfilled_index;
7188 p->unfilled_fields = constructor_unfilled_fields;
7189 p->bit_index = constructor_bit_index;
7190 p->elements = constructor_elements;
7191 p->constant = constructor_constant;
7192 p->simple = constructor_simple;
928c19bb 7193 p->nonconst = constructor_nonconst;
3e4093b6
RS
7194 p->erroneous = constructor_erroneous;
7195 p->pending_elts = constructor_pending_elts;
7196 p->depth = constructor_depth;
916c5919
JM
7197 p->replacement_value.value = 0;
7198 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7199 p->replacement_value.original_type = NULL;
3e4093b6
RS
7200 p->implicit = implicit;
7201 p->outer = 0;
7202 p->incremental = constructor_incremental;
7203 p->designated = constructor_designated;
7204 p->next = constructor_stack;
7205 p->range_stack = 0;
7206 constructor_stack = p;
400fbf9f 7207
3e4093b6
RS
7208 constructor_constant = 1;
7209 constructor_simple = 1;
928c19bb 7210 constructor_nonconst = 0;
3e4093b6 7211 constructor_depth = SPELLING_DEPTH ();
9771b263 7212 constructor_elements = NULL;
3e4093b6
RS
7213 constructor_incremental = 1;
7214 constructor_designated = 0;
7215 constructor_pending_elts = 0;
7216 if (!implicit)
400fbf9f 7217 {
3e4093b6
RS
7218 p->range_stack = constructor_range_stack;
7219 constructor_range_stack = 0;
7220 designator_depth = 0;
b06df647 7221 designator_erroneous = 0;
3e4093b6 7222 }
400fbf9f 7223
3e4093b6
RS
7224 /* Don't die if an entire brace-pair level is superfluous
7225 in the containing level. */
7226 if (constructor_type == 0)
7227 ;
7228 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7229 || TREE_CODE (constructor_type) == UNION_TYPE)
7230 {
7231 /* Don't die if there are extra init elts at the end. */
7232 if (constructor_fields == 0)
7233 constructor_type = 0;
7234 else
400fbf9f 7235 {
3e4093b6
RS
7236 constructor_type = TREE_TYPE (constructor_fields);
7237 push_member_name (constructor_fields);
7238 constructor_depth++;
400fbf9f 7239 }
3e4093b6
RS
7240 }
7241 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7242 {
7243 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7244 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7245 constructor_depth++;
400fbf9f
JW
7246 }
7247
3e4093b6 7248 if (constructor_type == 0)
400fbf9f 7249 {
3e4093b6
RS
7250 error_init ("extra brace group at end of initializer");
7251 constructor_fields = 0;
7252 constructor_unfilled_fields = 0;
7253 return;
400fbf9f
JW
7254 }
7255
3e4093b6
RS
7256 if (value && TREE_CODE (value) == CONSTRUCTOR)
7257 {
7258 constructor_constant = TREE_CONSTANT (value);
7259 constructor_simple = TREE_STATIC (value);
928c19bb 7260 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7261 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7262 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7263 && (TREE_CODE (constructor_type) == RECORD_TYPE
7264 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7265 set_nonincremental_init (braced_init_obstack);
3e4093b6 7266 }
400fbf9f 7267
3e4093b6
RS
7268 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7269 {
7270 missing_braces_mentioned = 1;
683d6ff9 7271 warning_init (OPT_Wmissing_braces, "missing braces around initializer");
3e4093b6 7272 }
400fbf9f 7273
3e4093b6
RS
7274 if (TREE_CODE (constructor_type) == RECORD_TYPE
7275 || TREE_CODE (constructor_type) == UNION_TYPE)
7276 {
7277 constructor_fields = TYPE_FIELDS (constructor_type);
7278 /* Skip any nameless bit fields at the beginning. */
7279 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7280 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7281 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7282
3e4093b6
RS
7283 constructor_unfilled_fields = constructor_fields;
7284 constructor_bit_index = bitsize_zero_node;
7285 }
7286 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7287 {
7288 /* Vectors are like simple fixed-size arrays. */
7289 constructor_max_index =
c62c040f
RG
7290 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7291 constructor_index = bitsize_int (0);
3e4093b6
RS
7292 constructor_unfilled_index = constructor_index;
7293 }
7294 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7295 {
7296 if (TYPE_DOMAIN (constructor_type))
7297 {
7298 constructor_max_index
7299 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7300
3e4093b6
RS
7301 /* Detect non-empty initializations of zero-length arrays. */
7302 if (constructor_max_index == NULL_TREE
7303 && TYPE_SIZE (constructor_type))
9a9d280e 7304 constructor_max_index = integer_minus_one_node;
de520661 7305
3e4093b6
RS
7306 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7307 to initialize VLAs will cause a proper error; avoid tree
7308 checking errors as well by setting a safe value. */
7309 if (constructor_max_index
7310 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7311 constructor_max_index = integer_minus_one_node;
b62acd60 7312
3e4093b6
RS
7313 constructor_index
7314 = convert (bitsizetype,
7315 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7316 }
7317 else
7318 constructor_index = bitsize_zero_node;
de520661 7319
3e4093b6
RS
7320 constructor_unfilled_index = constructor_index;
7321 if (value && TREE_CODE (value) == STRING_CST)
7322 {
7323 /* We need to split the char/wchar array into individual
7324 characters, so that we don't have to special case it
7325 everywhere. */
a1e3b3d9 7326 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7327 }
7328 }
7329 else
7330 {
b4519d39 7331 if (constructor_type != error_mark_node)
683d6ff9 7332 warning_init (0, "braces around scalar initializer");
3e4093b6
RS
7333 constructor_fields = constructor_type;
7334 constructor_unfilled_fields = constructor_type;
7335 }
7336}
8b6a5902 7337
3e4093b6 7338/* At the end of an implicit or explicit brace level,
916c5919
JM
7339 finish up that level of constructor. If a single expression
7340 with redundant braces initialized that level, return the
7341 c_expr structure for that expression. Otherwise, the original_code
7342 element is set to ERROR_MARK.
7343 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7344 from inner levels (process_init_element ignores that),
916c5919 7345 but return error_mark_node as the value from the outermost level
3e4093b6 7346 (that's what we want to put in DECL_INITIAL).
916c5919 7347 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7348
916c5919 7349struct c_expr
a1e3b3d9 7350pop_init_level (int implicit, struct obstack * braced_init_obstack)
3e4093b6
RS
7351{
7352 struct constructor_stack *p;
916c5919
JM
7353 struct c_expr ret;
7354 ret.value = 0;
7355 ret.original_code = ERROR_MARK;
6866c6e8 7356 ret.original_type = NULL;
de520661 7357
3e4093b6
RS
7358 if (implicit == 0)
7359 {
7360 /* When we come to an explicit close brace,
7361 pop any inner levels that didn't have explicit braces. */
7362 while (constructor_stack->implicit)
a1e3b3d9
LB
7363 {
7364 process_init_element (pop_init_level (1, braced_init_obstack),
7365 true, braced_init_obstack);
7366 }
366de0ce 7367 gcc_assert (!constructor_range_stack);
3e4093b6 7368 }
e5e809f4 7369
0066ef9c
RH
7370 /* Now output all pending elements. */
7371 constructor_incremental = 1;
a1e3b3d9 7372 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7373
3e4093b6 7374 p = constructor_stack;
e5e809f4 7375
3e4093b6
RS
7376 /* Error for initializing a flexible array member, or a zero-length
7377 array member in an inappropriate context. */
7378 if (constructor_type && constructor_fields
7379 && TREE_CODE (constructor_type) == ARRAY_TYPE
7380 && TYPE_DOMAIN (constructor_type)
3f75a254 7381 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7382 {
7383 /* Silently discard empty initializations. The parser will
7384 already have pedwarned for empty brackets. */
7385 if (integer_zerop (constructor_unfilled_index))
7386 constructor_type = NULL_TREE;
366de0ce 7387 else
3e4093b6 7388 {
366de0ce 7389 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7390
3e4093b6
RS
7391 if (constructor_depth > 2)
7392 error_init ("initialization of flexible array member in a nested context");
fcf73884 7393 else
c1771a20 7394 pedwarn_init (input_location, OPT_Wpedantic,
509c9d60 7395 "initialization of a flexible array member");
de520661 7396
3e4093b6
RS
7397 /* We have already issued an error message for the existence
7398 of a flexible array member not at the end of the structure.
535a42b1 7399 Discard the initializer so that we do not die later. */
910ad8de 7400 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7401 constructor_type = NULL_TREE;
7402 }
3e4093b6 7403 }
de520661 7404
3e4093b6 7405 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7406 if (warn_missing_field_initializers
3e4093b6
RS
7407 && constructor_type
7408 && TREE_CODE (constructor_type) == RECORD_TYPE
7409 && constructor_unfilled_fields)
7410 {
49819fef 7411 bool constructor_zeroinit =
9771b263
DN
7412 (vec_safe_length (constructor_elements) == 1
7413 && integer_zerop ((*constructor_elements)[0].value));
49819fef 7414
3e4093b6
RS
7415 /* Do not warn for flexible array members or zero-length arrays. */
7416 while (constructor_unfilled_fields
3f75a254 7417 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7418 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7419 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7420
49819fef
AM
7421 if (constructor_unfilled_fields
7422 /* Do not warn if this level of the initializer uses member
7423 designators; it is likely to be deliberate. */
7424 && !constructor_designated
7425 /* Do not warn about initializing with ` = {0}'. */
7426 && !constructor_zeroinit)
3e4093b6 7427 {
32397f22
MLI
7428 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7429 "missing initializer for field %qD of %qT",
7430 constructor_unfilled_fields,
7431 constructor_type))
7432 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7433 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7434 }
7435 }
de520661 7436
3e4093b6 7437 /* Pad out the end of the structure. */
916c5919 7438 if (p->replacement_value.value)
3e4093b6
RS
7439 /* If this closes a superfluous brace pair,
7440 just pass out the element between them. */
916c5919 7441 ret = p->replacement_value;
3e4093b6
RS
7442 else if (constructor_type == 0)
7443 ;
7444 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7445 && TREE_CODE (constructor_type) != UNION_TYPE
7446 && TREE_CODE (constructor_type) != ARRAY_TYPE
7447 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7448 {
7449 /* A nonincremental scalar initializer--just return
7450 the element, after verifying there is just one. */
9771b263 7451 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7452 {
7453 if (!constructor_erroneous)
7454 error_init ("empty scalar initializer");
916c5919 7455 ret.value = error_mark_node;
3e4093b6 7456 }
9771b263 7457 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6
RS
7458 {
7459 error_init ("extra elements in scalar initializer");
9771b263 7460 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7461 }
7462 else
9771b263 7463 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7464 }
7465 else
7466 {
7467 if (constructor_erroneous)
916c5919 7468 ret.value = error_mark_node;
3e4093b6
RS
7469 else
7470 {
916c5919 7471 ret.value = build_constructor (constructor_type,
4038c495 7472 constructor_elements);
3e4093b6 7473 if (constructor_constant)
51eed280 7474 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7475 if (constructor_constant && constructor_simple)
916c5919 7476 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7477 if (constructor_nonconst)
7478 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7479 }
7480 }
de520661 7481
928c19bb
JM
7482 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7483 {
7484 if (constructor_nonconst)
7485 ret.original_code = C_MAYBE_CONST_EXPR;
7486 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7487 ret.original_code = ERROR_MARK;
7488 }
7489
3e4093b6
RS
7490 constructor_type = p->type;
7491 constructor_fields = p->fields;
7492 constructor_index = p->index;
7493 constructor_max_index = p->max_index;
7494 constructor_unfilled_index = p->unfilled_index;
7495 constructor_unfilled_fields = p->unfilled_fields;
7496 constructor_bit_index = p->bit_index;
7497 constructor_elements = p->elements;
7498 constructor_constant = p->constant;
7499 constructor_simple = p->simple;
928c19bb 7500 constructor_nonconst = p->nonconst;
3e4093b6
RS
7501 constructor_erroneous = p->erroneous;
7502 constructor_incremental = p->incremental;
7503 constructor_designated = p->designated;
7504 constructor_pending_elts = p->pending_elts;
7505 constructor_depth = p->depth;
7506 if (!p->implicit)
7507 constructor_range_stack = p->range_stack;
7508 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7509
3e4093b6
RS
7510 constructor_stack = p->next;
7511 free (p);
b621a4dd 7512
5d5e98dc
VR
7513 if (ret.value == 0 && constructor_stack == 0)
7514 ret.value = error_mark_node;
916c5919 7515 return ret;
3e4093b6 7516}
8b6a5902 7517
3e4093b6
RS
7518/* Common handling for both array range and field name designators.
7519 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7520
3e4093b6 7521static int
a1e3b3d9 7522set_designator (int array, struct obstack * braced_init_obstack)
de520661 7523{
3e4093b6
RS
7524 tree subtype;
7525 enum tree_code subcode;
de520661 7526
3e4093b6
RS
7527 /* Don't die if an entire brace-pair level is superfluous
7528 in the containing level. */
7529 if (constructor_type == 0)
7530 return 1;
de520661 7531
366de0ce
NS
7532 /* If there were errors in this designator list already, bail out
7533 silently. */
b06df647 7534 if (designator_erroneous)
3e4093b6 7535 return 1;
e28cae4f 7536
3e4093b6
RS
7537 if (!designator_depth)
7538 {
366de0ce 7539 gcc_assert (!constructor_range_stack);
de520661 7540
3e4093b6
RS
7541 /* Designator list starts at the level of closest explicit
7542 braces. */
7543 while (constructor_stack->implicit)
a1e3b3d9
LB
7544 {
7545 process_init_element (pop_init_level (1, braced_init_obstack),
7546 true, braced_init_obstack);
7547 }
3e4093b6
RS
7548 constructor_designated = 1;
7549 return 0;
7550 }
de520661 7551
366de0ce 7552 switch (TREE_CODE (constructor_type))
3c3fa147 7553 {
366de0ce
NS
7554 case RECORD_TYPE:
7555 case UNION_TYPE:
3e4093b6
RS
7556 subtype = TREE_TYPE (constructor_fields);
7557 if (subtype != error_mark_node)
7558 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7559 break;
7560 case ARRAY_TYPE:
3e4093b6 7561 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7562 break;
7563 default:
7564 gcc_unreachable ();
de520661 7565 }
400fbf9f 7566
3e4093b6
RS
7567 subcode = TREE_CODE (subtype);
7568 if (array && subcode != ARRAY_TYPE)
7569 {
7570 error_init ("array index in non-array initializer");
7571 return 1;
7572 }
7573 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7574 {
7575 error_init ("field name not in record or union initializer");
7576 return 1;
7577 }
d45cf215 7578
3e4093b6 7579 constructor_designated = 1;
a1e3b3d9 7580 push_init_level (2, braced_init_obstack);
3e4093b6 7581 return 0;
de520661 7582}
400fbf9f 7583
3e4093b6
RS
7584/* If there are range designators in designator list, push a new designator
7585 to constructor_range_stack. RANGE_END is end of such stack range or
7586 NULL_TREE if there is no range designator at this level. */
400fbf9f 7587
3e4093b6 7588static void
a1e3b3d9 7589push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7590{
7591 struct constructor_range_stack *p;
400fbf9f 7592
a1e3b3d9
LB
7593 p = (struct constructor_range_stack *)
7594 obstack_alloc (braced_init_obstack,
7595 sizeof (struct constructor_range_stack));
3e4093b6
RS
7596 p->prev = constructor_range_stack;
7597 p->next = 0;
7598 p->fields = constructor_fields;
7599 p->range_start = constructor_index;
7600 p->index = constructor_index;
7601 p->stack = constructor_stack;
7602 p->range_end = range_end;
8b6a5902 7603 if (constructor_range_stack)
3e4093b6
RS
7604 constructor_range_stack->next = p;
7605 constructor_range_stack = p;
de520661 7606}
400fbf9f 7607
3e4093b6
RS
7608/* Within an array initializer, specify the next index to be initialized.
7609 FIRST is that index. If LAST is nonzero, then initialize a range
7610 of indices, running from FIRST through LAST. */
5a7ec9d9 7611
de520661 7612void
a1e3b3d9
LB
7613set_init_index (tree first, tree last,
7614 struct obstack * braced_init_obstack)
de520661 7615{
a1e3b3d9 7616 if (set_designator (1, braced_init_obstack))
3e4093b6 7617 return;
de520661 7618
b06df647 7619 designator_erroneous = 1;
de520661 7620
3ea8cd06
JM
7621 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7622 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7623 {
7624 error_init ("array index in initializer not of integer type");
7625 return;
7626 }
7627
2b6da65c
JM
7628 if (TREE_CODE (first) != INTEGER_CST)
7629 {
7630 first = c_fully_fold (first, false, NULL);
7631 if (TREE_CODE (first) == INTEGER_CST)
c1771a20 7632 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7633 "array index in initializer is not "
7634 "an integer constant expression");
7635 }
7636
7637 if (last && TREE_CODE (last) != INTEGER_CST)
7638 {
7639 last = c_fully_fold (last, false, NULL);
7640 if (TREE_CODE (last) == INTEGER_CST)
c1771a20 7641 pedwarn_init (input_location, OPT_Wpedantic,
2b6da65c
JM
7642 "array index in initializer is not "
7643 "an integer constant expression");
7644 }
7645
3e4093b6
RS
7646 if (TREE_CODE (first) != INTEGER_CST)
7647 error_init ("nonconstant array index in initializer");
7648 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
7649 error_init ("nonconstant array index in initializer");
7650 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
7651 error_init ("array index in non-array initializer");
622adc7e
MK
7652 else if (tree_int_cst_sgn (first) == -1)
7653 error_init ("array index in initializer exceeds array bounds");
3e4093b6
RS
7654 else if (constructor_max_index
7655 && tree_int_cst_lt (constructor_max_index, first))
7656 error_init ("array index in initializer exceeds array bounds");
7657 else
de520661 7658 {
928c19bb
JM
7659 constant_expression_warning (first);
7660 if (last)
7661 constant_expression_warning (last);
3e4093b6 7662 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7663 if (tree_int_cst_lt (constructor_index, first))
7664 {
7665 constructor_index = copy_node (constructor_index);
7666 TREE_OVERFLOW (constructor_index) = 1;
7667 }
665f2503 7668
3e4093b6 7669 if (last)
2bede729 7670 {
3e4093b6
RS
7671 if (tree_int_cst_equal (first, last))
7672 last = 0;
7673 else if (tree_int_cst_lt (last, first))
7674 {
7675 error_init ("empty index range in initializer");
7676 last = 0;
7677 }
7678 else
7679 {
7680 last = convert (bitsizetype, last);
7681 if (constructor_max_index != 0
7682 && tree_int_cst_lt (constructor_max_index, last))
7683 {
7684 error_init ("array index range in initializer exceeds array bounds");
7685 last = 0;
7686 }
7687 }
2bede729 7688 }
fed3cef0 7689
3e4093b6 7690 designator_depth++;
b06df647 7691 designator_erroneous = 0;
3e4093b6 7692 if (constructor_range_stack || last)
a1e3b3d9 7693 push_range_stack (last, braced_init_obstack);
de520661 7694 }
de520661 7695}
3e4093b6
RS
7696
7697/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7698
de520661 7699void
a1e3b3d9 7700set_init_label (tree fieldname, struct obstack * braced_init_obstack)
de520661 7701{
0fb96aa4 7702 tree field;
94ba5069 7703
a1e3b3d9 7704 if (set_designator (0, braced_init_obstack))
3e4093b6
RS
7705 return;
7706
b06df647 7707 designator_erroneous = 1;
3e4093b6
RS
7708
7709 if (TREE_CODE (constructor_type) != RECORD_TYPE
7710 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7711 {
3e4093b6
RS
7712 error_init ("field name not in record or union initializer");
7713 return;
94ba5069
RS
7714 }
7715
0fb96aa4 7716 field = lookup_field (constructor_type, fieldname);
8b6a5902 7717
0fb96aa4 7718 if (field == 0)
c51a1ba9 7719 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7720 else
0fb96aa4
JM
7721 do
7722 {
7723 constructor_fields = TREE_VALUE (field);
7724 designator_depth++;
7725 designator_erroneous = 0;
7726 if (constructor_range_stack)
7727 push_range_stack (NULL_TREE, braced_init_obstack);
7728 field = TREE_CHAIN (field);
7729 if (field)
7730 {
7731 if (set_designator (0, braced_init_obstack))
7732 return;
7733 }
7734 }
7735 while (field != NULL_TREE);
3e4093b6
RS
7736}
7737\f
7738/* Add a new initializer to the tree of pending initializers. PURPOSE
7739 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7740 VALUE is the value of that index or field. If ORIGTYPE is not
7741 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7742
7743 IMPLICIT is true if value comes from pop_init_level (1),
7744 the new initializer has been merged with the existing one
7745 and thus no warnings should be emitted about overriding an
7746 existing initializer. */
de520661 7747
3e4093b6 7748static void
a1e3b3d9
LB
7749add_pending_init (tree purpose, tree value, tree origtype, bool implicit,
7750 struct obstack * braced_init_obstack)
3e4093b6
RS
7751{
7752 struct init_node *p, **q, *r;
7753
7754 q = &constructor_pending_elts;
7755 p = 0;
7756
7757 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7758 {
3e4093b6 7759 while (*q != 0)
91fa3c30 7760 {
3e4093b6
RS
7761 p = *q;
7762 if (tree_int_cst_lt (purpose, p->purpose))
7763 q = &p->left;
7764 else if (tree_int_cst_lt (p->purpose, purpose))
7765 q = &p->right;
7766 else
7767 {
b295aee2
JJ
7768 if (!implicit)
7769 {
7770 if (TREE_SIDE_EFFECTS (p->value))
7771 warning_init (0, "initialized field with side-effects overwritten");
7772 else if (warn_override_init)
7773 warning_init (OPT_Woverride_init, "initialized field overwritten");
7774 }
3e4093b6 7775 p->value = value;
bbbbb16a 7776 p->origtype = origtype;
3e4093b6
RS
7777 return;
7778 }
91fa3c30 7779 }
de520661 7780 }
3e4093b6 7781 else
de520661 7782 {
3e4093b6 7783 tree bitpos;
400fbf9f 7784
3e4093b6
RS
7785 bitpos = bit_position (purpose);
7786 while (*q != NULL)
7787 {
7788 p = *q;
7789 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7790 q = &p->left;
7791 else if (p->purpose != purpose)
7792 q = &p->right;
7793 else
7794 {
b295aee2
JJ
7795 if (!implicit)
7796 {
7797 if (TREE_SIDE_EFFECTS (p->value))
7798 warning_init (0, "initialized field with side-effects overwritten");
7799 else if (warn_override_init)
7800 warning_init (OPT_Woverride_init, "initialized field overwritten");
7801 }
3e4093b6 7802 p->value = value;
bbbbb16a 7803 p->origtype = origtype;
3e4093b6
RS
7804 return;
7805 }
7806 }
91fa3c30 7807 }
b71c7f8a 7808
a1e3b3d9
LB
7809 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7810 sizeof (struct init_node));
3e4093b6
RS
7811 r->purpose = purpose;
7812 r->value = value;
bbbbb16a 7813 r->origtype = origtype;
8b6a5902 7814
3e4093b6
RS
7815 *q = r;
7816 r->parent = p;
7817 r->left = 0;
7818 r->right = 0;
7819 r->balance = 0;
b71c7f8a 7820
3e4093b6 7821 while (p)
de520661 7822 {
3e4093b6 7823 struct init_node *s;
665f2503 7824
3e4093b6 7825 if (r == p->left)
2bede729 7826 {
3e4093b6
RS
7827 if (p->balance == 0)
7828 p->balance = -1;
7829 else if (p->balance < 0)
7830 {
7831 if (r->balance < 0)
7832 {
7833 /* L rotation. */
7834 p->left = r->right;
7835 if (p->left)
7836 p->left->parent = p;
7837 r->right = p;
e7b6a0ee 7838
3e4093b6
RS
7839 p->balance = 0;
7840 r->balance = 0;
39bc99c2 7841
3e4093b6
RS
7842 s = p->parent;
7843 p->parent = r;
7844 r->parent = s;
7845 if (s)
7846 {
7847 if (s->left == p)
7848 s->left = r;
7849 else
7850 s->right = r;
7851 }
7852 else
7853 constructor_pending_elts = r;
7854 }
7855 else
7856 {
7857 /* LR rotation. */
7858 struct init_node *t = r->right;
e7b6a0ee 7859
3e4093b6
RS
7860 r->right = t->left;
7861 if (r->right)
7862 r->right->parent = r;
7863 t->left = r;
7864
7865 p->left = t->right;
7866 if (p->left)
7867 p->left->parent = p;
7868 t->right = p;
7869
7870 p->balance = t->balance < 0;
7871 r->balance = -(t->balance > 0);
7872 t->balance = 0;
7873
7874 s = p->parent;
7875 p->parent = t;
7876 r->parent = t;
7877 t->parent = s;
7878 if (s)
7879 {
7880 if (s->left == p)
7881 s->left = t;
7882 else
7883 s->right = t;
7884 }
7885 else
7886 constructor_pending_elts = t;
7887 }
7888 break;
7889 }
7890 else
7891 {
7892 /* p->balance == +1; growth of left side balances the node. */
7893 p->balance = 0;
7894 break;
7895 }
2bede729 7896 }
3e4093b6
RS
7897 else /* r == p->right */
7898 {
7899 if (p->balance == 0)
7900 /* Growth propagation from right side. */
7901 p->balance++;
7902 else if (p->balance > 0)
7903 {
7904 if (r->balance > 0)
7905 {
7906 /* R rotation. */
7907 p->right = r->left;
7908 if (p->right)
7909 p->right->parent = p;
7910 r->left = p;
7911
7912 p->balance = 0;
7913 r->balance = 0;
7914
7915 s = p->parent;
7916 p->parent = r;
7917 r->parent = s;
7918 if (s)
7919 {
7920 if (s->left == p)
7921 s->left = r;
7922 else
7923 s->right = r;
7924 }
7925 else
7926 constructor_pending_elts = r;
7927 }
7928 else /* r->balance == -1 */
7929 {
7930 /* RL rotation */
7931 struct init_node *t = r->left;
7932
7933 r->left = t->right;
7934 if (r->left)
7935 r->left->parent = r;
7936 t->right = r;
7937
7938 p->right = t->left;
7939 if (p->right)
7940 p->right->parent = p;
7941 t->left = p;
7942
7943 r->balance = (t->balance < 0);
7944 p->balance = -(t->balance > 0);
7945 t->balance = 0;
7946
7947 s = p->parent;
7948 p->parent = t;
7949 r->parent = t;
7950 t->parent = s;
7951 if (s)
7952 {
7953 if (s->left == p)
7954 s->left = t;
7955 else
7956 s->right = t;
7957 }
7958 else
7959 constructor_pending_elts = t;
7960 }
7961 break;
7962 }
7963 else
7964 {
7965 /* p->balance == -1; growth of right side balances the node. */
7966 p->balance = 0;
7967 break;
7968 }
7969 }
7970
7971 r = p;
7972 p = p->parent;
7973 }
7974}
7975
7976/* Build AVL tree from a sorted chain. */
7977
7978static void
a1e3b3d9 7979set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 7980{
4038c495
GB
7981 unsigned HOST_WIDE_INT ix;
7982 tree index, value;
3e4093b6
RS
7983
7984 if (TREE_CODE (constructor_type) != RECORD_TYPE
7985 && TREE_CODE (constructor_type) != ARRAY_TYPE)
7986 return;
7987
4038c495 7988 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
a1e3b3d9 7989 {
fdce1719 7990 add_pending_init (index, value, NULL_TREE, true,
a1e3b3d9
LB
7991 braced_init_obstack);
7992 }
9771b263 7993 constructor_elements = NULL;
3e4093b6
RS
7994 if (TREE_CODE (constructor_type) == RECORD_TYPE)
7995 {
7996 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
7997 /* Skip any nameless bit fields at the beginning. */
7998 while (constructor_unfilled_fields != 0
7999 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8000 && DECL_NAME (constructor_unfilled_fields) == 0)
8001 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8002
de520661 8003 }
3e4093b6 8004 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8005 {
3e4093b6
RS
8006 if (TYPE_DOMAIN (constructor_type))
8007 constructor_unfilled_index
8008 = convert (bitsizetype,
8009 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8010 else
8011 constructor_unfilled_index = bitsize_zero_node;
de520661 8012 }
3e4093b6 8013 constructor_incremental = 0;
de520661 8014}
400fbf9f 8015
3e4093b6 8016/* Build AVL tree from a string constant. */
de520661 8017
3e4093b6 8018static void
a1e3b3d9
LB
8019set_nonincremental_init_from_string (tree str,
8020 struct obstack * braced_init_obstack)
de520661 8021{
3e4093b6
RS
8022 tree value, purpose, type;
8023 HOST_WIDE_INT val[2];
8024 const char *p, *end;
8025 int byte, wchar_bytes, charwidth, bitpos;
de520661 8026
366de0ce 8027 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8028
c466b2cd 8029 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8030 charwidth = TYPE_PRECISION (char_type_node);
8031 type = TREE_TYPE (constructor_type);
8032 p = TREE_STRING_POINTER (str);
8033 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8034
3e4093b6 8035 for (purpose = bitsize_zero_node;
8824edff
JJ
8036 p < end
8037 && !(constructor_max_index
8038 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8039 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8040 {
3e4093b6 8041 if (wchar_bytes == 1)
ffc5c6a9 8042 {
3e4093b6
RS
8043 val[1] = (unsigned char) *p++;
8044 val[0] = 0;
ffc5c6a9
RH
8045 }
8046 else
3e4093b6
RS
8047 {
8048 val[0] = 0;
8049 val[1] = 0;
8050 for (byte = 0; byte < wchar_bytes; byte++)
8051 {
8052 if (BYTES_BIG_ENDIAN)
8053 bitpos = (wchar_bytes - byte - 1) * charwidth;
8054 else
8055 bitpos = byte * charwidth;
8056 val[bitpos < HOST_BITS_PER_WIDE_INT]
8057 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8058 << (bitpos % HOST_BITS_PER_WIDE_INT);
8059 }
8060 }
584ef5fe 8061
8df83eae 8062 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8063 {
8064 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8065 if (bitpos < HOST_BITS_PER_WIDE_INT)
8066 {
8067 if (val[1] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
8068 {
8069 val[1] |= ((HOST_WIDE_INT) -1) << bitpos;
8070 val[0] = -1;
8071 }
8072 }
8073 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8074 {
8075 if (val[1] < 0)
c22cacf3 8076 val[0] = -1;
3e4093b6
RS
8077 }
8078 else if (val[0] & (((HOST_WIDE_INT) 1)
8079 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
8080 val[0] |= ((HOST_WIDE_INT) -1)
8081 << (bitpos - HOST_BITS_PER_WIDE_INT);
8082 }
ffc5c6a9 8083
7d60be94 8084 value = build_int_cst_wide (type, val[1], val[0]);
fdce1719 8085 add_pending_init (purpose, value, NULL_TREE, true,
a1e3b3d9 8086 braced_init_obstack);
9dfcc8db
BH
8087 }
8088
3e4093b6
RS
8089 constructor_incremental = 0;
8090}
de520661 8091
3e4093b6
RS
8092/* Return value of FIELD in pending initializer or zero if the field was
8093 not initialized yet. */
8094
8095static tree
a1e3b3d9 8096find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8097{
8098 struct init_node *p;
8099
8100 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8101 {
3e4093b6
RS
8102 if (constructor_incremental
8103 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8104 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8105
8106 p = constructor_pending_elts;
8107 while (p)
19d76e60 8108 {
3e4093b6
RS
8109 if (tree_int_cst_lt (field, p->purpose))
8110 p = p->left;
8111 else if (tree_int_cst_lt (p->purpose, field))
8112 p = p->right;
8113 else
8114 return p->value;
19d76e60 8115 }
19d76e60 8116 }
3e4093b6 8117 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8118 {
3e4093b6 8119 tree bitpos = bit_position (field);
de520661 8120
3e4093b6
RS
8121 if (constructor_incremental
8122 && (!constructor_unfilled_fields
8123 || tree_int_cst_lt (bitpos,
8124 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8125 set_nonincremental_init (braced_init_obstack);
de520661 8126
3e4093b6
RS
8127 p = constructor_pending_elts;
8128 while (p)
8129 {
8130 if (field == p->purpose)
8131 return p->value;
8132 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8133 p = p->left;
8134 else
8135 p = p->right;
8136 }
8137 }
8138 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8139 {
9771b263
DN
8140 if (!vec_safe_is_empty (constructor_elements)
8141 && (constructor_elements->last ().index == field))
8142 return constructor_elements->last ().value;
de520661 8143 }
3e4093b6 8144 return 0;
de520661
RS
8145}
8146
3e4093b6
RS
8147/* "Output" the next constructor element.
8148 At top level, really output it to assembler code now.
8149 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8150 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8151 TYPE is the data type that the containing data type wants here.
8152 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8153 If VALUE is a string constant, STRICT_STRING is true if it is
8154 unparenthesized or we should not warn here for it being parenthesized.
8155 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8156
3e4093b6
RS
8157 PENDING if non-nil means output pending elements that belong
8158 right after this element. (PENDING is normally 1;
b295aee2
JJ
8159 it is 0 while outputting pending elements, to avoid recursion.)
8160
8161 IMPLICIT is true if value comes from pop_init_level (1),
8162 the new initializer has been merged with the existing one
8163 and thus no warnings should be emitted about overriding an
8164 existing initializer. */
8b6a5902 8165
3e4093b6 8166static void
bbbbb16a 8167output_init_element (tree value, tree origtype, bool strict_string, tree type,
a1e3b3d9
LB
8168 tree field, int pending, bool implicit,
8169 struct obstack * braced_init_obstack)
3e4093b6 8170{
8ce94e44 8171 tree semantic_type = NULL_TREE;
928c19bb
JM
8172 bool maybe_const = true;
8173 bool npc;
4038c495 8174
0a880880 8175 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8176 {
3e4093b6
RS
8177 constructor_erroneous = 1;
8178 return;
8b6a5902 8179 }
46bdb9cf
JM
8180 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8181 && (TREE_CODE (value) == STRING_CST
8182 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8183 && !(TREE_CODE (value) == STRING_CST
8184 && TREE_CODE (type) == ARRAY_TYPE
8185 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8186 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8187 TYPE_MAIN_VARIANT (type)))
c2255bc4 8188 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8189
3e4093b6
RS
8190 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8191 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 8192 {
3e4093b6
RS
8193 /* As an extension, allow initializing objects with static storage
8194 duration with compound literals (which are then treated just as
8195 the brace enclosed list they contain). */
8196 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8197 value = DECL_INITIAL (decl);
8b6a5902
JJ
8198 }
8199
928c19bb 8200 npc = null_pointer_constant_p (value);
8ce94e44
JM
8201 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8202 {
8203 semantic_type = TREE_TYPE (value);
8204 value = TREE_OPERAND (value, 0);
8205 }
928c19bb
JM
8206 value = c_fully_fold (value, require_constant_value, &maybe_const);
8207
3e4093b6
RS
8208 if (value == error_mark_node)
8209 constructor_erroneous = 1;
8210 else if (!TREE_CONSTANT (value))
8211 constructor_constant = 0;
116df786 8212 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8213 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8214 || TREE_CODE (constructor_type) == UNION_TYPE)
8215 && DECL_C_BIT_FIELD (field)
8216 && TREE_CODE (value) != INTEGER_CST))
8217 constructor_simple = 0;
928c19bb
JM
8218 if (!maybe_const)
8219 constructor_nonconst = 1;
3e4093b6 8220
116df786 8221 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8222 {
116df786
RH
8223 if (require_constant_value)
8224 {
8225 error_init ("initializer element is not constant");
8226 value = error_mark_node;
8227 }
8228 else if (require_constant_elements)
509c9d60
MLI
8229 pedwarn (input_location, 0,
8230 "initializer element is not computable at load time");
8b6a5902 8231 }
928c19bb
JM
8232 else if (!maybe_const
8233 && (require_constant_value || require_constant_elements))
8234 pedwarn_init (input_location, 0,
8235 "initializer element is not a constant expression");
3e4093b6 8236
81f40b79
ILT
8237 /* Issue -Wc++-compat warnings about initializing a bitfield with
8238 enum type. */
8239 if (warn_cxx_compat
8240 && field != NULL_TREE
8241 && TREE_CODE (field) == FIELD_DECL
8242 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8243 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8244 != TYPE_MAIN_VARIANT (type))
8245 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8246 {
8247 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8248 if (checktype != error_mark_node
8249 && (TYPE_MAIN_VARIANT (checktype)
8250 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
8251 warning_init (OPT_Wc___compat,
8252 "enum conversion in initialization is invalid in C++");
8253 }
8254
3e4093b6
RS
8255 /* If this field is empty (and not at the end of structure),
8256 don't do anything other than checking the initializer. */
8257 if (field
8258 && (TREE_TYPE (field) == error_mark_node
8259 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8260 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8261 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8262 || DECL_CHAIN (field)))))
3e4093b6
RS
8263 return;
8264
8ce94e44
JM
8265 if (semantic_type)
8266 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
c2255bc4
AH
8267 value = digest_init (input_location, type, value, origtype, npc,
8268 strict_string, require_constant_value);
3e4093b6 8269 if (value == error_mark_node)
8b6a5902 8270 {
3e4093b6
RS
8271 constructor_erroneous = 1;
8272 return;
8b6a5902 8273 }
928c19bb
JM
8274 if (require_constant_value || require_constant_elements)
8275 constant_expression_warning (value);
8b6a5902 8276
3e4093b6
RS
8277 /* If this element doesn't come next in sequence,
8278 put it on constructor_pending_elts. */
8279 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8280 && (!constructor_incremental
8281 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8282 {
3e4093b6
RS
8283 if (constructor_incremental
8284 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8285 set_nonincremental_init (braced_init_obstack);
3e4093b6 8286
a1e3b3d9
LB
8287 add_pending_init (field, value, origtype, implicit,
8288 braced_init_obstack);
3e4093b6 8289 return;
8b6a5902 8290 }
3e4093b6
RS
8291 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8292 && (!constructor_incremental
8293 || field != constructor_unfilled_fields))
8b6a5902 8294 {
3e4093b6
RS
8295 /* We do this for records but not for unions. In a union,
8296 no matter which field is specified, it can be initialized
8297 right away since it starts at the beginning of the union. */
8298 if (constructor_incremental)
8299 {
8300 if (!constructor_unfilled_fields)
a1e3b3d9 8301 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8302 else
8303 {
8304 tree bitpos, unfillpos;
8305
8306 bitpos = bit_position (field);
8307 unfillpos = bit_position (constructor_unfilled_fields);
8308
8309 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8310 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8311 }
8312 }
8313
a1e3b3d9
LB
8314 add_pending_init (field, value, origtype, implicit,
8315 braced_init_obstack);
3e4093b6 8316 return;
8b6a5902 8317 }
3e4093b6 8318 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8319 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8320 {
b295aee2
JJ
8321 if (!implicit)
8322 {
9771b263 8323 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
b295aee2
JJ
8324 warning_init (0,
8325 "initialized field with side-effects overwritten");
8326 else if (warn_override_init)
8327 warning_init (OPT_Woverride_init, "initialized field overwritten");
8328 }
8b6a5902 8329
3e4093b6 8330 /* We can have just one union field set. */
9771b263 8331 constructor_elements = NULL;
3e4093b6 8332 }
8b6a5902 8333
3e4093b6
RS
8334 /* Otherwise, output this element either to
8335 constructor_elements or to the assembler file. */
8b6a5902 8336
f32682ca 8337 constructor_elt celt = {field, value};
9771b263 8338 vec_safe_push (constructor_elements, celt);
8b6a5902 8339
3e4093b6
RS
8340 /* Advance the variable that indicates sequential elements output. */
8341 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8342 constructor_unfilled_index
db3927fb
AH
8343 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8344 bitsize_one_node);
3e4093b6
RS
8345 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8346 {
8347 constructor_unfilled_fields
910ad8de 8348 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8349
3e4093b6
RS
8350 /* Skip any nameless bit fields. */
8351 while (constructor_unfilled_fields != 0
8352 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8353 && DECL_NAME (constructor_unfilled_fields) == 0)
8354 constructor_unfilled_fields =
910ad8de 8355 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8356 }
8357 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8358 constructor_unfilled_fields = 0;
de520661 8359
3e4093b6
RS
8360 /* Now output any pending elements which have become next. */
8361 if (pending)
a1e3b3d9 8362 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8363}
8b6a5902 8364
3e4093b6
RS
8365/* Output any pending elements which have become next.
8366 As we output elements, constructor_unfilled_{fields,index}
8367 advances, which may cause other elements to become next;
8368 if so, they too are output.
8b6a5902 8369
3e4093b6
RS
8370 If ALL is 0, we return when there are
8371 no more pending elements to output now.
665f2503 8372
3e4093b6
RS
8373 If ALL is 1, we output space as necessary so that
8374 we can output all the pending elements. */
3e4093b6 8375static void
a1e3b3d9 8376output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8377{
8378 struct init_node *elt = constructor_pending_elts;
8379 tree next;
de520661 8380
3e4093b6
RS
8381 retry:
8382
ba228239 8383 /* Look through the whole pending tree.
3e4093b6
RS
8384 If we find an element that should be output now,
8385 output it. Otherwise, set NEXT to the element
8386 that comes first among those still pending. */
8387
8388 next = 0;
8389 while (elt)
8390 {
8391 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8392 {
3e4093b6
RS
8393 if (tree_int_cst_equal (elt->purpose,
8394 constructor_unfilled_index))
bbbbb16a 8395 output_init_element (elt->value, elt->origtype, true,
3e4093b6 8396 TREE_TYPE (constructor_type),
a1e3b3d9
LB
8397 constructor_unfilled_index, 0, false,
8398 braced_init_obstack);
3e4093b6
RS
8399 else if (tree_int_cst_lt (constructor_unfilled_index,
8400 elt->purpose))
8b6a5902 8401 {
3e4093b6
RS
8402 /* Advance to the next smaller node. */
8403 if (elt->left)
8404 elt = elt->left;
8405 else
8406 {
8407 /* We have reached the smallest node bigger than the
8408 current unfilled index. Fill the space first. */
8409 next = elt->purpose;
8410 break;
8411 }
8b6a5902 8412 }
ce662d4c
JJ
8413 else
8414 {
3e4093b6
RS
8415 /* Advance to the next bigger node. */
8416 if (elt->right)
8417 elt = elt->right;
8418 else
ce662d4c 8419 {
3e4093b6
RS
8420 /* We have reached the biggest node in a subtree. Find
8421 the parent of it, which is the next bigger node. */
8422 while (elt->parent && elt->parent->right == elt)
8423 elt = elt->parent;
8424 elt = elt->parent;
8425 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8426 elt->purpose))
8427 {
8428 next = elt->purpose;
8429 break;
8430 }
ce662d4c
JJ
8431 }
8432 }
8b6a5902 8433 }
3e4093b6
RS
8434 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8435 || TREE_CODE (constructor_type) == UNION_TYPE)
8436 {
8437 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8438
3e4093b6
RS
8439 /* If the current record is complete we are done. */
8440 if (constructor_unfilled_fields == 0)
8441 break;
de520661 8442
3e4093b6
RS
8443 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8444 elt_bitpos = bit_position (elt->purpose);
8445 /* We can't compare fields here because there might be empty
8446 fields in between. */
8447 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8448 {
8449 constructor_unfilled_fields = elt->purpose;
bbbbb16a
ILT
8450 output_init_element (elt->value, elt->origtype, true,
8451 TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8452 elt->purpose, 0, false,
8453 braced_init_obstack);
3e4093b6
RS
8454 }
8455 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8456 {
8457 /* Advance to the next smaller node. */
8458 if (elt->left)
8459 elt = elt->left;
8460 else
8461 {
8462 /* We have reached the smallest node bigger than the
8463 current unfilled field. Fill the space first. */
8464 next = elt->purpose;
8465 break;
8466 }
8467 }
8468 else
8469 {
8470 /* Advance to the next bigger node. */
8471 if (elt->right)
8472 elt = elt->right;
8473 else
8474 {
8475 /* We have reached the biggest node in a subtree. Find
8476 the parent of it, which is the next bigger node. */
8477 while (elt->parent && elt->parent->right == elt)
8478 elt = elt->parent;
8479 elt = elt->parent;
8480 if (elt
8481 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8482 bit_position (elt->purpose))))
8483 {
8484 next = elt->purpose;
8485 break;
8486 }
8487 }
8488 }
8489 }
8490 }
de520661 8491
3e4093b6
RS
8492 /* Ordinarily return, but not if we want to output all
8493 and there are elements left. */
3f75a254 8494 if (!(all && next != 0))
e5cfb88f
RK
8495 return;
8496
3e4093b6
RS
8497 /* If it's not incremental, just skip over the gap, so that after
8498 jumping to retry we will output the next successive element. */
8499 if (TREE_CODE (constructor_type) == RECORD_TYPE
8500 || TREE_CODE (constructor_type) == UNION_TYPE)
8501 constructor_unfilled_fields = next;
8502 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8503 constructor_unfilled_index = next;
de520661 8504
3e4093b6
RS
8505 /* ELT now points to the node in the pending tree with the next
8506 initializer to output. */
8507 goto retry;
de520661
RS
8508}
8509\f
3e4093b6
RS
8510/* Add one non-braced element to the current constructor level.
8511 This adjusts the current position within the constructor's type.
8512 This may also start or terminate implicit levels
8513 to handle a partly-braced initializer.
e5e809f4 8514
3e4093b6 8515 Once this has found the correct level for the new element,
b295aee2
JJ
8516 it calls output_init_element.
8517
8518 IMPLICIT is true if value comes from pop_init_level (1),
8519 the new initializer has been merged with the existing one
8520 and thus no warnings should be emitted about overriding an
8521 existing initializer. */
3e4093b6
RS
8522
8523void
a1e3b3d9
LB
8524process_init_element (struct c_expr value, bool implicit,
8525 struct obstack * braced_init_obstack)
e5e809f4 8526{
916c5919
JM
8527 tree orig_value = value.value;
8528 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8529 bool strict_string = value.original_code == STRING_CST;
340baef7 8530 bool was_designated = designator_depth != 0;
e5e809f4 8531
3e4093b6 8532 designator_depth = 0;
b06df647 8533 designator_erroneous = 0;
e5e809f4 8534
3e4093b6
RS
8535 /* Handle superfluous braces around string cst as in
8536 char x[] = {"foo"}; */
8537 if (string_flag
8538 && constructor_type
340baef7 8539 && !was_designated
3e4093b6 8540 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8541 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8542 && integer_zerop (constructor_unfilled_index))
e5e809f4 8543 {
916c5919 8544 if (constructor_stack->replacement_value.value)
c22cacf3 8545 error_init ("excess elements in char array initializer");
3e4093b6
RS
8546 constructor_stack->replacement_value = value;
8547 return;
e5e809f4 8548 }
8b6a5902 8549
916c5919 8550 if (constructor_stack->replacement_value.value != 0)
3e4093b6
RS
8551 {
8552 error_init ("excess elements in struct initializer");
8553 return;
e5e809f4
JL
8554 }
8555
3e4093b6
RS
8556 /* Ignore elements of a brace group if it is entirely superfluous
8557 and has already been diagnosed. */
8558 if (constructor_type == 0)
8559 return;
e5e809f4 8560
3e4093b6
RS
8561 /* If we've exhausted any levels that didn't have braces,
8562 pop them now. */
8563 while (constructor_stack->implicit)
8564 {
8565 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8566 || TREE_CODE (constructor_type) == UNION_TYPE)
8567 && constructor_fields == 0)
a1e3b3d9
LB
8568 process_init_element (pop_init_level (1, braced_init_obstack),
8569 true, braced_init_obstack);
53650abe
AP
8570 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8571 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8572 && constructor_max_index
8573 && tree_int_cst_lt (constructor_max_index,
8574 constructor_index))
a1e3b3d9
LB
8575 process_init_element (pop_init_level (1, braced_init_obstack),
8576 true, braced_init_obstack);
3e4093b6
RS
8577 else
8578 break;
8579 }
e5e809f4 8580
3e4093b6
RS
8581 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8582 if (constructor_range_stack)
e5e809f4 8583 {
3e4093b6
RS
8584 /* If value is a compound literal and we'll be just using its
8585 content, don't put it into a SAVE_EXPR. */
916c5919 8586 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8587 || !require_constant_value
8588 || flag_isoc99)
8ce94e44
JM
8589 {
8590 tree semantic_type = NULL_TREE;
8591 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8592 {
8593 semantic_type = TREE_TYPE (value.value);
8594 value.value = TREE_OPERAND (value.value, 0);
8595 }
8596 value.value = c_save_expr (value.value);
8597 if (semantic_type)
8598 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8599 value.value);
8600 }
3e4093b6 8601 }
e5e809f4 8602
3e4093b6
RS
8603 while (1)
8604 {
8605 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8606 {
3e4093b6
RS
8607 tree fieldtype;
8608 enum tree_code fieldcode;
e5e809f4 8609
3e4093b6
RS
8610 if (constructor_fields == 0)
8611 {
509c9d60
MLI
8612 pedwarn_init (input_location, 0,
8613 "excess elements in struct initializer");
3e4093b6
RS
8614 break;
8615 }
e5e809f4 8616
3e4093b6
RS
8617 fieldtype = TREE_TYPE (constructor_fields);
8618 if (fieldtype != error_mark_node)
8619 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8620 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8621
3e4093b6
RS
8622 /* Error for non-static initialization of a flexible array member. */
8623 if (fieldcode == ARRAY_TYPE
8624 && !require_constant_value
8625 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8626 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6
RS
8627 {
8628 error_init ("non-static initialization of a flexible array member");
8629 break;
8630 }
e5e809f4 8631
3e4093b6 8632 /* Accept a string constant to initialize a subarray. */
916c5919 8633 if (value.value != 0
3e4093b6 8634 && fieldcode == ARRAY_TYPE
197463ae 8635 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8636 && string_flag)
916c5919 8637 value.value = orig_value;
3e4093b6
RS
8638 /* Otherwise, if we have come to a subaggregate,
8639 and we don't have an element of its type, push into it. */
0953878d 8640 else if (value.value != 0
916c5919
JM
8641 && value.value != error_mark_node
8642 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8643 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8644 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8645 {
a1e3b3d9 8646 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8647 continue;
8648 }
e5e809f4 8649
916c5919 8650 if (value.value)
3e4093b6
RS
8651 {
8652 push_member_name (constructor_fields);
bbbbb16a
ILT
8653 output_init_element (value.value, value.original_type,
8654 strict_string, fieldtype,
a1e3b3d9
LB
8655 constructor_fields, 1, implicit,
8656 braced_init_obstack);
3e4093b6 8657 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8658 }
8659 else
3e4093b6
RS
8660 /* Do the bookkeeping for an element that was
8661 directly output as a constructor. */
e5e809f4 8662 {
3e4093b6
RS
8663 /* For a record, keep track of end position of last field. */
8664 if (DECL_SIZE (constructor_fields))
c22cacf3 8665 constructor_bit_index
db3927fb
AH
8666 = size_binop_loc (input_location, PLUS_EXPR,
8667 bit_position (constructor_fields),
8668 DECL_SIZE (constructor_fields));
3e4093b6
RS
8669
8670 /* If the current field was the first one not yet written out,
8671 it isn't now, so update. */
8672 if (constructor_unfilled_fields == constructor_fields)
8673 {
910ad8de 8674 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8675 /* Skip any nameless bit fields. */
8676 while (constructor_unfilled_fields != 0
8677 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8678 && DECL_NAME (constructor_unfilled_fields) == 0)
8679 constructor_unfilled_fields =
910ad8de 8680 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8681 }
e5e809f4 8682 }
3e4093b6 8683
910ad8de 8684 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8685 /* Skip any nameless bit fields at the beginning. */
8686 while (constructor_fields != 0
8687 && DECL_C_BIT_FIELD (constructor_fields)
8688 && DECL_NAME (constructor_fields) == 0)
910ad8de 8689 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8690 }
3e4093b6 8691 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8692 {
3e4093b6
RS
8693 tree fieldtype;
8694 enum tree_code fieldcode;
e5e809f4 8695
3e4093b6
RS
8696 if (constructor_fields == 0)
8697 {
509c9d60
MLI
8698 pedwarn_init (input_location, 0,
8699 "excess elements in union initializer");
3e4093b6
RS
8700 break;
8701 }
e5e809f4 8702
3e4093b6
RS
8703 fieldtype = TREE_TYPE (constructor_fields);
8704 if (fieldtype != error_mark_node)
8705 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8706 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8707
3e4093b6
RS
8708 /* Warn that traditional C rejects initialization of unions.
8709 We skip the warning if the value is zero. This is done
8710 under the assumption that the zero initializer in user
8711 code appears conditioned on e.g. __STDC__ to avoid
8712 "missing initializer" warnings and relies on default
8713 initialization to zero in the traditional C case.
8714 We also skip the warning if the initializer is designated,
8715 again on the assumption that this must be conditional on
8716 __STDC__ anyway (and we've already complained about the
8717 member-designator already). */
8400e75e 8718 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8719 && !(value.value && (integer_zerop (value.value)
8720 || real_zerop (value.value))))
3176a0c2
DD
8721 warning (OPT_Wtraditional, "traditional C rejects initialization "
8722 "of unions");
e5e809f4 8723
3e4093b6 8724 /* Accept a string constant to initialize a subarray. */
916c5919 8725 if (value.value != 0
3e4093b6 8726 && fieldcode == ARRAY_TYPE
197463ae 8727 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8728 && string_flag)
916c5919 8729 value.value = orig_value;
3e4093b6
RS
8730 /* Otherwise, if we have come to a subaggregate,
8731 and we don't have an element of its type, push into it. */
0953878d 8732 else if (value.value != 0
916c5919
JM
8733 && value.value != error_mark_node
8734 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8735 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8736 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8737 {
a1e3b3d9 8738 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8739 continue;
8740 }
e5e809f4 8741
916c5919 8742 if (value.value)
3e4093b6
RS
8743 {
8744 push_member_name (constructor_fields);
bbbbb16a
ILT
8745 output_init_element (value.value, value.original_type,
8746 strict_string, fieldtype,
a1e3b3d9
LB
8747 constructor_fields, 1, implicit,
8748 braced_init_obstack);
3e4093b6 8749 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8750 }
8751 else
3e4093b6
RS
8752 /* Do the bookkeeping for an element that was
8753 directly output as a constructor. */
e5e809f4 8754 {
3e4093b6 8755 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8756 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8757 }
e5e809f4 8758
3e4093b6
RS
8759 constructor_fields = 0;
8760 }
8761 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8762 {
8763 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8764 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8765
3e4093b6 8766 /* Accept a string constant to initialize a subarray. */
916c5919 8767 if (value.value != 0
3e4093b6 8768 && eltcode == ARRAY_TYPE
197463ae 8769 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8770 && string_flag)
916c5919 8771 value.value = orig_value;
3e4093b6
RS
8772 /* Otherwise, if we have come to a subaggregate,
8773 and we don't have an element of its type, push into it. */
0953878d 8774 else if (value.value != 0
916c5919
JM
8775 && value.value != error_mark_node
8776 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8777 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8778 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8779 {
a1e3b3d9 8780 push_init_level (1, braced_init_obstack);
3e4093b6
RS
8781 continue;
8782 }
8b6a5902 8783
3e4093b6
RS
8784 if (constructor_max_index != 0
8785 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8786 || integer_all_onesp (constructor_max_index)))
8787 {
509c9d60
MLI
8788 pedwarn_init (input_location, 0,
8789 "excess elements in array initializer");
3e4093b6
RS
8790 break;
8791 }
8b6a5902 8792
3e4093b6 8793 /* Now output the actual element. */
916c5919 8794 if (value.value)
3e4093b6 8795 {
ae7e9ddd 8796 push_array_bounds (tree_to_uhwi (constructor_index));
bbbbb16a
ILT
8797 output_init_element (value.value, value.original_type,
8798 strict_string, elttype,
a1e3b3d9
LB
8799 constructor_index, 1, implicit,
8800 braced_init_obstack);
3e4093b6
RS
8801 RESTORE_SPELLING_DEPTH (constructor_depth);
8802 }
2f6e4e97 8803
3e4093b6 8804 constructor_index
db3927fb
AH
8805 = size_binop_loc (input_location, PLUS_EXPR,
8806 constructor_index, bitsize_one_node);
8b6a5902 8807
916c5919 8808 if (!value.value)
3e4093b6
RS
8809 /* If we are doing the bookkeeping for an element that was
8810 directly output as a constructor, we must update
8811 constructor_unfilled_index. */
8812 constructor_unfilled_index = constructor_index;
8813 }
8814 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8815 {
8816 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8817
c22cacf3
MS
8818 /* Do a basic check of initializer size. Note that vectors
8819 always have a fixed size derived from their type. */
3e4093b6
RS
8820 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8821 {
509c9d60
MLI
8822 pedwarn_init (input_location, 0,
8823 "excess elements in vector initializer");
3e4093b6
RS
8824 break;
8825 }
8b6a5902 8826
3e4093b6 8827 /* Now output the actual element. */
916c5919 8828 if (value.value)
53650abe
AP
8829 {
8830 if (TREE_CODE (value.value) == VECTOR_CST)
8831 elttype = TYPE_MAIN_VARIANT (constructor_type);
8832 output_init_element (value.value, value.original_type,
8833 strict_string, elttype,
a1e3b3d9
LB
8834 constructor_index, 1, implicit,
8835 braced_init_obstack);
53650abe 8836 }
8b6a5902 8837
3e4093b6 8838 constructor_index
db3927fb
AH
8839 = size_binop_loc (input_location,
8840 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8841
916c5919 8842 if (!value.value)
3e4093b6
RS
8843 /* If we are doing the bookkeeping for an element that was
8844 directly output as a constructor, we must update
8845 constructor_unfilled_index. */
8846 constructor_unfilled_index = constructor_index;
8847 }
8b6a5902 8848
3e4093b6
RS
8849 /* Handle the sole element allowed in a braced initializer
8850 for a scalar variable. */
b4519d39
SB
8851 else if (constructor_type != error_mark_node
8852 && constructor_fields == 0)
8b6a5902 8853 {
509c9d60
MLI
8854 pedwarn_init (input_location, 0,
8855 "excess elements in scalar initializer");
3e4093b6 8856 break;
8b6a5902
JJ
8857 }
8858 else
8859 {
916c5919 8860 if (value.value)
bbbbb16a
ILT
8861 output_init_element (value.value, value.original_type,
8862 strict_string, constructor_type,
a1e3b3d9
LB
8863 NULL_TREE, 1, implicit,
8864 braced_init_obstack);
3e4093b6 8865 constructor_fields = 0;
8b6a5902
JJ
8866 }
8867
3e4093b6
RS
8868 /* Handle range initializers either at this level or anywhere higher
8869 in the designator stack. */
8870 if (constructor_range_stack)
8b6a5902 8871 {
3e4093b6
RS
8872 struct constructor_range_stack *p, *range_stack;
8873 int finish = 0;
8874
8875 range_stack = constructor_range_stack;
8876 constructor_range_stack = 0;
8877 while (constructor_stack != range_stack->stack)
8b6a5902 8878 {
366de0ce 8879 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8880 process_init_element (pop_init_level (1,
8881 braced_init_obstack),
8882 true, braced_init_obstack);
8b6a5902 8883 }
3e4093b6
RS
8884 for (p = range_stack;
8885 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8886 p = p->prev)
8b6a5902 8887 {
366de0ce 8888 gcc_assert (constructor_stack->implicit);
a1e3b3d9
LB
8889 process_init_element (pop_init_level (1, braced_init_obstack),
8890 true, braced_init_obstack);
8b6a5902 8891 }
3e4093b6 8892
db3927fb
AH
8893 p->index = size_binop_loc (input_location,
8894 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8895 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8896 finish = 1;
8897
8898 while (1)
8899 {
8900 constructor_index = p->index;
8901 constructor_fields = p->fields;
8902 if (finish && p->range_end && p->index == p->range_start)
8903 {
8904 finish = 0;
8905 p->prev = 0;
8906 }
8907 p = p->next;
8908 if (!p)
8909 break;
a1e3b3d9 8910 push_init_level (2, braced_init_obstack);
3e4093b6
RS
8911 p->stack = constructor_stack;
8912 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8913 p->index = p->range_start;
8914 }
8915
8916 if (!finish)
8917 constructor_range_stack = range_stack;
8918 continue;
8b6a5902
JJ
8919 }
8920
3e4093b6 8921 break;
8b6a5902
JJ
8922 }
8923
3e4093b6
RS
8924 constructor_range_stack = 0;
8925}
8926\f
9f0e2d86
ZW
8927/* Build a complete asm-statement, whose components are a CV_QUALIFIER
8928 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 8929 an ASM_EXPR node). */
3e4093b6 8930tree
9f0e2d86 8931build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 8932{
6de9cd9a
DN
8933 if (!ASM_VOLATILE_P (args) && cv_qualifier)
8934 ASM_VOLATILE_P (args) = 1;
9f0e2d86 8935 return add_stmt (args);
8b6a5902
JJ
8936}
8937
9f0e2d86
ZW
8938/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
8939 some INPUTS, and some CLOBBERS. The latter three may be NULL.
8940 SIMPLE indicates whether there was anything at all after the
8941 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 8942 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 8943tree
c2255bc4 8944build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 8945 tree clobbers, tree labels, bool simple)
e5e809f4 8946{
3e4093b6 8947 tree tail;
9f0e2d86 8948 tree args;
6de9cd9a
DN
8949 int i;
8950 const char *constraint;
74f0c611 8951 const char **oconstraints;
6de9cd9a 8952 bool allows_mem, allows_reg, is_inout;
74f0c611 8953 int ninputs, noutputs;
6de9cd9a
DN
8954
8955 ninputs = list_length (inputs);
8956 noutputs = list_length (outputs);
74f0c611
RH
8957 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
8958
1c384bf1 8959 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 8960
6de9cd9a
DN
8961 /* Remove output conversions that change the type but not the mode. */
8962 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 8963 {
3e4093b6 8964 tree output = TREE_VALUE (tail);
74f0c611 8965
eadd3d0d
JJ
8966 output = c_fully_fold (output, false, NULL);
8967
74f0c611
RH
8968 /* ??? Really, this should not be here. Users should be using a
8969 proper lvalue, dammit. But there's a long history of using casts
8970 in the output operands. In cases like longlong.h, this becomes a
8971 primitive form of typechecking -- if the cast can be removed, then
8972 the output operand had a type of the proper width; otherwise we'll
8973 get an error. Gross, but ... */
3e4093b6 8974 STRIP_NOPS (output);
74f0c611 8975
7bd11157 8976 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 8977 output = error_mark_node;
8b6a5902 8978
5544530a
PB
8979 if (output != error_mark_node
8980 && (TREE_READONLY (output)
8981 || TYPE_READONLY (TREE_TYPE (output))
8982 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
8983 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
8984 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 8985 readonly_error (loc, output, lv_asm);
5544530a 8986
6de9cd9a 8987 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
8988 oconstraints[i] = constraint;
8989
8990 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
8991 &allows_mem, &allows_reg, &is_inout))
8992 {
8993 /* If the operand is going to end up in memory,
8994 mark it addressable. */
8995 if (!allows_reg && !c_mark_addressable (output))
8996 output = error_mark_node;
bae5cddf
JJ
8997 if (!(!allows_reg && allows_mem)
8998 && output != error_mark_node
8999 && VOID_TYPE_P (TREE_TYPE (output)))
9000 {
9001 error_at (loc, "invalid use of void expression");
9002 output = error_mark_node;
9003 }
74f0c611
RH
9004 }
9005 else
c22cacf3 9006 output = error_mark_node;
3e4093b6 9007
74f0c611 9008 TREE_VALUE (tail) = output;
8b6a5902 9009 }
3e4093b6 9010
74f0c611
RH
9011 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9012 {
9013 tree input;
9014
9015 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9016 input = TREE_VALUE (tail);
9017
74f0c611
RH
9018 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9019 oconstraints, &allows_mem, &allows_reg))
9020 {
9021 /* If the operand is going to end up in memory,
9022 mark it addressable. */
b4c33883
AP
9023 if (!allows_reg && allows_mem)
9024 {
eadd3d0d
JJ
9025 input = c_fully_fold (input, false, NULL);
9026
b4c33883
AP
9027 /* Strip the nops as we allow this case. FIXME, this really
9028 should be rejected or made deprecated. */
9029 STRIP_NOPS (input);
9030 if (!c_mark_addressable (input))
9031 input = error_mark_node;
bae5cddf 9032 }
eadd3d0d 9033 else
bae5cddf 9034 {
eadd3d0d
JJ
9035 struct c_expr expr;
9036 memset (&expr, 0, sizeof (expr));
9037 expr.value = input;
267bac10 9038 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9039 input = c_fully_fold (expr.value, false, NULL);
9040
9041 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9042 {
9043 error_at (loc, "invalid use of void expression");
9044 input = error_mark_node;
9045 }
bae5cddf 9046 }
74f0c611
RH
9047 }
9048 else
9049 input = error_mark_node;
9050
9051 TREE_VALUE (tail) = input;
9052 }
3e4093b6 9053
1c384bf1
RH
9054 /* ASMs with labels cannot have outputs. This should have been
9055 enforced by the parser. */
9056 gcc_assert (outputs == NULL || labels == NULL);
9057
9058 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9059
5544530a
PB
9060 /* asm statements without outputs, including simple ones, are treated
9061 as volatile. */
9062 ASM_INPUT_P (args) = simple;
9063 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9064
9f0e2d86 9065 return args;
e5e809f4 9066}
3e4093b6 9067\f
c2255bc4
AH
9068/* Generate a goto statement to LABEL. LOC is the location of the
9069 GOTO. */
506e2710
RH
9070
9071tree
c2255bc4 9072c_finish_goto_label (location_t loc, tree label)
506e2710 9073{
e1b7793c 9074 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9075 if (!decl)
9076 return NULL_TREE;
506e2710 9077 TREE_USED (decl) = 1;
c2255bc4
AH
9078 {
9079 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9080 SET_EXPR_LOCATION (t, loc);
9081 return add_stmt (t);
9082 }
506e2710
RH
9083}
9084
c2255bc4
AH
9085/* Generate a computed goto statement to EXPR. LOC is the location of
9086 the GOTO. */
506e2710
RH
9087
9088tree
c2255bc4 9089c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9090{
c2255bc4 9091 tree t;
c1771a20 9092 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9093 expr = c_fully_fold (expr, false, NULL);
506e2710 9094 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9095 t = build1 (GOTO_EXPR, void_type_node, expr);
9096 SET_EXPR_LOCATION (t, loc);
9097 return add_stmt (t);
506e2710
RH
9098}
9099
5088b058 9100/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
9101 to return, or a null pointer for `return;' with no value. LOC is
9102 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9103 is the original type of RETVAL. */
de520661 9104
506e2710 9105tree
c2255bc4 9106c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9107{
0c9b182b
JJ
9108 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9109 bool no_warning = false;
928c19bb 9110 bool npc = false;
36536d79 9111 size_t rank = 0;
3e4093b6
RS
9112
9113 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9114 warning_at (loc, 0,
9115 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9116
36536d79
BI
9117 if (flag_enable_cilkplus && contains_array_notation_expr (retval))
9118 {
9119 /* Array notations are allowed in a return statement if it is inside a
9120 built-in array notation reduction function. */
9121 if (!find_rank (loc, retval, retval, false, &rank))
9122 return error_mark_node;
9123 if (rank >= 1)
9124 {
9125 error_at (loc, "array notation expression cannot be used as a "
9126 "return value");
9127 return error_mark_node;
9128 }
9129 }
939b37da
BI
9130 if (flag_enable_cilkplus && retval && TREE_CODE (retval) == CILK_SPAWN_STMT)
9131 {
9132 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9133 "allowed");
9134 return error_mark_node;
9135 }
928c19bb
JM
9136 if (retval)
9137 {
8ce94e44 9138 tree semantic_type = NULL_TREE;
928c19bb 9139 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9140 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9141 {
9142 semantic_type = TREE_TYPE (retval);
9143 retval = TREE_OPERAND (retval, 0);
9144 }
928c19bb 9145 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9146 if (semantic_type)
9147 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9148 }
9149
3e4093b6 9150 if (!retval)
de520661 9151 {
3e4093b6
RS
9152 current_function_returns_null = 1;
9153 if ((warn_return_type || flag_isoc99)
9154 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9155 {
b8698a0f 9156 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 9157 "%<return%> with no value, in "
0c9b182b
JJ
9158 "function returning non-void");
9159 no_warning = true;
9160 }
400fbf9f 9161 }
3e4093b6 9162 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9163 {
3e4093b6 9164 current_function_returns_null = 1;
2397c575 9165 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9166 pedwarn (loc, 0,
509c9d60 9167 "%<return%> with a value, in function returning void");
b8698a0f 9168 else
c1771a20 9169 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9170 "%<return%> with expression, in function returning void");
de520661 9171 }
3e4093b6 9172 else
de520661 9173 {
c2255bc4
AH
9174 tree t = convert_for_assignment (loc, valtype, retval, origtype,
9175 ic_return,
9176 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9177 tree res = DECL_RESULT (current_function_decl);
9178 tree inner;
9feb29df 9179 bool save;
3e4093b6
RS
9180
9181 current_function_returns_value = 1;
9182 if (t == error_mark_node)
506e2710 9183 return NULL_TREE;
3e4093b6 9184
9feb29df
JJ
9185 save = in_late_binary_op;
9186 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9187 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9188 in_late_binary_op = true;
3e4093b6 9189 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9190 in_late_binary_op = save;
3e4093b6
RS
9191
9192 /* Strip any conversions, additions, and subtractions, and see if
9193 we are returning the address of a local variable. Warn if so. */
9194 while (1)
8b6a5902 9195 {
3e4093b6 9196 switch (TREE_CODE (inner))
8b6a5902 9197 {
849421a3
JJ
9198 CASE_CONVERT:
9199 case NON_LVALUE_EXPR:
3e4093b6 9200 case PLUS_EXPR:
849421a3 9201 case POINTER_PLUS_EXPR:
3e4093b6
RS
9202 inner = TREE_OPERAND (inner, 0);
9203 continue;
9204
9205 case MINUS_EXPR:
9206 /* If the second operand of the MINUS_EXPR has a pointer
9207 type (or is converted from it), this may be valid, so
9208 don't give a warning. */
9209 {
9210 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9211
3f75a254 9212 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9213 && (CONVERT_EXPR_P (op1)
9214 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9215 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9216
3e4093b6
RS
9217 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9218 break;
8b6a5902 9219
3e4093b6
RS
9220 inner = TREE_OPERAND (inner, 0);
9221 continue;
9222 }
400fbf9f 9223
3e4093b6
RS
9224 case ADDR_EXPR:
9225 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9226
6615c446 9227 while (REFERENCE_CLASS_P (inner)
c22cacf3 9228 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9229 inner = TREE_OPERAND (inner, 0);
8b6a5902 9230
a2f1f4c3 9231 if (DECL_P (inner)
3f75a254
JM
9232 && !DECL_EXTERNAL (inner)
9233 && !TREE_STATIC (inner)
3e4093b6 9234 && DECL_CONTEXT (inner) == current_function_decl)
c2255bc4 9235 warning_at (loc,
880661a4
JW
9236 OPT_Wreturn_local_addr, "function returns address "
9237 "of local variable");
3e4093b6 9238 break;
8b6a5902 9239
3e4093b6
RS
9240 default:
9241 break;
9242 }
de520661 9243
3e4093b6
RS
9244 break;
9245 }
9246
53fb4de3 9247 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9248 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9249
9250 if (warn_sequence_point)
9251 verify_sequence_points (retval);
de520661 9252 }
8b6a5902 9253
c2255bc4 9254 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9255 TREE_NO_WARNING (ret_stmt) |= no_warning;
9256 return add_stmt (ret_stmt);
de520661 9257}
3e4093b6
RS
9258\f
9259struct c_switch {
604f5adf
ILT
9260 /* The SWITCH_EXPR being built. */
9261 tree switch_expr;
a6c0a76c 9262
89dbed81 9263 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9264 default conversion is applied. */
9265 tree orig_type;
9266
3e4093b6
RS
9267 /* A splay-tree mapping the low element of a case range to the high
9268 element, or NULL_TREE if there is no high element. Used to
9269 determine whether or not a new case label duplicates an old case
9270 label. We need a tree, rather than simply a hash table, because
9271 of the GNU case range extension. */
9272 splay_tree cases;
a6c0a76c 9273
e1b7793c
ILT
9274 /* The bindings at the point of the switch. This is used for
9275 warnings crossing decls when branching to a case label. */
9276 struct c_spot_bindings *bindings;
187230a7 9277
3e4093b6
RS
9278 /* The next node on the stack. */
9279 struct c_switch *next;
9280};
400fbf9f 9281
3e4093b6
RS
9282/* A stack of the currently active switch statements. The innermost
9283 switch statement is on the top of the stack. There is no need to
9284 mark the stack for garbage collection because it is only active
9285 during the processing of the body of a function, and we never
9286 collect at that point. */
de520661 9287
506e2710 9288struct c_switch *c_switch_stack;
de520661 9289
3e4093b6 9290/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4
AH
9291 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
9292 SWITCH_COND_LOC is the location of the switch's condition. */
de520661 9293
3e4093b6 9294tree
c2255bc4
AH
9295c_start_case (location_t switch_loc,
9296 location_t switch_cond_loc,
9297 tree exp)
de520661 9298{
c58e8676 9299 tree orig_type = error_mark_node;
3e4093b6 9300 struct c_switch *cs;
2f6e4e97 9301
3e4093b6 9302 if (exp != error_mark_node)
de520661 9303 {
3e4093b6
RS
9304 orig_type = TREE_TYPE (exp);
9305
c58e8676 9306 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9307 {
c58e8676
VR
9308 if (orig_type != error_mark_node)
9309 {
c2255bc4 9310 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9311 orig_type = error_mark_node;
9312 }
3e4093b6 9313 exp = integer_zero_node;
de520661 9314 }
3e4093b6 9315 else
de520661 9316 {
c58e8676 9317 tree type = TYPE_MAIN_VARIANT (orig_type);
8b6a5902 9318
8400e75e 9319 if (!in_system_header_at (input_location)
3e4093b6
RS
9320 && (type == long_integer_type_node
9321 || type == long_unsigned_type_node))
c2255bc4
AH
9322 warning_at (switch_cond_loc,
9323 OPT_Wtraditional, "%<long%> switch expression not "
9324 "converted to %<int%> in ISO C");
8b6a5902 9325
928c19bb 9326 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9327 exp = default_conversion (exp);
ca085fd7
MLI
9328
9329 if (warn_sequence_point)
9330 verify_sequence_points (exp);
3e4093b6
RS
9331 }
9332 }
9333
604f5adf 9334 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9335 cs = XNEW (struct c_switch);
604f5adf 9336 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9337 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9338 cs->orig_type = orig_type;
3e4093b6 9339 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9340 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9341 cs->next = c_switch_stack;
9342 c_switch_stack = cs;
3e4093b6 9343
604f5adf 9344 return add_stmt (cs->switch_expr);
3e4093b6
RS
9345}
9346
c2255bc4 9347/* Process a case label at location LOC. */
3e4093b6
RS
9348
9349tree
c2255bc4 9350do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9351{
9352 tree label = NULL_TREE;
9353
17cede2e
JM
9354 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9355 {
9356 low_value = c_fully_fold (low_value, false, NULL);
9357 if (TREE_CODE (low_value) == INTEGER_CST)
c1771a20 9358 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9359 "case label is not an integer constant expression");
9360 }
9361
9362 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9363 {
9364 high_value = c_fully_fold (high_value, false, NULL);
9365 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9366 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9367 "case label is not an integer constant expression");
9368 }
9369
e1b7793c 9370 if (c_switch_stack == NULL)
187230a7
JM
9371 {
9372 if (low_value)
e1b7793c 9373 error_at (loc, "case label not within a switch statement");
187230a7 9374 else
e1b7793c
ILT
9375 error_at (loc, "%<default%> label not within a switch statement");
9376 return NULL_TREE;
187230a7 9377 }
de520661 9378
e1b7793c
ILT
9379 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9380 EXPR_LOCATION (c_switch_stack->switch_expr),
9381 loc))
9382 return NULL_TREE;
9383
9384 label = c_add_case_label (loc, c_switch_stack->cases,
9385 SWITCH_COND (c_switch_stack->switch_expr),
9386 c_switch_stack->orig_type,
9387 low_value, high_value);
9388 if (label == error_mark_node)
9389 label = NULL_TREE;
3e4093b6
RS
9390 return label;
9391}
de520661 9392
3e4093b6 9393/* Finish the switch statement. */
de520661 9394
3e4093b6 9395void
325c3691 9396c_finish_case (tree body)
3e4093b6 9397{
506e2710 9398 struct c_switch *cs = c_switch_stack;
fbc315db 9399 location_t switch_location;
3e4093b6 9400
604f5adf 9401 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9402
6de9cd9a 9403 /* Emit warnings as needed. */
c2255bc4 9404 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
9405 c_do_switch_warnings (cs->cases, switch_location,
9406 TREE_TYPE (cs->switch_expr),
9407 SWITCH_COND (cs->switch_expr));
6de9cd9a 9408
3e4093b6 9409 /* Pop the stack. */
506e2710 9410 c_switch_stack = cs->next;
3e4093b6 9411 splay_tree_delete (cs->cases);
e1b7793c 9412 c_release_switch_bindings (cs->bindings);
5d038c4c 9413 XDELETE (cs);
de520661 9414}
325c3691 9415\f
506e2710
RH
9416/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9417 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9418 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9419 statement, and was not surrounded with parenthesis. */
325c3691 9420
9e51cf9d 9421void
506e2710
RH
9422c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9423 tree else_block, bool nested_if)
325c3691 9424{
506e2710 9425 tree stmt;
325c3691 9426
25c22937
BI
9427 /* If the condition has array notations, then the rank of the then_block and
9428 else_block must be either 0 or be equal to the rank of the condition. If
9429 the condition does not have array notations then break them up as it is
9430 broken up in a normal expression. */
9431 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9432 {
9433 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9434 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9435 return;
9436 if (then_block
9437 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9438 return;
9439 if (else_block
9440 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9441 return;
9442 if (cond_rank != then_rank && then_rank != 0)
9443 {
9444 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9445 " and the then-block");
9446 return;
9447 }
9448 else if (cond_rank != else_rank && else_rank != 0)
9449 {
9450 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9451 " and the else-block");
9452 return;
9453 }
9454 }
506e2710
RH
9455 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9456 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9457 {
506e2710 9458 tree inner_if = then_block;
16865eaa 9459
61ada8ae 9460 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9461 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9462 it might not be exactly THEN_BLOCK, but should be the last
9463 non-container statement within. */
9464 while (1)
9465 switch (TREE_CODE (inner_if))
9466 {
9467 case COND_EXPR:
9468 goto found;
9469 case BIND_EXPR:
9470 inner_if = BIND_EXPR_BODY (inner_if);
9471 break;
9472 case STATEMENT_LIST:
9473 inner_if = expr_last (then_block);
9474 break;
9475 case TRY_FINALLY_EXPR:
9476 case TRY_CATCH_EXPR:
9477 inner_if = TREE_OPERAND (inner_if, 0);
9478 break;
9479 default:
366de0ce 9480 gcc_unreachable ();
506e2710
RH
9481 }
9482 found:
16865eaa 9483
506e2710 9484 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9485 warning_at (if_locus, OPT_Wparentheses,
9486 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9487 }
16865eaa 9488
2214de30 9489 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9490 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9491 add_stmt (stmt);
325c3691
RH
9492}
9493
506e2710
RH
9494/* Emit a general-purpose loop construct. START_LOCUS is the location of
9495 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9496 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9497 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9498 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9499
9500void
506e2710
RH
9501c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9502 tree blab, tree clab, bool cond_is_first)
325c3691 9503{
506e2710
RH
9504 tree entry = NULL, exit = NULL, t;
9505
36536d79
BI
9506 if (flag_enable_cilkplus && contains_array_notation_expr (cond))
9507 {
9508 error_at (start_locus, "array notation expression cannot be used in a "
9509 "loop%'s condition");
9510 return;
9511 }
9512
28af952a
RS
9513 /* If the condition is zero don't generate a loop construct. */
9514 if (cond && integer_zerop (cond))
9515 {
9516 if (cond_is_first)
9517 {
9518 t = build_and_jump (&blab);
9519 SET_EXPR_LOCATION (t, start_locus);
9520 add_stmt (t);
9521 }
9522 }
9523 else
506e2710
RH
9524 {
9525 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9526
506e2710 9527 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9528 out of the loop, or to the top of it. If there's no exit condition,
9529 then we just build a jump back to the top. */
506e2710 9530 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9531
28af952a 9532 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9533 {
9534 /* Canonicalize the loop condition to the end. This means
9535 generating a branch to the loop condition. Reuse the
9536 continue label, if possible. */
9537 if (cond_is_first)
9538 {
9539 if (incr || !clab)
9540 {
9541 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9542 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9543 }
9544 else
9545 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9546 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9547 add_stmt (t);
9548 }
9549
506e2710 9550 t = build_and_jump (&blab);
506e2710 9551 if (cond_is_first)
db3927fb
AH
9552 exit = fold_build3_loc (start_locus,
9553 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9554 else
db3927fb
AH
9555 exit = fold_build3_loc (input_location,
9556 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9557 }
9558
506e2710
RH
9559 add_stmt (top);
9560 }
c22cacf3 9561
506e2710
RH
9562 if (body)
9563 add_stmt (body);
9564 if (clab)
9565 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9566 if (incr)
9567 add_stmt (incr);
9568 if (entry)
9569 add_stmt (entry);
9570 if (exit)
9571 add_stmt (exit);
9572 if (blab)
9573 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9574}
325c3691
RH
9575
9576tree
c2255bc4 9577c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9578{
089efaa4 9579 bool skip;
506e2710 9580 tree label = *label_p;
325c3691 9581
089efaa4
ILT
9582 /* In switch statements break is sometimes stylistically used after
9583 a return statement. This can lead to spurious warnings about
9584 control reaching the end of a non-void function when it is
9585 inlined. Note that we are calling block_may_fallthru with
9586 language specific tree nodes; this works because
9587 block_may_fallthru returns true when given something it does not
9588 understand. */
9589 skip = !block_may_fallthru (cur_stmt_list);
9590
506e2710 9591 if (!label)
089efaa4
ILT
9592 {
9593 if (!skip)
c2255bc4 9594 *label_p = label = create_artificial_label (loc);
089efaa4 9595 }
953ff289
DN
9596 else if (TREE_CODE (label) == LABEL_DECL)
9597 ;
9598 else switch (TREE_INT_CST_LOW (label))
506e2710 9599 {
953ff289 9600 case 0:
506e2710 9601 if (is_break)
c2255bc4 9602 error_at (loc, "break statement not within loop or switch");
506e2710 9603 else
c2255bc4 9604 error_at (loc, "continue statement not within a loop");
506e2710 9605 return NULL_TREE;
953ff289
DN
9606
9607 case 1:
9608 gcc_assert (is_break);
c2255bc4 9609 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9610 return NULL_TREE;
9611
c02065fc
AH
9612 case 2:
9613 if (is_break)
9614 error ("break statement within %<#pragma simd%> loop body");
9615 else
9616 error ("continue statement within %<#pragma simd%> loop body");
9617 return NULL_TREE;
9618
953ff289
DN
9619 default:
9620 gcc_unreachable ();
506e2710 9621 }
325c3691 9622
089efaa4
ILT
9623 if (skip)
9624 return NULL_TREE;
9625
2e28e797
JH
9626 if (!is_break)
9627 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9628
53fb4de3 9629 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9630}
9631
506e2710 9632/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9633
9634static void
c2255bc4 9635emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9636{
e6b5a630
RH
9637 if (expr == error_mark_node)
9638 ;
9639 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9640 {
9641 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9642 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9643 }
27f33b15 9644 else
c2255bc4 9645 warn_if_unused_value (expr, loc);
3a5b9284
RH
9646}
9647
506e2710 9648/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9649 diagnostics, but do not call ADD_STMT. LOC is the location of the
9650 statement. */
3a5b9284 9651
506e2710 9652tree
c2255bc4 9653c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9654{
056928b2
JJ
9655 tree exprv;
9656
3a5b9284 9657 if (!expr)
506e2710 9658 return NULL_TREE;
3a5b9284 9659
928c19bb
JM
9660 expr = c_fully_fold (expr, false, NULL);
9661
3a5b9284
RH
9662 if (warn_sequence_point)
9663 verify_sequence_points (expr);
9664
9665 if (TREE_TYPE (expr) != error_mark_node
9666 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9667 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9668 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9669
9670 /* If we're not processing a statement expression, warn about unused values.
9671 Warnings for statement expressions will be emitted later, once we figure
9672 out which is the result. */
9673 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9674 && warn_unused_value)
c2255bc4 9675 emit_side_effect_warnings (loc, expr);
3a5b9284 9676
056928b2
JJ
9677 exprv = expr;
9678 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9679 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9680 while (CONVERT_EXPR_P (exprv))
9681 exprv = TREE_OPERAND (exprv, 0);
9682 if (DECL_P (exprv)
9683 || handled_component_p (exprv)
9684 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9685 mark_exp_read (exprv);
fa8351f8 9686
3a5b9284
RH
9687 /* If the expression is not of a type to which we cannot assign a line
9688 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9689 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9690 {
9691 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9692 SET_EXPR_LOCATION (expr, loc);
9693 }
506e2710
RH
9694
9695 return expr;
9696}
9697
c2255bc4
AH
9698/* Emit an expression as a statement. LOC is the location of the
9699 expression. */
506e2710
RH
9700
9701tree
c2255bc4 9702c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9703{
9704 if (expr)
c2255bc4 9705 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9706 else
9707 return NULL;
3a5b9284
RH
9708}
9709
9710/* Do the opposite and emit a statement as an expression. To begin,
9711 create a new binding level and return it. */
325c3691
RH
9712
9713tree
9714c_begin_stmt_expr (void)
9715{
9716 tree ret;
9717
9718 /* We must force a BLOCK for this level so that, if it is not expanded
9719 later, there is a way to turn off the entire subtree of blocks that
9720 are contained in it. */
9721 keep_next_level ();
9722 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9723
9724 c_bindings_start_stmt_expr (c_switch_stack == NULL
9725 ? NULL
9726 : c_switch_stack->bindings);
325c3691
RH
9727
9728 /* Mark the current statement list as belonging to a statement list. */
9729 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9730
9731 return ret;
9732}
9733
c2255bc4
AH
9734/* LOC is the location of the compound statement to which this body
9735 belongs. */
9736
325c3691 9737tree
c2255bc4 9738c_finish_stmt_expr (location_t loc, tree body)
325c3691 9739{
3a5b9284 9740 tree last, type, tmp, val;
325c3691
RH
9741 tree *last_p;
9742
c2255bc4 9743 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9744
9745 c_bindings_end_stmt_expr (c_switch_stack == NULL
9746 ? NULL
9747 : c_switch_stack->bindings);
325c3691 9748
3a5b9284
RH
9749 /* Locate the last statement in BODY. See c_end_compound_stmt
9750 about always returning a BIND_EXPR. */
9751 last_p = &BIND_EXPR_BODY (body);
9752 last = BIND_EXPR_BODY (body);
9753
9754 continue_searching:
325c3691
RH
9755 if (TREE_CODE (last) == STATEMENT_LIST)
9756 {
3a5b9284
RH
9757 tree_stmt_iterator i;
9758
9759 /* This can happen with degenerate cases like ({ }). No value. */
9760 if (!TREE_SIDE_EFFECTS (last))
9761 return body;
9762
9763 /* If we're supposed to generate side effects warnings, process
9764 all of the statements except the last. */
27f33b15 9765 if (warn_unused_value)
325c3691 9766 {
3a5b9284 9767 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9768 {
9769 location_t tloc;
9770 tree t = tsi_stmt (i);
9771
9772 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9773 emit_side_effect_warnings (tloc, t);
9774 }
325c3691
RH
9775 }
9776 else
3a5b9284
RH
9777 i = tsi_last (last);
9778 last_p = tsi_stmt_ptr (i);
9779 last = *last_p;
325c3691
RH
9780 }
9781
3a5b9284
RH
9782 /* If the end of the list is exception related, then the list was split
9783 by a call to push_cleanup. Continue searching. */
9784 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9785 || TREE_CODE (last) == TRY_CATCH_EXPR)
9786 {
9787 last_p = &TREE_OPERAND (last, 0);
9788 last = *last_p;
9789 goto continue_searching;
9790 }
9791
26d8af35
JM
9792 if (last == error_mark_node)
9793 return last;
9794
3a5b9284
RH
9795 /* In the case that the BIND_EXPR is not necessary, return the
9796 expression out from inside it. */
26d8af35
JM
9797 if (last == BIND_EXPR_BODY (body)
9798 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9799 {
928c19bb
JM
9800 /* Even if this looks constant, do not allow it in a constant
9801 expression. */
e5a94231 9802 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9803 /* Do not warn if the return value of a statement expression is
9804 unused. */
928c19bb 9805 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9806 return last;
9807 }
325c3691
RH
9808
9809 /* Extract the type of said expression. */
9810 type = TREE_TYPE (last);
325c3691 9811
3a5b9284
RH
9812 /* If we're not returning a value at all, then the BIND_EXPR that
9813 we already have is a fine expression to return. */
9814 if (!type || VOID_TYPE_P (type))
9815 return body;
9816
9817 /* Now that we've located the expression containing the value, it seems
9818 silly to make voidify_wrapper_expr repeat the process. Create a
9819 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9820 tmp = create_tmp_var_raw (type, NULL);
9821
9822 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9823 tree_expr_nonnegative_p giving up immediately. */
9824 val = last;
9825 if (TREE_CODE (val) == NOP_EXPR
9826 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9827 val = TREE_OPERAND (val, 0);
9828
53fb4de3 9829 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9830 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9831
c2255bc4
AH
9832 {
9833 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9834 SET_EXPR_LOCATION (t, loc);
9835 return t;
9836 }
325c3691
RH
9837}
9838\f
9839/* Begin and end compound statements. This is as simple as pushing
9840 and popping new statement lists from the tree. */
9841
9842tree
9843c_begin_compound_stmt (bool do_scope)
9844{
9845 tree stmt = push_stmt_list ();
9846 if (do_scope)
4dfa0342 9847 push_scope ();
325c3691
RH
9848 return stmt;
9849}
9850
c2255bc4
AH
9851/* End a compound statement. STMT is the statement. LOC is the
9852 location of the compound statement-- this is usually the location
9853 of the opening brace. */
9854
325c3691 9855tree
c2255bc4 9856c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9857{
9858 tree block = NULL;
9859
9860 if (do_scope)
9861 {
9862 if (c_dialect_objc ())
9863 objc_clear_super_receiver ();
9864 block = pop_scope ();
9865 }
9866
9867 stmt = pop_stmt_list (stmt);
c2255bc4 9868 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9869
9870 /* If this compound statement is nested immediately inside a statement
9871 expression, then force a BIND_EXPR to be created. Otherwise we'll
9872 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9873 STATEMENT_LISTs merge, and thus we can lose track of what statement
9874 was really last. */
38e01f9e 9875 if (building_stmt_list_p ()
325c3691
RH
9876 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9877 && TREE_CODE (stmt) != BIND_EXPR)
9878 {
53fb4de3 9879 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9880 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9881 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9882 }
9883
9884 return stmt;
9885}
5a508662
RH
9886
9887/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9888 when the current scope is exited. EH_ONLY is true when this is not
9889 meant to apply to normal control flow transfer. */
9890
9891void
c2255bc4 9892push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 9893{
3a5b9284
RH
9894 enum tree_code code;
9895 tree stmt, list;
9896 bool stmt_expr;
9897
9898 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 9899 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 9900 add_stmt (stmt);
3a5b9284
RH
9901 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
9902 list = push_stmt_list ();
9903 TREE_OPERAND (stmt, 0) = list;
9904 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 9905}
325c3691 9906\f
3e4093b6
RS
9907/* Build a binary-operation expression without default conversions.
9908 CODE is the kind of expression to build.
ba47d38d 9909 LOCATION is the operator's location.
3e4093b6
RS
9910 This function differs from `build' in several ways:
9911 the data type of the result is computed and recorded in it,
9912 warnings are generated if arg data types are invalid,
9913 special handling for addition and subtraction of pointers is known,
9914 and some optimization is done (operations on narrow ints
9915 are done in the narrower type when that gives the same result).
9916 Constant folding is also done before the result is returned.
de520661 9917
3e4093b6
RS
9918 Note that the operands will never have enumeral types, or function
9919 or array types, because either they will have the default conversions
9920 performed or they have both just been converted to some other type in which
9921 the arithmetic is to be done. */
9922
9923tree
ba47d38d
AH
9924build_binary_op (location_t location, enum tree_code code,
9925 tree orig_op0, tree orig_op1, int convert_p)
de520661 9926{
8ce94e44
JM
9927 tree type0, type1, orig_type0, orig_type1;
9928 tree eptype;
3e4093b6
RS
9929 enum tree_code code0, code1;
9930 tree op0, op1;
c9f9eb5d 9931 tree ret = error_mark_node;
4de67c26 9932 const char *invalid_op_diag;
4d84fe7c 9933 bool op0_int_operands, op1_int_operands;
928c19bb 9934 bool int_const, int_const_or_overflow, int_operands;
b62acd60 9935
3e4093b6
RS
9936 /* Expression code to give to the expression when it is built.
9937 Normally this is CODE, which is what the caller asked for,
9938 but in some special cases we change it. */
9939 enum tree_code resultcode = code;
8b6a5902 9940
3e4093b6
RS
9941 /* Data type in which the computation is to be performed.
9942 In the simplest cases this is the common type of the arguments. */
9943 tree result_type = NULL;
9944
8ce94e44
JM
9945 /* When the computation is in excess precision, the type of the
9946 final EXCESS_PRECISION_EXPR. */
2d2e923f 9947 tree semantic_result_type = NULL;
8ce94e44 9948
3e4093b6
RS
9949 /* Nonzero means operands have already been type-converted
9950 in whatever way is necessary.
9951 Zero means they need to be converted to RESULT_TYPE. */
9952 int converted = 0;
9953
9954 /* Nonzero means create the expression with this type, rather than
9955 RESULT_TYPE. */
9956 tree build_type = 0;
9957
9958 /* Nonzero means after finally constructing the expression
9959 convert it to this type. */
9960 tree final_type = 0;
9961
9962 /* Nonzero if this is an operation like MIN or MAX which can
9963 safely be computed in short if both args are promoted shorts.
9964 Also implies COMMON.
9965 -1 indicates a bitwise operation; this makes a difference
9966 in the exact conditions for when it is safe to do the operation
9967 in a narrower mode. */
9968 int shorten = 0;
9969
9970 /* Nonzero if this is a comparison operation;
9971 if both args are promoted shorts, compare the original shorts.
9972 Also implies COMMON. */
9973 int short_compare = 0;
9974
9975 /* Nonzero if this is a right-shift operation, which can be computed on the
9976 original short and then promoted if the operand is a promoted short. */
9977 int short_shift = 0;
9978
9979 /* Nonzero means set RESULT_TYPE to the common type of the args. */
9980 int common = 0;
9981
58393038
ZL
9982 /* True means types are compatible as far as ObjC is concerned. */
9983 bool objc_ok;
9984
8ce94e44
JM
9985 /* True means this is an arithmetic operation that may need excess
9986 precision. */
9987 bool may_need_excess_precision;
9988
180f8dbb
JM
9989 /* True means this is a boolean operation that converts both its
9990 operands to truth-values. */
9991 bool boolean_op = false;
9992
de5a5fa1
MP
9993 /* Remember whether we're doing / or %. */
9994 bool doing_div_or_mod = false;
9995
9996 /* Remember whether we're doing << or >>. */
9997 bool doing_shift = false;
9998
9999 /* Tree holding instrumentation expression. */
10000 tree instrument_expr = NULL;
10001
ba47d38d
AH
10002 if (location == UNKNOWN_LOCATION)
10003 location = input_location;
10004
4d84fe7c
JM
10005 op0 = orig_op0;
10006 op1 = orig_op1;
10007
10008 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10009 if (op0_int_operands)
10010 op0 = remove_c_maybe_const_expr (op0);
10011 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10012 if (op1_int_operands)
10013 op1 = remove_c_maybe_const_expr (op1);
10014 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10015 if (int_operands)
10016 {
10017 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10018 && TREE_CODE (orig_op1) == INTEGER_CST);
10019 int_const = (int_const_or_overflow
10020 && !TREE_OVERFLOW (orig_op0)
10021 && !TREE_OVERFLOW (orig_op1));
10022 }
10023 else
10024 int_const = int_const_or_overflow = false;
10025
0e3a99ae 10026 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10027 if (convert_p
10028 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10029 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10030 {
4d84fe7c
JM
10031 op0 = default_conversion (op0);
10032 op1 = default_conversion (op1);
790e9490
RS
10033 }
10034
36536d79
BI
10035 /* When Cilk Plus is enabled and there are array notations inside op0, then
10036 we check to see if there are builtin array notation functions. If
10037 so, then we take on the type of the array notation inside it. */
10038 if (flag_enable_cilkplus && contains_array_notation_expr (op0))
10039 orig_type0 = type0 = find_correct_array_notation_type (op0);
10040 else
10041 orig_type0 = type0 = TREE_TYPE (op0);
10042
10043 if (flag_enable_cilkplus && contains_array_notation_expr (op1))
10044 orig_type1 = type1 = find_correct_array_notation_type (op1);
10045 else
10046 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10047
3e4093b6
RS
10048 /* The expression codes of the data types of the arguments tell us
10049 whether the arguments are integers, floating, pointers, etc. */
10050 code0 = TREE_CODE (type0);
10051 code1 = TREE_CODE (type1);
10052
10053 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10054 STRIP_TYPE_NOPS (op0);
10055 STRIP_TYPE_NOPS (op1);
10056
10057 /* If an error was already reported for one of the arguments,
10058 avoid reporting another error. */
10059
10060 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10061 return error_mark_node;
10062
4de67c26
JM
10063 if ((invalid_op_diag
10064 = targetm.invalid_binary_op (code, type0, type1)))
10065 {
ba47d38d 10066 error_at (location, invalid_op_diag);
4de67c26
JM
10067 return error_mark_node;
10068 }
10069
8ce94e44
JM
10070 switch (code)
10071 {
10072 case PLUS_EXPR:
10073 case MINUS_EXPR:
10074 case MULT_EXPR:
10075 case TRUNC_DIV_EXPR:
10076 case CEIL_DIV_EXPR:
10077 case FLOOR_DIV_EXPR:
10078 case ROUND_DIV_EXPR:
10079 case EXACT_DIV_EXPR:
10080 may_need_excess_precision = true;
10081 break;
10082 default:
10083 may_need_excess_precision = false;
10084 break;
10085 }
10086 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10087 {
10088 op0 = TREE_OPERAND (op0, 0);
10089 type0 = TREE_TYPE (op0);
10090 }
10091 else if (may_need_excess_precision
10092 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10093 {
10094 type0 = eptype;
10095 op0 = convert (eptype, op0);
10096 }
10097 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10098 {
10099 op1 = TREE_OPERAND (op1, 0);
10100 type1 = TREE_TYPE (op1);
10101 }
10102 else if (may_need_excess_precision
10103 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10104 {
10105 type1 = eptype;
10106 op1 = convert (eptype, op1);
10107 }
10108
58393038
ZL
10109 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10110
0e3a99ae
AS
10111 /* In case when one of the operands of the binary operation is
10112 a vector and another is a scalar -- convert scalar to vector. */
10113 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10114 {
a212e43f
MG
10115 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10116 true);
f90e8e2e 10117
0e3a99ae
AS
10118 switch (convert_flag)
10119 {
10120 case stv_error:
10121 return error_mark_node;
10122 case stv_firstarg:
10123 {
10124 bool maybe_const = true;
10125 tree sc;
10126 sc = c_fully_fold (op0, false, &maybe_const);
10127 sc = save_expr (sc);
10128 sc = convert (TREE_TYPE (type1), sc);
10129 op0 = build_vector_from_val (type1, sc);
10130 if (!maybe_const)
10131 op0 = c_wrap_maybe_const (op0, true);
10132 orig_type0 = type0 = TREE_TYPE (op0);
10133 code0 = TREE_CODE (type0);
10134 converted = 1;
10135 break;
10136 }
10137 case stv_secondarg:
10138 {
10139 bool maybe_const = true;
10140 tree sc;
10141 sc = c_fully_fold (op1, false, &maybe_const);
10142 sc = save_expr (sc);
10143 sc = convert (TREE_TYPE (type0), sc);
10144 op1 = build_vector_from_val (type0, sc);
10145 if (!maybe_const)
54b9f838 10146 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10147 orig_type1 = type1 = TREE_TYPE (op1);
10148 code1 = TREE_CODE (type1);
10149 converted = 1;
10150 break;
10151 }
10152 default:
10153 break;
10154 }
10155 }
10156
3e4093b6 10157 switch (code)
de520661 10158 {
3e4093b6
RS
10159 case PLUS_EXPR:
10160 /* Handle the pointer + int case. */
10161 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10162 {
db3927fb 10163 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10164 goto return_build_binary_op;
10165 }
3e4093b6 10166 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10167 {
db3927fb 10168 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10169 goto return_build_binary_op;
10170 }
fe67cf58 10171 else
3e4093b6
RS
10172 common = 1;
10173 break;
400fbf9f 10174
3e4093b6
RS
10175 case MINUS_EXPR:
10176 /* Subtraction of two similar pointers.
10177 We must subtract them as integers, then divide by object size. */
10178 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10179 && comp_target_types (location, type0, type1))
c9f9eb5d 10180 {
db3927fb 10181 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10182 goto return_build_binary_op;
10183 }
3e4093b6
RS
10184 /* Handle pointer minus int. Just like pointer plus int. */
10185 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10186 {
db3927fb 10187 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10188 goto return_build_binary_op;
10189 }
3e4093b6
RS
10190 else
10191 common = 1;
10192 break;
8b6a5902 10193
3e4093b6
RS
10194 case MULT_EXPR:
10195 common = 1;
10196 break;
10197
10198 case TRUNC_DIV_EXPR:
10199 case CEIL_DIV_EXPR:
10200 case FLOOR_DIV_EXPR:
10201 case ROUND_DIV_EXPR:
10202 case EXACT_DIV_EXPR:
de5a5fa1 10203 doing_div_or_mod = true;
c9f9eb5d 10204 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10205
10206 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10207 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10208 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10209 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10210 || code1 == FIXED_POINT_TYPE
3e4093b6 10211 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10212 {
5bed876a
AH
10213 enum tree_code tcode0 = code0, tcode1 = code1;
10214
3a021db2 10215 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10216 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10217 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10218 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10219
ab22c1fa
CF
10220 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10221 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10222 resultcode = RDIV_EXPR;
10223 else
10224 /* Although it would be tempting to shorten always here, that
10225 loses on some targets, since the modulo instruction is
10226 undefined if the quotient can't be represented in the
10227 computation mode. We shorten only if unsigned or if
10228 dividing by something we know != -1. */
8df83eae 10229 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10230 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10231 && !integer_all_onesp (op1)));
3e4093b6
RS
10232 common = 1;
10233 }
10234 break;
de520661 10235
3e4093b6 10236 case BIT_AND_EXPR:
3e4093b6
RS
10237 case BIT_IOR_EXPR:
10238 case BIT_XOR_EXPR:
10239 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10240 shorten = -1;
9ef0c8d9
AP
10241 /* Allow vector types which are not floating point types. */
10242 else if (code0 == VECTOR_TYPE
10243 && code1 == VECTOR_TYPE
10244 && !VECTOR_FLOAT_TYPE_P (type0)
10245 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10246 common = 1;
10247 break;
10248
10249 case TRUNC_MOD_EXPR:
10250 case FLOOR_MOD_EXPR:
de5a5fa1 10251 doing_div_or_mod = true;
c9f9eb5d 10252 warn_for_div_by_zero (location, op1);
de520661 10253
5cfd5d9b
AP
10254 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10255 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10256 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10257 common = 1;
10258 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10259 {
10260 /* Although it would be tempting to shorten always here, that loses
10261 on some targets, since the modulo instruction is undefined if the
10262 quotient can't be represented in the computation mode. We shorten
10263 only if unsigned or if dividing by something we know != -1. */
8df83eae 10264 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10265 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10266 && !integer_all_onesp (op1)));
3e4093b6
RS
10267 common = 1;
10268 }
10269 break;
de520661 10270
3e4093b6
RS
10271 case TRUTH_ANDIF_EXPR:
10272 case TRUTH_ORIF_EXPR:
10273 case TRUTH_AND_EXPR:
10274 case TRUTH_OR_EXPR:
10275 case TRUTH_XOR_EXPR:
10276 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10277 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10278 || code0 == FIXED_POINT_TYPE)
3e4093b6 10279 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10280 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10281 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10282 {
10283 /* Result of these operations is always an int,
10284 but that does not mean the operands should be
10285 converted to ints! */
10286 result_type = integer_type_node;
a27d595d
JM
10287 if (op0_int_operands)
10288 {
10289 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10290 op0 = remove_c_maybe_const_expr (op0);
10291 }
10292 else
10293 op0 = c_objc_common_truthvalue_conversion (location, op0);
10294 if (op1_int_operands)
10295 {
10296 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10297 op1 = remove_c_maybe_const_expr (op1);
10298 }
10299 else
10300 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10301 converted = 1;
180f8dbb 10302 boolean_op = true;
3e4093b6 10303 }
928c19bb
JM
10304 if (code == TRUTH_ANDIF_EXPR)
10305 {
10306 int_const_or_overflow = (int_operands
10307 && TREE_CODE (orig_op0) == INTEGER_CST
10308 && (op0 == truthvalue_false_node
10309 || TREE_CODE (orig_op1) == INTEGER_CST));
10310 int_const = (int_const_or_overflow
10311 && !TREE_OVERFLOW (orig_op0)
10312 && (op0 == truthvalue_false_node
10313 || !TREE_OVERFLOW (orig_op1)));
10314 }
10315 else if (code == TRUTH_ORIF_EXPR)
10316 {
10317 int_const_or_overflow = (int_operands
10318 && TREE_CODE (orig_op0) == INTEGER_CST
10319 && (op0 == truthvalue_true_node
10320 || TREE_CODE (orig_op1) == INTEGER_CST));
10321 int_const = (int_const_or_overflow
10322 && !TREE_OVERFLOW (orig_op0)
10323 && (op0 == truthvalue_true_node
10324 || !TREE_OVERFLOW (orig_op1)));
10325 }
3e4093b6 10326 break;
eba80994 10327
3e4093b6
RS
10328 /* Shift operations: result has same type as first operand;
10329 always convert second operand to int.
10330 Also set SHORT_SHIFT if shifting rightward. */
de520661 10331
3e4093b6 10332 case RSHIFT_EXPR:
f87bd04b
AS
10333 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10334 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10335 {
10336 result_type = type0;
10337 converted = 1;
10338 }
10339 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10340 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10341 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10342 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10343 {
10344 result_type = type0;
10345 converted = 1;
10346 }
10347 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10348 && code1 == INTEGER_TYPE)
3e4093b6 10349 {
de5a5fa1 10350 doing_shift = true;
928c19bb 10351 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10352 {
3e4093b6 10353 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10354 {
10355 int_const = false;
7d882b83 10356 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10357 warning (0, "right shift count is negative");
10358 }
3e4093b6 10359 else
bbb818c6 10360 {
3f75a254 10361 if (!integer_zerop (op1))
3e4093b6
RS
10362 short_shift = 1;
10363
10364 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10365 {
10366 int_const = false;
7d882b83 10367 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10368 warning (0, "right shift count >= width of type");
10369 }
bbb818c6 10370 }
b62acd60 10371 }
de520661 10372
3e4093b6
RS
10373 /* Use the type of the value to be shifted. */
10374 result_type = type0;
f87bd04b
AS
10375 /* Convert the non vector shift-count to an integer, regardless
10376 of size of value being shifted. */
10377 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10378 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10379 op1 = convert (integer_type_node, op1);
10380 /* Avoid converting op1 to result_type later. */
10381 converted = 1;
400fbf9f 10382 }
3e4093b6 10383 break;
253b6b82 10384
3e4093b6 10385 case LSHIFT_EXPR:
f87bd04b
AS
10386 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10387 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10388 {
10389 result_type = type0;
10390 converted = 1;
10391 }
10392 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10393 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10394 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10395 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10396 {
10397 result_type = type0;
10398 converted = 1;
10399 }
10400 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10401 && code1 == INTEGER_TYPE)
3e4093b6 10402 {
de5a5fa1 10403 doing_shift = true;
928c19bb 10404 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10405 {
3e4093b6 10406 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10407 {
10408 int_const = false;
7d882b83 10409 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10410 warning (0, "left shift count is negative");
10411 }
de520661 10412
3e4093b6 10413 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10414 {
10415 int_const = false;
7d882b83 10416 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10417 warning (0, "left shift count >= width of type");
10418 }
94ba5069 10419 }
de520661 10420
3e4093b6
RS
10421 /* Use the type of the value to be shifted. */
10422 result_type = type0;
f87bd04b
AS
10423 /* Convert the non vector shift-count to an integer, regardless
10424 of size of value being shifted. */
10425 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10426 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10427 op1 = convert (integer_type_node, op1);
10428 /* Avoid converting op1 to result_type later. */
10429 converted = 1;
400fbf9f 10430 }
3e4093b6 10431 break;
de520661 10432
3e4093b6
RS
10433 case EQ_EXPR:
10434 case NE_EXPR:
d246ab4f
AS
10435 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10436 {
10437 tree intt;
0af94e6f 10438 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10439 {
10440 error_at (location, "comparing vectors with different "
10441 "element types");
10442 return error_mark_node;
10443 }
10444
10445 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10446 {
10447 error_at (location, "comparing vectors with different "
10448 "number of elements");
10449 return error_mark_node;
10450 }
10451
10452 /* Always construct signed integer vector type. */
10453 intt = c_common_type_for_size (GET_MODE_BITSIZE
10454 (TYPE_MODE (TREE_TYPE (type0))), 0);
10455 result_type = build_opaque_vector_type (intt,
10456 TYPE_VECTOR_SUBPARTS (type0));
10457 converted = 1;
10458 break;
10459 }
ae311566 10460 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10461 warning_at (location,
10462 OPT_Wfloat_equal,
10463 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10464 /* Result of comparison is always int,
10465 but don't convert the args to int! */
10466 build_type = integer_type_node;
10467 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10468 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10469 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10470 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10471 short_compare = 1;
637f1455
SZ
10472 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10473 {
10474 if (TREE_CODE (op0) == ADDR_EXPR
10475 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10476 {
10477 if (code == EQ_EXPR)
10478 warning_at (location,
10479 OPT_Waddress,
10480 "the comparison will always evaluate as %<false%> "
10481 "for the address of %qD will never be NULL",
10482 TREE_OPERAND (op0, 0));
10483 else
10484 warning_at (location,
10485 OPT_Waddress,
10486 "the comparison will always evaluate as %<true%> "
10487 "for the address of %qD will never be NULL",
10488 TREE_OPERAND (op0, 0));
10489 }
10490 result_type = type0;
10491 }
10492 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10493 {
10494 if (TREE_CODE (op1) == ADDR_EXPR
10495 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10496 {
10497 if (code == EQ_EXPR)
10498 warning_at (location,
f90e8e2e 10499 OPT_Waddress,
637f1455
SZ
10500 "the comparison will always evaluate as %<false%> "
10501 "for the address of %qD will never be NULL",
10502 TREE_OPERAND (op1, 0));
10503 else
10504 warning_at (location,
10505 OPT_Waddress,
10506 "the comparison will always evaluate as %<true%> "
10507 "for the address of %qD will never be NULL",
10508 TREE_OPERAND (op1, 0));
10509 }
10510 result_type = type1;
10511 }
3e4093b6
RS
10512 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10513 {
10514 tree tt0 = TREE_TYPE (type0);
10515 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10516 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10517 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10518 addr_space_t as_common = ADDR_SPACE_GENERIC;
10519
3e4093b6
RS
10520 /* Anything compares with void *. void * compares with anything.
10521 Otherwise, the targets must be compatible
10522 and both must be object or both incomplete. */
744aa42f 10523 if (comp_target_types (location, type0, type1))
10bc1b1b 10524 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10525 else if (!addr_space_superset (as0, as1, &as_common))
10526 {
10527 error_at (location, "comparison of pointers to "
10528 "disjoint address spaces");
10529 return error_mark_node;
10530 }
267bac10 10531 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10532 {
36c5e70a 10533 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10534 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10535 "comparison of %<void *%> with function pointer");
ee2990e7 10536 }
267bac10 10537 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10538 {
36c5e70a 10539 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10540 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10541 "comparison of %<void *%> with function pointer");
e6834654 10542 }
3e4093b6 10543 else
58393038
ZL
10544 /* Avoid warning about the volatile ObjC EH puts on decls. */
10545 if (!objc_ok)
ba47d38d 10546 pedwarn (location, 0,
509c9d60 10547 "comparison of distinct pointer types lacks a cast");
e6834654 10548
3e4093b6 10549 if (result_type == NULL_TREE)
36c5e70a
BE
10550 {
10551 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10552 result_type = build_pointer_type
10553 (build_qualified_type (void_type_node, qual));
10554 }
e6834654 10555 }
3e4093b6 10556 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10557 {
3e4093b6 10558 result_type = type0;
ba47d38d 10559 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10560 }
3e4093b6 10561 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10562 {
3e4093b6 10563 result_type = type1;
ba47d38d 10564 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10565 }
3e4093b6 10566 break;
8b6a5902 10567
3e4093b6
RS
10568 case LE_EXPR:
10569 case GE_EXPR:
10570 case LT_EXPR:
10571 case GT_EXPR:
d246ab4f
AS
10572 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10573 {
10574 tree intt;
0af94e6f 10575 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10576 {
10577 error_at (location, "comparing vectors with different "
10578 "element types");
10579 return error_mark_node;
10580 }
10581
10582 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10583 {
10584 error_at (location, "comparing vectors with different "
10585 "number of elements");
10586 return error_mark_node;
10587 }
10588
10589 /* Always construct signed integer vector type. */
10590 intt = c_common_type_for_size (GET_MODE_BITSIZE
10591 (TYPE_MODE (TREE_TYPE (type0))), 0);
10592 result_type = build_opaque_vector_type (intt,
10593 TYPE_VECTOR_SUBPARTS (type0));
10594 converted = 1;
10595 break;
10596 }
3e4093b6 10597 build_type = integer_type_node;
ab22c1fa
CF
10598 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10599 || code0 == FIXED_POINT_TYPE)
10600 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10601 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10602 short_compare = 1;
10603 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10604 {
36c5e70a
BE
10605 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10606 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10607 addr_space_t as_common;
10608
744aa42f 10609 if (comp_target_types (location, type0, type1))
3e4093b6 10610 {
10bc1b1b 10611 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10612 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10613 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10614 pedwarn (location, 0,
509c9d60 10615 "comparison of complete and incomplete pointers");
fcf73884 10616 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10617 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10618 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10619 else if (null_pointer_constant_p (orig_op0)
10620 || null_pointer_constant_p (orig_op1))
10621 warning_at (location, OPT_Wextra,
10622 "ordered comparison of pointer with null pointer");
10623
3e4093b6 10624 }
36c5e70a
BE
10625 else if (!addr_space_superset (as0, as1, &as_common))
10626 {
10627 error_at (location, "comparison of pointers to "
10628 "disjoint address spaces");
10629 return error_mark_node;
10630 }
3e4093b6
RS
10631 else
10632 {
36c5e70a
BE
10633 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10634 result_type = build_pointer_type
10635 (build_qualified_type (void_type_node, qual));
ba47d38d 10636 pedwarn (location, 0,
509c9d60 10637 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10638 }
10639 }
6aa3c60d 10640 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10641 {
10642 result_type = type0;
fcf73884 10643 if (pedantic)
c1771a20 10644 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10645 "ordered comparison of pointer with integer zero");
10646 else if (extra_warnings)
ba47d38d 10647 warning_at (location, OPT_Wextra,
d42ba3b8 10648 "ordered comparison of pointer with integer zero");
3e4093b6 10649 }
6aa3c60d 10650 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10651 {
10652 result_type = type1;
d42ba3b8 10653 if (pedantic)
c1771a20 10654 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10655 "ordered comparison of pointer with integer zero");
10656 else if (extra_warnings)
10657 warning_at (location, OPT_Wextra,
10658 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10659 }
10660 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10661 {
10662 result_type = type0;
ba47d38d 10663 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10664 }
10665 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10666 {
10667 result_type = type1;
ba47d38d 10668 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10669 }
10670 break;
64094f6a 10671
3e4093b6 10672 default:
37b2f290 10673 gcc_unreachable ();
c9fe6f9f 10674 }
8f17b5c5 10675
e57e265b
PB
10676 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10677 return error_mark_node;
10678
5bed876a
AH
10679 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10680 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10681 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10682 {
ba47d38d 10683 binary_op_error (location, code, type0, type1);
5bed876a
AH
10684 return error_mark_node;
10685 }
10686
3e4093b6 10687 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10688 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10689 &&
10690 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10691 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10692 {
2ca862e9
JM
10693 bool first_complex = (code0 == COMPLEX_TYPE);
10694 bool second_complex = (code1 == COMPLEX_TYPE);
10695 int none_complex = (!first_complex && !second_complex);
39b726dd 10696
3e4093b6 10697 if (shorten || common || short_compare)
3bf6bfcc
JJ
10698 {
10699 result_type = c_common_type (type0, type1);
0a0b3574
MM
10700 do_warn_double_promotion (result_type, type0, type1,
10701 "implicit conversion from %qT to %qT "
10702 "to match other operand of binary "
10703 "expression",
10704 location);
3bf6bfcc
JJ
10705 if (result_type == error_mark_node)
10706 return error_mark_node;
10707 }
400fbf9f 10708
2ca862e9
JM
10709 if (first_complex != second_complex
10710 && (code == PLUS_EXPR
10711 || code == MINUS_EXPR
10712 || code == MULT_EXPR
10713 || (code == TRUNC_DIV_EXPR && first_complex))
10714 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10715 && flag_signed_zeros)
10716 {
10717 /* An operation on mixed real/complex operands must be
10718 handled specially, but the language-independent code can
10719 more easily optimize the plain complex arithmetic if
10720 -fno-signed-zeros. */
10721 tree real_type = TREE_TYPE (result_type);
10722 tree real, imag;
10723 if (type0 != orig_type0 || type1 != orig_type1)
10724 {
10725 gcc_assert (may_need_excess_precision && common);
2d2e923f 10726 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10727 }
10728 if (first_complex)
10729 {
10730 if (TREE_TYPE (op0) != result_type)
10731 op0 = convert_and_check (result_type, op0);
10732 if (TREE_TYPE (op1) != real_type)
10733 op1 = convert_and_check (real_type, op1);
10734 }
10735 else
10736 {
10737 if (TREE_TYPE (op0) != real_type)
10738 op0 = convert_and_check (real_type, op0);
10739 if (TREE_TYPE (op1) != result_type)
10740 op1 = convert_and_check (result_type, op1);
10741 }
10742 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10743 return error_mark_node;
10744 if (first_complex)
10745 {
10746 op0 = c_save_expr (op0);
10747 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10748 op0, 1);
10749 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10750 op0, 1);
10751 switch (code)
10752 {
10753 case MULT_EXPR:
10754 case TRUNC_DIV_EXPR:
c4603e7c 10755 op1 = c_save_expr (op1);
2ca862e9
JM
10756 imag = build2 (resultcode, real_type, imag, op1);
10757 /* Fall through. */
10758 case PLUS_EXPR:
10759 case MINUS_EXPR:
10760 real = build2 (resultcode, real_type, real, op1);
10761 break;
10762 default:
10763 gcc_unreachable();
10764 }
10765 }
10766 else
10767 {
10768 op1 = c_save_expr (op1);
10769 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10770 op1, 1);
10771 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10772 op1, 1);
10773 switch (code)
10774 {
10775 case MULT_EXPR:
c4603e7c 10776 op0 = c_save_expr (op0);
2ca862e9
JM
10777 imag = build2 (resultcode, real_type, op0, imag);
10778 /* Fall through. */
10779 case PLUS_EXPR:
10780 real = build2 (resultcode, real_type, op0, real);
10781 break;
10782 case MINUS_EXPR:
10783 real = build2 (resultcode, real_type, op0, real);
10784 imag = build1 (NEGATE_EXPR, real_type, imag);
10785 break;
10786 default:
10787 gcc_unreachable();
10788 }
10789 }
10790 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10791 goto return_build_binary_op;
10792 }
10793
3e4093b6
RS
10794 /* For certain operations (which identify themselves by shorten != 0)
10795 if both args were extended from the same smaller type,
10796 do the arithmetic in that type and then extend.
400fbf9f 10797
3e4093b6
RS
10798 shorten !=0 and !=1 indicates a bitwise operation.
10799 For them, this optimization is safe only if
10800 both args are zero-extended or both are sign-extended.
10801 Otherwise, we might change the result.
10802 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10803 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10804
3e4093b6
RS
10805 if (shorten && none_complex)
10806 {
3e4093b6 10807 final_type = result_type;
b8698a0f 10808 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10809 shorten == -1);
3e4093b6 10810 }
88a3dbc1 10811
3e4093b6 10812 /* Shifts can be shortened if shifting right. */
2f6e4e97 10813
3e4093b6
RS
10814 if (short_shift)
10815 {
10816 int unsigned_arg;
10817 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10818
3e4093b6 10819 final_type = result_type;
abe80e6d 10820
3e4093b6 10821 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10822 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10823
3e4093b6 10824 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10825 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10826 /* We can shorten only if the shift count is less than the
10827 number of bits in the smaller type size. */
10828 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10829 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10830 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10831 {
10832 /* Do an unsigned shift if the operand was zero-extended. */
10833 result_type
10834 = c_common_signed_or_unsigned_type (unsigned_arg,
10835 TREE_TYPE (arg0));
10836 /* Convert value-to-be-shifted to that type. */
10837 if (TREE_TYPE (op0) != result_type)
10838 op0 = convert (result_type, op0);
10839 converted = 1;
abe80e6d 10840 }
88a3dbc1
RK
10841 }
10842
3e4093b6
RS
10843 /* Comparison operations are shortened too but differently.
10844 They identify themselves by setting short_compare = 1. */
56cb9733 10845
3e4093b6
RS
10846 if (short_compare)
10847 {
10848 /* Don't write &op0, etc., because that would prevent op0
10849 from being kept in a register.
10850 Instead, make copies of the our local variables and
10851 pass the copies by reference, then copy them back afterward. */
10852 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10853 enum tree_code xresultcode = resultcode;
10854 tree val
10855 = shorten_compare (&xop0, &xop1, &xresult_type, &xresultcode);
8f17b5c5 10856
3e4093b6 10857 if (val != 0)
c9f9eb5d
AH
10858 {
10859 ret = val;
10860 goto return_build_binary_op;
10861 }
8f17b5c5 10862
3e4093b6
RS
10863 op0 = xop0, op1 = xop1;
10864 converted = 1;
10865 resultcode = xresultcode;
8f17b5c5 10866
7d882b83 10867 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10868 {
10869 bool op0_maybe_const = true;
10870 bool op1_maybe_const = true;
10871 tree orig_op0_folded, orig_op1_folded;
10872
10873 if (in_late_binary_op)
10874 {
10875 orig_op0_folded = orig_op0;
10876 orig_op1_folded = orig_op1;
10877 }
10878 else
10879 {
10880 /* Fold for the sake of possible warnings, as in
10881 build_conditional_expr. This requires the
10882 "original" values to be folded, not just op0 and
10883 op1. */
f5178456 10884 c_inhibit_evaluation_warnings++;
928c19bb
JM
10885 op0 = c_fully_fold (op0, require_constant_value,
10886 &op0_maybe_const);
10887 op1 = c_fully_fold (op1, require_constant_value,
10888 &op1_maybe_const);
f5178456 10889 c_inhibit_evaluation_warnings--;
928c19bb
JM
10890 orig_op0_folded = c_fully_fold (orig_op0,
10891 require_constant_value,
10892 NULL);
10893 orig_op1_folded = c_fully_fold (orig_op1,
10894 require_constant_value,
10895 NULL);
10896 }
10897
10898 if (warn_sign_compare)
10899 warn_for_sign_compare (location, orig_op0_folded,
10900 orig_op1_folded, op0, op1,
10901 result_type, resultcode);
5c2f94b4 10902 if (!in_late_binary_op && !int_operands)
928c19bb
JM
10903 {
10904 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 10905 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 10906 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 10907 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 10908 }
3e4093b6 10909 }
2ad1815d 10910 }
64094f6a 10911 }
64094f6a 10912
3e4093b6
RS
10913 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
10914 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
10915 Then the expression will be built.
10916 It will be given type FINAL_TYPE if that is nonzero;
10917 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 10918
3e4093b6
RS
10919 if (!result_type)
10920 {
ba47d38d 10921 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
10922 return error_mark_node;
10923 }
400fbf9f 10924
3e4093b6 10925 if (build_type == NULL_TREE)
8ce94e44
JM
10926 {
10927 build_type = result_type;
180f8dbb
JM
10928 if ((type0 != orig_type0 || type1 != orig_type1)
10929 && !boolean_op)
8ce94e44
JM
10930 {
10931 gcc_assert (may_need_excess_precision && common);
2d2e923f 10932 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
10933 }
10934 }
400fbf9f 10935
2d2e923f
MLI
10936 if (!converted)
10937 {
10938 op0 = ep_convert_and_check (result_type, op0, semantic_result_type);
10939 op1 = ep_convert_and_check (result_type, op1, semantic_result_type);
10940
10941 /* This can happen if one operand has a vector type, and the other
10942 has a different type. */
10943 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10944 return error_mark_node;
10945 }
10946
a24d975c 10947 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE))
de5a5fa1 10948 && current_function_decl != 0
ce6923c5
MP
10949 && !lookup_attribute ("no_sanitize_undefined",
10950 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
10951 && (doing_div_or_mod || doing_shift))
10952 {
10953 /* OP0 and/or OP1 might have side-effects. */
10954 op0 = c_save_expr (op0);
10955 op1 = c_save_expr (op1);
10956 op0 = c_fully_fold (op0, false, NULL);
10957 op1 = c_fully_fold (op1, false, NULL);
a24d975c 10958 if (doing_div_or_mod && (flag_sanitize & SANITIZE_DIVIDE))
de5a5fa1 10959 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 10960 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
10961 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
10962 }
10963
c9f9eb5d 10964 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
10965 if (int_const_or_overflow)
10966 ret = (require_constant_value
db3927fb
AH
10967 ? fold_build2_initializer_loc (location, resultcode, build_type,
10968 op0, op1)
10969 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
10970 else
10971 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
10972 if (final_type != 0)
10973 ret = convert (final_type, ret);
10974
10975 return_build_binary_op:
10976 gcc_assert (ret != error_mark_node);
928c19bb
JM
10977 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
10978 ret = (int_operands
10979 ? note_integer_operands (ret)
10980 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
10981 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
10982 && !in_late_binary_op)
10983 ret = note_integer_operands (ret);
2d2e923f
MLI
10984 if (semantic_result_type)
10985 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 10986 protected_set_expr_location (ret, location);
de5a5fa1 10987
a24d975c 10988 if (instrument_expr != NULL)
de5a5fa1
MP
10989 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
10990 instrument_expr, ret);
10991
c9f9eb5d 10992 return ret;
400fbf9f 10993}
85498824
JM
10994
10995
10996/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 10997 purpose. LOCATION is the source location for the expression. */
85498824
JM
10998
10999tree
ba47d38d 11000c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11001{
928c19bb
JM
11002 bool int_const, int_operands;
11003
85498824
JM
11004 switch (TREE_CODE (TREE_TYPE (expr)))
11005 {
11006 case ARRAY_TYPE:
ba47d38d 11007 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11008 return error_mark_node;
11009
11010 case RECORD_TYPE:
ba47d38d 11011 error_at (location, "used struct type value where scalar is required");
85498824
JM
11012 return error_mark_node;
11013
11014 case UNION_TYPE:
ba47d38d 11015 error_at (location, "used union type value where scalar is required");
85498824
JM
11016 return error_mark_node;
11017
04af8788
NP
11018 case VOID_TYPE:
11019 error_at (location, "void value not ignored as it ought to be");
11020 return error_mark_node;
11021
46bdb9cf
JM
11022 case FUNCTION_TYPE:
11023 gcc_unreachable ();
11024
d246ab4f
AS
11025 case VECTOR_TYPE:
11026 error_at (location, "used vector type where scalar is required");
11027 return error_mark_node;
11028
85498824
JM
11029 default:
11030 break;
11031 }
11032
928c19bb
JM
11033 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11034 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11035 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11036 {
11037 expr = remove_c_maybe_const_expr (expr);
11038 expr = build2 (NE_EXPR, integer_type_node, expr,
11039 convert (TREE_TYPE (expr), integer_zero_node));
11040 expr = note_integer_operands (expr);
11041 }
11042 else
11043 /* ??? Should we also give an error for vectors rather than leaving
11044 those to give errors later? */
11045 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11046
11047 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11048 {
11049 if (TREE_OVERFLOW (expr))
11050 return expr;
11051 else
11052 return note_integer_operands (expr);
11053 }
11054 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11055 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11056 return expr;
85498824 11057}
73f397d4
JM
11058\f
11059
11060/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11061 required. */
11062
11063tree
51eed280 11064c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11065{
11066 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11067 {
11068 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11069 /* Executing a compound literal inside a function reinitializes
11070 it. */
11071 if (!TREE_STATIC (decl))
11072 *se = true;
11073 return decl;
11074 }
11075 else
11076 return expr;
11077}
953ff289 11078\f
c0220ea4 11079/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11080
11081tree
11082c_begin_omp_parallel (void)
11083{
11084 tree block;
11085
11086 keep_next_level ();
11087 block = c_begin_compound_stmt (true);
11088
11089 return block;
11090}
11091
c2255bc4
AH
11092/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11093 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11094
953ff289 11095tree
c2255bc4 11096c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11097{
11098 tree stmt;
11099
c2255bc4 11100 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11101
11102 stmt = make_node (OMP_PARALLEL);
11103 TREE_TYPE (stmt) = void_type_node;
11104 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11105 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11106 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11107
11108 return add_stmt (stmt);
11109}
11110
a68ab351
JJ
11111/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11112
11113tree
11114c_begin_omp_task (void)
11115{
11116 tree block;
11117
11118 keep_next_level ();
11119 block = c_begin_compound_stmt (true);
11120
11121 return block;
11122}
11123
c2255bc4
AH
11124/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11125 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11126
11127tree
c2255bc4 11128c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11129{
11130 tree stmt;
11131
c2255bc4 11132 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11133
11134 stmt = make_node (OMP_TASK);
11135 TREE_TYPE (stmt) = void_type_node;
11136 OMP_TASK_CLAUSES (stmt) = clauses;
11137 OMP_TASK_BODY (stmt) = block;
c2255bc4 11138 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11139
11140 return add_stmt (stmt);
11141}
11142
acf0174b
JJ
11143/* Generate GOMP_cancel call for #pragma omp cancel. */
11144
11145void
11146c_finish_omp_cancel (location_t loc, tree clauses)
11147{
11148 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11149 int mask = 0;
11150 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11151 mask = 1;
11152 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11153 mask = 2;
11154 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11155 mask = 4;
11156 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11157 mask = 8;
11158 else
11159 {
11160 error_at (loc, "%<#pragma omp cancel must specify one of "
11161 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11162 "clauses");
11163 return;
11164 }
11165 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11166 if (ifc != NULL_TREE)
11167 {
11168 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11169 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11170 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11171 build_zero_cst (type));
11172 }
11173 else
11174 ifc = boolean_true_node;
11175 tree stmt = build_call_expr_loc (loc, fn, 2,
11176 build_int_cst (integer_type_node, mask),
11177 ifc);
11178 add_stmt (stmt);
11179}
11180
11181/* Generate GOMP_cancellation_point call for
11182 #pragma omp cancellation point. */
11183
11184void
11185c_finish_omp_cancellation_point (location_t loc, tree clauses)
11186{
11187 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11188 int mask = 0;
11189 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11190 mask = 1;
11191 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11192 mask = 2;
11193 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11194 mask = 4;
11195 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11196 mask = 8;
11197 else
11198 {
11199 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11200 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11201 "clauses");
11202 return;
11203 }
11204 tree stmt = build_call_expr_loc (loc, fn, 1,
11205 build_int_cst (integer_type_node, mask));
11206 add_stmt (stmt);
11207}
11208
11209/* Helper function for handle_omp_array_sections. Called recursively
11210 to handle multiple array-section-subscripts. C is the clause,
11211 T current expression (initially OMP_CLAUSE_DECL), which is either
11212 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11213 expression if specified, TREE_VALUE length expression if specified,
11214 TREE_CHAIN is what it has been specified after, or some decl.
11215 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11216 set to true if any of the array-section-subscript could have length
11217 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11218 first array-section-subscript which is known not to have length
11219 of one. Given say:
11220 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11221 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11222 all are or may have length of 1, array-section-subscript [:2] is the
11223 first one knonwn not to have length 1. For array-section-subscript
11224 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11225 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11226 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11227 case though, as some lengths could be zero. */
11228
11229static tree
11230handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11231 bool &maybe_zero_len, unsigned int &first_non_one)
11232{
11233 tree ret, low_bound, length, type;
11234 if (TREE_CODE (t) != TREE_LIST)
11235 {
11236 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11237 return error_mark_node;
11238 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11239 {
11240 if (DECL_P (t))
11241 error_at (OMP_CLAUSE_LOCATION (c),
11242 "%qD is not a variable in %qs clause", t,
11243 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11244 else
11245 error_at (OMP_CLAUSE_LOCATION (c),
11246 "%qE is not a variable in %qs clause", t,
11247 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11248 return error_mark_node;
11249 }
11250 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11251 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11252 {
11253 error_at (OMP_CLAUSE_LOCATION (c),
11254 "%qD is threadprivate variable in %qs clause", t,
11255 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11256 return error_mark_node;
11257 }
11258 return t;
11259 }
11260
11261 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11262 maybe_zero_len, first_non_one);
11263 if (ret == error_mark_node || ret == NULL_TREE)
11264 return ret;
11265
11266 type = TREE_TYPE (ret);
11267 low_bound = TREE_PURPOSE (t);
11268 length = TREE_VALUE (t);
11269
11270 if (low_bound == error_mark_node || length == error_mark_node)
11271 return error_mark_node;
11272
11273 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11274 {
11275 error_at (OMP_CLAUSE_LOCATION (c),
11276 "low bound %qE of array section does not have integral type",
11277 low_bound);
11278 return error_mark_node;
11279 }
11280 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11281 {
11282 error_at (OMP_CLAUSE_LOCATION (c),
11283 "length %qE of array section does not have integral type",
11284 length);
11285 return error_mark_node;
11286 }
11287 if (low_bound
11288 && TREE_CODE (low_bound) == INTEGER_CST
11289 && TYPE_PRECISION (TREE_TYPE (low_bound))
11290 > TYPE_PRECISION (sizetype))
11291 low_bound = fold_convert (sizetype, low_bound);
11292 if (length
11293 && TREE_CODE (length) == INTEGER_CST
11294 && TYPE_PRECISION (TREE_TYPE (length))
11295 > TYPE_PRECISION (sizetype))
11296 length = fold_convert (sizetype, length);
11297 if (low_bound == NULL_TREE)
11298 low_bound = integer_zero_node;
11299
11300 if (length != NULL_TREE)
11301 {
11302 if (!integer_nonzerop (length))
11303 maybe_zero_len = true;
11304 if (first_non_one == types.length ()
11305 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11306 first_non_one++;
11307 }
11308 if (TREE_CODE (type) == ARRAY_TYPE)
11309 {
11310 if (length == NULL_TREE
11311 && (TYPE_DOMAIN (type) == NULL_TREE
11312 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11313 {
11314 error_at (OMP_CLAUSE_LOCATION (c),
11315 "for unknown bound array type length expression must "
11316 "be specified");
11317 return error_mark_node;
11318 }
11319 if (TREE_CODE (low_bound) == INTEGER_CST
11320 && tree_int_cst_sgn (low_bound) == -1)
11321 {
11322 error_at (OMP_CLAUSE_LOCATION (c),
11323 "negative low bound in array section in %qs clause",
11324 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11325 return error_mark_node;
11326 }
11327 if (length != NULL_TREE
11328 && TREE_CODE (length) == INTEGER_CST
11329 && tree_int_cst_sgn (length) == -1)
11330 {
11331 error_at (OMP_CLAUSE_LOCATION (c),
11332 "negative length in array section in %qs clause",
11333 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11334 return error_mark_node;
11335 }
11336 if (TYPE_DOMAIN (type)
11337 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11338 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11339 == INTEGER_CST)
11340 {
11341 tree size = size_binop (PLUS_EXPR,
11342 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11343 size_one_node);
11344 if (TREE_CODE (low_bound) == INTEGER_CST)
11345 {
11346 if (tree_int_cst_lt (size, low_bound))
11347 {
11348 error_at (OMP_CLAUSE_LOCATION (c),
11349 "low bound %qE above array section size "
11350 "in %qs clause", low_bound,
11351 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11352 return error_mark_node;
11353 }
11354 if (tree_int_cst_equal (size, low_bound))
11355 maybe_zero_len = true;
11356 else if (length == NULL_TREE
11357 && first_non_one == types.length ()
11358 && tree_int_cst_equal
11359 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11360 low_bound))
11361 first_non_one++;
11362 }
11363 else if (length == NULL_TREE)
11364 {
11365 maybe_zero_len = true;
11366 if (first_non_one == types.length ())
11367 first_non_one++;
11368 }
11369 if (length && TREE_CODE (length) == INTEGER_CST)
11370 {
11371 if (tree_int_cst_lt (size, length))
11372 {
11373 error_at (OMP_CLAUSE_LOCATION (c),
11374 "length %qE above array section size "
11375 "in %qs clause", length,
11376 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11377 return error_mark_node;
11378 }
11379 if (TREE_CODE (low_bound) == INTEGER_CST)
11380 {
11381 tree lbpluslen
11382 = size_binop (PLUS_EXPR,
11383 fold_convert (sizetype, low_bound),
11384 fold_convert (sizetype, length));
11385 if (TREE_CODE (lbpluslen) == INTEGER_CST
11386 && tree_int_cst_lt (size, lbpluslen))
11387 {
11388 error_at (OMP_CLAUSE_LOCATION (c),
11389 "high bound %qE above array section size "
11390 "in %qs clause", lbpluslen,
11391 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11392 return error_mark_node;
11393 }
11394 }
11395 }
11396 }
11397 else if (length == NULL_TREE)
11398 {
11399 maybe_zero_len = true;
11400 if (first_non_one == types.length ())
11401 first_non_one++;
11402 }
11403
11404 /* For [lb:] we will need to evaluate lb more than once. */
11405 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11406 {
11407 tree lb = c_save_expr (low_bound);
11408 if (lb != low_bound)
11409 {
11410 TREE_PURPOSE (t) = lb;
11411 low_bound = lb;
11412 }
11413 }
11414 }
11415 else if (TREE_CODE (type) == POINTER_TYPE)
11416 {
11417 if (length == NULL_TREE)
11418 {
11419 error_at (OMP_CLAUSE_LOCATION (c),
11420 "for pointer type length expression must be specified");
11421 return error_mark_node;
11422 }
11423 /* If there is a pointer type anywhere but in the very first
11424 array-section-subscript, the array section can't be contiguous. */
11425 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11426 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11427 {
11428 error_at (OMP_CLAUSE_LOCATION (c),
11429 "array section is not contiguous in %qs clause",
11430 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11431 return error_mark_node;
11432 }
11433 }
11434 else
11435 {
11436 error_at (OMP_CLAUSE_LOCATION (c),
11437 "%qE does not have pointer or array type", ret);
11438 return error_mark_node;
11439 }
11440 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11441 types.safe_push (TREE_TYPE (ret));
11442 /* We will need to evaluate lb more than once. */
11443 tree lb = c_save_expr (low_bound);
11444 if (lb != low_bound)
11445 {
11446 TREE_PURPOSE (t) = lb;
11447 low_bound = lb;
11448 }
11449 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11450 return ret;
11451}
11452
11453/* Handle array sections for clause C. */
11454
11455static bool
11456handle_omp_array_sections (tree c)
11457{
11458 bool maybe_zero_len = false;
11459 unsigned int first_non_one = 0;
11460 vec<tree> types = vNULL;
11461 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11462 maybe_zero_len, first_non_one);
11463 if (first == error_mark_node)
11464 {
11465 types.release ();
11466 return true;
11467 }
11468 if (first == NULL_TREE)
11469 {
11470 types.release ();
11471 return false;
11472 }
11473 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11474 {
11475 tree t = OMP_CLAUSE_DECL (c);
11476 tree tem = NULL_TREE;
11477 types.release ();
11478 /* Need to evaluate side effects in the length expressions
11479 if any. */
11480 while (TREE_CODE (t) == TREE_LIST)
11481 {
11482 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11483 {
11484 if (tem == NULL_TREE)
11485 tem = TREE_VALUE (t);
11486 else
11487 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11488 TREE_VALUE (t), tem);
11489 }
11490 t = TREE_CHAIN (t);
11491 }
11492 if (tem)
11493 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11494 first = c_fully_fold (first, false, NULL);
11495 OMP_CLAUSE_DECL (c) = first;
11496 }
11497 else
11498 {
11499 unsigned int num = types.length (), i;
11500 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11501 tree condition = NULL_TREE;
11502
11503 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11504 maybe_zero_len = true;
11505
11506 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11507 t = TREE_CHAIN (t))
11508 {
11509 tree low_bound = TREE_PURPOSE (t);
11510 tree length = TREE_VALUE (t);
11511
11512 i--;
11513 if (low_bound
11514 && TREE_CODE (low_bound) == INTEGER_CST
11515 && TYPE_PRECISION (TREE_TYPE (low_bound))
11516 > TYPE_PRECISION (sizetype))
11517 low_bound = fold_convert (sizetype, low_bound);
11518 if (length
11519 && TREE_CODE (length) == INTEGER_CST
11520 && TYPE_PRECISION (TREE_TYPE (length))
11521 > TYPE_PRECISION (sizetype))
11522 length = fold_convert (sizetype, length);
11523 if (low_bound == NULL_TREE)
11524 low_bound = integer_zero_node;
11525 if (!maybe_zero_len && i > first_non_one)
11526 {
11527 if (integer_nonzerop (low_bound))
11528 goto do_warn_noncontiguous;
11529 if (length != NULL_TREE
11530 && TREE_CODE (length) == INTEGER_CST
11531 && TYPE_DOMAIN (types[i])
11532 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11533 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11534 == INTEGER_CST)
11535 {
11536 tree size;
11537 size = size_binop (PLUS_EXPR,
11538 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11539 size_one_node);
11540 if (!tree_int_cst_equal (length, size))
11541 {
11542 do_warn_noncontiguous:
11543 error_at (OMP_CLAUSE_LOCATION (c),
11544 "array section is not contiguous in %qs "
11545 "clause",
11546 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11547 types.release ();
11548 return true;
11549 }
11550 }
11551 if (length != NULL_TREE
11552 && TREE_SIDE_EFFECTS (length))
11553 {
11554 if (side_effects == NULL_TREE)
11555 side_effects = length;
11556 else
11557 side_effects = build2 (COMPOUND_EXPR,
11558 TREE_TYPE (side_effects),
11559 length, side_effects);
11560 }
11561 }
11562 else
11563 {
11564 tree l;
11565
11566 if (i > first_non_one && length && integer_nonzerop (length))
11567 continue;
11568 if (length)
11569 l = fold_convert (sizetype, length);
11570 else
11571 {
11572 l = size_binop (PLUS_EXPR,
11573 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11574 size_one_node);
11575 l = size_binop (MINUS_EXPR, l,
11576 fold_convert (sizetype, low_bound));
11577 }
11578 if (i > first_non_one)
11579 {
11580 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11581 size_zero_node);
11582 if (condition == NULL_TREE)
11583 condition = l;
11584 else
11585 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11586 l, condition);
11587 }
11588 else if (size == NULL_TREE)
11589 {
11590 size = size_in_bytes (TREE_TYPE (types[i]));
11591 size = size_binop (MULT_EXPR, size, l);
11592 if (condition)
11593 size = fold_build3 (COND_EXPR, sizetype, condition,
11594 size, size_zero_node);
11595 }
11596 else
11597 size = size_binop (MULT_EXPR, size, l);
11598 }
11599 }
11600 types.release ();
11601 if (side_effects)
11602 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11603 first = c_fully_fold (first, false, NULL);
11604 OMP_CLAUSE_DECL (c) = first;
11605 if (size)
11606 size = c_fully_fold (size, false, NULL);
11607 OMP_CLAUSE_SIZE (c) = size;
11608 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11609 return false;
11610 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11611 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11612 if (!c_mark_addressable (t))
11613 return false;
11614 OMP_CLAUSE_DECL (c2) = t;
11615 t = build_fold_addr_expr (first);
11616 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11617 tree ptr = OMP_CLAUSE_DECL (c2);
11618 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11619 ptr = build_fold_addr_expr (ptr);
11620 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11621 ptrdiff_type_node, t,
11622 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11623 ptrdiff_type_node, ptr));
11624 t = c_fully_fold (t, false, NULL);
11625 OMP_CLAUSE_SIZE (c2) = t;
11626 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11627 OMP_CLAUSE_CHAIN (c) = c2;
11628 }
11629 return false;
11630}
11631
11632/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11633 an inline call. But, remap
11634 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11635 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11636
11637static tree
11638c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11639 tree decl, tree placeholder)
11640{
11641 copy_body_data id;
11642 struct pointer_map_t *decl_map = pointer_map_create ();
11643
11644 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11645 *pointer_map_insert (decl_map, omp_decl2) = decl;
11646 memset (&id, 0, sizeof (id));
11647 id.src_fn = DECL_CONTEXT (omp_decl1);
11648 id.dst_fn = current_function_decl;
11649 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11650 id.decl_map = decl_map;
11651
11652 id.copy_decl = copy_decl_no_change;
11653 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11654 id.transform_new_cfg = true;
11655 id.transform_return_to_modify = false;
11656 id.transform_lang_insert_block = NULL;
11657 id.eh_lp_nr = 0;
11658 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11659 pointer_map_destroy (decl_map);
11660 return stmt;
11661}
11662
11663/* Helper function of c_finish_omp_clauses, called via walk_tree.
11664 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11665
11666static tree
11667c_find_omp_placeholder_r (tree *tp, int *, void *data)
11668{
11669 if (*tp == (tree) data)
11670 return *tp;
11671 return NULL_TREE;
11672}
11673
953ff289
DN
11674/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11675 Remove any elements from the list that are invalid. */
11676
11677tree
11678c_finish_omp_clauses (tree clauses)
11679{
11680 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11681 bitmap_head aligned_head;
953ff289 11682 tree c, t, *pc = &clauses;
acf0174b
JJ
11683 bool branch_seen = false;
11684 bool copyprivate_seen = false;
11685 tree *nowait_clause = NULL;
953ff289
DN
11686
11687 bitmap_obstack_initialize (NULL);
11688 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11689 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11690 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11691 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11692
11693 for (pc = &clauses, c = clauses; c ; c = *pc)
11694 {
11695 bool remove = false;
11696 bool need_complete = false;
11697 bool need_implicitly_determined = false;
11698
aaf46ef9 11699 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11700 {
11701 case OMP_CLAUSE_SHARED:
953ff289
DN
11702 need_implicitly_determined = true;
11703 goto check_dup_generic;
11704
11705 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11706 need_complete = true;
11707 need_implicitly_determined = true;
11708 goto check_dup_generic;
11709
11710 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11711 need_implicitly_determined = true;
11712 t = OMP_CLAUSE_DECL (c);
acf0174b
JJ
11713 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
11714 && FLOAT_TYPE_P (TREE_TYPE (t)))
953ff289
DN
11715 {
11716 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11717 const char *r_name = NULL;
11718
11719 switch (r_code)
11720 {
11721 case PLUS_EXPR:
11722 case MULT_EXPR:
11723 case MINUS_EXPR:
20906c66
JJ
11724 case MIN_EXPR:
11725 case MAX_EXPR:
953ff289
DN
11726 break;
11727 case BIT_AND_EXPR:
11728 r_name = "&";
11729 break;
11730 case BIT_XOR_EXPR:
11731 r_name = "^";
11732 break;
11733 case BIT_IOR_EXPR:
11734 r_name = "|";
11735 break;
11736 case TRUTH_ANDIF_EXPR:
11737 r_name = "&&";
11738 break;
11739 case TRUTH_ORIF_EXPR:
11740 r_name = "||";
11741 break;
11742 default:
11743 gcc_unreachable ();
11744 }
11745 if (r_name)
11746 {
c2255bc4
AH
11747 error_at (OMP_CLAUSE_LOCATION (c),
11748 "%qE has invalid type for %<reduction(%s)%>",
11749 t, r_name);
953ff289 11750 remove = true;
ee1d5a02 11751 break;
953ff289
DN
11752 }
11753 }
acf0174b
JJ
11754 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11755 {
11756 error_at (OMP_CLAUSE_LOCATION (c),
11757 "user defined reduction not found for %qD", t);
11758 remove = true;
ee1d5a02 11759 break;
acf0174b
JJ
11760 }
11761 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11762 {
11763 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11764 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11765 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11766 VAR_DECL, NULL_TREE, type);
11767 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11768 DECL_ARTIFICIAL (placeholder) = 1;
11769 DECL_IGNORED_P (placeholder) = 1;
11770 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11771 c_mark_addressable (placeholder);
11772 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11773 c_mark_addressable (OMP_CLAUSE_DECL (c));
11774 OMP_CLAUSE_REDUCTION_MERGE (c)
11775 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11776 TREE_VEC_ELT (list, 0),
11777 TREE_VEC_ELT (list, 1),
11778 OMP_CLAUSE_DECL (c), placeholder);
11779 OMP_CLAUSE_REDUCTION_MERGE (c)
11780 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11781 void_type_node, NULL_TREE,
11782 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11783 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11784 if (TREE_VEC_LENGTH (list) == 6)
11785 {
11786 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11787 c_mark_addressable (OMP_CLAUSE_DECL (c));
11788 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11789 c_mark_addressable (placeholder);
11790 tree init = TREE_VEC_ELT (list, 5);
11791 if (init == error_mark_node)
11792 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11793 OMP_CLAUSE_REDUCTION_INIT (c)
11794 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11795 TREE_VEC_ELT (list, 3),
11796 OMP_CLAUSE_DECL (c), placeholder);
11797 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11798 OMP_CLAUSE_REDUCTION_INIT (c)
11799 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11800 OMP_CLAUSE_REDUCTION_INIT (c));
11801 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11802 c_find_omp_placeholder_r,
11803 placeholder, NULL))
11804 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11805 }
11806 else
11807 {
11808 tree init;
11809 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11810 init = build_constructor (TREE_TYPE (t), NULL);
11811 else
11812 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11813 OMP_CLAUSE_REDUCTION_INIT (c)
11814 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11815 }
11816 OMP_CLAUSE_REDUCTION_INIT (c)
11817 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11818 void_type_node, NULL_TREE,
11819 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11820 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11821 }
953ff289
DN
11822 goto check_dup_generic;
11823
11824 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11825 copyprivate_seen = true;
11826 if (nowait_clause)
11827 {
11828 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11829 "%<nowait%> clause must not be used together "
11830 "with %<copyprivate%>");
11831 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11832 nowait_clause = NULL;
11833 }
953ff289
DN
11834 goto check_dup_generic;
11835
11836 case OMP_CLAUSE_COPYIN:
953ff289
DN
11837 t = OMP_CLAUSE_DECL (c);
11838 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11839 {
c2255bc4
AH
11840 error_at (OMP_CLAUSE_LOCATION (c),
11841 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 11842 remove = true;
ee1d5a02 11843 break;
953ff289
DN
11844 }
11845 goto check_dup_generic;
11846
acf0174b
JJ
11847 case OMP_CLAUSE_LINEAR:
11848 t = OMP_CLAUSE_DECL (c);
11849 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11850 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11851 {
11852 error_at (OMP_CLAUSE_LOCATION (c),
11853 "linear clause applied to non-integral non-pointer "
11854 "variable with type %qT", TREE_TYPE (t));
11855 remove = true;
11856 break;
11857 }
11858 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11859 {
11860 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11861 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11862 OMP_CLAUSE_DECL (c), s);
11863 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11864 sizetype, s, OMP_CLAUSE_DECL (c));
11865 if (s == error_mark_node)
11866 s = size_one_node;
11867 OMP_CLAUSE_LINEAR_STEP (c) = s;
11868 }
11869 goto check_dup_generic;
11870
953ff289
DN
11871 check_dup_generic:
11872 t = OMP_CLAUSE_DECL (c);
11873 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11874 {
c2255bc4 11875 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
11876 "%qE is not a variable in clause %qs", t,
11877 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
11878 remove = true;
11879 }
11880 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11881 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
11882 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11883 {
c2255bc4
AH
11884 error_at (OMP_CLAUSE_LOCATION (c),
11885 "%qE appears more than once in data clauses", t);
953ff289
DN
11886 remove = true;
11887 }
11888 else
11889 bitmap_set_bit (&generic_head, DECL_UID (t));
11890 break;
11891
11892 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
11893 t = OMP_CLAUSE_DECL (c);
11894 need_complete = true;
11895 need_implicitly_determined = true;
11896 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11897 {
c2255bc4
AH
11898 error_at (OMP_CLAUSE_LOCATION (c),
11899 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
11900 remove = true;
11901 }
11902 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11903 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
11904 {
c2255bc4
AH
11905 error_at (OMP_CLAUSE_LOCATION (c),
11906 "%qE appears more than once in data clauses", t);
953ff289
DN
11907 remove = true;
11908 }
11909 else
11910 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
11911 break;
11912
11913 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
11914 t = OMP_CLAUSE_DECL (c);
11915 need_complete = true;
11916 need_implicitly_determined = true;
11917 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11918 {
c2255bc4
AH
11919 error_at (OMP_CLAUSE_LOCATION (c),
11920 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
11921 remove = true;
11922 }
11923 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
11924 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
11925 {
c2255bc4
AH
11926 error_at (OMP_CLAUSE_LOCATION (c),
11927 "%qE appears more than once in data clauses", t);
953ff289
DN
11928 remove = true;
11929 }
11930 else
11931 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
11932 break;
11933
acf0174b
JJ
11934 case OMP_CLAUSE_ALIGNED:
11935 t = OMP_CLAUSE_DECL (c);
11936 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11937 {
11938 error_at (OMP_CLAUSE_LOCATION (c),
11939 "%qE is not a variable in %<aligned%> clause", t);
11940 remove = true;
11941 }
5a9785fb
JJ
11942 else if (!POINTER_TYPE_P (TREE_TYPE (t))
11943 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
11944 {
11945 error_at (OMP_CLAUSE_LOCATION (c),
11946 "%qE in %<aligned%> clause is neither a pointer nor "
11947 "an array", t);
11948 remove = true;
11949 }
acf0174b
JJ
11950 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
11951 {
11952 error_at (OMP_CLAUSE_LOCATION (c),
11953 "%qE appears more than once in %<aligned%> clauses",
11954 t);
11955 remove = true;
11956 }
11957 else
11958 bitmap_set_bit (&aligned_head, DECL_UID (t));
11959 break;
11960
11961 case OMP_CLAUSE_DEPEND:
11962 t = OMP_CLAUSE_DECL (c);
11963 if (TREE_CODE (t) == TREE_LIST)
11964 {
11965 if (handle_omp_array_sections (c))
11966 remove = true;
11967 break;
11968 }
11969 if (t == error_mark_node)
11970 remove = true;
11971 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11972 {
11973 error_at (OMP_CLAUSE_LOCATION (c),
11974 "%qE is not a variable in %<depend%> clause", t);
11975 remove = true;
11976 }
11977 else if (!c_mark_addressable (t))
11978 remove = true;
11979 break;
11980
11981 case OMP_CLAUSE_MAP:
11982 case OMP_CLAUSE_TO:
11983 case OMP_CLAUSE_FROM:
11984 t = OMP_CLAUSE_DECL (c);
11985 if (TREE_CODE (t) == TREE_LIST)
11986 {
11987 if (handle_omp_array_sections (c))
11988 remove = true;
11989 else
11990 {
11991 t = OMP_CLAUSE_DECL (c);
11992 if (!COMPLETE_TYPE_P (TREE_TYPE (t)))
11993 {
11994 error_at (OMP_CLAUSE_LOCATION (c),
11995 "array section does not have mappable type "
11996 "in %qs clause",
11997 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11998 remove = true;
11999 }
12000 }
12001 break;
12002 }
12003 if (t == error_mark_node)
12004 remove = true;
12005 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12006 {
12007 error_at (OMP_CLAUSE_LOCATION (c),
12008 "%qE is not a variable in %qs clause", t,
12009 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12010 remove = true;
12011 }
12012 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12013 {
12014 error_at (OMP_CLAUSE_LOCATION (c),
12015 "%qD is threadprivate variable in %qs clause", t,
12016 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12017 remove = true;
12018 }
12019 else if (!c_mark_addressable (t))
12020 remove = true;
12021 else if (!COMPLETE_TYPE_P (TREE_TYPE (t))
12022 && !(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12023 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER))
12024 {
12025 error_at (OMP_CLAUSE_LOCATION (c),
12026 "%qD does not have a mappable type in %qs clause", t,
12027 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12028 remove = true;
12029 }
12030 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12031 {
12032 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12033 error ("%qD appears more than once in motion clauses", t);
12034 else
12035 error ("%qD appears more than once in map clauses", t);
12036 remove = true;
12037 }
12038 else
12039 bitmap_set_bit (&generic_head, DECL_UID (t));
12040 break;
12041
12042 case OMP_CLAUSE_UNIFORM:
12043 t = OMP_CLAUSE_DECL (c);
12044 if (TREE_CODE (t) != PARM_DECL)
12045 {
12046 if (DECL_P (t))
12047 error_at (OMP_CLAUSE_LOCATION (c),
12048 "%qD is not an argument in %<uniform%> clause", t);
12049 else
12050 error_at (OMP_CLAUSE_LOCATION (c),
12051 "%qE is not an argument in %<uniform%> clause", t);
12052 remove = true;
ee1d5a02 12053 break;
acf0174b 12054 }
ee1d5a02 12055 goto check_dup_generic;
acf0174b
JJ
12056
12057 case OMP_CLAUSE_NOWAIT:
12058 if (copyprivate_seen)
12059 {
12060 error_at (OMP_CLAUSE_LOCATION (c),
12061 "%<nowait%> clause must not be used together "
12062 "with %<copyprivate%>");
12063 remove = true;
12064 break;
12065 }
12066 nowait_clause = pc;
12067 pc = &OMP_CLAUSE_CHAIN (c);
12068 continue;
12069
953ff289
DN
12070 case OMP_CLAUSE_IF:
12071 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12072 case OMP_CLAUSE_NUM_TEAMS:
12073 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12074 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12075 case OMP_CLAUSE_ORDERED:
12076 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12077 case OMP_CLAUSE_UNTIED:
12078 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12079 case OMP_CLAUSE_FINAL:
12080 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12081 case OMP_CLAUSE_SAFELEN:
12082 case OMP_CLAUSE_SIMDLEN:
12083 case OMP_CLAUSE_DEVICE:
12084 case OMP_CLAUSE_DIST_SCHEDULE:
12085 case OMP_CLAUSE_PARALLEL:
12086 case OMP_CLAUSE_FOR:
12087 case OMP_CLAUSE_SECTIONS:
12088 case OMP_CLAUSE_TASKGROUP:
12089 case OMP_CLAUSE_PROC_BIND:
12090 pc = &OMP_CLAUSE_CHAIN (c);
12091 continue;
12092
12093 case OMP_CLAUSE_INBRANCH:
12094 case OMP_CLAUSE_NOTINBRANCH:
12095 if (branch_seen)
12096 {
12097 error_at (OMP_CLAUSE_LOCATION (c),
12098 "%<inbranch%> clause is incompatible with "
12099 "%<notinbranch%>");
12100 remove = true;
12101 break;
12102 }
12103 branch_seen = true;
953ff289
DN
12104 pc = &OMP_CLAUSE_CHAIN (c);
12105 continue;
12106
12107 default:
12108 gcc_unreachable ();
12109 }
12110
12111 if (!remove)
12112 {
12113 t = OMP_CLAUSE_DECL (c);
12114
12115 if (need_complete)
12116 {
12117 t = require_complete_type (t);
12118 if (t == error_mark_node)
12119 remove = true;
12120 }
12121
12122 if (need_implicitly_determined)
12123 {
12124 const char *share_name = NULL;
12125
12126 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12127 share_name = "threadprivate";
12128 else switch (c_omp_predetermined_sharing (t))
12129 {
12130 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12131 break;
12132 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12133 /* const vars may be specified in firstprivate clause. */
12134 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12135 && TREE_READONLY (t))
12136 break;
953ff289
DN
12137 share_name = "shared";
12138 break;
12139 case OMP_CLAUSE_DEFAULT_PRIVATE:
12140 share_name = "private";
12141 break;
12142 default:
12143 gcc_unreachable ();
12144 }
12145 if (share_name)
12146 {
c2255bc4
AH
12147 error_at (OMP_CLAUSE_LOCATION (c),
12148 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12149 t, share_name,
12150 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12151 remove = true;
12152 }
12153 }
12154 }
12155
12156 if (remove)
12157 *pc = OMP_CLAUSE_CHAIN (c);
12158 else
12159 pc = &OMP_CLAUSE_CHAIN (c);
12160 }
12161
12162 bitmap_obstack_release (NULL);
12163 return clauses;
12164}
9ae165a0 12165
0a35513e
AH
12166/* Create a transaction node. */
12167
12168tree
12169c_finish_transaction (location_t loc, tree block, int flags)
12170{
12171 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12172 if (flags & TM_STMT_ATTR_OUTER)
12173 TRANSACTION_EXPR_OUTER (stmt) = 1;
12174 if (flags & TM_STMT_ATTR_RELAXED)
12175 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12176 return add_stmt (stmt);
12177}
12178
9ae165a0
DG
12179/* Make a variant type in the proper way for C/C++, propagating qualifiers
12180 down to the element type of an array. */
12181
12182tree
12183c_build_qualified_type (tree type, int type_quals)
12184{
12185 if (type == error_mark_node)
12186 return type;
12187
12188 if (TREE_CODE (type) == ARRAY_TYPE)
12189 {
12190 tree t;
12191 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12192 type_quals);
12193
12194 /* See if we already have an identically qualified type. */
12195 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12196 {
12197 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12198 && TYPE_NAME (t) == TYPE_NAME (type)
12199 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12200 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12201 TYPE_ATTRIBUTES (type)))
12202 break;
12203 }
12204 if (!t)
12205 {
12206 tree domain = TYPE_DOMAIN (type);
12207
12208 t = build_variant_type_copy (type);
12209 TREE_TYPE (t) = element_type;
12210
12211 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12212 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12213 SET_TYPE_STRUCTURAL_EQUALITY (t);
12214 else if (TYPE_CANONICAL (element_type) != element_type
12215 || (domain && TYPE_CANONICAL (domain) != domain))
12216 {
b8698a0f 12217 tree unqualified_canon
9ae165a0 12218 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12219 domain? TYPE_CANONICAL (domain)
9ae165a0 12220 : NULL_TREE);
b8698a0f 12221 TYPE_CANONICAL (t)
9ae165a0
DG
12222 = c_build_qualified_type (unqualified_canon, type_quals);
12223 }
12224 else
12225 TYPE_CANONICAL (t) = t;
12226 }
12227 return t;
12228 }
12229
12230 /* A restrict-qualified pointer type must be a pointer to object or
12231 incomplete type. Note that the use of POINTER_TYPE_P also allows
12232 REFERENCE_TYPEs, which is appropriate for C++. */
12233 if ((type_quals & TYPE_QUAL_RESTRICT)
12234 && (!POINTER_TYPE_P (type)
12235 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12236 {
12237 error ("invalid use of %<restrict%>");
12238 type_quals &= ~TYPE_QUAL_RESTRICT;
12239 }
12240
12241 return build_qualified_type (type, type_quals);
12242}
72b5577d
ILT
12243
12244/* Build a VA_ARG_EXPR for the C parser. */
12245
12246tree
c2255bc4 12247c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12248{
12249 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12250 warning_at (loc, OPT_Wc___compat,
12251 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12252 return build_va_arg (loc, expr, type);
72b5577d 12253}
acf0174b
JJ
12254
12255/* Return truthvalue of whether T1 is the same tree structure as T2.
12256 Return 1 if they are the same. Return 0 if they are different. */
12257
12258bool
12259c_tree_equal (tree t1, tree t2)
12260{
12261 enum tree_code code1, code2;
12262
12263 if (t1 == t2)
12264 return true;
12265 if (!t1 || !t2)
12266 return false;
12267
12268 for (code1 = TREE_CODE (t1);
12269 CONVERT_EXPR_CODE_P (code1)
12270 || code1 == NON_LVALUE_EXPR;
12271 code1 = TREE_CODE (t1))
12272 t1 = TREE_OPERAND (t1, 0);
12273 for (code2 = TREE_CODE (t2);
12274 CONVERT_EXPR_CODE_P (code2)
12275 || code2 == NON_LVALUE_EXPR;
12276 code2 = TREE_CODE (t2))
12277 t2 = TREE_OPERAND (t2, 0);
12278
12279 /* They might have become equal now. */
12280 if (t1 == t2)
12281 return true;
12282
12283 if (code1 != code2)
12284 return false;
12285
12286 switch (code1)
12287 {
12288 case INTEGER_CST:
12289 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
12290 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
12291
12292 case REAL_CST:
12293 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12294
12295 case STRING_CST:
12296 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12297 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12298 TREE_STRING_LENGTH (t1));
12299
12300 case FIXED_CST:
12301 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12302 TREE_FIXED_CST (t2));
12303
12304 case COMPLEX_CST:
12305 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12306 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12307
12308 case VECTOR_CST:
12309 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12310
12311 case CONSTRUCTOR:
12312 /* We need to do this when determining whether or not two
12313 non-type pointer to member function template arguments
12314 are the same. */
12315 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12316 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12317 return false;
12318 {
12319 tree field, value;
12320 unsigned int i;
12321 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12322 {
12323 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12324 if (!c_tree_equal (field, elt2->index)
12325 || !c_tree_equal (value, elt2->value))
12326 return false;
12327 }
12328 }
12329 return true;
12330
12331 case TREE_LIST:
12332 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12333 return false;
12334 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12335 return false;
12336 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12337
12338 case SAVE_EXPR:
12339 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12340
12341 case CALL_EXPR:
12342 {
12343 tree arg1, arg2;
12344 call_expr_arg_iterator iter1, iter2;
12345 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12346 return false;
12347 for (arg1 = first_call_expr_arg (t1, &iter1),
12348 arg2 = first_call_expr_arg (t2, &iter2);
12349 arg1 && arg2;
12350 arg1 = next_call_expr_arg (&iter1),
12351 arg2 = next_call_expr_arg (&iter2))
12352 if (!c_tree_equal (arg1, arg2))
12353 return false;
12354 if (arg1 || arg2)
12355 return false;
12356 return true;
12357 }
12358
12359 case TARGET_EXPR:
12360 {
12361 tree o1 = TREE_OPERAND (t1, 0);
12362 tree o2 = TREE_OPERAND (t2, 0);
12363
12364 /* Special case: if either target is an unallocated VAR_DECL,
12365 it means that it's going to be unified with whatever the
12366 TARGET_EXPR is really supposed to initialize, so treat it
12367 as being equivalent to anything. */
12368 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12369 && !DECL_RTL_SET_P (o1))
12370 /*Nop*/;
12371 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12372 && !DECL_RTL_SET_P (o2))
12373 /*Nop*/;
12374 else if (!c_tree_equal (o1, o2))
12375 return false;
12376
12377 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12378 }
12379
12380 case COMPONENT_REF:
12381 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12382 return false;
12383 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12384
12385 case PARM_DECL:
12386 case VAR_DECL:
12387 case CONST_DECL:
12388 case FIELD_DECL:
12389 case FUNCTION_DECL:
12390 case IDENTIFIER_NODE:
12391 case SSA_NAME:
12392 return false;
12393
12394 case TREE_VEC:
12395 {
12396 unsigned ix;
12397 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12398 return false;
12399 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12400 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12401 TREE_VEC_ELT (t2, ix)))
12402 return false;
12403 return true;
12404 }
12405
12406 default:
12407 break;
12408 }
12409
12410 switch (TREE_CODE_CLASS (code1))
12411 {
12412 case tcc_unary:
12413 case tcc_binary:
12414 case tcc_comparison:
12415 case tcc_expression:
12416 case tcc_vl_exp:
12417 case tcc_reference:
12418 case tcc_statement:
12419 {
12420 int i, n = TREE_OPERAND_LENGTH (t1);
12421
12422 switch (code1)
12423 {
12424 case PREINCREMENT_EXPR:
12425 case PREDECREMENT_EXPR:
12426 case POSTINCREMENT_EXPR:
12427 case POSTDECREMENT_EXPR:
12428 n = 1;
12429 break;
12430 case ARRAY_REF:
12431 n = 2;
12432 break;
12433 default:
12434 break;
12435 }
12436
12437 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12438 && n != TREE_OPERAND_LENGTH (t2))
12439 return false;
12440
12441 for (i = 0; i < n; ++i)
12442 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12443 return false;
12444
12445 return true;
12446 }
12447
12448 case tcc_type:
12449 return comptypes (t1, t2);
12450 default:
12451 gcc_unreachable ();
12452 }
12453 /* We can get here with --disable-checking. */
12454 return false;
12455}
12893402
BI
12456
12457/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12458 spawn-helper and BODY is the newly created body for FNDECL. */
12459
12460void
12461cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12462{
12463 tree list = alloc_stmt_list ();
12464 tree frame = make_cilk_frame (fndecl);
12465 tree dtor = create_cilk_function_exit (frame, false, true);
12466 add_local_decl (cfun, frame);
12467
12468 DECL_SAVED_TREE (fndecl) = list;
12469 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12470 frame);
12471 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12472 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12473
12474 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12475 append_to_statement_list (detach_expr, &body_list);
12476
12477 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12478 body = fold_build_cleanup_point_expr (void_type_node, body);
12479
12480 append_to_statement_list (body, &body_list);
12481 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12482 body_list, dtor), &list);
12483}