]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
re PR debug/52472 (ICE: in convert_debug_memory_address, at cfgexpand.c:2491)
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
23a5b65a 2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6
ZW
28#include "coretypes.h"
29#include "tm.h"
400fbf9f 30#include "tree.h"
d8a2d370
DN
31#include "stor-layout.h"
32#include "trans-mem.h"
33#include "varasm.h"
34#include "stmt.h"
e57e265b 35#include "langhooks.h"
400fbf9f 36#include "c-tree.h"
29cc57cf 37#include "c-lang.h"
400fbf9f 38#include "flags.h"
ab87f8c8 39#include "intl.h"
672a6f42 40#include "target.h"
325c3691 41#include "tree-iterator.h"
6662d794 42#include "bitmap.h"
2fb9a547
AM
43#include "pointer-set.h"
44#include "basic-block.h"
45#include "gimple-expr.h"
45b0be94 46#include "gimplify.h"
acf0174b 47#include "tree-inline.h"
0645c1a2 48#include "omp-low.h"
61d3ce20 49#include "c-family/c-objc.h"
a212e43f 50#include "c-family/c-common.h"
de5a5fa1 51#include "c-family/c-ubsan.h"
12893402 52#include "cilk.h"
807e902e 53#include "wide-int.h"
325c3691 54
2ac2f164
JM
55/* Possible cases of implicit bad conversions. Used to select
56 diagnostic messages in convert_for_assignment. */
57enum impl_conv {
58 ic_argpass,
59 ic_assign,
60 ic_init,
61 ic_return
62};
63
bc4b653b
JM
64/* The level of nesting inside "__alignof__". */
65int in_alignof;
66
67/* The level of nesting inside "sizeof". */
68int in_sizeof;
69
70/* The level of nesting inside "typeof". */
71int in_typeof;
400fbf9f 72
1a4049e7
JJ
73/* The argument of last parsed sizeof expression, only to be tested
74 if expr.original_code == SIZEOF_EXPR. */
75tree c_last_sizeof_arg;
76
b71c7f8a 77/* Nonzero if we've already printed a "missing braces around initializer"
103b7b17 78 message within this initializer. */
b71c7f8a 79static int missing_braces_mentioned;
103b7b17 80
bf730f15
RS
81static int require_constant_value;
82static int require_constant_elements;
83
58f9752a 84static bool null_pointer_constant_p (const_tree);
f55ade6e 85static tree qualify_type (tree, tree);
dc5027f4
JM
86static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
87 bool *);
744aa42f 88static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
89static int function_types_compatible_p (const_tree, const_tree, bool *,
90 bool *);
91static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 92static tree lookup_field (tree, tree);
81e5eca8
MP
93static int convert_arguments (location_t, vec<location_t>, tree,
94 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
95 tree);
db3927fb 96static tree pointer_diff (location_t, tree, tree);
68fca595 97static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 98 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
99static tree valid_compound_expr_initializer (tree, tree);
100static void push_string (const char *);
101static void push_member_name (tree);
f55ade6e
AJ
102static int spelling_length (void);
103static char *print_spelling (char *);
96b40f8d 104static void warning_init (location_t, int, const char *);
c2255bc4 105static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
34cf811f
MP
106static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
107 bool, struct obstack *);
a1e3b3d9 108static void output_pending_init_elements (int, struct obstack *);
ea58ef42 109static int set_designator (location_t, int, struct obstack *);
a1e3b3d9 110static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
111static void add_pending_init (location_t, tree, tree, tree, bool,
112 struct obstack *);
a1e3b3d9
LB
113static void set_nonincremental_init (struct obstack *);
114static void set_nonincremental_init_from_string (tree, struct obstack *);
115static tree find_init_member (tree, struct obstack *);
f37acdf9 116static void readonly_warning (tree, enum lvalue_use);
7bd11157 117static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 118static void record_maybe_used_decl (tree);
dc5027f4 119static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
120\f
121/* Return true if EXP is a null pointer constant, false otherwise. */
122
123static bool
58f9752a 124null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
125{
126 /* This should really operate on c_expr structures, but they aren't
127 yet available everywhere required. */
128 tree type = TREE_TYPE (expr);
129 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 130 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
131 && integer_zerop (expr)
132 && (INTEGRAL_TYPE_P (type)
133 || (TREE_CODE (type) == POINTER_TYPE
134 && VOID_TYPE_P (TREE_TYPE (type))
135 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
136}
928c19bb
JM
137
138/* EXPR may appear in an unevaluated part of an integer constant
139 expression, but not in an evaluated part. Wrap it in a
140 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
141 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
142
143static tree
144note_integer_operands (tree expr)
145{
146 tree ret;
147 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
148 {
149 ret = copy_node (expr);
150 TREE_OVERFLOW (ret) = 1;
151 }
152 else
153 {
154 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
155 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
156 }
157 return ret;
158}
159
4d84fe7c
JM
160/* Having checked whether EXPR may appear in an unevaluated part of an
161 integer constant expression and found that it may, remove any
162 C_MAYBE_CONST_EXPR noting this fact and return the resulting
163 expression. */
164
165static inline tree
166remove_c_maybe_const_expr (tree expr)
167{
168 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
169 return C_MAYBE_CONST_EXPR_EXPR (expr);
170 else
171 return expr;
172}
173
f13c9b2c
AP
174\f/* This is a cache to hold if two types are compatible or not. */
175
176struct tagged_tu_seen_cache {
177 const struct tagged_tu_seen_cache * next;
58f9752a
KG
178 const_tree t1;
179 const_tree t2;
f13c9b2c
AP
180 /* The return value of tagged_types_tu_compatible_p if we had seen
181 these two types already. */
182 int val;
183};
184
185static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
186static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
187
400fbf9f
JW
188/* Do `exp = require_complete_type (exp);' to make sure exp
189 does not have an incomplete type. (That includes void types.) */
190
191tree
2f6e4e97 192require_complete_type (tree value)
400fbf9f
JW
193{
194 tree type = TREE_TYPE (value);
195
c3d5c3fa 196 if (value == error_mark_node || type == error_mark_node)
ea0f786b
CB
197 return error_mark_node;
198
400fbf9f 199 /* First, detect a valid value with a complete type. */
d0f062fb 200 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
201 return value;
202
7a228918 203 c_incomplete_type_error (value, type);
400fbf9f
JW
204 return error_mark_node;
205}
206
207/* Print an error message for invalid use of an incomplete type.
208 VALUE is the expression that was used (or 0 if that isn't known)
209 and TYPE is the type that was invalid. */
210
211void
ac7d7749 212c_incomplete_type_error (const_tree value, const_tree type)
400fbf9f 213{
5d5993dd 214 const char *type_code_string;
400fbf9f
JW
215
216 /* Avoid duplicate error message. */
217 if (TREE_CODE (type) == ERROR_MARK)
218 return;
219
220 if (value != 0 && (TREE_CODE (value) == VAR_DECL
221 || TREE_CODE (value) == PARM_DECL))
c51a1ba9 222 error ("%qD has an incomplete type", value);
400fbf9f
JW
223 else
224 {
225 retry:
226 /* We must print an error message. Be clever about what it says. */
227
228 switch (TREE_CODE (type))
229 {
230 case RECORD_TYPE:
ab87f8c8 231 type_code_string = "struct";
400fbf9f
JW
232 break;
233
234 case UNION_TYPE:
ab87f8c8 235 type_code_string = "union";
400fbf9f
JW
236 break;
237
238 case ENUMERAL_TYPE:
ab87f8c8 239 type_code_string = "enum";
400fbf9f
JW
240 break;
241
242 case VOID_TYPE:
243 error ("invalid use of void expression");
244 return;
245
246 case ARRAY_TYPE:
247 if (TYPE_DOMAIN (type))
248 {
fba78abb
RH
249 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
250 {
251 error ("invalid use of flexible array member");
252 return;
253 }
400fbf9f
JW
254 type = TREE_TYPE (type);
255 goto retry;
256 }
257 error ("invalid use of array with unspecified bounds");
258 return;
259
260 default:
366de0ce 261 gcc_unreachable ();
400fbf9f
JW
262 }
263
264 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
c51a1ba9
JM
265 error ("invalid use of undefined type %<%s %E%>",
266 type_code_string, TYPE_NAME (type));
400fbf9f
JW
267 else
268 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
c51a1ba9 269 error ("invalid use of incomplete typedef %qD", TYPE_NAME (type));
400fbf9f
JW
270 }
271}
272
ab393bf1
NB
273/* Given a type, apply default promotions wrt unnamed function
274 arguments and return the new type. */
275
276tree
2f6e4e97 277c_type_promotes_to (tree type)
ab393bf1 278{
267bac10 279 tree ret = NULL_TREE;
ab393bf1 280
267bac10
JM
281 if (TYPE_MAIN_VARIANT (type) == float_type_node)
282 ret = double_type_node;
283 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
284 {
285 /* Preserve unsignedness if not really getting any wider. */
8df83eae 286 if (TYPE_UNSIGNED (type)
c22cacf3 287 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
288 ret = unsigned_type_node;
289 else
290 ret = integer_type_node;
ab393bf1
NB
291 }
292
267bac10
JM
293 if (ret != NULL_TREE)
294 return (TYPE_ATOMIC (type)
295 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
296 : ret);
297
ab393bf1
NB
298 return type;
299}
300
36c5e70a
BE
301/* Return true if between two named address spaces, whether there is a superset
302 named address space that encompasses both address spaces. If there is a
303 superset, return which address space is the superset. */
304
305static bool
306addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
307{
308 if (as1 == as2)
309 {
310 *common = as1;
311 return true;
312 }
313 else if (targetm.addr_space.subset_p (as1, as2))
314 {
315 *common = as2;
316 return true;
317 }
318 else if (targetm.addr_space.subset_p (as2, as1))
319 {
320 *common = as1;
321 return true;
322 }
323 else
324 return false;
325}
326
400fbf9f
JW
327/* Return a variant of TYPE which has all the type qualifiers of LIKE
328 as well as those of TYPE. */
329
330static tree
2f6e4e97 331qualify_type (tree type, tree like)
400fbf9f 332{
36c5e70a
BE
333 addr_space_t as_type = TYPE_ADDR_SPACE (type);
334 addr_space_t as_like = TYPE_ADDR_SPACE (like);
335 addr_space_t as_common;
336
337 /* If the two named address spaces are different, determine the common
338 superset address space. If there isn't one, raise an error. */
339 if (!addr_space_superset (as_type, as_like, &as_common))
340 {
341 as_common = as_type;
342 error ("%qT and %qT are in disjoint named address spaces",
343 type, like);
344 }
345
2f6e4e97 346 return c_build_qualified_type (type,
36c5e70a 347 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 348 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 349 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 350}
52ffd86e
MS
351
352/* Return true iff the given tree T is a variable length array. */
353
354bool
ac7d7749 355c_vla_type_p (const_tree t)
52ffd86e
MS
356{
357 if (TREE_CODE (t) == ARRAY_TYPE
358 && C_TYPE_VARIABLE_SIZE (t))
359 return true;
360 return false;
361}
400fbf9f 362\f
10bc1b1b 363/* Return the composite type of two compatible types.
5305f6d7 364
10bc1b1b
JM
365 We assume that comptypes has already been done and returned
366 nonzero; if that isn't so, this may crash. In particular, we
367 assume that qualifiers match. */
400fbf9f
JW
368
369tree
10bc1b1b 370composite_type (tree t1, tree t2)
400fbf9f 371{
b3694847
SS
372 enum tree_code code1;
373 enum tree_code code2;
4b027d16 374 tree attributes;
400fbf9f
JW
375
376 /* Save time if the two types are the same. */
377
378 if (t1 == t2) return t1;
379
380 /* If one type is nonsense, use the other. */
381 if (t1 == error_mark_node)
382 return t2;
383 if (t2 == error_mark_node)
384 return t1;
385
10bc1b1b
JM
386 code1 = TREE_CODE (t1);
387 code2 = TREE_CODE (t2);
388
d9525bec 389 /* Merge the attributes. */
5fd9b178 390 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 391
10bc1b1b
JM
392 /* If one is an enumerated type and the other is the compatible
393 integer type, the composite type might be either of the two
394 (DR#013 question 3). For consistency, use the enumerated type as
395 the composite type. */
400fbf9f 396
10bc1b1b
JM
397 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
398 return t1;
399 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
400 return t2;
75326e8c 401
366de0ce 402 gcc_assert (code1 == code2);
b6a10c9f 403
400fbf9f
JW
404 switch (code1)
405 {
400fbf9f 406 case POINTER_TYPE:
10bc1b1b 407 /* For two pointers, do this recursively on the target type. */
400fbf9f 408 {
3932261a
MM
409 tree pointed_to_1 = TREE_TYPE (t1);
410 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 411 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 412 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
413 t1 = build_type_attribute_variant (t1, attributes);
414 return qualify_type (t1, t2);
400fbf9f 415 }
400fbf9f
JW
416
417 case ARRAY_TYPE:
418 {
10bc1b1b 419 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
420 int quals;
421 tree unqual_elt;
ca8bdb78
JM
422 tree d1 = TYPE_DOMAIN (t1);
423 tree d2 = TYPE_DOMAIN (t2);
424 bool d1_variable, d2_variable;
425 bool d1_zero, d2_zero;
f6294de7 426 bool t1_complete, t2_complete;
46df2823 427
de46b2fe 428 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
429 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
430 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 431
f6294de7
JM
432 t1_complete = COMPLETE_TYPE_P (t1);
433 t2_complete = COMPLETE_TYPE_P (t2);
434
ca8bdb78
JM
435 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
436 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
437
438 d1_variable = (!d1_zero
439 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
440 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
441 d2_variable = (!d2_zero
442 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
443 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
444 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
445 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 446
400fbf9f 447 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
448 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
449 && (d2_variable || d2_zero || !d1_variable))
4b027d16 450 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
451 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
452 && (d1_variable || d1_zero || !d2_variable))
4b027d16 453 return build_type_attribute_variant (t2, attributes);
c22cacf3 454
de46b2fe
AP
455 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
456 return build_type_attribute_variant (t1, attributes);
457 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
458 return build_type_attribute_variant (t2, attributes);
c22cacf3 459
46df2823
JM
460 /* Merge the element types, and have a size if either arg has
461 one. We may have qualifiers on the element types. To set
462 up TYPE_MAIN_VARIANT correctly, we need to form the
463 composite of the unqualified types and add the qualifiers
464 back at the end. */
465 quals = TYPE_QUALS (strip_array_types (elt));
466 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
467 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
468 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
469 && (d2_variable
470 || d2_zero
471 || !d1_variable))
472 ? t1
473 : t2));
f6294de7
JM
474 /* Ensure a composite type involving a zero-length array type
475 is a zero-length type not an incomplete type. */
476 if (d1_zero && d2_zero
477 && (t1_complete || t2_complete)
478 && !COMPLETE_TYPE_P (t1))
479 {
480 TYPE_SIZE (t1) = bitsize_zero_node;
481 TYPE_SIZE_UNIT (t1) = size_zero_node;
482 }
46df2823 483 t1 = c_build_qualified_type (t1, quals);
de46b2fe 484 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
485 }
486
fcb99e7b
JJ
487 case ENUMERAL_TYPE:
488 case RECORD_TYPE:
489 case UNION_TYPE:
490 if (attributes != NULL)
491 {
492 /* Try harder not to create a new aggregate type. */
493 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
494 return t1;
495 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
496 return t2;
497 }
498 return build_type_attribute_variant (t1, attributes);
499
400fbf9f
JW
500 case FUNCTION_TYPE:
501 /* Function types: prefer the one that specified arg types.
502 If both do, merge the arg types. Also merge the return types. */
503 {
10bc1b1b 504 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
505 tree p1 = TYPE_ARG_TYPES (t1);
506 tree p2 = TYPE_ARG_TYPES (t2);
507 int len;
508 tree newargs, n;
509 int i;
510
511 /* Save space: see if the result is identical to one of the args. */
3f75a254 512 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 513 return build_type_attribute_variant (t1, attributes);
3f75a254 514 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 515 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
516
517 /* Simple way if one arg fails to specify argument types. */
518 if (TYPE_ARG_TYPES (t1) == 0)
4b027d16 519 {
fe7080d2
AP
520 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
521 t1 = build_type_attribute_variant (t1, attributes);
522 return qualify_type (t1, t2);
4b027d16 523 }
400fbf9f 524 if (TYPE_ARG_TYPES (t2) == 0)
4b027d16
RK
525 {
526 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
527 t1 = build_type_attribute_variant (t1, attributes);
528 return qualify_type (t1, t2);
4b027d16 529 }
400fbf9f
JW
530
531 /* If both args specify argument types, we must merge the two
532 lists, argument by argument. */
2f4e8f2b 533
400fbf9f
JW
534 len = list_length (p1);
535 newargs = 0;
536
537 for (i = 0; i < len; i++)
8d9bfdc5 538 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
539
540 n = newargs;
541
542 for (; p1;
543 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
544 {
545 /* A null type means arg type is not specified.
546 Take whatever the other function type has. */
547 if (TREE_VALUE (p1) == 0)
548 {
549 TREE_VALUE (n) = TREE_VALUE (p2);
550 goto parm_done;
551 }
552 if (TREE_VALUE (p2) == 0)
553 {
554 TREE_VALUE (n) = TREE_VALUE (p1);
555 goto parm_done;
556 }
2f6e4e97 557
400fbf9f
JW
558 /* Given wait (union {union wait *u; int *i} *)
559 and wait (union wait *),
560 prefer union wait * as type of parm. */
561 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
562 && TREE_VALUE (p1) != TREE_VALUE (p2))
563 {
564 tree memb;
58cb41e6
JJ
565 tree mv2 = TREE_VALUE (p2);
566 if (mv2 && mv2 != error_mark_node
567 && TREE_CODE (mv2) != ARRAY_TYPE)
568 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 569 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 570 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
571 {
572 tree mv3 = TREE_TYPE (memb);
573 if (mv3 && mv3 != error_mark_node
574 && TREE_CODE (mv3) != ARRAY_TYPE)
575 mv3 = TYPE_MAIN_VARIANT (mv3);
576 if (comptypes (mv3, mv2))
577 {
578 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
579 TREE_VALUE (p2));
c1771a20 580 pedwarn (input_location, OPT_Wpedantic,
fcf73884 581 "function types not truly compatible in ISO C");
58cb41e6
JJ
582 goto parm_done;
583 }
584 }
400fbf9f
JW
585 }
586 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
587 && TREE_VALUE (p2) != TREE_VALUE (p1))
588 {
589 tree memb;
58cb41e6
JJ
590 tree mv1 = TREE_VALUE (p1);
591 if (mv1 && mv1 != error_mark_node
592 && TREE_CODE (mv1) != ARRAY_TYPE)
593 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 594 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 595 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
596 {
597 tree mv3 = TREE_TYPE (memb);
598 if (mv3 && mv3 != error_mark_node
599 && TREE_CODE (mv3) != ARRAY_TYPE)
600 mv3 = TYPE_MAIN_VARIANT (mv3);
601 if (comptypes (mv3, mv1))
602 {
603 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
604 TREE_VALUE (p1));
c1771a20 605 pedwarn (input_location, OPT_Wpedantic,
fcf73884 606 "function types not truly compatible in ISO C");
58cb41e6
JJ
607 goto parm_done;
608 }
609 }
400fbf9f 610 }
10bc1b1b 611 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
612 parm_done: ;
613 }
614
4b027d16 615 t1 = build_function_type (valtype, newargs);
fe7080d2 616 t1 = qualify_type (t1, t2);
0f41302f 617 /* ... falls through ... */
400fbf9f
JW
618 }
619
620 default:
4b027d16 621 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
622 }
623
624}
10bc1b1b
JM
625
626/* Return the type of a conditional expression between pointers to
627 possibly differently qualified versions of compatible types.
628
629 We assume that comp_target_types has already been done and returned
630 nonzero; if that isn't so, this may crash. */
631
632static tree
633common_pointer_type (tree t1, tree t2)
634{
635 tree attributes;
46df2823
JM
636 tree pointed_to_1, mv1;
637 tree pointed_to_2, mv2;
10bc1b1b 638 tree target;
eb1387a0 639 unsigned target_quals;
36c5e70a
BE
640 addr_space_t as1, as2, as_common;
641 int quals1, quals2;
10bc1b1b
JM
642
643 /* Save time if the two types are the same. */
644
645 if (t1 == t2) return t1;
646
647 /* If one type is nonsense, use the other. */
648 if (t1 == error_mark_node)
649 return t2;
650 if (t2 == error_mark_node)
651 return t1;
652
366de0ce 653 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 654 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
655
656 /* Merge the attributes. */
657 attributes = targetm.merge_type_attributes (t1, t2);
658
659 /* Find the composite type of the target types, and combine the
46df2823
JM
660 qualifiers of the two types' targets. Do not lose qualifiers on
661 array element types by taking the TYPE_MAIN_VARIANT. */
662 mv1 = pointed_to_1 = TREE_TYPE (t1);
663 mv2 = pointed_to_2 = TREE_TYPE (t2);
664 if (TREE_CODE (mv1) != ARRAY_TYPE)
665 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
666 if (TREE_CODE (mv2) != ARRAY_TYPE)
667 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
668 target = composite_type (mv1, mv2);
eb1387a0
RG
669
670 /* For function types do not merge const qualifiers, but drop them
671 if used inconsistently. The middle-end uses these to mark const
672 and noreturn functions. */
36c5e70a
BE
673 quals1 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_1);
674 quals2 = TYPE_QUALS_NO_ADDR_SPACE (pointed_to_2);
675
eb1387a0 676 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 677 target_quals = (quals1 & quals2);
eb1387a0 678 else
36c5e70a
BE
679 target_quals = (quals1 | quals2);
680
681 /* If the two named address spaces are different, determine the common
682 superset address space. This is guaranteed to exist due to the
683 assumption that comp_target_type returned non-zero. */
684 as1 = TYPE_ADDR_SPACE (pointed_to_1);
685 as2 = TYPE_ADDR_SPACE (pointed_to_2);
686 if (!addr_space_superset (as1, as2, &as_common))
687 gcc_unreachable ();
688
689 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
690
eb1387a0 691 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
692 return build_type_attribute_variant (t1, attributes);
693}
694
695/* Return the common type for two arithmetic types under the usual
696 arithmetic conversions. The default conversions have already been
697 applied, and enumerated types converted to their compatible integer
698 types. The resulting type is unqualified and has no attributes.
699
700 This is the type for the result of most arithmetic operations
701 if the operands have the given two types. */
702
ccf7f880
JJ
703static tree
704c_common_type (tree t1, tree t2)
10bc1b1b
JM
705{
706 enum tree_code code1;
707 enum tree_code code2;
708
709 /* If one type is nonsense, use the other. */
710 if (t1 == error_mark_node)
711 return t2;
712 if (t2 == error_mark_node)
713 return t1;
714
715 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
716 t1 = TYPE_MAIN_VARIANT (t1);
717
718 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
719 t2 = TYPE_MAIN_VARIANT (t2);
720
721 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
722 t1 = build_type_attribute_variant (t1, NULL_TREE);
723
724 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
725 t2 = build_type_attribute_variant (t2, NULL_TREE);
726
727 /* Save time if the two types are the same. */
728
729 if (t1 == t2) return t1;
730
731 code1 = TREE_CODE (t1);
732 code2 = TREE_CODE (t2);
733
366de0ce 734 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
735 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
736 || code1 == INTEGER_TYPE);
366de0ce 737 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
738 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
739 || code2 == INTEGER_TYPE);
10bc1b1b 740
5fc89bfd
JJ
741 /* When one operand is a decimal float type, the other operand cannot be
742 a generic float type or a complex type. We also disallow vector types
743 here. */
744 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
745 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
746 {
747 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
748 {
749 error ("can%'t mix operands of decimal float and vector types");
750 return error_mark_node;
751 }
752 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
753 {
754 error ("can%'t mix operands of decimal float and complex types");
755 return error_mark_node;
756 }
757 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
758 {
759 error ("can%'t mix operands of decimal float and other float types");
760 return error_mark_node;
761 }
762 }
763
10bc1b1b
JM
764 /* If one type is a vector type, return that type. (How the usual
765 arithmetic conversions apply to the vector types extension is not
766 precisely specified.) */
767 if (code1 == VECTOR_TYPE)
768 return t1;
769
770 if (code2 == VECTOR_TYPE)
771 return t2;
772
773 /* If one type is complex, form the common type of the non-complex
774 components, then make that complex. Use T1 or T2 if it is the
775 required type. */
776 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
777 {
778 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
779 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 780 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
781
782 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
783 return t1;
784 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
785 return t2;
786 else
787 return build_complex_type (subtype);
788 }
789
790 /* If only one is real, use it as the result. */
791
792 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
793 return t1;
794
795 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
796 return t2;
797
9a8ce21f
JG
798 /* If both are real and either are decimal floating point types, use
799 the decimal floating point type with the greater precision. */
800
801 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
802 {
803 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
804 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
805 return dfloat128_type_node;
806 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
807 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
808 return dfloat64_type_node;
809 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
810 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
811 return dfloat32_type_node;
812 }
813
ab22c1fa
CF
814 /* Deal with fixed-point types. */
815 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
816 {
817 unsigned int unsignedp = 0, satp = 0;
818 enum machine_mode m1, m2;
819 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
820
821 m1 = TYPE_MODE (t1);
822 m2 = TYPE_MODE (t2);
823
824 /* If one input type is saturating, the result type is saturating. */
825 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
826 satp = 1;
827
828 /* If both fixed-point types are unsigned, the result type is unsigned.
829 When mixing fixed-point and integer types, follow the sign of the
830 fixed-point type.
831 Otherwise, the result type is signed. */
832 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
833 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
834 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
835 && TYPE_UNSIGNED (t1))
836 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
837 && TYPE_UNSIGNED (t2)))
838 unsignedp = 1;
839
840 /* The result type is signed. */
841 if (unsignedp == 0)
842 {
843 /* If the input type is unsigned, we need to convert to the
844 signed type. */
845 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
846 {
d75d71e0 847 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
848 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
849 mclass = MODE_FRACT;
850 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
851 mclass = MODE_ACCUM;
852 else
853 gcc_unreachable ();
854 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
855 }
856 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
857 {
d75d71e0 858 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
859 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
860 mclass = MODE_FRACT;
861 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
862 mclass = MODE_ACCUM;
863 else
864 gcc_unreachable ();
865 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
866 }
867 }
868
869 if (code1 == FIXED_POINT_TYPE)
870 {
871 fbit1 = GET_MODE_FBIT (m1);
872 ibit1 = GET_MODE_IBIT (m1);
873 }
874 else
875 {
876 fbit1 = 0;
877 /* Signed integers need to subtract one sign bit. */
878 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
879 }
880
881 if (code2 == FIXED_POINT_TYPE)
882 {
883 fbit2 = GET_MODE_FBIT (m2);
884 ibit2 = GET_MODE_IBIT (m2);
885 }
886 else
887 {
888 fbit2 = 0;
889 /* Signed integers need to subtract one sign bit. */
890 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
891 }
892
893 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
894 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
895 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
896 satp);
897 }
898
10bc1b1b
JM
899 /* Both real or both integers; use the one with greater precision. */
900
901 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
902 return t1;
903 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
904 return t2;
905
906 /* Same precision. Prefer long longs to longs to ints when the
907 same precision, following the C99 rules on integer type rank
908 (which are equivalent to the C90 rules for C90 types). */
909
910 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
911 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
912 return long_long_unsigned_type_node;
913
914 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
915 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
916 {
917 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
918 return long_long_unsigned_type_node;
919 else
c22cacf3 920 return long_long_integer_type_node;
10bc1b1b
JM
921 }
922
923 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
924 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
925 return long_unsigned_type_node;
926
927 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
928 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
929 {
930 /* But preserve unsignedness from the other type,
931 since long cannot hold all the values of an unsigned int. */
932 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
933 return long_unsigned_type_node;
934 else
935 return long_integer_type_node;
936 }
937
938 /* Likewise, prefer long double to double even if same size. */
939 if (TYPE_MAIN_VARIANT (t1) == long_double_type_node
940 || TYPE_MAIN_VARIANT (t2) == long_double_type_node)
941 return long_double_type_node;
942
2531a1d9
JR
943 /* Likewise, prefer double to float even if same size.
944 We got a couple of embedded targets with 32 bit doubles, and the
945 pdp11 might have 64 bit floats. */
946 if (TYPE_MAIN_VARIANT (t1) == double_type_node
947 || TYPE_MAIN_VARIANT (t2) == double_type_node)
948 return double_type_node;
949
10bc1b1b
JM
950 /* Otherwise prefer the unsigned one. */
951
952 if (TYPE_UNSIGNED (t1))
953 return t1;
954 else
955 return t2;
956}
400fbf9f 957\f
5922c215
JM
958/* Wrapper around c_common_type that is used by c-common.c and other
959 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
960 are allowed here and are converted to their compatible integer types.
961 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
962 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
963tree
964common_type (tree t1, tree t2)
965{
966 if (TREE_CODE (t1) == ENUMERAL_TYPE)
967 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
968 if (TREE_CODE (t2) == ENUMERAL_TYPE)
969 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
970
971 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
972 if (TREE_CODE (t1) == BOOLEAN_TYPE
973 && TREE_CODE (t2) == BOOLEAN_TYPE)
974 return boolean_type_node;
975
976 /* If either type is BOOLEAN_TYPE, then return the other. */
977 if (TREE_CODE (t1) == BOOLEAN_TYPE)
978 return t2;
979 if (TREE_CODE (t2) == BOOLEAN_TYPE)
980 return t1;
981
ccf7f880
JJ
982 return c_common_type (t1, t2);
983}
f13c9b2c 984
400fbf9f
JW
985/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
986 or various other operations. Return 2 if they are compatible
987 but a warning may be needed if you use them together. */
988
989int
132da1a5 990comptypes (tree type1, tree type2)
f13c9b2c
AP
991{
992 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
993 int val;
994
dc5027f4 995 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
996 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
997
998 return val;
999}
1000
1001/* Like comptypes, but if it returns non-zero because enum and int are
1002 compatible, it sets *ENUM_AND_INT_P to true. */
1003
1004static int
1005comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1006{
1007 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1008 int val;
1009
dc5027f4
JM
1010 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1011 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1012
1013 return val;
1014}
1015
1016/* Like comptypes, but if it returns nonzero for different types, it
1017 sets *DIFFERENT_TYPES_P to true. */
1018
1019int
1020comptypes_check_different_types (tree type1, tree type2,
1021 bool *different_types_p)
1022{
1023 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1024 int val;
1025
1026 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1028
f13c9b2c 1029 return val;
c22cacf3
MS
1030}
1031\f
f13c9b2c
AP
1032/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1033 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1034 but a warning may be needed if you use them together. If
1035 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1036 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1037 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1038 NULL, and the types are compatible but different enough not to be
48b0b196 1039 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1040 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1041 false, but may or may not be set if the types are incompatible.
1042 This differs from comptypes, in that we don't free the seen
1043 types. */
f13c9b2c
AP
1044
1045static int
dc5027f4
JM
1046comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1047 bool *different_types_p)
400fbf9f 1048{
58f9752a
KG
1049 const_tree t1 = type1;
1050 const_tree t2 = type2;
4b027d16 1051 int attrval, val;
400fbf9f
JW
1052
1053 /* Suppress errors caused by previously reported errors. */
1054
8d47dfc5
RH
1055 if (t1 == t2 || !t1 || !t2
1056 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1057 return 1;
1058
bca63328
JM
1059 /* Enumerated types are compatible with integer types, but this is
1060 not transitive: two enumerated types in the same translation unit
1061 are compatible with each other only if they are the same type. */
400fbf9f 1062
bca63328 1063 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1064 {
1065 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1066 if (TREE_CODE (t2) != VOID_TYPE)
1067 {
1068 if (enum_and_int_p != NULL)
1069 *enum_and_int_p = true;
1070 if (different_types_p != NULL)
1071 *different_types_p = true;
1072 }
744aa42f 1073 }
bca63328 1074 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1075 {
1076 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1077 if (TREE_CODE (t1) != VOID_TYPE)
1078 {
1079 if (enum_and_int_p != NULL)
1080 *enum_and_int_p = true;
1081 if (different_types_p != NULL)
1082 *different_types_p = true;
1083 }
744aa42f 1084 }
400fbf9f
JW
1085
1086 if (t1 == t2)
1087 return 1;
1088
1089 /* Different classes of types can't be compatible. */
1090
3aeb3655
EC
1091 if (TREE_CODE (t1) != TREE_CODE (t2))
1092 return 0;
400fbf9f 1093
118a3a8b 1094 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1095
3932261a 1096 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1097 return 0;
1098
08632da2
RS
1099 /* Allow for two different type nodes which have essentially the same
1100 definition. Note that we already checked for equality of the type
38e01259 1101 qualifiers (just above). */
400fbf9f 1102
46df2823
JM
1103 if (TREE_CODE (t1) != ARRAY_TYPE
1104 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1105 return 1;
1106
4b027d16 1107 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1108 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1109 return 0;
1110
1111 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1112 val = 0;
1113
400fbf9f
JW
1114 switch (TREE_CODE (t1))
1115 {
1116 case POINTER_TYPE:
106f5de5
UW
1117 /* Do not remove mode or aliasing information. */
1118 if (TYPE_MODE (t1) != TYPE_MODE (t2)
1119 || TYPE_REF_CAN_ALIAS_ALL (t1) != TYPE_REF_CAN_ALIAS_ALL (t2))
1120 break;
4b027d16 1121 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1122 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1123 enum_and_int_p, different_types_p));
4b027d16 1124 break;
400fbf9f
JW
1125
1126 case FUNCTION_TYPE:
dc5027f4
JM
1127 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1128 different_types_p);
4b027d16 1129 break;
400fbf9f
JW
1130
1131 case ARRAY_TYPE:
1132 {
400fbf9f
JW
1133 tree d1 = TYPE_DOMAIN (t1);
1134 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1135 bool d1_variable, d2_variable;
1136 bool d1_zero, d2_zero;
4b027d16 1137 val = 1;
400fbf9f
JW
1138
1139 /* Target types must match incl. qualifiers. */
1140 if (TREE_TYPE (t1) != TREE_TYPE (t2)
744aa42f 1141 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4
JM
1142 enum_and_int_p,
1143 different_types_p)))
400fbf9f
JW
1144 return 0;
1145
dc5027f4
JM
1146 if (different_types_p != NULL
1147 && (d1 == 0) != (d2 == 0))
1148 *different_types_p = true;
400fbf9f 1149 /* Sizes must match unless one is missing or variable. */
3f85558f 1150 if (d1 == 0 || d2 == 0 || d1 == d2)
4b027d16 1151 break;
400fbf9f 1152
3f75a254
JM
1153 d1_zero = !TYPE_MAX_VALUE (d1);
1154 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1155
3f75a254 1156 d1_variable = (!d1_zero
3f85558f
RH
1157 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1158 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1159 d2_variable = (!d2_zero
3f85558f
RH
1160 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1161 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1162 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1163 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1164
dc5027f4
JM
1165 if (different_types_p != NULL
1166 && d1_variable != d2_variable)
1167 *different_types_p = true;
3f85558f
RH
1168 if (d1_variable || d2_variable)
1169 break;
1170 if (d1_zero && d2_zero)
1171 break;
1172 if (d1_zero || d2_zero
3f75a254
JM
1173 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1174 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1175 val = 0;
1176
c22cacf3 1177 break;
400fbf9f
JW
1178 }
1179
d1bd0ded 1180 case ENUMERAL_TYPE:
58393038 1181 case RECORD_TYPE:
d1bd0ded 1182 case UNION_TYPE:
766beae1 1183 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1184 {
fcb99e7b
JJ
1185 tree a1 = TYPE_ATTRIBUTES (t1);
1186 tree a2 = TYPE_ATTRIBUTES (t2);
1187
1188 if (! attribute_list_contained (a1, a2)
1189 && ! attribute_list_contained (a2, a1))
1190 break;
1191
f13c9b2c 1192 if (attrval != 2)
dc5027f4
JM
1193 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1194 different_types_p);
1195 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1196 different_types_p);
f13c9b2c 1197 }
4b027d16 1198 break;
e9a25f70 1199
62e1dfcf 1200 case VECTOR_TYPE:
744aa42f
ILT
1201 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1202 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1203 enum_and_int_p, different_types_p));
62e1dfcf
NC
1204 break;
1205
e9a25f70
JL
1206 default:
1207 break;
400fbf9f 1208 }
4b027d16 1209 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1210}
1211
36c5e70a
BE
1212/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1213 their qualifiers, except for named address spaces. If the pointers point to
1214 different named addresses, then we must determine if one address space is a
1215 subset of the other. */
400fbf9f
JW
1216
1217static int
744aa42f 1218comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1219{
392202b0 1220 int val;
36c5e70a
BE
1221 tree mvl = TREE_TYPE (ttl);
1222 tree mvr = TREE_TYPE (ttr);
1223 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1224 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1225 addr_space_t as_common;
744aa42f 1226 bool enum_and_int_p;
8b40563c 1227
36c5e70a
BE
1228 /* Fail if pointers point to incompatible address spaces. */
1229 if (!addr_space_superset (asl, asr, &as_common))
1230 return 0;
1231
46df2823
JM
1232 /* Do not lose qualifiers on element types of array types that are
1233 pointer targets by taking their TYPE_MAIN_VARIANT. */
46df2823 1234 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
1235 mvl = (TYPE_ATOMIC (mvl)
1236 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1237 : TYPE_MAIN_VARIANT (mvl));
46df2823 1238 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
1239 mvr = (TYPE_ATOMIC (mvr)
1240 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1241 : TYPE_MAIN_VARIANT (mvr));
744aa42f
ILT
1242 enum_and_int_p = false;
1243 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1244
fcf73884 1245 if (val == 2)
c1771a20 1246 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1247
1248 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1249 warning_at (location, OPT_Wc___compat,
1250 "pointer target types incompatible in C++");
1251
400fbf9f
JW
1252 return val;
1253}
1254\f
1255/* Subroutines of `comptypes'. */
1256
f75fbaf7
ZW
1257/* Determine whether two trees derive from the same translation unit.
1258 If the CONTEXT chain ends in a null, that tree's context is still
1259 being parsed, so if two trees have context chains ending in null,
766beae1 1260 they're in the same translation unit. */
f75fbaf7 1261int
58f9752a 1262same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1263{
1264 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1265 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1266 {
6615c446
JO
1267 case tcc_declaration:
1268 t1 = DECL_CONTEXT (t1); break;
1269 case tcc_type:
1270 t1 = TYPE_CONTEXT (t1); break;
1271 case tcc_exceptional:
1272 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1273 default: gcc_unreachable ();
766beae1
ZW
1274 }
1275
1276 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1277 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1278 {
6615c446
JO
1279 case tcc_declaration:
1280 t2 = DECL_CONTEXT (t2); break;
1281 case tcc_type:
1282 t2 = TYPE_CONTEXT (t2); break;
1283 case tcc_exceptional:
1284 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1285 default: gcc_unreachable ();
766beae1
ZW
1286 }
1287
1288 return t1 == t2;
1289}
1290
f13c9b2c 1291/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1292
f13c9b2c 1293static struct tagged_tu_seen_cache *
58f9752a 1294alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1295{
cceb1885 1296 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1297 tu->next = tagged_tu_seen_base;
1298 tu->t1 = t1;
1299 tu->t2 = t2;
c22cacf3 1300
f13c9b2c 1301 tagged_tu_seen_base = tu;
c22cacf3 1302
f13c9b2c
AP
1303 /* The C standard says that two structures in different translation
1304 units are compatible with each other only if the types of their
1305 fields are compatible (among other things). We assume that they
1306 are compatible until proven otherwise when building the cache.
1307 An example where this can occur is:
1308 struct a
1309 {
1310 struct a *next;
1311 };
1312 If we are comparing this against a similar struct in another TU,
c83eecad 1313 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1314 loop. */
1315 tu->val = 1;
1316 return tu;
1317}
d1bd0ded 1318
f13c9b2c 1319/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1320
f13c9b2c
AP
1321static void
1322free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1323{
1324 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1325 while (tu != tu_til)
1326 {
741ac903
KG
1327 const struct tagged_tu_seen_cache *const tu1
1328 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1329 tu = tu1->next;
b1d5455a 1330 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1331 }
1332 tagged_tu_seen_base = tu_til;
1333}
d1bd0ded
GK
1334
1335/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1336 compatible. If the two types are not the same (which has been
1337 checked earlier), this can only happen when multiple translation
1338 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1339 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1340 comptypes_internal. */
d1bd0ded
GK
1341
1342static int
744aa42f 1343tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1344 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1345{
1346 tree s1, s2;
1347 bool needs_warning = false;
3aeb3655 1348
d1bd0ded
GK
1349 /* We have to verify that the tags of the types are the same. This
1350 is harder than it looks because this may be a typedef, so we have
1351 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1352 typedef...
1353 In the case of compiler-created builtin structs the TYPE_DECL
1354 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1355 while (TYPE_NAME (t1)
1356 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1357 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1358 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1359
dea984dc
ILT
1360 while (TYPE_NAME (t2)
1361 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1362 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1363 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1364
1365 /* C90 didn't have the requirement that the two tags be the same. */
1366 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1367 return 0;
3aeb3655 1368
d1bd0ded
GK
1369 /* C90 didn't say what happened if one or both of the types were
1370 incomplete; we choose to follow C99 rules here, which is that they
1371 are compatible. */
1372 if (TYPE_SIZE (t1) == NULL
1373 || TYPE_SIZE (t2) == NULL)
1374 return 1;
3aeb3655 1375
d1bd0ded 1376 {
f13c9b2c 1377 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1378 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1379 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1380 return tts_i->val;
d1bd0ded 1381 }
3aeb3655 1382
d1bd0ded
GK
1383 switch (TREE_CODE (t1))
1384 {
1385 case ENUMERAL_TYPE:
1386 {
f13c9b2c 1387 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1388 /* Speed up the case where the type values are in the same order. */
1389 tree tv1 = TYPE_VALUES (t1);
1390 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1391
c22cacf3 1392 if (tv1 == tv2)
f13c9b2c
AP
1393 {
1394 return 1;
1395 }
3aeb3655 1396
c22cacf3
MS
1397 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1398 {
1399 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1400 break;
1401 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1402 {
c22cacf3 1403 tu->val = 0;
f13c9b2c
AP
1404 return 0;
1405 }
c22cacf3 1406 }
3aeb3655 1407
c22cacf3 1408 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1409 {
1410 return 1;
1411 }
c22cacf3 1412 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1413 {
1414 tu->val = 0;
1415 return 0;
1416 }
3aeb3655 1417
d1bd0ded 1418 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1419 {
1420 tu->val = 0;
1421 return 0;
1422 }
3aeb3655 1423
d1bd0ded
GK
1424 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1425 {
1426 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1427 if (s2 == NULL
1428 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1429 {
1430 tu->val = 0;
1431 return 0;
1432 }
d1bd0ded
GK
1433 }
1434 return 1;
1435 }
1436
1437 case UNION_TYPE:
1438 {
f13c9b2c 1439 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1440 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1441 {
1442 tu->val = 0;
1443 return 0;
1444 }
c22cacf3 1445
f13c9b2c
AP
1446 /* Speed up the common case where the fields are in the same order. */
1447 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1448 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1449 {
1450 int result;
c22cacf3 1451
3ae4d3cc 1452 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1453 break;
744aa42f 1454 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1455 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1456
1457 if (result != 1 && !DECL_NAME (s1))
1458 break;
f13c9b2c
AP
1459 if (result == 0)
1460 {
1461 tu->val = 0;
1462 return 0;
1463 }
1464 if (result == 2)
1465 needs_warning = true;
1466
1467 if (TREE_CODE (s1) == FIELD_DECL
1468 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1469 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1470 {
1471 tu->val = 0;
1472 return 0;
1473 }
1474 }
1475 if (!s1 && !s2)
1476 {
1477 tu->val = needs_warning ? 2 : 1;
1478 return tu->val;
1479 }
d1bd0ded 1480
910ad8de 1481 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1482 {
1483 bool ok = false;
3aeb3655 1484
910ad8de 1485 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1486 if (DECL_NAME (s1) == DECL_NAME (s2))
1487 {
1488 int result;
1489
744aa42f 1490 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1491 enum_and_int_p,
1492 different_types_p);
3ae4d3cc
AO
1493
1494 if (result != 1 && !DECL_NAME (s1))
1495 continue;
1496 if (result == 0)
1497 {
1498 tu->val = 0;
1499 return 0;
1500 }
1501 if (result == 2)
1502 needs_warning = true;
1503
1504 if (TREE_CODE (s1) == FIELD_DECL
1505 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1506 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1507 break;
3ae4d3cc
AO
1508
1509 ok = true;
1510 break;
1511 }
3f75a254 1512 if (!ok)
f13c9b2c
AP
1513 {
1514 tu->val = 0;
1515 return 0;
1516 }
d1bd0ded 1517 }
f13c9b2c
AP
1518 tu->val = needs_warning ? 2 : 10;
1519 return tu->val;
d1bd0ded
GK
1520 }
1521
1522 case RECORD_TYPE:
1523 {
c22cacf3 1524 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1525
1526 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1527 s1 && s2;
910ad8de 1528 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1529 {
1530 int result;
1531 if (TREE_CODE (s1) != TREE_CODE (s2)
1532 || DECL_NAME (s1) != DECL_NAME (s2))
1533 break;
744aa42f 1534 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1535 enum_and_int_p, different_types_p);
d1bd0ded
GK
1536 if (result == 0)
1537 break;
1538 if (result == 2)
1539 needs_warning = true;
3aeb3655 1540
d1bd0ded
GK
1541 if (TREE_CODE (s1) == FIELD_DECL
1542 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1543 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1544 break;
1545 }
d1bd0ded 1546 if (s1 && s2)
f13c9b2c
AP
1547 tu->val = 0;
1548 else
1549 tu->val = needs_warning ? 2 : 1;
1550 return tu->val;
d1bd0ded
GK
1551 }
1552
1553 default:
366de0ce 1554 gcc_unreachable ();
d1bd0ded
GK
1555 }
1556}
1557
400fbf9f
JW
1558/* Return 1 if two function types F1 and F2 are compatible.
1559 If either type specifies no argument types,
1560 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1561 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1562 the other must specify that number of self-promoting arg types.
744aa42f 1563 Otherwise, the argument types must match.
dc5027f4 1564 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1565
1566static int
744aa42f 1567function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1568 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1569{
1570 tree args1, args2;
1571 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1572 int val = 1;
1573 int val1;
a6fdc086
GK
1574 tree ret1, ret2;
1575
1576 ret1 = TREE_TYPE (f1);
1577 ret2 = TREE_TYPE (f2);
1578
e508a019
JM
1579 /* 'volatile' qualifiers on a function's return type used to mean
1580 the function is noreturn. */
1581 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1582 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1583 if (TYPE_VOLATILE (ret1))
1584 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1585 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1586 if (TYPE_VOLATILE (ret2))
1587 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1588 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1589 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1590 if (val == 0)
400fbf9f
JW
1591 return 0;
1592
1593 args1 = TYPE_ARG_TYPES (f1);
1594 args2 = TYPE_ARG_TYPES (f2);
1595
dc5027f4
JM
1596 if (different_types_p != NULL
1597 && (args1 == 0) != (args2 == 0))
1598 *different_types_p = true;
1599
400fbf9f
JW
1600 /* An unspecified parmlist matches any specified parmlist
1601 whose argument types don't need default promotions. */
1602
1603 if (args1 == 0)
1604 {
1605 if (!self_promoting_args_p (args2))
1606 return 0;
1607 /* If one of these types comes from a non-prototype fn definition,
1608 compare that with the other type's arglist.
3176a0c2 1609 If they don't match, ask for a warning (but no error). */
400fbf9f 1610 if (TYPE_ACTUAL_ARG_TYPES (f1)
744aa42f 1611 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
dc5027f4 1612 enum_and_int_p, different_types_p))
400fbf9f
JW
1613 val = 2;
1614 return val;
1615 }
1616 if (args2 == 0)
1617 {
1618 if (!self_promoting_args_p (args1))
1619 return 0;
1620 if (TYPE_ACTUAL_ARG_TYPES (f2)
744aa42f 1621 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
dc5027f4 1622 enum_and_int_p, different_types_p))
400fbf9f
JW
1623 val = 2;
1624 return val;
1625 }
1626
1627 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1628 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1629 different_types_p);
400fbf9f
JW
1630 return val1 != 1 ? val1 : val;
1631}
1632
744aa42f
ILT
1633/* Check two lists of types for compatibility, returning 0 for
1634 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1635 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1636 comptypes_internal. */
400fbf9f
JW
1637
1638static int
744aa42f 1639type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1640 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1641{
1642 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1643 int val = 1;
9d5f3e49 1644 int newval = 0;
400fbf9f
JW
1645
1646 while (1)
1647 {
46df2823 1648 tree a1, mv1, a2, mv2;
400fbf9f
JW
1649 if (args1 == 0 && args2 == 0)
1650 return val;
1651 /* If one list is shorter than the other,
1652 they fail to match. */
1653 if (args1 == 0 || args2 == 0)
1654 return 0;
46df2823
JM
1655 mv1 = a1 = TREE_VALUE (args1);
1656 mv2 = a2 = TREE_VALUE (args2);
1657 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1658 mv1 = (TYPE_ATOMIC (mv1)
1659 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1660 TYPE_QUAL_ATOMIC)
1661 : TYPE_MAIN_VARIANT (mv1));
46df2823 1662 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1663 mv2 = (TYPE_ATOMIC (mv2)
1664 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1665 TYPE_QUAL_ATOMIC)
1666 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1667 /* A null pointer instead of a type
1668 means there is supposed to be an argument
1669 but nothing is specified about what type it has.
1670 So match anything that self-promotes. */
dc5027f4
JM
1671 if (different_types_p != NULL
1672 && (a1 == 0) != (a2 == 0))
1673 *different_types_p = true;
46df2823 1674 if (a1 == 0)
400fbf9f 1675 {
46df2823 1676 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1677 return 0;
1678 }
46df2823 1679 else if (a2 == 0)
400fbf9f 1680 {
46df2823 1681 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1682 return 0;
1683 }
8f5b6d29 1684 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1685 else if (TREE_CODE (a1) == ERROR_MARK
1686 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1687 ;
dc5027f4
JM
1688 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1689 different_types_p)))
400fbf9f 1690 {
dc5027f4
JM
1691 if (different_types_p != NULL)
1692 *different_types_p = true;
400fbf9f
JW
1693 /* Allow wait (union {union wait *u; int *i} *)
1694 and wait (union wait *) to be compatible. */
46df2823
JM
1695 if (TREE_CODE (a1) == UNION_TYPE
1696 && (TYPE_NAME (a1) == 0
ebf0bf7f 1697 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1698 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1699 && tree_int_cst_equal (TYPE_SIZE (a1),
1700 TYPE_SIZE (a2)))
400fbf9f
JW
1701 {
1702 tree memb;
46df2823 1703 for (memb = TYPE_FIELDS (a1);
910ad8de 1704 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1705 {
1706 tree mv3 = TREE_TYPE (memb);
1707 if (mv3 && mv3 != error_mark_node
1708 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1709 mv3 = (TYPE_ATOMIC (mv3)
1710 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1711 TYPE_QUAL_ATOMIC)
1712 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1713 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1714 different_types_p))
58cb41e6
JJ
1715 break;
1716 }
400fbf9f
JW
1717 if (memb == 0)
1718 return 0;
1719 }
46df2823
JM
1720 else if (TREE_CODE (a2) == UNION_TYPE
1721 && (TYPE_NAME (a2) == 0
ebf0bf7f 1722 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1723 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1724 && tree_int_cst_equal (TYPE_SIZE (a2),
1725 TYPE_SIZE (a1)))
400fbf9f
JW
1726 {
1727 tree memb;
46df2823 1728 for (memb = TYPE_FIELDS (a2);
910ad8de 1729 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1730 {
1731 tree mv3 = TREE_TYPE (memb);
1732 if (mv3 && mv3 != error_mark_node
1733 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1734 mv3 = (TYPE_ATOMIC (mv3)
1735 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1736 TYPE_QUAL_ATOMIC)
1737 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1738 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1739 different_types_p))
58cb41e6
JJ
1740 break;
1741 }
400fbf9f
JW
1742 if (memb == 0)
1743 return 0;
1744 }
1745 else
1746 return 0;
1747 }
1748
1749 /* comptypes said ok, but record if it said to warn. */
1750 if (newval > val)
1751 val = newval;
1752
1753 args1 = TREE_CHAIN (args1);
1754 args2 = TREE_CHAIN (args2);
1755 }
1756}
400fbf9f 1757\f
a0e24419
MP
1758/* Compute the size to increment a pointer by. When a function type or void
1759 type or incomplete type is passed, size_one_node is returned.
1760 This function does not emit any diagnostics; the caller is responsible
1761 for that. */
400fbf9f 1762
4e2fb7de 1763static tree
58f9752a 1764c_size_in_bytes (const_tree type)
400fbf9f
JW
1765{
1766 enum tree_code code = TREE_CODE (type);
1767
a0e24419
MP
1768 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1769 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1770 return size_one_node;
1771
400fbf9f 1772 /* Convert in case a char is more than one unit. */
db3927fb
AH
1773 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1774 size_int (TYPE_PRECISION (char_type_node)
1775 / BITS_PER_UNIT));
400fbf9f 1776}
400fbf9f 1777\f
400fbf9f
JW
1778/* Return either DECL or its known constant value (if it has one). */
1779
56cb9733 1780tree
2f6e4e97 1781decl_constant_value (tree decl)
400fbf9f 1782{
a7c1916a 1783 if (/* Don't change a variable array bound or initial value to a constant
4f976745
RK
1784 in a place where a variable is invalid. Note that DECL_INITIAL
1785 isn't valid for a PARM_DECL. */
a7c1916a 1786 current_function_decl != 0
4f976745 1787 && TREE_CODE (decl) != PARM_DECL
3f75a254 1788 && !TREE_THIS_VOLATILE (decl)
83bab8db 1789 && TREE_READONLY (decl)
400fbf9f
JW
1790 && DECL_INITIAL (decl) != 0
1791 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1792 /* This is invalid if initial value is not constant.
1793 If it has either a function call, a memory reference,
1794 or a variable, then re-evaluating it could give different results. */
1795 && TREE_CONSTANT (DECL_INITIAL (decl))
1796 /* Check for cases where this is sub-optimal, even though valid. */
2f74f7e9 1797 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
400fbf9f
JW
1798 return DECL_INITIAL (decl);
1799 return decl;
1800}
1801
f2a71bbc
JM
1802/* Convert the array expression EXP to a pointer. */
1803static tree
c2255bc4 1804array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1805{
f2a71bbc 1806 tree orig_exp = exp;
207bf485 1807 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1808 tree adr;
1809 tree restype = TREE_TYPE (type);
1810 tree ptrtype;
207bf485 1811
f2a71bbc 1812 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1813
f2a71bbc 1814 STRIP_TYPE_NOPS (exp);
207bf485 1815
487a92fe
JM
1816 if (TREE_NO_WARNING (orig_exp))
1817 TREE_NO_WARNING (exp) = 1;
207bf485 1818
f2a71bbc
JM
1819 ptrtype = build_pointer_type (restype);
1820
1821 if (TREE_CODE (exp) == INDIRECT_REF)
1822 return convert (ptrtype, TREE_OPERAND (exp, 0));
1823
1f37c583
JM
1824 /* In C++ array compound literals are temporary objects unless they are
1825 const or appear in namespace scope, so they are destroyed too soon
1826 to use them for much of anything (c++/53220). */
1827 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1828 {
1829 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1830 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1831 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1832 "converting an array compound literal to a pointer "
1833 "is ill-formed in C++");
1834 }
1835
c2255bc4 1836 adr = build_unary_op (loc, ADDR_EXPR, exp, 1);
f2a71bbc
JM
1837 return convert (ptrtype, adr);
1838}
207bf485 1839
f2a71bbc
JM
1840/* Convert the function expression EXP to a pointer. */
1841static tree
c2255bc4 1842function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1843{
1844 tree orig_exp = exp;
207bf485 1845
f2a71bbc 1846 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1847
f2a71bbc 1848 STRIP_TYPE_NOPS (exp);
207bf485 1849
f2a71bbc
JM
1850 if (TREE_NO_WARNING (orig_exp))
1851 TREE_NO_WARNING (exp) = 1;
207bf485 1852
c2255bc4 1853 return build_unary_op (loc, ADDR_EXPR, exp, 0);
f2a71bbc 1854}
207bf485 1855
ebfbbdc5
JJ
1856/* Mark EXP as read, not just set, for set but not used -Wunused
1857 warning purposes. */
1858
1859void
1860mark_exp_read (tree exp)
1861{
1862 switch (TREE_CODE (exp))
1863 {
1864 case VAR_DECL:
1865 case PARM_DECL:
1866 DECL_READ_P (exp) = 1;
1867 break;
1868 case ARRAY_REF:
1869 case COMPONENT_REF:
1870 case MODIFY_EXPR:
1871 case REALPART_EXPR:
1872 case IMAGPART_EXPR:
1873 CASE_CONVERT:
1874 case ADDR_EXPR:
1875 mark_exp_read (TREE_OPERAND (exp, 0));
1876 break;
1877 case COMPOUND_EXPR:
82c3c067 1878 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1879 mark_exp_read (TREE_OPERAND (exp, 1));
1880 break;
1881 default:
1882 break;
1883 }
1884}
1885
f2a71bbc
JM
1886/* Perform the default conversion of arrays and functions to pointers.
1887 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1888 return EXP.
1889
1890 LOC is the location of the expression. */
f2a71bbc
JM
1891
1892struct c_expr
c2255bc4 1893default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1894{
1895 tree orig_exp = exp.value;
1896 tree type = TREE_TYPE (exp.value);
1897 enum tree_code code = TREE_CODE (type);
1898
1899 switch (code)
1900 {
1901 case ARRAY_TYPE:
1902 {
1903 bool not_lvalue = false;
1904 bool lvalue_array_p;
1905
1906 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1907 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1908 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1909 {
1910 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1911 not_lvalue = true;
1912 exp.value = TREE_OPERAND (exp.value, 0);
1913 }
1914
1915 if (TREE_NO_WARNING (orig_exp))
1916 TREE_NO_WARNING (exp.value) = 1;
1917
1918 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1919 if (!flag_isoc99 && !lvalue_array_p)
1920 {
1921 /* Before C99, non-lvalue arrays do not decay to pointers.
1922 Normally, using such an array would be invalid; but it can
1923 be used correctly inside sizeof or as a statement expression.
1924 Thus, do not give an error here; an error will result later. */
1925 return exp;
1926 }
1927
c2255bc4 1928 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1929 }
1930 break;
1931 case FUNCTION_TYPE:
c2255bc4 1932 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1933 break;
1934 default:
f2a71bbc 1935 break;
207bf485 1936 }
f2a71bbc 1937
207bf485
JM
1938 return exp;
1939}
1940
ebfbbdc5
JJ
1941struct c_expr
1942default_function_array_read_conversion (location_t loc, struct c_expr exp)
1943{
1944 mark_exp_read (exp.value);
1945 return default_function_array_conversion (loc, exp);
1946}
522ddfa2 1947
267bac10
JM
1948/* Return whether EXPR should be treated as an atomic lvalue for the
1949 purposes of load and store handling. */
1950
1951static bool
1952really_atomic_lvalue (tree expr)
1953{
1954 if (expr == error_mark_node || TREE_TYPE (expr) == error_mark_node)
1955 return false;
1956 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
1957 return false;
1958 if (!lvalue_p (expr))
1959 return false;
1960
1961 /* Ignore _Atomic on register variables, since their addresses can't
1962 be taken so (a) atomicity is irrelevant and (b) the normal atomic
1963 sequences wouldn't work. Ignore _Atomic on structures containing
1964 bit-fields, since accessing elements of atomic structures or
1965 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
1966 it's undefined at translation time or execution time, and the
1967 normal atomic sequences again wouldn't work. */
1968 while (handled_component_p (expr))
1969 {
1970 if (TREE_CODE (expr) == COMPONENT_REF
1971 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
1972 return false;
1973 expr = TREE_OPERAND (expr, 0);
1974 }
1975 if (DECL_P (expr) && C_DECL_REGISTER (expr))
1976 return false;
1977 return true;
1978}
1979
1980/* Convert expression EXP (location LOC) from lvalue to rvalue,
1981 including converting functions and arrays to pointers if CONVERT_P.
1982 If READ_P, also mark the expression as having been read. */
1983
1984struct c_expr
1985convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
1986 bool convert_p, bool read_p)
1987{
1988 if (read_p)
1989 mark_exp_read (exp.value);
1990 if (convert_p)
1991 exp = default_function_array_conversion (loc, exp);
1992 if (really_atomic_lvalue (exp.value))
1993 {
1994 vec<tree, va_gc> *params;
1995 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
1996 tree expr_type = TREE_TYPE (exp.value);
1997 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, 0);
1998 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
1999
2000 gcc_assert (TYPE_ATOMIC (expr_type));
2001
2002 /* Expansion of a generic atomic load may require an addition
2003 element, so allocate enough to prevent a resize. */
2004 vec_alloc (params, 4);
2005
2006 /* Remove the qualifiers for the rest of the expressions and
2007 create the VAL temp variable to hold the RHS. */
2008 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
2009 tmp = create_tmp_var (nonatomic_type, NULL);
2010 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, 0);
2011 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2012 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2013
2014 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2015 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2016 params->quick_push (expr_addr);
2017 params->quick_push (tmp_addr);
2018 params->quick_push (seq_cst);
8edbfaa6 2019 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2020
cc28fc7f
MP
2021 /* EXPR is always read. */
2022 mark_exp_read (exp.value);
2023
267bac10
JM
2024 /* Return tmp which contains the value loaded. */
2025 exp.value = build2 (COMPOUND_EXPR, nonatomic_type, func_call, tmp);
2026 }
2027 return exp;
2028}
2029
522ddfa2
JM
2030/* EXP is an expression of integer type. Apply the integer promotions
2031 to it and return the promoted value. */
400fbf9f
JW
2032
2033tree
522ddfa2 2034perform_integral_promotions (tree exp)
400fbf9f 2035{
b3694847
SS
2036 tree type = TREE_TYPE (exp);
2037 enum tree_code code = TREE_CODE (type);
400fbf9f 2038
522ddfa2 2039 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2040
400fbf9f
JW
2041 /* Normally convert enums to int,
2042 but convert wide enums to something wider. */
2043 if (code == ENUMERAL_TYPE)
2044 {
b0c48229
NB
2045 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2046 TYPE_PRECISION (integer_type_node)),
2047 ((TYPE_PRECISION (type)
2048 >= TYPE_PRECISION (integer_type_node))
8df83eae 2049 && TYPE_UNSIGNED (type)));
05bccae2 2050
400fbf9f
JW
2051 return convert (type, exp);
2052 }
2053
522ddfa2
JM
2054 /* ??? This should no longer be needed now bit-fields have their
2055 proper types. */
9753f113 2056 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2057 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2058 /* If it's thinner than an int, promote it like a
d72040f5 2059 c_promoting_integer_type_p, otherwise leave it alone. */
05bccae2
RK
2060 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2061 TYPE_PRECISION (integer_type_node)))
f458d1d5 2062 return convert (integer_type_node, exp);
9753f113 2063
d72040f5 2064 if (c_promoting_integer_type_p (type))
400fbf9f 2065 {
f458d1d5 2066 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2067 if (TYPE_UNSIGNED (type)
f458d1d5 2068 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2069 return convert (unsigned_type_node, exp);
05bccae2 2070
400fbf9f
JW
2071 return convert (integer_type_node, exp);
2072 }
05bccae2 2073
522ddfa2
JM
2074 return exp;
2075}
2076
2077
2078/* Perform default promotions for C data used in expressions.
46bdb9cf 2079 Enumeral types or short or char are converted to int.
522ddfa2
JM
2080 In addition, manifest constants symbols are replaced by their values. */
2081
2082tree
2083default_conversion (tree exp)
2084{
2085 tree orig_exp;
2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
40449a90 2088 tree promoted_type;
522ddfa2 2089
ebfbbdc5
JJ
2090 mark_exp_read (exp);
2091
46bdb9cf
JM
2092 /* Functions and arrays have been converted during parsing. */
2093 gcc_assert (code != FUNCTION_TYPE);
2094 if (code == ARRAY_TYPE)
2095 return exp;
522ddfa2
JM
2096
2097 /* Constants can be used directly unless they're not loadable. */
2098 if (TREE_CODE (exp) == CONST_DECL)
2099 exp = DECL_INITIAL (exp);
2100
522ddfa2
JM
2101 /* Strip no-op conversions. */
2102 orig_exp = exp;
2103 STRIP_TYPE_NOPS (exp);
2104
2105 if (TREE_NO_WARNING (orig_exp))
2106 TREE_NO_WARNING (exp) = 1;
2107
400fbf9f
JW
2108 if (code == VOID_TYPE)
2109 {
5436fa2e
MP
2110 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2111 "void value not ignored as it ought to be");
400fbf9f
JW
2112 return error_mark_node;
2113 }
808d6eaa
JM
2114
2115 exp = require_complete_type (exp);
2116 if (exp == error_mark_node)
2117 return error_mark_node;
2118
40449a90
SL
2119 promoted_type = targetm.promoted_type (type);
2120 if (promoted_type)
2121 return convert (promoted_type, exp);
2122
808d6eaa
JM
2123 if (INTEGRAL_TYPE_P (type))
2124 return perform_integral_promotions (exp);
2125
400fbf9f
JW
2126 return exp;
2127}
2128\f
0fb96aa4 2129/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2130
2131 If the component name is not found, returns NULL_TREE. Otherwise,
2132 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2133 stepping down the chain to the component, which is in the last
2134 TREE_VALUE of the list. Normally the list is of length one, but if
2135 the component is embedded within (nested) anonymous structures or
2136 unions, the list steps down the chain to the component. */
2f6e4e97 2137
2f2d13da 2138static tree
0fb96aa4 2139lookup_field (tree type, tree component)
2f2d13da
DE
2140{
2141 tree field;
2142
2143 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2144 to the field elements. Use a binary search on this array to quickly
2145 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2146 will always be set for structures which have many elements. */
2147
22a0b85f 2148 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
2f2d13da
DE
2149 {
2150 int bot, top, half;
d07605f5 2151 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2152
2153 field = TYPE_FIELDS (type);
2154 bot = 0;
d07605f5 2155 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2156 while (top - bot > 1)
2157 {
2f2d13da
DE
2158 half = (top - bot + 1) >> 1;
2159 field = field_array[bot+half];
2160
2161 if (DECL_NAME (field) == NULL_TREE)
2162 {
2163 /* Step through all anon unions in linear fashion. */
2164 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2165 {
2f2d13da 2166 field = field_array[bot++];
a68b98cf
RK
2167 if (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2168 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
19d76e60 2169 {
0fb96aa4 2170 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2171
2172 if (anon)
2173 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2174
2175 /* The Plan 9 compiler permits referring
2176 directly to an anonymous struct/union field
2177 using a typedef name. */
2178 if (flag_plan9_extensions
2179 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2180 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2181 == TYPE_DECL)
2182 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2183 == component))
2184 break;
2f6e4e97 2185 }
2f2d13da
DE
2186 }
2187
2188 /* Entire record is only anon unions. */
2189 if (bot > top)
2190 return NULL_TREE;
2191
2192 /* Restart the binary search, with new lower bound. */
2193 continue;
2194 }
2195
e8b87aac 2196 if (DECL_NAME (field) == component)
2f2d13da 2197 break;
e8b87aac 2198 if (DECL_NAME (field) < component)
2f2d13da
DE
2199 bot += half;
2200 else
2201 top = bot + half;
2202 }
2203
2204 if (DECL_NAME (field_array[bot]) == component)
2205 field = field_array[bot];
2206 else if (DECL_NAME (field) != component)
e9b2c823 2207 return NULL_TREE;
2f2d13da
DE
2208 }
2209 else
2210 {
910ad8de 2211 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2212 {
e9b2c823
NB
2213 if (DECL_NAME (field) == NULL_TREE
2214 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
2215 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
2f2d13da 2216 {
0fb96aa4 2217 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2218
e9b2c823
NB
2219 if (anon)
2220 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2221
2222 /* The Plan 9 compiler permits referring directly to an
2223 anonymous struct/union field using a typedef
2224 name. */
2225 if (flag_plan9_extensions
2226 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2227 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2228 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2229 == component))
2230 break;
2f2d13da
DE
2231 }
2232
2233 if (DECL_NAME (field) == component)
2234 break;
2235 }
e9b2c823
NB
2236
2237 if (field == NULL_TREE)
2238 return NULL_TREE;
2f2d13da
DE
2239 }
2240
e9b2c823 2241 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2242}
2243
c2255bc4
AH
2244/* Make an expression to refer to the COMPONENT field of structure or
2245 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
2246 location of the COMPONENT_REF. */
400fbf9f
JW
2247
2248tree
c2255bc4 2249build_component_ref (location_t loc, tree datum, tree component)
400fbf9f 2250{
b3694847
SS
2251 tree type = TREE_TYPE (datum);
2252 enum tree_code code = TREE_CODE (type);
2253 tree field = NULL;
2254 tree ref;
1e57bf47 2255 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2256
7a3ea201
RH
2257 if (!objc_is_public (datum, component))
2258 return error_mark_node;
2259
46a88c12 2260 /* Detect Objective-C property syntax object.property. */
668ea4b1 2261 if (c_dialect_objc ()
46a88c12 2262 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2263 return ref;
2264
400fbf9f
JW
2265 /* See if there is a field or component with name COMPONENT. */
2266
2267 if (code == RECORD_TYPE || code == UNION_TYPE)
2268 {
d0f062fb 2269 if (!COMPLETE_TYPE_P (type))
400fbf9f 2270 {
7a228918 2271 c_incomplete_type_error (NULL_TREE, type);
400fbf9f
JW
2272 return error_mark_node;
2273 }
2274
0fb96aa4 2275 field = lookup_field (type, component);
400fbf9f
JW
2276
2277 if (!field)
2278 {
c2255bc4 2279 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2280 return error_mark_node;
2281 }
400fbf9f 2282
e9b2c823
NB
2283 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2284 This might be better solved in future the way the C++ front
2285 end does it - by giving the anonymous entities each a
2286 separate name and type, and then have build_component_ref
2287 recursively call itself. We can't do that here. */
46ea50cb 2288 do
19d76e60 2289 {
e9b2c823 2290 tree subdatum = TREE_VALUE (field);
efed193e
JM
2291 int quals;
2292 tree subtype;
1e57bf47 2293 bool use_datum_quals;
e9b2c823
NB
2294
2295 if (TREE_TYPE (subdatum) == error_mark_node)
2296 return error_mark_node;
2297
1e57bf47
JM
2298 /* If this is an rvalue, it does not have qualifiers in C
2299 standard terms and we must avoid propagating such
2300 qualifiers down to a non-lvalue array that is then
2301 converted to a pointer. */
2302 use_datum_quals = (datum_lvalue
2303 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2304
efed193e 2305 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2306 if (use_datum_quals)
2307 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2308 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2309
2310 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2311 NULL_TREE);
c2255bc4 2312 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2313 if (TREE_READONLY (subdatum)
2314 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2315 TREE_READONLY (ref) = 1;
1e57bf47
JM
2316 if (TREE_THIS_VOLATILE (subdatum)
2317 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2318 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2319
2320 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2321 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2322
19d76e60 2323 datum = ref;
46ea50cb
RS
2324
2325 field = TREE_CHAIN (field);
19d76e60 2326 }
46ea50cb 2327 while (field);
19d76e60 2328
400fbf9f
JW
2329 return ref;
2330 }
2331 else if (code != ERROR_MARK)
c2255bc4
AH
2332 error_at (loc,
2333 "request for member %qE in something not a structure or union",
2334 component);
400fbf9f
JW
2335
2336 return error_mark_node;
2337}
2338\f
2339/* Given an expression PTR for a pointer, return an expression
2340 for the value pointed to.
6a3799eb
AH
2341 ERRORSTRING is the name of the operator to appear in error messages.
2342
2343 LOC is the location to use for the generated tree. */
400fbf9f
JW
2344
2345tree
dd865ef6 2346build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2347{
b3694847
SS
2348 tree pointer = default_conversion (ptr);
2349 tree type = TREE_TYPE (pointer);
6a3799eb 2350 tree ref;
400fbf9f
JW
2351
2352 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2353 {
1043771b 2354 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2355 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2356 {
2357 /* If a warning is issued, mark it to avoid duplicates from
2358 the backend. This only needs to be done at
2359 warn_strict_aliasing > 2. */
2360 if (warn_strict_aliasing > 2)
2361 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2362 type, TREE_OPERAND (pointer, 0)))
2363 TREE_NO_WARNING (pointer) = 1;
2364 }
2365
870cc33b 2366 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2367 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2368 == TREE_TYPE (type)))
6a3799eb
AH
2369 {
2370 ref = TREE_OPERAND (pointer, 0);
2371 protected_set_expr_location (ref, loc);
2372 return ref;
2373 }
870cc33b
RS
2374 else
2375 {
2376 tree t = TREE_TYPE (type);
46df2823 2377
984dfd8c 2378 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2379
baae9b65 2380 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2381 {
c9f9eb5d 2382 error_at (loc, "dereferencing pointer to incomplete type");
870cc33b
RS
2383 return error_mark_node;
2384 }
7d882b83 2385 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2386 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2387
2388 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2389 so that we get the proper error message if the result is used
2390 to assign to. Also, &* is supposed to be a no-op.
2391 And ANSI C seems to specify that the type of the result
2392 should be the const type. */
2393 /* A de-reference of a pointer to const is not a const. It is valid
2394 to change it via some other pointer. */
2395 TREE_READONLY (ref) = TYPE_READONLY (t);
2396 TREE_SIDE_EFFECTS (ref)
271bd540 2397 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2398 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2399 protected_set_expr_location (ref, loc);
870cc33b
RS
2400 return ref;
2401 }
2402 }
400fbf9f 2403 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2404 invalid_indirection_error (loc, type, errstring);
2405
400fbf9f
JW
2406 return error_mark_node;
2407}
2408
2409/* This handles expressions of the form "a[i]", which denotes
2410 an array reference.
2411
2412 This is logically equivalent in C to *(a+i), but we may do it differently.
2413 If A is a variable or a member, we generate a primitive ARRAY_REF.
2414 This avoids forcing the array out of registers, and can work on
2415 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2416 by functions).
2417
30cd1c5d
AS
2418 For vector types, allow vector[i] but not i[vector], and create
2419 *(((type*)&vectortype) + i) for the expression.
2420
6a3799eb 2421 LOC is the location to use for the returned expression. */
400fbf9f
JW
2422
2423tree
c2255bc4 2424build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2425{
6a3799eb 2426 tree ret;
a4ab7973 2427 bool swapped = false;
400fbf9f
JW
2428 if (TREE_TYPE (array) == error_mark_node
2429 || TREE_TYPE (index) == error_mark_node)
2430 return error_mark_node;
2431
b72271b9 2432 if (flag_cilkplus && contains_array_notation_expr (index))
36536d79
BI
2433 {
2434 size_t rank = 0;
2435 if (!find_rank (loc, index, index, true, &rank))
2436 return error_mark_node;
2437 if (rank > 1)
2438 {
2439 error_at (loc, "rank of the array's index is greater than 1");
2440 return error_mark_node;
2441 }
2442 }
a4ab7973 2443 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2444 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2445 /* Allow vector[index] but not index[vector]. */
2446 && TREE_CODE (TREE_TYPE (array)) != VECTOR_TYPE)
400fbf9f 2447 {
a4ab7973
JM
2448 tree temp;
2449 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2450 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2451 {
f90e8e2e 2452 error_at (loc,
30cd1c5d
AS
2453 "subscripted value is neither array nor pointer nor vector");
2454
fdeefd49
RS
2455 return error_mark_node;
2456 }
a4ab7973
JM
2457 temp = array;
2458 array = index;
2459 index = temp;
2460 swapped = true;
2461 }
2462
2463 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2464 {
a63068b6 2465 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2466 return error_mark_node;
2467 }
2468
2469 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2470 {
a63068b6 2471 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2472 return error_mark_node;
2473 }
2474
ff6b6641
GDR
2475 /* ??? Existing practice has been to warn only when the char
2476 index is syntactically the index, not for char[array]. */
2477 if (!swapped)
2478 warn_array_subscript_with_type_char (index);
a4ab7973
JM
2479
2480 /* Apply default promotions *after* noticing character types. */
2481 index = default_conversion (index);
2482
2483 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2484
7edaa4d2 2485 convert_vector_to_pointer_for_subscript (loc, &array, index);
a4ab7973
JM
2486
2487 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2488 {
e4d35515 2489 tree rval, type;
fdeefd49 2490
400fbf9f
JW
2491 /* An array that is indexed by a non-constant
2492 cannot be stored in a register; we must be able to do
2493 address arithmetic on its address.
2494 Likewise an array of elements of variable size. */
2495 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2496 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2497 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2498 {
dffd7eb6 2499 if (!c_mark_addressable (array))
400fbf9f
JW
2500 return error_mark_node;
2501 }
e6d52559
JW
2502 /* An array that is indexed by a constant value which is not within
2503 the array bounds cannot be stored in a register either; because we
2504 would get a crash in store_bit_field/extract_bit_field when trying
2505 to access a non-existent part of the register. */
2506 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2507 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2508 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2509 {
dffd7eb6 2510 if (!c_mark_addressable (array))
e6d52559
JW
2511 return error_mark_node;
2512 }
400fbf9f 2513
400fbf9f
JW
2514 if (pedantic)
2515 {
2516 tree foo = array;
2517 while (TREE_CODE (foo) == COMPONENT_REF)
2518 foo = TREE_OPERAND (foo, 0);
5baeaac0 2519 if (TREE_CODE (foo) == VAR_DECL && C_DECL_REGISTER (foo))
c1771a20 2520 pedwarn (loc, OPT_Wpedantic,
fcf73884 2521 "ISO C forbids subscripting %<register%> array");
3f75a254 2522 else if (!flag_isoc99 && !lvalue_p (foo))
c1771a20 2523 pedwarn (loc, OPT_Wpedantic,
fcf73884 2524 "ISO C90 forbids subscripting non-lvalue array");
400fbf9f
JW
2525 }
2526
46df2823 2527 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2528 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2529 /* Array ref is const/volatile if the array elements are
c22cacf3 2530 or if the array is. */
400fbf9f
JW
2531 TREE_READONLY (rval)
2532 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2533 | TREE_READONLY (array));
2534 TREE_SIDE_EFFECTS (rval)
2535 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2536 | TREE_SIDE_EFFECTS (array));
2537 TREE_THIS_VOLATILE (rval)
2538 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2539 /* This was added by rms on 16 Nov 91.
2f6e4e97 2540 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2541 in an inline function.
2542 Hope it doesn't break something else. */
2543 | TREE_THIS_VOLATILE (array));
928c19bb 2544 ret = require_complete_type (rval);
6a3799eb
AH
2545 protected_set_expr_location (ret, loc);
2546 return ret;
400fbf9f 2547 }
a4ab7973
JM
2548 else
2549 {
2550 tree ar = default_conversion (array);
400fbf9f 2551
a4ab7973
JM
2552 if (ar == error_mark_node)
2553 return ar;
400fbf9f 2554
a4ab7973
JM
2555 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2556 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2557
ba47d38d 2558 return build_indirect_ref
c9f9eb5d 2559 (loc, build_binary_op (loc, PLUS_EXPR, ar, index, 0),
dd865ef6 2560 RO_ARRAY_INDEXING);
a4ab7973 2561 }
400fbf9f
JW
2562}
2563\f
7e585d16 2564/* Build an external reference to identifier ID. FUN indicates
766beb40 2565 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2566 location of the identifier. This sets *TYPE to the type of the
2567 identifier, which is not the same as the type of the returned value
2568 for CONST_DECLs defined as enum constants. If the type of the
2569 identifier is not available, *TYPE is set to NULL. */
7e585d16 2570tree
c2255bc4 2571build_external_ref (location_t loc, tree id, int fun, tree *type)
7e585d16
ZW
2572{
2573 tree ref;
2574 tree decl = lookup_name (id);
16b34ad6
ZL
2575
2576 /* In Objective-C, an instance variable (ivar) may be preferred to
2577 whatever lookup_name() found. */
2578 decl = objc_lookup_ivar (decl, id);
7e585d16 2579
6866c6e8 2580 *type = NULL;
339a28b9 2581 if (decl && decl != error_mark_node)
6866c6e8
ILT
2582 {
2583 ref = decl;
2584 *type = TREE_TYPE (ref);
2585 }
339a28b9
ZW
2586 else if (fun)
2587 /* Implicit function declaration. */
c2255bc4 2588 ref = implicitly_declare (loc, id);
339a28b9
ZW
2589 else if (decl == error_mark_node)
2590 /* Don't complain about something that's already been
2591 complained about. */
2592 return error_mark_node;
2593 else
2594 {
c2255bc4 2595 undeclared_variable (loc, id);
339a28b9
ZW
2596 return error_mark_node;
2597 }
7e585d16
ZW
2598
2599 if (TREE_TYPE (ref) == error_mark_node)
2600 return error_mark_node;
2601
339a28b9 2602 if (TREE_DEPRECATED (ref))
9b86d6bb 2603 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2604
ad960f56 2605 /* Recursive call does not count as usage. */
b8698a0f 2606 if (ref != current_function_decl)
ad960f56 2607 {
ad960f56
MLI
2608 TREE_USED (ref) = 1;
2609 }
7e585d16 2610
bc4b653b
JM
2611 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2612 {
2613 if (!in_sizeof && !in_typeof)
2614 C_DECL_USED (ref) = 1;
2615 else if (DECL_INITIAL (ref) == 0
2616 && DECL_EXTERNAL (ref)
2617 && !TREE_PUBLIC (ref))
2618 record_maybe_used_decl (ref);
2619 }
2620
7e585d16
ZW
2621 if (TREE_CODE (ref) == CONST_DECL)
2622 {
6193b8b7 2623 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2624
2625 if (warn_cxx_compat
2626 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2627 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2628 {
2629 warning_at (loc, OPT_Wc___compat,
2630 ("enum constant defined in struct or union "
2631 "is not visible in C++"));
2632 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2633 }
2634
7e585d16
ZW
2635 ref = DECL_INITIAL (ref);
2636 TREE_CONSTANT (ref) = 1;
2637 }
6a29edea 2638 else if (current_function_decl != 0
4b1e44be 2639 && !DECL_FILE_SCOPE_P (current_function_decl)
6a29edea
EB
2640 && (TREE_CODE (ref) == VAR_DECL
2641 || TREE_CODE (ref) == PARM_DECL
2642 || TREE_CODE (ref) == FUNCTION_DECL))
2643 {
2644 tree context = decl_function_context (ref);
2f6e4e97 2645
6a29edea
EB
2646 if (context != 0 && context != current_function_decl)
2647 DECL_NONLOCAL (ref) = 1;
2648 }
71113fcd
GK
2649 /* C99 6.7.4p3: An inline definition of a function with external
2650 linkage ... shall not contain a reference to an identifier with
2651 internal linkage. */
2652 else if (current_function_decl != 0
2653 && DECL_DECLARED_INLINE_P (current_function_decl)
2654 && DECL_EXTERNAL (current_function_decl)
2655 && VAR_OR_FUNCTION_DECL_P (ref)
2656 && (TREE_CODE (ref) != VAR_DECL || TREE_STATIC (ref))
1033ffa8
JJ
2657 && ! TREE_PUBLIC (ref)
2658 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2659 record_inline_static (loc, current_function_decl, ref,
2660 csi_internal);
7e585d16
ZW
2661
2662 return ref;
2663}
2664
bc4b653b
JM
2665/* Record details of decls possibly used inside sizeof or typeof. */
2666struct maybe_used_decl
2667{
2668 /* The decl. */
2669 tree decl;
2670 /* The level seen at (in_sizeof + in_typeof). */
2671 int level;
2672 /* The next one at this level or above, or NULL. */
2673 struct maybe_used_decl *next;
2674};
2675
2676static struct maybe_used_decl *maybe_used_decls;
2677
2678/* Record that DECL, an undefined static function reference seen
2679 inside sizeof or typeof, might be used if the operand of sizeof is
2680 a VLA type or the operand of typeof is a variably modified
2681 type. */
2682
4e2fb7de 2683static void
bc4b653b
JM
2684record_maybe_used_decl (tree decl)
2685{
2686 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2687 t->decl = decl;
2688 t->level = in_sizeof + in_typeof;
2689 t->next = maybe_used_decls;
2690 maybe_used_decls = t;
2691}
2692
2693/* Pop the stack of decls possibly used inside sizeof or typeof. If
2694 USED is false, just discard them. If it is true, mark them used
2695 (if no longer inside sizeof or typeof) or move them to the next
2696 level up (if still inside sizeof or typeof). */
2697
2698void
2699pop_maybe_used (bool used)
2700{
2701 struct maybe_used_decl *p = maybe_used_decls;
2702 int cur_level = in_sizeof + in_typeof;
2703 while (p && p->level > cur_level)
2704 {
2705 if (used)
2706 {
2707 if (cur_level == 0)
2708 C_DECL_USED (p->decl) = 1;
2709 else
2710 p->level = cur_level;
2711 }
2712 p = p->next;
2713 }
2714 if (!used || cur_level == 0)
2715 maybe_used_decls = p;
2716}
2717
2718/* Return the result of sizeof applied to EXPR. */
2719
2720struct c_expr
c2255bc4 2721c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2722{
2723 struct c_expr ret;
ad97f4be
JM
2724 if (expr.value == error_mark_node)
2725 {
2726 ret.value = error_mark_node;
2727 ret.original_code = ERROR_MARK;
6866c6e8 2728 ret.original_type = NULL;
ad97f4be
JM
2729 pop_maybe_used (false);
2730 }
2731 else
2732 {
928c19bb
JM
2733 bool expr_const_operands = true;
2734 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2735 &expr_const_operands);
c2255bc4 2736 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7
JJ
2737 c_last_sizeof_arg = expr.value;
2738 ret.original_code = SIZEOF_EXPR;
6866c6e8 2739 ret.original_type = NULL;
928c19bb 2740 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2741 {
2742 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2743 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2744 folded_expr, ret.value);
2745 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2746 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2747 }
928c19bb 2748 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2749 }
bc4b653b
JM
2750 return ret;
2751}
2752
2753/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2754 name passed to sizeof (rather than the type itself). LOC is the
2755 location of the original expression. */
bc4b653b
JM
2756
2757struct c_expr
c2255bc4 2758c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2759{
2760 tree type;
2761 struct c_expr ret;
928c19bb
JM
2762 tree type_expr = NULL_TREE;
2763 bool type_expr_const = true;
2764 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2765 ret.value = c_sizeof (loc, type);
1a4049e7
JJ
2766 c_last_sizeof_arg = type;
2767 ret.original_code = SIZEOF_EXPR;
6866c6e8 2768 ret.original_type = NULL;
24070fcb
JM
2769 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2770 && c_vla_type_p (type))
928c19bb 2771 {
24070fcb
JM
2772 /* If the type is a [*] array, it is a VLA but is represented as
2773 having a size of zero. In such a case we must ensure that
2774 the result of sizeof does not get folded to a constant by
2775 c_fully_fold, because if the size is evaluated the result is
2776 not constant and so constraints on zero or negative size
2777 arrays must not be applied when this sizeof call is inside
2778 another array declarator. */
2779 if (!type_expr)
2780 type_expr = integer_zero_node;
928c19bb
JM
2781 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2782 type_expr, ret.value);
2783 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2784 }
16464cc1
VR
2785 pop_maybe_used (type != error_mark_node
2786 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2787 return ret;
2788}
2789
400fbf9f 2790/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2791 The function call is at LOC.
400fbf9f
JW
2792 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2793 TREE_VALUE of each node is a parameter-expression.
2794 FUNCTION's data type may be a function type or a pointer-to-function. */
2795
2796tree
c2255bc4 2797build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2798{
9771b263 2799 vec<tree, va_gc> *v;
bbbbb16a
ILT
2800 tree ret;
2801
9771b263 2802 vec_alloc (v, list_length (params));
bbbbb16a 2803 for (; params; params = TREE_CHAIN (params))
9771b263 2804 v->quick_push (TREE_VALUE (params));
8edbfaa6 2805 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2806 vec_free (v);
bbbbb16a
ILT
2807 return ret;
2808}
2809
ae52741c
MLI
2810/* Give a note about the location of the declaration of DECL. */
2811
2812static void inform_declaration (tree decl)
2813{
2814 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_BUILT_IN (decl)))
2815 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2816}
2817
bbbbb16a
ILT
2818/* Build a function call to function FUNCTION with parameters PARAMS.
2819 ORIGTYPES, if not NULL, is a vector of types; each element is
2820 either NULL or the original type of the corresponding element in
2821 PARAMS. The original type may differ from TREE_TYPE of the
2822 parameter for enums. FUNCTION's data type may be a function type
2823 or pointer-to-function. This function changes the elements of
2824 PARAMS. */
2825
2826tree
81e5eca8
MP
2827build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2828 tree function, vec<tree, va_gc> *params,
9771b263 2829 vec<tree, va_gc> *origtypes)
400fbf9f 2830{
b3694847 2831 tree fntype, fundecl = 0;
4977bab6 2832 tree name = NULL_TREE, result;
c96f4f73 2833 tree tem;
94a0dd7b
SL
2834 int nargs;
2835 tree *argarray;
b8698a0f 2836
400fbf9f 2837
fc76e425 2838 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 2839 STRIP_TYPE_NOPS (function);
400fbf9f
JW
2840
2841 /* Convert anything with function type to a pointer-to-function. */
2842 if (TREE_CODE (function) == FUNCTION_DECL)
2843 {
2844 name = DECL_NAME (function);
0a35513e
AH
2845
2846 if (flag_tm)
2847 tm_malloc_replacement (function);
a5eadacc 2848 fundecl = function;
86951993
AM
2849 /* Atomic functions have type checking/casting already done. They are
2850 often rewritten and don't match the original parameter list. */
2851 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
2852 origtypes = NULL;
36536d79 2853
b72271b9 2854 if (flag_cilkplus
36536d79
BI
2855 && is_cilkplus_reduce_builtin (function))
2856 origtypes = NULL;
400fbf9f 2857 }
f2a71bbc 2858 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 2859 function = function_to_pointer_conversion (loc, function);
400fbf9f 2860
6e955430
ZL
2861 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
2862 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
2863 if (params && !params->is_empty ())
2864 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 2865
928c19bb
JM
2866 function = c_fully_fold (function, false, NULL);
2867
400fbf9f
JW
2868 fntype = TREE_TYPE (function);
2869
2870 if (TREE_CODE (fntype) == ERROR_MARK)
2871 return error_mark_node;
2872
2873 if (!(TREE_CODE (fntype) == POINTER_TYPE
2874 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
2875 {
ae52741c
MLI
2876 if (!flag_diagnostics_show_caret)
2877 error_at (loc,
2878 "called object %qE is not a function or function pointer",
2879 function);
2880 else if (DECL_P (function))
2881 {
2882 error_at (loc,
2883 "called object %qD is not a function or function pointer",
2884 function);
2885 inform_declaration (function);
2886 }
2887 else
2888 error_at (loc,
2889 "called object is not a function or function pointer");
400fbf9f
JW
2890 return error_mark_node;
2891 }
2892
5ce89b2e
JM
2893 if (fundecl && TREE_THIS_VOLATILE (fundecl))
2894 current_function_returns_abnormally = 1;
2895
400fbf9f
JW
2896 /* fntype now gets the type of function pointed to. */
2897 fntype = TREE_TYPE (fntype);
2898
ab4194da
JM
2899 /* Convert the parameters to the types declared in the
2900 function prototype, or apply default promotions. */
2901
81e5eca8
MP
2902 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
2903 origtypes, function, fundecl);
ab4194da
JM
2904 if (nargs < 0)
2905 return error_mark_node;
2906
c96f4f73 2907 /* Check that the function is called through a compatible prototype.
fa337f3a 2908 If it is not, warn. */
1043771b 2909 if (CONVERT_EXPR_P (function)
c96f4f73
EB
2910 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
2911 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 2912 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
2913 {
2914 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
2915
2916 /* This situation leads to run-time undefined behavior. We can't,
2917 therefore, simply error unless we can prove that all possible
2918 executions of the program must execute the code. */
fa337f3a 2919 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 2920
fa337f3a
RB
2921 if (VOID_TYPE_P (return_type)
2922 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
2923 pedwarn (loc, 0,
2924 "function with qualified void return type called");
2925 }
c96f4f73 2926
9771b263 2927 argarray = vec_safe_address (params);
bbbbb16a 2928
83322951
RG
2929 /* Check that arguments to builtin functions match the expectations. */
2930 if (fundecl
2931 && DECL_BUILT_IN (fundecl)
2932 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
2933 && !check_builtin_function_arguments (fundecl, nargs, argarray))
2934 return error_mark_node;
400fbf9f 2935
83322951 2936 /* Check that the arguments to the function are valid. */
dde05067 2937 check_function_arguments (fntype, nargs, argarray);
400fbf9f 2938
928c19bb
JM
2939 if (name != NULL_TREE
2940 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 2941 {
928c19bb 2942 if (require_constant_value)
b8698a0f 2943 result =
db3927fb
AH
2944 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
2945 function, nargs, argarray);
928c19bb 2946 else
db3927fb
AH
2947 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
2948 function, nargs, argarray);
928c19bb
JM
2949 if (TREE_CODE (result) == NOP_EXPR
2950 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
2951 STRIP_TYPE_NOPS (result);
bf730f15
RS
2952 }
2953 else
db3927fb
AH
2954 result = build_call_array_loc (loc, TREE_TYPE (fntype),
2955 function, nargs, argarray);
b0b3afb2 2956
71653180 2957 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
2958 {
2959 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 2960 pedwarn (loc, 0,
3ce62965
JM
2961 "function with qualified void return type called");
2962 return result;
2963 }
1eb8759b 2964 return require_complete_type (result);
400fbf9f 2965}
8edbfaa6
JJ
2966
2967/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
2968
2969tree
2970c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
2971 tree function, vec<tree, va_gc> *params,
2972 vec<tree, va_gc> *origtypes)
2973{
2974 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
2975 STRIP_TYPE_NOPS (function);
2976
2977 /* Convert anything with function type to a pointer-to-function. */
2978 if (TREE_CODE (function) == FUNCTION_DECL)
2979 {
2980 /* Implement type-directed function overloading for builtins.
2981 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
2982 handle all the type checking. The result is a complete expression
2983 that implements this function call. */
2984 tree tem = resolve_overloaded_builtin (loc, function, params);
2985 if (tem)
2986 return tem;
2987 }
2988 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
2989}
400fbf9f 2990\f
bbbbb16a
ILT
2991/* Convert the argument expressions in the vector VALUES
2992 to the types in the list TYPELIST.
400fbf9f
JW
2993
2994 If TYPELIST is exhausted, or when an element has NULL as its type,
2995 perform the default conversions.
2996
bbbbb16a
ILT
2997 ORIGTYPES is the original types of the expressions in VALUES. This
2998 holds the type of enum values which have been converted to integral
2999 types. It may be NULL.
400fbf9f 3000
03dafa61
JM
3001 FUNCTION is a tree for the called function. It is used only for
3002 error messages, where it is formatted with %qE.
400fbf9f
JW
3003
3004 This is also where warnings about wrong number of args are generated.
3005
81e5eca8
MP
3006 ARG_LOC are locations of function arguments (if any).
3007
94a0dd7b 3008 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3009 than the length of VALUES in some error situations), or -1 on
3010 failure. */
94a0dd7b
SL
3011
3012static int
81e5eca8
MP
3013convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3014 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3015 tree function, tree fundecl)
400fbf9f 3016{
bbbbb16a
ILT
3017 tree typetail, val;
3018 unsigned int parmnum;
06302a02 3019 bool error_args = false;
b5d32c25 3020 const bool type_generic = fundecl
81e5eca8 3021 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3022 bool type_generic_remove_excess_precision = false;
03dafa61 3023 tree selector;
03dafa61 3024
2ac2f164
JM
3025 /* Change pointer to function to the function itself for
3026 diagnostics. */
03dafa61
JM
3027 if (TREE_CODE (function) == ADDR_EXPR
3028 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3029 function = TREE_OPERAND (function, 0);
03dafa61
JM
3030
3031 /* Handle an ObjC selector specially for diagnostics. */
3032 selector = objc_message_selector ();
400fbf9f 3033
8ce94e44
JM
3034 /* For type-generic built-in functions, determine whether excess
3035 precision should be removed (classification) or not
3036 (comparison). */
3037 if (type_generic
3038 && DECL_BUILT_IN (fundecl)
3039 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3040 {
3041 switch (DECL_FUNCTION_CODE (fundecl))
3042 {
3043 case BUILT_IN_ISFINITE:
3044 case BUILT_IN_ISINF:
3045 case BUILT_IN_ISINF_SIGN:
3046 case BUILT_IN_ISNAN:
3047 case BUILT_IN_ISNORMAL:
3048 case BUILT_IN_FPCLASSIFY:
3049 type_generic_remove_excess_precision = true;
3050 break;
3051
3052 default:
3053 type_generic_remove_excess_precision = false;
3054 break;
3055 }
3056 }
b72271b9 3057 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6d6efbb3 3058 return vec_safe_length (values);
8ce94e44 3059
400fbf9f 3060 /* Scan the given expressions and types, producing individual
bbbbb16a 3061 converted arguments. */
400fbf9f 3062
bbbbb16a 3063 for (typetail = typelist, parmnum = 0;
9771b263 3064 values && values->iterate (parmnum, &val);
bbbbb16a 3065 ++parmnum)
400fbf9f 3066 {
b3694847 3067 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3068 tree valtype = TREE_TYPE (val);
03dafa61
JM
3069 tree rname = function;
3070 int argnum = parmnum + 1;
4d3e6fae 3071 const char *invalid_func_diag;
8ce94e44 3072 bool excess_precision = false;
928c19bb 3073 bool npc;
bbbbb16a 3074 tree parmval;
5c1bc275
MP
3075 /* Some __atomic_* builtins have additional hidden argument at
3076 position 0. */
3077 location_t ploc
3078 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3079 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3080 : input_location;
400fbf9f
JW
3081
3082 if (type == void_type_node)
3083 {
19dc6d01 3084 if (selector)
68fca595 3085 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3086 else
68fca595 3087 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3088 inform_declaration (fundecl);
94a0dd7b 3089 return parmnum;
400fbf9f
JW
3090 }
3091
03dafa61
JM
3092 if (selector && argnum > 2)
3093 {
3094 rname = selector;
3095 argnum -= 2;
3096 }
3097
928c19bb 3098 npc = null_pointer_constant_p (val);
8ce94e44
JM
3099
3100 /* If there is excess precision and a prototype, convert once to
3101 the required type rather than converting via the semantic
3102 type. Likewise without a prototype a float value represented
3103 as long double should be converted once to double. But for
3104 type-generic classification functions excess precision must
3105 be removed here. */
3106 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3107 && (type || !type_generic || !type_generic_remove_excess_precision))
3108 {
3109 val = TREE_OPERAND (val, 0);
3110 excess_precision = true;
3111 }
928c19bb 3112 val = c_fully_fold (val, false, NULL);
ed248cf7 3113 STRIP_TYPE_NOPS (val);
400fbf9f 3114
400fbf9f
JW
3115 val = require_complete_type (val);
3116
3117 if (type != 0)
3118 {
3119 /* Formal parm type is specified by a function prototype. */
400fbf9f 3120
20913689 3121 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3122 {
5c1bc275
MP
3123 error_at (ploc, "type of formal parameter %d is incomplete",
3124 parmnum + 1);
400fbf9f
JW
3125 parmval = val;
3126 }
3127 else
3128 {
bbbbb16a
ILT
3129 tree origtype;
3130
d45cf215
RS
3131 /* Optionally warn about conversions that
3132 differ from the default conversions. */
05170031 3133 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3134 {
e3a64162 3135 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3136
aae43c5f 3137 if (INTEGRAL_TYPE_P (type)
8ce94e44 3138 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3139 warning_at (ploc, OPT_Wtraditional_conversion,
3140 "passing argument %d of %qE as integer rather "
3141 "than floating due to prototype",
3142 argnum, rname);
03829ad2 3143 if (INTEGRAL_TYPE_P (type)
8ce94e44 3144 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3145 warning_at (ploc, OPT_Wtraditional_conversion,
3146 "passing argument %d of %qE as integer rather "
3147 "than complex due to prototype",
3148 argnum, rname);
aae43c5f 3149 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3150 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3151 warning_at (ploc, OPT_Wtraditional_conversion,
3152 "passing argument %d of %qE as complex rather "
3153 "than floating due to prototype",
3154 argnum, rname);
400fbf9f 3155 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3156 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3157 warning_at (ploc, OPT_Wtraditional_conversion,
3158 "passing argument %d of %qE as floating rather "
3159 "than integer due to prototype",
3160 argnum, rname);
03829ad2 3161 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3162 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3163 warning_at (ploc, OPT_Wtraditional_conversion,
3164 "passing argument %d of %qE as complex rather "
3165 "than integer due to prototype",
3166 argnum, rname);
aae43c5f 3167 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3168 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3169 warning_at (ploc, OPT_Wtraditional_conversion,
3170 "passing argument %d of %qE as floating rather "
3171 "than complex due to prototype",
3172 argnum, rname);
aae43c5f
RK
3173 /* ??? At some point, messages should be written about
3174 conversions between complex types, but that's too messy
3175 to do now. */
d45cf215 3176 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3177 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3178 {
3179 /* Warn if any argument is passed as `float',
047de90b 3180 since without a prototype it would be `double'. */
9a8ce21f
JG
3181 if (formal_prec == TYPE_PRECISION (float_type_node)
3182 && type != dfloat32_type_node)
5c1bc275
MP
3183 warning_at (ploc, 0,
3184 "passing argument %d of %qE as %<float%> "
3185 "rather than %<double%> due to prototype",
3186 argnum, rname);
9a8ce21f
JG
3187
3188 /* Warn if mismatch between argument and prototype
3189 for decimal float types. Warn of conversions with
3190 binary float types and of precision narrowing due to
3191 prototype. */
8ce94e44 3192 else if (type != valtype
9a8ce21f
JG
3193 && (type == dfloat32_type_node
3194 || type == dfloat64_type_node
c22cacf3 3195 || type == dfloat128_type_node
8ce94e44
JM
3196 || valtype == dfloat32_type_node
3197 || valtype == dfloat64_type_node
3198 || valtype == dfloat128_type_node)
c22cacf3 3199 && (formal_prec
8ce94e44 3200 <= TYPE_PRECISION (valtype)
9a8ce21f 3201 || (type == dfloat128_type_node
8ce94e44 3202 && (valtype
c22cacf3 3203 != dfloat64_type_node
8ce94e44 3204 && (valtype
9a8ce21f
JG
3205 != dfloat32_type_node)))
3206 || (type == dfloat64_type_node
8ce94e44 3207 && (valtype
9a8ce21f 3208 != dfloat32_type_node))))
5c1bc275
MP
3209 warning_at (ploc, 0,
3210 "passing argument %d of %qE as %qT "
3211 "rather than %qT due to prototype",
3212 argnum, rname, type, valtype);
9a8ce21f 3213
d45cf215 3214 }
3ed56f8a
KG
3215 /* Detect integer changing in width or signedness.
3216 These warnings are only activated with
05170031
MLI
3217 -Wtraditional-conversion, not with -Wtraditional. */
3218 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
8ce94e44 3219 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3220 {
d45cf215
RS
3221 tree would_have_been = default_conversion (val);
3222 tree type1 = TREE_TYPE (would_have_been);
3223
754a4d82 3224 if (TREE_CODE (type) == ENUMERAL_TYPE
a38b987a 3225 && (TYPE_MAIN_VARIANT (type)
8ce94e44 3226 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3227 /* No warning if function asks for enum
3228 and the actual arg is that enum type. */
3229 ;
3230 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3231 warning_at (ploc, OPT_Wtraditional_conversion,
3232 "passing argument %d of %qE "
3233 "with different width due to prototype",
3234 argnum, rname);
8df83eae 3235 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3236 ;
800cd3b9
RS
3237 /* Don't complain if the formal parameter type
3238 is an enum, because we can't tell now whether
3239 the value was an enum--even the same enum. */
3240 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3241 ;
400fbf9f
JW
3242 else if (TREE_CODE (val) == INTEGER_CST
3243 && int_fits_type_p (val, type))
3244 /* Change in signedness doesn't matter
3245 if a constant value is unaffected. */
3246 ;
ce9895ae
RS
3247 /* If the value is extended from a narrower
3248 unsigned type, it doesn't matter whether we
3249 pass it as signed or unsigned; the value
3250 certainly is the same either way. */
8ce94e44
JM
3251 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3252 && TYPE_UNSIGNED (valtype))
ce9895ae 3253 ;
8df83eae 3254 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3255 warning_at (ploc, OPT_Wtraditional_conversion,
3256 "passing argument %d of %qE "
3257 "as unsigned due to prototype",
3258 argnum, rname);
3ed56f8a 3259 else
5c1bc275
MP
3260 warning_at (ploc, OPT_Wtraditional_conversion,
3261 "passing argument %d of %qE "
3262 "as signed due to prototype",
3263 argnum, rname);
400fbf9f
JW
3264 }
3265 }
3266
8ce94e44
JM
3267 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3268 sake of better warnings from convert_and_check. */
3269 if (excess_precision)
3270 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3271 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3272 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3273 val, origtype, ic_argpass,
3274 npc, fundecl, function,
2ac2f164 3275 parmnum + 1);
2f6e4e97 3276
61f71b34 3277 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3278 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3279 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3280 parmval = default_conversion (parmval);
400fbf9f 3281 }
400fbf9f 3282 }
8ce94e44
JM
3283 else if (TREE_CODE (valtype) == REAL_TYPE
3284 && (TYPE_PRECISION (valtype)
2531a1d9 3285 <= TYPE_PRECISION (double_type_node))
0fdd4508
JR
3286 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3287 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
8ce94e44 3288 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
b5d32c25
KG
3289 {
3290 if (type_generic)
bbbbb16a 3291 parmval = val;
b5d32c25 3292 else
0a0b3574
MM
3293 {
3294 /* Convert `float' to `double'. */
3295 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3296 warning_at (ploc, OPT_Wdouble_promotion,
3297 "implicit conversion from %qT to %qT when passing "
3298 "argument to function",
3299 valtype, double_type_node);
0a0b3574
MM
3300 parmval = convert (double_type_node, val);
3301 }
b5d32c25 3302 }
8ce94e44
JM
3303 else if (excess_precision && !type_generic)
3304 /* A "double" argument with excess precision being passed
3305 without a prototype or in variable arguments. */
bbbbb16a 3306 parmval = convert (valtype, val);
c22cacf3
MS
3307 else if ((invalid_func_diag =
3308 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3309 {
3310 error (invalid_func_diag);
94a0dd7b 3311 return -1;
4d3e6fae 3312 }
400fbf9f
JW
3313 else
3314 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3315 parmval = default_conversion (val);
3316
9771b263 3317 (*values)[parmnum] = parmval;
06302a02
JJ
3318 if (parmval == error_mark_node)
3319 error_args = true;
400fbf9f
JW
3320
3321 if (typetail)
3322 typetail = TREE_CHAIN (typetail);
3323 }
3324
9771b263 3325 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3326
400fbf9f 3327 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
3789b316 3328 {
68fca595 3329 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3330 inform_declaration (fundecl);
3331 return -1;
3789b316 3332 }
400fbf9f 3333
06302a02 3334 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3335}
3336\f
43f6dfd3
RS
3337/* This is the entry point used by the parser to build unary operators
3338 in the input. CODE, a tree_code, specifies the unary operator, and
3339 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3340 CONVERT_EXPR for code.
3341
3342 LOC is the location to use for the tree generated.
3343*/
43f6dfd3
RS
3344
3345struct c_expr
c2255bc4 3346parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3347{
3348 struct c_expr result;
3349
c9f9eb5d 3350 result.value = build_unary_op (loc, code, arg.value, 0);
100d537d 3351 result.original_code = code;
6866c6e8
ILT
3352 result.original_type = NULL;
3353
59c0753d 3354 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
c2255bc4 3355 overflow_warning (loc, result.value);
59c0753d 3356
43f6dfd3
RS
3357 return result;
3358}
3359
3360/* This is the entry point used by the parser to build binary operators
3361 in the input. CODE, a tree_code, specifies the binary operator, and
3362 ARG1 and ARG2 are the operands. In addition to constructing the
3363 expression, we check for operands that were written with other binary
ba47d38d
AH
3364 operators in a way that is likely to confuse the user.
3365
3366 LOCATION is the location of the binary operator. */
edc7c4ec 3367
487a92fe 3368struct c_expr
ba47d38d
AH
3369parser_build_binary_op (location_t location, enum tree_code code,
3370 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3371{
487a92fe 3372 struct c_expr result;
400fbf9f 3373
487a92fe
JM
3374 enum tree_code code1 = arg1.original_code;
3375 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3376 tree type1 = (arg1.original_type
3377 ? arg1.original_type
3378 : TREE_TYPE (arg1.value));
3379 tree type2 = (arg2.original_type
3380 ? arg2.original_type
3381 : TREE_TYPE (arg2.value));
400fbf9f 3382
ba47d38d
AH
3383 result.value = build_binary_op (location, code,
3384 arg1.value, arg2.value, 1);
487a92fe 3385 result.original_code = code;
6866c6e8 3386 result.original_type = NULL;
58bf601b 3387
487a92fe
JM
3388 if (TREE_CODE (result.value) == ERROR_MARK)
3389 return result;
400fbf9f 3390
ba47d38d
AH
3391 if (location != UNKNOWN_LOCATION)
3392 protected_set_expr_location (result.value, location);
3393
400fbf9f 3394 /* Check for cases such as x+y<<z which users are likely
487a92fe 3395 to misinterpret. */
400fbf9f 3396 if (warn_parentheses)
393e8e8b
MP
3397 warn_about_parentheses (location, code, code1, arg1.value, code2,
3398 arg2.value);
001af587 3399
ca409efd 3400 if (warn_logical_op)
393e8e8b 3401 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3402 code1, arg1.value, code2, arg2.value);
63a08740 3403
742938c9
MP
3404 if (warn_logical_not_paren
3405 && code1 == TRUTH_NOT_EXPR)
3406 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
3407
e994a705
RS
3408 /* Warn about comparisons against string literals, with the exception
3409 of testing for equality or inequality of a string literal with NULL. */
3410 if (code == EQ_EXPR || code == NE_EXPR)
3411 {
3412 if ((code1 == STRING_CST && !integer_zerop (arg2.value))
3413 || (code2 == STRING_CST && !integer_zerop (arg1.value)))
c2255bc4
AH
3414 warning_at (location, OPT_Waddress,
3415 "comparison with string literal results in unspecified behavior");
e994a705
RS
3416 }
3417 else if (TREE_CODE_CLASS (code) == tcc_comparison
3418 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3419 warning_at (location, OPT_Waddress,
3420 "comparison with string literal results in unspecified behavior");
e994a705 3421
b8698a0f
L
3422 if (TREE_OVERFLOW_P (result.value)
3423 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3424 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3425 overflow_warning (location, result.value);
400fbf9f 3426
6866c6e8
ILT
3427 /* Warn about comparisons of different enum types. */
3428 if (warn_enum_compare
3429 && TREE_CODE_CLASS (code) == tcc_comparison
3430 && TREE_CODE (type1) == ENUMERAL_TYPE
3431 && TREE_CODE (type2) == ENUMERAL_TYPE
3432 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3433 warning_at (location, OPT_Wenum_compare,
3434 "comparison between %qT and %qT",
3435 type1, type2);
3436
400fbf9f
JW
3437 return result;
3438}
3e4093b6 3439\f
3e4093b6
RS
3440/* Return a tree for the difference of pointers OP0 and OP1.
3441 The resulting tree has type int. */
293c9fdd 3442
3e4093b6 3443static tree
db3927fb 3444pointer_diff (location_t loc, tree op0, tree op1)
3e4093b6 3445{
3e4093b6 3446 tree restype = ptrdiff_type_node;
36c5e70a 3447 tree result, inttype;
400fbf9f 3448
36c5e70a
BE
3449 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3450 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6
RS
3451 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3452 tree con0, con1, lit0, lit1;
3453 tree orig_op1 = op1;
400fbf9f 3454
36c5e70a
BE
3455 /* If the operands point into different address spaces, we need to
3456 explicitly convert them to pointers into the common address space
3457 before we can subtract the numerical address values. */
3458 if (as0 != as1)
3459 {
3460 addr_space_t as_common;
3461 tree common_type;
3462
3463 /* Determine the common superset address space. This is guaranteed
3464 to exist because the caller verified that comp_target_types
3465 returned non-zero. */
3466 if (!addr_space_superset (as0, as1, &as_common))
3467 gcc_unreachable ();
3468
3469 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3470 op0 = convert (common_type, op0);
3471 op1 = convert (common_type, op1);
3472 }
3473
3474 /* Determine integer type to perform computations in. This will usually
3475 be the same as the result type (ptrdiff_t), but may need to be a wider
3476 type if pointers for the address space are wider than ptrdiff_t. */
3477 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3478 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3479 else
3480 inttype = restype;
3481
3482
fcf73884 3483 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3484 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3485 "pointer of type %<void *%> used in subtraction");
3486 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3487 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3488 "pointer to a function used in subtraction");
400fbf9f 3489
3e4093b6
RS
3490 /* If the conversion to ptrdiff_type does anything like widening or
3491 converting a partial to an integral mode, we get a convert_expression
3492 that is in the way to do any simplifications.
3493 (fold-const.c doesn't know that the extra bits won't be needed.
3494 split_tree uses STRIP_SIGN_NOPS, which leaves conversions to a
3495 different mode in place.)
3496 So first try to find a common term here 'by hand'; we want to cover
3497 at least the cases that occur in legal static initializers. */
1043771b 3498 if (CONVERT_EXPR_P (op0)
1344f9a3
JM
3499 && (TYPE_PRECISION (TREE_TYPE (op0))
3500 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
3501 con0 = TREE_OPERAND (op0, 0);
3502 else
3503 con0 = op0;
1043771b 3504 if (CONVERT_EXPR_P (op1)
1344f9a3
JM
3505 && (TYPE_PRECISION (TREE_TYPE (op1))
3506 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op1, 0)))))
3507 con1 = TREE_OPERAND (op1, 0);
3508 else
3509 con1 = op1;
400fbf9f 3510
5de72424 3511 if (TREE_CODE (con0) == POINTER_PLUS_EXPR)
3e4093b6
RS
3512 {
3513 lit0 = TREE_OPERAND (con0, 1);
3514 con0 = TREE_OPERAND (con0, 0);
3515 }
3516 else
3517 lit0 = integer_zero_node;
400fbf9f 3518
5de72424 3519 if (TREE_CODE (con1) == POINTER_PLUS_EXPR)
400fbf9f 3520 {
3e4093b6
RS
3521 lit1 = TREE_OPERAND (con1, 1);
3522 con1 = TREE_OPERAND (con1, 0);
400fbf9f
JW
3523 }
3524 else
3e4093b6
RS
3525 lit1 = integer_zero_node;
3526
3527 if (operand_equal_p (con0, con1, 0))
400fbf9f 3528 {
3e4093b6
RS
3529 op0 = lit0;
3530 op1 = lit1;
400fbf9f
JW
3531 }
3532
400fbf9f 3533
3e4093b6
RS
3534 /* First do the subtraction as integers;
3535 then drop through to build the divide operator.
3536 Do not do default conversions on the minus operator
3537 in case restype is a short type. */
400fbf9f 3538
db3927fb 3539 op0 = build_binary_op (loc,
36c5e70a
BE
3540 MINUS_EXPR, convert (inttype, op0),
3541 convert (inttype, op1), 0);
3e4093b6
RS
3542 /* This generates an error if op1 is pointer to incomplete type. */
3543 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3544 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3545
3e4093b6 3546 op1 = c_size_in_bytes (target_type);
400fbf9f 3547
f04dda30
MP
3548 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3549 error_at (loc, "arithmetic on pointer to an empty aggregate");
3550
3e4093b6 3551 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3552 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3553 op0, convert (inttype, op1));
3554
3555 /* Convert to final result type if necessary. */
3556 return convert (restype, result);
3e4093b6
RS
3557}
3558\f
267bac10
JM
3559/* Expand atomic compound assignments into an approriate sequence as
3560 specified by the C11 standard section 6.5.16.2.
3561 given
3562 _Atomic T1 E1
3563 T2 E2
3564 E1 op= E2
3565
3566 This sequence is used for all types for which these operations are
3567 supported.
3568
3569 In addition, built-in versions of the 'fe' prefixed routines may
3570 need to be invoked for floating point (real, complex or vector) when
3571 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3572
3573 T1 newval;
3574 T1 old;
3575 T1 *addr
3576 T2 val
3577 fenv_t fenv
3578
3579 addr = &E1;
3580 val = (E2);
3581 __atomic_load (addr, &old, SEQ_CST);
3582 feholdexcept (&fenv);
3583loop:
3584 newval = old op val;
3585 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3586 SEQ_CST))
3587 goto done;
3588 feclearexcept (FE_ALL_EXCEPT);
3589 goto loop:
3590done:
3591 feupdateenv (&fenv);
3592
3593 Also note that the compiler is simply issuing the generic form of
3594 the atomic operations. This requires temp(s) and has their address
3595 taken. The atomic processing is smart enough to figure out when the
3596 size of an object can utilize a lock-free version, and convert the
3597 built-in call to the appropriate lock-free routine. The optimizers
3598 will then dispose of any temps that are no longer required, and
3599 lock-free implementations are utilized as long as there is target
3600 support for the required size.
3601
3602 If the operator is NOP_EXPR, then this is a simple assignment, and
3603 an __atomic_store is issued to perform the assignment rather than
3604 the above loop.
3605
3606*/
3607
3608/* Build an atomic assignment at LOC, expanding into the proper
3609 sequence to store LHS MODIFYCODE= RHS. Return a value representing
3610 the result of the operation, unless RETURN_OLD_P in which case
3611 return the old value of LHS (this is only for postincrement and
3612 postdecrement). */
3613static tree
3614build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3615 tree rhs, bool return_old_p)
3616{
3617 tree fndecl, func_call;
3618 vec<tree, va_gc> *params;
3619 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3620 tree old, old_addr;
3621 tree compound_stmt;
3622 tree stmt, goto_stmt;
3623 tree loop_label, loop_decl, done_label, done_decl;
3624
3625 tree lhs_type = TREE_TYPE (lhs);
3626 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, 0);
3627 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3628 tree rhs_type = TREE_TYPE (rhs);
3629
3630 gcc_assert (TYPE_ATOMIC (lhs_type));
3631
3632 if (return_old_p)
3633 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3634
3635 /* Allocate enough vector items for a compare_exchange. */
3636 vec_alloc (params, 6);
3637
3638 /* Create a compound statement to hold the sequence of statements
3639 with a loop. */
3640 compound_stmt = c_begin_compound_stmt (false);
3641
3642 /* Fold the RHS if it hasn't already been folded. */
3643 if (modifycode != NOP_EXPR)
3644 rhs = c_fully_fold (rhs, false, NULL);
3645
3646 /* Remove the qualifiers for the rest of the expressions and create
3647 the VAL temp variable to hold the RHS. */
3648 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3649 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
3650 val = create_tmp_var (nonatomic_rhs_type, NULL);
3651 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3652 TREE_NO_WARNING (val) = 1;
267bac10
JM
3653 rhs = build2 (MODIFY_EXPR, nonatomic_rhs_type, val, rhs);
3654 SET_EXPR_LOCATION (rhs, loc);
3655 add_stmt (rhs);
3656
3657 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3658 an atomic_store. */
3659 if (modifycode == NOP_EXPR)
3660 {
3661 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
3662 rhs = build_unary_op (loc, ADDR_EXPR, val, 0);
3663 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3664 params->quick_push (lhs_addr);
3665 params->quick_push (rhs);
3666 params->quick_push (seq_cst);
8edbfaa6 3667 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3668 add_stmt (func_call);
3669
3670 /* Finish the compound statement. */
3671 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3672
3673 /* VAL is the value which was stored, return a COMPOUND_STMT of
3674 the statement and that value. */
3675 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3676 }
3677
3678 /* Create the variables and labels required for the op= form. */
3679 old = create_tmp_var (nonatomic_lhs_type, NULL);
3680 old_addr = build_unary_op (loc, ADDR_EXPR, old, 0);
cc28fc7f
MP
3681 TREE_ADDRESSABLE (old) = 1;
3682 TREE_NO_WARNING (old) = 1;
267bac10
JM
3683
3684 newval = create_tmp_var (nonatomic_lhs_type, NULL);
3685 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, 0);
3686 TREE_ADDRESSABLE (newval) = 1;
3687
3688 loop_decl = create_artificial_label (loc);
3689 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
3690
3691 done_decl = create_artificial_label (loc);
3692 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
3693
3694 /* __atomic_load (addr, &old, SEQ_CST). */
3695 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
3696 params->quick_push (lhs_addr);
3697 params->quick_push (old_addr);
3698 params->quick_push (seq_cst);
8edbfaa6 3699 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3700 add_stmt (func_call);
3701 params->truncate (0);
3702
3703 /* Create the expressions for floating-point environment
3704 manipulation, if required. */
3705 bool need_fenv = (flag_trapping_math
3706 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
3707 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
3708 if (need_fenv)
3709 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
3710
3711 if (hold_call)
3712 add_stmt (hold_call);
3713
3714 /* loop: */
3715 add_stmt (loop_label);
3716
3717 /* newval = old + val; */
3718 rhs = build_binary_op (loc, modifycode, old, val, 1);
68fca595
MP
3719 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
3720 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
3721 NULL_TREE, 0);
3722 if (rhs != error_mark_node)
3723 {
3724 rhs = build2 (MODIFY_EXPR, nonatomic_lhs_type, newval, rhs);
3725 SET_EXPR_LOCATION (rhs, loc);
3726 add_stmt (rhs);
3727 }
3728
3729 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
3730 goto done; */
3731 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
3732 params->quick_push (lhs_addr);
3733 params->quick_push (old_addr);
3734 params->quick_push (newval_addr);
3735 params->quick_push (integer_zero_node);
3736 params->quick_push (seq_cst);
3737 params->quick_push (seq_cst);
8edbfaa6 3738 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
3739
3740 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
3741 SET_EXPR_LOCATION (goto_stmt, loc);
3742
3743 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
3744 SET_EXPR_LOCATION (stmt, loc);
3745 add_stmt (stmt);
3746
3747 if (clear_call)
3748 add_stmt (clear_call);
3749
3750 /* goto loop; */
3751 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
3752 SET_EXPR_LOCATION (goto_stmt, loc);
3753 add_stmt (goto_stmt);
3754
3755 /* done: */
3756 add_stmt (done_label);
3757
3758 if (update_call)
3759 add_stmt (update_call);
3760
3761 /* Finish the compound statement. */
3762 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3763
3764 /* NEWVAL is the value that was successfully stored, return a
3765 COMPOUND_EXPR of the statement and the appropriate value. */
3766 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
3767 return_old_p ? old : newval);
3768}
3769
3e4093b6
RS
3770/* Construct and perhaps optimize a tree representation
3771 for a unary operation. CODE, a tree_code, specifies the operation
3772 and XARG is the operand.
3773 For any CODE other than ADDR_EXPR, FLAG nonzero suppresses
3774 the default promotions (such as from short to int).
3775 For ADDR_EXPR, the default promotions are not applied; FLAG nonzero
3776 allows non-lvalues; this is only used to handle conversion of non-lvalue
c9f9eb5d
AH
3777 arrays to pointers in C99.
3778
3779 LOCATION is the location of the operator. */
400fbf9f 3780
3e4093b6 3781tree
c9f9eb5d
AH
3782build_unary_op (location_t location,
3783 enum tree_code code, tree xarg, int flag)
3e4093b6
RS
3784{
3785 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
3786 tree arg = xarg;
3787 tree argtype = 0;
808d6eaa 3788 enum tree_code typecode;
3e4093b6 3789 tree val;
c9f9eb5d 3790 tree ret = error_mark_node;
8ce94e44 3791 tree eptype = NULL_TREE;
3e4093b6 3792 int noconvert = flag;
4de67c26 3793 const char *invalid_op_diag;
928c19bb
JM
3794 bool int_operands;
3795
3796 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
3797 if (int_operands)
3798 arg = remove_c_maybe_const_expr (arg);
400fbf9f 3799
808d6eaa
JM
3800 if (code != ADDR_EXPR)
3801 arg = require_complete_type (arg);
3802
3803 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
3804 if (typecode == ERROR_MARK)
3805 return error_mark_node;
3806 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
3807 typecode = INTEGER_TYPE;
6c36d76b 3808
4de67c26
JM
3809 if ((invalid_op_diag
3810 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
3811 {
c9f9eb5d 3812 error_at (location, invalid_op_diag);
4de67c26
JM
3813 return error_mark_node;
3814 }
3815
8ce94e44
JM
3816 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
3817 {
3818 eptype = TREE_TYPE (arg);
3819 arg = TREE_OPERAND (arg, 0);
3820 }
3821
3e4093b6
RS
3822 switch (code)
3823 {
3824 case CONVERT_EXPR:
3825 /* This is used for unary plus, because a CONVERT_EXPR
3826 is enough to prevent anybody from looking inside for
3827 associativity, but won't generate any code. */
3828 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3829 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 3830 || typecode == VECTOR_TYPE))
400fbf9f 3831 {
c9f9eb5d 3832 error_at (location, "wrong type argument to unary plus");
3e4093b6 3833 return error_mark_node;
400fbf9f 3834 }
3e4093b6
RS
3835 else if (!noconvert)
3836 arg = default_conversion (arg);
db3927fb 3837 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
3838 break;
3839
3e4093b6
RS
3840 case NEGATE_EXPR:
3841 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 3842 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
3843 || typecode == VECTOR_TYPE))
3844 {
c9f9eb5d 3845 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
3846 return error_mark_node;
3847 }
3848 else if (!noconvert)
3849 arg = default_conversion (arg);
400fbf9f
JW
3850 break;
3851
3e4093b6 3852 case BIT_NOT_EXPR:
462643f0
AP
3853 /* ~ works on integer types and non float vectors. */
3854 if (typecode == INTEGER_TYPE
3855 || (typecode == VECTOR_TYPE
3856 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 3857 {
3e4093b6
RS
3858 if (!noconvert)
3859 arg = default_conversion (arg);
03d5b1f5 3860 }
3e4093b6 3861 else if (typecode == COMPLEX_TYPE)
400fbf9f 3862 {
3e4093b6 3863 code = CONJ_EXPR;
c1771a20 3864 pedwarn (location, OPT_Wpedantic,
fcf73884 3865 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
3866 if (!noconvert)
3867 arg = default_conversion (arg);
3868 }
3869 else
3870 {
c9f9eb5d 3871 error_at (location, "wrong type argument to bit-complement");
3e4093b6 3872 return error_mark_node;
400fbf9f
JW
3873 }
3874 break;
3875
3e4093b6 3876 case ABS_EXPR:
11017cc7 3877 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 3878 {
c9f9eb5d 3879 error_at (location, "wrong type argument to abs");
3e4093b6 3880 return error_mark_node;
400fbf9f 3881 }
3e4093b6
RS
3882 else if (!noconvert)
3883 arg = default_conversion (arg);
400fbf9f
JW
3884 break;
3885
3e4093b6
RS
3886 case CONJ_EXPR:
3887 /* Conjugating a real value is a no-op, but allow it anyway. */
3888 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
3889 || typecode == COMPLEX_TYPE))
400fbf9f 3890 {
c9f9eb5d 3891 error_at (location, "wrong type argument to conjugation");
3e4093b6 3892 return error_mark_node;
400fbf9f 3893 }
3e4093b6
RS
3894 else if (!noconvert)
3895 arg = default_conversion (arg);
400fbf9f
JW
3896 break;
3897
3e4093b6 3898 case TRUTH_NOT_EXPR:
ab22c1fa 3899 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 3900 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 3901 && typecode != COMPLEX_TYPE)
400fbf9f 3902 {
c9f9eb5d
AH
3903 error_at (location,
3904 "wrong type argument to unary exclamation mark");
3e4093b6 3905 return error_mark_node;
400fbf9f 3906 }
a27d595d
JM
3907 if (int_operands)
3908 {
3909 arg = c_objc_common_truthvalue_conversion (location, xarg);
3910 arg = remove_c_maybe_const_expr (arg);
3911 }
3912 else
3913 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 3914 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
3915 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
3916 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
3917 location = EXPR_LOCATION (ret);
c9f9eb5d 3918 goto return_build_unary_op;
3e4093b6 3919
3e4093b6 3920 case REALPART_EXPR:
3e4093b6 3921 case IMAGPART_EXPR:
fb52b50a
NF
3922 ret = build_real_imag_expr (location, code, arg);
3923 if (ret == error_mark_node)
3924 return error_mark_node;
8ce94e44
JM
3925 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
3926 eptype = TREE_TYPE (eptype);
c9f9eb5d 3927 goto return_build_unary_op;
3e4093b6
RS
3928
3929 case PREINCREMENT_EXPR:
3930 case POSTINCREMENT_EXPR:
3931 case PREDECREMENT_EXPR:
3932 case POSTDECREMENT_EXPR:
3e4093b6 3933
928c19bb
JM
3934 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
3935 {
3936 tree inner = build_unary_op (location, code,
3937 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
3938 if (inner == error_mark_node)
3939 return error_mark_node;
3940 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
3941 C_MAYBE_CONST_EXPR_PRE (arg), inner);
3942 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
3943 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
3944 goto return_build_unary_op;
3945 }
3946
925e8657
NP
3947 /* Complain about anything that is not a true lvalue. In
3948 Objective-C, skip this check for property_refs. */
f90e8e2e 3949 if (!objc_is_property_ref (arg)
7bd11157
TT
3950 && !lvalue_or_else (location,
3951 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
3952 || code == POSTINCREMENT_EXPR)
3953 ? lv_increment
3954 : lv_decrement)))
928c19bb
JM
3955 return error_mark_node;
3956
09639a83
ILT
3957 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
3958 {
3959 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
3960 warning_at (location, OPT_Wc___compat,
3961 "increment of enumeration value is invalid in C++");
3962 else
3963 warning_at (location, OPT_Wc___compat,
3964 "decrement of enumeration value is invalid in C++");
3965 }
3966
928c19bb
JM
3967 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
3968 arg = c_fully_fold (arg, false, NULL);
3969
267bac10
JM
3970 bool atomic_op;
3971 atomic_op = really_atomic_lvalue (arg);
3972
3e4093b6
RS
3973 /* Increment or decrement the real part of the value,
3974 and don't change the imaginary part. */
3975 if (typecode == COMPLEX_TYPE)
400fbf9f 3976 {
3e4093b6
RS
3977 tree real, imag;
3978
c1771a20 3979 pedwarn (location, OPT_Wpedantic,
509c9d60 3980 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 3981
267bac10
JM
3982 if (!atomic_op)
3983 {
3984 arg = stabilize_reference (arg);
3985 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg, 1);
3986 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg, 1);
3987 real = build_unary_op (EXPR_LOCATION (arg), code, real, 1);
3988 if (real == error_mark_node || imag == error_mark_node)
3989 return error_mark_node;
3990 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
3991 real, imag);
3992 goto return_build_unary_op;
3993 }
400fbf9f 3994 }
3e4093b6
RS
3995
3996 /* Report invalid types. */
3997
ab22c1fa 3998 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 3999 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4000 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4001 {
3e4093b6 4002 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4003 error_at (location, "wrong type argument to increment");
c22cacf3 4004 else
c9f9eb5d 4005 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4006
4007 return error_mark_node;
400fbf9f 4008 }
400fbf9f 4009
3e4093b6
RS
4010 {
4011 tree inc;
400fbf9f 4012
3e4093b6
RS
4013 argtype = TREE_TYPE (arg);
4014
4015 /* Compute the increment. */
4016
4017 if (typecode == POINTER_TYPE)
4018 {
a0e24419 4019 /* If pointer target is an incomplete type,
3e4093b6 4020 we just cannot know how to do the arithmetic. */
b70cef5d 4021 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4022 {
4023 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4024 error_at (location,
a0e24419
MP
4025 "increment of pointer to an incomplete type %qT",
4026 TREE_TYPE (argtype));
3e4093b6 4027 else
c9f9eb5d 4028 error_at (location,
a0e24419
MP
4029 "decrement of pointer to an incomplete type %qT",
4030 TREE_TYPE (argtype));
3e4093b6 4031 }
b70cef5d
JJ
4032 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4033 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4034 {
3e4093b6 4035 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4036 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4037 "wrong type argument to increment");
3e4093b6 4038 else
44d90fe1 4039 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4040 "wrong type argument to decrement");
3e4093b6
RS
4041 }
4042
b70cef5d 4043 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4044 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4045 }
b70cef5d 4046 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4047 {
4048 /* For signed fract types, we invert ++ to -- or
4049 -- to ++, and change inc from 1 to -1, because
4050 it is not possible to represent 1 in signed fract constants.
4051 For unsigned fract types, the result always overflows and
4052 we get an undefined (original) or the maximum value. */
4053 if (code == PREINCREMENT_EXPR)
4054 code = PREDECREMENT_EXPR;
4055 else if (code == PREDECREMENT_EXPR)
4056 code = PREINCREMENT_EXPR;
4057 else if (code == POSTINCREMENT_EXPR)
4058 code = POSTDECREMENT_EXPR;
4059 else /* code == POSTDECREMENT_EXPR */
4060 code = POSTINCREMENT_EXPR;
4061
4062 inc = integer_minus_one_node;
4063 inc = convert (argtype, inc);
4064 }
3e4093b6 4065 else
5be014d5 4066 {
241b71bb
TV
4067 inc = VECTOR_TYPE_P (argtype)
4068 ? build_one_cst (argtype)
4069 : integer_one_node;
5be014d5
AP
4070 inc = convert (argtype, inc);
4071 }
3e4093b6 4072
925e8657
NP
4073 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4074 need to ask Objective-C to build the increment or decrement
4075 expression for it. */
4076 if (objc_is_property_ref (arg))
f90e8e2e 4077 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4078 arg, inc);
4079
3e4093b6 4080 /* Report a read-only lvalue. */
f37acdf9 4081 if (TYPE_READONLY (argtype))
953ff289 4082 {
c02065fc 4083 readonly_error (location, arg,
953ff289
DN
4084 ((code == PREINCREMENT_EXPR
4085 || code == POSTINCREMENT_EXPR)
4086 ? lv_increment : lv_decrement));
4087 return error_mark_node;
4088 }
f37acdf9
JM
4089 else if (TREE_READONLY (arg))
4090 readonly_warning (arg,
4091 ((code == PREINCREMENT_EXPR
4092 || code == POSTINCREMENT_EXPR)
4093 ? lv_increment : lv_decrement));
3e4093b6 4094
267bac10
JM
4095 /* If the argument is atomic, use the special code sequences for
4096 atomic compound assignment. */
4097 if (atomic_op)
4098 {
4099 arg = stabilize_reference (arg);
4100 ret = build_atomic_assign (location, arg,
4101 ((code == PREINCREMENT_EXPR
4102 || code == POSTINCREMENT_EXPR)
4103 ? PLUS_EXPR
4104 : MINUS_EXPR),
4105 (FRACT_MODE_P (TYPE_MODE (argtype))
4106 ? inc
4107 : integer_one_node),
4108 (code == POSTINCREMENT_EXPR
4109 || code == POSTDECREMENT_EXPR));
4110 goto return_build_unary_op;
4111 }
4112
3e4093b6
RS
4113 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4114 val = boolean_increment (code, arg);
4115 else
53fb4de3 4116 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4117 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4118 if (TREE_CODE (val) != code)
6de9cd9a 4119 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4120 ret = val;
4121 goto return_build_unary_op;
3e4093b6
RS
4122 }
4123
4124 case ADDR_EXPR:
4125 /* Note that this operation never does default_conversion. */
4126
2b4b7036
JM
4127 /* The operand of unary '&' must be an lvalue (which excludes
4128 expressions of type void), or, in C99, the result of a [] or
4129 unary '*' operator. */
4130 if (VOID_TYPE_P (TREE_TYPE (arg))
4131 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
4132 && (TREE_CODE (arg) != INDIRECT_REF
4133 || !flag_isoc99))
4134 pedwarn (location, 0, "taking address of expression of type %<void%>");
4135
3e4093b6
RS
4136 /* Let &* cancel out to simplify resulting code. */
4137 if (TREE_CODE (arg) == INDIRECT_REF)
400fbf9f 4138 {
3e4093b6
RS
4139 /* Don't let this be an lvalue. */
4140 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4141 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4142 ret = TREE_OPERAND (arg, 0);
4143 goto return_build_unary_op;
400fbf9f 4144 }
1eb8759b 4145
7c672dfc 4146 /* For &x[y], return x+y */
3e4093b6 4147 if (TREE_CODE (arg) == ARRAY_REF)
1eb8759b 4148 {
f2a71bbc
JM
4149 tree op0 = TREE_OPERAND (arg, 0);
4150 if (!c_mark_addressable (op0))
3e4093b6 4151 return error_mark_node;
1eb8759b 4152 }
1eb8759b 4153
3e4093b6
RS
4154 /* Anything not already handled and not a true memory reference
4155 or a non-lvalue array is an error. */
4156 else if (typecode != FUNCTION_TYPE && !flag
7bd11157 4157 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4158 return error_mark_node;
b6a10c9f 4159
928c19bb
JM
4160 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4161 folding later. */
4162 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4163 {
4164 tree inner = build_unary_op (location, code,
4165 C_MAYBE_CONST_EXPR_EXPR (arg), flag);
4166 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4167 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4168 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4169 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4170 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4171 goto return_build_unary_op;
4172 }
4173
3e4093b6
RS
4174 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4175 argtype = TREE_TYPE (arg);
400fbf9f 4176
3e4093b6 4177 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4178 to which the address will point. This is only needed
f2c1da78 4179 for function types. */
6615c446 4180 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4181 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4182 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4183 {
4184 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4185 int quals = orig_quals;
4186
4187 if (TREE_READONLY (arg))
4188 quals |= TYPE_QUAL_CONST;
4189 if (TREE_THIS_VOLATILE (arg))
4190 quals |= TYPE_QUAL_VOLATILE;
4191
f2c1da78
JM
4192 argtype = c_build_qualified_type (argtype, quals);
4193 }
400fbf9f 4194
3e4093b6
RS
4195 if (!c_mark_addressable (arg))
4196 return error_mark_node;
400fbf9f 4197
abb54d14
JM
4198 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4199 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4200
5cc200fc 4201 argtype = build_pointer_type (argtype);
5e55f99d
RH
4202
4203 /* ??? Cope with user tricks that amount to offsetof. Delete this
4204 when we have proper support for integer constant expressions. */
4205 val = get_base_address (arg);
4206 if (val && TREE_CODE (val) == INDIRECT_REF
3aa2ddb8
JJ
4207 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4208 {
cf9e9959 4209 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
c9f9eb5d 4210 goto return_build_unary_op;
3aa2ddb8 4211 }
5e55f99d 4212
5cc200fc 4213 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4214
c9f9eb5d
AH
4215 ret = val;
4216 goto return_build_unary_op;
400fbf9f 4217
3e4093b6 4218 default:
1344f9a3 4219 gcc_unreachable ();
3e4093b6 4220 }
400fbf9f 4221
3e4093b6
RS
4222 if (argtype == 0)
4223 argtype = TREE_TYPE (arg);
928c19bb
JM
4224 if (TREE_CODE (arg) == INTEGER_CST)
4225 ret = (require_constant_value
db3927fb
AH
4226 ? fold_build1_initializer_loc (location, code, argtype, arg)
4227 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4228 else
4229 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4230 return_build_unary_op:
4231 gcc_assert (ret != error_mark_node);
928c19bb
JM
4232 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4233 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4234 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4235 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4236 ret = note_integer_operands (ret);
8ce94e44
JM
4237 if (eptype)
4238 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4239 protected_set_expr_location (ret, location);
4240 return ret;
3e4093b6 4241}
400fbf9f 4242
3e4093b6
RS
4243/* Return nonzero if REF is an lvalue valid for this language.
4244 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4245 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4246
1e4ae551 4247bool
58f9752a 4248lvalue_p (const_tree ref)
3e4093b6 4249{
58f9752a 4250 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4251
3e4093b6
RS
4252 switch (code)
4253 {
4254 case REALPART_EXPR:
4255 case IMAGPART_EXPR:
4256 case COMPONENT_REF:
4257 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4258
928c19bb
JM
4259 case C_MAYBE_CONST_EXPR:
4260 return lvalue_p (TREE_OPERAND (ref, 1));
4261
3e4093b6
RS
4262 case COMPOUND_LITERAL_EXPR:
4263 case STRING_CST:
4264 return 1;
400fbf9f 4265
3e4093b6
RS
4266 case INDIRECT_REF:
4267 case ARRAY_REF:
36536d79 4268 case ARRAY_NOTATION_REF:
3e4093b6
RS
4269 case VAR_DECL:
4270 case PARM_DECL:
4271 case RESULT_DECL:
4272 case ERROR_MARK:
4273 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4274 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4275
3e4093b6 4276 case BIND_EXPR:
3e4093b6 4277 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4278
3e4093b6
RS
4279 default:
4280 return 0;
4281 }
4282}
400fbf9f 4283\f
f37acdf9
JM
4284/* Give a warning for storing in something that is read-only in GCC
4285 terms but not const in ISO C terms. */
4286
4287static void
4288readonly_warning (tree arg, enum lvalue_use use)
4289{
4290 switch (use)
4291 {
4292 case lv_assign:
4293 warning (0, "assignment of read-only location %qE", arg);
4294 break;
4295 case lv_increment:
4296 warning (0, "increment of read-only location %qE", arg);
4297 break;
4298 case lv_decrement:
4299 warning (0, "decrement of read-only location %qE", arg);
4300 break;
4301 default:
4302 gcc_unreachable ();
4303 }
4304 return;
4305}
4306
37dc0d8d
JM
4307
4308/* Return nonzero if REF is an lvalue valid for this language;
4309 otherwise, print an error message and return zero. USE says
7bd11157
TT
4310 how the lvalue is being used and so selects the error message.
4311 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4312
4313static int
7bd11157 4314lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4315{
4316 int win = lvalue_p (ref);
4317
4318 if (!win)
7bd11157 4319 lvalue_error (loc, use);
37dc0d8d
JM
4320
4321 return win;
4322}
3e4093b6
RS
4323\f
4324/* Mark EXP saying that we need to be able to take the
4325 address of it; it should not be allocated in a register.
4326 Returns true if successful. */
54c93c30 4327
3e4093b6
RS
4328bool
4329c_mark_addressable (tree exp)
400fbf9f 4330{
3e4093b6 4331 tree x = exp;
95602da1 4332
3e4093b6
RS
4333 while (1)
4334 switch (TREE_CODE (x))
4335 {
4336 case COMPONENT_REF:
4337 if (DECL_C_BIT_FIELD (TREE_OPERAND (x, 1)))
4338 {
0039fa55
AN
4339 error
4340 ("cannot take address of bit-field %qD", TREE_OPERAND (x, 1));
3e4093b6
RS
4341 return false;
4342 }
95602da1 4343
3e4093b6 4344 /* ... fall through ... */
95602da1 4345
3e4093b6
RS
4346 case ADDR_EXPR:
4347 case ARRAY_REF:
4348 case REALPART_EXPR:
4349 case IMAGPART_EXPR:
4350 x = TREE_OPERAND (x, 0);
4351 break;
95602da1 4352
3e4093b6
RS
4353 case COMPOUND_LITERAL_EXPR:
4354 case CONSTRUCTOR:
4355 TREE_ADDRESSABLE (x) = 1;
4356 return true;
95602da1 4357
3e4093b6
RS
4358 case VAR_DECL:
4359 case CONST_DECL:
4360 case PARM_DECL:
4361 case RESULT_DECL:
5baeaac0 4362 if (C_DECL_REGISTER (x)
3e4093b6
RS
4363 && DECL_NONLOCAL (x))
4364 {
e697b20f 4365 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
3e4093b6 4366 {
0039fa55
AN
4367 error
4368 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4369 return false;
4370 }
509c9d60 4371 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4372 }
5baeaac0 4373 else if (C_DECL_REGISTER (x))
3e4093b6 4374 {
e697b20f 4375 if (TREE_PUBLIC (x) || TREE_STATIC (x) || DECL_EXTERNAL (x))
0039fa55
AN
4376 error ("address of global register variable %qD requested", x);
4377 else
4378 error ("address of register variable %qD requested", x);
4379 return false;
3e4093b6 4380 }
400fbf9f 4381
3e4093b6
RS
4382 /* drops in */
4383 case FUNCTION_DECL:
4384 TREE_ADDRESSABLE (x) = 1;
4385 /* drops out */
4386 default:
4387 return true;
4388 }
4389}
4390\f
2d2e923f
MLI
4391/* Convert EXPR to TYPE, warning about conversion problems with
4392 constants. SEMANTIC_TYPE is the type this conversion would use
4393 without excess precision. If SEMANTIC_TYPE is NULL, this function
4394 is equivalent to convert_and_check. This function is a wrapper that
4395 handles conversions that may be different than
4396 the usual ones because of excess precision. */
4397
4398static tree
68fca595
MP
4399ep_convert_and_check (location_t loc, tree type, tree expr,
4400 tree semantic_type)
2d2e923f
MLI
4401{
4402 if (TREE_TYPE (expr) == type)
4403 return expr;
4404
4405 if (!semantic_type)
68fca595 4406 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4407
4408 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4409 && TREE_TYPE (expr) != semantic_type)
4410 {
4411 /* For integers, we need to check the real conversion, not
4412 the conversion to the excess precision type. */
68fca595 4413 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4414 }
4415 /* Result type is the excess precision type, which should be
4416 large enough, so do not check. */
4417 return convert (type, expr);
4418}
4419
928c19bb
JM
4420/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4421 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4422 if folded to an integer constant then the unselected half may
4423 contain arbitrary operations not normally permitted in constant
c2255bc4 4424 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4425
4426tree
744aa42f 4427build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
d130ae11
ILT
4428 tree op1, tree op1_original_type, tree op2,
4429 tree op2_original_type)
400fbf9f 4430{
3e4093b6
RS
4431 tree type1;
4432 tree type2;
4433 enum tree_code code1;
4434 enum tree_code code2;
4435 tree result_type = NULL;
2d2e923f 4436 tree semantic_result_type = NULL;
3e4093b6 4437 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4438 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4439 bool ifexp_int_operands;
928c19bb 4440 tree ret;
400fbf9f 4441
4d84fe7c
JM
4442 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4443 if (op1_int_operands)
4444 op1 = remove_c_maybe_const_expr (op1);
4445 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4446 if (op2_int_operands)
4447 op2 = remove_c_maybe_const_expr (op2);
4448 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4449 if (ifexp_int_operands)
4450 ifexp = remove_c_maybe_const_expr (ifexp);
4451
3e4093b6
RS
4452 /* Promote both alternatives. */
4453
4454 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4455 op1 = default_conversion (op1);
4456 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4457 op2 = default_conversion (op2);
4458
4459 if (TREE_CODE (ifexp) == ERROR_MARK
4460 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4461 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4462 return error_mark_node;
400fbf9f 4463
3e4093b6
RS
4464 type1 = TREE_TYPE (op1);
4465 code1 = TREE_CODE (type1);
4466 type2 = TREE_TYPE (op2);
4467 code2 = TREE_CODE (type2);
4468
b1adf557
JM
4469 /* C90 does not permit non-lvalue arrays in conditional expressions.
4470 In C99 they will be pointers by now. */
4471 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4472 {
744aa42f 4473 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4474 return error_mark_node;
4475 }
4476
8ce94e44
JM
4477 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4478 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4479 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4480 || code1 == COMPLEX_TYPE)
4481 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4482 || code2 == COMPLEX_TYPE))
4483 {
2d2e923f 4484 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4485 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4486 {
4487 op1 = TREE_OPERAND (op1, 0);
4488 type1 = TREE_TYPE (op1);
4489 gcc_assert (TREE_CODE (type1) == code1);
4490 }
4491 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4492 {
4493 op2 = TREE_OPERAND (op2, 0);
4494 type2 = TREE_TYPE (op2);
4495 gcc_assert (TREE_CODE (type2) == code2);
4496 }
4497 }
4498
d130ae11
ILT
4499 if (warn_cxx_compat)
4500 {
4501 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4502 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4503
4504 if (TREE_CODE (t1) == ENUMERAL_TYPE
4505 && TREE_CODE (t2) == ENUMERAL_TYPE
4506 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4507 warning_at (colon_loc, OPT_Wc___compat,
4508 ("different enum types in conditional is "
4509 "invalid in C++: %qT vs %qT"),
4510 t1, t2);
4511 }
4512
3e4093b6
RS
4513 /* Quickly detect the usual case where op1 and op2 have the same type
4514 after promotion. */
4515 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 4516 {
3e4093b6
RS
4517 if (type1 == type2)
4518 result_type = type1;
4519 else
4520 result_type = TYPE_MAIN_VARIANT (type1);
4521 }
4522 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
4523 || code1 == COMPLEX_TYPE)
4524 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4525 || code2 == COMPLEX_TYPE))
3e4093b6 4526 {
ccf7f880 4527 result_type = c_common_type (type1, type2);
0a0b3574
MM
4528 do_warn_double_promotion (result_type, type1, type2,
4529 "implicit conversion from %qT to %qT to "
4530 "match other result of conditional",
4531 colon_loc);
400fbf9f 4532
3e4093b6
RS
4533 /* If -Wsign-compare, warn here if type1 and type2 have
4534 different signedness. We'll promote the signed to unsigned
4535 and later code won't know it used to be different.
4536 Do this check on the original types, so that explicit casts
4537 will be considered, but default promotions won't. */
7d882b83 4538 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 4539 {
8df83eae
RK
4540 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4541 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 4542
3e4093b6
RS
4543 if (unsigned_op1 ^ unsigned_op2)
4544 {
6ac01510
ILT
4545 bool ovf;
4546
3e4093b6
RS
4547 /* Do not warn if the result type is signed, since the
4548 signed type will only be chosen if it can represent
4549 all the values of the unsigned type. */
3f75a254 4550 if (!TYPE_UNSIGNED (result_type))
3e4093b6 4551 /* OK */;
3e4093b6 4552 else
928c19bb
JM
4553 {
4554 bool op1_maybe_const = true;
4555 bool op2_maybe_const = true;
4556
4557 /* Do not warn if the signed quantity is an
4558 unsuffixed integer literal (or some static
4559 constant expression involving such literals) and
4560 it is non-negative. This warning requires the
4561 operands to be folded for best results, so do
4562 that folding in this case even without
4563 warn_sign_compare to avoid warning options
4564 possibly affecting code generation. */
f5178456
RS
4565 c_inhibit_evaluation_warnings
4566 += (ifexp == truthvalue_false_node);
928c19bb
JM
4567 op1 = c_fully_fold (op1, require_constant_value,
4568 &op1_maybe_const);
f5178456
RS
4569 c_inhibit_evaluation_warnings
4570 -= (ifexp == truthvalue_false_node);
4571
4572 c_inhibit_evaluation_warnings
4573 += (ifexp == truthvalue_true_node);
928c19bb
JM
4574 op2 = c_fully_fold (op2, require_constant_value,
4575 &op2_maybe_const);
f5178456
RS
4576 c_inhibit_evaluation_warnings
4577 -= (ifexp == truthvalue_true_node);
928c19bb
JM
4578
4579 if (warn_sign_compare)
4580 {
4581 if ((unsigned_op2
4582 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4583 || (unsigned_op1
4584 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4585 /* OK */;
4586 else
744aa42f
ILT
4587 warning_at (colon_loc, OPT_Wsign_compare,
4588 ("signed and unsigned type in "
4589 "conditional expression"));
928c19bb
JM
4590 }
4591 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 4592 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 4593 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 4594 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 4595 }
3e4093b6
RS
4596 }
4597 }
4598 }
4599 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4600 {
fcf73884 4601 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 4602 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 4603 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
4604 result_type = void_type_node;
4605 }
4606 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
4607 {
36c5e70a
BE
4608 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
4609 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
4610 addr_space_t as_common;
4611
744aa42f 4612 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 4613 result_type = common_pointer_type (type1, type2);
6aa3c60d 4614 else if (null_pointer_constant_p (orig_op1))
36c5e70a 4615 result_type = type2;
6aa3c60d 4616 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
4617 result_type = type1;
4618 else if (!addr_space_superset (as1, as2, &as_common))
4619 {
4620 error_at (colon_loc, "pointers to disjoint address spaces "
4621 "used in conditional expression");
4622 return error_mark_node;
4623 }
267bac10
JM
4624 else if (VOID_TYPE_P (TREE_TYPE (type1))
4625 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 4626 {
fcf73884 4627 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 4628 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4629 "ISO C forbids conditional expr between "
bda67431 4630 "%<void *%> and function pointer");
3e4093b6
RS
4631 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
4632 TREE_TYPE (type2)));
34a80643 4633 }
267bac10
JM
4634 else if (VOID_TYPE_P (TREE_TYPE (type2))
4635 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 4636 {
fcf73884 4637 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 4638 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 4639 "ISO C forbids conditional expr between "
bda67431 4640 "%<void *%> and function pointer");
3e4093b6
RS
4641 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
4642 TREE_TYPE (type1)));
1c2a9b35 4643 }
b581b85b
NP
4644 /* Objective-C pointer comparisons are a bit more lenient. */
4645 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
4646 result_type = objc_common_type (type1, type2);
34a80643 4647 else
ab87f8c8 4648 {
36c5e70a
BE
4649 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
4650
b581b85b
NP
4651 pedwarn (colon_loc, 0,
4652 "pointer type mismatch in conditional expression");
36c5e70a
BE
4653 result_type = build_pointer_type
4654 (build_qualified_type (void_type_node, qual));
ab87f8c8 4655 }
3e4093b6
RS
4656 }
4657 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
4658 {
6aa3c60d 4659 if (!null_pointer_constant_p (orig_op2))
744aa42f 4660 pedwarn (colon_loc, 0,
509c9d60 4661 "pointer/integer type mismatch in conditional expression");
3e4093b6 4662 else
ab87f8c8 4663 {
3e4093b6 4664 op2 = null_pointer_node;
ab87f8c8 4665 }
3e4093b6
RS
4666 result_type = type1;
4667 }
4668 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
4669 {
6aa3c60d 4670 if (!null_pointer_constant_p (orig_op1))
744aa42f 4671 pedwarn (colon_loc, 0,
509c9d60 4672 "pointer/integer type mismatch in conditional expression");
3e4093b6 4673 else
ab87f8c8 4674 {
3e4093b6 4675 op1 = null_pointer_node;
ab87f8c8 4676 }
3e4093b6
RS
4677 result_type = type2;
4678 }
1c2a9b35 4679
3e4093b6
RS
4680 if (!result_type)
4681 {
4682 if (flag_cond_mismatch)
4683 result_type = void_type_node;
4684 else
400fbf9f 4685 {
c2255bc4 4686 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 4687 return error_mark_node;
400fbf9f 4688 }
3e4093b6 4689 }
400fbf9f 4690
3e4093b6
RS
4691 /* Merge const and volatile flags of the incoming types. */
4692 result_type
4693 = build_type_variant (result_type,
afbd0665
AS
4694 TYPE_READONLY (type1) || TYPE_READONLY (type2),
4695 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 4696
68fca595
MP
4697 op1 = ep_convert_and_check (colon_loc, result_type, op1,
4698 semantic_result_type);
4699 op2 = ep_convert_and_check (colon_loc, result_type, op2,
4700 semantic_result_type);
b6a10c9f 4701
928c19bb
JM
4702 if (ifexp_bcp && ifexp == truthvalue_true_node)
4703 {
4704 op2_int_operands = true;
4705 op1 = c_fully_fold (op1, require_constant_value, NULL);
4706 }
4707 if (ifexp_bcp && ifexp == truthvalue_false_node)
4708 {
4709 op1_int_operands = true;
4710 op2 = c_fully_fold (op2, require_constant_value, NULL);
4711 }
4d84fe7c 4712 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
4713 && op1_int_operands
4714 && op2_int_operands);
4715 if (int_operands)
4716 {
4717 int_const = ((ifexp == truthvalue_true_node
4718 && TREE_CODE (orig_op1) == INTEGER_CST
4719 && !TREE_OVERFLOW (orig_op1))
4720 || (ifexp == truthvalue_false_node
4721 && TREE_CODE (orig_op2) == INTEGER_CST
4722 && !TREE_OVERFLOW (orig_op2)));
4723 }
4724 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 4725 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
4726 else
4727 {
01c7ccbb
JM
4728 if (int_operands)
4729 {
f34f1c87
MP
4730 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
4731 nested inside of the expression. */
4732 op1 = c_fully_fold (op1, false, NULL);
4733 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 4734 }
928c19bb
JM
4735 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
4736 if (int_operands)
4737 ret = note_integer_operands (ret);
4738 }
2d2e923f
MLI
4739 if (semantic_result_type)
4740 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 4741
c2255bc4 4742 protected_set_expr_location (ret, colon_loc);
928c19bb 4743 return ret;
3e4093b6
RS
4744}
4745\f
487a92fe 4746/* Return a compound expression that performs two expressions and
c2255bc4
AH
4747 returns the value of the second of them.
4748
4749 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 4750
3e4093b6 4751tree
c2255bc4 4752build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 4753{
4d84fe7c 4754 bool expr1_int_operands, expr2_int_operands;
8ce94e44 4755 tree eptype = NULL_TREE;
928c19bb
JM
4756 tree ret;
4757
b72271b9 4758 if (flag_cilkplus
939b37da
BI
4759 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
4760 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
4761 {
4762 error_at (loc,
4763 "spawned function call cannot be part of a comma expression");
4764 return error_mark_node;
4765 }
4d84fe7c
JM
4766 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
4767 if (expr1_int_operands)
4768 expr1 = remove_c_maybe_const_expr (expr1);
4769 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
4770 if (expr2_int_operands)
4771 expr2 = remove_c_maybe_const_expr (expr2);
4772
8ce94e44
JM
4773 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
4774 expr1 = TREE_OPERAND (expr1, 0);
4775 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
4776 {
4777 eptype = TREE_TYPE (expr2);
4778 expr2 = TREE_OPERAND (expr2, 0);
4779 }
4780
3f75a254 4781 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
4782 {
4783 /* The left-hand operand of a comma expression is like an expression
c5409249 4784 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 4785 any side-effects, unless it was explicitly cast to (void). */
e14a6540 4786 if (warn_unused_value)
47aecf47 4787 {
e14a6540 4788 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 4789 && CONVERT_EXPR_P (expr1))
47aecf47 4790 ; /* (void) a, b */
e14a6540
JM
4791 else if (VOID_TYPE_P (TREE_TYPE (expr1))
4792 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 4793 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
4794 ; /* (void) a, (void) b, c */
4795 else
b8698a0f 4796 warning_at (loc, OPT_Wunused_value,
c2255bc4 4797 "left-hand operand of comma expression has no effect");
47aecf47 4798 }
3e4093b6 4799 }
789eadcd
MP
4800 else if (TREE_CODE (expr1) == COMPOUND_EXPR
4801 && warn_unused_value)
4802 {
4803 tree r = expr1;
4804 location_t cloc = loc;
4805 while (TREE_CODE (r) == COMPOUND_EXPR)
4806 {
4807 if (EXPR_HAS_LOCATION (r))
4808 cloc = EXPR_LOCATION (r);
4809 r = TREE_OPERAND (r, 1);
4810 }
4811 if (!TREE_SIDE_EFFECTS (r)
4812 && !VOID_TYPE_P (TREE_TYPE (r))
4813 && !CONVERT_EXPR_P (r))
4814 warning_at (cloc, OPT_Wunused_value,
4815 "right-hand operand of comma expression has no effect");
4816 }
400fbf9f 4817
3e4093b6
RS
4818 /* With -Wunused, we should also warn if the left-hand operand does have
4819 side-effects, but computes a value which is not used. For example, in
4820 `foo() + bar(), baz()' the result of the `+' operator is not used,
4821 so we should issue a warning. */
4822 else if (warn_unused_value)
c2255bc4 4823 warn_if_unused_value (expr1, loc);
400fbf9f 4824
e63d6886
AP
4825 if (expr2 == error_mark_node)
4826 return error_mark_node;
4827
928c19bb
JM
4828 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
4829
4830 if (flag_isoc99
4d84fe7c
JM
4831 && expr1_int_operands
4832 && expr2_int_operands)
928c19bb
JM
4833 ret = note_integer_operands (ret);
4834
8ce94e44
JM
4835 if (eptype)
4836 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
4837
c2255bc4 4838 protected_set_expr_location (ret, loc);
928c19bb 4839 return ret;
3e4093b6 4840}
400fbf9f 4841
67165eb3
ILT
4842/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
4843 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
4844 cast. Both TYPE and OTYPE are pointer types. LOC is the location
4845 of the cast. -Wcast-qual appeared on the command line. Named
4846 address space qualifiers are not handled here, because they result
4847 in different warnings. */
67165eb3
ILT
4848
4849static void
2ee3cb35 4850handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
4851{
4852 tree in_type = type;
4853 tree in_otype = otype;
4854 int added = 0;
4855 int discarded = 0;
4856 bool is_const;
4857
4858 /* Check that the qualifiers on IN_TYPE are a superset of the
4859 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
4860 nodes is uninteresting and we stop as soon as we hit a
4861 non-POINTER_TYPE node on either type. */
4862 do
4863 {
4864 in_otype = TREE_TYPE (in_otype);
4865 in_type = TREE_TYPE (in_type);
4866
4867 /* GNU C allows cv-qualified function types. 'const' means the
4868 function is very pure, 'volatile' means it can't return. We
4869 need to warn when such qualifiers are added, not when they're
4870 taken away. */
4871 if (TREE_CODE (in_otype) == FUNCTION_TYPE
4872 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
4873 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
4874 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 4875 else
36c5e70a
BE
4876 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
4877 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
4878 }
4879 while (TREE_CODE (in_type) == POINTER_TYPE
4880 && TREE_CODE (in_otype) == POINTER_TYPE);
4881
4882 if (added)
2ee3cb35
MLI
4883 warning_at (loc, OPT_Wcast_qual,
4884 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
4885
4886 if (discarded)
4887 /* There are qualifiers present in IN_OTYPE that are not present
4888 in IN_TYPE. */
2ee3cb35 4889 warning_at (loc, OPT_Wcast_qual,
7485aeea 4890 "cast discards %qv qualifier from pointer target type",
2ee3cb35 4891 discarded);
67165eb3
ILT
4892
4893 if (added || discarded)
4894 return;
4895
4896 /* A cast from **T to const **T is unsafe, because it can cause a
4897 const value to be changed with no additional warning. We only
4898 issue this warning if T is the same on both sides, and we only
4899 issue the warning if there are the same number of pointers on
4900 both sides, as otherwise the cast is clearly unsafe anyhow. A
4901 cast is unsafe when a qualifier is added at one level and const
4902 is not present at all outer levels.
4903
4904 To issue this warning, we check at each level whether the cast
4905 adds new qualifiers not already seen. We don't need to special
4906 case function types, as they won't have the same
4907 TYPE_MAIN_VARIANT. */
4908
4909 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
4910 return;
4911 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
4912 return;
4913
4914 in_type = type;
4915 in_otype = otype;
4916 is_const = TYPE_READONLY (TREE_TYPE (in_type));
4917 do
4918 {
4919 in_type = TREE_TYPE (in_type);
4920 in_otype = TREE_TYPE (in_otype);
4921 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
4922 && !is_const)
4923 {
2ee3cb35
MLI
4924 warning_at (loc, OPT_Wcast_qual,
4925 "to be safe all intermediate pointers in cast from "
4926 "%qT to %qT must be %<const%> qualified",
4927 otype, type);
67165eb3
ILT
4928 break;
4929 }
4930 if (is_const)
4931 is_const = TYPE_READONLY (in_type);
4932 }
4933 while (TREE_CODE (in_type) == POINTER_TYPE);
4934}
4935
b8698a0f 4936/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 4937 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 4938
3e4093b6 4939tree
c2255bc4 4940build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 4941{
8ce94e44
JM
4942 tree value;
4943
4944 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
4945 expr = TREE_OPERAND (expr, 0);
4946
4947 value = expr;
400fbf9f 4948
3e4093b6
RS
4949 if (type == error_mark_node || expr == error_mark_node)
4950 return error_mark_node;
400fbf9f 4951
3e4093b6
RS
4952 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
4953 only in <protocol> qualifications. But when constructing cast expressions,
4954 the protocols do matter and must be kept around. */
700686fa
ZL
4955 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
4956 return build1 (NOP_EXPR, type, expr);
4957
4958 type = TYPE_MAIN_VARIANT (type);
400fbf9f 4959
3e4093b6
RS
4960 if (TREE_CODE (type) == ARRAY_TYPE)
4961 {
c2255bc4 4962 error_at (loc, "cast specifies array type");
3e4093b6
RS
4963 return error_mark_node;
4964 }
400fbf9f 4965
3e4093b6
RS
4966 if (TREE_CODE (type) == FUNCTION_TYPE)
4967 {
c2255bc4 4968 error_at (loc, "cast specifies function type");
3e4093b6
RS
4969 return error_mark_node;
4970 }
400fbf9f 4971
808d6eaa
JM
4972 if (!VOID_TYPE_P (type))
4973 {
4974 value = require_complete_type (value);
4975 if (value == error_mark_node)
4976 return error_mark_node;
4977 }
4978
3e4093b6
RS
4979 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
4980 {
fcf73884
MLI
4981 if (TREE_CODE (type) == RECORD_TYPE
4982 || TREE_CODE (type) == UNION_TYPE)
c1771a20 4983 pedwarn (loc, OPT_Wpedantic,
fcf73884 4984 "ISO C forbids casting nonscalar to the same type");
3e4093b6
RS
4985 }
4986 else if (TREE_CODE (type) == UNION_TYPE)
4987 {
4988 tree field;
400fbf9f 4989
910ad8de 4990 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
4991 if (TREE_TYPE (field) != error_mark_node
4992 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
4993 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
4994 break;
4995
4996 if (field)
400fbf9f 4997 {
3e4093b6 4998 tree t;
e616f54d 4999 bool maybe_const = true;
3e4093b6 5000
c1771a20 5001 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5002 t = c_fully_fold (value, false, &maybe_const);
5003 t = build_constructor_single (type, field, t);
5004 if (!maybe_const)
5005 t = c_wrap_maybe_const (t, true);
5006 t = digest_init (loc, type, t,
bbbbb16a 5007 NULL_TREE, false, true, 0);
3e4093b6
RS
5008 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5009 return t;
400fbf9f 5010 }
c2255bc4 5011 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5012 return error_mark_node;
5013 }
5014 else
5015 {
5016 tree otype, ovalue;
400fbf9f 5017
3e4093b6 5018 if (type == void_type_node)
c2255bc4
AH
5019 {
5020 tree t = build1 (CONVERT_EXPR, type, value);
5021 SET_EXPR_LOCATION (t, loc);
5022 return t;
5023 }
400fbf9f 5024
3e4093b6 5025 otype = TREE_TYPE (value);
400fbf9f 5026
3e4093b6 5027 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5028 if (warn_cast_qual
5029 && TREE_CODE (type) == POINTER_TYPE
5030 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5031 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5032
36c5e70a
BE
5033 /* Warn about conversions between pointers to disjoint
5034 address spaces. */
5035 if (TREE_CODE (type) == POINTER_TYPE
5036 && TREE_CODE (otype) == POINTER_TYPE
5037 && !null_pointer_constant_p (value))
5038 {
5039 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5040 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5041 addr_space_t as_common;
5042
5043 if (!addr_space_superset (as_to, as_from, &as_common))
5044 {
5045 if (ADDR_SPACE_GENERIC_P (as_from))
5046 warning_at (loc, 0, "cast to %s address space pointer "
5047 "from disjoint generic address space pointer",
5048 c_addr_space_name (as_to));
5049
5050 else if (ADDR_SPACE_GENERIC_P (as_to))
5051 warning_at (loc, 0, "cast to generic address space pointer "
5052 "from disjoint %s address space pointer",
5053 c_addr_space_name (as_from));
5054
5055 else
5056 warning_at (loc, 0, "cast to %s address space pointer "
5057 "from disjoint %s address space pointer",
5058 c_addr_space_name (as_to),
5059 c_addr_space_name (as_from));
5060 }
5061 }
5062
3e4093b6 5063 /* Warn about possible alignment problems. */
3176a0c2 5064 if (STRICT_ALIGNMENT
3e4093b6
RS
5065 && TREE_CODE (type) == POINTER_TYPE
5066 && TREE_CODE (otype) == POINTER_TYPE
5067 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5068 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5069 /* Don't warn about opaque types, where the actual alignment
5070 restriction is unknown. */
5071 && !((TREE_CODE (TREE_TYPE (otype)) == UNION_TYPE
5072 || TREE_CODE (TREE_TYPE (otype)) == RECORD_TYPE)
5073 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5074 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
c2255bc4
AH
5075 warning_at (loc, OPT_Wcast_align,
5076 "cast increases required alignment of target type");
e9a25f70 5077
3176a0c2 5078 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5079 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5080 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5081 /* Unlike conversion of integers to pointers, where the
5082 warning is disabled for converting constants because
5083 of cases such as SIG_*, warn about converting constant
5084 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5085 sign extension, and a warning is appropriate. */
c2255bc4
AH
5086 warning_at (loc, OPT_Wpointer_to_int_cast,
5087 "cast from pointer to integer of different size");
400fbf9f 5088
3176a0c2 5089 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5090 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5091 warning_at (loc, OPT_Wbad_function_cast,
5092 "cast from function call of type %qT "
5093 "to non-matching type %qT", otype, type);
400fbf9f 5094
3176a0c2 5095 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5096 && TREE_CODE (otype) == INTEGER_TYPE
5097 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5098 /* Don't warn about converting any constant. */
5099 && !TREE_CONSTANT (value))
c2255bc4
AH
5100 warning_at (loc,
5101 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5102 "of different size");
400fbf9f 5103
79bedddc
SR
5104 if (warn_strict_aliasing <= 2)
5105 strict_aliasing_warning (otype, type, expr);
400fbf9f 5106
3897f229
JM
5107 /* If pedantic, warn for conversions between function and object
5108 pointer types, except for converting a null pointer constant
5109 to function pointer type. */
5110 if (pedantic
5111 && TREE_CODE (type) == POINTER_TYPE
5112 && TREE_CODE (otype) == POINTER_TYPE
5113 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5114 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5115 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5116 "conversion of function pointer to object pointer type");
3897f229
JM
5117
5118 if (pedantic
5119 && TREE_CODE (type) == POINTER_TYPE
5120 && TREE_CODE (otype) == POINTER_TYPE
5121 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5122 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5123 && !null_pointer_constant_p (value))
c1771a20 5124 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5125 "conversion of object pointer to function pointer type");
3897f229 5126
3e4093b6 5127 ovalue = value;
3e4093b6 5128 value = convert (type, value);
400fbf9f 5129
3e4093b6 5130 /* Ignore any integer overflow caused by the cast. */
928c19bb 5131 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5132 {
8bcd6380 5133 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5134 {
8bcd6380
RS
5135 if (!TREE_OVERFLOW (value))
5136 {
5137 /* Avoid clobbering a shared constant. */
5138 value = copy_node (value);
5139 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5140 }
6414bad6 5141 }
8bcd6380 5142 else if (TREE_OVERFLOW (value))
d8e1f97b 5143 /* Reset VALUE's overflow flags, ensuring constant sharing. */
807e902e 5144 value = wide_int_to_tree (TREE_TYPE (value), value);
3e4093b6
RS
5145 }
5146 }
400fbf9f 5147
53cd18ec
JM
5148 /* Don't let a cast be an lvalue. */
5149 if (value == expr)
db3927fb 5150 value = non_lvalue_loc (loc, value);
e9a25f70 5151
928c19bb
JM
5152 /* Don't allow the results of casting to floating-point or complex
5153 types be confused with actual constants, or casts involving
5154 integer and pointer types other than direct integer-to-integer
5155 and integer-to-pointer be confused with integer constant
5156 expressions and null pointer constants. */
5157 if (TREE_CODE (value) == REAL_CST
5158 || TREE_CODE (value) == COMPLEX_CST
5159 || (TREE_CODE (value) == INTEGER_CST
5160 && !((TREE_CODE (expr) == INTEGER_CST
5161 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5162 || TREE_CODE (expr) == REAL_CST
5163 || TREE_CODE (expr) == COMPLEX_CST)))
5164 value = build1 (NOP_EXPR, type, value);
5165
c2255bc4
AH
5166 if (CAN_HAVE_LOCATION_P (value))
5167 SET_EXPR_LOCATION (value, loc);
3e4093b6 5168 return value;
400fbf9f
JW
5169}
5170
c2255bc4
AH
5171/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5172 location of the open paren of the cast, or the position of the cast
5173 expr. */
3e4093b6 5174tree
c2255bc4 5175c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5176{
f8893e47 5177 tree type;
928c19bb
JM
5178 tree type_expr = NULL_TREE;
5179 bool type_expr_const = true;
5180 tree ret;
3e4093b6 5181 int saved_wsp = warn_strict_prototypes;
c5c76735 5182
3e4093b6
RS
5183 /* This avoids warnings about unprototyped casts on
5184 integers. E.g. "#define SIG_DFL (void(*)())0". */
5185 if (TREE_CODE (expr) == INTEGER_CST)
5186 warn_strict_prototypes = 0;
928c19bb 5187 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5188 warn_strict_prototypes = saved_wsp;
c5c76735 5189
c2255bc4 5190 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5191 if (type_expr)
5192 {
9f33203d
JM
5193 bool inner_expr_const = true;
5194 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5195 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5196 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5197 && inner_expr_const);
c2255bc4 5198 SET_EXPR_LOCATION (ret, loc);
928c19bb 5199 }
24b97832
ILT
5200
5201 if (CAN_HAVE_LOCATION_P (ret) && !EXPR_HAS_LOCATION (ret))
5202 SET_EXPR_LOCATION (ret, loc);
5203
9e5b2115
PB
5204 /* C++ does not permits types to be defined in a cast, but it
5205 allows references to incomplete types. */
5206 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5207 warning_at (loc, OPT_Wc___compat,
5208 "defining a type in a cast is invalid in C++");
5209
928c19bb 5210 return ret;
400fbf9f 5211}
3e4093b6
RS
5212\f
5213/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5214 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5215 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5216 MODIFYCODE is the code for a binary operator that we use
5217 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5218 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5219 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5220 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5221
c2255bc4
AH
5222 LOCATION is the location of the MODIFYCODE operator.
5223 RHS_LOC is the location of the RHS. */
2f6e4e97 5224
3e4093b6 5225tree
32e8bb8e 5226build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5227 enum tree_code modifycode,
c2255bc4 5228 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5229{
3e4093b6
RS
5230 tree result;
5231 tree newrhs;
241f845a 5232 tree rhseval = NULL_TREE;
8ce94e44 5233 tree rhs_semantic_type = NULL_TREE;
3e4093b6
RS
5234 tree lhstype = TREE_TYPE (lhs);
5235 tree olhstype = lhstype;
928c19bb 5236 bool npc;
267bac10 5237 bool is_atomic_op;
e9a25f70 5238
3e4093b6
RS
5239 /* Types that aren't fully specified cannot be used in assignments. */
5240 lhs = require_complete_type (lhs);
e9a25f70 5241
3e4093b6
RS
5242 /* Avoid duplicate error messages from operands that had errors. */
5243 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5244 return error_mark_node;
400fbf9f 5245
4c2ecab0
JM
5246 /* Ensure an error for assigning a non-lvalue array to an array in
5247 C90. */
5248 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5249 {
5250 error_at (location, "assignment to expression with array type");
5251 return error_mark_node;
5252 }
5253
46a88c12 5254 /* For ObjC properties, defer this check. */
7bd11157 5255 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5256 return error_mark_node;
5257
267bac10
JM
5258 is_atomic_op = really_atomic_lvalue (lhs);
5259
8ce94e44
JM
5260 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5261 {
5262 rhs_semantic_type = TREE_TYPE (rhs);
5263 rhs = TREE_OPERAND (rhs, 0);
5264 }
5265
3e4093b6 5266 newrhs = rhs;
400fbf9f 5267
928c19bb
JM
5268 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5269 {
5270 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5271 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5272 rhs_origtype);
928c19bb
JM
5273 if (inner == error_mark_node)
5274 return error_mark_node;
5275 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5276 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5277 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5278 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5279 protected_set_expr_location (result, location);
5280 return result;
5281 }
5282
3e4093b6
RS
5283 /* If a binary op has been requested, combine the old LHS value with the RHS
5284 producing the value we should actually store into the LHS. */
5285
5286 if (modifycode != NOP_EXPR)
400fbf9f 5287 {
928c19bb 5288 lhs = c_fully_fold (lhs, false, NULL);
3e4093b6 5289 lhs = stabilize_reference (lhs);
bbbbb16a 5290
267bac10
JM
5291 /* Construct the RHS for any non-atomic compound assignemnt. */
5292 if (!is_atomic_op)
5293 {
241f845a
JJ
5294 /* If in LHS op= RHS the RHS has side-effects, ensure they
5295 are preevaluated before the rest of the assignment expression's
5296 side-effects, because RHS could contain e.g. function calls
5297 that modify LHS. */
5298 if (TREE_SIDE_EFFECTS (rhs))
5299 {
5300 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5301 rhseval = newrhs;
5302 }
267bac10 5303 newrhs = build_binary_op (location,
241f845a 5304 modifycode, lhs, newrhs, 1);
267bac10
JM
5305
5306 /* The original type of the right hand side is no longer
5307 meaningful. */
5308 rhs_origtype = NULL_TREE;
5309 }
400fbf9f 5310 }
400fbf9f 5311
668ea4b1
IS
5312 if (c_dialect_objc ())
5313 {
46a88c12
NP
5314 /* Check if we are modifying an Objective-C property reference;
5315 if so, we need to generate setter calls. */
5316 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5317 if (result)
241f845a 5318 goto return_result;
46a88c12
NP
5319
5320 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5321 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5322 return error_mark_node;
5323 }
5324
9bf24266 5325 /* Give an error for storing in something that is 'const'. */
bbbd6700 5326
f37acdf9 5327 if (TYPE_READONLY (lhstype)
3e4093b6
RS
5328 || ((TREE_CODE (lhstype) == RECORD_TYPE
5329 || TREE_CODE (lhstype) == UNION_TYPE)
5330 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5331 {
c02065fc 5332 readonly_error (location, lhs, lv_assign);
953ff289
DN
5333 return error_mark_node;
5334 }
f37acdf9
JM
5335 else if (TREE_READONLY (lhs))
5336 readonly_warning (lhs, lv_assign);
bbbd6700 5337
3e4093b6
RS
5338 /* If storing into a structure or union member,
5339 it has probably been given type `int'.
5340 Compute the type that would go with
5341 the actual amount of storage the member occupies. */
bbbd6700 5342
3e4093b6
RS
5343 if (TREE_CODE (lhs) == COMPONENT_REF
5344 && (TREE_CODE (lhstype) == INTEGER_TYPE
5345 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5346 || TREE_CODE (lhstype) == REAL_TYPE
5347 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5348 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5349
3e4093b6
RS
5350 /* If storing in a field that is in actuality a short or narrower than one,
5351 we must store in the field in its actual type. */
5352
5353 if (lhstype != TREE_TYPE (lhs))
5354 {
5355 lhs = copy_node (lhs);
5356 TREE_TYPE (lhs) = lhstype;
400fbf9f 5357 }
400fbf9f 5358
32e8bb8e
ILT
5359 /* Issue -Wc++-compat warnings about an assignment to an enum type
5360 when LHS does not have its original type. This happens for,
5361 e.g., an enum bitfield in a struct. */
5362 if (warn_cxx_compat
5363 && lhs_origtype != NULL_TREE
5364 && lhs_origtype != lhstype
5365 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5366 {
5367 tree checktype = (rhs_origtype != NULL_TREE
5368 ? rhs_origtype
5369 : TREE_TYPE (rhs));
5370 if (checktype != error_mark_node
267bac10
JM
5371 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5372 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
5373 warning_at (location, OPT_Wc___compat,
5374 "enum conversion in assignment is invalid in C++");
5375 }
5376
267bac10
JM
5377 /* If the lhs is atomic, remove that qualifier. */
5378 if (is_atomic_op)
5379 {
5380 lhstype = build_qualified_type (lhstype,
5381 (TYPE_QUALS (lhstype)
5382 & ~TYPE_QUAL_ATOMIC));
5383 olhstype = build_qualified_type (olhstype,
5384 (TYPE_QUALS (lhstype)
5385 & ~TYPE_QUAL_ATOMIC));
5386 }
5387
8ce94e44
JM
5388 /* Convert new value to destination type. Fold it first, then
5389 restore any excess precision information, for the sake of
5390 conversion warnings. */
400fbf9f 5391
267bac10
JM
5392 if (!(is_atomic_op && modifycode != NOP_EXPR))
5393 {
5394 npc = null_pointer_constant_p (newrhs);
5395 newrhs = c_fully_fold (newrhs, false, NULL);
5396 if (rhs_semantic_type)
5397 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
5398 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5399 rhs_origtype, ic_assign, npc,
5400 NULL_TREE, NULL_TREE, 0);
267bac10
JM
5401 if (TREE_CODE (newrhs) == ERROR_MARK)
5402 return error_mark_node;
5403 }
400fbf9f 5404
6e955430
ZL
5405 /* Emit ObjC write barrier, if necessary. */
5406 if (c_dialect_objc () && flag_objc_gc)
5407 {
5408 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5409 if (result)
c9f9eb5d
AH
5410 {
5411 protected_set_expr_location (result, location);
241f845a 5412 goto return_result;
c9f9eb5d 5413 }
6e955430
ZL
5414 }
5415
ea4b7848 5416 /* Scan operands. */
400fbf9f 5417
267bac10
JM
5418 if (is_atomic_op)
5419 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5420 else
5421 {
5422 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5423 TREE_SIDE_EFFECTS (result) = 1;
5424 protected_set_expr_location (result, location);
5425 }
400fbf9f 5426
3e4093b6
RS
5427 /* If we got the LHS in a different type for storing in,
5428 convert the result back to the nominal type of LHS
5429 so that the value we return always has the same type
5430 as the LHS argument. */
e855c5ce 5431
3e4093b6 5432 if (olhstype == TREE_TYPE (result))
241f845a 5433 goto return_result;
c9f9eb5d 5434
68fca595
MP
5435 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5436 rhs_origtype, ic_assign, false, NULL_TREE,
5437 NULL_TREE, 0);
c9f9eb5d 5438 protected_set_expr_location (result, location);
241f845a
JJ
5439
5440return_result:
5441 if (rhseval)
5442 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 5443 return result;
3e4093b6
RS
5444}
5445\f
478a1c5b
ILT
5446/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5447 This is used to implement -fplan9-extensions. */
5448
5449static bool
5450find_anonymous_field_with_type (tree struct_type, tree type)
5451{
5452 tree field;
5453 bool found;
5454
5455 gcc_assert (TREE_CODE (struct_type) == RECORD_TYPE
5456 || TREE_CODE (struct_type) == UNION_TYPE);
5457 found = false;
5458 for (field = TYPE_FIELDS (struct_type);
5459 field != NULL_TREE;
5460 field = TREE_CHAIN (field))
5461 {
267bac10
JM
5462 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5463 ? c_build_qualified_type (TREE_TYPE (field),
5464 TYPE_QUAL_ATOMIC)
5465 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 5466 if (DECL_NAME (field) == NULL
267bac10 5467 && comptypes (type, fieldtype))
478a1c5b
ILT
5468 {
5469 if (found)
5470 return false;
5471 found = true;
5472 }
5473 else if (DECL_NAME (field) == NULL
5474 && (TREE_CODE (TREE_TYPE (field)) == RECORD_TYPE
5475 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5476 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5477 {
5478 if (found)
5479 return false;
5480 found = true;
5481 }
5482 }
5483 return found;
5484}
5485
5486/* RHS is an expression whose type is pointer to struct. If there is
5487 an anonymous field in RHS with type TYPE, then return a pointer to
5488 that field in RHS. This is used with -fplan9-extensions. This
5489 returns NULL if no conversion could be found. */
5490
5491static tree
5492convert_to_anonymous_field (location_t location, tree type, tree rhs)
5493{
5494 tree rhs_struct_type, lhs_main_type;
5495 tree field, found_field;
5496 bool found_sub_field;
5497 tree ret;
5498
5499 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5500 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
5501 gcc_assert (TREE_CODE (rhs_struct_type) == RECORD_TYPE
5502 || TREE_CODE (rhs_struct_type) == UNION_TYPE);
5503
5504 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
5505 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5506 ? c_build_qualified_type (TREE_TYPE (type),
5507 TYPE_QUAL_ATOMIC)
5508 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
5509
5510 found_field = NULL_TREE;
5511 found_sub_field = false;
5512 for (field = TYPE_FIELDS (rhs_struct_type);
5513 field != NULL_TREE;
5514 field = TREE_CHAIN (field))
5515 {
5516 if (DECL_NAME (field) != NULL_TREE
5517 || (TREE_CODE (TREE_TYPE (field)) != RECORD_TYPE
5518 && TREE_CODE (TREE_TYPE (field)) != UNION_TYPE))
5519 continue;
267bac10
JM
5520 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5521 ? c_build_qualified_type (TREE_TYPE (field),
5522 TYPE_QUAL_ATOMIC)
5523 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5524 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
5525 {
5526 if (found_field != NULL_TREE)
5527 return NULL_TREE;
5528 found_field = field;
5529 }
5530 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5531 lhs_main_type))
5532 {
5533 if (found_field != NULL_TREE)
5534 return NULL_TREE;
5535 found_field = field;
5536 found_sub_field = true;
5537 }
5538 }
5539
5540 if (found_field == NULL_TREE)
5541 return NULL_TREE;
5542
5543 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5544 build_fold_indirect_ref (rhs), found_field,
5545 NULL_TREE);
5546 ret = build_fold_addr_expr_loc (location, ret);
5547
5548 if (found_sub_field)
5549 {
5550 ret = convert_to_anonymous_field (location, type, ret);
5551 gcc_assert (ret != NULL_TREE);
5552 }
5553
5554 return ret;
5555}
5556
63bc4e87
MP
5557/* Issue an error message for a bad initializer component.
5558 GMSGID identifies the message.
5559 The component name is taken from the spelling stack. */
5560
5561static void
ea58ef42 5562error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
5563{
5564 char *ofwhat;
5565
5566 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 5567 error_at (loc, gmsgid);
63bc4e87
MP
5568 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5569 if (*ofwhat)
ea58ef42 5570 error_at (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
5571}
5572
5573/* Issue a pedantic warning for a bad initializer component. OPT is
5574 the option OPT_* (from options.h) controlling this warning or 0 if
5575 it is unconditionally given. GMSGID identifies the message. The
5576 component name is taken from the spelling stack. */
5577
5578static void
5579pedwarn_init (location_t location, int opt, const char *gmsgid)
5580{
5581 char *ofwhat;
5582
5583 /* The gmsgid may be a format string with %< and %>. */
5584 pedwarn (location, opt, gmsgid);
5585 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5586 if (*ofwhat)
5587 pedwarn (location, opt, "(near initialization for %qs)", ofwhat);
5588}
5589
5590/* Issue a warning for a bad initializer component.
5591
5592 OPT is the OPT_W* value corresponding to the warning option that
5593 controls this warning. GMSGID identifies the message. The
5594 component name is taken from the spelling stack. */
5595
5596static void
5597warning_init (location_t loc, int opt, const char *gmsgid)
5598{
5599 char *ofwhat;
5600
5601 /* The gmsgid may be a format string with %< and %>. */
5602 warning_at (loc, opt, gmsgid);
5603 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5604 if (*ofwhat)
5605 warning_at (loc, opt, "(near initialization for %qs)", ofwhat);
5606}
5607\f
5608/* If TYPE is an array type and EXPR is a parenthesized string
5609 constant, warn if pedantic that EXPR is being used to initialize an
5610 object of type TYPE. */
5611
5612void
d033409e 5613maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
5614{
5615 if (pedantic
5616 && TREE_CODE (type) == ARRAY_TYPE
5617 && TREE_CODE (expr.value) == STRING_CST
5618 && expr.original_code != STRING_CST)
d033409e 5619 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
5620 "array initialized from parenthesized string constant");
5621}
5622
bbbbb16a
ILT
5623/* Convert value RHS to type TYPE as preparation for an assignment to
5624 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
5625 original type of RHS; this differs from TREE_TYPE (RHS) for enum
5626 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
5627 constant before any folding.
3e4093b6
RS
5628 The real work of conversion is done by `convert'.
5629 The purpose of this function is to generate error messages
5630 for assignments that are not allowed in C.
2ac2f164
JM
5631 ERRTYPE says whether it is argument passing, assignment,
5632 initialization or return.
2f6e4e97 5633
81e5eca8
MP
5634 LOCATION is the location of the assignment, EXPR_LOC is the location of
5635 the RHS or, for a function, location of an argument.
2ac2f164 5636 FUNCTION is a tree for the function being called.
3e4093b6 5637 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 5638
3e4093b6 5639static tree
81e5eca8 5640convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 5641 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
5642 bool null_pointer_constant, tree fundecl,
5643 tree function, int parmnum)
3e4093b6
RS
5644{
5645 enum tree_code codel = TREE_CODE (type);
8ce94e44 5646 tree orig_rhs = rhs;
3e4093b6
RS
5647 tree rhstype;
5648 enum tree_code coder;
2ac2f164 5649 tree rname = NULL_TREE;
58393038 5650 bool objc_ok = false;
2ac2f164 5651
6b4ef5c1 5652 if (errtype == ic_argpass)
2ac2f164
JM
5653 {
5654 tree selector;
5655 /* Change pointer to function to the function itself for
5656 diagnostics. */
5657 if (TREE_CODE (function) == ADDR_EXPR
5658 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
5659 function = TREE_OPERAND (function, 0);
5660
5661 /* Handle an ObjC selector specially for diagnostics. */
5662 selector = objc_message_selector ();
5663 rname = function;
5664 if (selector && parmnum > 2)
5665 {
5666 rname = selector;
5667 parmnum -= 2;
5668 }
5669 }
5670
5671 /* This macro is used to emit diagnostics to ensure that all format
5672 strings are complete sentences, visible to gettext and checked at
5673 compile time. */
5c1bc275 5674#define WARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
5675 do { \
5676 switch (errtype) \
5677 { \
5678 case ic_argpass: \
5c1bc275 5679 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
c2255bc4 5680 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5681 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
1e053dfe
MLI
5682 "expected %qT but argument is of type %qT", \
5683 type, rhstype); \
5684 break; \
1e053dfe
MLI
5685 case ic_assign: \
5686 pedwarn (LOCATION, OPT, AS); \
5687 break; \
5688 case ic_init: \
6a8f4e12 5689 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
5690 break; \
5691 case ic_return: \
d033409e 5692 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
5693 break; \
5694 default: \
5695 gcc_unreachable (); \
5696 } \
2ac2f164 5697 } while (0)
cb3ca04e 5698
49706e39
MLI
5699 /* This macro is used to emit diagnostics to ensure that all format
5700 strings are complete sentences, visible to gettext and checked at
5701 compile time. It is the same as WARN_FOR_ASSIGNMENT but with an
5702 extra parameter to enumerate qualifiers. */
5703
5c1bc275 5704#define WARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
5705 do { \
5706 switch (errtype) \
5707 { \
5708 case ic_argpass: \
5c1bc275 5709 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
49706e39 5710 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
5c1bc275 5711 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
49706e39
MLI
5712 "expected %qT but argument is of type %qT", \
5713 type, rhstype); \
5714 break; \
5715 case ic_assign: \
5c1bc275 5716 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
5717 break; \
5718 case ic_init: \
5c1bc275 5719 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
5720 break; \
5721 case ic_return: \
5c1bc275 5722 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
5723 break; \
5724 default: \
5725 gcc_unreachable (); \
5726 } \
5727 } while (0)
5728
8ce94e44
JM
5729 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5730 rhs = TREE_OPERAND (rhs, 0);
5731
3e4093b6
RS
5732 rhstype = TREE_TYPE (rhs);
5733 coder = TREE_CODE (rhstype);
5734
5735 if (coder == ERROR_MARK)
5736 return error_mark_node;
5737
58393038
ZL
5738 if (c_dialect_objc ())
5739 {
5740 int parmno;
5741
5742 switch (errtype)
5743 {
5744 case ic_return:
5745 parmno = 0;
5746 break;
5747
5748 case ic_assign:
5749 parmno = -1;
5750 break;
5751
5752 case ic_init:
5753 parmno = -2;
5754 break;
5755
5756 default:
5757 parmno = parmnum;
5758 break;
5759 }
5760
5761 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
5762 }
5763
bbbbb16a
ILT
5764 if (warn_cxx_compat)
5765 {
5766 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
5767 if (checktype != error_mark_node
5768 && TREE_CODE (type) == ENUMERAL_TYPE
5769 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
5770 {
5c1bc275 5771 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
81f40b79
ILT
5772 G_("enum conversion when passing argument "
5773 "%d of %qE is invalid in C++"),
5774 G_("enum conversion in assignment is "
5775 "invalid in C++"),
5776 G_("enum conversion in initialization is "
5777 "invalid in C++"),
5778 G_("enum conversion in return is "
5779 "invalid in C++"));
bbbbb16a
ILT
5780 }
5781 }
5782
3e4093b6 5783 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 5784 return rhs;
3e4093b6
RS
5785
5786 if (coder == VOID_TYPE)
400fbf9f 5787 {
6dcc04b0
JM
5788 /* Except for passing an argument to an unprototyped function,
5789 this is a constraint violation. When passing an argument to
5790 an unprototyped function, it is compile-time undefined;
5791 making it a constraint in that case was rejected in
5792 DR#252. */
c2255bc4 5793 error_at (location, "void value not ignored as it ought to be");
3e4093b6 5794 return error_mark_node;
400fbf9f 5795 }
808d6eaa
JM
5796 rhs = require_complete_type (rhs);
5797 if (rhs == error_mark_node)
5798 return error_mark_node;
cd192ccc
MS
5799 /* A non-reference type can convert to a reference. This handles
5800 va_start, va_copy and possibly port built-ins. */
5801 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 5802 {
3e4093b6 5803 if (!lvalue_p (rhs))
400fbf9f 5804 {
c2255bc4 5805 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 5806 return error_mark_node;
400fbf9f 5807 }
3e4093b6
RS
5808 if (!c_mark_addressable (rhs))
5809 return error_mark_node;
5810 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 5811 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5812
81e5eca8 5813 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
5814 build_pointer_type (TREE_TYPE (type)),
5815 rhs, origtype, errtype,
5816 null_pointer_constant, fundecl, function,
5817 parmnum);
cd192ccc
MS
5818 if (rhs == error_mark_node)
5819 return error_mark_node;
3e4093b6
RS
5820
5821 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 5822 SET_EXPR_LOCATION (rhs, location);
3e4093b6 5823 return rhs;
400fbf9f 5824 }
3e4093b6 5825 /* Some types can interconvert without explicit casts. */
3274deff 5826 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 5827 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
5828 return convert (type, rhs);
5829 /* Arithmetic types all interconvert, and enum is treated like int. */
5830 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 5831 || codel == FIXED_POINT_TYPE
3e4093b6
RS
5832 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
5833 || codel == BOOLEAN_TYPE)
5834 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 5835 || coder == FIXED_POINT_TYPE
3e4093b6
RS
5836 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
5837 || coder == BOOLEAN_TYPE))
928c19bb
JM
5838 {
5839 tree ret;
5840 bool save = in_late_binary_op;
dfe776dd 5841 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb 5842 in_late_binary_op = true;
81e5eca8
MP
5843 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
5844 ? expr_loc : location, type, orig_rhs);
dfe776dd 5845 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE)
928c19bb
JM
5846 in_late_binary_op = save;
5847 return ret;
5848 }
400fbf9f 5849
79077aea
JJ
5850 /* Aggregates in different TUs might need conversion. */
5851 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
5852 && codel == coder
5853 && comptypes (type, rhstype))
81e5eca8
MP
5854 return convert_and_check (expr_loc != UNKNOWN_LOCATION
5855 ? expr_loc : location, type, rhs);
79077aea 5856
ebf0bf7f 5857 /* Conversion to a transparent union or record from its member types.
3e4093b6 5858 This applies only to function arguments. */
ebf0bf7f
JJ
5859 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
5860 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 5861 && errtype == ic_argpass)
400fbf9f 5862 {
0257e383 5863 tree memb, marginal_memb = NULL_TREE;
3e4093b6 5864
910ad8de 5865 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 5866 {
0257e383 5867 tree memb_type = TREE_TYPE (memb);
400fbf9f 5868
3e4093b6 5869 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 5870 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 5871 break;
e58cd767 5872
3e4093b6
RS
5873 if (TREE_CODE (memb_type) != POINTER_TYPE)
5874 continue;
2f6e4e97 5875
3e4093b6
RS
5876 if (coder == POINTER_TYPE)
5877 {
5878 tree ttl = TREE_TYPE (memb_type);
5879 tree ttr = TREE_TYPE (rhstype);
400fbf9f 5880
3e4093b6
RS
5881 /* Any non-function converts to a [const][volatile] void *
5882 and vice versa; otherwise, targets must be the same.
5883 Meanwhile, the lhs target must have all the qualifiers of
5884 the rhs. */
267bac10
JM
5885 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
5886 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 5887 || comp_target_types (location, memb_type, rhstype))
3e4093b6 5888 {
267bac10
JM
5889 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
5890 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 5891 /* If this type won't generate any warnings, use it. */
267bac10 5892 if (lquals == rquals
3e4093b6
RS
5893 || ((TREE_CODE (ttr) == FUNCTION_TYPE
5894 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
5895 ? ((lquals | rquals) == rquals)
5896 : ((lquals | rquals) == lquals)))
3e4093b6 5897 break;
400fbf9f 5898
3e4093b6 5899 /* Keep looking for a better type, but remember this one. */
0257e383
RH
5900 if (!marginal_memb)
5901 marginal_memb = memb;
3e4093b6
RS
5902 }
5903 }
82bde854 5904
3e4093b6 5905 /* Can convert integer zero to any pointer type. */
928c19bb 5906 if (null_pointer_constant)
3e4093b6
RS
5907 {
5908 rhs = null_pointer_node;
5909 break;
5910 }
5911 }
400fbf9f 5912
0257e383 5913 if (memb || marginal_memb)
3e4093b6 5914 {
0257e383 5915 if (!memb)
3e4093b6
RS
5916 {
5917 /* We have only a marginally acceptable member type;
5918 it needs a warning. */
0257e383 5919 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 5920 tree ttr = TREE_TYPE (rhstype);
714a0864 5921
3e4093b6
RS
5922 /* Const and volatile mean something different for function
5923 types, so the usual warnings are not appropriate. */
5924 if (TREE_CODE (ttr) == FUNCTION_TYPE
5925 && TREE_CODE (ttl) == FUNCTION_TYPE)
5926 {
5927 /* Because const and volatile on functions are
5928 restrictions that say the function will not do
5929 certain things, it is okay to use a const or volatile
5930 function where an ordinary one is wanted, but not
5931 vice-versa. */
36c5e70a
BE
5932 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
5933 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5c1bc275
MP
5934 WARN_FOR_QUALIFIERS (location, expr_loc,
5935 OPT_Wdiscarded_qualifiers,
509c9d60 5936 G_("passing argument %d of %qE "
49706e39 5937 "makes %q#v qualified function "
2ac2f164 5938 "pointer from unqualified"),
49706e39 5939 G_("assignment makes %q#v qualified "
2ac2f164
JM
5940 "function pointer from "
5941 "unqualified"),
49706e39 5942 G_("initialization makes %q#v qualified "
2ac2f164
JM
5943 "function pointer from "
5944 "unqualified"),
49706e39
MLI
5945 G_("return makes %q#v qualified function "
5946 "pointer from unqualified"),
5947 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 5948 }
36c5e70a
BE
5949 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
5950 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
5c1bc275
MP
5951 WARN_FOR_QUALIFIERS (location, expr_loc,
5952 OPT_Wdiscarded_qualifiers,
509c9d60 5953 G_("passing argument %d of %qE discards "
49706e39
MLI
5954 "%qv qualifier from pointer target type"),
5955 G_("assignment discards %qv qualifier "
2ac2f164 5956 "from pointer target type"),
49706e39 5957 G_("initialization discards %qv qualifier "
2ac2f164 5958 "from pointer target type"),
49706e39
MLI
5959 G_("return discards %qv qualifier from "
5960 "pointer target type"),
5961 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
5962
5963 memb = marginal_memb;
3e4093b6 5964 }
400fbf9f 5965
fcf73884 5966 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 5967 pedwarn (location, OPT_Wpedantic,
fcf73884 5968 "ISO C prohibits argument conversion to union type");
0e7c47fa 5969
db3927fb 5970 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 5971 return build_constructor_single (type, memb, rhs);
3e4093b6 5972 }
0e7c47fa
RK
5973 }
5974
3e4093b6
RS
5975 /* Conversions among pointers */
5976 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
5977 && (coder == codel))
400fbf9f 5978 {
3e4093b6
RS
5979 tree ttl = TREE_TYPE (type);
5980 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
5981 tree mvl = ttl;
5982 tree mvr = ttr;
3e4093b6 5983 bool is_opaque_pointer;
264fa2db 5984 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
5985 addr_space_t asl;
5986 addr_space_t asr;
400fbf9f 5987
46df2823 5988 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
5989 mvl = (TYPE_ATOMIC (mvl)
5990 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
5991 TYPE_QUAL_ATOMIC)
5992 : TYPE_MAIN_VARIANT (mvl));
46df2823 5993 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
5994 mvr = (TYPE_ATOMIC (mvr)
5995 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
5996 TYPE_QUAL_ATOMIC)
5997 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 5998 /* Opaque pointers are treated like void pointers. */
f83c7f63 5999 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6000
478a1c5b
ILT
6001 /* The Plan 9 compiler permits a pointer to a struct to be
6002 automatically converted into a pointer to an anonymous field
6003 within the struct. */
6004 if (flag_plan9_extensions
6005 && (TREE_CODE (mvl) == RECORD_TYPE || TREE_CODE(mvl) == UNION_TYPE)
6006 && (TREE_CODE (mvr) == RECORD_TYPE || TREE_CODE(mvr) == UNION_TYPE)
6007 && mvl != mvr)
6008 {
6009 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6010 if (new_rhs != NULL_TREE)
6011 {
6012 rhs = new_rhs;
6013 rhstype = TREE_TYPE (rhs);
6014 coder = TREE_CODE (rhstype);
6015 ttr = TREE_TYPE (rhstype);
6016 mvr = TYPE_MAIN_VARIANT (ttr);
6017 }
6018 }
6019
b7e20b53 6020 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6021 for the purpose of reducing the number of false positives, we
6022 tolerate the special case of
b7e20b53 6023
c22cacf3 6024 int *p = NULL;
b7e20b53 6025
c22cacf3 6026 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6027 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6028 warning_at (errtype == ic_argpass ? expr_loc : location,
6029 OPT_Wc___compat,
6030 "request for implicit conversion "
c2255bc4 6031 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6032
36c5e70a
BE
6033 /* See if the pointers point to incompatible address spaces. */
6034 asl = TYPE_ADDR_SPACE (ttl);
6035 asr = TYPE_ADDR_SPACE (ttr);
6036 if (!null_pointer_constant_p (rhs)
6037 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6038 {
6039 switch (errtype)
6040 {
6041 case ic_argpass:
8ffcdea8 6042 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6043 "non-enclosed address space", parmnum, rname);
6044 break;
6045 case ic_assign:
6046 error_at (location, "assignment from pointer to "
6047 "non-enclosed address space");
6048 break;
6049 case ic_init:
6050 error_at (location, "initialization from pointer to "
6051 "non-enclosed address space");
6052 break;
6053 case ic_return:
6054 error_at (location, "return from pointer to "
6055 "non-enclosed address space");
6056 break;
6057 default:
6058 gcc_unreachable ();
6059 }
6060 return error_mark_node;
6061 }
6062
7876a414
KG
6063 /* Check if the right-hand side has a format attribute but the
6064 left-hand side doesn't. */
90137d8f 6065 if (warn_suggest_attribute_format
104f8784 6066 && check_missing_format_attribute (type, rhstype))
c22cacf3 6067 {
104f8784
KG
6068 switch (errtype)
6069 {
6070 case ic_argpass:
8ffcdea8 6071 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6072 "argument %d of %qE might be "
6073 "a candidate for a format attribute",
6074 parmnum, rname);
104f8784
KG
6075 break;
6076 case ic_assign:
90137d8f 6077 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6078 "assignment left-hand side might be "
6079 "a candidate for a format attribute");
104f8784
KG
6080 break;
6081 case ic_init:
90137d8f 6082 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6083 "initialization left-hand side might be "
6084 "a candidate for a format attribute");
104f8784
KG
6085 break;
6086 case ic_return:
90137d8f 6087 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6088 "return type might be "
6089 "a candidate for a format attribute");
104f8784
KG
6090 break;
6091 default:
6092 gcc_unreachable ();
6093 }
7876a414 6094 }
c22cacf3 6095
3e4093b6
RS
6096 /* Any non-function converts to a [const][volatile] void *
6097 and vice versa; otherwise, targets must be the same.
6098 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6099 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6100 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6101 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6102 || is_opaque_pointer
f8a93a2e
JJ
6103 || ((c_common_unsigned_type (mvl)
6104 == c_common_unsigned_type (mvr))
267bac10
JM
6105 && (c_common_signed_type (mvl)
6106 == c_common_signed_type (mvr))
6107 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6
RS
6108 {
6109 if (pedantic
6110 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6111 ||
6112 (VOID_TYPE_P (ttr)
928c19bb 6113 && !null_pointer_constant
3e4093b6 6114 && TREE_CODE (ttl) == FUNCTION_TYPE)))
5c1bc275 6115 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
509c9d60 6116 G_("ISO C forbids passing argument %d of "
2ac2f164
JM
6117 "%qE between function pointer "
6118 "and %<void *%>"),
4b794eaf 6119 G_("ISO C forbids assignment between "
2ac2f164 6120 "function pointer and %<void *%>"),
4b794eaf 6121 G_("ISO C forbids initialization between "
2ac2f164 6122 "function pointer and %<void *%>"),
4b794eaf 6123 G_("ISO C forbids return between function "
2ac2f164 6124 "pointer and %<void *%>"));
3e4093b6
RS
6125 /* Const and volatile mean something different for function types,
6126 so the usual warnings are not appropriate. */
6127 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6128 && TREE_CODE (ttl) != FUNCTION_TYPE)
6129 {
267bac10
JM
6130 /* Assignments between atomic and non-atomic objects are OK. */
6131 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6132 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6133 {
5c1bc275
MP
6134 WARN_FOR_QUALIFIERS (location, expr_loc,
6135 OPT_Wdiscarded_qualifiers,
6c39e757
NP
6136 G_("passing argument %d of %qE discards "
6137 "%qv qualifier from pointer target type"),
6138 G_("assignment discards %qv qualifier "
6139 "from pointer target type"),
6140 G_("initialization discards %qv qualifier "
6141 "from pointer target type"),
6142 G_("return discards %qv qualifier from "
6143 "pointer target type"),
6144 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6145 }
3e4093b6
RS
6146 /* If this is not a case of ignoring a mismatch in signedness,
6147 no warning. */
6148 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6149 || target_cmp)
3e4093b6
RS
6150 ;
6151 /* If there is a mismatch, do warn. */
f2fd3821 6152 else if (warn_pointer_sign)
5c1bc275 6153 WARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
509c9d60 6154 G_("pointer targets in passing argument "
2ac2f164 6155 "%d of %qE differ in signedness"),
4b794eaf 6156 G_("pointer targets in assignment "
2ac2f164 6157 "differ in signedness"),
4b794eaf 6158 G_("pointer targets in initialization "
2ac2f164 6159 "differ in signedness"),
4b794eaf 6160 G_("pointer targets in return differ "
2ac2f164 6161 "in signedness"));
3e4093b6
RS
6162 }
6163 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6164 && TREE_CODE (ttr) == FUNCTION_TYPE)
6165 {
6166 /* Because const and volatile on functions are restrictions
6167 that say the function will not do certain things,
6168 it is okay to use a const or volatile function
6169 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6170 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6171 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
5c1bc275
MP
6172 WARN_FOR_QUALIFIERS (location, expr_loc,
6173 OPT_Wdiscarded_qualifiers,
509c9d60 6174 G_("passing argument %d of %qE makes "
49706e39 6175 "%q#v qualified function pointer "
2ac2f164 6176 "from unqualified"),
49706e39 6177 G_("assignment makes %q#v qualified function "
2ac2f164 6178 "pointer from unqualified"),
49706e39 6179 G_("initialization makes %q#v qualified "
2ac2f164 6180 "function pointer from unqualified"),
49706e39
MLI
6181 G_("return makes %q#v qualified function "
6182 "pointer from unqualified"),
6183 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
6184 }
6185 }
6186 else
58393038
ZL
6187 /* Avoid warning about the volatile ObjC EH puts on decls. */
6188 if (!objc_ok)
5c1bc275 6189 WARN_FOR_ASSIGNMENT (location, expr_loc, 0,
509c9d60 6190 G_("passing argument %d of %qE from "
58393038 6191 "incompatible pointer type"),
4b794eaf
JJ
6192 G_("assignment from incompatible pointer type"),
6193 G_("initialization from incompatible "
58393038 6194 "pointer type"),
4b794eaf 6195 G_("return from incompatible pointer type"));
58393038 6196
3e4093b6
RS
6197 return convert (type, rhs);
6198 }
b494fd98
EB
6199 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6200 {
6dcc04b0
JM
6201 /* ??? This should not be an error when inlining calls to
6202 unprototyped functions. */
c2255bc4 6203 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
6204 return error_mark_node;
6205 }
3e4093b6 6206 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 6207 {
3e4093b6
RS
6208 /* An explicit constant 0 can convert to a pointer,
6209 or one that results from arithmetic, even including
6210 a cast to integer type. */
928c19bb 6211 if (!null_pointer_constant)
5c1bc275 6212 WARN_FOR_ASSIGNMENT (location, expr_loc, 0,
509c9d60 6213 G_("passing argument %d of %qE makes "
2ac2f164 6214 "pointer from integer without a cast"),
4b794eaf 6215 G_("assignment makes pointer from integer "
2ac2f164 6216 "without a cast"),
4b794eaf 6217 G_("initialization makes pointer from "
2ac2f164 6218 "integer without a cast"),
4b794eaf 6219 G_("return makes pointer from integer "
2ac2f164 6220 "without a cast"));
b3006337
EB
6221
6222 return convert (type, rhs);
400fbf9f 6223 }
3e4093b6 6224 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 6225 {
5c1bc275 6226 WARN_FOR_ASSIGNMENT (location, expr_loc, 0,
509c9d60 6227 G_("passing argument %d of %qE makes integer "
2ac2f164 6228 "from pointer without a cast"),
4b794eaf 6229 G_("assignment makes integer from pointer "
2ac2f164 6230 "without a cast"),
4b794eaf 6231 G_("initialization makes integer from pointer "
2ac2f164 6232 "without a cast"),
4b794eaf 6233 G_("return makes integer from pointer "
2ac2f164 6234 "without a cast"));
3e4093b6
RS
6235 return convert (type, rhs);
6236 }
6237 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
6238 {
6239 tree ret;
6240 bool save = in_late_binary_op;
6241 in_late_binary_op = true;
6242 ret = convert (type, rhs);
6243 in_late_binary_op = save;
6244 return ret;
6245 }
400fbf9f 6246
2ac2f164 6247 switch (errtype)
3e4093b6 6248 {
2ac2f164 6249 case ic_argpass:
8ffcdea8
MP
6250 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6251 rname);
a7da8b42 6252 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
8ffcdea8 6253 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
a7da8b42 6254 "expected %qT but argument is of type %qT", type, rhstype);
2ac2f164
JM
6255 break;
6256 case ic_assign:
c2255bc4
AH
6257 error_at (location, "incompatible types when assigning to type %qT from "
6258 "type %qT", type, rhstype);
2ac2f164
JM
6259 break;
6260 case ic_init:
c2255bc4 6261 error_at (location,
8ffcdea8 6262 "incompatible types when initializing type %qT using type %qT",
c2255bc4 6263 type, rhstype);
2ac2f164
JM
6264 break;
6265 case ic_return:
c2255bc4 6266 error_at (location,
8ffcdea8 6267 "incompatible types when returning type %qT but %qT was "
c2255bc4 6268 "expected", rhstype, type);
2ac2f164
JM
6269 break;
6270 default:
6271 gcc_unreachable ();
400fbf9f 6272 }
53b01f59 6273
3e4093b6
RS
6274 return error_mark_node;
6275}
3e4093b6
RS
6276\f
6277/* If VALUE is a compound expr all of whose expressions are constant, then
6278 return its value. Otherwise, return error_mark_node.
15b732b2 6279
3e4093b6
RS
6280 This is for handling COMPOUND_EXPRs as initializer elements
6281 which is allowed with a warning when -pedantic is specified. */
15b732b2 6282
3e4093b6
RS
6283static tree
6284valid_compound_expr_initializer (tree value, tree endtype)
6285{
6286 if (TREE_CODE (value) == COMPOUND_EXPR)
6287 {
6288 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6289 == error_mark_node)
6290 return error_mark_node;
6291 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6292 endtype);
6293 }
116df786 6294 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
6295 return error_mark_node;
6296 else
6297 return value;
15b732b2 6298}
400fbf9f 6299\f
3e4093b6
RS
6300/* Perform appropriate conversions on the initial value of a variable,
6301 store it in the declaration DECL,
6302 and print any error messages that are appropriate.
bbbbb16a 6303 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
6304 If the init is invalid, store an ERROR_MARK.
6305
6306 INIT_LOC is the location of the initial value. */
400fbf9f 6307
3e4093b6 6308void
c2255bc4 6309store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 6310{
3e4093b6 6311 tree value, type;
928c19bb 6312 bool npc = false;
400fbf9f 6313
3e4093b6 6314 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 6315
3e4093b6
RS
6316 type = TREE_TYPE (decl);
6317 if (TREE_CODE (type) == ERROR_MARK)
6318 return;
400fbf9f 6319
3e4093b6 6320 /* Digest the specified initializer into an expression. */
400fbf9f 6321
928c19bb
JM
6322 if (init)
6323 npc = null_pointer_constant_p (init);
c2255bc4
AH
6324 value = digest_init (init_loc, type, init, origtype, npc,
6325 true, TREE_STATIC (decl));
400fbf9f 6326
3e4093b6 6327 /* Store the expression if valid; else report error. */
400fbf9f 6328
8400e75e 6329 if (!in_system_header_at (input_location)
3f75a254 6330 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
6331 warning (OPT_Wtraditional, "traditional C rejects automatic "
6332 "aggregate initialization");
2f6e4e97 6333
3e4093b6 6334 DECL_INITIAL (decl) = value;
400fbf9f 6335
3e4093b6
RS
6336 /* ANSI wants warnings about out-of-range constant initializers. */
6337 STRIP_TYPE_NOPS (value);
b8698a0f 6338 if (TREE_STATIC (decl))
c2658540 6339 constant_expression_warning (value);
400fbf9f 6340
3e4093b6
RS
6341 /* Check if we need to set array size from compound literal size. */
6342 if (TREE_CODE (type) == ARRAY_TYPE
6343 && TYPE_DOMAIN (type) == 0
6344 && value != error_mark_node)
400fbf9f 6345 {
3e4093b6
RS
6346 tree inside_init = init;
6347
ed248cf7 6348 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
6349 inside_init = fold (inside_init);
6350
6351 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6352 {
8d9f82d5 6353 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 6354
8d9f82d5 6355 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
6356 {
6357 /* For int foo[] = (int [3]){1}; we need to set array size
6358 now since later on array initializer will be just the
6359 brace enclosed list of the compound literal. */
e30ecc5d 6360 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 6361 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 6362 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 6363 layout_type (type);
8d9f82d5 6364 layout_decl (cldecl, 0);
e30ecc5d
JJ
6365 TREE_TYPE (decl)
6366 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
6367 }
6368 }
400fbf9f 6369 }
3e4093b6
RS
6370}
6371\f
6372/* Methods for storing and printing names for error messages. */
400fbf9f 6373
3e4093b6
RS
6374/* Implement a spelling stack that allows components of a name to be pushed
6375 and popped. Each element on the stack is this structure. */
400fbf9f 6376
3e4093b6
RS
6377struct spelling
6378{
6379 int kind;
6380 union
400fbf9f 6381 {
a0f0ab9f 6382 unsigned HOST_WIDE_INT i;
3e4093b6
RS
6383 const char *s;
6384 } u;
6385};
2f6e4e97 6386
3e4093b6
RS
6387#define SPELLING_STRING 1
6388#define SPELLING_MEMBER 2
6389#define SPELLING_BOUNDS 3
400fbf9f 6390
3e4093b6
RS
6391static struct spelling *spelling; /* Next stack element (unused). */
6392static struct spelling *spelling_base; /* Spelling stack base. */
6393static int spelling_size; /* Size of the spelling stack. */
400fbf9f 6394
3e4093b6
RS
6395/* Macros to save and restore the spelling stack around push_... functions.
6396 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 6397
3e4093b6
RS
6398#define SPELLING_DEPTH() (spelling - spelling_base)
6399#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 6400
3e4093b6
RS
6401/* Push an element on the spelling stack with type KIND and assign VALUE
6402 to MEMBER. */
400fbf9f 6403
3e4093b6
RS
6404#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6405{ \
6406 int depth = SPELLING_DEPTH (); \
6407 \
6408 if (depth >= spelling_size) \
6409 { \
6410 spelling_size += 10; \
cca8ead2
BI
6411 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6412 spelling_size); \
3e4093b6
RS
6413 RESTORE_SPELLING_DEPTH (depth); \
6414 } \
6415 \
6416 spelling->kind = (KIND); \
6417 spelling->MEMBER = (VALUE); \
6418 spelling++; \
6419}
400fbf9f 6420
3e4093b6 6421/* Push STRING on the stack. Printed literally. */
400fbf9f 6422
3e4093b6
RS
6423static void
6424push_string (const char *string)
6425{
6426 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6427}
400fbf9f 6428
3e4093b6 6429/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 6430
3e4093b6
RS
6431static void
6432push_member_name (tree decl)
6433{
6434 const char *const string
88388a52
JM
6435 = (DECL_NAME (decl)
6436 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6437 : _("<anonymous>"));
3e4093b6
RS
6438 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6439}
400fbf9f 6440
3e4093b6 6441/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 6442
3e4093b6 6443static void
a0f0ab9f 6444push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
6445{
6446 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6447}
bb58bec5 6448
3e4093b6 6449/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 6450
3e4093b6
RS
6451static int
6452spelling_length (void)
6453{
6454 int size = 0;
6455 struct spelling *p;
400fbf9f 6456
3e4093b6
RS
6457 for (p = spelling_base; p < spelling; p++)
6458 {
6459 if (p->kind == SPELLING_BOUNDS)
6460 size += 25;
6461 else
6462 size += strlen (p->u.s) + 1;
6463 }
6464
6465 return size;
400fbf9f 6466}
400fbf9f 6467
3e4093b6 6468/* Print the spelling to BUFFER and return it. */
400fbf9f 6469
3e4093b6
RS
6470static char *
6471print_spelling (char *buffer)
400fbf9f 6472{
3e4093b6
RS
6473 char *d = buffer;
6474 struct spelling *p;
400fbf9f 6475
3e4093b6
RS
6476 for (p = spelling_base; p < spelling; p++)
6477 if (p->kind == SPELLING_BOUNDS)
6478 {
a0f0ab9f 6479 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
6480 d += strlen (d);
6481 }
6482 else
6483 {
6484 const char *s;
6485 if (p->kind == SPELLING_MEMBER)
6486 *d++ = '.';
6487 for (s = p->u.s; (*d = *s++); d++)
6488 ;
6489 }
6490 *d++ = '\0';
6491 return buffer;
6492}
400fbf9f 6493
3e4093b6
RS
6494/* Digest the parser output INIT as an initializer for type TYPE.
6495 Return a C expression of type TYPE to represent the initial value.
7e842ef8 6496
bbbbb16a
ILT
6497 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
6498
928c19bb
JM
6499 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
6500
916c5919
JM
6501 If INIT is a string constant, STRICT_STRING is true if it is
6502 unparenthesized or we should not warn here for it being parenthesized.
6503 For other types of INIT, STRICT_STRING is not used.
6504
c2255bc4
AH
6505 INIT_LOC is the location of the INIT.
6506
3e4093b6
RS
6507 REQUIRE_CONSTANT requests an error if non-constant initializers or
6508 elements are seen. */
7e842ef8 6509
3e4093b6 6510static tree
c2255bc4
AH
6511digest_init (location_t init_loc, tree type, tree init, tree origtype,
6512 bool null_pointer_constant, bool strict_string,
6513 int require_constant)
3e4093b6
RS
6514{
6515 enum tree_code code = TREE_CODE (type);
6516 tree inside_init = init;
8ce94e44 6517 tree semantic_type = NULL_TREE;
928c19bb 6518 bool maybe_const = true;
7e842ef8 6519
3e4093b6 6520 if (type == error_mark_node
f01da1a5 6521 || !init
3e4093b6
RS
6522 || init == error_mark_node
6523 || TREE_TYPE (init) == error_mark_node)
6524 return error_mark_node;
7e842ef8 6525
ed248cf7 6526 STRIP_TYPE_NOPS (inside_init);
7e842ef8 6527
8ce94e44
JM
6528 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
6529 {
6530 semantic_type = TREE_TYPE (inside_init);
6531 inside_init = TREE_OPERAND (inside_init, 0);
6532 }
928c19bb
JM
6533 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
6534 inside_init = decl_constant_value_for_optimization (inside_init);
7e842ef8 6535
3e4093b6
RS
6536 /* Initialization of an array of chars from a string constant
6537 optionally enclosed in braces. */
7e842ef8 6538
197463ae
JM
6539 if (code == ARRAY_TYPE && inside_init
6540 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 6541 {
267bac10
JM
6542 tree typ1
6543 = (TYPE_ATOMIC (TREE_TYPE (type))
6544 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
6545 TYPE_QUAL_ATOMIC)
6546 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
6547 /* Note that an array could be both an array of character type
6548 and an array of wchar_t if wchar_t is signed char or unsigned
6549 char. */
6550 bool char_array = (typ1 == char_type_node
6551 || typ1 == signed_char_type_node
6552 || typ1 == unsigned_char_type_node);
6553 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
6554 bool char16_array = !!comptypes (typ1, char16_type_node);
6555 bool char32_array = !!comptypes (typ1, char32_type_node);
6556
6557 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 6558 {
916c5919 6559 struct c_expr expr;
c466b2cd 6560 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
6561 expr.value = inside_init;
6562 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 6563 expr.original_type = NULL;
d033409e 6564 maybe_warn_string_init (init_loc, type, expr);
916c5919 6565
a45e580b 6566 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 6567 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
6568 "initialization of a flexible array member");
6569
3e4093b6 6570 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6571 TYPE_MAIN_VARIANT (type)))
3e4093b6 6572 return inside_init;
7e842ef8 6573
c466b2cd 6574 if (char_array)
3e4093b6 6575 {
c466b2cd
KVH
6576 if (typ2 != char_type_node)
6577 {
ea58ef42
MP
6578 error_init (init_loc, "char-array initialized from wide "
6579 "string");
c466b2cd
KVH
6580 return error_mark_node;
6581 }
3e4093b6 6582 }
c466b2cd 6583 else
3e4093b6 6584 {
c466b2cd
KVH
6585 if (typ2 == char_type_node)
6586 {
ea58ef42
MP
6587 error_init (init_loc, "wide character array initialized "
6588 "from non-wide string");
c466b2cd
KVH
6589 return error_mark_node;
6590 }
6591 else if (!comptypes(typ1, typ2))
6592 {
ea58ef42
MP
6593 error_init (init_loc, "wide character array initialized "
6594 "from incompatible wide string");
c466b2cd
KVH
6595 return error_mark_node;
6596 }
7e842ef8 6597 }
2f6e4e97 6598
3e4093b6
RS
6599 TREE_TYPE (inside_init) = type;
6600 if (TYPE_DOMAIN (type) != 0
6601 && TYPE_SIZE (type) != 0
5eb4df45
ILT
6602 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
6603 {
6604 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
6605
c466b2cd 6606 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
6607 because it's ok to ignore the terminating null char
6608 that is counted in the length of the constant. */
5eb4df45
ILT
6609 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
6610 (len
6611 - (TYPE_PRECISION (typ1)
6612 / BITS_PER_UNIT))))
6613 pedwarn_init (init_loc, 0,
6614 ("initializer-string for array of chars "
6615 "is too long"));
6616 else if (warn_cxx_compat
6617 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
6618 warning_at (init_loc, OPT_Wc___compat,
6619 ("initializer-string for array chars "
6620 "is too long for C++"));
6621 }
7e842ef8 6622
3e4093b6 6623 return inside_init;
7e842ef8 6624 }
197463ae
JM
6625 else if (INTEGRAL_TYPE_P (typ1))
6626 {
ea58ef42 6627 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
6628 "from string constant");
6629 return error_mark_node;
6630 }
7e842ef8
PE
6631 }
6632
3e4093b6
RS
6633 /* Build a VECTOR_CST from a *constant* vector constructor. If the
6634 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
6635 below and handle as a constructor. */
e89be13b
JJ
6636 if (code == VECTOR_TYPE
6637 && TREE_CODE (TREE_TYPE (inside_init)) == VECTOR_TYPE
00c8e9f6 6638 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
6639 && TREE_CONSTANT (inside_init))
6640 {
6641 if (TREE_CODE (inside_init) == VECTOR_CST
6642 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
6643 TYPE_MAIN_VARIANT (type)))
6644 return inside_init;
6645
6646 if (TREE_CODE (inside_init) == CONSTRUCTOR)
6647 {
4038c495
GB
6648 unsigned HOST_WIDE_INT ix;
6649 tree value;
6650 bool constant_p = true;
e89be13b
JJ
6651
6652 /* Iterate through elements and check if all constructor
6653 elements are *_CSTs. */
4038c495
GB
6654 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
6655 if (!CONSTANT_CLASS_P (value))
6656 {
6657 constant_p = false;
6658 break;
6659 }
e89be13b 6660
4038c495
GB
6661 if (constant_p)
6662 return build_vector_from_ctor (type,
6663 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
6664 }
6665 }
6035d635 6666
ca085fd7
MLI
6667 if (warn_sequence_point)
6668 verify_sequence_points (inside_init);
6669
3e4093b6
RS
6670 /* Any type can be initialized
6671 from an expression of the same type, optionally with braces. */
400fbf9f 6672
3e4093b6
RS
6673 if (inside_init && TREE_TYPE (inside_init) != 0
6674 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 6675 TYPE_MAIN_VARIANT (type))
3e4093b6 6676 || (code == ARRAY_TYPE
132da1a5 6677 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6678 || (code == VECTOR_TYPE
132da1a5 6679 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 6680 || (code == POINTER_TYPE
3897f229 6681 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 6682 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 6683 TREE_TYPE (type)))))
3e4093b6
RS
6684 {
6685 if (code == POINTER_TYPE)
b494fd98 6686 {
b494fd98
EB
6687 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
6688 {
f2a71bbc
JM
6689 if (TREE_CODE (inside_init) == STRING_CST
6690 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
6691 inside_init = array_to_pointer_conversion
6692 (init_loc, inside_init);
f2a71bbc
JM
6693 else
6694 {
ea58ef42 6695 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
6696 return error_mark_node;
6697 }
b494fd98 6698 }
f2a71bbc 6699 }
b494fd98 6700
bae39a73
NS
6701 if (code == VECTOR_TYPE)
6702 /* Although the types are compatible, we may require a
6703 conversion. */
6704 inside_init = convert (type, inside_init);
3e4093b6 6705
ca58211b
PB
6706 if (require_constant
6707 && (code == VECTOR_TYPE || !flag_isoc99)
3e4093b6 6708 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 6709 {
3e4093b6
RS
6710 /* As an extension, allow initializing objects with static storage
6711 duration with compound literals (which are then treated just as
ca58211b
PB
6712 the brace enclosed list they contain). Also allow this for
6713 vectors, as we can only assign them with compound literals. */
3e4093b6
RS
6714 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
6715 inside_init = DECL_INITIAL (decl);
400fbf9f 6716 }
3e4093b6
RS
6717
6718 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
6719 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 6720 {
ea58ef42
MP
6721 error_init (init_loc, "array initialized from non-constant array "
6722 "expression");
3e4093b6 6723 return error_mark_node;
400fbf9f 6724 }
400fbf9f 6725
c1771a20 6726 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
6727 -pedantic-errors is specified. In the later case, we always want
6728 an error. In the former case, we simply want a warning. */
6729 if (require_constant && pedantic
6730 && TREE_CODE (inside_init) == COMPOUND_EXPR)
6731 {
6732 inside_init
6733 = valid_compound_expr_initializer (inside_init,
6734 TREE_TYPE (inside_init));
6735 if (inside_init == error_mark_node)
ea58ef42 6736 error_init (init_loc, "initializer element is not constant");
2f6e4e97 6737 else
c1771a20 6738 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 6739 "initializer element is not constant");
3e4093b6
RS
6740 if (flag_pedantic_errors)
6741 inside_init = error_mark_node;
6742 }
6743 else if (require_constant
116df786
RH
6744 && !initializer_constant_valid_p (inside_init,
6745 TREE_TYPE (inside_init)))
3e4093b6 6746 {
ea58ef42 6747 error_init (init_loc, "initializer element is not constant");
3e4093b6 6748 inside_init = error_mark_node;
8b40563c 6749 }
928c19bb 6750 else if (require_constant && !maybe_const)
c2255bc4 6751 pedwarn_init (init_loc, 0,
928c19bb 6752 "initializer element is not a constant expression");
f735a153 6753
90137d8f 6754 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 6755 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
6756 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
6757 type, inside_init, origtype,
bbbbb16a 6758 ic_init, null_pointer_constant,
928c19bb 6759 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
6760 return inside_init;
6761 }
f735a153 6762
3e4093b6 6763 /* Handle scalar types, including conversions. */
400fbf9f 6764
ab22c1fa
CF
6765 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
6766 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
6767 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 6768 {
f2a71bbc
JM
6769 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
6770 && (TREE_CODE (init) == STRING_CST
6771 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 6772 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
6773 if (semantic_type)
6774 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
6775 inside_init);
3e4093b6 6776 inside_init
68fca595
MP
6777 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
6778 inside_init, origtype, ic_init,
6779 null_pointer_constant, NULL_TREE, NULL_TREE,
6780 0);
2f6e4e97 6781
3274deff
JW
6782 /* Check to see if we have already given an error message. */
6783 if (inside_init == error_mark_node)
6784 ;
3f75a254 6785 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 6786 {
ea58ef42 6787 error_init (init_loc, "initializer element is not constant");
3e4093b6 6788 inside_init = error_mark_node;
400fbf9f 6789 }
3e4093b6 6790 else if (require_constant
116df786
RH
6791 && !initializer_constant_valid_p (inside_init,
6792 TREE_TYPE (inside_init)))
400fbf9f 6793 {
ea58ef42
MP
6794 error_init (init_loc, "initializer element is not computable at "
6795 "load time");
3e4093b6 6796 inside_init = error_mark_node;
400fbf9f 6797 }
928c19bb 6798 else if (require_constant && !maybe_const)
c2255bc4 6799 pedwarn_init (init_loc, 0,
928c19bb 6800 "initializer element is not a constant expression");
3e4093b6
RS
6801
6802 return inside_init;
400fbf9f 6803 }
d9fc6069 6804
3e4093b6 6805 /* Come here only for records and arrays. */
d9fc6069 6806
3e4093b6 6807 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 6808 {
ea58ef42 6809 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 6810 return error_mark_node;
d9fc6069 6811 }
3e4093b6 6812
ea58ef42 6813 error_init (init_loc, "invalid initializer");
3e4093b6 6814 return error_mark_node;
d9fc6069 6815}
400fbf9f 6816\f
3e4093b6 6817/* Handle initializers that use braces. */
400fbf9f 6818
3e4093b6
RS
6819/* Type of object we are accumulating a constructor for.
6820 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
6821static tree constructor_type;
400fbf9f 6822
3e4093b6
RS
6823/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
6824 left to fill. */
6825static tree constructor_fields;
400fbf9f 6826
3e4093b6
RS
6827/* For an ARRAY_TYPE, this is the specified index
6828 at which to store the next element we get. */
6829static tree constructor_index;
400fbf9f 6830
3e4093b6
RS
6831/* For an ARRAY_TYPE, this is the maximum index. */
6832static tree constructor_max_index;
400fbf9f 6833
3e4093b6
RS
6834/* For a RECORD_TYPE, this is the first field not yet written out. */
6835static tree constructor_unfilled_fields;
400fbf9f 6836
3e4093b6
RS
6837/* For an ARRAY_TYPE, this is the index of the first element
6838 not yet written out. */
6839static tree constructor_unfilled_index;
895ea614 6840
3e4093b6
RS
6841/* In a RECORD_TYPE, the byte index of the next consecutive field.
6842 This is so we can generate gaps between fields, when appropriate. */
6843static tree constructor_bit_index;
10d5caec 6844
3e4093b6
RS
6845/* If we are saving up the elements rather than allocating them,
6846 this is the list of elements so far (in reverse order,
6847 most recent first). */
9771b263 6848static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 6849
3e4093b6
RS
6850/* 1 if constructor should be incrementally stored into a constructor chain,
6851 0 if all the elements should be kept in AVL tree. */
6852static int constructor_incremental;
ad47f1e5 6853
3e4093b6
RS
6854/* 1 if so far this constructor's elements are all compile-time constants. */
6855static int constructor_constant;
ad47f1e5 6856
3e4093b6
RS
6857/* 1 if so far this constructor's elements are all valid address constants. */
6858static int constructor_simple;
ad47f1e5 6859
928c19bb
JM
6860/* 1 if this constructor has an element that cannot be part of a
6861 constant expression. */
6862static int constructor_nonconst;
6863
3e4093b6
RS
6864/* 1 if this constructor is erroneous so far. */
6865static int constructor_erroneous;
d45cf215 6866
3e4093b6
RS
6867/* Structure for managing pending initializer elements, organized as an
6868 AVL tree. */
d45cf215 6869
3e4093b6 6870struct init_node
d45cf215 6871{
3e4093b6
RS
6872 struct init_node *left, *right;
6873 struct init_node *parent;
6874 int balance;
6875 tree purpose;
6876 tree value;
bbbbb16a 6877 tree origtype;
d45cf215
RS
6878};
6879
3e4093b6
RS
6880/* Tree of pending elements at this constructor level.
6881 These are elements encountered out of order
6882 which belong at places we haven't reached yet in actually
6883 writing the output.
6884 Will never hold tree nodes across GC runs. */
6885static struct init_node *constructor_pending_elts;
d45cf215 6886
3e4093b6
RS
6887/* The SPELLING_DEPTH of this constructor. */
6888static int constructor_depth;
d45cf215 6889
3e4093b6
RS
6890/* DECL node for which an initializer is being read.
6891 0 means we are reading a constructor expression
6892 such as (struct foo) {...}. */
6893static tree constructor_decl;
d45cf215 6894
3e4093b6
RS
6895/* Nonzero if this is an initializer for a top-level decl. */
6896static int constructor_top_level;
d45cf215 6897
3e4093b6
RS
6898/* Nonzero if there were any member designators in this initializer. */
6899static int constructor_designated;
d45cf215 6900
3e4093b6
RS
6901/* Nesting depth of designator list. */
6902static int designator_depth;
d45cf215 6903
3e4093b6 6904/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 6905static int designator_erroneous;
d45cf215 6906
3e4093b6
RS
6907\f
6908/* This stack has a level for each implicit or explicit level of
6909 structuring in the initializer, including the outermost one. It
6910 saves the values of most of the variables above. */
d45cf215 6911
3e4093b6
RS
6912struct constructor_range_stack;
6913
6914struct constructor_stack
d45cf215 6915{
3e4093b6
RS
6916 struct constructor_stack *next;
6917 tree type;
6918 tree fields;
6919 tree index;
6920 tree max_index;
6921 tree unfilled_index;
6922 tree unfilled_fields;
6923 tree bit_index;
9771b263 6924 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6925 struct init_node *pending_elts;
6926 int offset;
6927 int depth;
916c5919 6928 /* If value nonzero, this value should replace the entire
3e4093b6 6929 constructor at this level. */
916c5919 6930 struct c_expr replacement_value;
3e4093b6
RS
6931 struct constructor_range_stack *range_stack;
6932 char constant;
6933 char simple;
928c19bb 6934 char nonconst;
3e4093b6
RS
6935 char implicit;
6936 char erroneous;
6937 char outer;
6938 char incremental;
6939 char designated;
6940};
d45cf215 6941
802415d1 6942static struct constructor_stack *constructor_stack;
d45cf215 6943
3e4093b6
RS
6944/* This stack represents designators from some range designator up to
6945 the last designator in the list. */
d45cf215 6946
3e4093b6
RS
6947struct constructor_range_stack
6948{
6949 struct constructor_range_stack *next, *prev;
6950 struct constructor_stack *stack;
6951 tree range_start;
6952 tree index;
6953 tree range_end;
6954 tree fields;
6955};
d45cf215 6956
802415d1 6957static struct constructor_range_stack *constructor_range_stack;
d45cf215 6958
3e4093b6
RS
6959/* This stack records separate initializers that are nested.
6960 Nested initializers can't happen in ANSI C, but GNU C allows them
6961 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 6962
3e4093b6 6963struct initializer_stack
d45cf215 6964{
3e4093b6
RS
6965 struct initializer_stack *next;
6966 tree decl;
3e4093b6
RS
6967 struct constructor_stack *constructor_stack;
6968 struct constructor_range_stack *constructor_range_stack;
9771b263 6969 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
6970 struct spelling *spelling;
6971 struct spelling *spelling_base;
6972 int spelling_size;
6973 char top_level;
6974 char require_constant_value;
6975 char require_constant_elements;
6976};
d45cf215 6977
802415d1 6978static struct initializer_stack *initializer_stack;
3e4093b6
RS
6979\f
6980/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
6981
6982void
a396f8ae 6983start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
400fbf9f 6984{
3e4093b6 6985 const char *locus;
cceb1885 6986 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 6987
3e4093b6 6988 p->decl = constructor_decl;
3e4093b6
RS
6989 p->require_constant_value = require_constant_value;
6990 p->require_constant_elements = require_constant_elements;
6991 p->constructor_stack = constructor_stack;
6992 p->constructor_range_stack = constructor_range_stack;
6993 p->elements = constructor_elements;
6994 p->spelling = spelling;
6995 p->spelling_base = spelling_base;
6996 p->spelling_size = spelling_size;
6997 p->top_level = constructor_top_level;
6998 p->next = initializer_stack;
6999 initializer_stack = p;
400fbf9f 7000
3e4093b6 7001 constructor_decl = decl;
3e4093b6
RS
7002 constructor_designated = 0;
7003 constructor_top_level = top_level;
400fbf9f 7004
6f17bbcf 7005 if (decl != 0 && decl != error_mark_node)
3e4093b6
RS
7006 {
7007 require_constant_value = TREE_STATIC (decl);
7008 require_constant_elements
7009 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7010 /* For a scalar, you can always use any value to initialize,
7011 even within braces. */
7012 && (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
7013 || TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
7014 || TREE_CODE (TREE_TYPE (decl)) == UNION_TYPE
7015 || TREE_CODE (TREE_TYPE (decl)) == QUAL_UNION_TYPE));
88388a52 7016 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7017 }
7018 else
7019 {
7020 require_constant_value = 0;
7021 require_constant_elements = 0;
88388a52 7022 locus = _("(anonymous)");
3e4093b6 7023 }
b71c7f8a 7024
3e4093b6
RS
7025 constructor_stack = 0;
7026 constructor_range_stack = 0;
b71c7f8a 7027
3e4093b6
RS
7028 missing_braces_mentioned = 0;
7029
7030 spelling_base = 0;
7031 spelling_size = 0;
7032 RESTORE_SPELLING_DEPTH (0);
7033
7034 if (locus)
7035 push_string (locus);
7036}
7037
7038void
7039finish_init (void)
b71c7f8a 7040{
3e4093b6 7041 struct initializer_stack *p = initializer_stack;
b71c7f8a 7042
3e4093b6
RS
7043 /* Free the whole constructor stack of this initializer. */
7044 while (constructor_stack)
7045 {
7046 struct constructor_stack *q = constructor_stack;
7047 constructor_stack = q->next;
7048 free (q);
7049 }
7050
366de0ce 7051 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7052
7053 /* Pop back to the data of the outer initializer (if any). */
36579663 7054 free (spelling_base);
3aeb3655 7055
3e4093b6 7056 constructor_decl = p->decl;
3e4093b6
RS
7057 require_constant_value = p->require_constant_value;
7058 require_constant_elements = p->require_constant_elements;
7059 constructor_stack = p->constructor_stack;
7060 constructor_range_stack = p->constructor_range_stack;
7061 constructor_elements = p->elements;
7062 spelling = p->spelling;
7063 spelling_base = p->spelling_base;
7064 spelling_size = p->spelling_size;
7065 constructor_top_level = p->top_level;
7066 initializer_stack = p->next;
7067 free (p);
b71c7f8a 7068}
400fbf9f 7069\f
3e4093b6
RS
7070/* Call here when we see the initializer is surrounded by braces.
7071 This is instead of a call to push_init_level;
7072 it is matched by a call to pop_init_level.
400fbf9f 7073
3e4093b6
RS
7074 TYPE is the type to initialize, for a constructor expression.
7075 For an initializer for a decl, TYPE is zero. */
400fbf9f 7076
3e4093b6
RS
7077void
7078really_start_incremental_init (tree type)
400fbf9f 7079{
5d038c4c 7080 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7081
3e4093b6
RS
7082 if (type == 0)
7083 type = TREE_TYPE (constructor_decl);
400fbf9f 7084
b6fc2cdb
PB
7085 if (TREE_CODE (type) == VECTOR_TYPE
7086 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7087 error ("opaque vector types cannot be initialized");
400fbf9f 7088
3e4093b6
RS
7089 p->type = constructor_type;
7090 p->fields = constructor_fields;
7091 p->index = constructor_index;
7092 p->max_index = constructor_max_index;
7093 p->unfilled_index = constructor_unfilled_index;
7094 p->unfilled_fields = constructor_unfilled_fields;
7095 p->bit_index = constructor_bit_index;
7096 p->elements = constructor_elements;
7097 p->constant = constructor_constant;
7098 p->simple = constructor_simple;
928c19bb 7099 p->nonconst = constructor_nonconst;
3e4093b6
RS
7100 p->erroneous = constructor_erroneous;
7101 p->pending_elts = constructor_pending_elts;
7102 p->depth = constructor_depth;
916c5919
JM
7103 p->replacement_value.value = 0;
7104 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7105 p->replacement_value.original_type = NULL;
3e4093b6
RS
7106 p->implicit = 0;
7107 p->range_stack = 0;
7108 p->outer = 0;
7109 p->incremental = constructor_incremental;
7110 p->designated = constructor_designated;
7111 p->next = 0;
7112 constructor_stack = p;
b13aca19 7113
3e4093b6
RS
7114 constructor_constant = 1;
7115 constructor_simple = 1;
928c19bb 7116 constructor_nonconst = 0;
3e4093b6 7117 constructor_depth = SPELLING_DEPTH ();
9771b263 7118 constructor_elements = NULL;
3e4093b6
RS
7119 constructor_pending_elts = 0;
7120 constructor_type = type;
7121 constructor_incremental = 1;
7122 constructor_designated = 0;
7123 designator_depth = 0;
b06df647 7124 designator_erroneous = 0;
400fbf9f 7125
3e4093b6
RS
7126 if (TREE_CODE (constructor_type) == RECORD_TYPE
7127 || TREE_CODE (constructor_type) == UNION_TYPE)
400fbf9f 7128 {
3e4093b6
RS
7129 constructor_fields = TYPE_FIELDS (constructor_type);
7130 /* Skip any nameless bit fields at the beginning. */
7131 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7132 && DECL_NAME (constructor_fields) == 0)
910ad8de 7133 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 7134
3e4093b6
RS
7135 constructor_unfilled_fields = constructor_fields;
7136 constructor_bit_index = bitsize_zero_node;
400fbf9f 7137 }
3e4093b6
RS
7138 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7139 {
7140 if (TYPE_DOMAIN (constructor_type))
7141 {
7142 constructor_max_index
7143 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7144
3e4093b6
RS
7145 /* Detect non-empty initializations of zero-length arrays. */
7146 if (constructor_max_index == NULL_TREE
7147 && TYPE_SIZE (constructor_type))
9a9d280e 7148 constructor_max_index = integer_minus_one_node;
400fbf9f 7149
3e4093b6
RS
7150 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7151 to initialize VLAs will cause a proper error; avoid tree
7152 checking errors as well by setting a safe value. */
7153 if (constructor_max_index
7154 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7155 constructor_max_index = integer_minus_one_node;
59c83dbf 7156
3e4093b6
RS
7157 constructor_index
7158 = convert (bitsizetype,
7159 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 7160 }
3e4093b6 7161 else
493179da
JM
7162 {
7163 constructor_index = bitsize_zero_node;
7164 constructor_max_index = NULL_TREE;
7165 }
59c83dbf 7166
3e4093b6
RS
7167 constructor_unfilled_index = constructor_index;
7168 }
7169 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7170 {
7171 /* Vectors are like simple fixed-size arrays. */
7172 constructor_max_index =
c62c040f 7173 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 7174 constructor_index = bitsize_zero_node;
3e4093b6
RS
7175 constructor_unfilled_index = constructor_index;
7176 }
7177 else
7178 {
7179 /* Handle the case of int x = {5}; */
7180 constructor_fields = constructor_type;
7181 constructor_unfilled_fields = constructor_type;
7182 }
7183}
7184\f
7185/* Push down into a subobject, for initialization.
7186 If this is for an explicit set of braces, IMPLICIT is 0.
7187 If it is because the next element belongs at a lower level,
7188 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 7189
3e4093b6 7190void
ea58ef42
MP
7191push_init_level (location_t loc, int implicit,
7192 struct obstack *braced_init_obstack)
3e4093b6
RS
7193{
7194 struct constructor_stack *p;
7195 tree value = NULL_TREE;
400fbf9f 7196
3e4093b6 7197 /* If we've exhausted any levels that didn't have braces,
472d98b4
JM
7198 pop them now. If implicit == 1, this will have been done in
7199 process_init_element; do not repeat it here because in the case
7200 of excess initializers for an empty aggregate this leads to an
7201 infinite cycle of popping a level and immediately recreating
7202 it. */
7203 if (implicit != 1)
3e4093b6 7204 {
472d98b4
JM
7205 while (constructor_stack->implicit)
7206 {
7207 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7208 || TREE_CODE (constructor_type) == UNION_TYPE)
7209 && constructor_fields == 0)
34cf811f 7210 process_init_element (input_location,
ea58ef42 7211 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7212 true, braced_init_obstack);
472d98b4
JM
7213 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7214 && constructor_max_index
7215 && tree_int_cst_lt (constructor_max_index,
7216 constructor_index))
34cf811f 7217 process_init_element (input_location,
ea58ef42 7218 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 7219 true, braced_init_obstack);
472d98b4
JM
7220 else
7221 break;
7222 }
3e4093b6 7223 }
400fbf9f 7224
3e4093b6
RS
7225 /* Unless this is an explicit brace, we need to preserve previous
7226 content if any. */
7227 if (implicit)
7228 {
7229 if ((TREE_CODE (constructor_type) == RECORD_TYPE
7230 || TREE_CODE (constructor_type) == UNION_TYPE)
7231 && constructor_fields)
a1e3b3d9 7232 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 7233 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 7234 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
7235 }
7236
5d038c4c 7237 p = XNEW (struct constructor_stack);
3e4093b6
RS
7238 p->type = constructor_type;
7239 p->fields = constructor_fields;
7240 p->index = constructor_index;
7241 p->max_index = constructor_max_index;
7242 p->unfilled_index = constructor_unfilled_index;
7243 p->unfilled_fields = constructor_unfilled_fields;
7244 p->bit_index = constructor_bit_index;
7245 p->elements = constructor_elements;
7246 p->constant = constructor_constant;
7247 p->simple = constructor_simple;
928c19bb 7248 p->nonconst = constructor_nonconst;
3e4093b6
RS
7249 p->erroneous = constructor_erroneous;
7250 p->pending_elts = constructor_pending_elts;
7251 p->depth = constructor_depth;
916c5919
JM
7252 p->replacement_value.value = 0;
7253 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 7254 p->replacement_value.original_type = NULL;
3e4093b6
RS
7255 p->implicit = implicit;
7256 p->outer = 0;
7257 p->incremental = constructor_incremental;
7258 p->designated = constructor_designated;
7259 p->next = constructor_stack;
7260 p->range_stack = 0;
7261 constructor_stack = p;
400fbf9f 7262
3e4093b6
RS
7263 constructor_constant = 1;
7264 constructor_simple = 1;
928c19bb 7265 constructor_nonconst = 0;
3e4093b6 7266 constructor_depth = SPELLING_DEPTH ();
9771b263 7267 constructor_elements = NULL;
3e4093b6
RS
7268 constructor_incremental = 1;
7269 constructor_designated = 0;
7270 constructor_pending_elts = 0;
7271 if (!implicit)
400fbf9f 7272 {
3e4093b6
RS
7273 p->range_stack = constructor_range_stack;
7274 constructor_range_stack = 0;
7275 designator_depth = 0;
b06df647 7276 designator_erroneous = 0;
3e4093b6 7277 }
400fbf9f 7278
3e4093b6
RS
7279 /* Don't die if an entire brace-pair level is superfluous
7280 in the containing level. */
7281 if (constructor_type == 0)
7282 ;
7283 else if (TREE_CODE (constructor_type) == RECORD_TYPE
7284 || TREE_CODE (constructor_type) == UNION_TYPE)
7285 {
7286 /* Don't die if there are extra init elts at the end. */
7287 if (constructor_fields == 0)
7288 constructor_type = 0;
7289 else
400fbf9f 7290 {
3e4093b6
RS
7291 constructor_type = TREE_TYPE (constructor_fields);
7292 push_member_name (constructor_fields);
7293 constructor_depth++;
400fbf9f 7294 }
6a358dcb
MP
7295 /* If upper initializer is designated, then mark this as
7296 designated too to prevent bogus warnings. */
7297 constructor_designated = p->designated;
3e4093b6
RS
7298 }
7299 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7300 {
7301 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 7302 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 7303 constructor_depth++;
400fbf9f
JW
7304 }
7305
3e4093b6 7306 if (constructor_type == 0)
400fbf9f 7307 {
ea58ef42 7308 error_init (loc, "extra brace group at end of initializer");
3e4093b6
RS
7309 constructor_fields = 0;
7310 constructor_unfilled_fields = 0;
7311 return;
400fbf9f
JW
7312 }
7313
3e4093b6
RS
7314 if (value && TREE_CODE (value) == CONSTRUCTOR)
7315 {
7316 constructor_constant = TREE_CONSTANT (value);
7317 constructor_simple = TREE_STATIC (value);
928c19bb 7318 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 7319 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 7320 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
7321 && (TREE_CODE (constructor_type) == RECORD_TYPE
7322 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 7323 set_nonincremental_init (braced_init_obstack);
3e4093b6 7324 }
400fbf9f 7325
3e4093b6
RS
7326 if (implicit == 1 && warn_missing_braces && !missing_braces_mentioned)
7327 {
7328 missing_braces_mentioned = 1;
96b40f8d
MP
7329 warning_init (input_location, OPT_Wmissing_braces,
7330 "missing braces around initializer");
3e4093b6 7331 }
400fbf9f 7332
3e4093b6
RS
7333 if (TREE_CODE (constructor_type) == RECORD_TYPE
7334 || TREE_CODE (constructor_type) == UNION_TYPE)
7335 {
7336 constructor_fields = TYPE_FIELDS (constructor_type);
7337 /* Skip any nameless bit fields at the beginning. */
7338 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7339 && DECL_NAME (constructor_fields) == 0)
f7587ed0 7340 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 7341
3e4093b6
RS
7342 constructor_unfilled_fields = constructor_fields;
7343 constructor_bit_index = bitsize_zero_node;
7344 }
7345 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
7346 {
7347 /* Vectors are like simple fixed-size arrays. */
7348 constructor_max_index =
c62c040f
RG
7349 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7350 constructor_index = bitsize_int (0);
3e4093b6
RS
7351 constructor_unfilled_index = constructor_index;
7352 }
7353 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7354 {
7355 if (TYPE_DOMAIN (constructor_type))
7356 {
7357 constructor_max_index
7358 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 7359
3e4093b6
RS
7360 /* Detect non-empty initializations of zero-length arrays. */
7361 if (constructor_max_index == NULL_TREE
7362 && TYPE_SIZE (constructor_type))
9a9d280e 7363 constructor_max_index = integer_minus_one_node;
de520661 7364
3e4093b6
RS
7365 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7366 to initialize VLAs will cause a proper error; avoid tree
7367 checking errors as well by setting a safe value. */
7368 if (constructor_max_index
7369 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 7370 constructor_max_index = integer_minus_one_node;
b62acd60 7371
3e4093b6
RS
7372 constructor_index
7373 = convert (bitsizetype,
7374 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7375 }
7376 else
7377 constructor_index = bitsize_zero_node;
de520661 7378
3e4093b6
RS
7379 constructor_unfilled_index = constructor_index;
7380 if (value && TREE_CODE (value) == STRING_CST)
7381 {
7382 /* We need to split the char/wchar array into individual
7383 characters, so that we don't have to special case it
7384 everywhere. */
a1e3b3d9 7385 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
7386 }
7387 }
7388 else
7389 {
b4519d39 7390 if (constructor_type != error_mark_node)
96b40f8d 7391 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
7392 constructor_fields = constructor_type;
7393 constructor_unfilled_fields = constructor_type;
7394 }
7395}
8b6a5902 7396
3e4093b6 7397/* At the end of an implicit or explicit brace level,
916c5919
JM
7398 finish up that level of constructor. If a single expression
7399 with redundant braces initialized that level, return the
7400 c_expr structure for that expression. Otherwise, the original_code
7401 element is set to ERROR_MARK.
7402 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 7403 from inner levels (process_init_element ignores that),
916c5919 7404 but return error_mark_node as the value from the outermost level
3e4093b6 7405 (that's what we want to put in DECL_INITIAL).
916c5919 7406 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 7407
916c5919 7408struct c_expr
ea58ef42
MP
7409pop_init_level (location_t loc, int implicit,
7410 struct obstack *braced_init_obstack)
3e4093b6
RS
7411{
7412 struct constructor_stack *p;
916c5919
JM
7413 struct c_expr ret;
7414 ret.value = 0;
7415 ret.original_code = ERROR_MARK;
6866c6e8 7416 ret.original_type = NULL;
de520661 7417
3e4093b6
RS
7418 if (implicit == 0)
7419 {
7420 /* When we come to an explicit close brace,
7421 pop any inner levels that didn't have explicit braces. */
7422 while (constructor_stack->implicit)
34cf811f 7423 process_init_element (input_location,
ea58ef42 7424 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7425 true, braced_init_obstack);
366de0ce 7426 gcc_assert (!constructor_range_stack);
3e4093b6 7427 }
e5e809f4 7428
0066ef9c
RH
7429 /* Now output all pending elements. */
7430 constructor_incremental = 1;
a1e3b3d9 7431 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 7432
3e4093b6 7433 p = constructor_stack;
e5e809f4 7434
3e4093b6
RS
7435 /* Error for initializing a flexible array member, or a zero-length
7436 array member in an inappropriate context. */
7437 if (constructor_type && constructor_fields
7438 && TREE_CODE (constructor_type) == ARRAY_TYPE
7439 && TYPE_DOMAIN (constructor_type)
3f75a254 7440 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
7441 {
7442 /* Silently discard empty initializations. The parser will
7443 already have pedwarned for empty brackets. */
7444 if (integer_zerop (constructor_unfilled_index))
7445 constructor_type = NULL_TREE;
366de0ce 7446 else
3e4093b6 7447 {
366de0ce 7448 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 7449
3e4093b6 7450 if (constructor_depth > 2)
ea58ef42 7451 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 7452 else
d033409e 7453 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 7454 "initialization of a flexible array member");
de520661 7455
3e4093b6
RS
7456 /* We have already issued an error message for the existence
7457 of a flexible array member not at the end of the structure.
535a42b1 7458 Discard the initializer so that we do not die later. */
910ad8de 7459 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
7460 constructor_type = NULL_TREE;
7461 }
3e4093b6 7462 }
de520661 7463
3e4093b6 7464 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 7465 if (warn_missing_field_initializers
3e4093b6
RS
7466 && constructor_type
7467 && TREE_CODE (constructor_type) == RECORD_TYPE
7468 && constructor_unfilled_fields)
7469 {
49819fef 7470 bool constructor_zeroinit =
9771b263
DN
7471 (vec_safe_length (constructor_elements) == 1
7472 && integer_zerop ((*constructor_elements)[0].value));
49819fef 7473
3e4093b6
RS
7474 /* Do not warn for flexible array members or zero-length arrays. */
7475 while (constructor_unfilled_fields
3f75a254 7476 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 7477 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 7478 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 7479
49819fef
AM
7480 if (constructor_unfilled_fields
7481 /* Do not warn if this level of the initializer uses member
7482 designators; it is likely to be deliberate. */
7483 && !constructor_designated
7484 /* Do not warn about initializing with ` = {0}'. */
7485 && !constructor_zeroinit)
3e4093b6 7486 {
32397f22
MLI
7487 if (warning_at (input_location, OPT_Wmissing_field_initializers,
7488 "missing initializer for field %qD of %qT",
7489 constructor_unfilled_fields,
7490 constructor_type))
7491 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 7492 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
7493 }
7494 }
de520661 7495
3e4093b6 7496 /* Pad out the end of the structure. */
916c5919 7497 if (p->replacement_value.value)
3e4093b6
RS
7498 /* If this closes a superfluous brace pair,
7499 just pass out the element between them. */
916c5919 7500 ret = p->replacement_value;
3e4093b6
RS
7501 else if (constructor_type == 0)
7502 ;
7503 else if (TREE_CODE (constructor_type) != RECORD_TYPE
7504 && TREE_CODE (constructor_type) != UNION_TYPE
7505 && TREE_CODE (constructor_type) != ARRAY_TYPE
7506 && TREE_CODE (constructor_type) != VECTOR_TYPE)
7507 {
7508 /* A nonincremental scalar initializer--just return
7509 the element, after verifying there is just one. */
9771b263 7510 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
7511 {
7512 if (!constructor_erroneous)
ea58ef42 7513 error_init (loc, "empty scalar initializer");
916c5919 7514 ret.value = error_mark_node;
3e4093b6 7515 }
9771b263 7516 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 7517 {
ea58ef42 7518 error_init (loc, "extra elements in scalar initializer");
9771b263 7519 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7520 }
7521 else
9771b263 7522 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
7523 }
7524 else
7525 {
7526 if (constructor_erroneous)
916c5919 7527 ret.value = error_mark_node;
3e4093b6
RS
7528 else
7529 {
916c5919 7530 ret.value = build_constructor (constructor_type,
4038c495 7531 constructor_elements);
3e4093b6 7532 if (constructor_constant)
51eed280 7533 TREE_CONSTANT (ret.value) = 1;
3e4093b6 7534 if (constructor_constant && constructor_simple)
916c5919 7535 TREE_STATIC (ret.value) = 1;
928c19bb
JM
7536 if (constructor_nonconst)
7537 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
7538 }
7539 }
de520661 7540
928c19bb
JM
7541 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
7542 {
7543 if (constructor_nonconst)
7544 ret.original_code = C_MAYBE_CONST_EXPR;
7545 else if (ret.original_code == C_MAYBE_CONST_EXPR)
7546 ret.original_code = ERROR_MARK;
7547 }
7548
3e4093b6
RS
7549 constructor_type = p->type;
7550 constructor_fields = p->fields;
7551 constructor_index = p->index;
7552 constructor_max_index = p->max_index;
7553 constructor_unfilled_index = p->unfilled_index;
7554 constructor_unfilled_fields = p->unfilled_fields;
7555 constructor_bit_index = p->bit_index;
7556 constructor_elements = p->elements;
7557 constructor_constant = p->constant;
7558 constructor_simple = p->simple;
928c19bb 7559 constructor_nonconst = p->nonconst;
3e4093b6
RS
7560 constructor_erroneous = p->erroneous;
7561 constructor_incremental = p->incremental;
7562 constructor_designated = p->designated;
7563 constructor_pending_elts = p->pending_elts;
7564 constructor_depth = p->depth;
7565 if (!p->implicit)
7566 constructor_range_stack = p->range_stack;
7567 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 7568
3e4093b6
RS
7569 constructor_stack = p->next;
7570 free (p);
b621a4dd 7571
5d5e98dc
VR
7572 if (ret.value == 0 && constructor_stack == 0)
7573 ret.value = error_mark_node;
916c5919 7574 return ret;
3e4093b6 7575}
8b6a5902 7576
3e4093b6
RS
7577/* Common handling for both array range and field name designators.
7578 ARRAY argument is nonzero for array ranges. Returns zero for success. */
400fbf9f 7579
3e4093b6 7580static int
ea58ef42
MP
7581set_designator (location_t loc, int array,
7582 struct obstack *braced_init_obstack)
de520661 7583{
3e4093b6
RS
7584 tree subtype;
7585 enum tree_code subcode;
de520661 7586
3e4093b6
RS
7587 /* Don't die if an entire brace-pair level is superfluous
7588 in the containing level. */
7589 if (constructor_type == 0)
7590 return 1;
de520661 7591
366de0ce
NS
7592 /* If there were errors in this designator list already, bail out
7593 silently. */
b06df647 7594 if (designator_erroneous)
3e4093b6 7595 return 1;
e28cae4f 7596
3e4093b6
RS
7597 if (!designator_depth)
7598 {
366de0ce 7599 gcc_assert (!constructor_range_stack);
de520661 7600
3e4093b6
RS
7601 /* Designator list starts at the level of closest explicit
7602 braces. */
7603 while (constructor_stack->implicit)
34cf811f 7604 process_init_element (input_location,
ea58ef42 7605 pop_init_level (loc, 1, braced_init_obstack),
34cf811f 7606 true, braced_init_obstack);
3e4093b6
RS
7607 constructor_designated = 1;
7608 return 0;
7609 }
de520661 7610
366de0ce 7611 switch (TREE_CODE (constructor_type))
3c3fa147 7612 {
366de0ce
NS
7613 case RECORD_TYPE:
7614 case UNION_TYPE:
3e4093b6
RS
7615 subtype = TREE_TYPE (constructor_fields);
7616 if (subtype != error_mark_node)
7617 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
7618 break;
7619 case ARRAY_TYPE:
3e4093b6 7620 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
7621 break;
7622 default:
7623 gcc_unreachable ();
de520661 7624 }
400fbf9f 7625
3e4093b6
RS
7626 subcode = TREE_CODE (subtype);
7627 if (array && subcode != ARRAY_TYPE)
7628 {
ea58ef42 7629 error_init (loc, "array index in non-array initializer");
3e4093b6
RS
7630 return 1;
7631 }
7632 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
7633 {
ea58ef42 7634 error_init (loc, "field name not in record or union initializer");
3e4093b6
RS
7635 return 1;
7636 }
d45cf215 7637
3e4093b6 7638 constructor_designated = 1;
ea58ef42 7639 push_init_level (loc, 2, braced_init_obstack);
3e4093b6 7640 return 0;
de520661 7641}
400fbf9f 7642
3e4093b6
RS
7643/* If there are range designators in designator list, push a new designator
7644 to constructor_range_stack. RANGE_END is end of such stack range or
7645 NULL_TREE if there is no range designator at this level. */
400fbf9f 7646
3e4093b6 7647static void
a1e3b3d9 7648push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
7649{
7650 struct constructor_range_stack *p;
400fbf9f 7651
a1e3b3d9
LB
7652 p = (struct constructor_range_stack *)
7653 obstack_alloc (braced_init_obstack,
7654 sizeof (struct constructor_range_stack));
3e4093b6
RS
7655 p->prev = constructor_range_stack;
7656 p->next = 0;
7657 p->fields = constructor_fields;
7658 p->range_start = constructor_index;
7659 p->index = constructor_index;
7660 p->stack = constructor_stack;
7661 p->range_end = range_end;
8b6a5902 7662 if (constructor_range_stack)
3e4093b6
RS
7663 constructor_range_stack->next = p;
7664 constructor_range_stack = p;
de520661 7665}
400fbf9f 7666
3e4093b6
RS
7667/* Within an array initializer, specify the next index to be initialized.
7668 FIRST is that index. If LAST is nonzero, then initialize a range
7669 of indices, running from FIRST through LAST. */
5a7ec9d9 7670
de520661 7671void
ea58ef42 7672set_init_index (location_t loc, tree first, tree last,
d033409e 7673 struct obstack *braced_init_obstack)
de520661 7674{
ea58ef42 7675 if (set_designator (loc, 1, braced_init_obstack))
3e4093b6 7676 return;
de520661 7677
b06df647 7678 designator_erroneous = 1;
de520661 7679
3ea8cd06
JM
7680 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
7681 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
7682 {
ea58ef42 7683 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
7684 return;
7685 }
7686
2b6da65c
JM
7687 if (TREE_CODE (first) != INTEGER_CST)
7688 {
7689 first = c_fully_fold (first, false, NULL);
7690 if (TREE_CODE (first) == INTEGER_CST)
d033409e 7691 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7692 "array index in initializer is not "
7693 "an integer constant expression");
7694 }
7695
7696 if (last && TREE_CODE (last) != INTEGER_CST)
7697 {
7698 last = c_fully_fold (last, false, NULL);
7699 if (TREE_CODE (last) == INTEGER_CST)
d033409e 7700 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
7701 "array index in initializer is not "
7702 "an integer constant expression");
7703 }
7704
3e4093b6 7705 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 7706 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7707 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
ea58ef42 7708 error_init (loc, "nonconstant array index in initializer");
3e4093b6 7709 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 7710 error_init (loc, "array index in non-array initializer");
622adc7e 7711 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 7712 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
7713 else if (constructor_max_index
7714 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 7715 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 7716 else
de520661 7717 {
928c19bb
JM
7718 constant_expression_warning (first);
7719 if (last)
7720 constant_expression_warning (last);
3e4093b6 7721 constructor_index = convert (bitsizetype, first);
40d3d530
JR
7722 if (tree_int_cst_lt (constructor_index, first))
7723 {
7724 constructor_index = copy_node (constructor_index);
7725 TREE_OVERFLOW (constructor_index) = 1;
7726 }
665f2503 7727
3e4093b6 7728 if (last)
2bede729 7729 {
3e4093b6
RS
7730 if (tree_int_cst_equal (first, last))
7731 last = 0;
7732 else if (tree_int_cst_lt (last, first))
7733 {
ea58ef42 7734 error_init (loc, "empty index range in initializer");
3e4093b6
RS
7735 last = 0;
7736 }
7737 else
7738 {
7739 last = convert (bitsizetype, last);
7740 if (constructor_max_index != 0
7741 && tree_int_cst_lt (constructor_max_index, last))
7742 {
ea58ef42
MP
7743 error_init (loc, "array index range in initializer exceeds "
7744 "array bounds");
3e4093b6
RS
7745 last = 0;
7746 }
7747 }
2bede729 7748 }
fed3cef0 7749
3e4093b6 7750 designator_depth++;
b06df647 7751 designator_erroneous = 0;
3e4093b6 7752 if (constructor_range_stack || last)
a1e3b3d9 7753 push_range_stack (last, braced_init_obstack);
de520661 7754 }
de520661 7755}
3e4093b6
RS
7756
7757/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 7758
de520661 7759void
ea58ef42
MP
7760set_init_label (location_t loc, tree fieldname,
7761 struct obstack *braced_init_obstack)
de520661 7762{
0fb96aa4 7763 tree field;
94ba5069 7764
ea58ef42 7765 if (set_designator (loc, 0, braced_init_obstack))
3e4093b6
RS
7766 return;
7767
b06df647 7768 designator_erroneous = 1;
3e4093b6
RS
7769
7770 if (TREE_CODE (constructor_type) != RECORD_TYPE
7771 && TREE_CODE (constructor_type) != UNION_TYPE)
94ba5069 7772 {
ea58ef42 7773 error_init (loc, "field name not in record or union initializer");
3e4093b6 7774 return;
94ba5069
RS
7775 }
7776
0fb96aa4 7777 field = lookup_field (constructor_type, fieldname);
8b6a5902 7778
0fb96aa4 7779 if (field == 0)
c51a1ba9 7780 error ("unknown field %qE specified in initializer", fieldname);
3e4093b6 7781 else
0fb96aa4
JM
7782 do
7783 {
7784 constructor_fields = TREE_VALUE (field);
7785 designator_depth++;
7786 designator_erroneous = 0;
7787 if (constructor_range_stack)
7788 push_range_stack (NULL_TREE, braced_init_obstack);
7789 field = TREE_CHAIN (field);
7790 if (field)
7791 {
ea58ef42 7792 if (set_designator (loc, 0, braced_init_obstack))
0fb96aa4
JM
7793 return;
7794 }
7795 }
7796 while (field != NULL_TREE);
3e4093b6
RS
7797}
7798\f
7799/* Add a new initializer to the tree of pending initializers. PURPOSE
7800 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
7801 VALUE is the value of that index or field. If ORIGTYPE is not
7802 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
7803
7804 IMPLICIT is true if value comes from pop_init_level (1),
7805 the new initializer has been merged with the existing one
7806 and thus no warnings should be emitted about overriding an
7807 existing initializer. */
de520661 7808
3e4093b6 7809static void
96b40f8d
MP
7810add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
7811 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
7812{
7813 struct init_node *p, **q, *r;
7814
7815 q = &constructor_pending_elts;
7816 p = 0;
7817
7818 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 7819 {
3e4093b6 7820 while (*q != 0)
91fa3c30 7821 {
3e4093b6
RS
7822 p = *q;
7823 if (tree_int_cst_lt (purpose, p->purpose))
7824 q = &p->left;
7825 else if (tree_int_cst_lt (p->purpose, purpose))
7826 q = &p->right;
7827 else
7828 {
b295aee2
JJ
7829 if (!implicit)
7830 {
7831 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7832 warning_init (loc, 0,
7833 "initialized field with side-effects "
7834 "overwritten");
b295aee2 7835 else if (warn_override_init)
96b40f8d
MP
7836 warning_init (loc, OPT_Woverride_init,
7837 "initialized field overwritten");
b295aee2 7838 }
3e4093b6 7839 p->value = value;
bbbbb16a 7840 p->origtype = origtype;
3e4093b6
RS
7841 return;
7842 }
91fa3c30 7843 }
de520661 7844 }
3e4093b6 7845 else
de520661 7846 {
3e4093b6 7847 tree bitpos;
400fbf9f 7848
3e4093b6
RS
7849 bitpos = bit_position (purpose);
7850 while (*q != NULL)
7851 {
7852 p = *q;
7853 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
7854 q = &p->left;
7855 else if (p->purpose != purpose)
7856 q = &p->right;
7857 else
7858 {
b295aee2
JJ
7859 if (!implicit)
7860 {
7861 if (TREE_SIDE_EFFECTS (p->value))
96b40f8d
MP
7862 warning_init (loc, 0,
7863 "initialized field with side-effects "
7864 "overwritten");
b295aee2 7865 else if (warn_override_init)
96b40f8d
MP
7866 warning_init (loc, OPT_Woverride_init,
7867 "initialized field overwritten");
b295aee2 7868 }
3e4093b6 7869 p->value = value;
bbbbb16a 7870 p->origtype = origtype;
3e4093b6
RS
7871 return;
7872 }
7873 }
91fa3c30 7874 }
b71c7f8a 7875
a1e3b3d9
LB
7876 r = (struct init_node *) obstack_alloc (braced_init_obstack,
7877 sizeof (struct init_node));
3e4093b6
RS
7878 r->purpose = purpose;
7879 r->value = value;
bbbbb16a 7880 r->origtype = origtype;
8b6a5902 7881
3e4093b6
RS
7882 *q = r;
7883 r->parent = p;
7884 r->left = 0;
7885 r->right = 0;
7886 r->balance = 0;
b71c7f8a 7887
3e4093b6 7888 while (p)
de520661 7889 {
3e4093b6 7890 struct init_node *s;
665f2503 7891
3e4093b6 7892 if (r == p->left)
2bede729 7893 {
3e4093b6
RS
7894 if (p->balance == 0)
7895 p->balance = -1;
7896 else if (p->balance < 0)
7897 {
7898 if (r->balance < 0)
7899 {
7900 /* L rotation. */
7901 p->left = r->right;
7902 if (p->left)
7903 p->left->parent = p;
7904 r->right = p;
e7b6a0ee 7905
3e4093b6
RS
7906 p->balance = 0;
7907 r->balance = 0;
39bc99c2 7908
3e4093b6
RS
7909 s = p->parent;
7910 p->parent = r;
7911 r->parent = s;
7912 if (s)
7913 {
7914 if (s->left == p)
7915 s->left = r;
7916 else
7917 s->right = r;
7918 }
7919 else
7920 constructor_pending_elts = r;
7921 }
7922 else
7923 {
7924 /* LR rotation. */
7925 struct init_node *t = r->right;
e7b6a0ee 7926
3e4093b6
RS
7927 r->right = t->left;
7928 if (r->right)
7929 r->right->parent = r;
7930 t->left = r;
7931
7932 p->left = t->right;
7933 if (p->left)
7934 p->left->parent = p;
7935 t->right = p;
7936
7937 p->balance = t->balance < 0;
7938 r->balance = -(t->balance > 0);
7939 t->balance = 0;
7940
7941 s = p->parent;
7942 p->parent = t;
7943 r->parent = t;
7944 t->parent = s;
7945 if (s)
7946 {
7947 if (s->left == p)
7948 s->left = t;
7949 else
7950 s->right = t;
7951 }
7952 else
7953 constructor_pending_elts = t;
7954 }
7955 break;
7956 }
7957 else
7958 {
7959 /* p->balance == +1; growth of left side balances the node. */
7960 p->balance = 0;
7961 break;
7962 }
2bede729 7963 }
3e4093b6
RS
7964 else /* r == p->right */
7965 {
7966 if (p->balance == 0)
7967 /* Growth propagation from right side. */
7968 p->balance++;
7969 else if (p->balance > 0)
7970 {
7971 if (r->balance > 0)
7972 {
7973 /* R rotation. */
7974 p->right = r->left;
7975 if (p->right)
7976 p->right->parent = p;
7977 r->left = p;
7978
7979 p->balance = 0;
7980 r->balance = 0;
7981
7982 s = p->parent;
7983 p->parent = r;
7984 r->parent = s;
7985 if (s)
7986 {
7987 if (s->left == p)
7988 s->left = r;
7989 else
7990 s->right = r;
7991 }
7992 else
7993 constructor_pending_elts = r;
7994 }
7995 else /* r->balance == -1 */
7996 {
7997 /* RL rotation */
7998 struct init_node *t = r->left;
7999
8000 r->left = t->right;
8001 if (r->left)
8002 r->left->parent = r;
8003 t->right = r;
8004
8005 p->right = t->left;
8006 if (p->right)
8007 p->right->parent = p;
8008 t->left = p;
8009
8010 r->balance = (t->balance < 0);
8011 p->balance = -(t->balance > 0);
8012 t->balance = 0;
8013
8014 s = p->parent;
8015 p->parent = t;
8016 r->parent = t;
8017 t->parent = s;
8018 if (s)
8019 {
8020 if (s->left == p)
8021 s->left = t;
8022 else
8023 s->right = t;
8024 }
8025 else
8026 constructor_pending_elts = t;
8027 }
8028 break;
8029 }
8030 else
8031 {
8032 /* p->balance == -1; growth of right side balances the node. */
8033 p->balance = 0;
8034 break;
8035 }
8036 }
8037
8038 r = p;
8039 p = p->parent;
8040 }
8041}
8042
8043/* Build AVL tree from a sorted chain. */
8044
8045static void
a1e3b3d9 8046set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8047{
4038c495
GB
8048 unsigned HOST_WIDE_INT ix;
8049 tree index, value;
3e4093b6
RS
8050
8051 if (TREE_CODE (constructor_type) != RECORD_TYPE
8052 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8053 return;
8054
4038c495 8055 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8056 add_pending_init (input_location, index, value, NULL_TREE, true,
8057 braced_init_obstack);
9771b263 8058 constructor_elements = NULL;
3e4093b6
RS
8059 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8060 {
8061 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8062 /* Skip any nameless bit fields at the beginning. */
8063 while (constructor_unfilled_fields != 0
8064 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8065 && DECL_NAME (constructor_unfilled_fields) == 0)
8066 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 8067
de520661 8068 }
3e4093b6 8069 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8070 {
3e4093b6
RS
8071 if (TYPE_DOMAIN (constructor_type))
8072 constructor_unfilled_index
8073 = convert (bitsizetype,
8074 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8075 else
8076 constructor_unfilled_index = bitsize_zero_node;
de520661 8077 }
3e4093b6 8078 constructor_incremental = 0;
de520661 8079}
400fbf9f 8080
3e4093b6 8081/* Build AVL tree from a string constant. */
de520661 8082
3e4093b6 8083static void
a1e3b3d9
LB
8084set_nonincremental_init_from_string (tree str,
8085 struct obstack * braced_init_obstack)
de520661 8086{
3e4093b6
RS
8087 tree value, purpose, type;
8088 HOST_WIDE_INT val[2];
8089 const char *p, *end;
8090 int byte, wchar_bytes, charwidth, bitpos;
de520661 8091
366de0ce 8092 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 8093
c466b2cd 8094 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6
RS
8095 charwidth = TYPE_PRECISION (char_type_node);
8096 type = TREE_TYPE (constructor_type);
8097 p = TREE_STRING_POINTER (str);
8098 end = p + TREE_STRING_LENGTH (str);
91fa3c30 8099
3e4093b6 8100 for (purpose = bitsize_zero_node;
8824edff
JJ
8101 p < end
8102 && !(constructor_max_index
8103 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 8104 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 8105 {
3e4093b6 8106 if (wchar_bytes == 1)
ffc5c6a9 8107 {
807e902e
KZ
8108 val[0] = (unsigned char) *p++;
8109 val[1] = 0;
ffc5c6a9
RH
8110 }
8111 else
3e4093b6 8112 {
3e4093b6 8113 val[1] = 0;
807e902e 8114 val[0] = 0;
3e4093b6
RS
8115 for (byte = 0; byte < wchar_bytes; byte++)
8116 {
8117 if (BYTES_BIG_ENDIAN)
8118 bitpos = (wchar_bytes - byte - 1) * charwidth;
8119 else
8120 bitpos = byte * charwidth;
807e902e 8121 val[bitpos % HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
8122 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8123 << (bitpos % HOST_BITS_PER_WIDE_INT);
8124 }
8125 }
584ef5fe 8126
8df83eae 8127 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
8128 {
8129 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8130 if (bitpos < HOST_BITS_PER_WIDE_INT)
8131 {
807e902e 8132 if (val[0] & (((HOST_WIDE_INT) 1) << (bitpos - 1)))
3e4093b6 8133 {
807e902e
KZ
8134 val[0] |= ((HOST_WIDE_INT) -1) << bitpos;
8135 val[1] = -1;
3e4093b6
RS
8136 }
8137 }
8138 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8139 {
807e902e
KZ
8140 if (val[0] < 0)
8141 val[1] = -1;
3e4093b6 8142 }
807e902e 8143 else if (val[1] & (((HOST_WIDE_INT) 1)
3e4093b6 8144 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
807e902e 8145 val[1] |= ((HOST_WIDE_INT) -1)
3e4093b6
RS
8146 << (bitpos - HOST_BITS_PER_WIDE_INT);
8147 }
ffc5c6a9 8148
807e902e
KZ
8149 value = wide_int_to_tree (type,
8150 wide_int::from_array (val, 2,
8151 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 8152 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 8153 braced_init_obstack);
9dfcc8db
BH
8154 }
8155
3e4093b6
RS
8156 constructor_incremental = 0;
8157}
de520661 8158
3e4093b6
RS
8159/* Return value of FIELD in pending initializer or zero if the field was
8160 not initialized yet. */
8161
8162static tree
a1e3b3d9 8163find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
8164{
8165 struct init_node *p;
8166
8167 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 8168 {
3e4093b6
RS
8169 if (constructor_incremental
8170 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8171 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8172
8173 p = constructor_pending_elts;
8174 while (p)
19d76e60 8175 {
3e4093b6
RS
8176 if (tree_int_cst_lt (field, p->purpose))
8177 p = p->left;
8178 else if (tree_int_cst_lt (p->purpose, field))
8179 p = p->right;
8180 else
8181 return p->value;
19d76e60 8182 }
19d76e60 8183 }
3e4093b6 8184 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 8185 {
3e4093b6 8186 tree bitpos = bit_position (field);
de520661 8187
3e4093b6
RS
8188 if (constructor_incremental
8189 && (!constructor_unfilled_fields
8190 || tree_int_cst_lt (bitpos,
8191 bit_position (constructor_unfilled_fields))))
a1e3b3d9 8192 set_nonincremental_init (braced_init_obstack);
de520661 8193
3e4093b6
RS
8194 p = constructor_pending_elts;
8195 while (p)
8196 {
8197 if (field == p->purpose)
8198 return p->value;
8199 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8200 p = p->left;
8201 else
8202 p = p->right;
8203 }
8204 }
8205 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 8206 {
9771b263
DN
8207 if (!vec_safe_is_empty (constructor_elements)
8208 && (constructor_elements->last ().index == field))
8209 return constructor_elements->last ().value;
de520661 8210 }
3e4093b6 8211 return 0;
de520661
RS
8212}
8213
3e4093b6
RS
8214/* "Output" the next constructor element.
8215 At top level, really output it to assembler code now.
8216 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 8217 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
8218 TYPE is the data type that the containing data type wants here.
8219 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
8220 If VALUE is a string constant, STRICT_STRING is true if it is
8221 unparenthesized or we should not warn here for it being parenthesized.
8222 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 8223
3e4093b6
RS
8224 PENDING if non-nil means output pending elements that belong
8225 right after this element. (PENDING is normally 1;
b295aee2
JJ
8226 it is 0 while outputting pending elements, to avoid recursion.)
8227
8228 IMPLICIT is true if value comes from pop_init_level (1),
8229 the new initializer has been merged with the existing one
8230 and thus no warnings should be emitted about overriding an
8231 existing initializer. */
8b6a5902 8232
3e4093b6 8233static void
34cf811f
MP
8234output_init_element (location_t loc, tree value, tree origtype,
8235 bool strict_string, tree type, tree field, int pending,
8236 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 8237{
8ce94e44 8238 tree semantic_type = NULL_TREE;
928c19bb
JM
8239 bool maybe_const = true;
8240 bool npc;
4038c495 8241
0a880880 8242 if (type == error_mark_node || value == error_mark_node)
8b6a5902 8243 {
3e4093b6
RS
8244 constructor_erroneous = 1;
8245 return;
8b6a5902 8246 }
46bdb9cf
JM
8247 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8248 && (TREE_CODE (value) == STRING_CST
8249 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8250 && !(TREE_CODE (value) == STRING_CST
8251 && TREE_CODE (type) == ARRAY_TYPE
8252 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8253 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8254 TYPE_MAIN_VARIANT (type)))
c2255bc4 8255 value = array_to_pointer_conversion (input_location, value);
8b6a5902 8256
3e4093b6
RS
8257 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
8258 && require_constant_value && !flag_isoc99 && pending)
8b6a5902 8259 {
3e4093b6
RS
8260 /* As an extension, allow initializing objects with static storage
8261 duration with compound literals (which are then treated just as
8262 the brace enclosed list they contain). */
8263 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8264 value = DECL_INITIAL (decl);
8b6a5902
JJ
8265 }
8266
928c19bb 8267 npc = null_pointer_constant_p (value);
8ce94e44
JM
8268 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8269 {
8270 semantic_type = TREE_TYPE (value);
8271 value = TREE_OPERAND (value, 0);
8272 }
928c19bb
JM
8273 value = c_fully_fold (value, require_constant_value, &maybe_const);
8274
3e4093b6
RS
8275 if (value == error_mark_node)
8276 constructor_erroneous = 1;
8277 else if (!TREE_CONSTANT (value))
8278 constructor_constant = 0;
116df786 8279 else if (!initializer_constant_valid_p (value, TREE_TYPE (value))
3e4093b6
RS
8280 || ((TREE_CODE (constructor_type) == RECORD_TYPE
8281 || TREE_CODE (constructor_type) == UNION_TYPE)
8282 && DECL_C_BIT_FIELD (field)
8283 && TREE_CODE (value) != INTEGER_CST))
8284 constructor_simple = 0;
928c19bb
JM
8285 if (!maybe_const)
8286 constructor_nonconst = 1;
3e4093b6 8287
116df786 8288 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 8289 {
116df786
RH
8290 if (require_constant_value)
8291 {
ea58ef42 8292 error_init (loc, "initializer element is not constant");
116df786
RH
8293 value = error_mark_node;
8294 }
8295 else if (require_constant_elements)
8337d1db 8296 pedwarn (loc, OPT_Wpedantic,
509c9d60 8297 "initializer element is not computable at load time");
8b6a5902 8298 }
928c19bb
JM
8299 else if (!maybe_const
8300 && (require_constant_value || require_constant_elements))
8337d1db 8301 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 8302 "initializer element is not a constant expression");
3e4093b6 8303
81f40b79
ILT
8304 /* Issue -Wc++-compat warnings about initializing a bitfield with
8305 enum type. */
8306 if (warn_cxx_compat
8307 && field != NULL_TREE
8308 && TREE_CODE (field) == FIELD_DECL
8309 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8310 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8311 != TYPE_MAIN_VARIANT (type))
8312 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8313 {
8314 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8315 if (checktype != error_mark_node
8316 && (TYPE_MAIN_VARIANT (checktype)
8317 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 8318 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
8319 "enum conversion in initialization is invalid in C++");
8320 }
8321
3e4093b6
RS
8322 /* If this field is empty (and not at the end of structure),
8323 don't do anything other than checking the initializer. */
8324 if (field
8325 && (TREE_TYPE (field) == error_mark_node
8326 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8327 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8328 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 8329 || DECL_CHAIN (field)))))
3e4093b6
RS
8330 return;
8331
8ce94e44
JM
8332 if (semantic_type)
8333 value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
34cf811f
MP
8334 value = digest_init (loc, type, value, origtype, npc, strict_string,
8335 require_constant_value);
3e4093b6 8336 if (value == error_mark_node)
8b6a5902 8337 {
3e4093b6
RS
8338 constructor_erroneous = 1;
8339 return;
8b6a5902 8340 }
928c19bb
JM
8341 if (require_constant_value || require_constant_elements)
8342 constant_expression_warning (value);
8b6a5902 8343
3e4093b6
RS
8344 /* If this element doesn't come next in sequence,
8345 put it on constructor_pending_elts. */
8346 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8347 && (!constructor_incremental
8348 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 8349 {
3e4093b6
RS
8350 if (constructor_incremental
8351 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 8352 set_nonincremental_init (braced_init_obstack);
3e4093b6 8353
96b40f8d 8354 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8355 braced_init_obstack);
3e4093b6 8356 return;
8b6a5902 8357 }
3e4093b6
RS
8358 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8359 && (!constructor_incremental
8360 || field != constructor_unfilled_fields))
8b6a5902 8361 {
3e4093b6
RS
8362 /* We do this for records but not for unions. In a union,
8363 no matter which field is specified, it can be initialized
8364 right away since it starts at the beginning of the union. */
8365 if (constructor_incremental)
8366 {
8367 if (!constructor_unfilled_fields)
a1e3b3d9 8368 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8369 else
8370 {
8371 tree bitpos, unfillpos;
8372
8373 bitpos = bit_position (field);
8374 unfillpos = bit_position (constructor_unfilled_fields);
8375
8376 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 8377 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
8378 }
8379 }
8380
96b40f8d 8381 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 8382 braced_init_obstack);
3e4093b6 8383 return;
8b6a5902 8384 }
3e4093b6 8385 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 8386 && !vec_safe_is_empty (constructor_elements))
3e4093b6 8387 {
b295aee2
JJ
8388 if (!implicit)
8389 {
9771b263 8390 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
96b40f8d 8391 warning_init (loc, 0,
b295aee2
JJ
8392 "initialized field with side-effects overwritten");
8393 else if (warn_override_init)
96b40f8d
MP
8394 warning_init (loc, OPT_Woverride_init,
8395 "initialized field overwritten");
b295aee2 8396 }
8b6a5902 8397
3e4093b6 8398 /* We can have just one union field set. */
9771b263 8399 constructor_elements = NULL;
3e4093b6 8400 }
8b6a5902 8401
3e4093b6
RS
8402 /* Otherwise, output this element either to
8403 constructor_elements or to the assembler file. */
8b6a5902 8404
f32682ca 8405 constructor_elt celt = {field, value};
9771b263 8406 vec_safe_push (constructor_elements, celt);
8b6a5902 8407
3e4093b6
RS
8408 /* Advance the variable that indicates sequential elements output. */
8409 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8410 constructor_unfilled_index
db3927fb
AH
8411 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8412 bitsize_one_node);
3e4093b6
RS
8413 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8414 {
8415 constructor_unfilled_fields
910ad8de 8416 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 8417
3e4093b6
RS
8418 /* Skip any nameless bit fields. */
8419 while (constructor_unfilled_fields != 0
8420 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8421 && DECL_NAME (constructor_unfilled_fields) == 0)
8422 constructor_unfilled_fields =
910ad8de 8423 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
8424 }
8425 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8426 constructor_unfilled_fields = 0;
de520661 8427
3e4093b6
RS
8428 /* Now output any pending elements which have become next. */
8429 if (pending)
a1e3b3d9 8430 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 8431}
8b6a5902 8432
3e4093b6
RS
8433/* Output any pending elements which have become next.
8434 As we output elements, constructor_unfilled_{fields,index}
8435 advances, which may cause other elements to become next;
8436 if so, they too are output.
8b6a5902 8437
3e4093b6
RS
8438 If ALL is 0, we return when there are
8439 no more pending elements to output now.
665f2503 8440
3e4093b6
RS
8441 If ALL is 1, we output space as necessary so that
8442 we can output all the pending elements. */
3e4093b6 8443static void
a1e3b3d9 8444output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
8445{
8446 struct init_node *elt = constructor_pending_elts;
8447 tree next;
de520661 8448
3e4093b6
RS
8449 retry:
8450
ba228239 8451 /* Look through the whole pending tree.
3e4093b6
RS
8452 If we find an element that should be output now,
8453 output it. Otherwise, set NEXT to the element
8454 that comes first among those still pending. */
8455
8456 next = 0;
8457 while (elt)
8458 {
8459 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 8460 {
3e4093b6
RS
8461 if (tree_int_cst_equal (elt->purpose,
8462 constructor_unfilled_index))
34cf811f
MP
8463 output_init_element (input_location, elt->value, elt->origtype,
8464 true, TREE_TYPE (constructor_type),
a1e3b3d9
LB
8465 constructor_unfilled_index, 0, false,
8466 braced_init_obstack);
3e4093b6
RS
8467 else if (tree_int_cst_lt (constructor_unfilled_index,
8468 elt->purpose))
8b6a5902 8469 {
3e4093b6
RS
8470 /* Advance to the next smaller node. */
8471 if (elt->left)
8472 elt = elt->left;
8473 else
8474 {
8475 /* We have reached the smallest node bigger than the
8476 current unfilled index. Fill the space first. */
8477 next = elt->purpose;
8478 break;
8479 }
8b6a5902 8480 }
ce662d4c
JJ
8481 else
8482 {
3e4093b6
RS
8483 /* Advance to the next bigger node. */
8484 if (elt->right)
8485 elt = elt->right;
8486 else
ce662d4c 8487 {
3e4093b6
RS
8488 /* We have reached the biggest node in a subtree. Find
8489 the parent of it, which is the next bigger node. */
8490 while (elt->parent && elt->parent->right == elt)
8491 elt = elt->parent;
8492 elt = elt->parent;
8493 if (elt && tree_int_cst_lt (constructor_unfilled_index,
8494 elt->purpose))
8495 {
8496 next = elt->purpose;
8497 break;
8498 }
ce662d4c
JJ
8499 }
8500 }
8b6a5902 8501 }
3e4093b6
RS
8502 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8503 || TREE_CODE (constructor_type) == UNION_TYPE)
8504 {
8505 tree ctor_unfilled_bitpos, elt_bitpos;
ce662d4c 8506
3e4093b6
RS
8507 /* If the current record is complete we are done. */
8508 if (constructor_unfilled_fields == 0)
8509 break;
de520661 8510
3e4093b6
RS
8511 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
8512 elt_bitpos = bit_position (elt->purpose);
8513 /* We can't compare fields here because there might be empty
8514 fields in between. */
8515 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
8516 {
8517 constructor_unfilled_fields = elt->purpose;
34cf811f
MP
8518 output_init_element (input_location, elt->value, elt->origtype,
8519 true, TREE_TYPE (elt->purpose),
a1e3b3d9
LB
8520 elt->purpose, 0, false,
8521 braced_init_obstack);
3e4093b6
RS
8522 }
8523 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
8524 {
8525 /* Advance to the next smaller node. */
8526 if (elt->left)
8527 elt = elt->left;
8528 else
8529 {
8530 /* We have reached the smallest node bigger than the
8531 current unfilled field. Fill the space first. */
8532 next = elt->purpose;
8533 break;
8534 }
8535 }
8536 else
8537 {
8538 /* Advance to the next bigger node. */
8539 if (elt->right)
8540 elt = elt->right;
8541 else
8542 {
8543 /* We have reached the biggest node in a subtree. Find
8544 the parent of it, which is the next bigger node. */
8545 while (elt->parent && elt->parent->right == elt)
8546 elt = elt->parent;
8547 elt = elt->parent;
8548 if (elt
8549 && (tree_int_cst_lt (ctor_unfilled_bitpos,
8550 bit_position (elt->purpose))))
8551 {
8552 next = elt->purpose;
8553 break;
8554 }
8555 }
8556 }
8557 }
8558 }
de520661 8559
3e4093b6
RS
8560 /* Ordinarily return, but not if we want to output all
8561 and there are elements left. */
3f75a254 8562 if (!(all && next != 0))
e5cfb88f
RK
8563 return;
8564
3e4093b6
RS
8565 /* If it's not incremental, just skip over the gap, so that after
8566 jumping to retry we will output the next successive element. */
8567 if (TREE_CODE (constructor_type) == RECORD_TYPE
8568 || TREE_CODE (constructor_type) == UNION_TYPE)
8569 constructor_unfilled_fields = next;
8570 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8571 constructor_unfilled_index = next;
de520661 8572
3e4093b6
RS
8573 /* ELT now points to the node in the pending tree with the next
8574 initializer to output. */
8575 goto retry;
de520661
RS
8576}
8577\f
3e4093b6
RS
8578/* Add one non-braced element to the current constructor level.
8579 This adjusts the current position within the constructor's type.
8580 This may also start or terminate implicit levels
8581 to handle a partly-braced initializer.
e5e809f4 8582
3e4093b6 8583 Once this has found the correct level for the new element,
b295aee2
JJ
8584 it calls output_init_element.
8585
8586 IMPLICIT is true if value comes from pop_init_level (1),
8587 the new initializer has been merged with the existing one
8588 and thus no warnings should be emitted about overriding an
8589 existing initializer. */
3e4093b6
RS
8590
8591void
34cf811f 8592process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 8593 struct obstack * braced_init_obstack)
e5e809f4 8594{
916c5919
JM
8595 tree orig_value = value.value;
8596 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
8597 bool strict_string = value.original_code == STRING_CST;
340baef7 8598 bool was_designated = designator_depth != 0;
e5e809f4 8599
3e4093b6 8600 designator_depth = 0;
b06df647 8601 designator_erroneous = 0;
e5e809f4 8602
3e4093b6
RS
8603 /* Handle superfluous braces around string cst as in
8604 char x[] = {"foo"}; */
8605 if (string_flag
8606 && constructor_type
340baef7 8607 && !was_designated
3e4093b6 8608 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 8609 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 8610 && integer_zerop (constructor_unfilled_index))
e5e809f4 8611 {
916c5919 8612 if (constructor_stack->replacement_value.value)
ea58ef42 8613 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
8614 constructor_stack->replacement_value = value;
8615 return;
e5e809f4 8616 }
8b6a5902 8617
916c5919 8618 if (constructor_stack->replacement_value.value != 0)
3e4093b6 8619 {
ea58ef42 8620 error_init (loc, "excess elements in struct initializer");
3e4093b6 8621 return;
e5e809f4
JL
8622 }
8623
3e4093b6
RS
8624 /* Ignore elements of a brace group if it is entirely superfluous
8625 and has already been diagnosed. */
8626 if (constructor_type == 0)
8627 return;
e5e809f4 8628
3e4093b6
RS
8629 /* If we've exhausted any levels that didn't have braces,
8630 pop them now. */
8631 while (constructor_stack->implicit)
8632 {
8633 if ((TREE_CODE (constructor_type) == RECORD_TYPE
8634 || TREE_CODE (constructor_type) == UNION_TYPE)
8635 && constructor_fields == 0)
ea58ef42
MP
8636 process_init_element (loc,
8637 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8638 true, braced_init_obstack);
53650abe
AP
8639 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
8640 || TREE_CODE (constructor_type) == VECTOR_TYPE)
8824edff
JJ
8641 && constructor_max_index
8642 && tree_int_cst_lt (constructor_max_index,
8643 constructor_index))
ea58ef42
MP
8644 process_init_element (loc,
8645 pop_init_level (loc, 1, braced_init_obstack),
a1e3b3d9 8646 true, braced_init_obstack);
3e4093b6
RS
8647 else
8648 break;
8649 }
e5e809f4 8650
3e4093b6
RS
8651 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
8652 if (constructor_range_stack)
e5e809f4 8653 {
3e4093b6
RS
8654 /* If value is a compound literal and we'll be just using its
8655 content, don't put it into a SAVE_EXPR. */
916c5919 8656 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
3e4093b6
RS
8657 || !require_constant_value
8658 || flag_isoc99)
8ce94e44
JM
8659 {
8660 tree semantic_type = NULL_TREE;
8661 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
8662 {
8663 semantic_type = TREE_TYPE (value.value);
8664 value.value = TREE_OPERAND (value.value, 0);
8665 }
8666 value.value = c_save_expr (value.value);
8667 if (semantic_type)
8668 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
8669 value.value);
8670 }
3e4093b6 8671 }
e5e809f4 8672
3e4093b6
RS
8673 while (1)
8674 {
8675 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 8676 {
3e4093b6
RS
8677 tree fieldtype;
8678 enum tree_code fieldcode;
e5e809f4 8679
3e4093b6
RS
8680 if (constructor_fields == 0)
8681 {
ea58ef42 8682 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
8683 break;
8684 }
e5e809f4 8685
3e4093b6
RS
8686 fieldtype = TREE_TYPE (constructor_fields);
8687 if (fieldtype != error_mark_node)
8688 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8689 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8690
3e4093b6
RS
8691 /* Error for non-static initialization of a flexible array member. */
8692 if (fieldcode == ARRAY_TYPE
8693 && !require_constant_value
8694 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 8695 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 8696 {
ea58ef42
MP
8697 error_init (loc, "non-static initialization of a flexible "
8698 "array member");
3e4093b6
RS
8699 break;
8700 }
e5e809f4 8701
3e4093b6 8702 /* Accept a string constant to initialize a subarray. */
916c5919 8703 if (value.value != 0
3e4093b6 8704 && fieldcode == ARRAY_TYPE
197463ae 8705 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8706 && string_flag)
916c5919 8707 value.value = orig_value;
3e4093b6
RS
8708 /* Otherwise, if we have come to a subaggregate,
8709 and we don't have an element of its type, push into it. */
0953878d 8710 else if (value.value != 0
916c5919
JM
8711 && value.value != error_mark_node
8712 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8713 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8714 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8715 {
ea58ef42 8716 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8717 continue;
8718 }
e5e809f4 8719
916c5919 8720 if (value.value)
3e4093b6
RS
8721 {
8722 push_member_name (constructor_fields);
34cf811f 8723 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8724 strict_string, fieldtype,
a1e3b3d9
LB
8725 constructor_fields, 1, implicit,
8726 braced_init_obstack);
3e4093b6 8727 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8728 }
8729 else
3e4093b6
RS
8730 /* Do the bookkeeping for an element that was
8731 directly output as a constructor. */
e5e809f4 8732 {
3e4093b6
RS
8733 /* For a record, keep track of end position of last field. */
8734 if (DECL_SIZE (constructor_fields))
c22cacf3 8735 constructor_bit_index
db3927fb
AH
8736 = size_binop_loc (input_location, PLUS_EXPR,
8737 bit_position (constructor_fields),
8738 DECL_SIZE (constructor_fields));
3e4093b6
RS
8739
8740 /* If the current field was the first one not yet written out,
8741 it isn't now, so update. */
8742 if (constructor_unfilled_fields == constructor_fields)
8743 {
910ad8de 8744 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8745 /* Skip any nameless bit fields. */
8746 while (constructor_unfilled_fields != 0
8747 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8748 && DECL_NAME (constructor_unfilled_fields) == 0)
8749 constructor_unfilled_fields =
910ad8de 8750 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 8751 }
e5e809f4 8752 }
3e4093b6 8753
910ad8de 8754 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
8755 /* Skip any nameless bit fields at the beginning. */
8756 while (constructor_fields != 0
8757 && DECL_C_BIT_FIELD (constructor_fields)
8758 && DECL_NAME (constructor_fields) == 0)
910ad8de 8759 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8760 }
3e4093b6 8761 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 8762 {
3e4093b6
RS
8763 tree fieldtype;
8764 enum tree_code fieldcode;
e5e809f4 8765
3e4093b6
RS
8766 if (constructor_fields == 0)
8767 {
d033409e 8768 pedwarn_init (loc, 0,
509c9d60 8769 "excess elements in union initializer");
3e4093b6
RS
8770 break;
8771 }
e5e809f4 8772
3e4093b6
RS
8773 fieldtype = TREE_TYPE (constructor_fields);
8774 if (fieldtype != error_mark_node)
8775 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
8776 fieldcode = TREE_CODE (fieldtype);
e5e809f4 8777
3e4093b6
RS
8778 /* Warn that traditional C rejects initialization of unions.
8779 We skip the warning if the value is zero. This is done
8780 under the assumption that the zero initializer in user
8781 code appears conditioned on e.g. __STDC__ to avoid
8782 "missing initializer" warnings and relies on default
8783 initialization to zero in the traditional C case.
8784 We also skip the warning if the initializer is designated,
8785 again on the assumption that this must be conditional on
8786 __STDC__ anyway (and we've already complained about the
8787 member-designator already). */
8400e75e 8788 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
8789 && !(value.value && (integer_zerop (value.value)
8790 || real_zerop (value.value))))
3176a0c2
DD
8791 warning (OPT_Wtraditional, "traditional C rejects initialization "
8792 "of unions");
e5e809f4 8793
3e4093b6 8794 /* Accept a string constant to initialize a subarray. */
916c5919 8795 if (value.value != 0
3e4093b6 8796 && fieldcode == ARRAY_TYPE
197463ae 8797 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 8798 && string_flag)
916c5919 8799 value.value = orig_value;
3e4093b6
RS
8800 /* Otherwise, if we have come to a subaggregate,
8801 and we don't have an element of its type, push into it. */
0953878d 8802 else if (value.value != 0
916c5919
JM
8803 && value.value != error_mark_node
8804 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 8805 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 8806 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 8807 {
ea58ef42 8808 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8809 continue;
8810 }
e5e809f4 8811
916c5919 8812 if (value.value)
3e4093b6
RS
8813 {
8814 push_member_name (constructor_fields);
34cf811f 8815 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8816 strict_string, fieldtype,
a1e3b3d9
LB
8817 constructor_fields, 1, implicit,
8818 braced_init_obstack);
3e4093b6 8819 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
8820 }
8821 else
3e4093b6
RS
8822 /* Do the bookkeeping for an element that was
8823 directly output as a constructor. */
e5e809f4 8824 {
3e4093b6 8825 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 8826 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 8827 }
e5e809f4 8828
3e4093b6
RS
8829 constructor_fields = 0;
8830 }
8831 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8832 {
8833 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8834 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 8835
3e4093b6 8836 /* Accept a string constant to initialize a subarray. */
916c5919 8837 if (value.value != 0
3e4093b6 8838 && eltcode == ARRAY_TYPE
197463ae 8839 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 8840 && string_flag)
916c5919 8841 value.value = orig_value;
3e4093b6
RS
8842 /* Otherwise, if we have come to a subaggregate,
8843 and we don't have an element of its type, push into it. */
0953878d 8844 else if (value.value != 0
916c5919
JM
8845 && value.value != error_mark_node
8846 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 8847 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 8848 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 8849 {
ea58ef42 8850 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
8851 continue;
8852 }
8b6a5902 8853
3e4093b6
RS
8854 if (constructor_max_index != 0
8855 && (tree_int_cst_lt (constructor_max_index, constructor_index)
8856 || integer_all_onesp (constructor_max_index)))
8857 {
d033409e 8858 pedwarn_init (loc, 0,
509c9d60 8859 "excess elements in array initializer");
3e4093b6
RS
8860 break;
8861 }
8b6a5902 8862
3e4093b6 8863 /* Now output the actual element. */
916c5919 8864 if (value.value)
3e4093b6 8865 {
ae7e9ddd 8866 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 8867 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8868 strict_string, elttype,
a1e3b3d9
LB
8869 constructor_index, 1, implicit,
8870 braced_init_obstack);
3e4093b6
RS
8871 RESTORE_SPELLING_DEPTH (constructor_depth);
8872 }
2f6e4e97 8873
3e4093b6 8874 constructor_index
db3927fb
AH
8875 = size_binop_loc (input_location, PLUS_EXPR,
8876 constructor_index, bitsize_one_node);
8b6a5902 8877
916c5919 8878 if (!value.value)
3e4093b6
RS
8879 /* If we are doing the bookkeeping for an element that was
8880 directly output as a constructor, we must update
8881 constructor_unfilled_index. */
8882 constructor_unfilled_index = constructor_index;
8883 }
8884 else if (TREE_CODE (constructor_type) == VECTOR_TYPE)
8885 {
8886 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 8887
c22cacf3
MS
8888 /* Do a basic check of initializer size. Note that vectors
8889 always have a fixed size derived from their type. */
3e4093b6
RS
8890 if (tree_int_cst_lt (constructor_max_index, constructor_index))
8891 {
d033409e 8892 pedwarn_init (loc, 0,
509c9d60 8893 "excess elements in vector initializer");
3e4093b6
RS
8894 break;
8895 }
8b6a5902 8896
3e4093b6 8897 /* Now output the actual element. */
916c5919 8898 if (value.value)
53650abe
AP
8899 {
8900 if (TREE_CODE (value.value) == VECTOR_CST)
8901 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 8902 output_init_element (loc, value.value, value.original_type,
53650abe 8903 strict_string, elttype,
a1e3b3d9
LB
8904 constructor_index, 1, implicit,
8905 braced_init_obstack);
53650abe 8906 }
8b6a5902 8907
3e4093b6 8908 constructor_index
db3927fb
AH
8909 = size_binop_loc (input_location,
8910 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 8911
916c5919 8912 if (!value.value)
3e4093b6
RS
8913 /* If we are doing the bookkeeping for an element that was
8914 directly output as a constructor, we must update
8915 constructor_unfilled_index. */
8916 constructor_unfilled_index = constructor_index;
8917 }
8b6a5902 8918
3e4093b6
RS
8919 /* Handle the sole element allowed in a braced initializer
8920 for a scalar variable. */
b4519d39
SB
8921 else if (constructor_type != error_mark_node
8922 && constructor_fields == 0)
8b6a5902 8923 {
d033409e 8924 pedwarn_init (loc, 0,
509c9d60 8925 "excess elements in scalar initializer");
3e4093b6 8926 break;
8b6a5902
JJ
8927 }
8928 else
8929 {
916c5919 8930 if (value.value)
34cf811f 8931 output_init_element (loc, value.value, value.original_type,
bbbbb16a 8932 strict_string, constructor_type,
a1e3b3d9
LB
8933 NULL_TREE, 1, implicit,
8934 braced_init_obstack);
3e4093b6 8935 constructor_fields = 0;
8b6a5902
JJ
8936 }
8937
3e4093b6
RS
8938 /* Handle range initializers either at this level or anywhere higher
8939 in the designator stack. */
8940 if (constructor_range_stack)
8b6a5902 8941 {
3e4093b6
RS
8942 struct constructor_range_stack *p, *range_stack;
8943 int finish = 0;
8944
8945 range_stack = constructor_range_stack;
8946 constructor_range_stack = 0;
8947 while (constructor_stack != range_stack->stack)
8b6a5902 8948 {
366de0ce 8949 gcc_assert (constructor_stack->implicit);
34cf811f 8950 process_init_element (loc,
ea58ef42
MP
8951 pop_init_level (loc, 1,
8952 braced_init_obstack),
a1e3b3d9 8953 true, braced_init_obstack);
8b6a5902 8954 }
3e4093b6
RS
8955 for (p = range_stack;
8956 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
8957 p = p->prev)
8b6a5902 8958 {
366de0ce 8959 gcc_assert (constructor_stack->implicit);
34cf811f 8960 process_init_element (loc,
ea58ef42
MP
8961 pop_init_level (loc, 1,
8962 braced_init_obstack),
a1e3b3d9 8963 true, braced_init_obstack);
8b6a5902 8964 }
3e4093b6 8965
db3927fb
AH
8966 p->index = size_binop_loc (input_location,
8967 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
8968 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
8969 finish = 1;
8970
8971 while (1)
8972 {
8973 constructor_index = p->index;
8974 constructor_fields = p->fields;
8975 if (finish && p->range_end && p->index == p->range_start)
8976 {
8977 finish = 0;
8978 p->prev = 0;
8979 }
8980 p = p->next;
8981 if (!p)
8982 break;
ea58ef42 8983 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
8984 p->stack = constructor_stack;
8985 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
8986 p->index = p->range_start;
8987 }
8988
8989 if (!finish)
8990 constructor_range_stack = range_stack;
8991 continue;
8b6a5902
JJ
8992 }
8993
3e4093b6 8994 break;
8b6a5902
JJ
8995 }
8996
3e4093b6
RS
8997 constructor_range_stack = 0;
8998}
8999\f
9f0e2d86
ZW
9000/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9001 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 9002 an ASM_EXPR node). */
3e4093b6 9003tree
9f0e2d86 9004build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 9005{
6de9cd9a
DN
9006 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9007 ASM_VOLATILE_P (args) = 1;
9f0e2d86 9008 return add_stmt (args);
8b6a5902
JJ
9009}
9010
9f0e2d86
ZW
9011/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9012 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9013 SIMPLE indicates whether there was anything at all after the
9014 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 9015 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 9016tree
c2255bc4 9017build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 9018 tree clobbers, tree labels, bool simple)
e5e809f4 9019{
3e4093b6 9020 tree tail;
9f0e2d86 9021 tree args;
6de9cd9a
DN
9022 int i;
9023 const char *constraint;
74f0c611 9024 const char **oconstraints;
6de9cd9a 9025 bool allows_mem, allows_reg, is_inout;
74f0c611 9026 int ninputs, noutputs;
6de9cd9a
DN
9027
9028 ninputs = list_length (inputs);
9029 noutputs = list_length (outputs);
74f0c611
RH
9030 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9031
1c384bf1 9032 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 9033
6de9cd9a
DN
9034 /* Remove output conversions that change the type but not the mode. */
9035 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 9036 {
3e4093b6 9037 tree output = TREE_VALUE (tail);
74f0c611 9038
eadd3d0d
JJ
9039 output = c_fully_fold (output, false, NULL);
9040
74f0c611
RH
9041 /* ??? Really, this should not be here. Users should be using a
9042 proper lvalue, dammit. But there's a long history of using casts
9043 in the output operands. In cases like longlong.h, this becomes a
9044 primitive form of typechecking -- if the cast can be removed, then
9045 the output operand had a type of the proper width; otherwise we'll
9046 get an error. Gross, but ... */
3e4093b6 9047 STRIP_NOPS (output);
74f0c611 9048
7bd11157 9049 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 9050 output = error_mark_node;
8b6a5902 9051
5544530a
PB
9052 if (output != error_mark_node
9053 && (TREE_READONLY (output)
9054 || TYPE_READONLY (TREE_TYPE (output))
9055 || ((TREE_CODE (TREE_TYPE (output)) == RECORD_TYPE
9056 || TREE_CODE (TREE_TYPE (output)) == UNION_TYPE)
9057 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 9058 readonly_error (loc, output, lv_asm);
5544530a 9059
6de9cd9a 9060 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
9061 oconstraints[i] = constraint;
9062
9063 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9064 &allows_mem, &allows_reg, &is_inout))
9065 {
9066 /* If the operand is going to end up in memory,
9067 mark it addressable. */
9068 if (!allows_reg && !c_mark_addressable (output))
9069 output = error_mark_node;
bae5cddf
JJ
9070 if (!(!allows_reg && allows_mem)
9071 && output != error_mark_node
9072 && VOID_TYPE_P (TREE_TYPE (output)))
9073 {
9074 error_at (loc, "invalid use of void expression");
9075 output = error_mark_node;
9076 }
74f0c611
RH
9077 }
9078 else
c22cacf3 9079 output = error_mark_node;
3e4093b6 9080
74f0c611 9081 TREE_VALUE (tail) = output;
8b6a5902 9082 }
3e4093b6 9083
74f0c611
RH
9084 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9085 {
9086 tree input;
9087
9088 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9089 input = TREE_VALUE (tail);
9090
74f0c611
RH
9091 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9092 oconstraints, &allows_mem, &allows_reg))
9093 {
9094 /* If the operand is going to end up in memory,
9095 mark it addressable. */
b4c33883
AP
9096 if (!allows_reg && allows_mem)
9097 {
eadd3d0d
JJ
9098 input = c_fully_fold (input, false, NULL);
9099
b4c33883
AP
9100 /* Strip the nops as we allow this case. FIXME, this really
9101 should be rejected or made deprecated. */
9102 STRIP_NOPS (input);
9103 if (!c_mark_addressable (input))
9104 input = error_mark_node;
bae5cddf 9105 }
eadd3d0d 9106 else
bae5cddf 9107 {
eadd3d0d
JJ
9108 struct c_expr expr;
9109 memset (&expr, 0, sizeof (expr));
9110 expr.value = input;
267bac10 9111 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
9112 input = c_fully_fold (expr.value, false, NULL);
9113
9114 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9115 {
9116 error_at (loc, "invalid use of void expression");
9117 input = error_mark_node;
9118 }
bae5cddf 9119 }
74f0c611
RH
9120 }
9121 else
9122 input = error_mark_node;
9123
9124 TREE_VALUE (tail) = input;
9125 }
3e4093b6 9126
1c384bf1
RH
9127 /* ASMs with labels cannot have outputs. This should have been
9128 enforced by the parser. */
9129 gcc_assert (outputs == NULL || labels == NULL);
9130
9131 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 9132
5544530a
PB
9133 /* asm statements without outputs, including simple ones, are treated
9134 as volatile. */
9135 ASM_INPUT_P (args) = simple;
9136 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 9137
9f0e2d86 9138 return args;
e5e809f4 9139}
3e4093b6 9140\f
c2255bc4
AH
9141/* Generate a goto statement to LABEL. LOC is the location of the
9142 GOTO. */
506e2710
RH
9143
9144tree
c2255bc4 9145c_finish_goto_label (location_t loc, tree label)
506e2710 9146{
e1b7793c 9147 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
9148 if (!decl)
9149 return NULL_TREE;
506e2710 9150 TREE_USED (decl) = 1;
c2255bc4
AH
9151 {
9152 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9153 SET_EXPR_LOCATION (t, loc);
9154 return add_stmt (t);
9155 }
506e2710
RH
9156}
9157
c2255bc4
AH
9158/* Generate a computed goto statement to EXPR. LOC is the location of
9159 the GOTO. */
506e2710
RH
9160
9161tree
c2255bc4 9162c_finish_goto_ptr (location_t loc, tree expr)
506e2710 9163{
c2255bc4 9164 tree t;
c1771a20 9165 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 9166 expr = c_fully_fold (expr, false, NULL);
506e2710 9167 expr = convert (ptr_type_node, expr);
c2255bc4
AH
9168 t = build1 (GOTO_EXPR, void_type_node, expr);
9169 SET_EXPR_LOCATION (t, loc);
9170 return add_stmt (t);
506e2710
RH
9171}
9172
5088b058 9173/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4
AH
9174 to return, or a null pointer for `return;' with no value. LOC is
9175 the location of the return statement. If ORIGTYPE is not NULL_TREE, it
9176 is the original type of RETVAL. */
de520661 9177
506e2710 9178tree
c2255bc4 9179c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 9180{
0c9b182b
JJ
9181 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9182 bool no_warning = false;
928c19bb 9183 bool npc = false;
36536d79 9184 size_t rank = 0;
3e4093b6
RS
9185
9186 if (TREE_THIS_VOLATILE (current_function_decl))
c2255bc4
AH
9187 warning_at (loc, 0,
9188 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 9189
b72271b9 9190 if (flag_cilkplus && contains_array_notation_expr (retval))
36536d79
BI
9191 {
9192 /* Array notations are allowed in a return statement if it is inside a
9193 built-in array notation reduction function. */
9194 if (!find_rank (loc, retval, retval, false, &rank))
9195 return error_mark_node;
9196 if (rank >= 1)
9197 {
9198 error_at (loc, "array notation expression cannot be used as a "
9199 "return value");
9200 return error_mark_node;
9201 }
9202 }
3af9c5e9 9203 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
939b37da
BI
9204 {
9205 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9206 "allowed");
9207 return error_mark_node;
9208 }
928c19bb
JM
9209 if (retval)
9210 {
8ce94e44 9211 tree semantic_type = NULL_TREE;
928c19bb 9212 npc = null_pointer_constant_p (retval);
8ce94e44
JM
9213 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9214 {
9215 semantic_type = TREE_TYPE (retval);
9216 retval = TREE_OPERAND (retval, 0);
9217 }
928c19bb 9218 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
9219 if (semantic_type)
9220 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
9221 }
9222
3e4093b6 9223 if (!retval)
de520661 9224 {
3e4093b6
RS
9225 current_function_returns_null = 1;
9226 if ((warn_return_type || flag_isoc99)
9227 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 9228 {
b8698a0f 9229 pedwarn_c99 (loc, flag_isoc99 ? 0 : OPT_Wreturn_type,
fcf73884 9230 "%<return%> with no value, in "
0c9b182b
JJ
9231 "function returning non-void");
9232 no_warning = true;
9233 }
400fbf9f 9234 }
3e4093b6 9235 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
de520661 9236 {
3e4093b6 9237 current_function_returns_null = 1;
2397c575 9238 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b8698a0f 9239 pedwarn (loc, 0,
509c9d60 9240 "%<return%> with a value, in function returning void");
b8698a0f 9241 else
c1771a20 9242 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 9243 "%<return%> with expression, in function returning void");
de520661 9244 }
3e4093b6 9245 else
de520661 9246 {
68fca595
MP
9247 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9248 retval, origtype, ic_return,
c2255bc4 9249 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
9250 tree res = DECL_RESULT (current_function_decl);
9251 tree inner;
9feb29df 9252 bool save;
3e4093b6
RS
9253
9254 current_function_returns_value = 1;
9255 if (t == error_mark_node)
506e2710 9256 return NULL_TREE;
3e4093b6 9257
9feb29df
JJ
9258 save = in_late_binary_op;
9259 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
9260 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE)
9261 in_late_binary_op = true;
3e4093b6 9262 inner = t = convert (TREE_TYPE (res), t);
9feb29df 9263 in_late_binary_op = save;
3e4093b6
RS
9264
9265 /* Strip any conversions, additions, and subtractions, and see if
9266 we are returning the address of a local variable. Warn if so. */
9267 while (1)
8b6a5902 9268 {
3e4093b6 9269 switch (TREE_CODE (inner))
8b6a5902 9270 {
849421a3
JJ
9271 CASE_CONVERT:
9272 case NON_LVALUE_EXPR:
3e4093b6 9273 case PLUS_EXPR:
849421a3 9274 case POINTER_PLUS_EXPR:
3e4093b6
RS
9275 inner = TREE_OPERAND (inner, 0);
9276 continue;
9277
9278 case MINUS_EXPR:
9279 /* If the second operand of the MINUS_EXPR has a pointer
9280 type (or is converted from it), this may be valid, so
9281 don't give a warning. */
9282 {
9283 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 9284
3f75a254 9285 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
9286 && (CONVERT_EXPR_P (op1)
9287 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 9288 op1 = TREE_OPERAND (op1, 0);
8b6a5902 9289
3e4093b6
RS
9290 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9291 break;
8b6a5902 9292
3e4093b6
RS
9293 inner = TREE_OPERAND (inner, 0);
9294 continue;
9295 }
400fbf9f 9296
3e4093b6
RS
9297 case ADDR_EXPR:
9298 inner = TREE_OPERAND (inner, 0);
c2f4acb7 9299
6615c446 9300 while (REFERENCE_CLASS_P (inner)
c22cacf3 9301 && TREE_CODE (inner) != INDIRECT_REF)
3e4093b6 9302 inner = TREE_OPERAND (inner, 0);
8b6a5902 9303
a2f1f4c3 9304 if (DECL_P (inner)
3f75a254
JM
9305 && !DECL_EXTERNAL (inner)
9306 && !TREE_STATIC (inner)
3e4093b6 9307 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
9308 {
9309 if (TREE_CODE (inner) == LABEL_DECL)
9310 warning_at (loc, OPT_Wreturn_local_addr,
9311 "function returns address of label");
9312 else
9313 warning_at (loc, OPT_Wreturn_local_addr,
9314 "function returns address of local variable");
9315 }
3e4093b6 9316 break;
8b6a5902 9317
3e4093b6
RS
9318 default:
9319 break;
9320 }
de520661 9321
3e4093b6
RS
9322 break;
9323 }
9324
53fb4de3 9325 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 9326 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
9327
9328 if (warn_sequence_point)
9329 verify_sequence_points (retval);
de520661 9330 }
8b6a5902 9331
c2255bc4 9332 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
9333 TREE_NO_WARNING (ret_stmt) |= no_warning;
9334 return add_stmt (ret_stmt);
de520661 9335}
3e4093b6
RS
9336\f
9337struct c_switch {
604f5adf
ILT
9338 /* The SWITCH_EXPR being built. */
9339 tree switch_expr;
a6c0a76c 9340
89dbed81 9341 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
9342 default conversion is applied. */
9343 tree orig_type;
9344
3e4093b6
RS
9345 /* A splay-tree mapping the low element of a case range to the high
9346 element, or NULL_TREE if there is no high element. Used to
9347 determine whether or not a new case label duplicates an old case
9348 label. We need a tree, rather than simply a hash table, because
9349 of the GNU case range extension. */
9350 splay_tree cases;
a6c0a76c 9351
e1b7793c
ILT
9352 /* The bindings at the point of the switch. This is used for
9353 warnings crossing decls when branching to a case label. */
9354 struct c_spot_bindings *bindings;
187230a7 9355
3e4093b6
RS
9356 /* The next node on the stack. */
9357 struct c_switch *next;
9358};
400fbf9f 9359
3e4093b6
RS
9360/* A stack of the currently active switch statements. The innermost
9361 switch statement is on the top of the stack. There is no need to
9362 mark the stack for garbage collection because it is only active
9363 during the processing of the body of a function, and we never
9364 collect at that point. */
de520661 9365
506e2710 9366struct c_switch *c_switch_stack;
de520661 9367
3e4093b6 9368/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 9369 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef
MP
9370 SWITCH_COND_LOC is the location of the switch's condition.
9371 EXPLICIT_CAST_P is true if the expression EXP has explicit cast. */
de520661 9372
3e4093b6 9373tree
c2255bc4
AH
9374c_start_case (location_t switch_loc,
9375 location_t switch_cond_loc,
fedfecef 9376 tree exp, bool explicit_cast_p)
de520661 9377{
c58e8676 9378 tree orig_type = error_mark_node;
3e4093b6 9379 struct c_switch *cs;
2f6e4e97 9380
3e4093b6 9381 if (exp != error_mark_node)
de520661 9382 {
3e4093b6
RS
9383 orig_type = TREE_TYPE (exp);
9384
c58e8676 9385 if (!INTEGRAL_TYPE_P (orig_type))
de520661 9386 {
c58e8676
VR
9387 if (orig_type != error_mark_node)
9388 {
c2255bc4 9389 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
9390 orig_type = error_mark_node;
9391 }
3e4093b6 9392 exp = integer_zero_node;
de520661 9393 }
3e4093b6 9394 else
de520661 9395 {
c58e8676 9396 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
9397 tree e = exp;
9398
9399 /* Warn if the condition has boolean value. */
9400 while (TREE_CODE (e) == COMPOUND_EXPR)
9401 e = TREE_OPERAND (e, 1);
9402
9403 if ((TREE_CODE (type) == BOOLEAN_TYPE
9404 || truth_value_p (TREE_CODE (e)))
9405 /* Explicit cast to int suppresses this warning. */
9406 && !(TREE_CODE (type) == INTEGER_TYPE
9407 && explicit_cast_p))
9408 warning_at (switch_cond_loc, OPT_Wswitch_bool,
9409 "switch condition has boolean value");
8b6a5902 9410
8400e75e 9411 if (!in_system_header_at (input_location)
3e4093b6
RS
9412 && (type == long_integer_type_node
9413 || type == long_unsigned_type_node))
c2255bc4
AH
9414 warning_at (switch_cond_loc,
9415 OPT_Wtraditional, "%<long%> switch expression not "
9416 "converted to %<int%> in ISO C");
8b6a5902 9417
928c19bb 9418 exp = c_fully_fold (exp, false, NULL);
3e4093b6 9419 exp = default_conversion (exp);
ca085fd7
MLI
9420
9421 if (warn_sequence_point)
9422 verify_sequence_points (exp);
3e4093b6
RS
9423 }
9424 }
9425
604f5adf 9426 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 9427 cs = XNEW (struct c_switch);
604f5adf 9428 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
c2255bc4 9429 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 9430 cs->orig_type = orig_type;
3e4093b6 9431 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 9432 cs->bindings = c_get_switch_bindings ();
506e2710
RH
9433 cs->next = c_switch_stack;
9434 c_switch_stack = cs;
3e4093b6 9435
604f5adf 9436 return add_stmt (cs->switch_expr);
3e4093b6
RS
9437}
9438
c2255bc4 9439/* Process a case label at location LOC. */
3e4093b6
RS
9440
9441tree
c2255bc4 9442do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
9443{
9444 tree label = NULL_TREE;
9445
17cede2e
JM
9446 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
9447 {
9448 low_value = c_fully_fold (low_value, false, NULL);
9449 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 9450 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
9451 "case label is not an integer constant expression");
9452 }
9453
9454 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
9455 {
9456 high_value = c_fully_fold (high_value, false, NULL);
9457 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 9458 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
9459 "case label is not an integer constant expression");
9460 }
9461
e1b7793c 9462 if (c_switch_stack == NULL)
187230a7
JM
9463 {
9464 if (low_value)
e1b7793c 9465 error_at (loc, "case label not within a switch statement");
187230a7 9466 else
e1b7793c
ILT
9467 error_at (loc, "%<default%> label not within a switch statement");
9468 return NULL_TREE;
187230a7 9469 }
de520661 9470
e1b7793c
ILT
9471 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
9472 EXPR_LOCATION (c_switch_stack->switch_expr),
9473 loc))
9474 return NULL_TREE;
9475
9476 label = c_add_case_label (loc, c_switch_stack->cases,
9477 SWITCH_COND (c_switch_stack->switch_expr),
9478 c_switch_stack->orig_type,
9479 low_value, high_value);
9480 if (label == error_mark_node)
9481 label = NULL_TREE;
3e4093b6
RS
9482 return label;
9483}
de520661 9484
3e4093b6 9485/* Finish the switch statement. */
de520661 9486
3e4093b6 9487void
325c3691 9488c_finish_case (tree body)
3e4093b6 9489{
506e2710 9490 struct c_switch *cs = c_switch_stack;
fbc315db 9491 location_t switch_location;
3e4093b6 9492
604f5adf 9493 SWITCH_BODY (cs->switch_expr) = body;
325c3691 9494
6de9cd9a 9495 /* Emit warnings as needed. */
c2255bc4 9496 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db
ILT
9497 c_do_switch_warnings (cs->cases, switch_location,
9498 TREE_TYPE (cs->switch_expr),
9499 SWITCH_COND (cs->switch_expr));
6de9cd9a 9500
3e4093b6 9501 /* Pop the stack. */
506e2710 9502 c_switch_stack = cs->next;
3e4093b6 9503 splay_tree_delete (cs->cases);
e1b7793c 9504 c_release_switch_bindings (cs->bindings);
5d038c4c 9505 XDELETE (cs);
de520661 9506}
325c3691 9507\f
506e2710
RH
9508/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
9509 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
9510 may be null. NESTED_IF is true if THEN_BLOCK contains another IF
9511 statement, and was not surrounded with parenthesis. */
325c3691 9512
9e51cf9d 9513void
506e2710
RH
9514c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
9515 tree else_block, bool nested_if)
325c3691 9516{
506e2710 9517 tree stmt;
325c3691 9518
25c22937
BI
9519 /* If the condition has array notations, then the rank of the then_block and
9520 else_block must be either 0 or be equal to the rank of the condition. If
9521 the condition does not have array notations then break them up as it is
9522 broken up in a normal expression. */
b72271b9 9523 if (flag_cilkplus && contains_array_notation_expr (cond))
25c22937
BI
9524 {
9525 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
9526 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
9527 return;
9528 if (then_block
9529 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
9530 return;
9531 if (else_block
9532 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
9533 return;
9534 if (cond_rank != then_rank && then_rank != 0)
9535 {
9536 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9537 " and the then-block");
9538 return;
9539 }
9540 else if (cond_rank != else_rank && else_rank != 0)
9541 {
9542 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
9543 " and the else-block");
9544 return;
9545 }
9546 }
506e2710
RH
9547 /* Diagnose an ambiguous else if if-then-else is nested inside if-then. */
9548 if (warn_parentheses && nested_if && else_block == NULL)
325c3691 9549 {
506e2710 9550 tree inner_if = then_block;
16865eaa 9551
61ada8ae 9552 /* We know from the grammar productions that there is an IF nested
506e2710
RH
9553 within THEN_BLOCK. Due to labels and c99 conditional declarations,
9554 it might not be exactly THEN_BLOCK, but should be the last
9555 non-container statement within. */
9556 while (1)
9557 switch (TREE_CODE (inner_if))
9558 {
9559 case COND_EXPR:
9560 goto found;
9561 case BIND_EXPR:
9562 inner_if = BIND_EXPR_BODY (inner_if);
9563 break;
9564 case STATEMENT_LIST:
9565 inner_if = expr_last (then_block);
9566 break;
9567 case TRY_FINALLY_EXPR:
9568 case TRY_CATCH_EXPR:
9569 inner_if = TREE_OPERAND (inner_if, 0);
9570 break;
9571 default:
366de0ce 9572 gcc_unreachable ();
506e2710
RH
9573 }
9574 found:
16865eaa 9575
506e2710 9576 if (COND_EXPR_ELSE (inner_if))
fab922b1
MLI
9577 warning_at (if_locus, OPT_Wparentheses,
9578 "suggest explicit braces to avoid ambiguous %<else%>");
506e2710 9579 }
16865eaa 9580
2214de30 9581 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 9582 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 9583 add_stmt (stmt);
325c3691
RH
9584}
9585
506e2710
RH
9586/* Emit a general-purpose loop construct. START_LOCUS is the location of
9587 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
9588 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 9589 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 9590 the continue label. Everything is allowed to be NULL. */
325c3691
RH
9591
9592void
506e2710
RH
9593c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
9594 tree blab, tree clab, bool cond_is_first)
325c3691 9595{
506e2710
RH
9596 tree entry = NULL, exit = NULL, t;
9597
b72271b9 9598 if (flag_cilkplus && contains_array_notation_expr (cond))
36536d79
BI
9599 {
9600 error_at (start_locus, "array notation expression cannot be used in a "
9601 "loop%'s condition");
9602 return;
9603 }
9604
28af952a
RS
9605 /* If the condition is zero don't generate a loop construct. */
9606 if (cond && integer_zerop (cond))
9607 {
9608 if (cond_is_first)
9609 {
9610 t = build_and_jump (&blab);
9611 SET_EXPR_LOCATION (t, start_locus);
9612 add_stmt (t);
9613 }
9614 }
9615 else
506e2710
RH
9616 {
9617 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 9618
506e2710 9619 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
9620 out of the loop, or to the top of it. If there's no exit condition,
9621 then we just build a jump back to the top. */
506e2710 9622 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 9623
28af952a 9624 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
9625 {
9626 /* Canonicalize the loop condition to the end. This means
9627 generating a branch to the loop condition. Reuse the
9628 continue label, if possible. */
9629 if (cond_is_first)
9630 {
9631 if (incr || !clab)
9632 {
9633 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
9634 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
9635 }
9636 else
9637 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 9638 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
9639 add_stmt (t);
9640 }
9641
506e2710 9642 t = build_and_jump (&blab);
506e2710 9643 if (cond_is_first)
db3927fb
AH
9644 exit = fold_build3_loc (start_locus,
9645 COND_EXPR, void_type_node, cond, exit, t);
506e2710 9646 else
db3927fb
AH
9647 exit = fold_build3_loc (input_location,
9648 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3
MS
9649 }
9650
506e2710
RH
9651 add_stmt (top);
9652 }
c22cacf3 9653
506e2710
RH
9654 if (body)
9655 add_stmt (body);
9656 if (clab)
9657 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
9658 if (incr)
9659 add_stmt (incr);
9660 if (entry)
9661 add_stmt (entry);
9662 if (exit)
9663 add_stmt (exit);
9664 if (blab)
9665 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 9666}
325c3691
RH
9667
9668tree
c2255bc4 9669c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 9670{
089efaa4 9671 bool skip;
506e2710 9672 tree label = *label_p;
325c3691 9673
089efaa4
ILT
9674 /* In switch statements break is sometimes stylistically used after
9675 a return statement. This can lead to spurious warnings about
9676 control reaching the end of a non-void function when it is
9677 inlined. Note that we are calling block_may_fallthru with
9678 language specific tree nodes; this works because
9679 block_may_fallthru returns true when given something it does not
9680 understand. */
9681 skip = !block_may_fallthru (cur_stmt_list);
9682
506e2710 9683 if (!label)
089efaa4
ILT
9684 {
9685 if (!skip)
c2255bc4 9686 *label_p = label = create_artificial_label (loc);
089efaa4 9687 }
953ff289
DN
9688 else if (TREE_CODE (label) == LABEL_DECL)
9689 ;
9690 else switch (TREE_INT_CST_LOW (label))
506e2710 9691 {
953ff289 9692 case 0:
506e2710 9693 if (is_break)
c2255bc4 9694 error_at (loc, "break statement not within loop or switch");
506e2710 9695 else
c2255bc4 9696 error_at (loc, "continue statement not within a loop");
506e2710 9697 return NULL_TREE;
953ff289
DN
9698
9699 case 1:
9700 gcc_assert (is_break);
c2255bc4 9701 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
9702 return NULL_TREE;
9703
c02065fc
AH
9704 case 2:
9705 if (is_break)
9706 error ("break statement within %<#pragma simd%> loop body");
9707 else
9708 error ("continue statement within %<#pragma simd%> loop body");
9709 return NULL_TREE;
9710
953ff289
DN
9711 default:
9712 gcc_unreachable ();
506e2710 9713 }
325c3691 9714
089efaa4
ILT
9715 if (skip)
9716 return NULL_TREE;
9717
2e28e797
JH
9718 if (!is_break)
9719 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
9720
53fb4de3 9721 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
9722}
9723
506e2710 9724/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
9725
9726static void
c2255bc4 9727emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 9728{
e6b5a630
RH
9729 if (expr == error_mark_node)
9730 ;
9731 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
9732 {
9733 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 9734 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 9735 }
789eadcd
MP
9736 else if (TREE_CODE (expr) == COMPOUND_EXPR)
9737 {
9738 tree r = expr;
9739 location_t cloc = loc;
9740 while (TREE_CODE (r) == COMPOUND_EXPR)
9741 {
9742 if (EXPR_HAS_LOCATION (r))
9743 cloc = EXPR_LOCATION (r);
9744 r = TREE_OPERAND (r, 1);
9745 }
9746 if (!TREE_SIDE_EFFECTS (r)
9747 && !VOID_TYPE_P (TREE_TYPE (r))
9748 && !CONVERT_EXPR_P (r)
cc28fc7f 9749 && !TREE_NO_WARNING (r)
789eadcd
MP
9750 && !TREE_NO_WARNING (expr))
9751 warning_at (cloc, OPT_Wunused_value,
9752 "right-hand operand of comma expression has no effect");
9753 }
27f33b15 9754 else
c2255bc4 9755 warn_if_unused_value (expr, loc);
3a5b9284
RH
9756}
9757
506e2710 9758/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
9759 diagnostics, but do not call ADD_STMT. LOC is the location of the
9760 statement. */
3a5b9284 9761
506e2710 9762tree
c2255bc4 9763c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 9764{
056928b2
JJ
9765 tree exprv;
9766
3a5b9284 9767 if (!expr)
506e2710 9768 return NULL_TREE;
3a5b9284 9769
928c19bb
JM
9770 expr = c_fully_fold (expr, false, NULL);
9771
3a5b9284
RH
9772 if (warn_sequence_point)
9773 verify_sequence_points (expr);
9774
9775 if (TREE_TYPE (expr) != error_mark_node
9776 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
9777 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 9778 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
9779
9780 /* If we're not processing a statement expression, warn about unused values.
9781 Warnings for statement expressions will be emitted later, once we figure
9782 out which is the result. */
9783 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 9784 && warn_unused_value)
c2255bc4 9785 emit_side_effect_warnings (loc, expr);
3a5b9284 9786
056928b2
JJ
9787 exprv = expr;
9788 while (TREE_CODE (exprv) == COMPOUND_EXPR)
9789 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
9790 while (CONVERT_EXPR_P (exprv))
9791 exprv = TREE_OPERAND (exprv, 0);
9792 if (DECL_P (exprv)
9793 || handled_component_p (exprv)
9794 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 9795 mark_exp_read (exprv);
fa8351f8 9796
3a5b9284
RH
9797 /* If the expression is not of a type to which we cannot assign a line
9798 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 9799 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
9800 {
9801 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
9802 SET_EXPR_LOCATION (expr, loc);
9803 }
506e2710
RH
9804
9805 return expr;
9806}
9807
c2255bc4
AH
9808/* Emit an expression as a statement. LOC is the location of the
9809 expression. */
506e2710
RH
9810
9811tree
c2255bc4 9812c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
9813{
9814 if (expr)
c2255bc4 9815 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
9816 else
9817 return NULL;
3a5b9284
RH
9818}
9819
9820/* Do the opposite and emit a statement as an expression. To begin,
9821 create a new binding level and return it. */
325c3691
RH
9822
9823tree
9824c_begin_stmt_expr (void)
9825{
9826 tree ret;
9827
9828 /* We must force a BLOCK for this level so that, if it is not expanded
9829 later, there is a way to turn off the entire subtree of blocks that
9830 are contained in it. */
9831 keep_next_level ();
9832 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
9833
9834 c_bindings_start_stmt_expr (c_switch_stack == NULL
9835 ? NULL
9836 : c_switch_stack->bindings);
325c3691
RH
9837
9838 /* Mark the current statement list as belonging to a statement list. */
9839 STATEMENT_LIST_STMT_EXPR (ret) = 1;
9840
9841 return ret;
9842}
9843
c2255bc4
AH
9844/* LOC is the location of the compound statement to which this body
9845 belongs. */
9846
325c3691 9847tree
c2255bc4 9848c_finish_stmt_expr (location_t loc, tree body)
325c3691 9849{
3a5b9284 9850 tree last, type, tmp, val;
325c3691
RH
9851 tree *last_p;
9852
c2255bc4 9853 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
9854
9855 c_bindings_end_stmt_expr (c_switch_stack == NULL
9856 ? NULL
9857 : c_switch_stack->bindings);
325c3691 9858
3a5b9284
RH
9859 /* Locate the last statement in BODY. See c_end_compound_stmt
9860 about always returning a BIND_EXPR. */
9861 last_p = &BIND_EXPR_BODY (body);
9862 last = BIND_EXPR_BODY (body);
9863
9864 continue_searching:
325c3691
RH
9865 if (TREE_CODE (last) == STATEMENT_LIST)
9866 {
3a5b9284
RH
9867 tree_stmt_iterator i;
9868
9869 /* This can happen with degenerate cases like ({ }). No value. */
9870 if (!TREE_SIDE_EFFECTS (last))
9871 return body;
9872
9873 /* If we're supposed to generate side effects warnings, process
9874 all of the statements except the last. */
27f33b15 9875 if (warn_unused_value)
325c3691 9876 {
3a5b9284 9877 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
c2255bc4
AH
9878 {
9879 location_t tloc;
9880 tree t = tsi_stmt (i);
9881
9882 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
9883 emit_side_effect_warnings (tloc, t);
9884 }
325c3691
RH
9885 }
9886 else
3a5b9284
RH
9887 i = tsi_last (last);
9888 last_p = tsi_stmt_ptr (i);
9889 last = *last_p;
325c3691
RH
9890 }
9891
3a5b9284
RH
9892 /* If the end of the list is exception related, then the list was split
9893 by a call to push_cleanup. Continue searching. */
9894 if (TREE_CODE (last) == TRY_FINALLY_EXPR
9895 || TREE_CODE (last) == TRY_CATCH_EXPR)
9896 {
9897 last_p = &TREE_OPERAND (last, 0);
9898 last = *last_p;
9899 goto continue_searching;
9900 }
9901
26d8af35
JM
9902 if (last == error_mark_node)
9903 return last;
9904
3a5b9284
RH
9905 /* In the case that the BIND_EXPR is not necessary, return the
9906 expression out from inside it. */
26d8af35
JM
9907 if (last == BIND_EXPR_BODY (body)
9908 && BIND_EXPR_VARS (body) == NULL)
591baeb0 9909 {
928c19bb
JM
9910 /* Even if this looks constant, do not allow it in a constant
9911 expression. */
e5a94231 9912 last = c_wrap_maybe_const (last, true);
591baeb0
JM
9913 /* Do not warn if the return value of a statement expression is
9914 unused. */
928c19bb 9915 TREE_NO_WARNING (last) = 1;
591baeb0
JM
9916 return last;
9917 }
325c3691
RH
9918
9919 /* Extract the type of said expression. */
9920 type = TREE_TYPE (last);
325c3691 9921
3a5b9284
RH
9922 /* If we're not returning a value at all, then the BIND_EXPR that
9923 we already have is a fine expression to return. */
9924 if (!type || VOID_TYPE_P (type))
9925 return body;
9926
9927 /* Now that we've located the expression containing the value, it seems
9928 silly to make voidify_wrapper_expr repeat the process. Create a
9929 temporary of the appropriate type and stick it in a TARGET_EXPR. */
9930 tmp = create_tmp_var_raw (type, NULL);
9931
9932 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
9933 tree_expr_nonnegative_p giving up immediately. */
9934 val = last;
9935 if (TREE_CODE (val) == NOP_EXPR
9936 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
9937 val = TREE_OPERAND (val, 0);
9938
53fb4de3 9939 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 9940 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 9941
c2255bc4
AH
9942 {
9943 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
9944 SET_EXPR_LOCATION (t, loc);
9945 return t;
9946 }
325c3691
RH
9947}
9948\f
9949/* Begin and end compound statements. This is as simple as pushing
9950 and popping new statement lists from the tree. */
9951
9952tree
9953c_begin_compound_stmt (bool do_scope)
9954{
9955 tree stmt = push_stmt_list ();
9956 if (do_scope)
4dfa0342 9957 push_scope ();
325c3691
RH
9958 return stmt;
9959}
9960
c2255bc4
AH
9961/* End a compound statement. STMT is the statement. LOC is the
9962 location of the compound statement-- this is usually the location
9963 of the opening brace. */
9964
325c3691 9965tree
c2255bc4 9966c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
9967{
9968 tree block = NULL;
9969
9970 if (do_scope)
9971 {
9972 if (c_dialect_objc ())
9973 objc_clear_super_receiver ();
9974 block = pop_scope ();
9975 }
9976
9977 stmt = pop_stmt_list (stmt);
c2255bc4 9978 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
9979
9980 /* If this compound statement is nested immediately inside a statement
9981 expression, then force a BIND_EXPR to be created. Otherwise we'll
9982 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
9983 STATEMENT_LISTs merge, and thus we can lose track of what statement
9984 was really last. */
38e01f9e 9985 if (building_stmt_list_p ()
325c3691
RH
9986 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
9987 && TREE_CODE (stmt) != BIND_EXPR)
9988 {
53fb4de3 9989 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 9990 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 9991 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
9992 }
9993
9994 return stmt;
9995}
5a508662
RH
9996
9997/* Queue a cleanup. CLEANUP is an expression/statement to be executed
9998 when the current scope is exited. EH_ONLY is true when this is not
9999 meant to apply to normal control flow transfer. */
10000
10001void
c2255bc4 10002push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 10003{
3a5b9284
RH
10004 enum tree_code code;
10005 tree stmt, list;
10006 bool stmt_expr;
10007
10008 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 10009 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 10010 add_stmt (stmt);
3a5b9284
RH
10011 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10012 list = push_stmt_list ();
10013 TREE_OPERAND (stmt, 0) = list;
10014 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 10015}
325c3691 10016\f
3e4093b6
RS
10017/* Build a binary-operation expression without default conversions.
10018 CODE is the kind of expression to build.
ba47d38d 10019 LOCATION is the operator's location.
3e4093b6
RS
10020 This function differs from `build' in several ways:
10021 the data type of the result is computed and recorded in it,
10022 warnings are generated if arg data types are invalid,
10023 special handling for addition and subtraction of pointers is known,
10024 and some optimization is done (operations on narrow ints
10025 are done in the narrower type when that gives the same result).
10026 Constant folding is also done before the result is returned.
de520661 10027
3e4093b6
RS
10028 Note that the operands will never have enumeral types, or function
10029 or array types, because either they will have the default conversions
10030 performed or they have both just been converted to some other type in which
10031 the arithmetic is to be done. */
10032
10033tree
ba47d38d
AH
10034build_binary_op (location_t location, enum tree_code code,
10035 tree orig_op0, tree orig_op1, int convert_p)
de520661 10036{
8ce94e44
JM
10037 tree type0, type1, orig_type0, orig_type1;
10038 tree eptype;
3e4093b6
RS
10039 enum tree_code code0, code1;
10040 tree op0, op1;
c9f9eb5d 10041 tree ret = error_mark_node;
4de67c26 10042 const char *invalid_op_diag;
4d84fe7c 10043 bool op0_int_operands, op1_int_operands;
928c19bb 10044 bool int_const, int_const_or_overflow, int_operands;
b62acd60 10045
3e4093b6
RS
10046 /* Expression code to give to the expression when it is built.
10047 Normally this is CODE, which is what the caller asked for,
10048 but in some special cases we change it. */
10049 enum tree_code resultcode = code;
8b6a5902 10050
3e4093b6
RS
10051 /* Data type in which the computation is to be performed.
10052 In the simplest cases this is the common type of the arguments. */
10053 tree result_type = NULL;
10054
8ce94e44
JM
10055 /* When the computation is in excess precision, the type of the
10056 final EXCESS_PRECISION_EXPR. */
2d2e923f 10057 tree semantic_result_type = NULL;
8ce94e44 10058
3e4093b6
RS
10059 /* Nonzero means operands have already been type-converted
10060 in whatever way is necessary.
10061 Zero means they need to be converted to RESULT_TYPE. */
10062 int converted = 0;
10063
10064 /* Nonzero means create the expression with this type, rather than
10065 RESULT_TYPE. */
10066 tree build_type = 0;
10067
10068 /* Nonzero means after finally constructing the expression
10069 convert it to this type. */
10070 tree final_type = 0;
10071
10072 /* Nonzero if this is an operation like MIN or MAX which can
10073 safely be computed in short if both args are promoted shorts.
10074 Also implies COMMON.
10075 -1 indicates a bitwise operation; this makes a difference
10076 in the exact conditions for when it is safe to do the operation
10077 in a narrower mode. */
10078 int shorten = 0;
10079
10080 /* Nonzero if this is a comparison operation;
10081 if both args are promoted shorts, compare the original shorts.
10082 Also implies COMMON. */
10083 int short_compare = 0;
10084
10085 /* Nonzero if this is a right-shift operation, which can be computed on the
10086 original short and then promoted if the operand is a promoted short. */
10087 int short_shift = 0;
10088
10089 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10090 int common = 0;
10091
58393038
ZL
10092 /* True means types are compatible as far as ObjC is concerned. */
10093 bool objc_ok;
10094
8ce94e44
JM
10095 /* True means this is an arithmetic operation that may need excess
10096 precision. */
10097 bool may_need_excess_precision;
10098
180f8dbb
JM
10099 /* True means this is a boolean operation that converts both its
10100 operands to truth-values. */
10101 bool boolean_op = false;
10102
de5a5fa1
MP
10103 /* Remember whether we're doing / or %. */
10104 bool doing_div_or_mod = false;
10105
10106 /* Remember whether we're doing << or >>. */
10107 bool doing_shift = false;
10108
10109 /* Tree holding instrumentation expression. */
10110 tree instrument_expr = NULL;
10111
ba47d38d
AH
10112 if (location == UNKNOWN_LOCATION)
10113 location = input_location;
10114
4d84fe7c
JM
10115 op0 = orig_op0;
10116 op1 = orig_op1;
10117
10118 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10119 if (op0_int_operands)
10120 op0 = remove_c_maybe_const_expr (op0);
10121 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10122 if (op1_int_operands)
10123 op1 = remove_c_maybe_const_expr (op1);
10124 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
10125 if (int_operands)
10126 {
10127 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10128 && TREE_CODE (orig_op1) == INTEGER_CST);
10129 int_const = (int_const_or_overflow
10130 && !TREE_OVERFLOW (orig_op0)
10131 && !TREE_OVERFLOW (orig_op1));
10132 }
10133 else
10134 int_const = int_const_or_overflow = false;
10135
0e3a99ae 10136 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e
AS
10137 if (convert_p
10138 && !((TREE_CODE (TREE_TYPE (op0)) == VECTOR_TYPE)
0e3a99ae 10139 != (TREE_CODE (TREE_TYPE (op1)) == VECTOR_TYPE)))
790e9490 10140 {
4d84fe7c
JM
10141 op0 = default_conversion (op0);
10142 op1 = default_conversion (op1);
790e9490
RS
10143 }
10144
36536d79
BI
10145 /* When Cilk Plus is enabled and there are array notations inside op0, then
10146 we check to see if there are builtin array notation functions. If
10147 so, then we take on the type of the array notation inside it. */
b72271b9 10148 if (flag_cilkplus && contains_array_notation_expr (op0))
36536d79
BI
10149 orig_type0 = type0 = find_correct_array_notation_type (op0);
10150 else
10151 orig_type0 = type0 = TREE_TYPE (op0);
10152
b72271b9 10153 if (flag_cilkplus && contains_array_notation_expr (op1))
36536d79
BI
10154 orig_type1 = type1 = find_correct_array_notation_type (op1);
10155 else
10156 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 10157
3e4093b6
RS
10158 /* The expression codes of the data types of the arguments tell us
10159 whether the arguments are integers, floating, pointers, etc. */
10160 code0 = TREE_CODE (type0);
10161 code1 = TREE_CODE (type1);
10162
10163 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10164 STRIP_TYPE_NOPS (op0);
10165 STRIP_TYPE_NOPS (op1);
10166
10167 /* If an error was already reported for one of the arguments,
10168 avoid reporting another error. */
10169
10170 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10171 return error_mark_node;
10172
4de67c26
JM
10173 if ((invalid_op_diag
10174 = targetm.invalid_binary_op (code, type0, type1)))
10175 {
ba47d38d 10176 error_at (location, invalid_op_diag);
4de67c26
JM
10177 return error_mark_node;
10178 }
10179
8ce94e44
JM
10180 switch (code)
10181 {
10182 case PLUS_EXPR:
10183 case MINUS_EXPR:
10184 case MULT_EXPR:
10185 case TRUNC_DIV_EXPR:
10186 case CEIL_DIV_EXPR:
10187 case FLOOR_DIV_EXPR:
10188 case ROUND_DIV_EXPR:
10189 case EXACT_DIV_EXPR:
10190 may_need_excess_precision = true;
10191 break;
10192 default:
10193 may_need_excess_precision = false;
10194 break;
10195 }
10196 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10197 {
10198 op0 = TREE_OPERAND (op0, 0);
10199 type0 = TREE_TYPE (op0);
10200 }
10201 else if (may_need_excess_precision
10202 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10203 {
10204 type0 = eptype;
10205 op0 = convert (eptype, op0);
10206 }
10207 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10208 {
10209 op1 = TREE_OPERAND (op1, 0);
10210 type1 = TREE_TYPE (op1);
10211 }
10212 else if (may_need_excess_precision
10213 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10214 {
10215 type1 = eptype;
10216 op1 = convert (eptype, op1);
10217 }
10218
58393038
ZL
10219 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10220
0e3a99ae
AS
10221 /* In case when one of the operands of the binary operation is
10222 a vector and another is a scalar -- convert scalar to vector. */
10223 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10224 {
a212e43f
MG
10225 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10226 true);
f90e8e2e 10227
0e3a99ae
AS
10228 switch (convert_flag)
10229 {
10230 case stv_error:
10231 return error_mark_node;
10232 case stv_firstarg:
10233 {
10234 bool maybe_const = true;
10235 tree sc;
10236 sc = c_fully_fold (op0, false, &maybe_const);
10237 sc = save_expr (sc);
10238 sc = convert (TREE_TYPE (type1), sc);
10239 op0 = build_vector_from_val (type1, sc);
10240 if (!maybe_const)
10241 op0 = c_wrap_maybe_const (op0, true);
10242 orig_type0 = type0 = TREE_TYPE (op0);
10243 code0 = TREE_CODE (type0);
10244 converted = 1;
10245 break;
10246 }
10247 case stv_secondarg:
10248 {
10249 bool maybe_const = true;
10250 tree sc;
10251 sc = c_fully_fold (op1, false, &maybe_const);
10252 sc = save_expr (sc);
10253 sc = convert (TREE_TYPE (type0), sc);
10254 op1 = build_vector_from_val (type0, sc);
10255 if (!maybe_const)
54b9f838 10256 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
10257 orig_type1 = type1 = TREE_TYPE (op1);
10258 code1 = TREE_CODE (type1);
10259 converted = 1;
10260 break;
10261 }
10262 default:
10263 break;
10264 }
10265 }
10266
3e4093b6 10267 switch (code)
de520661 10268 {
3e4093b6
RS
10269 case PLUS_EXPR:
10270 /* Handle the pointer + int case. */
10271 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10272 {
db3927fb 10273 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
10274 goto return_build_binary_op;
10275 }
3e4093b6 10276 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 10277 {
db3927fb 10278 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
10279 goto return_build_binary_op;
10280 }
fe67cf58 10281 else
3e4093b6
RS
10282 common = 1;
10283 break;
400fbf9f 10284
3e4093b6
RS
10285 case MINUS_EXPR:
10286 /* Subtraction of two similar pointers.
10287 We must subtract them as integers, then divide by object size. */
10288 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 10289 && comp_target_types (location, type0, type1))
c9f9eb5d 10290 {
db3927fb 10291 ret = pointer_diff (location, op0, op1);
c9f9eb5d
AH
10292 goto return_build_binary_op;
10293 }
3e4093b6
RS
10294 /* Handle pointer minus int. Just like pointer plus int. */
10295 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 10296 {
db3927fb 10297 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
10298 goto return_build_binary_op;
10299 }
3e4093b6
RS
10300 else
10301 common = 1;
10302 break;
8b6a5902 10303
3e4093b6
RS
10304 case MULT_EXPR:
10305 common = 1;
10306 break;
10307
10308 case TRUNC_DIV_EXPR:
10309 case CEIL_DIV_EXPR:
10310 case FLOOR_DIV_EXPR:
10311 case ROUND_DIV_EXPR:
10312 case EXACT_DIV_EXPR:
de5a5fa1 10313 doing_div_or_mod = true;
c9f9eb5d 10314 warn_for_div_by_zero (location, op1);
3e4093b6
RS
10315
10316 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10317 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
10318 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10319 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10320 || code1 == FIXED_POINT_TYPE
3e4093b6 10321 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10322 {
5bed876a
AH
10323 enum tree_code tcode0 = code0, tcode1 = code1;
10324
3a021db2 10325 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 10326 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 10327 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 10328 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 10329
ab22c1fa
CF
10330 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10331 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
10332 resultcode = RDIV_EXPR;
10333 else
10334 /* Although it would be tempting to shorten always here, that
10335 loses on some targets, since the modulo instruction is
10336 undefined if the quotient can't be represented in the
10337 computation mode. We shorten only if unsigned or if
10338 dividing by something we know != -1. */
8df83eae 10339 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10340 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10341 && !integer_all_onesp (op1)));
3e4093b6
RS
10342 common = 1;
10343 }
10344 break;
de520661 10345
3e4093b6 10346 case BIT_AND_EXPR:
3e4093b6
RS
10347 case BIT_IOR_EXPR:
10348 case BIT_XOR_EXPR:
10349 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10350 shorten = -1;
9ef0c8d9
AP
10351 /* Allow vector types which are not floating point types. */
10352 else if (code0 == VECTOR_TYPE
10353 && code1 == VECTOR_TYPE
10354 && !VECTOR_FLOAT_TYPE_P (type0)
10355 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
10356 common = 1;
10357 break;
10358
10359 case TRUNC_MOD_EXPR:
10360 case FLOOR_MOD_EXPR:
de5a5fa1 10361 doing_div_or_mod = true;
c9f9eb5d 10362 warn_for_div_by_zero (location, op1);
de520661 10363
5cfd5d9b
AP
10364 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10365 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10366 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10367 common = 1;
10368 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
10369 {
10370 /* Although it would be tempting to shorten always here, that loses
10371 on some targets, since the modulo instruction is undefined if the
10372 quotient can't be represented in the computation mode. We shorten
10373 only if unsigned or if dividing by something we know != -1. */
8df83eae 10374 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 10375 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 10376 && !integer_all_onesp (op1)));
3e4093b6
RS
10377 common = 1;
10378 }
10379 break;
de520661 10380
3e4093b6
RS
10381 case TRUTH_ANDIF_EXPR:
10382 case TRUTH_ORIF_EXPR:
10383 case TRUTH_AND_EXPR:
10384 case TRUTH_OR_EXPR:
10385 case TRUTH_XOR_EXPR:
10386 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
10387 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
10388 || code0 == FIXED_POINT_TYPE)
3e4093b6 10389 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
10390 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
10391 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10392 {
10393 /* Result of these operations is always an int,
10394 but that does not mean the operands should be
10395 converted to ints! */
10396 result_type = integer_type_node;
a27d595d
JM
10397 if (op0_int_operands)
10398 {
10399 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
10400 op0 = remove_c_maybe_const_expr (op0);
10401 }
10402 else
10403 op0 = c_objc_common_truthvalue_conversion (location, op0);
10404 if (op1_int_operands)
10405 {
10406 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
10407 op1 = remove_c_maybe_const_expr (op1);
10408 }
10409 else
10410 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 10411 converted = 1;
180f8dbb 10412 boolean_op = true;
3e4093b6 10413 }
928c19bb
JM
10414 if (code == TRUTH_ANDIF_EXPR)
10415 {
10416 int_const_or_overflow = (int_operands
10417 && TREE_CODE (orig_op0) == INTEGER_CST
10418 && (op0 == truthvalue_false_node
10419 || TREE_CODE (orig_op1) == INTEGER_CST));
10420 int_const = (int_const_or_overflow
10421 && !TREE_OVERFLOW (orig_op0)
10422 && (op0 == truthvalue_false_node
10423 || !TREE_OVERFLOW (orig_op1)));
10424 }
10425 else if (code == TRUTH_ORIF_EXPR)
10426 {
10427 int_const_or_overflow = (int_operands
10428 && TREE_CODE (orig_op0) == INTEGER_CST
10429 && (op0 == truthvalue_true_node
10430 || TREE_CODE (orig_op1) == INTEGER_CST));
10431 int_const = (int_const_or_overflow
10432 && !TREE_OVERFLOW (orig_op0)
10433 && (op0 == truthvalue_true_node
10434 || !TREE_OVERFLOW (orig_op1)));
10435 }
3e4093b6 10436 break;
eba80994 10437
3e4093b6
RS
10438 /* Shift operations: result has same type as first operand;
10439 always convert second operand to int.
10440 Also set SHORT_SHIFT if shifting rightward. */
de520661 10441
3e4093b6 10442 case RSHIFT_EXPR:
f87bd04b
AS
10443 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10444 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10445 {
10446 result_type = type0;
10447 converted = 1;
10448 }
10449 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10450 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10451 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10452 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10453 {
10454 result_type = type0;
10455 converted = 1;
10456 }
10457 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10458 && code1 == INTEGER_TYPE)
3e4093b6 10459 {
de5a5fa1 10460 doing_shift = true;
928c19bb 10461 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 10462 {
3e4093b6 10463 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10464 {
10465 int_const = false;
7d882b83 10466 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f 10467 warning_at (location, 0, "right shift count is negative");
928c19bb 10468 }
3e4093b6 10469 else
bbb818c6 10470 {
3f75a254 10471 if (!integer_zerop (op1))
3e4093b6
RS
10472 short_shift = 1;
10473
10474 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10475 {
10476 int_const = false;
7d882b83 10477 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f
MP
10478 warning_at (location, 0, "right shift count >= width "
10479 "of type");
928c19bb 10480 }
bbb818c6 10481 }
b62acd60 10482 }
de520661 10483
3e4093b6
RS
10484 /* Use the type of the value to be shifted. */
10485 result_type = type0;
f87bd04b
AS
10486 /* Convert the non vector shift-count to an integer, regardless
10487 of size of value being shifted. */
10488 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10489 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10490 op1 = convert (integer_type_node, op1);
10491 /* Avoid converting op1 to result_type later. */
10492 converted = 1;
400fbf9f 10493 }
3e4093b6 10494 break;
253b6b82 10495
3e4093b6 10496 case LSHIFT_EXPR:
f87bd04b
AS
10497 if (code0 == VECTOR_TYPE && code1 == INTEGER_TYPE
10498 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
10499 {
10500 result_type = type0;
10501 converted = 1;
10502 }
10503 else if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10504 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10505 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
10506 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
10507 {
10508 result_type = type0;
10509 converted = 1;
10510 }
10511 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE)
ab22c1fa 10512 && code1 == INTEGER_TYPE)
3e4093b6 10513 {
de5a5fa1 10514 doing_shift = true;
928c19bb 10515 if (TREE_CODE (op1) == INTEGER_CST)
de520661 10516 {
3e4093b6 10517 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
10518 {
10519 int_const = false;
7d882b83 10520 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f 10521 warning_at (location, 0, "left shift count is negative");
928c19bb 10522 }
de520661 10523
3e4093b6 10524 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
10525 {
10526 int_const = false;
7d882b83 10527 if (c_inhibit_evaluation_warnings == 0)
c4bdc42f
MP
10528 warning_at (location, 0, "left shift count >= width of "
10529 "type");
928c19bb 10530 }
94ba5069 10531 }
de520661 10532
3e4093b6
RS
10533 /* Use the type of the value to be shifted. */
10534 result_type = type0;
f87bd04b
AS
10535 /* Convert the non vector shift-count to an integer, regardless
10536 of size of value being shifted. */
10537 if (TREE_CODE (TREE_TYPE (op1)) != VECTOR_TYPE
10538 && TYPE_MAIN_VARIANT (TREE_TYPE (op1)) != integer_type_node)
3e4093b6
RS
10539 op1 = convert (integer_type_node, op1);
10540 /* Avoid converting op1 to result_type later. */
10541 converted = 1;
400fbf9f 10542 }
3e4093b6 10543 break;
de520661 10544
3e4093b6
RS
10545 case EQ_EXPR:
10546 case NE_EXPR:
d246ab4f
AS
10547 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10548 {
10549 tree intt;
0af94e6f 10550 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10551 {
10552 error_at (location, "comparing vectors with different "
10553 "element types");
10554 return error_mark_node;
10555 }
10556
10557 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10558 {
10559 error_at (location, "comparing vectors with different "
10560 "number of elements");
10561 return error_mark_node;
10562 }
10563
10564 /* Always construct signed integer vector type. */
10565 intt = c_common_type_for_size (GET_MODE_BITSIZE
10566 (TYPE_MODE (TREE_TYPE (type0))), 0);
10567 result_type = build_opaque_vector_type (intt,
10568 TYPE_VECTOR_SUBPARTS (type0));
10569 converted = 1;
10570 break;
10571 }
ae311566 10572 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
10573 warning_at (location,
10574 OPT_Wfloat_equal,
10575 "comparing floating point with == or != is unsafe");
3e4093b6
RS
10576 /* Result of comparison is always int,
10577 but don't convert the args to int! */
10578 build_type = integer_type_node;
10579 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 10580 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 10581 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 10582 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 10583 short_compare = 1;
637f1455
SZ
10584 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
10585 {
10586 if (TREE_CODE (op0) == ADDR_EXPR
10587 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0)))
10588 {
10589 if (code == EQ_EXPR)
10590 warning_at (location,
10591 OPT_Waddress,
10592 "the comparison will always evaluate as %<false%> "
10593 "for the address of %qD will never be NULL",
10594 TREE_OPERAND (op0, 0));
10595 else
10596 warning_at (location,
10597 OPT_Waddress,
10598 "the comparison will always evaluate as %<true%> "
10599 "for the address of %qD will never be NULL",
10600 TREE_OPERAND (op0, 0));
10601 }
10602 result_type = type0;
10603 }
10604 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
10605 {
10606 if (TREE_CODE (op1) == ADDR_EXPR
10607 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0)))
10608 {
10609 if (code == EQ_EXPR)
10610 warning_at (location,
f90e8e2e 10611 OPT_Waddress,
637f1455
SZ
10612 "the comparison will always evaluate as %<false%> "
10613 "for the address of %qD will never be NULL",
10614 TREE_OPERAND (op1, 0));
10615 else
10616 warning_at (location,
10617 OPT_Waddress,
10618 "the comparison will always evaluate as %<true%> "
10619 "for the address of %qD will never be NULL",
10620 TREE_OPERAND (op1, 0));
10621 }
10622 result_type = type1;
10623 }
3e4093b6
RS
10624 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10625 {
10626 tree tt0 = TREE_TYPE (type0);
10627 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
10628 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
10629 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
10630 addr_space_t as_common = ADDR_SPACE_GENERIC;
10631
3e4093b6
RS
10632 /* Anything compares with void *. void * compares with anything.
10633 Otherwise, the targets must be compatible
10634 and both must be object or both incomplete. */
744aa42f 10635 if (comp_target_types (location, type0, type1))
10bc1b1b 10636 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
10637 else if (!addr_space_superset (as0, as1, &as_common))
10638 {
10639 error_at (location, "comparison of pointers to "
10640 "disjoint address spaces");
10641 return error_mark_node;
10642 }
267bac10 10643 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 10644 {
36c5e70a 10645 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 10646 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10647 "comparison of %<void *%> with function pointer");
ee2990e7 10648 }
267bac10 10649 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 10650 {
36c5e70a 10651 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 10652 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10653 "comparison of %<void *%> with function pointer");
e6834654 10654 }
3e4093b6 10655 else
58393038
ZL
10656 /* Avoid warning about the volatile ObjC EH puts on decls. */
10657 if (!objc_ok)
ba47d38d 10658 pedwarn (location, 0,
509c9d60 10659 "comparison of distinct pointer types lacks a cast");
e6834654 10660
3e4093b6 10661 if (result_type == NULL_TREE)
36c5e70a
BE
10662 {
10663 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10664 result_type = build_pointer_type
10665 (build_qualified_type (void_type_node, qual));
10666 }
e6834654 10667 }
3e4093b6 10668 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 10669 {
3e4093b6 10670 result_type = type0;
ba47d38d 10671 pedwarn (location, 0, "comparison between pointer and integer");
de520661 10672 }
3e4093b6 10673 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 10674 {
3e4093b6 10675 result_type = type1;
ba47d38d 10676 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 10677 }
3e4093b6 10678 break;
8b6a5902 10679
3e4093b6
RS
10680 case LE_EXPR:
10681 case GE_EXPR:
10682 case LT_EXPR:
10683 case GT_EXPR:
d246ab4f
AS
10684 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
10685 {
10686 tree intt;
0af94e6f 10687 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
10688 {
10689 error_at (location, "comparing vectors with different "
10690 "element types");
10691 return error_mark_node;
10692 }
10693
10694 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
10695 {
10696 error_at (location, "comparing vectors with different "
10697 "number of elements");
10698 return error_mark_node;
10699 }
10700
10701 /* Always construct signed integer vector type. */
10702 intt = c_common_type_for_size (GET_MODE_BITSIZE
10703 (TYPE_MODE (TREE_TYPE (type0))), 0);
10704 result_type = build_opaque_vector_type (intt,
10705 TYPE_VECTOR_SUBPARTS (type0));
10706 converted = 1;
10707 break;
10708 }
3e4093b6 10709 build_type = integer_type_node;
ab22c1fa
CF
10710 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
10711 || code0 == FIXED_POINT_TYPE)
10712 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
10713 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
10714 short_compare = 1;
10715 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
10716 {
36c5e70a
BE
10717 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
10718 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
10719 addr_space_t as_common;
10720
744aa42f 10721 if (comp_target_types (location, type0, type1))
3e4093b6 10722 {
10bc1b1b 10723 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
10724 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
10725 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 10726 pedwarn (location, 0,
509c9d60 10727 "comparison of complete and incomplete pointers");
fcf73884 10728 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 10729 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 10730 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
10731 else if (null_pointer_constant_p (orig_op0)
10732 || null_pointer_constant_p (orig_op1))
10733 warning_at (location, OPT_Wextra,
10734 "ordered comparison of pointer with null pointer");
10735
3e4093b6 10736 }
36c5e70a
BE
10737 else if (!addr_space_superset (as0, as1, &as_common))
10738 {
10739 error_at (location, "comparison of pointers to "
10740 "disjoint address spaces");
10741 return error_mark_node;
10742 }
3e4093b6
RS
10743 else
10744 {
36c5e70a
BE
10745 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
10746 result_type = build_pointer_type
10747 (build_qualified_type (void_type_node, qual));
ba47d38d 10748 pedwarn (location, 0,
509c9d60 10749 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
10750 }
10751 }
6aa3c60d 10752 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
10753 {
10754 result_type = type0;
fcf73884 10755 if (pedantic)
c1771a20 10756 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
10757 "ordered comparison of pointer with integer zero");
10758 else if (extra_warnings)
ba47d38d 10759 warning_at (location, OPT_Wextra,
d42ba3b8 10760 "ordered comparison of pointer with integer zero");
3e4093b6 10761 }
6aa3c60d 10762 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
10763 {
10764 result_type = type1;
d42ba3b8 10765 if (pedantic)
c1771a20 10766 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
10767 "ordered comparison of pointer with integer zero");
10768 else if (extra_warnings)
10769 warning_at (location, OPT_Wextra,
10770 "ordered comparison of pointer with integer zero");
3e4093b6
RS
10771 }
10772 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
10773 {
10774 result_type = type0;
ba47d38d 10775 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10776 }
10777 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
10778 {
10779 result_type = type1;
ba47d38d 10780 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
10781 }
10782 break;
64094f6a 10783
3e4093b6 10784 default:
37b2f290 10785 gcc_unreachable ();
c9fe6f9f 10786 }
8f17b5c5 10787
e57e265b
PB
10788 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10789 return error_mark_node;
10790
5bed876a
AH
10791 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10792 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 10793 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 10794 {
ba47d38d 10795 binary_op_error (location, code, type0, type1);
5bed876a
AH
10796 return error_mark_node;
10797 }
10798
3e4093b6 10799 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 10800 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
10801 &&
10802 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 10803 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 10804 {
2ca862e9
JM
10805 bool first_complex = (code0 == COMPLEX_TYPE);
10806 bool second_complex = (code1 == COMPLEX_TYPE);
10807 int none_complex = (!first_complex && !second_complex);
39b726dd 10808
3e4093b6 10809 if (shorten || common || short_compare)
3bf6bfcc
JJ
10810 {
10811 result_type = c_common_type (type0, type1);
0a0b3574
MM
10812 do_warn_double_promotion (result_type, type0, type1,
10813 "implicit conversion from %qT to %qT "
10814 "to match other operand of binary "
10815 "expression",
10816 location);
3bf6bfcc
JJ
10817 if (result_type == error_mark_node)
10818 return error_mark_node;
10819 }
400fbf9f 10820
2ca862e9
JM
10821 if (first_complex != second_complex
10822 && (code == PLUS_EXPR
10823 || code == MINUS_EXPR
10824 || code == MULT_EXPR
10825 || (code == TRUNC_DIV_EXPR && first_complex))
10826 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
10827 && flag_signed_zeros)
10828 {
10829 /* An operation on mixed real/complex operands must be
10830 handled specially, but the language-independent code can
10831 more easily optimize the plain complex arithmetic if
10832 -fno-signed-zeros. */
10833 tree real_type = TREE_TYPE (result_type);
10834 tree real, imag;
10835 if (type0 != orig_type0 || type1 != orig_type1)
10836 {
10837 gcc_assert (may_need_excess_precision && common);
2d2e923f 10838 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
10839 }
10840 if (first_complex)
10841 {
10842 if (TREE_TYPE (op0) != result_type)
68fca595 10843 op0 = convert_and_check (location, result_type, op0);
2ca862e9 10844 if (TREE_TYPE (op1) != real_type)
68fca595 10845 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
10846 }
10847 else
10848 {
10849 if (TREE_TYPE (op0) != real_type)
68fca595 10850 op0 = convert_and_check (location, real_type, op0);
2ca862e9 10851 if (TREE_TYPE (op1) != result_type)
68fca595 10852 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
10853 }
10854 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
10855 return error_mark_node;
10856 if (first_complex)
10857 {
10858 op0 = c_save_expr (op0);
10859 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
10860 op0, 1);
10861 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
10862 op0, 1);
10863 switch (code)
10864 {
10865 case MULT_EXPR:
10866 case TRUNC_DIV_EXPR:
c4603e7c 10867 op1 = c_save_expr (op1);
2ca862e9
JM
10868 imag = build2 (resultcode, real_type, imag, op1);
10869 /* Fall through. */
10870 case PLUS_EXPR:
10871 case MINUS_EXPR:
10872 real = build2 (resultcode, real_type, real, op1);
10873 break;
10874 default:
10875 gcc_unreachable();
10876 }
10877 }
10878 else
10879 {
10880 op1 = c_save_expr (op1);
10881 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
10882 op1, 1);
10883 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
10884 op1, 1);
10885 switch (code)
10886 {
10887 case MULT_EXPR:
c4603e7c 10888 op0 = c_save_expr (op0);
2ca862e9
JM
10889 imag = build2 (resultcode, real_type, op0, imag);
10890 /* Fall through. */
10891 case PLUS_EXPR:
10892 real = build2 (resultcode, real_type, op0, real);
10893 break;
10894 case MINUS_EXPR:
10895 real = build2 (resultcode, real_type, op0, real);
10896 imag = build1 (NEGATE_EXPR, real_type, imag);
10897 break;
10898 default:
10899 gcc_unreachable();
10900 }
10901 }
10902 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
10903 goto return_build_binary_op;
10904 }
10905
3e4093b6
RS
10906 /* For certain operations (which identify themselves by shorten != 0)
10907 if both args were extended from the same smaller type,
10908 do the arithmetic in that type and then extend.
400fbf9f 10909
3e4093b6
RS
10910 shorten !=0 and !=1 indicates a bitwise operation.
10911 For them, this optimization is safe only if
10912 both args are zero-extended or both are sign-extended.
10913 Otherwise, we might change the result.
10914 Eg, (short)-1 | (unsigned short)-1 is (int)-1
10915 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 10916
3e4093b6
RS
10917 if (shorten && none_complex)
10918 {
3e4093b6 10919 final_type = result_type;
b8698a0f 10920 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 10921 shorten == -1);
3e4093b6 10922 }
88a3dbc1 10923
3e4093b6 10924 /* Shifts can be shortened if shifting right. */
2f6e4e97 10925
3e4093b6
RS
10926 if (short_shift)
10927 {
10928 int unsigned_arg;
10929 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 10930
3e4093b6 10931 final_type = result_type;
abe80e6d 10932
3e4093b6 10933 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 10934 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 10935
3e4093b6 10936 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 10937 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
10938 /* We can shorten only if the shift count is less than the
10939 number of bits in the smaller type size. */
10940 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
10941 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 10942 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
10943 {
10944 /* Do an unsigned shift if the operand was zero-extended. */
10945 result_type
10946 = c_common_signed_or_unsigned_type (unsigned_arg,
10947 TREE_TYPE (arg0));
10948 /* Convert value-to-be-shifted to that type. */
10949 if (TREE_TYPE (op0) != result_type)
10950 op0 = convert (result_type, op0);
10951 converted = 1;
abe80e6d 10952 }
88a3dbc1
RK
10953 }
10954
3e4093b6
RS
10955 /* Comparison operations are shortened too but differently.
10956 They identify themselves by setting short_compare = 1. */
56cb9733 10957
3e4093b6
RS
10958 if (short_compare)
10959 {
10960 /* Don't write &op0, etc., because that would prevent op0
10961 from being kept in a register.
10962 Instead, make copies of the our local variables and
10963 pass the copies by reference, then copy them back afterward. */
10964 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
10965 enum tree_code xresultcode = resultcode;
10966 tree val
393e8e8b
MP
10967 = shorten_compare (location, &xop0, &xop1, &xresult_type,
10968 &xresultcode);
8f17b5c5 10969
3e4093b6 10970 if (val != 0)
c9f9eb5d
AH
10971 {
10972 ret = val;
10973 goto return_build_binary_op;
10974 }
8f17b5c5 10975
3e4093b6
RS
10976 op0 = xop0, op1 = xop1;
10977 converted = 1;
10978 resultcode = xresultcode;
8f17b5c5 10979
7d882b83 10980 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
10981 {
10982 bool op0_maybe_const = true;
10983 bool op1_maybe_const = true;
10984 tree orig_op0_folded, orig_op1_folded;
10985
10986 if (in_late_binary_op)
10987 {
10988 orig_op0_folded = orig_op0;
10989 orig_op1_folded = orig_op1;
10990 }
10991 else
10992 {
10993 /* Fold for the sake of possible warnings, as in
10994 build_conditional_expr. This requires the
10995 "original" values to be folded, not just op0 and
10996 op1. */
f5178456 10997 c_inhibit_evaluation_warnings++;
928c19bb
JM
10998 op0 = c_fully_fold (op0, require_constant_value,
10999 &op0_maybe_const);
11000 op1 = c_fully_fold (op1, require_constant_value,
11001 &op1_maybe_const);
f5178456 11002 c_inhibit_evaluation_warnings--;
928c19bb
JM
11003 orig_op0_folded = c_fully_fold (orig_op0,
11004 require_constant_value,
11005 NULL);
11006 orig_op1_folded = c_fully_fold (orig_op1,
11007 require_constant_value,
11008 NULL);
11009 }
11010
11011 if (warn_sign_compare)
11012 warn_for_sign_compare (location, orig_op0_folded,
11013 orig_op1_folded, op0, op1,
11014 result_type, resultcode);
5c2f94b4 11015 if (!in_late_binary_op && !int_operands)
928c19bb
JM
11016 {
11017 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 11018 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 11019 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 11020 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 11021 }
3e4093b6 11022 }
2ad1815d 11023 }
64094f6a 11024 }
64094f6a 11025
3e4093b6
RS
11026 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11027 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11028 Then the expression will be built.
11029 It will be given type FINAL_TYPE if that is nonzero;
11030 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 11031
3e4093b6
RS
11032 if (!result_type)
11033 {
ba47d38d 11034 binary_op_error (location, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
11035 return error_mark_node;
11036 }
400fbf9f 11037
3e4093b6 11038 if (build_type == NULL_TREE)
8ce94e44
JM
11039 {
11040 build_type = result_type;
180f8dbb
JM
11041 if ((type0 != orig_type0 || type1 != orig_type1)
11042 && !boolean_op)
8ce94e44
JM
11043 {
11044 gcc_assert (may_need_excess_precision && common);
2d2e923f 11045 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
11046 }
11047 }
400fbf9f 11048
2d2e923f
MLI
11049 if (!converted)
11050 {
68fca595
MP
11051 op0 = ep_convert_and_check (location, result_type, op0,
11052 semantic_result_type);
11053 op1 = ep_convert_and_check (location, result_type, op1,
11054 semantic_result_type);
2d2e923f
MLI
11055
11056 /* This can happen if one operand has a vector type, and the other
11057 has a different type. */
11058 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11059 return error_mark_node;
11060 }
11061
f8ed5150
MP
11062 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11063 | SANITIZE_FLOAT_DIVIDE))
de5a5fa1 11064 && current_function_decl != 0
ce6923c5
MP
11065 && !lookup_attribute ("no_sanitize_undefined",
11066 DECL_ATTRIBUTES (current_function_decl))
de5a5fa1
MP
11067 && (doing_div_or_mod || doing_shift))
11068 {
11069 /* OP0 and/or OP1 might have side-effects. */
11070 op0 = c_save_expr (op0);
11071 op1 = c_save_expr (op1);
11072 op0 = c_fully_fold (op0, false, NULL);
11073 op1 = c_fully_fold (op1, false, NULL);
f8ed5150
MP
11074 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11075 | SANITIZE_FLOAT_DIVIDE)))
de5a5fa1 11076 instrument_expr = ubsan_instrument_division (location, op0, op1);
a24d975c 11077 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
de5a5fa1
MP
11078 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11079 }
11080
c9f9eb5d 11081 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
11082 if (int_const_or_overflow)
11083 ret = (require_constant_value
db3927fb
AH
11084 ? fold_build2_initializer_loc (location, resultcode, build_type,
11085 op0, op1)
11086 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
11087 else
11088 ret = build2 (resultcode, build_type, op0, op1);
c9f9eb5d
AH
11089 if (final_type != 0)
11090 ret = convert (final_type, ret);
11091
11092 return_build_binary_op:
11093 gcc_assert (ret != error_mark_node);
928c19bb
JM
11094 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11095 ret = (int_operands
11096 ? note_integer_operands (ret)
11097 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11098 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11099 && !in_late_binary_op)
11100 ret = note_integer_operands (ret);
2d2e923f
MLI
11101 if (semantic_result_type)
11102 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
c9f9eb5d 11103 protected_set_expr_location (ret, location);
de5a5fa1 11104
a24d975c 11105 if (instrument_expr != NULL)
de5a5fa1
MP
11106 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11107 instrument_expr, ret);
11108
c9f9eb5d 11109 return ret;
400fbf9f 11110}
85498824
JM
11111
11112
11113/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 11114 purpose. LOCATION is the source location for the expression. */
85498824
JM
11115
11116tree
ba47d38d 11117c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 11118{
928c19bb
JM
11119 bool int_const, int_operands;
11120
85498824
JM
11121 switch (TREE_CODE (TREE_TYPE (expr)))
11122 {
11123 case ARRAY_TYPE:
ba47d38d 11124 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
11125 return error_mark_node;
11126
11127 case RECORD_TYPE:
ba47d38d 11128 error_at (location, "used struct type value where scalar is required");
85498824
JM
11129 return error_mark_node;
11130
11131 case UNION_TYPE:
ba47d38d 11132 error_at (location, "used union type value where scalar is required");
85498824
JM
11133 return error_mark_node;
11134
04af8788
NP
11135 case VOID_TYPE:
11136 error_at (location, "void value not ignored as it ought to be");
11137 return error_mark_node;
11138
46bdb9cf
JM
11139 case FUNCTION_TYPE:
11140 gcc_unreachable ();
11141
d246ab4f
AS
11142 case VECTOR_TYPE:
11143 error_at (location, "used vector type where scalar is required");
11144 return error_mark_node;
11145
85498824
JM
11146 default:
11147 break;
11148 }
11149
928c19bb
JM
11150 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11151 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
11152 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11153 {
11154 expr = remove_c_maybe_const_expr (expr);
11155 expr = build2 (NE_EXPR, integer_type_node, expr,
11156 convert (TREE_TYPE (expr), integer_zero_node));
11157 expr = note_integer_operands (expr);
11158 }
11159 else
11160 /* ??? Should we also give an error for vectors rather than leaving
11161 those to give errors later? */
11162 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
11163
11164 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11165 {
11166 if (TREE_OVERFLOW (expr))
11167 return expr;
11168 else
11169 return note_integer_operands (expr);
11170 }
11171 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11172 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11173 return expr;
85498824 11174}
73f397d4
JM
11175\f
11176
11177/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11178 required. */
11179
11180tree
51eed280 11181c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
11182{
11183 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11184 {
11185 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11186 /* Executing a compound literal inside a function reinitializes
11187 it. */
11188 if (!TREE_STATIC (decl))
11189 *se = true;
11190 return decl;
11191 }
11192 else
11193 return expr;
11194}
953ff289 11195\f
c0220ea4 11196/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
11197
11198tree
11199c_begin_omp_parallel (void)
11200{
11201 tree block;
11202
11203 keep_next_level ();
11204 block = c_begin_compound_stmt (true);
11205
11206 return block;
11207}
11208
c2255bc4
AH
11209/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11210 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 11211
953ff289 11212tree
c2255bc4 11213c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
11214{
11215 tree stmt;
11216
c2255bc4 11217 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
11218
11219 stmt = make_node (OMP_PARALLEL);
11220 TREE_TYPE (stmt) = void_type_node;
11221 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11222 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 11223 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
11224
11225 return add_stmt (stmt);
11226}
11227
a68ab351
JJ
11228/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11229
11230tree
11231c_begin_omp_task (void)
11232{
11233 tree block;
11234
11235 keep_next_level ();
11236 block = c_begin_compound_stmt (true);
11237
11238 return block;
11239}
11240
c2255bc4
AH
11241/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11242 statement. LOC is the location of the #pragma. */
a68ab351
JJ
11243
11244tree
c2255bc4 11245c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
11246{
11247 tree stmt;
11248
c2255bc4 11249 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
11250
11251 stmt = make_node (OMP_TASK);
11252 TREE_TYPE (stmt) = void_type_node;
11253 OMP_TASK_CLAUSES (stmt) = clauses;
11254 OMP_TASK_BODY (stmt) = block;
c2255bc4 11255 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
11256
11257 return add_stmt (stmt);
11258}
11259
acf0174b
JJ
11260/* Generate GOMP_cancel call for #pragma omp cancel. */
11261
11262void
11263c_finish_omp_cancel (location_t loc, tree clauses)
11264{
11265 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
11266 int mask = 0;
11267 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11268 mask = 1;
11269 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11270 mask = 2;
11271 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11272 mask = 4;
11273 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11274 mask = 8;
11275 else
11276 {
11277 error_at (loc, "%<#pragma omp cancel must specify one of "
11278 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11279 "clauses");
11280 return;
11281 }
11282 tree ifc = find_omp_clause (clauses, OMP_CLAUSE_IF);
11283 if (ifc != NULL_TREE)
11284 {
11285 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
11286 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
11287 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
11288 build_zero_cst (type));
11289 }
11290 else
11291 ifc = boolean_true_node;
11292 tree stmt = build_call_expr_loc (loc, fn, 2,
11293 build_int_cst (integer_type_node, mask),
11294 ifc);
11295 add_stmt (stmt);
11296}
11297
11298/* Generate GOMP_cancellation_point call for
11299 #pragma omp cancellation point. */
11300
11301void
11302c_finish_omp_cancellation_point (location_t loc, tree clauses)
11303{
11304 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
11305 int mask = 0;
11306 if (find_omp_clause (clauses, OMP_CLAUSE_PARALLEL))
11307 mask = 1;
11308 else if (find_omp_clause (clauses, OMP_CLAUSE_FOR))
11309 mask = 2;
11310 else if (find_omp_clause (clauses, OMP_CLAUSE_SECTIONS))
11311 mask = 4;
11312 else if (find_omp_clause (clauses, OMP_CLAUSE_TASKGROUP))
11313 mask = 8;
11314 else
11315 {
11316 error_at (loc, "%<#pragma omp cancellation point must specify one of "
11317 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
11318 "clauses");
11319 return;
11320 }
11321 tree stmt = build_call_expr_loc (loc, fn, 1,
11322 build_int_cst (integer_type_node, mask));
11323 add_stmt (stmt);
11324}
11325
11326/* Helper function for handle_omp_array_sections. Called recursively
11327 to handle multiple array-section-subscripts. C is the clause,
11328 T current expression (initially OMP_CLAUSE_DECL), which is either
11329 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
11330 expression if specified, TREE_VALUE length expression if specified,
11331 TREE_CHAIN is what it has been specified after, or some decl.
11332 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
11333 set to true if any of the array-section-subscript could have length
11334 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
11335 first array-section-subscript which is known not to have length
11336 of one. Given say:
11337 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
11338 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
11339 all are or may have length of 1, array-section-subscript [:2] is the
11340 first one knonwn not to have length 1. For array-section-subscript
11341 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
11342 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
11343 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
11344 case though, as some lengths could be zero. */
11345
11346static tree
11347handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
11348 bool &maybe_zero_len, unsigned int &first_non_one)
11349{
11350 tree ret, low_bound, length, type;
11351 if (TREE_CODE (t) != TREE_LIST)
11352 {
11353 if (t == error_mark_node || TREE_TYPE (t) == error_mark_node)
11354 return error_mark_node;
11355 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
11356 {
11357 if (DECL_P (t))
11358 error_at (OMP_CLAUSE_LOCATION (c),
11359 "%qD is not a variable in %qs clause", t,
11360 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11361 else
11362 error_at (OMP_CLAUSE_LOCATION (c),
11363 "%qE is not a variable in %qs clause", t,
11364 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11365 return error_mark_node;
11366 }
11367 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11368 && TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
11369 {
11370 error_at (OMP_CLAUSE_LOCATION (c),
11371 "%qD is threadprivate variable in %qs clause", t,
11372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11373 return error_mark_node;
11374 }
11375 return t;
11376 }
11377
11378 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
11379 maybe_zero_len, first_non_one);
11380 if (ret == error_mark_node || ret == NULL_TREE)
11381 return ret;
11382
11383 type = TREE_TYPE (ret);
11384 low_bound = TREE_PURPOSE (t);
11385 length = TREE_VALUE (t);
11386
11387 if (low_bound == error_mark_node || length == error_mark_node)
11388 return error_mark_node;
11389
11390 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
11391 {
11392 error_at (OMP_CLAUSE_LOCATION (c),
11393 "low bound %qE of array section does not have integral type",
11394 low_bound);
11395 return error_mark_node;
11396 }
11397 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
11398 {
11399 error_at (OMP_CLAUSE_LOCATION (c),
11400 "length %qE of array section does not have integral type",
11401 length);
11402 return error_mark_node;
11403 }
11404 if (low_bound
11405 && TREE_CODE (low_bound) == INTEGER_CST
11406 && TYPE_PRECISION (TREE_TYPE (low_bound))
11407 > TYPE_PRECISION (sizetype))
11408 low_bound = fold_convert (sizetype, low_bound);
11409 if (length
11410 && TREE_CODE (length) == INTEGER_CST
11411 && TYPE_PRECISION (TREE_TYPE (length))
11412 > TYPE_PRECISION (sizetype))
11413 length = fold_convert (sizetype, length);
11414 if (low_bound == NULL_TREE)
11415 low_bound = integer_zero_node;
11416
11417 if (length != NULL_TREE)
11418 {
11419 if (!integer_nonzerop (length))
11420 maybe_zero_len = true;
11421 if (first_non_one == types.length ()
11422 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
11423 first_non_one++;
11424 }
11425 if (TREE_CODE (type) == ARRAY_TYPE)
11426 {
11427 if (length == NULL_TREE
11428 && (TYPE_DOMAIN (type) == NULL_TREE
11429 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
11430 {
11431 error_at (OMP_CLAUSE_LOCATION (c),
11432 "for unknown bound array type length expression must "
11433 "be specified");
11434 return error_mark_node;
11435 }
11436 if (TREE_CODE (low_bound) == INTEGER_CST
11437 && tree_int_cst_sgn (low_bound) == -1)
11438 {
11439 error_at (OMP_CLAUSE_LOCATION (c),
11440 "negative low bound in array section in %qs clause",
11441 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11442 return error_mark_node;
11443 }
11444 if (length != NULL_TREE
11445 && TREE_CODE (length) == INTEGER_CST
11446 && tree_int_cst_sgn (length) == -1)
11447 {
11448 error_at (OMP_CLAUSE_LOCATION (c),
11449 "negative length in array section in %qs clause",
11450 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11451 return error_mark_node;
11452 }
11453 if (TYPE_DOMAIN (type)
11454 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
11455 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
11456 == INTEGER_CST)
11457 {
11458 tree size = size_binop (PLUS_EXPR,
11459 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11460 size_one_node);
11461 if (TREE_CODE (low_bound) == INTEGER_CST)
11462 {
11463 if (tree_int_cst_lt (size, low_bound))
11464 {
11465 error_at (OMP_CLAUSE_LOCATION (c),
11466 "low bound %qE above array section size "
11467 "in %qs clause", low_bound,
11468 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11469 return error_mark_node;
11470 }
11471 if (tree_int_cst_equal (size, low_bound))
11472 maybe_zero_len = true;
11473 else if (length == NULL_TREE
11474 && first_non_one == types.length ()
11475 && tree_int_cst_equal
11476 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
11477 low_bound))
11478 first_non_one++;
11479 }
11480 else if (length == NULL_TREE)
11481 {
11482 maybe_zero_len = true;
11483 if (first_non_one == types.length ())
11484 first_non_one++;
11485 }
11486 if (length && TREE_CODE (length) == INTEGER_CST)
11487 {
11488 if (tree_int_cst_lt (size, length))
11489 {
11490 error_at (OMP_CLAUSE_LOCATION (c),
11491 "length %qE above array section size "
11492 "in %qs clause", length,
11493 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11494 return error_mark_node;
11495 }
11496 if (TREE_CODE (low_bound) == INTEGER_CST)
11497 {
11498 tree lbpluslen
11499 = size_binop (PLUS_EXPR,
11500 fold_convert (sizetype, low_bound),
11501 fold_convert (sizetype, length));
11502 if (TREE_CODE (lbpluslen) == INTEGER_CST
11503 && tree_int_cst_lt (size, lbpluslen))
11504 {
11505 error_at (OMP_CLAUSE_LOCATION (c),
11506 "high bound %qE above array section size "
11507 "in %qs clause", lbpluslen,
11508 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11509 return error_mark_node;
11510 }
11511 }
11512 }
11513 }
11514 else if (length == NULL_TREE)
11515 {
11516 maybe_zero_len = true;
11517 if (first_non_one == types.length ())
11518 first_non_one++;
11519 }
11520
11521 /* For [lb:] we will need to evaluate lb more than once. */
11522 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11523 {
11524 tree lb = c_save_expr (low_bound);
11525 if (lb != low_bound)
11526 {
11527 TREE_PURPOSE (t) = lb;
11528 low_bound = lb;
11529 }
11530 }
11531 }
11532 else if (TREE_CODE (type) == POINTER_TYPE)
11533 {
11534 if (length == NULL_TREE)
11535 {
11536 error_at (OMP_CLAUSE_LOCATION (c),
11537 "for pointer type length expression must be specified");
11538 return error_mark_node;
11539 }
11540 /* If there is a pointer type anywhere but in the very first
11541 array-section-subscript, the array section can't be contiguous. */
11542 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
11543 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
11544 {
11545 error_at (OMP_CLAUSE_LOCATION (c),
11546 "array section is not contiguous in %qs clause",
11547 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11548 return error_mark_node;
11549 }
11550 }
11551 else
11552 {
11553 error_at (OMP_CLAUSE_LOCATION (c),
11554 "%qE does not have pointer or array type", ret);
11555 return error_mark_node;
11556 }
11557 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
11558 types.safe_push (TREE_TYPE (ret));
11559 /* We will need to evaluate lb more than once. */
11560 tree lb = c_save_expr (low_bound);
11561 if (lb != low_bound)
11562 {
11563 TREE_PURPOSE (t) = lb;
11564 low_bound = lb;
11565 }
11566 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
11567 return ret;
11568}
11569
11570/* Handle array sections for clause C. */
11571
11572static bool
11573handle_omp_array_sections (tree c)
11574{
11575 bool maybe_zero_len = false;
11576 unsigned int first_non_one = 0;
11577 vec<tree> types = vNULL;
11578 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
11579 maybe_zero_len, first_non_one);
11580 if (first == error_mark_node)
11581 {
11582 types.release ();
11583 return true;
11584 }
11585 if (first == NULL_TREE)
11586 {
11587 types.release ();
11588 return false;
11589 }
11590 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
11591 {
11592 tree t = OMP_CLAUSE_DECL (c);
11593 tree tem = NULL_TREE;
11594 types.release ();
11595 /* Need to evaluate side effects in the length expressions
11596 if any. */
11597 while (TREE_CODE (t) == TREE_LIST)
11598 {
11599 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
11600 {
11601 if (tem == NULL_TREE)
11602 tem = TREE_VALUE (t);
11603 else
11604 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
11605 TREE_VALUE (t), tem);
11606 }
11607 t = TREE_CHAIN (t);
11608 }
11609 if (tem)
11610 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
11611 first = c_fully_fold (first, false, NULL);
11612 OMP_CLAUSE_DECL (c) = first;
11613 }
11614 else
11615 {
11616 unsigned int num = types.length (), i;
11617 tree t, side_effects = NULL_TREE, size = NULL_TREE;
11618 tree condition = NULL_TREE;
11619
11620 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
11621 maybe_zero_len = true;
11622
11623 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
11624 t = TREE_CHAIN (t))
11625 {
11626 tree low_bound = TREE_PURPOSE (t);
11627 tree length = TREE_VALUE (t);
11628
11629 i--;
11630 if (low_bound
11631 && TREE_CODE (low_bound) == INTEGER_CST
11632 && TYPE_PRECISION (TREE_TYPE (low_bound))
11633 > TYPE_PRECISION (sizetype))
11634 low_bound = fold_convert (sizetype, low_bound);
11635 if (length
11636 && TREE_CODE (length) == INTEGER_CST
11637 && TYPE_PRECISION (TREE_TYPE (length))
11638 > TYPE_PRECISION (sizetype))
11639 length = fold_convert (sizetype, length);
11640 if (low_bound == NULL_TREE)
11641 low_bound = integer_zero_node;
11642 if (!maybe_zero_len && i > first_non_one)
11643 {
11644 if (integer_nonzerop (low_bound))
11645 goto do_warn_noncontiguous;
11646 if (length != NULL_TREE
11647 && TREE_CODE (length) == INTEGER_CST
11648 && TYPE_DOMAIN (types[i])
11649 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
11650 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
11651 == INTEGER_CST)
11652 {
11653 tree size;
11654 size = size_binop (PLUS_EXPR,
11655 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11656 size_one_node);
11657 if (!tree_int_cst_equal (length, size))
11658 {
11659 do_warn_noncontiguous:
11660 error_at (OMP_CLAUSE_LOCATION (c),
11661 "array section is not contiguous in %qs "
11662 "clause",
11663 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
11664 types.release ();
11665 return true;
11666 }
11667 }
11668 if (length != NULL_TREE
11669 && TREE_SIDE_EFFECTS (length))
11670 {
11671 if (side_effects == NULL_TREE)
11672 side_effects = length;
11673 else
11674 side_effects = build2 (COMPOUND_EXPR,
11675 TREE_TYPE (side_effects),
11676 length, side_effects);
11677 }
11678 }
11679 else
11680 {
11681 tree l;
11682
11683 if (i > first_non_one && length && integer_nonzerop (length))
11684 continue;
11685 if (length)
11686 l = fold_convert (sizetype, length);
11687 else
11688 {
11689 l = size_binop (PLUS_EXPR,
11690 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
11691 size_one_node);
11692 l = size_binop (MINUS_EXPR, l,
11693 fold_convert (sizetype, low_bound));
11694 }
11695 if (i > first_non_one)
11696 {
11697 l = fold_build2 (NE_EXPR, boolean_type_node, l,
11698 size_zero_node);
11699 if (condition == NULL_TREE)
11700 condition = l;
11701 else
11702 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
11703 l, condition);
11704 }
11705 else if (size == NULL_TREE)
11706 {
11707 size = size_in_bytes (TREE_TYPE (types[i]));
11708 size = size_binop (MULT_EXPR, size, l);
11709 if (condition)
11710 size = fold_build3 (COND_EXPR, sizetype, condition,
11711 size, size_zero_node);
11712 }
11713 else
11714 size = size_binop (MULT_EXPR, size, l);
11715 }
11716 }
11717 types.release ();
11718 if (side_effects)
11719 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
11720 first = c_fully_fold (first, false, NULL);
11721 OMP_CLAUSE_DECL (c) = first;
11722 if (size)
11723 size = c_fully_fold (size, false, NULL);
11724 OMP_CLAUSE_SIZE (c) = size;
11725 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
11726 return false;
11727 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
11728 OMP_CLAUSE_MAP_KIND (c2) = OMP_CLAUSE_MAP_POINTER;
11729 if (!c_mark_addressable (t))
11730 return false;
11731 OMP_CLAUSE_DECL (c2) = t;
11732 t = build_fold_addr_expr (first);
11733 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
11734 tree ptr = OMP_CLAUSE_DECL (c2);
11735 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
11736 ptr = build_fold_addr_expr (ptr);
11737 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11738 ptrdiff_type_node, t,
11739 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
11740 ptrdiff_type_node, ptr));
11741 t = c_fully_fold (t, false, NULL);
11742 OMP_CLAUSE_SIZE (c2) = t;
11743 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
11744 OMP_CLAUSE_CHAIN (c) = c2;
11745 }
11746 return false;
11747}
11748
11749/* Helper function of finish_omp_clauses. Clone STMT as if we were making
11750 an inline call. But, remap
11751 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
11752 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
11753
11754static tree
11755c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
11756 tree decl, tree placeholder)
11757{
11758 copy_body_data id;
11759 struct pointer_map_t *decl_map = pointer_map_create ();
11760
11761 *pointer_map_insert (decl_map, omp_decl1) = placeholder;
11762 *pointer_map_insert (decl_map, omp_decl2) = decl;
11763 memset (&id, 0, sizeof (id));
11764 id.src_fn = DECL_CONTEXT (omp_decl1);
11765 id.dst_fn = current_function_decl;
11766 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
11767 id.decl_map = decl_map;
11768
11769 id.copy_decl = copy_decl_no_change;
11770 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
11771 id.transform_new_cfg = true;
11772 id.transform_return_to_modify = false;
11773 id.transform_lang_insert_block = NULL;
11774 id.eh_lp_nr = 0;
11775 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
11776 pointer_map_destroy (decl_map);
11777 return stmt;
11778}
11779
11780/* Helper function of c_finish_omp_clauses, called via walk_tree.
11781 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
11782
11783static tree
11784c_find_omp_placeholder_r (tree *tp, int *, void *data)
11785{
11786 if (*tp == (tree) data)
11787 return *tp;
11788 return NULL_TREE;
11789}
11790
953ff289
DN
11791/* For all elements of CLAUSES, validate them vs OpenMP constraints.
11792 Remove any elements from the list that are invalid. */
11793
11794tree
11795c_finish_omp_clauses (tree clauses)
11796{
11797 bitmap_head generic_head, firstprivate_head, lastprivate_head;
acf0174b 11798 bitmap_head aligned_head;
f3316c6d 11799 tree c, t, *pc;
acf0174b
JJ
11800 bool branch_seen = false;
11801 bool copyprivate_seen = false;
11802 tree *nowait_clause = NULL;
953ff289
DN
11803
11804 bitmap_obstack_initialize (NULL);
11805 bitmap_initialize (&generic_head, &bitmap_default_obstack);
11806 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
11807 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 11808 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
953ff289
DN
11809
11810 for (pc = &clauses, c = clauses; c ; c = *pc)
11811 {
11812 bool remove = false;
11813 bool need_complete = false;
11814 bool need_implicitly_determined = false;
11815
aaf46ef9 11816 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
11817 {
11818 case OMP_CLAUSE_SHARED:
953ff289
DN
11819 need_implicitly_determined = true;
11820 goto check_dup_generic;
11821
11822 case OMP_CLAUSE_PRIVATE:
953ff289
DN
11823 need_complete = true;
11824 need_implicitly_determined = true;
11825 goto check_dup_generic;
11826
11827 case OMP_CLAUSE_REDUCTION:
953ff289
DN
11828 need_implicitly_determined = true;
11829 t = OMP_CLAUSE_DECL (c);
acf0174b 11830 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
652fea39
JJ
11831 && (FLOAT_TYPE_P (TREE_TYPE (t))
11832 || TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE))
953ff289
DN
11833 {
11834 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
11835 const char *r_name = NULL;
11836
11837 switch (r_code)
11838 {
11839 case PLUS_EXPR:
11840 case MULT_EXPR:
11841 case MINUS_EXPR:
652fea39 11842 break;
20906c66 11843 case MIN_EXPR:
652fea39
JJ
11844 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11845 r_name = "min";
11846 break;
20906c66 11847 case MAX_EXPR:
652fea39
JJ
11848 if (TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
11849 r_name = "max";
953ff289
DN
11850 break;
11851 case BIT_AND_EXPR:
11852 r_name = "&";
11853 break;
11854 case BIT_XOR_EXPR:
11855 r_name = "^";
11856 break;
11857 case BIT_IOR_EXPR:
11858 r_name = "|";
11859 break;
11860 case TRUTH_ANDIF_EXPR:
652fea39
JJ
11861 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11862 r_name = "&&";
953ff289
DN
11863 break;
11864 case TRUTH_ORIF_EXPR:
652fea39
JJ
11865 if (FLOAT_TYPE_P (TREE_TYPE (t)))
11866 r_name = "||";
953ff289
DN
11867 break;
11868 default:
11869 gcc_unreachable ();
11870 }
11871 if (r_name)
11872 {
c2255bc4
AH
11873 error_at (OMP_CLAUSE_LOCATION (c),
11874 "%qE has invalid type for %<reduction(%s)%>",
11875 t, r_name);
953ff289 11876 remove = true;
ee1d5a02 11877 break;
953ff289
DN
11878 }
11879 }
acf0174b
JJ
11880 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
11881 {
11882 error_at (OMP_CLAUSE_LOCATION (c),
11883 "user defined reduction not found for %qD", t);
11884 remove = true;
ee1d5a02 11885 break;
acf0174b
JJ
11886 }
11887 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
11888 {
11889 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
11890 tree type = TYPE_MAIN_VARIANT (TREE_TYPE (t));
11891 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
11892 VAR_DECL, NULL_TREE, type);
11893 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
11894 DECL_ARTIFICIAL (placeholder) = 1;
11895 DECL_IGNORED_P (placeholder) = 1;
11896 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
11897 c_mark_addressable (placeholder);
11898 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
11899 c_mark_addressable (OMP_CLAUSE_DECL (c));
11900 OMP_CLAUSE_REDUCTION_MERGE (c)
11901 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
11902 TREE_VEC_ELT (list, 0),
11903 TREE_VEC_ELT (list, 1),
11904 OMP_CLAUSE_DECL (c), placeholder);
11905 OMP_CLAUSE_REDUCTION_MERGE (c)
11906 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11907 void_type_node, NULL_TREE,
11908 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
11909 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
11910 if (TREE_VEC_LENGTH (list) == 6)
11911 {
11912 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
11913 c_mark_addressable (OMP_CLAUSE_DECL (c));
11914 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
11915 c_mark_addressable (placeholder);
11916 tree init = TREE_VEC_ELT (list, 5);
11917 if (init == error_mark_node)
11918 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
11919 OMP_CLAUSE_REDUCTION_INIT (c)
11920 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
11921 TREE_VEC_ELT (list, 3),
11922 OMP_CLAUSE_DECL (c), placeholder);
11923 if (TREE_VEC_ELT (list, 5) == error_mark_node)
11924 OMP_CLAUSE_REDUCTION_INIT (c)
11925 = build2 (INIT_EXPR, TREE_TYPE (t), t,
11926 OMP_CLAUSE_REDUCTION_INIT (c));
11927 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
11928 c_find_omp_placeholder_r,
11929 placeholder, NULL))
11930 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
11931 }
11932 else
11933 {
11934 tree init;
11935 if (AGGREGATE_TYPE_P (TREE_TYPE (t)))
11936 init = build_constructor (TREE_TYPE (t), NULL);
11937 else
11938 init = fold_convert (TREE_TYPE (t), integer_zero_node);
11939 OMP_CLAUSE_REDUCTION_INIT (c)
11940 = build2 (INIT_EXPR, TREE_TYPE (t), t, init);
11941 }
11942 OMP_CLAUSE_REDUCTION_INIT (c)
11943 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
11944 void_type_node, NULL_TREE,
11945 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
11946 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
11947 }
953ff289
DN
11948 goto check_dup_generic;
11949
11950 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
11951 copyprivate_seen = true;
11952 if (nowait_clause)
11953 {
11954 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
11955 "%<nowait%> clause must not be used together "
11956 "with %<copyprivate%>");
11957 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
11958 nowait_clause = NULL;
11959 }
953ff289
DN
11960 goto check_dup_generic;
11961
11962 case OMP_CLAUSE_COPYIN:
953ff289
DN
11963 t = OMP_CLAUSE_DECL (c);
11964 if (TREE_CODE (t) != VAR_DECL || !DECL_THREAD_LOCAL_P (t))
11965 {
c2255bc4
AH
11966 error_at (OMP_CLAUSE_LOCATION (c),
11967 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 11968 remove = true;
ee1d5a02 11969 break;
953ff289
DN
11970 }
11971 goto check_dup_generic;
11972
acf0174b
JJ
11973 case OMP_CLAUSE_LINEAR:
11974 t = OMP_CLAUSE_DECL (c);
11975 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
11976 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
11977 {
11978 error_at (OMP_CLAUSE_LOCATION (c),
11979 "linear clause applied to non-integral non-pointer "
11980 "variable with type %qT", TREE_TYPE (t));
11981 remove = true;
11982 break;
11983 }
11984 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
11985 {
11986 tree s = OMP_CLAUSE_LINEAR_STEP (c);
11987 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
11988 OMP_CLAUSE_DECL (c), s);
11989 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
11990 sizetype, s, OMP_CLAUSE_DECL (c));
11991 if (s == error_mark_node)
11992 s = size_one_node;
11993 OMP_CLAUSE_LINEAR_STEP (c) = s;
11994 }
11995 goto check_dup_generic;
11996
953ff289
DN
11997 check_dup_generic:
11998 t = OMP_CLAUSE_DECL (c);
11999 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12000 {
c2255bc4 12001 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
12002 "%qE is not a variable in clause %qs", t,
12003 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12004 remove = true;
12005 }
12006 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12007 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
12008 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12009 {
c2255bc4
AH
12010 error_at (OMP_CLAUSE_LOCATION (c),
12011 "%qE appears more than once in data clauses", t);
953ff289
DN
12012 remove = true;
12013 }
12014 else
12015 bitmap_set_bit (&generic_head, DECL_UID (t));
12016 break;
12017
12018 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
12019 t = OMP_CLAUSE_DECL (c);
12020 need_complete = true;
12021 need_implicitly_determined = true;
12022 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12023 {
c2255bc4
AH
12024 error_at (OMP_CLAUSE_LOCATION (c),
12025 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
12026 remove = true;
12027 }
12028 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12029 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
12030 {
c2255bc4
AH
12031 error_at (OMP_CLAUSE_LOCATION (c),
12032 "%qE appears more than once in data clauses", t);
953ff289
DN
12033 remove = true;
12034 }
12035 else
12036 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
12037 break;
12038
12039 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
12040 t = OMP_CLAUSE_DECL (c);
12041 need_complete = true;
12042 need_implicitly_determined = true;
12043 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12044 {
c2255bc4
AH
12045 error_at (OMP_CLAUSE_LOCATION (c),
12046 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
12047 remove = true;
12048 }
12049 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
12050 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
12051 {
c2255bc4
AH
12052 error_at (OMP_CLAUSE_LOCATION (c),
12053 "%qE appears more than once in data clauses", t);
953ff289
DN
12054 remove = true;
12055 }
12056 else
12057 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
12058 break;
12059
acf0174b
JJ
12060 case OMP_CLAUSE_ALIGNED:
12061 t = OMP_CLAUSE_DECL (c);
12062 if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12063 {
12064 error_at (OMP_CLAUSE_LOCATION (c),
12065 "%qE is not a variable in %<aligned%> clause", t);
12066 remove = true;
12067 }
5a9785fb
JJ
12068 else if (!POINTER_TYPE_P (TREE_TYPE (t))
12069 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
12070 {
12071 error_at (OMP_CLAUSE_LOCATION (c),
12072 "%qE in %<aligned%> clause is neither a pointer nor "
12073 "an array", t);
12074 remove = true;
12075 }
acf0174b
JJ
12076 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
12077 {
12078 error_at (OMP_CLAUSE_LOCATION (c),
12079 "%qE appears more than once in %<aligned%> clauses",
12080 t);
12081 remove = true;
12082 }
12083 else
12084 bitmap_set_bit (&aligned_head, DECL_UID (t));
12085 break;
12086
12087 case OMP_CLAUSE_DEPEND:
12088 t = OMP_CLAUSE_DECL (c);
12089 if (TREE_CODE (t) == TREE_LIST)
12090 {
12091 if (handle_omp_array_sections (c))
12092 remove = true;
12093 break;
12094 }
12095 if (t == error_mark_node)
12096 remove = true;
12097 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12098 {
12099 error_at (OMP_CLAUSE_LOCATION (c),
12100 "%qE is not a variable in %<depend%> clause", t);
12101 remove = true;
12102 }
12103 else if (!c_mark_addressable (t))
12104 remove = true;
12105 break;
12106
12107 case OMP_CLAUSE_MAP:
12108 case OMP_CLAUSE_TO:
12109 case OMP_CLAUSE_FROM:
12110 t = OMP_CLAUSE_DECL (c);
12111 if (TREE_CODE (t) == TREE_LIST)
12112 {
12113 if (handle_omp_array_sections (c))
12114 remove = true;
12115 else
12116 {
12117 t = OMP_CLAUSE_DECL (c);
b17a8b07 12118 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12119 {
12120 error_at (OMP_CLAUSE_LOCATION (c),
12121 "array section does not have mappable type "
12122 "in %qs clause",
12123 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12124 remove = true;
12125 }
12126 }
12127 break;
12128 }
12129 if (t == error_mark_node)
12130 remove = true;
12131 else if (TREE_CODE (t) != VAR_DECL && TREE_CODE (t) != PARM_DECL)
12132 {
12133 error_at (OMP_CLAUSE_LOCATION (c),
12134 "%qE is not a variable in %qs clause", t,
12135 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12136 remove = true;
12137 }
12138 else if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12139 {
12140 error_at (OMP_CLAUSE_LOCATION (c),
12141 "%qD is threadprivate variable in %qs clause", t,
12142 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12143 remove = true;
12144 }
12145 else if (!c_mark_addressable (t))
12146 remove = true;
b17a8b07
TS
12147 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12148 && OMP_CLAUSE_MAP_KIND (c) == OMP_CLAUSE_MAP_POINTER)
12149 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
12150 {
12151 error_at (OMP_CLAUSE_LOCATION (c),
12152 "%qD does not have a mappable type in %qs clause", t,
12153 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12154 remove = true;
12155 }
12156 else if (bitmap_bit_p (&generic_head, DECL_UID (t)))
12157 {
12158 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
12159 error ("%qD appears more than once in motion clauses", t);
12160 else
12161 error ("%qD appears more than once in map clauses", t);
12162 remove = true;
12163 }
12164 else
12165 bitmap_set_bit (&generic_head, DECL_UID (t));
12166 break;
12167
12168 case OMP_CLAUSE_UNIFORM:
12169 t = OMP_CLAUSE_DECL (c);
12170 if (TREE_CODE (t) != PARM_DECL)
12171 {
12172 if (DECL_P (t))
12173 error_at (OMP_CLAUSE_LOCATION (c),
12174 "%qD is not an argument in %<uniform%> clause", t);
12175 else
12176 error_at (OMP_CLAUSE_LOCATION (c),
12177 "%qE is not an argument in %<uniform%> clause", t);
12178 remove = true;
ee1d5a02 12179 break;
acf0174b 12180 }
ee1d5a02 12181 goto check_dup_generic;
acf0174b
JJ
12182
12183 case OMP_CLAUSE_NOWAIT:
12184 if (copyprivate_seen)
12185 {
12186 error_at (OMP_CLAUSE_LOCATION (c),
12187 "%<nowait%> clause must not be used together "
12188 "with %<copyprivate%>");
12189 remove = true;
12190 break;
12191 }
12192 nowait_clause = pc;
12193 pc = &OMP_CLAUSE_CHAIN (c);
12194 continue;
12195
953ff289
DN
12196 case OMP_CLAUSE_IF:
12197 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
12198 case OMP_CLAUSE_NUM_TEAMS:
12199 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 12200 case OMP_CLAUSE_SCHEDULE:
953ff289
DN
12201 case OMP_CLAUSE_ORDERED:
12202 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
12203 case OMP_CLAUSE_UNTIED:
12204 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
12205 case OMP_CLAUSE_FINAL:
12206 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
12207 case OMP_CLAUSE_SAFELEN:
12208 case OMP_CLAUSE_SIMDLEN:
12209 case OMP_CLAUSE_DEVICE:
12210 case OMP_CLAUSE_DIST_SCHEDULE:
12211 case OMP_CLAUSE_PARALLEL:
12212 case OMP_CLAUSE_FOR:
12213 case OMP_CLAUSE_SECTIONS:
12214 case OMP_CLAUSE_TASKGROUP:
12215 case OMP_CLAUSE_PROC_BIND:
12216 pc = &OMP_CLAUSE_CHAIN (c);
12217 continue;
12218
12219 case OMP_CLAUSE_INBRANCH:
12220 case OMP_CLAUSE_NOTINBRANCH:
12221 if (branch_seen)
12222 {
12223 error_at (OMP_CLAUSE_LOCATION (c),
12224 "%<inbranch%> clause is incompatible with "
12225 "%<notinbranch%>");
12226 remove = true;
12227 break;
12228 }
12229 branch_seen = true;
953ff289
DN
12230 pc = &OMP_CLAUSE_CHAIN (c);
12231 continue;
12232
12233 default:
12234 gcc_unreachable ();
12235 }
12236
12237 if (!remove)
12238 {
12239 t = OMP_CLAUSE_DECL (c);
12240
12241 if (need_complete)
12242 {
12243 t = require_complete_type (t);
12244 if (t == error_mark_node)
12245 remove = true;
12246 }
12247
12248 if (need_implicitly_determined)
12249 {
12250 const char *share_name = NULL;
12251
12252 if (TREE_CODE (t) == VAR_DECL && DECL_THREAD_LOCAL_P (t))
12253 share_name = "threadprivate";
12254 else switch (c_omp_predetermined_sharing (t))
12255 {
12256 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
12257 break;
12258 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
12259 /* const vars may be specified in firstprivate clause. */
12260 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
12261 && TREE_READONLY (t))
12262 break;
953ff289
DN
12263 share_name = "shared";
12264 break;
12265 case OMP_CLAUSE_DEFAULT_PRIVATE:
12266 share_name = "private";
12267 break;
12268 default:
12269 gcc_unreachable ();
12270 }
12271 if (share_name)
12272 {
c2255bc4
AH
12273 error_at (OMP_CLAUSE_LOCATION (c),
12274 "%qE is predetermined %qs for %qs",
acf0174b
JJ
12275 t, share_name,
12276 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
12277 remove = true;
12278 }
12279 }
12280 }
12281
12282 if (remove)
12283 *pc = OMP_CLAUSE_CHAIN (c);
12284 else
12285 pc = &OMP_CLAUSE_CHAIN (c);
12286 }
12287
12288 bitmap_obstack_release (NULL);
12289 return clauses;
12290}
9ae165a0 12291
0a35513e
AH
12292/* Create a transaction node. */
12293
12294tree
12295c_finish_transaction (location_t loc, tree block, int flags)
12296{
12297 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
12298 if (flags & TM_STMT_ATTR_OUTER)
12299 TRANSACTION_EXPR_OUTER (stmt) = 1;
12300 if (flags & TM_STMT_ATTR_RELAXED)
12301 TRANSACTION_EXPR_RELAXED (stmt) = 1;
12302 return add_stmt (stmt);
12303}
12304
9ae165a0
DG
12305/* Make a variant type in the proper way for C/C++, propagating qualifiers
12306 down to the element type of an array. */
12307
12308tree
12309c_build_qualified_type (tree type, int type_quals)
12310{
12311 if (type == error_mark_node)
12312 return type;
12313
12314 if (TREE_CODE (type) == ARRAY_TYPE)
12315 {
12316 tree t;
12317 tree element_type = c_build_qualified_type (TREE_TYPE (type),
12318 type_quals);
12319
12320 /* See if we already have an identically qualified type. */
12321 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12322 {
12323 if (TYPE_QUALS (strip_array_types (t)) == type_quals
12324 && TYPE_NAME (t) == TYPE_NAME (type)
12325 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
12326 && attribute_list_equal (TYPE_ATTRIBUTES (t),
12327 TYPE_ATTRIBUTES (type)))
12328 break;
12329 }
12330 if (!t)
12331 {
12332 tree domain = TYPE_DOMAIN (type);
12333
12334 t = build_variant_type_copy (type);
12335 TREE_TYPE (t) = element_type;
12336
12337 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
12338 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
12339 SET_TYPE_STRUCTURAL_EQUALITY (t);
12340 else if (TYPE_CANONICAL (element_type) != element_type
12341 || (domain && TYPE_CANONICAL (domain) != domain))
12342 {
b8698a0f 12343 tree unqualified_canon
9ae165a0 12344 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 12345 domain? TYPE_CANONICAL (domain)
9ae165a0 12346 : NULL_TREE);
b8698a0f 12347 TYPE_CANONICAL (t)
9ae165a0
DG
12348 = c_build_qualified_type (unqualified_canon, type_quals);
12349 }
12350 else
12351 TYPE_CANONICAL (t) = t;
12352 }
12353 return t;
12354 }
12355
12356 /* A restrict-qualified pointer type must be a pointer to object or
12357 incomplete type. Note that the use of POINTER_TYPE_P also allows
12358 REFERENCE_TYPEs, which is appropriate for C++. */
12359 if ((type_quals & TYPE_QUAL_RESTRICT)
12360 && (!POINTER_TYPE_P (type)
12361 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
12362 {
12363 error ("invalid use of %<restrict%>");
12364 type_quals &= ~TYPE_QUAL_RESTRICT;
12365 }
12366
12367 return build_qualified_type (type, type_quals);
12368}
72b5577d
ILT
12369
12370/* Build a VA_ARG_EXPR for the C parser. */
12371
12372tree
c2255bc4 12373c_build_va_arg (location_t loc, tree expr, tree type)
72b5577d
ILT
12374{
12375 if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
12376 warning_at (loc, OPT_Wc___compat,
12377 "C++ requires promoted type, not enum type, in %<va_arg%>");
c2255bc4 12378 return build_va_arg (loc, expr, type);
72b5577d 12379}
acf0174b
JJ
12380
12381/* Return truthvalue of whether T1 is the same tree structure as T2.
12382 Return 1 if they are the same. Return 0 if they are different. */
12383
12384bool
12385c_tree_equal (tree t1, tree t2)
12386{
12387 enum tree_code code1, code2;
12388
12389 if (t1 == t2)
12390 return true;
12391 if (!t1 || !t2)
12392 return false;
12393
12394 for (code1 = TREE_CODE (t1);
12395 CONVERT_EXPR_CODE_P (code1)
12396 || code1 == NON_LVALUE_EXPR;
12397 code1 = TREE_CODE (t1))
12398 t1 = TREE_OPERAND (t1, 0);
12399 for (code2 = TREE_CODE (t2);
12400 CONVERT_EXPR_CODE_P (code2)
12401 || code2 == NON_LVALUE_EXPR;
12402 code2 = TREE_CODE (t2))
12403 t2 = TREE_OPERAND (t2, 0);
12404
12405 /* They might have become equal now. */
12406 if (t1 == t2)
12407 return true;
12408
12409 if (code1 != code2)
12410 return false;
12411
12412 switch (code1)
12413 {
12414 case INTEGER_CST:
807e902e 12415 return wi::eq_p (t1, t2);
acf0174b
JJ
12416
12417 case REAL_CST:
12418 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
12419
12420 case STRING_CST:
12421 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
12422 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
12423 TREE_STRING_LENGTH (t1));
12424
12425 case FIXED_CST:
12426 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
12427 TREE_FIXED_CST (t2));
12428
12429 case COMPLEX_CST:
12430 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
12431 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
12432
12433 case VECTOR_CST:
12434 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
12435
12436 case CONSTRUCTOR:
12437 /* We need to do this when determining whether or not two
12438 non-type pointer to member function template arguments
12439 are the same. */
12440 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
12441 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
12442 return false;
12443 {
12444 tree field, value;
12445 unsigned int i;
12446 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
12447 {
12448 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
12449 if (!c_tree_equal (field, elt2->index)
12450 || !c_tree_equal (value, elt2->value))
12451 return false;
12452 }
12453 }
12454 return true;
12455
12456 case TREE_LIST:
12457 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
12458 return false;
12459 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
12460 return false;
12461 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
12462
12463 case SAVE_EXPR:
12464 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12465
12466 case CALL_EXPR:
12467 {
12468 tree arg1, arg2;
12469 call_expr_arg_iterator iter1, iter2;
12470 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
12471 return false;
12472 for (arg1 = first_call_expr_arg (t1, &iter1),
12473 arg2 = first_call_expr_arg (t2, &iter2);
12474 arg1 && arg2;
12475 arg1 = next_call_expr_arg (&iter1),
12476 arg2 = next_call_expr_arg (&iter2))
12477 if (!c_tree_equal (arg1, arg2))
12478 return false;
12479 if (arg1 || arg2)
12480 return false;
12481 return true;
12482 }
12483
12484 case TARGET_EXPR:
12485 {
12486 tree o1 = TREE_OPERAND (t1, 0);
12487 tree o2 = TREE_OPERAND (t2, 0);
12488
12489 /* Special case: if either target is an unallocated VAR_DECL,
12490 it means that it's going to be unified with whatever the
12491 TARGET_EXPR is really supposed to initialize, so treat it
12492 as being equivalent to anything. */
12493 if (TREE_CODE (o1) == VAR_DECL && DECL_NAME (o1) == NULL_TREE
12494 && !DECL_RTL_SET_P (o1))
12495 /*Nop*/;
12496 else if (TREE_CODE (o2) == VAR_DECL && DECL_NAME (o2) == NULL_TREE
12497 && !DECL_RTL_SET_P (o2))
12498 /*Nop*/;
12499 else if (!c_tree_equal (o1, o2))
12500 return false;
12501
12502 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
12503 }
12504
12505 case COMPONENT_REF:
12506 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
12507 return false;
12508 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
12509
12510 case PARM_DECL:
12511 case VAR_DECL:
12512 case CONST_DECL:
12513 case FIELD_DECL:
12514 case FUNCTION_DECL:
12515 case IDENTIFIER_NODE:
12516 case SSA_NAME:
12517 return false;
12518
12519 case TREE_VEC:
12520 {
12521 unsigned ix;
12522 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
12523 return false;
12524 for (ix = TREE_VEC_LENGTH (t1); ix--;)
12525 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
12526 TREE_VEC_ELT (t2, ix)))
12527 return false;
12528 return true;
12529 }
12530
12531 default:
12532 break;
12533 }
12534
12535 switch (TREE_CODE_CLASS (code1))
12536 {
12537 case tcc_unary:
12538 case tcc_binary:
12539 case tcc_comparison:
12540 case tcc_expression:
12541 case tcc_vl_exp:
12542 case tcc_reference:
12543 case tcc_statement:
12544 {
12545 int i, n = TREE_OPERAND_LENGTH (t1);
12546
12547 switch (code1)
12548 {
12549 case PREINCREMENT_EXPR:
12550 case PREDECREMENT_EXPR:
12551 case POSTINCREMENT_EXPR:
12552 case POSTDECREMENT_EXPR:
12553 n = 1;
12554 break;
12555 case ARRAY_REF:
12556 n = 2;
12557 break;
12558 default:
12559 break;
12560 }
12561
12562 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
12563 && n != TREE_OPERAND_LENGTH (t2))
12564 return false;
12565
12566 for (i = 0; i < n; ++i)
12567 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
12568 return false;
12569
12570 return true;
12571 }
12572
12573 case tcc_type:
12574 return comptypes (t1, t2);
12575 default:
12576 gcc_unreachable ();
12577 }
12578 /* We can get here with --disable-checking. */
12579 return false;
12580}
12893402
BI
12581
12582/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
12583 spawn-helper and BODY is the newly created body for FNDECL. */
12584
12585void
12586cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
12587{
12588 tree list = alloc_stmt_list ();
12589 tree frame = make_cilk_frame (fndecl);
12590 tree dtor = create_cilk_function_exit (frame, false, true);
12591 add_local_decl (cfun, frame);
12592
12593 DECL_SAVED_TREE (fndecl) = list;
12594 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
12595 frame);
12596 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
12597 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
12598
12599 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
12600 append_to_statement_list (detach_expr, &body_list);
12601
12602 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
12603 body = fold_build_cleanup_point_expr (void_type_node, body);
12604
12605 append_to_statement_list (body, &body_list);
12606 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
12607 body_list, dtor), &list);
12608}