]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/c/c-typeck.c
Add support for grouping of related diagnostics (PR other/84889)
[thirdparty/gcc.git] / gcc / c / c-typeck.c
CommitLineData
400fbf9f 1/* Build expressions with type checking for C compiler.
85ec4feb 2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
400fbf9f 3
1322177d 4This file is part of GCC.
400fbf9f 5
1322177d
LB
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
9dcd6f09 8Software Foundation; either version 3, or (at your option) any later
1322177d 9version.
400fbf9f 10
1322177d
LB
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
400fbf9f
JW
15
16You should have received a copy of the GNU General Public License
9dcd6f09
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
400fbf9f
JW
19
20
21/* This file is part of the C front end.
22 It contains routines to build C expressions given their operands,
23 including computing the types of the result, C-specific error checks,
5088b058 24 and some optimization. */
400fbf9f
JW
25
26#include "config.h"
670ee920 27#include "system.h"
4977bab6 28#include "coretypes.h"
4d0cdd0c 29#include "memmodel.h"
2adfab87
AM
30#include "target.h"
31#include "function.h"
32#include "bitmap.h"
2adfab87
AM
33#include "c-tree.h"
34#include "gimple-expr.h"
35#include "predict.h"
d8a2d370
DN
36#include "stor-layout.h"
37#include "trans-mem.h"
38#include "varasm.h"
39#include "stmt.h"
e57e265b 40#include "langhooks.h"
29cc57cf 41#include "c-lang.h"
ab87f8c8 42#include "intl.h"
325c3691 43#include "tree-iterator.h"
45b0be94 44#include "gimplify.h"
acf0174b 45#include "tree-inline.h"
629b3d75 46#include "omp-general.h"
61d3ce20 47#include "c-family/c-objc.h"
de5a5fa1 48#include "c-family/c-ubsan.h"
41dbbb37 49#include "gomp-constants.h"
6a3f203c 50#include "spellcheck-tree.h"
745e411d 51#include "gcc-rich-location.h"
314e6352
ML
52#include "stringpool.h"
53#include "attribs.h"
45b2222a 54#include "asan.h"
325c3691 55
2ac2f164
JM
56/* Possible cases of implicit bad conversions. Used to select
57 diagnostic messages in convert_for_assignment. */
58enum impl_conv {
59 ic_argpass,
60 ic_assign,
61 ic_init,
62 ic_return
63};
64
bc4b653b
JM
65/* The level of nesting inside "__alignof__". */
66int in_alignof;
67
68/* The level of nesting inside "sizeof". */
69int in_sizeof;
70
71/* The level of nesting inside "typeof". */
72int in_typeof;
400fbf9f 73
1a4049e7
JJ
74/* The argument of last parsed sizeof expression, only to be tested
75 if expr.original_code == SIZEOF_EXPR. */
76tree c_last_sizeof_arg;
40ffd95f 77location_t c_last_sizeof_loc;
1a4049e7 78
9bac5cbb
G
79/* Nonzero if we might need to print a "missing braces around
80 initializer" message within this initializer. */
81static int found_missing_braces;
103b7b17 82
bf730f15
RS
83static int require_constant_value;
84static int require_constant_elements;
85
58f9752a 86static bool null_pointer_constant_p (const_tree);
f55ade6e 87static tree qualify_type (tree, tree);
dc5027f4
JM
88static int tagged_types_tu_compatible_p (const_tree, const_tree, bool *,
89 bool *);
744aa42f 90static int comp_target_types (location_t, tree, tree);
dc5027f4
JM
91static int function_types_compatible_p (const_tree, const_tree, bool *,
92 bool *);
93static int type_lists_compatible_p (const_tree, const_tree, bool *, bool *);
f55ade6e 94static tree lookup_field (tree, tree);
81e5eca8
MP
95static int convert_arguments (location_t, vec<location_t>, tree,
96 vec<tree, va_gc> *, vec<tree, va_gc> *, tree,
97 tree);
05abad4c 98static tree pointer_diff (location_t, tree, tree, tree *);
68fca595 99static tree convert_for_assignment (location_t, location_t, tree, tree, tree,
744aa42f 100 enum impl_conv, bool, tree, tree, int);
f55ade6e
AJ
101static tree valid_compound_expr_initializer (tree, tree);
102static void push_string (const char *);
103static void push_member_name (tree);
f55ade6e
AJ
104static int spelling_length (void);
105static char *print_spelling (char *);
96b40f8d 106static void warning_init (location_t, int, const char *);
c2255bc4 107static tree digest_init (location_t, tree, tree, tree, bool, bool, int);
30af3a2b 108static void output_init_element (location_t, tree, tree, bool, tree, tree, bool,
34cf811f 109 bool, struct obstack *);
a1e3b3d9 110static void output_pending_init_elements (int, struct obstack *);
30af3a2b 111static bool set_designator (location_t, bool, struct obstack *);
a1e3b3d9 112static void push_range_stack (tree, struct obstack *);
96b40f8d
MP
113static void add_pending_init (location_t, tree, tree, tree, bool,
114 struct obstack *);
a1e3b3d9
LB
115static void set_nonincremental_init (struct obstack *);
116static void set_nonincremental_init_from_string (tree, struct obstack *);
117static tree find_init_member (tree, struct obstack *);
f37acdf9 118static void readonly_warning (tree, enum lvalue_use);
7bd11157 119static int lvalue_or_else (location_t, const_tree, enum lvalue_use);
4e2fb7de 120static void record_maybe_used_decl (tree);
dc5027f4 121static int comptypes_internal (const_tree, const_tree, bool *, bool *);
6aa3c60d
JM
122\f
123/* Return true if EXP is a null pointer constant, false otherwise. */
124
125static bool
58f9752a 126null_pointer_constant_p (const_tree expr)
6aa3c60d
JM
127{
128 /* This should really operate on c_expr structures, but they aren't
129 yet available everywhere required. */
130 tree type = TREE_TYPE (expr);
131 return (TREE_CODE (expr) == INTEGER_CST
8bcd6380 132 && !TREE_OVERFLOW (expr)
6aa3c60d
JM
133 && integer_zerop (expr)
134 && (INTEGRAL_TYPE_P (type)
135 || (TREE_CODE (type) == POINTER_TYPE
136 && VOID_TYPE_P (TREE_TYPE (type))
137 && TYPE_QUALS (TREE_TYPE (type)) == TYPE_UNQUALIFIED)));
138}
928c19bb
JM
139
140/* EXPR may appear in an unevaluated part of an integer constant
141 expression, but not in an evaluated part. Wrap it in a
142 C_MAYBE_CONST_EXPR, or mark it with TREE_OVERFLOW if it is just an
143 INTEGER_CST and we cannot create a C_MAYBE_CONST_EXPR. */
144
145static tree
146note_integer_operands (tree expr)
147{
148 tree ret;
149 if (TREE_CODE (expr) == INTEGER_CST && in_late_binary_op)
150 {
151 ret = copy_node (expr);
152 TREE_OVERFLOW (ret) = 1;
153 }
154 else
155 {
156 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (expr), NULL_TREE, expr);
157 C_MAYBE_CONST_EXPR_INT_OPERANDS (ret) = 1;
158 }
159 return ret;
160}
161
4d84fe7c
JM
162/* Having checked whether EXPR may appear in an unevaluated part of an
163 integer constant expression and found that it may, remove any
164 C_MAYBE_CONST_EXPR noting this fact and return the resulting
165 expression. */
166
167static inline tree
168remove_c_maybe_const_expr (tree expr)
169{
170 if (TREE_CODE (expr) == C_MAYBE_CONST_EXPR)
171 return C_MAYBE_CONST_EXPR_EXPR (expr);
172 else
173 return expr;
174}
175
f13c9b2c
AP
176\f/* This is a cache to hold if two types are compatible or not. */
177
178struct tagged_tu_seen_cache {
179 const struct tagged_tu_seen_cache * next;
58f9752a
KG
180 const_tree t1;
181 const_tree t2;
f13c9b2c
AP
182 /* The return value of tagged_types_tu_compatible_p if we had seen
183 these two types already. */
184 int val;
185};
186
187static const struct tagged_tu_seen_cache * tagged_tu_seen_base;
188static void free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *);
189
4f2e1536
MP
190/* Do `exp = require_complete_type (loc, exp);' to make sure exp
191 does not have an incomplete type. (That includes void types.)
192 LOC is the location of the use. */
400fbf9f
JW
193
194tree
4f2e1536 195require_complete_type (location_t loc, tree value)
400fbf9f
JW
196{
197 tree type = TREE_TYPE (value);
198
7a0ca710 199 if (error_operand_p (value))
ea0f786b
CB
200 return error_mark_node;
201
400fbf9f 202 /* First, detect a valid value with a complete type. */
d0f062fb 203 if (COMPLETE_TYPE_P (type))
400fbf9f
JW
204 return value;
205
4f2e1536 206 c_incomplete_type_error (loc, value, type);
400fbf9f
JW
207 return error_mark_node;
208}
209
210/* Print an error message for invalid use of an incomplete type.
211 VALUE is the expression that was used (or 0 if that isn't known)
4f2e1536
MP
212 and TYPE is the type that was invalid. LOC is the location for
213 the error. */
400fbf9f
JW
214
215void
4f2e1536 216c_incomplete_type_error (location_t loc, const_tree value, const_tree type)
400fbf9f 217{
400fbf9f
JW
218 /* Avoid duplicate error message. */
219 if (TREE_CODE (type) == ERROR_MARK)
220 return;
221
3fa8871b 222 if (value != NULL_TREE && (VAR_P (value) || TREE_CODE (value) == PARM_DECL))
4f2e1536 223 error_at (loc, "%qD has an incomplete type %qT", value, type);
400fbf9f
JW
224 else
225 {
226 retry:
227 /* We must print an error message. Be clever about what it says. */
228
229 switch (TREE_CODE (type))
230 {
231 case RECORD_TYPE:
400fbf9f 232 case UNION_TYPE:
400fbf9f 233 case ENUMERAL_TYPE:
400fbf9f
JW
234 break;
235
236 case VOID_TYPE:
4f2e1536 237 error_at (loc, "invalid use of void expression");
400fbf9f
JW
238 return;
239
240 case ARRAY_TYPE:
241 if (TYPE_DOMAIN (type))
242 {
fba78abb
RH
243 if (TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL)
244 {
4f2e1536 245 error_at (loc, "invalid use of flexible array member");
fba78abb
RH
246 return;
247 }
400fbf9f
JW
248 type = TREE_TYPE (type);
249 goto retry;
250 }
4f2e1536 251 error_at (loc, "invalid use of array with unspecified bounds");
400fbf9f
JW
252 return;
253
254 default:
366de0ce 255 gcc_unreachable ();
400fbf9f
JW
256 }
257
258 if (TREE_CODE (TYPE_NAME (type)) == IDENTIFIER_NODE)
4f2e1536 259 error_at (loc, "invalid use of undefined type %qT", type);
400fbf9f
JW
260 else
261 /* If this type has a typedef-name, the TYPE_NAME is a TYPE_DECL. */
4f2e1536 262 error_at (loc, "invalid use of incomplete typedef %qT", type);
400fbf9f
JW
263 }
264}
265
ab393bf1
NB
266/* Given a type, apply default promotions wrt unnamed function
267 arguments and return the new type. */
268
269tree
2f6e4e97 270c_type_promotes_to (tree type)
ab393bf1 271{
267bac10 272 tree ret = NULL_TREE;
ab393bf1 273
267bac10
JM
274 if (TYPE_MAIN_VARIANT (type) == float_type_node)
275 ret = double_type_node;
276 else if (c_promoting_integer_type_p (type))
ab393bf1
NB
277 {
278 /* Preserve unsignedness if not really getting any wider. */
8df83eae 279 if (TYPE_UNSIGNED (type)
c22cacf3 280 && (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
267bac10
JM
281 ret = unsigned_type_node;
282 else
283 ret = integer_type_node;
ab393bf1
NB
284 }
285
267bac10
JM
286 if (ret != NULL_TREE)
287 return (TYPE_ATOMIC (type)
288 ? c_build_qualified_type (ret, TYPE_QUAL_ATOMIC)
289 : ret);
290
ab393bf1
NB
291 return type;
292}
293
36c5e70a
BE
294/* Return true if between two named address spaces, whether there is a superset
295 named address space that encompasses both address spaces. If there is a
296 superset, return which address space is the superset. */
297
298static bool
299addr_space_superset (addr_space_t as1, addr_space_t as2, addr_space_t *common)
300{
301 if (as1 == as2)
302 {
303 *common = as1;
304 return true;
305 }
306 else if (targetm.addr_space.subset_p (as1, as2))
307 {
308 *common = as2;
309 return true;
310 }
311 else if (targetm.addr_space.subset_p (as2, as1))
312 {
313 *common = as1;
314 return true;
315 }
316 else
317 return false;
318}
319
400fbf9f
JW
320/* Return a variant of TYPE which has all the type qualifiers of LIKE
321 as well as those of TYPE. */
322
323static tree
2f6e4e97 324qualify_type (tree type, tree like)
400fbf9f 325{
36c5e70a
BE
326 addr_space_t as_type = TYPE_ADDR_SPACE (type);
327 addr_space_t as_like = TYPE_ADDR_SPACE (like);
328 addr_space_t as_common;
329
330 /* If the two named address spaces are different, determine the common
331 superset address space. If there isn't one, raise an error. */
332 if (!addr_space_superset (as_type, as_like, &as_common))
333 {
334 as_common = as_type;
335 error ("%qT and %qT are in disjoint named address spaces",
336 type, like);
337 }
338
2f6e4e97 339 return c_build_qualified_type (type,
36c5e70a 340 TYPE_QUALS_NO_ADDR_SPACE (type)
267bac10 341 | TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (like)
36c5e70a 342 | ENCODE_QUAL_ADDR_SPACE (as_common));
400fbf9f 343}
52ffd86e
MS
344
345/* Return true iff the given tree T is a variable length array. */
346
347bool
ac7d7749 348c_vla_type_p (const_tree t)
52ffd86e
MS
349{
350 if (TREE_CODE (t) == ARRAY_TYPE
351 && C_TYPE_VARIABLE_SIZE (t))
352 return true;
353 return false;
354}
400fbf9f 355\f
10bc1b1b 356/* Return the composite type of two compatible types.
5305f6d7 357
10bc1b1b
JM
358 We assume that comptypes has already been done and returned
359 nonzero; if that isn't so, this may crash. In particular, we
360 assume that qualifiers match. */
400fbf9f
JW
361
362tree
10bc1b1b 363composite_type (tree t1, tree t2)
400fbf9f 364{
b3694847
SS
365 enum tree_code code1;
366 enum tree_code code2;
4b027d16 367 tree attributes;
400fbf9f
JW
368
369 /* Save time if the two types are the same. */
370
371 if (t1 == t2) return t1;
372
373 /* If one type is nonsense, use the other. */
374 if (t1 == error_mark_node)
375 return t2;
376 if (t2 == error_mark_node)
377 return t1;
378
10bc1b1b
JM
379 code1 = TREE_CODE (t1);
380 code2 = TREE_CODE (t2);
381
d9525bec 382 /* Merge the attributes. */
5fd9b178 383 attributes = targetm.merge_type_attributes (t1, t2);
4b027d16 384
10bc1b1b
JM
385 /* If one is an enumerated type and the other is the compatible
386 integer type, the composite type might be either of the two
387 (DR#013 question 3). For consistency, use the enumerated type as
388 the composite type. */
400fbf9f 389
10bc1b1b
JM
390 if (code1 == ENUMERAL_TYPE && code2 == INTEGER_TYPE)
391 return t1;
392 if (code2 == ENUMERAL_TYPE && code1 == INTEGER_TYPE)
393 return t2;
75326e8c 394
366de0ce 395 gcc_assert (code1 == code2);
b6a10c9f 396
400fbf9f
JW
397 switch (code1)
398 {
400fbf9f 399 case POINTER_TYPE:
10bc1b1b 400 /* For two pointers, do this recursively on the target type. */
400fbf9f 401 {
3932261a
MM
402 tree pointed_to_1 = TREE_TYPE (t1);
403 tree pointed_to_2 = TREE_TYPE (t2);
10bc1b1b 404 tree target = composite_type (pointed_to_1, pointed_to_2);
3db684fb 405 t1 = build_pointer_type_for_mode (target, TYPE_MODE (t1), false);
fe7080d2
AP
406 t1 = build_type_attribute_variant (t1, attributes);
407 return qualify_type (t1, t2);
400fbf9f 408 }
400fbf9f
JW
409
410 case ARRAY_TYPE:
411 {
10bc1b1b 412 tree elt = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
46df2823
JM
413 int quals;
414 tree unqual_elt;
ca8bdb78
JM
415 tree d1 = TYPE_DOMAIN (t1);
416 tree d2 = TYPE_DOMAIN (t2);
417 bool d1_variable, d2_variable;
418 bool d1_zero, d2_zero;
f6294de7 419 bool t1_complete, t2_complete;
46df2823 420
de46b2fe 421 /* We should not have any type quals on arrays at all. */
36c5e70a
BE
422 gcc_assert (!TYPE_QUALS_NO_ADDR_SPACE (t1)
423 && !TYPE_QUALS_NO_ADDR_SPACE (t2));
c22cacf3 424
f6294de7
JM
425 t1_complete = COMPLETE_TYPE_P (t1);
426 t2_complete = COMPLETE_TYPE_P (t2);
427
3fa8871b
MP
428 d1_zero = d1 == NULL_TREE || !TYPE_MAX_VALUE (d1);
429 d2_zero = d2 == NULL_TREE || !TYPE_MAX_VALUE (d2);
ca8bdb78
JM
430
431 d1_variable = (!d1_zero
432 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
433 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
434 d2_variable = (!d2_zero
435 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
436 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
437 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
438 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
ca8bdb78 439
400fbf9f 440 /* Save space: see if the result is identical to one of the args. */
ca8bdb78
JM
441 if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1)
442 && (d2_variable || d2_zero || !d1_variable))
4b027d16 443 return build_type_attribute_variant (t1, attributes);
ca8bdb78
JM
444 if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2)
445 && (d1_variable || d1_zero || !d2_variable))
4b027d16 446 return build_type_attribute_variant (t2, attributes);
c22cacf3 447
de46b2fe
AP
448 if (elt == TREE_TYPE (t1) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
449 return build_type_attribute_variant (t1, attributes);
450 if (elt == TREE_TYPE (t2) && !TYPE_DOMAIN (t2) && !TYPE_DOMAIN (t1))
451 return build_type_attribute_variant (t2, attributes);
c22cacf3 452
46df2823
JM
453 /* Merge the element types, and have a size if either arg has
454 one. We may have qualifiers on the element types. To set
455 up TYPE_MAIN_VARIANT correctly, we need to form the
456 composite of the unqualified types and add the qualifiers
457 back at the end. */
458 quals = TYPE_QUALS (strip_array_types (elt));
459 unqual_elt = c_build_qualified_type (elt, TYPE_UNQUALIFIED);
460 t1 = build_array_type (unqual_elt,
ca8bdb78
JM
461 TYPE_DOMAIN ((TYPE_DOMAIN (t1)
462 && (d2_variable
463 || d2_zero
464 || !d1_variable))
465 ? t1
466 : t2));
f6294de7
JM
467 /* Ensure a composite type involving a zero-length array type
468 is a zero-length type not an incomplete type. */
469 if (d1_zero && d2_zero
470 && (t1_complete || t2_complete)
471 && !COMPLETE_TYPE_P (t1))
472 {
473 TYPE_SIZE (t1) = bitsize_zero_node;
474 TYPE_SIZE_UNIT (t1) = size_zero_node;
475 }
46df2823 476 t1 = c_build_qualified_type (t1, quals);
de46b2fe 477 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
478 }
479
fcb99e7b
JJ
480 case ENUMERAL_TYPE:
481 case RECORD_TYPE:
482 case UNION_TYPE:
483 if (attributes != NULL)
484 {
485 /* Try harder not to create a new aggregate type. */
486 if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
487 return t1;
488 if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
489 return t2;
490 }
491 return build_type_attribute_variant (t1, attributes);
492
400fbf9f
JW
493 case FUNCTION_TYPE:
494 /* Function types: prefer the one that specified arg types.
495 If both do, merge the arg types. Also merge the return types. */
496 {
10bc1b1b 497 tree valtype = composite_type (TREE_TYPE (t1), TREE_TYPE (t2));
400fbf9f
JW
498 tree p1 = TYPE_ARG_TYPES (t1);
499 tree p2 = TYPE_ARG_TYPES (t2);
500 int len;
501 tree newargs, n;
502 int i;
503
504 /* Save space: see if the result is identical to one of the args. */
3f75a254 505 if (valtype == TREE_TYPE (t1) && !TYPE_ARG_TYPES (t2))
4b027d16 506 return build_type_attribute_variant (t1, attributes);
3f75a254 507 if (valtype == TREE_TYPE (t2) && !TYPE_ARG_TYPES (t1))
4b027d16 508 return build_type_attribute_variant (t2, attributes);
400fbf9f
JW
509
510 /* Simple way if one arg fails to specify argument types. */
3fa8871b 511 if (TYPE_ARG_TYPES (t1) == NULL_TREE)
4b027d16 512 {
fe7080d2
AP
513 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t2));
514 t1 = build_type_attribute_variant (t1, attributes);
515 return qualify_type (t1, t2);
4b027d16 516 }
3fa8871b 517 if (TYPE_ARG_TYPES (t2) == NULL_TREE)
4b027d16
RK
518 {
519 t1 = build_function_type (valtype, TYPE_ARG_TYPES (t1));
fe7080d2
AP
520 t1 = build_type_attribute_variant (t1, attributes);
521 return qualify_type (t1, t2);
4b027d16 522 }
400fbf9f
JW
523
524 /* If both args specify argument types, we must merge the two
525 lists, argument by argument. */
2f4e8f2b 526
fcc2b74f
JJ
527 for (len = 0, newargs = p1;
528 newargs && newargs != void_list_node;
529 len++, newargs = TREE_CHAIN (newargs))
530 ;
400fbf9f
JW
531
532 for (i = 0; i < len; i++)
8d9bfdc5 533 newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
400fbf9f
JW
534
535 n = newargs;
536
fcc2b74f 537 for (; p1 && p1 != void_list_node;
400fbf9f
JW
538 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n))
539 {
540 /* A null type means arg type is not specified.
541 Take whatever the other function type has. */
3fa8871b 542 if (TREE_VALUE (p1) == NULL_TREE)
400fbf9f
JW
543 {
544 TREE_VALUE (n) = TREE_VALUE (p2);
545 goto parm_done;
546 }
3fa8871b 547 if (TREE_VALUE (p2) == NULL_TREE)
400fbf9f
JW
548 {
549 TREE_VALUE (n) = TREE_VALUE (p1);
550 goto parm_done;
551 }
2f6e4e97 552
400fbf9f
JW
553 /* Given wait (union {union wait *u; int *i} *)
554 and wait (union wait *),
555 prefer union wait * as type of parm. */
556 if (TREE_CODE (TREE_VALUE (p1)) == UNION_TYPE
557 && TREE_VALUE (p1) != TREE_VALUE (p2))
558 {
559 tree memb;
58cb41e6
JJ
560 tree mv2 = TREE_VALUE (p2);
561 if (mv2 && mv2 != error_mark_node
562 && TREE_CODE (mv2) != ARRAY_TYPE)
563 mv2 = TYPE_MAIN_VARIANT (mv2);
400fbf9f 564 for (memb = TYPE_FIELDS (TREE_VALUE (p1));
910ad8de 565 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
566 {
567 tree mv3 = TREE_TYPE (memb);
568 if (mv3 && mv3 != error_mark_node
569 && TREE_CODE (mv3) != ARRAY_TYPE)
570 mv3 = TYPE_MAIN_VARIANT (mv3);
571 if (comptypes (mv3, mv2))
572 {
573 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
574 TREE_VALUE (p2));
c1771a20 575 pedwarn (input_location, OPT_Wpedantic,
fcf73884 576 "function types not truly compatible in ISO C");
58cb41e6
JJ
577 goto parm_done;
578 }
579 }
400fbf9f
JW
580 }
581 if (TREE_CODE (TREE_VALUE (p2)) == UNION_TYPE
582 && TREE_VALUE (p2) != TREE_VALUE (p1))
583 {
584 tree memb;
58cb41e6
JJ
585 tree mv1 = TREE_VALUE (p1);
586 if (mv1 && mv1 != error_mark_node
587 && TREE_CODE (mv1) != ARRAY_TYPE)
588 mv1 = TYPE_MAIN_VARIANT (mv1);
400fbf9f 589 for (memb = TYPE_FIELDS (TREE_VALUE (p2));
910ad8de 590 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
591 {
592 tree mv3 = TREE_TYPE (memb);
593 if (mv3 && mv3 != error_mark_node
594 && TREE_CODE (mv3) != ARRAY_TYPE)
595 mv3 = TYPE_MAIN_VARIANT (mv3);
596 if (comptypes (mv3, mv1))
597 {
598 TREE_VALUE (n) = composite_type (TREE_TYPE (memb),
599 TREE_VALUE (p1));
c1771a20 600 pedwarn (input_location, OPT_Wpedantic,
fcf73884 601 "function types not truly compatible in ISO C");
58cb41e6
JJ
602 goto parm_done;
603 }
604 }
400fbf9f 605 }
10bc1b1b 606 TREE_VALUE (n) = composite_type (TREE_VALUE (p1), TREE_VALUE (p2));
400fbf9f
JW
607 parm_done: ;
608 }
609
4b027d16 610 t1 = build_function_type (valtype, newargs);
fe7080d2 611 t1 = qualify_type (t1, t2);
400fbf9f 612 }
295844f6 613 /* FALLTHRU */
400fbf9f
JW
614
615 default:
4b027d16 616 return build_type_attribute_variant (t1, attributes);
400fbf9f
JW
617 }
618
619}
10bc1b1b
JM
620
621/* Return the type of a conditional expression between pointers to
622 possibly differently qualified versions of compatible types.
623
624 We assume that comp_target_types has already been done and returned
625 nonzero; if that isn't so, this may crash. */
626
627static tree
628common_pointer_type (tree t1, tree t2)
629{
630 tree attributes;
46df2823
JM
631 tree pointed_to_1, mv1;
632 tree pointed_to_2, mv2;
10bc1b1b 633 tree target;
eb1387a0 634 unsigned target_quals;
36c5e70a
BE
635 addr_space_t as1, as2, as_common;
636 int quals1, quals2;
10bc1b1b
JM
637
638 /* Save time if the two types are the same. */
639
640 if (t1 == t2) return t1;
641
642 /* If one type is nonsense, use the other. */
643 if (t1 == error_mark_node)
644 return t2;
645 if (t2 == error_mark_node)
646 return t1;
647
366de0ce 648 gcc_assert (TREE_CODE (t1) == POINTER_TYPE
c22cacf3 649 && TREE_CODE (t2) == POINTER_TYPE);
10bc1b1b
JM
650
651 /* Merge the attributes. */
652 attributes = targetm.merge_type_attributes (t1, t2);
653
654 /* Find the composite type of the target types, and combine the
46df2823
JM
655 qualifiers of the two types' targets. Do not lose qualifiers on
656 array element types by taking the TYPE_MAIN_VARIANT. */
657 mv1 = pointed_to_1 = TREE_TYPE (t1);
658 mv2 = pointed_to_2 = TREE_TYPE (t2);
659 if (TREE_CODE (mv1) != ARRAY_TYPE)
660 mv1 = TYPE_MAIN_VARIANT (pointed_to_1);
661 if (TREE_CODE (mv2) != ARRAY_TYPE)
662 mv2 = TYPE_MAIN_VARIANT (pointed_to_2);
663 target = composite_type (mv1, mv2);
eb1387a0 664
768952be
MU
665 /* Strip array types to get correct qualifier for pointers to arrays */
666 quals1 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_1));
667 quals2 = TYPE_QUALS_NO_ADDR_SPACE (strip_array_types (pointed_to_2));
668
eb1387a0
RG
669 /* For function types do not merge const qualifiers, but drop them
670 if used inconsistently. The middle-end uses these to mark const
671 and noreturn functions. */
672 if (TREE_CODE (pointed_to_1) == FUNCTION_TYPE)
36c5e70a 673 target_quals = (quals1 & quals2);
eb1387a0 674 else
36c5e70a
BE
675 target_quals = (quals1 | quals2);
676
677 /* If the two named address spaces are different, determine the common
678 superset address space. This is guaranteed to exist due to the
679 assumption that comp_target_type returned non-zero. */
680 as1 = TYPE_ADDR_SPACE (pointed_to_1);
681 as2 = TYPE_ADDR_SPACE (pointed_to_2);
682 if (!addr_space_superset (as1, as2, &as_common))
683 gcc_unreachable ();
684
685 target_quals |= ENCODE_QUAL_ADDR_SPACE (as_common);
686
eb1387a0 687 t1 = build_pointer_type (c_build_qualified_type (target, target_quals));
10bc1b1b
JM
688 return build_type_attribute_variant (t1, attributes);
689}
690
691/* Return the common type for two arithmetic types under the usual
692 arithmetic conversions. The default conversions have already been
693 applied, and enumerated types converted to their compatible integer
694 types. The resulting type is unqualified and has no attributes.
695
696 This is the type for the result of most arithmetic operations
697 if the operands have the given two types. */
698
ccf7f880
JJ
699static tree
700c_common_type (tree t1, tree t2)
10bc1b1b
JM
701{
702 enum tree_code code1;
703 enum tree_code code2;
704
705 /* If one type is nonsense, use the other. */
706 if (t1 == error_mark_node)
707 return t2;
708 if (t2 == error_mark_node)
709 return t1;
710
711 if (TYPE_QUALS (t1) != TYPE_UNQUALIFIED)
712 t1 = TYPE_MAIN_VARIANT (t1);
713
714 if (TYPE_QUALS (t2) != TYPE_UNQUALIFIED)
715 t2 = TYPE_MAIN_VARIANT (t2);
716
717 if (TYPE_ATTRIBUTES (t1) != NULL_TREE)
718 t1 = build_type_attribute_variant (t1, NULL_TREE);
719
720 if (TYPE_ATTRIBUTES (t2) != NULL_TREE)
721 t2 = build_type_attribute_variant (t2, NULL_TREE);
722
723 /* Save time if the two types are the same. */
724
725 if (t1 == t2) return t1;
726
727 code1 = TREE_CODE (t1);
728 code2 = TREE_CODE (t2);
729
366de0ce 730 gcc_assert (code1 == VECTOR_TYPE || code1 == COMPLEX_TYPE
ab22c1fa
CF
731 || code1 == FIXED_POINT_TYPE || code1 == REAL_TYPE
732 || code1 == INTEGER_TYPE);
366de0ce 733 gcc_assert (code2 == VECTOR_TYPE || code2 == COMPLEX_TYPE
ab22c1fa
CF
734 || code2 == FIXED_POINT_TYPE || code2 == REAL_TYPE
735 || code2 == INTEGER_TYPE);
10bc1b1b 736
5fc89bfd
JJ
737 /* When one operand is a decimal float type, the other operand cannot be
738 a generic float type or a complex type. We also disallow vector types
739 here. */
740 if ((DECIMAL_FLOAT_TYPE_P (t1) || DECIMAL_FLOAT_TYPE_P (t2))
741 && !(DECIMAL_FLOAT_TYPE_P (t1) && DECIMAL_FLOAT_TYPE_P (t2)))
742 {
743 if (code1 == VECTOR_TYPE || code2 == VECTOR_TYPE)
744 {
745 error ("can%'t mix operands of decimal float and vector types");
746 return error_mark_node;
747 }
748 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
749 {
750 error ("can%'t mix operands of decimal float and complex types");
751 return error_mark_node;
752 }
753 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
754 {
755 error ("can%'t mix operands of decimal float and other float types");
756 return error_mark_node;
757 }
758 }
759
10bc1b1b
JM
760 /* If one type is a vector type, return that type. (How the usual
761 arithmetic conversions apply to the vector types extension is not
762 precisely specified.) */
763 if (code1 == VECTOR_TYPE)
764 return t1;
765
766 if (code2 == VECTOR_TYPE)
767 return t2;
768
769 /* If one type is complex, form the common type of the non-complex
770 components, then make that complex. Use T1 or T2 if it is the
771 required type. */
772 if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
773 {
774 tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
775 tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
ccf7f880 776 tree subtype = c_common_type (subtype1, subtype2);
10bc1b1b
JM
777
778 if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
779 return t1;
780 else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
781 return t2;
782 else
783 return build_complex_type (subtype);
784 }
785
786 /* If only one is real, use it as the result. */
787
788 if (code1 == REAL_TYPE && code2 != REAL_TYPE)
789 return t1;
790
791 if (code2 == REAL_TYPE && code1 != REAL_TYPE)
792 return t2;
793
9a8ce21f
JG
794 /* If both are real and either are decimal floating point types, use
795 the decimal floating point type with the greater precision. */
796
797 if (code1 == REAL_TYPE && code2 == REAL_TYPE)
798 {
799 if (TYPE_MAIN_VARIANT (t1) == dfloat128_type_node
800 || TYPE_MAIN_VARIANT (t2) == dfloat128_type_node)
801 return dfloat128_type_node;
802 else if (TYPE_MAIN_VARIANT (t1) == dfloat64_type_node
803 || TYPE_MAIN_VARIANT (t2) == dfloat64_type_node)
804 return dfloat64_type_node;
805 else if (TYPE_MAIN_VARIANT (t1) == dfloat32_type_node
806 || TYPE_MAIN_VARIANT (t2) == dfloat32_type_node)
807 return dfloat32_type_node;
808 }
809
ab22c1fa
CF
810 /* Deal with fixed-point types. */
811 if (code1 == FIXED_POINT_TYPE || code2 == FIXED_POINT_TYPE)
812 {
813 unsigned int unsignedp = 0, satp = 0;
b397965c 814 scalar_mode m1, m2;
ab22c1fa
CF
815 unsigned int fbit1, ibit1, fbit2, ibit2, max_fbit, max_ibit;
816
b397965c
RS
817 m1 = SCALAR_TYPE_MODE (t1);
818 m2 = SCALAR_TYPE_MODE (t2);
ab22c1fa
CF
819
820 /* If one input type is saturating, the result type is saturating. */
821 if (TYPE_SATURATING (t1) || TYPE_SATURATING (t2))
822 satp = 1;
823
824 /* If both fixed-point types are unsigned, the result type is unsigned.
825 When mixing fixed-point and integer types, follow the sign of the
826 fixed-point type.
827 Otherwise, the result type is signed. */
828 if ((TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2)
829 && code1 == FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE)
830 || (code1 == FIXED_POINT_TYPE && code2 != FIXED_POINT_TYPE
831 && TYPE_UNSIGNED (t1))
832 || (code1 != FIXED_POINT_TYPE && code2 == FIXED_POINT_TYPE
833 && TYPE_UNSIGNED (t2)))
834 unsignedp = 1;
835
836 /* The result type is signed. */
837 if (unsignedp == 0)
838 {
839 /* If the input type is unsigned, we need to convert to the
840 signed type. */
841 if (code1 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t1))
842 {
d75d71e0 843 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
844 if (GET_MODE_CLASS (m1) == MODE_UFRACT)
845 mclass = MODE_FRACT;
846 else if (GET_MODE_CLASS (m1) == MODE_UACCUM)
847 mclass = MODE_ACCUM;
848 else
849 gcc_unreachable ();
b397965c
RS
850 m1 = as_a <scalar_mode>
851 (mode_for_size (GET_MODE_PRECISION (m1), mclass, 0));
ab22c1fa
CF
852 }
853 if (code2 == FIXED_POINT_TYPE && TYPE_UNSIGNED (t2))
854 {
d75d71e0 855 enum mode_class mclass = (enum mode_class) 0;
ab22c1fa
CF
856 if (GET_MODE_CLASS (m2) == MODE_UFRACT)
857 mclass = MODE_FRACT;
858 else if (GET_MODE_CLASS (m2) == MODE_UACCUM)
859 mclass = MODE_ACCUM;
860 else
861 gcc_unreachable ();
b397965c
RS
862 m2 = as_a <scalar_mode>
863 (mode_for_size (GET_MODE_PRECISION (m2), mclass, 0));
ab22c1fa
CF
864 }
865 }
866
867 if (code1 == FIXED_POINT_TYPE)
868 {
869 fbit1 = GET_MODE_FBIT (m1);
870 ibit1 = GET_MODE_IBIT (m1);
871 }
872 else
873 {
874 fbit1 = 0;
875 /* Signed integers need to subtract one sign bit. */
876 ibit1 = TYPE_PRECISION (t1) - (!TYPE_UNSIGNED (t1));
877 }
878
879 if (code2 == FIXED_POINT_TYPE)
880 {
881 fbit2 = GET_MODE_FBIT (m2);
882 ibit2 = GET_MODE_IBIT (m2);
883 }
884 else
885 {
886 fbit2 = 0;
887 /* Signed integers need to subtract one sign bit. */
888 ibit2 = TYPE_PRECISION (t2) - (!TYPE_UNSIGNED (t2));
889 }
890
891 max_ibit = ibit1 >= ibit2 ? ibit1 : ibit2;
892 max_fbit = fbit1 >= fbit2 ? fbit1 : fbit2;
893 return c_common_fixed_point_type_for_size (max_ibit, max_fbit, unsignedp,
894 satp);
895 }
896
10bc1b1b
JM
897 /* Both real or both integers; use the one with greater precision. */
898
899 if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
900 return t1;
901 else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
902 return t2;
903
904 /* Same precision. Prefer long longs to longs to ints when the
905 same precision, following the C99 rules on integer type rank
906 (which are equivalent to the C90 rules for C90 types). */
907
908 if (TYPE_MAIN_VARIANT (t1) == long_long_unsigned_type_node
909 || TYPE_MAIN_VARIANT (t2) == long_long_unsigned_type_node)
910 return long_long_unsigned_type_node;
911
912 if (TYPE_MAIN_VARIANT (t1) == long_long_integer_type_node
913 || TYPE_MAIN_VARIANT (t2) == long_long_integer_type_node)
914 {
915 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
916 return long_long_unsigned_type_node;
917 else
c22cacf3 918 return long_long_integer_type_node;
10bc1b1b
JM
919 }
920
921 if (TYPE_MAIN_VARIANT (t1) == long_unsigned_type_node
922 || TYPE_MAIN_VARIANT (t2) == long_unsigned_type_node)
923 return long_unsigned_type_node;
924
925 if (TYPE_MAIN_VARIANT (t1) == long_integer_type_node
926 || TYPE_MAIN_VARIANT (t2) == long_integer_type_node)
927 {
928 /* But preserve unsignedness from the other type,
929 since long cannot hold all the values of an unsigned int. */
930 if (TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
931 return long_unsigned_type_node;
932 else
933 return long_integer_type_node;
934 }
935
c65699ef
JM
936 /* For floating types of the same TYPE_PRECISION (which we here
937 assume means either the same set of values, or sets of values
938 neither a subset of the other, with behavior being undefined in
939 the latter case), follow the rules from TS 18661-3: prefer
940 interchange types _FloatN, then standard types long double,
941 double, float, then extended types _FloatNx. For extended types,
942 check them starting with _Float128x as that seems most consistent
943 in spirit with preferring long double to double; for interchange
944 types, also check in that order for consistency although it's not
945 possible for more than one of them to have the same
946 precision. */
947 tree mv1 = TYPE_MAIN_VARIANT (t1);
948 tree mv2 = TYPE_MAIN_VARIANT (t2);
949
950 for (int i = NUM_FLOATN_TYPES - 1; i >= 0; i--)
951 if (mv1 == FLOATN_TYPE_NODE (i) || mv2 == FLOATN_TYPE_NODE (i))
952 return FLOATN_TYPE_NODE (i);
953
10bc1b1b 954 /* Likewise, prefer long double to double even if same size. */
c65699ef 955 if (mv1 == long_double_type_node || mv2 == long_double_type_node)
10bc1b1b
JM
956 return long_double_type_node;
957
2531a1d9
JR
958 /* Likewise, prefer double to float even if same size.
959 We got a couple of embedded targets with 32 bit doubles, and the
960 pdp11 might have 64 bit floats. */
c65699ef 961 if (mv1 == double_type_node || mv2 == double_type_node)
2531a1d9
JR
962 return double_type_node;
963
c65699ef
JM
964 if (mv1 == float_type_node || mv2 == float_type_node)
965 return float_type_node;
966
967 for (int i = NUM_FLOATNX_TYPES - 1; i >= 0; i--)
968 if (mv1 == FLOATNX_TYPE_NODE (i) || mv2 == FLOATNX_TYPE_NODE (i))
969 return FLOATNX_TYPE_NODE (i);
970
10bc1b1b
JM
971 /* Otherwise prefer the unsigned one. */
972
973 if (TYPE_UNSIGNED (t1))
974 return t1;
975 else
976 return t2;
977}
400fbf9f 978\f
5922c215
JM
979/* Wrapper around c_common_type that is used by c-common.c and other
980 front end optimizations that remove promotions. ENUMERAL_TYPEs
b2232745
RS
981 are allowed here and are converted to their compatible integer types.
982 BOOLEAN_TYPEs are allowed here and return either boolean_type_node or
983 preferably a non-Boolean type as the common type. */
ccf7f880
JJ
984tree
985common_type (tree t1, tree t2)
986{
987 if (TREE_CODE (t1) == ENUMERAL_TYPE)
988 t1 = c_common_type_for_size (TYPE_PRECISION (t1), 1);
989 if (TREE_CODE (t2) == ENUMERAL_TYPE)
990 t2 = c_common_type_for_size (TYPE_PRECISION (t2), 1);
b2232745
RS
991
992 /* If both types are BOOLEAN_TYPE, then return boolean_type_node. */
993 if (TREE_CODE (t1) == BOOLEAN_TYPE
994 && TREE_CODE (t2) == BOOLEAN_TYPE)
995 return boolean_type_node;
996
997 /* If either type is BOOLEAN_TYPE, then return the other. */
998 if (TREE_CODE (t1) == BOOLEAN_TYPE)
999 return t2;
1000 if (TREE_CODE (t2) == BOOLEAN_TYPE)
1001 return t1;
1002
ccf7f880
JJ
1003 return c_common_type (t1, t2);
1004}
f13c9b2c 1005
400fbf9f
JW
1006/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1007 or various other operations. Return 2 if they are compatible
1008 but a warning may be needed if you use them together. */
1009
1010int
132da1a5 1011comptypes (tree type1, tree type2)
f13c9b2c
AP
1012{
1013 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1014 int val;
1015
dc5027f4 1016 val = comptypes_internal (type1, type2, NULL, NULL);
744aa42f
ILT
1017 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1018
1019 return val;
1020}
1021
1022/* Like comptypes, but if it returns non-zero because enum and int are
1023 compatible, it sets *ENUM_AND_INT_P to true. */
1024
1025static int
1026comptypes_check_enum_int (tree type1, tree type2, bool *enum_and_int_p)
1027{
1028 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1029 int val;
1030
dc5027f4
JM
1031 val = comptypes_internal (type1, type2, enum_and_int_p, NULL);
1032 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
1033
1034 return val;
1035}
1036
1037/* Like comptypes, but if it returns nonzero for different types, it
1038 sets *DIFFERENT_TYPES_P to true. */
1039
1040int
1041comptypes_check_different_types (tree type1, tree type2,
1042 bool *different_types_p)
1043{
1044 const struct tagged_tu_seen_cache * tagged_tu_seen_base1 = tagged_tu_seen_base;
1045 int val;
1046
1047 val = comptypes_internal (type1, type2, NULL, different_types_p);
f13c9b2c 1048 free_all_tagged_tu_seen_up_to (tagged_tu_seen_base1);
c22cacf3 1049
f13c9b2c 1050 return val;
c22cacf3
MS
1051}
1052\f
f13c9b2c
AP
1053/* Return 1 if TYPE1 and TYPE2 are compatible types for assignment
1054 or various other operations. Return 2 if they are compatible
744aa42f
ILT
1055 but a warning may be needed if you use them together. If
1056 ENUM_AND_INT_P is not NULL, and one type is an enum and the other a
1057 compatible integer type, then this sets *ENUM_AND_INT_P to true;
dc5027f4
JM
1058 *ENUM_AND_INT_P is never set to false. If DIFFERENT_TYPES_P is not
1059 NULL, and the types are compatible but different enough not to be
48b0b196 1060 permitted in C11 typedef redeclarations, then this sets
dc5027f4
JM
1061 *DIFFERENT_TYPES_P to true; *DIFFERENT_TYPES_P is never set to
1062 false, but may or may not be set if the types are incompatible.
1063 This differs from comptypes, in that we don't free the seen
1064 types. */
f13c9b2c
AP
1065
1066static int
dc5027f4
JM
1067comptypes_internal (const_tree type1, const_tree type2, bool *enum_and_int_p,
1068 bool *different_types_p)
400fbf9f 1069{
58f9752a
KG
1070 const_tree t1 = type1;
1071 const_tree t2 = type2;
4b027d16 1072 int attrval, val;
400fbf9f
JW
1073
1074 /* Suppress errors caused by previously reported errors. */
1075
8d47dfc5
RH
1076 if (t1 == t2 || !t1 || !t2
1077 || TREE_CODE (t1) == ERROR_MARK || TREE_CODE (t2) == ERROR_MARK)
400fbf9f
JW
1078 return 1;
1079
bca63328
JM
1080 /* Enumerated types are compatible with integer types, but this is
1081 not transitive: two enumerated types in the same translation unit
1082 are compatible with each other only if they are the same type. */
400fbf9f 1083
bca63328 1084 if (TREE_CODE (t1) == ENUMERAL_TYPE && TREE_CODE (t2) != ENUMERAL_TYPE)
744aa42f
ILT
1085 {
1086 t1 = c_common_type_for_size (TYPE_PRECISION (t1), TYPE_UNSIGNED (t1));
dc5027f4
JM
1087 if (TREE_CODE (t2) != VOID_TYPE)
1088 {
1089 if (enum_and_int_p != NULL)
1090 *enum_and_int_p = true;
1091 if (different_types_p != NULL)
1092 *different_types_p = true;
1093 }
744aa42f 1094 }
bca63328 1095 else if (TREE_CODE (t2) == ENUMERAL_TYPE && TREE_CODE (t1) != ENUMERAL_TYPE)
744aa42f
ILT
1096 {
1097 t2 = c_common_type_for_size (TYPE_PRECISION (t2), TYPE_UNSIGNED (t2));
dc5027f4
JM
1098 if (TREE_CODE (t1) != VOID_TYPE)
1099 {
1100 if (enum_and_int_p != NULL)
1101 *enum_and_int_p = true;
1102 if (different_types_p != NULL)
1103 *different_types_p = true;
1104 }
744aa42f 1105 }
400fbf9f
JW
1106
1107 if (t1 == t2)
1108 return 1;
1109
1110 /* Different classes of types can't be compatible. */
1111
3aeb3655
EC
1112 if (TREE_CODE (t1) != TREE_CODE (t2))
1113 return 0;
400fbf9f 1114
118a3a8b 1115 /* Qualifiers must match. C99 6.7.3p9 */
400fbf9f 1116
3932261a 1117 if (TYPE_QUALS (t1) != TYPE_QUALS (t2))
400fbf9f
JW
1118 return 0;
1119
08632da2
RS
1120 /* Allow for two different type nodes which have essentially the same
1121 definition. Note that we already checked for equality of the type
38e01259 1122 qualifiers (just above). */
400fbf9f 1123
46df2823
JM
1124 if (TREE_CODE (t1) != ARRAY_TYPE
1125 && TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
400fbf9f
JW
1126 return 1;
1127
4b027d16 1128 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
ac9a30ae 1129 if (!(attrval = comp_type_attributes (t1, t2)))
4b027d16
RK
1130 return 0;
1131
1132 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1133 val = 0;
1134
400fbf9f
JW
1135 switch (TREE_CODE (t1))
1136 {
5545a907
MP
1137 case INTEGER_TYPE:
1138 case FIXED_POINT_TYPE:
1139 case REAL_TYPE:
1140 /* With these nodes, we can't determine type equivalence by
1141 looking at what is stored in the nodes themselves, because
1142 two nodes might have different TYPE_MAIN_VARIANTs but still
1143 represent the same type. For example, wchar_t and int could
1144 have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
1145 TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
1146 and are distinct types. On the other hand, int and the
1147 following typedef
1148
1149 typedef int INT __attribute((may_alias));
1150
1151 have identical properties, different TYPE_MAIN_VARIANTs, but
1152 represent the same type. The canonical type system keeps
1153 track of equivalence in this case, so we fall back on it. */
1154 return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
1155
400fbf9f 1156 case POINTER_TYPE:
5545a907
MP
1157 /* Do not remove mode information. */
1158 if (TYPE_MODE (t1) != TYPE_MODE (t2))
106f5de5 1159 break;
4b027d16 1160 val = (TREE_TYPE (t1) == TREE_TYPE (t2)
744aa42f 1161 ? 1 : comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1162 enum_and_int_p, different_types_p));
4b027d16 1163 break;
400fbf9f
JW
1164
1165 case FUNCTION_TYPE:
dc5027f4
JM
1166 val = function_types_compatible_p (t1, t2, enum_and_int_p,
1167 different_types_p);
4b027d16 1168 break;
400fbf9f
JW
1169
1170 case ARRAY_TYPE:
1171 {
400fbf9f
JW
1172 tree d1 = TYPE_DOMAIN (t1);
1173 tree d2 = TYPE_DOMAIN (t2);
3f85558f
RH
1174 bool d1_variable, d2_variable;
1175 bool d1_zero, d2_zero;
4b027d16 1176 val = 1;
400fbf9f
JW
1177
1178 /* Target types must match incl. qualifiers. */
1179 if (TREE_TYPE (t1) != TREE_TYPE (t2)
01512446
JJ
1180 && (val = comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
1181 enum_and_int_p,
1182 different_types_p)) == 0)
400fbf9f
JW
1183 return 0;
1184
dc5027f4 1185 if (different_types_p != NULL
3fa8871b 1186 && (d1 == NULL_TREE) != (d2 == NULL_TREE))
dc5027f4 1187 *different_types_p = true;
400fbf9f 1188 /* Sizes must match unless one is missing or variable. */
3fa8871b 1189 if (d1 == NULL_TREE || d2 == NULL_TREE || d1 == d2)
4b027d16 1190 break;
400fbf9f 1191
3f75a254
JM
1192 d1_zero = !TYPE_MAX_VALUE (d1);
1193 d2_zero = !TYPE_MAX_VALUE (d2);
3f85558f 1194
3f75a254 1195 d1_variable = (!d1_zero
3f85558f
RH
1196 && (TREE_CODE (TYPE_MIN_VALUE (d1)) != INTEGER_CST
1197 || TREE_CODE (TYPE_MAX_VALUE (d1)) != INTEGER_CST));
3f75a254 1198 d2_variable = (!d2_zero
3f85558f
RH
1199 && (TREE_CODE (TYPE_MIN_VALUE (d2)) != INTEGER_CST
1200 || TREE_CODE (TYPE_MAX_VALUE (d2)) != INTEGER_CST));
52ffd86e
MS
1201 d1_variable = d1_variable || (d1_zero && c_vla_type_p (t1));
1202 d2_variable = d2_variable || (d2_zero && c_vla_type_p (t2));
3f85558f 1203
dc5027f4
JM
1204 if (different_types_p != NULL
1205 && d1_variable != d2_variable)
1206 *different_types_p = true;
3f85558f
RH
1207 if (d1_variable || d2_variable)
1208 break;
1209 if (d1_zero && d2_zero)
1210 break;
1211 if (d1_zero || d2_zero
3f75a254
JM
1212 || !tree_int_cst_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2))
1213 || !tree_int_cst_equal (TYPE_MAX_VALUE (d1), TYPE_MAX_VALUE (d2)))
05bccae2
RK
1214 val = 0;
1215
c22cacf3 1216 break;
400fbf9f
JW
1217 }
1218
d1bd0ded 1219 case ENUMERAL_TYPE:
58393038 1220 case RECORD_TYPE:
d1bd0ded 1221 case UNION_TYPE:
766beae1 1222 if (val != 1 && !same_translation_unit_p (t1, t2))
c22cacf3 1223 {
fcb99e7b
JJ
1224 tree a1 = TYPE_ATTRIBUTES (t1);
1225 tree a2 = TYPE_ATTRIBUTES (t2);
1226
1227 if (! attribute_list_contained (a1, a2)
1228 && ! attribute_list_contained (a2, a1))
1229 break;
1230
f13c9b2c 1231 if (attrval != 2)
dc5027f4
JM
1232 return tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1233 different_types_p);
1234 val = tagged_types_tu_compatible_p (t1, t2, enum_and_int_p,
1235 different_types_p);
f13c9b2c 1236 }
4b027d16 1237 break;
e9a25f70 1238
62e1dfcf 1239 case VECTOR_TYPE:
928686b1 1240 val = (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
744aa42f 1241 && comptypes_internal (TREE_TYPE (t1), TREE_TYPE (t2),
dc5027f4 1242 enum_and_int_p, different_types_p));
62e1dfcf
NC
1243 break;
1244
e9a25f70
JL
1245 default:
1246 break;
400fbf9f 1247 }
4b027d16 1248 return attrval == 2 && val == 1 ? 2 : val;
400fbf9f
JW
1249}
1250
36c5e70a
BE
1251/* Return 1 if TTL and TTR are pointers to types that are equivalent, ignoring
1252 their qualifiers, except for named address spaces. If the pointers point to
1253 different named addresses, then we must determine if one address space is a
1254 subset of the other. */
400fbf9f
JW
1255
1256static int
744aa42f 1257comp_target_types (location_t location, tree ttl, tree ttr)
400fbf9f 1258{
392202b0 1259 int val;
768952be 1260 int val_ped;
36c5e70a
BE
1261 tree mvl = TREE_TYPE (ttl);
1262 tree mvr = TREE_TYPE (ttr);
1263 addr_space_t asl = TYPE_ADDR_SPACE (mvl);
1264 addr_space_t asr = TYPE_ADDR_SPACE (mvr);
1265 addr_space_t as_common;
744aa42f 1266 bool enum_and_int_p;
8b40563c 1267
36c5e70a
BE
1268 /* Fail if pointers point to incompatible address spaces. */
1269 if (!addr_space_superset (asl, asr, &as_common))
1270 return 0;
1271
768952be
MU
1272 /* For pedantic record result of comptypes on arrays before losing
1273 qualifiers on the element type below. */
1274 val_ped = 1;
1275
1276 if (TREE_CODE (mvl) == ARRAY_TYPE
1277 && TREE_CODE (mvr) == ARRAY_TYPE)
1278 val_ped = comptypes (mvl, mvr);
1279
1280 /* Qualifiers on element types of array types that are
1281 pointer targets are lost by taking their TYPE_MAIN_VARIANT. */
1282
1283 mvl = (TYPE_ATOMIC (strip_array_types (mvl))
1284 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl), TYPE_QUAL_ATOMIC)
1285 : TYPE_MAIN_VARIANT (mvl));
1286
1287 mvr = (TYPE_ATOMIC (strip_array_types (mvr))
1288 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr), TYPE_QUAL_ATOMIC)
1289 : TYPE_MAIN_VARIANT (mvr));
1290
744aa42f
ILT
1291 enum_and_int_p = false;
1292 val = comptypes_check_enum_int (mvl, mvr, &enum_and_int_p);
8b40563c 1293
768952be
MU
1294 if (val == 1 && val_ped != 1)
1295 pedwarn (location, OPT_Wpedantic, "pointers to arrays with different qualifiers "
1296 "are incompatible in ISO C");
1297
fcf73884 1298 if (val == 2)
c1771a20 1299 pedwarn (location, OPT_Wpedantic, "types are not quite compatible");
744aa42f
ILT
1300
1301 if (val == 1 && enum_and_int_p && warn_cxx_compat)
1302 warning_at (location, OPT_Wc___compat,
1303 "pointer target types incompatible in C++");
1304
400fbf9f
JW
1305 return val;
1306}
1307\f
1308/* Subroutines of `comptypes'. */
1309
f75fbaf7
ZW
1310/* Determine whether two trees derive from the same translation unit.
1311 If the CONTEXT chain ends in a null, that tree's context is still
1312 being parsed, so if two trees have context chains ending in null,
766beae1 1313 they're in the same translation unit. */
e3455240
MP
1314
1315bool
58f9752a 1316same_translation_unit_p (const_tree t1, const_tree t2)
766beae1
ZW
1317{
1318 while (t1 && TREE_CODE (t1) != TRANSLATION_UNIT_DECL)
1319 switch (TREE_CODE_CLASS (TREE_CODE (t1)))
1320 {
6615c446
JO
1321 case tcc_declaration:
1322 t1 = DECL_CONTEXT (t1); break;
1323 case tcc_type:
1324 t1 = TYPE_CONTEXT (t1); break;
1325 case tcc_exceptional:
1326 t1 = BLOCK_SUPERCONTEXT (t1); break; /* assume block */
366de0ce 1327 default: gcc_unreachable ();
766beae1
ZW
1328 }
1329
1330 while (t2 && TREE_CODE (t2) != TRANSLATION_UNIT_DECL)
1331 switch (TREE_CODE_CLASS (TREE_CODE (t2)))
1332 {
6615c446
JO
1333 case tcc_declaration:
1334 t2 = DECL_CONTEXT (t2); break;
1335 case tcc_type:
1336 t2 = TYPE_CONTEXT (t2); break;
1337 case tcc_exceptional:
1338 t2 = BLOCK_SUPERCONTEXT (t2); break; /* assume block */
366de0ce 1339 default: gcc_unreachable ();
766beae1
ZW
1340 }
1341
1342 return t1 == t2;
1343}
1344
f13c9b2c 1345/* Allocate the seen two types, assuming that they are compatible. */
d1bd0ded 1346
f13c9b2c 1347static struct tagged_tu_seen_cache *
58f9752a 1348alloc_tagged_tu_seen_cache (const_tree t1, const_tree t2)
f13c9b2c 1349{
cceb1885 1350 struct tagged_tu_seen_cache *tu = XNEW (struct tagged_tu_seen_cache);
f13c9b2c
AP
1351 tu->next = tagged_tu_seen_base;
1352 tu->t1 = t1;
1353 tu->t2 = t2;
c22cacf3 1354
f13c9b2c 1355 tagged_tu_seen_base = tu;
c22cacf3 1356
f13c9b2c
AP
1357 /* The C standard says that two structures in different translation
1358 units are compatible with each other only if the types of their
1359 fields are compatible (among other things). We assume that they
1360 are compatible until proven otherwise when building the cache.
1361 An example where this can occur is:
1362 struct a
1363 {
1364 struct a *next;
1365 };
1366 If we are comparing this against a similar struct in another TU,
c83eecad 1367 and did not assume they were compatible, we end up with an infinite
f13c9b2c
AP
1368 loop. */
1369 tu->val = 1;
1370 return tu;
1371}
d1bd0ded 1372
f13c9b2c 1373/* Free the seen types until we get to TU_TIL. */
d1bd0ded 1374
f13c9b2c
AP
1375static void
1376free_all_tagged_tu_seen_up_to (const struct tagged_tu_seen_cache *tu_til)
1377{
1378 const struct tagged_tu_seen_cache *tu = tagged_tu_seen_base;
1379 while (tu != tu_til)
1380 {
741ac903
KG
1381 const struct tagged_tu_seen_cache *const tu1
1382 = (const struct tagged_tu_seen_cache *) tu;
f13c9b2c 1383 tu = tu1->next;
b1d5455a 1384 free (CONST_CAST (struct tagged_tu_seen_cache *, tu1));
f13c9b2c
AP
1385 }
1386 tagged_tu_seen_base = tu_til;
1387}
d1bd0ded
GK
1388
1389/* Return 1 if two 'struct', 'union', or 'enum' types T1 and T2 are
1390 compatible. If the two types are not the same (which has been
1391 checked earlier), this can only happen when multiple translation
1392 units are being compiled. See C99 6.2.7 paragraph 1 for the exact
dc5027f4
JM
1393 rules. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1394 comptypes_internal. */
d1bd0ded
GK
1395
1396static int
744aa42f 1397tagged_types_tu_compatible_p (const_tree t1, const_tree t2,
dc5027f4 1398 bool *enum_and_int_p, bool *different_types_p)
d1bd0ded
GK
1399{
1400 tree s1, s2;
1401 bool needs_warning = false;
3aeb3655 1402
d1bd0ded
GK
1403 /* We have to verify that the tags of the types are the same. This
1404 is harder than it looks because this may be a typedef, so we have
1405 to go look at the original type. It may even be a typedef of a
6de9cd9a
DN
1406 typedef...
1407 In the case of compiler-created builtin structs the TYPE_DECL
1408 may be a dummy, with no DECL_ORIGINAL_TYPE. Don't fault. */
dea984dc
ILT
1409 while (TYPE_NAME (t1)
1410 && TREE_CODE (TYPE_NAME (t1)) == TYPE_DECL
1411 && DECL_ORIGINAL_TYPE (TYPE_NAME (t1)))
d1bd0ded
GK
1412 t1 = DECL_ORIGINAL_TYPE (TYPE_NAME (t1));
1413
dea984dc
ILT
1414 while (TYPE_NAME (t2)
1415 && TREE_CODE (TYPE_NAME (t2)) == TYPE_DECL
1416 && DECL_ORIGINAL_TYPE (TYPE_NAME (t2)))
d1bd0ded
GK
1417 t2 = DECL_ORIGINAL_TYPE (TYPE_NAME (t2));
1418
1419 /* C90 didn't have the requirement that the two tags be the same. */
1420 if (flag_isoc99 && TYPE_NAME (t1) != TYPE_NAME (t2))
1421 return 0;
3aeb3655 1422
d1bd0ded
GK
1423 /* C90 didn't say what happened if one or both of the types were
1424 incomplete; we choose to follow C99 rules here, which is that they
1425 are compatible. */
1426 if (TYPE_SIZE (t1) == NULL
1427 || TYPE_SIZE (t2) == NULL)
1428 return 1;
3aeb3655 1429
d1bd0ded 1430 {
f13c9b2c 1431 const struct tagged_tu_seen_cache * tts_i;
d1bd0ded
GK
1432 for (tts_i = tagged_tu_seen_base; tts_i != NULL; tts_i = tts_i->next)
1433 if (tts_i->t1 == t1 && tts_i->t2 == t2)
f13c9b2c 1434 return tts_i->val;
d1bd0ded 1435 }
3aeb3655 1436
d1bd0ded
GK
1437 switch (TREE_CODE (t1))
1438 {
1439 case ENUMERAL_TYPE:
1440 {
f13c9b2c 1441 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
c22cacf3
MS
1442 /* Speed up the case where the type values are in the same order. */
1443 tree tv1 = TYPE_VALUES (t1);
1444 tree tv2 = TYPE_VALUES (t2);
3aeb3655 1445
c22cacf3 1446 if (tv1 == tv2)
f13c9b2c
AP
1447 {
1448 return 1;
1449 }
3aeb3655 1450
c22cacf3
MS
1451 for (;tv1 && tv2; tv1 = TREE_CHAIN (tv1), tv2 = TREE_CHAIN (tv2))
1452 {
1453 if (TREE_PURPOSE (tv1) != TREE_PURPOSE (tv2))
1454 break;
1455 if (simple_cst_equal (TREE_VALUE (tv1), TREE_VALUE (tv2)) != 1)
f13c9b2c 1456 {
c22cacf3 1457 tu->val = 0;
f13c9b2c
AP
1458 return 0;
1459 }
c22cacf3 1460 }
3aeb3655 1461
c22cacf3 1462 if (tv1 == NULL_TREE && tv2 == NULL_TREE)
f13c9b2c
AP
1463 {
1464 return 1;
1465 }
c22cacf3 1466 if (tv1 == NULL_TREE || tv2 == NULL_TREE)
f13c9b2c
AP
1467 {
1468 tu->val = 0;
1469 return 0;
1470 }
3aeb3655 1471
d1bd0ded 1472 if (list_length (TYPE_VALUES (t1)) != list_length (TYPE_VALUES (t2)))
f13c9b2c
AP
1473 {
1474 tu->val = 0;
1475 return 0;
1476 }
3aeb3655 1477
d1bd0ded
GK
1478 for (s1 = TYPE_VALUES (t1); s1; s1 = TREE_CHAIN (s1))
1479 {
1480 s2 = purpose_member (TREE_PURPOSE (s1), TYPE_VALUES (t2));
1481 if (s2 == NULL
1482 || simple_cst_equal (TREE_VALUE (s1), TREE_VALUE (s2)) != 1)
f13c9b2c
AP
1483 {
1484 tu->val = 0;
1485 return 0;
1486 }
d1bd0ded
GK
1487 }
1488 return 1;
1489 }
1490
1491 case UNION_TYPE:
1492 {
f13c9b2c 1493 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
d1bd0ded 1494 if (list_length (TYPE_FIELDS (t1)) != list_length (TYPE_FIELDS (t2)))
f13c9b2c
AP
1495 {
1496 tu->val = 0;
1497 return 0;
1498 }
c22cacf3 1499
f13c9b2c
AP
1500 /* Speed up the common case where the fields are in the same order. */
1501 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2); s1 && s2;
910ad8de 1502 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
f13c9b2c
AP
1503 {
1504 int result;
c22cacf3 1505
3ae4d3cc 1506 if (DECL_NAME (s1) != DECL_NAME (s2))
f13c9b2c 1507 break;
744aa42f 1508 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1509 enum_and_int_p, different_types_p);
3ae4d3cc
AO
1510
1511 if (result != 1 && !DECL_NAME (s1))
1512 break;
f13c9b2c
AP
1513 if (result == 0)
1514 {
1515 tu->val = 0;
1516 return 0;
1517 }
1518 if (result == 2)
1519 needs_warning = true;
1520
1521 if (TREE_CODE (s1) == FIELD_DECL
1522 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1523 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1524 {
1525 tu->val = 0;
1526 return 0;
1527 }
1528 }
1529 if (!s1 && !s2)
1530 {
1531 tu->val = needs_warning ? 2 : 1;
1532 return tu->val;
1533 }
d1bd0ded 1534
910ad8de 1535 for (s1 = TYPE_FIELDS (t1); s1; s1 = DECL_CHAIN (s1))
d1bd0ded
GK
1536 {
1537 bool ok = false;
3aeb3655 1538
910ad8de 1539 for (s2 = TYPE_FIELDS (t2); s2; s2 = DECL_CHAIN (s2))
3ae4d3cc
AO
1540 if (DECL_NAME (s1) == DECL_NAME (s2))
1541 {
1542 int result;
1543
744aa42f 1544 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4
JM
1545 enum_and_int_p,
1546 different_types_p);
3ae4d3cc
AO
1547
1548 if (result != 1 && !DECL_NAME (s1))
1549 continue;
1550 if (result == 0)
1551 {
1552 tu->val = 0;
1553 return 0;
1554 }
1555 if (result == 2)
1556 needs_warning = true;
1557
1558 if (TREE_CODE (s1) == FIELD_DECL
1559 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1560 DECL_FIELD_BIT_OFFSET (s2)) != 1)
d1bd0ded 1561 break;
3ae4d3cc
AO
1562
1563 ok = true;
1564 break;
1565 }
3f75a254 1566 if (!ok)
f13c9b2c
AP
1567 {
1568 tu->val = 0;
1569 return 0;
1570 }
d1bd0ded 1571 }
f13c9b2c
AP
1572 tu->val = needs_warning ? 2 : 10;
1573 return tu->val;
d1bd0ded
GK
1574 }
1575
1576 case RECORD_TYPE:
1577 {
c22cacf3 1578 struct tagged_tu_seen_cache *tu = alloc_tagged_tu_seen_cache (t1, t2);
3aeb3655
EC
1579
1580 for (s1 = TYPE_FIELDS (t1), s2 = TYPE_FIELDS (t2);
d1bd0ded 1581 s1 && s2;
910ad8de 1582 s1 = DECL_CHAIN (s1), s2 = DECL_CHAIN (s2))
d1bd0ded
GK
1583 {
1584 int result;
1585 if (TREE_CODE (s1) != TREE_CODE (s2)
1586 || DECL_NAME (s1) != DECL_NAME (s2))
1587 break;
744aa42f 1588 result = comptypes_internal (TREE_TYPE (s1), TREE_TYPE (s2),
dc5027f4 1589 enum_and_int_p, different_types_p);
d1bd0ded
GK
1590 if (result == 0)
1591 break;
1592 if (result == 2)
1593 needs_warning = true;
3aeb3655 1594
d1bd0ded
GK
1595 if (TREE_CODE (s1) == FIELD_DECL
1596 && simple_cst_equal (DECL_FIELD_BIT_OFFSET (s1),
1597 DECL_FIELD_BIT_OFFSET (s2)) != 1)
1598 break;
1599 }
d1bd0ded 1600 if (s1 && s2)
f13c9b2c
AP
1601 tu->val = 0;
1602 else
1603 tu->val = needs_warning ? 2 : 1;
1604 return tu->val;
d1bd0ded
GK
1605 }
1606
1607 default:
366de0ce 1608 gcc_unreachable ();
d1bd0ded
GK
1609 }
1610}
1611
400fbf9f
JW
1612/* Return 1 if two function types F1 and F2 are compatible.
1613 If either type specifies no argument types,
1614 the other must specify a fixed number of self-promoting arg types.
2f6e4e97 1615 Otherwise, if one type specifies only the number of arguments,
400fbf9f 1616 the other must specify that number of self-promoting arg types.
744aa42f 1617 Otherwise, the argument types must match.
dc5027f4 1618 ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in comptypes_internal. */
400fbf9f
JW
1619
1620static int
744aa42f 1621function_types_compatible_p (const_tree f1, const_tree f2,
dc5027f4 1622 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1623{
1624 tree args1, args2;
1625 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1626 int val = 1;
1627 int val1;
a6fdc086
GK
1628 tree ret1, ret2;
1629
1630 ret1 = TREE_TYPE (f1);
1631 ret2 = TREE_TYPE (f2);
1632
e508a019
JM
1633 /* 'volatile' qualifiers on a function's return type used to mean
1634 the function is noreturn. */
1635 if (TYPE_VOLATILE (ret1) != TYPE_VOLATILE (ret2))
509c9d60 1636 pedwarn (input_location, 0, "function return types not compatible due to %<volatile%>");
a6fdc086
GK
1637 if (TYPE_VOLATILE (ret1))
1638 ret1 = build_qualified_type (TYPE_MAIN_VARIANT (ret1),
1639 TYPE_QUALS (ret1) & ~TYPE_QUAL_VOLATILE);
1640 if (TYPE_VOLATILE (ret2))
1641 ret2 = build_qualified_type (TYPE_MAIN_VARIANT (ret2),
1642 TYPE_QUALS (ret2) & ~TYPE_QUAL_VOLATILE);
dc5027f4 1643 val = comptypes_internal (ret1, ret2, enum_and_int_p, different_types_p);
a6fdc086 1644 if (val == 0)
400fbf9f
JW
1645 return 0;
1646
1647 args1 = TYPE_ARG_TYPES (f1);
1648 args2 = TYPE_ARG_TYPES (f2);
1649
dc5027f4 1650 if (different_types_p != NULL
3fa8871b 1651 && (args1 == NULL_TREE) != (args2 == NULL_TREE))
dc5027f4
JM
1652 *different_types_p = true;
1653
400fbf9f
JW
1654 /* An unspecified parmlist matches any specified parmlist
1655 whose argument types don't need default promotions. */
1656
3fa8871b 1657 if (args1 == NULL_TREE)
400fbf9f
JW
1658 {
1659 if (!self_promoting_args_p (args2))
1660 return 0;
1661 /* If one of these types comes from a non-prototype fn definition,
1662 compare that with the other type's arglist.
3176a0c2 1663 If they don't match, ask for a warning (but no error). */
400fbf9f 1664 if (TYPE_ACTUAL_ARG_TYPES (f1)
01512446
JJ
1665 && type_lists_compatible_p (args2, TYPE_ACTUAL_ARG_TYPES (f1),
1666 enum_and_int_p, different_types_p) != 1)
400fbf9f
JW
1667 val = 2;
1668 return val;
1669 }
3fa8871b 1670 if (args2 == NULL_TREE)
400fbf9f
JW
1671 {
1672 if (!self_promoting_args_p (args1))
1673 return 0;
1674 if (TYPE_ACTUAL_ARG_TYPES (f2)
01512446
JJ
1675 && type_lists_compatible_p (args1, TYPE_ACTUAL_ARG_TYPES (f2),
1676 enum_and_int_p, different_types_p) != 1)
400fbf9f
JW
1677 val = 2;
1678 return val;
1679 }
1680
1681 /* Both types have argument lists: compare them and propagate results. */
dc5027f4
JM
1682 val1 = type_lists_compatible_p (args1, args2, enum_and_int_p,
1683 different_types_p);
400fbf9f
JW
1684 return val1 != 1 ? val1 : val;
1685}
1686
744aa42f
ILT
1687/* Check two lists of types for compatibility, returning 0 for
1688 incompatible, 1 for compatible, or 2 for compatible with
dc5027f4
JM
1689 warning. ENUM_AND_INT_P and DIFFERENT_TYPES_P are as in
1690 comptypes_internal. */
400fbf9f
JW
1691
1692static int
744aa42f 1693type_lists_compatible_p (const_tree args1, const_tree args2,
dc5027f4 1694 bool *enum_and_int_p, bool *different_types_p)
400fbf9f
JW
1695{
1696 /* 1 if no need for warning yet, 2 if warning cause has been seen. */
1697 int val = 1;
9d5f3e49 1698 int newval = 0;
400fbf9f
JW
1699
1700 while (1)
1701 {
46df2823 1702 tree a1, mv1, a2, mv2;
3fa8871b 1703 if (args1 == NULL_TREE && args2 == NULL_TREE)
400fbf9f
JW
1704 return val;
1705 /* If one list is shorter than the other,
1706 they fail to match. */
3fa8871b 1707 if (args1 == NULL_TREE || args2 == NULL_TREE)
400fbf9f 1708 return 0;
46df2823
JM
1709 mv1 = a1 = TREE_VALUE (args1);
1710 mv2 = a2 = TREE_VALUE (args2);
1711 if (mv1 && mv1 != error_mark_node && TREE_CODE (mv1) != ARRAY_TYPE)
267bac10
JM
1712 mv1 = (TYPE_ATOMIC (mv1)
1713 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv1),
1714 TYPE_QUAL_ATOMIC)
1715 : TYPE_MAIN_VARIANT (mv1));
46df2823 1716 if (mv2 && mv2 != error_mark_node && TREE_CODE (mv2) != ARRAY_TYPE)
267bac10
JM
1717 mv2 = (TYPE_ATOMIC (mv2)
1718 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv2),
1719 TYPE_QUAL_ATOMIC)
1720 : TYPE_MAIN_VARIANT (mv2));
400fbf9f
JW
1721 /* A null pointer instead of a type
1722 means there is supposed to be an argument
1723 but nothing is specified about what type it has.
1724 So match anything that self-promotes. */
dc5027f4 1725 if (different_types_p != NULL
3fa8871b 1726 && (a1 == NULL_TREE) != (a2 == NULL_TREE))
dc5027f4 1727 *different_types_p = true;
3fa8871b 1728 if (a1 == NULL_TREE)
400fbf9f 1729 {
46df2823 1730 if (c_type_promotes_to (a2) != a2)
400fbf9f
JW
1731 return 0;
1732 }
3fa8871b 1733 else if (a2 == NULL_TREE)
400fbf9f 1734 {
46df2823 1735 if (c_type_promotes_to (a1) != a1)
400fbf9f
JW
1736 return 0;
1737 }
8f5b6d29 1738 /* If one of the lists has an error marker, ignore this arg. */
46df2823
JM
1739 else if (TREE_CODE (a1) == ERROR_MARK
1740 || TREE_CODE (a2) == ERROR_MARK)
8f5b6d29 1741 ;
dc5027f4
JM
1742 else if (!(newval = comptypes_internal (mv1, mv2, enum_and_int_p,
1743 different_types_p)))
400fbf9f 1744 {
dc5027f4
JM
1745 if (different_types_p != NULL)
1746 *different_types_p = true;
400fbf9f
JW
1747 /* Allow wait (union {union wait *u; int *i} *)
1748 and wait (union wait *) to be compatible. */
46df2823 1749 if (TREE_CODE (a1) == UNION_TYPE
3fa8871b 1750 && (TYPE_NAME (a1) == NULL_TREE
ebf0bf7f 1751 || TYPE_TRANSPARENT_AGGR (a1))
46df2823
JM
1752 && TREE_CODE (TYPE_SIZE (a1)) == INTEGER_CST
1753 && tree_int_cst_equal (TYPE_SIZE (a1),
1754 TYPE_SIZE (a2)))
400fbf9f
JW
1755 {
1756 tree memb;
46df2823 1757 for (memb = TYPE_FIELDS (a1);
910ad8de 1758 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1759 {
1760 tree mv3 = TREE_TYPE (memb);
1761 if (mv3 && mv3 != error_mark_node
1762 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1763 mv3 = (TYPE_ATOMIC (mv3)
1764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1765 TYPE_QUAL_ATOMIC)
1766 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1767 if (comptypes_internal (mv3, mv2, enum_and_int_p,
1768 different_types_p))
58cb41e6
JJ
1769 break;
1770 }
3fa8871b 1771 if (memb == NULL_TREE)
400fbf9f
JW
1772 return 0;
1773 }
46df2823 1774 else if (TREE_CODE (a2) == UNION_TYPE
3fa8871b 1775 && (TYPE_NAME (a2) == NULL_TREE
ebf0bf7f 1776 || TYPE_TRANSPARENT_AGGR (a2))
46df2823
JM
1777 && TREE_CODE (TYPE_SIZE (a2)) == INTEGER_CST
1778 && tree_int_cst_equal (TYPE_SIZE (a2),
1779 TYPE_SIZE (a1)))
400fbf9f
JW
1780 {
1781 tree memb;
46df2823 1782 for (memb = TYPE_FIELDS (a2);
910ad8de 1783 memb; memb = DECL_CHAIN (memb))
58cb41e6
JJ
1784 {
1785 tree mv3 = TREE_TYPE (memb);
1786 if (mv3 && mv3 != error_mark_node
1787 && TREE_CODE (mv3) != ARRAY_TYPE)
267bac10
JM
1788 mv3 = (TYPE_ATOMIC (mv3)
1789 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mv3),
1790 TYPE_QUAL_ATOMIC)
1791 : TYPE_MAIN_VARIANT (mv3));
dc5027f4
JM
1792 if (comptypes_internal (mv3, mv1, enum_and_int_p,
1793 different_types_p))
58cb41e6
JJ
1794 break;
1795 }
3fa8871b 1796 if (memb == NULL_TREE)
400fbf9f
JW
1797 return 0;
1798 }
1799 else
1800 return 0;
1801 }
1802
1803 /* comptypes said ok, but record if it said to warn. */
1804 if (newval > val)
1805 val = newval;
1806
1807 args1 = TREE_CHAIN (args1);
1808 args2 = TREE_CHAIN (args2);
1809 }
1810}
400fbf9f 1811\f
a0e24419
MP
1812/* Compute the size to increment a pointer by. When a function type or void
1813 type or incomplete type is passed, size_one_node is returned.
1814 This function does not emit any diagnostics; the caller is responsible
1815 for that. */
400fbf9f 1816
4e2fb7de 1817static tree
58f9752a 1818c_size_in_bytes (const_tree type)
400fbf9f
JW
1819{
1820 enum tree_code code = TREE_CODE (type);
1821
a0e24419
MP
1822 if (code == FUNCTION_TYPE || code == VOID_TYPE || code == ERROR_MARK
1823 || !COMPLETE_TYPE_P (type))
fed3cef0
RK
1824 return size_one_node;
1825
400fbf9f 1826 /* Convert in case a char is more than one unit. */
db3927fb
AH
1827 return size_binop_loc (input_location, CEIL_DIV_EXPR, TYPE_SIZE_UNIT (type),
1828 size_int (TYPE_PRECISION (char_type_node)
1829 / BITS_PER_UNIT));
400fbf9f 1830}
400fbf9f 1831\f
400fbf9f
JW
1832/* Return either DECL or its known constant value (if it has one). */
1833
56cb9733 1834tree
b4923738 1835decl_constant_value_1 (tree decl, bool in_init)
400fbf9f 1836{
cc6534d4
JJ
1837 if (/* Note that DECL_INITIAL isn't valid for a PARM_DECL. */
1838 TREE_CODE (decl) != PARM_DECL
3f75a254 1839 && !TREE_THIS_VOLATILE (decl)
83bab8db 1840 && TREE_READONLY (decl)
3fa8871b 1841 && DECL_INITIAL (decl) != NULL_TREE
b4923738 1842 && !error_operand_p (DECL_INITIAL (decl))
400fbf9f
JW
1843 /* This is invalid if initial value is not constant.
1844 If it has either a function call, a memory reference,
1845 or a variable, then re-evaluating it could give different results. */
1846 && TREE_CONSTANT (DECL_INITIAL (decl))
1847 /* Check for cases where this is sub-optimal, even though valid. */
b4923738 1848 && (in_init || TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR))
400fbf9f
JW
1849 return DECL_INITIAL (decl);
1850 return decl;
1851}
1852
cc6534d4
JJ
1853/* Return either DECL or its known constant value (if it has one).
1854 Like the above, but always return decl outside of functions. */
1855
1856tree
1857decl_constant_value (tree decl)
1858{
1859 /* Don't change a variable array bound or initial value to a constant
1860 in a place where a variable is invalid. */
b4923738 1861 return current_function_decl ? decl_constant_value_1 (decl, false) : decl;
cc6534d4
JJ
1862}
1863
f2a71bbc
JM
1864/* Convert the array expression EXP to a pointer. */
1865static tree
c2255bc4 1866array_to_pointer_conversion (location_t loc, tree exp)
207bf485 1867{
f2a71bbc 1868 tree orig_exp = exp;
207bf485 1869 tree type = TREE_TYPE (exp);
f2a71bbc
JM
1870 tree adr;
1871 tree restype = TREE_TYPE (type);
1872 tree ptrtype;
207bf485 1873
f2a71bbc 1874 gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
207bf485 1875
f2a71bbc 1876 STRIP_TYPE_NOPS (exp);
207bf485 1877
487a92fe
JM
1878 if (TREE_NO_WARNING (orig_exp))
1879 TREE_NO_WARNING (exp) = 1;
207bf485 1880
f2a71bbc
JM
1881 ptrtype = build_pointer_type (restype);
1882
22d03525 1883 if (INDIRECT_REF_P (exp))
f2a71bbc
JM
1884 return convert (ptrtype, TREE_OPERAND (exp, 0));
1885
1f37c583
JM
1886 /* In C++ array compound literals are temporary objects unless they are
1887 const or appear in namespace scope, so they are destroyed too soon
1888 to use them for much of anything (c++/53220). */
1889 if (warn_cxx_compat && TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
1890 {
1891 tree decl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
1892 if (!TREE_READONLY (decl) && !TREE_STATIC (decl))
1893 warning_at (DECL_SOURCE_LOCATION (decl), OPT_Wc___compat,
1894 "converting an array compound literal to a pointer "
1895 "is ill-formed in C++");
1896 }
1897
e51fbec3 1898 adr = build_unary_op (loc, ADDR_EXPR, exp, true);
f2a71bbc
JM
1899 return convert (ptrtype, adr);
1900}
207bf485 1901
f2a71bbc
JM
1902/* Convert the function expression EXP to a pointer. */
1903static tree
c2255bc4 1904function_to_pointer_conversion (location_t loc, tree exp)
f2a71bbc
JM
1905{
1906 tree orig_exp = exp;
207bf485 1907
f2a71bbc 1908 gcc_assert (TREE_CODE (TREE_TYPE (exp)) == FUNCTION_TYPE);
207bf485 1909
f2a71bbc 1910 STRIP_TYPE_NOPS (exp);
207bf485 1911
f2a71bbc
JM
1912 if (TREE_NO_WARNING (orig_exp))
1913 TREE_NO_WARNING (exp) = 1;
207bf485 1914
e51fbec3 1915 return build_unary_op (loc, ADDR_EXPR, exp, false);
f2a71bbc 1916}
207bf485 1917
ebfbbdc5
JJ
1918/* Mark EXP as read, not just set, for set but not used -Wunused
1919 warning purposes. */
1920
1921void
1922mark_exp_read (tree exp)
1923{
1924 switch (TREE_CODE (exp))
1925 {
1926 case VAR_DECL:
1927 case PARM_DECL:
1928 DECL_READ_P (exp) = 1;
1929 break;
1930 case ARRAY_REF:
1931 case COMPONENT_REF:
1932 case MODIFY_EXPR:
1933 case REALPART_EXPR:
1934 case IMAGPART_EXPR:
1935 CASE_CONVERT:
1936 case ADDR_EXPR:
557e8c49 1937 case VIEW_CONVERT_EXPR:
ebfbbdc5
JJ
1938 mark_exp_read (TREE_OPERAND (exp, 0));
1939 break;
1940 case COMPOUND_EXPR:
82c3c067 1941 case C_MAYBE_CONST_EXPR:
ebfbbdc5
JJ
1942 mark_exp_read (TREE_OPERAND (exp, 1));
1943 break;
1944 default:
1945 break;
1946 }
1947}
1948
f2a71bbc
JM
1949/* Perform the default conversion of arrays and functions to pointers.
1950 Return the result of converting EXP. For any other expression, just
c2255bc4
AH
1951 return EXP.
1952
1953 LOC is the location of the expression. */
f2a71bbc
JM
1954
1955struct c_expr
c2255bc4 1956default_function_array_conversion (location_t loc, struct c_expr exp)
f2a71bbc
JM
1957{
1958 tree orig_exp = exp.value;
1959 tree type = TREE_TYPE (exp.value);
1960 enum tree_code code = TREE_CODE (type);
1961
1962 switch (code)
1963 {
1964 case ARRAY_TYPE:
1965 {
1966 bool not_lvalue = false;
1967 bool lvalue_array_p;
1968
1969 while ((TREE_CODE (exp.value) == NON_LVALUE_EXPR
1043771b 1970 || CONVERT_EXPR_P (exp.value))
f2a71bbc
JM
1971 && TREE_TYPE (TREE_OPERAND (exp.value, 0)) == type)
1972 {
1973 if (TREE_CODE (exp.value) == NON_LVALUE_EXPR)
1974 not_lvalue = true;
1975 exp.value = TREE_OPERAND (exp.value, 0);
1976 }
1977
1978 if (TREE_NO_WARNING (orig_exp))
1979 TREE_NO_WARNING (exp.value) = 1;
1980
1981 lvalue_array_p = !not_lvalue && lvalue_p (exp.value);
1982 if (!flag_isoc99 && !lvalue_array_p)
1983 {
1984 /* Before C99, non-lvalue arrays do not decay to pointers.
1985 Normally, using such an array would be invalid; but it can
1986 be used correctly inside sizeof or as a statement expression.
1987 Thus, do not give an error here; an error will result later. */
1988 return exp;
1989 }
1990
c2255bc4 1991 exp.value = array_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1992 }
1993 break;
1994 case FUNCTION_TYPE:
c2255bc4 1995 exp.value = function_to_pointer_conversion (loc, exp.value);
f2a71bbc
JM
1996 break;
1997 default:
f2a71bbc 1998 break;
207bf485 1999 }
f2a71bbc 2000
207bf485
JM
2001 return exp;
2002}
2003
ebfbbdc5
JJ
2004struct c_expr
2005default_function_array_read_conversion (location_t loc, struct c_expr exp)
2006{
2007 mark_exp_read (exp.value);
2008 return default_function_array_conversion (loc, exp);
2009}
522ddfa2 2010
267bac10
JM
2011/* Return whether EXPR should be treated as an atomic lvalue for the
2012 purposes of load and store handling. */
2013
2014static bool
2015really_atomic_lvalue (tree expr)
2016{
7a0ca710 2017 if (error_operand_p (expr))
267bac10
JM
2018 return false;
2019 if (!TYPE_ATOMIC (TREE_TYPE (expr)))
2020 return false;
2021 if (!lvalue_p (expr))
2022 return false;
2023
2024 /* Ignore _Atomic on register variables, since their addresses can't
2025 be taken so (a) atomicity is irrelevant and (b) the normal atomic
2026 sequences wouldn't work. Ignore _Atomic on structures containing
2027 bit-fields, since accessing elements of atomic structures or
2028 unions is undefined behavior (C11 6.5.2.3#5), but it's unclear if
2029 it's undefined at translation time or execution time, and the
2030 normal atomic sequences again wouldn't work. */
2031 while (handled_component_p (expr))
2032 {
2033 if (TREE_CODE (expr) == COMPONENT_REF
2034 && DECL_C_BIT_FIELD (TREE_OPERAND (expr, 1)))
2035 return false;
2036 expr = TREE_OPERAND (expr, 0);
2037 }
2038 if (DECL_P (expr) && C_DECL_REGISTER (expr))
2039 return false;
2040 return true;
2041}
2042
2043/* Convert expression EXP (location LOC) from lvalue to rvalue,
2044 including converting functions and arrays to pointers if CONVERT_P.
2045 If READ_P, also mark the expression as having been read. */
2046
2047struct c_expr
2048convert_lvalue_to_rvalue (location_t loc, struct c_expr exp,
2049 bool convert_p, bool read_p)
2050{
2051 if (read_p)
2052 mark_exp_read (exp.value);
2053 if (convert_p)
2054 exp = default_function_array_conversion (loc, exp);
2055 if (really_atomic_lvalue (exp.value))
2056 {
2057 vec<tree, va_gc> *params;
2058 tree nonatomic_type, tmp, tmp_addr, fndecl, func_call;
2059 tree expr_type = TREE_TYPE (exp.value);
e51fbec3 2060 tree expr_addr = build_unary_op (loc, ADDR_EXPR, exp.value, false);
267bac10
JM
2061 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
2062
2063 gcc_assert (TYPE_ATOMIC (expr_type));
2064
2065 /* Expansion of a generic atomic load may require an addition
2066 element, so allocate enough to prevent a resize. */
2067 vec_alloc (params, 4);
2068
2069 /* Remove the qualifiers for the rest of the expressions and
2070 create the VAL temp variable to hold the RHS. */
2071 nonatomic_type = build_qualified_type (expr_type, TYPE_UNQUALIFIED);
5c4abbb8 2072 tmp = create_tmp_var_raw (nonatomic_type);
e51fbec3 2073 tmp_addr = build_unary_op (loc, ADDR_EXPR, tmp, false);
267bac10 2074 TREE_ADDRESSABLE (tmp) = 1;
cc28fc7f 2075 TREE_NO_WARNING (tmp) = 1;
267bac10
JM
2076
2077 /* Issue __atomic_load (&expr, &tmp, SEQ_CST); */
2078 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
2079 params->quick_push (expr_addr);
2080 params->quick_push (tmp_addr);
2081 params->quick_push (seq_cst);
8edbfaa6 2082 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10 2083
cc28fc7f
MP
2084 /* EXPR is always read. */
2085 mark_exp_read (exp.value);
2086
267bac10 2087 /* Return tmp which contains the value loaded. */
5c4abbb8
MP
2088 exp.value = build4 (TARGET_EXPR, nonatomic_type, tmp, func_call,
2089 NULL_TREE, NULL_TREE);
267bac10
JM
2090 }
2091 return exp;
2092}
2093
522ddfa2
JM
2094/* EXP is an expression of integer type. Apply the integer promotions
2095 to it and return the promoted value. */
400fbf9f
JW
2096
2097tree
522ddfa2 2098perform_integral_promotions (tree exp)
400fbf9f 2099{
b3694847
SS
2100 tree type = TREE_TYPE (exp);
2101 enum tree_code code = TREE_CODE (type);
400fbf9f 2102
522ddfa2 2103 gcc_assert (INTEGRAL_TYPE_P (type));
157689c6 2104
400fbf9f
JW
2105 /* Normally convert enums to int,
2106 but convert wide enums to something wider. */
2107 if (code == ENUMERAL_TYPE)
2108 {
b0c48229
NB
2109 type = c_common_type_for_size (MAX (TYPE_PRECISION (type),
2110 TYPE_PRECISION (integer_type_node)),
2111 ((TYPE_PRECISION (type)
2112 >= TYPE_PRECISION (integer_type_node))
8df83eae 2113 && TYPE_UNSIGNED (type)));
05bccae2 2114
400fbf9f
JW
2115 return convert (type, exp);
2116 }
2117
522ddfa2
JM
2118 /* ??? This should no longer be needed now bit-fields have their
2119 proper types. */
9753f113 2120 if (TREE_CODE (exp) == COMPONENT_REF
05bccae2 2121 && DECL_C_BIT_FIELD (TREE_OPERAND (exp, 1))
cff9c407 2122 /* If it's thinner than an int, promote it like a
d72040f5 2123 c_promoting_integer_type_p, otherwise leave it alone. */
01512446
JJ
2124 && compare_tree_int (DECL_SIZE (TREE_OPERAND (exp, 1)),
2125 TYPE_PRECISION (integer_type_node)) < 0)
f458d1d5 2126 return convert (integer_type_node, exp);
9753f113 2127
d72040f5 2128 if (c_promoting_integer_type_p (type))
400fbf9f 2129 {
f458d1d5 2130 /* Preserve unsignedness if not really getting any wider. */
8df83eae 2131 if (TYPE_UNSIGNED (type)
f458d1d5 2132 && TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
400fbf9f 2133 return convert (unsigned_type_node, exp);
05bccae2 2134
400fbf9f
JW
2135 return convert (integer_type_node, exp);
2136 }
05bccae2 2137
522ddfa2
JM
2138 return exp;
2139}
2140
2141
2142/* Perform default promotions for C data used in expressions.
46bdb9cf 2143 Enumeral types or short or char are converted to int.
522ddfa2
JM
2144 In addition, manifest constants symbols are replaced by their values. */
2145
2146tree
2147default_conversion (tree exp)
2148{
2149 tree orig_exp;
2150 tree type = TREE_TYPE (exp);
2151 enum tree_code code = TREE_CODE (type);
40449a90 2152 tree promoted_type;
522ddfa2 2153
ebfbbdc5
JJ
2154 mark_exp_read (exp);
2155
46bdb9cf
JM
2156 /* Functions and arrays have been converted during parsing. */
2157 gcc_assert (code != FUNCTION_TYPE);
2158 if (code == ARRAY_TYPE)
2159 return exp;
522ddfa2
JM
2160
2161 /* Constants can be used directly unless they're not loadable. */
2162 if (TREE_CODE (exp) == CONST_DECL)
2163 exp = DECL_INITIAL (exp);
2164
522ddfa2
JM
2165 /* Strip no-op conversions. */
2166 orig_exp = exp;
2167 STRIP_TYPE_NOPS (exp);
2168
2169 if (TREE_NO_WARNING (orig_exp))
2170 TREE_NO_WARNING (exp) = 1;
2171
400fbf9f
JW
2172 if (code == VOID_TYPE)
2173 {
5436fa2e
MP
2174 error_at (EXPR_LOC_OR_LOC (exp, input_location),
2175 "void value not ignored as it ought to be");
400fbf9f
JW
2176 return error_mark_node;
2177 }
808d6eaa 2178
4f2e1536 2179 exp = require_complete_type (EXPR_LOC_OR_LOC (exp, input_location), exp);
808d6eaa
JM
2180 if (exp == error_mark_node)
2181 return error_mark_node;
2182
40449a90
SL
2183 promoted_type = targetm.promoted_type (type);
2184 if (promoted_type)
2185 return convert (promoted_type, exp);
2186
808d6eaa
JM
2187 if (INTEGRAL_TYPE_P (type))
2188 return perform_integral_promotions (exp);
2189
400fbf9f
JW
2190 return exp;
2191}
2192\f
0fb96aa4 2193/* Look up COMPONENT in a structure or union TYPE.
e9b2c823
NB
2194
2195 If the component name is not found, returns NULL_TREE. Otherwise,
2196 the return value is a TREE_LIST, with each TREE_VALUE a FIELD_DECL
2197 stepping down the chain to the component, which is in the last
2198 TREE_VALUE of the list. Normally the list is of length one, but if
2199 the component is embedded within (nested) anonymous structures or
2200 unions, the list steps down the chain to the component. */
2f6e4e97 2201
2f2d13da 2202static tree
0fb96aa4 2203lookup_field (tree type, tree component)
2f2d13da
DE
2204{
2205 tree field;
2206
2207 /* If TYPE_LANG_SPECIFIC is set, then it is a sorted array of pointers
2208 to the field elements. Use a binary search on this array to quickly
2209 find the element. Otherwise, do a linear search. TYPE_LANG_SPECIFIC
e5e7e50d 2210 will always be set for structures which have many elements.
2f2d13da 2211
e5e7e50d
BH
2212 Duplicate field checking replaces duplicates with NULL_TREE so
2213 TYPE_LANG_SPECIFIC arrays are potentially no longer sorted. In that
2214 case just iterate using DECL_CHAIN. */
2215
2216 if (TYPE_LANG_SPECIFIC (type) && TYPE_LANG_SPECIFIC (type)->s
2217 && !seen_error ())
2f2d13da
DE
2218 {
2219 int bot, top, half;
d07605f5 2220 tree *field_array = &TYPE_LANG_SPECIFIC (type)->s->elts[0];
2f2d13da
DE
2221
2222 field = TYPE_FIELDS (type);
2223 bot = 0;
d07605f5 2224 top = TYPE_LANG_SPECIFIC (type)->s->len;
2f2d13da
DE
2225 while (top - bot > 1)
2226 {
2f2d13da
DE
2227 half = (top - bot + 1) >> 1;
2228 field = field_array[bot+half];
2229
2230 if (DECL_NAME (field) == NULL_TREE)
2231 {
2232 /* Step through all anon unions in linear fashion. */
2233 while (DECL_NAME (field_array[bot]) == NULL_TREE)
2234 {
2f2d13da 2235 field = field_array[bot++];
3e636daf 2236 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
19d76e60 2237 {
0fb96aa4 2238 tree anon = lookup_field (TREE_TYPE (field), component);
e9b2c823
NB
2239
2240 if (anon)
2241 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2242
2243 /* The Plan 9 compiler permits referring
2244 directly to an anonymous struct/union field
2245 using a typedef name. */
2246 if (flag_plan9_extensions
2247 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2248 && (TREE_CODE (TYPE_NAME (TREE_TYPE (field)))
2249 == TYPE_DECL)
2250 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2251 == component))
2252 break;
2f6e4e97 2253 }
2f2d13da
DE
2254 }
2255
2256 /* Entire record is only anon unions. */
2257 if (bot > top)
2258 return NULL_TREE;
2259
2260 /* Restart the binary search, with new lower bound. */
2261 continue;
2262 }
2263
e8b87aac 2264 if (DECL_NAME (field) == component)
2f2d13da 2265 break;
e8b87aac 2266 if (DECL_NAME (field) < component)
2f2d13da
DE
2267 bot += half;
2268 else
2269 top = bot + half;
2270 }
2271
2272 if (DECL_NAME (field_array[bot]) == component)
2273 field = field_array[bot];
2274 else if (DECL_NAME (field) != component)
e9b2c823 2275 return NULL_TREE;
2f2d13da
DE
2276 }
2277 else
2278 {
910ad8de 2279 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2f2d13da 2280 {
e9b2c823 2281 if (DECL_NAME (field) == NULL_TREE
3e636daf 2282 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2f2d13da 2283 {
0fb96aa4 2284 tree anon = lookup_field (TREE_TYPE (field), component);
a68b98cf 2285
e9b2c823
NB
2286 if (anon)
2287 return tree_cons (NULL_TREE, field, anon);
478a1c5b
ILT
2288
2289 /* The Plan 9 compiler permits referring directly to an
2290 anonymous struct/union field using a typedef
2291 name. */
2292 if (flag_plan9_extensions
2293 && TYPE_NAME (TREE_TYPE (field)) != NULL_TREE
2294 && TREE_CODE (TYPE_NAME (TREE_TYPE (field))) == TYPE_DECL
2295 && (DECL_NAME (TYPE_NAME (TREE_TYPE (field)))
2296 == component))
2297 break;
2f2d13da
DE
2298 }
2299
2300 if (DECL_NAME (field) == component)
2301 break;
2302 }
e9b2c823
NB
2303
2304 if (field == NULL_TREE)
2305 return NULL_TREE;
2f2d13da
DE
2306 }
2307
e9b2c823 2308 return tree_cons (NULL_TREE, field, NULL_TREE);
2f2d13da
DE
2309}
2310
277fe616
DM
2311/* Recursively append candidate IDENTIFIER_NODEs to CANDIDATES. */
2312
2313static void
2314lookup_field_fuzzy_find_candidates (tree type, tree component,
2315 vec<tree> *candidates)
2316{
2317 tree field;
2318 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
2319 {
2320 if (DECL_NAME (field) == NULL_TREE
3e636daf
MP
2321 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
2322 lookup_field_fuzzy_find_candidates (TREE_TYPE (field), component,
2323 candidates);
277fe616
DM
2324
2325 if (DECL_NAME (field))
2326 candidates->safe_push (DECL_NAME (field));
2327 }
2328}
2329
2330/* Like "lookup_field", but find the closest matching IDENTIFIER_NODE,
2331 rather than returning a TREE_LIST for an exact match. */
2332
2333static tree
2334lookup_field_fuzzy (tree type, tree component)
2335{
2336 gcc_assert (TREE_CODE (component) == IDENTIFIER_NODE);
2337
2338 /* First, gather a list of candidates. */
2339 auto_vec <tree> candidates;
2340
2341 lookup_field_fuzzy_find_candidates (type, component,
2342 &candidates);
2343
8ece8dfb 2344 return find_closest_identifier (component, &candidates);
277fe616
DM
2345}
2346
a87a86e1
DM
2347/* Support function for build_component_ref's error-handling.
2348
2349 Given DATUM_TYPE, and "DATUM.COMPONENT", where DATUM is *not* a
2350 struct or union, should we suggest "DATUM->COMPONENT" as a hint? */
2351
2352static bool
2353should_suggest_deref_p (tree datum_type)
2354{
2355 /* We don't do it for Objective-C, since Objective-C 2.0 dot-syntax
2356 allows "." for ptrs; we could be handling a failed attempt
2357 to access a property. */
2358 if (c_dialect_objc ())
2359 return false;
2360
2361 /* Only suggest it for pointers... */
2362 if (TREE_CODE (datum_type) != POINTER_TYPE)
2363 return false;
2364
2365 /* ...to structs/unions. */
2366 tree underlying_type = TREE_TYPE (datum_type);
2367 enum tree_code code = TREE_CODE (underlying_type);
2368 if (code == RECORD_TYPE || code == UNION_TYPE)
2369 return true;
2370 else
2371 return false;
2372}
2373
c2255bc4
AH
2374/* Make an expression to refer to the COMPONENT field of structure or
2375 union value DATUM. COMPONENT is an IDENTIFIER_NODE. LOC is the
6ffd47b7
DM
2376 location of the COMPONENT_REF. COMPONENT_LOC is the location
2377 of COMPONENT. */
400fbf9f
JW
2378
2379tree
6ffd47b7
DM
2380build_component_ref (location_t loc, tree datum, tree component,
2381 location_t component_loc)
400fbf9f 2382{
b3694847
SS
2383 tree type = TREE_TYPE (datum);
2384 enum tree_code code = TREE_CODE (type);
2385 tree field = NULL;
2386 tree ref;
1e57bf47 2387 bool datum_lvalue = lvalue_p (datum);
400fbf9f 2388
7a3ea201
RH
2389 if (!objc_is_public (datum, component))
2390 return error_mark_node;
2391
46a88c12 2392 /* Detect Objective-C property syntax object.property. */
668ea4b1 2393 if (c_dialect_objc ()
46a88c12 2394 && (ref = objc_maybe_build_component_ref (datum, component)))
668ea4b1
IS
2395 return ref;
2396
400fbf9f
JW
2397 /* See if there is a field or component with name COMPONENT. */
2398
2399 if (code == RECORD_TYPE || code == UNION_TYPE)
2400 {
d0f062fb 2401 if (!COMPLETE_TYPE_P (type))
400fbf9f 2402 {
4f2e1536 2403 c_incomplete_type_error (loc, NULL_TREE, type);
400fbf9f
JW
2404 return error_mark_node;
2405 }
2406
0fb96aa4 2407 field = lookup_field (type, component);
400fbf9f
JW
2408
2409 if (!field)
2410 {
277fe616
DM
2411 tree guessed_id = lookup_field_fuzzy (type, component);
2412 if (guessed_id)
6ffd47b7
DM
2413 {
2414 /* Attempt to provide a fixit replacement hint, if
2415 we have a valid range for the component. */
2416 location_t reported_loc
2417 = (component_loc != UNKNOWN_LOCATION) ? component_loc : loc;
264757fb 2418 gcc_rich_location rich_loc (reported_loc);
6ffd47b7 2419 if (component_loc != UNKNOWN_LOCATION)
264757fb 2420 rich_loc.add_fixit_misspelled_id (component_loc, guessed_id);
64a5912c
DM
2421 error_at (&rich_loc,
2422 "%qT has no member named %qE; did you mean %qE?",
2423 type, component, guessed_id);
6ffd47b7 2424 }
277fe616
DM
2425 else
2426 error_at (loc, "%qT has no member named %qE", type, component);
400fbf9f
JW
2427 return error_mark_node;
2428 }
400fbf9f 2429
fda5652f
MP
2430 /* Accessing elements of atomic structures or unions is undefined
2431 behavior (C11 6.5.2.3#5). */
2432 if (TYPE_ATOMIC (type) && c_inhibit_evaluation_warnings == 0)
2433 {
2434 if (code == RECORD_TYPE)
2435 warning_at (loc, 0, "accessing a member %qE of an atomic "
2436 "structure %qE", component, datum);
2437 else
2438 warning_at (loc, 0, "accessing a member %qE of an atomic "
2439 "union %qE", component, datum);
2440 }
2441
e9b2c823
NB
2442 /* Chain the COMPONENT_REFs if necessary down to the FIELD.
2443 This might be better solved in future the way the C++ front
2444 end does it - by giving the anonymous entities each a
2445 separate name and type, and then have build_component_ref
2446 recursively call itself. We can't do that here. */
46ea50cb 2447 do
19d76e60 2448 {
e9b2c823 2449 tree subdatum = TREE_VALUE (field);
efed193e
JM
2450 int quals;
2451 tree subtype;
1e57bf47 2452 bool use_datum_quals;
e9b2c823
NB
2453
2454 if (TREE_TYPE (subdatum) == error_mark_node)
2455 return error_mark_node;
2456
1e57bf47
JM
2457 /* If this is an rvalue, it does not have qualifiers in C
2458 standard terms and we must avoid propagating such
2459 qualifiers down to a non-lvalue array that is then
2460 converted to a pointer. */
2461 use_datum_quals = (datum_lvalue
2462 || TREE_CODE (TREE_TYPE (subdatum)) != ARRAY_TYPE);
2463
efed193e 2464 quals = TYPE_QUALS (strip_array_types (TREE_TYPE (subdatum)));
1e57bf47
JM
2465 if (use_datum_quals)
2466 quals |= TYPE_QUALS (TREE_TYPE (datum));
efed193e
JM
2467 subtype = c_build_qualified_type (TREE_TYPE (subdatum), quals);
2468
2469 ref = build3 (COMPONENT_REF, subtype, datum, subdatum,
53fb4de3 2470 NULL_TREE);
c2255bc4 2471 SET_EXPR_LOCATION (ref, loc);
1e57bf47
JM
2472 if (TREE_READONLY (subdatum)
2473 || (use_datum_quals && TREE_READONLY (datum)))
19d76e60 2474 TREE_READONLY (ref) = 1;
1e57bf47
JM
2475 if (TREE_THIS_VOLATILE (subdatum)
2476 || (use_datum_quals && TREE_THIS_VOLATILE (datum)))
19d76e60 2477 TREE_THIS_VOLATILE (ref) = 1;
e23bd218
IR
2478
2479 if (TREE_DEPRECATED (subdatum))
9b86d6bb 2480 warn_deprecated_use (subdatum, NULL_TREE);
e23bd218 2481
19d76e60 2482 datum = ref;
46ea50cb
RS
2483
2484 field = TREE_CHAIN (field);
19d76e60 2485 }
46ea50cb 2486 while (field);
19d76e60 2487
400fbf9f
JW
2488 return ref;
2489 }
a87a86e1
DM
2490 else if (should_suggest_deref_p (type))
2491 {
2492 /* Special-case the error message for "ptr.field" for the case
2493 where the user has confused "." vs "->". */
2494 rich_location richloc (line_table, loc);
2495 /* "loc" should be the "." token. */
f9087798 2496 richloc.add_fixit_replace ("->");
64a5912c
DM
2497 error_at (&richloc,
2498 "%qE is a pointer; did you mean to use %<->%>?",
2499 datum);
a87a86e1
DM
2500 return error_mark_node;
2501 }
400fbf9f 2502 else if (code != ERROR_MARK)
c2255bc4
AH
2503 error_at (loc,
2504 "request for member %qE in something not a structure or union",
2505 component);
400fbf9f
JW
2506
2507 return error_mark_node;
2508}
2509\f
2510/* Given an expression PTR for a pointer, return an expression
2511 for the value pointed to.
6a3799eb
AH
2512 ERRORSTRING is the name of the operator to appear in error messages.
2513
2514 LOC is the location to use for the generated tree. */
400fbf9f
JW
2515
2516tree
dd865ef6 2517build_indirect_ref (location_t loc, tree ptr, ref_operator errstring)
400fbf9f 2518{
b3694847
SS
2519 tree pointer = default_conversion (ptr);
2520 tree type = TREE_TYPE (pointer);
6a3799eb 2521 tree ref;
400fbf9f
JW
2522
2523 if (TREE_CODE (type) == POINTER_TYPE)
870cc33b 2524 {
1043771b 2525 if (CONVERT_EXPR_P (pointer)
79bedddc
SR
2526 || TREE_CODE (pointer) == VIEW_CONVERT_EXPR)
2527 {
2528 /* If a warning is issued, mark it to avoid duplicates from
2529 the backend. This only needs to be done at
2530 warn_strict_aliasing > 2. */
2531 if (warn_strict_aliasing > 2)
0444aa9c 2532 if (strict_aliasing_warning (EXPR_LOCATION (pointer),
79bedddc
SR
2533 type, TREE_OPERAND (pointer, 0)))
2534 TREE_NO_WARNING (pointer) = 1;
2535 }
2536
870cc33b 2537 if (TREE_CODE (pointer) == ADDR_EXPR
870cc33b
RS
2538 && (TREE_TYPE (TREE_OPERAND (pointer, 0))
2539 == TREE_TYPE (type)))
6a3799eb
AH
2540 {
2541 ref = TREE_OPERAND (pointer, 0);
2542 protected_set_expr_location (ref, loc);
2543 return ref;
2544 }
870cc33b
RS
2545 else
2546 {
2547 tree t = TREE_TYPE (type);
46df2823 2548
984dfd8c 2549 ref = build1 (INDIRECT_REF, t, pointer);
400fbf9f 2550
baae9b65 2551 if (!COMPLETE_OR_VOID_TYPE_P (t) && TREE_CODE (t) != ARRAY_TYPE)
870cc33b 2552 {
d9b7be2e
MP
2553 if (!C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)))
2554 {
2555 error_at (loc, "dereferencing pointer to incomplete type "
2556 "%qT", t);
2557 C_TYPE_ERROR_REPORTED (TREE_TYPE (ptr)) = 1;
2558 }
870cc33b
RS
2559 return error_mark_node;
2560 }
7d882b83 2561 if (VOID_TYPE_P (t) && c_inhibit_evaluation_warnings == 0)
c9f9eb5d 2562 warning_at (loc, 0, "dereferencing %<void *%> pointer");
870cc33b
RS
2563
2564 /* We *must* set TREE_READONLY when dereferencing a pointer to const,
2565 so that we get the proper error message if the result is used
2566 to assign to. Also, &* is supposed to be a no-op.
2567 And ANSI C seems to specify that the type of the result
2568 should be the const type. */
2569 /* A de-reference of a pointer to const is not a const. It is valid
2570 to change it via some other pointer. */
2571 TREE_READONLY (ref) = TYPE_READONLY (t);
2572 TREE_SIDE_EFFECTS (ref)
271bd540 2573 = TYPE_VOLATILE (t) || TREE_SIDE_EFFECTS (pointer);
493692cd 2574 TREE_THIS_VOLATILE (ref) = TYPE_VOLATILE (t);
6a3799eb 2575 protected_set_expr_location (ref, loc);
870cc33b
RS
2576 return ref;
2577 }
2578 }
400fbf9f 2579 else if (TREE_CODE (pointer) != ERROR_MARK)
7a6daeb0
NF
2580 invalid_indirection_error (loc, type, errstring);
2581
400fbf9f
JW
2582 return error_mark_node;
2583}
2584
2585/* This handles expressions of the form "a[i]", which denotes
2586 an array reference.
2587
2588 This is logically equivalent in C to *(a+i), but we may do it differently.
2589 If A is a variable or a member, we generate a primitive ARRAY_REF.
2590 This avoids forcing the array out of registers, and can work on
2591 arrays that are not lvalues (for example, members of structures returned
6a3799eb
AH
2592 by functions).
2593
30cd1c5d
AS
2594 For vector types, allow vector[i] but not i[vector], and create
2595 *(((type*)&vectortype) + i) for the expression.
2596
6a3799eb 2597 LOC is the location to use for the returned expression. */
400fbf9f
JW
2598
2599tree
c2255bc4 2600build_array_ref (location_t loc, tree array, tree index)
400fbf9f 2601{
6a3799eb 2602 tree ret;
a4ab7973 2603 bool swapped = false;
400fbf9f
JW
2604 if (TREE_TYPE (array) == error_mark_node
2605 || TREE_TYPE (index) == error_mark_node)
2606 return error_mark_node;
2607
a4ab7973 2608 if (TREE_CODE (TREE_TYPE (array)) != ARRAY_TYPE
30cd1c5d
AS
2609 && TREE_CODE (TREE_TYPE (array)) != POINTER_TYPE
2610 /* Allow vector[index] but not index[vector]. */
31521951 2611 && !VECTOR_TYPE_P (TREE_TYPE (array)))
400fbf9f 2612 {
a4ab7973
JM
2613 if (TREE_CODE (TREE_TYPE (index)) != ARRAY_TYPE
2614 && TREE_CODE (TREE_TYPE (index)) != POINTER_TYPE)
fdeefd49 2615 {
f90e8e2e 2616 error_at (loc,
30cd1c5d
AS
2617 "subscripted value is neither array nor pointer nor vector");
2618
fdeefd49
RS
2619 return error_mark_node;
2620 }
fab27f52 2621 std::swap (array, index);
a4ab7973
JM
2622 swapped = true;
2623 }
2624
2625 if (!INTEGRAL_TYPE_P (TREE_TYPE (index)))
2626 {
a63068b6 2627 error_at (loc, "array subscript is not an integer");
a4ab7973
JM
2628 return error_mark_node;
2629 }
2630
2631 if (TREE_CODE (TREE_TYPE (TREE_TYPE (array))) == FUNCTION_TYPE)
2632 {
a63068b6 2633 error_at (loc, "subscripted value is pointer to function");
a4ab7973
JM
2634 return error_mark_node;
2635 }
2636
ff6b6641
GDR
2637 /* ??? Existing practice has been to warn only when the char
2638 index is syntactically the index, not for char[array]. */
2639 if (!swapped)
5bd012f8 2640 warn_array_subscript_with_type_char (loc, index);
a4ab7973
JM
2641
2642 /* Apply default promotions *after* noticing character types. */
2643 index = default_conversion (index);
f406ae1f
MP
2644 if (index == error_mark_node)
2645 return error_mark_node;
a4ab7973
JM
2646
2647 gcc_assert (TREE_CODE (TREE_TYPE (index)) == INTEGER_TYPE);
f90e8e2e 2648
f17a223d
RB
2649 bool was_vector = VECTOR_TYPE_P (TREE_TYPE (array));
2650 bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, index);
a4ab7973
JM
2651
2652 if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
2653 {
e4d35515 2654 tree rval, type;
fdeefd49 2655
400fbf9f
JW
2656 /* An array that is indexed by a non-constant
2657 cannot be stored in a register; we must be able to do
2658 address arithmetic on its address.
2659 Likewise an array of elements of variable size. */
2660 if (TREE_CODE (index) != INTEGER_CST
d0f062fb 2661 || (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
400fbf9f
JW
2662 && TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array)))) != INTEGER_CST))
2663 {
a9e4a1a5 2664 if (!c_mark_addressable (array, true))
400fbf9f
JW
2665 return error_mark_node;
2666 }
e6d52559
JW
2667 /* An array that is indexed by a constant value which is not within
2668 the array bounds cannot be stored in a register either; because we
2669 would get a crash in store_bit_field/extract_bit_field when trying
2670 to access a non-existent part of the register. */
2671 if (TREE_CODE (index) == INTEGER_CST
eb34af89 2672 && TYPE_DOMAIN (TREE_TYPE (array))
3f75a254 2673 && !int_fits_type_p (index, TYPE_DOMAIN (TREE_TYPE (array))))
e6d52559 2674 {
dffd7eb6 2675 if (!c_mark_addressable (array))
e6d52559
JW
2676 return error_mark_node;
2677 }
400fbf9f 2678
f17a223d
RB
2679 if ((pedantic || warn_c90_c99_compat)
2680 && ! was_vector)
400fbf9f
JW
2681 {
2682 tree foo = array;
2683 while (TREE_CODE (foo) == COMPONENT_REF)
2684 foo = TREE_OPERAND (foo, 0);
0ae9bd27 2685 if (VAR_P (foo) && C_DECL_REGISTER (foo))
c1771a20 2686 pedwarn (loc, OPT_Wpedantic,
fcf73884 2687 "ISO C forbids subscripting %<register%> array");
f3bede71
MP
2688 else if (!lvalue_p (foo))
2689 pedwarn_c90 (loc, OPT_Wpedantic,
2690 "ISO C90 forbids subscripting non-lvalue "
2691 "array");
400fbf9f
JW
2692 }
2693
46df2823 2694 type = TREE_TYPE (TREE_TYPE (array));
53fb4de3 2695 rval = build4 (ARRAY_REF, type, array, index, NULL_TREE, NULL_TREE);
400fbf9f 2696 /* Array ref is const/volatile if the array elements are
c22cacf3 2697 or if the array is. */
400fbf9f
JW
2698 TREE_READONLY (rval)
2699 |= (TYPE_READONLY (TREE_TYPE (TREE_TYPE (array)))
2700 | TREE_READONLY (array));
2701 TREE_SIDE_EFFECTS (rval)
2702 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2703 | TREE_SIDE_EFFECTS (array));
2704 TREE_THIS_VOLATILE (rval)
2705 |= (TYPE_VOLATILE (TREE_TYPE (TREE_TYPE (array)))
2706 /* This was added by rms on 16 Nov 91.
2f6e4e97 2707 It fixes vol struct foo *a; a->elts[1]
400fbf9f
JW
2708 in an inline function.
2709 Hope it doesn't break something else. */
2710 | TREE_THIS_VOLATILE (array));
4f2e1536 2711 ret = require_complete_type (loc, rval);
6a3799eb 2712 protected_set_expr_location (ret, loc);
aa7da51a
JJ
2713 if (non_lvalue)
2714 ret = non_lvalue_loc (loc, ret);
6a3799eb 2715 return ret;
400fbf9f 2716 }
a4ab7973
JM
2717 else
2718 {
2719 tree ar = default_conversion (array);
400fbf9f 2720
a4ab7973
JM
2721 if (ar == error_mark_node)
2722 return ar;
400fbf9f 2723
a4ab7973
JM
2724 gcc_assert (TREE_CODE (TREE_TYPE (ar)) == POINTER_TYPE);
2725 gcc_assert (TREE_CODE (TREE_TYPE (TREE_TYPE (ar))) != FUNCTION_TYPE);
400fbf9f 2726
aa7da51a 2727 ret = build_indirect_ref (loc, build_binary_op (loc, PLUS_EXPR, ar,
30af3a2b 2728 index, false),
aa7da51a
JJ
2729 RO_ARRAY_INDEXING);
2730 if (non_lvalue)
2731 ret = non_lvalue_loc (loc, ret);
2732 return ret;
a4ab7973 2733 }
400fbf9f
JW
2734}
2735\f
7e585d16 2736/* Build an external reference to identifier ID. FUN indicates
766beb40 2737 whether this will be used for a function call. LOC is the source
6866c6e8
ILT
2738 location of the identifier. This sets *TYPE to the type of the
2739 identifier, which is not the same as the type of the returned value
2740 for CONST_DECLs defined as enum constants. If the type of the
2741 identifier is not available, *TYPE is set to NULL. */
7e585d16 2742tree
30af3a2b 2743build_external_ref (location_t loc, tree id, bool fun, tree *type)
7e585d16
ZW
2744{
2745 tree ref;
2746 tree decl = lookup_name (id);
16b34ad6
ZL
2747
2748 /* In Objective-C, an instance variable (ivar) may be preferred to
2749 whatever lookup_name() found. */
2750 decl = objc_lookup_ivar (decl, id);
7e585d16 2751
6866c6e8 2752 *type = NULL;
339a28b9 2753 if (decl && decl != error_mark_node)
6866c6e8
ILT
2754 {
2755 ref = decl;
2756 *type = TREE_TYPE (ref);
2757 }
339a28b9
ZW
2758 else if (fun)
2759 /* Implicit function declaration. */
c2255bc4 2760 ref = implicitly_declare (loc, id);
339a28b9
ZW
2761 else if (decl == error_mark_node)
2762 /* Don't complain about something that's already been
2763 complained about. */
2764 return error_mark_node;
2765 else
2766 {
c2255bc4 2767 undeclared_variable (loc, id);
339a28b9
ZW
2768 return error_mark_node;
2769 }
7e585d16
ZW
2770
2771 if (TREE_TYPE (ref) == error_mark_node)
2772 return error_mark_node;
2773
339a28b9 2774 if (TREE_DEPRECATED (ref))
9b86d6bb 2775 warn_deprecated_use (ref, NULL_TREE);
339a28b9 2776
ad960f56 2777 /* Recursive call does not count as usage. */
b8698a0f 2778 if (ref != current_function_decl)
ad960f56 2779 {
ad960f56
MLI
2780 TREE_USED (ref) = 1;
2781 }
7e585d16 2782
bc4b653b
JM
2783 if (TREE_CODE (ref) == FUNCTION_DECL && !in_alignof)
2784 {
2785 if (!in_sizeof && !in_typeof)
2786 C_DECL_USED (ref) = 1;
3fa8871b 2787 else if (DECL_INITIAL (ref) == NULL_TREE
bc4b653b
JM
2788 && DECL_EXTERNAL (ref)
2789 && !TREE_PUBLIC (ref))
2790 record_maybe_used_decl (ref);
2791 }
2792
7e585d16
ZW
2793 if (TREE_CODE (ref) == CONST_DECL)
2794 {
6193b8b7 2795 used_types_insert (TREE_TYPE (ref));
24b97832
ILT
2796
2797 if (warn_cxx_compat
2798 && TREE_CODE (TREE_TYPE (ref)) == ENUMERAL_TYPE
2799 && C_TYPE_DEFINED_IN_STRUCT (TREE_TYPE (ref)))
2800 {
2801 warning_at (loc, OPT_Wc___compat,
2802 ("enum constant defined in struct or union "
2803 "is not visible in C++"));
2804 inform (DECL_SOURCE_LOCATION (ref), "enum constant defined here");
2805 }
2806
7e585d16
ZW
2807 ref = DECL_INITIAL (ref);
2808 TREE_CONSTANT (ref) = 1;
2809 }
3fa8871b 2810 else if (current_function_decl != NULL_TREE
4b1e44be 2811 && !DECL_FILE_SCOPE_P (current_function_decl)
21b634ae
MP
2812 && (VAR_OR_FUNCTION_DECL_P (ref)
2813 || TREE_CODE (ref) == PARM_DECL))
6a29edea
EB
2814 {
2815 tree context = decl_function_context (ref);
2f6e4e97 2816
3fa8871b 2817 if (context != NULL_TREE && context != current_function_decl)
6a29edea
EB
2818 DECL_NONLOCAL (ref) = 1;
2819 }
71113fcd
GK
2820 /* C99 6.7.4p3: An inline definition of a function with external
2821 linkage ... shall not contain a reference to an identifier with
2822 internal linkage. */
3fa8871b 2823 else if (current_function_decl != NULL_TREE
71113fcd
GK
2824 && DECL_DECLARED_INLINE_P (current_function_decl)
2825 && DECL_EXTERNAL (current_function_decl)
2826 && VAR_OR_FUNCTION_DECL_P (ref)
0ae9bd27 2827 && (!VAR_P (ref) || TREE_STATIC (ref))
1033ffa8
JJ
2828 && ! TREE_PUBLIC (ref)
2829 && DECL_CONTEXT (ref) != current_function_decl)
991d6621
JM
2830 record_inline_static (loc, current_function_decl, ref,
2831 csi_internal);
7e585d16
ZW
2832
2833 return ref;
2834}
2835
bc4b653b
JM
2836/* Record details of decls possibly used inside sizeof or typeof. */
2837struct maybe_used_decl
2838{
2839 /* The decl. */
2840 tree decl;
2841 /* The level seen at (in_sizeof + in_typeof). */
2842 int level;
2843 /* The next one at this level or above, or NULL. */
2844 struct maybe_used_decl *next;
2845};
2846
2847static struct maybe_used_decl *maybe_used_decls;
2848
2849/* Record that DECL, an undefined static function reference seen
2850 inside sizeof or typeof, might be used if the operand of sizeof is
2851 a VLA type or the operand of typeof is a variably modified
2852 type. */
2853
4e2fb7de 2854static void
bc4b653b
JM
2855record_maybe_used_decl (tree decl)
2856{
2857 struct maybe_used_decl *t = XOBNEW (&parser_obstack, struct maybe_used_decl);
2858 t->decl = decl;
2859 t->level = in_sizeof + in_typeof;
2860 t->next = maybe_used_decls;
2861 maybe_used_decls = t;
2862}
2863
2864/* Pop the stack of decls possibly used inside sizeof or typeof. If
2865 USED is false, just discard them. If it is true, mark them used
2866 (if no longer inside sizeof or typeof) or move them to the next
2867 level up (if still inside sizeof or typeof). */
2868
2869void
2870pop_maybe_used (bool used)
2871{
2872 struct maybe_used_decl *p = maybe_used_decls;
2873 int cur_level = in_sizeof + in_typeof;
2874 while (p && p->level > cur_level)
2875 {
2876 if (used)
2877 {
2878 if (cur_level == 0)
2879 C_DECL_USED (p->decl) = 1;
2880 else
2881 p->level = cur_level;
2882 }
2883 p = p->next;
2884 }
2885 if (!used || cur_level == 0)
2886 maybe_used_decls = p;
2887}
2888
2889/* Return the result of sizeof applied to EXPR. */
2890
2891struct c_expr
c2255bc4 2892c_expr_sizeof_expr (location_t loc, struct c_expr expr)
bc4b653b
JM
2893{
2894 struct c_expr ret;
ad97f4be
JM
2895 if (expr.value == error_mark_node)
2896 {
2897 ret.value = error_mark_node;
2898 ret.original_code = ERROR_MARK;
6866c6e8 2899 ret.original_type = NULL;
ad97f4be
JM
2900 pop_maybe_used (false);
2901 }
2902 else
2903 {
928c19bb 2904 bool expr_const_operands = true;
773ec47f
MP
2905
2906 if (TREE_CODE (expr.value) == PARM_DECL
2907 && C_ARRAY_PARAMETER (expr.value))
2908 {
097f82ec 2909 auto_diagnostic_group d;
773ec47f
MP
2910 if (warning_at (loc, OPT_Wsizeof_array_argument,
2911 "%<sizeof%> on array function parameter %qE will "
2912 "return size of %qT", expr.value,
267bbb6f 2913 TREE_TYPE (expr.value)))
773ec47f
MP
2914 inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
2915 }
928c19bb
JM
2916 tree folded_expr = c_fully_fold (expr.value, require_constant_value,
2917 &expr_const_operands);
c2255bc4 2918 ret.value = c_sizeof (loc, TREE_TYPE (folded_expr));
1a4049e7 2919 c_last_sizeof_arg = expr.value;
40ffd95f 2920 c_last_sizeof_loc = loc;
1a4049e7 2921 ret.original_code = SIZEOF_EXPR;
6866c6e8 2922 ret.original_type = NULL;
928c19bb 2923 if (c_vla_type_p (TREE_TYPE (folded_expr)))
52ffd86e
MS
2924 {
2925 /* sizeof is evaluated when given a vla (C99 6.5.3.4p2). */
928c19bb
JM
2926 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2927 folded_expr, ret.value);
2928 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !expr_const_operands;
c2255bc4 2929 SET_EXPR_LOCATION (ret.value, loc);
52ffd86e 2930 }
928c19bb 2931 pop_maybe_used (C_TYPE_VARIABLE_SIZE (TREE_TYPE (folded_expr)));
ad97f4be 2932 }
bc4b653b
JM
2933 return ret;
2934}
2935
2936/* Return the result of sizeof applied to T, a structure for the type
c2255bc4
AH
2937 name passed to sizeof (rather than the type itself). LOC is the
2938 location of the original expression. */
bc4b653b
JM
2939
2940struct c_expr
c2255bc4 2941c_expr_sizeof_type (location_t loc, struct c_type_name *t)
bc4b653b
JM
2942{
2943 tree type;
2944 struct c_expr ret;
928c19bb
JM
2945 tree type_expr = NULL_TREE;
2946 bool type_expr_const = true;
2947 type = groktypename (t, &type_expr, &type_expr_const);
c2255bc4 2948 ret.value = c_sizeof (loc, type);
1a4049e7 2949 c_last_sizeof_arg = type;
40ffd95f 2950 c_last_sizeof_loc = loc;
1a4049e7 2951 ret.original_code = SIZEOF_EXPR;
6866c6e8 2952 ret.original_type = NULL;
24070fcb
JM
2953 if ((type_expr || TREE_CODE (ret.value) == INTEGER_CST)
2954 && c_vla_type_p (type))
928c19bb 2955 {
24070fcb
JM
2956 /* If the type is a [*] array, it is a VLA but is represented as
2957 having a size of zero. In such a case we must ensure that
2958 the result of sizeof does not get folded to a constant by
2959 c_fully_fold, because if the size is evaluated the result is
2960 not constant and so constraints on zero or negative size
2961 arrays must not be applied when this sizeof call is inside
2962 another array declarator. */
2963 if (!type_expr)
2964 type_expr = integer_zero_node;
928c19bb
JM
2965 ret.value = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret.value),
2966 type_expr, ret.value);
2967 C_MAYBE_CONST_EXPR_NON_CONST (ret.value) = !type_expr_const;
2968 }
16464cc1
VR
2969 pop_maybe_used (type != error_mark_node
2970 ? C_TYPE_VARIABLE_SIZE (type) : false);
bc4b653b
JM
2971 return ret;
2972}
2973
400fbf9f 2974/* Build a function call to function FUNCTION with parameters PARAMS.
c2255bc4 2975 The function call is at LOC.
400fbf9f
JW
2976 PARAMS is a list--a chain of TREE_LIST nodes--in which the
2977 TREE_VALUE of each node is a parameter-expression.
2978 FUNCTION's data type may be a function type or a pointer-to-function. */
2979
2980tree
c2255bc4 2981build_function_call (location_t loc, tree function, tree params)
bbbbb16a 2982{
9771b263 2983 vec<tree, va_gc> *v;
bbbbb16a
ILT
2984 tree ret;
2985
9771b263 2986 vec_alloc (v, list_length (params));
bbbbb16a 2987 for (; params; params = TREE_CHAIN (params))
9771b263 2988 v->quick_push (TREE_VALUE (params));
8edbfaa6 2989 ret = c_build_function_call_vec (loc, vNULL, function, v, NULL);
9771b263 2990 vec_free (v);
bbbbb16a
ILT
2991 return ret;
2992}
2993
ae52741c
MLI
2994/* Give a note about the location of the declaration of DECL. */
2995
c7b70a3c
MP
2996static void
2997inform_declaration (tree decl)
ae52741c 2998{
c7b70a3c 2999 if (decl && (TREE_CODE (decl) != FUNCTION_DECL || !DECL_IS_BUILTIN (decl)))
ae52741c
MLI
3000 inform (DECL_SOURCE_LOCATION (decl), "declared here");
3001}
3002
bbbbb16a
ILT
3003/* Build a function call to function FUNCTION with parameters PARAMS.
3004 ORIGTYPES, if not NULL, is a vector of types; each element is
3005 either NULL or the original type of the corresponding element in
3006 PARAMS. The original type may differ from TREE_TYPE of the
3007 parameter for enums. FUNCTION's data type may be a function type
3008 or pointer-to-function. This function changes the elements of
3009 PARAMS. */
3010
3011tree
81e5eca8
MP
3012build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3013 tree function, vec<tree, va_gc> *params,
9771b263 3014 vec<tree, va_gc> *origtypes)
400fbf9f 3015{
3fa8871b 3016 tree fntype, fundecl = NULL_TREE;
4977bab6 3017 tree name = NULL_TREE, result;
c96f4f73 3018 tree tem;
94a0dd7b
SL
3019 int nargs;
3020 tree *argarray;
b8698a0f 3021
400fbf9f 3022
fc76e425 3023 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
a7d53fce 3024 STRIP_TYPE_NOPS (function);
400fbf9f
JW
3025
3026 /* Convert anything with function type to a pointer-to-function. */
3027 if (TREE_CODE (function) == FUNCTION_DECL)
3028 {
3029 name = DECL_NAME (function);
0a35513e
AH
3030
3031 if (flag_tm)
3032 tm_malloc_replacement (function);
a5eadacc 3033 fundecl = function;
86951993
AM
3034 /* Atomic functions have type checking/casting already done. They are
3035 often rewritten and don't match the original parameter list. */
3036 if (name && !strncmp (IDENTIFIER_POINTER (name), "__atomic_", 9))
3037 origtypes = NULL;
400fbf9f 3038 }
f2a71bbc 3039 if (TREE_CODE (TREE_TYPE (function)) == FUNCTION_TYPE)
c2255bc4 3040 function = function_to_pointer_conversion (loc, function);
400fbf9f 3041
6e955430
ZL
3042 /* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
3043 expressions, like those used for ObjC messenger dispatches. */
9771b263
DN
3044 if (params && !params->is_empty ())
3045 function = objc_rewrite_function_call (function, (*params)[0]);
6e955430 3046
928c19bb
JM
3047 function = c_fully_fold (function, false, NULL);
3048
400fbf9f
JW
3049 fntype = TREE_TYPE (function);
3050
3051 if (TREE_CODE (fntype) == ERROR_MARK)
3052 return error_mark_node;
3053
3054 if (!(TREE_CODE (fntype) == POINTER_TYPE
3055 && TREE_CODE (TREE_TYPE (fntype)) == FUNCTION_TYPE))
3056 {
ae52741c
MLI
3057 if (!flag_diagnostics_show_caret)
3058 error_at (loc,
3059 "called object %qE is not a function or function pointer",
3060 function);
3061 else if (DECL_P (function))
3062 {
3063 error_at (loc,
3064 "called object %qD is not a function or function pointer",
3065 function);
3066 inform_declaration (function);
3067 }
3068 else
3069 error_at (loc,
3070 "called object is not a function or function pointer");
400fbf9f
JW
3071 return error_mark_node;
3072 }
3073
5ce89b2e
JM
3074 if (fundecl && TREE_THIS_VOLATILE (fundecl))
3075 current_function_returns_abnormally = 1;
3076
400fbf9f
JW
3077 /* fntype now gets the type of function pointed to. */
3078 fntype = TREE_TYPE (fntype);
3079
ab4194da
JM
3080 /* Convert the parameters to the types declared in the
3081 function prototype, or apply default promotions. */
3082
81e5eca8
MP
3083 nargs = convert_arguments (loc, arg_loc, TYPE_ARG_TYPES (fntype), params,
3084 origtypes, function, fundecl);
ab4194da
JM
3085 if (nargs < 0)
3086 return error_mark_node;
3087
c96f4f73 3088 /* Check that the function is called through a compatible prototype.
fa337f3a 3089 If it is not, warn. */
1043771b 3090 if (CONVERT_EXPR_P (function)
c96f4f73
EB
3091 && TREE_CODE (tem = TREE_OPERAND (function, 0)) == ADDR_EXPR
3092 && TREE_CODE (tem = TREE_OPERAND (tem, 0)) == FUNCTION_DECL
3f75a254 3093 && !comptypes (fntype, TREE_TYPE (tem)))
c96f4f73
EB
3094 {
3095 tree return_type = TREE_TYPE (fntype);
c96f4f73
EB
3096
3097 /* This situation leads to run-time undefined behavior. We can't,
3098 therefore, simply error unless we can prove that all possible
3099 executions of the program must execute the code. */
fa337f3a 3100 warning_at (loc, 0, "function called through a non-compatible type");
c96f4f73 3101
fa337f3a
RB
3102 if (VOID_TYPE_P (return_type)
3103 && TYPE_QUALS (return_type) != TYPE_UNQUALIFIED)
3104 pedwarn (loc, 0,
3105 "function with qualified void return type called");
3106 }
c96f4f73 3107
9771b263 3108 argarray = vec_safe_address (params);
bbbbb16a 3109
83322951
RG
3110 /* Check that arguments to builtin functions match the expectations. */
3111 if (fundecl
3112 && DECL_BUILT_IN (fundecl)
3113 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL
79ce98bc
MP
3114 && !check_builtin_function_arguments (loc, arg_loc, fundecl, nargs,
3115 argarray))
83322951 3116 return error_mark_node;
400fbf9f 3117
83322951 3118 /* Check that the arguments to the function are valid. */
4227c9ad 3119 bool warned_p = check_function_arguments (loc, fundecl, fntype,
2f687306 3120 nargs, argarray, &arg_loc);
400fbf9f 3121
928c19bb
JM
3122 if (name != NULL_TREE
3123 && !strncmp (IDENTIFIER_POINTER (name), "__builtin_", 10))
bf730f15 3124 {
928c19bb 3125 if (require_constant_value)
0dba7960
JJ
3126 result
3127 = fold_build_call_array_initializer_loc (loc, TREE_TYPE (fntype),
3128 function, nargs, argarray);
928c19bb 3129 else
db3927fb
AH
3130 result = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
3131 function, nargs, argarray);
928c19bb
JM
3132 if (TREE_CODE (result) == NOP_EXPR
3133 && TREE_CODE (TREE_OPERAND (result, 0)) == INTEGER_CST)
3134 STRIP_TYPE_NOPS (result);
bf730f15
RS
3135 }
3136 else
db3927fb
AH
3137 result = build_call_array_loc (loc, TREE_TYPE (fntype),
3138 function, nargs, argarray);
0dba7960
JJ
3139 /* If -Wnonnull warning has been diagnosed, avoid diagnosing it again
3140 later. */
3141 if (warned_p && TREE_CODE (result) == CALL_EXPR)
3142 TREE_NO_WARNING (result) = 1;
b0b3afb2 3143
ab4c578f
MP
3144 /* In this improbable scenario, a nested function returns a VM type.
3145 Create a TARGET_EXPR so that the call always has a LHS, much as
3146 what the C++ FE does for functions returning non-PODs. */
3147 if (variably_modified_type_p (TREE_TYPE (fntype), NULL_TREE))
3148 {
3149 tree tmp = create_tmp_var_raw (TREE_TYPE (fntype));
3150 result = build4 (TARGET_EXPR, TREE_TYPE (fntype), tmp, result,
3151 NULL_TREE, NULL_TREE);
3152 }
3153
71653180 3154 if (VOID_TYPE_P (TREE_TYPE (result)))
3ce62965
JM
3155 {
3156 if (TYPE_QUALS (TREE_TYPE (result)) != TYPE_UNQUALIFIED)
db3927fb 3157 pedwarn (loc, 0,
3ce62965
JM
3158 "function with qualified void return type called");
3159 return result;
3160 }
4f2e1536 3161 return require_complete_type (loc, result);
400fbf9f 3162}
8edbfaa6
JJ
3163
3164/* Like build_function_call_vec, but call also resolve_overloaded_builtin. */
3165
3166tree
3167c_build_function_call_vec (location_t loc, vec<location_t> arg_loc,
3168 tree function, vec<tree, va_gc> *params,
3169 vec<tree, va_gc> *origtypes)
3170{
3171 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
3172 STRIP_TYPE_NOPS (function);
3173
3174 /* Convert anything with function type to a pointer-to-function. */
3175 if (TREE_CODE (function) == FUNCTION_DECL)
3176 {
3177 /* Implement type-directed function overloading for builtins.
3178 resolve_overloaded_builtin and targetm.resolve_overloaded_builtin
3179 handle all the type checking. The result is a complete expression
3180 that implements this function call. */
3181 tree tem = resolve_overloaded_builtin (loc, function, params);
3182 if (tem)
3183 return tem;
3184 }
3185 return build_function_call_vec (loc, arg_loc, function, params, origtypes);
3186}
400fbf9f 3187\f
bbbbb16a
ILT
3188/* Convert the argument expressions in the vector VALUES
3189 to the types in the list TYPELIST.
400fbf9f
JW
3190
3191 If TYPELIST is exhausted, or when an element has NULL as its type,
3192 perform the default conversions.
3193
bbbbb16a
ILT
3194 ORIGTYPES is the original types of the expressions in VALUES. This
3195 holds the type of enum values which have been converted to integral
3196 types. It may be NULL.
400fbf9f 3197
03dafa61
JM
3198 FUNCTION is a tree for the called function. It is used only for
3199 error messages, where it is formatted with %qE.
400fbf9f
JW
3200
3201 This is also where warnings about wrong number of args are generated.
3202
81e5eca8
MP
3203 ARG_LOC are locations of function arguments (if any).
3204
94a0dd7b 3205 Returns the actual number of arguments processed (which may be less
bbbbb16a
ILT
3206 than the length of VALUES in some error situations), or -1 on
3207 failure. */
94a0dd7b
SL
3208
3209static int
81e5eca8
MP
3210convert_arguments (location_t loc, vec<location_t> arg_loc, tree typelist,
3211 vec<tree, va_gc> *values, vec<tree, va_gc> *origtypes,
3212 tree function, tree fundecl)
400fbf9f 3213{
bbbbb16a
ILT
3214 tree typetail, val;
3215 unsigned int parmnum;
06302a02 3216 bool error_args = false;
b5d32c25 3217 const bool type_generic = fundecl
81e5eca8 3218 && lookup_attribute ("type generic", TYPE_ATTRIBUTES (TREE_TYPE (fundecl)));
8ce94e44 3219 bool type_generic_remove_excess_precision = false;
44a845ca 3220 bool type_generic_overflow_p = false;
03dafa61 3221 tree selector;
03dafa61 3222
2ac2f164
JM
3223 /* Change pointer to function to the function itself for
3224 diagnostics. */
03dafa61
JM
3225 if (TREE_CODE (function) == ADDR_EXPR
3226 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
2ac2f164 3227 function = TREE_OPERAND (function, 0);
03dafa61
JM
3228
3229 /* Handle an ObjC selector specially for diagnostics. */
3230 selector = objc_message_selector ();
400fbf9f 3231
8ce94e44
JM
3232 /* For type-generic built-in functions, determine whether excess
3233 precision should be removed (classification) or not
3234 (comparison). */
3235 if (type_generic
3236 && DECL_BUILT_IN (fundecl)
3237 && DECL_BUILT_IN_CLASS (fundecl) == BUILT_IN_NORMAL)
3238 {
3239 switch (DECL_FUNCTION_CODE (fundecl))
3240 {
3241 case BUILT_IN_ISFINITE:
3242 case BUILT_IN_ISINF:
3243 case BUILT_IN_ISINF_SIGN:
3244 case BUILT_IN_ISNAN:
3245 case BUILT_IN_ISNORMAL:
3246 case BUILT_IN_FPCLASSIFY:
3247 type_generic_remove_excess_precision = true;
3248 break;
3249
44a845ca
MS
3250 case BUILT_IN_ADD_OVERFLOW_P:
3251 case BUILT_IN_SUB_OVERFLOW_P:
3252 case BUILT_IN_MUL_OVERFLOW_P:
3253 /* The last argument of these type-generic builtins
3254 should not be promoted. */
3255 type_generic_overflow_p = true;
3256 break;
3257
8ce94e44 3258 default:
8ce94e44
JM
3259 break;
3260 }
3261 }
3262
400fbf9f 3263 /* Scan the given expressions and types, producing individual
bbbbb16a 3264 converted arguments. */
400fbf9f 3265
bbbbb16a 3266 for (typetail = typelist, parmnum = 0;
9771b263 3267 values && values->iterate (parmnum, &val);
bbbbb16a 3268 ++parmnum)
400fbf9f 3269 {
b3694847 3270 tree type = typetail ? TREE_VALUE (typetail) : 0;
8ce94e44 3271 tree valtype = TREE_TYPE (val);
03dafa61
JM
3272 tree rname = function;
3273 int argnum = parmnum + 1;
4d3e6fae 3274 const char *invalid_func_diag;
8ce94e44 3275 bool excess_precision = false;
928c19bb 3276 bool npc;
bbbbb16a 3277 tree parmval;
5c1bc275
MP
3278 /* Some __atomic_* builtins have additional hidden argument at
3279 position 0. */
3280 location_t ploc
3281 = !arg_loc.is_empty () && values->length () == arg_loc.length ()
3282 ? expansion_point_location_if_in_system_header (arg_loc[parmnum])
3283 : input_location;
400fbf9f
JW
3284
3285 if (type == void_type_node)
3286 {
19dc6d01 3287 if (selector)
68fca595 3288 error_at (loc, "too many arguments to method %qE", selector);
19dc6d01 3289 else
68fca595 3290 error_at (loc, "too many arguments to function %qE", function);
ae52741c 3291 inform_declaration (fundecl);
d38f7dce 3292 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3293 }
3294
03dafa61
JM
3295 if (selector && argnum > 2)
3296 {
3297 rname = selector;
3298 argnum -= 2;
3299 }
3300
928c19bb 3301 npc = null_pointer_constant_p (val);
8ce94e44
JM
3302
3303 /* If there is excess precision and a prototype, convert once to
3304 the required type rather than converting via the semantic
3305 type. Likewise without a prototype a float value represented
3306 as long double should be converted once to double. But for
3307 type-generic classification functions excess precision must
3308 be removed here. */
3309 if (TREE_CODE (val) == EXCESS_PRECISION_EXPR
3310 && (type || !type_generic || !type_generic_remove_excess_precision))
3311 {
3312 val = TREE_OPERAND (val, 0);
3313 excess_precision = true;
3314 }
928c19bb 3315 val = c_fully_fold (val, false, NULL);
ed248cf7 3316 STRIP_TYPE_NOPS (val);
400fbf9f 3317
4f2e1536 3318 val = require_complete_type (ploc, val);
400fbf9f 3319
c65699ef
JM
3320 /* Some floating-point arguments must be promoted to double when
3321 no type is specified by a prototype. This applies to
3322 arguments of type float, and to architecture-specific types
3323 (ARM __fp16), but not to _FloatN or _FloatNx types. */
3324 bool promote_float_arg = false;
3325 if (type == NULL_TREE
3326 && TREE_CODE (valtype) == REAL_TYPE
3327 && (TYPE_PRECISION (valtype)
3328 <= TYPE_PRECISION (double_type_node))
3329 && TYPE_MAIN_VARIANT (valtype) != double_type_node
3330 && TYPE_MAIN_VARIANT (valtype) != long_double_type_node
3331 && !DECIMAL_FLOAT_MODE_P (TYPE_MODE (valtype)))
3332 {
3333 /* Promote this argument, unless it has a _FloatN or
3334 _FloatNx type. */
3335 promote_float_arg = true;
3336 for (int i = 0; i < NUM_FLOATN_NX_TYPES; i++)
3337 if (TYPE_MAIN_VARIANT (valtype) == FLOATN_NX_TYPE_NODE (i))
3338 {
3339 promote_float_arg = false;
3340 break;
3341 }
3342 }
3343
3fa8871b 3344 if (type != NULL_TREE)
400fbf9f
JW
3345 {
3346 /* Formal parm type is specified by a function prototype. */
400fbf9f 3347
20913689 3348 if (type == error_mark_node || !COMPLETE_TYPE_P (type))
400fbf9f 3349 {
5c1bc275
MP
3350 error_at (ploc, "type of formal parameter %d is incomplete",
3351 parmnum + 1);
400fbf9f
JW
3352 parmval = val;
3353 }
3354 else
3355 {
bbbbb16a
ILT
3356 tree origtype;
3357
d45cf215
RS
3358 /* Optionally warn about conversions that
3359 differ from the default conversions. */
05170031 3360 if (warn_traditional_conversion || warn_traditional)
400fbf9f 3361 {
e3a64162 3362 unsigned int formal_prec = TYPE_PRECISION (type);
400fbf9f 3363
aae43c5f 3364 if (INTEGRAL_TYPE_P (type)
8ce94e44 3365 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3366 warning_at (ploc, OPT_Wtraditional_conversion,
3367 "passing argument %d of %qE as integer rather "
3368 "than floating due to prototype",
3369 argnum, rname);
03829ad2 3370 if (INTEGRAL_TYPE_P (type)
8ce94e44 3371 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3372 warning_at (ploc, OPT_Wtraditional_conversion,
3373 "passing argument %d of %qE as integer rather "
3374 "than complex due to prototype",
3375 argnum, rname);
aae43c5f 3376 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3377 && TREE_CODE (valtype) == REAL_TYPE)
5c1bc275
MP
3378 warning_at (ploc, OPT_Wtraditional_conversion,
3379 "passing argument %d of %qE as complex rather "
3380 "than floating due to prototype",
3381 argnum, rname);
400fbf9f 3382 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3383 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3384 warning_at (ploc, OPT_Wtraditional_conversion,
3385 "passing argument %d of %qE as floating rather "
3386 "than integer due to prototype",
3387 argnum, rname);
03829ad2 3388 else if (TREE_CODE (type) == COMPLEX_TYPE
8ce94e44 3389 && INTEGRAL_TYPE_P (valtype))
5c1bc275
MP
3390 warning_at (ploc, OPT_Wtraditional_conversion,
3391 "passing argument %d of %qE as complex rather "
3392 "than integer due to prototype",
3393 argnum, rname);
aae43c5f 3394 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3395 && TREE_CODE (valtype) == COMPLEX_TYPE)
5c1bc275
MP
3396 warning_at (ploc, OPT_Wtraditional_conversion,
3397 "passing argument %d of %qE as floating rather "
3398 "than complex due to prototype",
3399 argnum, rname);
aae43c5f
RK
3400 /* ??? At some point, messages should be written about
3401 conversions between complex types, but that's too messy
3402 to do now. */
d45cf215 3403 else if (TREE_CODE (type) == REAL_TYPE
8ce94e44 3404 && TREE_CODE (valtype) == REAL_TYPE)
d45cf215
RS
3405 {
3406 /* Warn if any argument is passed as `float',
047de90b 3407 since without a prototype it would be `double'. */
9a8ce21f
JG
3408 if (formal_prec == TYPE_PRECISION (float_type_node)
3409 && type != dfloat32_type_node)
5c1bc275
MP
3410 warning_at (ploc, 0,
3411 "passing argument %d of %qE as %<float%> "
3412 "rather than %<double%> due to prototype",
3413 argnum, rname);
9a8ce21f
JG
3414
3415 /* Warn if mismatch between argument and prototype
3416 for decimal float types. Warn of conversions with
3417 binary float types and of precision narrowing due to
3418 prototype. */
8ce94e44 3419 else if (type != valtype
9a8ce21f
JG
3420 && (type == dfloat32_type_node
3421 || type == dfloat64_type_node
c22cacf3 3422 || type == dfloat128_type_node
8ce94e44
JM
3423 || valtype == dfloat32_type_node
3424 || valtype == dfloat64_type_node
3425 || valtype == dfloat128_type_node)
c22cacf3 3426 && (formal_prec
8ce94e44 3427 <= TYPE_PRECISION (valtype)
9a8ce21f 3428 || (type == dfloat128_type_node
8ce94e44 3429 && (valtype
c22cacf3 3430 != dfloat64_type_node
8ce94e44 3431 && (valtype
9a8ce21f
JG
3432 != dfloat32_type_node)))
3433 || (type == dfloat64_type_node
8ce94e44 3434 && (valtype
9a8ce21f 3435 != dfloat32_type_node))))
5c1bc275
MP
3436 warning_at (ploc, 0,
3437 "passing argument %d of %qE as %qT "
3438 "rather than %qT due to prototype",
3439 argnum, rname, type, valtype);
9a8ce21f 3440
d45cf215 3441 }
3ed56f8a
KG
3442 /* Detect integer changing in width or signedness.
3443 These warnings are only activated with
05170031 3444 -Wtraditional-conversion, not with -Wtraditional. */
61ac5ebe
MP
3445 else if (warn_traditional_conversion
3446 && INTEGRAL_TYPE_P (type)
8ce94e44 3447 && INTEGRAL_TYPE_P (valtype))
400fbf9f 3448 {
d45cf215
RS
3449 tree would_have_been = default_conversion (val);
3450 tree type1 = TREE_TYPE (would_have_been);
3451
61ac5ebe
MP
3452 if (val == error_mark_node)
3453 /* VAL could have been of incomplete type. */;
3454 else if (TREE_CODE (type) == ENUMERAL_TYPE
3455 && (TYPE_MAIN_VARIANT (type)
3456 == TYPE_MAIN_VARIANT (valtype)))
754a4d82
RS
3457 /* No warning if function asks for enum
3458 and the actual arg is that enum type. */
3459 ;
3460 else if (formal_prec != TYPE_PRECISION (type1))
5c1bc275
MP
3461 warning_at (ploc, OPT_Wtraditional_conversion,
3462 "passing argument %d of %qE "
3463 "with different width due to prototype",
3464 argnum, rname);
8df83eae 3465 else if (TYPE_UNSIGNED (type) == TYPE_UNSIGNED (type1))
d45cf215 3466 ;
800cd3b9
RS
3467 /* Don't complain if the formal parameter type
3468 is an enum, because we can't tell now whether
3469 the value was an enum--even the same enum. */
3470 else if (TREE_CODE (type) == ENUMERAL_TYPE)
3471 ;
400fbf9f
JW
3472 else if (TREE_CODE (val) == INTEGER_CST
3473 && int_fits_type_p (val, type))
3474 /* Change in signedness doesn't matter
3475 if a constant value is unaffected. */
3476 ;
ce9895ae
RS
3477 /* If the value is extended from a narrower
3478 unsigned type, it doesn't matter whether we
3479 pass it as signed or unsigned; the value
3480 certainly is the same either way. */
8ce94e44
JM
3481 else if (TYPE_PRECISION (valtype) < TYPE_PRECISION (type)
3482 && TYPE_UNSIGNED (valtype))
ce9895ae 3483 ;
8df83eae 3484 else if (TYPE_UNSIGNED (type))
5c1bc275
MP
3485 warning_at (ploc, OPT_Wtraditional_conversion,
3486 "passing argument %d of %qE "
3487 "as unsigned due to prototype",
3488 argnum, rname);
3ed56f8a 3489 else
5c1bc275
MP
3490 warning_at (ploc, OPT_Wtraditional_conversion,
3491 "passing argument %d of %qE "
3492 "as signed due to prototype",
3493 argnum, rname);
400fbf9f
JW
3494 }
3495 }
3496
8ce94e44
JM
3497 /* Possibly restore an EXCESS_PRECISION_EXPR for the
3498 sake of better warnings from convert_and_check. */
3499 if (excess_precision)
3500 val = build1 (EXCESS_PRECISION_EXPR, valtype, val);
9771b263 3501 origtype = (!origtypes) ? NULL_TREE : (*origtypes)[parmnum];
5c1bc275 3502 parmval = convert_for_assignment (loc, ploc, type,
68fca595
MP
3503 val, origtype, ic_argpass,
3504 npc, fundecl, function,
2ac2f164 3505 parmnum + 1);
2f6e4e97 3506
61f71b34 3507 if (targetm.calls.promote_prototypes (fundecl ? TREE_TYPE (fundecl) : 0)
b6d6aa84 3508 && INTEGRAL_TYPE_P (type)
400fbf9f
JW
3509 && (TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node)))
3510 parmval = default_conversion (parmval);
400fbf9f 3511 }
400fbf9f 3512 }
c65699ef 3513 else if (promote_float_arg)
b5d32c25
KG
3514 {
3515 if (type_generic)
bbbbb16a 3516 parmval = val;
b5d32c25 3517 else
0a0b3574
MM
3518 {
3519 /* Convert `float' to `double'. */
3520 if (warn_double_promotion && !c_inhibit_evaluation_warnings)
5c1bc275
MP
3521 warning_at (ploc, OPT_Wdouble_promotion,
3522 "implicit conversion from %qT to %qT when passing "
3523 "argument to function",
3524 valtype, double_type_node);
0a0b3574
MM
3525 parmval = convert (double_type_node, val);
3526 }
b5d32c25 3527 }
44a845ca
MS
3528 else if ((excess_precision && !type_generic)
3529 || (type_generic_overflow_p && parmnum == 2))
8ce94e44 3530 /* A "double" argument with excess precision being passed
44a845ca
MS
3531 without a prototype or in variable arguments.
3532 The last argument of __builtin_*_overflow_p should not be
3533 promoted. */
bbbbb16a 3534 parmval = convert (valtype, val);
c22cacf3
MS
3535 else if ((invalid_func_diag =
3536 targetm.calls.invalid_arg_for_unprototyped_fn (typelist, fundecl, val)))
4d3e6fae
FJ
3537 {
3538 error (invalid_func_diag);
94a0dd7b 3539 return -1;
4d3e6fae 3540 }
1807ffc1
MS
3541 else if (TREE_CODE (val) == ADDR_EXPR && reject_gcc_builtin (val))
3542 {
3543 return -1;
3544 }
400fbf9f
JW
3545 else
3546 /* Convert `short' and `char' to full-size `int'. */
bbbbb16a
ILT
3547 parmval = default_conversion (val);
3548
9771b263 3549 (*values)[parmnum] = parmval;
06302a02
JJ
3550 if (parmval == error_mark_node)
3551 error_args = true;
400fbf9f
JW
3552
3553 if (typetail)
3554 typetail = TREE_CHAIN (typetail);
3555 }
3556
9771b263 3557 gcc_assert (parmnum == vec_safe_length (values));
94a0dd7b 3558
3fa8871b 3559 if (typetail != NULL_TREE && TREE_VALUE (typetail) != void_type_node)
3789b316 3560 {
68fca595 3561 error_at (loc, "too few arguments to function %qE", function);
6d6efbb3
BI
3562 inform_declaration (fundecl);
3563 return -1;
3789b316 3564 }
400fbf9f 3565
06302a02 3566 return error_args ? -1 : (int) parmnum;
400fbf9f
JW
3567}
3568\f
43f6dfd3
RS
3569/* This is the entry point used by the parser to build unary operators
3570 in the input. CODE, a tree_code, specifies the unary operator, and
3571 ARG is the operand. For unary plus, the C parser currently uses
6a3799eb
AH
3572 CONVERT_EXPR for code.
3573
3574 LOC is the location to use for the tree generated.
3575*/
43f6dfd3
RS
3576
3577struct c_expr
c2255bc4 3578parser_build_unary_op (location_t loc, enum tree_code code, struct c_expr arg)
43f6dfd3
RS
3579{
3580 struct c_expr result;
3581
100d537d 3582 result.original_code = code;
6866c6e8
ILT
3583 result.original_type = NULL;
3584
1807ffc1
MS
3585 if (reject_gcc_builtin (arg.value))
3586 {
3587 result.value = error_mark_node;
3588 }
3589 else
3590 {
e51fbec3 3591 result.value = build_unary_op (loc, code, arg.value, false);
1807ffc1 3592
e9892350 3593 if (TREE_OVERFLOW_P (result.value) && !TREE_OVERFLOW_P (arg.value))
3cd211af 3594 overflow_warning (loc, result.value, arg.value);
1807ffc1 3595 }
59c0753d 3596
ebedc9a3
DM
3597 /* We are typically called when parsing a prefix token at LOC acting on
3598 ARG. Reflect this by updating the source range of the result to
3599 start at LOC and end at the end of ARG. */
3600 set_c_expr_source_range (&result,
3601 loc, arg.get_finish ());
3602
43f6dfd3
RS
3603 return result;
3604}
3605
a9342885
MP
3606/* Returns true if TYPE is a character type, *not* including wchar_t. */
3607
3608static bool
3609char_type_p (tree type)
3610{
3611 return (type == char_type_node
3612 || type == unsigned_char_type_node
3613 || type == signed_char_type_node
3614 || type == char16_type_node
3615 || type == char32_type_node);
3616}
3617
43f6dfd3
RS
3618/* This is the entry point used by the parser to build binary operators
3619 in the input. CODE, a tree_code, specifies the binary operator, and
3620 ARG1 and ARG2 are the operands. In addition to constructing the
3621 expression, we check for operands that were written with other binary
ba47d38d
AH
3622 operators in a way that is likely to confuse the user.
3623
3624 LOCATION is the location of the binary operator. */
edc7c4ec 3625
487a92fe 3626struct c_expr
ba47d38d
AH
3627parser_build_binary_op (location_t location, enum tree_code code,
3628 struct c_expr arg1, struct c_expr arg2)
400fbf9f 3629{
487a92fe 3630 struct c_expr result;
400fbf9f 3631
487a92fe
JM
3632 enum tree_code code1 = arg1.original_code;
3633 enum tree_code code2 = arg2.original_code;
6866c6e8
ILT
3634 tree type1 = (arg1.original_type
3635 ? arg1.original_type
3636 : TREE_TYPE (arg1.value));
3637 tree type2 = (arg2.original_type
3638 ? arg2.original_type
3639 : TREE_TYPE (arg2.value));
400fbf9f 3640
ba47d38d 3641 result.value = build_binary_op (location, code,
30af3a2b 3642 arg1.value, arg2.value, true);
487a92fe 3643 result.original_code = code;
6866c6e8 3644 result.original_type = NULL;
58bf601b 3645
487a92fe 3646 if (TREE_CODE (result.value) == ERROR_MARK)
8a40fef3
DM
3647 {
3648 set_c_expr_source_range (&result,
3649 arg1.get_start (),
3650 arg2.get_finish ());
3651 return result;
3652 }
400fbf9f 3653
ba47d38d
AH
3654 if (location != UNKNOWN_LOCATION)
3655 protected_set_expr_location (result.value, location);
3656
ebedc9a3
DM
3657 set_c_expr_source_range (&result,
3658 arg1.get_start (),
3659 arg2.get_finish ());
3660
400fbf9f 3661 /* Check for cases such as x+y<<z which users are likely
487a92fe 3662 to misinterpret. */
400fbf9f 3663 if (warn_parentheses)
393e8e8b
MP
3664 warn_about_parentheses (location, code, code1, arg1.value, code2,
3665 arg2.value);
001af587 3666
ca409efd 3667 if (warn_logical_op)
393e8e8b 3668 warn_logical_operator (location, code, TREE_TYPE (result.value),
ca409efd 3669 code1, arg1.value, code2, arg2.value);
63a08740 3670
05b28fd6 3671 if (warn_tautological_compare)
433068cc
MP
3672 {
3673 tree lhs = arg1.value;
3674 tree rhs = arg2.value;
3675 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
3676 {
3677 if (C_MAYBE_CONST_EXPR_PRE (lhs) != NULL_TREE
3678 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (lhs)))
3679 lhs = NULL_TREE;
3680 else
3681 lhs = C_MAYBE_CONST_EXPR_EXPR (lhs);
3682 }
3683 if (TREE_CODE (rhs) == C_MAYBE_CONST_EXPR)
3684 {
3685 if (C_MAYBE_CONST_EXPR_PRE (rhs) != NULL_TREE
3686 && TREE_SIDE_EFFECTS (C_MAYBE_CONST_EXPR_PRE (rhs)))
3687 rhs = NULL_TREE;
3688 else
3689 rhs = C_MAYBE_CONST_EXPR_EXPR (rhs);
3690 }
3691 if (lhs != NULL_TREE && rhs != NULL_TREE)
3692 warn_tautological_cmp (location, code, lhs, rhs);
3693 }
05b28fd6 3694
742938c9 3695 if (warn_logical_not_paren
7ccb1a11 3696 && TREE_CODE_CLASS (code) == tcc_comparison
47c2554f 3697 && code1 == TRUTH_NOT_EXPR
01177669
JJ
3698 && code2 != TRUTH_NOT_EXPR
3699 /* Avoid warning for !!x == y. */
3700 && (TREE_CODE (arg1.value) != NE_EXPR
3701 || !integer_zerop (TREE_OPERAND (arg1.value, 1))))
3702 {
3703 /* Avoid warning for !b == y where b has _Bool type. */
3704 tree t = integer_zero_node;
3705 if (TREE_CODE (arg1.value) == EQ_EXPR
3706 && integer_zerop (TREE_OPERAND (arg1.value, 1))
3707 && TREE_TYPE (TREE_OPERAND (arg1.value, 0)) == integer_type_node)
3708 {
3709 t = TREE_OPERAND (arg1.value, 0);
3710 do
3711 {
3712 if (TREE_TYPE (t) != integer_type_node)
3713 break;
3714 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
3715 t = C_MAYBE_CONST_EXPR_EXPR (t);
3716 else if (CONVERT_EXPR_P (t))
3717 t = TREE_OPERAND (t, 0);
3718 else
3719 break;
3720 }
3721 while (1);
3722 }
3723 if (TREE_CODE (TREE_TYPE (t)) != BOOLEAN_TYPE)
ebef225f 3724 warn_logical_not_parentheses (location, code, arg1.value, arg2.value);
01177669 3725 }
742938c9 3726
e994a705
RS
3727 /* Warn about comparisons against string literals, with the exception
3728 of testing for equality or inequality of a string literal with NULL. */
3729 if (code == EQ_EXPR || code == NE_EXPR)
3730 {
66756373
JJ
3731 if ((code1 == STRING_CST
3732 && !integer_zerop (tree_strip_nop_conversions (arg2.value)))
3733 || (code2 == STRING_CST
3734 && !integer_zerop (tree_strip_nop_conversions (arg1.value))))
c2255bc4
AH
3735 warning_at (location, OPT_Waddress,
3736 "comparison with string literal results in unspecified behavior");
a9342885
MP
3737 /* Warn for ptr == '\0', it's likely that it should've been ptr[0]. */
3738 if (POINTER_TYPE_P (type1)
097f82ec
DM
3739 && null_pointer_constant_p (arg2.value)
3740 && char_type_p (type2))
3741 {
3742 auto_diagnostic_group d;
3743 if (warning_at (location, OPT_Wpointer_compare,
3744 "comparison between pointer and zero character "
3745 "constant"))
3746 inform (arg1.get_start (),
3747 "did you mean to dereference the pointer?");
3748 }
a9342885
MP
3749 else if (POINTER_TYPE_P (type2)
3750 && null_pointer_constant_p (arg1.value)
097f82ec
DM
3751 && char_type_p (type1))
3752 {
3753 auto_diagnostic_group d;
3754 if (warning_at (location, OPT_Wpointer_compare,
3755 "comparison between pointer and zero character "
3756 "constant"))
3757 inform (arg2.get_start (),
3758 "did you mean to dereference the pointer?");
3759 }
e994a705
RS
3760 }
3761 else if (TREE_CODE_CLASS (code) == tcc_comparison
3762 && (code1 == STRING_CST || code2 == STRING_CST))
c2255bc4
AH
3763 warning_at (location, OPT_Waddress,
3764 "comparison with string literal results in unspecified behavior");
e994a705 3765
b8698a0f
L
3766 if (TREE_OVERFLOW_P (result.value)
3767 && !TREE_OVERFLOW_P (arg1.value)
59c0753d 3768 && !TREE_OVERFLOW_P (arg2.value))
c2255bc4 3769 overflow_warning (location, result.value);
400fbf9f 3770
6866c6e8
ILT
3771 /* Warn about comparisons of different enum types. */
3772 if (warn_enum_compare
3773 && TREE_CODE_CLASS (code) == tcc_comparison
3774 && TREE_CODE (type1) == ENUMERAL_TYPE
3775 && TREE_CODE (type2) == ENUMERAL_TYPE
3776 && TYPE_MAIN_VARIANT (type1) != TYPE_MAIN_VARIANT (type2))
3777 warning_at (location, OPT_Wenum_compare,
3778 "comparison between %qT and %qT",
3779 type1, type2);
3780
400fbf9f
JW
3781 return result;
3782}
3e4093b6 3783\f
3e4093b6 3784/* Return a tree for the difference of pointers OP0 and OP1.
05abad4c
ML
3785 The resulting tree has type ptrdiff_t. If POINTER_SUBTRACT sanitization is
3786 enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
293c9fdd 3787
3e4093b6 3788static tree
05abad4c 3789pointer_diff (location_t loc, tree op0, tree op1, tree *instrument_expr)
3e4093b6 3790{
3e4093b6 3791 tree restype = ptrdiff_type_node;
36c5e70a 3792 tree result, inttype;
400fbf9f 3793
36c5e70a
BE
3794 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op0)));
3795 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (op1)));
3e4093b6 3796 tree target_type = TREE_TYPE (TREE_TYPE (op0));
3e4093b6 3797 tree orig_op1 = op1;
400fbf9f 3798
36c5e70a
BE
3799 /* If the operands point into different address spaces, we need to
3800 explicitly convert them to pointers into the common address space
3801 before we can subtract the numerical address values. */
3802 if (as0 != as1)
3803 {
3804 addr_space_t as_common;
3805 tree common_type;
3806
3807 /* Determine the common superset address space. This is guaranteed
3808 to exist because the caller verified that comp_target_types
3809 returned non-zero. */
3810 if (!addr_space_superset (as0, as1, &as_common))
3811 gcc_unreachable ();
3812
3813 common_type = common_pointer_type (TREE_TYPE (op0), TREE_TYPE (op1));
3814 op0 = convert (common_type, op0);
3815 op1 = convert (common_type, op1);
3816 }
3817
1af4ebf5 3818 /* Determine integer type result of the subtraction. This will usually
36c5e70a
BE
3819 be the same as the result type (ptrdiff_t), but may need to be a wider
3820 type if pointers for the address space are wider than ptrdiff_t. */
3821 if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
647d4b75 3822 inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
36c5e70a
BE
3823 else
3824 inttype = restype;
3825
fcf73884 3826 if (TREE_CODE (target_type) == VOID_TYPE)
44d90fe1 3827 pedwarn (loc, OPT_Wpointer_arith,
fcf73884
MLI
3828 "pointer of type %<void *%> used in subtraction");
3829 if (TREE_CODE (target_type) == FUNCTION_TYPE)
44d90fe1 3830 pedwarn (loc, OPT_Wpointer_arith,
fcf73884 3831 "pointer to a function used in subtraction");
400fbf9f 3832
05abad4c
ML
3833 if (sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
3834 {
3835 gcc_assert (current_function_decl != NULL_TREE);
3836
3837 op0 = save_expr (op0);
3838 op1 = save_expr (op1);
3839
3840 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
3841 *instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
3842 }
3843
1af4ebf5
MG
3844 /* First do the subtraction, then build the divide operator
3845 and only convert at the very end.
3846 Do not do default conversions in case restype is a short type. */
3847
3848 /* POINTER_DIFF_EXPR requires a signed integer type of the same size as
3849 pointers. If some platform cannot provide that, or has a larger
3850 ptrdiff_type to support differences larger than half the address
3851 space, cast the pointers to some larger integer type and do the
3852 computations in that type. */
3853 if (TYPE_PRECISION (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
05abad4c
ML
3854 op0 = build_binary_op (loc, MINUS_EXPR, convert (inttype, op0),
3855 convert (inttype, op1), false);
1af4ebf5 3856 else
487f2f61
JJ
3857 {
3858 /* Cast away qualifiers. */
3859 op0 = convert (c_common_type (TREE_TYPE (op0), TREE_TYPE (op0)), op0);
3860 op1 = convert (c_common_type (TREE_TYPE (op1), TREE_TYPE (op1)), op1);
3861 op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
3862 }
400fbf9f 3863
3e4093b6
RS
3864 /* This generates an error if op1 is pointer to incomplete type. */
3865 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (TREE_TYPE (orig_op1))))
db3927fb 3866 error_at (loc, "arithmetic on pointer to an incomplete type");
400fbf9f 3867
3e4093b6 3868 op1 = c_size_in_bytes (target_type);
400fbf9f 3869
f04dda30
MP
3870 if (pointer_to_zero_sized_aggr_p (TREE_TYPE (orig_op1)))
3871 error_at (loc, "arithmetic on pointer to an empty aggregate");
3872
3e4093b6 3873 /* Divide by the size, in easiest possible way. */
36c5e70a
BE
3874 result = fold_build2_loc (loc, EXACT_DIV_EXPR, inttype,
3875 op0, convert (inttype, op1));
3876
3877 /* Convert to final result type if necessary. */
3878 return convert (restype, result);
3e4093b6
RS
3879}
3880\f
2fe0a208
MP
3881/* Expand atomic compound assignments into an appropriate sequence as
3882 specified by the C11 standard section 6.5.16.2.
3883
267bac10
JM
3884 _Atomic T1 E1
3885 T2 E2
3886 E1 op= E2
3887
3888 This sequence is used for all types for which these operations are
3889 supported.
3890
3891 In addition, built-in versions of the 'fe' prefixed routines may
3892 need to be invoked for floating point (real, complex or vector) when
3893 floating-point exceptions are supported. See 6.5.16.2 footnote 113.
3894
3895 T1 newval;
3896 T1 old;
3897 T1 *addr
3898 T2 val
3899 fenv_t fenv
3900
3901 addr = &E1;
3902 val = (E2);
3903 __atomic_load (addr, &old, SEQ_CST);
3904 feholdexcept (&fenv);
3905loop:
3906 newval = old op val;
3907 if (__atomic_compare_exchange_strong (addr, &old, &newval, SEQ_CST,
3908 SEQ_CST))
3909 goto done;
3910 feclearexcept (FE_ALL_EXCEPT);
3911 goto loop:
3912done:
3913 feupdateenv (&fenv);
3914
2fe0a208
MP
3915 The compiler will issue the __atomic_fetch_* built-in when possible,
3916 otherwise it will generate the generic form of the atomic operations.
3917 This requires temp(s) and has their address taken. The atomic processing
3918 is smart enough to figure out when the size of an object can utilize
3919 a lock-free version, and convert the built-in call to the appropriate
3920 lock-free routine. The optimizers will then dispose of any temps that
3921 are no longer required, and lock-free implementations are utilized as
3922 long as there is target support for the required size.
267bac10
JM
3923
3924 If the operator is NOP_EXPR, then this is a simple assignment, and
3925 an __atomic_store is issued to perform the assignment rather than
2fe0a208 3926 the above loop. */
267bac10
JM
3927
3928/* Build an atomic assignment at LOC, expanding into the proper
3929 sequence to store LHS MODIFYCODE= RHS. Return a value representing
2fe0a208 3930 the result of the operation, unless RETURN_OLD_P, in which case
267bac10
JM
3931 return the old value of LHS (this is only for postincrement and
3932 postdecrement). */
2fe0a208 3933
267bac10
JM
3934static tree
3935build_atomic_assign (location_t loc, tree lhs, enum tree_code modifycode,
3936 tree rhs, bool return_old_p)
3937{
3938 tree fndecl, func_call;
3939 vec<tree, va_gc> *params;
3940 tree val, nonatomic_lhs_type, nonatomic_rhs_type, newval, newval_addr;
3941 tree old, old_addr;
3942 tree compound_stmt;
3943 tree stmt, goto_stmt;
3944 tree loop_label, loop_decl, done_label, done_decl;
3945
3946 tree lhs_type = TREE_TYPE (lhs);
e51fbec3 3947 tree lhs_addr = build_unary_op (loc, ADDR_EXPR, lhs, false);
267bac10 3948 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
e035be33
JM
3949 tree rhs_semantic_type = TREE_TYPE (rhs);
3950 tree nonatomic_rhs_semantic_type;
3951 tree rhs_type;
267bac10
JM
3952
3953 gcc_assert (TYPE_ATOMIC (lhs_type));
3954
3955 if (return_old_p)
3956 gcc_assert (modifycode == PLUS_EXPR || modifycode == MINUS_EXPR);
3957
3958 /* Allocate enough vector items for a compare_exchange. */
3959 vec_alloc (params, 6);
3960
3961 /* Create a compound statement to hold the sequence of statements
3962 with a loop. */
3963 compound_stmt = c_begin_compound_stmt (false);
3964
e035be33
JM
3965 /* Remove any excess precision (which is only present here in the
3966 case of compound assignments). */
3967 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
3968 {
3969 gcc_assert (modifycode != NOP_EXPR);
3970 rhs = TREE_OPERAND (rhs, 0);
3971 }
3972 rhs_type = TREE_TYPE (rhs);
3973
267bac10
JM
3974 /* Fold the RHS if it hasn't already been folded. */
3975 if (modifycode != NOP_EXPR)
3976 rhs = c_fully_fold (rhs, false, NULL);
3977
3978 /* Remove the qualifiers for the rest of the expressions and create
3979 the VAL temp variable to hold the RHS. */
3980 nonatomic_lhs_type = build_qualified_type (lhs_type, TYPE_UNQUALIFIED);
3981 nonatomic_rhs_type = build_qualified_type (rhs_type, TYPE_UNQUALIFIED);
e035be33
JM
3982 nonatomic_rhs_semantic_type = build_qualified_type (rhs_semantic_type,
3983 TYPE_UNQUALIFIED);
5c4abbb8 3984 val = create_tmp_var_raw (nonatomic_rhs_type);
267bac10 3985 TREE_ADDRESSABLE (val) = 1;
cc28fc7f 3986 TREE_NO_WARNING (val) = 1;
5c4abbb8
MP
3987 rhs = build4 (TARGET_EXPR, nonatomic_rhs_type, val, rhs, NULL_TREE,
3988 NULL_TREE);
267bac10
JM
3989 SET_EXPR_LOCATION (rhs, loc);
3990 add_stmt (rhs);
3991
3992 /* NOP_EXPR indicates it's a straight store of the RHS. Simply issue
3993 an atomic_store. */
3994 if (modifycode == NOP_EXPR)
3995 {
3996 /* Build __atomic_store (&lhs, &val, SEQ_CST) */
e51fbec3 3997 rhs = build_unary_op (loc, ADDR_EXPR, val, false);
267bac10
JM
3998 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
3999 params->quick_push (lhs_addr);
4000 params->quick_push (rhs);
4001 params->quick_push (seq_cst);
8edbfaa6 4002 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
4003 add_stmt (func_call);
4004
4005 /* Finish the compound statement. */
4006 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4007
4008 /* VAL is the value which was stored, return a COMPOUND_STMT of
4009 the statement and that value. */
4010 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, val);
4011 }
4012
2fe0a208
MP
4013 /* Attempt to implement the atomic operation as an __atomic_fetch_* or
4014 __atomic_*_fetch built-in rather than a CAS loop. atomic_bool type
4015 isn't applicable for such builtins. ??? Do we want to handle enums? */
4016 if ((TREE_CODE (lhs_type) == INTEGER_TYPE || POINTER_TYPE_P (lhs_type))
4017 && TREE_CODE (rhs_type) == INTEGER_TYPE)
4018 {
4019 built_in_function fncode;
4020 switch (modifycode)
4021 {
4022 case PLUS_EXPR:
4023 case POINTER_PLUS_EXPR:
4024 fncode = (return_old_p
4025 ? BUILT_IN_ATOMIC_FETCH_ADD_N
4026 : BUILT_IN_ATOMIC_ADD_FETCH_N);
4027 break;
4028 case MINUS_EXPR:
4029 fncode = (return_old_p
4030 ? BUILT_IN_ATOMIC_FETCH_SUB_N
4031 : BUILT_IN_ATOMIC_SUB_FETCH_N);
4032 break;
4033 case BIT_AND_EXPR:
4034 fncode = (return_old_p
4035 ? BUILT_IN_ATOMIC_FETCH_AND_N
4036 : BUILT_IN_ATOMIC_AND_FETCH_N);
4037 break;
4038 case BIT_IOR_EXPR:
4039 fncode = (return_old_p
4040 ? BUILT_IN_ATOMIC_FETCH_OR_N
4041 : BUILT_IN_ATOMIC_OR_FETCH_N);
4042 break;
4043 case BIT_XOR_EXPR:
4044 fncode = (return_old_p
4045 ? BUILT_IN_ATOMIC_FETCH_XOR_N
4046 : BUILT_IN_ATOMIC_XOR_FETCH_N);
4047 break;
4048 default:
4049 goto cas_loop;
4050 }
4051
4052 /* We can only use "_1" through "_16" variants of the atomic fetch
4053 built-ins. */
4054 unsigned HOST_WIDE_INT size = tree_to_uhwi (TYPE_SIZE_UNIT (lhs_type));
4055 if (size != 1 && size != 2 && size != 4 && size != 8 && size != 16)
4056 goto cas_loop;
4057
4058 /* If this is a pointer type, we need to multiply by the size of
4059 the pointer target type. */
4060 if (POINTER_TYPE_P (lhs_type))
4061 {
4062 if (!COMPLETE_TYPE_P (TREE_TYPE (lhs_type))
4063 /* ??? This would introduce -Wdiscarded-qualifiers
4064 warning: __atomic_fetch_* expect volatile void *
4065 type as the first argument. (Assignments between
4066 atomic and non-atomic objects are OK.) */
4067 || TYPE_RESTRICT (lhs_type))
4068 goto cas_loop;
4069 tree sz = TYPE_SIZE_UNIT (TREE_TYPE (lhs_type));
4070 rhs = fold_build2_loc (loc, MULT_EXPR, ptrdiff_type_node,
4071 convert (ptrdiff_type_node, rhs),
4072 convert (ptrdiff_type_node, sz));
4073 }
4074
4075 /* Build __atomic_fetch_* (&lhs, &val, SEQ_CST), or
4076 __atomic_*_fetch (&lhs, &val, SEQ_CST). */
4077 fndecl = builtin_decl_explicit (fncode);
4078 params->quick_push (lhs_addr);
4079 params->quick_push (rhs);
4080 params->quick_push (seq_cst);
4081 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
4082
4083 newval = create_tmp_var_raw (nonatomic_lhs_type);
4084 TREE_ADDRESSABLE (newval) = 1;
4085 TREE_NO_WARNING (newval) = 1;
4086 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, func_call,
4087 NULL_TREE, NULL_TREE);
4088 SET_EXPR_LOCATION (rhs, loc);
4089 add_stmt (rhs);
4090
4091 /* Finish the compound statement. */
4092 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4093
4094 /* NEWVAL is the value which was stored, return a COMPOUND_STMT of
4095 the statement and that value. */
4096 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt, newval);
4097 }
4098
4099cas_loop:
267bac10 4100 /* Create the variables and labels required for the op= form. */
5c4abbb8 4101 old = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4102 old_addr = build_unary_op (loc, ADDR_EXPR, old, false);
cc28fc7f
MP
4103 TREE_ADDRESSABLE (old) = 1;
4104 TREE_NO_WARNING (old) = 1;
267bac10 4105
5c4abbb8 4106 newval = create_tmp_var_raw (nonatomic_lhs_type);
e51fbec3 4107 newval_addr = build_unary_op (loc, ADDR_EXPR, newval, false);
267bac10 4108 TREE_ADDRESSABLE (newval) = 1;
2994fb91 4109 TREE_NO_WARNING (newval) = 1;
267bac10
JM
4110
4111 loop_decl = create_artificial_label (loc);
4112 loop_label = build1 (LABEL_EXPR, void_type_node, loop_decl);
4113
4114 done_decl = create_artificial_label (loc);
4115 done_label = build1 (LABEL_EXPR, void_type_node, done_decl);
4116
4117 /* __atomic_load (addr, &old, SEQ_CST). */
4118 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
4119 params->quick_push (lhs_addr);
4120 params->quick_push (old_addr);
4121 params->quick_push (seq_cst);
8edbfaa6 4122 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
5c4abbb8
MP
4123 old = build4 (TARGET_EXPR, nonatomic_lhs_type, old, func_call, NULL_TREE,
4124 NULL_TREE);
4125 add_stmt (old);
267bac10
JM
4126 params->truncate (0);
4127
4128 /* Create the expressions for floating-point environment
4129 manipulation, if required. */
4130 bool need_fenv = (flag_trapping_math
4131 && (FLOAT_TYPE_P (lhs_type) || FLOAT_TYPE_P (rhs_type)));
4132 tree hold_call = NULL_TREE, clear_call = NULL_TREE, update_call = NULL_TREE;
4133 if (need_fenv)
4134 targetm.atomic_assign_expand_fenv (&hold_call, &clear_call, &update_call);
4135
4136 if (hold_call)
4137 add_stmt (hold_call);
4138
4139 /* loop: */
4140 add_stmt (loop_label);
4141
4142 /* newval = old + val; */
e035be33
JM
4143 if (rhs_type != rhs_semantic_type)
4144 val = build1 (EXCESS_PRECISION_EXPR, nonatomic_rhs_semantic_type, val);
30af3a2b 4145 rhs = build_binary_op (loc, modifycode, old, val, true);
e035be33
JM
4146 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
4147 {
4148 tree eptype = TREE_TYPE (rhs);
4149 rhs = c_fully_fold (TREE_OPERAND (rhs, 0), false, NULL);
4150 rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
4151 }
4152 else
4153 rhs = c_fully_fold (rhs, false, NULL);
68fca595
MP
4154 rhs = convert_for_assignment (loc, UNKNOWN_LOCATION, nonatomic_lhs_type,
4155 rhs, NULL_TREE, ic_assign, false, NULL_TREE,
267bac10
JM
4156 NULL_TREE, 0);
4157 if (rhs != error_mark_node)
4158 {
5c4abbb8
MP
4159 rhs = build4 (TARGET_EXPR, nonatomic_lhs_type, newval, rhs, NULL_TREE,
4160 NULL_TREE);
267bac10
JM
4161 SET_EXPR_LOCATION (rhs, loc);
4162 add_stmt (rhs);
4163 }
4164
4165 /* if (__atomic_compare_exchange (addr, &old, &new, false, SEQ_CST, SEQ_CST))
4166 goto done; */
4167 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_COMPARE_EXCHANGE);
4168 params->quick_push (lhs_addr);
4169 params->quick_push (old_addr);
4170 params->quick_push (newval_addr);
4171 params->quick_push (integer_zero_node);
4172 params->quick_push (seq_cst);
4173 params->quick_push (seq_cst);
8edbfaa6 4174 func_call = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
267bac10
JM
4175
4176 goto_stmt = build1 (GOTO_EXPR, void_type_node, done_decl);
4177 SET_EXPR_LOCATION (goto_stmt, loc);
4178
4179 stmt = build3 (COND_EXPR, void_type_node, func_call, goto_stmt, NULL_TREE);
4180 SET_EXPR_LOCATION (stmt, loc);
4181 add_stmt (stmt);
5c4abbb8 4182
267bac10
JM
4183 if (clear_call)
4184 add_stmt (clear_call);
4185
4186 /* goto loop; */
4187 goto_stmt = build1 (GOTO_EXPR, void_type_node, loop_decl);
4188 SET_EXPR_LOCATION (goto_stmt, loc);
4189 add_stmt (goto_stmt);
5c4abbb8 4190
267bac10
JM
4191 /* done: */
4192 add_stmt (done_label);
4193
4194 if (update_call)
4195 add_stmt (update_call);
4196
4197 /* Finish the compound statement. */
4198 compound_stmt = c_end_compound_stmt (loc, compound_stmt, false);
4199
4200 /* NEWVAL is the value that was successfully stored, return a
4201 COMPOUND_EXPR of the statement and the appropriate value. */
4202 return build2 (COMPOUND_EXPR, nonatomic_lhs_type, compound_stmt,
4203 return_old_p ? old : newval);
4204}
4205
3e4093b6
RS
4206/* Construct and perhaps optimize a tree representation
4207 for a unary operation. CODE, a tree_code, specifies the operation
4208 and XARG is the operand.
e51fbec3
MP
4209 For any CODE other than ADDR_EXPR, NOCONVERT suppresses the default
4210 promotions (such as from short to int).
4211 For ADDR_EXPR, the default promotions are not applied; NOCONVERT allows
4212 non-lvalues; this is only used to handle conversion of non-lvalue arrays
4213 to pointers in C99.
c9f9eb5d
AH
4214
4215 LOCATION is the location of the operator. */
400fbf9f 4216
3e4093b6 4217tree
4c13ba17 4218build_unary_op (location_t location, enum tree_code code, tree xarg,
e51fbec3 4219 bool noconvert)
3e4093b6
RS
4220{
4221 /* No default_conversion here. It causes trouble for ADDR_EXPR. */
4222 tree arg = xarg;
3fa8871b 4223 tree argtype = NULL_TREE;
808d6eaa 4224 enum tree_code typecode;
3e4093b6 4225 tree val;
c9f9eb5d 4226 tree ret = error_mark_node;
8ce94e44 4227 tree eptype = NULL_TREE;
4de67c26 4228 const char *invalid_op_diag;
928c19bb
JM
4229 bool int_operands;
4230
4231 int_operands = EXPR_INT_CONST_OPERANDS (xarg);
4d84fe7c
JM
4232 if (int_operands)
4233 arg = remove_c_maybe_const_expr (arg);
400fbf9f 4234
808d6eaa 4235 if (code != ADDR_EXPR)
4f2e1536 4236 arg = require_complete_type (location, arg);
808d6eaa
JM
4237
4238 typecode = TREE_CODE (TREE_TYPE (arg));
3e4093b6
RS
4239 if (typecode == ERROR_MARK)
4240 return error_mark_node;
4241 if (typecode == ENUMERAL_TYPE || typecode == BOOLEAN_TYPE)
4242 typecode = INTEGER_TYPE;
6c36d76b 4243
4de67c26
JM
4244 if ((invalid_op_diag
4245 = targetm.invalid_unary_op (code, TREE_TYPE (xarg))))
4246 {
c9f9eb5d 4247 error_at (location, invalid_op_diag);
4de67c26
JM
4248 return error_mark_node;
4249 }
4250
8ce94e44
JM
4251 if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
4252 {
4253 eptype = TREE_TYPE (arg);
4254 arg = TREE_OPERAND (arg, 0);
4255 }
4256
3e4093b6
RS
4257 switch (code)
4258 {
4259 case CONVERT_EXPR:
4260 /* This is used for unary plus, because a CONVERT_EXPR
4261 is enough to prevent anybody from looking inside for
4262 associativity, but won't generate any code. */
4263 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4264 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
8a2cee38 4265 || typecode == VECTOR_TYPE))
400fbf9f 4266 {
c9f9eb5d 4267 error_at (location, "wrong type argument to unary plus");
3e4093b6 4268 return error_mark_node;
400fbf9f 4269 }
3e4093b6
RS
4270 else if (!noconvert)
4271 arg = default_conversion (arg);
db3927fb 4272 arg = non_lvalue_loc (location, arg);
400fbf9f
JW
4273 break;
4274
3e4093b6
RS
4275 case NEGATE_EXPR:
4276 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
ab22c1fa 4277 || typecode == FIXED_POINT_TYPE || typecode == COMPLEX_TYPE
3e4093b6
RS
4278 || typecode == VECTOR_TYPE))
4279 {
c9f9eb5d 4280 error_at (location, "wrong type argument to unary minus");
3e4093b6
RS
4281 return error_mark_node;
4282 }
4283 else if (!noconvert)
4284 arg = default_conversion (arg);
400fbf9f
JW
4285 break;
4286
3e4093b6 4287 case BIT_NOT_EXPR:
462643f0
AP
4288 /* ~ works on integer types and non float vectors. */
4289 if (typecode == INTEGER_TYPE
4290 || (typecode == VECTOR_TYPE
4291 && !VECTOR_FLOAT_TYPE_P (TREE_TYPE (arg))))
03d5b1f5 4292 {
9a2300e9
MP
4293 tree e = arg;
4294
4295 /* Warn if the expression has boolean value. */
4296 while (TREE_CODE (e) == COMPOUND_EXPR)
4297 e = TREE_OPERAND (e, 1);
4298
4299 if ((TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
097f82ec 4300 || truth_value_p (TREE_CODE (e))))
9a2300e9 4301 {
097f82ec
DM
4302 auto_diagnostic_group d;
4303 if (warning_at (location, OPT_Wbool_operation,
4304 "%<~%> on a boolean expression"))
4305 {
4306 gcc_rich_location richloc (location);
4307 richloc.add_fixit_insert_before (location, "!");
4308 inform (&richloc, "did you mean to use logical not?");
4309 }
9a2300e9 4310 }
3e4093b6
RS
4311 if (!noconvert)
4312 arg = default_conversion (arg);
03d5b1f5 4313 }
3e4093b6 4314 else if (typecode == COMPLEX_TYPE)
400fbf9f 4315 {
3e4093b6 4316 code = CONJ_EXPR;
c1771a20 4317 pedwarn (location, OPT_Wpedantic,
fcf73884 4318 "ISO C does not support %<~%> for complex conjugation");
3e4093b6
RS
4319 if (!noconvert)
4320 arg = default_conversion (arg);
4321 }
4322 else
4323 {
c9f9eb5d 4324 error_at (location, "wrong type argument to bit-complement");
3e4093b6 4325 return error_mark_node;
400fbf9f
JW
4326 }
4327 break;
4328
3e4093b6 4329 case ABS_EXPR:
11017cc7 4330 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE))
400fbf9f 4331 {
c9f9eb5d 4332 error_at (location, "wrong type argument to abs");
3e4093b6 4333 return error_mark_node;
400fbf9f 4334 }
3e4093b6
RS
4335 else if (!noconvert)
4336 arg = default_conversion (arg);
400fbf9f
JW
4337 break;
4338
e197e64e
KV
4339 case ABSU_EXPR:
4340 if (!(typecode == INTEGER_TYPE))
4341 {
4342 error_at (location, "wrong type argument to absu");
4343 return error_mark_node;
4344 }
4345 else if (!noconvert)
4346 arg = default_conversion (arg);
4347 break;
4348
3e4093b6
RS
4349 case CONJ_EXPR:
4350 /* Conjugating a real value is a no-op, but allow it anyway. */
4351 if (!(typecode == INTEGER_TYPE || typecode == REAL_TYPE
4352 || typecode == COMPLEX_TYPE))
400fbf9f 4353 {
c9f9eb5d 4354 error_at (location, "wrong type argument to conjugation");
3e4093b6 4355 return error_mark_node;
400fbf9f 4356 }
3e4093b6
RS
4357 else if (!noconvert)
4358 arg = default_conversion (arg);
400fbf9f
JW
4359 break;
4360
3e4093b6 4361 case TRUTH_NOT_EXPR:
ab22c1fa 4362 if (typecode != INTEGER_TYPE && typecode != FIXED_POINT_TYPE
3e4093b6 4363 && typecode != REAL_TYPE && typecode != POINTER_TYPE
46bdb9cf 4364 && typecode != COMPLEX_TYPE)
400fbf9f 4365 {
c9f9eb5d
AH
4366 error_at (location,
4367 "wrong type argument to unary exclamation mark");
3e4093b6 4368 return error_mark_node;
400fbf9f 4369 }
a27d595d
JM
4370 if (int_operands)
4371 {
4372 arg = c_objc_common_truthvalue_conversion (location, xarg);
4373 arg = remove_c_maybe_const_expr (arg);
4374 }
4375 else
4376 arg = c_objc_common_truthvalue_conversion (location, arg);
db3927fb 4377 ret = invert_truthvalue_loc (location, arg);
ca80e52b
EB
4378 /* If the TRUTH_NOT_EXPR has been folded, reset the location. */
4379 if (EXPR_P (ret) && EXPR_HAS_LOCATION (ret))
4380 location = EXPR_LOCATION (ret);
c9f9eb5d 4381 goto return_build_unary_op;
3e4093b6 4382
3e4093b6 4383 case REALPART_EXPR:
3e4093b6 4384 case IMAGPART_EXPR:
fb52b50a
NF
4385 ret = build_real_imag_expr (location, code, arg);
4386 if (ret == error_mark_node)
4387 return error_mark_node;
8ce94e44
JM
4388 if (eptype && TREE_CODE (eptype) == COMPLEX_TYPE)
4389 eptype = TREE_TYPE (eptype);
c9f9eb5d 4390 goto return_build_unary_op;
3e4093b6
RS
4391
4392 case PREINCREMENT_EXPR:
4393 case POSTINCREMENT_EXPR:
4394 case PREDECREMENT_EXPR:
4395 case POSTDECREMENT_EXPR:
3e4093b6 4396
928c19bb
JM
4397 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4398 {
4399 tree inner = build_unary_op (location, code,
4c13ba17
MP
4400 C_MAYBE_CONST_EXPR_EXPR (arg),
4401 noconvert);
928c19bb
JM
4402 if (inner == error_mark_node)
4403 return error_mark_node;
4404 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4405 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4406 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4407 C_MAYBE_CONST_EXPR_NON_CONST (ret) = 1;
4408 goto return_build_unary_op;
4409 }
4410
925e8657
NP
4411 /* Complain about anything that is not a true lvalue. In
4412 Objective-C, skip this check for property_refs. */
f90e8e2e 4413 if (!objc_is_property_ref (arg)
7bd11157
TT
4414 && !lvalue_or_else (location,
4415 arg, ((code == PREINCREMENT_EXPR
925e8657
NP
4416 || code == POSTINCREMENT_EXPR)
4417 ? lv_increment
4418 : lv_decrement)))
928c19bb
JM
4419 return error_mark_node;
4420
09639a83
ILT
4421 if (warn_cxx_compat && TREE_CODE (TREE_TYPE (arg)) == ENUMERAL_TYPE)
4422 {
4423 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4424 warning_at (location, OPT_Wc___compat,
4425 "increment of enumeration value is invalid in C++");
4426 else
4427 warning_at (location, OPT_Wc___compat,
4428 "decrement of enumeration value is invalid in C++");
4429 }
4430
9a2300e9
MP
4431 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4432 {
4433 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
4434 warning_at (location, OPT_Wbool_operation,
4435 "increment of a boolean expression");
4436 else
4437 warning_at (location, OPT_Wbool_operation,
4438 "decrement of a boolean expression");
4439 }
4440
928c19bb 4441 /* Ensure the argument is fully folded inside any SAVE_EXPR. */
f9c59f7e 4442 arg = c_fully_fold (arg, false, NULL, true);
928c19bb 4443
267bac10
JM
4444 bool atomic_op;
4445 atomic_op = really_atomic_lvalue (arg);
4446
3e4093b6
RS
4447 /* Increment or decrement the real part of the value,
4448 and don't change the imaginary part. */
4449 if (typecode == COMPLEX_TYPE)
400fbf9f 4450 {
3e4093b6
RS
4451 tree real, imag;
4452
c1771a20 4453 pedwarn (location, OPT_Wpedantic,
509c9d60 4454 "ISO C does not support %<++%> and %<--%> on complex types");
3e4093b6 4455
267bac10
JM
4456 if (!atomic_op)
4457 {
4458 arg = stabilize_reference (arg);
e51fbec3
MP
4459 real = build_unary_op (EXPR_LOCATION (arg), REALPART_EXPR, arg,
4460 true);
4461 imag = build_unary_op (EXPR_LOCATION (arg), IMAGPART_EXPR, arg,
4462 true);
4463 real = build_unary_op (EXPR_LOCATION (arg), code, real, true);
267bac10
JM
4464 if (real == error_mark_node || imag == error_mark_node)
4465 return error_mark_node;
4466 ret = build2 (COMPLEX_EXPR, TREE_TYPE (arg),
4467 real, imag);
4468 goto return_build_unary_op;
4469 }
400fbf9f 4470 }
3e4093b6
RS
4471
4472 /* Report invalid types. */
4473
ab22c1fa 4474 if (typecode != POINTER_TYPE && typecode != FIXED_POINT_TYPE
267bac10 4475 && typecode != INTEGER_TYPE && typecode != REAL_TYPE
241b71bb 4476 && typecode != COMPLEX_TYPE && typecode != VECTOR_TYPE)
400fbf9f 4477 {
3e4093b6 4478 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4479 error_at (location, "wrong type argument to increment");
c22cacf3 4480 else
c9f9eb5d 4481 error_at (location, "wrong type argument to decrement");
3e4093b6
RS
4482
4483 return error_mark_node;
400fbf9f 4484 }
400fbf9f 4485
3e4093b6
RS
4486 {
4487 tree inc;
400fbf9f 4488
3e4093b6
RS
4489 argtype = TREE_TYPE (arg);
4490
4491 /* Compute the increment. */
4492
4493 if (typecode == POINTER_TYPE)
4494 {
a0e24419 4495 /* If pointer target is an incomplete type,
3e4093b6 4496 we just cannot know how to do the arithmetic. */
b70cef5d 4497 if (!COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (argtype)))
3e4093b6
RS
4498 {
4499 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
c9f9eb5d 4500 error_at (location,
a0e24419
MP
4501 "increment of pointer to an incomplete type %qT",
4502 TREE_TYPE (argtype));
3e4093b6 4503 else
c9f9eb5d 4504 error_at (location,
a0e24419
MP
4505 "decrement of pointer to an incomplete type %qT",
4506 TREE_TYPE (argtype));
3e4093b6 4507 }
b70cef5d
JJ
4508 else if (TREE_CODE (TREE_TYPE (argtype)) == FUNCTION_TYPE
4509 || TREE_CODE (TREE_TYPE (argtype)) == VOID_TYPE)
c22cacf3 4510 {
3e4093b6 4511 if (code == PREINCREMENT_EXPR || code == POSTINCREMENT_EXPR)
44d90fe1 4512 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4513 "wrong type argument to increment");
3e4093b6 4514 else
44d90fe1 4515 pedwarn (location, OPT_Wpointer_arith,
fcf73884 4516 "wrong type argument to decrement");
3e4093b6
RS
4517 }
4518
b70cef5d 4519 inc = c_size_in_bytes (TREE_TYPE (argtype));
0d82a1c8 4520 inc = convert_to_ptrofftype_loc (location, inc);
3e4093b6 4521 }
b70cef5d 4522 else if (FRACT_MODE_P (TYPE_MODE (argtype)))
ab22c1fa
CF
4523 {
4524 /* For signed fract types, we invert ++ to -- or
4525 -- to ++, and change inc from 1 to -1, because
4526 it is not possible to represent 1 in signed fract constants.
4527 For unsigned fract types, the result always overflows and
4528 we get an undefined (original) or the maximum value. */
4529 if (code == PREINCREMENT_EXPR)
4530 code = PREDECREMENT_EXPR;
4531 else if (code == PREDECREMENT_EXPR)
4532 code = PREINCREMENT_EXPR;
4533 else if (code == POSTINCREMENT_EXPR)
4534 code = POSTDECREMENT_EXPR;
4535 else /* code == POSTDECREMENT_EXPR */
4536 code = POSTINCREMENT_EXPR;
4537
4538 inc = integer_minus_one_node;
4539 inc = convert (argtype, inc);
4540 }
3e4093b6 4541 else
5be014d5 4542 {
241b71bb
TV
4543 inc = VECTOR_TYPE_P (argtype)
4544 ? build_one_cst (argtype)
4545 : integer_one_node;
5be014d5
AP
4546 inc = convert (argtype, inc);
4547 }
3e4093b6 4548
925e8657
NP
4549 /* If 'arg' is an Objective-C PROPERTY_REF expression, then we
4550 need to ask Objective-C to build the increment or decrement
4551 expression for it. */
4552 if (objc_is_property_ref (arg))
f90e8e2e 4553 return objc_build_incr_expr_for_property_ref (location, code,
925e8657
NP
4554 arg, inc);
4555
3e4093b6 4556 /* Report a read-only lvalue. */
f37acdf9 4557 if (TYPE_READONLY (argtype))
953ff289 4558 {
c02065fc 4559 readonly_error (location, arg,
953ff289
DN
4560 ((code == PREINCREMENT_EXPR
4561 || code == POSTINCREMENT_EXPR)
4562 ? lv_increment : lv_decrement));
4563 return error_mark_node;
4564 }
f37acdf9
JM
4565 else if (TREE_READONLY (arg))
4566 readonly_warning (arg,
4567 ((code == PREINCREMENT_EXPR
4568 || code == POSTINCREMENT_EXPR)
4569 ? lv_increment : lv_decrement));
3e4093b6 4570
267bac10
JM
4571 /* If the argument is atomic, use the special code sequences for
4572 atomic compound assignment. */
4573 if (atomic_op)
4574 {
4575 arg = stabilize_reference (arg);
4576 ret = build_atomic_assign (location, arg,
4577 ((code == PREINCREMENT_EXPR
4578 || code == POSTINCREMENT_EXPR)
4579 ? PLUS_EXPR
4580 : MINUS_EXPR),
4581 (FRACT_MODE_P (TYPE_MODE (argtype))
4582 ? inc
4583 : integer_one_node),
4584 (code == POSTINCREMENT_EXPR
4585 || code == POSTDECREMENT_EXPR));
4586 goto return_build_unary_op;
4587 }
4588
3e4093b6
RS
4589 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
4590 val = boolean_increment (code, arg);
4591 else
53fb4de3 4592 val = build2 (code, TREE_TYPE (arg), arg, inc);
3e4093b6 4593 TREE_SIDE_EFFECTS (val) = 1;
3e4093b6 4594 if (TREE_CODE (val) != code)
6de9cd9a 4595 TREE_NO_WARNING (val) = 1;
c9f9eb5d
AH
4596 ret = val;
4597 goto return_build_unary_op;
3e4093b6
RS
4598 }
4599
4600 case ADDR_EXPR:
4601 /* Note that this operation never does default_conversion. */
4602
2b4b7036
JM
4603 /* The operand of unary '&' must be an lvalue (which excludes
4604 expressions of type void), or, in C99, the result of a [] or
4605 unary '*' operator. */
4606 if (VOID_TYPE_P (TREE_TYPE (arg))
4607 && TYPE_QUALS (TREE_TYPE (arg)) == TYPE_UNQUALIFIED
22d03525 4608 && (!INDIRECT_REF_P (arg) || !flag_isoc99))
2b4b7036
JM
4609 pedwarn (location, 0, "taking address of expression of type %<void%>");
4610
3e4093b6 4611 /* Let &* cancel out to simplify resulting code. */
22d03525 4612 if (INDIRECT_REF_P (arg))
400fbf9f 4613 {
3e4093b6
RS
4614 /* Don't let this be an lvalue. */
4615 if (lvalue_p (TREE_OPERAND (arg, 0)))
db3927fb 4616 return non_lvalue_loc (location, TREE_OPERAND (arg, 0));
c9f9eb5d
AH
4617 ret = TREE_OPERAND (arg, 0);
4618 goto return_build_unary_op;
400fbf9f 4619 }
1eb8759b 4620
3e4093b6
RS
4621 /* Anything not already handled and not a true memory reference
4622 or a non-lvalue array is an error. */
4c13ba17 4623 if (typecode != FUNCTION_TYPE && !noconvert
ee45a32d 4624 && !lvalue_or_else (location, arg, lv_addressof))
3e4093b6 4625 return error_mark_node;
b6a10c9f 4626
928c19bb
JM
4627 /* Move address operations inside C_MAYBE_CONST_EXPR to simplify
4628 folding later. */
4629 if (TREE_CODE (arg) == C_MAYBE_CONST_EXPR)
4630 {
4631 tree inner = build_unary_op (location, code,
4c13ba17
MP
4632 C_MAYBE_CONST_EXPR_EXPR (arg),
4633 noconvert);
928c19bb
JM
4634 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
4635 C_MAYBE_CONST_EXPR_PRE (arg), inner);
4636 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (arg));
4637 C_MAYBE_CONST_EXPR_NON_CONST (ret)
4638 = C_MAYBE_CONST_EXPR_NON_CONST (arg);
4639 goto return_build_unary_op;
4640 }
4641
3e4093b6
RS
4642 /* Ordinary case; arg is a COMPONENT_REF or a decl. */
4643 argtype = TREE_TYPE (arg);
400fbf9f 4644
3e4093b6 4645 /* If the lvalue is const or volatile, merge that into the type
e73a83fc 4646 to which the address will point. This is only needed
f2c1da78 4647 for function types. */
6615c446 4648 if ((DECL_P (arg) || REFERENCE_CLASS_P (arg))
e73a83fc
RB
4649 && (TREE_READONLY (arg) || TREE_THIS_VOLATILE (arg))
4650 && TREE_CODE (argtype) == FUNCTION_TYPE)
f2c1da78
JM
4651 {
4652 int orig_quals = TYPE_QUALS (strip_array_types (argtype));
4653 int quals = orig_quals;
4654
4655 if (TREE_READONLY (arg))
4656 quals |= TYPE_QUAL_CONST;
4657 if (TREE_THIS_VOLATILE (arg))
4658 quals |= TYPE_QUAL_VOLATILE;
4659
f2c1da78
JM
4660 argtype = c_build_qualified_type (argtype, quals);
4661 }
400fbf9f 4662
ee45a32d
EB
4663 switch (TREE_CODE (arg))
4664 {
4665 case COMPONENT_REF:
4666 if (DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)))
4667 {
949505a9
MP
4668 error_at (location, "cannot take address of bit-field %qD",
4669 TREE_OPERAND (arg, 1));
ee45a32d
EB
4670 return error_mark_node;
4671 }
4672
191816a3 4673 /* fall through */
ee45a32d
EB
4674
4675 case ARRAY_REF:
4676 if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (arg, 0))))
4677 {
4678 if (!AGGREGATE_TYPE_P (TREE_TYPE (arg))
4679 && !VECTOR_TYPE_P (TREE_TYPE (arg)))
4680 {
949505a9
MP
4681 error_at (location, "cannot take address of scalar with "
4682 "reverse storage order");
ee45a32d
EB
4683 return error_mark_node;
4684 }
4685
4686 if (TREE_CODE (TREE_TYPE (arg)) == ARRAY_TYPE
4687 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (arg)))
949505a9
MP
4688 warning_at (location, OPT_Wscalar_storage_order,
4689 "address of array with reverse scalar storage "
4690 "order requested");
ee45a32d
EB
4691 }
4692
4693 default:
4694 break;
4695 }
4696
3e4093b6
RS
4697 if (!c_mark_addressable (arg))
4698 return error_mark_node;
400fbf9f 4699
abb54d14
JM
4700 gcc_assert (TREE_CODE (arg) != COMPONENT_REF
4701 || !DECL_C_BIT_FIELD (TREE_OPERAND (arg, 1)));
400fbf9f 4702
5cc200fc 4703 argtype = build_pointer_type (argtype);
5e55f99d
RH
4704
4705 /* ??? Cope with user tricks that amount to offsetof. Delete this
4706 when we have proper support for integer constant expressions. */
4707 val = get_base_address (arg);
22d03525 4708 if (val && INDIRECT_REF_P (val)
3aa2ddb8
JJ
4709 && TREE_CONSTANT (TREE_OPERAND (val, 0)))
4710 {
79e7b1fe 4711 ret = fold_offsetof (arg, argtype);
c9f9eb5d 4712 goto return_build_unary_op;
3aa2ddb8 4713 }
5e55f99d 4714
5cc200fc 4715 val = build1 (ADDR_EXPR, argtype, arg);
400fbf9f 4716
c9f9eb5d
AH
4717 ret = val;
4718 goto return_build_unary_op;
400fbf9f 4719
3e4093b6 4720 default:
1344f9a3 4721 gcc_unreachable ();
3e4093b6 4722 }
400fbf9f 4723
3fa8871b 4724 if (argtype == NULL_TREE)
3e4093b6 4725 argtype = TREE_TYPE (arg);
928c19bb
JM
4726 if (TREE_CODE (arg) == INTEGER_CST)
4727 ret = (require_constant_value
db3927fb
AH
4728 ? fold_build1_initializer_loc (location, code, argtype, arg)
4729 : fold_build1_loc (location, code, argtype, arg));
928c19bb
JM
4730 else
4731 ret = build1 (code, argtype, arg);
c9f9eb5d
AH
4732 return_build_unary_op:
4733 gcc_assert (ret != error_mark_node);
928c19bb
JM
4734 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret)
4735 && !(TREE_CODE (xarg) == INTEGER_CST && !TREE_OVERFLOW (xarg)))
4736 ret = build1 (NOP_EXPR, TREE_TYPE (ret), ret);
4737 else if (TREE_CODE (ret) != INTEGER_CST && int_operands)
4738 ret = note_integer_operands (ret);
8ce94e44
JM
4739 if (eptype)
4740 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
c9f9eb5d
AH
4741 protected_set_expr_location (ret, location);
4742 return ret;
3e4093b6 4743}
400fbf9f 4744
3e4093b6
RS
4745/* Return nonzero if REF is an lvalue valid for this language.
4746 Lvalues can be assigned, unless their type has TYPE_READONLY.
5baeaac0 4747 Lvalues can have their address taken, unless they have C_DECL_REGISTER. */
400fbf9f 4748
1e4ae551 4749bool
58f9752a 4750lvalue_p (const_tree ref)
3e4093b6 4751{
58f9752a 4752 const enum tree_code code = TREE_CODE (ref);
400fbf9f 4753
3e4093b6
RS
4754 switch (code)
4755 {
4756 case REALPART_EXPR:
4757 case IMAGPART_EXPR:
4758 case COMPONENT_REF:
4759 return lvalue_p (TREE_OPERAND (ref, 0));
400fbf9f 4760
928c19bb
JM
4761 case C_MAYBE_CONST_EXPR:
4762 return lvalue_p (TREE_OPERAND (ref, 1));
4763
3e4093b6
RS
4764 case COMPOUND_LITERAL_EXPR:
4765 case STRING_CST:
5b73d2ab 4766 return true;
400fbf9f 4767
3e4093b6
RS
4768 case INDIRECT_REF:
4769 case ARRAY_REF:
4770 case VAR_DECL:
4771 case PARM_DECL:
4772 case RESULT_DECL:
4773 case ERROR_MARK:
4774 return (TREE_CODE (TREE_TYPE (ref)) != FUNCTION_TYPE
4775 && TREE_CODE (TREE_TYPE (ref)) != METHOD_TYPE);
665f2503 4776
3e4093b6 4777 case BIND_EXPR:
3e4093b6 4778 return TREE_CODE (TREE_TYPE (ref)) == ARRAY_TYPE;
665f2503 4779
3e4093b6 4780 default:
5b73d2ab 4781 return false;
3e4093b6
RS
4782 }
4783}
400fbf9f 4784\f
f37acdf9
JM
4785/* Give a warning for storing in something that is read-only in GCC
4786 terms but not const in ISO C terms. */
4787
4788static void
4789readonly_warning (tree arg, enum lvalue_use use)
4790{
4791 switch (use)
4792 {
4793 case lv_assign:
4794 warning (0, "assignment of read-only location %qE", arg);
4795 break;
4796 case lv_increment:
4797 warning (0, "increment of read-only location %qE", arg);
4798 break;
4799 case lv_decrement:
4800 warning (0, "decrement of read-only location %qE", arg);
4801 break;
4802 default:
4803 gcc_unreachable ();
4804 }
4805 return;
4806}
4807
37dc0d8d
JM
4808
4809/* Return nonzero if REF is an lvalue valid for this language;
4810 otherwise, print an error message and return zero. USE says
7bd11157
TT
4811 how the lvalue is being used and so selects the error message.
4812 LOCATION is the location at which any error should be reported. */
37dc0d8d
JM
4813
4814static int
7bd11157 4815lvalue_or_else (location_t loc, const_tree ref, enum lvalue_use use)
37dc0d8d
JM
4816{
4817 int win = lvalue_p (ref);
4818
4819 if (!win)
7bd11157 4820 lvalue_error (loc, use);
37dc0d8d
JM
4821
4822 return win;
4823}
3e4093b6
RS
4824\f
4825/* Mark EXP saying that we need to be able to take the
4826 address of it; it should not be allocated in a register.
a9e4a1a5
JJ
4827 Returns true if successful. ARRAY_REF_P is true if this
4828 is for ARRAY_REF construction - in that case we don't want
4829 to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
4830 it is fine to use ARRAY_REFs for vector subscripts on vector
4831 register variables. */
54c93c30 4832
3e4093b6 4833bool
a9e4a1a5 4834c_mark_addressable (tree exp, bool array_ref_p)
400fbf9f 4835{
3e4093b6 4836 tree x = exp;
95602da1 4837
3e4093b6
RS
4838 while (1)
4839 switch (TREE_CODE (x))
4840 {
a9e4a1a5
JJ
4841 case VIEW_CONVERT_EXPR:
4842 if (array_ref_p
4843 && TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
4844 && VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
4845 return true;
4846 /* FALLTHRU */
3e4093b6 4847 case COMPONENT_REF:
3e4093b6
RS
4848 case ADDR_EXPR:
4849 case ARRAY_REF:
4850 case REALPART_EXPR:
4851 case IMAGPART_EXPR:
4852 x = TREE_OPERAND (x, 0);
4853 break;
95602da1 4854
3e4093b6 4855 case COMPOUND_LITERAL_EXPR:
b33a0cb3
JJ
4856 TREE_ADDRESSABLE (x) = 1;
4857 TREE_ADDRESSABLE (COMPOUND_LITERAL_EXPR_DECL (x)) = 1;
4858 return true;
4859
3e4093b6
RS
4860 case CONSTRUCTOR:
4861 TREE_ADDRESSABLE (x) = 1;
4862 return true;
95602da1 4863
3e4093b6
RS
4864 case VAR_DECL:
4865 case CONST_DECL:
4866 case PARM_DECL:
4867 case RESULT_DECL:
5baeaac0 4868 if (C_DECL_REGISTER (x)
3e4093b6
RS
4869 && DECL_NONLOCAL (x))
4870 {
62f9079a 4871 if (TREE_PUBLIC (x) || is_global_var (x))
3e4093b6 4872 {
0039fa55
AN
4873 error
4874 ("global register variable %qD used in nested function", x);
3e4093b6
RS
4875 return false;
4876 }
509c9d60 4877 pedwarn (input_location, 0, "register variable %qD used in nested function", x);
3e4093b6 4878 }
5baeaac0 4879 else if (C_DECL_REGISTER (x))
3e4093b6 4880 {
62f9079a 4881 if (TREE_PUBLIC (x) || is_global_var (x))
0039fa55
AN
4882 error ("address of global register variable %qD requested", x);
4883 else
4884 error ("address of register variable %qD requested", x);
4885 return false;
3e4093b6 4886 }
400fbf9f 4887
191816a3 4888 /* FALLTHRU */
3e4093b6
RS
4889 case FUNCTION_DECL:
4890 TREE_ADDRESSABLE (x) = 1;
191816a3 4891 /* FALLTHRU */
3e4093b6
RS
4892 default:
4893 return true;
4894 }
4895}
4896\f
2d2e923f
MLI
4897/* Convert EXPR to TYPE, warning about conversion problems with
4898 constants. SEMANTIC_TYPE is the type this conversion would use
4899 without excess precision. If SEMANTIC_TYPE is NULL, this function
4900 is equivalent to convert_and_check. This function is a wrapper that
4901 handles conversions that may be different than
4902 the usual ones because of excess precision. */
4903
4904static tree
68fca595
MP
4905ep_convert_and_check (location_t loc, tree type, tree expr,
4906 tree semantic_type)
2d2e923f
MLI
4907{
4908 if (TREE_TYPE (expr) == type)
4909 return expr;
4910
bb75facd
JM
4911 /* For C11, integer conversions may have results with excess
4912 precision. */
4913 if (flag_isoc11 || !semantic_type)
68fca595 4914 return convert_and_check (loc, type, expr);
2d2e923f
MLI
4915
4916 if (TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
4917 && TREE_TYPE (expr) != semantic_type)
4918 {
4919 /* For integers, we need to check the real conversion, not
4920 the conversion to the excess precision type. */
68fca595 4921 expr = convert_and_check (loc, semantic_type, expr);
2d2e923f
MLI
4922 }
4923 /* Result type is the excess precision type, which should be
4924 large enough, so do not check. */
4925 return convert (type, expr);
4926}
4927
928c19bb
JM
4928/* Build and return a conditional expression IFEXP ? OP1 : OP2. If
4929 IFEXP_BCP then the condition is a call to __builtin_constant_p, and
4930 if folded to an integer constant then the unselected half may
4931 contain arbitrary operations not normally permitted in constant
c2255bc4 4932 expressions. Set the location of the expression to LOC. */
400fbf9f
JW
4933
4934tree
744aa42f 4935build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp,
a32c8316
MP
4936 tree op1, tree op1_original_type, location_t op1_loc,
4937 tree op2, tree op2_original_type, location_t op2_loc)
400fbf9f 4938{
3e4093b6
RS
4939 tree type1;
4940 tree type2;
4941 enum tree_code code1;
4942 enum tree_code code2;
4943 tree result_type = NULL;
2d2e923f 4944 tree semantic_result_type = NULL;
3e4093b6 4945 tree orig_op1 = op1, orig_op2 = op2;
928c19bb 4946 bool int_const, op1_int_operands, op2_int_operands, int_operands;
4d84fe7c 4947 bool ifexp_int_operands;
928c19bb 4948 tree ret;
400fbf9f 4949
4d84fe7c
JM
4950 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
4951 if (op1_int_operands)
4952 op1 = remove_c_maybe_const_expr (op1);
4953 op2_int_operands = EXPR_INT_CONST_OPERANDS (orig_op2);
4954 if (op2_int_operands)
4955 op2 = remove_c_maybe_const_expr (op2);
4956 ifexp_int_operands = EXPR_INT_CONST_OPERANDS (ifexp);
4957 if (ifexp_int_operands)
4958 ifexp = remove_c_maybe_const_expr (ifexp);
4959
3e4093b6
RS
4960 /* Promote both alternatives. */
4961
4962 if (TREE_CODE (TREE_TYPE (op1)) != VOID_TYPE)
4963 op1 = default_conversion (op1);
4964 if (TREE_CODE (TREE_TYPE (op2)) != VOID_TYPE)
4965 op2 = default_conversion (op2);
4966
4967 if (TREE_CODE (ifexp) == ERROR_MARK
4968 || TREE_CODE (TREE_TYPE (op1)) == ERROR_MARK
4969 || TREE_CODE (TREE_TYPE (op2)) == ERROR_MARK)
400fbf9f 4970 return error_mark_node;
400fbf9f 4971
3e4093b6
RS
4972 type1 = TREE_TYPE (op1);
4973 code1 = TREE_CODE (type1);
4974 type2 = TREE_TYPE (op2);
4975 code2 = TREE_CODE (type2);
4976
1807ffc1
MS
4977 if (code1 == POINTER_TYPE && reject_gcc_builtin (op1))
4978 return error_mark_node;
4979
4980 if (code2 == POINTER_TYPE && reject_gcc_builtin (op2))
4981 return error_mark_node;
4982
b1adf557
JM
4983 /* C90 does not permit non-lvalue arrays in conditional expressions.
4984 In C99 they will be pointers by now. */
4985 if (code1 == ARRAY_TYPE || code2 == ARRAY_TYPE)
4986 {
744aa42f 4987 error_at (colon_loc, "non-lvalue array in conditional expression");
b1adf557
JM
4988 return error_mark_node;
4989 }
4990
8ce94e44
JM
4991 if ((TREE_CODE (op1) == EXCESS_PRECISION_EXPR
4992 || TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
4993 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
4994 || code1 == COMPLEX_TYPE)
4995 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
4996 || code2 == COMPLEX_TYPE))
4997 {
2d2e923f 4998 semantic_result_type = c_common_type (type1, type2);
8ce94e44
JM
4999 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
5000 {
5001 op1 = TREE_OPERAND (op1, 0);
5002 type1 = TREE_TYPE (op1);
5003 gcc_assert (TREE_CODE (type1) == code1);
5004 }
5005 if (TREE_CODE (op2) == EXCESS_PRECISION_EXPR)
5006 {
5007 op2 = TREE_OPERAND (op2, 0);
5008 type2 = TREE_TYPE (op2);
5009 gcc_assert (TREE_CODE (type2) == code2);
5010 }
5011 }
5012
d130ae11
ILT
5013 if (warn_cxx_compat)
5014 {
5015 tree t1 = op1_original_type ? op1_original_type : TREE_TYPE (orig_op1);
5016 tree t2 = op2_original_type ? op2_original_type : TREE_TYPE (orig_op2);
5017
5018 if (TREE_CODE (t1) == ENUMERAL_TYPE
5019 && TREE_CODE (t2) == ENUMERAL_TYPE
5020 && TYPE_MAIN_VARIANT (t1) != TYPE_MAIN_VARIANT (t2))
5021 warning_at (colon_loc, OPT_Wc___compat,
5022 ("different enum types in conditional is "
5023 "invalid in C++: %qT vs %qT"),
5024 t1, t2);
5025 }
5026
3e4093b6
RS
5027 /* Quickly detect the usual case where op1 and op2 have the same type
5028 after promotion. */
5029 if (TYPE_MAIN_VARIANT (type1) == TYPE_MAIN_VARIANT (type2))
400fbf9f 5030 {
3e4093b6
RS
5031 if (type1 == type2)
5032 result_type = type1;
5033 else
5034 result_type = TYPE_MAIN_VARIANT (type1);
5035 }
5036 else if ((code1 == INTEGER_TYPE || code1 == REAL_TYPE
c22cacf3
MS
5037 || code1 == COMPLEX_TYPE)
5038 && (code2 == INTEGER_TYPE || code2 == REAL_TYPE
5039 || code2 == COMPLEX_TYPE))
3e4093b6 5040 {
bb75facd
JM
5041 /* In C11, a conditional expression between a floating-point
5042 type and an integer type should convert the integer type to
5043 the evaluation format of the floating-point type, with
5044 possible excess precision. */
5045 tree eptype1 = type1;
5046 tree eptype2 = type2;
5047 if (flag_isoc11)
5048 {
5049 tree eptype;
5050 if (ANY_INTEGRAL_TYPE_P (type1)
5051 && (eptype = excess_precision_type (type2)) != NULL_TREE)
5052 {
5053 eptype2 = eptype;
5054 if (!semantic_result_type)
5055 semantic_result_type = c_common_type (type1, type2);
5056 }
5057 else if (ANY_INTEGRAL_TYPE_P (type2)
5058 && (eptype = excess_precision_type (type1)) != NULL_TREE)
5059 {
5060 eptype1 = eptype;
5061 if (!semantic_result_type)
5062 semantic_result_type = c_common_type (type1, type2);
5063 }
5064 }
5065 result_type = c_common_type (eptype1, eptype2);
5a578671
JM
5066 if (result_type == error_mark_node)
5067 return error_mark_node;
0a0b3574
MM
5068 do_warn_double_promotion (result_type, type1, type2,
5069 "implicit conversion from %qT to %qT to "
5070 "match other result of conditional",
5071 colon_loc);
400fbf9f 5072
3e4093b6
RS
5073 /* If -Wsign-compare, warn here if type1 and type2 have
5074 different signedness. We'll promote the signed to unsigned
5075 and later code won't know it used to be different.
5076 Do this check on the original types, so that explicit casts
5077 will be considered, but default promotions won't. */
7d882b83 5078 if (c_inhibit_evaluation_warnings == 0)
ab87f8c8 5079 {
8df83eae
RK
5080 int unsigned_op1 = TYPE_UNSIGNED (TREE_TYPE (orig_op1));
5081 int unsigned_op2 = TYPE_UNSIGNED (TREE_TYPE (orig_op2));
400fbf9f 5082
3e4093b6
RS
5083 if (unsigned_op1 ^ unsigned_op2)
5084 {
6ac01510
ILT
5085 bool ovf;
5086
3e4093b6
RS
5087 /* Do not warn if the result type is signed, since the
5088 signed type will only be chosen if it can represent
5089 all the values of the unsigned type. */
3f75a254 5090 if (!TYPE_UNSIGNED (result_type))
3e4093b6 5091 /* OK */;
3e4093b6 5092 else
928c19bb
JM
5093 {
5094 bool op1_maybe_const = true;
5095 bool op2_maybe_const = true;
5096
5097 /* Do not warn if the signed quantity is an
5098 unsuffixed integer literal (or some static
5099 constant expression involving such literals) and
5100 it is non-negative. This warning requires the
5101 operands to be folded for best results, so do
5102 that folding in this case even without
5103 warn_sign_compare to avoid warning options
5104 possibly affecting code generation. */
f5178456
RS
5105 c_inhibit_evaluation_warnings
5106 += (ifexp == truthvalue_false_node);
928c19bb
JM
5107 op1 = c_fully_fold (op1, require_constant_value,
5108 &op1_maybe_const);
f5178456
RS
5109 c_inhibit_evaluation_warnings
5110 -= (ifexp == truthvalue_false_node);
5111
5112 c_inhibit_evaluation_warnings
5113 += (ifexp == truthvalue_true_node);
928c19bb
JM
5114 op2 = c_fully_fold (op2, require_constant_value,
5115 &op2_maybe_const);
f5178456
RS
5116 c_inhibit_evaluation_warnings
5117 -= (ifexp == truthvalue_true_node);
928c19bb
JM
5118
5119 if (warn_sign_compare)
5120 {
5121 if ((unsigned_op2
5122 && tree_expr_nonnegative_warnv_p (op1, &ovf))
5123 || (unsigned_op1
5124 && tree_expr_nonnegative_warnv_p (op2, &ovf)))
5125 /* OK */;
a32c8316
MP
5126 else if (unsigned_op2)
5127 warning_at (op1_loc, OPT_Wsign_compare,
5128 "operand of ?: changes signedness from "
5129 "%qT to %qT due to unsignedness of other "
5130 "operand", TREE_TYPE (orig_op1),
5131 TREE_TYPE (orig_op2));
928c19bb 5132 else
a32c8316
MP
5133 warning_at (op2_loc, OPT_Wsign_compare,
5134 "operand of ?: changes signedness from "
5135 "%qT to %qT due to unsignedness of other "
5136 "operand", TREE_TYPE (orig_op2),
5137 TREE_TYPE (orig_op1));
928c19bb
JM
5138 }
5139 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 5140 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 5141 if (!op2_maybe_const || TREE_CODE (op2) != INTEGER_CST)
e5a94231 5142 op2 = c_wrap_maybe_const (op2, !op2_maybe_const);
928c19bb 5143 }
3e4093b6
RS
5144 }
5145 }
5146 }
5147 else if (code1 == VOID_TYPE || code2 == VOID_TYPE)
5148 {
fcf73884 5149 if (code1 != VOID_TYPE || code2 != VOID_TYPE)
c1771a20 5150 pedwarn (colon_loc, OPT_Wpedantic,
fcf73884 5151 "ISO C forbids conditional expr with only one void side");
3e4093b6
RS
5152 result_type = void_type_node;
5153 }
5154 else if (code1 == POINTER_TYPE && code2 == POINTER_TYPE)
5155 {
36c5e70a
BE
5156 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
5157 addr_space_t as2 = TYPE_ADDR_SPACE (TREE_TYPE (type2));
5158 addr_space_t as_common;
5159
744aa42f 5160 if (comp_target_types (colon_loc, type1, type2))
10bc1b1b 5161 result_type = common_pointer_type (type1, type2);
6aa3c60d 5162 else if (null_pointer_constant_p (orig_op1))
36c5e70a 5163 result_type = type2;
6aa3c60d 5164 else if (null_pointer_constant_p (orig_op2))
36c5e70a
BE
5165 result_type = type1;
5166 else if (!addr_space_superset (as1, as2, &as_common))
5167 {
5168 error_at (colon_loc, "pointers to disjoint address spaces "
5169 "used in conditional expression");
5170 return error_mark_node;
5171 }
267bac10
JM
5172 else if (VOID_TYPE_P (TREE_TYPE (type1))
5173 && !TYPE_ATOMIC (TREE_TYPE (type1)))
34a80643 5174 {
768952be
MU
5175 if ((TREE_CODE (TREE_TYPE (type2)) == ARRAY_TYPE)
5176 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type2)))
5177 & ~TYPE_QUALS (TREE_TYPE (type1))))
5178 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5179 "pointer to array loses qualifier "
5180 "in conditional expression");
5181
fcf73884 5182 if (TREE_CODE (TREE_TYPE (type2)) == FUNCTION_TYPE)
c1771a20 5183 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5184 "ISO C forbids conditional expr between "
bda67431 5185 "%<void *%> and function pointer");
3e4093b6
RS
5186 result_type = build_pointer_type (qualify_type (TREE_TYPE (type1),
5187 TREE_TYPE (type2)));
34a80643 5188 }
267bac10
JM
5189 else if (VOID_TYPE_P (TREE_TYPE (type2))
5190 && !TYPE_ATOMIC (TREE_TYPE (type2)))
1c2a9b35 5191 {
768952be
MU
5192 if ((TREE_CODE (TREE_TYPE (type1)) == ARRAY_TYPE)
5193 && (TYPE_QUALS (strip_array_types (TREE_TYPE (type1)))
5194 & ~TYPE_QUALS (TREE_TYPE (type2))))
5195 warning_at (colon_loc, OPT_Wdiscarded_array_qualifiers,
5196 "pointer to array loses qualifier "
5197 "in conditional expression");
5198
fcf73884 5199 if (TREE_CODE (TREE_TYPE (type1)) == FUNCTION_TYPE)
c1771a20 5200 pedwarn (colon_loc, OPT_Wpedantic,
509c9d60 5201 "ISO C forbids conditional expr between "
bda67431 5202 "%<void *%> and function pointer");
3e4093b6
RS
5203 result_type = build_pointer_type (qualify_type (TREE_TYPE (type2),
5204 TREE_TYPE (type1)));
1c2a9b35 5205 }
b581b85b
NP
5206 /* Objective-C pointer comparisons are a bit more lenient. */
5207 else if (objc_have_common_type (type1, type2, -3, NULL_TREE))
5208 result_type = objc_common_type (type1, type2);
34a80643 5209 else
ab87f8c8 5210 {
36c5e70a
BE
5211 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
5212
b581b85b
NP
5213 pedwarn (colon_loc, 0,
5214 "pointer type mismatch in conditional expression");
36c5e70a
BE
5215 result_type = build_pointer_type
5216 (build_qualified_type (void_type_node, qual));
ab87f8c8 5217 }
3e4093b6
RS
5218 }
5219 else if (code1 == POINTER_TYPE && code2 == INTEGER_TYPE)
5220 {
6aa3c60d 5221 if (!null_pointer_constant_p (orig_op2))
744aa42f 5222 pedwarn (colon_loc, 0,
509c9d60 5223 "pointer/integer type mismatch in conditional expression");
3e4093b6 5224 else
ab87f8c8 5225 {
3e4093b6 5226 op2 = null_pointer_node;
ab87f8c8 5227 }
3e4093b6
RS
5228 result_type = type1;
5229 }
5230 else if (code2 == POINTER_TYPE && code1 == INTEGER_TYPE)
5231 {
6aa3c60d 5232 if (!null_pointer_constant_p (orig_op1))
744aa42f 5233 pedwarn (colon_loc, 0,
509c9d60 5234 "pointer/integer type mismatch in conditional expression");
3e4093b6 5235 else
ab87f8c8 5236 {
3e4093b6 5237 op1 = null_pointer_node;
ab87f8c8 5238 }
3e4093b6
RS
5239 result_type = type2;
5240 }
1c2a9b35 5241
3e4093b6
RS
5242 if (!result_type)
5243 {
5244 if (flag_cond_mismatch)
5245 result_type = void_type_node;
5246 else
400fbf9f 5247 {
c2255bc4 5248 error_at (colon_loc, "type mismatch in conditional expression");
ab87f8c8 5249 return error_mark_node;
400fbf9f 5250 }
3e4093b6 5251 }
400fbf9f 5252
3e4093b6
RS
5253 /* Merge const and volatile flags of the incoming types. */
5254 result_type
5255 = build_type_variant (result_type,
afbd0665
AS
5256 TYPE_READONLY (type1) || TYPE_READONLY (type2),
5257 TYPE_VOLATILE (type1) || TYPE_VOLATILE (type2));
b6a10c9f 5258
68fca595
MP
5259 op1 = ep_convert_and_check (colon_loc, result_type, op1,
5260 semantic_result_type);
5261 op2 = ep_convert_and_check (colon_loc, result_type, op2,
5262 semantic_result_type);
b6a10c9f 5263
928c19bb
JM
5264 if (ifexp_bcp && ifexp == truthvalue_true_node)
5265 {
5266 op2_int_operands = true;
5267 op1 = c_fully_fold (op1, require_constant_value, NULL);
5268 }
5269 if (ifexp_bcp && ifexp == truthvalue_false_node)
5270 {
5271 op1_int_operands = true;
5272 op2 = c_fully_fold (op2, require_constant_value, NULL);
5273 }
4d84fe7c 5274 int_const = int_operands = (ifexp_int_operands
928c19bb
JM
5275 && op1_int_operands
5276 && op2_int_operands);
5277 if (int_operands)
5278 {
5279 int_const = ((ifexp == truthvalue_true_node
5280 && TREE_CODE (orig_op1) == INTEGER_CST
5281 && !TREE_OVERFLOW (orig_op1))
5282 || (ifexp == truthvalue_false_node
5283 && TREE_CODE (orig_op2) == INTEGER_CST
5284 && !TREE_OVERFLOW (orig_op2)));
5285 }
9f47c7e5
IE
5286
5287 /* Need to convert condition operand into a vector mask. */
5288 if (VECTOR_TYPE_P (TREE_TYPE (ifexp)))
5289 {
5290 tree vectype = TREE_TYPE (ifexp);
5291 tree elem_type = TREE_TYPE (vectype);
5292 tree zero = build_int_cst (elem_type, 0);
5293 tree zero_vec = build_vector_from_val (vectype, zero);
5294 tree cmp_type = build_same_sized_truth_vector_type (vectype);
5295 ifexp = build2 (NE_EXPR, cmp_type, ifexp, zero_vec);
5296 }
5297
928c19bb 5298 if (int_const || (ifexp_bcp && TREE_CODE (ifexp) == INTEGER_CST))
db3927fb 5299 ret = fold_build3_loc (colon_loc, COND_EXPR, result_type, ifexp, op1, op2);
928c19bb
JM
5300 else
5301 {
01c7ccbb
JM
5302 if (int_operands)
5303 {
f34f1c87
MP
5304 /* Use c_fully_fold here, since C_MAYBE_CONST_EXPR might be
5305 nested inside of the expression. */
5306 op1 = c_fully_fold (op1, false, NULL);
5307 op2 = c_fully_fold (op2, false, NULL);
01c7ccbb 5308 }
928c19bb
JM
5309 ret = build3 (COND_EXPR, result_type, ifexp, op1, op2);
5310 if (int_operands)
5311 ret = note_integer_operands (ret);
5312 }
2d2e923f
MLI
5313 if (semantic_result_type)
5314 ret = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, ret);
928c19bb 5315
c2255bc4 5316 protected_set_expr_location (ret, colon_loc);
2ebd93e1
MP
5317
5318 /* If the OP1 and OP2 are the same and don't have side-effects,
5319 warn here, because the COND_EXPR will be turned into OP1. */
5320 if (warn_duplicated_branches
5321 && TREE_CODE (ret) == COND_EXPR
5322 && (op1 == op2 || operand_equal_p (op1, op2, 0)))
5323 warning_at (EXPR_LOCATION (ret), OPT_Wduplicated_branches,
5324 "this condition has identical branches");
5325
928c19bb 5326 return ret;
3e4093b6
RS
5327}
5328\f
487a92fe 5329/* Return a compound expression that performs two expressions and
c2255bc4
AH
5330 returns the value of the second of them.
5331
5332 LOC is the location of the COMPOUND_EXPR. */
400fbf9f 5333
3e4093b6 5334tree
c2255bc4 5335build_compound_expr (location_t loc, tree expr1, tree expr2)
3e4093b6 5336{
4d84fe7c 5337 bool expr1_int_operands, expr2_int_operands;
8ce94e44 5338 tree eptype = NULL_TREE;
928c19bb
JM
5339 tree ret;
5340
4d84fe7c
JM
5341 expr1_int_operands = EXPR_INT_CONST_OPERANDS (expr1);
5342 if (expr1_int_operands)
5343 expr1 = remove_c_maybe_const_expr (expr1);
5344 expr2_int_operands = EXPR_INT_CONST_OPERANDS (expr2);
5345 if (expr2_int_operands)
5346 expr2 = remove_c_maybe_const_expr (expr2);
5347
8ce94e44
JM
5348 if (TREE_CODE (expr1) == EXCESS_PRECISION_EXPR)
5349 expr1 = TREE_OPERAND (expr1, 0);
5350 if (TREE_CODE (expr2) == EXCESS_PRECISION_EXPR)
5351 {
5352 eptype = TREE_TYPE (expr2);
5353 expr2 = TREE_OPERAND (expr2, 0);
5354 }
5355
3f75a254 5356 if (!TREE_SIDE_EFFECTS (expr1))
3e4093b6
RS
5357 {
5358 /* The left-hand operand of a comma expression is like an expression
c5409249 5359 statement: with -Wunused, we should warn if it doesn't have
3e4093b6 5360 any side-effects, unless it was explicitly cast to (void). */
e14a6540 5361 if (warn_unused_value)
47aecf47 5362 {
e14a6540 5363 if (VOID_TYPE_P (TREE_TYPE (expr1))
1043771b 5364 && CONVERT_EXPR_P (expr1))
47aecf47 5365 ; /* (void) a, b */
e14a6540
JM
5366 else if (VOID_TYPE_P (TREE_TYPE (expr1))
5367 && TREE_CODE (expr1) == COMPOUND_EXPR
1043771b 5368 && CONVERT_EXPR_P (TREE_OPERAND (expr1, 1)))
47aecf47
JM
5369 ; /* (void) a, (void) b, c */
5370 else
b8698a0f 5371 warning_at (loc, OPT_Wunused_value,
c2255bc4 5372 "left-hand operand of comma expression has no effect");
47aecf47 5373 }
3e4093b6 5374 }
789eadcd
MP
5375 else if (TREE_CODE (expr1) == COMPOUND_EXPR
5376 && warn_unused_value)
5377 {
5378 tree r = expr1;
5379 location_t cloc = loc;
5380 while (TREE_CODE (r) == COMPOUND_EXPR)
5381 {
5382 if (EXPR_HAS_LOCATION (r))
5383 cloc = EXPR_LOCATION (r);
5384 r = TREE_OPERAND (r, 1);
5385 }
5386 if (!TREE_SIDE_EFFECTS (r)
5387 && !VOID_TYPE_P (TREE_TYPE (r))
5388 && !CONVERT_EXPR_P (r))
5389 warning_at (cloc, OPT_Wunused_value,
5390 "right-hand operand of comma expression has no effect");
5391 }
400fbf9f 5392
3e4093b6
RS
5393 /* With -Wunused, we should also warn if the left-hand operand does have
5394 side-effects, but computes a value which is not used. For example, in
5395 `foo() + bar(), baz()' the result of the `+' operator is not used,
5396 so we should issue a warning. */
5397 else if (warn_unused_value)
c2255bc4 5398 warn_if_unused_value (expr1, loc);
400fbf9f 5399
e63d6886
AP
5400 if (expr2 == error_mark_node)
5401 return error_mark_node;
5402
928c19bb
JM
5403 ret = build2 (COMPOUND_EXPR, TREE_TYPE (expr2), expr1, expr2);
5404
5405 if (flag_isoc99
4d84fe7c
JM
5406 && expr1_int_operands
5407 && expr2_int_operands)
928c19bb
JM
5408 ret = note_integer_operands (ret);
5409
8ce94e44
JM
5410 if (eptype)
5411 ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
5412
c2255bc4 5413 protected_set_expr_location (ret, loc);
928c19bb 5414 return ret;
3e4093b6 5415}
400fbf9f 5416
67165eb3
ILT
5417/* Issue -Wcast-qual warnings when appropriate. TYPE is the type to
5418 which we are casting. OTYPE is the type of the expression being
2ee3cb35
MLI
5419 cast. Both TYPE and OTYPE are pointer types. LOC is the location
5420 of the cast. -Wcast-qual appeared on the command line. Named
5421 address space qualifiers are not handled here, because they result
5422 in different warnings. */
67165eb3
ILT
5423
5424static void
2ee3cb35 5425handle_warn_cast_qual (location_t loc, tree type, tree otype)
67165eb3
ILT
5426{
5427 tree in_type = type;
5428 tree in_otype = otype;
5429 int added = 0;
5430 int discarded = 0;
5431 bool is_const;
5432
5433 /* Check that the qualifiers on IN_TYPE are a superset of the
5434 qualifiers of IN_OTYPE. The outermost level of POINTER_TYPE
5435 nodes is uninteresting and we stop as soon as we hit a
5436 non-POINTER_TYPE node on either type. */
5437 do
5438 {
5439 in_otype = TREE_TYPE (in_otype);
5440 in_type = TREE_TYPE (in_type);
5441
5442 /* GNU C allows cv-qualified function types. 'const' means the
5443 function is very pure, 'volatile' means it can't return. We
5444 need to warn when such qualifiers are added, not when they're
5445 taken away. */
5446 if (TREE_CODE (in_otype) == FUNCTION_TYPE
5447 && TREE_CODE (in_type) == FUNCTION_TYPE)
36c5e70a
BE
5448 added |= (TYPE_QUALS_NO_ADDR_SPACE (in_type)
5449 & ~TYPE_QUALS_NO_ADDR_SPACE (in_otype));
67165eb3 5450 else
36c5e70a
BE
5451 discarded |= (TYPE_QUALS_NO_ADDR_SPACE (in_otype)
5452 & ~TYPE_QUALS_NO_ADDR_SPACE (in_type));
67165eb3
ILT
5453 }
5454 while (TREE_CODE (in_type) == POINTER_TYPE
5455 && TREE_CODE (in_otype) == POINTER_TYPE);
5456
5457 if (added)
2ee3cb35
MLI
5458 warning_at (loc, OPT_Wcast_qual,
5459 "cast adds %q#v qualifier to function type", added);
67165eb3
ILT
5460
5461 if (discarded)
5462 /* There are qualifiers present in IN_OTYPE that are not present
5463 in IN_TYPE. */
2ee3cb35 5464 warning_at (loc, OPT_Wcast_qual,
7485aeea 5465 "cast discards %qv qualifier from pointer target type",
2ee3cb35 5466 discarded);
67165eb3
ILT
5467
5468 if (added || discarded)
5469 return;
5470
5471 /* A cast from **T to const **T is unsafe, because it can cause a
5472 const value to be changed with no additional warning. We only
5473 issue this warning if T is the same on both sides, and we only
5474 issue the warning if there are the same number of pointers on
5475 both sides, as otherwise the cast is clearly unsafe anyhow. A
5476 cast is unsafe when a qualifier is added at one level and const
5477 is not present at all outer levels.
5478
5479 To issue this warning, we check at each level whether the cast
5480 adds new qualifiers not already seen. We don't need to special
5481 case function types, as they won't have the same
5482 TYPE_MAIN_VARIANT. */
5483
5484 if (TYPE_MAIN_VARIANT (in_type) != TYPE_MAIN_VARIANT (in_otype))
5485 return;
5486 if (TREE_CODE (TREE_TYPE (type)) != POINTER_TYPE)
5487 return;
5488
5489 in_type = type;
5490 in_otype = otype;
5491 is_const = TYPE_READONLY (TREE_TYPE (in_type));
5492 do
5493 {
5494 in_type = TREE_TYPE (in_type);
5495 in_otype = TREE_TYPE (in_otype);
5496 if ((TYPE_QUALS (in_type) &~ TYPE_QUALS (in_otype)) != 0
5497 && !is_const)
5498 {
2ee3cb35
MLI
5499 warning_at (loc, OPT_Wcast_qual,
5500 "to be safe all intermediate pointers in cast from "
5501 "%qT to %qT must be %<const%> qualified",
5502 otype, type);
67165eb3
ILT
5503 break;
5504 }
5505 if (is_const)
5506 is_const = TYPE_READONLY (in_type);
5507 }
5508 while (TREE_CODE (in_type) == POINTER_TYPE);
5509}
5510
c65e18d3
BE
5511/* Heuristic check if two parameter types can be considered ABI-equivalent. */
5512
5513static bool
5514c_safe_arg_type_equiv_p (tree t1, tree t2)
5515{
5516 t1 = TYPE_MAIN_VARIANT (t1);
5517 t2 = TYPE_MAIN_VARIANT (t2);
5518
5519 if (TREE_CODE (t1) == POINTER_TYPE
5520 && TREE_CODE (t2) == POINTER_TYPE)
5521 return true;
5522
5523 /* The signedness of the parameter matters only when an integral
5524 type smaller than int is promoted to int, otherwise only the
5525 precision of the parameter matters.
5526 This check should make sure that the callee does not see
5527 undefined values in argument registers. */
5528 if (INTEGRAL_TYPE_P (t1)
5529 && INTEGRAL_TYPE_P (t2)
5530 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
5531 && (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
5532 || !targetm.calls.promote_prototypes (NULL_TREE)
5533 || TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
5534 return true;
5535
5536 return comptypes (t1, t2);
5537}
5538
5539/* Check if a type cast between two function types can be considered safe. */
5540
5541static bool
5542c_safe_function_type_cast_p (tree t1, tree t2)
5543{
5544 if (TREE_TYPE (t1) == void_type_node &&
5545 TYPE_ARG_TYPES (t1) == void_list_node)
5546 return true;
5547
5548 if (TREE_TYPE (t2) == void_type_node &&
5549 TYPE_ARG_TYPES (t2) == void_list_node)
5550 return true;
5551
5552 if (!c_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
5553 return false;
5554
5555 for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
5556 t1 && t2;
5557 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
5558 if (!c_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
5559 return false;
5560
5561 return true;
5562}
5563
b8698a0f 5564/* Build an expression representing a cast to type TYPE of expression EXPR.
c2255bc4 5565 LOC is the location of the cast-- typically the open paren of the cast. */
400fbf9f 5566
3e4093b6 5567tree
c2255bc4 5568build_c_cast (location_t loc, tree type, tree expr)
3e4093b6 5569{
8ce94e44
JM
5570 tree value;
5571
5572 if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
5573 expr = TREE_OPERAND (expr, 0);
5574
5575 value = expr;
400fbf9f 5576
3e4093b6
RS
5577 if (type == error_mark_node || expr == error_mark_node)
5578 return error_mark_node;
400fbf9f 5579
3e4093b6
RS
5580 /* The ObjC front-end uses TYPE_MAIN_VARIANT to tie together types differing
5581 only in <protocol> qualifications. But when constructing cast expressions,
5582 the protocols do matter and must be kept around. */
700686fa
ZL
5583 if (objc_is_object_ptr (type) && objc_is_object_ptr (TREE_TYPE (expr)))
5584 return build1 (NOP_EXPR, type, expr);
5585
5586 type = TYPE_MAIN_VARIANT (type);
400fbf9f 5587
3e4093b6
RS
5588 if (TREE_CODE (type) == ARRAY_TYPE)
5589 {
c2255bc4 5590 error_at (loc, "cast specifies array type");
3e4093b6
RS
5591 return error_mark_node;
5592 }
400fbf9f 5593
3e4093b6
RS
5594 if (TREE_CODE (type) == FUNCTION_TYPE)
5595 {
c2255bc4 5596 error_at (loc, "cast specifies function type");
3e4093b6
RS
5597 return error_mark_node;
5598 }
400fbf9f 5599
808d6eaa
JM
5600 if (!VOID_TYPE_P (type))
5601 {
4f2e1536 5602 value = require_complete_type (loc, value);
808d6eaa
JM
5603 if (value == error_mark_node)
5604 return error_mark_node;
5605 }
5606
3e4093b6
RS
5607 if (type == TYPE_MAIN_VARIANT (TREE_TYPE (value)))
5608 {
3e636daf 5609 if (RECORD_OR_UNION_TYPE_P (type))
c1771a20 5610 pedwarn (loc, OPT_Wpedantic,
fcf73884 5611 "ISO C forbids casting nonscalar to the same type");
c77935ee
PP
5612
5613 /* Convert to remove any qualifiers from VALUE's type. */
5614 value = convert (type, value);
3e4093b6
RS
5615 }
5616 else if (TREE_CODE (type) == UNION_TYPE)
5617 {
5618 tree field;
400fbf9f 5619
910ad8de 5620 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
39ffbac9
VR
5621 if (TREE_TYPE (field) != error_mark_node
5622 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (field)),
5623 TYPE_MAIN_VARIANT (TREE_TYPE (value))))
3e4093b6
RS
5624 break;
5625
5626 if (field)
400fbf9f 5627 {
3e4093b6 5628 tree t;
e616f54d 5629 bool maybe_const = true;
3e4093b6 5630
c1771a20 5631 pedwarn (loc, OPT_Wpedantic, "ISO C forbids casts to union type");
e616f54d
JM
5632 t = c_fully_fold (value, false, &maybe_const);
5633 t = build_constructor_single (type, field, t);
5634 if (!maybe_const)
5635 t = c_wrap_maybe_const (t, true);
5636 t = digest_init (loc, type, t,
bbbbb16a 5637 NULL_TREE, false, true, 0);
3e4093b6
RS
5638 TREE_CONSTANT (t) = TREE_CONSTANT (value);
5639 return t;
400fbf9f 5640 }
c2255bc4 5641 error_at (loc, "cast to union type from type not present in union");
3e4093b6
RS
5642 return error_mark_node;
5643 }
5644 else
5645 {
5646 tree otype, ovalue;
400fbf9f 5647
3e4093b6 5648 if (type == void_type_node)
c2255bc4
AH
5649 {
5650 tree t = build1 (CONVERT_EXPR, type, value);
5651 SET_EXPR_LOCATION (t, loc);
5652 return t;
5653 }
400fbf9f 5654
3e4093b6 5655 otype = TREE_TYPE (value);
400fbf9f 5656
3e4093b6 5657 /* Optionally warn about potentially worrisome casts. */
3e4093b6
RS
5658 if (warn_cast_qual
5659 && TREE_CODE (type) == POINTER_TYPE
5660 && TREE_CODE (otype) == POINTER_TYPE)
2ee3cb35 5661 handle_warn_cast_qual (loc, type, otype);
400fbf9f 5662
36c5e70a
BE
5663 /* Warn about conversions between pointers to disjoint
5664 address spaces. */
5665 if (TREE_CODE (type) == POINTER_TYPE
5666 && TREE_CODE (otype) == POINTER_TYPE
5667 && !null_pointer_constant_p (value))
5668 {
5669 addr_space_t as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
5670 addr_space_t as_from = TYPE_ADDR_SPACE (TREE_TYPE (otype));
5671 addr_space_t as_common;
5672
5673 if (!addr_space_superset (as_to, as_from, &as_common))
5674 {
5675 if (ADDR_SPACE_GENERIC_P (as_from))
5676 warning_at (loc, 0, "cast to %s address space pointer "
5677 "from disjoint generic address space pointer",
5678 c_addr_space_name (as_to));
5679
5680 else if (ADDR_SPACE_GENERIC_P (as_to))
5681 warning_at (loc, 0, "cast to generic address space pointer "
5682 "from disjoint %s address space pointer",
5683 c_addr_space_name (as_from));
5684
5685 else
5686 warning_at (loc, 0, "cast to %s address space pointer "
5687 "from disjoint %s address space pointer",
5688 c_addr_space_name (as_to),
5689 c_addr_space_name (as_from));
5690 }
5691 }
5692
3e4093b6 5693 /* Warn about possible alignment problems. */
1d933576 5694 if ((STRICT_ALIGNMENT || warn_cast_align == 2)
3e4093b6
RS
5695 && TREE_CODE (type) == POINTER_TYPE
5696 && TREE_CODE (otype) == POINTER_TYPE
5697 && TREE_CODE (TREE_TYPE (otype)) != VOID_TYPE
5698 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
5699 /* Don't warn about opaque types, where the actual alignment
5700 restriction is unknown. */
3e636daf 5701 && !(RECORD_OR_UNION_TYPE_P (TREE_TYPE (otype))
3e4093b6 5702 && TYPE_MODE (TREE_TYPE (otype)) == VOIDmode)
1d933576
BE
5703 && min_align_of_type (TREE_TYPE (type))
5704 > min_align_of_type (TREE_TYPE (otype)))
c2255bc4
AH
5705 warning_at (loc, OPT_Wcast_align,
5706 "cast increases required alignment of target type");
e9a25f70 5707
3176a0c2 5708 if (TREE_CODE (type) == INTEGER_TYPE
3e4093b6 5709 && TREE_CODE (otype) == POINTER_TYPE
8d1c7aef 5710 && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
c22cacf3
MS
5711 /* Unlike conversion of integers to pointers, where the
5712 warning is disabled for converting constants because
5713 of cases such as SIG_*, warn about converting constant
5714 pointers to integers. In some cases it may cause unwanted
8d1c7aef 5715 sign extension, and a warning is appropriate. */
c2255bc4
AH
5716 warning_at (loc, OPT_Wpointer_to_int_cast,
5717 "cast from pointer to integer of different size");
400fbf9f 5718
3176a0c2 5719 if (TREE_CODE (value) == CALL_EXPR
3e4093b6 5720 && TREE_CODE (type) != TREE_CODE (otype))
c2255bc4
AH
5721 warning_at (loc, OPT_Wbad_function_cast,
5722 "cast from function call of type %qT "
5723 "to non-matching type %qT", otype, type);
400fbf9f 5724
3176a0c2 5725 if (TREE_CODE (type) == POINTER_TYPE
3e4093b6
RS
5726 && TREE_CODE (otype) == INTEGER_TYPE
5727 && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
5728 /* Don't warn about converting any constant. */
5729 && !TREE_CONSTANT (value))
c2255bc4
AH
5730 warning_at (loc,
5731 OPT_Wint_to_pointer_cast, "cast to pointer from integer "
5732 "of different size");
400fbf9f 5733
79bedddc 5734 if (warn_strict_aliasing <= 2)
0444aa9c 5735 strict_aliasing_warning (EXPR_LOCATION (value), type, expr);
400fbf9f 5736
3897f229
JM
5737 /* If pedantic, warn for conversions between function and object
5738 pointer types, except for converting a null pointer constant
5739 to function pointer type. */
5740 if (pedantic
5741 && TREE_CODE (type) == POINTER_TYPE
5742 && TREE_CODE (otype) == POINTER_TYPE
5743 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5744 && TREE_CODE (TREE_TYPE (type)) != FUNCTION_TYPE)
c1771a20 5745 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5746 "conversion of function pointer to object pointer type");
3897f229
JM
5747
5748 if (pedantic
5749 && TREE_CODE (type) == POINTER_TYPE
5750 && TREE_CODE (otype) == POINTER_TYPE
5751 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5752 && TREE_CODE (TREE_TYPE (otype)) != FUNCTION_TYPE
6aa3c60d 5753 && !null_pointer_constant_p (value))
c1771a20 5754 pedwarn (loc, OPT_Wpedantic, "ISO C forbids "
fcf73884 5755 "conversion of object pointer to function pointer type");
3897f229 5756
c65e18d3
BE
5757 if (TREE_CODE (type) == POINTER_TYPE
5758 && TREE_CODE (otype) == POINTER_TYPE
5759 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
5760 && TREE_CODE (TREE_TYPE (otype)) == FUNCTION_TYPE
5761 && !c_safe_function_type_cast_p (TREE_TYPE (type),
5762 TREE_TYPE (otype)))
5763 warning_at (loc, OPT_Wcast_function_type,
5764 "cast between incompatible function types"
5765 " from %qT to %qT", otype, type);
5766
3e4093b6 5767 ovalue = value;
3e4093b6 5768 value = convert (type, value);
400fbf9f 5769
3e4093b6 5770 /* Ignore any integer overflow caused by the cast. */
928c19bb 5771 if (TREE_CODE (value) == INTEGER_CST && !FLOAT_TYPE_P (otype))
3e4093b6 5772 {
8bcd6380 5773 if (CONSTANT_CLASS_P (ovalue) && TREE_OVERFLOW (ovalue))
6414bad6 5774 {
8bcd6380
RS
5775 if (!TREE_OVERFLOW (value))
5776 {
5777 /* Avoid clobbering a shared constant. */
5778 value = copy_node (value);
5779 TREE_OVERFLOW (value) = TREE_OVERFLOW (ovalue);
5780 }
6414bad6 5781 }
8bcd6380 5782 else if (TREE_OVERFLOW (value))
d8e1f97b 5783 /* Reset VALUE's overflow flags, ensuring constant sharing. */
8e6cdc90 5784 value = wide_int_to_tree (TREE_TYPE (value), wi::to_wide (value));
3e4093b6
RS
5785 }
5786 }
400fbf9f 5787
53cd18ec 5788 /* Don't let a cast be an lvalue. */
9482b620 5789 if (lvalue_p (value))
db3927fb 5790 value = non_lvalue_loc (loc, value);
e9a25f70 5791
928c19bb
JM
5792 /* Don't allow the results of casting to floating-point or complex
5793 types be confused with actual constants, or casts involving
5794 integer and pointer types other than direct integer-to-integer
5795 and integer-to-pointer be confused with integer constant
5796 expressions and null pointer constants. */
5797 if (TREE_CODE (value) == REAL_CST
5798 || TREE_CODE (value) == COMPLEX_CST
5799 || (TREE_CODE (value) == INTEGER_CST
5800 && !((TREE_CODE (expr) == INTEGER_CST
5801 && INTEGRAL_TYPE_P (TREE_TYPE (expr)))
5802 || TREE_CODE (expr) == REAL_CST
5803 || TREE_CODE (expr) == COMPLEX_CST)))
5804 value = build1 (NOP_EXPR, type, value);
5805
21ba0cea 5806 protected_set_expr_location (value, loc);
3e4093b6 5807 return value;
400fbf9f
JW
5808}
5809
c2255bc4
AH
5810/* Interpret a cast of expression EXPR to type TYPE. LOC is the
5811 location of the open paren of the cast, or the position of the cast
5812 expr. */
3e4093b6 5813tree
c2255bc4 5814c_cast_expr (location_t loc, struct c_type_name *type_name, tree expr)
400fbf9f 5815{
f8893e47 5816 tree type;
928c19bb
JM
5817 tree type_expr = NULL_TREE;
5818 bool type_expr_const = true;
5819 tree ret;
3e4093b6 5820 int saved_wsp = warn_strict_prototypes;
c5c76735 5821
3e4093b6
RS
5822 /* This avoids warnings about unprototyped casts on
5823 integers. E.g. "#define SIG_DFL (void(*)())0". */
5824 if (TREE_CODE (expr) == INTEGER_CST)
5825 warn_strict_prototypes = 0;
928c19bb 5826 type = groktypename (type_name, &type_expr, &type_expr_const);
3e4093b6 5827 warn_strict_prototypes = saved_wsp;
c5c76735 5828
1807ffc1
MS
5829 if (TREE_CODE (expr) == ADDR_EXPR && !VOID_TYPE_P (type)
5830 && reject_gcc_builtin (expr))
5831 return error_mark_node;
5832
c2255bc4 5833 ret = build_c_cast (loc, type, expr);
928c19bb
JM
5834 if (type_expr)
5835 {
9f33203d
JM
5836 bool inner_expr_const = true;
5837 ret = c_fully_fold (ret, require_constant_value, &inner_expr_const);
928c19bb 5838 ret = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (ret), type_expr, ret);
9f33203d
JM
5839 C_MAYBE_CONST_EXPR_NON_CONST (ret) = !(type_expr_const
5840 && inner_expr_const);
c2255bc4 5841 SET_EXPR_LOCATION (ret, loc);
928c19bb 5842 }
24b97832 5843
21ba0cea
MP
5844 if (!EXPR_HAS_LOCATION (ret))
5845 protected_set_expr_location (ret, loc);
24b97832 5846
9e5b2115
PB
5847 /* C++ does not permits types to be defined in a cast, but it
5848 allows references to incomplete types. */
5849 if (warn_cxx_compat && type_name->specs->typespec_kind == ctsk_tagdef)
24b97832
ILT
5850 warning_at (loc, OPT_Wc___compat,
5851 "defining a type in a cast is invalid in C++");
5852
928c19bb 5853 return ret;
400fbf9f 5854}
3e4093b6
RS
5855\f
5856/* Build an assignment expression of lvalue LHS from value RHS.
32e8bb8e
ILT
5857 If LHS_ORIGTYPE is not NULL, it is the original type of LHS, which
5858 may differ from TREE_TYPE (LHS) for an enum bitfield.
3e4093b6
RS
5859 MODIFYCODE is the code for a binary operator that we use
5860 to combine the old value of LHS with RHS to get the new value.
c9f9eb5d 5861 Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
bbbbb16a
ILT
5862 If RHS_ORIGTYPE is not NULL_TREE, it is the original type of RHS,
5863 which may differ from TREE_TYPE (RHS) for an enum value.
c9f9eb5d 5864
c2255bc4
AH
5865 LOCATION is the location of the MODIFYCODE operator.
5866 RHS_LOC is the location of the RHS. */
2f6e4e97 5867
3e4093b6 5868tree
32e8bb8e 5869build_modify_expr (location_t location, tree lhs, tree lhs_origtype,
b8698a0f 5870 enum tree_code modifycode,
c2255bc4 5871 location_t rhs_loc, tree rhs, tree rhs_origtype)
400fbf9f 5872{
3e4093b6
RS
5873 tree result;
5874 tree newrhs;
241f845a 5875 tree rhseval = NULL_TREE;
3e4093b6
RS
5876 tree lhstype = TREE_TYPE (lhs);
5877 tree olhstype = lhstype;
928c19bb 5878 bool npc;
267bac10 5879 bool is_atomic_op;
e9a25f70 5880
3e4093b6 5881 /* Types that aren't fully specified cannot be used in assignments. */
4f2e1536 5882 lhs = require_complete_type (location, lhs);
e9a25f70 5883
3e4093b6
RS
5884 /* Avoid duplicate error messages from operands that had errors. */
5885 if (TREE_CODE (lhs) == ERROR_MARK || TREE_CODE (rhs) == ERROR_MARK)
5886 return error_mark_node;
400fbf9f 5887
4c2ecab0
JM
5888 /* Ensure an error for assigning a non-lvalue array to an array in
5889 C90. */
5890 if (TREE_CODE (lhstype) == ARRAY_TYPE)
5891 {
5892 error_at (location, "assignment to expression with array type");
5893 return error_mark_node;
5894 }
5895
46a88c12 5896 /* For ObjC properties, defer this check. */
7bd11157 5897 if (!objc_is_property_ref (lhs) && !lvalue_or_else (location, lhs, lv_assign))
c0bcacec
VR
5898 return error_mark_node;
5899
267bac10
JM
5900 is_atomic_op = really_atomic_lvalue (lhs);
5901
3e4093b6 5902 newrhs = rhs;
400fbf9f 5903
928c19bb
JM
5904 if (TREE_CODE (lhs) == C_MAYBE_CONST_EXPR)
5905 {
5906 tree inner = build_modify_expr (location, C_MAYBE_CONST_EXPR_EXPR (lhs),
c2255bc4 5907 lhs_origtype, modifycode, rhs_loc, rhs,
32e8bb8e 5908 rhs_origtype);
928c19bb
JM
5909 if (inner == error_mark_node)
5910 return error_mark_node;
5911 result = build2 (C_MAYBE_CONST_EXPR, TREE_TYPE (inner),
5912 C_MAYBE_CONST_EXPR_PRE (lhs), inner);
5913 gcc_assert (!C_MAYBE_CONST_EXPR_INT_OPERANDS (lhs));
5914 C_MAYBE_CONST_EXPR_NON_CONST (result) = 1;
5915 protected_set_expr_location (result, location);
5916 return result;
5917 }
5918
3e4093b6
RS
5919 /* If a binary op has been requested, combine the old LHS value with the RHS
5920 producing the value we should actually store into the LHS. */
5921
5922 if (modifycode != NOP_EXPR)
400fbf9f 5923 {
f9c59f7e 5924 lhs = c_fully_fold (lhs, false, NULL, true);
3e4093b6 5925 lhs = stabilize_reference (lhs);
bbbbb16a 5926
267bac10
JM
5927 /* Construct the RHS for any non-atomic compound assignemnt. */
5928 if (!is_atomic_op)
5929 {
241f845a
JJ
5930 /* If in LHS op= RHS the RHS has side-effects, ensure they
5931 are preevaluated before the rest of the assignment expression's
5932 side-effects, because RHS could contain e.g. function calls
5933 that modify LHS. */
5934 if (TREE_SIDE_EFFECTS (rhs))
5935 {
e035be33
JM
5936 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5937 newrhs = save_expr (TREE_OPERAND (rhs, 0));
5938 else
5939 newrhs = save_expr (rhs);
241f845a 5940 rhseval = newrhs;
e035be33
JM
5941 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
5942 newrhs = build1 (EXCESS_PRECISION_EXPR, TREE_TYPE (rhs),
5943 newrhs);
241f845a 5944 }
267bac10 5945 newrhs = build_binary_op (location,
30af3a2b 5946 modifycode, lhs, newrhs, true);
267bac10
JM
5947
5948 /* The original type of the right hand side is no longer
5949 meaningful. */
5950 rhs_origtype = NULL_TREE;
5951 }
400fbf9f 5952 }
400fbf9f 5953
668ea4b1
IS
5954 if (c_dialect_objc ())
5955 {
46a88c12
NP
5956 /* Check if we are modifying an Objective-C property reference;
5957 if so, we need to generate setter calls. */
e035be33
JM
5958 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
5959 result = objc_maybe_build_modify_expr (lhs, TREE_OPERAND (newrhs, 0));
5960 else
5961 result = objc_maybe_build_modify_expr (lhs, newrhs);
668ea4b1 5962 if (result)
241f845a 5963 goto return_result;
46a88c12
NP
5964
5965 /* Else, do the check that we postponed for Objective-C. */
7bd11157 5966 if (!lvalue_or_else (location, lhs, lv_assign))
668ea4b1
IS
5967 return error_mark_node;
5968 }
5969
9bf24266 5970 /* Give an error for storing in something that is 'const'. */
bbbd6700 5971
f37acdf9 5972 if (TYPE_READONLY (lhstype)
3e636daf 5973 || (RECORD_OR_UNION_TYPE_P (lhstype)
3e4093b6 5974 && C_TYPE_FIELDS_READONLY (lhstype)))
953ff289 5975 {
c02065fc 5976 readonly_error (location, lhs, lv_assign);
953ff289
DN
5977 return error_mark_node;
5978 }
f37acdf9
JM
5979 else if (TREE_READONLY (lhs))
5980 readonly_warning (lhs, lv_assign);
bbbd6700 5981
3e4093b6
RS
5982 /* If storing into a structure or union member,
5983 it has probably been given type `int'.
5984 Compute the type that would go with
5985 the actual amount of storage the member occupies. */
bbbd6700 5986
3e4093b6
RS
5987 if (TREE_CODE (lhs) == COMPONENT_REF
5988 && (TREE_CODE (lhstype) == INTEGER_TYPE
5989 || TREE_CODE (lhstype) == BOOLEAN_TYPE
5990 || TREE_CODE (lhstype) == REAL_TYPE
5991 || TREE_CODE (lhstype) == ENUMERAL_TYPE))
5992 lhstype = TREE_TYPE (get_unwidened (lhs, 0));
400fbf9f 5993
3e4093b6
RS
5994 /* If storing in a field that is in actuality a short or narrower than one,
5995 we must store in the field in its actual type. */
5996
5997 if (lhstype != TREE_TYPE (lhs))
5998 {
5999 lhs = copy_node (lhs);
6000 TREE_TYPE (lhs) = lhstype;
400fbf9f 6001 }
400fbf9f 6002
32e8bb8e
ILT
6003 /* Issue -Wc++-compat warnings about an assignment to an enum type
6004 when LHS does not have its original type. This happens for,
6005 e.g., an enum bitfield in a struct. */
6006 if (warn_cxx_compat
6007 && lhs_origtype != NULL_TREE
6008 && lhs_origtype != lhstype
6009 && TREE_CODE (lhs_origtype) == ENUMERAL_TYPE)
6010 {
6011 tree checktype = (rhs_origtype != NULL_TREE
6012 ? rhs_origtype
6013 : TREE_TYPE (rhs));
6014 if (checktype != error_mark_node
267bac10
JM
6015 && (TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (lhs_origtype)
6016 || (is_atomic_op && modifycode != NOP_EXPR)))
32e8bb8e
ILT
6017 warning_at (location, OPT_Wc___compat,
6018 "enum conversion in assignment is invalid in C++");
6019 }
6020
267bac10
JM
6021 /* If the lhs is atomic, remove that qualifier. */
6022 if (is_atomic_op)
6023 {
6024 lhstype = build_qualified_type (lhstype,
6025 (TYPE_QUALS (lhstype)
6026 & ~TYPE_QUAL_ATOMIC));
6027 olhstype = build_qualified_type (olhstype,
6028 (TYPE_QUALS (lhstype)
6029 & ~TYPE_QUAL_ATOMIC));
6030 }
6031
8ce94e44
JM
6032 /* Convert new value to destination type. Fold it first, then
6033 restore any excess precision information, for the sake of
6034 conversion warnings. */
400fbf9f 6035
267bac10
JM
6036 if (!(is_atomic_op && modifycode != NOP_EXPR))
6037 {
e035be33
JM
6038 tree rhs_semantic_type = NULL_TREE;
6039 if (TREE_CODE (newrhs) == EXCESS_PRECISION_EXPR)
6040 {
6041 rhs_semantic_type = TREE_TYPE (newrhs);
6042 newrhs = TREE_OPERAND (newrhs, 0);
6043 }
267bac10
JM
6044 npc = null_pointer_constant_p (newrhs);
6045 newrhs = c_fully_fold (newrhs, false, NULL);
6046 if (rhs_semantic_type)
6047 newrhs = build1 (EXCESS_PRECISION_EXPR, rhs_semantic_type, newrhs);
68fca595
MP
6048 newrhs = convert_for_assignment (location, rhs_loc, lhstype, newrhs,
6049 rhs_origtype, ic_assign, npc,
6050 NULL_TREE, NULL_TREE, 0);
267bac10
JM
6051 if (TREE_CODE (newrhs) == ERROR_MARK)
6052 return error_mark_node;
6053 }
400fbf9f 6054
6e955430
ZL
6055 /* Emit ObjC write barrier, if necessary. */
6056 if (c_dialect_objc () && flag_objc_gc)
6057 {
6058 result = objc_generate_write_barrier (lhs, modifycode, newrhs);
6059 if (result)
c9f9eb5d
AH
6060 {
6061 protected_set_expr_location (result, location);
241f845a 6062 goto return_result;
c9f9eb5d 6063 }
6e955430
ZL
6064 }
6065
ea4b7848 6066 /* Scan operands. */
400fbf9f 6067
267bac10
JM
6068 if (is_atomic_op)
6069 result = build_atomic_assign (location, lhs, modifycode, newrhs, false);
6070 else
6071 {
6072 result = build2 (MODIFY_EXPR, lhstype, lhs, newrhs);
6073 TREE_SIDE_EFFECTS (result) = 1;
6074 protected_set_expr_location (result, location);
6075 }
400fbf9f 6076
3e4093b6
RS
6077 /* If we got the LHS in a different type for storing in,
6078 convert the result back to the nominal type of LHS
6079 so that the value we return always has the same type
6080 as the LHS argument. */
e855c5ce 6081
3e4093b6 6082 if (olhstype == TREE_TYPE (result))
241f845a 6083 goto return_result;
c9f9eb5d 6084
68fca595
MP
6085 result = convert_for_assignment (location, rhs_loc, olhstype, result,
6086 rhs_origtype, ic_assign, false, NULL_TREE,
6087 NULL_TREE, 0);
c9f9eb5d 6088 protected_set_expr_location (result, location);
241f845a
JJ
6089
6090return_result:
6091 if (rhseval)
6092 result = build2 (COMPOUND_EXPR, TREE_TYPE (result), rhseval, result);
c9f9eb5d 6093 return result;
3e4093b6
RS
6094}
6095\f
478a1c5b
ILT
6096/* Return whether STRUCT_TYPE has an anonymous field with type TYPE.
6097 This is used to implement -fplan9-extensions. */
6098
6099static bool
6100find_anonymous_field_with_type (tree struct_type, tree type)
6101{
6102 tree field;
6103 bool found;
6104
3e636daf 6105 gcc_assert (RECORD_OR_UNION_TYPE_P (struct_type));
478a1c5b
ILT
6106 found = false;
6107 for (field = TYPE_FIELDS (struct_type);
6108 field != NULL_TREE;
6109 field = TREE_CHAIN (field))
6110 {
267bac10
JM
6111 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6112 ? c_build_qualified_type (TREE_TYPE (field),
6113 TYPE_QUAL_ATOMIC)
6114 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
478a1c5b 6115 if (DECL_NAME (field) == NULL
267bac10 6116 && comptypes (type, fieldtype))
478a1c5b
ILT
6117 {
6118 if (found)
6119 return false;
6120 found = true;
6121 }
6122 else if (DECL_NAME (field) == NULL
3e636daf 6123 && RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
478a1c5b
ILT
6124 && find_anonymous_field_with_type (TREE_TYPE (field), type))
6125 {
6126 if (found)
6127 return false;
6128 found = true;
6129 }
6130 }
6131 return found;
6132}
6133
6134/* RHS is an expression whose type is pointer to struct. If there is
6135 an anonymous field in RHS with type TYPE, then return a pointer to
6136 that field in RHS. This is used with -fplan9-extensions. This
6137 returns NULL if no conversion could be found. */
6138
6139static tree
6140convert_to_anonymous_field (location_t location, tree type, tree rhs)
6141{
6142 tree rhs_struct_type, lhs_main_type;
6143 tree field, found_field;
6144 bool found_sub_field;
6145 tree ret;
6146
6147 gcc_assert (POINTER_TYPE_P (TREE_TYPE (rhs)));
6148 rhs_struct_type = TREE_TYPE (TREE_TYPE (rhs));
3e636daf 6149 gcc_assert (RECORD_OR_UNION_TYPE_P (rhs_struct_type));
478a1c5b
ILT
6150
6151 gcc_assert (POINTER_TYPE_P (type));
267bac10
JM
6152 lhs_main_type = (TYPE_ATOMIC (TREE_TYPE (type))
6153 ? c_build_qualified_type (TREE_TYPE (type),
6154 TYPE_QUAL_ATOMIC)
6155 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
478a1c5b
ILT
6156
6157 found_field = NULL_TREE;
6158 found_sub_field = false;
6159 for (field = TYPE_FIELDS (rhs_struct_type);
6160 field != NULL_TREE;
6161 field = TREE_CHAIN (field))
6162 {
6163 if (DECL_NAME (field) != NULL_TREE
3e636daf 6164 || !RECORD_OR_UNION_TYPE_P (TREE_TYPE (field)))
478a1c5b 6165 continue;
267bac10
JM
6166 tree fieldtype = (TYPE_ATOMIC (TREE_TYPE (field))
6167 ? c_build_qualified_type (TREE_TYPE (field),
6168 TYPE_QUAL_ATOMIC)
6169 : TYPE_MAIN_VARIANT (TREE_TYPE (field)));
6170 if (comptypes (lhs_main_type, fieldtype))
478a1c5b
ILT
6171 {
6172 if (found_field != NULL_TREE)
6173 return NULL_TREE;
6174 found_field = field;
6175 }
6176 else if (find_anonymous_field_with_type (TREE_TYPE (field),
6177 lhs_main_type))
6178 {
6179 if (found_field != NULL_TREE)
6180 return NULL_TREE;
6181 found_field = field;
6182 found_sub_field = true;
6183 }
6184 }
6185
6186 if (found_field == NULL_TREE)
6187 return NULL_TREE;
6188
6189 ret = fold_build3_loc (location, COMPONENT_REF, TREE_TYPE (found_field),
6190 build_fold_indirect_ref (rhs), found_field,
6191 NULL_TREE);
6192 ret = build_fold_addr_expr_loc (location, ret);
6193
6194 if (found_sub_field)
6195 {
6196 ret = convert_to_anonymous_field (location, type, ret);
6197 gcc_assert (ret != NULL_TREE);
6198 }
6199
6200 return ret;
6201}
6202
63bc4e87
MP
6203/* Issue an error message for a bad initializer component.
6204 GMSGID identifies the message.
6205 The component name is taken from the spelling stack. */
6206
6207static void
ea58ef42 6208error_init (location_t loc, const char *gmsgid)
63bc4e87
MP
6209{
6210 char *ofwhat;
6211
097f82ec
DM
6212 auto_diagnostic_group d;
6213
63bc4e87 6214 /* The gmsgid may be a format string with %< and %>. */
ea58ef42 6215 error_at (loc, gmsgid);
63bc4e87
MP
6216 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
6217 if (*ofwhat)
d7ff7ae5 6218 inform (loc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6219}
6220
6221/* Issue a pedantic warning for a bad initializer component. OPT is
6222 the option OPT_* (from options.h) controlling this warning or 0 if
6223 it is unconditionally given. GMSGID identifies the message. The
6224 component name is taken from the spelling stack. */
6225
296c53ac
MP
6226static void ATTRIBUTE_GCC_DIAG (3,0)
6227pedwarn_init (location_t loc, int opt, const char *gmsgid, ...)
63bc4e87 6228{
79063edd
MS
6229 /* Use the location where a macro was expanded rather than where
6230 it was defined to make sure macros defined in system headers
6231 but used incorrectly elsewhere are diagnosed. */
6232 source_location exploc = expansion_point_location_if_in_system_header (loc);
097f82ec 6233 auto_diagnostic_group d;
296c53ac
MP
6234 va_list ap;
6235 va_start (ap, gmsgid);
6236 bool warned = emit_diagnostic_valist (DK_PEDWARN, exploc, opt, gmsgid, &ap);
6237 va_end (ap);
6238 char *ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6239 if (*ofwhat && warned)
79063edd 6240 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6241}
6242
6243/* Issue a warning for a bad initializer component.
6244
6245 OPT is the OPT_W* value corresponding to the warning option that
6246 controls this warning. GMSGID identifies the message. The
6247 component name is taken from the spelling stack. */
6248
6249static void
6250warning_init (location_t loc, int opt, const char *gmsgid)
6251{
6252 char *ofwhat;
d7ff7ae5 6253 bool warned;
63bc4e87 6254
097f82ec
DM
6255 auto_diagnostic_group d;
6256
79063edd
MS
6257 /* Use the location where a macro was expanded rather than where
6258 it was defined to make sure macros defined in system headers
6259 but used incorrectly elsewhere are diagnosed. */
6260 source_location exploc = expansion_point_location_if_in_system_header (loc);
6261
63bc4e87 6262 /* The gmsgid may be a format string with %< and %>. */
79063edd 6263 warned = warning_at (exploc, opt, gmsgid);
63bc4e87 6264 ofwhat = print_spelling ((char *) alloca (spelling_length () + 1));
d7ff7ae5 6265 if (*ofwhat && warned)
79063edd 6266 inform (exploc, "(near initialization for %qs)", ofwhat);
63bc4e87
MP
6267}
6268\f
6269/* If TYPE is an array type and EXPR is a parenthesized string
6270 constant, warn if pedantic that EXPR is being used to initialize an
6271 object of type TYPE. */
6272
6273void
d033409e 6274maybe_warn_string_init (location_t loc, tree type, struct c_expr expr)
63bc4e87
MP
6275{
6276 if (pedantic
6277 && TREE_CODE (type) == ARRAY_TYPE
6278 && TREE_CODE (expr.value) == STRING_CST
6279 && expr.original_code != STRING_CST)
d033409e 6280 pedwarn_init (loc, OPT_Wpedantic,
63bc4e87
MP
6281 "array initialized from parenthesized string constant");
6282}
6283
8139a48e
DM
6284/* Attempt to locate the parameter with the given index within FNDECL,
6285 returning DECL_SOURCE_LOCATION (FNDECL) if it can't be found. */
6286
6287static location_t
6288get_fndecl_argument_location (tree fndecl, int argnum)
6289{
6290 int i;
6291 tree param;
6292
6293 /* Locate param by index within DECL_ARGUMENTS (fndecl). */
6294 for (i = 0, param = DECL_ARGUMENTS (fndecl);
6295 i < argnum && param;
6296 i++, param = TREE_CHAIN (param))
6297 ;
6298
6299 /* If something went wrong (e.g. if we have a builtin and thus no arguments),
6300 return DECL_SOURCE_LOCATION (FNDECL). */
6301 if (param == NULL)
6302 return DECL_SOURCE_LOCATION (fndecl);
6303
6304 return DECL_SOURCE_LOCATION (param);
6305}
6306
6307/* Issue a note about a mismatching argument for parameter PARMNUM
6308 to FUNDECL, for types EXPECTED_TYPE and ACTUAL_TYPE.
6309 Attempt to issue the note at the pertinent parameter of the decl;
6310 failing that issue it at the location of FUNDECL; failing that
6311 issue it at PLOC. */
6312
6313static void
6314inform_for_arg (tree fundecl, location_t ploc, int parmnum,
6315 tree expected_type, tree actual_type)
6316{
6317 location_t loc;
6318 if (fundecl && !DECL_IS_BUILTIN (fundecl))
6319 loc = get_fndecl_argument_location (fundecl, parmnum - 1);
6320 else
6321 loc = ploc;
6322
6323 inform (loc,
6324 "expected %qT but argument is of type %qT",
6325 expected_type, actual_type);
6326}
6327
bbbbb16a
ILT
6328/* Convert value RHS to type TYPE as preparation for an assignment to
6329 an lvalue of type TYPE. If ORIGTYPE is not NULL_TREE, it is the
6330 original type of RHS; this differs from TREE_TYPE (RHS) for enum
6331 types. NULL_POINTER_CONSTANT says whether RHS was a null pointer
6332 constant before any folding.
3e4093b6
RS
6333 The real work of conversion is done by `convert'.
6334 The purpose of this function is to generate error messages
6335 for assignments that are not allowed in C.
2ac2f164
JM
6336 ERRTYPE says whether it is argument passing, assignment,
6337 initialization or return.
2f6e4e97 6338
aabef2de
MP
6339 In the following example, '~' denotes where EXPR_LOC and '^' where
6340 LOCATION point to:
6341
6342 f (var); [ic_argpass]
6343 ^ ~~~
6344 x = var; [ic_assign]
6345 ^ ~~~;
6346 int x = var; [ic_init]
6347 ^^^
6348 return x; [ic_return]
6349 ^
6350
2ac2f164 6351 FUNCTION is a tree for the function being called.
3e4093b6 6352 PARMNUM is the number of the argument, for printing in error messages. */
cb3ca04e 6353
3e4093b6 6354static tree
81e5eca8 6355convert_for_assignment (location_t location, location_t expr_loc, tree type,
68fca595 6356 tree rhs, tree origtype, enum impl_conv errtype,
744aa42f
ILT
6357 bool null_pointer_constant, tree fundecl,
6358 tree function, int parmnum)
3e4093b6
RS
6359{
6360 enum tree_code codel = TREE_CODE (type);
8ce94e44 6361 tree orig_rhs = rhs;
3e4093b6
RS
6362 tree rhstype;
6363 enum tree_code coder;
2ac2f164 6364 tree rname = NULL_TREE;
58393038 6365 bool objc_ok = false;
2ac2f164 6366
1c7485af
MP
6367 /* Use the expansion point location to handle cases such as user's
6368 function returning a wrong-type macro defined in a system header. */
6369 location = expansion_point_location_if_in_system_header (location);
6370
6b4ef5c1 6371 if (errtype == ic_argpass)
2ac2f164
JM
6372 {
6373 tree selector;
6374 /* Change pointer to function to the function itself for
6375 diagnostics. */
6376 if (TREE_CODE (function) == ADDR_EXPR
6377 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
6378 function = TREE_OPERAND (function, 0);
6379
6380 /* Handle an ObjC selector specially for diagnostics. */
6381 selector = objc_message_selector ();
6382 rname = function;
6383 if (selector && parmnum > 2)
6384 {
6385 rname = selector;
6386 parmnum -= 2;
6387 }
6388 }
6389
6390 /* This macro is used to emit diagnostics to ensure that all format
6391 strings are complete sentences, visible to gettext and checked at
6392 compile time. */
768952be 6393#define PEDWARN_FOR_ASSIGNMENT(LOCATION, PLOC, OPT, AR, AS, IN, RE) \
1e053dfe
MLI
6394 do { \
6395 switch (errtype) \
6396 { \
6397 case ic_argpass: \
097f82ec
DM
6398 { \
6399 auto_diagnostic_group d; \
6400 if (pedwarn (PLOC, OPT, AR, parmnum, rname)) \
6401 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6402 } \
1e053dfe 6403 break; \
1e053dfe
MLI
6404 case ic_assign: \
6405 pedwarn (LOCATION, OPT, AS); \
6406 break; \
6407 case ic_init: \
6a8f4e12 6408 pedwarn_init (LOCATION, OPT, IN); \
1e053dfe
MLI
6409 break; \
6410 case ic_return: \
d033409e 6411 pedwarn (LOCATION, OPT, RE); \
1e053dfe
MLI
6412 break; \
6413 default: \
6414 gcc_unreachable (); \
6415 } \
2ac2f164 6416 } while (0)
cb3ca04e 6417
49706e39
MLI
6418 /* This macro is used to emit diagnostics to ensure that all format
6419 strings are complete sentences, visible to gettext and checked at
768952be 6420 compile time. It is the same as PEDWARN_FOR_ASSIGNMENT but with an
49706e39 6421 extra parameter to enumerate qualifiers. */
768952be 6422#define PEDWARN_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
49706e39
MLI
6423 do { \
6424 switch (errtype) \
6425 { \
6426 case ic_argpass: \
097f82ec
DM
6427 { \
6428 auto_diagnostic_group d; \
6429 if (pedwarn (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6430 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6431 } \
49706e39
MLI
6432 break; \
6433 case ic_assign: \
5c1bc275 6434 pedwarn (LOCATION, OPT, AS, QUALS); \
49706e39
MLI
6435 break; \
6436 case ic_init: \
5c1bc275 6437 pedwarn (LOCATION, OPT, IN, QUALS); \
49706e39
MLI
6438 break; \
6439 case ic_return: \
5c1bc275 6440 pedwarn (LOCATION, OPT, RE, QUALS); \
49706e39
MLI
6441 break; \
6442 default: \
6443 gcc_unreachable (); \
6444 } \
6445 } while (0)
6446
768952be
MU
6447 /* This macro is used to emit diagnostics to ensure that all format
6448 strings are complete sentences, visible to gettext and checked at
6449 compile time. It is the same as PEDWARN_FOR_QUALIFIERS but uses
6450 warning_at instead of pedwarn. */
6451#define WARNING_FOR_QUALIFIERS(LOCATION, PLOC, OPT, AR, AS, IN, RE, QUALS) \
6452 do { \
6453 switch (errtype) \
6454 { \
6455 case ic_argpass: \
097f82ec
DM
6456 { \
6457 auto_diagnostic_group d; \
6458 if (warning_at (PLOC, OPT, AR, parmnum, rname, QUALS)) \
6459 inform_for_arg (fundecl, (PLOC), parmnum, type, rhstype); \
6460 } \
768952be
MU
6461 break; \
6462 case ic_assign: \
6463 warning_at (LOCATION, OPT, AS, QUALS); \
6464 break; \
6465 case ic_init: \
6466 warning_at (LOCATION, OPT, IN, QUALS); \
6467 break; \
6468 case ic_return: \
6469 warning_at (LOCATION, OPT, RE, QUALS); \
6470 break; \
6471 default: \
6472 gcc_unreachable (); \
6473 } \
6474 } while (0)
6475
8ce94e44
JM
6476 if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
6477 rhs = TREE_OPERAND (rhs, 0);
6478
3e4093b6
RS
6479 rhstype = TREE_TYPE (rhs);
6480 coder = TREE_CODE (rhstype);
6481
6482 if (coder == ERROR_MARK)
6483 return error_mark_node;
6484
58393038
ZL
6485 if (c_dialect_objc ())
6486 {
6487 int parmno;
6488
6489 switch (errtype)
6490 {
6491 case ic_return:
6492 parmno = 0;
6493 break;
6494
6495 case ic_assign:
6496 parmno = -1;
6497 break;
6498
6499 case ic_init:
6500 parmno = -2;
6501 break;
6502
6503 default:
6504 parmno = parmnum;
6505 break;
6506 }
6507
6508 objc_ok = objc_compare_types (type, rhstype, parmno, rname);
6509 }
6510
bbbbb16a
ILT
6511 if (warn_cxx_compat)
6512 {
6513 tree checktype = origtype != NULL_TREE ? origtype : rhstype;
6514 if (checktype != error_mark_node
6515 && TREE_CODE (type) == ENUMERAL_TYPE
6516 && TYPE_MAIN_VARIANT (checktype) != TYPE_MAIN_VARIANT (type))
296c53ac
MP
6517 switch (errtype)
6518 {
6519 case ic_argpass:
6520 if (pedwarn (expr_loc, OPT_Wc___compat, "enum conversion when "
6521 "passing argument %d of %qE is invalid in C++",
6522 parmnum, rname))
6523 inform ((fundecl && !DECL_IS_BUILTIN (fundecl))
6524 ? DECL_SOURCE_LOCATION (fundecl) : expr_loc,
6525 "expected %qT but argument is of type %qT",
6526 type, rhstype);
6527 break;
6528 case ic_assign:
6529 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6530 "%qT in assignment is invalid in C++", rhstype, type);
6531 break;
6532 case ic_init:
6533 pedwarn_init (location, OPT_Wc___compat, "enum conversion from "
6534 "%qT to %qT in initialization is invalid in C++",
6535 rhstype, type);
6536 break;
6537 case ic_return:
6538 pedwarn (location, OPT_Wc___compat, "enum conversion from %qT to "
6539 "%qT in return is invalid in C++", rhstype, type);
6540 break;
6541 default:
6542 gcc_unreachable ();
6543 }
bbbbb16a
ILT
6544 }
6545
3e4093b6 6546 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (rhstype))
59c0753d 6547 return rhs;
3e4093b6
RS
6548
6549 if (coder == VOID_TYPE)
400fbf9f 6550 {
6dcc04b0
JM
6551 /* Except for passing an argument to an unprototyped function,
6552 this is a constraint violation. When passing an argument to
6553 an unprototyped function, it is compile-time undefined;
6554 making it a constraint in that case was rejected in
6555 DR#252. */
c2255bc4 6556 error_at (location, "void value not ignored as it ought to be");
3e4093b6 6557 return error_mark_node;
400fbf9f 6558 }
4f2e1536 6559 rhs = require_complete_type (location, rhs);
808d6eaa
JM
6560 if (rhs == error_mark_node)
6561 return error_mark_node;
1807ffc1
MS
6562
6563 if (coder == POINTER_TYPE && reject_gcc_builtin (rhs))
6564 return error_mark_node;
6565
cd192ccc
MS
6566 /* A non-reference type can convert to a reference. This handles
6567 va_start, va_copy and possibly port built-ins. */
6568 if (codel == REFERENCE_TYPE && coder != REFERENCE_TYPE)
400fbf9f 6569 {
3e4093b6 6570 if (!lvalue_p (rhs))
400fbf9f 6571 {
c2255bc4 6572 error_at (location, "cannot pass rvalue to reference parameter");
3e4093b6 6573 return error_mark_node;
400fbf9f 6574 }
3e4093b6
RS
6575 if (!c_mark_addressable (rhs))
6576 return error_mark_node;
6577 rhs = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (rhs)), rhs);
c2255bc4 6578 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6579
81e5eca8 6580 rhs = convert_for_assignment (location, expr_loc,
68fca595
MP
6581 build_pointer_type (TREE_TYPE (type)),
6582 rhs, origtype, errtype,
6583 null_pointer_constant, fundecl, function,
6584 parmnum);
cd192ccc
MS
6585 if (rhs == error_mark_node)
6586 return error_mark_node;
3e4093b6
RS
6587
6588 rhs = build1 (NOP_EXPR, type, rhs);
c2255bc4 6589 SET_EXPR_LOCATION (rhs, location);
3e4093b6 6590 return rhs;
400fbf9f 6591 }
3e4093b6 6592 /* Some types can interconvert without explicit casts. */
3274deff 6593 else if (codel == VECTOR_TYPE && coder == VECTOR_TYPE
00c8e9f6 6594 && vector_types_convertible_p (type, TREE_TYPE (rhs), true))
3e4093b6
RS
6595 return convert (type, rhs);
6596 /* Arithmetic types all interconvert, and enum is treated like int. */
6597 else if ((codel == INTEGER_TYPE || codel == REAL_TYPE
ab22c1fa 6598 || codel == FIXED_POINT_TYPE
3e4093b6
RS
6599 || codel == ENUMERAL_TYPE || codel == COMPLEX_TYPE
6600 || codel == BOOLEAN_TYPE)
6601 && (coder == INTEGER_TYPE || coder == REAL_TYPE
ab22c1fa 6602 || coder == FIXED_POINT_TYPE
3e4093b6
RS
6603 || coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
6604 || coder == BOOLEAN_TYPE))
928c19bb
JM
6605 {
6606 tree ret;
6607 bool save = in_late_binary_op;
e5341100
JJ
6608 if (codel == BOOLEAN_TYPE || codel == COMPLEX_TYPE
6609 || (coder == REAL_TYPE
6610 && (codel == INTEGER_TYPE || codel == ENUMERAL_TYPE)
45b2222a 6611 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
928c19bb 6612 in_late_binary_op = true;
81e5eca8
MP
6613 ret = convert_and_check (expr_loc != UNKNOWN_LOCATION
6614 ? expr_loc : location, type, orig_rhs);
e5341100 6615 in_late_binary_op = save;
928c19bb
JM
6616 return ret;
6617 }
400fbf9f 6618
79077aea
JJ
6619 /* Aggregates in different TUs might need conversion. */
6620 if ((codel == RECORD_TYPE || codel == UNION_TYPE)
6621 && codel == coder
6622 && comptypes (type, rhstype))
81e5eca8
MP
6623 return convert_and_check (expr_loc != UNKNOWN_LOCATION
6624 ? expr_loc : location, type, rhs);
79077aea 6625
ebf0bf7f 6626 /* Conversion to a transparent union or record from its member types.
3e4093b6 6627 This applies only to function arguments. */
ebf0bf7f
JJ
6628 if (((codel == UNION_TYPE || codel == RECORD_TYPE)
6629 && TYPE_TRANSPARENT_AGGR (type))
6b4ef5c1 6630 && errtype == ic_argpass)
400fbf9f 6631 {
0257e383 6632 tree memb, marginal_memb = NULL_TREE;
3e4093b6 6633
910ad8de 6634 for (memb = TYPE_FIELDS (type); memb ; memb = DECL_CHAIN (memb))
400fbf9f 6635 {
0257e383 6636 tree memb_type = TREE_TYPE (memb);
400fbf9f 6637
3e4093b6 6638 if (comptypes (TYPE_MAIN_VARIANT (memb_type),
132da1a5 6639 TYPE_MAIN_VARIANT (rhstype)))
3e4093b6 6640 break;
e58cd767 6641
3e4093b6
RS
6642 if (TREE_CODE (memb_type) != POINTER_TYPE)
6643 continue;
2f6e4e97 6644
3e4093b6
RS
6645 if (coder == POINTER_TYPE)
6646 {
6647 tree ttl = TREE_TYPE (memb_type);
6648 tree ttr = TREE_TYPE (rhstype);
400fbf9f 6649
3e4093b6
RS
6650 /* Any non-function converts to a [const][volatile] void *
6651 and vice versa; otherwise, targets must be the same.
6652 Meanwhile, the lhs target must have all the qualifiers of
6653 the rhs. */
267bac10
JM
6654 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6655 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6656 || comp_target_types (location, memb_type, rhstype))
3e4093b6 6657 {
267bac10
JM
6658 int lquals = TYPE_QUALS (ttl) & ~TYPE_QUAL_ATOMIC;
6659 int rquals = TYPE_QUALS (ttr) & ~TYPE_QUAL_ATOMIC;
3e4093b6 6660 /* If this type won't generate any warnings, use it. */
267bac10 6661 if (lquals == rquals
3e4093b6
RS
6662 || ((TREE_CODE (ttr) == FUNCTION_TYPE
6663 && TREE_CODE (ttl) == FUNCTION_TYPE)
267bac10
JM
6664 ? ((lquals | rquals) == rquals)
6665 : ((lquals | rquals) == lquals)))
3e4093b6 6666 break;
400fbf9f 6667
3e4093b6 6668 /* Keep looking for a better type, but remember this one. */
0257e383
RH
6669 if (!marginal_memb)
6670 marginal_memb = memb;
3e4093b6
RS
6671 }
6672 }
82bde854 6673
3e4093b6 6674 /* Can convert integer zero to any pointer type. */
928c19bb 6675 if (null_pointer_constant)
3e4093b6
RS
6676 {
6677 rhs = null_pointer_node;
6678 break;
6679 }
6680 }
400fbf9f 6681
0257e383 6682 if (memb || marginal_memb)
3e4093b6 6683 {
0257e383 6684 if (!memb)
3e4093b6
RS
6685 {
6686 /* We have only a marginally acceptable member type;
6687 it needs a warning. */
0257e383 6688 tree ttl = TREE_TYPE (TREE_TYPE (marginal_memb));
3e4093b6 6689 tree ttr = TREE_TYPE (rhstype);
714a0864 6690
3e4093b6
RS
6691 /* Const and volatile mean something different for function
6692 types, so the usual warnings are not appropriate. */
6693 if (TREE_CODE (ttr) == FUNCTION_TYPE
6694 && TREE_CODE (ttl) == FUNCTION_TYPE)
6695 {
6696 /* Because const and volatile on functions are
6697 restrictions that say the function will not do
6698 certain things, it is okay to use a const or volatile
6699 function where an ordinary one is wanted, but not
6700 vice-versa. */
36c5e70a
BE
6701 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6702 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6703 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6704 OPT_Wdiscarded_qualifiers,
6705 G_("passing argument %d of %qE "
6706 "makes %q#v qualified function "
6707 "pointer from unqualified"),
6708 G_("assignment makes %q#v qualified "
6709 "function pointer from "
6710 "unqualified"),
6711 G_("initialization makes %q#v qualified "
6712 "function pointer from "
6713 "unqualified"),
6714 G_("return makes %q#v qualified function "
6715 "pointer from unqualified"),
6716 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6 6717 }
36c5e70a
BE
6718 else if (TYPE_QUALS_NO_ADDR_SPACE (ttr)
6719 & ~TYPE_QUALS_NO_ADDR_SPACE (ttl))
768952be
MU
6720 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6721 OPT_Wdiscarded_qualifiers,
6722 G_("passing argument %d of %qE discards "
6723 "%qv qualifier from pointer target type"),
6724 G_("assignment discards %qv qualifier "
6725 "from pointer target type"),
6726 G_("initialization discards %qv qualifier "
6727 "from pointer target type"),
6728 G_("return discards %qv qualifier from "
6729 "pointer target type"),
6730 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
0257e383
RH
6731
6732 memb = marginal_memb;
3e4093b6 6733 }
400fbf9f 6734
fcf73884 6735 if (!fundecl || !DECL_IN_SYSTEM_HEADER (fundecl))
c1771a20 6736 pedwarn (location, OPT_Wpedantic,
fcf73884 6737 "ISO C prohibits argument conversion to union type");
0e7c47fa 6738
db3927fb 6739 rhs = fold_convert_loc (location, TREE_TYPE (memb), rhs);
0257e383 6740 return build_constructor_single (type, memb, rhs);
3e4093b6 6741 }
0e7c47fa
RK
6742 }
6743
3e4093b6
RS
6744 /* Conversions among pointers */
6745 else if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
6746 && (coder == codel))
400fbf9f 6747 {
3e4093b6
RS
6748 tree ttl = TREE_TYPE (type);
6749 tree ttr = TREE_TYPE (rhstype);
46df2823
JM
6750 tree mvl = ttl;
6751 tree mvr = ttr;
3e4093b6 6752 bool is_opaque_pointer;
264fa2db 6753 int target_cmp = 0; /* Cache comp_target_types () result. */
36c5e70a
BE
6754 addr_space_t asl;
6755 addr_space_t asr;
400fbf9f 6756
46df2823 6757 if (TREE_CODE (mvl) != ARRAY_TYPE)
267bac10
JM
6758 mvl = (TYPE_ATOMIC (mvl)
6759 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvl),
6760 TYPE_QUAL_ATOMIC)
6761 : TYPE_MAIN_VARIANT (mvl));
46df2823 6762 if (TREE_CODE (mvr) != ARRAY_TYPE)
267bac10
JM
6763 mvr = (TYPE_ATOMIC (mvr)
6764 ? c_build_qualified_type (TYPE_MAIN_VARIANT (mvr),
6765 TYPE_QUAL_ATOMIC)
6766 : TYPE_MAIN_VARIANT (mvr));
3e4093b6 6767 /* Opaque pointers are treated like void pointers. */
f83c7f63 6768 is_opaque_pointer = vector_targets_convertible_p (ttl, ttr);
c22cacf3 6769
478a1c5b
ILT
6770 /* The Plan 9 compiler permits a pointer to a struct to be
6771 automatically converted into a pointer to an anonymous field
6772 within the struct. */
6773 if (flag_plan9_extensions
3e636daf
MP
6774 && RECORD_OR_UNION_TYPE_P (mvl)
6775 && RECORD_OR_UNION_TYPE_P (mvr)
478a1c5b
ILT
6776 && mvl != mvr)
6777 {
6778 tree new_rhs = convert_to_anonymous_field (location, type, rhs);
6779 if (new_rhs != NULL_TREE)
6780 {
6781 rhs = new_rhs;
6782 rhstype = TREE_TYPE (rhs);
6783 coder = TREE_CODE (rhstype);
6784 ttr = TREE_TYPE (rhstype);
6785 mvr = TYPE_MAIN_VARIANT (ttr);
6786 }
6787 }
6788
b7e20b53 6789 /* C++ does not allow the implicit conversion void* -> T*. However,
c22cacf3
MS
6790 for the purpose of reducing the number of false positives, we
6791 tolerate the special case of
b7e20b53 6792
c22cacf3 6793 int *p = NULL;
b7e20b53 6794
c22cacf3 6795 where NULL is typically defined in C to be '(void *) 0'. */
c6bdf92e 6796 if (VOID_TYPE_P (ttr) && rhs != null_pointer_node && !VOID_TYPE_P (ttl))
8ffcdea8
MP
6797 warning_at (errtype == ic_argpass ? expr_loc : location,
6798 OPT_Wc___compat,
6799 "request for implicit conversion "
c2255bc4 6800 "from %qT to %qT not permitted in C++", rhstype, type);
400fbf9f 6801
36c5e70a
BE
6802 /* See if the pointers point to incompatible address spaces. */
6803 asl = TYPE_ADDR_SPACE (ttl);
6804 asr = TYPE_ADDR_SPACE (ttr);
6805 if (!null_pointer_constant_p (rhs)
6806 && asr != asl && !targetm.addr_space.subset_p (asr, asl))
6807 {
6808 switch (errtype)
6809 {
6810 case ic_argpass:
8ffcdea8 6811 error_at (expr_loc, "passing argument %d of %qE from pointer to "
36c5e70a
BE
6812 "non-enclosed address space", parmnum, rname);
6813 break;
6814 case ic_assign:
6815 error_at (location, "assignment from pointer to "
6816 "non-enclosed address space");
6817 break;
6818 case ic_init:
6819 error_at (location, "initialization from pointer to "
6820 "non-enclosed address space");
6821 break;
6822 case ic_return:
6823 error_at (location, "return from pointer to "
6824 "non-enclosed address space");
6825 break;
6826 default:
6827 gcc_unreachable ();
6828 }
6829 return error_mark_node;
6830 }
6831
7876a414
KG
6832 /* Check if the right-hand side has a format attribute but the
6833 left-hand side doesn't. */
90137d8f 6834 if (warn_suggest_attribute_format
104f8784 6835 && check_missing_format_attribute (type, rhstype))
c22cacf3 6836 {
104f8784
KG
6837 switch (errtype)
6838 {
6839 case ic_argpass:
8ffcdea8 6840 warning_at (expr_loc, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6841 "argument %d of %qE might be "
6842 "a candidate for a format attribute",
6843 parmnum, rname);
104f8784
KG
6844 break;
6845 case ic_assign:
90137d8f 6846 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6847 "assignment left-hand side might be "
6848 "a candidate for a format attribute");
104f8784
KG
6849 break;
6850 case ic_init:
90137d8f 6851 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6852 "initialization left-hand side might be "
6853 "a candidate for a format attribute");
104f8784
KG
6854 break;
6855 case ic_return:
90137d8f 6856 warning_at (location, OPT_Wsuggest_attribute_format,
c2255bc4
AH
6857 "return type might be "
6858 "a candidate for a format attribute");
104f8784
KG
6859 break;
6860 default:
6861 gcc_unreachable ();
6862 }
7876a414 6863 }
c22cacf3 6864
3e4093b6
RS
6865 /* Any non-function converts to a [const][volatile] void *
6866 and vice versa; otherwise, targets must be the same.
6867 Meanwhile, the lhs target must have all the qualifiers of the rhs. */
267bac10
JM
6868 if ((VOID_TYPE_P (ttl) && !TYPE_ATOMIC (ttl))
6869 || (VOID_TYPE_P (ttr) && !TYPE_ATOMIC (ttr))
744aa42f 6870 || (target_cmp = comp_target_types (location, type, rhstype))
3e4093b6 6871 || is_opaque_pointer
f8a93a2e
JJ
6872 || ((c_common_unsigned_type (mvl)
6873 == c_common_unsigned_type (mvr))
267bac10
JM
6874 && (c_common_signed_type (mvl)
6875 == c_common_signed_type (mvr))
6876 && TYPE_ATOMIC (mvl) == TYPE_ATOMIC (mvr)))
3e4093b6 6877 {
768952be
MU
6878 /* Warn about loss of qualifers from pointers to arrays with
6879 qualifiers on the element type. */
6880 if (TREE_CODE (ttr) == ARRAY_TYPE)
6881 {
6882 ttr = strip_array_types (ttr);
6883 ttl = strip_array_types (ttl);
6884
6885 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6886 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
6887 WARNING_FOR_QUALIFIERS (location, expr_loc,
6888 OPT_Wdiscarded_array_qualifiers,
6889 G_("passing argument %d of %qE discards "
6890 "%qv qualifier from pointer target type"),
6891 G_("assignment discards %qv qualifier "
6892 "from pointer target type"),
6893 G_("initialization discards %qv qualifier "
6894 "from pointer target type"),
6895 G_("return discards %qv qualifier from "
6896 "pointer target type"),
6897 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
6898 }
6899 else if (pedantic
3e4093b6
RS
6900 && ((VOID_TYPE_P (ttl) && TREE_CODE (ttr) == FUNCTION_TYPE)
6901 ||
6902 (VOID_TYPE_P (ttr)
928c19bb 6903 && !null_pointer_constant
3e4093b6 6904 && TREE_CODE (ttl) == FUNCTION_TYPE)))
768952be
MU
6905 PEDWARN_FOR_ASSIGNMENT (location, expr_loc, OPT_Wpedantic,
6906 G_("ISO C forbids passing argument %d of "
6907 "%qE between function pointer "
6908 "and %<void *%>"),
6909 G_("ISO C forbids assignment between "
6910 "function pointer and %<void *%>"),
6911 G_("ISO C forbids initialization between "
6912 "function pointer and %<void *%>"),
6913 G_("ISO C forbids return between function "
6914 "pointer and %<void *%>"));
3e4093b6
RS
6915 /* Const and volatile mean something different for function types,
6916 so the usual warnings are not appropriate. */
6917 else if (TREE_CODE (ttr) != FUNCTION_TYPE
6918 && TREE_CODE (ttl) != FUNCTION_TYPE)
6919 {
768952be
MU
6920 /* Don't warn about loss of qualifier for conversions from
6921 qualified void* to pointers to arrays with corresponding
6922 qualifier on the element type. */
6923 if (!pedantic)
6924 ttl = strip_array_types (ttl);
6925
267bac10
JM
6926 /* Assignments between atomic and non-atomic objects are OK. */
6927 if (TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttr)
6928 & ~TYPE_QUALS_NO_ADDR_SPACE_NO_ATOMIC (ttl))
58393038 6929 {
768952be
MU
6930 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6931 OPT_Wdiscarded_qualifiers,
6932 G_("passing argument %d of %qE discards "
6933 "%qv qualifier from pointer target type"),
6934 G_("assignment discards %qv qualifier "
6935 "from pointer target type"),
6936 G_("initialization discards %qv qualifier "
6937 "from pointer target type"),
6938 G_("return discards %qv qualifier from "
6939 "pointer target type"),
6940 TYPE_QUALS (ttr) & ~TYPE_QUALS (ttl));
58393038 6941 }
3e4093b6
RS
6942 /* If this is not a case of ignoring a mismatch in signedness,
6943 no warning. */
6944 else if (VOID_TYPE_P (ttl) || VOID_TYPE_P (ttr)
264fa2db 6945 || target_cmp)
3e4093b6
RS
6946 ;
6947 /* If there is a mismatch, do warn. */
f2fd3821 6948 else if (warn_pointer_sign)
296c53ac
MP
6949 switch (errtype)
6950 {
6951 case ic_argpass:
96e6ae57 6952 {
097f82ec 6953 auto_diagnostic_group d;
96e6ae57
DM
6954 range_label_for_type_mismatch rhs_label (rhstype, type);
6955 gcc_rich_location richloc (expr_loc, &rhs_label);
6956 if (pedwarn (&richloc, OPT_Wpointer_sign,
6957 "pointer targets in passing argument %d of "
6958 "%qE differ in signedness", parmnum, rname))
6959 inform_for_arg (fundecl, expr_loc, parmnum, type,
6960 rhstype);
6961 }
296c53ac
MP
6962 break;
6963 case ic_assign:
6964 pedwarn (location, OPT_Wpointer_sign,
6965 "pointer targets in assignment from %qT to %qT "
6966 "differ in signedness", rhstype, type);
6967 break;
6968 case ic_init:
6969 pedwarn_init (location, OPT_Wpointer_sign,
6970 "pointer targets in initialization of %qT "
6971 "from %qT differ in signedness", type,
6972 rhstype);
6973 break;
6974 case ic_return:
6975 pedwarn (location, OPT_Wpointer_sign, "pointer targets in "
6976 "returning %qT from a function with return type "
6977 "%qT differ in signedness", rhstype, type);
6978 break;
6979 default:
6980 gcc_unreachable ();
6981 }
3e4093b6
RS
6982 }
6983 else if (TREE_CODE (ttl) == FUNCTION_TYPE
6984 && TREE_CODE (ttr) == FUNCTION_TYPE)
6985 {
6986 /* Because const and volatile on functions are restrictions
6987 that say the function will not do certain things,
6988 it is okay to use a const or volatile function
6989 where an ordinary one is wanted, but not vice-versa. */
36c5e70a
BE
6990 if (TYPE_QUALS_NO_ADDR_SPACE (ttl)
6991 & ~TYPE_QUALS_NO_ADDR_SPACE (ttr))
768952be
MU
6992 PEDWARN_FOR_QUALIFIERS (location, expr_loc,
6993 OPT_Wdiscarded_qualifiers,
6994 G_("passing argument %d of %qE makes "
6995 "%q#v qualified function pointer "
6996 "from unqualified"),
6997 G_("assignment makes %q#v qualified function "
6998 "pointer from unqualified"),
6999 G_("initialization makes %q#v qualified "
7000 "function pointer from unqualified"),
7001 G_("return makes %q#v qualified function "
7002 "pointer from unqualified"),
7003 TYPE_QUALS (ttl) & ~TYPE_QUALS (ttr));
3e4093b6
RS
7004 }
7005 }
296c53ac
MP
7006 /* Avoid warning about the volatile ObjC EH puts on decls. */
7007 else if (!objc_ok)
7008 {
7009 switch (errtype)
7010 {
7011 case ic_argpass:
96e6ae57 7012 {
097f82ec 7013 auto_diagnostic_group d;
96e6ae57
DM
7014 range_label_for_type_mismatch rhs_label (rhstype, type);
7015 gcc_rich_location richloc (expr_loc, &rhs_label);
7016 if (pedwarn (&richloc, OPT_Wincompatible_pointer_types,
7017 "passing argument %d of %qE from incompatible "
7018 "pointer type", parmnum, rname))
7019 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7020 }
296c53ac
MP
7021 break;
7022 case ic_assign:
7023 pedwarn (location, OPT_Wincompatible_pointer_types,
7024 "assignment to %qT from incompatible pointer type %qT",
7025 type, rhstype);
7026 break;
7027 case ic_init:
7028 pedwarn_init (location, OPT_Wincompatible_pointer_types,
7029 "initialization of %qT from incompatible pointer "
7030 "type %qT", type, rhstype);
7031 break;
7032 case ic_return:
7033 pedwarn (location, OPT_Wincompatible_pointer_types,
7034 "returning %qT from a function with incompatible "
7035 "return type %qT", rhstype, type);
7036 break;
7037 default:
7038 gcc_unreachable ();
7039 }
7040 }
58393038 7041
3e4093b6
RS
7042 return convert (type, rhs);
7043 }
b494fd98
EB
7044 else if (codel == POINTER_TYPE && coder == ARRAY_TYPE)
7045 {
6dcc04b0
JM
7046 /* ??? This should not be an error when inlining calls to
7047 unprototyped functions. */
c2255bc4 7048 error_at (location, "invalid use of non-lvalue array");
b494fd98
EB
7049 return error_mark_node;
7050 }
3e4093b6 7051 else if (codel == POINTER_TYPE && coder == INTEGER_TYPE)
400fbf9f 7052 {
3e4093b6
RS
7053 /* An explicit constant 0 can convert to a pointer,
7054 or one that results from arithmetic, even including
7055 a cast to integer type. */
928c19bb 7056 if (!null_pointer_constant)
296c53ac
MP
7057 switch (errtype)
7058 {
7059 case ic_argpass:
96e6ae57 7060 {
097f82ec 7061 auto_diagnostic_group d;
96e6ae57
DM
7062 range_label_for_type_mismatch rhs_label (rhstype, type);
7063 gcc_rich_location richloc (expr_loc, &rhs_label);
7064 if (pedwarn (&richloc, OPT_Wint_conversion,
7065 "passing argument %d of %qE makes pointer from "
7066 "integer without a cast", parmnum, rname))
7067 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7068 }
296c53ac
MP
7069 break;
7070 case ic_assign:
7071 pedwarn (location, OPT_Wint_conversion,
7072 "assignment to %qT from %qT makes pointer from integer "
7073 "without a cast", type, rhstype);
7074 break;
7075 case ic_init:
7076 pedwarn_init (location, OPT_Wint_conversion,
7077 "initialization of %qT from %qT makes pointer from "
7078 "integer without a cast", type, rhstype);
7079 break;
7080 case ic_return:
7081 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7082 "function with return type %qT makes pointer from "
7083 "integer without a cast", rhstype, type);
7084 break;
7085 default:
7086 gcc_unreachable ();
7087 }
b3006337
EB
7088
7089 return convert (type, rhs);
400fbf9f 7090 }
3e4093b6 7091 else if (codel == INTEGER_TYPE && coder == POINTER_TYPE)
400fbf9f 7092 {
296c53ac
MP
7093 switch (errtype)
7094 {
7095 case ic_argpass:
96e6ae57 7096 {
097f82ec 7097 auto_diagnostic_group d;
96e6ae57
DM
7098 range_label_for_type_mismatch rhs_label (rhstype, type);
7099 gcc_rich_location richloc (expr_loc, &rhs_label);
7100 if (pedwarn (&richloc, OPT_Wint_conversion,
7101 "passing argument %d of %qE makes integer from "
7102 "pointer without a cast", parmnum, rname))
7103 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7104 }
296c53ac
MP
7105 break;
7106 case ic_assign:
7107 pedwarn (location, OPT_Wint_conversion,
7108 "assignment to %qT from %qT makes integer from pointer "
7109 "without a cast", type, rhstype);
7110 break;
7111 case ic_init:
7112 pedwarn_init (location, OPT_Wint_conversion,
7113 "initialization of %qT from %qT makes integer from "
7114 "pointer without a cast", type, rhstype);
7115 break;
7116 case ic_return:
7117 pedwarn (location, OPT_Wint_conversion, "returning %qT from a "
7118 "function with return type %qT makes integer from "
7119 "pointer without a cast", rhstype, type);
7120 break;
7121 default:
7122 gcc_unreachable ();
7123 }
7124
3e4093b6
RS
7125 return convert (type, rhs);
7126 }
7127 else if (codel == BOOLEAN_TYPE && coder == POINTER_TYPE)
928c19bb
JM
7128 {
7129 tree ret;
7130 bool save = in_late_binary_op;
7131 in_late_binary_op = true;
7132 ret = convert (type, rhs);
7133 in_late_binary_op = save;
7134 return ret;
7135 }
400fbf9f 7136
2ac2f164 7137 switch (errtype)
3e4093b6 7138 {
2ac2f164 7139 case ic_argpass:
96e6ae57 7140 {
097f82ec 7141 auto_diagnostic_group d;
96e6ae57
DM
7142 range_label_for_type_mismatch rhs_label (rhstype, type);
7143 gcc_rich_location richloc (expr_loc, &rhs_label);
7144 error_at (&richloc, "incompatible type for argument %d of %qE", parmnum,
7145 rname);
7146 inform_for_arg (fundecl, expr_loc, parmnum, type, rhstype);
7147 }
2ac2f164
JM
7148 break;
7149 case ic_assign:
c2255bc4
AH
7150 error_at (location, "incompatible types when assigning to type %qT from "
7151 "type %qT", type, rhstype);
2ac2f164
JM
7152 break;
7153 case ic_init:
c2255bc4 7154 error_at (location,
8ffcdea8 7155 "incompatible types when initializing type %qT using type %qT",
c2255bc4 7156 type, rhstype);
2ac2f164
JM
7157 break;
7158 case ic_return:
c2255bc4 7159 error_at (location,
8ffcdea8 7160 "incompatible types when returning type %qT but %qT was "
c2255bc4 7161 "expected", rhstype, type);
2ac2f164
JM
7162 break;
7163 default:
7164 gcc_unreachable ();
400fbf9f 7165 }
53b01f59 7166
3e4093b6
RS
7167 return error_mark_node;
7168}
3e4093b6
RS
7169\f
7170/* If VALUE is a compound expr all of whose expressions are constant, then
7171 return its value. Otherwise, return error_mark_node.
15b732b2 7172
3e4093b6
RS
7173 This is for handling COMPOUND_EXPRs as initializer elements
7174 which is allowed with a warning when -pedantic is specified. */
15b732b2 7175
3e4093b6
RS
7176static tree
7177valid_compound_expr_initializer (tree value, tree endtype)
7178{
7179 if (TREE_CODE (value) == COMPOUND_EXPR)
7180 {
7181 if (valid_compound_expr_initializer (TREE_OPERAND (value, 0), endtype)
7182 == error_mark_node)
7183 return error_mark_node;
7184 return valid_compound_expr_initializer (TREE_OPERAND (value, 1),
7185 endtype);
7186 }
116df786 7187 else if (!initializer_constant_valid_p (value, endtype))
3e4093b6
RS
7188 return error_mark_node;
7189 else
7190 return value;
15b732b2 7191}
400fbf9f 7192\f
3e4093b6
RS
7193/* Perform appropriate conversions on the initial value of a variable,
7194 store it in the declaration DECL,
7195 and print any error messages that are appropriate.
bbbbb16a 7196 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
c2255bc4
AH
7197 If the init is invalid, store an ERROR_MARK.
7198
7199 INIT_LOC is the location of the initial value. */
400fbf9f 7200
3e4093b6 7201void
c2255bc4 7202store_init_value (location_t init_loc, tree decl, tree init, tree origtype)
400fbf9f 7203{
3e4093b6 7204 tree value, type;
928c19bb 7205 bool npc = false;
400fbf9f 7206
3e4093b6 7207 /* If variable's type was invalidly declared, just ignore it. */
400fbf9f 7208
3e4093b6
RS
7209 type = TREE_TYPE (decl);
7210 if (TREE_CODE (type) == ERROR_MARK)
7211 return;
400fbf9f 7212
3e4093b6 7213 /* Digest the specified initializer into an expression. */
400fbf9f 7214
928c19bb
JM
7215 if (init)
7216 npc = null_pointer_constant_p (init);
c2255bc4
AH
7217 value = digest_init (init_loc, type, init, origtype, npc,
7218 true, TREE_STATIC (decl));
400fbf9f 7219
3e4093b6 7220 /* Store the expression if valid; else report error. */
400fbf9f 7221
8400e75e 7222 if (!in_system_header_at (input_location)
3f75a254 7223 && AGGREGATE_TYPE_P (TREE_TYPE (decl)) && !TREE_STATIC (decl))
3176a0c2
DD
7224 warning (OPT_Wtraditional, "traditional C rejects automatic "
7225 "aggregate initialization");
2f6e4e97 7226
dea63e49
JJ
7227 if (value != error_mark_node || TREE_CODE (decl) != FUNCTION_DECL)
7228 DECL_INITIAL (decl) = value;
400fbf9f 7229
3e4093b6
RS
7230 /* ANSI wants warnings about out-of-range constant initializers. */
7231 STRIP_TYPE_NOPS (value);
b8698a0f 7232 if (TREE_STATIC (decl))
c2658540 7233 constant_expression_warning (value);
400fbf9f 7234
3e4093b6
RS
7235 /* Check if we need to set array size from compound literal size. */
7236 if (TREE_CODE (type) == ARRAY_TYPE
3fa8871b 7237 && TYPE_DOMAIN (type) == NULL_TREE
3e4093b6 7238 && value != error_mark_node)
400fbf9f 7239 {
3e4093b6
RS
7240 tree inside_init = init;
7241
ed248cf7 7242 STRIP_TYPE_NOPS (inside_init);
3e4093b6
RS
7243 inside_init = fold (inside_init);
7244
7245 if (TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
7246 {
8d9f82d5 7247 tree cldecl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
3e4093b6 7248
8d9f82d5 7249 if (TYPE_DOMAIN (TREE_TYPE (cldecl)))
3e4093b6
RS
7250 {
7251 /* For int foo[] = (int [3]){1}; we need to set array size
7252 now since later on array initializer will be just the
7253 brace enclosed list of the compound literal. */
e30ecc5d 7254 tree etype = strip_array_types (TREE_TYPE (decl));
8d9f82d5 7255 type = build_distinct_type_copy (TYPE_MAIN_VARIANT (type));
8d9f82d5 7256 TYPE_DOMAIN (type) = TYPE_DOMAIN (TREE_TYPE (cldecl));
3e4093b6 7257 layout_type (type);
8d9f82d5 7258 layout_decl (cldecl, 0);
e30ecc5d
JJ
7259 TREE_TYPE (decl)
7260 = c_build_qualified_type (type, TYPE_QUALS (etype));
3e4093b6
RS
7261 }
7262 }
400fbf9f 7263 }
3e4093b6
RS
7264}
7265\f
7266/* Methods for storing and printing names for error messages. */
400fbf9f 7267
3e4093b6
RS
7268/* Implement a spelling stack that allows components of a name to be pushed
7269 and popped. Each element on the stack is this structure. */
400fbf9f 7270
3e4093b6
RS
7271struct spelling
7272{
7273 int kind;
7274 union
400fbf9f 7275 {
a0f0ab9f 7276 unsigned HOST_WIDE_INT i;
3e4093b6
RS
7277 const char *s;
7278 } u;
7279};
2f6e4e97 7280
3e4093b6
RS
7281#define SPELLING_STRING 1
7282#define SPELLING_MEMBER 2
7283#define SPELLING_BOUNDS 3
400fbf9f 7284
3e4093b6
RS
7285static struct spelling *spelling; /* Next stack element (unused). */
7286static struct spelling *spelling_base; /* Spelling stack base. */
7287static int spelling_size; /* Size of the spelling stack. */
400fbf9f 7288
3e4093b6
RS
7289/* Macros to save and restore the spelling stack around push_... functions.
7290 Alternative to SAVE_SPELLING_STACK. */
400fbf9f 7291
3e4093b6
RS
7292#define SPELLING_DEPTH() (spelling - spelling_base)
7293#define RESTORE_SPELLING_DEPTH(DEPTH) (spelling = spelling_base + (DEPTH))
400fbf9f 7294
3e4093b6
RS
7295/* Push an element on the spelling stack with type KIND and assign VALUE
7296 to MEMBER. */
400fbf9f 7297
3e4093b6
RS
7298#define PUSH_SPELLING(KIND, VALUE, MEMBER) \
7299{ \
7300 int depth = SPELLING_DEPTH (); \
7301 \
7302 if (depth >= spelling_size) \
7303 { \
7304 spelling_size += 10; \
cca8ead2
BI
7305 spelling_base = XRESIZEVEC (struct spelling, spelling_base, \
7306 spelling_size); \
3e4093b6
RS
7307 RESTORE_SPELLING_DEPTH (depth); \
7308 } \
7309 \
7310 spelling->kind = (KIND); \
7311 spelling->MEMBER = (VALUE); \
7312 spelling++; \
7313}
400fbf9f 7314
3e4093b6 7315/* Push STRING on the stack. Printed literally. */
400fbf9f 7316
3e4093b6
RS
7317static void
7318push_string (const char *string)
7319{
7320 PUSH_SPELLING (SPELLING_STRING, string, u.s);
7321}
400fbf9f 7322
3e4093b6 7323/* Push a member name on the stack. Printed as '.' STRING. */
400fbf9f 7324
3e4093b6
RS
7325static void
7326push_member_name (tree decl)
7327{
7328 const char *const string
88388a52
JM
7329 = (DECL_NAME (decl)
7330 ? identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)))
7331 : _("<anonymous>"));
3e4093b6
RS
7332 PUSH_SPELLING (SPELLING_MEMBER, string, u.s);
7333}
400fbf9f 7334
3e4093b6 7335/* Push an array bounds on the stack. Printed as [BOUNDS]. */
400fbf9f 7336
3e4093b6 7337static void
a0f0ab9f 7338push_array_bounds (unsigned HOST_WIDE_INT bounds)
3e4093b6
RS
7339{
7340 PUSH_SPELLING (SPELLING_BOUNDS, bounds, u.i);
7341}
bb58bec5 7342
3e4093b6 7343/* Compute the maximum size in bytes of the printed spelling. */
400fbf9f 7344
3e4093b6
RS
7345static int
7346spelling_length (void)
7347{
7348 int size = 0;
7349 struct spelling *p;
400fbf9f 7350
3e4093b6
RS
7351 for (p = spelling_base; p < spelling; p++)
7352 {
7353 if (p->kind == SPELLING_BOUNDS)
7354 size += 25;
7355 else
7356 size += strlen (p->u.s) + 1;
7357 }
7358
7359 return size;
400fbf9f 7360}
400fbf9f 7361
3e4093b6 7362/* Print the spelling to BUFFER and return it. */
400fbf9f 7363
3e4093b6
RS
7364static char *
7365print_spelling (char *buffer)
400fbf9f 7366{
3e4093b6
RS
7367 char *d = buffer;
7368 struct spelling *p;
400fbf9f 7369
3e4093b6
RS
7370 for (p = spelling_base; p < spelling; p++)
7371 if (p->kind == SPELLING_BOUNDS)
7372 {
a0f0ab9f 7373 sprintf (d, "[" HOST_WIDE_INT_PRINT_UNSIGNED "]", p->u.i);
3e4093b6
RS
7374 d += strlen (d);
7375 }
7376 else
7377 {
7378 const char *s;
7379 if (p->kind == SPELLING_MEMBER)
7380 *d++ = '.';
7381 for (s = p->u.s; (*d = *s++); d++)
7382 ;
7383 }
7384 *d++ = '\0';
7385 return buffer;
7386}
400fbf9f 7387
3e4093b6
RS
7388/* Digest the parser output INIT as an initializer for type TYPE.
7389 Return a C expression of type TYPE to represent the initial value.
7e842ef8 7390
bbbbb16a
ILT
7391 If ORIGTYPE is not NULL_TREE, it is the original type of INIT.
7392
928c19bb
JM
7393 NULL_POINTER_CONSTANT is true if INIT is a null pointer constant.
7394
916c5919
JM
7395 If INIT is a string constant, STRICT_STRING is true if it is
7396 unparenthesized or we should not warn here for it being parenthesized.
7397 For other types of INIT, STRICT_STRING is not used.
7398
c2255bc4
AH
7399 INIT_LOC is the location of the INIT.
7400
3e4093b6
RS
7401 REQUIRE_CONSTANT requests an error if non-constant initializers or
7402 elements are seen. */
7e842ef8 7403
3e4093b6 7404static tree
c2255bc4
AH
7405digest_init (location_t init_loc, tree type, tree init, tree origtype,
7406 bool null_pointer_constant, bool strict_string,
7407 int require_constant)
3e4093b6
RS
7408{
7409 enum tree_code code = TREE_CODE (type);
7410 tree inside_init = init;
8ce94e44 7411 tree semantic_type = NULL_TREE;
928c19bb 7412 bool maybe_const = true;
7e842ef8 7413
3e4093b6 7414 if (type == error_mark_node
f01da1a5 7415 || !init
7a0ca710 7416 || error_operand_p (init))
3e4093b6 7417 return error_mark_node;
7e842ef8 7418
ed248cf7 7419 STRIP_TYPE_NOPS (inside_init);
7e842ef8 7420
8ce94e44
JM
7421 if (TREE_CODE (inside_init) == EXCESS_PRECISION_EXPR)
7422 {
7423 semantic_type = TREE_TYPE (inside_init);
7424 inside_init = TREE_OPERAND (inside_init, 0);
7425 }
928c19bb 7426 inside_init = c_fully_fold (inside_init, require_constant, &maybe_const);
7e842ef8 7427
3e4093b6
RS
7428 /* Initialization of an array of chars from a string constant
7429 optionally enclosed in braces. */
7e842ef8 7430
197463ae
JM
7431 if (code == ARRAY_TYPE && inside_init
7432 && TREE_CODE (inside_init) == STRING_CST)
3e4093b6 7433 {
267bac10
JM
7434 tree typ1
7435 = (TYPE_ATOMIC (TREE_TYPE (type))
7436 ? c_build_qualified_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
7437 TYPE_QUAL_ATOMIC)
7438 : TYPE_MAIN_VARIANT (TREE_TYPE (type)));
197463ae
JM
7439 /* Note that an array could be both an array of character type
7440 and an array of wchar_t if wchar_t is signed char or unsigned
7441 char. */
7442 bool char_array = (typ1 == char_type_node
7443 || typ1 == signed_char_type_node
7444 || typ1 == unsigned_char_type_node);
7445 bool wchar_array = !!comptypes (typ1, wchar_type_node);
c466b2cd
KVH
7446 bool char16_array = !!comptypes (typ1, char16_type_node);
7447 bool char32_array = !!comptypes (typ1, char32_type_node);
7448
7449 if (char_array || wchar_array || char16_array || char32_array)
7e842ef8 7450 {
916c5919 7451 struct c_expr expr;
c466b2cd 7452 tree typ2 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (inside_init)));
916c5919
JM
7453 expr.value = inside_init;
7454 expr.original_code = (strict_string ? STRING_CST : ERROR_MARK);
6866c6e8 7455 expr.original_type = NULL;
d033409e 7456 maybe_warn_string_init (init_loc, type, expr);
916c5919 7457
a45e580b 7458 if (TYPE_DOMAIN (type) && !TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
c1771a20 7459 pedwarn_init (init_loc, OPT_Wpedantic,
a45e580b
JM
7460 "initialization of a flexible array member");
7461
3e4093b6 7462 if (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7463 TYPE_MAIN_VARIANT (type)))
3e4093b6 7464 return inside_init;
7e842ef8 7465
c466b2cd 7466 if (char_array)
3e4093b6 7467 {
c466b2cd
KVH
7468 if (typ2 != char_type_node)
7469 {
ea58ef42
MP
7470 error_init (init_loc, "char-array initialized from wide "
7471 "string");
c466b2cd
KVH
7472 return error_mark_node;
7473 }
3e4093b6 7474 }
c466b2cd 7475 else
3e4093b6 7476 {
c466b2cd
KVH
7477 if (typ2 == char_type_node)
7478 {
ea58ef42
MP
7479 error_init (init_loc, "wide character array initialized "
7480 "from non-wide string");
c466b2cd
KVH
7481 return error_mark_node;
7482 }
7483 else if (!comptypes(typ1, typ2))
7484 {
ea58ef42
MP
7485 error_init (init_loc, "wide character array initialized "
7486 "from incompatible wide string");
c466b2cd
KVH
7487 return error_mark_node;
7488 }
7e842ef8 7489 }
2f6e4e97 7490
3e4093b6 7491 TREE_TYPE (inside_init) = type;
3fa8871b
MP
7492 if (TYPE_DOMAIN (type) != NULL_TREE
7493 && TYPE_SIZE (type) != NULL_TREE
5eb4df45
ILT
7494 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
7495 {
7496 unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init);
7497
c466b2cd 7498 /* Subtract the size of a single (possibly wide) character
3e4093b6
RS
7499 because it's ok to ignore the terminating null char
7500 that is counted in the length of the constant. */
01512446
JJ
7501 if (compare_tree_int (TYPE_SIZE_UNIT (type),
7502 (len - (TYPE_PRECISION (typ1)
7503 / BITS_PER_UNIT))) < 0)
5eb4df45
ILT
7504 pedwarn_init (init_loc, 0,
7505 ("initializer-string for array of chars "
7506 "is too long"));
7507 else if (warn_cxx_compat
01512446 7508 && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0)
5eb4df45
ILT
7509 warning_at (init_loc, OPT_Wc___compat,
7510 ("initializer-string for array chars "
7511 "is too long for C++"));
7512 }
7e842ef8 7513
3e4093b6 7514 return inside_init;
7e842ef8 7515 }
197463ae
JM
7516 else if (INTEGRAL_TYPE_P (typ1))
7517 {
ea58ef42 7518 error_init (init_loc, "array of inappropriate type initialized "
197463ae
JM
7519 "from string constant");
7520 return error_mark_node;
7521 }
7e842ef8
PE
7522 }
7523
3e4093b6
RS
7524 /* Build a VECTOR_CST from a *constant* vector constructor. If the
7525 vector constructor is not constant (e.g. {1,2,3,foo()}) then punt
7526 below and handle as a constructor. */
e89be13b 7527 if (code == VECTOR_TYPE
31521951 7528 && VECTOR_TYPE_P (TREE_TYPE (inside_init))
00c8e9f6 7529 && vector_types_convertible_p (TREE_TYPE (inside_init), type, true)
e89be13b
JJ
7530 && TREE_CONSTANT (inside_init))
7531 {
7532 if (TREE_CODE (inside_init) == VECTOR_CST
7533 && comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
7534 TYPE_MAIN_VARIANT (type)))
7535 return inside_init;
7536
7537 if (TREE_CODE (inside_init) == CONSTRUCTOR)
7538 {
4038c495
GB
7539 unsigned HOST_WIDE_INT ix;
7540 tree value;
7541 bool constant_p = true;
e89be13b
JJ
7542
7543 /* Iterate through elements and check if all constructor
7544 elements are *_CSTs. */
4038c495
GB
7545 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (inside_init), ix, value)
7546 if (!CONSTANT_CLASS_P (value))
7547 {
7548 constant_p = false;
7549 break;
7550 }
e89be13b 7551
4038c495
GB
7552 if (constant_p)
7553 return build_vector_from_ctor (type,
7554 CONSTRUCTOR_ELTS (inside_init));
e89be13b
JJ
7555 }
7556 }
6035d635 7557
ca085fd7
MLI
7558 if (warn_sequence_point)
7559 verify_sequence_points (inside_init);
7560
3e4093b6
RS
7561 /* Any type can be initialized
7562 from an expression of the same type, optionally with braces. */
400fbf9f 7563
3fa8871b 7564 if (inside_init && TREE_TYPE (inside_init) != NULL_TREE
3e4093b6 7565 && (comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (inside_init)),
132da1a5 7566 TYPE_MAIN_VARIANT (type))
3e4093b6 7567 || (code == ARRAY_TYPE
132da1a5 7568 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7569 || (code == VECTOR_TYPE
132da1a5 7570 && comptypes (TREE_TYPE (inside_init), type))
3e4093b6 7571 || (code == POINTER_TYPE
3897f229 7572 && TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE
3e4093b6 7573 && comptypes (TREE_TYPE (TREE_TYPE (inside_init)),
132da1a5 7574 TREE_TYPE (type)))))
3e4093b6
RS
7575 {
7576 if (code == POINTER_TYPE)
b494fd98 7577 {
b494fd98
EB
7578 if (TREE_CODE (TREE_TYPE (inside_init)) == ARRAY_TYPE)
7579 {
f2a71bbc
JM
7580 if (TREE_CODE (inside_init) == STRING_CST
7581 || TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
c2255bc4
AH
7582 inside_init = array_to_pointer_conversion
7583 (init_loc, inside_init);
f2a71bbc
JM
7584 else
7585 {
ea58ef42 7586 error_init (init_loc, "invalid use of non-lvalue array");
f2a71bbc
JM
7587 return error_mark_node;
7588 }
b494fd98 7589 }
f2a71bbc 7590 }
b494fd98 7591
bae39a73
NS
7592 if (code == VECTOR_TYPE)
7593 /* Although the types are compatible, we may require a
7594 conversion. */
7595 inside_init = convert (type, inside_init);
3e4093b6 7596
ca58211b 7597 if (require_constant
3e4093b6 7598 && TREE_CODE (inside_init) == COMPOUND_LITERAL_EXPR)
400fbf9f 7599 {
3e4093b6
RS
7600 /* As an extension, allow initializing objects with static storage
7601 duration with compound literals (which are then treated just as
ca58211b
PB
7602 the brace enclosed list they contain). Also allow this for
7603 vectors, as we can only assign them with compound literals. */
7278465e
MP
7604 if (flag_isoc99 && code != VECTOR_TYPE)
7605 pedwarn_init (init_loc, OPT_Wpedantic, "initializer element "
7606 "is not constant");
3e4093b6
RS
7607 tree decl = COMPOUND_LITERAL_EXPR_DECL (inside_init);
7608 inside_init = DECL_INITIAL (decl);
400fbf9f 7609 }
3e4093b6
RS
7610
7611 if (code == ARRAY_TYPE && TREE_CODE (inside_init) != STRING_CST
7612 && TREE_CODE (inside_init) != CONSTRUCTOR)
400fbf9f 7613 {
ea58ef42
MP
7614 error_init (init_loc, "array initialized from non-constant array "
7615 "expression");
3e4093b6 7616 return error_mark_node;
400fbf9f 7617 }
400fbf9f 7618
c1771a20 7619 /* Compound expressions can only occur here if -Wpedantic or
3e4093b6
RS
7620 -pedantic-errors is specified. In the later case, we always want
7621 an error. In the former case, we simply want a warning. */
7622 if (require_constant && pedantic
7623 && TREE_CODE (inside_init) == COMPOUND_EXPR)
7624 {
7625 inside_init
7626 = valid_compound_expr_initializer (inside_init,
7627 TREE_TYPE (inside_init));
7628 if (inside_init == error_mark_node)
ea58ef42 7629 error_init (init_loc, "initializer element is not constant");
2f6e4e97 7630 else
c1771a20 7631 pedwarn_init (init_loc, OPT_Wpedantic,
509c9d60 7632 "initializer element is not constant");
3e4093b6
RS
7633 if (flag_pedantic_errors)
7634 inside_init = error_mark_node;
7635 }
7636 else if (require_constant
116df786
RH
7637 && !initializer_constant_valid_p (inside_init,
7638 TREE_TYPE (inside_init)))
3e4093b6 7639 {
ea58ef42 7640 error_init (init_loc, "initializer element is not constant");
3e4093b6 7641 inside_init = error_mark_node;
8b40563c 7642 }
928c19bb 7643 else if (require_constant && !maybe_const)
3aa3c9fc 7644 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7645 "initializer element is not a constant expression");
f735a153 7646
90137d8f 7647 /* Added to enable additional -Wsuggest-attribute=format warnings. */
8fcef540 7648 if (TREE_CODE (TREE_TYPE (inside_init)) == POINTER_TYPE)
68fca595
MP
7649 inside_init = convert_for_assignment (init_loc, UNKNOWN_LOCATION,
7650 type, inside_init, origtype,
bbbbb16a 7651 ic_init, null_pointer_constant,
928c19bb 7652 NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
7653 return inside_init;
7654 }
f735a153 7655
3e4093b6 7656 /* Handle scalar types, including conversions. */
400fbf9f 7657
ab22c1fa
CF
7658 if (code == INTEGER_TYPE || code == REAL_TYPE || code == FIXED_POINT_TYPE
7659 || code == POINTER_TYPE || code == ENUMERAL_TYPE || code == BOOLEAN_TYPE
7660 || code == COMPLEX_TYPE || code == VECTOR_TYPE)
400fbf9f 7661 {
f2a71bbc
JM
7662 if (TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE
7663 && (TREE_CODE (init) == STRING_CST
7664 || TREE_CODE (init) == COMPOUND_LITERAL_EXPR))
c2255bc4 7665 inside_init = init = array_to_pointer_conversion (init_loc, init);
8ce94e44
JM
7666 if (semantic_type)
7667 inside_init = build1 (EXCESS_PRECISION_EXPR, semantic_type,
7668 inside_init);
3e4093b6 7669 inside_init
68fca595
MP
7670 = convert_for_assignment (init_loc, UNKNOWN_LOCATION, type,
7671 inside_init, origtype, ic_init,
7672 null_pointer_constant, NULL_TREE, NULL_TREE,
7673 0);
2f6e4e97 7674
3274deff
JW
7675 /* Check to see if we have already given an error message. */
7676 if (inside_init == error_mark_node)
7677 ;
3f75a254 7678 else if (require_constant && !TREE_CONSTANT (inside_init))
400fbf9f 7679 {
ea58ef42 7680 error_init (init_loc, "initializer element is not constant");
3e4093b6 7681 inside_init = error_mark_node;
400fbf9f 7682 }
3e4093b6 7683 else if (require_constant
116df786
RH
7684 && !initializer_constant_valid_p (inside_init,
7685 TREE_TYPE (inside_init)))
400fbf9f 7686 {
ea58ef42
MP
7687 error_init (init_loc, "initializer element is not computable at "
7688 "load time");
3e4093b6 7689 inside_init = error_mark_node;
400fbf9f 7690 }
928c19bb 7691 else if (require_constant && !maybe_const)
451b5e48 7692 pedwarn_init (init_loc, OPT_Wpedantic,
928c19bb 7693 "initializer element is not a constant expression");
3e4093b6
RS
7694
7695 return inside_init;
400fbf9f 7696 }
d9fc6069 7697
3e4093b6 7698 /* Come here only for records and arrays. */
d9fc6069 7699
3e4093b6 7700 if (COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
d9fc6069 7701 {
ea58ef42 7702 error_init (init_loc, "variable-sized object may not be initialized");
3e4093b6 7703 return error_mark_node;
d9fc6069 7704 }
3e4093b6 7705
ea58ef42 7706 error_init (init_loc, "invalid initializer");
3e4093b6 7707 return error_mark_node;
d9fc6069 7708}
400fbf9f 7709\f
3e4093b6 7710/* Handle initializers that use braces. */
400fbf9f 7711
3e4093b6
RS
7712/* Type of object we are accumulating a constructor for.
7713 This type is always a RECORD_TYPE, UNION_TYPE or ARRAY_TYPE. */
7714static tree constructor_type;
400fbf9f 7715
3e4093b6
RS
7716/* For a RECORD_TYPE or UNION_TYPE, this is the chain of fields
7717 left to fill. */
7718static tree constructor_fields;
400fbf9f 7719
3e4093b6
RS
7720/* For an ARRAY_TYPE, this is the specified index
7721 at which to store the next element we get. */
7722static tree constructor_index;
400fbf9f 7723
3e4093b6
RS
7724/* For an ARRAY_TYPE, this is the maximum index. */
7725static tree constructor_max_index;
400fbf9f 7726
3e4093b6
RS
7727/* For a RECORD_TYPE, this is the first field not yet written out. */
7728static tree constructor_unfilled_fields;
400fbf9f 7729
3e4093b6
RS
7730/* For an ARRAY_TYPE, this is the index of the first element
7731 not yet written out. */
7732static tree constructor_unfilled_index;
895ea614 7733
3e4093b6
RS
7734/* In a RECORD_TYPE, the byte index of the next consecutive field.
7735 This is so we can generate gaps between fields, when appropriate. */
7736static tree constructor_bit_index;
10d5caec 7737
3e4093b6
RS
7738/* If we are saving up the elements rather than allocating them,
7739 this is the list of elements so far (in reverse order,
7740 most recent first). */
9771b263 7741static vec<constructor_elt, va_gc> *constructor_elements;
ad47f1e5 7742
3e4093b6
RS
7743/* 1 if constructor should be incrementally stored into a constructor chain,
7744 0 if all the elements should be kept in AVL tree. */
7745static int constructor_incremental;
ad47f1e5 7746
3e4093b6
RS
7747/* 1 if so far this constructor's elements are all compile-time constants. */
7748static int constructor_constant;
ad47f1e5 7749
3e4093b6
RS
7750/* 1 if so far this constructor's elements are all valid address constants. */
7751static int constructor_simple;
ad47f1e5 7752
928c19bb
JM
7753/* 1 if this constructor has an element that cannot be part of a
7754 constant expression. */
7755static int constructor_nonconst;
7756
3e4093b6
RS
7757/* 1 if this constructor is erroneous so far. */
7758static int constructor_erroneous;
d45cf215 7759
9bac5cbb
G
7760/* 1 if this constructor is the universal zero initializer { 0 }. */
7761static int constructor_zeroinit;
7762
3e4093b6
RS
7763/* Structure for managing pending initializer elements, organized as an
7764 AVL tree. */
d45cf215 7765
3e4093b6 7766struct init_node
d45cf215 7767{
3e4093b6
RS
7768 struct init_node *left, *right;
7769 struct init_node *parent;
7770 int balance;
7771 tree purpose;
7772 tree value;
bbbbb16a 7773 tree origtype;
d45cf215
RS
7774};
7775
3e4093b6
RS
7776/* Tree of pending elements at this constructor level.
7777 These are elements encountered out of order
7778 which belong at places we haven't reached yet in actually
7779 writing the output.
7780 Will never hold tree nodes across GC runs. */
7781static struct init_node *constructor_pending_elts;
d45cf215 7782
3e4093b6
RS
7783/* The SPELLING_DEPTH of this constructor. */
7784static int constructor_depth;
d45cf215 7785
3e4093b6
RS
7786/* DECL node for which an initializer is being read.
7787 0 means we are reading a constructor expression
7788 such as (struct foo) {...}. */
7789static tree constructor_decl;
d45cf215 7790
3e4093b6
RS
7791/* Nonzero if this is an initializer for a top-level decl. */
7792static int constructor_top_level;
d45cf215 7793
3e4093b6
RS
7794/* Nonzero if there were any member designators in this initializer. */
7795static int constructor_designated;
d45cf215 7796
3e4093b6
RS
7797/* Nesting depth of designator list. */
7798static int designator_depth;
d45cf215 7799
3e4093b6 7800/* Nonzero if there were diagnosed errors in this designator list. */
b06df647 7801static int designator_erroneous;
d45cf215 7802
3e4093b6
RS
7803\f
7804/* This stack has a level for each implicit or explicit level of
7805 structuring in the initializer, including the outermost one. It
7806 saves the values of most of the variables above. */
d45cf215 7807
3e4093b6
RS
7808struct constructor_range_stack;
7809
7810struct constructor_stack
d45cf215 7811{
3e4093b6
RS
7812 struct constructor_stack *next;
7813 tree type;
7814 tree fields;
7815 tree index;
7816 tree max_index;
7817 tree unfilled_index;
7818 tree unfilled_fields;
7819 tree bit_index;
9771b263 7820 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7821 struct init_node *pending_elts;
7822 int offset;
7823 int depth;
916c5919 7824 /* If value nonzero, this value should replace the entire
3e4093b6 7825 constructor at this level. */
916c5919 7826 struct c_expr replacement_value;
3e4093b6
RS
7827 struct constructor_range_stack *range_stack;
7828 char constant;
7829 char simple;
928c19bb 7830 char nonconst;
3e4093b6
RS
7831 char implicit;
7832 char erroneous;
7833 char outer;
7834 char incremental;
7835 char designated;
976d5a22 7836 int designator_depth;
3e4093b6 7837};
d45cf215 7838
802415d1 7839static struct constructor_stack *constructor_stack;
d45cf215 7840
3e4093b6
RS
7841/* This stack represents designators from some range designator up to
7842 the last designator in the list. */
d45cf215 7843
3e4093b6
RS
7844struct constructor_range_stack
7845{
7846 struct constructor_range_stack *next, *prev;
7847 struct constructor_stack *stack;
7848 tree range_start;
7849 tree index;
7850 tree range_end;
7851 tree fields;
7852};
d45cf215 7853
802415d1 7854static struct constructor_range_stack *constructor_range_stack;
d45cf215 7855
3e4093b6
RS
7856/* This stack records separate initializers that are nested.
7857 Nested initializers can't happen in ANSI C, but GNU C allows them
7858 in cases like { ... (struct foo) { ... } ... }. */
d45cf215 7859
3e4093b6 7860struct initializer_stack
d45cf215 7861{
3e4093b6
RS
7862 struct initializer_stack *next;
7863 tree decl;
3e4093b6
RS
7864 struct constructor_stack *constructor_stack;
7865 struct constructor_range_stack *constructor_range_stack;
9771b263 7866 vec<constructor_elt, va_gc> *elements;
3e4093b6
RS
7867 struct spelling *spelling;
7868 struct spelling *spelling_base;
7869 int spelling_size;
7870 char top_level;
7871 char require_constant_value;
7872 char require_constant_elements;
5dd9a9d0 7873 rich_location *missing_brace_richloc;
3e4093b6 7874};
d45cf215 7875
802415d1 7876static struct initializer_stack *initializer_stack;
3e4093b6
RS
7877\f
7878/* Prepare to parse and output the initializer for variable DECL. */
400fbf9f
JW
7879
7880void
5dd9a9d0
DM
7881start_init (tree decl, tree asmspec_tree ATTRIBUTE_UNUSED, int top_level,
7882 rich_location *richloc)
400fbf9f 7883{
3e4093b6 7884 const char *locus;
cceb1885 7885 struct initializer_stack *p = XNEW (struct initializer_stack);
400fbf9f 7886
3e4093b6 7887 p->decl = constructor_decl;
3e4093b6
RS
7888 p->require_constant_value = require_constant_value;
7889 p->require_constant_elements = require_constant_elements;
7890 p->constructor_stack = constructor_stack;
7891 p->constructor_range_stack = constructor_range_stack;
7892 p->elements = constructor_elements;
7893 p->spelling = spelling;
7894 p->spelling_base = spelling_base;
7895 p->spelling_size = spelling_size;
7896 p->top_level = constructor_top_level;
7897 p->next = initializer_stack;
5dd9a9d0 7898 p->missing_brace_richloc = richloc;
3e4093b6 7899 initializer_stack = p;
400fbf9f 7900
3e4093b6 7901 constructor_decl = decl;
3e4093b6
RS
7902 constructor_designated = 0;
7903 constructor_top_level = top_level;
400fbf9f 7904
3fa8871b 7905 if (decl != NULL_TREE && decl != error_mark_node)
3e4093b6
RS
7906 {
7907 require_constant_value = TREE_STATIC (decl);
7908 require_constant_elements
7909 = ((TREE_STATIC (decl) || (pedantic && !flag_isoc99))
7910 /* For a scalar, you can always use any value to initialize,
7911 even within braces. */
296a8c2f 7912 && AGGREGATE_TYPE_P (TREE_TYPE (decl)));
88388a52 7913 locus = identifier_to_locale (IDENTIFIER_POINTER (DECL_NAME (decl)));
3e4093b6
RS
7914 }
7915 else
7916 {
7917 require_constant_value = 0;
7918 require_constant_elements = 0;
88388a52 7919 locus = _("(anonymous)");
3e4093b6 7920 }
b71c7f8a 7921
3e4093b6
RS
7922 constructor_stack = 0;
7923 constructor_range_stack = 0;
b71c7f8a 7924
9bac5cbb 7925 found_missing_braces = 0;
3e4093b6
RS
7926
7927 spelling_base = 0;
7928 spelling_size = 0;
7929 RESTORE_SPELLING_DEPTH (0);
7930
7931 if (locus)
7932 push_string (locus);
7933}
7934
7935void
7936finish_init (void)
b71c7f8a 7937{
3e4093b6 7938 struct initializer_stack *p = initializer_stack;
b71c7f8a 7939
3e4093b6
RS
7940 /* Free the whole constructor stack of this initializer. */
7941 while (constructor_stack)
7942 {
7943 struct constructor_stack *q = constructor_stack;
7944 constructor_stack = q->next;
7945 free (q);
7946 }
7947
366de0ce 7948 gcc_assert (!constructor_range_stack);
3e4093b6
RS
7949
7950 /* Pop back to the data of the outer initializer (if any). */
36579663 7951 free (spelling_base);
3aeb3655 7952
3e4093b6 7953 constructor_decl = p->decl;
3e4093b6
RS
7954 require_constant_value = p->require_constant_value;
7955 require_constant_elements = p->require_constant_elements;
7956 constructor_stack = p->constructor_stack;
7957 constructor_range_stack = p->constructor_range_stack;
7958 constructor_elements = p->elements;
7959 spelling = p->spelling;
7960 spelling_base = p->spelling_base;
7961 spelling_size = p->spelling_size;
7962 constructor_top_level = p->top_level;
7963 initializer_stack = p->next;
7964 free (p);
b71c7f8a 7965}
400fbf9f 7966\f
3e4093b6
RS
7967/* Call here when we see the initializer is surrounded by braces.
7968 This is instead of a call to push_init_level;
7969 it is matched by a call to pop_init_level.
400fbf9f 7970
3e4093b6
RS
7971 TYPE is the type to initialize, for a constructor expression.
7972 For an initializer for a decl, TYPE is zero. */
400fbf9f 7973
3e4093b6
RS
7974void
7975really_start_incremental_init (tree type)
400fbf9f 7976{
5d038c4c 7977 struct constructor_stack *p = XNEW (struct constructor_stack);
400fbf9f 7978
3fa8871b 7979 if (type == NULL_TREE)
3e4093b6 7980 type = TREE_TYPE (constructor_decl);
400fbf9f 7981
31521951 7982 if (VECTOR_TYPE_P (type)
b6fc2cdb 7983 && TYPE_VECTOR_OPAQUE (type))
3e4093b6 7984 error ("opaque vector types cannot be initialized");
400fbf9f 7985
3e4093b6
RS
7986 p->type = constructor_type;
7987 p->fields = constructor_fields;
7988 p->index = constructor_index;
7989 p->max_index = constructor_max_index;
7990 p->unfilled_index = constructor_unfilled_index;
7991 p->unfilled_fields = constructor_unfilled_fields;
7992 p->bit_index = constructor_bit_index;
7993 p->elements = constructor_elements;
7994 p->constant = constructor_constant;
7995 p->simple = constructor_simple;
928c19bb 7996 p->nonconst = constructor_nonconst;
3e4093b6
RS
7997 p->erroneous = constructor_erroneous;
7998 p->pending_elts = constructor_pending_elts;
7999 p->depth = constructor_depth;
916c5919
JM
8000 p->replacement_value.value = 0;
8001 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 8002 p->replacement_value.original_type = NULL;
3e4093b6
RS
8003 p->implicit = 0;
8004 p->range_stack = 0;
8005 p->outer = 0;
8006 p->incremental = constructor_incremental;
8007 p->designated = constructor_designated;
976d5a22 8008 p->designator_depth = designator_depth;
3e4093b6
RS
8009 p->next = 0;
8010 constructor_stack = p;
b13aca19 8011
3e4093b6
RS
8012 constructor_constant = 1;
8013 constructor_simple = 1;
928c19bb 8014 constructor_nonconst = 0;
3e4093b6 8015 constructor_depth = SPELLING_DEPTH ();
9771b263 8016 constructor_elements = NULL;
3e4093b6
RS
8017 constructor_pending_elts = 0;
8018 constructor_type = type;
8019 constructor_incremental = 1;
8020 constructor_designated = 0;
9bac5cbb 8021 constructor_zeroinit = 1;
3e4093b6 8022 designator_depth = 0;
b06df647 8023 designator_erroneous = 0;
400fbf9f 8024
3e636daf 8025 if (RECORD_OR_UNION_TYPE_P (constructor_type))
400fbf9f 8026 {
3e4093b6
RS
8027 constructor_fields = TYPE_FIELDS (constructor_type);
8028 /* Skip any nameless bit fields at the beginning. */
3fa8871b 8029 while (constructor_fields != NULL_TREE
7c30b12a 8030 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
910ad8de 8031 constructor_fields = DECL_CHAIN (constructor_fields);
05bccae2 8032
3e4093b6
RS
8033 constructor_unfilled_fields = constructor_fields;
8034 constructor_bit_index = bitsize_zero_node;
400fbf9f 8035 }
3e4093b6
RS
8036 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8037 {
8038 if (TYPE_DOMAIN (constructor_type))
8039 {
8040 constructor_max_index
8041 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 8042
3e4093b6
RS
8043 /* Detect non-empty initializations of zero-length arrays. */
8044 if (constructor_max_index == NULL_TREE
8045 && TYPE_SIZE (constructor_type))
9a9d280e 8046 constructor_max_index = integer_minus_one_node;
400fbf9f 8047
3e4093b6
RS
8048 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8049 to initialize VLAs will cause a proper error; avoid tree
8050 checking errors as well by setting a safe value. */
8051 if (constructor_max_index
8052 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 8053 constructor_max_index = integer_minus_one_node;
59c83dbf 8054
3e4093b6
RS
8055 constructor_index
8056 = convert (bitsizetype,
8057 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
59c83dbf 8058 }
3e4093b6 8059 else
493179da
JM
8060 {
8061 constructor_index = bitsize_zero_node;
8062 constructor_max_index = NULL_TREE;
8063 }
59c83dbf 8064
3e4093b6
RS
8065 constructor_unfilled_index = constructor_index;
8066 }
31521951 8067 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8068 {
8069 /* Vectors are like simple fixed-size arrays. */
8070 constructor_max_index =
c62c040f 8071 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
a0f0ab9f 8072 constructor_index = bitsize_zero_node;
3e4093b6
RS
8073 constructor_unfilled_index = constructor_index;
8074 }
8075 else
8076 {
8077 /* Handle the case of int x = {5}; */
8078 constructor_fields = constructor_type;
8079 constructor_unfilled_fields = constructor_type;
8080 }
8081}
8082\f
5dd9a9d0
DM
8083extern location_t last_init_list_comma;
8084
16595a1f
BS
8085/* Called when we see an open brace for a nested initializer. Finish
8086 off any pending levels with implicit braces. */
8087void
8088finish_implicit_inits (location_t loc, struct obstack *braced_init_obstack)
8089{
8090 while (constructor_stack->implicit)
8091 {
8092 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3fa8871b 8093 && constructor_fields == NULL_TREE)
16595a1f 8094 process_init_element (input_location,
5dd9a9d0
DM
8095 pop_init_level (loc, 1, braced_init_obstack,
8096 last_init_list_comma),
16595a1f
BS
8097 true, braced_init_obstack);
8098 else if (TREE_CODE (constructor_type) == ARRAY_TYPE
8099 && constructor_max_index
8100 && tree_int_cst_lt (constructor_max_index,
8101 constructor_index))
8102 process_init_element (input_location,
5dd9a9d0
DM
8103 pop_init_level (loc, 1, braced_init_obstack,
8104 last_init_list_comma),
16595a1f
BS
8105 true, braced_init_obstack);
8106 else
8107 break;
8108 }
8109}
8110
3e4093b6
RS
8111/* Push down into a subobject, for initialization.
8112 If this is for an explicit set of braces, IMPLICIT is 0.
8113 If it is because the next element belongs at a lower level,
8114 IMPLICIT is 1 (or 2 if the push is because of designator list). */
400fbf9f 8115
3e4093b6 8116void
ea58ef42
MP
8117push_init_level (location_t loc, int implicit,
8118 struct obstack *braced_init_obstack)
3e4093b6
RS
8119{
8120 struct constructor_stack *p;
8121 tree value = NULL_TREE;
400fbf9f 8122
3e4093b6
RS
8123 /* Unless this is an explicit brace, we need to preserve previous
8124 content if any. */
8125 if (implicit)
8126 {
3e636daf 8127 if (RECORD_OR_UNION_TYPE_P (constructor_type) && constructor_fields)
a1e3b3d9 8128 value = find_init_member (constructor_fields, braced_init_obstack);
3e4093b6 8129 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
a1e3b3d9 8130 value = find_init_member (constructor_index, braced_init_obstack);
400fbf9f
JW
8131 }
8132
5d038c4c 8133 p = XNEW (struct constructor_stack);
3e4093b6
RS
8134 p->type = constructor_type;
8135 p->fields = constructor_fields;
8136 p->index = constructor_index;
8137 p->max_index = constructor_max_index;
8138 p->unfilled_index = constructor_unfilled_index;
8139 p->unfilled_fields = constructor_unfilled_fields;
8140 p->bit_index = constructor_bit_index;
8141 p->elements = constructor_elements;
8142 p->constant = constructor_constant;
8143 p->simple = constructor_simple;
928c19bb 8144 p->nonconst = constructor_nonconst;
3e4093b6
RS
8145 p->erroneous = constructor_erroneous;
8146 p->pending_elts = constructor_pending_elts;
8147 p->depth = constructor_depth;
3fa8871b 8148 p->replacement_value.value = NULL_TREE;
916c5919 8149 p->replacement_value.original_code = ERROR_MARK;
6866c6e8 8150 p->replacement_value.original_type = NULL;
3e4093b6
RS
8151 p->implicit = implicit;
8152 p->outer = 0;
8153 p->incremental = constructor_incremental;
8154 p->designated = constructor_designated;
976d5a22 8155 p->designator_depth = designator_depth;
3e4093b6
RS
8156 p->next = constructor_stack;
8157 p->range_stack = 0;
8158 constructor_stack = p;
400fbf9f 8159
3e4093b6
RS
8160 constructor_constant = 1;
8161 constructor_simple = 1;
928c19bb 8162 constructor_nonconst = 0;
3e4093b6 8163 constructor_depth = SPELLING_DEPTH ();
9771b263 8164 constructor_elements = NULL;
3e4093b6
RS
8165 constructor_incremental = 1;
8166 constructor_designated = 0;
8167 constructor_pending_elts = 0;
8168 if (!implicit)
400fbf9f 8169 {
3e4093b6
RS
8170 p->range_stack = constructor_range_stack;
8171 constructor_range_stack = 0;
8172 designator_depth = 0;
b06df647 8173 designator_erroneous = 0;
3e4093b6 8174 }
400fbf9f 8175
3e4093b6
RS
8176 /* Don't die if an entire brace-pair level is superfluous
8177 in the containing level. */
3fa8871b 8178 if (constructor_type == NULL_TREE)
3e4093b6 8179 ;
3e636daf 8180 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8181 {
8182 /* Don't die if there are extra init elts at the end. */
3fa8871b
MP
8183 if (constructor_fields == NULL_TREE)
8184 constructor_type = NULL_TREE;
3e4093b6 8185 else
400fbf9f 8186 {
3e4093b6
RS
8187 constructor_type = TREE_TYPE (constructor_fields);
8188 push_member_name (constructor_fields);
8189 constructor_depth++;
400fbf9f 8190 }
6a358dcb
MP
8191 /* If upper initializer is designated, then mark this as
8192 designated too to prevent bogus warnings. */
8193 constructor_designated = p->designated;
3e4093b6
RS
8194 }
8195 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8196 {
8197 constructor_type = TREE_TYPE (constructor_type);
ae7e9ddd 8198 push_array_bounds (tree_to_uhwi (constructor_index));
3e4093b6 8199 constructor_depth++;
400fbf9f
JW
8200 }
8201
3fa8871b 8202 if (constructor_type == NULL_TREE)
400fbf9f 8203 {
ea58ef42 8204 error_init (loc, "extra brace group at end of initializer");
3fa8871b
MP
8205 constructor_fields = NULL_TREE;
8206 constructor_unfilled_fields = NULL_TREE;
3e4093b6 8207 return;
400fbf9f
JW
8208 }
8209
3e4093b6
RS
8210 if (value && TREE_CODE (value) == CONSTRUCTOR)
8211 {
8212 constructor_constant = TREE_CONSTANT (value);
8213 constructor_simple = TREE_STATIC (value);
928c19bb 8214 constructor_nonconst = CONSTRUCTOR_NON_CONST (value);
3e4093b6 8215 constructor_elements = CONSTRUCTOR_ELTS (value);
9771b263 8216 if (!vec_safe_is_empty (constructor_elements)
3e4093b6
RS
8217 && (TREE_CODE (constructor_type) == RECORD_TYPE
8218 || TREE_CODE (constructor_type) == ARRAY_TYPE))
a1e3b3d9 8219 set_nonincremental_init (braced_init_obstack);
3e4093b6 8220 }
400fbf9f 8221
9bac5cbb 8222 if (implicit == 1)
5dd9a9d0
DM
8223 {
8224 found_missing_braces = 1;
8225 if (initializer_stack->missing_brace_richloc)
8226 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8227 (loc, "{");
8228 }
400fbf9f 8229
3e636daf 8230 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
8231 {
8232 constructor_fields = TYPE_FIELDS (constructor_type);
8233 /* Skip any nameless bit fields at the beginning. */
3fa8871b 8234 while (constructor_fields != NULL_TREE
7c30b12a 8235 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
f7587ed0 8236 constructor_fields = DECL_CHAIN (constructor_fields);
103b7b17 8237
3e4093b6
RS
8238 constructor_unfilled_fields = constructor_fields;
8239 constructor_bit_index = bitsize_zero_node;
8240 }
31521951 8241 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8242 {
8243 /* Vectors are like simple fixed-size arrays. */
8244 constructor_max_index =
c62c040f
RG
8245 bitsize_int (TYPE_VECTOR_SUBPARTS (constructor_type) - 1);
8246 constructor_index = bitsize_int (0);
3e4093b6
RS
8247 constructor_unfilled_index = constructor_index;
8248 }
8249 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8250 {
8251 if (TYPE_DOMAIN (constructor_type))
8252 {
8253 constructor_max_index
8254 = TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type));
400fbf9f 8255
3e4093b6
RS
8256 /* Detect non-empty initializations of zero-length arrays. */
8257 if (constructor_max_index == NULL_TREE
8258 && TYPE_SIZE (constructor_type))
9a9d280e 8259 constructor_max_index = integer_minus_one_node;
de520661 8260
3e4093b6
RS
8261 /* constructor_max_index needs to be an INTEGER_CST. Attempts
8262 to initialize VLAs will cause a proper error; avoid tree
8263 checking errors as well by setting a safe value. */
8264 if (constructor_max_index
8265 && TREE_CODE (constructor_max_index) != INTEGER_CST)
9a9d280e 8266 constructor_max_index = integer_minus_one_node;
b62acd60 8267
3e4093b6
RS
8268 constructor_index
8269 = convert (bitsizetype,
8270 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
8271 }
8272 else
8273 constructor_index = bitsize_zero_node;
de520661 8274
3e4093b6
RS
8275 constructor_unfilled_index = constructor_index;
8276 if (value && TREE_CODE (value) == STRING_CST)
8277 {
8278 /* We need to split the char/wchar array into individual
8279 characters, so that we don't have to special case it
8280 everywhere. */
a1e3b3d9 8281 set_nonincremental_init_from_string (value, braced_init_obstack);
3e4093b6
RS
8282 }
8283 }
8284 else
8285 {
b4519d39 8286 if (constructor_type != error_mark_node)
96b40f8d 8287 warning_init (input_location, 0, "braces around scalar initializer");
3e4093b6
RS
8288 constructor_fields = constructor_type;
8289 constructor_unfilled_fields = constructor_type;
8290 }
8291}
8b6a5902 8292
3e4093b6 8293/* At the end of an implicit or explicit brace level,
916c5919
JM
8294 finish up that level of constructor. If a single expression
8295 with redundant braces initialized that level, return the
8296 c_expr structure for that expression. Otherwise, the original_code
8297 element is set to ERROR_MARK.
8298 If we were outputting the elements as they are read, return 0 as the value
3e4093b6 8299 from inner levels (process_init_element ignores that),
916c5919 8300 but return error_mark_node as the value from the outermost level
3e4093b6 8301 (that's what we want to put in DECL_INITIAL).
916c5919 8302 Otherwise, return a CONSTRUCTOR expression as the value. */
de520661 8303
916c5919 8304struct c_expr
ea58ef42 8305pop_init_level (location_t loc, int implicit,
5dd9a9d0
DM
8306 struct obstack *braced_init_obstack,
8307 location_t insert_before)
3e4093b6
RS
8308{
8309 struct constructor_stack *p;
916c5919 8310 struct c_expr ret;
3fa8871b 8311 ret.value = NULL_TREE;
916c5919 8312 ret.original_code = ERROR_MARK;
6866c6e8 8313 ret.original_type = NULL;
de520661 8314
3e4093b6
RS
8315 if (implicit == 0)
8316 {
8317 /* When we come to an explicit close brace,
8318 pop any inner levels that didn't have explicit braces. */
8319 while (constructor_stack->implicit)
34cf811f 8320 process_init_element (input_location,
5dd9a9d0
DM
8321 pop_init_level (loc, 1, braced_init_obstack,
8322 insert_before),
34cf811f 8323 true, braced_init_obstack);
366de0ce 8324 gcc_assert (!constructor_range_stack);
3e4093b6 8325 }
5dd9a9d0
DM
8326 else
8327 if (initializer_stack->missing_brace_richloc)
8328 initializer_stack->missing_brace_richloc->add_fixit_insert_before
8329 (insert_before, "}");
e5e809f4 8330
0066ef9c
RH
8331 /* Now output all pending elements. */
8332 constructor_incremental = 1;
a1e3b3d9 8333 output_pending_init_elements (1, braced_init_obstack);
0066ef9c 8334
3e4093b6 8335 p = constructor_stack;
e5e809f4 8336
3e4093b6
RS
8337 /* Error for initializing a flexible array member, or a zero-length
8338 array member in an inappropriate context. */
8339 if (constructor_type && constructor_fields
8340 && TREE_CODE (constructor_type) == ARRAY_TYPE
8341 && TYPE_DOMAIN (constructor_type)
3f75a254 8342 && !TYPE_MAX_VALUE (TYPE_DOMAIN (constructor_type)))
3e4093b6
RS
8343 {
8344 /* Silently discard empty initializations. The parser will
8345 already have pedwarned for empty brackets. */
8346 if (integer_zerop (constructor_unfilled_index))
8347 constructor_type = NULL_TREE;
366de0ce 8348 else
3e4093b6 8349 {
366de0ce 8350 gcc_assert (!TYPE_SIZE (constructor_type));
c22cacf3 8351
3e4093b6 8352 if (constructor_depth > 2)
ea58ef42 8353 error_init (loc, "initialization of flexible array member in a nested context");
fcf73884 8354 else
d033409e 8355 pedwarn_init (loc, OPT_Wpedantic,
509c9d60 8356 "initialization of a flexible array member");
de520661 8357
3e4093b6
RS
8358 /* We have already issued an error message for the existence
8359 of a flexible array member not at the end of the structure.
535a42b1 8360 Discard the initializer so that we do not die later. */
910ad8de 8361 if (DECL_CHAIN (constructor_fields) != NULL_TREE)
3e4093b6
RS
8362 constructor_type = NULL_TREE;
8363 }
3e4093b6 8364 }
de520661 8365
7b33f0c8
MP
8366 switch (vec_safe_length (constructor_elements))
8367 {
8368 case 0:
8369 /* Initialization with { } counts as zeroinit. */
8370 constructor_zeroinit = 1;
8371 break;
8372 case 1:
8373 /* This might be zeroinit as well. */
8374 if (integer_zerop ((*constructor_elements)[0].value))
8375 constructor_zeroinit = 1;
8376 break;
8377 default:
8378 /* If the constructor has more than one element, it can't be { 0 }. */
8379 constructor_zeroinit = 0;
8380 break;
8381 }
9bac5cbb
G
8382
8383 /* Warn when some structs are initialized with direct aggregation. */
8384 if (!implicit && found_missing_braces && warn_missing_braces
8385 && !constructor_zeroinit)
5dd9a9d0
DM
8386 {
8387 gcc_assert (initializer_stack->missing_brace_richloc);
64a5912c
DM
8388 warning_at (initializer_stack->missing_brace_richloc,
8389 OPT_Wmissing_braces,
8390 "missing braces around initializer");
5dd9a9d0 8391 }
9bac5cbb 8392
3e4093b6 8393 /* Warn when some struct elements are implicitly initialized to zero. */
eaac4679 8394 if (warn_missing_field_initializers
3e4093b6
RS
8395 && constructor_type
8396 && TREE_CODE (constructor_type) == RECORD_TYPE
8397 && constructor_unfilled_fields)
8398 {
8399 /* Do not warn for flexible array members or zero-length arrays. */
8400 while (constructor_unfilled_fields
3f75a254 8401 && (!DECL_SIZE (constructor_unfilled_fields)
3e4093b6 8402 || integer_zerop (DECL_SIZE (constructor_unfilled_fields))))
910ad8de 8403 constructor_unfilled_fields = DECL_CHAIN (constructor_unfilled_fields);
cc77d4d5 8404
49819fef
AM
8405 if (constructor_unfilled_fields
8406 /* Do not warn if this level of the initializer uses member
8407 designators; it is likely to be deliberate. */
8408 && !constructor_designated
84937de2 8409 /* Do not warn about initializing with { 0 } or with { }. */
49819fef 8410 && !constructor_zeroinit)
3e4093b6 8411 {
32397f22
MLI
8412 if (warning_at (input_location, OPT_Wmissing_field_initializers,
8413 "missing initializer for field %qD of %qT",
8414 constructor_unfilled_fields,
8415 constructor_type))
8416 inform (DECL_SOURCE_LOCATION (constructor_unfilled_fields),
ae933128 8417 "%qD declared here", constructor_unfilled_fields);
3e4093b6
RS
8418 }
8419 }
de520661 8420
3e4093b6 8421 /* Pad out the end of the structure. */
916c5919 8422 if (p->replacement_value.value)
3e4093b6
RS
8423 /* If this closes a superfluous brace pair,
8424 just pass out the element between them. */
916c5919 8425 ret = p->replacement_value;
3fa8871b 8426 else if (constructor_type == NULL_TREE)
3e4093b6 8427 ;
3e636daf 8428 else if (!RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6 8429 && TREE_CODE (constructor_type) != ARRAY_TYPE
31521951 8430 && !VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
8431 {
8432 /* A nonincremental scalar initializer--just return
8433 the element, after verifying there is just one. */
9771b263 8434 if (vec_safe_is_empty (constructor_elements))
3e4093b6
RS
8435 {
8436 if (!constructor_erroneous)
ea58ef42 8437 error_init (loc, "empty scalar initializer");
916c5919 8438 ret.value = error_mark_node;
3e4093b6 8439 }
9771b263 8440 else if (vec_safe_length (constructor_elements) != 1)
3e4093b6 8441 {
ea58ef42 8442 error_init (loc, "extra elements in scalar initializer");
9771b263 8443 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8444 }
8445 else
9771b263 8446 ret.value = (*constructor_elements)[0].value;
3e4093b6
RS
8447 }
8448 else
8449 {
8450 if (constructor_erroneous)
916c5919 8451 ret.value = error_mark_node;
3e4093b6
RS
8452 else
8453 {
916c5919 8454 ret.value = build_constructor (constructor_type,
4038c495 8455 constructor_elements);
3e4093b6 8456 if (constructor_constant)
51eed280 8457 TREE_CONSTANT (ret.value) = 1;
3e4093b6 8458 if (constructor_constant && constructor_simple)
916c5919 8459 TREE_STATIC (ret.value) = 1;
928c19bb
JM
8460 if (constructor_nonconst)
8461 CONSTRUCTOR_NON_CONST (ret.value) = 1;
3e4093b6
RS
8462 }
8463 }
de520661 8464
928c19bb
JM
8465 if (ret.value && TREE_CODE (ret.value) != CONSTRUCTOR)
8466 {
8467 if (constructor_nonconst)
8468 ret.original_code = C_MAYBE_CONST_EXPR;
8469 else if (ret.original_code == C_MAYBE_CONST_EXPR)
8470 ret.original_code = ERROR_MARK;
8471 }
8472
3e4093b6
RS
8473 constructor_type = p->type;
8474 constructor_fields = p->fields;
8475 constructor_index = p->index;
8476 constructor_max_index = p->max_index;
8477 constructor_unfilled_index = p->unfilled_index;
8478 constructor_unfilled_fields = p->unfilled_fields;
8479 constructor_bit_index = p->bit_index;
8480 constructor_elements = p->elements;
8481 constructor_constant = p->constant;
8482 constructor_simple = p->simple;
928c19bb 8483 constructor_nonconst = p->nonconst;
3e4093b6
RS
8484 constructor_erroneous = p->erroneous;
8485 constructor_incremental = p->incremental;
8486 constructor_designated = p->designated;
976d5a22 8487 designator_depth = p->designator_depth;
3e4093b6
RS
8488 constructor_pending_elts = p->pending_elts;
8489 constructor_depth = p->depth;
8490 if (!p->implicit)
8491 constructor_range_stack = p->range_stack;
8492 RESTORE_SPELLING_DEPTH (constructor_depth);
de520661 8493
3e4093b6
RS
8494 constructor_stack = p->next;
8495 free (p);
b621a4dd 8496
3fa8871b 8497 if (ret.value == NULL_TREE && constructor_stack == 0)
5d5e98dc 8498 ret.value = error_mark_node;
916c5919 8499 return ret;
3e4093b6 8500}
8b6a5902 8501
3e4093b6 8502/* Common handling for both array range and field name designators.
e3455240 8503 ARRAY argument is nonzero for array ranges. Returns false for success. */
400fbf9f 8504
e3455240 8505static bool
30af3a2b 8506set_designator (location_t loc, bool array,
ea58ef42 8507 struct obstack *braced_init_obstack)
de520661 8508{
3e4093b6
RS
8509 tree subtype;
8510 enum tree_code subcode;
de520661 8511
3e4093b6
RS
8512 /* Don't die if an entire brace-pair level is superfluous
8513 in the containing level. */
3fa8871b 8514 if (constructor_type == NULL_TREE)
e3455240 8515 return true;
de520661 8516
366de0ce
NS
8517 /* If there were errors in this designator list already, bail out
8518 silently. */
b06df647 8519 if (designator_erroneous)
e3455240 8520 return true;
e28cae4f 8521
3e4093b6
RS
8522 if (!designator_depth)
8523 {
366de0ce 8524 gcc_assert (!constructor_range_stack);
de520661 8525
3e4093b6
RS
8526 /* Designator list starts at the level of closest explicit
8527 braces. */
8528 while (constructor_stack->implicit)
34cf811f 8529 process_init_element (input_location,
5dd9a9d0
DM
8530 pop_init_level (loc, 1, braced_init_obstack,
8531 last_init_list_comma),
34cf811f 8532 true, braced_init_obstack);
3e4093b6 8533 constructor_designated = 1;
e3455240 8534 return false;
3e4093b6 8535 }
de520661 8536
366de0ce 8537 switch (TREE_CODE (constructor_type))
3c3fa147 8538 {
366de0ce
NS
8539 case RECORD_TYPE:
8540 case UNION_TYPE:
3e4093b6
RS
8541 subtype = TREE_TYPE (constructor_fields);
8542 if (subtype != error_mark_node)
8543 subtype = TYPE_MAIN_VARIANT (subtype);
366de0ce
NS
8544 break;
8545 case ARRAY_TYPE:
3e4093b6 8546 subtype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
366de0ce
NS
8547 break;
8548 default:
8549 gcc_unreachable ();
de520661 8550 }
400fbf9f 8551
3e4093b6
RS
8552 subcode = TREE_CODE (subtype);
8553 if (array && subcode != ARRAY_TYPE)
8554 {
ea58ef42 8555 error_init (loc, "array index in non-array initializer");
e3455240 8556 return true;
3e4093b6
RS
8557 }
8558 else if (!array && subcode != RECORD_TYPE && subcode != UNION_TYPE)
8559 {
ea58ef42 8560 error_init (loc, "field name not in record or union initializer");
e3455240 8561 return true;
3e4093b6 8562 }
d45cf215 8563
3e4093b6 8564 constructor_designated = 1;
16595a1f 8565 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 8566 push_init_level (loc, 2, braced_init_obstack);
e3455240 8567 return false;
de520661 8568}
400fbf9f 8569
3e4093b6
RS
8570/* If there are range designators in designator list, push a new designator
8571 to constructor_range_stack. RANGE_END is end of such stack range or
8572 NULL_TREE if there is no range designator at this level. */
400fbf9f 8573
3e4093b6 8574static void
a1e3b3d9 8575push_range_stack (tree range_end, struct obstack * braced_init_obstack)
3e4093b6
RS
8576{
8577 struct constructor_range_stack *p;
400fbf9f 8578
a1e3b3d9
LB
8579 p = (struct constructor_range_stack *)
8580 obstack_alloc (braced_init_obstack,
8581 sizeof (struct constructor_range_stack));
3e4093b6
RS
8582 p->prev = constructor_range_stack;
8583 p->next = 0;
8584 p->fields = constructor_fields;
8585 p->range_start = constructor_index;
8586 p->index = constructor_index;
8587 p->stack = constructor_stack;
8588 p->range_end = range_end;
8b6a5902 8589 if (constructor_range_stack)
3e4093b6
RS
8590 constructor_range_stack->next = p;
8591 constructor_range_stack = p;
de520661 8592}
400fbf9f 8593
3e4093b6
RS
8594/* Within an array initializer, specify the next index to be initialized.
8595 FIRST is that index. If LAST is nonzero, then initialize a range
8596 of indices, running from FIRST through LAST. */
5a7ec9d9 8597
de520661 8598void
ea58ef42 8599set_init_index (location_t loc, tree first, tree last,
d033409e 8600 struct obstack *braced_init_obstack)
de520661 8601{
30af3a2b 8602 if (set_designator (loc, true, braced_init_obstack))
3e4093b6 8603 return;
de520661 8604
b06df647 8605 designator_erroneous = 1;
de520661 8606
3ea8cd06
JM
8607 if (!INTEGRAL_TYPE_P (TREE_TYPE (first))
8608 || (last && !INTEGRAL_TYPE_P (TREE_TYPE (last))))
8609 {
ea58ef42 8610 error_init (loc, "array index in initializer not of integer type");
3ea8cd06
JM
8611 return;
8612 }
8613
2b6da65c
JM
8614 if (TREE_CODE (first) != INTEGER_CST)
8615 {
8616 first = c_fully_fold (first, false, NULL);
8617 if (TREE_CODE (first) == INTEGER_CST)
d033409e 8618 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8619 "array index in initializer is not "
8620 "an integer constant expression");
8621 }
8622
8623 if (last && TREE_CODE (last) != INTEGER_CST)
8624 {
8625 last = c_fully_fold (last, false, NULL);
8626 if (TREE_CODE (last) == INTEGER_CST)
d033409e 8627 pedwarn_init (loc, OPT_Wpedantic,
2b6da65c
JM
8628 "array index in initializer is not "
8629 "an integer constant expression");
8630 }
8631
3e4093b6 8632 if (TREE_CODE (first) != INTEGER_CST)
ea58ef42 8633 error_init (loc, "nonconstant array index in initializer");
3fa8871b 8634 else if (last != NULL_TREE && TREE_CODE (last) != INTEGER_CST)
ea58ef42 8635 error_init (loc, "nonconstant array index in initializer");
3e4093b6 8636 else if (TREE_CODE (constructor_type) != ARRAY_TYPE)
ea58ef42 8637 error_init (loc, "array index in non-array initializer");
622adc7e 8638 else if (tree_int_cst_sgn (first) == -1)
ea58ef42 8639 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6
RS
8640 else if (constructor_max_index
8641 && tree_int_cst_lt (constructor_max_index, first))
ea58ef42 8642 error_init (loc, "array index in initializer exceeds array bounds");
3e4093b6 8643 else
de520661 8644 {
928c19bb
JM
8645 constant_expression_warning (first);
8646 if (last)
8647 constant_expression_warning (last);
3e4093b6 8648 constructor_index = convert (bitsizetype, first);
40d3d530
JR
8649 if (tree_int_cst_lt (constructor_index, first))
8650 {
8651 constructor_index = copy_node (constructor_index);
8652 TREE_OVERFLOW (constructor_index) = 1;
8653 }
665f2503 8654
3e4093b6 8655 if (last)
2bede729 8656 {
3e4093b6 8657 if (tree_int_cst_equal (first, last))
3fa8871b 8658 last = NULL_TREE;
3e4093b6
RS
8659 else if (tree_int_cst_lt (last, first))
8660 {
ea58ef42 8661 error_init (loc, "empty index range in initializer");
3fa8871b 8662 last = NULL_TREE;
3e4093b6
RS
8663 }
8664 else
8665 {
8666 last = convert (bitsizetype, last);
3fa8871b 8667 if (constructor_max_index != NULL_TREE
3e4093b6
RS
8668 && tree_int_cst_lt (constructor_max_index, last))
8669 {
ea58ef42
MP
8670 error_init (loc, "array index range in initializer exceeds "
8671 "array bounds");
3fa8871b 8672 last = NULL_TREE;
3e4093b6
RS
8673 }
8674 }
2bede729 8675 }
fed3cef0 8676
3e4093b6 8677 designator_depth++;
b06df647 8678 designator_erroneous = 0;
3e4093b6 8679 if (constructor_range_stack || last)
a1e3b3d9 8680 push_range_stack (last, braced_init_obstack);
de520661 8681 }
de520661 8682}
3e4093b6
RS
8683
8684/* Within a struct initializer, specify the next field to be initialized. */
400fbf9f 8685
de520661 8686void
f7e4f2e3 8687set_init_label (location_t loc, tree fieldname, location_t fieldname_loc,
ea58ef42 8688 struct obstack *braced_init_obstack)
de520661 8689{
0fb96aa4 8690 tree field;
94ba5069 8691
30af3a2b 8692 if (set_designator (loc, false, braced_init_obstack))
3e4093b6
RS
8693 return;
8694
b06df647 8695 designator_erroneous = 1;
3e4093b6 8696
3e636daf 8697 if (!RECORD_OR_UNION_TYPE_P (constructor_type))
94ba5069 8698 {
ea58ef42 8699 error_init (loc, "field name not in record or union initializer");
3e4093b6 8700 return;
94ba5069
RS
8701 }
8702
0fb96aa4 8703 field = lookup_field (constructor_type, fieldname);
8b6a5902 8704
3fa8871b 8705 if (field == NULL_TREE)
f7e4f2e3
DM
8706 {
8707 tree guessed_id = lookup_field_fuzzy (constructor_type, fieldname);
8708 if (guessed_id)
8709 {
264757fb
DM
8710 gcc_rich_location rich_loc (fieldname_loc);
8711 rich_loc.add_fixit_misspelled_id (fieldname_loc, guessed_id);
64a5912c
DM
8712 error_at (&rich_loc,
8713 "%qT has no member named %qE; did you mean %qE?",
8714 constructor_type, fieldname, guessed_id);
f7e4f2e3
DM
8715 }
8716 else
8717 error_at (fieldname_loc, "%qT has no member named %qE",
8718 constructor_type, fieldname);
8719 }
3e4093b6 8720 else
0fb96aa4
JM
8721 do
8722 {
8723 constructor_fields = TREE_VALUE (field);
8724 designator_depth++;
8725 designator_erroneous = 0;
8726 if (constructor_range_stack)
8727 push_range_stack (NULL_TREE, braced_init_obstack);
8728 field = TREE_CHAIN (field);
8729 if (field)
8730 {
30af3a2b 8731 if (set_designator (loc, false, braced_init_obstack))
0fb96aa4
JM
8732 return;
8733 }
8734 }
8735 while (field != NULL_TREE);
3e4093b6
RS
8736}
8737\f
8738/* Add a new initializer to the tree of pending initializers. PURPOSE
8739 identifies the initializer, either array index or field in a structure.
bbbbb16a
ILT
8740 VALUE is the value of that index or field. If ORIGTYPE is not
8741 NULL_TREE, it is the original type of VALUE.
b295aee2
JJ
8742
8743 IMPLICIT is true if value comes from pop_init_level (1),
8744 the new initializer has been merged with the existing one
8745 and thus no warnings should be emitted about overriding an
8746 existing initializer. */
de520661 8747
3e4093b6 8748static void
96b40f8d
MP
8749add_pending_init (location_t loc, tree purpose, tree value, tree origtype,
8750 bool implicit, struct obstack *braced_init_obstack)
3e4093b6
RS
8751{
8752 struct init_node *p, **q, *r;
8753
8754 q = &constructor_pending_elts;
8755 p = 0;
8756
8757 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 8758 {
3e4093b6 8759 while (*q != 0)
91fa3c30 8760 {
3e4093b6
RS
8761 p = *q;
8762 if (tree_int_cst_lt (purpose, p->purpose))
8763 q = &p->left;
8764 else if (tree_int_cst_lt (p->purpose, purpose))
8765 q = &p->right;
8766 else
8767 {
b295aee2
JJ
8768 if (!implicit)
8769 {
8770 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8771 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8772 "initialized field with side-effects "
8773 "overwritten");
b295aee2 8774 else if (warn_override_init)
96b40f8d
MP
8775 warning_init (loc, OPT_Woverride_init,
8776 "initialized field overwritten");
b295aee2 8777 }
3e4093b6 8778 p->value = value;
bbbbb16a 8779 p->origtype = origtype;
3e4093b6
RS
8780 return;
8781 }
91fa3c30 8782 }
de520661 8783 }
3e4093b6 8784 else
de520661 8785 {
3e4093b6 8786 tree bitpos;
400fbf9f 8787
3e4093b6
RS
8788 bitpos = bit_position (purpose);
8789 while (*q != NULL)
8790 {
8791 p = *q;
8792 if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
8793 q = &p->left;
8794 else if (p->purpose != purpose)
8795 q = &p->right;
8796 else
8797 {
b295aee2
JJ
8798 if (!implicit)
8799 {
8800 if (TREE_SIDE_EFFECTS (p->value))
755e528f 8801 warning_init (loc, OPT_Woverride_init_side_effects,
96b40f8d
MP
8802 "initialized field with side-effects "
8803 "overwritten");
b295aee2 8804 else if (warn_override_init)
96b40f8d
MP
8805 warning_init (loc, OPT_Woverride_init,
8806 "initialized field overwritten");
b295aee2 8807 }
3e4093b6 8808 p->value = value;
bbbbb16a 8809 p->origtype = origtype;
3e4093b6
RS
8810 return;
8811 }
8812 }
91fa3c30 8813 }
b71c7f8a 8814
a1e3b3d9
LB
8815 r = (struct init_node *) obstack_alloc (braced_init_obstack,
8816 sizeof (struct init_node));
3e4093b6
RS
8817 r->purpose = purpose;
8818 r->value = value;
bbbbb16a 8819 r->origtype = origtype;
8b6a5902 8820
3e4093b6
RS
8821 *q = r;
8822 r->parent = p;
8823 r->left = 0;
8824 r->right = 0;
8825 r->balance = 0;
b71c7f8a 8826
3e4093b6 8827 while (p)
de520661 8828 {
3e4093b6 8829 struct init_node *s;
665f2503 8830
3e4093b6 8831 if (r == p->left)
2bede729 8832 {
3e4093b6
RS
8833 if (p->balance == 0)
8834 p->balance = -1;
8835 else if (p->balance < 0)
8836 {
8837 if (r->balance < 0)
8838 {
8839 /* L rotation. */
8840 p->left = r->right;
8841 if (p->left)
8842 p->left->parent = p;
8843 r->right = p;
e7b6a0ee 8844
3e4093b6
RS
8845 p->balance = 0;
8846 r->balance = 0;
39bc99c2 8847
3e4093b6
RS
8848 s = p->parent;
8849 p->parent = r;
8850 r->parent = s;
8851 if (s)
8852 {
8853 if (s->left == p)
8854 s->left = r;
8855 else
8856 s->right = r;
8857 }
8858 else
8859 constructor_pending_elts = r;
8860 }
8861 else
8862 {
8863 /* LR rotation. */
8864 struct init_node *t = r->right;
e7b6a0ee 8865
3e4093b6
RS
8866 r->right = t->left;
8867 if (r->right)
8868 r->right->parent = r;
8869 t->left = r;
8870
8871 p->left = t->right;
8872 if (p->left)
8873 p->left->parent = p;
8874 t->right = p;
8875
8876 p->balance = t->balance < 0;
8877 r->balance = -(t->balance > 0);
8878 t->balance = 0;
8879
8880 s = p->parent;
8881 p->parent = t;
8882 r->parent = t;
8883 t->parent = s;
8884 if (s)
8885 {
8886 if (s->left == p)
8887 s->left = t;
8888 else
8889 s->right = t;
8890 }
8891 else
8892 constructor_pending_elts = t;
8893 }
8894 break;
8895 }
8896 else
8897 {
8898 /* p->balance == +1; growth of left side balances the node. */
8899 p->balance = 0;
8900 break;
8901 }
2bede729 8902 }
3e4093b6
RS
8903 else /* r == p->right */
8904 {
8905 if (p->balance == 0)
8906 /* Growth propagation from right side. */
8907 p->balance++;
8908 else if (p->balance > 0)
8909 {
8910 if (r->balance > 0)
8911 {
8912 /* R rotation. */
8913 p->right = r->left;
8914 if (p->right)
8915 p->right->parent = p;
8916 r->left = p;
8917
8918 p->balance = 0;
8919 r->balance = 0;
8920
8921 s = p->parent;
8922 p->parent = r;
8923 r->parent = s;
8924 if (s)
8925 {
8926 if (s->left == p)
8927 s->left = r;
8928 else
8929 s->right = r;
8930 }
8931 else
8932 constructor_pending_elts = r;
8933 }
8934 else /* r->balance == -1 */
8935 {
8936 /* RL rotation */
8937 struct init_node *t = r->left;
8938
8939 r->left = t->right;
8940 if (r->left)
8941 r->left->parent = r;
8942 t->right = r;
8943
8944 p->right = t->left;
8945 if (p->right)
8946 p->right->parent = p;
8947 t->left = p;
8948
8949 r->balance = (t->balance < 0);
8950 p->balance = -(t->balance > 0);
8951 t->balance = 0;
8952
8953 s = p->parent;
8954 p->parent = t;
8955 r->parent = t;
8956 t->parent = s;
8957 if (s)
8958 {
8959 if (s->left == p)
8960 s->left = t;
8961 else
8962 s->right = t;
8963 }
8964 else
8965 constructor_pending_elts = t;
8966 }
8967 break;
8968 }
8969 else
8970 {
8971 /* p->balance == -1; growth of right side balances the node. */
8972 p->balance = 0;
8973 break;
8974 }
8975 }
8976
8977 r = p;
8978 p = p->parent;
8979 }
8980}
8981
8982/* Build AVL tree from a sorted chain. */
8983
8984static void
a1e3b3d9 8985set_nonincremental_init (struct obstack * braced_init_obstack)
3e4093b6 8986{
4038c495
GB
8987 unsigned HOST_WIDE_INT ix;
8988 tree index, value;
3e4093b6
RS
8989
8990 if (TREE_CODE (constructor_type) != RECORD_TYPE
8991 && TREE_CODE (constructor_type) != ARRAY_TYPE)
8992 return;
8993
4038c495 8994 FOR_EACH_CONSTRUCTOR_ELT (constructor_elements, ix, index, value)
96b40f8d
MP
8995 add_pending_init (input_location, index, value, NULL_TREE, true,
8996 braced_init_obstack);
9771b263 8997 constructor_elements = NULL;
3e4093b6
RS
8998 if (TREE_CODE (constructor_type) == RECORD_TYPE)
8999 {
9000 constructor_unfilled_fields = TYPE_FIELDS (constructor_type);
9001 /* Skip any nameless bit fields at the beginning. */
3fa8871b 9002 while (constructor_unfilled_fields != NULL_TREE
7c30b12a 9003 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
3e4093b6 9004 constructor_unfilled_fields = TREE_CHAIN (constructor_unfilled_fields);
fed3cef0 9005
de520661 9006 }
3e4093b6 9007 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
de520661 9008 {
3e4093b6
RS
9009 if (TYPE_DOMAIN (constructor_type))
9010 constructor_unfilled_index
9011 = convert (bitsizetype,
9012 TYPE_MIN_VALUE (TYPE_DOMAIN (constructor_type)));
9013 else
9014 constructor_unfilled_index = bitsize_zero_node;
de520661 9015 }
3e4093b6 9016 constructor_incremental = 0;
de520661 9017}
400fbf9f 9018
3e4093b6 9019/* Build AVL tree from a string constant. */
de520661 9020
3e4093b6 9021static void
a1e3b3d9
LB
9022set_nonincremental_init_from_string (tree str,
9023 struct obstack * braced_init_obstack)
de520661 9024{
3e4093b6
RS
9025 tree value, purpose, type;
9026 HOST_WIDE_INT val[2];
9027 const char *p, *end;
9028 int byte, wchar_bytes, charwidth, bitpos;
de520661 9029
366de0ce 9030 gcc_assert (TREE_CODE (constructor_type) == ARRAY_TYPE);
940ff66d 9031
c466b2cd 9032 wchar_bytes = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (str))) / BITS_PER_UNIT;
3e4093b6 9033 charwidth = TYPE_PRECISION (char_type_node);
2f1364c2
JJ
9034 gcc_assert ((size_t) wchar_bytes * charwidth
9035 <= ARRAY_SIZE (val) * HOST_BITS_PER_WIDE_INT);
3e4093b6
RS
9036 type = TREE_TYPE (constructor_type);
9037 p = TREE_STRING_POINTER (str);
9038 end = p + TREE_STRING_LENGTH (str);
91fa3c30 9039
3e4093b6 9040 for (purpose = bitsize_zero_node;
8824edff
JJ
9041 p < end
9042 && !(constructor_max_index
9043 && tree_int_cst_lt (constructor_max_index, purpose));
3e4093b6 9044 purpose = size_binop (PLUS_EXPR, purpose, bitsize_one_node))
584ef5fe 9045 {
3e4093b6 9046 if (wchar_bytes == 1)
ffc5c6a9 9047 {
807e902e
KZ
9048 val[0] = (unsigned char) *p++;
9049 val[1] = 0;
ffc5c6a9
RH
9050 }
9051 else
3e4093b6 9052 {
3e4093b6 9053 val[1] = 0;
807e902e 9054 val[0] = 0;
3e4093b6
RS
9055 for (byte = 0; byte < wchar_bytes; byte++)
9056 {
9057 if (BYTES_BIG_ENDIAN)
9058 bitpos = (wchar_bytes - byte - 1) * charwidth;
9059 else
9060 bitpos = byte * charwidth;
2f1364c2 9061 val[bitpos / HOST_BITS_PER_WIDE_INT]
3e4093b6
RS
9062 |= ((unsigned HOST_WIDE_INT) ((unsigned char) *p++))
9063 << (bitpos % HOST_BITS_PER_WIDE_INT);
9064 }
9065 }
584ef5fe 9066
8df83eae 9067 if (!TYPE_UNSIGNED (type))
3e4093b6
RS
9068 {
9069 bitpos = ((wchar_bytes - 1) * charwidth) + HOST_BITS_PER_CHAR;
9070 if (bitpos < HOST_BITS_PER_WIDE_INT)
9071 {
e3fe09c1 9072 if (val[0] & (HOST_WIDE_INT_1 << (bitpos - 1)))
3e4093b6 9073 {
aa256c4a 9074 val[0] |= HOST_WIDE_INT_M1U << bitpos;
807e902e 9075 val[1] = -1;
3e4093b6
RS
9076 }
9077 }
9078 else if (bitpos == HOST_BITS_PER_WIDE_INT)
9079 {
807e902e
KZ
9080 if (val[0] < 0)
9081 val[1] = -1;
3e4093b6 9082 }
e3fe09c1 9083 else if (val[1] & (HOST_WIDE_INT_1
3e4093b6 9084 << (bitpos - 1 - HOST_BITS_PER_WIDE_INT)))
aa256c4a 9085 val[1] |= HOST_WIDE_INT_M1U << (bitpos - HOST_BITS_PER_WIDE_INT);
3e4093b6 9086 }
ffc5c6a9 9087
807e902e
KZ
9088 value = wide_int_to_tree (type,
9089 wide_int::from_array (val, 2,
9090 HOST_BITS_PER_WIDE_INT * 2));
96b40f8d 9091 add_pending_init (input_location, purpose, value, NULL_TREE, true,
a1e3b3d9 9092 braced_init_obstack);
9dfcc8db
BH
9093 }
9094
3e4093b6
RS
9095 constructor_incremental = 0;
9096}
de520661 9097
6574d78e 9098/* Return value of FIELD in pending initializer or NULL_TREE if the field was
3e4093b6
RS
9099 not initialized yet. */
9100
9101static tree
a1e3b3d9 9102find_init_member (tree field, struct obstack * braced_init_obstack)
3e4093b6
RS
9103{
9104 struct init_node *p;
9105
9106 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
19d76e60 9107 {
3e4093b6
RS
9108 if (constructor_incremental
9109 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 9110 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
9111
9112 p = constructor_pending_elts;
9113 while (p)
19d76e60 9114 {
3e4093b6
RS
9115 if (tree_int_cst_lt (field, p->purpose))
9116 p = p->left;
9117 else if (tree_int_cst_lt (p->purpose, field))
9118 p = p->right;
9119 else
9120 return p->value;
19d76e60 9121 }
19d76e60 9122 }
3e4093b6 9123 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
de520661 9124 {
3e4093b6 9125 tree bitpos = bit_position (field);
de520661 9126
3e4093b6
RS
9127 if (constructor_incremental
9128 && (!constructor_unfilled_fields
9129 || tree_int_cst_lt (bitpos,
9130 bit_position (constructor_unfilled_fields))))
a1e3b3d9 9131 set_nonincremental_init (braced_init_obstack);
de520661 9132
3e4093b6
RS
9133 p = constructor_pending_elts;
9134 while (p)
9135 {
9136 if (field == p->purpose)
9137 return p->value;
9138 else if (tree_int_cst_lt (bitpos, bit_position (p->purpose)))
9139 p = p->left;
9140 else
9141 p = p->right;
9142 }
9143 }
9144 else if (TREE_CODE (constructor_type) == UNION_TYPE)
de520661 9145 {
9771b263
DN
9146 if (!vec_safe_is_empty (constructor_elements)
9147 && (constructor_elements->last ().index == field))
9148 return constructor_elements->last ().value;
de520661 9149 }
6574d78e 9150 return NULL_TREE;
de520661
RS
9151}
9152
3e4093b6
RS
9153/* "Output" the next constructor element.
9154 At top level, really output it to assembler code now.
9155 Otherwise, collect it in a list from which we will make a CONSTRUCTOR.
bbbbb16a 9156 If ORIGTYPE is not NULL_TREE, it is the original type of VALUE.
3e4093b6
RS
9157 TYPE is the data type that the containing data type wants here.
9158 FIELD is the field (a FIELD_DECL) or the index that this element fills.
916c5919
JM
9159 If VALUE is a string constant, STRICT_STRING is true if it is
9160 unparenthesized or we should not warn here for it being parenthesized.
9161 For other types of VALUE, STRICT_STRING is not used.
8b6a5902 9162
30af3a2b
MP
9163 PENDING if true means output pending elements that belong
9164 right after this element. (PENDING is normally true;
9165 it is false while outputting pending elements, to avoid recursion.)
b295aee2
JJ
9166
9167 IMPLICIT is true if value comes from pop_init_level (1),
9168 the new initializer has been merged with the existing one
9169 and thus no warnings should be emitted about overriding an
9170 existing initializer. */
8b6a5902 9171
3e4093b6 9172static void
34cf811f 9173output_init_element (location_t loc, tree value, tree origtype,
30af3a2b 9174 bool strict_string, tree type, tree field, bool pending,
34cf811f 9175 bool implicit, struct obstack * braced_init_obstack)
3e4093b6 9176{
8ce94e44 9177 tree semantic_type = NULL_TREE;
928c19bb
JM
9178 bool maybe_const = true;
9179 bool npc;
4038c495 9180
0a880880 9181 if (type == error_mark_node || value == error_mark_node)
8b6a5902 9182 {
3e4093b6
RS
9183 constructor_erroneous = 1;
9184 return;
8b6a5902 9185 }
46bdb9cf
JM
9186 if (TREE_CODE (TREE_TYPE (value)) == ARRAY_TYPE
9187 && (TREE_CODE (value) == STRING_CST
9188 || TREE_CODE (value) == COMPOUND_LITERAL_EXPR)
9189 && !(TREE_CODE (value) == STRING_CST
9190 && TREE_CODE (type) == ARRAY_TYPE
9191 && INTEGRAL_TYPE_P (TREE_TYPE (type)))
9192 && !comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (value)),
9193 TYPE_MAIN_VARIANT (type)))
c2255bc4 9194 value = array_to_pointer_conversion (input_location, value);
8b6a5902 9195
3e4093b6 9196 if (TREE_CODE (value) == COMPOUND_LITERAL_EXPR
4435bb92 9197 && require_constant_value && pending)
8b6a5902 9198 {
3e4093b6
RS
9199 /* As an extension, allow initializing objects with static storage
9200 duration with compound literals (which are then treated just as
9201 the brace enclosed list they contain). */
4435bb92
MP
9202 if (flag_isoc99)
9203 pedwarn_init (loc, OPT_Wpedantic, "initializer element is not "
9204 "constant");
3e4093b6
RS
9205 tree decl = COMPOUND_LITERAL_EXPR_DECL (value);
9206 value = DECL_INITIAL (decl);
8b6a5902
JJ
9207 }
9208
928c19bb 9209 npc = null_pointer_constant_p (value);
8ce94e44
JM
9210 if (TREE_CODE (value) == EXCESS_PRECISION_EXPR)
9211 {
9212 semantic_type = TREE_TYPE (value);
9213 value = TREE_OPERAND (value, 0);
9214 }
928c19bb
JM
9215 value = c_fully_fold (value, require_constant_value, &maybe_const);
9216
3e4093b6
RS
9217 if (value == error_mark_node)
9218 constructor_erroneous = 1;
9219 else if (!TREE_CONSTANT (value))
9220 constructor_constant = 0;
ee45a32d
EB
9221 else if (!initializer_constant_valid_p (value,
9222 TREE_TYPE (value),
9223 AGGREGATE_TYPE_P (constructor_type)
9224 && TYPE_REVERSE_STORAGE_ORDER
9225 (constructor_type))
3e636daf 9226 || (RECORD_OR_UNION_TYPE_P (constructor_type)
3e4093b6
RS
9227 && DECL_C_BIT_FIELD (field)
9228 && TREE_CODE (value) != INTEGER_CST))
9229 constructor_simple = 0;
928c19bb
JM
9230 if (!maybe_const)
9231 constructor_nonconst = 1;
3e4093b6 9232
4378d117
MS
9233 /* Digest the initializer and issue any errors about incompatible
9234 types before issuing errors about non-constant initializers. */
9235 tree new_value = value;
9236 if (semantic_type)
9237 new_value = build1 (EXCESS_PRECISION_EXPR, semantic_type, value);
9238 new_value = digest_init (loc, type, new_value, origtype, npc, strict_string,
9239 require_constant_value);
9240 if (new_value == error_mark_node)
9241 {
9242 constructor_erroneous = 1;
9243 return;
9244 }
9245 if (require_constant_value || require_constant_elements)
9246 constant_expression_warning (new_value);
9247
9248 /* Proceed to check the constness of the original initializer. */
116df786 9249 if (!initializer_constant_valid_p (value, TREE_TYPE (value)))
8b6a5902 9250 {
116df786
RH
9251 if (require_constant_value)
9252 {
ea58ef42 9253 error_init (loc, "initializer element is not constant");
116df786
RH
9254 value = error_mark_node;
9255 }
9256 else if (require_constant_elements)
8337d1db 9257 pedwarn (loc, OPT_Wpedantic,
509c9d60 9258 "initializer element is not computable at load time");
8b6a5902 9259 }
928c19bb
JM
9260 else if (!maybe_const
9261 && (require_constant_value || require_constant_elements))
8337d1db 9262 pedwarn_init (loc, OPT_Wpedantic,
928c19bb 9263 "initializer element is not a constant expression");
3e4093b6 9264
81f40b79
ILT
9265 /* Issue -Wc++-compat warnings about initializing a bitfield with
9266 enum type. */
9267 if (warn_cxx_compat
9268 && field != NULL_TREE
9269 && TREE_CODE (field) == FIELD_DECL
9270 && DECL_BIT_FIELD_TYPE (field) != NULL_TREE
9271 && (TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))
9272 != TYPE_MAIN_VARIANT (type))
9273 && TREE_CODE (DECL_BIT_FIELD_TYPE (field)) == ENUMERAL_TYPE)
9274 {
9275 tree checktype = origtype != NULL_TREE ? origtype : TREE_TYPE (value);
9276 if (checktype != error_mark_node
9277 && (TYPE_MAIN_VARIANT (checktype)
9278 != TYPE_MAIN_VARIANT (DECL_BIT_FIELD_TYPE (field))))
96b40f8d 9279 warning_init (loc, OPT_Wc___compat,
81f40b79
ILT
9280 "enum conversion in initialization is invalid in C++");
9281 }
9282
ada6bad9
DP
9283 /* If this field is empty and does not have side effects (and is not at
9284 the end of structure), don't do anything other than checking the
9285 initializer. */
3e4093b6
RS
9286 if (field
9287 && (TREE_TYPE (field) == error_mark_node
9288 || (COMPLETE_TYPE_P (TREE_TYPE (field))
9289 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))
ada6bad9 9290 && !TREE_SIDE_EFFECTS (new_value)
3e4093b6 9291 && (TREE_CODE (constructor_type) == ARRAY_TYPE
910ad8de 9292 || DECL_CHAIN (field)))))
3e4093b6
RS
9293 return;
9294
4378d117
MS
9295 /* Finally, set VALUE to the initializer value digested above. */
9296 value = new_value;
8b6a5902 9297
3e4093b6
RS
9298 /* If this element doesn't come next in sequence,
9299 put it on constructor_pending_elts. */
9300 if (TREE_CODE (constructor_type) == ARRAY_TYPE
9301 && (!constructor_incremental
9302 || !tree_int_cst_equal (field, constructor_unfilled_index)))
8b6a5902 9303 {
3e4093b6
RS
9304 if (constructor_incremental
9305 && tree_int_cst_lt (field, constructor_unfilled_index))
a1e3b3d9 9306 set_nonincremental_init (braced_init_obstack);
3e4093b6 9307
96b40f8d 9308 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 9309 braced_init_obstack);
3e4093b6 9310 return;
8b6a5902 9311 }
3e4093b6
RS
9312 else if (TREE_CODE (constructor_type) == RECORD_TYPE
9313 && (!constructor_incremental
9314 || field != constructor_unfilled_fields))
8b6a5902 9315 {
3e4093b6
RS
9316 /* We do this for records but not for unions. In a union,
9317 no matter which field is specified, it can be initialized
9318 right away since it starts at the beginning of the union. */
9319 if (constructor_incremental)
9320 {
9321 if (!constructor_unfilled_fields)
a1e3b3d9 9322 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
9323 else
9324 {
9325 tree bitpos, unfillpos;
9326
9327 bitpos = bit_position (field);
9328 unfillpos = bit_position (constructor_unfilled_fields);
9329
9330 if (tree_int_cst_lt (bitpos, unfillpos))
a1e3b3d9 9331 set_nonincremental_init (braced_init_obstack);
3e4093b6
RS
9332 }
9333 }
9334
96b40f8d 9335 add_pending_init (loc, field, value, origtype, implicit,
a1e3b3d9 9336 braced_init_obstack);
3e4093b6 9337 return;
8b6a5902 9338 }
3e4093b6 9339 else if (TREE_CODE (constructor_type) == UNION_TYPE
9771b263 9340 && !vec_safe_is_empty (constructor_elements))
3e4093b6 9341 {
b295aee2
JJ
9342 if (!implicit)
9343 {
9771b263 9344 if (TREE_SIDE_EFFECTS (constructor_elements->last ().value))
755e528f 9345 warning_init (loc, OPT_Woverride_init_side_effects,
b295aee2
JJ
9346 "initialized field with side-effects overwritten");
9347 else if (warn_override_init)
96b40f8d
MP
9348 warning_init (loc, OPT_Woverride_init,
9349 "initialized field overwritten");
b295aee2 9350 }
8b6a5902 9351
3e4093b6 9352 /* We can have just one union field set. */
9771b263 9353 constructor_elements = NULL;
3e4093b6 9354 }
8b6a5902 9355
3e4093b6
RS
9356 /* Otherwise, output this element either to
9357 constructor_elements or to the assembler file. */
8b6a5902 9358
f32682ca 9359 constructor_elt celt = {field, value};
9771b263 9360 vec_safe_push (constructor_elements, celt);
8b6a5902 9361
3e4093b6
RS
9362 /* Advance the variable that indicates sequential elements output. */
9363 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9364 constructor_unfilled_index
db3927fb
AH
9365 = size_binop_loc (input_location, PLUS_EXPR, constructor_unfilled_index,
9366 bitsize_one_node);
3e4093b6
RS
9367 else if (TREE_CODE (constructor_type) == RECORD_TYPE)
9368 {
9369 constructor_unfilled_fields
910ad8de 9370 = DECL_CHAIN (constructor_unfilled_fields);
8b6a5902 9371
3e4093b6 9372 /* Skip any nameless bit fields. */
3fa8871b 9373 while (constructor_unfilled_fields != NULL_TREE
7c30b12a 9374 && DECL_UNNAMED_BIT_FIELD (constructor_unfilled_fields))
3e4093b6 9375 constructor_unfilled_fields =
910ad8de 9376 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6
RS
9377 }
9378 else if (TREE_CODE (constructor_type) == UNION_TYPE)
3fa8871b 9379 constructor_unfilled_fields = NULL_TREE;
de520661 9380
3e4093b6
RS
9381 /* Now output any pending elements which have become next. */
9382 if (pending)
a1e3b3d9 9383 output_pending_init_elements (0, braced_init_obstack);
3e4093b6 9384}
8b6a5902 9385
5922dcb5
JJ
9386/* For two FIELD_DECLs in the same chain, return -1 if field1
9387 comes before field2, 1 if field1 comes after field2 and
9388 0 if field1 == field2. */
9389
9390static int
9391init_field_decl_cmp (tree field1, tree field2)
9392{
9393 if (field1 == field2)
9394 return 0;
9395
9396 tree bitpos1 = bit_position (field1);
9397 tree bitpos2 = bit_position (field2);
9398 if (tree_int_cst_equal (bitpos1, bitpos2))
9399 {
9400 /* If one of the fields has non-zero bitsize, then that
9401 field must be the last one in a sequence of zero
9402 sized fields, fields after it will have bigger
9403 bit_position. */
9404 if (TREE_TYPE (field1) != error_mark_node
9405 && COMPLETE_TYPE_P (TREE_TYPE (field1))
9406 && integer_nonzerop (TREE_TYPE (field1)))
9407 return 1;
9408 if (TREE_TYPE (field2) != error_mark_node
9409 && COMPLETE_TYPE_P (TREE_TYPE (field2))
9410 && integer_nonzerop (TREE_TYPE (field2)))
9411 return -1;
9412 /* Otherwise, fallback to DECL_CHAIN walk to find out
9413 which field comes earlier. Walk chains of both
9414 fields, so that if field1 and field2 are close to each
9415 other in either order, it is found soon even for large
9416 sequences of zero sized fields. */
9417 tree f1 = field1, f2 = field2;
9418 while (1)
9419 {
9420 f1 = DECL_CHAIN (f1);
9421 f2 = DECL_CHAIN (f2);
9422 if (f1 == NULL_TREE)
9423 {
9424 gcc_assert (f2);
9425 return 1;
9426 }
9427 if (f2 == NULL_TREE)
9428 return -1;
9429 if (f1 == field2)
9430 return -1;
9431 if (f2 == field1)
9432 return 1;
9433 if (!tree_int_cst_equal (bit_position (f1), bitpos1))
9434 return 1;
9435 if (!tree_int_cst_equal (bit_position (f2), bitpos1))
9436 return -1;
9437 }
9438 }
9439 else if (tree_int_cst_lt (bitpos1, bitpos2))
9440 return -1;
9441 else
9442 return 1;
9443}
9444
3e4093b6
RS
9445/* Output any pending elements which have become next.
9446 As we output elements, constructor_unfilled_{fields,index}
9447 advances, which may cause other elements to become next;
9448 if so, they too are output.
8b6a5902 9449
3e4093b6
RS
9450 If ALL is 0, we return when there are
9451 no more pending elements to output now.
665f2503 9452
3e4093b6
RS
9453 If ALL is 1, we output space as necessary so that
9454 we can output all the pending elements. */
3e4093b6 9455static void
a1e3b3d9 9456output_pending_init_elements (int all, struct obstack * braced_init_obstack)
3e4093b6
RS
9457{
9458 struct init_node *elt = constructor_pending_elts;
9459 tree next;
de520661 9460
3e4093b6
RS
9461 retry:
9462
ba228239 9463 /* Look through the whole pending tree.
3e4093b6
RS
9464 If we find an element that should be output now,
9465 output it. Otherwise, set NEXT to the element
9466 that comes first among those still pending. */
9467
3fa8871b 9468 next = NULL_TREE;
3e4093b6
RS
9469 while (elt)
9470 {
9471 if (TREE_CODE (constructor_type) == ARRAY_TYPE)
8b6a5902 9472 {
3e4093b6
RS
9473 if (tree_int_cst_equal (elt->purpose,
9474 constructor_unfilled_index))
34cf811f
MP
9475 output_init_element (input_location, elt->value, elt->origtype,
9476 true, TREE_TYPE (constructor_type),
30af3a2b 9477 constructor_unfilled_index, false, false,
a1e3b3d9 9478 braced_init_obstack);
3e4093b6
RS
9479 else if (tree_int_cst_lt (constructor_unfilled_index,
9480 elt->purpose))
8b6a5902 9481 {
3e4093b6
RS
9482 /* Advance to the next smaller node. */
9483 if (elt->left)
9484 elt = elt->left;
9485 else
9486 {
9487 /* We have reached the smallest node bigger than the
9488 current unfilled index. Fill the space first. */
9489 next = elt->purpose;
9490 break;
9491 }
8b6a5902 9492 }
ce662d4c
JJ
9493 else
9494 {
3e4093b6
RS
9495 /* Advance to the next bigger node. */
9496 if (elt->right)
9497 elt = elt->right;
9498 else
ce662d4c 9499 {
3e4093b6
RS
9500 /* We have reached the biggest node in a subtree. Find
9501 the parent of it, which is the next bigger node. */
9502 while (elt->parent && elt->parent->right == elt)
9503 elt = elt->parent;
9504 elt = elt->parent;
9505 if (elt && tree_int_cst_lt (constructor_unfilled_index,
9506 elt->purpose))
9507 {
9508 next = elt->purpose;
9509 break;
9510 }
ce662d4c
JJ
9511 }
9512 }
8b6a5902 9513 }
3e636daf 9514 else if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6 9515 {
3e4093b6 9516 /* If the current record is complete we are done. */
3fa8871b 9517 if (constructor_unfilled_fields == NULL_TREE)
3e4093b6 9518 break;
de520661 9519
5922dcb5
JJ
9520 int cmp = init_field_decl_cmp (constructor_unfilled_fields,
9521 elt->purpose);
9522 if (cmp == 0)
9523 output_init_element (input_location, elt->value, elt->origtype,
9524 true, TREE_TYPE (elt->purpose),
9525 elt->purpose, false, false,
9526 braced_init_obstack);
9527 else if (cmp < 0)
3e4093b6
RS
9528 {
9529 /* Advance to the next smaller node. */
9530 if (elt->left)
9531 elt = elt->left;
9532 else
9533 {
9534 /* We have reached the smallest node bigger than the
9535 current unfilled field. Fill the space first. */
9536 next = elt->purpose;
9537 break;
9538 }
9539 }
9540 else
9541 {
9542 /* Advance to the next bigger node. */
9543 if (elt->right)
9544 elt = elt->right;
9545 else
9546 {
9547 /* We have reached the biggest node in a subtree. Find
9548 the parent of it, which is the next bigger node. */
9549 while (elt->parent && elt->parent->right == elt)
9550 elt = elt->parent;
9551 elt = elt->parent;
9552 if (elt
5922dcb5
JJ
9553 && init_field_decl_cmp (constructor_unfilled_fields,
9554 elt->purpose) < 0)
3e4093b6
RS
9555 {
9556 next = elt->purpose;
9557 break;
9558 }
9559 }
9560 }
9561 }
9562 }
de520661 9563
3e4093b6
RS
9564 /* Ordinarily return, but not if we want to output all
9565 and there are elements left. */
3fa8871b 9566 if (!(all && next != NULL_TREE))
e5cfb88f
RK
9567 return;
9568
3e4093b6
RS
9569 /* If it's not incremental, just skip over the gap, so that after
9570 jumping to retry we will output the next successive element. */
3e636daf 9571 if (RECORD_OR_UNION_TYPE_P (constructor_type))
3e4093b6
RS
9572 constructor_unfilled_fields = next;
9573 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9574 constructor_unfilled_index = next;
de520661 9575
3e4093b6
RS
9576 /* ELT now points to the node in the pending tree with the next
9577 initializer to output. */
9578 goto retry;
de520661
RS
9579}
9580\f
3e4093b6
RS
9581/* Add one non-braced element to the current constructor level.
9582 This adjusts the current position within the constructor's type.
9583 This may also start or terminate implicit levels
9584 to handle a partly-braced initializer.
e5e809f4 9585
3e4093b6 9586 Once this has found the correct level for the new element,
b295aee2
JJ
9587 it calls output_init_element.
9588
9589 IMPLICIT is true if value comes from pop_init_level (1),
9590 the new initializer has been merged with the existing one
9591 and thus no warnings should be emitted about overriding an
9592 existing initializer. */
3e4093b6
RS
9593
9594void
34cf811f 9595process_init_element (location_t loc, struct c_expr value, bool implicit,
a1e3b3d9 9596 struct obstack * braced_init_obstack)
e5e809f4 9597{
916c5919 9598 tree orig_value = value.value;
3fa8871b
MP
9599 int string_flag
9600 = (orig_value != NULL_TREE && TREE_CODE (orig_value) == STRING_CST);
916c5919 9601 bool strict_string = value.original_code == STRING_CST;
340baef7 9602 bool was_designated = designator_depth != 0;
e5e809f4 9603
3e4093b6 9604 designator_depth = 0;
b06df647 9605 designator_erroneous = 0;
e5e809f4 9606
9bac5cbb
G
9607 if (!implicit && value.value && !integer_zerop (value.value))
9608 constructor_zeroinit = 0;
9609
3e4093b6
RS
9610 /* Handle superfluous braces around string cst as in
9611 char x[] = {"foo"}; */
9612 if (string_flag
9613 && constructor_type
340baef7 9614 && !was_designated
3e4093b6 9615 && TREE_CODE (constructor_type) == ARRAY_TYPE
197463ae 9616 && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
3e4093b6 9617 && integer_zerop (constructor_unfilled_index))
e5e809f4 9618 {
916c5919 9619 if (constructor_stack->replacement_value.value)
ea58ef42 9620 error_init (loc, "excess elements in char array initializer");
3e4093b6
RS
9621 constructor_stack->replacement_value = value;
9622 return;
e5e809f4 9623 }
8b6a5902 9624
3fa8871b 9625 if (constructor_stack->replacement_value.value != NULL_TREE)
3e4093b6 9626 {
ea58ef42 9627 error_init (loc, "excess elements in struct initializer");
3e4093b6 9628 return;
e5e809f4
JL
9629 }
9630
3e4093b6
RS
9631 /* Ignore elements of a brace group if it is entirely superfluous
9632 and has already been diagnosed. */
3fa8871b 9633 if (constructor_type == NULL_TREE)
3e4093b6 9634 return;
e5e809f4 9635
976d5a22
TT
9636 if (!implicit && warn_designated_init && !was_designated
9637 && TREE_CODE (constructor_type) == RECORD_TYPE
9638 && lookup_attribute ("designated_init",
9639 TYPE_ATTRIBUTES (constructor_type)))
9640 warning_init (loc,
9641 OPT_Wdesignated_init,
9642 "positional initialization of field "
9643 "in %<struct%> declared with %<designated_init%> attribute");
9644
3e4093b6
RS
9645 /* If we've exhausted any levels that didn't have braces,
9646 pop them now. */
9647 while (constructor_stack->implicit)
9648 {
3e636daf 9649 if (RECORD_OR_UNION_TYPE_P (constructor_type)
3fa8871b 9650 && constructor_fields == NULL_TREE)
ea58ef42 9651 process_init_element (loc,
5dd9a9d0
DM
9652 pop_init_level (loc, 1, braced_init_obstack,
9653 last_init_list_comma),
a1e3b3d9 9654 true, braced_init_obstack);
53650abe 9655 else if ((TREE_CODE (constructor_type) == ARRAY_TYPE
31521951 9656 || VECTOR_TYPE_P (constructor_type))
8824edff
JJ
9657 && constructor_max_index
9658 && tree_int_cst_lt (constructor_max_index,
9659 constructor_index))
ea58ef42 9660 process_init_element (loc,
5dd9a9d0
DM
9661 pop_init_level (loc, 1, braced_init_obstack,
9662 last_init_list_comma),
a1e3b3d9 9663 true, braced_init_obstack);
3e4093b6
RS
9664 else
9665 break;
9666 }
e5e809f4 9667
3e4093b6
RS
9668 /* In the case of [LO ... HI] = VALUE, only evaluate VALUE once. */
9669 if (constructor_range_stack)
e5e809f4 9670 {
3e4093b6
RS
9671 /* If value is a compound literal and we'll be just using its
9672 content, don't put it into a SAVE_EXPR. */
916c5919 9673 if (TREE_CODE (value.value) != COMPOUND_LITERAL_EXPR
c3e38a03 9674 || !require_constant_value)
8ce94e44
JM
9675 {
9676 tree semantic_type = NULL_TREE;
9677 if (TREE_CODE (value.value) == EXCESS_PRECISION_EXPR)
9678 {
9679 semantic_type = TREE_TYPE (value.value);
9680 value.value = TREE_OPERAND (value.value, 0);
9681 }
b2fa0a8b 9682 value.value = save_expr (value.value);
8ce94e44
JM
9683 if (semantic_type)
9684 value.value = build1 (EXCESS_PRECISION_EXPR, semantic_type,
9685 value.value);
9686 }
3e4093b6 9687 }
e5e809f4 9688
3e4093b6
RS
9689 while (1)
9690 {
9691 if (TREE_CODE (constructor_type) == RECORD_TYPE)
e5e809f4 9692 {
3e4093b6
RS
9693 tree fieldtype;
9694 enum tree_code fieldcode;
e5e809f4 9695
3fa8871b 9696 if (constructor_fields == NULL_TREE)
3e4093b6 9697 {
ea58ef42 9698 pedwarn_init (loc, 0, "excess elements in struct initializer");
3e4093b6
RS
9699 break;
9700 }
e5e809f4 9701
3e4093b6
RS
9702 fieldtype = TREE_TYPE (constructor_fields);
9703 if (fieldtype != error_mark_node)
9704 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9705 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9706
3e4093b6
RS
9707 /* Error for non-static initialization of a flexible array member. */
9708 if (fieldcode == ARRAY_TYPE
9709 && !require_constant_value
9710 && TYPE_SIZE (fieldtype) == NULL_TREE
f7587ed0 9711 && DECL_CHAIN (constructor_fields) == NULL_TREE)
3e4093b6 9712 {
ea58ef42
MP
9713 error_init (loc, "non-static initialization of a flexible "
9714 "array member");
3e4093b6
RS
9715 break;
9716 }
e5e809f4 9717
2cc901dc
MP
9718 /* Error for initialization of a flexible array member with
9719 a string constant if the structure is in an array. E.g.:
9720 struct S { int x; char y[]; };
9721 struct S s[] = { { 1, "foo" } };
9722 is invalid. */
9723 if (string_flag
9724 && fieldcode == ARRAY_TYPE
9725 && constructor_depth > 1
9726 && TYPE_SIZE (fieldtype) == NULL_TREE
9727 && DECL_CHAIN (constructor_fields) == NULL_TREE)
9728 {
9729 bool in_array_p = false;
9730 for (struct constructor_stack *p = constructor_stack;
9731 p && p->type; p = p->next)
9732 if (TREE_CODE (p->type) == ARRAY_TYPE)
9733 {
9734 in_array_p = true;
9735 break;
9736 }
9737 if (in_array_p)
9738 {
9739 error_init (loc, "initialization of flexible array "
9740 "member in a nested context");
9741 break;
9742 }
9743 }
9744
3e4093b6 9745 /* Accept a string constant to initialize a subarray. */
3fa8871b 9746 if (value.value != NULL_TREE
3e4093b6 9747 && fieldcode == ARRAY_TYPE
197463ae 9748 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9749 && string_flag)
916c5919 9750 value.value = orig_value;
3e4093b6
RS
9751 /* Otherwise, if we have come to a subaggregate,
9752 and we don't have an element of its type, push into it. */
3fa8871b 9753 else if (value.value != NULL_TREE
916c5919
JM
9754 && value.value != error_mark_node
9755 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9756 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9757 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9758 {
ea58ef42 9759 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9760 continue;
9761 }
e5e809f4 9762
916c5919 9763 if (value.value)
3e4093b6
RS
9764 {
9765 push_member_name (constructor_fields);
34cf811f 9766 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9767 strict_string, fieldtype,
30af3a2b 9768 constructor_fields, true, implicit,
a1e3b3d9 9769 braced_init_obstack);
3e4093b6 9770 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9771 }
9772 else
3e4093b6
RS
9773 /* Do the bookkeeping for an element that was
9774 directly output as a constructor. */
e5e809f4 9775 {
3e4093b6
RS
9776 /* For a record, keep track of end position of last field. */
9777 if (DECL_SIZE (constructor_fields))
c22cacf3 9778 constructor_bit_index
db3927fb
AH
9779 = size_binop_loc (input_location, PLUS_EXPR,
9780 bit_position (constructor_fields),
9781 DECL_SIZE (constructor_fields));
3e4093b6
RS
9782
9783 /* If the current field was the first one not yet written out,
9784 it isn't now, so update. */
9785 if (constructor_unfilled_fields == constructor_fields)
9786 {
910ad8de 9787 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
3e4093b6
RS
9788 /* Skip any nameless bit fields. */
9789 while (constructor_unfilled_fields != 0
7c30b12a
PC
9790 && (DECL_UNNAMED_BIT_FIELD
9791 (constructor_unfilled_fields)))
3e4093b6 9792 constructor_unfilled_fields =
910ad8de 9793 DECL_CHAIN (constructor_unfilled_fields);
3e4093b6 9794 }
e5e809f4 9795 }
3e4093b6 9796
910ad8de 9797 constructor_fields = DECL_CHAIN (constructor_fields);
3e4093b6 9798 /* Skip any nameless bit fields at the beginning. */
3fa8871b 9799 while (constructor_fields != NULL_TREE
7c30b12a 9800 && DECL_UNNAMED_BIT_FIELD (constructor_fields))
910ad8de 9801 constructor_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9802 }
3e4093b6 9803 else if (TREE_CODE (constructor_type) == UNION_TYPE)
e5e809f4 9804 {
3e4093b6
RS
9805 tree fieldtype;
9806 enum tree_code fieldcode;
e5e809f4 9807
3fa8871b 9808 if (constructor_fields == NULL_TREE)
3e4093b6 9809 {
d033409e 9810 pedwarn_init (loc, 0,
509c9d60 9811 "excess elements in union initializer");
3e4093b6
RS
9812 break;
9813 }
e5e809f4 9814
3e4093b6
RS
9815 fieldtype = TREE_TYPE (constructor_fields);
9816 if (fieldtype != error_mark_node)
9817 fieldtype = TYPE_MAIN_VARIANT (fieldtype);
9818 fieldcode = TREE_CODE (fieldtype);
e5e809f4 9819
3e4093b6
RS
9820 /* Warn that traditional C rejects initialization of unions.
9821 We skip the warning if the value is zero. This is done
9822 under the assumption that the zero initializer in user
9823 code appears conditioned on e.g. __STDC__ to avoid
9824 "missing initializer" warnings and relies on default
9825 initialization to zero in the traditional C case.
9826 We also skip the warning if the initializer is designated,
9827 again on the assumption that this must be conditional on
9828 __STDC__ anyway (and we've already complained about the
9829 member-designator already). */
8400e75e 9830 if (!in_system_header_at (input_location) && !constructor_designated
916c5919
JM
9831 && !(value.value && (integer_zerop (value.value)
9832 || real_zerop (value.value))))
3176a0c2
DD
9833 warning (OPT_Wtraditional, "traditional C rejects initialization "
9834 "of unions");
e5e809f4 9835
3e4093b6 9836 /* Accept a string constant to initialize a subarray. */
3fa8871b 9837 if (value.value != NULL_TREE
3e4093b6 9838 && fieldcode == ARRAY_TYPE
197463ae 9839 && INTEGRAL_TYPE_P (TREE_TYPE (fieldtype))
3e4093b6 9840 && string_flag)
916c5919 9841 value.value = orig_value;
3e4093b6
RS
9842 /* Otherwise, if we have come to a subaggregate,
9843 and we don't have an element of its type, push into it. */
3fa8871b 9844 else if (value.value != NULL_TREE
916c5919
JM
9845 && value.value != error_mark_node
9846 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != fieldtype
3e4093b6 9847 && (fieldcode == RECORD_TYPE || fieldcode == ARRAY_TYPE
53650abe 9848 || fieldcode == UNION_TYPE || fieldcode == VECTOR_TYPE))
3e4093b6 9849 {
ea58ef42 9850 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9851 continue;
9852 }
e5e809f4 9853
916c5919 9854 if (value.value)
3e4093b6
RS
9855 {
9856 push_member_name (constructor_fields);
34cf811f 9857 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9858 strict_string, fieldtype,
30af3a2b 9859 constructor_fields, true, implicit,
a1e3b3d9 9860 braced_init_obstack);
3e4093b6 9861 RESTORE_SPELLING_DEPTH (constructor_depth);
e5e809f4
JL
9862 }
9863 else
3e4093b6
RS
9864 /* Do the bookkeeping for an element that was
9865 directly output as a constructor. */
e5e809f4 9866 {
3e4093b6 9867 constructor_bit_index = DECL_SIZE (constructor_fields);
f7587ed0 9868 constructor_unfilled_fields = DECL_CHAIN (constructor_fields);
e5e809f4 9869 }
e5e809f4 9870
3fa8871b 9871 constructor_fields = NULL_TREE;
3e4093b6
RS
9872 }
9873 else if (TREE_CODE (constructor_type) == ARRAY_TYPE)
9874 {
9875 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
9876 enum tree_code eltcode = TREE_CODE (elttype);
e5e809f4 9877
3e4093b6 9878 /* Accept a string constant to initialize a subarray. */
3fa8871b 9879 if (value.value != NULL_TREE
3e4093b6 9880 && eltcode == ARRAY_TYPE
197463ae 9881 && INTEGRAL_TYPE_P (TREE_TYPE (elttype))
3e4093b6 9882 && string_flag)
916c5919 9883 value.value = orig_value;
3e4093b6
RS
9884 /* Otherwise, if we have come to a subaggregate,
9885 and we don't have an element of its type, push into it. */
3fa8871b 9886 else if (value.value != NULL_TREE
916c5919
JM
9887 && value.value != error_mark_node
9888 && TYPE_MAIN_VARIANT (TREE_TYPE (value.value)) != elttype
3e4093b6 9889 && (eltcode == RECORD_TYPE || eltcode == ARRAY_TYPE
53650abe 9890 || eltcode == UNION_TYPE || eltcode == VECTOR_TYPE))
3e4093b6 9891 {
ea58ef42 9892 push_init_level (loc, 1, braced_init_obstack);
3e4093b6
RS
9893 continue;
9894 }
8b6a5902 9895
3fa8871b 9896 if (constructor_max_index != NULL_TREE
3e4093b6
RS
9897 && (tree_int_cst_lt (constructor_max_index, constructor_index)
9898 || integer_all_onesp (constructor_max_index)))
9899 {
d033409e 9900 pedwarn_init (loc, 0,
509c9d60 9901 "excess elements in array initializer");
3e4093b6
RS
9902 break;
9903 }
8b6a5902 9904
3e4093b6 9905 /* Now output the actual element. */
916c5919 9906 if (value.value)
3e4093b6 9907 {
ae7e9ddd 9908 push_array_bounds (tree_to_uhwi (constructor_index));
34cf811f 9909 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9910 strict_string, elttype,
30af3a2b 9911 constructor_index, true, implicit,
a1e3b3d9 9912 braced_init_obstack);
3e4093b6
RS
9913 RESTORE_SPELLING_DEPTH (constructor_depth);
9914 }
2f6e4e97 9915
3e4093b6 9916 constructor_index
db3927fb
AH
9917 = size_binop_loc (input_location, PLUS_EXPR,
9918 constructor_index, bitsize_one_node);
8b6a5902 9919
916c5919 9920 if (!value.value)
3e4093b6
RS
9921 /* If we are doing the bookkeeping for an element that was
9922 directly output as a constructor, we must update
9923 constructor_unfilled_index. */
9924 constructor_unfilled_index = constructor_index;
9925 }
31521951 9926 else if (VECTOR_TYPE_P (constructor_type))
3e4093b6
RS
9927 {
9928 tree elttype = TYPE_MAIN_VARIANT (TREE_TYPE (constructor_type));
8b6a5902 9929
c22cacf3
MS
9930 /* Do a basic check of initializer size. Note that vectors
9931 always have a fixed size derived from their type. */
3e4093b6
RS
9932 if (tree_int_cst_lt (constructor_max_index, constructor_index))
9933 {
d033409e 9934 pedwarn_init (loc, 0,
509c9d60 9935 "excess elements in vector initializer");
3e4093b6
RS
9936 break;
9937 }
8b6a5902 9938
3e4093b6 9939 /* Now output the actual element. */
916c5919 9940 if (value.value)
53650abe
AP
9941 {
9942 if (TREE_CODE (value.value) == VECTOR_CST)
9943 elttype = TYPE_MAIN_VARIANT (constructor_type);
34cf811f 9944 output_init_element (loc, value.value, value.original_type,
53650abe 9945 strict_string, elttype,
30af3a2b 9946 constructor_index, true, implicit,
a1e3b3d9 9947 braced_init_obstack);
53650abe 9948 }
8b6a5902 9949
3e4093b6 9950 constructor_index
db3927fb
AH
9951 = size_binop_loc (input_location,
9952 PLUS_EXPR, constructor_index, bitsize_one_node);
8b6a5902 9953
916c5919 9954 if (!value.value)
3e4093b6
RS
9955 /* If we are doing the bookkeeping for an element that was
9956 directly output as a constructor, we must update
9957 constructor_unfilled_index. */
9958 constructor_unfilled_index = constructor_index;
9959 }
8b6a5902 9960
3e4093b6
RS
9961 /* Handle the sole element allowed in a braced initializer
9962 for a scalar variable. */
b4519d39 9963 else if (constructor_type != error_mark_node
3fa8871b 9964 && constructor_fields == NULL_TREE)
8b6a5902 9965 {
d033409e 9966 pedwarn_init (loc, 0,
509c9d60 9967 "excess elements in scalar initializer");
3e4093b6 9968 break;
8b6a5902
JJ
9969 }
9970 else
9971 {
916c5919 9972 if (value.value)
34cf811f 9973 output_init_element (loc, value.value, value.original_type,
bbbbb16a 9974 strict_string, constructor_type,
30af3a2b 9975 NULL_TREE, true, implicit,
a1e3b3d9 9976 braced_init_obstack);
3fa8871b 9977 constructor_fields = NULL_TREE;
8b6a5902
JJ
9978 }
9979
3e4093b6
RS
9980 /* Handle range initializers either at this level or anywhere higher
9981 in the designator stack. */
9982 if (constructor_range_stack)
8b6a5902 9983 {
3e4093b6
RS
9984 struct constructor_range_stack *p, *range_stack;
9985 int finish = 0;
9986
9987 range_stack = constructor_range_stack;
9988 constructor_range_stack = 0;
9989 while (constructor_stack != range_stack->stack)
8b6a5902 9990 {
366de0ce 9991 gcc_assert (constructor_stack->implicit);
34cf811f 9992 process_init_element (loc,
ea58ef42 9993 pop_init_level (loc, 1,
5dd9a9d0
DM
9994 braced_init_obstack,
9995 last_init_list_comma),
a1e3b3d9 9996 true, braced_init_obstack);
8b6a5902 9997 }
3e4093b6
RS
9998 for (p = range_stack;
9999 !p->range_end || tree_int_cst_equal (p->index, p->range_end);
10000 p = p->prev)
8b6a5902 10001 {
366de0ce 10002 gcc_assert (constructor_stack->implicit);
34cf811f 10003 process_init_element (loc,
ea58ef42 10004 pop_init_level (loc, 1,
5dd9a9d0
DM
10005 braced_init_obstack,
10006 last_init_list_comma),
a1e3b3d9 10007 true, braced_init_obstack);
8b6a5902 10008 }
3e4093b6 10009
db3927fb
AH
10010 p->index = size_binop_loc (input_location,
10011 PLUS_EXPR, p->index, bitsize_one_node);
3e4093b6
RS
10012 if (tree_int_cst_equal (p->index, p->range_end) && !p->prev)
10013 finish = 1;
10014
10015 while (1)
10016 {
10017 constructor_index = p->index;
10018 constructor_fields = p->fields;
10019 if (finish && p->range_end && p->index == p->range_start)
10020 {
10021 finish = 0;
10022 p->prev = 0;
10023 }
10024 p = p->next;
10025 if (!p)
10026 break;
16595a1f 10027 finish_implicit_inits (loc, braced_init_obstack);
ea58ef42 10028 push_init_level (loc, 2, braced_init_obstack);
3e4093b6
RS
10029 p->stack = constructor_stack;
10030 if (p->range_end && tree_int_cst_equal (p->index, p->range_end))
10031 p->index = p->range_start;
10032 }
10033
10034 if (!finish)
10035 constructor_range_stack = range_stack;
10036 continue;
8b6a5902
JJ
10037 }
10038
3e4093b6 10039 break;
8b6a5902
JJ
10040 }
10041
3e4093b6
RS
10042 constructor_range_stack = 0;
10043}
10044\f
9f0e2d86
ZW
10045/* Build a complete asm-statement, whose components are a CV_QUALIFIER
10046 (guaranteed to be 'volatile' or null) and ARGS (represented using
e130a54b 10047 an ASM_EXPR node). */
3e4093b6 10048tree
9f0e2d86 10049build_asm_stmt (tree cv_qualifier, tree args)
3e4093b6 10050{
6de9cd9a
DN
10051 if (!ASM_VOLATILE_P (args) && cv_qualifier)
10052 ASM_VOLATILE_P (args) = 1;
9f0e2d86 10053 return add_stmt (args);
8b6a5902
JJ
10054}
10055
9f0e2d86
ZW
10056/* Build an asm-expr, whose components are a STRING, some OUTPUTS,
10057 some INPUTS, and some CLOBBERS. The latter three may be NULL.
10058 SIMPLE indicates whether there was anything at all after the
10059 string in the asm expression -- asm("blah") and asm("blah" : )
e130a54b 10060 are subtly different. We use a ASM_EXPR node to represent this. */
3e4093b6 10061tree
c2255bc4 10062build_asm_expr (location_t loc, tree string, tree outputs, tree inputs,
1c384bf1 10063 tree clobbers, tree labels, bool simple)
e5e809f4 10064{
3e4093b6 10065 tree tail;
9f0e2d86 10066 tree args;
6de9cd9a
DN
10067 int i;
10068 const char *constraint;
74f0c611 10069 const char **oconstraints;
6de9cd9a 10070 bool allows_mem, allows_reg, is_inout;
74f0c611 10071 int ninputs, noutputs;
6de9cd9a
DN
10072
10073 ninputs = list_length (inputs);
10074 noutputs = list_length (outputs);
74f0c611
RH
10075 oconstraints = (const char **) alloca (noutputs * sizeof (const char *));
10076
1c384bf1 10077 string = resolve_asm_operand_names (string, outputs, inputs, labels);
3e4093b6 10078
6de9cd9a
DN
10079 /* Remove output conversions that change the type but not the mode. */
10080 for (i = 0, tail = outputs; tail; ++i, tail = TREE_CHAIN (tail))
e5e809f4 10081 {
3e4093b6 10082 tree output = TREE_VALUE (tail);
74f0c611 10083
f9c59f7e 10084 output = c_fully_fold (output, false, NULL, true);
eadd3d0d 10085
74f0c611
RH
10086 /* ??? Really, this should not be here. Users should be using a
10087 proper lvalue, dammit. But there's a long history of using casts
10088 in the output operands. In cases like longlong.h, this becomes a
10089 primitive form of typechecking -- if the cast can be removed, then
10090 the output operand had a type of the proper width; otherwise we'll
10091 get an error. Gross, but ... */
3e4093b6 10092 STRIP_NOPS (output);
74f0c611 10093
7bd11157 10094 if (!lvalue_or_else (loc, output, lv_asm))
74f0c611 10095 output = error_mark_node;
8b6a5902 10096
5544530a
PB
10097 if (output != error_mark_node
10098 && (TREE_READONLY (output)
10099 || TYPE_READONLY (TREE_TYPE (output))
3e636daf 10100 || (RECORD_OR_UNION_TYPE_P (TREE_TYPE (output))
5544530a 10101 && C_TYPE_FIELDS_READONLY (TREE_TYPE (output)))))
c02065fc 10102 readonly_error (loc, output, lv_asm);
5544530a 10103
6de9cd9a 10104 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
74f0c611
RH
10105 oconstraints[i] = constraint;
10106
10107 if (parse_output_constraint (&constraint, i, ninputs, noutputs,
10108 &allows_mem, &allows_reg, &is_inout))
10109 {
10110 /* If the operand is going to end up in memory,
10111 mark it addressable. */
10112 if (!allows_reg && !c_mark_addressable (output))
10113 output = error_mark_node;
bae5cddf
JJ
10114 if (!(!allows_reg && allows_mem)
10115 && output != error_mark_node
10116 && VOID_TYPE_P (TREE_TYPE (output)))
10117 {
10118 error_at (loc, "invalid use of void expression");
10119 output = error_mark_node;
10120 }
74f0c611
RH
10121 }
10122 else
c22cacf3 10123 output = error_mark_node;
3e4093b6 10124
74f0c611 10125 TREE_VALUE (tail) = output;
8b6a5902 10126 }
3e4093b6 10127
74f0c611
RH
10128 for (i = 0, tail = inputs; tail; ++i, tail = TREE_CHAIN (tail))
10129 {
10130 tree input;
10131
10132 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
10133 input = TREE_VALUE (tail);
10134
74f0c611
RH
10135 if (parse_input_constraint (&constraint, i, ninputs, noutputs, 0,
10136 oconstraints, &allows_mem, &allows_reg))
10137 {
10138 /* If the operand is going to end up in memory,
10139 mark it addressable. */
b4c33883
AP
10140 if (!allows_reg && allows_mem)
10141 {
f9c59f7e 10142 input = c_fully_fold (input, false, NULL, true);
eadd3d0d 10143
b4c33883
AP
10144 /* Strip the nops as we allow this case. FIXME, this really
10145 should be rejected or made deprecated. */
10146 STRIP_NOPS (input);
10147 if (!c_mark_addressable (input))
10148 input = error_mark_node;
bae5cddf 10149 }
eadd3d0d 10150 else
bae5cddf 10151 {
eadd3d0d
JJ
10152 struct c_expr expr;
10153 memset (&expr, 0, sizeof (expr));
10154 expr.value = input;
267bac10 10155 expr = convert_lvalue_to_rvalue (loc, expr, true, false);
eadd3d0d
JJ
10156 input = c_fully_fold (expr.value, false, NULL);
10157
10158 if (input != error_mark_node && VOID_TYPE_P (TREE_TYPE (input)))
10159 {
10160 error_at (loc, "invalid use of void expression");
10161 input = error_mark_node;
10162 }
bae5cddf 10163 }
74f0c611
RH
10164 }
10165 else
10166 input = error_mark_node;
10167
10168 TREE_VALUE (tail) = input;
10169 }
3e4093b6 10170
1c384bf1
RH
10171 /* ASMs with labels cannot have outputs. This should have been
10172 enforced by the parser. */
10173 gcc_assert (outputs == NULL || labels == NULL);
10174
10175 args = build_stmt (loc, ASM_EXPR, string, outputs, inputs, clobbers, labels);
9f0e2d86 10176
5544530a
PB
10177 /* asm statements without outputs, including simple ones, are treated
10178 as volatile. */
10179 ASM_INPUT_P (args) = simple;
10180 ASM_VOLATILE_P (args) = (noutputs == 0);
74f0c611 10181
9f0e2d86 10182 return args;
e5e809f4 10183}
3e4093b6 10184\f
c2255bc4
AH
10185/* Generate a goto statement to LABEL. LOC is the location of the
10186 GOTO. */
506e2710
RH
10187
10188tree
c2255bc4 10189c_finish_goto_label (location_t loc, tree label)
506e2710 10190{
e1b7793c 10191 tree decl = lookup_label_for_goto (loc, label);
506e2710
RH
10192 if (!decl)
10193 return NULL_TREE;
506e2710 10194 TREE_USED (decl) = 1;
c2255bc4 10195 {
7fef86d3 10196 add_stmt (build_predict_expr (PRED_GOTO, NOT_TAKEN));
c2255bc4
AH
10197 tree t = build1 (GOTO_EXPR, void_type_node, decl);
10198 SET_EXPR_LOCATION (t, loc);
10199 return add_stmt (t);
10200 }
506e2710
RH
10201}
10202
c2255bc4
AH
10203/* Generate a computed goto statement to EXPR. LOC is the location of
10204 the GOTO. */
506e2710
RH
10205
10206tree
c2255bc4 10207c_finish_goto_ptr (location_t loc, tree expr)
506e2710 10208{
c2255bc4 10209 tree t;
c1771a20 10210 pedwarn (loc, OPT_Wpedantic, "ISO C forbids %<goto *expr;%>");
928c19bb 10211 expr = c_fully_fold (expr, false, NULL);
506e2710 10212 expr = convert (ptr_type_node, expr);
c2255bc4
AH
10213 t = build1 (GOTO_EXPR, void_type_node, expr);
10214 SET_EXPR_LOCATION (t, loc);
10215 return add_stmt (t);
506e2710
RH
10216}
10217
5088b058 10218/* Generate a C `return' statement. RETVAL is the expression for what
c2255bc4 10219 to return, or a null pointer for `return;' with no value. LOC is
6e07c515
MP
10220 the location of the return statement, or the location of the expression,
10221 if the statement has any. If ORIGTYPE is not NULL_TREE, it
c2255bc4 10222 is the original type of RETVAL. */
de520661 10223
506e2710 10224tree
c2255bc4 10225c_finish_return (location_t loc, tree retval, tree origtype)
3e4093b6 10226{
0c9b182b
JJ
10227 tree valtype = TREE_TYPE (TREE_TYPE (current_function_decl)), ret_stmt;
10228 bool no_warning = false;
928c19bb 10229 bool npc = false;
3e4093b6 10230
de8ddd5f
MP
10231 /* Use the expansion point to handle cases such as returning NULL
10232 in a function returning void. */
10233 source_location xloc = expansion_point_location_if_in_system_header (loc);
10234
3e4093b6 10235 if (TREE_THIS_VOLATILE (current_function_decl))
de8ddd5f 10236 warning_at (xloc, 0,
c2255bc4 10237 "function declared %<noreturn%> has a %<return%> statement");
3e4093b6 10238
928c19bb
JM
10239 if (retval)
10240 {
8ce94e44 10241 tree semantic_type = NULL_TREE;
928c19bb 10242 npc = null_pointer_constant_p (retval);
8ce94e44
JM
10243 if (TREE_CODE (retval) == EXCESS_PRECISION_EXPR)
10244 {
10245 semantic_type = TREE_TYPE (retval);
10246 retval = TREE_OPERAND (retval, 0);
10247 }
928c19bb 10248 retval = c_fully_fold (retval, false, NULL);
8ce94e44
JM
10249 if (semantic_type)
10250 retval = build1 (EXCESS_PRECISION_EXPR, semantic_type, retval);
928c19bb
JM
10251 }
10252
3e4093b6 10253 if (!retval)
de520661 10254 {
3e4093b6 10255 current_function_returns_null = 1;
b67b9225 10256 if ((warn_return_type >= 0 || flag_isoc99)
3fa8871b 10257 && valtype != NULL_TREE && TREE_CODE (valtype) != VOID_TYPE)
0c9b182b 10258 {
94c40e19 10259 bool warned_here;
35aff4fb 10260 if (flag_isoc99)
94c40e19 10261 warned_here = pedwarn
b67b9225 10262 (loc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
94c40e19 10263 "%<return%> with no value, in function returning non-void");
35aff4fb 10264 else
94c40e19
DM
10265 warned_here = warning_at
10266 (loc, OPT_Wreturn_type,
10267 "%<return%> with no value, in function returning non-void");
0c9b182b 10268 no_warning = true;
94c40e19
DM
10269 if (warned_here)
10270 inform (DECL_SOURCE_LOCATION (current_function_decl),
10271 "declared here");
0c9b182b 10272 }
400fbf9f 10273 }
3fa8871b 10274 else if (valtype == NULL_TREE || TREE_CODE (valtype) == VOID_TYPE)
de520661 10275 {
3e4093b6 10276 current_function_returns_null = 1;
94c40e19 10277 bool warned_here;
2397c575 10278 if (TREE_CODE (TREE_TYPE (retval)) != VOID_TYPE)
94c40e19 10279 warned_here = pedwarn
b67b9225 10280 (xloc, warn_return_type >= 0 ? OPT_Wreturn_type : 0,
94c40e19 10281 "%<return%> with a value, in function returning void");
b8698a0f 10282 else
94c40e19
DM
10283 warned_here = pedwarn
10284 (xloc, OPT_Wpedantic, "ISO C forbids "
10285 "%<return%> with expression, in function returning void");
10286 if (warned_here)
10287 inform (DECL_SOURCE_LOCATION (current_function_decl),
10288 "declared here");
de520661 10289 }
3e4093b6 10290 else
de520661 10291 {
68fca595
MP
10292 tree t = convert_for_assignment (loc, UNKNOWN_LOCATION, valtype,
10293 retval, origtype, ic_return,
c2255bc4 10294 npc, NULL_TREE, NULL_TREE, 0);
3e4093b6
RS
10295 tree res = DECL_RESULT (current_function_decl);
10296 tree inner;
9feb29df 10297 bool save;
3e4093b6
RS
10298
10299 current_function_returns_value = 1;
10300 if (t == error_mark_node)
506e2710 10301 return NULL_TREE;
3e4093b6 10302
9feb29df
JJ
10303 save = in_late_binary_op;
10304 if (TREE_CODE (TREE_TYPE (res)) == BOOLEAN_TYPE
e5341100
JJ
10305 || TREE_CODE (TREE_TYPE (res)) == COMPLEX_TYPE
10306 || (TREE_CODE (TREE_TYPE (t)) == REAL_TYPE
10307 && (TREE_CODE (TREE_TYPE (res)) == INTEGER_TYPE
10308 || TREE_CODE (TREE_TYPE (res)) == ENUMERAL_TYPE)
45b2222a 10309 && sanitize_flags_p (SANITIZE_FLOAT_CAST)))
9feb29df 10310 in_late_binary_op = true;
3e4093b6 10311 inner = t = convert (TREE_TYPE (res), t);
9feb29df 10312 in_late_binary_op = save;
3e4093b6
RS
10313
10314 /* Strip any conversions, additions, and subtractions, and see if
10315 we are returning the address of a local variable. Warn if so. */
10316 while (1)
8b6a5902 10317 {
3e4093b6 10318 switch (TREE_CODE (inner))
8b6a5902 10319 {
849421a3
JJ
10320 CASE_CONVERT:
10321 case NON_LVALUE_EXPR:
3e4093b6 10322 case PLUS_EXPR:
849421a3 10323 case POINTER_PLUS_EXPR:
3e4093b6
RS
10324 inner = TREE_OPERAND (inner, 0);
10325 continue;
10326
10327 case MINUS_EXPR:
10328 /* If the second operand of the MINUS_EXPR has a pointer
10329 type (or is converted from it), this may be valid, so
10330 don't give a warning. */
10331 {
10332 tree op1 = TREE_OPERAND (inner, 1);
8b6a5902 10333
3f75a254 10334 while (!POINTER_TYPE_P (TREE_TYPE (op1))
1043771b
TB
10335 && (CONVERT_EXPR_P (op1)
10336 || TREE_CODE (op1) == NON_LVALUE_EXPR))
3e4093b6 10337 op1 = TREE_OPERAND (op1, 0);
8b6a5902 10338
3e4093b6
RS
10339 if (POINTER_TYPE_P (TREE_TYPE (op1)))
10340 break;
8b6a5902 10341
3e4093b6
RS
10342 inner = TREE_OPERAND (inner, 0);
10343 continue;
10344 }
400fbf9f 10345
3e4093b6
RS
10346 case ADDR_EXPR:
10347 inner = TREE_OPERAND (inner, 0);
c2f4acb7 10348
6615c446 10349 while (REFERENCE_CLASS_P (inner)
22d03525 10350 && !INDIRECT_REF_P (inner))
3e4093b6 10351 inner = TREE_OPERAND (inner, 0);
8b6a5902 10352
a2f1f4c3 10353 if (DECL_P (inner)
3f75a254
JM
10354 && !DECL_EXTERNAL (inner)
10355 && !TREE_STATIC (inner)
3e4093b6 10356 && DECL_CONTEXT (inner) == current_function_decl)
19fc9faa
MP
10357 {
10358 if (TREE_CODE (inner) == LABEL_DECL)
10359 warning_at (loc, OPT_Wreturn_local_addr,
10360 "function returns address of label");
10361 else
b4dfdc11
MG
10362 {
10363 warning_at (loc, OPT_Wreturn_local_addr,
10364 "function returns address of local variable");
10365 tree zero = build_zero_cst (TREE_TYPE (res));
10366 t = build2 (COMPOUND_EXPR, TREE_TYPE (res), t, zero);
10367 }
19fc9faa 10368 }
3e4093b6 10369 break;
8b6a5902 10370
3e4093b6
RS
10371 default:
10372 break;
10373 }
de520661 10374
3e4093b6
RS
10375 break;
10376 }
10377
53fb4de3 10378 retval = build2 (MODIFY_EXPR, TREE_TYPE (res), res, t);
c2255bc4 10379 SET_EXPR_LOCATION (retval, loc);
ca085fd7
MLI
10380
10381 if (warn_sequence_point)
10382 verify_sequence_points (retval);
de520661 10383 }
8b6a5902 10384
c2255bc4 10385 ret_stmt = build_stmt (loc, RETURN_EXPR, retval);
0c9b182b
JJ
10386 TREE_NO_WARNING (ret_stmt) |= no_warning;
10387 return add_stmt (ret_stmt);
de520661 10388}
3e4093b6
RS
10389\f
10390struct c_switch {
604f5adf
ILT
10391 /* The SWITCH_EXPR being built. */
10392 tree switch_expr;
a6c0a76c 10393
89dbed81 10394 /* The original type of the testing expression, i.e. before the
a6c0a76c
SB
10395 default conversion is applied. */
10396 tree orig_type;
10397
3e4093b6
RS
10398 /* A splay-tree mapping the low element of a case range to the high
10399 element, or NULL_TREE if there is no high element. Used to
10400 determine whether or not a new case label duplicates an old case
10401 label. We need a tree, rather than simply a hash table, because
10402 of the GNU case range extension. */
10403 splay_tree cases;
a6c0a76c 10404
e1b7793c
ILT
10405 /* The bindings at the point of the switch. This is used for
10406 warnings crossing decls when branching to a case label. */
10407 struct c_spot_bindings *bindings;
187230a7 10408
3e4093b6
RS
10409 /* The next node on the stack. */
10410 struct c_switch *next;
b155cfd9
MP
10411
10412 /* Remember whether the controlling expression had boolean type
10413 before integer promotions for the sake of -Wswitch-bool. */
10414 bool bool_cond_p;
10415
10416 /* Remember whether there was a case value that is outside the
10417 range of the ORIG_TYPE. */
10418 bool outside_range_p;
3e4093b6 10419};
400fbf9f 10420
3e4093b6
RS
10421/* A stack of the currently active switch statements. The innermost
10422 switch statement is on the top of the stack. There is no need to
10423 mark the stack for garbage collection because it is only active
10424 during the processing of the body of a function, and we never
10425 collect at that point. */
de520661 10426
506e2710 10427struct c_switch *c_switch_stack;
de520661 10428
3e4093b6 10429/* Start a C switch statement, testing expression EXP. Return the new
c2255bc4 10430 SWITCH_EXPR. SWITCH_LOC is the location of the `switch'.
fedfecef 10431 SWITCH_COND_LOC is the location of the switch's condition.
b155cfd9 10432 EXPLICIT_CAST_P is true if the expression EXP has an explicit cast. */
de520661 10433
3e4093b6 10434tree
c2255bc4
AH
10435c_start_case (location_t switch_loc,
10436 location_t switch_cond_loc,
fedfecef 10437 tree exp, bool explicit_cast_p)
de520661 10438{
c58e8676 10439 tree orig_type = error_mark_node;
b155cfd9 10440 bool bool_cond_p = false;
3e4093b6 10441 struct c_switch *cs;
2f6e4e97 10442
3e4093b6 10443 if (exp != error_mark_node)
de520661 10444 {
3e4093b6
RS
10445 orig_type = TREE_TYPE (exp);
10446
c58e8676 10447 if (!INTEGRAL_TYPE_P (orig_type))
de520661 10448 {
c58e8676
VR
10449 if (orig_type != error_mark_node)
10450 {
c2255bc4 10451 error_at (switch_cond_loc, "switch quantity not an integer");
c58e8676
VR
10452 orig_type = error_mark_node;
10453 }
3e4093b6 10454 exp = integer_zero_node;
de520661 10455 }
3e4093b6 10456 else
de520661 10457 {
c58e8676 10458 tree type = TYPE_MAIN_VARIANT (orig_type);
fedfecef
MP
10459 tree e = exp;
10460
10461 /* Warn if the condition has boolean value. */
10462 while (TREE_CODE (e) == COMPOUND_EXPR)
10463 e = TREE_OPERAND (e, 1);
10464
10465 if ((TREE_CODE (type) == BOOLEAN_TYPE
10466 || truth_value_p (TREE_CODE (e)))
10467 /* Explicit cast to int suppresses this warning. */
10468 && !(TREE_CODE (type) == INTEGER_TYPE
10469 && explicit_cast_p))
b155cfd9 10470 bool_cond_p = true;
8b6a5902 10471
8400e75e 10472 if (!in_system_header_at (input_location)
3e4093b6
RS
10473 && (type == long_integer_type_node
10474 || type == long_unsigned_type_node))
c2255bc4
AH
10475 warning_at (switch_cond_loc,
10476 OPT_Wtraditional, "%<long%> switch expression not "
10477 "converted to %<int%> in ISO C");
8b6a5902 10478
928c19bb 10479 exp = c_fully_fold (exp, false, NULL);
3e4093b6 10480 exp = default_conversion (exp);
ca085fd7
MLI
10481
10482 if (warn_sequence_point)
10483 verify_sequence_points (exp);
3e4093b6
RS
10484 }
10485 }
10486
604f5adf 10487 /* Add this new SWITCH_EXPR to the stack. */
5d038c4c 10488 cs = XNEW (struct c_switch);
9e851845 10489 cs->switch_expr = build2 (SWITCH_EXPR, orig_type, exp, NULL_TREE);
c2255bc4 10490 SET_EXPR_LOCATION (cs->switch_expr, switch_loc);
a6c0a76c 10491 cs->orig_type = orig_type;
3e4093b6 10492 cs->cases = splay_tree_new (case_compare, NULL, NULL);
e1b7793c 10493 cs->bindings = c_get_switch_bindings ();
b155cfd9
MP
10494 cs->bool_cond_p = bool_cond_p;
10495 cs->outside_range_p = false;
506e2710
RH
10496 cs->next = c_switch_stack;
10497 c_switch_stack = cs;
3e4093b6 10498
604f5adf 10499 return add_stmt (cs->switch_expr);
3e4093b6
RS
10500}
10501
c2255bc4 10502/* Process a case label at location LOC. */
3e4093b6
RS
10503
10504tree
c2255bc4 10505do_case (location_t loc, tree low_value, tree high_value)
3e4093b6
RS
10506{
10507 tree label = NULL_TREE;
10508
17cede2e
JM
10509 if (low_value && TREE_CODE (low_value) != INTEGER_CST)
10510 {
10511 low_value = c_fully_fold (low_value, false, NULL);
10512 if (TREE_CODE (low_value) == INTEGER_CST)
d033409e 10513 pedwarn (loc, OPT_Wpedantic,
17cede2e
JM
10514 "case label is not an integer constant expression");
10515 }
10516
10517 if (high_value && TREE_CODE (high_value) != INTEGER_CST)
10518 {
10519 high_value = c_fully_fold (high_value, false, NULL);
10520 if (TREE_CODE (high_value) == INTEGER_CST)
c1771a20 10521 pedwarn (input_location, OPT_Wpedantic,
17cede2e
JM
10522 "case label is not an integer constant expression");
10523 }
10524
e1b7793c 10525 if (c_switch_stack == NULL)
187230a7
JM
10526 {
10527 if (low_value)
e1b7793c 10528 error_at (loc, "case label not within a switch statement");
187230a7 10529 else
e1b7793c
ILT
10530 error_at (loc, "%<default%> label not within a switch statement");
10531 return NULL_TREE;
187230a7 10532 }
de520661 10533
e1b7793c
ILT
10534 if (c_check_switch_jump_warnings (c_switch_stack->bindings,
10535 EXPR_LOCATION (c_switch_stack->switch_expr),
10536 loc))
10537 return NULL_TREE;
10538
10539 label = c_add_case_label (loc, c_switch_stack->cases,
10540 SWITCH_COND (c_switch_stack->switch_expr),
10541 c_switch_stack->orig_type,
b155cfd9
MP
10542 low_value, high_value,
10543 &c_switch_stack->outside_range_p);
e1b7793c
ILT
10544 if (label == error_mark_node)
10545 label = NULL_TREE;
3e4093b6
RS
10546 return label;
10547}
de520661 10548
083e891e
MP
10549/* Finish the switch statement. TYPE is the original type of the
10550 controlling expression of the switch, or NULL_TREE. */
de520661 10551
3e4093b6 10552void
083e891e 10553c_finish_case (tree body, tree type)
3e4093b6 10554{
506e2710 10555 struct c_switch *cs = c_switch_stack;
fbc315db 10556 location_t switch_location;
3e4093b6 10557
604f5adf 10558 SWITCH_BODY (cs->switch_expr) = body;
325c3691 10559
6de9cd9a 10560 /* Emit warnings as needed. */
c2255bc4 10561 switch_location = EXPR_LOCATION (cs->switch_expr);
fbc315db 10562 c_do_switch_warnings (cs->cases, switch_location,
083e891e 10563 type ? type : TREE_TYPE (cs->switch_expr),
b155cfd9
MP
10564 SWITCH_COND (cs->switch_expr),
10565 cs->bool_cond_p, cs->outside_range_p);
058f0b9e
JJ
10566 if (c_switch_covers_all_cases_p (cs->cases, TREE_TYPE (cs->switch_expr)))
10567 SWITCH_ALL_CASES_P (cs->switch_expr) = 1;
6de9cd9a 10568
3e4093b6 10569 /* Pop the stack. */
506e2710 10570 c_switch_stack = cs->next;
3e4093b6 10571 splay_tree_delete (cs->cases);
e1b7793c 10572 c_release_switch_bindings (cs->bindings);
5d038c4c 10573 XDELETE (cs);
de520661 10574}
325c3691 10575\f
506e2710
RH
10576/* Emit an if statement. IF_LOCUS is the location of the 'if'. COND,
10577 THEN_BLOCK and ELSE_BLOCK are expressions to be used; ELSE_BLOCK
99cd9857 10578 may be null. */
325c3691 10579
9e51cf9d 10580void
506e2710 10581c_finish_if_stmt (location_t if_locus, tree cond, tree then_block,
99cd9857 10582 tree else_block)
325c3691 10583{
506e2710 10584 tree stmt;
325c3691 10585
2214de30 10586 stmt = build3 (COND_EXPR, void_type_node, cond, then_block, else_block);
a281759f 10587 SET_EXPR_LOCATION (stmt, if_locus);
506e2710 10588 add_stmt (stmt);
325c3691
RH
10589}
10590
506e2710
RH
10591/* Emit a general-purpose loop construct. START_LOCUS is the location of
10592 the beginning of the loop. COND is the loop condition. COND_IS_FIRST
10593 is false for DO loops. INCR is the FOR increment expression. BODY is
61ada8ae 10594 the statement controlled by the loop. BLAB is the break label. CLAB is
506e2710 10595 the continue label. Everything is allowed to be NULL. */
325c3691
RH
10596
10597void
506e2710
RH
10598c_finish_loop (location_t start_locus, tree cond, tree incr, tree body,
10599 tree blab, tree clab, bool cond_is_first)
325c3691 10600{
506e2710
RH
10601 tree entry = NULL, exit = NULL, t;
10602
28af952a
RS
10603 /* If the condition is zero don't generate a loop construct. */
10604 if (cond && integer_zerop (cond))
10605 {
10606 if (cond_is_first)
10607 {
10608 t = build_and_jump (&blab);
10609 SET_EXPR_LOCATION (t, start_locus);
10610 add_stmt (t);
10611 }
10612 }
10613 else
506e2710
RH
10614 {
10615 tree top = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
c22cacf3 10616
506e2710 10617 /* If we have an exit condition, then we build an IF with gotos either
c22cacf3
MS
10618 out of the loop, or to the top of it. If there's no exit condition,
10619 then we just build a jump back to the top. */
506e2710 10620 exit = build_and_jump (&LABEL_EXPR_LABEL (top));
c22cacf3 10621
28af952a 10622 if (cond && !integer_nonzerop (cond))
c22cacf3
MS
10623 {
10624 /* Canonicalize the loop condition to the end. This means
10625 generating a branch to the loop condition. Reuse the
10626 continue label, if possible. */
10627 if (cond_is_first)
10628 {
10629 if (incr || !clab)
10630 {
10631 entry = build1 (LABEL_EXPR, void_type_node, NULL_TREE);
10632 t = build_and_jump (&LABEL_EXPR_LABEL (entry));
10633 }
10634 else
10635 t = build1 (GOTO_EXPR, void_type_node, clab);
a281759f 10636 SET_EXPR_LOCATION (t, start_locus);
c22cacf3
MS
10637 add_stmt (t);
10638 }
10639
506e2710 10640 t = build_and_jump (&blab);
506e2710 10641 if (cond_is_first)
db3927fb
AH
10642 exit = fold_build3_loc (start_locus,
10643 COND_EXPR, void_type_node, cond, exit, t);
506e2710 10644 else
db3927fb
AH
10645 exit = fold_build3_loc (input_location,
10646 COND_EXPR, void_type_node, cond, exit, t);
c22cacf3 10647 }
fc402eec
AA
10648 else
10649 {
10650 /* For the backward-goto's location of an unconditional loop
10651 use the beginning of the body, or, if there is none, the
10652 top of the loop. */
10653 location_t loc = EXPR_LOCATION (expr_first (body));
10654 if (loc == UNKNOWN_LOCATION)
10655 loc = start_locus;
10656 SET_EXPR_LOCATION (exit, loc);
10657 }
c22cacf3 10658
506e2710
RH
10659 add_stmt (top);
10660 }
c22cacf3 10661
506e2710
RH
10662 if (body)
10663 add_stmt (body);
10664 if (clab)
10665 add_stmt (build1 (LABEL_EXPR, void_type_node, clab));
10666 if (incr)
10667 add_stmt (incr);
10668 if (entry)
10669 add_stmt (entry);
10670 if (exit)
10671 add_stmt (exit);
10672 if (blab)
10673 add_stmt (build1 (LABEL_EXPR, void_type_node, blab));
325c3691 10674}
325c3691
RH
10675
10676tree
c2255bc4 10677c_finish_bc_stmt (location_t loc, tree *label_p, bool is_break)
325c3691 10678{
089efaa4 10679 bool skip;
506e2710 10680 tree label = *label_p;
325c3691 10681
089efaa4
ILT
10682 /* In switch statements break is sometimes stylistically used after
10683 a return statement. This can lead to spurious warnings about
10684 control reaching the end of a non-void function when it is
10685 inlined. Note that we are calling block_may_fallthru with
10686 language specific tree nodes; this works because
10687 block_may_fallthru returns true when given something it does not
10688 understand. */
10689 skip = !block_may_fallthru (cur_stmt_list);
10690
506e2710 10691 if (!label)
089efaa4
ILT
10692 {
10693 if (!skip)
c2255bc4 10694 *label_p = label = create_artificial_label (loc);
089efaa4 10695 }
953ff289
DN
10696 else if (TREE_CODE (label) == LABEL_DECL)
10697 ;
10698 else switch (TREE_INT_CST_LOW (label))
506e2710 10699 {
953ff289 10700 case 0:
506e2710 10701 if (is_break)
c2255bc4 10702 error_at (loc, "break statement not within loop or switch");
506e2710 10703 else
c2255bc4 10704 error_at (loc, "continue statement not within a loop");
506e2710 10705 return NULL_TREE;
953ff289
DN
10706
10707 case 1:
10708 gcc_assert (is_break);
c2255bc4 10709 error_at (loc, "break statement used with OpenMP for loop");
953ff289
DN
10710 return NULL_TREE;
10711
c02065fc
AH
10712 case 2:
10713 if (is_break)
10714 error ("break statement within %<#pragma simd%> loop body");
10715 else
10716 error ("continue statement within %<#pragma simd%> loop body");
10717 return NULL_TREE;
10718
953ff289
DN
10719 default:
10720 gcc_unreachable ();
506e2710 10721 }
325c3691 10722
089efaa4
ILT
10723 if (skip)
10724 return NULL_TREE;
10725
2e28e797
JH
10726 if (!is_break)
10727 add_stmt (build_predict_expr (PRED_CONTINUE, NOT_TAKEN));
10728
53fb4de3 10729 return add_stmt (build1 (GOTO_EXPR, void_type_node, label));
325c3691
RH
10730}
10731
506e2710 10732/* A helper routine for c_process_expr_stmt and c_finish_stmt_expr. */
3a5b9284
RH
10733
10734static void
c2255bc4 10735emit_side_effect_warnings (location_t loc, tree expr)
3a5b9284 10736{
e6b5a630
RH
10737 if (expr == error_mark_node)
10738 ;
10739 else if (!TREE_SIDE_EFFECTS (expr))
3a5b9284
RH
10740 {
10741 if (!VOID_TYPE_P (TREE_TYPE (expr)) && !TREE_NO_WARNING (expr))
c2255bc4 10742 warning_at (loc, OPT_Wunused_value, "statement with no effect");
3a5b9284 10743 }
789eadcd
MP
10744 else if (TREE_CODE (expr) == COMPOUND_EXPR)
10745 {
10746 tree r = expr;
10747 location_t cloc = loc;
10748 while (TREE_CODE (r) == COMPOUND_EXPR)
10749 {
10750 if (EXPR_HAS_LOCATION (r))
10751 cloc = EXPR_LOCATION (r);
10752 r = TREE_OPERAND (r, 1);
10753 }
10754 if (!TREE_SIDE_EFFECTS (r)
10755 && !VOID_TYPE_P (TREE_TYPE (r))
10756 && !CONVERT_EXPR_P (r)
cc28fc7f 10757 && !TREE_NO_WARNING (r)
789eadcd
MP
10758 && !TREE_NO_WARNING (expr))
10759 warning_at (cloc, OPT_Wunused_value,
10760 "right-hand operand of comma expression has no effect");
10761 }
27f33b15 10762 else
c2255bc4 10763 warn_if_unused_value (expr, loc);
3a5b9284
RH
10764}
10765
506e2710 10766/* Process an expression as if it were a complete statement. Emit
c2255bc4
AH
10767 diagnostics, but do not call ADD_STMT. LOC is the location of the
10768 statement. */
3a5b9284 10769
506e2710 10770tree
c2255bc4 10771c_process_expr_stmt (location_t loc, tree expr)
3a5b9284 10772{
056928b2
JJ
10773 tree exprv;
10774
3a5b9284 10775 if (!expr)
506e2710 10776 return NULL_TREE;
3a5b9284 10777
928c19bb
JM
10778 expr = c_fully_fold (expr, false, NULL);
10779
3a5b9284
RH
10780 if (warn_sequence_point)
10781 verify_sequence_points (expr);
10782
10783 if (TREE_TYPE (expr) != error_mark_node
10784 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
10785 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
c2255bc4 10786 error_at (loc, "expression statement has incomplete type");
3a5b9284
RH
10787
10788 /* If we're not processing a statement expression, warn about unused values.
10789 Warnings for statement expressions will be emitted later, once we figure
10790 out which is the result. */
10791 if (!STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
27f33b15 10792 && warn_unused_value)
d06f8b75 10793 emit_side_effect_warnings (EXPR_LOC_OR_LOC (expr, loc), expr);
3a5b9284 10794
056928b2
JJ
10795 exprv = expr;
10796 while (TREE_CODE (exprv) == COMPOUND_EXPR)
10797 exprv = TREE_OPERAND (exprv, 1);
f1a89dd0
JJ
10798 while (CONVERT_EXPR_P (exprv))
10799 exprv = TREE_OPERAND (exprv, 0);
10800 if (DECL_P (exprv)
10801 || handled_component_p (exprv)
10802 || TREE_CODE (exprv) == ADDR_EXPR)
056928b2 10803 mark_exp_read (exprv);
fa8351f8 10804
3a5b9284
RH
10805 /* If the expression is not of a type to which we cannot assign a line
10806 number, wrap the thing in a no-op NOP_EXPR. */
6615c446 10807 if (DECL_P (expr) || CONSTANT_CLASS_P (expr))
c2255bc4
AH
10808 {
10809 expr = build1 (NOP_EXPR, TREE_TYPE (expr), expr);
10810 SET_EXPR_LOCATION (expr, loc);
10811 }
506e2710
RH
10812
10813 return expr;
10814}
10815
c2255bc4
AH
10816/* Emit an expression as a statement. LOC is the location of the
10817 expression. */
506e2710
RH
10818
10819tree
c2255bc4 10820c_finish_expr_stmt (location_t loc, tree expr)
506e2710
RH
10821{
10822 if (expr)
c2255bc4 10823 return add_stmt (c_process_expr_stmt (loc, expr));
506e2710
RH
10824 else
10825 return NULL;
3a5b9284
RH
10826}
10827
10828/* Do the opposite and emit a statement as an expression. To begin,
10829 create a new binding level and return it. */
325c3691
RH
10830
10831tree
10832c_begin_stmt_expr (void)
10833{
10834 tree ret;
10835
10836 /* We must force a BLOCK for this level so that, if it is not expanded
10837 later, there is a way to turn off the entire subtree of blocks that
10838 are contained in it. */
10839 keep_next_level ();
10840 ret = c_begin_compound_stmt (true);
e1b7793c
ILT
10841
10842 c_bindings_start_stmt_expr (c_switch_stack == NULL
10843 ? NULL
10844 : c_switch_stack->bindings);
325c3691
RH
10845
10846 /* Mark the current statement list as belonging to a statement list. */
10847 STATEMENT_LIST_STMT_EXPR (ret) = 1;
10848
10849 return ret;
10850}
10851
c2255bc4
AH
10852/* LOC is the location of the compound statement to which this body
10853 belongs. */
10854
325c3691 10855tree
c2255bc4 10856c_finish_stmt_expr (location_t loc, tree body)
325c3691 10857{
3a5b9284 10858 tree last, type, tmp, val;
325c3691
RH
10859 tree *last_p;
10860
c2255bc4 10861 body = c_end_compound_stmt (loc, body, true);
e1b7793c
ILT
10862
10863 c_bindings_end_stmt_expr (c_switch_stack == NULL
10864 ? NULL
10865 : c_switch_stack->bindings);
325c3691 10866
3a5b9284
RH
10867 /* Locate the last statement in BODY. See c_end_compound_stmt
10868 about always returning a BIND_EXPR. */
10869 last_p = &BIND_EXPR_BODY (body);
10870 last = BIND_EXPR_BODY (body);
10871
10872 continue_searching:
325c3691
RH
10873 if (TREE_CODE (last) == STATEMENT_LIST)
10874 {
11d29d63
JJ
10875 tree_stmt_iterator l = tsi_last (last);
10876
10877 while (!tsi_end_p (l) && TREE_CODE (tsi_stmt (l)) == DEBUG_BEGIN_STMT)
10878 tsi_prev (&l);
3a5b9284
RH
10879
10880 /* This can happen with degenerate cases like ({ }). No value. */
11d29d63 10881 if (tsi_end_p (l))
3a5b9284
RH
10882 return body;
10883
10884 /* If we're supposed to generate side effects warnings, process
10885 all of the statements except the last. */
27f33b15 10886 if (warn_unused_value)
325c3691 10887 {
11d29d63
JJ
10888 for (tree_stmt_iterator i = tsi_start (last);
10889 tsi_stmt (i) != tsi_stmt (l); tsi_next (&i))
c2255bc4
AH
10890 {
10891 location_t tloc;
10892 tree t = tsi_stmt (i);
10893
10894 tloc = EXPR_HAS_LOCATION (t) ? EXPR_LOCATION (t) : loc;
10895 emit_side_effect_warnings (tloc, t);
10896 }
325c3691 10897 }
11d29d63 10898 last_p = tsi_stmt_ptr (l);
3a5b9284 10899 last = *last_p;
325c3691
RH
10900 }
10901
3a5b9284
RH
10902 /* If the end of the list is exception related, then the list was split
10903 by a call to push_cleanup. Continue searching. */
10904 if (TREE_CODE (last) == TRY_FINALLY_EXPR
10905 || TREE_CODE (last) == TRY_CATCH_EXPR)
10906 {
10907 last_p = &TREE_OPERAND (last, 0);
10908 last = *last_p;
10909 goto continue_searching;
10910 }
10911
26d8af35
JM
10912 if (last == error_mark_node)
10913 return last;
10914
3a5b9284
RH
10915 /* In the case that the BIND_EXPR is not necessary, return the
10916 expression out from inside it. */
96a95ac1
AO
10917 if ((last == BIND_EXPR_BODY (body)
10918 /* Skip nested debug stmts. */
10919 || last == expr_first (BIND_EXPR_BODY (body)))
26d8af35 10920 && BIND_EXPR_VARS (body) == NULL)
591baeb0 10921 {
928c19bb
JM
10922 /* Even if this looks constant, do not allow it in a constant
10923 expression. */
e5a94231 10924 last = c_wrap_maybe_const (last, true);
591baeb0
JM
10925 /* Do not warn if the return value of a statement expression is
10926 unused. */
928c19bb 10927 TREE_NO_WARNING (last) = 1;
591baeb0
JM
10928 return last;
10929 }
325c3691
RH
10930
10931 /* Extract the type of said expression. */
10932 type = TREE_TYPE (last);
325c3691 10933
3a5b9284
RH
10934 /* If we're not returning a value at all, then the BIND_EXPR that
10935 we already have is a fine expression to return. */
10936 if (!type || VOID_TYPE_P (type))
10937 return body;
10938
10939 /* Now that we've located the expression containing the value, it seems
10940 silly to make voidify_wrapper_expr repeat the process. Create a
10941 temporary of the appropriate type and stick it in a TARGET_EXPR. */
b731b390 10942 tmp = create_tmp_var_raw (type);
3a5b9284
RH
10943
10944 /* Unwrap a no-op NOP_EXPR as added by c_finish_expr_stmt. This avoids
10945 tree_expr_nonnegative_p giving up immediately. */
10946 val = last;
10947 if (TREE_CODE (val) == NOP_EXPR
10948 && TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0)))
10949 val = TREE_OPERAND (val, 0);
10950
53fb4de3 10951 *last_p = build2 (MODIFY_EXPR, void_type_node, tmp, val);
5e278028 10952 SET_EXPR_LOCATION (*last_p, EXPR_LOCATION (last));
3a5b9284 10953
c2255bc4
AH
10954 {
10955 tree t = build4 (TARGET_EXPR, type, tmp, body, NULL_TREE, NULL_TREE);
10956 SET_EXPR_LOCATION (t, loc);
10957 return t;
10958 }
325c3691
RH
10959}
10960\f
10961/* Begin and end compound statements. This is as simple as pushing
10962 and popping new statement lists from the tree. */
10963
10964tree
10965c_begin_compound_stmt (bool do_scope)
10966{
10967 tree stmt = push_stmt_list ();
10968 if (do_scope)
4dfa0342 10969 push_scope ();
325c3691
RH
10970 return stmt;
10971}
10972
c2255bc4
AH
10973/* End a compound statement. STMT is the statement. LOC is the
10974 location of the compound statement-- this is usually the location
10975 of the opening brace. */
10976
325c3691 10977tree
c2255bc4 10978c_end_compound_stmt (location_t loc, tree stmt, bool do_scope)
325c3691
RH
10979{
10980 tree block = NULL;
10981
10982 if (do_scope)
10983 {
10984 if (c_dialect_objc ())
10985 objc_clear_super_receiver ();
10986 block = pop_scope ();
10987 }
10988
10989 stmt = pop_stmt_list (stmt);
c2255bc4 10990 stmt = c_build_bind_expr (loc, block, stmt);
325c3691
RH
10991
10992 /* If this compound statement is nested immediately inside a statement
10993 expression, then force a BIND_EXPR to be created. Otherwise we'll
10994 do the wrong thing for ({ { 1; } }) or ({ 1; { } }). In particular,
10995 STATEMENT_LISTs merge, and thus we can lose track of what statement
10996 was really last. */
38e01f9e 10997 if (building_stmt_list_p ()
325c3691
RH
10998 && STATEMENT_LIST_STMT_EXPR (cur_stmt_list)
10999 && TREE_CODE (stmt) != BIND_EXPR)
11000 {
53fb4de3 11001 stmt = build3 (BIND_EXPR, void_type_node, NULL, stmt, NULL);
325c3691 11002 TREE_SIDE_EFFECTS (stmt) = 1;
c2255bc4 11003 SET_EXPR_LOCATION (stmt, loc);
325c3691
RH
11004 }
11005
11006 return stmt;
11007}
5a508662
RH
11008
11009/* Queue a cleanup. CLEANUP is an expression/statement to be executed
11010 when the current scope is exited. EH_ONLY is true when this is not
11011 meant to apply to normal control flow transfer. */
11012
11013void
c2255bc4 11014push_cleanup (tree decl, tree cleanup, bool eh_only)
5a508662 11015{
3a5b9284
RH
11016 enum tree_code code;
11017 tree stmt, list;
11018 bool stmt_expr;
11019
11020 code = eh_only ? TRY_CATCH_EXPR : TRY_FINALLY_EXPR;
c2255bc4 11021 stmt = build_stmt (DECL_SOURCE_LOCATION (decl), code, NULL, cleanup);
5a508662 11022 add_stmt (stmt);
3a5b9284
RH
11023 stmt_expr = STATEMENT_LIST_STMT_EXPR (cur_stmt_list);
11024 list = push_stmt_list ();
11025 TREE_OPERAND (stmt, 0) = list;
11026 STATEMENT_LIST_STMT_EXPR (list) = stmt_expr;
5a508662 11027}
325c3691 11028\f
9f47c7e5
IE
11029/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
11030 into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
11031
11032static tree
11033build_vec_cmp (tree_code code, tree type,
11034 tree arg0, tree arg1)
11035{
11036 tree zero_vec = build_zero_cst (type);
11037 tree minus_one_vec = build_minus_one_cst (type);
11038 tree cmp_type = build_same_sized_truth_vector_type (type);
11039 tree cmp = build2 (code, cmp_type, arg0, arg1);
11040 return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
11041}
11042
96e6ae57
DM
11043/* Subclass of range_label for labelling the type of EXPR when reporting
11044 a type mismatch between EXPR and OTHER_EXPR.
11045 Either or both of EXPR and OTHER_EXPR could be NULL. */
11046
11047class maybe_range_label_for_tree_type_mismatch : public range_label
11048{
11049 public:
11050 maybe_range_label_for_tree_type_mismatch (tree expr, tree other_expr)
11051 : m_expr (expr), m_other_expr (other_expr)
11052 {
11053 }
11054
11055 label_text get_text () const FINAL OVERRIDE
11056 {
11057 if (m_expr == NULL_TREE
11058 || !EXPR_P (m_expr))
11059 return label_text (NULL, false);
11060 tree expr_type = TREE_TYPE (m_expr);
11061
11062 tree other_type = NULL_TREE;
11063 if (m_other_expr && EXPR_P (m_other_expr))
11064 other_type = TREE_TYPE (m_other_expr);
11065
11066 range_label_for_type_mismatch inner (expr_type, other_type);
11067 return inner.get_text ();
11068 }
11069
11070 private:
11071 tree m_expr;
11072 tree m_other_expr;
11073};
11074
3e4093b6
RS
11075/* Build a binary-operation expression without default conversions.
11076 CODE is the kind of expression to build.
ba47d38d 11077 LOCATION is the operator's location.
3e4093b6
RS
11078 This function differs from `build' in several ways:
11079 the data type of the result is computed and recorded in it,
11080 warnings are generated if arg data types are invalid,
11081 special handling for addition and subtraction of pointers is known,
11082 and some optimization is done (operations on narrow ints
11083 are done in the narrower type when that gives the same result).
11084 Constant folding is also done before the result is returned.
de520661 11085
3e4093b6
RS
11086 Note that the operands will never have enumeral types, or function
11087 or array types, because either they will have the default conversions
11088 performed or they have both just been converted to some other type in which
11089 the arithmetic is to be done. */
11090
11091tree
ba47d38d 11092build_binary_op (location_t location, enum tree_code code,
30af3a2b 11093 tree orig_op0, tree orig_op1, bool convert_p)
de520661 11094{
8ce94e44
JM
11095 tree type0, type1, orig_type0, orig_type1;
11096 tree eptype;
3e4093b6
RS
11097 enum tree_code code0, code1;
11098 tree op0, op1;
c9f9eb5d 11099 tree ret = error_mark_node;
4de67c26 11100 const char *invalid_op_diag;
4d84fe7c 11101 bool op0_int_operands, op1_int_operands;
928c19bb 11102 bool int_const, int_const_or_overflow, int_operands;
b62acd60 11103
3e4093b6
RS
11104 /* Expression code to give to the expression when it is built.
11105 Normally this is CODE, which is what the caller asked for,
11106 but in some special cases we change it. */
11107 enum tree_code resultcode = code;
8b6a5902 11108
3e4093b6
RS
11109 /* Data type in which the computation is to be performed.
11110 In the simplest cases this is the common type of the arguments. */
11111 tree result_type = NULL;
11112
8ce94e44
JM
11113 /* When the computation is in excess precision, the type of the
11114 final EXCESS_PRECISION_EXPR. */
2d2e923f 11115 tree semantic_result_type = NULL;
8ce94e44 11116
3e4093b6
RS
11117 /* Nonzero means operands have already been type-converted
11118 in whatever way is necessary.
11119 Zero means they need to be converted to RESULT_TYPE. */
11120 int converted = 0;
11121
11122 /* Nonzero means create the expression with this type, rather than
11123 RESULT_TYPE. */
3fa8871b 11124 tree build_type = NULL_TREE;
3e4093b6
RS
11125
11126 /* Nonzero means after finally constructing the expression
11127 convert it to this type. */
3fa8871b 11128 tree final_type = NULL_TREE;
3e4093b6
RS
11129
11130 /* Nonzero if this is an operation like MIN or MAX which can
11131 safely be computed in short if both args are promoted shorts.
11132 Also implies COMMON.
11133 -1 indicates a bitwise operation; this makes a difference
11134 in the exact conditions for when it is safe to do the operation
11135 in a narrower mode. */
11136 int shorten = 0;
11137
11138 /* Nonzero if this is a comparison operation;
11139 if both args are promoted shorts, compare the original shorts.
11140 Also implies COMMON. */
11141 int short_compare = 0;
11142
11143 /* Nonzero if this is a right-shift operation, which can be computed on the
11144 original short and then promoted if the operand is a promoted short. */
11145 int short_shift = 0;
11146
11147 /* Nonzero means set RESULT_TYPE to the common type of the args. */
11148 int common = 0;
11149
58393038
ZL
11150 /* True means types are compatible as far as ObjC is concerned. */
11151 bool objc_ok;
11152
8ce94e44
JM
11153 /* True means this is an arithmetic operation that may need excess
11154 precision. */
11155 bool may_need_excess_precision;
11156
180f8dbb
JM
11157 /* True means this is a boolean operation that converts both its
11158 operands to truth-values. */
11159 bool boolean_op = false;
11160
de5a5fa1
MP
11161 /* Remember whether we're doing / or %. */
11162 bool doing_div_or_mod = false;
11163
11164 /* Remember whether we're doing << or >>. */
11165 bool doing_shift = false;
11166
11167 /* Tree holding instrumentation expression. */
11168 tree instrument_expr = NULL;
11169
ba47d38d
AH
11170 if (location == UNKNOWN_LOCATION)
11171 location = input_location;
11172
4d84fe7c
JM
11173 op0 = orig_op0;
11174 op1 = orig_op1;
11175
11176 op0_int_operands = EXPR_INT_CONST_OPERANDS (orig_op0);
11177 if (op0_int_operands)
11178 op0 = remove_c_maybe_const_expr (op0);
11179 op1_int_operands = EXPR_INT_CONST_OPERANDS (orig_op1);
11180 if (op1_int_operands)
11181 op1 = remove_c_maybe_const_expr (op1);
11182 int_operands = (op0_int_operands && op1_int_operands);
928c19bb
JM
11183 if (int_operands)
11184 {
11185 int_const_or_overflow = (TREE_CODE (orig_op0) == INTEGER_CST
11186 && TREE_CODE (orig_op1) == INTEGER_CST);
11187 int_const = (int_const_or_overflow
11188 && !TREE_OVERFLOW (orig_op0)
11189 && !TREE_OVERFLOW (orig_op1));
11190 }
11191 else
11192 int_const = int_const_or_overflow = false;
11193
0e3a99ae 11194 /* Do not apply default conversion in mixed vector/scalar expression. */
f90e8e2e 11195 if (convert_p
31521951 11196 && VECTOR_TYPE_P (TREE_TYPE (op0)) == VECTOR_TYPE_P (TREE_TYPE (op1)))
790e9490 11197 {
4d84fe7c
JM
11198 op0 = default_conversion (op0);
11199 op1 = default_conversion (op1);
790e9490
RS
11200 }
11201
5e9d6aa4 11202 orig_type0 = type0 = TREE_TYPE (op0);
36536d79 11203
5e9d6aa4 11204 orig_type1 = type1 = TREE_TYPE (op1);
91fa3c30 11205
3e4093b6
RS
11206 /* The expression codes of the data types of the arguments tell us
11207 whether the arguments are integers, floating, pointers, etc. */
11208 code0 = TREE_CODE (type0);
11209 code1 = TREE_CODE (type1);
11210
11211 /* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
11212 STRIP_TYPE_NOPS (op0);
11213 STRIP_TYPE_NOPS (op1);
11214
11215 /* If an error was already reported for one of the arguments,
11216 avoid reporting another error. */
11217
11218 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11219 return error_mark_node;
11220
1807ffc1
MS
11221 if (code0 == POINTER_TYPE
11222 && reject_gcc_builtin (op0, EXPR_LOCATION (orig_op0)))
11223 return error_mark_node;
11224
11225 if (code1 == POINTER_TYPE
11226 && reject_gcc_builtin (op1, EXPR_LOCATION (orig_op1)))
11227 return error_mark_node;
11228
4de67c26
JM
11229 if ((invalid_op_diag
11230 = targetm.invalid_binary_op (code, type0, type1)))
11231 {
ba47d38d 11232 error_at (location, invalid_op_diag);
4de67c26
JM
11233 return error_mark_node;
11234 }
11235
8ce94e44
JM
11236 switch (code)
11237 {
11238 case PLUS_EXPR:
11239 case MINUS_EXPR:
11240 case MULT_EXPR:
11241 case TRUNC_DIV_EXPR:
11242 case CEIL_DIV_EXPR:
11243 case FLOOR_DIV_EXPR:
11244 case ROUND_DIV_EXPR:
11245 case EXACT_DIV_EXPR:
11246 may_need_excess_precision = true;
11247 break;
11248 default:
11249 may_need_excess_precision = false;
11250 break;
11251 }
11252 if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
11253 {
11254 op0 = TREE_OPERAND (op0, 0);
11255 type0 = TREE_TYPE (op0);
11256 }
11257 else if (may_need_excess_precision
11258 && (eptype = excess_precision_type (type0)) != NULL_TREE)
11259 {
11260 type0 = eptype;
11261 op0 = convert (eptype, op0);
11262 }
11263 if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
11264 {
11265 op1 = TREE_OPERAND (op1, 0);
11266 type1 = TREE_TYPE (op1);
11267 }
11268 else if (may_need_excess_precision
11269 && (eptype = excess_precision_type (type1)) != NULL_TREE)
11270 {
11271 type1 = eptype;
11272 op1 = convert (eptype, op1);
11273 }
11274
58393038
ZL
11275 objc_ok = objc_compare_types (type0, type1, -3, NULL_TREE);
11276
0e3a99ae
AS
11277 /* In case when one of the operands of the binary operation is
11278 a vector and another is a scalar -- convert scalar to vector. */
11279 if ((code0 == VECTOR_TYPE) != (code1 == VECTOR_TYPE))
11280 {
a212e43f
MG
11281 enum stv_conv convert_flag = scalar_to_vector (location, code, op0, op1,
11282 true);
f90e8e2e 11283
0e3a99ae
AS
11284 switch (convert_flag)
11285 {
11286 case stv_error:
11287 return error_mark_node;
11288 case stv_firstarg:
11289 {
11290 bool maybe_const = true;
11291 tree sc;
11292 sc = c_fully_fold (op0, false, &maybe_const);
11293 sc = save_expr (sc);
11294 sc = convert (TREE_TYPE (type1), sc);
11295 op0 = build_vector_from_val (type1, sc);
11296 if (!maybe_const)
11297 op0 = c_wrap_maybe_const (op0, true);
11298 orig_type0 = type0 = TREE_TYPE (op0);
11299 code0 = TREE_CODE (type0);
11300 converted = 1;
11301 break;
11302 }
11303 case stv_secondarg:
11304 {
11305 bool maybe_const = true;
11306 tree sc;
11307 sc = c_fully_fold (op1, false, &maybe_const);
11308 sc = save_expr (sc);
11309 sc = convert (TREE_TYPE (type0), sc);
11310 op1 = build_vector_from_val (type0, sc);
11311 if (!maybe_const)
54b9f838 11312 op1 = c_wrap_maybe_const (op1, true);
0e3a99ae
AS
11313 orig_type1 = type1 = TREE_TYPE (op1);
11314 code1 = TREE_CODE (type1);
11315 converted = 1;
11316 break;
11317 }
11318 default:
11319 break;
11320 }
11321 }
11322
3e4093b6 11323 switch (code)
de520661 11324 {
3e4093b6
RS
11325 case PLUS_EXPR:
11326 /* Handle the pointer + int case. */
11327 if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 11328 {
db3927fb 11329 ret = pointer_int_sum (location, PLUS_EXPR, op0, op1);
c9f9eb5d
AH
11330 goto return_build_binary_op;
11331 }
3e4093b6 11332 else if (code1 == POINTER_TYPE && code0 == INTEGER_TYPE)
c9f9eb5d 11333 {
db3927fb 11334 ret = pointer_int_sum (location, PLUS_EXPR, op1, op0);
c9f9eb5d
AH
11335 goto return_build_binary_op;
11336 }
fe67cf58 11337 else
3e4093b6
RS
11338 common = 1;
11339 break;
400fbf9f 11340
3e4093b6
RS
11341 case MINUS_EXPR:
11342 /* Subtraction of two similar pointers.
11343 We must subtract them as integers, then divide by object size. */
11344 if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
744aa42f 11345 && comp_target_types (location, type0, type1))
c9f9eb5d 11346 {
05abad4c 11347 ret = pointer_diff (location, op0, op1, &instrument_expr);
c9f9eb5d
AH
11348 goto return_build_binary_op;
11349 }
3e4093b6
RS
11350 /* Handle pointer minus int. Just like pointer plus int. */
11351 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
c9f9eb5d 11352 {
db3927fb 11353 ret = pointer_int_sum (location, MINUS_EXPR, op0, op1);
c9f9eb5d
AH
11354 goto return_build_binary_op;
11355 }
3e4093b6
RS
11356 else
11357 common = 1;
11358 break;
8b6a5902 11359
3e4093b6
RS
11360 case MULT_EXPR:
11361 common = 1;
11362 break;
11363
11364 case TRUNC_DIV_EXPR:
11365 case CEIL_DIV_EXPR:
11366 case FLOOR_DIV_EXPR:
11367 case ROUND_DIV_EXPR:
11368 case EXACT_DIV_EXPR:
de5a5fa1 11369 doing_div_or_mod = true;
c9f9eb5d 11370 warn_for_div_by_zero (location, op1);
3e4093b6
RS
11371
11372 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11373 || code0 == FIXED_POINT_TYPE
3e4093b6
RS
11374 || code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
11375 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11376 || code1 == FIXED_POINT_TYPE
3e4093b6 11377 || code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11378 {
5bed876a
AH
11379 enum tree_code tcode0 = code0, tcode1 = code1;
11380
3a021db2 11381 if (code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
5bed876a 11382 tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
3a021db2 11383 if (code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE)
5bed876a 11384 tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
3a021db2 11385
ab22c1fa
CF
11386 if (!((tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE)
11387 || (tcode0 == FIXED_POINT_TYPE && tcode1 == FIXED_POINT_TYPE)))
3e4093b6
RS
11388 resultcode = RDIV_EXPR;
11389 else
11390 /* Although it would be tempting to shorten always here, that
11391 loses on some targets, since the modulo instruction is
11392 undefined if the quotient can't be represented in the
11393 computation mode. We shorten only if unsigned or if
11394 dividing by something we know != -1. */
8df83eae 11395 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 11396 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 11397 && !integer_all_onesp (op1)));
3e4093b6
RS
11398 common = 1;
11399 }
11400 break;
de520661 11401
3e4093b6 11402 case BIT_AND_EXPR:
3e4093b6
RS
11403 case BIT_IOR_EXPR:
11404 case BIT_XOR_EXPR:
11405 if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
11406 shorten = -1;
9ef0c8d9
AP
11407 /* Allow vector types which are not floating point types. */
11408 else if (code0 == VECTOR_TYPE
11409 && code1 == VECTOR_TYPE
11410 && !VECTOR_FLOAT_TYPE_P (type0)
11411 && !VECTOR_FLOAT_TYPE_P (type1))
3e4093b6
RS
11412 common = 1;
11413 break;
11414
11415 case TRUNC_MOD_EXPR:
11416 case FLOOR_MOD_EXPR:
de5a5fa1 11417 doing_div_or_mod = true;
c9f9eb5d 11418 warn_for_div_by_zero (location, op1);
de520661 11419
5cfd5d9b
AP
11420 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11421 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11422 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
11423 common = 1;
11424 else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
3e4093b6
RS
11425 {
11426 /* Although it would be tempting to shorten always here, that loses
11427 on some targets, since the modulo instruction is undefined if the
11428 quotient can't be represented in the computation mode. We shorten
11429 only if unsigned or if dividing by something we know != -1. */
8df83eae 11430 shorten = (TYPE_UNSIGNED (TREE_TYPE (orig_op0))
3e4093b6 11431 || (TREE_CODE (op1) == INTEGER_CST
3f75a254 11432 && !integer_all_onesp (op1)));
3e4093b6
RS
11433 common = 1;
11434 }
11435 break;
de520661 11436
3e4093b6
RS
11437 case TRUTH_ANDIF_EXPR:
11438 case TRUTH_ORIF_EXPR:
11439 case TRUTH_AND_EXPR:
11440 case TRUTH_OR_EXPR:
11441 case TRUTH_XOR_EXPR:
11442 if ((code0 == INTEGER_TYPE || code0 == POINTER_TYPE
ab22c1fa
CF
11443 || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
11444 || code0 == FIXED_POINT_TYPE)
3e4093b6 11445 && (code1 == INTEGER_TYPE || code1 == POINTER_TYPE
ab22c1fa
CF
11446 || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
11447 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11448 {
11449 /* Result of these operations is always an int,
11450 but that does not mean the operands should be
11451 converted to ints! */
11452 result_type = integer_type_node;
a27d595d
JM
11453 if (op0_int_operands)
11454 {
11455 op0 = c_objc_common_truthvalue_conversion (location, orig_op0);
11456 op0 = remove_c_maybe_const_expr (op0);
11457 }
11458 else
11459 op0 = c_objc_common_truthvalue_conversion (location, op0);
11460 if (op1_int_operands)
11461 {
11462 op1 = c_objc_common_truthvalue_conversion (location, orig_op1);
11463 op1 = remove_c_maybe_const_expr (op1);
11464 }
11465 else
11466 op1 = c_objc_common_truthvalue_conversion (location, op1);
3e4093b6 11467 converted = 1;
180f8dbb 11468 boolean_op = true;
3e4093b6 11469 }
928c19bb
JM
11470 if (code == TRUTH_ANDIF_EXPR)
11471 {
11472 int_const_or_overflow = (int_operands
11473 && TREE_CODE (orig_op0) == INTEGER_CST
11474 && (op0 == truthvalue_false_node
11475 || TREE_CODE (orig_op1) == INTEGER_CST));
11476 int_const = (int_const_or_overflow
11477 && !TREE_OVERFLOW (orig_op0)
11478 && (op0 == truthvalue_false_node
11479 || !TREE_OVERFLOW (orig_op1)));
11480 }
11481 else if (code == TRUTH_ORIF_EXPR)
11482 {
11483 int_const_or_overflow = (int_operands
11484 && TREE_CODE (orig_op0) == INTEGER_CST
11485 && (op0 == truthvalue_true_node
11486 || TREE_CODE (orig_op1) == INTEGER_CST));
11487 int_const = (int_const_or_overflow
11488 && !TREE_OVERFLOW (orig_op0)
11489 && (op0 == truthvalue_true_node
11490 || !TREE_OVERFLOW (orig_op1)));
11491 }
3e4093b6 11492 break;
eba80994 11493
3e4093b6
RS
11494 /* Shift operations: result has same type as first operand;
11495 always convert second operand to int.
11496 Also set SHORT_SHIFT if shifting rightward. */
de520661 11497
3e4093b6 11498 case RSHIFT_EXPR:
76e2c821
JB
11499 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11500 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11501 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
928686b1
RS
11502 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11503 TYPE_VECTOR_SUBPARTS (type1)))
f87bd04b
AS
11504 {
11505 result_type = type0;
11506 converted = 1;
11507 }
76e2c821 11508 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
9bb45a95
JJ
11509 || (code0 == VECTOR_TYPE
11510 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
451b5e48 11511 && code1 == INTEGER_TYPE)
3e4093b6 11512 {
de5a5fa1 11513 doing_shift = true;
928c19bb 11514 if (TREE_CODE (op1) == INTEGER_CST)
b62acd60 11515 {
3e4093b6 11516 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11517 {
11518 int_const = false;
7d882b83 11519 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11520 warning_at (location, OPT_Wshift_count_negative,
11521 "right shift count is negative");
928c19bb 11522 }
76e2c821
JB
11523 else if (code0 == VECTOR_TYPE)
11524 {
11525 if (compare_tree_int (op1,
11526 TYPE_PRECISION (TREE_TYPE (type0)))
11527 >= 0)
11528 {
11529 int_const = false;
11530 if (c_inhibit_evaluation_warnings == 0)
11531 warning_at (location, OPT_Wshift_count_overflow,
11532 "right shift count >= width of vector element");
11533 }
11534 }
3e4093b6 11535 else
bbb818c6 11536 {
3f75a254 11537 if (!integer_zerop (op1))
3e4093b6
RS
11538 short_shift = 1;
11539
11540 if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11541 {
11542 int_const = false;
7d882b83 11543 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11544 warning_at (location, OPT_Wshift_count_overflow,
11545 "right shift count >= width of type");
928c19bb 11546 }
bbb818c6 11547 }
b62acd60 11548 }
de520661 11549
3e4093b6
RS
11550 /* Use the type of the value to be shifted. */
11551 result_type = type0;
3e4093b6
RS
11552 /* Avoid converting op1 to result_type later. */
11553 converted = 1;
400fbf9f 11554 }
3e4093b6 11555 break;
253b6b82 11556
3e4093b6 11557 case LSHIFT_EXPR:
76e2c821
JB
11558 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11559 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
11560 && TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
928686b1
RS
11561 && known_eq (TYPE_VECTOR_SUBPARTS (type0),
11562 TYPE_VECTOR_SUBPARTS (type1)))
f87bd04b
AS
11563 {
11564 result_type = type0;
11565 converted = 1;
11566 }
76e2c821 11567 else if ((code0 == INTEGER_TYPE || code0 == FIXED_POINT_TYPE
9bb45a95
JJ
11568 || (code0 == VECTOR_TYPE
11569 && TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE))
451b5e48 11570 && code1 == INTEGER_TYPE)
3e4093b6 11571 {
de5a5fa1 11572 doing_shift = true;
0173bd2a
MP
11573 if (TREE_CODE (op0) == INTEGER_CST
11574 && tree_int_cst_sgn (op0) < 0)
11575 {
11576 /* Don't reject a left shift of a negative value in a context
11577 where a constant expression is needed in C90. */
11578 if (flag_isoc99)
11579 int_const = false;
11580 if (c_inhibit_evaluation_warnings == 0)
11581 warning_at (location, OPT_Wshift_negative_value,
11582 "left shift of negative value");
11583 }
928c19bb 11584 if (TREE_CODE (op1) == INTEGER_CST)
de520661 11585 {
3e4093b6 11586 if (tree_int_cst_sgn (op1) < 0)
928c19bb
JM
11587 {
11588 int_const = false;
7d882b83 11589 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11590 warning_at (location, OPT_Wshift_count_negative,
11591 "left shift count is negative");
928c19bb 11592 }
76e2c821
JB
11593 else if (code0 == VECTOR_TYPE)
11594 {
11595 if (compare_tree_int (op1,
11596 TYPE_PRECISION (TREE_TYPE (type0)))
11597 >= 0)
11598 {
11599 int_const = false;
11600 if (c_inhibit_evaluation_warnings == 0)
11601 warning_at (location, OPT_Wshift_count_overflow,
11602 "left shift count >= width of vector element");
11603 }
11604 }
3e4093b6 11605 else if (compare_tree_int (op1, TYPE_PRECISION (type0)) >= 0)
928c19bb
JM
11606 {
11607 int_const = false;
7d882b83 11608 if (c_inhibit_evaluation_warnings == 0)
13c21655
PC
11609 warning_at (location, OPT_Wshift_count_overflow,
11610 "left shift count >= width of type");
928c19bb 11611 }
451b5e48
MP
11612 else if (TREE_CODE (op0) == INTEGER_CST
11613 && maybe_warn_shift_overflow (location, op0, op1)
11614 && flag_isoc99)
11615 int_const = false;
94ba5069 11616 }
de520661 11617
3e4093b6
RS
11618 /* Use the type of the value to be shifted. */
11619 result_type = type0;
3e4093b6
RS
11620 /* Avoid converting op1 to result_type later. */
11621 converted = 1;
400fbf9f 11622 }
3e4093b6 11623 break;
de520661 11624
3e4093b6
RS
11625 case EQ_EXPR:
11626 case NE_EXPR:
d246ab4f
AS
11627 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11628 {
11629 tree intt;
0af94e6f 11630 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11631 {
11632 error_at (location, "comparing vectors with different "
11633 "element types");
11634 return error_mark_node;
11635 }
11636
928686b1
RS
11637 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11638 TYPE_VECTOR_SUBPARTS (type1)))
d246ab4f
AS
11639 {
11640 error_at (location, "comparing vectors with different "
11641 "number of elements");
11642 return error_mark_node;
11643 }
11644
fa74a4bc
MP
11645 /* It's not precisely specified how the usual arithmetic
11646 conversions apply to the vector types. Here, we use
11647 the unsigned type if one of the operands is signed and
11648 the other one is unsigned. */
11649 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11650 {
11651 if (!TYPE_UNSIGNED (type0))
11652 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11653 else
11654 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11655 warning_at (location, OPT_Wsign_compare, "comparison between "
11656 "types %qT and %qT", type0, type1);
11657 }
11658
d246ab4f
AS
11659 /* Always construct signed integer vector type. */
11660 intt = c_common_type_for_size (GET_MODE_BITSIZE
b397965c
RS
11661 (SCALAR_TYPE_MODE
11662 (TREE_TYPE (type0))), 0);
aa1c9429
JJ
11663 if (!intt)
11664 {
11665 error_at (location, "could not find an integer type "
11666 "of the same size as %qT",
11667 TREE_TYPE (type0));
11668 return error_mark_node;
11669 }
d246ab4f
AS
11670 result_type = build_opaque_vector_type (intt,
11671 TYPE_VECTOR_SUBPARTS (type0));
11672 converted = 1;
9f47c7e5
IE
11673 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11674 goto return_build_binary_op;
d246ab4f 11675 }
ae311566 11676 if (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1))
ba47d38d
AH
11677 warning_at (location,
11678 OPT_Wfloat_equal,
11679 "comparing floating point with == or != is unsafe");
3e4093b6
RS
11680 /* Result of comparison is always int,
11681 but don't convert the args to int! */
11682 build_type = integer_type_node;
11683 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
ab22c1fa 11684 || code0 == FIXED_POINT_TYPE || code0 == COMPLEX_TYPE)
3e4093b6 11685 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
ab22c1fa 11686 || code1 == FIXED_POINT_TYPE || code1 == COMPLEX_TYPE))
3e4093b6 11687 short_compare = 1;
637f1455
SZ
11688 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
11689 {
11690 if (TREE_CODE (op0) == ADDR_EXPR
de55efd5
MP
11691 && decl_with_nonnull_addr_p (TREE_OPERAND (op0, 0))
11692 && !from_macro_expansion_at (location))
637f1455
SZ
11693 {
11694 if (code == EQ_EXPR)
11695 warning_at (location,
11696 OPT_Waddress,
11697 "the comparison will always evaluate as %<false%> "
11698 "for the address of %qD will never be NULL",
11699 TREE_OPERAND (op0, 0));
11700 else
11701 warning_at (location,
11702 OPT_Waddress,
11703 "the comparison will always evaluate as %<true%> "
11704 "for the address of %qD will never be NULL",
11705 TREE_OPERAND (op0, 0));
11706 }
11707 result_type = type0;
11708 }
11709 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
11710 {
11711 if (TREE_CODE (op1) == ADDR_EXPR
de55efd5
MP
11712 && decl_with_nonnull_addr_p (TREE_OPERAND (op1, 0))
11713 && !from_macro_expansion_at (location))
637f1455
SZ
11714 {
11715 if (code == EQ_EXPR)
11716 warning_at (location,
f90e8e2e 11717 OPT_Waddress,
637f1455
SZ
11718 "the comparison will always evaluate as %<false%> "
11719 "for the address of %qD will never be NULL",
11720 TREE_OPERAND (op1, 0));
11721 else
11722 warning_at (location,
11723 OPT_Waddress,
11724 "the comparison will always evaluate as %<true%> "
11725 "for the address of %qD will never be NULL",
11726 TREE_OPERAND (op1, 0));
11727 }
11728 result_type = type1;
11729 }
3e4093b6
RS
11730 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11731 {
11732 tree tt0 = TREE_TYPE (type0);
11733 tree tt1 = TREE_TYPE (type1);
36c5e70a
BE
11734 addr_space_t as0 = TYPE_ADDR_SPACE (tt0);
11735 addr_space_t as1 = TYPE_ADDR_SPACE (tt1);
11736 addr_space_t as_common = ADDR_SPACE_GENERIC;
11737
3e4093b6
RS
11738 /* Anything compares with void *. void * compares with anything.
11739 Otherwise, the targets must be compatible
11740 and both must be object or both incomplete. */
744aa42f 11741 if (comp_target_types (location, type0, type1))
10bc1b1b 11742 result_type = common_pointer_type (type0, type1);
36c5e70a
BE
11743 else if (!addr_space_superset (as0, as1, &as_common))
11744 {
11745 error_at (location, "comparison of pointers to "
11746 "disjoint address spaces");
11747 return error_mark_node;
11748 }
267bac10 11749 else if (VOID_TYPE_P (tt0) && !TYPE_ATOMIC (tt0))
ee2990e7 11750 {
36c5e70a 11751 if (pedantic && TREE_CODE (tt1) == FUNCTION_TYPE)
c1771a20 11752 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11753 "comparison of %<void *%> with function pointer");
ee2990e7 11754 }
267bac10 11755 else if (VOID_TYPE_P (tt1) && !TYPE_ATOMIC (tt1))
e6834654 11756 {
36c5e70a 11757 if (pedantic && TREE_CODE (tt0) == FUNCTION_TYPE)
c1771a20 11758 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11759 "comparison of %<void *%> with function pointer");
e6834654 11760 }
3e4093b6 11761 else
58393038
ZL
11762 /* Avoid warning about the volatile ObjC EH puts on decls. */
11763 if (!objc_ok)
ba47d38d 11764 pedwarn (location, 0,
509c9d60 11765 "comparison of distinct pointer types lacks a cast");
e6834654 11766
3e4093b6 11767 if (result_type == NULL_TREE)
36c5e70a
BE
11768 {
11769 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11770 result_type = build_pointer_type
11771 (build_qualified_type (void_type_node, qual));
11772 }
e6834654 11773 }
3e4093b6 11774 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
de520661 11775 {
3e4093b6 11776 result_type = type0;
ba47d38d 11777 pedwarn (location, 0, "comparison between pointer and integer");
de520661 11778 }
3e4093b6 11779 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
8b6a5902 11780 {
3e4093b6 11781 result_type = type1;
ba47d38d 11782 pedwarn (location, 0, "comparison between pointer and integer");
8b6a5902 11783 }
04159acf
MP
11784 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11785 || truth_value_p (TREE_CODE (orig_op0)))
11786 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11787 || truth_value_p (TREE_CODE (orig_op1))))
11788 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11789 break;
8b6a5902 11790
3e4093b6
RS
11791 case LE_EXPR:
11792 case GE_EXPR:
11793 case LT_EXPR:
11794 case GT_EXPR:
d246ab4f
AS
11795 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE)
11796 {
11797 tree intt;
0af94e6f 11798 if (!vector_types_compatible_elements_p (type0, type1))
d246ab4f
AS
11799 {
11800 error_at (location, "comparing vectors with different "
11801 "element types");
11802 return error_mark_node;
11803 }
11804
928686b1
RS
11805 if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
11806 TYPE_VECTOR_SUBPARTS (type1)))
d246ab4f
AS
11807 {
11808 error_at (location, "comparing vectors with different "
11809 "number of elements");
11810 return error_mark_node;
11811 }
11812
fa74a4bc
MP
11813 /* It's not precisely specified how the usual arithmetic
11814 conversions apply to the vector types. Here, we use
11815 the unsigned type if one of the operands is signed and
11816 the other one is unsigned. */
11817 if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
11818 {
11819 if (!TYPE_UNSIGNED (type0))
11820 op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
11821 else
11822 op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
11823 warning_at (location, OPT_Wsign_compare, "comparison between "
11824 "types %qT and %qT", type0, type1);
11825 }
11826
d246ab4f
AS
11827 /* Always construct signed integer vector type. */
11828 intt = c_common_type_for_size (GET_MODE_BITSIZE
b397965c
RS
11829 (SCALAR_TYPE_MODE
11830 (TREE_TYPE (type0))), 0);
aa1c9429
JJ
11831 if (!intt)
11832 {
11833 error_at (location, "could not find an integer type "
11834 "of the same size as %qT",
11835 TREE_TYPE (type0));
11836 return error_mark_node;
11837 }
d246ab4f
AS
11838 result_type = build_opaque_vector_type (intt,
11839 TYPE_VECTOR_SUBPARTS (type0));
11840 converted = 1;
9f47c7e5
IE
11841 ret = build_vec_cmp (resultcode, result_type, op0, op1);
11842 goto return_build_binary_op;
d246ab4f 11843 }
3e4093b6 11844 build_type = integer_type_node;
ab22c1fa
CF
11845 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
11846 || code0 == FIXED_POINT_TYPE)
11847 && (code1 == INTEGER_TYPE || code1 == REAL_TYPE
11848 || code1 == FIXED_POINT_TYPE))
3e4093b6
RS
11849 short_compare = 1;
11850 else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
11851 {
36c5e70a
BE
11852 addr_space_t as0 = TYPE_ADDR_SPACE (TREE_TYPE (type0));
11853 addr_space_t as1 = TYPE_ADDR_SPACE (TREE_TYPE (type1));
11854 addr_space_t as_common;
11855
744aa42f 11856 if (comp_target_types (location, type0, type1))
3e4093b6 11857 {
10bc1b1b 11858 result_type = common_pointer_type (type0, type1);
3e4093b6
RS
11859 if (!COMPLETE_TYPE_P (TREE_TYPE (type0))
11860 != !COMPLETE_TYPE_P (TREE_TYPE (type1)))
ba47d38d 11861 pedwarn (location, 0,
509c9d60 11862 "comparison of complete and incomplete pointers");
fcf73884 11863 else if (TREE_CODE (TREE_TYPE (type0)) == FUNCTION_TYPE)
c1771a20 11864 pedwarn (location, OPT_Wpedantic, "ISO C forbids "
fcf73884 11865 "ordered comparisons of pointers to functions");
d42ba3b8
SZ
11866 else if (null_pointer_constant_p (orig_op0)
11867 || null_pointer_constant_p (orig_op1))
11868 warning_at (location, OPT_Wextra,
11869 "ordered comparison of pointer with null pointer");
11870
3e4093b6 11871 }
36c5e70a
BE
11872 else if (!addr_space_superset (as0, as1, &as_common))
11873 {
11874 error_at (location, "comparison of pointers to "
11875 "disjoint address spaces");
11876 return error_mark_node;
11877 }
3e4093b6
RS
11878 else
11879 {
36c5e70a
BE
11880 int qual = ENCODE_QUAL_ADDR_SPACE (as_common);
11881 result_type = build_pointer_type
11882 (build_qualified_type (void_type_node, qual));
ba47d38d 11883 pedwarn (location, 0,
509c9d60 11884 "comparison of distinct pointer types lacks a cast");
3e4093b6
RS
11885 }
11886 }
6aa3c60d 11887 else if (code0 == POINTER_TYPE && null_pointer_constant_p (orig_op1))
3e4093b6
RS
11888 {
11889 result_type = type0;
fcf73884 11890 if (pedantic)
c1771a20 11891 pedwarn (location, OPT_Wpedantic,
fcf73884
MLI
11892 "ordered comparison of pointer with integer zero");
11893 else if (extra_warnings)
ba47d38d 11894 warning_at (location, OPT_Wextra,
d42ba3b8 11895 "ordered comparison of pointer with integer zero");
3e4093b6 11896 }
6aa3c60d 11897 else if (code1 == POINTER_TYPE && null_pointer_constant_p (orig_op0))
3e4093b6
RS
11898 {
11899 result_type = type1;
d42ba3b8 11900 if (pedantic)
c1771a20 11901 pedwarn (location, OPT_Wpedantic,
d42ba3b8
SZ
11902 "ordered comparison of pointer with integer zero");
11903 else if (extra_warnings)
11904 warning_at (location, OPT_Wextra,
11905 "ordered comparison of pointer with integer zero");
3e4093b6
RS
11906 }
11907 else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
11908 {
11909 result_type = type0;
ba47d38d 11910 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6
RS
11911 }
11912 else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
11913 {
11914 result_type = type1;
ba47d38d 11915 pedwarn (location, 0, "comparison between pointer and integer");
3e4093b6 11916 }
05abad4c
ML
11917
11918 if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
11919 && sanitize_flags_p (SANITIZE_POINTER_COMPARE))
11920 {
11921 op0 = save_expr (op0);
11922 op1 = save_expr (op1);
11923
11924 tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
11925 instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
11926 }
11927
04159acf
MP
11928 if ((TREE_CODE (TREE_TYPE (orig_op0)) == BOOLEAN_TYPE
11929 || truth_value_p (TREE_CODE (orig_op0)))
11930 ^ (TREE_CODE (TREE_TYPE (orig_op1)) == BOOLEAN_TYPE
11931 || truth_value_p (TREE_CODE (orig_op1))))
11932 maybe_warn_bool_compare (location, code, orig_op0, orig_op1);
3e4093b6 11933 break;
64094f6a 11934
3e4093b6 11935 default:
37b2f290 11936 gcc_unreachable ();
c9fe6f9f 11937 }
8f17b5c5 11938
e57e265b
PB
11939 if (code0 == ERROR_MARK || code1 == ERROR_MARK)
11940 return error_mark_node;
11941
5bed876a
AH
11942 if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
11943 && (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
0af94e6f 11944 || !vector_types_compatible_elements_p (type0, type1)))
5bed876a 11945 {
745e411d 11946 gcc_rich_location richloc (location);
96e6ae57
DM
11947 maybe_range_label_for_tree_type_mismatch
11948 label_for_op0 (orig_op0, orig_op1),
11949 label_for_op1 (orig_op1, orig_op0);
11950 richloc.maybe_add_expr (orig_op0, &label_for_op0);
11951 richloc.maybe_add_expr (orig_op1, &label_for_op1);
745e411d 11952 binary_op_error (&richloc, code, type0, type1);
5bed876a
AH
11953 return error_mark_node;
11954 }
11955
3e4093b6 11956 if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
ab22c1fa 11957 || code0 == FIXED_POINT_TYPE || code0 == VECTOR_TYPE)
3e4093b6
RS
11958 &&
11959 (code1 == INTEGER_TYPE || code1 == REAL_TYPE || code1 == COMPLEX_TYPE
ab22c1fa 11960 || code1 == FIXED_POINT_TYPE || code1 == VECTOR_TYPE))
400fbf9f 11961 {
2ca862e9
JM
11962 bool first_complex = (code0 == COMPLEX_TYPE);
11963 bool second_complex = (code1 == COMPLEX_TYPE);
11964 int none_complex = (!first_complex && !second_complex);
39b726dd 11965
3e4093b6 11966 if (shorten || common || short_compare)
3bf6bfcc
JJ
11967 {
11968 result_type = c_common_type (type0, type1);
0a0b3574
MM
11969 do_warn_double_promotion (result_type, type0, type1,
11970 "implicit conversion from %qT to %qT "
11971 "to match other operand of binary "
11972 "expression",
11973 location);
3bf6bfcc
JJ
11974 if (result_type == error_mark_node)
11975 return error_mark_node;
11976 }
400fbf9f 11977
2ca862e9
JM
11978 if (first_complex != second_complex
11979 && (code == PLUS_EXPR
11980 || code == MINUS_EXPR
11981 || code == MULT_EXPR
11982 || (code == TRUNC_DIV_EXPR && first_complex))
11983 && TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
11984 && flag_signed_zeros)
11985 {
11986 /* An operation on mixed real/complex operands must be
11987 handled specially, but the language-independent code can
11988 more easily optimize the plain complex arithmetic if
11989 -fno-signed-zeros. */
11990 tree real_type = TREE_TYPE (result_type);
11991 tree real, imag;
11992 if (type0 != orig_type0 || type1 != orig_type1)
11993 {
11994 gcc_assert (may_need_excess_precision && common);
2d2e923f 11995 semantic_result_type = c_common_type (orig_type0, orig_type1);
2ca862e9
JM
11996 }
11997 if (first_complex)
11998 {
11999 if (TREE_TYPE (op0) != result_type)
68fca595 12000 op0 = convert_and_check (location, result_type, op0);
2ca862e9 12001 if (TREE_TYPE (op1) != real_type)
68fca595 12002 op1 = convert_and_check (location, real_type, op1);
2ca862e9
JM
12003 }
12004 else
12005 {
12006 if (TREE_TYPE (op0) != real_type)
68fca595 12007 op0 = convert_and_check (location, real_type, op0);
2ca862e9 12008 if (TREE_TYPE (op1) != result_type)
68fca595 12009 op1 = convert_and_check (location, result_type, op1);
2ca862e9
JM
12010 }
12011 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12012 return error_mark_node;
12013 if (first_complex)
12014 {
b2fa0a8b 12015 op0 = save_expr (op0);
2ca862e9 12016 real = build_unary_op (EXPR_LOCATION (orig_op0), REALPART_EXPR,
e51fbec3 12017 op0, true);
2ca862e9 12018 imag = build_unary_op (EXPR_LOCATION (orig_op0), IMAGPART_EXPR,
e51fbec3 12019 op0, true);
2ca862e9
JM
12020 switch (code)
12021 {
12022 case MULT_EXPR:
12023 case TRUNC_DIV_EXPR:
b2fa0a8b 12024 op1 = save_expr (op1);
2ca862e9
JM
12025 imag = build2 (resultcode, real_type, imag, op1);
12026 /* Fall through. */
12027 case PLUS_EXPR:
12028 case MINUS_EXPR:
12029 real = build2 (resultcode, real_type, real, op1);
12030 break;
12031 default:
12032 gcc_unreachable();
12033 }
12034 }
12035 else
12036 {
b2fa0a8b 12037 op1 = save_expr (op1);
2ca862e9 12038 real = build_unary_op (EXPR_LOCATION (orig_op1), REALPART_EXPR,
e51fbec3 12039 op1, true);
2ca862e9 12040 imag = build_unary_op (EXPR_LOCATION (orig_op1), IMAGPART_EXPR,
e51fbec3 12041 op1, true);
2ca862e9
JM
12042 switch (code)
12043 {
12044 case MULT_EXPR:
b2fa0a8b 12045 op0 = save_expr (op0);
2ca862e9
JM
12046 imag = build2 (resultcode, real_type, op0, imag);
12047 /* Fall through. */
12048 case PLUS_EXPR:
12049 real = build2 (resultcode, real_type, op0, real);
12050 break;
12051 case MINUS_EXPR:
12052 real = build2 (resultcode, real_type, op0, real);
12053 imag = build1 (NEGATE_EXPR, real_type, imag);
12054 break;
12055 default:
12056 gcc_unreachable();
12057 }
12058 }
12059 ret = build2 (COMPLEX_EXPR, result_type, real, imag);
12060 goto return_build_binary_op;
12061 }
12062
3e4093b6
RS
12063 /* For certain operations (which identify themselves by shorten != 0)
12064 if both args were extended from the same smaller type,
12065 do the arithmetic in that type and then extend.
400fbf9f 12066
3e4093b6
RS
12067 shorten !=0 and !=1 indicates a bitwise operation.
12068 For them, this optimization is safe only if
12069 both args are zero-extended or both are sign-extended.
12070 Otherwise, we might change the result.
12071 Eg, (short)-1 | (unsigned short)-1 is (int)-1
12072 but calculated in (unsigned short) it would be (unsigned short)-1. */
400fbf9f 12073
3e4093b6
RS
12074 if (shorten && none_complex)
12075 {
3e4093b6 12076 final_type = result_type;
b8698a0f 12077 result_type = shorten_binary_op (result_type, op0, op1,
6715192c 12078 shorten == -1);
3e4093b6 12079 }
88a3dbc1 12080
3e4093b6 12081 /* Shifts can be shortened if shifting right. */
2f6e4e97 12082
3e4093b6
RS
12083 if (short_shift)
12084 {
12085 int unsigned_arg;
12086 tree arg0 = get_narrower (op0, &unsigned_arg);
88a3dbc1 12087
3e4093b6 12088 final_type = result_type;
abe80e6d 12089
3e4093b6 12090 if (arg0 == op0 && final_type == TREE_TYPE (op0))
8df83eae 12091 unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
e9a25f70 12092
3e4093b6 12093 if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
be123439 12094 && tree_int_cst_sgn (op1) > 0
3e4093b6
RS
12095 /* We can shorten only if the shift count is less than the
12096 number of bits in the smaller type size. */
12097 && compare_tree_int (op1, TYPE_PRECISION (TREE_TYPE (arg0))) < 0
12098 /* We cannot drop an unsigned shift after sign-extension. */
8df83eae 12099 && (!TYPE_UNSIGNED (final_type) || unsigned_arg))
3e4093b6
RS
12100 {
12101 /* Do an unsigned shift if the operand was zero-extended. */
12102 result_type
12103 = c_common_signed_or_unsigned_type (unsigned_arg,
12104 TREE_TYPE (arg0));
12105 /* Convert value-to-be-shifted to that type. */
12106 if (TREE_TYPE (op0) != result_type)
12107 op0 = convert (result_type, op0);
12108 converted = 1;
abe80e6d 12109 }
88a3dbc1
RK
12110 }
12111
3e4093b6
RS
12112 /* Comparison operations are shortened too but differently.
12113 They identify themselves by setting short_compare = 1. */
56cb9733 12114
3e4093b6
RS
12115 if (short_compare)
12116 {
12117 /* Don't write &op0, etc., because that would prevent op0
12118 from being kept in a register.
12119 Instead, make copies of the our local variables and
12120 pass the copies by reference, then copy them back afterward. */
12121 tree xop0 = op0, xop1 = op1, xresult_type = result_type;
12122 enum tree_code xresultcode = resultcode;
12123 tree val
393e8e8b
MP
12124 = shorten_compare (location, &xop0, &xop1, &xresult_type,
12125 &xresultcode);
8f17b5c5 12126
3fa8871b 12127 if (val != NULL_TREE)
c9f9eb5d
AH
12128 {
12129 ret = val;
12130 goto return_build_binary_op;
12131 }
8f17b5c5 12132
3e4093b6
RS
12133 op0 = xop0, op1 = xop1;
12134 converted = 1;
12135 resultcode = xresultcode;
8f17b5c5 12136
7d882b83 12137 if (c_inhibit_evaluation_warnings == 0)
928c19bb
JM
12138 {
12139 bool op0_maybe_const = true;
12140 bool op1_maybe_const = true;
12141 tree orig_op0_folded, orig_op1_folded;
12142
12143 if (in_late_binary_op)
12144 {
12145 orig_op0_folded = orig_op0;
12146 orig_op1_folded = orig_op1;
12147 }
12148 else
12149 {
12150 /* Fold for the sake of possible warnings, as in
12151 build_conditional_expr. This requires the
12152 "original" values to be folded, not just op0 and
12153 op1. */
f5178456 12154 c_inhibit_evaluation_warnings++;
928c19bb
JM
12155 op0 = c_fully_fold (op0, require_constant_value,
12156 &op0_maybe_const);
12157 op1 = c_fully_fold (op1, require_constant_value,
12158 &op1_maybe_const);
f5178456 12159 c_inhibit_evaluation_warnings--;
928c19bb
JM
12160 orig_op0_folded = c_fully_fold (orig_op0,
12161 require_constant_value,
12162 NULL);
12163 orig_op1_folded = c_fully_fold (orig_op1,
12164 require_constant_value,
12165 NULL);
12166 }
12167
12168 if (warn_sign_compare)
12169 warn_for_sign_compare (location, orig_op0_folded,
12170 orig_op1_folded, op0, op1,
12171 result_type, resultcode);
5c2f94b4 12172 if (!in_late_binary_op && !int_operands)
928c19bb
JM
12173 {
12174 if (!op0_maybe_const || TREE_CODE (op0) != INTEGER_CST)
e5a94231 12175 op0 = c_wrap_maybe_const (op0, !op0_maybe_const);
928c19bb 12176 if (!op1_maybe_const || TREE_CODE (op1) != INTEGER_CST)
e5a94231 12177 op1 = c_wrap_maybe_const (op1, !op1_maybe_const);
928c19bb 12178 }
3e4093b6 12179 }
2ad1815d 12180 }
64094f6a 12181 }
64094f6a 12182
3e4093b6
RS
12183 /* At this point, RESULT_TYPE must be nonzero to avoid an error message.
12184 If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
12185 Then the expression will be built.
12186 It will be given type FINAL_TYPE if that is nonzero;
12187 otherwise, it will be given type RESULT_TYPE. */
400fbf9f 12188
3e4093b6
RS
12189 if (!result_type)
12190 {
745e411d 12191 gcc_rich_location richloc (location);
96e6ae57
DM
12192 maybe_range_label_for_tree_type_mismatch
12193 label_for_op0 (orig_op0, orig_op1),
12194 label_for_op1 (orig_op1, orig_op0);
12195 richloc.maybe_add_expr (orig_op0, &label_for_op0);
12196 richloc.maybe_add_expr (orig_op1, &label_for_op1);
745e411d 12197 binary_op_error (&richloc, code, TREE_TYPE (op0), TREE_TYPE (op1));
3e4093b6
RS
12198 return error_mark_node;
12199 }
400fbf9f 12200
3e4093b6 12201 if (build_type == NULL_TREE)
8ce94e44
JM
12202 {
12203 build_type = result_type;
180f8dbb
JM
12204 if ((type0 != orig_type0 || type1 != orig_type1)
12205 && !boolean_op)
8ce94e44
JM
12206 {
12207 gcc_assert (may_need_excess_precision && common);
2d2e923f 12208 semantic_result_type = c_common_type (orig_type0, orig_type1);
8ce94e44
JM
12209 }
12210 }
400fbf9f 12211
2d2e923f
MLI
12212 if (!converted)
12213 {
68fca595
MP
12214 op0 = ep_convert_and_check (location, result_type, op0,
12215 semantic_result_type);
12216 op1 = ep_convert_and_check (location, result_type, op1,
12217 semantic_result_type);
2d2e923f
MLI
12218
12219 /* This can happen if one operand has a vector type, and the other
12220 has a different type. */
12221 if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
12222 return error_mark_node;
12223 }
12224
45b2222a
ML
12225 if (sanitize_flags_p ((SANITIZE_SHIFT
12226 | SANITIZE_DIVIDE | SANITIZE_FLOAT_DIVIDE))
f34ebeb2 12227 && current_function_decl != NULL_TREE
b2aaf235
MP
12228 && (doing_div_or_mod || doing_shift)
12229 && !require_constant_value)
de5a5fa1
MP
12230 {
12231 /* OP0 and/or OP1 might have side-effects. */
b2fa0a8b
MP
12232 op0 = save_expr (op0);
12233 op1 = save_expr (op1);
de5a5fa1
MP
12234 op0 = c_fully_fold (op0, false, NULL);
12235 op1 = c_fully_fold (op1, false, NULL);
45b2222a
ML
12236 if (doing_div_or_mod && (sanitize_flags_p ((SANITIZE_DIVIDE
12237 | SANITIZE_FLOAT_DIVIDE))))
de5a5fa1 12238 instrument_expr = ubsan_instrument_division (location, op0, op1);
45b2222a 12239 else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
de5a5fa1
MP
12240 instrument_expr = ubsan_instrument_shift (location, code, op0, op1);
12241 }
12242
c9f9eb5d 12243 /* Treat expressions in initializers specially as they can't trap. */
928c19bb
JM
12244 if (int_const_or_overflow)
12245 ret = (require_constant_value
db3927fb
AH
12246 ? fold_build2_initializer_loc (location, resultcode, build_type,
12247 op0, op1)
12248 : fold_build2_loc (location, resultcode, build_type, op0, op1));
928c19bb
JM
12249 else
12250 ret = build2 (resultcode, build_type, op0, op1);
3fa8871b 12251 if (final_type != NULL_TREE)
c9f9eb5d
AH
12252 ret = convert (final_type, ret);
12253
12254 return_build_binary_op:
12255 gcc_assert (ret != error_mark_node);
928c19bb
JM
12256 if (TREE_CODE (ret) == INTEGER_CST && !TREE_OVERFLOW (ret) && !int_const)
12257 ret = (int_operands
12258 ? note_integer_operands (ret)
12259 : build1 (NOP_EXPR, TREE_TYPE (ret), ret));
12260 else if (TREE_CODE (ret) != INTEGER_CST && int_operands
12261 && !in_late_binary_op)
12262 ret = note_integer_operands (ret);
c9f9eb5d 12263 protected_set_expr_location (ret, location);
de5a5fa1 12264
a24d975c 12265 if (instrument_expr != NULL)
de5a5fa1
MP
12266 ret = fold_build2 (COMPOUND_EXPR, TREE_TYPE (ret),
12267 instrument_expr, ret);
12268
4d1b8e70
JJ
12269 if (semantic_result_type)
12270 ret = build1_loc (location, EXCESS_PRECISION_EXPR,
12271 semantic_result_type, ret);
12272
c9f9eb5d 12273 return ret;
400fbf9f 12274}
85498824
JM
12275
12276
12277/* Convert EXPR to be a truth-value, validating its type for this
ba47d38d 12278 purpose. LOCATION is the source location for the expression. */
85498824
JM
12279
12280tree
ba47d38d 12281c_objc_common_truthvalue_conversion (location_t location, tree expr)
85498824 12282{
928c19bb
JM
12283 bool int_const, int_operands;
12284
85498824
JM
12285 switch (TREE_CODE (TREE_TYPE (expr)))
12286 {
12287 case ARRAY_TYPE:
ba47d38d 12288 error_at (location, "used array that cannot be converted to pointer where scalar is required");
85498824
JM
12289 return error_mark_node;
12290
12291 case RECORD_TYPE:
ba47d38d 12292 error_at (location, "used struct type value where scalar is required");
85498824
JM
12293 return error_mark_node;
12294
12295 case UNION_TYPE:
ba47d38d 12296 error_at (location, "used union type value where scalar is required");
85498824
JM
12297 return error_mark_node;
12298
04af8788
NP
12299 case VOID_TYPE:
12300 error_at (location, "void value not ignored as it ought to be");
12301 return error_mark_node;
12302
1807ffc1
MS
12303 case POINTER_TYPE:
12304 if (reject_gcc_builtin (expr))
12305 return error_mark_node;
12306 break;
12307
46bdb9cf
JM
12308 case FUNCTION_TYPE:
12309 gcc_unreachable ();
12310
d246ab4f
AS
12311 case VECTOR_TYPE:
12312 error_at (location, "used vector type where scalar is required");
12313 return error_mark_node;
12314
85498824
JM
12315 default:
12316 break;
12317 }
12318
928c19bb
JM
12319 int_const = (TREE_CODE (expr) == INTEGER_CST && !TREE_OVERFLOW (expr));
12320 int_operands = EXPR_INT_CONST_OPERANDS (expr);
a27d595d
JM
12321 if (int_operands && TREE_CODE (expr) != INTEGER_CST)
12322 {
12323 expr = remove_c_maybe_const_expr (expr);
12324 expr = build2 (NE_EXPR, integer_type_node, expr,
12325 convert (TREE_TYPE (expr), integer_zero_node));
12326 expr = note_integer_operands (expr);
12327 }
12328 else
12329 /* ??? Should we also give an error for vectors rather than leaving
12330 those to give errors later? */
12331 expr = c_common_truthvalue_conversion (location, expr);
928c19bb
JM
12332
12333 if (TREE_CODE (expr) == INTEGER_CST && int_operands && !int_const)
12334 {
12335 if (TREE_OVERFLOW (expr))
12336 return expr;
12337 else
12338 return note_integer_operands (expr);
12339 }
12340 if (TREE_CODE (expr) == INTEGER_CST && !int_const)
12341 return build1 (NOP_EXPR, TREE_TYPE (expr), expr);
12342 return expr;
85498824 12343}
73f397d4
JM
12344\f
12345
12346/* Convert EXPR to a contained DECL, updating *TC, *TI and *SE as
12347 required. */
12348
12349tree
51eed280 12350c_expr_to_decl (tree expr, bool *tc ATTRIBUTE_UNUSED, bool *se)
73f397d4
JM
12351{
12352 if (TREE_CODE (expr) == COMPOUND_LITERAL_EXPR)
12353 {
12354 tree decl = COMPOUND_LITERAL_EXPR_DECL (expr);
12355 /* Executing a compound literal inside a function reinitializes
12356 it. */
12357 if (!TREE_STATIC (decl))
12358 *se = true;
12359 return decl;
12360 }
12361 else
12362 return expr;
12363}
953ff289 12364\f
88bae6f4
TS
12365/* Generate OMP construct CODE, with BODY and CLAUSES as its compound
12366 statement. LOC is the location of the construct. */
41dbbb37
TS
12367
12368tree
88bae6f4
TS
12369c_finish_omp_construct (location_t loc, enum tree_code code, tree body,
12370 tree clauses)
41dbbb37 12371{
88bae6f4 12372 body = c_end_compound_stmt (loc, body, true);
41dbbb37 12373
88bae6f4 12374 tree stmt = make_node (code);
41dbbb37 12375 TREE_TYPE (stmt) = void_type_node;
88bae6f4
TS
12376 OMP_BODY (stmt) = body;
12377 OMP_CLAUSES (stmt) = clauses;
41dbbb37
TS
12378 SET_EXPR_LOCATION (stmt, loc);
12379
12380 return add_stmt (stmt);
12381}
12382
12383/* Generate OACC_DATA, with CLAUSES and BLOCK as its compound
12384 statement. LOC is the location of the OACC_DATA. */
12385
12386tree
12387c_finish_oacc_data (location_t loc, tree clauses, tree block)
12388{
12389 tree stmt;
12390
12391 block = c_end_compound_stmt (loc, block, true);
12392
12393 stmt = make_node (OACC_DATA);
12394 TREE_TYPE (stmt) = void_type_node;
12395 OACC_DATA_CLAUSES (stmt) = clauses;
12396 OACC_DATA_BODY (stmt) = block;
12397 SET_EXPR_LOCATION (stmt, loc);
12398
12399 return add_stmt (stmt);
12400}
12401
37d5ad46
JB
12402/* Generate OACC_HOST_DATA, with CLAUSES and BLOCK as its compound
12403 statement. LOC is the location of the OACC_HOST_DATA. */
12404
12405tree
12406c_finish_oacc_host_data (location_t loc, tree clauses, tree block)
12407{
12408 tree stmt;
12409
12410 block = c_end_compound_stmt (loc, block, true);
12411
12412 stmt = make_node (OACC_HOST_DATA);
12413 TREE_TYPE (stmt) = void_type_node;
12414 OACC_HOST_DATA_CLAUSES (stmt) = clauses;
12415 OACC_HOST_DATA_BODY (stmt) = block;
12416 SET_EXPR_LOCATION (stmt, loc);
12417
12418 return add_stmt (stmt);
12419}
12420
c0220ea4 12421/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
953ff289
DN
12422
12423tree
12424c_begin_omp_parallel (void)
12425{
12426 tree block;
12427
12428 keep_next_level ();
12429 block = c_begin_compound_stmt (true);
12430
12431 return block;
12432}
12433
c2255bc4
AH
12434/* Generate OMP_PARALLEL, with CLAUSES and BLOCK as its compound
12435 statement. LOC is the location of the OMP_PARALLEL. */
a68ab351 12436
953ff289 12437tree
c2255bc4 12438c_finish_omp_parallel (location_t loc, tree clauses, tree block)
953ff289
DN
12439{
12440 tree stmt;
12441
c2255bc4 12442 block = c_end_compound_stmt (loc, block, true);
953ff289
DN
12443
12444 stmt = make_node (OMP_PARALLEL);
12445 TREE_TYPE (stmt) = void_type_node;
12446 OMP_PARALLEL_CLAUSES (stmt) = clauses;
12447 OMP_PARALLEL_BODY (stmt) = block;
c2255bc4 12448 SET_EXPR_LOCATION (stmt, loc);
953ff289
DN
12449
12450 return add_stmt (stmt);
12451}
12452
a68ab351
JJ
12453/* Like c_begin_compound_stmt, except force the retention of the BLOCK. */
12454
12455tree
12456c_begin_omp_task (void)
12457{
12458 tree block;
12459
12460 keep_next_level ();
12461 block = c_begin_compound_stmt (true);
12462
12463 return block;
12464}
12465
c2255bc4
AH
12466/* Generate OMP_TASK, with CLAUSES and BLOCK as its compound
12467 statement. LOC is the location of the #pragma. */
a68ab351
JJ
12468
12469tree
c2255bc4 12470c_finish_omp_task (location_t loc, tree clauses, tree block)
a68ab351
JJ
12471{
12472 tree stmt;
12473
c2255bc4 12474 block = c_end_compound_stmt (loc, block, true);
a68ab351
JJ
12475
12476 stmt = make_node (OMP_TASK);
12477 TREE_TYPE (stmt) = void_type_node;
12478 OMP_TASK_CLAUSES (stmt) = clauses;
12479 OMP_TASK_BODY (stmt) = block;
c2255bc4 12480 SET_EXPR_LOCATION (stmt, loc);
a68ab351
JJ
12481
12482 return add_stmt (stmt);
12483}
12484
acf0174b
JJ
12485/* Generate GOMP_cancel call for #pragma omp cancel. */
12486
12487void
12488c_finish_omp_cancel (location_t loc, tree clauses)
12489{
12490 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCEL);
12491 int mask = 0;
629b3d75 12492 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12493 mask = 1;
629b3d75 12494 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12495 mask = 2;
629b3d75 12496 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12497 mask = 4;
629b3d75 12498 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12499 mask = 8;
12500 else
12501 {
54d19c3b 12502 error_at (loc, "%<#pragma omp cancel%> must specify one of "
acf0174b
JJ
12503 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12504 "clauses");
12505 return;
12506 }
629b3d75 12507 tree ifc = omp_find_clause (clauses, OMP_CLAUSE_IF);
acf0174b
JJ
12508 if (ifc != NULL_TREE)
12509 {
12510 tree type = TREE_TYPE (OMP_CLAUSE_IF_EXPR (ifc));
12511 ifc = fold_build2_loc (OMP_CLAUSE_LOCATION (ifc), NE_EXPR,
12512 boolean_type_node, OMP_CLAUSE_IF_EXPR (ifc),
12513 build_zero_cst (type));
12514 }
12515 else
12516 ifc = boolean_true_node;
12517 tree stmt = build_call_expr_loc (loc, fn, 2,
12518 build_int_cst (integer_type_node, mask),
12519 ifc);
12520 add_stmt (stmt);
12521}
12522
12523/* Generate GOMP_cancellation_point call for
12524 #pragma omp cancellation point. */
12525
12526void
12527c_finish_omp_cancellation_point (location_t loc, tree clauses)
12528{
12529 tree fn = builtin_decl_explicit (BUILT_IN_GOMP_CANCELLATION_POINT);
12530 int mask = 0;
629b3d75 12531 if (omp_find_clause (clauses, OMP_CLAUSE_PARALLEL))
acf0174b 12532 mask = 1;
629b3d75 12533 else if (omp_find_clause (clauses, OMP_CLAUSE_FOR))
acf0174b 12534 mask = 2;
629b3d75 12535 else if (omp_find_clause (clauses, OMP_CLAUSE_SECTIONS))
acf0174b 12536 mask = 4;
629b3d75 12537 else if (omp_find_clause (clauses, OMP_CLAUSE_TASKGROUP))
acf0174b
JJ
12538 mask = 8;
12539 else
12540 {
54d19c3b 12541 error_at (loc, "%<#pragma omp cancellation point%> must specify one of "
acf0174b
JJ
12542 "%<parallel%>, %<for%>, %<sections%> or %<taskgroup%> "
12543 "clauses");
12544 return;
12545 }
12546 tree stmt = build_call_expr_loc (loc, fn, 1,
12547 build_int_cst (integer_type_node, mask));
12548 add_stmt (stmt);
12549}
12550
12551/* Helper function for handle_omp_array_sections. Called recursively
12552 to handle multiple array-section-subscripts. C is the clause,
12553 T current expression (initially OMP_CLAUSE_DECL), which is either
12554 a TREE_LIST for array-section-subscript (TREE_PURPOSE is low-bound
12555 expression if specified, TREE_VALUE length expression if specified,
12556 TREE_CHAIN is what it has been specified after, or some decl.
12557 TYPES vector is populated with array section types, MAYBE_ZERO_LEN
12558 set to true if any of the array-section-subscript could have length
12559 of zero (explicit or implicit), FIRST_NON_ONE is the index of the
12560 first array-section-subscript which is known not to have length
12561 of one. Given say:
12562 map(a[:b][2:1][:c][:2][:d][e:f][2:5])
12563 FIRST_NON_ONE will be 3, array-section-subscript [:b], [2:1] and [:c]
12564 all are or may have length of 1, array-section-subscript [:2] is the
d9a6bd32 12565 first one known not to have length 1. For array-section-subscript
acf0174b
JJ
12566 <= FIRST_NON_ONE we diagnose non-contiguous arrays if low bound isn't
12567 0 or length isn't the array domain max + 1, for > FIRST_NON_ONE we
12568 can if MAYBE_ZERO_LEN is false. MAYBE_ZERO_LEN will be true in the above
12569 case though, as some lengths could be zero. */
12570
12571static tree
12572handle_omp_array_sections_1 (tree c, tree t, vec<tree> &types,
d9a6bd32 12573 bool &maybe_zero_len, unsigned int &first_non_one,
e46c7770 12574 enum c_omp_region_type ort)
acf0174b
JJ
12575{
12576 tree ret, low_bound, length, type;
12577 if (TREE_CODE (t) != TREE_LIST)
12578 {
7a0ca710 12579 if (error_operand_p (t))
acf0174b 12580 return error_mark_node;
d9a6bd32 12581 ret = t;
9dc5773f
JJ
12582 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12583 && TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
12584 {
12585 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qE in %qs clause",
12586 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12587 return error_mark_node;
12588 }
d9a6bd32 12589 if (TREE_CODE (t) == COMPONENT_REF
e46c7770 12590 && ort == C_ORT_OMP
d9a6bd32
JJ
12591 && (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
12592 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO
12593 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FROM))
12594 {
12595 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
12596 {
12597 error_at (OMP_CLAUSE_LOCATION (c),
12598 "bit-field %qE in %qs clause",
12599 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12600 return error_mark_node;
12601 }
12602 while (TREE_CODE (t) == COMPONENT_REF)
12603 {
12604 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
12605 {
12606 error_at (OMP_CLAUSE_LOCATION (c),
12607 "%qE is a member of a union", t);
12608 return error_mark_node;
12609 }
12610 t = TREE_OPERAND (t, 0);
12611 }
12612 }
0ae9bd27 12613 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
12614 {
12615 if (DECL_P (t))
12616 error_at (OMP_CLAUSE_LOCATION (c),
12617 "%qD is not a variable in %qs clause", t,
12618 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12619 else
12620 error_at (OMP_CLAUSE_LOCATION (c),
12621 "%qE is not a variable in %qs clause", t,
12622 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12623 return error_mark_node;
12624 }
12625 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
9dc5773f
JJ
12626 && TYPE_ATOMIC (TREE_TYPE (t)))
12627 {
12628 error_at (OMP_CLAUSE_LOCATION (c), "%<_Atomic%> %qD in %qs clause",
12629 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12630 return error_mark_node;
12631 }
12632 else if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12633 && VAR_P (t)
12634 && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
12635 {
12636 error_at (OMP_CLAUSE_LOCATION (c),
12637 "%qD is threadprivate variable in %qs clause", t,
12638 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12639 return error_mark_node;
12640 }
9dc5773f
JJ
12641 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12642 && TYPE_ATOMIC (TREE_TYPE (t))
12643 && POINTER_TYPE_P (TREE_TYPE (t)))
12644 {
12645 /* If the array section is pointer based and the pointer
12646 itself is _Atomic qualified, we need to atomically load
12647 the pointer. */
12648 c_expr expr;
12649 memset (&expr, 0, sizeof (expr));
12650 expr.value = ret;
12651 expr = convert_lvalue_to_rvalue (OMP_CLAUSE_LOCATION (c),
12652 expr, false, false);
12653 ret = expr.value;
12654 }
d9a6bd32 12655 return ret;
acf0174b
JJ
12656 }
12657
12658 ret = handle_omp_array_sections_1 (c, TREE_CHAIN (t), types,
e46c7770 12659 maybe_zero_len, first_non_one, ort);
acf0174b
JJ
12660 if (ret == error_mark_node || ret == NULL_TREE)
12661 return ret;
12662
12663 type = TREE_TYPE (ret);
12664 low_bound = TREE_PURPOSE (t);
12665 length = TREE_VALUE (t);
12666
12667 if (low_bound == error_mark_node || length == error_mark_node)
12668 return error_mark_node;
12669
12670 if (low_bound && !INTEGRAL_TYPE_P (TREE_TYPE (low_bound)))
12671 {
12672 error_at (OMP_CLAUSE_LOCATION (c),
12673 "low bound %qE of array section does not have integral type",
12674 low_bound);
12675 return error_mark_node;
12676 }
12677 if (length && !INTEGRAL_TYPE_P (TREE_TYPE (length)))
12678 {
12679 error_at (OMP_CLAUSE_LOCATION (c),
12680 "length %qE of array section does not have integral type",
12681 length);
12682 return error_mark_node;
12683 }
12684 if (low_bound
12685 && TREE_CODE (low_bound) == INTEGER_CST
12686 && TYPE_PRECISION (TREE_TYPE (low_bound))
12687 > TYPE_PRECISION (sizetype))
12688 low_bound = fold_convert (sizetype, low_bound);
12689 if (length
12690 && TREE_CODE (length) == INTEGER_CST
12691 && TYPE_PRECISION (TREE_TYPE (length))
12692 > TYPE_PRECISION (sizetype))
12693 length = fold_convert (sizetype, length);
12694 if (low_bound == NULL_TREE)
12695 low_bound = integer_zero_node;
12696
12697 if (length != NULL_TREE)
12698 {
12699 if (!integer_nonzerop (length))
d9a6bd32
JJ
12700 {
12701 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12702 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12703 {
12704 if (integer_zerop (length))
12705 {
12706 error_at (OMP_CLAUSE_LOCATION (c),
12707 "zero length array section in %qs clause",
12708 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12709 return error_mark_node;
12710 }
12711 }
12712 else
12713 maybe_zero_len = true;
12714 }
acf0174b
JJ
12715 if (first_non_one == types.length ()
12716 && (TREE_CODE (length) != INTEGER_CST || integer_onep (length)))
12717 first_non_one++;
12718 }
12719 if (TREE_CODE (type) == ARRAY_TYPE)
12720 {
12721 if (length == NULL_TREE
12722 && (TYPE_DOMAIN (type) == NULL_TREE
12723 || TYPE_MAX_VALUE (TYPE_DOMAIN (type)) == NULL_TREE))
12724 {
12725 error_at (OMP_CLAUSE_LOCATION (c),
12726 "for unknown bound array type length expression must "
12727 "be specified");
12728 return error_mark_node;
12729 }
12730 if (TREE_CODE (low_bound) == INTEGER_CST
12731 && tree_int_cst_sgn (low_bound) == -1)
12732 {
12733 error_at (OMP_CLAUSE_LOCATION (c),
12734 "negative low bound in array section in %qs clause",
12735 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12736 return error_mark_node;
12737 }
12738 if (length != NULL_TREE
12739 && TREE_CODE (length) == INTEGER_CST
12740 && tree_int_cst_sgn (length) == -1)
12741 {
12742 error_at (OMP_CLAUSE_LOCATION (c),
12743 "negative length in array section in %qs clause",
12744 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12745 return error_mark_node;
12746 }
12747 if (TYPE_DOMAIN (type)
12748 && TYPE_MAX_VALUE (TYPE_DOMAIN (type))
12749 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
12750 == INTEGER_CST)
12751 {
8ab7005b
JJ
12752 tree size
12753 = fold_convert (sizetype, TYPE_MAX_VALUE (TYPE_DOMAIN (type)));
12754 size = size_binop (PLUS_EXPR, size, size_one_node);
acf0174b
JJ
12755 if (TREE_CODE (low_bound) == INTEGER_CST)
12756 {
12757 if (tree_int_cst_lt (size, low_bound))
12758 {
12759 error_at (OMP_CLAUSE_LOCATION (c),
12760 "low bound %qE above array section size "
12761 "in %qs clause", low_bound,
12762 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12763 return error_mark_node;
12764 }
12765 if (tree_int_cst_equal (size, low_bound))
d9a6bd32
JJ
12766 {
12767 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND
12768 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
12769 {
12770 error_at (OMP_CLAUSE_LOCATION (c),
12771 "zero length array section in %qs clause",
12772 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12773 return error_mark_node;
12774 }
12775 maybe_zero_len = true;
12776 }
acf0174b
JJ
12777 else if (length == NULL_TREE
12778 && first_non_one == types.length ()
12779 && tree_int_cst_equal
12780 (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
12781 low_bound))
12782 first_non_one++;
12783 }
12784 else if (length == NULL_TREE)
12785 {
d9a6bd32
JJ
12786 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12787 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12788 maybe_zero_len = true;
acf0174b
JJ
12789 if (first_non_one == types.length ())
12790 first_non_one++;
12791 }
12792 if (length && TREE_CODE (length) == INTEGER_CST)
12793 {
12794 if (tree_int_cst_lt (size, length))
12795 {
12796 error_at (OMP_CLAUSE_LOCATION (c),
12797 "length %qE above array section size "
12798 "in %qs clause", length,
12799 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12800 return error_mark_node;
12801 }
12802 if (TREE_CODE (low_bound) == INTEGER_CST)
12803 {
12804 tree lbpluslen
12805 = size_binop (PLUS_EXPR,
12806 fold_convert (sizetype, low_bound),
12807 fold_convert (sizetype, length));
12808 if (TREE_CODE (lbpluslen) == INTEGER_CST
12809 && tree_int_cst_lt (size, lbpluslen))
12810 {
12811 error_at (OMP_CLAUSE_LOCATION (c),
12812 "high bound %qE above array section size "
12813 "in %qs clause", lbpluslen,
12814 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12815 return error_mark_node;
12816 }
12817 }
12818 }
12819 }
12820 else if (length == NULL_TREE)
12821 {
d9a6bd32
JJ
12822 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12823 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_REDUCTION)
12824 maybe_zero_len = true;
acf0174b
JJ
12825 if (first_non_one == types.length ())
12826 first_non_one++;
12827 }
12828
12829 /* For [lb:] we will need to evaluate lb more than once. */
12830 if (length == NULL_TREE && OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12831 {
b2fa0a8b 12832 tree lb = save_expr (low_bound);
acf0174b
JJ
12833 if (lb != low_bound)
12834 {
12835 TREE_PURPOSE (t) = lb;
12836 low_bound = lb;
12837 }
12838 }
12839 }
12840 else if (TREE_CODE (type) == POINTER_TYPE)
12841 {
12842 if (length == NULL_TREE)
12843 {
12844 error_at (OMP_CLAUSE_LOCATION (c),
12845 "for pointer type length expression must be specified");
12846 return error_mark_node;
12847 }
d9a6bd32
JJ
12848 if (length != NULL_TREE
12849 && TREE_CODE (length) == INTEGER_CST
12850 && tree_int_cst_sgn (length) == -1)
12851 {
12852 error_at (OMP_CLAUSE_LOCATION (c),
12853 "negative length in array section in %qs clause",
12854 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12855 return error_mark_node;
12856 }
acf0174b
JJ
12857 /* If there is a pointer type anywhere but in the very first
12858 array-section-subscript, the array section can't be contiguous. */
12859 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND
12860 && TREE_CODE (TREE_CHAIN (t)) == TREE_LIST)
12861 {
12862 error_at (OMP_CLAUSE_LOCATION (c),
12863 "array section is not contiguous in %qs clause",
12864 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
12865 return error_mark_node;
12866 }
12867 }
12868 else
12869 {
12870 error_at (OMP_CLAUSE_LOCATION (c),
12871 "%qE does not have pointer or array type", ret);
12872 return error_mark_node;
12873 }
12874 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_DEPEND)
12875 types.safe_push (TREE_TYPE (ret));
12876 /* We will need to evaluate lb more than once. */
b2fa0a8b 12877 tree lb = save_expr (low_bound);
acf0174b
JJ
12878 if (lb != low_bound)
12879 {
12880 TREE_PURPOSE (t) = lb;
12881 low_bound = lb;
12882 }
12883 ret = build_array_ref (OMP_CLAUSE_LOCATION (c), ret, low_bound);
12884 return ret;
12885}
12886
12887/* Handle array sections for clause C. */
12888
12889static bool
e46c7770 12890handle_omp_array_sections (tree c, enum c_omp_region_type ort)
acf0174b
JJ
12891{
12892 bool maybe_zero_len = false;
12893 unsigned int first_non_one = 0;
d9a6bd32 12894 auto_vec<tree, 10> types;
acf0174b 12895 tree first = handle_omp_array_sections_1 (c, OMP_CLAUSE_DECL (c), types,
d9a6bd32 12896 maybe_zero_len, first_non_one,
e46c7770 12897 ort);
acf0174b 12898 if (first == error_mark_node)
d9a6bd32 12899 return true;
acf0174b 12900 if (first == NULL_TREE)
d9a6bd32 12901 return false;
acf0174b
JJ
12902 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEPEND)
12903 {
12904 tree t = OMP_CLAUSE_DECL (c);
12905 tree tem = NULL_TREE;
acf0174b
JJ
12906 /* Need to evaluate side effects in the length expressions
12907 if any. */
12908 while (TREE_CODE (t) == TREE_LIST)
12909 {
12910 if (TREE_VALUE (t) && TREE_SIDE_EFFECTS (TREE_VALUE (t)))
12911 {
12912 if (tem == NULL_TREE)
12913 tem = TREE_VALUE (t);
12914 else
12915 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem),
12916 TREE_VALUE (t), tem);
12917 }
12918 t = TREE_CHAIN (t);
12919 }
12920 if (tem)
12921 first = build2 (COMPOUND_EXPR, TREE_TYPE (first), tem, first);
f9c59f7e 12922 first = c_fully_fold (first, false, NULL, true);
acf0174b
JJ
12923 OMP_CLAUSE_DECL (c) = first;
12924 }
12925 else
12926 {
12927 unsigned int num = types.length (), i;
12928 tree t, side_effects = NULL_TREE, size = NULL_TREE;
12929 tree condition = NULL_TREE;
12930
12931 if (int_size_in_bytes (TREE_TYPE (first)) <= 0)
12932 maybe_zero_len = true;
12933
12934 for (i = num, t = OMP_CLAUSE_DECL (c); i > 0;
12935 t = TREE_CHAIN (t))
12936 {
12937 tree low_bound = TREE_PURPOSE (t);
12938 tree length = TREE_VALUE (t);
12939
12940 i--;
12941 if (low_bound
12942 && TREE_CODE (low_bound) == INTEGER_CST
12943 && TYPE_PRECISION (TREE_TYPE (low_bound))
12944 > TYPE_PRECISION (sizetype))
12945 low_bound = fold_convert (sizetype, low_bound);
12946 if (length
12947 && TREE_CODE (length) == INTEGER_CST
12948 && TYPE_PRECISION (TREE_TYPE (length))
12949 > TYPE_PRECISION (sizetype))
12950 length = fold_convert (sizetype, length);
12951 if (low_bound == NULL_TREE)
12952 low_bound = integer_zero_node;
12953 if (!maybe_zero_len && i > first_non_one)
12954 {
12955 if (integer_nonzerop (low_bound))
12956 goto do_warn_noncontiguous;
12957 if (length != NULL_TREE
12958 && TREE_CODE (length) == INTEGER_CST
12959 && TYPE_DOMAIN (types[i])
12960 && TYPE_MAX_VALUE (TYPE_DOMAIN (types[i]))
12961 && TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])))
12962 == INTEGER_CST)
12963 {
12964 tree size;
12965 size = size_binop (PLUS_EXPR,
12966 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
12967 size_one_node);
12968 if (!tree_int_cst_equal (length, size))
12969 {
12970 do_warn_noncontiguous:
12971 error_at (OMP_CLAUSE_LOCATION (c),
12972 "array section is not contiguous in %qs "
12973 "clause",
12974 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
acf0174b
JJ
12975 return true;
12976 }
12977 }
12978 if (length != NULL_TREE
12979 && TREE_SIDE_EFFECTS (length))
12980 {
12981 if (side_effects == NULL_TREE)
12982 side_effects = length;
12983 else
12984 side_effects = build2 (COMPOUND_EXPR,
12985 TREE_TYPE (side_effects),
12986 length, side_effects);
12987 }
12988 }
12989 else
12990 {
12991 tree l;
12992
d9a6bd32
JJ
12993 if (i > first_non_one
12994 && ((length && integer_nonzerop (length))
12995 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION))
acf0174b
JJ
12996 continue;
12997 if (length)
12998 l = fold_convert (sizetype, length);
12999 else
13000 {
13001 l = size_binop (PLUS_EXPR,
13002 TYPE_MAX_VALUE (TYPE_DOMAIN (types[i])),
13003 size_one_node);
13004 l = size_binop (MINUS_EXPR, l,
13005 fold_convert (sizetype, low_bound));
13006 }
13007 if (i > first_non_one)
13008 {
13009 l = fold_build2 (NE_EXPR, boolean_type_node, l,
13010 size_zero_node);
13011 if (condition == NULL_TREE)
13012 condition = l;
13013 else
13014 condition = fold_build2 (BIT_AND_EXPR, boolean_type_node,
13015 l, condition);
13016 }
13017 else if (size == NULL_TREE)
13018 {
13019 size = size_in_bytes (TREE_TYPE (types[i]));
d9a6bd32
JJ
13020 tree eltype = TREE_TYPE (types[num - 1]);
13021 while (TREE_CODE (eltype) == ARRAY_TYPE)
13022 eltype = TREE_TYPE (eltype);
13023 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13024 {
13025 if (integer_zerop (size)
13026 || integer_zerop (size_in_bytes (eltype)))
13027 {
13028 error_at (OMP_CLAUSE_LOCATION (c),
13029 "zero length array section in %qs clause",
13030 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13031 return error_mark_node;
13032 }
13033 size = size_binop (EXACT_DIV_EXPR, size,
13034 size_in_bytes (eltype));
13035 }
acf0174b
JJ
13036 size = size_binop (MULT_EXPR, size, l);
13037 if (condition)
13038 size = fold_build3 (COND_EXPR, sizetype, condition,
13039 size, size_zero_node);
13040 }
13041 else
13042 size = size_binop (MULT_EXPR, size, l);
13043 }
13044 }
acf0174b
JJ
13045 if (side_effects)
13046 size = build2 (COMPOUND_EXPR, sizetype, side_effects, size);
d9a6bd32
JJ
13047 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13048 {
13049 size = size_binop (MINUS_EXPR, size, size_one_node);
13050 size = c_fully_fold (size, false, NULL);
13051 tree index_type = build_index_type (size);
13052 tree eltype = TREE_TYPE (first);
13053 while (TREE_CODE (eltype) == ARRAY_TYPE)
13054 eltype = TREE_TYPE (eltype);
13055 tree type = build_array_type (eltype, index_type);
13056 tree ptype = build_pointer_type (eltype);
13057 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13058 t = build_fold_addr_expr (t);
e01d41e5
JJ
13059 tree t2 = build_fold_addr_expr (first);
13060 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13061 ptrdiff_type_node, t2);
13062 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13063 ptrdiff_type_node, t2,
13064 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13065 ptrdiff_type_node, t));
13066 t2 = c_fully_fold (t2, false, NULL);
13067 if (tree_fits_shwi_p (t2))
13068 t = build2 (MEM_REF, type, t,
13069 build_int_cst (ptype, tree_to_shwi (t2)));
13070 else
13071 {
13072 t2 = fold_convert_loc (OMP_CLAUSE_LOCATION (c), sizetype, t2);
13073 t = build2_loc (OMP_CLAUSE_LOCATION (c), POINTER_PLUS_EXPR,
13074 TREE_TYPE (t), t, t2);
13075 t = build2 (MEM_REF, type, t, build_int_cst (ptype, 0));
13076 }
d9a6bd32
JJ
13077 OMP_CLAUSE_DECL (c) = t;
13078 return false;
13079 }
acf0174b
JJ
13080 first = c_fully_fold (first, false, NULL);
13081 OMP_CLAUSE_DECL (c) = first;
13082 if (size)
13083 size = c_fully_fold (size, false, NULL);
13084 OMP_CLAUSE_SIZE (c) = size;
d9a6bd32
JJ
13085 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13086 || (TREE_CODE (t) == COMPONENT_REF
13087 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE))
acf0174b 13088 return false;
41dbbb37 13089 gcc_assert (OMP_CLAUSE_MAP_KIND (c) != GOMP_MAP_FORCE_DEVICEPTR);
e46c7770 13090 if (ort == C_ORT_OMP || ort == C_ORT_ACC)
d9a6bd32
JJ
13091 switch (OMP_CLAUSE_MAP_KIND (c))
13092 {
13093 case GOMP_MAP_ALLOC:
13094 case GOMP_MAP_TO:
13095 case GOMP_MAP_FROM:
13096 case GOMP_MAP_TOFROM:
13097 case GOMP_MAP_ALWAYS_TO:
13098 case GOMP_MAP_ALWAYS_FROM:
13099 case GOMP_MAP_ALWAYS_TOFROM:
13100 case GOMP_MAP_RELEASE:
13101 case GOMP_MAP_DELETE:
b1c9c068
CP
13102 case GOMP_MAP_FORCE_TO:
13103 case GOMP_MAP_FORCE_FROM:
13104 case GOMP_MAP_FORCE_TOFROM:
13105 case GOMP_MAP_FORCE_PRESENT:
d9a6bd32
JJ
13106 OMP_CLAUSE_MAP_MAYBE_ZERO_LENGTH_ARRAY_SECTION (c) = 1;
13107 break;
13108 default:
13109 break;
13110 }
acf0174b 13111 tree c2 = build_omp_clause (OMP_CLAUSE_LOCATION (c), OMP_CLAUSE_MAP);
e46c7770 13112 if (ort != C_ORT_OMP && ort != C_ORT_ACC)
e01d41e5
JJ
13113 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_POINTER);
13114 else if (TREE_CODE (t) == COMPONENT_REF)
13115 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_ALWAYS_POINTER);
13116 else
13117 OMP_CLAUSE_SET_MAP_KIND (c2, GOMP_MAP_FIRSTPRIVATE_POINTER);
13118 if (OMP_CLAUSE_MAP_KIND (c2) != GOMP_MAP_FIRSTPRIVATE_POINTER
13119 && !c_mark_addressable (t))
acf0174b
JJ
13120 return false;
13121 OMP_CLAUSE_DECL (c2) = t;
13122 t = build_fold_addr_expr (first);
13123 t = fold_convert_loc (OMP_CLAUSE_LOCATION (c), ptrdiff_type_node, t);
13124 tree ptr = OMP_CLAUSE_DECL (c2);
13125 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
13126 ptr = build_fold_addr_expr (ptr);
13127 t = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
13128 ptrdiff_type_node, t,
13129 fold_convert_loc (OMP_CLAUSE_LOCATION (c),
13130 ptrdiff_type_node, ptr));
13131 t = c_fully_fold (t, false, NULL);
13132 OMP_CLAUSE_SIZE (c2) = t;
13133 OMP_CLAUSE_CHAIN (c2) = OMP_CLAUSE_CHAIN (c);
13134 OMP_CLAUSE_CHAIN (c) = c2;
13135 }
13136 return false;
13137}
13138
13139/* Helper function of finish_omp_clauses. Clone STMT as if we were making
13140 an inline call. But, remap
13141 the OMP_DECL1 VAR_DECL (omp_out resp. omp_orig) to PLACEHOLDER
13142 and OMP_DECL2 VAR_DECL (omp_in resp. omp_priv) to DECL. */
13143
13144static tree
13145c_clone_omp_udr (tree stmt, tree omp_decl1, tree omp_decl2,
13146 tree decl, tree placeholder)
13147{
13148 copy_body_data id;
b787e7a2 13149 hash_map<tree, tree> decl_map;
acf0174b 13150
b787e7a2
TS
13151 decl_map.put (omp_decl1, placeholder);
13152 decl_map.put (omp_decl2, decl);
acf0174b
JJ
13153 memset (&id, 0, sizeof (id));
13154 id.src_fn = DECL_CONTEXT (omp_decl1);
13155 id.dst_fn = current_function_decl;
13156 id.src_cfun = DECL_STRUCT_FUNCTION (id.src_fn);
b787e7a2 13157 id.decl_map = &decl_map;
acf0174b
JJ
13158
13159 id.copy_decl = copy_decl_no_change;
13160 id.transform_call_graph_edges = CB_CGE_DUPLICATE;
13161 id.transform_new_cfg = true;
13162 id.transform_return_to_modify = false;
13163 id.transform_lang_insert_block = NULL;
13164 id.eh_lp_nr = 0;
13165 walk_tree (&stmt, copy_tree_body_r, &id, NULL);
acf0174b
JJ
13166 return stmt;
13167}
13168
13169/* Helper function of c_finish_omp_clauses, called via walk_tree.
13170 Find OMP_CLAUSE_PLACEHOLDER (passed in DATA) in *TP. */
13171
13172static tree
13173c_find_omp_placeholder_r (tree *tp, int *, void *data)
13174{
13175 if (*tp == (tree) data)
13176 return *tp;
13177 return NULL_TREE;
13178}
13179
41dbbb37 13180/* For all elements of CLAUSES, validate them against their constraints.
953ff289
DN
13181 Remove any elements from the list that are invalid. */
13182
13183tree
77886428 13184c_finish_omp_clauses (tree clauses, enum c_omp_region_type ort)
953ff289
DN
13185{
13186 bitmap_head generic_head, firstprivate_head, lastprivate_head;
e46c7770 13187 bitmap_head aligned_head, map_head, map_field_head, oacc_reduction_head;
d9a6bd32
JJ
13188 tree c, t, type, *pc;
13189 tree simdlen = NULL_TREE, safelen = NULL_TREE;
acf0174b
JJ
13190 bool branch_seen = false;
13191 bool copyprivate_seen = false;
e01d41e5 13192 bool linear_variable_step_check = false;
acf0174b 13193 tree *nowait_clause = NULL;
e01d41e5
JJ
13194 bool ordered_seen = false;
13195 tree schedule_clause = NULL_TREE;
b605f663 13196 bool oacc_async = false;
953ff289
DN
13197
13198 bitmap_obstack_initialize (NULL);
13199 bitmap_initialize (&generic_head, &bitmap_default_obstack);
13200 bitmap_initialize (&firstprivate_head, &bitmap_default_obstack);
13201 bitmap_initialize (&lastprivate_head, &bitmap_default_obstack);
acf0174b 13202 bitmap_initialize (&aligned_head, &bitmap_default_obstack);
d9a6bd32
JJ
13203 bitmap_initialize (&map_head, &bitmap_default_obstack);
13204 bitmap_initialize (&map_field_head, &bitmap_default_obstack);
e46c7770 13205 bitmap_initialize (&oacc_reduction_head, &bitmap_default_obstack);
953ff289 13206
b605f663
CLT
13207 if (ort & C_ORT_ACC)
13208 for (c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
13209 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_ASYNC)
13210 {
13211 oacc_async = true;
13212 break;
13213 }
9dc5773f 13214
953ff289
DN
13215 for (pc = &clauses, c = clauses; c ; c = *pc)
13216 {
13217 bool remove = false;
13218 bool need_complete = false;
13219 bool need_implicitly_determined = false;
13220
aaf46ef9 13221 switch (OMP_CLAUSE_CODE (c))
953ff289
DN
13222 {
13223 case OMP_CLAUSE_SHARED:
953ff289
DN
13224 need_implicitly_determined = true;
13225 goto check_dup_generic;
13226
13227 case OMP_CLAUSE_PRIVATE:
953ff289
DN
13228 need_complete = true;
13229 need_implicitly_determined = true;
13230 goto check_dup_generic;
13231
13232 case OMP_CLAUSE_REDUCTION:
953ff289
DN
13233 need_implicitly_determined = true;
13234 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13235 if (TREE_CODE (t) == TREE_LIST)
13236 {
e46c7770 13237 if (handle_omp_array_sections (c, ort))
d9a6bd32
JJ
13238 {
13239 remove = true;
13240 break;
13241 }
13242
13243 t = OMP_CLAUSE_DECL (c);
13244 }
4f2e1536 13245 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
d9a6bd32
JJ
13246 if (t == error_mark_node)
13247 {
13248 remove = true;
13249 break;
13250 }
b605f663
CLT
13251 if (oacc_async)
13252 c_mark_addressable (t);
d9a6bd32
JJ
13253 type = TREE_TYPE (t);
13254 if (TREE_CODE (t) == MEM_REF)
13255 type = TREE_TYPE (type);
13256 if (TREE_CODE (type) == ARRAY_TYPE)
13257 {
13258 tree oatype = type;
13259 gcc_assert (TREE_CODE (t) != MEM_REF);
13260 while (TREE_CODE (type) == ARRAY_TYPE)
13261 type = TREE_TYPE (type);
13262 if (integer_zerop (TYPE_SIZE_UNIT (type)))
13263 {
13264 error_at (OMP_CLAUSE_LOCATION (c),
13265 "%qD in %<reduction%> clause is a zero size array",
13266 t);
13267 remove = true;
13268 break;
13269 }
13270 tree size = size_binop (EXACT_DIV_EXPR, TYPE_SIZE_UNIT (oatype),
13271 TYPE_SIZE_UNIT (type));
13272 if (integer_zerop (size))
13273 {
13274 error_at (OMP_CLAUSE_LOCATION (c),
13275 "%qD in %<reduction%> clause is a zero size array",
13276 t);
13277 remove = true;
13278 break;
13279 }
13280 size = size_binop (MINUS_EXPR, size, size_one_node);
13281 tree index_type = build_index_type (size);
13282 tree atype = build_array_type (type, index_type);
13283 tree ptype = build_pointer_type (type);
13284 if (TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13285 t = build_fold_addr_expr (t);
13286 t = build2 (MEM_REF, atype, t, build_int_cst (ptype, 0));
13287 OMP_CLAUSE_DECL (c) = t;
13288 }
9dc5773f
JJ
13289 if (TYPE_ATOMIC (type))
13290 {
13291 error_at (OMP_CLAUSE_LOCATION (c),
13292 "%<_Atomic%> %qE in %<reduction%> clause", t);
13293 remove = true;
13294 break;
13295 }
acf0174b 13296 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == NULL_TREE
d9a6bd32
JJ
13297 && (FLOAT_TYPE_P (type)
13298 || TREE_CODE (type) == COMPLEX_TYPE))
953ff289
DN
13299 {
13300 enum tree_code r_code = OMP_CLAUSE_REDUCTION_CODE (c);
13301 const char *r_name = NULL;
13302
13303 switch (r_code)
13304 {
13305 case PLUS_EXPR:
13306 case MULT_EXPR:
13307 case MINUS_EXPR:
652fea39 13308 break;
20906c66 13309 case MIN_EXPR:
d9a6bd32 13310 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39
JJ
13311 r_name = "min";
13312 break;
20906c66 13313 case MAX_EXPR:
d9a6bd32 13314 if (TREE_CODE (type) == COMPLEX_TYPE)
652fea39 13315 r_name = "max";
953ff289
DN
13316 break;
13317 case BIT_AND_EXPR:
13318 r_name = "&";
13319 break;
13320 case BIT_XOR_EXPR:
13321 r_name = "^";
13322 break;
13323 case BIT_IOR_EXPR:
13324 r_name = "|";
13325 break;
13326 case TRUTH_ANDIF_EXPR:
d9a6bd32 13327 if (FLOAT_TYPE_P (type))
652fea39 13328 r_name = "&&";
953ff289
DN
13329 break;
13330 case TRUTH_ORIF_EXPR:
d9a6bd32 13331 if (FLOAT_TYPE_P (type))
652fea39 13332 r_name = "||";
953ff289
DN
13333 break;
13334 default:
13335 gcc_unreachable ();
13336 }
13337 if (r_name)
13338 {
c2255bc4
AH
13339 error_at (OMP_CLAUSE_LOCATION (c),
13340 "%qE has invalid type for %<reduction(%s)%>",
13341 t, r_name);
953ff289 13342 remove = true;
ee1d5a02 13343 break;
953ff289
DN
13344 }
13345 }
acf0174b
JJ
13346 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) == error_mark_node)
13347 {
13348 error_at (OMP_CLAUSE_LOCATION (c),
d9a6bd32 13349 "user defined reduction not found for %qE", t);
acf0174b 13350 remove = true;
ee1d5a02 13351 break;
acf0174b
JJ
13352 }
13353 else if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (c))
13354 {
13355 tree list = OMP_CLAUSE_REDUCTION_PLACEHOLDER (c);
d9a6bd32 13356 type = TYPE_MAIN_VARIANT (type);
acf0174b
JJ
13357 tree placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13358 VAR_DECL, NULL_TREE, type);
d9a6bd32 13359 tree decl_placeholder = NULL_TREE;
acf0174b
JJ
13360 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = placeholder;
13361 DECL_ARTIFICIAL (placeholder) = 1;
13362 DECL_IGNORED_P (placeholder) = 1;
d9a6bd32
JJ
13363 if (TREE_CODE (t) == MEM_REF)
13364 {
13365 decl_placeholder = build_decl (OMP_CLAUSE_LOCATION (c),
13366 VAR_DECL, NULL_TREE, type);
13367 OMP_CLAUSE_REDUCTION_DECL_PLACEHOLDER (c) = decl_placeholder;
13368 DECL_ARTIFICIAL (decl_placeholder) = 1;
13369 DECL_IGNORED_P (decl_placeholder) = 1;
13370 }
acf0174b
JJ
13371 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 0)))
13372 c_mark_addressable (placeholder);
13373 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 1)))
d9a6bd32
JJ
13374 c_mark_addressable (decl_placeholder ? decl_placeholder
13375 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
13376 OMP_CLAUSE_REDUCTION_MERGE (c)
13377 = c_clone_omp_udr (TREE_VEC_ELT (list, 2),
13378 TREE_VEC_ELT (list, 0),
13379 TREE_VEC_ELT (list, 1),
d9a6bd32
JJ
13380 decl_placeholder ? decl_placeholder
13381 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b
JJ
13382 OMP_CLAUSE_REDUCTION_MERGE (c)
13383 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13384 void_type_node, NULL_TREE,
d9a6bd32 13385 OMP_CLAUSE_REDUCTION_MERGE (c), NULL_TREE);
acf0174b
JJ
13386 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_MERGE (c)) = 1;
13387 if (TREE_VEC_LENGTH (list) == 6)
13388 {
13389 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 3)))
d9a6bd32
JJ
13390 c_mark_addressable (decl_placeholder ? decl_placeholder
13391 : OMP_CLAUSE_DECL (c));
acf0174b
JJ
13392 if (TREE_ADDRESSABLE (TREE_VEC_ELT (list, 4)))
13393 c_mark_addressable (placeholder);
13394 tree init = TREE_VEC_ELT (list, 5);
13395 if (init == error_mark_node)
13396 init = DECL_INITIAL (TREE_VEC_ELT (list, 3));
13397 OMP_CLAUSE_REDUCTION_INIT (c)
13398 = c_clone_omp_udr (init, TREE_VEC_ELT (list, 4),
13399 TREE_VEC_ELT (list, 3),
d9a6bd32
JJ
13400 decl_placeholder ? decl_placeholder
13401 : OMP_CLAUSE_DECL (c), placeholder);
acf0174b 13402 if (TREE_VEC_ELT (list, 5) == error_mark_node)
d9a6bd32
JJ
13403 {
13404 tree v = decl_placeholder ? decl_placeholder : t;
13405 OMP_CLAUSE_REDUCTION_INIT (c)
13406 = build2 (INIT_EXPR, TREE_TYPE (v), v,
13407 OMP_CLAUSE_REDUCTION_INIT (c));
13408 }
acf0174b
JJ
13409 if (walk_tree (&OMP_CLAUSE_REDUCTION_INIT (c),
13410 c_find_omp_placeholder_r,
13411 placeholder, NULL))
13412 OMP_CLAUSE_REDUCTION_OMP_ORIG_REF (c) = 1;
13413 }
13414 else
13415 {
13416 tree init;
d9a6bd32
JJ
13417 tree v = decl_placeholder ? decl_placeholder : t;
13418 if (AGGREGATE_TYPE_P (TREE_TYPE (v)))
13419 init = build_constructor (TREE_TYPE (v), NULL);
acf0174b 13420 else
d9a6bd32 13421 init = fold_convert (TREE_TYPE (v), integer_zero_node);
acf0174b 13422 OMP_CLAUSE_REDUCTION_INIT (c)
d9a6bd32 13423 = build2 (INIT_EXPR, TREE_TYPE (v), v, init);
acf0174b
JJ
13424 }
13425 OMP_CLAUSE_REDUCTION_INIT (c)
13426 = build3_loc (OMP_CLAUSE_LOCATION (c), BIND_EXPR,
13427 void_type_node, NULL_TREE,
13428 OMP_CLAUSE_REDUCTION_INIT (c), NULL_TREE);
13429 TREE_SIDE_EFFECTS (OMP_CLAUSE_REDUCTION_INIT (c)) = 1;
13430 }
d9a6bd32
JJ
13431 if (TREE_CODE (t) == MEM_REF)
13432 {
13433 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))) == NULL_TREE
13434 || TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (t))))
13435 != INTEGER_CST)
13436 {
13437 sorry ("variable length element type in array "
13438 "%<reduction%> clause");
13439 remove = true;
13440 break;
13441 }
13442 t = TREE_OPERAND (t, 0);
e01d41e5
JJ
13443 if (TREE_CODE (t) == POINTER_PLUS_EXPR)
13444 t = TREE_OPERAND (t, 0);
d9a6bd32
JJ
13445 if (TREE_CODE (t) == ADDR_EXPR)
13446 t = TREE_OPERAND (t, 0);
13447 }
13448 goto check_dup_generic_t;
953ff289
DN
13449
13450 case OMP_CLAUSE_COPYPRIVATE:
acf0174b
JJ
13451 copyprivate_seen = true;
13452 if (nowait_clause)
13453 {
13454 error_at (OMP_CLAUSE_LOCATION (*nowait_clause),
13455 "%<nowait%> clause must not be used together "
13456 "with %<copyprivate%>");
13457 *nowait_clause = OMP_CLAUSE_CHAIN (*nowait_clause);
13458 nowait_clause = NULL;
13459 }
953ff289
DN
13460 goto check_dup_generic;
13461
13462 case OMP_CLAUSE_COPYIN:
953ff289 13463 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13464 if (!VAR_P (t) || !DECL_THREAD_LOCAL_P (t))
953ff289 13465 {
c2255bc4
AH
13466 error_at (OMP_CLAUSE_LOCATION (c),
13467 "%qE must be %<threadprivate%> for %<copyin%>", t);
953ff289 13468 remove = true;
ee1d5a02 13469 break;
953ff289
DN
13470 }
13471 goto check_dup_generic;
13472
acf0174b 13473 case OMP_CLAUSE_LINEAR:
77886428 13474 if (ort != C_ORT_OMP_DECLARE_SIMD)
d9a6bd32 13475 need_implicitly_determined = true;
acf0174b 13476 t = OMP_CLAUSE_DECL (c);
77886428 13477 if (ort != C_ORT_OMP_DECLARE_SIMD
d9a6bd32
JJ
13478 && OMP_CLAUSE_LINEAR_KIND (c) != OMP_CLAUSE_LINEAR_DEFAULT)
13479 {
13480 error_at (OMP_CLAUSE_LOCATION (c),
13481 "modifier should not be specified in %<linear%> "
13482 "clause on %<simd%> or %<for%> constructs");
13483 OMP_CLAUSE_LINEAR_KIND (c) = OMP_CLAUSE_LINEAR_DEFAULT;
13484 }
5e9d6aa4
JK
13485 if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
13486 && TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE)
acf0174b 13487 {
5e9d6aa4
JK
13488 error_at (OMP_CLAUSE_LOCATION (c),
13489 "linear clause applied to non-integral non-pointer "
13490 "variable with type %qT", TREE_TYPE (t));
13491 remove = true;
13492 break;
477d4906 13493 }
5e9d6aa4 13494 if (TYPE_ATOMIC (TREE_TYPE (t)))
477d4906 13495 {
5e9d6aa4
JK
13496 error_at (OMP_CLAUSE_LOCATION (c),
13497 "%<_Atomic%> %qD in %<linear%> clause", t);
13498 remove = true;
13499 break;
acf0174b 13500 }
77886428 13501 if (ort == C_ORT_OMP_DECLARE_SIMD)
e01d41e5
JJ
13502 {
13503 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13504 if (TREE_CODE (s) == PARM_DECL)
13505 {
13506 OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c) = 1;
13507 /* map_head bitmap is used as uniform_head if
13508 declare_simd. */
13509 if (!bitmap_bit_p (&map_head, DECL_UID (s)))
13510 linear_variable_step_check = true;
13511 goto check_dup_generic;
13512 }
13513 if (TREE_CODE (s) != INTEGER_CST)
13514 {
13515 error_at (OMP_CLAUSE_LOCATION (c),
13516 "%<linear%> clause step %qE is neither constant "
13517 "nor a parameter", s);
13518 remove = true;
13519 break;
13520 }
13521 }
acf0174b
JJ
13522 if (TREE_CODE (TREE_TYPE (OMP_CLAUSE_DECL (c))) == POINTER_TYPE)
13523 {
13524 tree s = OMP_CLAUSE_LINEAR_STEP (c);
13525 s = pointer_int_sum (OMP_CLAUSE_LOCATION (c), PLUS_EXPR,
13526 OMP_CLAUSE_DECL (c), s);
13527 s = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13528 sizetype, fold_convert (sizetype, s),
13529 fold_convert
13530 (sizetype, OMP_CLAUSE_DECL (c)));
acf0174b
JJ
13531 if (s == error_mark_node)
13532 s = size_one_node;
13533 OMP_CLAUSE_LINEAR_STEP (c) = s;
13534 }
da6f124d
JJ
13535 else
13536 OMP_CLAUSE_LINEAR_STEP (c)
13537 = fold_convert (TREE_TYPE (t), OMP_CLAUSE_LINEAR_STEP (c));
acf0174b
JJ
13538 goto check_dup_generic;
13539
953ff289
DN
13540 check_dup_generic:
13541 t = OMP_CLAUSE_DECL (c);
d9a6bd32 13542 check_dup_generic_t:
0ae9bd27 13543 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13544 {
c2255bc4 13545 error_at (OMP_CLAUSE_LOCATION (c),
acf0174b
JJ
13546 "%qE is not a variable in clause %qs", t,
13547 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
13548 remove = true;
13549 }
e46c7770
CP
13550 else if (ort == C_ORT_ACC
13551 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION)
13552 {
13553 if (bitmap_bit_p (&oacc_reduction_head, DECL_UID (t)))
13554 {
13555 error ("%qD appears more than once in reduction clauses", t);
13556 remove = true;
13557 }
13558 else
13559 bitmap_set_bit (&oacc_reduction_head, DECL_UID (t));
13560 }
953ff289
DN
13561 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13562 || bitmap_bit_p (&firstprivate_head, DECL_UID (t))
13563 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13564 {
c2255bc4
AH
13565 error_at (OMP_CLAUSE_LOCATION (c),
13566 "%qE appears more than once in data clauses", t);
953ff289
DN
13567 remove = true;
13568 }
e01d41e5
JJ
13569 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
13570 && bitmap_bit_p (&map_head, DECL_UID (t)))
13571 {
e46c7770
CP
13572 if (ort == C_ORT_ACC)
13573 error ("%qD appears more than once in data clauses", t);
13574 else
13575 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13576 remove = true;
13577 }
953ff289
DN
13578 else
13579 bitmap_set_bit (&generic_head, DECL_UID (t));
13580 break;
13581
13582 case OMP_CLAUSE_FIRSTPRIVATE:
953ff289
DN
13583 t = OMP_CLAUSE_DECL (c);
13584 need_complete = true;
13585 need_implicitly_determined = true;
0ae9bd27 13586 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13587 {
c2255bc4
AH
13588 error_at (OMP_CLAUSE_LOCATION (c),
13589 "%qE is not a variable in clause %<firstprivate%>", t);
953ff289
DN
13590 remove = true;
13591 }
13592 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13593 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13594 {
c2255bc4
AH
13595 error_at (OMP_CLAUSE_LOCATION (c),
13596 "%qE appears more than once in data clauses", t);
953ff289
DN
13597 remove = true;
13598 }
e01d41e5
JJ
13599 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
13600 {
e46c7770
CP
13601 if (ort == C_ORT_ACC)
13602 error ("%qD appears more than once in data clauses", t);
13603 else
13604 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13605 remove = true;
13606 }
953ff289
DN
13607 else
13608 bitmap_set_bit (&firstprivate_head, DECL_UID (t));
13609 break;
13610
13611 case OMP_CLAUSE_LASTPRIVATE:
953ff289
DN
13612 t = OMP_CLAUSE_DECL (c);
13613 need_complete = true;
13614 need_implicitly_determined = true;
0ae9bd27 13615 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
953ff289 13616 {
c2255bc4
AH
13617 error_at (OMP_CLAUSE_LOCATION (c),
13618 "%qE is not a variable in clause %<lastprivate%>", t);
953ff289
DN
13619 remove = true;
13620 }
13621 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13622 || bitmap_bit_p (&lastprivate_head, DECL_UID (t)))
13623 {
c2255bc4
AH
13624 error_at (OMP_CLAUSE_LOCATION (c),
13625 "%qE appears more than once in data clauses", t);
953ff289
DN
13626 remove = true;
13627 }
13628 else
13629 bitmap_set_bit (&lastprivate_head, DECL_UID (t));
13630 break;
13631
acf0174b
JJ
13632 case OMP_CLAUSE_ALIGNED:
13633 t = OMP_CLAUSE_DECL (c);
0ae9bd27 13634 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13635 {
13636 error_at (OMP_CLAUSE_LOCATION (c),
13637 "%qE is not a variable in %<aligned%> clause", t);
13638 remove = true;
13639 }
5a9785fb
JJ
13640 else if (!POINTER_TYPE_P (TREE_TYPE (t))
13641 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13642 {
13643 error_at (OMP_CLAUSE_LOCATION (c),
13644 "%qE in %<aligned%> clause is neither a pointer nor "
13645 "an array", t);
13646 remove = true;
13647 }
9dc5773f
JJ
13648 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13649 {
13650 error_at (OMP_CLAUSE_LOCATION (c),
13651 "%<_Atomic%> %qD in %<aligned%> clause", t);
13652 remove = true;
13653 break;
13654 }
acf0174b
JJ
13655 else if (bitmap_bit_p (&aligned_head, DECL_UID (t)))
13656 {
13657 error_at (OMP_CLAUSE_LOCATION (c),
13658 "%qE appears more than once in %<aligned%> clauses",
13659 t);
13660 remove = true;
13661 }
13662 else
13663 bitmap_set_bit (&aligned_head, DECL_UID (t));
13664 break;
13665
13666 case OMP_CLAUSE_DEPEND:
13667 t = OMP_CLAUSE_DECL (c);
d9a6bd32
JJ
13668 if (t == NULL_TREE)
13669 {
13670 gcc_assert (OMP_CLAUSE_DEPEND_KIND (c)
13671 == OMP_CLAUSE_DEPEND_SOURCE);
13672 break;
13673 }
13674 if (OMP_CLAUSE_DEPEND_KIND (c) == OMP_CLAUSE_DEPEND_SINK)
13675 {
13676 gcc_assert (TREE_CODE (t) == TREE_LIST);
13677 for (; t; t = TREE_CHAIN (t))
13678 {
13679 tree decl = TREE_VALUE (t);
13680 if (TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
13681 {
13682 tree offset = TREE_PURPOSE (t);
8e6cdc90 13683 bool neg = wi::neg_p (wi::to_wide (offset));
d9a6bd32
JJ
13684 offset = fold_unary (ABS_EXPR, TREE_TYPE (offset), offset);
13685 tree t2 = pointer_int_sum (OMP_CLAUSE_LOCATION (c),
13686 neg ? MINUS_EXPR : PLUS_EXPR,
13687 decl, offset);
13688 t2 = fold_build2_loc (OMP_CLAUSE_LOCATION (c), MINUS_EXPR,
a8fc2579
RB
13689 sizetype,
13690 fold_convert (sizetype, t2),
13691 fold_convert (sizetype, decl));
d9a6bd32
JJ
13692 if (t2 == error_mark_node)
13693 {
13694 remove = true;
13695 break;
13696 }
13697 TREE_PURPOSE (t) = t2;
13698 }
13699 }
13700 break;
13701 }
acf0174b
JJ
13702 if (TREE_CODE (t) == TREE_LIST)
13703 {
e46c7770 13704 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13705 remove = true;
13706 break;
13707 }
13708 if (t == error_mark_node)
13709 remove = true;
0ae9bd27 13710 else if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13711 {
13712 error_at (OMP_CLAUSE_LOCATION (c),
13713 "%qE is not a variable in %<depend%> clause", t);
13714 remove = true;
13715 }
13716 else if (!c_mark_addressable (t))
13717 remove = true;
13718 break;
13719
13720 case OMP_CLAUSE_MAP:
13721 case OMP_CLAUSE_TO:
13722 case OMP_CLAUSE_FROM:
41dbbb37 13723 case OMP_CLAUSE__CACHE_:
acf0174b
JJ
13724 t = OMP_CLAUSE_DECL (c);
13725 if (TREE_CODE (t) == TREE_LIST)
13726 {
e46c7770 13727 if (handle_omp_array_sections (c, ort))
acf0174b
JJ
13728 remove = true;
13729 else
13730 {
13731 t = OMP_CLAUSE_DECL (c);
b17a8b07 13732 if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13733 {
13734 error_at (OMP_CLAUSE_LOCATION (c),
13735 "array section does not have mappable type "
13736 "in %qs clause",
13737 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13738 remove = true;
13739 }
9dc5773f
JJ
13740 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13741 {
13742 error_at (OMP_CLAUSE_LOCATION (c),
13743 "%<_Atomic%> %qE in %qs clause", t,
13744 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13745 remove = true;
13746 }
d9a6bd32
JJ
13747 while (TREE_CODE (t) == ARRAY_REF)
13748 t = TREE_OPERAND (t, 0);
13749 if (TREE_CODE (t) == COMPONENT_REF
13750 && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE)
13751 {
13752 while (TREE_CODE (t) == COMPONENT_REF)
13753 t = TREE_OPERAND (t, 0);
13754 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
13755 break;
13756 if (bitmap_bit_p (&map_head, DECL_UID (t)))
13757 {
13758 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13759 error ("%qD appears more than once in motion"
13760 " clauses", t);
e46c7770
CP
13761 else if (ort == C_ORT_ACC)
13762 error ("%qD appears more than once in data"
13763 " clauses", t);
d9a6bd32
JJ
13764 else
13765 error ("%qD appears more than once in map"
13766 " clauses", t);
13767 remove = true;
13768 }
13769 else
13770 {
13771 bitmap_set_bit (&map_head, DECL_UID (t));
13772 bitmap_set_bit (&map_field_head, DECL_UID (t));
13773 }
13774 }
acf0174b
JJ
13775 }
13776 break;
13777 }
13778 if (t == error_mark_node)
d9a6bd32
JJ
13779 {
13780 remove = true;
13781 break;
13782 }
13783 if (TREE_CODE (t) == COMPONENT_REF
77886428 13784 && (ort & C_ORT_OMP)
d9a6bd32
JJ
13785 && OMP_CLAUSE_CODE (c) != OMP_CLAUSE__CACHE_)
13786 {
13787 if (DECL_BIT_FIELD (TREE_OPERAND (t, 1)))
13788 {
13789 error_at (OMP_CLAUSE_LOCATION (c),
13790 "bit-field %qE in %qs clause",
13791 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13792 remove = true;
13793 }
13794 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13795 {
13796 error_at (OMP_CLAUSE_LOCATION (c),
13797 "%qE does not have a mappable type in %qs clause",
13798 t, omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13799 remove = true;
13800 }
9dc5773f
JJ
13801 else if (TYPE_ATOMIC (TREE_TYPE (t)))
13802 {
13803 error_at (OMP_CLAUSE_LOCATION (c),
13804 "%<_Atomic%> %qE in %qs clause", t,
13805 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13806 remove = true;
13807 }
d9a6bd32
JJ
13808 while (TREE_CODE (t) == COMPONENT_REF)
13809 {
13810 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0)))
13811 == UNION_TYPE)
13812 {
13813 error_at (OMP_CLAUSE_LOCATION (c),
13814 "%qE is a member of a union", t);
13815 remove = true;
13816 break;
13817 }
13818 t = TREE_OPERAND (t, 0);
13819 }
13820 if (remove)
13821 break;
13822 if (VAR_P (t) || TREE_CODE (t) == PARM_DECL)
13823 {
e01d41e5 13824 if (bitmap_bit_p (&map_field_head, DECL_UID (t)))
d9a6bd32
JJ
13825 break;
13826 }
13827 }
13828 if (!VAR_P (t) && TREE_CODE (t) != PARM_DECL)
acf0174b
JJ
13829 {
13830 error_at (OMP_CLAUSE_LOCATION (c),
13831 "%qE is not a variable in %qs clause", t,
13832 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13833 remove = true;
13834 }
0ae9bd27 13835 else if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
acf0174b
JJ
13836 {
13837 error_at (OMP_CLAUSE_LOCATION (c),
13838 "%qD is threadprivate variable in %qs clause", t,
13839 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13840 remove = true;
13841 }
e4606348
JJ
13842 else if ((OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP
13843 || (OMP_CLAUSE_MAP_KIND (c)
13844 != GOMP_MAP_FIRSTPRIVATE_POINTER))
13845 && !c_mark_addressable (t))
acf0174b 13846 remove = true;
b17a8b07 13847 else if (!(OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
41dbbb37 13848 && (OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_POINTER
d9a6bd32
JJ
13849 || (OMP_CLAUSE_MAP_KIND (c)
13850 == GOMP_MAP_FIRSTPRIVATE_POINTER)
41dbbb37
TS
13851 || (OMP_CLAUSE_MAP_KIND (c)
13852 == GOMP_MAP_FORCE_DEVICEPTR)))
d9a6bd32 13853 && t == OMP_CLAUSE_DECL (c)
b17a8b07 13854 && !lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
acf0174b
JJ
13855 {
13856 error_at (OMP_CLAUSE_LOCATION (c),
13857 "%qD does not have a mappable type in %qs clause", t,
13858 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13859 remove = true;
13860 }
9dc5773f
JJ
13861 else if (TREE_TYPE (t) == error_mark_node)
13862 remove = true;
13863 else if (TYPE_ATOMIC (strip_array_types (TREE_TYPE (t))))
13864 {
13865 error_at (OMP_CLAUSE_LOCATION (c),
13866 "%<_Atomic%> %qE in %qs clause", t,
13867 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13868 remove = true;
13869 }
d9a6bd32
JJ
13870 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_MAP
13871 && OMP_CLAUSE_MAP_KIND (c) == GOMP_MAP_FIRSTPRIVATE_POINTER)
13872 {
13873 if (bitmap_bit_p (&generic_head, DECL_UID (t))
13874 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13875 {
13876 error ("%qD appears more than once in data clauses", t);
13877 remove = true;
13878 }
e01d41e5 13879 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
d9a6bd32 13880 {
e46c7770
CP
13881 if (ort == C_ORT_ACC)
13882 error ("%qD appears more than once in data clauses", t);
13883 else
13884 error ("%qD appears both in data and map clauses", t);
e01d41e5 13885 remove = true;
d9a6bd32 13886 }
e01d41e5
JJ
13887 else
13888 bitmap_set_bit (&generic_head, DECL_UID (t));
d9a6bd32
JJ
13889 }
13890 else if (bitmap_bit_p (&map_head, DECL_UID (t)))
acf0174b
JJ
13891 {
13892 if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_MAP)
13893 error ("%qD appears more than once in motion clauses", t);
e46c7770
CP
13894 else if (ort == C_ORT_ACC)
13895 error ("%qD appears more than once in data clauses", t);
acf0174b
JJ
13896 else
13897 error ("%qD appears more than once in map clauses", t);
13898 remove = true;
13899 }
e01d41e5
JJ
13900 else if (bitmap_bit_p (&generic_head, DECL_UID (t))
13901 || bitmap_bit_p (&firstprivate_head, DECL_UID (t)))
13902 {
e46c7770
CP
13903 if (ort == C_ORT_ACC)
13904 error ("%qD appears more than once in data clauses", t);
13905 else
13906 error ("%qD appears both in data and map clauses", t);
e01d41e5
JJ
13907 remove = true;
13908 }
acf0174b 13909 else
d9a6bd32
JJ
13910 {
13911 bitmap_set_bit (&map_head, DECL_UID (t));
13912 if (t != OMP_CLAUSE_DECL (c)
13913 && TREE_CODE (OMP_CLAUSE_DECL (c)) == COMPONENT_REF)
13914 bitmap_set_bit (&map_field_head, DECL_UID (t));
13915 }
13916 break;
13917
13918 case OMP_CLAUSE_TO_DECLARE:
d9a6bd32
JJ
13919 case OMP_CLAUSE_LINK:
13920 t = OMP_CLAUSE_DECL (c);
e01d41e5
JJ
13921 if (TREE_CODE (t) == FUNCTION_DECL
13922 && OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13923 ;
13924 else if (!VAR_P (t))
d9a6bd32 13925 {
e01d41e5
JJ
13926 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_TO_DECLARE)
13927 error_at (OMP_CLAUSE_LOCATION (c),
13928 "%qE is neither a variable nor a function name in "
13929 "clause %qs", t,
13930 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13931 else
13932 error_at (OMP_CLAUSE_LOCATION (c),
13933 "%qE is not a variable in clause %qs", t,
13934 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
d9a6bd32
JJ
13935 remove = true;
13936 }
13937 else if (DECL_THREAD_LOCAL_P (t))
13938 {
13939 error_at (OMP_CLAUSE_LOCATION (c),
13940 "%qD is threadprivate variable in %qs clause", t,
13941 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13942 remove = true;
13943 }
13944 else if (!lang_hooks.types.omp_mappable_type (TREE_TYPE (t)))
13945 {
13946 error_at (OMP_CLAUSE_LOCATION (c),
13947 "%qD does not have a mappable type in %qs clause", t,
13948 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13949 remove = true;
13950 }
e01d41e5
JJ
13951 if (remove)
13952 break;
13953 if (bitmap_bit_p (&generic_head, DECL_UID (t)))
13954 {
13955 error_at (OMP_CLAUSE_LOCATION (c),
13956 "%qE appears more than once on the same "
13957 "%<declare target%> directive", t);
13958 remove = true;
13959 }
13960 else
13961 bitmap_set_bit (&generic_head, DECL_UID (t));
acf0174b
JJ
13962 break;
13963
13964 case OMP_CLAUSE_UNIFORM:
13965 t = OMP_CLAUSE_DECL (c);
13966 if (TREE_CODE (t) != PARM_DECL)
13967 {
13968 if (DECL_P (t))
13969 error_at (OMP_CLAUSE_LOCATION (c),
13970 "%qD is not an argument in %<uniform%> clause", t);
13971 else
13972 error_at (OMP_CLAUSE_LOCATION (c),
13973 "%qE is not an argument in %<uniform%> clause", t);
13974 remove = true;
ee1d5a02 13975 break;
acf0174b 13976 }
e01d41e5
JJ
13977 /* map_head bitmap is used as uniform_head if declare_simd. */
13978 bitmap_set_bit (&map_head, DECL_UID (t));
ee1d5a02 13979 goto check_dup_generic;
acf0174b 13980
d9a6bd32
JJ
13981 case OMP_CLAUSE_IS_DEVICE_PTR:
13982 case OMP_CLAUSE_USE_DEVICE_PTR:
13983 t = OMP_CLAUSE_DECL (c);
13984 if (TREE_CODE (TREE_TYPE (t)) != POINTER_TYPE
13985 && TREE_CODE (TREE_TYPE (t)) != ARRAY_TYPE)
13986 {
13987 error_at (OMP_CLAUSE_LOCATION (c),
13988 "%qs variable is neither a pointer nor an array",
13989 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
13990 remove = true;
13991 }
13992 goto check_dup_generic;
13993
acf0174b
JJ
13994 case OMP_CLAUSE_NOWAIT:
13995 if (copyprivate_seen)
13996 {
13997 error_at (OMP_CLAUSE_LOCATION (c),
13998 "%<nowait%> clause must not be used together "
13999 "with %<copyprivate%>");
14000 remove = true;
14001 break;
14002 }
14003 nowait_clause = pc;
14004 pc = &OMP_CLAUSE_CHAIN (c);
14005 continue;
14006
953ff289
DN
14007 case OMP_CLAUSE_IF:
14008 case OMP_CLAUSE_NUM_THREADS:
acf0174b
JJ
14009 case OMP_CLAUSE_NUM_TEAMS:
14010 case OMP_CLAUSE_THREAD_LIMIT:
953ff289 14011 case OMP_CLAUSE_DEFAULT:
a68ab351
JJ
14012 case OMP_CLAUSE_UNTIED:
14013 case OMP_CLAUSE_COLLAPSE:
20906c66
JJ
14014 case OMP_CLAUSE_FINAL:
14015 case OMP_CLAUSE_MERGEABLE:
acf0174b
JJ
14016 case OMP_CLAUSE_DEVICE:
14017 case OMP_CLAUSE_DIST_SCHEDULE:
14018 case OMP_CLAUSE_PARALLEL:
14019 case OMP_CLAUSE_FOR:
14020 case OMP_CLAUSE_SECTIONS:
14021 case OMP_CLAUSE_TASKGROUP:
14022 case OMP_CLAUSE_PROC_BIND:
d9a6bd32
JJ
14023 case OMP_CLAUSE_PRIORITY:
14024 case OMP_CLAUSE_GRAINSIZE:
14025 case OMP_CLAUSE_NUM_TASKS:
14026 case OMP_CLAUSE_NOGROUP:
14027 case OMP_CLAUSE_THREADS:
14028 case OMP_CLAUSE_SIMD:
14029 case OMP_CLAUSE_HINT:
14030 case OMP_CLAUSE_DEFAULTMAP:
41dbbb37
TS
14031 case OMP_CLAUSE_NUM_GANGS:
14032 case OMP_CLAUSE_NUM_WORKERS:
14033 case OMP_CLAUSE_VECTOR_LENGTH:
14034 case OMP_CLAUSE_ASYNC:
14035 case OMP_CLAUSE_WAIT:
14036 case OMP_CLAUSE_AUTO:
7a5e4956 14037 case OMP_CLAUSE_INDEPENDENT:
41dbbb37
TS
14038 case OMP_CLAUSE_SEQ:
14039 case OMP_CLAUSE_GANG:
14040 case OMP_CLAUSE_WORKER:
14041 case OMP_CLAUSE_VECTOR:
7a5e4956 14042 case OMP_CLAUSE_TILE:
829c6349
CLT
14043 case OMP_CLAUSE_IF_PRESENT:
14044 case OMP_CLAUSE_FINALIZE:
acf0174b
JJ
14045 pc = &OMP_CLAUSE_CHAIN (c);
14046 continue;
14047
e01d41e5
JJ
14048 case OMP_CLAUSE_SCHEDULE:
14049 if (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_NONMONOTONIC)
14050 {
14051 const char *p = NULL;
14052 switch (OMP_CLAUSE_SCHEDULE_KIND (c) & OMP_CLAUSE_SCHEDULE_MASK)
14053 {
14054 case OMP_CLAUSE_SCHEDULE_STATIC: p = "static"; break;
14055 case OMP_CLAUSE_SCHEDULE_DYNAMIC: break;
14056 case OMP_CLAUSE_SCHEDULE_GUIDED: break;
14057 case OMP_CLAUSE_SCHEDULE_AUTO: p = "auto"; break;
14058 case OMP_CLAUSE_SCHEDULE_RUNTIME: p = "runtime"; break;
14059 default: gcc_unreachable ();
14060 }
14061 if (p)
14062 {
14063 error_at (OMP_CLAUSE_LOCATION (c),
14064 "%<nonmonotonic%> modifier specified for %qs "
14065 "schedule kind", p);
14066 OMP_CLAUSE_SCHEDULE_KIND (c)
14067 = (enum omp_clause_schedule_kind)
14068 (OMP_CLAUSE_SCHEDULE_KIND (c)
14069 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14070 }
14071 }
14072 schedule_clause = c;
14073 pc = &OMP_CLAUSE_CHAIN (c);
14074 continue;
14075
14076 case OMP_CLAUSE_ORDERED:
14077 ordered_seen = true;
14078 pc = &OMP_CLAUSE_CHAIN (c);
14079 continue;
14080
d9a6bd32
JJ
14081 case OMP_CLAUSE_SAFELEN:
14082 safelen = c;
14083 pc = &OMP_CLAUSE_CHAIN (c);
14084 continue;
14085 case OMP_CLAUSE_SIMDLEN:
14086 simdlen = c;
14087 pc = &OMP_CLAUSE_CHAIN (c);
14088 continue;
14089
acf0174b
JJ
14090 case OMP_CLAUSE_INBRANCH:
14091 case OMP_CLAUSE_NOTINBRANCH:
14092 if (branch_seen)
14093 {
14094 error_at (OMP_CLAUSE_LOCATION (c),
14095 "%<inbranch%> clause is incompatible with "
14096 "%<notinbranch%>");
14097 remove = true;
14098 break;
14099 }
14100 branch_seen = true;
953ff289
DN
14101 pc = &OMP_CLAUSE_CHAIN (c);
14102 continue;
14103
14104 default:
14105 gcc_unreachable ();
14106 }
14107
14108 if (!remove)
14109 {
14110 t = OMP_CLAUSE_DECL (c);
14111
14112 if (need_complete)
14113 {
4f2e1536 14114 t = require_complete_type (OMP_CLAUSE_LOCATION (c), t);
953ff289
DN
14115 if (t == error_mark_node)
14116 remove = true;
14117 }
14118
14119 if (need_implicitly_determined)
14120 {
14121 const char *share_name = NULL;
14122
0ae9bd27 14123 if (VAR_P (t) && DECL_THREAD_LOCAL_P (t))
953ff289
DN
14124 share_name = "threadprivate";
14125 else switch (c_omp_predetermined_sharing (t))
14126 {
14127 case OMP_CLAUSE_DEFAULT_UNSPECIFIED:
14128 break;
14129 case OMP_CLAUSE_DEFAULT_SHARED:
20906c66
JJ
14130 /* const vars may be specified in firstprivate clause. */
14131 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
14132 && TREE_READONLY (t))
14133 break;
953ff289
DN
14134 share_name = "shared";
14135 break;
14136 case OMP_CLAUSE_DEFAULT_PRIVATE:
14137 share_name = "private";
14138 break;
14139 default:
14140 gcc_unreachable ();
14141 }
14142 if (share_name)
14143 {
c2255bc4
AH
14144 error_at (OMP_CLAUSE_LOCATION (c),
14145 "%qE is predetermined %qs for %qs",
acf0174b
JJ
14146 t, share_name,
14147 omp_clause_code_name[OMP_CLAUSE_CODE (c)]);
953ff289
DN
14148 remove = true;
14149 }
14150 }
14151 }
14152
14153 if (remove)
14154 *pc = OMP_CLAUSE_CHAIN (c);
14155 else
14156 pc = &OMP_CLAUSE_CHAIN (c);
14157 }
14158
d9a6bd32
JJ
14159 if (simdlen
14160 && safelen
14161 && tree_int_cst_lt (OMP_CLAUSE_SAFELEN_EXPR (safelen),
14162 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)))
14163 {
14164 error_at (OMP_CLAUSE_LOCATION (simdlen),
14165 "%<simdlen%> clause value is bigger than "
14166 "%<safelen%> clause value");
14167 OMP_CLAUSE_SIMDLEN_EXPR (simdlen)
14168 = OMP_CLAUSE_SAFELEN_EXPR (safelen);
14169 }
14170
e01d41e5
JJ
14171 if (ordered_seen
14172 && schedule_clause
14173 && (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14174 & OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
14175 {
14176 error_at (OMP_CLAUSE_LOCATION (schedule_clause),
14177 "%<nonmonotonic%> schedule modifier specified together "
14178 "with %<ordered%> clause");
14179 OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14180 = (enum omp_clause_schedule_kind)
14181 (OMP_CLAUSE_SCHEDULE_KIND (schedule_clause)
14182 & ~OMP_CLAUSE_SCHEDULE_NONMONOTONIC);
14183 }
14184
14185 if (linear_variable_step_check)
14186 for (pc = &clauses, c = clauses; c ; c = *pc)
14187 {
14188 bool remove = false;
14189 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR
14190 && OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (c)
14191 && !bitmap_bit_p (&map_head,
14192 DECL_UID (OMP_CLAUSE_LINEAR_STEP (c))))
14193 {
14194 error_at (OMP_CLAUSE_LOCATION (c),
14195 "%<linear%> clause step is a parameter %qD not "
14196 "specified in %<uniform%> clause",
14197 OMP_CLAUSE_LINEAR_STEP (c));
14198 remove = true;
14199 }
14200
14201 if (remove)
14202 *pc = OMP_CLAUSE_CHAIN (c);
14203 else
14204 pc = &OMP_CLAUSE_CHAIN (c);
14205 }
14206
953ff289
DN
14207 bitmap_obstack_release (NULL);
14208 return clauses;
14209}
9ae165a0 14210
9dc5773f
JJ
14211/* Return code to initialize DST with a copy constructor from SRC.
14212 C doesn't have copy constructors nor assignment operators, only for
14213 _Atomic vars we need to perform __atomic_load from src into a temporary
14214 followed by __atomic_store of the temporary to dst. */
14215
14216tree
14217c_omp_clause_copy_ctor (tree clause, tree dst, tree src)
14218{
14219 if (!really_atomic_lvalue (dst) && !really_atomic_lvalue (src))
14220 return build2 (MODIFY_EXPR, TREE_TYPE (dst), dst, src);
14221
14222 location_t loc = OMP_CLAUSE_LOCATION (clause);
14223 tree type = TREE_TYPE (dst);
14224 tree nonatomic_type = build_qualified_type (type, TYPE_UNQUALIFIED);
14225 tree tmp = create_tmp_var (nonatomic_type);
14226 tree tmp_addr = build_fold_addr_expr (tmp);
14227 TREE_ADDRESSABLE (tmp) = 1;
14228 TREE_NO_WARNING (tmp) = 1;
14229 tree src_addr = build_fold_addr_expr (src);
14230 tree dst_addr = build_fold_addr_expr (dst);
14231 tree seq_cst = build_int_cst (integer_type_node, MEMMODEL_SEQ_CST);
14232 vec<tree, va_gc> *params;
14233 /* Expansion of a generic atomic load may require an addition
14234 element, so allocate enough to prevent a resize. */
14235 vec_alloc (params, 4);
14236
14237 /* Build __atomic_load (&src, &tmp, SEQ_CST); */
14238 tree fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_LOAD);
14239 params->quick_push (src_addr);
14240 params->quick_push (tmp_addr);
14241 params->quick_push (seq_cst);
14242 tree load = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14243
14244 vec_alloc (params, 4);
14245
14246 /* Build __atomic_store (&dst, &tmp, SEQ_CST); */
14247 fndecl = builtin_decl_explicit (BUILT_IN_ATOMIC_STORE);
14248 params->quick_push (dst_addr);
14249 params->quick_push (tmp_addr);
14250 params->quick_push (seq_cst);
14251 tree store = c_build_function_call_vec (loc, vNULL, fndecl, params, NULL);
14252 return build2 (COMPOUND_EXPR, void_type_node, load, store);
14253}
14254
0a35513e
AH
14255/* Create a transaction node. */
14256
14257tree
14258c_finish_transaction (location_t loc, tree block, int flags)
14259{
14260 tree stmt = build_stmt (loc, TRANSACTION_EXPR, block);
14261 if (flags & TM_STMT_ATTR_OUTER)
14262 TRANSACTION_EXPR_OUTER (stmt) = 1;
14263 if (flags & TM_STMT_ATTR_RELAXED)
14264 TRANSACTION_EXPR_RELAXED (stmt) = 1;
14265 return add_stmt (stmt);
14266}
14267
9ae165a0 14268/* Make a variant type in the proper way for C/C++, propagating qualifiers
e9e32ee6
JM
14269 down to the element type of an array. If ORIG_QUAL_TYPE is not
14270 NULL, then it should be used as the qualified type
14271 ORIG_QUAL_INDIRECT levels down in array type derivation (to
14272 preserve information about the typedef name from which an array
14273 type was derived). */
9ae165a0
DG
14274
14275tree
e9e32ee6
JM
14276c_build_qualified_type (tree type, int type_quals, tree orig_qual_type,
14277 size_t orig_qual_indirect)
9ae165a0
DG
14278{
14279 if (type == error_mark_node)
14280 return type;
14281
14282 if (TREE_CODE (type) == ARRAY_TYPE)
14283 {
14284 tree t;
14285 tree element_type = c_build_qualified_type (TREE_TYPE (type),
e9e32ee6
JM
14286 type_quals, orig_qual_type,
14287 orig_qual_indirect - 1);
9ae165a0
DG
14288
14289 /* See if we already have an identically qualified type. */
e9e32ee6
JM
14290 if (orig_qual_type && orig_qual_indirect == 0)
14291 t = orig_qual_type;
14292 else
14293 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
14294 {
14295 if (TYPE_QUALS (strip_array_types (t)) == type_quals
14296 && TYPE_NAME (t) == TYPE_NAME (type)
14297 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
14298 && attribute_list_equal (TYPE_ATTRIBUTES (t),
14299 TYPE_ATTRIBUTES (type)))
14300 break;
14301 }
9ae165a0
DG
14302 if (!t)
14303 {
14304 tree domain = TYPE_DOMAIN (type);
14305
14306 t = build_variant_type_copy (type);
14307 TREE_TYPE (t) = element_type;
14308
14309 if (TYPE_STRUCTURAL_EQUALITY_P (element_type)
14310 || (domain && TYPE_STRUCTURAL_EQUALITY_P (domain)))
14311 SET_TYPE_STRUCTURAL_EQUALITY (t);
14312 else if (TYPE_CANONICAL (element_type) != element_type
14313 || (domain && TYPE_CANONICAL (domain) != domain))
14314 {
b8698a0f 14315 tree unqualified_canon
9ae165a0 14316 = build_array_type (TYPE_CANONICAL (element_type),
b8698a0f 14317 domain? TYPE_CANONICAL (domain)
9ae165a0 14318 : NULL_TREE);
ee45a32d
EB
14319 if (TYPE_REVERSE_STORAGE_ORDER (type))
14320 {
14321 unqualified_canon
14322 = build_distinct_type_copy (unqualified_canon);
14323 TYPE_REVERSE_STORAGE_ORDER (unqualified_canon) = 1;
14324 }
b8698a0f 14325 TYPE_CANONICAL (t)
9ae165a0
DG
14326 = c_build_qualified_type (unqualified_canon, type_quals);
14327 }
14328 else
14329 TYPE_CANONICAL (t) = t;
14330 }
14331 return t;
14332 }
14333
14334 /* A restrict-qualified pointer type must be a pointer to object or
14335 incomplete type. Note that the use of POINTER_TYPE_P also allows
14336 REFERENCE_TYPEs, which is appropriate for C++. */
14337 if ((type_quals & TYPE_QUAL_RESTRICT)
14338 && (!POINTER_TYPE_P (type)
14339 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
14340 {
14341 error ("invalid use of %<restrict%>");
14342 type_quals &= ~TYPE_QUAL_RESTRICT;
14343 }
14344
e9e32ee6
JM
14345 tree var_type = (orig_qual_type && orig_qual_indirect == 0
14346 ? orig_qual_type
14347 : build_qualified_type (type, type_quals));
e922069e
JW
14348 /* A variant type does not inherit the list of incomplete vars from the
14349 type main variant. */
152ef731
JJ
14350 if (RECORD_OR_UNION_TYPE_P (var_type)
14351 && TYPE_MAIN_VARIANT (var_type) != var_type)
e922069e
JW
14352 C_TYPE_INCOMPLETE_VARS (var_type) = 0;
14353 return var_type;
9ae165a0 14354}
72b5577d
ILT
14355
14356/* Build a VA_ARG_EXPR for the C parser. */
14357
14358tree
f187980b 14359c_build_va_arg (location_t loc1, tree expr, location_t loc2, tree type)
72b5577d 14360{
ec3fba51
MP
14361 if (error_operand_p (type))
14362 return error_mark_node;
f187980b
EB
14363 /* VA_ARG_EXPR cannot be used for a scalar va_list with reverse storage
14364 order because it takes the address of the expression. */
14365 else if (handled_component_p (expr)
14366 && reverse_storage_order_for_component_p (expr))
14367 {
14368 error_at (loc1, "cannot use %<va_arg%> with reverse storage order");
14369 return error_mark_node;
14370 }
ec3fba51 14371 else if (!COMPLETE_TYPE_P (type))
4e81b788 14372 {
f187980b 14373 error_at (loc2, "second argument to %<va_arg%> is of incomplete "
ec3fba51 14374 "type %qT", type);
4e81b788
MP
14375 return error_mark_node;
14376 }
ec3fba51 14377 else if (warn_cxx_compat && TREE_CODE (type) == ENUMERAL_TYPE)
f187980b 14378 warning_at (loc2, OPT_Wc___compat,
ec3fba51 14379 "C++ requires promoted type, not enum type, in %<va_arg%>");
f187980b 14380 return build_va_arg (loc2, expr, type);
72b5577d 14381}
acf0174b
JJ
14382
14383/* Return truthvalue of whether T1 is the same tree structure as T2.
6574d78e 14384 Return 1 if they are the same. Return false if they are different. */
acf0174b
JJ
14385
14386bool
14387c_tree_equal (tree t1, tree t2)
14388{
14389 enum tree_code code1, code2;
14390
14391 if (t1 == t2)
14392 return true;
14393 if (!t1 || !t2)
14394 return false;
14395
14396 for (code1 = TREE_CODE (t1);
14397 CONVERT_EXPR_CODE_P (code1)
14398 || code1 == NON_LVALUE_EXPR;
14399 code1 = TREE_CODE (t1))
14400 t1 = TREE_OPERAND (t1, 0);
14401 for (code2 = TREE_CODE (t2);
14402 CONVERT_EXPR_CODE_P (code2)
14403 || code2 == NON_LVALUE_EXPR;
14404 code2 = TREE_CODE (t2))
14405 t2 = TREE_OPERAND (t2, 0);
14406
14407 /* They might have become equal now. */
14408 if (t1 == t2)
14409 return true;
14410
14411 if (code1 != code2)
14412 return false;
14413
14414 switch (code1)
14415 {
14416 case INTEGER_CST:
8e6cdc90 14417 return wi::to_wide (t1) == wi::to_wide (t2);
acf0174b
JJ
14418
14419 case REAL_CST:
624d31fe 14420 return real_equal (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
acf0174b
JJ
14421
14422 case STRING_CST:
14423 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
14424 && !memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
14425 TREE_STRING_LENGTH (t1));
14426
14427 case FIXED_CST:
14428 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1),
14429 TREE_FIXED_CST (t2));
14430
14431 case COMPLEX_CST:
14432 return c_tree_equal (TREE_REALPART (t1), TREE_REALPART (t2))
14433 && c_tree_equal (TREE_IMAGPART (t1), TREE_IMAGPART (t2));
14434
14435 case VECTOR_CST:
14436 return operand_equal_p (t1, t2, OEP_ONLY_CONST);
14437
14438 case CONSTRUCTOR:
14439 /* We need to do this when determining whether or not two
14440 non-type pointer to member function template arguments
14441 are the same. */
14442 if (!comptypes (TREE_TYPE (t1), TREE_TYPE (t2))
14443 || CONSTRUCTOR_NELTS (t1) != CONSTRUCTOR_NELTS (t2))
14444 return false;
14445 {
14446 tree field, value;
14447 unsigned int i;
14448 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t1), i, field, value)
14449 {
14450 constructor_elt *elt2 = CONSTRUCTOR_ELT (t2, i);
14451 if (!c_tree_equal (field, elt2->index)
14452 || !c_tree_equal (value, elt2->value))
14453 return false;
14454 }
14455 }
14456 return true;
14457
14458 case TREE_LIST:
14459 if (!c_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)))
14460 return false;
14461 if (!c_tree_equal (TREE_VALUE (t1), TREE_VALUE (t2)))
14462 return false;
14463 return c_tree_equal (TREE_CHAIN (t1), TREE_CHAIN (t2));
14464
14465 case SAVE_EXPR:
14466 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14467
14468 case CALL_EXPR:
14469 {
14470 tree arg1, arg2;
14471 call_expr_arg_iterator iter1, iter2;
14472 if (!c_tree_equal (CALL_EXPR_FN (t1), CALL_EXPR_FN (t2)))
14473 return false;
14474 for (arg1 = first_call_expr_arg (t1, &iter1),
14475 arg2 = first_call_expr_arg (t2, &iter2);
14476 arg1 && arg2;
14477 arg1 = next_call_expr_arg (&iter1),
14478 arg2 = next_call_expr_arg (&iter2))
14479 if (!c_tree_equal (arg1, arg2))
14480 return false;
14481 if (arg1 || arg2)
14482 return false;
14483 return true;
14484 }
14485
14486 case TARGET_EXPR:
14487 {
14488 tree o1 = TREE_OPERAND (t1, 0);
14489 tree o2 = TREE_OPERAND (t2, 0);
14490
14491 /* Special case: if either target is an unallocated VAR_DECL,
14492 it means that it's going to be unified with whatever the
14493 TARGET_EXPR is really supposed to initialize, so treat it
14494 as being equivalent to anything. */
0ae9bd27 14495 if (VAR_P (o1) && DECL_NAME (o1) == NULL_TREE
acf0174b
JJ
14496 && !DECL_RTL_SET_P (o1))
14497 /*Nop*/;
0ae9bd27 14498 else if (VAR_P (o2) && DECL_NAME (o2) == NULL_TREE
acf0174b
JJ
14499 && !DECL_RTL_SET_P (o2))
14500 /*Nop*/;
14501 else if (!c_tree_equal (o1, o2))
14502 return false;
14503
14504 return c_tree_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
14505 }
14506
14507 case COMPONENT_REF:
14508 if (TREE_OPERAND (t1, 1) != TREE_OPERAND (t2, 1))
14509 return false;
14510 return c_tree_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
14511
14512 case PARM_DECL:
14513 case VAR_DECL:
14514 case CONST_DECL:
14515 case FIELD_DECL:
14516 case FUNCTION_DECL:
14517 case IDENTIFIER_NODE:
14518 case SSA_NAME:
14519 return false;
14520
14521 case TREE_VEC:
14522 {
14523 unsigned ix;
14524 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
14525 return false;
14526 for (ix = TREE_VEC_LENGTH (t1); ix--;)
14527 if (!c_tree_equal (TREE_VEC_ELT (t1, ix),
14528 TREE_VEC_ELT (t2, ix)))
14529 return false;
14530 return true;
14531 }
14532
14533 default:
14534 break;
14535 }
14536
14537 switch (TREE_CODE_CLASS (code1))
14538 {
14539 case tcc_unary:
14540 case tcc_binary:
14541 case tcc_comparison:
14542 case tcc_expression:
14543 case tcc_vl_exp:
14544 case tcc_reference:
14545 case tcc_statement:
14546 {
14547 int i, n = TREE_OPERAND_LENGTH (t1);
14548
14549 switch (code1)
14550 {
14551 case PREINCREMENT_EXPR:
14552 case PREDECREMENT_EXPR:
14553 case POSTINCREMENT_EXPR:
14554 case POSTDECREMENT_EXPR:
14555 n = 1;
14556 break;
14557 case ARRAY_REF:
14558 n = 2;
14559 break;
14560 default:
14561 break;
14562 }
14563
14564 if (TREE_CODE_CLASS (code1) == tcc_vl_exp
14565 && n != TREE_OPERAND_LENGTH (t2))
14566 return false;
14567
14568 for (i = 0; i < n; ++i)
14569 if (!c_tree_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i)))
14570 return false;
14571
14572 return true;
14573 }
14574
14575 case tcc_type:
14576 return comptypes (t1, t2);
14577 default:
14578 gcc_unreachable ();
14579 }
14580 /* We can get here with --disable-checking. */
14581 return false;
14582}
12893402 14583
1807ffc1
MS
14584/* Returns true when the function declaration FNDECL is implicit,
14585 introduced as a result of a call to an otherwise undeclared
14586 function, and false otherwise. */
14587
14588bool
14589c_decl_implicit (const_tree fndecl)
14590{
14591 return C_DECL_IMPLICIT (fndecl);
14592}