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