]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
Split omp-low into multiple files
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
628fa4f8 1/* Build expressions with type checking for C compiler.
f1717362 2 Copyright (C) 1987-2016 Free Software Foundation, Inc.
628fa4f8 3
f12b58b3 4This file is part of GCC.
628fa4f8 5
f12b58b3 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
8c4c00c1 8Software Foundation; either version 3, or (at your option) any later
f12b58b3 9version.
628fa4f8 10
f12b58b3 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.
628fa4f8 15
16You should have received a copy of the GNU General Public License
8c4c00c1 17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
628fa4f8 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,
c857cd60 24 and some optimization. */
628fa4f8 25
26#include "config.h"
405711de 27#include "system.h"
805e22b2 28#include "coretypes.h"
ad7b10a2 29#include "memmodel.h"
4cba6f60 30#include "target.h"
31#include "function.h"
32#include "bitmap.h"
4cba6f60 33#include "c-tree.h"
34#include "gimple-expr.h"
35#include "predict.h"
9ed99284 36#include "stor-layout.h"
37#include "trans-mem.h"
38#include "varasm.h"
39#include "stmt.h"
d1b92730 40#include "langhooks.h"
874993a5 41#include "c-lang.h"
be2828ce 42#include "intl.h"
2363ef00 43#include "tree-iterator.h"
a8783bee 44#include "gimplify.h"
bc7bff74 45#include "tree-inline.h"
4954efd4 46#include "omp-general.h"
6c536c4f 47#include "c-family/c-objc.h"
9e46467d 48#include "c-family/c-ubsan.h"
433e804e 49#include "cilk.h"
ca4c3545 50#include "gomp-constants.h"
f0d77991 51#include "spellcheck-tree.h"
c0bf500c 52#include "gcc-rich-location.h"
2363ef00 53
245d6740 54/* Possible cases of implicit bad conversions. Used to select
55 diagnostic messages in convert_for_assignment. */
56enum impl_conv {
57 ic_argpass,
58 ic_assign,
59 ic_init,
60 ic_return
61};
62
9823d3a9 63/* The level of nesting inside "__alignof__". */
64int in_alignof;
65
66/* The level of nesting inside "sizeof". */
67int in_sizeof;
68
69/* The level of nesting inside "typeof". */
70int in_typeof;
628fa4f8 71
f003f5dc 72/* The argument of last parsed sizeof expression, only to be tested
73 if expr.original_code == SIZEOF_EXPR. */
74tree c_last_sizeof_arg;
75
95cdf3fd 76/* Nonzero if we might need to print a "missing braces around
77 initializer" message within this initializer. */
78static int found_missing_braces;
7de1d3ba 79
16cb78b3 80static int require_constant_value;
81static int require_constant_elements;
82
9f627b1a 83static bool null_pointer_constant_p (const_tree);
5a2784f8 84static tree qualify_type (tree, tree);
ce3765bf 85static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
86 bool *);
d5b637fa 87static int comp_target_types (location_t, tree, tree);
ce3765bf 88static int function_types_compatible_p (const_tree, const_tree, bool *,
89 bool *);
90static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
5a2784f8 91static tree lookup_field (tree, tree);
ec761d5a 92static int convert_arguments (location_t, vec<location_t>, tree,
93 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
94 tree);
389dd41b 95static tree pointer_diff (location_t, tree, tree);
22a75734 96static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
d5b637fa 97 enum impl_conv, bool, tree, tree, int);
5a2784f8 98static tree valid_compound_expr_initializer (tree, tree);
99static void push_string (const char *);
100static void push_member_name (tree);
5a2784f8 101static int spelling_length (void);
102static char *print_spelling (char *);
d44f2f7c 103static void warning_init (location_t, int, const char *);
e60a6f7b 104static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
adc93f31 105static void output_init_element (location_t, tree, tree, bool, tree, tree, int,
106 bool, struct obstack *);
759791ee 107static void output_pending_init_elements (int, struct obstack *);
53de1631 108static int set_designator (location_t, int, struct obstack *);
759791ee 109static void push_range_stack (tree, struct obstack *);
d44f2f7c 110static void add_pending_init (location_t, tree, tree, tree, bool,
111 struct obstack *);
759791ee 112static void set_nonincremental_init (struct obstack *);
113static void set_nonincremental_init_from_string (tree, struct obstack *);
114static tree find_init_member (tree, struct obstack *);
d4e60318 115static void readonly_warning (tree, enum lvalue_use);
fdd84b77 116static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
91ffb709 117static void record_maybe_used_decl (tree);
ce3765bf 118static int comptypes_internal (const_tree, const_tree, bool *, bool *);
79f925ed 119\f
120/* Return true if EXP is a null pointer constant, false otherwise. */
121
122static bool
9f627b1a 123null_pointer_constant_p (const_tree expr)
79f925ed 124{
125 /* This should really operate on c_expr structures, but they aren't
126 yet available everywhere required. */
127 tree type = TREE_TYPE (expr);
128 return (TREE_CODE (expr) == INTEGER_CST
ca24357c 129 && !TREE_OVERFLOW (expr)
79f925ed 130 && integer_zerop (expr)
131 && (INTEGRAL_TYPE_P (type)
132 || (TREE_CODE (type) == POINTER_TYPE
133 && VOID_TYPE_P (TREE_TYPE (type))
134 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
135}
a75b1c71 136
137/* EXPR may appear in an unevaluated part of an integer constant
138 expression, but not in an evaluated part. Wrap it in a
139 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
140 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
141
142static tree
143note_integer_operands (tree expr)
144{
145 tree ret;
146 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
147 {
148 ret = copy_node (expr);
149 TREE_OVERFLOW (ret) = 1;
150 }
151 else
152 {
153 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
154 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
155 }
156 return ret;
157}
158
9a8bed72 159/* Having checked whether EXPR may appear in an unevaluated part of an
160 integer constant expression and found that it may, remove any
161 C_MAYBE_CONST_EXPR noting this fact and return the resulting
162 expression. */
163
164static inline tree
165remove_c_maybe_const_expr (tree expr)
166{
167 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
168 return C_MAYBE_CONST_EXPR_EXPR (expr);
169 else
170 return expr;
171}
172
7e58afa1 173\f/* This is a cache to hold if two types are compatible or not. */
174
175struct tagged_tu_seen_cache {
176 const struct tagged_tu_seen_cache * next;
9f627b1a 177 const_tree t1;
178 const_tree t2;
7e58afa1 179 /* The return value of tagged_types_tu_compatible_p if we had seen
180 these two types already. */
181 int val;
182};
183
184static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
185static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
186
22a3f7bd 187/* Do `exp = require_complete_type (loc, exp);' to make sure exp
188 does not have an incomplete type. (That includes void types.)
189 LOC is the location of the use. */
628fa4f8 190
191tree
22a3f7bd 192require_complete_type (location_t loc, tree value)
628fa4f8 193{
194 tree type = TREE_TYPE (value);
195
224bdd51 196 if (error_operand_p (value))
1e4f99e8 197 return error_mark_node;
198
628fa4f8 199 /* First, detect a valid value with a complete type. */
4b72716d 200 if (COMPLETE_TYPE_P (type))
628fa4f8 201 return value;
202
22a3f7bd 203 c_incomplete_type_error (loc, value, type);
628fa4f8 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)
22a3f7bd 209 and TYPE is the type that was invalid. LOC is the location for
210 the error. */
628fa4f8 211
212void
22a3f7bd 213c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
628fa4f8 214{
628fa4f8 215 /* Avoid duplicate error message. */
216 if (TREE_CODE (type) == ERROR_MARK)
217 return;
218
f48c7f4a 219 if (value != 0 && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
22a3f7bd 220 error_at (loc, "%qD has an incomplete type %qT", value, type);
628fa4f8 221 else
222 {
223 retry:
224 /* We must print an error message. Be clever about what it says. */
225
226 switch (TREE_CODE (type))
227 {
228 case RECORD_TYPE:
628fa4f8 229 case UNION_TYPE:
628fa4f8 230 case ENUMERAL_TYPE:
628fa4f8 231 break;
232
233 case VOID_TYPE:
22a3f7bd 234 error_at (loc, "invalid use of void expression");
628fa4f8 235 return;
236
237 case ARRAY_TYPE:
238 if (TYPE_DOMAIN (type))
239 {
e40a1d5c 240 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
241 {
22a3f7bd 242 error_at (loc, "invalid use of flexible array member");
e40a1d5c 243 return;
244 }
628fa4f8 245 type = TREE_TYPE (type);
246 goto retry;
247 }
22a3f7bd 248 error_at (loc, "invalid use of array with unspecified bounds");
628fa4f8 249 return;
250
251 default:
231bd014 252 gcc_unreachable ();
628fa4f8 253 }
254
255 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
22a3f7bd 256 error_at (loc, "invalid use of undefined type %qT", type);
628fa4f8 257 else
258 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
22a3f7bd 259 error_at (loc, "invalid use of incomplete typedef %qT", type);
628fa4f8 260 }
261}
262
63c62881 263/* Given a type, apply default promotions wrt unnamed function
264 arguments and return the new type. */
265
266tree
2b30d46c 267c_type_promotes_to (tree type)
63c62881 268{
b560fabd 269 tree ret = NULL_TREE;
63c62881 270
b560fabd 271 if (TYPE_MAIN_VARIANT (type) == float_type_node)
272 ret = double_type_node;
273 else if (c_promoting_integer_type_p (type))
63c62881 274 {
275 /* Preserve unsignedness if not really getting any wider. */
78a8ed03 276 if (TYPE_UNSIGNED (type)
a0c938f0 277 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
b560fabd 278 ret = unsigned_type_node;
279 else
280 ret = integer_type_node;
63c62881 281 }
282
b560fabd 283 if (ret != NULL_TREE)
284 return (TYPE_ATOMIC (type)
285 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
286 : ret);
287
63c62881 288 return type;
289}
290
6d5d708e 291/* Return true if between two named address spaces, whether there is a superset
292 named address space that encompasses both address spaces. If there is a
293 superset, return which address space is the superset. */
294
295static bool
296addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
297{
298 if (as1 == as2)
299 {
300 *common = as1;
301 return true;
302 }
303 else if (targetm.addr_space.subset_p (as1, as2))
304 {
305 *common = as2;
306 return true;
307 }
308 else if (targetm.addr_space.subset_p (as2, as1))
309 {
310 *common = as1;
311 return true;
312 }
313 else
314 return false;
315}
316
628fa4f8 317/* Return a variant of TYPE which has all the type qualifiers of LIKE
318 as well as those of TYPE. */
319
320static tree
2b30d46c 321qualify_type (tree type, tree like)
628fa4f8 322{
6d5d708e 323 addr_space_t as_type = TYPE_ADDR_SPACE (type);
324 addr_space_t as_like = TYPE_ADDR_SPACE (like);
325 addr_space_t as_common;
326
327 /* If the two named address spaces are different, determine the common
328 superset address space. If there isn't one, raise an error. */
329 if (!addr_space_superset (as_type, as_like, &as_common))
330 {
331 as_common = as_type;
332 error ("%qT and %qT are in disjoint named address spaces",
333 type, like);
334 }
335
2b30d46c 336 return c_build_qualified_type (type,
6d5d708e 337 TYPE_QUALS_NO_ADDR_SPACE (type)
b560fabd 338 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
6d5d708e 339 | ENCODE_QUAL_ADDR_SPACE (as_common));
628fa4f8 340}
32d33ab2 341
342/* Return true iff the given tree T is a variable length array. */
343
344bool
f8fd23c0 345c_vla_type_p (const_tree t)
32d33ab2 346{
347 if (TREE_CODE (t) == ARRAY_TYPE
348 && C_TYPE_VARIABLE_SIZE (t))
349 return true;
350 return false;
351}
628fa4f8 352\f
1ea7fa60 353/* Return the composite type of two compatible types.
3ac2c7b8 354
1ea7fa60 355 We assume that comptypes has already been done and returned
356 nonzero; if that isn't so, this may crash. In particular, we
357 assume that qualifiers match. */
628fa4f8 358
359tree
1ea7fa60 360composite_type (tree t1, tree t2)
628fa4f8 361{
19cb6b50 362 enum tree_code code1;
363 enum tree_code code2;
8fc98180 364 tree attributes;
628fa4f8 365
366 /* Save time if the two types are the same. */
367
368 if (t1 == t2) return t1;
369
370 /* If one type is nonsense, use the other. */
371 if (t1 == error_mark_node)
372 return t2;
373 if (t2 == error_mark_node)
374 return t1;
375
1ea7fa60 376 code1 = TREE_CODE (t1);
377 code2 = TREE_CODE (t2);
378
0bf60c2b 379 /* Merge the attributes. */
883b2e73 380 attributes = targetm.merge_type_attributes (t1, t2);
8fc98180 381
1ea7fa60 382 /* If one is an enumerated type and the other is the compatible
383 integer type, the composite type might be either of the two
384 (DR#013 question 3). For consistency, use the enumerated type as
385 the composite type. */
628fa4f8 386
1ea7fa60 387 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
388 return t1;
389 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
390 return t2;
1d790105 391
231bd014 392 gcc_assert (code1 == code2);
6b854be5 393
628fa4f8 394 switch (code1)
395 {
628fa4f8 396 case POINTER_TYPE:
1ea7fa60 397 /* For two pointers, do this recursively on the target type. */
628fa4f8 398 {
a5b1863e 399 tree pointed_to_1 = TREE_TYPE (t1);
400 tree pointed_to_2 = TREE_TYPE (t2);
1ea7fa60 401 tree target = composite_type (pointed_to_1, pointed_to_2);
b589b3b4 402 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
5be41d48 403 t1 = build_type_attribute_variant (t1, attributes);
404 return qualify_type (t1, t2);
628fa4f8 405 }
628fa4f8 406
407 case ARRAY_TYPE:
408 {
1ea7fa60 409 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
aebc8537 410 int quals;
411 tree unqual_elt;
a2f96aea 412 tree d1 = TYPE_DOMAIN (t1);
413 tree d2 = TYPE_DOMAIN (t2);
414 bool d1_variable, d2_variable;
415 bool d1_zero, d2_zero;
e5d71db4 416 bool t1_complete, t2_complete;
aebc8537 417
4e7c4787 418 /* We should not have any type quals on arrays at all. */
6d5d708e 419 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
420 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
a0c938f0 421
e5d71db4 422 t1_complete = COMPLETE_TYPE_P (t1);
423 t2_complete = COMPLETE_TYPE_P (t2);
424
a2f96aea 425 d1_zero = d1 == 0 || !TYPE_MAX_VALUE (d1);
426 d2_zero = d2 == 0 || !TYPE_MAX_VALUE (d2);
427
428 d1_variable = (!d1_zero
429 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
430 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
431 d2_variable = (!d2_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
32d33ab2 434 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
435 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
a2f96aea 436
628fa4f8 437 /* Save space: see if the result is identical to one of the args. */
a2f96aea 438 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
439 && (d2_variable || d2_zero || !d1_variable))
8fc98180 440 return build_type_attribute_variant (t1, attributes);
a2f96aea 441 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
442 && (d1_variable || d1_zero || !d2_variable))
8fc98180 443 return build_type_attribute_variant (t2, attributes);
a0c938f0 444
4e7c4787 445 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
446 return build_type_attribute_variant (t1, attributes);
447 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
448 return build_type_attribute_variant (t2, attributes);
a0c938f0 449
aebc8537 450 /* Merge the element types, and have a size if either arg has
451 one. We may have qualifiers on the element types. To set
452 up TYPE_MAIN_VARIANT correctly, we need to form the
453 composite of the unqualified types and add the qualifiers
454 back at the end. */
455 quals = TYPE_QUALS (strip_array_types (elt));
456 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
457 t1 = build_array_type (unqual_elt,
a2f96aea 458 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
459 && (d2_variable
460 || d2_zero
461 || !d1_variable))
462 ? t1
463 : t2));
e5d71db4 464 /* Ensure a composite type involving a zero-length array type
465 is a zero-length type not an incomplete type. */
466 if (d1_zero && d2_zero
467 && (t1_complete || t2_complete)
468 && !COMPLETE_TYPE_P (t1))
469 {
470 TYPE_SIZE (t1) = bitsize_zero_node;
471 TYPE_SIZE_UNIT (t1) = size_zero_node;
472 }
aebc8537 473 t1 = c_build_qualified_type (t1, quals);
4e7c4787 474 return build_type_attribute_variant (t1, attributes);
628fa4f8 475 }
476
9369a09f 477 case ENUMERAL_TYPE:
478 case RECORD_TYPE:
479 case UNION_TYPE:
480 if (attributes != NULL)
481 {
482 /* Try harder not to create a new aggregate type. */
483 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
484 return t1;
485 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
486 return t2;
487 }
488 return build_type_attribute_variant (t1, attributes);
489
628fa4f8 490 case FUNCTION_TYPE:
491 /* Function types: prefer the one that specified arg types.
492 If both do, merge the arg types. Also merge the return types. */
493 {
1ea7fa60 494 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
628fa4f8 495 tree p1 = TYPE_ARG_TYPES (t1);
496 tree p2 = TYPE_ARG_TYPES (t2);
497 int len;
498 tree newargs, n;
499 int i;
500
501 /* Save space: see if the result is identical to one of the args. */
84166705 502 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
8fc98180 503 return build_type_attribute_variant (t1, attributes);
84166705 504 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
8fc98180 505 return build_type_attribute_variant (t2, attributes);
628fa4f8 506
507 /* Simple way if one arg fails to specify argument types. */
508 if (TYPE_ARG_TYPES (t1) == 0)
8fc98180 509 {
5be41d48 510 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
511 t1 = build_type_attribute_variant (t1, attributes);
512 return qualify_type (t1, t2);
8fc98180 513 }
628fa4f8 514 if (TYPE_ARG_TYPES (t2) == 0)
8fc98180 515 {
516 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
5be41d48 517 t1 = build_type_attribute_variant (t1, attributes);
518 return qualify_type (t1, t2);
8fc98180 519 }
628fa4f8 520
521 /* If both args specify argument types, we must merge the two
522 lists, argument by argument. */
6e11a41e 523
29a514fa 524 for (len = 0, newargs = p1;
525 newargs && newargs != void_list_node;
526 len++, newargs = TREE_CHAIN (newargs))
527 ;
628fa4f8 528
529 for (i = 0; i < len; i++)
7224cf20 530 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
628fa4f8 531
532 n = newargs;
533
29a514fa 534 for (; p1 && p1 != void_list_node;
628fa4f8 535 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
536 {
537 /* A null type means arg type is not specified.
538 Take whatever the other function type has. */
539 if (TREE_VALUE (p1) == 0)
540 {
541 TREE_VALUE (n) = TREE_VALUE (p2);
542 goto parm_done;
543 }
544 if (TREE_VALUE (p2) == 0)
545 {
546 TREE_VALUE (n) = TREE_VALUE (p1);
547 goto parm_done;
548 }
2b30d46c 549
628fa4f8 550 /* Given wait (union {union wait *u; int *i} *)
551 and wait (union wait *),
552 prefer union wait * as type of parm. */
553 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
554 && TREE_VALUE (p1) != TREE_VALUE (p2))
555 {
556 tree memb;
c75da5d5 557 tree mv2 = TREE_VALUE (p2);
558 if (mv2 && mv2 != error_mark_node
559 && TREE_CODE (mv2) != ARRAY_TYPE)
560 mv2 = TYPE_MAIN_VARIANT (mv2);
628fa4f8 561 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
1767a056 562 memb; memb = DECL_CHAIN (memb))
c75da5d5 563 {
564 tree mv3 = TREE_TYPE (memb);
565 if (mv3 && mv3 != error_mark_node
566 && TREE_CODE (mv3) != ARRAY_TYPE)
567 mv3 = TYPE_MAIN_VARIANT (mv3);
568 if (comptypes (mv3, mv2))
569 {
570 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
571 TREE_VALUE (p2));
29438999 572 pedwarn (input_location, OPT_Wpedantic,
8864917d 573 "function types not truly compatible in ISO C");
c75da5d5 574 goto parm_done;
575 }
576 }
628fa4f8 577 }
578 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
579 && TREE_VALUE (p2) != TREE_VALUE (p1))
580 {
581 tree memb;
c75da5d5 582 tree mv1 = TREE_VALUE (p1);
583 if (mv1 && mv1 != error_mark_node
584 && TREE_CODE (mv1) != ARRAY_TYPE)
585 mv1 = TYPE_MAIN_VARIANT (mv1);
628fa4f8 586 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
1767a056 587 memb; memb = DECL_CHAIN (memb))
c75da5d5 588 {
589 tree mv3 = TREE_TYPE (memb);
590 if (mv3 && mv3 != error_mark_node
591 && TREE_CODE (mv3) != ARRAY_TYPE)
592 mv3 = TYPE_MAIN_VARIANT (mv3);
593 if (comptypes (mv3, mv1))
594 {
595 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
596 TREE_VALUE (p1));
29438999 597 pedwarn (input_location, OPT_Wpedantic,
8864917d 598 "function types not truly compatible in ISO C");
c75da5d5 599 goto parm_done;
600 }
601 }
628fa4f8 602 }
1ea7fa60 603 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
628fa4f8 604 parm_done: ;
605 }
606
8fc98180 607 t1 = build_function_type (valtype, newargs);
5be41d48 608 t1 = qualify_type (t1, t2);
628fa4f8 609 }
c7afb782 610 /* FALLTHRU */
628fa4f8 611
612 default:
8fc98180 613 return build_type_attribute_variant (t1, attributes);
628fa4f8 614 }
615
616}
1ea7fa60 617
618/* Return the type of a conditional expression between pointers to
619 possibly differently qualified versions of compatible types.
620
621 We assume that comp_target_types has already been done and returned
622 nonzero; if that isn't so, this may crash. */
623
624static tree
625common_pointer_type (tree t1, tree t2)
626{
627 tree attributes;
aebc8537 628 tree pointed_to_1, mv1;
629 tree pointed_to_2, mv2;
1ea7fa60 630 tree target;
36d43c4a 631 unsigned target_quals;
6d5d708e 632 addr_space_t as1, as2, as_common;
633 int quals1, quals2;
1ea7fa60 634
635 /* Save time if the two types are the same. */
636
637 if (t1 == t2) return t1;
638
639 /* If one type is nonsense, use the other. */
640 if (t1 == error_mark_node)
641 return t2;
642 if (t2 == error_mark_node)
643 return t1;
644
231bd014 645 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
a0c938f0 646 && TREE_CODE (t2) == POINTER_TYPE);
1ea7fa60 647
648 /* Merge the attributes. */
649 attributes = targetm.merge_type_attributes (t1, t2);
650
651 /* Find the composite type of the target types, and combine the
aebc8537 652 qualifiers of the two types' targets. Do not lose qualifiers on
653 array element types by taking the TYPE_MAIN_VARIANT. */
654 mv1 = pointed_to_1 = TREE_TYPE (t1);
655 mv2 = pointed_to_2 = TREE_TYPE (t2);
656 if (TREE_CODE (mv1) != ARRAY_TYPE)
657 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
658 if (TREE_CODE (mv2) != ARRAY_TYPE)
659 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
660 target = composite_type (mv1, mv2);
36d43c4a 661
8a8211df 662 /* Strip array types to get correct qualifier for pointers to arrays */
663 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
664 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
665
36d43c4a 666 /* For function types do not merge const qualifiers, but drop them
667 if used inconsistently. The middle-end uses these to mark const
668 and noreturn functions. */
669 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
6d5d708e 670 target_quals = (quals1 & quals2);
36d43c4a 671 else
6d5d708e 672 target_quals = (quals1 | quals2);
673
674 /* If the two named address spaces are different, determine the common
675 superset address space. This is guaranteed to exist due to the
676 assumption that comp_target_type returned non-zero. */
677 as1 = TYPE_ADDR_SPACE (pointed_to_1);
678 as2 = TYPE_ADDR_SPACE (pointed_to_2);
679 if (!addr_space_superset (as1, as2, &as_common))
680 gcc_unreachable ();
681
682 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
683
36d43c4a 684 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
1ea7fa60 685 return build_type_attribute_variant (t1, attributes);
686}
687
688/* Return the common type for two arithmetic types under the usual
689 arithmetic conversions. The default conversions have already been
690 applied, and enumerated types converted to their compatible integer
691 types. The resulting type is unqualified and has no attributes.
692
693 This is the type for the result of most arithmetic operations
694 if the operands have the given two types. */
695
79d37242 696static tree
697c_common_type (tree t1, tree t2)
1ea7fa60 698{
699 enum tree_code code1;
700 enum tree_code code2;
701
702 /* If one type is nonsense, use the other. */
703 if (t1 == error_mark_node)
704 return t2;
705 if (t2 == error_mark_node)
706 return t1;
707
708 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
709 t1 = TYPE_MAIN_VARIANT (t1);
710
711 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
712 t2 = TYPE_MAIN_VARIANT (t2);
713
714 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
715 t1 = build_type_attribute_variant (t1, NULL_TREE);
716
717 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
718 t2 = build_type_attribute_variant (t2, NULL_TREE);
719
720 /* Save time if the two types are the same. */
721
722 if (t1 == t2) return t1;
723
724 code1 = TREE_CODE (t1);
725 code2 = TREE_CODE (t2);
726
231bd014 727 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
9421ebb9 728 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
729 || code1 == INTEGER_TYPE);
231bd014 730 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
9421ebb9 731 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
732 || code2 == INTEGER_TYPE);
1ea7fa60 733
e5d0ba06 734 /* When one operand is a decimal float type, the other operand cannot be
735 a generic float type or a complex type. We also disallow vector types
736 here. */
737 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
738 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
739 {
740 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
741 {
742 error ("can%'t mix operands of decimal float and vector types");
743 return error_mark_node;
744 }
745 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
746 {
747 error ("can%'t mix operands of decimal float and complex types");
748 return error_mark_node;
749 }
750 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
751 {
752 error ("can%'t mix operands of decimal float and other float types");
753 return error_mark_node;
754 }
755 }
756
1ea7fa60 757 /* If one type is a vector type, return that type. (How the usual
758 arithmetic conversions apply to the vector types extension is not
759 precisely specified.) */
760 if (code1 == VECTOR_TYPE)
761 return t1;
762
763 if (code2 == VECTOR_TYPE)
764 return t2;
765
766 /* If one type is complex, form the common type of the non-complex
767 components, then make that complex. Use T1 or T2 if it is the
768 required type. */
769 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
770 {
771 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
772 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
79d37242 773 tree subtype = c_common_type (subtype1, subtype2);
1ea7fa60 774
775 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
776 return t1;
777 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
778 return t2;
779 else
780 return build_complex_type (subtype);
781 }
782
783 /* If only one is real, use it as the result. */
784
785 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
786 return t1;
787
788 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
789 return t2;
790
c4503c0a 791 /* If both are real and either are decimal floating point types, use
792 the decimal floating point type with the greater precision. */
793
794 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
795 {
796 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
797 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
798 return dfloat128_type_node;
799 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
801 return dfloat64_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
804 return dfloat32_type_node;
805 }
806
9421ebb9 807 /* Deal with fixed-point types. */
808 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
809 {
810 unsigned int unsignedp = 0, satp = 0;
3754d046 811 machine_mode m1, m2;
9421ebb9 812 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
813
814 m1 = TYPE_MODE (t1);
815 m2 = TYPE_MODE (t2);
816
817 /* If one input type is saturating, the result type is saturating. */
818 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
819 satp = 1;
820
821 /* If both fixed-point types are unsigned, the result type is unsigned.
822 When mixing fixed-point and integer types, follow the sign of the
823 fixed-point type.
824 Otherwise, the result type is signed. */
825 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
826 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
827 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
828 && TYPE_UNSIGNED (t1))
829 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
830 && TYPE_UNSIGNED (t2)))
831 unsignedp = 1;
832
833 /* The result type is signed. */
834 if (unsignedp == 0)
835 {
836 /* If the input type is unsigned, we need to convert to the
837 signed type. */
838 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
839 {
7d339f93 840 enum mode_class mclass = (enum mode_class) 0;
9421ebb9 841 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
842 mclass = MODE_FRACT;
843 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
844 mclass = MODE_ACCUM;
845 else
846 gcc_unreachable ();
847 m1 = mode_for_size (GET_MODE_PRECISION (m1), mclass, 0);
848 }
849 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
850 {
7d339f93 851 enum mode_class mclass = (enum mode_class) 0;
9421ebb9 852 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
853 mclass = MODE_FRACT;
854 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
855 mclass = MODE_ACCUM;
856 else
857 gcc_unreachable ();
858 m2 = mode_for_size (GET_MODE_PRECISION (m2), mclass, 0);
859 }
860 }
861
862 if (code1 == FIXED_POINT_TYPE)
863 {
864 fbit1 = GET_MODE_FBIT (m1);
865 ibit1 = GET_MODE_IBIT (m1);
866 }
867 else
868 {
869 fbit1 = 0;
870 /* Signed integers need to subtract one sign bit. */
871 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
872 }
873
874 if (code2 == FIXED_POINT_TYPE)
875 {
876 fbit2 = GET_MODE_FBIT (m2);
877 ibit2 = GET_MODE_IBIT (m2);
878 }
879 else
880 {
881 fbit2 = 0;
882 /* Signed integers need to subtract one sign bit. */
883 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
884 }
885
886 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
887 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
888 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
889 satp);
890 }
891
1ea7fa60 892 /* Both real or both integers; use the one with greater precision. */
893
894 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
895 return t1;
896 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
897 return t2;
898
899 /* Same precision. Prefer long longs to longs to ints when the
900 same precision, following the C99 rules on integer type rank
901 (which are equivalent to the C90 rules for C90 types). */
902
903 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
904 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
905 return long_long_unsigned_type_node;
906
907 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
908 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
909 {
910 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
911 return long_long_unsigned_type_node;
912 else
a0c938f0 913 return long_long_integer_type_node;
1ea7fa60 914 }
915
916 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
917 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
918 return long_unsigned_type_node;
919
920 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
921 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
922 {
923 /* But preserve unsignedness from the other type,
924 since long cannot hold all the values of an unsigned int. */
925 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
926 return long_unsigned_type_node;
927 else
928 return long_integer_type_node;
929 }
930
82c85aba 931 /* For floating types of the same TYPE_PRECISION (which we here
932 assume means either the same set of values, or sets of values
933 neither a subset of the other, with behavior being undefined in
934 the latter case), follow the rules from TS 18661-3: prefer
935 interchange types _FloatN, then standard types long double,
936 double, float, then extended types _FloatNx. For extended types,
937 check them starting with _Float128x as that seems most consistent
938 in spirit with preferring long double to double; for interchange
939 types, also check in that order for consistency although it's not
940 possible for more than one of them to have the same
941 precision. */
942 tree mv1 = TYPE_MAIN_VARIANT (t1);
943 tree mv2 = TYPE_MAIN_VARIANT (t2);
944
945 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
946 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
947 return FLOATN_TYPE_NODE (i);
948
1ea7fa60 949 /* Likewise, prefer long double to double even if same size. */
82c85aba 950 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
1ea7fa60 951 return long_double_type_node;
952
f8234178 953 /* Likewise, prefer double to float even if same size.
954 We got a couple of embedded targets with 32 bit doubles, and the
955 pdp11 might have 64 bit floats. */
82c85aba 956 if (mv1 == double_type_node || mv2 == double_type_node)
f8234178 957 return double_type_node;
958
82c85aba 959 if (mv1 == float_type_node || mv2 == float_type_node)
960 return float_type_node;
961
962 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
963 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
964 return FLOATNX_TYPE_NODE (i);
965
1ea7fa60 966 /* Otherwise prefer the unsigned one. */
967
968 if (TYPE_UNSIGNED (t1))
969 return t1;
970 else
971 return t2;
972}
628fa4f8 973\f
7422cd7b 974/* Wrapper around c_common_type that is used by c-common.c and other
975 front end optimizations that remove promotions. ENUMERAL_TYPEs
c3a8afb5 976 are allowed here and are converted to their compatible integer types.
977 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
978 preferably a non-Boolean type as the common type. */
79d37242 979tree
980common_type (tree t1, tree t2)
981{
982 if (TREE_CODE (t1) == ENUMERAL_TYPE)
983 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
984 if (TREE_CODE (t2) == ENUMERAL_TYPE)
985 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
c3a8afb5 986
987 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
988 if (TREE_CODE (t1) == BOOLEAN_TYPE
989 && TREE_CODE (t2) == BOOLEAN_TYPE)
990 return boolean_type_node;
991
992 /* If either type is BOOLEAN_TYPE, then return the other. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE)
994 return t2;
995 if (TREE_CODE (t2) == BOOLEAN_TYPE)
996 return t1;
997
79d37242 998 return c_common_type (t1, t2);
999}
7e58afa1 1000
628fa4f8 1001/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1002 or various other operations. Return 2 if they are compatible
1003 but a warning may be needed if you use them together. */
1004
1005int
3ed275a6 1006comptypes (tree type1, tree type2)
7e58afa1 1007{
1008 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1009 int val;
1010
ce3765bf 1011 val = comptypes_internal (type1, type2, NULL, NULL);
d5b637fa 1012 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1013
1014 return val;
1015}
1016
1017/* Like comptypes, but if it returns non-zero because enum and int are
1018 compatible, it sets *ENUM_AND_INT_P to true. */
1019
1020static int
1021comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1022{
1023 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1024 int val;
1025
ce3765bf 1026 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1027 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1028
1029 return val;
1030}
1031
1032/* Like comptypes, but if it returns nonzero for different types, it
1033 sets *DIFFERENT_TYPES_P to true. */
1034
1035int
1036comptypes_check_different_types (tree type1, tree type2,
1037 bool *different_types_p)
1038{
1039 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1040 int val;
1041
1042 val = comptypes_internal (type1, type2, NULL, different_types_p);
7e58afa1 1043 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
a0c938f0 1044
7e58afa1 1045 return val;
a0c938f0 1046}
1047\f
7e58afa1 1048/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1049 or various other operations. Return 2 if they are compatible
d5b637fa 1050 but a warning may be needed if you use them together. If
1051 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1052 compatible integer type, then this sets *ENUM_AND_INT_P to true;
ce3765bf 1053 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1054 NULL, and the types are compatible but different enough not to be
32074525 1055 permitted in C11 typedef redeclarations, then this sets
ce3765bf 1056 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1057 false, but may or may not be set if the types are incompatible.
1058 This differs from comptypes, in that we don't free the seen
1059 types. */
7e58afa1 1060
1061static int
ce3765bf 1062comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1063 bool *different_types_p)
628fa4f8 1064{
9f627b1a 1065 const_tree t1 = type1;
1066 const_tree t2 = type2;
8fc98180 1067 int attrval, val;
628fa4f8 1068
1069 /* Suppress errors caused by previously reported errors. */
1070
6b4e4ff6 1071 if (t1 == t2 || !t1 || !t2
1072 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
628fa4f8 1073 return 1;
1074
09e0a2a1 1075 /* Enumerated types are compatible with integer types, but this is
1076 not transitive: two enumerated types in the same translation unit
1077 are compatible with each other only if they are the same type. */
628fa4f8 1078
09e0a2a1 1079 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
d5b637fa 1080 {
1081 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
ce3765bf 1082 if (TREE_CODE (t2) != VOID_TYPE)
1083 {
1084 if (enum_and_int_p != NULL)
1085 *enum_and_int_p = true;
1086 if (different_types_p != NULL)
1087 *different_types_p = true;
1088 }
d5b637fa 1089 }
09e0a2a1 1090 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
d5b637fa 1091 {
1092 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
ce3765bf 1093 if (TREE_CODE (t1) != VOID_TYPE)
1094 {
1095 if (enum_and_int_p != NULL)
1096 *enum_and_int_p = true;
1097 if (different_types_p != NULL)
1098 *different_types_p = true;
1099 }
d5b637fa 1100 }
628fa4f8 1101
1102 if (t1 == t2)
1103 return 1;
1104
1105 /* Different classes of types can't be compatible. */
1106
a5d9b222 1107 if (TREE_CODE (t1) != TREE_CODE (t2))
1108 return 0;
628fa4f8 1109
14338e9a 1110 /* Qualifiers must match. C99 6.7.3p9 */
628fa4f8 1111
a5b1863e 1112 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
628fa4f8 1113 return 0;
1114
2201c2d1 1115 /* Allow for two different type nodes which have essentially the same
1116 definition. Note that we already checked for equality of the type
3398e91d 1117 qualifiers (just above). */
628fa4f8 1118
aebc8537 1119 if (TREE_CODE (t1) != ARRAY_TYPE
1120 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
628fa4f8 1121 return 1;
1122
8fc98180 1123 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
309303cf 1124 if (!(attrval = comp_type_attributes (t1, t2)))
8fc98180 1125 return 0;
1126
1127 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1128 val = 0;
1129
628fa4f8 1130 switch (TREE_CODE (t1))
1131 {
e4b92538 1132 case INTEGER_TYPE:
1133 case FIXED_POINT_TYPE:
1134 case REAL_TYPE:
1135 /* With these nodes, we can't determine type equivalence by
1136 looking at what is stored in the nodes themselves, because
1137 two nodes might have different TYPE_MAIN_VARIANTs but still
1138 represent the same type. For example, wchar_t and int could
1139 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1140 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1141 and are distinct types. On the other hand, int and the
1142 following typedef
1143
1144 typedef int INT __attribute((may_alias));
1145
1146 have identical properties, different TYPE_MAIN_VARIANTs, but
1147 represent the same type. The canonical type system keeps
1148 track of equivalence in this case, so we fall back on it. */
1149 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1150
628fa4f8 1151 case POINTER_TYPE:
e4b92538 1152 /* Do not remove mode information. */
1153 if (TYPE_MODE (t1) != TYPE_MODE (t2))
4cb9d5c8 1154 break;
8fc98180 1155 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
d5b637fa 1156 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
ce3765bf 1157 enum_and_int_p, different_types_p));
8fc98180 1158 break;
628fa4f8 1159
1160 case FUNCTION_TYPE:
ce3765bf 1161 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1162 different_types_p);
8fc98180 1163 break;
628fa4f8 1164
1165 case ARRAY_TYPE:
1166 {
628fa4f8 1167 tree d1 = TYPE_DOMAIN (t1);
1168 tree d2 = TYPE_DOMAIN (t2);
2af9221e 1169 bool d1_variable, d2_variable;
1170 bool d1_zero, d2_zero;
8fc98180 1171 val = 1;
628fa4f8 1172
1173 /* Target types must match incl. qualifiers. */
1174 if (TREE_TYPE (t1) != TREE_TYPE (t2)
d5b637fa 1175 && 0 == (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
ce3765bf 1176 enum_and_int_p,
1177 different_types_p)))
628fa4f8 1178 return 0;
1179
ce3765bf 1180 if (different_types_p != NULL
1181 && (d1 == 0) != (d2 == 0))
1182 *different_types_p = true;
628fa4f8 1183 /* Sizes must match unless one is missing or variable. */
2af9221e 1184 if (d1 == 0 || d2 == 0 || d1 == d2)
8fc98180 1185 break;
628fa4f8 1186
84166705 1187 d1_zero = !TYPE_MAX_VALUE (d1);
1188 d2_zero = !TYPE_MAX_VALUE (d2);
2af9221e 1189
84166705 1190 d1_variable = (!d1_zero
2af9221e 1191 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1192 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
84166705 1193 d2_variable = (!d2_zero
2af9221e 1194 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1195 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
32d33ab2 1196 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1197 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
2af9221e 1198
ce3765bf 1199 if (different_types_p != NULL
1200 && d1_variable != d2_variable)
1201 *different_types_p = true;
2af9221e 1202 if (d1_variable || d2_variable)
1203 break;
1204 if (d1_zero && d2_zero)
1205 break;
1206 if (d1_zero || d2_zero
84166705 1207 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1208 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
a0c2c45b 1209 val = 0;
1210
a0c938f0 1211 break;
628fa4f8 1212 }
1213
40109983 1214 case ENUMERAL_TYPE:
34d3c5de 1215 case RECORD_TYPE:
40109983 1216 case UNION_TYPE:
76c16586 1217 if (val != 1 && !same_translation_unit_p (t1, t2))
a0c938f0 1218 {
9369a09f 1219 tree a1 = TYPE_ATTRIBUTES (t1);
1220 tree a2 = TYPE_ATTRIBUTES (t2);
1221
1222 if (! attribute_list_contained (a1, a2)
1223 && ! attribute_list_contained (a2, a1))
1224 break;
1225
7e58afa1 1226 if (attrval != 2)
ce3765bf 1227 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1228 different_types_p);
1229 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1230 different_types_p);
7e58afa1 1231 }
8fc98180 1232 break;
0dbd1c74 1233
5050b2f7 1234 case VECTOR_TYPE:
d5b637fa 1235 val = (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
1236 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
ce3765bf 1237 enum_and_int_p, different_types_p));
5050b2f7 1238 break;
1239
0dbd1c74 1240 default:
1241 break;
628fa4f8 1242 }
8fc98180 1243 return attrval == 2 && val == 1 ? 2 : val;
628fa4f8 1244}
1245
6d5d708e 1246/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1247 their qualifiers, except for named address spaces. If the pointers point to
1248 different named addresses, then we must determine if one address space is a
1249 subset of the other. */
628fa4f8 1250
1251static int
d5b637fa 1252comp_target_types (location_t location, tree ttl, tree ttr)
628fa4f8 1253{
fadbd30d 1254 int val;
8a8211df 1255 int val_ped;
6d5d708e 1256 tree mvl = TREE_TYPE (ttl);
1257 tree mvr = TREE_TYPE (ttr);
1258 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1259 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1260 addr_space_t as_common;
d5b637fa 1261 bool enum_and_int_p;
ea5fb8f9 1262
6d5d708e 1263 /* Fail if pointers point to incompatible address spaces. */
1264 if (!addr_space_superset (asl, asr, &as_common))
1265 return 0;
1266
8a8211df 1267 /* For pedantic record result of comptypes on arrays before losing
1268 qualifiers on the element type below. */
1269 val_ped = 1;
1270
1271 if (TREE_CODE (mvl) == ARRAY_TYPE
1272 && TREE_CODE (mvr) == ARRAY_TYPE)
1273 val_ped = comptypes (mvl, mvr);
1274
1275 /* Qualifiers on element types of array types that are
1276 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1277
1278 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1279 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1280 : TYPE_MAIN_VARIANT (mvl));
1281
1282 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1283 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1284 : TYPE_MAIN_VARIANT (mvr));
1285
d5b637fa 1286 enum_and_int_p = false;
1287 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
ea5fb8f9 1288
8a8211df 1289 if (val == 1 && val_ped != 1)
1290 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1291 "are incompatible in ISO C");
1292
8864917d 1293 if (val == 2)
29438999 1294 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
d5b637fa 1295
1296 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1297 warning_at (location, OPT_Wc___compat,
1298 "pointer target types incompatible in C++");
1299
628fa4f8 1300 return val;
1301}
1302\f
1303/* Subroutines of `comptypes'. */
1304
393b349a 1305/* Determine whether two trees derive from the same translation unit.
1306 If the CONTEXT chain ends in a null, that tree's context is still
1307 being parsed, so if two trees have context chains ending in null,
76c16586 1308 they're in the same translation unit. */
393b349a 1309int
9f627b1a 1310same_translation_unit_p (const_tree t1, const_tree t2)
76c16586 1311{
1312 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1313 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1314 {
ce45a448 1315 case tcc_declaration:
1316 t1 = DECL_CONTEXT (t1); break;
1317 case tcc_type:
1318 t1 = TYPE_CONTEXT (t1); break;
1319 case tcc_exceptional:
1320 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
231bd014 1321 default: gcc_unreachable ();
76c16586 1322 }
1323
1324 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1325 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1326 {
ce45a448 1327 case tcc_declaration:
1328 t2 = DECL_CONTEXT (t2); break;
1329 case tcc_type:
1330 t2 = TYPE_CONTEXT (t2); break;
1331 case tcc_exceptional:
1332 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
231bd014 1333 default: gcc_unreachable ();
76c16586 1334 }
1335
1336 return t1 == t2;
1337}
1338
7e58afa1 1339/* Allocate the seen two types, assuming that they are compatible. */
40109983 1340
7e58afa1 1341static struct tagged_tu_seen_cache *
9f627b1a 1342alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
7e58afa1 1343{
a9c6c0e3 1344 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
7e58afa1 1345 tu->next = tagged_tu_seen_base;
1346 tu->t1 = t1;
1347 tu->t2 = t2;
a0c938f0 1348
7e58afa1 1349 tagged_tu_seen_base = tu;
a0c938f0 1350
7e58afa1 1351 /* The C standard says that two structures in different translation
1352 units are compatible with each other only if the types of their
1353 fields are compatible (among other things). We assume that they
1354 are compatible until proven otherwise when building the cache.
1355 An example where this can occur is:
1356 struct a
1357 {
1358 struct a *next;
1359 };
1360 If we are comparing this against a similar struct in another TU,
7063afc3 1361 and did not assume they were compatible, we end up with an infinite
7e58afa1 1362 loop. */
1363 tu->val = 1;
1364 return tu;
1365}
40109983 1366
7e58afa1 1367/* Free the seen types until we get to TU_TIL. */
40109983 1368
7e58afa1 1369static void
1370free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1371{
1372 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1373 while (tu != tu_til)
1374 {
aae87fc3 1375 const struct tagged_tu_seen_cache *const tu1
1376 = (const struct tagged_tu_seen_cache *) tu;
7e58afa1 1377 tu = tu1->next;
e47a6f81 1378 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
7e58afa1 1379 }
1380 tagged_tu_seen_base = tu_til;
1381}
40109983 1382
1383/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1384 compatible. If the two types are not the same (which has been
1385 checked earlier), this can only happen when multiple translation
1386 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
ce3765bf 1387 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1388 comptypes_internal. */
40109983 1389
1390static int
d5b637fa 1391tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
ce3765bf 1392 bool *enum_and_int_p, bool *different_types_p)
40109983 1393{
1394 tree s1, s2;
1395 bool needs_warning = false;
a5d9b222 1396
40109983 1397 /* We have to verify that the tags of the types are the same. This
1398 is harder than it looks because this may be a typedef, so we have
1399 to go look at the original type. It may even be a typedef of a
4ee9c684 1400 typedef...
1401 In the case of compiler-created builtin structs the TYPE_DECL
1402 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dba94932 1403 while (TYPE_NAME (t1)
1404 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1405 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
40109983 1406 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1407
dba94932 1408 while (TYPE_NAME (t2)
1409 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1410 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
40109983 1411 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1412
1413 /* C90 didn't have the requirement that the two tags be the same. */
1414 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1415 return 0;
a5d9b222 1416
40109983 1417 /* C90 didn't say what happened if one or both of the types were
1418 incomplete; we choose to follow C99 rules here, which is that they
1419 are compatible. */
1420 if (TYPE_SIZE (t1) == NULL
1421 || TYPE_SIZE (t2) == NULL)
1422 return 1;
a5d9b222 1423
40109983 1424 {
7e58afa1 1425 const struct tagged_tu_seen_cache * tts_i;
40109983 1426 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1427 if (tts_i->t1 == t1 && tts_i->t2 == t2)
7e58afa1 1428 return tts_i->val;
40109983 1429 }
a5d9b222 1430
40109983 1431 switch (TREE_CODE (t1))
1432 {
1433 case ENUMERAL_TYPE:
1434 {
7e58afa1 1435 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
a0c938f0 1436 /* Speed up the case where the type values are in the same order. */
1437 tree tv1 = TYPE_VALUES (t1);
1438 tree tv2 = TYPE_VALUES (t2);
a5d9b222 1439
a0c938f0 1440 if (tv1 == tv2)
7e58afa1 1441 {
1442 return 1;
1443 }
a5d9b222 1444
a0c938f0 1445 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1446 {
1447 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1448 break;
1449 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
7e58afa1 1450 {
a0c938f0 1451 tu->val = 0;
7e58afa1 1452 return 0;
1453 }
a0c938f0 1454 }
a5d9b222 1455
a0c938f0 1456 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
7e58afa1 1457 {
1458 return 1;
1459 }
a0c938f0 1460 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
7e58afa1 1461 {
1462 tu->val = 0;
1463 return 0;
1464 }
a5d9b222 1465
40109983 1466 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
7e58afa1 1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
a5d9b222 1471
40109983 1472 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1473 {
1474 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1475 if (s2 == NULL
1476 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
7e58afa1 1477 {
1478 tu->val = 0;
1479 return 0;
1480 }
40109983 1481 }
1482 return 1;
1483 }
1484
1485 case UNION_TYPE:
1486 {
7e58afa1 1487 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
40109983 1488 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
7e58afa1 1489 {
1490 tu->val = 0;
1491 return 0;
1492 }
a0c938f0 1493
7e58afa1 1494 /* Speed up the common case where the fields are in the same order. */
1495 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
1767a056 1496 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
7e58afa1 1497 {
1498 int result;
a0c938f0 1499
7a07cc13 1500 if (DECL_NAME (s1) != DECL_NAME (s2))
7e58afa1 1501 break;
d5b637fa 1502 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
ce3765bf 1503 enum_and_int_p, different_types_p);
7a07cc13 1504
1505 if (result != 1 && !DECL_NAME (s1))
1506 break;
7e58afa1 1507 if (result == 0)
1508 {
1509 tu->val = 0;
1510 return 0;
1511 }
1512 if (result == 2)
1513 needs_warning = true;
1514
1515 if (TREE_CODE (s1) == FIELD_DECL
1516 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1517 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1518 {
1519 tu->val = 0;
1520 return 0;
1521 }
1522 }
1523 if (!s1 && !s2)
1524 {
1525 tu->val = needs_warning ? 2 : 1;
1526 return tu->val;
1527 }
40109983 1528
1767a056 1529 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
40109983 1530 {
1531 bool ok = false;
a5d9b222 1532
1767a056 1533 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
7a07cc13 1534 if (DECL_NAME (s1) == DECL_NAME (s2))
1535 {
1536 int result;
1537
d5b637fa 1538 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
ce3765bf 1539 enum_and_int_p,
1540 different_types_p);
7a07cc13 1541
1542 if (result != 1 && !DECL_NAME (s1))
1543 continue;
1544 if (result == 0)
1545 {
1546 tu->val = 0;
1547 return 0;
1548 }
1549 if (result == 2)
1550 needs_warning = true;
1551
1552 if (TREE_CODE (s1) == FIELD_DECL
1553 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1554 DECL_FIELD_BIT_OFFSET (s2)) != 1)
40109983 1555 break;
7a07cc13 1556
1557 ok = true;
1558 break;
1559 }
84166705 1560 if (!ok)
7e58afa1 1561 {
1562 tu->val = 0;
1563 return 0;
1564 }
40109983 1565 }
7e58afa1 1566 tu->val = needs_warning ? 2 : 10;
1567 return tu->val;
40109983 1568 }
1569
1570 case RECORD_TYPE:
1571 {
a0c938f0 1572 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
a5d9b222 1573
1574 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
40109983 1575 s1 && s2;
1767a056 1576 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
40109983 1577 {
1578 int result;
1579 if (TREE_CODE (s1) != TREE_CODE (s2)
1580 || DECL_NAME (s1) != DECL_NAME (s2))
1581 break;
d5b637fa 1582 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
ce3765bf 1583 enum_and_int_p, different_types_p);
40109983 1584 if (result == 0)
1585 break;
1586 if (result == 2)
1587 needs_warning = true;
a5d9b222 1588
40109983 1589 if (TREE_CODE (s1) == FIELD_DECL
1590 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1591 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1592 break;
1593 }
40109983 1594 if (s1 && s2)
7e58afa1 1595 tu->val = 0;
1596 else
1597 tu->val = needs_warning ? 2 : 1;
1598 return tu->val;
40109983 1599 }
1600
1601 default:
231bd014 1602 gcc_unreachable ();
40109983 1603 }
1604}
1605
628fa4f8 1606/* Return 1 if two function types F1 and F2 are compatible.
1607 If either type specifies no argument types,
1608 the other must specify a fixed number of self-promoting arg types.
2b30d46c 1609 Otherwise, if one type specifies only the number of arguments,
628fa4f8 1610 the other must specify that number of self-promoting arg types.
d5b637fa 1611 Otherwise, the argument types must match.
ce3765bf 1612 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
628fa4f8 1613
1614static int
d5b637fa 1615function_types_compatible_p (const_tree f1, const_tree f2,
ce3765bf 1616 bool *enum_and_int_p, bool *different_types_p)
628fa4f8 1617{
1618 tree args1, args2;
1619 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1620 int val = 1;
1621 int val1;
fd49c655 1622 tree ret1, ret2;
1623
1624 ret1 = TREE_TYPE (f1);
1625 ret2 = TREE_TYPE (f2);
1626
0bfe2579 1627 /* 'volatile' qualifiers on a function's return type used to mean
1628 the function is noreturn. */
1629 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
21ca8540 1630 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
fd49c655 1631 if (TYPE_VOLATILE (ret1))
1632 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1633 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1634 if (TYPE_VOLATILE (ret2))
1635 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1636 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
ce3765bf 1637 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
fd49c655 1638 if (val == 0)
628fa4f8 1639 return 0;
1640
1641 args1 = TYPE_ARG_TYPES (f1);
1642 args2 = TYPE_ARG_TYPES (f2);
1643
ce3765bf 1644 if (different_types_p != NULL
1645 && (args1 == 0) != (args2 == 0))
1646 *different_types_p = true;
1647
628fa4f8 1648 /* An unspecified parmlist matches any specified parmlist
1649 whose argument types don't need default promotions. */
1650
1651 if (args1 == 0)
1652 {
1653 if (!self_promoting_args_p (args2))
1654 return 0;
1655 /* If one of these types comes from a non-prototype fn definition,
1656 compare that with the other type's arglist.
6bf97f82 1657 If they don't match, ask for a warning (but no error). */
628fa4f8 1658 if (TYPE_ACTUAL_ARG_TYPES (f1)
d5b637fa 1659 && 1 != type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
ce3765bf 1660 enum_and_int_p, different_types_p))
628fa4f8 1661 val = 2;
1662 return val;
1663 }
1664 if (args2 == 0)
1665 {
1666 if (!self_promoting_args_p (args1))
1667 return 0;
1668 if (TYPE_ACTUAL_ARG_TYPES (f2)
d5b637fa 1669 && 1 != type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
ce3765bf 1670 enum_and_int_p, different_types_p))
628fa4f8 1671 val = 2;
1672 return val;
1673 }
1674
1675 /* Both types have argument lists: compare them and propagate results. */
ce3765bf 1676 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1677 different_types_p);
628fa4f8 1678 return val1 != 1 ? val1 : val;
1679}
1680
d5b637fa 1681/* Check two lists of types for compatibility, returning 0 for
1682 incompatible, 1 for compatible, or 2 for compatible with
ce3765bf 1683 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1684 comptypes_internal. */
628fa4f8 1685
1686static int
d5b637fa 1687type_lists_compatible_p (const_tree args1, const_tree args2,
ce3765bf 1688 bool *enum_and_int_p, bool *different_types_p)
628fa4f8 1689{
1690 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1691 int val = 1;
c2879dd7 1692 int newval = 0;
628fa4f8 1693
1694 while (1)
1695 {
aebc8537 1696 tree a1, mv1, a2, mv2;
628fa4f8 1697 if (args1 == 0 && args2 == 0)
1698 return val;
1699 /* If one list is shorter than the other,
1700 they fail to match. */
1701 if (args1 == 0 || args2 == 0)
1702 return 0;
aebc8537 1703 mv1 = a1 = TREE_VALUE (args1);
1704 mv2 = a2 = TREE_VALUE (args2);
1705 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
b560fabd 1706 mv1 = (TYPE_ATOMIC (mv1)
1707 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1708 TYPE_QUAL_ATOMIC)
1709 : TYPE_MAIN_VARIANT (mv1));
aebc8537 1710 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
b560fabd 1711 mv2 = (TYPE_ATOMIC (mv2)
1712 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1713 TYPE_QUAL_ATOMIC)
1714 : TYPE_MAIN_VARIANT (mv2));
628fa4f8 1715 /* A null pointer instead of a type
1716 means there is supposed to be an argument
1717 but nothing is specified about what type it has.
1718 So match anything that self-promotes. */
ce3765bf 1719 if (different_types_p != NULL
1720 && (a1 == 0) != (a2 == 0))
1721 *different_types_p = true;
aebc8537 1722 if (a1 == 0)
628fa4f8 1723 {
aebc8537 1724 if (c_type_promotes_to (a2) != a2)
628fa4f8 1725 return 0;
1726 }
aebc8537 1727 else if (a2 == 0)
628fa4f8 1728 {
aebc8537 1729 if (c_type_promotes_to (a1) != a1)
628fa4f8 1730 return 0;
1731 }
aa76d997 1732 /* If one of the lists has an error marker, ignore this arg. */
aebc8537 1733 else if (TREE_CODE (a1) == ERROR_MARK
1734 || TREE_CODE (a2) == ERROR_MARK)
aa76d997 1735 ;
ce3765bf 1736 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1737 different_types_p)))
628fa4f8 1738 {
ce3765bf 1739 if (different_types_p != NULL)
1740 *different_types_p = true;
628fa4f8 1741 /* Allow wait (union {union wait *u; int *i} *)
1742 and wait (union wait *) to be compatible. */
aebc8537 1743 if (TREE_CODE (a1) == UNION_TYPE
1744 && (TYPE_NAME (a1) == 0
8df5a43d 1745 || TYPE_TRANSPARENT_AGGR (a1))
aebc8537 1746 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1747 && tree_int_cst_equal (TYPE_SIZE (a1),
1748 TYPE_SIZE (a2)))
628fa4f8 1749 {
1750 tree memb;
aebc8537 1751 for (memb = TYPE_FIELDS (a1);
1767a056 1752 memb; memb = DECL_CHAIN (memb))
c75da5d5 1753 {
1754 tree mv3 = TREE_TYPE (memb);
1755 if (mv3 && mv3 != error_mark_node
1756 && TREE_CODE (mv3) != ARRAY_TYPE)
b560fabd 1757 mv3 = (TYPE_ATOMIC (mv3)
1758 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1759 TYPE_QUAL_ATOMIC)
1760 : TYPE_MAIN_VARIANT (mv3));
ce3765bf 1761 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1762 different_types_p))
c75da5d5 1763 break;
1764 }
628fa4f8 1765 if (memb == 0)
1766 return 0;
1767 }
aebc8537 1768 else if (TREE_CODE (a2) == UNION_TYPE
1769 && (TYPE_NAME (a2) == 0
8df5a43d 1770 || TYPE_TRANSPARENT_AGGR (a2))
aebc8537 1771 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1772 && tree_int_cst_equal (TYPE_SIZE (a2),
1773 TYPE_SIZE (a1)))
628fa4f8 1774 {
1775 tree memb;
aebc8537 1776 for (memb = TYPE_FIELDS (a2);
1767a056 1777 memb; memb = DECL_CHAIN (memb))
c75da5d5 1778 {
1779 tree mv3 = TREE_TYPE (memb);
1780 if (mv3 && mv3 != error_mark_node
1781 && TREE_CODE (mv3) != ARRAY_TYPE)
b560fabd 1782 mv3 = (TYPE_ATOMIC (mv3)
1783 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1784 TYPE_QUAL_ATOMIC)
1785 : TYPE_MAIN_VARIANT (mv3));
ce3765bf 1786 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1787 different_types_p))
c75da5d5 1788 break;
1789 }
628fa4f8 1790 if (memb == 0)
1791 return 0;
1792 }
1793 else
1794 return 0;
1795 }
1796
1797 /* comptypes said ok, but record if it said to warn. */
1798 if (newval > val)
1799 val = newval;
1800
1801 args1 = TREE_CHAIN (args1);
1802 args2 = TREE_CHAIN (args2);
1803 }
1804}
628fa4f8 1805\f
d44f2f7c 1806/* Compute the size to increment a pointer by. When a function type or void
1807 type or incomplete type is passed, size_one_node is returned.
1808 This function does not emit any diagnostics; the caller is responsible
1809 for that. */
628fa4f8 1810
91ffb709 1811static tree
9f627b1a 1812c_size_in_bytes (const_tree type)
628fa4f8 1813{
1814 enum tree_code code = TREE_CODE (type);
1815
d44f2f7c 1816 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1817 || !COMPLETE_TYPE_P (type))
902de8ed 1818 return size_one_node;
1819
628fa4f8 1820 /* Convert in case a char is more than one unit. */
389dd41b 1821 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1822 size_int (TYPE_PRECISION (char_type_node)
1823 / BITS_PER_UNIT));
628fa4f8 1824}
628fa4f8 1825\f
628fa4f8 1826/* Return either DECL or its known constant value (if it has one). */
1827
225ec6aa 1828tree
2b30d46c 1829decl_constant_value (tree decl)
628fa4f8 1830{
6cd7ce61 1831 if (/* Don't change a variable array bound or initial value to a constant
66d12a6c 1832 in a place where a variable is invalid. Note that DECL_INITIAL
1833 isn't valid for a PARM_DECL. */
6cd7ce61 1834 current_function_decl != 0
66d12a6c 1835 && TREE_CODE (decl) != PARM_DECL
84166705 1836 && !TREE_THIS_VOLATILE (decl)
57913890 1837 && TREE_READONLY (decl)
628fa4f8 1838 && DECL_INITIAL (decl) != 0
1839 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
1840 /* This is invalid if initial value is not constant.
1841 If it has either a function call, a memory reference,
1842 or a variable, then re-evaluating it could give different results. */
1843 && TREE_CONSTANT (DECL_INITIAL (decl))
1844 /* Check for cases where this is sub-optimal, even though valid. */
629d8b08 1845 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
628fa4f8 1846 return DECL_INITIAL (decl);
1847 return decl;
1848}
1849
e78cf9a7 1850/* Convert the array expression EXP to a pointer. */
1851static tree
e60a6f7b 1852array_to_pointer_conversion (location_t loc, tree exp)
62827f44 1853{
e78cf9a7 1854 tree orig_exp = exp;
62827f44 1855 tree type = TREE_TYPE (exp);
e78cf9a7 1856 tree adr;
1857 tree restype = TREE_TYPE (type);
1858 tree ptrtype;
62827f44 1859
e78cf9a7 1860 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
62827f44 1861
e78cf9a7 1862 STRIP_TYPE_NOPS (exp);
62827f44 1863
066f26bf 1864 if (TREE_NO_WARNING (orig_exp))
1865 TREE_NO_WARNING (exp) = 1;
62827f44 1866
e78cf9a7 1867 ptrtype = build_pointer_type (restype);
1868
aa3e402e 1869 if (INDIRECT_REF_P (exp))
e78cf9a7 1870 return convert (ptrtype, TREE_OPERAND (exp, 0));
1871
eb5d9b7b 1872 /* In C++ array compound literals are temporary objects unless they are
1873 const or appear in namespace scope, so they are destroyed too soon
1874 to use them for much of anything (c++/53220). */
1875 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1876 {
1877 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1878 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1879 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1880 "converting an array compound literal to a pointer "
1881 "is ill-formed in C++");
1882 }
1883
b99cc6da 1884 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
e78cf9a7 1885 return convert (ptrtype, adr);
1886}
62827f44 1887
e78cf9a7 1888/* Convert the function expression EXP to a pointer. */
1889static tree
e60a6f7b 1890function_to_pointer_conversion (location_t loc, tree exp)
e78cf9a7 1891{
1892 tree orig_exp = exp;
62827f44 1893
e78cf9a7 1894 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
62827f44 1895
e78cf9a7 1896 STRIP_TYPE_NOPS (exp);
62827f44 1897
e78cf9a7 1898 if (TREE_NO_WARNING (orig_exp))
1899 TREE_NO_WARNING (exp) = 1;
62827f44 1900
b99cc6da 1901 return build_unary_op (loc, ADDR_EXPR, exp, false);
e78cf9a7 1902}
62827f44 1903
abc6c64f 1904/* Mark EXP as read, not just set, for set but not used -Wunused
1905 warning purposes. */
1906
1907void
1908mark_exp_read (tree exp)
1909{
1910 switch (TREE_CODE (exp))
1911 {
1912 case VAR_DECL:
1913 case PARM_DECL:
1914 DECL_READ_P (exp) = 1;
1915 break;
1916 case ARRAY_REF:
1917 case COMPONENT_REF:
1918 case MODIFY_EXPR:
1919 case REALPART_EXPR:
1920 case IMAGPART_EXPR:
1921 CASE_CONVERT:
1922 case ADDR_EXPR:
c364d0f3 1923 case VIEW_CONVERT_EXPR:
abc6c64f 1924 mark_exp_read (TREE_OPERAND (exp, 0));
1925 break;
1926 case COMPOUND_EXPR:
062966c1 1927 case C_MAYBE_CONST_EXPR:
abc6c64f 1928 mark_exp_read (TREE_OPERAND (exp, 1));
1929 break;
1930 default:
1931 break;
1932 }
1933}
1934
e78cf9a7 1935/* Perform the default conversion of arrays and functions to pointers.
1936 Return the result of converting EXP. For any other expression, just
e60a6f7b 1937 return EXP.
1938
1939 LOC is the location of the expression. */
e78cf9a7 1940
1941struct c_expr
e60a6f7b 1942default_function_array_conversion (location_t loc, struct c_expr exp)
e78cf9a7 1943{
1944 tree orig_exp = exp.value;
1945 tree type = TREE_TYPE (exp.value);
1946 enum tree_code code = TREE_CODE (type);
1947
1948 switch (code)
1949 {
1950 case ARRAY_TYPE:
1951 {
1952 bool not_lvalue = false;
1953 bool lvalue_array_p;
1954
1955 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
72dd6141 1956 || CONVERT_EXPR_P (exp.value))
e78cf9a7 1957 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1958 {
1959 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1960 not_lvalue = true;
1961 exp.value = TREE_OPERAND (exp.value, 0);
1962 }
1963
1964 if (TREE_NO_WARNING (orig_exp))
1965 TREE_NO_WARNING (exp.value) = 1;
1966
1967 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1968 if (!flag_isoc99 && !lvalue_array_p)
1969 {
1970 /* Before C99, non-lvalue arrays do not decay to pointers.
1971 Normally, using such an array would be invalid; but it can
1972 be used correctly inside sizeof or as a statement expression.
1973 Thus, do not give an error here; an error will result later. */
1974 return exp;
1975 }
1976
e60a6f7b 1977 exp.value = array_to_pointer_conversion (loc, exp.value);
e78cf9a7 1978 }
1979 break;
1980 case FUNCTION_TYPE:
e60a6f7b 1981 exp.value = function_to_pointer_conversion (loc, exp.value);
e78cf9a7 1982 break;
1983 default:
e78cf9a7 1984 break;
62827f44 1985 }
e78cf9a7 1986
62827f44 1987 return exp;
1988}
1989
abc6c64f 1990struct c_expr
1991default_function_array_read_conversion (location_t loc, struct c_expr exp)
1992{
1993 mark_exp_read (exp.value);
1994 return default_function_array_conversion (loc, exp);
1995}
b96dc121 1996
b560fabd 1997/* Return whether EXPR should be treated as an atomic lvalue for the
1998 purposes of load and store handling. */
1999
2000static bool
2001really_atomic_lvalue (tree expr)
2002{
224bdd51 2003 if (error_operand_p (expr))
b560fabd 2004 return false;
2005 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2006 return false;
2007 if (!lvalue_p (expr))
2008 return false;
2009
2010 /* Ignore _Atomic on register variables, since their addresses can't
2011 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2012 sequences wouldn't work. Ignore _Atomic on structures containing
2013 bit-fields, since accessing elements of atomic structures or
2014 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2015 it's undefined at translation time or execution time, and the
2016 normal atomic sequences again wouldn't work. */
2017 while (handled_component_p (expr))
2018 {
2019 if (TREE_CODE (expr) == COMPONENT_REF
2020 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2021 return false;
2022 expr = TREE_OPERAND (expr, 0);
2023 }
2024 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2025 return false;
2026 return true;
2027}
2028
2029/* Convert expression EXP (location LOC) from lvalue to rvalue,
2030 including converting functions and arrays to pointers if CONVERT_P.
2031 If READ_P, also mark the expression as having been read. */
2032
2033struct c_expr
2034convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2035 bool convert_p, bool read_p)
2036{
2037 if (read_p)
2038 mark_exp_read (exp.value);
2039 if (convert_p)
2040 exp = default_function_array_conversion (loc, exp);
2041 if (really_atomic_lvalue (exp.value))
2042 {
2043 vec<tree, va_gc> *params;
2044 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2045 tree expr_type = TREE_TYPE (exp.value);
b99cc6da 2046 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
b560fabd 2047 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2048
2049 gcc_assert (TYPE_ATOMIC (expr_type));
2050
2051 /* Expansion of a generic atomic load may require an addition
2052 element, so allocate enough to prevent a resize. */
2053 vec_alloc (params, 4);
2054
2055 /* Remove the qualifiers for the rest of the expressions and
2056 create the VAL temp variable to hold the RHS. */
2057 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
0cd6b1b4 2058 tmp = create_tmp_var_raw (nonatomic_type);
b99cc6da 2059 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
b560fabd 2060 TREE_ADDRESSABLE (tmp) = 1;
977b1853 2061 TREE_NO_WARNING (tmp) = 1;
b560fabd 2062
2063 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2064 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2065 params->quick_push (expr_addr);
2066 params->quick_push (tmp_addr);
2067 params->quick_push (seq_cst);
5a672e62 2068 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
b560fabd 2069
977b1853 2070 /* EXPR is always read. */
2071 mark_exp_read (exp.value);
b560fabd 2072
2073 /* Return tmp which contains the value loaded. */
0cd6b1b4 2074 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2075 NULL_TREE, NULL_TREE);
b560fabd 2076 }
2077 return exp;
2078}
2079
b96dc121 2080/* EXP is an expression of integer type. Apply the integer promotions
2081 to it and return the promoted value. */
628fa4f8 2082
2083tree
b96dc121 2084perform_integral_promotions (tree exp)
628fa4f8 2085{
19cb6b50 2086 tree type = TREE_TYPE (exp);
2087 enum tree_code code = TREE_CODE (type);
628fa4f8 2088
b96dc121 2089 gcc_assert (INTEGRAL_TYPE_P (type));
98821063 2090
628fa4f8 2091 /* Normally convert enums to int,
2092 but convert wide enums to something wider. */
2093 if (code == ENUMERAL_TYPE)
2094 {
771d21fa 2095 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2096 TYPE_PRECISION (integer_type_node)),
2097 ((TYPE_PRECISION (type)
2098 >= TYPE_PRECISION (integer_type_node))
78a8ed03 2099 && TYPE_UNSIGNED (type)));
a0c2c45b 2100
628fa4f8 2101 return convert (type, exp);
2102 }
2103
b96dc121 2104 /* ??? This should no longer be needed now bit-fields have their
2105 proper types. */
3486c3bf 2106 if (TREE_CODE (exp) == COMPONENT_REF
a0c2c45b 2107 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
ff552418 2108 /* If it's thinner than an int, promote it like a
d7aeef06 2109 c_promoting_integer_type_p, otherwise leave it alone. */
a0c2c45b 2110 && 0 > compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2111 TYPE_PRECISION (integer_type_node)))
455730ef 2112 return convert (integer_type_node, exp);
3486c3bf 2113
d7aeef06 2114 if (c_promoting_integer_type_p (type))
628fa4f8 2115 {
455730ef 2116 /* Preserve unsignedness if not really getting any wider. */
78a8ed03 2117 if (TYPE_UNSIGNED (type)
455730ef 2118 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
628fa4f8 2119 return convert (unsigned_type_node, exp);
a0c2c45b 2120
628fa4f8 2121 return convert (integer_type_node, exp);
2122 }
a0c2c45b 2123
b96dc121 2124 return exp;
2125}
2126
2127
2128/* Perform default promotions for C data used in expressions.
f14c8207 2129 Enumeral types or short or char are converted to int.
b96dc121 2130 In addition, manifest constants symbols are replaced by their values. */
2131
2132tree
2133default_conversion (tree exp)
2134{
2135 tree orig_exp;
2136 tree type = TREE_TYPE (exp);
2137 enum tree_code code = TREE_CODE (type);
2b3c93a3 2138 tree promoted_type;
b96dc121 2139
abc6c64f 2140 mark_exp_read (exp);
2141
f14c8207 2142 /* Functions and arrays have been converted during parsing. */
2143 gcc_assert (code != FUNCTION_TYPE);
2144 if (code == ARRAY_TYPE)
2145 return exp;
b96dc121 2146
2147 /* Constants can be used directly unless they're not loadable. */
2148 if (TREE_CODE (exp) == CONST_DECL)
2149 exp = DECL_INITIAL (exp);
2150
b96dc121 2151 /* Strip no-op conversions. */
2152 orig_exp = exp;
2153 STRIP_TYPE_NOPS (exp);
2154
2155 if (TREE_NO_WARNING (orig_exp))
2156 TREE_NO_WARNING (exp) = 1;
2157
628fa4f8 2158 if (code == VOID_TYPE)
2159 {
d44f2f7c 2160 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2161 "void value not ignored as it ought to be");
628fa4f8 2162 return error_mark_node;
2163 }
f831c98f 2164
22a3f7bd 2165 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
f831c98f 2166 if (exp == error_mark_node)
2167 return error_mark_node;
2168
2b3c93a3 2169 promoted_type = targetm.promoted_type (type);
2170 if (promoted_type)
2171 return convert (promoted_type, exp);
2172
f831c98f 2173 if (INTEGRAL_TYPE_P (type))
2174 return perform_integral_promotions (exp);
2175
628fa4f8 2176 return exp;
2177}
2178\f
10064fb9 2179/* Look up COMPONENT in a structure or union TYPE.
9303bbca 2180
2181 If the component name is not found, returns NULL_TREE. Otherwise,
2182 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2183 stepping down the chain to the component, which is in the last
2184 TREE_VALUE of the list. Normally the list is of length one, but if
2185 the component is embedded within (nested) anonymous structures or
2186 unions, the list steps down the chain to the component. */
2b30d46c 2187
bdbd2f6d 2188static tree
10064fb9 2189lookup_field (tree type, tree component)
bdbd2f6d 2190{
2191 tree field;
2192
2193 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2194 to the field elements. Use a binary search on this array to quickly
2195 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
2196 will always be set for structures which have many elements. */
2197
4bb2a91a 2198 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s)
bdbd2f6d 2199 {
2200 int bot, top, half;
860251be 2201 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
bdbd2f6d 2202
2203 field = TYPE_FIELDS (type);
2204 bot = 0;
860251be 2205 top = TYPE_LANG_SPECIFIC (type)->s->len;
bdbd2f6d 2206 while (top - bot > 1)
2207 {
bdbd2f6d 2208 half = (top - bot + 1) >> 1;
2209 field = field_array[bot+half];
2210
2211 if (DECL_NAME (field) == NULL_TREE)
2212 {
2213 /* Step through all anon unions in linear fashion. */
2214 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2215 {
bdbd2f6d 2216 field = field_array[bot++];
ef61516b 2217 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
37c005bb 2218 {
10064fb9 2219 tree anon = lookup_field (TREE_TYPE (field), component);
9303bbca 2220
2221 if (anon)
2222 return tree_cons (NULL_TREE, field, anon);
2fdec027 2223
2224 /* The Plan 9 compiler permits referring
2225 directly to an anonymous struct/union field
2226 using a typedef name. */
2227 if (flag_plan9_extensions
2228 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2229 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2230 == TYPE_DECL)
2231 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2232 == component))
2233 break;
2b30d46c 2234 }
bdbd2f6d 2235 }
2236
2237 /* Entire record is only anon unions. */
2238 if (bot > top)
2239 return NULL_TREE;
2240
2241 /* Restart the binary search, with new lower bound. */
2242 continue;
2243 }
2244
d3016716 2245 if (DECL_NAME (field) == component)
bdbd2f6d 2246 break;
d3016716 2247 if (DECL_NAME (field) < component)
bdbd2f6d 2248 bot += half;
2249 else
2250 top = bot + half;
2251 }
2252
2253 if (DECL_NAME (field_array[bot]) == component)
2254 field = field_array[bot];
2255 else if (DECL_NAME (field) != component)
9303bbca 2256 return NULL_TREE;
bdbd2f6d 2257 }
2258 else
2259 {
1767a056 2260 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
bdbd2f6d 2261 {
9303bbca 2262 if (DECL_NAME (field) == NULL_TREE
ef61516b 2263 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
bdbd2f6d 2264 {
10064fb9 2265 tree anon = lookup_field (TREE_TYPE (field), component);
f9d98a50 2266
9303bbca 2267 if (anon)
2268 return tree_cons (NULL_TREE, field, anon);
2fdec027 2269
2270 /* The Plan 9 compiler permits referring directly to an
2271 anonymous struct/union field using a typedef
2272 name. */
2273 if (flag_plan9_extensions
2274 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2275 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2276 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2277 == component))
2278 break;
bdbd2f6d 2279 }
2280
2281 if (DECL_NAME (field) == component)
2282 break;
2283 }
9303bbca 2284
2285 if (field == NULL_TREE)
2286 return NULL_TREE;
bdbd2f6d 2287 }
2288
9303bbca 2289 return tree_cons (NULL_TREE, field, NULL_TREE);
bdbd2f6d 2290}
2291
b67f0560 2292/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2293
2294static void
2295lookup_field_fuzzy_find_candidates (tree type, tree component,
2296 vec<tree> *candidates)
2297{
2298 tree field;
2299 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2300 {
2301 if (DECL_NAME (field) == NULL_TREE
ef61516b 2302 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2303 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2304 candidates);
b67f0560 2305
2306 if (DECL_NAME (field))
2307 candidates->safe_push (DECL_NAME (field));
2308 }
2309}
2310
2311/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2312 rather than returning a TREE_LIST for an exact match. */
2313
2314static tree
2315lookup_field_fuzzy (tree type, tree component)
2316{
2317 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2318
2319 /* First, gather a list of candidates. */
2320 auto_vec <tree> candidates;
2321
2322 lookup_field_fuzzy_find_candidates (type, component,
2323 &candidates);
2324
11d8dd6f 2325 return find_closest_identifier (component, &candidates);
b67f0560 2326}
2327
734caf84 2328/* Support function for build_component_ref's error-handling.
2329
2330 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2331 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2332
2333static bool
2334should_suggest_deref_p (tree datum_type)
2335{
2336 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2337 allows "." for ptrs; we could be handling a failed attempt
2338 to access a property. */
2339 if (c_dialect_objc ())
2340 return false;
2341
2342 /* Only suggest it for pointers... */
2343 if (TREE_CODE (datum_type) != POINTER_TYPE)
2344 return false;
2345
2346 /* ...to structs/unions. */
2347 tree underlying_type = TREE_TYPE (datum_type);
2348 enum tree_code code = TREE_CODE (underlying_type);
2349 if (code == RECORD_TYPE || code == UNION_TYPE)
2350 return true;
2351 else
2352 return false;
2353}
2354
e60a6f7b 2355/* Make an expression to refer to the COMPONENT field of structure or
2356 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
7cfde92a 2357 location of the COMPONENT_REF. COMPONENT_LOC is the location
2358 of COMPONENT. */
628fa4f8 2359
2360tree
7cfde92a 2361build_component_ref (location_t loc, tree datum, tree component,
2362 location_t component_loc)
628fa4f8 2363{
19cb6b50 2364 tree type = TREE_TYPE (datum);
2365 enum tree_code code = TREE_CODE (type);
2366 tree field = NULL;
2367 tree ref;
7666d572 2368 bool datum_lvalue = lvalue_p (datum);
628fa4f8 2369
43bf5d72 2370 if (!objc_is_public (datum, component))
2371 return error_mark_node;
2372
9d9f5bb3 2373 /* Detect Objective-C property syntax object.property. */
86c110ac 2374 if (c_dialect_objc ()
9d9f5bb3 2375 && (ref = objc_maybe_build_component_ref (datum, component)))
86c110ac 2376 return ref;
2377
628fa4f8 2378 /* See if there is a field or component with name COMPONENT. */
2379
2380 if (code == RECORD_TYPE || code == UNION_TYPE)
2381 {
4b72716d 2382 if (!COMPLETE_TYPE_P (type))
628fa4f8 2383 {
22a3f7bd 2384 c_incomplete_type_error (loc, NULL_TREE, type);
628fa4f8 2385 return error_mark_node;
2386 }
2387
10064fb9 2388 field = lookup_field (type, component);
628fa4f8 2389
2390 if (!field)
2391 {
b67f0560 2392 tree guessed_id = lookup_field_fuzzy (type, component);
2393 if (guessed_id)
7cfde92a 2394 {
2395 /* Attempt to provide a fixit replacement hint, if
2396 we have a valid range for the component. */
2397 location_t reported_loc
2398 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
2a990300 2399 gcc_rich_location rich_loc (reported_loc);
7cfde92a 2400 if (component_loc != UNKNOWN_LOCATION)
2a990300 2401 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
7cfde92a 2402 error_at_rich_loc
2403 (&rich_loc,
2404 "%qT has no member named %qE; did you mean %qE?",
2405 type, component, guessed_id);
2406 }
b67f0560 2407 else
2408 error_at (loc, "%qT has no member named %qE", type, component);
628fa4f8 2409 return error_mark_node;
2410 }
628fa4f8 2411
37e2d32f 2412 /* Accessing elements of atomic structures or unions is undefined
2413 behavior (C11 6.5.2.3#5). */
2414 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2415 {
2416 if (code == RECORD_TYPE)
2417 warning_at (loc, 0, "accessing a member %qE of an atomic "
2418 "structure %qE", component, datum);
2419 else
2420 warning_at (loc, 0, "accessing a member %qE of an atomic "
2421 "union %qE", component, datum);
2422 }
2423
9303bbca 2424 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2425 This might be better solved in future the way the C++ front
2426 end does it - by giving the anonymous entities each a
2427 separate name and type, and then have build_component_ref
2428 recursively call itself. We can't do that here. */
04416275 2429 do
37c005bb 2430 {
9303bbca 2431 tree subdatum = TREE_VALUE (field);
62e58612 2432 int quals;
2433 tree subtype;
7666d572 2434 bool use_datum_quals;
9303bbca 2435
2436 if (TREE_TYPE (subdatum) == error_mark_node)
2437 return error_mark_node;
2438
7666d572 2439 /* If this is an rvalue, it does not have qualifiers in C
2440 standard terms and we must avoid propagating such
2441 qualifiers down to a non-lvalue array that is then
2442 converted to a pointer. */
2443 use_datum_quals = (datum_lvalue
2444 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2445
62e58612 2446 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
7666d572 2447 if (use_datum_quals)
2448 quals |= TYPE_QUALS (TREE_TYPE (datum));
62e58612 2449 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2450
2451 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
14ae0310 2452 NULL_TREE);
e60a6f7b 2453 SET_EXPR_LOCATION (ref, loc);
7666d572 2454 if (TREE_READONLY (subdatum)
2455 || (use_datum_quals && TREE_READONLY (datum)))
37c005bb 2456 TREE_READONLY (ref) = 1;
7666d572 2457 if (TREE_THIS_VOLATILE (subdatum)
2458 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
37c005bb 2459 TREE_THIS_VOLATILE (ref) = 1;
88da234d 2460
2461 if (TREE_DEPRECATED (subdatum))
45c4e798 2462 warn_deprecated_use (subdatum, NULL_TREE);
88da234d 2463
37c005bb 2464 datum = ref;
04416275 2465
2466 field = TREE_CHAIN (field);
37c005bb 2467 }
04416275 2468 while (field);
37c005bb 2469
628fa4f8 2470 return ref;
2471 }
734caf84 2472 else if (should_suggest_deref_p (type))
2473 {
2474 /* Special-case the error message for "ptr.field" for the case
2475 where the user has confused "." vs "->". */
2476 rich_location richloc (line_table, loc);
2477 /* "loc" should be the "." token. */
850c2009 2478 richloc.add_fixit_replace ("->");
734caf84 2479 error_at_rich_loc (&richloc,
2480 "%qE is a pointer; did you mean to use %<->%>?",
2481 datum);
2482 return error_mark_node;
2483 }
628fa4f8 2484 else if (code != ERROR_MARK)
e60a6f7b 2485 error_at (loc,
2486 "request for member %qE in something not a structure or union",
2487 component);
628fa4f8 2488
2489 return error_mark_node;
2490}
2491\f
2492/* Given an expression PTR for a pointer, return an expression
2493 for the value pointed to.
dda49785 2494 ERRORSTRING is the name of the operator to appear in error messages.
2495
2496 LOC is the location to use for the generated tree. */
628fa4f8 2497
2498tree
f08923b3 2499build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
628fa4f8 2500{
19cb6b50 2501 tree pointer = default_conversion (ptr);
2502 tree type = TREE_TYPE (pointer);
dda49785 2503 tree ref;
628fa4f8 2504
2505 if (TREE_CODE (type) == POINTER_TYPE)
bfbc4d3a 2506 {
72dd6141 2507 if (CONVERT_EXPR_P (pointer)
e6fa0ea6 2508 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2509 {
2510 /* If a warning is issued, mark it to avoid duplicates from
2511 the backend. This only needs to be done at
2512 warn_strict_aliasing > 2. */
2513 if (warn_strict_aliasing > 2)
2514 if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)),
2515 type, TREE_OPERAND (pointer, 0)))
2516 TREE_NO_WARNING (pointer) = 1;
2517 }
2518
bfbc4d3a 2519 if (TREE_CODE (pointer) == ADDR_EXPR
bfbc4d3a 2520 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2521 == TREE_TYPE (type)))
dda49785 2522 {
2523 ref = TREE_OPERAND (pointer, 0);
2524 protected_set_expr_location (ref, loc);
2525 return ref;
2526 }
bfbc4d3a 2527 else
2528 {
2529 tree t = TREE_TYPE (type);
aebc8537 2530
ca136233 2531 ref = build1 (INDIRECT_REF, t, pointer);
628fa4f8 2532
fe2c198f 2533 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
bfbc4d3a 2534 {
d9c77450 2535 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2536 {
2537 error_at (loc, "dereferencing pointer to incomplete type "
2538 "%qT", t);
2539 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2540 }
bfbc4d3a 2541 return error_mark_node;
2542 }
48d94ede 2543 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
b6889cb0 2544 warning_at (loc, 0, "dereferencing %<void *%> pointer");
bfbc4d3a 2545
2546 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2547 so that we get the proper error message if the result is used
2548 to assign to. Also, &* is supposed to be a no-op.
2549 And ANSI C seems to specify that the type of the result
2550 should be the const type. */
2551 /* A de-reference of a pointer to const is not a const. It is valid
2552 to change it via some other pointer. */
2553 TREE_READONLY (ref) = TYPE_READONLY (t);
2554 TREE_SIDE_EFFECTS (ref)
c1771f2c 2555 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
618c9975 2556 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
dda49785 2557 protected_set_expr_location (ref, loc);
bfbc4d3a 2558 return ref;
2559 }
2560 }
628fa4f8 2561 else if (TREE_CODE (pointer) != ERROR_MARK)
b1bbc8e5 2562 invalid_indirection_error (loc, type, errstring);
2563
628fa4f8 2564 return error_mark_node;
2565}
2566
2567/* This handles expressions of the form "a[i]", which denotes
2568 an array reference.
2569
2570 This is logically equivalent in C to *(a+i), but we may do it differently.
2571 If A is a variable or a member, we generate a primitive ARRAY_REF.
2572 This avoids forcing the array out of registers, and can work on
2573 arrays that are not lvalues (for example, members of structures returned
dda49785 2574 by functions).
2575
93426222 2576 For vector types, allow vector[i] but not i[vector], and create
2577 *(((type*)&vectortype) + i) for the expression.
2578
dda49785 2579 LOC is the location to use for the returned expression. */
628fa4f8 2580
2581tree
e60a6f7b 2582build_array_ref (location_t loc, tree array, tree index)
628fa4f8 2583{
dda49785 2584 tree ret;
20931c08 2585 bool swapped = false;
628fa4f8 2586 if (TREE_TYPE (array) == error_mark_node
2587 || TREE_TYPE (index) == error_mark_node)
2588 return error_mark_node;
2589
a89e6c15 2590 if (flag_cilkplus && contains_array_notation_expr (index))
3c6d4197 2591 {
2592 size_t rank = 0;
2593 if (!find_rank (loc, index, index, true, &rank))
2594 return error_mark_node;
2595 if (rank > 1)
2596 {
2597 error_at (loc, "rank of the array's index is greater than 1");
2598 return error_mark_node;
2599 }
2600 }
20931c08 2601 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
93426222 2602 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2603 /* Allow vector[index] but not index[vector]. */
6290f0db 2604 && !VECTOR_TYPE_P (TREE_TYPE (array)))
628fa4f8 2605 {
20931c08 2606 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2607 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
c4f6b7e5 2608 {
6cf89e04 2609 error_at (loc,
93426222 2610 "subscripted value is neither array nor pointer nor vector");
2611
c4f6b7e5 2612 return error_mark_node;
2613 }
dfcf26a5 2614 std::swap (array, index);
20931c08 2615 swapped = true;
2616 }
2617
2618 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2619 {
3bc1df45 2620 error_at (loc, "array subscript is not an integer");
20931c08 2621 return error_mark_node;
2622 }
2623
2624 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2625 {
3bc1df45 2626 error_at (loc, "subscripted value is pointer to function");
20931c08 2627 return error_mark_node;
2628 }
2629
2840aae4 2630 /* ??? Existing practice has been to warn only when the char
2631 index is syntactically the index, not for char[array]. */
2632 if (!swapped)
92b63884 2633 warn_array_subscript_with_type_char (loc, index);
20931c08 2634
2635 /* Apply default promotions *after* noticing character types. */
2636 index = default_conversion (index);
a2bbd0a7 2637 if (index == error_mark_node)
2638 return error_mark_node;
20931c08 2639
2640 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
6cf89e04 2641
7345b977 2642 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2643 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
20931c08 2644
2645 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2646 {
8e31b4b2 2647 tree rval, type;
c4f6b7e5 2648
628fa4f8 2649 /* An array that is indexed by a non-constant
2650 cannot be stored in a register; we must be able to do
2651 address arithmetic on its address.
2652 Likewise an array of elements of variable size. */
2653 if (TREE_CODE (index) != INTEGER_CST
4b72716d 2654 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
628fa4f8 2655 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2656 {
9b86eec0 2657 if (!c_mark_addressable (array))
628fa4f8 2658 return error_mark_node;
2659 }
754b3ff5 2660 /* An array that is indexed by a constant value which is not within
2661 the array bounds cannot be stored in a register either; because we
2662 would get a crash in store_bit_field/extract_bit_field when trying
2663 to access a non-existent part of the register. */
2664 if (TREE_CODE (index) == INTEGER_CST
82bb2115 2665 && TYPE_DOMAIN (TREE_TYPE (array))
84166705 2666 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
754b3ff5 2667 {
9b86eec0 2668 if (!c_mark_addressable (array))
754b3ff5 2669 return error_mark_node;
2670 }
628fa4f8 2671
7345b977 2672 if ((pedantic || warn_c90_c99_compat)
2673 && ! was_vector)
628fa4f8 2674 {
2675 tree foo = array;
2676 while (TREE_CODE (foo) == COMPONENT_REF)
2677 foo = TREE_OPERAND (foo, 0);
f48c7f4a 2678 if (VAR_P (foo) && C_DECL_REGISTER (foo))
29438999 2679 pedwarn (loc, OPT_Wpedantic,
8864917d 2680 "ISO C forbids subscripting %<register%> array");
890c2e2f 2681 else if (!lvalue_p (foo))
2682 pedwarn_c90 (loc, OPT_Wpedantic,
2683 "ISO C90 forbids subscripting non-lvalue "
2684 "array");
628fa4f8 2685 }
2686
aebc8537 2687 type = TREE_TYPE (TREE_TYPE (array));
14ae0310 2688 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
628fa4f8 2689 /* Array ref is const/volatile if the array elements are
a0c938f0 2690 or if the array is. */
628fa4f8 2691 TREE_READONLY (rval)
2692 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2693 | TREE_READONLY (array));
2694 TREE_SIDE_EFFECTS (rval)
2695 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2696 | TREE_SIDE_EFFECTS (array));
2697 TREE_THIS_VOLATILE (rval)
2698 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2699 /* This was added by rms on 16 Nov 91.
2b30d46c 2700 It fixes vol struct foo *a; a->elts[1]
628fa4f8 2701 in an inline function.
2702 Hope it doesn't break something else. */
2703 | TREE_THIS_VOLATILE (array));
22a3f7bd 2704 ret = require_complete_type (loc, rval);
dda49785 2705 protected_set_expr_location (ret, loc);
c61ef207 2706 if (non_lvalue)
2707 ret = non_lvalue_loc (loc, ret);
dda49785 2708 return ret;
628fa4f8 2709 }
20931c08 2710 else
2711 {
2712 tree ar = default_conversion (array);
628fa4f8 2713
20931c08 2714 if (ar == error_mark_node)
2715 return ar;
628fa4f8 2716
20931c08 2717 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2718 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
628fa4f8 2719
c61ef207 2720 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
2721 index, 0),
2722 RO_ARRAY_INDEXING);
2723 if (non_lvalue)
2724 ret = non_lvalue_loc (loc, ret);
2725 return ret;
20931c08 2726 }
628fa4f8 2727}
2728\f
1beb61f9 2729/* Build an external reference to identifier ID. FUN indicates
8dc938b9 2730 whether this will be used for a function call. LOC is the source
841fdaa6 2731 location of the identifier. This sets *TYPE to the type of the
2732 identifier, which is not the same as the type of the returned value
2733 for CONST_DECLs defined as enum constants. If the type of the
2734 identifier is not available, *TYPE is set to NULL. */
1beb61f9 2735tree
e60a6f7b 2736build_external_ref (location_t loc, tree id, int fun, tree *type)
1beb61f9 2737{
2738 tree ref;
2739 tree decl = lookup_name (id);
e913c82d 2740
2741 /* In Objective-C, an instance variable (ivar) may be preferred to
2742 whatever lookup_name() found. */
2743 decl = objc_lookup_ivar (decl, id);
1beb61f9 2744
841fdaa6 2745 *type = NULL;
31ddae9f 2746 if (decl && decl != error_mark_node)
841fdaa6 2747 {
2748 ref = decl;
2749 *type = TREE_TYPE (ref);
2750 }
31ddae9f 2751 else if (fun)
2752 /* Implicit function declaration. */
e60a6f7b 2753 ref = implicitly_declare (loc, id);
31ddae9f 2754 else if (decl == error_mark_node)
2755 /* Don't complain about something that's already been
2756 complained about. */
2757 return error_mark_node;
2758 else
2759 {
e60a6f7b 2760 undeclared_variable (loc, id);
31ddae9f 2761 return error_mark_node;
2762 }
1beb61f9 2763
2764 if (TREE_TYPE (ref) == error_mark_node)
2765 return error_mark_node;
2766
31ddae9f 2767 if (TREE_DEPRECATED (ref))
45c4e798 2768 warn_deprecated_use (ref, NULL_TREE);
31ddae9f 2769
3d053e06 2770 /* Recursive call does not count as usage. */
48e1416a 2771 if (ref != current_function_decl)
3d053e06 2772 {
3d053e06 2773 TREE_USED (ref) = 1;
2774 }
1beb61f9 2775
9823d3a9 2776 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2777 {
2778 if (!in_sizeof && !in_typeof)
2779 C_DECL_USED (ref) = 1;
2780 else if (DECL_INITIAL (ref) == 0
2781 && DECL_EXTERNAL (ref)
2782 && !TREE_PUBLIC (ref))
2783 record_maybe_used_decl (ref);
2784 }
2785
1beb61f9 2786 if (TREE_CODE (ref) == CONST_DECL)
2787 {
89c30811 2788 used_types_insert (TREE_TYPE (ref));
0b09525f 2789
2790 if (warn_cxx_compat
2791 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2792 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2793 {
2794 warning_at (loc, OPT_Wc___compat,
2795 ("enum constant defined in struct or union "
2796 "is not visible in C++"));
2797 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2798 }
2799
1beb61f9 2800 ref = DECL_INITIAL (ref);
2801 TREE_CONSTANT (ref) = 1;
2802 }
f3d3a8e2 2803 else if (current_function_decl != 0
3a2bf8d3 2804 && !DECL_FILE_SCOPE_P (current_function_decl)
b443c459 2805 && (VAR_OR_FUNCTION_DECL_P (ref)
2806 || TREE_CODE (ref) == PARM_DECL))
f3d3a8e2 2807 {
2808 tree context = decl_function_context (ref);
2b30d46c 2809
f3d3a8e2 2810 if (context != 0 && context != current_function_decl)
2811 DECL_NONLOCAL (ref) = 1;
2812 }
c42cc79e 2813 /* C99 6.7.4p3: An inline definition of a function with external
2814 linkage ... shall not contain a reference to an identifier with
2815 internal linkage. */
2816 else if (current_function_decl != 0
2817 && DECL_DECLARED_INLINE_P (current_function_decl)
2818 && DECL_EXTERNAL (current_function_decl)
2819 && VAR_OR_FUNCTION_DECL_P (ref)
f48c7f4a 2820 && (!VAR_P (ref) || TREE_STATIC (ref))
c55e2943 2821 && ! TREE_PUBLIC (ref)
2822 && DECL_CONTEXT (ref) != current_function_decl)
551ed71a 2823 record_inline_static (loc, current_function_decl, ref,
2824 csi_internal);
1beb61f9 2825
2826 return ref;
2827}
2828
9823d3a9 2829/* Record details of decls possibly used inside sizeof or typeof. */
2830struct maybe_used_decl
2831{
2832 /* The decl. */
2833 tree decl;
2834 /* The level seen at (in_sizeof + in_typeof). */
2835 int level;
2836 /* The next one at this level or above, or NULL. */
2837 struct maybe_used_decl *next;
2838};
2839
2840static struct maybe_used_decl *maybe_used_decls;
2841
2842/* Record that DECL, an undefined static function reference seen
2843 inside sizeof or typeof, might be used if the operand of sizeof is
2844 a VLA type or the operand of typeof is a variably modified
2845 type. */
2846
91ffb709 2847static void
9823d3a9 2848record_maybe_used_decl (tree decl)
2849{
2850 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2851 t->decl = decl;
2852 t->level = in_sizeof + in_typeof;
2853 t->next = maybe_used_decls;
2854 maybe_used_decls = t;
2855}
2856
2857/* Pop the stack of decls possibly used inside sizeof or typeof. If
2858 USED is false, just discard them. If it is true, mark them used
2859 (if no longer inside sizeof or typeof) or move them to the next
2860 level up (if still inside sizeof or typeof). */
2861
2862void
2863pop_maybe_used (bool used)
2864{
2865 struct maybe_used_decl *p = maybe_used_decls;
2866 int cur_level = in_sizeof + in_typeof;
2867 while (p && p->level > cur_level)
2868 {
2869 if (used)
2870 {
2871 if (cur_level == 0)
2872 C_DECL_USED (p->decl) = 1;
2873 else
2874 p->level = cur_level;
2875 }
2876 p = p->next;
2877 }
2878 if (!used || cur_level == 0)
2879 maybe_used_decls = p;
2880}
2881
2882/* Return the result of sizeof applied to EXPR. */
2883
2884struct c_expr
e60a6f7b 2885c_expr_sizeof_expr (location_t loc, struct c_expr expr)
9823d3a9 2886{
2887 struct c_expr ret;
7d32f35d 2888 if (expr.value == error_mark_node)
2889 {
2890 ret.value = error_mark_node;
2891 ret.original_code = ERROR_MARK;
841fdaa6 2892 ret.original_type = NULL;
7d32f35d 2893 pop_maybe_used (false);
2894 }
2895 else
2896 {
a75b1c71 2897 bool expr_const_operands = true;
87d59e72 2898
2899 if (TREE_CODE (expr.value) == PARM_DECL
2900 && C_ARRAY_PARAMETER (expr.value))
2901 {
2902 if (warning_at (loc, OPT_Wsizeof_array_argument,
2903 "%<sizeof%> on array function parameter %qE will "
2904 "return size of %qT", expr.value,
2905 expr.original_type))
2906 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2907 }
a75b1c71 2908 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2909 &expr_const_operands);
e60a6f7b 2910 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
f003f5dc 2911 c_last_sizeof_arg = expr.value;
2912 ret.original_code = SIZEOF_EXPR;
841fdaa6 2913 ret.original_type = NULL;
a75b1c71 2914 if (c_vla_type_p (TREE_TYPE (folded_expr)))
32d33ab2 2915 {
2916 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
a75b1c71 2917 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2918 folded_expr, ret.value);
2919 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
e60a6f7b 2920 SET_EXPR_LOCATION (ret.value, loc);
32d33ab2 2921 }
a75b1c71 2922 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
7d32f35d 2923 }
9823d3a9 2924 return ret;
2925}
2926
2927/* Return the result of sizeof applied to T, a structure for the type
e60a6f7b 2928 name passed to sizeof (rather than the type itself). LOC is the
2929 location of the original expression. */
9823d3a9 2930
2931struct c_expr
e60a6f7b 2932c_expr_sizeof_type (location_t loc, struct c_type_name *t)
9823d3a9 2933{
2934 tree type;
2935 struct c_expr ret;
a75b1c71 2936 tree type_expr = NULL_TREE;
2937 bool type_expr_const = true;
2938 type = groktypename (t, &type_expr, &type_expr_const);
e60a6f7b 2939 ret.value = c_sizeof (loc, type);
f003f5dc 2940 c_last_sizeof_arg = type;
2941 ret.original_code = SIZEOF_EXPR;
841fdaa6 2942 ret.original_type = NULL;
e8074982 2943 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2944 && c_vla_type_p (type))
a75b1c71 2945 {
e8074982 2946 /* If the type is a [*] array, it is a VLA but is represented as
2947 having a size of zero. In such a case we must ensure that
2948 the result of sizeof does not get folded to a constant by
2949 c_fully_fold, because if the size is evaluated the result is
2950 not constant and so constraints on zero or negative size
2951 arrays must not be applied when this sizeof call is inside
2952 another array declarator. */
2953 if (!type_expr)
2954 type_expr = integer_zero_node;
a75b1c71 2955 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2956 type_expr, ret.value);
2957 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2958 }
8a98ec4a 2959 pop_maybe_used (type != error_mark_node
2960 ? C_TYPE_VARIABLE_SIZE (type) : false);
9823d3a9 2961 return ret;
2962}
2963
628fa4f8 2964/* Build a function call to function FUNCTION with parameters PARAMS.
e60a6f7b 2965 The function call is at LOC.
628fa4f8 2966 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2967 TREE_VALUE of each node is a parameter-expression.
2968 FUNCTION's data type may be a function type or a pointer-to-function. */
2969
2970tree
e60a6f7b 2971build_function_call (location_t loc, tree function, tree params)
b9c74b4d 2972{
f1f41a6c 2973 vec<tree, va_gc> *v;
b9c74b4d 2974 tree ret;
2975
f1f41a6c 2976 vec_alloc (v, list_length (params));
b9c74b4d 2977 for (; params; params = TREE_CHAIN (params))
f1f41a6c 2978 v->quick_push (TREE_VALUE (params));
5a672e62 2979 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
f1f41a6c 2980 vec_free (v);
b9c74b4d 2981 return ret;
2982}
2983
28738b20 2984/* Give a note about the location of the declaration of DECL. */
2985
5650b4ff 2986static void
2987inform_declaration (tree decl)
28738b20 2988{
5650b4ff 2989 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
28738b20 2990 inform (DECL_SOURCE_LOCATION (decl), "declared here");
2991}
2992
b9c74b4d 2993/* Build a function call to function FUNCTION with parameters PARAMS.
2994 ORIGTYPES, if not NULL, is a vector of types; each element is
2995 either NULL or the original type of the corresponding element in
2996 PARAMS. The original type may differ from TREE_TYPE of the
2997 parameter for enums. FUNCTION's data type may be a function type
2998 or pointer-to-function. This function changes the elements of
2999 PARAMS. */
3000
3001tree
ec761d5a 3002build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3003 tree function, vec<tree, va_gc> *params,
f1f41a6c 3004 vec<tree, va_gc> *origtypes)
628fa4f8 3005{
19cb6b50 3006 tree fntype, fundecl = 0;
805e22b2 3007 tree name = NULL_TREE, result;
59798ded 3008 tree tem;
d01f58f9 3009 int nargs;
3010 tree *argarray;
48e1416a 3011
628fa4f8 3012
a390f7f2 3013 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
fe4d64a0 3014 STRIP_TYPE_NOPS (function);
628fa4f8 3015
3016 /* Convert anything with function type to a pointer-to-function. */
3017 if (TREE_CODE (function) == FUNCTION_DECL)
3018 {
3019 name = DECL_NAME (function);
4c0315d0 3020
3021 if (flag_tm)
3022 tm_malloc_replacement (function);
25fd8fda 3023 fundecl = function;
1cd6e20d 3024 /* Atomic functions have type checking/casting already done. They are
3025 often rewritten and don't match the original parameter list. */
3026 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3027 origtypes = NULL;
3c6d4197 3028
a89e6c15 3029 if (flag_cilkplus
3c6d4197 3030 && is_cilkplus_reduce_builtin (function))
3031 origtypes = NULL;
628fa4f8 3032 }
e78cf9a7 3033 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
e60a6f7b 3034 function = function_to_pointer_conversion (loc, function);
628fa4f8 3035
ddb5d39d 3036 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3037 expressions, like those used for ObjC messenger dispatches. */
f1f41a6c 3038 if (params && !params->is_empty ())
3039 function = objc_rewrite_function_call (function, (*params)[0]);
ddb5d39d 3040
a75b1c71 3041 function = c_fully_fold (function, false, NULL);
3042
628fa4f8 3043 fntype = TREE_TYPE (function);
3044
3045 if (TREE_CODE (fntype) == ERROR_MARK)
3046 return error_mark_node;
3047
3048 if (!(TREE_CODE (fntype) == POINTER_TYPE
3049 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3050 {
28738b20 3051 if (!flag_diagnostics_show_caret)
3052 error_at (loc,
3053 "called object %qE is not a function or function pointer",
3054 function);
3055 else if (DECL_P (function))
3056 {
3057 error_at (loc,
3058 "called object %qD is not a function or function pointer",
3059 function);
3060 inform_declaration (function);
3061 }
3062 else
3063 error_at (loc,
3064 "called object is not a function or function pointer");
628fa4f8 3065 return error_mark_node;
3066 }
3067
af857942 3068 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3069 current_function_returns_abnormally = 1;
3070
628fa4f8 3071 /* fntype now gets the type of function pointed to. */
3072 fntype = TREE_TYPE (fntype);
3073
ad0e7a06 3074 /* Convert the parameters to the types declared in the
3075 function prototype, or apply default promotions. */
3076
ec761d5a 3077 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3078 origtypes, function, fundecl);
ad0e7a06 3079 if (nargs < 0)
3080 return error_mark_node;
3081
59798ded 3082 /* Check that the function is called through a compatible prototype.
29f580ac 3083 If it is not, warn. */
72dd6141 3084 if (CONVERT_EXPR_P (function)
59798ded 3085 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3086 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
84166705 3087 && !comptypes (fntype, TREE_TYPE (tem)))
59798ded 3088 {
3089 tree return_type = TREE_TYPE (fntype);
59798ded 3090
3091 /* This situation leads to run-time undefined behavior. We can't,
3092 therefore, simply error unless we can prove that all possible
3093 executions of the program must execute the code. */
29f580ac 3094 warning_at (loc, 0, "function called through a non-compatible type");
59798ded 3095
29f580ac 3096 if (VOID_TYPE_P (return_type)
3097 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3098 pedwarn (loc, 0,
3099 "function with qualified void return type called");
3100 }
59798ded 3101
f1f41a6c 3102 argarray = vec_safe_address (params);
b9c74b4d 3103
d43cee80 3104 /* Check that arguments to builtin functions match the expectations. */
3105 if (fundecl
3106 && DECL_BUILT_IN (fundecl)
3107 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
5d4db8ef 3108 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3109 argarray))
d43cee80 3110 return error_mark_node;
628fa4f8 3111
d43cee80 3112 /* Check that the arguments to the function are valid. */
bda05c57 3113 check_function_arguments (loc, fntype, nargs, argarray);
628fa4f8 3114
a75b1c71 3115 if (name != NULL_TREE
3116 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
16cb78b3 3117 {
a75b1c71 3118 if (require_constant_value)
48e1416a 3119 result =
389dd41b 3120 fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3121 function, nargs, argarray);
a75b1c71 3122 else
389dd41b 3123 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3124 function, nargs, argarray);
a75b1c71 3125 if (TREE_CODE (result) == NOP_EXPR
3126 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3127 STRIP_TYPE_NOPS (result);
16cb78b3 3128 }
3129 else
389dd41b 3130 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
650e4c94 3132
462ca422 3133 /* In this improbable scenario, a nested function returns a VM type.
3134 Create a TARGET_EXPR so that the call always has a LHS, much as
3135 what the C++ FE does for functions returning non-PODs. */
3136 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3137 {
3138 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3139 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3140 NULL_TREE, NULL_TREE);
3141 }
3142
5c803577 3143 if (VOID_TYPE_P (TREE_TYPE (result)))
8f24e4bd 3144 {
3145 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
389dd41b 3146 pedwarn (loc, 0,
8f24e4bd 3147 "function with qualified void return type called");
3148 return result;
3149 }
22a3f7bd 3150 return require_complete_type (loc, result);
628fa4f8 3151}
5a672e62 3152
3153/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3154
3155tree
3156c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3157 tree function, vec<tree, va_gc> *params,
3158 vec<tree, va_gc> *origtypes)
3159{
3160 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3161 STRIP_TYPE_NOPS (function);
3162
3163 /* Convert anything with function type to a pointer-to-function. */
3164 if (TREE_CODE (function) == FUNCTION_DECL)
3165 {
3166 /* Implement type-directed function overloading for builtins.
3167 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3168 handle all the type checking. The result is a complete expression
3169 that implements this function call. */
3170 tree tem = resolve_overloaded_builtin (loc, function, params);
3171 if (tem)
3172 return tem;
3173 }
3174 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3175}
628fa4f8 3176\f
b9c74b4d 3177/* Convert the argument expressions in the vector VALUES
3178 to the types in the list TYPELIST.
628fa4f8 3179
3180 If TYPELIST is exhausted, or when an element has NULL as its type,
3181 perform the default conversions.
3182
b9c74b4d 3183 ORIGTYPES is the original types of the expressions in VALUES. This
3184 holds the type of enum values which have been converted to integral
3185 types. It may be NULL.
628fa4f8 3186
8ccb0952 3187 FUNCTION is a tree for the called function. It is used only for
3188 error messages, where it is formatted with %qE.
628fa4f8 3189
3190 This is also where warnings about wrong number of args are generated.
3191
ec761d5a 3192 ARG_LOC are locations of function arguments (if any).
3193
d01f58f9 3194 Returns the actual number of arguments processed (which may be less
b9c74b4d 3195 than the length of VALUES in some error situations), or -1 on
3196 failure. */
d01f58f9 3197
3198static int
ec761d5a 3199convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3200 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3201 tree function, tree fundecl)
628fa4f8 3202{
b9c74b4d 3203 tree typetail, val;
3204 unsigned int parmnum;
cb60919f 3205 bool error_args = false;
b5c26b42 3206 const bool type_generic = fundecl
ec761d5a 3207 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
c6418a4e 3208 bool type_generic_remove_excess_precision = false;
732905bb 3209 bool type_generic_overflow_p = false;
8ccb0952 3210 tree selector;
8ccb0952 3211
245d6740 3212 /* Change pointer to function to the function itself for
3213 diagnostics. */
8ccb0952 3214 if (TREE_CODE (function) == ADDR_EXPR
3215 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
245d6740 3216 function = TREE_OPERAND (function, 0);
8ccb0952 3217
3218 /* Handle an ObjC selector specially for diagnostics. */
3219 selector = objc_message_selector ();
628fa4f8 3220
c6418a4e 3221 /* For type-generic built-in functions, determine whether excess
3222 precision should be removed (classification) or not
3223 (comparison). */
3224 if (type_generic
3225 && DECL_BUILT_IN (fundecl)
3226 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3227 {
3228 switch (DECL_FUNCTION_CODE (fundecl))
3229 {
3230 case BUILT_IN_ISFINITE:
3231 case BUILT_IN_ISINF:
3232 case BUILT_IN_ISINF_SIGN:
3233 case BUILT_IN_ISNAN:
3234 case BUILT_IN_ISNORMAL:
3235 case BUILT_IN_FPCLASSIFY:
3236 type_generic_remove_excess_precision = true;
3237 break;
3238
732905bb 3239 case BUILT_IN_ADD_OVERFLOW_P:
3240 case BUILT_IN_SUB_OVERFLOW_P:
3241 case BUILT_IN_MUL_OVERFLOW_P:
3242 /* The last argument of these type-generic builtins
3243 should not be promoted. */
3244 type_generic_overflow_p = true;
3245 break;
3246
c6418a4e 3247 default:
c6418a4e 3248 break;
3249 }
3250 }
a89e6c15 3251 if (flag_cilkplus && fundecl && is_cilkplus_reduce_builtin (fundecl))
6e13bc01 3252 return vec_safe_length (values);
c6418a4e 3253
628fa4f8 3254 /* Scan the given expressions and types, producing individual
b9c74b4d 3255 converted arguments. */
628fa4f8 3256
b9c74b4d 3257 for (typetail = typelist, parmnum = 0;
f1f41a6c 3258 values && values->iterate (parmnum, &val);
b9c74b4d 3259 ++parmnum)
628fa4f8 3260 {
19cb6b50 3261 tree type = typetail ? TREE_VALUE (typetail) : 0;
c6418a4e 3262 tree valtype = TREE_TYPE (val);
8ccb0952 3263 tree rname = function;
3264 int argnum = parmnum + 1;
10fda9eb 3265 const char *invalid_func_diag;
c6418a4e 3266 bool excess_precision = false;
a75b1c71 3267 bool npc;
b9c74b4d 3268 tree parmval;
dcfff6f9 3269 /* Some __atomic_* builtins have additional hidden argument at
3270 position 0. */
3271 location_t ploc
3272 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3273 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3274 : input_location;
628fa4f8 3275
3276 if (type == void_type_node)
3277 {
1f3a51d6 3278 if (selector)
22a75734 3279 error_at (loc, "too many arguments to method %qE", selector);
1f3a51d6 3280 else
22a75734 3281 error_at (loc, "too many arguments to function %qE", function);
28738b20 3282 inform_declaration (fundecl);
87778dc4 3283 return error_args ? -1 : (int) parmnum;
628fa4f8 3284 }
3285
8ccb0952 3286 if (selector && argnum > 2)
3287 {
3288 rname = selector;
3289 argnum -= 2;
3290 }
3291
a75b1c71 3292 npc = null_pointer_constant_p (val);
c6418a4e 3293
3294 /* If there is excess precision and a prototype, convert once to
3295 the required type rather than converting via the semantic
3296 type. Likewise without a prototype a float value represented
3297 as long double should be converted once to double. But for
3298 type-generic classification functions excess precision must
3299 be removed here. */
3300 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3301 && (type || !type_generic || !type_generic_remove_excess_precision))
3302 {
3303 val = TREE_OPERAND (val, 0);
3304 excess_precision = true;
3305 }
a75b1c71 3306 val = c_fully_fold (val, false, NULL);
a97c952c 3307 STRIP_TYPE_NOPS (val);
628fa4f8 3308
22a3f7bd 3309 val = require_complete_type (ploc, val);
628fa4f8 3310
82c85aba 3311 /* Some floating-point arguments must be promoted to double when
3312 no type is specified by a prototype. This applies to
3313 arguments of type float, and to architecture-specific types
3314 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3315 bool promote_float_arg = false;
3316 if (type == NULL_TREE
3317 && TREE_CODE (valtype) == REAL_TYPE
3318 && (TYPE_PRECISION (valtype)
3319 <= TYPE_PRECISION (double_type_node))
3320 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3321 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3322 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3323 {
3324 /* Promote this argument, unless it has a _FloatN or
3325 _FloatNx type. */
3326 promote_float_arg = true;
3327 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3328 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3329 {
3330 promote_float_arg = false;
3331 break;
3332 }
3333 }
3334
628fa4f8 3335 if (type != 0)
3336 {
3337 /* Formal parm type is specified by a function prototype. */
628fa4f8 3338
d487ebe8 3339 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
628fa4f8 3340 {
dcfff6f9 3341 error_at (ploc, "type of formal parameter %d is incomplete",
3342 parmnum + 1);
628fa4f8 3343 parmval = val;
3344 }
3345 else
3346 {
b9c74b4d 3347 tree origtype;
3348
f9e15121 3349 /* Optionally warn about conversions that
3350 differ from the default conversions. */
583272a2 3351 if (warn_traditional_conversion || warn_traditional)
628fa4f8 3352 {
e916c70c 3353 unsigned int formal_prec = TYPE_PRECISION (type);
628fa4f8 3354
054521db 3355 if (INTEGRAL_TYPE_P (type)
c6418a4e 3356 && TREE_CODE (valtype) == REAL_TYPE)
dcfff6f9 3357 warning_at (ploc, OPT_Wtraditional_conversion,
3358 "passing argument %d of %qE as integer rather "
3359 "than floating due to prototype",
3360 argnum, rname);
af324fb3 3361 if (INTEGRAL_TYPE_P (type)
c6418a4e 3362 && TREE_CODE (valtype) == COMPLEX_TYPE)
dcfff6f9 3363 warning_at (ploc, OPT_Wtraditional_conversion,
3364 "passing argument %d of %qE as integer rather "
3365 "than complex due to prototype",
3366 argnum, rname);
054521db 3367 else if (TREE_CODE (type) == COMPLEX_TYPE
c6418a4e 3368 && TREE_CODE (valtype) == REAL_TYPE)
dcfff6f9 3369 warning_at (ploc, OPT_Wtraditional_conversion,
3370 "passing argument %d of %qE as complex rather "
3371 "than floating due to prototype",
3372 argnum, rname);
628fa4f8 3373 else if (TREE_CODE (type) == REAL_TYPE
c6418a4e 3374 && INTEGRAL_TYPE_P (valtype))
dcfff6f9 3375 warning_at (ploc, OPT_Wtraditional_conversion,
3376 "passing argument %d of %qE as floating rather "
3377 "than integer due to prototype",
3378 argnum, rname);
af324fb3 3379 else if (TREE_CODE (type) == COMPLEX_TYPE
c6418a4e 3380 && INTEGRAL_TYPE_P (valtype))
dcfff6f9 3381 warning_at (ploc, OPT_Wtraditional_conversion,
3382 "passing argument %d of %qE as complex rather "
3383 "than integer due to prototype",
3384 argnum, rname);
054521db 3385 else if (TREE_CODE (type) == REAL_TYPE
c6418a4e 3386 && TREE_CODE (valtype) == COMPLEX_TYPE)
dcfff6f9 3387 warning_at (ploc, OPT_Wtraditional_conversion,
3388 "passing argument %d of %qE as floating rather "
3389 "than complex due to prototype",
3390 argnum, rname);
054521db 3391 /* ??? At some point, messages should be written about
3392 conversions between complex types, but that's too messy
3393 to do now. */
f9e15121 3394 else if (TREE_CODE (type) == REAL_TYPE
c6418a4e 3395 && TREE_CODE (valtype) == REAL_TYPE)
f9e15121 3396 {
3397 /* Warn if any argument is passed as `float',
66618a1e 3398 since without a prototype it would be `double'. */
c4503c0a 3399 if (formal_prec == TYPE_PRECISION (float_type_node)
3400 && type != dfloat32_type_node)
dcfff6f9 3401 warning_at (ploc, 0,
3402 "passing argument %d of %qE as %<float%> "
3403 "rather than %<double%> due to prototype",
3404 argnum, rname);
c4503c0a 3405
3406 /* Warn if mismatch between argument and prototype
3407 for decimal float types. Warn of conversions with
3408 binary float types and of precision narrowing due to
3409 prototype. */
c6418a4e 3410 else if (type != valtype
c4503c0a 3411 && (type == dfloat32_type_node
3412 || type == dfloat64_type_node
a0c938f0 3413 || type == dfloat128_type_node
c6418a4e 3414 || valtype == dfloat32_type_node
3415 || valtype == dfloat64_type_node
3416 || valtype == dfloat128_type_node)
a0c938f0 3417 && (formal_prec
c6418a4e 3418 <= TYPE_PRECISION (valtype)
c4503c0a 3419 || (type == dfloat128_type_node
c6418a4e 3420 && (valtype
a0c938f0 3421 != dfloat64_type_node
c6418a4e 3422 && (valtype
c4503c0a 3423 != dfloat32_type_node)))
3424 || (type == dfloat64_type_node
c6418a4e 3425 && (valtype
c4503c0a 3426 != dfloat32_type_node))))
dcfff6f9 3427 warning_at (ploc, 0,
3428 "passing argument %d of %qE as %qT "
3429 "rather than %qT due to prototype",
3430 argnum, rname, type, valtype);
c4503c0a 3431
f9e15121 3432 }
261366e3 3433 /* Detect integer changing in width or signedness.
3434 These warnings are only activated with
583272a2 3435 -Wtraditional-conversion, not with -Wtraditional. */
3436 else if (warn_traditional_conversion && INTEGRAL_TYPE_P (type)
c6418a4e 3437 && INTEGRAL_TYPE_P (valtype))
628fa4f8 3438 {
f9e15121 3439 tree would_have_been = default_conversion (val);
3440 tree type1 = TREE_TYPE (would_have_been);
3441
a14622ee 3442 if (TREE_CODE (type) == ENUMERAL_TYPE
bab5a44d 3443 && (TYPE_MAIN_VARIANT (type)
c6418a4e 3444 == TYPE_MAIN_VARIANT (valtype)))
a14622ee 3445 /* No warning if function asks for enum
3446 and the actual arg is that enum type. */
3447 ;
3448 else if (formal_prec != TYPE_PRECISION (type1))
dcfff6f9 3449 warning_at (ploc, OPT_Wtraditional_conversion,
3450 "passing argument %d of %qE "
3451 "with different width due to prototype",
3452 argnum, rname);
78a8ed03 3453 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
f9e15121 3454 ;
1ca23b2d 3455 /* Don't complain if the formal parameter type
3456 is an enum, because we can't tell now whether
3457 the value was an enum--even the same enum. */
3458 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3459 ;
628fa4f8 3460 else if (TREE_CODE (val) == INTEGER_CST
3461 && int_fits_type_p (val, type))
3462 /* Change in signedness doesn't matter
3463 if a constant value is unaffected. */
3464 ;
c6abd4de 3465 /* If the value is extended from a narrower
3466 unsigned type, it doesn't matter whether we
3467 pass it as signed or unsigned; the value
3468 certainly is the same either way. */
c6418a4e 3469 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3470 && TYPE_UNSIGNED (valtype))
c6abd4de 3471 ;
78a8ed03 3472 else if (TYPE_UNSIGNED (type))
dcfff6f9 3473 warning_at (ploc, OPT_Wtraditional_conversion,
3474 "passing argument %d of %qE "
3475 "as unsigned due to prototype",
3476 argnum, rname);
261366e3 3477 else
dcfff6f9 3478 warning_at (ploc, OPT_Wtraditional_conversion,
3479 "passing argument %d of %qE "
3480 "as signed due to prototype",
3481 argnum, rname);
628fa4f8 3482 }
3483 }
3484
c6418a4e 3485 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3486 sake of better warnings from convert_and_check. */
3487 if (excess_precision)
3488 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
f1f41a6c 3489 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
dcfff6f9 3490 parmval = convert_for_assignment (loc, ploc, type,
22a75734 3491 val, origtype, ic_argpass,
3492 npc, fundecl, function,
245d6740 3493 parmnum + 1);
2b30d46c 3494
45550790 3495 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
a12f4e51 3496 && INTEGRAL_TYPE_P (type)
628fa4f8 3497 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3498 parmval = default_conversion (parmval);
628fa4f8 3499 }
628fa4f8 3500 }
82c85aba 3501 else if (promote_float_arg)
b5c26b42 3502 {
3503 if (type_generic)
b9c74b4d 3504 parmval = val;
b5c26b42 3505 else
c920faa3 3506 {
3507 /* Convert `float' to `double'. */
3508 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
dcfff6f9 3509 warning_at (ploc, OPT_Wdouble_promotion,
3510 "implicit conversion from %qT to %qT when passing "
3511 "argument to function",
3512 valtype, double_type_node);
c920faa3 3513 parmval = convert (double_type_node, val);
3514 }
b5c26b42 3515 }
732905bb 3516 else if ((excess_precision && !type_generic)
3517 || (type_generic_overflow_p && parmnum == 2))
c6418a4e 3518 /* A "double" argument with excess precision being passed
732905bb 3519 without a prototype or in variable arguments.
3520 The last argument of __builtin_*_overflow_p should not be
3521 promoted. */
b9c74b4d 3522 parmval = convert (valtype, val);
a0c938f0 3523 else if ((invalid_func_diag =
3524 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
10fda9eb 3525 {
3526 error (invalid_func_diag);
d01f58f9 3527 return -1;
10fda9eb 3528 }
547c6b1f 3529 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3530 {
3531 return -1;
3532 }
628fa4f8 3533 else
3534 /* Convert `short' and `char' to full-size `int'. */
b9c74b4d 3535 parmval = default_conversion (val);
3536
f1f41a6c 3537 (*values)[parmnum] = parmval;
cb60919f 3538 if (parmval == error_mark_node)
3539 error_args = true;
628fa4f8 3540
3541 if (typetail)
3542 typetail = TREE_CHAIN (typetail);
3543 }
3544
f1f41a6c 3545 gcc_assert (parmnum == vec_safe_length (values));
d01f58f9 3546
628fa4f8 3547 if (typetail != 0 && TREE_VALUE (typetail) != void_type_node)
7b2e6555 3548 {
22a75734 3549 error_at (loc, "too few arguments to function %qE", function);
6e13bc01 3550 inform_declaration (fundecl);
3551 return -1;
7b2e6555 3552 }
628fa4f8 3553
cb60919f 3554 return error_args ? -1 : (int) parmnum;
628fa4f8 3555}
3556\f
5381eb98 3557/* This is the entry point used by the parser to build unary operators
3558 in the input. CODE, a tree_code, specifies the unary operator, and
3559 ARG is the operand. For unary plus, the C parser currently uses
dda49785 3560 CONVERT_EXPR for code.
3561
3562 LOC is the location to use for the tree generated.
3563*/
5381eb98 3564
3565struct c_expr
e60a6f7b 3566parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
5381eb98 3567{
3568 struct c_expr result;
3569
82012ffe 3570 result.original_code = code;
841fdaa6 3571 result.original_type = NULL;
3572
547c6b1f 3573 if (reject_gcc_builtin (arg.value))
3574 {
3575 result.value = error_mark_node;
3576 }
3577 else
3578 {
b99cc6da 3579 result.value = build_unary_op (loc, code, arg.value, false);
547c6b1f 3580
e68d7b49 3581 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3582 overflow_warning (loc, result.value);
547c6b1f 3583 }
f170d67f 3584
a96cefb2 3585 /* We are typically called when parsing a prefix token at LOC acting on
3586 ARG. Reflect this by updating the source range of the result to
3587 start at LOC and end at the end of ARG. */
3588 set_c_expr_source_range (&result,
3589 loc, arg.get_finish ());
3590
5381eb98 3591 return result;
3592}
3593
3594/* This is the entry point used by the parser to build binary operators
3595 in the input. CODE, a tree_code, specifies the binary operator, and
3596 ARG1 and ARG2 are the operands. In addition to constructing the
3597 expression, we check for operands that were written with other binary
8e70fb09 3598 operators in a way that is likely to confuse the user.
3599
3600 LOCATION is the location of the binary operator. */
a9ba5ac4 3601
066f26bf 3602struct c_expr
8e70fb09 3603parser_build_binary_op (location_t location, enum tree_code code,
3604 struct c_expr arg1, struct c_expr arg2)
628fa4f8 3605{
066f26bf 3606 struct c_expr result;
628fa4f8 3607
066f26bf 3608 enum tree_code code1 = arg1.original_code;
3609 enum tree_code code2 = arg2.original_code;
841fdaa6 3610 tree type1 = (arg1.original_type
3611 ? arg1.original_type
3612 : TREE_TYPE (arg1.value));
3613 tree type2 = (arg2.original_type
3614 ? arg2.original_type
3615 : TREE_TYPE (arg2.value));
628fa4f8 3616
8e70fb09 3617 result.value = build_binary_op (location, code,
3618 arg1.value, arg2.value, 1);
066f26bf 3619 result.original_code = code;
841fdaa6 3620 result.original_type = NULL;
5eaf60b3 3621
066f26bf 3622 if (TREE_CODE (result.value) == ERROR_MARK)
97c71d71 3623 {
3624 set_c_expr_source_range (&result,
3625 arg1.get_start (),
3626 arg2.get_finish ());
3627 return result;
3628 }
628fa4f8 3629
8e70fb09 3630 if (location != UNKNOWN_LOCATION)
3631 protected_set_expr_location (result.value, location);
3632
a96cefb2 3633 set_c_expr_source_range (&result,
3634 arg1.get_start (),
3635 arg2.get_finish ());
3636
628fa4f8 3637 /* Check for cases such as x+y<<z which users are likely
066f26bf 3638 to misinterpret. */
628fa4f8 3639 if (warn_parentheses)
2623625f 3640 warn_about_parentheses (location, code, code1, arg1.value, code2,
3641 arg2.value);
8ee1dd9f 3642
03033af4 3643 if (warn_logical_op)
2623625f 3644 warn_logical_operator (location, code, TREE_TYPE (result.value),
03033af4 3645 code1, arg1.value, code2, arg2.value);
b13d1547 3646
6784a472 3647 if (warn_tautological_compare)
62efaf6b 3648 {
3649 tree lhs = arg1.value;
3650 tree rhs = arg2.value;
3651 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3652 {
3653 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3654 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3655 lhs = NULL_TREE;
3656 else
3657 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3658 }
3659 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3660 {
3661 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3662 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3663 rhs = NULL_TREE;
3664 else
3665 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3666 }
3667 if (lhs != NULL_TREE && rhs != NULL_TREE)
3668 warn_tautological_cmp (location, code, lhs, rhs);
3669 }
6784a472 3670
32dc1512 3671 if (warn_logical_not_paren
16f958b3 3672 && TREE_CODE_CLASS (code) == tcc_comparison
499b7390 3673 && code1 == TRUTH_NOT_EXPR
6054103a 3674 && code2 != TRUTH_NOT_EXPR
3675 /* Avoid warning for !!x == y. */
3676 && (TREE_CODE (arg1.value) != NE_EXPR
3677 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3678 {
3679 /* Avoid warning for !b == y where b has _Bool type. */
3680 tree t = integer_zero_node;
3681 if (TREE_CODE (arg1.value) == EQ_EXPR
3682 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3683 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3684 {
3685 t = TREE_OPERAND (arg1.value, 0);
3686 do
3687 {
3688 if (TREE_TYPE (t) != integer_type_node)
3689 break;
3690 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3691 t = C_MAYBE_CONST_EXPR_EXPR (t);
3692 else if (CONVERT_EXPR_P (t))
3693 t = TREE_OPERAND (t, 0);
3694 else
3695 break;
3696 }
3697 while (1);
3698 }
3699 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
c4963714 3700 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
6054103a 3701 }
32dc1512 3702
19f0596c 3703 /* Warn about comparisons against string literals, with the exception
3704 of testing for equality or inequality of a string literal with NULL. */
3705 if (code == EQ_EXPR || code == NE_EXPR)
3706 {
40b38ace 3707 if ((code1 == STRING_CST
3708 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3709 || (code2 == STRING_CST
3710 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
e60a6f7b 3711 warning_at (location, OPT_Waddress,
3712 "comparison with string literal results in unspecified behavior");
19f0596c 3713 }
3714 else if (TREE_CODE_CLASS (code) == tcc_comparison
3715 && (code1 == STRING_CST || code2 == STRING_CST))
e60a6f7b 3716 warning_at (location, OPT_Waddress,
3717 "comparison with string literal results in unspecified behavior");
19f0596c 3718
48e1416a 3719 if (TREE_OVERFLOW_P (result.value)
3720 && !TREE_OVERFLOW_P (arg1.value)
f170d67f 3721 && !TREE_OVERFLOW_P (arg2.value))
e60a6f7b 3722 overflow_warning (location, result.value);
628fa4f8 3723
841fdaa6 3724 /* Warn about comparisons of different enum types. */
3725 if (warn_enum_compare
3726 && TREE_CODE_CLASS (code) == tcc_comparison
3727 && TREE_CODE (type1) == ENUMERAL_TYPE
3728 && TREE_CODE (type2) == ENUMERAL_TYPE
3729 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3730 warning_at (location, OPT_Wenum_compare,
3731 "comparison between %qT and %qT",
3732 type1, type2);
3733
628fa4f8 3734 return result;
3735}
276beea2 3736\f
276beea2 3737/* Return a tree for the difference of pointers OP0 and OP1.
3738 The resulting tree has type int. */
367d2866 3739
276beea2 3740static tree
389dd41b 3741pointer_diff (location_t loc, tree op0, tree op1)
276beea2 3742{
276beea2 3743 tree restype = ptrdiff_type_node;
6d5d708e 3744 tree result, inttype;
628fa4f8 3745
6d5d708e 3746 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3747 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
276beea2 3748 tree target_type = TREE_TYPE (TREE_TYPE (op0));
276beea2 3749 tree orig_op1 = op1;
628fa4f8 3750
6d5d708e 3751 /* If the operands point into different address spaces, we need to
3752 explicitly convert them to pointers into the common address space
3753 before we can subtract the numerical address values. */
3754 if (as0 != as1)
3755 {
3756 addr_space_t as_common;
3757 tree common_type;
3758
3759 /* Determine the common superset address space. This is guaranteed
3760 to exist because the caller verified that comp_target_types
3761 returned non-zero. */
3762 if (!addr_space_superset (as0, as1, &as_common))
3763 gcc_unreachable ();
3764
3765 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3766 op0 = convert (common_type, op0);
3767 op1 = convert (common_type, op1);
3768 }
3769
3770 /* Determine integer type to perform computations in. This will usually
3771 be the same as the result type (ptrdiff_t), but may need to be a wider
3772 type if pointers for the address space are wider than ptrdiff_t. */
3773 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
3c9b85aa 3774 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
6d5d708e 3775 else
3776 inttype = restype;
3777
8864917d 3778 if (TREE_CODE (target_type) == VOID_TYPE)
9205a6cc 3779 pedwarn (loc, OPT_Wpointer_arith,
8864917d 3780 "pointer of type %<void *%> used in subtraction");
3781 if (TREE_CODE (target_type) == FUNCTION_TYPE)
9205a6cc 3782 pedwarn (loc, OPT_Wpointer_arith,
8864917d 3783 "pointer to a function used in subtraction");
628fa4f8 3784
276beea2 3785 /* First do the subtraction as integers;
3786 then drop through to build the divide operator.
3787 Do not do default conversions on the minus operator
3788 in case restype is a short type. */
628fa4f8 3789
389dd41b 3790 op0 = build_binary_op (loc,
6d5d708e 3791 MINUS_EXPR, convert (inttype, op0),
3792 convert (inttype, op1), 0);
276beea2 3793 /* This generates an error if op1 is pointer to incomplete type. */
3794 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
389dd41b 3795 error_at (loc, "arithmetic on pointer to an incomplete type");
628fa4f8 3796
276beea2 3797 op1 = c_size_in_bytes (target_type);
628fa4f8 3798
46da3601 3799 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3800 error_at (loc, "arithmetic on pointer to an empty aggregate");
3801
276beea2 3802 /* Divide by the size, in easiest possible way. */
6d5d708e 3803 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3804 op0, convert (inttype, op1));
3805
3806 /* Convert to final result type if necessary. */
3807 return convert (restype, result);
276beea2 3808}
3809\f
d7d6a3ab 3810/* Expand atomic compound assignments into an appropriate sequence as
3811 specified by the C11 standard section 6.5.16.2.
3812
b560fabd 3813 _Atomic T1 E1
3814 T2 E2
3815 E1 op= E2
3816
3817 This sequence is used for all types for which these operations are
3818 supported.
3819
3820 In addition, built-in versions of the 'fe' prefixed routines may
3821 need to be invoked for floating point (real, complex or vector) when
3822 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3823
3824 T1 newval;
3825 T1 old;
3826 T1 *addr
3827 T2 val
3828 fenv_t fenv
3829
3830 addr = &E1;
3831 val = (E2);
3832 __atomic_load (addr, &old, SEQ_CST);
3833 feholdexcept (&fenv);
3834loop:
3835 newval = old op val;
3836 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3837 SEQ_CST))
3838 goto done;
3839 feclearexcept (FE_ALL_EXCEPT);
3840 goto loop:
3841done:
3842 feupdateenv (&fenv);
3843
d7d6a3ab 3844 The compiler will issue the __atomic_fetch_* built-in when possible,
3845 otherwise it will generate the generic form of the atomic operations.
3846 This requires temp(s) and has their address taken. The atomic processing
3847 is smart enough to figure out when the size of an object can utilize
3848 a lock-free version, and convert the built-in call to the appropriate
3849 lock-free routine. The optimizers will then dispose of any temps that
3850 are no longer required, and lock-free implementations are utilized as
3851 long as there is target support for the required size.
b560fabd 3852
3853 If the operator is NOP_EXPR, then this is a simple assignment, and
3854 an __atomic_store is issued to perform the assignment rather than
d7d6a3ab 3855 the above loop. */
b560fabd 3856
3857/* Build an atomic assignment at LOC, expanding into the proper
3858 sequence to store LHS MODIFYCODE= RHS. Return a value representing
d7d6a3ab 3859 the result of the operation, unless RETURN_OLD_P, in which case
b560fabd 3860 return the old value of LHS (this is only for postincrement and
3861 postdecrement). */
d7d6a3ab 3862
b560fabd 3863static tree
3864build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3865 tree rhs, bool return_old_p)
3866{
3867 tree fndecl, func_call;
3868 vec<tree, va_gc> *params;
3869 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3870 tree old, old_addr;
3871 tree compound_stmt;
3872 tree stmt, goto_stmt;
3873 tree loop_label, loop_decl, done_label, done_decl;
3874
3875 tree lhs_type = TREE_TYPE (lhs);
b99cc6da 3876 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
b560fabd 3877 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
3878 tree rhs_type = TREE_TYPE (rhs);
3879
3880 gcc_assert (TYPE_ATOMIC (lhs_type));
3881
3882 if (return_old_p)
3883 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3884
3885 /* Allocate enough vector items for a compare_exchange. */
3886 vec_alloc (params, 6);
3887
3888 /* Create a compound statement to hold the sequence of statements
3889 with a loop. */
3890 compound_stmt = c_begin_compound_stmt (false);
3891
3892 /* Fold the RHS if it hasn't already been folded. */
3893 if (modifycode != NOP_EXPR)
3894 rhs = c_fully_fold (rhs, false, NULL);
3895
3896 /* Remove the qualifiers for the rest of the expressions and create
3897 the VAL temp variable to hold the RHS. */
3898 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3899 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
0cd6b1b4 3900 val = create_tmp_var_raw (nonatomic_rhs_type);
b560fabd 3901 TREE_ADDRESSABLE (val) = 1;
977b1853 3902 TREE_NO_WARNING (val) = 1;
0cd6b1b4 3903 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3904 NULL_TREE);
b560fabd 3905 SET_EXPR_LOCATION (rhs, loc);
3906 add_stmt (rhs);
3907
3908 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3909 an atomic_store. */
3910 if (modifycode == NOP_EXPR)
3911 {
3912 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
b99cc6da 3913 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
b560fabd 3914 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3915 params->quick_push (lhs_addr);
3916 params->quick_push (rhs);
3917 params->quick_push (seq_cst);
5a672e62 3918 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
b560fabd 3919 add_stmt (func_call);
3920
3921 /* Finish the compound statement. */
3922 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
3923
3924 /* VAL is the value which was stored, return a COMPOUND_STMT of
3925 the statement and that value. */
3926 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
3927 }
3928
d7d6a3ab 3929 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
3930 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
3931 isn't applicable for such builtins. ??? Do we want to handle enums? */
3932 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
3933 && TREE_CODE (rhs_type) == INTEGER_TYPE)
3934 {
3935 built_in_function fncode;
3936 switch (modifycode)
3937 {
3938 case PLUS_EXPR:
3939 case POINTER_PLUS_EXPR:
3940 fncode = (return_old_p
3941 ? BUILT_IN_ATOMIC_FETCH_ADD_N
3942 : BUILT_IN_ATOMIC_ADD_FETCH_N);
3943 break;
3944 case MINUS_EXPR:
3945 fncode = (return_old_p
3946 ? BUILT_IN_ATOMIC_FETCH_SUB_N
3947 : BUILT_IN_ATOMIC_SUB_FETCH_N);
3948 break;
3949 case BIT_AND_EXPR:
3950 fncode = (return_old_p
3951 ? BUILT_IN_ATOMIC_FETCH_AND_N
3952 : BUILT_IN_ATOMIC_AND_FETCH_N);
3953 break;
3954 case BIT_IOR_EXPR:
3955 fncode = (return_old_p
3956 ? BUILT_IN_ATOMIC_FETCH_OR_N
3957 : BUILT_IN_ATOMIC_OR_FETCH_N);
3958 break;
3959 case BIT_XOR_EXPR:
3960 fncode = (return_old_p
3961 ? BUILT_IN_ATOMIC_FETCH_XOR_N
3962 : BUILT_IN_ATOMIC_XOR_FETCH_N);
3963 break;
3964 default:
3965 goto cas_loop;
3966 }
3967
3968 /* We can only use "_1" through "_16" variants of the atomic fetch
3969 built-ins. */
3970 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
3971 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
3972 goto cas_loop;
3973
3974 /* If this is a pointer type, we need to multiply by the size of
3975 the pointer target type. */
3976 if (POINTER_TYPE_P (lhs_type))
3977 {
3978 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
3979 /* ??? This would introduce -Wdiscarded-qualifiers
3980 warning: __atomic_fetch_* expect volatile void *
3981 type as the first argument. (Assignments between
3982 atomic and non-atomic objects are OK.) */
3983 || TYPE_RESTRICT (lhs_type))
3984 goto cas_loop;
3985 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
3986 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
3987 convert (ptrdiff_type_node, rhs),
3988 convert (ptrdiff_type_node, sz));
3989 }
3990
3991 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
3992 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
3993 fndecl = builtin_decl_explicit (fncode);
3994 params->quick_push (lhs_addr);
3995 params->quick_push (rhs);
3996 params->quick_push (seq_cst);
3997 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
3998
3999 newval = create_tmp_var_raw (nonatomic_lhs_type);
4000 TREE_ADDRESSABLE (newval) = 1;
4001 TREE_NO_WARNING (newval) = 1;
4002 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4003 NULL_TREE, NULL_TREE);
4004 SET_EXPR_LOCATION (rhs, loc);
4005 add_stmt (rhs);
4006
4007 /* Finish the compound statement. */
4008 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4009
4010 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4011 the statement and that value. */
4012 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4013 }
4014
4015cas_loop:
b560fabd 4016 /* Create the variables and labels required for the op= form. */
0cd6b1b4 4017 old = create_tmp_var_raw (nonatomic_lhs_type);
b99cc6da 4018 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
977b1853 4019 TREE_ADDRESSABLE (old) = 1;
4020 TREE_NO_WARNING (old) = 1;
b560fabd 4021
0cd6b1b4 4022 newval = create_tmp_var_raw (nonatomic_lhs_type);
b99cc6da 4023 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
b560fabd 4024 TREE_ADDRESSABLE (newval) = 1;
75e722a6 4025 TREE_NO_WARNING (newval) = 1;
b560fabd 4026
4027 loop_decl = create_artificial_label (loc);
4028 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4029
4030 done_decl = create_artificial_label (loc);
4031 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4032
4033 /* __atomic_load (addr, &old, SEQ_CST). */
4034 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4035 params->quick_push (lhs_addr);
4036 params->quick_push (old_addr);
4037 params->quick_push (seq_cst);
5a672e62 4038 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
0cd6b1b4 4039 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4040 NULL_TREE);
4041 add_stmt (old);
b560fabd 4042 params->truncate (0);
4043
4044 /* Create the expressions for floating-point environment
4045 manipulation, if required. */
4046 bool need_fenv = (flag_trapping_math
4047 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4048 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4049 if (need_fenv)
4050 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4051
4052 if (hold_call)
4053 add_stmt (hold_call);
4054
4055 /* loop: */
4056 add_stmt (loop_label);
4057
4058 /* newval = old + val; */
4059 rhs = build_binary_op (loc, modifycode, old, val, 1);
0cd6b1b4 4060 rhs = c_fully_fold (rhs, false, NULL);
22a75734 4061 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4062 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
b560fabd 4063 NULL_TREE, 0);
4064 if (rhs != error_mark_node)
4065 {
0cd6b1b4 4066 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4067 NULL_TREE);
b560fabd 4068 SET_EXPR_LOCATION (rhs, loc);
4069 add_stmt (rhs);
4070 }
4071
4072 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4073 goto done; */
4074 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4075 params->quick_push (lhs_addr);
4076 params->quick_push (old_addr);
4077 params->quick_push (newval_addr);
4078 params->quick_push (integer_zero_node);
4079 params->quick_push (seq_cst);
4080 params->quick_push (seq_cst);
5a672e62 4081 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
b560fabd 4082
4083 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4084 SET_EXPR_LOCATION (goto_stmt, loc);
4085
4086 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4087 SET_EXPR_LOCATION (stmt, loc);
4088 add_stmt (stmt);
0cd6b1b4 4089
b560fabd 4090 if (clear_call)
4091 add_stmt (clear_call);
4092
4093 /* goto loop; */
4094 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4095 SET_EXPR_LOCATION (goto_stmt, loc);
4096 add_stmt (goto_stmt);
0cd6b1b4 4097
b560fabd 4098 /* done: */
4099 add_stmt (done_label);
4100
4101 if (update_call)
4102 add_stmt (update_call);
4103
4104 /* Finish the compound statement. */
4105 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4106
4107 /* NEWVAL is the value that was successfully stored, return a
4108 COMPOUND_EXPR of the statement and the appropriate value. */
4109 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4110 return_old_p ? old : newval);
4111}
4112
276beea2 4113/* Construct and perhaps optimize a tree representation
4114 for a unary operation. CODE, a tree_code, specifies the operation
4115 and XARG is the operand.
b99cc6da 4116 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4117 promotions (such as from short to int).
4118 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4119 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4120 to pointers in C99.
b6889cb0 4121
4122 LOCATION is the location of the operator. */
628fa4f8 4123
276beea2 4124tree
89bcb5a3 4125build_unary_op (location_t location, enum tree_code code, tree xarg,
b99cc6da 4126 bool noconvert)
276beea2 4127{
4128 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4129 tree arg = xarg;
4130 tree argtype = 0;
f831c98f 4131 enum tree_code typecode;
276beea2 4132 tree val;
b6889cb0 4133 tree ret = error_mark_node;
c6418a4e 4134 tree eptype = NULL_TREE;
7a979707 4135 const char *invalid_op_diag;
a75b1c71 4136 bool int_operands;
4137
4138 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
9a8bed72 4139 if (int_operands)
4140 arg = remove_c_maybe_const_expr (arg);
628fa4f8 4141
f831c98f 4142 if (code != ADDR_EXPR)
22a3f7bd 4143 arg = require_complete_type (location, arg);
f831c98f 4144
4145 typecode = TREE_CODE (TREE_TYPE (arg));
276beea2 4146 if (typecode == ERROR_MARK)
4147 return error_mark_node;
4148 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4149 typecode = INTEGER_TYPE;
bd08a4e6 4150
7a979707 4151 if ((invalid_op_diag
4152 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4153 {
b6889cb0 4154 error_at (location, invalid_op_diag);
7a979707 4155 return error_mark_node;
4156 }
4157
c6418a4e 4158 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4159 {
4160 eptype = TREE_TYPE (arg);
4161 arg = TREE_OPERAND (arg, 0);
4162 }
4163
276beea2 4164 switch (code)
4165 {
4166 case CONVERT_EXPR:
4167 /* This is used for unary plus, because a CONVERT_EXPR
4168 is enough to prevent anybody from looking inside for
4169 associativity, but won't generate any code. */
4170 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
9421ebb9 4171 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
71c777ee 4172 || typecode == VECTOR_TYPE))
628fa4f8 4173 {
b6889cb0 4174 error_at (location, "wrong type argument to unary plus");
276beea2 4175 return error_mark_node;
628fa4f8 4176 }
276beea2 4177 else if (!noconvert)
4178 arg = default_conversion (arg);
389dd41b 4179 arg = non_lvalue_loc (location, arg);
628fa4f8 4180 break;
4181
276beea2 4182 case NEGATE_EXPR:
4183 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
9421ebb9 4184 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
276beea2 4185 || typecode == VECTOR_TYPE))
4186 {
b6889cb0 4187 error_at (location, "wrong type argument to unary minus");
276beea2 4188 return error_mark_node;
4189 }
4190 else if (!noconvert)
4191 arg = default_conversion (arg);
628fa4f8 4192 break;
4193
276beea2 4194 case BIT_NOT_EXPR:
0eb14c3d 4195 /* ~ works on integer types and non float vectors. */
4196 if (typecode == INTEGER_TYPE
4197 || (typecode == VECTOR_TYPE
4198 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
d2c0af2b 4199 {
481ce481 4200 tree e = arg;
4201
4202 /* Warn if the expression has boolean value. */
4203 while (TREE_CODE (e) == COMPOUND_EXPR)
4204 e = TREE_OPERAND (e, 1);
4205
4206 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
4207 || truth_value_p (TREE_CODE (e)))
4208 && warning_at (location, OPT_Wbool_operation,
4209 "%<~%> on a boolean expression"))
4210 {
4211 gcc_rich_location richloc (location);
4212 richloc.add_fixit_insert_before (location, "!");
4213 inform_at_rich_loc (&richloc, "did you mean to use logical "
4214 "not?");
4215 }
276beea2 4216 if (!noconvert)
4217 arg = default_conversion (arg);
d2c0af2b 4218 }
276beea2 4219 else if (typecode == COMPLEX_TYPE)
628fa4f8 4220 {
276beea2 4221 code = CONJ_EXPR;
29438999 4222 pedwarn (location, OPT_Wpedantic,
8864917d 4223 "ISO C does not support %<~%> for complex conjugation");
276beea2 4224 if (!noconvert)
4225 arg = default_conversion (arg);
4226 }
4227 else
4228 {
b6889cb0 4229 error_at (location, "wrong type argument to bit-complement");
276beea2 4230 return error_mark_node;
628fa4f8 4231 }
4232 break;
4233
276beea2 4234 case ABS_EXPR:
71d5a758 4235 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
628fa4f8 4236 {
b6889cb0 4237 error_at (location, "wrong type argument to abs");
276beea2 4238 return error_mark_node;
628fa4f8 4239 }
276beea2 4240 else if (!noconvert)
4241 arg = default_conversion (arg);
628fa4f8 4242 break;
4243
276beea2 4244 case CONJ_EXPR:
4245 /* Conjugating a real value is a no-op, but allow it anyway. */
4246 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4247 || typecode == COMPLEX_TYPE))
628fa4f8 4248 {
b6889cb0 4249 error_at (location, "wrong type argument to conjugation");
276beea2 4250 return error_mark_node;
628fa4f8 4251 }
276beea2 4252 else if (!noconvert)
4253 arg = default_conversion (arg);
628fa4f8 4254 break;
4255
276beea2 4256 case TRUTH_NOT_EXPR:
9421ebb9 4257 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
276beea2 4258 && typecode != REAL_TYPE && typecode != POINTER_TYPE
f14c8207 4259 && typecode != COMPLEX_TYPE)
628fa4f8 4260 {
b6889cb0 4261 error_at (location,
4262 "wrong type argument to unary exclamation mark");
276beea2 4263 return error_mark_node;
628fa4f8 4264 }
bb554051 4265 if (int_operands)
4266 {
4267 arg = c_objc_common_truthvalue_conversion (location, xarg);
4268 arg = remove_c_maybe_const_expr (arg);
4269 }
4270 else
4271 arg = c_objc_common_truthvalue_conversion (location, arg);
389dd41b 4272 ret = invert_truthvalue_loc (location, arg);
43158006 4273 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4274 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4275 location = EXPR_LOCATION (ret);
b6889cb0 4276 goto return_build_unary_op;
276beea2 4277
276beea2 4278 case REALPART_EXPR:
276beea2 4279 case IMAGPART_EXPR:
0b5fc5d6 4280 ret = build_real_imag_expr (location, code, arg);
4281 if (ret == error_mark_node)
4282 return error_mark_node;
c6418a4e 4283 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4284 eptype = TREE_TYPE (eptype);
b6889cb0 4285 goto return_build_unary_op;
276beea2 4286
4287 case PREINCREMENT_EXPR:
4288 case POSTINCREMENT_EXPR:
4289 case PREDECREMENT_EXPR:
4290 case POSTDECREMENT_EXPR:
276beea2 4291
a75b1c71 4292 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4293 {
4294 tree inner = build_unary_op (location, code,
89bcb5a3 4295 C_MAYBE_CONST_EXPR_EXPR (arg),
4296 noconvert);
a75b1c71 4297 if (inner == error_mark_node)
4298 return error_mark_node;
4299 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4300 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4301 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4302 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4303 goto return_build_unary_op;
4304 }
4305
e4a7640a 4306 /* Complain about anything that is not a true lvalue. In
4307 Objective-C, skip this check for property_refs. */
6cf89e04 4308 if (!objc_is_property_ref (arg)
fdd84b77 4309 && !lvalue_or_else (location,
4310 arg, ((code == PREINCREMENT_EXPR
e4a7640a 4311 || code == POSTINCREMENT_EXPR)
4312 ? lv_increment
4313 : lv_decrement)))
a75b1c71 4314 return error_mark_node;
4315
9f1b7d17 4316 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4317 {
4318 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4319 warning_at (location, OPT_Wc___compat,
4320 "increment of enumeration value is invalid in C++");
4321 else
4322 warning_at (location, OPT_Wc___compat,
4323 "decrement of enumeration value is invalid in C++");
4324 }
4325
481ce481 4326 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4327 {
4328 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4329 warning_at (location, OPT_Wbool_operation,
4330 "increment of a boolean expression");
4331 else
4332 warning_at (location, OPT_Wbool_operation,
4333 "decrement of a boolean expression");
4334 }
4335
a75b1c71 4336 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
4337 arg = c_fully_fold (arg, false, NULL);
4338
b560fabd 4339 bool atomic_op;
4340 atomic_op = really_atomic_lvalue (arg);
4341
276beea2 4342 /* Increment or decrement the real part of the value,
4343 and don't change the imaginary part. */
4344 if (typecode == COMPLEX_TYPE)
628fa4f8 4345 {
276beea2 4346 tree real, imag;
4347
29438999 4348 pedwarn (location, OPT_Wpedantic,
21ca8540 4349 "ISO C does not support %<++%> and %<--%> on complex types");
276beea2 4350
b560fabd 4351 if (!atomic_op)
4352 {
4353 arg = stabilize_reference (arg);
b99cc6da 4354 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4355 true);
4356 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4357 true);
4358 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
b560fabd 4359 if (real == error_mark_node || imag == error_mark_node)
4360 return error_mark_node;
4361 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4362 real, imag);
4363 goto return_build_unary_op;
4364 }
628fa4f8 4365 }
276beea2 4366
4367 /* Report invalid types. */
4368
9421ebb9 4369 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
b560fabd 4370 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
73fd9cbe 4371 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
628fa4f8 4372 {
276beea2 4373 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
b6889cb0 4374 error_at (location, "wrong type argument to increment");
a0c938f0 4375 else
b6889cb0 4376 error_at (location, "wrong type argument to decrement");
276beea2 4377
4378 return error_mark_node;
628fa4f8 4379 }
628fa4f8 4380
276beea2 4381 {
4382 tree inc;
628fa4f8 4383
276beea2 4384 argtype = TREE_TYPE (arg);
4385
4386 /* Compute the increment. */
4387
4388 if (typecode == POINTER_TYPE)
4389 {
d44f2f7c 4390 /* If pointer target is an incomplete type,
276beea2 4391 we just cannot know how to do the arithmetic. */
20a44a69 4392 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
276beea2 4393 {
4394 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
b6889cb0 4395 error_at (location,
d44f2f7c 4396 "increment of pointer to an incomplete type %qT",
4397 TREE_TYPE (argtype));
276beea2 4398 else
b6889cb0 4399 error_at (location,
d44f2f7c 4400 "decrement of pointer to an incomplete type %qT",
4401 TREE_TYPE (argtype));
276beea2 4402 }
20a44a69 4403 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4404 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
a0c938f0 4405 {
276beea2 4406 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
9205a6cc 4407 pedwarn (location, OPT_Wpointer_arith,
8864917d 4408 "wrong type argument to increment");
276beea2 4409 else
9205a6cc 4410 pedwarn (location, OPT_Wpointer_arith,
8864917d 4411 "wrong type argument to decrement");
276beea2 4412 }
4413
20a44a69 4414 inc = c_size_in_bytes (TREE_TYPE (argtype));
a0553bff 4415 inc = convert_to_ptrofftype_loc (location, inc);
276beea2 4416 }
20a44a69 4417 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
9421ebb9 4418 {
4419 /* For signed fract types, we invert ++ to -- or
4420 -- to ++, and change inc from 1 to -1, because
4421 it is not possible to represent 1 in signed fract constants.
4422 For unsigned fract types, the result always overflows and
4423 we get an undefined (original) or the maximum value. */
4424 if (code == PREINCREMENT_EXPR)
4425 code = PREDECREMENT_EXPR;
4426 else if (code == PREDECREMENT_EXPR)
4427 code = PREINCREMENT_EXPR;
4428 else if (code == POSTINCREMENT_EXPR)
4429 code = POSTDECREMENT_EXPR;
4430 else /* code == POSTDECREMENT_EXPR */
4431 code = POSTINCREMENT_EXPR;
4432
4433 inc = integer_minus_one_node;
4434 inc = convert (argtype, inc);
4435 }
276beea2 4436 else
0de36bdb 4437 {
73fd9cbe 4438 inc = VECTOR_TYPE_P (argtype)
4439 ? build_one_cst (argtype)
4440 : integer_one_node;
0de36bdb 4441 inc = convert (argtype, inc);
4442 }
276beea2 4443
e4a7640a 4444 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4445 need to ask Objective-C to build the increment or decrement
4446 expression for it. */
4447 if (objc_is_property_ref (arg))
6cf89e04 4448 return objc_build_incr_expr_for_property_ref (location, code,
e4a7640a 4449 arg, inc);
4450
276beea2 4451 /* Report a read-only lvalue. */
d4e60318 4452 if (TYPE_READONLY (argtype))
1e8e9920 4453 {
f2697631 4454 readonly_error (location, arg,
1e8e9920 4455 ((code == PREINCREMENT_EXPR
4456 || code == POSTINCREMENT_EXPR)
4457 ? lv_increment : lv_decrement));
4458 return error_mark_node;
4459 }
d4e60318 4460 else if (TREE_READONLY (arg))
4461 readonly_warning (arg,
4462 ((code == PREINCREMENT_EXPR
4463 || code == POSTINCREMENT_EXPR)
4464 ? lv_increment : lv_decrement));
276beea2 4465
b560fabd 4466 /* If the argument is atomic, use the special code sequences for
4467 atomic compound assignment. */
4468 if (atomic_op)
4469 {
4470 arg = stabilize_reference (arg);
4471 ret = build_atomic_assign (location, arg,
4472 ((code == PREINCREMENT_EXPR
4473 || code == POSTINCREMENT_EXPR)
4474 ? PLUS_EXPR
4475 : MINUS_EXPR),
4476 (FRACT_MODE_P (TYPE_MODE (argtype))
4477 ? inc
4478 : integer_one_node),
4479 (code == POSTINCREMENT_EXPR
4480 || code == POSTDECREMENT_EXPR));
4481 goto return_build_unary_op;
4482 }
4483
276beea2 4484 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4485 val = boolean_increment (code, arg);
4486 else
14ae0310 4487 val = build2 (code, TREE_TYPE (arg), arg, inc);
276beea2 4488 TREE_SIDE_EFFECTS (val) = 1;
276beea2 4489 if (TREE_CODE (val) != code)
4ee9c684 4490 TREE_NO_WARNING (val) = 1;
b6889cb0 4491 ret = val;
4492 goto return_build_unary_op;
276beea2 4493 }
4494
4495 case ADDR_EXPR:
4496 /* Note that this operation never does default_conversion. */
4497
1b157e43 4498 /* The operand of unary '&' must be an lvalue (which excludes
4499 expressions of type void), or, in C99, the result of a [] or
4500 unary '*' operator. */
4501 if (VOID_TYPE_P (TREE_TYPE (arg))
4502 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
aa3e402e 4503 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
1b157e43 4504 pedwarn (location, 0, "taking address of expression of type %<void%>");
4505
276beea2 4506 /* Let &* cancel out to simplify resulting code. */
aa3e402e 4507 if (INDIRECT_REF_P (arg))
628fa4f8 4508 {
276beea2 4509 /* Don't let this be an lvalue. */
4510 if (lvalue_p (TREE_OPERAND (arg, 0)))
389dd41b 4511 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
b6889cb0 4512 ret = TREE_OPERAND (arg, 0);
4513 goto return_build_unary_op;
628fa4f8 4514 }
a4110d9a 4515
276beea2 4516 /* Anything not already handled and not a true memory reference
4517 or a non-lvalue array is an error. */
89bcb5a3 4518 if (typecode != FUNCTION_TYPE && !noconvert
292237f3 4519 && !lvalue_or_else (location, arg, lv_addressof))
276beea2 4520 return error_mark_node;
6b854be5 4521
a75b1c71 4522 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4523 folding later. */
4524 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4525 {
4526 tree inner = build_unary_op (location, code,
89bcb5a3 4527 C_MAYBE_CONST_EXPR_EXPR (arg),
4528 noconvert);
a75b1c71 4529 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4530 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4531 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4532 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4533 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4534 goto return_build_unary_op;
4535 }
4536
276beea2 4537 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4538 argtype = TREE_TYPE (arg);
628fa4f8 4539
276beea2 4540 /* If the lvalue is const or volatile, merge that into the type
8ae93b58 4541 to which the address will point. This is only needed
51e25433 4542 for function types. */
ce45a448 4543 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
8ae93b58 4544 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4545 && TREE_CODE (argtype) == FUNCTION_TYPE)
51e25433 4546 {
4547 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4548 int quals = orig_quals;
4549
4550 if (TREE_READONLY (arg))
4551 quals |= TYPE_QUAL_CONST;
4552 if (TREE_THIS_VOLATILE (arg))
4553 quals |= TYPE_QUAL_VOLATILE;
4554
51e25433 4555 argtype = c_build_qualified_type (argtype, quals);
4556 }
628fa4f8 4557
292237f3 4558 switch (TREE_CODE (arg))
4559 {
4560 case COMPONENT_REF:
4561 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4562 {
eac243e7 4563 error_at (location, "cannot take address of bit-field %qD",
4564 TREE_OPERAND (arg, 1));
292237f3 4565 return error_mark_node;
4566 }
4567
e3533433 4568 /* fall through */
292237f3 4569
4570 case ARRAY_REF:
4571 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4572 {
4573 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4574 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4575 {
eac243e7 4576 error_at (location, "cannot take address of scalar with "
4577 "reverse storage order");
292237f3 4578 return error_mark_node;
4579 }
4580
4581 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4582 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
eac243e7 4583 warning_at (location, OPT_Wscalar_storage_order,
4584 "address of array with reverse scalar storage "
4585 "order requested");
292237f3 4586 }
4587
4588 default:
4589 break;
4590 }
4591
276beea2 4592 if (!c_mark_addressable (arg))
4593 return error_mark_node;
628fa4f8 4594
43d08288 4595 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4596 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
628fa4f8 4597
7da1265c 4598 argtype = build_pointer_type (argtype);
3ce29533 4599
4600 /* ??? Cope with user tricks that amount to offsetof. Delete this
4601 when we have proper support for integer constant expressions. */
4602 val = get_base_address (arg);
aa3e402e 4603 if (val && INDIRECT_REF_P (val)
120c6697 4604 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4605 {
7549df0d 4606 ret = fold_convert_loc (location, argtype, fold_offsetof_1 (arg));
b6889cb0 4607 goto return_build_unary_op;
120c6697 4608 }
3ce29533 4609
7da1265c 4610 val = build1 (ADDR_EXPR, argtype, arg);
628fa4f8 4611
b6889cb0 4612 ret = val;
4613 goto return_build_unary_op;
628fa4f8 4614
276beea2 4615 default:
c44afe23 4616 gcc_unreachable ();
276beea2 4617 }
628fa4f8 4618
276beea2 4619 if (argtype == 0)
4620 argtype = TREE_TYPE (arg);
a75b1c71 4621 if (TREE_CODE (arg) == INTEGER_CST)
4622 ret = (require_constant_value
389dd41b 4623 ? fold_build1_initializer_loc (location, code, argtype, arg)
4624 : fold_build1_loc (location, code, argtype, arg));
a75b1c71 4625 else
4626 ret = build1 (code, argtype, arg);
b6889cb0 4627 return_build_unary_op:
4628 gcc_assert (ret != error_mark_node);
a75b1c71 4629 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4630 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4631 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4632 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4633 ret = note_integer_operands (ret);
c6418a4e 4634 if (eptype)
4635 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
b6889cb0 4636 protected_set_expr_location (ret, location);
4637 return ret;
276beea2 4638}
628fa4f8 4639
276beea2 4640/* Return nonzero if REF is an lvalue valid for this language.
4641 Lvalues can be assigned, unless their type has TYPE_READONLY.
7746224a 4642 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
628fa4f8 4643
79973b57 4644bool
9f627b1a 4645lvalue_p (const_tree ref)
276beea2 4646{
9f627b1a 4647 const enum tree_code code = TREE_CODE (ref);
628fa4f8 4648
276beea2 4649 switch (code)
4650 {
4651 case REALPART_EXPR:
4652 case IMAGPART_EXPR:
4653 case COMPONENT_REF:
4654 return lvalue_p (TREE_OPERAND (ref, 0));
628fa4f8 4655
a75b1c71 4656 case C_MAYBE_CONST_EXPR:
4657 return lvalue_p (TREE_OPERAND (ref, 1));
4658
276beea2 4659 case COMPOUND_LITERAL_EXPR:
4660 case STRING_CST:
d80c1c6c 4661 return true;
628fa4f8 4662
276beea2 4663 case INDIRECT_REF:
4664 case ARRAY_REF:
3c6d4197 4665 case ARRAY_NOTATION_REF:
276beea2 4666 case VAR_DECL:
4667 case PARM_DECL:
4668 case RESULT_DECL:
4669 case ERROR_MARK:
4670 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4671 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
5d844ba2 4672
276beea2 4673 case BIND_EXPR:
276beea2 4674 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
5d844ba2 4675
276beea2 4676 default:
d80c1c6c 4677 return false;
276beea2 4678 }
4679}
628fa4f8 4680\f
d4e60318 4681/* Give a warning for storing in something that is read-only in GCC
4682 terms but not const in ISO C terms. */
4683
4684static void
4685readonly_warning (tree arg, enum lvalue_use use)
4686{
4687 switch (use)
4688 {
4689 case lv_assign:
4690 warning (0, "assignment of read-only location %qE", arg);
4691 break;
4692 case lv_increment:
4693 warning (0, "increment of read-only location %qE", arg);
4694 break;
4695 case lv_decrement:
4696 warning (0, "decrement of read-only location %qE", arg);
4697 break;
4698 default:
4699 gcc_unreachable ();
4700 }
4701 return;
4702}
4703
e35976b1 4704
4705/* Return nonzero if REF is an lvalue valid for this language;
4706 otherwise, print an error message and return zero. USE says
fdd84b77 4707 how the lvalue is being used and so selects the error message.
4708 LOCATION is the location at which any error should be reported. */
e35976b1 4709
4710static int
fdd84b77 4711lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
e35976b1 4712{
4713 int win = lvalue_p (ref);
4714
4715 if (!win)
fdd84b77 4716 lvalue_error (loc, use);
e35976b1 4717
4718 return win;
4719}
276beea2 4720\f
4721/* Mark EXP saying that we need to be able to take the
4722 address of it; it should not be allocated in a register.
4723 Returns true if successful. */
e4d4d9ae 4724
276beea2 4725bool
4726c_mark_addressable (tree exp)
628fa4f8 4727{
276beea2 4728 tree x = exp;
277f8686 4729
276beea2 4730 while (1)
4731 switch (TREE_CODE (x))
4732 {
4733 case COMPONENT_REF:
276beea2 4734 case ADDR_EXPR:
4735 case ARRAY_REF:
4736 case REALPART_EXPR:
4737 case IMAGPART_EXPR:
4738 x = TREE_OPERAND (x, 0);
4739 break;
277f8686 4740
276beea2 4741 case COMPOUND_LITERAL_EXPR:
4742 case CONSTRUCTOR:
4743 TREE_ADDRESSABLE (x) = 1;
4744 return true;
277f8686 4745
276beea2 4746 case VAR_DECL:
4747 case CONST_DECL:
4748 case PARM_DECL:
4749 case RESULT_DECL:
7746224a 4750 if (C_DECL_REGISTER (x)
276beea2 4751 && DECL_NONLOCAL (x))
4752 {
ce41e81a 4753 if (TREE_PUBLIC (x) || is_global_var (x))
276beea2 4754 {
e3ab5fb6 4755 error
4756 ("global register variable %qD used in nested function", x);
276beea2 4757 return false;
4758 }
21ca8540 4759 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
276beea2 4760 }
7746224a 4761 else if (C_DECL_REGISTER (x))
276beea2 4762 {
ce41e81a 4763 if (TREE_PUBLIC (x) || is_global_var (x))
e3ab5fb6 4764 error ("address of global register variable %qD requested", x);
4765 else
4766 error ("address of register variable %qD requested", x);
4767 return false;
276beea2 4768 }
628fa4f8 4769
e3533433 4770 /* FALLTHRU */
276beea2 4771 case FUNCTION_DECL:
4772 TREE_ADDRESSABLE (x) = 1;
e3533433 4773 /* FALLTHRU */
276beea2 4774 default:
4775 return true;
4776 }
4777}
4778\f
8f45ab4d 4779/* Convert EXPR to TYPE, warning about conversion problems with
4780 constants. SEMANTIC_TYPE is the type this conversion would use
4781 without excess precision. If SEMANTIC_TYPE is NULL, this function
4782 is equivalent to convert_and_check. This function is a wrapper that
4783 handles conversions that may be different than
4784 the usual ones because of excess precision. */
4785
4786static tree
22a75734 4787ep_convert_and_check (location_t loc, tree type, tree expr,
4788 tree semantic_type)
8f45ab4d 4789{
4790 if (TREE_TYPE (expr) == type)
4791 return expr;
4792
4793 if (!semantic_type)
22a75734 4794 return convert_and_check (loc, type, expr);
8f45ab4d 4795
4796 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4797 && TREE_TYPE (expr) != semantic_type)
4798 {
4799 /* For integers, we need to check the real conversion, not
4800 the conversion to the excess precision type. */
22a75734 4801 expr = convert_and_check (loc, semantic_type, expr);
8f45ab4d 4802 }
4803 /* Result type is the excess precision type, which should be
4804 large enough, so do not check. */
4805 return convert (type, expr);
4806}
4807
a75b1c71 4808/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4809 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4810 if folded to an integer constant then the unselected half may
4811 contain arbitrary operations not normally permitted in constant
e60a6f7b 4812 expressions. Set the location of the expression to LOC. */
628fa4f8 4813
4814tree
d5b637fa 4815build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
6163a125 4816 tree op1, tree op1_original_type, tree op2,
4817 tree op2_original_type)
628fa4f8 4818{
276beea2 4819 tree type1;
4820 tree type2;
4821 enum tree_code code1;
4822 enum tree_code code2;
4823 tree result_type = NULL;
8f45ab4d 4824 tree semantic_result_type = NULL;
276beea2 4825 tree orig_op1 = op1, orig_op2 = op2;
a75b1c71 4826 bool int_const, op1_int_operands, op2_int_operands, int_operands;
9a8bed72 4827 bool ifexp_int_operands;
a75b1c71 4828 tree ret;
628fa4f8 4829
9a8bed72 4830 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4831 if (op1_int_operands)
4832 op1 = remove_c_maybe_const_expr (op1);
4833 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4834 if (op2_int_operands)
4835 op2 = remove_c_maybe_const_expr (op2);
4836 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4837 if (ifexp_int_operands)
4838 ifexp = remove_c_maybe_const_expr (ifexp);
4839
276beea2 4840 /* Promote both alternatives. */
4841
4842 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4843 op1 = default_conversion (op1);
4844 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4845 op2 = default_conversion (op2);
4846
4847 if (TREE_CODE (ifexp) == ERROR_MARK
4848 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4849 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
628fa4f8 4850 return error_mark_node;
628fa4f8 4851
276beea2 4852 type1 = TREE_TYPE (op1);
4853 code1 = TREE_CODE (type1);
4854 type2 = TREE_TYPE (op2);
4855 code2 = TREE_CODE (type2);
4856
547c6b1f 4857 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4858 return error_mark_node;
4859
4860 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4861 return error_mark_node;
4862
9b73f131 4863 /* C90 does not permit non-lvalue arrays in conditional expressions.
4864 In C99 they will be pointers by now. */
4865 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4866 {
d5b637fa 4867 error_at (colon_loc, "non-lvalue array in conditional expression");
9b73f131 4868 return error_mark_node;
4869 }
4870
c6418a4e 4871 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4872 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4873 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4874 || code1 == COMPLEX_TYPE)
4875 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4876 || code2 == COMPLEX_TYPE))
4877 {
8f45ab4d 4878 semantic_result_type = c_common_type (type1, type2);
c6418a4e 4879 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
4880 {
4881 op1 = TREE_OPERAND (op1, 0);
4882 type1 = TREE_TYPE (op1);
4883 gcc_assert (TREE_CODE (type1) == code1);
4884 }
4885 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4886 {
4887 op2 = TREE_OPERAND (op2, 0);
4888 type2 = TREE_TYPE (op2);
4889 gcc_assert (TREE_CODE (type2) == code2);
4890 }
4891 }
4892
6163a125 4893 if (warn_cxx_compat)
4894 {
4895 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
4896 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
4897
4898 if (TREE_CODE (t1) == ENUMERAL_TYPE
4899 && TREE_CODE (t2) == ENUMERAL_TYPE
4900 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
4901 warning_at (colon_loc, OPT_Wc___compat,
4902 ("different enum types in conditional is "
4903 "invalid in C++: %qT vs %qT"),
4904 t1, t2);
4905 }
4906
276beea2 4907 /* Quickly detect the usual case where op1 and op2 have the same type
4908 after promotion. */
4909 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
628fa4f8 4910 {
276beea2 4911 if (type1 == type2)
4912 result_type = type1;
4913 else
4914 result_type = TYPE_MAIN_VARIANT (type1);
4915 }
4916 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
a0c938f0 4917 || code1 == COMPLEX_TYPE)
4918 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4919 || code2 == COMPLEX_TYPE))
276beea2 4920 {
79d37242 4921 result_type = c_common_type (type1, type2);
e218d07f 4922 if (result_type == error_mark_node)
4923 return error_mark_node;
c920faa3 4924 do_warn_double_promotion (result_type, type1, type2,
4925 "implicit conversion from %qT to %qT to "
4926 "match other result of conditional",
4927 colon_loc);
628fa4f8 4928
276beea2 4929 /* If -Wsign-compare, warn here if type1 and type2 have
4930 different signedness. We'll promote the signed to unsigned
4931 and later code won't know it used to be different.
4932 Do this check on the original types, so that explicit casts
4933 will be considered, but default promotions won't. */
48d94ede 4934 if (c_inhibit_evaluation_warnings == 0)
be2828ce 4935 {
78a8ed03 4936 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
4937 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
628fa4f8 4938
276beea2 4939 if (unsigned_op1 ^ unsigned_op2)
4940 {
add6ee5e 4941 bool ovf;
4942
276beea2 4943 /* Do not warn if the result type is signed, since the
4944 signed type will only be chosen if it can represent
4945 all the values of the unsigned type. */
84166705 4946 if (!TYPE_UNSIGNED (result_type))
276beea2 4947 /* OK */;
276beea2 4948 else
a75b1c71 4949 {
4950 bool op1_maybe_const = true;
4951 bool op2_maybe_const = true;
4952
4953 /* Do not warn if the signed quantity is an
4954 unsuffixed integer literal (or some static
4955 constant expression involving such literals) and
4956 it is non-negative. This warning requires the
4957 operands to be folded for best results, so do
4958 that folding in this case even without
4959 warn_sign_compare to avoid warning options
4960 possibly affecting code generation. */
672d914b 4961 c_inhibit_evaluation_warnings
4962 += (ifexp == truthvalue_false_node);
a75b1c71 4963 op1 = c_fully_fold (op1, require_constant_value,
4964 &op1_maybe_const);
672d914b 4965 c_inhibit_evaluation_warnings
4966 -= (ifexp == truthvalue_false_node);
4967
4968 c_inhibit_evaluation_warnings
4969 += (ifexp == truthvalue_true_node);
a75b1c71 4970 op2 = c_fully_fold (op2, require_constant_value,
4971 &op2_maybe_const);
672d914b 4972 c_inhibit_evaluation_warnings
4973 -= (ifexp == truthvalue_true_node);
a75b1c71 4974
4975 if (warn_sign_compare)
4976 {
4977 if ((unsigned_op2
4978 && tree_expr_nonnegative_warnv_p (op1, &ovf))
4979 || (unsigned_op1
4980 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
4981 /* OK */;
4982 else
d5b637fa 4983 warning_at (colon_loc, OPT_Wsign_compare,
4984 ("signed and unsigned type in "
4985 "conditional expression"));
a75b1c71 4986 }
4987 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
f59e3889 4988 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
a75b1c71 4989 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
f59e3889 4990 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
a75b1c71 4991 }
276beea2 4992 }
4993 }
4994 }
4995 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
4996 {
8864917d 4997 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
29438999 4998 pedwarn (colon_loc, OPT_Wpedantic,
8864917d 4999 "ISO C forbids conditional expr with only one void side");
276beea2 5000 result_type = void_type_node;
5001 }
5002 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5003 {
6d5d708e 5004 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5005 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5006 addr_space_t as_common;
5007
d5b637fa 5008 if (comp_target_types (colon_loc, type1, type2))
1ea7fa60 5009 result_type = common_pointer_type (type1, type2);
79f925ed 5010 else if (null_pointer_constant_p (orig_op1))
6d5d708e 5011 result_type = type2;
79f925ed 5012 else if (null_pointer_constant_p (orig_op2))
6d5d708e 5013 result_type = type1;
5014 else if (!addr_space_superset (as1, as2, &as_common))
5015 {
5016 error_at (colon_loc, "pointers to disjoint address spaces "
5017 "used in conditional expression");
5018 return error_mark_node;
5019 }
b560fabd 5020 else if (VOID_TYPE_P (TREE_TYPE (type1))
5021 && !TYPE_ATOMIC (TREE_TYPE (type1)))
20407c42 5022 {
8a8211df 5023 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5024 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5025 & ~TYPE_QUALS (TREE_TYPE (type1))))
5026 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5027 "pointer to array loses qualifier "
5028 "in conditional expression");
5029
8864917d 5030 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
29438999 5031 pedwarn (colon_loc, OPT_Wpedantic,
21ca8540 5032 "ISO C forbids conditional expr between "
b0b1af64 5033 "%<void *%> and function pointer");
276beea2 5034 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5035 TREE_TYPE (type2)));
20407c42 5036 }
b560fabd 5037 else if (VOID_TYPE_P (TREE_TYPE (type2))
5038 && !TYPE_ATOMIC (TREE_TYPE (type2)))
a5eb6189 5039 {
8a8211df 5040 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5041 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5042 & ~TYPE_QUALS (TREE_TYPE (type2))))
5043 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5044 "pointer to array loses qualifier "
5045 "in conditional expression");
5046
8864917d 5047 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
29438999 5048 pedwarn (colon_loc, OPT_Wpedantic,
21ca8540 5049 "ISO C forbids conditional expr between "
b0b1af64 5050 "%<void *%> and function pointer");
276beea2 5051 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5052 TREE_TYPE (type1)));
a5eb6189 5053 }
4abfc532 5054 /* Objective-C pointer comparisons are a bit more lenient. */
5055 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5056 result_type = objc_common_type (type1, type2);
20407c42 5057 else
be2828ce 5058 {
6d5d708e 5059 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5060
4abfc532 5061 pedwarn (colon_loc, 0,
5062 "pointer type mismatch in conditional expression");
6d5d708e 5063 result_type = build_pointer_type
5064 (build_qualified_type (void_type_node, qual));
be2828ce 5065 }
276beea2 5066 }
5067 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5068 {
79f925ed 5069 if (!null_pointer_constant_p (orig_op2))
d5b637fa 5070 pedwarn (colon_loc, 0,
21ca8540 5071 "pointer/integer type mismatch in conditional expression");
276beea2 5072 else
be2828ce 5073 {
276beea2 5074 op2 = null_pointer_node;
be2828ce 5075 }
276beea2 5076 result_type = type1;
5077 }
5078 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5079 {
79f925ed 5080 if (!null_pointer_constant_p (orig_op1))
d5b637fa 5081 pedwarn (colon_loc, 0,
21ca8540 5082 "pointer/integer type mismatch in conditional expression");
276beea2 5083 else
be2828ce 5084 {
276beea2 5085 op1 = null_pointer_node;
be2828ce 5086 }
276beea2 5087 result_type = type2;
5088 }
a5eb6189 5089
276beea2 5090 if (!result_type)
5091 {
5092 if (flag_cond_mismatch)
5093 result_type = void_type_node;
5094 else
628fa4f8 5095 {
e60a6f7b 5096 error_at (colon_loc, "type mismatch in conditional expression");
be2828ce 5097 return error_mark_node;
628fa4f8 5098 }
276beea2 5099 }
628fa4f8 5100
276beea2 5101 /* Merge const and volatile flags of the incoming types. */
5102 result_type
5103 = build_type_variant (result_type,
a6b4313a 5104 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5105 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
6b854be5 5106
22a75734 5107 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5108 semantic_result_type);
5109 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5110 semantic_result_type);
6b854be5 5111
a75b1c71 5112 if (ifexp_bcp && ifexp == truthvalue_true_node)
5113 {
5114 op2_int_operands = true;
5115 op1 = c_fully_fold (op1, require_constant_value, NULL);
5116 }
5117 if (ifexp_bcp && ifexp == truthvalue_false_node)
5118 {
5119 op1_int_operands = true;
5120 op2 = c_fully_fold (op2, require_constant_value, NULL);
5121 }
9a8bed72 5122 int_const = int_operands = (ifexp_int_operands
a75b1c71 5123 && op1_int_operands
5124 && op2_int_operands);
5125 if (int_operands)
5126 {
5127 int_const = ((ifexp == truthvalue_true_node
5128 && TREE_CODE (orig_op1) == INTEGER_CST
5129 && !TREE_OVERFLOW (orig_op1))
5130 || (ifexp == truthvalue_false_node
5131 && TREE_CODE (orig_op2) == INTEGER_CST
5132 && !TREE_OVERFLOW (orig_op2)));
5133 }
1f137e6d 5134
5135 /* Need to convert condition operand into a vector mask. */
5136 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5137 {
5138 tree vectype = TREE_TYPE (ifexp);
5139 tree elem_type = TREE_TYPE (vectype);
5140 tree zero = build_int_cst (elem_type, 0);
5141 tree zero_vec = build_vector_from_val (vectype, zero);
5142 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5143 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5144 }
5145
a75b1c71 5146 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
389dd41b 5147 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
a75b1c71 5148 else
5149 {
7c2df6db 5150 if (int_operands)
5151 {
ec431042 5152 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5153 nested inside of the expression. */
5154 op1 = c_fully_fold (op1, false, NULL);
5155 op2 = c_fully_fold (op2, false, NULL);
7c2df6db 5156 }
a75b1c71 5157 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5158 if (int_operands)
5159 ret = note_integer_operands (ret);
5160 }
8f45ab4d 5161 if (semantic_result_type)
5162 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
a75b1c71 5163
e60a6f7b 5164 protected_set_expr_location (ret, colon_loc);
a75b1c71 5165 return ret;
276beea2 5166}
5167\f
066f26bf 5168/* Return a compound expression that performs two expressions and
e60a6f7b 5169 returns the value of the second of them.
5170
5171 LOC is the location of the COMPOUND_EXPR. */
628fa4f8 5172
276beea2 5173tree
e60a6f7b 5174build_compound_expr (location_t loc, tree expr1, tree expr2)
276beea2 5175{
9a8bed72 5176 bool expr1_int_operands, expr2_int_operands;
c6418a4e 5177 tree eptype = NULL_TREE;
a75b1c71 5178 tree ret;
5179
a89e6c15 5180 if (flag_cilkplus
d037099f 5181 && (TREE_CODE (expr1) == CILK_SPAWN_STMT
5182 || TREE_CODE (expr2) == CILK_SPAWN_STMT))
5183 {
5184 error_at (loc,
5185 "spawned function call cannot be part of a comma expression");
5186 return error_mark_node;
5187 }
9a8bed72 5188 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5189 if (expr1_int_operands)
5190 expr1 = remove_c_maybe_const_expr (expr1);
5191 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5192 if (expr2_int_operands)
5193 expr2 = remove_c_maybe_const_expr (expr2);
5194
c6418a4e 5195 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5196 expr1 = TREE_OPERAND (expr1, 0);
5197 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5198 {
5199 eptype = TREE_TYPE (expr2);
5200 expr2 = TREE_OPERAND (expr2, 0);
5201 }
5202
84166705 5203 if (!TREE_SIDE_EFFECTS (expr1))
276beea2 5204 {
5205 /* The left-hand operand of a comma expression is like an expression
2006d7dc 5206 statement: with -Wunused, we should warn if it doesn't have
276beea2 5207 any side-effects, unless it was explicitly cast to (void). */
aaa8f689 5208 if (warn_unused_value)
778e3b56 5209 {
aaa8f689 5210 if (VOID_TYPE_P (TREE_TYPE (expr1))
72dd6141 5211 && CONVERT_EXPR_P (expr1))
778e3b56 5212 ; /* (void) a, b */
aaa8f689 5213 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5214 && TREE_CODE (expr1) == COMPOUND_EXPR
72dd6141 5215 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
778e3b56 5216 ; /* (void) a, (void) b, c */
5217 else
48e1416a 5218 warning_at (loc, OPT_Wunused_value,
e60a6f7b 5219 "left-hand operand of comma expression has no effect");
778e3b56 5220 }
276beea2 5221 }
9a9db3c1 5222 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5223 && warn_unused_value)
5224 {
5225 tree r = expr1;
5226 location_t cloc = loc;
5227 while (TREE_CODE (r) == COMPOUND_EXPR)
5228 {
5229 if (EXPR_HAS_LOCATION (r))
5230 cloc = EXPR_LOCATION (r);
5231 r = TREE_OPERAND (r, 1);
5232 }
5233 if (!TREE_SIDE_EFFECTS (r)
5234 && !VOID_TYPE_P (TREE_TYPE (r))
5235 && !CONVERT_EXPR_P (r))
5236 warning_at (cloc, OPT_Wunused_value,
5237 "right-hand operand of comma expression has no effect");
5238 }
628fa4f8 5239
276beea2 5240 /* With -Wunused, we should also warn if the left-hand operand does have
5241 side-effects, but computes a value which is not used. For example, in
5242 `foo() + bar(), baz()' the result of the `+' operator is not used,
5243 so we should issue a warning. */
5244 else if (warn_unused_value)
e60a6f7b 5245 warn_if_unused_value (expr1, loc);
628fa4f8 5246
47adf96b 5247 if (expr2 == error_mark_node)
5248 return error_mark_node;
5249
a75b1c71 5250 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5251
5252 if (flag_isoc99
9a8bed72 5253 && expr1_int_operands
5254 && expr2_int_operands)
a75b1c71 5255 ret = note_integer_operands (ret);
5256
c6418a4e 5257 if (eptype)
5258 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5259
e60a6f7b 5260 protected_set_expr_location (ret, loc);
a75b1c71 5261 return ret;
276beea2 5262}
628fa4f8 5263
402ba866 5264/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5265 which we are casting. OTYPE is the type of the expression being
80373d81 5266 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5267 of the cast. -Wcast-qual appeared on the command line. Named
5268 address space qualifiers are not handled here, because they result
5269 in different warnings. */
402ba866 5270
5271static void
80373d81 5272handle_warn_cast_qual (location_t loc, tree type, tree otype)
402ba866 5273{
5274 tree in_type = type;
5275 tree in_otype = otype;
5276 int added = 0;
5277 int discarded = 0;
5278 bool is_const;
5279
5280 /* Check that the qualifiers on IN_TYPE are a superset of the
5281 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5282 nodes is uninteresting and we stop as soon as we hit a
5283 non-POINTER_TYPE node on either type. */
5284 do
5285 {
5286 in_otype = TREE_TYPE (in_otype);
5287 in_type = TREE_TYPE (in_type);
5288
5289 /* GNU C allows cv-qualified function types. 'const' means the
5290 function is very pure, 'volatile' means it can't return. We
5291 need to warn when such qualifiers are added, not when they're
5292 taken away. */
5293 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5294 && TREE_CODE (in_type) == FUNCTION_TYPE)
6d5d708e 5295 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5296 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
402ba866 5297 else
6d5d708e 5298 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5299 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
402ba866 5300 }
5301 while (TREE_CODE (in_type) == POINTER_TYPE
5302 && TREE_CODE (in_otype) == POINTER_TYPE);
5303
5304 if (added)
80373d81 5305 warning_at (loc, OPT_Wcast_qual,
5306 "cast adds %q#v qualifier to function type", added);
402ba866 5307
5308 if (discarded)
5309 /* There are qualifiers present in IN_OTYPE that are not present
5310 in IN_TYPE. */
80373d81 5311 warning_at (loc, OPT_Wcast_qual,
f40f5e0e 5312 "cast discards %qv qualifier from pointer target type",
80373d81 5313 discarded);
402ba866 5314
5315 if (added || discarded)
5316 return;
5317
5318 /* A cast from **T to const **T is unsafe, because it can cause a
5319 const value to be changed with no additional warning. We only
5320 issue this warning if T is the same on both sides, and we only
5321 issue the warning if there are the same number of pointers on
5322 both sides, as otherwise the cast is clearly unsafe anyhow. A
5323 cast is unsafe when a qualifier is added at one level and const
5324 is not present at all outer levels.
5325
5326 To issue this warning, we check at each level whether the cast
5327 adds new qualifiers not already seen. We don't need to special
5328 case function types, as they won't have the same
5329 TYPE_MAIN_VARIANT. */
5330
5331 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5332 return;
5333 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5334 return;
5335
5336 in_type = type;
5337 in_otype = otype;
5338 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5339 do
5340 {
5341 in_type = TREE_TYPE (in_type);
5342 in_otype = TREE_TYPE (in_otype);
5343 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5344 && !is_const)
5345 {
80373d81 5346 warning_at (loc, OPT_Wcast_qual,
5347 "to be safe all intermediate pointers in cast from "
5348 "%qT to %qT must be %<const%> qualified",
5349 otype, type);
402ba866 5350 break;
5351 }
5352 if (is_const)
5353 is_const = TYPE_READONLY (in_type);
5354 }
5355 while (TREE_CODE (in_type) == POINTER_TYPE);
5356}
5357
48e1416a 5358/* Build an expression representing a cast to type TYPE of expression EXPR.
e60a6f7b 5359 LOC is the location of the cast-- typically the open paren of the cast. */
628fa4f8 5360
276beea2 5361tree
e60a6f7b 5362build_c_cast (location_t loc, tree type, tree expr)
276beea2 5363{
c6418a4e 5364 tree value;
5365
5366 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5367 expr = TREE_OPERAND (expr, 0);
5368
5369 value = expr;
628fa4f8 5370
276beea2 5371 if (type == error_mark_node || expr == error_mark_node)
5372 return error_mark_node;
628fa4f8 5373
276beea2 5374 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5375 only in <protocol> qualifications. But when constructing cast expressions,
5376 the protocols do matter and must be kept around. */
e0d844be 5377 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5378 return build1 (NOP_EXPR, type, expr);
5379
5380 type = TYPE_MAIN_VARIANT (type);
628fa4f8 5381
276beea2 5382 if (TREE_CODE (type) == ARRAY_TYPE)
5383 {
e60a6f7b 5384 error_at (loc, "cast specifies array type");
276beea2 5385 return error_mark_node;
5386 }
628fa4f8 5387
276beea2 5388 if (TREE_CODE (type) == FUNCTION_TYPE)
5389 {
e60a6f7b 5390 error_at (loc, "cast specifies function type");
276beea2 5391 return error_mark_node;
5392 }
628fa4f8 5393
f831c98f 5394 if (!VOID_TYPE_P (type))
5395 {
22a3f7bd 5396 value = require_complete_type (loc, value);
f831c98f 5397 if (value == error_mark_node)
5398 return error_mark_node;
5399 }
5400
276beea2 5401 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5402 {
ef61516b 5403 if (RECORD_OR_UNION_TYPE_P (type))
29438999 5404 pedwarn (loc, OPT_Wpedantic,
8864917d 5405 "ISO C forbids casting nonscalar to the same type");
005b8fe7 5406
5407 /* Convert to remove any qualifiers from VALUE's type. */
5408 value = convert (type, value);
276beea2 5409 }
5410 else if (TREE_CODE (type) == UNION_TYPE)
5411 {
5412 tree field;
628fa4f8 5413
1767a056 5414 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
ae4400fc 5415 if (TREE_TYPE (field) != error_mark_node
5416 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5417 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
276beea2 5418 break;
5419
5420 if (field)
628fa4f8 5421 {
276beea2 5422 tree t;
8c212779 5423 bool maybe_const = true;
276beea2 5424
29438999 5425 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
8c212779 5426 t = c_fully_fold (value, false, &maybe_const);
5427 t = build_constructor_single (type, field, t);
5428 if (!maybe_const)
5429 t = c_wrap_maybe_const (t, true);
5430 t = digest_init (loc, type, t,
b9c74b4d 5431 NULL_TREE, false, true, 0);
276beea2 5432 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5433 return t;
628fa4f8 5434 }
e60a6f7b 5435 error_at (loc, "cast to union type from type not present in union");
276beea2 5436 return error_mark_node;
5437 }
5438 else
5439 {
5440 tree otype, ovalue;
628fa4f8 5441
276beea2 5442 if (type == void_type_node)
e60a6f7b 5443 {
5444 tree t = build1 (CONVERT_EXPR, type, value);
5445 SET_EXPR_LOCATION (t, loc);
5446 return t;
5447 }
628fa4f8 5448
276beea2 5449 otype = TREE_TYPE (value);
628fa4f8 5450
276beea2 5451 /* Optionally warn about potentially worrisome casts. */
276beea2 5452 if (warn_cast_qual
5453 && TREE_CODE (type) == POINTER_TYPE
5454 && TREE_CODE (otype) == POINTER_TYPE)
80373d81 5455 handle_warn_cast_qual (loc, type, otype);
628fa4f8 5456
6d5d708e 5457 /* Warn about conversions between pointers to disjoint
5458 address spaces. */
5459 if (TREE_CODE (type) == POINTER_TYPE
5460 && TREE_CODE (otype) == POINTER_TYPE
5461 && !null_pointer_constant_p (value))
5462 {
5463 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5464 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5465 addr_space_t as_common;
5466
5467 if (!addr_space_superset (as_to, as_from, &as_common))
5468 {
5469 if (ADDR_SPACE_GENERIC_P (as_from))
5470 warning_at (loc, 0, "cast to %s address space pointer "
5471 "from disjoint generic address space pointer",
5472 c_addr_space_name (as_to));
5473
5474 else if (ADDR_SPACE_GENERIC_P (as_to))
5475 warning_at (loc, 0, "cast to generic address space pointer "
5476 "from disjoint %s address space pointer",
5477 c_addr_space_name (as_from));
5478
5479 else
5480 warning_at (loc, 0, "cast to %s address space pointer "
5481 "from disjoint %s address space pointer",
5482 c_addr_space_name (as_to),
5483 c_addr_space_name (as_from));
5484 }
5485 }
5486
276beea2 5487 /* Warn about possible alignment problems. */
6bf97f82 5488 if (STRICT_ALIGNMENT
276beea2 5489 && TREE_CODE (type) == POINTER_TYPE
5490 && TREE_CODE (otype) == POINTER_TYPE
5491 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5492 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5493 /* Don't warn about opaque types, where the actual alignment
5494 restriction is unknown. */
ef61516b 5495 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
276beea2 5496 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
5497 && TYPE_ALIGN (TREE_TYPE (type)) > TYPE_ALIGN (TREE_TYPE (otype)))
e60a6f7b 5498 warning_at (loc, OPT_Wcast_align,
5499 "cast increases required alignment of target type");
0dbd1c74 5500
6bf97f82 5501 if (TREE_CODE (type) == INTEGER_TYPE
276beea2 5502 && TREE_CODE (otype) == POINTER_TYPE
b8656032 5503 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
a0c938f0 5504 /* Unlike conversion of integers to pointers, where the
5505 warning is disabled for converting constants because
5506 of cases such as SIG_*, warn about converting constant
5507 pointers to integers. In some cases it may cause unwanted
b8656032 5508 sign extension, and a warning is appropriate. */
e60a6f7b 5509 warning_at (loc, OPT_Wpointer_to_int_cast,
5510 "cast from pointer to integer of different size");
628fa4f8 5511
6bf97f82 5512 if (TREE_CODE (value) == CALL_EXPR
276beea2 5513 && TREE_CODE (type) != TREE_CODE (otype))
e60a6f7b 5514 warning_at (loc, OPT_Wbad_function_cast,
5515 "cast from function call of type %qT "
5516 "to non-matching type %qT", otype, type);
628fa4f8 5517
6bf97f82 5518 if (TREE_CODE (type) == POINTER_TYPE
276beea2 5519 && TREE_CODE (otype) == INTEGER_TYPE
5520 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5521 /* Don't warn about converting any constant. */
5522 && !TREE_CONSTANT (value))
e60a6f7b 5523 warning_at (loc,
5524 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5525 "of different size");
628fa4f8 5526
e6fa0ea6 5527 if (warn_strict_aliasing <= 2)
5528 strict_aliasing_warning (otype, type, expr);
628fa4f8 5529
48b3d385 5530 /* If pedantic, warn for conversions between function and object
5531 pointer types, except for converting a null pointer constant
5532 to function pointer type. */
5533 if (pedantic
5534 && TREE_CODE (type) == POINTER_TYPE
5535 && TREE_CODE (otype) == POINTER_TYPE
5536 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5537 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
29438999 5538 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8864917d 5539 "conversion of function pointer to object pointer type");
48b3d385 5540
5541 if (pedantic
5542 && TREE_CODE (type) == POINTER_TYPE
5543 && TREE_CODE (otype) == POINTER_TYPE
5544 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5545 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
79f925ed 5546 && !null_pointer_constant_p (value))
29438999 5547 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
8864917d 5548 "conversion of object pointer to function pointer type");
48b3d385 5549
276beea2 5550 ovalue = value;
276beea2 5551 value = convert (type, value);
628fa4f8 5552
276beea2 5553 /* Ignore any integer overflow caused by the cast. */
a75b1c71 5554 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
276beea2 5555 {
ca24357c 5556 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
b468918a 5557 {
ca24357c 5558 if (!TREE_OVERFLOW (value))
5559 {
5560 /* Avoid clobbering a shared constant. */
5561 value = copy_node (value);
5562 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5563 }
b468918a 5564 }
ca24357c 5565 else if (TREE_OVERFLOW (value))
84086eb0 5566 /* Reset VALUE's overflow flags, ensuring constant sharing. */
3a54beaf 5567 value = wide_int_to_tree (TREE_TYPE (value), value);
276beea2 5568 }
5569 }
628fa4f8 5570
a7ea5e81 5571 /* Don't let a cast be an lvalue. */
b44134dc 5572 if (lvalue_p (value))
389dd41b 5573 value = non_lvalue_loc (loc, value);
0dbd1c74 5574
a75b1c71 5575 /* Don't allow the results of casting to floating-point or complex
5576 types be confused with actual constants, or casts involving
5577 integer and pointer types other than direct integer-to-integer
5578 and integer-to-pointer be confused with integer constant
5579 expressions and null pointer constants. */
5580 if (TREE_CODE (value) == REAL_CST
5581 || TREE_CODE (value) == COMPLEX_CST
5582 || (TREE_CODE (value) == INTEGER_CST
5583 && !((TREE_CODE (expr) == INTEGER_CST
5584 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5585 || TREE_CODE (expr) == REAL_CST
5586 || TREE_CODE (expr) == COMPLEX_CST)))
5587 value = build1 (NOP_EXPR, type, value);
5588
789fbb51 5589 protected_set_expr_location (value, loc);
276beea2 5590 return value;
628fa4f8 5591}
5592
e60a6f7b 5593/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5594 location of the open paren of the cast, or the position of the cast
5595 expr. */
276beea2 5596tree
e60a6f7b 5597c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
628fa4f8 5598{
ac206aff 5599 tree type;
a75b1c71 5600 tree type_expr = NULL_TREE;
5601 bool type_expr_const = true;
5602 tree ret;
276beea2 5603 int saved_wsp = warn_strict_prototypes;
7014838c 5604
276beea2 5605 /* This avoids warnings about unprototyped casts on
5606 integers. E.g. "#define SIG_DFL (void(*)())0". */
5607 if (TREE_CODE (expr) == INTEGER_CST)
5608 warn_strict_prototypes = 0;
a75b1c71 5609 type = groktypename (type_name, &type_expr, &type_expr_const);
276beea2 5610 warn_strict_prototypes = saved_wsp;
7014838c 5611
547c6b1f 5612 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5613 && reject_gcc_builtin (expr))
5614 return error_mark_node;
5615
e60a6f7b 5616 ret = build_c_cast (loc, type, expr);
a75b1c71 5617 if (type_expr)
5618 {
c4b1a13a 5619 bool inner_expr_const = true;
5620 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
a75b1c71 5621 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
c4b1a13a 5622 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5623 && inner_expr_const);
e60a6f7b 5624 SET_EXPR_LOCATION (ret, loc);
a75b1c71 5625 }
0b09525f 5626
789fbb51 5627 if (!EXPR_HAS_LOCATION (ret))
5628 protected_set_expr_location (ret, loc);
0b09525f 5629
ae5ead32 5630 /* C++ does not permits types to be defined in a cast, but it
5631 allows references to incomplete types. */
5632 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
0b09525f 5633 warning_at (loc, OPT_Wc___compat,
5634 "defining a type in a cast is invalid in C++");
5635
a75b1c71 5636 return ret;
628fa4f8 5637}
276beea2 5638\f
5639/* Build an assignment expression of lvalue LHS from value RHS.
8458f4ca 5640 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5641 may differ from TREE_TYPE (LHS) for an enum bitfield.
276beea2 5642 MODIFYCODE is the code for a binary operator that we use
5643 to combine the old value of LHS with RHS to get the new value.
b6889cb0 5644 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
b9c74b4d 5645 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5646 which may differ from TREE_TYPE (RHS) for an enum value.
b6889cb0 5647
e60a6f7b 5648 LOCATION is the location of the MODIFYCODE operator.
5649 RHS_LOC is the location of the RHS. */
2b30d46c 5650
276beea2 5651tree
8458f4ca 5652build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
48e1416a 5653 enum tree_code modifycode,
e60a6f7b 5654 location_t rhs_loc, tree rhs, tree rhs_origtype)
628fa4f8 5655{
276beea2 5656 tree result;
5657 tree newrhs;
1248c663 5658 tree rhseval = NULL_TREE;
c6418a4e 5659 tree rhs_semantic_type = NULL_TREE;
276beea2 5660 tree lhstype = TREE_TYPE (lhs);
5661 tree olhstype = lhstype;
a75b1c71 5662 bool npc;
b560fabd 5663 bool is_atomic_op;
0dbd1c74 5664
276beea2 5665 /* Types that aren't fully specified cannot be used in assignments. */
22a3f7bd 5666 lhs = require_complete_type (location, lhs);
0dbd1c74 5667
276beea2 5668 /* Avoid duplicate error messages from operands that had errors. */
5669 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5670 return error_mark_node;
628fa4f8 5671
39f74d66 5672 /* Ensure an error for assigning a non-lvalue array to an array in
5673 C90. */
5674 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5675 {
5676 error_at (location, "assignment to expression with array type");
5677 return error_mark_node;
5678 }
5679
9d9f5bb3 5680 /* For ObjC properties, defer this check. */
fdd84b77 5681 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
ab774101 5682 return error_mark_node;
5683
b560fabd 5684 is_atomic_op = really_atomic_lvalue (lhs);
5685
c6418a4e 5686 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5687 {
5688 rhs_semantic_type = TREE_TYPE (rhs);
5689 rhs = TREE_OPERAND (rhs, 0);
5690 }
5691
276beea2 5692 newrhs = rhs;
628fa4f8 5693
a75b1c71 5694 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5695 {
5696 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
e60a6f7b 5697 lhs_origtype, modifycode, rhs_loc, rhs,
8458f4ca 5698 rhs_origtype);
a75b1c71 5699 if (inner == error_mark_node)
5700 return error_mark_node;
5701 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5702 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5703 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5704 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5705 protected_set_expr_location (result, location);
5706 return result;
5707 }
5708
276beea2 5709 /* If a binary op has been requested, combine the old LHS value with the RHS
5710 producing the value we should actually store into the LHS. */
5711
5712 if (modifycode != NOP_EXPR)
628fa4f8 5713 {
a75b1c71 5714 lhs = c_fully_fold (lhs, false, NULL);
276beea2 5715 lhs = stabilize_reference (lhs);
b9c74b4d 5716
b560fabd 5717 /* Construct the RHS for any non-atomic compound assignemnt. */
5718 if (!is_atomic_op)
5719 {
1248c663 5720 /* If in LHS op= RHS the RHS has side-effects, ensure they
5721 are preevaluated before the rest of the assignment expression's
5722 side-effects, because RHS could contain e.g. function calls
5723 that modify LHS. */
5724 if (TREE_SIDE_EFFECTS (rhs))
5725 {
5726 newrhs = in_late_binary_op ? save_expr (rhs) : c_save_expr (rhs);
5727 rhseval = newrhs;
5728 }
b560fabd 5729 newrhs = build_binary_op (location,
1248c663 5730 modifycode, lhs, newrhs, 1);
b560fabd 5731
5732 /* The original type of the right hand side is no longer
5733 meaningful. */
5734 rhs_origtype = NULL_TREE;
5735 }
628fa4f8 5736 }
628fa4f8 5737
86c110ac 5738 if (c_dialect_objc ())
5739 {
9d9f5bb3 5740 /* Check if we are modifying an Objective-C property reference;
5741 if so, we need to generate setter calls. */
5742 result = objc_maybe_build_modify_expr (lhs, newrhs);
86c110ac 5743 if (result)
1248c663 5744 goto return_result;
9d9f5bb3 5745
5746 /* Else, do the check that we postponed for Objective-C. */
fdd84b77 5747 if (!lvalue_or_else (location, lhs, lv_assign))
86c110ac 5748 return error_mark_node;
5749 }
5750
056a8ee6 5751 /* Give an error for storing in something that is 'const'. */
874d597d 5752
d4e60318 5753 if (TYPE_READONLY (lhstype)
ef61516b 5754 || (RECORD_OR_UNION_TYPE_P (lhstype)
276beea2 5755 && C_TYPE_FIELDS_READONLY (lhstype)))
1e8e9920 5756 {
f2697631 5757 readonly_error (location, lhs, lv_assign);
1e8e9920 5758 return error_mark_node;
5759 }
d4e60318 5760 else if (TREE_READONLY (lhs))
5761 readonly_warning (lhs, lv_assign);
874d597d 5762
276beea2 5763 /* If storing into a structure or union member,
5764 it has probably been given type `int'.
5765 Compute the type that would go with
5766 the actual amount of storage the member occupies. */
874d597d 5767
276beea2 5768 if (TREE_CODE (lhs) == COMPONENT_REF
5769 && (TREE_CODE (lhstype) == INTEGER_TYPE
5770 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5771 || TREE_CODE (lhstype) == REAL_TYPE
5772 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5773 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
628fa4f8 5774
276beea2 5775 /* If storing in a field that is in actuality a short or narrower than one,
5776 we must store in the field in its actual type. */
5777
5778 if (lhstype != TREE_TYPE (lhs))
5779 {
5780 lhs = copy_node (lhs);
5781 TREE_TYPE (lhs) = lhstype;
628fa4f8 5782 }
628fa4f8 5783
8458f4ca 5784 /* Issue -Wc++-compat warnings about an assignment to an enum type
5785 when LHS does not have its original type. This happens for,
5786 e.g., an enum bitfield in a struct. */
5787 if (warn_cxx_compat
5788 && lhs_origtype != NULL_TREE
5789 && lhs_origtype != lhstype
5790 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
5791 {
5792 tree checktype = (rhs_origtype != NULL_TREE
5793 ? rhs_origtype
5794 : TREE_TYPE (rhs));
5795 if (checktype != error_mark_node
b560fabd 5796 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
5797 || (is_atomic_op && modifycode != NOP_EXPR)))
8458f4ca 5798 warning_at (location, OPT_Wc___compat,
5799 "enum conversion in assignment is invalid in C++");
5800 }
5801
b560fabd 5802 /* If the lhs is atomic, remove that qualifier. */
5803 if (is_atomic_op)
5804 {
5805 lhstype = build_qualified_type (lhstype,
5806 (TYPE_QUALS (lhstype)
5807 & ~TYPE_QUAL_ATOMIC));
5808 olhstype = build_qualified_type (olhstype,
5809 (TYPE_QUALS (lhstype)
5810 & ~TYPE_QUAL_ATOMIC));
5811 }
5812
c6418a4e 5813 /* Convert new value to destination type. Fold it first, then
5814 restore any excess precision information, for the sake of
5815 conversion warnings. */
628fa4f8 5816
b560fabd 5817 if (!(is_atomic_op && modifycode != NOP_EXPR))
5818 {
5819 npc = null_pointer_constant_p (newrhs);
5820 newrhs = c_fully_fold (newrhs, false, NULL);
5821 if (rhs_semantic_type)
5822 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
22a75734 5823 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
5824 rhs_origtype, ic_assign, npc,
5825 NULL_TREE, NULL_TREE, 0);
b560fabd 5826 if (TREE_CODE (newrhs) == ERROR_MARK)
5827 return error_mark_node;
5828 }
628fa4f8 5829
ddb5d39d 5830 /* Emit ObjC write barrier, if necessary. */
5831 if (c_dialect_objc () && flag_objc_gc)
5832 {
5833 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
5834 if (result)
b6889cb0 5835 {
5836 protected_set_expr_location (result, location);
1248c663 5837 goto return_result;
b6889cb0 5838 }
ddb5d39d 5839 }
5840
778ac06a 5841 /* Scan operands. */
628fa4f8 5842
b560fabd 5843 if (is_atomic_op)
5844 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
5845 else
5846 {
5847 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
5848 TREE_SIDE_EFFECTS (result) = 1;
5849 protected_set_expr_location (result, location);
5850 }
628fa4f8 5851
276beea2 5852 /* If we got the LHS in a different type for storing in,
5853 convert the result back to the nominal type of LHS
5854 so that the value we return always has the same type
5855 as the LHS argument. */
5ada9644 5856
276beea2 5857 if (olhstype == TREE_TYPE (result))
1248c663 5858 goto return_result;
b6889cb0 5859
22a75734 5860 result = convert_for_assignment (location, rhs_loc, olhstype, result,
5861 rhs_origtype, ic_assign, false, NULL_TREE,
5862 NULL_TREE, 0);
b6889cb0 5863 protected_set_expr_location (result, location);
1248c663 5864
5865return_result:
5866 if (rhseval)
5867 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
b6889cb0 5868 return result;
276beea2 5869}
5870\f
2fdec027 5871/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
5872 This is used to implement -fplan9-extensions. */
5873
5874static bool
5875find_anonymous_field_with_type (tree struct_type, tree type)
5876{
5877 tree field;
5878 bool found;
5879
ef61516b 5880 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
2fdec027 5881 found = false;
5882 for (field = TYPE_FIELDS (struct_type);
5883 field != NULL_TREE;
5884 field = TREE_CHAIN (field))
5885 {
b560fabd 5886 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5887 ? c_build_qualified_type (TREE_TYPE (field),
5888 TYPE_QUAL_ATOMIC)
5889 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
2fdec027 5890 if (DECL_NAME (field) == NULL
b560fabd 5891 && comptypes (type, fieldtype))
2fdec027 5892 {
5893 if (found)
5894 return false;
5895 found = true;
5896 }
5897 else if (DECL_NAME (field) == NULL
ef61516b 5898 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
2fdec027 5899 && find_anonymous_field_with_type (TREE_TYPE (field), type))
5900 {
5901 if (found)
5902 return false;
5903 found = true;
5904 }
5905 }
5906 return found;
5907}
5908
5909/* RHS is an expression whose type is pointer to struct. If there is
5910 an anonymous field in RHS with type TYPE, then return a pointer to
5911 that field in RHS. This is used with -fplan9-extensions. This
5912 returns NULL if no conversion could be found. */
5913
5914static tree
5915convert_to_anonymous_field (location_t location, tree type, tree rhs)
5916{
5917 tree rhs_struct_type, lhs_main_type;
5918 tree field, found_field;
5919 bool found_sub_field;
5920 tree ret;
5921
5922 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
5923 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
ef61516b 5924 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
2fdec027 5925
5926 gcc_assert (POINTER_TYPE_P (type));
b560fabd 5927 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
5928 ? c_build_qualified_type (TREE_TYPE (type),
5929 TYPE_QUAL_ATOMIC)
5930 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
2fdec027 5931
5932 found_field = NULL_TREE;
5933 found_sub_field = false;
5934 for (field = TYPE_FIELDS (rhs_struct_type);
5935 field != NULL_TREE;
5936 field = TREE_CHAIN (field))
5937 {
5938 if (DECL_NAME (field) != NULL_TREE
ef61516b 5939 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2fdec027 5940 continue;
b560fabd 5941 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
5942 ? c_build_qualified_type (TREE_TYPE (field),
5943 TYPE_QUAL_ATOMIC)
5944 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
5945 if (comptypes (lhs_main_type, fieldtype))
2fdec027 5946 {
5947 if (found_field != NULL_TREE)
5948 return NULL_TREE;
5949 found_field = field;
5950 }
5951 else if (find_anonymous_field_with_type (TREE_TYPE (field),
5952 lhs_main_type))
5953 {
5954 if (found_field != NULL_TREE)
5955 return NULL_TREE;
5956 found_field = field;
5957 found_sub_field = true;
5958 }
5959 }
5960
5961 if (found_field == NULL_TREE)
5962 return NULL_TREE;
5963
5964 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
5965 build_fold_indirect_ref (rhs), found_field,
5966 NULL_TREE);
5967 ret = build_fold_addr_expr_loc (location, ret);
5968
5969 if (found_sub_field)
5970 {
5971 ret = convert_to_anonymous_field (location, type, ret);
5972 gcc_assert (ret != NULL_TREE);
5973 }
5974
5975 return ret;
5976}
5977
d44f2f7c 5978/* Issue an error message for a bad initializer component.
5979 GMSGID identifies the message.
5980 The component name is taken from the spelling stack. */
5981
5982static void
53de1631 5983error_init (location_t loc, const char *gmsgid)
d44f2f7c 5984{
5985 char *ofwhat;
5986
5987 /* The gmsgid may be a format string with %< and %>. */
53de1631 5988 error_at (loc, gmsgid);
d44f2f7c 5989 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
5990 if (*ofwhat)
064b0d02 5991 inform (loc, "(near initialization for %qs)", ofwhat);
d44f2f7c 5992}
5993
5994/* Issue a pedantic warning for a bad initializer component. OPT is
5995 the option OPT_* (from options.h) controlling this warning or 0 if
5996 it is unconditionally given. GMSGID identifies the message. The
5997 component name is taken from the spelling stack. */
5998
5999static void
8bdfbc08 6000pedwarn_init (location_t loc, int opt, const char *gmsgid)
d44f2f7c 6001{
6002 char *ofwhat;
064b0d02 6003 bool warned;
d44f2f7c 6004
8bdfbc08 6005 /* Use the location where a macro was expanded rather than where
6006 it was defined to make sure macros defined in system headers
6007 but used incorrectly elsewhere are diagnosed. */
6008 source_location exploc = expansion_point_location_if_in_system_header (loc);
6009
d44f2f7c 6010 /* The gmsgid may be a format string with %< and %>. */
8bdfbc08 6011 warned = pedwarn (exploc, opt, gmsgid);
d44f2f7c 6012 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
064b0d02 6013 if (*ofwhat && warned)
8bdfbc08 6014 inform (exploc, "(near initialization for %qs)", ofwhat);
d44f2f7c 6015}
6016
6017/* Issue a warning for a bad initializer component.
6018
6019 OPT is the OPT_W* value corresponding to the warning option that
6020 controls this warning. GMSGID identifies the message. The
6021 component name is taken from the spelling stack. */
6022
6023static void
6024warning_init (location_t loc, int opt, const char *gmsgid)
6025{
6026 char *ofwhat;
064b0d02 6027 bool warned;
d44f2f7c 6028
8bdfbc08 6029 /* Use the location where a macro was expanded rather than where
6030 it was defined to make sure macros defined in system headers
6031 but used incorrectly elsewhere are diagnosed. */
6032 source_location exploc = expansion_point_location_if_in_system_header (loc);
6033
d44f2f7c 6034 /* The gmsgid may be a format string with %< and %>. */
8bdfbc08 6035 warned = warning_at (exploc, opt, gmsgid);
d44f2f7c 6036 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
064b0d02 6037 if (*ofwhat && warned)
8bdfbc08 6038 inform (exploc, "(near initialization for %qs)", ofwhat);
d44f2f7c 6039}
6040\f
6041/* If TYPE is an array type and EXPR is a parenthesized string
6042 constant, warn if pedantic that EXPR is being used to initialize an
6043 object of type TYPE. */
6044
6045void
c9e51d25 6046maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
d44f2f7c 6047{
6048 if (pedantic
6049 && TREE_CODE (type) == ARRAY_TYPE
6050 && TREE_CODE (expr.value) == STRING_CST
6051 && expr.original_code != STRING_CST)
c9e51d25 6052 pedwarn_init (loc, OPT_Wpedantic,
d44f2f7c 6053 "array initialized from parenthesized string constant");
6054}
6055
b9c74b4d 6056/* Convert value RHS to type TYPE as preparation for an assignment to
6057 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6058 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6059 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6060 constant before any folding.
276beea2 6061 The real work of conversion is done by `convert'.
6062 The purpose of this function is to generate error messages
6063 for assignments that are not allowed in C.
245d6740 6064 ERRTYPE says whether it is argument passing, assignment,
6065 initialization or return.
2b30d46c 6066
7e93eb1a 6067 In the following example, '~' denotes where EXPR_LOC and '^' where
6068 LOCATION point to:
6069
6070 f (var); [ic_argpass]
6071 ^ ~~~
6072 x = var; [ic_assign]
6073 ^ ~~~;
6074 int x = var; [ic_init]
6075 ^^^
6076 return x; [ic_return]
6077 ^
6078
245d6740 6079 FUNCTION is a tree for the function being called.
276beea2 6080 PARMNUM is the number of the argument, for printing in error messages. */
dcf6eb8b 6081
276beea2 6082static tree
ec761d5a 6083convert_for_assignment (location_t location, location_t expr_loc, tree type,
22a75734 6084 tree rhs, tree origtype, enum impl_conv errtype,
d5b637fa 6085 bool null_pointer_constant, tree fundecl,
6086 tree function, int parmnum)
276beea2 6087{
6088 enum tree_code codel = TREE_CODE (type);
c6418a4e 6089 tree orig_rhs = rhs;
276beea2 6090 tree rhstype;
6091 enum tree_code coder;
245d6740 6092 tree rname = NULL_TREE;
34d3c5de 6093 bool objc_ok = false;
245d6740 6094
802c32cd 6095 /* Use the expansion point location to handle cases such as user's
6096 function returning a wrong-type macro defined in a system header. */
6097 location = expansion_point_location_if_in_system_header (location);
6098
5933982f 6099 if (errtype == ic_argpass)
245d6740 6100 {
6101 tree selector;
6102 /* Change pointer to function to the function itself for
6103 diagnostics. */
6104 if (TREE_CODE (function) == ADDR_EXPR
6105 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6106 function = TREE_OPERAND (function, 0);
6107
6108 /* Handle an ObjC selector specially for diagnostics. */
6109 selector = objc_message_selector ();
6110 rname = function;
6111 if (selector && parmnum > 2)
6112 {
6113 rname = selector;
6114 parmnum -= 2;
6115 }
6116 }
6117
6118 /* This macro is used to emit diagnostics to ensure that all format
6119 strings are complete sentences, visible to gettext and checked at
6120 compile time. */
8a8211df 6121#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
3230bfa8 6122 do { \
6123 switch (errtype) \
6124 { \
6125 case ic_argpass: \
dcfff6f9 6126 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
e60a6f7b 6127 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
dcfff6f9 6128 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
3230bfa8 6129 "expected %qT but argument is of type %qT", \
6130 type, rhstype); \
6131 break; \
3230bfa8 6132 case ic_assign: \
6133 pedwarn (LOCATION, OPT, AS); \
6134 break; \
6135 case ic_init: \
1183a806 6136 pedwarn_init (LOCATION, OPT, IN); \
3230bfa8 6137 break; \
6138 case ic_return: \
c9e51d25 6139 pedwarn (LOCATION, OPT, RE); \
3230bfa8 6140 break; \
6141 default: \
6142 gcc_unreachable (); \
6143 } \
245d6740 6144 } while (0)
dcf6eb8b 6145
754eb2f5 6146 /* This macro is used to emit diagnostics to ensure that all format
6147 strings are complete sentences, visible to gettext and checked at
8a8211df 6148 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
754eb2f5 6149 extra parameter to enumerate qualifiers. */
8a8211df 6150#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
754eb2f5 6151 do { \
6152 switch (errtype) \
6153 { \
6154 case ic_argpass: \
dcfff6f9 6155 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
754eb2f5 6156 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
dcfff6f9 6157 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
754eb2f5 6158 "expected %qT but argument is of type %qT", \
6159 type, rhstype); \
6160 break; \
6161 case ic_assign: \
dcfff6f9 6162 pedwarn (LOCATION, OPT, AS, QUALS); \
754eb2f5 6163 break; \
6164 case ic_init: \
dcfff6f9 6165 pedwarn (LOCATION, OPT, IN, QUALS); \
754eb2f5 6166 break; \
6167 case ic_return: \
dcfff6f9 6168 pedwarn (LOCATION, OPT, RE, QUALS); \
754eb2f5 6169 break; \
6170 default: \
6171 gcc_unreachable (); \
6172 } \
6173 } while (0)
6174
8a8211df 6175 /* This macro is used to emit diagnostics to ensure that all format
6176 strings are complete sentences, visible to gettext and checked at
6177 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6178 warning_at instead of pedwarn. */
6179#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6180 do { \
6181 switch (errtype) \
6182 { \
6183 case ic_argpass: \
6184 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6185 inform ((fundecl && !DECL_IS_BUILTIN (fundecl)) \
6186 ? DECL_SOURCE_LOCATION (fundecl) : PLOC, \
6187 "expected %qT but argument is of type %qT", \
6188 type, rhstype); \
6189 break; \
6190 case ic_assign: \
6191 warning_at (LOCATION, OPT, AS, QUALS); \
6192 break; \
6193 case ic_init: \
6194 warning_at (LOCATION, OPT, IN, QUALS); \
6195 break; \
6196 case ic_return: \
6197 warning_at (LOCATION, OPT, RE, QUALS); \
6198 break; \
6199 default: \
6200 gcc_unreachable (); \
6201 } \
6202 } while (0)
6203
c6418a4e 6204 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6205 rhs = TREE_OPERAND (rhs, 0);
6206
276beea2 6207 rhstype = TREE_TYPE (rhs);
6208 coder = TREE_CODE (rhstype);
6209
6210 if (coder == ERROR_MARK)
6211 return error_mark_node;
6212
34d3c5de 6213 if (c_dialect_objc ())
6214 {
6215 int parmno;
6216
6217 switch (errtype)
6218 {
6219 case ic_return:
6220 parmno = 0;
6221 break;
6222
6223 case ic_assign:
6224 parmno = -1;
6225 break;
6226
6227 case ic_init:
6228 parmno = -2;
6229 break;
6230
6231 default:
6232 parmno = parmnum;
6233 break;
6234 }
6235
6236 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6237 }
6238
b9c74b4d 6239 if (warn_cxx_compat)
6240 {
6241 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6242 if (checktype != error_mark_node
6243 && TREE_CODE (type) == ENUMERAL_TYPE
6244 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
6245 {
8a8211df 6246 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wc___compat,
6247 G_("enum conversion when passing argument "
6248 "%d of %qE is invalid in C++"),
6249 G_("enum conversion in assignment is "
6250 "invalid in C++"),
6251 G_("enum conversion in initialization is "
6252 "invalid in C++"),
6253 G_("enum conversion in return is "
6254 "invalid in C++"));
b9c74b4d 6255 }
6256 }
6257
276beea2 6258 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
f170d67f 6259 return rhs;
276beea2 6260
6261 if (coder == VOID_TYPE)
628fa4f8 6262 {
050526fe 6263 /* Except for passing an argument to an unprototyped function,
6264 this is a constraint violation. When passing an argument to
6265 an unprototyped function, it is compile-time undefined;
6266 making it a constraint in that case was rejected in
6267 DR#252. */
e60a6f7b 6268 error_at (location, "void value not ignored as it ought to be");
276beea2 6269 return error_mark_node;
628fa4f8 6270 }
22a3f7bd 6271 rhs = require_complete_type (location, rhs);
f831c98f 6272 if (rhs == error_mark_node)
6273 return error_mark_node;
547c6b1f 6274
6275 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6276 return error_mark_node;
6277
2c1c8a2c 6278 /* A non-reference type can convert to a reference. This handles
6279 va_start, va_copy and possibly port built-ins. */
6280 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
628fa4f8 6281 {
276beea2 6282 if (!lvalue_p (rhs))
628fa4f8 6283 {
e60a6f7b 6284 error_at (location, "cannot pass rvalue to reference parameter");
276beea2 6285 return error_mark_node;
628fa4f8 6286 }
276beea2 6287 if (!c_mark_addressable (rhs))
6288 return error_mark_node;
6289 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
e60a6f7b 6290 SET_EXPR_LOCATION (rhs, location);
276beea2 6291
ec761d5a 6292 rhs = convert_for_assignment (location, expr_loc,
22a75734 6293 build_pointer_type (TREE_TYPE (type)),
6294 rhs, origtype, errtype,
6295 null_pointer_constant, fundecl, function,
6296 parmnum);
2c1c8a2c 6297 if (rhs == error_mark_node)
6298 return error_mark_node;
276beea2 6299
6300 rhs = build1 (NOP_EXPR, type, rhs);
e60a6f7b 6301 SET_EXPR_LOCATION (rhs, location);
276beea2 6302 return rhs;
628fa4f8 6303 }
276beea2 6304 /* Some types can interconvert without explicit casts. */
0d45e76f 6305 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
546c4794 6306 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
276beea2 6307 return convert (type, rhs);
6308 /* Arithmetic types all interconvert, and enum is treated like int. */
6309 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
9421ebb9 6310 || codel == FIXED_POINT_TYPE
276beea2 6311 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6312 || codel == BOOLEAN_TYPE)
6313 && (coder == INTEGER_TYPE || coder == REAL_TYPE
9421ebb9 6314 || coder == FIXED_POINT_TYPE
276beea2 6315 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6316 || coder == BOOLEAN_TYPE))
a75b1c71 6317 {
6318 tree ret;
6319 bool save = in_late_binary_op;
823a9dd4 6320 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6321 || (coder == REAL_TYPE
6322 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
6323 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
a75b1c71 6324 in_late_binary_op = true;
ec761d5a 6325 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6326 ? expr_loc : location, type, orig_rhs);
823a9dd4 6327 in_late_binary_op = save;
a75b1c71 6328 return ret;
6329 }
628fa4f8 6330
aedd6e32 6331 /* Aggregates in different TUs might need conversion. */
6332 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6333 && codel == coder
6334 && comptypes (type, rhstype))
ec761d5a 6335 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6336 ? expr_loc : location, type, rhs);
aedd6e32 6337
8df5a43d 6338 /* Conversion to a transparent union or record from its member types.
276beea2 6339 This applies only to function arguments. */
8df5a43d 6340 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6341 && TYPE_TRANSPARENT_AGGR (type))
5933982f 6342 && errtype == ic_argpass)
628fa4f8 6343 {
0f7adc03 6344 tree memb, marginal_memb = NULL_TREE;
276beea2 6345
1767a056 6346 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
628fa4f8 6347 {
0f7adc03 6348 tree memb_type = TREE_TYPE (memb);
628fa4f8 6349
276beea2 6350 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
3ed275a6 6351 TYPE_MAIN_VARIANT (rhstype)))
276beea2 6352 break;
8915b00e 6353
276beea2 6354 if (TREE_CODE (memb_type) != POINTER_TYPE)
6355 continue;
2b30d46c 6356
276beea2 6357 if (coder == POINTER_TYPE)
6358 {
6359 tree ttl = TREE_TYPE (memb_type);
6360 tree ttr = TREE_TYPE (rhstype);
628fa4f8 6361
276beea2 6362 /* Any non-function converts to a [const][volatile] void *
6363 and vice versa; otherwise, targets must be the same.
6364 Meanwhile, the lhs target must have all the qualifiers of
6365 the rhs. */
b560fabd 6366 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6367 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
d5b637fa 6368 || comp_target_types (location, memb_type, rhstype))
276beea2 6369 {
b560fabd 6370 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6371 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
276beea2 6372 /* If this type won't generate any warnings, use it. */
b560fabd 6373 if (lquals == rquals
276beea2 6374 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6375 && TREE_CODE (ttl) == FUNCTION_TYPE)
b560fabd 6376 ? ((lquals | rquals) == rquals)
6377 : ((lquals | rquals) == lquals)))
276beea2 6378 break;
628fa4f8 6379
276beea2 6380 /* Keep looking for a better type, but remember this one. */
0f7adc03 6381 if (!marginal_memb)
6382 marginal_memb = memb;
276beea2 6383 }
6384 }
448d3ee7 6385
276beea2 6386 /* Can convert integer zero to any pointer type. */
a75b1c71 6387 if (null_pointer_constant)
276beea2 6388 {
6389 rhs = null_pointer_node;
6390 break;
6391 }
6392 }
628fa4f8 6393
0f7adc03 6394 if (memb || marginal_memb)
276beea2 6395 {
0f7adc03 6396 if (!memb)
276beea2 6397 {
6398 /* We have only a marginally acceptable member type;
6399 it needs a warning. */
0f7adc03 6400 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
276beea2 6401 tree ttr = TREE_TYPE (rhstype);
c377133d 6402
276beea2 6403 /* Const and volatile mean something different for function
6404 types, so the usual warnings are not appropriate. */
6405 if (TREE_CODE (ttr) == FUNCTION_TYPE
6406 && TREE_CODE (ttl) == FUNCTION_TYPE)
6407 {
6408 /* Because const and volatile on functions are
6409 restrictions that say the function will not do
6410 certain things, it is okay to use a const or volatile
6411 function where an ordinary one is wanted, but not
6412 vice-versa. */
6d5d708e 6413 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6414 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8a8211df 6415 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6416 OPT_Wdiscarded_qualifiers,
6417 G_("passing argument %d of %qE "
6418 "makes %q#v qualified function "
6419 "pointer from unqualified"),
6420 G_("assignment makes %q#v qualified "
6421 "function pointer from "
6422 "unqualified"),
6423 G_("initialization makes %q#v qualified "
6424 "function pointer from "
6425 "unqualified"),
6426 G_("return makes %q#v qualified function "
6427 "pointer from unqualified"),
6428 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
276beea2 6429 }
6d5d708e 6430 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6431 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
8a8211df 6432 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6433 OPT_Wdiscarded_qualifiers,
6434 G_("passing argument %d of %qE discards "
6435 "%qv qualifier from pointer target type"),
6436 G_("assignment discards %qv qualifier "
6437 "from pointer target type"),
6438 G_("initialization discards %qv qualifier "
6439 "from pointer target type"),
6440 G_("return discards %qv qualifier from "
6441 "pointer target type"),
6442 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0f7adc03 6443
6444 memb = marginal_memb;
276beea2 6445 }
628fa4f8 6446
8864917d 6447 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
29438999 6448 pedwarn (location, OPT_Wpedantic,
8864917d 6449 "ISO C prohibits argument conversion to union type");
5fcbb750 6450
389dd41b 6451 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0f7adc03 6452 return build_constructor_single (type, memb, rhs);
276beea2 6453 }
5fcbb750 6454 }
6455
276beea2 6456 /* Conversions among pointers */
6457 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6458 && (coder == codel))
628fa4f8 6459 {
276beea2 6460 tree ttl = TREE_TYPE (type);
6461 tree ttr = TREE_TYPE (rhstype);
aebc8537 6462 tree mvl = ttl;
6463 tree mvr = ttr;
276beea2 6464 bool is_opaque_pointer;
c17b85ea 6465 int target_cmp = 0; /* Cache comp_target_types () result. */
6d5d708e 6466 addr_space_t asl;
6467 addr_space_t asr;
628fa4f8 6468
aebc8537 6469 if (TREE_CODE (mvl) != ARRAY_TYPE)
b560fabd 6470 mvl = (TYPE_ATOMIC (mvl)
6471 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6472 TYPE_QUAL_ATOMIC)
6473 : TYPE_MAIN_VARIANT (mvl));
aebc8537 6474 if (TREE_CODE (mvr) != ARRAY_TYPE)
b560fabd 6475 mvr = (TYPE_ATOMIC (mvr)
6476 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6477 TYPE_QUAL_ATOMIC)
6478 : TYPE_MAIN_VARIANT (mvr));
276beea2 6479 /* Opaque pointers are treated like void pointers. */
ed7c4e62 6480 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
a0c938f0 6481
2fdec027 6482 /* The Plan 9 compiler permits a pointer to a struct to be
6483 automatically converted into a pointer to an anonymous field
6484 within the struct. */
6485 if (flag_plan9_extensions
ef61516b 6486 && RECORD_OR_UNION_TYPE_P (mvl)
6487 && RECORD_OR_UNION_TYPE_P (mvr)
2fdec027 6488 && mvl != mvr)
6489 {
6490 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6491 if (new_rhs != NULL_TREE)
6492 {
6493 rhs = new_rhs;
6494 rhstype = TREE_TYPE (rhs);
6495 coder = TREE_CODE (rhstype);
6496 ttr = TREE_TYPE (rhstype);
6497 mvr = TYPE_MAIN_VARIANT (ttr);
6498 }
6499 }
6500
fa2eba0d 6501 /* C++ does not allow the implicit conversion void* -> T*. However,
a0c938f0 6502 for the purpose of reducing the number of false positives, we
6503 tolerate the special case of
fa2eba0d 6504
a0c938f0 6505 int *p = NULL;
fa2eba0d 6506
a0c938f0 6507 where NULL is typically defined in C to be '(void *) 0'. */
bcdafdcb 6508 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
a07f86a6 6509 warning_at (errtype == ic_argpass ? expr_loc : location,
6510 OPT_Wc___compat,
6511 "request for implicit conversion "
e60a6f7b 6512 "from %qT to %qT not permitted in C++", rhstype, type);
628fa4f8 6513
6d5d708e 6514 /* See if the pointers point to incompatible address spaces. */
6515 asl = TYPE_ADDR_SPACE (ttl);
6516 asr = TYPE_ADDR_SPACE (ttr);
6517 if (!null_pointer_constant_p (rhs)
6518 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6519 {
6520 switch (errtype)
6521 {
6522 case ic_argpass:
a07f86a6 6523 error_at (expr_loc, "passing argument %d of %qE from pointer to "
6d5d708e 6524 "non-enclosed address space", parmnum, rname);
6525 break;
6526 case ic_assign:
6527 error_at (location, "assignment from pointer to "
6528 "non-enclosed address space");
6529 break;
6530 case ic_init:
6531 error_at (location, "initialization from pointer to "
6532 "non-enclosed address space");
6533 break;
6534 case ic_return:
6535 error_at (location, "return from pointer to "
6536 "non-enclosed address space");
6537 break;
6538 default:
6539 gcc_unreachable ();
6540 }
6541 return error_mark_node;
6542 }
6543
95c90e04 6544 /* Check if the right-hand side has a format attribute but the
6545 left-hand side doesn't. */
b86527d8 6546 if (warn_suggest_attribute_format
be7350e7 6547 && check_missing_format_attribute (type, rhstype))
a0c938f0 6548 {
be7350e7 6549 switch (errtype)
6550 {
6551 case ic_argpass:
a07f86a6 6552 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
e60a6f7b 6553 "argument %d of %qE might be "
6554 "a candidate for a format attribute",
6555 parmnum, rname);
be7350e7 6556 break;
6557 case ic_assign:
b86527d8 6558 warning_at (location, OPT_Wsuggest_attribute_format,
e60a6f7b 6559 "assignment left-hand side might be "
6560 "a candidate for a format attribute");
be7350e7 6561 break;
6562 case ic_init:
b86527d8 6563 warning_at (location, OPT_Wsuggest_attribute_format,
e60a6f7b 6564 "initialization left-hand side might be "
6565 "a candidate for a format attribute");
be7350e7 6566 break;
6567 case ic_return:
b86527d8 6568 warning_at (location, OPT_Wsuggest_attribute_format,
e60a6f7b 6569 "return type might be "
6570 "a candidate for a format attribute");
be7350e7 6571 break;
6572 default:
6573 gcc_unreachable ();
6574 }
95c90e04 6575 }
a0c938f0 6576
276beea2 6577 /* Any non-function converts to a [const][volatile] void *
6578 and vice versa; otherwise, targets must be the same.
6579 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
b560fabd 6580 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6581 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
d5b637fa 6582 || (target_cmp = comp_target_types (location, type, rhstype))
276beea2 6583 || is_opaque_pointer
7cbb7f1a 6584 || ((c_common_unsigned_type (mvl)
6585 == c_common_unsigned_type (mvr))
b560fabd 6586 && (c_common_signed_type (mvl)
6587 == c_common_signed_type (mvr))
6588 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
276beea2 6589 {
8a8211df 6590 /* Warn about loss of qualifers from pointers to arrays with
6591 qualifiers on the element type. */
6592 if (TREE_CODE (ttr) == ARRAY_TYPE)
6593 {
6594 ttr = strip_array_types (ttr);
6595 ttl = strip_array_types (ttl);
6596
6597 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6598 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6599 WARNING_FOR_QUALIFIERS (location, expr_loc,
6600 OPT_Wdiscarded_array_qualifiers,
6601 G_("passing argument %d of %qE discards "
6602 "%qv qualifier from pointer target type"),
6603 G_("assignment discards %qv qualifier "
6604 "from pointer target type"),
6605 G_("initialization discards %qv qualifier "
6606 "from pointer target type"),
6607 G_("return discards %qv qualifier from "
6608 "pointer target type"),
6609 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6610 }
6611 else if (pedantic
276beea2 6612 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6613 ||
6614 (VOID_TYPE_P (ttr)
a75b1c71 6615 && !null_pointer_constant
276beea2 6616 && TREE_CODE (ttl) == FUNCTION_TYPE)))
8a8211df 6617 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6618 G_("ISO C forbids passing argument %d of "
6619 "%qE between function pointer "
6620 "and %<void *%>"),
6621 G_("ISO C forbids assignment between "
6622 "function pointer and %<void *%>"),
6623 G_("ISO C forbids initialization between "
6624 "function pointer and %<void *%>"),
6625 G_("ISO C forbids return between function "
6626 "pointer and %<void *%>"));
276beea2 6627 /* Const and volatile mean something different for function types,
6628 so the usual warnings are not appropriate. */
6629 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6630 && TREE_CODE (ttl) != FUNCTION_TYPE)
6631 {
8a8211df 6632 /* Don't warn about loss of qualifier for conversions from
6633 qualified void* to pointers to arrays with corresponding
6634 qualifier on the element type. */
6635 if (!pedantic)
6636 ttl = strip_array_types (ttl);
6637
b560fabd 6638 /* Assignments between atomic and non-atomic objects are OK. */
6639 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6640 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
34d3c5de 6641 {
8a8211df 6642 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6643 OPT_Wdiscarded_qualifiers,
6644 G_("passing argument %d of %qE discards "
6645 "%qv qualifier from pointer target type"),
6646 G_("assignment discards %qv qualifier "
6647 "from pointer target type"),
6648 G_("initialization discards %qv qualifier "
6649 "from pointer target type"),
6650 G_("return discards %qv qualifier from "
6651 "pointer target type"),
6652 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
34d3c5de 6653 }
276beea2 6654 /* If this is not a case of ignoring a mismatch in signedness,
6655 no warning. */
6656 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
c17b85ea 6657 || target_cmp)
276beea2 6658 ;
6659 /* If there is a mismatch, do warn. */
380bf74b 6660 else if (warn_pointer_sign)
8a8211df 6661 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpointer_sign,
6662 G_("pointer targets in passing argument "
6663 "%d of %qE differ in signedness"),
6664 G_("pointer targets in assignment "
6665 "differ in signedness"),
6666 G_("pointer targets in initialization "
6667 "differ in signedness"),
6668 G_("pointer targets in return differ "
6669 "in signedness"));
276beea2 6670 }
6671 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6672 && TREE_CODE (ttr) == FUNCTION_TYPE)
6673 {
6674 /* Because const and volatile on functions are restrictions
6675 that say the function will not do certain things,
6676 it is okay to use a const or volatile function
6677 where an ordinary one is wanted, but not vice-versa. */
6d5d708e 6678 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6679 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
8a8211df 6680 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6681 OPT_Wdiscarded_qualifiers,
6682 G_("passing argument %d of %qE makes "
6683 "%q#v qualified function pointer "
6684 "from unqualified"),
6685 G_("assignment makes %q#v qualified function "
6686 "pointer from unqualified"),
6687 G_("initialization makes %q#v qualified "
6688 "function pointer from unqualified"),
6689 G_("return makes %q#v qualified function "
6690 "pointer from unqualified"),
6691 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
276beea2 6692 }
6693 }
6694 else
34d3c5de 6695 /* Avoid warning about the volatile ObjC EH puts on decls. */
6696 if (!objc_ok)
8a8211df 6697 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6698 OPT_Wincompatible_pointer_types,
6699 G_("passing argument %d of %qE from "
6700 "incompatible pointer type"),
6701 G_("assignment from incompatible pointer type"),
6702 G_("initialization from incompatible "
6703 "pointer type"),
6704 G_("return from incompatible pointer type"));
34d3c5de 6705
276beea2 6706 return convert (type, rhs);
6707 }
5cd106da 6708 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
6709 {
050526fe 6710 /* ??? This should not be an error when inlining calls to
6711 unprototyped functions. */
e60a6f7b 6712 error_at (location, "invalid use of non-lvalue array");
5cd106da 6713 return error_mark_node;
6714 }
276beea2 6715 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
628fa4f8 6716 {
276beea2 6717 /* An explicit constant 0 can convert to a pointer,
6718 or one that results from arithmetic, even including
6719 a cast to integer type. */
a75b1c71 6720 if (!null_pointer_constant)
8a8211df 6721 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6722 OPT_Wint_conversion,
6723 G_("passing argument %d of %qE makes "
6724 "pointer from integer without a cast"),
6725 G_("assignment makes pointer from integer "
6726 "without a cast"),
6727 G_("initialization makes pointer from "
6728 "integer without a cast"),
6729 G_("return makes pointer from integer "
6730 "without a cast"));
d318227b 6731
6732 return convert (type, rhs);
628fa4f8 6733 }
276beea2 6734 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
628fa4f8 6735 {
8a8211df 6736 PEDWARN_FOR_ASSIGNMENT (location, expr_loc,
6737 OPT_Wint_conversion,
6738 G_("passing argument %d of %qE makes integer "
6739 "from pointer without a cast"),
6740 G_("assignment makes integer from pointer "
6741 "without a cast"),
6742 G_("initialization makes integer from pointer "
6743 "without a cast"),
6744 G_("return makes integer from pointer "
6745 "without a cast"));
276beea2 6746 return convert (type, rhs);
6747 }
6748 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
a75b1c71 6749 {
6750 tree ret;
6751 bool save = in_late_binary_op;
6752 in_late_binary_op = true;
6753 ret = convert (type, rhs);
6754 in_late_binary_op = save;
6755 return ret;
6756 }
628fa4f8 6757
245d6740 6758 switch (errtype)
276beea2 6759 {
245d6740 6760 case ic_argpass:
a07f86a6 6761 error_at (expr_loc, "incompatible type for argument %d of %qE", parmnum,
6762 rname);
bd8a0c52 6763 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
a07f86a6 6764 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
bd8a0c52 6765 "expected %qT but argument is of type %qT", type, rhstype);
245d6740 6766 break;
6767 case ic_assign:
e60a6f7b 6768 error_at (location, "incompatible types when assigning to type %qT from "
6769 "type %qT", type, rhstype);
245d6740 6770 break;
6771 case ic_init:
e60a6f7b 6772 error_at (location,
a07f86a6 6773 "incompatible types when initializing type %qT using type %qT",
e60a6f7b 6774 type, rhstype);
245d6740 6775 break;
6776 case ic_return:
e60a6f7b 6777 error_at (location,
a07f86a6 6778 "incompatible types when returning type %qT but %qT was "
e60a6f7b 6779 "expected", rhstype, type);
245d6740 6780 break;
6781 default:
6782 gcc_unreachable ();
628fa4f8 6783 }
8ba8da08 6784
276beea2 6785 return error_mark_node;
6786}
276beea2 6787\f
6788/* If VALUE is a compound expr all of whose expressions are constant, then
6789 return its value. Otherwise, return error_mark_node.
d0a47c8d 6790
276beea2 6791 This is for handling COMPOUND_EXPRs as initializer elements
6792 which is allowed with a warning when -pedantic is specified. */
d0a47c8d 6793
276beea2 6794static tree
6795valid_compound_expr_initializer (tree value, tree endtype)
6796{
6797 if (TREE_CODE (value) == COMPOUND_EXPR)
6798 {
6799 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
6800 == error_mark_node)
6801 return error_mark_node;
6802 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
6803 endtype);
6804 }
fdf95cc9 6805 else if (!initializer_constant_valid_p (value, endtype))
276beea2 6806 return error_mark_node;
6807 else
6808 return value;
d0a47c8d 6809}
628fa4f8 6810\f
276beea2 6811/* Perform appropriate conversions on the initial value of a variable,
6812 store it in the declaration DECL,
6813 and print any error messages that are appropriate.
b9c74b4d 6814 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
e60a6f7b 6815 If the init is invalid, store an ERROR_MARK.
6816
6817 INIT_LOC is the location of the initial value. */
628fa4f8 6818
276beea2 6819void
e60a6f7b 6820store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
628fa4f8 6821{
276beea2 6822 tree value, type;
a75b1c71 6823 bool npc = false;
628fa4f8 6824
276beea2 6825 /* If variable's type was invalidly declared, just ignore it. */
628fa4f8 6826
276beea2 6827 type = TREE_TYPE (decl);
6828 if (TREE_CODE (type) == ERROR_MARK)
6829 return;
628fa4f8 6830
276beea2 6831 /* Digest the specified initializer into an expression. */
628fa4f8 6832
a75b1c71 6833 if (init)
6834 npc = null_pointer_constant_p (init);
e60a6f7b 6835 value = digest_init (init_loc, type, init, origtype, npc,
6836 true, TREE_STATIC (decl));
628fa4f8 6837
276beea2 6838 /* Store the expression if valid; else report error. */
628fa4f8 6839
3df42822 6840 if (!in_system_header_at (input_location)
84166705 6841 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
6bf97f82 6842 warning (OPT_Wtraditional, "traditional C rejects automatic "
6843 "aggregate initialization");
2b30d46c 6844
591970b4 6845 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
6846 DECL_INITIAL (decl) = value;
628fa4f8 6847
276beea2 6848 /* ANSI wants warnings about out-of-range constant initializers. */
6849 STRIP_TYPE_NOPS (value);
48e1416a 6850 if (TREE_STATIC (decl))
67e05044 6851 constant_expression_warning (value);
628fa4f8 6852
276beea2 6853 /* Check if we need to set array size from compound literal size. */
6854 if (TREE_CODE (type) == ARRAY_TYPE
6855 && TYPE_DOMAIN (type) == 0
6856 && value != error_mark_node)
628fa4f8 6857 {
276beea2 6858 tree inside_init = init;
6859
a97c952c 6860 STRIP_TYPE_NOPS (inside_init);
276beea2 6861 inside_init = fold (inside_init);
6862
6863 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
6864 {
ce675af0 6865 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
276beea2 6866
ce675af0 6867 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
276beea2 6868 {
6869 /* For int foo[] = (int [3]){1}; we need to set array size
6870 now since later on array initializer will be just the
6871 brace enclosed list of the compound literal. */
9e1f1fbf 6872 tree etype = strip_array_types (TREE_TYPE (decl));
ce675af0 6873 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
ce675af0 6874 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
276beea2 6875 layout_type (type);
ce675af0 6876 layout_decl (cldecl, 0);
9e1f1fbf 6877 TREE_TYPE (decl)
6878 = c_build_qualified_type (type, TYPE_QUALS (etype));
276beea2 6879 }
6880 }
628fa4f8 6881 }
276beea2 6882}
6883\f
6884/* Methods for storing and printing names for error messages. */
628fa4f8 6885
276beea2 6886/* Implement a spelling stack that allows components of a name to be pushed
6887 and popped. Each element on the stack is this structure. */
628fa4f8 6888
276beea2 6889struct spelling
6890{
6891 int kind;
6892 union
628fa4f8 6893 {
4aeeff51 6894 unsigned HOST_WIDE_INT i;
276beea2 6895 const char *s;
6896 } u;
6897};
2b30d46c 6898
276beea2 6899#define SPELLING_STRING 1
6900#define SPELLING_MEMBER 2
6901#define SPELLING_BOUNDS 3
628fa4f8 6902
276beea2 6903static struct spelling *spelling; /* Next stack element (unused). */
6904static struct spelling *spelling_base; /* Spelling stack base. */
6905static int spelling_size; /* Size of the spelling stack. */
628fa4f8 6906
276beea2 6907/* Macros to save and restore the spelling stack around push_... functions.
6908 Alternative to SAVE_SPELLING_STACK. */
628fa4f8 6909
276beea2 6910#define SPELLING_DEPTH() (spelling - spelling_base)
6911#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
628fa4f8 6912
276beea2 6913/* Push an element on the spelling stack with type KIND and assign VALUE
6914 to MEMBER. */
628fa4f8 6915
276beea2 6916#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
6917{ \
6918 int depth = SPELLING_DEPTH (); \
6919 \
6920 if (depth >= spelling_size) \
6921 { \
6922 spelling_size += 10; \
63aa3db6 6923 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
6924 spelling_size); \
276beea2 6925 RESTORE_SPELLING_DEPTH (depth); \
6926 } \
6927 \
6928 spelling->kind = (KIND); \
6929 spelling->MEMBER = (VALUE); \
6930 spelling++; \
6931}
628fa4f8 6932
276beea2 6933/* Push STRING on the stack. Printed literally. */
628fa4f8 6934
276beea2 6935static void
6936push_string (const char *string)
6937{
6938 PUSH_SPELLING (SPELLING_STRING, string, u.s);
6939}
628fa4f8 6940
276beea2 6941/* Push a member name on the stack. Printed as '.' STRING. */
628fa4f8 6942
276beea2 6943static void
6944push_member_name (tree decl)
6945{
6946 const char *const string
d1dd9ac0 6947 = (DECL_NAME (decl)
6948 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
6949 : _("<anonymous>"));
276beea2 6950 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
6951}
628fa4f8 6952
276beea2 6953/* Push an array bounds on the stack. Printed as [BOUNDS]. */
628fa4f8 6954
276beea2 6955static void
4aeeff51 6956push_array_bounds (unsigned HOST_WIDE_INT bounds)
276beea2 6957{
6958 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
6959}
cf18ae1f 6960
276beea2 6961/* Compute the maximum size in bytes of the printed spelling. */
628fa4f8 6962
276beea2 6963static int
6964spelling_length (void)
6965{
6966 int size = 0;
6967 struct spelling *p;
628fa4f8 6968
276beea2 6969 for (p = spelling_base; p < spelling; p++)
6970 {
6971 if (p->kind == SPELLING_BOUNDS)
6972 size += 25;
6973 else
6974 size += strlen (p->u.s) + 1;
6975 }
6976
6977 return size;
628fa4f8 6978}
628fa4f8 6979
276beea2 6980/* Print the spelling to BUFFER and return it. */
628fa4f8 6981
276beea2 6982static char *
6983print_spelling (char *buffer)
628fa4f8 6984{
276beea2 6985 char *d = buffer;
6986 struct spelling *p;
628fa4f8 6987
276beea2 6988 for (p = spelling_base; p < spelling; p++)
6989 if (p->kind == SPELLING_BOUNDS)
6990 {
4aeeff51 6991 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
276beea2 6992 d += strlen (d);
6993 }
6994 else
6995 {
6996 const char *s;
6997 if (p->kind == SPELLING_MEMBER)
6998 *d++ = '.';
6999 for (s = p->u.s; (*d = *s++); d++)
7000 ;
7001 }
7002 *d++ = '\0';
7003 return buffer;
7004}
628fa4f8 7005
276beea2 7006/* Digest the parser output INIT as an initializer for type TYPE.
7007 Return a C expression of type TYPE to represent the initial value.
fdcfba98 7008
b9c74b4d 7009 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7010
a75b1c71 7011 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7012
3ad5f027 7013 If INIT is a string constant, STRICT_STRING is true if it is
7014 unparenthesized or we should not warn here for it being parenthesized.
7015 For other types of INIT, STRICT_STRING is not used.
7016
e60a6f7b 7017 INIT_LOC is the location of the INIT.
7018
276beea2 7019 REQUIRE_CONSTANT requests an error if non-constant initializers or
7020 elements are seen. */
fdcfba98 7021
276beea2 7022static tree
e60a6f7b 7023digest_init (location_t init_loc, tree type, tree init, tree origtype,
7024 bool null_pointer_constant, bool strict_string,
7025 int require_constant)
276beea2 7026{
7027 enum tree_code code = TREE_CODE (type);
7028 tree inside_init = init;
c6418a4e 7029 tree semantic_type = NULL_TREE;
a75b1c71 7030 bool maybe_const = true;
fdcfba98 7031
276beea2 7032 if (type == error_mark_node
b5a68e6b 7033 || !init
224bdd51 7034 || error_operand_p (init))
276beea2 7035 return error_mark_node;
fdcfba98 7036
a97c952c 7037 STRIP_TYPE_NOPS (inside_init);
fdcfba98 7038
c6418a4e 7039 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7040 {
7041 semantic_type = TREE_TYPE (inside_init);
7042 inside_init = TREE_OPERAND (inside_init, 0);
7043 }
a75b1c71 7044 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7045 inside_init = decl_constant_value_for_optimization (inside_init);
fdcfba98 7046
276beea2 7047 /* Initialization of an array of chars from a string constant
7048 optionally enclosed in braces. */
fdcfba98 7049
e91a0d39 7050 if (code == ARRAY_TYPE && inside_init
7051 && TREE_CODE (inside_init) == STRING_CST)
276beea2 7052 {
b560fabd 7053 tree typ1
7054 = (TYPE_ATOMIC (TREE_TYPE (type))
7055 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7056 TYPE_QUAL_ATOMIC)
7057 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
e91a0d39 7058 /* Note that an array could be both an array of character type
7059 and an array of wchar_t if wchar_t is signed char or unsigned
7060 char. */
7061 bool char_array = (typ1 == char_type_node
7062 || typ1 == signed_char_type_node
7063 || typ1 == unsigned_char_type_node);
7064 bool wchar_array = !!comptypes (typ1, wchar_type_node);
b0726616 7065 bool char16_array = !!comptypes (typ1, char16_type_node);
7066 bool char32_array = !!comptypes (typ1, char32_type_node);
7067
7068 if (char_array || wchar_array || char16_array || char32_array)
fdcfba98 7069 {
3ad5f027 7070 struct c_expr expr;
b0726616 7071 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
3ad5f027 7072 expr.value = inside_init;
7073 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
841fdaa6 7074 expr.original_type = NULL;
c9e51d25 7075 maybe_warn_string_init (init_loc, type, expr);
3ad5f027 7076
fe49e588 7077 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
29438999 7078 pedwarn_init (init_loc, OPT_Wpedantic,
fe49e588 7079 "initialization of a flexible array member");
7080
276beea2 7081 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3ed275a6 7082 TYPE_MAIN_VARIANT (type)))
276beea2 7083 return inside_init;
fdcfba98 7084
b0726616 7085 if (char_array)
276beea2 7086 {
b0726616 7087 if (typ2 != char_type_node)
7088 {
53de1631 7089 error_init (init_loc, "char-array initialized from wide "
7090 "string");
b0726616 7091 return error_mark_node;
7092 }
276beea2 7093 }
b0726616 7094 else
276beea2 7095 {
b0726616 7096 if (typ2 == char_type_node)
7097 {
53de1631 7098 error_init (init_loc, "wide character array initialized "
7099 "from non-wide string");
b0726616 7100 return error_mark_node;
7101 }
7102 else if (!comptypes(typ1, typ2))
7103 {
53de1631 7104 error_init (init_loc, "wide character array initialized "
7105 "from incompatible wide string");
b0726616 7106 return error_mark_node;
7107 }
fdcfba98 7108 }
2b30d46c 7109
276beea2 7110 TREE_TYPE (inside_init) = type;
7111 if (TYPE_DOMAIN (type) != 0
7112 && TYPE_SIZE (type) != 0
019c559b 7113 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7114 {
7115 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7116
b0726616 7117 /* Subtract the size of a single (possibly wide) character
276beea2 7118 because it's ok to ignore the terminating null char
7119 that is counted in the length of the constant. */
019c559b 7120 if (0 > compare_tree_int (TYPE_SIZE_UNIT (type),
7121 (len
7122 - (TYPE_PRECISION (typ1)
7123 / BITS_PER_UNIT))))
7124 pedwarn_init (init_loc, 0,
7125 ("initializer-string for array of chars "
7126 "is too long"));
7127 else if (warn_cxx_compat
7128 && 0 > compare_tree_int (TYPE_SIZE_UNIT (type), len))
7129 warning_at (init_loc, OPT_Wc___compat,
7130 ("initializer-string for array chars "
7131 "is too long for C++"));
7132 }
fdcfba98 7133
276beea2 7134 return inside_init;
fdcfba98 7135 }
e91a0d39 7136 else if (INTEGRAL_TYPE_P (typ1))
7137 {
53de1631 7138 error_init (init_loc, "array of inappropriate type initialized "
e91a0d39 7139 "from string constant");
7140 return error_mark_node;
7141 }
fdcfba98 7142 }
7143
276beea2 7144 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7145 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7146 below and handle as a constructor. */
3a3c175c 7147 if (code == VECTOR_TYPE
6290f0db 7148 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
546c4794 7149 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
3a3c175c 7150 && TREE_CONSTANT (inside_init))
7151 {
7152 if (TREE_CODE (inside_init) == VECTOR_CST
7153 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7154 TYPE_MAIN_VARIANT (type)))
7155 return inside_init;
7156
7157 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7158 {
c75b4594 7159 unsigned HOST_WIDE_INT ix;
7160 tree value;
7161 bool constant_p = true;
3a3c175c 7162
7163 /* Iterate through elements and check if all constructor
7164 elements are *_CSTs. */
c75b4594 7165 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7166 if (!CONSTANT_CLASS_P (value))
7167 {
7168 constant_p = false;
7169 break;
7170 }
3a3c175c 7171
c75b4594 7172 if (constant_p)
7173 return build_vector_from_ctor (type,
7174 CONSTRUCTOR_ELTS (inside_init));
3a3c175c 7175 }
7176 }
4b4dcf9c 7177
6513b50d 7178 if (warn_sequence_point)
7179 verify_sequence_points (inside_init);
7180
276beea2 7181 /* Any type can be initialized
7182 from an expression of the same type, optionally with braces. */
628fa4f8 7183
276beea2 7184 if (inside_init && TREE_TYPE (inside_init) != 0
7185 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
3ed275a6 7186 TYPE_MAIN_VARIANT (type))
276beea2 7187 || (code == ARRAY_TYPE
3ed275a6 7188 && comptypes (TREE_TYPE (inside_init), type))
276beea2 7189 || (code == VECTOR_TYPE
3ed275a6 7190 && comptypes (TREE_TYPE (inside_init), type))
276beea2 7191 || (code == POINTER_TYPE
48b3d385 7192 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
276beea2 7193 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
3ed275a6 7194 TREE_TYPE (type)))))
276beea2 7195 {
7196 if (code == POINTER_TYPE)
5cd106da 7197 {
5cd106da 7198 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7199 {
e78cf9a7 7200 if (TREE_CODE (inside_init) == STRING_CST
7201 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
e60a6f7b 7202 inside_init = array_to_pointer_conversion
7203 (init_loc, inside_init);
e78cf9a7 7204 else
7205 {
53de1631 7206 error_init (init_loc, "invalid use of non-lvalue array");
e78cf9a7 7207 return error_mark_node;
7208 }
5cd106da 7209 }
e78cf9a7 7210 }
5cd106da 7211
cd81f692 7212 if (code == VECTOR_TYPE)
7213 /* Although the types are compatible, we may require a
7214 conversion. */
7215 inside_init = convert (type, inside_init);
276beea2 7216
b7d1b569 7217 if (require_constant
276beea2 7218 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
628fa4f8 7219 {
276beea2 7220 /* As an extension, allow initializing objects with static storage
7221 duration with compound literals (which are then treated just as
b7d1b569 7222 the brace enclosed list they contain). Also allow this for
7223 vectors, as we can only assign them with compound literals. */
bccf97da 7224 if (flag_isoc99 && code != VECTOR_TYPE)
7225 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7226 "is not constant");
276beea2 7227 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7228 inside_init = DECL_INITIAL (decl);
628fa4f8 7229 }
276beea2 7230
7231 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7232 && TREE_CODE (inside_init) != CONSTRUCTOR)
628fa4f8 7233 {
53de1631 7234 error_init (init_loc, "array initialized from non-constant array "
7235 "expression");
276beea2 7236 return error_mark_node;
628fa4f8 7237 }
628fa4f8 7238
29438999 7239 /* Compound expressions can only occur here if -Wpedantic or
276beea2 7240 -pedantic-errors is specified. In the later case, we always want
7241 an error. In the former case, we simply want a warning. */
7242 if (require_constant && pedantic
7243 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7244 {
7245 inside_init
7246 = valid_compound_expr_initializer (inside_init,
7247 TREE_TYPE (inside_init));
7248 if (inside_init == error_mark_node)
53de1631 7249 error_init (init_loc, "initializer element is not constant");
2b30d46c 7250 else
29438999 7251 pedwarn_init (init_loc, OPT_Wpedantic,
21ca8540 7252 "initializer element is not constant");
276beea2 7253 if (flag_pedantic_errors)
7254 inside_init = error_mark_node;
7255 }
7256 else if (require_constant
fdf95cc9 7257 && !initializer_constant_valid_p (inside_init,
7258 TREE_TYPE (inside_init)))
276beea2 7259 {
53de1631 7260 error_init (init_loc, "initializer element is not constant");
276beea2 7261 inside_init = error_mark_node;
ea5fb8f9 7262 }
a75b1c71 7263 else if (require_constant && !maybe_const)
aac24642 7264 pedwarn_init (init_loc, OPT_Wpedantic,
a75b1c71 7265 "initializer element is not a constant expression");
78b80426 7266
b86527d8 7267 /* Added to enable additional -Wsuggest-attribute=format warnings. */
c7eca986 7268 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
22a75734 7269 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7270 type, inside_init, origtype,
b9c74b4d 7271 ic_init, null_pointer_constant,
a75b1c71 7272 NULL_TREE, NULL_TREE, 0);
276beea2 7273 return inside_init;
7274 }
78b80426 7275
276beea2 7276 /* Handle scalar types, including conversions. */
628fa4f8 7277
9421ebb9 7278 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7279 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7280 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
628fa4f8 7281 {
e78cf9a7 7282 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7283 && (TREE_CODE (init) == STRING_CST
7284 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
e60a6f7b 7285 inside_init = init = array_to_pointer_conversion (init_loc, init);
c6418a4e 7286 if (semantic_type)
7287 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7288 inside_init);
276beea2 7289 inside_init
22a75734 7290 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7291 inside_init, origtype, ic_init,
7292 null_pointer_constant, NULL_TREE, NULL_TREE,
7293 0);
2b30d46c 7294
0d45e76f 7295 /* Check to see if we have already given an error message. */
7296 if (inside_init == error_mark_node)
7297 ;
84166705 7298 else if (require_constant && !TREE_CONSTANT (inside_init))
628fa4f8 7299 {
53de1631 7300 error_init (init_loc, "initializer element is not constant");
276beea2 7301 inside_init = error_mark_node;
628fa4f8 7302 }
276beea2 7303 else if (require_constant
fdf95cc9 7304 && !initializer_constant_valid_p (inside_init,
7305 TREE_TYPE (inside_init)))
628fa4f8 7306 {
53de1631 7307 error_init (init_loc, "initializer element is not computable at "
7308 "load time");
276beea2 7309 inside_init = error_mark_node;
628fa4f8 7310 }
a75b1c71 7311 else if (require_constant && !maybe_const)
8d669e79 7312 pedwarn_init (init_loc, OPT_Wpedantic,
a75b1c71 7313 "initializer element is not a constant expression");
276beea2 7314
7315 return inside_init;
628fa4f8 7316 }
bb94852b 7317
276beea2 7318 /* Come here only for records and arrays. */
bb94852b 7319
276beea2 7320 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
bb94852b 7321 {
53de1631 7322 error_init (init_loc, "variable-sized object may not be initialized");
276beea2 7323 return error_mark_node;
bb94852b 7324 }
276beea2 7325
53de1631 7326 error_init (init_loc, "invalid initializer");
276beea2 7327 return error_mark_node;
bb94852b 7328}
628fa4f8 7329\f
276beea2 7330/* Handle initializers that use braces. */
628fa4f8 7331
276beea2 7332/* Type of object we are accumulating a constructor for.
7333 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7334static tree constructor_type;
628fa4f8 7335
276beea2 7336/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7337 left to fill. */
7338static tree constructor_fields;
628fa4f8 7339
276beea2 7340/* For an ARRAY_TYPE, this is the specified index
7341 at which to store the next element we get. */
7342static tree constructor_index;
628fa4f8 7343
276beea2 7344/* For an ARRAY_TYPE, this is the maximum index. */
7345static tree constructor_max_index;
628fa4f8 7346
276beea2 7347/* For a RECORD_TYPE, this is the first field not yet written out. */
7348static tree constructor_unfilled_fields;
628fa4f8 7349
276beea2 7350/* For an ARRAY_TYPE, this is the index of the first element
7351 not yet written out. */
7352static tree constructor_unfilled_index;
3a77db2f 7353
276beea2 7354/* In a RECORD_TYPE, the byte index of the next consecutive field.
7355 This is so we can generate gaps between fields, when appropriate. */
7356static tree constructor_bit_index;
550e465e 7357
276beea2 7358/* If we are saving up the elements rather than allocating them,
7359 this is the list of elements so far (in reverse order,
7360 most recent first). */
f1f41a6c 7361static vec<constructor_elt, va_gc> *constructor_elements;
02643ebc 7362
276beea2 7363/* 1 if constructor should be incrementally stored into a constructor chain,
7364 0 if all the elements should be kept in AVL tree. */
7365static int constructor_incremental;
02643ebc 7366
276beea2 7367/* 1 if so far this constructor's elements are all compile-time constants. */
7368static int constructor_constant;
02643ebc 7369
276beea2 7370/* 1 if so far this constructor's elements are all valid address constants. */
7371static int constructor_simple;
02643ebc 7372
a75b1c71 7373/* 1 if this constructor has an element that cannot be part of a
7374 constant expression. */
7375static int constructor_nonconst;
7376
276beea2 7377/* 1 if this constructor is erroneous so far. */
7378static int constructor_erroneous;
f9e15121 7379
95cdf3fd 7380/* 1 if this constructor is the universal zero initializer { 0 }. */
7381static int constructor_zeroinit;
7382
276beea2 7383/* Structure for managing pending initializer elements, organized as an
7384 AVL tree. */
f9e15121 7385
276beea2 7386struct init_node
f9e15121 7387{
276beea2 7388 struct init_node *left, *right;
7389 struct init_node *parent;
7390 int balance;
7391 tree purpose;
7392 tree value;
b9c74b4d 7393 tree origtype;
f9e15121 7394};
7395
276beea2 7396/* Tree of pending elements at this constructor level.
7397 These are elements encountered out of order
7398 which belong at places we haven't reached yet in actually
7399 writing the output.
7400 Will never hold tree nodes across GC runs. */
7401static struct init_node *constructor_pending_elts;
f9e15121 7402
276beea2 7403/* The SPELLING_DEPTH of this constructor. */
7404static int constructor_depth;
f9e15121 7405
276beea2 7406/* DECL node for which an initializer is being read.
7407 0 means we are reading a constructor expression
7408 such as (struct foo) {...}. */
7409static tree constructor_decl;
f9e15121 7410
276beea2 7411/* Nonzero if this is an initializer for a top-level decl. */
7412static int constructor_top_level;
f9e15121 7413
276beea2 7414/* Nonzero if there were any member designators in this initializer. */
7415static int constructor_designated;
f9e15121 7416
276beea2 7417/* Nesting depth of designator list. */
7418static int designator_depth;
f9e15121 7419
276beea2 7420/* Nonzero if there were diagnosed errors in this designator list. */
ebe39827 7421static int designator_erroneous;
f9e15121 7422
276beea2 7423\f
7424/* This stack has a level for each implicit or explicit level of
7425 structuring in the initializer, including the outermost one. It
7426 saves the values of most of the variables above. */
f9e15121 7427
276beea2 7428struct constructor_range_stack;
7429
7430struct constructor_stack
f9e15121 7431{
276beea2 7432 struct constructor_stack *next;
7433 tree type;
7434 tree fields;
7435 tree index;
7436 tree max_index;
7437 tree unfilled_index;
7438 tree unfilled_fields;
7439 tree bit_index;
f1f41a6c 7440 vec<constructor_elt, va_gc> *elements;
276beea2 7441 struct init_node *pending_elts;
7442 int offset;
7443 int depth;
3ad5f027 7444 /* If value nonzero, this value should replace the entire
276beea2 7445 constructor at this level. */
3ad5f027 7446 struct c_expr replacement_value;
276beea2 7447 struct constructor_range_stack *range_stack;
7448 char constant;
7449 char simple;
a75b1c71 7450 char nonconst;
276beea2 7451 char implicit;
7452 char erroneous;
7453 char outer;
7454 char incremental;
7455 char designated;
74691f46 7456 int designator_depth;
276beea2 7457};
f9e15121 7458
4aa31b78 7459static struct constructor_stack *constructor_stack;
f9e15121 7460
276beea2 7461/* This stack represents designators from some range designator up to
7462 the last designator in the list. */
f9e15121 7463
276beea2 7464struct constructor_range_stack
7465{
7466 struct constructor_range_stack *next, *prev;
7467 struct constructor_stack *stack;
7468 tree range_start;
7469 tree index;
7470 tree range_end;
7471 tree fields;
7472};
f9e15121 7473
4aa31b78 7474static struct constructor_range_stack *constructor_range_stack;
f9e15121 7475
276beea2 7476/* This stack records separate initializers that are nested.
7477 Nested initializers can't happen in ANSI C, but GNU C allows them
7478 in cases like { ... (struct foo) { ... } ... }. */
f9e15121 7479
276beea2 7480struct initializer_stack
f9e15121 7481{
276beea2 7482 struct initializer_stack *next;
7483 tree decl;
276beea2 7484 struct constructor_stack *constructor_stack;
7485 struct constructor_range_stack *constructor_range_stack;
f1f41a6c 7486 vec<constructor_elt, va_gc> *elements;
276beea2 7487 struct spelling *spelling;
7488 struct spelling *spelling_base;
7489 int spelling_size;
7490 char top_level;
7491 char require_constant_value;
7492 char require_constant_elements;
7493};
f9e15121 7494
4aa31b78 7495static struct initializer_stack *initializer_stack;
276beea2 7496\f
7497/* Prepare to parse and output the initializer for variable DECL. */
628fa4f8 7498
7499void
d5406300 7500start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level)
628fa4f8 7501{
276beea2 7502 const char *locus;
a9c6c0e3 7503 struct initializer_stack *p = XNEW (struct initializer_stack);
628fa4f8 7504
276beea2 7505 p->decl = constructor_decl;
276beea2 7506 p->require_constant_value = require_constant_value;
7507 p->require_constant_elements = require_constant_elements;
7508 p->constructor_stack = constructor_stack;
7509 p->constructor_range_stack = constructor_range_stack;
7510 p->elements = constructor_elements;
7511 p->spelling = spelling;
7512 p->spelling_base = spelling_base;
7513 p->spelling_size = spelling_size;
7514 p->top_level = constructor_top_level;
7515 p->next = initializer_stack;
7516 initializer_stack = p;
628fa4f8 7517
276beea2 7518 constructor_decl = decl;
276beea2 7519 constructor_designated = 0;
7520 constructor_top_level = top_level;
628fa4f8 7521
61ba37bf 7522 if (decl != 0 && decl != error_mark_node)
276beea2 7523 {
7524 require_constant_value = TREE_STATIC (decl);
7525 require_constant_elements
7526 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7527 /* For a scalar, you can always use any value to initialize,
7528 even within braces. */
9e73638f 7529 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
d1dd9ac0 7530 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
276beea2 7531 }
7532 else
7533 {
7534 require_constant_value = 0;
7535 require_constant_elements = 0;
d1dd9ac0 7536 locus = _("(anonymous)");
276beea2 7537 }
89a5f6d1 7538
276beea2 7539 constructor_stack = 0;
7540 constructor_range_stack = 0;
89a5f6d1 7541
95cdf3fd 7542 found_missing_braces = 0;
276beea2 7543
7544 spelling_base = 0;
7545 spelling_size = 0;
7546 RESTORE_SPELLING_DEPTH (0);
7547
7548 if (locus)
7549 push_string (locus);
7550}
7551
7552void
7553finish_init (void)
89a5f6d1 7554{
276beea2 7555 struct initializer_stack *p = initializer_stack;
89a5f6d1 7556
276beea2 7557 /* Free the whole constructor stack of this initializer. */
7558 while (constructor_stack)
7559 {
7560 struct constructor_stack *q = constructor_stack;
7561 constructor_stack = q->next;
7562 free (q);
7563 }
7564
231bd014 7565 gcc_assert (!constructor_range_stack);
276beea2 7566
7567 /* Pop back to the data of the outer initializer (if any). */
dcd8fd01 7568 free (spelling_base);
a5d9b222 7569
276beea2 7570 constructor_decl = p->decl;
276beea2 7571 require_constant_value = p->require_constant_value;
7572 require_constant_elements = p->require_constant_elements;
7573 constructor_stack = p->constructor_stack;
7574 constructor_range_stack = p->constructor_range_stack;
7575 constructor_elements = p->elements;
7576 spelling = p->spelling;
7577 spelling_base = p->spelling_base;
7578 spelling_size = p->spelling_size;
7579 constructor_top_level = p->top_level;
7580 initializer_stack = p->next;
7581 free (p);
89a5f6d1 7582}
628fa4f8 7583\f
276beea2 7584/* Call here when we see the initializer is surrounded by braces.
7585 This is instead of a call to push_init_level;
7586 it is matched by a call to pop_init_level.
628fa4f8 7587
276beea2 7588 TYPE is the type to initialize, for a constructor expression.
7589 For an initializer for a decl, TYPE is zero. */
628fa4f8 7590
276beea2 7591void
7592really_start_incremental_init (tree type)
628fa4f8 7593{
9318f22c 7594 struct constructor_stack *p = XNEW (struct constructor_stack);
628fa4f8 7595
276beea2 7596 if (type == 0)
7597 type = TREE_TYPE (constructor_decl);
628fa4f8 7598
6290f0db 7599 if (VECTOR_TYPE_P (type)
8d125f7d 7600 && TYPE_VECTOR_OPAQUE (type))
276beea2 7601 error ("opaque vector types cannot be initialized");
628fa4f8 7602
276beea2 7603 p->type = constructor_type;
7604 p->fields = constructor_fields;
7605 p->index = constructor_index;
7606 p->max_index = constructor_max_index;
7607 p->unfilled_index = constructor_unfilled_index;
7608 p->unfilled_fields = constructor_unfilled_fields;
7609 p->bit_index = constructor_bit_index;
7610 p->elements = constructor_elements;
7611 p->constant = constructor_constant;
7612 p->simple = constructor_simple;
a75b1c71 7613 p->nonconst = constructor_nonconst;
276beea2 7614 p->erroneous = constructor_erroneous;
7615 p->pending_elts = constructor_pending_elts;
7616 p->depth = constructor_depth;
3ad5f027 7617 p->replacement_value.value = 0;
7618 p->replacement_value.original_code = ERROR_MARK;
841fdaa6 7619 p->replacement_value.original_type = NULL;
276beea2 7620 p->implicit = 0;
7621 p->range_stack = 0;
7622 p->outer = 0;
7623 p->incremental = constructor_incremental;
7624 p->designated = constructor_designated;
74691f46 7625 p->designator_depth = designator_depth;
276beea2 7626 p->next = 0;
7627 constructor_stack = p;
189495e1 7628
276beea2 7629 constructor_constant = 1;
7630 constructor_simple = 1;
a75b1c71 7631 constructor_nonconst = 0;
276beea2 7632 constructor_depth = SPELLING_DEPTH ();
f1f41a6c 7633 constructor_elements = NULL;
276beea2 7634 constructor_pending_elts = 0;
7635 constructor_type = type;
7636 constructor_incremental = 1;
7637 constructor_designated = 0;
95cdf3fd 7638 constructor_zeroinit = 1;
276beea2 7639 designator_depth = 0;
ebe39827 7640 designator_erroneous = 0;
628fa4f8 7641
ef61516b 7642 if (RECORD_OR_UNION_TYPE_P (constructor_type))
628fa4f8 7643 {
276beea2 7644 constructor_fields = TYPE_FIELDS (constructor_type);
7645 /* Skip any nameless bit fields at the beginning. */
7646 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7647 && DECL_NAME (constructor_fields) == 0)
1767a056 7648 constructor_fields = DECL_CHAIN (constructor_fields);
a0c2c45b 7649
276beea2 7650 constructor_unfilled_fields = constructor_fields;
7651 constructor_bit_index = bitsize_zero_node;
628fa4f8 7652 }
276beea2 7653 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7654 {
7655 if (TYPE_DOMAIN (constructor_type))
7656 {
7657 constructor_max_index
7658 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
628fa4f8 7659
276beea2 7660 /* Detect non-empty initializations of zero-length arrays. */
7661 if (constructor_max_index == NULL_TREE
7662 && TYPE_SIZE (constructor_type))
2512209b 7663 constructor_max_index = integer_minus_one_node;
628fa4f8 7664
276beea2 7665 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7666 to initialize VLAs will cause a proper error; avoid tree
7667 checking errors as well by setting a safe value. */
7668 if (constructor_max_index
7669 && TREE_CODE (constructor_max_index) != INTEGER_CST)
2512209b 7670 constructor_max_index = integer_minus_one_node;
4619fc15 7671
276beea2 7672 constructor_index
7673 = convert (bitsizetype,
7674 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
4619fc15 7675 }
276beea2 7676 else
fb213c85 7677 {
7678 constructor_index = bitsize_zero_node;
7679 constructor_max_index = NULL_TREE;
7680 }
4619fc15 7681
276beea2 7682 constructor_unfilled_index = constructor_index;
7683 }
6290f0db 7684 else if (VECTOR_TYPE_P (constructor_type))
276beea2 7685 {
7686 /* Vectors are like simple fixed-size arrays. */
7687 constructor_max_index =
ceb7b692 7688 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
4aeeff51 7689 constructor_index = bitsize_zero_node;
276beea2 7690 constructor_unfilled_index = constructor_index;
7691 }
7692 else
7693 {
7694 /* Handle the case of int x = {5}; */
7695 constructor_fields = constructor_type;
7696 constructor_unfilled_fields = constructor_type;
7697 }
7698}
7699\f
74a4140f 7700/* Called when we see an open brace for a nested initializer. Finish
7701 off any pending levels with implicit braces. */
7702void
7703finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
7704{
7705 while (constructor_stack->implicit)
7706 {
7707 if (RECORD_OR_UNION_TYPE_P (constructor_type)
7708 && constructor_fields == 0)
7709 process_init_element (input_location,
7710 pop_init_level (loc, 1, braced_init_obstack),
7711 true, braced_init_obstack);
7712 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
7713 && constructor_max_index
7714 && tree_int_cst_lt (constructor_max_index,
7715 constructor_index))
7716 process_init_element (input_location,
7717 pop_init_level (loc, 1, braced_init_obstack),
7718 true, braced_init_obstack);
7719 else
7720 break;
7721 }
7722}
7723
276beea2 7724/* Push down into a subobject, for initialization.
7725 If this is for an explicit set of braces, IMPLICIT is 0.
7726 If it is because the next element belongs at a lower level,
7727 IMPLICIT is 1 (or 2 if the push is because of designator list). */
628fa4f8 7728
276beea2 7729void
53de1631 7730push_init_level (location_t loc, int implicit,
7731 struct obstack *braced_init_obstack)
276beea2 7732{
7733 struct constructor_stack *p;
7734 tree value = NULL_TREE;
628fa4f8 7735
276beea2 7736 /* Unless this is an explicit brace, we need to preserve previous
7737 content if any. */
7738 if (implicit)
7739 {
ef61516b 7740 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
759791ee 7741 value = find_init_member (constructor_fields, braced_init_obstack);
276beea2 7742 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
759791ee 7743 value = find_init_member (constructor_index, braced_init_obstack);
628fa4f8 7744 }
7745
9318f22c 7746 p = XNEW (struct constructor_stack);
276beea2 7747 p->type = constructor_type;
7748 p->fields = constructor_fields;
7749 p->index = constructor_index;
7750 p->max_index = constructor_max_index;
7751 p->unfilled_index = constructor_unfilled_index;
7752 p->unfilled_fields = constructor_unfilled_fields;
7753 p->bit_index = constructor_bit_index;
7754 p->elements = constructor_elements;
7755 p->constant = constructor_constant;
7756 p->simple = constructor_simple;
a75b1c71 7757 p->nonconst = constructor_nonconst;
276beea2 7758 p->erroneous = constructor_erroneous;
7759 p->pending_elts = constructor_pending_elts;
7760 p->depth = constructor_depth;
3ad5f027 7761 p->replacement_value.value = 0;
7762 p->replacement_value.original_code = ERROR_MARK;
841fdaa6 7763 p->replacement_value.original_type = NULL;
276beea2 7764 p->implicit = implicit;
7765 p->outer = 0;
7766 p->incremental = constructor_incremental;
7767 p->designated = constructor_designated;
74691f46 7768 p->designator_depth = designator_depth;
276beea2 7769 p->next = constructor_stack;
7770 p->range_stack = 0;
7771 constructor_stack = p;
628fa4f8 7772
276beea2 7773 constructor_constant = 1;
7774 constructor_simple = 1;
a75b1c71 7775 constructor_nonconst = 0;
276beea2 7776 constructor_depth = SPELLING_DEPTH ();
f1f41a6c 7777 constructor_elements = NULL;
276beea2 7778 constructor_incremental = 1;
7779 constructor_designated = 0;
7780 constructor_pending_elts = 0;
7781 if (!implicit)
628fa4f8 7782 {
276beea2 7783 p->range_stack = constructor_range_stack;
7784 constructor_range_stack = 0;
7785 designator_depth = 0;
ebe39827 7786 designator_erroneous = 0;
276beea2 7787 }
628fa4f8 7788
276beea2 7789 /* Don't die if an entire brace-pair level is superfluous
7790 in the containing level. */
7791 if (constructor_type == 0)
7792 ;
ef61516b 7793 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 7794 {
7795 /* Don't die if there are extra init elts at the end. */
7796 if (constructor_fields == 0)
7797 constructor_type = 0;
7798 else
628fa4f8 7799 {
276beea2 7800 constructor_type = TREE_TYPE (constructor_fields);
7801 push_member_name (constructor_fields);
7802 constructor_depth++;
628fa4f8 7803 }
d44f2f7c 7804 /* If upper initializer is designated, then mark this as
7805 designated too to prevent bogus warnings. */
7806 constructor_designated = p->designated;
276beea2 7807 }
7808 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7809 {
7810 constructor_type = TREE_TYPE (constructor_type);
e913b5cd 7811 push_array_bounds (tree_to_uhwi (constructor_index));
276beea2 7812 constructor_depth++;
628fa4f8 7813 }
7814
276beea2 7815 if (constructor_type == 0)
628fa4f8 7816 {
53de1631 7817 error_init (loc, "extra brace group at end of initializer");
276beea2 7818 constructor_fields = 0;
7819 constructor_unfilled_fields = 0;
7820 return;
628fa4f8 7821 }
7822
276beea2 7823 if (value && TREE_CODE (value) == CONSTRUCTOR)
7824 {
7825 constructor_constant = TREE_CONSTANT (value);
7826 constructor_simple = TREE_STATIC (value);
a75b1c71 7827 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
276beea2 7828 constructor_elements = CONSTRUCTOR_ELTS (value);
f1f41a6c 7829 if (!vec_safe_is_empty (constructor_elements)
276beea2 7830 && (TREE_CODE (constructor_type) == RECORD_TYPE
7831 || TREE_CODE (constructor_type) == ARRAY_TYPE))
759791ee 7832 set_nonincremental_init (braced_init_obstack);
276beea2 7833 }
628fa4f8 7834
95cdf3fd 7835 if (implicit == 1)
7836 found_missing_braces = 1;
628fa4f8 7837
ef61516b 7838 if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 7839 {
7840 constructor_fields = TYPE_FIELDS (constructor_type);
7841 /* Skip any nameless bit fields at the beginning. */
7842 while (constructor_fields != 0 && DECL_C_BIT_FIELD (constructor_fields)
7843 && DECL_NAME (constructor_fields) == 0)
d059ef79 7844 constructor_fields = DECL_CHAIN (constructor_fields);
7de1d3ba 7845
276beea2 7846 constructor_unfilled_fields = constructor_fields;
7847 constructor_bit_index = bitsize_zero_node;
7848 }
6290f0db 7849 else if (VECTOR_TYPE_P (constructor_type))
276beea2 7850 {
7851 /* Vectors are like simple fixed-size arrays. */
7852 constructor_max_index =
ceb7b692 7853 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
7854 constructor_index = bitsize_int (0);
276beea2 7855 constructor_unfilled_index = constructor_index;
7856 }
7857 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
7858 {
7859 if (TYPE_DOMAIN (constructor_type))
7860 {
7861 constructor_max_index
7862 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
628fa4f8 7863
276beea2 7864 /* Detect non-empty initializations of zero-length arrays. */
7865 if (constructor_max_index == NULL_TREE
7866 && TYPE_SIZE (constructor_type))
2512209b 7867 constructor_max_index = integer_minus_one_node;
fc698c9d 7868
276beea2 7869 /* constructor_max_index needs to be an INTEGER_CST. Attempts
7870 to initialize VLAs will cause a proper error; avoid tree
7871 checking errors as well by setting a safe value. */
7872 if (constructor_max_index
7873 && TREE_CODE (constructor_max_index) != INTEGER_CST)
2512209b 7874 constructor_max_index = integer_minus_one_node;
554732cd 7875
276beea2 7876 constructor_index
7877 = convert (bitsizetype,
7878 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
7879 }
7880 else
7881 constructor_index = bitsize_zero_node;
fc698c9d 7882
276beea2 7883 constructor_unfilled_index = constructor_index;
7884 if (value && TREE_CODE (value) == STRING_CST)
7885 {
7886 /* We need to split the char/wchar array into individual
7887 characters, so that we don't have to special case it
7888 everywhere. */
759791ee 7889 set_nonincremental_init_from_string (value, braced_init_obstack);
276beea2 7890 }
7891 }
7892 else
7893 {
b765c734 7894 if (constructor_type != error_mark_node)
d44f2f7c 7895 warning_init (input_location, 0, "braces around scalar initializer");
276beea2 7896 constructor_fields = constructor_type;
7897 constructor_unfilled_fields = constructor_type;
7898 }
7899}
989bbcfc 7900
276beea2 7901/* At the end of an implicit or explicit brace level,
3ad5f027 7902 finish up that level of constructor. If a single expression
7903 with redundant braces initialized that level, return the
7904 c_expr structure for that expression. Otherwise, the original_code
7905 element is set to ERROR_MARK.
7906 If we were outputting the elements as they are read, return 0 as the value
276beea2 7907 from inner levels (process_init_element ignores that),
3ad5f027 7908 but return error_mark_node as the value from the outermost level
276beea2 7909 (that's what we want to put in DECL_INITIAL).
3ad5f027 7910 Otherwise, return a CONSTRUCTOR expression as the value. */
fc698c9d 7911
3ad5f027 7912struct c_expr
53de1631 7913pop_init_level (location_t loc, int implicit,
7914 struct obstack *braced_init_obstack)
276beea2 7915{
7916 struct constructor_stack *p;
3ad5f027 7917 struct c_expr ret;
7918 ret.value = 0;
7919 ret.original_code = ERROR_MARK;
841fdaa6 7920 ret.original_type = NULL;
fc698c9d 7921
276beea2 7922 if (implicit == 0)
7923 {
7924 /* When we come to an explicit close brace,
7925 pop any inner levels that didn't have explicit braces. */
7926 while (constructor_stack->implicit)
adc93f31 7927 process_init_element (input_location,
53de1631 7928 pop_init_level (loc, 1, braced_init_obstack),
adc93f31 7929 true, braced_init_obstack);
231bd014 7930 gcc_assert (!constructor_range_stack);
276beea2 7931 }
997d68fe 7932
36e0475c 7933 /* Now output all pending elements. */
7934 constructor_incremental = 1;
759791ee 7935 output_pending_init_elements (1, braced_init_obstack);
36e0475c 7936
276beea2 7937 p = constructor_stack;
997d68fe 7938
276beea2 7939 /* Error for initializing a flexible array member, or a zero-length
7940 array member in an inappropriate context. */
7941 if (constructor_type && constructor_fields
7942 && TREE_CODE (constructor_type) == ARRAY_TYPE
7943 && TYPE_DOMAIN (constructor_type)
84166705 7944 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
276beea2 7945 {
7946 /* Silently discard empty initializations. The parser will
7947 already have pedwarned for empty brackets. */
7948 if (integer_zerop (constructor_unfilled_index))
7949 constructor_type = NULL_TREE;
231bd014 7950 else
276beea2 7951 {
231bd014 7952 gcc_assert (!TYPE_SIZE (constructor_type));
a0c938f0 7953
276beea2 7954 if (constructor_depth > 2)
53de1631 7955 error_init (loc, "initialization of flexible array member in a nested context");
8864917d 7956 else
c9e51d25 7957 pedwarn_init (loc, OPT_Wpedantic,
21ca8540 7958 "initialization of a flexible array member");
fc698c9d 7959
276beea2 7960 /* We have already issued an error message for the existence
7961 of a flexible array member not at the end of the structure.
89f18f73 7962 Discard the initializer so that we do not die later. */
1767a056 7963 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
276beea2 7964 constructor_type = NULL_TREE;
7965 }
276beea2 7966 }
fc698c9d 7967
70279be9 7968 switch (vec_safe_length (constructor_elements))
7969 {
7970 case 0:
7971 /* Initialization with { } counts as zeroinit. */
7972 constructor_zeroinit = 1;
7973 break;
7974 case 1:
7975 /* This might be zeroinit as well. */
7976 if (integer_zerop ((*constructor_elements)[0].value))
7977 constructor_zeroinit = 1;
7978 break;
7979 default:
7980 /* If the constructor has more than one element, it can't be { 0 }. */
7981 constructor_zeroinit = 0;
7982 break;
7983 }
95cdf3fd 7984
7985 /* Warn when some structs are initialized with direct aggregation. */
7986 if (!implicit && found_missing_braces && warn_missing_braces
7987 && !constructor_zeroinit)
70279be9 7988 warning_init (loc, OPT_Wmissing_braces,
7989 "missing braces around initializer");
95cdf3fd 7990
276beea2 7991 /* Warn when some struct elements are implicitly initialized to zero. */
3304fb1c 7992 if (warn_missing_field_initializers
276beea2 7993 && constructor_type
7994 && TREE_CODE (constructor_type) == RECORD_TYPE
7995 && constructor_unfilled_fields)
7996 {
7997 /* Do not warn for flexible array members or zero-length arrays. */
7998 while (constructor_unfilled_fields
84166705 7999 && (!DECL_SIZE (constructor_unfilled_fields)
276beea2 8000 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
1767a056 8001 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
1955ae11 8002
ac4e675f 8003 if (constructor_unfilled_fields
8004 /* Do not warn if this level of the initializer uses member
8005 designators; it is likely to be deliberate. */
8006 && !constructor_designated
61a892dc 8007 /* Do not warn about initializing with { 0 } or with { }. */
ac4e675f 8008 && !constructor_zeroinit)
276beea2 8009 {
292bb106 8010 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8011 "missing initializer for field %qD of %qT",
8012 constructor_unfilled_fields,
8013 constructor_type))
8014 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
b0ec32b9 8015 "%qD declared here", constructor_unfilled_fields);
276beea2 8016 }
8017 }
fc698c9d 8018
276beea2 8019 /* Pad out the end of the structure. */
3ad5f027 8020 if (p->replacement_value.value)
276beea2 8021 /* If this closes a superfluous brace pair,
8022 just pass out the element between them. */
3ad5f027 8023 ret = p->replacement_value;
276beea2 8024 else if (constructor_type == 0)
8025 ;
ef61516b 8026 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
276beea2 8027 && TREE_CODE (constructor_type) != ARRAY_TYPE
6290f0db 8028 && !VECTOR_TYPE_P (constructor_type))
276beea2 8029 {
8030 /* A nonincremental scalar initializer--just return
8031 the element, after verifying there is just one. */
f1f41a6c 8032 if (vec_safe_is_empty (constructor_elements))
276beea2 8033 {
8034 if (!constructor_erroneous)
53de1631 8035 error_init (loc, "empty scalar initializer");
3ad5f027 8036 ret.value = error_mark_node;
276beea2 8037 }
f1f41a6c 8038 else if (vec_safe_length (constructor_elements) != 1)
276beea2 8039 {
53de1631 8040 error_init (loc, "extra elements in scalar initializer");
f1f41a6c 8041 ret.value = (*constructor_elements)[0].value;
276beea2 8042 }
8043 else
f1f41a6c 8044 ret.value = (*constructor_elements)[0].value;
276beea2 8045 }
8046 else
8047 {
8048 if (constructor_erroneous)
3ad5f027 8049 ret.value = error_mark_node;
276beea2 8050 else
8051 {
3ad5f027 8052 ret.value = build_constructor (constructor_type,
c75b4594 8053 constructor_elements);
276beea2 8054 if (constructor_constant)
c7d4e749 8055 TREE_CONSTANT (ret.value) = 1;
276beea2 8056 if (constructor_constant && constructor_simple)
3ad5f027 8057 TREE_STATIC (ret.value) = 1;
a75b1c71 8058 if (constructor_nonconst)
8059 CONSTRUCTOR_NON_CONST (ret.value) = 1;
276beea2 8060 }
8061 }
fc698c9d 8062
a75b1c71 8063 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8064 {
8065 if (constructor_nonconst)
8066 ret.original_code = C_MAYBE_CONST_EXPR;
8067 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8068 ret.original_code = ERROR_MARK;
8069 }
8070
276beea2 8071 constructor_type = p->type;
8072 constructor_fields = p->fields;
8073 constructor_index = p->index;
8074 constructor_max_index = p->max_index;
8075 constructor_unfilled_index = p->unfilled_index;
8076 constructor_unfilled_fields = p->unfilled_fields;
8077 constructor_bit_index = p->bit_index;
8078 constructor_elements = p->elements;
8079 constructor_constant = p->constant;
8080 constructor_simple = p->simple;
a75b1c71 8081 constructor_nonconst = p->nonconst;
276beea2 8082 constructor_erroneous = p->erroneous;
8083 constructor_incremental = p->incremental;
8084 constructor_designated = p->designated;
74691f46 8085 designator_depth = p->designator_depth;
276beea2 8086 constructor_pending_elts = p->pending_elts;
8087 constructor_depth = p->depth;
8088 if (!p->implicit)
8089 constructor_range_stack = p->range_stack;
8090 RESTORE_SPELLING_DEPTH (constructor_depth);
fc698c9d 8091
276beea2 8092 constructor_stack = p->next;
8093 free (p);
52136990 8094
09d2b71a 8095 if (ret.value == 0 && constructor_stack == 0)
8096 ret.value = error_mark_node;
3ad5f027 8097 return ret;
276beea2 8098}
989bbcfc 8099
276beea2 8100/* Common handling for both array range and field name designators.
8101 ARRAY argument is nonzero for array ranges. Returns zero for success. */
628fa4f8 8102
276beea2 8103static int
53de1631 8104set_designator (location_t loc, int array,
8105 struct obstack *braced_init_obstack)
fc698c9d 8106{
276beea2 8107 tree subtype;
8108 enum tree_code subcode;
fc698c9d 8109
276beea2 8110 /* Don't die if an entire brace-pair level is superfluous
8111 in the containing level. */
8112 if (constructor_type == 0)
8113 return 1;
fc698c9d 8114
231bd014 8115 /* If there were errors in this designator list already, bail out
8116 silently. */
ebe39827 8117 if (designator_erroneous)
276beea2 8118 return 1;
0583de9c 8119
276beea2 8120 if (!designator_depth)
8121 {
231bd014 8122 gcc_assert (!constructor_range_stack);
fc698c9d 8123
276beea2 8124 /* Designator list starts at the level of closest explicit
8125 braces. */
8126 while (constructor_stack->implicit)
adc93f31 8127 process_init_element (input_location,
53de1631 8128 pop_init_level (loc, 1, braced_init_obstack),
adc93f31 8129 true, braced_init_obstack);
276beea2 8130 constructor_designated = 1;
8131 return 0;
8132 }
fc698c9d 8133
231bd014 8134 switch (TREE_CODE (constructor_type))
f1c75cb5 8135 {
231bd014 8136 case RECORD_TYPE:
8137 case UNION_TYPE:
276beea2 8138 subtype = TREE_TYPE (constructor_fields);
8139 if (subtype != error_mark_node)
8140 subtype = TYPE_MAIN_VARIANT (subtype);
231bd014 8141 break;
8142 case ARRAY_TYPE:
276beea2 8143 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
231bd014 8144 break;
8145 default:
8146 gcc_unreachable ();
fc698c9d 8147 }
628fa4f8 8148
276beea2 8149 subcode = TREE_CODE (subtype);
8150 if (array && subcode != ARRAY_TYPE)
8151 {
53de1631 8152 error_init (loc, "array index in non-array initializer");
276beea2 8153 return 1;
8154 }
8155 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8156 {
53de1631 8157 error_init (loc, "field name not in record or union initializer");
276beea2 8158 return 1;
8159 }
f9e15121 8160
276beea2 8161 constructor_designated = 1;
74a4140f 8162 finish_implicit_inits (loc, braced_init_obstack);
53de1631 8163 push_init_level (loc, 2, braced_init_obstack);
276beea2 8164 return 0;
fc698c9d 8165}
628fa4f8 8166
276beea2 8167/* If there are range designators in designator list, push a new designator
8168 to constructor_range_stack. RANGE_END is end of such stack range or
8169 NULL_TREE if there is no range designator at this level. */
628fa4f8 8170
276beea2 8171static void
759791ee 8172push_range_stack (tree range_end, struct obstack * braced_init_obstack)
276beea2 8173{
8174 struct constructor_range_stack *p;
628fa4f8 8175
759791ee 8176 p = (struct constructor_range_stack *)
8177 obstack_alloc (braced_init_obstack,
8178 sizeof (struct constructor_range_stack));
276beea2 8179 p->prev = constructor_range_stack;
8180 p->next = 0;
8181 p->fields = constructor_fields;
8182 p->range_start = constructor_index;
8183 p->index = constructor_index;
8184 p->stack = constructor_stack;
8185 p->range_end = range_end;
989bbcfc 8186 if (constructor_range_stack)
276beea2 8187 constructor_range_stack->next = p;
8188 constructor_range_stack = p;
fc698c9d 8189}
628fa4f8 8190
276beea2 8191/* Within an array initializer, specify the next index to be initialized.
8192 FIRST is that index. If LAST is nonzero, then initialize a range
8193 of indices, running from FIRST through LAST. */
ca9c4c86 8194
fc698c9d 8195void
53de1631 8196set_init_index (location_t loc, tree first, tree last,
c9e51d25 8197 struct obstack *braced_init_obstack)
fc698c9d 8198{
53de1631 8199 if (set_designator (loc, 1, braced_init_obstack))
276beea2 8200 return;
fc698c9d 8201
ebe39827 8202 designator_erroneous = 1;
fc698c9d 8203
a250ed2e 8204 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8205 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8206 {
53de1631 8207 error_init (loc, "array index in initializer not of integer type");
a250ed2e 8208 return;
8209 }
8210
88c32270 8211 if (TREE_CODE (first) != INTEGER_CST)
8212 {
8213 first = c_fully_fold (first, false, NULL);
8214 if (TREE_CODE (first) == INTEGER_CST)
c9e51d25 8215 pedwarn_init (loc, OPT_Wpedantic,
88c32270 8216 "array index in initializer is not "
8217 "an integer constant expression");
8218 }
8219
8220 if (last && TREE_CODE (last) != INTEGER_CST)
8221 {
8222 last = c_fully_fold (last, false, NULL);
8223 if (TREE_CODE (last) == INTEGER_CST)
c9e51d25 8224 pedwarn_init (loc, OPT_Wpedantic,
88c32270 8225 "array index in initializer is not "
8226 "an integer constant expression");
8227 }
8228
276beea2 8229 if (TREE_CODE (first) != INTEGER_CST)
53de1631 8230 error_init (loc, "nonconstant array index in initializer");
276beea2 8231 else if (last != 0 && TREE_CODE (last) != INTEGER_CST)
53de1631 8232 error_init (loc, "nonconstant array index in initializer");
276beea2 8233 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
53de1631 8234 error_init (loc, "array index in non-array initializer");
42b1a257 8235 else if (tree_int_cst_sgn (first) == -1)
53de1631 8236 error_init (loc, "array index in initializer exceeds array bounds");
276beea2 8237 else if (constructor_max_index
8238 && tree_int_cst_lt (constructor_max_index, first))
53de1631 8239 error_init (loc, "array index in initializer exceeds array bounds");
276beea2 8240 else
fc698c9d 8241 {
a75b1c71 8242 constant_expression_warning (first);
8243 if (last)
8244 constant_expression_warning (last);
276beea2 8245 constructor_index = convert (bitsizetype, first);
f5298614 8246 if (tree_int_cst_lt (constructor_index, first))
8247 {
8248 constructor_index = copy_node (constructor_index);
8249 TREE_OVERFLOW (constructor_index) = 1;
8250 }
5d844ba2 8251
276beea2 8252 if (last)
886de5de 8253 {
276beea2 8254 if (tree_int_cst_equal (first, last))
8255 last = 0;
8256 else if (tree_int_cst_lt (last, first))
8257 {
53de1631 8258 error_init (loc, "empty index range in initializer");
276beea2 8259 last = 0;
8260 }
8261 else
8262 {
8263 last = convert (bitsizetype, last);
8264 if (constructor_max_index != 0
8265 && tree_int_cst_lt (constructor_max_index, last))
8266 {
53de1631 8267 error_init (loc, "array index range in initializer exceeds "
8268 "array bounds");
276beea2 8269 last = 0;
8270 }
8271 }
886de5de 8272 }
902de8ed 8273
276beea2 8274 designator_depth++;
ebe39827 8275 designator_erroneous = 0;
276beea2 8276 if (constructor_range_stack || last)
759791ee 8277 push_range_stack (last, braced_init_obstack);
fc698c9d 8278 }
fc698c9d 8279}
276beea2 8280
8281/* Within a struct initializer, specify the next field to be initialized. */
628fa4f8 8282
fc698c9d 8283void
527790c1 8284set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
53de1631 8285 struct obstack *braced_init_obstack)
fc698c9d 8286{
10064fb9 8287 tree field;
d354d271 8288
53de1631 8289 if (set_designator (loc, 0, braced_init_obstack))
276beea2 8290 return;
8291
ebe39827 8292 designator_erroneous = 1;
276beea2 8293
ef61516b 8294 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
d354d271 8295 {
53de1631 8296 error_init (loc, "field name not in record or union initializer");
276beea2 8297 return;
d354d271 8298 }
8299
10064fb9 8300 field = lookup_field (constructor_type, fieldname);
989bbcfc 8301
10064fb9 8302 if (field == 0)
527790c1 8303 {
8304 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8305 if (guessed_id)
8306 {
2a990300 8307 gcc_rich_location rich_loc (fieldname_loc);
8308 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
527790c1 8309 error_at_rich_loc
8310 (&rich_loc,
8311 "%qT has no member named %qE; did you mean %qE?",
8312 constructor_type, fieldname, guessed_id);
8313 }
8314 else
8315 error_at (fieldname_loc, "%qT has no member named %qE",
8316 constructor_type, fieldname);
8317 }
276beea2 8318 else
10064fb9 8319 do
8320 {
8321 constructor_fields = TREE_VALUE (field);
8322 designator_depth++;
8323 designator_erroneous = 0;
8324 if (constructor_range_stack)
8325 push_range_stack (NULL_TREE, braced_init_obstack);
8326 field = TREE_CHAIN (field);
8327 if (field)
8328 {
53de1631 8329 if (set_designator (loc, 0, braced_init_obstack))
10064fb9 8330 return;
8331 }
8332 }
8333 while (field != NULL_TREE);
276beea2 8334}
8335\f
8336/* Add a new initializer to the tree of pending initializers. PURPOSE
8337 identifies the initializer, either array index or field in a structure.
b9c74b4d 8338 VALUE is the value of that index or field. If ORIGTYPE is not
8339 NULL_TREE, it is the original type of VALUE.
d241bf0d 8340
8341 IMPLICIT is true if value comes from pop_init_level (1),
8342 the new initializer has been merged with the existing one
8343 and thus no warnings should be emitted about overriding an
8344 existing initializer. */
fc698c9d 8345
276beea2 8346static void
d44f2f7c 8347add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8348 bool implicit, struct obstack *braced_init_obstack)
276beea2 8349{
8350 struct init_node *p, **q, *r;
8351
8352 q = &constructor_pending_elts;
8353 p = 0;
8354
8355 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
fc698c9d 8356 {
276beea2 8357 while (*q != 0)
812fff1a 8358 {
276beea2 8359 p = *q;
8360 if (tree_int_cst_lt (purpose, p->purpose))
8361 q = &p->left;
8362 else if (tree_int_cst_lt (p->purpose, purpose))
8363 q = &p->right;
8364 else
8365 {
d241bf0d 8366 if (!implicit)
8367 {
8368 if (TREE_SIDE_EFFECTS (p->value))
dc5945dc 8369 warning_init (loc, OPT_Woverride_init_side_effects,
d44f2f7c 8370 "initialized field with side-effects "
8371 "overwritten");
d241bf0d 8372 else if (warn_override_init)
d44f2f7c 8373 warning_init (loc, OPT_Woverride_init,
8374 "initialized field overwritten");
d241bf0d 8375 }
276beea2 8376 p->value = value;
b9c74b4d 8377 p->origtype = origtype;
276beea2 8378 return;
8379 }
812fff1a 8380 }
fc698c9d 8381 }
276beea2 8382 else
fc698c9d 8383 {
276beea2 8384 tree bitpos;
628fa4f8 8385
276beea2 8386 bitpos = bit_position (purpose);
8387 while (*q != NULL)
8388 {
8389 p = *q;
8390 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8391 q = &p->left;
8392 else if (p->purpose != purpose)
8393 q = &p->right;
8394 else
8395 {
d241bf0d 8396 if (!implicit)
8397 {
8398 if (TREE_SIDE_EFFECTS (p->value))
dc5945dc 8399 warning_init (loc, OPT_Woverride_init_side_effects,
d44f2f7c 8400 "initialized field with side-effects "
8401 "overwritten");
d241bf0d 8402 else if (warn_override_init)
d44f2f7c 8403 warning_init (loc, OPT_Woverride_init,
8404 "initialized field overwritten");
d241bf0d 8405 }
276beea2 8406 p->value = value;
b9c74b4d 8407 p->origtype = origtype;
276beea2 8408 return;
8409 }
8410 }
812fff1a 8411 }
89a5f6d1 8412
759791ee 8413 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8414 sizeof (struct init_node));
276beea2 8415 r->purpose = purpose;
8416 r->value = value;
b9c74b4d 8417 r->origtype = origtype;
989bbcfc 8418
276beea2 8419 *q = r;
8420 r->parent = p;
8421 r->left = 0;
8422 r->right = 0;
8423 r->balance = 0;
89a5f6d1 8424
276beea2 8425 while (p)
fc698c9d 8426 {
276beea2 8427 struct init_node *s;
5d844ba2 8428
276beea2 8429 if (r == p->left)
886de5de 8430 {
276beea2 8431 if (p->balance == 0)
8432 p->balance = -1;
8433 else if (p->balance < 0)
8434 {
8435 if (r->balance < 0)
8436 {
8437 /* L rotation. */
8438 p->left = r->right;
8439 if (p->left)
8440 p->left->parent = p;
8441 r->right = p;
26877a3f 8442
276beea2 8443 p->balance = 0;
8444 r->balance = 0;
c052c0ba 8445
276beea2 8446 s = p->parent;
8447 p->parent = r;
8448 r->parent = s;
8449 if (s)
8450 {
8451 if (s->left == p)
8452 s->left = r;
8453 else
8454 s->right = r;
8455 }
8456 else
8457 constructor_pending_elts = r;
8458 }
8459 else
8460 {
8461 /* LR rotation. */
8462 struct init_node *t = r->right;
26877a3f 8463
276beea2 8464 r->right = t->left;
8465 if (r->right)
8466 r->right->parent = r;
8467 t->left = r;
8468
8469 p->left = t->right;
8470 if (p->left)
8471 p->left->parent = p;
8472 t->right = p;
8473
8474 p->balance = t->balance < 0;
8475 r->balance = -(t->balance > 0);
8476 t->balance = 0;
8477
8478 s = p->parent;
8479 p->parent = t;
8480 r->parent = t;
8481 t->parent = s;
8482 if (s)
8483 {
8484 if (s->left == p)
8485 s->left = t;
8486 else
8487 s->right = t;
8488 }
8489 else
8490 constructor_pending_elts = t;
8491 }
8492 break;
8493 }
8494 else
8495 {
8496 /* p->balance == +1; growth of left side balances the node. */
8497 p->balance = 0;
8498 break;
8499 }
886de5de 8500 }
276beea2 8501 else /* r == p->right */
8502 {
8503 if (p->balance == 0)
8504 /* Growth propagation from right side. */
8505 p->balance++;
8506 else if (p->balance > 0)
8507 {
8508 if (r->balance > 0)
8509 {
8510 /* R rotation. */
8511 p->right = r->left;
8512 if (p->right)
8513 p->right->parent = p;
8514 r->left = p;
8515
8516 p->balance = 0;
8517 r->balance = 0;
8518
8519 s = p->parent;
8520 p->parent = r;
8521 r->parent = s;
8522 if (s)
8523 {
8524 if (s->left == p)
8525 s->left = r;
8526 else
8527 s->right = r;
8528 }
8529 else
8530 constructor_pending_elts = r;
8531 }
8532 else /* r->balance == -1 */
8533 {
8534 /* RL rotation */
8535 struct init_node *t = r->left;
8536
8537 r->left = t->right;
8538 if (r->left)
8539 r->left->parent = r;
8540 t->right = r;
8541
8542 p->right = t->left;
8543 if (p->right)
8544 p->right->parent = p;
8545 t->left = p;
8546
8547 r->balance = (t->balance < 0);
8548 p->balance = -(t->balance > 0);
8549 t->balance = 0;
8550
8551 s = p->parent;
8552 p->parent = t;
8553 r->parent = t;
8554 t->parent = s;
8555 if (s)
8556 {
8557 if (s->left == p)
8558 s->left = t;
8559 else
8560 s->right = t;
8561 }
8562 else
8563 constructor_pending_elts = t;
8564 }
8565 break;
8566 }
8567 else
8568 {
8569 /* p->balance == -1; growth of right side balances the node. */
8570 p->balance = 0;
8571 break;
8572 }
8573 }
8574
8575 r = p;
8576 p = p->parent;
8577 }
8578}
8579
8580/* Build AVL tree from a sorted chain. */
8581
8582static void
759791ee 8583set_nonincremental_init (struct obstack * braced_init_obstack)
276beea2 8584{
c75b4594 8585 unsigned HOST_WIDE_INT ix;
8586 tree index, value;
276beea2 8587
8588 if (TREE_CODE (constructor_type) != RECORD_TYPE
8589 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8590 return;
8591
c75b4594 8592 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
d44f2f7c 8593 add_pending_init (input_location, index, value, NULL_TREE, true,
8594 braced_init_obstack);
f1f41a6c 8595 constructor_elements = NULL;
276beea2 8596 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8597 {
8598 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
8599 /* Skip any nameless bit fields at the beginning. */
8600 while (constructor_unfilled_fields != 0
8601 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8602 && DECL_NAME (constructor_unfilled_fields) == 0)
8603 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
902de8ed 8604
fc698c9d 8605 }
276beea2 8606 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
fc698c9d 8607 {
276beea2 8608 if (TYPE_DOMAIN (constructor_type))
8609 constructor_unfilled_index
8610 = convert (bitsizetype,
8611 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8612 else
8613 constructor_unfilled_index = bitsize_zero_node;
fc698c9d 8614 }
276beea2 8615 constructor_incremental = 0;
fc698c9d 8616}
628fa4f8 8617
276beea2 8618/* Build AVL tree from a string constant. */
fc698c9d 8619
276beea2 8620static void
759791ee 8621set_nonincremental_init_from_string (tree str,
8622 struct obstack * braced_init_obstack)
fc698c9d 8623{
276beea2 8624 tree value, purpose, type;
8625 HOST_WIDE_INT val[2];
8626 const char *p, *end;
8627 int byte, wchar_bytes, charwidth, bitpos;
fc698c9d 8628
231bd014 8629 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
b9fc4354 8630
b0726616 8631 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
276beea2 8632 charwidth = TYPE_PRECISION (char_type_node);
7175bb2a 8633 gcc_assert ((size_t) wchar_bytes * charwidth
8634 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
276beea2 8635 type = TREE_TYPE (constructor_type);
8636 p = TREE_STRING_POINTER (str);
8637 end = p + TREE_STRING_LENGTH (str);
812fff1a 8638
276beea2 8639 for (purpose = bitsize_zero_node;
54550ecb 8640 p < end
8641 && !(constructor_max_index
8642 && tree_int_cst_lt (constructor_max_index, purpose));
276beea2 8643 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
15119d83 8644 {
276beea2 8645 if (wchar_bytes == 1)
21c8999d 8646 {
e913b5cd 8647 val[0] = (unsigned char) *p++;
8648 val[1] = 0;
21c8999d 8649 }
8650 else
276beea2 8651 {
276beea2 8652 val[1] = 0;
e913b5cd 8653 val[0] = 0;
276beea2 8654 for (byte = 0; byte < wchar_bytes; byte++)
8655 {
8656 if (BYTES_BIG_ENDIAN)
8657 bitpos = (wchar_bytes - byte - 1) * charwidth;
8658 else
8659 bitpos = byte * charwidth;
7175bb2a 8660 val[bitpos / HOST_BITS_PER_WIDE_INT]
276beea2 8661 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
8662 << (bitpos % HOST_BITS_PER_WIDE_INT);
8663 }
8664 }
15119d83 8665
78a8ed03 8666 if (!TYPE_UNSIGNED (type))
276beea2 8667 {
8668 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
8669 if (bitpos < HOST_BITS_PER_WIDE_INT)
8670 {
59c1507a 8671 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
276beea2 8672 {
85f5e2ee 8673 val[0] |= HOST_WIDE_INT_M1U << bitpos;
e913b5cd 8674 val[1] = -1;
276beea2 8675 }
8676 }
8677 else if (bitpos == HOST_BITS_PER_WIDE_INT)
8678 {
e913b5cd 8679 if (val[0] < 0)
8680 val[1] = -1;
276beea2 8681 }
59c1507a 8682 else if (val[1] & (HOST_WIDE_INT_1
276beea2 8683 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
85f5e2ee 8684 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
276beea2 8685 }
21c8999d 8686
ddb1be65 8687 value = wide_int_to_tree (type,
8688 wide_int::from_array (val, 2,
e913b5cd 8689 HOST_BITS_PER_WIDE_INT * 2));
d44f2f7c 8690 add_pending_init (input_location, purpose, value, NULL_TREE, true,
759791ee 8691 braced_init_obstack);
03632119 8692 }
8693
276beea2 8694 constructor_incremental = 0;
8695}
fc698c9d 8696
276beea2 8697/* Return value of FIELD in pending initializer or zero if the field was
8698 not initialized yet. */
8699
8700static tree
759791ee 8701find_init_member (tree field, struct obstack * braced_init_obstack)
276beea2 8702{
8703 struct init_node *p;
8704
8705 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
37c005bb 8706 {
276beea2 8707 if (constructor_incremental
8708 && tree_int_cst_lt (field, constructor_unfilled_index))
759791ee 8709 set_nonincremental_init (braced_init_obstack);
276beea2 8710
8711 p = constructor_pending_elts;
8712 while (p)
37c005bb 8713 {
276beea2 8714 if (tree_int_cst_lt (field, p->purpose))
8715 p = p->left;
8716 else if (tree_int_cst_lt (p->purpose, field))
8717 p = p->right;
8718 else
8719 return p->value;
37c005bb 8720 }
37c005bb 8721 }
276beea2 8722 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
fc698c9d 8723 {
276beea2 8724 tree bitpos = bit_position (field);
fc698c9d 8725
276beea2 8726 if (constructor_incremental
8727 && (!constructor_unfilled_fields
8728 || tree_int_cst_lt (bitpos,
8729 bit_position (constructor_unfilled_fields))))
759791ee 8730 set_nonincremental_init (braced_init_obstack);
fc698c9d 8731
276beea2 8732 p = constructor_pending_elts;
8733 while (p)
8734 {
8735 if (field == p->purpose)
8736 return p->value;
8737 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8738 p = p->left;
8739 else
8740 p = p->right;
8741 }
8742 }
8743 else if (TREE_CODE (constructor_type) == UNION_TYPE)
fc698c9d 8744 {
f1f41a6c 8745 if (!vec_safe_is_empty (constructor_elements)
8746 && (constructor_elements->last ().index == field))
8747 return constructor_elements->last ().value;
fc698c9d 8748 }
276beea2 8749 return 0;
fc698c9d 8750}
8751
276beea2 8752/* "Output" the next constructor element.
8753 At top level, really output it to assembler code now.
8754 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
b9c74b4d 8755 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
276beea2 8756 TYPE is the data type that the containing data type wants here.
8757 FIELD is the field (a FIELD_DECL) or the index that this element fills.
3ad5f027 8758 If VALUE is a string constant, STRICT_STRING is true if it is
8759 unparenthesized or we should not warn here for it being parenthesized.
8760 For other types of VALUE, STRICT_STRING is not used.
989bbcfc 8761
276beea2 8762 PENDING if non-nil means output pending elements that belong
8763 right after this element. (PENDING is normally 1;
d241bf0d 8764 it is 0 while outputting pending elements, to avoid recursion.)
8765
8766 IMPLICIT is true if value comes from pop_init_level (1),
8767 the new initializer has been merged with the existing one
8768 and thus no warnings should be emitted about overriding an
8769 existing initializer. */
989bbcfc 8770
276beea2 8771static void
adc93f31 8772output_init_element (location_t loc, tree value, tree origtype,
8773 bool strict_string, tree type, tree field, int pending,
8774 bool implicit, struct obstack * braced_init_obstack)
276beea2 8775{
c6418a4e 8776 tree semantic_type = NULL_TREE;
a75b1c71 8777 bool maybe_const = true;
8778 bool npc;
c75b4594 8779
081b8c23 8780 if (type == error_mark_node || value == error_mark_node)
989bbcfc 8781 {
276beea2 8782 constructor_erroneous = 1;
8783 return;
989bbcfc 8784 }
f14c8207 8785 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
8786 && (TREE_CODE (value) == STRING_CST
8787 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
8788 && !(TREE_CODE (value) == STRING_CST
8789 && TREE_CODE (type) == ARRAY_TYPE
8790 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
8791 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
8792 TYPE_MAIN_VARIANT (type)))
e60a6f7b 8793 value = array_to_pointer_conversion (input_location, value);
989bbcfc 8794
276beea2 8795 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
d303aeaf 8796 && require_constant_value && pending)
989bbcfc 8797 {
276beea2 8798 /* As an extension, allow initializing objects with static storage
8799 duration with compound literals (which are then treated just as
8800 the brace enclosed list they contain). */
d303aeaf 8801 if (flag_isoc99)
8802 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
8803 "constant");
276beea2 8804 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
8805 value = DECL_INITIAL (decl);
989bbcfc 8806 }
8807
a75b1c71 8808 npc = null_pointer_constant_p (value);
c6418a4e 8809 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
8810 {
8811 semantic_type = TREE_TYPE (value);
8812 value = TREE_OPERAND (value, 0);
8813 }
a75b1c71 8814 value = c_fully_fold (value, require_constant_value, &maybe_const);
8815
276beea2 8816 if (value == error_mark_node)
8817 constructor_erroneous = 1;
8818 else if (!TREE_CONSTANT (value))
8819 constructor_constant = 0;
292237f3 8820 else if (!initializer_constant_valid_p (value,
8821 TREE_TYPE (value),
8822 AGGREGATE_TYPE_P (constructor_type)
8823 && TYPE_REVERSE_STORAGE_ORDER
8824 (constructor_type))
ef61516b 8825 || (RECORD_OR_UNION_TYPE_P (constructor_type)
276beea2 8826 && DECL_C_BIT_FIELD (field)
8827 && TREE_CODE (value) != INTEGER_CST))
8828 constructor_simple = 0;
a75b1c71 8829 if (!maybe_const)
8830 constructor_nonconst = 1;
276beea2 8831
9c3ffbcb 8832 /* Digest the initializer and issue any errors about incompatible
8833 types before issuing errors about non-constant initializers. */
8834 tree new_value = value;
8835 if (semantic_type)
8836 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
8837 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
8838 require_constant_value);
8839 if (new_value == error_mark_node)
8840 {
8841 constructor_erroneous = 1;
8842 return;
8843 }
8844 if (require_constant_value || require_constant_elements)
8845 constant_expression_warning (new_value);
8846
8847 /* Proceed to check the constness of the original initializer. */
fdf95cc9 8848 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
989bbcfc 8849 {
fdf95cc9 8850 if (require_constant_value)
8851 {
53de1631 8852 error_init (loc, "initializer element is not constant");
fdf95cc9 8853 value = error_mark_node;
8854 }
8855 else if (require_constant_elements)
af379dd3 8856 pedwarn (loc, OPT_Wpedantic,
21ca8540 8857 "initializer element is not computable at load time");
989bbcfc 8858 }
a75b1c71 8859 else if (!maybe_const
8860 && (require_constant_value || require_constant_elements))
af379dd3 8861 pedwarn_init (loc, OPT_Wpedantic,
a75b1c71 8862 "initializer element is not a constant expression");
276beea2 8863
bc620c5c 8864 /* Issue -Wc++-compat warnings about initializing a bitfield with
8865 enum type. */
8866 if (warn_cxx_compat
8867 && field != NULL_TREE
8868 && TREE_CODE (field) == FIELD_DECL
8869 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
8870 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
8871 != TYPE_MAIN_VARIANT (type))
8872 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
8873 {
8874 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
8875 if (checktype != error_mark_node
8876 && (TYPE_MAIN_VARIANT (checktype)
8877 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
d44f2f7c 8878 warning_init (loc, OPT_Wc___compat,
bc620c5c 8879 "enum conversion in initialization is invalid in C++");
8880 }
8881
276beea2 8882 /* If this field is empty (and not at the end of structure),
8883 don't do anything other than checking the initializer. */
8884 if (field
8885 && (TREE_TYPE (field) == error_mark_node
8886 || (COMPLETE_TYPE_P (TREE_TYPE (field))
8887 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
8888 && (TREE_CODE (constructor_type) == ARRAY_TYPE
1767a056 8889 || DECL_CHAIN (field)))))
276beea2 8890 return;
8891
9c3ffbcb 8892 /* Finally, set VALUE to the initializer value digested above. */
8893 value = new_value;
989bbcfc 8894
276beea2 8895 /* If this element doesn't come next in sequence,
8896 put it on constructor_pending_elts. */
8897 if (TREE_CODE (constructor_type) == ARRAY_TYPE
8898 && (!constructor_incremental
8899 || !tree_int_cst_equal (field, constructor_unfilled_index)))
989bbcfc 8900 {
276beea2 8901 if (constructor_incremental
8902 && tree_int_cst_lt (field, constructor_unfilled_index))
759791ee 8903 set_nonincremental_init (braced_init_obstack);
276beea2 8904
d44f2f7c 8905 add_pending_init (loc, field, value, origtype, implicit,
759791ee 8906 braced_init_obstack);
276beea2 8907 return;
989bbcfc 8908 }
276beea2 8909 else if (TREE_CODE (constructor_type) == RECORD_TYPE
8910 && (!constructor_incremental
8911 || field != constructor_unfilled_fields))
989bbcfc 8912 {
276beea2 8913 /* We do this for records but not for unions. In a union,
8914 no matter which field is specified, it can be initialized
8915 right away since it starts at the beginning of the union. */
8916 if (constructor_incremental)
8917 {
8918 if (!constructor_unfilled_fields)
759791ee 8919 set_nonincremental_init (braced_init_obstack);
276beea2 8920 else
8921 {
8922 tree bitpos, unfillpos;
8923
8924 bitpos = bit_position (field);
8925 unfillpos = bit_position (constructor_unfilled_fields);
8926
8927 if (tree_int_cst_lt (bitpos, unfillpos))
759791ee 8928 set_nonincremental_init (braced_init_obstack);
276beea2 8929 }
8930 }
8931
d44f2f7c 8932 add_pending_init (loc, field, value, origtype, implicit,
759791ee 8933 braced_init_obstack);
276beea2 8934 return;
989bbcfc 8935 }
276beea2 8936 else if (TREE_CODE (constructor_type) == UNION_TYPE
f1f41a6c 8937 && !vec_safe_is_empty (constructor_elements))
276beea2 8938 {
d241bf0d 8939 if (!implicit)
8940 {
f1f41a6c 8941 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
dc5945dc 8942 warning_init (loc, OPT_Woverride_init_side_effects,
d241bf0d 8943 "initialized field with side-effects overwritten");
8944 else if (warn_override_init)
d44f2f7c 8945 warning_init (loc, OPT_Woverride_init,
8946 "initialized field overwritten");
d241bf0d 8947 }
989bbcfc 8948
276beea2 8949 /* We can have just one union field set. */
f1f41a6c 8950 constructor_elements = NULL;
276beea2 8951 }
989bbcfc 8952
276beea2 8953 /* Otherwise, output this element either to
8954 constructor_elements or to the assembler file. */
989bbcfc 8955
e82e4eb5 8956 constructor_elt celt = {field, value};
f1f41a6c 8957 vec_safe_push (constructor_elements, celt);
989bbcfc 8958
276beea2 8959 /* Advance the variable that indicates sequential elements output. */
8960 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8961 constructor_unfilled_index
389dd41b 8962 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
8963 bitsize_one_node);
276beea2 8964 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
8965 {
8966 constructor_unfilled_fields
1767a056 8967 = DECL_CHAIN (constructor_unfilled_fields);
989bbcfc 8968
276beea2 8969 /* Skip any nameless bit fields. */
8970 while (constructor_unfilled_fields != 0
8971 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
8972 && DECL_NAME (constructor_unfilled_fields) == 0)
8973 constructor_unfilled_fields =
1767a056 8974 DECL_CHAIN (constructor_unfilled_fields);
276beea2 8975 }
8976 else if (TREE_CODE (constructor_type) == UNION_TYPE)
8977 constructor_unfilled_fields = 0;
fc698c9d 8978
276beea2 8979 /* Now output any pending elements which have become next. */
8980 if (pending)
759791ee 8981 output_pending_init_elements (0, braced_init_obstack);
276beea2 8982}
989bbcfc 8983
276beea2 8984/* Output any pending elements which have become next.
8985 As we output elements, constructor_unfilled_{fields,index}
8986 advances, which may cause other elements to become next;
8987 if so, they too are output.
989bbcfc 8988
276beea2 8989 If ALL is 0, we return when there are
8990 no more pending elements to output now.
5d844ba2 8991
276beea2 8992 If ALL is 1, we output space as necessary so that
8993 we can output all the pending elements. */
276beea2 8994static void
759791ee 8995output_pending_init_elements (int all, struct obstack * braced_init_obstack)
276beea2 8996{
8997 struct init_node *elt = constructor_pending_elts;
8998 tree next;
fc698c9d 8999
276beea2 9000 retry:
9001
c7bf1374 9002 /* Look through the whole pending tree.
276beea2 9003 If we find an element that should be output now,
9004 output it. Otherwise, set NEXT to the element
9005 that comes first among those still pending. */
9006
9007 next = 0;
9008 while (elt)
9009 {
9010 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
989bbcfc 9011 {
276beea2 9012 if (tree_int_cst_equal (elt->purpose,
9013 constructor_unfilled_index))
adc93f31 9014 output_init_element (input_location, elt->value, elt->origtype,
9015 true, TREE_TYPE (constructor_type),
759791ee 9016 constructor_unfilled_index, 0, false,
9017 braced_init_obstack);
276beea2 9018 else if (tree_int_cst_lt (constructor_unfilled_index,
9019 elt->purpose))
989bbcfc 9020 {
276beea2 9021 /* Advance to the next smaller node. */
9022 if (elt->left)
9023 elt = elt->left;
9024 else
9025 {
9026 /* We have reached the smallest node bigger than the
9027 current unfilled index. Fill the space first. */
9028 next = elt->purpose;
9029 break;
9030 }
989bbcfc 9031 }
b10b344f 9032 else
9033 {
276beea2 9034 /* Advance to the next bigger node. */
9035 if (elt->right)
9036 elt = elt->right;
9037 else
b10b344f 9038 {
276beea2 9039 /* We have reached the biggest node in a subtree. Find
9040 the parent of it, which is the next bigger node. */
9041 while (elt->parent && elt->parent->right == elt)
9042 elt = elt->parent;
9043 elt = elt->parent;
9044 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9045 elt->purpose))
9046 {
9047 next = elt->purpose;
9048 break;
9049 }
b10b344f 9050 }
9051 }
989bbcfc 9052 }
ef61516b 9053 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 9054 {
9055 tree ctor_unfilled_bitpos, elt_bitpos;
b10b344f 9056
276beea2 9057 /* If the current record is complete we are done. */
9058 if (constructor_unfilled_fields == 0)
9059 break;
fc698c9d 9060
276beea2 9061 ctor_unfilled_bitpos = bit_position (constructor_unfilled_fields);
9062 elt_bitpos = bit_position (elt->purpose);
9063 /* We can't compare fields here because there might be empty
9064 fields in between. */
9065 if (tree_int_cst_equal (elt_bitpos, ctor_unfilled_bitpos))
9066 {
9067 constructor_unfilled_fields = elt->purpose;
adc93f31 9068 output_init_element (input_location, elt->value, elt->origtype,
9069 true, TREE_TYPE (elt->purpose),
759791ee 9070 elt->purpose, 0, false,
9071 braced_init_obstack);
276beea2 9072 }
9073 else if (tree_int_cst_lt (ctor_unfilled_bitpos, elt_bitpos))
9074 {
9075 /* Advance to the next smaller node. */
9076 if (elt->left)
9077 elt = elt->left;
9078 else
9079 {
9080 /* We have reached the smallest node bigger than the
9081 current unfilled field. Fill the space first. */
9082 next = elt->purpose;
9083 break;
9084 }
9085 }
9086 else
9087 {
9088 /* Advance to the next bigger node. */
9089 if (elt->right)
9090 elt = elt->right;
9091 else
9092 {
9093 /* We have reached the biggest node in a subtree. Find
9094 the parent of it, which is the next bigger node. */
9095 while (elt->parent && elt->parent->right == elt)
9096 elt = elt->parent;
9097 elt = elt->parent;
9098 if (elt
9099 && (tree_int_cst_lt (ctor_unfilled_bitpos,
9100 bit_position (elt->purpose))))
9101 {
9102 next = elt->purpose;
9103 break;
9104 }
9105 }
9106 }
9107 }
9108 }
fc698c9d 9109
276beea2 9110 /* Ordinarily return, but not if we want to output all
9111 and there are elements left. */
84166705 9112 if (!(all && next != 0))
c2e053d8 9113 return;
9114
276beea2 9115 /* If it's not incremental, just skip over the gap, so that after
9116 jumping to retry we will output the next successive element. */
ef61516b 9117 if (RECORD_OR_UNION_TYPE_P (constructor_type))
276beea2 9118 constructor_unfilled_fields = next;
9119 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9120 constructor_unfilled_index = next;
fc698c9d 9121
276beea2 9122 /* ELT now points to the node in the pending tree with the next
9123 initializer to output. */
9124 goto retry;
fc698c9d 9125}
9126\f
276beea2 9127/* Add one non-braced element to the current constructor level.
9128 This adjusts the current position within the constructor's type.
9129 This may also start or terminate implicit levels
9130 to handle a partly-braced initializer.
997d68fe 9131
276beea2 9132 Once this has found the correct level for the new element,
d241bf0d 9133 it calls output_init_element.
9134
9135 IMPLICIT is true if value comes from pop_init_level (1),
9136 the new initializer has been merged with the existing one
9137 and thus no warnings should be emitted about overriding an
9138 existing initializer. */
276beea2 9139
9140void
adc93f31 9141process_init_element (location_t loc, struct c_expr value, bool implicit,
759791ee 9142 struct obstack * braced_init_obstack)
997d68fe 9143{
3ad5f027 9144 tree orig_value = value.value;
9145 int string_flag = orig_value != 0 && TREE_CODE (orig_value) == STRING_CST;
9146 bool strict_string = value.original_code == STRING_CST;
f221adca 9147 bool was_designated = designator_depth != 0;
997d68fe 9148
276beea2 9149 designator_depth = 0;
ebe39827 9150 designator_erroneous = 0;
997d68fe 9151
95cdf3fd 9152 if (!implicit && value.value && !integer_zerop (value.value))
9153 constructor_zeroinit = 0;
9154
276beea2 9155 /* Handle superfluous braces around string cst as in
9156 char x[] = {"foo"}; */
9157 if (string_flag
9158 && constructor_type
f221adca 9159 && !was_designated
276beea2 9160 && TREE_CODE (constructor_type) == ARRAY_TYPE
e91a0d39 9161 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
276beea2 9162 && integer_zerop (constructor_unfilled_index))
997d68fe 9163 {
3ad5f027 9164 if (constructor_stack->replacement_value.value)
53de1631 9165 error_init (loc, "excess elements in char array initializer");
276beea2 9166 constructor_stack->replacement_value = value;
9167 return;
997d68fe 9168 }
989bbcfc 9169
3ad5f027 9170 if (constructor_stack->replacement_value.value != 0)
276beea2 9171 {
53de1631 9172 error_init (loc, "excess elements in struct initializer");
276beea2 9173 return;
997d68fe 9174 }
9175
276beea2 9176 /* Ignore elements of a brace group if it is entirely superfluous
9177 and has already been diagnosed. */
9178 if (constructor_type == 0)
9179 return;
997d68fe 9180
74691f46 9181 if (!implicit && warn_designated_init && !was_designated
9182 && TREE_CODE (constructor_type) == RECORD_TYPE
9183 && lookup_attribute ("designated_init",
9184 TYPE_ATTRIBUTES (constructor_type)))
9185 warning_init (loc,
9186 OPT_Wdesignated_init,
9187 "positional initialization of field "
9188 "in %<struct%> declared with %<designated_init%> attribute");
9189
276beea2 9190 /* If we've exhausted any levels that didn't have braces,
9191 pop them now. */
9192 while (constructor_stack->implicit)
9193 {
ef61516b 9194 if (RECORD_OR_UNION_TYPE_P (constructor_type)
276beea2 9195 && constructor_fields == 0)
53de1631 9196 process_init_element (loc,
9197 pop_init_level (loc, 1, braced_init_obstack),
759791ee 9198 true, braced_init_obstack);
415528ef 9199 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
6290f0db 9200 || VECTOR_TYPE_P (constructor_type))
54550ecb 9201 && constructor_max_index
9202 && tree_int_cst_lt (constructor_max_index,
9203 constructor_index))
53de1631 9204 process_init_element (loc,
9205 pop_init_level (loc, 1, braced_init_obstack),
759791ee 9206 true, braced_init_obstack);
276beea2 9207 else
9208 break;
9209 }
997d68fe 9210
276beea2 9211 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9212 if (constructor_range_stack)
997d68fe 9213 {
276beea2 9214 /* If value is a compound literal and we'll be just using its
9215 content, don't put it into a SAVE_EXPR. */
3ad5f027 9216 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
46861609 9217 || !require_constant_value)
c6418a4e 9218 {
9219 tree semantic_type = NULL_TREE;
9220 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9221 {
9222 semantic_type = TREE_TYPE (value.value);
9223 value.value = TREE_OPERAND (value.value, 0);
9224 }
9225 value.value = c_save_expr (value.value);
9226 if (semantic_type)
9227 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9228 value.value);
9229 }
276beea2 9230 }
997d68fe 9231
276beea2 9232 while (1)
9233 {
9234 if (TREE_CODE (constructor_type) == RECORD_TYPE)
997d68fe 9235 {
276beea2 9236 tree fieldtype;
9237 enum tree_code fieldcode;
997d68fe 9238
276beea2 9239 if (constructor_fields == 0)
9240 {
53de1631 9241 pedwarn_init (loc, 0, "excess elements in struct initializer");
276beea2 9242 break;
9243 }
997d68fe 9244
276beea2 9245 fieldtype = TREE_TYPE (constructor_fields);
9246 if (fieldtype != error_mark_node)
9247 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9248 fieldcode = TREE_CODE (fieldtype);
997d68fe 9249
276beea2 9250 /* Error for non-static initialization of a flexible array member. */
9251 if (fieldcode == ARRAY_TYPE
9252 && !require_constant_value
9253 && TYPE_SIZE (fieldtype) == NULL_TREE
d059ef79 9254 && DECL_CHAIN (constructor_fields) == NULL_TREE)
276beea2 9255 {
53de1631 9256 error_init (loc, "non-static initialization of a flexible "
9257 "array member");
276beea2 9258 break;
9259 }
997d68fe 9260
15e18a8c 9261 /* Error for initialization of a flexible array member with
9262 a string constant if the structure is in an array. E.g.:
9263 struct S { int x; char y[]; };
9264 struct S s[] = { { 1, "foo" } };
9265 is invalid. */
9266 if (string_flag
9267 && fieldcode == ARRAY_TYPE
9268 && constructor_depth > 1
9269 && TYPE_SIZE (fieldtype) == NULL_TREE
9270 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9271 {
9272 bool in_array_p = false;
9273 for (struct constructor_stack *p = constructor_stack;
9274 p && p->type; p = p->next)
9275 if (TREE_CODE (p->type) == ARRAY_TYPE)
9276 {
9277 in_array_p = true;
9278 break;
9279 }
9280 if (in_array_p)
9281 {
9282 error_init (loc, "initialization of flexible array "
9283 "member in a nested context");
9284 break;
9285 }
9286 }
9287
276beea2 9288 /* Accept a string constant to initialize a subarray. */
3ad5f027 9289 if (value.value != 0
276beea2 9290 && fieldcode == ARRAY_TYPE
e91a0d39 9291 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
276beea2 9292 && string_flag)
3ad5f027 9293 value.value = orig_value;
276beea2 9294 /* Otherwise, if we have come to a subaggregate,
9295 and we don't have an element of its type, push into it. */
d585e4c9 9296 else if (value.value != 0
3ad5f027 9297 && value.value != error_mark_node
9298 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
276beea2 9299 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
415528ef 9300 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
276beea2 9301 {
53de1631 9302 push_init_level (loc, 1, braced_init_obstack);
276beea2 9303 continue;
9304 }
997d68fe 9305
3ad5f027 9306 if (value.value)
276beea2 9307 {
9308 push_member_name (constructor_fields);
adc93f31 9309 output_init_element (loc, value.value, value.original_type,
b9c74b4d 9310 strict_string, fieldtype,
759791ee 9311 constructor_fields, 1, implicit,
9312 braced_init_obstack);
276beea2 9313 RESTORE_SPELLING_DEPTH (constructor_depth);
997d68fe 9314 }
9315 else
276beea2 9316 /* Do the bookkeeping for an element that was
9317 directly output as a constructor. */
997d68fe 9318 {
276beea2 9319 /* For a record, keep track of end position of last field. */
9320 if (DECL_SIZE (constructor_fields))
a0c938f0 9321 constructor_bit_index
389dd41b 9322 = size_binop_loc (input_location, PLUS_EXPR,
9323 bit_position (constructor_fields),
9324 DECL_SIZE (constructor_fields));
276beea2 9325
9326 /* If the current field was the first one not yet written out,
9327 it isn't now, so update. */
9328 if (constructor_unfilled_fields == constructor_fields)
9329 {
1767a056 9330 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
276beea2 9331 /* Skip any nameless bit fields. */
9332 while (constructor_unfilled_fields != 0
9333 && DECL_C_BIT_FIELD (constructor_unfilled_fields)
9334 && DECL_NAME (constructor_unfilled_fields) == 0)
9335 constructor_unfilled_fields =
1767a056 9336 DECL_CHAIN (constructor_unfilled_fields);
276beea2 9337 }
997d68fe 9338 }
276beea2 9339
1767a056 9340 constructor_fields = DECL_CHAIN (constructor_fields);
276beea2 9341 /* Skip any nameless bit fields at the beginning. */
9342 while (constructor_fields != 0
9343 && DECL_C_BIT_FIELD (constructor_fields)
9344 && DECL_NAME (constructor_fields) == 0)
1767a056 9345 constructor_fields = DECL_CHAIN (constructor_fields);
997d68fe 9346 }
276beea2 9347 else if (TREE_CODE (constructor_type) == UNION_TYPE)
997d68fe 9348 {
276beea2 9349 tree fieldtype;
9350 enum tree_code fieldcode;
997d68fe 9351
276beea2 9352 if (constructor_fields == 0)
9353 {
c9e51d25 9354 pedwarn_init (loc, 0,
21ca8540 9355 "excess elements in union initializer");
276beea2 9356 break;
9357 }
997d68fe 9358
276beea2 9359 fieldtype = TREE_TYPE (constructor_fields);
9360 if (fieldtype != error_mark_node)
9361 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9362 fieldcode = TREE_CODE (fieldtype);
997d68fe 9363
276beea2 9364 /* Warn that traditional C rejects initialization of unions.
9365 We skip the warning if the value is zero. This is done
9366 under the assumption that the zero initializer in user
9367 code appears conditioned on e.g. __STDC__ to avoid
9368 "missing initializer" warnings and relies on default
9369 initialization to zero in the traditional C case.
9370 We also skip the warning if the initializer is designated,
9371 again on the assumption that this must be conditional on
9372 __STDC__ anyway (and we've already complained about the
9373 member-designator already). */
3df42822 9374 if (!in_system_header_at (input_location) && !constructor_designated
3ad5f027 9375 && !(value.value && (integer_zerop (value.value)
9376 || real_zerop (value.value))))
6bf97f82 9377 warning (OPT_Wtraditional, "traditional C rejects initialization "
9378 "of unions");
997d68fe 9379
276beea2 9380 /* Accept a string constant to initialize a subarray. */
3ad5f027 9381 if (value.value != 0
276beea2 9382 && fieldcode == ARRAY_TYPE
e91a0d39 9383 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
276beea2 9384 && string_flag)
3ad5f027 9385 value.value = orig_value;
276beea2 9386 /* Otherwise, if we have come to a subaggregate,
9387 and we don't have an element of its type, push into it. */
d585e4c9 9388 else if (value.value != 0
3ad5f027 9389 && value.value != error_mark_node
9390 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
276beea2 9391 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
415528ef 9392 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
276beea2 9393 {
53de1631 9394 push_init_level (loc, 1, braced_init_obstack);
276beea2 9395 continue;
9396 }
997d68fe 9397
3ad5f027 9398 if (value.value)
276beea2 9399 {
9400 push_member_name (constructor_fields);
adc93f31 9401 output_init_element (loc, value.value, value.original_type,
b9c74b4d 9402 strict_string, fieldtype,
759791ee 9403 constructor_fields, 1, implicit,
9404 braced_init_obstack);
276beea2 9405 RESTORE_SPELLING_DEPTH (constructor_depth);
997d68fe 9406 }
9407 else
276beea2 9408 /* Do the bookkeeping for an element that was
9409 directly output as a constructor. */
997d68fe 9410 {
276beea2 9411 constructor_bit_index = DECL_SIZE (constructor_fields);
d059ef79 9412 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
997d68fe 9413 }
997d68fe 9414
276beea2 9415 constructor_fields = 0;
9416 }
9417 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9418 {
9419 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9420 enum tree_code eltcode = TREE_CODE (elttype);
997d68fe 9421
276beea2 9422 /* Accept a string constant to initialize a subarray. */
3ad5f027 9423 if (value.value != 0
276beea2 9424 && eltcode == ARRAY_TYPE
e91a0d39 9425 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
276beea2 9426 && string_flag)
3ad5f027 9427 value.value = orig_value;
276beea2 9428 /* Otherwise, if we have come to a subaggregate,
9429 and we don't have an element of its type, push into it. */
d585e4c9 9430 else if (value.value != 0
3ad5f027 9431 && value.value != error_mark_node
9432 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
276beea2 9433 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
415528ef 9434 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
276beea2 9435 {
53de1631 9436 push_init_level (loc, 1, braced_init_obstack);
276beea2 9437 continue;
9438 }
989bbcfc 9439
276beea2 9440 if (constructor_max_index != 0
9441 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9442 || integer_all_onesp (constructor_max_index)))
9443 {
c9e51d25 9444 pedwarn_init (loc, 0,
21ca8540 9445 "excess elements in array initializer");
276beea2 9446 break;
9447 }
989bbcfc 9448
276beea2 9449 /* Now output the actual element. */
3ad5f027 9450 if (value.value)
276beea2 9451 {
e913b5cd 9452 push_array_bounds (tree_to_uhwi (constructor_index));
adc93f31 9453 output_init_element (loc, value.value, value.original_type,
b9c74b4d 9454 strict_string, elttype,
759791ee 9455 constructor_index, 1, implicit,
9456 braced_init_obstack);
276beea2 9457 RESTORE_SPELLING_DEPTH (constructor_depth);
9458 }
2b30d46c 9459
276beea2 9460 constructor_index
389dd41b 9461 = size_binop_loc (input_location, PLUS_EXPR,
9462 constructor_index, bitsize_one_node);
989bbcfc 9463
3ad5f027 9464 if (!value.value)
276beea2 9465 /* If we are doing the bookkeeping for an element that was
9466 directly output as a constructor, we must update
9467 constructor_unfilled_index. */
9468 constructor_unfilled_index = constructor_index;
9469 }
6290f0db 9470 else if (VECTOR_TYPE_P (constructor_type))
276beea2 9471 {
9472 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
989bbcfc 9473
a0c938f0 9474 /* Do a basic check of initializer size. Note that vectors
9475 always have a fixed size derived from their type. */
276beea2 9476 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9477 {
c9e51d25 9478 pedwarn_init (loc, 0,
21ca8540 9479 "excess elements in vector initializer");
276beea2 9480 break;
9481 }
989bbcfc 9482
276beea2 9483 /* Now output the actual element. */
3ad5f027 9484 if (value.value)
415528ef 9485 {
9486 if (TREE_CODE (value.value) == VECTOR_CST)
9487 elttype = TYPE_MAIN_VARIANT (constructor_type);
adc93f31 9488 output_init_element (loc, value.value, value.original_type,
415528ef 9489 strict_string, elttype,
759791ee 9490 constructor_index, 1, implicit,
9491 braced_init_obstack);
415528ef 9492 }
989bbcfc 9493
276beea2 9494 constructor_index
389dd41b 9495 = size_binop_loc (input_location,
9496 PLUS_EXPR, constructor_index, bitsize_one_node);
989bbcfc 9497
3ad5f027 9498 if (!value.value)
276beea2 9499 /* If we are doing the bookkeeping for an element that was
9500 directly output as a constructor, we must update
9501 constructor_unfilled_index. */
9502 constructor_unfilled_index = constructor_index;
9503 }
989bbcfc 9504
276beea2 9505 /* Handle the sole element allowed in a braced initializer
9506 for a scalar variable. */
b765c734 9507 else if (constructor_type != error_mark_node
9508 && constructor_fields == 0)
989bbcfc 9509 {
c9e51d25 9510 pedwarn_init (loc, 0,
21ca8540 9511 "excess elements in scalar initializer");
276beea2 9512 break;
989bbcfc 9513 }
9514 else
9515 {
3ad5f027 9516 if (value.value)
adc93f31 9517 output_init_element (loc, value.value, value.original_type,
b9c74b4d 9518 strict_string, constructor_type,
759791ee 9519 NULL_TREE, 1, implicit,
9520 braced_init_obstack);
276beea2 9521 constructor_fields = 0;
989bbcfc 9522 }
9523
276beea2 9524 /* Handle range initializers either at this level or anywhere higher
9525 in the designator stack. */
9526 if (constructor_range_stack)
989bbcfc 9527 {
276beea2 9528 struct constructor_range_stack *p, *range_stack;
9529 int finish = 0;
9530
9531 range_stack = constructor_range_stack;
9532 constructor_range_stack = 0;
9533 while (constructor_stack != range_stack->stack)
989bbcfc 9534 {
231bd014 9535 gcc_assert (constructor_stack->implicit);
adc93f31 9536 process_init_element (loc,
53de1631 9537 pop_init_level (loc, 1,
9538 braced_init_obstack),
759791ee 9539 true, braced_init_obstack);
989bbcfc 9540 }
276beea2 9541 for (p = range_stack;
9542 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
9543 p = p->prev)
989bbcfc 9544 {
231bd014 9545 gcc_assert (constructor_stack->implicit);
adc93f31 9546 process_init_element (loc,
53de1631 9547 pop_init_level (loc, 1,
9548 braced_init_obstack),
759791ee 9549 true, braced_init_obstack);
989bbcfc 9550 }
276beea2 9551
389dd41b 9552 p->index = size_binop_loc (input_location,
9553 PLUS_EXPR, p->index, bitsize_one_node);
276beea2 9554 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
9555 finish = 1;
9556
9557 while (1)
9558 {
9559 constructor_index = p->index;
9560 constructor_fields = p->fields;
9561 if (finish && p->range_end && p->index == p->range_start)
9562 {
9563 finish = 0;
9564 p->prev = 0;
9565 }
9566 p = p->next;
9567 if (!p)
9568 break;
74a4140f 9569 finish_implicit_inits (loc, braced_init_obstack);
53de1631 9570 push_init_level (loc, 2, braced_init_obstack);
276beea2 9571 p->stack = constructor_stack;
9572 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
9573 p->index = p->range_start;
9574 }
9575
9576 if (!finish)
9577 constructor_range_stack = range_stack;
9578 continue;
989bbcfc 9579 }
9580
276beea2 9581 break;
989bbcfc 9582 }
9583
276beea2 9584 constructor_range_stack = 0;
9585}
9586\f
fae5a040 9587/* Build a complete asm-statement, whose components are a CV_QUALIFIER
9588 (guaranteed to be 'volatile' or null) and ARGS (represented using
226c2452 9589 an ASM_EXPR node). */
276beea2 9590tree
fae5a040 9591build_asm_stmt (tree cv_qualifier, tree args)
276beea2 9592{
4ee9c684 9593 if (!ASM_VOLATILE_P (args) && cv_qualifier)
9594 ASM_VOLATILE_P (args) = 1;
fae5a040 9595 return add_stmt (args);
989bbcfc 9596}
9597
fae5a040 9598/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
9599 some INPUTS, and some CLOBBERS. The latter three may be NULL.
9600 SIMPLE indicates whether there was anything at all after the
9601 string in the asm expression -- asm("blah") and asm("blah" : )
226c2452 9602 are subtly different. We use a ASM_EXPR node to represent this. */
276beea2 9603tree
e60a6f7b 9604build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
78f55ca8 9605 tree clobbers, tree labels, bool simple)
997d68fe 9606{
276beea2 9607 tree tail;
fae5a040 9608 tree args;
4ee9c684 9609 int i;
9610 const char *constraint;
8b0d0af6 9611 const char **oconstraints;
4ee9c684 9612 bool allows_mem, allows_reg, is_inout;
8b0d0af6 9613 int ninputs, noutputs;
4ee9c684 9614
9615 ninputs = list_length (inputs);
9616 noutputs = list_length (outputs);
8b0d0af6 9617 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
9618
78f55ca8 9619 string = resolve_asm_operand_names (string, outputs, inputs, labels);
276beea2 9620
4ee9c684 9621 /* Remove output conversions that change the type but not the mode. */
9622 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
997d68fe 9623 {
276beea2 9624 tree output = TREE_VALUE (tail);
8b0d0af6 9625
035b65e2 9626 output = c_fully_fold (output, false, NULL);
9627
8b0d0af6 9628 /* ??? Really, this should not be here. Users should be using a
9629 proper lvalue, dammit. But there's a long history of using casts
9630 in the output operands. In cases like longlong.h, this becomes a
9631 primitive form of typechecking -- if the cast can be removed, then
9632 the output operand had a type of the proper width; otherwise we'll
9633 get an error. Gross, but ... */
276beea2 9634 STRIP_NOPS (output);
8b0d0af6 9635
fdd84b77 9636 if (!lvalue_or_else (loc, output, lv_asm))
8b0d0af6 9637 output = error_mark_node;
989bbcfc 9638
89552023 9639 if (output != error_mark_node
9640 && (TREE_READONLY (output)
9641 || TYPE_READONLY (TREE_TYPE (output))
ef61516b 9642 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
89552023 9643 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
f2697631 9644 readonly_error (loc, output, lv_asm);
89552023 9645
4ee9c684 9646 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
8b0d0af6 9647 oconstraints[i] = constraint;
9648
9649 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
9650 &allows_mem, &allows_reg, &is_inout))
9651 {
9652 /* If the operand is going to end up in memory,
9653 mark it addressable. */
9654 if (!allows_reg && !c_mark_addressable (output))
9655 output = error_mark_node;
ef31075b 9656 if (!(!allows_reg && allows_mem)
9657 && output != error_mark_node
9658 && VOID_TYPE_P (TREE_TYPE (output)))
9659 {
9660 error_at (loc, "invalid use of void expression");
9661 output = error_mark_node;
9662 }
8b0d0af6 9663 }
9664 else
a0c938f0 9665 output = error_mark_node;
276beea2 9666
8b0d0af6 9667 TREE_VALUE (tail) = output;
989bbcfc 9668 }
276beea2 9669
8b0d0af6 9670 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
9671 {
9672 tree input;
9673
9674 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
9675 input = TREE_VALUE (tail);
9676
8b0d0af6 9677 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
9678 oconstraints, &allows_mem, &allows_reg))
9679 {
9680 /* If the operand is going to end up in memory,
9681 mark it addressable. */
c6e95001 9682 if (!allows_reg && allows_mem)
9683 {
035b65e2 9684 input = c_fully_fold (input, false, NULL);
9685
c6e95001 9686 /* Strip the nops as we allow this case. FIXME, this really
9687 should be rejected or made deprecated. */
9688 STRIP_NOPS (input);
9689 if (!c_mark_addressable (input))
9690 input = error_mark_node;
ef31075b 9691 }
035b65e2 9692 else
ef31075b 9693 {
035b65e2 9694 struct c_expr expr;
9695 memset (&expr, 0, sizeof (expr));
9696 expr.value = input;
b560fabd 9697 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
035b65e2 9698 input = c_fully_fold (expr.value, false, NULL);
9699
9700 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
9701 {
9702 error_at (loc, "invalid use of void expression");
9703 input = error_mark_node;
9704 }
ef31075b 9705 }
8b0d0af6 9706 }
9707 else
9708 input = error_mark_node;
9709
9710 TREE_VALUE (tail) = input;
9711 }
276beea2 9712
78f55ca8 9713 /* ASMs with labels cannot have outputs. This should have been
9714 enforced by the parser. */
9715 gcc_assert (outputs == NULL || labels == NULL);
9716
9717 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
fae5a040 9718
89552023 9719 /* asm statements without outputs, including simple ones, are treated
9720 as volatile. */
9721 ASM_INPUT_P (args) = simple;
9722 ASM_VOLATILE_P (args) = (noutputs == 0);
8b0d0af6 9723
fae5a040 9724 return args;
997d68fe 9725}
276beea2 9726\f
e60a6f7b 9727/* Generate a goto statement to LABEL. LOC is the location of the
9728 GOTO. */
0375a275 9729
9730tree
e60a6f7b 9731c_finish_goto_label (location_t loc, tree label)
0375a275 9732{
f805d53d 9733 tree decl = lookup_label_for_goto (loc, label);
0375a275 9734 if (!decl)
9735 return NULL_TREE;
0375a275 9736 TREE_USED (decl) = 1;
e60a6f7b 9737 {
9738 tree t = build1 (GOTO_EXPR, void_type_node, decl);
9739 SET_EXPR_LOCATION (t, loc);
9740 return add_stmt (t);
9741 }
0375a275 9742}
9743
e60a6f7b 9744/* Generate a computed goto statement to EXPR. LOC is the location of
9745 the GOTO. */
0375a275 9746
9747tree
e60a6f7b 9748c_finish_goto_ptr (location_t loc, tree expr)
0375a275 9749{
e60a6f7b 9750 tree t;
29438999 9751 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
a75b1c71 9752 expr = c_fully_fold (expr, false, NULL);
0375a275 9753 expr = convert (ptr_type_node, expr);
e60a6f7b 9754 t = build1 (GOTO_EXPR, void_type_node, expr);
9755 SET_EXPR_LOCATION (t, loc);
9756 return add_stmt (t);
0375a275 9757}
9758
c857cd60 9759/* Generate a C `return' statement. RETVAL is the expression for what
e60a6f7b 9760 to return, or a null pointer for `return;' with no value. LOC is
430b73ee 9761 the location of the return statement, or the location of the expression,
9762 if the statement has any. If ORIGTYPE is not NULL_TREE, it
e60a6f7b 9763 is the original type of RETVAL. */
fc698c9d 9764
0375a275 9765tree
e60a6f7b 9766c_finish_return (location_t loc, tree retval, tree origtype)
276beea2 9767{
81a3c55b 9768 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
9769 bool no_warning = false;
a75b1c71 9770 bool npc = false;
3c6d4197 9771 size_t rank = 0;
276beea2 9772
ab4c803c 9773 /* Use the expansion point to handle cases such as returning NULL
9774 in a function returning void. */
9775 source_location xloc = expansion_point_location_if_in_system_header (loc);
9776
276beea2 9777 if (TREE_THIS_VOLATILE (current_function_decl))
ab4c803c 9778 warning_at (xloc, 0,
e60a6f7b 9779 "function declared %<noreturn%> has a %<return%> statement");
276beea2 9780
a89e6c15 9781 if (flag_cilkplus && contains_array_notation_expr (retval))
3c6d4197 9782 {
9783 /* Array notations are allowed in a return statement if it is inside a
9784 built-in array notation reduction function. */
9785 if (!find_rank (loc, retval, retval, false, &rank))
9786 return error_mark_node;
9787 if (rank >= 1)
9788 {
9789 error_at (loc, "array notation expression cannot be used as a "
9790 "return value");
9791 return error_mark_node;
9792 }
9793 }
3bfdc94f 9794 if (flag_cilkplus && retval && contains_cilk_spawn_stmt (retval))
d037099f 9795 {
9796 error_at (loc, "use of %<_Cilk_spawn%> in a return statement is not "
9797 "allowed");
9798 return error_mark_node;
9799 }
a75b1c71 9800 if (retval)
9801 {
c6418a4e 9802 tree semantic_type = NULL_TREE;
a75b1c71 9803 npc = null_pointer_constant_p (retval);
c6418a4e 9804 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
9805 {
9806 semantic_type = TREE_TYPE (retval);
9807 retval = TREE_OPERAND (retval, 0);
9808 }
a75b1c71 9809 retval = c_fully_fold (retval, false, NULL);
c6418a4e 9810 if (semantic_type)
9811 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
a75b1c71 9812 }
9813
276beea2 9814 if (!retval)
fc698c9d 9815 {
276beea2 9816 current_function_returns_null = 1;
9817 if ((warn_return_type || flag_isoc99)
9818 && valtype != 0 && TREE_CODE (valtype) != VOID_TYPE)
81a3c55b 9819 {
b9f9586a 9820 bool warned_here;
508ea33a 9821 if (flag_isoc99)
b9f9586a 9822 warned_here = pedwarn
9823 (loc, 0,
9824 "%<return%> with no value, in function returning non-void");
508ea33a 9825 else
b9f9586a 9826 warned_here = warning_at
9827 (loc, OPT_Wreturn_type,
9828 "%<return%> with no value, in function returning non-void");
81a3c55b 9829 no_warning = true;
b9f9586a 9830 if (warned_here)
9831 inform (DECL_SOURCE_LOCATION (current_function_decl),
9832 "declared here");
81a3c55b 9833 }
628fa4f8 9834 }
276beea2 9835 else if (valtype == 0 || TREE_CODE (valtype) == VOID_TYPE)
fc698c9d 9836 {
276beea2 9837 current_function_returns_null = 1;
b9f9586a 9838 bool warned_here;
6e4e3580 9839 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
b9f9586a 9840 warned_here = pedwarn
9841 (xloc, 0,
9842 "%<return%> with a value, in function returning void");
48e1416a 9843 else
b9f9586a 9844 warned_here = pedwarn
9845 (xloc, OPT_Wpedantic, "ISO C forbids "
9846 "%<return%> with expression, in function returning void");
9847 if (warned_here)
9848 inform (DECL_SOURCE_LOCATION (current_function_decl),
9849 "declared here");
fc698c9d 9850 }
276beea2 9851 else
fc698c9d 9852 {
22a75734 9853 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
9854 retval, origtype, ic_return,
e60a6f7b 9855 npc, NULL_TREE, NULL_TREE, 0);
276beea2 9856 tree res = DECL_RESULT (current_function_decl);
9857 tree inner;
c708c6ef 9858 bool save;
276beea2 9859
9860 current_function_returns_value = 1;
9861 if (t == error_mark_node)
0375a275 9862 return NULL_TREE;
276beea2 9863
c708c6ef 9864 save = in_late_binary_op;
9865 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
823a9dd4 9866 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
9867 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
9868 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
9869 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
9870 && (flag_sanitize & SANITIZE_FLOAT_CAST)))
c708c6ef 9871 in_late_binary_op = true;
276beea2 9872 inner = t = convert (TREE_TYPE (res), t);
c708c6ef 9873 in_late_binary_op = save;
276beea2 9874
9875 /* Strip any conversions, additions, and subtractions, and see if
9876 we are returning the address of a local variable. Warn if so. */
9877 while (1)
989bbcfc 9878 {
276beea2 9879 switch (TREE_CODE (inner))
989bbcfc 9880 {
e7e7c0fe 9881 CASE_CONVERT:
9882 case NON_LVALUE_EXPR:
276beea2 9883 case PLUS_EXPR:
e7e7c0fe 9884 case POINTER_PLUS_EXPR:
276beea2 9885 inner = TREE_OPERAND (inner, 0);
9886 continue;
9887
9888 case MINUS_EXPR:
9889 /* If the second operand of the MINUS_EXPR has a pointer
9890 type (or is converted from it), this may be valid, so
9891 don't give a warning. */
9892 {
9893 tree op1 = TREE_OPERAND (inner, 1);
989bbcfc 9894
84166705 9895 while (!POINTER_TYPE_P (TREE_TYPE (op1))
72dd6141 9896 && (CONVERT_EXPR_P (op1)
9897 || TREE_CODE (op1) == NON_LVALUE_EXPR))
276beea2 9898 op1 = TREE_OPERAND (op1, 0);
989bbcfc 9899
276beea2 9900 if (POINTER_TYPE_P (TREE_TYPE (op1)))
9901 break;
989bbcfc 9902
276beea2 9903 inner = TREE_OPERAND (inner, 0);
9904 continue;
9905 }
628fa4f8 9906
276beea2 9907 case ADDR_EXPR:
9908 inner = TREE_OPERAND (inner, 0);
8ee1039f 9909
ce45a448 9910 while (REFERENCE_CLASS_P (inner)
aa3e402e 9911 && !INDIRECT_REF_P (inner))
276beea2 9912 inner = TREE_OPERAND (inner, 0);
989bbcfc 9913
aaadf915 9914 if (DECL_P (inner)
84166705 9915 && !DECL_EXTERNAL (inner)
9916 && !TREE_STATIC (inner)
276beea2 9917 && DECL_CONTEXT (inner) == current_function_decl)
d44f2f7c 9918 {
9919 if (TREE_CODE (inner) == LABEL_DECL)
9920 warning_at (loc, OPT_Wreturn_local_addr,
9921 "function returns address of label");
9922 else
f22a2cb7 9923 {
9924 warning_at (loc, OPT_Wreturn_local_addr,
9925 "function returns address of local variable");
9926 tree zero = build_zero_cst (TREE_TYPE (res));
9927 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
9928 }
d44f2f7c 9929 }
276beea2 9930 break;
989bbcfc 9931
276beea2 9932 default:
9933 break;
9934 }
fc698c9d 9935
276beea2 9936 break;
9937 }
9938
14ae0310 9939 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
e60a6f7b 9940 SET_EXPR_LOCATION (retval, loc);
6513b50d 9941
9942 if (warn_sequence_point)
9943 verify_sequence_points (retval);
fc698c9d 9944 }
989bbcfc 9945
e60a6f7b 9946 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
81a3c55b 9947 TREE_NO_WARNING (ret_stmt) |= no_warning;
9948 return add_stmt (ret_stmt);
fc698c9d 9949}
276beea2 9950\f
9951struct c_switch {
197638d4 9952 /* The SWITCH_EXPR being built. */
9953 tree switch_expr;
2ca392fd 9954
5c9dae64 9955 /* The original type of the testing expression, i.e. before the
2ca392fd 9956 default conversion is applied. */
9957 tree orig_type;
9958
276beea2 9959 /* A splay-tree mapping the low element of a case range to the high
9960 element, or NULL_TREE if there is no high element. Used to
9961 determine whether or not a new case label duplicates an old case
9962 label. We need a tree, rather than simply a hash table, because
9963 of the GNU case range extension. */
9964 splay_tree cases;
2ca392fd 9965
f805d53d 9966 /* The bindings at the point of the switch. This is used for
9967 warnings crossing decls when branching to a case label. */
9968 struct c_spot_bindings *bindings;
f5d156fd 9969
276beea2 9970 /* The next node on the stack. */
9971 struct c_switch *next;
be23b16f 9972
9973 /* Remember whether the controlling expression had boolean type
9974 before integer promotions for the sake of -Wswitch-bool. */
9975 bool bool_cond_p;
9976
9977 /* Remember whether there was a case value that is outside the
9978 range of the ORIG_TYPE. */
9979 bool outside_range_p;
276beea2 9980};
628fa4f8 9981
276beea2 9982/* A stack of the currently active switch statements. The innermost
9983 switch statement is on the top of the stack. There is no need to
9984 mark the stack for garbage collection because it is only active
9985 during the processing of the body of a function, and we never
9986 collect at that point. */
fc698c9d 9987
0375a275 9988struct c_switch *c_switch_stack;
fc698c9d 9989
276beea2 9990/* Start a C switch statement, testing expression EXP. Return the new
e60a6f7b 9991 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
c69ec07d 9992 SWITCH_COND_LOC is the location of the switch's condition.
be23b16f 9993 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
fc698c9d 9994
276beea2 9995tree
e60a6f7b 9996c_start_case (location_t switch_loc,
9997 location_t switch_cond_loc,
c69ec07d 9998 tree exp, bool explicit_cast_p)
fc698c9d 9999{
55d7e60b 10000 tree orig_type = error_mark_node;
be23b16f 10001 bool bool_cond_p = false;
276beea2 10002 struct c_switch *cs;
2b30d46c 10003
276beea2 10004 if (exp != error_mark_node)
fc698c9d 10005 {
276beea2 10006 orig_type = TREE_TYPE (exp);
10007
55d7e60b 10008 if (!INTEGRAL_TYPE_P (orig_type))
fc698c9d 10009 {
55d7e60b 10010 if (orig_type != error_mark_node)
10011 {
e60a6f7b 10012 error_at (switch_cond_loc, "switch quantity not an integer");
55d7e60b 10013 orig_type = error_mark_node;
10014 }
276beea2 10015 exp = integer_zero_node;
fc698c9d 10016 }
276beea2 10017 else
fc698c9d 10018 {
55d7e60b 10019 tree type = TYPE_MAIN_VARIANT (orig_type);
c69ec07d 10020 tree e = exp;
10021
10022 /* Warn if the condition has boolean value. */
10023 while (TREE_CODE (e) == COMPOUND_EXPR)
10024 e = TREE_OPERAND (e, 1);
10025
10026 if ((TREE_CODE (type) == BOOLEAN_TYPE
10027 || truth_value_p (TREE_CODE (e)))
10028 /* Explicit cast to int suppresses this warning. */
10029 && !(TREE_CODE (type) == INTEGER_TYPE
10030 && explicit_cast_p))
be23b16f 10031 bool_cond_p = true;
989bbcfc 10032
3df42822 10033 if (!in_system_header_at (input_location)
276beea2 10034 && (type == long_integer_type_node
10035 || type == long_unsigned_type_node))
e60a6f7b 10036 warning_at (switch_cond_loc,
10037 OPT_Wtraditional, "%<long%> switch expression not "
10038 "converted to %<int%> in ISO C");
989bbcfc 10039
a75b1c71 10040 exp = c_fully_fold (exp, false, NULL);
276beea2 10041 exp = default_conversion (exp);
6513b50d 10042
10043 if (warn_sequence_point)
10044 verify_sequence_points (exp);
276beea2 10045 }
10046 }
10047
197638d4 10048 /* Add this new SWITCH_EXPR to the stack. */
9318f22c 10049 cs = XNEW (struct c_switch);
197638d4 10050 cs->switch_expr = build3 (SWITCH_EXPR, orig_type, exp, NULL_TREE, NULL_TREE);
e60a6f7b 10051 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
2ca392fd 10052 cs->orig_type = orig_type;
276beea2 10053 cs->cases = splay_tree_new (case_compare, NULL, NULL);
f805d53d 10054 cs->bindings = c_get_switch_bindings ();
be23b16f 10055 cs->bool_cond_p = bool_cond_p;
10056 cs->outside_range_p = false;
0375a275 10057 cs->next = c_switch_stack;
10058 c_switch_stack = cs;
276beea2 10059
197638d4 10060 return add_stmt (cs->switch_expr);
276beea2 10061}
10062
e60a6f7b 10063/* Process a case label at location LOC. */
276beea2 10064
10065tree
e60a6f7b 10066do_case (location_t loc, tree low_value, tree high_value)
276beea2 10067{
10068 tree label = NULL_TREE;
10069
482c1ea0 10070 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10071 {
10072 low_value = c_fully_fold (low_value, false, NULL);
10073 if (TREE_CODE (low_value) == INTEGER_CST)
c9e51d25 10074 pedwarn (loc, OPT_Wpedantic,
482c1ea0 10075 "case label is not an integer constant expression");
10076 }
10077
10078 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10079 {
10080 high_value = c_fully_fold (high_value, false, NULL);
10081 if (TREE_CODE (high_value) == INTEGER_CST)
29438999 10082 pedwarn (input_location, OPT_Wpedantic,
482c1ea0 10083 "case label is not an integer constant expression");
10084 }
10085
f805d53d 10086 if (c_switch_stack == NULL)
f5d156fd 10087 {
10088 if (low_value)
f805d53d 10089 error_at (loc, "case label not within a switch statement");
f5d156fd 10090 else
f805d53d 10091 error_at (loc, "%<default%> label not within a switch statement");
10092 return NULL_TREE;
f5d156fd 10093 }
fc698c9d 10094
f805d53d 10095 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10096 EXPR_LOCATION (c_switch_stack->switch_expr),
10097 loc))
10098 return NULL_TREE;
10099
10100 label = c_add_case_label (loc, c_switch_stack->cases,
10101 SWITCH_COND (c_switch_stack->switch_expr),
10102 c_switch_stack->orig_type,
be23b16f 10103 low_value, high_value,
10104 &c_switch_stack->outside_range_p);
f805d53d 10105 if (label == error_mark_node)
10106 label = NULL_TREE;
276beea2 10107 return label;
10108}
fc698c9d 10109
5213d6c9 10110/* Finish the switch statement. TYPE is the original type of the
10111 controlling expression of the switch, or NULL_TREE. */
fc698c9d 10112
276beea2 10113void
5213d6c9 10114c_finish_case (tree body, tree type)
276beea2 10115{
0375a275 10116 struct c_switch *cs = c_switch_stack;
e7911019 10117 location_t switch_location;
276beea2 10118
197638d4 10119 SWITCH_BODY (cs->switch_expr) = body;
2363ef00 10120
4ee9c684 10121 /* Emit warnings as needed. */
e60a6f7b 10122 switch_location = EXPR_LOCATION (cs->switch_expr);
e7911019 10123 c_do_switch_warnings (cs->cases, switch_location,
5213d6c9 10124 type ? type : TREE_TYPE (cs->switch_expr),
be23b16f 10125 SWITCH_COND (cs->switch_expr),
10126 cs->bool_cond_p, cs->outside_range_p);
4ee9c684 10127
276beea2 10128 /* Pop the stack. */
0375a275 10129 c_switch_stack = cs->next;
276beea2 10130 splay_tree_delete (cs->cases);
f805d53d 10131 c_release_switch_bindings (cs->bindings);
9318f22c 10132 XDELETE (cs);
fc698c9d 10133}
2363ef00 10134\f
0375a275 10135/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10136 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
0241e4dc 10137 may be null. */
2363ef00 10138
b576c0c1 10139void
0375a275 10140c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
0241e4dc 10141 tree else_block)
2363ef00 10142{
0375a275 10143 tree stmt;
2363ef00 10144
cc92dddc 10145 /* If the condition has array notations, then the rank of the then_block and
10146 else_block must be either 0 or be equal to the rank of the condition. If
10147 the condition does not have array notations then break them up as it is
10148 broken up in a normal expression. */
a89e6c15 10149 if (flag_cilkplus && contains_array_notation_expr (cond))
cc92dddc 10150 {
10151 size_t then_rank = 0, cond_rank = 0, else_rank = 0;
10152 if (!find_rank (if_locus, cond, cond, true, &cond_rank))
10153 return;
10154 if (then_block
10155 && !find_rank (if_locus, then_block, then_block, true, &then_rank))
10156 return;
10157 if (else_block
10158 && !find_rank (if_locus, else_block, else_block, true, &else_rank))
10159 return;
10160 if (cond_rank != then_rank && then_rank != 0)
10161 {
10162 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10163 " and the then-block");
10164 return;
10165 }
10166 else if (cond_rank != else_rank && else_rank != 0)
10167 {
10168 error_at (if_locus, "rank-mismatch between if-statement%'s condition"
10169 " and the else-block");
10170 return;
10171 }
10172 }
b6bbe4a7 10173
fee9f7da 10174 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
2ed8b5d0 10175 SET_EXPR_LOCATION (stmt, if_locus);
0375a275 10176 add_stmt (stmt);
2363ef00 10177}
10178
0375a275 10179/* Emit a general-purpose loop construct. START_LOCUS is the location of
10180 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10181 is false for DO loops. INCR is the FOR increment expression. BODY is
2c763ed4 10182 the statement controlled by the loop. BLAB is the break label. CLAB is
0375a275 10183 the continue label. Everything is allowed to be NULL. */
2363ef00 10184
10185void
0375a275 10186c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10187 tree blab, tree clab, bool cond_is_first)
2363ef00 10188{
0375a275 10189 tree entry = NULL, exit = NULL, t;
10190
fdd735a7 10191 /* In theory could forbid cilk spawn for loop increment expression,
10192 but it should work just fine. */
3c6d4197 10193
3959c2d9 10194 /* If the condition is zero don't generate a loop construct. */
10195 if (cond && integer_zerop (cond))
10196 {
10197 if (cond_is_first)
10198 {
10199 t = build_and_jump (&blab);
10200 SET_EXPR_LOCATION (t, start_locus);
10201 add_stmt (t);
10202 }
10203 }
10204 else
0375a275 10205 {
10206 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
a0c938f0 10207
0375a275 10208 /* If we have an exit condition, then we build an IF with gotos either
a0c938f0 10209 out of the loop, or to the top of it. If there's no exit condition,
10210 then we just build a jump back to the top. */
0375a275 10211 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
a0c938f0 10212
3959c2d9 10213 if (cond && !integer_nonzerop (cond))
a0c938f0 10214 {
10215 /* Canonicalize the loop condition to the end. This means
10216 generating a branch to the loop condition. Reuse the
10217 continue label, if possible. */
10218 if (cond_is_first)
10219 {
10220 if (incr || !clab)
10221 {
10222 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10223 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10224 }
10225 else
10226 t = build1 (GOTO_EXPR, void_type_node, clab);
2ed8b5d0 10227 SET_EXPR_LOCATION (t, start_locus);
a0c938f0 10228 add_stmt (t);
10229 }
10230
0375a275 10231 t = build_and_jump (&blab);
0375a275 10232 if (cond_is_first)
389dd41b 10233 exit = fold_build3_loc (start_locus,
10234 COND_EXPR, void_type_node, cond, exit, t);
0375a275 10235 else
389dd41b 10236 exit = fold_build3_loc (input_location,
10237 COND_EXPR, void_type_node, cond, exit, t);
a0c938f0 10238 }
7758d608 10239 else
10240 {
10241 /* For the backward-goto's location of an unconditional loop
10242 use the beginning of the body, or, if there is none, the
10243 top of the loop. */
10244 location_t loc = EXPR_LOCATION (expr_first (body));
10245 if (loc == UNKNOWN_LOCATION)
10246 loc = start_locus;
10247 SET_EXPR_LOCATION (exit, loc);
10248 }
a0c938f0 10249
0375a275 10250 add_stmt (top);
10251 }
a0c938f0 10252
0375a275 10253 if (body)
10254 add_stmt (body);
10255 if (clab)
10256 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10257 if (incr)
10258 add_stmt (incr);
10259 if (entry)
10260 add_stmt (entry);
10261 if (exit)
10262 add_stmt (exit);
10263 if (blab)
10264 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
2363ef00 10265}
2363ef00 10266
10267tree
e60a6f7b 10268c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
2363ef00 10269{
7f0f308d 10270 bool skip;
0375a275 10271 tree label = *label_p;
2363ef00 10272
7f0f308d 10273 /* In switch statements break is sometimes stylistically used after
10274 a return statement. This can lead to spurious warnings about
10275 control reaching the end of a non-void function when it is
10276 inlined. Note that we are calling block_may_fallthru with
10277 language specific tree nodes; this works because
10278 block_may_fallthru returns true when given something it does not
10279 understand. */
10280 skip = !block_may_fallthru (cur_stmt_list);
10281
0375a275 10282 if (!label)
7f0f308d 10283 {
10284 if (!skip)
e60a6f7b 10285 *label_p = label = create_artificial_label (loc);
7f0f308d 10286 }
1e8e9920 10287 else if (TREE_CODE (label) == LABEL_DECL)
10288 ;
f9ae6f95 10289 else switch (TREE_INT_CST_LOW (label))
0375a275 10290 {
1e8e9920 10291 case 0:
0375a275 10292 if (is_break)
e60a6f7b 10293 error_at (loc, "break statement not within loop or switch");
0375a275 10294 else
e60a6f7b 10295 error_at (loc, "continue statement not within a loop");
0375a275 10296 return NULL_TREE;
1e8e9920 10297
10298 case 1:
10299 gcc_assert (is_break);
e60a6f7b 10300 error_at (loc, "break statement used with OpenMP for loop");
1e8e9920 10301 return NULL_TREE;
10302
f2697631 10303 case 2:
10304 if (is_break)
10305 error ("break statement within %<#pragma simd%> loop body");
10306 else
10307 error ("continue statement within %<#pragma simd%> loop body");
10308 return NULL_TREE;
10309
1e8e9920 10310 default:
10311 gcc_unreachable ();
0375a275 10312 }
2363ef00 10313
7f0f308d 10314 if (skip)
10315 return NULL_TREE;
10316
4a1849e3 10317 if (!is_break)
10318 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10319
14ae0310 10320 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
2363ef00 10321}
10322
0375a275 10323/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
2569a1be 10324
10325static void
e60a6f7b 10326emit_side_effect_warnings (location_t loc, tree expr)
2569a1be 10327{
29eda12a 10328 if (expr == error_mark_node)
10329 ;
10330 else if (!TREE_SIDE_EFFECTS (expr))
2569a1be 10331 {
10332 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
e60a6f7b 10333 warning_at (loc, OPT_Wunused_value, "statement with no effect");
2569a1be 10334 }
9a9db3c1 10335 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10336 {
10337 tree r = expr;
10338 location_t cloc = loc;
10339 while (TREE_CODE (r) == COMPOUND_EXPR)
10340 {
10341 if (EXPR_HAS_LOCATION (r))
10342 cloc = EXPR_LOCATION (r);
10343 r = TREE_OPERAND (r, 1);
10344 }
10345 if (!TREE_SIDE_EFFECTS (r)
10346 && !VOID_TYPE_P (TREE_TYPE (r))
10347 && !CONVERT_EXPR_P (r)
977b1853 10348 && !TREE_NO_WARNING (r)
9a9db3c1 10349 && !TREE_NO_WARNING (expr))
10350 warning_at (cloc, OPT_Wunused_value,
10351 "right-hand operand of comma expression has no effect");
10352 }
43667bd3 10353 else
e60a6f7b 10354 warn_if_unused_value (expr, loc);
2569a1be 10355}
10356
0375a275 10357/* Process an expression as if it were a complete statement. Emit
e60a6f7b 10358 diagnostics, but do not call ADD_STMT. LOC is the location of the
10359 statement. */
2569a1be 10360
0375a275 10361tree
e60a6f7b 10362c_process_expr_stmt (location_t loc, tree expr)
2569a1be 10363{
b2af2a93 10364 tree exprv;
10365
2569a1be 10366 if (!expr)
0375a275 10367 return NULL_TREE;
2569a1be 10368
a75b1c71 10369 expr = c_fully_fold (expr, false, NULL);
10370
2569a1be 10371 if (warn_sequence_point)
10372 verify_sequence_points (expr);
10373
10374 if (TREE_TYPE (expr) != error_mark_node
10375 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10376 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
e60a6f7b 10377 error_at (loc, "expression statement has incomplete type");
2569a1be 10378
10379 /* If we're not processing a statement expression, warn about unused values.
10380 Warnings for statement expressions will be emitted later, once we figure
10381 out which is the result. */
10382 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
43667bd3 10383 && warn_unused_value)
1d65bac2 10384 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
2569a1be 10385
b2af2a93 10386 exprv = expr;
10387 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10388 exprv = TREE_OPERAND (exprv, 1);
0902fd73 10389 while (CONVERT_EXPR_P (exprv))
10390 exprv = TREE_OPERAND (exprv, 0);
10391 if (DECL_P (exprv)
10392 || handled_component_p (exprv)
10393 || TREE_CODE (exprv) == ADDR_EXPR)
b2af2a93 10394 mark_exp_read (exprv);
7cb9c999 10395
2569a1be 10396 /* If the expression is not of a type to which we cannot assign a line
10397 number, wrap the thing in a no-op NOP_EXPR. */
ce45a448 10398 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
e60a6f7b 10399 {
10400 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10401 SET_EXPR_LOCATION (expr, loc);
10402 }
0375a275 10403
10404 return expr;
10405}
10406
e60a6f7b 10407/* Emit an expression as a statement. LOC is the location of the
10408 expression. */
0375a275 10409
10410tree
e60a6f7b 10411c_finish_expr_stmt (location_t loc, tree expr)
0375a275 10412{
10413 if (expr)
e60a6f7b 10414 return add_stmt (c_process_expr_stmt (loc, expr));
0375a275 10415 else
10416 return NULL;
2569a1be 10417}
10418
10419/* Do the opposite and emit a statement as an expression. To begin,
10420 create a new binding level and return it. */
2363ef00 10421
10422tree
10423c_begin_stmt_expr (void)
10424{
10425 tree ret;
10426
10427 /* We must force a BLOCK for this level so that, if it is not expanded
10428 later, there is a way to turn off the entire subtree of blocks that
10429 are contained in it. */
10430 keep_next_level ();
10431 ret = c_begin_compound_stmt (true);
f805d53d 10432
10433 c_bindings_start_stmt_expr (c_switch_stack == NULL
10434 ? NULL
10435 : c_switch_stack->bindings);
2363ef00 10436
10437 /* Mark the current statement list as belonging to a statement list. */
10438 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10439
10440 return ret;
10441}
10442
e60a6f7b 10443/* LOC is the location of the compound statement to which this body
10444 belongs. */
10445
2363ef00 10446tree
e60a6f7b 10447c_finish_stmt_expr (location_t loc, tree body)
2363ef00 10448{
2569a1be 10449 tree last, type, tmp, val;
2363ef00 10450 tree *last_p;
10451
e60a6f7b 10452 body = c_end_compound_stmt (loc, body, true);
f805d53d 10453
10454 c_bindings_end_stmt_expr (c_switch_stack == NULL
10455 ? NULL
10456 : c_switch_stack->bindings);
2363ef00 10457
2569a1be 10458 /* Locate the last statement in BODY. See c_end_compound_stmt
10459 about always returning a BIND_EXPR. */
10460 last_p = &BIND_EXPR_BODY (body);
10461 last = BIND_EXPR_BODY (body);
10462
10463 continue_searching:
2363ef00 10464 if (TREE_CODE (last) == STATEMENT_LIST)
10465 {
2569a1be 10466 tree_stmt_iterator i;
10467
10468 /* This can happen with degenerate cases like ({ }). No value. */
10469 if (!TREE_SIDE_EFFECTS (last))
10470 return body;
10471
10472 /* If we're supposed to generate side effects warnings, process
10473 all of the statements except the last. */
43667bd3 10474 if (warn_unused_value)
2363ef00 10475 {
2569a1be 10476 for (i = tsi_start (last); !tsi_one_before_end_p (i); tsi_next (&i))
e60a6f7b 10477 {
10478 location_t tloc;
10479 tree t = tsi_stmt (i);
10480
10481 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10482 emit_side_effect_warnings (tloc, t);
10483 }
2363ef00 10484 }
10485 else
2569a1be 10486 i = tsi_last (last);
10487 last_p = tsi_stmt_ptr (i);
10488 last = *last_p;
2363ef00 10489 }
10490
2569a1be 10491 /* If the end of the list is exception related, then the list was split
10492 by a call to push_cleanup. Continue searching. */
10493 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10494 || TREE_CODE (last) == TRY_CATCH_EXPR)
10495 {
10496 last_p = &TREE_OPERAND (last, 0);
10497 last = *last_p;
10498 goto continue_searching;
10499 }
10500
9fa2814c 10501 if (last == error_mark_node)
10502 return last;
10503
2569a1be 10504 /* In the case that the BIND_EXPR is not necessary, return the
10505 expression out from inside it. */
9fa2814c 10506 if (last == BIND_EXPR_BODY (body)
10507 && BIND_EXPR_VARS (body) == NULL)
1c3d3e7c 10508 {
a75b1c71 10509 /* Even if this looks constant, do not allow it in a constant
10510 expression. */
f59e3889 10511 last = c_wrap_maybe_const (last, true);
1c3d3e7c 10512 /* Do not warn if the return value of a statement expression is
10513 unused. */
a75b1c71 10514 TREE_NO_WARNING (last) = 1;
1c3d3e7c 10515 return last;
10516 }
2363ef00 10517
10518 /* Extract the type of said expression. */
10519 type = TREE_TYPE (last);
2363ef00 10520
2569a1be 10521 /* If we're not returning a value at all, then the BIND_EXPR that
10522 we already have is a fine expression to return. */
10523 if (!type || VOID_TYPE_P (type))
10524 return body;
10525
10526 /* Now that we've located the expression containing the value, it seems
10527 silly to make voidify_wrapper_expr repeat the process. Create a
10528 temporary of the appropriate type and stick it in a TARGET_EXPR. */
f9e245b2 10529 tmp = create_tmp_var_raw (type);
2569a1be 10530
10531 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10532 tree_expr_nonnegative_p giving up immediately. */
10533 val = last;
10534 if (TREE_CODE (val) == NOP_EXPR
10535 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10536 val = TREE_OPERAND (val, 0);
10537
14ae0310 10538 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
1cf1742e 10539 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
2569a1be 10540
e60a6f7b 10541 {
10542 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10543 SET_EXPR_LOCATION (t, loc);
10544 return t;
10545 }
2363ef00 10546}
10547\f
10548/* Begin and end compound statements. This is as simple as pushing
10549 and popping new statement lists from the tree. */
10550
10551tree
10552c_begin_compound_stmt (bool do_scope)
10553{
10554 tree stmt = push_stmt_list ();
10555 if (do_scope)
735f4358 10556 push_scope ();
2363ef00 10557 return stmt;
10558}
10559
e60a6f7b 10560/* End a compound statement. STMT is the statement. LOC is the
10561 location of the compound statement-- this is usually the location
10562 of the opening brace. */
10563
2363ef00 10564tree
e60a6f7b 10565c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
2363ef00 10566{
10567 tree block = NULL;
10568
10569 if (do_scope)
10570 {
10571 if (c_dialect_objc ())
10572 objc_clear_super_receiver ();
10573 block = pop_scope ();
10574 }
10575
10576 stmt = pop_stmt_list (stmt);
e60a6f7b 10577 stmt = c_build_bind_expr (loc, block, stmt);
2363ef00 10578
10579 /* If this compound statement is nested immediately inside a statement
10580 expression, then force a BIND_EXPR to be created. Otherwise we'll
10581 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10582 STATEMENT_LISTs merge, and thus we can lose track of what statement
10583 was really last. */
cacfdc02 10584 if (building_stmt_list_p ()
2363ef00 10585 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10586 && TREE_CODE (stmt) != BIND_EXPR)
10587 {
14ae0310 10588 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
2363ef00 10589 TREE_SIDE_EFFECTS (stmt) = 1;
e60a6f7b 10590 SET_EXPR_LOCATION (stmt, loc);
2363ef00 10591 }
10592
10593 return stmt;
10594}
dddab69e 10595
10596/* Queue a cleanup. CLEANUP is an expression/statement to be executed
10597 when the current scope is exited. EH_ONLY is true when this is not
10598 meant to apply to normal control flow transfer. */
10599
10600void
e60a6f7b 10601push_cleanup (tree decl, tree cleanup, bool eh_only)
dddab69e 10602{
2569a1be 10603 enum tree_code code;
10604 tree stmt, list;
10605 bool stmt_expr;
10606
10607 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
e60a6f7b 10608 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
dddab69e 10609 add_stmt (stmt);
2569a1be 10610 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
10611 list = push_stmt_list ();
10612 TREE_OPERAND (stmt, 0) = list;
10613 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
dddab69e 10614}
2363ef00 10615\f
1f137e6d 10616/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
10617 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
10618
10619static tree
10620build_vec_cmp (tree_code code, tree type,
10621 tree arg0, tree arg1)
10622{
10623 tree zero_vec = build_zero_cst (type);
10624 tree minus_one_vec = build_minus_one_cst (type);
10625 tree cmp_type = build_same_sized_truth_vector_type (type);
10626 tree cmp = build2 (code, cmp_type, arg0, arg1);
10627 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
10628}
10629
276beea2 10630/* Build a binary-operation expression without default conversions.
10631 CODE is the kind of expression to build.
8e70fb09 10632 LOCATION is the operator's location.
276beea2 10633 This function differs from `build' in several ways:
10634 the data type of the result is computed and recorded in it,
10635 warnings are generated if arg data types are invalid,
10636 special handling for addition and subtraction of pointers is known,
10637 and some optimization is done (operations on narrow ints
10638 are done in the narrower type when that gives the same result).
10639 Constant folding is also done before the result is returned.
fc698c9d 10640
276beea2 10641 Note that the operands will never have enumeral types, or function
10642 or array types, because either they will have the default conversions
10643 performed or they have both just been converted to some other type in which
10644 the arithmetic is to be done. */
10645
10646tree
8e70fb09 10647build_binary_op (location_t location, enum tree_code code,
10648 tree orig_op0, tree orig_op1, int convert_p)
fc698c9d 10649{
c6418a4e 10650 tree type0, type1, orig_type0, orig_type1;
10651 tree eptype;
276beea2 10652 enum tree_code code0, code1;
10653 tree op0, op1;
b6889cb0 10654 tree ret = error_mark_node;
7a979707 10655 const char *invalid_op_diag;
9a8bed72 10656 bool op0_int_operands, op1_int_operands;
a75b1c71 10657 bool int_const, int_const_or_overflow, int_operands;
554732cd 10658
276beea2 10659 /* Expression code to give to the expression when it is built.
10660 Normally this is CODE, which is what the caller asked for,
10661 but in some special cases we change it. */
10662 enum tree_code resultcode = code;
989bbcfc 10663
276beea2 10664 /* Data type in which the computation is to be performed.
10665 In the simplest cases this is the common type of the arguments. */
10666 tree result_type = NULL;
10667
c6418a4e 10668 /* When the computation is in excess precision, the type of the
10669 final EXCESS_PRECISION_EXPR. */
8f45ab4d 10670 tree semantic_result_type = NULL;
c6418a4e 10671
276beea2 10672 /* Nonzero means operands have already been type-converted
10673 in whatever way is necessary.
10674 Zero means they need to be converted to RESULT_TYPE. */
10675 int converted = 0;
10676
10677 /* Nonzero means create the expression with this type, rather than
10678 RESULT_TYPE. */
10679 tree build_type = 0;
10680
10681 /* Nonzero means after finally constructing the expression
10682 convert it to this type. */
10683 tree final_type = 0;
10684
10685 /* Nonzero if this is an operation like MIN or MAX which can
10686 safely be computed in short if both args are promoted shorts.
10687 Also implies COMMON.
10688 -1 indicates a bitwise operation; this makes a difference
10689 in the exact conditions for when it is safe to do the operation
10690 in a narrower mode. */
10691 int shorten = 0;
10692
10693 /* Nonzero if this is a comparison operation;
10694 if both args are promoted shorts, compare the original shorts.
10695 Also implies COMMON. */
10696 int short_compare = 0;
10697
10698 /* Nonzero if this is a right-shift operation, which can be computed on the
10699 original short and then promoted if the operand is a promoted short. */
10700 int short_shift = 0;
10701
10702 /* Nonzero means set RESULT_TYPE to the common type of the args. */
10703 int common = 0;
10704
34d3c5de 10705 /* True means types are compatible as far as ObjC is concerned. */
10706 bool objc_ok;
10707
c6418a4e 10708 /* True means this is an arithmetic operation that may need excess
10709 precision. */
10710 bool may_need_excess_precision;
10711
16a547ac 10712 /* True means this is a boolean operation that converts both its
10713 operands to truth-values. */
10714 bool boolean_op = false;
10715
9e46467d 10716 /* Remember whether we're doing / or %. */
10717 bool doing_div_or_mod = false;
10718
10719 /* Remember whether we're doing << or >>. */
10720 bool doing_shift = false;
10721
10722 /* Tree holding instrumentation expression. */
10723 tree instrument_expr = NULL;
10724
8e70fb09 10725 if (location == UNKNOWN_LOCATION)
10726 location = input_location;
10727
9a8bed72 10728 op0 = orig_op0;
10729 op1 = orig_op1;
10730
10731 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
10732 if (op0_int_operands)
10733 op0 = remove_c_maybe_const_expr (op0);
10734 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
10735 if (op1_int_operands)
10736 op1 = remove_c_maybe_const_expr (op1);
10737 int_operands = (op0_int_operands && op1_int_operands);
a75b1c71 10738 if (int_operands)
10739 {
10740 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
10741 && TREE_CODE (orig_op1) == INTEGER_CST);
10742 int_const = (int_const_or_overflow
10743 && !TREE_OVERFLOW (orig_op0)
10744 && !TREE_OVERFLOW (orig_op1));
10745 }
10746 else
10747 int_const = int_const_or_overflow = false;
10748
7dfa155b 10749 /* Do not apply default conversion in mixed vector/scalar expression. */
6cf89e04 10750 if (convert_p
6290f0db 10751 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
fcacf125 10752 {
9a8bed72 10753 op0 = default_conversion (op0);
10754 op1 = default_conversion (op1);
fcacf125 10755 }
10756
3c6d4197 10757 /* When Cilk Plus is enabled and there are array notations inside op0, then
10758 we check to see if there are builtin array notation functions. If
10759 so, then we take on the type of the array notation inside it. */
a89e6c15 10760 if (flag_cilkplus && contains_array_notation_expr (op0))
3c6d4197 10761 orig_type0 = type0 = find_correct_array_notation_type (op0);
10762 else
10763 orig_type0 = type0 = TREE_TYPE (op0);
10764
a89e6c15 10765 if (flag_cilkplus && contains_array_notation_expr (op1))
3c6d4197 10766 orig_type1 = type1 = find_correct_array_notation_type (op1);
10767 else
10768 orig_type1 = type1 = TREE_TYPE (op1);
812fff1a 10769
276beea2 10770 /* The expression codes of the data types of the arguments tell us
10771 whether the arguments are integers, floating, pointers, etc. */
10772 code0 = TREE_CODE (type0);
10773 code1 = TREE_CODE (type1);
10774
10775 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
10776 STRIP_TYPE_NOPS (op0);
10777 STRIP_TYPE_NOPS (op1);
10778
10779 /* If an error was already reported for one of the arguments,
10780 avoid reporting another error. */
10781
10782 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
10783 return error_mark_node;
10784
547c6b1f 10785 if (code0 == POINTER_TYPE
10786 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
10787 return error_mark_node;
10788
10789 if (code1 == POINTER_TYPE
10790 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
10791 return error_mark_node;
10792
7a979707 10793 if ((invalid_op_diag
10794 = targetm.invalid_binary_op (code, type0, type1)))
10795 {
8e70fb09 10796 error_at (location, invalid_op_diag);
7a979707 10797 return error_mark_node;
10798 }
10799
c6418a4e 10800 switch (code)
10801 {
10802 case PLUS_EXPR:
10803 case MINUS_EXPR:
10804 case MULT_EXPR:
10805 case TRUNC_DIV_EXPR:
10806 case CEIL_DIV_EXPR:
10807 case FLOOR_DIV_EXPR:
10808 case ROUND_DIV_EXPR:
10809 case EXACT_DIV_EXPR:
10810 may_need_excess_precision = true;
10811 break;
10812 default:
10813 may_need_excess_precision = false;
10814 break;
10815 }
10816 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
10817 {
10818 op0 = TREE_OPERAND (op0, 0);
10819 type0 = TREE_TYPE (op0);
10820 }
10821 else if (may_need_excess_precision
10822 && (eptype = excess_precision_type (type0)) != NULL_TREE)
10823 {
10824 type0 = eptype;
10825 op0 = convert (eptype, op0);
10826 }
10827 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
10828 {
10829 op1 = TREE_OPERAND (op1, 0);
10830 type1 = TREE_TYPE (op1);
10831 }
10832 else if (may_need_excess_precision
10833 && (eptype = excess_precision_type (type1)) != NULL_TREE)
10834 {
10835 type1 = eptype;
10836 op1 = convert (eptype, op1);
10837 }
10838
34d3c5de 10839 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
10840
7dfa155b 10841 /* In case when one of the operands of the binary operation is
10842 a vector and another is a scalar -- convert scalar to vector. */
10843 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
10844 {
41ed701a 10845 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
10846 true);
6cf89e04 10847
7dfa155b 10848 switch (convert_flag)
10849 {
10850 case stv_error:
10851 return error_mark_node;
10852 case stv_firstarg:
10853 {
10854 bool maybe_const = true;
10855 tree sc;
10856 sc = c_fully_fold (op0, false, &maybe_const);
10857 sc = save_expr (sc);
10858 sc = convert (TREE_TYPE (type1), sc);
10859 op0 = build_vector_from_val (type1, sc);
10860 if (!maybe_const)
10861 op0 = c_wrap_maybe_const (op0, true);
10862 orig_type0 = type0 = TREE_TYPE (op0);
10863 code0 = TREE_CODE (type0);
10864 converted = 1;
10865 break;
10866 }
10867 case stv_secondarg:
10868 {
10869 bool maybe_const = true;
10870 tree sc;
10871 sc = c_fully_fold (op1, false, &maybe_const);
10872 sc = save_expr (sc);
10873 sc = convert (TREE_TYPE (type0), sc);
10874 op1 = build_vector_from_val (type0, sc);
10875 if (!maybe_const)
104619f5 10876 op1 = c_wrap_maybe_const (op1, true);
7dfa155b 10877 orig_type1 = type1 = TREE_TYPE (op1);
10878 code1 = TREE_CODE (type1);
10879 converted = 1;
10880 break;
10881 }
10882 default:
10883 break;
10884 }
10885 }
10886
276beea2 10887 switch (code)
fc698c9d 10888 {
276beea2 10889 case PLUS_EXPR:
10890 /* Handle the pointer + int case. */
10891 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
b6889cb0 10892 {
389dd41b 10893 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
b6889cb0 10894 goto return_build_binary_op;
10895 }
276beea2 10896 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
b6889cb0 10897 {
389dd41b 10898 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
b6889cb0 10899 goto return_build_binary_op;
10900 }
16d7533c 10901 else
276beea2 10902 common = 1;
10903 break;
628fa4f8 10904
276beea2 10905 case MINUS_EXPR:
10906 /* Subtraction of two similar pointers.
10907 We must subtract them as integers, then divide by object size. */
10908 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
d5b637fa 10909 && comp_target_types (location, type0, type1))
b6889cb0 10910 {
389dd41b 10911 ret = pointer_diff (location, op0, op1);
b6889cb0 10912 goto return_build_binary_op;
10913 }
276beea2 10914 /* Handle pointer minus int. Just like pointer plus int. */
10915 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
b6889cb0 10916 {
389dd41b 10917 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
b6889cb0 10918 goto return_build_binary_op;
10919 }
276beea2 10920 else
10921 common = 1;
10922 break;
989bbcfc 10923
276beea2 10924 case MULT_EXPR:
10925 common = 1;
10926 break;
10927
10928 case TRUNC_DIV_EXPR:
10929 case CEIL_DIV_EXPR:
10930 case FLOOR_DIV_EXPR:
10931 case ROUND_DIV_EXPR:
10932 case EXACT_DIV_EXPR:
9e46467d 10933 doing_div_or_mod = true;
b6889cb0 10934 warn_for_div_by_zero (location, op1);
276beea2 10935
10936 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9421ebb9 10937 || code0 == FIXED_POINT_TYPE
276beea2 10938 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
10939 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9421ebb9 10940 || code1 == FIXED_POINT_TYPE
276beea2 10941 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
628fa4f8 10942 {
491255f5 10943 enum tree_code tcode0 = code0, tcode1 = code1;
10944
4418a1d4 10945 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
491255f5 10946 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
4418a1d4 10947 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
491255f5 10948 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
4418a1d4 10949
9421ebb9 10950 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
10951 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
276beea2 10952 resultcode = RDIV_EXPR;
10953 else
10954 /* Although it would be tempting to shorten always here, that
10955 loses on some targets, since the modulo instruction is
10956 undefined if the quotient can't be represented in the
10957 computation mode. We shorten only if unsigned or if
10958 dividing by something we know != -1. */
78a8ed03 10959 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
276beea2 10960 || (TREE_CODE (op1) == INTEGER_CST
84166705 10961 && !integer_all_onesp (op1)));
276beea2 10962 common = 1;
10963 }
10964 break;
fc698c9d 10965
276beea2 10966 case BIT_AND_EXPR:
276beea2 10967 case BIT_IOR_EXPR:
10968 case BIT_XOR_EXPR:
10969 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
10970 shorten = -1;
f8629d35 10971 /* Allow vector types which are not floating point types. */
10972 else if (code0 == VECTOR_TYPE
10973 && code1 == VECTOR_TYPE
10974 && !VECTOR_FLOAT_TYPE_P (type0)
10975 && !VECTOR_FLOAT_TYPE_P (type1))
276beea2 10976 common = 1;
10977 break;
10978
10979 case TRUNC_MOD_EXPR:
10980 case FLOOR_MOD_EXPR:
9e46467d 10981 doing_div_or_mod = true;
b6889cb0 10982 warn_for_div_by_zero (location, op1);
fc698c9d 10983
b6d7101e 10984 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
10985 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
10986 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
10987 common = 1;
10988 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
276beea2 10989 {
10990 /* Although it would be tempting to shorten always here, that loses
10991 on some targets, since the modulo instruction is undefined if the
10992 quotient can't be represented in the computation mode. We shorten
10993 only if unsigned or if dividing by something we know != -1. */
78a8ed03 10994 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
276beea2 10995 || (TREE_CODE (op1) == INTEGER_CST
84166705 10996 && !integer_all_onesp (op1)));
276beea2 10997 common = 1;
10998 }
10999 break;
fc698c9d 11000
276beea2 11001 case TRUTH_ANDIF_EXPR:
11002 case TRUTH_ORIF_EXPR:
11003 case TRUTH_AND_EXPR:
11004 case TRUTH_OR_EXPR:
11005 case TRUTH_XOR_EXPR:
11006 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
9421ebb9 11007 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11008 || code0 == FIXED_POINT_TYPE)
276beea2 11009 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
9421ebb9 11010 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11011 || code1 == FIXED_POINT_TYPE))
276beea2 11012 {
11013 /* Result of these operations is always an int,
11014 but that does not mean the operands should be
11015 converted to ints! */
11016 result_type = integer_type_node;
bb554051 11017 if (op0_int_operands)
11018 {
11019 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11020 op0 = remove_c_maybe_const_expr (op0);
11021 }
11022 else
11023 op0 = c_objc_common_truthvalue_conversion (location, op0);
11024 if (op1_int_operands)
11025 {
11026 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11027 op1 = remove_c_maybe_const_expr (op1);
11028 }
11029 else
11030 op1 = c_objc_common_truthvalue_conversion (location, op1);
276beea2 11031 converted = 1;
16a547ac 11032 boolean_op = true;
276beea2 11033 }
a75b1c71 11034 if (code == TRUTH_ANDIF_EXPR)
11035 {
11036 int_const_or_overflow = (int_operands
11037 && TREE_CODE (orig_op0) == INTEGER_CST
11038 && (op0 == truthvalue_false_node
11039 || TREE_CODE (orig_op1) == INTEGER_CST));
11040 int_const = (int_const_or_overflow
11041 && !TREE_OVERFLOW (orig_op0)
11042 && (op0 == truthvalue_false_node
11043 || !TREE_OVERFLOW (orig_op1)));
11044 }
11045 else if (code == TRUTH_ORIF_EXPR)
11046 {
11047 int_const_or_overflow = (int_operands
11048 && TREE_CODE (orig_op0) == INTEGER_CST
11049 && (op0 == truthvalue_true_node
11050 || TREE_CODE (orig_op1) == INTEGER_CST));
11051 int_const = (int_const_or_overflow
11052 && !TREE_OVERFLOW (orig_op0)
11053 && (op0 == truthvalue_true_node
11054 || !TREE_OVERFLOW (orig_op1)));
11055 }
276beea2 11056 break;
cad0b4b0 11057
276beea2 11058 /* Shift operations: result has same type as first operand;
11059 always convert second operand to int.
11060 Also set SHORT_SHIFT if shifting rightward. */
fc698c9d 11061
276beea2 11062 case RSHIFT_EXPR:
f3c032a4 11063 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11064 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11065 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11066 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
191b2c61 11067 {
11068 result_type = type0;
11069 converted = 1;
11070 }
f3c032a4 11071 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11072 || code0 == VECTOR_TYPE)
8d669e79 11073 && code1 == INTEGER_TYPE)
276beea2 11074 {
9e46467d 11075 doing_shift = true;
a75b1c71 11076 if (TREE_CODE (op1) == INTEGER_CST)
554732cd 11077 {
276beea2 11078 if (tree_int_cst_sgn (op1) < 0)
a75b1c71 11079 {
11080 int_const = false;
48d94ede 11081 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11082 warning_at (location, OPT_Wshift_count_negative,
11083 "right shift count is negative");
a75b1c71 11084 }
f3c032a4 11085 else if (code0 == VECTOR_TYPE)
11086 {
11087 if (compare_tree_int (op1,
11088 TYPE_PRECISION (TREE_TYPE (type0)))
11089 >= 0)
11090 {
11091 int_const = false;
11092 if (c_inhibit_evaluation_warnings == 0)
11093 warning_at (location, OPT_Wshift_count_overflow,
11094 "right shift count >= width of vector element");
11095 }
11096 }
276beea2 11097 else
66fb954a 11098 {
84166705 11099 if (!integer_zerop (op1))
276beea2 11100 short_shift = 1;
11101
11102 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
a75b1c71 11103 {
11104 int_const = false;
48d94ede 11105 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11106 warning_at (location, OPT_Wshift_count_overflow,
11107 "right shift count >= width of type");
a75b1c71 11108 }
66fb954a 11109 }
554732cd 11110 }
fc698c9d 11111
276beea2 11112 /* Use the type of the value to be shifted. */
11113 result_type = type0;
276beea2 11114 /* Avoid converting op1 to result_type later. */
11115 converted = 1;
628fa4f8 11116 }
276beea2 11117 break;
fa6ddf23 11118
276beea2 11119 case LSHIFT_EXPR:
f3c032a4 11120 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11121 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11122 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
11123 && TYPE_VECTOR_SUBPARTS (type0) == TYPE_VECTOR_SUBPARTS (type1))
191b2c61 11124 {
11125 result_type = type0;
11126 converted = 1;
11127 }
f3c032a4 11128 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
11129 || code0 == VECTOR_TYPE)
8d669e79 11130 && code1 == INTEGER_TYPE)
276beea2 11131 {
9e46467d 11132 doing_shift = true;
cdc64059 11133 if (TREE_CODE (op0) == INTEGER_CST
11134 && tree_int_cst_sgn (op0) < 0)
11135 {
11136 /* Don't reject a left shift of a negative value in a context
11137 where a constant expression is needed in C90. */
11138 if (flag_isoc99)
11139 int_const = false;
11140 if (c_inhibit_evaluation_warnings == 0)
11141 warning_at (location, OPT_Wshift_negative_value,
11142 "left shift of negative value");
11143 }
a75b1c71 11144 if (TREE_CODE (op1) == INTEGER_CST)
fc698c9d 11145 {
276beea2 11146 if (tree_int_cst_sgn (op1) < 0)
a75b1c71 11147 {
11148 int_const = false;
48d94ede 11149 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11150 warning_at (location, OPT_Wshift_count_negative,
11151 "left shift count is negative");
a75b1c71 11152 }
f3c032a4 11153 else if (code0 == VECTOR_TYPE)
11154 {
11155 if (compare_tree_int (op1,
11156 TYPE_PRECISION (TREE_TYPE (type0)))
11157 >= 0)
11158 {
11159 int_const = false;
11160 if (c_inhibit_evaluation_warnings == 0)
11161 warning_at (location, OPT_Wshift_count_overflow,
11162 "left shift count >= width of vector element");
11163 }
11164 }
276beea2 11165 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
a75b1c71 11166 {
11167 int_const = false;
48d94ede 11168 if (c_inhibit_evaluation_warnings == 0)
0feb4de1 11169 warning_at (location, OPT_Wshift_count_overflow,
11170 "left shift count >= width of type");
a75b1c71 11171 }
8d669e79 11172 else if (TREE_CODE (op0) == INTEGER_CST
11173 && maybe_warn_shift_overflow (location, op0, op1)
11174 && flag_isoc99)
11175 int_const = false;
d354d271 11176 }
fc698c9d 11177
276beea2 11178 /* Use the type of the value to be shifted. */
11179 result_type = type0;
276beea2 11180 /* Avoid converting op1 to result_type later. */
11181 converted = 1;
628fa4f8 11182 }
276beea2 11183 break;
fc698c9d 11184
276beea2 11185 case EQ_EXPR:
11186 case NE_EXPR:
d7ad16c2 11187 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11188 {
11189 tree intt;
73437615 11190 if (!vector_types_compatible_elements_p (type0, type1))
d7ad16c2 11191 {
11192 error_at (location, "comparing vectors with different "
11193 "element types");
11194 return error_mark_node;
11195 }
11196
11197 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11198 {
11199 error_at (location, "comparing vectors with different "
11200 "number of elements");
11201 return error_mark_node;
11202 }
11203
fee5e491 11204 /* It's not precisely specified how the usual arithmetic
11205 conversions apply to the vector types. Here, we use
11206 the unsigned type if one of the operands is signed and
11207 the other one is unsigned. */
11208 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11209 {
11210 if (!TYPE_UNSIGNED (type0))
11211 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11212 else
11213 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11214 warning_at (location, OPT_Wsign_compare, "comparison between "
11215 "types %qT and %qT", type0, type1);
11216 }
11217
d7ad16c2 11218 /* Always construct signed integer vector type. */
11219 intt = c_common_type_for_size (GET_MODE_BITSIZE
11220 (TYPE_MODE (TREE_TYPE (type0))), 0);
11221 result_type = build_opaque_vector_type (intt,
11222 TYPE_VECTOR_SUBPARTS (type0));
11223 converted = 1;
1f137e6d 11224 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11225 goto return_build_binary_op;
d7ad16c2 11226 }
067d82a2 11227 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
8e70fb09 11228 warning_at (location,
11229 OPT_Wfloat_equal,
11230 "comparing floating point with == or != is unsafe");
276beea2 11231 /* Result of comparison is always int,
11232 but don't convert the args to int! */
11233 build_type = integer_type_node;
11234 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
9421ebb9 11235 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
276beea2 11236 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
9421ebb9 11237 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
276beea2 11238 short_compare = 1;
bf8949d1 11239 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11240 {
11241 if (TREE_CODE (op0) == ADDR_EXPR
a64030fe 11242 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11243 && !from_macro_expansion_at (location))
bf8949d1 11244 {
11245 if (code == EQ_EXPR)
11246 warning_at (location,
11247 OPT_Waddress,
11248 "the comparison will always evaluate as %<false%> "
11249 "for the address of %qD will never be NULL",
11250 TREE_OPERAND (op0, 0));
11251 else
11252 warning_at (location,
11253 OPT_Waddress,
11254 "the comparison will always evaluate as %<true%> "
11255 "for the address of %qD will never be NULL",
11256 TREE_OPERAND (op0, 0));
11257 }
11258 result_type = type0;
11259 }
11260 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11261 {
11262 if (TREE_CODE (op1) == ADDR_EXPR
a64030fe 11263 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11264 && !from_macro_expansion_at (location))
bf8949d1 11265 {
11266 if (code == EQ_EXPR)
11267 warning_at (location,
6cf89e04 11268 OPT_Waddress,
bf8949d1 11269 "the comparison will always evaluate as %<false%> "
11270 "for the address of %qD will never be NULL",
11271 TREE_OPERAND (op1, 0));
11272 else
11273 warning_at (location,
11274 OPT_Waddress,
11275 "the comparison will always evaluate as %<true%> "
11276 "for the address of %qD will never be NULL",
11277 TREE_OPERAND (op1, 0));
11278 }
11279 result_type = type1;
11280 }
276beea2 11281 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11282 {
11283 tree tt0 = TREE_TYPE (type0);
11284 tree tt1 = TREE_TYPE (type1);
6d5d708e 11285 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11286 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11287 addr_space_t as_common = ADDR_SPACE_GENERIC;
11288
276beea2 11289 /* Anything compares with void *. void * compares with anything.
11290 Otherwise, the targets must be compatible
11291 and both must be object or both incomplete. */
d5b637fa 11292 if (comp_target_types (location, type0, type1))
1ea7fa60 11293 result_type = common_pointer_type (type0, type1);
6d5d708e 11294 else if (!addr_space_superset (as0, as1, &as_common))
11295 {
11296 error_at (location, "comparison of pointers to "
11297 "disjoint address spaces");
11298 return error_mark_node;
11299 }
b560fabd 11300 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
5e4a4d15 11301 {
6d5d708e 11302 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
29438999 11303 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
8864917d 11304 "comparison of %<void *%> with function pointer");
5e4a4d15 11305 }
b560fabd 11306 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
0ad236c2 11307 {
6d5d708e 11308 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
29438999 11309 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
8864917d 11310 "comparison of %<void *%> with function pointer");
0ad236c2 11311 }
276beea2 11312 else
34d3c5de 11313 /* Avoid warning about the volatile ObjC EH puts on decls. */
11314 if (!objc_ok)
8e70fb09 11315 pedwarn (location, 0,
21ca8540 11316 "comparison of distinct pointer types lacks a cast");
0ad236c2 11317
276beea2 11318 if (result_type == NULL_TREE)
6d5d708e 11319 {
11320 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11321 result_type = build_pointer_type
11322 (build_qualified_type (void_type_node, qual));
11323 }
0ad236c2 11324 }
276beea2 11325 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
fc698c9d 11326 {
276beea2 11327 result_type = type0;
8e70fb09 11328 pedwarn (location, 0, "comparison between pointer and integer");
fc698c9d 11329 }
276beea2 11330 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
989bbcfc 11331 {
276beea2 11332 result_type = type1;
8e70fb09 11333 pedwarn (location, 0, "comparison between pointer and integer");
989bbcfc 11334 }
78bf4156 11335 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11336 || truth_value_p (TREE_CODE (orig_op0)))
11337 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11338 || truth_value_p (TREE_CODE (orig_op1))))
11339 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
276beea2 11340 break;
989bbcfc 11341
276beea2 11342 case LE_EXPR:
11343 case GE_EXPR:
11344 case LT_EXPR:
11345 case GT_EXPR:
d7ad16c2 11346 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11347 {
11348 tree intt;
73437615 11349 if (!vector_types_compatible_elements_p (type0, type1))
d7ad16c2 11350 {
11351 error_at (location, "comparing vectors with different "
11352 "element types");
11353 return error_mark_node;
11354 }
11355
11356 if (TYPE_VECTOR_SUBPARTS (type0) != TYPE_VECTOR_SUBPARTS (type1))
11357 {
11358 error_at (location, "comparing vectors with different "
11359 "number of elements");
11360 return error_mark_node;
11361 }
11362
fee5e491 11363 /* It's not precisely specified how the usual arithmetic
11364 conversions apply to the vector types. Here, we use
11365 the unsigned type if one of the operands is signed and
11366 the other one is unsigned. */
11367 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11368 {
11369 if (!TYPE_UNSIGNED (type0))
11370 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11371 else
11372 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11373 warning_at (location, OPT_Wsign_compare, "comparison between "
11374 "types %qT and %qT", type0, type1);
11375 }
11376
d7ad16c2 11377 /* Always construct signed integer vector type. */
11378 intt = c_common_type_for_size (GET_MODE_BITSIZE
11379 (TYPE_MODE (TREE_TYPE (type0))), 0);
11380 result_type = build_opaque_vector_type (intt,
11381 TYPE_VECTOR_SUBPARTS (type0));
11382 converted = 1;
1f137e6d 11383 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11384 goto return_build_binary_op;
d7ad16c2 11385 }
276beea2 11386 build_type = integer_type_node;
9421ebb9 11387 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11388 || code0 == FIXED_POINT_TYPE)
11389 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11390 || code1 == FIXED_POINT_TYPE))
276beea2 11391 short_compare = 1;
11392 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11393 {
6d5d708e 11394 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11395 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11396 addr_space_t as_common;
11397
d5b637fa 11398 if (comp_target_types (location, type0, type1))
276beea2 11399 {
1ea7fa60 11400 result_type = common_pointer_type (type0, type1);
276beea2 11401 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11402 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
8e70fb09 11403 pedwarn (location, 0,
21ca8540 11404 "comparison of complete and incomplete pointers");
8864917d 11405 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
29438999 11406 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
8864917d 11407 "ordered comparisons of pointers to functions");
cdd6b2f1 11408 else if (null_pointer_constant_p (orig_op0)
11409 || null_pointer_constant_p (orig_op1))
11410 warning_at (location, OPT_Wextra,
11411 "ordered comparison of pointer with null pointer");
11412
276beea2 11413 }
6d5d708e 11414 else if (!addr_space_superset (as0, as1, &as_common))
11415 {
11416 error_at (location, "comparison of pointers to "
11417 "disjoint address spaces");
11418 return error_mark_node;
11419 }
276beea2 11420 else
11421 {
6d5d708e 11422 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11423 result_type = build_pointer_type
11424 (build_qualified_type (void_type_node, qual));
8e70fb09 11425 pedwarn (location, 0,
21ca8540 11426 "comparison of distinct pointer types lacks a cast");
276beea2 11427 }
11428 }
79f925ed 11429 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
276beea2 11430 {
11431 result_type = type0;
8864917d 11432 if (pedantic)
29438999 11433 pedwarn (location, OPT_Wpedantic,
8864917d 11434 "ordered comparison of pointer with integer zero");
11435 else if (extra_warnings)
8e70fb09 11436 warning_at (location, OPT_Wextra,
cdd6b2f1 11437 "ordered comparison of pointer with integer zero");
276beea2 11438 }
79f925ed 11439 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
276beea2 11440 {
11441 result_type = type1;
cdd6b2f1 11442 if (pedantic)
29438999 11443 pedwarn (location, OPT_Wpedantic,
cdd6b2f1 11444 "ordered comparison of pointer with integer zero");
11445 else if (extra_warnings)
11446 warning_at (location, OPT_Wextra,
11447 "ordered comparison of pointer with integer zero");
276beea2 11448 }
11449 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11450 {
11451 result_type = type0;
8e70fb09 11452 pedwarn (location, 0, "comparison between pointer and integer");
276beea2 11453 }
11454 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11455 {
11456 result_type = type1;
8e70fb09 11457 pedwarn (location, 0, "comparison between pointer and integer");
276beea2 11458 }
78bf4156 11459 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11460 || truth_value_p (TREE_CODE (orig_op0)))
11461 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11462 || truth_value_p (TREE_CODE (orig_op1))))
11463 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
276beea2 11464 break;
7cad36f4 11465
276beea2 11466 default:
315ba355 11467 gcc_unreachable ();
f1929933 11468 }
e41f0d80 11469
d1b92730 11470 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11471 return error_mark_node;
11472
491255f5 11473 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11474 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
73437615 11475 || !vector_types_compatible_elements_p (type0, type1)))
491255f5 11476 {
c0bf500c 11477 gcc_rich_location richloc (location);
11478 richloc.maybe_add_expr (orig_op0);
11479 richloc.maybe_add_expr (orig_op1);
11480 binary_op_error (&richloc, code, type0, type1);
491255f5 11481 return error_mark_node;
11482 }
11483
276beea2 11484 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
9421ebb9 11485 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
276beea2 11486 &&
11487 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
9421ebb9 11488 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
628fa4f8 11489 {
0f241d3f 11490 bool first_complex = (code0 == COMPLEX_TYPE);
11491 bool second_complex = (code1 == COMPLEX_TYPE);
11492 int none_complex = (!first_complex && !second_complex);
4ec54c99 11493
276beea2 11494 if (shorten || common || short_compare)
441df341 11495 {
11496 result_type = c_common_type (type0, type1);
c920faa3 11497 do_warn_double_promotion (result_type, type0, type1,
11498 "implicit conversion from %qT to %qT "
11499 "to match other operand of binary "
11500 "expression",
11501 location);
441df341 11502 if (result_type == error_mark_node)
11503 return error_mark_node;
11504 }
628fa4f8 11505
0f241d3f 11506 if (first_complex != second_complex
11507 && (code == PLUS_EXPR
11508 || code == MINUS_EXPR
11509 || code == MULT_EXPR
11510 || (code == TRUNC_DIV_EXPR && first_complex))
11511 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11512 && flag_signed_zeros)
11513 {
11514 /* An operation on mixed real/complex operands must be
11515 handled specially, but the language-independent code can
11516 more easily optimize the plain complex arithmetic if
11517 -fno-signed-zeros. */
11518 tree real_type = TREE_TYPE (result_type);
11519 tree real, imag;
11520 if (type0 != orig_type0 || type1 != orig_type1)
11521 {
11522 gcc_assert (may_need_excess_precision && common);
8f45ab4d 11523 semantic_result_type = c_common_type (orig_type0, orig_type1);
0f241d3f 11524 }
11525 if (first_complex)
11526 {
11527 if (TREE_TYPE (op0) != result_type)
22a75734 11528 op0 = convert_and_check (location, result_type, op0);
0f241d3f 11529 if (TREE_TYPE (op1) != real_type)
22a75734 11530 op1 = convert_and_check (location, real_type, op1);
0f241d3f 11531 }
11532 else
11533 {
11534 if (TREE_TYPE (op0) != real_type)
22a75734 11535 op0 = convert_and_check (location, real_type, op0);
0f241d3f 11536 if (TREE_TYPE (op1) != result_type)
22a75734 11537 op1 = convert_and_check (location, result_type, op1);
0f241d3f 11538 }
11539 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11540 return error_mark_node;
11541 if (first_complex)
11542 {
11543 op0 = c_save_expr (op0);
11544 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
b99cc6da 11545 op0, true);
0f241d3f 11546 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
b99cc6da 11547 op0, true);
0f241d3f 11548 switch (code)
11549 {
11550 case MULT_EXPR:
11551 case TRUNC_DIV_EXPR:
66031ea7 11552 op1 = c_save_expr (op1);
0f241d3f 11553 imag = build2 (resultcode, real_type, imag, op1);
11554 /* Fall through. */
11555 case PLUS_EXPR:
11556 case MINUS_EXPR:
11557 real = build2 (resultcode, real_type, real, op1);
11558 break;
11559 default:
11560 gcc_unreachable();
11561 }
11562 }
11563 else
11564 {
11565 op1 = c_save_expr (op1);
11566 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
b99cc6da 11567 op1, true);
0f241d3f 11568 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
b99cc6da 11569 op1, true);
0f241d3f 11570 switch (code)
11571 {
11572 case MULT_EXPR:
66031ea7 11573 op0 = c_save_expr (op0);
0f241d3f 11574 imag = build2 (resultcode, real_type, op0, imag);
11575 /* Fall through. */
11576 case PLUS_EXPR:
11577 real = build2 (resultcode, real_type, op0, real);
11578 break;
11579 case MINUS_EXPR:
11580 real = build2 (resultcode, real_type, op0, real);
11581 imag = build1 (NEGATE_EXPR, real_type, imag);
11582 break;
11583 default:
11584 gcc_unreachable();
11585 }
11586 }
11587 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
11588 goto return_build_binary_op;
11589 }
11590
276beea2 11591 /* For certain operations (which identify themselves by shorten != 0)
11592 if both args were extended from the same smaller type,
11593 do the arithmetic in that type and then extend.
628fa4f8 11594
276beea2 11595 shorten !=0 and !=1 indicates a bitwise operation.
11596 For them, this optimization is safe only if
11597 both args are zero-extended or both are sign-extended.
11598 Otherwise, we might change the result.
11599 Eg, (short)-1 | (unsigned short)-1 is (int)-1
11600 but calculated in (unsigned short) it would be (unsigned short)-1. */
628fa4f8 11601
276beea2 11602 if (shorten && none_complex)
11603 {
276beea2 11604 final_type = result_type;
48e1416a 11605 result_type = shorten_binary_op (result_type, op0, op1,
2561cea2 11606 shorten == -1);
276beea2 11607 }
30cd880f 11608
276beea2 11609 /* Shifts can be shortened if shifting right. */
2b30d46c 11610
276beea2 11611 if (short_shift)
11612 {
11613 int unsigned_arg;
11614 tree arg0 = get_narrower (op0, &unsigned_arg);
30cd880f 11615
276beea2 11616 final_type = result_type;
17677b84 11617
276beea2 11618 if (arg0 == op0 && final_type == TREE_TYPE (op0))
78a8ed03 11619 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
0dbd1c74 11620
276beea2 11621 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
9e072631 11622 && tree_int_cst_sgn (op1) > 0
276beea2 11623 /* We can shorten only if the shift count is less than the
11624 number of bits in the smaller type size. */
11625 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
11626 /* We cannot drop an unsigned shift after sign-extension. */
78a8ed03 11627 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
276beea2 11628 {
11629 /* Do an unsigned shift if the operand was zero-extended. */
11630 result_type
11631 = c_common_signed_or_unsigned_type (unsigned_arg,
11632 TREE_TYPE (arg0));
11633 /* Convert value-to-be-shifted to that type. */
11634 if (TREE_TYPE (op0) != result_type)
11635 op0 = convert (result_type, op0);
11636 converted = 1;
17677b84 11637 }
30cd880f 11638 }
11639
276beea2 11640 /* Comparison operations are shortened too but differently.
11641 They identify themselves by setting short_compare = 1. */
225ec6aa 11642
276beea2 11643 if (short_compare)
11644 {
11645 /* Don't write &op0, etc., because that would prevent op0
11646 from being kept in a register.
11647 Instead, make copies of the our local variables and
11648 pass the copies by reference, then copy them back afterward. */
11649 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
11650 enum tree_code xresultcode = resultcode;
11651 tree val
2623625f 11652 = shorten_compare (location, &xop0, &xop1, &xresult_type,
11653 &xresultcode);
e41f0d80 11654
276beea2 11655 if (val != 0)
b6889cb0 11656 {
11657 ret = val;
11658 goto return_build_binary_op;
11659 }
e41f0d80 11660
276beea2 11661 op0 = xop0, op1 = xop1;
11662 converted = 1;
11663 resultcode = xresultcode;
e41f0d80 11664
48d94ede 11665 if (c_inhibit_evaluation_warnings == 0)
a75b1c71 11666 {
11667 bool op0_maybe_const = true;
11668 bool op1_maybe_const = true;
11669 tree orig_op0_folded, orig_op1_folded;
11670
11671 if (in_late_binary_op)
11672 {
11673 orig_op0_folded = orig_op0;
11674 orig_op1_folded = orig_op1;
11675 }
11676 else
11677 {
11678 /* Fold for the sake of possible warnings, as in
11679 build_conditional_expr. This requires the
11680 "original" values to be folded, not just op0 and
11681 op1. */
672d914b 11682 c_inhibit_evaluation_warnings++;
a75b1c71 11683 op0 = c_fully_fold (op0, require_constant_value,
11684 &op0_maybe_const);
11685 op1 = c_fully_fold (op1, require_constant_value,
11686 &op1_maybe_const);
672d914b 11687 c_inhibit_evaluation_warnings--;
a75b1c71 11688 orig_op0_folded = c_fully_fold (orig_op0,
11689 require_constant_value,
11690 NULL);
11691 orig_op1_folded = c_fully_fold (orig_op1,
11692 require_constant_value,
11693 NULL);
11694 }
11695
11696 if (warn_sign_compare)
11697 warn_for_sign_compare (location, orig_op0_folded,
11698 orig_op1_folded, op0, op1,
11699 result_type, resultcode);
4095a8c8 11700 if (!in_late_binary_op && !int_operands)
a75b1c71 11701 {
11702 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
f59e3889 11703 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
a75b1c71 11704 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
f59e3889 11705 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
a75b1c71 11706 }
276beea2 11707 }
736393e5 11708 }
7cad36f4 11709 }
7cad36f4 11710
276beea2 11711 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
11712 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
11713 Then the expression will be built.
11714 It will be given type FINAL_TYPE if that is nonzero;
11715 otherwise, it will be given type RESULT_TYPE. */
628fa4f8 11716
276beea2 11717 if (!result_type)
11718 {
c0bf500c 11719 gcc_rich_location richloc (location);
11720 richloc.maybe_add_expr (orig_op0);
11721 richloc.maybe_add_expr (orig_op1);
11722 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
276beea2 11723 return error_mark_node;
11724 }
628fa4f8 11725
276beea2 11726 if (build_type == NULL_TREE)
c6418a4e 11727 {
11728 build_type = result_type;
16a547ac 11729 if ((type0 != orig_type0 || type1 != orig_type1)
11730 && !boolean_op)
c6418a4e 11731 {
11732 gcc_assert (may_need_excess_precision && common);
8f45ab4d 11733 semantic_result_type = c_common_type (orig_type0, orig_type1);
c6418a4e 11734 }
11735 }
628fa4f8 11736
8f45ab4d 11737 if (!converted)
11738 {
22a75734 11739 op0 = ep_convert_and_check (location, result_type, op0,
11740 semantic_result_type);
11741 op1 = ep_convert_and_check (location, result_type, op1,
11742 semantic_result_type);
8f45ab4d 11743
11744 /* This can happen if one operand has a vector type, and the other
11745 has a different type. */
11746 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
11747 return error_mark_node;
11748 }
11749
52cc0072 11750 if ((flag_sanitize & (SANITIZE_SHIFT | SANITIZE_DIVIDE
11751 | SANITIZE_FLOAT_DIVIDE))
de3a1e03 11752 && do_ubsan_in_current_function ()
e96292e9 11753 && (doing_div_or_mod || doing_shift)
11754 && !require_constant_value)
9e46467d 11755 {
11756 /* OP0 and/or OP1 might have side-effects. */
11757 op0 = c_save_expr (op0);
11758 op1 = c_save_expr (op1);
11759 op0 = c_fully_fold (op0, false, NULL);
11760 op1 = c_fully_fold (op1, false, NULL);
52cc0072 11761 if (doing_div_or_mod && (flag_sanitize & (SANITIZE_DIVIDE
11762 | SANITIZE_FLOAT_DIVIDE)))
9e46467d 11763 instrument_expr = ubsan_instrument_division (location, op0, op1);
1b309377 11764 else if (doing_shift && (flag_sanitize & SANITIZE_SHIFT))
9e46467d 11765 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
11766 }
11767
b6889cb0 11768 /* Treat expressions in initializers specially as they can't trap. */
a75b1c71 11769 if (int_const_or_overflow)
11770 ret = (require_constant_value
389dd41b 11771 ? fold_build2_initializer_loc (location, resultcode, build_type,
11772 op0, op1)
11773 : fold_build2_loc (location, resultcode, build_type, op0, op1));
a75b1c71 11774 else
11775 ret = build2 (resultcode, build_type, op0, op1);
b6889cb0 11776 if (final_type != 0)
11777 ret = convert (final_type, ret);
11778
11779 return_build_binary_op:
11780 gcc_assert (ret != error_mark_node);
a75b1c71 11781 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
11782 ret = (int_operands
11783 ? note_integer_operands (ret)
11784 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
11785 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
11786 && !in_late_binary_op)
11787 ret = note_integer_operands (ret);
8f45ab4d 11788 if (semantic_result_type)
11789 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
b6889cb0 11790 protected_set_expr_location (ret, location);
9e46467d 11791
1b309377 11792 if (instrument_expr != NULL)
9e46467d 11793 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
11794 instrument_expr, ret);
11795
b6889cb0 11796 return ret;
628fa4f8 11797}
45a78cc0 11798
11799
11800/* Convert EXPR to be a truth-value, validating its type for this
8e70fb09 11801 purpose. LOCATION is the source location for the expression. */
45a78cc0 11802
11803tree
8e70fb09 11804c_objc_common_truthvalue_conversion (location_t location, tree expr)
45a78cc0 11805{
a75b1c71 11806 bool int_const, int_operands;
11807
45a78cc0 11808 switch (TREE_CODE (TREE_TYPE (expr)))
11809 {
11810 case ARRAY_TYPE:
8e70fb09 11811 error_at (location, "used array that cannot be converted to pointer where scalar is required");
45a78cc0 11812 return error_mark_node;
11813
11814 case RECORD_TYPE:
8e70fb09 11815 error_at (location, "used struct type value where scalar is required");
45a78cc0 11816 return error_mark_node;
11817
11818 case UNION_TYPE:
8e70fb09 11819 error_at (location, "used union type value where scalar is required");
45a78cc0 11820 return error_mark_node;
11821
a43fc97e 11822 case VOID_TYPE:
11823 error_at (location, "void value not ignored as it ought to be");
11824 return error_mark_node;
11825
547c6b1f 11826 case POINTER_TYPE:
11827 if (reject_gcc_builtin (expr))
11828 return error_mark_node;
11829 break;
11830
f14c8207 11831 case FUNCTION_TYPE:
11832 gcc_unreachable ();
11833
d7ad16c2 11834 case VECTOR_TYPE:
11835 error_at (location, "used vector type where scalar is required");
11836 return error_mark_node;
11837
45a78cc0 11838 default:
11839 break;
11840 }
11841
a75b1c71 11842 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
11843 int_operands = EXPR_INT_CONST_OPERANDS (expr);
bb554051 11844 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
11845 {
11846 expr = remove_c_maybe_const_expr (expr);
11847 expr = build2 (NE_EXPR, integer_type_node, expr,
11848 convert (TREE_TYPE (expr), integer_zero_node));
11849 expr = note_integer_operands (expr);
11850 }
11851 else
11852 /* ??? Should we also give an error for vectors rather than leaving
11853 those to give errors later? */
11854 expr = c_common_truthvalue_conversion (location, expr);
a75b1c71 11855
11856 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
11857 {
11858 if (TREE_OVERFLOW (expr))
11859 return expr;
11860 else
11861 return note_integer_operands (expr);
11862 }
11863 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
11864 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
11865 return expr;
45a78cc0 11866}
54d7165a 11867\f
11868
11869/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
11870 required. */
11871
11872tree
c7d4e749 11873c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
54d7165a 11874{
11875 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
11876 {
11877 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
11878 /* Executing a compound literal inside a function reinitializes
11879 it. */
11880 if (!TREE_STATIC (decl))
11881 *se = true;
11882 return decl;
11883 }
11884 else
11885 return expr;
11886}
1e8e9920 11887\f
2c4c8725 11888/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
11889 statement. LOC is the location of the construct. */
ca4c3545 11890
11891tree
2c4c8725 11892c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
11893 tree clauses)
ca4c3545 11894{
2c4c8725 11895 body = c_end_compound_stmt (loc, body, true);
ca4c3545 11896
2c4c8725 11897 tree stmt = make_node (code);
ca4c3545 11898 TREE_TYPE (stmt) = void_type_node;
2c4c8725 11899 OMP_BODY (stmt) = body;
11900 OMP_CLAUSES (stmt) = clauses;
ca4c3545 11901 SET_EXPR_LOCATION (stmt, loc);
11902
11903 return add_stmt (stmt);
11904}
11905
11906/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
11907 statement. LOC is the location of the OACC_DATA. */
11908
11909tree
11910c_finish_oacc_data (location_t loc, tree clauses, tree block)
11911{
11912 tree stmt;
11913
11914 block = c_end_compound_stmt (loc, block, true);
11915
11916 stmt = make_node (OACC_DATA);
11917 TREE_TYPE (stmt) = void_type_node;
11918 OACC_DATA_CLAUSES (stmt) = clauses;
11919 OACC_DATA_BODY (stmt) = block;
11920 SET_EXPR_LOCATION (stmt, loc);
11921
11922 return add_stmt (stmt);
11923}
11924
571b3486 11925/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
11926 statement. LOC is the location of the OACC_HOST_DATA. */
11927
11928tree
11929c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
11930{
11931 tree stmt;
11932
11933 block = c_end_compound_stmt (loc, block, true);
11934
11935 stmt = make_node (OACC_HOST_DATA);
11936 TREE_TYPE (stmt) = void_type_node;
11937 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
11938 OACC_HOST_DATA_BODY (stmt) = block;
11939 SET_EXPR_LOCATION (stmt, loc);
11940
11941 return add_stmt (stmt);
11942}
11943
334ec2d8 11944/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
1e8e9920 11945
11946tree
11947c_begin_omp_parallel (void)
11948{
11949 tree block;
11950
11951 keep_next_level ();
11952 block = c_begin_compound_stmt (true);
11953
11954 return block;
11955}
11956
e60a6f7b 11957/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
11958 statement. LOC is the location of the OMP_PARALLEL. */
fd6481cf 11959
1e8e9920 11960tree
e60a6f7b 11961c_finish_omp_parallel (location_t loc, tree clauses, tree block)
1e8e9920 11962{
11963 tree stmt;
11964
e60a6f7b 11965 block = c_end_compound_stmt (loc, block, true);
1e8e9920 11966
11967 stmt = make_node (OMP_PARALLEL);
11968 TREE_TYPE (stmt) = void_type_node;
11969 OMP_PARALLEL_CLAUSES (stmt) = clauses;
11970 OMP_PARALLEL_BODY (stmt) = block;
e60a6f7b 11971 SET_EXPR_LOCATION (stmt, loc);
1e8e9920 11972
11973 return add_stmt (stmt);
11974}
11975
fd6481cf 11976/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
11977
11978tree
11979c_begin_omp_task (void)
11980{
11981 tree block;
11982
11983 keep_next_level ();
11984 block = c_begin_compound_stmt (true);
11985
11986 return block;
11987}
11988
e60a6f7b 11989/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
11990 statement. LOC is the location of the #pragma. */
fd6481cf 11991
11992tree
e60a6f7b 11993c_finish_omp_task (location_t loc, tree clauses, tree block)
fd6481cf 11994{
11995 tree stmt;
11996
e60a6f7b 11997 block = c_end_compound_stmt (loc, block, true);
fd6481cf 11998
11999 stmt = make_node (OMP_TASK);
12000 TREE_TYPE (stmt) = void_type_node;
12001 OMP_TASK_CLAUSES (stmt) = clauses;
12002 OMP_TASK_BODY (stmt) = block;
e60a6f7b 12003 SET_EXPR_LOCATION (stmt, loc);
fd6481cf 12004
12005 return add_stmt (stmt);
12006}
12007
bc7bff74 12008/* Generate GOMP_cancel call for #pragma omp cancel. */
12009
12010void
12011c_finish_omp_cancel (location_t loc, tree clauses)
12012{
12013 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12014 int mask = 0;
4954efd4 12015 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
bc7bff74 12016 mask = 1;
4954efd4 12017 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
bc7bff74 12018 mask = 2;
4954efd4 12019 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
bc7bff74 12020 mask = 4;
4954efd4 12021 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
bc7bff74 12022 mask = 8;
12023 else
12024 {
44e775d8 12025 error_at (loc, "%<#pragma omp cancel%> must specify one of "
bc7bff74 12026 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12027 "clauses");
12028 return;
12029 }
4954efd4 12030 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
bc7bff74 12031 if (ifc != NULL_TREE)
12032 {
12033 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12034 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12035 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12036 build_zero_cst (type));
12037 }
12038 else
12039 ifc = boolean_true_node;
12040 tree stmt = build_call_expr_loc (loc, fn, 2,
12041 build_int_cst (integer_type_node, mask),
12042 ifc);
12043 add_stmt (stmt);
12044}
12045
12046/* Generate GOMP_cancellation_point call for
12047 #pragma omp cancellation point. */
12048
12049void
12050c_finish_omp_cancellation_point (location_t loc, tree clauses)
12051{
12052 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12053 int mask = 0;
4954efd4 12054 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
bc7bff74 12055 mask = 1;
4954efd4 12056 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
bc7bff74 12057 mask = 2;
4954efd4 12058 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
bc7bff74 12059 mask = 4;
4954efd4 12060 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
bc7bff74 12061 mask = 8;
12062 else
12063 {
44e775d8 12064 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
bc7bff74 12065 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12066 "clauses");
12067 return;
12068 }
12069 tree stmt = build_call_expr_loc (loc, fn, 1,
12070 build_int_cst (integer_type_node, mask));
12071 add_stmt (stmt);
12072}
12073
12074/* Helper function for handle_omp_array_sections. Called recursively
12075 to handle multiple array-section-subscripts. C is the clause,
12076 T current expression (initially OMP_CLAUSE_DECL), which is either
12077 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12078 expression if specified, TREE_VALUE length expression if specified,
12079 TREE_CHAIN is what it has been specified after, or some decl.
12080 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12081 set to true if any of the array-section-subscript could have length
12082 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12083 first array-section-subscript which is known not to have length
12084 of one. Given say:
12085 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12086 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12087 all are or may have length of 1, array-section-subscript [:2] is the
43895be5 12088 first one known not to have length 1. For array-section-subscript
bc7bff74 12089 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12090 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12091 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12092 case though, as some lengths could be zero. */
12093
12094static tree
12095handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
43895be5 12096 bool &maybe_zero_len, unsigned int &first_non_one,
6d6a3fc3 12097 enum c_omp_region_type ort)
bc7bff74 12098{
12099 tree ret, low_bound, length, type;
12100 if (TREE_CODE (t) != TREE_LIST)
12101 {
224bdd51 12102 if (error_operand_p (t))
bc7bff74 12103 return error_mark_node;
43895be5 12104 ret = t;
0b80c4b2 12105 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12106 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12107 {
12108 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12109 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12110 return error_mark_node;
12111 }
43895be5 12112 if (TREE_CODE (t) == COMPONENT_REF
6d6a3fc3 12113 && ort == C_ORT_OMP
43895be5 12114 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12115 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12116 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12117 {
12118 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12119 {
12120 error_at (OMP_CLAUSE_LOCATION (c),
12121 "bit-field %qE in %qs clause",
12122 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12123 return error_mark_node;
12124 }
12125 while (TREE_CODE (t) == COMPONENT_REF)
12126 {
12127 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12128 {
12129 error_at (OMP_CLAUSE_LOCATION (c),
12130 "%qE is a member of a union", t);
12131 return error_mark_node;
12132 }
12133 t = TREE_OPERAND (t, 0);
12134 }
12135 }
f48c7f4a 12136 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
bc7bff74 12137 {
12138 if (DECL_P (t))
12139 error_at (OMP_CLAUSE_LOCATION (c),
12140 "%qD is not a variable in %qs clause", t,
12141 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12142 else
12143 error_at (OMP_CLAUSE_LOCATION (c),
12144 "%qE is not a variable in %qs clause", t,
12145 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12146 return error_mark_node;
12147 }
12148 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
0b80c4b2 12149 && TYPE_ATOMIC (TREE_TYPE (t)))
12150 {
12151 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12152 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12153 return error_mark_node;
12154 }
12155 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12156 && VAR_P (t)
12157 && DECL_THREAD_LOCAL_P (t))
bc7bff74 12158 {
12159 error_at (OMP_CLAUSE_LOCATION (c),
12160 "%qD is threadprivate variable in %qs clause", t,
12161 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12162 return error_mark_node;
12163 }
0b80c4b2 12164 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12165 && TYPE_ATOMIC (TREE_TYPE (t))
12166 && POINTER_TYPE_P (TREE_TYPE (t)))
12167 {
12168 /* If the array section is pointer based and the pointer
12169 itself is _Atomic qualified, we need to atomically load
12170 the pointer. */
12171 c_expr expr;
12172 memset (&expr, 0, sizeof (expr));
12173 expr.value = ret;
12174 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12175 expr, false, false);
12176 ret = expr.value;
12177 }
43895be5 12178 return ret;
bc7bff74 12179 }
12180
12181 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
6d6a3fc3 12182 maybe_zero_len, first_non_one, ort);
bc7bff74 12183 if (ret == error_mark_node || ret == NULL_TREE)
12184 return ret;
12185
12186 type = TREE_TYPE (ret);
12187 low_bound = TREE_PURPOSE (t);
12188 length = TREE_VALUE (t);
12189
12190 if (low_bound == error_mark_node || length == error_mark_node)
12191 return error_mark_node;
12192
12193 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12194 {
12195 error_at (OMP_CLAUSE_LOCATION (c),
12196 "low bound %qE of array section does not have integral type",
12197 low_bound);
12198 return error_mark_node;
12199 }
12200 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12201 {
12202 error_at (OMP_CLAUSE_LOCATION (c),
12203 "length %qE of array section does not have integral type",
12204 length);
12205 return error_mark_node;
12206 }
12207 if (low_bound
12208 && TREE_CODE (low_bound) == INTEGER_CST
12209 && TYPE_PRECISION (TREE_TYPE (low_bound))
12210 > TYPE_PRECISION (sizetype))
12211 low_bound = fold_convert (sizetype, low_bound);
12212 if (length
12213 && TREE_CODE (length) == INTEGER_CST
12214 && TYPE_PRECISION (TREE_TYPE (length))
12215 > TYPE_PRECISION (sizetype))
12216 length = fold_convert (sizetype, length);
12217 if (low_bound == NULL_TREE)
12218 low_bound = integer_zero_node;
12219
12220 if (length != NULL_TREE)
12221 {
12222 if (!integer_nonzerop (length))
43895be5 12223 {
12224 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12225 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12226 {
12227 if (integer_zerop (length))
12228 {
12229 error_at (OMP_CLAUSE_LOCATION (c),
12230 "zero length array section in %qs clause",
12231 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12232 return error_mark_node;
12233 }
12234 }
12235 else
12236 maybe_zero_len = true;
12237 }
bc7bff74 12238 if (first_non_one == types.length ()
12239 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12240 first_non_one++;
12241 }
12242 if (TREE_CODE (type) == ARRAY_TYPE)
12243 {
12244 if (length == NULL_TREE
12245 && (TYPE_DOMAIN (type) == NULL_TREE
12246 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12247 {
12248 error_at (OMP_CLAUSE_LOCATION (c),
12249 "for unknown bound array type length expression must "
12250 "be specified");
12251 return error_mark_node;
12252 }
12253 if (TREE_CODE (low_bound) == INTEGER_CST
12254 && tree_int_cst_sgn (low_bound) == -1)
12255 {
12256 error_at (OMP_CLAUSE_LOCATION (c),
12257 "negative low bound in array section in %qs clause",
12258 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12259 return error_mark_node;
12260 }
12261 if (length != NULL_TREE
12262 && TREE_CODE (length) == INTEGER_CST
12263 && tree_int_cst_sgn (length) == -1)
12264 {
12265 error_at (OMP_CLAUSE_LOCATION (c),
12266 "negative length in array section in %qs clause",
12267 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12268 return error_mark_node;
12269 }
12270 if (TYPE_DOMAIN (type)
12271 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12272 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12273 == INTEGER_CST)
12274 {
12275 tree size = size_binop (PLUS_EXPR,
12276 TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12277 size_one_node);
12278 if (TREE_CODE (low_bound) == INTEGER_CST)
12279 {
12280 if (tree_int_cst_lt (size, low_bound))
12281 {
12282 error_at (OMP_CLAUSE_LOCATION (c),
12283 "low bound %qE above array section size "
12284 "in %qs clause", low_bound,
12285 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12286 return error_mark_node;
12287 }
12288 if (tree_int_cst_equal (size, low_bound))
43895be5 12289 {
12290 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12291 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12292 {
12293 error_at (OMP_CLAUSE_LOCATION (c),
12294 "zero length array section in %qs clause",
12295 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12296 return error_mark_node;
12297 }
12298 maybe_zero_len = true;
12299 }
bc7bff74 12300 else if (length == NULL_TREE
12301 && first_non_one == types.length ()
12302 && tree_int_cst_equal
12303 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12304 low_bound))
12305 first_non_one++;
12306 }
12307 else if (length == NULL_TREE)
12308 {
43895be5 12309 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12310 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12311 maybe_zero_len = true;
bc7bff74 12312 if (first_non_one == types.length ())
12313 first_non_one++;
12314 }
12315 if (length && TREE_CODE (length) == INTEGER_CST)
12316 {
12317 if (tree_int_cst_lt (size, length))
12318 {
12319 error_at (OMP_CLAUSE_LOCATION (c),
12320 "length %qE above array section size "
12321 "in %qs clause", length,
12322 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12323 return error_mark_node;
12324 }
12325 if (TREE_CODE (low_bound) == INTEGER_CST)
12326 {
12327 tree lbpluslen
12328 = size_binop (PLUS_EXPR,
12329 fold_convert (sizetype, low_bound),
12330 fold_convert (sizetype, length));
12331 if (TREE_CODE (lbpluslen) == INTEGER_CST
12332 && tree_int_cst_lt (size, lbpluslen))
12333 {
12334 error_at (OMP_CLAUSE_LOCATION (c),
12335 "high bound %qE above array section size "
12336 "in %qs clause", lbpluslen,
12337 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12338 return error_mark_node;
12339 }
12340 }
12341 }
12342 }
12343 else if (length == NULL_TREE)
12344 {
43895be5 12345 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12346 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12347 maybe_zero_len = true;
bc7bff74 12348 if (first_non_one == types.length ())
12349 first_non_one++;
12350 }
12351
12352 /* For [lb:] we will need to evaluate lb more than once. */
12353 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12354 {
12355 tree lb = c_save_expr (low_bound);
12356 if (lb != low_bound)
12357 {
12358 TREE_PURPOSE (t) = lb;
12359 low_bound = lb;
12360 }
12361 }
12362 }
12363 else if (TREE_CODE (type) == POINTER_TYPE)
12364 {
12365 if (length == NULL_TREE)
12366 {
12367 error_at (OMP_CLAUSE_LOCATION (c),
12368 "for pointer type length expression must be specified");
12369 return error_mark_node;
12370 }
43895be5 12371 if (length != NULL_TREE
12372 && TREE_CODE (length) == INTEGER_CST
12373 && tree_int_cst_sgn (length) == -1)
12374 {
12375 error_at (OMP_CLAUSE_LOCATION (c),
12376 "negative length in array section in %qs clause",
12377 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12378 return error_mark_node;
12379 }
bc7bff74 12380 /* If there is a pointer type anywhere but in the very first
12381 array-section-subscript, the array section can't be contiguous. */
12382 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12383 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12384 {
12385 error_at (OMP_CLAUSE_LOCATION (c),
12386 "array section is not contiguous in %qs clause",
12387 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12388 return error_mark_node;
12389 }
12390 }
12391 else
12392 {
12393 error_at (OMP_CLAUSE_LOCATION (c),
12394 "%qE does not have pointer or array type", ret);
12395 return error_mark_node;
12396 }
12397 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12398 types.safe_push (TREE_TYPE (ret));
12399 /* We will need to evaluate lb more than once. */
12400 tree lb = c_save_expr (low_bound);
12401 if (lb != low_bound)
12402 {
12403 TREE_PURPOSE (t) = lb;
12404 low_bound = lb;
12405 }
12406 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12407 return ret;
12408}
12409
12410/* Handle array sections for clause C. */
12411
12412static bool
6d6a3fc3 12413handle_omp_array_sections (tree c, enum c_omp_region_type ort)
bc7bff74 12414{
12415 bool maybe_zero_len = false;
12416 unsigned int first_non_one = 0;
43895be5 12417 auto_vec<tree, 10> types;
bc7bff74 12418 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
43895be5 12419 maybe_zero_len, first_non_one,
6d6a3fc3 12420 ort);
bc7bff74 12421 if (first == error_mark_node)
43895be5 12422 return true;
bc7bff74 12423 if (first == NULL_TREE)
43895be5 12424 return false;
bc7bff74 12425 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12426 {
12427 tree t = OMP_CLAUSE_DECL (c);
12428 tree tem = NULL_TREE;
bc7bff74 12429 /* Need to evaluate side effects in the length expressions
12430 if any. */
12431 while (TREE_CODE (t) == TREE_LIST)
12432 {
12433 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12434 {
12435 if (tem == NULL_TREE)
12436 tem = TREE_VALUE (t);
12437 else
12438 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12439 TREE_VALUE (t), tem);
12440 }
12441 t = TREE_CHAIN (t);
12442 }
12443 if (tem)
12444 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
12445 first = c_fully_fold (first, false, NULL);
12446 OMP_CLAUSE_DECL (c) = first;
12447 }
12448 else
12449 {
12450 unsigned int num = types.length (), i;
12451 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12452 tree condition = NULL_TREE;
12453
12454 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12455 maybe_zero_len = true;
12456
12457 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12458 t = TREE_CHAIN (t))
12459 {
12460 tree low_bound = TREE_PURPOSE (t);
12461 tree length = TREE_VALUE (t);
12462
12463 i--;
12464 if (low_bound
12465 && TREE_CODE (low_bound) == INTEGER_CST
12466 && TYPE_PRECISION (TREE_TYPE (low_bound))
12467 > TYPE_PRECISION (sizetype))
12468 low_bound = fold_convert (sizetype, low_bound);
12469 if (length
12470 && TREE_CODE (length) == INTEGER_CST
12471 && TYPE_PRECISION (TREE_TYPE (length))
12472 > TYPE_PRECISION (sizetype))
12473 length = fold_convert (sizetype, length);
12474 if (low_bound == NULL_TREE)
12475 low_bound = integer_zero_node;
12476 if (!maybe_zero_len && i > first_non_one)
12477 {
12478 if (integer_nonzerop (low_bound))
12479 goto do_warn_noncontiguous;
12480 if (length != NULL_TREE
12481 && TREE_CODE (length) == INTEGER_CST
12482 && TYPE_DOMAIN (types[i])
12483 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12484 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12485 == INTEGER_CST)
12486 {
12487 tree size;
12488 size = size_binop (PLUS_EXPR,
12489 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12490 size_one_node);
12491 if (!tree_int_cst_equal (length, size))
12492 {
12493 do_warn_noncontiguous:
12494 error_at (OMP_CLAUSE_LOCATION (c),
12495 "array section is not contiguous in %qs "
12496 "clause",
12497 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
bc7bff74 12498 return true;
12499 }
12500 }
12501 if (length != NULL_TREE
12502 && TREE_SIDE_EFFECTS (length))
12503 {
12504 if (side_effects == NULL_TREE)
12505 side_effects = length;
12506 else
12507 side_effects = build2 (COMPOUND_EXPR,
12508 TREE_TYPE (side_effects),
12509 length, side_effects);
12510 }
12511 }
12512 else
12513 {
12514 tree l;
12515
43895be5 12516 if (i > first_non_one
12517 && ((length && integer_nonzerop (length))
12518 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
bc7bff74 12519 continue;
12520 if (length)
12521 l = fold_convert (sizetype, length);
12522 else
12523 {
12524 l = size_binop (PLUS_EXPR,
12525 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12526 size_one_node);
12527 l = size_binop (MINUS_EXPR, l,
12528 fold_convert (sizetype, low_bound));
12529 }
12530 if (i > first_non_one)
12531 {
12532 l = fold_build2 (NE_EXPR, boolean_type_node, l,
12533 size_zero_node);
12534 if (condition == NULL_TREE)
12535 condition = l;
12536 else
12537 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
12538 l, condition);
12539 }
12540 else if (size == NULL_TREE)
12541 {
12542 size = size_in_bytes (TREE_TYPE (types[i]));
43895be5 12543 tree eltype = TREE_TYPE (types[num - 1]);
12544 while (TREE_CODE (eltype) == ARRAY_TYPE)
12545 eltype = TREE_TYPE (eltype);
12546 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12547 {
12548 if (integer_zerop (size)
12549 || integer_zerop (size_in_bytes (eltype)))
12550 {
12551 error_at (OMP_CLAUSE_LOCATION (c),
12552 "zero length array section in %qs clause",
12553 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12554 return error_mark_node;
12555 }
12556 size = size_binop (EXACT_DIV_EXPR, size,
12557 size_in_bytes (eltype));
12558 }
bc7bff74 12559 size = size_binop (MULT_EXPR, size, l);
12560 if (condition)
12561 size = fold_build3 (COND_EXPR, sizetype, condition,
12562 size, size_zero_node);
12563 }
12564 else
12565 size = size_binop (MULT_EXPR, size, l);
12566 }
12567 }
bc7bff74 12568 if (side_effects)
12569 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
43895be5 12570 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12571 {
12572 size = size_binop (MINUS_EXPR, size, size_one_node);
12573 size = c_fully_fold (size, false, NULL);
12574 tree index_type = build_index_type (size);
12575 tree eltype = TREE_TYPE (first);
12576 while (TREE_CODE (eltype) == ARRAY_TYPE)
12577 eltype = TREE_TYPE (eltype);
12578 tree type = build_array_type (eltype, index_type);
12579 tree ptype = build_pointer_type (eltype);
12580 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12581 t = build_fold_addr_expr (t);
9561765e 12582 tree t2 = build_fold_addr_expr (first);
12583 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12584 ptrdiff_type_node, t2);
12585 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12586 ptrdiff_type_node, t2,
12587 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12588 ptrdiff_type_node, t));
12589 t2 = c_fully_fold (t2, false, NULL);
12590 if (tree_fits_shwi_p (t2))
12591 t = build2 (MEM_REF, type, t,
12592 build_int_cst (ptype, tree_to_shwi (t2)));
12593 else
12594 {
12595 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
12596 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
12597 TREE_TYPE (t), t, t2);
12598 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
12599 }
43895be5 12600 OMP_CLAUSE_DECL (c) = t;
12601 return false;
12602 }
bc7bff74 12603 first = c_fully_fold (first, false, NULL);
12604 OMP_CLAUSE_DECL (c) = first;
12605 if (size)
12606 size = c_fully_fold (size, false, NULL);
12607 OMP_CLAUSE_SIZE (c) = size;
43895be5 12608 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
12609 || (TREE_CODE (t) == COMPONENT_REF
12610 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
bc7bff74 12611 return false;
ca4c3545 12612 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
6d6a3fc3 12613 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
43895be5 12614 switch (OMP_CLAUSE_MAP_KIND (c))
12615 {
12616 case GOMP_MAP_ALLOC:
12617 case GOMP_MAP_TO:
12618 case GOMP_MAP_FROM:
12619 case GOMP_MAP_TOFROM:
12620 case GOMP_MAP_ALWAYS_TO:
12621 case GOMP_MAP_ALWAYS_FROM:
12622 case GOMP_MAP_ALWAYS_TOFROM:
12623 case GOMP_MAP_RELEASE:
12624 case GOMP_MAP_DELETE:
0ef9358d 12625 case GOMP_MAP_FORCE_TO:
12626 case GOMP_MAP_FORCE_FROM:
12627 case GOMP_MAP_FORCE_TOFROM:
12628 case GOMP_MAP_FORCE_PRESENT:
43895be5 12629 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
12630 break;
12631 default:
12632 break;
12633 }
bc7bff74 12634 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
6d6a3fc3 12635 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
9561765e 12636 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
12637 else if (TREE_CODE (t) == COMPONENT_REF)
12638 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
12639 else
12640 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
12641 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
12642 && !c_mark_addressable (t))
bc7bff74 12643 return false;
12644 OMP_CLAUSE_DECL (c2) = t;
12645 t = build_fold_addr_expr (first);
12646 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
12647 tree ptr = OMP_CLAUSE_DECL (c2);
12648 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
12649 ptr = build_fold_addr_expr (ptr);
12650 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
12651 ptrdiff_type_node, t,
12652 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
12653 ptrdiff_type_node, ptr));
12654 t = c_fully_fold (t, false, NULL);
12655 OMP_CLAUSE_SIZE (c2) = t;
12656 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
12657 OMP_CLAUSE_CHAIN (c) = c2;
12658 }
12659 return false;
12660}
12661
12662/* Helper function of finish_omp_clauses. Clone STMT as if we were making
12663 an inline call. But, remap
12664 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
12665 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
12666
12667static tree
12668c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
12669 tree decl, tree placeholder)
12670{
12671 copy_body_data id;
06ecf488 12672 hash_map<tree, tree> decl_map;
bc7bff74 12673
06ecf488 12674 decl_map.put (omp_decl1, placeholder);
12675 decl_map.put (omp_decl2, decl);
bc7bff74 12676 memset (&id, 0, sizeof (id));
12677 id.src_fn = DECL_CONTEXT (omp_decl1);
12678 id.dst_fn = current_function_decl;
12679 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
06ecf488 12680 id.decl_map = &decl_map;
bc7bff74 12681
12682 id.copy_decl = copy_decl_no_change;
12683 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
12684 id.transform_new_cfg = true;
12685 id.transform_return_to_modify = false;
12686 id.transform_lang_insert_block = NULL;
12687 id.eh_lp_nr = 0;
12688 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
bc7bff74 12689 return stmt;
12690}
12691
12692/* Helper function of c_finish_omp_clauses, called via walk_tree.
12693 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
12694
12695static tree
12696c_find_omp_placeholder_r (tree *tp, int *, void *data)
12697{
12698 if (*tp == (tree) data)
12699 return *tp;
12700 return NULL_TREE;
12701}
12702
ca4c3545 12703/* For all elements of CLAUSES, validate them against their constraints.
1e8e9920 12704 Remove any elements from the list that are invalid. */
12705
12706tree
b5e88f74 12707c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
1e8e9920 12708{
12709 bitmap_head generic_head, firstprivate_head, lastprivate_head;
6d6a3fc3 12710 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
43895be5 12711 tree c, t, type, *pc;
12712 tree simdlen = NULL_TREE, safelen = NULL_TREE;
bc7bff74 12713 bool branch_seen = false;
12714 bool copyprivate_seen = false;
9561765e 12715 bool linear_variable_step_check = false;
bc7bff74 12716 tree *nowait_clause = NULL;
9561765e 12717 bool ordered_seen = false;
12718 tree schedule_clause = NULL_TREE;
2234363c 12719 bool oacc_async = false;
1e8e9920 12720
12721 bitmap_obstack_initialize (NULL);
12722 bitmap_initialize (&generic_head, &bitmap_default_obstack);
12723 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
12724 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
bc7bff74 12725 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
43895be5 12726 bitmap_initialize (&map_head, &bitmap_default_obstack);
12727 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
6d6a3fc3 12728 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
1e8e9920 12729
2234363c 12730 if (ort & C_ORT_ACC)
12731 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
12732 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
12733 {
12734 oacc_async = true;
12735 break;
12736 }
0b80c4b2 12737
1e8e9920 12738 for (pc = &clauses, c = clauses; c ; c = *pc)
12739 {
12740 bool remove = false;
12741 bool need_complete = false;
12742 bool need_implicitly_determined = false;
12743
55d6e7cd 12744 switch (OMP_CLAUSE_CODE (c))
1e8e9920 12745 {
12746 case OMP_CLAUSE_SHARED:
1e8e9920 12747 need_implicitly_determined = true;
12748 goto check_dup_generic;
12749
12750 case OMP_CLAUSE_PRIVATE:
1e8e9920 12751 need_complete = true;
12752 need_implicitly_determined = true;
12753 goto check_dup_generic;
12754
12755 case OMP_CLAUSE_REDUCTION:
1e8e9920 12756 need_implicitly_determined = true;
12757 t = OMP_CLAUSE_DECL (c);
43895be5 12758 if (TREE_CODE (t) == TREE_LIST)
12759 {
6d6a3fc3 12760 if (handle_omp_array_sections (c, ort))
43895be5 12761 {
12762 remove = true;
12763 break;
12764 }
12765
12766 t = OMP_CLAUSE_DECL (c);
12767 }
22a3f7bd 12768 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
43895be5 12769 if (t == error_mark_node)
12770 {
12771 remove = true;
12772 break;
12773 }
2234363c 12774 if (oacc_async)
12775 c_mark_addressable (t);
43895be5 12776 type = TREE_TYPE (t);
12777 if (TREE_CODE (t) == MEM_REF)
12778 type = TREE_TYPE (type);
12779 if (TREE_CODE (type) == ARRAY_TYPE)
12780 {
12781 tree oatype = type;
12782 gcc_assert (TREE_CODE (t) != MEM_REF);
12783 while (TREE_CODE (type) == ARRAY_TYPE)
12784 type = TREE_TYPE (type);
12785 if (integer_zerop (TYPE_SIZE_UNIT (type)))
12786 {
12787 error_at (OMP_CLAUSE_LOCATION (c),
12788 "%qD in %<reduction%> clause is a zero size array",
12789 t);
12790 remove = true;
12791 break;
12792 }
12793 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
12794 TYPE_SIZE_UNIT (type));
12795 if (integer_zerop (size))
12796 {
12797 error_at (OMP_CLAUSE_LOCATION (c),
12798 "%qD in %<reduction%> clause is a zero size array",
12799 t);
12800 remove = true;
12801 break;
12802 }
12803 size = size_binop (MINUS_EXPR, size, size_one_node);
12804 tree index_type = build_index_type (size);
12805 tree atype = build_array_type (type, index_type);
12806 tree ptype = build_pointer_type (type);
12807 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
12808 t = build_fold_addr_expr (t);
12809 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
12810 OMP_CLAUSE_DECL (c) = t;
12811 }
0b80c4b2 12812 if (TYPE_ATOMIC (type))
12813 {
12814 error_at (OMP_CLAUSE_LOCATION (c),
12815 "%<_Atomic%> %qE in %<reduction%> clause", t);
12816 remove = true;
12817 break;
12818 }
bc7bff74 12819 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
43895be5 12820 && (FLOAT_TYPE_P (type)
12821 || TREE_CODE (type) == COMPLEX_TYPE))
1e8e9920 12822 {
12823 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
12824 const char *r_name = NULL;
12825
12826 switch (r_code)
12827 {
12828 case PLUS_EXPR:
12829 case MULT_EXPR:
12830 case MINUS_EXPR:
d6779b5f 12831 break;
2169f33b 12832 case MIN_EXPR:
43895be5 12833 if (TREE_CODE (type) == COMPLEX_TYPE)
d6779b5f 12834 r_name = "min";
12835 break;
2169f33b 12836 case MAX_EXPR:
43895be5 12837 if (TREE_CODE (type) == COMPLEX_TYPE)
d6779b5f 12838 r_name = "max";
1e8e9920 12839 break;
12840 case BIT_AND_EXPR:
12841 r_name = "&";
12842 break;
12843 case BIT_XOR_EXPR:
12844 r_name = "^";
12845 break;
12846 case BIT_IOR_EXPR:
12847 r_name = "|";
12848 break;
12849 case TRUTH_ANDIF_EXPR:
43895be5 12850 if (FLOAT_TYPE_P (type))
d6779b5f 12851 r_name = "&&";
1e8e9920 12852 break;
12853 case TRUTH_ORIF_EXPR:
43895be5 12854 if (FLOAT_TYPE_P (type))
d6779b5f 12855 r_name = "||";
1e8e9920 12856 break;
12857 default:
12858 gcc_unreachable ();
12859 }
12860 if (r_name)
12861 {
e60a6f7b 12862 error_at (OMP_CLAUSE_LOCATION (c),
12863 "%qE has invalid type for %<reduction(%s)%>",
12864 t, r_name);
1e8e9920 12865 remove = true;
df205251 12866 break;
1e8e9920 12867 }
12868 }
bc7bff74 12869 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
12870 {
12871 error_at (OMP_CLAUSE_LOCATION (c),
43895be5 12872 "user defined reduction not found for %qE", t);
bc7bff74 12873 remove = true;
df205251 12874 break;
bc7bff74 12875 }
12876 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
12877 {
12878 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
43895be5 12879 type = TYPE_MAIN_VARIANT (type);
bc7bff74 12880 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12881 VAR_DECL, NULL_TREE, type);
43895be5 12882 tree decl_placeholder = NULL_TREE;
bc7bff74 12883 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
12884 DECL_ARTIFICIAL (placeholder) = 1;
12885 DECL_IGNORED_P (placeholder) = 1;
43895be5 12886 if (TREE_CODE (t) == MEM_REF)
12887 {
12888 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
12889 VAR_DECL, NULL_TREE, type);
12890 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
12891 DECL_ARTIFICIAL (decl_placeholder) = 1;
12892 DECL_IGNORED_P (decl_placeholder) = 1;
12893 }
bc7bff74 12894 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
12895 c_mark_addressable (placeholder);
12896 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
43895be5 12897 c_mark_addressable (decl_placeholder ? decl_placeholder
12898 : OMP_CLAUSE_DECL (c));
bc7bff74 12899 OMP_CLAUSE_REDUCTION_MERGE (c)
12900 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
12901 TREE_VEC_ELT (list, 0),
12902 TREE_VEC_ELT (list, 1),
43895be5 12903 decl_placeholder ? decl_placeholder
12904 : OMP_CLAUSE_DECL (c), placeholder);
bc7bff74 12905 OMP_CLAUSE_REDUCTION_MERGE (c)
12906 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12907 void_type_node, NULL_TREE,
43895be5 12908 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
bc7bff74 12909 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
12910 if (TREE_VEC_LENGTH (list) == 6)
12911 {
12912 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
43895be5 12913 c_mark_addressable (decl_placeholder ? decl_placeholder
12914 : OMP_CLAUSE_DECL (c));
bc7bff74 12915 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
12916 c_mark_addressable (placeholder);
12917 tree init = TREE_VEC_ELT (list, 5);
12918 if (init == error_mark_node)
12919 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
12920 OMP_CLAUSE_REDUCTION_INIT (c)
12921 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
12922 TREE_VEC_ELT (list, 3),
43895be5 12923 decl_placeholder ? decl_placeholder
12924 : OMP_CLAUSE_DECL (c), placeholder);
bc7bff74 12925 if (TREE_VEC_ELT (list, 5) == error_mark_node)
43895be5 12926 {
12927 tree v = decl_placeholder ? decl_placeholder : t;
12928 OMP_CLAUSE_REDUCTION_INIT (c)
12929 = build2 (INIT_EXPR, TREE_TYPE (v), v,
12930 OMP_CLAUSE_REDUCTION_INIT (c));
12931 }
bc7bff74 12932 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
12933 c_find_omp_placeholder_r,
12934 placeholder, NULL))
12935 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
12936 }
12937 else
12938 {
12939 tree init;
43895be5 12940 tree v = decl_placeholder ? decl_placeholder : t;
12941 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
12942 init = build_constructor (TREE_TYPE (v), NULL);
bc7bff74 12943 else
43895be5 12944 init = fold_convert (TREE_TYPE (v), integer_zero_node);
bc7bff74 12945 OMP_CLAUSE_REDUCTION_INIT (c)
43895be5 12946 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
bc7bff74 12947 }
12948 OMP_CLAUSE_REDUCTION_INIT (c)
12949 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
12950 void_type_node, NULL_TREE,
12951 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
12952 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
12953 }
43895be5 12954 if (TREE_CODE (t) == MEM_REF)
12955 {
12956 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
12957 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
12958 != INTEGER_CST)
12959 {
12960 sorry ("variable length element type in array "
12961 "%<reduction%> clause");
12962 remove = true;
12963 break;
12964 }
12965 t = TREE_OPERAND (t, 0);
9561765e 12966 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
12967 t = TREE_OPERAND (t, 0);
43895be5 12968 if (TREE_CODE (t) == ADDR_EXPR)
12969 t = TREE_OPERAND (t, 0);
12970 }
12971 goto check_dup_generic_t;
1e8e9920 12972
12973 case OMP_CLAUSE_COPYPRIVATE:
bc7bff74 12974 copyprivate_seen = true;
12975 if (nowait_clause)
12976 {
12977 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
12978 "%<nowait%> clause must not be used together "
12979 "with %<copyprivate%>");
12980 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
12981 nowait_clause = NULL;
12982 }
1e8e9920 12983 goto check_dup_generic;
12984
12985 case OMP_CLAUSE_COPYIN:
1e8e9920 12986 t = OMP_CLAUSE_DECL (c);
f48c7f4a 12987 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
1e8e9920 12988 {
e60a6f7b 12989 error_at (OMP_CLAUSE_LOCATION (c),
12990 "%qE must be %<threadprivate%> for %<copyin%>", t);
1e8e9920 12991 remove = true;
df205251 12992 break;
1e8e9920 12993 }
12994 goto check_dup_generic;
12995
bc7bff74 12996 case OMP_CLAUSE_LINEAR:
b5e88f74 12997 if (ort != C_ORT_OMP_DECLARE_SIMD)
43895be5 12998 need_implicitly_determined = true;
bc7bff74 12999 t = OMP_CLAUSE_DECL (c);
b5e88f74 13000 if (ort != C_ORT_OMP_DECLARE_SIMD
43895be5 13001 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13002 {
13003 error_at (OMP_CLAUSE_LOCATION (c),
13004 "modifier should not be specified in %<linear%> "
13005 "clause on %<simd%> or %<for%> constructs");
13006 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13007 }
b5e88f74 13008 if (ort & C_ORT_CILK)
bc7bff74 13009 {
70574e60 13010 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13011 && !SCALAR_FLOAT_TYPE_P (TREE_TYPE (t))
13012 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13013 {
13014 error_at (OMP_CLAUSE_LOCATION (c),
13015 "linear clause applied to non-integral, "
13016 "non-floating, non-pointer variable with type %qT",
13017 TREE_TYPE (t));
13018 remove = true;
13019 break;
13020 }
13021 }
13022 else
13023 {
13024 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13025 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
13026 {
13027 error_at (OMP_CLAUSE_LOCATION (c),
13028 "linear clause applied to non-integral non-pointer "
13029 "variable with type %qT", TREE_TYPE (t));
13030 remove = true;
13031 break;
13032 }
0b80c4b2 13033 if (TYPE_ATOMIC (TREE_TYPE (t)))
13034 {
13035 error_at (OMP_CLAUSE_LOCATION (c),
13036 "%<_Atomic%> %qD in %<linear%> clause", t);
13037 remove = true;
13038 break;
13039 }
bc7bff74 13040 }
b5e88f74 13041 if (ort == C_ORT_OMP_DECLARE_SIMD)
9561765e 13042 {
13043 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13044 if (TREE_CODE (s) == PARM_DECL)
13045 {
13046 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13047 /* map_head bitmap is used as uniform_head if
13048 declare_simd. */
13049 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13050 linear_variable_step_check = true;
13051 goto check_dup_generic;
13052 }
13053 if (TREE_CODE (s) != INTEGER_CST)
13054 {
13055 error_at (OMP_CLAUSE_LOCATION (c),
13056 "%<linear%> clause step %qE is neither constant "
13057 "nor a parameter", s);
13058 remove = true;
13059 break;
13060 }
13061 }
bc7bff74 13062 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13063 {
13064 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13065 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13066 OMP_CLAUSE_DECL (c), s);
13067 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
844cece0 13068 sizetype, fold_convert (sizetype, s),
13069 fold_convert
13070 (sizetype, OMP_CLAUSE_DECL (c)));
bc7bff74 13071 if (s == error_mark_node)
13072 s = size_one_node;
13073 OMP_CLAUSE_LINEAR_STEP (c) = s;
13074 }
9580cb79 13075 else
13076 OMP_CLAUSE_LINEAR_STEP (c)
13077 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
bc7bff74 13078 goto check_dup_generic;
13079
1e8e9920 13080 check_dup_generic:
13081 t = OMP_CLAUSE_DECL (c);
43895be5 13082 check_dup_generic_t:
f48c7f4a 13083 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1e8e9920 13084 {
e60a6f7b 13085 error_at (OMP_CLAUSE_LOCATION (c),
bc7bff74 13086 "%qE is not a variable in clause %qs", t,
13087 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
1e8e9920 13088 remove = true;
13089 }
6d6a3fc3 13090 else if (ort == C_ORT_ACC
13091 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13092 {
13093 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13094 {
13095 error ("%qD appears more than once in reduction clauses", t);
13096 remove = true;
13097 }
13098 else
13099 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13100 }
1e8e9920 13101 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13102 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13103 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13104 {
e60a6f7b 13105 error_at (OMP_CLAUSE_LOCATION (c),
13106 "%qE appears more than once in data clauses", t);
1e8e9920 13107 remove = true;
13108 }
9561765e 13109 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13110 && bitmap_bit_p (&map_head, DECL_UID (t)))
13111 {
6d6a3fc3 13112 if (ort == C_ORT_ACC)
13113 error ("%qD appears more than once in data clauses", t);
13114 else
13115 error ("%qD appears both in data and map clauses", t);
9561765e 13116 remove = true;
13117 }
1e8e9920 13118 else
13119 bitmap_set_bit (&generic_head, DECL_UID (t));
13120 break;
13121
13122 case OMP_CLAUSE_FIRSTPRIVATE:
1e8e9920 13123 t = OMP_CLAUSE_DECL (c);
13124 need_complete = true;
13125 need_implicitly_determined = true;
f48c7f4a 13126 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1e8e9920 13127 {
e60a6f7b 13128 error_at (OMP_CLAUSE_LOCATION (c),
13129 "%qE is not a variable in clause %<firstprivate%>", t);
1e8e9920 13130 remove = true;
13131 }
13132 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13133 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13134 {
e60a6f7b 13135 error_at (OMP_CLAUSE_LOCATION (c),
13136 "%qE appears more than once in data clauses", t);
1e8e9920 13137 remove = true;
13138 }
9561765e 13139 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13140 {
6d6a3fc3 13141 if (ort == C_ORT_ACC)
13142 error ("%qD appears more than once in data clauses", t);
13143 else
13144 error ("%qD appears both in data and map clauses", t);
9561765e 13145 remove = true;
13146 }
1e8e9920 13147 else
13148 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13149 break;
13150
13151 case OMP_CLAUSE_LASTPRIVATE:
1e8e9920 13152 t = OMP_CLAUSE_DECL (c);
13153 need_complete = true;
13154 need_implicitly_determined = true;
f48c7f4a 13155 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
1e8e9920 13156 {
e60a6f7b 13157 error_at (OMP_CLAUSE_LOCATION (c),
13158 "%qE is not a variable in clause %<lastprivate%>", t);
1e8e9920 13159 remove = true;
13160 }
13161 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13162 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13163 {
e60a6f7b 13164 error_at (OMP_CLAUSE_LOCATION (c),
13165 "%qE appears more than once in data clauses", t);
1e8e9920 13166 remove = true;
13167 }
13168 else
13169 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13170 break;
13171
bc7bff74 13172 case OMP_CLAUSE_ALIGNED:
13173 t = OMP_CLAUSE_DECL (c);
f48c7f4a 13174 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
bc7bff74 13175 {
13176 error_at (OMP_CLAUSE_LOCATION (c),
13177 "%qE is not a variable in %<aligned%> clause", t);
13178 remove = true;
13179 }
23871d0c 13180 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13181 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13182 {
13183 error_at (OMP_CLAUSE_LOCATION (c),
13184 "%qE in %<aligned%> clause is neither a pointer nor "
13185 "an array", t);
13186 remove = true;
13187 }
0b80c4b2 13188 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13189 {
13190 error_at (OMP_CLAUSE_LOCATION (c),
13191 "%<_Atomic%> %qD in %<aligned%> clause", t);
13192 remove = true;
13193 break;
13194 }
bc7bff74 13195 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13196 {
13197 error_at (OMP_CLAUSE_LOCATION (c),
13198 "%qE appears more than once in %<aligned%> clauses",
13199 t);
13200 remove = true;
13201 }
13202 else
13203 bitmap_set_bit (&aligned_head, DECL_UID (t));
13204 break;
13205
13206 case OMP_CLAUSE_DEPEND:
13207 t = OMP_CLAUSE_DECL (c);
43895be5 13208 if (t == NULL_TREE)
13209 {
13210 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13211 == OMP_CLAUSE_DEPEND_SOURCE);
13212 break;
13213 }
13214 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13215 {
13216 gcc_assert (TREE_CODE (t) == TREE_LIST);
13217 for (; t; t = TREE_CHAIN (t))
13218 {
13219 tree decl = TREE_VALUE (t);
13220 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13221 {
13222 tree offset = TREE_PURPOSE (t);
13223 bool neg = wi::neg_p ((wide_int) offset);
13224 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13225 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13226 neg ? MINUS_EXPR : PLUS_EXPR,
13227 decl, offset);
13228 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
844cece0 13229 sizetype,
13230 fold_convert (sizetype, t2),
13231 fold_convert (sizetype, decl));
43895be5 13232 if (t2 == error_mark_node)
13233 {
13234 remove = true;
13235 break;
13236 }
13237 TREE_PURPOSE (t) = t2;
13238 }
13239 }
13240 break;
13241 }
bc7bff74 13242 if (TREE_CODE (t) == TREE_LIST)
13243 {
6d6a3fc3 13244 if (handle_omp_array_sections (c, ort))
bc7bff74 13245 remove = true;
13246 break;
13247 }
13248 if (t == error_mark_node)
13249 remove = true;
f48c7f4a 13250 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
bc7bff74 13251 {
13252 error_at (OMP_CLAUSE_LOCATION (c),
13253 "%qE is not a variable in %<depend%> clause", t);
13254 remove = true;
13255 }
13256 else if (!c_mark_addressable (t))
13257 remove = true;
13258 break;
13259
13260 case OMP_CLAUSE_MAP:
13261 case OMP_CLAUSE_TO:
13262 case OMP_CLAUSE_FROM:
ca4c3545 13263 case OMP_CLAUSE__CACHE_:
bc7bff74 13264 t = OMP_CLAUSE_DECL (c);
13265 if (TREE_CODE (t) == TREE_LIST)
13266 {
6d6a3fc3 13267 if (handle_omp_array_sections (c, ort))
bc7bff74 13268 remove = true;
13269 else
13270 {
13271 t = OMP_CLAUSE_DECL (c);
9ec099a3 13272 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
bc7bff74 13273 {
13274 error_at (OMP_CLAUSE_LOCATION (c),
13275 "array section does not have mappable type "
13276 "in %qs clause",
13277 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13278 remove = true;
13279 }
0b80c4b2 13280 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13281 {
13282 error_at (OMP_CLAUSE_LOCATION (c),
13283 "%<_Atomic%> %qE in %qs clause", t,
13284 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13285 remove = true;
13286 }
43895be5 13287 while (TREE_CODE (t) == ARRAY_REF)
13288 t = TREE_OPERAND (t, 0);
13289 if (TREE_CODE (t) == COMPONENT_REF
13290 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13291 {
13292 while (TREE_CODE (t) == COMPONENT_REF)
13293 t = TREE_OPERAND (t, 0);
13294 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13295 break;
13296 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13297 {
13298 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13299 error ("%qD appears more than once in motion"
13300 " clauses", t);
6d6a3fc3 13301 else if (ort == C_ORT_ACC)
13302 error ("%qD appears more than once in data"
13303 " clauses", t);
43895be5 13304 else
13305 error ("%qD appears more than once in map"
13306 " clauses", t);
13307 remove = true;
13308 }
13309 else
13310 {
13311 bitmap_set_bit (&map_head, DECL_UID (t));
13312 bitmap_set_bit (&map_field_head, DECL_UID (t));
13313 }
13314 }
bc7bff74 13315 }
13316 break;
13317 }
13318 if (t == error_mark_node)
43895be5 13319 {
13320 remove = true;
13321 break;
13322 }
13323 if (TREE_CODE (t) == COMPONENT_REF
b5e88f74 13324 && (ort & C_ORT_OMP)
43895be5 13325 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13326 {
13327 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13328 {
13329 error_at (OMP_CLAUSE_LOCATION (c),
13330 "bit-field %qE in %qs clause",
13331 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13332 remove = true;
13333 }
13334 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13335 {
13336 error_at (OMP_CLAUSE_LOCATION (c),
13337 "%qE does not have a mappable type in %qs clause",
13338 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13339 remove = true;
13340 }
0b80c4b2 13341 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13342 {
13343 error_at (OMP_CLAUSE_LOCATION (c),
13344 "%<_Atomic%> %qE in %qs clause", t,
13345 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13346 remove = true;
13347 }
43895be5 13348 while (TREE_CODE (t) == COMPONENT_REF)
13349 {
13350 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13351 == UNION_TYPE)
13352 {
13353 error_at (OMP_CLAUSE_LOCATION (c),
13354 "%qE is a member of a union", t);
13355 remove = true;
13356 break;
13357 }
13358 t = TREE_OPERAND (t, 0);
13359 }
13360 if (remove)
13361 break;
13362 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13363 {
9561765e 13364 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
43895be5 13365 break;
13366 }
13367 }
13368 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
bc7bff74 13369 {
13370 error_at (OMP_CLAUSE_LOCATION (c),
13371 "%qE is not a variable in %qs clause", t,
13372 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13373 remove = true;
13374 }
f48c7f4a 13375 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
bc7bff74 13376 {
13377 error_at (OMP_CLAUSE_LOCATION (c),
13378 "%qD is threadprivate variable in %qs clause", t,
13379 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13380 remove = true;
13381 }
a9833286 13382 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13383 || (OMP_CLAUSE_MAP_KIND (c)
13384 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13385 && !c_mark_addressable (t))
bc7bff74 13386 remove = true;
9ec099a3 13387 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
ca4c3545 13388 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
43895be5 13389 || (OMP_CLAUSE_MAP_KIND (c)
13390 == GOMP_MAP_FIRSTPRIVATE_POINTER)
ca4c3545 13391 || (OMP_CLAUSE_MAP_KIND (c)
13392 == GOMP_MAP_FORCE_DEVICEPTR)))
43895be5 13393 && t == OMP_CLAUSE_DECL (c)
9ec099a3 13394 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
bc7bff74 13395 {
13396 error_at (OMP_CLAUSE_LOCATION (c),
13397 "%qD does not have a mappable type in %qs clause", t,
13398 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13399 remove = true;
13400 }
0b80c4b2 13401 else if (TREE_TYPE (t) == error_mark_node)
13402 remove = true;
13403 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13404 {
13405 error_at (OMP_CLAUSE_LOCATION (c),
13406 "%<_Atomic%> %qE in %qs clause", t,
13407 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13408 remove = true;
13409 }
43895be5 13410 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13411 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13412 {
13413 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13414 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13415 {
13416 error ("%qD appears more than once in data clauses", t);
13417 remove = true;
13418 }
9561765e 13419 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
43895be5 13420 {
6d6a3fc3 13421 if (ort == C_ORT_ACC)
13422 error ("%qD appears more than once in data clauses", t);
13423 else
13424 error ("%qD appears both in data and map clauses", t);
9561765e 13425 remove = true;
43895be5 13426 }
9561765e 13427 else
13428 bitmap_set_bit (&generic_head, DECL_UID (t));
43895be5 13429 }
13430 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
bc7bff74 13431 {
13432 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13433 error ("%qD appears more than once in motion clauses", t);
6d6a3fc3 13434 else if (ort == C_ORT_ACC)
13435 error ("%qD appears more than once in data clauses", t);
bc7bff74 13436 else
13437 error ("%qD appears more than once in map clauses", t);
13438 remove = true;
13439 }
9561765e 13440 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13441 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13442 {
6d6a3fc3 13443 if (ort == C_ORT_ACC)
13444 error ("%qD appears more than once in data clauses", t);
13445 else
13446 error ("%qD appears both in data and map clauses", t);
9561765e 13447 remove = true;
13448 }
bc7bff74 13449 else
43895be5 13450 {
13451 bitmap_set_bit (&map_head, DECL_UID (t));
13452 if (t != OMP_CLAUSE_DECL (c)
13453 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13454 bitmap_set_bit (&map_field_head, DECL_UID (t));
13455 }
13456 break;
13457
13458 case OMP_CLAUSE_TO_DECLARE:
43895be5 13459 case OMP_CLAUSE_LINK:
13460 t = OMP_CLAUSE_DECL (c);
9561765e 13461 if (TREE_CODE (t) == FUNCTION_DECL
13462 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13463 ;
13464 else if (!VAR_P (t))
43895be5 13465 {
9561765e 13466 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13467 error_at (OMP_CLAUSE_LOCATION (c),
13468 "%qE is neither a variable nor a function name in "
13469 "clause %qs", t,
13470 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13471 else
13472 error_at (OMP_CLAUSE_LOCATION (c),
13473 "%qE is not a variable in clause %qs", t,
13474 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
43895be5 13475 remove = true;
13476 }
13477 else if (DECL_THREAD_LOCAL_P (t))
13478 {
13479 error_at (OMP_CLAUSE_LOCATION (c),
13480 "%qD is threadprivate variable in %qs clause", t,
13481 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13482 remove = true;
13483 }
13484 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13485 {
13486 error_at (OMP_CLAUSE_LOCATION (c),
13487 "%qD does not have a mappable type in %qs clause", t,
13488 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13489 remove = true;
13490 }
9561765e 13491 if (remove)
13492 break;
13493 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13494 {
13495 error_at (OMP_CLAUSE_LOCATION (c),
13496 "%qE appears more than once on the same "
13497 "%<declare target%> directive", t);
13498 remove = true;
13499 }
13500 else
13501 bitmap_set_bit (&generic_head, DECL_UID (t));
bc7bff74 13502 break;
13503
13504 case OMP_CLAUSE_UNIFORM:
13505 t = OMP_CLAUSE_DECL (c);
13506 if (TREE_CODE (t) != PARM_DECL)
13507 {
13508 if (DECL_P (t))
13509 error_at (OMP_CLAUSE_LOCATION (c),
13510 "%qD is not an argument in %<uniform%> clause", t);
13511 else
13512 error_at (OMP_CLAUSE_LOCATION (c),
13513 "%qE is not an argument in %<uniform%> clause", t);
13514 remove = true;
df205251 13515 break;
bc7bff74 13516 }
9561765e 13517 /* map_head bitmap is used as uniform_head if declare_simd. */
13518 bitmap_set_bit (&map_head, DECL_UID (t));
df205251 13519 goto check_dup_generic;
bc7bff74 13520
43895be5 13521 case OMP_CLAUSE_IS_DEVICE_PTR:
13522 case OMP_CLAUSE_USE_DEVICE_PTR:
13523 t = OMP_CLAUSE_DECL (c);
13524 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13525 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13526 {
13527 error_at (OMP_CLAUSE_LOCATION (c),
13528 "%qs variable is neither a pointer nor an array",
13529 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13530 remove = true;
13531 }
13532 goto check_dup_generic;
13533
bc7bff74 13534 case OMP_CLAUSE_NOWAIT:
13535 if (copyprivate_seen)
13536 {
13537 error_at (OMP_CLAUSE_LOCATION (c),
13538 "%<nowait%> clause must not be used together "
13539 "with %<copyprivate%>");
13540 remove = true;
13541 break;
13542 }
13543 nowait_clause = pc;
13544 pc = &OMP_CLAUSE_CHAIN (c);
13545 continue;
13546
1e8e9920 13547 case OMP_CLAUSE_IF:
13548 case OMP_CLAUSE_NUM_THREADS:
bc7bff74 13549 case OMP_CLAUSE_NUM_TEAMS:
13550 case OMP_CLAUSE_THREAD_LIMIT:
1e8e9920 13551 case OMP_CLAUSE_DEFAULT:
fd6481cf 13552 case OMP_CLAUSE_UNTIED:
13553 case OMP_CLAUSE_COLLAPSE:
2169f33b 13554 case OMP_CLAUSE_FINAL:
13555 case OMP_CLAUSE_MERGEABLE:
bc7bff74 13556 case OMP_CLAUSE_DEVICE:
13557 case OMP_CLAUSE_DIST_SCHEDULE:
13558 case OMP_CLAUSE_PARALLEL:
13559 case OMP_CLAUSE_FOR:
13560 case OMP_CLAUSE_SECTIONS:
13561 case OMP_CLAUSE_TASKGROUP:
13562 case OMP_CLAUSE_PROC_BIND:
43895be5 13563 case OMP_CLAUSE_PRIORITY:
13564 case OMP_CLAUSE_GRAINSIZE:
13565 case OMP_CLAUSE_NUM_TASKS:
13566 case OMP_CLAUSE_NOGROUP:
13567 case OMP_CLAUSE_THREADS:
13568 case OMP_CLAUSE_SIMD:
13569 case OMP_CLAUSE_HINT:
13570 case OMP_CLAUSE_DEFAULTMAP:
40750995 13571 case OMP_CLAUSE__CILK_FOR_COUNT_:
ca4c3545 13572 case OMP_CLAUSE_NUM_GANGS:
13573 case OMP_CLAUSE_NUM_WORKERS:
13574 case OMP_CLAUSE_VECTOR_LENGTH:
13575 case OMP_CLAUSE_ASYNC:
13576 case OMP_CLAUSE_WAIT:
13577 case OMP_CLAUSE_AUTO:
ef014f95 13578 case OMP_CLAUSE_INDEPENDENT:
ca4c3545 13579 case OMP_CLAUSE_SEQ:
13580 case OMP_CLAUSE_GANG:
13581 case OMP_CLAUSE_WORKER:
13582 case OMP_CLAUSE_VECTOR:
ef014f95 13583 case OMP_CLAUSE_TILE:
bc7bff74 13584 pc = &OMP_CLAUSE_CHAIN (c);
13585 continue;
13586
9561765e 13587 case OMP_CLAUSE_SCHEDULE:
13588 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
13589 {
13590 const char *p = NULL;
13591 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
13592 {
13593 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
13594 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
13595 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
13596 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
13597 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
13598 default: gcc_unreachable ();
13599 }
13600 if (p)
13601 {
13602 error_at (OMP_CLAUSE_LOCATION (c),
13603 "%<nonmonotonic%> modifier specified for %qs "
13604 "schedule kind", p);
13605 OMP_CLAUSE_SCHEDULE_KIND (c)
13606 = (enum omp_clause_schedule_kind)
13607 (OMP_CLAUSE_SCHEDULE_KIND (c)
13608 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13609 }
13610 }
13611 schedule_clause = c;
13612 pc = &OMP_CLAUSE_CHAIN (c);
13613 continue;
13614
13615 case OMP_CLAUSE_ORDERED:
13616 ordered_seen = true;
13617 pc = &OMP_CLAUSE_CHAIN (c);
13618 continue;
13619
43895be5 13620 case OMP_CLAUSE_SAFELEN:
13621 safelen = c;
13622 pc = &OMP_CLAUSE_CHAIN (c);
13623 continue;
13624 case OMP_CLAUSE_SIMDLEN:
13625 simdlen = c;
13626 pc = &OMP_CLAUSE_CHAIN (c);
13627 continue;
13628
bc7bff74 13629 case OMP_CLAUSE_INBRANCH:
13630 case OMP_CLAUSE_NOTINBRANCH:
13631 if (branch_seen)
13632 {
13633 error_at (OMP_CLAUSE_LOCATION (c),
13634 "%<inbranch%> clause is incompatible with "
13635 "%<notinbranch%>");
13636 remove = true;
13637 break;
13638 }
13639 branch_seen = true;
1e8e9920 13640 pc = &OMP_CLAUSE_CHAIN (c);
13641 continue;
13642
13643 default:
13644 gcc_unreachable ();
13645 }
13646
13647 if (!remove)
13648 {
13649 t = OMP_CLAUSE_DECL (c);
13650
13651 if (need_complete)
13652 {
22a3f7bd 13653 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
1e8e9920 13654 if (t == error_mark_node)
13655 remove = true;
13656 }
13657
13658 if (need_implicitly_determined)
13659 {
13660 const char *share_name = NULL;
13661
f48c7f4a 13662 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
1e8e9920 13663 share_name = "threadprivate";
13664 else switch (c_omp_predetermined_sharing (t))
13665 {
13666 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
13667 break;
13668 case OMP_CLAUSE_DEFAULT_SHARED:
2169f33b 13669 /* const vars may be specified in firstprivate clause. */
13670 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
13671 && TREE_READONLY (t))
13672 break;
1e8e9920 13673 share_name = "shared";
13674 break;
13675 case OMP_CLAUSE_DEFAULT_PRIVATE:
13676 share_name = "private";
13677 break;
13678 default:
13679 gcc_unreachable ();
13680 }
13681 if (share_name)
13682 {
e60a6f7b 13683 error_at (OMP_CLAUSE_LOCATION (c),
13684 "%qE is predetermined %qs for %qs",
bc7bff74 13685 t, share_name,
13686 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
1e8e9920 13687 remove = true;
13688 }
13689 }
13690 }
13691
13692 if (remove)
13693 *pc = OMP_CLAUSE_CHAIN (c);
13694 else
13695 pc = &OMP_CLAUSE_CHAIN (c);
13696 }
13697
43895be5 13698 if (simdlen
13699 && safelen
13700 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
13701 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
13702 {
13703 error_at (OMP_CLAUSE_LOCATION (simdlen),
13704 "%<simdlen%> clause value is bigger than "
13705 "%<safelen%> clause value");
13706 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
13707 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
13708 }
13709
9561765e 13710 if (ordered_seen
13711 && schedule_clause
13712 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13713 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
13714 {
13715 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
13716 "%<nonmonotonic%> schedule modifier specified together "
13717 "with %<ordered%> clause");
13718 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13719 = (enum omp_clause_schedule_kind)
13720 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
13721 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
13722 }
13723
13724 if (linear_variable_step_check)
13725 for (pc = &clauses, c = clauses; c ; c = *pc)
13726 {
13727 bool remove = false;
13728 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
13729 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
13730 && !bitmap_bit_p (&map_head,
13731 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
13732 {
13733 error_at (OMP_CLAUSE_LOCATION (c),
13734 "%<linear%> clause step is a parameter %qD not "
13735 "specified in %<uniform%> clause",
13736 OMP_CLAUSE_LINEAR_STEP (c));
13737 remove = true;
13738 }
13739
13740 if (remove)
13741 *pc = OMP_CLAUSE_CHAIN (c);
13742 else
13743 pc = &OMP_CLAUSE_CHAIN (c);
13744 }
13745
1e8e9920 13746 bitmap_obstack_release (NULL);
13747 return clauses;
13748}
796735dc 13749
0b80c4b2 13750/* Return code to initialize DST with a copy constructor from SRC.
13751 C doesn't have copy constructors nor assignment operators, only for
13752 _Atomic vars we need to perform __atomic_load from src into a temporary
13753 followed by __atomic_store of the temporary to dst. */
13754
13755tree
13756c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
13757{
13758 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
13759 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
13760
13761 location_t loc = OMP_CLAUSE_LOCATION (clause);
13762 tree type = TREE_TYPE (dst);
13763 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
13764 tree tmp = create_tmp_var (nonatomic_type);
13765 tree tmp_addr = build_fold_addr_expr (tmp);
13766 TREE_ADDRESSABLE (tmp) = 1;
13767 TREE_NO_WARNING (tmp) = 1;
13768 tree src_addr = build_fold_addr_expr (src);
13769 tree dst_addr = build_fold_addr_expr (dst);
13770 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
13771 vec<tree, va_gc> *params;
13772 /* Expansion of a generic atomic load may require an addition
13773 element, so allocate enough to prevent a resize. */
13774 vec_alloc (params, 4);
13775
13776 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
13777 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
13778 params->quick_push (src_addr);
13779 params->quick_push (tmp_addr);
13780 params->quick_push (seq_cst);
13781 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13782
13783 vec_alloc (params, 4);
13784
13785 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
13786 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
13787 params->quick_push (dst_addr);
13788 params->quick_push (tmp_addr);
13789 params->quick_push (seq_cst);
13790 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
13791 return build2 (COMPOUND_EXPR, void_type_node, load, store);
13792}
13793
4c0315d0 13794/* Create a transaction node. */
13795
13796tree
13797c_finish_transaction (location_t loc, tree block, int flags)
13798{
13799 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
13800 if (flags & TM_STMT_ATTR_OUTER)
13801 TRANSACTION_EXPR_OUTER (stmt) = 1;
13802 if (flags & TM_STMT_ATTR_RELAXED)
13803 TRANSACTION_EXPR_RELAXED (stmt) = 1;
13804 return add_stmt (stmt);
13805}
13806
796735dc 13807/* Make a variant type in the proper way for C/C++, propagating qualifiers
89f16034 13808 down to the element type of an array. If ORIG_QUAL_TYPE is not
13809 NULL, then it should be used as the qualified type
13810 ORIG_QUAL_INDIRECT levels down in array type derivation (to
13811 preserve information about the typedef name from which an array
13812 type was derived). */
796735dc 13813
13814tree
89f16034 13815c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
13816 size_t orig_qual_indirect)
796735dc 13817{
13818 if (type == error_mark_node)
13819 return type;
13820
13821 if (TREE_CODE (type) == ARRAY_TYPE)
13822 {
13823 tree t;
13824 tree element_type = c_build_qualified_type (TREE_TYPE (type),
89f16034 13825 type_quals, orig_qual_type,
13826 orig_qual_indirect - 1);
796735dc 13827
13828 /* See if we already have an identically qualified type. */
89f16034 13829 if (orig_qual_type && orig_qual_indirect == 0)
13830 t = orig_qual_type;
13831 else
13832 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
13833 {
13834 if (TYPE_QUALS (strip_array_types (t)) == type_quals
13835 && TYPE_NAME (t) == TYPE_NAME (type)
13836 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
13837 && attribute_list_equal (TYPE_ATTRIBUTES (t),
13838 TYPE_ATTRIBUTES (type)))
13839 break;
13840 }
796735dc 13841 if (!t)
13842 {
13843 tree domain = TYPE_DOMAIN (type);
13844
13845 t = build_variant_type_copy (type);
13846 TREE_TYPE (t) = element_type;
13847
13848 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
13849 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
13850 SET_TYPE_STRUCTURAL_EQUALITY (t);
13851 else if (TYPE_CANONICAL (element_type) != element_type
13852 || (domain && TYPE_CANONICAL (domain) != domain))
13853 {
48e1416a 13854 tree unqualified_canon
796735dc 13855 = build_array_type (TYPE_CANONICAL (element_type),
48e1416a 13856 domain? TYPE_CANONICAL (domain)
796735dc 13857 : NULL_TREE);
292237f3 13858 if (TYPE_REVERSE_STORAGE_ORDER (type))
13859 {
13860 unqualified_canon
13861 = build_distinct_type_copy (unqualified_canon);
13862 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
13863 }
48e1416a 13864 TYPE_CANONICAL (t)
796735dc 13865 = c_build_qualified_type (unqualified_canon, type_quals);
13866 }
13867 else
13868 TYPE_CANONICAL (t) = t;
13869 }
13870 return t;
13871 }
13872
13873 /* A restrict-qualified pointer type must be a pointer to object or
13874 incomplete type. Note that the use of POINTER_TYPE_P also allows
13875 REFERENCE_TYPEs, which is appropriate for C++. */
13876 if ((type_quals & TYPE_QUAL_RESTRICT)
13877 && (!POINTER_TYPE_P (type)
13878 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
13879 {
13880 error ("invalid use of %<restrict%>");
13881 type_quals &= ~TYPE_QUAL_RESTRICT;
13882 }
13883
89f16034 13884 tree var_type = (orig_qual_type && orig_qual_indirect == 0
13885 ? orig_qual_type
13886 : build_qualified_type (type, type_quals));
7b15d957 13887 /* A variant type does not inherit the list of incomplete vars from the
13888 type main variant. */
1af10ce6 13889 if (RECORD_OR_UNION_TYPE_P (var_type)
13890 && TYPE_MAIN_VARIANT (var_type) != var_type)
7b15d957 13891 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
13892 return var_type;
796735dc 13893}
d62e827b 13894
13895/* Build a VA_ARG_EXPR for the C parser. */
13896
13897tree
a4ab54ac 13898c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
d62e827b 13899{
2cd7caae 13900 if (error_operand_p (type))
13901 return error_mark_node;
a4ab54ac 13902 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
13903 order because it takes the address of the expression. */
13904 else if (handled_component_p (expr)
13905 && reverse_storage_order_for_component_p (expr))
13906 {
13907 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
13908 return error_mark_node;
13909 }
2cd7caae 13910 else if (!COMPLETE_TYPE_P (type))
5580bf42 13911 {
a4ab54ac 13912 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
2cd7caae 13913 "type %qT", type);
5580bf42 13914 return error_mark_node;
13915 }
2cd7caae 13916 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
a4ab54ac 13917 warning_at (loc2, OPT_Wc___compat,
2cd7caae 13918 "C++ requires promoted type, not enum type, in %<va_arg%>");
a4ab54ac 13919 return build_va_arg (loc2, expr, type);
d62e827b 13920}
bc7bff74 13921
13922/* Return truthvalue of whether T1 is the same tree structure as T2.
13923 Return 1 if they are the same. Return 0 if they are different. */
13924
13925bool
13926c_tree_equal (tree t1, tree t2)
13927{
13928 enum tree_code code1, code2;
13929
13930 if (t1 == t2)
13931 return true;
13932 if (!t1 || !t2)
13933 return false;
13934
13935 for (code1 = TREE_CODE (t1);
13936 CONVERT_EXPR_CODE_P (code1)
13937 || code1 == NON_LVALUE_EXPR;
13938 code1 = TREE_CODE (t1))
13939 t1 = TREE_OPERAND (t1, 0);
13940 for (code2 = TREE_CODE (t2);
13941 CONVERT_EXPR_CODE_P (code2)
13942 || code2 == NON_LVALUE_EXPR;
13943 code2 = TREE_CODE (t2))
13944 t2 = TREE_OPERAND (t2, 0);
13945
13946 /* They might have become equal now. */
13947 if (t1 == t2)
13948 return true;
13949
13950 if (code1 != code2)
13951 return false;
13952
13953 switch (code1)
13954 {
13955 case INTEGER_CST:
6b409616 13956 return wi::eq_p (t1, t2);
bc7bff74 13957
13958 case REAL_CST:
20cb53c9 13959 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
bc7bff74 13960
13961 case STRING_CST:
13962 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
13963 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
13964 TREE_STRING_LENGTH (t1));
13965
13966 case FIXED_CST:
13967 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
13968 TREE_FIXED_CST (t2));
13969
13970 case COMPLEX_CST:
13971 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
13972 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
13973
13974 case VECTOR_CST:
13975 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
13976
13977 case CONSTRUCTOR:
13978 /* We need to do this when determining whether or not two
13979 non-type pointer to member function template arguments
13980 are the same. */
13981 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
13982 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
13983 return false;
13984 {
13985 tree field, value;
13986 unsigned int i;
13987 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
13988 {
13989 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
13990 if (!c_tree_equal (field, elt2->index)
13991 || !c_tree_equal (value, elt2->value))
13992 return false;
13993 }
13994 }
13995 return true;
13996
13997 case TREE_LIST:
13998 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
13999 return false;
14000 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14001 return false;
14002 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14003
14004 case SAVE_EXPR:
14005 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14006
14007 case CALL_EXPR:
14008 {
14009 tree arg1, arg2;
14010 call_expr_arg_iterator iter1, iter2;
14011 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14012 return false;
14013 for (arg1 = first_call_expr_arg (t1, &iter1),
14014 arg2 = first_call_expr_arg (t2, &iter2);
14015 arg1 && arg2;
14016 arg1 = next_call_expr_arg (&iter1),
14017 arg2 = next_call_expr_arg (&iter2))
14018 if (!c_tree_equal (arg1, arg2))
14019 return false;
14020 if (arg1 || arg2)
14021 return false;
14022 return true;
14023 }
14024
14025 case TARGET_EXPR:
14026 {
14027 tree o1 = TREE_OPERAND (t1, 0);
14028 tree o2 = TREE_OPERAND (t2, 0);
14029
14030 /* Special case: if either target is an unallocated VAR_DECL,
14031 it means that it's going to be unified with whatever the
14032 TARGET_EXPR is really supposed to initialize, so treat it
14033 as being equivalent to anything. */
f48c7f4a 14034 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
bc7bff74 14035 && !DECL_RTL_SET_P (o1))
14036 /*Nop*/;
f48c7f4a 14037 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
bc7bff74 14038 && !DECL_RTL_SET_P (o2))
14039 /*Nop*/;
14040 else if (!c_tree_equal (o1, o2))
14041 return false;
14042
14043 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14044 }
14045
14046 case COMPONENT_REF:
14047 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14048 return false;
14049 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14050
14051 case PARM_DECL:
14052 case VAR_DECL:
14053 case CONST_DECL:
14054 case FIELD_DECL:
14055 case FUNCTION_DECL:
14056 case IDENTIFIER_NODE:
14057 case SSA_NAME:
14058 return false;
14059
14060 case TREE_VEC:
14061 {
14062 unsigned ix;
14063 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14064 return false;
14065 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14066 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14067 TREE_VEC_ELT (t2, ix)))
14068 return false;
14069 return true;
14070 }
14071
14072 default:
14073 break;
14074 }
14075
14076 switch (TREE_CODE_CLASS (code1))
14077 {
14078 case tcc_unary:
14079 case tcc_binary:
14080 case tcc_comparison:
14081 case tcc_expression:
14082 case tcc_vl_exp:
14083 case tcc_reference:
14084 case tcc_statement:
14085 {
14086 int i, n = TREE_OPERAND_LENGTH (t1);
14087
14088 switch (code1)
14089 {
14090 case PREINCREMENT_EXPR:
14091 case PREDECREMENT_EXPR:
14092 case POSTINCREMENT_EXPR:
14093 case POSTDECREMENT_EXPR:
14094 n = 1;
14095 break;
14096 case ARRAY_REF:
14097 n = 2;
14098 break;
14099 default:
14100 break;
14101 }
14102
14103 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14104 && n != TREE_OPERAND_LENGTH (t2))
14105 return false;
14106
14107 for (i = 0; i < n; ++i)
14108 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14109 return false;
14110
14111 return true;
14112 }
14113
14114 case tcc_type:
14115 return comptypes (t1, t2);
14116 default:
14117 gcc_unreachable ();
14118 }
14119 /* We can get here with --disable-checking. */
14120 return false;
14121}
433e804e 14122
14123/* Inserts "cleanup" functions after the function-body of FNDECL. FNDECL is a
14124 spawn-helper and BODY is the newly created body for FNDECL. */
14125
14126void
14127cilk_install_body_with_frame_cleanup (tree fndecl, tree body, void *w)
14128{
14129 tree list = alloc_stmt_list ();
14130 tree frame = make_cilk_frame (fndecl);
14131 tree dtor = create_cilk_function_exit (frame, false, true);
14132 add_local_decl (cfun, frame);
14133
14134 DECL_SAVED_TREE (fndecl) = list;
14135 tree frame_ptr = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (frame)),
14136 frame);
14137 tree body_list = cilk_install_body_pedigree_operations (frame_ptr);
14138 gcc_assert (TREE_CODE (body_list) == STATEMENT_LIST);
14139
14140 tree detach_expr = build_call_expr (cilk_detach_fndecl, 1, frame_ptr);
14141 append_to_statement_list (detach_expr, &body_list);
14142
14143 cilk_outline (fndecl, &body, (struct wrapper_data *) w);
14144 body = fold_build_cleanup_point_expr (void_type_node, body);
14145
14146 append_to_statement_list (body, &body_list);
14147 append_to_statement_list (build_stmt (EXPR_LOCATION (body), TRY_FINALLY_EXPR,
14148 body_list, dtor), &list);
14149}
547c6b1f 14150
14151/* Returns true when the function declaration FNDECL is implicit,
14152 introduced as a result of a call to an otherwise undeclared
14153 function, and false otherwise. */
14154
14155bool
14156c_decl_implicit (const_tree fndecl)
14157{
14158 return C_DECL_IMPLICIT (fndecl);
14159}