]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR target/63910 (ICE: simplify_immed_subreg, at simplify-rtx.c:5519 with -mstringo...
[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"
60393bbc
AM
43#include "predict.h"
44#include "vec.h"
45#include "hashtab.h"
46#include "hash-set.h"
47#include "machmode.h"
48#include "hard-reg-set.h"
49#include "input.h"
50#include "function.h"
2fb9a547 51#include "gimple-expr.h"
45b0be94 52#include "gimplify.h"
acf0174b 53#include "tree-inline.h"
0645c1a2 54#include "omp-low.h"
61d3ce20 55#include "c-family/c-objc.h"
a212e43f 56#include "c-family/c-common.h"
de5a5fa1 57#include "c-family/c-ubsan.h"
12893402 58#include "cilk.h"
807e902e 59#include "wide-int.h"
325c3691 60
2ac2f164
JM
61/* Possible cases of implicit bad conversions. Used to select
62 diagnostic messages in convert_for_assignment. */
63enum impl_conv {
64 ic_argpass,
65 ic_assign,
66 ic_init,
67 ic_return
68};
69
bc4b653b
JM
70/* The level of nesting inside "__alignof__". */
71int in_alignof;
72
73/* The level of nesting inside "sizeof". */
74int in_sizeof;
75
76/* The level of nesting inside "typeof". */
77int in_typeof;
400fbf9f 78
1a4049e7
JJ
79/* The argument of last parsed sizeof expression, only to be tested
80 if expr.original_code == SIZEOF_EXPR. */
81tree c_last_sizeof_arg;
82
9bac5cbb
G
83/* Nonzero if we might need to print a "missing braces around
84 initializer" message within this initializer. */
85static int found_missing_braces;
103b7b17 86
bf730f15
RS
87static int require_constant_value;
88static int require_constant_elements;
89
58f9752a 90static bool null_pointer_constant_p (const_tree);
f55ade6e 91static tree qualify_type (tree, tree);
dc5027f4
JM
92static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
93 bool *);
744aa42f 94static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
95static int function_types_compatible_p (const_tree, const_tree, bool *,
96 bool *);
97static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 98static tree lookup_field (tree, tree);
81e5eca8
MP
99static int convert_arguments (location_t, vec<location_t>, tree,
100 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
101 tree);
db3927fb 102static tree pointer_diff (location_t, tree, tree);
68fca595 103static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 104 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
105static tree valid_compound_expr_initializer (tree, tree);
106static void push_string (const char *);
107static void push_member_name (tree);
f55ade6e
AJ
108static int spelling_length (void);
109static char *print_spelling (char *);
96b40f8d 110static void warning_init (location_t, int, const char *);
c2255bc4 111static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
112static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
113 bool, struct obstack *);
a1e3b3d9 114static void output_pending_init_elements (int, struct obstack *);
ea58ef42 115static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 116static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
117static void add_pending_init (location_t, tree, tree, tree, bool,
118 struct obstack *);
a1e3b3d9
LB
119static void set_nonincremental_init (struct obstack *);
120static void set_nonincremental_init_from_string (tree, struct obstack *);
121static tree find_init_member (tree, struct obstack *);
f37acdf9 122static void readonly_warning (tree, enum lvalue_use);
7bd11157 123static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 124static void record_maybe_used_decl (tree);
dc5027f4 125static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
126\f
127/* Return true if EXP is a null pointer constant, false otherwise. */
128
129static bool
58f9752a 130null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
131{
132 /* This should really operate on c_expr structures, but they aren't
133 yet available everywhere required. */
134 tree type = TREE_TYPE (expr);
135 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 136 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
137 && integer_zerop (expr)
138 && (INTEGRAL_TYPE_P (type)
139 || (TREE_CODE (type) == POINTER_TYPE
140 && VOID_TYPE_P (TREE_TYPE (type))
141 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
142}
928c19bb
JM
143
144/* EXPR may appear in an unevaluated part of an integer constant
145 expression, but not in an evaluated part. Wrap it in a
146 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
147 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
148
149static tree
150note_integer_operands (tree expr)
151{
152 tree ret;
153 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
154 {
155 ret = copy_node (expr);
156 TREE_OVERFLOW (ret) = 1;
157 }
158 else
159 {
160 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
161 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
162 }
163 return ret;
164}
165
4d84fe7c
JM
166/* Having checked whether EXPR may appear in an unevaluated part of an
167 integer constant expression and found that it may, remove any
168 C_MAYBE_CONST_EXPR noting this fact and return the resulting
169 expression. */
170
171static inline tree
172remove_c_maybe_const_expr (tree expr)
173{
174 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
175 return C_MAYBE_CONST_EXPR_EXPR (expr);
176 else
177 return expr;
178}
179
f13c9b2c
AP
180\f/* This is a cache to hold if two types are compatible or not. */
181
182struct tagged_tu_seen_cache {
183 const struct tagged_tu_seen_cache * next;
58f9752a
KG
184 const_tree t1;
185 const_tree t2;
f13c9b2c
AP
186 /* The return value of tagged_types_tu_compatible_p if we had seen
187 these two types already. */
188 int val;
189};
190
191static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
192static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
193
400fbf9f
JW
194/* Do `exp = require_complete_type (exp);' to make sure exp
195 does not have an incomplete type. (That includes void types.) */
196
197tree
2f6e4e97 198require_complete_type (tree value)
400fbf9f
JW
199{
200 tree type = TREE_TYPE (value);
201
7a0ca710 202 if (error_operand_p (value))
ea0f786b
CB
203 return error_mark_node;
204
400fbf9f 205 /* First, detect a valid value with a complete type. */
d0f062fb 206 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
207 return value;
208
7a228918 209 c_incomplete_type_error (value, type);
400fbf9f
JW
210 return error_mark_node;
211}
212
213/* Print an error message for invalid use of an incomplete type.
214 VALUE is the expression that was used (or 0 if that isn't known)
215 and TYPE is the type that was invalid. */
216
217void
ac7d7749 218c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 219{
5d5993dd 220 const char *type_code_string;
400fbf9f
JW
221
222 /* Avoid duplicate error message. */
223 if (TREE_CODE (type) == ERROR_MARK)
224 return;
225
226 if (value != 0 && (TREE_CODE (value) == VAR_DECL
227 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 228 error ("%qD has an incomplete type", value);
400fbf9f
JW
229 else
230 {
231 retry:
232 /* We must print an error message. Be clever about what it says. */
233
234 switch (TREE_CODE (type))
235 {
236 case RECORD_TYPE:
ab87f8c8 237 type_code_string = "struct";
400fbf9f
JW
238 break;
239
240 case UNION_TYPE:
ab87f8c8 241 type_code_string = "union";
400fbf9f
JW
242 break;
243
244 case ENUMERAL_TYPE:
ab87f8c8 245 type_code_string = "enum";
400fbf9f
JW
246 break;
247
248 case VOID_TYPE:
249 error ("invalid use of void expression");
250 return;
251
252 case ARRAY_TYPE:
253 if (TYPE_DOMAIN (type))
254 {
fba78abb
RH
255 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
256 {
257 error ("invalid use of flexible array member");
258 return;
259 }
400fbf9f
JW
260 type = TREE_TYPE (type);
261 goto retry;
262 }
263 error ("invalid use of array with unspecified bounds");
264 return;
265
266 default:
366de0ce 267 gcc_unreachable ();
400fbf9f
JW
268 }
269
270 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
271 error ("invalid use of undefined type %<%s %E%>",
272 type_code_string, TYPE_NAME (type));
400fbf9f
JW
273 else
274 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 275 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
276 }
277}
278
ab393bf1
NB
279/* Given a type, apply default promotions wrt unnamed function
280 arguments and return the new type. */
281
282tree
2f6e4e97 283c_type_promotes_to (tree type)
ab393bf1 284{
267bac10 285 tree ret = NULL_TREE;
ab393bf1 286
267bac10
JM
287 if (TYPE_MAIN_VARIANT (type) == float_type_node)
288 ret = double_type_node;
289 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
290 {
291 /* Preserve unsignedness if not really getting any wider. */
8df83eae 292 if (TYPE_UNSIGNED (type)
c22cacf3 293 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
294 ret = unsigned_type_node;
295 else
296 ret = integer_type_node;
ab393bf1
NB
297 }
298
267bac10
JM
299 if (ret != NULL_TREE)
300 return (TYPE_ATOMIC (type)
301 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
302 : ret);
303
ab393bf1
NB
304 return type;
305}
306
36c5e70a
BE
307/* Return true if between two named address spaces, whether there is a superset
308 named address space that encompasses both address spaces. If there is a
309 superset, return which address space is the superset. */
310
311static bool
312addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
313{
314 if (as1 == as2)
315 {
316 *common = as1;
317 return true;
318 }
319 else if (targetm.addr_space.subset_p (as1, as2))
320 {
321 *common = as2;
322 return true;
323 }
324 else if (targetm.addr_space.subset_p (as2, as1))
325 {
326 *common = as1;
327 return true;
328 }
329 else
330 return false;
331}
332
400fbf9f
JW
333/* Return a variant of TYPE which has all the type qualifiers of LIKE
334 as well as those of TYPE. */
335
336static tree
2f6e4e97 337qualify_type (tree type, tree like)
400fbf9f 338{
36c5e70a
BE
339 addr_space_t as_type = TYPE_ADDR_SPACE (type);
340 addr_space_t as_like = TYPE_ADDR_SPACE (like);
341 addr_space_t as_common;
342
343 /* If the two named address spaces are different, determine the common
344 superset address space. If there isn't one, raise an error. */
345 if (!addr_space_superset (as_type, as_like, &as_common))
346 {
347 as_common = as_type;
348 error ("%qT and %qT are in disjoint named address spaces",
349 type, like);
350 }
351
2f6e4e97 352 return c_build_qualified_type (type,
36c5e70a 353 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 354 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 355 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 356}
52ffd86e
MS
357
358/* Return true iff the given tree T is a variable length array. */
359
360bool
ac7d7749 361c_vla_type_p (const_tree t)
52ffd86e
MS
362{
363 if (TREE_CODE (t) == ARRAY_TYPE
364 && C_TYPE_VARIABLE_SIZE (t))
365 return true;
366 return false;
367}
400fbf9f 368\f
10bc1b1b 369/* Return the composite type of two compatible types.
5305f6d7 370
10bc1b1b
JM
371 We assume that comptypes has already been done and returned
372 nonzero; if that isn't so, this may crash. In particular, we
373 assume that qualifiers match. */
400fbf9f
JW
374
375tree
10bc1b1b 376composite_type (tree t1, tree t2)
400fbf9f 377{
b3694847
SS
378 enum tree_code code1;
379 enum tree_code code2;
4b027d16 380 tree attributes;
400fbf9f
JW
381
382 /* Save time if the two types are the same. */
383
384 if (t1 == t2) return t1;
385
386 /* If one type is nonsense, use the other. */
387 if (t1 == error_mark_node)
388 return t2;
389 if (t2 == error_mark_node)
390 return t1;
391
10bc1b1b
JM
392 code1 = TREE_CODE (t1);
393 code2 = TREE_CODE (t2);
394
d9525bec 395 /* Merge the attributes. */
5fd9b178 396 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 397
10bc1b1b
JM
398 /* If one is an enumerated type and the other is the compatible
399 integer type, the composite type might be either of the two
400 (DR#013 question 3). For consistency, use the enumerated type as
401 the composite type. */
400fbf9f 402
10bc1b1b
JM
403 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
404 return t1;
405 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
406 return t2;
75326e8c 407
366de0ce 408 gcc_assert (code1 == code2);
b6a10c9f 409
400fbf9f
JW
410 switch (code1)
411 {
400fbf9f 412 case POINTER_TYPE:
10bc1b1b 413 /* For two pointers, do this recursively on the target type. */
400fbf9f 414 {
3932261a
MM
415 tree pointed_to_1 = TREE_TYPE (t1);
416 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 417 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 418 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
419 t1 = build_type_attribute_variant (t1, attributes);
420 return qualify_type (t1, t2);
400fbf9f 421 }
400fbf9f
JW
422
423 case ARRAY_TYPE:
424 {
10bc1b1b 425 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
426 int quals;
427 tree unqual_elt;
ca8bdb78
JM
428 tree d1 = TYPE_DOMAIN (t1);
429 tree d2 = TYPE_DOMAIN (t2);
430 bool d1_variable, d2_variable;
431 bool d1_zero, d2_zero;
f6294de7 432 bool t1_complete, t2_complete;
46df2823 433
de46b2fe 434 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
435 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
436 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 437
f6294de7
JM
438 t1_complete = COMPLETE_TYPE_P (t1);
439 t2_complete = COMPLETE_TYPE_P (t2);
440
ca8bdb78
JM
441 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
442 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
443
444 d1_variable = (!d1_zero
445 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
446 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
447 d2_variable = (!d2_zero
448 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
449 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
450 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
451 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 452
400fbf9f 453 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
454 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
455 && (d2_variable || d2_zero || !d1_variable))
4b027d16 456 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
457 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
458 && (d1_variable || d1_zero || !d2_variable))
4b027d16 459 return build_type_attribute_variant (t2, attributes);
c22cacf3 460
de46b2fe
AP
461 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
462 return build_type_attribute_variant (t1, attributes);
463 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
464 return build_type_attribute_variant (t2, attributes);
c22cacf3 465
46df2823
JM
466 /* Merge the element types, and have a size if either arg has
467 one. We may have qualifiers on the element types. To set
468 up TYPE_MAIN_VARIANT correctly, we need to form the
469 composite of the unqualified types and add the qualifiers
470 back at the end. */
471 quals = TYPE_QUALS (strip_array_types (elt));
472 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
473 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
474 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
475 && (d2_variable
476 || d2_zero
477 || !d1_variable))
478 ? t1
479 : t2));
f6294de7
JM
480 /* Ensure a composite type involving a zero-length array type
481 is a zero-length type not an incomplete type. */
482 if (d1_zero && d2_zero
483 && (t1_complete || t2_complete)
484 && !COMPLETE_TYPE_P (t1))
485 {
486 TYPE_SIZE (t1) = bitsize_zero_node;
487 TYPE_SIZE_UNIT (t1) = size_zero_node;
488 }
46df2823 489 t1 = c_build_qualified_type (t1, quals);
de46b2fe 490 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
491 }
492
fcb99e7b
JJ
493 case ENUMERAL_TYPE:
494 case RECORD_TYPE:
495 case UNION_TYPE:
496 if (attributes != NULL)
497 {
498 /* Try harder not to create a new aggregate type. */
499 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
500 return t1;
501 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
502 return t2;
503 }
504 return build_type_attribute_variant (t1, attributes);
505
400fbf9f
JW
506 case FUNCTION_TYPE:
507 /* Function types: prefer the one that specified arg types.
508 If both do, merge the arg types. Also merge the return types. */
509 {
10bc1b1b 510 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
511 tree p1 = TYPE_ARG_TYPES (t1);
512 tree p2 = TYPE_ARG_TYPES (t2);
513 int len;
514 tree newargs, n;
515 int i;
516
517 /* Save space: see if the result is identical to one of the args. */
3f75a254 518 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 519 return build_type_attribute_variant (t1, attributes);
3f75a254 520 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 521 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
522
523 /* Simple way if one arg fails to specify argument types. */
524 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 525 {
fe7080d2
AP
526 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
527 t1 = build_type_attribute_variant (t1, attributes);
528 return qualify_type (t1, t2);
4b027d16 529 }
400fbf9f 530 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
531 {
532 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
533 t1 = build_type_attribute_variant (t1, attributes);
534 return qualify_type (t1, t2);
4b027d16 535 }
400fbf9f
JW
536
537 /* If both args specify argument types, we must merge the two
538 lists, argument by argument. */
2f4e8f2b 539
400fbf9f
JW
540 len = list_length (p1);
541 newargs = 0;
542
543 for (i = 0; i < len; i++)
8d9bfdc5 544 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
545
546 n = newargs;
547
548 for (; p1;
549 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
550 {
551 /* A null type means arg type is not specified.
552 Take whatever the other function type has. */
553 if (TREE_VALUE (p1) == 0)
554 {
555 TREE_VALUE (n) = TREE_VALUE (p2);
556 goto parm_done;
557 }
558 if (TREE_VALUE (p2) == 0)
559 {
560 TREE_VALUE (n) = TREE_VALUE (p1);
561 goto parm_done;
562 }
2f6e4e97 563
400fbf9f
JW
564 /* Given wait (union {union wait *u; int *i} *)
565 and wait (union wait *),
566 prefer union wait * as type of parm. */
567 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
568 && TREE_VALUE (p1) != TREE_VALUE (p2))
569 {
570 tree memb;
58cb41e6
JJ
571 tree mv2 = TREE_VALUE (p2);
572 if (mv2 && mv2 != error_mark_node
573 && TREE_CODE (mv2) != ARRAY_TYPE)
574 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 575 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 576 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
577 {
578 tree mv3 = TREE_TYPE (memb);
579 if (mv3 && mv3 != error_mark_node
580 && TREE_CODE (mv3) != ARRAY_TYPE)
581 mv3 = TYPE_MAIN_VARIANT (mv3);
582 if (comptypes (mv3, mv2))
583 {
584 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
585 TREE_VALUE (p2));
c1771a20 586 pedwarn (input_location, OPT_Wpedantic,
fcf73884 587 "function types not truly compatible in ISO C");
58cb41e6
JJ
588 goto parm_done;
589 }
590 }
400fbf9f
JW
591 }
592 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
593 && TREE_VALUE (p2) != TREE_VALUE (p1))
594 {
595 tree memb;
58cb41e6
JJ
596 tree mv1 = TREE_VALUE (p1);
597 if (mv1 && mv1 != error_mark_node
598 && TREE_CODE (mv1) != ARRAY_TYPE)
599 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 600 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 601 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
602 {
603 tree mv3 = TREE_TYPE (memb);
604 if (mv3 && mv3 != error_mark_node
605 && TREE_CODE (mv3) != ARRAY_TYPE)
606 mv3 = TYPE_MAIN_VARIANT (mv3);
607 if (comptypes (mv3, mv1))
608 {
609 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
610 TREE_VALUE (p1));
c1771a20 611 pedwarn (input_location, OPT_Wpedantic,
fcf73884 612 "function types not truly compatible in ISO C");
58cb41e6
JJ
613 goto parm_done;
614 }
615 }
400fbf9f 616 }
10bc1b1b 617 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
618 parm_done: ;
619 }
620
4b027d16 621 t1 = build_function_type (valtype, newargs);
fe7080d2 622 t1 = qualify_type (t1, t2);
0f41302f 623 /* ... falls through ... */
400fbf9f
JW
624 }
625
626 default:
4b027d16 627 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
628 }
629
630}
10bc1b1b
JM
631
632/* Return the type of a conditional expression between pointers to
633 possibly differently qualified versions of compatible types.
634
635 We assume that comp_target_types has already been done and returned
636 nonzero; if that isn't so, this may crash. */
637
638static tree
639common_pointer_type (tree t1, tree t2)
640{
641 tree attributes;
46df2823
JM
642 tree pointed_to_1, mv1;
643 tree pointed_to_2, mv2;
10bc1b1b 644 tree target;
eb1387a0 645 unsigned target_quals;
36c5e70a
BE
646 addr_space_t as1, as2, as_common;
647 int quals1, quals2;
10bc1b1b
JM
648
649 /* Save time if the two types are the same. */
650
651 if (t1 == t2) return t1;
652
653 /* If one type is nonsense, use the other. */
654 if (t1 == error_mark_node)
655 return t2;
656 if (t2 == error_mark_node)
657 return t1;
658
366de0ce 659 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 660 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
661
662 /* Merge the attributes. */
663 attributes = targetm.merge_type_attributes (t1, t2);
664
665 /* Find the composite type of the target types, and combine the
46df2823
JM
666 qualifiers of the two types' targets. Do not lose qualifiers on
667 array element types by taking the TYPE_MAIN_VARIANT. */
668 mv1 = pointed_to_1 = TREE_TYPE (t1);
669 mv2 = pointed_to_2 = TREE_TYPE (t2);
670 if (TREE_CODE (mv1) != ARRAY_TYPE)
671 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
672 if (TREE_CODE (mv2) != ARRAY_TYPE)
673 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
674 target = composite_type (mv1, mv2);
eb1387a0
RG
675
676 /* For function types do not merge const qualifiers, but drop them
677 if used inconsistently. The middle-end uses these to mark const
678 and noreturn functions. */
36c5e70a
BE
679 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
680 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
681
eb1387a0 682 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 683 target_quals = (quals1 & quals2);
eb1387a0 684 else
36c5e70a
BE
685 target_quals = (quals1 | quals2);
686
687 /* If the two named address spaces are different, determine the common
688 superset address space. This is guaranteed to exist due to the
689 assumption that comp_target_type returned non-zero. */
690 as1 = TYPE_ADDR_SPACE (pointed_to_1);
691 as2 = TYPE_ADDR_SPACE (pointed_to_2);
692 if (!addr_space_superset (as1, as2, &as_common))
693 gcc_unreachable ();
694
695 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
696
eb1387a0 697 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
698 return build_type_attribute_variant (t1, attributes);
699}
700
701/* Return the common type for two arithmetic types under the usual
702 arithmetic conversions. The default conversions have already been
703 applied, and enumerated types converted to their compatible integer
704 types. The resulting type is unqualified and has no attributes.
705
706 This is the type for the result of most arithmetic operations
707 if the operands have the given two types. */
708
ccf7f880
JJ
709static tree
710c_common_type (tree t1, tree t2)
10bc1b1b
JM
711{
712 enum tree_code code1;
713 enum tree_code code2;
714
715 /* If one type is nonsense, use the other. */
716 if (t1 == error_mark_node)
717 return t2;
718 if (t2 == error_mark_node)
719 return t1;
720
721 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
722 t1 = TYPE_MAIN_VARIANT (t1);
723
724 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
725 t2 = TYPE_MAIN_VARIANT (t2);
726
727 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
728 t1 = build_type_attribute_variant (t1, NULL_TREE);
729
730 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
731 t2 = build_type_attribute_variant (t2, NULL_TREE);
732
733 /* Save time if the two types are the same. */
734
735 if (t1 == t2) return t1;
736
737 code1 = TREE_CODE (t1);
738 code2 = TREE_CODE (t2);
739
366de0ce 740 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
741 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
742 || code1 == INTEGER_TYPE);
366de0ce 743 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
744 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
745 || code2 == INTEGER_TYPE);
10bc1b1b 746
5fc89bfd
JJ
747 /* When one operand is a decimal float type, the other operand cannot be
748 a generic float type or a complex type. We also disallow vector types
749 here. */
750 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
751 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
752 {
753 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
754 {
755 error ("can%'t mix operands of decimal float and vector types");
756 return error_mark_node;
757 }
758 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
759 {
760 error ("can%'t mix operands of decimal float and complex types");
761 return error_mark_node;
762 }
763 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
764 {
765 error ("can%'t mix operands of decimal float and other float types");
766 return error_mark_node;
767 }
768 }
769
10bc1b1b
JM
770 /* If one type is a vector type, return that type. (How the usual
771 arithmetic conversions apply to the vector types extension is not
772 precisely specified.) */
773 if (code1 == VECTOR_TYPE)
774 return t1;
775
776 if (code2 == VECTOR_TYPE)
777 return t2;
778
779 /* If one type is complex, form the common type of the non-complex
780 components, then make that complex. Use T1 or T2 if it is the
781 required type. */
782 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
783 {
784 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
785 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 786 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
787
788 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
789 return t1;
790 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
791 return t2;
792 else
793 return build_complex_type (subtype);
794 }
795
796 /* If only one is real, use it as the result. */
797
798 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
799 return t1;
800
801 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
802 return t2;
803
9a8ce21f
JG
804 /* If both are real and either are decimal floating point types, use
805 the decimal floating point type with the greater precision. */
806
807 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
808 {
809 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
810 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
811 return dfloat128_type_node;
812 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
813 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
814 return dfloat64_type_node;
815 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
816 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
817 return dfloat32_type_node;
818 }
819
ab22c1fa
CF
820 /* Deal with fixed-point types. */
821 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
822 {
823 unsigned int unsignedp = 0, satp = 0;
ef4bddc2 824 machine_mode m1, m2;
ab22c1fa
CF
825 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
826
827 m1 = TYPE_MODE (t1);
828 m2 = TYPE_MODE (t2);
829
830 /* If one input type is saturating, the result type is saturating. */
831 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
832 satp = 1;
833
834 /* If both fixed-point types are unsigned, the result type is unsigned.
835 When mixing fixed-point and integer types, follow the sign of the
836 fixed-point type.
837 Otherwise, the result type is signed. */
838 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
839 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
840 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
841 && TYPE_UNSIGNED (t1))
842 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
843 && TYPE_UNSIGNED (t2)))
844 unsignedp = 1;
845
846 /* The result type is signed. */
847 if (unsignedp == 0)
848 {
849 /* If the input type is unsigned, we need to convert to the
850 signed type. */
851 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
852 {
d75d71e0 853 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
854 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
855 mclass = MODE_FRACT;
856 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
857 mclass = MODE_ACCUM;
858 else
859 gcc_unreachable ();
860 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
861 }
862 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
863 {
d75d71e0 864 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
865 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
866 mclass = MODE_FRACT;
867 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
868 mclass = MODE_ACCUM;
869 else
870 gcc_unreachable ();
871 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
872 }
873 }
874
875 if (code1 == FIXED_POINT_TYPE)
876 {
877 fbit1 = GET_MODE_FBIT (m1);
878 ibit1 = GET_MODE_IBIT (m1);
879 }
880 else
881 {
882 fbit1 = 0;
883 /* Signed integers need to subtract one sign bit. */
884 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
885 }
886
887 if (code2 == FIXED_POINT_TYPE)
888 {
889 fbit2 = GET_MODE_FBIT (m2);
890 ibit2 = GET_MODE_IBIT (m2);
891 }
892 else
893 {
894 fbit2 = 0;
895 /* Signed integers need to subtract one sign bit. */
896 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
897 }
898
899 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
900 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
901 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
902 satp);
903 }
904
10bc1b1b
JM
905 /* Both real or both integers; use the one with greater precision. */
906
907 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
908 return t1;
909 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
910 return t2;
911
912 /* Same precision. Prefer long longs to longs to ints when the
913 same precision, following the C99 rules on integer type rank
914 (which are equivalent to the C90 rules for C90 types). */
915
916 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
918 return long_long_unsigned_type_node;
919
920 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
922 {
923 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
924 return long_long_unsigned_type_node;
925 else
c22cacf3 926 return long_long_integer_type_node;
10bc1b1b
JM
927 }
928
929 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
930 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
931 return long_unsigned_type_node;
932
933 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
934 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
935 {
936 /* But preserve unsignedness from the other type,
937 since long cannot hold all the values of an unsigned int. */
938 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
939 return long_unsigned_type_node;
940 else
941 return long_integer_type_node;
942 }
943
944 /* Likewise, prefer long double to double even if same size. */
945 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
946 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
947 return long_double_type_node;
948
2531a1d9
JR
949 /* Likewise, prefer double to float even if same size.
950 We got a couple of embedded targets with 32 bit doubles, and the
951 pdp11 might have 64 bit floats. */
952 if (TYPE_MAIN_VARIANT (t1) == double_type_node
953 || TYPE_MAIN_VARIANT (t2) == double_type_node)
954 return double_type_node;
955
10bc1b1b
JM
956 /* Otherwise prefer the unsigned one. */
957
958 if (TYPE_UNSIGNED (t1))
959 return t1;
960 else
961 return t2;
962}
400fbf9f 963\f
5922c215
JM
964/* Wrapper around c_common_type that is used by c-common.c and other
965 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
966 are allowed here and are converted to their compatible integer types.
967 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
968 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
969tree
970common_type (tree t1, tree t2)
971{
972 if (TREE_CODE (t1) == ENUMERAL_TYPE)
973 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
974 if (TREE_CODE (t2) == ENUMERAL_TYPE)
975 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
976
977 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
978 if (TREE_CODE (t1) == BOOLEAN_TYPE
979 && TREE_CODE (t2) == BOOLEAN_TYPE)
980 return boolean_type_node;
981
982 /* If either type is BOOLEAN_TYPE, then return the other. */
983 if (TREE_CODE (t1) == BOOLEAN_TYPE)
984 return t2;
985 if (TREE_CODE (t2) == BOOLEAN_TYPE)
986 return t1;
987
ccf7f880
JJ
988 return c_common_type (t1, t2);
989}
f13c9b2c 990
400fbf9f
JW
991/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
992 or various other operations. Return 2 if they are compatible
993 but a warning may be needed if you use them together. */
994
995int
132da1a5 996comptypes (tree type1, tree type2)
f13c9b2c
AP
997{
998 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
999 int val;
1000
dc5027f4 1001 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
1002 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1003
1004 return val;
1005}
1006
1007/* Like comptypes, but if it returns non-zero because enum and int are
1008 compatible, it sets *ENUM_AND_INT_P to true. */
1009
1010static int
1011comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1012{
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1015
dc5027f4
JM
1016 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1018
1019 return val;
1020}
1021
1022/* Like comptypes, but if it returns nonzero for different types, it
1023 sets *DIFFERENT_TYPES_P to true. */
1024
1025int
1026comptypes_check_different_types (tree type1, tree type2,
1027 bool *different_types_p)
1028{
1029 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1030 int val;
1031
1032 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1033 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1034
f13c9b2c 1035 return val;
c22cacf3
MS
1036}
1037\f
f13c9b2c
AP
1038/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1039 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1040 but a warning may be needed if you use them together. If
1041 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1042 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1043 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1044 NULL, and the types are compatible but different enough not to be
48b0b196 1045 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1046 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1047 false, but may or may not be set if the types are incompatible.
1048 This differs from comptypes, in that we don't free the seen
1049 types. */
f13c9b2c
AP
1050
1051static int
dc5027f4
JM
1052comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1053 bool *different_types_p)
400fbf9f 1054{
58f9752a
KG
1055 const_tree t1 = type1;
1056 const_tree t2 = type2;
4b027d16 1057 int attrval, val;
400fbf9f
JW
1058
1059 /* Suppress errors caused by previously reported errors. */
1060
8d47dfc5
RH
1061 if (t1 == t2 || !t1 || !t2
1062 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1063 return 1;
1064
bca63328
JM
1065 /* Enumerated types are compatible with integer types, but this is
1066 not transitive: two enumerated types in the same translation unit
1067 are compatible with each other only if they are the same type. */
400fbf9f 1068
bca63328 1069 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1070 {
1071 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1072 if (TREE_CODE (t2) != VOID_TYPE)
1073 {
1074 if (enum_and_int_p != NULL)
1075 *enum_and_int_p = true;
1076 if (different_types_p != NULL)
1077 *different_types_p = true;
1078 }
744aa42f 1079 }
bca63328 1080 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1081 {
1082 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1083 if (TREE_CODE (t1) != VOID_TYPE)
1084 {
1085 if (enum_and_int_p != NULL)
1086 *enum_and_int_p = true;
1087 if (different_types_p != NULL)
1088 *different_types_p = true;
1089 }
744aa42f 1090 }
400fbf9f
JW
1091
1092 if (t1 == t2)
1093 return 1;
1094
1095 /* Different classes of types can't be compatible. */
1096
3aeb3655
EC
1097 if (TREE_CODE (t1) != TREE_CODE (t2))
1098 return 0;
400fbf9f 1099
118a3a8b 1100 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1101
3932261a 1102 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1103 return 0;
1104
08632da2
RS
1105 /* Allow for two different type nodes which have essentially the same
1106 definition. Note that we already checked for equality of the type
38e01259 1107 qualifiers (just above). */
400fbf9f 1108
46df2823
JM
1109 if (TREE_CODE (t1) != ARRAY_TYPE
1110 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1111 return 1;
1112
4b027d16 1113 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1114 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1115 return 0;
1116
1117 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1118 val = 0;
1119
400fbf9f
JW
1120 switch (TREE_CODE (t1))
1121 {
1122 case POINTER_TYPE:
106f5de5
UW
1123 /* Do not remove mode or aliasing information. */
1124 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1125 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1126 break;
4b027d16 1127 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1128 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1129 enum_and_int_p, different_types_p));
4b027d16 1130 break;
400fbf9f
JW
1131
1132 case FUNCTION_TYPE:
dc5027f4
JM
1133 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1134 different_types_p);
4b027d16 1135 break;
400fbf9f
JW
1136
1137 case ARRAY_TYPE:
1138 {
400fbf9f
JW
1139 tree d1 = TYPE_DOMAIN (t1);
1140 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1141 bool d1_variable, d2_variable;
1142 bool d1_zero, d2_zero;
4b027d16 1143 val = 1;
400fbf9f
JW
1144
1145 /* Target types must match incl. qualifiers. */
1146 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1147 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1148 enum_and_int_p,
1149 different_types_p)))
400fbf9f
JW
1150 return 0;
1151
dc5027f4
JM
1152 if (different_types_p != NULL
1153 && (d1 == 0) != (d2 == 0))
1154 *different_types_p = true;
400fbf9f 1155 /* Sizes must match unless one is missing or variable. */
3f85558f 1156 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1157 break;
400fbf9f 1158
3f75a254
JM
1159 d1_zero = !TYPE_MAX_VALUE (d1);
1160 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1161
3f75a254 1162 d1_variable = (!d1_zero
3f85558f
RH
1163 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1164 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1165 d2_variable = (!d2_zero
3f85558f
RH
1166 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1167 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1168 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1169 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1170
dc5027f4
JM
1171 if (different_types_p != NULL
1172 && d1_variable != d2_variable)
1173 *different_types_p = true;
3f85558f
RH
1174 if (d1_variable || d2_variable)
1175 break;
1176 if (d1_zero && d2_zero)
1177 break;
1178 if (d1_zero || d2_zero
3f75a254
JM
1179 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1180 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1181 val = 0;
1182
c22cacf3 1183 break;
400fbf9f
JW
1184 }
1185
d1bd0ded 1186 case ENUMERAL_TYPE:
58393038 1187 case RECORD_TYPE:
d1bd0ded 1188 case UNION_TYPE:
766beae1 1189 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1190 {
fcb99e7b
JJ
1191 tree a1 = TYPE_ATTRIBUTES (t1);
1192 tree a2 = TYPE_ATTRIBUTES (t2);
1193
1194 if (! attribute_list_contained (a1, a2)
1195 && ! attribute_list_contained (a2, a1))
1196 break;
1197
f13c9b2c 1198 if (attrval != 2)
dc5027f4
JM
1199 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1200 different_types_p);
1201 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1202 different_types_p);
f13c9b2c 1203 }
4b027d16 1204 break;
e9a25f70 1205
62e1dfcf 1206 case VECTOR_TYPE:
744aa42f
ILT
1207 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1208 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1209 enum_and_int_p, different_types_p));
62e1dfcf
NC
1210 break;
1211
e9a25f70
JL
1212 default:
1213 break;
400fbf9f 1214 }
4b027d16 1215 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1216}
1217
36c5e70a
BE
1218/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1219 their qualifiers, except for named address spaces. If the pointers point to
1220 different named addresses, then we must determine if one address space is a
1221 subset of the other. */
400fbf9f
JW
1222
1223static int
744aa42f 1224comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1225{
392202b0 1226 int val;
36c5e70a
BE
1227 tree mvl = TREE_TYPE (ttl);
1228 tree mvr = TREE_TYPE (ttr);
1229 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1230 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1231 addr_space_t as_common;
744aa42f 1232 bool enum_and_int_p;
8b40563c 1233
36c5e70a
BE
1234 /* Fail if pointers point to incompatible address spaces. */
1235 if (!addr_space_superset (asl, asr, &as_common))
1236 return 0;
1237
46df2823
JM
1238 /* Do not lose qualifiers on element types of array types that are
1239 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1240 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1241 mvl = (TYPE_ATOMIC (mvl)
1242 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1243 : TYPE_MAIN_VARIANT (mvl));
46df2823 1244 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1245 mvr = (TYPE_ATOMIC (mvr)
1246 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1247 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1248 enum_and_int_p = false;
1249 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1250
fcf73884 1251 if (val == 2)
c1771a20 1252 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1253
1254 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1255 warning_at (location, OPT_Wc___compat,
1256 "pointer target types incompatible in C++");
1257
400fbf9f
JW
1258 return val;
1259}
1260\f
1261/* Subroutines of `comptypes'. */
1262
f75fbaf7
ZW
1263/* Determine whether two trees derive from the same translation unit.
1264 If the CONTEXT chain ends in a null, that tree's context is still
1265 being parsed, so if two trees have context chains ending in null,
766beae1 1266 they're in the same translation unit. */
f75fbaf7 1267int
58f9752a 1268same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1269{
1270 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1271 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1272 {
6615c446
JO
1273 case tcc_declaration:
1274 t1 = DECL_CONTEXT (t1); break;
1275 case tcc_type:
1276 t1 = TYPE_CONTEXT (t1); break;
1277 case tcc_exceptional:
1278 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1279 default: gcc_unreachable ();
766beae1
ZW
1280 }
1281
1282 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1283 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1284 {
6615c446
JO
1285 case tcc_declaration:
1286 t2 = DECL_CONTEXT (t2); break;
1287 case tcc_type:
1288 t2 = TYPE_CONTEXT (t2); break;
1289 case tcc_exceptional:
1290 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1291 default: gcc_unreachable ();
766beae1
ZW
1292 }
1293
1294 return t1 == t2;
1295}
1296
f13c9b2c 1297/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1298
f13c9b2c 1299static struct tagged_tu_seen_cache *
58f9752a 1300alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1301{
cceb1885 1302 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1303 tu->next = tagged_tu_seen_base;
1304 tu->t1 = t1;
1305 tu->t2 = t2;
c22cacf3 1306
f13c9b2c 1307 tagged_tu_seen_base = tu;
c22cacf3 1308
f13c9b2c
AP
1309 /* The C standard says that two structures in different translation
1310 units are compatible with each other only if the types of their
1311 fields are compatible (among other things). We assume that they
1312 are compatible until proven otherwise when building the cache.
1313 An example where this can occur is:
1314 struct a
1315 {
1316 struct a *next;
1317 };
1318 If we are comparing this against a similar struct in another TU,
c83eecad 1319 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1320 loop. */
1321 tu->val = 1;
1322 return tu;
1323}
d1bd0ded 1324
f13c9b2c 1325/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1326
f13c9b2c
AP
1327static void
1328free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1329{
1330 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1331 while (tu != tu_til)
1332 {
741ac903
KG
1333 const struct tagged_tu_seen_cache *const tu1
1334 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1335 tu = tu1->next;
b1d5455a 1336 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1337 }
1338 tagged_tu_seen_base = tu_til;
1339}
d1bd0ded
GK
1340
1341/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1342 compatible. If the two types are not the same (which has been
1343 checked earlier), this can only happen when multiple translation
1344 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1345 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1346 comptypes_internal. */
d1bd0ded
GK
1347
1348static int
744aa42f 1349tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1350 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1351{
1352 tree s1, s2;
1353 bool needs_warning = false;
3aeb3655 1354
d1bd0ded
GK
1355 /* We have to verify that the tags of the types are the same. This
1356 is harder than it looks because this may be a typedef, so we have
1357 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1358 typedef...
1359 In the case of compiler-created builtin structs the TYPE_DECL
1360 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1361 while (TYPE_NAME (t1)
1362 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1363 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1364 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1365
dea984dc
ILT
1366 while (TYPE_NAME (t2)
1367 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1368 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1369 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1370
1371 /* C90 didn't have the requirement that the two tags be the same. */
1372 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1373 return 0;
3aeb3655 1374
d1bd0ded
GK
1375 /* C90 didn't say what happened if one or both of the types were
1376 incomplete; we choose to follow C99 rules here, which is that they
1377 are compatible. */
1378 if (TYPE_SIZE (t1) == NULL
1379 || TYPE_SIZE (t2) == NULL)
1380 return 1;
3aeb3655 1381
d1bd0ded 1382 {
f13c9b2c 1383 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1384 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1385 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1386 return tts_i->val;
d1bd0ded 1387 }
3aeb3655 1388
d1bd0ded
GK
1389 switch (TREE_CODE (t1))
1390 {
1391 case ENUMERAL_TYPE:
1392 {
f13c9b2c 1393 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1394 /* Speed up the case where the type values are in the same order. */
1395 tree tv1 = TYPE_VALUES (t1);
1396 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1397
c22cacf3 1398 if (tv1 == tv2)
f13c9b2c
AP
1399 {
1400 return 1;
1401 }
3aeb3655 1402
c22cacf3
MS
1403 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1404 {
1405 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1406 break;
1407 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1408 {
c22cacf3 1409 tu->val = 0;
f13c9b2c
AP
1410 return 0;
1411 }
c22cacf3 1412 }
3aeb3655 1413
c22cacf3 1414 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1415 {
1416 return 1;
1417 }
c22cacf3 1418 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1419 {
1420 tu->val = 0;
1421 return 0;
1422 }
3aeb3655 1423
d1bd0ded 1424 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1425 {
1426 tu->val = 0;
1427 return 0;
1428 }
3aeb3655 1429
d1bd0ded
GK
1430 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1431 {
1432 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1433 if (s2 == NULL
1434 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1435 {
1436 tu->val = 0;
1437 return 0;
1438 }
d1bd0ded
GK
1439 }
1440 return 1;
1441 }
1442
1443 case UNION_TYPE:
1444 {
f13c9b2c 1445 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1446 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1447 {
1448 tu->val = 0;
1449 return 0;
1450 }
c22cacf3 1451
f13c9b2c
AP
1452 /* Speed up the common case where the fields are in the same order. */
1453 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1454 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1455 {
1456 int result;
c22cacf3 1457
3ae4d3cc 1458 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1459 break;
744aa42f 1460 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1461 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1462
1463 if (result != 1 && !DECL_NAME (s1))
1464 break;
f13c9b2c
AP
1465 if (result == 0)
1466 {
1467 tu->val = 0;
1468 return 0;
1469 }
1470 if (result == 2)
1471 needs_warning = true;
1472
1473 if (TREE_CODE (s1) == FIELD_DECL
1474 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1475 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1476 {
1477 tu->val = 0;
1478 return 0;
1479 }
1480 }
1481 if (!s1 && !s2)
1482 {
1483 tu->val = needs_warning ? 2 : 1;
1484 return tu->val;
1485 }
d1bd0ded 1486
910ad8de 1487 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1488 {
1489 bool ok = false;
3aeb3655 1490
910ad8de 1491 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1492 if (DECL_NAME (s1) == DECL_NAME (s2))
1493 {
1494 int result;
1495
744aa42f 1496 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1497 enum_and_int_p,
1498 different_types_p);
3ae4d3cc
AO
1499
1500 if (result != 1 && !DECL_NAME (s1))
1501 continue;
1502 if (result == 0)
1503 {
1504 tu->val = 0;
1505 return 0;
1506 }
1507 if (result == 2)
1508 needs_warning = true;
1509
1510 if (TREE_CODE (s1) == FIELD_DECL
1511 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1512 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1513 break;
3ae4d3cc
AO
1514
1515 ok = true;
1516 break;
1517 }
3f75a254 1518 if (!ok)
f13c9b2c
AP
1519 {
1520 tu->val = 0;
1521 return 0;
1522 }
d1bd0ded 1523 }
f13c9b2c
AP
1524 tu->val = needs_warning ? 2 : 10;
1525 return tu->val;
d1bd0ded
GK
1526 }
1527
1528 case RECORD_TYPE:
1529 {
c22cacf3 1530 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1531
1532 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1533 s1 && s2;
910ad8de 1534 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1535 {
1536 int result;
1537 if (TREE_CODE (s1) != TREE_CODE (s2)
1538 || DECL_NAME (s1) != DECL_NAME (s2))
1539 break;
744aa42f 1540 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1541 enum_and_int_p, different_types_p);
d1bd0ded
GK
1542 if (result == 0)
1543 break;
1544 if (result == 2)
1545 needs_warning = true;
3aeb3655 1546
d1bd0ded
GK
1547 if (TREE_CODE (s1) == FIELD_DECL
1548 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1549 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1550 break;
1551 }
d1bd0ded 1552 if (s1 && s2)
f13c9b2c
AP
1553 tu->val = 0;
1554 else
1555 tu->val = needs_warning ? 2 : 1;
1556 return tu->val;
d1bd0ded
GK
1557 }
1558
1559 default:
366de0ce 1560 gcc_unreachable ();
d1bd0ded
GK
1561 }
1562}
1563
400fbf9f
JW
1564/* Return 1 if two function types F1 and F2 are compatible.
1565 If either type specifies no argument types,
1566 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1567 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1568 the other must specify that number of self-promoting arg types.
744aa42f 1569 Otherwise, the argument types must match.
dc5027f4 1570 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1571
1572static int
744aa42f 1573function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1574 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1575{
1576 tree args1, args2;
1577 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1578 int val = 1;
1579 int val1;
a6fdc086
GK
1580 tree ret1, ret2;
1581
1582 ret1 = TREE_TYPE (f1);
1583 ret2 = TREE_TYPE (f2);
1584
e508a019
JM
1585 /* 'volatile' qualifiers on a function's return type used to mean
1586 the function is noreturn. */
1587 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1588 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1589 if (TYPE_VOLATILE (ret1))
1590 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1591 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1592 if (TYPE_VOLATILE (ret2))
1593 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1594 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1595 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1596 if (val == 0)
400fbf9f
JW
1597 return 0;
1598
1599 args1 = TYPE_ARG_TYPES (f1);
1600 args2 = TYPE_ARG_TYPES (f2);
1601
dc5027f4
JM
1602 if (different_types_p != NULL
1603 && (args1 == 0) != (args2 == 0))
1604 *different_types_p = true;
1605
400fbf9f
JW
1606 /* An unspecified parmlist matches any specified parmlist
1607 whose argument types don't need default promotions. */
1608
1609 if (args1 == 0)
1610 {
1611 if (!self_promoting_args_p (args2))
1612 return 0;
1613 /* If one of these types comes from a non-prototype fn definition,
1614 compare that with the other type's arglist.
3176a0c2 1615 If they don't match, ask for a warning (but no error). */
400fbf9f 1616 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1617 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1618 enum_and_int_p, different_types_p))
400fbf9f
JW
1619 val = 2;
1620 return val;
1621 }
1622 if (args2 == 0)
1623 {
1624 if (!self_promoting_args_p (args1))
1625 return 0;
1626 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1627 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1628 enum_and_int_p, different_types_p))
400fbf9f
JW
1629 val = 2;
1630 return val;
1631 }
1632
1633 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1634 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1635 different_types_p);
400fbf9f
JW
1636 return val1 != 1 ? val1 : val;
1637}
1638
744aa42f
ILT
1639/* Check two lists of types for compatibility, returning 0 for
1640 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1641 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1642 comptypes_internal. */
400fbf9f
JW
1643
1644static int
744aa42f 1645type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1646 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1647{
1648 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1649 int val = 1;
9d5f3e49 1650 int newval = 0;
400fbf9f
JW
1651
1652 while (1)
1653 {
46df2823 1654 tree a1, mv1, a2, mv2;
400fbf9f
JW
1655 if (args1 == 0 && args2 == 0)
1656 return val;
1657 /* If one list is shorter than the other,
1658 they fail to match. */
1659 if (args1 == 0 || args2 == 0)
1660 return 0;
46df2823
JM
1661 mv1 = a1 = TREE_VALUE (args1);
1662 mv2 = a2 = TREE_VALUE (args2);
1663 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1664 mv1 = (TYPE_ATOMIC (mv1)
1665 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1666 TYPE_QUAL_ATOMIC)
1667 : TYPE_MAIN_VARIANT (mv1));
46df2823 1668 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1669 mv2 = (TYPE_ATOMIC (mv2)
1670 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1671 TYPE_QUAL_ATOMIC)
1672 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1673 /* A null pointer instead of a type
1674 means there is supposed to be an argument
1675 but nothing is specified about what type it has.
1676 So match anything that self-promotes. */
dc5027f4
JM
1677 if (different_types_p != NULL
1678 && (a1 == 0) != (a2 == 0))
1679 *different_types_p = true;
46df2823 1680 if (a1 == 0)
400fbf9f 1681 {
46df2823 1682 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1683 return 0;
1684 }
46df2823 1685 else if (a2 == 0)
400fbf9f 1686 {
46df2823 1687 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1688 return 0;
1689 }
8f5b6d29 1690 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1691 else if (TREE_CODE (a1) == ERROR_MARK
1692 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1693 ;
dc5027f4
JM
1694 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1695 different_types_p)))
400fbf9f 1696 {
dc5027f4
JM
1697 if (different_types_p != NULL)
1698 *different_types_p = true;
400fbf9f
JW
1699 /* Allow wait (union {union wait *u; int *i} *)
1700 and wait (union wait *) to be compatible. */
46df2823
JM
1701 if (TREE_CODE (a1) == UNION_TYPE
1702 && (TYPE_NAME (a1) == 0
ebf0bf7f 1703 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1704 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1705 && tree_int_cst_equal (TYPE_SIZE (a1),
1706 TYPE_SIZE (a2)))
400fbf9f
JW
1707 {
1708 tree memb;
46df2823 1709 for (memb = TYPE_FIELDS (a1);
910ad8de 1710 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1711 {
1712 tree mv3 = TREE_TYPE (memb);
1713 if (mv3 && mv3 != error_mark_node
1714 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1715 mv3 = (TYPE_ATOMIC (mv3)
1716 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1717 TYPE_QUAL_ATOMIC)
1718 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1719 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1720 different_types_p))
58cb41e6
JJ
1721 break;
1722 }
400fbf9f
JW
1723 if (memb == 0)
1724 return 0;
1725 }
46df2823
JM
1726 else if (TREE_CODE (a2) == UNION_TYPE
1727 && (TYPE_NAME (a2) == 0
ebf0bf7f 1728 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1729 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1730 && tree_int_cst_equal (TYPE_SIZE (a2),
1731 TYPE_SIZE (a1)))
400fbf9f
JW
1732 {
1733 tree memb;
46df2823 1734 for (memb = TYPE_FIELDS (a2);
910ad8de 1735 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1736 {
1737 tree mv3 = TREE_TYPE (memb);
1738 if (mv3 && mv3 != error_mark_node
1739 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1740 mv3 = (TYPE_ATOMIC (mv3)
1741 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1742 TYPE_QUAL_ATOMIC)
1743 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1744 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1745 different_types_p))
58cb41e6
JJ
1746 break;
1747 }
400fbf9f
JW
1748 if (memb == 0)
1749 return 0;
1750 }
1751 else
1752 return 0;
1753 }
1754
1755 /* comptypes said ok, but record if it said to warn. */
1756 if (newval > val)
1757 val = newval;
1758
1759 args1 = TREE_CHAIN (args1);
1760 args2 = TREE_CHAIN (args2);
1761 }
1762}
400fbf9f 1763\f
a0e24419
MP
1764/* Compute the size to increment a pointer by. When a function type or void
1765 type or incomplete type is passed, size_one_node is returned.
1766 This function does not emit any diagnostics; the caller is responsible
1767 for that. */
400fbf9f 1768
4e2fb7de 1769static tree
58f9752a 1770c_size_in_bytes (const_tree type)
400fbf9f
JW
1771{
1772 enum tree_code code = TREE_CODE (type);
1773
a0e24419
MP
1774 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1775 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1776 return size_one_node;
1777
400fbf9f 1778 /* Convert in case a char is more than one unit. */
db3927fb
AH
1779 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1780 size_int (TYPE_PRECISION (char_type_node)
1781 / BITS_PER_UNIT));
400fbf9f 1782}
400fbf9f 1783\f
400fbf9f
JW
1784/* Return either DECL or its known constant value (if it has one). */
1785
56cb9733 1786tree
2f6e4e97 1787decl_constant_value (tree decl)
400fbf9f 1788{
a7c1916a 1789 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1790 in a place where a variable is invalid. Note that DECL_INITIAL
1791 isn't valid for a PARM_DECL. */
a7c1916a 1792 current_function_decl != 0
4f976745 1793 && TREE_CODE (decl) != PARM_DECL
3f75a254 1794 && !TREE_THIS_VOLATILE (decl)
83bab8db 1795 && TREE_READONLY (decl)
400fbf9f
JW
1796 && DECL_INITIAL (decl) != 0
1797 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1798 /* This is invalid if initial value is not constant.
1799 If it has either a function call, a memory reference,
1800 or a variable, then re-evaluating it could give different results. */
1801 && TREE_CONSTANT (DECL_INITIAL (decl))
1802 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1803 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1804 return DECL_INITIAL (decl);
1805 return decl;
1806}
1807
f2a71bbc
JM
1808/* Convert the array expression EXP to a pointer. */
1809static tree
c2255bc4 1810array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1811{
f2a71bbc 1812 tree orig_exp = exp;
207bf485 1813 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1814 tree adr;
1815 tree restype = TREE_TYPE (type);
1816 tree ptrtype;
207bf485 1817
f2a71bbc 1818 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1819
f2a71bbc 1820 STRIP_TYPE_NOPS (exp);
207bf485 1821
487a92fe
JM
1822 if (TREE_NO_WARNING (orig_exp))
1823 TREE_NO_WARNING (exp) = 1;
207bf485 1824
f2a71bbc
JM
1825 ptrtype = build_pointer_type (restype);
1826
1827 if (TREE_CODE (exp) == INDIRECT_REF)
1828 return convert (ptrtype, TREE_OPERAND (exp, 0));
1829
1f37c583
JM
1830 /* In C++ array compound literals are temporary objects unless they are
1831 const or appear in namespace scope, so they are destroyed too soon
1832 to use them for much of anything (c++/53220). */
1833 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1834 {
1835 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1836 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1837 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1838 "converting an array compound literal to a pointer "
1839 "is ill-formed in C++");
1840 }
1841
c2255bc4 1842 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1843 return convert (ptrtype, adr);
1844}
207bf485 1845
f2a71bbc
JM
1846/* Convert the function expression EXP to a pointer. */
1847static tree
c2255bc4 1848function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1849{
1850 tree orig_exp = exp;
207bf485 1851
f2a71bbc 1852 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1853
f2a71bbc 1854 STRIP_TYPE_NOPS (exp);
207bf485 1855
f2a71bbc
JM
1856 if (TREE_NO_WARNING (orig_exp))
1857 TREE_NO_WARNING (exp) = 1;
207bf485 1858
c2255bc4 1859 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1860}
207bf485 1861
ebfbbdc5
JJ
1862/* Mark EXP as read, not just set, for set but not used -Wunused
1863 warning purposes. */
1864
1865void
1866mark_exp_read (tree exp)
1867{
1868 switch (TREE_CODE (exp))
1869 {
1870 case VAR_DECL:
1871 case PARM_DECL:
1872 DECL_READ_P (exp) = 1;
1873 break;
1874 case ARRAY_REF:
1875 case COMPONENT_REF:
1876 case MODIFY_EXPR:
1877 case REALPART_EXPR:
1878 case IMAGPART_EXPR:
1879 CASE_CONVERT:
1880 case ADDR_EXPR:
1881 mark_exp_read (TREE_OPERAND (exp, 0));
1882 break;
1883 case COMPOUND_EXPR:
82c3c067 1884 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1885 mark_exp_read (TREE_OPERAND (exp, 1));
1886 break;
1887 default:
1888 break;
1889 }
1890}
1891
f2a71bbc
JM
1892/* Perform the default conversion of arrays and functions to pointers.
1893 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1894 return EXP.
1895
1896 LOC is the location of the expression. */
f2a71bbc
JM
1897
1898struct c_expr
c2255bc4 1899default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1900{
1901 tree orig_exp = exp.value;
1902 tree type = TREE_TYPE (exp.value);
1903 enum tree_code code = TREE_CODE (type);
1904
1905 switch (code)
1906 {
1907 case ARRAY_TYPE:
1908 {
1909 bool not_lvalue = false;
1910 bool lvalue_array_p;
1911
1912 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1913 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1914 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1915 {
1916 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1917 not_lvalue = true;
1918 exp.value = TREE_OPERAND (exp.value, 0);
1919 }
1920
1921 if (TREE_NO_WARNING (orig_exp))
1922 TREE_NO_WARNING (exp.value) = 1;
1923
1924 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1925 if (!flag_isoc99 && !lvalue_array_p)
1926 {
1927 /* Before C99, non-lvalue arrays do not decay to pointers.
1928 Normally, using such an array would be invalid; but it can
1929 be used correctly inside sizeof or as a statement expression.
1930 Thus, do not give an error here; an error will result later. */
1931 return exp;
1932 }
1933
c2255bc4 1934 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1935 }
1936 break;
1937 case FUNCTION_TYPE:
c2255bc4 1938 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1939 break;
1940 default:
f2a71bbc 1941 break;
207bf485 1942 }
f2a71bbc 1943
207bf485
JM
1944 return exp;
1945}
1946
ebfbbdc5
JJ
1947struct c_expr
1948default_function_array_read_conversion (location_t loc, struct c_expr exp)
1949{
1950 mark_exp_read (exp.value);
1951 return default_function_array_conversion (loc, exp);
1952}
522ddfa2 1953
267bac10
JM
1954/* Return whether EXPR should be treated as an atomic lvalue for the
1955 purposes of load and store handling. */
1956
1957static bool
1958really_atomic_lvalue (tree expr)
1959{
7a0ca710 1960 if (error_operand_p (expr))
267bac10
JM
1961 return false;
1962 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1963 return false;
1964 if (!lvalue_p (expr))
1965 return false;
1966
1967 /* Ignore _Atomic on register variables, since their addresses can't
1968 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1969 sequences wouldn't work. Ignore _Atomic on structures containing
1970 bit-fields, since accessing elements of atomic structures or
1971 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1972 it's undefined at translation time or execution time, and the
1973 normal atomic sequences again wouldn't work. */
1974 while (handled_component_p (expr))
1975 {
1976 if (TREE_CODE (expr) == COMPONENT_REF
1977 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1978 return false;
1979 expr = TREE_OPERAND (expr, 0);
1980 }
1981 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1982 return false;
1983 return true;
1984}
1985
1986/* Convert expression EXP (location LOC) from lvalue to rvalue,
1987 including converting functions and arrays to pointers if CONVERT_P.
1988 If READ_P, also mark the expression as having been read. */
1989
1990struct c_expr
1991convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1992 bool convert_p, bool read_p)
1993{
1994 if (read_p)
1995 mark_exp_read (exp.value);
1996 if (convert_p)
1997 exp = default_function_array_conversion (loc, exp);
1998 if (really_atomic_lvalue (exp.value))
1999 {
2000 vec<tree, va_gc> *params;
2001 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2002 tree expr_type = TREE_TYPE (exp.value);
2003 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
2004 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2005
2006 gcc_assert (TYPE_ATOMIC (expr_type));
2007
2008 /* Expansion of a generic atomic load may require an addition
2009 element, so allocate enough to prevent a resize. */
2010 vec_alloc (params, 4);
2011
2012 /* Remove the qualifiers for the rest of the expressions and
2013 create the VAL temp variable to hold the RHS. */
2014 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2015 tmp = create_tmp_var (nonatomic_type, NULL);
2016 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2017 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2018 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2019
2020 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2021 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2022 params->quick_push (expr_addr);
2023 params->quick_push (tmp_addr);
2024 params->quick_push (seq_cst);
8edbfaa6 2025 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2026
cc28fc7f
MP
2027 /* EXPR is always read. */
2028 mark_exp_read (exp.value);
2029
267bac10
JM
2030 /* Return tmp which contains the value loaded. */
2031 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2032 }
2033 return exp;
2034}
2035
522ddfa2
JM
2036/* EXP is an expression of integer type. Apply the integer promotions
2037 to it and return the promoted value. */
400fbf9f
JW
2038
2039tree
522ddfa2 2040perform_integral_promotions (tree exp)
400fbf9f 2041{
b3694847
SS
2042 tree type = TREE_TYPE (exp);
2043 enum tree_code code = TREE_CODE (type);
400fbf9f 2044
522ddfa2 2045 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2046
400fbf9f
JW
2047 /* Normally convert enums to int,
2048 but convert wide enums to something wider. */
2049 if (code == ENUMERAL_TYPE)
2050 {
b0c48229
NB
2051 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2052 TYPE_PRECISION (integer_type_node)),
2053 ((TYPE_PRECISION (type)
2054 >= TYPE_PRECISION (integer_type_node))
8df83eae 2055 && TYPE_UNSIGNED (type)));
05bccae2 2056
400fbf9f
JW
2057 return convert (type, exp);
2058 }
2059
522ddfa2
JM
2060 /* ??? This should no longer be needed now bit-fields have their
2061 proper types. */
9753f113 2062 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2063 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2064 /* If it's thinner than an int, promote it like a
d72040f5 2065 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2066 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2067 TYPE_PRECISION (integer_type_node)))
f458d1d5 2068 return convert (integer_type_node, exp);
9753f113 2069
d72040f5 2070 if (c_promoting_integer_type_p (type))
400fbf9f 2071 {
f458d1d5 2072 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2073 if (TYPE_UNSIGNED (type)
f458d1d5 2074 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2075 return convert (unsigned_type_node, exp);
05bccae2 2076
400fbf9f
JW
2077 return convert (integer_type_node, exp);
2078 }
05bccae2 2079
522ddfa2
JM
2080 return exp;
2081}
2082
2083
2084/* Perform default promotions for C data used in expressions.
46bdb9cf 2085 Enumeral types or short or char are converted to int.
522ddfa2
JM
2086 In addition, manifest constants symbols are replaced by their values. */
2087
2088tree
2089default_conversion (tree exp)
2090{
2091 tree orig_exp;
2092 tree type = TREE_TYPE (exp);
2093 enum tree_code code = TREE_CODE (type);
40449a90 2094 tree promoted_type;
522ddfa2 2095
ebfbbdc5
JJ
2096 mark_exp_read (exp);
2097
46bdb9cf
JM
2098 /* Functions and arrays have been converted during parsing. */
2099 gcc_assert (code != FUNCTION_TYPE);
2100 if (code == ARRAY_TYPE)
2101 return exp;
522ddfa2
JM
2102
2103 /* Constants can be used directly unless they're not loadable. */
2104 if (TREE_CODE (exp) == CONST_DECL)
2105 exp = DECL_INITIAL (exp);
2106
522ddfa2
JM
2107 /* Strip no-op conversions. */
2108 orig_exp = exp;
2109 STRIP_TYPE_NOPS (exp);
2110
2111 if (TREE_NO_WARNING (orig_exp))
2112 TREE_NO_WARNING (exp) = 1;
2113
400fbf9f
JW
2114 if (code == VOID_TYPE)
2115 {
5436fa2e
MP
2116 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2117 "void value not ignored as it ought to be");
400fbf9f
JW
2118 return error_mark_node;
2119 }
808d6eaa
JM
2120
2121 exp = require_complete_type (exp);
2122 if (exp == error_mark_node)
2123 return error_mark_node;
2124
40449a90
SL
2125 promoted_type = targetm.promoted_type (type);
2126 if (promoted_type)
2127 return convert (promoted_type, exp);
2128
808d6eaa
JM
2129 if (INTEGRAL_TYPE_P (type))
2130 return perform_integral_promotions (exp);
2131
400fbf9f
JW
2132 return exp;
2133}
2134\f
0fb96aa4 2135/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2136
2137 If the component name is not found, returns NULL_TREE. Otherwise,
2138 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2139 stepping down the chain to the component, which is in the last
2140 TREE_VALUE of the list. Normally the list is of length one, but if
2141 the component is embedded within (nested) anonymous structures or
2142 unions, the list steps down the chain to the component. */
2f6e4e97 2143
2f2d13da 2144static tree
0fb96aa4 2145lookup_field (tree type, tree component)
2f2d13da
DE
2146{
2147 tree field;
2148
2149 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2150 to the field elements. Use a binary search on this array to quickly
2151 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2152 will always be set for structures which have many elements. */
2153
22a0b85f 2154 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2155 {
2156 int bot, top, half;
d07605f5 2157 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2158
2159 field = TYPE_FIELDS (type);
2160 bot = 0;
d07605f5 2161 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2162 while (top - bot > 1)
2163 {
2f2d13da
DE
2164 half = (top - bot + 1) >> 1;
2165 field = field_array[bot+half];
2166
2167 if (DECL_NAME (field) == NULL_TREE)
2168 {
2169 /* Step through all anon unions in linear fashion. */
2170 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2171 {
2f2d13da 2172 field = field_array[bot++];
a68b98cf
RK
2173 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2174 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2175 {
0fb96aa4 2176 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2177
2178 if (anon)
2179 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2180
2181 /* The Plan 9 compiler permits referring
2182 directly to an anonymous struct/union field
2183 using a typedef name. */
2184 if (flag_plan9_extensions
2185 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2186 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2187 == TYPE_DECL)
2188 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2189 == component))
2190 break;
2f6e4e97 2191 }
2f2d13da
DE
2192 }
2193
2194 /* Entire record is only anon unions. */
2195 if (bot > top)
2196 return NULL_TREE;
2197
2198 /* Restart the binary search, with new lower bound. */
2199 continue;
2200 }
2201
e8b87aac 2202 if (DECL_NAME (field) == component)
2f2d13da 2203 break;
e8b87aac 2204 if (DECL_NAME (field) < component)
2f2d13da
DE
2205 bot += half;
2206 else
2207 top = bot + half;
2208 }
2209
2210 if (DECL_NAME (field_array[bot]) == component)
2211 field = field_array[bot];
2212 else if (DECL_NAME (field) != component)
e9b2c823 2213 return NULL_TREE;
2f2d13da
DE
2214 }
2215 else
2216 {
910ad8de 2217 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2218 {
e9b2c823
NB
2219 if (DECL_NAME (field) == NULL_TREE
2220 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2221 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2222 {
0fb96aa4 2223 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2224
e9b2c823
NB
2225 if (anon)
2226 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2227
2228 /* The Plan 9 compiler permits referring directly to an
2229 anonymous struct/union field using a typedef
2230 name. */
2231 if (flag_plan9_extensions
2232 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2233 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2234 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2235 == component))
2236 break;
2f2d13da
DE
2237 }
2238
2239 if (DECL_NAME (field) == component)
2240 break;
2241 }
e9b2c823
NB
2242
2243 if (field == NULL_TREE)
2244 return NULL_TREE;
2f2d13da
DE
2245 }
2246
e9b2c823 2247 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2248}
2249
c2255bc4
AH
2250/* Make an expression to refer to the COMPONENT field of structure or
2251 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2252 location of the COMPONENT_REF. */
400fbf9f
JW
2253
2254tree
c2255bc4 2255build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2256{
b3694847
SS
2257 tree type = TREE_TYPE (datum);
2258 enum tree_code code = TREE_CODE (type);
2259 tree field = NULL;
2260 tree ref;
1e57bf47 2261 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2262
7a3ea201
RH
2263 if (!objc_is_public (datum, component))
2264 return error_mark_node;
2265
46a88c12 2266 /* Detect Objective-C property syntax object.property. */
668ea4b1 2267 if (c_dialect_objc ()
46a88c12 2268 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2269 return ref;
2270
400fbf9f
JW
2271 /* See if there is a field or component with name COMPONENT. */
2272
2273 if (code == RECORD_TYPE || code == UNION_TYPE)
2274 {
d0f062fb 2275 if (!COMPLETE_TYPE_P (type))
400fbf9f 2276 {
7a228918 2277 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2278 return error_mark_node;
2279 }
2280
0fb96aa4 2281 field = lookup_field (type, component);
400fbf9f
JW
2282
2283 if (!field)
2284 {
c2255bc4 2285 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2286 return error_mark_node;
2287 }
400fbf9f 2288
e9b2c823
NB
2289 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2290 This might be better solved in future the way the C++ front
2291 end does it - by giving the anonymous entities each a
2292 separate name and type, and then have build_component_ref
2293 recursively call itself. We can't do that here. */
46ea50cb 2294 do
19d76e60 2295 {
e9b2c823 2296 tree subdatum = TREE_VALUE (field);
efed193e
JM
2297 int quals;
2298 tree subtype;
1e57bf47 2299 bool use_datum_quals;
e9b2c823
NB
2300
2301 if (TREE_TYPE (subdatum) == error_mark_node)
2302 return error_mark_node;
2303
1e57bf47
JM
2304 /* If this is an rvalue, it does not have qualifiers in C
2305 standard terms and we must avoid propagating such
2306 qualifiers down to a non-lvalue array that is then
2307 converted to a pointer. */
2308 use_datum_quals = (datum_lvalue
2309 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2310
efed193e 2311 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2312 if (use_datum_quals)
2313 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2314 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2315
2316 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2317 NULL_TREE);
c2255bc4 2318 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2319 if (TREE_READONLY (subdatum)
2320 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2321 TREE_READONLY (ref) = 1;
1e57bf47
JM
2322 if (TREE_THIS_VOLATILE (subdatum)
2323 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2324 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2325
2326 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2327 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2328
19d76e60 2329 datum = ref;
46ea50cb
RS
2330
2331 field = TREE_CHAIN (field);
19d76e60 2332 }
46ea50cb 2333 while (field);
19d76e60 2334
400fbf9f
JW
2335 return ref;
2336 }
2337 else if (code != ERROR_MARK)
c2255bc4
AH
2338 error_at (loc,
2339 "request for member %qE in something not a structure or union",
2340 component);
400fbf9f
JW
2341
2342 return error_mark_node;
2343}
2344\f
2345/* Given an expression PTR for a pointer, return an expression
2346 for the value pointed to.
6a3799eb
AH
2347 ERRORSTRING is the name of the operator to appear in error messages.
2348
2349 LOC is the location to use for the generated tree. */
400fbf9f
JW
2350
2351tree
dd865ef6 2352build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2353{
b3694847
SS
2354 tree pointer = default_conversion (ptr);
2355 tree type = TREE_TYPE (pointer);
6a3799eb 2356 tree ref;
400fbf9f
JW
2357
2358 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2359 {
1043771b 2360 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2361 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2362 {
2363 /* If a warning is issued, mark it to avoid duplicates from
2364 the backend. This only needs to be done at
2365 warn_strict_aliasing > 2. */
2366 if (warn_strict_aliasing > 2)
2367 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2368 type, TREE_OPERAND (pointer, 0)))
2369 TREE_NO_WARNING (pointer) = 1;
2370 }
2371
870cc33b 2372 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2373 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2374 == TREE_TYPE (type)))
6a3799eb
AH
2375 {
2376 ref = TREE_OPERAND (pointer, 0);
2377 protected_set_expr_location (ref, loc);
2378 return ref;
2379 }
870cc33b
RS
2380 else
2381 {
2382 tree t = TREE_TYPE (type);
46df2823 2383
984dfd8c 2384 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2385
baae9b65 2386 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2387 {
d9b7be2e
MP
2388 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2389 {
2390 error_at (loc, "dereferencing pointer to incomplete type "
2391 "%qT", t);
2392 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2393 }
870cc33b
RS
2394 return error_mark_node;
2395 }
7d882b83 2396 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2397 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2398
2399 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2400 so that we get the proper error message if the result is used
2401 to assign to. Also, &* is supposed to be a no-op.
2402 And ANSI C seems to specify that the type of the result
2403 should be the const type. */
2404 /* A de-reference of a pointer to const is not a const. It is valid
2405 to change it via some other pointer. */
2406 TREE_READONLY (ref) = TYPE_READONLY (t);
2407 TREE_SIDE_EFFECTS (ref)
271bd540 2408 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2409 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2410 protected_set_expr_location (ref, loc);
870cc33b
RS
2411 return ref;
2412 }
2413 }
400fbf9f 2414 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2415 invalid_indirection_error (loc, type, errstring);
2416
400fbf9f
JW
2417 return error_mark_node;
2418}
2419
2420/* This handles expressions of the form "a[i]", which denotes
2421 an array reference.
2422
2423 This is logically equivalent in C to *(a+i), but we may do it differently.
2424 If A is a variable or a member, we generate a primitive ARRAY_REF.
2425 This avoids forcing the array out of registers, and can work on
2426 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2427 by functions).
2428
30cd1c5d
AS
2429 For vector types, allow vector[i] but not i[vector], and create
2430 *(((type*)&vectortype) + i) for the expression.
2431
6a3799eb 2432 LOC is the location to use for the returned expression. */
400fbf9f
JW
2433
2434tree
c2255bc4 2435build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2436{
6a3799eb 2437 tree ret;
a4ab7973 2438 bool swapped = false;
400fbf9f
JW
2439 if (TREE_TYPE (array) == error_mark_node
2440 || TREE_TYPE (index) == error_mark_node)
2441 return error_mark_node;
2442
b72271b9 2443 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2444 {
2445 size_t rank = 0;
2446 if (!find_rank (loc, index, index, true, &rank))
2447 return error_mark_node;
2448 if (rank > 1)
2449 {
2450 error_at (loc, "rank of the array's index is greater than 1");
2451 return error_mark_node;
2452 }
2453 }
a4ab7973 2454 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2455 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2456 /* Allow vector[index] but not index[vector]. */
2457 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2458 {
a4ab7973
JM
2459 tree temp;
2460 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2461 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2462 {
f90e8e2e 2463 error_at (loc,
30cd1c5d
AS
2464 "subscripted value is neither array nor pointer nor vector");
2465
fdeefd49
RS
2466 return error_mark_node;
2467 }
a4ab7973
JM
2468 temp = array;
2469 array = index;
2470 index = temp;
2471 swapped = true;
2472 }
2473
2474 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2475 {
a63068b6 2476 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2477 return error_mark_node;
2478 }
2479
2480 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2481 {
a63068b6 2482 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2483 return error_mark_node;
2484 }
2485
ff6b6641
GDR
2486 /* ??? Existing practice has been to warn only when the char
2487 index is syntactically the index, not for char[array]. */
2488 if (!swapped)
2489 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2490
2491 /* Apply default promotions *after* noticing character types. */
2492 index = default_conversion (index);
f406ae1f
MP
2493 if (index == error_mark_node)
2494 return error_mark_node;
a4ab7973
JM
2495
2496 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2497
7edaa4d2 2498 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2499
2500 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2501 {
e4d35515 2502 tree rval, type;
fdeefd49 2503
400fbf9f
JW
2504 /* An array that is indexed by a non-constant
2505 cannot be stored in a register; we must be able to do
2506 address arithmetic on its address.
2507 Likewise an array of elements of variable size. */
2508 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2509 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2510 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2511 {
dffd7eb6 2512 if (!c_mark_addressable (array))
400fbf9f
JW
2513 return error_mark_node;
2514 }
e6d52559
JW
2515 /* An array that is indexed by a constant value which is not within
2516 the array bounds cannot be stored in a register either; because we
2517 would get a crash in store_bit_field/extract_bit_field when trying
2518 to access a non-existent part of the register. */
2519 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2520 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2521 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2522 {
dffd7eb6 2523 if (!c_mark_addressable (array))
e6d52559
JW
2524 return error_mark_node;
2525 }
400fbf9f 2526
f3bede71 2527 if (pedantic || warn_c90_c99_compat)
400fbf9f
JW
2528 {
2529 tree foo = array;
2530 while (TREE_CODE (foo) == COMPONENT_REF)
2531 foo = TREE_OPERAND (foo, 0);
5baeaac0 2532 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2533 pedwarn (loc, OPT_Wpedantic,
fcf73884 2534 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2535 else if (!lvalue_p (foo))
2536 pedwarn_c90 (loc, OPT_Wpedantic,
2537 "ISO C90 forbids subscripting non-lvalue "
2538 "array");
400fbf9f
JW
2539 }
2540
46df2823 2541 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2542 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2543 /* Array ref is const/volatile if the array elements are
c22cacf3 2544 or if the array is. */
400fbf9f
JW
2545 TREE_READONLY (rval)
2546 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2547 | TREE_READONLY (array));
2548 TREE_SIDE_EFFECTS (rval)
2549 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2550 | TREE_SIDE_EFFECTS (array));
2551 TREE_THIS_VOLATILE (rval)
2552 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2553 /* This was added by rms on 16 Nov 91.
2f6e4e97 2554 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2555 in an inline function.
2556 Hope it doesn't break something else. */
2557 | TREE_THIS_VOLATILE (array));
928c19bb 2558 ret = require_complete_type (rval);
6a3799eb
AH
2559 protected_set_expr_location (ret, loc);
2560 return ret;
400fbf9f 2561 }
a4ab7973
JM
2562 else
2563 {
2564 tree ar = default_conversion (array);
400fbf9f 2565
a4ab7973
JM
2566 if (ar == error_mark_node)
2567 return ar;
400fbf9f 2568
a4ab7973
JM
2569 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2570 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2571
ba47d38d 2572 return build_indirect_ref
c9f9eb5d 2573 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2574 RO_ARRAY_INDEXING);
a4ab7973 2575 }
400fbf9f
JW
2576}
2577\f
7e585d16 2578/* Build an external reference to identifier ID. FUN indicates
766beb40 2579 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2580 location of the identifier. This sets *TYPE to the type of the
2581 identifier, which is not the same as the type of the returned value
2582 for CONST_DECLs defined as enum constants. If the type of the
2583 identifier is not available, *TYPE is set to NULL. */
7e585d16 2584tree
c2255bc4 2585build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2586{
2587 tree ref;
2588 tree decl = lookup_name (id);
16b34ad6
ZL
2589
2590 /* In Objective-C, an instance variable (ivar) may be preferred to
2591 whatever lookup_name() found. */
2592 decl = objc_lookup_ivar (decl, id);
7e585d16 2593
6866c6e8 2594 *type = NULL;
339a28b9 2595 if (decl && decl != error_mark_node)
6866c6e8
ILT
2596 {
2597 ref = decl;
2598 *type = TREE_TYPE (ref);
2599 }
339a28b9
ZW
2600 else if (fun)
2601 /* Implicit function declaration. */
c2255bc4 2602 ref = implicitly_declare (loc, id);
339a28b9
ZW
2603 else if (decl == error_mark_node)
2604 /* Don't complain about something that's already been
2605 complained about. */
2606 return error_mark_node;
2607 else
2608 {
c2255bc4 2609 undeclared_variable (loc, id);
339a28b9
ZW
2610 return error_mark_node;
2611 }
7e585d16
ZW
2612
2613 if (TREE_TYPE (ref) == error_mark_node)
2614 return error_mark_node;
2615
339a28b9 2616 if (TREE_DEPRECATED (ref))
9b86d6bb 2617 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2618
ad960f56 2619 /* Recursive call does not count as usage. */
b8698a0f 2620 if (ref != current_function_decl)
ad960f56 2621 {
ad960f56
MLI
2622 TREE_USED (ref) = 1;
2623 }
7e585d16 2624
bc4b653b
JM
2625 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2626 {
2627 if (!in_sizeof && !in_typeof)
2628 C_DECL_USED (ref) = 1;
2629 else if (DECL_INITIAL (ref) == 0
2630 && DECL_EXTERNAL (ref)
2631 && !TREE_PUBLIC (ref))
2632 record_maybe_used_decl (ref);
2633 }
2634
7e585d16
ZW
2635 if (TREE_CODE (ref) == CONST_DECL)
2636 {
6193b8b7 2637 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2638
2639 if (warn_cxx_compat
2640 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2641 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2642 {
2643 warning_at (loc, OPT_Wc___compat,
2644 ("enum constant defined in struct or union "
2645 "is not visible in C++"));
2646 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2647 }
2648
7e585d16
ZW
2649 ref = DECL_INITIAL (ref);
2650 TREE_CONSTANT (ref) = 1;
2651 }
6a29edea 2652 else if (current_function_decl != 0
4b1e44be 2653 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2654 && (TREE_CODE (ref) == VAR_DECL
2655 || TREE_CODE (ref) == PARM_DECL
2656 || TREE_CODE (ref) == FUNCTION_DECL))
2657 {
2658 tree context = decl_function_context (ref);
2f6e4e97 2659
6a29edea
EB
2660 if (context != 0 && context != current_function_decl)
2661 DECL_NONLOCAL (ref) = 1;
2662 }
71113fcd
GK
2663 /* C99 6.7.4p3: An inline definition of a function with external
2664 linkage ... shall not contain a reference to an identifier with
2665 internal linkage. */
2666 else if (current_function_decl != 0
2667 && DECL_DECLARED_INLINE_P (current_function_decl)
2668 && DECL_EXTERNAL (current_function_decl)
2669 && VAR_OR_FUNCTION_DECL_P (ref)
2670 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2671 && ! TREE_PUBLIC (ref)
2672 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2673 record_inline_static (loc, current_function_decl, ref,
2674 csi_internal);
7e585d16
ZW
2675
2676 return ref;
2677}
2678
bc4b653b
JM
2679/* Record details of decls possibly used inside sizeof or typeof. */
2680struct maybe_used_decl
2681{
2682 /* The decl. */
2683 tree decl;
2684 /* The level seen at (in_sizeof + in_typeof). */
2685 int level;
2686 /* The next one at this level or above, or NULL. */
2687 struct maybe_used_decl *next;
2688};
2689
2690static struct maybe_used_decl *maybe_used_decls;
2691
2692/* Record that DECL, an undefined static function reference seen
2693 inside sizeof or typeof, might be used if the operand of sizeof is
2694 a VLA type or the operand of typeof is a variably modified
2695 type. */
2696
4e2fb7de 2697static void
bc4b653b
JM
2698record_maybe_used_decl (tree decl)
2699{
2700 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2701 t->decl = decl;
2702 t->level = in_sizeof + in_typeof;
2703 t->next = maybe_used_decls;
2704 maybe_used_decls = t;
2705}
2706
2707/* Pop the stack of decls possibly used inside sizeof or typeof. If
2708 USED is false, just discard them. If it is true, mark them used
2709 (if no longer inside sizeof or typeof) or move them to the next
2710 level up (if still inside sizeof or typeof). */
2711
2712void
2713pop_maybe_used (bool used)
2714{
2715 struct maybe_used_decl *p = maybe_used_decls;
2716 int cur_level = in_sizeof + in_typeof;
2717 while (p && p->level > cur_level)
2718 {
2719 if (used)
2720 {
2721 if (cur_level == 0)
2722 C_DECL_USED (p->decl) = 1;
2723 else
2724 p->level = cur_level;
2725 }
2726 p = p->next;
2727 }
2728 if (!used || cur_level == 0)
2729 maybe_used_decls = p;
2730}
2731
2732/* Return the result of sizeof applied to EXPR. */
2733
2734struct c_expr
c2255bc4 2735c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2736{
2737 struct c_expr ret;
ad97f4be
JM
2738 if (expr.value == error_mark_node)
2739 {
2740 ret.value = error_mark_node;
2741 ret.original_code = ERROR_MARK;
6866c6e8 2742 ret.original_type = NULL;
ad97f4be
JM
2743 pop_maybe_used (false);
2744 }
2745 else
2746 {
928c19bb 2747 bool expr_const_operands = true;
773ec47f
MP
2748
2749 if (TREE_CODE (expr.value) == PARM_DECL
2750 && C_ARRAY_PARAMETER (expr.value))
2751 {
2752 if (warning_at (loc, OPT_Wsizeof_array_argument,
2753 "%<sizeof%> on array function parameter %qE will "
2754 "return size of %qT", expr.value,
2755 expr.original_type))
2756 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2757 }
928c19bb
JM
2758 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2759 &expr_const_operands);
c2255bc4 2760 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2761 c_last_sizeof_arg = expr.value;
2762 ret.original_code = SIZEOF_EXPR;
6866c6e8 2763 ret.original_type = NULL;
928c19bb 2764 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2765 {
2766 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2767 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2768 folded_expr, ret.value);
2769 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2770 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2771 }
928c19bb 2772 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2773 }
bc4b653b
JM
2774 return ret;
2775}
2776
2777/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2778 name passed to sizeof (rather than the type itself). LOC is the
2779 location of the original expression. */
bc4b653b
JM
2780
2781struct c_expr
c2255bc4 2782c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2783{
2784 tree type;
2785 struct c_expr ret;
928c19bb
JM
2786 tree type_expr = NULL_TREE;
2787 bool type_expr_const = true;
2788 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2789 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2790 c_last_sizeof_arg = type;
2791 ret.original_code = SIZEOF_EXPR;
6866c6e8 2792 ret.original_type = NULL;
24070fcb
JM
2793 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2794 && c_vla_type_p (type))
928c19bb 2795 {
24070fcb
JM
2796 /* If the type is a [*] array, it is a VLA but is represented as
2797 having a size of zero. In such a case we must ensure that
2798 the result of sizeof does not get folded to a constant by
2799 c_fully_fold, because if the size is evaluated the result is
2800 not constant and so constraints on zero or negative size
2801 arrays must not be applied when this sizeof call is inside
2802 another array declarator. */
2803 if (!type_expr)
2804 type_expr = integer_zero_node;
928c19bb
JM
2805 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2806 type_expr, ret.value);
2807 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2808 }
16464cc1
VR
2809 pop_maybe_used (type != error_mark_node
2810 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2811 return ret;
2812}
2813
400fbf9f 2814/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2815 The function call is at LOC.
400fbf9f
JW
2816 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2817 TREE_VALUE of each node is a parameter-expression.
2818 FUNCTION's data type may be a function type or a pointer-to-function. */
2819
2820tree
c2255bc4 2821build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2822{
9771b263 2823 vec<tree, va_gc> *v;
bbbbb16a
ILT
2824 tree ret;
2825
9771b263 2826 vec_alloc (v, list_length (params));
bbbbb16a 2827 for (; params; params = TREE_CHAIN (params))
9771b263 2828 v->quick_push (TREE_VALUE (params));
8edbfaa6 2829 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2830 vec_free (v);
bbbbb16a
ILT
2831 return ret;
2832}
2833
ae52741c
MLI
2834/* Give a note about the location of the declaration of DECL. */
2835
2836static void inform_declaration (tree decl)
2837{
2838 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2839 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2840}
2841
bbbbb16a
ILT
2842/* Build a function call to function FUNCTION with parameters PARAMS.
2843 ORIGTYPES, if not NULL, is a vector of types; each element is
2844 either NULL or the original type of the corresponding element in
2845 PARAMS. The original type may differ from TREE_TYPE of the
2846 parameter for enums. FUNCTION's data type may be a function type
2847 or pointer-to-function. This function changes the elements of
2848 PARAMS. */
2849
2850tree
81e5eca8
MP
2851build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2852 tree function, vec<tree, va_gc> *params,
9771b263 2853 vec<tree, va_gc> *origtypes)
400fbf9f 2854{
b3694847 2855 tree fntype, fundecl = 0;
4977bab6 2856 tree name = NULL_TREE, result;
c96f4f73 2857 tree tem;
94a0dd7b
SL
2858 int nargs;
2859 tree *argarray;
b8698a0f 2860
400fbf9f 2861
fc76e425 2862 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2863 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2864
2865 /* Convert anything with function type to a pointer-to-function. */
2866 if (TREE_CODE (function) == FUNCTION_DECL)
2867 {
2868 name = DECL_NAME (function);
0a35513e
AH
2869
2870 if (flag_tm)
2871 tm_malloc_replacement (function);
a5eadacc 2872 fundecl = function;
86951993
AM
2873 /* Atomic functions have type checking/casting already done. They are
2874 often rewritten and don't match the original parameter list. */
2875 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2876 origtypes = NULL;
36536d79 2877
b72271b9 2878 if (flag_cilkplus
36536d79
BI
2879 && is_cilkplus_reduce_builtin (function))
2880 origtypes = NULL;
400fbf9f 2881 }
f2a71bbc 2882 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2883 function = function_to_pointer_conversion (loc, function);
400fbf9f 2884
6e955430
ZL
2885 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2886 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2887 if (params && !params->is_empty ())
2888 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2889
928c19bb
JM
2890 function = c_fully_fold (function, false, NULL);
2891
400fbf9f
JW
2892 fntype = TREE_TYPE (function);
2893
2894 if (TREE_CODE (fntype) == ERROR_MARK)
2895 return error_mark_node;
2896
2897 if (!(TREE_CODE (fntype) == POINTER_TYPE
2898 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2899 {
ae52741c
MLI
2900 if (!flag_diagnostics_show_caret)
2901 error_at (loc,
2902 "called object %qE is not a function or function pointer",
2903 function);
2904 else if (DECL_P (function))
2905 {
2906 error_at (loc,
2907 "called object %qD is not a function or function pointer",
2908 function);
2909 inform_declaration (function);
2910 }
2911 else
2912 error_at (loc,
2913 "called object is not a function or function pointer");
400fbf9f
JW
2914 return error_mark_node;
2915 }
2916
5ce89b2e
JM
2917 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2918 current_function_returns_abnormally = 1;
2919
400fbf9f
JW
2920 /* fntype now gets the type of function pointed to. */
2921 fntype = TREE_TYPE (fntype);
2922
ab4194da
JM
2923 /* Convert the parameters to the types declared in the
2924 function prototype, or apply default promotions. */
2925
81e5eca8
MP
2926 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2927 origtypes, function, fundecl);
ab4194da
JM
2928 if (nargs < 0)
2929 return error_mark_node;
2930
c96f4f73 2931 /* Check that the function is called through a compatible prototype.
fa337f3a 2932 If it is not, warn. */
1043771b 2933 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2934 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2935 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2936 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2937 {
2938 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2939
2940 /* This situation leads to run-time undefined behavior. We can't,
2941 therefore, simply error unless we can prove that all possible
2942 executions of the program must execute the code. */
fa337f3a 2943 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2944
fa337f3a
RB
2945 if (VOID_TYPE_P (return_type)
2946 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2947 pedwarn (loc, 0,
2948 "function with qualified void return type called");
2949 }
c96f4f73 2950
9771b263 2951 argarray = vec_safe_address (params);
bbbbb16a 2952
83322951
RG
2953 /* Check that arguments to builtin functions match the expectations. */
2954 if (fundecl
2955 && DECL_BUILT_IN (fundecl)
2956 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2957 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2958 return error_mark_node;
400fbf9f 2959
83322951 2960 /* Check that the arguments to the function are valid. */
dde05067 2961 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2962
928c19bb
JM
2963 if (name != NULL_TREE
2964 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2965 {
928c19bb 2966 if (require_constant_value)
b8698a0f 2967 result =
db3927fb
AH
2968 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2969 function, nargs, argarray);
928c19bb 2970 else
db3927fb
AH
2971 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2972 function, nargs, argarray);
928c19bb
JM
2973 if (TREE_CODE (result) == NOP_EXPR
2974 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2975 STRIP_TYPE_NOPS (result);
bf730f15
RS
2976 }
2977 else
db3927fb
AH
2978 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2979 function, nargs, argarray);
b0b3afb2 2980
71653180 2981 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2982 {
2983 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2984 pedwarn (loc, 0,
3ce62965
JM
2985 "function with qualified void return type called");
2986 return result;
2987 }
1eb8759b 2988 return require_complete_type (result);
400fbf9f 2989}
8edbfaa6
JJ
2990
2991/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2992
2993tree
2994c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2995 tree function, vec<tree, va_gc> *params,
2996 vec<tree, va_gc> *origtypes)
2997{
2998 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2999 STRIP_TYPE_NOPS (function);
3000
3001 /* Convert anything with function type to a pointer-to-function. */
3002 if (TREE_CODE (function) == FUNCTION_DECL)
3003 {
3004 /* Implement type-directed function overloading for builtins.
3005 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3006 handle all the type checking. The result is a complete expression
3007 that implements this function call. */
3008 tree tem = resolve_overloaded_builtin (loc, function, params);
3009 if (tem)
3010 return tem;
3011 }
3012 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3013}
400fbf9f 3014\f
bbbbb16a
ILT
3015/* Convert the argument expressions in the vector VALUES
3016 to the types in the list TYPELIST.
400fbf9f
JW
3017
3018 If TYPELIST is exhausted, or when an element has NULL as its type,
3019 perform the default conversions.
3020
bbbbb16a
ILT
3021 ORIGTYPES is the original types of the expressions in VALUES. This
3022 holds the type of enum values which have been converted to integral
3023 types. It may be NULL.
400fbf9f 3024
03dafa61
JM
3025 FUNCTION is a tree for the called function. It is used only for
3026 error messages, where it is formatted with %qE.
400fbf9f
JW
3027
3028 This is also where warnings about wrong number of args are generated.
3029
81e5eca8
MP
3030 ARG_LOC are locations of function arguments (if any).
3031
94a0dd7b 3032 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3033 than the length of VALUES in some error situations), or -1 on
3034 failure. */
94a0dd7b
SL
3035
3036static int
81e5eca8
MP
3037convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3038 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3039 tree function, tree fundecl)
400fbf9f 3040{
bbbbb16a
ILT
3041 tree typetail, val;
3042 unsigned int parmnum;
06302a02 3043 bool error_args = false;
b5d32c25 3044 const bool type_generic = fundecl
81e5eca8 3045 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3046 bool type_generic_remove_excess_precision = false;
03dafa61 3047 tree selector;
03dafa61 3048
2ac2f164
JM
3049 /* Change pointer to function to the function itself for
3050 diagnostics. */
03dafa61
JM
3051 if (TREE_CODE (function) == ADDR_EXPR
3052 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3053 function = TREE_OPERAND (function, 0);
03dafa61
JM
3054
3055 /* Handle an ObjC selector specially for diagnostics. */
3056 selector = objc_message_selector ();
400fbf9f 3057
8ce94e44
JM
3058 /* For type-generic built-in functions, determine whether excess
3059 precision should be removed (classification) or not
3060 (comparison). */
3061 if (type_generic
3062 && DECL_BUILT_IN (fundecl)
3063 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3064 {
3065 switch (DECL_FUNCTION_CODE (fundecl))
3066 {
3067 case BUILT_IN_ISFINITE:
3068 case BUILT_IN_ISINF:
3069 case BUILT_IN_ISINF_SIGN:
3070 case BUILT_IN_ISNAN:
3071 case BUILT_IN_ISNORMAL:
3072 case BUILT_IN_FPCLASSIFY:
3073 type_generic_remove_excess_precision = true;
3074 break;
3075
3076 default:
3077 type_generic_remove_excess_precision = false;
3078 break;
3079 }
3080 }
b72271b9 3081 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3082 return vec_safe_length (values);
8ce94e44 3083
400fbf9f 3084 /* Scan the given expressions and types, producing individual
bbbbb16a 3085 converted arguments. */
400fbf9f 3086
bbbbb16a 3087 for (typetail = typelist, parmnum = 0;
9771b263 3088 values && values->iterate (parmnum, &val);
bbbbb16a 3089 ++parmnum)
400fbf9f 3090 {
b3694847 3091 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3092 tree valtype = TREE_TYPE (val);
03dafa61
JM
3093 tree rname = function;
3094 int argnum = parmnum + 1;
4d3e6fae 3095 const char *invalid_func_diag;
8ce94e44 3096 bool excess_precision = false;
928c19bb 3097 bool npc;
bbbbb16a 3098 tree parmval;
5c1bc275
MP
3099 /* Some __atomic_* builtins have additional hidden argument at
3100 position 0. */
3101 location_t ploc
3102 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3103 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3104 : input_location;
400fbf9f
JW
3105
3106 if (type == void_type_node)
3107 {
19dc6d01 3108 if (selector)
68fca595 3109 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3110 else
68fca595 3111 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3112 inform_declaration (fundecl);
94a0dd7b 3113 return parmnum;
400fbf9f
JW
3114 }
3115
03dafa61
JM
3116 if (selector && argnum > 2)
3117 {
3118 rname = selector;
3119 argnum -= 2;
3120 }
3121
928c19bb 3122 npc = null_pointer_constant_p (val);
8ce94e44
JM
3123
3124 /* If there is excess precision and a prototype, convert once to
3125 the required type rather than converting via the semantic
3126 type. Likewise without a prototype a float value represented
3127 as long double should be converted once to double. But for
3128 type-generic classification functions excess precision must
3129 be removed here. */
3130 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3131 && (type || !type_generic || !type_generic_remove_excess_precision))
3132 {
3133 val = TREE_OPERAND (val, 0);
3134 excess_precision = true;
3135 }
928c19bb 3136 val = c_fully_fold (val, false, NULL);
ed248cf7 3137 STRIP_TYPE_NOPS (val);
400fbf9f 3138
400fbf9f
JW
3139 val = require_complete_type (val);
3140
3141 if (type != 0)
3142 {
3143 /* Formal parm type is specified by a function prototype. */
400fbf9f 3144
20913689 3145 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3146 {
5c1bc275
MP
3147 error_at (ploc, "type of formal parameter %d is incomplete",
3148 parmnum + 1);
400fbf9f
JW
3149 parmval = val;
3150 }
3151 else
3152 {
bbbbb16a
ILT
3153 tree origtype;
3154
d45cf215
RS
3155 /* Optionally warn about conversions that
3156 differ from the default conversions. */
05170031 3157 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3158 {
e3a64162 3159 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3160
aae43c5f 3161 if (INTEGRAL_TYPE_P (type)
8ce94e44 3162 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3163 warning_at (ploc, OPT_Wtraditional_conversion,
3164 "passing argument %d of %qE as integer rather "
3165 "than floating due to prototype",
3166 argnum, rname);
03829ad2 3167 if (INTEGRAL_TYPE_P (type)
8ce94e44 3168 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3169 warning_at (ploc, OPT_Wtraditional_conversion,
3170 "passing argument %d of %qE as integer rather "
3171 "than complex due to prototype",
3172 argnum, rname);
aae43c5f 3173 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3174 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3175 warning_at (ploc, OPT_Wtraditional_conversion,
3176 "passing argument %d of %qE as complex rather "
3177 "than floating due to prototype",
3178 argnum, rname);
400fbf9f 3179 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3180 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3181 warning_at (ploc, OPT_Wtraditional_conversion,
3182 "passing argument %d of %qE as floating rather "
3183 "than integer due to prototype",
3184 argnum, rname);
03829ad2 3185 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3186 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3187 warning_at (ploc, OPT_Wtraditional_conversion,
3188 "passing argument %d of %qE as complex rather "
3189 "than integer due to prototype",
3190 argnum, rname);
aae43c5f 3191 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3192 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3193 warning_at (ploc, OPT_Wtraditional_conversion,
3194 "passing argument %d of %qE as floating rather "
3195 "than complex due to prototype",
3196 argnum, rname);
aae43c5f
RK
3197 /* ??? At some point, messages should be written about
3198 conversions between complex types, but that's too messy
3199 to do now. */
d45cf215 3200 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3201 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3202 {
3203 /* Warn if any argument is passed as `float',
047de90b 3204 since without a prototype it would be `double'. */
9a8ce21f
JG
3205 if (formal_prec == TYPE_PRECISION (float_type_node)
3206 && type != dfloat32_type_node)
5c1bc275
MP
3207 warning_at (ploc, 0,
3208 "passing argument %d of %qE as %<float%> "
3209 "rather than %<double%> due to prototype",
3210 argnum, rname);
9a8ce21f
JG
3211
3212 /* Warn if mismatch between argument and prototype
3213 for decimal float types. Warn of conversions with
3214 binary float types and of precision narrowing due to
3215 prototype. */
8ce94e44 3216 else if (type != valtype
9a8ce21f
JG
3217 && (type == dfloat32_type_node
3218 || type == dfloat64_type_node
c22cacf3 3219 || type == dfloat128_type_node
8ce94e44
JM
3220 || valtype == dfloat32_type_node
3221 || valtype == dfloat64_type_node
3222 || valtype == dfloat128_type_node)
c22cacf3 3223 && (formal_prec
8ce94e44 3224 <= TYPE_PRECISION (valtype)
9a8ce21f 3225 || (type == dfloat128_type_node
8ce94e44 3226 && (valtype
c22cacf3 3227 != dfloat64_type_node
8ce94e44 3228 && (valtype
9a8ce21f
JG
3229 != dfloat32_type_node)))
3230 || (type == dfloat64_type_node
8ce94e44 3231 && (valtype
9a8ce21f 3232 != dfloat32_type_node))))
5c1bc275
MP
3233 warning_at (ploc, 0,
3234 "passing argument %d of %qE as %qT "
3235 "rather than %qT due to prototype",
3236 argnum, rname, type, valtype);
9a8ce21f 3237
d45cf215 3238 }
3ed56f8a
KG
3239 /* Detect integer changing in width or signedness.
3240 These warnings are only activated with
05170031
MLI
3241 -Wtraditional-conversion, not with -Wtraditional. */
3242 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3243 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3244 {
d45cf215
RS
3245 tree would_have_been = default_conversion (val);
3246 tree type1 = TREE_TYPE (would_have_been);
3247
754a4d82 3248 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3249 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3250 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3251 /* No warning if function asks for enum
3252 and the actual arg is that enum type. */
3253 ;
3254 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3255 warning_at (ploc, OPT_Wtraditional_conversion,
3256 "passing argument %d of %qE "
3257 "with different width due to prototype",
3258 argnum, rname);
8df83eae 3259 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3260 ;
800cd3b9
RS
3261 /* Don't complain if the formal parameter type
3262 is an enum, because we can't tell now whether
3263 the value was an enum--even the same enum. */
3264 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3265 ;
400fbf9f
JW
3266 else if (TREE_CODE (val) == INTEGER_CST
3267 && int_fits_type_p (val, type))
3268 /* Change in signedness doesn't matter
3269 if a constant value is unaffected. */
3270 ;
ce9895ae
RS
3271 /* If the value is extended from a narrower
3272 unsigned type, it doesn't matter whether we
3273 pass it as signed or unsigned; the value
3274 certainly is the same either way. */
8ce94e44
JM
3275 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3276 && TYPE_UNSIGNED (valtype))
ce9895ae 3277 ;
8df83eae 3278 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3279 warning_at (ploc, OPT_Wtraditional_conversion,
3280 "passing argument %d of %qE "
3281 "as unsigned due to prototype",
3282 argnum, rname);
3ed56f8a 3283 else
5c1bc275
MP
3284 warning_at (ploc, OPT_Wtraditional_conversion,
3285 "passing argument %d of %qE "
3286 "as signed due to prototype",
3287 argnum, rname);
400fbf9f
JW
3288 }
3289 }
3290
8ce94e44
JM
3291 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3292 sake of better warnings from convert_and_check. */
3293 if (excess_precision)
3294 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3295 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3296 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3297 val, origtype, ic_argpass,
3298 npc, fundecl, function,
2ac2f164 3299 parmnum + 1);
2f6e4e97 3300
61f71b34 3301 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3302 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3303 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3304 parmval = default_conversion (parmval);
400fbf9f 3305 }
400fbf9f 3306 }
8ce94e44
JM
3307 else if (TREE_CODE (valtype) == REAL_TYPE
3308 && (TYPE_PRECISION (valtype)
2531a1d9 3309 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3310 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3311 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3312 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3313 {
3314 if (type_generic)
bbbbb16a 3315 parmval = val;
b5d32c25 3316 else
0a0b3574
MM
3317 {
3318 /* Convert `float' to `double'. */
3319 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3320 warning_at (ploc, OPT_Wdouble_promotion,
3321 "implicit conversion from %qT to %qT when passing "
3322 "argument to function",
3323 valtype, double_type_node);
0a0b3574
MM
3324 parmval = convert (double_type_node, val);
3325 }
b5d32c25 3326 }
8ce94e44
JM
3327 else if (excess_precision && !type_generic)
3328 /* A "double" argument with excess precision being passed
3329 without a prototype or in variable arguments. */
bbbbb16a 3330 parmval = convert (valtype, val);
c22cacf3
MS
3331 else if ((invalid_func_diag =
3332 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3333 {
3334 error (invalid_func_diag);
94a0dd7b 3335 return -1;
4d3e6fae 3336 }
400fbf9f
JW
3337 else
3338 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3339 parmval = default_conversion (val);
3340
9771b263 3341 (*values)[parmnum] = parmval;
06302a02
JJ
3342 if (parmval == error_mark_node)
3343 error_args = true;
400fbf9f
JW
3344
3345 if (typetail)
3346 typetail = TREE_CHAIN (typetail);
3347 }
3348
9771b263 3349 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3350
400fbf9f 3351 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3352 {
68fca595 3353 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3354 inform_declaration (fundecl);
3355 return -1;
3789b316 3356 }
400fbf9f 3357
06302a02 3358 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3359}
3360\f
43f6dfd3
RS
3361/* This is the entry point used by the parser to build unary operators
3362 in the input. CODE, a tree_code, specifies the unary operator, and
3363 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3364 CONVERT_EXPR for code.
3365
3366 LOC is the location to use for the tree generated.
3367*/
43f6dfd3
RS
3368
3369struct c_expr
c2255bc4 3370parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3371{
3372 struct c_expr result;
3373
c9f9eb5d 3374 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3375 result.original_code = code;
6866c6e8
ILT
3376 result.original_type = NULL;
3377
59c0753d 3378 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3379 overflow_warning (loc, result.value);
59c0753d 3380
43f6dfd3
RS
3381 return result;
3382}
3383
3384/* This is the entry point used by the parser to build binary operators
3385 in the input. CODE, a tree_code, specifies the binary operator, and
3386 ARG1 and ARG2 are the operands. In addition to constructing the
3387 expression, we check for operands that were written with other binary
ba47d38d
AH
3388 operators in a way that is likely to confuse the user.
3389
3390 LOCATION is the location of the binary operator. */
edc7c4ec 3391
487a92fe 3392struct c_expr
ba47d38d
AH
3393parser_build_binary_op (location_t location, enum tree_code code,
3394 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3395{
487a92fe 3396 struct c_expr result;
400fbf9f 3397
487a92fe
JM
3398 enum tree_code code1 = arg1.original_code;
3399 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3400 tree type1 = (arg1.original_type
3401 ? arg1.original_type
3402 : TREE_TYPE (arg1.value));
3403 tree type2 = (arg2.original_type
3404 ? arg2.original_type
3405 : TREE_TYPE (arg2.value));
400fbf9f 3406
ba47d38d
AH
3407 result.value = build_binary_op (location, code,
3408 arg1.value, arg2.value, 1);
487a92fe 3409 result.original_code = code;
6866c6e8 3410 result.original_type = NULL;
58bf601b 3411
487a92fe
JM
3412 if (TREE_CODE (result.value) == ERROR_MARK)
3413 return result;
400fbf9f 3414
ba47d38d
AH
3415 if (location != UNKNOWN_LOCATION)
3416 protected_set_expr_location (result.value, location);
3417
400fbf9f 3418 /* Check for cases such as x+y<<z which users are likely
487a92fe 3419 to misinterpret. */
400fbf9f 3420 if (warn_parentheses)
393e8e8b
MP
3421 warn_about_parentheses (location, code, code1, arg1.value, code2,
3422 arg2.value);
001af587 3423
ca409efd 3424 if (warn_logical_op)
393e8e8b 3425 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3426 code1, arg1.value, code2, arg2.value);
63a08740 3427
742938c9 3428 if (warn_logical_not_paren
47c2554f
MP
3429 && code1 == TRUTH_NOT_EXPR
3430 && code2 != TRUTH_NOT_EXPR)
59ea0364 3431 warn_logical_not_parentheses (location, code, arg2.value);
742938c9 3432
e994a705
RS
3433 /* Warn about comparisons against string literals, with the exception
3434 of testing for equality or inequality of a string literal with NULL. */
3435 if (code == EQ_EXPR || code == NE_EXPR)
3436 {
3437 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3438 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3439 warning_at (location, OPT_Waddress,
3440 "comparison with string literal results in unspecified behavior");
e994a705
RS
3441 }
3442 else if (TREE_CODE_CLASS (code) == tcc_comparison
3443 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3444 warning_at (location, OPT_Waddress,
3445 "comparison with string literal results in unspecified behavior");
e994a705 3446
b8698a0f
L
3447 if (TREE_OVERFLOW_P (result.value)
3448 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3449 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3450 overflow_warning (location, result.value);
400fbf9f 3451
6866c6e8
ILT
3452 /* Warn about comparisons of different enum types. */
3453 if (warn_enum_compare
3454 && TREE_CODE_CLASS (code) == tcc_comparison
3455 && TREE_CODE (type1) == ENUMERAL_TYPE
3456 && TREE_CODE (type2) == ENUMERAL_TYPE
3457 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3458 warning_at (location, OPT_Wenum_compare,
3459 "comparison between %qT and %qT",
3460 type1, type2);
3461
400fbf9f
JW
3462 return result;
3463}
3e4093b6 3464\f
3e4093b6
RS
3465/* Return a tree for the difference of pointers OP0 and OP1.
3466 The resulting tree has type int. */
293c9fdd 3467
3e4093b6 3468static tree
db3927fb 3469pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3470{
3e4093b6 3471 tree restype = ptrdiff_type_node;
36c5e70a 3472 tree result, inttype;
400fbf9f 3473
36c5e70a
BE
3474 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3475 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3476 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3477 tree orig_op1 = op1;
400fbf9f 3478
36c5e70a
BE
3479 /* If the operands point into different address spaces, we need to
3480 explicitly convert them to pointers into the common address space
3481 before we can subtract the numerical address values. */
3482 if (as0 != as1)
3483 {
3484 addr_space_t as_common;
3485 tree common_type;
3486
3487 /* Determine the common superset address space. This is guaranteed
3488 to exist because the caller verified that comp_target_types
3489 returned non-zero. */
3490 if (!addr_space_superset (as0, as1, &as_common))
3491 gcc_unreachable ();
3492
3493 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3494 op0 = convert (common_type, op0);
3495 op1 = convert (common_type, op1);
3496 }
3497
3498 /* Determine integer type to perform computations in. This will usually
3499 be the same as the result type (ptrdiff_t), but may need to be a wider
3500 type if pointers for the address space are wider than ptrdiff_t. */
3501 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3502 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3503 else
3504 inttype = restype;
3505
fcf73884 3506 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3507 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3508 "pointer of type %<void *%> used in subtraction");
3509 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3510 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3511 "pointer to a function used in subtraction");
400fbf9f 3512
3e4093b6
RS
3513 /* First do the subtraction as integers;
3514 then drop through to build the divide operator.
3515 Do not do default conversions on the minus operator
3516 in case restype is a short type. */
400fbf9f 3517
db3927fb 3518 op0 = build_binary_op (loc,
36c5e70a
BE
3519 MINUS_EXPR, convert (inttype, op0),
3520 convert (inttype, op1), 0);
3e4093b6
RS
3521 /* This generates an error if op1 is pointer to incomplete type. */
3522 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3523 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3524
3e4093b6 3525 op1 = c_size_in_bytes (target_type);
400fbf9f 3526
f04dda30
MP
3527 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3528 error_at (loc, "arithmetic on pointer to an empty aggregate");
3529
3e4093b6 3530 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3531 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3532 op0, convert (inttype, op1));
3533
3534 /* Convert to final result type if necessary. */
3535 return convert (restype, result);
3e4093b6
RS
3536}
3537\f
267bac10
JM
3538/* Expand atomic compound assignments into an approriate sequence as
3539 specified by the C11 standard section 6.5.16.2.
3540 given
3541 _Atomic T1 E1
3542 T2 E2
3543 E1 op= E2
3544
3545 This sequence is used for all types for which these operations are
3546 supported.
3547
3548 In addition, built-in versions of the 'fe' prefixed routines may
3549 need to be invoked for floating point (real, complex or vector) when
3550 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3551
3552 T1 newval;
3553 T1 old;
3554 T1 *addr
3555 T2 val
3556 fenv_t fenv
3557
3558 addr = &E1;
3559 val = (E2);
3560 __atomic_load (addr, &old, SEQ_CST);
3561 feholdexcept (&fenv);
3562loop:
3563 newval = old op val;
3564 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3565 SEQ_CST))
3566 goto done;
3567 feclearexcept (FE_ALL_EXCEPT);
3568 goto loop:
3569done:
3570 feupdateenv (&fenv);
3571
3572 Also note that the compiler is simply issuing the generic form of
3573 the atomic operations. This requires temp(s) and has their address
3574 taken. The atomic processing is smart enough to figure out when the
3575 size of an object can utilize a lock-free version, and convert the
3576 built-in call to the appropriate lock-free routine. The optimizers
3577 will then dispose of any temps that are no longer required, and
3578 lock-free implementations are utilized as long as there is target
3579 support for the required size.
3580
3581 If the operator is NOP_EXPR, then this is a simple assignment, and
3582 an __atomic_store is issued to perform the assignment rather than
3583 the above loop.
3584
3585*/
3586
3587/* Build an atomic assignment at LOC, expanding into the proper
3588 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3589 the result of the operation, unless RETURN_OLD_P in which case
3590 return the old value of LHS (this is only for postincrement and
3591 postdecrement). */
3592static tree
3593build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3594 tree rhs, bool return_old_p)
3595{
3596 tree fndecl, func_call;
3597 vec<tree, va_gc> *params;
3598 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3599 tree old, old_addr;
3600 tree compound_stmt;
3601 tree stmt, goto_stmt;
3602 tree loop_label, loop_decl, done_label, done_decl;
3603
3604 tree lhs_type = TREE_TYPE (lhs);
3605 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3606 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3607 tree rhs_type = TREE_TYPE (rhs);
3608
3609 gcc_assert (TYPE_ATOMIC (lhs_type));
3610
3611 if (return_old_p)
3612 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3613
3614 /* Allocate enough vector items for a compare_exchange. */
3615 vec_alloc (params, 6);
3616
3617 /* Create a compound statement to hold the sequence of statements
3618 with a loop. */
3619 compound_stmt = c_begin_compound_stmt (false);
3620
3621 /* Fold the RHS if it hasn't already been folded. */
3622 if (modifycode != NOP_EXPR)
3623 rhs = c_fully_fold (rhs, false, NULL);
3624
3625 /* Remove the qualifiers for the rest of the expressions and create
3626 the VAL temp variable to hold the RHS. */
3627 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3628 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3629 val = create_tmp_var (nonatomic_rhs_type, NULL);
3630 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3631 TREE_NO_WARNING (val) = 1;
267bac10
JM
3632 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3633 SET_EXPR_LOCATION (rhs, loc);
3634 add_stmt (rhs);
3635
3636 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3637 an atomic_store. */
3638 if (modifycode == NOP_EXPR)
3639 {
3640 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3641 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3642 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3643 params->quick_push (lhs_addr);
3644 params->quick_push (rhs);
3645 params->quick_push (seq_cst);
8edbfaa6 3646 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3647 add_stmt (func_call);
3648
3649 /* Finish the compound statement. */
3650 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3651
3652 /* VAL is the value which was stored, return a COMPOUND_STMT of
3653 the statement and that value. */
3654 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3655 }
3656
3657 /* Create the variables and labels required for the op= form. */
3658 old = create_tmp_var (nonatomic_lhs_type, NULL);
3659 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3660 TREE_ADDRESSABLE (old) = 1;
3661 TREE_NO_WARNING (old) = 1;
267bac10
JM
3662
3663 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3664 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3665 TREE_ADDRESSABLE (newval) = 1;
3666
3667 loop_decl = create_artificial_label (loc);
3668 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3669
3670 done_decl = create_artificial_label (loc);
3671 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3672
3673 /* __atomic_load (addr, &old, SEQ_CST). */
3674 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3675 params->quick_push (lhs_addr);
3676 params->quick_push (old_addr);
3677 params->quick_push (seq_cst);
8edbfaa6 3678 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3679 add_stmt (func_call);
3680 params->truncate (0);
3681
3682 /* Create the expressions for floating-point environment
3683 manipulation, if required. */
3684 bool need_fenv = (flag_trapping_math
3685 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3686 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3687 if (need_fenv)
3688 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3689
3690 if (hold_call)
3691 add_stmt (hold_call);
3692
3693 /* loop: */
3694 add_stmt (loop_label);
3695
3696 /* newval = old + val; */
3697 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3698 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3699 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3700 NULL_TREE, 0);
3701 if (rhs != error_mark_node)
3702 {
3703 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3704 SET_EXPR_LOCATION (rhs, loc);
3705 add_stmt (rhs);
3706 }
3707
3708 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3709 goto done; */
3710 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3711 params->quick_push (lhs_addr);
3712 params->quick_push (old_addr);
3713 params->quick_push (newval_addr);
3714 params->quick_push (integer_zero_node);
3715 params->quick_push (seq_cst);
3716 params->quick_push (seq_cst);
8edbfaa6 3717 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3718
3719 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3720 SET_EXPR_LOCATION (goto_stmt, loc);
3721
3722 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3723 SET_EXPR_LOCATION (stmt, loc);
3724 add_stmt (stmt);
3725
3726 if (clear_call)
3727 add_stmt (clear_call);
3728
3729 /* goto loop; */
3730 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3731 SET_EXPR_LOCATION (goto_stmt, loc);
3732 add_stmt (goto_stmt);
3733
3734 /* done: */
3735 add_stmt (done_label);
3736
3737 if (update_call)
3738 add_stmt (update_call);
3739
3740 /* Finish the compound statement. */
3741 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3742
3743 /* NEWVAL is the value that was successfully stored, return a
3744 COMPOUND_EXPR of the statement and the appropriate value. */
3745 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3746 return_old_p ? old : newval);
3747}
3748
3e4093b6
RS
3749/* Construct and perhaps optimize a tree representation
3750 for a unary operation. CODE, a tree_code, specifies the operation
3751 and XARG is the operand.
3752 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3753 the default promotions (such as from short to int).
3754 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3755 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3756 arrays to pointers in C99.
3757
3758 LOCATION is the location of the operator. */
400fbf9f 3759
3e4093b6 3760tree
c9f9eb5d
AH
3761build_unary_op (location_t location,
3762 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3763{
3764 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3765 tree arg = xarg;
3766 tree argtype = 0;
808d6eaa 3767 enum tree_code typecode;
3e4093b6 3768 tree val;
c9f9eb5d 3769 tree ret = error_mark_node;
8ce94e44 3770 tree eptype = NULL_TREE;
3e4093b6 3771 int noconvert = flag;
4de67c26 3772 const char *invalid_op_diag;
928c19bb
JM
3773 bool int_operands;
3774
3775 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3776 if (int_operands)
3777 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3778
808d6eaa
JM
3779 if (code != ADDR_EXPR)
3780 arg = require_complete_type (arg);
3781
3782 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3783 if (typecode == ERROR_MARK)
3784 return error_mark_node;
3785 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3786 typecode = INTEGER_TYPE;
6c36d76b 3787
4de67c26
JM
3788 if ((invalid_op_diag
3789 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3790 {
c9f9eb5d 3791 error_at (location, invalid_op_diag);
4de67c26
JM
3792 return error_mark_node;
3793 }
3794
8ce94e44
JM
3795 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3796 {
3797 eptype = TREE_TYPE (arg);
3798 arg = TREE_OPERAND (arg, 0);
3799 }
3800
3e4093b6
RS
3801 switch (code)
3802 {
3803 case CONVERT_EXPR:
3804 /* This is used for unary plus, because a CONVERT_EXPR
3805 is enough to prevent anybody from looking inside for
3806 associativity, but won't generate any code. */
3807 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3808 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3809 || typecode == VECTOR_TYPE))
400fbf9f 3810 {
c9f9eb5d 3811 error_at (location, "wrong type argument to unary plus");
3e4093b6 3812 return error_mark_node;
400fbf9f 3813 }
3e4093b6
RS
3814 else if (!noconvert)
3815 arg = default_conversion (arg);
db3927fb 3816 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3817 break;
3818
3e4093b6
RS
3819 case NEGATE_EXPR:
3820 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3821 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3822 || typecode == VECTOR_TYPE))
3823 {
c9f9eb5d 3824 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3825 return error_mark_node;
3826 }
3827 else if (!noconvert)
3828 arg = default_conversion (arg);
400fbf9f
JW
3829 break;
3830
3e4093b6 3831 case BIT_NOT_EXPR:
462643f0
AP
3832 /* ~ works on integer types and non float vectors. */
3833 if (typecode == INTEGER_TYPE
3834 || (typecode == VECTOR_TYPE
3835 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3836 {
3e4093b6
RS
3837 if (!noconvert)
3838 arg = default_conversion (arg);
03d5b1f5 3839 }
3e4093b6 3840 else if (typecode == COMPLEX_TYPE)
400fbf9f 3841 {
3e4093b6 3842 code = CONJ_EXPR;
c1771a20 3843 pedwarn (location, OPT_Wpedantic,
fcf73884 3844 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3845 if (!noconvert)
3846 arg = default_conversion (arg);
3847 }
3848 else
3849 {
c9f9eb5d 3850 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3851 return error_mark_node;
400fbf9f
JW
3852 }
3853 break;
3854
3e4093b6 3855 case ABS_EXPR:
11017cc7 3856 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3857 {
c9f9eb5d 3858 error_at (location, "wrong type argument to abs");
3e4093b6 3859 return error_mark_node;
400fbf9f 3860 }
3e4093b6
RS
3861 else if (!noconvert)
3862 arg = default_conversion (arg);
400fbf9f
JW
3863 break;
3864
3e4093b6
RS
3865 case CONJ_EXPR:
3866 /* Conjugating a real value is a no-op, but allow it anyway. */
3867 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3868 || typecode == COMPLEX_TYPE))
400fbf9f 3869 {
c9f9eb5d 3870 error_at (location, "wrong type argument to conjugation");
3e4093b6 3871 return error_mark_node;
400fbf9f 3872 }
3e4093b6
RS
3873 else if (!noconvert)
3874 arg = default_conversion (arg);
400fbf9f
JW
3875 break;
3876
3e4093b6 3877 case TRUTH_NOT_EXPR:
ab22c1fa 3878 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3879 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3880 && typecode != COMPLEX_TYPE)
400fbf9f 3881 {
c9f9eb5d
AH
3882 error_at (location,
3883 "wrong type argument to unary exclamation mark");
3e4093b6 3884 return error_mark_node;
400fbf9f 3885 }
a27d595d
JM
3886 if (int_operands)
3887 {
3888 arg = c_objc_common_truthvalue_conversion (location, xarg);
3889 arg = remove_c_maybe_const_expr (arg);
3890 }
3891 else
3892 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3893 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3894 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3895 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3896 location = EXPR_LOCATION (ret);
c9f9eb5d 3897 goto return_build_unary_op;
3e4093b6 3898
3e4093b6 3899 case REALPART_EXPR:
3e4093b6 3900 case IMAGPART_EXPR:
fb52b50a
NF
3901 ret = build_real_imag_expr (location, code, arg);
3902 if (ret == error_mark_node)
3903 return error_mark_node;
8ce94e44
JM
3904 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3905 eptype = TREE_TYPE (eptype);
c9f9eb5d 3906 goto return_build_unary_op;
3e4093b6
RS
3907
3908 case PREINCREMENT_EXPR:
3909 case POSTINCREMENT_EXPR:
3910 case PREDECREMENT_EXPR:
3911 case POSTDECREMENT_EXPR:
3e4093b6 3912
928c19bb
JM
3913 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3914 {
3915 tree inner = build_unary_op (location, code,
3916 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3917 if (inner == error_mark_node)
3918 return error_mark_node;
3919 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3920 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3921 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3922 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3923 goto return_build_unary_op;
3924 }
3925
925e8657
NP
3926 /* Complain about anything that is not a true lvalue. In
3927 Objective-C, skip this check for property_refs. */
f90e8e2e 3928 if (!objc_is_property_ref (arg)
7bd11157
TT
3929 && !lvalue_or_else (location,
3930 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3931 || code == POSTINCREMENT_EXPR)
3932 ? lv_increment
3933 : lv_decrement)))
928c19bb
JM
3934 return error_mark_node;
3935
09639a83
ILT
3936 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3937 {
3938 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3939 warning_at (location, OPT_Wc___compat,
3940 "increment of enumeration value is invalid in C++");
3941 else
3942 warning_at (location, OPT_Wc___compat,
3943 "decrement of enumeration value is invalid in C++");
3944 }
3945
928c19bb
JM
3946 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3947 arg = c_fully_fold (arg, false, NULL);
3948
267bac10
JM
3949 bool atomic_op;
3950 atomic_op = really_atomic_lvalue (arg);
3951
3e4093b6
RS
3952 /* Increment or decrement the real part of the value,
3953 and don't change the imaginary part. */
3954 if (typecode == COMPLEX_TYPE)
400fbf9f 3955 {
3e4093b6
RS
3956 tree real, imag;
3957
c1771a20 3958 pedwarn (location, OPT_Wpedantic,
509c9d60 3959 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3960
267bac10
JM
3961 if (!atomic_op)
3962 {
3963 arg = stabilize_reference (arg);
3964 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3965 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3966 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3967 if (real == error_mark_node || imag == error_mark_node)
3968 return error_mark_node;
3969 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3970 real, imag);
3971 goto return_build_unary_op;
3972 }
400fbf9f 3973 }
3e4093b6
RS
3974
3975 /* Report invalid types. */
3976
ab22c1fa 3977 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3978 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 3979 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 3980 {
3e4093b6 3981 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 3982 error_at (location, "wrong type argument to increment");
c22cacf3 3983 else
c9f9eb5d 3984 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
3985
3986 return error_mark_node;
400fbf9f 3987 }
400fbf9f 3988
3e4093b6
RS
3989 {
3990 tree inc;
400fbf9f 3991
3e4093b6
RS
3992 argtype = TREE_TYPE (arg);
3993
3994 /* Compute the increment. */
3995
3996 if (typecode == POINTER_TYPE)
3997 {
a0e24419 3998 /* If pointer target is an incomplete type,
3e4093b6 3999 we just cannot know how to do the arithmetic. */
b70cef5d 4000 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4001 {
4002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4003 error_at (location,
a0e24419
MP
4004 "increment of pointer to an incomplete type %qT",
4005 TREE_TYPE (argtype));
3e4093b6 4006 else
c9f9eb5d 4007 error_at (location,
a0e24419
MP
4008 "decrement of pointer to an incomplete type %qT",
4009 TREE_TYPE (argtype));
3e4093b6 4010 }
b70cef5d
JJ
4011 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4012 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4013 {
3e4093b6 4014 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4015 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4016 "wrong type argument to increment");
3e4093b6 4017 else
44d90fe1 4018 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4019 "wrong type argument to decrement");
3e4093b6
RS
4020 }
4021
b70cef5d 4022 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4023 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4024 }
b70cef5d 4025 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4026 {
4027 /* For signed fract types, we invert ++ to -- or
4028 -- to ++, and change inc from 1 to -1, because
4029 it is not possible to represent 1 in signed fract constants.
4030 For unsigned fract types, the result always overflows and
4031 we get an undefined (original) or the maximum value. */
4032 if (code == PREINCREMENT_EXPR)
4033 code = PREDECREMENT_EXPR;
4034 else if (code == PREDECREMENT_EXPR)
4035 code = PREINCREMENT_EXPR;
4036 else if (code == POSTINCREMENT_EXPR)
4037 code = POSTDECREMENT_EXPR;
4038 else /* code == POSTDECREMENT_EXPR */
4039 code = POSTINCREMENT_EXPR;
4040
4041 inc = integer_minus_one_node;
4042 inc = convert (argtype, inc);
4043 }
3e4093b6 4044 else
5be014d5 4045 {
241b71bb
TV
4046 inc = VECTOR_TYPE_P (argtype)
4047 ? build_one_cst (argtype)
4048 : integer_one_node;
5be014d5
AP
4049 inc = convert (argtype, inc);
4050 }
3e4093b6 4051
925e8657
NP
4052 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4053 need to ask Objective-C to build the increment or decrement
4054 expression for it. */
4055 if (objc_is_property_ref (arg))
f90e8e2e 4056 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4057 arg, inc);
4058
3e4093b6 4059 /* Report a read-only lvalue. */
f37acdf9 4060 if (TYPE_READONLY (argtype))
953ff289 4061 {
c02065fc 4062 readonly_error (location, arg,
953ff289
DN
4063 ((code == PREINCREMENT_EXPR
4064 || code == POSTINCREMENT_EXPR)
4065 ? lv_increment : lv_decrement));
4066 return error_mark_node;
4067 }
f37acdf9
JM
4068 else if (TREE_READONLY (arg))
4069 readonly_warning (arg,
4070 ((code == PREINCREMENT_EXPR
4071 || code == POSTINCREMENT_EXPR)
4072 ? lv_increment : lv_decrement));
3e4093b6 4073
267bac10
JM
4074 /* If the argument is atomic, use the special code sequences for
4075 atomic compound assignment. */
4076 if (atomic_op)
4077 {
4078 arg = stabilize_reference (arg);
4079 ret = build_atomic_assign (location, arg,
4080 ((code == PREINCREMENT_EXPR
4081 || code == POSTINCREMENT_EXPR)
4082 ? PLUS_EXPR
4083 : MINUS_EXPR),
4084 (FRACT_MODE_P (TYPE_MODE (argtype))
4085 ? inc
4086 : integer_one_node),
4087 (code == POSTINCREMENT_EXPR
4088 || code == POSTDECREMENT_EXPR));
4089 goto return_build_unary_op;
4090 }
4091
3e4093b6
RS
4092 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4093 val = boolean_increment (code, arg);
4094 else
53fb4de3 4095 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4096 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4097 if (TREE_CODE (val) != code)
6de9cd9a 4098 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4099 ret = val;
4100 goto return_build_unary_op;
3e4093b6
RS
4101 }
4102
4103 case ADDR_EXPR:
4104 /* Note that this operation never does default_conversion. */
4105
2b4b7036
JM
4106 /* The operand of unary '&' must be an lvalue (which excludes
4107 expressions of type void), or, in C99, the result of a [] or
4108 unary '*' operator. */
4109 if (VOID_TYPE_P (TREE_TYPE (arg))
4110 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4111 && (TREE_CODE (arg) != INDIRECT_REF
4112 || !flag_isoc99))
4113 pedwarn (location, 0, "taking address of expression of type %<void%>");
4114
3e4093b6
RS
4115 /* Let &* cancel out to simplify resulting code. */
4116 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4117 {
3e4093b6
RS
4118 /* Don't let this be an lvalue. */
4119 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4120 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4121 ret = TREE_OPERAND (arg, 0);
4122 goto return_build_unary_op;
400fbf9f 4123 }
1eb8759b 4124
7c672dfc 4125 /* For &x[y], return x+y */
3e4093b6 4126 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4127 {
f2a71bbc
JM
4128 tree op0 = TREE_OPERAND (arg, 0);
4129 if (!c_mark_addressable (op0))
3e4093b6 4130 return error_mark_node;
1eb8759b 4131 }
1eb8759b 4132
3e4093b6
RS
4133 /* Anything not already handled and not a true memory reference
4134 or a non-lvalue array is an error. */
4135 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4136 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4137 return error_mark_node;
b6a10c9f 4138
928c19bb
JM
4139 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4140 folding later. */
4141 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4142 {
4143 tree inner = build_unary_op (location, code,
4144 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4145 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4146 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4147 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4148 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4149 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4150 goto return_build_unary_op;
4151 }
4152
3e4093b6
RS
4153 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4154 argtype = TREE_TYPE (arg);
400fbf9f 4155
3e4093b6 4156 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4157 to which the address will point. This is only needed
f2c1da78 4158 for function types. */
6615c446 4159 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4160 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4161 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4162 {
4163 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4164 int quals = orig_quals;
4165
4166 if (TREE_READONLY (arg))
4167 quals |= TYPE_QUAL_CONST;
4168 if (TREE_THIS_VOLATILE (arg))
4169 quals |= TYPE_QUAL_VOLATILE;
4170
f2c1da78
JM
4171 argtype = c_build_qualified_type (argtype, quals);
4172 }
400fbf9f 4173
3e4093b6
RS
4174 if (!c_mark_addressable (arg))
4175 return error_mark_node;
400fbf9f 4176
abb54d14
JM
4177 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4178 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4179
5cc200fc 4180 argtype = build_pointer_type (argtype);
5e55f99d
RH
4181
4182 /* ??? Cope with user tricks that amount to offsetof. Delete this
4183 when we have proper support for integer constant expressions. */
4184 val = get_base_address (arg);
4185 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4186 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4187 {
cf9e9959 4188 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4189 goto return_build_unary_op;
3aa2ddb8 4190 }
5e55f99d 4191
5cc200fc 4192 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4193
c9f9eb5d
AH
4194 ret = val;
4195 goto return_build_unary_op;
400fbf9f 4196
3e4093b6 4197 default:
1344f9a3 4198 gcc_unreachable ();
3e4093b6 4199 }
400fbf9f 4200
3e4093b6
RS
4201 if (argtype == 0)
4202 argtype = TREE_TYPE (arg);
928c19bb
JM
4203 if (TREE_CODE (arg) == INTEGER_CST)
4204 ret = (require_constant_value
db3927fb
AH
4205 ? fold_build1_initializer_loc (location, code, argtype, arg)
4206 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4207 else
4208 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4209 return_build_unary_op:
4210 gcc_assert (ret != error_mark_node);
928c19bb
JM
4211 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4212 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4213 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4214 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4215 ret = note_integer_operands (ret);
8ce94e44
JM
4216 if (eptype)
4217 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4218 protected_set_expr_location (ret, location);
4219 return ret;
3e4093b6 4220}
400fbf9f 4221
3e4093b6
RS
4222/* Return nonzero if REF is an lvalue valid for this language.
4223 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4224 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4225
1e4ae551 4226bool
58f9752a 4227lvalue_p (const_tree ref)
3e4093b6 4228{
58f9752a 4229 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4230
3e4093b6
RS
4231 switch (code)
4232 {
4233 case REALPART_EXPR:
4234 case IMAGPART_EXPR:
4235 case COMPONENT_REF:
4236 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4237
928c19bb
JM
4238 case C_MAYBE_CONST_EXPR:
4239 return lvalue_p (TREE_OPERAND (ref, 1));
4240
3e4093b6
RS
4241 case COMPOUND_LITERAL_EXPR:
4242 case STRING_CST:
4243 return 1;
400fbf9f 4244
3e4093b6
RS
4245 case INDIRECT_REF:
4246 case ARRAY_REF:
36536d79 4247 case ARRAY_NOTATION_REF:
3e4093b6
RS
4248 case VAR_DECL:
4249 case PARM_DECL:
4250 case RESULT_DECL:
4251 case ERROR_MARK:
4252 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4253 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4254
3e4093b6 4255 case BIND_EXPR:
3e4093b6 4256 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4257
3e4093b6
RS
4258 default:
4259 return 0;
4260 }
4261}
400fbf9f 4262\f
f37acdf9
JM
4263/* Give a warning for storing in something that is read-only in GCC
4264 terms but not const in ISO C terms. */
4265
4266static void
4267readonly_warning (tree arg, enum lvalue_use use)
4268{
4269 switch (use)
4270 {
4271 case lv_assign:
4272 warning (0, "assignment of read-only location %qE", arg);
4273 break;
4274 case lv_increment:
4275 warning (0, "increment of read-only location %qE", arg);
4276 break;
4277 case lv_decrement:
4278 warning (0, "decrement of read-only location %qE", arg);
4279 break;
4280 default:
4281 gcc_unreachable ();
4282 }
4283 return;
4284}
4285
37dc0d8d
JM
4286
4287/* Return nonzero if REF is an lvalue valid for this language;
4288 otherwise, print an error message and return zero. USE says
7bd11157
TT
4289 how the lvalue is being used and so selects the error message.
4290 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4291
4292static int
7bd11157 4293lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4294{
4295 int win = lvalue_p (ref);
4296
4297 if (!win)
7bd11157 4298 lvalue_error (loc, use);
37dc0d8d
JM
4299
4300 return win;
4301}
3e4093b6
RS
4302\f
4303/* Mark EXP saying that we need to be able to take the
4304 address of it; it should not be allocated in a register.
4305 Returns true if successful. */
54c93c30 4306
3e4093b6
RS
4307bool
4308c_mark_addressable (tree exp)
400fbf9f 4309{
3e4093b6 4310 tree x = exp;
95602da1 4311
3e4093b6
RS
4312 while (1)
4313 switch (TREE_CODE (x))
4314 {
4315 case COMPONENT_REF:
4316 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4317 {
0039fa55
AN
4318 error
4319 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4320 return false;
4321 }
95602da1 4322
3e4093b6 4323 /* ... fall through ... */
95602da1 4324
3e4093b6
RS
4325 case ADDR_EXPR:
4326 case ARRAY_REF:
4327 case REALPART_EXPR:
4328 case IMAGPART_EXPR:
4329 x = TREE_OPERAND (x, 0);
4330 break;
95602da1 4331
3e4093b6
RS
4332 case COMPOUND_LITERAL_EXPR:
4333 case CONSTRUCTOR:
4334 TREE_ADDRESSABLE (x) = 1;
4335 return true;
95602da1 4336
3e4093b6
RS
4337 case VAR_DECL:
4338 case CONST_DECL:
4339 case PARM_DECL:
4340 case RESULT_DECL:
5baeaac0 4341 if (C_DECL_REGISTER (x)
3e4093b6
RS
4342 && DECL_NONLOCAL (x))
4343 {
e697b20f 4344 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4345 {
0039fa55
AN
4346 error
4347 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4348 return false;
4349 }
509c9d60 4350 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4351 }
5baeaac0 4352 else if (C_DECL_REGISTER (x))
3e4093b6 4353 {
e697b20f 4354 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4355 error ("address of global register variable %qD requested", x);
4356 else
4357 error ("address of register variable %qD requested", x);
4358 return false;
3e4093b6 4359 }
400fbf9f 4360
3e4093b6
RS
4361 /* drops in */
4362 case FUNCTION_DECL:
4363 TREE_ADDRESSABLE (x) = 1;
4364 /* drops out */
4365 default:
4366 return true;
4367 }
4368}
4369\f
2d2e923f
MLI
4370/* Convert EXPR to TYPE, warning about conversion problems with
4371 constants. SEMANTIC_TYPE is the type this conversion would use
4372 without excess precision. If SEMANTIC_TYPE is NULL, this function
4373 is equivalent to convert_and_check. This function is a wrapper that
4374 handles conversions that may be different than
4375 the usual ones because of excess precision. */
4376
4377static tree
68fca595
MP
4378ep_convert_and_check (location_t loc, tree type, tree expr,
4379 tree semantic_type)
2d2e923f
MLI
4380{
4381 if (TREE_TYPE (expr) == type)
4382 return expr;
4383
4384 if (!semantic_type)
68fca595 4385 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4386
4387 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4388 && TREE_TYPE (expr) != semantic_type)
4389 {
4390 /* For integers, we need to check the real conversion, not
4391 the conversion to the excess precision type. */
68fca595 4392 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4393 }
4394 /* Result type is the excess precision type, which should be
4395 large enough, so do not check. */
4396 return convert (type, expr);
4397}
4398
928c19bb
JM
4399/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4400 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4401 if folded to an integer constant then the unselected half may
4402 contain arbitrary operations not normally permitted in constant
c2255bc4 4403 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4404
4405tree
744aa42f 4406build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4407 tree op1, tree op1_original_type, tree op2,
4408 tree op2_original_type)
400fbf9f 4409{
3e4093b6
RS
4410 tree type1;
4411 tree type2;
4412 enum tree_code code1;
4413 enum tree_code code2;
4414 tree result_type = NULL;
2d2e923f 4415 tree semantic_result_type = NULL;
3e4093b6 4416 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4417 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4418 bool ifexp_int_operands;
928c19bb 4419 tree ret;
400fbf9f 4420
4d84fe7c
JM
4421 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4422 if (op1_int_operands)
4423 op1 = remove_c_maybe_const_expr (op1);
4424 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4425 if (op2_int_operands)
4426 op2 = remove_c_maybe_const_expr (op2);
4427 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4428 if (ifexp_int_operands)
4429 ifexp = remove_c_maybe_const_expr (ifexp);
4430
3e4093b6
RS
4431 /* Promote both alternatives. */
4432
4433 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4434 op1 = default_conversion (op1);
4435 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4436 op2 = default_conversion (op2);
4437
4438 if (TREE_CODE (ifexp) == ERROR_MARK
4439 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4440 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4441 return error_mark_node;
400fbf9f 4442
3e4093b6
RS
4443 type1 = TREE_TYPE (op1);
4444 code1 = TREE_CODE (type1);
4445 type2 = TREE_TYPE (op2);
4446 code2 = TREE_CODE (type2);
4447
b1adf557
JM
4448 /* C90 does not permit non-lvalue arrays in conditional expressions.
4449 In C99 they will be pointers by now. */
4450 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4451 {
744aa42f 4452 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4453 return error_mark_node;
4454 }
4455
8ce94e44
JM
4456 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4457 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4458 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4459 || code1 == COMPLEX_TYPE)
4460 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4461 || code2 == COMPLEX_TYPE))
4462 {
2d2e923f 4463 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4464 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4465 {
4466 op1 = TREE_OPERAND (op1, 0);
4467 type1 = TREE_TYPE (op1);
4468 gcc_assert (TREE_CODE (type1) == code1);
4469 }
4470 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4471 {
4472 op2 = TREE_OPERAND (op2, 0);
4473 type2 = TREE_TYPE (op2);
4474 gcc_assert (TREE_CODE (type2) == code2);
4475 }
4476 }
4477
d130ae11
ILT
4478 if (warn_cxx_compat)
4479 {
4480 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4481 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4482
4483 if (TREE_CODE (t1) == ENUMERAL_TYPE
4484 && TREE_CODE (t2) == ENUMERAL_TYPE
4485 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4486 warning_at (colon_loc, OPT_Wc___compat,
4487 ("different enum types in conditional is "
4488 "invalid in C++: %qT vs %qT"),
4489 t1, t2);
4490 }
4491
3e4093b6
RS
4492 /* Quickly detect the usual case where op1 and op2 have the same type
4493 after promotion. */
4494 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4495 {
3e4093b6
RS
4496 if (type1 == type2)
4497 result_type = type1;
4498 else
4499 result_type = TYPE_MAIN_VARIANT (type1);
4500 }
4501 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4502 || code1 == COMPLEX_TYPE)
4503 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4504 || code2 == COMPLEX_TYPE))
3e4093b6 4505 {
ccf7f880 4506 result_type = c_common_type (type1, type2);
0a0b3574
MM
4507 do_warn_double_promotion (result_type, type1, type2,
4508 "implicit conversion from %qT to %qT to "
4509 "match other result of conditional",
4510 colon_loc);
400fbf9f 4511
3e4093b6
RS
4512 /* If -Wsign-compare, warn here if type1 and type2 have
4513 different signedness. We'll promote the signed to unsigned
4514 and later code won't know it used to be different.
4515 Do this check on the original types, so that explicit casts
4516 will be considered, but default promotions won't. */
7d882b83 4517 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4518 {
8df83eae
RK
4519 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4520 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4521
3e4093b6
RS
4522 if (unsigned_op1 ^ unsigned_op2)
4523 {
6ac01510
ILT
4524 bool ovf;
4525
3e4093b6
RS
4526 /* Do not warn if the result type is signed, since the
4527 signed type will only be chosen if it can represent
4528 all the values of the unsigned type. */
3f75a254 4529 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4530 /* OK */;
3e4093b6 4531 else
928c19bb
JM
4532 {
4533 bool op1_maybe_const = true;
4534 bool op2_maybe_const = true;
4535
4536 /* Do not warn if the signed quantity is an
4537 unsuffixed integer literal (or some static
4538 constant expression involving such literals) and
4539 it is non-negative. This warning requires the
4540 operands to be folded for best results, so do
4541 that folding in this case even without
4542 warn_sign_compare to avoid warning options
4543 possibly affecting code generation. */
f5178456
RS
4544 c_inhibit_evaluation_warnings
4545 += (ifexp == truthvalue_false_node);
928c19bb
JM
4546 op1 = c_fully_fold (op1, require_constant_value,
4547 &op1_maybe_const);
f5178456
RS
4548 c_inhibit_evaluation_warnings
4549 -= (ifexp == truthvalue_false_node);
4550
4551 c_inhibit_evaluation_warnings
4552 += (ifexp == truthvalue_true_node);
928c19bb
JM
4553 op2 = c_fully_fold (op2, require_constant_value,
4554 &op2_maybe_const);
f5178456
RS
4555 c_inhibit_evaluation_warnings
4556 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4557
4558 if (warn_sign_compare)
4559 {
4560 if ((unsigned_op2
4561 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4562 || (unsigned_op1
4563 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4564 /* OK */;
4565 else
744aa42f
ILT
4566 warning_at (colon_loc, OPT_Wsign_compare,
4567 ("signed and unsigned type in "
4568 "conditional expression"));
928c19bb
JM
4569 }
4570 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4571 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4572 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4573 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4574 }
3e4093b6
RS
4575 }
4576 }
4577 }
4578 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4579 {
fcf73884 4580 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4581 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4582 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4583 result_type = void_type_node;
4584 }
4585 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4586 {
36c5e70a
BE
4587 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4588 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4589 addr_space_t as_common;
4590
744aa42f 4591 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4592 result_type = common_pointer_type (type1, type2);
6aa3c60d 4593 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4594 result_type = type2;
6aa3c60d 4595 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4596 result_type = type1;
4597 else if (!addr_space_superset (as1, as2, &as_common))
4598 {
4599 error_at (colon_loc, "pointers to disjoint address spaces "
4600 "used in conditional expression");
4601 return error_mark_node;
4602 }
267bac10
JM
4603 else if (VOID_TYPE_P (TREE_TYPE (type1))
4604 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4605 {
fcf73884 4606 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4607 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4608 "ISO C forbids conditional expr between "
bda67431 4609 "%<void *%> and function pointer");
3e4093b6
RS
4610 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4611 TREE_TYPE (type2)));
34a80643 4612 }
267bac10
JM
4613 else if (VOID_TYPE_P (TREE_TYPE (type2))
4614 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4615 {
fcf73884 4616 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4617 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4618 "ISO C forbids conditional expr between "
bda67431 4619 "%<void *%> and function pointer");
3e4093b6
RS
4620 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4621 TREE_TYPE (type1)));
1c2a9b35 4622 }
b581b85b
NP
4623 /* Objective-C pointer comparisons are a bit more lenient. */
4624 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4625 result_type = objc_common_type (type1, type2);
34a80643 4626 else
ab87f8c8 4627 {
36c5e70a
BE
4628 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4629
b581b85b
NP
4630 pedwarn (colon_loc, 0,
4631 "pointer type mismatch in conditional expression");
36c5e70a
BE
4632 result_type = build_pointer_type
4633 (build_qualified_type (void_type_node, qual));
ab87f8c8 4634 }
3e4093b6
RS
4635 }
4636 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4637 {
6aa3c60d 4638 if (!null_pointer_constant_p (orig_op2))
744aa42f 4639 pedwarn (colon_loc, 0,
509c9d60 4640 "pointer/integer type mismatch in conditional expression");
3e4093b6 4641 else
ab87f8c8 4642 {
3e4093b6 4643 op2 = null_pointer_node;
ab87f8c8 4644 }
3e4093b6
RS
4645 result_type = type1;
4646 }
4647 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4648 {
6aa3c60d 4649 if (!null_pointer_constant_p (orig_op1))
744aa42f 4650 pedwarn (colon_loc, 0,
509c9d60 4651 "pointer/integer type mismatch in conditional expression");
3e4093b6 4652 else
ab87f8c8 4653 {
3e4093b6 4654 op1 = null_pointer_node;
ab87f8c8 4655 }
3e4093b6
RS
4656 result_type = type2;
4657 }
1c2a9b35 4658
3e4093b6
RS
4659 if (!result_type)
4660 {
4661 if (flag_cond_mismatch)
4662 result_type = void_type_node;
4663 else
400fbf9f 4664 {
c2255bc4 4665 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4666 return error_mark_node;
400fbf9f 4667 }
3e4093b6 4668 }
400fbf9f 4669
3e4093b6
RS
4670 /* Merge const and volatile flags of the incoming types. */
4671 result_type
4672 = build_type_variant (result_type,
afbd0665
AS
4673 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4674 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4675
68fca595
MP
4676 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4677 semantic_result_type);
4678 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4679 semantic_result_type);
b6a10c9f 4680
928c19bb
JM
4681 if (ifexp_bcp && ifexp == truthvalue_true_node)
4682 {
4683 op2_int_operands = true;
4684 op1 = c_fully_fold (op1, require_constant_value, NULL);
4685 }
4686 if (ifexp_bcp && ifexp == truthvalue_false_node)
4687 {
4688 op1_int_operands = true;
4689 op2 = c_fully_fold (op2, require_constant_value, NULL);
4690 }
4d84fe7c 4691 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4692 && op1_int_operands
4693 && op2_int_operands);
4694 if (int_operands)
4695 {
4696 int_const = ((ifexp == truthvalue_true_node
4697 && TREE_CODE (orig_op1) == INTEGER_CST
4698 && !TREE_OVERFLOW (orig_op1))
4699 || (ifexp == truthvalue_false_node
4700 && TREE_CODE (orig_op2) == INTEGER_CST
4701 && !TREE_OVERFLOW (orig_op2)));
4702 }
4703 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4704 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4705 else
4706 {
01c7ccbb
JM
4707 if (int_operands)
4708 {
f34f1c87
MP
4709 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4710 nested inside of the expression. */
4711 op1 = c_fully_fold (op1, false, NULL);
4712 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4713 }
928c19bb
JM
4714 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4715 if (int_operands)
4716 ret = note_integer_operands (ret);
4717 }
2d2e923f
MLI
4718 if (semantic_result_type)
4719 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4720
c2255bc4 4721 protected_set_expr_location (ret, colon_loc);
928c19bb 4722 return ret;
3e4093b6
RS
4723}
4724\f
487a92fe 4725/* Return a compound expression that performs two expressions and
c2255bc4
AH
4726 returns the value of the second of them.
4727
4728 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4729
3e4093b6 4730tree
c2255bc4 4731build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4732{
4d84fe7c 4733 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4734 tree eptype = NULL_TREE;
928c19bb
JM
4735 tree ret;
4736
b72271b9 4737 if (flag_cilkplus
939b37da
BI
4738 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4739 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4740 {
4741 error_at (loc,
4742 "spawned function call cannot be part of a comma expression");
4743 return error_mark_node;
4744 }
4d84fe7c
JM
4745 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4746 if (expr1_int_operands)
4747 expr1 = remove_c_maybe_const_expr (expr1);
4748 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4749 if (expr2_int_operands)
4750 expr2 = remove_c_maybe_const_expr (expr2);
4751
8ce94e44
JM
4752 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4753 expr1 = TREE_OPERAND (expr1, 0);
4754 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4755 {
4756 eptype = TREE_TYPE (expr2);
4757 expr2 = TREE_OPERAND (expr2, 0);
4758 }
4759
3f75a254 4760 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4761 {
4762 /* The left-hand operand of a comma expression is like an expression
c5409249 4763 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4764 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4765 if (warn_unused_value)
47aecf47 4766 {
e14a6540 4767 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4768 && CONVERT_EXPR_P (expr1))
47aecf47 4769 ; /* (void) a, b */
e14a6540
JM
4770 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4771 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4772 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4773 ; /* (void) a, (void) b, c */
4774 else
b8698a0f 4775 warning_at (loc, OPT_Wunused_value,
c2255bc4 4776 "left-hand operand of comma expression has no effect");
47aecf47 4777 }
3e4093b6 4778 }
789eadcd
MP
4779 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4780 && warn_unused_value)
4781 {
4782 tree r = expr1;
4783 location_t cloc = loc;
4784 while (TREE_CODE (r) == COMPOUND_EXPR)
4785 {
4786 if (EXPR_HAS_LOCATION (r))
4787 cloc = EXPR_LOCATION (r);
4788 r = TREE_OPERAND (r, 1);
4789 }
4790 if (!TREE_SIDE_EFFECTS (r)
4791 && !VOID_TYPE_P (TREE_TYPE (r))
4792 && !CONVERT_EXPR_P (r))
4793 warning_at (cloc, OPT_Wunused_value,
4794 "right-hand operand of comma expression has no effect");
4795 }
400fbf9f 4796
3e4093b6
RS
4797 /* With -Wunused, we should also warn if the left-hand operand does have
4798 side-effects, but computes a value which is not used. For example, in
4799 `foo() + bar(), baz()' the result of the `+' operator is not used,
4800 so we should issue a warning. */
4801 else if (warn_unused_value)
c2255bc4 4802 warn_if_unused_value (expr1, loc);
400fbf9f 4803
e63d6886
AP
4804 if (expr2 == error_mark_node)
4805 return error_mark_node;
4806
928c19bb
JM
4807 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4808
4809 if (flag_isoc99
4d84fe7c
JM
4810 && expr1_int_operands
4811 && expr2_int_operands)
928c19bb
JM
4812 ret = note_integer_operands (ret);
4813
8ce94e44
JM
4814 if (eptype)
4815 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4816
c2255bc4 4817 protected_set_expr_location (ret, loc);
928c19bb 4818 return ret;
3e4093b6 4819}
400fbf9f 4820
67165eb3
ILT
4821/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4822 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4823 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4824 of the cast. -Wcast-qual appeared on the command line. Named
4825 address space qualifiers are not handled here, because they result
4826 in different warnings. */
67165eb3
ILT
4827
4828static void
2ee3cb35 4829handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4830{
4831 tree in_type = type;
4832 tree in_otype = otype;
4833 int added = 0;
4834 int discarded = 0;
4835 bool is_const;
4836
4837 /* Check that the qualifiers on IN_TYPE are a superset of the
4838 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4839 nodes is uninteresting and we stop as soon as we hit a
4840 non-POINTER_TYPE node on either type. */
4841 do
4842 {
4843 in_otype = TREE_TYPE (in_otype);
4844 in_type = TREE_TYPE (in_type);
4845
4846 /* GNU C allows cv-qualified function types. 'const' means the
4847 function is very pure, 'volatile' means it can't return. We
4848 need to warn when such qualifiers are added, not when they're
4849 taken away. */
4850 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4851 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4852 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4853 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4854 else
36c5e70a
BE
4855 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4856 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4857 }
4858 while (TREE_CODE (in_type) == POINTER_TYPE
4859 && TREE_CODE (in_otype) == POINTER_TYPE);
4860
4861 if (added)
2ee3cb35
MLI
4862 warning_at (loc, OPT_Wcast_qual,
4863 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4864
4865 if (discarded)
4866 /* There are qualifiers present in IN_OTYPE that are not present
4867 in IN_TYPE. */
2ee3cb35 4868 warning_at (loc, OPT_Wcast_qual,
7485aeea 4869 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4870 discarded);
67165eb3
ILT
4871
4872 if (added || discarded)
4873 return;
4874
4875 /* A cast from **T to const **T is unsafe, because it can cause a
4876 const value to be changed with no additional warning. We only
4877 issue this warning if T is the same on both sides, and we only
4878 issue the warning if there are the same number of pointers on
4879 both sides, as otherwise the cast is clearly unsafe anyhow. A
4880 cast is unsafe when a qualifier is added at one level and const
4881 is not present at all outer levels.
4882
4883 To issue this warning, we check at each level whether the cast
4884 adds new qualifiers not already seen. We don't need to special
4885 case function types, as they won't have the same
4886 TYPE_MAIN_VARIANT. */
4887
4888 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4889 return;
4890 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4891 return;
4892
4893 in_type = type;
4894 in_otype = otype;
4895 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4896 do
4897 {
4898 in_type = TREE_TYPE (in_type);
4899 in_otype = TREE_TYPE (in_otype);
4900 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4901 && !is_const)
4902 {
2ee3cb35
MLI
4903 warning_at (loc, OPT_Wcast_qual,
4904 "to be safe all intermediate pointers in cast from "
4905 "%qT to %qT must be %<const%> qualified",
4906 otype, type);
67165eb3
ILT
4907 break;
4908 }
4909 if (is_const)
4910 is_const = TYPE_READONLY (in_type);
4911 }
4912 while (TREE_CODE (in_type) == POINTER_TYPE);
4913}
4914
b8698a0f 4915/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4916 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4917
3e4093b6 4918tree
c2255bc4 4919build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4920{
8ce94e44
JM
4921 tree value;
4922
4923 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4924 expr = TREE_OPERAND (expr, 0);
4925
4926 value = expr;
400fbf9f 4927
3e4093b6
RS
4928 if (type == error_mark_node || expr == error_mark_node)
4929 return error_mark_node;
400fbf9f 4930
3e4093b6
RS
4931 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4932 only in <protocol> qualifications. But when constructing cast expressions,
4933 the protocols do matter and must be kept around. */
700686fa
ZL
4934 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4935 return build1 (NOP_EXPR, type, expr);
4936
4937 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4938
3e4093b6
RS
4939 if (TREE_CODE (type) == ARRAY_TYPE)
4940 {
c2255bc4 4941 error_at (loc, "cast specifies array type");
3e4093b6
RS
4942 return error_mark_node;
4943 }
400fbf9f 4944
3e4093b6
RS
4945 if (TREE_CODE (type) == FUNCTION_TYPE)
4946 {
c2255bc4 4947 error_at (loc, "cast specifies function type");
3e4093b6
RS
4948 return error_mark_node;
4949 }
400fbf9f 4950
808d6eaa
JM
4951 if (!VOID_TYPE_P (type))
4952 {
4953 value = require_complete_type (value);
4954 if (value == error_mark_node)
4955 return error_mark_node;
4956 }
4957
3e4093b6
RS
4958 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4959 {
fcf73884
MLI
4960 if (TREE_CODE (type) == RECORD_TYPE
4961 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4962 pedwarn (loc, OPT_Wpedantic,
fcf73884 4963 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
4964
4965 /* Convert to remove any qualifiers from VALUE's type. */
4966 value = convert (type, value);
3e4093b6
RS
4967 }
4968 else if (TREE_CODE (type) == UNION_TYPE)
4969 {
4970 tree field;
400fbf9f 4971
910ad8de 4972 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4973 if (TREE_TYPE (field) != error_mark_node
4974 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4975 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4976 break;
4977
4978 if (field)
400fbf9f 4979 {
3e4093b6 4980 tree t;
e616f54d 4981 bool maybe_const = true;
3e4093b6 4982
c1771a20 4983 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
4984 t = c_fully_fold (value, false, &maybe_const);
4985 t = build_constructor_single (type, field, t);
4986 if (!maybe_const)
4987 t = c_wrap_maybe_const (t, true);
4988 t = digest_init (loc, type, t,
bbbbb16a 4989 NULL_TREE, false, true, 0);
3e4093b6
RS
4990 TREE_CONSTANT (t) = TREE_CONSTANT (value);
4991 return t;
400fbf9f 4992 }
c2255bc4 4993 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
4994 return error_mark_node;
4995 }
4996 else
4997 {
4998 tree otype, ovalue;
400fbf9f 4999
3e4093b6 5000 if (type == void_type_node)
c2255bc4
AH
5001 {
5002 tree t = build1 (CONVERT_EXPR, type, value);
5003 SET_EXPR_LOCATION (t, loc);
5004 return t;
5005 }
400fbf9f 5006
3e4093b6 5007 otype = TREE_TYPE (value);
400fbf9f 5008
3e4093b6 5009 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5010 if (warn_cast_qual
5011 && TREE_CODE (type) == POINTER_TYPE
5012 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5013 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5014
36c5e70a
BE
5015 /* Warn about conversions between pointers to disjoint
5016 address spaces. */
5017 if (TREE_CODE (type) == POINTER_TYPE
5018 && TREE_CODE (otype) == POINTER_TYPE
5019 && !null_pointer_constant_p (value))
5020 {
5021 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5022 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5023 addr_space_t as_common;
5024
5025 if (!addr_space_superset (as_to, as_from, &as_common))
5026 {
5027 if (ADDR_SPACE_GENERIC_P (as_from))
5028 warning_at (loc, 0, "cast to %s address space pointer "
5029 "from disjoint generic address space pointer",
5030 c_addr_space_name (as_to));
5031
5032 else if (ADDR_SPACE_GENERIC_P (as_to))
5033 warning_at (loc, 0, "cast to generic address space pointer "
5034 "from disjoint %s address space pointer",
5035 c_addr_space_name (as_from));
5036
5037 else
5038 warning_at (loc, 0, "cast to %s address space pointer "
5039 "from disjoint %s address space pointer",
5040 c_addr_space_name (as_to),
5041 c_addr_space_name (as_from));
5042 }
5043 }
5044
3e4093b6 5045 /* Warn about possible alignment problems. */
3176a0c2 5046 if (STRICT_ALIGNMENT
3e4093b6
RS
5047 && TREE_CODE (type) == POINTER_TYPE
5048 && TREE_CODE (otype) == POINTER_TYPE
5049 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5050 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5051 /* Don't warn about opaque types, where the actual alignment
5052 restriction is unknown. */
5053 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5054 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5055 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5056 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5057 warning_at (loc, OPT_Wcast_align,
5058 "cast increases required alignment of target type");
e9a25f70 5059
3176a0c2 5060 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5061 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5062 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5063 /* Unlike conversion of integers to pointers, where the
5064 warning is disabled for converting constants because
5065 of cases such as SIG_*, warn about converting constant
5066 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5067 sign extension, and a warning is appropriate. */
c2255bc4
AH
5068 warning_at (loc, OPT_Wpointer_to_int_cast,
5069 "cast from pointer to integer of different size");
400fbf9f 5070
3176a0c2 5071 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5072 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5073 warning_at (loc, OPT_Wbad_function_cast,
5074 "cast from function call of type %qT "
5075 "to non-matching type %qT", otype, type);
400fbf9f 5076
3176a0c2 5077 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5078 && TREE_CODE (otype) == INTEGER_TYPE
5079 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5080 /* Don't warn about converting any constant. */
5081 && !TREE_CONSTANT (value))
c2255bc4
AH
5082 warning_at (loc,
5083 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5084 "of different size");
400fbf9f 5085
79bedddc
SR
5086 if (warn_strict_aliasing <= 2)
5087 strict_aliasing_warning (otype, type, expr);
400fbf9f 5088
3897f229
JM
5089 /* If pedantic, warn for conversions between function and object
5090 pointer types, except for converting a null pointer constant
5091 to function pointer type. */
5092 if (pedantic
5093 && TREE_CODE (type) == POINTER_TYPE
5094 && TREE_CODE (otype) == POINTER_TYPE
5095 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5096 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5097 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5098 "conversion of function pointer to object pointer type");
3897f229
JM
5099
5100 if (pedantic
5101 && TREE_CODE (type) == POINTER_TYPE
5102 && TREE_CODE (otype) == POINTER_TYPE
5103 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5104 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5105 && !null_pointer_constant_p (value))
c1771a20 5106 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5107 "conversion of object pointer to function pointer type");
3897f229 5108
3e4093b6 5109 ovalue = value;
3e4093b6 5110 value = convert (type, value);
400fbf9f 5111
3e4093b6 5112 /* Ignore any integer overflow caused by the cast. */
928c19bb 5113 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5114 {
8bcd6380 5115 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5116 {
8bcd6380
RS
5117 if (!TREE_OVERFLOW (value))
5118 {
5119 /* Avoid clobbering a shared constant. */
5120 value = copy_node (value);
5121 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5122 }
6414bad6 5123 }
8bcd6380 5124 else if (TREE_OVERFLOW (value))
d8e1f97b 5125 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5126 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5127 }
5128 }
400fbf9f 5129
53cd18ec
JM
5130 /* Don't let a cast be an lvalue. */
5131 if (value == expr)
db3927fb 5132 value = non_lvalue_loc (loc, value);
e9a25f70 5133
928c19bb
JM
5134 /* Don't allow the results of casting to floating-point or complex
5135 types be confused with actual constants, or casts involving
5136 integer and pointer types other than direct integer-to-integer
5137 and integer-to-pointer be confused with integer constant
5138 expressions and null pointer constants. */
5139 if (TREE_CODE (value) == REAL_CST
5140 || TREE_CODE (value) == COMPLEX_CST
5141 || (TREE_CODE (value) == INTEGER_CST
5142 && !((TREE_CODE (expr) == INTEGER_CST
5143 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5144 || TREE_CODE (expr) == REAL_CST
5145 || TREE_CODE (expr) == COMPLEX_CST)))
5146 value = build1 (NOP_EXPR, type, value);
5147
c2255bc4
AH
5148 if (CAN_HAVE_LOCATION_P (value))
5149 SET_EXPR_LOCATION (value, loc);
3e4093b6 5150 return value;
400fbf9f
JW
5151}
5152
c2255bc4
AH
5153/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5154 location of the open paren of the cast, or the position of the cast
5155 expr. */
3e4093b6 5156tree
c2255bc4 5157c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5158{
f8893e47 5159 tree type;
928c19bb
JM
5160 tree type_expr = NULL_TREE;
5161 bool type_expr_const = true;
5162 tree ret;
3e4093b6 5163 int saved_wsp = warn_strict_prototypes;
c5c76735 5164
3e4093b6
RS
5165 /* This avoids warnings about unprototyped casts on
5166 integers. E.g. "#define SIG_DFL (void(*)())0". */
5167 if (TREE_CODE (expr) == INTEGER_CST)
5168 warn_strict_prototypes = 0;
928c19bb 5169 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5170 warn_strict_prototypes = saved_wsp;
c5c76735 5171
c2255bc4 5172 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5173 if (type_expr)
5174 {
9f33203d
JM
5175 bool inner_expr_const = true;
5176 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5177 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5178 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5179 && inner_expr_const);
c2255bc4 5180 SET_EXPR_LOCATION (ret, loc);
928c19bb 5181 }
24b97832
ILT
5182
5183 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5184 SET_EXPR_LOCATION (ret, loc);
5185
9e5b2115
PB
5186 /* C++ does not permits types to be defined in a cast, but it
5187 allows references to incomplete types. */
5188 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5189 warning_at (loc, OPT_Wc___compat,
5190 "defining a type in a cast is invalid in C++");
5191
928c19bb 5192 return ret;
400fbf9f 5193}
3e4093b6
RS
5194\f
5195/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5196 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5197 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5198 MODIFYCODE is the code for a binary operator that we use
5199 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5200 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5201 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5202 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5203
c2255bc4
AH
5204 LOCATION is the location of the MODIFYCODE operator.
5205 RHS_LOC is the location of the RHS. */
2f6e4e97 5206
3e4093b6 5207tree
32e8bb8e 5208build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5209 enum tree_code modifycode,
c2255bc4 5210 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5211{
3e4093b6
RS
5212 tree result;
5213 tree newrhs;
241f845a 5214 tree rhseval = NULL_TREE;
8ce94e44 5215 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5216 tree lhstype = TREE_TYPE (lhs);
5217 tree olhstype = lhstype;
928c19bb 5218 bool npc;
267bac10 5219 bool is_atomic_op;
e9a25f70 5220
3e4093b6
RS
5221 /* Types that aren't fully specified cannot be used in assignments. */
5222 lhs = require_complete_type (lhs);
e9a25f70 5223
3e4093b6
RS
5224 /* Avoid duplicate error messages from operands that had errors. */
5225 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5226 return error_mark_node;
400fbf9f 5227
4c2ecab0
JM
5228 /* Ensure an error for assigning a non-lvalue array to an array in
5229 C90. */
5230 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5231 {
5232 error_at (location, "assignment to expression with array type");
5233 return error_mark_node;
5234 }
5235
46a88c12 5236 /* For ObjC properties, defer this check. */
7bd11157 5237 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5238 return error_mark_node;
5239
267bac10
JM
5240 is_atomic_op = really_atomic_lvalue (lhs);
5241
8ce94e44
JM
5242 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5243 {
5244 rhs_semantic_type = TREE_TYPE (rhs);
5245 rhs = TREE_OPERAND (rhs, 0);
5246 }
5247
3e4093b6 5248 newrhs = rhs;
400fbf9f 5249
928c19bb
JM
5250 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5251 {
5252 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5253 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5254 rhs_origtype);
928c19bb
JM
5255 if (inner == error_mark_node)
5256 return error_mark_node;
5257 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5258 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5259 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5260 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5261 protected_set_expr_location (result, location);
5262 return result;
5263 }
5264
3e4093b6
RS
5265 /* If a binary op has been requested, combine the old LHS value with the RHS
5266 producing the value we should actually store into the LHS. */
5267
5268 if (modifycode != NOP_EXPR)
400fbf9f 5269 {
928c19bb 5270 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5271 lhs = stabilize_reference (lhs);
bbbbb16a 5272
267bac10
JM
5273 /* Construct the RHS for any non-atomic compound assignemnt. */
5274 if (!is_atomic_op)
5275 {
241f845a
JJ
5276 /* If in LHS op= RHS the RHS has side-effects, ensure they
5277 are preevaluated before the rest of the assignment expression's
5278 side-effects, because RHS could contain e.g. function calls
5279 that modify LHS. */
5280 if (TREE_SIDE_EFFECTS (rhs))
5281 {
5282 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5283 rhseval = newrhs;
5284 }
267bac10 5285 newrhs = build_binary_op (location,
241f845a 5286 modifycode, lhs, newrhs, 1);
267bac10
JM
5287
5288 /* The original type of the right hand side is no longer
5289 meaningful. */
5290 rhs_origtype = NULL_TREE;
5291 }
400fbf9f 5292 }
400fbf9f 5293
668ea4b1
IS
5294 if (c_dialect_objc ())
5295 {
46a88c12
NP
5296 /* Check if we are modifying an Objective-C property reference;
5297 if so, we need to generate setter calls. */
5298 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5299 if (result)
241f845a 5300 goto return_result;
46a88c12
NP
5301
5302 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5303 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5304 return error_mark_node;
5305 }
5306
9bf24266 5307 /* Give an error for storing in something that is 'const'. */
bbbd6700 5308
f37acdf9 5309 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5310 || ((TREE_CODE (lhstype) == RECORD_TYPE
5311 || TREE_CODE (lhstype) == UNION_TYPE)
5312 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5313 {
c02065fc 5314 readonly_error (location, lhs, lv_assign);
953ff289
DN
5315 return error_mark_node;
5316 }
f37acdf9
JM
5317 else if (TREE_READONLY (lhs))
5318 readonly_warning (lhs, lv_assign);
bbbd6700 5319
3e4093b6
RS
5320 /* If storing into a structure or union member,
5321 it has probably been given type `int'.
5322 Compute the type that would go with
5323 the actual amount of storage the member occupies. */
bbbd6700 5324
3e4093b6
RS
5325 if (TREE_CODE (lhs) == COMPONENT_REF
5326 && (TREE_CODE (lhstype) == INTEGER_TYPE
5327 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5328 || TREE_CODE (lhstype) == REAL_TYPE
5329 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5330 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5331
3e4093b6
RS
5332 /* If storing in a field that is in actuality a short or narrower than one,
5333 we must store in the field in its actual type. */
5334
5335 if (lhstype != TREE_TYPE (lhs))
5336 {
5337 lhs = copy_node (lhs);
5338 TREE_TYPE (lhs) = lhstype;
400fbf9f 5339 }
400fbf9f 5340
32e8bb8e
ILT
5341 /* Issue -Wc++-compat warnings about an assignment to an enum type
5342 when LHS does not have its original type. This happens for,
5343 e.g., an enum bitfield in a struct. */
5344 if (warn_cxx_compat
5345 && lhs_origtype != NULL_TREE
5346 && lhs_origtype != lhstype
5347 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5348 {
5349 tree checktype = (rhs_origtype != NULL_TREE
5350 ? rhs_origtype
5351 : TREE_TYPE (rhs));
5352 if (checktype != error_mark_node
267bac10
JM
5353 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5354 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5355 warning_at (location, OPT_Wc___compat,
5356 "enum conversion in assignment is invalid in C++");
5357 }
5358
267bac10
JM
5359 /* If the lhs is atomic, remove that qualifier. */
5360 if (is_atomic_op)
5361 {
5362 lhstype = build_qualified_type (lhstype,
5363 (TYPE_QUALS (lhstype)
5364 & ~TYPE_QUAL_ATOMIC));
5365 olhstype = build_qualified_type (olhstype,
5366 (TYPE_QUALS (lhstype)
5367 & ~TYPE_QUAL_ATOMIC));
5368 }
5369
8ce94e44
JM
5370 /* Convert new value to destination type. Fold it first, then
5371 restore any excess precision information, for the sake of
5372 conversion warnings. */
400fbf9f 5373
267bac10
JM
5374 if (!(is_atomic_op && modifycode != NOP_EXPR))
5375 {
5376 npc = null_pointer_constant_p (newrhs);
5377 newrhs = c_fully_fold (newrhs, false, NULL);
5378 if (rhs_semantic_type)
5379 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5380 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5381 rhs_origtype, ic_assign, npc,
5382 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5383 if (TREE_CODE (newrhs) == ERROR_MARK)
5384 return error_mark_node;
5385 }
400fbf9f 5386
6e955430
ZL
5387 /* Emit ObjC write barrier, if necessary. */
5388 if (c_dialect_objc () && flag_objc_gc)
5389 {
5390 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5391 if (result)
c9f9eb5d
AH
5392 {
5393 protected_set_expr_location (result, location);
241f845a 5394 goto return_result;
c9f9eb5d 5395 }
6e955430
ZL
5396 }
5397
ea4b7848 5398 /* Scan operands. */
400fbf9f 5399
267bac10
JM
5400 if (is_atomic_op)
5401 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5402 else
5403 {
5404 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5405 TREE_SIDE_EFFECTS (result) = 1;
5406 protected_set_expr_location (result, location);
5407 }
400fbf9f 5408
3e4093b6
RS
5409 /* If we got the LHS in a different type for storing in,
5410 convert the result back to the nominal type of LHS
5411 so that the value we return always has the same type
5412 as the LHS argument. */
e855c5ce 5413
3e4093b6 5414 if (olhstype == TREE_TYPE (result))
241f845a 5415 goto return_result;
c9f9eb5d 5416
68fca595
MP
5417 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5418 rhs_origtype, ic_assign, false, NULL_TREE,
5419 NULL_TREE, 0);
c9f9eb5d 5420 protected_set_expr_location (result, location);
241f845a
JJ
5421
5422return_result:
5423 if (rhseval)
5424 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5425 return result;
3e4093b6
RS
5426}
5427\f
478a1c5b
ILT
5428/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5429 This is used to implement -fplan9-extensions. */
5430
5431static bool
5432find_anonymous_field_with_type (tree struct_type, tree type)
5433{
5434 tree field;
5435 bool found;
5436
5437 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5438 || TREE_CODE (struct_type) == UNION_TYPE);
5439 found = false;
5440 for (field = TYPE_FIELDS (struct_type);
5441 field != NULL_TREE;
5442 field = TREE_CHAIN (field))
5443 {
267bac10
JM
5444 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5445 ? c_build_qualified_type (TREE_TYPE (field),
5446 TYPE_QUAL_ATOMIC)
5447 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5448 if (DECL_NAME (field) == NULL
267bac10 5449 && comptypes (type, fieldtype))
478a1c5b
ILT
5450 {
5451 if (found)
5452 return false;
5453 found = true;
5454 }
5455 else if (DECL_NAME (field) == NULL
5456 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5457 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5458 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5459 {
5460 if (found)
5461 return false;
5462 found = true;
5463 }
5464 }
5465 return found;
5466}
5467
5468/* RHS is an expression whose type is pointer to struct. If there is
5469 an anonymous field in RHS with type TYPE, then return a pointer to
5470 that field in RHS. This is used with -fplan9-extensions. This
5471 returns NULL if no conversion could be found. */
5472
5473static tree
5474convert_to_anonymous_field (location_t location, tree type, tree rhs)
5475{
5476 tree rhs_struct_type, lhs_main_type;
5477 tree field, found_field;
5478 bool found_sub_field;
5479 tree ret;
5480
5481 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5482 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5483 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5484 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5485
5486 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5487 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5488 ? c_build_qualified_type (TREE_TYPE (type),
5489 TYPE_QUAL_ATOMIC)
5490 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5491
5492 found_field = NULL_TREE;
5493 found_sub_field = false;
5494 for (field = TYPE_FIELDS (rhs_struct_type);
5495 field != NULL_TREE;
5496 field = TREE_CHAIN (field))
5497 {
5498 if (DECL_NAME (field) != NULL_TREE
5499 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5500 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5501 continue;
267bac10
JM
5502 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5503 ? c_build_qualified_type (TREE_TYPE (field),
5504 TYPE_QUAL_ATOMIC)
5505 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5506 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5507 {
5508 if (found_field != NULL_TREE)
5509 return NULL_TREE;
5510 found_field = field;
5511 }
5512 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5513 lhs_main_type))
5514 {
5515 if (found_field != NULL_TREE)
5516 return NULL_TREE;
5517 found_field = field;
5518 found_sub_field = true;
5519 }
5520 }
5521
5522 if (found_field == NULL_TREE)
5523 return NULL_TREE;
5524
5525 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5526 build_fold_indirect_ref (rhs), found_field,
5527 NULL_TREE);
5528 ret = build_fold_addr_expr_loc (location, ret);
5529
5530 if (found_sub_field)
5531 {
5532 ret = convert_to_anonymous_field (location, type, ret);
5533 gcc_assert (ret != NULL_TREE);
5534 }
5535
5536 return ret;
5537}
5538
63bc4e87
MP
5539/* Issue an error message for a bad initializer component.
5540 GMSGID identifies the message.
5541 The component name is taken from the spelling stack. */
5542
5543static void
ea58ef42 5544error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5545{
5546 char *ofwhat;
5547
5548 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5549 error_at (loc, gmsgid);
63bc4e87
MP
5550 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5551 if (*ofwhat)
d7ff7ae5 5552 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5553}
5554
5555/* Issue a pedantic warning for a bad initializer component. OPT is
5556 the option OPT_* (from options.h) controlling this warning or 0 if
5557 it is unconditionally given. GMSGID identifies the message. The
5558 component name is taken from the spelling stack. */
5559
5560static void
5561pedwarn_init (location_t location, int opt, const char *gmsgid)
5562{
5563 char *ofwhat;
d7ff7ae5 5564 bool warned;
63bc4e87
MP
5565
5566 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5567 warned = pedwarn (location, opt, gmsgid);
63bc4e87 5568 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5569 if (*ofwhat && warned)
5570 inform (location, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5571}
5572
5573/* Issue a warning for a bad initializer component.
5574
5575 OPT is the OPT_W* value corresponding to the warning option that
5576 controls this warning. GMSGID identifies the message. The
5577 component name is taken from the spelling stack. */
5578
5579static void
5580warning_init (location_t loc, int opt, const char *gmsgid)
5581{
5582 char *ofwhat;
d7ff7ae5 5583 bool warned;
63bc4e87
MP
5584
5585 /* The gmsgid may be a format string with %< and %>. */
d7ff7ae5 5586 warned = warning_at (loc, opt, gmsgid);
63bc4e87 5587 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5
MP
5588 if (*ofwhat && warned)
5589 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5590}
5591\f
5592/* If TYPE is an array type and EXPR is a parenthesized string
5593 constant, warn if pedantic that EXPR is being used to initialize an
5594 object of type TYPE. */
5595
5596void
d033409e 5597maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5598{
5599 if (pedantic
5600 && TREE_CODE (type) == ARRAY_TYPE
5601 && TREE_CODE (expr.value) == STRING_CST
5602 && expr.original_code != STRING_CST)
d033409e 5603 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5604 "array initialized from parenthesized string constant");
5605}
5606
bbbbb16a
ILT
5607/* Convert value RHS to type TYPE as preparation for an assignment to
5608 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5609 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5610 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5611 constant before any folding.
3e4093b6
RS
5612 The real work of conversion is done by `convert'.
5613 The purpose of this function is to generate error messages
5614 for assignments that are not allowed in C.
2ac2f164
JM
5615 ERRTYPE says whether it is argument passing, assignment,
5616 initialization or return.
2f6e4e97 5617
81e5eca8
MP
5618 LOCATION is the location of the assignment, EXPR_LOC is the location of
5619 the RHS or, for a function, location of an argument.
2ac2f164 5620 FUNCTION is a tree for the function being called.
3e4093b6 5621 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5622
3e4093b6 5623static tree
81e5eca8 5624convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5625 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5626 bool null_pointer_constant, tree fundecl,
5627 tree function, int parmnum)
3e4093b6
RS
5628{
5629 enum tree_code codel = TREE_CODE (type);
8ce94e44 5630 tree orig_rhs = rhs;
3e4093b6
RS
5631 tree rhstype;
5632 enum tree_code coder;
2ac2f164 5633 tree rname = NULL_TREE;
58393038 5634 bool objc_ok = false;
2ac2f164 5635
6b4ef5c1 5636 if (errtype == ic_argpass)
2ac2f164
JM
5637 {
5638 tree selector;
5639 /* Change pointer to function to the function itself for
5640 diagnostics. */
5641 if (TREE_CODE (function) == ADDR_EXPR
5642 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5643 function = TREE_OPERAND (function, 0);
5644
5645 /* Handle an ObjC selector specially for diagnostics. */
5646 selector = objc_message_selector ();
5647 rname = function;
5648 if (selector && parmnum > 2)
5649 {
5650 rname = selector;
5651 parmnum -= 2;
5652 }
5653 }
5654
5655 /* This macro is used to emit diagnostics to ensure that all format
5656 strings are complete sentences, visible to gettext and checked at
5657 compile time. */
5c1bc275 5658#define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5659 do { \
5660 switch (errtype) \
5661 { \
5662 case ic_argpass: \
5c1bc275 5663 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5664 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5665 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5666 "expected %qT but argument is of type %qT", \
5667 type, rhstype); \
5668 break; \
1e053dfe
MLI
5669 case ic_assign: \
5670 pedwarn (LOCATION, OPT, AS); \
5671 break; \
5672 case ic_init: \
6a8f4e12 5673 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5674 break; \
5675 case ic_return: \
d033409e 5676 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5677 break; \
5678 default: \
5679 gcc_unreachable (); \
5680 } \
2ac2f164 5681 } while (0)
cb3ca04e 5682
49706e39
MLI
5683 /* This macro is used to emit diagnostics to ensure that all format
5684 strings are complete sentences, visible to gettext and checked at
5685 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5686 extra parameter to enumerate qualifiers. */
5687
5c1bc275 5688#define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5689 do { \
5690 switch (errtype) \
5691 { \
5692 case ic_argpass: \
5c1bc275 5693 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5694 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5695 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5696 "expected %qT but argument is of type %qT", \
5697 type, rhstype); \
5698 break; \
5699 case ic_assign: \
5c1bc275 5700 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5701 break; \
5702 case ic_init: \
5c1bc275 5703 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5704 break; \
5705 case ic_return: \
5c1bc275 5706 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5707 break; \
5708 default: \
5709 gcc_unreachable (); \
5710 } \
5711 } while (0)
5712
8ce94e44
JM
5713 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5714 rhs = TREE_OPERAND (rhs, 0);
5715
3e4093b6
RS
5716 rhstype = TREE_TYPE (rhs);
5717 coder = TREE_CODE (rhstype);
5718
5719 if (coder == ERROR_MARK)
5720 return error_mark_node;
5721
58393038
ZL
5722 if (c_dialect_objc ())
5723 {
5724 int parmno;
5725
5726 switch (errtype)
5727 {
5728 case ic_return:
5729 parmno = 0;
5730 break;
5731
5732 case ic_assign:
5733 parmno = -1;
5734 break;
5735
5736 case ic_init:
5737 parmno = -2;
5738 break;
5739
5740 default:
5741 parmno = parmnum;
5742 break;
5743 }
5744
5745 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5746 }
5747
bbbbb16a
ILT
5748 if (warn_cxx_compat)
5749 {
5750 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5751 if (checktype != error_mark_node
5752 && TREE_CODE (type) == ENUMERAL_TYPE
5753 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5754 {
5c1bc275 5755 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
81f40b79
ILT
5756 G_("enum conversion when passing argument "
5757 "%d of %qE is invalid in C++"),
5758 G_("enum conversion in assignment is "
5759 "invalid in C++"),
5760 G_("enum conversion in initialization is "
5761 "invalid in C++"),
5762 G_("enum conversion in return is "
5763 "invalid in C++"));
bbbbb16a
ILT
5764 }
5765 }
5766
3e4093b6 5767 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5768 return rhs;
3e4093b6
RS
5769
5770 if (coder == VOID_TYPE)
400fbf9f 5771 {
6dcc04b0
JM
5772 /* Except for passing an argument to an unprototyped function,
5773 this is a constraint violation. When passing an argument to
5774 an unprototyped function, it is compile-time undefined;
5775 making it a constraint in that case was rejected in
5776 DR#252. */
c2255bc4 5777 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5778 return error_mark_node;
400fbf9f 5779 }
808d6eaa
JM
5780 rhs = require_complete_type (rhs);
5781 if (rhs == error_mark_node)
5782 return error_mark_node;
cd192ccc
MS
5783 /* A non-reference type can convert to a reference. This handles
5784 va_start, va_copy and possibly port built-ins. */
5785 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5786 {
3e4093b6 5787 if (!lvalue_p (rhs))
400fbf9f 5788 {
c2255bc4 5789 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5790 return error_mark_node;
400fbf9f 5791 }
3e4093b6
RS
5792 if (!c_mark_addressable (rhs))
5793 return error_mark_node;
5794 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5795 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5796
81e5eca8 5797 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5798 build_pointer_type (TREE_TYPE (type)),
5799 rhs, origtype, errtype,
5800 null_pointer_constant, fundecl, function,
5801 parmnum);
cd192ccc
MS
5802 if (rhs == error_mark_node)
5803 return error_mark_node;
3e4093b6
RS
5804
5805 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5806 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5807 return rhs;
400fbf9f 5808 }
3e4093b6 5809 /* Some types can interconvert without explicit casts. */
3274deff 5810 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5811 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5812 return convert (type, rhs);
5813 /* Arithmetic types all interconvert, and enum is treated like int. */
5814 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5815 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5816 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5817 || codel == BOOLEAN_TYPE)
5818 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5819 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5820 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5821 || coder == BOOLEAN_TYPE))
928c19bb
JM
5822 {
5823 tree ret;
5824 bool save = in_late_binary_op;
dfe776dd 5825 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5826 in_late_binary_op = true;
81e5eca8
MP
5827 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5828 ? expr_loc : location, type, orig_rhs);
dfe776dd 5829 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5830 in_late_binary_op = save;
5831 return ret;
5832 }
400fbf9f 5833
79077aea
JJ
5834 /* Aggregates in different TUs might need conversion. */
5835 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5836 && codel == coder
5837 && comptypes (type, rhstype))
81e5eca8
MP
5838 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5839 ? expr_loc : location, type, rhs);
79077aea 5840
ebf0bf7f 5841 /* Conversion to a transparent union or record from its member types.
3e4093b6 5842 This applies only to function arguments. */
ebf0bf7f
JJ
5843 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5844 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5845 && errtype == ic_argpass)
400fbf9f 5846 {
0257e383 5847 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5848
910ad8de 5849 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5850 {
0257e383 5851 tree memb_type = TREE_TYPE (memb);
400fbf9f 5852
3e4093b6 5853 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5854 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5855 break;
e58cd767 5856
3e4093b6
RS
5857 if (TREE_CODE (memb_type) != POINTER_TYPE)
5858 continue;
2f6e4e97 5859
3e4093b6
RS
5860 if (coder == POINTER_TYPE)
5861 {
5862 tree ttl = TREE_TYPE (memb_type);
5863 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5864
3e4093b6
RS
5865 /* Any non-function converts to a [const][volatile] void *
5866 and vice versa; otherwise, targets must be the same.
5867 Meanwhile, the lhs target must have all the qualifiers of
5868 the rhs. */
267bac10
JM
5869 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5870 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5871 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5872 {
267bac10
JM
5873 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5874 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5875 /* If this type won't generate any warnings, use it. */
267bac10 5876 if (lquals == rquals
3e4093b6
RS
5877 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5878 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5879 ? ((lquals | rquals) == rquals)
5880 : ((lquals | rquals) == lquals)))
3e4093b6 5881 break;
400fbf9f 5882
3e4093b6 5883 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5884 if (!marginal_memb)
5885 marginal_memb = memb;
3e4093b6
RS
5886 }
5887 }
82bde854 5888
3e4093b6 5889 /* Can convert integer zero to any pointer type. */
928c19bb 5890 if (null_pointer_constant)
3e4093b6
RS
5891 {
5892 rhs = null_pointer_node;
5893 break;
5894 }
5895 }
400fbf9f 5896
0257e383 5897 if (memb || marginal_memb)
3e4093b6 5898 {
0257e383 5899 if (!memb)
3e4093b6
RS
5900 {
5901 /* We have only a marginally acceptable member type;
5902 it needs a warning. */
0257e383 5903 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5904 tree ttr = TREE_TYPE (rhstype);
714a0864 5905
3e4093b6
RS
5906 /* Const and volatile mean something different for function
5907 types, so the usual warnings are not appropriate. */
5908 if (TREE_CODE (ttr) == FUNCTION_TYPE
5909 && TREE_CODE (ttl) == FUNCTION_TYPE)
5910 {
5911 /* Because const and volatile on functions are
5912 restrictions that say the function will not do
5913 certain things, it is okay to use a const or volatile
5914 function where an ordinary one is wanted, but not
5915 vice-versa. */
36c5e70a
BE
5916 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5917 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5c1bc275
MP
5918 WARN_FOR_QUALIFIERS (location, expr_loc,
5919 OPT_Wdiscarded_qualifiers,
509c9d60 5920 G_("passing argument %d of %qE "
49706e39 5921 "makes %q#v qualified function "
2ac2f164 5922 "pointer from unqualified"),
49706e39 5923 G_("assignment makes %q#v qualified "
2ac2f164
JM
5924 "function pointer from "
5925 "unqualified"),
49706e39 5926 G_("initialization makes %q#v qualified "
2ac2f164
JM
5927 "function pointer from "
5928 "unqualified"),
49706e39
MLI
5929 G_("return makes %q#v qualified function "
5930 "pointer from unqualified"),
5931 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5932 }
36c5e70a
BE
5933 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5934 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5c1bc275
MP
5935 WARN_FOR_QUALIFIERS (location, expr_loc,
5936 OPT_Wdiscarded_qualifiers,
509c9d60 5937 G_("passing argument %d of %qE discards "
49706e39
MLI
5938 "%qv qualifier from pointer target type"),
5939 G_("assignment discards %qv qualifier "
2ac2f164 5940 "from pointer target type"),
49706e39 5941 G_("initialization discards %qv qualifier "
2ac2f164 5942 "from pointer target type"),
49706e39
MLI
5943 G_("return discards %qv qualifier from "
5944 "pointer target type"),
5945 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5946
5947 memb = marginal_memb;
3e4093b6 5948 }
400fbf9f 5949
fcf73884 5950 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5951 pedwarn (location, OPT_Wpedantic,
fcf73884 5952 "ISO C prohibits argument conversion to union type");
0e7c47fa 5953
db3927fb 5954 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5955 return build_constructor_single (type, memb, rhs);
3e4093b6 5956 }
0e7c47fa
RK
5957 }
5958
3e4093b6
RS
5959 /* Conversions among pointers */
5960 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5961 && (coder == codel))
400fbf9f 5962 {
3e4093b6
RS
5963 tree ttl = TREE_TYPE (type);
5964 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5965 tree mvl = ttl;
5966 tree mvr = ttr;
3e4093b6 5967 bool is_opaque_pointer;
264fa2db 5968 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5969 addr_space_t asl;
5970 addr_space_t asr;
400fbf9f 5971
46df2823 5972 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5973 mvl = (TYPE_ATOMIC (mvl)
5974 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5975 TYPE_QUAL_ATOMIC)
5976 : TYPE_MAIN_VARIANT (mvl));
46df2823 5977 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5978 mvr = (TYPE_ATOMIC (mvr)
5979 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5980 TYPE_QUAL_ATOMIC)
5981 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5982 /* Opaque pointers are treated like void pointers. */
f83c7f63 5983 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 5984
478a1c5b
ILT
5985 /* The Plan 9 compiler permits a pointer to a struct to be
5986 automatically converted into a pointer to an anonymous field
5987 within the struct. */
5988 if (flag_plan9_extensions
5989 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
5990 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
5991 && mvl != mvr)
5992 {
5993 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
5994 if (new_rhs != NULL_TREE)
5995 {
5996 rhs = new_rhs;
5997 rhstype = TREE_TYPE (rhs);
5998 coder = TREE_CODE (rhstype);
5999 ttr = TREE_TYPE (rhstype);
6000 mvr = TYPE_MAIN_VARIANT (ttr);
6001 }
6002 }
6003
b7e20b53 6004 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6005 for the purpose of reducing the number of false positives, we
6006 tolerate the special case of
b7e20b53 6007
c22cacf3 6008 int *p = NULL;
b7e20b53 6009
c22cacf3 6010 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6011 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6012 warning_at (errtype == ic_argpass ? expr_loc : location,
6013 OPT_Wc___compat,
6014 "request for implicit conversion "
c2255bc4 6015 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6016
36c5e70a
BE
6017 /* See if the pointers point to incompatible address spaces. */
6018 asl = TYPE_ADDR_SPACE (ttl);
6019 asr = TYPE_ADDR_SPACE (ttr);
6020 if (!null_pointer_constant_p (rhs)
6021 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6022 {
6023 switch (errtype)
6024 {
6025 case ic_argpass:
8ffcdea8 6026 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6027 "non-enclosed address space", parmnum, rname);
6028 break;
6029 case ic_assign:
6030 error_at (location, "assignment from pointer to "
6031 "non-enclosed address space");
6032 break;
6033 case ic_init:
6034 error_at (location, "initialization from pointer to "
6035 "non-enclosed address space");
6036 break;
6037 case ic_return:
6038 error_at (location, "return from pointer to "
6039 "non-enclosed address space");
6040 break;
6041 default:
6042 gcc_unreachable ();
6043 }
6044 return error_mark_node;
6045 }
6046
7876a414
KG
6047 /* Check if the right-hand side has a format attribute but the
6048 left-hand side doesn't. */
90137d8f 6049 if (warn_suggest_attribute_format
104f8784 6050 && check_missing_format_attribute (type, rhstype))
c22cacf3 6051 {
104f8784
KG
6052 switch (errtype)
6053 {
6054 case ic_argpass:
8ffcdea8 6055 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6056 "argument %d of %qE might be "
6057 "a candidate for a format attribute",
6058 parmnum, rname);
104f8784
KG
6059 break;
6060 case ic_assign:
90137d8f 6061 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6062 "assignment left-hand side might be "
6063 "a candidate for a format attribute");
104f8784
KG
6064 break;
6065 case ic_init:
90137d8f 6066 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6067 "initialization left-hand side might be "
6068 "a candidate for a format attribute");
104f8784
KG
6069 break;
6070 case ic_return:
90137d8f 6071 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6072 "return type might be "
6073 "a candidate for a format attribute");
104f8784
KG
6074 break;
6075 default:
6076 gcc_unreachable ();
6077 }
7876a414 6078 }
c22cacf3 6079
3e4093b6
RS
6080 /* Any non-function converts to a [const][volatile] void *
6081 and vice versa; otherwise, targets must be the same.
6082 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6083 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6084 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6085 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6086 || is_opaque_pointer
f8a93a2e
JJ
6087 || ((c_common_unsigned_type (mvl)
6088 == c_common_unsigned_type (mvr))
267bac10
JM
6089 && (c_common_signed_type (mvl)
6090 == c_common_signed_type (mvr))
6091 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
6092 {
6093 if (pedantic
6094 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6095 ||
6096 (VOID_TYPE_P (ttr)
928c19bb 6097 && !null_pointer_constant
3e4093b6 6098 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5c1bc275 6099 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
509c9d60 6100 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
6101 "%qE between function pointer "
6102 "and %<void *%>"),
4b794eaf 6103 G_("ISO C forbids assignment between "
2ac2f164 6104 "function pointer and %<void *%>"),
4b794eaf 6105 G_("ISO C forbids initialization between "
2ac2f164 6106 "function pointer and %<void *%>"),
4b794eaf 6107 G_("ISO C forbids return between function "
2ac2f164 6108 "pointer and %<void *%>"));
3e4093b6
RS
6109 /* Const and volatile mean something different for function types,
6110 so the usual warnings are not appropriate. */
6111 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6112 && TREE_CODE (ttl) != FUNCTION_TYPE)
6113 {
267bac10
JM
6114 /* Assignments between atomic and non-atomic objects are OK. */
6115 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6116 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6117 {
5c1bc275
MP
6118 WARN_FOR_QUALIFIERS (location, expr_loc,
6119 OPT_Wdiscarded_qualifiers,
6c39e757
NP
6120 G_("passing argument %d of %qE discards "
6121 "%qv qualifier from pointer target type"),
6122 G_("assignment discards %qv qualifier "
6123 "from pointer target type"),
6124 G_("initialization discards %qv qualifier "
6125 "from pointer target type"),
6126 G_("return discards %qv qualifier from "
6127 "pointer target type"),
6128 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6129 }
3e4093b6
RS
6130 /* If this is not a case of ignoring a mismatch in signedness,
6131 no warning. */
6132 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6133 || target_cmp)
3e4093b6
RS
6134 ;
6135 /* If there is a mismatch, do warn. */
f2fd3821 6136 else if (warn_pointer_sign)
5c1bc275 6137 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
509c9d60 6138 G_("pointer targets in passing argument "
2ac2f164 6139 "%d of %qE differ in signedness"),
4b794eaf 6140 G_("pointer targets in assignment "
2ac2f164 6141 "differ in signedness"),
4b794eaf 6142 G_("pointer targets in initialization "
2ac2f164 6143 "differ in signedness"),
4b794eaf 6144 G_("pointer targets in return differ "
2ac2f164 6145 "in signedness"));
3e4093b6
RS
6146 }
6147 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6148 && TREE_CODE (ttr) == FUNCTION_TYPE)
6149 {
6150 /* Because const and volatile on functions are restrictions
6151 that say the function will not do certain things,
6152 it is okay to use a const or volatile function
6153 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6154 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6155 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5c1bc275
MP
6156 WARN_FOR_QUALIFIERS (location, expr_loc,
6157 OPT_Wdiscarded_qualifiers,
509c9d60 6158 G_("passing argument %d of %qE makes "
49706e39 6159 "%q#v qualified function pointer "
2ac2f164 6160 "from unqualified"),
49706e39 6161 G_("assignment makes %q#v qualified function "
2ac2f164 6162 "pointer from unqualified"),
49706e39 6163 G_("initialization makes %q#v qualified "
2ac2f164 6164 "function pointer from unqualified"),
49706e39
MLI
6165 G_("return makes %q#v qualified function "
6166 "pointer from unqualified"),
6167 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6168 }
6169 }
6170 else
58393038
ZL
6171 /* Avoid warning about the volatile ObjC EH puts on decls. */
6172 if (!objc_ok)
d5c3d343
MP
6173 WARN_FOR_ASSIGNMENT (location, expr_loc,
6174 OPT_Wincompatible_pointer_types,
509c9d60 6175 G_("passing argument %d of %qE from "
58393038 6176 "incompatible pointer type"),
4b794eaf
JJ
6177 G_("assignment from incompatible pointer type"),
6178 G_("initialization from incompatible "
58393038 6179 "pointer type"),
4b794eaf 6180 G_("return from incompatible pointer type"));
58393038 6181
3e4093b6
RS
6182 return convert (type, rhs);
6183 }
b494fd98
EB
6184 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6185 {
6dcc04b0
JM
6186 /* ??? This should not be an error when inlining calls to
6187 unprototyped functions. */
c2255bc4 6188 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6189 return error_mark_node;
6190 }
3e4093b6 6191 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6192 {
3e4093b6
RS
6193 /* An explicit constant 0 can convert to a pointer,
6194 or one that results from arithmetic, even including
6195 a cast to integer type. */
928c19bb 6196 if (!null_pointer_constant)
52ec0ea3
MP
6197 WARN_FOR_ASSIGNMENT (location, expr_loc,
6198 OPT_Wint_conversion,
509c9d60 6199 G_("passing argument %d of %qE makes "
2ac2f164 6200 "pointer from integer without a cast"),
4b794eaf 6201 G_("assignment makes pointer from integer "
2ac2f164 6202 "without a cast"),
4b794eaf 6203 G_("initialization makes pointer from "
2ac2f164 6204 "integer without a cast"),
4b794eaf 6205 G_("return makes pointer from integer "
2ac2f164 6206 "without a cast"));
b3006337
EB
6207
6208 return convert (type, rhs);
400fbf9f 6209 }
3e4093b6 6210 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6211 {
52ec0ea3
MP
6212 WARN_FOR_ASSIGNMENT (location, expr_loc,
6213 OPT_Wint_conversion,
509c9d60 6214 G_("passing argument %d of %qE makes integer "
2ac2f164 6215 "from pointer without a cast"),
4b794eaf 6216 G_("assignment makes integer from pointer "
2ac2f164 6217 "without a cast"),
4b794eaf 6218 G_("initialization makes integer from pointer "
2ac2f164 6219 "without a cast"),
4b794eaf 6220 G_("return makes integer from pointer "
2ac2f164 6221 "without a cast"));
3e4093b6
RS
6222 return convert (type, rhs);
6223 }
6224 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6225 {
6226 tree ret;
6227 bool save = in_late_binary_op;
6228 in_late_binary_op = true;
6229 ret = convert (type, rhs);
6230 in_late_binary_op = save;
6231 return ret;
6232 }
400fbf9f 6233
2ac2f164 6234 switch (errtype)
3e4093b6 6235 {
2ac2f164 6236 case ic_argpass:
8ffcdea8
MP
6237 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6238 rname);
a7da8b42 6239 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6240 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6241 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6242 break;
6243 case ic_assign:
c2255bc4
AH
6244 error_at (location, "incompatible types when assigning to type %qT from "
6245 "type %qT", type, rhstype);
2ac2f164
JM
6246 break;
6247 case ic_init:
c2255bc4 6248 error_at (location,
8ffcdea8 6249 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6250 type, rhstype);
2ac2f164
JM
6251 break;
6252 case ic_return:
c2255bc4 6253 error_at (location,
8ffcdea8 6254 "incompatible types when returning type %qT but %qT was "
c2255bc4 6255 "expected", rhstype, type);
2ac2f164
JM
6256 break;
6257 default:
6258 gcc_unreachable ();
400fbf9f 6259 }
53b01f59 6260
3e4093b6
RS
6261 return error_mark_node;
6262}
3e4093b6
RS
6263\f
6264/* If VALUE is a compound expr all of whose expressions are constant, then
6265 return its value. Otherwise, return error_mark_node.
15b732b2 6266
3e4093b6
RS
6267 This is for handling COMPOUND_EXPRs as initializer elements
6268 which is allowed with a warning when -pedantic is specified. */
15b732b2 6269
3e4093b6
RS
6270static tree
6271valid_compound_expr_initializer (tree value, tree endtype)
6272{
6273 if (TREE_CODE (value) == COMPOUND_EXPR)
6274 {
6275 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6276 == error_mark_node)
6277 return error_mark_node;
6278 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6279 endtype);
6280 }
116df786 6281 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6282 return error_mark_node;
6283 else
6284 return value;
15b732b2 6285}
400fbf9f 6286\f
3e4093b6
RS
6287/* Perform appropriate conversions on the initial value of a variable,
6288 store it in the declaration DECL,
6289 and print any error messages that are appropriate.
bbbbb16a 6290 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6291 If the init is invalid, store an ERROR_MARK.
6292
6293 INIT_LOC is the location of the initial value. */
400fbf9f 6294
3e4093b6 6295void
c2255bc4 6296store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6297{
3e4093b6 6298 tree value, type;
928c19bb 6299 bool npc = false;
400fbf9f 6300
3e4093b6 6301 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6302
3e4093b6
RS
6303 type = TREE_TYPE (decl);
6304 if (TREE_CODE (type) == ERROR_MARK)
6305 return;
400fbf9f 6306
3e4093b6 6307 /* Digest the specified initializer into an expression. */
400fbf9f 6308
928c19bb
JM
6309 if (init)
6310 npc = null_pointer_constant_p (init);
c2255bc4
AH
6311 value = digest_init (init_loc, type, init, origtype, npc,
6312 true, TREE_STATIC (decl));
400fbf9f 6313
3e4093b6 6314 /* Store the expression if valid; else report error. */
400fbf9f 6315
8400e75e 6316 if (!in_system_header_at (input_location)
3f75a254 6317 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6318 warning (OPT_Wtraditional, "traditional C rejects automatic "
6319 "aggregate initialization");
2f6e4e97 6320
3e4093b6 6321 DECL_INITIAL (decl) = value;
400fbf9f 6322
3e4093b6
RS
6323 /* ANSI wants warnings about out-of-range constant initializers. */
6324 STRIP_TYPE_NOPS (value);
b8698a0f 6325 if (TREE_STATIC (decl))
c2658540 6326 constant_expression_warning (value);
400fbf9f 6327
3e4093b6
RS
6328 /* Check if we need to set array size from compound literal size. */
6329 if (TREE_CODE (type) == ARRAY_TYPE
6330 && TYPE_DOMAIN (type) == 0
6331 && value != error_mark_node)
400fbf9f 6332 {
3e4093b6
RS
6333 tree inside_init = init;
6334
ed248cf7 6335 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6336 inside_init = fold (inside_init);
6337
6338 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6339 {
8d9f82d5 6340 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6341
8d9f82d5 6342 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6343 {
6344 /* For int foo[] = (int [3]){1}; we need to set array size
6345 now since later on array initializer will be just the
6346 brace enclosed list of the compound literal. */
e30ecc5d 6347 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6348 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6349 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6350 layout_type (type);
8d9f82d5 6351 layout_decl (cldecl, 0);
e30ecc5d
JJ
6352 TREE_TYPE (decl)
6353 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6354 }
6355 }
400fbf9f 6356 }
3e4093b6
RS
6357}
6358\f
6359/* Methods for storing and printing names for error messages. */
400fbf9f 6360
3e4093b6
RS
6361/* Implement a spelling stack that allows components of a name to be pushed
6362 and popped. Each element on the stack is this structure. */
400fbf9f 6363
3e4093b6
RS
6364struct spelling
6365{
6366 int kind;
6367 union
400fbf9f 6368 {
a0f0ab9f 6369 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6370 const char *s;
6371 } u;
6372};
2f6e4e97 6373
3e4093b6
RS
6374#define SPELLING_STRING 1
6375#define SPELLING_MEMBER 2
6376#define SPELLING_BOUNDS 3
400fbf9f 6377
3e4093b6
RS
6378static struct spelling *spelling; /* Next stack element (unused). */
6379static struct spelling *spelling_base; /* Spelling stack base. */
6380static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6381
3e4093b6
RS
6382/* Macros to save and restore the spelling stack around push_... functions.
6383 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6384
3e4093b6
RS
6385#define SPELLING_DEPTH() (spelling - spelling_base)
6386#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6387
3e4093b6
RS
6388/* Push an element on the spelling stack with type KIND and assign VALUE
6389 to MEMBER. */
400fbf9f 6390
3e4093b6
RS
6391#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6392{ \
6393 int depth = SPELLING_DEPTH (); \
6394 \
6395 if (depth >= spelling_size) \
6396 { \
6397 spelling_size += 10; \
cca8ead2
BI
6398 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6399 spelling_size); \
3e4093b6
RS
6400 RESTORE_SPELLING_DEPTH (depth); \
6401 } \
6402 \
6403 spelling->kind = (KIND); \
6404 spelling->MEMBER = (VALUE); \
6405 spelling++; \
6406}
400fbf9f 6407
3e4093b6 6408/* Push STRING on the stack. Printed literally. */
400fbf9f 6409
3e4093b6
RS
6410static void
6411push_string (const char *string)
6412{
6413 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6414}
400fbf9f 6415
3e4093b6 6416/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6417
3e4093b6
RS
6418static void
6419push_member_name (tree decl)
6420{
6421 const char *const string
88388a52
JM
6422 = (DECL_NAME (decl)
6423 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6424 : _("<anonymous>"));
3e4093b6
RS
6425 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6426}
400fbf9f 6427
3e4093b6 6428/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6429
3e4093b6 6430static void
a0f0ab9f 6431push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6432{
6433 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6434}
bb58bec5 6435
3e4093b6 6436/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6437
3e4093b6
RS
6438static int
6439spelling_length (void)
6440{
6441 int size = 0;
6442 struct spelling *p;
400fbf9f 6443
3e4093b6
RS
6444 for (p = spelling_base; p < spelling; p++)
6445 {
6446 if (p->kind == SPELLING_BOUNDS)
6447 size += 25;
6448 else
6449 size += strlen (p->u.s) + 1;
6450 }
6451
6452 return size;
400fbf9f 6453}
400fbf9f 6454
3e4093b6 6455/* Print the spelling to BUFFER and return it. */
400fbf9f 6456
3e4093b6
RS
6457static char *
6458print_spelling (char *buffer)
400fbf9f 6459{
3e4093b6
RS
6460 char *d = buffer;
6461 struct spelling *p;
400fbf9f 6462
3e4093b6
RS
6463 for (p = spelling_base; p < spelling; p++)
6464 if (p->kind == SPELLING_BOUNDS)
6465 {
a0f0ab9f 6466 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6467 d += strlen (d);
6468 }
6469 else
6470 {
6471 const char *s;
6472 if (p->kind == SPELLING_MEMBER)
6473 *d++ = '.';
6474 for (s = p->u.s; (*d = *s++); d++)
6475 ;
6476 }
6477 *d++ = '\0';
6478 return buffer;
6479}
400fbf9f 6480
3e4093b6
RS
6481/* Digest the parser output INIT as an initializer for type TYPE.
6482 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6483
bbbbb16a
ILT
6484 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6485
928c19bb
JM
6486 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6487
916c5919
JM
6488 If INIT is a string constant, STRICT_STRING is true if it is
6489 unparenthesized or we should not warn here for it being parenthesized.
6490 For other types of INIT, STRICT_STRING is not used.
6491
c2255bc4
AH
6492 INIT_LOC is the location of the INIT.
6493
3e4093b6
RS
6494 REQUIRE_CONSTANT requests an error if non-constant initializers or
6495 elements are seen. */
7e842ef8 6496
3e4093b6 6497static tree
c2255bc4
AH
6498digest_init (location_t init_loc, tree type, tree init, tree origtype,
6499 bool null_pointer_constant, bool strict_string,
6500 int require_constant)
3e4093b6
RS
6501{
6502 enum tree_code code = TREE_CODE (type);
6503 tree inside_init = init;
8ce94e44 6504 tree semantic_type = NULL_TREE;
928c19bb 6505 bool maybe_const = true;
7e842ef8 6506
3e4093b6 6507 if (type == error_mark_node
f01da1a5 6508 || !init
7a0ca710 6509 || error_operand_p (init))
3e4093b6 6510 return error_mark_node;
7e842ef8 6511
ed248cf7 6512 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6513
8ce94e44
JM
6514 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6515 {
6516 semantic_type = TREE_TYPE (inside_init);
6517 inside_init = TREE_OPERAND (inside_init, 0);
6518 }
928c19bb
JM
6519 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6520 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6521
3e4093b6
RS
6522 /* Initialization of an array of chars from a string constant
6523 optionally enclosed in braces. */
7e842ef8 6524
197463ae
JM
6525 if (code == ARRAY_TYPE && inside_init
6526 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6527 {
267bac10
JM
6528 tree typ1
6529 = (TYPE_ATOMIC (TREE_TYPE (type))
6530 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6531 TYPE_QUAL_ATOMIC)
6532 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6533 /* Note that an array could be both an array of character type
6534 and an array of wchar_t if wchar_t is signed char or unsigned
6535 char. */
6536 bool char_array = (typ1 == char_type_node
6537 || typ1 == signed_char_type_node
6538 || typ1 == unsigned_char_type_node);
6539 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6540 bool char16_array = !!comptypes (typ1, char16_type_node);
6541 bool char32_array = !!comptypes (typ1, char32_type_node);
6542
6543 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6544 {
916c5919 6545 struct c_expr expr;
c466b2cd 6546 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6547 expr.value = inside_init;
6548 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6549 expr.original_type = NULL;
d033409e 6550 maybe_warn_string_init (init_loc, type, expr);
916c5919 6551
a45e580b 6552 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6553 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6554 "initialization of a flexible array member");
6555
3e4093b6 6556 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6557 TYPE_MAIN_VARIANT (type)))
3e4093b6 6558 return inside_init;
7e842ef8 6559
c466b2cd 6560 if (char_array)
3e4093b6 6561 {
c466b2cd
KVH
6562 if (typ2 != char_type_node)
6563 {
ea58ef42
MP
6564 error_init (init_loc, "char-array initialized from wide "
6565 "string");
c466b2cd
KVH
6566 return error_mark_node;
6567 }
3e4093b6 6568 }
c466b2cd 6569 else
3e4093b6 6570 {
c466b2cd
KVH
6571 if (typ2 == char_type_node)
6572 {
ea58ef42
MP
6573 error_init (init_loc, "wide character array initialized "
6574 "from non-wide string");
c466b2cd
KVH
6575 return error_mark_node;
6576 }
6577 else if (!comptypes(typ1, typ2))
6578 {
ea58ef42
MP
6579 error_init (init_loc, "wide character array initialized "
6580 "from incompatible wide string");
c466b2cd
KVH
6581 return error_mark_node;
6582 }
7e842ef8 6583 }
2f6e4e97 6584
3e4093b6
RS
6585 TREE_TYPE (inside_init) = type;
6586 if (TYPE_DOMAIN (type) != 0
6587 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6588 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6589 {
6590 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6591
c466b2cd 6592 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6593 because it's ok to ignore the terminating null char
6594 that is counted in the length of the constant. */
5eb4df45
ILT
6595 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6596 (len
6597 - (TYPE_PRECISION (typ1)
6598 / BITS_PER_UNIT))))
6599 pedwarn_init (init_loc, 0,
6600 ("initializer-string for array of chars "
6601 "is too long"));
6602 else if (warn_cxx_compat
6603 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6604 warning_at (init_loc, OPT_Wc___compat,
6605 ("initializer-string for array chars "
6606 "is too long for C++"));
6607 }
7e842ef8 6608
3e4093b6 6609 return inside_init;
7e842ef8 6610 }
197463ae
JM
6611 else if (INTEGRAL_TYPE_P (typ1))
6612 {
ea58ef42 6613 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6614 "from string constant");
6615 return error_mark_node;
6616 }
7e842ef8
PE
6617 }
6618
3e4093b6
RS
6619 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6620 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6621 below and handle as a constructor. */
e89be13b
JJ
6622 if (code == VECTOR_TYPE
6623 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6624 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6625 && TREE_CONSTANT (inside_init))
6626 {
6627 if (TREE_CODE (inside_init) == VECTOR_CST
6628 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6629 TYPE_MAIN_VARIANT (type)))
6630 return inside_init;
6631
6632 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6633 {
4038c495
GB
6634 unsigned HOST_WIDE_INT ix;
6635 tree value;
6636 bool constant_p = true;
e89be13b
JJ
6637
6638 /* Iterate through elements and check if all constructor
6639 elements are *_CSTs. */
4038c495
GB
6640 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6641 if (!CONSTANT_CLASS_P (value))
6642 {
6643 constant_p = false;
6644 break;
6645 }
e89be13b 6646
4038c495
GB
6647 if (constant_p)
6648 return build_vector_from_ctor (type,
6649 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6650 }
6651 }
6035d635 6652
ca085fd7
MLI
6653 if (warn_sequence_point)
6654 verify_sequence_points (inside_init);
6655
3e4093b6
RS
6656 /* Any type can be initialized
6657 from an expression of the same type, optionally with braces. */
400fbf9f 6658
3e4093b6
RS
6659 if (inside_init && TREE_TYPE (inside_init) != 0
6660 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6661 TYPE_MAIN_VARIANT (type))
3e4093b6 6662 || (code == ARRAY_TYPE
132da1a5 6663 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6664 || (code == VECTOR_TYPE
132da1a5 6665 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6666 || (code == POINTER_TYPE
3897f229 6667 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6668 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6669 TREE_TYPE (type)))))
3e4093b6
RS
6670 {
6671 if (code == POINTER_TYPE)
b494fd98 6672 {
b494fd98
EB
6673 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6674 {
f2a71bbc
JM
6675 if (TREE_CODE (inside_init) == STRING_CST
6676 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6677 inside_init = array_to_pointer_conversion
6678 (init_loc, inside_init);
f2a71bbc
JM
6679 else
6680 {
ea58ef42 6681 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6682 return error_mark_node;
6683 }
b494fd98 6684 }
f2a71bbc 6685 }
b494fd98 6686
bae39a73
NS
6687 if (code == VECTOR_TYPE)
6688 /* Although the types are compatible, we may require a
6689 conversion. */
6690 inside_init = convert (type, inside_init);
3e4093b6 6691
ca58211b 6692 if (require_constant
3e4093b6 6693 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6694 {
3e4093b6
RS
6695 /* As an extension, allow initializing objects with static storage
6696 duration with compound literals (which are then treated just as
ca58211b
PB
6697 the brace enclosed list they contain). Also allow this for
6698 vectors, as we can only assign them with compound literals. */
7278465e
MP
6699 if (flag_isoc99 && code != VECTOR_TYPE)
6700 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
6701 "is not constant");
3e4093b6
RS
6702 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6703 inside_init = DECL_INITIAL (decl);
400fbf9f 6704 }
3e4093b6
RS
6705
6706 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6707 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6708 {
ea58ef42
MP
6709 error_init (init_loc, "array initialized from non-constant array "
6710 "expression");
3e4093b6 6711 return error_mark_node;
400fbf9f 6712 }
400fbf9f 6713
c1771a20 6714 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6715 -pedantic-errors is specified. In the later case, we always want
6716 an error. In the former case, we simply want a warning. */
6717 if (require_constant && pedantic
6718 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6719 {
6720 inside_init
6721 = valid_compound_expr_initializer (inside_init,
6722 TREE_TYPE (inside_init));
6723 if (inside_init == error_mark_node)
ea58ef42 6724 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6725 else
c1771a20 6726 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6727 "initializer element is not constant");
3e4093b6
RS
6728 if (flag_pedantic_errors)
6729 inside_init = error_mark_node;
6730 }
6731 else if (require_constant
116df786
RH
6732 && !initializer_constant_valid_p (inside_init,
6733 TREE_TYPE (inside_init)))
3e4093b6 6734 {
ea58ef42 6735 error_init (init_loc, "initializer element is not constant");
3e4093b6 6736 inside_init = error_mark_node;
8b40563c 6737 }
928c19bb 6738 else if (require_constant && !maybe_const)
c2255bc4 6739 pedwarn_init (init_loc, 0,
928c19bb 6740 "initializer element is not a constant expression");
f735a153 6741
90137d8f 6742 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6743 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6744 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6745 type, inside_init, origtype,
bbbbb16a 6746 ic_init, null_pointer_constant,
928c19bb 6747 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6748 return inside_init;
6749 }
f735a153 6750
3e4093b6 6751 /* Handle scalar types, including conversions. */
400fbf9f 6752
ab22c1fa
CF
6753 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6754 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6755 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6756 {
f2a71bbc
JM
6757 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6758 && (TREE_CODE (init) == STRING_CST
6759 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6760 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6761 if (semantic_type)
6762 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6763 inside_init);
3e4093b6 6764 inside_init
68fca595
MP
6765 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6766 inside_init, origtype, ic_init,
6767 null_pointer_constant, NULL_TREE, NULL_TREE,
6768 0);
2f6e4e97 6769
3274deff
JW
6770 /* Check to see if we have already given an error message. */
6771 if (inside_init == error_mark_node)
6772 ;
3f75a254 6773 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6774 {
ea58ef42 6775 error_init (init_loc, "initializer element is not constant");
3e4093b6 6776 inside_init = error_mark_node;
400fbf9f 6777 }
3e4093b6 6778 else if (require_constant
116df786
RH
6779 && !initializer_constant_valid_p (inside_init,
6780 TREE_TYPE (inside_init)))
400fbf9f 6781 {
ea58ef42
MP
6782 error_init (init_loc, "initializer element is not computable at "
6783 "load time");
3e4093b6 6784 inside_init = error_mark_node;
400fbf9f 6785 }
928c19bb 6786 else if (require_constant && !maybe_const)
c2255bc4 6787 pedwarn_init (init_loc, 0,
928c19bb 6788 "initializer element is not a constant expression");
3e4093b6
RS
6789
6790 return inside_init;
400fbf9f 6791 }
d9fc6069 6792
3e4093b6 6793 /* Come here only for records and arrays. */
d9fc6069 6794
3e4093b6 6795 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6796 {
ea58ef42 6797 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6798 return error_mark_node;
d9fc6069 6799 }
3e4093b6 6800
ea58ef42 6801 error_init (init_loc, "invalid initializer");
3e4093b6 6802 return error_mark_node;
d9fc6069 6803}
400fbf9f 6804\f
3e4093b6 6805/* Handle initializers that use braces. */
400fbf9f 6806
3e4093b6
RS
6807/* Type of object we are accumulating a constructor for.
6808 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6809static tree constructor_type;
400fbf9f 6810
3e4093b6
RS
6811/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6812 left to fill. */
6813static tree constructor_fields;
400fbf9f 6814
3e4093b6
RS
6815/* For an ARRAY_TYPE, this is the specified index
6816 at which to store the next element we get. */
6817static tree constructor_index;
400fbf9f 6818
3e4093b6
RS
6819/* For an ARRAY_TYPE, this is the maximum index. */
6820static tree constructor_max_index;
400fbf9f 6821
3e4093b6
RS
6822/* For a RECORD_TYPE, this is the first field not yet written out. */
6823static tree constructor_unfilled_fields;
400fbf9f 6824
3e4093b6
RS
6825/* For an ARRAY_TYPE, this is the index of the first element
6826 not yet written out. */
6827static tree constructor_unfilled_index;
895ea614 6828
3e4093b6
RS
6829/* In a RECORD_TYPE, the byte index of the next consecutive field.
6830 This is so we can generate gaps between fields, when appropriate. */
6831static tree constructor_bit_index;
10d5caec 6832
3e4093b6
RS
6833/* If we are saving up the elements rather than allocating them,
6834 this is the list of elements so far (in reverse order,
6835 most recent first). */
9771b263 6836static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6837
3e4093b6
RS
6838/* 1 if constructor should be incrementally stored into a constructor chain,
6839 0 if all the elements should be kept in AVL tree. */
6840static int constructor_incremental;
ad47f1e5 6841
3e4093b6
RS
6842/* 1 if so far this constructor's elements are all compile-time constants. */
6843static int constructor_constant;
ad47f1e5 6844
3e4093b6
RS
6845/* 1 if so far this constructor's elements are all valid address constants. */
6846static int constructor_simple;
ad47f1e5 6847
928c19bb
JM
6848/* 1 if this constructor has an element that cannot be part of a
6849 constant expression. */
6850static int constructor_nonconst;
6851
3e4093b6
RS
6852/* 1 if this constructor is erroneous so far. */
6853static int constructor_erroneous;
d45cf215 6854
9bac5cbb
G
6855/* 1 if this constructor is the universal zero initializer { 0 }. */
6856static int constructor_zeroinit;
6857
3e4093b6
RS
6858/* Structure for managing pending initializer elements, organized as an
6859 AVL tree. */
d45cf215 6860
3e4093b6 6861struct init_node
d45cf215 6862{
3e4093b6
RS
6863 struct init_node *left, *right;
6864 struct init_node *parent;
6865 int balance;
6866 tree purpose;
6867 tree value;
bbbbb16a 6868 tree origtype;
d45cf215
RS
6869};
6870
3e4093b6
RS
6871/* Tree of pending elements at this constructor level.
6872 These are elements encountered out of order
6873 which belong at places we haven't reached yet in actually
6874 writing the output.
6875 Will never hold tree nodes across GC runs. */
6876static struct init_node *constructor_pending_elts;
d45cf215 6877
3e4093b6
RS
6878/* The SPELLING_DEPTH of this constructor. */
6879static int constructor_depth;
d45cf215 6880
3e4093b6
RS
6881/* DECL node for which an initializer is being read.
6882 0 means we are reading a constructor expression
6883 such as (struct foo) {...}. */
6884static tree constructor_decl;
d45cf215 6885
3e4093b6
RS
6886/* Nonzero if this is an initializer for a top-level decl. */
6887static int constructor_top_level;
d45cf215 6888
3e4093b6
RS
6889/* Nonzero if there were any member designators in this initializer. */
6890static int constructor_designated;
d45cf215 6891
3e4093b6
RS
6892/* Nesting depth of designator list. */
6893static int designator_depth;
d45cf215 6894
3e4093b6 6895/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6896static int designator_erroneous;
d45cf215 6897
3e4093b6
RS
6898\f
6899/* This stack has a level for each implicit or explicit level of
6900 structuring in the initializer, including the outermost one. It
6901 saves the values of most of the variables above. */
d45cf215 6902
3e4093b6
RS
6903struct constructor_range_stack;
6904
6905struct constructor_stack
d45cf215 6906{
3e4093b6
RS
6907 struct constructor_stack *next;
6908 tree type;
6909 tree fields;
6910 tree index;
6911 tree max_index;
6912 tree unfilled_index;
6913 tree unfilled_fields;
6914 tree bit_index;
9771b263 6915 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6916 struct init_node *pending_elts;
6917 int offset;
6918 int depth;
916c5919 6919 /* If value nonzero, this value should replace the entire
3e4093b6 6920 constructor at this level. */
916c5919 6921 struct c_expr replacement_value;
3e4093b6
RS
6922 struct constructor_range_stack *range_stack;
6923 char constant;
6924 char simple;
928c19bb 6925 char nonconst;
3e4093b6
RS
6926 char implicit;
6927 char erroneous;
6928 char outer;
6929 char incremental;
6930 char designated;
976d5a22 6931 int designator_depth;
3e4093b6 6932};
d45cf215 6933
802415d1 6934static struct constructor_stack *constructor_stack;
d45cf215 6935
3e4093b6
RS
6936/* This stack represents designators from some range designator up to
6937 the last designator in the list. */
d45cf215 6938
3e4093b6
RS
6939struct constructor_range_stack
6940{
6941 struct constructor_range_stack *next, *prev;
6942 struct constructor_stack *stack;
6943 tree range_start;
6944 tree index;
6945 tree range_end;
6946 tree fields;
6947};
d45cf215 6948
802415d1 6949static struct constructor_range_stack *constructor_range_stack;
d45cf215 6950
3e4093b6
RS
6951/* This stack records separate initializers that are nested.
6952 Nested initializers can't happen in ANSI C, but GNU C allows them
6953 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6954
3e4093b6 6955struct initializer_stack
d45cf215 6956{
3e4093b6
RS
6957 struct initializer_stack *next;
6958 tree decl;
3e4093b6
RS
6959 struct constructor_stack *constructor_stack;
6960 struct constructor_range_stack *constructor_range_stack;
9771b263 6961 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6962 struct spelling *spelling;
6963 struct spelling *spelling_base;
6964 int spelling_size;
6965 char top_level;
6966 char require_constant_value;
6967 char require_constant_elements;
6968};
d45cf215 6969
802415d1 6970static struct initializer_stack *initializer_stack;
3e4093b6
RS
6971\f
6972/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6973
6974void
a396f8ae 6975start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6976{
3e4093b6 6977 const char *locus;
cceb1885 6978 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6979
3e4093b6 6980 p->decl = constructor_decl;
3e4093b6
RS
6981 p->require_constant_value = require_constant_value;
6982 p->require_constant_elements = require_constant_elements;
6983 p->constructor_stack = constructor_stack;
6984 p->constructor_range_stack = constructor_range_stack;
6985 p->elements = constructor_elements;
6986 p->spelling = spelling;
6987 p->spelling_base = spelling_base;
6988 p->spelling_size = spelling_size;
6989 p->top_level = constructor_top_level;
6990 p->next = initializer_stack;
6991 initializer_stack = p;
400fbf9f 6992
3e4093b6 6993 constructor_decl = decl;
3e4093b6
RS
6994 constructor_designated = 0;
6995 constructor_top_level = top_level;
400fbf9f 6996
6f17bbcf 6997 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
6998 {
6999 require_constant_value = TREE_STATIC (decl);
7000 require_constant_elements
7001 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7002 /* For a scalar, you can always use any value to initialize,
7003 even within braces. */
7004 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7005 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7006 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7007 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 7008 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7009 }
7010 else
7011 {
7012 require_constant_value = 0;
7013 require_constant_elements = 0;
88388a52 7014 locus = _("(anonymous)");
3e4093b6 7015 }
b71c7f8a 7016
3e4093b6
RS
7017 constructor_stack = 0;
7018 constructor_range_stack = 0;
b71c7f8a 7019
9bac5cbb 7020 found_missing_braces = 0;
3e4093b6
RS
7021
7022 spelling_base = 0;
7023 spelling_size = 0;
7024 RESTORE_SPELLING_DEPTH (0);
7025
7026 if (locus)
7027 push_string (locus);
7028}
7029
7030void
7031finish_init (void)
b71c7f8a 7032{
3e4093b6 7033 struct initializer_stack *p = initializer_stack;
b71c7f8a 7034
3e4093b6
RS
7035 /* Free the whole constructor stack of this initializer. */
7036 while (constructor_stack)
7037 {
7038 struct constructor_stack *q = constructor_stack;
7039 constructor_stack = q->next;
7040 free (q);
7041 }
7042
366de0ce 7043 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7044
7045 /* Pop back to the data of the outer initializer (if any). */
36579663 7046 free (spelling_base);
3aeb3655 7047
3e4093b6 7048 constructor_decl = p->decl;
3e4093b6
RS
7049 require_constant_value = p->require_constant_value;
7050 require_constant_elements = p->require_constant_elements;
7051 constructor_stack = p->constructor_stack;
7052 constructor_range_stack = p->constructor_range_stack;
7053 constructor_elements = p->elements;
7054 spelling = p->spelling;
7055 spelling_base = p->spelling_base;
7056 spelling_size = p->spelling_size;
7057 constructor_top_level = p->top_level;
7058 initializer_stack = p->next;
7059 free (p);
b71c7f8a 7060}
400fbf9f 7061\f
3e4093b6
RS
7062/* Call here when we see the initializer is surrounded by braces.
7063 This is instead of a call to push_init_level;
7064 it is matched by a call to pop_init_level.
400fbf9f 7065
3e4093b6
RS
7066 TYPE is the type to initialize, for a constructor expression.
7067 For an initializer for a decl, TYPE is zero. */
400fbf9f 7068
3e4093b6
RS
7069void
7070really_start_incremental_init (tree type)
400fbf9f 7071{
5d038c4c 7072 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7073
3e4093b6
RS
7074 if (type == 0)
7075 type = TREE_TYPE (constructor_decl);
400fbf9f 7076
b6fc2cdb
PB
7077 if (TREE_CODE (type) == VECTOR_TYPE
7078 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7079 error ("opaque vector types cannot be initialized");
400fbf9f 7080
3e4093b6
RS
7081 p->type = constructor_type;
7082 p->fields = constructor_fields;
7083 p->index = constructor_index;
7084 p->max_index = constructor_max_index;
7085 p->unfilled_index = constructor_unfilled_index;
7086 p->unfilled_fields = constructor_unfilled_fields;
7087 p->bit_index = constructor_bit_index;
7088 p->elements = constructor_elements;
7089 p->constant = constructor_constant;
7090 p->simple = constructor_simple;
928c19bb 7091 p->nonconst = constructor_nonconst;
3e4093b6
RS
7092 p->erroneous = constructor_erroneous;
7093 p->pending_elts = constructor_pending_elts;
7094 p->depth = constructor_depth;
916c5919
JM
7095 p->replacement_value.value = 0;
7096 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7097 p->replacement_value.original_type = NULL;
3e4093b6
RS
7098 p->implicit = 0;
7099 p->range_stack = 0;
7100 p->outer = 0;
7101 p->incremental = constructor_incremental;
7102 p->designated = constructor_designated;
976d5a22 7103 p->designator_depth = designator_depth;
3e4093b6
RS
7104 p->next = 0;
7105 constructor_stack = p;
b13aca19 7106
3e4093b6
RS
7107 constructor_constant = 1;
7108 constructor_simple = 1;
928c19bb 7109 constructor_nonconst = 0;
3e4093b6 7110 constructor_depth = SPELLING_DEPTH ();
9771b263 7111 constructor_elements = NULL;
3e4093b6
RS
7112 constructor_pending_elts = 0;
7113 constructor_type = type;
7114 constructor_incremental = 1;
7115 constructor_designated = 0;
9bac5cbb 7116 constructor_zeroinit = 1;
3e4093b6 7117 designator_depth = 0;
b06df647 7118 designator_erroneous = 0;
400fbf9f 7119
3e4093b6
RS
7120 if (TREE_CODE (constructor_type) == RECORD_TYPE
7121 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7122 {
3e4093b6
RS
7123 constructor_fields = TYPE_FIELDS (constructor_type);
7124 /* Skip any nameless bit fields at the beginning. */
7125 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7126 && DECL_NAME (constructor_fields) == 0)
910ad8de 7127 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7128
3e4093b6
RS
7129 constructor_unfilled_fields = constructor_fields;
7130 constructor_bit_index = bitsize_zero_node;
400fbf9f 7131 }
3e4093b6
RS
7132 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7133 {
7134 if (TYPE_DOMAIN (constructor_type))
7135 {
7136 constructor_max_index
7137 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7138
3e4093b6
RS
7139 /* Detect non-empty initializations of zero-length arrays. */
7140 if (constructor_max_index == NULL_TREE
7141 && TYPE_SIZE (constructor_type))
9a9d280e 7142 constructor_max_index = integer_minus_one_node;
400fbf9f 7143
3e4093b6
RS
7144 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7145 to initialize VLAs will cause a proper error; avoid tree
7146 checking errors as well by setting a safe value. */
7147 if (constructor_max_index
7148 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7149 constructor_max_index = integer_minus_one_node;
59c83dbf 7150
3e4093b6
RS
7151 constructor_index
7152 = convert (bitsizetype,
7153 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7154 }
3e4093b6 7155 else
493179da
JM
7156 {
7157 constructor_index = bitsize_zero_node;
7158 constructor_max_index = NULL_TREE;
7159 }
59c83dbf 7160
3e4093b6
RS
7161 constructor_unfilled_index = constructor_index;
7162 }
7163 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7164 {
7165 /* Vectors are like simple fixed-size arrays. */
7166 constructor_max_index =
c62c040f 7167 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7168 constructor_index = bitsize_zero_node;
3e4093b6
RS
7169 constructor_unfilled_index = constructor_index;
7170 }
7171 else
7172 {
7173 /* Handle the case of int x = {5}; */
7174 constructor_fields = constructor_type;
7175 constructor_unfilled_fields = constructor_type;
7176 }
7177}
7178\f
7179/* Push down into a subobject, for initialization.
7180 If this is for an explicit set of braces, IMPLICIT is 0.
7181 If it is because the next element belongs at a lower level,
7182 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7183
3e4093b6 7184void
ea58ef42
MP
7185push_init_level (location_t loc, int implicit,
7186 struct obstack *braced_init_obstack)
3e4093b6
RS
7187{
7188 struct constructor_stack *p;
7189 tree value = NULL_TREE;
400fbf9f 7190
3e4093b6 7191 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7192 pop them now. If implicit == 1, this will have been done in
7193 process_init_element; do not repeat it here because in the case
7194 of excess initializers for an empty aggregate this leads to an
7195 infinite cycle of popping a level and immediately recreating
7196 it. */
7197 if (implicit != 1)
3e4093b6 7198 {
472d98b4
JM
7199 while (constructor_stack->implicit)
7200 {
7201 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7202 || TREE_CODE (constructor_type) == UNION_TYPE)
7203 && constructor_fields == 0)
34cf811f 7204 process_init_element (input_location,
ea58ef42 7205 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7206 true, braced_init_obstack);
472d98b4
JM
7207 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7208 && constructor_max_index
7209 && tree_int_cst_lt (constructor_max_index,
7210 constructor_index))
34cf811f 7211 process_init_element (input_location,
ea58ef42 7212 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7213 true, braced_init_obstack);
472d98b4
JM
7214 else
7215 break;
7216 }
3e4093b6 7217 }
400fbf9f 7218
3e4093b6
RS
7219 /* Unless this is an explicit brace, we need to preserve previous
7220 content if any. */
7221 if (implicit)
7222 {
7223 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7224 || TREE_CODE (constructor_type) == UNION_TYPE)
7225 && constructor_fields)
a1e3b3d9 7226 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7227 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7228 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7229 }
7230
5d038c4c 7231 p = XNEW (struct constructor_stack);
3e4093b6
RS
7232 p->type = constructor_type;
7233 p->fields = constructor_fields;
7234 p->index = constructor_index;
7235 p->max_index = constructor_max_index;
7236 p->unfilled_index = constructor_unfilled_index;
7237 p->unfilled_fields = constructor_unfilled_fields;
7238 p->bit_index = constructor_bit_index;
7239 p->elements = constructor_elements;
7240 p->constant = constructor_constant;
7241 p->simple = constructor_simple;
928c19bb 7242 p->nonconst = constructor_nonconst;
3e4093b6
RS
7243 p->erroneous = constructor_erroneous;
7244 p->pending_elts = constructor_pending_elts;
7245 p->depth = constructor_depth;
916c5919
JM
7246 p->replacement_value.value = 0;
7247 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7248 p->replacement_value.original_type = NULL;
3e4093b6
RS
7249 p->implicit = implicit;
7250 p->outer = 0;
7251 p->incremental = constructor_incremental;
7252 p->designated = constructor_designated;
976d5a22 7253 p->designator_depth = designator_depth;
3e4093b6
RS
7254 p->next = constructor_stack;
7255 p->range_stack = 0;
7256 constructor_stack = p;
400fbf9f 7257
3e4093b6
RS
7258 constructor_constant = 1;
7259 constructor_simple = 1;
928c19bb 7260 constructor_nonconst = 0;
3e4093b6 7261 constructor_depth = SPELLING_DEPTH ();
9771b263 7262 constructor_elements = NULL;
3e4093b6
RS
7263 constructor_incremental = 1;
7264 constructor_designated = 0;
7265 constructor_pending_elts = 0;
7266 if (!implicit)
400fbf9f 7267 {
3e4093b6
RS
7268 p->range_stack = constructor_range_stack;
7269 constructor_range_stack = 0;
7270 designator_depth = 0;
b06df647 7271 designator_erroneous = 0;
3e4093b6 7272 }
400fbf9f 7273
3e4093b6
RS
7274 /* Don't die if an entire brace-pair level is superfluous
7275 in the containing level. */
7276 if (constructor_type == 0)
7277 ;
7278 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7279 || TREE_CODE (constructor_type) == UNION_TYPE)
7280 {
7281 /* Don't die if there are extra init elts at the end. */
7282 if (constructor_fields == 0)
7283 constructor_type = 0;
7284 else
400fbf9f 7285 {
3e4093b6
RS
7286 constructor_type = TREE_TYPE (constructor_fields);
7287 push_member_name (constructor_fields);
7288 constructor_depth++;
400fbf9f 7289 }
6a358dcb
MP
7290 /* If upper initializer is designated, then mark this as
7291 designated too to prevent bogus warnings. */
7292 constructor_designated = p->designated;
3e4093b6
RS
7293 }
7294 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7295 {
7296 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7297 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7298 constructor_depth++;
400fbf9f
JW
7299 }
7300
3e4093b6 7301 if (constructor_type == 0)
400fbf9f 7302 {
ea58ef42 7303 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7304 constructor_fields = 0;
7305 constructor_unfilled_fields = 0;
7306 return;
400fbf9f
JW
7307 }
7308
3e4093b6
RS
7309 if (value && TREE_CODE (value) == CONSTRUCTOR)
7310 {
7311 constructor_constant = TREE_CONSTANT (value);
7312 constructor_simple = TREE_STATIC (value);
928c19bb 7313 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7314 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7315 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7316 && (TREE_CODE (constructor_type) == RECORD_TYPE
7317 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7318 set_nonincremental_init (braced_init_obstack);
3e4093b6 7319 }
400fbf9f 7320
9bac5cbb
G
7321 if (implicit == 1)
7322 found_missing_braces = 1;
400fbf9f 7323
3e4093b6
RS
7324 if (TREE_CODE (constructor_type) == RECORD_TYPE
7325 || TREE_CODE (constructor_type) == UNION_TYPE)
7326 {
7327 constructor_fields = TYPE_FIELDS (constructor_type);
7328 /* Skip any nameless bit fields at the beginning. */
7329 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7330 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7331 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7332
3e4093b6
RS
7333 constructor_unfilled_fields = constructor_fields;
7334 constructor_bit_index = bitsize_zero_node;
7335 }
7336 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7337 {
7338 /* Vectors are like simple fixed-size arrays. */
7339 constructor_max_index =
c62c040f
RG
7340 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7341 constructor_index = bitsize_int (0);
3e4093b6
RS
7342 constructor_unfilled_index = constructor_index;
7343 }
7344 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7345 {
7346 if (TYPE_DOMAIN (constructor_type))
7347 {
7348 constructor_max_index
7349 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7350
3e4093b6
RS
7351 /* Detect non-empty initializations of zero-length arrays. */
7352 if (constructor_max_index == NULL_TREE
7353 && TYPE_SIZE (constructor_type))
9a9d280e 7354 constructor_max_index = integer_minus_one_node;
de520661 7355
3e4093b6
RS
7356 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7357 to initialize VLAs will cause a proper error; avoid tree
7358 checking errors as well by setting a safe value. */
7359 if (constructor_max_index
7360 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7361 constructor_max_index = integer_minus_one_node;
b62acd60 7362
3e4093b6
RS
7363 constructor_index
7364 = convert (bitsizetype,
7365 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7366 }
7367 else
7368 constructor_index = bitsize_zero_node;
de520661 7369
3e4093b6
RS
7370 constructor_unfilled_index = constructor_index;
7371 if (value && TREE_CODE (value) == STRING_CST)
7372 {
7373 /* We need to split the char/wchar array into individual
7374 characters, so that we don't have to special case it
7375 everywhere. */
a1e3b3d9 7376 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7377 }
7378 }
7379 else
7380 {
b4519d39 7381 if (constructor_type != error_mark_node)
96b40f8d 7382 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7383 constructor_fields = constructor_type;
7384 constructor_unfilled_fields = constructor_type;
7385 }
7386}
8b6a5902 7387
3e4093b6 7388/* At the end of an implicit or explicit brace level,
916c5919
JM
7389 finish up that level of constructor. If a single expression
7390 with redundant braces initialized that level, return the
7391 c_expr structure for that expression. Otherwise, the original_code
7392 element is set to ERROR_MARK.
7393 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7394 from inner levels (process_init_element ignores that),
916c5919 7395 but return error_mark_node as the value from the outermost level
3e4093b6 7396 (that's what we want to put in DECL_INITIAL).
916c5919 7397 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7398
916c5919 7399struct c_expr
ea58ef42
MP
7400pop_init_level (location_t loc, int implicit,
7401 struct obstack *braced_init_obstack)
3e4093b6
RS
7402{
7403 struct constructor_stack *p;
916c5919
JM
7404 struct c_expr ret;
7405 ret.value = 0;
7406 ret.original_code = ERROR_MARK;
6866c6e8 7407 ret.original_type = NULL;
de520661 7408
3e4093b6
RS
7409 if (implicit == 0)
7410 {
7411 /* When we come to an explicit close brace,
7412 pop any inner levels that didn't have explicit braces. */
7413 while (constructor_stack->implicit)
34cf811f 7414 process_init_element (input_location,
ea58ef42 7415 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7416 true, braced_init_obstack);
366de0ce 7417 gcc_assert (!constructor_range_stack);
3e4093b6 7418 }
e5e809f4 7419
0066ef9c
RH
7420 /* Now output all pending elements. */
7421 constructor_incremental = 1;
a1e3b3d9 7422 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7423
3e4093b6 7424 p = constructor_stack;
e5e809f4 7425
3e4093b6
RS
7426 /* Error for initializing a flexible array member, or a zero-length
7427 array member in an inappropriate context. */
7428 if (constructor_type && constructor_fields
7429 && TREE_CODE (constructor_type) == ARRAY_TYPE
7430 && TYPE_DOMAIN (constructor_type)
3f75a254 7431 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7432 {
7433 /* Silently discard empty initializations. The parser will
7434 already have pedwarned for empty brackets. */
7435 if (integer_zerop (constructor_unfilled_index))
7436 constructor_type = NULL_TREE;
366de0ce 7437 else
3e4093b6 7438 {
366de0ce 7439 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7440
3e4093b6 7441 if (constructor_depth > 2)
ea58ef42 7442 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7443 else
d033409e 7444 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7445 "initialization of a flexible array member");
de520661 7446
3e4093b6
RS
7447 /* We have already issued an error message for the existence
7448 of a flexible array member not at the end of the structure.
535a42b1 7449 Discard the initializer so that we do not die later. */
910ad8de 7450 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7451 constructor_type = NULL_TREE;
7452 }
3e4093b6 7453 }
de520661 7454
84937de2
MP
7455 /* Initialization with { } counts as zeroinit. */
7456 if (vec_safe_length (constructor_elements) == 0)
7457 constructor_zeroinit = 1;
7458 /* If the constructor has more than one element, it can't be { 0 }. */
7459 else if (vec_safe_length (constructor_elements) != 1)
9bac5cbb
G
7460 constructor_zeroinit = 0;
7461
7462 /* Warn when some structs are initialized with direct aggregation. */
7463 if (!implicit && found_missing_braces && warn_missing_braces
7464 && !constructor_zeroinit)
7465 {
7466 warning_init (loc, OPT_Wmissing_braces,
7467 "missing braces around initializer");
7468 }
7469
3e4093b6 7470 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7471 if (warn_missing_field_initializers
3e4093b6
RS
7472 && constructor_type
7473 && TREE_CODE (constructor_type) == RECORD_TYPE
7474 && constructor_unfilled_fields)
7475 {
7476 /* Do not warn for flexible array members or zero-length arrays. */
7477 while (constructor_unfilled_fields
3f75a254 7478 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7479 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7480 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7481
49819fef
AM
7482 if (constructor_unfilled_fields
7483 /* Do not warn if this level of the initializer uses member
7484 designators; it is likely to be deliberate. */
7485 && !constructor_designated
84937de2 7486 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 7487 && !constructor_zeroinit)
3e4093b6 7488 {
32397f22
MLI
7489 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7490 "missing initializer for field %qD of %qT",
7491 constructor_unfilled_fields,
7492 constructor_type))
7493 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7494 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7495 }
7496 }
de520661 7497
3e4093b6 7498 /* Pad out the end of the structure. */
916c5919 7499 if (p->replacement_value.value)
3e4093b6
RS
7500 /* If this closes a superfluous brace pair,
7501 just pass out the element between them. */
916c5919 7502 ret = p->replacement_value;
3e4093b6
RS
7503 else if (constructor_type == 0)
7504 ;
7505 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7506 && TREE_CODE (constructor_type) != UNION_TYPE
7507 && TREE_CODE (constructor_type) != ARRAY_TYPE
7508 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7509 {
7510 /* A nonincremental scalar initializer--just return
7511 the element, after verifying there is just one. */
9771b263 7512 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7513 {
7514 if (!constructor_erroneous)
ea58ef42 7515 error_init (loc, "empty scalar initializer");
916c5919 7516 ret.value = error_mark_node;
3e4093b6 7517 }
9771b263 7518 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7519 {
ea58ef42 7520 error_init (loc, "extra elements in scalar initializer");
9771b263 7521 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7522 }
7523 else
9771b263 7524 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7525 }
7526 else
7527 {
7528 if (constructor_erroneous)
916c5919 7529 ret.value = error_mark_node;
3e4093b6
RS
7530 else
7531 {
916c5919 7532 ret.value = build_constructor (constructor_type,
4038c495 7533 constructor_elements);
3e4093b6 7534 if (constructor_constant)
51eed280 7535 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7536 if (constructor_constant && constructor_simple)
916c5919 7537 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7538 if (constructor_nonconst)
7539 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7540 }
7541 }
de520661 7542
928c19bb
JM
7543 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7544 {
7545 if (constructor_nonconst)
7546 ret.original_code = C_MAYBE_CONST_EXPR;
7547 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7548 ret.original_code = ERROR_MARK;
7549 }
7550
3e4093b6
RS
7551 constructor_type = p->type;
7552 constructor_fields = p->fields;
7553 constructor_index = p->index;
7554 constructor_max_index = p->max_index;
7555 constructor_unfilled_index = p->unfilled_index;
7556 constructor_unfilled_fields = p->unfilled_fields;
7557 constructor_bit_index = p->bit_index;
7558 constructor_elements = p->elements;
7559 constructor_constant = p->constant;
7560 constructor_simple = p->simple;
928c19bb 7561 constructor_nonconst = p->nonconst;
3e4093b6
RS
7562 constructor_erroneous = p->erroneous;
7563 constructor_incremental = p->incremental;
7564 constructor_designated = p->designated;
976d5a22 7565 designator_depth = p->designator_depth;
3e4093b6
RS
7566 constructor_pending_elts = p->pending_elts;
7567 constructor_depth = p->depth;
7568 if (!p->implicit)
7569 constructor_range_stack = p->range_stack;
7570 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7571
3e4093b6
RS
7572 constructor_stack = p->next;
7573 free (p);
b621a4dd 7574
5d5e98dc
VR
7575 if (ret.value == 0 && constructor_stack == 0)
7576 ret.value = error_mark_node;
916c5919 7577 return ret;
3e4093b6 7578}
8b6a5902 7579
3e4093b6
RS
7580/* Common handling for both array range and field name designators.
7581 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7582
3e4093b6 7583static int
ea58ef42
MP
7584set_designator (location_t loc, int array,
7585 struct obstack *braced_init_obstack)
de520661 7586{
3e4093b6
RS
7587 tree subtype;
7588 enum tree_code subcode;
de520661 7589
3e4093b6
RS
7590 /* Don't die if an entire brace-pair level is superfluous
7591 in the containing level. */
7592 if (constructor_type == 0)
7593 return 1;
de520661 7594
366de0ce
NS
7595 /* If there were errors in this designator list already, bail out
7596 silently. */
b06df647 7597 if (designator_erroneous)
3e4093b6 7598 return 1;
e28cae4f 7599
3e4093b6
RS
7600 if (!designator_depth)
7601 {
366de0ce 7602 gcc_assert (!constructor_range_stack);
de520661 7603
3e4093b6
RS
7604 /* Designator list starts at the level of closest explicit
7605 braces. */
7606 while (constructor_stack->implicit)
34cf811f 7607 process_init_element (input_location,
ea58ef42 7608 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7609 true, braced_init_obstack);
3e4093b6
RS
7610 constructor_designated = 1;
7611 return 0;
7612 }
de520661 7613
366de0ce 7614 switch (TREE_CODE (constructor_type))
3c3fa147 7615 {
366de0ce
NS
7616 case RECORD_TYPE:
7617 case UNION_TYPE:
3e4093b6
RS
7618 subtype = TREE_TYPE (constructor_fields);
7619 if (subtype != error_mark_node)
7620 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7621 break;
7622 case ARRAY_TYPE:
3e4093b6 7623 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7624 break;
7625 default:
7626 gcc_unreachable ();
de520661 7627 }
400fbf9f 7628
3e4093b6
RS
7629 subcode = TREE_CODE (subtype);
7630 if (array && subcode != ARRAY_TYPE)
7631 {
ea58ef42 7632 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7633 return 1;
7634 }
7635 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7636 {
ea58ef42 7637 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7638 return 1;
7639 }
d45cf215 7640
3e4093b6 7641 constructor_designated = 1;
ea58ef42 7642 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7643 return 0;
de520661 7644}
400fbf9f 7645
3e4093b6
RS
7646/* If there are range designators in designator list, push a new designator
7647 to constructor_range_stack. RANGE_END is end of such stack range or
7648 NULL_TREE if there is no range designator at this level. */
400fbf9f 7649
3e4093b6 7650static void
a1e3b3d9 7651push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7652{
7653 struct constructor_range_stack *p;
400fbf9f 7654
a1e3b3d9
LB
7655 p = (struct constructor_range_stack *)
7656 obstack_alloc (braced_init_obstack,
7657 sizeof (struct constructor_range_stack));
3e4093b6
RS
7658 p->prev = constructor_range_stack;
7659 p->next = 0;
7660 p->fields = constructor_fields;
7661 p->range_start = constructor_index;
7662 p->index = constructor_index;
7663 p->stack = constructor_stack;
7664 p->range_end = range_end;
8b6a5902 7665 if (constructor_range_stack)
3e4093b6
RS
7666 constructor_range_stack->next = p;
7667 constructor_range_stack = p;
de520661 7668}
400fbf9f 7669
3e4093b6
RS
7670/* Within an array initializer, specify the next index to be initialized.
7671 FIRST is that index. If LAST is nonzero, then initialize a range
7672 of indices, running from FIRST through LAST. */
5a7ec9d9 7673
de520661 7674void
ea58ef42 7675set_init_index (location_t loc, tree first, tree last,
d033409e 7676 struct obstack *braced_init_obstack)
de520661 7677{
ea58ef42 7678 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7679 return;
de520661 7680
b06df647 7681 designator_erroneous = 1;
de520661 7682
3ea8cd06
JM
7683 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7684 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7685 {
ea58ef42 7686 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7687 return;
7688 }
7689
2b6da65c
JM
7690 if (TREE_CODE (first) != INTEGER_CST)
7691 {
7692 first = c_fully_fold (first, false, NULL);
7693 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7694 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7695 "array index in initializer is not "
7696 "an integer constant expression");
7697 }
7698
7699 if (last && TREE_CODE (last) != INTEGER_CST)
7700 {
7701 last = c_fully_fold (last, false, NULL);
7702 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7703 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7704 "array index in initializer is not "
7705 "an integer constant expression");
7706 }
7707
3e4093b6 7708 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7709 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7710 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7711 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7712 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7713 error_init (loc, "array index in non-array initializer");
622adc7e 7714 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7715 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7716 else if (constructor_max_index
7717 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7718 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7719 else
de520661 7720 {
928c19bb
JM
7721 constant_expression_warning (first);
7722 if (last)
7723 constant_expression_warning (last);
3e4093b6 7724 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7725 if (tree_int_cst_lt (constructor_index, first))
7726 {
7727 constructor_index = copy_node (constructor_index);
7728 TREE_OVERFLOW (constructor_index) = 1;
7729 }
665f2503 7730
3e4093b6 7731 if (last)
2bede729 7732 {
3e4093b6
RS
7733 if (tree_int_cst_equal (first, last))
7734 last = 0;
7735 else if (tree_int_cst_lt (last, first))
7736 {
ea58ef42 7737 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7738 last = 0;
7739 }
7740 else
7741 {
7742 last = convert (bitsizetype, last);
7743 if (constructor_max_index != 0
7744 && tree_int_cst_lt (constructor_max_index, last))
7745 {
ea58ef42
MP
7746 error_init (loc, "array index range in initializer exceeds "
7747 "array bounds");
3e4093b6
RS
7748 last = 0;
7749 }
7750 }
2bede729 7751 }
fed3cef0 7752
3e4093b6 7753 designator_depth++;
b06df647 7754 designator_erroneous = 0;
3e4093b6 7755 if (constructor_range_stack || last)
a1e3b3d9 7756 push_range_stack (last, braced_init_obstack);
de520661 7757 }
de520661 7758}
3e4093b6
RS
7759
7760/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7761
de520661 7762void
ea58ef42
MP
7763set_init_label (location_t loc, tree fieldname,
7764 struct obstack *braced_init_obstack)
de520661 7765{
0fb96aa4 7766 tree field;
94ba5069 7767
ea58ef42 7768 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7769 return;
7770
b06df647 7771 designator_erroneous = 1;
3e4093b6
RS
7772
7773 if (TREE_CODE (constructor_type) != RECORD_TYPE
7774 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7775 {
ea58ef42 7776 error_init (loc, "field name not in record or union initializer");
3e4093b6 7777 return;
94ba5069
RS
7778 }
7779
0fb96aa4 7780 field = lookup_field (constructor_type, fieldname);
8b6a5902 7781
0fb96aa4 7782 if (field == 0)
c51a1ba9 7783 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7784 else
0fb96aa4
JM
7785 do
7786 {
7787 constructor_fields = TREE_VALUE (field);
7788 designator_depth++;
7789 designator_erroneous = 0;
7790 if (constructor_range_stack)
7791 push_range_stack (NULL_TREE, braced_init_obstack);
7792 field = TREE_CHAIN (field);
7793 if (field)
7794 {
ea58ef42 7795 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7796 return;
7797 }
7798 }
7799 while (field != NULL_TREE);
3e4093b6
RS
7800}
7801\f
7802/* Add a new initializer to the tree of pending initializers. PURPOSE
7803 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7804 VALUE is the value of that index or field. If ORIGTYPE is not
7805 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7806
7807 IMPLICIT is true if value comes from pop_init_level (1),
7808 the new initializer has been merged with the existing one
7809 and thus no warnings should be emitted about overriding an
7810 existing initializer. */
de520661 7811
3e4093b6 7812static void
96b40f8d
MP
7813add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7814 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7815{
7816 struct init_node *p, **q, *r;
7817
7818 q = &constructor_pending_elts;
7819 p = 0;
7820
7821 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7822 {
3e4093b6 7823 while (*q != 0)
91fa3c30 7824 {
3e4093b6
RS
7825 p = *q;
7826 if (tree_int_cst_lt (purpose, p->purpose))
7827 q = &p->left;
7828 else if (tree_int_cst_lt (p->purpose, purpose))
7829 q = &p->right;
7830 else
7831 {
b295aee2
JJ
7832 if (!implicit)
7833 {
7834 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7835 warning_init (loc, 0,
7836 "initialized field with side-effects "
7837 "overwritten");
b295aee2 7838 else if (warn_override_init)
96b40f8d
MP
7839 warning_init (loc, OPT_Woverride_init,
7840 "initialized field overwritten");
b295aee2 7841 }
3e4093b6 7842 p->value = value;
bbbbb16a 7843 p->origtype = origtype;
3e4093b6
RS
7844 return;
7845 }
91fa3c30 7846 }
de520661 7847 }
3e4093b6 7848 else
de520661 7849 {
3e4093b6 7850 tree bitpos;
400fbf9f 7851
3e4093b6
RS
7852 bitpos = bit_position (purpose);
7853 while (*q != NULL)
7854 {
7855 p = *q;
7856 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7857 q = &p->left;
7858 else if (p->purpose != purpose)
7859 q = &p->right;
7860 else
7861 {
b295aee2
JJ
7862 if (!implicit)
7863 {
7864 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7865 warning_init (loc, 0,
7866 "initialized field with side-effects "
7867 "overwritten");
b295aee2 7868 else if (warn_override_init)
96b40f8d
MP
7869 warning_init (loc, OPT_Woverride_init,
7870 "initialized field overwritten");
b295aee2 7871 }
3e4093b6 7872 p->value = value;
bbbbb16a 7873 p->origtype = origtype;
3e4093b6
RS
7874 return;
7875 }
7876 }
91fa3c30 7877 }
b71c7f8a 7878
a1e3b3d9
LB
7879 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7880 sizeof (struct init_node));
3e4093b6
RS
7881 r->purpose = purpose;
7882 r->value = value;
bbbbb16a 7883 r->origtype = origtype;
8b6a5902 7884
3e4093b6
RS
7885 *q = r;
7886 r->parent = p;
7887 r->left = 0;
7888 r->right = 0;
7889 r->balance = 0;
b71c7f8a 7890
3e4093b6 7891 while (p)
de520661 7892 {
3e4093b6 7893 struct init_node *s;
665f2503 7894
3e4093b6 7895 if (r == p->left)
2bede729 7896 {
3e4093b6
RS
7897 if (p->balance == 0)
7898 p->balance = -1;
7899 else if (p->balance < 0)
7900 {
7901 if (r->balance < 0)
7902 {
7903 /* L rotation. */
7904 p->left = r->right;
7905 if (p->left)
7906 p->left->parent = p;
7907 r->right = p;
e7b6a0ee 7908
3e4093b6
RS
7909 p->balance = 0;
7910 r->balance = 0;
39bc99c2 7911
3e4093b6
RS
7912 s = p->parent;
7913 p->parent = r;
7914 r->parent = s;
7915 if (s)
7916 {
7917 if (s->left == p)
7918 s->left = r;
7919 else
7920 s->right = r;
7921 }
7922 else
7923 constructor_pending_elts = r;
7924 }
7925 else
7926 {
7927 /* LR rotation. */
7928 struct init_node *t = r->right;
e7b6a0ee 7929
3e4093b6
RS
7930 r->right = t->left;
7931 if (r->right)
7932 r->right->parent = r;
7933 t->left = r;
7934
7935 p->left = t->right;
7936 if (p->left)
7937 p->left->parent = p;
7938 t->right = p;
7939
7940 p->balance = t->balance < 0;
7941 r->balance = -(t->balance > 0);
7942 t->balance = 0;
7943
7944 s = p->parent;
7945 p->parent = t;
7946 r->parent = t;
7947 t->parent = s;
7948 if (s)
7949 {
7950 if (s->left == p)
7951 s->left = t;
7952 else
7953 s->right = t;
7954 }
7955 else
7956 constructor_pending_elts = t;
7957 }
7958 break;
7959 }
7960 else
7961 {
7962 /* p->balance == +1; growth of left side balances the node. */
7963 p->balance = 0;
7964 break;
7965 }
2bede729 7966 }
3e4093b6
RS
7967 else /* r == p->right */
7968 {
7969 if (p->balance == 0)
7970 /* Growth propagation from right side. */
7971 p->balance++;
7972 else if (p->balance > 0)
7973 {
7974 if (r->balance > 0)
7975 {
7976 /* R rotation. */
7977 p->right = r->left;
7978 if (p->right)
7979 p->right->parent = p;
7980 r->left = p;
7981
7982 p->balance = 0;
7983 r->balance = 0;
7984
7985 s = p->parent;
7986 p->parent = r;
7987 r->parent = s;
7988 if (s)
7989 {
7990 if (s->left == p)
7991 s->left = r;
7992 else
7993 s->right = r;
7994 }
7995 else
7996 constructor_pending_elts = r;
7997 }
7998 else /* r->balance == -1 */
7999 {
8000 /* RL rotation */
8001 struct init_node *t = r->left;
8002
8003 r->left = t->right;
8004 if (r->left)
8005 r->left->parent = r;
8006 t->right = r;
8007
8008 p->right = t->left;
8009 if (p->right)
8010 p->right->parent = p;
8011 t->left = p;
8012
8013 r->balance = (t->balance < 0);
8014 p->balance = -(t->balance > 0);
8015 t->balance = 0;
8016
8017 s = p->parent;
8018 p->parent = t;
8019 r->parent = t;
8020 t->parent = s;
8021 if (s)
8022 {
8023 if (s->left == p)
8024 s->left = t;
8025 else
8026 s->right = t;
8027 }
8028 else
8029 constructor_pending_elts = t;
8030 }
8031 break;
8032 }
8033 else
8034 {
8035 /* p->balance == -1; growth of right side balances the node. */
8036 p->balance = 0;
8037 break;
8038 }
8039 }
8040
8041 r = p;
8042 p = p->parent;
8043 }
8044}
8045
8046/* Build AVL tree from a sorted chain. */
8047
8048static void
a1e3b3d9 8049set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8050{
4038c495
GB
8051 unsigned HOST_WIDE_INT ix;
8052 tree index, value;
3e4093b6
RS
8053
8054 if (TREE_CODE (constructor_type) != RECORD_TYPE
8055 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8056 return;
8057
4038c495 8058 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8059 add_pending_init (input_location, index, value, NULL_TREE, true,
8060 braced_init_obstack);
9771b263 8061 constructor_elements = NULL;
3e4093b6
RS
8062 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8063 {
8064 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8065 /* Skip any nameless bit fields at the beginning. */
8066 while (constructor_unfilled_fields != 0
8067 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8068 && DECL_NAME (constructor_unfilled_fields) == 0)
8069 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8070
de520661 8071 }
3e4093b6 8072 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8073 {
3e4093b6
RS
8074 if (TYPE_DOMAIN (constructor_type))
8075 constructor_unfilled_index
8076 = convert (bitsizetype,
8077 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8078 else
8079 constructor_unfilled_index = bitsize_zero_node;
de520661 8080 }
3e4093b6 8081 constructor_incremental = 0;
de520661 8082}
400fbf9f 8083
3e4093b6 8084/* Build AVL tree from a string constant. */
de520661 8085
3e4093b6 8086static void
a1e3b3d9
LB
8087set_nonincremental_init_from_string (tree str,
8088 struct obstack * braced_init_obstack)
de520661 8089{
3e4093b6
RS
8090 tree value, purpose, type;
8091 HOST_WIDE_INT val[2];
8092 const char *p, *end;
8093 int byte, wchar_bytes, charwidth, bitpos;
de520661 8094
366de0ce 8095 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8096
c466b2cd 8097 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8098 charwidth = TYPE_PRECISION (char_type_node);
8099 type = TREE_TYPE (constructor_type);
8100 p = TREE_STRING_POINTER (str);
8101 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8102
3e4093b6 8103 for (purpose = bitsize_zero_node;
8824edff
JJ
8104 p < end
8105 && !(constructor_max_index
8106 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8107 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8108 {
3e4093b6 8109 if (wchar_bytes == 1)
ffc5c6a9 8110 {
807e902e
KZ
8111 val[0] = (unsigned char) *p++;
8112 val[1] = 0;
ffc5c6a9
RH
8113 }
8114 else
3e4093b6 8115 {
3e4093b6 8116 val[1] = 0;
807e902e 8117 val[0] = 0;
3e4093b6
RS
8118 for (byte = 0; byte < wchar_bytes; byte++)
8119 {
8120 if (BYTES_BIG_ENDIAN)
8121 bitpos = (wchar_bytes - byte - 1) * charwidth;
8122 else
8123 bitpos = byte * charwidth;
807e902e 8124 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8125 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8126 << (bitpos % HOST_BITS_PER_WIDE_INT);
8127 }
8128 }
584ef5fe 8129
8df83eae 8130 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8131 {
8132 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8133 if (bitpos < HOST_BITS_PER_WIDE_INT)
8134 {
807e902e 8135 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8136 {
807e902e
KZ
8137 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8138 val[1] = -1;
3e4093b6
RS
8139 }
8140 }
8141 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8142 {
807e902e
KZ
8143 if (val[0] < 0)
8144 val[1] = -1;
3e4093b6 8145 }
807e902e 8146 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8147 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8148 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8149 << (bitpos - HOST_BITS_PER_WIDE_INT);
8150 }
ffc5c6a9 8151
807e902e
KZ
8152 value = wide_int_to_tree (type,
8153 wide_int::from_array (val, 2,
8154 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8155 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8156 braced_init_obstack);
9dfcc8db
BH
8157 }
8158
3e4093b6
RS
8159 constructor_incremental = 0;
8160}
de520661 8161
3e4093b6
RS
8162/* Return value of FIELD in pending initializer or zero if the field was
8163 not initialized yet. */
8164
8165static tree
a1e3b3d9 8166find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8167{
8168 struct init_node *p;
8169
8170 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8171 {
3e4093b6
RS
8172 if (constructor_incremental
8173 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8174 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8175
8176 p = constructor_pending_elts;
8177 while (p)
19d76e60 8178 {
3e4093b6
RS
8179 if (tree_int_cst_lt (field, p->purpose))
8180 p = p->left;
8181 else if (tree_int_cst_lt (p->purpose, field))
8182 p = p->right;
8183 else
8184 return p->value;
19d76e60 8185 }
19d76e60 8186 }
3e4093b6 8187 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8188 {
3e4093b6 8189 tree bitpos = bit_position (field);
de520661 8190
3e4093b6
RS
8191 if (constructor_incremental
8192 && (!constructor_unfilled_fields
8193 || tree_int_cst_lt (bitpos,
8194 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8195 set_nonincremental_init (braced_init_obstack);
de520661 8196
3e4093b6
RS
8197 p = constructor_pending_elts;
8198 while (p)
8199 {
8200 if (field == p->purpose)
8201 return p->value;
8202 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8203 p = p->left;
8204 else
8205 p = p->right;
8206 }
8207 }
8208 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8209 {
9771b263
DN
8210 if (!vec_safe_is_empty (constructor_elements)
8211 && (constructor_elements->last ().index == field))
8212 return constructor_elements->last ().value;
de520661 8213 }
3e4093b6 8214 return 0;
de520661
RS
8215}
8216
3e4093b6
RS
8217/* "Output" the next constructor element.
8218 At top level, really output it to assembler code now.
8219 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8220 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8221 TYPE is the data type that the containing data type wants here.
8222 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8223 If VALUE is a string constant, STRICT_STRING is true if it is
8224 unparenthesized or we should not warn here for it being parenthesized.
8225 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8226
3e4093b6
RS
8227 PENDING if non-nil means output pending elements that belong
8228 right after this element. (PENDING is normally 1;
b295aee2
JJ
8229 it is 0 while outputting pending elements, to avoid recursion.)
8230
8231 IMPLICIT is true if value comes from pop_init_level (1),
8232 the new initializer has been merged with the existing one
8233 and thus no warnings should be emitted about overriding an
8234 existing initializer. */
8b6a5902 8235
3e4093b6 8236static void
34cf811f
MP
8237output_init_element (location_t loc, tree value, tree origtype,
8238 bool strict_string, tree type, tree field, int pending,
8239 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8240{
8ce94e44 8241 tree semantic_type = NULL_TREE;
928c19bb
JM
8242 bool maybe_const = true;
8243 bool npc;
4038c495 8244
0a880880 8245 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8246 {
3e4093b6
RS
8247 constructor_erroneous = 1;
8248 return;
8b6a5902 8249 }
46bdb9cf
JM
8250 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8251 && (TREE_CODE (value) == STRING_CST
8252 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8253 && !(TREE_CODE (value) == STRING_CST
8254 && TREE_CODE (type) == ARRAY_TYPE
8255 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8256 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8257 TYPE_MAIN_VARIANT (type)))
c2255bc4 8258 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8259
3e4093b6 8260 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 8261 && require_constant_value && pending)
8b6a5902 8262 {
3e4093b6
RS
8263 /* As an extension, allow initializing objects with static storage
8264 duration with compound literals (which are then treated just as
8265 the brace enclosed list they contain). */
4435bb92
MP
8266 if (flag_isoc99)
8267 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8268 "constant");
3e4093b6
RS
8269 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8270 value = DECL_INITIAL (decl);
8b6a5902
JJ
8271 }
8272
928c19bb 8273 npc = null_pointer_constant_p (value);
8ce94e44
JM
8274 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8275 {
8276 semantic_type = TREE_TYPE (value);
8277 value = TREE_OPERAND (value, 0);
8278 }
928c19bb
JM
8279 value = c_fully_fold (value, require_constant_value, &maybe_const);
8280
3e4093b6
RS
8281 if (value == error_mark_node)
8282 constructor_erroneous = 1;
8283 else if (!TREE_CONSTANT (value))
8284 constructor_constant = 0;
116df786 8285 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8286 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8287 || TREE_CODE (constructor_type) == UNION_TYPE)
8288 && DECL_C_BIT_FIELD (field)
8289 && TREE_CODE (value) != INTEGER_CST))
8290 constructor_simple = 0;
928c19bb
JM
8291 if (!maybe_const)
8292 constructor_nonconst = 1;
3e4093b6 8293
116df786 8294 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8295 {
116df786
RH
8296 if (require_constant_value)
8297 {
ea58ef42 8298 error_init (loc, "initializer element is not constant");
116df786
RH
8299 value = error_mark_node;
8300 }
8301 else if (require_constant_elements)
8337d1db 8302 pedwarn (loc, OPT_Wpedantic,
509c9d60 8303 "initializer element is not computable at load time");
8b6a5902 8304 }
928c19bb
JM
8305 else if (!maybe_const
8306 && (require_constant_value || require_constant_elements))
8337d1db 8307 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8308 "initializer element is not a constant expression");
3e4093b6 8309
81f40b79
ILT
8310 /* Issue -Wc++-compat warnings about initializing a bitfield with
8311 enum type. */
8312 if (warn_cxx_compat
8313 && field != NULL_TREE
8314 && TREE_CODE (field) == FIELD_DECL
8315 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8316 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8317 != TYPE_MAIN_VARIANT (type))
8318 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8319 {
8320 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8321 if (checktype != error_mark_node
8322 && (TYPE_MAIN_VARIANT (checktype)
8323 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8324 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8325 "enum conversion in initialization is invalid in C++");
8326 }
8327
3e4093b6
RS
8328 /* If this field is empty (and not at the end of structure),
8329 don't do anything other than checking the initializer. */
8330 if (field
8331 && (TREE_TYPE (field) == error_mark_node
8332 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8333 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8334 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8335 || DECL_CHAIN (field)))))
3e4093b6
RS
8336 return;
8337
8ce94e44
JM
8338 if (semantic_type)
8339 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8340 value = digest_init (loc, type, value, origtype, npc, strict_string,
8341 require_constant_value);
3e4093b6 8342 if (value == error_mark_node)
8b6a5902 8343 {
3e4093b6
RS
8344 constructor_erroneous = 1;
8345 return;
8b6a5902 8346 }
928c19bb
JM
8347 if (require_constant_value || require_constant_elements)
8348 constant_expression_warning (value);
8b6a5902 8349
3e4093b6
RS
8350 /* If this element doesn't come next in sequence,
8351 put it on constructor_pending_elts. */
8352 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8353 && (!constructor_incremental
8354 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8355 {
3e4093b6
RS
8356 if (constructor_incremental
8357 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8358 set_nonincremental_init (braced_init_obstack);
3e4093b6 8359
96b40f8d 8360 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8361 braced_init_obstack);
3e4093b6 8362 return;
8b6a5902 8363 }
3e4093b6
RS
8364 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8365 && (!constructor_incremental
8366 || field != constructor_unfilled_fields))
8b6a5902 8367 {
3e4093b6
RS
8368 /* We do this for records but not for unions. In a union,
8369 no matter which field is specified, it can be initialized
8370 right away since it starts at the beginning of the union. */
8371 if (constructor_incremental)
8372 {
8373 if (!constructor_unfilled_fields)
a1e3b3d9 8374 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8375 else
8376 {
8377 tree bitpos, unfillpos;
8378
8379 bitpos = bit_position (field);
8380 unfillpos = bit_position (constructor_unfilled_fields);
8381
8382 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8383 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8384 }
8385 }
8386
96b40f8d 8387 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8388 braced_init_obstack);
3e4093b6 8389 return;
8b6a5902 8390 }
3e4093b6 8391 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8392 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8393 {
b295aee2
JJ
8394 if (!implicit)
8395 {
9771b263 8396 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
96b40f8d 8397 warning_init (loc, 0,
b295aee2
JJ
8398 "initialized field with side-effects overwritten");
8399 else if (warn_override_init)
96b40f8d
MP
8400 warning_init (loc, OPT_Woverride_init,
8401 "initialized field overwritten");
b295aee2 8402 }
8b6a5902 8403
3e4093b6 8404 /* We can have just one union field set. */
9771b263 8405 constructor_elements = NULL;
3e4093b6 8406 }
8b6a5902 8407
3e4093b6
RS
8408 /* Otherwise, output this element either to
8409 constructor_elements or to the assembler file. */
8b6a5902 8410
f32682ca 8411 constructor_elt celt = {field, value};
9771b263 8412 vec_safe_push (constructor_elements, celt);
8b6a5902 8413
3e4093b6
RS
8414 /* Advance the variable that indicates sequential elements output. */
8415 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8416 constructor_unfilled_index
db3927fb
AH
8417 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8418 bitsize_one_node);
3e4093b6
RS
8419 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8420 {
8421 constructor_unfilled_fields
910ad8de 8422 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8423
3e4093b6
RS
8424 /* Skip any nameless bit fields. */
8425 while (constructor_unfilled_fields != 0
8426 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8427 && DECL_NAME (constructor_unfilled_fields) == 0)
8428 constructor_unfilled_fields =
910ad8de 8429 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8430 }
8431 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8432 constructor_unfilled_fields = 0;
de520661 8433
3e4093b6
RS
8434 /* Now output any pending elements which have become next. */
8435 if (pending)
a1e3b3d9 8436 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8437}
8b6a5902 8438
3e4093b6
RS
8439/* Output any pending elements which have become next.
8440 As we output elements, constructor_unfilled_{fields,index}
8441 advances, which may cause other elements to become next;
8442 if so, they too are output.
8b6a5902 8443
3e4093b6
RS
8444 If ALL is 0, we return when there are
8445 no more pending elements to output now.
665f2503 8446
3e4093b6
RS
8447 If ALL is 1, we output space as necessary so that
8448 we can output all the pending elements. */
3e4093b6 8449static void
a1e3b3d9 8450output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8451{
8452 struct init_node *elt = constructor_pending_elts;
8453 tree next;
de520661 8454
3e4093b6
RS
8455 retry:
8456
ba228239 8457 /* Look through the whole pending tree.
3e4093b6
RS
8458 If we find an element that should be output now,
8459 output it. Otherwise, set NEXT to the element
8460 that comes first among those still pending. */
8461
8462 next = 0;
8463 while (elt)
8464 {
8465 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8466 {
3e4093b6
RS
8467 if (tree_int_cst_equal (elt->purpose,
8468 constructor_unfilled_index))
34cf811f
MP
8469 output_init_element (input_location, elt->value, elt->origtype,
8470 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8471 constructor_unfilled_index, 0, false,
8472 braced_init_obstack);
3e4093b6
RS
8473 else if (tree_int_cst_lt (constructor_unfilled_index,
8474 elt->purpose))
8b6a5902 8475 {
3e4093b6
RS
8476 /* Advance to the next smaller node. */
8477 if (elt->left)
8478 elt = elt->left;
8479 else
8480 {
8481 /* We have reached the smallest node bigger than the
8482 current unfilled index. Fill the space first. */
8483 next = elt->purpose;
8484 break;
8485 }
8b6a5902 8486 }
ce662d4c
JJ
8487 else
8488 {
3e4093b6
RS
8489 /* Advance to the next bigger node. */
8490 if (elt->right)
8491 elt = elt->right;
8492 else
ce662d4c 8493 {
3e4093b6
RS
8494 /* We have reached the biggest node in a subtree. Find
8495 the parent of it, which is the next bigger node. */
8496 while (elt->parent && elt->parent->right == elt)
8497 elt = elt->parent;
8498 elt = elt->parent;
8499 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8500 elt->purpose))
8501 {
8502 next = elt->purpose;
8503 break;
8504 }
ce662d4c
JJ
8505 }
8506 }
8b6a5902 8507 }
3e4093b6
RS
8508 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8509 || TREE_CODE (constructor_type) == UNION_TYPE)
8510 {
8511 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8512
3e4093b6
RS
8513 /* If the current record is complete we are done. */
8514 if (constructor_unfilled_fields == 0)
8515 break;
de520661 8516
3e4093b6
RS
8517 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8518 elt_bitpos = bit_position (elt->purpose);
8519 /* We can't compare fields here because there might be empty
8520 fields in between. */
8521 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8522 {
8523 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8524 output_init_element (input_location, elt->value, elt->origtype,
8525 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8526 elt->purpose, 0, false,
8527 braced_init_obstack);
3e4093b6
RS
8528 }
8529 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8530 {
8531 /* Advance to the next smaller node. */
8532 if (elt->left)
8533 elt = elt->left;
8534 else
8535 {
8536 /* We have reached the smallest node bigger than the
8537 current unfilled field. Fill the space first. */
8538 next = elt->purpose;
8539 break;
8540 }
8541 }
8542 else
8543 {
8544 /* Advance to the next bigger node. */
8545 if (elt->right)
8546 elt = elt->right;
8547 else
8548 {
8549 /* We have reached the biggest node in a subtree. Find
8550 the parent of it, which is the next bigger node. */
8551 while (elt->parent && elt->parent->right == elt)
8552 elt = elt->parent;
8553 elt = elt->parent;
8554 if (elt
8555 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8556 bit_position (elt->purpose))))
8557 {
8558 next = elt->purpose;
8559 break;
8560 }
8561 }
8562 }
8563 }
8564 }
de520661 8565
3e4093b6
RS
8566 /* Ordinarily return, but not if we want to output all
8567 and there are elements left. */
3f75a254 8568 if (!(all && next != 0))
e5cfb88f
RK
8569 return;
8570
3e4093b6
RS
8571 /* If it's not incremental, just skip over the gap, so that after
8572 jumping to retry we will output the next successive element. */
8573 if (TREE_CODE (constructor_type) == RECORD_TYPE
8574 || TREE_CODE (constructor_type) == UNION_TYPE)
8575 constructor_unfilled_fields = next;
8576 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8577 constructor_unfilled_index = next;
de520661 8578
3e4093b6
RS
8579 /* ELT now points to the node in the pending tree with the next
8580 initializer to output. */
8581 goto retry;
de520661
RS
8582}
8583\f
3e4093b6
RS
8584/* Add one non-braced element to the current constructor level.
8585 This adjusts the current position within the constructor's type.
8586 This may also start or terminate implicit levels
8587 to handle a partly-braced initializer.
e5e809f4 8588
3e4093b6 8589 Once this has found the correct level for the new element,
b295aee2
JJ
8590 it calls output_init_element.
8591
8592 IMPLICIT is true if value comes from pop_init_level (1),
8593 the new initializer has been merged with the existing one
8594 and thus no warnings should be emitted about overriding an
8595 existing initializer. */
3e4093b6
RS
8596
8597void
34cf811f 8598process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8599 struct obstack * braced_init_obstack)
e5e809f4 8600{
916c5919
JM
8601 tree orig_value = value.value;
8602 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8603 bool strict_string = value.original_code == STRING_CST;
340baef7 8604 bool was_designated = designator_depth != 0;
e5e809f4 8605
3e4093b6 8606 designator_depth = 0;
b06df647 8607 designator_erroneous = 0;
e5e809f4 8608
9bac5cbb
G
8609 if (!implicit && value.value && !integer_zerop (value.value))
8610 constructor_zeroinit = 0;
8611
3e4093b6
RS
8612 /* Handle superfluous braces around string cst as in
8613 char x[] = {"foo"}; */
8614 if (string_flag
8615 && constructor_type
340baef7 8616 && !was_designated
3e4093b6 8617 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8618 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8619 && integer_zerop (constructor_unfilled_index))
e5e809f4 8620 {
916c5919 8621 if (constructor_stack->replacement_value.value)
ea58ef42 8622 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8623 constructor_stack->replacement_value = value;
8624 return;
e5e809f4 8625 }
8b6a5902 8626
916c5919 8627 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8628 {
ea58ef42 8629 error_init (loc, "excess elements in struct initializer");
3e4093b6 8630 return;
e5e809f4
JL
8631 }
8632
3e4093b6
RS
8633 /* Ignore elements of a brace group if it is entirely superfluous
8634 and has already been diagnosed. */
8635 if (constructor_type == 0)
8636 return;
e5e809f4 8637
976d5a22
TT
8638 if (!implicit && warn_designated_init && !was_designated
8639 && TREE_CODE (constructor_type) == RECORD_TYPE
8640 && lookup_attribute ("designated_init",
8641 TYPE_ATTRIBUTES (constructor_type)))
8642 warning_init (loc,
8643 OPT_Wdesignated_init,
8644 "positional initialization of field "
8645 "in %<struct%> declared with %<designated_init%> attribute");
8646
3e4093b6
RS
8647 /* If we've exhausted any levels that didn't have braces,
8648 pop them now. */
8649 while (constructor_stack->implicit)
8650 {
8651 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8652 || TREE_CODE (constructor_type) == UNION_TYPE)
8653 && constructor_fields == 0)
ea58ef42
MP
8654 process_init_element (loc,
8655 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8656 true, braced_init_obstack);
53650abe
AP
8657 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8658 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8659 && constructor_max_index
8660 && tree_int_cst_lt (constructor_max_index,
8661 constructor_index))
ea58ef42
MP
8662 process_init_element (loc,
8663 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8664 true, braced_init_obstack);
3e4093b6
RS
8665 else
8666 break;
8667 }
e5e809f4 8668
3e4093b6
RS
8669 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8670 if (constructor_range_stack)
e5e809f4 8671 {
3e4093b6
RS
8672 /* If value is a compound literal and we'll be just using its
8673 content, don't put it into a SAVE_EXPR. */
916c5919 8674 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8675 || !require_constant_value
8676 || flag_isoc99)
8ce94e44
JM
8677 {
8678 tree semantic_type = NULL_TREE;
8679 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8680 {
8681 semantic_type = TREE_TYPE (value.value);
8682 value.value = TREE_OPERAND (value.value, 0);
8683 }
8684 value.value = c_save_expr (value.value);
8685 if (semantic_type)
8686 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8687 value.value);
8688 }
3e4093b6 8689 }
e5e809f4 8690
3e4093b6
RS
8691 while (1)
8692 {
8693 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8694 {
3e4093b6
RS
8695 tree fieldtype;
8696 enum tree_code fieldcode;
e5e809f4 8697
3e4093b6
RS
8698 if (constructor_fields == 0)
8699 {
ea58ef42 8700 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8701 break;
8702 }
e5e809f4 8703
3e4093b6
RS
8704 fieldtype = TREE_TYPE (constructor_fields);
8705 if (fieldtype != error_mark_node)
8706 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8707 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8708
3e4093b6
RS
8709 /* Error for non-static initialization of a flexible array member. */
8710 if (fieldcode == ARRAY_TYPE
8711 && !require_constant_value
8712 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8713 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8714 {
ea58ef42
MP
8715 error_init (loc, "non-static initialization of a flexible "
8716 "array member");
3e4093b6
RS
8717 break;
8718 }
e5e809f4 8719
3e4093b6 8720 /* Accept a string constant to initialize a subarray. */
916c5919 8721 if (value.value != 0
3e4093b6 8722 && fieldcode == ARRAY_TYPE
197463ae 8723 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8724 && string_flag)
916c5919 8725 value.value = orig_value;
3e4093b6
RS
8726 /* Otherwise, if we have come to a subaggregate,
8727 and we don't have an element of its type, push into it. */
0953878d 8728 else if (value.value != 0
916c5919
JM
8729 && value.value != error_mark_node
8730 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8731 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8732 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8733 {
ea58ef42 8734 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8735 continue;
8736 }
e5e809f4 8737
916c5919 8738 if (value.value)
3e4093b6
RS
8739 {
8740 push_member_name (constructor_fields);
34cf811f 8741 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8742 strict_string, fieldtype,
a1e3b3d9
LB
8743 constructor_fields, 1, implicit,
8744 braced_init_obstack);
3e4093b6 8745 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8746 }
8747 else
3e4093b6
RS
8748 /* Do the bookkeeping for an element that was
8749 directly output as a constructor. */
e5e809f4 8750 {
3e4093b6
RS
8751 /* For a record, keep track of end position of last field. */
8752 if (DECL_SIZE (constructor_fields))
c22cacf3 8753 constructor_bit_index
db3927fb
AH
8754 = size_binop_loc (input_location, PLUS_EXPR,
8755 bit_position (constructor_fields),
8756 DECL_SIZE (constructor_fields));
3e4093b6
RS
8757
8758 /* If the current field was the first one not yet written out,
8759 it isn't now, so update. */
8760 if (constructor_unfilled_fields == constructor_fields)
8761 {
910ad8de 8762 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8763 /* Skip any nameless bit fields. */
8764 while (constructor_unfilled_fields != 0
8765 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8766 && DECL_NAME (constructor_unfilled_fields) == 0)
8767 constructor_unfilled_fields =
910ad8de 8768 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8769 }
e5e809f4 8770 }
3e4093b6 8771
910ad8de 8772 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8773 /* Skip any nameless bit fields at the beginning. */
8774 while (constructor_fields != 0
8775 && DECL_C_BIT_FIELD (constructor_fields)
8776 && DECL_NAME (constructor_fields) == 0)
910ad8de 8777 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8778 }
3e4093b6 8779 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8780 {
3e4093b6
RS
8781 tree fieldtype;
8782 enum tree_code fieldcode;
e5e809f4 8783
3e4093b6
RS
8784 if (constructor_fields == 0)
8785 {
d033409e 8786 pedwarn_init (loc, 0,
509c9d60 8787 "excess elements in union initializer");
3e4093b6
RS
8788 break;
8789 }
e5e809f4 8790
3e4093b6
RS
8791 fieldtype = TREE_TYPE (constructor_fields);
8792 if (fieldtype != error_mark_node)
8793 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8794 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8795
3e4093b6
RS
8796 /* Warn that traditional C rejects initialization of unions.
8797 We skip the warning if the value is zero. This is done
8798 under the assumption that the zero initializer in user
8799 code appears conditioned on e.g. __STDC__ to avoid
8800 "missing initializer" warnings and relies on default
8801 initialization to zero in the traditional C case.
8802 We also skip the warning if the initializer is designated,
8803 again on the assumption that this must be conditional on
8804 __STDC__ anyway (and we've already complained about the
8805 member-designator already). */
8400e75e 8806 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8807 && !(value.value && (integer_zerop (value.value)
8808 || real_zerop (value.value))))
3176a0c2
DD
8809 warning (OPT_Wtraditional, "traditional C rejects initialization "
8810 "of unions");
e5e809f4 8811
3e4093b6 8812 /* Accept a string constant to initialize a subarray. */
916c5919 8813 if (value.value != 0
3e4093b6 8814 && fieldcode == ARRAY_TYPE
197463ae 8815 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8816 && string_flag)
916c5919 8817 value.value = orig_value;
3e4093b6
RS
8818 /* Otherwise, if we have come to a subaggregate,
8819 and we don't have an element of its type, push into it. */
0953878d 8820 else if (value.value != 0
916c5919
JM
8821 && value.value != error_mark_node
8822 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8823 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8824 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8825 {
ea58ef42 8826 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8827 continue;
8828 }
e5e809f4 8829
916c5919 8830 if (value.value)
3e4093b6
RS
8831 {
8832 push_member_name (constructor_fields);
34cf811f 8833 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8834 strict_string, fieldtype,
a1e3b3d9
LB
8835 constructor_fields, 1, implicit,
8836 braced_init_obstack);
3e4093b6 8837 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8838 }
8839 else
3e4093b6
RS
8840 /* Do the bookkeeping for an element that was
8841 directly output as a constructor. */
e5e809f4 8842 {
3e4093b6 8843 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8844 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8845 }
e5e809f4 8846
3e4093b6
RS
8847 constructor_fields = 0;
8848 }
8849 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8850 {
8851 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8852 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8853
3e4093b6 8854 /* Accept a string constant to initialize a subarray. */
916c5919 8855 if (value.value != 0
3e4093b6 8856 && eltcode == ARRAY_TYPE
197463ae 8857 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8858 && string_flag)
916c5919 8859 value.value = orig_value;
3e4093b6
RS
8860 /* Otherwise, if we have come to a subaggregate,
8861 and we don't have an element of its type, push into it. */
0953878d 8862 else if (value.value != 0
916c5919
JM
8863 && value.value != error_mark_node
8864 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8865 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8866 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8867 {
ea58ef42 8868 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8869 continue;
8870 }
8b6a5902 8871
3e4093b6
RS
8872 if (constructor_max_index != 0
8873 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8874 || integer_all_onesp (constructor_max_index)))
8875 {
d033409e 8876 pedwarn_init (loc, 0,
509c9d60 8877 "excess elements in array initializer");
3e4093b6
RS
8878 break;
8879 }
8b6a5902 8880
3e4093b6 8881 /* Now output the actual element. */
916c5919 8882 if (value.value)
3e4093b6 8883 {
ae7e9ddd 8884 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 8885 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8886 strict_string, elttype,
a1e3b3d9
LB
8887 constructor_index, 1, implicit,
8888 braced_init_obstack);
3e4093b6
RS
8889 RESTORE_SPELLING_DEPTH (constructor_depth);
8890 }
2f6e4e97 8891
3e4093b6 8892 constructor_index
db3927fb
AH
8893 = size_binop_loc (input_location, PLUS_EXPR,
8894 constructor_index, bitsize_one_node);
8b6a5902 8895
916c5919 8896 if (!value.value)
3e4093b6
RS
8897 /* If we are doing the bookkeeping for an element that was
8898 directly output as a constructor, we must update
8899 constructor_unfilled_index. */
8900 constructor_unfilled_index = constructor_index;
8901 }
8902 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8903 {
8904 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8905
c22cacf3
MS
8906 /* Do a basic check of initializer size. Note that vectors
8907 always have a fixed size derived from their type. */
3e4093b6
RS
8908 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8909 {
d033409e 8910 pedwarn_init (loc, 0,
509c9d60 8911 "excess elements in vector initializer");
3e4093b6
RS
8912 break;
8913 }
8b6a5902 8914
3e4093b6 8915 /* Now output the actual element. */
916c5919 8916 if (value.value)
53650abe
AP
8917 {
8918 if (TREE_CODE (value.value) == VECTOR_CST)
8919 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 8920 output_init_element (loc, value.value, value.original_type,
53650abe 8921 strict_string, elttype,
a1e3b3d9
LB
8922 constructor_index, 1, implicit,
8923 braced_init_obstack);
53650abe 8924 }
8b6a5902 8925
3e4093b6 8926 constructor_index
db3927fb
AH
8927 = size_binop_loc (input_location,
8928 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8929
916c5919 8930 if (!value.value)
3e4093b6
RS
8931 /* If we are doing the bookkeeping for an element that was
8932 directly output as a constructor, we must update
8933 constructor_unfilled_index. */
8934 constructor_unfilled_index = constructor_index;
8935 }
8b6a5902 8936
3e4093b6
RS
8937 /* Handle the sole element allowed in a braced initializer
8938 for a scalar variable. */
b4519d39
SB
8939 else if (constructor_type != error_mark_node
8940 && constructor_fields == 0)
8b6a5902 8941 {
d033409e 8942 pedwarn_init (loc, 0,
509c9d60 8943 "excess elements in scalar initializer");
3e4093b6 8944 break;
8b6a5902
JJ
8945 }
8946 else
8947 {
916c5919 8948 if (value.value)
34cf811f 8949 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8950 strict_string, constructor_type,
a1e3b3d9
LB
8951 NULL_TREE, 1, implicit,
8952 braced_init_obstack);
3e4093b6 8953 constructor_fields = 0;
8b6a5902
JJ
8954 }
8955
3e4093b6
RS
8956 /* Handle range initializers either at this level or anywhere higher
8957 in the designator stack. */
8958 if (constructor_range_stack)
8b6a5902 8959 {
3e4093b6
RS
8960 struct constructor_range_stack *p, *range_stack;
8961 int finish = 0;
8962
8963 range_stack = constructor_range_stack;
8964 constructor_range_stack = 0;
8965 while (constructor_stack != range_stack->stack)
8b6a5902 8966 {
366de0ce 8967 gcc_assert (constructor_stack->implicit);
34cf811f 8968 process_init_element (loc,
ea58ef42
MP
8969 pop_init_level (loc, 1,
8970 braced_init_obstack),
a1e3b3d9 8971 true, braced_init_obstack);
8b6a5902 8972 }
3e4093b6
RS
8973 for (p = range_stack;
8974 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8975 p = p->prev)
8b6a5902 8976 {
366de0ce 8977 gcc_assert (constructor_stack->implicit);
34cf811f 8978 process_init_element (loc,
ea58ef42
MP
8979 pop_init_level (loc, 1,
8980 braced_init_obstack),
a1e3b3d9 8981 true, braced_init_obstack);
8b6a5902 8982 }
3e4093b6 8983
db3927fb
AH
8984 p->index = size_binop_loc (input_location,
8985 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8986 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8987 finish = 1;
8988
8989 while (1)
8990 {
8991 constructor_index = p->index;
8992 constructor_fields = p->fields;
8993 if (finish && p->range_end && p->index == p->range_start)
8994 {
8995 finish = 0;
8996 p->prev = 0;
8997 }
8998 p = p->next;
8999 if (!p)
9000 break;
ea58ef42 9001 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
9002 p->stack = constructor_stack;
9003 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9004 p->index = p->range_start;
9005 }
9006
9007 if (!finish)
9008 constructor_range_stack = range_stack;
9009 continue;
8b6a5902
JJ
9010 }
9011
3e4093b6 9012 break;
8b6a5902
JJ
9013 }
9014
3e4093b6
RS
9015 constructor_range_stack = 0;
9016}
9017\f
9f0e2d86
ZW
9018/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9019 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9020 an ASM_EXPR node). */
3e4093b6 9021tree
9f0e2d86 9022build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9023{
6de9cd9a
DN
9024 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9025 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9026 return add_stmt (args);
8b6a5902
JJ
9027}
9028
9f0e2d86
ZW
9029/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9030 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9031 SIMPLE indicates whether there was anything at all after the
9032 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9033 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9034tree
c2255bc4 9035build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9036 tree clobbers, tree labels, bool simple)
e5e809f4 9037{
3e4093b6 9038 tree tail;
9f0e2d86 9039 tree args;
6de9cd9a
DN
9040 int i;
9041 const char *constraint;
74f0c611 9042 const char **oconstraints;
6de9cd9a 9043 bool allows_mem, allows_reg, is_inout;
74f0c611 9044 int ninputs, noutputs;
6de9cd9a
DN
9045
9046 ninputs = list_length (inputs);
9047 noutputs = list_length (outputs);
74f0c611
RH
9048 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9049
1c384bf1 9050 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9051
6de9cd9a
DN
9052 /* Remove output conversions that change the type but not the mode. */
9053 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9054 {
3e4093b6 9055 tree output = TREE_VALUE (tail);
74f0c611 9056
eadd3d0d
JJ
9057 output = c_fully_fold (output, false, NULL);
9058
74f0c611
RH
9059 /* ??? Really, this should not be here. Users should be using a
9060 proper lvalue, dammit. But there's a long history of using casts
9061 in the output operands. In cases like longlong.h, this becomes a
9062 primitive form of typechecking -- if the cast can be removed, then
9063 the output operand had a type of the proper width; otherwise we'll
9064 get an error. Gross, but ... */
3e4093b6 9065 STRIP_NOPS (output);
74f0c611 9066
7bd11157 9067 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9068 output = error_mark_node;
8b6a5902 9069
5544530a
PB
9070 if (output != error_mark_node
9071 && (TREE_READONLY (output)
9072 || TYPE_READONLY (TREE_TYPE (output))
9073 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9074 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9075 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9076 readonly_error (loc, output, lv_asm);
5544530a 9077
6de9cd9a 9078 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9079 oconstraints[i] = constraint;
9080
9081 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9082 &allows_mem, &allows_reg, &is_inout))
9083 {
9084 /* If the operand is going to end up in memory,
9085 mark it addressable. */
9086 if (!allows_reg && !c_mark_addressable (output))
9087 output = error_mark_node;
bae5cddf
JJ
9088 if (!(!allows_reg && allows_mem)
9089 && output != error_mark_node
9090 && VOID_TYPE_P (TREE_TYPE (output)))
9091 {
9092 error_at (loc, "invalid use of void expression");
9093 output = error_mark_node;
9094 }
74f0c611
RH
9095 }
9096 else
c22cacf3 9097 output = error_mark_node;
3e4093b6 9098
74f0c611 9099 TREE_VALUE (tail) = output;
8b6a5902 9100 }
3e4093b6 9101
74f0c611
RH
9102 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9103 {
9104 tree input;
9105
9106 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9107 input = TREE_VALUE (tail);
9108
74f0c611
RH
9109 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9110 oconstraints, &allows_mem, &allows_reg))
9111 {
9112 /* If the operand is going to end up in memory,
9113 mark it addressable. */
b4c33883
AP
9114 if (!allows_reg && allows_mem)
9115 {
eadd3d0d
JJ
9116 input = c_fully_fold (input, false, NULL);
9117
b4c33883
AP
9118 /* Strip the nops as we allow this case. FIXME, this really
9119 should be rejected or made deprecated. */
9120 STRIP_NOPS (input);
9121 if (!c_mark_addressable (input))
9122 input = error_mark_node;
bae5cddf 9123 }
eadd3d0d 9124 else
bae5cddf 9125 {
eadd3d0d
JJ
9126 struct c_expr expr;
9127 memset (&expr, 0, sizeof (expr));
9128 expr.value = input;
267bac10 9129 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9130 input = c_fully_fold (expr.value, false, NULL);
9131
9132 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9133 {
9134 error_at (loc, "invalid use of void expression");
9135 input = error_mark_node;
9136 }
bae5cddf 9137 }
74f0c611
RH
9138 }
9139 else
9140 input = error_mark_node;
9141
9142 TREE_VALUE (tail) = input;
9143 }
3e4093b6 9144
1c384bf1
RH
9145 /* ASMs with labels cannot have outputs. This should have been
9146 enforced by the parser. */
9147 gcc_assert (outputs == NULL || labels == NULL);
9148
9149 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9150
5544530a
PB
9151 /* asm statements without outputs, including simple ones, are treated
9152 as volatile. */
9153 ASM_INPUT_P (args) = simple;
9154 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9155
9f0e2d86 9156 return args;
e5e809f4 9157}
3e4093b6 9158\f
c2255bc4
AH
9159/* Generate a goto statement to LABEL. LOC is the location of the
9160 GOTO. */
506e2710
RH
9161
9162tree
c2255bc4 9163c_finish_goto_label (location_t loc, tree label)
506e2710 9164{
e1b7793c 9165 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9166 if (!decl)
9167 return NULL_TREE;
506e2710 9168 TREE_USED (decl) = 1;
c2255bc4
AH
9169 {
9170 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9171 SET_EXPR_LOCATION (t, loc);
9172 return add_stmt (t);
9173 }
506e2710
RH
9174}
9175
c2255bc4
AH
9176/* Generate a computed goto statement to EXPR. LOC is the location of
9177 the GOTO. */
506e2710
RH
9178
9179tree
c2255bc4 9180c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9181{
c2255bc4 9182 tree t;
c1771a20 9183 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9184 expr = c_fully_fold (expr, false, NULL);
506e2710 9185 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9186 t = build1 (GOTO_EXPR, void_type_node, expr);
9187 SET_EXPR_LOCATION (t, loc);
9188 return add_stmt (t);
506e2710
RH
9189}
9190
5088b058 9191/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 9192 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
9193 the location of the return statement, or the location of the expression,
9194 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 9195 is the original type of RETVAL. */
de520661 9196
506e2710 9197tree
c2255bc4 9198c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9199{
0c9b182b
JJ
9200 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9201 bool no_warning = false;
928c19bb 9202 bool npc = false;
36536d79 9203 size_t rank = 0;
3e4093b6
RS
9204
9205 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9206 warning_at (loc, 0,
9207 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9208
b72271b9 9209 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9210 {
9211 /* Array notations are allowed in a return statement if it is inside a
9212 built-in array notation reduction function. */
9213 if (!find_rank (loc, retval, retval, false, &rank))
9214 return error_mark_node;
9215 if (rank >= 1)
9216 {
9217 error_at (loc, "array notation expression cannot be used as a "
9218 "return value");
9219 return error_mark_node;
9220 }
9221 }
3af9c5e9 9222 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9223 {
9224 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9225 "allowed");
9226 return error_mark_node;
9227 }
928c19bb
JM
9228 if (retval)
9229 {
8ce94e44 9230 tree semantic_type = NULL_TREE;
928c19bb 9231 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9232 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9233 {
9234 semantic_type = TREE_TYPE (retval);
9235 retval = TREE_OPERAND (retval, 0);
9236 }
928c19bb 9237 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9238 if (semantic_type)
9239 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9240 }
9241
3e4093b6 9242 if (!retval)
de520661 9243 {
3e4093b6
RS
9244 current_function_returns_null = 1;
9245 if ((warn_return_type || flag_isoc99)
9246 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9247 {
35aff4fb
MP
9248 if (flag_isoc99)
9249 pedwarn (loc, 0, "%<return%> with no value, in "
9250 "function returning non-void");
9251 else
9252 warning_at (loc, OPT_Wreturn_type, "%<return%> with no value, "
9253 "in function returning non-void");
0c9b182b
JJ
9254 no_warning = true;
9255 }
400fbf9f 9256 }
3e4093b6 9257 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9258 {
3e4093b6 9259 current_function_returns_null = 1;
2397c575 9260 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9261 pedwarn (loc, 0,
509c9d60 9262 "%<return%> with a value, in function returning void");
b8698a0f 9263 else
c1771a20 9264 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9265 "%<return%> with expression, in function returning void");
de520661 9266 }
3e4093b6 9267 else
de520661 9268 {
68fca595
MP
9269 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9270 retval, origtype, ic_return,
c2255bc4 9271 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9272 tree res = DECL_RESULT (current_function_decl);
9273 tree inner;
9feb29df 9274 bool save;
3e4093b6
RS
9275
9276 current_function_returns_value = 1;
9277 if (t == error_mark_node)
506e2710 9278 return NULL_TREE;
3e4093b6 9279
9feb29df
JJ
9280 save = in_late_binary_op;
9281 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9282 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9283 in_late_binary_op = true;
3e4093b6 9284 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9285 in_late_binary_op = save;
3e4093b6
RS
9286
9287 /* Strip any conversions, additions, and subtractions, and see if
9288 we are returning the address of a local variable. Warn if so. */
9289 while (1)
8b6a5902 9290 {
3e4093b6 9291 switch (TREE_CODE (inner))
8b6a5902 9292 {
849421a3
JJ
9293 CASE_CONVERT:
9294 case NON_LVALUE_EXPR:
3e4093b6 9295 case PLUS_EXPR:
849421a3 9296 case POINTER_PLUS_EXPR:
3e4093b6
RS
9297 inner = TREE_OPERAND (inner, 0);
9298 continue;
9299
9300 case MINUS_EXPR:
9301 /* If the second operand of the MINUS_EXPR has a pointer
9302 type (or is converted from it), this may be valid, so
9303 don't give a warning. */
9304 {
9305 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9306
3f75a254 9307 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9308 && (CONVERT_EXPR_P (op1)
9309 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9310 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9311
3e4093b6
RS
9312 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9313 break;
8b6a5902 9314
3e4093b6
RS
9315 inner = TREE_OPERAND (inner, 0);
9316 continue;
9317 }
400fbf9f 9318
3e4093b6
RS
9319 case ADDR_EXPR:
9320 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9321
6615c446 9322 while (REFERENCE_CLASS_P (inner)
c22cacf3 9323 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9324 inner = TREE_OPERAND (inner, 0);
8b6a5902 9325
a2f1f4c3 9326 if (DECL_P (inner)
3f75a254
JM
9327 && !DECL_EXTERNAL (inner)
9328 && !TREE_STATIC (inner)
3e4093b6 9329 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9330 {
9331 if (TREE_CODE (inner) == LABEL_DECL)
9332 warning_at (loc, OPT_Wreturn_local_addr,
9333 "function returns address of label");
9334 else
b4dfdc11
MG
9335 {
9336 warning_at (loc, OPT_Wreturn_local_addr,
9337 "function returns address of local variable");
9338 tree zero = build_zero_cst (TREE_TYPE (res));
9339 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9340 }
19fc9faa 9341 }
3e4093b6 9342 break;
8b6a5902 9343
3e4093b6
RS
9344 default:
9345 break;
9346 }
de520661 9347
3e4093b6
RS
9348 break;
9349 }
9350
53fb4de3 9351 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9352 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9353
9354 if (warn_sequence_point)
9355 verify_sequence_points (retval);
de520661 9356 }
8b6a5902 9357
c2255bc4 9358 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9359 TREE_NO_WARNING (ret_stmt) |= no_warning;
9360 return add_stmt (ret_stmt);
de520661 9361}
3e4093b6
RS
9362\f
9363struct c_switch {
604f5adf
ILT
9364 /* The SWITCH_EXPR being built. */
9365 tree switch_expr;
a6c0a76c 9366
89dbed81 9367 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9368 default conversion is applied. */
9369 tree orig_type;
9370
3e4093b6
RS
9371 /* A splay-tree mapping the low element of a case range to the high
9372 element, or NULL_TREE if there is no high element. Used to
9373 determine whether or not a new case label duplicates an old case
9374 label. We need a tree, rather than simply a hash table, because
9375 of the GNU case range extension. */
9376 splay_tree cases;
a6c0a76c 9377
e1b7793c
ILT
9378 /* The bindings at the point of the switch. This is used for
9379 warnings crossing decls when branching to a case label. */
9380 struct c_spot_bindings *bindings;
187230a7 9381
3e4093b6
RS
9382 /* The next node on the stack. */
9383 struct c_switch *next;
9384};
400fbf9f 9385
3e4093b6
RS
9386/* A stack of the currently active switch statements. The innermost
9387 switch statement is on the top of the stack. There is no need to
9388 mark the stack for garbage collection because it is only active
9389 during the processing of the body of a function, and we never
9390 collect at that point. */
de520661 9391
506e2710 9392struct c_switch *c_switch_stack;
de520661 9393
3e4093b6 9394/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9395 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9396 SWITCH_COND_LOC is the location of the switch's condition.
9397 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9398
3e4093b6 9399tree
c2255bc4
AH
9400c_start_case (location_t switch_loc,
9401 location_t switch_cond_loc,
fedfecef 9402 tree exp, bool explicit_cast_p)
de520661 9403{
c58e8676 9404 tree orig_type = error_mark_node;
3e4093b6 9405 struct c_switch *cs;
2f6e4e97 9406
3e4093b6 9407 if (exp != error_mark_node)
de520661 9408 {
3e4093b6
RS
9409 orig_type = TREE_TYPE (exp);
9410
c58e8676 9411 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9412 {
c58e8676
VR
9413 if (orig_type != error_mark_node)
9414 {
c2255bc4 9415 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9416 orig_type = error_mark_node;
9417 }
3e4093b6 9418 exp = integer_zero_node;
de520661 9419 }
3e4093b6 9420 else
de520661 9421 {
c58e8676 9422 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9423 tree e = exp;
9424
9425 /* Warn if the condition has boolean value. */
9426 while (TREE_CODE (e) == COMPOUND_EXPR)
9427 e = TREE_OPERAND (e, 1);
9428
9429 if ((TREE_CODE (type) == BOOLEAN_TYPE
9430 || truth_value_p (TREE_CODE (e)))
9431 /* Explicit cast to int suppresses this warning. */
9432 && !(TREE_CODE (type) == INTEGER_TYPE
9433 && explicit_cast_p))
9434 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9435 "switch condition has boolean value");
8b6a5902 9436
8400e75e 9437 if (!in_system_header_at (input_location)
3e4093b6
RS
9438 && (type == long_integer_type_node
9439 || type == long_unsigned_type_node))
c2255bc4
AH
9440 warning_at (switch_cond_loc,
9441 OPT_Wtraditional, "%<long%> switch expression not "
9442 "converted to %<int%> in ISO C");
8b6a5902 9443
928c19bb 9444 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9445 exp = default_conversion (exp);
ca085fd7
MLI
9446
9447 if (warn_sequence_point)
9448 verify_sequence_points (exp);
3e4093b6
RS
9449 }
9450 }
9451
604f5adf 9452 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9453 cs = XNEW (struct c_switch);
604f5adf 9454 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9455 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9456 cs->orig_type = orig_type;
3e4093b6 9457 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9458 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9459 cs->next = c_switch_stack;
9460 c_switch_stack = cs;
3e4093b6 9461
604f5adf 9462 return add_stmt (cs->switch_expr);
3e4093b6
RS
9463}
9464
c2255bc4 9465/* Process a case label at location LOC. */
3e4093b6
RS
9466
9467tree
c2255bc4 9468do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9469{
9470 tree label = NULL_TREE;
9471
17cede2e
JM
9472 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9473 {
9474 low_value = c_fully_fold (low_value, false, NULL);
9475 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9476 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9477 "case label is not an integer constant expression");
9478 }
9479
9480 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9481 {
9482 high_value = c_fully_fold (high_value, false, NULL);
9483 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9484 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9485 "case label is not an integer constant expression");
9486 }
9487
e1b7793c 9488 if (c_switch_stack == NULL)
187230a7
JM
9489 {
9490 if (low_value)
e1b7793c 9491 error_at (loc, "case label not within a switch statement");
187230a7 9492 else
e1b7793c
ILT
9493 error_at (loc, "%<default%> label not within a switch statement");
9494 return NULL_TREE;
187230a7 9495 }
de520661 9496
e1b7793c
ILT
9497 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9498 EXPR_LOCATION (c_switch_stack->switch_expr),
9499 loc))
9500 return NULL_TREE;
9501
9502 label = c_add_case_label (loc, c_switch_stack->cases,
9503 SWITCH_COND (c_switch_stack->switch_expr),
9504 c_switch_stack->orig_type,
9505 low_value, high_value);
9506 if (label == error_mark_node)
9507 label = NULL_TREE;
3e4093b6
RS
9508 return label;
9509}
de520661 9510
083e891e
MP
9511/* Finish the switch statement. TYPE is the original type of the
9512 controlling expression of the switch, or NULL_TREE. */
de520661 9513
3e4093b6 9514void
083e891e 9515c_finish_case (tree body, tree type)
3e4093b6 9516{
506e2710 9517 struct c_switch *cs = c_switch_stack;
fbc315db 9518 location_t switch_location;
3e4093b6 9519
604f5adf 9520 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9521
6de9cd9a 9522 /* Emit warnings as needed. */
c2255bc4 9523 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 9524 c_do_switch_warnings (cs->cases, switch_location,
083e891e 9525 type ? type : TREE_TYPE (cs->switch_expr),
fbc315db 9526 SWITCH_COND (cs->switch_expr));
6de9cd9a 9527
3e4093b6 9528 /* Pop the stack. */
506e2710 9529 c_switch_stack = cs->next;
3e4093b6 9530 splay_tree_delete (cs->cases);
e1b7793c 9531 c_release_switch_bindings (cs->bindings);
5d038c4c 9532 XDELETE (cs);
de520661 9533}
325c3691 9534\f
506e2710
RH
9535/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9536 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9537 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9538 statement, and was not surrounded with parenthesis. */
325c3691 9539
9e51cf9d 9540void
506e2710
RH
9541c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9542 tree else_block, bool nested_if)
325c3691 9543{
506e2710 9544 tree stmt;
325c3691 9545
25c22937
BI
9546 /* If the condition has array notations, then the rank of the then_block and
9547 else_block must be either 0 or be equal to the rank of the condition. If
9548 the condition does not have array notations then break them up as it is
9549 broken up in a normal expression. */
b72271b9 9550 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9551 {
9552 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9553 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9554 return;
9555 if (then_block
9556 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9557 return;
9558 if (else_block
9559 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9560 return;
9561 if (cond_rank != then_rank && then_rank != 0)
9562 {
9563 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9564 " and the then-block");
9565 return;
9566 }
9567 else if (cond_rank != else_rank && else_rank != 0)
9568 {
9569 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9570 " and the else-block");
9571 return;
9572 }
9573 }
506e2710
RH
9574 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9575 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9576 {
506e2710 9577 tree inner_if = then_block;
16865eaa 9578
61ada8ae 9579 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9580 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9581 it might not be exactly THEN_BLOCK, but should be the last
9582 non-container statement within. */
9583 while (1)
9584 switch (TREE_CODE (inner_if))
9585 {
9586 case COND_EXPR:
9587 goto found;
9588 case BIND_EXPR:
9589 inner_if = BIND_EXPR_BODY (inner_if);
9590 break;
9591 case STATEMENT_LIST:
9592 inner_if = expr_last (then_block);
9593 break;
9594 case TRY_FINALLY_EXPR:
9595 case TRY_CATCH_EXPR:
9596 inner_if = TREE_OPERAND (inner_if, 0);
9597 break;
9598 default:
366de0ce 9599 gcc_unreachable ();
506e2710
RH
9600 }
9601 found:
16865eaa 9602
506e2710 9603 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9604 warning_at (if_locus, OPT_Wparentheses,
9605 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9606 }
16865eaa 9607
2214de30 9608 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9609 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9610 add_stmt (stmt);
325c3691
RH
9611}
9612
506e2710
RH
9613/* Emit a general-purpose loop construct. START_LOCUS is the location of
9614 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9615 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9616 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9617 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9618
9619void
506e2710
RH
9620c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9621 tree blab, tree clab, bool cond_is_first)
325c3691 9622{
506e2710
RH
9623 tree entry = NULL, exit = NULL, t;
9624
e5e44252
AK
9625 /* In theory could forbid cilk spawn for loop increment expression,
9626 but it should work just fine. */
36536d79 9627
28af952a
RS
9628 /* If the condition is zero don't generate a loop construct. */
9629 if (cond && integer_zerop (cond))
9630 {
9631 if (cond_is_first)
9632 {
9633 t = build_and_jump (&blab);
9634 SET_EXPR_LOCATION (t, start_locus);
9635 add_stmt (t);
9636 }
9637 }
9638 else
506e2710
RH
9639 {
9640 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9641
506e2710 9642 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9643 out of the loop, or to the top of it. If there's no exit condition,
9644 then we just build a jump back to the top. */
506e2710 9645 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9646
28af952a 9647 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9648 {
9649 /* Canonicalize the loop condition to the end. This means
9650 generating a branch to the loop condition. Reuse the
9651 continue label, if possible. */
9652 if (cond_is_first)
9653 {
9654 if (incr || !clab)
9655 {
9656 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9657 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9658 }
9659 else
9660 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9661 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9662 add_stmt (t);
9663 }
9664
506e2710 9665 t = build_and_jump (&blab);
506e2710 9666 if (cond_is_first)
db3927fb
AH
9667 exit = fold_build3_loc (start_locus,
9668 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9669 else
db3927fb
AH
9670 exit = fold_build3_loc (input_location,
9671 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9672 }
9673
506e2710
RH
9674 add_stmt (top);
9675 }
c22cacf3 9676
506e2710
RH
9677 if (body)
9678 add_stmt (body);
9679 if (clab)
9680 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9681 if (incr)
9682 add_stmt (incr);
9683 if (entry)
9684 add_stmt (entry);
9685 if (exit)
9686 add_stmt (exit);
9687 if (blab)
9688 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9689}
325c3691
RH
9690
9691tree
c2255bc4 9692c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9693{
089efaa4 9694 bool skip;
506e2710 9695 tree label = *label_p;
325c3691 9696
089efaa4
ILT
9697 /* In switch statements break is sometimes stylistically used after
9698 a return statement. This can lead to spurious warnings about
9699 control reaching the end of a non-void function when it is
9700 inlined. Note that we are calling block_may_fallthru with
9701 language specific tree nodes; this works because
9702 block_may_fallthru returns true when given something it does not
9703 understand. */
9704 skip = !block_may_fallthru (cur_stmt_list);
9705
506e2710 9706 if (!label)
089efaa4
ILT
9707 {
9708 if (!skip)
c2255bc4 9709 *label_p = label = create_artificial_label (loc);
089efaa4 9710 }
953ff289
DN
9711 else if (TREE_CODE (label) == LABEL_DECL)
9712 ;
9713 else switch (TREE_INT_CST_LOW (label))
506e2710 9714 {
953ff289 9715 case 0:
506e2710 9716 if (is_break)
c2255bc4 9717 error_at (loc, "break statement not within loop or switch");
506e2710 9718 else
c2255bc4 9719 error_at (loc, "continue statement not within a loop");
506e2710 9720 return NULL_TREE;
953ff289
DN
9721
9722 case 1:
9723 gcc_assert (is_break);
c2255bc4 9724 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9725 return NULL_TREE;
9726
c02065fc
AH
9727 case 2:
9728 if (is_break)
9729 error ("break statement within %<#pragma simd%> loop body");
9730 else
9731 error ("continue statement within %<#pragma simd%> loop body");
9732 return NULL_TREE;
9733
953ff289
DN
9734 default:
9735 gcc_unreachable ();
506e2710 9736 }
325c3691 9737
089efaa4
ILT
9738 if (skip)
9739 return NULL_TREE;
9740
2e28e797
JH
9741 if (!is_break)
9742 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9743
53fb4de3 9744 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9745}
9746
506e2710 9747/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9748
9749static void
c2255bc4 9750emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9751{
e6b5a630
RH
9752 if (expr == error_mark_node)
9753 ;
9754 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9755 {
9756 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9757 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9758 }
789eadcd
MP
9759 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9760 {
9761 tree r = expr;
9762 location_t cloc = loc;
9763 while (TREE_CODE (r) == COMPOUND_EXPR)
9764 {
9765 if (EXPR_HAS_LOCATION (r))
9766 cloc = EXPR_LOCATION (r);
9767 r = TREE_OPERAND (r, 1);
9768 }
9769 if (!TREE_SIDE_EFFECTS (r)
9770 && !VOID_TYPE_P (TREE_TYPE (r))
9771 && !CONVERT_EXPR_P (r)
cc28fc7f 9772 && !TREE_NO_WARNING (r)
789eadcd
MP
9773 && !TREE_NO_WARNING (expr))
9774 warning_at (cloc, OPT_Wunused_value,
9775 "right-hand operand of comma expression has no effect");
9776 }
27f33b15 9777 else
c2255bc4 9778 warn_if_unused_value (expr, loc);
3a5b9284
RH
9779}
9780
506e2710 9781/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9782 diagnostics, but do not call ADD_STMT. LOC is the location of the
9783 statement. */
3a5b9284 9784
506e2710 9785tree
c2255bc4 9786c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9787{
056928b2
JJ
9788 tree exprv;
9789
3a5b9284 9790 if (!expr)
506e2710 9791 return NULL_TREE;
3a5b9284 9792
928c19bb
JM
9793 expr = c_fully_fold (expr, false, NULL);
9794
3a5b9284
RH
9795 if (warn_sequence_point)
9796 verify_sequence_points (expr);
9797
9798 if (TREE_TYPE (expr) != error_mark_node
9799 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9800 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9801 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9802
9803 /* If we're not processing a statement expression, warn about unused values.
9804 Warnings for statement expressions will be emitted later, once we figure
9805 out which is the result. */
9806 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9807 && warn_unused_value)
c2255bc4 9808 emit_side_effect_warnings (loc, expr);
3a5b9284 9809
056928b2
JJ
9810 exprv = expr;
9811 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9812 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9813 while (CONVERT_EXPR_P (exprv))
9814 exprv = TREE_OPERAND (exprv, 0);
9815 if (DECL_P (exprv)
9816 || handled_component_p (exprv)
9817 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9818 mark_exp_read (exprv);
fa8351f8 9819
3a5b9284
RH
9820 /* If the expression is not of a type to which we cannot assign a line
9821 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9822 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9823 {
9824 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9825 SET_EXPR_LOCATION (expr, loc);
9826 }
506e2710
RH
9827
9828 return expr;
9829}
9830
c2255bc4
AH
9831/* Emit an expression as a statement. LOC is the location of the
9832 expression. */
506e2710
RH
9833
9834tree
c2255bc4 9835c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9836{
9837 if (expr)
c2255bc4 9838 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9839 else
9840 return NULL;
3a5b9284
RH
9841}
9842
9843/* Do the opposite and emit a statement as an expression. To begin,
9844 create a new binding level and return it. */
325c3691
RH
9845
9846tree
9847c_begin_stmt_expr (void)
9848{
9849 tree ret;
9850
9851 /* We must force a BLOCK for this level so that, if it is not expanded
9852 later, there is a way to turn off the entire subtree of blocks that
9853 are contained in it. */
9854 keep_next_level ();
9855 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9856
9857 c_bindings_start_stmt_expr (c_switch_stack == NULL
9858 ? NULL
9859 : c_switch_stack->bindings);
325c3691
RH
9860
9861 /* Mark the current statement list as belonging to a statement list. */
9862 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9863
9864 return ret;
9865}
9866
c2255bc4
AH
9867/* LOC is the location of the compound statement to which this body
9868 belongs. */
9869
325c3691 9870tree
c2255bc4 9871c_finish_stmt_expr (location_t loc, tree body)
325c3691 9872{
3a5b9284 9873 tree last, type, tmp, val;
325c3691
RH
9874 tree *last_p;
9875
c2255bc4 9876 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9877
9878 c_bindings_end_stmt_expr (c_switch_stack == NULL
9879 ? NULL
9880 : c_switch_stack->bindings);
325c3691 9881
3a5b9284
RH
9882 /* Locate the last statement in BODY. See c_end_compound_stmt
9883 about always returning a BIND_EXPR. */
9884 last_p = &BIND_EXPR_BODY (body);
9885 last = BIND_EXPR_BODY (body);
9886
9887 continue_searching:
325c3691
RH
9888 if (TREE_CODE (last) == STATEMENT_LIST)
9889 {
3a5b9284
RH
9890 tree_stmt_iterator i;
9891
9892 /* This can happen with degenerate cases like ({ }). No value. */
9893 if (!TREE_SIDE_EFFECTS (last))
9894 return body;
9895
9896 /* If we're supposed to generate side effects warnings, process
9897 all of the statements except the last. */
27f33b15 9898 if (warn_unused_value)
325c3691 9899 {
3a5b9284 9900 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9901 {
9902 location_t tloc;
9903 tree t = tsi_stmt (i);
9904
9905 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9906 emit_side_effect_warnings (tloc, t);
9907 }
325c3691
RH
9908 }
9909 else
3a5b9284
RH
9910 i = tsi_last (last);
9911 last_p = tsi_stmt_ptr (i);
9912 last = *last_p;
325c3691
RH
9913 }
9914
3a5b9284
RH
9915 /* If the end of the list is exception related, then the list was split
9916 by a call to push_cleanup. Continue searching. */
9917 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9918 || TREE_CODE (last) == TRY_CATCH_EXPR)
9919 {
9920 last_p = &TREE_OPERAND (last, 0);
9921 last = *last_p;
9922 goto continue_searching;
9923 }
9924
26d8af35
JM
9925 if (last == error_mark_node)
9926 return last;
9927
3a5b9284
RH
9928 /* In the case that the BIND_EXPR is not necessary, return the
9929 expression out from inside it. */
26d8af35
JM
9930 if (last == BIND_EXPR_BODY (body)
9931 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9932 {
928c19bb
JM
9933 /* Even if this looks constant, do not allow it in a constant
9934 expression. */
e5a94231 9935 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9936 /* Do not warn if the return value of a statement expression is
9937 unused. */
928c19bb 9938 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9939 return last;
9940 }
325c3691
RH
9941
9942 /* Extract the type of said expression. */
9943 type = TREE_TYPE (last);
325c3691 9944
3a5b9284
RH
9945 /* If we're not returning a value at all, then the BIND_EXPR that
9946 we already have is a fine expression to return. */
9947 if (!type || VOID_TYPE_P (type))
9948 return body;
9949
9950 /* Now that we've located the expression containing the value, it seems
9951 silly to make voidify_wrapper_expr repeat the process. Create a
9952 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9953 tmp = create_tmp_var_raw (type, NULL);
9954
9955 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9956 tree_expr_nonnegative_p giving up immediately. */
9957 val = last;
9958 if (TREE_CODE (val) == NOP_EXPR
9959 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9960 val = TREE_OPERAND (val, 0);
9961
53fb4de3 9962 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9963 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9964
c2255bc4
AH
9965 {
9966 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9967 SET_EXPR_LOCATION (t, loc);
9968 return t;
9969 }
325c3691
RH
9970}
9971\f
9972/* Begin and end compound statements. This is as simple as pushing
9973 and popping new statement lists from the tree. */
9974
9975tree
9976c_begin_compound_stmt (bool do_scope)
9977{
9978 tree stmt = push_stmt_list ();
9979 if (do_scope)
4dfa0342 9980 push_scope ();
325c3691
RH
9981 return stmt;
9982}
9983
c2255bc4
AH
9984/* End a compound statement. STMT is the statement. LOC is the
9985 location of the compound statement-- this is usually the location
9986 of the opening brace. */
9987
325c3691 9988tree
c2255bc4 9989c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9990{
9991 tree block = NULL;
9992
9993 if (do_scope)
9994 {
9995 if (c_dialect_objc ())
9996 objc_clear_super_receiver ();
9997 block = pop_scope ();
9998 }
9999
10000 stmt = pop_stmt_list (stmt);
c2255bc4 10001 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10002
10003 /* If this compound statement is nested immediately inside a statement
10004 expression, then force a BIND_EXPR to be created. Otherwise we'll
10005 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10006 STATEMENT_LISTs merge, and thus we can lose track of what statement
10007 was really last. */
38e01f9e 10008 if (building_stmt_list_p ()
325c3691
RH
10009 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10010 && TREE_CODE (stmt) != BIND_EXPR)
10011 {
53fb4de3 10012 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 10013 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 10014 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
10015 }
10016
10017 return stmt;
10018}
5a508662
RH
10019
10020/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10021 when the current scope is exited. EH_ONLY is true when this is not
10022 meant to apply to normal control flow transfer. */
10023
10024void
c2255bc4 10025push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10026{
3a5b9284
RH
10027 enum tree_code code;
10028 tree stmt, list;
10029 bool stmt_expr;
10030
10031 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10032 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10033 add_stmt (stmt);
3a5b9284
RH
10034 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10035 list = push_stmt_list ();
10036 TREE_OPERAND (stmt, 0) = list;
10037 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10038}
325c3691 10039\f
3e4093b6
RS
10040/* Build a binary-operation expression without default conversions.
10041 CODE is the kind of expression to build.
ba47d38d 10042 LOCATION is the operator's location.
3e4093b6
RS
10043 This function differs from `build' in several ways:
10044 the data type of the result is computed and recorded in it,
10045 warnings are generated if arg data types are invalid,
10046 special handling for addition and subtraction of pointers is known,
10047 and some optimization is done (operations on narrow ints
10048 are done in the narrower type when that gives the same result).
10049 Constant folding is also done before the result is returned.
de520661 10050
3e4093b6
RS
10051 Note that the operands will never have enumeral types, or function
10052 or array types, because either they will have the default conversions
10053 performed or they have both just been converted to some other type in which
10054 the arithmetic is to be done. */
10055
10056tree
ba47d38d
AH
10057build_binary_op (location_t location, enum tree_code code,
10058 tree orig_op0, tree orig_op1, int convert_p)
de520661 10059{
8ce94e44
JM
10060 tree type0, type1, orig_type0, orig_type1;
10061 tree eptype;
3e4093b6
RS
10062 enum tree_code code0, code1;
10063 tree op0, op1;
c9f9eb5d 10064 tree ret = error_mark_node;
4de67c26 10065 const char *invalid_op_diag;
4d84fe7c 10066 bool op0_int_operands, op1_int_operands;
928c19bb 10067 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10068
3e4093b6
RS
10069 /* Expression code to give to the expression when it is built.
10070 Normally this is CODE, which is what the caller asked for,
10071 but in some special cases we change it. */
10072 enum tree_code resultcode = code;
8b6a5902 10073
3e4093b6
RS
10074 /* Data type in which the computation is to be performed.
10075 In the simplest cases this is the common type of the arguments. */
10076 tree result_type = NULL;
10077
8ce94e44
JM
10078 /* When the computation is in excess precision, the type of the
10079 final EXCESS_PRECISION_EXPR. */
2d2e923f 10080 tree semantic_result_type = NULL;
8ce94e44 10081
3e4093b6
RS
10082 /* Nonzero means operands have already been type-converted
10083 in whatever way is necessary.
10084 Zero means they need to be converted to RESULT_TYPE. */
10085 int converted = 0;
10086
10087 /* Nonzero means create the expression with this type, rather than
10088 RESULT_TYPE. */
10089 tree build_type = 0;
10090
10091 /* Nonzero means after finally constructing the expression
10092 convert it to this type. */
10093 tree final_type = 0;
10094
10095 /* Nonzero if this is an operation like MIN or MAX which can
10096 safely be computed in short if both args are promoted shorts.
10097 Also implies COMMON.
10098 -1 indicates a bitwise operation; this makes a difference
10099 in the exact conditions for when it is safe to do the operation
10100 in a narrower mode. */
10101 int shorten = 0;
10102
10103 /* Nonzero if this is a comparison operation;
10104 if both args are promoted shorts, compare the original shorts.
10105 Also implies COMMON. */
10106 int short_compare = 0;
10107
10108 /* Nonzero if this is a right-shift operation, which can be computed on the
10109 original short and then promoted if the operand is a promoted short. */
10110 int short_shift = 0;
10111
10112 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10113 int common = 0;
10114
58393038
ZL
10115 /* True means types are compatible as far as ObjC is concerned. */
10116 bool objc_ok;
10117
8ce94e44
JM
10118 /* True means this is an arithmetic operation that may need excess
10119 precision. */
10120 bool may_need_excess_precision;
10121
180f8dbb
JM
10122 /* True means this is a boolean operation that converts both its
10123 operands to truth-values. */
10124 bool boolean_op = false;
10125
de5a5fa1
MP
10126 /* Remember whether we're doing / or %. */
10127 bool doing_div_or_mod = false;
10128
10129 /* Remember whether we're doing << or >>. */
10130 bool doing_shift = false;
10131
10132 /* Tree holding instrumentation expression. */
10133 tree instrument_expr = NULL;
10134
ba47d38d
AH
10135 if (location == UNKNOWN_LOCATION)
10136 location = input_location;
10137
4d84fe7c
JM
10138 op0 = orig_op0;
10139 op1 = orig_op1;
10140
10141 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10142 if (op0_int_operands)
10143 op0 = remove_c_maybe_const_expr (op0);
10144 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10145 if (op1_int_operands)
10146 op1 = remove_c_maybe_const_expr (op1);
10147 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10148 if (int_operands)
10149 {
10150 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10151 && TREE_CODE (orig_op1) == INTEGER_CST);
10152 int_const = (int_const_or_overflow
10153 && !TREE_OVERFLOW (orig_op0)
10154 && !TREE_OVERFLOW (orig_op1));
10155 }
10156 else
10157 int_const = int_const_or_overflow = false;
10158
0e3a99ae 10159 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10160 if (convert_p
10161 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10162 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10163 {
4d84fe7c
JM
10164 op0 = default_conversion (op0);
10165 op1 = default_conversion (op1);
790e9490
RS
10166 }
10167
36536d79
BI
10168 /* When Cilk Plus is enabled and there are array notations inside op0, then
10169 we check to see if there are builtin array notation functions. If
10170 so, then we take on the type of the array notation inside it. */
b72271b9 10171 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10172 orig_type0 = type0 = find_correct_array_notation_type (op0);
10173 else
10174 orig_type0 = type0 = TREE_TYPE (op0);
10175
b72271b9 10176 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10177 orig_type1 = type1 = find_correct_array_notation_type (op1);
10178 else
10179 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10180
3e4093b6
RS
10181 /* The expression codes of the data types of the arguments tell us
10182 whether the arguments are integers, floating, pointers, etc. */
10183 code0 = TREE_CODE (type0);
10184 code1 = TREE_CODE (type1);
10185
10186 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10187 STRIP_TYPE_NOPS (op0);
10188 STRIP_TYPE_NOPS (op1);
10189
10190 /* If an error was already reported for one of the arguments,
10191 avoid reporting another error. */
10192
10193 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10194 return error_mark_node;
10195
4de67c26
JM
10196 if ((invalid_op_diag
10197 = targetm.invalid_binary_op (code, type0, type1)))
10198 {
ba47d38d 10199 error_at (location, invalid_op_diag);
4de67c26
JM
10200 return error_mark_node;
10201 }
10202
8ce94e44
JM
10203 switch (code)
10204 {
10205 case PLUS_EXPR:
10206 case MINUS_EXPR:
10207 case MULT_EXPR:
10208 case TRUNC_DIV_EXPR:
10209 case CEIL_DIV_EXPR:
10210 case FLOOR_DIV_EXPR:
10211 case ROUND_DIV_EXPR:
10212 case EXACT_DIV_EXPR:
10213 may_need_excess_precision = true;
10214 break;
10215 default:
10216 may_need_excess_precision = false;
10217 break;
10218 }
10219 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10220 {
10221 op0 = TREE_OPERAND (op0, 0);
10222 type0 = TREE_TYPE (op0);
10223 }
10224 else if (may_need_excess_precision
10225 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10226 {
10227 type0 = eptype;
10228 op0 = convert (eptype, op0);
10229 }
10230 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10231 {
10232 op1 = TREE_OPERAND (op1, 0);
10233 type1 = TREE_TYPE (op1);
10234 }
10235 else if (may_need_excess_precision
10236 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10237 {
10238 type1 = eptype;
10239 op1 = convert (eptype, op1);
10240 }
10241
58393038
ZL
10242 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10243
0e3a99ae
AS
10244 /* In case when one of the operands of the binary operation is
10245 a vector and another is a scalar -- convert scalar to vector. */
10246 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10247 {
a212e43f
MG
10248 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10249 true);
f90e8e2e 10250
0e3a99ae
AS
10251 switch (convert_flag)
10252 {
10253 case stv_error:
10254 return error_mark_node;
10255 case stv_firstarg:
10256 {
10257 bool maybe_const = true;
10258 tree sc;
10259 sc = c_fully_fold (op0, false, &maybe_const);
10260 sc = save_expr (sc);
10261 sc = convert (TREE_TYPE (type1), sc);
10262 op0 = build_vector_from_val (type1, sc);
10263 if (!maybe_const)
10264 op0 = c_wrap_maybe_const (op0, true);
10265 orig_type0 = type0 = TREE_TYPE (op0);
10266 code0 = TREE_CODE (type0);
10267 converted = 1;
10268 break;
10269 }
10270 case stv_secondarg:
10271 {
10272 bool maybe_const = true;
10273 tree sc;
10274 sc = c_fully_fold (op1, false, &maybe_const);
10275 sc = save_expr (sc);
10276 sc = convert (TREE_TYPE (type0), sc);
10277 op1 = build_vector_from_val (type0, sc);
10278 if (!maybe_const)
54b9f838 10279 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10280 orig_type1 = type1 = TREE_TYPE (op1);
10281 code1 = TREE_CODE (type1);
10282 converted = 1;
10283 break;
10284 }
10285 default:
10286 break;
10287 }
10288 }
10289
3e4093b6 10290 switch (code)
de520661 10291 {
3e4093b6
RS
10292 case PLUS_EXPR:
10293 /* Handle the pointer + int case. */
10294 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10295 {
db3927fb 10296 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10297 goto return_build_binary_op;
10298 }
3e4093b6 10299 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10300 {
db3927fb 10301 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10302 goto return_build_binary_op;
10303 }
fe67cf58 10304 else
3e4093b6
RS
10305 common = 1;
10306 break;
400fbf9f 10307
3e4093b6
RS
10308 case MINUS_EXPR:
10309 /* Subtraction of two similar pointers.
10310 We must subtract them as integers, then divide by object size. */
10311 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10312 && comp_target_types (location, type0, type1))
c9f9eb5d 10313 {
db3927fb 10314 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10315 goto return_build_binary_op;
10316 }
3e4093b6
RS
10317 /* Handle pointer minus int. Just like pointer plus int. */
10318 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10319 {
db3927fb 10320 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10321 goto return_build_binary_op;
10322 }
3e4093b6
RS
10323 else
10324 common = 1;
10325 break;
8b6a5902 10326
3e4093b6
RS
10327 case MULT_EXPR:
10328 common = 1;
10329 break;
10330
10331 case TRUNC_DIV_EXPR:
10332 case CEIL_DIV_EXPR:
10333 case FLOOR_DIV_EXPR:
10334 case ROUND_DIV_EXPR:
10335 case EXACT_DIV_EXPR:
de5a5fa1 10336 doing_div_or_mod = true;
c9f9eb5d 10337 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10338
10339 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10340 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10341 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10342 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10343 || code1 == FIXED_POINT_TYPE
3e4093b6 10344 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10345 {
5bed876a
AH
10346 enum tree_code tcode0 = code0, tcode1 = code1;
10347
3a021db2 10348 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10349 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10350 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10351 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10352
ab22c1fa
CF
10353 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10354 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10355 resultcode = RDIV_EXPR;
10356 else
10357 /* Although it would be tempting to shorten always here, that
10358 loses on some targets, since the modulo instruction is
10359 undefined if the quotient can't be represented in the
10360 computation mode. We shorten only if unsigned or if
10361 dividing by something we know != -1. */
8df83eae 10362 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10363 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10364 && !integer_all_onesp (op1)));
3e4093b6
RS
10365 common = 1;
10366 }
10367 break;
de520661 10368
3e4093b6 10369 case BIT_AND_EXPR:
3e4093b6
RS
10370 case BIT_IOR_EXPR:
10371 case BIT_XOR_EXPR:
10372 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10373 shorten = -1;
9ef0c8d9
AP
10374 /* Allow vector types which are not floating point types. */
10375 else if (code0 == VECTOR_TYPE
10376 && code1 == VECTOR_TYPE
10377 && !VECTOR_FLOAT_TYPE_P (type0)
10378 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10379 common = 1;
10380 break;
10381
10382 case TRUNC_MOD_EXPR:
10383 case FLOOR_MOD_EXPR:
de5a5fa1 10384 doing_div_or_mod = true;
c9f9eb5d 10385 warn_for_div_by_zero (location, op1);
de520661 10386
5cfd5d9b
AP
10387 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10388 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10389 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10390 common = 1;
10391 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10392 {
10393 /* Although it would be tempting to shorten always here, that loses
10394 on some targets, since the modulo instruction is undefined if the
10395 quotient can't be represented in the computation mode. We shorten
10396 only if unsigned or if dividing by something we know != -1. */
8df83eae 10397 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10398 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10399 && !integer_all_onesp (op1)));
3e4093b6
RS
10400 common = 1;
10401 }
10402 break;
de520661 10403
3e4093b6
RS
10404 case TRUTH_ANDIF_EXPR:
10405 case TRUTH_ORIF_EXPR:
10406 case TRUTH_AND_EXPR:
10407 case TRUTH_OR_EXPR:
10408 case TRUTH_XOR_EXPR:
10409 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10410 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10411 || code0 == FIXED_POINT_TYPE)
3e4093b6 10412 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10413 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10414 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10415 {
10416 /* Result of these operations is always an int,
10417 but that does not mean the operands should be
10418 converted to ints! */
10419 result_type = integer_type_node;
a27d595d
JM
10420 if (op0_int_operands)
10421 {
10422 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10423 op0 = remove_c_maybe_const_expr (op0);
10424 }
10425 else
10426 op0 = c_objc_common_truthvalue_conversion (location, op0);
10427 if (op1_int_operands)
10428 {
10429 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10430 op1 = remove_c_maybe_const_expr (op1);
10431 }
10432 else
10433 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10434 converted = 1;
180f8dbb 10435 boolean_op = true;
3e4093b6 10436 }
928c19bb
JM
10437 if (code == TRUTH_ANDIF_EXPR)
10438 {
10439 int_const_or_overflow = (int_operands
10440 && TREE_CODE (orig_op0) == INTEGER_CST
10441 && (op0 == truthvalue_false_node
10442 || TREE_CODE (orig_op1) == INTEGER_CST));
10443 int_const = (int_const_or_overflow
10444 && !TREE_OVERFLOW (orig_op0)
10445 && (op0 == truthvalue_false_node
10446 || !TREE_OVERFLOW (orig_op1)));
10447 }
10448 else if (code == TRUTH_ORIF_EXPR)
10449 {
10450 int_const_or_overflow = (int_operands
10451 && TREE_CODE (orig_op0) == INTEGER_CST
10452 && (op0 == truthvalue_true_node
10453 || TREE_CODE (orig_op1) == INTEGER_CST));
10454 int_const = (int_const_or_overflow
10455 && !TREE_OVERFLOW (orig_op0)
10456 && (op0 == truthvalue_true_node
10457 || !TREE_OVERFLOW (orig_op1)));
10458 }
3e4093b6 10459 break;
eba80994 10460
3e4093b6
RS
10461 /* Shift operations: result has same type as first operand;
10462 always convert second operand to int.
10463 Also set SHORT_SHIFT if shifting rightward. */
de520661 10464
3e4093b6 10465 case RSHIFT_EXPR:
f87bd04b
AS
10466 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10467 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10468 {
10469 result_type = type0;
10470 converted = 1;
10471 }
10472 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10473 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10474 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10475 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10476 {
10477 result_type = type0;
10478 converted = 1;
10479 }
10480 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10481 && code1 == INTEGER_TYPE)
3e4093b6 10482 {
de5a5fa1 10483 doing_shift = true;
928c19bb 10484 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10485 {
3e4093b6 10486 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10487 {
10488 int_const = false;
7d882b83 10489 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10490 warning_at (location, OPT_Wshift_count_negative,
10491 "right shift count is negative");
928c19bb 10492 }
3e4093b6 10493 else
bbb818c6 10494 {
3f75a254 10495 if (!integer_zerop (op1))
3e4093b6
RS
10496 short_shift = 1;
10497
10498 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10499 {
10500 int_const = false;
7d882b83 10501 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10502 warning_at (location, OPT_Wshift_count_overflow,
10503 "right shift count >= width of type");
928c19bb 10504 }
bbb818c6 10505 }
b62acd60 10506 }
de520661 10507
3e4093b6
RS
10508 /* Use the type of the value to be shifted. */
10509 result_type = type0;
f87bd04b
AS
10510 /* Convert the non vector shift-count to an integer, regardless
10511 of size of value being shifted. */
10512 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10513 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10514 op1 = convert (integer_type_node, op1);
10515 /* Avoid converting op1 to result_type later. */
10516 converted = 1;
400fbf9f 10517 }
3e4093b6 10518 break;
253b6b82 10519
3e4093b6 10520 case LSHIFT_EXPR:
f87bd04b
AS
10521 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10522 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10523 {
10524 result_type = type0;
10525 converted = 1;
10526 }
10527 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10528 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10529 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10530 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10531 {
10532 result_type = type0;
10533 converted = 1;
10534 }
10535 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10536 && code1 == INTEGER_TYPE)
3e4093b6 10537 {
de5a5fa1 10538 doing_shift = true;
928c19bb 10539 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10540 {
3e4093b6 10541 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10542 {
10543 int_const = false;
7d882b83 10544 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10545 warning_at (location, OPT_Wshift_count_negative,
10546 "left shift count is negative");
928c19bb 10547 }
de520661 10548
3e4093b6 10549 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10550 {
10551 int_const = false;
7d882b83 10552 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
10553 warning_at (location, OPT_Wshift_count_overflow,
10554 "left shift count >= width of type");
928c19bb 10555 }
94ba5069 10556 }
de520661 10557
3e4093b6
RS
10558 /* Use the type of the value to be shifted. */
10559 result_type = type0;
f87bd04b
AS
10560 /* Convert the non vector shift-count to an integer, regardless
10561 of size of value being shifted. */
10562 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10563 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10564 op1 = convert (integer_type_node, op1);
10565 /* Avoid converting op1 to result_type later. */
10566 converted = 1;
400fbf9f 10567 }
3e4093b6 10568 break;
de520661 10569
3e4093b6
RS
10570 case EQ_EXPR:
10571 case NE_EXPR:
d246ab4f
AS
10572 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10573 {
10574 tree intt;
0af94e6f 10575 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10576 {
10577 error_at (location, "comparing vectors with different "
10578 "element types");
10579 return error_mark_node;
10580 }
10581
10582 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10583 {
10584 error_at (location, "comparing vectors with different "
10585 "number of elements");
10586 return error_mark_node;
10587 }
10588
10589 /* Always construct signed integer vector type. */
10590 intt = c_common_type_for_size (GET_MODE_BITSIZE
10591 (TYPE_MODE (TREE_TYPE (type0))), 0);
10592 result_type = build_opaque_vector_type (intt,
10593 TYPE_VECTOR_SUBPARTS (type0));
10594 converted = 1;
10595 break;
10596 }
ae311566 10597 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10598 warning_at (location,
10599 OPT_Wfloat_equal,
10600 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10601 /* Result of comparison is always int,
10602 but don't convert the args to int! */
10603 build_type = integer_type_node;
10604 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10605 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10606 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10607 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10608 short_compare = 1;
637f1455
SZ
10609 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10610 {
10611 if (TREE_CODE (op0) == ADDR_EXPR
10612 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10613 {
10614 if (code == EQ_EXPR)
10615 warning_at (location,
10616 OPT_Waddress,
10617 "the comparison will always evaluate as %<false%> "
10618 "for the address of %qD will never be NULL",
10619 TREE_OPERAND (op0, 0));
10620 else
10621 warning_at (location,
10622 OPT_Waddress,
10623 "the comparison will always evaluate as %<true%> "
10624 "for the address of %qD will never be NULL",
10625 TREE_OPERAND (op0, 0));
10626 }
10627 result_type = type0;
10628 }
10629 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10630 {
10631 if (TREE_CODE (op1) == ADDR_EXPR
10632 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10633 {
10634 if (code == EQ_EXPR)
10635 warning_at (location,
f90e8e2e 10636 OPT_Waddress,
637f1455
SZ
10637 "the comparison will always evaluate as %<false%> "
10638 "for the address of %qD will never be NULL",
10639 TREE_OPERAND (op1, 0));
10640 else
10641 warning_at (location,
10642 OPT_Waddress,
10643 "the comparison will always evaluate as %<true%> "
10644 "for the address of %qD will never be NULL",
10645 TREE_OPERAND (op1, 0));
10646 }
10647 result_type = type1;
10648 }
3e4093b6
RS
10649 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10650 {
10651 tree tt0 = TREE_TYPE (type0);
10652 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10653 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10654 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10655 addr_space_t as_common = ADDR_SPACE_GENERIC;
10656
3e4093b6
RS
10657 /* Anything compares with void *. void * compares with anything.
10658 Otherwise, the targets must be compatible
10659 and both must be object or both incomplete. */
744aa42f 10660 if (comp_target_types (location, type0, type1))
10bc1b1b 10661 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10662 else if (!addr_space_superset (as0, as1, &as_common))
10663 {
10664 error_at (location, "comparison of pointers to "
10665 "disjoint address spaces");
10666 return error_mark_node;
10667 }
267bac10 10668 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10669 {
36c5e70a 10670 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10671 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10672 "comparison of %<void *%> with function pointer");
ee2990e7 10673 }
267bac10 10674 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10675 {
36c5e70a 10676 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10677 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10678 "comparison of %<void *%> with function pointer");
e6834654 10679 }
3e4093b6 10680 else
58393038
ZL
10681 /* Avoid warning about the volatile ObjC EH puts on decls. */
10682 if (!objc_ok)
ba47d38d 10683 pedwarn (location, 0,
509c9d60 10684 "comparison of distinct pointer types lacks a cast");
e6834654 10685
3e4093b6 10686 if (result_type == NULL_TREE)
36c5e70a
BE
10687 {
10688 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10689 result_type = build_pointer_type
10690 (build_qualified_type (void_type_node, qual));
10691 }
e6834654 10692 }
3e4093b6 10693 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10694 {
3e4093b6 10695 result_type = type0;
ba47d38d 10696 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10697 }
3e4093b6 10698 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10699 {
3e4093b6 10700 result_type = type1;
ba47d38d 10701 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10702 }
04159acf
MP
10703 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10704 || truth_value_p (TREE_CODE (orig_op0)))
10705 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10706 || truth_value_p (TREE_CODE (orig_op1))))
10707 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10708 break;
8b6a5902 10709
3e4093b6
RS
10710 case LE_EXPR:
10711 case GE_EXPR:
10712 case LT_EXPR:
10713 case GT_EXPR:
d246ab4f
AS
10714 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10715 {
10716 tree intt;
0af94e6f 10717 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10718 {
10719 error_at (location, "comparing vectors with different "
10720 "element types");
10721 return error_mark_node;
10722 }
10723
10724 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10725 {
10726 error_at (location, "comparing vectors with different "
10727 "number of elements");
10728 return error_mark_node;
10729 }
10730
10731 /* Always construct signed integer vector type. */
10732 intt = c_common_type_for_size (GET_MODE_BITSIZE
10733 (TYPE_MODE (TREE_TYPE (type0))), 0);
10734 result_type = build_opaque_vector_type (intt,
10735 TYPE_VECTOR_SUBPARTS (type0));
10736 converted = 1;
10737 break;
10738 }
3e4093b6 10739 build_type = integer_type_node;
ab22c1fa
CF
10740 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10741 || code0 == FIXED_POINT_TYPE)
10742 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10743 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10744 short_compare = 1;
10745 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10746 {
36c5e70a
BE
10747 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10748 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10749 addr_space_t as_common;
10750
744aa42f 10751 if (comp_target_types (location, type0, type1))
3e4093b6 10752 {
10bc1b1b 10753 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10754 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10755 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10756 pedwarn (location, 0,
509c9d60 10757 "comparison of complete and incomplete pointers");
fcf73884 10758 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10759 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10760 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10761 else if (null_pointer_constant_p (orig_op0)
10762 || null_pointer_constant_p (orig_op1))
10763 warning_at (location, OPT_Wextra,
10764 "ordered comparison of pointer with null pointer");
10765
3e4093b6 10766 }
36c5e70a
BE
10767 else if (!addr_space_superset (as0, as1, &as_common))
10768 {
10769 error_at (location, "comparison of pointers to "
10770 "disjoint address spaces");
10771 return error_mark_node;
10772 }
3e4093b6
RS
10773 else
10774 {
36c5e70a
BE
10775 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10776 result_type = build_pointer_type
10777 (build_qualified_type (void_type_node, qual));
ba47d38d 10778 pedwarn (location, 0,
509c9d60 10779 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10780 }
10781 }
6aa3c60d 10782 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10783 {
10784 result_type = type0;
fcf73884 10785 if (pedantic)
c1771a20 10786 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10787 "ordered comparison of pointer with integer zero");
10788 else if (extra_warnings)
ba47d38d 10789 warning_at (location, OPT_Wextra,
d42ba3b8 10790 "ordered comparison of pointer with integer zero");
3e4093b6 10791 }
6aa3c60d 10792 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10793 {
10794 result_type = type1;
d42ba3b8 10795 if (pedantic)
c1771a20 10796 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10797 "ordered comparison of pointer with integer zero");
10798 else if (extra_warnings)
10799 warning_at (location, OPT_Wextra,
10800 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10801 }
10802 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10803 {
10804 result_type = type0;
ba47d38d 10805 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10806 }
10807 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10808 {
10809 result_type = type1;
ba47d38d 10810 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 10811 }
04159acf
MP
10812 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
10813 || truth_value_p (TREE_CODE (orig_op0)))
10814 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
10815 || truth_value_p (TREE_CODE (orig_op1))))
10816 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 10817 break;
64094f6a 10818
3e4093b6 10819 default:
37b2f290 10820 gcc_unreachable ();
c9fe6f9f 10821 }
8f17b5c5 10822
e57e265b
PB
10823 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10824 return error_mark_node;
10825
5bed876a
AH
10826 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10827 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10828 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10829 {
ba47d38d 10830 binary_op_error (location, code, type0, type1);
5bed876a
AH
10831 return error_mark_node;
10832 }
10833
3e4093b6 10834 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10835 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10836 &&
10837 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10838 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10839 {
2ca862e9
JM
10840 bool first_complex = (code0 == COMPLEX_TYPE);
10841 bool second_complex = (code1 == COMPLEX_TYPE);
10842 int none_complex = (!first_complex && !second_complex);
39b726dd 10843
3e4093b6 10844 if (shorten || common || short_compare)
3bf6bfcc
JJ
10845 {
10846 result_type = c_common_type (type0, type1);
0a0b3574
MM
10847 do_warn_double_promotion (result_type, type0, type1,
10848 "implicit conversion from %qT to %qT "
10849 "to match other operand of binary "
10850 "expression",
10851 location);
3bf6bfcc
JJ
10852 if (result_type == error_mark_node)
10853 return error_mark_node;
10854 }
400fbf9f 10855
2ca862e9
JM
10856 if (first_complex != second_complex
10857 && (code == PLUS_EXPR
10858 || code == MINUS_EXPR
10859 || code == MULT_EXPR
10860 || (code == TRUNC_DIV_EXPR && first_complex))
10861 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10862 && flag_signed_zeros)
10863 {
10864 /* An operation on mixed real/complex operands must be
10865 handled specially, but the language-independent code can
10866 more easily optimize the plain complex arithmetic if
10867 -fno-signed-zeros. */
10868 tree real_type = TREE_TYPE (result_type);
10869 tree real, imag;
10870 if (type0 != orig_type0 || type1 != orig_type1)
10871 {
10872 gcc_assert (may_need_excess_precision && common);
2d2e923f 10873 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10874 }
10875 if (first_complex)
10876 {
10877 if (TREE_TYPE (op0) != result_type)
68fca595 10878 op0 = convert_and_check (location, result_type, op0);
2ca862e9 10879 if (TREE_TYPE (op1) != real_type)
68fca595 10880 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
10881 }
10882 else
10883 {
10884 if (TREE_TYPE (op0) != real_type)
68fca595 10885 op0 = convert_and_check (location, real_type, op0);
2ca862e9 10886 if (TREE_TYPE (op1) != result_type)
68fca595 10887 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
10888 }
10889 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10890 return error_mark_node;
10891 if (first_complex)
10892 {
10893 op0 = c_save_expr (op0);
10894 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10895 op0, 1);
10896 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10897 op0, 1);
10898 switch (code)
10899 {
10900 case MULT_EXPR:
10901 case TRUNC_DIV_EXPR:
c4603e7c 10902 op1 = c_save_expr (op1);
2ca862e9
JM
10903 imag = build2 (resultcode, real_type, imag, op1);
10904 /* Fall through. */
10905 case PLUS_EXPR:
10906 case MINUS_EXPR:
10907 real = build2 (resultcode, real_type, real, op1);
10908 break;
10909 default:
10910 gcc_unreachable();
10911 }
10912 }
10913 else
10914 {
10915 op1 = c_save_expr (op1);
10916 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10917 op1, 1);
10918 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10919 op1, 1);
10920 switch (code)
10921 {
10922 case MULT_EXPR:
c4603e7c 10923 op0 = c_save_expr (op0);
2ca862e9
JM
10924 imag = build2 (resultcode, real_type, op0, imag);
10925 /* Fall through. */
10926 case PLUS_EXPR:
10927 real = build2 (resultcode, real_type, op0, real);
10928 break;
10929 case MINUS_EXPR:
10930 real = build2 (resultcode, real_type, op0, real);
10931 imag = build1 (NEGATE_EXPR, real_type, imag);
10932 break;
10933 default:
10934 gcc_unreachable();
10935 }
10936 }
10937 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10938 goto return_build_binary_op;
10939 }
10940
3e4093b6
RS
10941 /* For certain operations (which identify themselves by shorten != 0)
10942 if both args were extended from the same smaller type,
10943 do the arithmetic in that type and then extend.
400fbf9f 10944
3e4093b6
RS
10945 shorten !=0 and !=1 indicates a bitwise operation.
10946 For them, this optimization is safe only if
10947 both args are zero-extended or both are sign-extended.
10948 Otherwise, we might change the result.
10949 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10950 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10951
3e4093b6
RS
10952 if (shorten && none_complex)
10953 {
3e4093b6 10954 final_type = result_type;
b8698a0f 10955 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10956 shorten == -1);
3e4093b6 10957 }
88a3dbc1 10958
3e4093b6 10959 /* Shifts can be shortened if shifting right. */
2f6e4e97 10960
3e4093b6
RS
10961 if (short_shift)
10962 {
10963 int unsigned_arg;
10964 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10965
3e4093b6 10966 final_type = result_type;
abe80e6d 10967
3e4093b6 10968 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10969 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10970
3e4093b6 10971 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10972 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10973 /* We can shorten only if the shift count is less than the
10974 number of bits in the smaller type size. */
10975 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10976 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10977 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10978 {
10979 /* Do an unsigned shift if the operand was zero-extended. */
10980 result_type
10981 = c_common_signed_or_unsigned_type (unsigned_arg,
10982 TREE_TYPE (arg0));
10983 /* Convert value-to-be-shifted to that type. */
10984 if (TREE_TYPE (op0) != result_type)
10985 op0 = convert (result_type, op0);
10986 converted = 1;
abe80e6d 10987 }
88a3dbc1
RK
10988 }
10989
3e4093b6
RS
10990 /* Comparison operations are shortened too but differently.
10991 They identify themselves by setting short_compare = 1. */
56cb9733 10992
3e4093b6
RS
10993 if (short_compare)
10994 {
10995 /* Don't write &op0, etc., because that would prevent op0
10996 from being kept in a register.
10997 Instead, make copies of the our local variables and
10998 pass the copies by reference, then copy them back afterward. */
10999 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11000 enum tree_code xresultcode = resultcode;
11001 tree val
393e8e8b
MP
11002 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11003 &xresultcode);
8f17b5c5 11004
3e4093b6 11005 if (val != 0)
c9f9eb5d
AH
11006 {
11007 ret = val;
11008 goto return_build_binary_op;
11009 }
8f17b5c5 11010
3e4093b6
RS
11011 op0 = xop0, op1 = xop1;
11012 converted = 1;
11013 resultcode = xresultcode;
8f17b5c5 11014
7d882b83 11015 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
11016 {
11017 bool op0_maybe_const = true;
11018 bool op1_maybe_const = true;
11019 tree orig_op0_folded, orig_op1_folded;
11020
11021 if (in_late_binary_op)
11022 {
11023 orig_op0_folded = orig_op0;
11024 orig_op1_folded = orig_op1;
11025 }
11026 else
11027 {
11028 /* Fold for the sake of possible warnings, as in
11029 build_conditional_expr. This requires the
11030 "original" values to be folded, not just op0 and
11031 op1. */
f5178456 11032 c_inhibit_evaluation_warnings++;
928c19bb
JM
11033 op0 = c_fully_fold (op0, require_constant_value,
11034 &op0_maybe_const);
11035 op1 = c_fully_fold (op1, require_constant_value,
11036 &op1_maybe_const);
f5178456 11037 c_inhibit_evaluation_warnings--;
928c19bb
JM
11038 orig_op0_folded = c_fully_fold (orig_op0,
11039 require_constant_value,
11040 NULL);
11041 orig_op1_folded = c_fully_fold (orig_op1,
11042 require_constant_value,
11043 NULL);
11044 }
11045
11046 if (warn_sign_compare)
11047 warn_for_sign_compare (location, orig_op0_folded,
11048 orig_op1_folded, op0, op1,
11049 result_type, resultcode);
5c2f94b4 11050 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11051 {
11052 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11053 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11054 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11055 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11056 }
3e4093b6 11057 }
2ad1815d 11058 }
64094f6a 11059 }
64094f6a 11060
3e4093b6
RS
11061 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11062 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11063 Then the expression will be built.
11064 It will be given type FINAL_TYPE if that is nonzero;
11065 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11066
3e4093b6
RS
11067 if (!result_type)
11068 {
ba47d38d 11069 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11070 return error_mark_node;
11071 }
400fbf9f 11072
3e4093b6 11073 if (build_type == NULL_TREE)
8ce94e44
JM
11074 {
11075 build_type = result_type;
180f8dbb
JM
11076 if ((type0 != orig_type0 || type1 != orig_type1)
11077 && !boolean_op)
8ce94e44
JM
11078 {
11079 gcc_assert (may_need_excess_precision && common);
2d2e923f 11080 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11081 }
11082 }
400fbf9f 11083
2d2e923f
MLI
11084 if (!converted)
11085 {
68fca595
MP
11086 op0 = ep_convert_and_check (location, result_type, op0,
11087 semantic_result_type);
11088 op1 = ep_convert_and_check (location, result_type, op1,
11089 semantic_result_type);
2d2e923f
MLI
11090
11091 /* This can happen if one operand has a vector type, and the other
11092 has a different type. */
11093 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11094 return error_mark_node;
11095 }
11096
f8ed5150
MP
11097 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11098 | SANITIZE_FLOAT_DIVIDE))
de5a5fa1 11099 && current_function_decl != 0
ce6923c5
MP
11100 && !lookup_attribute ("no_sanitize_undefined",
11101 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
11102 && (doing_div_or_mod || doing_shift))
11103 {
11104 /* OP0 and/or OP1 might have side-effects. */
11105 op0 = c_save_expr (op0);
11106 op1 = c_save_expr (op1);
11107 op0 = c_fully_fold (op0, false, NULL);
11108 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11109 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11110 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11111 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11112 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11113 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11114 }
11115
c9f9eb5d 11116 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11117 if (int_const_or_overflow)
11118 ret = (require_constant_value
db3927fb
AH
11119 ? fold_build2_initializer_loc (location, resultcode, build_type,
11120 op0, op1)
11121 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11122 else
11123 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11124 if (final_type != 0)
11125 ret = convert (final_type, ret);
11126
11127 return_build_binary_op:
11128 gcc_assert (ret != error_mark_node);
928c19bb
JM
11129 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11130 ret = (int_operands
11131 ? note_integer_operands (ret)
11132 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11133 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11134 && !in_late_binary_op)
11135 ret = note_integer_operands (ret);
2d2e923f
MLI
11136 if (semantic_result_type)
11137 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11138 protected_set_expr_location (ret, location);
de5a5fa1 11139
a24d975c 11140 if (instrument_expr != NULL)
de5a5fa1
MP
11141 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11142 instrument_expr, ret);
11143
c9f9eb5d 11144 return ret;
400fbf9f 11145}
85498824
JM
11146
11147
11148/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11149 purpose. LOCATION is the source location for the expression. */
85498824
JM
11150
11151tree
ba47d38d 11152c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11153{
928c19bb
JM
11154 bool int_const, int_operands;
11155
85498824
JM
11156 switch (TREE_CODE (TREE_TYPE (expr)))
11157 {
11158 case ARRAY_TYPE:
ba47d38d 11159 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11160 return error_mark_node;
11161
11162 case RECORD_TYPE:
ba47d38d 11163 error_at (location, "used struct type value where scalar is required");
85498824
JM
11164 return error_mark_node;
11165
11166 case UNION_TYPE:
ba47d38d 11167 error_at (location, "used union type value where scalar is required");
85498824
JM
11168 return error_mark_node;
11169
04af8788
NP
11170 case VOID_TYPE:
11171 error_at (location, "void value not ignored as it ought to be");
11172 return error_mark_node;
11173
46bdb9cf
JM
11174 case FUNCTION_TYPE:
11175 gcc_unreachable ();
11176
d246ab4f
AS
11177 case VECTOR_TYPE:
11178 error_at (location, "used vector type where scalar is required");
11179 return error_mark_node;
11180
85498824
JM
11181 default:
11182 break;
11183 }
11184
928c19bb
JM
11185 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11186 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11187 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11188 {
11189 expr = remove_c_maybe_const_expr (expr);
11190 expr = build2 (NE_EXPR, integer_type_node, expr,
11191 convert (TREE_TYPE (expr), integer_zero_node));
11192 expr = note_integer_operands (expr);
11193 }
11194 else
11195 /* ??? Should we also give an error for vectors rather than leaving
11196 those to give errors later? */
11197 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11198
11199 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11200 {
11201 if (TREE_OVERFLOW (expr))
11202 return expr;
11203 else
11204 return note_integer_operands (expr);
11205 }
11206 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11207 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11208 return expr;
85498824 11209}
73f397d4
JM
11210\f
11211
11212/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11213 required. */
11214
11215tree
51eed280 11216c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11217{
11218 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11219 {
11220 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11221 /* Executing a compound literal inside a function reinitializes
11222 it. */
11223 if (!TREE_STATIC (decl))
11224 *se = true;
11225 return decl;
11226 }
11227 else
11228 return expr;
11229}
953ff289 11230\f
c0220ea4 11231/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11232
11233tree
11234c_begin_omp_parallel (void)
11235{
11236 tree block;
11237
11238 keep_next_level ();
11239 block = c_begin_compound_stmt (true);
11240
11241 return block;
11242}
11243
c2255bc4
AH
11244/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11245 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11246
953ff289 11247tree
c2255bc4 11248c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11249{
11250 tree stmt;
11251
c2255bc4 11252 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11253
11254 stmt = make_node (OMP_PARALLEL);
11255 TREE_TYPE (stmt) = void_type_node;
11256 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11257 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11258 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11259
11260 return add_stmt (stmt);
11261}
11262
a68ab351
JJ
11263/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11264
11265tree
11266c_begin_omp_task (void)
11267{
11268 tree block;
11269
11270 keep_next_level ();
11271 block = c_begin_compound_stmt (true);
11272
11273 return block;
11274}
11275
c2255bc4
AH
11276/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11277 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11278
11279tree
c2255bc4 11280c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11281{
11282 tree stmt;
11283
c2255bc4 11284 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11285
11286 stmt = make_node (OMP_TASK);
11287 TREE_TYPE (stmt) = void_type_node;
11288 OMP_TASK_CLAUSES (stmt) = clauses;
11289 OMP_TASK_BODY (stmt) = block;
c2255bc4 11290 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11291
11292 return add_stmt (stmt);
11293}
11294
acf0174b
JJ
11295/* Generate GOMP_cancel call for #pragma omp cancel. */
11296
11297void
11298c_finish_omp_cancel (location_t loc, tree clauses)
11299{
11300 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11301 int mask = 0;
11302 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11303 mask = 1;
11304 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11305 mask = 2;
11306 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11307 mask = 4;
11308 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11309 mask = 8;
11310 else
11311 {
11312 error_at (loc, "%<#pragma omp cancel must specify one of "
11313 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11314 "clauses");
11315 return;
11316 }
11317 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11318 if (ifc != NULL_TREE)
11319 {
11320 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11321 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11322 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11323 build_zero_cst (type));
11324 }
11325 else
11326 ifc = boolean_true_node;
11327 tree stmt = build_call_expr_loc (loc, fn, 2,
11328 build_int_cst (integer_type_node, mask),
11329 ifc);
11330 add_stmt (stmt);
11331}
11332
11333/* Generate GOMP_cancellation_point call for
11334 #pragma omp cancellation point. */
11335
11336void
11337c_finish_omp_cancellation_point (location_t loc, tree clauses)
11338{
11339 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11340 int mask = 0;
11341 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11342 mask = 1;
11343 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11344 mask = 2;
11345 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11346 mask = 4;
11347 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11348 mask = 8;
11349 else
11350 {
11351 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11352 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11353 "clauses");
11354 return;
11355 }
11356 tree stmt = build_call_expr_loc (loc, fn, 1,
11357 build_int_cst (integer_type_node, mask));
11358 add_stmt (stmt);
11359}
11360
11361/* Helper function for handle_omp_array_sections. Called recursively
11362 to handle multiple array-section-subscripts. C is the clause,
11363 T current expression (initially OMP_CLAUSE_DECL), which is either
11364 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11365 expression if specified, TREE_VALUE length expression if specified,
11366 TREE_CHAIN is what it has been specified after, or some decl.
11367 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11368 set to true if any of the array-section-subscript could have length
11369 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11370 first array-section-subscript which is known not to have length
11371 of one. Given say:
11372 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11373 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11374 all are or may have length of 1, array-section-subscript [:2] is the
11375 first one knonwn not to have length 1. For array-section-subscript
11376 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11377 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11378 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11379 case though, as some lengths could be zero. */
11380
11381static tree
11382handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11383 bool &maybe_zero_len, unsigned int &first_non_one)
11384{
11385 tree ret, low_bound, length, type;
11386 if (TREE_CODE (t) != TREE_LIST)
11387 {
7a0ca710 11388 if (error_operand_p (t))
acf0174b
JJ
11389 return error_mark_node;
11390 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11391 {
11392 if (DECL_P (t))
11393 error_at (OMP_CLAUSE_LOCATION (c),
11394 "%qD is not a variable in %qs clause", t,
11395 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11396 else
11397 error_at (OMP_CLAUSE_LOCATION (c),
11398 "%qE is not a variable in %qs clause", t,
11399 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11400 return error_mark_node;
11401 }
11402 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11403 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11404 {
11405 error_at (OMP_CLAUSE_LOCATION (c),
11406 "%qD is threadprivate variable in %qs clause", t,
11407 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11408 return error_mark_node;
11409 }
11410 return t;
11411 }
11412
11413 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11414 maybe_zero_len, first_non_one);
11415 if (ret == error_mark_node || ret == NULL_TREE)
11416 return ret;
11417
11418 type = TREE_TYPE (ret);
11419 low_bound = TREE_PURPOSE (t);
11420 length = TREE_VALUE (t);
11421
11422 if (low_bound == error_mark_node || length == error_mark_node)
11423 return error_mark_node;
11424
11425 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11426 {
11427 error_at (OMP_CLAUSE_LOCATION (c),
11428 "low bound %qE of array section does not have integral type",
11429 low_bound);
11430 return error_mark_node;
11431 }
11432 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11433 {
11434 error_at (OMP_CLAUSE_LOCATION (c),
11435 "length %qE of array section does not have integral type",
11436 length);
11437 return error_mark_node;
11438 }
11439 if (low_bound
11440 && TREE_CODE (low_bound) == INTEGER_CST
11441 && TYPE_PRECISION (TREE_TYPE (low_bound))
11442 > TYPE_PRECISION (sizetype))
11443 low_bound = fold_convert (sizetype, low_bound);
11444 if (length
11445 && TREE_CODE (length) == INTEGER_CST
11446 && TYPE_PRECISION (TREE_TYPE (length))
11447 > TYPE_PRECISION (sizetype))
11448 length = fold_convert (sizetype, length);
11449 if (low_bound == NULL_TREE)
11450 low_bound = integer_zero_node;
11451
11452 if (length != NULL_TREE)
11453 {
11454 if (!integer_nonzerop (length))
11455 maybe_zero_len = true;
11456 if (first_non_one == types.length ()
11457 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11458 first_non_one++;
11459 }
11460 if (TREE_CODE (type) == ARRAY_TYPE)
11461 {
11462 if (length == NULL_TREE
11463 && (TYPE_DOMAIN (type) == NULL_TREE
11464 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11465 {
11466 error_at (OMP_CLAUSE_LOCATION (c),
11467 "for unknown bound array type length expression must "
11468 "be specified");
11469 return error_mark_node;
11470 }
11471 if (TREE_CODE (low_bound) == INTEGER_CST
11472 && tree_int_cst_sgn (low_bound) == -1)
11473 {
11474 error_at (OMP_CLAUSE_LOCATION (c),
11475 "negative low bound in array section in %qs clause",
11476 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11477 return error_mark_node;
11478 }
11479 if (length != NULL_TREE
11480 && TREE_CODE (length) == INTEGER_CST
11481 && tree_int_cst_sgn (length) == -1)
11482 {
11483 error_at (OMP_CLAUSE_LOCATION (c),
11484 "negative length in array section in %qs clause",
11485 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11486 return error_mark_node;
11487 }
11488 if (TYPE_DOMAIN (type)
11489 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11490 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11491 == INTEGER_CST)
11492 {
11493 tree size = size_binop (PLUS_EXPR,
11494 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11495 size_one_node);
11496 if (TREE_CODE (low_bound) == INTEGER_CST)
11497 {
11498 if (tree_int_cst_lt (size, low_bound))
11499 {
11500 error_at (OMP_CLAUSE_LOCATION (c),
11501 "low bound %qE above array section size "
11502 "in %qs clause", low_bound,
11503 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11504 return error_mark_node;
11505 }
11506 if (tree_int_cst_equal (size, low_bound))
11507 maybe_zero_len = true;
11508 else if (length == NULL_TREE
11509 && first_non_one == types.length ()
11510 && tree_int_cst_equal
11511 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11512 low_bound))
11513 first_non_one++;
11514 }
11515 else if (length == NULL_TREE)
11516 {
11517 maybe_zero_len = true;
11518 if (first_non_one == types.length ())
11519 first_non_one++;
11520 }
11521 if (length && TREE_CODE (length) == INTEGER_CST)
11522 {
11523 if (tree_int_cst_lt (size, length))
11524 {
11525 error_at (OMP_CLAUSE_LOCATION (c),
11526 "length %qE above array section size "
11527 "in %qs clause", length,
11528 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11529 return error_mark_node;
11530 }
11531 if (TREE_CODE (low_bound) == INTEGER_CST)
11532 {
11533 tree lbpluslen
11534 = size_binop (PLUS_EXPR,
11535 fold_convert (sizetype, low_bound),
11536 fold_convert (sizetype, length));
11537 if (TREE_CODE (lbpluslen) == INTEGER_CST
11538 && tree_int_cst_lt (size, lbpluslen))
11539 {
11540 error_at (OMP_CLAUSE_LOCATION (c),
11541 "high bound %qE above array section size "
11542 "in %qs clause", lbpluslen,
11543 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11544 return error_mark_node;
11545 }
11546 }
11547 }
11548 }
11549 else if (length == NULL_TREE)
11550 {
11551 maybe_zero_len = true;
11552 if (first_non_one == types.length ())
11553 first_non_one++;
11554 }
11555
11556 /* For [lb:] we will need to evaluate lb more than once. */
11557 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11558 {
11559 tree lb = c_save_expr (low_bound);
11560 if (lb != low_bound)
11561 {
11562 TREE_PURPOSE (t) = lb;
11563 low_bound = lb;
11564 }
11565 }
11566 }
11567 else if (TREE_CODE (type) == POINTER_TYPE)
11568 {
11569 if (length == NULL_TREE)
11570 {
11571 error_at (OMP_CLAUSE_LOCATION (c),
11572 "for pointer type length expression must be specified");
11573 return error_mark_node;
11574 }
11575 /* If there is a pointer type anywhere but in the very first
11576 array-section-subscript, the array section can't be contiguous. */
11577 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11578 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11579 {
11580 error_at (OMP_CLAUSE_LOCATION (c),
11581 "array section is not contiguous in %qs clause",
11582 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11583 return error_mark_node;
11584 }
11585 }
11586 else
11587 {
11588 error_at (OMP_CLAUSE_LOCATION (c),
11589 "%qE does not have pointer or array type", ret);
11590 return error_mark_node;
11591 }
11592 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11593 types.safe_push (TREE_TYPE (ret));
11594 /* We will need to evaluate lb more than once. */
11595 tree lb = c_save_expr (low_bound);
11596 if (lb != low_bound)
11597 {
11598 TREE_PURPOSE (t) = lb;
11599 low_bound = lb;
11600 }
11601 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11602 return ret;
11603}
11604
11605/* Handle array sections for clause C. */
11606
11607static bool
11608handle_omp_array_sections (tree c)
11609{
11610 bool maybe_zero_len = false;
11611 unsigned int first_non_one = 0;
11612 vec<tree> types = vNULL;
11613 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11614 maybe_zero_len, first_non_one);
11615 if (first == error_mark_node)
11616 {
11617 types.release ();
11618 return true;
11619 }
11620 if (first == NULL_TREE)
11621 {
11622 types.release ();
11623 return false;
11624 }
11625 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11626 {
11627 tree t = OMP_CLAUSE_DECL (c);
11628 tree tem = NULL_TREE;
11629 types.release ();
11630 /* Need to evaluate side effects in the length expressions
11631 if any. */
11632 while (TREE_CODE (t) == TREE_LIST)
11633 {
11634 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11635 {
11636 if (tem == NULL_TREE)
11637 tem = TREE_VALUE (t);
11638 else
11639 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11640 TREE_VALUE (t), tem);
11641 }
11642 t = TREE_CHAIN (t);
11643 }
11644 if (tem)
11645 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11646 first = c_fully_fold (first, false, NULL);
11647 OMP_CLAUSE_DECL (c) = first;
11648 }
11649 else
11650 {
11651 unsigned int num = types.length (), i;
11652 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11653 tree condition = NULL_TREE;
11654
11655 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11656 maybe_zero_len = true;
11657
11658 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11659 t = TREE_CHAIN (t))
11660 {
11661 tree low_bound = TREE_PURPOSE (t);
11662 tree length = TREE_VALUE (t);
11663
11664 i--;
11665 if (low_bound
11666 && TREE_CODE (low_bound) == INTEGER_CST
11667 && TYPE_PRECISION (TREE_TYPE (low_bound))
11668 > TYPE_PRECISION (sizetype))
11669 low_bound = fold_convert (sizetype, low_bound);
11670 if (length
11671 && TREE_CODE (length) == INTEGER_CST
11672 && TYPE_PRECISION (TREE_TYPE (length))
11673 > TYPE_PRECISION (sizetype))
11674 length = fold_convert (sizetype, length);
11675 if (low_bound == NULL_TREE)
11676 low_bound = integer_zero_node;
11677 if (!maybe_zero_len && i > first_non_one)
11678 {
11679 if (integer_nonzerop (low_bound))
11680 goto do_warn_noncontiguous;
11681 if (length != NULL_TREE
11682 && TREE_CODE (length) == INTEGER_CST
11683 && TYPE_DOMAIN (types[i])
11684 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11685 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11686 == INTEGER_CST)
11687 {
11688 tree size;
11689 size = size_binop (PLUS_EXPR,
11690 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11691 size_one_node);
11692 if (!tree_int_cst_equal (length, size))
11693 {
11694 do_warn_noncontiguous:
11695 error_at (OMP_CLAUSE_LOCATION (c),
11696 "array section is not contiguous in %qs "
11697 "clause",
11698 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11699 types.release ();
11700 return true;
11701 }
11702 }
11703 if (length != NULL_TREE
11704 && TREE_SIDE_EFFECTS (length))
11705 {
11706 if (side_effects == NULL_TREE)
11707 side_effects = length;
11708 else
11709 side_effects = build2 (COMPOUND_EXPR,
11710 TREE_TYPE (side_effects),
11711 length, side_effects);
11712 }
11713 }
11714 else
11715 {
11716 tree l;
11717
11718 if (i > first_non_one && length && integer_nonzerop (length))
11719 continue;
11720 if (length)
11721 l = fold_convert (sizetype, length);
11722 else
11723 {
11724 l = size_binop (PLUS_EXPR,
11725 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11726 size_one_node);
11727 l = size_binop (MINUS_EXPR, l,
11728 fold_convert (sizetype, low_bound));
11729 }
11730 if (i > first_non_one)
11731 {
11732 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11733 size_zero_node);
11734 if (condition == NULL_TREE)
11735 condition = l;
11736 else
11737 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11738 l, condition);
11739 }
11740 else if (size == NULL_TREE)
11741 {
11742 size = size_in_bytes (TREE_TYPE (types[i]));
11743 size = size_binop (MULT_EXPR, size, l);
11744 if (condition)
11745 size = fold_build3 (COND_EXPR, sizetype, condition,
11746 size, size_zero_node);
11747 }
11748 else
11749 size = size_binop (MULT_EXPR, size, l);
11750 }
11751 }
11752 types.release ();
11753 if (side_effects)
11754 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11755 first = c_fully_fold (first, false, NULL);
11756 OMP_CLAUSE_DECL (c) = first;
11757 if (size)
11758 size = c_fully_fold (size, false, NULL);
11759 OMP_CLAUSE_SIZE (c) = size;
11760 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11761 return false;
11762 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11763 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11764 if (!c_mark_addressable (t))
11765 return false;
11766 OMP_CLAUSE_DECL (c2) = t;
11767 t = build_fold_addr_expr (first);
11768 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11769 tree ptr = OMP_CLAUSE_DECL (c2);
11770 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11771 ptr = build_fold_addr_expr (ptr);
11772 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11773 ptrdiff_type_node, t,
11774 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11775 ptrdiff_type_node, ptr));
11776 t = c_fully_fold (t, false, NULL);
11777 OMP_CLAUSE_SIZE (c2) = t;
11778 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11779 OMP_CLAUSE_CHAIN (c) = c2;
11780 }
11781 return false;
11782}
11783
11784/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11785 an inline call. But, remap
11786 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11787 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11788
11789static tree
11790c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11791 tree decl, tree placeholder)
11792{
11793 copy_body_data id;
b787e7a2 11794 hash_map<tree, tree> decl_map;
acf0174b 11795
b787e7a2
TS
11796 decl_map.put (omp_decl1, placeholder);
11797 decl_map.put (omp_decl2, decl);
acf0174b
JJ
11798 memset (&id, 0, sizeof (id));
11799 id.src_fn = DECL_CONTEXT (omp_decl1);
11800 id.dst_fn = current_function_decl;
11801 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 11802 id.decl_map = &decl_map;
acf0174b
JJ
11803
11804 id.copy_decl = copy_decl_no_change;
11805 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11806 id.transform_new_cfg = true;
11807 id.transform_return_to_modify = false;
11808 id.transform_lang_insert_block = NULL;
11809 id.eh_lp_nr = 0;
11810 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
11811 return stmt;
11812}
11813
11814/* Helper function of c_finish_omp_clauses, called via walk_tree.
11815 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11816
11817static tree
11818c_find_omp_placeholder_r (tree *tp, int *, void *data)
11819{
11820 if (*tp == (tree) data)
11821 return *tp;
11822 return NULL_TREE;
11823}
11824
953ff289
DN
11825/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11826 Remove any elements from the list that are invalid. */
11827
11828tree
11829c_finish_omp_clauses (tree clauses)
11830{
11831 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11832 bitmap_head aligned_head;
f3316c6d 11833 tree c, t, *pc;
acf0174b
JJ
11834 bool branch_seen = false;
11835 bool copyprivate_seen = false;
11836 tree *nowait_clause = NULL;
953ff289
DN
11837
11838 bitmap_obstack_initialize (NULL);
11839 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11840 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11841 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11842 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11843
11844 for (pc = &clauses, c = clauses; c ; c = *pc)
11845 {
11846 bool remove = false;
11847 bool need_complete = false;
11848 bool need_implicitly_determined = false;
11849
aaf46ef9 11850 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11851 {
11852 case OMP_CLAUSE_SHARED:
953ff289
DN
11853 need_implicitly_determined = true;
11854 goto check_dup_generic;
11855
11856 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11857 need_complete = true;
11858 need_implicitly_determined = true;
11859 goto check_dup_generic;
11860
11861 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11862 need_implicitly_determined = true;
11863 t = OMP_CLAUSE_DECL (c);
acf0174b 11864 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11865 && (FLOAT_TYPE_P (TREE_TYPE (t))
11866 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11867 {
11868 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11869 const char *r_name = NULL;
11870
11871 switch (r_code)
11872 {
11873 case PLUS_EXPR:
11874 case MULT_EXPR:
11875 case MINUS_EXPR:
652fea39 11876 break;
20906c66 11877 case MIN_EXPR:
652fea39
JJ
11878 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11879 r_name = "min";
11880 break;
20906c66 11881 case MAX_EXPR:
652fea39
JJ
11882 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11883 r_name = "max";
953ff289
DN
11884 break;
11885 case BIT_AND_EXPR:
11886 r_name = "&";
11887 break;
11888 case BIT_XOR_EXPR:
11889 r_name = "^";
11890 break;
11891 case BIT_IOR_EXPR:
11892 r_name = "|";
11893 break;
11894 case TRUTH_ANDIF_EXPR:
652fea39
JJ
11895 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11896 r_name = "&&";
953ff289
DN
11897 break;
11898 case TRUTH_ORIF_EXPR:
652fea39
JJ
11899 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11900 r_name = "||";
953ff289
DN
11901 break;
11902 default:
11903 gcc_unreachable ();
11904 }
11905 if (r_name)
11906 {
c2255bc4
AH
11907 error_at (OMP_CLAUSE_LOCATION (c),
11908 "%qE has invalid type for %<reduction(%s)%>",
11909 t, r_name);
953ff289 11910 remove = true;
ee1d5a02 11911 break;
953ff289
DN
11912 }
11913 }
acf0174b
JJ
11914 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11915 {
11916 error_at (OMP_CLAUSE_LOCATION (c),
11917 "user defined reduction not found for %qD", t);
11918 remove = true;
ee1d5a02 11919 break;
acf0174b
JJ
11920 }
11921 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11922 {
11923 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11924 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11925 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11926 VAR_DECL, NULL_TREE, type);
11927 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11928 DECL_ARTIFICIAL (placeholder) = 1;
11929 DECL_IGNORED_P (placeholder) = 1;
11930 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11931 c_mark_addressable (placeholder);
11932 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11933 c_mark_addressable (OMP_CLAUSE_DECL (c));
11934 OMP_CLAUSE_REDUCTION_MERGE (c)
11935 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11936 TREE_VEC_ELT (list, 0),
11937 TREE_VEC_ELT (list, 1),
11938 OMP_CLAUSE_DECL (c), placeholder);
11939 OMP_CLAUSE_REDUCTION_MERGE (c)
11940 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11941 void_type_node, NULL_TREE,
11942 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11943 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11944 if (TREE_VEC_LENGTH (list) == 6)
11945 {
11946 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11947 c_mark_addressable (OMP_CLAUSE_DECL (c));
11948 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11949 c_mark_addressable (placeholder);
11950 tree init = TREE_VEC_ELT (list, 5);
11951 if (init == error_mark_node)
11952 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11953 OMP_CLAUSE_REDUCTION_INIT (c)
11954 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11955 TREE_VEC_ELT (list, 3),
11956 OMP_CLAUSE_DECL (c), placeholder);
11957 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11958 OMP_CLAUSE_REDUCTION_INIT (c)
11959 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11960 OMP_CLAUSE_REDUCTION_INIT (c));
11961 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11962 c_find_omp_placeholder_r,
11963 placeholder, NULL))
11964 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11965 }
11966 else
11967 {
11968 tree init;
11969 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11970 init = build_constructor (TREE_TYPE (t), NULL);
11971 else
11972 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11973 OMP_CLAUSE_REDUCTION_INIT (c)
11974 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11975 }
11976 OMP_CLAUSE_REDUCTION_INIT (c)
11977 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11978 void_type_node, NULL_TREE,
11979 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11980 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11981 }
953ff289
DN
11982 goto check_dup_generic;
11983
11984 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11985 copyprivate_seen = true;
11986 if (nowait_clause)
11987 {
11988 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11989 "%<nowait%> clause must not be used together "
11990 "with %<copyprivate%>");
11991 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11992 nowait_clause = NULL;
11993 }
953ff289
DN
11994 goto check_dup_generic;
11995
11996 case OMP_CLAUSE_COPYIN:
953ff289
DN
11997 t = OMP_CLAUSE_DECL (c);
11998 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11999 {
c2255bc4
AH
12000 error_at (OMP_CLAUSE_LOCATION (c),
12001 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 12002 remove = true;
ee1d5a02 12003 break;
953ff289
DN
12004 }
12005 goto check_dup_generic;
12006
acf0174b
JJ
12007 case OMP_CLAUSE_LINEAR:
12008 t = OMP_CLAUSE_DECL (c);
12009 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
12010 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
12011 {
12012 error_at (OMP_CLAUSE_LOCATION (c),
12013 "linear clause applied to non-integral non-pointer "
12014 "variable with type %qT", TREE_TYPE (t));
12015 remove = true;
12016 break;
12017 }
12018 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
12019 {
12020 tree s = OMP_CLAUSE_LINEAR_STEP (c);
12021 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
12022 OMP_CLAUSE_DECL (c), s);
12023 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12024 sizetype, s, OMP_CLAUSE_DECL (c));
12025 if (s == error_mark_node)
12026 s = size_one_node;
12027 OMP_CLAUSE_LINEAR_STEP (c) = s;
12028 }
da6f124d
JJ
12029 else
12030 OMP_CLAUSE_LINEAR_STEP (c)
12031 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
12032 goto check_dup_generic;
12033
953ff289
DN
12034 check_dup_generic:
12035 t = OMP_CLAUSE_DECL (c);
12036 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12037 {
c2255bc4 12038 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12039 "%qE is not a variable in clause %qs", t,
12040 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12041 remove = true;
12042 }
12043 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12044 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12045 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12046 {
c2255bc4
AH
12047 error_at (OMP_CLAUSE_LOCATION (c),
12048 "%qE appears more than once in data clauses", t);
953ff289
DN
12049 remove = true;
12050 }
12051 else
12052 bitmap_set_bit (&generic_head, DECL_UID (t));
12053 break;
12054
12055 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12056 t = OMP_CLAUSE_DECL (c);
12057 need_complete = true;
12058 need_implicitly_determined = true;
12059 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12060 {
c2255bc4
AH
12061 error_at (OMP_CLAUSE_LOCATION (c),
12062 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12063 remove = true;
12064 }
12065 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12066 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12067 {
c2255bc4
AH
12068 error_at (OMP_CLAUSE_LOCATION (c),
12069 "%qE appears more than once in data clauses", t);
953ff289
DN
12070 remove = true;
12071 }
12072 else
12073 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12074 break;
12075
12076 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12077 t = OMP_CLAUSE_DECL (c);
12078 need_complete = true;
12079 need_implicitly_determined = true;
12080 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12081 {
c2255bc4
AH
12082 error_at (OMP_CLAUSE_LOCATION (c),
12083 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12084 remove = true;
12085 }
12086 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12087 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12088 {
c2255bc4
AH
12089 error_at (OMP_CLAUSE_LOCATION (c),
12090 "%qE appears more than once in data clauses", t);
953ff289
DN
12091 remove = true;
12092 }
12093 else
12094 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12095 break;
12096
acf0174b
JJ
12097 case OMP_CLAUSE_ALIGNED:
12098 t = OMP_CLAUSE_DECL (c);
12099 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12100 {
12101 error_at (OMP_CLAUSE_LOCATION (c),
12102 "%qE is not a variable in %<aligned%> clause", t);
12103 remove = true;
12104 }
5a9785fb
JJ
12105 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12106 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12107 {
12108 error_at (OMP_CLAUSE_LOCATION (c),
12109 "%qE in %<aligned%> clause is neither a pointer nor "
12110 "an array", t);
12111 remove = true;
12112 }
acf0174b
JJ
12113 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12114 {
12115 error_at (OMP_CLAUSE_LOCATION (c),
12116 "%qE appears more than once in %<aligned%> clauses",
12117 t);
12118 remove = true;
12119 }
12120 else
12121 bitmap_set_bit (&aligned_head, DECL_UID (t));
12122 break;
12123
12124 case OMP_CLAUSE_DEPEND:
12125 t = OMP_CLAUSE_DECL (c);
12126 if (TREE_CODE (t) == TREE_LIST)
12127 {
12128 if (handle_omp_array_sections (c))
12129 remove = true;
12130 break;
12131 }
12132 if (t == error_mark_node)
12133 remove = true;
12134 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12135 {
12136 error_at (OMP_CLAUSE_LOCATION (c),
12137 "%qE is not a variable in %<depend%> clause", t);
12138 remove = true;
12139 }
12140 else if (!c_mark_addressable (t))
12141 remove = true;
12142 break;
12143
12144 case OMP_CLAUSE_MAP:
12145 case OMP_CLAUSE_TO:
12146 case OMP_CLAUSE_FROM:
12147 t = OMP_CLAUSE_DECL (c);
12148 if (TREE_CODE (t) == TREE_LIST)
12149 {
12150 if (handle_omp_array_sections (c))
12151 remove = true;
12152 else
12153 {
12154 t = OMP_CLAUSE_DECL (c);
b17a8b07 12155 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12156 {
12157 error_at (OMP_CLAUSE_LOCATION (c),
12158 "array section does not have mappable type "
12159 "in %qs clause",
12160 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12161 remove = true;
12162 }
12163 }
12164 break;
12165 }
12166 if (t == error_mark_node)
12167 remove = true;
12168 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12169 {
12170 error_at (OMP_CLAUSE_LOCATION (c),
12171 "%qE is not a variable in %qs clause", t,
12172 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12173 remove = true;
12174 }
12175 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12176 {
12177 error_at (OMP_CLAUSE_LOCATION (c),
12178 "%qD is threadprivate variable in %qs clause", t,
12179 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12180 remove = true;
12181 }
12182 else if (!c_mark_addressable (t))
12183 remove = true;
b17a8b07
TS
12184 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12185 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12186 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12187 {
12188 error_at (OMP_CLAUSE_LOCATION (c),
12189 "%qD does not have a mappable type in %qs clause", t,
12190 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12191 remove = true;
12192 }
12193 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12194 {
12195 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12196 error ("%qD appears more than once in motion clauses", t);
12197 else
12198 error ("%qD appears more than once in map clauses", t);
12199 remove = true;
12200 }
12201 else
12202 bitmap_set_bit (&generic_head, DECL_UID (t));
12203 break;
12204
12205 case OMP_CLAUSE_UNIFORM:
12206 t = OMP_CLAUSE_DECL (c);
12207 if (TREE_CODE (t) != PARM_DECL)
12208 {
12209 if (DECL_P (t))
12210 error_at (OMP_CLAUSE_LOCATION (c),
12211 "%qD is not an argument in %<uniform%> clause", t);
12212 else
12213 error_at (OMP_CLAUSE_LOCATION (c),
12214 "%qE is not an argument in %<uniform%> clause", t);
12215 remove = true;
ee1d5a02 12216 break;
acf0174b 12217 }
ee1d5a02 12218 goto check_dup_generic;
acf0174b
JJ
12219
12220 case OMP_CLAUSE_NOWAIT:
12221 if (copyprivate_seen)
12222 {
12223 error_at (OMP_CLAUSE_LOCATION (c),
12224 "%<nowait%> clause must not be used together "
12225 "with %<copyprivate%>");
12226 remove = true;
12227 break;
12228 }
12229 nowait_clause = pc;
12230 pc = &OMP_CLAUSE_CHAIN (c);
12231 continue;
12232
953ff289
DN
12233 case OMP_CLAUSE_IF:
12234 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12235 case OMP_CLAUSE_NUM_TEAMS:
12236 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12237 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12238 case OMP_CLAUSE_ORDERED:
12239 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12240 case OMP_CLAUSE_UNTIED:
12241 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12242 case OMP_CLAUSE_FINAL:
12243 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12244 case OMP_CLAUSE_SAFELEN:
12245 case OMP_CLAUSE_SIMDLEN:
12246 case OMP_CLAUSE_DEVICE:
12247 case OMP_CLAUSE_DIST_SCHEDULE:
12248 case OMP_CLAUSE_PARALLEL:
12249 case OMP_CLAUSE_FOR:
12250 case OMP_CLAUSE_SECTIONS:
12251 case OMP_CLAUSE_TASKGROUP:
12252 case OMP_CLAUSE_PROC_BIND:
9a771876 12253 case OMP_CLAUSE__CILK_FOR_COUNT_:
acf0174b
JJ
12254 pc = &OMP_CLAUSE_CHAIN (c);
12255 continue;
12256
12257 case OMP_CLAUSE_INBRANCH:
12258 case OMP_CLAUSE_NOTINBRANCH:
12259 if (branch_seen)
12260 {
12261 error_at (OMP_CLAUSE_LOCATION (c),
12262 "%<inbranch%> clause is incompatible with "
12263 "%<notinbranch%>");
12264 remove = true;
12265 break;
12266 }
12267 branch_seen = true;
953ff289
DN
12268 pc = &OMP_CLAUSE_CHAIN (c);
12269 continue;
12270
12271 default:
12272 gcc_unreachable ();
12273 }
12274
12275 if (!remove)
12276 {
12277 t = OMP_CLAUSE_DECL (c);
12278
12279 if (need_complete)
12280 {
12281 t = require_complete_type (t);
12282 if (t == error_mark_node)
12283 remove = true;
12284 }
12285
12286 if (need_implicitly_determined)
12287 {
12288 const char *share_name = NULL;
12289
12290 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12291 share_name = "threadprivate";
12292 else switch (c_omp_predetermined_sharing (t))
12293 {
12294 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12295 break;
12296 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12297 /* const vars may be specified in firstprivate clause. */
12298 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12299 && TREE_READONLY (t))
12300 break;
953ff289
DN
12301 share_name = "shared";
12302 break;
12303 case OMP_CLAUSE_DEFAULT_PRIVATE:
12304 share_name = "private";
12305 break;
12306 default:
12307 gcc_unreachable ();
12308 }
12309 if (share_name)
12310 {
c2255bc4
AH
12311 error_at (OMP_CLAUSE_LOCATION (c),
12312 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12313 t, share_name,
12314 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12315 remove = true;
12316 }
12317 }
12318 }
12319
12320 if (remove)
12321 *pc = OMP_CLAUSE_CHAIN (c);
12322 else
12323 pc = &OMP_CLAUSE_CHAIN (c);
12324 }
12325
12326 bitmap_obstack_release (NULL);
12327 return clauses;
12328}
9ae165a0 12329
0a35513e
AH
12330/* Create a transaction node. */
12331
12332tree
12333c_finish_transaction (location_t loc, tree block, int flags)
12334{
12335 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12336 if (flags & TM_STMT_ATTR_OUTER)
12337 TRANSACTION_EXPR_OUTER (stmt) = 1;
12338 if (flags & TM_STMT_ATTR_RELAXED)
12339 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12340 return add_stmt (stmt);
12341}
12342
9ae165a0
DG
12343/* Make a variant type in the proper way for C/C++, propagating qualifiers
12344 down to the element type of an array. */
12345
12346tree
12347c_build_qualified_type (tree type, int type_quals)
12348{
12349 if (type == error_mark_node)
12350 return type;
12351
12352 if (TREE_CODE (type) == ARRAY_TYPE)
12353 {
12354 tree t;
12355 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12356 type_quals);
12357
12358 /* See if we already have an identically qualified type. */
12359 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12360 {
12361 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12362 && TYPE_NAME (t) == TYPE_NAME (type)
12363 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12364 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12365 TYPE_ATTRIBUTES (type)))
12366 break;
12367 }
12368 if (!t)
12369 {
12370 tree domain = TYPE_DOMAIN (type);
12371
12372 t = build_variant_type_copy (type);
12373 TREE_TYPE (t) = element_type;
12374
12375 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12376 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12377 SET_TYPE_STRUCTURAL_EQUALITY (t);
12378 else if (TYPE_CANONICAL (element_type) != element_type
12379 || (domain && TYPE_CANONICAL (domain) != domain))
12380 {
b8698a0f 12381 tree unqualified_canon
9ae165a0 12382 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12383 domain? TYPE_CANONICAL (domain)
9ae165a0 12384 : NULL_TREE);
b8698a0f 12385 TYPE_CANONICAL (t)
9ae165a0
DG
12386 = c_build_qualified_type (unqualified_canon, type_quals);
12387 }
12388 else
12389 TYPE_CANONICAL (t) = t;
12390 }
12391 return t;
12392 }
12393
12394 /* A restrict-qualified pointer type must be a pointer to object or
12395 incomplete type. Note that the use of POINTER_TYPE_P also allows
12396 REFERENCE_TYPEs, which is appropriate for C++. */
12397 if ((type_quals & TYPE_QUAL_RESTRICT)
12398 && (!POINTER_TYPE_P (type)
12399 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12400 {
12401 error ("invalid use of %<restrict%>");
12402 type_quals &= ~TYPE_QUAL_RESTRICT;
12403 }
12404
12405 return build_qualified_type (type, type_quals);
12406}
72b5577d
ILT
12407
12408/* Build a VA_ARG_EXPR for the C parser. */
12409
12410tree
c2255bc4 12411c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12412{
12413 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12414 warning_at (loc, OPT_Wc___compat,
12415 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12416 return build_va_arg (loc, expr, type);
72b5577d 12417}
acf0174b
JJ
12418
12419/* Return truthvalue of whether T1 is the same tree structure as T2.
12420 Return 1 if they are the same. Return 0 if they are different. */
12421
12422bool
12423c_tree_equal (tree t1, tree t2)
12424{
12425 enum tree_code code1, code2;
12426
12427 if (t1 == t2)
12428 return true;
12429 if (!t1 || !t2)
12430 return false;
12431
12432 for (code1 = TREE_CODE (t1);
12433 CONVERT_EXPR_CODE_P (code1)
12434 || code1 == NON_LVALUE_EXPR;
12435 code1 = TREE_CODE (t1))
12436 t1 = TREE_OPERAND (t1, 0);
12437 for (code2 = TREE_CODE (t2);
12438 CONVERT_EXPR_CODE_P (code2)
12439 || code2 == NON_LVALUE_EXPR;
12440 code2 = TREE_CODE (t2))
12441 t2 = TREE_OPERAND (t2, 0);
12442
12443 /* They might have become equal now. */
12444 if (t1 == t2)
12445 return true;
12446
12447 if (code1 != code2)
12448 return false;
12449
12450 switch (code1)
12451 {
12452 case INTEGER_CST:
807e902e 12453 return wi::eq_p (t1, t2);
acf0174b
JJ
12454
12455 case REAL_CST:
12456 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12457
12458 case STRING_CST:
12459 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12460 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12461 TREE_STRING_LENGTH (t1));
12462
12463 case FIXED_CST:
12464 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12465 TREE_FIXED_CST (t2));
12466
12467 case COMPLEX_CST:
12468 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12469 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12470
12471 case VECTOR_CST:
12472 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12473
12474 case CONSTRUCTOR:
12475 /* We need to do this when determining whether or not two
12476 non-type pointer to member function template arguments
12477 are the same. */
12478 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12479 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12480 return false;
12481 {
12482 tree field, value;
12483 unsigned int i;
12484 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12485 {
12486 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12487 if (!c_tree_equal (field, elt2->index)
12488 || !c_tree_equal (value, elt2->value))
12489 return false;
12490 }
12491 }
12492 return true;
12493
12494 case TREE_LIST:
12495 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12496 return false;
12497 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12498 return false;
12499 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12500
12501 case SAVE_EXPR:
12502 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12503
12504 case CALL_EXPR:
12505 {
12506 tree arg1, arg2;
12507 call_expr_arg_iterator iter1, iter2;
12508 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12509 return false;
12510 for (arg1 = first_call_expr_arg (t1, &iter1),
12511 arg2 = first_call_expr_arg (t2, &iter2);
12512 arg1 && arg2;
12513 arg1 = next_call_expr_arg (&iter1),
12514 arg2 = next_call_expr_arg (&iter2))
12515 if (!c_tree_equal (arg1, arg2))
12516 return false;
12517 if (arg1 || arg2)
12518 return false;
12519 return true;
12520 }
12521
12522 case TARGET_EXPR:
12523 {
12524 tree o1 = TREE_OPERAND (t1, 0);
12525 tree o2 = TREE_OPERAND (t2, 0);
12526
12527 /* Special case: if either target is an unallocated VAR_DECL,
12528 it means that it's going to be unified with whatever the
12529 TARGET_EXPR is really supposed to initialize, so treat it
12530 as being equivalent to anything. */
12531 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12532 && !DECL_RTL_SET_P (o1))
12533 /*Nop*/;
12534 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12535 && !DECL_RTL_SET_P (o2))
12536 /*Nop*/;
12537 else if (!c_tree_equal (o1, o2))
12538 return false;
12539
12540 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12541 }
12542
12543 case COMPONENT_REF:
12544 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12545 return false;
12546 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12547
12548 case PARM_DECL:
12549 case VAR_DECL:
12550 case CONST_DECL:
12551 case FIELD_DECL:
12552 case FUNCTION_DECL:
12553 case IDENTIFIER_NODE:
12554 case SSA_NAME:
12555 return false;
12556
12557 case TREE_VEC:
12558 {
12559 unsigned ix;
12560 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12561 return false;
12562 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12563 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12564 TREE_VEC_ELT (t2, ix)))
12565 return false;
12566 return true;
12567 }
12568
12569 default:
12570 break;
12571 }
12572
12573 switch (TREE_CODE_CLASS (code1))
12574 {
12575 case tcc_unary:
12576 case tcc_binary:
12577 case tcc_comparison:
12578 case tcc_expression:
12579 case tcc_vl_exp:
12580 case tcc_reference:
12581 case tcc_statement:
12582 {
12583 int i, n = TREE_OPERAND_LENGTH (t1);
12584
12585 switch (code1)
12586 {
12587 case PREINCREMENT_EXPR:
12588 case PREDECREMENT_EXPR:
12589 case POSTINCREMENT_EXPR:
12590 case POSTDECREMENT_EXPR:
12591 n = 1;
12592 break;
12593 case ARRAY_REF:
12594 n = 2;
12595 break;
12596 default:
12597 break;
12598 }
12599
12600 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12601 && n != TREE_OPERAND_LENGTH (t2))
12602 return false;
12603
12604 for (i = 0; i < n; ++i)
12605 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12606 return false;
12607
12608 return true;
12609 }
12610
12611 case tcc_type:
12612 return comptypes (t1, t2);
12613 default:
12614 gcc_unreachable ();
12615 }
12616 /* We can get here with --disable-checking. */
12617 return false;
12618}
12893402
BI
12619
12620/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12621 spawn-helper and BODY is the newly created body for FNDECL. */
12622
12623void
12624cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12625{
12626 tree list = alloc_stmt_list ();
12627 tree frame = make_cilk_frame (fndecl);
12628 tree dtor = create_cilk_function_exit (frame, false, true);
12629 add_local_decl (cfun, frame);
12630
12631 DECL_SAVED_TREE (fndecl) = list;
12632 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12633 frame);
12634 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12635 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12636
12637 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12638 append_to_statement_list (detach_expr, &body_list);
12639
12640 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12641 body = fold_build_cleanup_point_expr (void_type_node, body);
12642
12643 append_to_statement_list (body, &body_list);
12644 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12645 body_list, dtor), &list);
12646}